Annotation of src/sys/netinet/udp_usrreq.c, Revision 1.174.2.2
1.174.2.2! jym 1: /* $NetBSD: udp_usrreq.c,v 1.178 2009/07/19 23:17:33 minskim 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.174.2.2! jym 64: __KERNEL_RCSID(0, "$NetBSD: udp_usrreq.c,v 1.178 2009/07/19 23:17:33 minskim 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.174.2.1 jym 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.174.2.1 jym 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.174.2.1 jym 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.174.2.1 jym 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.174.2.1 jym 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.174.2.1 jym 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.174.2.2! jym 839: /*
! 840: * Check the minimum TTL for socket.
! 841: */
! 842: if (mtod(m, struct ip *)->ip_ttl < inp->inp_ip_minttl)
! 843: goto bad;
! 844:
1.53 itojun 845: udp4_sendup(m, off, (struct sockaddr *)src, inp->inp_socket);
846: rcvcnt++;
847: }
848:
849: bad:
850: return rcvcnt;
851: }
1.72 itojun 852: #endif
1.53 itojun 853:
854: #ifdef INET6
855: static int
1.119 matt 856: udp6_realinput(int af, struct sockaddr_in6 *src, struct sockaddr_in6 *dst,
857: struct mbuf *m, int off)
1.53 itojun 858: {
1.79 itojun 859: u_int16_t sport, dport;
1.53 itojun 860: int rcvcnt;
1.146 rpaulo 861: struct in6_addr src6, *dst6;
1.79 itojun 862: const struct in_addr *dst4;
1.109 itojun 863: struct inpcb_hdr *inph;
1.53 itojun 864: struct in6pcb *in6p;
865:
866: rcvcnt = 0;
867: off += sizeof(struct udphdr); /* now, offset of payload */
868:
869: if (af != AF_INET && af != AF_INET6)
870: goto bad;
871: if (src->sin6_family != AF_INET6 || dst->sin6_family != AF_INET6)
872: goto bad;
873:
1.146 rpaulo 874: src6 = src->sin6_addr;
875: if (sa6_recoverscope(src) != 0) {
876: /* XXX: should be impossible. */
877: goto bad;
878: }
1.79 itojun 879: sport = src->sin6_port;
1.146 rpaulo 880:
1.79 itojun 881: dport = dst->sin6_port;
1.88 itojun 882: dst4 = (struct in_addr *)&dst->sin6_addr.s6_addr[12];
1.146 rpaulo 883: dst6 = &dst->sin6_addr;
1.53 itojun 884:
1.146 rpaulo 885: if (IN6_IS_ADDR_MULTICAST(dst6) ||
1.79 itojun 886: (af == AF_INET && IN_MULTICAST(dst4->s_addr))) {
1.53 itojun 887: /*
888: * Deliver a multicast or broadcast datagram to *all* sockets
889: * for which the local and remote addresses and ports match
890: * those of the incoming datagram. This allows more than
891: * one process to receive multi/broadcasts on the same port.
892: * (This really ought to be done for unicast datagrams as
893: * well, but that would cause problems with existing
894: * applications that open both address-specific sockets and
895: * a wildcard socket listening to the same port -- they would
896: * end up receiving duplicates of every unicast datagram.
897: * Those applications open the multiple sockets to overcome an
898: * inadequacy of the UDP socket interface, but for backwards
899: * compatibility we avoid the problem here rather than
900: * fixing the interface. Maybe 4.5BSD will remedy this?)
901: */
902:
903: /*
1.92 itojun 904: * KAME note: traditionally we dropped udpiphdr from mbuf here.
1.71 itojun 905: * we need udpiphdr for IPsec processing so we do that later.
1.53 itojun 906: */
907: /*
908: * Locate pcb(s) for datagram.
909: */
1.109 itojun 910: CIRCLEQ_FOREACH(inph, &udbtable.inpt_queue, inph_queue) {
911: in6p = (struct in6pcb *)inph;
912: if (in6p->in6p_af != AF_INET6)
913: continue;
914:
1.79 itojun 915: if (in6p->in6p_lport != dport)
1.53 itojun 916: continue;
917: if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
1.146 rpaulo 918: if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr,
919: dst6))
1.53 itojun 920: continue;
1.92 itojun 921: } else {
1.146 rpaulo 922: if (IN6_IS_ADDR_V4MAPPED(dst6) &&
1.85 itojun 923: (in6p->in6p_flags & IN6P_IPV6_V6ONLY))
1.55 itojun 924: continue;
925: }
1.53 itojun 926: if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
1.79 itojun 927: if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,
928: &src6) || in6p->in6p_fport != sport)
1.53 itojun 929: continue;
1.92 itojun 930: } else {
1.79 itojun 931: if (IN6_IS_ADDR_V4MAPPED(&src6) &&
1.85 itojun 932: (in6p->in6p_flags & IN6P_IPV6_V6ONLY))
1.55 itojun 933: continue;
934: }
1.53 itojun 935:
936: udp6_sendup(m, off, (struct sockaddr *)src,
937: in6p->in6p_socket);
938: rcvcnt++;
939:
940: /*
941: * Don't look for additional matches if this one does
942: * not have either the SO_REUSEPORT or SO_REUSEADDR
943: * socket options set. This heuristic avoids searching
944: * through all pcbs in the common case of a non-shared
945: * port. It assumes that an application will never
946: * clear these options after setting them.
947: */
948: if ((in6p->in6p_socket->so_options &
949: (SO_REUSEPORT|SO_REUSEADDR)) == 0)
950: break;
951: }
952: } else {
953: /*
954: * Locate pcb for datagram.
955: */
1.146 rpaulo 956: in6p = in6_pcblookup_connect(&udbtable, &src6, sport, dst6,
957: dport, 0);
1.53 itojun 958: if (in6p == 0) {
1.166 thorpej 959: UDP_STATINC(UDP_STAT_PCBHASHMISS);
1.146 rpaulo 960: in6p = in6_pcblookup_bind(&udbtable, dst6, dport, 0);
1.82 itojun 961: if (in6p == 0)
1.53 itojun 962: return rcvcnt;
963: }
964:
965: udp6_sendup(m, off, (struct sockaddr *)src, in6p->in6p_socket);
966: rcvcnt++;
967: }
968:
969: bad:
970: return rcvcnt;
971: }
972: #endif
1.1 cgd 973:
1.72 itojun 974: #ifdef INET
1.1 cgd 975: /*
976: * Notify a udp user of an asynchronous error;
977: * just wake up so that he can collect error status.
978: */
1.7 mycroft 979: static void
1.119 matt 980: udp_notify(struct inpcb *inp, int errno)
1.1 cgd 981: {
982: inp->inp_socket->so_error = errno;
983: sorwakeup(inp->inp_socket);
984: sowwakeup(inp->inp_socket);
985: }
986:
1.27 christos 987: void *
1.157 dyoung 988: udp_ctlinput(int cmd, const struct sockaddr *sa, void *v)
1.1 cgd 989: {
1.66 augustss 990: struct ip *ip = v;
991: struct udphdr *uh;
1.119 matt 992: void (*notify)(struct inpcb *, int) = udp_notify;
1.21 mycroft 993: int errno;
1.1 cgd 994:
1.53 itojun 995: if (sa->sa_family != AF_INET
996: || sa->sa_len != sizeof(struct sockaddr_in))
1.51 itojun 997: return NULL;
1.20 mycroft 998: if ((unsigned)cmd >= PRC_NCMDS)
1.27 christos 999: return NULL;
1.20 mycroft 1000: errno = inetctlerrmap[cmd];
1.18 mycroft 1001: if (PRC_IS_REDIRECT(cmd))
1.19 mycroft 1002: notify = in_rtchange, ip = 0;
1.18 mycroft 1003: else if (cmd == PRC_HOSTDEAD)
1.19 mycroft 1004: ip = 0;
1.23 cgd 1005: else if (errno == 0)
1.27 christos 1006: return NULL;
1.19 mycroft 1007: if (ip) {
1.158 christos 1008: uh = (struct udphdr *)((char *)ip + (ip->ip_hl << 2));
1.157 dyoung 1009: in_pcbnotify(&udbtable, satocsin(sa)->sin_addr, uh->uh_dport,
1.34 mycroft 1010: ip->ip_src, uh->uh_sport, errno, notify);
1.53 itojun 1011:
1012: /* XXX mapped address case */
1.19 mycroft 1013: } else
1.157 dyoung 1014: in_pcbnotifyall(&udbtable, satocsin(sa)->sin_addr, errno,
1.34 mycroft 1015: notify);
1.27 christos 1016: return NULL;
1.1 cgd 1017: }
1018:
1.7 mycroft 1019: int
1.173 plunky 1020: udp_ctloutput(int op, struct socket *so, struct sockopt *sopt)
1.130 manu 1021: {
1022: int s;
1023: int error = 0;
1024: struct inpcb *inp;
1025: int family;
1.173 plunky 1026: int optval;
1.130 manu 1027:
1028: family = so->so_proto->pr_domain->dom_family;
1029:
1030: s = splsoftnet();
1031: switch (family) {
1032: #ifdef INET
1033: case PF_INET:
1.173 plunky 1034: if (sopt->sopt_level != IPPROTO_UDP) {
1035: error = ip_ctloutput(op, so, sopt);
1.130 manu 1036: goto end;
1037: }
1038: break;
1039: #endif
1040: #ifdef INET6
1041: case PF_INET6:
1.173 plunky 1042: if (sopt->sopt_level != IPPROTO_UDP) {
1043: error = ip6_ctloutput(op, so, sopt);
1.130 manu 1044: goto end;
1045: }
1046: break;
1047: #endif
1048: default:
1049: error = EAFNOSUPPORT;
1050: goto end;
1051: }
1052:
1053:
1054: switch (op) {
1055: case PRCO_SETOPT:
1056: inp = sotoinpcb(so);
1057:
1.173 plunky 1058: switch (sopt->sopt_name) {
1.130 manu 1059: case UDP_ENCAP:
1.173 plunky 1060: error = sockopt_getint(sopt, &optval);
1061: if (error)
1.153 yamt 1062: break;
1.131 perry 1063:
1.173 plunky 1064: switch(optval) {
1.130 manu 1065: #ifdef IPSEC_NAT_T
1066: case 0:
1067: inp->inp_flags &= ~INP_ESPINUDP_ALL;
1068: break;
1069:
1070: case UDP_ENCAP_ESPINUDP:
1071: inp->inp_flags &= ~INP_ESPINUDP_ALL;
1072: inp->inp_flags |= INP_ESPINUDP;
1073: break;
1.131 perry 1074:
1.130 manu 1075: case UDP_ENCAP_ESPINUDP_NON_IKE:
1076: inp->inp_flags &= ~INP_ESPINUDP_ALL;
1077: inp->inp_flags |= INP_ESPINUDP_NON_IKE;
1078: break;
1079: #endif
1080: default:
1081: error = EINVAL;
1082: break;
1083: }
1084: break;
1085:
1086: default:
1087: error = ENOPROTOOPT;
1088: break;
1089: }
1090: break;
1091:
1092: default:
1093: error = EINVAL;
1094: break;
1.131 perry 1095: }
1096:
1.130 manu 1097: end:
1098: splx(s);
1099: return error;
1100: }
1.131 perry 1101:
1.130 manu 1102:
1103: int
1.27 christos 1104: udp_output(struct mbuf *m, ...)
1105: {
1.66 augustss 1106: struct inpcb *inp;
1107: struct udpiphdr *ui;
1.125 thorpej 1108: struct route *ro;
1.66 augustss 1109: int len = m->m_pkthdr.len;
1.31 mycroft 1110: int error = 0;
1.27 christos 1111: va_list ap;
1112:
1.98 matt 1113: MCLAIM(m, &udp_tx_mowner);
1.27 christos 1114: va_start(ap, m);
1115: inp = va_arg(ap, struct inpcb *);
1116: va_end(ap);
1.1 cgd 1117:
1118: /*
1119: * Calculate data length and get a mbuf
1120: * for UDP and IP headers.
1121: */
1.13 mycroft 1122: M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT);
1123: if (m == 0) {
1124: error = ENOBUFS;
1.39 thorpej 1125: goto release;
1126: }
1127:
1128: /*
1129: * Compute the packet length of the IP header, and
1130: * punt if the length looks bogus.
1131: */
1.96 itojun 1132: if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
1.39 thorpej 1133: error = EMSGSIZE;
1.13 mycroft 1134: goto release;
1135: }
1.1 cgd 1136:
1137: /*
1138: * Fill in mbuf with extended UDP header
1139: * and addresses and length put into network format.
1140: */
1141: ui = mtod(m, struct udpiphdr *);
1142: ui->ui_pr = IPPROTO_UDP;
1143: ui->ui_src = inp->inp_laddr;
1144: ui->ui_dst = inp->inp_faddr;
1145: ui->ui_sport = inp->inp_lport;
1146: ui->ui_dport = inp->inp_fport;
1.78 thorpej 1147: ui->ui_ulen = htons((u_int16_t)len + sizeof(struct udphdr));
1.1 cgd 1148:
1.125 thorpej 1149: ro = &inp->inp_route;
1150:
1.1 cgd 1151: /*
1.78 thorpej 1152: * Set up checksum and output datagram.
1.1 cgd 1153: */
1154: if (udpcksum) {
1.78 thorpej 1155: /*
1156: * XXX Cache pseudo-header checksum part for
1157: * XXX "connected" UDP sockets.
1158: */
1159: ui->ui_sum = in_cksum_phdr(ui->ui_src.s_addr,
1160: ui->ui_dst.s_addr, htons((u_int16_t)len +
1161: sizeof(struct udphdr) + IPPROTO_UDP));
1.135 yamt 1162: m->m_pkthdr.csum_flags = M_CSUM_UDPv4;
1.78 thorpej 1163: m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
1164: } else
1165: ui->ui_sum = 0;
1.96 itojun 1166: ((struct ip *)ui)->ip_len = htons(sizeof (struct udpiphdr) + len);
1.1 cgd 1167: ((struct ip *)ui)->ip_ttl = inp->inp_ip.ip_ttl; /* XXX */
1168: ((struct ip *)ui)->ip_tos = inp->inp_ip.ip_tos; /* XXX */
1.166 thorpej 1169: UDP_STATINC(UDP_STAT_OPACKETS);
1.48 itojun 1170:
1.125 thorpej 1171: return (ip_output(m, inp->inp_options, ro,
1.12 mycroft 1172: inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST),
1.107 itojun 1173: inp->inp_moptions, inp->inp_socket));
1.1 cgd 1174:
1175: release:
1176: m_freem(m);
1177: return (error);
1178: }
1179:
1.42 thorpej 1180: int udp_sendspace = 9216; /* really max datagram size */
1181: int udp_recvspace = 40 * (1024 + sizeof(struct sockaddr_in));
1.1 cgd 1182: /* 40 1K datagrams */
1183:
1184: /*ARGSUSED*/
1.7 mycroft 1185: int
1.119 matt 1186: udp_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
1.145 christos 1187: struct mbuf *control, struct lwp *l)
1.1 cgd 1188: {
1.66 augustss 1189: struct inpcb *inp;
1.1 cgd 1190: int s;
1.66 augustss 1191: int error = 0;
1.1 cgd 1192:
1193: if (req == PRU_CONTROL)
1.158 christos 1194: return (in_control(so, (long)m, (void *)nam,
1.148 ad 1195: (struct ifnet *)control, l));
1.59 thorpej 1196:
1.149 tls 1197: s = splsoftnet();
1198:
1.60 thorpej 1199: if (req == PRU_PURGEIF) {
1.170 ad 1200: mutex_enter(softnet_lock);
1.80 itojun 1201: in_pcbpurgeif0(&udbtable, (struct ifnet *)control);
1.60 thorpej 1202: in_purgeif((struct ifnet *)control);
1203: in_pcbpurgeif(&udbtable, (struct ifnet *)control);
1.170 ad 1204: mutex_exit(softnet_lock);
1.149 tls 1205: splx(s);
1.59 thorpej 1206: return (0);
1207: }
1.31 mycroft 1208:
1209: inp = sotoinpcb(so);
1.32 mycroft 1210: #ifdef DIAGNOSTIC
1211: if (req != PRU_SEND && req != PRU_SENDOOB && control)
1212: panic("udp_usrreq: unexpected control mbuf");
1213: #endif
1.170 ad 1214: if (req == PRU_ATTACH) {
1215: sosetlock(so);
1216: } else if (inp == 0) {
1.31 mycroft 1217: error = EINVAL;
1218: goto release;
1219: }
1220:
1.1 cgd 1221: /*
1222: * Note: need to block udp_input while changing
1223: * the udp pcb queue and/or pcb addresses.
1224: */
1225: switch (req) {
1226:
1227: case PRU_ATTACH:
1.31 mycroft 1228: if (inp != 0) {
1229: error = EISCONN;
1.1 cgd 1230: break;
1231: }
1.98 matt 1232: #ifdef MBUFTRACE
1233: so->so_mowner = &udp_mowner;
1234: so->so_rcv.sb_mowner = &udp_rx_mowner;
1235: so->so_snd.sb_mowner = &udp_tx_mowner;
1236: #endif
1.31 mycroft 1237: if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1238: error = soreserve(so, udp_sendspace, udp_recvspace);
1239: if (error)
1240: break;
1241: }
1.18 mycroft 1242: error = in_pcballoc(so, &udbtable);
1.1 cgd 1243: if (error)
1244: break;
1.31 mycroft 1245: inp = sotoinpcb(so);
1246: inp->inp_ip.ip_ttl = ip_defttl;
1.1 cgd 1247: break;
1248:
1249: case PRU_DETACH:
1.31 mycroft 1250: in_pcbdetach(inp);
1.1 cgd 1251: break;
1252:
1253: case PRU_BIND:
1.148 ad 1254: error = in_pcbbind(inp, nam, l);
1.1 cgd 1255: break;
1256:
1257: case PRU_LISTEN:
1258: error = EOPNOTSUPP;
1259: break;
1260:
1261: case PRU_CONNECT:
1.148 ad 1262: error = in_pcbconnect(inp, nam, l);
1.31 mycroft 1263: if (error)
1.1 cgd 1264: break;
1.31 mycroft 1265: soisconnected(so);
1.1 cgd 1266: break;
1267:
1268: case PRU_CONNECT2:
1269: error = EOPNOTSUPP;
1270: break;
1271:
1272: case PRU_DISCONNECT:
1.31 mycroft 1273: /*soisdisconnected(so);*/
1274: so->so_state &= ~SS_ISCONNECTED; /* XXX */
1.1 cgd 1275: in_pcbdisconnect(inp);
1.34 mycroft 1276: inp->inp_laddr = zeroin_addr; /* XXX */
1.35 mycroft 1277: in_pcbstate(inp, INP_BOUND); /* XXX */
1.1 cgd 1278: break;
1279:
1280: case PRU_SHUTDOWN:
1281: socantsendmore(so);
1282: break;
1283:
1.31 mycroft 1284: case PRU_RCVD:
1285: error = EOPNOTSUPP;
1.1 cgd 1286: break;
1287:
1.31 mycroft 1288: case PRU_SEND:
1.32 mycroft 1289: if (control && control->m_len) {
1290: m_freem(control);
1291: m_freem(m);
1292: error = EINVAL;
1293: break;
1294: }
1.31 mycroft 1295: {
1.35 mycroft 1296: struct in_addr laddr; /* XXX */
1.1 cgd 1297:
1.31 mycroft 1298: if (nam) {
1.35 mycroft 1299: laddr = inp->inp_laddr; /* XXX */
1.31 mycroft 1300: if ((so->so_state & SS_ISCONNECTED) != 0) {
1301: error = EISCONN;
1.32 mycroft 1302: goto die;
1.31 mycroft 1303: }
1.148 ad 1304: error = in_pcbconnect(inp, nam, l);
1.100 matt 1305: if (error)
1306: goto die;
1.31 mycroft 1307: } else {
1308: if ((so->so_state & SS_ISCONNECTED) == 0) {
1309: error = ENOTCONN;
1.32 mycroft 1310: goto die;
1.31 mycroft 1311: }
1312: }
1.33 mycroft 1313: error = udp_output(m, inp);
1.100 matt 1314: m = NULL;
1.31 mycroft 1315: if (nam) {
1316: in_pcbdisconnect(inp);
1.35 mycroft 1317: inp->inp_laddr = laddr; /* XXX */
1318: in_pcbstate(inp, INP_BOUND); /* XXX */
1.31 mycroft 1319: }
1.100 matt 1320: die:
1321: if (m)
1322: m_freem(m);
1.31 mycroft 1323: }
1.1 cgd 1324: break;
1325:
1326: case PRU_SENSE:
1327: /*
1328: * stat: don't bother with a blocksize.
1329: */
1.31 mycroft 1330: splx(s);
1.1 cgd 1331: return (0);
1332:
1.31 mycroft 1333: case PRU_RCVOOB:
1334: error = EOPNOTSUPP;
1335: break;
1336:
1.1 cgd 1337: case PRU_SENDOOB:
1.32 mycroft 1338: m_freem(control);
1.31 mycroft 1339: m_freem(m);
1.1 cgd 1340: error = EOPNOTSUPP;
1341: break;
1342:
1.31 mycroft 1343: case PRU_SOCKADDR:
1344: in_setsockaddr(inp, nam);
1345: break;
1346:
1347: case PRU_PEERADDR:
1348: in_setpeeraddr(inp, nam);
1349: break;
1.1 cgd 1350:
1351: default:
1352: panic("udp_usrreq");
1353: }
1354:
1355: release:
1.31 mycroft 1356: splx(s);
1.1 cgd 1357: return (error);
1.13 mycroft 1358: }
1359:
1.166 thorpej 1360: static int
1361: sysctl_net_inet_udp_stats(SYSCTLFN_ARGS)
1362: {
1363:
1.172 thorpej 1364: return (NETSTAT_SYSCTL(udpstat_percpu, UDP_NSTATS));
1.166 thorpej 1365: }
1366:
1.13 mycroft 1367: /*
1368: * Sysctl for udp variables.
1369: */
1.114 atatat 1370: SYSCTL_SETUP(sysctl_net_inet_udp_setup, "sysctl net.inet.udp subtree setup")
1.13 mycroft 1371: {
1.114 atatat 1372:
1.116 atatat 1373: sysctl_createv(clog, 0, NULL, NULL,
1374: CTLFLAG_PERMANENT,
1.114 atatat 1375: CTLTYPE_NODE, "net", NULL,
1376: NULL, 0, NULL, 0,
1377: CTL_NET, CTL_EOL);
1.116 atatat 1378: sysctl_createv(clog, 0, NULL, NULL,
1379: CTLFLAG_PERMANENT,
1.114 atatat 1380: CTLTYPE_NODE, "inet", NULL,
1381: NULL, 0, NULL, 0,
1382: CTL_NET, PF_INET, CTL_EOL);
1.116 atatat 1383: sysctl_createv(clog, 0, NULL, NULL,
1384: CTLFLAG_PERMANENT,
1.122 atatat 1385: CTLTYPE_NODE, "udp",
1386: SYSCTL_DESCR("UDPv4 related settings"),
1.114 atatat 1387: NULL, 0, NULL, 0,
1388: CTL_NET, PF_INET, IPPROTO_UDP, CTL_EOL);
1389:
1.116 atatat 1390: sysctl_createv(clog, 0, NULL, NULL,
1391: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.122 atatat 1392: CTLTYPE_INT, "checksum",
1.123 heas 1393: SYSCTL_DESCR("Compute UDP checksums"),
1.114 atatat 1394: NULL, 0, &udpcksum, 0,
1395: CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_CHECKSUM,
1396: CTL_EOL);
1.116 atatat 1397: sysctl_createv(clog, 0, NULL, NULL,
1398: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.122 atatat 1399: CTLTYPE_INT, "sendspace",
1400: SYSCTL_DESCR("Default UDP send buffer size"),
1.114 atatat 1401: NULL, 0, &udp_sendspace, 0,
1402: CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_SENDSPACE,
1403: CTL_EOL);
1.116 atatat 1404: sysctl_createv(clog, 0, NULL, NULL,
1405: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.122 atatat 1406: CTLTYPE_INT, "recvspace",
1407: SYSCTL_DESCR("Default UDP receive buffer size"),
1.114 atatat 1408: NULL, 0, &udp_recvspace, 0,
1409: CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_RECVSPACE,
1410: CTL_EOL);
1.125 thorpej 1411: sysctl_createv(clog, 0, NULL, NULL,
1412: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1413: CTLTYPE_INT, "do_loopback_cksum",
1414: SYSCTL_DESCR("Perform UDP checksum on loopback"),
1415: NULL, 0, &udp_do_loopback_cksum, 0,
1416: CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_LOOPBACKCKSUM,
1417: CTL_EOL);
1.132 atatat 1418: sysctl_createv(clog, 0, NULL, NULL,
1419: CTLFLAG_PERMANENT,
1.134 atatat 1420: CTLTYPE_STRUCT, "pcblist",
1.132 atatat 1421: SYSCTL_DESCR("UDP protocol control block list"),
1422: sysctl_inpcblist, 0, &udbtable, 0,
1423: CTL_NET, PF_INET, IPPROTO_UDP, CTL_CREATE,
1424: CTL_EOL);
1.139 elad 1425: sysctl_createv(clog, 0, NULL, NULL,
1426: CTLFLAG_PERMANENT,
1427: CTLTYPE_STRUCT, "stats",
1428: SYSCTL_DESCR("UDP statistics"),
1.166 thorpej 1429: sysctl_net_inet_udp_stats, 0, NULL, 0,
1.139 elad 1430: CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_STATS,
1431: CTL_EOL);
1.1 cgd 1432: }
1.72 itojun 1433: #endif
1.130 manu 1434:
1.166 thorpej 1435: void
1436: udp_statinc(u_int stat)
1437: {
1438:
1439: KASSERT(stat < UDP_NSTATS);
1440: UDP_STATINC(stat);
1441: }
1442:
1.130 manu 1443: #if (defined INET && defined IPSEC_NAT_T)
1444: /*
1445: * Returns:
1446: * 1 if the packet was processed
1447: * 0 if normal UDP processing should take place
1.144 manu 1448: * -1 if an error occurent and m was freed
1.130 manu 1449: */
1450: static int
1.154 yamt 1451: udp4_espinudp(struct mbuf **mp, int off, struct sockaddr *src,
1.151 christos 1452: struct socket *so)
1.130 manu 1453: {
1454: size_t len;
1.158 christos 1455: void *data;
1.130 manu 1456: struct inpcb *inp;
1457: size_t skip = 0;
1458: size_t minlen;
1459: size_t iphdrlen;
1460: struct ip *ip;
1461: struct mbuf *n;
1.136 manu 1462: struct m_tag *tag;
1463: struct udphdr *udphdr;
1464: u_int16_t sport, dport;
1.144 manu 1465: struct mbuf *m = *mp;
1.130 manu 1466:
1.131 perry 1467: /*
1.130 manu 1468: * Collapse the mbuf chain if the first mbuf is too short
1469: * The longest case is: UDP + non ESP marker + ESP
1470: */
1471: minlen = off + sizeof(u_int64_t) + sizeof(struct esp);
1472: if (minlen > m->m_pkthdr.len)
1473: minlen = m->m_pkthdr.len;
1474:
1475: if (m->m_len < minlen) {
1.144 manu 1476: if ((*mp = m_pullup(m, minlen)) == NULL) {
1.130 manu 1477: printf("udp4_espinudp: m_pullup failed\n");
1.144 manu 1478: return -1;
1.130 manu 1479: }
1.144 manu 1480: m = *mp;
1.130 manu 1481: }
1482:
1.131 perry 1483: len = m->m_len - off;
1.158 christos 1484: data = mtod(m, char *) + off;
1.130 manu 1485: inp = sotoinpcb(so);
1486:
1487: /* Ignore keepalive packets */
1.158 christos 1488: if ((len == 1) && (*(unsigned char *)data == 0xff)) {
1.130 manu 1489: return 1;
1490: }
1491:
1.131 perry 1492: /*
1493: * Check that the payload is long enough to hold
1.130 manu 1494: * an ESP header and compute the length of encapsulation
1.131 perry 1495: * header to remove
1.130 manu 1496: */
1497: if (inp->inp_flags & INP_ESPINUDP) {
1498: u_int32_t *st = (u_int32_t *)data;
1499:
1500: if ((len <= sizeof(struct esp)) || (*st == 0))
1501: return 0; /* Normal UDP processing */
1502:
1503: skip = sizeof(struct udphdr);
1504: }
1505:
1506: if (inp->inp_flags & INP_ESPINUDP_NON_IKE) {
1.142 kleink 1507: u_int32_t *st = (u_int32_t *)data;
1.130 manu 1508:
1509: if ((len <= sizeof(u_int64_t) + sizeof(struct esp))
1.142 kleink 1510: || ((st[0] | st[1]) != 0))
1.130 manu 1511: return 0; /* Normal UDP processing */
1.131 perry 1512:
1.130 manu 1513: skip = sizeof(struct udphdr) + sizeof(u_int64_t);
1514: }
1515:
1516: /*
1.136 manu 1517: * Get the UDP ports. They are handled in network
1518: * order everywhere in IPSEC_NAT_T code.
1519: */
1.158 christos 1520: udphdr = (struct udphdr *)((char *)data - skip);
1.136 manu 1521: sport = udphdr->uh_sport;
1522: dport = udphdr->uh_dport;
1523:
1524: /*
1.130 manu 1525: * Remove the UDP header (and possibly the non ESP marker)
1526: * IP header lendth is iphdrlen
1.131 perry 1527: * Before:
1.130 manu 1528: * <--- off --->
1529: * +----+------+-----+
1530: * | IP | UDP | ESP |
1531: * +----+------+-----+
1532: * <-skip->
1533: * After:
1534: * +----+-----+
1535: * | IP | ESP |
1536: * +----+-----+
1537: * <-skip->
1538: */
1539: iphdrlen = off - sizeof(struct udphdr);
1.158 christos 1540: memmove(mtod(m, char *) + skip, mtod(m, void *), iphdrlen);
1.130 manu 1541: m_adj(m, skip);
1542:
1543: ip = mtod(m, struct ip *);
1544: ip->ip_len = htons(ntohs(ip->ip_len) - skip);
1545: ip->ip_p = IPPROTO_ESP;
1546:
1547: /*
1.131 perry 1548: * Copy the mbuf to avoid multiple free, as both
1549: * esp4_input (which we call) and udp_input (which
1.130 manu 1550: * called us) free the mbuf.
1551: */
1552: if ((n = m_dup(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
1553: printf("udp4_espinudp: m_dup failed\n");
1554: return 0;
1555: }
1556:
1.136 manu 1557: /*
1558: * Add a PACKET_TAG_IPSEC_NAT_T_PORT tag to remember
1559: * the source UDP port. This is required if we want
1560: * to select the right SPD for multiple hosts behind
1561: * same NAT
1562: */
1.137 manu 1563: if ((tag = m_tag_get(PACKET_TAG_IPSEC_NAT_T_PORTS,
1564: sizeof(sport) + sizeof(dport), M_DONTWAIT)) == NULL) {
1565: printf("udp4_espinudp: m_tag_get failed\n");
1.138 manu 1566: m_freem(n);
1.137 manu 1567: return 0;
1568: }
1.136 manu 1569: ((u_int16_t *)(tag + 1))[0] = sport;
1570: ((u_int16_t *)(tag + 1))[1] = dport;
1571: m_tag_prepend(n, tag);
1572:
1.147 christos 1573: #ifdef FAST_IPSEC
1.160 degroote 1574: ipsec4_common_input(n, iphdrlen, IPPROTO_ESP);
1.147 christos 1575: #else
1.130 manu 1576: esp4_input(n, iphdrlen);
1.147 christos 1577: #endif
1.130 manu 1578:
1579: /* We handled it, it shoudln't be handled by UDP */
1580: return 1;
1581: }
1582: #endif
CVSweb <webmaster@jp.NetBSD.org>