Home | History | Annotate | Line # | Download | only in netinet
udp_usrreq.c revision 1.56
      1 /*	$NetBSD: udp_usrreq.c,v 1.56 2000/01/06 15:46:08 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 "opt_ipsec.h"
     68 
     69 #include "ipkdb.h"
     70 
     71 #include <sys/param.h>
     72 #include <sys/malloc.h>
     73 #include <sys/mbuf.h>
     74 #include <sys/protosw.h>
     75 #include <sys/socket.h>
     76 #include <sys/socketvar.h>
     77 #include <sys/errno.h>
     78 #include <sys/stat.h>
     79 #include <sys/systm.h>
     80 #include <sys/proc.h>
     81 #include <sys/domain.h>
     82 
     83 #include <vm/vm.h>
     84 #include <sys/sysctl.h>
     85 
     86 #include <net/if.h>
     87 #include <net/route.h>
     88 
     89 #include <netinet/in.h>
     90 #include <netinet/in_systm.h>
     91 #include <netinet/in_var.h>
     92 #include <netinet/ip.h>
     93 #include <netinet/in_pcb.h>
     94 #include <netinet/ip_var.h>
     95 #include <netinet/ip_icmp.h>
     96 #include <netinet/udp.h>
     97 #include <netinet/udp_var.h>
     98 
     99 #ifdef INET6
    100 #include <netinet/ip6.h>
    101 #include <netinet/icmp6.h>
    102 #include <netinet6/ip6_var.h>
    103 #include <netinet6/in6_pcb.h>
    104 #include <netinet6/udp6_var.h>
    105 #endif
    106 
    107 #ifdef PULLDOWN_TEST
    108 #ifndef INET6
    109 /* always need ip6.h for IP6_EXTHDR_GET */
    110 #include <netinet/ip6.h>
    111 #endif
    112 #endif
    113 
    114 #include <machine/stdarg.h>
    115 
    116 #ifdef IPSEC
    117 #include <netinet6/ipsec.h>
    118 #include <netkey/key.h>
    119 #include <netkey/key_debug.h>
    120 #endif /*IPSEC*/
    121 
    122 /*
    123  * UDP protocol implementation.
    124  * Per RFC 768, August, 1980.
    125  */
    126 #ifndef	COMPAT_42
    127 int	udpcksum = 1;
    128 #else
    129 int	udpcksum = 0;		/* XXX */
    130 #endif
    131 
    132 static void udp4_sendup __P((struct mbuf *, int, struct sockaddr *,
    133 	struct socket *));
    134 static int udp4_realinput __P((struct sockaddr_in *, struct sockaddr_in *,
    135 	struct mbuf *, int));
    136 #ifdef INET6
    137 static void udp6_sendup __P((struct mbuf *, int, struct sockaddr *,
    138 	struct socket *));
    139 static	int in6_mcmatch __P((struct in6pcb *, struct in6_addr *,
    140 	struct ifnet *));
    141 static int udp6_realinput __P((int, struct sockaddr_in6 *,
    142 	struct sockaddr_in6 *, struct mbuf *, int));
    143 #endif
    144 static	void udp_notify __P((struct inpcb *, int));
    145 
    146 #ifndef UDBHASHSIZE
    147 #define	UDBHASHSIZE	128
    148 #endif
    149 int	udbhashsize = UDBHASHSIZE;
    150 
    151 void
    152 udp_init()
    153 {
    154 
    155 	in_pcbinit(&udbtable, udbhashsize, udbhashsize);
    156 }
    157 
    158 #ifndef UDP6
    159 void
    160 #if __STDC__
    161 udp_input(struct mbuf *m, ...)
    162 #else
    163 udp_input(m, va_alist)
    164 	struct mbuf *m;
    165 	va_dcl
    166 #endif
    167 {
    168 	va_list ap;
    169 	struct sockaddr_in src, dst;
    170 	struct ip *ip;
    171 	struct udphdr *uh;
    172 	int iphlen, proto;
    173 	int len;
    174 	int n;
    175 
    176 	va_start(ap, m);
    177 	iphlen = va_arg(ap, int);
    178 	proto = va_arg(ap, int);
    179 	va_end(ap);
    180 
    181 	udpstat.udps_ipackets++;
    182 
    183 #ifndef PULLDOWN_TEST
    184 	/*
    185 	 * Strip IP options, if any; should skip this,
    186 	 * make available to user, and use on returned packets,
    187 	 * but we don't yet have a way to check the checksum
    188 	 * with options still present.
    189 	 */
    190 	if (iphlen > sizeof (struct ip)) {
    191 		ip_stripoptions(m, (struct mbuf *)0);
    192 		iphlen = sizeof(struct ip);
    193 	}
    194 #else
    195 	/*
    196 	 * we may enable the above code if we save and pass IPv4 options
    197 	 * to the userland.
    198 	 */
    199 #endif
    200 
    201 	/*
    202 	 * Get IP and UDP header together in first mbuf.
    203 	 */
    204 	ip = mtod(m, struct ip *);
    205 #ifndef PULLDOWN_TEST
    206 	if (m->m_len < iphlen + sizeof(struct udphdr)) {
    207 		if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) {
    208 			udpstat.udps_hdrops++;
    209 			return;
    210 		}
    211 		ip = mtod(m, struct ip *);
    212 	}
    213 	uh = (struct udphdr *)((caddr_t)ip + iphlen);
    214 #else
    215 	IP6_EXTHDR_GET(uh, struct udphdr *, m, iphlen, sizeof(struct udphdr));
    216 	if (uh == NULL) {
    217 		udpstat.udps_hdrops++;
    218 		return;
    219 	}
    220 #endif
    221 
    222 	/*
    223 	 * Make mbuf data length reflect UDP length.
    224 	 * If not enough data to reflect UDP length, drop.
    225 	 */
    226 	len = ntohs((u_int16_t)uh->uh_ulen);
    227 	if (ip->ip_len != iphlen + len) {
    228 		if (ip->ip_len < iphlen + len) {
    229 			udpstat.udps_badlen++;
    230 			goto bad;
    231 		}
    232 		m_adj(m, iphlen + len - ip->ip_len);
    233 	}
    234 
    235 	/*
    236 	 * Checksum extended UDP header and data.
    237 	 */
    238 	if (uh->uh_sum) {
    239 #ifndef PULLDOWN_TEST
    240 		struct ip save_ip;
    241 
    242 		/*
    243 		 * Save a copy of the IP header in case we want restore it
    244 		 * for sending an ICMP error message in response.
    245 		 */
    246 		save_ip = *ip;
    247 
    248 		bzero(((struct ipovly *)ip)->ih_x1,
    249 		    sizeof ((struct ipovly *)ip)->ih_x1);
    250 		((struct ipovly *)ip)->ih_len = uh->uh_ulen;
    251 		if (in_cksum(m, len + sizeof (struct ip)) != 0) {
    252 			udpstat.udps_badsum++;
    253 			m_freem(m);
    254 			return;
    255 		}
    256 
    257 		*ip = save_ip;
    258 #else
    259 		if (in4_cksum(m, IPPROTO_UDP, iphlen, len) != 0) {
    260 			udpstat.udps_badsum++;
    261 			m_freem(m);
    262 			return;
    263 		}
    264 #endif
    265 	}
    266 
    267 	/* construct source and dst sockaddrs. */
    268 	bzero(&src, sizeof(src));
    269 	src.sin_family = AF_INET;
    270 	src.sin_len = sizeof(struct sockaddr_in);
    271 	bcopy(&ip->ip_src, &src.sin_addr, sizeof(src.sin_addr));
    272 	src.sin_port = uh->uh_sport;
    273 	bzero(&dst, sizeof(dst));
    274 	dst.sin_family = AF_INET;
    275 	dst.sin_len = sizeof(struct sockaddr_in);
    276 	bcopy(&ip->ip_dst, &dst.sin_addr, sizeof(dst.sin_addr));
    277 	dst.sin_port = uh->uh_dport;
    278 
    279 	n = udp4_realinput(&src, &dst, m, iphlen);
    280 #ifdef INET6
    281 	if (IN_MULTICAST(ip->ip_dst.s_addr) || n == 0) {
    282 		struct sockaddr_in6 src6, dst6;
    283 
    284 		bzero(&src6, sizeof(src6));
    285 		src6.sin6_family = AF_INET6;
    286 		src6.sin6_len = sizeof(struct sockaddr_in6);
    287 		src6.sin6_addr.s6_addr[10] = src6.sin6_addr.s6_addr[11] = 0xff;
    288 		bcopy(&ip->ip_src, &src6.sin6_addr.s6_addr[12],
    289 			sizeof(ip->ip_src));
    290 		src6.sin6_port = uh->uh_sport;
    291 		bzero(&dst6, sizeof(dst6));
    292 		dst6.sin6_family = AF_INET6;
    293 		dst6.sin6_len = sizeof(struct sockaddr_in6);
    294 		dst6.sin6_addr.s6_addr[10] = dst6.sin6_addr.s6_addr[11] = 0xff;
    295 		bcopy(&ip->ip_dst, &dst6.sin6_addr.s6_addr[12],
    296 			sizeof(ip->ip_dst));
    297 		dst6.sin6_port = uh->uh_dport;
    298 
    299 		n += udp6_realinput(AF_INET, &src6, &dst6, m, iphlen);
    300 	}
    301 #endif
    302 
    303 	if (n == 0) {
    304 		udpstat.udps_noport++;
    305 		if (m->m_flags & (M_BCAST | M_MCAST)) {
    306 			udpstat.udps_noportbcast++;
    307 			goto bad;
    308 		}
    309 #if NIPKDB > 0
    310 		if (checkipkdb(&ip->ip_src, uh->uh_sport, uh->uh_dport,
    311 				m, iphlen + sizeof(struct udphdr),
    312 				m->m_pkthdr.len - iphlen - sizeof(struct udphdr))) {
    313 			/*
    314 			 * It was a debugger connect packet,
    315 			 * just drop it now
    316 			 */
    317 			goto bad;
    318 		}
    319 #endif
    320 		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
    321 		m = NULL;
    322 	}
    323 
    324 bad:
    325 	if (m)
    326 		m_freem(m);
    327 }
    328 
    329 #ifdef INET6
    330 int
    331 udp6_input(mp, offp, proto)
    332 	struct mbuf **mp;
    333 	int *offp, proto;
    334 {
    335 	struct mbuf *m = *mp;
    336 	int off = *offp;
    337 	struct sockaddr_in6 src, dst;
    338 	struct ip6_hdr *ip6;
    339 	struct udphdr *uh;
    340 	u_int32_t plen, ulen;
    341 
    342 #if defined(NFAITH) && 0 < NFAITH
    343 	if (m->m_pkthdr.rcvif) {
    344 		if (m->m_pkthdr.rcvif->if_type == IFT_FAITH) {
    345 			/* send icmp6 host unreach? */
    346 			m_freem(m);
    347 			return IPPROTO_DONE;
    348 		}
    349 	}
    350 #endif
    351 
    352 	udp6stat.udp6s_ipackets++;
    353 
    354 #ifndef PULLDOWN_TEST
    355 	IP6_EXTHDR_CHECK(m, off, sizeof(struct udphdr), IPPROTO_DONE);
    356 #endif
    357 
    358 	ip6 = mtod(m, struct ip6_hdr *);
    359 	/* check for jumbogram is done in ip6_input.  we can trust pkthdr.len */
    360 	plen = m->m_pkthdr.len - off;
    361 #ifndef PULLDOWN_TEST
    362 	uh = (struct udphdr *)((caddr_t)ip6 + off);
    363 #else
    364 	IP6_EXTHDR_GET(uh, struct udphdr *, m, off, sizeof(struct udphdr));
    365 	if (uh == NULL) {
    366 		ip6stat.ip6s_tooshort++;
    367 		return IPPROTO_DONE;
    368 	}
    369 #endif
    370 	ulen = ntohs((u_short)uh->uh_ulen);
    371 	if (ulen == 0 && plen > 0xffff)
    372 		ulen = plen;
    373 
    374 	if (plen != ulen) {
    375 		udp6stat.udp6s_badlen++;
    376 		goto bad;
    377 	}
    378 
    379 	/* Be proactive about malicious use of IPv4 mapped address */
    380 	if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
    381 	    IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
    382 		/* XXX stat */
    383 		goto bad;
    384 	}
    385 
    386 	/*
    387 	 * Checksum extended UDP header and data.
    388 	 */
    389 	if (uh->uh_sum == 0)
    390 		udp6stat.udp6s_nosum++;
    391 	else if (in6_cksum(m, IPPROTO_UDP, off, ulen) != 0) {
    392 		udp6stat.udp6s_badsum++;
    393 		goto bad;
    394 	}
    395 
    396 	/*
    397 	 * Construct source and dst sockaddrs.
    398 	 * Note that ifindex (s6_addr16[1]) is already filled.
    399 	 */
    400 	bzero(&src, sizeof(src));
    401 	src.sin6_family = AF_INET6;
    402 	src.sin6_len = sizeof(struct sockaddr_in6);
    403 	bcopy(&ip6->ip6_src, &src.sin6_addr, sizeof(src.sin6_addr));
    404 	if (IN6_IS_SCOPE_LINKLOCAL(&src.sin6_addr))
    405 		src.sin6_addr.s6_addr16[1] = 0;
    406 	if (m->m_pkthdr.rcvif) {
    407 		if (IN6_IS_SCOPE_LINKLOCAL(&src.sin6_addr))
    408 			src.sin6_scope_id = m->m_pkthdr.rcvif->if_index;
    409 		else
    410 			src.sin6_scope_id = 0;
    411 	}
    412 	src.sin6_port = uh->uh_sport;
    413 	bzero(&dst, sizeof(dst));
    414 	dst.sin6_family = AF_INET6;
    415 	dst.sin6_len = sizeof(struct sockaddr_in6);
    416 	bcopy(&ip6->ip6_dst, &dst.sin6_addr, sizeof(dst.sin6_addr));
    417 	if (IN6_IS_SCOPE_LINKLOCAL(&dst.sin6_addr))
    418 		dst.sin6_addr.s6_addr16[1] = 0;
    419 	if (m->m_pkthdr.rcvif) {
    420 		if (IN6_IS_SCOPE_LINKLOCAL(&dst.sin6_addr))
    421 			dst.sin6_scope_id = m->m_pkthdr.rcvif->if_index;
    422 		else
    423 			dst.sin6_scope_id = 0;
    424 	}
    425 	dst.sin6_port = uh->uh_dport;
    426 
    427 	if (udp6_realinput(AF_INET6, &src, &dst, m, off) == 0) {
    428 		udp6stat.udp6s_noport++;
    429 		if (m->m_flags & M_MCAST) {
    430 			udp6stat.udp6s_noportmcast++;
    431 			goto bad;
    432 		}
    433 		icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
    434 		m = NULL;
    435 	}
    436 
    437 bad:
    438 	if (m)
    439 		m_freem(m);
    440 	return IPPROTO_DONE;
    441 }
    442 #endif
    443 
    444 static void
    445 udp4_sendup(m, off, src, so)
    446 	struct mbuf *m;
    447 	int off;	/* offset of data portion */
    448 	struct sockaddr *src;
    449 	struct socket *so;
    450 {
    451 	struct mbuf *opts = NULL;
    452 	struct mbuf *n;
    453 	struct inpcb *inp = NULL;
    454 #ifdef INET6
    455 	struct in6pcb *in6p = NULL;
    456 #endif
    457 
    458 	if (!so)
    459 		return;
    460 	switch (so->so_proto->pr_domain->dom_family) {
    461 	case AF_INET:
    462 		inp = sotoinpcb(so);
    463 		break;
    464 #ifdef INET6
    465 	case AF_INET6:
    466 		in6p = sotoin6pcb(so);
    467 		break;
    468 #endif
    469 	default:
    470 		return;
    471 	}
    472 
    473 #ifdef IPSEC
    474 	/* check AH/ESP integrity. */
    475 	if (so != NULL && ipsec4_in_reject_so(m, so)) {
    476 		ipsecstat.in_polvio++;
    477 		return;
    478 	}
    479 #endif /*IPSEC*/
    480 
    481 	if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
    482 		if (inp && (inp->inp_flags & INP_CONTROLOPTS
    483 			 || so->so_options & SO_TIMESTAMP)) {
    484 			struct ip *ip = mtod(n, struct ip *);
    485 			ip_savecontrol(inp, &opts, ip, n);
    486 		}
    487 
    488 		m_adj(n, off);
    489 		if (sbappendaddr(&so->so_rcv, src, n,
    490 				opts) == 0) {
    491 			m_freem(n);
    492 			if (opts)
    493 				m_freem(opts);
    494 		} else
    495 			sorwakeup(so);
    496 	}
    497 }
    498 
    499 #ifdef INET6
    500 static void
    501 udp6_sendup(m, off, src, so)
    502 	struct mbuf *m;
    503 	int off;	/* offset of data portion */
    504 	struct sockaddr *src;
    505 	struct socket *so;
    506 {
    507 	struct mbuf *opts = NULL;
    508 	struct mbuf *n;
    509 	struct in6pcb *in6p = NULL;
    510 
    511 	if (!so)
    512 		return;
    513 	if (so->so_proto->pr_domain->dom_family != AF_INET6)
    514 		return;
    515 	in6p = sotoin6pcb(so);
    516 
    517 #ifdef IPSEC
    518 	/* check AH/ESP integrity. */
    519 	if (so != NULL && ipsec6_in_reject_so(m, so)) {
    520 		ipsec6stat.in_polvio++;
    521 		return;
    522 	}
    523 #endif /*IPSEC*/
    524 
    525 	if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
    526 		if (in6p && (in6p->in6p_flags & IN6P_CONTROLOPTS
    527 			  || in6p->in6p_socket->so_options & SO_TIMESTAMP)) {
    528 			struct ip6_hdr *ip6 = mtod(n, struct ip6_hdr *);
    529 			ip6_savecontrol(in6p, &opts, ip6, n);
    530 		}
    531 
    532 		m_adj(n, off);
    533 		if (sbappendaddr(&so->so_rcv, src, n, opts) == 0) {
    534 			m_freem(n);
    535 			if (opts)
    536 				m_freem(opts);
    537 			udp6stat.udp6s_fullsock++;
    538 		} else
    539 			sorwakeup(so);
    540 	}
    541 }
    542 #endif
    543 
    544 static int
    545 udp4_realinput(src, dst, m, off)
    546 	struct sockaddr_in *src;
    547 	struct sockaddr_in *dst;
    548 	struct mbuf *m;
    549 	int off;	/* offset of udphdr */
    550 {
    551 	u_int16_t *sport, *dport;
    552 	int rcvcnt;
    553 	struct in_addr *src4, *dst4;
    554 	struct inpcb *inp;
    555 
    556 	rcvcnt = 0;
    557 	off += sizeof(struct udphdr);	/* now, offset of payload */
    558 
    559 	if (src->sin_family != AF_INET || dst->sin_family != AF_INET)
    560 		goto bad;
    561 
    562 	src4 = &src->sin_addr;
    563 	sport = &src->sin_port;
    564 	dst4 = &dst->sin_addr;
    565 	dport = &dst->sin_port;
    566 
    567 	if (IN_MULTICAST(src4->s_addr) ||
    568 	    in_broadcast(*dst4, m->m_pkthdr.rcvif)) {
    569 		struct inpcb *last;
    570 		/*
    571 		 * Deliver a multicast or broadcast datagram to *all* sockets
    572 		 * for which the local and remote addresses and ports match
    573 		 * those of the incoming datagram.  This allows more than
    574 		 * one process to receive multi/broadcasts on the same port.
    575 		 * (This really ought to be done for unicast datagrams as
    576 		 * well, but that would cause problems with existing
    577 		 * applications that open both address-specific sockets and
    578 		 * a wildcard socket listening to the same port -- they would
    579 		 * end up receiving duplicates of every unicast datagram.
    580 		 * Those applications open the multiple sockets to overcome an
    581 		 * inadequacy of the UDP socket interface, but for backwards
    582 		 * compatibility we avoid the problem here rather than
    583 		 * fixing the interface.  Maybe 4.5BSD will remedy this?)
    584 		 */
    585 
    586 		/*
    587 		 * KAME note: usually we drop udpiphdr from mbuf here.
    588 		 * we need udpiphdr for iPsec processing so we do that later.
    589 		 */
    590 		/*
    591 		 * Locate pcb(s) for datagram.
    592 		 */
    593 		for (inp = udbtable.inpt_queue.cqh_first;
    594 		    inp != (struct inpcb *)&udbtable.inpt_queue;
    595 		    inp = inp->inp_queue.cqe_next) {
    596 			if (inp->inp_lport != *dport)
    597 				continue;
    598 			if (!in_nullhost(inp->inp_laddr)) {
    599 				if (!in_hosteq(inp->inp_laddr, *dst4))
    600 					continue;
    601 			}
    602 			if (!in_nullhost(inp->inp_faddr)) {
    603 				if (!in_hosteq(inp->inp_faddr, *src4) ||
    604 				    inp->inp_fport != *sport)
    605 					continue;
    606 			}
    607 
    608 			last = inp;
    609 			udp4_sendup(m, off, (struct sockaddr *)src,
    610 				inp->inp_socket);
    611 			rcvcnt++;
    612 
    613 			/*
    614 			 * Don't look for additional matches if this one does
    615 			 * not have either the SO_REUSEPORT or SO_REUSEADDR
    616 			 * socket options set.  This heuristic avoids searching
    617 			 * through all pcbs in the common case of a non-shared
    618 			 * port.  It assumes that an application will never
    619 			 * clear these options after setting them.
    620 			 */
    621 			if ((inp->inp_socket->so_options &
    622 			    (SO_REUSEPORT|SO_REUSEADDR)) == 0)
    623 				break;
    624 		}
    625 
    626 #if 0
    627 		if (last == NULL) {
    628 			/*
    629 			 * No matching pcb found; discard datagram.
    630 			 * (No need to send an ICMP Port Unreachable
    631 			 * for a broadcast or multicast datgram.)
    632 			 */
    633 			udpstat.udps_noport++;
    634 			udpstat.udps_noportbcast++;
    635 			goto bad;
    636 		}
    637 #endif
    638 	} else {
    639 		/*
    640 		 * Locate pcb for datagram.
    641 		 */
    642 		inp = in_pcblookup_connect(&udbtable, *src4, *sport, *dst4, *dport);
    643 		if (inp == 0) {
    644 			++udpstat.udps_pcbhashmiss;
    645 			inp = in_pcblookup_bind(&udbtable, *dst4, *dport);
    646 			if (inp == 0) {
    647 #if 0
    648 				struct mbuf *n;
    649 
    650 				udpstat.udps_noport++;
    651 				if (m->m_flags & (M_BCAST | M_MCAST)) {
    652 					udpstat.udps_noportbcast++;
    653 					goto bad;
    654 				}
    655 #if NIPKDB > 0
    656 				if (checkipkdb(src4, *sport, *dport, m, off,
    657 					       m->m_pkthdr.len - off)) {
    658 					/*
    659 					 * It was a debugger connect packet,
    660 					 * just drop it now
    661 					 */
    662 					goto bad;
    663 				}
    664 #endif
    665 				if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
    666 					icmp_error(n, ICMP_UNREACH,
    667 						ICMP_UNREACH_PORT, 0, 0);
    668 				}
    669 #endif
    670 				return rcvcnt;
    671 			}
    672 		}
    673 
    674 		udp4_sendup(m, off, (struct sockaddr *)src, inp->inp_socket);
    675 		rcvcnt++;
    676 	}
    677 
    678 bad:
    679 	return rcvcnt;
    680 }
    681 
    682 #ifdef INET6
    683 static int
    684 in6_mcmatch(in6p, ia6, ifp)
    685 	struct in6pcb *in6p;
    686 	register struct in6_addr *ia6;
    687 	struct ifnet *ifp;
    688 {
    689 	struct ip6_moptions *im6o = in6p->in6p_moptions;
    690 	struct in6_multi_mship *imm;
    691 
    692 	if (im6o == NULL)
    693 		return 0;
    694 
    695 	for (imm = im6o->im6o_memberships.lh_first; imm != NULL;
    696 	     imm = imm->i6mm_chain.le_next) {
    697 		if ((ifp == NULL ||
    698 		     imm->i6mm_maddr->in6m_ifp == ifp) &&
    699 		    IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
    700 				       ia6))
    701 			return 1;
    702 	}
    703 	return 0;
    704 }
    705 
    706 static int
    707 udp6_realinput(af, src, dst, m, off)
    708 	int af;		/* af on packet */
    709 	struct sockaddr_in6 *src;
    710 	struct sockaddr_in6 *dst;
    711 	struct mbuf *m;
    712 	int off;	/* offset of udphdr */
    713 {
    714 	u_int16_t *sport, *dport;
    715 	int rcvcnt;
    716 	struct in6_addr *src6, *dst6;
    717 	struct in_addr *src4;
    718 	struct in6pcb *in6p;
    719 
    720 	rcvcnt = 0;
    721 	off += sizeof(struct udphdr);	/* now, offset of payload */
    722 
    723 	if (af != AF_INET && af != AF_INET6)
    724 		goto bad;
    725 	if (src->sin6_family != AF_INET6 || dst->sin6_family != AF_INET6)
    726 		goto bad;
    727 
    728 	src6 = &src->sin6_addr;
    729 	sport = &src->sin6_port;
    730 	dst6 = &dst->sin6_addr;
    731 	dport = &dst->sin6_port;
    732 	src4 = (struct in_addr *)&src->sin6_addr.s6_addr32[12];
    733 
    734 	if (IN6_IS_ADDR_MULTICAST(dst6)
    735 	 || (af == AF_INET && IN_MULTICAST(src4->s_addr))) {
    736 		struct in6pcb *last;
    737 		/*
    738 		 * Deliver a multicast or broadcast datagram to *all* sockets
    739 		 * for which the local and remote addresses and ports match
    740 		 * those of the incoming datagram.  This allows more than
    741 		 * one process to receive multi/broadcasts on the same port.
    742 		 * (This really ought to be done for unicast datagrams as
    743 		 * well, but that would cause problems with existing
    744 		 * applications that open both address-specific sockets and
    745 		 * a wildcard socket listening to the same port -- they would
    746 		 * end up receiving duplicates of every unicast datagram.
    747 		 * Those applications open the multiple sockets to overcome an
    748 		 * inadequacy of the UDP socket interface, but for backwards
    749 		 * compatibility we avoid the problem here rather than
    750 		 * fixing the interface.  Maybe 4.5BSD will remedy this?)
    751 		 */
    752 
    753 		/*
    754 		 * KAME note: usually we drop udpiphdr from mbuf here.
    755 		 * we need udpiphdr for iPsec processing so we do that later.
    756 		 */
    757 		/*
    758 		 * Locate pcb(s) for datagram.
    759 		 */
    760 		for (in6p = udb6.in6p_next; in6p != &udb6;
    761 		     in6p = in6p->in6p_next) {
    762 			if (in6p->in6p_lport != *dport)
    763 				continue;
    764 			if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
    765 				if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, dst6)
    766 				 && !in6_mcmatch(in6p, dst6, m->m_pkthdr.rcvif))
    767 					continue;
    768 			}
    769 #ifndef INET6_BINDV6ONLY
    770 			else {
    771 				if (IN6_IS_ADDR_V4MAPPED(dst6)
    772 				 && (in6p->in6p_flags & IN6P_BINDV6ONLY))
    773 					continue;
    774 			}
    775 #endif
    776 			if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
    777 				if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, src6)
    778 				 || in6p->in6p_fport != *sport)
    779 					continue;
    780 			}
    781 #ifndef INET6_BINDV6ONLY
    782 			else {
    783 				if (IN6_IS_ADDR_V4MAPPED(src6)
    784 				 && (in6p->in6p_flags & IN6P_BINDV6ONLY))
    785 					continue;
    786 			}
    787 #endif
    788 
    789 			last = in6p;
    790 			udp6_sendup(m, off, (struct sockaddr *)src,
    791 				in6p->in6p_socket);
    792 			rcvcnt++;
    793 
    794 			/*
    795 			 * Don't look for additional matches if this one does
    796 			 * not have either the SO_REUSEPORT or SO_REUSEADDR
    797 			 * socket options set.  This heuristic avoids searching
    798 			 * through all pcbs in the common case of a non-shared
    799 			 * port.  It assumes that an application will never
    800 			 * clear these options after setting them.
    801 			 */
    802 			if ((in6p->in6p_socket->so_options &
    803 			    (SO_REUSEPORT|SO_REUSEADDR)) == 0)
    804 				break;
    805 		}
    806 
    807 #if 0
    808 		if (last == NULL) {
    809 			/*
    810 			 * No matching pcb found; discard datagram.
    811 			 * (No need to send an ICMP Port Unreachable
    812 			 * for a broadcast or multicast datgram.)
    813 			 */
    814 			switch (af) {
    815 			case AF_INET:
    816 				udpstat.udps_noport++;
    817 				udpstat.udps_noportbcast++;
    818 				break;
    819 			case AF_INET6:
    820 				udp6stat.udp6s_noport++;
    821 				udp6stat.udp6s_noportmcast++;
    822 				break;
    823 			}
    824 			goto bad;
    825 		}
    826 #endif
    827 	} else {
    828 		/*
    829 		 * Locate pcb for datagram.
    830 		 */
    831 		in6p = in6_pcblookup_connect(&udb6, src6, *sport,
    832 			dst6, *dport, 0);
    833 		if (in6p == 0) {
    834 			++udpstat.udps_pcbhashmiss;
    835 			in6p = in6_pcblookup_bind(&udb6, dst6, *dport, 0);
    836 			if (in6p == 0) {
    837 #if 0
    838 				struct mbuf *n;
    839 				n = m_copy(m, 0, M_COPYALL);
    840 				switch (af) {
    841 				case AF_INET:
    842 					udpstat.udps_noport++;
    843 					if (m->m_flags & (M_BCAST | M_MCAST)) {
    844 						udpstat.udps_noportbcast++;
    845 						goto bad;
    846 					}
    847 					if (n != NULL)
    848 						icmp_error(n, ICMP_UNREACH,
    849 						    ICMP_UNREACH_PORT, 0, 0);
    850 					break;
    851 				case AF_INET6:
    852 					udp6stat.udp6s_noport++;
    853 					if (m->m_flags & M_MCAST) {
    854 						udp6stat.udp6s_noportmcast++;
    855 						goto bad;
    856 					}
    857 					if (n != NULL)
    858 						icmp6_error(n, ICMP6_DST_UNREACH,
    859 						    ICMP6_DST_UNREACH_NOPORT, 0);
    860 					break;
    861 				}
    862 #endif
    863 
    864 				return rcvcnt;
    865 			}
    866 		}
    867 
    868 		udp6_sendup(m, off, (struct sockaddr *)src, in6p->in6p_socket);
    869 		rcvcnt++;
    870 	}
    871 
    872 bad:
    873 	return rcvcnt;
    874 }
    875 #endif
    876 
    877 #else /*UDP6*/
    878 
    879 void
    880 #if __STDC__
    881 udp_input(struct mbuf *m, ...)
    882 #else
    883 udp_input(m, va_alist)
    884 	struct mbuf *m;
    885 	va_dcl
    886 #endif
    887 {
    888 	int proto;
    889 	register struct ip *ip;
    890 	register struct udphdr *uh;
    891 	register struct inpcb *inp;
    892 	struct mbuf *opts = 0;
    893 	int len;
    894 	struct ip save_ip;
    895 	int iphlen;
    896 	va_list ap;
    897 	struct sockaddr_in udpsrc;
    898 	struct sockaddr *sa;
    899 
    900 	va_start(ap, m);
    901 	iphlen = va_arg(ap, int);
    902 	proto = va_arg(ap, int);
    903 	va_end(ap);
    904 
    905 	udpstat.udps_ipackets++;
    906 
    907 	/*
    908 	 * Strip IP options, if any; should skip this,
    909 	 * make available to user, and use on returned packets,
    910 	 * but we don't yet have a way to check the checksum
    911 	 * with options still present.
    912 	 */
    913 	if (iphlen > sizeof (struct ip)) {
    914 		ip_stripoptions(m, (struct mbuf *)0);
    915 		iphlen = sizeof(struct ip);
    916 	}
    917 
    918 	/*
    919 	 * Get IP and UDP header together in first mbuf.
    920 	 */
    921 	ip = mtod(m, struct ip *);
    922 	if (m->m_len < iphlen + sizeof(struct udphdr)) {
    923 		if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) {
    924 			udpstat.udps_hdrops++;
    925 			return;
    926 		}
    927 		ip = mtod(m, struct ip *);
    928 	}
    929 	uh = (struct udphdr *)((caddr_t)ip + iphlen);
    930 
    931 	/*
    932 	 * Make mbuf data length reflect UDP length.
    933 	 * If not enough data to reflect UDP length, drop.
    934 	 */
    935 	len = ntohs((u_int16_t)uh->uh_ulen);
    936 	if (ip->ip_len != iphlen + len) {
    937 		if (ip->ip_len < iphlen + len) {
    938 			udpstat.udps_badlen++;
    939 			goto bad;
    940 		}
    941 		m_adj(m, iphlen + len - ip->ip_len);
    942 	}
    943 	/*
    944 	 * Save a copy of the IP header in case we want restore it
    945 	 * for sending an ICMP error message in response.
    946 	 */
    947 	save_ip = *ip;
    948 
    949 	/*
    950 	 * Checksum extended UDP header and data.
    951 	 */
    952 	if (uh->uh_sum) {
    953 		bzero(((struct ipovly *)ip)->ih_x1,
    954 		    sizeof ((struct ipovly *)ip)->ih_x1);
    955 		((struct ipovly *)ip)->ih_len = uh->uh_ulen;
    956 		if (in_cksum(m, len + sizeof (struct ip)) != 0) {
    957 			udpstat.udps_badsum++;
    958 			m_freem(m);
    959 			return;
    960 		}
    961 	}
    962 
    963 	/*
    964 	 * Construct sockaddr format source address.
    965 	 */
    966 	udpsrc.sin_family = AF_INET;
    967 	udpsrc.sin_len = sizeof(struct sockaddr_in);
    968 	udpsrc.sin_addr = ip->ip_src;
    969 	udpsrc.sin_port = uh->uh_sport;
    970 	bzero((caddr_t)udpsrc.sin_zero, sizeof(udpsrc.sin_zero));
    971 
    972 	if (IN_MULTICAST(ip->ip_dst.s_addr) ||
    973 	    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
    974 		struct inpcb *last;
    975 		/*
    976 		 * Deliver a multicast or broadcast datagram to *all* sockets
    977 		 * for which the local and remote addresses and ports match
    978 		 * those of the incoming datagram.  This allows more than
    979 		 * one process to receive multi/broadcasts on the same port.
    980 		 * (This really ought to be done for unicast datagrams as
    981 		 * well, but that would cause problems with existing
    982 		 * applications that open both address-specific sockets and
    983 		 * a wildcard socket listening to the same port -- they would
    984 		 * end up receiving duplicates of every unicast datagram.
    985 		 * Those applications open the multiple sockets to overcome an
    986 		 * inadequacy of the UDP socket interface, but for backwards
    987 		 * compatibility we avoid the problem here rather than
    988 		 * fixing the interface.  Maybe 4.5BSD will remedy this?)
    989 		 */
    990 
    991 		iphlen += sizeof(struct udphdr);
    992 		/*
    993 		 * KAME note: usually we drop udpiphdr from mbuf here.
    994 		 * we need udpiphdr for iPsec processing so we do that later.
    995 		 */
    996 		/*
    997 		 * Locate pcb(s) for datagram.
    998 		 * (Algorithm copied from raw_intr().)
    999 		 */
   1000 		last = NULL;
   1001 		for (inp = udbtable.inpt_queue.cqh_first;
   1002 		    inp != (struct inpcb *)&udbtable.inpt_queue;
   1003 		    inp = inp->inp_queue.cqe_next) {
   1004 			if (inp->inp_lport != uh->uh_dport)
   1005 				continue;
   1006 			if (!in_nullhost(inp->inp_laddr)) {
   1007 				if (!in_hosteq(inp->inp_laddr, ip->ip_dst))
   1008 					continue;
   1009 			}
   1010 			if (!in_nullhost(inp->inp_faddr)) {
   1011 				if (!in_hosteq(inp->inp_faddr, ip->ip_src) ||
   1012 				    inp->inp_fport != uh->uh_sport)
   1013 					continue;
   1014 			}
   1015 
   1016 			if (last != NULL) {
   1017 				struct mbuf *n;
   1018 
   1019 #ifdef IPSEC
   1020 				/* check AH/ESP integrity. */
   1021 				if (last != NULL && ipsec4_in_reject(m, last)) {
   1022 					ipsecstat.in_polvio++;
   1023 					/* do not inject data to pcb */
   1024 				} else
   1025 #endif /*IPSEC*/
   1026 				if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
   1027 					if (last->inp_flags & INP_CONTROLOPTS
   1028 					    || last->inp_socket->so_options &
   1029 					       SO_TIMESTAMP) {
   1030 						ip_savecontrol(last, &opts,
   1031 						    ip, n);
   1032 					}
   1033 					m_adj(n, iphlen);
   1034 					sa = (struct sockaddr *)&udpsrc;
   1035 					if (sbappendaddr(
   1036 					    &last->inp_socket->so_rcv,
   1037 					    sa, n, opts) == 0) {
   1038 						m_freem(n);
   1039 						if (opts)
   1040 							m_freem(opts);
   1041 					} else
   1042 						sorwakeup(last->inp_socket);
   1043 					opts = 0;
   1044 				}
   1045 			}
   1046 			last = inp;
   1047 			/*
   1048 			 * Don't look for additional matches if this one does
   1049 			 * not have either the SO_REUSEPORT or SO_REUSEADDR
   1050 			 * socket options set.  This heuristic avoids searching
   1051 			 * through all pcbs in the common case of a non-shared
   1052 			 * port.  It * assumes that an application will never
   1053 			 * clear these options after setting them.
   1054 			 */
   1055 			if ((last->inp_socket->so_options &
   1056 			    (SO_REUSEPORT|SO_REUSEADDR)) == 0)
   1057 				break;
   1058 		}
   1059 
   1060 		if (last == NULL) {
   1061 			/*
   1062 			 * No matching pcb found; discard datagram.
   1063 			 * (No need to send an ICMP Port Unreachable
   1064 			 * for a broadcast or multicast datgram.)
   1065 			 */
   1066 			udpstat.udps_noport++;
   1067 			udpstat.udps_noportbcast++;
   1068 			goto bad;
   1069 		}
   1070 #ifdef IPSEC
   1071 		/* check AH/ESP integrity. */
   1072 		if (last != NULL && ipsec4_in_reject(m, last)) {
   1073 			ipsecstat.in_polvio++;
   1074 			goto bad;
   1075 		}
   1076 #endif /*IPSEC*/
   1077 		if (last->inp_flags & INP_CONTROLOPTS ||
   1078 		    last->inp_socket->so_options & SO_TIMESTAMP)
   1079 			ip_savecontrol(last, &opts, ip, m);
   1080 		m->m_len -= iphlen;
   1081 		m->m_pkthdr.len -= iphlen;
   1082 		m->m_data += iphlen;
   1083 		sa = (struct sockaddr *)&udpsrc;
   1084 		if (sbappendaddr(&last->inp_socket->so_rcv, sa, m, opts) == 0) {
   1085 			udpstat.udps_fullsock++;
   1086 			goto bad;
   1087 		}
   1088 		sorwakeup(last->inp_socket);
   1089 		return;
   1090 	}
   1091 	/*
   1092 	 * Locate pcb for datagram.
   1093 	 */
   1094 	inp = in_pcblookup_connect(&udbtable, ip->ip_src, uh->uh_sport,
   1095 	    ip->ip_dst, uh->uh_dport);
   1096 	if (inp == 0) {
   1097 		++udpstat.udps_pcbhashmiss;
   1098 		inp = in_pcblookup_bind(&udbtable, ip->ip_dst, uh->uh_dport);
   1099 		if (inp == 0) {
   1100 			udpstat.udps_noport++;
   1101 			if (m->m_flags & (M_BCAST | M_MCAST)) {
   1102 				udpstat.udps_noportbcast++;
   1103 				goto bad;
   1104 			}
   1105 			*ip = save_ip;
   1106 #if NIPKDB > 0
   1107 			if (checkipkdb(&ip->ip_src,
   1108 				       uh->uh_sport,
   1109 				       uh->uh_dport,
   1110 				       m,
   1111 				       iphlen + sizeof(struct udphdr),
   1112 				       len - sizeof(struct udphdr)))
   1113 			/* It was a debugger connect packet, just drop it now */
   1114 				goto bad;
   1115 #endif
   1116 			icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
   1117 			return;
   1118 		}
   1119 	}
   1120 #ifdef IPSEC
   1121 	if (inp != NULL && ipsec4_in_reject(m, inp)) {
   1122 		ipsecstat.in_polvio++;
   1123 		goto bad;
   1124 	}
   1125 #endif /*IPSEC*/
   1126 
   1127 	/*
   1128 	 * Stuff source address and datagram in user buffer.
   1129 	 */
   1130 	if (inp->inp_flags & INP_CONTROLOPTS ||
   1131 	    inp->inp_socket->so_options & SO_TIMESTAMP)
   1132 		ip_savecontrol(inp, &opts, ip, m);
   1133 	iphlen += sizeof(struct udphdr);
   1134 	m->m_len -= iphlen;
   1135 	m->m_pkthdr.len -= iphlen;
   1136 	m->m_data += iphlen;
   1137 	sa = (struct sockaddr *)&udpsrc;
   1138 	if (sbappendaddr(&inp->inp_socket->so_rcv, sa, m, opts) == 0) {
   1139 		udpstat.udps_fullsock++;
   1140 		goto bad;
   1141 	}
   1142 	sorwakeup(inp->inp_socket);
   1143 	return;
   1144 bad:
   1145 	m_freem(m);
   1146 	if (opts)
   1147 		m_freem(opts);
   1148 }
   1149 #endif /*UDP6*/
   1150 
   1151 /*
   1152  * Notify a udp user of an asynchronous error;
   1153  * just wake up so that he can collect error status.
   1154  */
   1155 static void
   1156 udp_notify(inp, errno)
   1157 	register struct inpcb *inp;
   1158 	int errno;
   1159 {
   1160 
   1161 	inp->inp_socket->so_error = errno;
   1162 	sorwakeup(inp->inp_socket);
   1163 	sowwakeup(inp->inp_socket);
   1164 }
   1165 
   1166 void *
   1167 udp_ctlinput(cmd, sa, v)
   1168 	int cmd;
   1169 	struct sockaddr *sa;
   1170 	void *v;
   1171 {
   1172 	register struct ip *ip = v;
   1173 	register struct udphdr *uh;
   1174 	extern int inetctlerrmap[];
   1175 	void (*notify) __P((struct inpcb *, int)) = udp_notify;
   1176 	int errno;
   1177 
   1178 	if (sa->sa_family != AF_INET
   1179 	 || sa->sa_len != sizeof(struct sockaddr_in))
   1180 		return NULL;
   1181 	if ((unsigned)cmd >= PRC_NCMDS)
   1182 		return NULL;
   1183 	errno = inetctlerrmap[cmd];
   1184 	if (PRC_IS_REDIRECT(cmd))
   1185 		notify = in_rtchange, ip = 0;
   1186 	else if (cmd == PRC_HOSTDEAD)
   1187 		ip = 0;
   1188 	else if (errno == 0)
   1189 		return NULL;
   1190 	if (ip) {
   1191 		uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
   1192 		in_pcbnotify(&udbtable, satosin(sa)->sin_addr, uh->uh_dport,
   1193 		    ip->ip_src, uh->uh_sport, errno, notify);
   1194 
   1195 		/* XXX mapped address case */
   1196 	} else
   1197 		in_pcbnotifyall(&udbtable, satosin(sa)->sin_addr, errno,
   1198 		    notify);
   1199 	return NULL;
   1200 }
   1201 
   1202 int
   1203 #if __STDC__
   1204 udp_output(struct mbuf *m, ...)
   1205 #else
   1206 udp_output(m, va_alist)
   1207 	struct mbuf *m;
   1208 	va_dcl
   1209 #endif
   1210 {
   1211 	register struct inpcb *inp;
   1212 	register struct udpiphdr *ui;
   1213 	register int len = m->m_pkthdr.len;
   1214 	int error = 0;
   1215 	va_list ap;
   1216 
   1217 	va_start(ap, m);
   1218 	inp = va_arg(ap, struct inpcb *);
   1219 	va_end(ap);
   1220 
   1221 	/*
   1222 	 * Calculate data length and get a mbuf
   1223 	 * for UDP and IP headers.
   1224 	 */
   1225 	M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT);
   1226 	if (m == 0) {
   1227 		error = ENOBUFS;
   1228 		goto release;
   1229 	}
   1230 
   1231 	/*
   1232 	 * Compute the packet length of the IP header, and
   1233 	 * punt if the length looks bogus.
   1234 	 */
   1235 	if ((len + sizeof(struct udpiphdr)) > IP_MAXPACKET) {
   1236 		error = EMSGSIZE;
   1237 		goto release;
   1238 	}
   1239 
   1240 	/*
   1241 	 * Fill in mbuf with extended UDP header
   1242 	 * and addresses and length put into network format.
   1243 	 */
   1244 	ui = mtod(m, struct udpiphdr *);
   1245 	bzero(ui->ui_x1, sizeof ui->ui_x1);
   1246 	ui->ui_pr = IPPROTO_UDP;
   1247 	ui->ui_len = htons((u_int16_t)len + sizeof (struct udphdr));
   1248 	ui->ui_src = inp->inp_laddr;
   1249 	ui->ui_dst = inp->inp_faddr;
   1250 	ui->ui_sport = inp->inp_lport;
   1251 	ui->ui_dport = inp->inp_fport;
   1252 	ui->ui_ulen = ui->ui_len;
   1253 
   1254 	/*
   1255 	 * Stuff checksum and output datagram.
   1256 	 */
   1257 	ui->ui_sum = 0;
   1258 	if (udpcksum) {
   1259 	    if ((ui->ui_sum = in_cksum(m, sizeof (struct udpiphdr) + len)) == 0)
   1260 		ui->ui_sum = 0xffff;
   1261 	}
   1262 	((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
   1263 	((struct ip *)ui)->ip_ttl = inp->inp_ip.ip_ttl;	/* XXX */
   1264 	((struct ip *)ui)->ip_tos = inp->inp_ip.ip_tos;	/* XXX */
   1265 	udpstat.udps_opackets++;
   1266 
   1267 #ifdef IPSEC
   1268 	m->m_pkthdr.rcvif = (struct ifnet *)inp->inp_socket;
   1269 #endif /*IPSEC*/
   1270 
   1271 	return (ip_output(m, inp->inp_options, &inp->inp_route,
   1272 	    inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST),
   1273 	    inp->inp_moptions));
   1274 
   1275 release:
   1276 	m_freem(m);
   1277 	return (error);
   1278 }
   1279 
   1280 int	udp_sendspace = 9216;		/* really max datagram size */
   1281 int	udp_recvspace = 40 * (1024 + sizeof(struct sockaddr_in));
   1282 					/* 40 1K datagrams */
   1283 
   1284 /*ARGSUSED*/
   1285 int
   1286 udp_usrreq(so, req, m, nam, control, p)
   1287 	struct socket *so;
   1288 	int req;
   1289 	struct mbuf *m, *nam, *control;
   1290 	struct proc *p;
   1291 {
   1292 	register struct inpcb *inp;
   1293 	int s;
   1294 	register int error = 0;
   1295 
   1296 	if (req == PRU_CONTROL)
   1297 		return (in_control(so, (long)m, (caddr_t)nam,
   1298 		    (struct ifnet *)control, p));
   1299 
   1300 	s = splsoftnet();
   1301 	inp = sotoinpcb(so);
   1302 #ifdef DIAGNOSTIC
   1303 	if (req != PRU_SEND && req != PRU_SENDOOB && control)
   1304 		panic("udp_usrreq: unexpected control mbuf");
   1305 #endif
   1306 	if (inp == 0 && req != PRU_ATTACH) {
   1307 		error = EINVAL;
   1308 		goto release;
   1309 	}
   1310 
   1311 	/*
   1312 	 * Note: need to block udp_input while changing
   1313 	 * the udp pcb queue and/or pcb addresses.
   1314 	 */
   1315 	switch (req) {
   1316 
   1317 	case PRU_ATTACH:
   1318 		if (inp != 0) {
   1319 			error = EISCONN;
   1320 			break;
   1321 		}
   1322 		if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
   1323 			error = soreserve(so, udp_sendspace, udp_recvspace);
   1324 			if (error)
   1325 				break;
   1326 		}
   1327 		error = in_pcballoc(so, &udbtable);
   1328 		if (error)
   1329 			break;
   1330 		inp = sotoinpcb(so);
   1331 		inp->inp_ip.ip_ttl = ip_defttl;
   1332 #ifdef IPSEC
   1333 		error = ipsec_init_policy(&inp->inp_sp);
   1334 		if (error != 0) {
   1335 			in_pcbdetach(inp);
   1336 			break;
   1337 		}
   1338 #endif /*IPSEC*/
   1339 		break;
   1340 
   1341 	case PRU_DETACH:
   1342 		in_pcbdetach(inp);
   1343 		break;
   1344 
   1345 	case PRU_BIND:
   1346 		error = in_pcbbind(inp, nam, p);
   1347 		break;
   1348 
   1349 	case PRU_LISTEN:
   1350 		error = EOPNOTSUPP;
   1351 		break;
   1352 
   1353 	case PRU_CONNECT:
   1354 		error = in_pcbconnect(inp, nam);
   1355 		if (error)
   1356 			break;
   1357 		soisconnected(so);
   1358 		break;
   1359 
   1360 	case PRU_CONNECT2:
   1361 		error = EOPNOTSUPP;
   1362 		break;
   1363 
   1364 	case PRU_DISCONNECT:
   1365 		/*soisdisconnected(so);*/
   1366 		so->so_state &= ~SS_ISCONNECTED;	/* XXX */
   1367 		in_pcbdisconnect(inp);
   1368 		inp->inp_laddr = zeroin_addr;		/* XXX */
   1369 		in_pcbstate(inp, INP_BOUND);		/* XXX */
   1370 		break;
   1371 
   1372 	case PRU_SHUTDOWN:
   1373 		socantsendmore(so);
   1374 		break;
   1375 
   1376 	case PRU_RCVD:
   1377 		error = EOPNOTSUPP;
   1378 		break;
   1379 
   1380 	case PRU_SEND:
   1381 		if (control && control->m_len) {
   1382 			m_freem(control);
   1383 			m_freem(m);
   1384 			error = EINVAL;
   1385 			break;
   1386 		}
   1387 	{
   1388 		struct in_addr laddr;			/* XXX */
   1389 
   1390 		if (nam) {
   1391 			laddr = inp->inp_laddr;		/* XXX */
   1392 			if ((so->so_state & SS_ISCONNECTED) != 0) {
   1393 				error = EISCONN;
   1394 				goto die;
   1395 			}
   1396 			error = in_pcbconnect(inp, nam);
   1397 			if (error) {
   1398 			die:
   1399 				m_freem(m);
   1400 				break;
   1401 			}
   1402 		} else {
   1403 			if ((so->so_state & SS_ISCONNECTED) == 0) {
   1404 				error = ENOTCONN;
   1405 				goto die;
   1406 			}
   1407 		}
   1408 		error = udp_output(m, inp);
   1409 		if (nam) {
   1410 			in_pcbdisconnect(inp);
   1411 			inp->inp_laddr = laddr;		/* XXX */
   1412 			in_pcbstate(inp, INP_BOUND);	/* XXX */
   1413 		}
   1414 	}
   1415 		break;
   1416 
   1417 	case PRU_SENSE:
   1418 		/*
   1419 		 * stat: don't bother with a blocksize.
   1420 		 */
   1421 		splx(s);
   1422 		return (0);
   1423 
   1424 	case PRU_RCVOOB:
   1425 		error =  EOPNOTSUPP;
   1426 		break;
   1427 
   1428 	case PRU_SENDOOB:
   1429 		m_freem(control);
   1430 		m_freem(m);
   1431 		error =  EOPNOTSUPP;
   1432 		break;
   1433 
   1434 	case PRU_SOCKADDR:
   1435 		in_setsockaddr(inp, nam);
   1436 		break;
   1437 
   1438 	case PRU_PEERADDR:
   1439 		in_setpeeraddr(inp, nam);
   1440 		break;
   1441 
   1442 	default:
   1443 		panic("udp_usrreq");
   1444 	}
   1445 
   1446 release:
   1447 	splx(s);
   1448 	return (error);
   1449 }
   1450 
   1451 /*
   1452  * Sysctl for udp variables.
   1453  */
   1454 int
   1455 udp_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
   1456 	int *name;
   1457 	u_int namelen;
   1458 	void *oldp;
   1459 	size_t *oldlenp;
   1460 	void *newp;
   1461 	size_t newlen;
   1462 {
   1463 	/* All sysctl names at this level are terminal. */
   1464 	if (namelen != 1)
   1465 		return (ENOTDIR);
   1466 
   1467 	switch (name[0]) {
   1468 	case UDPCTL_CHECKSUM:
   1469 		return (sysctl_int(oldp, oldlenp, newp, newlen, &udpcksum));
   1470 	case UDPCTL_SENDSPACE:
   1471 		return (sysctl_int(oldp, oldlenp, newp, newlen,
   1472 		    &udp_sendspace));
   1473 	case UDPCTL_RECVSPACE:
   1474 		return (sysctl_int(oldp, oldlenp, newp, newlen,
   1475 		    &udp_recvspace));
   1476 	default:
   1477 		return (ENOPROTOOPT);
   1478 	}
   1479 	/* NOTREACHED */
   1480 }
   1481