Home | History | Annotate | Line # | Download | only in netinet
udp_usrreq.c revision 1.242
      1 /*	$NetBSD: udp_usrreq.c,v 1.242 2018/02/14 05:24:44 maxv 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 /*
     64  * UDP protocol implementation.
     65  * Per RFC 768, August, 1980.
     66  */
     67 
     68 #include <sys/cdefs.h>
     69 __KERNEL_RCSID(0, "$NetBSD: udp_usrreq.c,v 1.242 2018/02/14 05:24:44 maxv Exp $");
     70 
     71 #ifdef _KERNEL_OPT
     72 #include "opt_inet.h"
     73 #include "opt_ipsec.h"
     74 #include "opt_inet_csum.h"
     75 #include "opt_ipkdb.h"
     76 #include "opt_mbuftrace.h"
     77 #include "opt_net_mpsafe.h"
     78 #endif
     79 
     80 #include <sys/param.h>
     81 #include <sys/mbuf.h>
     82 #include <sys/once.h>
     83 #include <sys/protosw.h>
     84 #include <sys/socket.h>
     85 #include <sys/socketvar.h>
     86 #include <sys/systm.h>
     87 #include <sys/proc.h>
     88 #include <sys/domain.h>
     89 #include <sys/sysctl.h>
     90 
     91 #include <net/if.h>
     92 
     93 #include <netinet/in.h>
     94 #include <netinet/in_systm.h>
     95 #include <netinet/in_var.h>
     96 #include <netinet/ip.h>
     97 #include <netinet/in_pcb.h>
     98 #include <netinet/ip_var.h>
     99 #include <netinet/ip_icmp.h>
    100 #include <netinet/udp.h>
    101 #include <netinet/udp_var.h>
    102 #include <netinet/udp_private.h>
    103 
    104 #ifdef INET6
    105 #include <netinet/ip6.h>
    106 #include <netinet/icmp6.h>
    107 #include <netinet6/ip6_var.h>
    108 #include <netinet6/ip6_private.h>
    109 #include <netinet6/in6_pcb.h>
    110 #include <netinet6/udp6_var.h>
    111 #include <netinet6/udp6_private.h>
    112 #endif
    113 
    114 #ifndef INET6
    115 /* always need ip6.h for IP6_EXTHDR_GET */
    116 #include <netinet/ip6.h>
    117 #endif
    118 
    119 #ifdef IPSEC
    120 #include <netipsec/ipsec.h>
    121 #include <netipsec/ipsec_var.h>
    122 #include <netipsec/ipsec_private.h>
    123 #include <netipsec/esp.h>
    124 #ifdef INET6
    125 #include <netipsec/ipsec6.h>
    126 #endif
    127 #endif
    128 
    129 #ifdef IPKDB
    130 #include <ipkdb/ipkdb.h>
    131 #endif
    132 
    133 int udpcksum = 1;
    134 int udp_do_loopback_cksum = 0;
    135 
    136 struct inpcbtable udbtable;
    137 
    138 percpu_t *udpstat_percpu;
    139 
    140 #ifdef INET
    141 #ifdef IPSEC
    142 static int udp4_espinudp(struct mbuf **, int, struct sockaddr *,
    143     struct socket *);
    144 #endif
    145 static void udp4_sendup(struct mbuf *, int, struct sockaddr *,
    146     struct socket *);
    147 static int udp4_realinput(struct sockaddr_in *, struct sockaddr_in *,
    148     struct mbuf **, int);
    149 static int udp4_input_checksum(struct mbuf *, const struct udphdr *, int, int);
    150 #endif
    151 #ifdef INET
    152 static void udp_notify (struct inpcb *, int);
    153 #endif
    154 
    155 #ifndef UDBHASHSIZE
    156 #define	UDBHASHSIZE	128
    157 #endif
    158 int udbhashsize = UDBHASHSIZE;
    159 
    160 /*
    161  * For send - really max datagram size; for receive - 40 1K datagrams.
    162  */
    163 static int udp_sendspace = 9216;
    164 static int udp_recvspace = 40 * (1024 + sizeof(struct sockaddr_in));
    165 
    166 #ifdef MBUFTRACE
    167 struct mowner udp_mowner = MOWNER_INIT("udp", "");
    168 struct mowner udp_rx_mowner = MOWNER_INIT("udp", "rx");
    169 struct mowner udp_tx_mowner = MOWNER_INIT("udp", "tx");
    170 #endif
    171 
    172 #ifdef UDP_CSUM_COUNTERS
    173 #include <sys/device.h>
    174 
    175 #if defined(INET)
    176 struct evcnt udp_hwcsum_bad = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
    177     NULL, "udp", "hwcsum bad");
    178 struct evcnt udp_hwcsum_ok = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
    179     NULL, "udp", "hwcsum ok");
    180 struct evcnt udp_hwcsum_data = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
    181     NULL, "udp", "hwcsum data");
    182 struct evcnt udp_swcsum = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
    183     NULL, "udp", "swcsum");
    184 
    185 EVCNT_ATTACH_STATIC(udp_hwcsum_bad);
    186 EVCNT_ATTACH_STATIC(udp_hwcsum_ok);
    187 EVCNT_ATTACH_STATIC(udp_hwcsum_data);
    188 EVCNT_ATTACH_STATIC(udp_swcsum);
    189 #endif /* defined(INET) */
    190 
    191 #define	UDP_CSUM_COUNTER_INCR(ev)	(ev)->ev_count++
    192 #else
    193 #define	UDP_CSUM_COUNTER_INCR(ev)	/* nothing */
    194 #endif /* UDP_CSUM_COUNTERS */
    195 
    196 static void sysctl_net_inet_udp_setup(struct sysctllog **);
    197 
    198 static int
    199 do_udpinit(void)
    200 {
    201 
    202 	in_pcbinit(&udbtable, udbhashsize, udbhashsize);
    203 	udpstat_percpu = percpu_alloc(sizeof(uint64_t) * UDP_NSTATS);
    204 
    205 	MOWNER_ATTACH(&udp_tx_mowner);
    206 	MOWNER_ATTACH(&udp_rx_mowner);
    207 	MOWNER_ATTACH(&udp_mowner);
    208 
    209 	return 0;
    210 }
    211 
    212 void
    213 udp_init_common(void)
    214 {
    215 	static ONCE_DECL(doudpinit);
    216 
    217 	RUN_ONCE(&doudpinit, do_udpinit);
    218 }
    219 
    220 void
    221 udp_init(void)
    222 {
    223 
    224 	sysctl_net_inet_udp_setup(NULL);
    225 
    226 	udp_init_common();
    227 }
    228 
    229 /*
    230  * Checksum extended UDP header and data.
    231  */
    232 int
    233 udp_input_checksum(int af, struct mbuf *m, const struct udphdr *uh,
    234     int iphlen, int len)
    235 {
    236 
    237 	switch (af) {
    238 #ifdef INET
    239 	case AF_INET:
    240 		return udp4_input_checksum(m, uh, iphlen, len);
    241 #endif
    242 #ifdef INET6
    243 	case AF_INET6:
    244 		return udp6_input_checksum(m, uh, iphlen, len);
    245 #endif
    246 	}
    247 #ifdef DIAGNOSTIC
    248 	panic("udp_input_checksum: unknown af %d", af);
    249 #endif
    250 	/* NOTREACHED */
    251 	return -1;
    252 }
    253 
    254 #ifdef INET
    255 
    256 /*
    257  * Checksum extended UDP header and data.
    258  */
    259 static int
    260 udp4_input_checksum(struct mbuf *m, const struct udphdr *uh,
    261     int iphlen, int len)
    262 {
    263 
    264 	/*
    265 	 * XXX it's better to record and check if this mbuf is
    266 	 * already checked.
    267 	 */
    268 
    269 	if (uh->uh_sum == 0)
    270 		return 0;
    271 
    272 	switch (m->m_pkthdr.csum_flags &
    273 	    ((m_get_rcvif_NOMPSAFE(m)->if_csum_flags_rx & M_CSUM_UDPv4) |
    274 	    M_CSUM_TCP_UDP_BAD | M_CSUM_DATA)) {
    275 	case M_CSUM_UDPv4|M_CSUM_TCP_UDP_BAD:
    276 		UDP_CSUM_COUNTER_INCR(&udp_hwcsum_bad);
    277 		goto badcsum;
    278 
    279 	case M_CSUM_UDPv4|M_CSUM_DATA: {
    280 		u_int32_t hw_csum = m->m_pkthdr.csum_data;
    281 
    282 		UDP_CSUM_COUNTER_INCR(&udp_hwcsum_data);
    283 		if (m->m_pkthdr.csum_flags & M_CSUM_NO_PSEUDOHDR) {
    284 			const struct ip *ip =
    285 			    mtod(m, const struct ip *);
    286 
    287 			hw_csum = in_cksum_phdr(ip->ip_src.s_addr,
    288 			    ip->ip_dst.s_addr,
    289 			    htons(hw_csum + len + IPPROTO_UDP));
    290 		}
    291 		if ((hw_csum ^ 0xffff) != 0)
    292 			goto badcsum;
    293 		break;
    294 	}
    295 
    296 	case M_CSUM_UDPv4:
    297 		/* Checksum was okay. */
    298 		UDP_CSUM_COUNTER_INCR(&udp_hwcsum_ok);
    299 		break;
    300 
    301 	default:
    302 		/*
    303 		 * Need to compute it ourselves.  Maybe skip checksum
    304 		 * on loopback interfaces.
    305 		 */
    306 		if (__predict_true(!(m_get_rcvif_NOMPSAFE(m)->if_flags &
    307 				     IFF_LOOPBACK) ||
    308 				   udp_do_loopback_cksum)) {
    309 			UDP_CSUM_COUNTER_INCR(&udp_swcsum);
    310 			if (in4_cksum(m, IPPROTO_UDP, iphlen, len) != 0)
    311 				goto badcsum;
    312 		}
    313 		break;
    314 	}
    315 
    316 	return 0;
    317 
    318 badcsum:
    319 	UDP_STATINC(UDP_STAT_BADSUM);
    320 	return -1;
    321 }
    322 
    323 void
    324 udp_input(struct mbuf *m, ...)
    325 {
    326 	va_list ap;
    327 	struct sockaddr_in src, dst;
    328 	struct ip *ip;
    329 	struct udphdr *uh;
    330 	int iphlen;
    331 	int len;
    332 	int n;
    333 	u_int16_t ip_len;
    334 
    335 	va_start(ap, m);
    336 	iphlen = va_arg(ap, int);
    337 	(void)va_arg(ap, int);		/* ignore value, advance ap */
    338 	va_end(ap);
    339 
    340 	MCLAIM(m, &udp_rx_mowner);
    341 	UDP_STATINC(UDP_STAT_IPACKETS);
    342 
    343 	/*
    344 	 * Get IP and UDP header together in first mbuf.
    345 	 */
    346 	ip = mtod(m, struct ip *);
    347 	IP6_EXTHDR_GET(uh, struct udphdr *, m, iphlen, sizeof(struct udphdr));
    348 	if (uh == NULL) {
    349 		UDP_STATINC(UDP_STAT_HDROPS);
    350 		return;
    351 	}
    352 
    353 	/*
    354 	 * Enforce alignment requirements that are violated in
    355 	 * some cases, see kern/50766 for details.
    356 	 */
    357 	if (UDP_HDR_ALIGNED_P(uh) == 0) {
    358 		m = m_copyup(m, iphlen + sizeof(struct udphdr), 0);
    359 		if (m == NULL) {
    360 			UDP_STATINC(UDP_STAT_HDROPS);
    361 			return;
    362 		}
    363 		ip = mtod(m, struct ip *);
    364 		uh = (struct udphdr *)(mtod(m, char *) + iphlen);
    365 	}
    366 	KASSERT(UDP_HDR_ALIGNED_P(uh));
    367 
    368 	/* destination port of 0 is illegal, based on RFC768. */
    369 	if (uh->uh_dport == 0)
    370 		goto bad;
    371 
    372 	/*
    373 	 * Make mbuf data length reflect UDP length.
    374 	 * If not enough data to reflect UDP length, drop.
    375 	 */
    376 	ip_len = ntohs(ip->ip_len);
    377 	len = ntohs((u_int16_t)uh->uh_ulen);
    378 	if (ip_len != iphlen + len) {
    379 		if (ip_len < iphlen + len || len < sizeof(struct udphdr)) {
    380 			UDP_STATINC(UDP_STAT_BADLEN);
    381 			goto bad;
    382 		}
    383 		m_adj(m, iphlen + len - ip_len);
    384 	}
    385 
    386 	/*
    387 	 * Checksum extended UDP header and data.
    388 	 */
    389 	if (udp4_input_checksum(m, uh, iphlen, len))
    390 		goto badcsum;
    391 
    392 	/* construct source and dst sockaddrs. */
    393 	sockaddr_in_init(&src, &ip->ip_src, uh->uh_sport);
    394 	sockaddr_in_init(&dst, &ip->ip_dst, uh->uh_dport);
    395 
    396 	if ((n = udp4_realinput(&src, &dst, &m, iphlen)) == -1) {
    397 		UDP_STATINC(UDP_STAT_HDROPS);
    398 		return;
    399 	}
    400 	if (m == NULL) {
    401 		/*
    402 		 * packet has been processed by ESP stuff -
    403 		 * e.g. dropped NAT-T-keep-alive-packet ...
    404 		 */
    405 		return;
    406 	}
    407 
    408 	ip = mtod(m, struct ip *);
    409 	IP6_EXTHDR_GET(uh, struct udphdr *, m, iphlen, sizeof(struct udphdr));
    410 	if (uh == NULL) {
    411 		UDP_STATINC(UDP_STAT_HDROPS);
    412 		return;
    413 	}
    414 	/* XXX Re-enforce alignment? */
    415 
    416 #ifdef INET6
    417 	if (IN_MULTICAST(ip->ip_dst.s_addr) || n == 0) {
    418 		struct sockaddr_in6 src6, dst6;
    419 
    420 		memset(&src6, 0, sizeof(src6));
    421 		src6.sin6_family = AF_INET6;
    422 		src6.sin6_len = sizeof(struct sockaddr_in6);
    423 		in6_in_2_v4mapin6(&ip->ip_src, &src6.sin6_addr);
    424 		src6.sin6_port = uh->uh_sport;
    425 		memset(&dst6, 0, sizeof(dst6));
    426 		dst6.sin6_family = AF_INET6;
    427 		dst6.sin6_len = sizeof(struct sockaddr_in6);
    428 		in6_in_2_v4mapin6(&ip->ip_dst, &dst6.sin6_addr);
    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 INET
    467 static void
    468 udp4_sendup(struct mbuf *m, int off /* offset of data portion */,
    469     struct sockaddr *src, struct socket *so)
    470 {
    471 	struct mbuf *opts = NULL;
    472 	struct mbuf *n;
    473 	struct inpcb *inp;
    474 
    475 	KASSERT(so != NULL);
    476 	KASSERT(so->so_proto->pr_domain->dom_family == AF_INET);
    477 	inp = sotoinpcb(so);
    478 	KASSERT(inp != NULL);
    479 
    480 #if defined(IPSEC)
    481 	/* check AH/ESP integrity. */
    482 	if (ipsec_used && ipsec4_in_reject(m, inp)) {
    483 		IPSEC_STATINC(IPSEC_STAT_IN_POLVIO);
    484 		if ((n = m_copypacket(m, M_DONTWAIT)) != NULL)
    485 			icmp_error(n, ICMP_UNREACH, ICMP_UNREACH_ADMIN_PROHIBIT,
    486 			    0, 0);
    487 		return;
    488 	}
    489 #endif
    490 
    491 	if ((n = m_copypacket(m, M_DONTWAIT)) != NULL) {
    492 		if (inp->inp_flags & INP_CONTROLOPTS ||
    493 		    SOOPT_TIMESTAMP(so->so_options)) {
    494 			struct ip *ip = mtod(n, struct ip *);
    495 			ip_savecontrol(inp, &opts, ip, n);
    496 		}
    497 
    498 		m_adj(n, off);
    499 		if (sbappendaddr(&so->so_rcv, src, n, opts) == 0) {
    500 			m_freem(n);
    501 			if (opts)
    502 				m_freem(opts);
    503 			so->so_rcv.sb_overflowed++;
    504 			UDP_STATINC(UDP_STAT_FULLSOCK);
    505 		} else
    506 			sorwakeup(so);
    507 	}
    508 }
    509 #endif
    510 
    511 #ifdef INET
    512 static int
    513 udp4_realinput(struct sockaddr_in *src, struct sockaddr_in *dst,
    514     struct mbuf **mp, int off /* offset of udphdr */)
    515 {
    516 	u_int16_t *sport, *dport;
    517 	int rcvcnt;
    518 	struct in_addr *src4, *dst4;
    519 	struct inpcb_hdr *inph;
    520 	struct inpcb *inp;
    521 	struct mbuf *m = *mp;
    522 
    523 	rcvcnt = 0;
    524 	off += sizeof(struct udphdr);	/* now, offset of payload */
    525 
    526 	if (src->sin_family != AF_INET || dst->sin_family != AF_INET)
    527 		goto bad;
    528 
    529 	src4 = &src->sin_addr;
    530 	sport = &src->sin_port;
    531 	dst4 = &dst->sin_addr;
    532 	dport = &dst->sin_port;
    533 
    534 	if (IN_MULTICAST(dst4->s_addr) ||
    535 	    in_broadcast(*dst4, m_get_rcvif_NOMPSAFE(m))) {
    536 		/*
    537 		 * Deliver a multicast or broadcast datagram to *all* sockets
    538 		 * for which the local and remote addresses and ports match
    539 		 * those of the incoming datagram.  This allows more than
    540 		 * one process to receive multi/broadcasts on the same port.
    541 		 * (This really ought to be done for unicast datagrams as
    542 		 * well, but that would cause problems with existing
    543 		 * applications that open both address-specific sockets and
    544 		 * a wildcard socket listening to the same port -- they would
    545 		 * end up receiving duplicates of every unicast datagram.
    546 		 * Those applications open the multiple sockets to overcome an
    547 		 * inadequacy of the UDP socket interface, but for backwards
    548 		 * compatibility we avoid the problem here rather than
    549 		 * fixing the interface.  Maybe 4.5BSD will remedy this?)
    550 		 */
    551 
    552 		/*
    553 		 * KAME note: traditionally we dropped udpiphdr from mbuf here.
    554 		 * we need udpiphdr for IPsec processing so we do that later.
    555 		 */
    556 		/*
    557 		 * Locate pcb(s) for datagram.
    558 		 */
    559 		TAILQ_FOREACH(inph, &udbtable.inpt_queue, inph_queue) {
    560 			inp = (struct inpcb *)inph;
    561 			if (inp->inp_af != AF_INET)
    562 				continue;
    563 
    564 			if (inp->inp_lport != *dport)
    565 				continue;
    566 			if (!in_nullhost(inp->inp_laddr)) {
    567 				if (!in_hosteq(inp->inp_laddr, *dst4))
    568 					continue;
    569 			}
    570 			if (!in_nullhost(inp->inp_faddr)) {
    571 				if (!in_hosteq(inp->inp_faddr, *src4) ||
    572 				    inp->inp_fport != *sport)
    573 					continue;
    574 			}
    575 
    576 			udp4_sendup(m, off, (struct sockaddr *)src,
    577 			    inp->inp_socket);
    578 			rcvcnt++;
    579 
    580 			/*
    581 			 * Don't look for additional matches if this one does
    582 			 * not have either the SO_REUSEPORT or SO_REUSEADDR
    583 			 * socket options set.  This heuristic avoids searching
    584 			 * through all pcbs in the common case of a non-shared
    585 			 * port.  It assumes that an application will never
    586 			 * clear these options after setting them.
    587 			 */
    588 			if ((inp->inp_socket->so_options &
    589 			    (SO_REUSEPORT|SO_REUSEADDR)) == 0)
    590 				break;
    591 		}
    592 	} else {
    593 		/*
    594 		 * Locate pcb for datagram.
    595 		 */
    596 		inp = in_pcblookup_connect(&udbtable, *src4, *sport, *dst4,
    597 		    *dport, 0);
    598 		if (inp == 0) {
    599 			UDP_STATINC(UDP_STAT_PCBHASHMISS);
    600 			inp = in_pcblookup_bind(&udbtable, *dst4, *dport);
    601 			if (inp == 0)
    602 				return rcvcnt;
    603 		}
    604 
    605 #ifdef IPSEC
    606 		/* Handle ESP over UDP */
    607 		if (inp->inp_flags & INP_ESPINUDP_ALL) {
    608 			struct sockaddr *sa = (struct sockaddr *)src;
    609 
    610 			switch (udp4_espinudp(mp, off, sa, inp->inp_socket)) {
    611 			case -1: /* Error, m was freed */
    612 				rcvcnt = -1;
    613 				goto bad;
    614 
    615 			case 1: /* ESP over UDP */
    616 				rcvcnt++;
    617 				goto bad;
    618 
    619 			case 0: /* plain UDP */
    620 			default: /* Unexpected */
    621 				/*
    622 				 * Normal UDP processing will take place,
    623 				 * m may have changed.
    624 				 */
    625 				m = *mp;
    626 				break;
    627 			}
    628 		}
    629 #endif
    630 
    631 		/*
    632 		 * Check the minimum TTL for socket.
    633 		 */
    634 		if (mtod(m, struct ip *)->ip_ttl < inp->inp_ip_minttl)
    635 			goto bad;
    636 
    637 		udp4_sendup(m, off, (struct sockaddr *)src, inp->inp_socket);
    638 		rcvcnt++;
    639 	}
    640 
    641 bad:
    642 	return rcvcnt;
    643 }
    644 #endif
    645 
    646 #ifdef INET
    647 /*
    648  * Notify a udp user of an asynchronous error;
    649  * just wake up so that he can collect error status.
    650  */
    651 static void
    652 udp_notify(struct inpcb *inp, int errno)
    653 {
    654 	inp->inp_socket->so_error = errno;
    655 	sorwakeup(inp->inp_socket);
    656 	sowwakeup(inp->inp_socket);
    657 }
    658 
    659 void *
    660 udp_ctlinput(int cmd, const struct sockaddr *sa, void *v)
    661 {
    662 	struct ip *ip = v;
    663 	struct udphdr *uh;
    664 	void (*notify)(struct inpcb *, int) = udp_notify;
    665 	int errno;
    666 
    667 	if (sa->sa_family != AF_INET ||
    668 	    sa->sa_len != sizeof(struct sockaddr_in))
    669 		return NULL;
    670 	if ((unsigned)cmd >= PRC_NCMDS)
    671 		return NULL;
    672 
    673 	errno = inetctlerrmap[cmd];
    674 	if (PRC_IS_REDIRECT(cmd)) {
    675 		notify = in_rtchange;
    676 		ip = NULL;
    677 	} else if (cmd == PRC_HOSTDEAD) {
    678 		ip = NULL;
    679 	} else if (errno == 0) {
    680 		return NULL;
    681 	}
    682 
    683 	if (ip) {
    684 		uh = (struct udphdr *)((char *)ip + (ip->ip_hl << 2));
    685 		in_pcbnotify(&udbtable, satocsin(sa)->sin_addr, uh->uh_dport,
    686 		    ip->ip_src, uh->uh_sport, errno, notify);
    687 		/* XXX mapped address case */
    688 	} else {
    689 		in_pcbnotifyall(&udbtable, satocsin(sa)->sin_addr, errno,
    690 		    notify);
    691 	}
    692 
    693 	return NULL;
    694 }
    695 
    696 int
    697 udp_ctloutput(int op, struct socket *so, struct sockopt *sopt)
    698 {
    699 	int s;
    700 	int error = 0;
    701 	struct inpcb *inp;
    702 	int family;
    703 	int optval;
    704 
    705 	family = so->so_proto->pr_domain->dom_family;
    706 
    707 	s = splsoftnet();
    708 	switch (family) {
    709 #ifdef INET
    710 	case PF_INET:
    711 		if (sopt->sopt_level != IPPROTO_UDP) {
    712 			error = ip_ctloutput(op, so, sopt);
    713 			goto end;
    714 		}
    715 		break;
    716 #endif
    717 #ifdef INET6
    718 	case PF_INET6:
    719 		if (sopt->sopt_level != IPPROTO_UDP) {
    720 			error = ip6_ctloutput(op, so, sopt);
    721 			goto end;
    722 		}
    723 		break;
    724 #endif
    725 	default:
    726 		error = EAFNOSUPPORT;
    727 		goto end;
    728 	}
    729 
    730 
    731 	switch (op) {
    732 	case PRCO_SETOPT:
    733 		inp = sotoinpcb(so);
    734 
    735 		switch (sopt->sopt_name) {
    736 		case UDP_ENCAP:
    737 			error = sockopt_getint(sopt, &optval);
    738 			if (error)
    739 				break;
    740 
    741 			switch(optval) {
    742 			case 0:
    743 				inp->inp_flags &= ~INP_ESPINUDP_ALL;
    744 				break;
    745 
    746 			case UDP_ENCAP_ESPINUDP:
    747 				inp->inp_flags &= ~INP_ESPINUDP_ALL;
    748 				inp->inp_flags |= INP_ESPINUDP;
    749 				break;
    750 
    751 			case UDP_ENCAP_ESPINUDP_NON_IKE:
    752 				inp->inp_flags &= ~INP_ESPINUDP_ALL;
    753 				inp->inp_flags |= INP_ESPINUDP_NON_IKE;
    754 				break;
    755 			default:
    756 				error = EINVAL;
    757 				break;
    758 			}
    759 			break;
    760 
    761 		default:
    762 			error = ENOPROTOOPT;
    763 			break;
    764 		}
    765 		break;
    766 
    767 	default:
    768 		error = EINVAL;
    769 		break;
    770 	}
    771 
    772 end:
    773 	splx(s);
    774 	return error;
    775 }
    776 
    777 int
    778 udp_output(struct mbuf *m, struct inpcb *inp, struct mbuf *control,
    779     struct lwp *l)
    780 {
    781 	struct udpiphdr *ui;
    782 	struct route *ro;
    783 	struct ip_pktopts pktopts;
    784 	kauth_cred_t cred;
    785 	int len = m->m_pkthdr.len;
    786 	int error, flags = 0;
    787 
    788 	MCLAIM(m, &udp_tx_mowner);
    789 
    790 	/*
    791 	 * Calculate data length and get a mbuf
    792 	 * for UDP and IP headers.
    793 	 */
    794 	M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT);
    795 	if (m == NULL) {
    796 		error = ENOBUFS;
    797 		goto release;
    798 	}
    799 
    800 	/*
    801 	 * Compute the packet length of the IP header, and
    802 	 * punt if the length looks bogus.
    803 	 */
    804 	if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
    805 		error = EMSGSIZE;
    806 		goto release;
    807 	}
    808 
    809 	if (l == NULL)
    810 		cred = NULL;
    811 	else
    812 		cred = l->l_cred;
    813 
    814 	/* Setup IP outgoing packet options */
    815 	memset(&pktopts, 0, sizeof(pktopts));
    816 	error = ip_setpktopts(control, &pktopts, &flags, inp, cred);
    817 	if (error != 0)
    818 		goto release;
    819 
    820 	if (control != NULL) {
    821 		m_freem(control);
    822 		control = NULL;
    823 	}
    824 
    825 	/*
    826 	 * Fill in mbuf with extended UDP header
    827 	 * and addresses and length put into network format.
    828 	 */
    829 	ui = mtod(m, struct udpiphdr *);
    830 	ui->ui_pr = IPPROTO_UDP;
    831 	ui->ui_src = pktopts.ippo_laddr.sin_addr;
    832 	ui->ui_dst = inp->inp_faddr;
    833 	ui->ui_sport = inp->inp_lport;
    834 	ui->ui_dport = inp->inp_fport;
    835 	ui->ui_ulen = htons((u_int16_t)len + sizeof(struct udphdr));
    836 
    837 	ro = &inp->inp_route;
    838 
    839 	/*
    840 	 * Set up checksum and output datagram.
    841 	 */
    842 	if (udpcksum) {
    843 		/*
    844 		 * XXX Cache pseudo-header checksum part for
    845 		 * XXX "connected" UDP sockets.
    846 		 */
    847 		ui->ui_sum = in_cksum_phdr(ui->ui_src.s_addr,
    848 		    ui->ui_dst.s_addr, htons((u_int16_t)len +
    849 		    sizeof(struct udphdr) + IPPROTO_UDP));
    850 		m->m_pkthdr.csum_flags = M_CSUM_UDPv4;
    851 		m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
    852 	} else
    853 		ui->ui_sum = 0;
    854 
    855 	((struct ip *)ui)->ip_len = htons(sizeof(struct udpiphdr) + len);
    856 	((struct ip *)ui)->ip_ttl = inp->inp_ip.ip_ttl;	/* XXX */
    857 	((struct ip *)ui)->ip_tos = inp->inp_ip.ip_tos;	/* XXX */
    858 	UDP_STATINC(UDP_STAT_OPACKETS);
    859 
    860 	flags |= inp->inp_socket->so_options & (SO_DONTROUTE|SO_BROADCAST);
    861 	return ip_output(m, inp->inp_options, ro, flags, pktopts.ippo_imo, inp);
    862 
    863  release:
    864 	if (control != NULL)
    865 		m_freem(control);
    866 	m_freem(m);
    867 	return error;
    868 }
    869 
    870 static int
    871 udp_attach(struct socket *so, int proto)
    872 {
    873 	struct inpcb *inp;
    874 	int error;
    875 
    876 	KASSERT(sotoinpcb(so) == NULL);
    877 
    878 	/* Assign the lock (must happen even if we will error out). */
    879 	sosetlock(so);
    880 
    881 #ifdef MBUFTRACE
    882 	so->so_mowner = &udp_mowner;
    883 	so->so_rcv.sb_mowner = &udp_rx_mowner;
    884 	so->so_snd.sb_mowner = &udp_tx_mowner;
    885 #endif
    886 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
    887 		error = soreserve(so, udp_sendspace, udp_recvspace);
    888 		if (error) {
    889 			return error;
    890 		}
    891 	}
    892 
    893 	error = in_pcballoc(so, &udbtable);
    894 	if (error) {
    895 		return error;
    896 	}
    897 	inp = sotoinpcb(so);
    898 	inp->inp_ip.ip_ttl = ip_defttl;
    899 	KASSERT(solocked(so));
    900 
    901 	return error;
    902 }
    903 
    904 static void
    905 udp_detach(struct socket *so)
    906 {
    907 	struct inpcb *inp;
    908 
    909 	KASSERT(solocked(so));
    910 	inp = sotoinpcb(so);
    911 	KASSERT(inp != NULL);
    912 	in_pcbdetach(inp);
    913 }
    914 
    915 static int
    916 udp_accept(struct socket *so, struct sockaddr *nam)
    917 {
    918 	KASSERT(solocked(so));
    919 
    920 	panic("udp_accept");
    921 
    922 	return EOPNOTSUPP;
    923 }
    924 
    925 static int
    926 udp_bind(struct socket *so, struct sockaddr *nam, struct lwp *l)
    927 {
    928 	struct inpcb *inp = sotoinpcb(so);
    929 	struct sockaddr_in *sin = (struct sockaddr_in *)nam;
    930 	int error = 0;
    931 	int s;
    932 
    933 	KASSERT(solocked(so));
    934 	KASSERT(inp != NULL);
    935 	KASSERT(nam != NULL);
    936 
    937 	s = splsoftnet();
    938 	error = in_pcbbind(inp, sin, l);
    939 	splx(s);
    940 
    941 	return error;
    942 }
    943 
    944 static int
    945 udp_listen(struct socket *so, struct lwp *l)
    946 {
    947 	KASSERT(solocked(so));
    948 
    949 	return EOPNOTSUPP;
    950 }
    951 
    952 static int
    953 udp_connect(struct socket *so, struct sockaddr *nam, struct lwp *l)
    954 {
    955 	struct inpcb *inp = sotoinpcb(so);
    956 	int error = 0;
    957 	int s;
    958 
    959 	KASSERT(solocked(so));
    960 	KASSERT(inp != NULL);
    961 	KASSERT(nam != NULL);
    962 
    963 	s = splsoftnet();
    964 	error = in_pcbconnect(inp, (struct sockaddr_in *)nam, l);
    965 	if (! error)
    966 		soisconnected(so);
    967 	splx(s);
    968 	return error;
    969 }
    970 
    971 static int
    972 udp_connect2(struct socket *so, struct socket *so2)
    973 {
    974 	KASSERT(solocked(so));
    975 
    976 	return EOPNOTSUPP;
    977 }
    978 
    979 static int
    980 udp_disconnect(struct socket *so)
    981 {
    982 	struct inpcb *inp = sotoinpcb(so);
    983 	int s;
    984 
    985 	KASSERT(solocked(so));
    986 	KASSERT(inp != NULL);
    987 
    988 	s = splsoftnet();
    989 	/*soisdisconnected(so);*/
    990 	so->so_state &= ~SS_ISCONNECTED;	/* XXX */
    991 	in_pcbdisconnect(inp);
    992 	inp->inp_laddr = zeroin_addr;		/* XXX */
    993 	in_pcbstate(inp, INP_BOUND);		/* XXX */
    994 	splx(s);
    995 
    996 	return 0;
    997 }
    998 
    999 static int
   1000 udp_shutdown(struct socket *so)
   1001 {
   1002 	int s;
   1003 
   1004 	KASSERT(solocked(so));
   1005 
   1006 	s = splsoftnet();
   1007 	socantsendmore(so);
   1008 	splx(s);
   1009 
   1010 	return 0;
   1011 }
   1012 
   1013 static int
   1014 udp_abort(struct socket *so)
   1015 {
   1016 	KASSERT(solocked(so));
   1017 
   1018 	panic("udp_abort");
   1019 
   1020 	return EOPNOTSUPP;
   1021 }
   1022 
   1023 static int
   1024 udp_ioctl(struct socket *so, u_long cmd, void *nam, struct ifnet *ifp)
   1025 {
   1026 	return in_control(so, cmd, nam, ifp);
   1027 }
   1028 
   1029 static int
   1030 udp_stat(struct socket *so, struct stat *ub)
   1031 {
   1032 	KASSERT(solocked(so));
   1033 
   1034 	/* stat: don't bother with a blocksize. */
   1035 	return 0;
   1036 }
   1037 
   1038 static int
   1039 udp_peeraddr(struct socket *so, struct sockaddr *nam)
   1040 {
   1041 	int s;
   1042 
   1043 	KASSERT(solocked(so));
   1044 	KASSERT(sotoinpcb(so) != NULL);
   1045 	KASSERT(nam != NULL);
   1046 
   1047 	s = splsoftnet();
   1048 	in_setpeeraddr(sotoinpcb(so), (struct sockaddr_in *)nam);
   1049 	splx(s);
   1050 
   1051 	return 0;
   1052 }
   1053 
   1054 static int
   1055 udp_sockaddr(struct socket *so, struct sockaddr *nam)
   1056 {
   1057 	int s;
   1058 
   1059 	KASSERT(solocked(so));
   1060 	KASSERT(sotoinpcb(so) != NULL);
   1061 	KASSERT(nam != NULL);
   1062 
   1063 	s = splsoftnet();
   1064 	in_setsockaddr(sotoinpcb(so), (struct sockaddr_in *)nam);
   1065 	splx(s);
   1066 
   1067 	return 0;
   1068 }
   1069 
   1070 static int
   1071 udp_rcvd(struct socket *so, int flags, struct lwp *l)
   1072 {
   1073 	KASSERT(solocked(so));
   1074 
   1075 	return EOPNOTSUPP;
   1076 }
   1077 
   1078 static int
   1079 udp_recvoob(struct socket *so, struct mbuf *m, int flags)
   1080 {
   1081 	KASSERT(solocked(so));
   1082 
   1083 	return EOPNOTSUPP;
   1084 }
   1085 
   1086 static int
   1087 udp_send(struct socket *so, struct mbuf *m, struct sockaddr *nam,
   1088     struct mbuf *control, struct lwp *l)
   1089 {
   1090 	struct inpcb *inp = sotoinpcb(so);
   1091 	int error = 0;
   1092 	struct in_addr laddr;			/* XXX */
   1093 	int s;
   1094 
   1095 	KASSERT(solocked(so));
   1096 	KASSERT(inp != NULL);
   1097 	KASSERT(m != NULL);
   1098 
   1099 	memset(&laddr, 0, sizeof laddr);
   1100 
   1101 	s = splsoftnet();
   1102 	if (nam) {
   1103 		laddr = inp->inp_laddr;		/* XXX */
   1104 		if ((so->so_state & SS_ISCONNECTED) != 0) {
   1105 			error = EISCONN;
   1106 			goto die;
   1107 		}
   1108 		error = in_pcbconnect(inp, (struct sockaddr_in *)nam, l);
   1109 		if (error)
   1110 			goto die;
   1111 	} else {
   1112 		if ((so->so_state & SS_ISCONNECTED) == 0) {
   1113 			error = ENOTCONN;
   1114 			goto die;
   1115 		}
   1116 	}
   1117 	error = udp_output(m, inp, control, l);
   1118 	m = NULL;
   1119 	control = NULL;
   1120 	if (nam) {
   1121 		in_pcbdisconnect(inp);
   1122 		inp->inp_laddr = laddr;		/* XXX */
   1123 		in_pcbstate(inp, INP_BOUND);	/* XXX */
   1124 	}
   1125   die:
   1126 	if (m != NULL)
   1127 		m_freem(m);
   1128 	if (control != NULL)
   1129 		m_freem(control);
   1130 
   1131 	splx(s);
   1132 	return error;
   1133 }
   1134 
   1135 static int
   1136 udp_sendoob(struct socket *so, struct mbuf *m, struct mbuf *control)
   1137 {
   1138 	KASSERT(solocked(so));
   1139 
   1140 	m_freem(m);
   1141 	m_freem(control);
   1142 
   1143 	return EOPNOTSUPP;
   1144 }
   1145 
   1146 static int
   1147 udp_purgeif(struct socket *so, struct ifnet *ifp)
   1148 {
   1149 	int s;
   1150 
   1151 	s = splsoftnet();
   1152 	mutex_enter(softnet_lock);
   1153 	in_pcbpurgeif0(&udbtable, ifp);
   1154 #ifdef NET_MPSAFE
   1155 	mutex_exit(softnet_lock);
   1156 #endif
   1157 	in_purgeif(ifp);
   1158 #ifdef NET_MPSAFE
   1159 	mutex_enter(softnet_lock);
   1160 #endif
   1161 	in_pcbpurgeif(&udbtable, ifp);
   1162 	mutex_exit(softnet_lock);
   1163 	splx(s);
   1164 
   1165 	return 0;
   1166 }
   1167 
   1168 static int
   1169 sysctl_net_inet_udp_stats(SYSCTLFN_ARGS)
   1170 {
   1171 
   1172 	return (NETSTAT_SYSCTL(udpstat_percpu, UDP_NSTATS));
   1173 }
   1174 
   1175 /*
   1176  * Sysctl for udp variables.
   1177  */
   1178 static void
   1179 sysctl_net_inet_udp_setup(struct sysctllog **clog)
   1180 {
   1181 
   1182 	sysctl_createv(clog, 0, NULL, NULL,
   1183 		       CTLFLAG_PERMANENT,
   1184 		       CTLTYPE_NODE, "inet", NULL,
   1185 		       NULL, 0, NULL, 0,
   1186 		       CTL_NET, PF_INET, CTL_EOL);
   1187 	sysctl_createv(clog, 0, NULL, NULL,
   1188 		       CTLFLAG_PERMANENT,
   1189 		       CTLTYPE_NODE, "udp",
   1190 		       SYSCTL_DESCR("UDPv4 related settings"),
   1191 		       NULL, 0, NULL, 0,
   1192 		       CTL_NET, PF_INET, IPPROTO_UDP, CTL_EOL);
   1193 
   1194 	sysctl_createv(clog, 0, NULL, NULL,
   1195 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1196 		       CTLTYPE_INT, "checksum",
   1197 		       SYSCTL_DESCR("Compute UDP checksums"),
   1198 		       NULL, 0, &udpcksum, 0,
   1199 		       CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_CHECKSUM,
   1200 		       CTL_EOL);
   1201 	sysctl_createv(clog, 0, NULL, NULL,
   1202 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1203 		       CTLTYPE_INT, "sendspace",
   1204 		       SYSCTL_DESCR("Default UDP send buffer size"),
   1205 		       NULL, 0, &udp_sendspace, 0,
   1206 		       CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_SENDSPACE,
   1207 		       CTL_EOL);
   1208 	sysctl_createv(clog, 0, NULL, NULL,
   1209 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1210 		       CTLTYPE_INT, "recvspace",
   1211 		       SYSCTL_DESCR("Default UDP receive buffer size"),
   1212 		       NULL, 0, &udp_recvspace, 0,
   1213 		       CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_RECVSPACE,
   1214 		       CTL_EOL);
   1215 	sysctl_createv(clog, 0, NULL, NULL,
   1216 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1217 		       CTLTYPE_INT, "do_loopback_cksum",
   1218 		       SYSCTL_DESCR("Perform UDP checksum on loopback"),
   1219 		       NULL, 0, &udp_do_loopback_cksum, 0,
   1220 		       CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_LOOPBACKCKSUM,
   1221 		       CTL_EOL);
   1222 	sysctl_createv(clog, 0, NULL, NULL,
   1223 		       CTLFLAG_PERMANENT,
   1224 		       CTLTYPE_STRUCT, "pcblist",
   1225 		       SYSCTL_DESCR("UDP protocol control block list"),
   1226 		       sysctl_inpcblist, 0, &udbtable, 0,
   1227 		       CTL_NET, PF_INET, IPPROTO_UDP, CTL_CREATE,
   1228 		       CTL_EOL);
   1229 	sysctl_createv(clog, 0, NULL, NULL,
   1230 		       CTLFLAG_PERMANENT,
   1231 		       CTLTYPE_STRUCT, "stats",
   1232 		       SYSCTL_DESCR("UDP statistics"),
   1233 		       sysctl_net_inet_udp_stats, 0, NULL, 0,
   1234 		       CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_STATS,
   1235 		       CTL_EOL);
   1236 }
   1237 #endif
   1238 
   1239 void
   1240 udp_statinc(u_int stat)
   1241 {
   1242 
   1243 	KASSERT(stat < UDP_NSTATS);
   1244 	UDP_STATINC(stat);
   1245 }
   1246 
   1247 #if defined(INET) && defined(IPSEC)
   1248 /*
   1249  * Returns:
   1250  *     1 if the packet was processed
   1251  *     0 if normal UDP processing should take place
   1252  *    -1 if an error occurred and m was freed
   1253  */
   1254 static int
   1255 udp4_espinudp(struct mbuf **mp, int off, struct sockaddr *src,
   1256     struct socket *so)
   1257 {
   1258 	size_t len;
   1259 	void *data;
   1260 	struct inpcb *inp;
   1261 	size_t skip = 0;
   1262 	size_t minlen;
   1263 	size_t iphdrlen;
   1264 	struct ip *ip;
   1265 	struct m_tag *tag;
   1266 	struct udphdr *udphdr;
   1267 	u_int16_t sport, dport;
   1268 	struct mbuf *m = *mp;
   1269 
   1270 	/*
   1271 	 * Collapse the mbuf chain if the first mbuf is too short
   1272 	 * The longest case is: UDP + non ESP marker + ESP.
   1273 	 */
   1274 	minlen = off + sizeof(u_int64_t) + sizeof(struct esp);
   1275 	if (minlen > m->m_pkthdr.len)
   1276 		minlen = m->m_pkthdr.len;
   1277 
   1278 	if (m->m_len < minlen) {
   1279 		if ((*mp = m_pullup(m, minlen)) == NULL) {
   1280 			return -1;
   1281 		}
   1282 		m = *mp;
   1283 	}
   1284 
   1285 	len = m->m_len - off;
   1286 	data = mtod(m, char *) + off;
   1287 	inp = sotoinpcb(so);
   1288 
   1289 	/* Ignore keepalive packets */
   1290 	if ((len == 1) && (*(unsigned char *)data == 0xff)) {
   1291 		m_freem(m);
   1292 		*mp = NULL; /* avoid any further processing by caller ... */
   1293 		return 1;
   1294 	}
   1295 
   1296 	/*
   1297 	 * Check that the payload is long enough to hold
   1298 	 * an ESP header and compute the length of encapsulation
   1299 	 * header to remove
   1300 	 */
   1301 	if (inp->inp_flags & INP_ESPINUDP) {
   1302 		u_int32_t *st = (u_int32_t *)data;
   1303 
   1304 		if ((len <= sizeof(struct esp)) || (*st == 0))
   1305 			return 0; /* Normal UDP processing */
   1306 
   1307 		skip = sizeof(struct udphdr);
   1308 	}
   1309 
   1310 	if (inp->inp_flags & INP_ESPINUDP_NON_IKE) {
   1311 		u_int32_t *st = (u_int32_t *)data;
   1312 
   1313 		if ((len <= sizeof(u_int64_t) + sizeof(struct esp)) ||
   1314 		    ((st[0] | st[1]) != 0))
   1315 			return 0; /* Normal UDP processing */
   1316 
   1317 		skip = sizeof(struct udphdr) + sizeof(u_int64_t);
   1318 	}
   1319 
   1320 	/*
   1321 	 * Get the UDP ports. They are handled in network
   1322 	 * order everywhere in IPSEC_NAT_T code.
   1323 	 */
   1324 	udphdr = (struct udphdr *)((char *)data - skip);
   1325 	sport = udphdr->uh_sport;
   1326 	dport = udphdr->uh_dport;
   1327 
   1328 	/*
   1329 	 * Remove the UDP header (and possibly the non ESP marker)
   1330 	 * IP header length is iphdrlen
   1331 	 * Before:
   1332 	 *   <--- off --->
   1333 	 *   +----+------+-----+
   1334 	 *   | IP |  UDP | ESP |
   1335 	 *   +----+------+-----+
   1336 	 *        <-skip->
   1337 	 * After:
   1338 	 *          +----+-----+
   1339 	 *          | IP | ESP |
   1340 	 *          +----+-----+
   1341 	 *   <-skip->
   1342 	 */
   1343 	iphdrlen = off - sizeof(struct udphdr);
   1344 	memmove(mtod(m, char *) + skip, mtod(m, void *), iphdrlen);
   1345 	m_adj(m, skip);
   1346 
   1347 	ip = mtod(m, struct ip *);
   1348 	ip->ip_len = htons(ntohs(ip->ip_len) - skip);
   1349 	ip->ip_p = IPPROTO_ESP;
   1350 
   1351 	/*
   1352 	 * We have modified the packet - it is now ESP, so we should not
   1353 	 * return to UDP processing ...
   1354 	 *
   1355 	 * Add a PACKET_TAG_IPSEC_NAT_T_PORT tag to remember
   1356 	 * the source UDP port. This is required if we want
   1357 	 * to select the right SPD for multiple hosts behind
   1358 	 * same NAT
   1359 	 */
   1360 	if ((tag = m_tag_get(PACKET_TAG_IPSEC_NAT_T_PORTS,
   1361 	    sizeof(sport) + sizeof(dport), M_DONTWAIT)) == NULL) {
   1362 		m_freem(m);
   1363 		return -1;
   1364 	}
   1365 	((u_int16_t *)(tag + 1))[0] = sport;
   1366 	((u_int16_t *)(tag + 1))[1] = dport;
   1367 	m_tag_prepend(m, tag);
   1368 
   1369 	if (ipsec_used)
   1370 		ipsec4_common_input(m, iphdrlen, IPPROTO_ESP);
   1371 	else
   1372 		m_freem(m);
   1373 
   1374 	/* We handled it, it shouldn't be handled by UDP */
   1375 	*mp = NULL; /* avoid free by caller ... */
   1376 	return 1;
   1377 }
   1378 #endif
   1379 
   1380 PR_WRAP_USRREQS(udp)
   1381 #define	udp_attach	udp_attach_wrapper
   1382 #define	udp_detach	udp_detach_wrapper
   1383 #define	udp_accept	udp_accept_wrapper
   1384 #define	udp_bind	udp_bind_wrapper
   1385 #define	udp_listen	udp_listen_wrapper
   1386 #define	udp_connect	udp_connect_wrapper
   1387 #define	udp_connect2	udp_connect2_wrapper
   1388 #define	udp_disconnect	udp_disconnect_wrapper
   1389 #define	udp_shutdown	udp_shutdown_wrapper
   1390 #define	udp_abort	udp_abort_wrapper
   1391 #define	udp_ioctl	udp_ioctl_wrapper
   1392 #define	udp_stat	udp_stat_wrapper
   1393 #define	udp_peeraddr	udp_peeraddr_wrapper
   1394 #define	udp_sockaddr	udp_sockaddr_wrapper
   1395 #define	udp_rcvd	udp_rcvd_wrapper
   1396 #define	udp_recvoob	udp_recvoob_wrapper
   1397 #define	udp_send	udp_send_wrapper
   1398 #define	udp_sendoob	udp_sendoob_wrapper
   1399 #define	udp_purgeif	udp_purgeif_wrapper
   1400 
   1401 const struct pr_usrreqs udp_usrreqs = {
   1402 	.pr_attach	= udp_attach,
   1403 	.pr_detach	= udp_detach,
   1404 	.pr_accept	= udp_accept,
   1405 	.pr_bind	= udp_bind,
   1406 	.pr_listen	= udp_listen,
   1407 	.pr_connect	= udp_connect,
   1408 	.pr_connect2	= udp_connect2,
   1409 	.pr_disconnect	= udp_disconnect,
   1410 	.pr_shutdown	= udp_shutdown,
   1411 	.pr_abort	= udp_abort,
   1412 	.pr_ioctl	= udp_ioctl,
   1413 	.pr_stat	= udp_stat,
   1414 	.pr_peeraddr	= udp_peeraddr,
   1415 	.pr_sockaddr	= udp_sockaddr,
   1416 	.pr_rcvd	= udp_rcvd,
   1417 	.pr_recvoob	= udp_recvoob,
   1418 	.pr_send	= udp_send,
   1419 	.pr_sendoob	= udp_sendoob,
   1420 	.pr_purgeif	= udp_purgeif,
   1421 };
   1422