Home | History | Annotate | Line # | Download | only in netinet
in_pcb.c revision 1.120
      1 /*	$NetBSD: in_pcb.c,v 1.120 2007/12/16 14:12:34 elad 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) 1998 The NetBSD Foundation, Inc.
     34  * All rights reserved.
     35  *
     36  * This code is derived from software contributed to The NetBSD Foundation
     37  * by Public Access Networks Corporation ("Panix").  It was developed under
     38  * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon.
     39  *
     40  * Redistribution and use in source and binary forms, with or without
     41  * modification, are permitted provided that the following conditions
     42  * are met:
     43  * 1. Redistributions of source code must retain the above copyright
     44  *    notice, this list of conditions and the following disclaimer.
     45  * 2. Redistributions in binary form must reproduce the above copyright
     46  *    notice, this list of conditions and the following disclaimer in the
     47  *    documentation and/or other materials provided with the distribution.
     48  * 3. All advertising materials mentioning features or use of this software
     49  *    must display the following acknowledgement:
     50  *	This product includes software developed by the NetBSD
     51  *	Foundation, Inc. and its contributors.
     52  * 4. Neither the name of The NetBSD Foundation nor the names of its
     53  *    contributors may be used to endorse or promote products derived
     54  *    from this software without specific prior written permission.
     55  *
     56  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     57  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     58  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     59  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     60  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     61  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     62  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     63  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     64  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     65  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     66  * POSSIBILITY OF SUCH DAMAGE.
     67  */
     68 
     69 /*
     70  * Copyright (c) 1982, 1986, 1991, 1993, 1995
     71  *	The Regents of the University of California.  All rights reserved.
     72  *
     73  * Redistribution and use in source and binary forms, with or without
     74  * modification, are permitted provided that the following conditions
     75  * are met:
     76  * 1. Redistributions of source code must retain the above copyright
     77  *    notice, this list of conditions and the following disclaimer.
     78  * 2. Redistributions in binary form must reproduce the above copyright
     79  *    notice, this list of conditions and the following disclaimer in the
     80  *    documentation and/or other materials provided with the distribution.
     81  * 3. Neither the name of the University nor the names of its contributors
     82  *    may be used to endorse or promote products derived from this software
     83  *    without specific prior written permission.
     84  *
     85  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     86  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     87  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     88  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     89  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     90  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     91  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     92  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     93  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     94  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     95  * SUCH DAMAGE.
     96  *
     97  *	@(#)in_pcb.c	8.4 (Berkeley) 5/24/95
     98  */
     99 
    100 #include <sys/cdefs.h>
    101 __KERNEL_RCSID(0, "$NetBSD: in_pcb.c,v 1.120 2007/12/16 14:12:34 elad Exp $");
    102 
    103 #include "opt_inet.h"
    104 #include "opt_ipsec.h"
    105 
    106 #include <sys/param.h>
    107 #include <sys/systm.h>
    108 #include <sys/malloc.h>
    109 #include <sys/mbuf.h>
    110 #include <sys/protosw.h>
    111 #include <sys/socket.h>
    112 #include <sys/socketvar.h>
    113 #include <sys/ioctl.h>
    114 #include <sys/errno.h>
    115 #include <sys/time.h>
    116 #include <sys/pool.h>
    117 #include <sys/proc.h>
    118 #include <sys/kauth.h>
    119 
    120 #include <net/if.h>
    121 #include <net/route.h>
    122 
    123 #include <netinet/in.h>
    124 #include <netinet/in_systm.h>
    125 #include <netinet/ip.h>
    126 #include <netinet/in_pcb.h>
    127 #include <netinet/in_var.h>
    128 #include <netinet/ip_var.h>
    129 
    130 #ifdef INET6
    131 #include <netinet/ip6.h>
    132 #include <netinet6/ip6_var.h>
    133 #include <netinet6/in6_pcb.h>
    134 #endif
    135 
    136 #ifdef IPSEC
    137 #include <netinet6/ipsec.h>
    138 #include <netkey/key.h>
    139 #elif FAST_IPSEC
    140 #include <netipsec/ipsec.h>
    141 #include <netipsec/key.h>
    142 #endif /* IPSEC */
    143 
    144 struct	in_addr zeroin_addr;
    145 
    146 #define	INPCBHASH_PORT(table, lport) \
    147 	&(table)->inpt_porthashtbl[ntohs(lport) & (table)->inpt_porthash]
    148 #define	INPCBHASH_BIND(table, laddr, lport) \
    149 	&(table)->inpt_bindhashtbl[ \
    150 	    ((ntohl((laddr).s_addr) + ntohs(lport))) & (table)->inpt_bindhash]
    151 #define	INPCBHASH_CONNECT(table, faddr, fport, laddr, lport) \
    152 	&(table)->inpt_connecthashtbl[ \
    153 	    ((ntohl((faddr).s_addr) + ntohs(fport)) + \
    154 	     (ntohl((laddr).s_addr) + ntohs(lport))) & (table)->inpt_connecthash]
    155 
    156 int	anonportmin = IPPORT_ANONMIN;
    157 int	anonportmax = IPPORT_ANONMAX;
    158 int	lowportmin  = IPPORT_RESERVEDMIN;
    159 int	lowportmax  = IPPORT_RESERVEDMAX;
    160 
    161 POOL_INIT(inpcb_pool, sizeof(struct inpcb), 0, 0, 0, "inpcbpl", NULL,
    162     IPL_NET);
    163 
    164 void
    165 in_pcbinit(struct inpcbtable *table, int bindhashsize, int connecthashsize)
    166 {
    167 
    168 	CIRCLEQ_INIT(&table->inpt_queue);
    169 	table->inpt_porthashtbl = hashinit(bindhashsize, HASH_LIST, M_PCB,
    170 	    M_WAITOK, &table->inpt_porthash);
    171 	table->inpt_bindhashtbl = hashinit(bindhashsize, HASH_LIST, M_PCB,
    172 	    M_WAITOK, &table->inpt_bindhash);
    173 	table->inpt_connecthashtbl = hashinit(connecthashsize, HASH_LIST,
    174 	    M_PCB, M_WAITOK, &table->inpt_connecthash);
    175 	table->inpt_lastlow = IPPORT_RESERVEDMAX;
    176 	table->inpt_lastport = (u_int16_t)anonportmax;
    177 }
    178 
    179 int
    180 in_pcballoc(struct socket *so, void *v)
    181 {
    182 	struct inpcbtable *table = v;
    183 	struct inpcb *inp;
    184 	int s;
    185 #if defined(IPSEC) || defined(FAST_IPSEC)
    186 	int error;
    187 #endif
    188 
    189 	s = splnet();
    190 	inp = pool_get(&inpcb_pool, PR_NOWAIT);
    191 	splx(s);
    192 	if (inp == NULL)
    193 		return (ENOBUFS);
    194 	bzero((void *)inp, sizeof(*inp));
    195 	inp->inp_af = AF_INET;
    196 	inp->inp_table = table;
    197 	inp->inp_socket = so;
    198 	inp->inp_errormtu = -1;
    199 #if defined(IPSEC) || defined(FAST_IPSEC)
    200 	error = ipsec_init_pcbpolicy(so, &inp->inp_sp);
    201 	if (error != 0) {
    202 		s = splnet();
    203 		pool_put(&inpcb_pool, inp);
    204 		splx(s);
    205 		return error;
    206 	}
    207 #endif
    208 	so->so_pcb = inp;
    209 	s = splnet();
    210 	CIRCLEQ_INSERT_HEAD(&table->inpt_queue, &inp->inp_head,
    211 	    inph_queue);
    212 	LIST_INSERT_HEAD(INPCBHASH_PORT(table, inp->inp_lport), &inp->inp_head,
    213 	    inph_lhash);
    214 	in_pcbstate(inp, INP_ATTACHED);
    215 	splx(s);
    216 	return (0);
    217 }
    218 
    219 int
    220 in_pcbbind(void *v, struct mbuf *nam, struct lwp *l)
    221 {
    222 	struct in_ifaddr *ia = NULL;
    223 	struct inpcb *inp = v;
    224 	struct socket *so = inp->inp_socket;
    225 	struct inpcbtable *table = inp->inp_table;
    226 	struct sockaddr_in *sin = NULL; /* XXXGCC */
    227 	u_int16_t lport = 0;
    228 	int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
    229 	kauth_cred_t cred = l->l_cred;
    230 
    231 	if (inp->inp_af != AF_INET)
    232 		return (EINVAL);
    233 
    234 	if (TAILQ_FIRST(&in_ifaddrhead) == 0)
    235 		return (EADDRNOTAVAIL);
    236 	if (inp->inp_lport || !in_nullhost(inp->inp_laddr))
    237 		return (EINVAL);
    238 	if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
    239 		wild = 1;
    240 	if (nam == 0)
    241 		goto noname;
    242 	sin = mtod(nam, struct sockaddr_in *);
    243 	if (nam->m_len != sizeof (*sin))
    244 		return (EINVAL);
    245 	if (sin->sin_family != AF_INET)
    246 		return (EAFNOSUPPORT);
    247 	lport = sin->sin_port;
    248 	if (IN_MULTICAST(sin->sin_addr.s_addr)) {
    249 		/*
    250 		 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
    251 		 * allow complete duplication of binding if
    252 		 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
    253 		 * and a multicast address is bound on both
    254 		 * new and duplicated sockets.
    255 		 */
    256 		if (so->so_options & SO_REUSEADDR)
    257 			reuseport = SO_REUSEADDR|SO_REUSEPORT;
    258 	} else if (!in_nullhost(sin->sin_addr)) {
    259 		sin->sin_port = 0;		/* yech... */
    260 		INADDR_TO_IA(sin->sin_addr, ia);
    261 		/* check for broadcast addresses */
    262 		if (ia == NULL)
    263 			ia = ifatoia(ifa_ifwithaddr(sintosa(sin)));
    264 		if (ia == NULL)
    265 			return (EADDRNOTAVAIL);
    266 	}
    267 	if (lport) {
    268 		struct inpcb *t;
    269 #ifdef INET6
    270 		struct in6pcb *t6;
    271 		struct in6_addr mapped;
    272 #endif
    273 #ifndef IPNOPRIVPORTS
    274 		/* GROSS */
    275 		if (ntohs(lport) < IPPORT_RESERVED &&
    276 		    kauth_authorize_network(cred,
    277 		    KAUTH_NETWORK_BIND,
    278 		    KAUTH_REQ_NETWORK_BIND_PRIVPORT, so, sin,
    279 		    NULL))
    280 			return (EACCES);
    281 #endif
    282 #ifdef INET6
    283 		memset(&mapped, 0, sizeof(mapped));
    284 		mapped.s6_addr16[5] = 0xffff;
    285 		memcpy(&mapped.s6_addr32[3], &sin->sin_addr,
    286 		    sizeof(mapped.s6_addr32[3]));
    287 		t6 = in6_pcblookup_port(table, &mapped, lport, wild);
    288 		if (t6 && (reuseport & t6->in6p_socket->so_options) == 0)
    289 			return (EADDRINUSE);
    290 #endif
    291 		if (so->so_uidinfo->ui_uid && !IN_MULTICAST(sin->sin_addr.s_addr)) {
    292 			t = in_pcblookup_port(table, sin->sin_addr, lport, 1);
    293 		/*
    294 		 * XXX:	investigate ramifications of loosening this
    295 		 *	restriction so that as long as both ports have
    296 		 *	SO_REUSEPORT allow the bind
    297 		 */
    298 			if (t &&
    299 			    (!in_nullhost(sin->sin_addr) ||
    300 			     !in_nullhost(t->inp_laddr) ||
    301 			     (t->inp_socket->so_options & SO_REUSEPORT) == 0)
    302 			    && (so->so_uidinfo->ui_uid != t->inp_socket->so_uidinfo->ui_uid)) {
    303 				return (EADDRINUSE);
    304 			}
    305 		}
    306 		t = in_pcblookup_port(table, sin->sin_addr, lport, wild);
    307 		if (t && (reuseport & t->inp_socket->so_options) == 0)
    308 			return (EADDRINUSE);
    309 	}
    310 	inp->inp_laddr = sin->sin_addr;
    311 
    312 noname:
    313 	if (lport == 0) {
    314 		int	   cnt;
    315 		u_int16_t  mymin, mymax;
    316 		u_int16_t *lastport;
    317 
    318 		if (inp->inp_flags & INP_LOWPORT) {
    319 #ifndef IPNOPRIVPORTS
    320 			if (kauth_authorize_network(cred,
    321 			    KAUTH_NETWORK_BIND,
    322 			    KAUTH_REQ_NETWORK_BIND_PRIVPORT, so,
    323 			    sin, NULL))
    324 				return (EACCES);
    325 #endif
    326 			mymin = lowportmin;
    327 			mymax = lowportmax;
    328 			lastport = &table->inpt_lastlow;
    329 		} else {
    330 			mymin = anonportmin;
    331 			mymax = anonportmax;
    332 			lastport = &table->inpt_lastport;
    333 		}
    334 		if (mymin > mymax) {	/* sanity check */
    335 			u_int16_t swp;
    336 
    337 			swp = mymin;
    338 			mymin = mymax;
    339 			mymax = swp;
    340 		}
    341 
    342 		lport = *lastport - 1;
    343 		for (cnt = mymax - mymin + 1; cnt; cnt--, lport--) {
    344 			if (lport < mymin || lport > mymax)
    345 				lport = mymax;
    346 			if (!in_pcblookup_port(table, inp->inp_laddr,
    347 			    htons(lport), 1))
    348 				goto found;
    349 		}
    350 		if (!in_nullhost(inp->inp_laddr))
    351 			inp->inp_laddr.s_addr = INADDR_ANY;
    352 		return (EAGAIN);
    353 	found:
    354 		inp->inp_flags |= INP_ANONPORT;
    355 		*lastport = lport;
    356 		lport = htons(lport);
    357 	}
    358 	inp->inp_lport = lport;
    359 	LIST_REMOVE(&inp->inp_head, inph_lhash);
    360 	LIST_INSERT_HEAD(INPCBHASH_PORT(table, inp->inp_lport), &inp->inp_head,
    361 	    inph_lhash);
    362 	in_pcbstate(inp, INP_BOUND);
    363 	return (0);
    364 }
    365 
    366 /*
    367  * Connect from a socket to a specified address.
    368  * Both address and port must be specified in argument sin.
    369  * If don't have a local address for this socket yet,
    370  * then pick one.
    371  */
    372 int
    373 in_pcbconnect(void *v, struct mbuf *nam, struct lwp *l)
    374 {
    375 	struct inpcb *inp = v;
    376 	struct in_ifaddr *ia = NULL;
    377 	struct sockaddr_in *ifaddr = NULL;
    378 	struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *);
    379 	int error;
    380 
    381 	if (inp->inp_af != AF_INET)
    382 		return (EINVAL);
    383 
    384 	if (nam->m_len != sizeof (*sin))
    385 		return (EINVAL);
    386 	if (sin->sin_family != AF_INET)
    387 		return (EAFNOSUPPORT);
    388 	if (sin->sin_port == 0)
    389 		return (EADDRNOTAVAIL);
    390 	if (TAILQ_FIRST(&in_ifaddrhead) != 0) {
    391 		/*
    392 		 * If the destination address is INADDR_ANY,
    393 		 * use any local address (likely loopback).
    394 		 * If the supplied address is INADDR_BROADCAST,
    395 		 * use the broadcast address of an interface
    396 		 * which supports broadcast. (loopback does not)
    397 		 */
    398 
    399 		if (in_nullhost(sin->sin_addr)) {
    400 			sin->sin_addr =
    401 			    TAILQ_FIRST(&in_ifaddrhead)->ia_addr.sin_addr;
    402 		} else if (sin->sin_addr.s_addr == INADDR_BROADCAST) {
    403 			TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) {
    404 				if (ia->ia_ifp->if_flags & IFF_BROADCAST) {
    405 					sin->sin_addr =
    406 					    ia->ia_broadaddr.sin_addr;
    407 					break;
    408 				}
    409 			}
    410 		}
    411 	}
    412 	/*
    413 	 * If we haven't bound which network number to use as ours,
    414 	 * we will use the number of the outgoing interface.
    415 	 * This depends on having done a routing lookup, which
    416 	 * we will probably have to do anyway, so we might
    417 	 * as well do it now.  On the other hand if we are
    418 	 * sending to multiple destinations we may have already
    419 	 * done the lookup, so see if we can use the route
    420 	 * from before.  In any case, we only
    421 	 * chose a port number once, even if sending to multiple
    422 	 * destinations.
    423 	 */
    424 	if (in_nullhost(inp->inp_laddr)) {
    425 		int xerror;
    426 		ifaddr = in_selectsrc(sin, &inp->inp_route,
    427 		    inp->inp_socket->so_options, inp->inp_moptions, &xerror);
    428 		if (ifaddr == NULL) {
    429 			if (xerror == 0)
    430 				xerror = EADDRNOTAVAIL;
    431 			return xerror;
    432 		}
    433 		INADDR_TO_IA(ifaddr->sin_addr, ia);
    434 		if (ia == NULL)
    435 			return (EADDRNOTAVAIL);
    436 	}
    437 	if (in_pcblookup_connect(inp->inp_table, sin->sin_addr, sin->sin_port,
    438 	    !in_nullhost(inp->inp_laddr) ? inp->inp_laddr : ifaddr->sin_addr,
    439 	    inp->inp_lport) != 0)
    440 		return (EADDRINUSE);
    441 	if (in_nullhost(inp->inp_laddr)) {
    442 		if (inp->inp_lport == 0) {
    443 			error = in_pcbbind(inp, NULL, l);
    444 			/*
    445 			 * This used to ignore the return value
    446 			 * completely, but we need to check for
    447 			 * ephemeral port shortage.
    448 			 * And attempts to request low ports if not root.
    449 			 */
    450 			if (error != 0)
    451 				return (error);
    452 		}
    453 		inp->inp_laddr = ifaddr->sin_addr;
    454 	}
    455 	inp->inp_faddr = sin->sin_addr;
    456 	inp->inp_fport = sin->sin_port;
    457 	in_pcbstate(inp, INP_CONNECTED);
    458 #if defined(IPSEC) || defined(FAST_IPSEC)
    459 	if (inp->inp_socket->so_type == SOCK_STREAM)
    460 		ipsec_pcbconn(inp->inp_sp);
    461 #endif
    462 	return (0);
    463 }
    464 
    465 void
    466 in_pcbdisconnect(void *v)
    467 {
    468 	struct inpcb *inp = v;
    469 
    470 	if (inp->inp_af != AF_INET)
    471 		return;
    472 
    473 	inp->inp_faddr = zeroin_addr;
    474 	inp->inp_fport = 0;
    475 	in_pcbstate(inp, INP_BOUND);
    476 #if defined(IPSEC) || defined(FAST_IPSEC)
    477 	ipsec_pcbdisconn(inp->inp_sp);
    478 #endif
    479 	if (inp->inp_socket->so_state & SS_NOFDREF)
    480 		in_pcbdetach(inp);
    481 }
    482 
    483 void
    484 in_pcbdetach(void *v)
    485 {
    486 	struct inpcb *inp = v;
    487 	struct socket *so = inp->inp_socket;
    488 	int s;
    489 
    490 	if (inp->inp_af != AF_INET)
    491 		return;
    492 
    493 #if defined(IPSEC) || defined(FAST_IPSEC)
    494 	ipsec4_delete_pcbpolicy(inp);
    495 #endif /*IPSEC*/
    496 	so->so_pcb = 0;
    497 	sofree(so);
    498 	if (inp->inp_options)
    499 		(void)m_free(inp->inp_options);
    500 	rtcache_free(&inp->inp_route);
    501 	ip_freemoptions(inp->inp_moptions);
    502 	s = splnet();
    503 	in_pcbstate(inp, INP_ATTACHED);
    504 	LIST_REMOVE(&inp->inp_head, inph_lhash);
    505 	CIRCLEQ_REMOVE(&inp->inp_table->inpt_queue, &inp->inp_head,
    506 	    inph_queue);
    507 	pool_put(&inpcb_pool, inp);
    508 	splx(s);
    509 }
    510 
    511 void
    512 in_setsockaddr(struct inpcb *inp, struct mbuf *nam)
    513 {
    514 	struct sockaddr_in *sin;
    515 
    516 	if (inp->inp_af != AF_INET)
    517 		return;
    518 
    519 	sin = mtod(nam, struct sockaddr_in *);
    520 	sockaddr_in_init(sin, &inp->inp_laddr, inp->inp_lport);
    521 	nam->m_len = sin->sin_len;
    522 }
    523 
    524 void
    525 in_setpeeraddr(struct inpcb *inp, struct mbuf *nam)
    526 {
    527 	struct sockaddr_in *sin;
    528 
    529 	if (inp->inp_af != AF_INET)
    530 		return;
    531 
    532 	sin = mtod(nam, struct sockaddr_in *);
    533 	sockaddr_in_init(sin, &inp->inp_faddr, inp->inp_fport);
    534 	nam->m_len = sin->sin_len;
    535 }
    536 
    537 /*
    538  * Pass some notification to all connections of a protocol
    539  * associated with address dst.  The local address and/or port numbers
    540  * may be specified to limit the search.  The "usual action" will be
    541  * taken, depending on the ctlinput cmd.  The caller must filter any
    542  * cmds that are uninteresting (e.g., no error in the map).
    543  * Call the protocol specific routine (if any) to report
    544  * any errors for each matching socket.
    545  *
    546  * Must be called at splsoftnet.
    547  */
    548 int
    549 in_pcbnotify(struct inpcbtable *table, struct in_addr faddr, u_int fport_arg,
    550     struct in_addr laddr, u_int lport_arg, int errno,
    551     void (*notify)(struct inpcb *, int))
    552 {
    553 	struct inpcbhead *head;
    554 	struct inpcb *inp, *ninp;
    555 	u_int16_t fport = fport_arg, lport = lport_arg;
    556 	int nmatch;
    557 
    558 	if (in_nullhost(faddr) || notify == 0)
    559 		return (0);
    560 
    561 	nmatch = 0;
    562 	head = INPCBHASH_CONNECT(table, faddr, fport, laddr, lport);
    563 	for (inp = (struct inpcb *)LIST_FIRST(head); inp != NULL; inp = ninp) {
    564 		ninp = (struct inpcb *)LIST_NEXT(inp, inp_hash);
    565 		if (inp->inp_af != AF_INET)
    566 			continue;
    567 		if (in_hosteq(inp->inp_faddr, faddr) &&
    568 		    inp->inp_fport == fport &&
    569 		    inp->inp_lport == lport &&
    570 		    in_hosteq(inp->inp_laddr, laddr)) {
    571 			(*notify)(inp, errno);
    572 			nmatch++;
    573 		}
    574 	}
    575 	return (nmatch);
    576 }
    577 
    578 void
    579 in_pcbnotifyall(struct inpcbtable *table, struct in_addr faddr, int errno,
    580     void (*notify)(struct inpcb *, int))
    581 {
    582 	struct inpcb *inp, *ninp;
    583 
    584 	if (in_nullhost(faddr) || notify == 0)
    585 		return;
    586 
    587 	for (inp = (struct inpcb *)CIRCLEQ_FIRST(&table->inpt_queue);
    588 	    inp != (void *)&table->inpt_queue;
    589 	    inp = ninp) {
    590 		ninp = (struct inpcb *)CIRCLEQ_NEXT(inp, inp_queue);
    591 		if (inp->inp_af != AF_INET)
    592 			continue;
    593 		if (in_hosteq(inp->inp_faddr, faddr))
    594 			(*notify)(inp, errno);
    595 	}
    596 }
    597 
    598 void
    599 in_pcbpurgeif0(struct inpcbtable *table, struct ifnet *ifp)
    600 {
    601 	struct inpcb *inp, *ninp;
    602 	struct ip_moptions *imo;
    603 	int i, gap;
    604 
    605 	for (inp = (struct inpcb *)CIRCLEQ_FIRST(&table->inpt_queue);
    606 	    inp != (void *)&table->inpt_queue;
    607 	    inp = ninp) {
    608 		ninp = (struct inpcb *)CIRCLEQ_NEXT(inp, inp_queue);
    609 		if (inp->inp_af != AF_INET)
    610 			continue;
    611 		imo = inp->inp_moptions;
    612 		if (imo != NULL) {
    613 			/*
    614 			 * Unselect the outgoing interface if it is being
    615 			 * detached.
    616 			 */
    617 			if (imo->imo_multicast_ifp == ifp)
    618 				imo->imo_multicast_ifp = NULL;
    619 
    620 			/*
    621 			 * Drop multicast group membership if we joined
    622 			 * through the interface being detached.
    623 			 */
    624 			for (i = 0, gap = 0; i < imo->imo_num_memberships;
    625 			    i++) {
    626 				if (imo->imo_membership[i]->inm_ifp == ifp) {
    627 					in_delmulti(imo->imo_membership[i]);
    628 					gap++;
    629 				} else if (gap != 0)
    630 					imo->imo_membership[i - gap] =
    631 					    imo->imo_membership[i];
    632 			}
    633 			imo->imo_num_memberships -= gap;
    634 		}
    635 	}
    636 }
    637 
    638 void
    639 in_pcbpurgeif(struct inpcbtable *table, struct ifnet *ifp)
    640 {
    641 	struct inpcb *inp, *ninp;
    642 
    643 	for (inp = (struct inpcb *)CIRCLEQ_FIRST(&table->inpt_queue);
    644 	    inp != (void *)&table->inpt_queue;
    645 	    inp = ninp) {
    646 		ninp = (struct inpcb *)CIRCLEQ_NEXT(inp, inp_queue);
    647 		if (inp->inp_af != AF_INET)
    648 			continue;
    649 		if (inp->inp_route.ro_rt != NULL &&
    650 		    inp->inp_route.ro_rt->rt_ifp == ifp)
    651 			in_rtchange(inp, 0);
    652 	}
    653 }
    654 
    655 /*
    656  * Check for alternatives when higher level complains
    657  * about service problems.  For now, invalidate cached
    658  * routing information.  If the route was created dynamically
    659  * (by a redirect), time to try a default gateway again.
    660  */
    661 void
    662 in_losing(struct inpcb *inp)
    663 {
    664 	struct rtentry *rt;
    665 	struct rt_addrinfo info;
    666 
    667 	if (inp->inp_af != AF_INET)
    668 		return;
    669 
    670 	if ((rt = inp->inp_route.ro_rt) != NULL) {
    671 		memset(&info, 0, sizeof(info));
    672 		info.rti_info[RTAX_DST] = rtcache_getdst(&inp->inp_route);
    673 		info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
    674 		info.rti_info[RTAX_NETMASK] = rt_mask(rt);
    675 		rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
    676 		if (rt->rt_flags & RTF_DYNAMIC)
    677 			(void) rtrequest(RTM_DELETE, rt_getkey(rt),
    678 				rt->rt_gateway, rt_mask(rt), rt->rt_flags,
    679 				NULL);
    680 		/*
    681 		 * A new route can be allocated
    682 		 * the next time output is attempted.
    683 		 */
    684 		rtcache_free(&inp->inp_route);
    685 	}
    686 }
    687 
    688 /*
    689  * After a routing change, flush old routing.  A new route can be
    690  * allocated the next time output is attempted.
    691  */
    692 void
    693 in_rtchange(struct inpcb *inp, int errno)
    694 {
    695 
    696 	if (inp->inp_af != AF_INET)
    697 		return;
    698 
    699 	rtcache_free(&inp->inp_route);
    700 
    701 	/* XXX SHOULD NOTIFY HIGHER-LEVEL PROTOCOLS */
    702 }
    703 
    704 struct inpcb *
    705 in_pcblookup_port(struct inpcbtable *table, struct in_addr laddr,
    706     u_int lport_arg, int lookup_wildcard)
    707 {
    708 	struct inpcbhead *head;
    709 	struct inpcb_hdr *inph;
    710 	struct inpcb *inp, *match = 0;
    711 	int matchwild = 3, wildcard;
    712 	u_int16_t lport = lport_arg;
    713 
    714 	head = INPCBHASH_PORT(table, lport);
    715 	LIST_FOREACH(inph, head, inph_lhash) {
    716 		inp = (struct inpcb *)inph;
    717 		if (inp->inp_af != AF_INET)
    718 			continue;
    719 
    720 		if (inp->inp_lport != lport)
    721 			continue;
    722 		wildcard = 0;
    723 		if (!in_nullhost(inp->inp_faddr))
    724 			wildcard++;
    725 		if (in_nullhost(inp->inp_laddr)) {
    726 			if (!in_nullhost(laddr))
    727 				wildcard++;
    728 		} else {
    729 			if (in_nullhost(laddr))
    730 				wildcard++;
    731 			else {
    732 				if (!in_hosteq(inp->inp_laddr, laddr))
    733 					continue;
    734 			}
    735 		}
    736 		if (wildcard && !lookup_wildcard)
    737 			continue;
    738 		if (wildcard < matchwild) {
    739 			match = inp;
    740 			matchwild = wildcard;
    741 			if (matchwild == 0)
    742 				break;
    743 		}
    744 	}
    745 	return (match);
    746 }
    747 
    748 #ifdef DIAGNOSTIC
    749 int	in_pcbnotifymiss = 0;
    750 #endif
    751 
    752 struct inpcb *
    753 in_pcblookup_connect(struct inpcbtable *table,
    754     struct in_addr faddr, u_int fport_arg,
    755     struct in_addr laddr, u_int lport_arg)
    756 {
    757 	struct inpcbhead *head;
    758 	struct inpcb_hdr *inph;
    759 	struct inpcb *inp;
    760 	u_int16_t fport = fport_arg, lport = lport_arg;
    761 
    762 	head = INPCBHASH_CONNECT(table, faddr, fport, laddr, lport);
    763 	LIST_FOREACH(inph, head, inph_hash) {
    764 		inp = (struct inpcb *)inph;
    765 		if (inp->inp_af != AF_INET)
    766 			continue;
    767 
    768 		if (in_hosteq(inp->inp_faddr, faddr) &&
    769 		    inp->inp_fport == fport &&
    770 		    inp->inp_lport == lport &&
    771 		    in_hosteq(inp->inp_laddr, laddr))
    772 			goto out;
    773 	}
    774 #ifdef DIAGNOSTIC
    775 	if (in_pcbnotifymiss) {
    776 		printf("in_pcblookup_connect: faddr=%08x fport=%d laddr=%08x lport=%d\n",
    777 		    ntohl(faddr.s_addr), ntohs(fport),
    778 		    ntohl(laddr.s_addr), ntohs(lport));
    779 	}
    780 #endif
    781 	return (0);
    782 
    783 out:
    784 	/* Move this PCB to the head of hash chain. */
    785 	inph = &inp->inp_head;
    786 	if (inph != LIST_FIRST(head)) {
    787 		LIST_REMOVE(inph, inph_hash);
    788 		LIST_INSERT_HEAD(head, inph, inph_hash);
    789 	}
    790 	return (inp);
    791 }
    792 
    793 struct inpcb *
    794 in_pcblookup_bind(struct inpcbtable *table,
    795     struct in_addr laddr, u_int lport_arg)
    796 {
    797 	struct inpcbhead *head;
    798 	struct inpcb_hdr *inph;
    799 	struct inpcb *inp;
    800 	u_int16_t lport = lport_arg;
    801 
    802 	head = INPCBHASH_BIND(table, laddr, lport);
    803 	LIST_FOREACH(inph, head, inph_hash) {
    804 		inp = (struct inpcb *)inph;
    805 		if (inp->inp_af != AF_INET)
    806 			continue;
    807 
    808 		if (inp->inp_lport == lport &&
    809 		    in_hosteq(inp->inp_laddr, laddr))
    810 			goto out;
    811 	}
    812 	head = INPCBHASH_BIND(table, zeroin_addr, lport);
    813 	LIST_FOREACH(inph, head, inph_hash) {
    814 		inp = (struct inpcb *)inph;
    815 		if (inp->inp_af != AF_INET)
    816 			continue;
    817 
    818 		if (inp->inp_lport == lport &&
    819 		    in_hosteq(inp->inp_laddr, zeroin_addr))
    820 			goto out;
    821 	}
    822 #ifdef DIAGNOSTIC
    823 	if (in_pcbnotifymiss) {
    824 		printf("in_pcblookup_bind: laddr=%08x lport=%d\n",
    825 		    ntohl(laddr.s_addr), ntohs(lport));
    826 	}
    827 #endif
    828 	return (0);
    829 
    830 out:
    831 	/* Move this PCB to the head of hash chain. */
    832 	inph = &inp->inp_head;
    833 	if (inph != LIST_FIRST(head)) {
    834 		LIST_REMOVE(inph, inph_hash);
    835 		LIST_INSERT_HEAD(head, inph, inph_hash);
    836 	}
    837 	return (inp);
    838 }
    839 
    840 void
    841 in_pcbstate(struct inpcb *inp, int state)
    842 {
    843 
    844 	if (inp->inp_af != AF_INET)
    845 		return;
    846 
    847 	if (inp->inp_state > INP_ATTACHED)
    848 		LIST_REMOVE(&inp->inp_head, inph_hash);
    849 
    850 	switch (state) {
    851 	case INP_BOUND:
    852 		LIST_INSERT_HEAD(INPCBHASH_BIND(inp->inp_table,
    853 		    inp->inp_laddr, inp->inp_lport), &inp->inp_head,
    854 		    inph_hash);
    855 		break;
    856 	case INP_CONNECTED:
    857 		LIST_INSERT_HEAD(INPCBHASH_CONNECT(inp->inp_table,
    858 		    inp->inp_faddr, inp->inp_fport,
    859 		    inp->inp_laddr, inp->inp_lport), &inp->inp_head,
    860 		    inph_hash);
    861 		break;
    862 	}
    863 
    864 	inp->inp_state = state;
    865 }
    866 
    867 struct rtentry *
    868 in_pcbrtentry(struct inpcb *inp)
    869 {
    870 	struct route *ro;
    871 	union {
    872 		struct sockaddr		dst;
    873 		struct sockaddr_in	dst4;
    874 	} u;
    875 
    876 	if (inp->inp_af != AF_INET)
    877 		return (NULL);
    878 
    879 	ro = &inp->inp_route;
    880 
    881 	sockaddr_in_init(&u.dst4, &inp->inp_faddr, 0);
    882 	return rtcache_lookup(ro, &u.dst);
    883 }
    884 
    885 struct sockaddr_in *
    886 in_selectsrc(struct sockaddr_in *sin, struct route *ro,
    887     int soopts, struct ip_moptions *mopts, int *errorp)
    888 {
    889 	struct in_ifaddr *ia = NULL;
    890 
    891 	/*
    892          * If route is known or can be allocated now, take the
    893          * source address from the interface.  Otherwise, punt.
    894 	 */
    895 	if ((soopts & SO_DONTROUTE) != 0)
    896 		rtcache_free(ro);
    897 	else {
    898 		union {
    899 			struct sockaddr		dst;
    900 			struct sockaddr_in	dst4;
    901 		} u;
    902 
    903 		sockaddr_in_init(&u.dst4, &sin->sin_addr, 0);
    904 		(void)rtcache_lookup(ro, &u.dst);
    905 	}
    906 	/*
    907 	 * If we found a route, use the address
    908 	 * corresponding to the outgoing interface
    909 	 * unless it is the loopback (in case a route
    910 	 * to our address on another net goes to loopback).
    911 	 *
    912 	 * XXX Is this still true?  Do we care?
    913 	 */
    914 	if (ro->ro_rt != NULL && !(ro->ro_rt->rt_ifp->if_flags & IFF_LOOPBACK))
    915 		ia = ifatoia(ro->ro_rt->rt_ifa);
    916 	if (ia == NULL) {
    917 		u_int16_t fport = sin->sin_port;
    918 
    919 		sin->sin_port = 0;
    920 		ia = ifatoia(ifa_ifwithladdr(sintosa(sin)));
    921 		sin->sin_port = fport;
    922 		if (ia == NULL) {
    923 			/* Find 1st non-loopback AF_INET address */
    924 			TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) {
    925 				if (!(ia->ia_ifp->if_flags & IFF_LOOPBACK))
    926 					break;
    927 			}
    928 		}
    929 		if (ia == NULL) {
    930 			*errorp = EADDRNOTAVAIL;
    931 			return NULL;
    932 		}
    933 	}
    934 	/*
    935 	 * If the destination address is multicast and an outgoing
    936 	 * interface has been set as a multicast option, use the
    937 	 * address of that interface as our source address.
    938 	 */
    939 	if (IN_MULTICAST(sin->sin_addr.s_addr) && mopts != NULL) {
    940 		struct ip_moptions *imo;
    941 		struct ifnet *ifp;
    942 
    943 		imo = mopts;
    944 		if (imo->imo_multicast_ifp != NULL) {
    945 			ifp = imo->imo_multicast_ifp;
    946 			IFP_TO_IA(ifp, ia);		/* XXX */
    947 			if (ia == 0) {
    948 				*errorp = EADDRNOTAVAIL;
    949 				return NULL;
    950 			}
    951 		}
    952 	}
    953 	if (ia->ia_ifa.ifa_getifa != NULL) {
    954 		ia = ifatoia((*ia->ia_ifa.ifa_getifa)(&ia->ia_ifa,
    955 		                                      sintosa(sin)));
    956 	}
    957 #ifdef GETIFA_DEBUG
    958 	else
    959 		printf("%s: missing ifa_getifa\n", __func__);
    960 #endif
    961 	return satosin(&ia->ia_addr);
    962 }
    963