2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1982, 1986, 1991, 1993, 1995
24 * The Regents of the University of California. All rights reserved.
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * 3. All advertising materials mentioning features or use of this software
35 * must display the following acknowledgement:
36 * This product includes software developed by the University of
37 * California, Berkeley and its contributors.
38 * 4. Neither the name of the University nor the names of its contributors
39 * may be used to endorse or promote products derived from this software
40 * without specific prior written permission.
42 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * @(#)in_pcb.c 8.4 (Berkeley) 5/24/95
55 * $FreeBSD: src/sys/netinet/in_pcb.c,v 1.59.2.17 2001/08/13 16:26:17 ume Exp $
58 #include <sys/param.h>
59 #include <sys/systm.h>
60 #include <sys/malloc.h>
62 #include <sys/domain.h>
63 #include <sys/protosw.h>
64 #include <sys/socket.h>
65 #include <sys/socketvar.h>
70 #include <sys/kernel.h>
71 #include <sys/sysctl.h>
73 #include <machine/limits.h>
76 #include <kern/zalloc.h>
80 #include <net/if_types.h>
81 #include <net/route.h>
83 #include <netinet/in.h>
84 #include <netinet/in_pcb.h>
85 #include <netinet/in_var.h>
86 #include <netinet/ip_var.h>
88 #include <netinet/ip6.h>
89 #include <netinet6/ip6_var.h>
95 #include <netinet6/ipsec.h>
96 #include <netkey/key.h>
99 #include <sys/kdebug.h>
102 extern int ipsec_bypass
;
105 #define DBG_FNC_PCB_LOOKUP NETDBG_CODE(DBG_NETTCP, (6 << 8))
106 #define DBG_FNC_PCB_HLOOKUP NETDBG_CODE(DBG_NETTCP, ((6 << 8) | 1))
108 struct in_addr zeroin_addr
;
111 * These configure the range of local port addresses assigned to
112 * "unspecified" outgoing connections/packets/whatever.
114 int ipport_lowfirstauto
= IPPORT_RESERVED
- 1; /* 1023 */
115 int ipport_lowlastauto
= IPPORT_RESERVEDSTART
; /* 600 */
117 int ipport_firstauto
= IPPORT_RESERVED
; /* 1024 */
118 int ipport_lastauto
= IPPORT_USERRESERVED
; /* 5000 */
120 int ipport_firstauto
= IPPORT_HIFIRSTAUTO
; /* 49152 */
121 int ipport_lastauto
= IPPORT_HILASTAUTO
; /* 65535 */
123 int ipport_hifirstauto
= IPPORT_HIFIRSTAUTO
; /* 49152 */
124 int ipport_hilastauto
= IPPORT_HILASTAUTO
; /* 65535 */
126 #define RANGECHK(var, min, max) \
127 if ((var) < (min)) { (var) = (min); } \
128 else if ((var) > (max)) { (var) = (max); }
131 sysctl_net_ipport_check SYSCTL_HANDLER_ARGS
133 int error
= sysctl_handle_int(oidp
,
134 oidp
->oid_arg1
, oidp
->oid_arg2
, req
);
136 RANGECHK(ipport_lowfirstauto
, 1, IPPORT_RESERVED
- 1);
137 RANGECHK(ipport_lowlastauto
, 1, IPPORT_RESERVED
- 1);
138 RANGECHK(ipport_firstauto
, IPPORT_RESERVED
, USHRT_MAX
);
139 RANGECHK(ipport_lastauto
, IPPORT_RESERVED
, USHRT_MAX
);
140 RANGECHK(ipport_hifirstauto
, IPPORT_RESERVED
, USHRT_MAX
);
141 RANGECHK(ipport_hilastauto
, IPPORT_RESERVED
, USHRT_MAX
);
148 SYSCTL_NODE(_net_inet_ip
, IPPROTO_IP
, portrange
, CTLFLAG_RW
, 0, "IP Ports");
150 SYSCTL_PROC(_net_inet_ip_portrange
, OID_AUTO
, lowfirst
, CTLTYPE_INT
|CTLFLAG_RW
,
151 &ipport_lowfirstauto
, 0, &sysctl_net_ipport_check
, "I", "");
152 SYSCTL_PROC(_net_inet_ip_portrange
, OID_AUTO
, lowlast
, CTLTYPE_INT
|CTLFLAG_RW
,
153 &ipport_lowlastauto
, 0, &sysctl_net_ipport_check
, "I", "");
154 SYSCTL_PROC(_net_inet_ip_portrange
, OID_AUTO
, first
, CTLTYPE_INT
|CTLFLAG_RW
,
155 &ipport_firstauto
, 0, &sysctl_net_ipport_check
, "I", "");
156 SYSCTL_PROC(_net_inet_ip_portrange
, OID_AUTO
, last
, CTLTYPE_INT
|CTLFLAG_RW
,
157 &ipport_lastauto
, 0, &sysctl_net_ipport_check
, "I", "");
158 SYSCTL_PROC(_net_inet_ip_portrange
, OID_AUTO
, hifirst
, CTLTYPE_INT
|CTLFLAG_RW
,
159 &ipport_hifirstauto
, 0, &sysctl_net_ipport_check
, "I", "");
160 SYSCTL_PROC(_net_inet_ip_portrange
, OID_AUTO
, hilast
, CTLTYPE_INT
|CTLFLAG_RW
,
161 &ipport_hilastauto
, 0, &sysctl_net_ipport_check
, "I", "");
164 * in_pcb.c: manage the Protocol Control Blocks.
166 * NOTE: It is assumed that most of these functions will be called at
167 * splnet(). XXX - There are, unfortunately, a few exceptions to this
168 * rule that should be fixed.
172 * Allocate a PCB and associate it with the socket.
175 in_pcballoc(so
, pcbinfo
, p
)
177 struct inpcbinfo
*pcbinfo
;
180 register struct inpcb
*inp
;
186 if (so
->cached_in_sock_layer
== 0) {
188 printf("PCBALLOC calling zalloc for socket %x\n", so
);
190 inp
= (struct inpcb
*) zalloc(pcbinfo
->ipi_zone
);
193 bzero((caddr_t
)inp
, sizeof(*inp
));
197 printf("PCBALLOC reusing PCB for socket %x\n", so
);
199 inp
= (struct inpcb
*) so
->so_saved_pcb
;
200 temp
= inp
->inp_saved_ppcb
;
201 bzero((caddr_t
) inp
, sizeof(*inp
));
202 inp
->inp_saved_ppcb
= temp
;
205 inp
->inp_gencnt
= ++pcbinfo
->ipi_gencnt
;
206 inp
->inp_pcbinfo
= pcbinfo
;
207 inp
->inp_socket
= so
;
210 if (ipsec_bypass
== 0) {
211 error
= ipsec_init_policy(so
, &inp
->inp_sp
);
213 zfree(pcbinfo
->ipi_zone
, (vm_offset_t
)inp
);
220 if (INP_SOCKAF(so
) == AF_INET6
&& !ip6_mapped_addr_on
)
221 inp
->inp_flags
|= IN6P_IPV6_V6ONLY
;
223 LIST_INSERT_HEAD(pcbinfo
->listhead
, inp
, inp_list
);
224 pcbinfo
->ipi_count
++;
225 so
->so_pcb
= (caddr_t
)inp
;
227 if (ip6_auto_flowlabel
)
228 inp
->inp_flags
|= IN6P_AUTOFLOWLABEL
;
234 in_pcbbind(inp
, nam
, p
)
235 register struct inpcb
*inp
;
236 struct sockaddr
*nam
;
239 register struct socket
*so
= inp
->inp_socket
;
240 unsigned short *lastport
;
241 struct sockaddr_in
*sin
;
242 struct inpcbinfo
*pcbinfo
= inp
->inp_pcbinfo
;
244 int wild
= 0, reuseport
= (so
->so_options
& SO_REUSEPORT
);
247 if (TAILQ_EMPTY(&in_ifaddrhead
)) /* XXX broken! */
248 return (EADDRNOTAVAIL
);
249 if (inp
->inp_lport
|| inp
->inp_laddr
.s_addr
!= INADDR_ANY
)
251 if ((so
->so_options
& (SO_REUSEADDR
|SO_REUSEPORT
)) == 0)
254 sin
= (struct sockaddr_in
*)nam
;
255 if (nam
->sa_len
!= sizeof (*sin
))
259 * We should check the family, but old programs
260 * incorrectly fail to initialize it.
262 if (sin
->sin_family
!= AF_INET
)
263 return (EAFNOSUPPORT
);
265 lport
= sin
->sin_port
;
266 if (IN_MULTICAST(ntohl(sin
->sin_addr
.s_addr
))) {
268 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
269 * allow complete duplication of binding if
270 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
271 * and a multicast address is bound on both
272 * new and duplicated sockets.
274 if (so
->so_options
& SO_REUSEADDR
)
275 reuseport
= SO_REUSEADDR
|SO_REUSEPORT
;
276 } else if (sin
->sin_addr
.s_addr
!= INADDR_ANY
) {
277 sin
->sin_port
= 0; /* yech... */
278 if (ifa_ifwithaddr((struct sockaddr
*)sin
) == 0)
279 return (EADDRNOTAVAIL
);
285 if (ntohs(lport
) < IPPORT_RESERVED
&& p
&&
286 suser(p
->p_ucred
, &p
->p_acflag
))
289 !IN_MULTICAST(ntohl(sin
->sin_addr
.s_addr
))) {
290 t
= in_pcblookup_local(inp
->inp_pcbinfo
,
291 sin
->sin_addr
, lport
, INPLOOKUP_WILDCARD
);
293 (ntohl(sin
->sin_addr
.s_addr
) != INADDR_ANY
||
294 ntohl(t
->inp_laddr
.s_addr
) != INADDR_ANY
||
295 (t
->inp_socket
->so_options
&
296 SO_REUSEPORT
) == 0) &&
297 (so
->so_uid
!= t
->inp_socket
->so_uid
)) {
299 if (ntohl(sin
->sin_addr
.s_addr
) !=
301 ntohl(t
->inp_laddr
.s_addr
) !=
304 INP_SOCKAF(t
->inp_socket
))
305 #endif /* defined(INET6) */
309 t
= in_pcblookup_local(pcbinfo
, sin
->sin_addr
,
312 (reuseport
& t
->inp_socket
->so_options
) == 0) {
314 if (ip6_mapped_addr_on
== 0 ||
315 ntohl(sin
->sin_addr
.s_addr
) !=
317 ntohl(t
->inp_laddr
.s_addr
) !=
320 INP_SOCKAF(t
->inp_socket
))
321 #endif /* defined(INET6) */
325 inp
->inp_laddr
= sin
->sin_addr
;
331 inp
->inp_flags
|= INP_ANONPORT
;
333 if (inp
->inp_flags
& INP_HIGHPORT
) {
334 first
= ipport_hifirstauto
; /* sysctl */
335 last
= ipport_hilastauto
;
336 lastport
= &pcbinfo
->lasthi
;
337 } else if (inp
->inp_flags
& INP_LOWPORT
) {
338 if (p
&& (error
= suser(p
->p_ucred
, &p
->p_acflag
)))
340 first
= ipport_lowfirstauto
; /* 1023 */
341 last
= ipport_lowlastauto
; /* 600 */
342 lastport
= &pcbinfo
->lastlow
;
344 first
= ipport_firstauto
; /* sysctl */
345 last
= ipport_lastauto
;
346 lastport
= &pcbinfo
->lastport
;
349 * Simple check to ensure all ports are not used up causing
352 * We split the two cases (up and down) so that the direction
353 * is not being tested on each round of the loop.
359 count
= first
- last
;
362 if (count
-- < 0) { /* completely used? */
363 inp
->inp_laddr
.s_addr
= INADDR_ANY
;
364 return (EADDRNOTAVAIL
);
367 if (*lastport
> first
|| *lastport
< last
)
369 lport
= htons(*lastport
);
370 } while (in_pcblookup_local(pcbinfo
,
371 inp
->inp_laddr
, lport
, wild
));
376 count
= last
- first
;
379 if (count
-- < 0) { /* completely used? */
380 inp
->inp_laddr
.s_addr
= INADDR_ANY
;
381 return (EADDRNOTAVAIL
);
384 if (*lastport
< first
|| *lastport
> last
)
386 lport
= htons(*lastport
);
387 } while (in_pcblookup_local(pcbinfo
,
388 inp
->inp_laddr
, lport
, wild
));
391 inp
->inp_lport
= lport
;
392 if (in_pcbinshash(inp
) != 0) {
393 inp
->inp_laddr
.s_addr
= INADDR_ANY
;
401 * Transform old in_pcbconnect() into an inner subroutine for new
402 * in_pcbconnect(): Do some validity-checking on the remote
403 * address (in mbuf 'nam') and then determine local host address
404 * (i.e., which interface) to use to access that remote host.
406 * This preserves definition of in_pcbconnect(), while supporting a
407 * slightly different version for T/TCP. (This is more than
408 * a bit of a kludge, but cleaning up the internal interfaces would
409 * have forced minor changes in every protocol).
413 in_pcbladdr(inp
, nam
, plocal_sin
)
414 register struct inpcb
*inp
;
415 struct sockaddr
*nam
;
416 struct sockaddr_in
**plocal_sin
;
418 struct in_ifaddr
*ia
;
419 register struct sockaddr_in
*sin
= (struct sockaddr_in
*)nam
;
421 if (nam
->sa_len
!= sizeof (*sin
))
423 if (sin
->sin_family
!= AF_INET
)
424 return (EAFNOSUPPORT
);
425 if (sin
->sin_port
== 0)
426 return (EADDRNOTAVAIL
);
427 if (!TAILQ_EMPTY(&in_ifaddrhead
)) {
429 * If the destination address is INADDR_ANY,
430 * use the primary local address.
431 * If the supplied address is INADDR_BROADCAST,
432 * and the primary interface supports broadcast,
433 * choose the broadcast address for that interface.
435 #define satosin(sa) ((struct sockaddr_in *)(sa))
436 #define sintosa(sin) ((struct sockaddr *)(sin))
437 #define ifatoia(ifa) ((struct in_ifaddr *)(ifa))
438 if (sin
->sin_addr
.s_addr
== INADDR_ANY
)
439 sin
->sin_addr
= IA_SIN(TAILQ_FIRST(&in_ifaddrhead
))->sin_addr
;
440 else if (sin
->sin_addr
.s_addr
== (u_long
)INADDR_BROADCAST
&&
441 (TAILQ_FIRST(&in_ifaddrhead
)->ia_ifp
->if_flags
& IFF_BROADCAST
))
442 sin
->sin_addr
= satosin(&TAILQ_FIRST(&in_ifaddrhead
)->ia_broadaddr
)->sin_addr
;
444 if (inp
->inp_laddr
.s_addr
== INADDR_ANY
) {
445 register struct route
*ro
;
447 ia
= (struct in_ifaddr
*)0;
449 * If route is known or can be allocated now,
450 * our src addr is taken from the i/f, else punt.
452 ro
= &inp
->inp_route
;
454 (satosin(&ro
->ro_dst
)->sin_addr
.s_addr
!=
455 sin
->sin_addr
.s_addr
||
456 inp
->inp_socket
->so_options
& SO_DONTROUTE
)) {
458 ro
->ro_rt
= (struct rtentry
*)0;
460 if ((inp
->inp_socket
->so_options
& SO_DONTROUTE
) == 0 && /*XXX*/
461 (ro
->ro_rt
== (struct rtentry
*)0 ||
462 ro
->ro_rt
->rt_ifp
== (struct ifnet
*)0)) {
463 /* No route yet, so try to acquire one */
464 ro
->ro_dst
.sa_family
= AF_INET
;
465 ro
->ro_dst
.sa_len
= sizeof(struct sockaddr_in
);
466 ((struct sockaddr_in
*) &ro
->ro_dst
)->sin_addr
=
471 * If we found a route, use the address
472 * corresponding to the outgoing interface
473 * unless it is the loopback (in case a route
474 * to our address on another net goes to loopback).
476 if (ro
->ro_rt
&& !(ro
->ro_rt
->rt_ifp
->if_flags
& IFF_LOOPBACK
))
477 ia
= ifatoia(ro
->ro_rt
->rt_ifa
);
479 u_short fport
= sin
->sin_port
;
482 ia
= ifatoia(ifa_ifwithdstaddr(sintosa(sin
)));
484 ia
= ifatoia(ifa_ifwithnet(sintosa(sin
)));
485 sin
->sin_port
= fport
;
487 ia
= TAILQ_FIRST(&in_ifaddrhead
);
489 return (EADDRNOTAVAIL
);
492 * If the destination address is multicast and an outgoing
493 * interface has been set as a multicast option, use the
494 * address of that interface as our source address.
496 if (IN_MULTICAST(ntohl(sin
->sin_addr
.s_addr
)) &&
497 inp
->inp_moptions
!= NULL
) {
498 struct ip_moptions
*imo
;
501 imo
= inp
->inp_moptions
;
502 if (imo
->imo_multicast_ifp
!= NULL
) {
503 ifp
= imo
->imo_multicast_ifp
;
504 TAILQ_FOREACH(ia
, &in_ifaddrhead
, ia_link
)
505 if (ia
->ia_ifp
== ifp
)
508 return (EADDRNOTAVAIL
);
512 * Don't do pcblookup call here; return interface in plocal_sin
513 * and exit to caller, that will do the lookup.
515 *plocal_sin
= &ia
->ia_addr
;
523 * Connect from a socket to a specified address.
524 * Both address and port must be specified in argument sin.
525 * If don't have a local address for this socket yet,
529 in_pcbconnect(inp
, nam
, p
)
530 register struct inpcb
*inp
;
531 struct sockaddr
*nam
;
534 struct sockaddr_in
*ifaddr
;
535 struct sockaddr_in
*sin
= (struct sockaddr_in
*)nam
;
536 struct sockaddr_in sa
;
540 * Call inner routine, to assign local interface address.
542 if ((error
= in_pcbladdr(inp
, nam
, &ifaddr
)) != 0)
545 if (in_pcblookup_hash(inp
->inp_pcbinfo
, sin
->sin_addr
, sin
->sin_port
,
546 inp
->inp_laddr
.s_addr
? inp
->inp_laddr
: ifaddr
->sin_addr
,
547 inp
->inp_lport
, 0, NULL
) != NULL
) {
550 if (inp
->inp_laddr
.s_addr
== INADDR_ANY
) {
551 if (inp
->inp_lport
== 0) {
552 error
= in_pcbbind(inp
, (struct sockaddr
*)0, p
);
556 inp
->inp_laddr
= ifaddr
->sin_addr
;
558 inp
->inp_faddr
= sin
->sin_addr
;
559 inp
->inp_fport
= sin
->sin_port
;
565 in_pcbdisconnect(inp
)
569 inp
->inp_faddr
.s_addr
= INADDR_ANY
;
572 if (inp
->inp_socket
->so_state
& SS_NOFDREF
)
580 struct socket
*so
= inp
->inp_socket
;
581 struct inpcbinfo
*ipi
= inp
->inp_pcbinfo
;
582 struct rtentry
*rt
= inp
->inp_route
.ro_rt
;
585 ipsec4_delete_pcbpolicy(inp
);
587 inp
->inp_gencnt
= ++ipi
->ipi_gencnt
;
591 if (so
->cached_in_sock_layer
)
592 printf("PCB_DETACH for cached socket %x\n", so
);
594 printf("PCB_DETACH for allocated socket %x\n", so
);
599 if (inp
->inp_options
)
600 (void)m_free(inp
->inp_options
);
603 * route deletion requires reference count to be <= zero
605 if ((rt
->rt_flags
& RTF_DELCLONE
) &&
606 (rt
->rt_flags
& RTF_WASCLONED
) &&
607 (rt
->rt_refcnt
<= 1)) {
609 rt
->rt_flags
&= ~RTF_UP
;
610 rtrequest(RTM_DELETE
, rt_key(rt
),
611 rt
->rt_gateway
, rt_mask(rt
),
612 rt
->rt_flags
, (struct rtentry
**)0);
617 ip_freemoptions(inp
->inp_moptions
);
619 if (so
->cached_in_sock_layer
)
620 so
->so_saved_pcb
= (caddr_t
) inp
;
622 zfree(ipi
->ipi_zone
, (vm_offset_t
) inp
);
628 * The calling convention of in_setsockaddr() and in_setpeeraddr() was
629 * modified to match the pru_sockaddr() and pru_peeraddr() entry points
630 * in struct pr_usrreqs, so that protocols can just reference then directly
631 * without the need for a wrapper function. The socket must have a valid
632 * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
633 * except through a kernel programming error, so it is acceptable to panic
634 * (or in this case trap) if the PCB is invalid. (Actually, we don't trap
635 * because there actually /is/ a programming error somewhere... XXX)
638 in_setsockaddr(so
, nam
)
640 struct sockaddr
**nam
;
643 register struct inpcb
*inp
;
644 register struct sockaddr_in
*sin
;
647 * Do the malloc first in case it blocks.
649 MALLOC(sin
, struct sockaddr_in
*, sizeof *sin
, M_SONAME
, M_WAITOK
);
652 bzero(sin
, sizeof *sin
);
653 sin
->sin_family
= AF_INET
;
654 sin
->sin_len
= sizeof(*sin
);
663 sin
->sin_port
= inp
->inp_lport
;
664 sin
->sin_addr
= inp
->inp_laddr
;
667 *nam
= (struct sockaddr
*)sin
;
672 in_setpeeraddr(so
, nam
)
674 struct sockaddr
**nam
;
678 register struct sockaddr_in
*sin
;
681 * Do the malloc first in case it blocks.
683 MALLOC(sin
, struct sockaddr_in
*, sizeof *sin
, M_SONAME
, M_WAITOK
);
686 bzero((caddr_t
)sin
, sizeof (*sin
));
687 sin
->sin_family
= AF_INET
;
688 sin
->sin_len
= sizeof(*sin
);
697 sin
->sin_port
= inp
->inp_fport
;
698 sin
->sin_addr
= inp
->inp_faddr
;
701 *nam
= (struct sockaddr
*)sin
;
706 in_pcbnotifyall(head
, faddr
, errno
, notify
)
707 struct inpcbhead
*head
;
708 struct in_addr faddr
;
709 void (*notify
) __P((struct inpcb
*, int));
711 struct inpcb
*inp
, *ninp
;
715 for (inp
= LIST_FIRST(head
); inp
!= NULL
; inp
= ninp
) {
716 ninp
= LIST_NEXT(inp
, inp_list
);
718 if ((inp
->inp_vflag
& INP_IPV4
) == 0)
721 if (inp
->inp_faddr
.s_addr
!= faddr
.s_addr
||
722 inp
->inp_socket
== NULL
)
724 (*notify
)(inp
, errno
);
730 in_pcbpurgeif0(head
, ifp
)
735 struct ip_moptions
*imo
;
738 for (inp
= head
; inp
!= NULL
; inp
= LIST_NEXT(inp
, inp_list
)) {
739 imo
= inp
->inp_moptions
;
740 if ((inp
->inp_vflag
& INP_IPV4
) &&
743 * Unselect the outgoing interface if it is being
746 if (imo
->imo_multicast_ifp
== ifp
)
747 imo
->imo_multicast_ifp
= NULL
;
750 * Drop multicast group membership if we joined
751 * through the interface being detached.
753 for (i
= 0, gap
= 0; i
< imo
->imo_num_memberships
;
755 if (imo
->imo_membership
[i
]->inm_ifp
== ifp
) {
756 in_delmulti(imo
->imo_membership
[i
]);
759 imo
->imo_membership
[i
- gap
] =
760 imo
->imo_membership
[i
];
762 imo
->imo_num_memberships
-= gap
;
768 * Check for alternatives when higher level complains
769 * about service problems. For now, invalidate cached
770 * routing information. If the route was created dynamically
771 * (by a redirect), time to try a default gateway again.
777 register struct rtentry
*rt
;
778 struct rt_addrinfo info
;
780 if ((rt
= inp
->inp_route
.ro_rt
)) {
781 bzero((caddr_t
)&info
, sizeof(info
));
782 info
.rti_info
[RTAX_DST
] =
783 (struct sockaddr
*)&inp
->inp_route
.ro_dst
;
784 info
.rti_info
[RTAX_GATEWAY
] = rt
->rt_gateway
;
785 info
.rti_info
[RTAX_NETMASK
] = rt_mask(rt
);
786 rt_missmsg(RTM_LOSING
, &info
, rt
->rt_flags
, 0);
787 if (rt
->rt_flags
& RTF_DYNAMIC
)
788 (void) rtrequest(RTM_DELETE
, rt_key(rt
),
789 rt
->rt_gateway
, rt_mask(rt
), rt
->rt_flags
,
790 (struct rtentry
**)0);
791 inp
->inp_route
.ro_rt
= 0;
794 * A new route can be allocated
795 * the next time output is attempted.
801 * After a routing change, flush old routing
802 * and allocate a (hopefully) better one.
805 in_rtchange(inp
, errno
)
806 register struct inpcb
*inp
;
809 if (inp
->inp_route
.ro_rt
) {
810 rtfree(inp
->inp_route
.ro_rt
);
811 inp
->inp_route
.ro_rt
= 0;
813 * A new route can be allocated the next time
814 * output is attempted.
820 * Lookup a PCB based on the local address and port.
823 in_pcblookup_local(pcbinfo
, laddr
, lport_arg
, wild_okay
)
824 struct inpcbinfo
*pcbinfo
;
825 struct in_addr laddr
;
829 register struct inpcb
*inp
;
830 int matchwild
= 3, wildcard
;
831 u_short lport
= lport_arg
;
833 KERNEL_DEBUG(DBG_FNC_PCB_LOOKUP
| DBG_FUNC_START
, 0,0,0,0,0);
836 struct inpcbhead
*head
;
838 * Look for an unconnected (wildcard foreign addr) PCB that
839 * matches the local address and port we're looking for.
841 head
= &pcbinfo
->hashbase
[INP_PCBHASH(INADDR_ANY
, lport
, 0, pcbinfo
->hashmask
)];
842 LIST_FOREACH(inp
, head
, inp_hash
) {
844 if ((inp
->inp_vflag
& INP_IPV4
) == 0)
847 if (inp
->inp_faddr
.s_addr
== INADDR_ANY
&&
848 inp
->inp_laddr
.s_addr
== laddr
.s_addr
&&
849 inp
->inp_lport
== lport
) {
859 KERNEL_DEBUG(DBG_FNC_PCB_LOOKUP
| DBG_FUNC_END
, 0,0,0,0,0);
862 struct inpcbporthead
*porthash
;
863 struct inpcbport
*phd
;
864 struct inpcb
*match
= NULL
;
866 * Best fit PCB lookup.
868 * First see if this local port is in use by looking on the
871 porthash
= &pcbinfo
->porthashbase
[INP_PCBPORTHASH(lport
,
872 pcbinfo
->porthashmask
)];
873 LIST_FOREACH(phd
, porthash
, phd_hash
) {
874 if (phd
->phd_port
== lport
)
879 * Port is in use by one or more PCBs. Look for best
882 LIST_FOREACH(inp
, &phd
->phd_pcblist
, inp_portlist
) {
885 if ((inp
->inp_vflag
& INP_IPV4
) == 0)
888 if (inp
->inp_faddr
.s_addr
!= INADDR_ANY
)
890 if (inp
->inp_laddr
.s_addr
!= INADDR_ANY
) {
891 if (laddr
.s_addr
== INADDR_ANY
)
893 else if (inp
->inp_laddr
.s_addr
!= laddr
.s_addr
)
896 if (laddr
.s_addr
!= INADDR_ANY
)
899 if (wildcard
< matchwild
) {
901 matchwild
= wildcard
;
902 if (matchwild
== 0) {
908 KERNEL_DEBUG(DBG_FNC_PCB_LOOKUP
| DBG_FUNC_END
, match
,0,0,0,0);
914 * Lookup PCB in hash list.
917 in_pcblookup_hash(pcbinfo
, faddr
, fport_arg
, laddr
, lport_arg
, wildcard
,
919 struct inpcbinfo
*pcbinfo
;
920 struct in_addr faddr
, laddr
;
921 u_int fport_arg
, lport_arg
;
925 struct inpcbhead
*head
;
926 register struct inpcb
*inp
;
927 u_short fport
= fport_arg
, lport
= lport_arg
;
930 * We may have found the pcb in the last lookup - check this first.
933 if ((!IN_MULTICAST(laddr
.s_addr
)) && (pcbinfo
->last_pcb
)) {
934 if (faddr
.s_addr
== pcbinfo
->last_pcb
->inp_faddr
.s_addr
&&
935 laddr
.s_addr
== pcbinfo
->last_pcb
->inp_laddr
.s_addr
&&
936 fport_arg
== pcbinfo
->last_pcb
->inp_fport
&&
937 lport_arg
== pcbinfo
->last_pcb
->inp_lport
) {
941 return (pcbinfo
->last_pcb
);
944 pcbinfo
->last_pcb
= 0;
948 * First look for an exact match.
950 head
= &pcbinfo
->hashbase
[INP_PCBHASH(faddr
.s_addr
, lport
, fport
, pcbinfo
->hashmask
)];
951 LIST_FOREACH(inp
, head
, inp_hash
) {
953 if ((inp
->inp_vflag
& INP_IPV4
) == 0)
956 if (inp
->inp_faddr
.s_addr
== faddr
.s_addr
&&
957 inp
->inp_laddr
.s_addr
== laddr
.s_addr
&&
958 inp
->inp_fport
== fport
&&
959 inp
->inp_lport
== lport
) {
967 struct inpcb
*local_wild
= NULL
;
969 struct inpcb
*local_wild_mapped
= NULL
;
972 head
= &pcbinfo
->hashbase
[INP_PCBHASH(INADDR_ANY
, lport
, 0, pcbinfo
->hashmask
)];
973 LIST_FOREACH(inp
, head
, inp_hash
) {
975 if ((inp
->inp_vflag
& INP_IPV4
) == 0)
978 if (inp
->inp_faddr
.s_addr
== INADDR_ANY
&&
979 inp
->inp_lport
== lport
) {
980 #if defined(NFAITH) && NFAITH > 0
981 if (ifp
&& ifp
->if_type
== IFT_FAITH
&&
982 (inp
->inp_flags
& INP_FAITH
) == 0)
985 if (inp
->inp_laddr
.s_addr
== laddr
.s_addr
)
987 else if (inp
->inp_laddr
.s_addr
== INADDR_ANY
) {
989 if (INP_CHECK_SOCKAF(inp
->inp_socket
,
991 local_wild_mapped
= inp
;
993 #endif /* defined(INET6) */
999 if (local_wild
== NULL
)
1000 return (local_wild_mapped
);
1001 #endif /* defined(INET6) */
1002 return (local_wild
);
1012 * Insert PCB onto various hash lists.
1018 struct inpcbhead
*pcbhash
;
1019 struct inpcbporthead
*pcbporthash
;
1020 struct inpcbinfo
*pcbinfo
= inp
->inp_pcbinfo
;
1021 struct inpcbport
*phd
;
1022 u_int32_t hashkey_faddr
;
1025 if (inp
->inp_vflag
& INP_IPV6
)
1026 hashkey_faddr
= inp
->in6p_faddr
.s6_addr32
[3] /* XXX */;
1029 hashkey_faddr
= inp
->inp_faddr
.s_addr
;
1031 pcbhash
= &pcbinfo
->hashbase
[INP_PCBHASH(hashkey_faddr
,
1032 inp
->inp_lport
, inp
->inp_fport
, pcbinfo
->hashmask
)];
1034 pcbporthash
= &pcbinfo
->porthashbase
[INP_PCBPORTHASH(inp
->inp_lport
,
1035 pcbinfo
->porthashmask
)];
1038 * Go through port list and look for a head for this lport.
1040 LIST_FOREACH(phd
, pcbporthash
, phd_hash
) {
1041 if (phd
->phd_port
== inp
->inp_lport
)
1045 * If none exists, malloc one and tack it on.
1048 MALLOC(phd
, struct inpcbport
*, sizeof(struct inpcbport
), M_PCB
, M_WAITOK
);
1050 return (ENOBUFS
); /* XXX */
1052 phd
->phd_port
= inp
->inp_lport
;
1053 LIST_INIT(&phd
->phd_pcblist
);
1054 LIST_INSERT_HEAD(pcbporthash
, phd
, phd_hash
);
1057 LIST_INSERT_HEAD(&phd
->phd_pcblist
, inp
, inp_portlist
);
1058 LIST_INSERT_HEAD(pcbhash
, inp
, inp_hash
);
1060 inp
->hash_element
= INP_PCBHASH(inp
->inp_faddr
.s_addr
, inp
->inp_lport
,
1061 inp
->inp_fport
, pcbinfo
->hashmask
);
1067 * Move PCB to the proper hash bucket when { faddr, fport } have been
1068 * changed. NOTE: This does not handle the case of the lport changing (the
1069 * hashed port list would have to be updated as well), so the lport must
1070 * not change after in_pcbinshash() has been called.
1076 struct inpcbhead
*head
;
1077 u_int32_t hashkey_faddr
;
1080 if (inp
->inp_vflag
& INP_IPV6
)
1081 hashkey_faddr
= inp
->in6p_faddr
.s6_addr32
[3] /* XXX */;
1084 hashkey_faddr
= inp
->inp_faddr
.s_addr
;
1086 head
= &inp
->inp_pcbinfo
->hashbase
[INP_PCBHASH(hashkey_faddr
,
1087 inp
->inp_lport
, inp
->inp_fport
, inp
->inp_pcbinfo
->hashmask
)];
1089 LIST_REMOVE(inp
, inp_hash
);
1090 LIST_INSERT_HEAD(head
, inp
, inp_hash
);
1092 inp
->hash_element
= INP_PCBHASH(inp
->inp_faddr
.s_addr
, inp
->inp_lport
,
1093 inp
->inp_fport
, inp
->inp_pcbinfo
->hashmask
);
1098 * Remove PCB from various lists.
1104 inp
->inp_gencnt
= ++inp
->inp_pcbinfo
->ipi_gencnt
;
1106 if (inp
== inp
->inp_pcbinfo
->last_pcb
)
1107 inp
->inp_pcbinfo
->last_pcb
= 0;
1110 if (inp
->inp_lport
) {
1111 struct inpcbport
*phd
= inp
->inp_phd
;
1113 LIST_REMOVE(inp
, inp_hash
);
1114 LIST_REMOVE(inp
, inp_portlist
);
1115 if (LIST_FIRST(&phd
->phd_pcblist
) == NULL
) {
1116 LIST_REMOVE(phd
, phd_hash
);
1120 LIST_REMOVE(inp
, inp_list
);
1121 inp
->inp_pcbinfo
->ipi_count
--;
1125 in_pcb_grab_port
__P((struct inpcbinfo
*pcbinfo
,
1127 struct in_addr laddr
,
1129 struct in_addr faddr
,
1135 struct sockaddr_in sin
;
1136 struct proc
*p
= current_proc();
1140 pcbinfo
->nat_dummy_socket
.so_pcb
= 0;
1141 pcbinfo
->nat_dummy_socket
.so_options
= 0;
1143 /* The grabber wants a particular port */
1145 if (faddr
.s_addr
|| fport
) {
1147 * This is either the second half of an active connect, or
1148 * it's from the acceptance of an incoming connection.
1150 if (laddr
.s_addr
== 0) {
1154 if (in_pcblookup_hash(pcbinfo
, faddr
, fport
,
1155 laddr
, *lport
, 0, NULL
) != NULL
) {
1156 if (!(IN_MULTICAST(ntohl(laddr
.s_addr
)))) {
1157 return (EADDRINUSE
);
1161 stat
= in_pcballoc(&pcbinfo
->nat_dummy_socket
, pcbinfo
, p
);
1164 pcb
= sotoinpcb(&pcbinfo
->nat_dummy_socket
);
1165 pcb
->inp_vflag
|= INP_IPV4
;
1167 pcb
->inp_lport
= *lport
;
1168 pcb
->inp_laddr
.s_addr
= laddr
.s_addr
;
1170 pcb
->inp_faddr
= faddr
;
1171 pcb
->inp_fport
= fport
;
1176 * This is either a bind for a passive socket, or it's the
1177 * first part of bind-connect sequence (not likely since an
1178 * ephemeral port is usually used in this case). Or, it's
1179 * the result of a connection acceptance when the foreign
1180 * address/port cannot be provided (which requires the SO_REUSEADDR
1181 * flag if laddr is not multicast).
1184 stat
= in_pcballoc(&pcbinfo
->nat_dummy_socket
, pcbinfo
, p
);
1187 pcb
= sotoinpcb(&pcbinfo
->nat_dummy_socket
);
1188 pcb
->inp_vflag
|= INP_IPV4
;
1190 pcbinfo
->nat_dummy_socket
.so_options
= options
;
1191 bzero(&sin
, sizeof(struct sockaddr_in
));
1192 sin
.sin_len
= sizeof(struct sockaddr_in
);
1193 sin
.sin_family
= AF_INET
;
1194 sin
.sin_addr
.s_addr
= laddr
.s_addr
;
1195 sin
.sin_port
= *lport
;
1197 stat
= in_pcbbind((struct inpcb
*) pcbinfo
->nat_dummy_socket
.so_pcb
,
1198 (struct sockaddr
*) &sin
, p
);
1206 /* The grabber wants an ephemeral port */
1208 stat
= in_pcballoc(&pcbinfo
->nat_dummy_socket
, pcbinfo
, p
);
1211 pcb
= sotoinpcb(&pcbinfo
->nat_dummy_socket
);
1212 pcb
->inp_vflag
|= INP_IPV4
;
1214 bzero(&sin
, sizeof(struct sockaddr_in
));
1215 sin
.sin_len
= sizeof(struct sockaddr_in
);
1216 sin
.sin_family
= AF_INET
;
1217 sin
.sin_addr
.s_addr
= laddr
.s_addr
;
1220 if (faddr
.s_addr
|| fport
) {
1222 * Not sure if this case will be used - could occur when connect
1223 * is called, skipping the bind.
1226 if (laddr
.s_addr
== 0) {
1231 stat
= in_pcbbind((struct inpcb
*) pcbinfo
->nat_dummy_socket
.so_pcb
,
1232 (struct sockaddr
*) &sin
, p
);
1238 if (in_pcblookup_hash(pcbinfo
, faddr
, fport
,
1239 pcb
->inp_laddr
, pcb
->inp_lport
, 0, NULL
) != NULL
) {
1241 return (EADDRINUSE
);
1244 pcb
->inp_faddr
= faddr
;
1245 pcb
->inp_fport
= fport
;
1250 * This is a simple bind of an ephemeral port. The local addr
1251 * may or may not be defined.
1254 stat
= in_pcbbind((struct inpcb
*) pcbinfo
->nat_dummy_socket
.so_pcb
,
1255 (struct sockaddr
*) &sin
, p
);
1261 *lport
= pcb
->inp_lport
;
1265 pcb
->nat_owner
= owner_id
;
1266 pcb
->nat_cookie
= cookie
;
1267 pcb
->inp_ppcb
= (caddr_t
) pcbinfo
->dummy_cb
;
1272 in_pcb_letgo_port
__P((struct inpcbinfo
*pcbinfo
, struct in_addr laddr
, u_short lport
,
1273 struct in_addr faddr
, u_short fport
, u_char owner_id
))
1275 struct inpcbhead
*head
;
1276 register struct inpcb
*inp
;
1280 * First look for an exact match.
1282 head
= &pcbinfo
->hashbase
[INP_PCBHASH(faddr
.s_addr
, lport
, fport
, pcbinfo
->hashmask
)];
1283 for (inp
= head
->lh_first
; inp
!= NULL
; inp
= inp
->inp_hash
.le_next
) {
1284 if (inp
->inp_faddr
.s_addr
== faddr
.s_addr
&&
1285 inp
->inp_laddr
.s_addr
== laddr
.s_addr
&&
1286 inp
->inp_fport
== fport
&&
1287 inp
->inp_lport
== lport
&&
1288 inp
->nat_owner
== owner_id
) {
1301 in_pcb_get_owner(struct inpcbinfo
*pcbinfo
,
1302 struct in_addr laddr
, u_short lport
,
1303 struct in_addr faddr
, u_short fport
,
1308 u_char owner_id
= INPCB_NO_OWNER
;
1309 struct inpcbport
*phd
;
1310 struct inpcbporthead
*porthash
;
1313 if (IN_MULTICAST(laddr
.s_addr
)) {
1315 * Walk through PCB's looking for registered
1319 porthash
= &pcbinfo
->porthashbase
[INP_PCBPORTHASH(lport
,
1320 pcbinfo
->porthashmask
)];
1321 for (phd
= porthash
->lh_first
; phd
!= NULL
; phd
= phd
->phd_hash
.le_next
) {
1322 if (phd
->phd_port
== lport
)
1327 return INPCB_NO_OWNER
;
1330 owner_id
= INPCB_NO_OWNER
;
1331 for (inp
= phd
->phd_pcblist
.lh_first
; inp
!= NULL
;
1332 inp
= inp
->inp_portlist
.le_next
) {
1334 if (inp
->inp_laddr
.s_addr
== laddr
.s_addr
) {
1335 if (inp
->nat_owner
== 0)
1336 owner_id
|= INPCB_OWNED_BY_X
;
1338 owner_id
|= inp
->nat_owner
;
1345 inp
= in_pcblookup_hash(pcbinfo
, faddr
, fport
,
1346 laddr
, lport
, 1, NULL
);
1348 if (inp
->nat_owner
) {
1349 owner_id
= inp
->nat_owner
;
1350 *cookie
= inp
->nat_cookie
;
1353 pcbinfo
->last_pcb
= inp
;
1354 owner_id
= INPCB_OWNED_BY_X
;
1358 owner_id
= INPCB_NO_OWNER
;
1365 in_pcb_new_share_client(struct inpcbinfo
*pcbinfo
, u_char
*owner_id
)
1371 for (i
=0; i
< INPCB_MAX_IDS
; i
++) {
1372 if ((pcbinfo
->all_owners
& (1 << i
)) == 0) {
1373 pcbinfo
->all_owners
|= (1 << i
);
1374 *owner_id
= (1 << i
);
1383 in_pcb_rem_share_client(struct inpcbinfo
*pcbinfo
, u_char owner_id
)
1388 if (pcbinfo
->all_owners
& owner_id
) {
1389 pcbinfo
->all_owners
&= ~owner_id
;
1390 for (inp
= pcbinfo
->listhead
->lh_first
; inp
!= NULL
; inp
= inp
->inp_list
.le_next
) {
1391 if (inp
->nat_owner
& owner_id
) {
1392 if (inp
->nat_owner
== owner_id
)
1394 * Deallocate the pcb
1398 inp
->nat_owner
&= ~owner_id
;
1411 void in_pcb_nat_init(struct inpcbinfo
*pcbinfo
, int afamily
,
1412 int pfamily
, int protocol
)
1414 bzero(&pcbinfo
->nat_dummy_socket
, sizeof(struct socket
));
1415 pcbinfo
->nat_dummy_socket
.so_proto
= pffindproto(afamily
, pfamily
, protocol
);
1416 pcbinfo
->all_owners
= 0;
1421 prison_xinpcb(struct proc
*p
, struct inpcb
*inp
)
1425 if (ntohl(inp
->inp_laddr
.s_addr
) == p
->p_prison
->pr_ip
)