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