xref: /netbsd-src/sys/netinet/udp_usrreq.c (revision b757af438b42b93f8c6571f026d8b8ef3eaf5fc9)
1 /*	$NetBSD: udp_usrreq.c,v 1.186 2012/03/22 20:34:39 drochner Exp $	*/
2 
3 /*
4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the project nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 /*
33  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
34  *	The Regents of the University of California.  All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  * 3. Neither the name of the University nor the names of its contributors
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  *
60  *	@(#)udp_usrreq.c	8.6 (Berkeley) 5/23/95
61  */
62 
63 #include <sys/cdefs.h>
64 __KERNEL_RCSID(0, "$NetBSD: udp_usrreq.c,v 1.186 2012/03/22 20:34:39 drochner Exp $");
65 
66 #include "opt_inet.h"
67 #include "opt_compat_netbsd.h"
68 #include "opt_ipsec.h"
69 #include "opt_inet_csum.h"
70 #include "opt_ipkdb.h"
71 #include "opt_mbuftrace.h"
72 
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>
79 #include <sys/errno.h>
80 #include <sys/stat.h>
81 #include <sys/systm.h>
82 #include <sys/proc.h>
83 #include <sys/domain.h>
84 #include <sys/sysctl.h>
85 
86 #include <net/if.h>
87 #include <net/route.h>
88 
89 #include <netinet/in.h>
90 #include <netinet/in_systm.h>
91 #include <netinet/in_var.h>
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>
98 #include <netinet/udp_private.h>
99 #include <netinet/rfc6056.h>
100 
101 #ifdef INET6
102 #include <netinet/ip6.h>
103 #include <netinet/icmp6.h>
104 #include <netinet6/ip6_var.h>
105 #include <netinet6/ip6_private.h>
106 #include <netinet6/in6_pcb.h>
107 #include <netinet6/udp6_var.h>
108 #include <netinet6/udp6_private.h>
109 #include <netinet6/scope6_var.h>
110 #endif
111 
112 #ifndef INET6
113 /* always need ip6.h for IP6_EXTHDR_GET */
114 #include <netinet/ip6.h>
115 #endif
116 
117 #include "faith.h"
118 #if defined(NFAITH) && NFAITH > 0
119 #include <net/if_faith.h>
120 #endif
121 
122 #ifdef FAST_IPSEC
123 #include <netipsec/ipsec.h>
124 #include <netipsec/ipsec_var.h>
125 #include <netipsec/ipsec_private.h>
126 #include <netipsec/esp.h>
127 #ifdef INET6
128 #include <netipsec/ipsec6.h>
129 #endif
130 #endif	/* FAST_IPSEC */
131 
132 #ifdef COMPAT_50
133 #include <compat/sys/socket.h>
134 #endif
135 
136 #ifdef IPKDB
137 #include <ipkdb/ipkdb.h>
138 #endif
139 
140 /*
141  * UDP protocol implementation.
142  * Per RFC 768, August, 1980.
143  */
144 int	udpcksum = 1;
145 int	udp_do_loopback_cksum = 0;
146 
147 struct	inpcbtable udbtable;
148 
149 percpu_t *udpstat_percpu;
150 
151 #ifdef INET
152 #ifdef IPSEC_NAT_T
153 static int udp4_espinudp (struct mbuf **, int, struct sockaddr *,
154 	struct socket *);
155 #endif
156 static void udp4_sendup (struct mbuf *, int, struct sockaddr *,
157 	struct socket *);
158 static int udp4_realinput (struct sockaddr_in *, struct sockaddr_in *,
159 	struct mbuf **, int);
160 static int udp4_input_checksum(struct mbuf *, const struct udphdr *, int, int);
161 #endif
162 #ifdef INET6
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);
167 static int udp6_input_checksum(struct mbuf *, const struct udphdr *, int, int);
168 #endif
169 #ifdef INET
170 static	void udp_notify (struct inpcb *, int);
171 #endif
172 
173 #ifndef UDBHASHSIZE
174 #define	UDBHASHSIZE	128
175 #endif
176 int	udbhashsize = UDBHASHSIZE;
177 
178 #ifdef MBUFTRACE
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");
182 #endif
183 
184 #ifdef UDP_CSUM_COUNTERS
185 #include <sys/device.h>
186 
187 #if defined(INET)
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 
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);
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++
220 
221 #else
222 
223 #define	UDP_CSUM_COUNTER_INCR(ev)	/* nothing */
224 
225 #endif /* UDP_CSUM_COUNTERS */
226 
227 static void sysctl_net_inet_udp_setup(struct sysctllog **);
228 
229 void
230 udp_init(void)
231 {
232 
233 	sysctl_net_inet_udp_setup(NULL);
234 
235 	in_pcbinit(&udbtable, udbhashsize, udbhashsize);
236 
237 	MOWNER_ATTACH(&udp_tx_mowner);
238 	MOWNER_ATTACH(&udp_rx_mowner);
239 	MOWNER_ATTACH(&udp_mowner);
240 
241 #ifdef INET
242 	udpstat_percpu = percpu_alloc(sizeof(uint64_t) * UDP_NSTATS);
243 #endif
244 #ifdef INET6
245 	udp6stat_percpu = percpu_alloc(sizeof(uint64_t) * UDP6_NSTATS);
246 #endif
247 }
248 
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) {
259 #ifdef INET
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:
341 	UDP_STATINC(UDP_STAT_BADSUM);
342 	return -1;
343 }
344 
345 void
346 udp_input(struct mbuf *m, ...)
347 {
348 	va_list ap;
349 	struct sockaddr_in src, dst;
350 	struct ip *ip;
351 	struct udphdr *uh;
352 	int iphlen;
353 	int len;
354 	int n;
355 	u_int16_t ip_len;
356 
357 	va_start(ap, m);
358 	iphlen = va_arg(ap, int);
359 	(void)va_arg(ap, int);		/* ignore value, advance ap */
360 	va_end(ap);
361 
362 	MCLAIM(m, &udp_rx_mowner);
363 	UDP_STATINC(UDP_STAT_IPACKETS);
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) {
371 		UDP_STATINC(UDP_STAT_HDROPS);
372 		return;
373 	}
374 	KASSERT(UDP_HDR_ALIGNED_P(uh));
375 
376 	/* destination port of 0 is illegal, based on RFC768. */
377 	if (uh->uh_dport == 0)
378 		goto bad;
379 
380 	/*
381 	 * Make mbuf data length reflect UDP length.
382 	 * If not enough data to reflect UDP length, drop.
383 	 */
384 	ip_len = ntohs(ip->ip_len);
385 	len = ntohs((u_int16_t)uh->uh_ulen);
386 	if (ip_len != iphlen + len) {
387 		if (ip_len < iphlen + len || len < sizeof(struct udphdr)) {
388 			UDP_STATINC(UDP_STAT_BADLEN);
389 			goto bad;
390 		}
391 		m_adj(m, iphlen + len - ip_len);
392 	}
393 
394 	/*
395 	 * Checksum extended UDP header and data.
396 	 */
397 	if (udp4_input_checksum(m, uh, iphlen, len))
398 		goto badcsum;
399 
400 	/* construct source and dst sockaddrs. */
401 	sockaddr_in_init(&src, &ip->ip_src, uh->uh_sport);
402 	sockaddr_in_init(&dst, &ip->ip_dst, uh->uh_dport);
403 
404 	if ((n = udp4_realinput(&src, &dst, &m, iphlen)) == -1) {
405 		UDP_STATINC(UDP_STAT_HDROPS);
406 		return;
407 	}
408 #ifdef INET6
409 	if (IN_MULTICAST(ip->ip_dst.s_addr) || n == 0) {
410 		struct sockaddr_in6 src6, dst6;
411 
412 		memset(&src6, 0, sizeof(src6));
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;
416 		memcpy(&src6.sin6_addr.s6_addr[12], &ip->ip_src,
417 			sizeof(ip->ip_src));
418 		src6.sin6_port = uh->uh_sport;
419 		memset(&dst6, 0, sizeof(dst6));
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;
423 		memcpy(&dst6.sin6_addr.s6_addr[12], &ip->ip_dst,
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)) {
433 			UDP_STATINC(UDP_STAT_NOPORTBCAST);
434 			goto bad;
435 		}
436 		UDP_STATINC(UDP_STAT_NOPORT);
437 #ifdef IPKDB
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);
455 	return;
456 
457 badcsum:
458 	m_freem(m);
459 }
460 #endif
461 
462 #ifdef INET6
463 static int
464 udp6_input_checksum(struct mbuf *m, const struct udphdr *uh, int off, int len)
465 {
466 
467 	/*
468 	 * XXX it's better to record and check if this mbuf is
469 	 * already checked.
470 	 */
471 
472 	if (__predict_false((m->m_flags & M_LOOP) && !udp_do_loopback_cksum)) {
473 		goto good;
474 	}
475 	if (uh->uh_sum == 0) {
476 		UDP6_STATINC(UDP6_STAT_NOSUM);
477 		goto bad;
478 	}
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);
485 		UDP6_STATINC(UDP6_STAT_BADSUM);
486 		goto bad;
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) {
504 			UDP6_STATINC(UDP6_STAT_BADSUM);
505 			goto bad;
506 		}
507 	}
508 
509 good:
510 	return 0;
511 bad:
512 	return -1;
513 }
514 
515 int
516 udp6_input(struct mbuf **mp, int *offp, int proto)
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 
525 	ip6 = mtod(m, struct ip6_hdr *);
526 
527 #if defined(NFAITH) && 0 < NFAITH
528 	if (faithprefix(&ip6->ip6_dst)) {
529 		/* send icmp6 host unreach? */
530 		m_freem(m);
531 		return IPPROTO_DONE;
532 	}
533 #endif
534 
535 	UDP6_STATINC(UDP6_STAT_IPACKETS);
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) {
541 		IP6_STATINC(IP6_STAT_TOOSHORT);
542 		return IPPROTO_DONE;
543 	}
544 	KASSERT(UDP_HDR_ALIGNED_P(uh));
545 	ulen = ntohs((u_short)uh->uh_ulen);
546 	/*
547 	 * RFC2675 section 4: jumbograms will have 0 in the UDP header field,
548 	 * iff payload length > 0xffff.
549 	 */
550 	if (ulen == 0 && plen > 0xffff)
551 		ulen = plen;
552 
553 	if (plen != ulen) {
554 		UDP6_STATINC(UDP6_STAT_BADLEN);
555 		goto bad;
556 	}
557 
558 	/* destination port of 0 is illegal, based on RFC768. */
559 	if (uh->uh_dport == 0)
560 		goto bad;
561 
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 */
566 		goto bad;
567 	}
568 
569 	/*
570 	 * Checksum extended UDP header and data.  Maybe skip checksum
571 	 * on loopback interfaces.
572 	 */
573 	if (udp6_input_checksum(m, uh, off, ulen))
574 		goto bad;
575 
576 	/*
577 	 * Construct source and dst sockaddrs.
578 	 */
579 	memset(&src, 0, sizeof(src));
580 	src.sin6_family = AF_INET6;
581 	src.sin6_len = sizeof(struct sockaddr_in6);
582 	src.sin6_addr = ip6->ip6_src;
583 	src.sin6_port = uh->uh_sport;
584 	memset(&dst, 0, sizeof(dst));
585 	dst.sin6_family = AF_INET6;
586 	dst.sin6_len = sizeof(struct sockaddr_in6);
587 	dst.sin6_addr = ip6->ip6_dst;
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) {
592 			UDP6_STATINC(UDP6_STAT_NOPORTMCAST);
593 			goto bad;
594 		}
595 		UDP6_STATINC(UDP6_STAT_NOPORT);
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 
607 #ifdef INET
608 static void
609 udp4_sendup(struct mbuf *m, int off /* offset of data portion */,
610 	struct sockaddr *src, struct socket *so)
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 
630 #if defined(FAST_IPSEC)
631 	/* check AH/ESP integrity. */
632 	if (so != NULL && ipsec4_in_reject_so(m, so)) {
633 		IPSEC_STATINC(IPSEC_STAT_IN_POLVIO);
634 		if ((n = m_copypacket(m, M_DONTWAIT)) != NULL)
635 			icmp_error(n, ICMP_UNREACH, ICMP_UNREACH_ADMIN_PROHIBIT,
636 			    0, 0);
637 		return;
638 	}
639 #endif /*IPSEC*/
640 
641 	if ((n = m_copypacket(m, M_DONTWAIT)) != NULL) {
642 		if (inp && (inp->inp_flags & INP_CONTROLOPTS
643 #ifdef SO_OTIMESTAMP
644 			 || so->so_options & SO_OTIMESTAMP
645 #endif
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);
657 			so->so_rcv.sb_overflowed++;
658 			UDP_STATINC(UDP_STAT_FULLSOCK);
659 		} else
660 			sorwakeup(so);
661 	}
662 }
663 #endif
664 
665 #ifdef INET6
666 static void
667 udp6_sendup(struct mbuf *m, int off /* offset of data portion */,
668 	struct sockaddr *src, struct socket *so)
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 
680 #if defined(FAST_IPSEC)
681 	/* check AH/ESP integrity. */
682 	if (so != NULL && ipsec6_in_reject_so(m, so)) {
683 		IPSEC6_STATINC(IPSEC_STAT_IN_POLVIO);
684 		if ((n = m_copypacket(m, M_DONTWAIT)) != NULL)
685 			icmp6_error(n, ICMP6_DST_UNREACH,
686 			    ICMP6_DST_UNREACH_ADMIN, 0);
687 		return;
688 	}
689 #endif /*IPSEC*/
690 
691 	if ((n = m_copypacket(m, M_DONTWAIT)) != NULL) {
692 		if (in6p && (in6p->in6p_flags & IN6P_CONTROLOPTS
693 #ifdef SO_OTIMESTAMP
694 		    || in6p->in6p_socket->so_options & SO_OTIMESTAMP
695 #endif
696 		    || in6p->in6p_socket->so_options & SO_TIMESTAMP)) {
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);
706 			so->so_rcv.sb_overflowed++;
707 			UDP6_STATINC(UDP6_STAT_FULLSOCK);
708 		} else
709 			sorwakeup(so);
710 	}
711 }
712 #endif
713 
714 #ifdef INET
715 static int
716 udp4_realinput(struct sockaddr_in *src, struct sockaddr_in *dst,
717 	struct mbuf **mp, int off /* offset of udphdr */)
718 {
719 	u_int16_t *sport, *dport;
720 	int rcvcnt;
721 	struct in_addr *src4, *dst4;
722 	struct inpcb_hdr *inph;
723 	struct inpcb *inp;
724 	struct mbuf *m = *mp;
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 
737 	if (IN_MULTICAST(dst4->s_addr) ||
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 		/*
756 		 * KAME note: traditionally we dropped udpiphdr from mbuf here.
757 		 * we need udpiphdr for IPsec processing so we do that later.
758 		 */
759 		/*
760 		 * Locate pcb(s) for datagram.
761 		 */
762 		CIRCLEQ_FOREACH(inph, &udbtable.inpt_queue, inph_queue) {
763 			inp = (struct inpcb *)inph;
764 			if (inp->inp_af != AF_INET)
765 				continue;
766 
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 		 */
799 		inp = in_pcblookup_connect(&udbtable, *src4, *sport, *dst4,
800 		    *dport, 0);
801 		if (inp == 0) {
802 			UDP_STATINC(UDP_STAT_PCBHASHMISS);
803 			inp = in_pcblookup_bind(&udbtable, *dst4, *dport);
804 			if (inp == 0)
805 				return rcvcnt;
806 		}
807 
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 
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 */
820 				rcvcnt++;
821 				goto bad;
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;
832 			}
833 		}
834 #endif
835 
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 
842 		udp4_sendup(m, off, (struct sockaddr *)src, inp->inp_socket);
843 		rcvcnt++;
844 	}
845 
846 bad:
847 	return rcvcnt;
848 }
849 #endif
850 
851 #ifdef INET6
852 static int
853 udp6_realinput(int af, struct sockaddr_in6 *src, struct sockaddr_in6 *dst,
854 	struct mbuf *m, int off)
855 {
856 	u_int16_t sport, dport;
857 	int rcvcnt;
858 	struct in6_addr src6, *dst6;
859 	const struct in_addr *dst4;
860 	struct inpcb_hdr *inph;
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 
871 	src6 = src->sin6_addr;
872 	if (sa6_recoverscope(src) != 0) {
873 		/* XXX: should be impossible. */
874 		goto bad;
875 	}
876 	sport = src->sin6_port;
877 
878 	dport = dst->sin6_port;
879 	dst4 = (struct in_addr *)&dst->sin6_addr.s6_addr[12];
880 	dst6 = &dst->sin6_addr;
881 
882 	if (IN6_IS_ADDR_MULTICAST(dst6) ||
883 	    (af == AF_INET && IN_MULTICAST(dst4->s_addr))) {
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 		/*
901 		 * KAME note: traditionally we dropped udpiphdr from mbuf here.
902 		 * we need udpiphdr for IPsec processing so we do that later.
903 		 */
904 		/*
905 		 * Locate pcb(s) for datagram.
906 		 */
907 		CIRCLEQ_FOREACH(inph, &udbtable.inpt_queue, inph_queue) {
908 			in6p = (struct in6pcb *)inph;
909 			if (in6p->in6p_af != AF_INET6)
910 				continue;
911 
912 			if (in6p->in6p_lport != dport)
913 				continue;
914 			if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
915 				if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr,
916 				    dst6))
917 					continue;
918 			} else {
919 				if (IN6_IS_ADDR_V4MAPPED(dst6) &&
920 				    (in6p->in6p_flags & IN6P_IPV6_V6ONLY))
921 					continue;
922 			}
923 			if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
924 				if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,
925 				    &src6) || in6p->in6p_fport != sport)
926 					continue;
927 			} else {
928 				if (IN6_IS_ADDR_V4MAPPED(&src6) &&
929 				    (in6p->in6p_flags & IN6P_IPV6_V6ONLY))
930 					continue;
931 			}
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 		 */
953 		in6p = in6_pcblookup_connect(&udbtable, &src6, sport, dst6,
954 					     dport, 0, 0);
955 		if (in6p == 0) {
956 			UDP_STATINC(UDP_STAT_PCBHASHMISS);
957 			in6p = in6_pcblookup_bind(&udbtable, dst6, dport, 0);
958 			if (in6p == 0)
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
970 
971 #ifdef INET
972 /*
973  * Notify a udp user of an asynchronous error;
974  * just wake up so that he can collect error status.
975  */
976 static void
977 udp_notify(struct inpcb *inp, int errno)
978 {
979 	inp->inp_socket->so_error = errno;
980 	sorwakeup(inp->inp_socket);
981 	sowwakeup(inp->inp_socket);
982 }
983 
984 void *
985 udp_ctlinput(int cmd, const struct sockaddr *sa, void *v)
986 {
987 	struct ip *ip = v;
988 	struct udphdr *uh;
989 	void (*notify)(struct inpcb *, int) = udp_notify;
990 	int errno;
991 
992 	if (sa->sa_family != AF_INET
993 	 || sa->sa_len != sizeof(struct sockaddr_in))
994 		return NULL;
995 	if ((unsigned)cmd >= PRC_NCMDS)
996 		return NULL;
997 	errno = inetctlerrmap[cmd];
998 	if (PRC_IS_REDIRECT(cmd))
999 		notify = in_rtchange, ip = 0;
1000 	else if (cmd == PRC_HOSTDEAD)
1001 		ip = 0;
1002 	else if (errno == 0)
1003 		return NULL;
1004 	if (ip) {
1005 		uh = (struct udphdr *)((char *)ip + (ip->ip_hl << 2));
1006 		in_pcbnotify(&udbtable, satocsin(sa)->sin_addr, uh->uh_dport,
1007 		    ip->ip_src, uh->uh_sport, errno, notify);
1008 
1009 		/* XXX mapped address case */
1010 	} else
1011 		in_pcbnotifyall(&udbtable, satocsin(sa)->sin_addr, errno,
1012 		    notify);
1013 	return NULL;
1014 }
1015 
1016 int
1017 udp_ctloutput(int op, struct socket *so, struct sockopt *sopt)
1018 {
1019 	int s;
1020 	int error = 0;
1021 	struct inpcb *inp;
1022 	int family;
1023 	int optval;
1024 
1025 	family = so->so_proto->pr_domain->dom_family;
1026 
1027 	s = splsoftnet();
1028 	switch (family) {
1029 #ifdef INET
1030 	case PF_INET:
1031 		if (sopt->sopt_level != IPPROTO_UDP) {
1032 			error = ip_ctloutput(op, so, sopt);
1033 			goto end;
1034 		}
1035 		break;
1036 #endif
1037 #ifdef INET6
1038 	case PF_INET6:
1039 		if (sopt->sopt_level != IPPROTO_UDP) {
1040 			error = ip6_ctloutput(op, so, sopt);
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 
1055 		switch (sopt->sopt_name) {
1056 		case UDP_ENCAP:
1057 			error = sockopt_getint(sopt, &optval);
1058 			if (error)
1059 				break;
1060 
1061 			switch(optval) {
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;
1071 
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;
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;
1091 
1092 		default:
1093 			error = ENOPROTOOPT;
1094 			break;
1095 		}
1096 		break;
1097 
1098 	default:
1099 		error = EINVAL;
1100 		break;
1101 	}
1102 
1103 end:
1104 	splx(s);
1105 	return error;
1106 }
1107 
1108 
1109 int
1110 udp_output(struct mbuf *m, ...)
1111 {
1112 	struct inpcb *inp;
1113 	struct udpiphdr *ui;
1114 	struct route *ro;
1115 	int len = m->m_pkthdr.len;
1116 	int error = 0;
1117 	va_list ap;
1118 
1119 	MCLAIM(m, &udp_tx_mowner);
1120 	va_start(ap, m);
1121 	inp = va_arg(ap, struct inpcb *);
1122 	va_end(ap);
1123 
1124 	/*
1125 	 * Calculate data length and get a mbuf
1126 	 * for UDP and IP headers.
1127 	 */
1128 	M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT);
1129 	if (m == 0) {
1130 		error = ENOBUFS;
1131 		goto release;
1132 	}
1133 
1134 	/*
1135 	 * Compute the packet length of the IP header, and
1136 	 * punt if the length looks bogus.
1137 	 */
1138 	if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
1139 		error = EMSGSIZE;
1140 		goto release;
1141 	}
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;
1153 	ui->ui_ulen = htons((u_int16_t)len + sizeof(struct udphdr));
1154 
1155 	ro = &inp->inp_route;
1156 
1157 	/*
1158 	 * Set up checksum and output datagram.
1159 	 */
1160 	if (udpcksum) {
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));
1168 		m->m_pkthdr.csum_flags = M_CSUM_UDPv4;
1169 		m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
1170 	} else
1171 		ui->ui_sum = 0;
1172 	((struct ip *)ui)->ip_len = htons(sizeof (struct udpiphdr) + len);
1173 	((struct ip *)ui)->ip_ttl = inp->inp_ip.ip_ttl;	/* XXX */
1174 	((struct ip *)ui)->ip_tos = inp->inp_ip.ip_tos;	/* XXX */
1175 	UDP_STATINC(UDP_STAT_OPACKETS);
1176 
1177 	return (ip_output(m, inp->inp_options, ro,
1178 	    inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST),
1179 	    inp->inp_moptions, inp->inp_socket));
1180 
1181 release:
1182 	m_freem(m);
1183 	return (error);
1184 }
1185 
1186 int	udp_sendspace = 9216;		/* really max datagram size */
1187 int	udp_recvspace = 40 * (1024 + sizeof(struct sockaddr_in));
1188 					/* 40 1K datagrams */
1189 
1190 /*ARGSUSED*/
1191 int
1192 udp_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
1193 	struct mbuf *control, struct lwp *l)
1194 {
1195 	struct inpcb *inp;
1196 	int s;
1197 	int error = 0;
1198 
1199 	if (req == PRU_CONTROL)
1200 		return (in_control(so, (long)m, (void *)nam,
1201 		    (struct ifnet *)control, l));
1202 
1203 	s = splsoftnet();
1204 
1205 	if (req == PRU_PURGEIF) {
1206 		mutex_enter(softnet_lock);
1207 		in_pcbpurgeif0(&udbtable, (struct ifnet *)control);
1208 		in_purgeif((struct ifnet *)control);
1209 		in_pcbpurgeif(&udbtable, (struct ifnet *)control);
1210 		mutex_exit(softnet_lock);
1211 		splx(s);
1212 		return (0);
1213 	}
1214 
1215 	inp = sotoinpcb(so);
1216 #ifdef DIAGNOSTIC
1217 	if (req != PRU_SEND && req != PRU_SENDOOB && control)
1218 		panic("udp_usrreq: unexpected control mbuf");
1219 #endif
1220 	if (req == PRU_ATTACH) {
1221 		sosetlock(so);
1222 	} else if (inp == 0) {
1223 		error = EINVAL;
1224 		goto release;
1225 	}
1226 
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:
1234 		if (inp != 0) {
1235 			error = EISCONN;
1236 			break;
1237 		}
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
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 		}
1248 		error = in_pcballoc(so, &udbtable);
1249 		if (error)
1250 			break;
1251 		inp = sotoinpcb(so);
1252 		inp->inp_ip.ip_ttl = ip_defttl;
1253 		break;
1254 
1255 	case PRU_DETACH:
1256 		in_pcbdetach(inp);
1257 		break;
1258 
1259 	case PRU_BIND:
1260 		error = in_pcbbind(inp, nam, l);
1261 		break;
1262 
1263 	case PRU_LISTEN:
1264 		error = EOPNOTSUPP;
1265 		break;
1266 
1267 	case PRU_CONNECT:
1268 		error = in_pcbconnect(inp, nam, l);
1269 		if (error)
1270 			break;
1271 		soisconnected(so);
1272 		break;
1273 
1274 	case PRU_CONNECT2:
1275 		error = EOPNOTSUPP;
1276 		break;
1277 
1278 	case PRU_DISCONNECT:
1279 		/*soisdisconnected(so);*/
1280 		so->so_state &= ~SS_ISCONNECTED;	/* XXX */
1281 		in_pcbdisconnect(inp);
1282 		inp->inp_laddr = zeroin_addr;		/* XXX */
1283 		in_pcbstate(inp, INP_BOUND);		/* XXX */
1284 		break;
1285 
1286 	case PRU_SHUTDOWN:
1287 		socantsendmore(so);
1288 		break;
1289 
1290 	case PRU_RCVD:
1291 		error = EOPNOTSUPP;
1292 		break;
1293 
1294 	case PRU_SEND:
1295 		if (control && control->m_len) {
1296 			m_freem(control);
1297 			m_freem(m);
1298 			error = EINVAL;
1299 			break;
1300 		}
1301 	{
1302 		struct in_addr laddr;			/* XXX */
1303 
1304 		memset(&laddr, 0, sizeof laddr);
1305 		if (nam) {
1306 			laddr = inp->inp_laddr;		/* XXX */
1307 			if ((so->so_state & SS_ISCONNECTED) != 0) {
1308 				error = EISCONN;
1309 				goto die;
1310 			}
1311 			error = in_pcbconnect(inp, nam, l);
1312 			if (error)
1313 				goto die;
1314 		} else {
1315 			if ((so->so_state & SS_ISCONNECTED) == 0) {
1316 				error = ENOTCONN;
1317 				goto die;
1318 			}
1319 		}
1320 		error = udp_output(m, inp);
1321 		m = NULL;
1322 		if (nam) {
1323 			in_pcbdisconnect(inp);
1324 			inp->inp_laddr = laddr;		/* XXX */
1325 			in_pcbstate(inp, INP_BOUND);	/* XXX */
1326 		}
1327 	  die:
1328 		if (m)
1329 			m_freem(m);
1330 	}
1331 		break;
1332 
1333 	case PRU_SENSE:
1334 		/*
1335 		 * stat: don't bother with a blocksize.
1336 		 */
1337 		splx(s);
1338 		return (0);
1339 
1340 	case PRU_RCVOOB:
1341 		error =  EOPNOTSUPP;
1342 		break;
1343 
1344 	case PRU_SENDOOB:
1345 		m_freem(control);
1346 		m_freem(m);
1347 		error =  EOPNOTSUPP;
1348 		break;
1349 
1350 	case PRU_SOCKADDR:
1351 		in_setsockaddr(inp, nam);
1352 		break;
1353 
1354 	case PRU_PEERADDR:
1355 		in_setpeeraddr(inp, nam);
1356 		break;
1357 
1358 	default:
1359 		panic("udp_usrreq");
1360 	}
1361 
1362 release:
1363 	splx(s);
1364 	return (error);
1365 }
1366 
1367 static int
1368 sysctl_net_inet_udp_stats(SYSCTLFN_ARGS)
1369 {
1370 
1371 	return (NETSTAT_SYSCTL(udpstat_percpu, UDP_NSTATS));
1372 }
1373 
1374 /*
1375  * Sysctl for udp variables.
1376  */
1377 static void
1378 sysctl_net_inet_udp_setup(struct sysctllog **clog)
1379 {
1380 	const struct sysctlnode *rfc6056_node;
1381 
1382 	sysctl_createv(clog, 0, NULL, NULL,
1383 		       CTLFLAG_PERMANENT,
1384 		       CTLTYPE_NODE, "net", NULL,
1385 		       NULL, 0, NULL, 0,
1386 		       CTL_NET, CTL_EOL);
1387 	sysctl_createv(clog, 0, NULL, NULL,
1388 		       CTLFLAG_PERMANENT,
1389 		       CTLTYPE_NODE, "inet", NULL,
1390 		       NULL, 0, NULL, 0,
1391 		       CTL_NET, PF_INET, CTL_EOL);
1392 	sysctl_createv(clog, 0, NULL, NULL,
1393 		       CTLFLAG_PERMANENT,
1394 		       CTLTYPE_NODE, "udp",
1395 		       SYSCTL_DESCR("UDPv4 related settings"),
1396 		       NULL, 0, NULL, 0,
1397 		       CTL_NET, PF_INET, IPPROTO_UDP, CTL_EOL);
1398 
1399 	sysctl_createv(clog, 0, NULL, NULL,
1400 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1401 		       CTLTYPE_INT, "checksum",
1402 		       SYSCTL_DESCR("Compute UDP checksums"),
1403 		       NULL, 0, &udpcksum, 0,
1404 		       CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_CHECKSUM,
1405 		       CTL_EOL);
1406 	sysctl_createv(clog, 0, NULL, NULL,
1407 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1408 		       CTLTYPE_INT, "sendspace",
1409 		       SYSCTL_DESCR("Default UDP send buffer size"),
1410 		       NULL, 0, &udp_sendspace, 0,
1411 		       CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_SENDSPACE,
1412 		       CTL_EOL);
1413 	sysctl_createv(clog, 0, NULL, NULL,
1414 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1415 		       CTLTYPE_INT, "recvspace",
1416 		       SYSCTL_DESCR("Default UDP receive buffer size"),
1417 		       NULL, 0, &udp_recvspace, 0,
1418 		       CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_RECVSPACE,
1419 		       CTL_EOL);
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);
1427 	sysctl_createv(clog, 0, NULL, NULL,
1428 		       CTLFLAG_PERMANENT,
1429 		       CTLTYPE_STRUCT, "pcblist",
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);
1434 	sysctl_createv(clog, 0, NULL, NULL,
1435 		       CTLFLAG_PERMANENT,
1436 		       CTLTYPE_STRUCT, "stats",
1437 		       SYSCTL_DESCR("UDP statistics"),
1438 		       sysctl_net_inet_udp_stats, 0, NULL, 0,
1439 		       CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_STATS,
1440 		       CTL_EOL);
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);
1460 }
1461 #endif
1462 
1463 void
1464 udp_statinc(u_int stat)
1465 {
1466 
1467 	KASSERT(stat < UDP_NSTATS);
1468 	UDP_STATINC(stat);
1469 }
1470 
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
1476  * -1 if an error occurent and m was freed
1477  */
1478 static int
1479 udp4_espinudp(struct mbuf **mp, int off, struct sockaddr *src,
1480     struct socket *so)
1481 {
1482 	size_t len;
1483 	void *data;
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;
1490 	struct m_tag *tag;
1491 	struct udphdr *udphdr;
1492 	u_int16_t sport, dport;
1493 	struct mbuf *m = *mp;
1494 
1495 	/*
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) {
1504 		if ((*mp = m_pullup(m, minlen)) == NULL) {
1505 			printf("udp4_espinudp: m_pullup failed\n");
1506 			return -1;
1507 		}
1508 		m = *mp;
1509 	}
1510 
1511 	len = m->m_len - off;
1512 	data = mtod(m, char *) + off;
1513 	inp = sotoinpcb(so);
1514 
1515 	/* Ignore keepalive packets */
1516 	if ((len == 1) && (*(unsigned char *)data == 0xff)) {
1517 		return 1;
1518 	}
1519 
1520 	/*
1521 	 * Check that the payload is long enough to hold
1522 	 * an ESP header and compute the length of encapsulation
1523 	 * header to remove
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) {
1535 		u_int32_t *st = (u_int32_t *)data;
1536 
1537 		if ((len <= sizeof(u_int64_t) + sizeof(struct esp))
1538 		    || ((st[0] | st[1]) != 0))
1539 			return 0; /* Normal UDP processing */
1540 
1541 		skip = sizeof(struct udphdr) + sizeof(u_int64_t);
1542 	}
1543 
1544 	/*
1545 	 * Get the UDP ports. They are handled in network
1546 	 * order everywhere in IPSEC_NAT_T code.
1547 	 */
1548 	udphdr = (struct udphdr *)((char *)data - skip);
1549 	sport = udphdr->uh_sport;
1550 	dport = udphdr->uh_dport;
1551 
1552 	/*
1553 	 * Remove the UDP header (and possibly the non ESP marker)
1554 	 * IP header lendth is iphdrlen
1555 	 * Before:
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);
1568 	memmove(mtod(m, char *) + skip, mtod(m, void *), iphdrlen);
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 	/*
1576 	 * Copy the mbuf to avoid multiple free, as both
1577 	 * esp4_input (which we call) and udp_input (which
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 
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 	 */
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");
1594 		m_freem(n);
1595 		return 0;
1596 	}
1597 	((u_int16_t *)(tag + 1))[0] = sport;
1598 	((u_int16_t *)(tag + 1))[1] = dport;
1599 	m_tag_prepend(n, tag);
1600 
1601 #ifdef FAST_IPSEC
1602 	ipsec4_common_input(n, iphdrlen, IPPROTO_ESP);
1603 #else
1604 	esp4_input(n, iphdrlen);
1605 #endif
1606 
1607 	/* We handled it, it shouldn't be handled by UDP */
1608 	return 1;
1609 }
1610 #endif
1611