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