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