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