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