Annotation of src/sys/netinet/udp_usrreq.c, Revision 1.55
1.55 ! itojun 1: /* $NetBSD: udp_usrreq.c,v 1.54 1999/12/22 04:03:01 itojun Exp $ */
1.48 itojun 2:
3: /*
4: * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5: * All rights reserved.
6: *
7: * Redistribution and use in source and binary forms, with or without
8: * modification, are permitted provided that the following conditions
9: * are met:
10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
12: * 2. Redistributions in binary form must reproduce the above copyright
13: * notice, this list of conditions and the following disclaimer in the
14: * documentation and/or other materials provided with the distribution.
15: * 3. Neither the name of the project nor the names of its contributors
16: * may be used to endorse or promote products derived from this software
17: * without specific prior written permission.
18: *
19: * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22: * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29: * SUCH DAMAGE.
30: */
1.14 cgd 31:
1.1 cgd 32: /*
1.44 thorpej 33: * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
1.13 mycroft 34: * The Regents of the University of California. All rights reserved.
1.1 cgd 35: *
36: * Redistribution and use in source and binary forms, with or without
37: * modification, are permitted provided that the following conditions
38: * are met:
39: * 1. Redistributions of source code must retain the above copyright
40: * notice, this list of conditions and the following disclaimer.
41: * 2. Redistributions in binary form must reproduce the above copyright
42: * notice, this list of conditions and the following disclaimer in the
43: * documentation and/or other materials provided with the distribution.
44: * 3. All advertising materials mentioning features or use of this software
45: * must display the following acknowledgement:
46: * This product includes software developed by the University of
47: * California, Berkeley and its contributors.
48: * 4. Neither the name of the University nor the names of its contributors
49: * may be used to endorse or promote products derived from this software
50: * without specific prior written permission.
51: *
52: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62: * SUCH DAMAGE.
63: *
1.44 thorpej 64: * @(#)udp_usrreq.c 8.6 (Berkeley) 5/23/95
1.1 cgd 65: */
1.50 thorpej 66:
67: #include "opt_ipsec.h"
68:
1.38 ws 69: #include "ipkdb.h"
1.1 cgd 70:
1.5 mycroft 71: #include <sys/param.h>
72: #include <sys/malloc.h>
73: #include <sys/mbuf.h>
74: #include <sys/protosw.h>
75: #include <sys/socket.h>
76: #include <sys/socketvar.h>
1.13 mycroft 77: #include <sys/errno.h>
1.5 mycroft 78: #include <sys/stat.h>
1.27 christos 79: #include <sys/systm.h>
80: #include <sys/proc.h>
1.53 itojun 81: #include <sys/domain.h>
1.27 christos 82:
83: #include <vm/vm.h>
84: #include <sys/sysctl.h>
1.1 cgd 85:
1.5 mycroft 86: #include <net/if.h>
87: #include <net/route.h>
1.1 cgd 88:
1.5 mycroft 89: #include <netinet/in.h>
90: #include <netinet/in_systm.h>
1.15 cgd 91: #include <netinet/in_var.h>
1.5 mycroft 92: #include <netinet/ip.h>
93: #include <netinet/in_pcb.h>
94: #include <netinet/ip_var.h>
95: #include <netinet/ip_icmp.h>
96: #include <netinet/udp.h>
97: #include <netinet/udp_var.h>
1.1 cgd 98:
1.53 itojun 99: #ifdef INET6
100: #include <netinet/ip6.h>
101: #include <netinet/icmp6.h>
102: #include <netinet6/ip6_var.h>
103: #include <netinet6/in6_pcb.h>
104: #include <netinet6/udp6_var.h>
105: #endif
106:
107: #ifdef PULLDOWN_TEST
108: #ifndef INET6
109: /* always need ip6.h for IP6_EXTHDR_GET */
110: #include <netinet/ip6.h>
111: #endif
112: #endif
113:
1.27 christos 114: #include <machine/stdarg.h>
115:
1.48 itojun 116: #ifdef IPSEC
117: #include <netinet6/ipsec.h>
118: #include <netkey/key.h>
119: #include <netkey/key_debug.h>
120: #endif /*IPSEC*/
121:
1.8 mycroft 122: /*
123: * UDP protocol implementation.
124: * Per RFC 768, August, 1980.
125: */
126: #ifndef COMPAT_42
127: int udpcksum = 1;
128: #else
129: int udpcksum = 0; /* XXX */
130: #endif
131:
1.53 itojun 132: static void udp4_sendup __P((struct mbuf *, int, struct sockaddr *,
133: struct socket *));
134: static int udp4_realinput __P((struct sockaddr_in *, struct sockaddr_in *,
135: struct mbuf *, int));
136: #ifdef INET6
137: static void udp6_sendup __P((struct mbuf *, int, struct sockaddr *,
138: struct socket *));
139: static int in6_mcmatch __P((struct in6pcb *, struct in6_addr *,
140: struct ifnet *));
141: static int udp6_realinput __P((int, struct sockaddr_in6 *,
142: struct sockaddr_in6 *, struct mbuf *, int));
143: #endif
1.13 mycroft 144: static void udp_notify __P((struct inpcb *, int));
1.7 mycroft 145:
1.26 mycroft 146: #ifndef UDBHASHSIZE
147: #define UDBHASHSIZE 128
148: #endif
149: int udbhashsize = UDBHASHSIZE;
150:
1.7 mycroft 151: void
1.1 cgd 152: udp_init()
153: {
1.18 mycroft 154:
1.35 mycroft 155: in_pcbinit(&udbtable, udbhashsize, udbhashsize);
1.1 cgd 156: }
157:
1.7 mycroft 158: void
1.27 christos 159: #if __STDC__
160: udp_input(struct mbuf *m, ...)
161: #else
162: udp_input(m, va_alist)
163: struct mbuf *m;
164: va_dcl
165: #endif
1.1 cgd 166: {
1.53 itojun 167: va_list ap;
168: struct sockaddr_in src, dst;
169: struct ip *ip;
170: struct udphdr *uh;
171: int iphlen, proto;
172: int len;
173: int n;
174:
175: va_start(ap, m);
176: iphlen = va_arg(ap, int);
177: proto = va_arg(ap, int);
178: va_end(ap);
179:
180: udpstat.udps_ipackets++;
181:
182: #ifndef PULLDOWN_TEST
183: /*
184: * Strip IP options, if any; should skip this,
185: * make available to user, and use on returned packets,
186: * but we don't yet have a way to check the checksum
187: * with options still present.
188: */
189: if (iphlen > sizeof (struct ip)) {
190: ip_stripoptions(m, (struct mbuf *)0);
191: iphlen = sizeof(struct ip);
192: }
193: #else
194: /*
195: * we may enable the above code if we save and pass IPv4 options
196: * to the userland.
197: */
198: #endif
199:
200: /*
201: * Get IP and UDP header together in first mbuf.
202: */
203: ip = mtod(m, struct ip *);
204: #ifndef PULLDOWN_TEST
205: if (m->m_len < iphlen + sizeof(struct udphdr)) {
206: if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) {
207: udpstat.udps_hdrops++;
208: return;
209: }
210: ip = mtod(m, struct ip *);
211: }
212: uh = (struct udphdr *)((caddr_t)ip + iphlen);
213: #else
214: IP6_EXTHDR_GET(uh, struct udphdr *, m, iphlen, sizeof(struct udphdr));
215: if (uh == NULL) {
216: udpstat.udps_hdrops++;
217: return;
218: }
219: #endif
220:
221: /*
222: * Make mbuf data length reflect UDP length.
223: * If not enough data to reflect UDP length, drop.
224: */
225: len = ntohs((u_int16_t)uh->uh_ulen);
226: if (ip->ip_len != iphlen + len) {
227: if (ip->ip_len < iphlen + len) {
228: udpstat.udps_badlen++;
229: goto bad;
230: }
231: m_adj(m, iphlen + len - ip->ip_len);
232: }
233:
234: /*
235: * Checksum extended UDP header and data.
236: */
237: if (uh->uh_sum) {
238: #ifndef PULLDOWN_TEST
239: struct ip save_ip;
240:
241: /*
242: * Save a copy of the IP header in case we want restore it
243: * for sending an ICMP error message in response.
244: */
245: save_ip = *ip;
246:
247: bzero(((struct ipovly *)ip)->ih_x1,
248: sizeof ((struct ipovly *)ip)->ih_x1);
249: ((struct ipovly *)ip)->ih_len = uh->uh_ulen;
250: if (in_cksum(m, len + sizeof (struct ip)) != 0) {
251: udpstat.udps_badsum++;
252: m_freem(m);
253: return;
254: }
255:
256: *ip = save_ip;
257: #else
258: if (in4_cksum(m, IPPROTO_UDP, iphlen, len) != 0) {
259: udpstat.udps_badsum++;
260: m_freem(m);
261: return;
262: }
263: #endif
264: }
265:
266: /* construct source and dst sockaddrs. */
267: bzero(&src, sizeof(src));
268: src.sin_family = AF_INET;
269: src.sin_len = sizeof(struct sockaddr_in);
270: bcopy(&ip->ip_src, &src.sin_addr, sizeof(src.sin_addr));
271: src.sin_port = uh->uh_sport;
272: bzero(&dst, sizeof(dst));
273: dst.sin_family = AF_INET;
274: dst.sin_len = sizeof(struct sockaddr_in);
275: bcopy(&ip->ip_dst, &dst.sin_addr, sizeof(dst.sin_addr));
276: dst.sin_port = uh->uh_dport;
277:
278: n = udp4_realinput(&src, &dst, m, iphlen);
279: #ifdef INET6
280: if (IN_MULTICAST(ip->ip_dst.s_addr) || n == 0) {
281: struct sockaddr_in6 src6, dst6;
282:
283: bzero(&src6, sizeof(src6));
284: src6.sin6_family = AF_INET6;
285: src6.sin6_len = sizeof(struct sockaddr_in6);
286: src6.sin6_addr.s6_addr[10] = src6.sin6_addr.s6_addr[11] = 0xff;
287: bcopy(&ip->ip_src, &src6.sin6_addr.s6_addr[12],
288: sizeof(ip->ip_src));
289: src6.sin6_port = uh->uh_sport;
290: bzero(&dst6, sizeof(dst6));
291: dst6.sin6_family = AF_INET6;
292: dst6.sin6_len = sizeof(struct sockaddr_in6);
293: dst6.sin6_addr.s6_addr[10] = dst6.sin6_addr.s6_addr[11] = 0xff;
294: bcopy(&ip->ip_dst, &dst6.sin6_addr.s6_addr[12],
295: sizeof(ip->ip_dst));
296: dst6.sin6_port = uh->uh_dport;
297:
298: n += udp6_realinput(AF_INET, &src6, &dst6, m, iphlen);
299: }
300: #endif
301:
302: if (n == 0) {
303: udpstat.udps_noport++;
304: if (m->m_flags & (M_BCAST | M_MCAST)) {
305: udpstat.udps_noportbcast++;
306: goto bad;
307: }
308: #if NIPKDB > 0
309: if (checkipkdb(&ip->ip_src, uh->uh_sport, uh->uh_dport,
310: m, iphlen + sizeof(struct udphdr),
311: m->m_pkthdr.len - iphlen - sizeof(struct udphdr))) {
312: /*
313: * It was a debugger connect packet,
314: * just drop it now
315: */
316: goto bad;
317: }
318: #endif
319: icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
320: m = NULL;
321: }
322:
323: bad:
324: if (m)
325: m_freem(m);
326: }
327:
328: #ifdef INET6
329: int
330: udp6_input(mp, offp, proto)
331: struct mbuf **mp;
332: int *offp, proto;
333: {
334: struct mbuf *m = *mp;
335: int off = *offp;
336: struct sockaddr_in6 src, dst;
337: struct ip6_hdr *ip6;
338: struct udphdr *uh;
339: u_int32_t plen, ulen;
340:
341: #if defined(NFAITH) && 0 < NFAITH
342: if (m->m_pkthdr.rcvif) {
343: if (m->m_pkthdr.rcvif->if_type == IFT_FAITH) {
344: /* send icmp6 host unreach? */
345: m_freem(m);
346: return IPPROTO_DONE;
347: }
348: }
349: #endif
350:
351: udp6stat.udp6s_ipackets++;
352:
353: #ifndef PULLDOWN_TEST
354: IP6_EXTHDR_CHECK(m, off, sizeof(struct udphdr), IPPROTO_DONE);
355: #endif
356:
357: ip6 = mtod(m, struct ip6_hdr *);
358: /* check for jumbogram is done in ip6_input. we can trust pkthdr.len */
359: plen = m->m_pkthdr.len - off;
360: #ifndef PULLDOWN_TEST
361: uh = (struct udphdr *)((caddr_t)ip6 + off);
362: #else
363: IP6_EXTHDR_GET(uh, struct udphdr *, m, off, sizeof(struct udphdr));
364: if (uh == NULL) {
365: ip6stat.ip6s_tooshort++;
366: return IPPROTO_DONE;
367: }
368: #endif
369: ulen = ntohs((u_short)uh->uh_ulen);
370: if (ulen == 0 && plen > 0xffff)
371: ulen = plen;
372:
373: if (plen != ulen) {
374: udp6stat.udp6s_badlen++;
1.54 itojun 375: goto bad;
376: }
377:
378: /* Be proactive about malicious use of IPv4 mapped address */
379: if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
380: IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
381: /* XXX stat */
1.53 itojun 382: goto bad;
383: }
384:
385: /*
386: * Checksum extended UDP header and data.
387: */
388: if (uh->uh_sum == 0)
389: udp6stat.udp6s_nosum++;
390: else if (in6_cksum(m, IPPROTO_UDP, off, ulen) != 0) {
391: udp6stat.udp6s_badsum++;
392: goto bad;
393: }
394:
395: /*
396: * Construct source and dst sockaddrs.
397: * Note that ifindex (s6_addr16[1]) is already filled.
398: */
399: bzero(&src, sizeof(src));
400: src.sin6_family = AF_INET6;
401: src.sin6_len = sizeof(struct sockaddr_in6);
402: bcopy(&ip6->ip6_src, &src.sin6_addr, sizeof(src.sin6_addr));
403: if (IN6_IS_SCOPE_LINKLOCAL(&src.sin6_addr))
404: src.sin6_addr.s6_addr16[1] = 0;
405: if (m->m_pkthdr.rcvif) {
406: if (IN6_IS_SCOPE_LINKLOCAL(&src.sin6_addr))
407: src.sin6_scope_id = m->m_pkthdr.rcvif->if_index;
408: else
409: src.sin6_scope_id = 0;
410: }
411: src.sin6_port = uh->uh_sport;
412: bzero(&dst, sizeof(dst));
413: dst.sin6_family = AF_INET6;
414: dst.sin6_len = sizeof(struct sockaddr_in6);
415: bcopy(&ip6->ip6_dst, &dst.sin6_addr, sizeof(dst.sin6_addr));
416: if (IN6_IS_SCOPE_LINKLOCAL(&dst.sin6_addr))
417: dst.sin6_addr.s6_addr16[1] = 0;
418: if (m->m_pkthdr.rcvif) {
419: if (IN6_IS_SCOPE_LINKLOCAL(&dst.sin6_addr))
420: dst.sin6_scope_id = m->m_pkthdr.rcvif->if_index;
421: else
422: dst.sin6_scope_id = 0;
423: }
424: dst.sin6_port = uh->uh_dport;
425:
426: if (udp6_realinput(AF_INET6, &src, &dst, m, off) == 0) {
427: udp6stat.udp6s_noport++;
428: if (m->m_flags & M_MCAST) {
429: udp6stat.udp6s_noportmcast++;
430: goto bad;
431: }
432: icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
433: m = NULL;
434: }
435:
436: bad:
437: if (m)
438: m_freem(m);
439: return IPPROTO_DONE;
440: }
441: #endif
442:
443: static void
444: udp4_sendup(m, off, src, so)
445: struct mbuf *m;
446: int off; /* offset of data portion */
447: struct sockaddr *src;
448: struct socket *so;
449: {
450: struct mbuf *opts = NULL;
451: struct mbuf *n;
452: struct inpcb *inp = NULL;
453: #ifdef INET6
454: struct in6pcb *in6p = NULL;
455: #endif
456:
457: if (!so)
458: return;
459: switch (so->so_proto->pr_domain->dom_family) {
460: case AF_INET:
461: inp = sotoinpcb(so);
462: break;
463: #ifdef INET6
464: case AF_INET6:
465: in6p = sotoin6pcb(so);
466: break;
467: #endif
468: default:
469: return;
470: }
471:
472: #ifdef IPSEC
473: /* check AH/ESP integrity. */
474: if (so != NULL && ipsec4_in_reject_so(m, so)) {
475: ipsecstat.in_polvio++;
476: return;
477: }
478: #endif /*IPSEC*/
479:
480: if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
481: if (inp && (inp->inp_flags & INP_CONTROLOPTS
482: || so->so_options & SO_TIMESTAMP)) {
483: struct ip *ip = mtod(n, struct ip *);
484: ip_savecontrol(inp, &opts, ip, n);
485: }
486:
487: m_adj(n, off);
488: if (sbappendaddr(&so->so_rcv, src, n,
489: opts) == 0) {
490: m_freem(n);
491: if (opts)
492: m_freem(opts);
493: } else
494: sorwakeup(so);
495: }
496: }
497:
498: #ifdef INET6
499: static void
500: udp6_sendup(m, off, src, so)
501: struct mbuf *m;
502: int off; /* offset of data portion */
503: struct sockaddr *src;
504: struct socket *so;
505: {
506: struct mbuf *opts = NULL;
507: struct mbuf *n;
508: struct in6pcb *in6p = NULL;
509:
510: if (!so)
511: return;
512: if (so->so_proto->pr_domain->dom_family != AF_INET6)
513: return;
514: in6p = sotoin6pcb(so);
515:
516: #ifdef IPSEC
517: /* check AH/ESP integrity. */
518: if (so != NULL && ipsec6_in_reject_so(m, so)) {
519: ipsec6stat.in_polvio++;
520: return;
521: }
522: #endif /*IPSEC*/
523:
524: if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
525: if (in6p && (in6p->in6p_flags & IN6P_CONTROLOPTS
526: || in6p->in6p_socket->so_options & SO_TIMESTAMP)) {
527: struct ip6_hdr *ip6 = mtod(n, struct ip6_hdr *);
528: ip6_savecontrol(in6p, &opts, ip6, n);
529: }
530:
531: m_adj(n, off);
532: if (sbappendaddr(&so->so_rcv, src, n, opts) == 0) {
533: m_freem(n);
534: if (opts)
535: m_freem(opts);
536: udp6stat.udp6s_fullsock++;
537: } else
538: sorwakeup(so);
539: }
540: }
541: #endif
542:
543: static int
544: udp4_realinput(src, dst, m, off)
545: struct sockaddr_in *src;
546: struct sockaddr_in *dst;
547: struct mbuf *m;
548: int off; /* offset of udphdr */
549: {
550: u_int16_t *sport, *dport;
551: int rcvcnt;
552: struct in_addr *src4, *dst4;
553: struct inpcb *inp;
554:
555: rcvcnt = 0;
556: off += sizeof(struct udphdr); /* now, offset of payload */
557:
558: if (src->sin_family != AF_INET || dst->sin_family != AF_INET)
559: goto bad;
560:
561: src4 = &src->sin_addr;
562: sport = &src->sin_port;
563: dst4 = &dst->sin_addr;
564: dport = &dst->sin_port;
565:
566: if (IN_MULTICAST(src4->s_addr) ||
567: in_broadcast(*dst4, m->m_pkthdr.rcvif)) {
568: struct inpcb *last;
569: /*
570: * Deliver a multicast or broadcast datagram to *all* sockets
571: * for which the local and remote addresses and ports match
572: * those of the incoming datagram. This allows more than
573: * one process to receive multi/broadcasts on the same port.
574: * (This really ought to be done for unicast datagrams as
575: * well, but that would cause problems with existing
576: * applications that open both address-specific sockets and
577: * a wildcard socket listening to the same port -- they would
578: * end up receiving duplicates of every unicast datagram.
579: * Those applications open the multiple sockets to overcome an
580: * inadequacy of the UDP socket interface, but for backwards
581: * compatibility we avoid the problem here rather than
582: * fixing the interface. Maybe 4.5BSD will remedy this?)
583: */
584:
585: /*
586: * KAME note: usually we drop udpiphdr from mbuf here.
587: * we need udpiphdr for iPsec processing so we do that later.
588: */
589: /*
590: * Locate pcb(s) for datagram.
591: */
592: for (inp = udbtable.inpt_queue.cqh_first;
593: inp != (struct inpcb *)&udbtable.inpt_queue;
594: inp = inp->inp_queue.cqe_next) {
595: if (inp->inp_lport != *dport)
596: continue;
597: if (!in_nullhost(inp->inp_laddr)) {
598: if (!in_hosteq(inp->inp_laddr, *dst4))
599: continue;
600: }
601: if (!in_nullhost(inp->inp_faddr)) {
602: if (!in_hosteq(inp->inp_faddr, *src4) ||
603: inp->inp_fport != *sport)
604: continue;
605: }
606:
607: last = inp;
608: udp4_sendup(m, off, (struct sockaddr *)src,
609: inp->inp_socket);
610: rcvcnt++;
611:
612: /*
613: * Don't look for additional matches if this one does
614: * not have either the SO_REUSEPORT or SO_REUSEADDR
615: * socket options set. This heuristic avoids searching
616: * through all pcbs in the common case of a non-shared
617: * port. It assumes that an application will never
618: * clear these options after setting them.
619: */
620: if ((inp->inp_socket->so_options &
621: (SO_REUSEPORT|SO_REUSEADDR)) == 0)
622: break;
623: }
624:
625: #if 0
626: if (last == NULL) {
627: /*
628: * No matching pcb found; discard datagram.
629: * (No need to send an ICMP Port Unreachable
630: * for a broadcast or multicast datgram.)
631: */
632: udpstat.udps_noport++;
633: udpstat.udps_noportbcast++;
634: goto bad;
635: }
636: #endif
637: } else {
638: /*
639: * Locate pcb for datagram.
640: */
641: inp = in_pcblookup_connect(&udbtable, *src4, *sport, *dst4, *dport);
642: if (inp == 0) {
643: ++udpstat.udps_pcbhashmiss;
644: inp = in_pcblookup_bind(&udbtable, *dst4, *dport);
645: if (inp == 0) {
646: #if 0
647: struct mbuf *n;
648:
649: udpstat.udps_noport++;
650: if (m->m_flags & (M_BCAST | M_MCAST)) {
651: udpstat.udps_noportbcast++;
652: goto bad;
653: }
654: #if NIPKDB > 0
655: if (checkipkdb(src4, *sport, *dport, m, off,
656: m->m_pkthdr.len - off)) {
657: /*
658: * It was a debugger connect packet,
659: * just drop it now
660: */
661: goto bad;
662: }
663: #endif
664: if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
665: icmp_error(n, ICMP_UNREACH,
666: ICMP_UNREACH_PORT, 0, 0);
667: }
668: #endif
669: return rcvcnt;
670: }
671: }
672:
673: udp4_sendup(m, off, (struct sockaddr *)src, inp->inp_socket);
674: rcvcnt++;
675: }
676:
677: bad:
678: return rcvcnt;
679: }
680:
681: #ifdef INET6
682: static int
683: in6_mcmatch(in6p, ia6, ifp)
684: struct in6pcb *in6p;
685: register struct in6_addr *ia6;
686: struct ifnet *ifp;
687: {
688: struct ip6_moptions *im6o = in6p->in6p_moptions;
689: struct in6_multi_mship *imm;
690:
691: if (im6o == NULL)
692: return 0;
693:
694: for (imm = im6o->im6o_memberships.lh_first; imm != NULL;
695: imm = imm->i6mm_chain.le_next) {
696: if ((ifp == NULL ||
697: imm->i6mm_maddr->in6m_ifp == ifp) &&
698: IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
699: ia6))
700: return 1;
701: }
702: return 0;
703: }
704:
705: static int
706: udp6_realinput(af, src, dst, m, off)
707: int af; /* af on packet */
708: struct sockaddr_in6 *src;
709: struct sockaddr_in6 *dst;
710: struct mbuf *m;
711: int off; /* offset of udphdr */
712: {
713: u_int16_t *sport, *dport;
714: int rcvcnt;
715: struct in6_addr *src6, *dst6;
716: struct in_addr *src4;
717: struct in6pcb *in6p;
718:
719: rcvcnt = 0;
720: off += sizeof(struct udphdr); /* now, offset of payload */
721:
722: if (af != AF_INET && af != AF_INET6)
723: goto bad;
724: if (src->sin6_family != AF_INET6 || dst->sin6_family != AF_INET6)
725: goto bad;
726:
727: src6 = &src->sin6_addr;
728: sport = &src->sin6_port;
729: dst6 = &dst->sin6_addr;
730: dport = &dst->sin6_port;
731: src4 = (struct in_addr *)&src->sin6_addr.s6_addr32[12];
732:
733: if (IN6_IS_ADDR_MULTICAST(dst6)
734: || (af == AF_INET && IN_MULTICAST(src4->s_addr))) {
735: struct in6pcb *last;
736: /*
737: * Deliver a multicast or broadcast datagram to *all* sockets
738: * for which the local and remote addresses and ports match
739: * those of the incoming datagram. This allows more than
740: * one process to receive multi/broadcasts on the same port.
741: * (This really ought to be done for unicast datagrams as
742: * well, but that would cause problems with existing
743: * applications that open both address-specific sockets and
744: * a wildcard socket listening to the same port -- they would
745: * end up receiving duplicates of every unicast datagram.
746: * Those applications open the multiple sockets to overcome an
747: * inadequacy of the UDP socket interface, but for backwards
748: * compatibility we avoid the problem here rather than
749: * fixing the interface. Maybe 4.5BSD will remedy this?)
750: */
751:
752: /*
753: * KAME note: usually we drop udpiphdr from mbuf here.
754: * we need udpiphdr for iPsec processing so we do that later.
755: */
756: /*
757: * Locate pcb(s) for datagram.
758: */
759: for (in6p = udb6.in6p_next; in6p != &udb6;
760: in6p = in6p->in6p_next) {
761: if (in6p->in6p_lport != *dport)
762: continue;
763: if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
764: if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, dst6)
765: && !in6_mcmatch(in6p, dst6, m->m_pkthdr.rcvif))
766: continue;
767: }
1.55 ! itojun 768: #ifndef INET6_BINDV6ONLY
! 769: else {
! 770: if (IN6_IS_ADDR_V4MAPPED(dst6)
! 771: && (in6p->in6p_flags & IN6P_BINDV6ONLY))
! 772: continue;
! 773: }
! 774: #endif
1.53 itojun 775: if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
776: if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, src6)
777: || in6p->in6p_fport != *sport)
778: continue;
779: }
1.55 ! itojun 780: #ifndef INET6_BINDV6ONLY
! 781: else {
! 782: if (IN6_IS_ADDR_V4MAPPED(src6)
! 783: && (in6p->in6p_flags & IN6P_BINDV6ONLY))
! 784: continue;
! 785: }
! 786: #endif
1.53 itojun 787:
788: last = in6p;
789: udp6_sendup(m, off, (struct sockaddr *)src,
790: in6p->in6p_socket);
791: rcvcnt++;
792:
793: /*
794: * Don't look for additional matches if this one does
795: * not have either the SO_REUSEPORT or SO_REUSEADDR
796: * socket options set. This heuristic avoids searching
797: * through all pcbs in the common case of a non-shared
798: * port. It assumes that an application will never
799: * clear these options after setting them.
800: */
801: if ((in6p->in6p_socket->so_options &
802: (SO_REUSEPORT|SO_REUSEADDR)) == 0)
803: break;
804: }
805:
806: #if 0
807: if (last == NULL) {
808: /*
809: * No matching pcb found; discard datagram.
810: * (No need to send an ICMP Port Unreachable
811: * for a broadcast or multicast datgram.)
812: */
813: switch (af) {
814: case AF_INET:
815: udpstat.udps_noport++;
816: udpstat.udps_noportbcast++;
817: break;
818: case AF_INET6:
819: udp6stat.udp6s_noport++;
820: udp6stat.udp6s_noportmcast++;
821: break;
822: }
823: goto bad;
824: }
825: #endif
826: } else {
827: /*
828: * Locate pcb for datagram.
829: */
830: in6p = in6_pcblookup_connect(&udb6, src6, *sport,
831: dst6, *dport, 0);
832: if (in6p == 0) {
833: ++udpstat.udps_pcbhashmiss;
834: in6p = in6_pcblookup_bind(&udb6, dst6, *dport, 0);
835: if (in6p == 0) {
836: #if 0
837: struct mbuf *n;
838: n = m_copy(m, 0, M_COPYALL);
839: switch (af) {
840: case AF_INET:
841: udpstat.udps_noport++;
842: if (m->m_flags & (M_BCAST | M_MCAST)) {
843: udpstat.udps_noportbcast++;
844: goto bad;
845: }
846: if (n != NULL)
847: icmp_error(n, ICMP_UNREACH,
848: ICMP_UNREACH_PORT, 0, 0);
849: break;
850: case AF_INET6:
851: udp6stat.udp6s_noport++;
852: if (m->m_flags & M_MCAST) {
853: udp6stat.udp6s_noportmcast++;
854: goto bad;
855: }
856: if (n != NULL)
857: icmp6_error(n, ICMP6_DST_UNREACH,
858: ICMP6_DST_UNREACH_NOPORT, 0);
859: break;
860: }
861: #endif
862:
863: return rcvcnt;
864: }
865: }
866:
867: udp6_sendup(m, off, (struct sockaddr *)src, in6p->in6p_socket);
868: rcvcnt++;
869: }
870:
871: bad:
872: return rcvcnt;
873: }
874: #endif
875:
876: #if 0
877: void
878: #if __STDC__
879: udp_input(struct mbuf *m, ...)
880: #else
881: udp_input(m, va_alist)
882: struct mbuf *m;
883: va_dcl
884: #endif
885: {
1.48 itojun 886: int proto;
1.1 cgd 887: register struct ip *ip;
888: register struct udphdr *uh;
889: register struct inpcb *inp;
890: struct mbuf *opts = 0;
891: int len;
892: struct ip save_ip;
1.27 christos 893: int iphlen;
894: va_list ap;
1.34 mycroft 895: struct sockaddr_in udpsrc;
1.48 itojun 896: struct sockaddr *sa;
1.27 christos 897:
898: va_start(ap, m);
899: iphlen = va_arg(ap, int);
1.48 itojun 900: proto = va_arg(ap, int);
1.27 christos 901: va_end(ap);
1.1 cgd 902:
903: udpstat.udps_ipackets++;
904:
905: /*
906: * Strip IP options, if any; should skip this,
907: * make available to user, and use on returned packets,
908: * but we don't yet have a way to check the checksum
909: * with options still present.
910: */
911: if (iphlen > sizeof (struct ip)) {
912: ip_stripoptions(m, (struct mbuf *)0);
913: iphlen = sizeof(struct ip);
914: }
915:
916: /*
917: * Get IP and UDP header together in first mbuf.
918: */
919: ip = mtod(m, struct ip *);
920: if (m->m_len < iphlen + sizeof(struct udphdr)) {
921: if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) {
922: udpstat.udps_hdrops++;
923: return;
924: }
925: ip = mtod(m, struct ip *);
926: }
927: uh = (struct udphdr *)((caddr_t)ip + iphlen);
928:
929: /*
930: * Make mbuf data length reflect UDP length.
931: * If not enough data to reflect UDP length, drop.
932: */
1.15 cgd 933: len = ntohs((u_int16_t)uh->uh_ulen);
1.47 mycroft 934: if (ip->ip_len != iphlen + len) {
935: if (ip->ip_len < iphlen + len) {
1.1 cgd 936: udpstat.udps_badlen++;
937: goto bad;
938: }
1.47 mycroft 939: m_adj(m, iphlen + len - ip->ip_len);
1.1 cgd 940: }
941: /*
942: * Save a copy of the IP header in case we want restore it
943: * for sending an ICMP error message in response.
944: */
945: save_ip = *ip;
946:
947: /*
948: * Checksum extended UDP header and data.
949: */
1.29 mrg 950: if (uh->uh_sum) {
1.25 cgd 951: bzero(((struct ipovly *)ip)->ih_x1,
952: sizeof ((struct ipovly *)ip)->ih_x1);
1.1 cgd 953: ((struct ipovly *)ip)->ih_len = uh->uh_ulen;
1.46 mycroft 954: if (in_cksum(m, len + sizeof (struct ip)) != 0) {
1.1 cgd 955: udpstat.udps_badsum++;
956: m_freem(m);
957: return;
958: }
959: }
1.13 mycroft 960:
1.48 itojun 961: /*
962: * Construct sockaddr format source address.
963: */
964: udpsrc.sin_family = AF_INET;
965: udpsrc.sin_len = sizeof(struct sockaddr_in);
966: udpsrc.sin_addr = ip->ip_src;
967: udpsrc.sin_port = uh->uh_sport;
968: bzero((caddr_t)udpsrc.sin_zero, sizeof(udpsrc.sin_zero));
969:
1.16 mycroft 970: if (IN_MULTICAST(ip->ip_dst.s_addr) ||
1.13 mycroft 971: in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
1.40 thorpej 972: struct inpcb *last;
1.4 hpeyerl 973: /*
974: * Deliver a multicast or broadcast datagram to *all* sockets
975: * for which the local and remote addresses and ports match
976: * those of the incoming datagram. This allows more than
977: * one process to receive multi/broadcasts on the same port.
978: * (This really ought to be done for unicast datagrams as
979: * well, but that would cause problems with existing
980: * applications that open both address-specific sockets and
981: * a wildcard socket listening to the same port -- they would
982: * end up receiving duplicates of every unicast datagram.
983: * Those applications open the multiple sockets to overcome an
984: * inadequacy of the UDP socket interface, but for backwards
985: * compatibility we avoid the problem here rather than
1.13 mycroft 986: * fixing the interface. Maybe 4.5BSD will remedy this?)
1.4 hpeyerl 987: */
1.13 mycroft 988:
1.48 itojun 989: iphlen += sizeof(struct udphdr);
1.4 hpeyerl 990: /*
1.48 itojun 991: * KAME note: usually we drop udpiphdr from mbuf here.
992: * we need udpiphdr for iPsec processing so we do that later.
1.4 hpeyerl 993: */
994: /*
995: * Locate pcb(s) for datagram.
996: * (Algorithm copied from raw_intr().)
997: */
998: last = NULL;
1.22 cgd 999: for (inp = udbtable.inpt_queue.cqh_first;
1000: inp != (struct inpcb *)&udbtable.inpt_queue;
1001: inp = inp->inp_queue.cqe_next) {
1.4 hpeyerl 1002: if (inp->inp_lport != uh->uh_dport)
1003: continue;
1.34 mycroft 1004: if (!in_nullhost(inp->inp_laddr)) {
1005: if (!in_hosteq(inp->inp_laddr, ip->ip_dst))
1.4 hpeyerl 1006: continue;
1007: }
1.34 mycroft 1008: if (!in_nullhost(inp->inp_faddr)) {
1009: if (!in_hosteq(inp->inp_faddr, ip->ip_src) ||
1.4 hpeyerl 1010: inp->inp_fport != uh->uh_sport)
1011: continue;
1012: }
1013:
1014: if (last != NULL) {
1015: struct mbuf *n;
1016:
1.48 itojun 1017: #ifdef IPSEC
1018: /* check AH/ESP integrity. */
1019: if (last != NULL && ipsec4_in_reject(m, last)) {
1020: ipsecstat.in_polvio++;
1021: /* do not inject data to pcb */
1022: } else
1023: #endif /*IPSEC*/
1.4 hpeyerl 1024: if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
1.40 thorpej 1025: if (last->inp_flags & INP_CONTROLOPTS
1026: || last->inp_socket->so_options &
1027: SO_TIMESTAMP) {
1028: ip_savecontrol(last, &opts,
1029: ip, n);
1030: }
1.49 drochner 1031: m_adj(n, iphlen);
1.48 itojun 1032: sa = (struct sockaddr *)&udpsrc;
1.40 thorpej 1033: if (sbappendaddr(
1034: &last->inp_socket->so_rcv,
1.48 itojun 1035: sa, n, opts) == 0) {
1.4 hpeyerl 1036: m_freem(n);
1.40 thorpej 1037: if (opts)
1038: m_freem(opts);
1.13 mycroft 1039: } else
1.40 thorpej 1040: sorwakeup(last->inp_socket);
1041: opts = 0;
1.4 hpeyerl 1042: }
1043: }
1.40 thorpej 1044: last = inp;
1.4 hpeyerl 1045: /*
1.13 mycroft 1046: * Don't look for additional matches if this one does
1047: * not have either the SO_REUSEPORT or SO_REUSEADDR
1048: * socket options set. This heuristic avoids searching
1049: * through all pcbs in the common case of a non-shared
1050: * port. It * assumes that an application will never
1051: * clear these options after setting them.
1.4 hpeyerl 1052: */
1.40 thorpej 1053: if ((last->inp_socket->so_options &
1054: (SO_REUSEPORT|SO_REUSEADDR)) == 0)
1.4 hpeyerl 1055: break;
1056: }
1.6 mycroft 1057:
1.4 hpeyerl 1058: if (last == NULL) {
1059: /*
1060: * No matching pcb found; discard datagram.
1061: * (No need to send an ICMP Port Unreachable
1062: * for a broadcast or multicast datgram.)
1063: */
1.48 itojun 1064: udpstat.udps_noport++;
1.13 mycroft 1065: udpstat.udps_noportbcast++;
1.4 hpeyerl 1066: goto bad;
1067: }
1.48 itojun 1068: #ifdef IPSEC
1069: /* check AH/ESP integrity. */
1070: if (last != NULL && ipsec4_in_reject(m, last)) {
1071: ipsecstat.in_polvio++;
1072: goto bad;
1073: }
1074: #endif /*IPSEC*/
1.40 thorpej 1075: if (last->inp_flags & INP_CONTROLOPTS ||
1076: last->inp_socket->so_options & SO_TIMESTAMP)
1077: ip_savecontrol(last, &opts, ip, m);
1.49 drochner 1078: m->m_len -= iphlen;
1079: m->m_pkthdr.len -= iphlen;
1080: m->m_data += iphlen;
1.48 itojun 1081: sa = (struct sockaddr *)&udpsrc;
1082: if (sbappendaddr(&last->inp_socket->so_rcv, sa, m, opts) == 0) {
1.13 mycroft 1083: udpstat.udps_fullsock++;
1.4 hpeyerl 1084: goto bad;
1.13 mycroft 1085: }
1.40 thorpej 1086: sorwakeup(last->inp_socket);
1.4 hpeyerl 1087: return;
1088: }
1.1 cgd 1089: /*
1090: * Locate pcb for datagram.
1091: */
1.35 mycroft 1092: inp = in_pcblookup_connect(&udbtable, ip->ip_src, uh->uh_sport,
1.26 mycroft 1093: ip->ip_dst, uh->uh_dport);
1094: if (inp == 0) {
1095: ++udpstat.udps_pcbhashmiss;
1.35 mycroft 1096: inp = in_pcblookup_bind(&udbtable, ip->ip_dst, uh->uh_dport);
1.18 mycroft 1097: if (inp == 0) {
1098: udpstat.udps_noport++;
1099: if (m->m_flags & (M_BCAST | M_MCAST)) {
1100: udpstat.udps_noportbcast++;
1101: goto bad;
1102: }
1103: *ip = save_ip;
1.38 ws 1104: #if NIPKDB > 0
1105: if (checkipkdb(&ip->ip_src,
1106: uh->uh_sport,
1107: uh->uh_dport,
1108: m,
1109: iphlen + sizeof(struct udphdr),
1110: len - sizeof(struct udphdr)))
1.37 ws 1111: /* It was a debugger connect packet, just drop it now */
1112: goto bad;
1113: #endif
1.18 mycroft 1114: icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
1115: return;
1.13 mycroft 1116: }
1.1 cgd 1117: }
1.48 itojun 1118: #ifdef IPSEC
1119: if (inp != NULL && ipsec4_in_reject(m, inp)) {
1120: ipsecstat.in_polvio++;
1121: goto bad;
1122: }
1123: #endif /*IPSEC*/
1.1 cgd 1124:
1125: /*
1126: * Stuff source address and datagram in user buffer.
1127: */
1.40 thorpej 1128: if (inp->inp_flags & INP_CONTROLOPTS ||
1129: inp->inp_socket->so_options & SO_TIMESTAMP)
1130: ip_savecontrol(inp, &opts, ip, m);
1.1 cgd 1131: iphlen += sizeof(struct udphdr);
1132: m->m_len -= iphlen;
1133: m->m_pkthdr.len -= iphlen;
1134: m->m_data += iphlen;
1.48 itojun 1135: sa = (struct sockaddr *)&udpsrc;
1136: if (sbappendaddr(&inp->inp_socket->so_rcv, sa, m, opts) == 0) {
1.1 cgd 1137: udpstat.udps_fullsock++;
1138: goto bad;
1139: }
1140: sorwakeup(inp->inp_socket);
1141: return;
1142: bad:
1143: m_freem(m);
1144: if (opts)
1145: m_freem(opts);
1146: }
1.53 itojun 1147: #endif
1.1 cgd 1148:
1149: /*
1150: * Notify a udp user of an asynchronous error;
1151: * just wake up so that he can collect error status.
1152: */
1.7 mycroft 1153: static void
1.1 cgd 1154: udp_notify(inp, errno)
1155: register struct inpcb *inp;
1.7 mycroft 1156: int errno;
1.1 cgd 1157: {
1.34 mycroft 1158:
1.1 cgd 1159: inp->inp_socket->so_error = errno;
1160: sorwakeup(inp->inp_socket);
1161: sowwakeup(inp->inp_socket);
1162: }
1163:
1.27 christos 1164: void *
1165: udp_ctlinput(cmd, sa, v)
1.1 cgd 1166: int cmd;
1167: struct sockaddr *sa;
1.27 christos 1168: void *v;
1.1 cgd 1169: {
1.27 christos 1170: register struct ip *ip = v;
1.1 cgd 1171: register struct udphdr *uh;
1.21 mycroft 1172: extern int inetctlerrmap[];
1.18 mycroft 1173: void (*notify) __P((struct inpcb *, int)) = udp_notify;
1.21 mycroft 1174: int errno;
1.1 cgd 1175:
1.53 itojun 1176: if (sa->sa_family != AF_INET
1177: || sa->sa_len != sizeof(struct sockaddr_in))
1.51 itojun 1178: return NULL;
1.20 mycroft 1179: if ((unsigned)cmd >= PRC_NCMDS)
1.27 christos 1180: return NULL;
1.20 mycroft 1181: errno = inetctlerrmap[cmd];
1.18 mycroft 1182: if (PRC_IS_REDIRECT(cmd))
1.19 mycroft 1183: notify = in_rtchange, ip = 0;
1.18 mycroft 1184: else if (cmd == PRC_HOSTDEAD)
1.19 mycroft 1185: ip = 0;
1.23 cgd 1186: else if (errno == 0)
1.27 christos 1187: return NULL;
1.19 mycroft 1188: if (ip) {
1.1 cgd 1189: uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
1.34 mycroft 1190: in_pcbnotify(&udbtable, satosin(sa)->sin_addr, uh->uh_dport,
1191: ip->ip_src, uh->uh_sport, errno, notify);
1.53 itojun 1192:
1193: /* XXX mapped address case */
1.19 mycroft 1194: } else
1.34 mycroft 1195: in_pcbnotifyall(&udbtable, satosin(sa)->sin_addr, errno,
1196: notify);
1.27 christos 1197: return NULL;
1.1 cgd 1198: }
1199:
1.7 mycroft 1200: int
1.27 christos 1201: #if __STDC__
1202: udp_output(struct mbuf *m, ...)
1203: #else
1204: udp_output(m, va_alist)
1205: struct mbuf *m;
1206: va_dcl
1207: #endif
1208: {
1.1 cgd 1209: register struct inpcb *inp;
1210: register struct udpiphdr *ui;
1211: register int len = m->m_pkthdr.len;
1.31 mycroft 1212: int error = 0;
1.27 christos 1213: va_list ap;
1214:
1215: va_start(ap, m);
1216: inp = va_arg(ap, struct inpcb *);
1217: va_end(ap);
1.1 cgd 1218:
1219: /*
1220: * Calculate data length and get a mbuf
1221: * for UDP and IP headers.
1222: */
1.13 mycroft 1223: M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT);
1224: if (m == 0) {
1225: error = ENOBUFS;
1.39 thorpej 1226: goto release;
1227: }
1228:
1229: /*
1230: * Compute the packet length of the IP header, and
1231: * punt if the length looks bogus.
1232: */
1233: if ((len + sizeof(struct udpiphdr)) > IP_MAXPACKET) {
1234: error = EMSGSIZE;
1.13 mycroft 1235: goto release;
1236: }
1.1 cgd 1237:
1238: /*
1239: * Fill in mbuf with extended UDP header
1240: * and addresses and length put into network format.
1241: */
1242: ui = mtod(m, struct udpiphdr *);
1.25 cgd 1243: bzero(ui->ui_x1, sizeof ui->ui_x1);
1.1 cgd 1244: ui->ui_pr = IPPROTO_UDP;
1.15 cgd 1245: ui->ui_len = htons((u_int16_t)len + sizeof (struct udphdr));
1.1 cgd 1246: ui->ui_src = inp->inp_laddr;
1247: ui->ui_dst = inp->inp_faddr;
1248: ui->ui_sport = inp->inp_lport;
1249: ui->ui_dport = inp->inp_fport;
1250: ui->ui_ulen = ui->ui_len;
1251:
1252: /*
1253: * Stuff checksum and output datagram.
1254: */
1255: ui->ui_sum = 0;
1256: if (udpcksum) {
1257: if ((ui->ui_sum = in_cksum(m, sizeof (struct udpiphdr) + len)) == 0)
1258: ui->ui_sum = 0xffff;
1259: }
1260: ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
1261: ((struct ip *)ui)->ip_ttl = inp->inp_ip.ip_ttl; /* XXX */
1262: ((struct ip *)ui)->ip_tos = inp->inp_ip.ip_tos; /* XXX */
1263: udpstat.udps_opackets++;
1.48 itojun 1264:
1265: #ifdef IPSEC
1266: m->m_pkthdr.rcvif = (struct ifnet *)inp->inp_socket;
1267: #endif /*IPSEC*/
1268:
1.31 mycroft 1269: return (ip_output(m, inp->inp_options, &inp->inp_route,
1.12 mycroft 1270: inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST),
1.31 mycroft 1271: inp->inp_moptions));
1.1 cgd 1272:
1273: release:
1274: m_freem(m);
1275: return (error);
1276: }
1277:
1.42 thorpej 1278: int udp_sendspace = 9216; /* really max datagram size */
1279: int udp_recvspace = 40 * (1024 + sizeof(struct sockaddr_in));
1.1 cgd 1280: /* 40 1K datagrams */
1281:
1282: /*ARGSUSED*/
1.7 mycroft 1283: int
1.31 mycroft 1284: udp_usrreq(so, req, m, nam, control, p)
1.1 cgd 1285: struct socket *so;
1286: int req;
1.31 mycroft 1287: struct mbuf *m, *nam, *control;
1.30 mycroft 1288: struct proc *p;
1.1 cgd 1289: {
1.31 mycroft 1290: register struct inpcb *inp;
1.1 cgd 1291: int s;
1.31 mycroft 1292: register int error = 0;
1.1 cgd 1293:
1294: if (req == PRU_CONTROL)
1.31 mycroft 1295: return (in_control(so, (long)m, (caddr_t)nam,
1.30 mycroft 1296: (struct ifnet *)control, p));
1.31 mycroft 1297:
1298: s = splsoftnet();
1299: inp = sotoinpcb(so);
1.32 mycroft 1300: #ifdef DIAGNOSTIC
1301: if (req != PRU_SEND && req != PRU_SENDOOB && control)
1302: panic("udp_usrreq: unexpected control mbuf");
1303: #endif
1.31 mycroft 1304: if (inp == 0 && req != PRU_ATTACH) {
1305: error = EINVAL;
1306: goto release;
1307: }
1308:
1.1 cgd 1309: /*
1310: * Note: need to block udp_input while changing
1311: * the udp pcb queue and/or pcb addresses.
1312: */
1313: switch (req) {
1314:
1315: case PRU_ATTACH:
1.31 mycroft 1316: if (inp != 0) {
1317: error = EISCONN;
1.1 cgd 1318: break;
1319: }
1.31 mycroft 1320: if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1321: error = soreserve(so, udp_sendspace, udp_recvspace);
1322: if (error)
1323: break;
1324: }
1.18 mycroft 1325: error = in_pcballoc(so, &udbtable);
1.1 cgd 1326: if (error)
1327: break;
1.31 mycroft 1328: inp = sotoinpcb(so);
1329: inp->inp_ip.ip_ttl = ip_defttl;
1.48 itojun 1330: #ifdef IPSEC
1.53 itojun 1331: error = ipsec_init_policy(&inp->inp_sp);
1332: if (error != 0) {
1.52 itojun 1333: in_pcbdetach(inp);
1.53 itojun 1334: break;
1335: }
1.48 itojun 1336: #endif /*IPSEC*/
1.1 cgd 1337: break;
1338:
1339: case PRU_DETACH:
1.31 mycroft 1340: in_pcbdetach(inp);
1.1 cgd 1341: break;
1342:
1343: case PRU_BIND:
1.31 mycroft 1344: error = in_pcbbind(inp, nam, p);
1.1 cgd 1345: break;
1346:
1347: case PRU_LISTEN:
1348: error = EOPNOTSUPP;
1349: break;
1350:
1351: case PRU_CONNECT:
1.31 mycroft 1352: error = in_pcbconnect(inp, nam);
1353: if (error)
1.1 cgd 1354: break;
1.31 mycroft 1355: soisconnected(so);
1.1 cgd 1356: break;
1357:
1358: case PRU_CONNECT2:
1359: error = EOPNOTSUPP;
1360: break;
1361:
1362: case PRU_DISCONNECT:
1.31 mycroft 1363: /*soisdisconnected(so);*/
1364: so->so_state &= ~SS_ISCONNECTED; /* XXX */
1.1 cgd 1365: in_pcbdisconnect(inp);
1.34 mycroft 1366: inp->inp_laddr = zeroin_addr; /* XXX */
1.35 mycroft 1367: in_pcbstate(inp, INP_BOUND); /* XXX */
1.1 cgd 1368: break;
1369:
1370: case PRU_SHUTDOWN:
1371: socantsendmore(so);
1372: break;
1373:
1.31 mycroft 1374: case PRU_RCVD:
1375: error = EOPNOTSUPP;
1.1 cgd 1376: break;
1377:
1.31 mycroft 1378: case PRU_SEND:
1.32 mycroft 1379: if (control && control->m_len) {
1380: m_freem(control);
1381: m_freem(m);
1382: error = EINVAL;
1383: break;
1384: }
1.31 mycroft 1385: {
1.35 mycroft 1386: struct in_addr laddr; /* XXX */
1.1 cgd 1387:
1.31 mycroft 1388: if (nam) {
1.35 mycroft 1389: laddr = inp->inp_laddr; /* XXX */
1.31 mycroft 1390: if ((so->so_state & SS_ISCONNECTED) != 0) {
1391: error = EISCONN;
1.32 mycroft 1392: goto die;
1.31 mycroft 1393: }
1394: error = in_pcbconnect(inp, nam);
1.32 mycroft 1395: if (error) {
1396: die:
1397: m_freem(m);
1.31 mycroft 1398: break;
1.32 mycroft 1399: }
1.31 mycroft 1400: } else {
1401: if ((so->so_state & SS_ISCONNECTED) == 0) {
1402: error = ENOTCONN;
1.32 mycroft 1403: goto die;
1.31 mycroft 1404: }
1405: }
1.33 mycroft 1406: error = udp_output(m, inp);
1.31 mycroft 1407: if (nam) {
1408: in_pcbdisconnect(inp);
1.35 mycroft 1409: inp->inp_laddr = laddr; /* XXX */
1410: in_pcbstate(inp, INP_BOUND); /* XXX */
1.31 mycroft 1411: }
1412: }
1.1 cgd 1413: break;
1414:
1415: case PRU_SENSE:
1416: /*
1417: * stat: don't bother with a blocksize.
1418: */
1.31 mycroft 1419: splx(s);
1.1 cgd 1420: return (0);
1421:
1.31 mycroft 1422: case PRU_RCVOOB:
1423: error = EOPNOTSUPP;
1424: break;
1425:
1.1 cgd 1426: case PRU_SENDOOB:
1.32 mycroft 1427: m_freem(control);
1.31 mycroft 1428: m_freem(m);
1.1 cgd 1429: error = EOPNOTSUPP;
1430: break;
1431:
1.31 mycroft 1432: case PRU_SOCKADDR:
1433: in_setsockaddr(inp, nam);
1434: break;
1435:
1436: case PRU_PEERADDR:
1437: in_setpeeraddr(inp, nam);
1438: break;
1.1 cgd 1439:
1440: default:
1441: panic("udp_usrreq");
1442: }
1443:
1444: release:
1.31 mycroft 1445: splx(s);
1.1 cgd 1446: return (error);
1.13 mycroft 1447: }
1448:
1449: /*
1450: * Sysctl for udp variables.
1451: */
1.27 christos 1452: int
1.13 mycroft 1453: udp_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
1454: int *name;
1455: u_int namelen;
1456: void *oldp;
1457: size_t *oldlenp;
1458: void *newp;
1459: size_t newlen;
1460: {
1461: /* All sysctl names at this level are terminal. */
1462: if (namelen != 1)
1463: return (ENOTDIR);
1464:
1465: switch (name[0]) {
1466: case UDPCTL_CHECKSUM:
1467: return (sysctl_int(oldp, oldlenp, newp, newlen, &udpcksum));
1.42 thorpej 1468: case UDPCTL_SENDSPACE:
1469: return (sysctl_int(oldp, oldlenp, newp, newlen,
1470: &udp_sendspace));
1471: case UDPCTL_RECVSPACE:
1472: return (sysctl_int(oldp, oldlenp, newp, newlen,
1473: &udp_recvspace));
1.13 mycroft 1474: default:
1475: return (ENOPROTOOPT);
1476: }
1477: /* NOTREACHED */
1.1 cgd 1478: }
CVSweb <webmaster@jp.NetBSD.org>