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