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