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