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