Home | History | Annotate | Line # | Download | only in netinet
in_pcb.c revision 1.45
      1 /*	$NetBSD: in_pcb.c,v 1.45 1998/01/07 22:51:23 lukem 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 #include <sys/param.h>
     39 #include <sys/systm.h>
     40 #include <sys/malloc.h>
     41 #include <sys/mbuf.h>
     42 #include <sys/protosw.h>
     43 #include <sys/socket.h>
     44 #include <sys/socketvar.h>
     45 #include <sys/ioctl.h>
     46 #include <sys/errno.h>
     47 #include <sys/time.h>
     48 #include <sys/proc.h>
     49 
     50 #include <net/if.h>
     51 #include <net/route.h>
     52 
     53 #include <netinet/in.h>
     54 #include <netinet/in_systm.h>
     55 #include <netinet/ip.h>
     56 #include <netinet/in_pcb.h>
     57 #include <netinet/in_var.h>
     58 #include <netinet/ip_var.h>
     59 
     60 struct	in_addr zeroin_addr;
     61 
     62 #define	INPCBHASH_BIND(table, laddr, lport) \
     63 	&(table)->inpt_bindhashtbl[ \
     64 	    ((ntohl((laddr).s_addr) + ntohs(lport))) & (table)->inpt_bindhash]
     65 #define	INPCBHASH_CONNECT(table, faddr, fport, laddr, lport) \
     66 	&(table)->inpt_connecthashtbl[ \
     67 	    ((ntohl((faddr).s_addr) + ntohs(fport)) + \
     68 	     (ntohl((laddr).s_addr) + ntohs(lport))) & (table)->inpt_connecthash]
     69 
     70 struct inpcb *
     71 	in_pcblookup_port __P((struct inpcbtable *,
     72 	    struct in_addr, u_int, int));
     73 
     74 int	anonportmin = IPPORT_ANONMIN;
     75 int	anonportmax = IPPORT_ANONMAX;
     76 
     77 void
     78 in_pcbinit(table, bindhashsize, connecthashsize)
     79 	struct inpcbtable *table;
     80 	int bindhashsize, connecthashsize;
     81 {
     82 
     83 	CIRCLEQ_INIT(&table->inpt_queue);
     84 	table->inpt_bindhashtbl =
     85 	    hashinit(bindhashsize, M_PCB, &table->inpt_bindhash);
     86 	table->inpt_connecthashtbl =
     87 	    hashinit(connecthashsize, M_PCB, &table->inpt_connecthash);
     88 	table->inpt_lastport = (u_int16_t)anonportmin;
     89 }
     90 
     91 int
     92 in_pcballoc(so, v)
     93 	struct socket *so;
     94 	void *v;
     95 {
     96 	struct inpcbtable *table = v;
     97 	register struct inpcb *inp;
     98 	int s;
     99 
    100 	MALLOC(inp, struct inpcb *, sizeof(*inp), M_PCB, M_WAITOK);
    101 	if (inp == NULL)
    102 		return (ENOBUFS);
    103 	bzero((caddr_t)inp, sizeof(*inp));
    104 	inp->inp_table = table;
    105 	inp->inp_socket = so;
    106 	inp->inp_errormtu = -1;
    107 	so->so_pcb = inp;
    108 	s = splnet();
    109 	CIRCLEQ_INSERT_HEAD(&table->inpt_queue, inp, inp_queue);
    110 	in_pcbstate(inp, INP_ATTACHED);
    111 	splx(s);
    112 	return (0);
    113 }
    114 
    115 int
    116 in_pcbbind(v, nam, p)
    117 	void *v;
    118 	struct mbuf *nam;
    119 	struct proc *p;
    120 {
    121 	register struct inpcb *inp = v;
    122 	register struct socket *so = inp->inp_socket;
    123 	register struct inpcbtable *table = inp->inp_table;
    124 	register struct sockaddr_in *sin;
    125 	u_int16_t lport = 0;
    126 	int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
    127 #ifndef IPNOPRIVPORTS
    128 	int error;
    129 #endif
    130 
    131 	if (in_ifaddr.tqh_first == 0)
    132 		return (EADDRNOTAVAIL);
    133 	if (inp->inp_lport || !in_nullhost(inp->inp_laddr))
    134 		return (EINVAL);
    135 	if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0 &&
    136 	    ((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0 ||
    137 	     (so->so_options & SO_ACCEPTCONN) == 0))
    138 		wild = INPLOOKUP_WILDCARD;
    139 	if (nam == 0)
    140 		goto noname;
    141 	sin = mtod(nam, struct sockaddr_in *);
    142 	if (nam->m_len != sizeof (*sin))
    143 		return (EINVAL);
    144 #ifdef notdef
    145 	/*
    146 	 * We should check the family, but old programs
    147 	 * incorrectly fail to initialize it.
    148 	 */
    149 	if (sin->sin_family != AF_INET)
    150 		return (EAFNOSUPPORT);
    151 #endif
    152 	lport = sin->sin_port;
    153 	if (IN_MULTICAST(sin->sin_addr.s_addr)) {
    154 		/*
    155 		 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
    156 		 * allow complete duplication of binding if
    157 		 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
    158 		 * and a multicast address is bound on both
    159 		 * new and duplicated sockets.
    160 		 */
    161 		if (so->so_options & SO_REUSEADDR)
    162 			reuseport = SO_REUSEADDR|SO_REUSEPORT;
    163 	} else if (!in_nullhost(sin->sin_addr)) {
    164 		sin->sin_port = 0;		/* yech... */
    165 		if (ifa_ifwithaddr(sintosa(sin)) == 0)
    166 			return (EADDRNOTAVAIL);
    167 	}
    168 	if (lport) {
    169 		struct inpcb *t;
    170 #ifndef IPNOPRIVPORTS
    171 		/* GROSS */
    172 		if (ntohs(lport) < IPPORT_RESERVED &&
    173 		    (p == 0 || (error = suser(p->p_ucred, &p->p_acflag))))
    174 			return (EACCES);
    175 #endif
    176 		t = in_pcblookup_port(table, sin->sin_addr, lport, wild);
    177 		if (t && (reuseport & t->inp_socket->so_options) == 0)
    178 			return (EADDRINUSE);
    179 	}
    180 	inp->inp_laddr = sin->sin_addr;
    181 
    182 noname:
    183 	if (lport == 0) {
    184 		int	   cnt
    185 		u_int16_t  min, max;
    186 		u_int16_t *lastport;
    187 
    188 		if (inp->inp_flags & INP_LOWPORT) {
    189 #ifndef IPNOPRIVPORTS
    190 			if (p == 0 || (error = suser(p->p_ucred, &p->p_acflag)))
    191 				return (EACCES);
    192 #endif
    193 			min = IPPORT_RESERVEDMIN;
    194 			max = IPPORT_RESERVEDMAX;
    195 			lastport = &table->inpt_lastlow;
    196 		} else {
    197 			min = anonportmin;
    198 			max = anonportmax;
    199 			lastport = &table->inpt_lastport;
    200 		}
    201 		if (min > max) {	/* sanity check */
    202 			u_int16_t swp;
    203 
    204 			swp = min;
    205 			min = max;
    206 			max = swp;
    207 		}
    208 
    209 		lport = *lastport + 1;
    210 		for (cnt = max - min + 1; cnt; cnt--, lport++) {
    211 			if (lport < min || lport > max)
    212 				lport = min;
    213 			if (!in_pcblookup_port(table, inp->inp_laddr,
    214 			    htons(lport), wild))
    215 				goto found;
    216 		}
    217 		if (!in_nullhost(inp->inp_laddr))
    218 			inp->inp_laddr.s_addr = INADDR_ANY;
    219 		return (EAGAIN);
    220 	found:
    221 		inp->inp_flags |= INP_ANONPORT;
    222 		*lastport = lport;
    223 		lport = htons(lport);
    224 	}
    225 	inp->inp_lport = lport;
    226 	in_pcbstate(inp, INP_BOUND);
    227 	return (0);
    228 }
    229 
    230 /*
    231  * Connect from a socket to a specified address.
    232  * Both address and port must be specified in argument sin.
    233  * If don't have a local address for this socket yet,
    234  * then pick one.
    235  */
    236 int
    237 in_pcbconnect(v, nam)
    238 	register void *v;
    239 	struct mbuf *nam;
    240 {
    241 	register struct inpcb *inp = v;
    242 	struct in_ifaddr *ia;
    243 	struct sockaddr_in *ifaddr = NULL;
    244 	register struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *);
    245 	int error;
    246 
    247 	if (nam->m_len != sizeof (*sin))
    248 		return (EINVAL);
    249 	if (sin->sin_family != AF_INET)
    250 		return (EAFNOSUPPORT);
    251 	if (sin->sin_port == 0)
    252 		return (EADDRNOTAVAIL);
    253 	if (in_ifaddr.tqh_first != 0) {
    254 		/*
    255 		 * If the destination address is INADDR_ANY,
    256 		 * use the primary local address.
    257 		 * If the supplied address is INADDR_BROADCAST,
    258 		 * and the primary interface supports broadcast,
    259 		 * choose the broadcast address for that interface.
    260 		 */
    261 		if (in_nullhost(sin->sin_addr))
    262 			sin->sin_addr = in_ifaddr.tqh_first->ia_addr.sin_addr;
    263 		else if (sin->sin_addr.s_addr == INADDR_BROADCAST &&
    264 		  (in_ifaddr.tqh_first->ia_ifp->if_flags & IFF_BROADCAST))
    265 			sin->sin_addr = in_ifaddr.tqh_first->ia_broadaddr.sin_addr;
    266 	}
    267 	/*
    268 	 * If we haven't bound which network number to use as ours,
    269 	 * we will use the number of the outgoing interface.
    270 	 * This depends on having done a routing lookup, which
    271 	 * we will probably have to do anyway, so we might
    272 	 * as well do it now.  On the other hand if we are
    273 	 * sending to multiple destinations we may have already
    274 	 * done the lookup, so see if we can use the route
    275 	 * from before.  In any case, we only
    276 	 * chose a port number once, even if sending to multiple
    277 	 * destinations.
    278 	 */
    279 	if (in_nullhost(inp->inp_laddr)) {
    280 		register struct route *ro;
    281 
    282 		ia = (struct in_ifaddr *)0;
    283 		/*
    284 		 * If route is known or can be allocated now,
    285 		 * our src addr is taken from the i/f, else punt.
    286 		 */
    287 		ro = &inp->inp_route;
    288 		if (ro->ro_rt &&
    289 		    (!in_hosteq(satosin(&ro->ro_dst)->sin_addr,
    290 			sin->sin_addr) ||
    291 		    inp->inp_socket->so_options & SO_DONTROUTE)) {
    292 			RTFREE(ro->ro_rt);
    293 			ro->ro_rt = (struct rtentry *)0;
    294 		}
    295 		if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0 && /*XXX*/
    296 		    (ro->ro_rt == (struct rtentry *)0 ||
    297 		    ro->ro_rt->rt_ifp == (struct ifnet *)0)) {
    298 			/* No route yet, so try to acquire one */
    299 			ro->ro_dst.sa_family = AF_INET;
    300 			ro->ro_dst.sa_len = sizeof(struct sockaddr_in);
    301 			satosin(&ro->ro_dst)->sin_addr = sin->sin_addr;
    302 			rtalloc(ro);
    303 		}
    304 		/*
    305 		 * If we found a route, use the address
    306 		 * corresponding to the outgoing interface
    307 		 * unless it is the loopback (in case a route
    308 		 * to our address on another net goes to loopback).
    309 		 */
    310 		if (ro->ro_rt && !(ro->ro_rt->rt_ifp->if_flags & IFF_LOOPBACK))
    311 			ia = ifatoia(ro->ro_rt->rt_ifa);
    312 		if (ia == 0) {
    313 			u_int16_t fport = sin->sin_port;
    314 
    315 			sin->sin_port = 0;
    316 			ia = ifatoia(ifa_ifwithladdr(sintosa(sin)));
    317 			sin->sin_port = fport;
    318 			if (ia == 0)
    319 				ia = in_ifaddr.tqh_first;
    320 			if (ia == 0)
    321 				return (EADDRNOTAVAIL);
    322 		}
    323 		/*
    324 		 * If the destination address is multicast and an outgoing
    325 		 * interface has been set as a multicast option, use the
    326 		 * address of that interface as our source address.
    327 		 */
    328 		if (IN_MULTICAST(sin->sin_addr.s_addr) &&
    329 		    inp->inp_moptions != NULL) {
    330 			struct ip_moptions *imo;
    331 			struct ifnet *ifp;
    332 
    333 			imo = inp->inp_moptions;
    334 			if (imo->imo_multicast_ifp != NULL) {
    335 				ifp = imo->imo_multicast_ifp;
    336 				for (ia = in_ifaddr.tqh_first; ia != 0;
    337 				    ia = ia->ia_list.tqe_next)
    338 					if (ia->ia_ifp == ifp)
    339 						break;
    340 				if (ia == 0)
    341 					return (EADDRNOTAVAIL);
    342 			}
    343 		}
    344 		ifaddr = satosin(&ia->ia_addr);
    345 	}
    346 	if (in_pcblookup_connect(inp->inp_table, sin->sin_addr, sin->sin_port,
    347 	    !in_nullhost(inp->inp_laddr) ? inp->inp_laddr : ifaddr->sin_addr,
    348 	    inp->inp_lport) != 0)
    349 		return (EADDRINUSE);
    350 	if (in_nullhost(inp->inp_laddr)) {
    351 		if (inp->inp_lport == 0) {
    352 			error = in_pcbbind(inp, (struct mbuf *)0,
    353 			    (struct proc *)0);
    354 			/*
    355 			 * This used to ignore the return value
    356 			 * completely, but we need to check for
    357 			 * ephemeral port shortage.
    358 			 * XXX Should we check for other errors, too?
    359 			 */
    360 			if (error == EAGAIN)
    361 				return (error);
    362 		}
    363 		inp->inp_laddr = ifaddr->sin_addr;
    364 	}
    365 	inp->inp_faddr = sin->sin_addr;
    366 	inp->inp_fport = sin->sin_port;
    367 	in_pcbstate(inp, INP_CONNECTED);
    368 	return (0);
    369 }
    370 
    371 void
    372 in_pcbdisconnect(v)
    373 	void *v;
    374 {
    375 	struct inpcb *inp = v;
    376 
    377 	inp->inp_faddr = zeroin_addr;
    378 	inp->inp_fport = 0;
    379 	in_pcbstate(inp, INP_BOUND);
    380 	if (inp->inp_socket->so_state & SS_NOFDREF)
    381 		in_pcbdetach(inp);
    382 }
    383 
    384 void
    385 in_pcbdetach(v)
    386 	void *v;
    387 {
    388 	struct inpcb *inp = v;
    389 	struct socket *so = inp->inp_socket;
    390 	int s;
    391 
    392 	so->so_pcb = 0;
    393 	sofree(so);
    394 	if (inp->inp_options)
    395 		(void)m_free(inp->inp_options);
    396 	if (inp->inp_route.ro_rt)
    397 		rtfree(inp->inp_route.ro_rt);
    398 	ip_freemoptions(inp->inp_moptions);
    399 	s = splnet();
    400 	in_pcbstate(inp, INP_ATTACHED);
    401 	CIRCLEQ_REMOVE(&inp->inp_table->inpt_queue, inp, inp_queue);
    402 	splx(s);
    403 	FREE(inp, M_PCB);
    404 }
    405 
    406 void
    407 in_setsockaddr(inp, nam)
    408 	register struct inpcb *inp;
    409 	struct mbuf *nam;
    410 {
    411 	register struct sockaddr_in *sin;
    412 
    413 	nam->m_len = sizeof (*sin);
    414 	sin = mtod(nam, struct sockaddr_in *);
    415 	bzero((caddr_t)sin, sizeof (*sin));
    416 	sin->sin_family = AF_INET;
    417 	sin->sin_len = sizeof(*sin);
    418 	sin->sin_port = inp->inp_lport;
    419 	sin->sin_addr = inp->inp_laddr;
    420 }
    421 
    422 void
    423 in_setpeeraddr(inp, nam)
    424 	struct inpcb *inp;
    425 	struct mbuf *nam;
    426 {
    427 	register struct sockaddr_in *sin;
    428 
    429 	nam->m_len = sizeof (*sin);
    430 	sin = mtod(nam, struct sockaddr_in *);
    431 	bzero((caddr_t)sin, sizeof (*sin));
    432 	sin->sin_family = AF_INET;
    433 	sin->sin_len = sizeof(*sin);
    434 	sin->sin_port = inp->inp_fport;
    435 	sin->sin_addr = inp->inp_faddr;
    436 }
    437 
    438 /*
    439  * Pass some notification to all connections of a protocol
    440  * associated with address dst.  The local address and/or port numbers
    441  * may be specified to limit the search.  The "usual action" will be
    442  * taken, depending on the ctlinput cmd.  The caller must filter any
    443  * cmds that are uninteresting (e.g., no error in the map).
    444  * Call the protocol specific routine (if any) to report
    445  * any errors for each matching socket.
    446  *
    447  * Must be called at splsoftnet.
    448  */
    449 int
    450 in_pcbnotify(table, faddr, fport_arg, laddr, lport_arg, errno, notify)
    451 	struct inpcbtable *table;
    452 	struct in_addr faddr, laddr;
    453 	u_int fport_arg, lport_arg;
    454 	int errno;
    455 	void (*notify) __P((struct inpcb *, int));
    456 {
    457 	struct inpcbhead *head;
    458 	register struct inpcb *inp, *ninp;
    459 	u_int16_t fport = fport_arg, lport = lport_arg;
    460 	int nmatch;
    461 
    462 	if (in_nullhost(faddr) || notify == 0)
    463 		return (0);
    464 
    465 	nmatch = 0;
    466 	head = INPCBHASH_CONNECT(table, faddr, fport, laddr, lport);
    467 	for (inp = head->lh_first; inp != NULL; inp = ninp) {
    468 		ninp = inp->inp_hash.le_next;
    469 		if (in_hosteq(inp->inp_faddr, faddr) &&
    470 		    inp->inp_fport == fport &&
    471 		    inp->inp_lport == lport &&
    472 		    in_hosteq(inp->inp_laddr, laddr)) {
    473 			(*notify)(inp, errno);
    474 			nmatch++;
    475 		}
    476 	}
    477 	return (nmatch);
    478 }
    479 
    480 void
    481 in_pcbnotifyall(table, faddr, errno, notify)
    482 	struct inpcbtable *table;
    483 	struct in_addr faddr;
    484 	int errno;
    485 	void (*notify) __P((struct inpcb *, int));
    486 {
    487 	register struct inpcb *inp, *ninp;
    488 
    489 	if (in_nullhost(faddr) || notify == 0)
    490 		return;
    491 
    492 	for (inp = table->inpt_queue.cqh_first;
    493 	    inp != (struct inpcb *)&table->inpt_queue;
    494 	    inp = ninp) {
    495 		ninp = inp->inp_queue.cqe_next;
    496 		if (in_hosteq(inp->inp_faddr, faddr))
    497 			(*notify)(inp, errno);
    498 	}
    499 }
    500 
    501 /*
    502  * Check for alternatives when higher level complains
    503  * about service problems.  For now, invalidate cached
    504  * routing information.  If the route was created dynamically
    505  * (by a redirect), time to try a default gateway again.
    506  */
    507 void
    508 in_losing(inp)
    509 	struct inpcb *inp;
    510 {
    511 	register struct rtentry *rt;
    512 	struct rt_addrinfo info;
    513 
    514 	if ((rt = inp->inp_route.ro_rt)) {
    515 		inp->inp_route.ro_rt = 0;
    516 		bzero((caddr_t)&info, sizeof(info));
    517 		info.rti_info[RTAX_DST] = &inp->inp_route.ro_dst;
    518 		info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
    519 		info.rti_info[RTAX_NETMASK] = rt_mask(rt);
    520 		rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
    521 		if (rt->rt_flags & RTF_DYNAMIC)
    522 			(void) rtrequest(RTM_DELETE, rt_key(rt),
    523 				rt->rt_gateway, rt_mask(rt), rt->rt_flags,
    524 				(struct rtentry **)0);
    525 		else
    526 		/*
    527 		 * A new route can be allocated
    528 		 * the next time output is attempted.
    529 		 */
    530 			rtfree(rt);
    531 	}
    532 }
    533 
    534 /*
    535  * After a routing change, flush old routing
    536  * and allocate a (hopefully) better one.
    537  */
    538 void
    539 in_rtchange(inp, errno)
    540 	register struct inpcb *inp;
    541 	int errno;
    542 {
    543 
    544 	if (inp->inp_route.ro_rt) {
    545 		rtfree(inp->inp_route.ro_rt);
    546 		inp->inp_route.ro_rt = 0;
    547 		/*
    548 		 * A new route can be allocated the next time
    549 		 * output is attempted.
    550 		 */
    551 	}
    552 	/* SHOULD NOTIFY HIGHER-LEVEL PROTOCOLS */
    553 }
    554 
    555 struct inpcb *
    556 in_pcblookup_port(table, laddr, lport_arg, flags)
    557 	struct inpcbtable *table;
    558 	struct in_addr laddr;
    559 	u_int lport_arg;
    560 	int flags;
    561 {
    562 	register struct inpcb *inp, *match = 0;
    563 	int matchwild = 3, wildcard;
    564 	u_int16_t lport = lport_arg;
    565 
    566 	for (inp = table->inpt_queue.cqh_first;
    567 	    inp != (struct inpcb *)&table->inpt_queue;
    568 	    inp = inp->inp_queue.cqe_next) {
    569 		if (inp->inp_lport != lport)
    570 			continue;
    571 		wildcard = 0;
    572 		if (!in_nullhost(inp->inp_faddr))
    573 			wildcard++;
    574 		if (in_nullhost(inp->inp_laddr)) {
    575 			if (!in_nullhost(laddr))
    576 				wildcard++;
    577 		} else {
    578 			if (in_nullhost(laddr))
    579 				wildcard++;
    580 			else {
    581 				if (!in_hosteq(inp->inp_laddr, laddr))
    582 					continue;
    583 			}
    584 		}
    585 		if (wildcard && (flags & INPLOOKUP_WILDCARD) == 0)
    586 			continue;
    587 		if (wildcard < matchwild) {
    588 			match = inp;
    589 			matchwild = wildcard;
    590 			if (matchwild == 0)
    591 				break;
    592 		}
    593 	}
    594 	return (match);
    595 }
    596 
    597 #ifdef DIAGNOSTIC
    598 int	in_pcbnotifymiss = 0;
    599 #endif
    600 
    601 struct inpcb *
    602 in_pcblookup_connect(table, faddr, fport_arg, laddr, lport_arg)
    603 	struct inpcbtable *table;
    604 	struct in_addr faddr, laddr;
    605 	u_int fport_arg, lport_arg;
    606 {
    607 	struct inpcbhead *head;
    608 	register struct inpcb *inp;
    609 	u_int16_t fport = fport_arg, lport = lport_arg;
    610 
    611 	head = INPCBHASH_CONNECT(table, faddr, fport, laddr, lport);
    612 	for (inp = head->lh_first; inp != NULL; inp = inp->inp_hash.le_next) {
    613 		if (in_hosteq(inp->inp_faddr, faddr) &&
    614 		    inp->inp_fport == fport &&
    615 		    inp->inp_lport == lport &&
    616 		    in_hosteq(inp->inp_laddr, laddr))
    617 			goto out;
    618 	}
    619 #ifdef DIAGNOSTIC
    620 	if (in_pcbnotifymiss) {
    621 		printf("in_pcblookup_connect: faddr=%08x fport=%d laddr=%08x lport=%d\n",
    622 		    ntohl(faddr.s_addr), ntohs(fport),
    623 		    ntohl(laddr.s_addr), ntohs(lport));
    624 	}
    625 #endif
    626 	return (0);
    627 
    628 out:
    629 	/* Move this PCB to the head of hash chain. */
    630 	if (inp != head->lh_first) {
    631 		LIST_REMOVE(inp, inp_hash);
    632 		LIST_INSERT_HEAD(head, inp, inp_hash);
    633 	}
    634 	return (inp);
    635 }
    636 
    637 struct inpcb *
    638 in_pcblookup_bind(table, laddr, lport_arg)
    639 	struct inpcbtable *table;
    640 	struct in_addr laddr;
    641 	u_int lport_arg;
    642 {
    643 	struct inpcbhead *head;
    644 	register struct inpcb *inp;
    645 	u_int16_t lport = lport_arg;
    646 
    647 	head = INPCBHASH_BIND(table, laddr, lport);
    648 	for (inp = head->lh_first; inp != NULL; inp = inp->inp_hash.le_next) {
    649 		if (inp->inp_lport == lport &&
    650 		    in_hosteq(inp->inp_laddr, laddr))
    651 			goto out;
    652 	}
    653 	head = INPCBHASH_BIND(table, zeroin_addr, lport);
    654 	for (inp = head->lh_first; inp != NULL; inp = inp->inp_hash.le_next) {
    655 		if (inp->inp_lport == lport &&
    656 		    in_hosteq(inp->inp_laddr, zeroin_addr))
    657 			goto out;
    658 	}
    659 #ifdef DIAGNOSTIC
    660 	if (in_pcbnotifymiss) {
    661 		printf("in_pcblookup_bind: laddr=%08x lport=%d\n",
    662 		    ntohl(laddr.s_addr), ntohs(lport));
    663 	}
    664 #endif
    665 	return (0);
    666 
    667 out:
    668 	/* Move this PCB to the head of hash chain. */
    669 	if (inp != head->lh_first) {
    670 		LIST_REMOVE(inp, inp_hash);
    671 		LIST_INSERT_HEAD(head, inp, inp_hash);
    672 	}
    673 	return (inp);
    674 }
    675 
    676 void
    677 in_pcbstate(inp, state)
    678 	struct inpcb *inp;
    679 	int state;
    680 {
    681 
    682 	if (inp->inp_state > INP_ATTACHED)
    683 		LIST_REMOVE(inp, inp_hash);
    684 
    685 	switch (state) {
    686 	case INP_BOUND:
    687 		LIST_INSERT_HEAD(INPCBHASH_BIND(inp->inp_table,
    688 		    inp->inp_laddr, inp->inp_lport), inp, inp_hash);
    689 		break;
    690 	case INP_CONNECTED:
    691 		LIST_INSERT_HEAD(INPCBHASH_CONNECT(inp->inp_table,
    692 		    inp->inp_faddr, inp->inp_fport,
    693 		    inp->inp_laddr, inp->inp_lport), inp, inp_hash);
    694 		break;
    695 	}
    696 
    697 	inp->inp_state = state;
    698 }
    699 
    700 struct rtentry *
    701 in_pcbrtentry(inp)
    702 	struct inpcb *inp;
    703 {
    704 	struct route *ro;
    705 
    706 	ro = &inp->inp_route;
    707 
    708 	if (ro->ro_rt == NULL) {
    709 		/*
    710 		 * No route yet, so try to acquire one.
    711 		 */
    712 		if (!in_nullhost(inp->inp_faddr)) {
    713 			ro->ro_dst.sa_family = AF_INET;
    714 			ro->ro_dst.sa_len = sizeof(ro->ro_dst);
    715 			satosin(&ro->ro_dst)->sin_addr = inp->inp_faddr;
    716 			rtalloc(ro);
    717 		}
    718 	}
    719 	return (ro->ro_rt);
    720 }
    721