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