2 * Copyright (c) 2004 Jeffrey M. Hsu. All rights reserved.
3 * Copyright (c) 2004 The DragonFly Project. All rights reserved.
5 * This code is derived from software contributed to The DragonFly Project
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of The DragonFly Project nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific, prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * Copyright (c) 1982, 1986, 1991, 1993, 1995
36 * The Regents of the University of California. All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * @(#)in_pcb.c 8.4 (Berkeley) 5/24/95
67 * $FreeBSD: src/sys/netinet/in_pcb.c,v 1.59.2.27 2004/01/02 04:06:42 ambrisko Exp $
68 * $DragonFly: src/sys/netinet/in_pcb.c,v 1.45 2008/03/26 14:44:59 sephe Exp $
71 #include "opt_ipsec.h"
72 #include "opt_inet6.h"
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/malloc.h>
78 #include <sys/domain.h>
79 #include <sys/protosw.h>
80 #include <sys/socket.h>
81 #include <sys/socketvar.h>
84 #include <sys/kernel.h>
85 #include <sys/sysctl.h>
86 #include <sys/thread2.h>
88 #include <machine/limits.h>
90 #include <vm/vm_zone.h>
93 #include <net/if_types.h>
94 #include <net/route.h>
96 #include <netinet/in.h>
97 #include <netinet/in_pcb.h>
98 #include <netinet/in_var.h>
99 #include <netinet/ip_var.h>
101 #include <netinet/ip6.h>
102 #include <netinet6/ip6_var.h>
106 #include <netinet6/ipsec.h>
107 #include <netproto/key/key.h>
111 #if defined(IPSEC) || defined(IPSEC_ESP)
112 #error "Bad idea: don't compile with both IPSEC and FAST_IPSEC!"
115 #include <netproto/ipsec/ipsec.h>
116 #include <netproto/ipsec/key.h>
118 #endif /* FAST_IPSEC */
120 struct in_addr zeroin_addr
;
123 * These configure the range of local port addresses assigned to
124 * "unspecified" outgoing connections/packets/whatever.
126 int ipport_lowfirstauto
= IPPORT_RESERVED
- 1; /* 1023 */
127 int ipport_lowlastauto
= IPPORT_RESERVEDSTART
; /* 600 */
129 int ipport_firstauto
= IPPORT_RESERVED
; /* 1024 */
130 int ipport_lastauto
= IPPORT_USERRESERVED
; /* 5000 */
132 int ipport_hifirstauto
= IPPORT_HIFIRSTAUTO
; /* 49152 */
133 int ipport_hilastauto
= IPPORT_HILASTAUTO
; /* 65535 */
136 RANGECHK(int var
, int min
, int max
)
145 sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS
)
149 error
= sysctl_handle_int(oidp
, oidp
->oid_arg1
, oidp
->oid_arg2
, req
);
151 RANGECHK(ipport_lowfirstauto
, 1, IPPORT_RESERVED
- 1);
152 RANGECHK(ipport_lowlastauto
, 1, IPPORT_RESERVED
- 1);
154 RANGECHK(ipport_firstauto
, IPPORT_RESERVED
, USHRT_MAX
);
155 RANGECHK(ipport_lastauto
, IPPORT_RESERVED
, USHRT_MAX
);
157 RANGECHK(ipport_hifirstauto
, IPPORT_RESERVED
, USHRT_MAX
);
158 RANGECHK(ipport_hilastauto
, IPPORT_RESERVED
, USHRT_MAX
);
163 SYSCTL_NODE(_net_inet_ip
, IPPROTO_IP
, portrange
, CTLFLAG_RW
, 0, "IP Ports");
165 SYSCTL_PROC(_net_inet_ip_portrange
, OID_AUTO
, lowfirst
, CTLTYPE_INT
|CTLFLAG_RW
,
166 &ipport_lowfirstauto
, 0, &sysctl_net_ipport_check
, "I", "");
167 SYSCTL_PROC(_net_inet_ip_portrange
, OID_AUTO
, lowlast
, CTLTYPE_INT
|CTLFLAG_RW
,
168 &ipport_lowlastauto
, 0, &sysctl_net_ipport_check
, "I", "");
169 SYSCTL_PROC(_net_inet_ip_portrange
, OID_AUTO
, first
, CTLTYPE_INT
|CTLFLAG_RW
,
170 &ipport_firstauto
, 0, &sysctl_net_ipport_check
, "I", "");
171 SYSCTL_PROC(_net_inet_ip_portrange
, OID_AUTO
, last
, CTLTYPE_INT
|CTLFLAG_RW
,
172 &ipport_lastauto
, 0, &sysctl_net_ipport_check
, "I", "");
173 SYSCTL_PROC(_net_inet_ip_portrange
, OID_AUTO
, hifirst
, CTLTYPE_INT
|CTLFLAG_RW
,
174 &ipport_hifirstauto
, 0, &sysctl_net_ipport_check
, "I", "");
175 SYSCTL_PROC(_net_inet_ip_portrange
, OID_AUTO
, hilast
, CTLTYPE_INT
|CTLFLAG_RW
,
176 &ipport_hilastauto
, 0, &sysctl_net_ipport_check
, "I", "");
179 * in_pcb.c: manage the Protocol Control Blocks.
181 * NOTE: It is assumed that most of these functions will be called from
182 * a critical section. XXX - There are, unfortunately, a few exceptions
183 * to this rule that should be fixed.
185 * NOTE: The caller should initialize the cpu field to the cpu running the
186 * protocol stack associated with this inpcbinfo.
190 in_pcbinfo_init(struct inpcbinfo
*pcbinfo
)
192 LIST_INIT(&pcbinfo
->pcblisthead
);
197 * Allocate a PCB and associate it with the socket.
200 in_pcballoc(struct socket
*so
, struct inpcbinfo
*pcbinfo
)
207 inp
= zalloc(pcbinfo
->ipi_zone
);
210 bzero(inp
, sizeof *inp
);
211 inp
->inp_gencnt
= ++pcbinfo
->ipi_gencnt
;
212 inp
->inp_pcbinfo
= inp
->inp_cpcbinfo
= pcbinfo
;
213 inp
->inp_socket
= so
;
215 error
= ipsec_init_policy(so
, &inp
->inp_sp
);
217 zfree(pcbinfo
->ipi_zone
, inp
);
222 if (INP_SOCKAF(so
) == AF_INET6
&& ip6_v6only
)
223 inp
->inp_flags
|= IN6P_IPV6_V6ONLY
;
224 if (ip6_auto_flowlabel
)
225 inp
->inp_flags
|= IN6P_AUTOFLOWLABEL
;
228 LIST_INSERT_HEAD(&pcbinfo
->pcblisthead
, inp
, inp_list
);
229 pcbinfo
->ipi_count
++;
234 in_pcbbind(struct inpcb
*inp
, struct sockaddr
*nam
, struct thread
*td
)
236 struct socket
*so
= inp
->inp_socket
;
237 struct proc
*p
= td
->td_proc
;
238 unsigned short *lastport
;
239 struct sockaddr_in
*sin
;
240 struct sockaddr_in jsin
;
241 struct inpcbinfo
*pcbinfo
= inp
->inp_pcbinfo
;
242 struct ucred
*cred
= NULL
;
244 int wild
= 0, reuseport
= (so
->so_options
& SO_REUSEPORT
);
249 if (TAILQ_EMPTY(&in_ifaddrhead
)) /* XXX broken! */
250 return (EADDRNOTAVAIL
);
251 if (inp
->inp_lport
!= 0 || inp
->inp_laddr
.s_addr
!= INADDR_ANY
)
252 return (EINVAL
); /* already bound */
253 if (!(so
->so_options
& (SO_REUSEADDR
|SO_REUSEPORT
)))
254 wild
= 1; /* neither SO_REUSEADDR nor SO_REUSEPORT is set */
258 sin
= (struct sockaddr_in
*)nam
;
259 if (nam
->sa_len
!= sizeof *sin
)
263 * We should check the family, but old programs
264 * incorrectly fail to initialize it.
266 if (sin
->sin_family
!= AF_INET
)
267 return (EAFNOSUPPORT
);
269 if (!prison_replace_wildcards(td
, nam
))
271 lport
= sin
->sin_port
;
272 if (IN_MULTICAST(ntohl(sin
->sin_addr
.s_addr
))) {
274 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
275 * allow complete duplication of binding if
276 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
277 * and a multicast address is bound on both
278 * new and duplicated sockets.
280 if (so
->so_options
& SO_REUSEADDR
)
281 reuseport
= SO_REUSEADDR
| SO_REUSEPORT
;
282 } else if (sin
->sin_addr
.s_addr
!= INADDR_ANY
) {
283 sin
->sin_port
= 0; /* yech... */
284 bzero(&sin
->sin_zero
, sizeof sin
->sin_zero
);
285 if (ifa_ifwithaddr((struct sockaddr
*)sin
) == NULL
)
286 return (EADDRNOTAVAIL
);
292 if (ntohs(lport
) < IPPORT_RESERVED
&&
293 cred
&& suser_cred(cred
, PRISON_ROOT
))
295 if (so
->so_cred
->cr_uid
!= 0 &&
296 !IN_MULTICAST(ntohl(sin
->sin_addr
.s_addr
))) {
297 t
= in_pcblookup_local(inp
->inp_pcbinfo
,
298 sin
->sin_addr
, lport
,
299 INPLOOKUP_WILDCARD
, cred
);
301 (!in_nullhost(sin
->sin_addr
) ||
302 !in_nullhost(t
->inp_laddr
) ||
303 (t
->inp_socket
->so_options
&
304 SO_REUSEPORT
) == 0) &&
305 (so
->so_cred
->cr_uid
!=
306 t
->inp_socket
->so_cred
->cr_uid
)) {
308 if (!in_nullhost(sin
->sin_addr
) ||
309 !in_nullhost(t
->inp_laddr
) ||
311 INP_SOCKAF(t
->inp_socket
))
316 if (cred
&& !prison_replace_wildcards(td
, nam
))
317 return (EADDRNOTAVAIL
);
318 t
= in_pcblookup_local(pcbinfo
, sin
->sin_addr
, lport
,
320 if (t
&& !(reuseport
& t
->inp_socket
->so_options
)) {
322 if (!in_nullhost(sin
->sin_addr
) ||
323 !in_nullhost(t
->inp_laddr
) ||
324 INP_SOCKAF(so
) == INP_SOCKAF(t
->inp_socket
))
329 inp
->inp_laddr
= sin
->sin_addr
;
335 jsin
.sin_family
= AF_INET
;
336 jsin
.sin_addr
.s_addr
= inp
->inp_laddr
.s_addr
;
337 if (!prison_replace_wildcards(td
, (struct sockaddr
*)&jsin
)) {
338 inp
->inp_laddr
.s_addr
= INADDR_ANY
;
341 inp
->inp_laddr
.s_addr
= jsin
.sin_addr
.s_addr
;
343 inp
->inp_flags
|= INP_ANONPORT
;
345 if (inp
->inp_flags
& INP_HIGHPORT
) {
346 first
= ipport_hifirstauto
; /* sysctl */
347 last
= ipport_hilastauto
;
348 lastport
= &pcbinfo
->lasthi
;
349 } else if (inp
->inp_flags
& INP_LOWPORT
) {
351 (error
= suser_cred(cred
, PRISON_ROOT
))) {
352 inp
->inp_laddr
.s_addr
= INADDR_ANY
;
355 first
= ipport_lowfirstauto
; /* 1023 */
356 last
= ipport_lowlastauto
; /* 600 */
357 lastport
= &pcbinfo
->lastlow
;
359 first
= ipport_firstauto
; /* sysctl */
360 last
= ipport_lastauto
;
361 lastport
= &pcbinfo
->lastport
;
364 * Simple check to ensure all ports are not used up causing
367 * We split the two cases (up and down) so that the direction
368 * is not being tested on each round of the loop.
374 count
= first
- last
;
377 if (count
-- < 0) { /* completely used? */
378 inp
->inp_laddr
.s_addr
= INADDR_ANY
;
379 return (EADDRNOTAVAIL
);
382 if (*lastport
> first
|| *lastport
< last
)
384 lport
= htons(*lastport
);
385 } while (in_pcblookup_local(pcbinfo
, inp
->inp_laddr
,
391 count
= last
- first
;
394 if (count
-- < 0) { /* completely used? */
395 inp
->inp_laddr
.s_addr
= INADDR_ANY
;
396 return (EADDRNOTAVAIL
);
399 if (*lastport
< first
|| *lastport
> last
)
401 lport
= htons(*lastport
);
402 } while (in_pcblookup_local(pcbinfo
, inp
->inp_laddr
,
406 inp
->inp_lport
= lport
;
408 jsin
.sin_family
= AF_INET
;
409 jsin
.sin_addr
.s_addr
= inp
->inp_laddr
.s_addr
;
410 if (!prison_replace_wildcards(td
, (struct sockaddr
*)&jsin
)) {
411 inp
->inp_laddr
.s_addr
= INADDR_ANY
;
415 inp
->inp_laddr
.s_addr
= jsin
.sin_addr
.s_addr
;
417 if (in_pcbinsporthash(inp
) != 0) {
418 inp
->inp_laddr
.s_addr
= INADDR_ANY
;
426 * Transform old in_pcbconnect() into an inner subroutine for new
427 * in_pcbconnect(): Do some validity-checking on the remote
428 * address (in mbuf 'nam') and then determine local host address
429 * (i.e., which interface) to use to access that remote host.
431 * This preserves definition of in_pcbconnect(), while supporting a
432 * slightly different version for T/TCP. (This is more than
433 * a bit of a kludge, but cleaning up the internal interfaces would
434 * have forced minor changes in every protocol).
437 in_pcbladdr(struct inpcb
*inp
, struct sockaddr
*nam
,
438 struct sockaddr_in
**plocal_sin
, struct thread
*td
)
440 struct in_ifaddr
*ia
;
441 struct ucred
*cred
= NULL
;
442 struct sockaddr_in
*sin
= (struct sockaddr_in
*)nam
;
443 struct sockaddr
*jsin
;
444 int jailed
= 0, alloc_route
= 0;
446 if (nam
->sa_len
!= sizeof *sin
)
448 if (sin
->sin_family
!= AF_INET
)
449 return (EAFNOSUPPORT
);
450 if (sin
->sin_port
== 0)
451 return (EADDRNOTAVAIL
);
452 if (td
&& td
->td_proc
&& td
->td_proc
->p_ucred
)
453 cred
= td
->td_proc
->p_ucred
;
454 if (cred
&& cred
->cr_prison
)
456 if (!TAILQ_EMPTY(&in_ifaddrhead
)) {
457 ia
= TAILQ_FIRST(&in_ifaddrhead
);
459 * If the destination address is INADDR_ANY,
460 * use the primary local address.
461 * If the supplied address is INADDR_BROADCAST,
462 * and the primary interface supports broadcast,
463 * choose the broadcast address for that interface.
465 if (sin
->sin_addr
.s_addr
== INADDR_ANY
)
466 sin
->sin_addr
= IA_SIN(ia
)->sin_addr
;
467 else if (sin
->sin_addr
.s_addr
== (u_long
)INADDR_BROADCAST
&&
468 (ia
->ia_ifp
->if_flags
& IFF_BROADCAST
))
469 sin
->sin_addr
= satosin(&ia
->ia_broadaddr
)->sin_addr
;
471 if (inp
->inp_laddr
.s_addr
== INADDR_ANY
) {
474 ia
= (struct in_ifaddr
*)NULL
;
476 * If route is known or can be allocated now,
477 * our src addr is taken from the i/f, else punt.
478 * Note that we should check the address family of the cached
479 * destination, in case of sharing the cache with IPv6.
481 ro
= &inp
->inp_route
;
483 (!(ro
->ro_rt
->rt_flags
& RTF_UP
) ||
484 ro
->ro_dst
.sa_family
!= AF_INET
||
485 satosin(&ro
->ro_dst
)->sin_addr
.s_addr
!=
486 sin
->sin_addr
.s_addr
||
487 inp
->inp_socket
->so_options
& SO_DONTROUTE
)) {
489 ro
->ro_rt
= (struct rtentry
*)NULL
;
491 if (!(inp
->inp_socket
->so_options
& SO_DONTROUTE
) && /*XXX*/
492 (ro
->ro_rt
== (struct rtentry
*)NULL
||
493 ro
->ro_rt
->rt_ifp
== (struct ifnet
*)NULL
)) {
494 /* No route yet, so try to acquire one */
495 bzero(&ro
->ro_dst
, sizeof(struct sockaddr_in
));
496 ro
->ro_dst
.sa_family
= AF_INET
;
497 ro
->ro_dst
.sa_len
= sizeof(struct sockaddr_in
);
498 ((struct sockaddr_in
*) &ro
->ro_dst
)->sin_addr
=
504 * If we found a route, use the address
505 * corresponding to the outgoing interface
506 * unless it is the loopback (in case a route
507 * to our address on another net goes to loopback).
509 if (ro
->ro_rt
&& !(ro
->ro_rt
->rt_ifp
->if_flags
& IFF_LOOPBACK
)) {
511 if (jailed_ip(cred
->cr_prison
,
512 ro
->ro_rt
->rt_ifa
->ifa_addr
)) {
513 ia
= ifatoia(ro
->ro_rt
->rt_ifa
);
516 ia
= ifatoia(ro
->ro_rt
->rt_ifa
);
520 u_short fport
= sin
->sin_port
;
523 ia
= ifatoia(ifa_ifwithdstaddr(sintosa(sin
)));
524 if (ia
&& jailed
&& !jailed_ip(cred
->cr_prison
,
525 sintosa(&ia
->ia_addr
)))
528 ia
= ifatoia(ifa_ifwithnet(sintosa(sin
)));
529 if (ia
&& jailed
&& !jailed_ip(cred
->cr_prison
,
530 sintosa(&ia
->ia_addr
)))
532 sin
->sin_port
= fport
;
534 ia
= TAILQ_FIRST(&in_ifaddrhead
);
535 if (ia
&& jailed
&& !jailed_ip(cred
->cr_prison
,
536 sintosa(&ia
->ia_addr
)))
539 if (!jailed
&& ia
== NULL
)
543 * If the destination address is multicast and an outgoing
544 * interface has been set as a multicast option, use the
545 * address of that interface as our source address.
547 if (!jailed
&& IN_MULTICAST(ntohl(sin
->sin_addr
.s_addr
)) &&
548 inp
->inp_moptions
!= NULL
) {
549 struct ip_moptions
*imo
;
552 imo
= inp
->inp_moptions
;
553 if (imo
->imo_multicast_ifp
!= NULL
) {
554 ifp
= imo
->imo_multicast_ifp
;
555 TAILQ_FOREACH(ia
, &in_ifaddrhead
, ia_link
)
556 if (ia
->ia_ifp
== ifp
)
563 * Don't do pcblookup call here; return interface in plocal_sin
564 * and exit to caller, that will do the lookup.
566 if (ia
== NULL
&& jailed
) {
567 if ((jsin
= prison_get_nonlocal(cred
->cr_prison
, AF_INET
, NULL
)) != NULL
||
568 (jsin
= prison_get_local(cred
->cr_prison
, AF_INET
, NULL
)) != NULL
) {
569 *plocal_sin
= satosin(jsin
);
575 *plocal_sin
= &ia
->ia_addr
;
581 struct route
*ro
= &inp
->inp_route
;
583 if (ro
->ro_rt
!= NULL
)
585 bzero(ro
, sizeof(*ro
));
587 return (EADDRNOTAVAIL
);
592 * Connect from a socket to a specified address.
593 * Both address and port must be specified in argument sin.
594 * If don't have a local address for this socket yet,
598 in_pcbconnect(struct inpcb
*inp
, struct sockaddr
*nam
, struct thread
*td
)
600 struct sockaddr_in
*if_sin
;
601 struct sockaddr_in
*sin
= (struct sockaddr_in
*)nam
;
604 /* Call inner routine to assign local interface address. */
605 if ((error
= in_pcbladdr(inp
, nam
, &if_sin
, td
)) != 0)
608 if (in_pcblookup_hash(inp
->inp_cpcbinfo
, sin
->sin_addr
, sin
->sin_port
,
609 inp
->inp_laddr
.s_addr
? inp
->inp_laddr
: if_sin
->sin_addr
,
610 inp
->inp_lport
, FALSE
, NULL
) != NULL
) {
613 if (inp
->inp_laddr
.s_addr
== INADDR_ANY
) {
614 if (inp
->inp_lport
== 0) {
615 error
= in_pcbbind(inp
, (struct sockaddr
*)NULL
, td
);
619 inp
->inp_laddr
= if_sin
->sin_addr
;
621 inp
->inp_faddr
= sin
->sin_addr
;
622 inp
->inp_fport
= sin
->sin_port
;
623 in_pcbinsconnhash(inp
);
628 in_pcbdisconnect(struct inpcb
*inp
)
631 inp
->inp_faddr
.s_addr
= INADDR_ANY
;
633 in_pcbremconnhash(inp
);
634 if (inp
->inp_socket
->so_state
& SS_NOFDREF
)
639 in_pcbdetach(struct inpcb
*inp
)
641 struct socket
*so
= inp
->inp_socket
;
642 struct inpcbinfo
*ipi
= inp
->inp_pcbinfo
;
645 ipsec4_delete_pcbpolicy(inp
);
647 inp
->inp_gencnt
= ++ipi
->ipi_gencnt
;
651 if (inp
->inp_options
)
652 m_free(inp
->inp_options
);
653 if (inp
->inp_route
.ro_rt
)
654 rtfree(inp
->inp_route
.ro_rt
);
655 ip_freemoptions(inp
->inp_moptions
);
657 zfree(ipi
->ipi_zone
, inp
);
661 * The calling convention of in_setsockaddr() and in_setpeeraddr() was
662 * modified to match the pru_sockaddr() and pru_peeraddr() entry points
663 * in struct pr_usrreqs, so that protocols can just reference then directly
664 * without the need for a wrapper function. The socket must have a valid
665 * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
666 * except through a kernel programming error, so it is acceptable to panic
667 * (or in this case trap) if the PCB is invalid. (Actually, we don't trap
668 * because there actually /is/ a programming error somewhere... XXX)
671 in_setsockaddr(struct socket
*so
, struct sockaddr
**nam
)
674 struct sockaddr_in
*sin
;
677 * Do the malloc first in case it blocks.
679 MALLOC(sin
, struct sockaddr_in
*, sizeof *sin
, M_SONAME
,
681 sin
->sin_family
= AF_INET
;
682 sin
->sin_len
= sizeof *sin
;
688 kfree(sin
, M_SONAME
);
691 sin
->sin_port
= inp
->inp_lport
;
692 sin
->sin_addr
= inp
->inp_laddr
;
695 *nam
= (struct sockaddr
*)sin
;
700 in_setpeeraddr(struct socket
*so
, struct sockaddr
**nam
)
703 struct sockaddr_in
*sin
;
706 * Do the malloc first in case it blocks.
708 MALLOC(sin
, struct sockaddr_in
*, sizeof *sin
, M_SONAME
,
710 sin
->sin_family
= AF_INET
;
711 sin
->sin_len
= sizeof *sin
;
717 kfree(sin
, M_SONAME
);
720 sin
->sin_port
= inp
->inp_fport
;
721 sin
->sin_addr
= inp
->inp_faddr
;
724 *nam
= (struct sockaddr
*)sin
;
729 in_pcbnotifyall(struct inpcbhead
*head
, struct in_addr faddr
, int err
,
730 void (*notify
)(struct inpcb
*, int))
732 struct inpcb
*inp
, *ninp
;
735 * note: if INP_PLACEMARKER is set we must ignore the rest of
736 * the structure and skip it.
739 LIST_FOREACH_MUTABLE(inp
, head
, inp_list
, ninp
) {
740 if (inp
->inp_flags
& INP_PLACEMARKER
)
743 if (!(inp
->inp_vflag
& INP_IPV4
))
746 if (inp
->inp_faddr
.s_addr
!= faddr
.s_addr
||
747 inp
->inp_socket
== NULL
)
749 (*notify
)(inp
, err
); /* can remove inp from list! */
755 in_pcbpurgeif0(struct inpcb
*head
, struct ifnet
*ifp
)
758 struct ip_moptions
*imo
;
761 for (inp
= head
; inp
!= NULL
; inp
= LIST_NEXT(inp
, inp_list
)) {
762 if (inp
->inp_flags
& INP_PLACEMARKER
)
764 imo
= inp
->inp_moptions
;
765 if ((inp
->inp_vflag
& INP_IPV4
) && imo
!= NULL
) {
767 * Unselect the outgoing interface if it is being
770 if (imo
->imo_multicast_ifp
== ifp
)
771 imo
->imo_multicast_ifp
= NULL
;
774 * Drop multicast group membership if we joined
775 * through the interface being detached.
777 for (i
= 0, gap
= 0; i
< imo
->imo_num_memberships
;
779 if (imo
->imo_membership
[i
]->inm_ifp
== ifp
) {
780 in_delmulti(imo
->imo_membership
[i
]);
783 imo
->imo_membership
[i
- gap
] =
784 imo
->imo_membership
[i
];
786 imo
->imo_num_memberships
-= gap
;
792 * Check for alternatives when higher level complains
793 * about service problems. For now, invalidate cached
794 * routing information. If the route was created dynamically
795 * (by a redirect), time to try a default gateway again.
798 in_losing(struct inpcb
*inp
)
801 struct rt_addrinfo rtinfo
;
803 if ((rt
= inp
->inp_route
.ro_rt
)) {
804 bzero(&rtinfo
, sizeof(struct rt_addrinfo
));
805 rtinfo
.rti_info
[RTAX_DST
] = rt_key(rt
);
806 rtinfo
.rti_info
[RTAX_GATEWAY
] = rt
->rt_gateway
;
807 rtinfo
.rti_info
[RTAX_NETMASK
] = rt_mask(rt
);
808 rtinfo
.rti_flags
= rt
->rt_flags
;
809 rt_missmsg(RTM_LOSING
, &rtinfo
, rt
->rt_flags
, 0);
810 if (rt
->rt_flags
& RTF_DYNAMIC
)
811 rtrequest1_global(RTM_DELETE
, &rtinfo
, NULL
, NULL
);
812 inp
->inp_route
.ro_rt
= NULL
;
815 * A new route can be allocated
816 * the next time output is attempted.
822 * After a routing change, flush old routing
823 * and allocate a (hopefully) better one.
826 in_rtchange(struct inpcb
*inp
, int err
)
828 if (inp
->inp_route
.ro_rt
) {
829 rtfree(inp
->inp_route
.ro_rt
);
830 inp
->inp_route
.ro_rt
= NULL
;
832 * A new route can be allocated the next time
833 * output is attempted.
839 * Lookup a PCB based on the local address and port.
842 in_pcblookup_local(struct inpcbinfo
*pcbinfo
, struct in_addr laddr
,
843 u_int lport_arg
, int wild_okay
, struct ucred
*cred
)
846 int matchwild
= 3, wildcard
;
847 u_short lport
= lport_arg
;
849 struct inpcbporthead
*porthash
;
850 struct inpcbport
*phd
;
851 struct inpcb
*match
= NULL
;
854 * Best fit PCB lookup.
856 * First see if this local port is in use by looking on the
859 porthash
= &pcbinfo
->porthashbase
[INP_PCBPORTHASH(lport
,
860 pcbinfo
->porthashmask
)];
861 LIST_FOREACH(phd
, porthash
, phd_hash
) {
862 if (phd
->phd_port
== lport
)
867 * Port is in use by one or more PCBs. Look for best
870 LIST_FOREACH(inp
, &phd
->phd_pcblist
, inp_portlist
) {
873 if ((inp
->inp_vflag
& INP_IPV4
) == 0)
876 if (inp
->inp_faddr
.s_addr
!= INADDR_ANY
)
878 if (inp
->inp_laddr
.s_addr
!= INADDR_ANY
) {
879 if (laddr
.s_addr
== INADDR_ANY
)
881 else if (inp
->inp_laddr
.s_addr
!= laddr
.s_addr
)
884 if (laddr
.s_addr
!= INADDR_ANY
)
887 if (wildcard
&& !wild_okay
)
889 if (wildcard
< matchwild
&&
892 inp
->inp_socket
->so_cred
->cr_prison
)) {
894 matchwild
= wildcard
;
895 if (matchwild
== 0) {
905 * Lookup PCB in hash list.
908 in_pcblookup_hash(struct inpcbinfo
*pcbinfo
, struct in_addr faddr
, u_int fport_arg
,
909 struct in_addr laddr
, u_int lport_arg
, boolean_t wildcard
,
912 struct inpcbhead
*head
;
913 struct inpcb
*inp
, *jinp
=NULL
;
914 u_short fport
= fport_arg
, lport
= lport_arg
;
917 * First look for an exact match.
919 head
= &pcbinfo
->hashbase
[INP_PCBCONNHASH(faddr
.s_addr
, fport
,
920 laddr
.s_addr
, lport
, pcbinfo
->hashmask
)];
921 LIST_FOREACH(inp
, head
, inp_hash
) {
923 if (!(inp
->inp_vflag
& INP_IPV4
))
926 if (in_hosteq(inp
->inp_faddr
, faddr
) &&
927 in_hosteq(inp
->inp_laddr
, laddr
) &&
928 inp
->inp_fport
== fport
&& inp
->inp_lport
== lport
) {
930 if (inp
->inp_socket
== NULL
||
931 inp
->inp_socket
->so_cred
->cr_prison
== NULL
) {
942 struct inpcb
*local_wild
= NULL
;
943 struct inpcb
*jinp_wild
= NULL
;
945 struct inpcb
*local_wild_mapped
= NULL
;
947 struct inpcontainer
*ic
;
948 struct inpcontainerhead
*chead
;
949 struct sockaddr_in jsin
;
953 * Order of socket selection:
954 * 1. non-jailed, non-wild.
955 * 2. non-jailed, wild.
956 * 3. jailed, non-wild.
959 jsin
.sin_family
= AF_INET
;
960 chead
= &pcbinfo
->wildcardhashbase
[
961 INP_PCBWILDCARDHASH(lport
, pcbinfo
->wildcardhashmask
)];
962 LIST_FOREACH(ic
, chead
, ic_list
) {
964 jsin
.sin_addr
.s_addr
= laddr
.s_addr
;
966 if (!(inp
->inp_vflag
& INP_IPV4
))
969 if (inp
->inp_socket
!= NULL
)
970 cred
= inp
->inp_socket
->so_cred
;
973 if (cred
!= NULL
&& jailed(cred
)) {
977 if (!jailed_ip(cred
->cr_prison
,
978 (struct sockaddr
*)&jsin
))
981 if (inp
->inp_lport
== lport
) {
982 if (ifp
&& ifp
->if_type
== IFT_FAITH
&&
983 !(inp
->inp_flags
& INP_FAITH
))
985 if (inp
->inp_laddr
.s_addr
== laddr
.s_addr
) {
986 if (cred
!= NULL
&& jailed(cred
))
991 if (inp
->inp_laddr
.s_addr
== INADDR_ANY
) {
993 if (INP_CHECK_SOCKAF(inp
->inp_socket
,
995 local_wild_mapped
= inp
;
1006 if (local_wild
!= NULL
)
1007 return (local_wild
);
1009 if (local_wild_mapped
!= NULL
)
1010 return (local_wild_mapped
);
1024 * Insert PCB into connection hash table.
1027 in_pcbinsconnhash(struct inpcb
*inp
)
1029 struct inpcbinfo
*pcbinfo
= inp
->inp_cpcbinfo
;
1030 struct inpcbhead
*bucket
;
1031 u_int32_t hashkey_faddr
, hashkey_laddr
;
1034 if (inp
->inp_vflag
& INP_IPV6
) {
1035 hashkey_faddr
= inp
->in6p_faddr
.s6_addr32
[3] /* XXX JH */;
1036 hashkey_laddr
= inp
->in6p_laddr
.s6_addr32
[3] /* XXX JH */;
1039 hashkey_faddr
= inp
->inp_faddr
.s_addr
;
1040 hashkey_laddr
= inp
->inp_laddr
.s_addr
;
1045 KASSERT(!(inp
->inp_flags
& INP_CONNECTED
), ("already on hash list"));
1046 inp
->inp_flags
|= INP_CONNECTED
;
1049 * Insert into the connection hash table.
1051 bucket
= &pcbinfo
->hashbase
[INP_PCBCONNHASH(hashkey_faddr
,
1052 inp
->inp_fport
, hashkey_laddr
, inp
->inp_lport
, pcbinfo
->hashmask
)];
1053 LIST_INSERT_HEAD(bucket
, inp
, inp_hash
);
1057 * Remove PCB from connection hash table.
1060 in_pcbremconnhash(struct inpcb
*inp
)
1062 KASSERT(inp
->inp_flags
& INP_CONNECTED
, ("inp not connected"));
1063 LIST_REMOVE(inp
, inp_hash
);
1064 inp
->inp_flags
&= ~INP_CONNECTED
;
1068 * Insert PCB into port hash table.
1071 in_pcbinsporthash(struct inpcb
*inp
)
1073 struct inpcbinfo
*pcbinfo
= inp
->inp_pcbinfo
;
1074 struct inpcbporthead
*pcbporthash
;
1075 struct inpcbport
*phd
;
1078 * Insert into the port hash table.
1080 pcbporthash
= &pcbinfo
->porthashbase
[
1081 INP_PCBPORTHASH(inp
->inp_lport
, pcbinfo
->porthashmask
)];
1083 /* Go through port list and look for a head for this lport. */
1084 LIST_FOREACH(phd
, pcbporthash
, phd_hash
)
1085 if (phd
->phd_port
== inp
->inp_lport
)
1088 /* If none exists, malloc one and tack it on. */
1090 MALLOC(phd
, struct inpcbport
*, sizeof(struct inpcbport
),
1091 M_PCB
, M_INTWAIT
| M_NULLOK
);
1093 return (ENOBUFS
); /* XXX */
1094 phd
->phd_port
= inp
->inp_lport
;
1095 LIST_INIT(&phd
->phd_pcblist
);
1096 LIST_INSERT_HEAD(pcbporthash
, phd
, phd_hash
);
1100 LIST_INSERT_HEAD(&phd
->phd_pcblist
, inp
, inp_portlist
);
1106 in_pcbinswildcardhash_oncpu(struct inpcb
*inp
, struct inpcbinfo
*pcbinfo
)
1108 struct inpcontainer
*ic
;
1109 struct inpcontainerhead
*bucket
;
1111 bucket
= &pcbinfo
->wildcardhashbase
[
1112 INP_PCBWILDCARDHASH(inp
->inp_lport
, pcbinfo
->wildcardhashmask
)];
1114 ic
= kmalloc(sizeof(struct inpcontainer
), M_TEMP
, M_INTWAIT
);
1116 LIST_INSERT_HEAD(bucket
, ic
, ic_list
);
1120 * Insert PCB into wildcard hash table.
1123 in_pcbinswildcardhash(struct inpcb
*inp
)
1125 struct inpcbinfo
*pcbinfo
= inp
->inp_pcbinfo
;
1127 KKASSERT(pcbinfo
!= NULL
);
1129 in_pcbinswildcardhash_oncpu(inp
, pcbinfo
);
1130 inp
->inp_flags
|= INP_WILDCARD
;
1134 in_pcbremwildcardhash_oncpu(struct inpcb
*inp
, struct inpcbinfo
*pcbinfo
)
1136 struct inpcontainer
*ic
;
1137 struct inpcontainerhead
*head
;
1140 head
= &pcbinfo
->wildcardhashbase
[
1141 INP_PCBWILDCARDHASH(inp
->inp_lport
, pcbinfo
->wildcardhashmask
)];
1143 LIST_FOREACH(ic
, head
, ic_list
) {
1144 if (ic
->ic_inp
== inp
)
1147 return; /* not found! */
1150 LIST_REMOVE(ic
, ic_list
); /* remove container from bucket chain */
1151 kfree(ic
, M_TEMP
); /* deallocate container */
1155 * Remove PCB from wildcard hash table.
1158 in_pcbremwildcardhash(struct inpcb
*inp
)
1160 struct inpcbinfo
*pcbinfo
= inp
->inp_pcbinfo
;
1162 KASSERT(inp
->inp_flags
& INP_WILDCARD
, ("inp not wildcard"));
1163 in_pcbremwildcardhash_oncpu(inp
, pcbinfo
);
1164 inp
->inp_flags
&= ~INP_WILDCARD
;
1168 * Remove PCB from various lists.
1171 in_pcbremlists(struct inpcb
*inp
)
1173 if (inp
->inp_lport
) {
1174 struct inpcbport
*phd
= inp
->inp_phd
;
1176 LIST_REMOVE(inp
, inp_portlist
);
1177 if (LIST_FIRST(&phd
->phd_pcblist
) == NULL
) {
1178 LIST_REMOVE(phd
, phd_hash
);
1182 if (inp
->inp_flags
& INP_WILDCARD
) {
1183 in_pcbremwildcardhash(inp
);
1184 } else if (inp
->inp_flags
& INP_CONNECTED
) {
1185 in_pcbremconnhash(inp
);
1187 LIST_REMOVE(inp
, inp_list
);
1188 inp
->inp_pcbinfo
->ipi_count
--;
1192 prison_xinpcb(struct thread
*td
, struct inpcb
*inp
)
1196 if (td
->td_proc
== NULL
)
1198 cr
= td
->td_proc
->p_ucred
;
1199 if (cr
->cr_prison
== NULL
)
1201 if (inp
->inp_socket
&& inp
->inp_socket
->so_cred
&&
1202 inp
->inp_socket
->so_cred
->cr_prison
&&
1203 cr
->cr_prison
== inp
->inp_socket
->so_cred
->cr_prison
)
1209 in_pcblist_global(SYSCTL_HANDLER_ARGS
)
1211 struct inpcbinfo
*pcbinfo
= arg1
;
1212 struct inpcb
*inp
, *marker
;
1218 * The process of preparing the TCB list is too time-consuming and
1219 * resource-intensive to repeat twice on every request.
1221 if (req
->oldptr
== NULL
) {
1222 n
= pcbinfo
->ipi_count
;
1223 req
->oldidx
= (n
+ n
/8 + 10) * sizeof(struct xinpcb
);
1227 if (req
->newptr
!= NULL
)
1231 * OK, now we're committed to doing something. Re-fetch ipi_count
1232 * after obtaining the generation count.
1234 gencnt
= pcbinfo
->ipi_gencnt
;
1235 n
= pcbinfo
->ipi_count
;
1237 marker
= kmalloc(sizeof(struct inpcb
), M_TEMP
, M_WAITOK
|M_ZERO
);
1238 marker
->inp_flags
|= INP_PLACEMARKER
;
1239 LIST_INSERT_HEAD(&pcbinfo
->pcblisthead
, marker
, inp_list
);
1244 while ((inp
= LIST_NEXT(marker
, inp_list
)) != NULL
&& i
< n
) {
1245 LIST_REMOVE(marker
, inp_list
);
1246 LIST_INSERT_AFTER(inp
, marker
, inp_list
);
1248 if (inp
->inp_flags
& INP_PLACEMARKER
)
1250 if (inp
->inp_gencnt
> gencnt
)
1252 if (prison_xinpcb(req
->td
, inp
))
1254 bzero(&xi
, sizeof xi
);
1255 xi
.xi_len
= sizeof xi
;
1256 bcopy(inp
, &xi
.xi_inp
, sizeof *inp
);
1257 if (inp
->inp_socket
)
1258 sotoxsocket(inp
->inp_socket
, &xi
.xi_socket
);
1259 if ((error
= SYSCTL_OUT(req
, &xi
, sizeof xi
)) != 0)
1263 LIST_REMOVE(marker
, inp_list
);
1264 if (error
== 0 && i
< n
) {
1265 bzero(&xi
, sizeof xi
);
1266 xi
.xi_len
= sizeof xi
;
1268 error
= SYSCTL_OUT(req
, &xi
, sizeof xi
);
1272 kfree(marker
, M_TEMP
);