Home | History | Annotate | Line # | Download | only in netinet
udp_usrreq.c revision 1.93.4.2
      1 /*	$NetBSD: udp_usrreq.c,v 1.93.4.2 2004/03/31 20:10:29 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.2 2004/03/31 20:10:29 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 	}
    452 	if (in6_cksum(m, IPPROTO_UDP, off, ulen) != 0) {
    453 		udp6stat.udp6s_badsum++;
    454 		goto bad;
    455 	}
    456 
    457 	/*
    458 	 * Construct source and dst sockaddrs.
    459 	 * Note that ifindex (s6_addr16[1]) is already filled.
    460 	 */
    461 	bzero(&src, sizeof(src));
    462 	src.sin6_family = AF_INET6;
    463 	src.sin6_len = sizeof(struct sockaddr_in6);
    464 	/* KAME hack: recover scopeid */
    465 	(void)in6_recoverscope(&src, &ip6->ip6_src, m->m_pkthdr.rcvif);
    466 	src.sin6_port = uh->uh_sport;
    467 	bzero(&dst, sizeof(dst));
    468 	dst.sin6_family = AF_INET6;
    469 	dst.sin6_len = sizeof(struct sockaddr_in6);
    470 	/* KAME hack: recover scopeid */
    471 	(void)in6_recoverscope(&dst, &ip6->ip6_dst, m->m_pkthdr.rcvif);
    472 	dst.sin6_port = uh->uh_dport;
    473 
    474 	if (udp6_realinput(AF_INET6, &src, &dst, m, off) == 0) {
    475 		if (m->m_flags & M_MCAST) {
    476 			udp6stat.udp6s_noportmcast++;
    477 			goto bad;
    478 		}
    479 		udp6stat.udp6s_noport++;
    480 		icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
    481 		m = NULL;
    482 	}
    483 
    484 bad:
    485 	if (m)
    486 		m_freem(m);
    487 	return IPPROTO_DONE;
    488 }
    489 #endif
    490 
    491 #ifdef INET
    492 static void
    493 udp4_sendup(m, off, src, so)
    494 	struct mbuf *m;
    495 	int off;	/* offset of data portion */
    496 	struct sockaddr *src;
    497 	struct socket *so;
    498 {
    499 	struct mbuf *opts = NULL;
    500 	struct mbuf *n;
    501 	struct inpcb *inp = NULL;
    502 #ifdef INET6
    503 	struct in6pcb *in6p = NULL;
    504 #endif
    505 
    506 	if (!so)
    507 		return;
    508 	switch (so->so_proto->pr_domain->dom_family) {
    509 	case AF_INET:
    510 		inp = sotoinpcb(so);
    511 		break;
    512 #ifdef INET6
    513 	case AF_INET6:
    514 		in6p = sotoin6pcb(so);
    515 		break;
    516 #endif
    517 	default:
    518 		return;
    519 	}
    520 
    521 #ifdef IPSEC
    522 	/* check AH/ESP integrity. */
    523 	if (so != NULL && ipsec4_in_reject_so(m, so)) {
    524 		ipsecstat.in_polvio++;
    525 		return;
    526 	}
    527 #endif /*IPSEC*/
    528 
    529 	if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
    530 		if (inp && (inp->inp_flags & INP_CONTROLOPTS
    531 			 || so->so_options & SO_TIMESTAMP)) {
    532 			struct ip *ip = mtod(n, struct ip *);
    533 			ip_savecontrol(inp, &opts, ip, n);
    534 		}
    535 
    536 		m_adj(n, off);
    537 		if (sbappendaddr(&so->so_rcv, src, n,
    538 				opts) == 0) {
    539 			m_freem(n);
    540 			if (opts)
    541 				m_freem(opts);
    542 			udpstat.udps_fullsock++;
    543 		} else
    544 			sorwakeup(so);
    545 	}
    546 }
    547 #endif
    548 
    549 #ifdef INET6
    550 static void
    551 udp6_sendup(m, off, src, so)
    552 	struct mbuf *m;
    553 	int off;	/* offset of data portion */
    554 	struct sockaddr *src;
    555 	struct socket *so;
    556 {
    557 	struct mbuf *opts = NULL;
    558 	struct mbuf *n;
    559 	struct in6pcb *in6p = NULL;
    560 
    561 	if (!so)
    562 		return;
    563 	if (so->so_proto->pr_domain->dom_family != AF_INET6)
    564 		return;
    565 	in6p = sotoin6pcb(so);
    566 
    567 #ifdef IPSEC
    568 	/* check AH/ESP integrity. */
    569 	if (so != NULL && ipsec6_in_reject_so(m, so)) {
    570 		ipsec6stat.in_polvio++;
    571 		return;
    572 	}
    573 #endif /*IPSEC*/
    574 
    575 	if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
    576 		if (in6p && (in6p->in6p_flags & IN6P_CONTROLOPTS
    577 			  || in6p->in6p_socket->so_options & SO_TIMESTAMP)) {
    578 			struct ip6_hdr *ip6 = mtod(n, struct ip6_hdr *);
    579 			ip6_savecontrol(in6p, &opts, ip6, n);
    580 		}
    581 
    582 		m_adj(n, off);
    583 		if (sbappendaddr(&so->so_rcv, src, n, opts) == 0) {
    584 			m_freem(n);
    585 			if (opts)
    586 				m_freem(opts);
    587 			udp6stat.udp6s_fullsock++;
    588 		} else
    589 			sorwakeup(so);
    590 	}
    591 }
    592 #endif
    593 
    594 #ifdef INET
    595 static int
    596 udp4_realinput(src, dst, m, off)
    597 	struct sockaddr_in *src;
    598 	struct sockaddr_in *dst;
    599 	struct mbuf *m;
    600 	int off;	/* offset of udphdr */
    601 {
    602 	u_int16_t *sport, *dport;
    603 	int rcvcnt;
    604 	struct in_addr *src4, *dst4;
    605 	struct inpcb *inp;
    606 
    607 	rcvcnt = 0;
    608 	off += sizeof(struct udphdr);	/* now, offset of payload */
    609 
    610 	if (src->sin_family != AF_INET || dst->sin_family != AF_INET)
    611 		goto bad;
    612 
    613 	src4 = &src->sin_addr;
    614 	sport = &src->sin_port;
    615 	dst4 = &dst->sin_addr;
    616 	dport = &dst->sin_port;
    617 
    618 	if (IN_MULTICAST(dst4->s_addr) ||
    619 	    in_broadcast(*dst4, m->m_pkthdr.rcvif)) {
    620 		struct inpcb *last;
    621 		/*
    622 		 * Deliver a multicast or broadcast datagram to *all* sockets
    623 		 * for which the local and remote addresses and ports match
    624 		 * those of the incoming datagram.  This allows more than
    625 		 * one process to receive multi/broadcasts on the same port.
    626 		 * (This really ought to be done for unicast datagrams as
    627 		 * well, but that would cause problems with existing
    628 		 * applications that open both address-specific sockets and
    629 		 * a wildcard socket listening to the same port -- they would
    630 		 * end up receiving duplicates of every unicast datagram.
    631 		 * Those applications open the multiple sockets to overcome an
    632 		 * inadequacy of the UDP socket interface, but for backwards
    633 		 * compatibility we avoid the problem here rather than
    634 		 * fixing the interface.  Maybe 4.5BSD will remedy this?)
    635 		 */
    636 
    637 		/*
    638 		 * KAME note: traditionally we dropped udpiphdr from mbuf here.
    639 		 * we need udpiphdr for IPsec processing so we do that later.
    640 		 */
    641 		/*
    642 		 * Locate pcb(s) for datagram.
    643 		 */
    644 		CIRCLEQ_FOREACH(inp, &udbtable.inpt_queue, inp_queue) {
    645 			if (inp->inp_lport != *dport)
    646 				continue;
    647 			if (!in_nullhost(inp->inp_laddr)) {
    648 				if (!in_hosteq(inp->inp_laddr, *dst4))
    649 					continue;
    650 			}
    651 			if (!in_nullhost(inp->inp_faddr)) {
    652 				if (!in_hosteq(inp->inp_faddr, *src4) ||
    653 				    inp->inp_fport != *sport)
    654 					continue;
    655 			}
    656 
    657 			last = inp;
    658 			udp4_sendup(m, off, (struct sockaddr *)src,
    659 				inp->inp_socket);
    660 			rcvcnt++;
    661 
    662 			/*
    663 			 * Don't look for additional matches if this one does
    664 			 * not have either the SO_REUSEPORT or SO_REUSEADDR
    665 			 * socket options set.  This heuristic avoids searching
    666 			 * through all pcbs in the common case of a non-shared
    667 			 * port.  It assumes that an application will never
    668 			 * clear these options after setting them.
    669 			 */
    670 			if ((inp->inp_socket->so_options &
    671 			    (SO_REUSEPORT|SO_REUSEADDR)) == 0)
    672 				break;
    673 		}
    674 	} else {
    675 		/*
    676 		 * Locate pcb for datagram.
    677 		 */
    678 		inp = in_pcblookup_connect(&udbtable, *src4, *sport, *dst4, *dport);
    679 		if (inp == 0) {
    680 			++udpstat.udps_pcbhashmiss;
    681 			inp = in_pcblookup_bind(&udbtable, *dst4, *dport);
    682 			if (inp == 0)
    683 				return rcvcnt;
    684 		}
    685 
    686 		udp4_sendup(m, off, (struct sockaddr *)src, inp->inp_socket);
    687 		rcvcnt++;
    688 	}
    689 
    690 bad:
    691 	return rcvcnt;
    692 }
    693 #endif
    694 
    695 #ifdef INET6
    696 static int
    697 udp6_realinput(af, src, dst, m, off)
    698 	int af;		/* af on packet */
    699 	struct sockaddr_in6 *src;
    700 	struct sockaddr_in6 *dst;
    701 	struct mbuf *m;
    702 	int off;	/* offset of udphdr */
    703 {
    704 	u_int16_t sport, dport;
    705 	int rcvcnt;
    706 	struct in6_addr src6, dst6;
    707 	const struct in_addr *dst4;
    708 	struct in6pcb *in6p;
    709 
    710 	rcvcnt = 0;
    711 	off += sizeof(struct udphdr);	/* now, offset of payload */
    712 
    713 	if (af != AF_INET && af != AF_INET6)
    714 		goto bad;
    715 	if (src->sin6_family != AF_INET6 || dst->sin6_family != AF_INET6)
    716 		goto bad;
    717 
    718 	in6_embedscope(&src6, src, NULL, NULL);
    719 	sport = src->sin6_port;
    720 	in6_embedscope(&dst6, dst, NULL, NULL);
    721 	dport = dst->sin6_port;
    722 	dst4 = (struct in_addr *)&dst->sin6_addr.s6_addr[12];
    723 
    724 	if (IN6_IS_ADDR_MULTICAST(&dst6) ||
    725 	    (af == AF_INET && IN_MULTICAST(dst4->s_addr))) {
    726 		struct in6pcb *last;
    727 		/*
    728 		 * Deliver a multicast or broadcast datagram to *all* sockets
    729 		 * for which the local and remote addresses and ports match
    730 		 * those of the incoming datagram.  This allows more than
    731 		 * one process to receive multi/broadcasts on the same port.
    732 		 * (This really ought to be done for unicast datagrams as
    733 		 * well, but that would cause problems with existing
    734 		 * applications that open both address-specific sockets and
    735 		 * a wildcard socket listening to the same port -- they would
    736 		 * end up receiving duplicates of every unicast datagram.
    737 		 * Those applications open the multiple sockets to overcome an
    738 		 * inadequacy of the UDP socket interface, but for backwards
    739 		 * compatibility we avoid the problem here rather than
    740 		 * fixing the interface.  Maybe 4.5BSD will remedy this?)
    741 		 */
    742 
    743 		/*
    744 		 * KAME note: traditionally we dropped udpiphdr from mbuf here.
    745 		 * we need udpiphdr for IPsec processing so we do that later.
    746 		 */
    747 		/*
    748 		 * Locate pcb(s) for datagram.
    749 		 */
    750 		for (in6p = udb6.in6p_next; in6p != &udb6;
    751 		     in6p = in6p->in6p_next) {
    752 			if (in6p->in6p_lport != dport)
    753 				continue;
    754 			if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
    755 				if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &dst6))
    756 					continue;
    757 			} else {
    758 				if (IN6_IS_ADDR_V4MAPPED(&dst6) &&
    759 				    (in6p->in6p_flags & IN6P_IPV6_V6ONLY))
    760 					continue;
    761 			}
    762 			if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
    763 				if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,
    764 				    &src6) || in6p->in6p_fport != sport)
    765 					continue;
    766 			} else {
    767 				if (IN6_IS_ADDR_V4MAPPED(&src6) &&
    768 				    (in6p->in6p_flags & IN6P_IPV6_V6ONLY))
    769 					continue;
    770 			}
    771 
    772 			last = in6p;
    773 			udp6_sendup(m, off, (struct sockaddr *)src,
    774 				in6p->in6p_socket);
    775 			rcvcnt++;
    776 
    777 			/*
    778 			 * Don't look for additional matches if this one does
    779 			 * not have either the SO_REUSEPORT or SO_REUSEADDR
    780 			 * socket options set.  This heuristic avoids searching
    781 			 * through all pcbs in the common case of a non-shared
    782 			 * port.  It assumes that an application will never
    783 			 * clear these options after setting them.
    784 			 */
    785 			if ((in6p->in6p_socket->so_options &
    786 			    (SO_REUSEPORT|SO_REUSEADDR)) == 0)
    787 				break;
    788 		}
    789 	} else {
    790 		/*
    791 		 * Locate pcb for datagram.
    792 		 */
    793 		in6p = in6_pcblookup_connect(&udb6, &src6, sport,
    794 		    &dst6, dport, 0);
    795 		if (in6p == 0) {
    796 			++udpstat.udps_pcbhashmiss;
    797 			in6p = in6_pcblookup_bind(&udb6, &dst6, dport, 0);
    798 			if (in6p == 0)
    799 				return rcvcnt;
    800 		}
    801 
    802 		udp6_sendup(m, off, (struct sockaddr *)src, in6p->in6p_socket);
    803 		rcvcnt++;
    804 	}
    805 
    806 bad:
    807 	return rcvcnt;
    808 }
    809 #endif
    810 
    811 #ifdef INET
    812 /*
    813  * Notify a udp user of an asynchronous error;
    814  * just wake up so that he can collect error status.
    815  */
    816 static void
    817 udp_notify(inp, errno)
    818 	struct inpcb *inp;
    819 	int errno;
    820 {
    821 
    822 	inp->inp_socket->so_error = errno;
    823 	sorwakeup(inp->inp_socket);
    824 	sowwakeup(inp->inp_socket);
    825 }
    826 
    827 void *
    828 udp_ctlinput(cmd, sa, v)
    829 	int cmd;
    830 	struct sockaddr *sa;
    831 	void *v;
    832 {
    833 	struct ip *ip = v;
    834 	struct udphdr *uh;
    835 	void (*notify) __P((struct inpcb *, int)) = udp_notify;
    836 	int errno;
    837 
    838 	if (sa->sa_family != AF_INET
    839 	 || sa->sa_len != sizeof(struct sockaddr_in))
    840 		return NULL;
    841 	if ((unsigned)cmd >= PRC_NCMDS)
    842 		return NULL;
    843 	errno = inetctlerrmap[cmd];
    844 	if (PRC_IS_REDIRECT(cmd))
    845 		notify = in_rtchange, ip = 0;
    846 	else if (cmd == PRC_HOSTDEAD)
    847 		ip = 0;
    848 	else if (errno == 0)
    849 		return NULL;
    850 	if (ip) {
    851 		uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
    852 		in_pcbnotify(&udbtable, satosin(sa)->sin_addr, uh->uh_dport,
    853 		    ip->ip_src, uh->uh_sport, errno, notify);
    854 
    855 		/* XXX mapped address case */
    856 	} else
    857 		in_pcbnotifyall(&udbtable, satosin(sa)->sin_addr, errno,
    858 		    notify);
    859 	return NULL;
    860 }
    861 
    862 int
    863 #if __STDC__
    864 udp_output(struct mbuf *m, ...)
    865 #else
    866 udp_output(m, va_alist)
    867 	struct mbuf *m;
    868 	va_dcl
    869 #endif
    870 {
    871 	struct inpcb *inp;
    872 	struct udpiphdr *ui;
    873 	int len = m->m_pkthdr.len;
    874 	int error = 0;
    875 	va_list ap;
    876 
    877 	va_start(ap, m);
    878 	inp = va_arg(ap, struct inpcb *);
    879 	va_end(ap);
    880 
    881 	/*
    882 	 * Calculate data length and get a mbuf
    883 	 * for UDP and IP headers.
    884 	 */
    885 	M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT);
    886 	if (m == 0) {
    887 		error = ENOBUFS;
    888 		goto release;
    889 	}
    890 
    891 	/*
    892 	 * Compute the packet length of the IP header, and
    893 	 * punt if the length looks bogus.
    894 	 */
    895 	if ((len + sizeof(struct udpiphdr)) > IP_MAXPACKET) {
    896 		error = EMSGSIZE;
    897 		goto release;
    898 	}
    899 
    900 	/*
    901 	 * Fill in mbuf with extended UDP header
    902 	 * and addresses and length put into network format.
    903 	 */
    904 	ui = mtod(m, struct udpiphdr *);
    905 	ui->ui_pr = IPPROTO_UDP;
    906 	ui->ui_src = inp->inp_laddr;
    907 	ui->ui_dst = inp->inp_faddr;
    908 	ui->ui_sport = inp->inp_lport;
    909 	ui->ui_dport = inp->inp_fport;
    910 	ui->ui_ulen = htons((u_int16_t)len + sizeof(struct udphdr));
    911 
    912 	/*
    913 	 * Set up checksum and output datagram.
    914 	 */
    915 	if (udpcksum) {
    916 		/*
    917 		 * XXX Cache pseudo-header checksum part for
    918 		 * XXX "connected" UDP sockets.
    919 		 */
    920 		ui->ui_sum = in_cksum_phdr(ui->ui_src.s_addr,
    921 		    ui->ui_dst.s_addr, htons((u_int16_t)len +
    922 		    sizeof(struct udphdr) + IPPROTO_UDP));
    923 		m->m_pkthdr.csum_flags = M_CSUM_UDPv4;
    924 		m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
    925 	} else
    926 		ui->ui_sum = 0;
    927 	((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
    928 	((struct ip *)ui)->ip_ttl = inp->inp_ip.ip_ttl;	/* XXX */
    929 	((struct ip *)ui)->ip_tos = inp->inp_ip.ip_tos;	/* XXX */
    930 	udpstat.udps_opackets++;
    931 
    932 #ifdef IPSEC
    933 	if (ipsec_setsocket(m, inp->inp_socket) != 0) {
    934 		error = ENOBUFS;
    935 		goto release;
    936 	}
    937 #endif /*IPSEC*/
    938 
    939 	return (ip_output(m, inp->inp_options, &inp->inp_route,
    940 	    inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST),
    941 	    inp->inp_moptions));
    942 
    943 release:
    944 	m_freem(m);
    945 	return (error);
    946 }
    947 
    948 int	udp_sendspace = 9216;		/* really max datagram size */
    949 int	udp_recvspace = 40 * (1024 + sizeof(struct sockaddr_in));
    950 					/* 40 1K datagrams */
    951 
    952 /*ARGSUSED*/
    953 int
    954 udp_usrreq(so, req, m, nam, control, p)
    955 	struct socket *so;
    956 	int req;
    957 	struct mbuf *m, *nam, *control;
    958 	struct proc *p;
    959 {
    960 	struct inpcb *inp;
    961 	int s;
    962 	int error = 0;
    963 
    964 	if (req == PRU_CONTROL)
    965 		return (in_control(so, (long)m, (caddr_t)nam,
    966 		    (struct ifnet *)control, p));
    967 
    968 	if (req == PRU_PURGEIF) {
    969 		in_pcbpurgeif0(&udbtable, (struct ifnet *)control);
    970 		in_purgeif((struct ifnet *)control);
    971 		in_pcbpurgeif(&udbtable, (struct ifnet *)control);
    972 		return (0);
    973 	}
    974 
    975 	s = splsoftnet();
    976 	inp = sotoinpcb(so);
    977 #ifdef DIAGNOSTIC
    978 	if (req != PRU_SEND && req != PRU_SENDOOB && control)
    979 		panic("udp_usrreq: unexpected control mbuf");
    980 #endif
    981 	if (inp == 0 && req != PRU_ATTACH) {
    982 		error = EINVAL;
    983 		goto release;
    984 	}
    985 
    986 	/*
    987 	 * Note: need to block udp_input while changing
    988 	 * the udp pcb queue and/or pcb addresses.
    989 	 */
    990 	switch (req) {
    991 
    992 	case PRU_ATTACH:
    993 		if (inp != 0) {
    994 			error = EISCONN;
    995 			break;
    996 		}
    997 		if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
    998 			error = soreserve(so, udp_sendspace, udp_recvspace);
    999 			if (error)
   1000 				break;
   1001 		}
   1002 		error = in_pcballoc(so, &udbtable);
   1003 		if (error)
   1004 			break;
   1005 		inp = sotoinpcb(so);
   1006 		inp->inp_ip.ip_ttl = ip_defttl;
   1007 		break;
   1008 
   1009 	case PRU_DETACH:
   1010 		in_pcbdetach(inp);
   1011 		break;
   1012 
   1013 	case PRU_BIND:
   1014 		error = in_pcbbind(inp, nam, p);
   1015 		break;
   1016 
   1017 	case PRU_LISTEN:
   1018 		error = EOPNOTSUPP;
   1019 		break;
   1020 
   1021 	case PRU_CONNECT:
   1022 		error = in_pcbconnect(inp, nam);
   1023 		if (error)
   1024 			break;
   1025 		soisconnected(so);
   1026 		break;
   1027 
   1028 	case PRU_CONNECT2:
   1029 		error = EOPNOTSUPP;
   1030 		break;
   1031 
   1032 	case PRU_DISCONNECT:
   1033 		/*soisdisconnected(so);*/
   1034 		so->so_state &= ~SS_ISCONNECTED;	/* XXX */
   1035 		in_pcbdisconnect(inp);
   1036 		inp->inp_laddr = zeroin_addr;		/* XXX */
   1037 		in_pcbstate(inp, INP_BOUND);		/* XXX */
   1038 		break;
   1039 
   1040 	case PRU_SHUTDOWN:
   1041 		socantsendmore(so);
   1042 		break;
   1043 
   1044 	case PRU_RCVD:
   1045 		error = EOPNOTSUPP;
   1046 		break;
   1047 
   1048 	case PRU_SEND:
   1049 		if (control && control->m_len) {
   1050 			m_freem(control);
   1051 			m_freem(m);
   1052 			error = EINVAL;
   1053 			break;
   1054 		}
   1055 	{
   1056 		struct in_addr laddr;			/* XXX */
   1057 
   1058 		if (nam) {
   1059 			laddr = inp->inp_laddr;		/* XXX */
   1060 			if ((so->so_state & SS_ISCONNECTED) != 0) {
   1061 				error = EISCONN;
   1062 				goto die;
   1063 			}
   1064 			error = in_pcbconnect(inp, nam);
   1065 			if (error) {
   1066 			die:
   1067 				m_freem(m);
   1068 				break;
   1069 			}
   1070 		} else {
   1071 			if ((so->so_state & SS_ISCONNECTED) == 0) {
   1072 				error = ENOTCONN;
   1073 				goto die;
   1074 			}
   1075 		}
   1076 		error = udp_output(m, inp);
   1077 		if (nam) {
   1078 			in_pcbdisconnect(inp);
   1079 			inp->inp_laddr = laddr;		/* XXX */
   1080 			in_pcbstate(inp, INP_BOUND);	/* XXX */
   1081 		}
   1082 	}
   1083 		break;
   1084 
   1085 	case PRU_SENSE:
   1086 		/*
   1087 		 * stat: don't bother with a blocksize.
   1088 		 */
   1089 		splx(s);
   1090 		return (0);
   1091 
   1092 	case PRU_RCVOOB:
   1093 		error =  EOPNOTSUPP;
   1094 		break;
   1095 
   1096 	case PRU_SENDOOB:
   1097 		m_freem(control);
   1098 		m_freem(m);
   1099 		error =  EOPNOTSUPP;
   1100 		break;
   1101 
   1102 	case PRU_SOCKADDR:
   1103 		in_setsockaddr(inp, nam);
   1104 		break;
   1105 
   1106 	case PRU_PEERADDR:
   1107 		in_setpeeraddr(inp, nam);
   1108 		break;
   1109 
   1110 	default:
   1111 		panic("udp_usrreq");
   1112 	}
   1113 
   1114 release:
   1115 	splx(s);
   1116 	return (error);
   1117 }
   1118 
   1119 /*
   1120  * Sysctl for udp variables.
   1121  */
   1122 int
   1123 udp_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
   1124 	int *name;
   1125 	u_int namelen;
   1126 	void *oldp;
   1127 	size_t *oldlenp;
   1128 	void *newp;
   1129 	size_t newlen;
   1130 {
   1131 	/* All sysctl names at this level are terminal. */
   1132 	if (namelen != 1)
   1133 		return (ENOTDIR);
   1134 
   1135 	switch (name[0]) {
   1136 	case UDPCTL_CHECKSUM:
   1137 		return (sysctl_int(oldp, oldlenp, newp, newlen, &udpcksum));
   1138 	case UDPCTL_SENDSPACE:
   1139 		return (sysctl_int(oldp, oldlenp, newp, newlen,
   1140 		    &udp_sendspace));
   1141 	case UDPCTL_RECVSPACE:
   1142 		return (sysctl_int(oldp, oldlenp, newp, newlen,
   1143 		    &udp_recvspace));
   1144 	default:
   1145 		return (ENOPROTOOPT);
   1146 	}
   1147 	/* NOTREACHED */
   1148 }
   1149 #endif
   1150