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