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