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