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