2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1982, 1986, 1991, 1993, 1995
27 * The Regents of the University of California. All rights reserved.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. All advertising materials mentioning features or use of this software
38 * must display the following acknowledgement:
39 * This product includes software developed by the University of
40 * California, Berkeley and its contributors.
41 * 4. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 * @(#)in_pcb.c 8.4 (Berkeley) 5/24/95
58 * $FreeBSD: src/sys/netinet/in_pcb.c,v 1.59.2.17 2001/08/13 16:26:17 ume Exp $
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/malloc.h>
65 #include <sys/domain.h>
66 #include <sys/protosw.h>
67 #include <sys/socket.h>
68 #include <sys/socketvar.h>
73 #include <sys/kernel.h>
74 #include <sys/sysctl.h>
76 #include <machine/limits.h>
79 #include <kern/zalloc.h>
83 #include <net/if_types.h>
84 #include <net/route.h>
86 #include <netinet/in.h>
87 #include <netinet/in_pcb.h>
88 #include <netinet/in_var.h>
89 #include <netinet/ip_var.h>
91 #include <netinet/ip6.h>
92 #include <netinet6/ip6_var.h>
98 #include <netinet6/ipsec.h>
99 #include <netkey/key.h>
102 #include <sys/kdebug.h>
105 extern int ipsec_bypass
;
108 #define DBG_FNC_PCB_LOOKUP NETDBG_CODE(DBG_NETTCP, (6 << 8))
109 #define DBG_FNC_PCB_HLOOKUP NETDBG_CODE(DBG_NETTCP, ((6 << 8) | 1))
111 struct in_addr zeroin_addr
;
114 * These configure the range of local port addresses assigned to
115 * "unspecified" outgoing connections/packets/whatever.
117 int ipport_lowfirstauto
= IPPORT_RESERVED
- 1; /* 1023 */
118 int ipport_lowlastauto
= IPPORT_RESERVEDSTART
; /* 600 */
120 int ipport_firstauto
= IPPORT_RESERVED
; /* 1024 */
121 int ipport_lastauto
= IPPORT_USERRESERVED
; /* 5000 */
123 int ipport_firstauto
= IPPORT_HIFIRSTAUTO
; /* 49152 */
124 int ipport_lastauto
= IPPORT_HILASTAUTO
; /* 65535 */
126 int ipport_hifirstauto
= IPPORT_HIFIRSTAUTO
; /* 49152 */
127 int ipport_hilastauto
= IPPORT_HILASTAUTO
; /* 65535 */
129 #define RANGECHK(var, min, max) \
130 if ((var) < (min)) { (var) = (min); } \
131 else if ((var) > (max)) { (var) = (max); }
134 sysctl_net_ipport_check SYSCTL_HANDLER_ARGS
136 int error
= sysctl_handle_int(oidp
,
137 oidp
->oid_arg1
, oidp
->oid_arg2
, req
);
139 RANGECHK(ipport_lowfirstauto
, 1, IPPORT_RESERVED
- 1);
140 RANGECHK(ipport_lowlastauto
, 1, IPPORT_RESERVED
- 1);
141 RANGECHK(ipport_firstauto
, IPPORT_RESERVED
, USHRT_MAX
);
142 RANGECHK(ipport_lastauto
, IPPORT_RESERVED
, USHRT_MAX
);
143 RANGECHK(ipport_hifirstauto
, IPPORT_RESERVED
, USHRT_MAX
);
144 RANGECHK(ipport_hilastauto
, IPPORT_RESERVED
, USHRT_MAX
);
151 SYSCTL_NODE(_net_inet_ip
, IPPROTO_IP
, portrange
, CTLFLAG_RW
, 0, "IP Ports");
153 SYSCTL_PROC(_net_inet_ip_portrange
, OID_AUTO
, lowfirst
, CTLTYPE_INT
|CTLFLAG_RW
,
154 &ipport_lowfirstauto
, 0, &sysctl_net_ipport_check
, "I", "");
155 SYSCTL_PROC(_net_inet_ip_portrange
, OID_AUTO
, lowlast
, CTLTYPE_INT
|CTLFLAG_RW
,
156 &ipport_lowlastauto
, 0, &sysctl_net_ipport_check
, "I", "");
157 SYSCTL_PROC(_net_inet_ip_portrange
, OID_AUTO
, first
, CTLTYPE_INT
|CTLFLAG_RW
,
158 &ipport_firstauto
, 0, &sysctl_net_ipport_check
, "I", "");
159 SYSCTL_PROC(_net_inet_ip_portrange
, OID_AUTO
, last
, CTLTYPE_INT
|CTLFLAG_RW
,
160 &ipport_lastauto
, 0, &sysctl_net_ipport_check
, "I", "");
161 SYSCTL_PROC(_net_inet_ip_portrange
, OID_AUTO
, hifirst
, CTLTYPE_INT
|CTLFLAG_RW
,
162 &ipport_hifirstauto
, 0, &sysctl_net_ipport_check
, "I", "");
163 SYSCTL_PROC(_net_inet_ip_portrange
, OID_AUTO
, hilast
, CTLTYPE_INT
|CTLFLAG_RW
,
164 &ipport_hilastauto
, 0, &sysctl_net_ipport_check
, "I", "");
167 * in_pcb.c: manage the Protocol Control Blocks.
169 * NOTE: It is assumed that most of these functions will be called at
170 * splnet(). XXX - There are, unfortunately, a few exceptions to this
171 * rule that should be fixed.
175 * Allocate a PCB and associate it with the socket.
178 in_pcballoc(so
, pcbinfo
, p
)
180 struct inpcbinfo
*pcbinfo
;
183 register struct inpcb
*inp
;
189 if (so
->cached_in_sock_layer
== 0) {
191 printf("PCBALLOC calling zalloc for socket %x\n", so
);
193 inp
= (struct inpcb
*) zalloc(pcbinfo
->ipi_zone
);
196 bzero((caddr_t
)inp
, sizeof(*inp
));
200 printf("PCBALLOC reusing PCB for socket %x\n", so
);
202 inp
= (struct inpcb
*) so
->so_saved_pcb
;
203 temp
= inp
->inp_saved_ppcb
;
204 bzero((caddr_t
) inp
, sizeof(*inp
));
205 inp
->inp_saved_ppcb
= temp
;
208 inp
->inp_gencnt
= ++pcbinfo
->ipi_gencnt
;
209 inp
->inp_pcbinfo
= pcbinfo
;
210 inp
->inp_socket
= so
;
213 if (ipsec_bypass
== 0) {
214 error
= ipsec_init_policy(so
, &inp
->inp_sp
);
216 zfree(pcbinfo
->ipi_zone
, (vm_offset_t
)inp
);
223 if (INP_SOCKAF(so
) == AF_INET6
&& !ip6_mapped_addr_on
)
224 inp
->inp_flags
|= IN6P_IPV6_V6ONLY
;
226 LIST_INSERT_HEAD(pcbinfo
->listhead
, inp
, inp_list
);
227 pcbinfo
->ipi_count
++;
228 so
->so_pcb
= (caddr_t
)inp
;
230 if (ip6_auto_flowlabel
)
231 inp
->inp_flags
|= IN6P_AUTOFLOWLABEL
;
237 in_pcbbind(inp
, nam
, p
)
238 register struct inpcb
*inp
;
239 struct sockaddr
*nam
;
242 register struct socket
*so
= inp
->inp_socket
;
243 unsigned short *lastport
;
244 struct sockaddr_in
*sin
;
245 struct inpcbinfo
*pcbinfo
= inp
->inp_pcbinfo
;
247 int wild
= 0, reuseport
= (so
->so_options
& SO_REUSEPORT
);
250 if (TAILQ_EMPTY(&in_ifaddrhead
)) /* XXX broken! */
251 return (EADDRNOTAVAIL
);
252 if (inp
->inp_lport
|| inp
->inp_laddr
.s_addr
!= INADDR_ANY
)
254 if ((so
->so_options
& (SO_REUSEADDR
|SO_REUSEPORT
)) == 0)
257 sin
= (struct sockaddr_in
*)nam
;
258 if (nam
->sa_len
!= sizeof (*sin
))
262 * We should check the family, but old programs
263 * incorrectly fail to initialize it.
265 if (sin
->sin_family
!= AF_INET
)
266 return (EAFNOSUPPORT
);
268 lport
= sin
->sin_port
;
269 if (IN_MULTICAST(ntohl(sin
->sin_addr
.s_addr
))) {
271 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
272 * allow complete duplication of binding if
273 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
274 * and a multicast address is bound on both
275 * new and duplicated sockets.
277 if (so
->so_options
& SO_REUSEADDR
)
278 reuseport
= SO_REUSEADDR
|SO_REUSEPORT
;
279 } else if (sin
->sin_addr
.s_addr
!= INADDR_ANY
) {
280 sin
->sin_port
= 0; /* yech... */
281 if (ifa_ifwithaddr((struct sockaddr
*)sin
) == 0)
282 return (EADDRNOTAVAIL
);
288 if (ntohs(lport
) < IPPORT_RESERVED
&& p
&&
289 suser(p
->p_ucred
, &p
->p_acflag
))
292 !IN_MULTICAST(ntohl(sin
->sin_addr
.s_addr
))) {
293 t
= in_pcblookup_local(inp
->inp_pcbinfo
,
294 sin
->sin_addr
, lport
, INPLOOKUP_WILDCARD
);
296 (ntohl(sin
->sin_addr
.s_addr
) != INADDR_ANY
||
297 ntohl(t
->inp_laddr
.s_addr
) != INADDR_ANY
||
298 (t
->inp_socket
->so_options
&
299 SO_REUSEPORT
) == 0) &&
300 (so
->so_uid
!= t
->inp_socket
->so_uid
)) {
302 if (ntohl(sin
->sin_addr
.s_addr
) !=
304 ntohl(t
->inp_laddr
.s_addr
) !=
307 INP_SOCKAF(t
->inp_socket
))
308 #endif /* defined(INET6) */
312 t
= in_pcblookup_local(pcbinfo
, sin
->sin_addr
,
315 (reuseport
& t
->inp_socket
->so_options
) == 0) {
317 if (ip6_mapped_addr_on
== 0 ||
318 ntohl(sin
->sin_addr
.s_addr
) !=
320 ntohl(t
->inp_laddr
.s_addr
) !=
323 INP_SOCKAF(t
->inp_socket
))
324 #endif /* defined(INET6) */
328 inp
->inp_laddr
= sin
->sin_addr
;
334 inp
->inp_flags
|= INP_ANONPORT
;
336 if (inp
->inp_flags
& INP_HIGHPORT
) {
337 first
= ipport_hifirstauto
; /* sysctl */
338 last
= ipport_hilastauto
;
339 lastport
= &pcbinfo
->lasthi
;
340 } else if (inp
->inp_flags
& INP_LOWPORT
) {
341 if (p
&& (error
= suser(p
->p_ucred
, &p
->p_acflag
)))
343 first
= ipport_lowfirstauto
; /* 1023 */
344 last
= ipport_lowlastauto
; /* 600 */
345 lastport
= &pcbinfo
->lastlow
;
347 first
= ipport_firstauto
; /* sysctl */
348 last
= ipport_lastauto
;
349 lastport
= &pcbinfo
->lastport
;
352 * Simple check to ensure all ports are not used up causing
355 * We split the two cases (up and down) so that the direction
356 * is not being tested on each round of the loop.
362 count
= first
- last
;
365 if (count
-- < 0) { /* completely used? */
366 inp
->inp_laddr
.s_addr
= INADDR_ANY
;
367 return (EADDRNOTAVAIL
);
370 if (*lastport
> first
|| *lastport
< last
)
372 lport
= htons(*lastport
);
373 } while (in_pcblookup_local(pcbinfo
,
374 inp
->inp_laddr
, lport
, wild
));
379 count
= last
- first
;
382 if (count
-- < 0) { /* completely used? */
383 inp
->inp_laddr
.s_addr
= INADDR_ANY
;
384 return (EADDRNOTAVAIL
);
387 if (*lastport
< first
|| *lastport
> last
)
389 lport
= htons(*lastport
);
390 } while (in_pcblookup_local(pcbinfo
,
391 inp
->inp_laddr
, lport
, wild
));
394 inp
->inp_lport
= lport
;
395 if (in_pcbinshash(inp
) != 0) {
396 inp
->inp_laddr
.s_addr
= INADDR_ANY
;
404 * Transform old in_pcbconnect() into an inner subroutine for new
405 * in_pcbconnect(): Do some validity-checking on the remote
406 * address (in mbuf 'nam') and then determine local host address
407 * (i.e., which interface) to use to access that remote host.
409 * This preserves definition of in_pcbconnect(), while supporting a
410 * slightly different version for T/TCP. (This is more than
411 * a bit of a kludge, but cleaning up the internal interfaces would
412 * have forced minor changes in every protocol).
416 in_pcbladdr(inp
, nam
, plocal_sin
)
417 register struct inpcb
*inp
;
418 struct sockaddr
*nam
;
419 struct sockaddr_in
**plocal_sin
;
421 struct in_ifaddr
*ia
;
422 register struct sockaddr_in
*sin
= (struct sockaddr_in
*)nam
;
424 if (nam
->sa_len
!= sizeof (*sin
))
426 if (sin
->sin_family
!= AF_INET
)
427 return (EAFNOSUPPORT
);
428 if (sin
->sin_port
== 0)
429 return (EADDRNOTAVAIL
);
430 if (!TAILQ_EMPTY(&in_ifaddrhead
)) {
432 * If the destination address is INADDR_ANY,
433 * use the primary local address.
434 * If the supplied address is INADDR_BROADCAST,
435 * and the primary interface supports broadcast,
436 * choose the broadcast address for that interface.
438 #define satosin(sa) ((struct sockaddr_in *)(sa))
439 #define sintosa(sin) ((struct sockaddr *)(sin))
440 #define ifatoia(ifa) ((struct in_ifaddr *)(ifa))
441 if (sin
->sin_addr
.s_addr
== INADDR_ANY
)
442 sin
->sin_addr
= IA_SIN(TAILQ_FIRST(&in_ifaddrhead
))->sin_addr
;
443 else if (sin
->sin_addr
.s_addr
== (u_long
)INADDR_BROADCAST
&&
444 (TAILQ_FIRST(&in_ifaddrhead
)->ia_ifp
->if_flags
& IFF_BROADCAST
))
445 sin
->sin_addr
= satosin(&TAILQ_FIRST(&in_ifaddrhead
)->ia_broadaddr
)->sin_addr
;
447 if (inp
->inp_laddr
.s_addr
== INADDR_ANY
) {
448 register struct route
*ro
;
450 ia
= (struct in_ifaddr
*)0;
452 * If route is known or can be allocated now,
453 * our src addr is taken from the i/f, else punt.
455 ro
= &inp
->inp_route
;
457 (satosin(&ro
->ro_dst
)->sin_addr
.s_addr
!=
458 sin
->sin_addr
.s_addr
||
459 inp
->inp_socket
->so_options
& SO_DONTROUTE
)) {
461 ro
->ro_rt
= (struct rtentry
*)0;
463 if ((inp
->inp_socket
->so_options
& SO_DONTROUTE
) == 0 && /*XXX*/
464 (ro
->ro_rt
== (struct rtentry
*)0 ||
465 ro
->ro_rt
->rt_ifp
== (struct ifnet
*)0)) {
466 /* No route yet, so try to acquire one */
467 ro
->ro_dst
.sa_family
= AF_INET
;
468 ro
->ro_dst
.sa_len
= sizeof(struct sockaddr_in
);
469 ((struct sockaddr_in
*) &ro
->ro_dst
)->sin_addr
=
474 * If we found a route, use the address
475 * corresponding to the outgoing interface
476 * unless it is the loopback (in case a route
477 * to our address on another net goes to loopback).
479 if (ro
->ro_rt
&& !(ro
->ro_rt
->rt_ifp
->if_flags
& IFF_LOOPBACK
))
480 ia
= ifatoia(ro
->ro_rt
->rt_ifa
);
482 u_short fport
= sin
->sin_port
;
485 ia
= ifatoia(ifa_ifwithdstaddr(sintosa(sin
)));
487 ia
= ifatoia(ifa_ifwithnet(sintosa(sin
)));
488 sin
->sin_port
= fport
;
490 ia
= TAILQ_FIRST(&in_ifaddrhead
);
492 return (EADDRNOTAVAIL
);
495 * If the destination address is multicast and an outgoing
496 * interface has been set as a multicast option, use the
497 * address of that interface as our source address.
499 if (IN_MULTICAST(ntohl(sin
->sin_addr
.s_addr
)) &&
500 inp
->inp_moptions
!= NULL
) {
501 struct ip_moptions
*imo
;
504 imo
= inp
->inp_moptions
;
505 if (imo
->imo_multicast_ifp
!= NULL
) {
506 ifp
= imo
->imo_multicast_ifp
;
507 TAILQ_FOREACH(ia
, &in_ifaddrhead
, ia_link
)
508 if (ia
->ia_ifp
== ifp
)
511 return (EADDRNOTAVAIL
);
515 * Don't do pcblookup call here; return interface in plocal_sin
516 * and exit to caller, that will do the lookup.
518 *plocal_sin
= &ia
->ia_addr
;
526 * Connect from a socket to a specified address.
527 * Both address and port must be specified in argument sin.
528 * If don't have a local address for this socket yet,
532 in_pcbconnect(inp
, nam
, p
)
533 register struct inpcb
*inp
;
534 struct sockaddr
*nam
;
537 struct sockaddr_in
*ifaddr
;
538 struct sockaddr_in
*sin
= (struct sockaddr_in
*)nam
;
539 struct sockaddr_in sa
;
543 * Call inner routine, to assign local interface address.
545 if ((error
= in_pcbladdr(inp
, nam
, &ifaddr
)) != 0)
548 if (in_pcblookup_hash(inp
->inp_pcbinfo
, sin
->sin_addr
, sin
->sin_port
,
549 inp
->inp_laddr
.s_addr
? inp
->inp_laddr
: ifaddr
->sin_addr
,
550 inp
->inp_lport
, 0, NULL
) != NULL
) {
553 if (inp
->inp_laddr
.s_addr
== INADDR_ANY
) {
554 if (inp
->inp_lport
== 0) {
555 error
= in_pcbbind(inp
, (struct sockaddr
*)0, p
);
559 inp
->inp_laddr
= ifaddr
->sin_addr
;
561 inp
->inp_faddr
= sin
->sin_addr
;
562 inp
->inp_fport
= sin
->sin_port
;
568 in_pcbdisconnect(inp
)
572 inp
->inp_faddr
.s_addr
= INADDR_ANY
;
575 if (inp
->inp_socket
->so_state
& SS_NOFDREF
)
583 struct socket
*so
= inp
->inp_socket
;
584 struct inpcbinfo
*ipi
= inp
->inp_pcbinfo
;
585 struct rtentry
*rt
= inp
->inp_route
.ro_rt
;
588 ipsec4_delete_pcbpolicy(inp
);
590 inp
->inp_gencnt
= ++ipi
->ipi_gencnt
;
594 if (so
->cached_in_sock_layer
)
595 printf("PCB_DETACH for cached socket %x\n", so
);
597 printf("PCB_DETACH for allocated socket %x\n", so
);
602 if (inp
->inp_options
)
603 (void)m_free(inp
->inp_options
);
606 * route deletion requires reference count to be <= zero
608 if ((rt
->rt_flags
& RTF_DELCLONE
) &&
609 (rt
->rt_flags
& RTF_WASCLONED
) &&
610 (rt
->rt_refcnt
<= 1)) {
612 rt
->rt_flags
&= ~RTF_UP
;
613 rtrequest(RTM_DELETE
, rt_key(rt
),
614 rt
->rt_gateway
, rt_mask(rt
),
615 rt
->rt_flags
, (struct rtentry
**)0);
620 ip_freemoptions(inp
->inp_moptions
);
622 if (so
->cached_in_sock_layer
)
623 so
->so_saved_pcb
= (caddr_t
) inp
;
625 zfree(ipi
->ipi_zone
, (vm_offset_t
) inp
);
631 * The calling convention of in_setsockaddr() and in_setpeeraddr() was
632 * modified to match the pru_sockaddr() and pru_peeraddr() entry points
633 * in struct pr_usrreqs, so that protocols can just reference then directly
634 * without the need for a wrapper function. The socket must have a valid
635 * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
636 * except through a kernel programming error, so it is acceptable to panic
637 * (or in this case trap) if the PCB is invalid. (Actually, we don't trap
638 * because there actually /is/ a programming error somewhere... XXX)
641 in_setsockaddr(so
, nam
)
643 struct sockaddr
**nam
;
646 register struct inpcb
*inp
;
647 register struct sockaddr_in
*sin
;
650 * Do the malloc first in case it blocks.
652 MALLOC(sin
, struct sockaddr_in
*, sizeof *sin
, M_SONAME
, M_WAITOK
);
655 bzero(sin
, sizeof *sin
);
656 sin
->sin_family
= AF_INET
;
657 sin
->sin_len
= sizeof(*sin
);
666 sin
->sin_port
= inp
->inp_lport
;
667 sin
->sin_addr
= inp
->inp_laddr
;
670 *nam
= (struct sockaddr
*)sin
;
675 in_setpeeraddr(so
, nam
)
677 struct sockaddr
**nam
;
681 register struct sockaddr_in
*sin
;
684 * Do the malloc first in case it blocks.
686 MALLOC(sin
, struct sockaddr_in
*, sizeof *sin
, M_SONAME
, M_WAITOK
);
689 bzero((caddr_t
)sin
, sizeof (*sin
));
690 sin
->sin_family
= AF_INET
;
691 sin
->sin_len
= sizeof(*sin
);
700 sin
->sin_port
= inp
->inp_fport
;
701 sin
->sin_addr
= inp
->inp_faddr
;
704 *nam
= (struct sockaddr
*)sin
;
709 in_pcbnotifyall(head
, faddr
, errno
, notify
)
710 struct inpcbhead
*head
;
711 struct in_addr faddr
;
712 void (*notify
) __P((struct inpcb
*, int));
714 struct inpcb
*inp
, *ninp
;
718 for (inp
= LIST_FIRST(head
); inp
!= NULL
; inp
= ninp
) {
719 ninp
= LIST_NEXT(inp
, inp_list
);
721 if ((inp
->inp_vflag
& INP_IPV4
) == 0)
724 if (inp
->inp_faddr
.s_addr
!= faddr
.s_addr
||
725 inp
->inp_socket
== NULL
)
727 (*notify
)(inp
, errno
);
733 in_pcbpurgeif0(head
, ifp
)
738 struct ip_moptions
*imo
;
741 for (inp
= head
; inp
!= NULL
; inp
= LIST_NEXT(inp
, inp_list
)) {
742 imo
= inp
->inp_moptions
;
743 if ((inp
->inp_vflag
& INP_IPV4
) &&
746 * Unselect the outgoing interface if it is being
749 if (imo
->imo_multicast_ifp
== ifp
)
750 imo
->imo_multicast_ifp
= NULL
;
753 * Drop multicast group membership if we joined
754 * through the interface being detached.
756 for (i
= 0, gap
= 0; i
< imo
->imo_num_memberships
;
758 if (imo
->imo_membership
[i
]->inm_ifp
== ifp
) {
759 in_delmulti(imo
->imo_membership
[i
]);
762 imo
->imo_membership
[i
- gap
] =
763 imo
->imo_membership
[i
];
765 imo
->imo_num_memberships
-= gap
;
771 * Check for alternatives when higher level complains
772 * about service problems. For now, invalidate cached
773 * routing information. If the route was created dynamically
774 * (by a redirect), time to try a default gateway again.
780 register struct rtentry
*rt
;
781 struct rt_addrinfo info
;
783 if ((rt
= inp
->inp_route
.ro_rt
)) {
784 bzero((caddr_t
)&info
, sizeof(info
));
785 info
.rti_info
[RTAX_DST
] =
786 (struct sockaddr
*)&inp
->inp_route
.ro_dst
;
787 info
.rti_info
[RTAX_GATEWAY
] = rt
->rt_gateway
;
788 info
.rti_info
[RTAX_NETMASK
] = rt_mask(rt
);
789 rt_missmsg(RTM_LOSING
, &info
, rt
->rt_flags
, 0);
790 if (rt
->rt_flags
& RTF_DYNAMIC
)
791 (void) rtrequest(RTM_DELETE
, rt_key(rt
),
792 rt
->rt_gateway
, rt_mask(rt
), rt
->rt_flags
,
793 (struct rtentry
**)0);
794 inp
->inp_route
.ro_rt
= 0;
797 * A new route can be allocated
798 * the next time output is attempted.
804 * After a routing change, flush old routing
805 * and allocate a (hopefully) better one.
808 in_rtchange(inp
, errno
)
809 register struct inpcb
*inp
;
812 if (inp
->inp_route
.ro_rt
) {
813 rtfree(inp
->inp_route
.ro_rt
);
814 inp
->inp_route
.ro_rt
= 0;
816 * A new route can be allocated the next time
817 * output is attempted.
823 * Lookup a PCB based on the local address and port.
826 in_pcblookup_local(pcbinfo
, laddr
, lport_arg
, wild_okay
)
827 struct inpcbinfo
*pcbinfo
;
828 struct in_addr laddr
;
832 register struct inpcb
*inp
;
833 int matchwild
= 3, wildcard
;
834 u_short lport
= lport_arg
;
836 KERNEL_DEBUG(DBG_FNC_PCB_LOOKUP
| DBG_FUNC_START
, 0,0,0,0,0);
839 struct inpcbhead
*head
;
841 * Look for an unconnected (wildcard foreign addr) PCB that
842 * matches the local address and port we're looking for.
844 head
= &pcbinfo
->hashbase
[INP_PCBHASH(INADDR_ANY
, lport
, 0, pcbinfo
->hashmask
)];
845 LIST_FOREACH(inp
, head
, inp_hash
) {
847 if ((inp
->inp_vflag
& INP_IPV4
) == 0)
850 if (inp
->inp_faddr
.s_addr
== INADDR_ANY
&&
851 inp
->inp_laddr
.s_addr
== laddr
.s_addr
&&
852 inp
->inp_lport
== lport
) {
862 KERNEL_DEBUG(DBG_FNC_PCB_LOOKUP
| DBG_FUNC_END
, 0,0,0,0,0);
865 struct inpcbporthead
*porthash
;
866 struct inpcbport
*phd
;
867 struct inpcb
*match
= NULL
;
869 * Best fit PCB lookup.
871 * First see if this local port is in use by looking on the
874 porthash
= &pcbinfo
->porthashbase
[INP_PCBPORTHASH(lport
,
875 pcbinfo
->porthashmask
)];
876 LIST_FOREACH(phd
, porthash
, phd_hash
) {
877 if (phd
->phd_port
== lport
)
882 * Port is in use by one or more PCBs. Look for best
885 LIST_FOREACH(inp
, &phd
->phd_pcblist
, inp_portlist
) {
888 if ((inp
->inp_vflag
& INP_IPV4
) == 0)
891 if (inp
->inp_faddr
.s_addr
!= INADDR_ANY
)
893 if (inp
->inp_laddr
.s_addr
!= INADDR_ANY
) {
894 if (laddr
.s_addr
== INADDR_ANY
)
896 else if (inp
->inp_laddr
.s_addr
!= laddr
.s_addr
)
899 if (laddr
.s_addr
!= INADDR_ANY
)
902 if (wildcard
< matchwild
) {
904 matchwild
= wildcard
;
905 if (matchwild
== 0) {
911 KERNEL_DEBUG(DBG_FNC_PCB_LOOKUP
| DBG_FUNC_END
, match
,0,0,0,0);
917 * Lookup PCB in hash list.
920 in_pcblookup_hash(pcbinfo
, faddr
, fport_arg
, laddr
, lport_arg
, wildcard
,
922 struct inpcbinfo
*pcbinfo
;
923 struct in_addr faddr
, laddr
;
924 u_int fport_arg
, lport_arg
;
928 struct inpcbhead
*head
;
929 register struct inpcb
*inp
;
930 u_short fport
= fport_arg
, lport
= lport_arg
;
933 * We may have found the pcb in the last lookup - check this first.
936 if ((!IN_MULTICAST(laddr
.s_addr
)) && (pcbinfo
->last_pcb
)) {
937 if (faddr
.s_addr
== pcbinfo
->last_pcb
->inp_faddr
.s_addr
&&
938 laddr
.s_addr
== pcbinfo
->last_pcb
->inp_laddr
.s_addr
&&
939 fport_arg
== pcbinfo
->last_pcb
->inp_fport
&&
940 lport_arg
== pcbinfo
->last_pcb
->inp_lport
) {
944 return (pcbinfo
->last_pcb
);
947 pcbinfo
->last_pcb
= 0;
951 * First look for an exact match.
953 head
= &pcbinfo
->hashbase
[INP_PCBHASH(faddr
.s_addr
, lport
, fport
, pcbinfo
->hashmask
)];
954 LIST_FOREACH(inp
, head
, inp_hash
) {
956 if ((inp
->inp_vflag
& INP_IPV4
) == 0)
959 if (inp
->inp_faddr
.s_addr
== faddr
.s_addr
&&
960 inp
->inp_laddr
.s_addr
== laddr
.s_addr
&&
961 inp
->inp_fport
== fport
&&
962 inp
->inp_lport
== lport
) {
970 struct inpcb
*local_wild
= NULL
;
972 struct inpcb
*local_wild_mapped
= NULL
;
975 head
= &pcbinfo
->hashbase
[INP_PCBHASH(INADDR_ANY
, lport
, 0, pcbinfo
->hashmask
)];
976 LIST_FOREACH(inp
, head
, inp_hash
) {
978 if ((inp
->inp_vflag
& INP_IPV4
) == 0)
981 if (inp
->inp_faddr
.s_addr
== INADDR_ANY
&&
982 inp
->inp_lport
== lport
) {
983 #if defined(NFAITH) && NFAITH > 0
984 if (ifp
&& ifp
->if_type
== IFT_FAITH
&&
985 (inp
->inp_flags
& INP_FAITH
) == 0)
988 if (inp
->inp_laddr
.s_addr
== laddr
.s_addr
)
990 else if (inp
->inp_laddr
.s_addr
== INADDR_ANY
) {
992 if (INP_CHECK_SOCKAF(inp
->inp_socket
,
994 local_wild_mapped
= inp
;
996 #endif /* defined(INET6) */
1002 if (local_wild
== NULL
)
1003 return (local_wild_mapped
);
1004 #endif /* defined(INET6) */
1005 return (local_wild
);
1015 * Insert PCB onto various hash lists.
1021 struct inpcbhead
*pcbhash
;
1022 struct inpcbporthead
*pcbporthash
;
1023 struct inpcbinfo
*pcbinfo
= inp
->inp_pcbinfo
;
1024 struct inpcbport
*phd
;
1025 u_int32_t hashkey_faddr
;
1028 if (inp
->inp_vflag
& INP_IPV6
)
1029 hashkey_faddr
= inp
->in6p_faddr
.s6_addr32
[3] /* XXX */;
1032 hashkey_faddr
= inp
->inp_faddr
.s_addr
;
1034 pcbhash
= &pcbinfo
->hashbase
[INP_PCBHASH(hashkey_faddr
,
1035 inp
->inp_lport
, inp
->inp_fport
, pcbinfo
->hashmask
)];
1037 pcbporthash
= &pcbinfo
->porthashbase
[INP_PCBPORTHASH(inp
->inp_lport
,
1038 pcbinfo
->porthashmask
)];
1041 * Go through port list and look for a head for this lport.
1043 LIST_FOREACH(phd
, pcbporthash
, phd_hash
) {
1044 if (phd
->phd_port
== inp
->inp_lport
)
1048 * If none exists, malloc one and tack it on.
1051 MALLOC(phd
, struct inpcbport
*, sizeof(struct inpcbport
), M_PCB
, M_WAITOK
);
1053 return (ENOBUFS
); /* XXX */
1055 phd
->phd_port
= inp
->inp_lport
;
1056 LIST_INIT(&phd
->phd_pcblist
);
1057 LIST_INSERT_HEAD(pcbporthash
, phd
, phd_hash
);
1060 LIST_INSERT_HEAD(&phd
->phd_pcblist
, inp
, inp_portlist
);
1061 LIST_INSERT_HEAD(pcbhash
, inp
, inp_hash
);
1063 inp
->hash_element
= INP_PCBHASH(inp
->inp_faddr
.s_addr
, inp
->inp_lport
,
1064 inp
->inp_fport
, pcbinfo
->hashmask
);
1070 * Move PCB to the proper hash bucket when { faddr, fport } have been
1071 * changed. NOTE: This does not handle the case of the lport changing (the
1072 * hashed port list would have to be updated as well), so the lport must
1073 * not change after in_pcbinshash() has been called.
1079 struct inpcbhead
*head
;
1080 u_int32_t hashkey_faddr
;
1083 if (inp
->inp_vflag
& INP_IPV6
)
1084 hashkey_faddr
= inp
->in6p_faddr
.s6_addr32
[3] /* XXX */;
1087 hashkey_faddr
= inp
->inp_faddr
.s_addr
;
1089 head
= &inp
->inp_pcbinfo
->hashbase
[INP_PCBHASH(hashkey_faddr
,
1090 inp
->inp_lport
, inp
->inp_fport
, inp
->inp_pcbinfo
->hashmask
)];
1092 LIST_REMOVE(inp
, inp_hash
);
1093 LIST_INSERT_HEAD(head
, inp
, inp_hash
);
1095 inp
->hash_element
= INP_PCBHASH(inp
->inp_faddr
.s_addr
, inp
->inp_lport
,
1096 inp
->inp_fport
, inp
->inp_pcbinfo
->hashmask
);
1101 * Remove PCB from various lists.
1107 inp
->inp_gencnt
= ++inp
->inp_pcbinfo
->ipi_gencnt
;
1109 if (inp
== inp
->inp_pcbinfo
->last_pcb
)
1110 inp
->inp_pcbinfo
->last_pcb
= 0;
1113 if (inp
->inp_lport
) {
1114 struct inpcbport
*phd
= inp
->inp_phd
;
1116 LIST_REMOVE(inp
, inp_hash
);
1117 LIST_REMOVE(inp
, inp_portlist
);
1118 if (LIST_FIRST(&phd
->phd_pcblist
) == NULL
) {
1119 LIST_REMOVE(phd
, phd_hash
);
1123 LIST_REMOVE(inp
, inp_list
);
1124 inp
->inp_pcbinfo
->ipi_count
--;
1128 in_pcb_grab_port
__P((struct inpcbinfo
*pcbinfo
,
1130 struct in_addr laddr
,
1132 struct in_addr faddr
,
1138 struct sockaddr_in sin
;
1139 struct proc
*p
= current_proc();
1143 pcbinfo
->nat_dummy_socket
.so_pcb
= 0;
1144 pcbinfo
->nat_dummy_socket
.so_options
= 0;
1146 /* The grabber wants a particular port */
1148 if (faddr
.s_addr
|| fport
) {
1150 * This is either the second half of an active connect, or
1151 * it's from the acceptance of an incoming connection.
1153 if (laddr
.s_addr
== 0) {
1157 if (in_pcblookup_hash(pcbinfo
, faddr
, fport
,
1158 laddr
, *lport
, 0, NULL
) != NULL
) {
1159 if (!(IN_MULTICAST(ntohl(laddr
.s_addr
)))) {
1160 return (EADDRINUSE
);
1164 stat
= in_pcballoc(&pcbinfo
->nat_dummy_socket
, pcbinfo
, p
);
1167 pcb
= sotoinpcb(&pcbinfo
->nat_dummy_socket
);
1168 pcb
->inp_vflag
|= INP_IPV4
;
1170 pcb
->inp_lport
= *lport
;
1171 pcb
->inp_laddr
.s_addr
= laddr
.s_addr
;
1173 pcb
->inp_faddr
= faddr
;
1174 pcb
->inp_fport
= fport
;
1179 * This is either a bind for a passive socket, or it's the
1180 * first part of bind-connect sequence (not likely since an
1181 * ephemeral port is usually used in this case). Or, it's
1182 * the result of a connection acceptance when the foreign
1183 * address/port cannot be provided (which requires the SO_REUSEADDR
1184 * flag if laddr is not multicast).
1187 stat
= in_pcballoc(&pcbinfo
->nat_dummy_socket
, pcbinfo
, p
);
1190 pcb
= sotoinpcb(&pcbinfo
->nat_dummy_socket
);
1191 pcb
->inp_vflag
|= INP_IPV4
;
1193 pcbinfo
->nat_dummy_socket
.so_options
= options
;
1194 bzero(&sin
, sizeof(struct sockaddr_in
));
1195 sin
.sin_len
= sizeof(struct sockaddr_in
);
1196 sin
.sin_family
= AF_INET
;
1197 sin
.sin_addr
.s_addr
= laddr
.s_addr
;
1198 sin
.sin_port
= *lport
;
1200 stat
= in_pcbbind((struct inpcb
*) pcbinfo
->nat_dummy_socket
.so_pcb
,
1201 (struct sockaddr
*) &sin
, p
);
1209 /* The grabber wants an ephemeral port */
1211 stat
= in_pcballoc(&pcbinfo
->nat_dummy_socket
, pcbinfo
, p
);
1214 pcb
= sotoinpcb(&pcbinfo
->nat_dummy_socket
);
1215 pcb
->inp_vflag
|= INP_IPV4
;
1217 bzero(&sin
, sizeof(struct sockaddr_in
));
1218 sin
.sin_len
= sizeof(struct sockaddr_in
);
1219 sin
.sin_family
= AF_INET
;
1220 sin
.sin_addr
.s_addr
= laddr
.s_addr
;
1223 if (faddr
.s_addr
|| fport
) {
1225 * Not sure if this case will be used - could occur when connect
1226 * is called, skipping the bind.
1229 if (laddr
.s_addr
== 0) {
1234 stat
= in_pcbbind((struct inpcb
*) pcbinfo
->nat_dummy_socket
.so_pcb
,
1235 (struct sockaddr
*) &sin
, p
);
1241 if (in_pcblookup_hash(pcbinfo
, faddr
, fport
,
1242 pcb
->inp_laddr
, pcb
->inp_lport
, 0, NULL
) != NULL
) {
1244 return (EADDRINUSE
);
1247 pcb
->inp_faddr
= faddr
;
1248 pcb
->inp_fport
= fport
;
1253 * This is a simple bind of an ephemeral port. The local addr
1254 * may or may not be defined.
1257 stat
= in_pcbbind((struct inpcb
*) pcbinfo
->nat_dummy_socket
.so_pcb
,
1258 (struct sockaddr
*) &sin
, p
);
1264 *lport
= pcb
->inp_lport
;
1268 pcb
->nat_owner
= owner_id
;
1269 pcb
->nat_cookie
= cookie
;
1270 pcb
->inp_ppcb
= (caddr_t
) pcbinfo
->dummy_cb
;
1275 in_pcb_letgo_port
__P((struct inpcbinfo
*pcbinfo
, struct in_addr laddr
, u_short lport
,
1276 struct in_addr faddr
, u_short fport
, u_char owner_id
))
1278 struct inpcbhead
*head
;
1279 register struct inpcb
*inp
;
1283 * First look for an exact match.
1285 head
= &pcbinfo
->hashbase
[INP_PCBHASH(faddr
.s_addr
, lport
, fport
, pcbinfo
->hashmask
)];
1286 for (inp
= head
->lh_first
; inp
!= NULL
; inp
= inp
->inp_hash
.le_next
) {
1287 if (inp
->inp_faddr
.s_addr
== faddr
.s_addr
&&
1288 inp
->inp_laddr
.s_addr
== laddr
.s_addr
&&
1289 inp
->inp_fport
== fport
&&
1290 inp
->inp_lport
== lport
&&
1291 inp
->nat_owner
== owner_id
) {
1304 in_pcb_get_owner(struct inpcbinfo
*pcbinfo
,
1305 struct in_addr laddr
, u_short lport
,
1306 struct in_addr faddr
, u_short fport
,
1311 u_char owner_id
= INPCB_NO_OWNER
;
1312 struct inpcbport
*phd
;
1313 struct inpcbporthead
*porthash
;
1316 if (IN_MULTICAST(laddr
.s_addr
)) {
1318 * Walk through PCB's looking for registered
1322 porthash
= &pcbinfo
->porthashbase
[INP_PCBPORTHASH(lport
,
1323 pcbinfo
->porthashmask
)];
1324 for (phd
= porthash
->lh_first
; phd
!= NULL
; phd
= phd
->phd_hash
.le_next
) {
1325 if (phd
->phd_port
== lport
)
1330 return INPCB_NO_OWNER
;
1333 owner_id
= INPCB_NO_OWNER
;
1334 for (inp
= phd
->phd_pcblist
.lh_first
; inp
!= NULL
;
1335 inp
= inp
->inp_portlist
.le_next
) {
1337 if (inp
->inp_laddr
.s_addr
== laddr
.s_addr
) {
1338 if (inp
->nat_owner
== 0)
1339 owner_id
|= INPCB_OWNED_BY_X
;
1341 owner_id
|= inp
->nat_owner
;
1348 inp
= in_pcblookup_hash(pcbinfo
, faddr
, fport
,
1349 laddr
, lport
, 1, NULL
);
1351 if (inp
->nat_owner
) {
1352 owner_id
= inp
->nat_owner
;
1353 *cookie
= inp
->nat_cookie
;
1356 pcbinfo
->last_pcb
= inp
;
1357 owner_id
= INPCB_OWNED_BY_X
;
1361 owner_id
= INPCB_NO_OWNER
;
1368 in_pcb_new_share_client(struct inpcbinfo
*pcbinfo
, u_char
*owner_id
)
1374 for (i
=0; i
< INPCB_MAX_IDS
; i
++) {
1375 if ((pcbinfo
->all_owners
& (1 << i
)) == 0) {
1376 pcbinfo
->all_owners
|= (1 << i
);
1377 *owner_id
= (1 << i
);
1386 in_pcb_rem_share_client(struct inpcbinfo
*pcbinfo
, u_char owner_id
)
1391 if (pcbinfo
->all_owners
& owner_id
) {
1392 pcbinfo
->all_owners
&= ~owner_id
;
1393 for (inp
= pcbinfo
->listhead
->lh_first
; inp
!= NULL
; inp
= inp
->inp_list
.le_next
) {
1394 if (inp
->nat_owner
& owner_id
) {
1395 if (inp
->nat_owner
== owner_id
)
1397 * Deallocate the pcb
1401 inp
->nat_owner
&= ~owner_id
;
1414 void in_pcb_nat_init(struct inpcbinfo
*pcbinfo
, int afamily
,
1415 int pfamily
, int protocol
)
1417 bzero(&pcbinfo
->nat_dummy_socket
, sizeof(struct socket
));
1418 pcbinfo
->nat_dummy_socket
.so_proto
= pffindproto(afamily
, pfamily
, protocol
);
1419 pcbinfo
->all_owners
= 0;
1424 prison_xinpcb(struct proc
*p
, struct inpcb
*inp
)
1428 if (ntohl(inp
->inp_laddr
.s_addr
) == p
->p_prison
->pr_ip
)