Home | History | Annotate | Line # | Download | only in netinet
in_pcb.c revision 1.105
      1 /*	$NetBSD: in_pcb.c,v 1.105 2006/09/19 21:42:30 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.105 2006/09/19 21:42:30 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 
    163 void
    164 in_pcbinit(struct inpcbtable *table, int bindhashsize, int connecthashsize)
    165 {
    166 
    167 	CIRCLEQ_INIT(&table->inpt_queue);
    168 	table->inpt_porthashtbl = hashinit(bindhashsize, HASH_LIST, M_PCB,
    169 	    M_WAITOK, &table->inpt_porthash);
    170 	table->inpt_bindhashtbl = hashinit(bindhashsize, HASH_LIST, M_PCB,
    171 	    M_WAITOK, &table->inpt_bindhash);
    172 	table->inpt_connecthashtbl = hashinit(connecthashsize, HASH_LIST,
    173 	    M_PCB, M_WAITOK, &table->inpt_connecthash);
    174 	table->inpt_lastlow = IPPORT_RESERVEDMAX;
    175 	table->inpt_lastport = (u_int16_t)anonportmax;
    176 }
    177 
    178 int
    179 in_pcballoc(struct socket *so, void *v)
    180 {
    181 	struct inpcbtable *table = v;
    182 	struct inpcb *inp;
    183 	int s;
    184 #if defined(IPSEC) || defined(FAST_IPSEC)
    185 	int error;
    186 #endif
    187 
    188 	inp = pool_get(&inpcb_pool, PR_NOWAIT);
    189 	if (inp == NULL)
    190 		return (ENOBUFS);
    191 	bzero((caddr_t)inp, sizeof(*inp));
    192 	inp->inp_af = AF_INET;
    193 	inp->inp_table = table;
    194 	inp->inp_socket = so;
    195 	inp->inp_errormtu = -1;
    196 #if defined(IPSEC) || defined(FAST_IPSEC)
    197 	error = ipsec_init_pcbpolicy(so, &inp->inp_sp);
    198 	if (error != 0) {
    199 		pool_put(&inpcb_pool, inp);
    200 		return error;
    201 	}
    202 #endif
    203 	so->so_pcb = inp;
    204 	s = splnet();
    205 	CIRCLEQ_INSERT_HEAD(&table->inpt_queue, &inp->inp_head,
    206 	    inph_queue);
    207 	LIST_INSERT_HEAD(INPCBHASH_PORT(table, inp->inp_lport), &inp->inp_head,
    208 	    inph_lhash);
    209 	in_pcbstate(inp, INP_ATTACHED);
    210 	splx(s);
    211 	return (0);
    212 }
    213 
    214 int
    215 in_pcbbind(void *v, struct mbuf *nam, struct lwp *l)
    216 {
    217 	struct in_ifaddr *ia = NULL;
    218 	struct inpcb *inp = v;
    219 	struct socket *so = inp->inp_socket;
    220 	struct inpcbtable *table = inp->inp_table;
    221 	struct sockaddr_in *sin = NULL; /* XXXGCC */
    222 	u_int16_t lport = 0;
    223 	int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
    224 
    225 	if (inp->inp_af != AF_INET)
    226 		return (EINVAL);
    227 
    228 	if (TAILQ_FIRST(&in_ifaddrhead) == 0)
    229 		return (EADDRNOTAVAIL);
    230 	if (inp->inp_lport || !in_nullhost(inp->inp_laddr))
    231 		return (EINVAL);
    232 	if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
    233 		wild = 1;
    234 	if (nam == 0)
    235 		goto noname;
    236 	sin = mtod(nam, struct sockaddr_in *);
    237 	if (nam->m_len != sizeof (*sin))
    238 		return (EINVAL);
    239 	if (sin->sin_family != AF_INET)
    240 		return (EAFNOSUPPORT);
    241 	lport = sin->sin_port;
    242 	if (IN_MULTICAST(sin->sin_addr.s_addr)) {
    243 		/*
    244 		 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
    245 		 * allow complete duplication of binding if
    246 		 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
    247 		 * and a multicast address is bound on both
    248 		 * new and duplicated sockets.
    249 		 */
    250 		if (so->so_options & SO_REUSEADDR)
    251 			reuseport = SO_REUSEADDR|SO_REUSEPORT;
    252 	} else if (!in_nullhost(sin->sin_addr)) {
    253 		sin->sin_port = 0;		/* yech... */
    254 		INADDR_TO_IA(sin->sin_addr, ia);
    255 		/* check for broadcast addresses */
    256 		if (ia == NULL)
    257 			ia = ifatoia(ifa_ifwithaddr(sintosa(sin)));
    258 		if (ia == NULL)
    259 			return (EADDRNOTAVAIL);
    260 	}
    261 	if (lport) {
    262 		struct inpcb *t;
    263 #ifdef INET6
    264 		struct in6pcb *t6;
    265 		struct in6_addr mapped;
    266 #endif
    267 #ifndef IPNOPRIVPORTS
    268 		/* GROSS */
    269 		if (ntohs(lport) < IPPORT_RESERVED &&
    270 		    (l == 0 || kauth_authorize_network(l->l_cred,
    271 		    KAUTH_NETWORK_BIND,
    272 		    KAUTH_REQ_NETWORK_BIND_PRIVPORT, so, sin,
    273 		    NULL)))
    274 			return (EACCES);
    275 #endif
    276 #ifdef INET6
    277 		memset(&mapped, 0, sizeof(mapped));
    278 		mapped.s6_addr16[5] = 0xffff;
    279 		memcpy(&mapped.s6_addr32[3], &sin->sin_addr,
    280 		    sizeof(mapped.s6_addr32[3]));
    281 		t6 = in6_pcblookup_port(table, &mapped, lport, wild);
    282 		if (t6 && (reuseport & t6->in6p_socket->so_options) == 0)
    283 			return (EADDRINUSE);
    284 #endif
    285 		if (so->so_uidinfo->ui_uid && !IN_MULTICAST(sin->sin_addr.s_addr)) {
    286 			t = in_pcblookup_port(table, sin->sin_addr, lport, 1);
    287 		/*
    288 		 * XXX:	investigate ramifications of loosening this
    289 		 *	restriction so that as long as both ports have
    290 		 *	SO_REUSEPORT allow the bind
    291 		 */
    292 			if (t &&
    293 			    (!in_nullhost(sin->sin_addr) ||
    294 			     !in_nullhost(t->inp_laddr) ||
    295 			     (t->inp_socket->so_options & SO_REUSEPORT) == 0)
    296 			    && (so->so_uidinfo->ui_uid != t->inp_socket->so_uidinfo->ui_uid)) {
    297 				return (EADDRINUSE);
    298 			}
    299 		}
    300 		t = in_pcblookup_port(table, sin->sin_addr, lport, wild);
    301 		if (t && (reuseport & t->inp_socket->so_options) == 0)
    302 			return (EADDRINUSE);
    303 	}
    304 	inp->inp_laddr = sin->sin_addr;
    305 
    306 noname:
    307 	if (lport == 0) {
    308 		int	   cnt;
    309 		u_int16_t  mymin, mymax;
    310 		u_int16_t *lastport;
    311 
    312 		if (inp->inp_flags & INP_LOWPORT) {
    313 #ifndef IPNOPRIVPORTS
    314 			if (l == 0 || kauth_authorize_network(l->l_cred,
    315 			    KAUTH_NETWORK_BIND,
    316 			    KAUTH_REQ_NETWORK_BIND_PRIVPORT, so,
    317 			    sin, NULL))
    318 				return (EACCES);
    319 #endif
    320 			mymin = lowportmin;
    321 			mymax = lowportmax;
    322 			lastport = &table->inpt_lastlow;
    323 		} else {
    324 			mymin = anonportmin;
    325 			mymax = anonportmax;
    326 			lastport = &table->inpt_lastport;
    327 		}
    328 		if (mymin > mymax) {	/* sanity check */
    329 			u_int16_t swp;
    330 
    331 			swp = mymin;
    332 			mymin = mymax;
    333 			mymax = swp;
    334 		}
    335 
    336 		lport = *lastport - 1;
    337 		for (cnt = mymax - mymin + 1; cnt; cnt--, lport--) {
    338 			if (lport < mymin || lport > mymax)
    339 				lport = mymax;
    340 			if (!in_pcblookup_port(table, inp->inp_laddr,
    341 			    htons(lport), 1))
    342 				goto found;
    343 		}
    344 		if (!in_nullhost(inp->inp_laddr))
    345 			inp->inp_laddr.s_addr = INADDR_ANY;
    346 		return (EAGAIN);
    347 	found:
    348 		inp->inp_flags |= INP_ANONPORT;
    349 		*lastport = lport;
    350 		lport = htons(lport);
    351 	}
    352 	inp->inp_lport = lport;
    353 	LIST_REMOVE(&inp->inp_head, inph_lhash);
    354 	LIST_INSERT_HEAD(INPCBHASH_PORT(table, inp->inp_lport), &inp->inp_head,
    355 	    inph_lhash);
    356 	in_pcbstate(inp, INP_BOUND);
    357 	return (0);
    358 }
    359 
    360 /*
    361  * Connect from a socket to a specified address.
    362  * Both address and port must be specified in argument sin.
    363  * If don't have a local address for this socket yet,
    364  * then pick one.
    365  */
    366 int
    367 in_pcbconnect(void *v, struct mbuf *nam, struct lwp *l)
    368 {
    369 	struct inpcb *inp = v;
    370 	struct in_ifaddr *ia = NULL;
    371 	struct sockaddr_in *ifaddr = NULL;
    372 	struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *);
    373 	int error;
    374 
    375 	if (inp->inp_af != AF_INET)
    376 		return (EINVAL);
    377 
    378 	if (nam->m_len != sizeof (*sin))
    379 		return (EINVAL);
    380 	if (sin->sin_family != AF_INET)
    381 		return (EAFNOSUPPORT);
    382 	if (sin->sin_port == 0)
    383 		return (EADDRNOTAVAIL);
    384 	if (TAILQ_FIRST(&in_ifaddrhead) != 0) {
    385 		/*
    386 		 * If the destination address is INADDR_ANY,
    387 		 * use any local address (likely loopback).
    388 		 * If the supplied address is INADDR_BROADCAST,
    389 		 * use the broadcast address of an interface
    390 		 * which supports broadcast. (loopback does not)
    391 		 */
    392 
    393 		if (in_nullhost(sin->sin_addr)) {
    394 			sin->sin_addr =
    395 			    TAILQ_FIRST(&in_ifaddrhead)->ia_addr.sin_addr;
    396 		} else if (sin->sin_addr.s_addr == INADDR_BROADCAST) {
    397 			TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) {
    398 				if (ia->ia_ifp->if_flags & IFF_BROADCAST) {
    399 					sin->sin_addr =
    400 					    ia->ia_broadaddr.sin_addr;
    401 					break;
    402 				}
    403 			}
    404 		}
    405 	}
    406 	/*
    407 	 * If we haven't bound which network number to use as ours,
    408 	 * we will use the number of the outgoing interface.
    409 	 * This depends on having done a routing lookup, which
    410 	 * we will probably have to do anyway, so we might
    411 	 * as well do it now.  On the other hand if we are
    412 	 * sending to multiple destinations we may have already
    413 	 * done the lookup, so see if we can use the route
    414 	 * from before.  In any case, we only
    415 	 * chose a port number once, even if sending to multiple
    416 	 * destinations.
    417 	 */
    418 	if (in_nullhost(inp->inp_laddr)) {
    419 		int xerror;
    420 		ifaddr = in_selectsrc(sin, &inp->inp_route,
    421 		    inp->inp_socket->so_options, inp->inp_moptions, &xerror);
    422 		if (ifaddr == NULL) {
    423 			if (xerror == 0)
    424 				xerror = EADDRNOTAVAIL;
    425 			return xerror;
    426 		}
    427 		INADDR_TO_IA(ifaddr->sin_addr, ia);
    428 		if (ia == NULL)
    429 			return (EADDRNOTAVAIL);
    430 	}
    431 	if (in_pcblookup_connect(inp->inp_table, sin->sin_addr, sin->sin_port,
    432 	    !in_nullhost(inp->inp_laddr) ? inp->inp_laddr : ifaddr->sin_addr,
    433 	    inp->inp_lport) != 0)
    434 		return (EADDRINUSE);
    435 	if (in_nullhost(inp->inp_laddr)) {
    436 		if (inp->inp_lport == 0) {
    437 			error = in_pcbbind(inp, NULL, l);
    438 			/*
    439 			 * This used to ignore the return value
    440 			 * completely, but we need to check for
    441 			 * ephemeral port shortage.
    442 			 * And attempts to request low ports if not root.
    443 			 */
    444 			if (error != 0)
    445 				return (error);
    446 		}
    447 		inp->inp_laddr = ifaddr->sin_addr;
    448 	}
    449 	inp->inp_faddr = sin->sin_addr;
    450 	inp->inp_fport = sin->sin_port;
    451 	in_pcbstate(inp, INP_CONNECTED);
    452 #if defined(IPSEC) || defined(FAST_IPSEC)
    453 	if (inp->inp_socket->so_type == SOCK_STREAM)
    454 		ipsec_pcbconn(inp->inp_sp);
    455 #endif
    456 	return (0);
    457 }
    458 
    459 void
    460 in_pcbdisconnect(void *v)
    461 {
    462 	struct inpcb *inp = v;
    463 
    464 	if (inp->inp_af != AF_INET)
    465 		return;
    466 
    467 	inp->inp_faddr = zeroin_addr;
    468 	inp->inp_fport = 0;
    469 	in_pcbstate(inp, INP_BOUND);
    470 #if defined(IPSEC) || defined(FAST_IPSEC)
    471 	ipsec_pcbdisconn(inp->inp_sp);
    472 #endif
    473 	if (inp->inp_socket->so_state & SS_NOFDREF)
    474 		in_pcbdetach(inp);
    475 }
    476 
    477 void
    478 in_pcbdetach(void *v)
    479 {
    480 	struct inpcb *inp = v;
    481 	struct socket *so = inp->inp_socket;
    482 	int s;
    483 
    484 	if (inp->inp_af != AF_INET)
    485 		return;
    486 
    487 #if defined(IPSEC) || defined(FAST_IPSEC)
    488 	ipsec4_delete_pcbpolicy(inp);
    489 #endif /*IPSEC*/
    490 	so->so_pcb = 0;
    491 	sofree(so);
    492 	if (inp->inp_options)
    493 		(void)m_free(inp->inp_options);
    494 	if (inp->inp_route.ro_rt)
    495 		rtfree(inp->inp_route.ro_rt);
    496 	ip_freemoptions(inp->inp_moptions);
    497 	s = splnet();
    498 	in_pcbstate(inp, INP_ATTACHED);
    499 	LIST_REMOVE(&inp->inp_head, inph_lhash);
    500 	CIRCLEQ_REMOVE(&inp->inp_table->inpt_queue, &inp->inp_head,
    501 	    inph_queue);
    502 	splx(s);
    503 	pool_put(&inpcb_pool, inp);
    504 }
    505 
    506 void
    507 in_setsockaddr(struct inpcb *inp, struct mbuf *nam)
    508 {
    509 	struct sockaddr_in *sin;
    510 
    511 	if (inp->inp_af != AF_INET)
    512 		return;
    513 
    514 	nam->m_len = sizeof (*sin);
    515 	sin = mtod(nam, struct sockaddr_in *);
    516 	bzero((caddr_t)sin, sizeof (*sin));
    517 	sin->sin_family = AF_INET;
    518 	sin->sin_len = sizeof(*sin);
    519 	sin->sin_port = inp->inp_lport;
    520 	sin->sin_addr = inp->inp_laddr;
    521 }
    522 
    523 void
    524 in_setpeeraddr(struct inpcb *inp, struct mbuf *nam)
    525 {
    526 	struct sockaddr_in *sin;
    527 
    528 	if (inp->inp_af != AF_INET)
    529 		return;
    530 
    531 	nam->m_len = sizeof (*sin);
    532 	sin = mtod(nam, struct sockaddr_in *);
    533 	bzero((caddr_t)sin, sizeof (*sin));
    534 	sin->sin_family = AF_INET;
    535 	sin->sin_len = sizeof(*sin);
    536 	sin->sin_port = inp->inp_fport;
    537 	sin->sin_addr = inp->inp_faddr;
    538 }
    539 
    540 /*
    541  * Pass some notification to all connections of a protocol
    542  * associated with address dst.  The local address and/or port numbers
    543  * may be specified to limit the search.  The "usual action" will be
    544  * taken, depending on the ctlinput cmd.  The caller must filter any
    545  * cmds that are uninteresting (e.g., no error in the map).
    546  * Call the protocol specific routine (if any) to report
    547  * any errors for each matching socket.
    548  *
    549  * Must be called at splsoftnet.
    550  */
    551 int
    552 in_pcbnotify(struct inpcbtable *table, struct in_addr faddr, u_int fport_arg,
    553     struct in_addr laddr, u_int lport_arg, int errno,
    554     void (*notify)(struct inpcb *, int))
    555 {
    556 	struct inpcbhead *head;
    557 	struct inpcb *inp, *ninp;
    558 	u_int16_t fport = fport_arg, lport = lport_arg;
    559 	int nmatch;
    560 
    561 	if (in_nullhost(faddr) || notify == 0)
    562 		return (0);
    563 
    564 	nmatch = 0;
    565 	head = INPCBHASH_CONNECT(table, faddr, fport, laddr, lport);
    566 	for (inp = (struct inpcb *)LIST_FIRST(head); inp != NULL; inp = ninp) {
    567 		ninp = (struct inpcb *)LIST_NEXT(inp, inp_hash);
    568 		if (inp->inp_af != AF_INET)
    569 			continue;
    570 		if (in_hosteq(inp->inp_faddr, faddr) &&
    571 		    inp->inp_fport == fport &&
    572 		    inp->inp_lport == lport &&
    573 		    in_hosteq(inp->inp_laddr, laddr)) {
    574 			(*notify)(inp, errno);
    575 			nmatch++;
    576 		}
    577 	}
    578 	return (nmatch);
    579 }
    580 
    581 void
    582 in_pcbnotifyall(struct inpcbtable *table, struct in_addr faddr, int errno,
    583     void (*notify)(struct inpcb *, int))
    584 {
    585 	struct inpcb *inp, *ninp;
    586 
    587 	if (in_nullhost(faddr) || notify == 0)
    588 		return;
    589 
    590 	for (inp = (struct inpcb *)CIRCLEQ_FIRST(&table->inpt_queue);
    591 	    inp != (void *)&table->inpt_queue;
    592 	    inp = ninp) {
    593 		ninp = (struct inpcb *)CIRCLEQ_NEXT(inp, inp_queue);
    594 		if (inp->inp_af != AF_INET)
    595 			continue;
    596 		if (in_hosteq(inp->inp_faddr, faddr))
    597 			(*notify)(inp, errno);
    598 	}
    599 }
    600 
    601 void
    602 in_pcbpurgeif0(struct inpcbtable *table, struct ifnet *ifp)
    603 {
    604 	struct inpcb *inp, *ninp;
    605 	struct ip_moptions *imo;
    606 	int i, gap;
    607 
    608 	for (inp = (struct inpcb *)CIRCLEQ_FIRST(&table->inpt_queue);
    609 	    inp != (void *)&table->inpt_queue;
    610 	    inp = ninp) {
    611 		ninp = (struct inpcb *)CIRCLEQ_NEXT(inp, inp_queue);
    612 		if (inp->inp_af != AF_INET)
    613 			continue;
    614 		imo = inp->inp_moptions;
    615 		if (imo != NULL) {
    616 			/*
    617 			 * Unselect the outgoing interface if it is being
    618 			 * detached.
    619 			 */
    620 			if (imo->imo_multicast_ifp == ifp)
    621 				imo->imo_multicast_ifp = NULL;
    622 
    623 			/*
    624 			 * Drop multicast group membership if we joined
    625 			 * through the interface being detached.
    626 			 */
    627 			for (i = 0, gap = 0; i < imo->imo_num_memberships;
    628 			    i++) {
    629 				if (imo->imo_membership[i]->inm_ifp == ifp) {
    630 					in_delmulti(imo->imo_membership[i]);
    631 					gap++;
    632 				} else if (gap != 0)
    633 					imo->imo_membership[i - gap] =
    634 					    imo->imo_membership[i];
    635 			}
    636 			imo->imo_num_memberships -= gap;
    637 		}
    638 	}
    639 }
    640 
    641 void
    642 in_pcbpurgeif(struct inpcbtable *table, struct ifnet *ifp)
    643 {
    644 	struct inpcb *inp, *ninp;
    645 
    646 	for (inp = (struct inpcb *)CIRCLEQ_FIRST(&table->inpt_queue);
    647 	    inp != (void *)&table->inpt_queue;
    648 	    inp = ninp) {
    649 		ninp = (struct inpcb *)CIRCLEQ_NEXT(inp, inp_queue);
    650 		if (inp->inp_af != AF_INET)
    651 			continue;
    652 		if (inp->inp_route.ro_rt != NULL &&
    653 		    inp->inp_route.ro_rt->rt_ifp == ifp)
    654 			in_rtchange(inp, 0);
    655 	}
    656 }
    657 
    658 /*
    659  * Check for alternatives when higher level complains
    660  * about service problems.  For now, invalidate cached
    661  * routing information.  If the route was created dynamically
    662  * (by a redirect), time to try a default gateway again.
    663  */
    664 void
    665 in_losing(struct inpcb *inp)
    666 {
    667 	struct rtentry *rt;
    668 	struct rt_addrinfo info;
    669 
    670 	if (inp->inp_af != AF_INET)
    671 		return;
    672 
    673 	if ((rt = inp->inp_route.ro_rt)) {
    674 		inp->inp_route.ro_rt = 0;
    675 		bzero((caddr_t)&info, sizeof(info));
    676 		info.rti_info[RTAX_DST] = &inp->inp_route.ro_dst;
    677 		info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
    678 		info.rti_info[RTAX_NETMASK] = rt_mask(rt);
    679 		rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
    680 		if (rt->rt_flags & RTF_DYNAMIC)
    681 			(void) rtrequest(RTM_DELETE, rt_key(rt),
    682 				rt->rt_gateway, rt_mask(rt), rt->rt_flags,
    683 				(struct rtentry **)0);
    684 		else
    685 		/*
    686 		 * A new route can be allocated
    687 		 * the next time output is attempted.
    688 		 */
    689 			rtfree(rt);
    690 	}
    691 }
    692 
    693 /*
    694  * After a routing change, flush old routing
    695  * and allocate a (hopefully) better one.
    696  */
    697 void
    698 in_rtchange(struct inpcb *inp, int errno)
    699 {
    700 
    701 	if (inp->inp_af != AF_INET)
    702 		return;
    703 
    704 	if (inp->inp_route.ro_rt) {
    705 		rtfree(inp->inp_route.ro_rt);
    706 		inp->inp_route.ro_rt = 0;
    707 		/*
    708 		 * A new route can be allocated the next time
    709 		 * output is attempted.
    710 		 */
    711 	}
    712 	/* XXX SHOULD NOTIFY HIGHER-LEVEL PROTOCOLS */
    713 }
    714 
    715 struct inpcb *
    716 in_pcblookup_port(struct inpcbtable *table, struct in_addr laddr,
    717     u_int lport_arg, int lookup_wildcard)
    718 {
    719 	struct inpcbhead *head;
    720 	struct inpcb_hdr *inph;
    721 	struct inpcb *inp, *match = 0;
    722 	int matchwild = 3, wildcard;
    723 	u_int16_t lport = lport_arg;
    724 
    725 	head = INPCBHASH_PORT(table, lport);
    726 	LIST_FOREACH(inph, head, inph_lhash) {
    727 		inp = (struct inpcb *)inph;
    728 		if (inp->inp_af != AF_INET)
    729 			continue;
    730 
    731 		if (inp->inp_lport != lport)
    732 			continue;
    733 		wildcard = 0;
    734 		if (!in_nullhost(inp->inp_faddr))
    735 			wildcard++;
    736 		if (in_nullhost(inp->inp_laddr)) {
    737 			if (!in_nullhost(laddr))
    738 				wildcard++;
    739 		} else {
    740 			if (in_nullhost(laddr))
    741 				wildcard++;
    742 			else {
    743 				if (!in_hosteq(inp->inp_laddr, laddr))
    744 					continue;
    745 			}
    746 		}
    747 		if (wildcard && !lookup_wildcard)
    748 			continue;
    749 		if (wildcard < matchwild) {
    750 			match = inp;
    751 			matchwild = wildcard;
    752 			if (matchwild == 0)
    753 				break;
    754 		}
    755 	}
    756 	return (match);
    757 }
    758 
    759 #ifdef DIAGNOSTIC
    760 int	in_pcbnotifymiss = 0;
    761 #endif
    762 
    763 struct inpcb *
    764 in_pcblookup_connect(struct inpcbtable *table,
    765     struct in_addr faddr, u_int fport_arg,
    766     struct in_addr laddr, u_int lport_arg)
    767 {
    768 	struct inpcbhead *head;
    769 	struct inpcb_hdr *inph;
    770 	struct inpcb *inp;
    771 	u_int16_t fport = fport_arg, lport = lport_arg;
    772 
    773 	head = INPCBHASH_CONNECT(table, faddr, fport, laddr, lport);
    774 	LIST_FOREACH(inph, head, inph_hash) {
    775 		inp = (struct inpcb *)inph;
    776 		if (inp->inp_af != AF_INET)
    777 			continue;
    778 
    779 		if (in_hosteq(inp->inp_faddr, faddr) &&
    780 		    inp->inp_fport == fport &&
    781 		    inp->inp_lport == lport &&
    782 		    in_hosteq(inp->inp_laddr, laddr))
    783 			goto out;
    784 	}
    785 #ifdef DIAGNOSTIC
    786 	if (in_pcbnotifymiss) {
    787 		printf("in_pcblookup_connect: faddr=%08x fport=%d laddr=%08x lport=%d\n",
    788 		    ntohl(faddr.s_addr), ntohs(fport),
    789 		    ntohl(laddr.s_addr), ntohs(lport));
    790 	}
    791 #endif
    792 	return (0);
    793 
    794 out:
    795 	/* Move this PCB to the head of hash chain. */
    796 	inph = &inp->inp_head;
    797 	if (inph != LIST_FIRST(head)) {
    798 		LIST_REMOVE(inph, inph_hash);
    799 		LIST_INSERT_HEAD(head, inph, inph_hash);
    800 	}
    801 	return (inp);
    802 }
    803 
    804 struct inpcb *
    805 in_pcblookup_bind(struct inpcbtable *table,
    806     struct in_addr laddr, u_int lport_arg)
    807 {
    808 	struct inpcbhead *head;
    809 	struct inpcb_hdr *inph;
    810 	struct inpcb *inp;
    811 	u_int16_t lport = lport_arg;
    812 
    813 	head = INPCBHASH_BIND(table, laddr, lport);
    814 	LIST_FOREACH(inph, head, inph_hash) {
    815 		inp = (struct inpcb *)inph;
    816 		if (inp->inp_af != AF_INET)
    817 			continue;
    818 
    819 		if (inp->inp_lport == lport &&
    820 		    in_hosteq(inp->inp_laddr, laddr))
    821 			goto out;
    822 	}
    823 	head = INPCBHASH_BIND(table, zeroin_addr, lport);
    824 	LIST_FOREACH(inph, head, inph_hash) {
    825 		inp = (struct inpcb *)inph;
    826 		if (inp->inp_af != AF_INET)
    827 			continue;
    828 
    829 		if (inp->inp_lport == lport &&
    830 		    in_hosteq(inp->inp_laddr, zeroin_addr))
    831 			goto out;
    832 	}
    833 #ifdef DIAGNOSTIC
    834 	if (in_pcbnotifymiss) {
    835 		printf("in_pcblookup_bind: laddr=%08x lport=%d\n",
    836 		    ntohl(laddr.s_addr), ntohs(lport));
    837 	}
    838 #endif
    839 	return (0);
    840 
    841 out:
    842 	/* Move this PCB to the head of hash chain. */
    843 	inph = &inp->inp_head;
    844 	if (inph != LIST_FIRST(head)) {
    845 		LIST_REMOVE(inph, inph_hash);
    846 		LIST_INSERT_HEAD(head, inph, inph_hash);
    847 	}
    848 	return (inp);
    849 }
    850 
    851 void
    852 in_pcbstate(struct inpcb *inp, int state)
    853 {
    854 
    855 	if (inp->inp_af != AF_INET)
    856 		return;
    857 
    858 	if (inp->inp_state > INP_ATTACHED)
    859 		LIST_REMOVE(&inp->inp_head, inph_hash);
    860 
    861 	switch (state) {
    862 	case INP_BOUND:
    863 		LIST_INSERT_HEAD(INPCBHASH_BIND(inp->inp_table,
    864 		    inp->inp_laddr, inp->inp_lport), &inp->inp_head,
    865 		    inph_hash);
    866 		break;
    867 	case INP_CONNECTED:
    868 		LIST_INSERT_HEAD(INPCBHASH_CONNECT(inp->inp_table,
    869 		    inp->inp_faddr, inp->inp_fport,
    870 		    inp->inp_laddr, inp->inp_lport), &inp->inp_head,
    871 		    inph_hash);
    872 		break;
    873 	}
    874 
    875 	inp->inp_state = state;
    876 }
    877 
    878 struct rtentry *
    879 in_pcbrtentry(struct inpcb *inp)
    880 {
    881 	struct route *ro;
    882 
    883 	if (inp->inp_af != AF_INET)
    884 		return (NULL);
    885 
    886 	ro = &inp->inp_route;
    887 
    888 	if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
    889 	    !in_hosteq(satosin(&ro->ro_dst)->sin_addr, inp->inp_faddr))) {
    890 		RTFREE(ro->ro_rt);
    891 		ro->ro_rt = (struct rtentry *)NULL;
    892 	}
    893 	if (ro->ro_rt == (struct rtentry *)NULL &&
    894 	    !in_nullhost(inp->inp_faddr)) {
    895 		bzero(&ro->ro_dst, sizeof(struct sockaddr_in));
    896 		ro->ro_dst.sa_family = AF_INET;
    897 		ro->ro_dst.sa_len = sizeof(ro->ro_dst);
    898 		satosin(&ro->ro_dst)->sin_addr = inp->inp_faddr;
    899 		rtalloc(ro);
    900 	}
    901 	return (ro->ro_rt);
    902 }
    903 
    904 struct sockaddr_in *
    905 in_selectsrc(struct sockaddr_in *sin, struct route *ro,
    906     int soopts, struct ip_moptions *mopts, int *errorp)
    907 {
    908 	struct in_ifaddr *ia;
    909 
    910 	ia = (struct in_ifaddr *)0;
    911 	/*
    912 	 * If route is known or can be allocated now,
    913 	 * our src addr is taken from the i/f, else punt.
    914 	 * Note that we should check the address family of the cached
    915 	 * destination, in case of sharing the cache with IPv6.
    916 	 */
    917 	if (ro->ro_rt &&
    918 	    (ro->ro_dst.sa_family != AF_INET ||
    919 	    !in_hosteq(satosin(&ro->ro_dst)->sin_addr, sin->sin_addr) ||
    920 	    soopts & SO_DONTROUTE)) {
    921 		RTFREE(ro->ro_rt);
    922 		ro->ro_rt = (struct rtentry *)0;
    923 	}
    924 	if ((soopts & SO_DONTROUTE) == 0 && /*XXX*/
    925 	    (ro->ro_rt == (struct rtentry *)0 ||
    926 	     ro->ro_rt->rt_ifp == (struct ifnet *)0)) {
    927 		/* No route yet, so try to acquire one */
    928 		bzero(&ro->ro_dst, sizeof(struct sockaddr_in));
    929 		ro->ro_dst.sa_family = AF_INET;
    930 		ro->ro_dst.sa_len = sizeof(struct sockaddr_in);
    931 		satosin(&ro->ro_dst)->sin_addr = sin->sin_addr;
    932 		rtalloc(ro);
    933 	}
    934 	/*
    935 	 * If we found a route, use the address
    936 	 * corresponding to the outgoing interface
    937 	 * unless it is the loopback (in case a route
    938 	 * to our address on another net goes to loopback).
    939 	 *
    940 	 * XXX Is this still true?  Do we care?
    941 	 */
    942 	if (ro->ro_rt && !(ro->ro_rt->rt_ifp->if_flags & IFF_LOOPBACK))
    943 		ia = ifatoia(ro->ro_rt->rt_ifa);
    944 	if (ia == NULL) {
    945 		u_int16_t fport = sin->sin_port;
    946 
    947 		sin->sin_port = 0;
    948 		ia = ifatoia(ifa_ifwithladdr(sintosa(sin)));
    949 		sin->sin_port = fport;
    950 		if (ia == 0) {
    951 			/* Find 1st non-loopback AF_INET address */
    952 			TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) {
    953 				if (!(ia->ia_ifp->if_flags & IFF_LOOPBACK))
    954 					break;
    955 			}
    956 		}
    957 		if (ia == NULL) {
    958 			*errorp = EADDRNOTAVAIL;
    959 			return NULL;
    960 		}
    961 	}
    962 	/*
    963 	 * If the destination address is multicast and an outgoing
    964 	 * interface has been set as a multicast option, use the
    965 	 * address of that interface as our source address.
    966 	 */
    967 	if (IN_MULTICAST(sin->sin_addr.s_addr) && mopts != NULL) {
    968 		struct ip_moptions *imo;
    969 		struct ifnet *ifp;
    970 
    971 		imo = mopts;
    972 		if (imo->imo_multicast_ifp != NULL) {
    973 			ifp = imo->imo_multicast_ifp;
    974 			IFP_TO_IA(ifp, ia);		/* XXX */
    975 			if (ia == 0) {
    976 				*errorp = EADDRNOTAVAIL;
    977 				return NULL;
    978 			}
    979 		}
    980 	}
    981 	return satosin(&ia->ia_addr);
    982 }
    983