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