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