Annotation of src/sys/netinet/udp_usrreq.c, Revision 1.95
1.95 ! thorpej 1: /* $NetBSD: udp_usrreq.c,v 1.94 2002/06/09 16:33:45 itojun Exp $ */
1.48 itojun 2:
3: /*
4: * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5: * All rights reserved.
1.94 itojun 6: *
1.48 itojun 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.
1.94 itojun 18: *
1.48 itojun 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.91 lukem 66:
67: #include <sys/cdefs.h>
1.95 ! thorpej 68: __KERNEL_RCSID(0, "$NetBSD: udp_usrreq.c,v 1.94 2002/06/09 16:33:45 itojun Exp $");
1.50 thorpej 69:
1.77 soda 70: #include "opt_inet.h"
1.50 thorpej 71: #include "opt_ipsec.h"
1.78 thorpej 72: #include "opt_inet_csum.h"
1.64 ws 73: #include "opt_ipkdb.h"
1.1 cgd 74:
1.5 mycroft 75: #include <sys/param.h>
76: #include <sys/malloc.h>
77: #include <sys/mbuf.h>
78: #include <sys/protosw.h>
79: #include <sys/socket.h>
80: #include <sys/socketvar.h>
1.13 mycroft 81: #include <sys/errno.h>
1.5 mycroft 82: #include <sys/stat.h>
1.27 christos 83: #include <sys/systm.h>
84: #include <sys/proc.h>
1.53 itojun 85: #include <sys/domain.h>
1.27 christos 86: #include <sys/sysctl.h>
1.1 cgd 87:
1.5 mycroft 88: #include <net/if.h>
89: #include <net/route.h>
1.1 cgd 90:
1.5 mycroft 91: #include <netinet/in.h>
92: #include <netinet/in_systm.h>
1.15 cgd 93: #include <netinet/in_var.h>
1.5 mycroft 94: #include <netinet/ip.h>
95: #include <netinet/in_pcb.h>
96: #include <netinet/ip_var.h>
97: #include <netinet/ip_icmp.h>
98: #include <netinet/udp.h>
99: #include <netinet/udp_var.h>
1.1 cgd 100:
1.53 itojun 101: #ifdef INET6
102: #include <netinet/ip6.h>
103: #include <netinet/icmp6.h>
104: #include <netinet6/ip6_var.h>
105: #include <netinet6/in6_pcb.h>
106: #include <netinet6/udp6_var.h>
107: #endif
108:
109: #ifdef PULLDOWN_TEST
110: #ifndef INET6
111: /* always need ip6.h for IP6_EXTHDR_GET */
112: #include <netinet/ip6.h>
113: #endif
114: #endif
115:
1.76 itojun 116: #include "faith.h"
117: #if defined(NFAITH) && NFAITH > 0
118: #include <net/if_faith.h>
119: #endif
120:
1.27 christos 121: #include <machine/stdarg.h>
122:
1.48 itojun 123: #ifdef IPSEC
124: #include <netinet6/ipsec.h>
125: #include <netkey/key.h>
126: #endif /*IPSEC*/
127:
1.64 ws 128: #ifdef IPKDB
129: #include <ipkdb/ipkdb.h>
130: #endif
131:
1.8 mycroft 132: /*
133: * UDP protocol implementation.
134: * Per RFC 768, August, 1980.
135: */
136: #ifndef COMPAT_42
137: int udpcksum = 1;
138: #else
139: int udpcksum = 0; /* XXX */
140: #endif
1.93 matt 141:
142: struct inpcbtable udbtable;
143: struct udpstat udpstat;
1.8 mycroft 144:
1.72 itojun 145: #ifdef INET
1.53 itojun 146: static void udp4_sendup __P((struct mbuf *, int, struct sockaddr *,
147: struct socket *));
148: static int udp4_realinput __P((struct sockaddr_in *, struct sockaddr_in *,
149: struct mbuf *, int));
1.72 itojun 150: #endif
1.53 itojun 151: #ifdef INET6
152: static void udp6_sendup __P((struct mbuf *, int, struct sockaddr *,
153: struct socket *));
154: static int udp6_realinput __P((int, struct sockaddr_in6 *,
155: struct sockaddr_in6 *, struct mbuf *, int));
156: #endif
1.72 itojun 157: #ifdef INET
1.13 mycroft 158: static void udp_notify __P((struct inpcb *, int));
1.72 itojun 159: #endif
1.7 mycroft 160:
1.26 mycroft 161: #ifndef UDBHASHSIZE
162: #define UDBHASHSIZE 128
163: #endif
164: int udbhashsize = UDBHASHSIZE;
165:
1.78 thorpej 166: #ifdef UDP_CSUM_COUNTERS
167: #include <sys/device.h>
168:
169: struct evcnt udp_hwcsum_bad = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
170: NULL, "udp", "hwcsum bad");
171: struct evcnt udp_hwcsum_ok = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
172: NULL, "udp", "hwcsum ok");
173: struct evcnt udp_hwcsum_data = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
174: NULL, "udp", "hwcsum data");
175: struct evcnt udp_swcsum = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
176: NULL, "udp", "swcsum");
177:
178: #define UDP_CSUM_COUNTER_INCR(ev) (ev)->ev_count++
179:
180: #else
181:
182: #define UDP_CSUM_COUNTER_INCR(ev) /* nothing */
183:
184: #endif /* UDP_CSUM_COUNTERS */
185:
1.7 mycroft 186: void
1.1 cgd 187: udp_init()
188: {
1.18 mycroft 189:
1.72 itojun 190: #ifdef INET
1.35 mycroft 191: in_pcbinit(&udbtable, udbhashsize, udbhashsize);
1.72 itojun 192: #endif
1.78 thorpej 193:
194: #ifdef UDP_CSUM_COUNTERS
195: evcnt_attach_static(&udp_hwcsum_bad);
196: evcnt_attach_static(&udp_hwcsum_ok);
197: evcnt_attach_static(&udp_hwcsum_data);
198: evcnt_attach_static(&udp_swcsum);
199: #endif /* UDP_CSUM_COUNTERS */
1.1 cgd 200: }
201:
1.72 itojun 202: #ifdef INET
1.7 mycroft 203: void
1.27 christos 204: #if __STDC__
205: udp_input(struct mbuf *m, ...)
206: #else
207: udp_input(m, va_alist)
208: struct mbuf *m;
209: va_dcl
210: #endif
1.1 cgd 211: {
1.53 itojun 212: va_list ap;
213: struct sockaddr_in src, dst;
214: struct ip *ip;
215: struct udphdr *uh;
216: int iphlen, proto;
217: int len;
218: int n;
219:
220: va_start(ap, m);
221: iphlen = va_arg(ap, int);
222: proto = va_arg(ap, int);
223: va_end(ap);
224:
225: udpstat.udps_ipackets++;
226:
227: #ifndef PULLDOWN_TEST
228: /*
229: * Strip IP options, if any; should skip this,
230: * make available to user, and use on returned packets,
231: * but we don't yet have a way to check the checksum
232: * with options still present.
233: */
234: if (iphlen > sizeof (struct ip)) {
235: ip_stripoptions(m, (struct mbuf *)0);
236: iphlen = sizeof(struct ip);
237: }
238: #else
239: /*
240: * we may enable the above code if we save and pass IPv4 options
241: * to the userland.
242: */
243: #endif
244:
245: /*
246: * Get IP and UDP header together in first mbuf.
247: */
248: ip = mtod(m, struct ip *);
249: #ifndef PULLDOWN_TEST
250: if (m->m_len < iphlen + sizeof(struct udphdr)) {
251: if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) {
252: udpstat.udps_hdrops++;
253: return;
254: }
255: ip = mtod(m, struct ip *);
256: }
257: uh = (struct udphdr *)((caddr_t)ip + iphlen);
258: #else
259: IP6_EXTHDR_GET(uh, struct udphdr *, m, iphlen, sizeof(struct udphdr));
260: if (uh == NULL) {
261: udpstat.udps_hdrops++;
262: return;
263: }
264: #endif
1.95 ! thorpej 265: KASSERT(UDP_HDR_ALIGNED_P(uh));
1.53 itojun 266:
1.57 itojun 267: /* destination port of 0 is illegal, based on RFC768. */
268: if (uh->uh_dport == 0)
269: goto bad;
270:
1.53 itojun 271: /*
272: * Make mbuf data length reflect UDP length.
273: * If not enough data to reflect UDP length, drop.
274: */
275: len = ntohs((u_int16_t)uh->uh_ulen);
276: if (ip->ip_len != iphlen + len) {
1.70 sommerfe 277: if (ip->ip_len < iphlen + len || len < sizeof(struct udphdr)) {
1.53 itojun 278: udpstat.udps_badlen++;
279: goto bad;
280: }
281: m_adj(m, iphlen + len - ip->ip_len);
282: }
283:
284: /*
285: * Checksum extended UDP header and data.
286: */
287: if (uh->uh_sum) {
1.78 thorpej 288: switch (m->m_pkthdr.csum_flags &
1.84 thorpej 289: ((m->m_pkthdr.rcvif->if_csum_flags_rx & M_CSUM_UDPv4) |
1.78 thorpej 290: M_CSUM_TCP_UDP_BAD | M_CSUM_DATA)) {
291: case M_CSUM_UDPv4|M_CSUM_TCP_UDP_BAD:
292: UDP_CSUM_COUNTER_INCR(&udp_hwcsum_bad);
293: goto badcsum;
294:
295: case M_CSUM_UDPv4|M_CSUM_DATA:
296: UDP_CSUM_COUNTER_INCR(&udp_hwcsum_data);
297: if ((m->m_pkthdr.csum_data ^ 0xffff) != 0)
298: goto badcsum;
299: break;
300:
301: case M_CSUM_UDPv4:
302: /* Checksum was okay. */
303: UDP_CSUM_COUNTER_INCR(&udp_hwcsum_ok);
304: break;
305:
306: default:
307: /* Need to compute it ourselves. */
308: UDP_CSUM_COUNTER_INCR(&udp_swcsum);
309: if (in4_cksum(m, IPPROTO_UDP, iphlen, len) != 0)
310: goto badcsum;
311: break;
1.53 itojun 312: }
313: }
314:
315: /* construct source and dst sockaddrs. */
316: bzero(&src, sizeof(src));
317: src.sin_family = AF_INET;
318: src.sin_len = sizeof(struct sockaddr_in);
319: bcopy(&ip->ip_src, &src.sin_addr, sizeof(src.sin_addr));
320: src.sin_port = uh->uh_sport;
321: bzero(&dst, sizeof(dst));
322: dst.sin_family = AF_INET;
323: dst.sin_len = sizeof(struct sockaddr_in);
324: bcopy(&ip->ip_dst, &dst.sin_addr, sizeof(dst.sin_addr));
325: dst.sin_port = uh->uh_dport;
326:
327: n = udp4_realinput(&src, &dst, m, iphlen);
328: #ifdef INET6
329: if (IN_MULTICAST(ip->ip_dst.s_addr) || n == 0) {
330: struct sockaddr_in6 src6, dst6;
331:
332: bzero(&src6, sizeof(src6));
333: src6.sin6_family = AF_INET6;
334: src6.sin6_len = sizeof(struct sockaddr_in6);
335: src6.sin6_addr.s6_addr[10] = src6.sin6_addr.s6_addr[11] = 0xff;
336: bcopy(&ip->ip_src, &src6.sin6_addr.s6_addr[12],
337: sizeof(ip->ip_src));
338: src6.sin6_port = uh->uh_sport;
339: bzero(&dst6, sizeof(dst6));
340: dst6.sin6_family = AF_INET6;
341: dst6.sin6_len = sizeof(struct sockaddr_in6);
342: dst6.sin6_addr.s6_addr[10] = dst6.sin6_addr.s6_addr[11] = 0xff;
343: bcopy(&ip->ip_dst, &dst6.sin6_addr.s6_addr[12],
344: sizeof(ip->ip_dst));
345: dst6.sin6_port = uh->uh_dport;
346:
347: n += udp6_realinput(AF_INET, &src6, &dst6, m, iphlen);
348: }
349: #endif
350:
351: if (n == 0) {
352: if (m->m_flags & (M_BCAST | M_MCAST)) {
353: udpstat.udps_noportbcast++;
354: goto bad;
355: }
1.61 itojun 356: udpstat.udps_noport++;
1.64 ws 357: #ifdef IPKDB
1.53 itojun 358: if (checkipkdb(&ip->ip_src, uh->uh_sport, uh->uh_dport,
359: m, iphlen + sizeof(struct udphdr),
360: m->m_pkthdr.len - iphlen - sizeof(struct udphdr))) {
361: /*
362: * It was a debugger connect packet,
363: * just drop it now
364: */
365: goto bad;
366: }
367: #endif
368: icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
369: m = NULL;
370: }
371:
372: bad:
373: if (m)
374: m_freem(m);
1.78 thorpej 375: return;
376:
377: badcsum:
378: m_freem(m);
379: udpstat.udps_badsum++;
1.53 itojun 380: }
1.72 itojun 381: #endif
1.53 itojun 382:
383: #ifdef INET6
384: int
385: udp6_input(mp, offp, proto)
386: struct mbuf **mp;
387: int *offp, proto;
388: {
389: struct mbuf *m = *mp;
390: int off = *offp;
391: struct sockaddr_in6 src, dst;
392: struct ip6_hdr *ip6;
393: struct udphdr *uh;
394: u_int32_t plen, ulen;
395:
1.76 itojun 396: #ifndef PULLDOWN_TEST
397: IP6_EXTHDR_CHECK(m, off, sizeof(struct udphdr), IPPROTO_DONE);
398: #endif
399: ip6 = mtod(m, struct ip6_hdr *);
400:
1.53 itojun 401: #if defined(NFAITH) && 0 < NFAITH
1.76 itojun 402: if (faithprefix(&ip6->ip6_dst)) {
403: /* send icmp6 host unreach? */
404: m_freem(m);
405: return IPPROTO_DONE;
1.53 itojun 406: }
407: #endif
408:
409: udp6stat.udp6s_ipackets++;
410:
411: /* check for jumbogram is done in ip6_input. we can trust pkthdr.len */
412: plen = m->m_pkthdr.len - off;
413: #ifndef PULLDOWN_TEST
414: uh = (struct udphdr *)((caddr_t)ip6 + off);
415: #else
416: IP6_EXTHDR_GET(uh, struct udphdr *, m, off, sizeof(struct udphdr));
417: if (uh == NULL) {
418: ip6stat.ip6s_tooshort++;
419: return IPPROTO_DONE;
420: }
421: #endif
1.95 ! thorpej 422: KASSERT(UDP_HDR_ALIGNED_P(uh));
1.53 itojun 423: ulen = ntohs((u_short)uh->uh_ulen);
1.69 itojun 424: /*
425: * RFC2675 section 4: jumbograms will have 0 in the UDP header field,
426: * iff payload length > 0xffff.
427: */
1.53 itojun 428: if (ulen == 0 && plen > 0xffff)
429: ulen = plen;
430:
431: if (plen != ulen) {
432: udp6stat.udp6s_badlen++;
1.54 itojun 433: goto bad;
434: }
435:
1.57 itojun 436: /* destination port of 0 is illegal, based on RFC768. */
437: if (uh->uh_dport == 0)
438: goto bad;
439:
1.54 itojun 440: /* Be proactive about malicious use of IPv4 mapped address */
441: if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
442: IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
443: /* XXX stat */
1.53 itojun 444: goto bad;
445: }
446:
447: /*
448: * Checksum extended UDP header and data.
449: */
450: if (uh->uh_sum == 0)
451: udp6stat.udp6s_nosum++;
452: else if (in6_cksum(m, IPPROTO_UDP, off, ulen) != 0) {
453: udp6stat.udp6s_badsum++;
454: goto bad;
455: }
456:
457: /*
458: * Construct source and dst sockaddrs.
459: * Note that ifindex (s6_addr16[1]) is already filled.
460: */
461: bzero(&src, sizeof(src));
462: src.sin6_family = AF_INET6;
463: src.sin6_len = sizeof(struct sockaddr_in6);
1.69 itojun 464: /* KAME hack: recover scopeid */
465: (void)in6_recoverscope(&src, &ip6->ip6_src, m->m_pkthdr.rcvif);
1.53 itojun 466: src.sin6_port = uh->uh_sport;
467: bzero(&dst, sizeof(dst));
468: dst.sin6_family = AF_INET6;
469: dst.sin6_len = sizeof(struct sockaddr_in6);
1.69 itojun 470: /* KAME hack: recover scopeid */
471: (void)in6_recoverscope(&dst, &ip6->ip6_dst, m->m_pkthdr.rcvif);
1.53 itojun 472: dst.sin6_port = uh->uh_dport;
473:
474: if (udp6_realinput(AF_INET6, &src, &dst, m, off) == 0) {
475: if (m->m_flags & M_MCAST) {
476: udp6stat.udp6s_noportmcast++;
477: goto bad;
478: }
1.61 itojun 479: udp6stat.udp6s_noport++;
1.53 itojun 480: icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
481: m = NULL;
482: }
483:
484: bad:
485: if (m)
486: m_freem(m);
487: return IPPROTO_DONE;
488: }
489: #endif
490:
1.72 itojun 491: #ifdef INET
1.53 itojun 492: static void
493: udp4_sendup(m, off, src, so)
494: struct mbuf *m;
495: int off; /* offset of data portion */
496: struct sockaddr *src;
497: struct socket *so;
498: {
499: struct mbuf *opts = NULL;
500: struct mbuf *n;
501: struct inpcb *inp = NULL;
502: #ifdef INET6
503: struct in6pcb *in6p = NULL;
504: #endif
505:
506: if (!so)
507: return;
508: switch (so->so_proto->pr_domain->dom_family) {
509: case AF_INET:
510: inp = sotoinpcb(so);
511: break;
512: #ifdef INET6
513: case AF_INET6:
514: in6p = sotoin6pcb(so);
515: break;
516: #endif
517: default:
518: return;
519: }
520:
521: #ifdef IPSEC
522: /* check AH/ESP integrity. */
523: if (so != NULL && ipsec4_in_reject_so(m, so)) {
524: ipsecstat.in_polvio++;
525: return;
526: }
527: #endif /*IPSEC*/
528:
529: if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
530: if (inp && (inp->inp_flags & INP_CONTROLOPTS
531: || so->so_options & SO_TIMESTAMP)) {
532: struct ip *ip = mtod(n, struct ip *);
533: ip_savecontrol(inp, &opts, ip, n);
534: }
535:
536: m_adj(n, off);
537: if (sbappendaddr(&so->so_rcv, src, n,
538: opts) == 0) {
539: m_freem(n);
540: if (opts)
541: m_freem(opts);
1.81 enami 542: udpstat.udps_fullsock++;
1.53 itojun 543: } else
544: sorwakeup(so);
545: }
546: }
1.72 itojun 547: #endif
1.53 itojun 548:
549: #ifdef INET6
550: static void
551: udp6_sendup(m, off, src, so)
552: struct mbuf *m;
553: int off; /* offset of data portion */
554: struct sockaddr *src;
555: struct socket *so;
556: {
557: struct mbuf *opts = NULL;
558: struct mbuf *n;
559: struct in6pcb *in6p = NULL;
560:
561: if (!so)
562: return;
563: if (so->so_proto->pr_domain->dom_family != AF_INET6)
564: return;
565: in6p = sotoin6pcb(so);
566:
567: #ifdef IPSEC
568: /* check AH/ESP integrity. */
569: if (so != NULL && ipsec6_in_reject_so(m, so)) {
570: ipsec6stat.in_polvio++;
571: return;
572: }
573: #endif /*IPSEC*/
574:
575: if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
576: if (in6p && (in6p->in6p_flags & IN6P_CONTROLOPTS
577: || in6p->in6p_socket->so_options & SO_TIMESTAMP)) {
578: struct ip6_hdr *ip6 = mtod(n, struct ip6_hdr *);
579: ip6_savecontrol(in6p, &opts, ip6, n);
580: }
581:
582: m_adj(n, off);
583: if (sbappendaddr(&so->so_rcv, src, n, opts) == 0) {
584: m_freem(n);
585: if (opts)
586: m_freem(opts);
587: udp6stat.udp6s_fullsock++;
588: } else
589: sorwakeup(so);
590: }
591: }
592: #endif
593:
1.72 itojun 594: #ifdef INET
1.53 itojun 595: static int
596: udp4_realinput(src, dst, m, off)
597: struct sockaddr_in *src;
598: struct sockaddr_in *dst;
599: struct mbuf *m;
600: int off; /* offset of udphdr */
601: {
602: u_int16_t *sport, *dport;
603: int rcvcnt;
604: struct in_addr *src4, *dst4;
605: struct inpcb *inp;
606:
607: rcvcnt = 0;
608: off += sizeof(struct udphdr); /* now, offset of payload */
609:
610: if (src->sin_family != AF_INET || dst->sin_family != AF_INET)
611: goto bad;
612:
613: src4 = &src->sin_addr;
614: sport = &src->sin_port;
615: dst4 = &dst->sin_addr;
616: dport = &dst->sin_port;
617:
1.73 itojun 618: if (IN_MULTICAST(dst4->s_addr) ||
1.53 itojun 619: in_broadcast(*dst4, m->m_pkthdr.rcvif)) {
620: struct inpcb *last;
621: /*
622: * Deliver a multicast or broadcast datagram to *all* sockets
623: * for which the local and remote addresses and ports match
624: * those of the incoming datagram. This allows more than
625: * one process to receive multi/broadcasts on the same port.
626: * (This really ought to be done for unicast datagrams as
627: * well, but that would cause problems with existing
628: * applications that open both address-specific sockets and
629: * a wildcard socket listening to the same port -- they would
630: * end up receiving duplicates of every unicast datagram.
631: * Those applications open the multiple sockets to overcome an
632: * inadequacy of the UDP socket interface, but for backwards
633: * compatibility we avoid the problem here rather than
634: * fixing the interface. Maybe 4.5BSD will remedy this?)
635: */
636:
637: /*
1.92 itojun 638: * KAME note: traditionally we dropped udpiphdr from mbuf here.
1.71 itojun 639: * we need udpiphdr for IPsec processing so we do that later.
1.53 itojun 640: */
641: /*
642: * Locate pcb(s) for datagram.
643: */
1.89 matt 644: CIRCLEQ_FOREACH(inp, &udbtable.inpt_queue, inp_queue) {
1.53 itojun 645: if (inp->inp_lport != *dport)
646: continue;
647: if (!in_nullhost(inp->inp_laddr)) {
648: if (!in_hosteq(inp->inp_laddr, *dst4))
649: continue;
650: }
651: if (!in_nullhost(inp->inp_faddr)) {
652: if (!in_hosteq(inp->inp_faddr, *src4) ||
653: inp->inp_fport != *sport)
654: continue;
655: }
656:
657: last = inp;
658: udp4_sendup(m, off, (struct sockaddr *)src,
659: inp->inp_socket);
660: rcvcnt++;
661:
662: /*
663: * Don't look for additional matches if this one does
664: * not have either the SO_REUSEPORT or SO_REUSEADDR
665: * socket options set. This heuristic avoids searching
666: * through all pcbs in the common case of a non-shared
667: * port. It assumes that an application will never
668: * clear these options after setting them.
669: */
670: if ((inp->inp_socket->so_options &
671: (SO_REUSEPORT|SO_REUSEADDR)) == 0)
672: break;
673: }
674: } else {
675: /*
676: * Locate pcb for datagram.
677: */
678: inp = in_pcblookup_connect(&udbtable, *src4, *sport, *dst4, *dport);
679: if (inp == 0) {
680: ++udpstat.udps_pcbhashmiss;
681: inp = in_pcblookup_bind(&udbtable, *dst4, *dport);
1.82 itojun 682: if (inp == 0)
1.53 itojun 683: return rcvcnt;
684: }
685:
686: udp4_sendup(m, off, (struct sockaddr *)src, inp->inp_socket);
687: rcvcnt++;
688: }
689:
690: bad:
691: return rcvcnt;
692: }
1.72 itojun 693: #endif
1.53 itojun 694:
695: #ifdef INET6
696: static int
697: udp6_realinput(af, src, dst, m, off)
698: int af; /* af on packet */
699: struct sockaddr_in6 *src;
700: struct sockaddr_in6 *dst;
701: struct mbuf *m;
702: int off; /* offset of udphdr */
703: {
1.79 itojun 704: u_int16_t sport, dport;
1.53 itojun 705: int rcvcnt;
1.79 itojun 706: struct in6_addr src6, dst6;
707: const struct in_addr *dst4;
1.53 itojun 708: struct in6pcb *in6p;
709:
710: rcvcnt = 0;
711: off += sizeof(struct udphdr); /* now, offset of payload */
712:
713: if (af != AF_INET && af != AF_INET6)
714: goto bad;
715: if (src->sin6_family != AF_INET6 || dst->sin6_family != AF_INET6)
716: goto bad;
717:
1.79 itojun 718: in6_embedscope(&src6, src, NULL, NULL);
719: sport = src->sin6_port;
720: in6_embedscope(&dst6, dst, NULL, NULL);
721: dport = dst->sin6_port;
1.88 itojun 722: dst4 = (struct in_addr *)&dst->sin6_addr.s6_addr[12];
1.53 itojun 723:
1.79 itojun 724: if (IN6_IS_ADDR_MULTICAST(&dst6) ||
725: (af == AF_INET && IN_MULTICAST(dst4->s_addr))) {
1.53 itojun 726: struct in6pcb *last;
727: /*
728: * Deliver a multicast or broadcast datagram to *all* sockets
729: * for which the local and remote addresses and ports match
730: * those of the incoming datagram. This allows more than
731: * one process to receive multi/broadcasts on the same port.
732: * (This really ought to be done for unicast datagrams as
733: * well, but that would cause problems with existing
734: * applications that open both address-specific sockets and
735: * a wildcard socket listening to the same port -- they would
736: * end up receiving duplicates of every unicast datagram.
737: * Those applications open the multiple sockets to overcome an
738: * inadequacy of the UDP socket interface, but for backwards
739: * compatibility we avoid the problem here rather than
740: * fixing the interface. Maybe 4.5BSD will remedy this?)
741: */
742:
743: /*
1.92 itojun 744: * KAME note: traditionally we dropped udpiphdr from mbuf here.
1.71 itojun 745: * we need udpiphdr for IPsec processing so we do that later.
1.53 itojun 746: */
747: /*
748: * Locate pcb(s) for datagram.
749: */
750: for (in6p = udb6.in6p_next; in6p != &udb6;
751: in6p = in6p->in6p_next) {
1.79 itojun 752: if (in6p->in6p_lport != dport)
1.53 itojun 753: continue;
754: if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
1.90 itojun 755: if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &dst6))
1.53 itojun 756: continue;
1.92 itojun 757: } else {
1.79 itojun 758: if (IN6_IS_ADDR_V4MAPPED(&dst6) &&
1.85 itojun 759: (in6p->in6p_flags & IN6P_IPV6_V6ONLY))
1.55 itojun 760: continue;
761: }
1.53 itojun 762: if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
1.79 itojun 763: if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,
764: &src6) || in6p->in6p_fport != sport)
1.53 itojun 765: continue;
1.92 itojun 766: } else {
1.79 itojun 767: if (IN6_IS_ADDR_V4MAPPED(&src6) &&
1.85 itojun 768: (in6p->in6p_flags & IN6P_IPV6_V6ONLY))
1.55 itojun 769: continue;
770: }
1.53 itojun 771:
772: last = in6p;
773: udp6_sendup(m, off, (struct sockaddr *)src,
774: in6p->in6p_socket);
775: rcvcnt++;
776:
777: /*
778: * Don't look for additional matches if this one does
779: * not have either the SO_REUSEPORT or SO_REUSEADDR
780: * socket options set. This heuristic avoids searching
781: * through all pcbs in the common case of a non-shared
782: * port. It assumes that an application will never
783: * clear these options after setting them.
784: */
785: if ((in6p->in6p_socket->so_options &
786: (SO_REUSEPORT|SO_REUSEADDR)) == 0)
787: break;
788: }
789: } else {
790: /*
791: * Locate pcb for datagram.
792: */
1.79 itojun 793: in6p = in6_pcblookup_connect(&udb6, &src6, sport,
794: &dst6, dport, 0);
1.53 itojun 795: if (in6p == 0) {
796: ++udpstat.udps_pcbhashmiss;
1.79 itojun 797: in6p = in6_pcblookup_bind(&udb6, &dst6, dport, 0);
1.82 itojun 798: if (in6p == 0)
1.53 itojun 799: return rcvcnt;
800: }
801:
802: udp6_sendup(m, off, (struct sockaddr *)src, in6p->in6p_socket);
803: rcvcnt++;
804: }
805:
806: bad:
807: return rcvcnt;
808: }
809: #endif
1.1 cgd 810:
1.72 itojun 811: #ifdef INET
1.1 cgd 812: /*
813: * Notify a udp user of an asynchronous error;
814: * just wake up so that he can collect error status.
815: */
1.7 mycroft 816: static void
1.1 cgd 817: udp_notify(inp, errno)
1.66 augustss 818: struct inpcb *inp;
1.7 mycroft 819: int errno;
1.1 cgd 820: {
1.34 mycroft 821:
1.1 cgd 822: inp->inp_socket->so_error = errno;
823: sorwakeup(inp->inp_socket);
824: sowwakeup(inp->inp_socket);
825: }
826:
1.27 christos 827: void *
828: udp_ctlinput(cmd, sa, v)
1.1 cgd 829: int cmd;
830: struct sockaddr *sa;
1.27 christos 831: void *v;
1.1 cgd 832: {
1.66 augustss 833: struct ip *ip = v;
834: struct udphdr *uh;
1.18 mycroft 835: void (*notify) __P((struct inpcb *, int)) = udp_notify;
1.21 mycroft 836: int errno;
1.1 cgd 837:
1.53 itojun 838: if (sa->sa_family != AF_INET
839: || sa->sa_len != sizeof(struct sockaddr_in))
1.51 itojun 840: return NULL;
1.20 mycroft 841: if ((unsigned)cmd >= PRC_NCMDS)
1.27 christos 842: return NULL;
1.20 mycroft 843: errno = inetctlerrmap[cmd];
1.18 mycroft 844: if (PRC_IS_REDIRECT(cmd))
1.19 mycroft 845: notify = in_rtchange, ip = 0;
1.18 mycroft 846: else if (cmd == PRC_HOSTDEAD)
1.19 mycroft 847: ip = 0;
1.23 cgd 848: else if (errno == 0)
1.27 christos 849: return NULL;
1.19 mycroft 850: if (ip) {
1.1 cgd 851: uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
1.34 mycroft 852: in_pcbnotify(&udbtable, satosin(sa)->sin_addr, uh->uh_dport,
853: ip->ip_src, uh->uh_sport, errno, notify);
1.53 itojun 854:
855: /* XXX mapped address case */
1.19 mycroft 856: } else
1.34 mycroft 857: in_pcbnotifyall(&udbtable, satosin(sa)->sin_addr, errno,
858: notify);
1.27 christos 859: return NULL;
1.1 cgd 860: }
861:
1.7 mycroft 862: int
1.27 christos 863: #if __STDC__
864: udp_output(struct mbuf *m, ...)
865: #else
866: udp_output(m, va_alist)
867: struct mbuf *m;
868: va_dcl
869: #endif
870: {
1.66 augustss 871: struct inpcb *inp;
872: struct udpiphdr *ui;
873: int len = m->m_pkthdr.len;
1.31 mycroft 874: int error = 0;
1.27 christos 875: va_list ap;
876:
877: va_start(ap, m);
878: inp = va_arg(ap, struct inpcb *);
879: va_end(ap);
1.1 cgd 880:
881: /*
882: * Calculate data length and get a mbuf
883: * for UDP and IP headers.
884: */
1.13 mycroft 885: M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT);
886: if (m == 0) {
887: error = ENOBUFS;
1.39 thorpej 888: goto release;
889: }
890:
891: /*
892: * Compute the packet length of the IP header, and
893: * punt if the length looks bogus.
894: */
895: if ((len + sizeof(struct udpiphdr)) > IP_MAXPACKET) {
896: error = EMSGSIZE;
1.13 mycroft 897: goto release;
898: }
1.1 cgd 899:
900: /*
901: * Fill in mbuf with extended UDP header
902: * and addresses and length put into network format.
903: */
904: ui = mtod(m, struct udpiphdr *);
905: ui->ui_pr = IPPROTO_UDP;
906: ui->ui_src = inp->inp_laddr;
907: ui->ui_dst = inp->inp_faddr;
908: ui->ui_sport = inp->inp_lport;
909: ui->ui_dport = inp->inp_fport;
1.78 thorpej 910: ui->ui_ulen = htons((u_int16_t)len + sizeof(struct udphdr));
1.1 cgd 911:
912: /*
1.78 thorpej 913: * Set up checksum and output datagram.
1.1 cgd 914: */
915: if (udpcksum) {
1.78 thorpej 916: /*
917: * XXX Cache pseudo-header checksum part for
918: * XXX "connected" UDP sockets.
919: */
920: ui->ui_sum = in_cksum_phdr(ui->ui_src.s_addr,
921: ui->ui_dst.s_addr, htons((u_int16_t)len +
922: sizeof(struct udphdr) + IPPROTO_UDP));
923: m->m_pkthdr.csum_flags = M_CSUM_UDPv4;
924: m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
925: } else
926: ui->ui_sum = 0;
1.1 cgd 927: ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
928: ((struct ip *)ui)->ip_ttl = inp->inp_ip.ip_ttl; /* XXX */
929: ((struct ip *)ui)->ip_tos = inp->inp_ip.ip_tos; /* XXX */
930: udpstat.udps_opackets++;
1.48 itojun 931:
932: #ifdef IPSEC
1.75 itojun 933: if (ipsec_setsocket(m, inp->inp_socket) != 0) {
934: error = ENOBUFS;
935: goto release;
936: }
1.48 itojun 937: #endif /*IPSEC*/
938:
1.31 mycroft 939: return (ip_output(m, inp->inp_options, &inp->inp_route,
1.12 mycroft 940: inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST),
1.31 mycroft 941: inp->inp_moptions));
1.1 cgd 942:
943: release:
944: m_freem(m);
945: return (error);
946: }
947:
1.42 thorpej 948: int udp_sendspace = 9216; /* really max datagram size */
949: int udp_recvspace = 40 * (1024 + sizeof(struct sockaddr_in));
1.1 cgd 950: /* 40 1K datagrams */
951:
952: /*ARGSUSED*/
1.7 mycroft 953: int
1.31 mycroft 954: udp_usrreq(so, req, m, nam, control, p)
1.1 cgd 955: struct socket *so;
956: int req;
1.31 mycroft 957: struct mbuf *m, *nam, *control;
1.30 mycroft 958: struct proc *p;
1.1 cgd 959: {
1.66 augustss 960: struct inpcb *inp;
1.1 cgd 961: int s;
1.66 augustss 962: int error = 0;
1.1 cgd 963:
964: if (req == PRU_CONTROL)
1.31 mycroft 965: return (in_control(so, (long)m, (caddr_t)nam,
1.30 mycroft 966: (struct ifnet *)control, p));
1.59 thorpej 967:
1.60 thorpej 968: if (req == PRU_PURGEIF) {
1.80 itojun 969: in_pcbpurgeif0(&udbtable, (struct ifnet *)control);
1.60 thorpej 970: in_purgeif((struct ifnet *)control);
971: in_pcbpurgeif(&udbtable, (struct ifnet *)control);
1.59 thorpej 972: return (0);
973: }
1.31 mycroft 974:
975: s = splsoftnet();
976: inp = sotoinpcb(so);
1.32 mycroft 977: #ifdef DIAGNOSTIC
978: if (req != PRU_SEND && req != PRU_SENDOOB && control)
979: panic("udp_usrreq: unexpected control mbuf");
980: #endif
1.31 mycroft 981: if (inp == 0 && req != PRU_ATTACH) {
982: error = EINVAL;
983: goto release;
984: }
985:
1.1 cgd 986: /*
987: * Note: need to block udp_input while changing
988: * the udp pcb queue and/or pcb addresses.
989: */
990: switch (req) {
991:
992: case PRU_ATTACH:
1.31 mycroft 993: if (inp != 0) {
994: error = EISCONN;
1.1 cgd 995: break;
996: }
1.31 mycroft 997: if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
998: error = soreserve(so, udp_sendspace, udp_recvspace);
999: if (error)
1000: break;
1001: }
1.18 mycroft 1002: error = in_pcballoc(so, &udbtable);
1.1 cgd 1003: if (error)
1004: break;
1.31 mycroft 1005: inp = sotoinpcb(so);
1006: inp->inp_ip.ip_ttl = ip_defttl;
1.1 cgd 1007: break;
1008:
1009: case PRU_DETACH:
1.31 mycroft 1010: in_pcbdetach(inp);
1.1 cgd 1011: break;
1012:
1013: case PRU_BIND:
1.31 mycroft 1014: error = in_pcbbind(inp, nam, p);
1.1 cgd 1015: break;
1016:
1017: case PRU_LISTEN:
1018: error = EOPNOTSUPP;
1019: break;
1020:
1021: case PRU_CONNECT:
1.31 mycroft 1022: error = in_pcbconnect(inp, nam);
1023: if (error)
1.1 cgd 1024: break;
1.31 mycroft 1025: soisconnected(so);
1.1 cgd 1026: break;
1027:
1028: case PRU_CONNECT2:
1029: error = EOPNOTSUPP;
1030: break;
1031:
1032: case PRU_DISCONNECT:
1.31 mycroft 1033: /*soisdisconnected(so);*/
1034: so->so_state &= ~SS_ISCONNECTED; /* XXX */
1.1 cgd 1035: in_pcbdisconnect(inp);
1.34 mycroft 1036: inp->inp_laddr = zeroin_addr; /* XXX */
1.35 mycroft 1037: in_pcbstate(inp, INP_BOUND); /* XXX */
1.1 cgd 1038: break;
1039:
1040: case PRU_SHUTDOWN:
1041: socantsendmore(so);
1042: break;
1043:
1.31 mycroft 1044: case PRU_RCVD:
1045: error = EOPNOTSUPP;
1.1 cgd 1046: break;
1047:
1.31 mycroft 1048: case PRU_SEND:
1.32 mycroft 1049: if (control && control->m_len) {
1050: m_freem(control);
1051: m_freem(m);
1052: error = EINVAL;
1053: break;
1054: }
1.31 mycroft 1055: {
1.35 mycroft 1056: struct in_addr laddr; /* XXX */
1.1 cgd 1057:
1.31 mycroft 1058: if (nam) {
1.35 mycroft 1059: laddr = inp->inp_laddr; /* XXX */
1.31 mycroft 1060: if ((so->so_state & SS_ISCONNECTED) != 0) {
1061: error = EISCONN;
1.32 mycroft 1062: goto die;
1.31 mycroft 1063: }
1064: error = in_pcbconnect(inp, nam);
1.32 mycroft 1065: if (error) {
1066: die:
1067: m_freem(m);
1.31 mycroft 1068: break;
1.32 mycroft 1069: }
1.31 mycroft 1070: } else {
1071: if ((so->so_state & SS_ISCONNECTED) == 0) {
1072: error = ENOTCONN;
1.32 mycroft 1073: goto die;
1.31 mycroft 1074: }
1075: }
1.33 mycroft 1076: error = udp_output(m, inp);
1.31 mycroft 1077: if (nam) {
1078: in_pcbdisconnect(inp);
1.35 mycroft 1079: inp->inp_laddr = laddr; /* XXX */
1080: in_pcbstate(inp, INP_BOUND); /* XXX */
1.31 mycroft 1081: }
1082: }
1.1 cgd 1083: break;
1084:
1085: case PRU_SENSE:
1086: /*
1087: * stat: don't bother with a blocksize.
1088: */
1.31 mycroft 1089: splx(s);
1.1 cgd 1090: return (0);
1091:
1.31 mycroft 1092: case PRU_RCVOOB:
1093: error = EOPNOTSUPP;
1094: break;
1095:
1.1 cgd 1096: case PRU_SENDOOB:
1.32 mycroft 1097: m_freem(control);
1.31 mycroft 1098: m_freem(m);
1.1 cgd 1099: error = EOPNOTSUPP;
1100: break;
1101:
1.31 mycroft 1102: case PRU_SOCKADDR:
1103: in_setsockaddr(inp, nam);
1104: break;
1105:
1106: case PRU_PEERADDR:
1107: in_setpeeraddr(inp, nam);
1108: break;
1.1 cgd 1109:
1110: default:
1111: panic("udp_usrreq");
1112: }
1113:
1114: release:
1.31 mycroft 1115: splx(s);
1.1 cgd 1116: return (error);
1.13 mycroft 1117: }
1118:
1119: /*
1120: * Sysctl for udp variables.
1121: */
1.27 christos 1122: int
1.13 mycroft 1123: udp_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
1124: int *name;
1125: u_int namelen;
1126: void *oldp;
1127: size_t *oldlenp;
1128: void *newp;
1129: size_t newlen;
1130: {
1131: /* All sysctl names at this level are terminal. */
1132: if (namelen != 1)
1133: return (ENOTDIR);
1134:
1135: switch (name[0]) {
1136: case UDPCTL_CHECKSUM:
1137: return (sysctl_int(oldp, oldlenp, newp, newlen, &udpcksum));
1.42 thorpej 1138: case UDPCTL_SENDSPACE:
1139: return (sysctl_int(oldp, oldlenp, newp, newlen,
1140: &udp_sendspace));
1141: case UDPCTL_RECVSPACE:
1.94 itojun 1142: return (sysctl_int(oldp, oldlenp, newp, newlen,
1.42 thorpej 1143: &udp_recvspace));
1.13 mycroft 1144: default:
1145: return (ENOPROTOOPT);
1146: }
1147: /* NOTREACHED */
1.1 cgd 1148: }
1.72 itojun 1149: #endif
CVSweb <webmaster@jp.NetBSD.org>