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