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