Home | History | Annotate | Line # | Download | only in netinet
udp_usrreq.c revision 1.98
      1 /*	$NetBSD: udp_usrreq.c,v 1.98 2003/02/26 06:31:17 matt 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. All advertising materials mentioning features or use of this software
     45  *    must display the following acknowledgement:
     46  *	This product includes software developed by the University of
     47  *	California, Berkeley and its contributors.
     48  * 4. Neither the name of the University nor the names of its contributors
     49  *    may be used to endorse or promote products derived from this software
     50  *    without specific prior written permission.
     51  *
     52  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     53  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     54  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     55  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     56  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     57  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     58  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     59  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     60  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     61  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     62  * SUCH DAMAGE.
     63  *
     64  *	@(#)udp_usrreq.c	8.6 (Berkeley) 5/23/95
     65  */
     66 
     67 #include <sys/cdefs.h>
     68 __KERNEL_RCSID(0, "$NetBSD: udp_usrreq.c,v 1.98 2003/02/26 06:31:17 matt Exp $");
     69 
     70 #include "opt_inet.h"
     71 #include "opt_ipsec.h"
     72 #include "opt_inet_csum.h"
     73 #include "opt_ipkdb.h"
     74 
     75 #include <sys/param.h>
     76 #include <sys/malloc.h>
     77 #include <sys/mbuf.h>
     78 #include <sys/protosw.h>
     79 #include <sys/socket.h>
     80 #include <sys/socketvar.h>
     81 #include <sys/errno.h>
     82 #include <sys/stat.h>
     83 #include <sys/systm.h>
     84 #include <sys/proc.h>
     85 #include <sys/domain.h>
     86 #include <sys/sysctl.h>
     87 
     88 #include <net/if.h>
     89 #include <net/route.h>
     90 
     91 #include <netinet/in.h>
     92 #include <netinet/in_systm.h>
     93 #include <netinet/in_var.h>
     94 #include <netinet/ip.h>
     95 #include <netinet/in_pcb.h>
     96 #include <netinet/ip_var.h>
     97 #include <netinet/ip_icmp.h>
     98 #include <netinet/udp.h>
     99 #include <netinet/udp_var.h>
    100 
    101 #ifdef INET6
    102 #include <netinet/ip6.h>
    103 #include <netinet/icmp6.h>
    104 #include <netinet6/ip6_var.h>
    105 #include <netinet6/in6_pcb.h>
    106 #include <netinet6/udp6_var.h>
    107 #endif
    108 
    109 #ifdef PULLDOWN_TEST
    110 #ifndef INET6
    111 /* always need ip6.h for IP6_EXTHDR_GET */
    112 #include <netinet/ip6.h>
    113 #endif
    114 #endif
    115 
    116 #include "faith.h"
    117 #if defined(NFAITH) && NFAITH > 0
    118 #include <net/if_faith.h>
    119 #endif
    120 
    121 #include <machine/stdarg.h>
    122 
    123 #ifdef IPSEC
    124 #include <netinet6/ipsec.h>
    125 #include <netkey/key.h>
    126 #endif /*IPSEC*/
    127 
    128 #ifdef IPKDB
    129 #include <ipkdb/ipkdb.h>
    130 #endif
    131 
    132 /*
    133  * UDP protocol implementation.
    134  * Per RFC 768, August, 1980.
    135  */
    136 #ifndef	COMPAT_42
    137 int	udpcksum = 1;
    138 #else
    139 int	udpcksum = 0;		/* XXX */
    140 #endif
    141 
    142 struct	inpcbtable udbtable;
    143 struct	udpstat udpstat;
    144 
    145 #ifdef INET
    146 static void udp4_sendup __P((struct mbuf *, int, struct sockaddr *,
    147 	struct socket *));
    148 static int udp4_realinput __P((struct sockaddr_in *, struct sockaddr_in *,
    149 	struct mbuf *, int));
    150 #endif
    151 #ifdef INET6
    152 static void udp6_sendup __P((struct mbuf *, int, struct sockaddr *,
    153 	struct socket *));
    154 static int udp6_realinput __P((int, struct sockaddr_in6 *,
    155 	struct sockaddr_in6 *, struct mbuf *, int));
    156 #endif
    157 #ifdef INET
    158 static	void udp_notify __P((struct inpcb *, int));
    159 #endif
    160 
    161 #ifndef UDBHASHSIZE
    162 #define	UDBHASHSIZE	128
    163 #endif
    164 int	udbhashsize = UDBHASHSIZE;
    165 
    166 #ifdef MBUFTRACE
    167 struct mowner udp_mowner = { "udp" };
    168 struct mowner udp_rx_mowner = { "udp", "rx" };
    169 struct mowner udp_tx_mowner = { "udp", "tx" };
    170 #endif
    171 
    172 #ifdef UDP_CSUM_COUNTERS
    173 #include <sys/device.h>
    174 
    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 #define	UDP_CSUM_COUNTER_INCR(ev)	(ev)->ev_count++
    185 
    186 #else
    187 
    188 #define	UDP_CSUM_COUNTER_INCR(ev)	/* nothing */
    189 
    190 #endif /* UDP_CSUM_COUNTERS */
    191 
    192 void
    193 udp_init()
    194 {
    195 
    196 #ifdef INET
    197 	in_pcbinit(&udbtable, udbhashsize, udbhashsize);
    198 #endif
    199 
    200 #ifdef UDP_CSUM_COUNTERS
    201 	evcnt_attach_static(&udp_hwcsum_bad);
    202 	evcnt_attach_static(&udp_hwcsum_ok);
    203 	evcnt_attach_static(&udp_hwcsum_data);
    204 	evcnt_attach_static(&udp_swcsum);
    205 #endif /* UDP_CSUM_COUNTERS */
    206 
    207 	MOWNER_ATTACH(&udp_tx_mowner);
    208 	MOWNER_ATTACH(&udp_rx_mowner);
    209 	MOWNER_ATTACH(&udp_mowner);
    210 }
    211 
    212 #ifdef INET
    213 void
    214 #if __STDC__
    215 udp_input(struct mbuf *m, ...)
    216 #else
    217 udp_input(m, va_alist)
    218 	struct mbuf *m;
    219 	va_dcl
    220 #endif
    221 {
    222 	va_list ap;
    223 	struct sockaddr_in src, dst;
    224 	struct ip *ip;
    225 	struct udphdr *uh;
    226 	int iphlen;
    227 	int len;
    228 	int n;
    229 	u_int16_t ip_len;
    230 
    231 	va_start(ap, m);
    232 	iphlen = va_arg(ap, int);
    233 	(void)va_arg(ap, int);		/* ignore value, advance ap */
    234 	va_end(ap);
    235 
    236 	MCLAIM(m, &udp_rx_mowner);
    237 	udpstat.udps_ipackets++;
    238 
    239 #ifndef PULLDOWN_TEST
    240 	/*
    241 	 * Strip IP options, if any; should skip this,
    242 	 * make available to user, and use on returned packets,
    243 	 * but we don't yet have a way to check the checksum
    244 	 * with options still present.
    245 	 */
    246 	if (iphlen > sizeof (struct ip)) {
    247 		ip_stripoptions(m, (struct mbuf *)0);
    248 		iphlen = sizeof(struct ip);
    249 	}
    250 #else
    251 	/*
    252 	 * we may enable the above code if we save and pass IPv4 options
    253 	 * to the userland.
    254 	 */
    255 #endif
    256 
    257 	/*
    258 	 * Get IP and UDP header together in first mbuf.
    259 	 */
    260 	ip = mtod(m, struct ip *);
    261 #ifndef PULLDOWN_TEST
    262 	if (m->m_len < iphlen + sizeof(struct udphdr)) {
    263 		if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) {
    264 			udpstat.udps_hdrops++;
    265 			return;
    266 		}
    267 		ip = mtod(m, struct ip *);
    268 	}
    269 	uh = (struct udphdr *)((caddr_t)ip + iphlen);
    270 #else
    271 	IP6_EXTHDR_GET(uh, struct udphdr *, m, iphlen, sizeof(struct udphdr));
    272 	if (uh == NULL) {
    273 		udpstat.udps_hdrops++;
    274 		return;
    275 	}
    276 #endif
    277 	KASSERT(UDP_HDR_ALIGNED_P(uh));
    278 
    279 	/* destination port of 0 is illegal, based on RFC768. */
    280 	if (uh->uh_dport == 0)
    281 		goto bad;
    282 
    283 	/*
    284 	 * Make mbuf data length reflect UDP length.
    285 	 * If not enough data to reflect UDP length, drop.
    286 	 */
    287 	ip_len = ntohs(ip->ip_len);
    288 	len = ntohs((u_int16_t)uh->uh_ulen);
    289 	if (ip_len != iphlen + len) {
    290 		if (ip_len < iphlen + len || len < sizeof(struct udphdr)) {
    291 			udpstat.udps_badlen++;
    292 			goto bad;
    293 		}
    294 		m_adj(m, iphlen + len - ip_len);
    295 	}
    296 
    297 	/*
    298 	 * Checksum extended UDP header and data.
    299 	 */
    300 	if (uh->uh_sum) {
    301 		switch (m->m_pkthdr.csum_flags &
    302 			((m->m_pkthdr.rcvif->if_csum_flags_rx & M_CSUM_UDPv4) |
    303 			 M_CSUM_TCP_UDP_BAD | M_CSUM_DATA)) {
    304 		case M_CSUM_UDPv4|M_CSUM_TCP_UDP_BAD:
    305 			UDP_CSUM_COUNTER_INCR(&udp_hwcsum_bad);
    306 			goto badcsum;
    307 
    308 		case M_CSUM_UDPv4|M_CSUM_DATA:
    309 			UDP_CSUM_COUNTER_INCR(&udp_hwcsum_data);
    310 			if ((m->m_pkthdr.csum_data ^ 0xffff) != 0)
    311 				goto badcsum;
    312 			break;
    313 
    314 		case M_CSUM_UDPv4:
    315 			/* Checksum was okay. */
    316 			UDP_CSUM_COUNTER_INCR(&udp_hwcsum_ok);
    317 			break;
    318 
    319 		default:
    320 			/* Need to compute it ourselves. */
    321 			UDP_CSUM_COUNTER_INCR(&udp_swcsum);
    322 			if (in4_cksum(m, IPPROTO_UDP, iphlen, len) != 0)
    323 				goto badcsum;
    324 			break;
    325 		}
    326 	}
    327 
    328 	/* construct source and dst sockaddrs. */
    329 	bzero(&src, sizeof(src));
    330 	src.sin_family = AF_INET;
    331 	src.sin_len = sizeof(struct sockaddr_in);
    332 	bcopy(&ip->ip_src, &src.sin_addr, sizeof(src.sin_addr));
    333 	src.sin_port = uh->uh_sport;
    334 	bzero(&dst, sizeof(dst));
    335 	dst.sin_family = AF_INET;
    336 	dst.sin_len = sizeof(struct sockaddr_in);
    337 	bcopy(&ip->ip_dst, &dst.sin_addr, sizeof(dst.sin_addr));
    338 	dst.sin_port = uh->uh_dport;
    339 
    340 	n = udp4_realinput(&src, &dst, m, iphlen);
    341 #ifdef INET6
    342 	if (IN_MULTICAST(ip->ip_dst.s_addr) || n == 0) {
    343 		struct sockaddr_in6 src6, dst6;
    344 
    345 		bzero(&src6, sizeof(src6));
    346 		src6.sin6_family = AF_INET6;
    347 		src6.sin6_len = sizeof(struct sockaddr_in6);
    348 		src6.sin6_addr.s6_addr[10] = src6.sin6_addr.s6_addr[11] = 0xff;
    349 		bcopy(&ip->ip_src, &src6.sin6_addr.s6_addr[12],
    350 			sizeof(ip->ip_src));
    351 		src6.sin6_port = uh->uh_sport;
    352 		bzero(&dst6, sizeof(dst6));
    353 		dst6.sin6_family = AF_INET6;
    354 		dst6.sin6_len = sizeof(struct sockaddr_in6);
    355 		dst6.sin6_addr.s6_addr[10] = dst6.sin6_addr.s6_addr[11] = 0xff;
    356 		bcopy(&ip->ip_dst, &dst6.sin6_addr.s6_addr[12],
    357 			sizeof(ip->ip_dst));
    358 		dst6.sin6_port = uh->uh_dport;
    359 
    360 		n += udp6_realinput(AF_INET, &src6, &dst6, m, iphlen);
    361 	}
    362 #endif
    363 
    364 	if (n == 0) {
    365 		if (m->m_flags & (M_BCAST | M_MCAST)) {
    366 			udpstat.udps_noportbcast++;
    367 			goto bad;
    368 		}
    369 		udpstat.udps_noport++;
    370 #ifdef IPKDB
    371 		if (checkipkdb(&ip->ip_src, uh->uh_sport, uh->uh_dport,
    372 				m, iphlen + sizeof(struct udphdr),
    373 				m->m_pkthdr.len - iphlen - sizeof(struct udphdr))) {
    374 			/*
    375 			 * It was a debugger connect packet,
    376 			 * just drop it now
    377 			 */
    378 			goto bad;
    379 		}
    380 #endif
    381 		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
    382 		m = NULL;
    383 	}
    384 
    385 bad:
    386 	if (m)
    387 		m_freem(m);
    388 	return;
    389 
    390 badcsum:
    391 	m_freem(m);
    392 	udpstat.udps_badsum++;
    393 }
    394 #endif
    395 
    396 #ifdef INET6
    397 int
    398 udp6_input(mp, offp, proto)
    399 	struct mbuf **mp;
    400 	int *offp, proto;
    401 {
    402 	struct mbuf *m = *mp;
    403 	int off = *offp;
    404 	struct sockaddr_in6 src, dst;
    405 	struct ip6_hdr *ip6;
    406 	struct udphdr *uh;
    407 	u_int32_t plen, ulen;
    408 
    409 #ifndef PULLDOWN_TEST
    410 	IP6_EXTHDR_CHECK(m, off, sizeof(struct udphdr), IPPROTO_DONE);
    411 #endif
    412 	ip6 = mtod(m, struct ip6_hdr *);
    413 
    414 #if defined(NFAITH) && 0 < NFAITH
    415 	if (faithprefix(&ip6->ip6_dst)) {
    416 		/* send icmp6 host unreach? */
    417 		m_freem(m);
    418 		return IPPROTO_DONE;
    419 	}
    420 #endif
    421 
    422 	udp6stat.udp6s_ipackets++;
    423 
    424 	/* check for jumbogram is done in ip6_input.  we can trust pkthdr.len */
    425 	plen = m->m_pkthdr.len - off;
    426 #ifndef PULLDOWN_TEST
    427 	uh = (struct udphdr *)((caddr_t)ip6 + off);
    428 #else
    429 	IP6_EXTHDR_GET(uh, struct udphdr *, m, off, sizeof(struct udphdr));
    430 	if (uh == NULL) {
    431 		ip6stat.ip6s_tooshort++;
    432 		return IPPROTO_DONE;
    433 	}
    434 #endif
    435 	KASSERT(UDP_HDR_ALIGNED_P(uh));
    436 	ulen = ntohs((u_short)uh->uh_ulen);
    437 	/*
    438 	 * RFC2675 section 4: jumbograms will have 0 in the UDP header field,
    439 	 * iff payload length > 0xffff.
    440 	 */
    441 	if (ulen == 0 && plen > 0xffff)
    442 		ulen = plen;
    443 
    444 	if (plen != ulen) {
    445 		udp6stat.udp6s_badlen++;
    446 		goto bad;
    447 	}
    448 
    449 	/* destination port of 0 is illegal, based on RFC768. */
    450 	if (uh->uh_dport == 0)
    451 		goto bad;
    452 
    453 	/* Be proactive about malicious use of IPv4 mapped address */
    454 	if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
    455 	    IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
    456 		/* XXX stat */
    457 		goto bad;
    458 	}
    459 
    460 	/*
    461 	 * Checksum extended UDP header and data.
    462 	 */
    463 	if (uh->uh_sum == 0)
    464 		udp6stat.udp6s_nosum++;
    465 	else if (in6_cksum(m, IPPROTO_UDP, off, ulen) != 0) {
    466 		udp6stat.udp6s_badsum++;
    467 		goto bad;
    468 	}
    469 
    470 	/*
    471 	 * Construct source and dst sockaddrs.
    472 	 * Note that ifindex (s6_addr16[1]) is already filled.
    473 	 */
    474 	bzero(&src, sizeof(src));
    475 	src.sin6_family = AF_INET6;
    476 	src.sin6_len = sizeof(struct sockaddr_in6);
    477 	/* KAME hack: recover scopeid */
    478 	(void)in6_recoverscope(&src, &ip6->ip6_src, m->m_pkthdr.rcvif);
    479 	src.sin6_port = uh->uh_sport;
    480 	bzero(&dst, sizeof(dst));
    481 	dst.sin6_family = AF_INET6;
    482 	dst.sin6_len = sizeof(struct sockaddr_in6);
    483 	/* KAME hack: recover scopeid */
    484 	(void)in6_recoverscope(&dst, &ip6->ip6_dst, m->m_pkthdr.rcvif);
    485 	dst.sin6_port = uh->uh_dport;
    486 
    487 	if (udp6_realinput(AF_INET6, &src, &dst, m, off) == 0) {
    488 		if (m->m_flags & M_MCAST) {
    489 			udp6stat.udp6s_noportmcast++;
    490 			goto bad;
    491 		}
    492 		udp6stat.udp6s_noport++;
    493 		icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
    494 		m = NULL;
    495 	}
    496 
    497 bad:
    498 	if (m)
    499 		m_freem(m);
    500 	return IPPROTO_DONE;
    501 }
    502 #endif
    503 
    504 #ifdef INET
    505 static void
    506 udp4_sendup(m, off, src, so)
    507 	struct mbuf *m;
    508 	int off;	/* offset of data portion */
    509 	struct sockaddr *src;
    510 	struct socket *so;
    511 {
    512 	struct mbuf *opts = NULL;
    513 	struct mbuf *n;
    514 	struct inpcb *inp = NULL;
    515 
    516 	if (!so)
    517 		return;
    518 	switch (so->so_proto->pr_domain->dom_family) {
    519 	case AF_INET:
    520 		inp = sotoinpcb(so);
    521 		break;
    522 #ifdef INET6
    523 	case AF_INET6:
    524 		break;
    525 #endif
    526 	default:
    527 		return;
    528 	}
    529 
    530 #ifdef IPSEC
    531 	/* check AH/ESP integrity. */
    532 	if (so != NULL && ipsec4_in_reject_so(m, so)) {
    533 		ipsecstat.in_polvio++;
    534 		return;
    535 	}
    536 #endif /*IPSEC*/
    537 
    538 	if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
    539 		if (inp && (inp->inp_flags & INP_CONTROLOPTS
    540 			 || so->so_options & SO_TIMESTAMP)) {
    541 			struct ip *ip = mtod(n, struct ip *);
    542 			ip_savecontrol(inp, &opts, ip, n);
    543 		}
    544 
    545 		m_adj(n, off);
    546 		if (sbappendaddr(&so->so_rcv, src, n,
    547 				opts) == 0) {
    548 			m_freem(n);
    549 			if (opts)
    550 				m_freem(opts);
    551 			udpstat.udps_fullsock++;
    552 		} else
    553 			sorwakeup(so);
    554 	}
    555 }
    556 #endif
    557 
    558 #ifdef INET6
    559 static void
    560 udp6_sendup(m, off, src, so)
    561 	struct mbuf *m;
    562 	int off;	/* offset of data portion */
    563 	struct sockaddr *src;
    564 	struct socket *so;
    565 {
    566 	struct mbuf *opts = NULL;
    567 	struct mbuf *n;
    568 	struct in6pcb *in6p = NULL;
    569 
    570 	if (!so)
    571 		return;
    572 	if (so->so_proto->pr_domain->dom_family != AF_INET6)
    573 		return;
    574 	in6p = sotoin6pcb(so);
    575 
    576 #ifdef IPSEC
    577 	/* check AH/ESP integrity. */
    578 	if (so != NULL && ipsec6_in_reject_so(m, so)) {
    579 		ipsec6stat.in_polvio++;
    580 		return;
    581 	}
    582 #endif /*IPSEC*/
    583 
    584 	if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
    585 		if (in6p && (in6p->in6p_flags & IN6P_CONTROLOPTS
    586 			  || in6p->in6p_socket->so_options & SO_TIMESTAMP)) {
    587 			struct ip6_hdr *ip6 = mtod(n, struct ip6_hdr *);
    588 			ip6_savecontrol(in6p, &opts, ip6, n);
    589 		}
    590 
    591 		m_adj(n, off);
    592 		if (sbappendaddr(&so->so_rcv, src, n, opts) == 0) {
    593 			m_freem(n);
    594 			if (opts)
    595 				m_freem(opts);
    596 			udp6stat.udp6s_fullsock++;
    597 		} else
    598 			sorwakeup(so);
    599 	}
    600 }
    601 #endif
    602 
    603 #ifdef INET
    604 static int
    605 udp4_realinput(src, dst, m, off)
    606 	struct sockaddr_in *src;
    607 	struct sockaddr_in *dst;
    608 	struct mbuf *m;
    609 	int off;	/* offset of udphdr */
    610 {
    611 	u_int16_t *sport, *dport;
    612 	int rcvcnt;
    613 	struct in_addr *src4, *dst4;
    614 	struct inpcb *inp;
    615 
    616 	rcvcnt = 0;
    617 	off += sizeof(struct udphdr);	/* now, offset of payload */
    618 
    619 	if (src->sin_family != AF_INET || dst->sin_family != AF_INET)
    620 		goto bad;
    621 
    622 	src4 = &src->sin_addr;
    623 	sport = &src->sin_port;
    624 	dst4 = &dst->sin_addr;
    625 	dport = &dst->sin_port;
    626 
    627 	if (IN_MULTICAST(dst4->s_addr) ||
    628 	    in_broadcast(*dst4, m->m_pkthdr.rcvif)) {
    629 		/*
    630 		 * Deliver a multicast or broadcast datagram to *all* sockets
    631 		 * for which the local and remote addresses and ports match
    632 		 * those of the incoming datagram.  This allows more than
    633 		 * one process to receive multi/broadcasts on the same port.
    634 		 * (This really ought to be done for unicast datagrams as
    635 		 * well, but that would cause problems with existing
    636 		 * applications that open both address-specific sockets and
    637 		 * a wildcard socket listening to the same port -- they would
    638 		 * end up receiving duplicates of every unicast datagram.
    639 		 * Those applications open the multiple sockets to overcome an
    640 		 * inadequacy of the UDP socket interface, but for backwards
    641 		 * compatibility we avoid the problem here rather than
    642 		 * fixing the interface.  Maybe 4.5BSD will remedy this?)
    643 		 */
    644 
    645 		/*
    646 		 * KAME note: traditionally we dropped udpiphdr from mbuf here.
    647 		 * we need udpiphdr for IPsec processing so we do that later.
    648 		 */
    649 		/*
    650 		 * Locate pcb(s) for datagram.
    651 		 */
    652 		CIRCLEQ_FOREACH(inp, &udbtable.inpt_queue, inp_queue) {
    653 			if (inp->inp_lport != *dport)
    654 				continue;
    655 			if (!in_nullhost(inp->inp_laddr)) {
    656 				if (!in_hosteq(inp->inp_laddr, *dst4))
    657 					continue;
    658 			}
    659 			if (!in_nullhost(inp->inp_faddr)) {
    660 				if (!in_hosteq(inp->inp_faddr, *src4) ||
    661 				    inp->inp_fport != *sport)
    662 					continue;
    663 			}
    664 
    665 			udp4_sendup(m, off, (struct sockaddr *)src,
    666 				inp->inp_socket);
    667 			rcvcnt++;
    668 
    669 			/*
    670 			 * Don't look for additional matches if this one does
    671 			 * not have either the SO_REUSEPORT or SO_REUSEADDR
    672 			 * socket options set.  This heuristic avoids searching
    673 			 * through all pcbs in the common case of a non-shared
    674 			 * port.  It assumes that an application will never
    675 			 * clear these options after setting them.
    676 			 */
    677 			if ((inp->inp_socket->so_options &
    678 			    (SO_REUSEPORT|SO_REUSEADDR)) == 0)
    679 				break;
    680 		}
    681 	} else {
    682 		/*
    683 		 * Locate pcb for datagram.
    684 		 */
    685 		inp = in_pcblookup_connect(&udbtable, *src4, *sport, *dst4, *dport);
    686 		if (inp == 0) {
    687 			++udpstat.udps_pcbhashmiss;
    688 			inp = in_pcblookup_bind(&udbtable, *dst4, *dport);
    689 			if (inp == 0)
    690 				return rcvcnt;
    691 		}
    692 
    693 		udp4_sendup(m, off, (struct sockaddr *)src, inp->inp_socket);
    694 		rcvcnt++;
    695 	}
    696 
    697 bad:
    698 	return rcvcnt;
    699 }
    700 #endif
    701 
    702 #ifdef INET6
    703 static int
    704 udp6_realinput(af, src, dst, m, off)
    705 	int af;		/* af on packet */
    706 	struct sockaddr_in6 *src;
    707 	struct sockaddr_in6 *dst;
    708 	struct mbuf *m;
    709 	int off;	/* offset of udphdr */
    710 {
    711 	u_int16_t sport, dport;
    712 	int rcvcnt;
    713 	struct in6_addr src6, dst6;
    714 	const struct in_addr *dst4;
    715 	struct in6pcb *in6p;
    716 
    717 	rcvcnt = 0;
    718 	off += sizeof(struct udphdr);	/* now, offset of payload */
    719 
    720 	if (af != AF_INET && af != AF_INET6)
    721 		goto bad;
    722 	if (src->sin6_family != AF_INET6 || dst->sin6_family != AF_INET6)
    723 		goto bad;
    724 
    725 	in6_embedscope(&src6, src, NULL, NULL);
    726 	sport = src->sin6_port;
    727 	in6_embedscope(&dst6, dst, NULL, NULL);
    728 	dport = dst->sin6_port;
    729 	dst4 = (struct in_addr *)&dst->sin6_addr.s6_addr[12];
    730 
    731 	if (IN6_IS_ADDR_MULTICAST(&dst6) ||
    732 	    (af == AF_INET && IN_MULTICAST(dst4->s_addr))) {
    733 		/*
    734 		 * Deliver a multicast or broadcast datagram to *all* sockets
    735 		 * for which the local and remote addresses and ports match
    736 		 * those of the incoming datagram.  This allows more than
    737 		 * one process to receive multi/broadcasts on the same port.
    738 		 * (This really ought to be done for unicast datagrams as
    739 		 * well, but that would cause problems with existing
    740 		 * applications that open both address-specific sockets and
    741 		 * a wildcard socket listening to the same port -- they would
    742 		 * end up receiving duplicates of every unicast datagram.
    743 		 * Those applications open the multiple sockets to overcome an
    744 		 * inadequacy of the UDP socket interface, but for backwards
    745 		 * compatibility we avoid the problem here rather than
    746 		 * fixing the interface.  Maybe 4.5BSD will remedy this?)
    747 		 */
    748 
    749 		/*
    750 		 * KAME note: traditionally we dropped udpiphdr from mbuf here.
    751 		 * we need udpiphdr for IPsec processing so we do that later.
    752 		 */
    753 		/*
    754 		 * Locate pcb(s) for datagram.
    755 		 */
    756 		for (in6p = udb6.in6p_next; in6p != &udb6;
    757 		     in6p = in6p->in6p_next) {
    758 			if (in6p->in6p_lport != dport)
    759 				continue;
    760 			if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
    761 				if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &dst6))
    762 					continue;
    763 			} else {
    764 				if (IN6_IS_ADDR_V4MAPPED(&dst6) &&
    765 				    (in6p->in6p_flags & IN6P_IPV6_V6ONLY))
    766 					continue;
    767 			}
    768 			if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
    769 				if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,
    770 				    &src6) || in6p->in6p_fport != sport)
    771 					continue;
    772 			} else {
    773 				if (IN6_IS_ADDR_V4MAPPED(&src6) &&
    774 				    (in6p->in6p_flags & IN6P_IPV6_V6ONLY))
    775 					continue;
    776 			}
    777 
    778 			udp6_sendup(m, off, (struct sockaddr *)src,
    779 				in6p->in6p_socket);
    780 			rcvcnt++;
    781 
    782 			/*
    783 			 * Don't look for additional matches if this one does
    784 			 * not have either the SO_REUSEPORT or SO_REUSEADDR
    785 			 * socket options set.  This heuristic avoids searching
    786 			 * through all pcbs in the common case of a non-shared
    787 			 * port.  It assumes that an application will never
    788 			 * clear these options after setting them.
    789 			 */
    790 			if ((in6p->in6p_socket->so_options &
    791 			    (SO_REUSEPORT|SO_REUSEADDR)) == 0)
    792 				break;
    793 		}
    794 	} else {
    795 		/*
    796 		 * Locate pcb for datagram.
    797 		 */
    798 		in6p = in6_pcblookup_connect(&udb6, &src6, sport,
    799 		    &dst6, dport, 0);
    800 		if (in6p == 0) {
    801 			++udpstat.udps_pcbhashmiss;
    802 			in6p = in6_pcblookup_bind(&udb6, &dst6, dport, 0);
    803 			if (in6p == 0)
    804 				return rcvcnt;
    805 		}
    806 
    807 		udp6_sendup(m, off, (struct sockaddr *)src, in6p->in6p_socket);
    808 		rcvcnt++;
    809 	}
    810 
    811 bad:
    812 	return rcvcnt;
    813 }
    814 #endif
    815 
    816 #ifdef INET
    817 /*
    818  * Notify a udp user of an asynchronous error;
    819  * just wake up so that he can collect error status.
    820  */
    821 static void
    822 udp_notify(inp, errno)
    823 	struct inpcb *inp;
    824 	int errno;
    825 {
    826 
    827 	inp->inp_socket->so_error = errno;
    828 	sorwakeup(inp->inp_socket);
    829 	sowwakeup(inp->inp_socket);
    830 }
    831 
    832 void *
    833 udp_ctlinput(cmd, sa, v)
    834 	int cmd;
    835 	struct sockaddr *sa;
    836 	void *v;
    837 {
    838 	struct ip *ip = v;
    839 	struct udphdr *uh;
    840 	void (*notify) __P((struct inpcb *, int)) = udp_notify;
    841 	int errno;
    842 
    843 	if (sa->sa_family != AF_INET
    844 	 || sa->sa_len != sizeof(struct sockaddr_in))
    845 		return NULL;
    846 	if ((unsigned)cmd >= PRC_NCMDS)
    847 		return NULL;
    848 	errno = inetctlerrmap[cmd];
    849 	if (PRC_IS_REDIRECT(cmd))
    850 		notify = in_rtchange, ip = 0;
    851 	else if (cmd == PRC_HOSTDEAD)
    852 		ip = 0;
    853 	else if (errno == 0)
    854 		return NULL;
    855 	if (ip) {
    856 		uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
    857 		in_pcbnotify(&udbtable, satosin(sa)->sin_addr, uh->uh_dport,
    858 		    ip->ip_src, uh->uh_sport, errno, notify);
    859 
    860 		/* XXX mapped address case */
    861 	} else
    862 		in_pcbnotifyall(&udbtable, satosin(sa)->sin_addr, errno,
    863 		    notify);
    864 	return NULL;
    865 }
    866 
    867 int
    868 #if __STDC__
    869 udp_output(struct mbuf *m, ...)
    870 #else
    871 udp_output(m, va_alist)
    872 	struct mbuf *m;
    873 	va_dcl
    874 #endif
    875 {
    876 	struct inpcb *inp;
    877 	struct udpiphdr *ui;
    878 	int len = m->m_pkthdr.len;
    879 	int error = 0;
    880 	va_list ap;
    881 
    882 	MCLAIM(m, &udp_tx_mowner);
    883 	va_start(ap, m);
    884 	inp = va_arg(ap, struct inpcb *);
    885 	va_end(ap);
    886 
    887 	/*
    888 	 * Calculate data length and get a mbuf
    889 	 * for UDP and IP headers.
    890 	 */
    891 	M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT);
    892 	if (m == 0) {
    893 		error = ENOBUFS;
    894 		goto release;
    895 	}
    896 
    897 	/*
    898 	 * Compute the packet length of the IP header, and
    899 	 * punt if the length looks bogus.
    900 	 */
    901 	if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
    902 		error = EMSGSIZE;
    903 		goto release;
    904 	}
    905 
    906 	/*
    907 	 * Fill in mbuf with extended UDP header
    908 	 * and addresses and length put into network format.
    909 	 */
    910 	ui = mtod(m, struct udpiphdr *);
    911 	ui->ui_pr = IPPROTO_UDP;
    912 	ui->ui_src = inp->inp_laddr;
    913 	ui->ui_dst = inp->inp_faddr;
    914 	ui->ui_sport = inp->inp_lport;
    915 	ui->ui_dport = inp->inp_fport;
    916 	ui->ui_ulen = htons((u_int16_t)len + sizeof(struct udphdr));
    917 
    918 	/*
    919 	 * Set up checksum and output datagram.
    920 	 */
    921 	if (udpcksum) {
    922 		/*
    923 		 * XXX Cache pseudo-header checksum part for
    924 		 * XXX "connected" UDP sockets.
    925 		 */
    926 		ui->ui_sum = in_cksum_phdr(ui->ui_src.s_addr,
    927 		    ui->ui_dst.s_addr, htons((u_int16_t)len +
    928 		    sizeof(struct udphdr) + IPPROTO_UDP));
    929 		m->m_pkthdr.csum_flags = M_CSUM_UDPv4;
    930 		m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
    931 	} else
    932 		ui->ui_sum = 0;
    933 	((struct ip *)ui)->ip_len = htons(sizeof (struct udpiphdr) + len);
    934 	((struct ip *)ui)->ip_ttl = inp->inp_ip.ip_ttl;	/* XXX */
    935 	((struct ip *)ui)->ip_tos = inp->inp_ip.ip_tos;	/* XXX */
    936 	udpstat.udps_opackets++;
    937 
    938 #ifdef IPSEC
    939 	if (ipsec_setsocket(m, inp->inp_socket) != 0) {
    940 		error = ENOBUFS;
    941 		goto release;
    942 	}
    943 #endif /*IPSEC*/
    944 
    945 	return (ip_output(m, inp->inp_options, &inp->inp_route,
    946 	    inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST),
    947 	    inp->inp_moptions));
    948 
    949 release:
    950 	m_freem(m);
    951 	return (error);
    952 }
    953 
    954 int	udp_sendspace = 9216;		/* really max datagram size */
    955 int	udp_recvspace = 40 * (1024 + sizeof(struct sockaddr_in));
    956 					/* 40 1K datagrams */
    957 
    958 /*ARGSUSED*/
    959 int
    960 udp_usrreq(so, req, m, nam, control, p)
    961 	struct socket *so;
    962 	int req;
    963 	struct mbuf *m, *nam, *control;
    964 	struct proc *p;
    965 {
    966 	struct inpcb *inp;
    967 	int s;
    968 	int error = 0;
    969 
    970 	if (req == PRU_CONTROL)
    971 		return (in_control(so, (long)m, (caddr_t)nam,
    972 		    (struct ifnet *)control, p));
    973 
    974 	if (req == PRU_PURGEIF) {
    975 		in_pcbpurgeif0(&udbtable, (struct ifnet *)control);
    976 		in_purgeif((struct ifnet *)control);
    977 		in_pcbpurgeif(&udbtable, (struct ifnet *)control);
    978 		return (0);
    979 	}
    980 
    981 	s = splsoftnet();
    982 	inp = sotoinpcb(so);
    983 #ifdef DIAGNOSTIC
    984 	if (req != PRU_SEND && req != PRU_SENDOOB && control)
    985 		panic("udp_usrreq: unexpected control mbuf");
    986 #endif
    987 	if (inp == 0 && req != PRU_ATTACH) {
    988 		error = EINVAL;
    989 		goto release;
    990 	}
    991 
    992 	/*
    993 	 * Note: need to block udp_input while changing
    994 	 * the udp pcb queue and/or pcb addresses.
    995 	 */
    996 	switch (req) {
    997 
    998 	case PRU_ATTACH:
    999 		if (inp != 0) {
   1000 			error = EISCONN;
   1001 			break;
   1002 		}
   1003 #ifdef MBUFTRACE
   1004 		so->so_mowner = &udp_mowner;
   1005 		so->so_rcv.sb_mowner = &udp_rx_mowner;
   1006 		so->so_snd.sb_mowner = &udp_tx_mowner;
   1007 #endif
   1008 		if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
   1009 			error = soreserve(so, udp_sendspace, udp_recvspace);
   1010 			if (error)
   1011 				break;
   1012 		}
   1013 		error = in_pcballoc(so, &udbtable);
   1014 		if (error)
   1015 			break;
   1016 		inp = sotoinpcb(so);
   1017 		inp->inp_ip.ip_ttl = ip_defttl;
   1018 		break;
   1019 
   1020 	case PRU_DETACH:
   1021 		in_pcbdetach(inp);
   1022 		break;
   1023 
   1024 	case PRU_BIND:
   1025 		error = in_pcbbind(inp, nam, p);
   1026 		break;
   1027 
   1028 	case PRU_LISTEN:
   1029 		error = EOPNOTSUPP;
   1030 		break;
   1031 
   1032 	case PRU_CONNECT:
   1033 		error = in_pcbconnect(inp, nam);
   1034 		if (error)
   1035 			break;
   1036 		soisconnected(so);
   1037 		break;
   1038 
   1039 	case PRU_CONNECT2:
   1040 		error = EOPNOTSUPP;
   1041 		break;
   1042 
   1043 	case PRU_DISCONNECT:
   1044 		/*soisdisconnected(so);*/
   1045 		so->so_state &= ~SS_ISCONNECTED;	/* XXX */
   1046 		in_pcbdisconnect(inp);
   1047 		inp->inp_laddr = zeroin_addr;		/* XXX */
   1048 		in_pcbstate(inp, INP_BOUND);		/* XXX */
   1049 		break;
   1050 
   1051 	case PRU_SHUTDOWN:
   1052 		socantsendmore(so);
   1053 		break;
   1054 
   1055 	case PRU_RCVD:
   1056 		error = EOPNOTSUPP;
   1057 		break;
   1058 
   1059 	case PRU_SEND:
   1060 		if (control && control->m_len) {
   1061 			m_freem(control);
   1062 			m_freem(m);
   1063 			error = EINVAL;
   1064 			break;
   1065 		}
   1066 	{
   1067 		struct in_addr laddr;			/* XXX */
   1068 
   1069 		if (nam) {
   1070 			laddr = inp->inp_laddr;		/* XXX */
   1071 			if ((so->so_state & SS_ISCONNECTED) != 0) {
   1072 				error = EISCONN;
   1073 				goto die;
   1074 			}
   1075 			error = in_pcbconnect(inp, nam);
   1076 			if (error) {
   1077 			die:
   1078 				m_freem(m);
   1079 				break;
   1080 			}
   1081 		} else {
   1082 			if ((so->so_state & SS_ISCONNECTED) == 0) {
   1083 				error = ENOTCONN;
   1084 				goto die;
   1085 			}
   1086 		}
   1087 		error = udp_output(m, inp);
   1088 		if (nam) {
   1089 			in_pcbdisconnect(inp);
   1090 			inp->inp_laddr = laddr;		/* XXX */
   1091 			in_pcbstate(inp, INP_BOUND);	/* XXX */
   1092 		}
   1093 	}
   1094 		break;
   1095 
   1096 	case PRU_SENSE:
   1097 		/*
   1098 		 * stat: don't bother with a blocksize.
   1099 		 */
   1100 		splx(s);
   1101 		return (0);
   1102 
   1103 	case PRU_RCVOOB:
   1104 		error =  EOPNOTSUPP;
   1105 		break;
   1106 
   1107 	case PRU_SENDOOB:
   1108 		m_freem(control);
   1109 		m_freem(m);
   1110 		error =  EOPNOTSUPP;
   1111 		break;
   1112 
   1113 	case PRU_SOCKADDR:
   1114 		in_setsockaddr(inp, nam);
   1115 		break;
   1116 
   1117 	case PRU_PEERADDR:
   1118 		in_setpeeraddr(inp, nam);
   1119 		break;
   1120 
   1121 	default:
   1122 		panic("udp_usrreq");
   1123 	}
   1124 
   1125 release:
   1126 	splx(s);
   1127 	return (error);
   1128 }
   1129 
   1130 /*
   1131  * Sysctl for udp variables.
   1132  */
   1133 int
   1134 udp_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
   1135 	int *name;
   1136 	u_int namelen;
   1137 	void *oldp;
   1138 	size_t *oldlenp;
   1139 	void *newp;
   1140 	size_t newlen;
   1141 {
   1142 	/* All sysctl names at this level are terminal. */
   1143 	if (namelen != 1)
   1144 		return (ENOTDIR);
   1145 
   1146 	switch (name[0]) {
   1147 	case UDPCTL_CHECKSUM:
   1148 		return (sysctl_int(oldp, oldlenp, newp, newlen, &udpcksum));
   1149 	case UDPCTL_SENDSPACE:
   1150 		return (sysctl_int(oldp, oldlenp, newp, newlen,
   1151 		    &udp_sendspace));
   1152 	case UDPCTL_RECVSPACE:
   1153 		return (sysctl_int(oldp, oldlenp, newp, newlen,
   1154 		    &udp_recvspace));
   1155 	default:
   1156 		return (ENOPROTOOPT);
   1157 	}
   1158 	/* NOTREACHED */
   1159 }
   1160 #endif
   1161