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