in_pcb.c revision 1.176 1 /* $NetBSD: in_pcb.c,v 1.176 2017/03/02 05:29:31 ozaki-r 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.176 2017/03/02 05:29:31 ozaki-r Exp $");
97
98 #ifdef _KERNEL_OPT
99 #include "opt_inet.h"
100 #include "opt_ipsec.h"
101 #endif
102
103 #include <sys/param.h>
104 #include <sys/systm.h>
105 #include <sys/mbuf.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/portalgo.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 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 TAILQ_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
195 inp = pool_get(&inpcb_pool, PR_NOWAIT);
196 if (inp == NULL)
197 return (ENOBUFS);
198 memset(inp, 0, sizeof(*inp));
199 inp->inp_af = AF_INET;
200 inp->inp_table = table;
201 inp->inp_socket = so;
202 inp->inp_errormtu = -1;
203 inp->inp_portalgo = PORTALGO_DEFAULT;
204 inp->inp_bindportonsend = false;
205 #if defined(IPSEC)
206 if (ipsec_enabled) {
207 int error = ipsec_init_pcbpolicy(so, &inp->inp_sp);
208 if (error != 0) {
209 pool_put(&inpcb_pool, inp);
210 return error;
211 }
212 }
213 #endif
214 so->so_pcb = inp;
215 s = splsoftnet();
216 TAILQ_INSERT_HEAD(&table->inpt_queue, &inp->inp_head, inph_queue);
217 LIST_INSERT_HEAD(INPCBHASH_PORT(table, inp->inp_lport), &inp->inp_head,
218 inph_lhash);
219 in_pcbstate(inp, INP_ATTACHED);
220 splx(s);
221 return (0);
222 }
223
224 static int
225 in_pcbsetport(struct sockaddr_in *sin, struct inpcb *inp, kauth_cred_t cred)
226 {
227 struct inpcbtable *table = inp->inp_table;
228 struct socket *so = inp->inp_socket;
229 u_int16_t *lastport;
230 u_int16_t lport = 0;
231 enum kauth_network_req req;
232 int error;
233
234 if (inp->inp_flags & INP_LOWPORT) {
235 #ifndef IPNOPRIVPORTS
236 req = KAUTH_REQ_NETWORK_BIND_PRIVPORT;
237 #else
238 req = KAUTH_REQ_NETWORK_BIND_PORT;
239 #endif
240
241 lastport = &table->inpt_lastlow;
242 } else {
243 req = KAUTH_REQ_NETWORK_BIND_PORT;
244
245 lastport = &table->inpt_lastport;
246 }
247
248 /* XXX-kauth: KAUTH_REQ_NETWORK_BIND_AUTOASSIGN_{,PRIV}PORT */
249 error = kauth_authorize_network(cred, KAUTH_NETWORK_BIND, req, so, sin,
250 NULL);
251 if (error)
252 return (EACCES);
253
254 /*
255 * Use RFC6056 randomized port selection
256 */
257 error = portalgo_randport(&lport, &inp->inp_head, cred);
258 if (error)
259 return error;
260
261 inp->inp_flags |= INP_ANONPORT;
262 *lastport = lport;
263 lport = htons(lport);
264 inp->inp_lport = lport;
265 in_pcbstate(inp, INP_BOUND);
266
267 return (0);
268 }
269
270 static int
271 in_pcbbind_addr(struct inpcb *inp, struct sockaddr_in *sin, kauth_cred_t cred)
272 {
273 int error = EADDRNOTAVAIL;
274 struct ifaddr *ifa = NULL;
275 int s;
276
277 if (sin->sin_family != AF_INET)
278 return (EAFNOSUPPORT);
279
280 s = pserialize_read_enter();
281 if (IN_MULTICAST(sin->sin_addr.s_addr)) {
282 /* Always succeed; port reuse handled in in_pcbbind_port(). */
283 } else if (!in_nullhost(sin->sin_addr)) {
284 struct in_ifaddr *ia;
285
286 ia = in_get_ia(sin->sin_addr);
287 /* check for broadcast addresses */
288 if (ia == NULL) {
289 ifa = ifa_ifwithaddr(sintosa(sin));
290 if (ifa != NULL)
291 ia = ifatoia(ifa);
292 }
293 if (ia == NULL)
294 goto error;
295 if (ia->ia4_flags & IN_IFF_DUPLICATED)
296 goto error;
297 }
298 pserialize_read_exit(s);
299
300 inp->inp_laddr = sin->sin_addr;
301
302 return (0);
303 error:
304 pserialize_read_exit(s);
305 return error;
306 }
307
308 static int
309 in_pcbbind_port(struct inpcb *inp, struct sockaddr_in *sin, kauth_cred_t cred)
310 {
311 struct inpcbtable *table = inp->inp_table;
312 struct socket *so = inp->inp_socket;
313 int reuseport = (so->so_options & SO_REUSEPORT);
314 int wild = 0, error;
315
316 if (IN_MULTICAST(sin->sin_addr.s_addr)) {
317 /*
318 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
319 * allow complete duplication of binding if
320 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
321 * and a multicast address is bound on both
322 * new and duplicated sockets.
323 */
324 if (so->so_options & (SO_REUSEADDR | SO_REUSEPORT))
325 reuseport = SO_REUSEADDR|SO_REUSEPORT;
326 }
327
328 if (sin->sin_port == 0) {
329 error = in_pcbsetport(sin, inp, cred);
330 if (error)
331 return (error);
332 } else {
333 struct inpcb *t;
334 vestigial_inpcb_t vestige;
335 #ifdef INET6
336 struct in6pcb *t6;
337 struct in6_addr mapped;
338 #endif
339 enum kauth_network_req req;
340
341 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
342 wild = 1;
343
344 #ifndef IPNOPRIVPORTS
345 if (ntohs(sin->sin_port) < IPPORT_RESERVED)
346 req = KAUTH_REQ_NETWORK_BIND_PRIVPORT;
347 else
348 #endif /* !IPNOPRIVPORTS */
349 req = KAUTH_REQ_NETWORK_BIND_PORT;
350
351 error = kauth_authorize_network(cred, KAUTH_NETWORK_BIND, req,
352 so, sin, NULL);
353 if (error)
354 return (EACCES);
355
356 #ifdef INET6
357 in6_in_2_v4mapin6(&sin->sin_addr, &mapped);
358 t6 = in6_pcblookup_port(table, &mapped, sin->sin_port, wild, &vestige);
359 if (t6 && (reuseport & t6->in6p_socket->so_options) == 0)
360 return (EADDRINUSE);
361 if (!t6 && vestige.valid) {
362 if (!!reuseport != !!vestige.reuse_port) {
363 return EADDRINUSE;
364 }
365 }
366 #endif
367
368 /* XXX-kauth */
369 if (so->so_uidinfo->ui_uid && !IN_MULTICAST(sin->sin_addr.s_addr)) {
370 t = in_pcblookup_port(table, sin->sin_addr, sin->sin_port, 1, &vestige);
371 /*
372 * XXX: investigate ramifications of loosening this
373 * restriction so that as long as both ports have
374 * SO_REUSEPORT allow the bind
375 */
376 if (t &&
377 (!in_nullhost(sin->sin_addr) ||
378 !in_nullhost(t->inp_laddr) ||
379 (t->inp_socket->so_options & SO_REUSEPORT) == 0)
380 && (so->so_uidinfo->ui_uid != t->inp_socket->so_uidinfo->ui_uid)) {
381 return (EADDRINUSE);
382 }
383 if (!t && vestige.valid) {
384 if ((!in_nullhost(sin->sin_addr)
385 || !in_nullhost(vestige.laddr.v4)
386 || !vestige.reuse_port)
387 && so->so_uidinfo->ui_uid != vestige.uid) {
388 return EADDRINUSE;
389 }
390 }
391 }
392 t = in_pcblookup_port(table, sin->sin_addr, sin->sin_port, wild, &vestige);
393 if (t && (reuseport & t->inp_socket->so_options) == 0)
394 return (EADDRINUSE);
395 if (!t
396 && vestige.valid
397 && !(reuseport && vestige.reuse_port))
398 return EADDRINUSE;
399
400 inp->inp_lport = sin->sin_port;
401 in_pcbstate(inp, INP_BOUND);
402 }
403
404 LIST_REMOVE(&inp->inp_head, inph_lhash);
405 LIST_INSERT_HEAD(INPCBHASH_PORT(table, inp->inp_lport), &inp->inp_head,
406 inph_lhash);
407
408 return (0);
409 }
410
411 int
412 in_pcbbind(void *v, struct sockaddr_in *sin, struct lwp *l)
413 {
414 struct inpcb *inp = v;
415 struct sockaddr_in lsin;
416 int error;
417
418 if (inp->inp_af != AF_INET)
419 return (EINVAL);
420
421 if (IN_ADDRLIST_READER_EMPTY())
422 return (EADDRNOTAVAIL);
423 if (inp->inp_lport || !in_nullhost(inp->inp_laddr))
424 return (EINVAL);
425
426 if (NULL != sin) {
427 if (sin->sin_len != sizeof(*sin))
428 return (EINVAL);
429 } else {
430 lsin = *((const struct sockaddr_in *)
431 inp->inp_socket->so_proto->pr_domain->dom_sa_any);
432 sin = &lsin;
433 }
434
435 /* Bind address. */
436 error = in_pcbbind_addr(inp, sin, l->l_cred);
437 if (error)
438 return (error);
439
440 /* Bind port. */
441 error = in_pcbbind_port(inp, sin, l->l_cred);
442 if (error) {
443 inp->inp_laddr.s_addr = INADDR_ANY;
444
445 return (error);
446 }
447
448 return (0);
449 }
450
451 /*
452 * Connect from a socket to a specified address.
453 * Both address and port must be specified in argument sin.
454 * If don't have a local address for this socket yet,
455 * then pick one.
456 */
457 int
458 in_pcbconnect(void *v, struct sockaddr_in *sin, struct lwp *l)
459 {
460 struct inpcb *inp = v;
461 vestigial_inpcb_t vestige;
462 int error;
463 struct in_addr laddr;
464
465 if (inp->inp_af != AF_INET)
466 return (EINVAL);
467
468 if (sin->sin_len != sizeof (*sin))
469 return (EINVAL);
470 if (sin->sin_family != AF_INET)
471 return (EAFNOSUPPORT);
472 if (sin->sin_port == 0)
473 return (EADDRNOTAVAIL);
474
475 if (IN_MULTICAST(sin->sin_addr.s_addr) &&
476 inp->inp_socket->so_type == SOCK_STREAM)
477 return EADDRNOTAVAIL;
478
479 if (!IN_ADDRLIST_READER_EMPTY()) {
480 /*
481 * If the destination address is INADDR_ANY,
482 * use any local address (likely loopback).
483 * If the supplied address is INADDR_BROADCAST,
484 * use the broadcast address of an interface
485 * which supports broadcast. (loopback does not)
486 */
487
488 if (in_nullhost(sin->sin_addr)) {
489 /* XXX racy */
490 sin->sin_addr =
491 IN_ADDRLIST_READER_FIRST()->ia_addr.sin_addr;
492 } else if (sin->sin_addr.s_addr == INADDR_BROADCAST) {
493 struct in_ifaddr *ia;
494 int s = pserialize_read_enter();
495 IN_ADDRLIST_READER_FOREACH(ia) {
496 if (ia->ia_ifp->if_flags & IFF_BROADCAST) {
497 sin->sin_addr =
498 ia->ia_broadaddr.sin_addr;
499 break;
500 }
501 }
502 pserialize_read_exit(s);
503 }
504 }
505 /*
506 * If we haven't bound which network number to use as ours,
507 * we will use the number of the outgoing interface.
508 * This depends on having done a routing lookup, which
509 * we will probably have to do anyway, so we might
510 * as well do it now. On the other hand if we are
511 * sending to multiple destinations we may have already
512 * done the lookup, so see if we can use the route
513 * from before. In any case, we only
514 * chose a port number once, even if sending to multiple
515 * destinations.
516 */
517 if (in_nullhost(inp->inp_laddr)) {
518 int xerror;
519 struct in_ifaddr *ia, *_ia;
520 int s;
521 struct psref psref;
522 int bound;
523
524 bound = curlwp_bind();
525 ia = in_selectsrc(sin, &inp->inp_route,
526 inp->inp_socket->so_options, inp->inp_moptions, &xerror,
527 &psref);
528 if (ia == NULL) {
529 curlwp_bindx(bound);
530 if (xerror == 0)
531 xerror = EADDRNOTAVAIL;
532 return xerror;
533 }
534 s = pserialize_read_enter();
535 _ia = in_get_ia(IA_SIN(ia)->sin_addr);
536 if (_ia == NULL) {
537 pserialize_read_exit(s);
538 ia4_release(ia, &psref);
539 curlwp_bindx(bound);
540 return (EADDRNOTAVAIL);
541 }
542 pserialize_read_exit(s);
543 laddr = IA_SIN(ia)->sin_addr;
544 ia4_release(ia, &psref);
545 curlwp_bindx(bound);
546 } else
547 laddr = inp->inp_laddr;
548 if (in_pcblookup_connect(inp->inp_table, sin->sin_addr, sin->sin_port,
549 laddr, inp->inp_lport, &vestige) != NULL ||
550 vestige.valid) {
551 return (EADDRINUSE);
552 }
553 if (in_nullhost(inp->inp_laddr)) {
554 if (inp->inp_lport == 0) {
555 error = in_pcbbind(inp, NULL, l);
556 /*
557 * This used to ignore the return value
558 * completely, but we need to check for
559 * ephemeral port shortage.
560 * And attempts to request low ports if not root.
561 */
562 if (error != 0)
563 return (error);
564 }
565 inp->inp_laddr = laddr;
566 }
567 inp->inp_faddr = sin->sin_addr;
568 inp->inp_fport = sin->sin_port;
569
570 /* Late bind, if needed */
571 if (inp->inp_bindportonsend) {
572 struct sockaddr_in lsin = *((const struct sockaddr_in *)
573 inp->inp_socket->so_proto->pr_domain->dom_sa_any);
574 lsin.sin_addr = inp->inp_laddr;
575 lsin.sin_port = 0;
576
577 if ((error = in_pcbbind_port(inp, &lsin, l->l_cred)) != 0)
578 return error;
579 }
580
581 in_pcbstate(inp, INP_CONNECTED);
582 #if defined(IPSEC)
583 if (ipsec_enabled && inp->inp_socket->so_type == SOCK_STREAM)
584 ipsec_pcbconn(inp->inp_sp);
585 #endif
586 return (0);
587 }
588
589 void
590 in_pcbdisconnect(void *v)
591 {
592 struct inpcb *inp = v;
593
594 if (inp->inp_af != AF_INET)
595 return;
596
597 inp->inp_faddr = zeroin_addr;
598 inp->inp_fport = 0;
599 in_pcbstate(inp, INP_BOUND);
600 #if defined(IPSEC)
601 if (ipsec_enabled)
602 ipsec_pcbdisconn(inp->inp_sp);
603 #endif
604 if (inp->inp_socket->so_state & SS_NOFDREF)
605 in_pcbdetach(inp);
606 }
607
608 void
609 in_pcbdetach(void *v)
610 {
611 struct inpcb *inp = v;
612 struct socket *so = inp->inp_socket;
613 int s;
614
615 if (inp->inp_af != AF_INET)
616 return;
617
618 #if defined(IPSEC)
619 if (ipsec_enabled)
620 ipsec4_delete_pcbpolicy(inp);
621 #endif
622 so->so_pcb = NULL;
623
624 s = splsoftnet();
625 in_pcbstate(inp, INP_ATTACHED);
626 LIST_REMOVE(&inp->inp_head, inph_lhash);
627 TAILQ_REMOVE(&inp->inp_table->inpt_queue, &inp->inp_head, inph_queue);
628 splx(s);
629
630 if (inp->inp_options) {
631 m_free(inp->inp_options);
632 }
633 rtcache_free(&inp->inp_route);
634 ip_freemoptions(inp->inp_moptions);
635 sofree(so); /* drops the socket's lock */
636
637 pool_put(&inpcb_pool, inp);
638 mutex_enter(softnet_lock); /* reacquire the softnet_lock */
639 }
640
641 void
642 in_setsockaddr(struct inpcb *inp, struct sockaddr_in *sin)
643 {
644
645 if (inp->inp_af != AF_INET)
646 return;
647
648 sockaddr_in_init(sin, &inp->inp_laddr, inp->inp_lport);
649 }
650
651 void
652 in_setpeeraddr(struct inpcb *inp, struct sockaddr_in *sin)
653 {
654
655 if (inp->inp_af != AF_INET)
656 return;
657
658 sockaddr_in_init(sin, &inp->inp_faddr, inp->inp_fport);
659 }
660
661 /*
662 * Pass some notification to all connections of a protocol
663 * associated with address dst. The local address and/or port numbers
664 * may be specified to limit the search. The "usual action" will be
665 * taken, depending on the ctlinput cmd. The caller must filter any
666 * cmds that are uninteresting (e.g., no error in the map).
667 * Call the protocol specific routine (if any) to report
668 * any errors for each matching socket.
669 *
670 * Must be called at splsoftnet.
671 */
672 int
673 in_pcbnotify(struct inpcbtable *table, struct in_addr faddr, u_int fport_arg,
674 struct in_addr laddr, u_int lport_arg, int errno,
675 void (*notify)(struct inpcb *, int))
676 {
677 struct inpcbhead *head;
678 struct inpcb *inp, *ninp;
679 u_int16_t fport = fport_arg, lport = lport_arg;
680 int nmatch;
681
682 if (in_nullhost(faddr) || notify == 0)
683 return (0);
684
685 nmatch = 0;
686 head = INPCBHASH_CONNECT(table, faddr, fport, laddr, lport);
687 for (inp = (struct inpcb *)LIST_FIRST(head); inp != NULL; inp = ninp) {
688 ninp = (struct inpcb *)LIST_NEXT(inp, inp_hash);
689 if (inp->inp_af != AF_INET)
690 continue;
691 if (in_hosteq(inp->inp_faddr, faddr) &&
692 inp->inp_fport == fport &&
693 inp->inp_lport == lport &&
694 in_hosteq(inp->inp_laddr, laddr)) {
695 (*notify)(inp, errno);
696 nmatch++;
697 }
698 }
699 return (nmatch);
700 }
701
702 void
703 in_pcbnotifyall(struct inpcbtable *table, struct in_addr faddr, int errno,
704 void (*notify)(struct inpcb *, int))
705 {
706 struct inpcb_hdr *inph, *ninph;
707
708 if (in_nullhost(faddr) || notify == 0)
709 return;
710
711 TAILQ_FOREACH_SAFE(inph, &table->inpt_queue, inph_queue, ninph) {
712 struct inpcb *inp = (struct inpcb *)inph;
713 if (inp->inp_af != AF_INET)
714 continue;
715 if (in_hosteq(inp->inp_faddr, faddr))
716 (*notify)(inp, errno);
717 }
718 }
719
720 void
721 in_purgeifmcast(struct ip_moptions *imo, struct ifnet *ifp)
722 {
723 int i, gap;
724
725 /* The owner of imo should be protected by solock */
726 KASSERT(ifp != NULL);
727
728 if (imo == NULL)
729 return;
730
731 /*
732 * Unselect the outgoing interface if it is being
733 * detached.
734 */
735 if (imo->imo_multicast_if_index == ifp->if_index)
736 imo->imo_multicast_if_index = 0;
737
738 /*
739 * Drop multicast group membership if we joined
740 * through the interface being detached.
741 */
742 for (i = 0, gap = 0; i < imo->imo_num_memberships; i++) {
743 if (imo->imo_membership[i]->inm_ifp == ifp) {
744 in_delmulti(imo->imo_membership[i]);
745 gap++;
746 } else if (gap != 0)
747 imo->imo_membership[i - gap] = imo->imo_membership[i];
748 }
749 imo->imo_num_memberships -= gap;
750 }
751
752 void
753 in_pcbpurgeif0(struct inpcbtable *table, struct ifnet *ifp)
754 {
755 struct inpcb_hdr *inph, *ninph;
756
757 TAILQ_FOREACH_SAFE(inph, &table->inpt_queue, inph_queue, ninph) {
758 struct inpcb *inp = (struct inpcb *)inph;
759 bool need_unlock = false;
760
761 if (inp->inp_af != AF_INET)
762 continue;
763
764 /* The caller holds either one of inps' lock */
765 if (!inp_locked(inp)) {
766 inp_lock(inp);
767 need_unlock = true;
768 }
769
770 in_purgeifmcast(inp->inp_moptions, ifp);
771
772 if (need_unlock)
773 inp_unlock(inp);
774 }
775 }
776
777 void
778 in_pcbpurgeif(struct inpcbtable *table, struct ifnet *ifp)
779 {
780 struct rtentry *rt;
781 struct inpcb_hdr *inph, *ninph;
782
783 TAILQ_FOREACH_SAFE(inph, &table->inpt_queue, inph_queue, ninph) {
784 struct inpcb *inp = (struct inpcb *)inph;
785 if (inp->inp_af != AF_INET)
786 continue;
787 if ((rt = rtcache_validate(&inp->inp_route)) != NULL &&
788 rt->rt_ifp == ifp) {
789 rtcache_unref(rt, &inp->inp_route);
790 in_rtchange(inp, 0);
791 } else
792 rtcache_unref(rt, &inp->inp_route);
793 }
794 }
795
796 /*
797 * Check for alternatives when higher level complains
798 * about service problems. For now, invalidate cached
799 * routing information. If the route was created dynamically
800 * (by a redirect), time to try a default gateway again.
801 */
802 void
803 in_losing(struct inpcb *inp)
804 {
805 struct rtentry *rt;
806 struct rt_addrinfo info;
807
808 if (inp->inp_af != AF_INET)
809 return;
810
811 if ((rt = rtcache_validate(&inp->inp_route)) == NULL)
812 return;
813
814 memset(&info, 0, sizeof(info));
815 info.rti_info[RTAX_DST] = rtcache_getdst(&inp->inp_route);
816 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
817 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
818 rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
819 if (rt->rt_flags & RTF_DYNAMIC) {
820 int error;
821 struct rtentry *nrt;
822
823 error = rtrequest(RTM_DELETE, rt_getkey(rt),
824 rt->rt_gateway, rt_mask(rt), rt->rt_flags, &nrt);
825 rtcache_unref(rt, &inp->inp_route);
826 if (error == 0)
827 rt_free(nrt);
828 } else
829 rtcache_unref(rt, &inp->inp_route);
830 /*
831 * A new route can be allocated
832 * the next time output is attempted.
833 */
834 rtcache_free(&inp->inp_route);
835 }
836
837 /*
838 * After a routing change, flush old routing. A new route can be
839 * allocated the next time output is attempted.
840 */
841 void
842 in_rtchange(struct inpcb *inp, int errno)
843 {
844
845 if (inp->inp_af != AF_INET)
846 return;
847
848 rtcache_free(&inp->inp_route);
849
850 /* XXX SHOULD NOTIFY HIGHER-LEVEL PROTOCOLS */
851 }
852
853 struct inpcb *
854 in_pcblookup_port(struct inpcbtable *table, struct in_addr laddr,
855 u_int lport_arg, int lookup_wildcard, vestigial_inpcb_t *vp)
856 {
857 struct inpcbhead *head;
858 struct inpcb_hdr *inph;
859 struct inpcb *match = NULL;
860 int matchwild = 3;
861 int wildcard;
862 u_int16_t lport = lport_arg;
863
864 if (vp)
865 vp->valid = 0;
866
867 head = INPCBHASH_PORT(table, lport);
868 LIST_FOREACH(inph, head, inph_lhash) {
869 struct inpcb * const inp = (struct inpcb *)inph;
870
871 if (inp->inp_af != AF_INET)
872 continue;
873 if (inp->inp_lport != lport)
874 continue;
875 /*
876 * check if inp's faddr and laddr match with ours.
877 * our faddr is considered null.
878 * count the number of wildcard matches. (0 - 2)
879 *
880 * null null match
881 * A null wildcard match
882 * null B wildcard match
883 * A B non match
884 * A A match
885 */
886 wildcard = 0;
887 if (!in_nullhost(inp->inp_faddr))
888 wildcard++;
889 if (in_nullhost(inp->inp_laddr)) {
890 if (!in_nullhost(laddr))
891 wildcard++;
892 } else {
893 if (in_nullhost(laddr))
894 wildcard++;
895 else {
896 if (!in_hosteq(inp->inp_laddr, laddr))
897 continue;
898 }
899 }
900 if (wildcard && !lookup_wildcard)
901 continue;
902 /*
903 * prefer an address with less wildcards.
904 */
905 if (wildcard < matchwild) {
906 match = inp;
907 matchwild = wildcard;
908 if (matchwild == 0)
909 break;
910 }
911 }
912 if (match && matchwild == 0)
913 return match;
914
915 if (vp && table->vestige) {
916 void *state = (*table->vestige->init_ports4)(laddr, lport_arg, lookup_wildcard);
917 vestigial_inpcb_t better;
918
919 while (table->vestige
920 && (*table->vestige->next_port4)(state, vp)) {
921
922 if (vp->lport != lport)
923 continue;
924 wildcard = 0;
925 if (!in_nullhost(vp->faddr.v4))
926 wildcard++;
927 if (in_nullhost(vp->laddr.v4)) {
928 if (!in_nullhost(laddr))
929 wildcard++;
930 } else {
931 if (in_nullhost(laddr))
932 wildcard++;
933 else {
934 if (!in_hosteq(vp->laddr.v4, laddr))
935 continue;
936 }
937 }
938 if (wildcard && !lookup_wildcard)
939 continue;
940 if (wildcard < matchwild) {
941 better = *vp;
942 match = (void*)&better;
943
944 matchwild = wildcard;
945 if (matchwild == 0)
946 break;
947 }
948 }
949
950 if (match) {
951 if (match != (void*)&better)
952 return match;
953 else {
954 *vp = better;
955 return 0;
956 }
957 }
958 }
959
960 return (match);
961 }
962
963 #ifdef DIAGNOSTIC
964 int in_pcbnotifymiss = 0;
965 #endif
966
967 struct inpcb *
968 in_pcblookup_connect(struct inpcbtable *table,
969 struct in_addr faddr, u_int fport_arg,
970 struct in_addr laddr, u_int lport_arg,
971 vestigial_inpcb_t *vp)
972 {
973 struct inpcbhead *head;
974 struct inpcb_hdr *inph;
975 struct inpcb *inp;
976 u_int16_t fport = fport_arg, lport = lport_arg;
977
978 if (vp)
979 vp->valid = 0;
980
981 head = INPCBHASH_CONNECT(table, faddr, fport, laddr, lport);
982 LIST_FOREACH(inph, head, inph_hash) {
983 inp = (struct inpcb *)inph;
984 if (inp->inp_af != AF_INET)
985 continue;
986
987 if (in_hosteq(inp->inp_faddr, faddr) &&
988 inp->inp_fport == fport &&
989 inp->inp_lport == lport &&
990 in_hosteq(inp->inp_laddr, laddr))
991 goto out;
992 }
993 if (vp && table->vestige) {
994 if ((*table->vestige->lookup4)(faddr, fport_arg,
995 laddr, lport_arg, vp))
996 return 0;
997 }
998
999 #ifdef DIAGNOSTIC
1000 if (in_pcbnotifymiss) {
1001 printf("in_pcblookup_connect: faddr=%08x fport=%d laddr=%08x lport=%d\n",
1002 ntohl(faddr.s_addr), ntohs(fport),
1003 ntohl(laddr.s_addr), ntohs(lport));
1004 }
1005 #endif
1006 return (0);
1007
1008 out:
1009 /* Move this PCB to the head of hash chain. */
1010 inph = &inp->inp_head;
1011 if (inph != LIST_FIRST(head)) {
1012 LIST_REMOVE(inph, inph_hash);
1013 LIST_INSERT_HEAD(head, inph, inph_hash);
1014 }
1015 return (inp);
1016 }
1017
1018 struct inpcb *
1019 in_pcblookup_bind(struct inpcbtable *table,
1020 struct in_addr laddr, u_int lport_arg)
1021 {
1022 struct inpcbhead *head;
1023 struct inpcb_hdr *inph;
1024 struct inpcb *inp;
1025 u_int16_t lport = lport_arg;
1026
1027 head = INPCBHASH_BIND(table, laddr, lport);
1028 LIST_FOREACH(inph, head, inph_hash) {
1029 inp = (struct inpcb *)inph;
1030 if (inp->inp_af != AF_INET)
1031 continue;
1032
1033 if (inp->inp_lport == lport &&
1034 in_hosteq(inp->inp_laddr, laddr))
1035 goto out;
1036 }
1037 head = INPCBHASH_BIND(table, zeroin_addr, lport);
1038 LIST_FOREACH(inph, head, inph_hash) {
1039 inp = (struct inpcb *)inph;
1040 if (inp->inp_af != AF_INET)
1041 continue;
1042
1043 if (inp->inp_lport == lport &&
1044 in_hosteq(inp->inp_laddr, zeroin_addr))
1045 goto out;
1046 }
1047 #ifdef DIAGNOSTIC
1048 if (in_pcbnotifymiss) {
1049 printf("in_pcblookup_bind: laddr=%08x lport=%d\n",
1050 ntohl(laddr.s_addr), ntohs(lport));
1051 }
1052 #endif
1053 return (0);
1054
1055 out:
1056 /* Move this PCB to the head of hash chain. */
1057 inph = &inp->inp_head;
1058 if (inph != LIST_FIRST(head)) {
1059 LIST_REMOVE(inph, inph_hash);
1060 LIST_INSERT_HEAD(head, inph, inph_hash);
1061 }
1062 return (inp);
1063 }
1064
1065 void
1066 in_pcbstate(struct inpcb *inp, int state)
1067 {
1068
1069 if (inp->inp_af != AF_INET)
1070 return;
1071
1072 if (inp->inp_state > INP_ATTACHED)
1073 LIST_REMOVE(&inp->inp_head, inph_hash);
1074
1075 switch (state) {
1076 case INP_BOUND:
1077 LIST_INSERT_HEAD(INPCBHASH_BIND(inp->inp_table,
1078 inp->inp_laddr, inp->inp_lport), &inp->inp_head,
1079 inph_hash);
1080 break;
1081 case INP_CONNECTED:
1082 LIST_INSERT_HEAD(INPCBHASH_CONNECT(inp->inp_table,
1083 inp->inp_faddr, inp->inp_fport,
1084 inp->inp_laddr, inp->inp_lport), &inp->inp_head,
1085 inph_hash);
1086 break;
1087 }
1088
1089 inp->inp_state = state;
1090 }
1091
1092 struct rtentry *
1093 in_pcbrtentry(struct inpcb *inp)
1094 {
1095 struct route *ro;
1096 union {
1097 struct sockaddr dst;
1098 struct sockaddr_in dst4;
1099 } u;
1100
1101 if (inp->inp_af != AF_INET)
1102 return (NULL);
1103
1104 ro = &inp->inp_route;
1105
1106 sockaddr_in_init(&u.dst4, &inp->inp_faddr, 0);
1107 return rtcache_lookup(ro, &u.dst);
1108 }
1109
1110 void
1111 in_pcbrtentry_unref(struct rtentry *rt, struct inpcb *inp)
1112 {
1113
1114 rtcache_unref(rt, &inp->inp_route);
1115 }
1116