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