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