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