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