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