xref: /netbsd-src/sys/netinet/in_pcb.c (revision b757af438b42b93f8c6571f026d8b8ef3eaf5fc9)
1 /*	$NetBSD: in_pcb.c,v 1.141 2012/03/22 20:34:38 drochner 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, 2011 The NetBSD Foundation, Inc.
34  * All rights reserved.
35  *
36  * This code is derived from software contributed to The NetBSD Foundation
37  * by Coyote Point Systems, Inc.
38  * This code is derived from software contributed to The NetBSD Foundation
39  * by Public Access Networks Corporation ("Panix").  It was developed under
40  * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon.
41  *
42  * Redistribution and use in source and binary forms, with or without
43  * modification, are permitted provided that the following conditions
44  * are met:
45  * 1. Redistributions of source code must retain the above copyright
46  *    notice, this list of conditions and the following disclaimer.
47  * 2. Redistributions in binary form must reproduce the above copyright
48  *    notice, this list of conditions and the following disclaimer in the
49  *    documentation and/or other materials provided with the distribution.
50  *
51  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
52  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
53  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
54  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
55  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
56  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
57  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
58  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
59  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
60  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
61  * POSSIBILITY OF SUCH DAMAGE.
62  */
63 
64 /*
65  * Copyright (c) 1982, 1986, 1991, 1993, 1995
66  *	The Regents of the University of California.  All rights reserved.
67  *
68  * Redistribution and use in source and binary forms, with or without
69  * modification, are permitted provided that the following conditions
70  * are met:
71  * 1. Redistributions of source code must retain the above copyright
72  *    notice, this list of conditions and the following disclaimer.
73  * 2. Redistributions in binary form must reproduce the above copyright
74  *    notice, this list of conditions and the following disclaimer in the
75  *    documentation and/or other materials provided with the distribution.
76  * 3. Neither the name of the University nor the names of its contributors
77  *    may be used to endorse or promote products derived from this software
78  *    without specific prior written permission.
79  *
80  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
81  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
82  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
83  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
84  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
85  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
86  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
87  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
88  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
89  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
90  * SUCH DAMAGE.
91  *
92  *	@(#)in_pcb.c	8.4 (Berkeley) 5/24/95
93  */
94 
95 #include <sys/cdefs.h>
96 __KERNEL_RCSID(0, "$NetBSD: in_pcb.c,v 1.141 2012/03/22 20:34:38 drochner Exp $");
97 
98 #include "opt_inet.h"
99 #include "opt_ipsec.h"
100 
101 #include <sys/param.h>
102 #include <sys/systm.h>
103 #include <sys/malloc.h>
104 #include <sys/mbuf.h>
105 #include <sys/protosw.h>
106 #include <sys/socket.h>
107 #include <sys/socketvar.h>
108 #include <sys/ioctl.h>
109 #include <sys/errno.h>
110 #include <sys/time.h>
111 #include <sys/once.h>
112 #include <sys/pool.h>
113 #include <sys/proc.h>
114 #include <sys/kauth.h>
115 #include <sys/uidinfo.h>
116 #include <sys/domain.h>
117 
118 #include <net/if.h>
119 #include <net/route.h>
120 
121 #include <netinet/in.h>
122 #include <netinet/in_systm.h>
123 #include <netinet/ip.h>
124 #include <netinet/in_pcb.h>
125 #include <netinet/in_var.h>
126 #include <netinet/ip_var.h>
127 #include <netinet/rfc6056.h>
128 
129 #ifdef INET6
130 #include <netinet/ip6.h>
131 #include <netinet6/ip6_var.h>
132 #include <netinet6/in6_pcb.h>
133 #endif
134 
135 #ifdef FAST_IPSEC
136 #include <netipsec/ipsec.h>
137 #include <netipsec/key.h>
138 #endif /* IPSEC */
139 
140 #include <netinet/tcp_vtw.h>
141 
142 struct	in_addr zeroin_addr;
143 
144 #define	INPCBHASH_PORT(table, lport) \
145 	&(table)->inpt_porthashtbl[ntohs(lport) & (table)->inpt_porthash]
146 #define	INPCBHASH_BIND(table, laddr, lport) \
147 	&(table)->inpt_bindhashtbl[ \
148 	    ((ntohl((laddr).s_addr) + ntohs(lport))) & (table)->inpt_bindhash]
149 #define	INPCBHASH_CONNECT(table, faddr, fport, laddr, lport) \
150 	&(table)->inpt_connecthashtbl[ \
151 	    ((ntohl((faddr).s_addr) + ntohs(fport)) + \
152 	     (ntohl((laddr).s_addr) + ntohs(lport))) & (table)->inpt_connecthash]
153 
154 int	anonportmin = IPPORT_ANONMIN;
155 int	anonportmax = IPPORT_ANONMAX;
156 int	lowportmin  = IPPORT_RESERVEDMIN;
157 int	lowportmax  = IPPORT_RESERVEDMAX;
158 
159 static struct pool inpcb_pool;
160 
161 static int
162 inpcb_poolinit(void)
163 {
164 
165 	pool_init(&inpcb_pool, sizeof(struct inpcb), 0, 0, 0, "inpcbpl", NULL,
166 	    IPL_NET);
167 	return 0;
168 }
169 
170 void
171 in_pcbinit(struct inpcbtable *table, int bindhashsize, int connecthashsize)
172 {
173 	static ONCE_DECL(control);
174 
175 	CIRCLEQ_INIT(&table->inpt_queue);
176 	table->inpt_porthashtbl = hashinit(bindhashsize, HASH_LIST, true,
177 	    &table->inpt_porthash);
178 	table->inpt_bindhashtbl = hashinit(bindhashsize, HASH_LIST, true,
179 	    &table->inpt_bindhash);
180 	table->inpt_connecthashtbl = hashinit(connecthashsize, HASH_LIST, true,
181 	    &table->inpt_connecthash);
182 	table->inpt_lastlow = IPPORT_RESERVEDMAX;
183 	table->inpt_lastport = (u_int16_t)anonportmax;
184 
185 	RUN_ONCE(&control, inpcb_poolinit);
186 }
187 
188 int
189 in_pcballoc(struct socket *so, void *v)
190 {
191 	struct inpcbtable *table = v;
192 	struct inpcb *inp;
193 	int s;
194 #if defined(FAST_IPSEC)
195 	int error;
196 #endif
197 
198 	s = splnet();
199 	inp = pool_get(&inpcb_pool, PR_NOWAIT);
200 	splx(s);
201 	if (inp == NULL)
202 		return (ENOBUFS);
203 	memset(inp, 0, sizeof(*inp));
204 	inp->inp_af = AF_INET;
205 	inp->inp_table = table;
206 	inp->inp_socket = so;
207 	inp->inp_errormtu = -1;
208 	inp->inp_rfc6056algo = RFC6056_ALGO_DEFAULT;
209 	inp->inp_bindportonsend = false;
210 #if defined(FAST_IPSEC)
211 	error = ipsec_init_pcbpolicy(so, &inp->inp_sp);
212 	if (error != 0) {
213 		s = splnet();
214 		pool_put(&inpcb_pool, inp);
215 		splx(s);
216 		return error;
217 	}
218 #endif
219 	so->so_pcb = inp;
220 	s = splnet();
221 	CIRCLEQ_INSERT_HEAD(&table->inpt_queue, &inp->inp_head,
222 	    inph_queue);
223 	LIST_INSERT_HEAD(INPCBHASH_PORT(table, inp->inp_lport), &inp->inp_head,
224 	    inph_lhash);
225 	in_pcbstate(inp, INP_ATTACHED);
226 	splx(s);
227 	return (0);
228 }
229 
230 static int
231 in_pcbsetport(struct sockaddr_in *sin, struct inpcb *inp, kauth_cred_t cred)
232 {
233 	struct inpcbtable *table = inp->inp_table;
234 	struct socket *so = inp->inp_socket;
235 	u_int16_t *lastport;
236 	u_int16_t lport = 0;
237 	enum kauth_network_req req;
238 	int error;
239 
240 	if (inp->inp_flags & INP_LOWPORT) {
241 #ifndef IPNOPRIVPORTS
242 		req = KAUTH_REQ_NETWORK_BIND_PRIVPORT;
243 #else
244 		req = KAUTH_REQ_NETWORK_BIND_PORT;
245 #endif
246 
247 		lastport = &table->inpt_lastlow;
248 	} else {
249 		req = KAUTH_REQ_NETWORK_BIND_PORT;
250 
251 		lastport = &table->inpt_lastport;
252 	}
253 
254 	/* XXX-kauth: KAUTH_REQ_NETWORK_BIND_AUTOASSIGN_{,PRIV}PORT */
255 	error = kauth_authorize_network(cred, KAUTH_NETWORK_BIND, req, so, sin,
256 	    NULL);
257 	if (error)
258 		return (EACCES);
259 
260        /*
261         * Use RFC6056 randomized port selection
262         */
263 	error = rfc6056_randport(&lport, &inp->inp_head, cred);
264 	if (error)
265 		return error;
266 
267 	inp->inp_flags |= INP_ANONPORT;
268 	*lastport = lport;
269 	lport = htons(lport);
270 	inp->inp_lport = lport;
271 	in_pcbstate(inp, INP_BOUND);
272 
273 	return (0);
274 }
275 
276 static int
277 in_pcbbind_addr(struct inpcb *inp, struct sockaddr_in *sin, kauth_cred_t cred)
278 {
279 	if (sin->sin_family != AF_INET)
280 		return (EAFNOSUPPORT);
281 
282 	if (IN_MULTICAST(sin->sin_addr.s_addr)) {
283 		/* Always succeed; port reuse handled in in_pcbbind_port(). */
284 	} else if (!in_nullhost(sin->sin_addr)) {
285 		struct in_ifaddr *ia = NULL;
286 
287 		INADDR_TO_IA(sin->sin_addr, ia);
288 		/* check for broadcast addresses */
289 		if (ia == NULL)
290 			ia = ifatoia(ifa_ifwithaddr(sintosa(sin)));
291 		if (ia == NULL)
292 			return (EADDRNOTAVAIL);
293 	}
294 
295 	inp->inp_laddr = sin->sin_addr;
296 
297 	return (0);
298 }
299 
300 static int
301 in_pcbbind_port(struct inpcb *inp, struct sockaddr_in *sin, kauth_cred_t cred)
302 {
303 	struct inpcbtable *table = inp->inp_table;
304 	struct socket *so = inp->inp_socket;
305 	int reuseport = (so->so_options & SO_REUSEPORT);
306 	int wild = 0, error;
307 
308 	if (IN_MULTICAST(sin->sin_addr.s_addr)) {
309 		/*
310 		 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
311 		 * allow complete duplication of binding if
312 		 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
313 		 * and a multicast address is bound on both
314 		 * new and duplicated sockets.
315 		 */
316 		if (so->so_options & SO_REUSEADDR)
317 			reuseport = SO_REUSEADDR|SO_REUSEPORT;
318 	}
319 
320 	if (sin->sin_port == 0) {
321 		error = in_pcbsetport(sin, inp, cred);
322 		if (error)
323 			return (error);
324 	} else {
325 		struct inpcb *t;
326 		vestigial_inpcb_t vestige;
327 #ifdef INET6
328 		struct in6pcb *t6;
329 		struct in6_addr mapped;
330 #endif
331 		enum kauth_network_req req;
332 
333 		if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
334 			wild = 1;
335 
336 #ifndef IPNOPRIVPORTS
337 		if (ntohs(sin->sin_port) < IPPORT_RESERVED)
338 			req = KAUTH_REQ_NETWORK_BIND_PRIVPORT;
339 		else
340 #endif /* !IPNOPRIVPORTS */
341 			req = KAUTH_REQ_NETWORK_BIND_PORT;
342 
343 		error = kauth_authorize_network(cred, KAUTH_NETWORK_BIND, req,
344 		    so, sin, NULL);
345 		if (error)
346 			return (EACCES);
347 
348 #ifdef INET6
349 		memset(&mapped, 0, sizeof(mapped));
350 		mapped.s6_addr16[5] = 0xffff;
351 		memcpy(&mapped.s6_addr32[3], &sin->sin_addr,
352 		    sizeof(mapped.s6_addr32[3]));
353 		t6 = in6_pcblookup_port(table, &mapped, sin->sin_port, wild, &vestige);
354 		if (t6 && (reuseport & t6->in6p_socket->so_options) == 0)
355 			return (EADDRINUSE);
356 		if (!t6 && vestige.valid) {
357 		    if (!!reuseport != !!vestige.reuse_port) {
358 			return EADDRINUSE;
359 		    }
360 		}
361 #endif
362 
363 		/* XXX-kauth */
364 		if (so->so_uidinfo->ui_uid && !IN_MULTICAST(sin->sin_addr.s_addr)) {
365 			t = in_pcblookup_port(table, sin->sin_addr, sin->sin_port, 1, &vestige);
366 			/*
367 			 * XXX:	investigate ramifications of loosening this
368 			 *	restriction so that as long as both ports have
369 			 *	SO_REUSEPORT allow the bind
370 			 */
371 			if (t &&
372 			    (!in_nullhost(sin->sin_addr) ||
373 			     !in_nullhost(t->inp_laddr) ||
374 			     (t->inp_socket->so_options & SO_REUSEPORT) == 0)
375 			    && (so->so_uidinfo->ui_uid != t->inp_socket->so_uidinfo->ui_uid)) {
376 				return (EADDRINUSE);
377 			}
378 			if (!t && vestige.valid) {
379 				if ((!in_nullhost(sin->sin_addr)
380 				     || !in_nullhost(vestige.laddr.v4)
381 				     || !vestige.reuse_port)
382 				    && so->so_uidinfo->ui_uid != vestige.uid) {
383 					return EADDRINUSE;
384 				}
385 			}
386 		}
387 		t = in_pcblookup_port(table, sin->sin_addr, sin->sin_port, wild, &vestige);
388 		if (t && (reuseport & t->inp_socket->so_options) == 0)
389 			return (EADDRINUSE);
390 		if (!t
391 		    && vestige.valid
392 		    && !(reuseport && vestige.reuse_port))
393 			return EADDRINUSE;
394 
395 		inp->inp_lport = sin->sin_port;
396 		in_pcbstate(inp, INP_BOUND);
397 	}
398 
399 	LIST_REMOVE(&inp->inp_head, inph_lhash);
400 	LIST_INSERT_HEAD(INPCBHASH_PORT(table, inp->inp_lport), &inp->inp_head,
401 	    inph_lhash);
402 
403 	return (0);
404 }
405 
406 int
407 in_pcbbind(void *v, struct mbuf *nam, struct lwp *l)
408 {
409 	struct inpcb *inp = v;
410 	struct sockaddr_in *sin = NULL; /* XXXGCC */
411 	struct sockaddr_in lsin;
412 	int error;
413 
414 	if (inp->inp_af != AF_INET)
415 		return (EINVAL);
416 
417 	if (TAILQ_FIRST(&in_ifaddrhead) == 0)
418 		return (EADDRNOTAVAIL);
419 	if (inp->inp_lport || !in_nullhost(inp->inp_laddr))
420 		return (EINVAL);
421 
422 	if (nam != NULL) {
423 		sin = mtod(nam, struct sockaddr_in *);
424 		if (nam->m_len != sizeof (*sin))
425 			return (EINVAL);
426 	} else {
427 		lsin = *((const struct sockaddr_in *)
428 		    inp->inp_socket->so_proto->pr_domain->dom_sa_any);
429 		sin = &lsin;
430 	}
431 
432 	/* Bind address. */
433 	error = in_pcbbind_addr(inp, sin, l->l_cred);
434 	if (error)
435 		return (error);
436 
437 	/* Bind port. */
438 	error = in_pcbbind_port(inp, sin, l->l_cred);
439 	if (error) {
440 		inp->inp_laddr.s_addr = INADDR_ANY;
441 
442 		return (error);
443 	}
444 
445 	return (0);
446 }
447 
448 /*
449  * Connect from a socket to a specified address.
450  * Both address and port must be specified in argument sin.
451  * If don't have a local address for this socket yet,
452  * then pick one.
453  */
454 int
455 in_pcbconnect(void *v, struct mbuf *nam, struct lwp *l)
456 {
457 	struct inpcb *inp = v;
458 	struct in_ifaddr *ia = NULL;
459 	struct sockaddr_in *ifaddr = NULL;
460 	struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *);
461 	vestigial_inpcb_t vestige;
462 	int error;
463 
464 	if (inp->inp_af != AF_INET)
465 		return (EINVAL);
466 
467 	if (nam->m_len != sizeof (*sin))
468 		return (EINVAL);
469 	if (sin->sin_family != AF_INET)
470 		return (EAFNOSUPPORT);
471 	if (sin->sin_port == 0)
472 		return (EADDRNOTAVAIL);
473 	if (TAILQ_FIRST(&in_ifaddrhead) != 0) {
474 		/*
475 		 * If the destination address is INADDR_ANY,
476 		 * use any local address (likely loopback).
477 		 * If the supplied address is INADDR_BROADCAST,
478 		 * use the broadcast address of an interface
479 		 * which supports broadcast. (loopback does not)
480 		 */
481 
482 		if (in_nullhost(sin->sin_addr)) {
483 			sin->sin_addr =
484 			    TAILQ_FIRST(&in_ifaddrhead)->ia_addr.sin_addr;
485 		} else if (sin->sin_addr.s_addr == INADDR_BROADCAST) {
486 			TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) {
487 				if (ia->ia_ifp->if_flags & IFF_BROADCAST) {
488 					sin->sin_addr =
489 					    ia->ia_broadaddr.sin_addr;
490 					break;
491 				}
492 			}
493 		}
494 	}
495 	/*
496 	 * If we haven't bound which network number to use as ours,
497 	 * we will use the number of the outgoing interface.
498 	 * This depends on having done a routing lookup, which
499 	 * we will probably have to do anyway, so we might
500 	 * as well do it now.  On the other hand if we are
501 	 * sending to multiple destinations we may have already
502 	 * done the lookup, so see if we can use the route
503 	 * from before.  In any case, we only
504 	 * chose a port number once, even if sending to multiple
505 	 * destinations.
506 	 */
507 	if (in_nullhost(inp->inp_laddr)) {
508 		int xerror;
509 		ifaddr = in_selectsrc(sin, &inp->inp_route,
510 		    inp->inp_socket->so_options, inp->inp_moptions, &xerror);
511 		if (ifaddr == NULL) {
512 			if (xerror == 0)
513 				xerror = EADDRNOTAVAIL;
514 			return xerror;
515 		}
516 		INADDR_TO_IA(ifaddr->sin_addr, ia);
517 		if (ia == NULL)
518 			return (EADDRNOTAVAIL);
519 	}
520 	if (in_pcblookup_connect(inp->inp_table, sin->sin_addr, sin->sin_port,
521 	    !in_nullhost(inp->inp_laddr) ? inp->inp_laddr : ifaddr->sin_addr,
522 				 inp->inp_lport, &vestige) != 0
523 	    || vestige.valid)
524 		return (EADDRINUSE);
525 	if (in_nullhost(inp->inp_laddr)) {
526 		if (inp->inp_lport == 0) {
527 			error = in_pcbbind(inp, NULL, l);
528 			/*
529 			 * This used to ignore the return value
530 			 * completely, but we need to check for
531 			 * ephemeral port shortage.
532 			 * And attempts to request low ports if not root.
533 			 */
534 			if (error != 0)
535 				return (error);
536 		}
537 		inp->inp_laddr = ifaddr->sin_addr;
538 	}
539 	inp->inp_faddr = sin->sin_addr;
540 	inp->inp_fport = sin->sin_port;
541 
542         /* Late bind, if needed */
543 	if (inp->inp_bindportonsend) {
544                struct sockaddr_in lsin = *((const struct sockaddr_in *)
545 		    inp->inp_socket->so_proto->pr_domain->dom_sa_any);
546 		lsin.sin_addr = inp->inp_laddr;
547 		lsin.sin_port = 0;
548 
549                if ((error = in_pcbbind_port(inp, &lsin, l->l_cred)) != 0)
550                        return error;
551 	}
552 
553 	in_pcbstate(inp, INP_CONNECTED);
554 #if defined(FAST_IPSEC)
555 	if (inp->inp_socket->so_type == SOCK_STREAM)
556 		ipsec_pcbconn(inp->inp_sp);
557 #endif
558 	return (0);
559 }
560 
561 void
562 in_pcbdisconnect(void *v)
563 {
564 	struct inpcb *inp = v;
565 
566 	if (inp->inp_af != AF_INET)
567 		return;
568 
569 	inp->inp_faddr = zeroin_addr;
570 	inp->inp_fport = 0;
571 	in_pcbstate(inp, INP_BOUND);
572 #if defined(FAST_IPSEC)
573 	ipsec_pcbdisconn(inp->inp_sp);
574 #endif
575 	if (inp->inp_socket->so_state & SS_NOFDREF)
576 		in_pcbdetach(inp);
577 }
578 
579 void
580 in_pcbdetach(void *v)
581 {
582 	struct inpcb *inp = v;
583 	struct socket *so = inp->inp_socket;
584 	int s;
585 
586 	if (inp->inp_af != AF_INET)
587 		return;
588 
589 #if defined(FAST_IPSEC)
590 	ipsec4_delete_pcbpolicy(inp);
591 #endif /*IPSEC*/
592 	so->so_pcb = 0;
593 	if (inp->inp_options)
594 		(void)m_free(inp->inp_options);
595 	rtcache_free(&inp->inp_route);
596 	ip_freemoptions(inp->inp_moptions);
597 	s = splnet();
598 	in_pcbstate(inp, INP_ATTACHED);
599 	LIST_REMOVE(&inp->inp_head, inph_lhash);
600 	CIRCLEQ_REMOVE(&inp->inp_table->inpt_queue, &inp->inp_head,
601 	    inph_queue);
602 	pool_put(&inpcb_pool, inp);
603 	splx(s);
604 	sofree(so);			/* drops the socket's lock */
605 	mutex_enter(softnet_lock);	/* reacquire the softnet_lock */
606 }
607 
608 void
609 in_setsockaddr(struct inpcb *inp, struct mbuf *nam)
610 {
611 	struct sockaddr_in *sin;
612 
613 	if (inp->inp_af != AF_INET)
614 		return;
615 
616 	sin = mtod(nam, struct sockaddr_in *);
617 	sockaddr_in_init(sin, &inp->inp_laddr, inp->inp_lport);
618 	nam->m_len = sin->sin_len;
619 }
620 
621 void
622 in_setpeeraddr(struct inpcb *inp, struct mbuf *nam)
623 {
624 	struct sockaddr_in *sin;
625 
626 	if (inp->inp_af != AF_INET)
627 		return;
628 
629 	sin = mtod(nam, struct sockaddr_in *);
630 	sockaddr_in_init(sin, &inp->inp_faddr, inp->inp_fport);
631 	nam->m_len = sin->sin_len;
632 }
633 
634 /*
635  * Pass some notification to all connections of a protocol
636  * associated with address dst.  The local address and/or port numbers
637  * may be specified to limit the search.  The "usual action" will be
638  * taken, depending on the ctlinput cmd.  The caller must filter any
639  * cmds that are uninteresting (e.g., no error in the map).
640  * Call the protocol specific routine (if any) to report
641  * any errors for each matching socket.
642  *
643  * Must be called at splsoftnet.
644  */
645 int
646 in_pcbnotify(struct inpcbtable *table, struct in_addr faddr, u_int fport_arg,
647     struct in_addr laddr, u_int lport_arg, int errno,
648     void (*notify)(struct inpcb *, int))
649 {
650 	struct inpcbhead *head;
651 	struct inpcb *inp, *ninp;
652 	u_int16_t fport = fport_arg, lport = lport_arg;
653 	int nmatch;
654 
655 	if (in_nullhost(faddr) || notify == 0)
656 		return (0);
657 
658 	nmatch = 0;
659 	head = INPCBHASH_CONNECT(table, faddr, fport, laddr, lport);
660 	for (inp = (struct inpcb *)LIST_FIRST(head); inp != NULL; inp = ninp) {
661 		ninp = (struct inpcb *)LIST_NEXT(inp, inp_hash);
662 		if (inp->inp_af != AF_INET)
663 			continue;
664 		if (in_hosteq(inp->inp_faddr, faddr) &&
665 		    inp->inp_fport == fport &&
666 		    inp->inp_lport == lport &&
667 		    in_hosteq(inp->inp_laddr, laddr)) {
668 			(*notify)(inp, errno);
669 			nmatch++;
670 		}
671 	}
672 	return (nmatch);
673 }
674 
675 void
676 in_pcbnotifyall(struct inpcbtable *table, struct in_addr faddr, int errno,
677     void (*notify)(struct inpcb *, int))
678 {
679 	struct inpcb *inp, *ninp;
680 
681 	if (in_nullhost(faddr) || notify == 0)
682 		return;
683 
684 	for (inp = (struct inpcb *)CIRCLEQ_FIRST(&table->inpt_queue);
685 	    inp != (void *)&table->inpt_queue;
686 	    inp = ninp) {
687 		ninp = (struct inpcb *)CIRCLEQ_NEXT(inp, inp_queue);
688 		if (inp->inp_af != AF_INET)
689 			continue;
690 		if (in_hosteq(inp->inp_faddr, faddr))
691 			(*notify)(inp, errno);
692 	}
693 }
694 
695 void
696 in_pcbpurgeif0(struct inpcbtable *table, struct ifnet *ifp)
697 {
698 	struct inpcb *inp, *ninp;
699 	struct ip_moptions *imo;
700 	int i, gap;
701 
702 	for (inp = (struct inpcb *)CIRCLEQ_FIRST(&table->inpt_queue);
703 	    inp != (void *)&table->inpt_queue;
704 	    inp = ninp) {
705 		ninp = (struct inpcb *)CIRCLEQ_NEXT(inp, inp_queue);
706 		if (inp->inp_af != AF_INET)
707 			continue;
708 		imo = inp->inp_moptions;
709 		if (imo != NULL) {
710 			/*
711 			 * Unselect the outgoing interface if it is being
712 			 * detached.
713 			 */
714 			if (imo->imo_multicast_ifp == ifp)
715 				imo->imo_multicast_ifp = NULL;
716 
717 			/*
718 			 * Drop multicast group membership if we joined
719 			 * through the interface being detached.
720 			 */
721 			for (i = 0, gap = 0; i < imo->imo_num_memberships;
722 			    i++) {
723 				if (imo->imo_membership[i]->inm_ifp == ifp) {
724 					in_delmulti(imo->imo_membership[i]);
725 					gap++;
726 				} else if (gap != 0)
727 					imo->imo_membership[i - gap] =
728 					    imo->imo_membership[i];
729 			}
730 			imo->imo_num_memberships -= gap;
731 		}
732 	}
733 }
734 
735 void
736 in_pcbpurgeif(struct inpcbtable *table, struct ifnet *ifp)
737 {
738 	struct rtentry *rt;
739 	struct inpcb *inp, *ninp;
740 
741 	for (inp = (struct inpcb *)CIRCLEQ_FIRST(&table->inpt_queue);
742 	    inp != (void *)&table->inpt_queue;
743 	    inp = ninp) {
744 		ninp = (struct inpcb *)CIRCLEQ_NEXT(inp, inp_queue);
745 		if (inp->inp_af != AF_INET)
746 			continue;
747 		if ((rt = rtcache_validate(&inp->inp_route)) != NULL &&
748 		    rt->rt_ifp == ifp)
749 			in_rtchange(inp, 0);
750 	}
751 }
752 
753 /*
754  * Check for alternatives when higher level complains
755  * about service problems.  For now, invalidate cached
756  * routing information.  If the route was created dynamically
757  * (by a redirect), time to try a default gateway again.
758  */
759 void
760 in_losing(struct inpcb *inp)
761 {
762 	struct rtentry *rt;
763 	struct rt_addrinfo info;
764 
765 	if (inp->inp_af != AF_INET)
766 		return;
767 
768 	if ((rt = rtcache_validate(&inp->inp_route)) == NULL)
769 		return;
770 
771 	memset(&info, 0, sizeof(info));
772 	info.rti_info[RTAX_DST] = rtcache_getdst(&inp->inp_route);
773 	info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
774 	info.rti_info[RTAX_NETMASK] = rt_mask(rt);
775 	rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
776 	if (rt->rt_flags & RTF_DYNAMIC)
777 		(void) rtrequest(RTM_DELETE, rt_getkey(rt),
778 			rt->rt_gateway, rt_mask(rt), rt->rt_flags,
779 			NULL);
780 	/*
781 	 * A new route can be allocated
782 	 * the next time output is attempted.
783 	 */
784 	rtcache_free(&inp->inp_route);
785 }
786 
787 /*
788  * After a routing change, flush old routing.  A new route can be
789  * allocated the next time output is attempted.
790  */
791 void
792 in_rtchange(struct inpcb *inp, int errno)
793 {
794 
795 	if (inp->inp_af != AF_INET)
796 		return;
797 
798 	rtcache_free(&inp->inp_route);
799 
800 	/* XXX SHOULD NOTIFY HIGHER-LEVEL PROTOCOLS */
801 }
802 
803 struct inpcb *
804 in_pcblookup_port(struct inpcbtable *table, struct in_addr laddr,
805 		  u_int lport_arg, int lookup_wildcard, vestigial_inpcb_t *vp)
806 {
807 	struct inpcbhead *head;
808 	struct inpcb_hdr *inph;
809 	struct inpcb *inp, *match = 0;
810 	int matchwild = 3, wildcard;
811 	u_int16_t lport = lport_arg;
812 
813 	if (vp)
814 		vp->valid = 0;
815 
816 	head = INPCBHASH_PORT(table, lport);
817 	LIST_FOREACH(inph, head, inph_lhash) {
818 		inp = (struct inpcb *)inph;
819 		if (inp->inp_af != AF_INET)
820 			continue;
821 
822 		if (inp->inp_lport != lport)
823 			continue;
824 		wildcard = 0;
825 		if (!in_nullhost(inp->inp_faddr))
826 			wildcard++;
827 		if (in_nullhost(inp->inp_laddr)) {
828 			if (!in_nullhost(laddr))
829 				wildcard++;
830 		} else {
831 			if (in_nullhost(laddr))
832 				wildcard++;
833 			else {
834 				if (!in_hosteq(inp->inp_laddr, laddr))
835 					continue;
836 			}
837 		}
838 		if (wildcard && !lookup_wildcard)
839 			continue;
840 		if (wildcard < matchwild) {
841 			match = inp;
842 			matchwild = wildcard;
843 			if (matchwild == 0)
844 				break;
845 		}
846 	}
847 	if (match && matchwild == 0)
848 		return match;
849 
850 	if (vp && table->vestige) {
851 		void	*state = (*table->vestige->init_ports4)(laddr, lport_arg, lookup_wildcard);
852 		vestigial_inpcb_t better;
853 
854 		while (table->vestige
855 		       && (*table->vestige->next_port4)(state, vp)) {
856 
857 			if (vp->lport != lport)
858 				continue;
859 			wildcard = 0;
860 			if (!in_nullhost(vp->faddr.v4))
861 				wildcard++;
862 			if (in_nullhost(vp->laddr.v4)) {
863 				if (!in_nullhost(laddr))
864 					wildcard++;
865 			} else {
866 				if (in_nullhost(laddr))
867 					wildcard++;
868 				else {
869 					if (!in_hosteq(vp->laddr.v4, laddr))
870 						continue;
871 				}
872 			}
873 			if (wildcard && !lookup_wildcard)
874 				continue;
875 			if (wildcard < matchwild) {
876 				better = *vp;
877 				match  = (void*)&better;
878 
879 				matchwild = wildcard;
880 				if (matchwild == 0)
881 					break;
882 			}
883 		}
884 
885 		if (match) {
886 			if (match != (void*)&better)
887 				return match;
888 			else {
889 				*vp = better;
890 				return 0;
891 			}
892 		}
893 	}
894 
895 	return (match);
896 }
897 
898 #ifdef DIAGNOSTIC
899 int	in_pcbnotifymiss = 0;
900 #endif
901 
902 struct inpcb *
903 in_pcblookup_connect(struct inpcbtable *table,
904     struct in_addr faddr, u_int fport_arg,
905     struct in_addr laddr, u_int lport_arg,
906     vestigial_inpcb_t *vp)
907 {
908 	struct inpcbhead *head;
909 	struct inpcb_hdr *inph;
910 	struct inpcb *inp;
911 	u_int16_t fport = fport_arg, lport = lport_arg;
912 
913 	if (vp)
914 		vp->valid = 0;
915 
916 	head = INPCBHASH_CONNECT(table, faddr, fport, laddr, lport);
917 	LIST_FOREACH(inph, head, inph_hash) {
918 		inp = (struct inpcb *)inph;
919 		if (inp->inp_af != AF_INET)
920 			continue;
921 
922 		if (in_hosteq(inp->inp_faddr, faddr) &&
923 		    inp->inp_fport == fport &&
924 		    inp->inp_lport == lport &&
925 		    in_hosteq(inp->inp_laddr, laddr))
926 			goto out;
927 	}
928 	if (vp && table->vestige) {
929 		if ((*table->vestige->lookup4)(faddr, fport_arg,
930 					       laddr, lport_arg, vp))
931 			return 0;
932 	}
933 
934 #ifdef DIAGNOSTIC
935 	if (in_pcbnotifymiss) {
936 		printf("in_pcblookup_connect: faddr=%08x fport=%d laddr=%08x lport=%d\n",
937 		    ntohl(faddr.s_addr), ntohs(fport),
938 		    ntohl(laddr.s_addr), ntohs(lport));
939 	}
940 #endif
941 	return (0);
942 
943 out:
944 	/* Move this PCB to the head of hash chain. */
945 	inph = &inp->inp_head;
946 	if (inph != LIST_FIRST(head)) {
947 		LIST_REMOVE(inph, inph_hash);
948 		LIST_INSERT_HEAD(head, inph, inph_hash);
949 	}
950 	return (inp);
951 }
952 
953 struct inpcb *
954 in_pcblookup_bind(struct inpcbtable *table,
955     struct in_addr laddr, u_int lport_arg)
956 {
957 	struct inpcbhead *head;
958 	struct inpcb_hdr *inph;
959 	struct inpcb *inp;
960 	u_int16_t lport = lport_arg;
961 
962 	head = INPCBHASH_BIND(table, laddr, lport);
963 	LIST_FOREACH(inph, head, inph_hash) {
964 		inp = (struct inpcb *)inph;
965 		if (inp->inp_af != AF_INET)
966 			continue;
967 
968 		if (inp->inp_lport == lport &&
969 		    in_hosteq(inp->inp_laddr, laddr))
970 			goto out;
971 	}
972 	head = INPCBHASH_BIND(table, zeroin_addr, lport);
973 	LIST_FOREACH(inph, head, inph_hash) {
974 		inp = (struct inpcb *)inph;
975 		if (inp->inp_af != AF_INET)
976 			continue;
977 
978 		if (inp->inp_lport == lport &&
979 		    in_hosteq(inp->inp_laddr, zeroin_addr))
980 			goto out;
981 	}
982 #ifdef DIAGNOSTIC
983 	if (in_pcbnotifymiss) {
984 		printf("in_pcblookup_bind: laddr=%08x lport=%d\n",
985 		    ntohl(laddr.s_addr), ntohs(lport));
986 	}
987 #endif
988 	return (0);
989 
990 out:
991 	/* Move this PCB to the head of hash chain. */
992 	inph = &inp->inp_head;
993 	if (inph != LIST_FIRST(head)) {
994 		LIST_REMOVE(inph, inph_hash);
995 		LIST_INSERT_HEAD(head, inph, inph_hash);
996 	}
997 	return (inp);
998 }
999 
1000 void
1001 in_pcbstate(struct inpcb *inp, int state)
1002 {
1003 
1004 	if (inp->inp_af != AF_INET)
1005 		return;
1006 
1007 	if (inp->inp_state > INP_ATTACHED)
1008 		LIST_REMOVE(&inp->inp_head, inph_hash);
1009 
1010 	switch (state) {
1011 	case INP_BOUND:
1012 		LIST_INSERT_HEAD(INPCBHASH_BIND(inp->inp_table,
1013 		    inp->inp_laddr, inp->inp_lport), &inp->inp_head,
1014 		    inph_hash);
1015 		break;
1016 	case INP_CONNECTED:
1017 		LIST_INSERT_HEAD(INPCBHASH_CONNECT(inp->inp_table,
1018 		    inp->inp_faddr, inp->inp_fport,
1019 		    inp->inp_laddr, inp->inp_lport), &inp->inp_head,
1020 		    inph_hash);
1021 		break;
1022 	}
1023 
1024 	inp->inp_state = state;
1025 }
1026 
1027 struct rtentry *
1028 in_pcbrtentry(struct inpcb *inp)
1029 {
1030 	struct route *ro;
1031 	union {
1032 		struct sockaddr		dst;
1033 		struct sockaddr_in	dst4;
1034 	} u;
1035 
1036 	if (inp->inp_af != AF_INET)
1037 		return (NULL);
1038 
1039 	ro = &inp->inp_route;
1040 
1041 	sockaddr_in_init(&u.dst4, &inp->inp_faddr, 0);
1042 	return rtcache_lookup(ro, &u.dst);
1043 }
1044 
1045 struct sockaddr_in *
1046 in_selectsrc(struct sockaddr_in *sin, struct route *ro,
1047     int soopts, struct ip_moptions *mopts, int *errorp)
1048 {
1049 	struct rtentry *rt = NULL;
1050 	struct in_ifaddr *ia = NULL;
1051 
1052 	/*
1053          * If route is known or can be allocated now, take the
1054          * source address from the interface.  Otherwise, punt.
1055 	 */
1056 	if ((soopts & SO_DONTROUTE) != 0)
1057 		rtcache_free(ro);
1058 	else {
1059 		union {
1060 			struct sockaddr		dst;
1061 			struct sockaddr_in	dst4;
1062 		} u;
1063 
1064 		sockaddr_in_init(&u.dst4, &sin->sin_addr, 0);
1065 		rt = rtcache_lookup(ro, &u.dst);
1066 	}
1067 	/*
1068 	 * If we found a route, use the address
1069 	 * corresponding to the outgoing interface
1070 	 * unless it is the loopback (in case a route
1071 	 * to our address on another net goes to loopback).
1072 	 *
1073 	 * XXX Is this still true?  Do we care?
1074 	 */
1075 	if (rt != NULL && (rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0)
1076 		ia = ifatoia(rt->rt_ifa);
1077 	if (ia == NULL) {
1078 		u_int16_t fport = sin->sin_port;
1079 
1080 		sin->sin_port = 0;
1081 		ia = ifatoia(ifa_ifwithladdr(sintosa(sin)));
1082 		sin->sin_port = fport;
1083 		if (ia == NULL) {
1084 			/* Find 1st non-loopback AF_INET address */
1085 			TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) {
1086 				if (!(ia->ia_ifp->if_flags & IFF_LOOPBACK))
1087 					break;
1088 			}
1089 		}
1090 		if (ia == NULL) {
1091 			*errorp = EADDRNOTAVAIL;
1092 			return NULL;
1093 		}
1094 	}
1095 	/*
1096 	 * If the destination address is multicast and an outgoing
1097 	 * interface has been set as a multicast option, use the
1098 	 * address of that interface as our source address.
1099 	 */
1100 	if (IN_MULTICAST(sin->sin_addr.s_addr) && mopts != NULL) {
1101 		struct ip_moptions *imo;
1102 		struct ifnet *ifp;
1103 
1104 		imo = mopts;
1105 		if (imo->imo_multicast_ifp != NULL) {
1106 			ifp = imo->imo_multicast_ifp;
1107 			IFP_TO_IA(ifp, ia);		/* XXX */
1108 			if (ia == 0) {
1109 				*errorp = EADDRNOTAVAIL;
1110 				return NULL;
1111 			}
1112 		}
1113 	}
1114 	if (ia->ia_ifa.ifa_getifa != NULL) {
1115 		ia = ifatoia((*ia->ia_ifa.ifa_getifa)(&ia->ia_ifa,
1116 		                                      sintosa(sin)));
1117 	}
1118 #ifdef GETIFA_DEBUG
1119 	else
1120 		printf("%s: missing ifa_getifa\n", __func__);
1121 #endif
1122 	return satosin(&ia->ia_addr);
1123 }
1124