Annotation of src/sys/netinet/udp_usrreq.c, Revision 1.177
1.177 ! tsutsui 1: /* $NetBSD: udp_usrreq.c,v 1.176 2009/03/18 17:06:52 cegger Exp $ */
1.48 itojun 2:
3: /*
4: * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5: * All rights reserved.
1.94 itojun 6: *
1.48 itojun 7: * Redistribution and use in source and binary forms, with or without
8: * modification, are permitted provided that the following conditions
9: * are met:
10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
12: * 2. Redistributions in binary form must reproduce the above copyright
13: * notice, this list of conditions and the following disclaimer in the
14: * documentation and/or other materials provided with the distribution.
15: * 3. Neither the name of the project nor the names of its contributors
16: * may be used to endorse or promote products derived from this software
17: * without specific prior written permission.
1.94 itojun 18: *
1.48 itojun 19: * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22: * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29: * SUCH DAMAGE.
30: */
1.14 cgd 31:
1.1 cgd 32: /*
1.44 thorpej 33: * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
1.13 mycroft 34: * The Regents of the University of California. All rights reserved.
1.1 cgd 35: *
36: * Redistribution and use in source and binary forms, with or without
37: * modification, are permitted provided that the following conditions
38: * are met:
39: * 1. Redistributions of source code must retain the above copyright
40: * notice, this list of conditions and the following disclaimer.
41: * 2. Redistributions in binary form must reproduce the above copyright
42: * notice, this list of conditions and the following disclaimer in the
43: * documentation and/or other materials provided with the distribution.
1.104 agc 44: * 3. Neither the name of the University nor the names of its contributors
1.1 cgd 45: * may be used to endorse or promote products derived from this software
46: * without specific prior written permission.
47: *
48: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58: * SUCH DAMAGE.
59: *
1.44 thorpej 60: * @(#)udp_usrreq.c 8.6 (Berkeley) 5/23/95
1.1 cgd 61: */
1.91 lukem 62:
63: #include <sys/cdefs.h>
1.177 ! tsutsui 64: __KERNEL_RCSID(0, "$NetBSD: udp_usrreq.c,v 1.176 2009/03/18 17:06:52 cegger Exp $");
1.50 thorpej 65:
1.77 soda 66: #include "opt_inet.h"
1.174 christos 67: #include "opt_compat_netbsd.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.101 martin 71: #include "opt_mbuftrace.h"
1.1 cgd 72:
1.5 mycroft 73: #include <sys/param.h>
74: #include <sys/malloc.h>
75: #include <sys/mbuf.h>
76: #include <sys/protosw.h>
77: #include <sys/socket.h>
78: #include <sys/socketvar.h>
1.13 mycroft 79: #include <sys/errno.h>
1.5 mycroft 80: #include <sys/stat.h>
1.27 christos 81: #include <sys/systm.h>
82: #include <sys/proc.h>
1.53 itojun 83: #include <sys/domain.h>
1.27 christos 84: #include <sys/sysctl.h>
1.1 cgd 85:
1.5 mycroft 86: #include <net/if.h>
87: #include <net/route.h>
1.1 cgd 88:
1.5 mycroft 89: #include <netinet/in.h>
90: #include <netinet/in_systm.h>
1.15 cgd 91: #include <netinet/in_var.h>
1.5 mycroft 92: #include <netinet/ip.h>
93: #include <netinet/in_pcb.h>
94: #include <netinet/ip_var.h>
95: #include <netinet/ip_icmp.h>
96: #include <netinet/udp.h>
97: #include <netinet/udp_var.h>
1.166 thorpej 98: #include <netinet/udp_private.h>
1.1 cgd 99:
1.53 itojun 100: #ifdef INET6
101: #include <netinet/ip6.h>
102: #include <netinet/icmp6.h>
103: #include <netinet6/ip6_var.h>
1.167 thorpej 104: #include <netinet6/ip6_private.h>
1.53 itojun 105: #include <netinet6/in6_pcb.h>
106: #include <netinet6/udp6_var.h>
1.168 thorpej 107: #include <netinet6/udp6_private.h>
1.146 rpaulo 108: #include <netinet6/scope6_var.h>
1.53 itojun 109: #endif
110:
111: #ifndef INET6
112: /* always need ip6.h for IP6_EXTHDR_GET */
113: #include <netinet/ip6.h>
114: #endif
115:
1.76 itojun 116: #include "faith.h"
117: #if defined(NFAITH) && NFAITH > 0
118: #include <net/if_faith.h>
119: #endif
120:
1.27 christos 121: #include <machine/stdarg.h>
122:
1.105 jonathan 123: #ifdef FAST_IPSEC
124: #include <netipsec/ipsec.h>
1.169 thorpej 125: #include <netipsec/ipsec_var.h>
126: #include <netipsec/ipsec_private.h>
1.147 christos 127: #include <netipsec/esp.h>
1.105 jonathan 128: #ifdef INET6
129: #include <netipsec/ipsec6.h>
130: #endif
1.169 thorpej 131: #endif /* FAST_IPSEC */
1.105 jonathan 132:
1.48 itojun 133: #ifdef IPSEC
134: #include <netinet6/ipsec.h>
1.169 thorpej 135: #include <netinet6/ipsec_private.h>
1.147 christos 136: #include <netinet6/esp.h>
1.48 itojun 137: #include <netkey/key.h>
1.169 thorpej 138: #endif /* IPSEC */
1.48 itojun 139:
1.174 christos 140: #ifdef COMPAT_50
141: #include <compat/sys/socket.h>
142: #endif
143:
1.64 ws 144: #ifdef IPKDB
145: #include <ipkdb/ipkdb.h>
146: #endif
147:
1.8 mycroft 148: /*
149: * UDP protocol implementation.
150: * Per RFC 768, August, 1980.
151: */
152: int udpcksum = 1;
1.141 yamt 153: int udp_do_loopback_cksum = 0;
1.93 matt 154:
155: struct inpcbtable udbtable;
1.164 thorpej 156:
1.166 thorpej 157: percpu_t *udpstat_percpu;
1.8 mycroft 158:
1.72 itojun 159: #ifdef INET
1.130 manu 160: #ifdef IPSEC_NAT_T
1.144 manu 161: static int udp4_espinudp (struct mbuf **, int, struct sockaddr *,
1.130 manu 162: struct socket *);
163: #endif
1.119 matt 164: static void udp4_sendup (struct mbuf *, int, struct sockaddr *,
165: struct socket *);
166: static int udp4_realinput (struct sockaddr_in *, struct sockaddr_in *,
1.144 manu 167: struct mbuf **, int);
1.129 yamt 168: static int udp4_input_checksum(struct mbuf *, const struct udphdr *, int, int);
1.72 itojun 169: #endif
1.53 itojun 170: #ifdef INET6
1.119 matt 171: static void udp6_sendup (struct mbuf *, int, struct sockaddr *,
172: struct socket *);
173: static int udp6_realinput (int, struct sockaddr_in6 *,
174: struct sockaddr_in6 *, struct mbuf *, int);
1.129 yamt 175: static int udp6_input_checksum(struct mbuf *, const struct udphdr *, int, int);
1.53 itojun 176: #endif
1.72 itojun 177: #ifdef INET
1.119 matt 178: static void udp_notify (struct inpcb *, int);
1.72 itojun 179: #endif
1.7 mycroft 180:
1.26 mycroft 181: #ifndef UDBHASHSIZE
182: #define UDBHASHSIZE 128
183: #endif
184: int udbhashsize = UDBHASHSIZE;
185:
1.98 matt 186: #ifdef MBUFTRACE
1.150 dogcow 187: struct mowner udp_mowner = MOWNER_INIT("udp", "");
188: struct mowner udp_rx_mowner = MOWNER_INIT("udp", "rx");
189: struct mowner udp_tx_mowner = MOWNER_INIT("udp", "tx");
1.98 matt 190: #endif
191:
1.78 thorpej 192: #ifdef UDP_CSUM_COUNTERS
193: #include <sys/device.h>
194:
1.140 yamt 195: #if defined(INET)
1.78 thorpej 196: struct evcnt udp_hwcsum_bad = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
197: NULL, "udp", "hwcsum bad");
198: struct evcnt udp_hwcsum_ok = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
199: NULL, "udp", "hwcsum ok");
200: struct evcnt udp_hwcsum_data = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
201: NULL, "udp", "hwcsum data");
202: struct evcnt udp_swcsum = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
203: NULL, "udp", "swcsum");
204:
1.120 matt 205: EVCNT_ATTACH_STATIC(udp_hwcsum_bad);
206: EVCNT_ATTACH_STATIC(udp_hwcsum_ok);
207: EVCNT_ATTACH_STATIC(udp_hwcsum_data);
208: EVCNT_ATTACH_STATIC(udp_swcsum);
1.140 yamt 209: #endif /* defined(INET) */
210:
211: #if defined(INET6)
212: struct evcnt udp6_hwcsum_bad = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
213: NULL, "udp6", "hwcsum bad");
214: struct evcnt udp6_hwcsum_ok = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
215: NULL, "udp6", "hwcsum ok");
216: struct evcnt udp6_hwcsum_data = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
217: NULL, "udp6", "hwcsum data");
218: struct evcnt udp6_swcsum = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
219: NULL, "udp6", "swcsum");
220:
221: EVCNT_ATTACH_STATIC(udp6_hwcsum_bad);
222: EVCNT_ATTACH_STATIC(udp6_hwcsum_ok);
223: EVCNT_ATTACH_STATIC(udp6_hwcsum_data);
224: EVCNT_ATTACH_STATIC(udp6_swcsum);
225: #endif /* defined(INET6) */
226:
227: #define UDP_CSUM_COUNTER_INCR(ev) (ev)->ev_count++
1.120 matt 228:
1.78 thorpej 229: #else
230:
231: #define UDP_CSUM_COUNTER_INCR(ev) /* nothing */
232:
233: #endif /* UDP_CSUM_COUNTERS */
234:
1.7 mycroft 235: void
1.119 matt 236: udp_init(void)
1.1 cgd 237: {
1.18 mycroft 238:
1.35 mycroft 239: in_pcbinit(&udbtable, udbhashsize, udbhashsize);
1.78 thorpej 240:
1.98 matt 241: MOWNER_ATTACH(&udp_tx_mowner);
242: MOWNER_ATTACH(&udp_rx_mowner);
243: MOWNER_ATTACH(&udp_mowner);
1.166 thorpej 244:
1.171 yamt 245: #ifdef INET
1.166 thorpej 246: udpstat_percpu = percpu_alloc(sizeof(uint64_t) * UDP_NSTATS);
1.171 yamt 247: #endif
248: #ifdef INET6
249: udp6stat_percpu = percpu_alloc(sizeof(uint64_t) * UDP6_NSTATS);
250: #endif
1.1 cgd 251: }
252:
1.129 yamt 253: /*
254: * Checksum extended UDP header and data.
255: */
256:
257: int
258: udp_input_checksum(int af, struct mbuf *m, const struct udphdr *uh,
259: int iphlen, int len)
260: {
261:
262: switch (af) {
1.72 itojun 263: #ifdef INET
1.129 yamt 264: case AF_INET:
265: return udp4_input_checksum(m, uh, iphlen, len);
266: #endif
267: #ifdef INET6
268: case AF_INET6:
269: return udp6_input_checksum(m, uh, iphlen, len);
270: #endif
271: }
272: #ifdef DIAGNOSTIC
273: panic("udp_input_checksum: unknown af %d", af);
274: #endif
275: /* NOTREACHED */
276: return -1;
277: }
278:
279: #ifdef INET
280:
281: /*
282: * Checksum extended UDP header and data.
283: */
284:
285: static int
286: udp4_input_checksum(struct mbuf *m, const struct udphdr *uh,
287: int iphlen, int len)
288: {
289:
290: /*
291: * XXX it's better to record and check if this mbuf is
292: * already checked.
293: */
294:
295: if (uh->uh_sum == 0)
296: return 0;
297:
298: switch (m->m_pkthdr.csum_flags &
299: ((m->m_pkthdr.rcvif->if_csum_flags_rx & M_CSUM_UDPv4) |
300: M_CSUM_TCP_UDP_BAD | M_CSUM_DATA)) {
301: case M_CSUM_UDPv4|M_CSUM_TCP_UDP_BAD:
302: UDP_CSUM_COUNTER_INCR(&udp_hwcsum_bad);
303: goto badcsum;
304:
305: case M_CSUM_UDPv4|M_CSUM_DATA: {
306: u_int32_t hw_csum = m->m_pkthdr.csum_data;
307:
308: UDP_CSUM_COUNTER_INCR(&udp_hwcsum_data);
309: if (m->m_pkthdr.csum_flags & M_CSUM_NO_PSEUDOHDR) {
310: const struct ip *ip =
311: mtod(m, const struct ip *);
312:
313: hw_csum = in_cksum_phdr(ip->ip_src.s_addr,
314: ip->ip_dst.s_addr,
315: htons(hw_csum + len + IPPROTO_UDP));
316: }
317: if ((hw_csum ^ 0xffff) != 0)
318: goto badcsum;
319: break;
320: }
321:
322: case M_CSUM_UDPv4:
323: /* Checksum was okay. */
324: UDP_CSUM_COUNTER_INCR(&udp_hwcsum_ok);
325: break;
326:
327: default:
328: /*
329: * Need to compute it ourselves. Maybe skip checksum
330: * on loopback interfaces.
331: */
332: if (__predict_true(!(m->m_pkthdr.rcvif->if_flags &
333: IFF_LOOPBACK) ||
334: udp_do_loopback_cksum)) {
335: UDP_CSUM_COUNTER_INCR(&udp_swcsum);
336: if (in4_cksum(m, IPPROTO_UDP, iphlen, len) != 0)
337: goto badcsum;
338: }
339: break;
340: }
341:
342: return 0;
343:
344: badcsum:
1.166 thorpej 345: UDP_STATINC(UDP_STAT_BADSUM);
1.129 yamt 346: return -1;
347: }
348:
1.7 mycroft 349: void
1.27 christos 350: udp_input(struct mbuf *m, ...)
1.1 cgd 351: {
1.53 itojun 352: va_list ap;
353: struct sockaddr_in src, dst;
354: struct ip *ip;
355: struct udphdr *uh;
1.97 simonb 356: int iphlen;
1.53 itojun 357: int len;
358: int n;
1.96 itojun 359: u_int16_t ip_len;
1.53 itojun 360:
361: va_start(ap, m);
362: iphlen = va_arg(ap, int);
1.97 simonb 363: (void)va_arg(ap, int); /* ignore value, advance ap */
1.53 itojun 364: va_end(ap);
365:
1.98 matt 366: MCLAIM(m, &udp_rx_mowner);
1.166 thorpej 367: UDP_STATINC(UDP_STAT_IPACKETS);
1.53 itojun 368:
369: /*
370: * Get IP and UDP header together in first mbuf.
371: */
372: ip = mtod(m, struct ip *);
373: IP6_EXTHDR_GET(uh, struct udphdr *, m, iphlen, sizeof(struct udphdr));
374: if (uh == NULL) {
1.166 thorpej 375: UDP_STATINC(UDP_STAT_HDROPS);
1.53 itojun 376: return;
377: }
1.95 thorpej 378: KASSERT(UDP_HDR_ALIGNED_P(uh));
1.53 itojun 379:
1.57 itojun 380: /* destination port of 0 is illegal, based on RFC768. */
381: if (uh->uh_dport == 0)
382: goto bad;
383:
1.53 itojun 384: /*
385: * Make mbuf data length reflect UDP length.
386: * If not enough data to reflect UDP length, drop.
387: */
1.96 itojun 388: ip_len = ntohs(ip->ip_len);
1.53 itojun 389: len = ntohs((u_int16_t)uh->uh_ulen);
1.96 itojun 390: if (ip_len != iphlen + len) {
391: if (ip_len < iphlen + len || len < sizeof(struct udphdr)) {
1.166 thorpej 392: UDP_STATINC(UDP_STAT_BADLEN);
1.53 itojun 393: goto bad;
394: }
1.96 itojun 395: m_adj(m, iphlen + len - ip_len);
1.53 itojun 396: }
397:
398: /*
399: * Checksum extended UDP header and data.
400: */
1.129 yamt 401: if (udp4_input_checksum(m, uh, iphlen, len))
402: goto badcsum;
1.53 itojun 403:
404: /* construct source and dst sockaddrs. */
1.159 dyoung 405: sockaddr_in_init(&src, &ip->ip_src, uh->uh_sport);
406: sockaddr_in_init(&dst, &ip->ip_dst, uh->uh_dport);
1.53 itojun 407:
1.144 manu 408: if ((n = udp4_realinput(&src, &dst, &m, iphlen)) == -1) {
1.166 thorpej 409: UDP_STATINC(UDP_STAT_HDROPS);
1.144 manu 410: return;
411: }
1.53 itojun 412: #ifdef INET6
413: if (IN_MULTICAST(ip->ip_dst.s_addr) || n == 0) {
414: struct sockaddr_in6 src6, dst6;
415:
1.175 cegger 416: memset(&src6, 0, sizeof(src6));
1.53 itojun 417: src6.sin6_family = AF_INET6;
418: src6.sin6_len = sizeof(struct sockaddr_in6);
419: src6.sin6_addr.s6_addr[10] = src6.sin6_addr.s6_addr[11] = 0xff;
1.177 ! tsutsui 420: memcpy(&src6.sin6_addr.s6_addr[12], &ip->ip_src,
1.53 itojun 421: sizeof(ip->ip_src));
422: src6.sin6_port = uh->uh_sport;
1.175 cegger 423: memset(&dst6, 0, sizeof(dst6));
1.53 itojun 424: dst6.sin6_family = AF_INET6;
425: dst6.sin6_len = sizeof(struct sockaddr_in6);
426: dst6.sin6_addr.s6_addr[10] = dst6.sin6_addr.s6_addr[11] = 0xff;
1.177 ! tsutsui 427: memcpy(&dst6.sin6_addr.s6_addr[12], &ip->ip_dst,
1.53 itojun 428: sizeof(ip->ip_dst));
429: dst6.sin6_port = uh->uh_dport;
430:
431: n += udp6_realinput(AF_INET, &src6, &dst6, m, iphlen);
432: }
433: #endif
434:
435: if (n == 0) {
436: if (m->m_flags & (M_BCAST | M_MCAST)) {
1.166 thorpej 437: UDP_STATINC(UDP_STAT_NOPORTBCAST);
1.53 itojun 438: goto bad;
439: }
1.166 thorpej 440: UDP_STATINC(UDP_STAT_NOPORT);
1.64 ws 441: #ifdef IPKDB
1.53 itojun 442: if (checkipkdb(&ip->ip_src, uh->uh_sport, uh->uh_dport,
443: m, iphlen + sizeof(struct udphdr),
444: m->m_pkthdr.len - iphlen - sizeof(struct udphdr))) {
445: /*
446: * It was a debugger connect packet,
447: * just drop it now
448: */
449: goto bad;
450: }
451: #endif
452: icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
453: m = NULL;
454: }
455:
456: bad:
457: if (m)
458: m_freem(m);
1.78 thorpej 459: return;
460:
461: badcsum:
462: m_freem(m);
1.53 itojun 463: }
1.72 itojun 464: #endif
1.53 itojun 465:
466: #ifdef INET6
1.129 yamt 467: static int
468: udp6_input_checksum(struct mbuf *m, const struct udphdr *uh, int off, int len)
469: {
470:
1.140 yamt 471: /*
472: * XXX it's better to record and check if this mbuf is
473: * already checked.
474: */
475:
1.129 yamt 476: if (__predict_false((m->m_flags & M_LOOP) && !udp_do_loopback_cksum)) {
477: goto good;
478: }
479: if (uh->uh_sum == 0) {
1.168 thorpej 480: UDP6_STATINC(UDP6_STAT_NOSUM);
1.129 yamt 481: goto bad;
482: }
1.140 yamt 483:
484: switch (m->m_pkthdr.csum_flags &
485: ((m->m_pkthdr.rcvif->if_csum_flags_rx & M_CSUM_UDPv6) |
486: M_CSUM_TCP_UDP_BAD | M_CSUM_DATA)) {
487: case M_CSUM_UDPv6|M_CSUM_TCP_UDP_BAD:
488: UDP_CSUM_COUNTER_INCR(&udp6_hwcsum_bad);
1.168 thorpej 489: UDP6_STATINC(UDP6_STAT_BADSUM);
1.129 yamt 490: goto bad;
1.140 yamt 491:
492: #if 0 /* notyet */
493: case M_CSUM_UDPv6|M_CSUM_DATA:
494: #endif
495:
496: case M_CSUM_UDPv6:
497: /* Checksum was okay. */
498: UDP_CSUM_COUNTER_INCR(&udp6_hwcsum_ok);
499: break;
500:
501: default:
502: /*
503: * Need to compute it ourselves. Maybe skip checksum
504: * on loopback interfaces.
505: */
506: UDP_CSUM_COUNTER_INCR(&udp6_swcsum);
507: if (in6_cksum(m, IPPROTO_UDP, off, len) != 0) {
1.168 thorpej 508: UDP6_STATINC(UDP6_STAT_BADSUM);
1.140 yamt 509: goto bad;
510: }
1.129 yamt 511: }
512:
513: good:
514: return 0;
515: bad:
516: return -1;
517: }
518:
1.53 itojun 519: int
1.154 yamt 520: udp6_input(struct mbuf **mp, int *offp, int proto)
1.53 itojun 521: {
522: struct mbuf *m = *mp;
523: int off = *offp;
524: struct sockaddr_in6 src, dst;
525: struct ip6_hdr *ip6;
526: struct udphdr *uh;
527: u_int32_t plen, ulen;
528:
1.76 itojun 529: ip6 = mtod(m, struct ip6_hdr *);
530:
1.53 itojun 531: #if defined(NFAITH) && 0 < NFAITH
1.76 itojun 532: if (faithprefix(&ip6->ip6_dst)) {
533: /* send icmp6 host unreach? */
534: m_freem(m);
535: return IPPROTO_DONE;
1.53 itojun 536: }
537: #endif
538:
1.168 thorpej 539: UDP6_STATINC(UDP6_STAT_IPACKETS);
1.53 itojun 540:
541: /* check for jumbogram is done in ip6_input. we can trust pkthdr.len */
542: plen = m->m_pkthdr.len - off;
543: IP6_EXTHDR_GET(uh, struct udphdr *, m, off, sizeof(struct udphdr));
544: if (uh == NULL) {
1.167 thorpej 545: IP6_STATINC(IP6_STAT_TOOSHORT);
1.53 itojun 546: return IPPROTO_DONE;
547: }
1.95 thorpej 548: KASSERT(UDP_HDR_ALIGNED_P(uh));
1.53 itojun 549: ulen = ntohs((u_short)uh->uh_ulen);
1.69 itojun 550: /*
551: * RFC2675 section 4: jumbograms will have 0 in the UDP header field,
552: * iff payload length > 0xffff.
553: */
1.53 itojun 554: if (ulen == 0 && plen > 0xffff)
555: ulen = plen;
556:
557: if (plen != ulen) {
1.168 thorpej 558: UDP6_STATINC(UDP6_STAT_BADLEN);
1.54 itojun 559: goto bad;
560: }
561:
1.57 itojun 562: /* destination port of 0 is illegal, based on RFC768. */
563: if (uh->uh_dport == 0)
564: goto bad;
565:
1.54 itojun 566: /* Be proactive about malicious use of IPv4 mapped address */
567: if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
568: IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
569: /* XXX stat */
1.53 itojun 570: goto bad;
571: }
572:
573: /*
1.125 thorpej 574: * Checksum extended UDP header and data. Maybe skip checksum
575: * on loopback interfaces.
1.53 itojun 576: */
1.129 yamt 577: if (udp6_input_checksum(m, uh, off, ulen))
578: goto bad;
1.53 itojun 579:
580: /*
581: * Construct source and dst sockaddrs.
582: */
1.175 cegger 583: memset(&src, 0, sizeof(src));
1.53 itojun 584: src.sin6_family = AF_INET6;
585: src.sin6_len = sizeof(struct sockaddr_in6);
1.146 rpaulo 586: src.sin6_addr = ip6->ip6_src;
1.53 itojun 587: src.sin6_port = uh->uh_sport;
1.175 cegger 588: memset(&dst, 0, sizeof(dst));
1.53 itojun 589: dst.sin6_family = AF_INET6;
590: dst.sin6_len = sizeof(struct sockaddr_in6);
1.146 rpaulo 591: dst.sin6_addr = ip6->ip6_dst;
1.53 itojun 592: dst.sin6_port = uh->uh_dport;
593:
594: if (udp6_realinput(AF_INET6, &src, &dst, m, off) == 0) {
595: if (m->m_flags & M_MCAST) {
1.168 thorpej 596: UDP6_STATINC(UDP6_STAT_NOPORTMCAST);
1.53 itojun 597: goto bad;
598: }
1.168 thorpej 599: UDP6_STATINC(UDP6_STAT_NOPORT);
1.53 itojun 600: icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
601: m = NULL;
602: }
603:
604: bad:
605: if (m)
606: m_freem(m);
607: return IPPROTO_DONE;
608: }
609: #endif
610:
1.72 itojun 611: #ifdef INET
1.53 itojun 612: static void
1.119 matt 613: udp4_sendup(struct mbuf *m, int off /* offset of data portion */,
614: struct sockaddr *src, struct socket *so)
1.53 itojun 615: {
616: struct mbuf *opts = NULL;
617: struct mbuf *n;
618: struct inpcb *inp = NULL;
619:
620: if (!so)
621: return;
622: switch (so->so_proto->pr_domain->dom_family) {
623: case AF_INET:
624: inp = sotoinpcb(so);
625: break;
626: #ifdef INET6
627: case AF_INET6:
628: break;
629: #endif
630: default:
631: return;
632: }
633:
1.105 jonathan 634: #if defined(IPSEC) || defined(FAST_IPSEC)
1.53 itojun 635: /* check AH/ESP integrity. */
636: if (so != NULL && ipsec4_in_reject_so(m, so)) {
1.169 thorpej 637: IPSEC_STATINC(IPSEC_STAT_IN_POLVIO);
1.162 dyoung 638: if ((n = m_copypacket(m, M_DONTWAIT)) != NULL)
1.110 itojun 639: icmp_error(n, ICMP_UNREACH, ICMP_UNREACH_ADMIN_PROHIBIT,
640: 0, 0);
1.53 itojun 641: return;
642: }
643: #endif /*IPSEC*/
644:
1.162 dyoung 645: if ((n = m_copypacket(m, M_DONTWAIT)) != NULL) {
1.53 itojun 646: if (inp && (inp->inp_flags & INP_CONTROLOPTS
1.174 christos 647: #ifdef SO_OTIMESTAMP
648: || so->so_options & SO_OTIMESTAMP
649: #endif
1.53 itojun 650: || so->so_options & SO_TIMESTAMP)) {
651: struct ip *ip = mtod(n, struct ip *);
652: ip_savecontrol(inp, &opts, ip, n);
653: }
654:
655: m_adj(n, off);
656: if (sbappendaddr(&so->so_rcv, src, n,
657: opts) == 0) {
658: m_freem(n);
659: if (opts)
660: m_freem(opts);
1.124 darrenr 661: so->so_rcv.sb_overflowed++;
1.166 thorpej 662: UDP_STATINC(UDP_STAT_FULLSOCK);
1.53 itojun 663: } else
664: sorwakeup(so);
665: }
666: }
1.72 itojun 667: #endif
1.53 itojun 668:
669: #ifdef INET6
670: static void
1.119 matt 671: udp6_sendup(struct mbuf *m, int off /* offset of data portion */,
672: struct sockaddr *src, struct socket *so)
1.53 itojun 673: {
674: struct mbuf *opts = NULL;
675: struct mbuf *n;
676: struct in6pcb *in6p = NULL;
677:
678: if (!so)
679: return;
680: if (so->so_proto->pr_domain->dom_family != AF_INET6)
681: return;
682: in6p = sotoin6pcb(so);
683:
1.105 jonathan 684: #if defined(IPSEC) || defined(FAST_IPSEC)
1.53 itojun 685: /* check AH/ESP integrity. */
686: if (so != NULL && ipsec6_in_reject_so(m, so)) {
1.169 thorpej 687: IPSEC6_STATINC(IPSEC_STAT_IN_POLVIO);
1.162 dyoung 688: if ((n = m_copypacket(m, M_DONTWAIT)) != NULL)
1.110 itojun 689: icmp6_error(n, ICMP6_DST_UNREACH,
690: ICMP6_DST_UNREACH_ADMIN, 0);
1.53 itojun 691: return;
692: }
693: #endif /*IPSEC*/
694:
1.162 dyoung 695: if ((n = m_copypacket(m, M_DONTWAIT)) != NULL) {
1.53 itojun 696: if (in6p && (in6p->in6p_flags & IN6P_CONTROLOPTS
1.174 christos 697: #ifdef SO_OTIMESTAMP
698: || in6p->in6p_socket->so_options & SO_OTIMESTAMP
699: #endif
700: || in6p->in6p_socket->so_options & SO_TIMESTAMP)) {
1.53 itojun 701: struct ip6_hdr *ip6 = mtod(n, struct ip6_hdr *);
702: ip6_savecontrol(in6p, &opts, ip6, n);
703: }
704:
705: m_adj(n, off);
706: if (sbappendaddr(&so->so_rcv, src, n, opts) == 0) {
707: m_freem(n);
708: if (opts)
709: m_freem(opts);
1.124 darrenr 710: so->so_rcv.sb_overflowed++;
1.168 thorpej 711: UDP6_STATINC(UDP6_STAT_FULLSOCK);
1.53 itojun 712: } else
713: sorwakeup(so);
714: }
715: }
716: #endif
717:
1.72 itojun 718: #ifdef INET
1.53 itojun 719: static int
1.119 matt 720: udp4_realinput(struct sockaddr_in *src, struct sockaddr_in *dst,
1.144 manu 721: struct mbuf **mp, int off /* offset of udphdr */)
1.53 itojun 722: {
723: u_int16_t *sport, *dport;
724: int rcvcnt;
725: struct in_addr *src4, *dst4;
1.109 itojun 726: struct inpcb_hdr *inph;
1.53 itojun 727: struct inpcb *inp;
1.144 manu 728: struct mbuf *m = *mp;
1.53 itojun 729:
730: rcvcnt = 0;
731: off += sizeof(struct udphdr); /* now, offset of payload */
732:
733: if (src->sin_family != AF_INET || dst->sin_family != AF_INET)
734: goto bad;
735:
736: src4 = &src->sin_addr;
737: sport = &src->sin_port;
738: dst4 = &dst->sin_addr;
739: dport = &dst->sin_port;
740:
1.73 itojun 741: if (IN_MULTICAST(dst4->s_addr) ||
1.53 itojun 742: in_broadcast(*dst4, m->m_pkthdr.rcvif)) {
743: /*
744: * Deliver a multicast or broadcast datagram to *all* sockets
745: * for which the local and remote addresses and ports match
746: * those of the incoming datagram. This allows more than
747: * one process to receive multi/broadcasts on the same port.
748: * (This really ought to be done for unicast datagrams as
749: * well, but that would cause problems with existing
750: * applications that open both address-specific sockets and
751: * a wildcard socket listening to the same port -- they would
752: * end up receiving duplicates of every unicast datagram.
753: * Those applications open the multiple sockets to overcome an
754: * inadequacy of the UDP socket interface, but for backwards
755: * compatibility we avoid the problem here rather than
756: * fixing the interface. Maybe 4.5BSD will remedy this?)
757: */
758:
759: /*
1.92 itojun 760: * KAME note: traditionally we dropped udpiphdr from mbuf here.
1.71 itojun 761: * we need udpiphdr for IPsec processing so we do that later.
1.53 itojun 762: */
763: /*
764: * Locate pcb(s) for datagram.
765: */
1.109 itojun 766: CIRCLEQ_FOREACH(inph, &udbtable.inpt_queue, inph_queue) {
767: inp = (struct inpcb *)inph;
768: if (inp->inp_af != AF_INET)
769: continue;
770:
1.53 itojun 771: if (inp->inp_lport != *dport)
772: continue;
773: if (!in_nullhost(inp->inp_laddr)) {
774: if (!in_hosteq(inp->inp_laddr, *dst4))
775: continue;
776: }
777: if (!in_nullhost(inp->inp_faddr)) {
778: if (!in_hosteq(inp->inp_faddr, *src4) ||
779: inp->inp_fport != *sport)
780: continue;
781: }
782:
783: udp4_sendup(m, off, (struct sockaddr *)src,
784: inp->inp_socket);
785: rcvcnt++;
786:
787: /*
788: * Don't look for additional matches if this one does
789: * not have either the SO_REUSEPORT or SO_REUSEADDR
790: * socket options set. This heuristic avoids searching
791: * through all pcbs in the common case of a non-shared
792: * port. It assumes that an application will never
793: * clear these options after setting them.
794: */
795: if ((inp->inp_socket->so_options &
796: (SO_REUSEPORT|SO_REUSEADDR)) == 0)
797: break;
798: }
799: } else {
800: /*
801: * Locate pcb for datagram.
802: */
803: inp = in_pcblookup_connect(&udbtable, *src4, *sport, *dst4, *dport);
804: if (inp == 0) {
1.166 thorpej 805: UDP_STATINC(UDP_STAT_PCBHASHMISS);
1.53 itojun 806: inp = in_pcblookup_bind(&udbtable, *dst4, *dport);
1.82 itojun 807: if (inp == 0)
1.53 itojun 808: return rcvcnt;
809: }
810:
1.130 manu 811: #ifdef IPSEC_NAT_T
812: /* Handle ESP over UDP */
813: if (inp->inp_flags & INP_ESPINUDP_ALL) {
814: struct sockaddr *sa = (struct sockaddr *)src;
815:
1.144 manu 816: switch(udp4_espinudp(mp, off, sa, inp->inp_socket)) {
817: case -1: /* Error, m was freeed */
818: rcvcnt = -1;
819: goto bad;
820: break;
821:
822: case 1: /* ESP over UDP */
1.130 manu 823: rcvcnt++;
824: goto bad;
1.144 manu 825: break;
826:
827: case 0: /* plain UDP */
828: default: /* Unexpected */
829: /*
830: * Normal UDP processing will take place
831: * m may have changed.
832: */
833: m = *mp;
834: break;
1.130 manu 835: }
836: }
837: #endif
838:
1.53 itojun 839: udp4_sendup(m, off, (struct sockaddr *)src, inp->inp_socket);
840: rcvcnt++;
841: }
842:
843: bad:
844: return rcvcnt;
845: }
1.72 itojun 846: #endif
1.53 itojun 847:
848: #ifdef INET6
849: static int
1.119 matt 850: udp6_realinput(int af, struct sockaddr_in6 *src, struct sockaddr_in6 *dst,
851: struct mbuf *m, int off)
1.53 itojun 852: {
1.79 itojun 853: u_int16_t sport, dport;
1.53 itojun 854: int rcvcnt;
1.146 rpaulo 855: struct in6_addr src6, *dst6;
1.79 itojun 856: const struct in_addr *dst4;
1.109 itojun 857: struct inpcb_hdr *inph;
1.53 itojun 858: struct in6pcb *in6p;
859:
860: rcvcnt = 0;
861: off += sizeof(struct udphdr); /* now, offset of payload */
862:
863: if (af != AF_INET && af != AF_INET6)
864: goto bad;
865: if (src->sin6_family != AF_INET6 || dst->sin6_family != AF_INET6)
866: goto bad;
867:
1.146 rpaulo 868: src6 = src->sin6_addr;
869: if (sa6_recoverscope(src) != 0) {
870: /* XXX: should be impossible. */
871: goto bad;
872: }
1.79 itojun 873: sport = src->sin6_port;
1.146 rpaulo 874:
1.79 itojun 875: dport = dst->sin6_port;
1.88 itojun 876: dst4 = (struct in_addr *)&dst->sin6_addr.s6_addr[12];
1.146 rpaulo 877: dst6 = &dst->sin6_addr;
1.53 itojun 878:
1.146 rpaulo 879: if (IN6_IS_ADDR_MULTICAST(dst6) ||
1.79 itojun 880: (af == AF_INET && IN_MULTICAST(dst4->s_addr))) {
1.53 itojun 881: /*
882: * Deliver a multicast or broadcast datagram to *all* sockets
883: * for which the local and remote addresses and ports match
884: * those of the incoming datagram. This allows more than
885: * one process to receive multi/broadcasts on the same port.
886: * (This really ought to be done for unicast datagrams as
887: * well, but that would cause problems with existing
888: * applications that open both address-specific sockets and
889: * a wildcard socket listening to the same port -- they would
890: * end up receiving duplicates of every unicast datagram.
891: * Those applications open the multiple sockets to overcome an
892: * inadequacy of the UDP socket interface, but for backwards
893: * compatibility we avoid the problem here rather than
894: * fixing the interface. Maybe 4.5BSD will remedy this?)
895: */
896:
897: /*
1.92 itojun 898: * KAME note: traditionally we dropped udpiphdr from mbuf here.
1.71 itojun 899: * we need udpiphdr for IPsec processing so we do that later.
1.53 itojun 900: */
901: /*
902: * Locate pcb(s) for datagram.
903: */
1.109 itojun 904: CIRCLEQ_FOREACH(inph, &udbtable.inpt_queue, inph_queue) {
905: in6p = (struct in6pcb *)inph;
906: if (in6p->in6p_af != AF_INET6)
907: continue;
908:
1.79 itojun 909: if (in6p->in6p_lport != dport)
1.53 itojun 910: continue;
911: if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
1.146 rpaulo 912: if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr,
913: dst6))
1.53 itojun 914: continue;
1.92 itojun 915: } else {
1.146 rpaulo 916: if (IN6_IS_ADDR_V4MAPPED(dst6) &&
1.85 itojun 917: (in6p->in6p_flags & IN6P_IPV6_V6ONLY))
1.55 itojun 918: continue;
919: }
1.53 itojun 920: if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
1.79 itojun 921: if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,
922: &src6) || in6p->in6p_fport != sport)
1.53 itojun 923: continue;
1.92 itojun 924: } else {
1.79 itojun 925: if (IN6_IS_ADDR_V4MAPPED(&src6) &&
1.85 itojun 926: (in6p->in6p_flags & IN6P_IPV6_V6ONLY))
1.55 itojun 927: continue;
928: }
1.53 itojun 929:
930: udp6_sendup(m, off, (struct sockaddr *)src,
931: in6p->in6p_socket);
932: rcvcnt++;
933:
934: /*
935: * Don't look for additional matches if this one does
936: * not have either the SO_REUSEPORT or SO_REUSEADDR
937: * socket options set. This heuristic avoids searching
938: * through all pcbs in the common case of a non-shared
939: * port. It assumes that an application will never
940: * clear these options after setting them.
941: */
942: if ((in6p->in6p_socket->so_options &
943: (SO_REUSEPORT|SO_REUSEADDR)) == 0)
944: break;
945: }
946: } else {
947: /*
948: * Locate pcb for datagram.
949: */
1.146 rpaulo 950: in6p = in6_pcblookup_connect(&udbtable, &src6, sport, dst6,
951: dport, 0);
1.53 itojun 952: if (in6p == 0) {
1.166 thorpej 953: UDP_STATINC(UDP_STAT_PCBHASHMISS);
1.146 rpaulo 954: in6p = in6_pcblookup_bind(&udbtable, dst6, dport, 0);
1.82 itojun 955: if (in6p == 0)
1.53 itojun 956: return rcvcnt;
957: }
958:
959: udp6_sendup(m, off, (struct sockaddr *)src, in6p->in6p_socket);
960: rcvcnt++;
961: }
962:
963: bad:
964: return rcvcnt;
965: }
966: #endif
1.1 cgd 967:
1.72 itojun 968: #ifdef INET
1.1 cgd 969: /*
970: * Notify a udp user of an asynchronous error;
971: * just wake up so that he can collect error status.
972: */
1.7 mycroft 973: static void
1.119 matt 974: udp_notify(struct inpcb *inp, int errno)
1.1 cgd 975: {
976: inp->inp_socket->so_error = errno;
977: sorwakeup(inp->inp_socket);
978: sowwakeup(inp->inp_socket);
979: }
980:
1.27 christos 981: void *
1.157 dyoung 982: udp_ctlinput(int cmd, const struct sockaddr *sa, void *v)
1.1 cgd 983: {
1.66 augustss 984: struct ip *ip = v;
985: struct udphdr *uh;
1.119 matt 986: void (*notify)(struct inpcb *, int) = udp_notify;
1.21 mycroft 987: int errno;
1.1 cgd 988:
1.53 itojun 989: if (sa->sa_family != AF_INET
990: || sa->sa_len != sizeof(struct sockaddr_in))
1.51 itojun 991: return NULL;
1.20 mycroft 992: if ((unsigned)cmd >= PRC_NCMDS)
1.27 christos 993: return NULL;
1.20 mycroft 994: errno = inetctlerrmap[cmd];
1.18 mycroft 995: if (PRC_IS_REDIRECT(cmd))
1.19 mycroft 996: notify = in_rtchange, ip = 0;
1.18 mycroft 997: else if (cmd == PRC_HOSTDEAD)
1.19 mycroft 998: ip = 0;
1.23 cgd 999: else if (errno == 0)
1.27 christos 1000: return NULL;
1.19 mycroft 1001: if (ip) {
1.158 christos 1002: uh = (struct udphdr *)((char *)ip + (ip->ip_hl << 2));
1.157 dyoung 1003: in_pcbnotify(&udbtable, satocsin(sa)->sin_addr, uh->uh_dport,
1.34 mycroft 1004: ip->ip_src, uh->uh_sport, errno, notify);
1.53 itojun 1005:
1006: /* XXX mapped address case */
1.19 mycroft 1007: } else
1.157 dyoung 1008: in_pcbnotifyall(&udbtable, satocsin(sa)->sin_addr, errno,
1.34 mycroft 1009: notify);
1.27 christos 1010: return NULL;
1.1 cgd 1011: }
1012:
1.7 mycroft 1013: int
1.173 plunky 1014: udp_ctloutput(int op, struct socket *so, struct sockopt *sopt)
1.130 manu 1015: {
1016: int s;
1017: int error = 0;
1018: struct inpcb *inp;
1019: int family;
1.173 plunky 1020: int optval;
1.130 manu 1021:
1022: family = so->so_proto->pr_domain->dom_family;
1023:
1024: s = splsoftnet();
1025: switch (family) {
1026: #ifdef INET
1027: case PF_INET:
1.173 plunky 1028: if (sopt->sopt_level != IPPROTO_UDP) {
1029: error = ip_ctloutput(op, so, sopt);
1.130 manu 1030: goto end;
1031: }
1032: break;
1033: #endif
1034: #ifdef INET6
1035: case PF_INET6:
1.173 plunky 1036: if (sopt->sopt_level != IPPROTO_UDP) {
1037: error = ip6_ctloutput(op, so, sopt);
1.130 manu 1038: goto end;
1039: }
1040: break;
1041: #endif
1042: default:
1043: error = EAFNOSUPPORT;
1044: goto end;
1045: }
1046:
1047:
1048: switch (op) {
1049: case PRCO_SETOPT:
1050: inp = sotoinpcb(so);
1051:
1.173 plunky 1052: switch (sopt->sopt_name) {
1.130 manu 1053: case UDP_ENCAP:
1.173 plunky 1054: error = sockopt_getint(sopt, &optval);
1055: if (error)
1.153 yamt 1056: break;
1.131 perry 1057:
1.173 plunky 1058: switch(optval) {
1.130 manu 1059: #ifdef IPSEC_NAT_T
1060: case 0:
1061: inp->inp_flags &= ~INP_ESPINUDP_ALL;
1062: break;
1063:
1064: case UDP_ENCAP_ESPINUDP:
1065: inp->inp_flags &= ~INP_ESPINUDP_ALL;
1066: inp->inp_flags |= INP_ESPINUDP;
1067: break;
1.131 perry 1068:
1.130 manu 1069: case UDP_ENCAP_ESPINUDP_NON_IKE:
1070: inp->inp_flags &= ~INP_ESPINUDP_ALL;
1071: inp->inp_flags |= INP_ESPINUDP_NON_IKE;
1072: break;
1073: #endif
1074: default:
1075: error = EINVAL;
1076: break;
1077: }
1078: break;
1079:
1080: default:
1081: error = ENOPROTOOPT;
1082: break;
1083: }
1084: break;
1085:
1086: default:
1087: error = EINVAL;
1088: break;
1.131 perry 1089: }
1090:
1.130 manu 1091: end:
1092: splx(s);
1093: return error;
1094: }
1.131 perry 1095:
1.130 manu 1096:
1097: int
1.27 christos 1098: udp_output(struct mbuf *m, ...)
1099: {
1.66 augustss 1100: struct inpcb *inp;
1101: struct udpiphdr *ui;
1.125 thorpej 1102: struct route *ro;
1.66 augustss 1103: int len = m->m_pkthdr.len;
1.31 mycroft 1104: int error = 0;
1.27 christos 1105: va_list ap;
1106:
1.98 matt 1107: MCLAIM(m, &udp_tx_mowner);
1.27 christos 1108: va_start(ap, m);
1109: inp = va_arg(ap, struct inpcb *);
1110: va_end(ap);
1.1 cgd 1111:
1112: /*
1113: * Calculate data length and get a mbuf
1114: * for UDP and IP headers.
1115: */
1.13 mycroft 1116: M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT);
1117: if (m == 0) {
1118: error = ENOBUFS;
1.39 thorpej 1119: goto release;
1120: }
1121:
1122: /*
1123: * Compute the packet length of the IP header, and
1124: * punt if the length looks bogus.
1125: */
1.96 itojun 1126: if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
1.39 thorpej 1127: error = EMSGSIZE;
1.13 mycroft 1128: goto release;
1129: }
1.1 cgd 1130:
1131: /*
1132: * Fill in mbuf with extended UDP header
1133: * and addresses and length put into network format.
1134: */
1135: ui = mtod(m, struct udpiphdr *);
1136: ui->ui_pr = IPPROTO_UDP;
1137: ui->ui_src = inp->inp_laddr;
1138: ui->ui_dst = inp->inp_faddr;
1139: ui->ui_sport = inp->inp_lport;
1140: ui->ui_dport = inp->inp_fport;
1.78 thorpej 1141: ui->ui_ulen = htons((u_int16_t)len + sizeof(struct udphdr));
1.1 cgd 1142:
1.125 thorpej 1143: ro = &inp->inp_route;
1144:
1.1 cgd 1145: /*
1.78 thorpej 1146: * Set up checksum and output datagram.
1.1 cgd 1147: */
1148: if (udpcksum) {
1.78 thorpej 1149: /*
1150: * XXX Cache pseudo-header checksum part for
1151: * XXX "connected" UDP sockets.
1152: */
1153: ui->ui_sum = in_cksum_phdr(ui->ui_src.s_addr,
1154: ui->ui_dst.s_addr, htons((u_int16_t)len +
1155: sizeof(struct udphdr) + IPPROTO_UDP));
1.135 yamt 1156: m->m_pkthdr.csum_flags = M_CSUM_UDPv4;
1.78 thorpej 1157: m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
1158: } else
1159: ui->ui_sum = 0;
1.96 itojun 1160: ((struct ip *)ui)->ip_len = htons(sizeof (struct udpiphdr) + len);
1.1 cgd 1161: ((struct ip *)ui)->ip_ttl = inp->inp_ip.ip_ttl; /* XXX */
1162: ((struct ip *)ui)->ip_tos = inp->inp_ip.ip_tos; /* XXX */
1.166 thorpej 1163: UDP_STATINC(UDP_STAT_OPACKETS);
1.48 itojun 1164:
1.125 thorpej 1165: return (ip_output(m, inp->inp_options, ro,
1.12 mycroft 1166: inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST),
1.107 itojun 1167: inp->inp_moptions, inp->inp_socket));
1.1 cgd 1168:
1169: release:
1170: m_freem(m);
1171: return (error);
1172: }
1173:
1.42 thorpej 1174: int udp_sendspace = 9216; /* really max datagram size */
1175: int udp_recvspace = 40 * (1024 + sizeof(struct sockaddr_in));
1.1 cgd 1176: /* 40 1K datagrams */
1177:
1178: /*ARGSUSED*/
1.7 mycroft 1179: int
1.119 matt 1180: udp_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
1.145 christos 1181: struct mbuf *control, struct lwp *l)
1.1 cgd 1182: {
1.66 augustss 1183: struct inpcb *inp;
1.1 cgd 1184: int s;
1.66 augustss 1185: int error = 0;
1.1 cgd 1186:
1187: if (req == PRU_CONTROL)
1.158 christos 1188: return (in_control(so, (long)m, (void *)nam,
1.148 ad 1189: (struct ifnet *)control, l));
1.59 thorpej 1190:
1.149 tls 1191: s = splsoftnet();
1192:
1.60 thorpej 1193: if (req == PRU_PURGEIF) {
1.170 ad 1194: mutex_enter(softnet_lock);
1.80 itojun 1195: in_pcbpurgeif0(&udbtable, (struct ifnet *)control);
1.60 thorpej 1196: in_purgeif((struct ifnet *)control);
1197: in_pcbpurgeif(&udbtable, (struct ifnet *)control);
1.170 ad 1198: mutex_exit(softnet_lock);
1.149 tls 1199: splx(s);
1.59 thorpej 1200: return (0);
1201: }
1.31 mycroft 1202:
1203: inp = sotoinpcb(so);
1.32 mycroft 1204: #ifdef DIAGNOSTIC
1205: if (req != PRU_SEND && req != PRU_SENDOOB && control)
1206: panic("udp_usrreq: unexpected control mbuf");
1207: #endif
1.170 ad 1208: if (req == PRU_ATTACH) {
1209: sosetlock(so);
1210: } else if (inp == 0) {
1.31 mycroft 1211: error = EINVAL;
1212: goto release;
1213: }
1214:
1.1 cgd 1215: /*
1216: * Note: need to block udp_input while changing
1217: * the udp pcb queue and/or pcb addresses.
1218: */
1219: switch (req) {
1220:
1221: case PRU_ATTACH:
1.31 mycroft 1222: if (inp != 0) {
1223: error = EISCONN;
1.1 cgd 1224: break;
1225: }
1.98 matt 1226: #ifdef MBUFTRACE
1227: so->so_mowner = &udp_mowner;
1228: so->so_rcv.sb_mowner = &udp_rx_mowner;
1229: so->so_snd.sb_mowner = &udp_tx_mowner;
1230: #endif
1.31 mycroft 1231: if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1232: error = soreserve(so, udp_sendspace, udp_recvspace);
1233: if (error)
1234: break;
1235: }
1.18 mycroft 1236: error = in_pcballoc(so, &udbtable);
1.1 cgd 1237: if (error)
1238: break;
1.31 mycroft 1239: inp = sotoinpcb(so);
1240: inp->inp_ip.ip_ttl = ip_defttl;
1.1 cgd 1241: break;
1242:
1243: case PRU_DETACH:
1.31 mycroft 1244: in_pcbdetach(inp);
1.1 cgd 1245: break;
1246:
1247: case PRU_BIND:
1.148 ad 1248: error = in_pcbbind(inp, nam, l);
1.1 cgd 1249: break;
1250:
1251: case PRU_LISTEN:
1252: error = EOPNOTSUPP;
1253: break;
1254:
1255: case PRU_CONNECT:
1.148 ad 1256: error = in_pcbconnect(inp, nam, l);
1.31 mycroft 1257: if (error)
1.1 cgd 1258: break;
1.31 mycroft 1259: soisconnected(so);
1.1 cgd 1260: break;
1261:
1262: case PRU_CONNECT2:
1263: error = EOPNOTSUPP;
1264: break;
1265:
1266: case PRU_DISCONNECT:
1.31 mycroft 1267: /*soisdisconnected(so);*/
1268: so->so_state &= ~SS_ISCONNECTED; /* XXX */
1.1 cgd 1269: in_pcbdisconnect(inp);
1.34 mycroft 1270: inp->inp_laddr = zeroin_addr; /* XXX */
1.35 mycroft 1271: in_pcbstate(inp, INP_BOUND); /* XXX */
1.1 cgd 1272: break;
1273:
1274: case PRU_SHUTDOWN:
1275: socantsendmore(so);
1276: break;
1277:
1.31 mycroft 1278: case PRU_RCVD:
1279: error = EOPNOTSUPP;
1.1 cgd 1280: break;
1281:
1.31 mycroft 1282: case PRU_SEND:
1.32 mycroft 1283: if (control && control->m_len) {
1284: m_freem(control);
1285: m_freem(m);
1286: error = EINVAL;
1287: break;
1288: }
1.31 mycroft 1289: {
1.35 mycroft 1290: struct in_addr laddr; /* XXX */
1.1 cgd 1291:
1.31 mycroft 1292: if (nam) {
1.35 mycroft 1293: laddr = inp->inp_laddr; /* XXX */
1.31 mycroft 1294: if ((so->so_state & SS_ISCONNECTED) != 0) {
1295: error = EISCONN;
1.32 mycroft 1296: goto die;
1.31 mycroft 1297: }
1.148 ad 1298: error = in_pcbconnect(inp, nam, l);
1.100 matt 1299: if (error)
1300: goto die;
1.31 mycroft 1301: } else {
1302: if ((so->so_state & SS_ISCONNECTED) == 0) {
1303: error = ENOTCONN;
1.32 mycroft 1304: goto die;
1.31 mycroft 1305: }
1306: }
1.33 mycroft 1307: error = udp_output(m, inp);
1.100 matt 1308: m = NULL;
1.31 mycroft 1309: if (nam) {
1310: in_pcbdisconnect(inp);
1.35 mycroft 1311: inp->inp_laddr = laddr; /* XXX */
1312: in_pcbstate(inp, INP_BOUND); /* XXX */
1.31 mycroft 1313: }
1.100 matt 1314: die:
1315: if (m)
1316: m_freem(m);
1.31 mycroft 1317: }
1.1 cgd 1318: break;
1319:
1320: case PRU_SENSE:
1321: /*
1322: * stat: don't bother with a blocksize.
1323: */
1.31 mycroft 1324: splx(s);
1.1 cgd 1325: return (0);
1326:
1.31 mycroft 1327: case PRU_RCVOOB:
1328: error = EOPNOTSUPP;
1329: break;
1330:
1.1 cgd 1331: case PRU_SENDOOB:
1.32 mycroft 1332: m_freem(control);
1.31 mycroft 1333: m_freem(m);
1.1 cgd 1334: error = EOPNOTSUPP;
1335: break;
1336:
1.31 mycroft 1337: case PRU_SOCKADDR:
1338: in_setsockaddr(inp, nam);
1339: break;
1340:
1341: case PRU_PEERADDR:
1342: in_setpeeraddr(inp, nam);
1343: break;
1.1 cgd 1344:
1345: default:
1346: panic("udp_usrreq");
1347: }
1348:
1349: release:
1.31 mycroft 1350: splx(s);
1.1 cgd 1351: return (error);
1.13 mycroft 1352: }
1353:
1.166 thorpej 1354: static int
1355: sysctl_net_inet_udp_stats(SYSCTLFN_ARGS)
1356: {
1357:
1.172 thorpej 1358: return (NETSTAT_SYSCTL(udpstat_percpu, UDP_NSTATS));
1.166 thorpej 1359: }
1360:
1.13 mycroft 1361: /*
1362: * Sysctl for udp variables.
1363: */
1.114 atatat 1364: SYSCTL_SETUP(sysctl_net_inet_udp_setup, "sysctl net.inet.udp subtree setup")
1.13 mycroft 1365: {
1.114 atatat 1366:
1.116 atatat 1367: sysctl_createv(clog, 0, NULL, NULL,
1368: CTLFLAG_PERMANENT,
1.114 atatat 1369: CTLTYPE_NODE, "net", NULL,
1370: NULL, 0, NULL, 0,
1371: CTL_NET, CTL_EOL);
1.116 atatat 1372: sysctl_createv(clog, 0, NULL, NULL,
1373: CTLFLAG_PERMANENT,
1.114 atatat 1374: CTLTYPE_NODE, "inet", NULL,
1375: NULL, 0, NULL, 0,
1376: CTL_NET, PF_INET, CTL_EOL);
1.116 atatat 1377: sysctl_createv(clog, 0, NULL, NULL,
1378: CTLFLAG_PERMANENT,
1.122 atatat 1379: CTLTYPE_NODE, "udp",
1380: SYSCTL_DESCR("UDPv4 related settings"),
1.114 atatat 1381: NULL, 0, NULL, 0,
1382: CTL_NET, PF_INET, IPPROTO_UDP, CTL_EOL);
1383:
1.116 atatat 1384: sysctl_createv(clog, 0, NULL, NULL,
1385: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.122 atatat 1386: CTLTYPE_INT, "checksum",
1.123 heas 1387: SYSCTL_DESCR("Compute UDP checksums"),
1.114 atatat 1388: NULL, 0, &udpcksum, 0,
1389: CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_CHECKSUM,
1390: CTL_EOL);
1.116 atatat 1391: sysctl_createv(clog, 0, NULL, NULL,
1392: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.122 atatat 1393: CTLTYPE_INT, "sendspace",
1394: SYSCTL_DESCR("Default UDP send buffer size"),
1.114 atatat 1395: NULL, 0, &udp_sendspace, 0,
1396: CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_SENDSPACE,
1397: CTL_EOL);
1.116 atatat 1398: sysctl_createv(clog, 0, NULL, NULL,
1399: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.122 atatat 1400: CTLTYPE_INT, "recvspace",
1401: SYSCTL_DESCR("Default UDP receive buffer size"),
1.114 atatat 1402: NULL, 0, &udp_recvspace, 0,
1403: CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_RECVSPACE,
1404: CTL_EOL);
1.125 thorpej 1405: sysctl_createv(clog, 0, NULL, NULL,
1406: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1407: CTLTYPE_INT, "do_loopback_cksum",
1408: SYSCTL_DESCR("Perform UDP checksum on loopback"),
1409: NULL, 0, &udp_do_loopback_cksum, 0,
1410: CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_LOOPBACKCKSUM,
1411: CTL_EOL);
1.132 atatat 1412: sysctl_createv(clog, 0, NULL, NULL,
1413: CTLFLAG_PERMANENT,
1.134 atatat 1414: CTLTYPE_STRUCT, "pcblist",
1.132 atatat 1415: SYSCTL_DESCR("UDP protocol control block list"),
1416: sysctl_inpcblist, 0, &udbtable, 0,
1417: CTL_NET, PF_INET, IPPROTO_UDP, CTL_CREATE,
1418: CTL_EOL);
1.139 elad 1419: sysctl_createv(clog, 0, NULL, NULL,
1420: CTLFLAG_PERMANENT,
1421: CTLTYPE_STRUCT, "stats",
1422: SYSCTL_DESCR("UDP statistics"),
1.166 thorpej 1423: sysctl_net_inet_udp_stats, 0, NULL, 0,
1.139 elad 1424: CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_STATS,
1425: CTL_EOL);
1.1 cgd 1426: }
1.72 itojun 1427: #endif
1.130 manu 1428:
1.166 thorpej 1429: void
1430: udp_statinc(u_int stat)
1431: {
1432:
1433: KASSERT(stat < UDP_NSTATS);
1434: UDP_STATINC(stat);
1435: }
1436:
1.130 manu 1437: #if (defined INET && defined IPSEC_NAT_T)
1438: /*
1439: * Returns:
1440: * 1 if the packet was processed
1441: * 0 if normal UDP processing should take place
1.144 manu 1442: * -1 if an error occurent and m was freed
1.130 manu 1443: */
1444: static int
1.154 yamt 1445: udp4_espinudp(struct mbuf **mp, int off, struct sockaddr *src,
1.151 christos 1446: struct socket *so)
1.130 manu 1447: {
1448: size_t len;
1.158 christos 1449: void *data;
1.130 manu 1450: struct inpcb *inp;
1451: size_t skip = 0;
1452: size_t minlen;
1453: size_t iphdrlen;
1454: struct ip *ip;
1455: struct mbuf *n;
1.136 manu 1456: struct m_tag *tag;
1457: struct udphdr *udphdr;
1458: u_int16_t sport, dport;
1.144 manu 1459: struct mbuf *m = *mp;
1.130 manu 1460:
1.131 perry 1461: /*
1.130 manu 1462: * Collapse the mbuf chain if the first mbuf is too short
1463: * The longest case is: UDP + non ESP marker + ESP
1464: */
1465: minlen = off + sizeof(u_int64_t) + sizeof(struct esp);
1466: if (minlen > m->m_pkthdr.len)
1467: minlen = m->m_pkthdr.len;
1468:
1469: if (m->m_len < minlen) {
1.144 manu 1470: if ((*mp = m_pullup(m, minlen)) == NULL) {
1.130 manu 1471: printf("udp4_espinudp: m_pullup failed\n");
1.144 manu 1472: return -1;
1.130 manu 1473: }
1.144 manu 1474: m = *mp;
1.130 manu 1475: }
1476:
1.131 perry 1477: len = m->m_len - off;
1.158 christos 1478: data = mtod(m, char *) + off;
1.130 manu 1479: inp = sotoinpcb(so);
1480:
1481: /* Ignore keepalive packets */
1.158 christos 1482: if ((len == 1) && (*(unsigned char *)data == 0xff)) {
1.130 manu 1483: return 1;
1484: }
1485:
1.131 perry 1486: /*
1487: * Check that the payload is long enough to hold
1.130 manu 1488: * an ESP header and compute the length of encapsulation
1.131 perry 1489: * header to remove
1.130 manu 1490: */
1491: if (inp->inp_flags & INP_ESPINUDP) {
1492: u_int32_t *st = (u_int32_t *)data;
1493:
1494: if ((len <= sizeof(struct esp)) || (*st == 0))
1495: return 0; /* Normal UDP processing */
1496:
1497: skip = sizeof(struct udphdr);
1498: }
1499:
1500: if (inp->inp_flags & INP_ESPINUDP_NON_IKE) {
1.142 kleink 1501: u_int32_t *st = (u_int32_t *)data;
1.130 manu 1502:
1503: if ((len <= sizeof(u_int64_t) + sizeof(struct esp))
1.142 kleink 1504: || ((st[0] | st[1]) != 0))
1.130 manu 1505: return 0; /* Normal UDP processing */
1.131 perry 1506:
1.130 manu 1507: skip = sizeof(struct udphdr) + sizeof(u_int64_t);
1508: }
1509:
1510: /*
1.136 manu 1511: * Get the UDP ports. They are handled in network
1512: * order everywhere in IPSEC_NAT_T code.
1513: */
1.158 christos 1514: udphdr = (struct udphdr *)((char *)data - skip);
1.136 manu 1515: sport = udphdr->uh_sport;
1516: dport = udphdr->uh_dport;
1517:
1518: /*
1.130 manu 1519: * Remove the UDP header (and possibly the non ESP marker)
1520: * IP header lendth is iphdrlen
1.131 perry 1521: * Before:
1.130 manu 1522: * <--- off --->
1523: * +----+------+-----+
1524: * | IP | UDP | ESP |
1525: * +----+------+-----+
1526: * <-skip->
1527: * After:
1528: * +----+-----+
1529: * | IP | ESP |
1530: * +----+-----+
1531: * <-skip->
1532: */
1533: iphdrlen = off - sizeof(struct udphdr);
1.158 christos 1534: memmove(mtod(m, char *) + skip, mtod(m, void *), iphdrlen);
1.130 manu 1535: m_adj(m, skip);
1536:
1537: ip = mtod(m, struct ip *);
1538: ip->ip_len = htons(ntohs(ip->ip_len) - skip);
1539: ip->ip_p = IPPROTO_ESP;
1540:
1541: /*
1.131 perry 1542: * Copy the mbuf to avoid multiple free, as both
1543: * esp4_input (which we call) and udp_input (which
1.130 manu 1544: * called us) free the mbuf.
1545: */
1546: if ((n = m_dup(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
1547: printf("udp4_espinudp: m_dup failed\n");
1548: return 0;
1549: }
1550:
1.136 manu 1551: /*
1552: * Add a PACKET_TAG_IPSEC_NAT_T_PORT tag to remember
1553: * the source UDP port. This is required if we want
1554: * to select the right SPD for multiple hosts behind
1555: * same NAT
1556: */
1.137 manu 1557: if ((tag = m_tag_get(PACKET_TAG_IPSEC_NAT_T_PORTS,
1558: sizeof(sport) + sizeof(dport), M_DONTWAIT)) == NULL) {
1559: printf("udp4_espinudp: m_tag_get failed\n");
1.138 manu 1560: m_freem(n);
1.137 manu 1561: return 0;
1562: }
1.136 manu 1563: ((u_int16_t *)(tag + 1))[0] = sport;
1564: ((u_int16_t *)(tag + 1))[1] = dport;
1565: m_tag_prepend(n, tag);
1566:
1.147 christos 1567: #ifdef FAST_IPSEC
1.160 degroote 1568: ipsec4_common_input(n, iphdrlen, IPPROTO_ESP);
1.147 christos 1569: #else
1.130 manu 1570: esp4_input(n, iphdrlen);
1.147 christos 1571: #endif
1.130 manu 1572:
1573: /* We handled it, it shoudln't be handled by UDP */
1574: return 1;
1575: }
1576: #endif
CVSweb <webmaster@jp.NetBSD.org>