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