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