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