xref: /netbsd-src/sys/netinet/in_pcb.c (revision 5e4c038a45edbc7d63b7c2daa76e29f88b64a4e3)
1 /*	$NetBSD: in_pcb.c,v 1.77 2002/05/28 11:10:52 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.77 2002/05/28 11:10:52 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_policy(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 inpcb *inp = v;
221 	struct socket *so = inp->inp_socket;
222 	struct inpcbtable *table = inp->inp_table;
223 	struct sockaddr_in *sin;
224 	u_int16_t lport = 0;
225 	int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
226 #ifndef IPNOPRIVPORTS
227 	int error;
228 #endif
229 
230 	if (TAILQ_FIRST(&in_ifaddr) == 0)
231 		return (EADDRNOTAVAIL);
232 	if (inp->inp_lport || !in_nullhost(inp->inp_laddr))
233 		return (EINVAL);
234 	if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
235 		wild = 1;
236 	if (nam == 0)
237 		goto noname;
238 	sin = mtod(nam, struct sockaddr_in *);
239 	if (nam->m_len != sizeof (*sin))
240 		return (EINVAL);
241 #ifdef notdef
242 	/*
243 	 * We should check the family, but old programs
244 	 * incorrectly fail to initialize it.
245 	 */
246 	if (sin->sin_family != AF_INET)
247 		return (EAFNOSUPPORT);
248 #endif
249 	lport = sin->sin_port;
250 	if (IN_MULTICAST(sin->sin_addr.s_addr)) {
251 		/*
252 		 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
253 		 * allow complete duplication of binding if
254 		 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
255 		 * and a multicast address is bound on both
256 		 * new and duplicated sockets.
257 		 */
258 		if (so->so_options & SO_REUSEADDR)
259 			reuseport = SO_REUSEADDR|SO_REUSEPORT;
260 	} else if (!in_nullhost(sin->sin_addr)) {
261 		sin->sin_port = 0;		/* yech... */
262 		if (ifa_ifwithaddr(sintosa(sin)) == 0)
263 			return (EADDRNOTAVAIL);
264 	}
265 	if (lport) {
266 		struct inpcb *t;
267 #ifndef IPNOPRIVPORTS
268 		/* GROSS */
269 		if (ntohs(lport) < IPPORT_RESERVED &&
270 		    (p == 0 || (error = suser(p->p_ucred, &p->p_acflag))))
271 			return (EACCES);
272 #endif
273 		if (so->so_uid && !IN_MULTICAST(sin->sin_addr.s_addr)) {
274 			t = in_pcblookup_port(table, sin->sin_addr, lport, 1);
275 		/*
276 		 * XXX:	investigate ramifications of loosening this
277 		 *	restriction so that as long as both ports have
278 		 *	SO_REUSEPORT allow the bind
279 		 */
280 			if (t &&
281 			    (!in_nullhost(sin->sin_addr) ||
282 			     !in_nullhost(t->inp_laddr) ||
283 			     (t->inp_socket->so_options & SO_REUSEPORT) == 0)
284 			    && (so->so_uid != t->inp_socket->so_uid)) {
285 				return (EADDRINUSE);
286 			}
287 		}
288 		t = in_pcblookup_port(table, sin->sin_addr, lport, wild);
289 		if (t && (reuseport & t->inp_socket->so_options) == 0)
290 			return (EADDRINUSE);
291 	}
292 	inp->inp_laddr = sin->sin_addr;
293 
294 noname:
295 	if (lport == 0) {
296 		int	   cnt;
297 		u_int16_t  min, max;
298 		u_int16_t *lastport;
299 
300 		if (inp->inp_flags & INP_LOWPORT) {
301 #ifndef IPNOPRIVPORTS
302 			if (p == 0 || (error = suser(p->p_ucred, &p->p_acflag)))
303 				return (EACCES);
304 #endif
305 			min = lowportmin;
306 			max = lowportmax;
307 			lastport = &table->inpt_lastlow;
308 		} else {
309 			min = anonportmin;
310 			max = anonportmax;
311 			lastport = &table->inpt_lastport;
312 		}
313 		if (min > max) {	/* sanity check */
314 			u_int16_t swp;
315 
316 			swp = min;
317 			min = max;
318 			max = swp;
319 		}
320 
321 		lport = *lastport - 1;
322 		for (cnt = max - min + 1; cnt; cnt--, lport--) {
323 			if (lport < min || lport > max)
324 				lport = max;
325 			if (!in_pcblookup_port(table, inp->inp_laddr,
326 			    htons(lport), 1))
327 				goto found;
328 		}
329 		if (!in_nullhost(inp->inp_laddr))
330 			inp->inp_laddr.s_addr = INADDR_ANY;
331 		return (EAGAIN);
332 	found:
333 		inp->inp_flags |= INP_ANONPORT;
334 		*lastport = lport;
335 		lport = htons(lport);
336 	}
337 	inp->inp_lport = lport;
338 	in_pcbstate(inp, INP_BOUND);
339 	return (0);
340 }
341 
342 /*
343  * Connect from a socket to a specified address.
344  * Both address and port must be specified in argument sin.
345  * If don't have a local address for this socket yet,
346  * then pick one.
347  */
348 int
349 in_pcbconnect(v, nam)
350 	void *v;
351 	struct mbuf *nam;
352 {
353 	struct inpcb *inp = v;
354 	struct in_ifaddr *ia;
355 	struct sockaddr_in *ifaddr = NULL;
356 	struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *);
357 	int error;
358 
359 	if (nam->m_len != sizeof (*sin))
360 		return (EINVAL);
361 	if (sin->sin_family != AF_INET)
362 		return (EAFNOSUPPORT);
363 	if (sin->sin_port == 0)
364 		return (EADDRNOTAVAIL);
365 	if (TAILQ_FIRST(&in_ifaddr) != 0) {
366 		/*
367 		 * If the destination address is INADDR_ANY,
368 		 * use any local address (likely loopback).
369 		 * If the supplied address is INADDR_BROADCAST,
370 		 * use the broadcast address of an interface
371 		 * which supports broadcast. (loopback does not)
372 		 */
373 
374 		if (in_nullhost(sin->sin_addr)) {
375 			sin->sin_addr =
376 			    TAILQ_FIRST(&in_ifaddr)->ia_addr.sin_addr;
377 		} else if (sin->sin_addr.s_addr == INADDR_BROADCAST) {
378 			TAILQ_FOREACH(ia, &in_ifaddr, ia_list) {
379 				if (ia->ia_ifp->if_flags & IFF_BROADCAST) {
380 					sin->sin_addr =
381 					    ia->ia_broadaddr.sin_addr;
382 					break;
383 				}
384 			}
385 		}
386 	}
387 	/*
388 	 * If we haven't bound which network number to use as ours,
389 	 * we will use the number of the outgoing interface.
390 	 * This depends on having done a routing lookup, which
391 	 * we will probably have to do anyway, so we might
392 	 * as well do it now.  On the other hand if we are
393 	 * sending to multiple destinations we may have already
394 	 * done the lookup, so see if we can use the route
395 	 * from before.  In any case, we only
396 	 * chose a port number once, even if sending to multiple
397 	 * destinations.
398 	 */
399 	if (in_nullhost(inp->inp_laddr)) {
400 		int error;
401 		ifaddr = in_selectsrc(sin, &inp->inp_route,
402 			inp->inp_socket->so_options, inp->inp_moptions, &error);
403 		if (ifaddr == NULL) {
404 			if (error == 0)
405 				error = EADDRNOTAVAIL;
406 			return error;
407 		}
408 	}
409 	if (in_pcblookup_connect(inp->inp_table, sin->sin_addr, sin->sin_port,
410 	    !in_nullhost(inp->inp_laddr) ? inp->inp_laddr : ifaddr->sin_addr,
411 	    inp->inp_lport) != 0)
412 		return (EADDRINUSE);
413 	if (in_nullhost(inp->inp_laddr)) {
414 		if (inp->inp_lport == 0) {
415 			error = in_pcbbind(inp, (struct mbuf *)0,
416 			    (struct proc *)0);
417 			/*
418 			 * This used to ignore the return value
419 			 * completely, but we need to check for
420 			 * ephemeral port shortage.
421 			 * XXX Should we check for other errors, too?
422 			 */
423 			if (error == EAGAIN)
424 				return (error);
425 		}
426 		inp->inp_laddr = ifaddr->sin_addr;
427 	}
428 	inp->inp_faddr = sin->sin_addr;
429 	inp->inp_fport = sin->sin_port;
430 	in_pcbstate(inp, INP_CONNECTED);
431 #ifdef IPSEC
432 	if (inp->inp_socket->so_type == SOCK_STREAM)
433 		ipsec_pcbconn(inp->inp_sp);
434 #endif
435 	return (0);
436 }
437 
438 void
439 in_pcbdisconnect(v)
440 	void *v;
441 {
442 	struct inpcb *inp = v;
443 
444 	inp->inp_faddr = zeroin_addr;
445 	inp->inp_fport = 0;
446 	in_pcbstate(inp, INP_BOUND);
447 	if (inp->inp_socket->so_state & SS_NOFDREF)
448 		in_pcbdetach(inp);
449 #ifdef IPSEC
450 	ipsec_pcbdisconn(inp->inp_sp);
451 #endif
452 }
453 
454 void
455 in_pcbdetach(v)
456 	void *v;
457 {
458 	struct inpcb *inp = v;
459 	struct socket *so = inp->inp_socket;
460 	int s;
461 
462 #ifdef IPSEC
463 	ipsec4_delete_pcbpolicy(inp);
464 #endif /*IPSEC*/
465 	so->so_pcb = 0;
466 	sofree(so);
467 	if (inp->inp_options)
468 		(void)m_free(inp->inp_options);
469 	if (inp->inp_route.ro_rt)
470 		rtfree(inp->inp_route.ro_rt);
471 	ip_freemoptions(inp->inp_moptions);
472 	s = splnet();
473 	in_pcbstate(inp, INP_ATTACHED);
474 	CIRCLEQ_REMOVE(&inp->inp_table->inpt_queue, inp, inp_queue);
475 	splx(s);
476 	pool_put(&inpcb_pool, inp);
477 }
478 
479 void
480 in_setsockaddr(inp, nam)
481 	struct inpcb *inp;
482 	struct mbuf *nam;
483 {
484 	struct sockaddr_in *sin;
485 
486 	nam->m_len = sizeof (*sin);
487 	sin = mtod(nam, struct sockaddr_in *);
488 	bzero((caddr_t)sin, sizeof (*sin));
489 	sin->sin_family = AF_INET;
490 	sin->sin_len = sizeof(*sin);
491 	sin->sin_port = inp->inp_lport;
492 	sin->sin_addr = inp->inp_laddr;
493 }
494 
495 void
496 in_setpeeraddr(inp, nam)
497 	struct inpcb *inp;
498 	struct mbuf *nam;
499 {
500 	struct sockaddr_in *sin;
501 
502 	nam->m_len = sizeof (*sin);
503 	sin = mtod(nam, struct sockaddr_in *);
504 	bzero((caddr_t)sin, sizeof (*sin));
505 	sin->sin_family = AF_INET;
506 	sin->sin_len = sizeof(*sin);
507 	sin->sin_port = inp->inp_fport;
508 	sin->sin_addr = inp->inp_faddr;
509 }
510 
511 /*
512  * Pass some notification to all connections of a protocol
513  * associated with address dst.  The local address and/or port numbers
514  * may be specified to limit the search.  The "usual action" will be
515  * taken, depending on the ctlinput cmd.  The caller must filter any
516  * cmds that are uninteresting (e.g., no error in the map).
517  * Call the protocol specific routine (if any) to report
518  * any errors for each matching socket.
519  *
520  * Must be called at splsoftnet.
521  */
522 int
523 in_pcbnotify(table, faddr, fport_arg, laddr, lport_arg, errno, notify)
524 	struct inpcbtable *table;
525 	struct in_addr faddr, laddr;
526 	u_int fport_arg, lport_arg;
527 	int errno;
528 	void (*notify) __P((struct inpcb *, int));
529 {
530 	struct inpcbhead *head;
531 	struct inpcb *inp, *ninp;
532 	u_int16_t fport = fport_arg, lport = lport_arg;
533 	int nmatch;
534 
535 	if (in_nullhost(faddr) || notify == 0)
536 		return (0);
537 
538 	nmatch = 0;
539 	head = INPCBHASH_CONNECT(table, faddr, fport, laddr, lport);
540 	for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) {
541 		ninp = LIST_NEXT(inp, inp_hash);
542 		if (in_hosteq(inp->inp_faddr, faddr) &&
543 		    inp->inp_fport == fport &&
544 		    inp->inp_lport == lport &&
545 		    in_hosteq(inp->inp_laddr, laddr)) {
546 			(*notify)(inp, errno);
547 			nmatch++;
548 		}
549 	}
550 	return (nmatch);
551 }
552 
553 void
554 in_pcbnotifyall(table, faddr, errno, notify)
555 	struct inpcbtable *table;
556 	struct in_addr faddr;
557 	int errno;
558 	void (*notify) __P((struct inpcb *, int));
559 {
560 	struct inpcb *inp, *ninp;
561 
562 	if (in_nullhost(faddr) || notify == 0)
563 		return;
564 
565 	for (inp = CIRCLEQ_FIRST(&table->inpt_queue);
566 	    inp != (void *)&table->inpt_queue;
567 	    inp = ninp) {
568 		ninp = CIRCLEQ_NEXT(inp, inp_queue);
569 		if (in_hosteq(inp->inp_faddr, faddr))
570 			(*notify)(inp, errno);
571 	}
572 }
573 
574 void
575 in_pcbpurgeif0(table, ifp)
576 	struct inpcbtable *table;
577 	struct ifnet *ifp;
578 {
579 	struct inpcb *inp, *ninp;
580 	struct ip_moptions *imo;
581 	int i, gap;
582 
583 	for (inp = CIRCLEQ_FIRST(&table->inpt_queue);
584 	    inp != (void *)&table->inpt_queue;
585 	    inp = ninp) {
586 		ninp = CIRCLEQ_NEXT(inp, inp_queue);
587 		imo = inp->inp_moptions;
588 		if (imo != NULL) {
589 			/*
590 			 * Unselect the outgoing interface if it is being
591 			 * detached.
592 			 */
593 			if (imo->imo_multicast_ifp == ifp)
594 				imo->imo_multicast_ifp = NULL;
595 
596 			/*
597 			 * Drop multicast group membership if we joined
598 			 * through the interface being detached.
599 			 */
600 			for (i = 0, gap = 0; i < imo->imo_num_memberships;
601 			    i++) {
602 				if (imo->imo_membership[i]->inm_ifp == ifp) {
603 					in_delmulti(imo->imo_membership[i]);
604 					gap++;
605 				} else if (gap != 0)
606 					imo->imo_membership[i - gap] =
607 					    imo->imo_membership[i];
608 			}
609 			imo->imo_num_memberships -= gap;
610 		}
611 	}
612 }
613 
614 void
615 in_pcbpurgeif(table, ifp)
616 	struct inpcbtable *table;
617 	struct ifnet *ifp;
618 {
619 	struct inpcb *inp, *ninp;
620 
621 	for (inp = CIRCLEQ_FIRST(&table->inpt_queue);
622 	    inp != (void *)&table->inpt_queue;
623 	    inp = ninp) {
624 		ninp = CIRCLEQ_NEXT(inp, inp_queue);
625 		if (inp->inp_route.ro_rt != NULL &&
626 		    inp->inp_route.ro_rt->rt_ifp == ifp)
627 			in_rtchange(inp, 0);
628 	}
629 }
630 
631 /*
632  * Check for alternatives when higher level complains
633  * about service problems.  For now, invalidate cached
634  * routing information.  If the route was created dynamically
635  * (by a redirect), time to try a default gateway again.
636  */
637 void
638 in_losing(inp)
639 	struct inpcb *inp;
640 {
641 	struct rtentry *rt;
642 	struct rt_addrinfo info;
643 
644 	if ((rt = inp->inp_route.ro_rt)) {
645 		inp->inp_route.ro_rt = 0;
646 		bzero((caddr_t)&info, sizeof(info));
647 		info.rti_info[RTAX_DST] = &inp->inp_route.ro_dst;
648 		info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
649 		info.rti_info[RTAX_NETMASK] = rt_mask(rt);
650 		rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
651 		if (rt->rt_flags & RTF_DYNAMIC)
652 			(void) rtrequest(RTM_DELETE, rt_key(rt),
653 				rt->rt_gateway, rt_mask(rt), rt->rt_flags,
654 				(struct rtentry **)0);
655 		else
656 		/*
657 		 * A new route can be allocated
658 		 * the next time output is attempted.
659 		 */
660 			rtfree(rt);
661 	}
662 }
663 
664 /*
665  * After a routing change, flush old routing
666  * and allocate a (hopefully) better one.
667  */
668 void
669 in_rtchange(inp, errno)
670 	struct inpcb *inp;
671 	int errno;
672 {
673 
674 	if (inp->inp_route.ro_rt) {
675 		rtfree(inp->inp_route.ro_rt);
676 		inp->inp_route.ro_rt = 0;
677 		/*
678 		 * A new route can be allocated the next time
679 		 * output is attempted.
680 		 */
681 	}
682 	/* XXX SHOULD NOTIFY HIGHER-LEVEL PROTOCOLS */
683 }
684 
685 struct inpcb *
686 in_pcblookup_port(table, laddr, lport_arg, lookup_wildcard)
687 	struct inpcbtable *table;
688 	struct in_addr laddr;
689 	u_int lport_arg;
690 	int lookup_wildcard;
691 {
692 	struct inpcb *inp, *match = 0;
693 	int matchwild = 3, wildcard;
694 	u_int16_t lport = lport_arg;
695 
696 	CIRCLEQ_FOREACH(inp, &table->inpt_queue, inp_queue) {
697 		if (inp->inp_lport != lport)
698 			continue;
699 		wildcard = 0;
700 		if (!in_nullhost(inp->inp_faddr))
701 			wildcard++;
702 		if (in_nullhost(inp->inp_laddr)) {
703 			if (!in_nullhost(laddr))
704 				wildcard++;
705 		} else {
706 			if (in_nullhost(laddr))
707 				wildcard++;
708 			else {
709 				if (!in_hosteq(inp->inp_laddr, laddr))
710 					continue;
711 			}
712 		}
713 		if (wildcard && !lookup_wildcard)
714 			continue;
715 		if (wildcard < matchwild) {
716 			match = inp;
717 			matchwild = wildcard;
718 			if (matchwild == 0)
719 				break;
720 		}
721 	}
722 	return (match);
723 }
724 
725 #ifdef DIAGNOSTIC
726 int	in_pcbnotifymiss = 0;
727 #endif
728 
729 struct inpcb *
730 in_pcblookup_connect(table, faddr, fport_arg, laddr, lport_arg)
731 	struct inpcbtable *table;
732 	struct in_addr faddr, laddr;
733 	u_int fport_arg, lport_arg;
734 {
735 	struct inpcbhead *head;
736 	struct inpcb *inp;
737 	u_int16_t fport = fport_arg, lport = lport_arg;
738 
739 	head = INPCBHASH_CONNECT(table, faddr, fport, laddr, lport);
740 	LIST_FOREACH(inp, head, inp_hash) {
741 		if (in_hosteq(inp->inp_faddr, faddr) &&
742 		    inp->inp_fport == fport &&
743 		    inp->inp_lport == lport &&
744 		    in_hosteq(inp->inp_laddr, laddr))
745 			goto out;
746 	}
747 #ifdef DIAGNOSTIC
748 	if (in_pcbnotifymiss) {
749 		printf("in_pcblookup_connect: faddr=%08x fport=%d laddr=%08x lport=%d\n",
750 		    ntohl(faddr.s_addr), ntohs(fport),
751 		    ntohl(laddr.s_addr), ntohs(lport));
752 	}
753 #endif
754 	return (0);
755 
756 out:
757 	/* Move this PCB to the head of hash chain. */
758 	if (inp != LIST_FIRST(head)) {
759 		LIST_REMOVE(inp, inp_hash);
760 		LIST_INSERT_HEAD(head, inp, inp_hash);
761 	}
762 	return (inp);
763 }
764 
765 struct inpcb *
766 in_pcblookup_bind(table, laddr, lport_arg)
767 	struct inpcbtable *table;
768 	struct in_addr laddr;
769 	u_int lport_arg;
770 {
771 	struct inpcbhead *head;
772 	struct inpcb *inp;
773 	u_int16_t lport = lport_arg;
774 
775 	head = INPCBHASH_BIND(table, laddr, lport);
776 	LIST_FOREACH(inp, head, inp_hash) {
777 		if (inp->inp_lport == lport &&
778 		    in_hosteq(inp->inp_laddr, laddr))
779 			goto out;
780 	}
781 	head = INPCBHASH_BIND(table, zeroin_addr, lport);
782 	LIST_FOREACH(inp, head, inp_hash) {
783 		if (inp->inp_lport == lport &&
784 		    in_hosteq(inp->inp_laddr, zeroin_addr))
785 			goto out;
786 	}
787 #ifdef DIAGNOSTIC
788 	if (in_pcbnotifymiss) {
789 		printf("in_pcblookup_bind: laddr=%08x lport=%d\n",
790 		    ntohl(laddr.s_addr), ntohs(lport));
791 	}
792 #endif
793 	return (0);
794 
795 out:
796 	/* Move this PCB to the head of hash chain. */
797 	if (inp != LIST_FIRST(head)) {
798 		LIST_REMOVE(inp, inp_hash);
799 		LIST_INSERT_HEAD(head, inp, inp_hash);
800 	}
801 	return (inp);
802 }
803 
804 void
805 in_pcbstate(inp, state)
806 	struct inpcb *inp;
807 	int state;
808 {
809 
810 	if (inp->inp_state > INP_ATTACHED)
811 		LIST_REMOVE(inp, inp_hash);
812 
813 	switch (state) {
814 	case INP_BOUND:
815 		LIST_INSERT_HEAD(INPCBHASH_BIND(inp->inp_table,
816 		    inp->inp_laddr, inp->inp_lport), inp, inp_hash);
817 		break;
818 	case INP_CONNECTED:
819 		LIST_INSERT_HEAD(INPCBHASH_CONNECT(inp->inp_table,
820 		    inp->inp_faddr, inp->inp_fport,
821 		    inp->inp_laddr, inp->inp_lport), inp, inp_hash);
822 		break;
823 	}
824 
825 	inp->inp_state = state;
826 }
827 
828 struct rtentry *
829 in_pcbrtentry(inp)
830 	struct inpcb *inp;
831 {
832 	struct route *ro;
833 
834 	ro = &inp->inp_route;
835 
836 	if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
837 	    !in_hosteq(satosin(&ro->ro_dst)->sin_addr, inp->inp_faddr))) {
838 		RTFREE(ro->ro_rt);
839 		ro->ro_rt = (struct rtentry *)NULL;
840 	}
841 	if (ro->ro_rt == (struct rtentry *)NULL &&
842 	    !in_nullhost(inp->inp_faddr)) {
843 		bzero(&ro->ro_dst, sizeof(struct sockaddr_in));
844 		ro->ro_dst.sa_family = AF_INET;
845 		ro->ro_dst.sa_len = sizeof(ro->ro_dst);
846 		satosin(&ro->ro_dst)->sin_addr = inp->inp_faddr;
847 		rtalloc(ro);
848 	}
849 	return (ro->ro_rt);
850 }
851 
852 struct sockaddr_in *
853 in_selectsrc(sin, ro, soopts, mopts, errorp)
854 	struct sockaddr_in *sin;
855 	struct route *ro;
856 	int soopts;
857 	struct ip_moptions *mopts;
858 	int *errorp;
859 {
860 	struct in_ifaddr *ia;
861 
862 	ia = (struct in_ifaddr *)0;
863 	/*
864 	 * If route is known or can be allocated now,
865 	 * our src addr is taken from the i/f, else punt.
866 	 * Note that we should check the address family of the cached
867 	 * destination, in case of sharing the cache with IPv6.
868 	 */
869 	if (ro->ro_rt &&
870 	    (ro->ro_dst.sa_family != AF_INET ||
871 	    !in_hosteq(satosin(&ro->ro_dst)->sin_addr, sin->sin_addr) ||
872 	    soopts & SO_DONTROUTE)) {
873 		RTFREE(ro->ro_rt);
874 		ro->ro_rt = (struct rtentry *)0;
875 	}
876 	if ((soopts & SO_DONTROUTE) == 0 && /*XXX*/
877 	    (ro->ro_rt == (struct rtentry *)0 ||
878 	     ro->ro_rt->rt_ifp == (struct ifnet *)0)) {
879 		/* No route yet, so try to acquire one */
880 		bzero(&ro->ro_dst, sizeof(struct sockaddr_in));
881 		ro->ro_dst.sa_family = AF_INET;
882 		ro->ro_dst.sa_len = sizeof(struct sockaddr_in);
883 		satosin(&ro->ro_dst)->sin_addr = sin->sin_addr;
884 		rtalloc(ro);
885 	}
886 	/*
887 	 * If we found a route, use the address
888 	 * corresponding to the outgoing interface
889 	 * unless it is the loopback (in case a route
890 	 * to our address on another net goes to loopback).
891 	 *
892 	 * XXX Is this still true?  Do we care?
893 	 */
894 	if (ro->ro_rt && !(ro->ro_rt->rt_ifp->if_flags & IFF_LOOPBACK))
895 		ia = ifatoia(ro->ro_rt->rt_ifa);
896 	if (ia == NULL) {
897 		u_int16_t fport = sin->sin_port;
898 
899 		sin->sin_port = 0;
900 		ia = ifatoia(ifa_ifwithladdr(sintosa(sin)));
901 		sin->sin_port = fport;
902 		if (ia == 0) {
903 			/* Find 1st non-loopback AF_INET address */
904 			TAILQ_FOREACH(ia, &in_ifaddr, ia_list) {
905 				if (!(ia->ia_ifp->if_flags & IFF_LOOPBACK))
906 					break;
907 			}
908 		}
909 		if (ia == NULL) {
910 			*errorp = EADDRNOTAVAIL;
911 			return NULL;
912 		}
913 	}
914 	/*
915 	 * If the destination address is multicast and an outgoing
916 	 * interface has been set as a multicast option, use the
917 	 * address of that interface as our source address.
918 	 */
919 	if (IN_MULTICAST(sin->sin_addr.s_addr) && mopts != NULL) {
920 		struct ip_moptions *imo;
921 		struct ifnet *ifp;
922 
923 		imo = mopts;
924 		if (imo->imo_multicast_ifp != NULL) {
925 			ifp = imo->imo_multicast_ifp;
926 			IFP_TO_IA(ifp, ia);		/* XXX */
927 			if (ia == 0) {
928 				*errorp = EADDRNOTAVAIL;
929 				return NULL;
930 			}
931 		}
932 	}
933 	return satosin(&ia->ia_addr);
934 }
935