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 extern u_long route_generation
;
107 #define DBG_FNC_PCB_LOOKUP NETDBG_CODE(DBG_NETTCP, (6 << 8))
108 #define DBG_FNC_PCB_HLOOKUP NETDBG_CODE(DBG_NETTCP, ((6 << 8) | 1))
110 struct in_addr zeroin_addr
;
113 * These configure the range of local port addresses assigned to
114 * "unspecified" outgoing connections/packets/whatever.
116 int ipport_lowfirstauto
= IPPORT_RESERVED
- 1; /* 1023 */
117 int ipport_lowlastauto
= IPPORT_RESERVEDSTART
; /* 600 */
119 int ipport_firstauto
= IPPORT_RESERVED
; /* 1024 */
120 int ipport_lastauto
= IPPORT_USERRESERVED
; /* 5000 */
122 int ipport_firstauto
= IPPORT_HIFIRSTAUTO
; /* 49152 */
123 int ipport_lastauto
= IPPORT_HILASTAUTO
; /* 65535 */
125 int ipport_hifirstauto
= IPPORT_HIFIRSTAUTO
; /* 49152 */
126 int ipport_hilastauto
= IPPORT_HILASTAUTO
; /* 65535 */
128 #define RANGECHK(var, min, max) \
129 if ((var) < (min)) { (var) = (min); } \
130 else if ((var) > (max)) { (var) = (max); }
133 sysctl_net_ipport_check SYSCTL_HANDLER_ARGS
135 int error
= sysctl_handle_int(oidp
,
136 oidp
->oid_arg1
, oidp
->oid_arg2
, req
);
138 RANGECHK(ipport_lowfirstauto
, 1, IPPORT_RESERVED
- 1);
139 RANGECHK(ipport_lowlastauto
, 1, IPPORT_RESERVED
- 1);
140 RANGECHK(ipport_firstauto
, IPPORT_RESERVED
, USHRT_MAX
);
141 RANGECHK(ipport_lastauto
, IPPORT_RESERVED
, USHRT_MAX
);
142 RANGECHK(ipport_hifirstauto
, IPPORT_RESERVED
, USHRT_MAX
);
143 RANGECHK(ipport_hilastauto
, IPPORT_RESERVED
, USHRT_MAX
);
150 SYSCTL_NODE(_net_inet_ip
, IPPROTO_IP
, portrange
, CTLFLAG_RW
, 0, "IP Ports");
152 SYSCTL_PROC(_net_inet_ip_portrange
, OID_AUTO
, lowfirst
, CTLTYPE_INT
|CTLFLAG_RW
,
153 &ipport_lowfirstauto
, 0, &sysctl_net_ipport_check
, "I", "");
154 SYSCTL_PROC(_net_inet_ip_portrange
, OID_AUTO
, lowlast
, CTLTYPE_INT
|CTLFLAG_RW
,
155 &ipport_lowlastauto
, 0, &sysctl_net_ipport_check
, "I", "");
156 SYSCTL_PROC(_net_inet_ip_portrange
, OID_AUTO
, first
, CTLTYPE_INT
|CTLFLAG_RW
,
157 &ipport_firstauto
, 0, &sysctl_net_ipport_check
, "I", "");
158 SYSCTL_PROC(_net_inet_ip_portrange
, OID_AUTO
, last
, CTLTYPE_INT
|CTLFLAG_RW
,
159 &ipport_lastauto
, 0, &sysctl_net_ipport_check
, "I", "");
160 SYSCTL_PROC(_net_inet_ip_portrange
, OID_AUTO
, hifirst
, CTLTYPE_INT
|CTLFLAG_RW
,
161 &ipport_hifirstauto
, 0, &sysctl_net_ipport_check
, "I", "");
162 SYSCTL_PROC(_net_inet_ip_portrange
, OID_AUTO
, hilast
, CTLTYPE_INT
|CTLFLAG_RW
,
163 &ipport_hilastauto
, 0, &sysctl_net_ipport_check
, "I", "");
166 * in_pcb.c: manage the Protocol Control Blocks.
168 * NOTE: It is assumed that most of these functions will be called at
169 * splnet(). XXX - There are, unfortunately, a few exceptions to this
170 * rule that should be fixed.
174 * Allocate a PCB and associate it with the socket.
177 in_pcballoc(so
, pcbinfo
, p
)
179 struct inpcbinfo
*pcbinfo
;
182 register struct inpcb
*inp
;
188 if (so
->cached_in_sock_layer
== 0) {
190 printf("PCBALLOC calling zalloc for socket %x\n", so
);
192 inp
= (struct inpcb
*) zalloc(pcbinfo
->ipi_zone
);
195 bzero((caddr_t
)inp
, sizeof(*inp
));
199 printf("PCBALLOC reusing PCB for socket %x\n", so
);
201 inp
= (struct inpcb
*) so
->so_saved_pcb
;
202 temp
= inp
->inp_saved_ppcb
;
203 bzero((caddr_t
) inp
, sizeof(*inp
));
204 inp
->inp_saved_ppcb
= temp
;
207 inp
->inp_gencnt
= ++pcbinfo
->ipi_gencnt
;
208 inp
->inp_pcbinfo
= pcbinfo
;
209 inp
->inp_socket
= so
;
212 if (ipsec_bypass
== 0) {
213 error
= ipsec_init_policy(so
, &inp
->inp_sp
);
215 zfree(pcbinfo
->ipi_zone
, (vm_offset_t
)inp
);
222 if (INP_SOCKAF(so
) == AF_INET6
&& !ip6_mapped_addr_on
)
223 inp
->inp_flags
|= IN6P_IPV6_V6ONLY
;
225 LIST_INSERT_HEAD(pcbinfo
->listhead
, inp
, inp_list
);
226 pcbinfo
->ipi_count
++;
227 so
->so_pcb
= (caddr_t
)inp
;
229 if (ip6_auto_flowlabel
)
230 inp
->inp_flags
|= IN6P_AUTOFLOWLABEL
;
236 in_pcbbind(inp
, nam
, p
)
237 register struct inpcb
*inp
;
238 struct sockaddr
*nam
;
241 register struct socket
*so
= inp
->inp_socket
;
242 unsigned short *lastport
;
243 struct sockaddr_in
*sin
;
244 struct inpcbinfo
*pcbinfo
= inp
->inp_pcbinfo
;
246 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
|| inp
->inp_laddr
.s_addr
!= INADDR_ANY
)
253 if ((so
->so_options
& (SO_REUSEADDR
|SO_REUSEPORT
)) == 0)
256 sin
= (struct sockaddr_in
*)nam
;
257 if (nam
->sa_len
!= sizeof (*sin
))
261 * We should check the family, but old programs
262 * incorrectly fail to initialize it.
264 if (sin
->sin_family
!= AF_INET
)
265 return (EAFNOSUPPORT
);
267 lport
= sin
->sin_port
;
268 if (IN_MULTICAST(ntohl(sin
->sin_addr
.s_addr
))) {
270 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
271 * allow complete duplication of binding if
272 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
273 * and a multicast address is bound on both
274 * new and duplicated sockets.
276 if (so
->so_options
& SO_REUSEADDR
)
277 reuseport
= SO_REUSEADDR
|SO_REUSEPORT
;
278 } else if (sin
->sin_addr
.s_addr
!= INADDR_ANY
) {
279 sin
->sin_port
= 0; /* yech... */
280 if (ifa_ifwithaddr((struct sockaddr
*)sin
) == 0)
281 return (EADDRNOTAVAIL
);
287 if (ntohs(lport
) < IPPORT_RESERVED
&& p
&&
288 suser(p
->p_ucred
, &p
->p_acflag
))
291 !IN_MULTICAST(ntohl(sin
->sin_addr
.s_addr
))) {
292 t
= in_pcblookup_local(inp
->inp_pcbinfo
,
293 sin
->sin_addr
, lport
, INPLOOKUP_WILDCARD
);
295 (ntohl(sin
->sin_addr
.s_addr
) != INADDR_ANY
||
296 ntohl(t
->inp_laddr
.s_addr
) != INADDR_ANY
||
297 (t
->inp_socket
->so_options
&
298 SO_REUSEPORT
) == 0) &&
299 (so
->so_uid
!= t
->inp_socket
->so_uid
)) {
301 if (ntohl(sin
->sin_addr
.s_addr
) !=
303 ntohl(t
->inp_laddr
.s_addr
) !=
306 INP_SOCKAF(t
->inp_socket
))
307 #endif /* defined(INET6) */
311 t
= in_pcblookup_local(pcbinfo
, sin
->sin_addr
,
314 (reuseport
& t
->inp_socket
->so_options
) == 0) {
316 if (ip6_mapped_addr_on
== 0 ||
317 ntohl(sin
->sin_addr
.s_addr
) !=
319 ntohl(t
->inp_laddr
.s_addr
) !=
322 INP_SOCKAF(t
->inp_socket
))
323 #endif /* defined(INET6) */
327 inp
->inp_laddr
= sin
->sin_addr
;
333 inp
->inp_flags
|= INP_ANONPORT
;
335 if (inp
->inp_flags
& INP_HIGHPORT
) {
336 first
= ipport_hifirstauto
; /* sysctl */
337 last
= ipport_hilastauto
;
338 lastport
= &pcbinfo
->lasthi
;
339 } else if (inp
->inp_flags
& INP_LOWPORT
) {
340 if (p
&& (error
= suser(p
->p_ucred
, &p
->p_acflag
)))
342 first
= ipport_lowfirstauto
; /* 1023 */
343 last
= ipport_lowlastauto
; /* 600 */
344 lastport
= &pcbinfo
->lastlow
;
346 first
= ipport_firstauto
; /* sysctl */
347 last
= ipport_lastauto
;
348 lastport
= &pcbinfo
->lastport
;
351 * Simple check to ensure all ports are not used up causing
354 * We split the two cases (up and down) so that the direction
355 * is not being tested on each round of the loop.
361 count
= first
- last
;
364 if (count
-- < 0) { /* completely used? */
365 inp
->inp_laddr
.s_addr
= INADDR_ANY
;
366 return (EADDRNOTAVAIL
);
369 if (*lastport
> first
|| *lastport
< last
)
371 lport
= htons(*lastport
);
372 } while (in_pcblookup_local(pcbinfo
,
373 inp
->inp_laddr
, lport
, wild
));
378 count
= last
- first
;
381 if (count
-- < 0) { /* completely used? */
382 inp
->inp_laddr
.s_addr
= INADDR_ANY
;
383 return (EADDRNOTAVAIL
);
386 if (*lastport
< first
|| *lastport
> last
)
388 lport
= htons(*lastport
);
389 } while (in_pcblookup_local(pcbinfo
,
390 inp
->inp_laddr
, lport
, wild
));
393 inp
->inp_lport
= lport
;
394 if (in_pcbinshash(inp
) != 0) {
395 inp
->inp_laddr
.s_addr
= INADDR_ANY
;
403 * Transform old in_pcbconnect() into an inner subroutine for new
404 * in_pcbconnect(): Do some validity-checking on the remote
405 * address (in mbuf 'nam') and then determine local host address
406 * (i.e., which interface) to use to access that remote host.
408 * This preserves definition of in_pcbconnect(), while supporting a
409 * slightly different version for T/TCP. (This is more than
410 * a bit of a kludge, but cleaning up the internal interfaces would
411 * have forced minor changes in every protocol).
415 in_pcbladdr(inp
, nam
, plocal_sin
)
416 register struct inpcb
*inp
;
417 struct sockaddr
*nam
;
418 struct sockaddr_in
**plocal_sin
;
420 struct in_ifaddr
*ia
;
421 register struct sockaddr_in
*sin
= (struct sockaddr_in
*)nam
;
423 if (nam
->sa_len
!= sizeof (*sin
))
425 if (sin
->sin_family
!= AF_INET
)
426 return (EAFNOSUPPORT
);
427 if (sin
->sin_port
== 0)
428 return (EADDRNOTAVAIL
);
429 if (!TAILQ_EMPTY(&in_ifaddrhead
)) {
431 * If the destination address is INADDR_ANY,
432 * use the primary local address.
433 * If the supplied address is INADDR_BROADCAST,
434 * and the primary interface supports broadcast,
435 * choose the broadcast address for that interface.
437 #define satosin(sa) ((struct sockaddr_in *)(sa))
438 #define sintosa(sin) ((struct sockaddr *)(sin))
439 #define ifatoia(ifa) ((struct in_ifaddr *)(ifa))
440 if (sin
->sin_addr
.s_addr
== INADDR_ANY
)
441 sin
->sin_addr
= IA_SIN(TAILQ_FIRST(&in_ifaddrhead
))->sin_addr
;
442 else if (sin
->sin_addr
.s_addr
== (u_long
)INADDR_BROADCAST
&&
443 (TAILQ_FIRST(&in_ifaddrhead
)->ia_ifp
->if_flags
& IFF_BROADCAST
))
444 sin
->sin_addr
= satosin(&TAILQ_FIRST(&in_ifaddrhead
)->ia_broadaddr
)->sin_addr
;
446 if (inp
->inp_laddr
.s_addr
== INADDR_ANY
) {
447 register struct route
*ro
;
449 ia
= (struct in_ifaddr
*)0;
451 * If route is known or can be allocated now,
452 * our src addr is taken from the i/f, else punt.
453 * Note that we should check the address family of the cached
454 * destination, in case of sharing the cache with IPv6.
456 ro
= &inp
->inp_route
;
458 (ro
->ro_dst
.sa_family
!= AF_INET
||
459 satosin(&ro
->ro_dst
)->sin_addr
.s_addr
!=
460 sin
->sin_addr
.s_addr
||
461 inp
->inp_socket
->so_options
& SO_DONTROUTE
||
462 ro
->ro_rt
->generation_id
!= route_generation
)) {
464 ro
->ro_rt
= (struct rtentry
*)0;
466 if ((inp
->inp_socket
->so_options
& SO_DONTROUTE
) == 0 && /*XXX*/
467 (ro
->ro_rt
== (struct rtentry
*)0 ||
468 ro
->ro_rt
->rt_ifp
== (struct ifnet
*)0)) {
469 /* No route yet, so try to acquire one */
470 bzero(&ro
->ro_dst
, sizeof(struct sockaddr_in
));
471 ro
->ro_dst
.sa_family
= AF_INET
;
472 ro
->ro_dst
.sa_len
= sizeof(struct sockaddr_in
);
473 ((struct sockaddr_in
*) &ro
->ro_dst
)->sin_addr
=
478 * If we found a route, use the address
479 * corresponding to the outgoing interface
480 * unless it is the loopback (in case a route
481 * to our address on another net goes to loopback).
483 if (ro
->ro_rt
&& !(ro
->ro_rt
->rt_ifp
->if_flags
& IFF_LOOPBACK
))
484 ia
= ifatoia(ro
->ro_rt
->rt_ifa
);
486 u_short fport
= sin
->sin_port
;
489 ia
= ifatoia(ifa_ifwithdstaddr(sintosa(sin
)));
491 ia
= ifatoia(ifa_ifwithnet(sintosa(sin
)));
492 sin
->sin_port
= fport
;
494 ia
= TAILQ_FIRST(&in_ifaddrhead
);
496 return (EADDRNOTAVAIL
);
499 * If the destination address is multicast and an outgoing
500 * interface has been set as a multicast option, use the
501 * address of that interface as our source address.
503 if (IN_MULTICAST(ntohl(sin
->sin_addr
.s_addr
)) &&
504 inp
->inp_moptions
!= NULL
) {
505 struct ip_moptions
*imo
;
508 imo
= inp
->inp_moptions
;
509 if (imo
->imo_multicast_ifp
!= NULL
) {
510 ifp
= imo
->imo_multicast_ifp
;
511 TAILQ_FOREACH(ia
, &in_ifaddrhead
, ia_link
)
512 if (ia
->ia_ifp
== ifp
)
515 return (EADDRNOTAVAIL
);
519 * Don't do pcblookup call here; return interface in plocal_sin
520 * and exit to caller, that will do the lookup.
522 *plocal_sin
= &ia
->ia_addr
;
530 * Connect from a socket to a specified address.
531 * Both address and port must be specified in argument sin.
532 * If don't have a local address for this socket yet,
536 in_pcbconnect(inp
, nam
, p
)
537 register struct inpcb
*inp
;
538 struct sockaddr
*nam
;
541 struct sockaddr_in
*ifaddr
;
542 struct sockaddr_in
*sin
= (struct sockaddr_in
*)nam
;
543 struct sockaddr_in sa
;
547 * Call inner routine, to assign local interface address.
549 if ((error
= in_pcbladdr(inp
, nam
, &ifaddr
)) != 0)
552 if (in_pcblookup_hash(inp
->inp_pcbinfo
, sin
->sin_addr
, sin
->sin_port
,
553 inp
->inp_laddr
.s_addr
? inp
->inp_laddr
: ifaddr
->sin_addr
,
554 inp
->inp_lport
, 0, NULL
) != NULL
) {
557 if (inp
->inp_laddr
.s_addr
== INADDR_ANY
) {
558 if (inp
->inp_lport
== 0) {
559 error
= in_pcbbind(inp
, (struct sockaddr
*)0, p
);
563 inp
->inp_laddr
= ifaddr
->sin_addr
;
564 inp
->inp_flags
|= INP_INADDR_ANY
;
566 inp
->inp_faddr
= sin
->sin_addr
;
567 inp
->inp_fport
= sin
->sin_port
;
573 in_pcbdisconnect(inp
)
577 inp
->inp_faddr
.s_addr
= INADDR_ANY
;
580 if (inp
->inp_socket
->so_state
& SS_NOFDREF
)
588 struct socket
*so
= inp
->inp_socket
;
589 struct inpcbinfo
*ipi
= inp
->inp_pcbinfo
;
590 struct rtentry
*rt
= inp
->inp_route
.ro_rt
;
593 if (so
->so_pcb
== 0) /* we've been called twice, ignore */
597 ipsec4_delete_pcbpolicy(inp
);
599 inp
->inp_gencnt
= ++ipi
->ipi_gencnt
;
603 if (so
->cached_in_sock_layer
)
604 printf("PCB_DETACH for cached socket %x\n", so
);
606 printf("PCB_DETACH for allocated socket %x\n", so
);
611 if (inp
->inp_options
)
612 (void)m_free(inp
->inp_options
);
615 * route deletion requires reference count to be <= zero
617 if ((rt
->rt_flags
& RTF_DELCLONE
) &&
618 (rt
->rt_flags
& RTF_WASCLONED
) &&
619 (rt
->rt_refcnt
<= 1)) {
621 rt
->rt_flags
&= ~RTF_UP
;
622 rtrequest(RTM_DELETE
, rt_key(rt
),
623 rt
->rt_gateway
, rt_mask(rt
),
624 rt
->rt_flags
, (struct rtentry
**)0);
628 inp
->inp_route
.ro_rt
= 0;
631 ip_freemoptions(inp
->inp_moptions
);
633 if (so
->cached_in_sock_layer
)
634 so
->so_saved_pcb
= (caddr_t
) inp
;
636 zfree(ipi
->ipi_zone
, (vm_offset_t
) inp
);
642 * The calling convention of in_setsockaddr() and in_setpeeraddr() was
643 * modified to match the pru_sockaddr() and pru_peeraddr() entry points
644 * in struct pr_usrreqs, so that protocols can just reference then directly
645 * without the need for a wrapper function. The socket must have a valid
646 * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
647 * except through a kernel programming error, so it is acceptable to panic
648 * (or in this case trap) if the PCB is invalid. (Actually, we don't trap
649 * because there actually /is/ a programming error somewhere... XXX)
652 in_setsockaddr(so
, nam
)
654 struct sockaddr
**nam
;
657 register struct inpcb
*inp
;
658 register struct sockaddr_in
*sin
;
661 * Do the malloc first in case it blocks.
663 MALLOC(sin
, struct sockaddr_in
*, sizeof *sin
, M_SONAME
, M_WAITOK
);
666 bzero(sin
, sizeof *sin
);
667 sin
->sin_family
= AF_INET
;
668 sin
->sin_len
= sizeof(*sin
);
677 sin
->sin_port
= inp
->inp_lport
;
678 sin
->sin_addr
= inp
->inp_laddr
;
681 *nam
= (struct sockaddr
*)sin
;
686 in_setpeeraddr(so
, nam
)
688 struct sockaddr
**nam
;
692 register struct sockaddr_in
*sin
;
695 * Do the malloc first in case it blocks.
697 MALLOC(sin
, struct sockaddr_in
*, sizeof *sin
, M_SONAME
, M_WAITOK
);
700 bzero((caddr_t
)sin
, sizeof (*sin
));
701 sin
->sin_family
= AF_INET
;
702 sin
->sin_len
= sizeof(*sin
);
711 sin
->sin_port
= inp
->inp_fport
;
712 sin
->sin_addr
= inp
->inp_faddr
;
715 *nam
= (struct sockaddr
*)sin
;
720 in_pcbnotifyall(head
, faddr
, errno
, notify
)
721 struct inpcbhead
*head
;
722 struct in_addr faddr
;
723 void (*notify
) __P((struct inpcb
*, int));
725 struct inpcb
*inp
, *ninp
;
729 for (inp
= LIST_FIRST(head
); inp
!= NULL
; inp
= ninp
) {
730 ninp
= LIST_NEXT(inp
, inp_list
);
732 if ((inp
->inp_vflag
& INP_IPV4
) == 0)
735 if (inp
->inp_faddr
.s_addr
!= faddr
.s_addr
||
736 inp
->inp_socket
== NULL
)
738 (*notify
)(inp
, errno
);
744 in_pcbpurgeif0(head
, ifp
)
749 struct ip_moptions
*imo
;
752 for (inp
= head
; inp
!= NULL
; inp
= LIST_NEXT(inp
, inp_list
)) {
753 imo
= inp
->inp_moptions
;
754 if ((inp
->inp_vflag
& INP_IPV4
) &&
757 * Unselect the outgoing interface if it is being
760 if (imo
->imo_multicast_ifp
== ifp
)
761 imo
->imo_multicast_ifp
= NULL
;
764 * Drop multicast group membership if we joined
765 * through the interface being detached.
767 for (i
= 0, gap
= 0; i
< imo
->imo_num_memberships
;
769 if (imo
->imo_membership
[i
]->inm_ifp
== ifp
) {
770 in_delmulti(imo
->imo_membership
[i
]);
773 imo
->imo_membership
[i
- gap
] =
774 imo
->imo_membership
[i
];
776 imo
->imo_num_memberships
-= gap
;
782 * Check for alternatives when higher level complains
783 * about service problems. For now, invalidate cached
784 * routing information. If the route was created dynamically
785 * (by a redirect), time to try a default gateway again.
791 register struct rtentry
*rt
;
792 struct rt_addrinfo info
;
794 if ((rt
= inp
->inp_route
.ro_rt
)) {
795 bzero((caddr_t
)&info
, sizeof(info
));
796 info
.rti_info
[RTAX_DST
] =
797 (struct sockaddr
*)&inp
->inp_route
.ro_dst
;
798 info
.rti_info
[RTAX_GATEWAY
] = rt
->rt_gateway
;
799 info
.rti_info
[RTAX_NETMASK
] = rt_mask(rt
);
800 rt_missmsg(RTM_LOSING
, &info
, rt
->rt_flags
, 0);
801 if (rt
->rt_flags
& RTF_DYNAMIC
)
802 (void) rtrequest(RTM_DELETE
, rt_key(rt
),
803 rt
->rt_gateway
, rt_mask(rt
), rt
->rt_flags
,
804 (struct rtentry
**)0);
805 inp
->inp_route
.ro_rt
= 0;
808 * A new route can be allocated
809 * the next time output is attempted.
815 * After a routing change, flush old routing
816 * and allocate a (hopefully) better one.
819 in_rtchange(inp
, errno
)
820 register struct inpcb
*inp
;
823 if (inp
->inp_route
.ro_rt
) {
824 if (ifa_foraddr(inp
->inp_laddr
.s_addr
) == NULL
)
825 return; /* we can't remove the route now. not sure if still ok to use src */
826 rtfree(inp
->inp_route
.ro_rt
);
827 inp
->inp_route
.ro_rt
= 0;
829 * A new route can be allocated the next time
830 * output is attempted.
836 * Lookup a PCB based on the local address and port.
839 in_pcblookup_local(pcbinfo
, laddr
, lport_arg
, wild_okay
)
840 struct inpcbinfo
*pcbinfo
;
841 struct in_addr laddr
;
845 register struct inpcb
*inp
;
846 int matchwild
= 3, wildcard
;
847 u_short lport
= lport_arg
;
849 KERNEL_DEBUG(DBG_FNC_PCB_LOOKUP
| DBG_FUNC_START
, 0,0,0,0,0);
852 struct inpcbhead
*head
;
854 * Look for an unconnected (wildcard foreign addr) PCB that
855 * matches the local address and port we're looking for.
857 head
= &pcbinfo
->hashbase
[INP_PCBHASH(INADDR_ANY
, lport
, 0, pcbinfo
->hashmask
)];
858 LIST_FOREACH(inp
, head
, inp_hash
) {
860 if ((inp
->inp_vflag
& INP_IPV4
) == 0)
863 if (inp
->inp_faddr
.s_addr
== INADDR_ANY
&&
864 inp
->inp_laddr
.s_addr
== laddr
.s_addr
&&
865 inp
->inp_lport
== lport
) {
875 KERNEL_DEBUG(DBG_FNC_PCB_LOOKUP
| DBG_FUNC_END
, 0,0,0,0,0);
878 struct inpcbporthead
*porthash
;
879 struct inpcbport
*phd
;
880 struct inpcb
*match
= NULL
;
882 * Best fit PCB lookup.
884 * First see if this local port is in use by looking on the
887 porthash
= &pcbinfo
->porthashbase
[INP_PCBPORTHASH(lport
,
888 pcbinfo
->porthashmask
)];
889 LIST_FOREACH(phd
, porthash
, phd_hash
) {
890 if (phd
->phd_port
== lport
)
895 * Port is in use by one or more PCBs. Look for best
898 LIST_FOREACH(inp
, &phd
->phd_pcblist
, inp_portlist
) {
901 if ((inp
->inp_vflag
& INP_IPV4
) == 0)
904 if (inp
->inp_faddr
.s_addr
!= INADDR_ANY
)
906 if (inp
->inp_laddr
.s_addr
!= INADDR_ANY
) {
907 if (laddr
.s_addr
== INADDR_ANY
)
909 else if (inp
->inp_laddr
.s_addr
!= laddr
.s_addr
)
912 if (laddr
.s_addr
!= INADDR_ANY
)
915 if (wildcard
< matchwild
) {
917 matchwild
= wildcard
;
918 if (matchwild
== 0) {
924 KERNEL_DEBUG(DBG_FNC_PCB_LOOKUP
| DBG_FUNC_END
, match
,0,0,0,0);
930 * Lookup PCB in hash list.
933 in_pcblookup_hash(pcbinfo
, faddr
, fport_arg
, laddr
, lport_arg
, wildcard
,
935 struct inpcbinfo
*pcbinfo
;
936 struct in_addr faddr
, laddr
;
937 u_int fport_arg
, lport_arg
;
941 struct inpcbhead
*head
;
942 register struct inpcb
*inp
;
943 u_short fport
= fport_arg
, lport
= lport_arg
;
946 * We may have found the pcb in the last lookup - check this first.
949 if ((!IN_MULTICAST(laddr
.s_addr
)) && (pcbinfo
->last_pcb
)) {
950 if (faddr
.s_addr
== pcbinfo
->last_pcb
->inp_faddr
.s_addr
&&
951 laddr
.s_addr
== pcbinfo
->last_pcb
->inp_laddr
.s_addr
&&
952 fport_arg
== pcbinfo
->last_pcb
->inp_fport
&&
953 lport_arg
== pcbinfo
->last_pcb
->inp_lport
) {
957 return (pcbinfo
->last_pcb
);
960 pcbinfo
->last_pcb
= 0;
964 * First look for an exact match.
966 head
= &pcbinfo
->hashbase
[INP_PCBHASH(faddr
.s_addr
, lport
, fport
, pcbinfo
->hashmask
)];
967 LIST_FOREACH(inp
, head
, inp_hash
) {
969 if ((inp
->inp_vflag
& INP_IPV4
) == 0)
972 if (inp
->inp_faddr
.s_addr
== faddr
.s_addr
&&
973 inp
->inp_laddr
.s_addr
== laddr
.s_addr
&&
974 inp
->inp_fport
== fport
&&
975 inp
->inp_lport
== lport
) {
983 struct inpcb
*local_wild
= NULL
;
985 struct inpcb
*local_wild_mapped
= NULL
;
988 head
= &pcbinfo
->hashbase
[INP_PCBHASH(INADDR_ANY
, lport
, 0, pcbinfo
->hashmask
)];
989 LIST_FOREACH(inp
, head
, inp_hash
) {
991 if ((inp
->inp_vflag
& INP_IPV4
) == 0)
994 if (inp
->inp_faddr
.s_addr
== INADDR_ANY
&&
995 inp
->inp_lport
== lport
) {
996 #if defined(NFAITH) && NFAITH > 0
997 if (ifp
&& ifp
->if_type
== IFT_FAITH
&&
998 (inp
->inp_flags
& INP_FAITH
) == 0)
1001 if (inp
->inp_laddr
.s_addr
== laddr
.s_addr
)
1003 else if (inp
->inp_laddr
.s_addr
== INADDR_ANY
) {
1005 if (INP_CHECK_SOCKAF(inp
->inp_socket
,
1007 local_wild_mapped
= inp
;
1009 #endif /* defined(INET6) */
1015 if (local_wild
== NULL
)
1016 return (local_wild_mapped
);
1017 #endif /* defined(INET6) */
1018 return (local_wild
);
1028 * Insert PCB onto various hash lists.
1034 struct inpcbhead
*pcbhash
;
1035 struct inpcbporthead
*pcbporthash
;
1036 struct inpcbinfo
*pcbinfo
= inp
->inp_pcbinfo
;
1037 struct inpcbport
*phd
;
1038 u_int32_t hashkey_faddr
;
1041 if (inp
->inp_vflag
& INP_IPV6
)
1042 hashkey_faddr
= inp
->in6p_faddr
.s6_addr32
[3] /* XXX */;
1045 hashkey_faddr
= inp
->inp_faddr
.s_addr
;
1047 pcbhash
= &pcbinfo
->hashbase
[INP_PCBHASH(hashkey_faddr
,
1048 inp
->inp_lport
, inp
->inp_fport
, pcbinfo
->hashmask
)];
1050 pcbporthash
= &pcbinfo
->porthashbase
[INP_PCBPORTHASH(inp
->inp_lport
,
1051 pcbinfo
->porthashmask
)];
1054 * Go through port list and look for a head for this lport.
1056 LIST_FOREACH(phd
, pcbporthash
, phd_hash
) {
1057 if (phd
->phd_port
== inp
->inp_lport
)
1061 * If none exists, malloc one and tack it on.
1064 MALLOC(phd
, struct inpcbport
*, sizeof(struct inpcbport
), M_PCB
, M_WAITOK
);
1066 return (ENOBUFS
); /* XXX */
1068 phd
->phd_port
= inp
->inp_lport
;
1069 LIST_INIT(&phd
->phd_pcblist
);
1070 LIST_INSERT_HEAD(pcbporthash
, phd
, phd_hash
);
1073 LIST_INSERT_HEAD(&phd
->phd_pcblist
, inp
, inp_portlist
);
1074 LIST_INSERT_HEAD(pcbhash
, inp
, inp_hash
);
1076 inp
->hash_element
= INP_PCBHASH(inp
->inp_faddr
.s_addr
, inp
->inp_lport
,
1077 inp
->inp_fport
, pcbinfo
->hashmask
);
1083 * Move PCB to the proper hash bucket when { faddr, fport } have been
1084 * changed. NOTE: This does not handle the case of the lport changing (the
1085 * hashed port list would have to be updated as well), so the lport must
1086 * not change after in_pcbinshash() has been called.
1092 struct inpcbhead
*head
;
1093 u_int32_t hashkey_faddr
;
1096 if (inp
->inp_vflag
& INP_IPV6
)
1097 hashkey_faddr
= inp
->in6p_faddr
.s6_addr32
[3] /* XXX */;
1100 hashkey_faddr
= inp
->inp_faddr
.s_addr
;
1102 head
= &inp
->inp_pcbinfo
->hashbase
[INP_PCBHASH(hashkey_faddr
,
1103 inp
->inp_lport
, inp
->inp_fport
, inp
->inp_pcbinfo
->hashmask
)];
1105 LIST_REMOVE(inp
, inp_hash
);
1106 LIST_INSERT_HEAD(head
, inp
, inp_hash
);
1108 inp
->hash_element
= INP_PCBHASH(inp
->inp_faddr
.s_addr
, inp
->inp_lport
,
1109 inp
->inp_fport
, inp
->inp_pcbinfo
->hashmask
);
1114 * Remove PCB from various lists.
1120 inp
->inp_gencnt
= ++inp
->inp_pcbinfo
->ipi_gencnt
;
1122 if (inp
== inp
->inp_pcbinfo
->last_pcb
)
1123 inp
->inp_pcbinfo
->last_pcb
= 0;
1126 if (inp
->inp_lport
) {
1127 struct inpcbport
*phd
= inp
->inp_phd
;
1129 LIST_REMOVE(inp
, inp_hash
);
1130 LIST_REMOVE(inp
, inp_portlist
);
1131 if (phd
!= NULL
&& (LIST_FIRST(&phd
->phd_pcblist
) == NULL
)) {
1132 LIST_REMOVE(phd
, phd_hash
);
1136 LIST_REMOVE(inp
, inp_list
);
1137 inp
->inp_pcbinfo
->ipi_count
--;
1141 in_pcb_grab_port
__P((struct inpcbinfo
*pcbinfo
,
1143 struct in_addr laddr
,
1145 struct in_addr faddr
,
1151 struct sockaddr_in sin
;
1152 struct proc
*p
= current_proc();
1156 pcbinfo
->nat_dummy_socket
.so_pcb
= 0;
1157 pcbinfo
->nat_dummy_socket
.so_options
= 0;
1159 /* The grabber wants a particular port */
1161 if (faddr
.s_addr
|| fport
) {
1163 * This is either the second half of an active connect, or
1164 * it's from the acceptance of an incoming connection.
1166 if (laddr
.s_addr
== 0) {
1170 if (in_pcblookup_hash(pcbinfo
, faddr
, fport
,
1171 laddr
, *lport
, 0, NULL
) != NULL
) {
1172 if (!(IN_MULTICAST(ntohl(laddr
.s_addr
)))) {
1173 return (EADDRINUSE
);
1177 stat
= in_pcballoc(&pcbinfo
->nat_dummy_socket
, pcbinfo
, p
);
1180 pcb
= sotoinpcb(&pcbinfo
->nat_dummy_socket
);
1181 pcb
->inp_vflag
|= INP_IPV4
;
1183 pcb
->inp_lport
= *lport
;
1184 pcb
->inp_laddr
.s_addr
= laddr
.s_addr
;
1186 pcb
->inp_faddr
= faddr
;
1187 pcb
->inp_fport
= fport
;
1192 * This is either a bind for a passive socket, or it's the
1193 * first part of bind-connect sequence (not likely since an
1194 * ephemeral port is usually used in this case). Or, it's
1195 * the result of a connection acceptance when the foreign
1196 * address/port cannot be provided (which requires the SO_REUSEADDR
1197 * flag if laddr is not multicast).
1200 stat
= in_pcballoc(&pcbinfo
->nat_dummy_socket
, pcbinfo
, p
);
1203 pcb
= sotoinpcb(&pcbinfo
->nat_dummy_socket
);
1204 pcb
->inp_vflag
|= INP_IPV4
;
1206 pcbinfo
->nat_dummy_socket
.so_options
= options
;
1207 bzero(&sin
, sizeof(struct sockaddr_in
));
1208 sin
.sin_len
= sizeof(struct sockaddr_in
);
1209 sin
.sin_family
= AF_INET
;
1210 sin
.sin_addr
.s_addr
= laddr
.s_addr
;
1211 sin
.sin_port
= *lport
;
1213 stat
= in_pcbbind((struct inpcb
*) pcbinfo
->nat_dummy_socket
.so_pcb
,
1214 (struct sockaddr
*) &sin
, p
);
1222 /* The grabber wants an ephemeral port */
1224 stat
= in_pcballoc(&pcbinfo
->nat_dummy_socket
, pcbinfo
, p
);
1227 pcb
= sotoinpcb(&pcbinfo
->nat_dummy_socket
);
1228 pcb
->inp_vflag
|= INP_IPV4
;
1230 bzero(&sin
, sizeof(struct sockaddr_in
));
1231 sin
.sin_len
= sizeof(struct sockaddr_in
);
1232 sin
.sin_family
= AF_INET
;
1233 sin
.sin_addr
.s_addr
= laddr
.s_addr
;
1236 if (faddr
.s_addr
|| fport
) {
1238 * Not sure if this case will be used - could occur when connect
1239 * is called, skipping the bind.
1242 if (laddr
.s_addr
== 0) {
1247 stat
= in_pcbbind((struct inpcb
*) pcbinfo
->nat_dummy_socket
.so_pcb
,
1248 (struct sockaddr
*) &sin
, p
);
1254 if (in_pcblookup_hash(pcbinfo
, faddr
, fport
,
1255 pcb
->inp_laddr
, pcb
->inp_lport
, 0, NULL
) != NULL
) {
1257 return (EADDRINUSE
);
1260 pcb
->inp_faddr
= faddr
;
1261 pcb
->inp_fport
= fport
;
1266 * This is a simple bind of an ephemeral port. The local addr
1267 * may or may not be defined.
1270 stat
= in_pcbbind((struct inpcb
*) pcbinfo
->nat_dummy_socket
.so_pcb
,
1271 (struct sockaddr
*) &sin
, p
);
1277 *lport
= pcb
->inp_lport
;
1281 pcb
->nat_owner
= owner_id
;
1282 pcb
->nat_cookie
= cookie
;
1283 pcb
->inp_ppcb
= (caddr_t
) pcbinfo
->dummy_cb
;
1288 in_pcb_letgo_port
__P((struct inpcbinfo
*pcbinfo
, struct in_addr laddr
, u_short lport
,
1289 struct in_addr faddr
, u_short fport
, u_char owner_id
))
1291 struct inpcbhead
*head
;
1292 register struct inpcb
*inp
;
1296 * First look for an exact match.
1298 head
= &pcbinfo
->hashbase
[INP_PCBHASH(faddr
.s_addr
, lport
, fport
, pcbinfo
->hashmask
)];
1299 for (inp
= head
->lh_first
; inp
!= NULL
; inp
= inp
->inp_hash
.le_next
) {
1300 if (inp
->inp_faddr
.s_addr
== faddr
.s_addr
&&
1301 inp
->inp_laddr
.s_addr
== laddr
.s_addr
&&
1302 inp
->inp_fport
== fport
&&
1303 inp
->inp_lport
== lport
&&
1304 inp
->nat_owner
== owner_id
) {
1317 in_pcb_get_owner(struct inpcbinfo
*pcbinfo
,
1318 struct in_addr laddr
, u_short lport
,
1319 struct in_addr faddr
, u_short fport
,
1324 u_char owner_id
= INPCB_NO_OWNER
;
1325 struct inpcbport
*phd
;
1326 struct inpcbporthead
*porthash
;
1329 if (IN_MULTICAST(laddr
.s_addr
)) {
1331 * Walk through PCB's looking for registered
1335 porthash
= &pcbinfo
->porthashbase
[INP_PCBPORTHASH(lport
,
1336 pcbinfo
->porthashmask
)];
1337 for (phd
= porthash
->lh_first
; phd
!= NULL
; phd
= phd
->phd_hash
.le_next
) {
1338 if (phd
->phd_port
== lport
)
1343 return INPCB_NO_OWNER
;
1346 owner_id
= INPCB_NO_OWNER
;
1347 for (inp
= phd
->phd_pcblist
.lh_first
; inp
!= NULL
;
1348 inp
= inp
->inp_portlist
.le_next
) {
1350 if (inp
->inp_laddr
.s_addr
== laddr
.s_addr
) {
1351 if (inp
->nat_owner
== 0)
1352 owner_id
|= INPCB_OWNED_BY_X
;
1354 owner_id
|= inp
->nat_owner
;
1361 inp
= in_pcblookup_hash(pcbinfo
, faddr
, fport
,
1362 laddr
, lport
, 1, NULL
);
1364 if (inp
->nat_owner
) {
1365 owner_id
= inp
->nat_owner
;
1366 *cookie
= inp
->nat_cookie
;
1369 pcbinfo
->last_pcb
= inp
;
1370 owner_id
= INPCB_OWNED_BY_X
;
1374 owner_id
= INPCB_NO_OWNER
;
1381 in_pcb_new_share_client(struct inpcbinfo
*pcbinfo
, u_char
*owner_id
)
1387 for (i
=0; i
< INPCB_MAX_IDS
; i
++) {
1388 if ((pcbinfo
->all_owners
& (1 << i
)) == 0) {
1389 pcbinfo
->all_owners
|= (1 << i
);
1390 *owner_id
= (1 << i
);
1399 in_pcb_rem_share_client(struct inpcbinfo
*pcbinfo
, u_char owner_id
)
1404 if (pcbinfo
->all_owners
& owner_id
) {
1405 pcbinfo
->all_owners
&= ~owner_id
;
1406 for (inp
= pcbinfo
->listhead
->lh_first
; inp
!= NULL
; inp
= inp
->inp_list
.le_next
) {
1407 if (inp
->nat_owner
& owner_id
) {
1408 if (inp
->nat_owner
== owner_id
)
1410 * Deallocate the pcb
1414 inp
->nat_owner
&= ~owner_id
;
1427 void in_pcb_nat_init(struct inpcbinfo
*pcbinfo
, int afamily
,
1428 int pfamily
, int protocol
)
1430 bzero(&pcbinfo
->nat_dummy_socket
, sizeof(struct socket
));
1431 pcbinfo
->nat_dummy_socket
.so_proto
= pffindproto(afamily
, pfamily
, protocol
);
1432 pcbinfo
->all_owners
= 0;
1437 prison_xinpcb(struct proc
*p
, struct inpcb
*inp
)
1441 if (ntohl(inp
->inp_laddr
.s_addr
) == p
->p_prison
->pr_ip
)