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