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