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