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