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 extern u_long route_generation
;
110 #define DBG_FNC_PCB_LOOKUP NETDBG_CODE(DBG_NETTCP, (6 << 8))
111 #define DBG_FNC_PCB_HLOOKUP NETDBG_CODE(DBG_NETTCP, ((6 << 8) | 1))
113 struct in_addr zeroin_addr
;
116 * These configure the range of local port addresses assigned to
117 * "unspecified" outgoing connections/packets/whatever.
119 int ipport_lowfirstauto
= IPPORT_RESERVED
- 1; /* 1023 */
120 int ipport_lowlastauto
= IPPORT_RESERVEDSTART
; /* 600 */
122 int ipport_firstauto
= IPPORT_RESERVED
; /* 1024 */
123 int ipport_lastauto
= IPPORT_USERRESERVED
; /* 5000 */
125 int ipport_firstauto
= IPPORT_HIFIRSTAUTO
; /* 49152 */
126 int ipport_lastauto
= IPPORT_HILASTAUTO
; /* 65535 */
128 int ipport_hifirstauto
= IPPORT_HIFIRSTAUTO
; /* 49152 */
129 int ipport_hilastauto
= IPPORT_HILASTAUTO
; /* 65535 */
131 #define RANGECHK(var, min, max) \
132 if ((var) < (min)) { (var) = (min); } \
133 else if ((var) > (max)) { (var) = (max); }
136 sysctl_net_ipport_check SYSCTL_HANDLER_ARGS
138 int error
= sysctl_handle_int(oidp
,
139 oidp
->oid_arg1
, oidp
->oid_arg2
, req
);
141 RANGECHK(ipport_lowfirstauto
, 1, IPPORT_RESERVED
- 1);
142 RANGECHK(ipport_lowlastauto
, 1, IPPORT_RESERVED
- 1);
143 RANGECHK(ipport_firstauto
, IPPORT_RESERVED
, USHRT_MAX
);
144 RANGECHK(ipport_lastauto
, IPPORT_RESERVED
, USHRT_MAX
);
145 RANGECHK(ipport_hifirstauto
, IPPORT_RESERVED
, USHRT_MAX
);
146 RANGECHK(ipport_hilastauto
, IPPORT_RESERVED
, USHRT_MAX
);
153 SYSCTL_NODE(_net_inet_ip
, IPPROTO_IP
, portrange
, CTLFLAG_RW
, 0, "IP Ports");
155 SYSCTL_PROC(_net_inet_ip_portrange
, OID_AUTO
, lowfirst
, CTLTYPE_INT
|CTLFLAG_RW
,
156 &ipport_lowfirstauto
, 0, &sysctl_net_ipport_check
, "I", "");
157 SYSCTL_PROC(_net_inet_ip_portrange
, OID_AUTO
, lowlast
, CTLTYPE_INT
|CTLFLAG_RW
,
158 &ipport_lowlastauto
, 0, &sysctl_net_ipport_check
, "I", "");
159 SYSCTL_PROC(_net_inet_ip_portrange
, OID_AUTO
, first
, CTLTYPE_INT
|CTLFLAG_RW
,
160 &ipport_firstauto
, 0, &sysctl_net_ipport_check
, "I", "");
161 SYSCTL_PROC(_net_inet_ip_portrange
, OID_AUTO
, last
, CTLTYPE_INT
|CTLFLAG_RW
,
162 &ipport_lastauto
, 0, &sysctl_net_ipport_check
, "I", "");
163 SYSCTL_PROC(_net_inet_ip_portrange
, OID_AUTO
, hifirst
, CTLTYPE_INT
|CTLFLAG_RW
,
164 &ipport_hifirstauto
, 0, &sysctl_net_ipport_check
, "I", "");
165 SYSCTL_PROC(_net_inet_ip_portrange
, OID_AUTO
, hilast
, CTLTYPE_INT
|CTLFLAG_RW
,
166 &ipport_hilastauto
, 0, &sysctl_net_ipport_check
, "I", "");
169 * in_pcb.c: manage the Protocol Control Blocks.
171 * NOTE: It is assumed that most of these functions will be called at
172 * splnet(). XXX - There are, unfortunately, a few exceptions to this
173 * rule that should be fixed.
177 * Allocate a PCB and associate it with the socket.
180 in_pcballoc(so
, pcbinfo
, p
)
182 struct inpcbinfo
*pcbinfo
;
185 register struct inpcb
*inp
;
191 if (so
->cached_in_sock_layer
== 0) {
193 printf("PCBALLOC calling zalloc for socket %x\n", so
);
195 inp
= (struct inpcb
*) zalloc(pcbinfo
->ipi_zone
);
198 bzero((caddr_t
)inp
, sizeof(*inp
));
202 printf("PCBALLOC reusing PCB for socket %x\n", so
);
204 inp
= (struct inpcb
*) so
->so_saved_pcb
;
205 temp
= inp
->inp_saved_ppcb
;
206 bzero((caddr_t
) inp
, sizeof(*inp
));
207 inp
->inp_saved_ppcb
= temp
;
210 inp
->inp_gencnt
= ++pcbinfo
->ipi_gencnt
;
211 inp
->inp_pcbinfo
= pcbinfo
;
212 inp
->inp_socket
= so
;
215 if (ipsec_bypass
== 0) {
216 error
= ipsec_init_policy(so
, &inp
->inp_sp
);
218 zfree(pcbinfo
->ipi_zone
, (vm_offset_t
)inp
);
225 if (INP_SOCKAF(so
) == AF_INET6
&& !ip6_mapped_addr_on
)
226 inp
->inp_flags
|= IN6P_IPV6_V6ONLY
;
228 LIST_INSERT_HEAD(pcbinfo
->listhead
, inp
, inp_list
);
229 pcbinfo
->ipi_count
++;
230 so
->so_pcb
= (caddr_t
)inp
;
232 if (ip6_auto_flowlabel
)
233 inp
->inp_flags
|= IN6P_AUTOFLOWLABEL
;
239 in_pcbbind(inp
, nam
, p
)
240 register struct inpcb
*inp
;
241 struct sockaddr
*nam
;
244 register struct socket
*so
= inp
->inp_socket
;
245 unsigned short *lastport
;
246 struct sockaddr_in
*sin
;
247 struct inpcbinfo
*pcbinfo
= inp
->inp_pcbinfo
;
249 int wild
= 0, reuseport
= (so
->so_options
& SO_REUSEPORT
);
252 if (TAILQ_EMPTY(&in_ifaddrhead
)) /* XXX broken! */
253 return (EADDRNOTAVAIL
);
254 if (inp
->inp_lport
|| inp
->inp_laddr
.s_addr
!= INADDR_ANY
)
256 if ((so
->so_options
& (SO_REUSEADDR
|SO_REUSEPORT
)) == 0)
259 sin
= (struct sockaddr_in
*)nam
;
260 if (nam
->sa_len
!= sizeof (*sin
))
264 * We should check the family, but old programs
265 * incorrectly fail to initialize it.
267 if (sin
->sin_family
!= AF_INET
)
268 return (EAFNOSUPPORT
);
270 lport
= sin
->sin_port
;
271 if (IN_MULTICAST(ntohl(sin
->sin_addr
.s_addr
))) {
273 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
274 * allow complete duplication of binding if
275 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
276 * and a multicast address is bound on both
277 * new and duplicated sockets.
279 if (so
->so_options
& SO_REUSEADDR
)
280 reuseport
= SO_REUSEADDR
|SO_REUSEPORT
;
281 } else if (sin
->sin_addr
.s_addr
!= INADDR_ANY
) {
282 sin
->sin_port
= 0; /* yech... */
283 if (ifa_ifwithaddr((struct sockaddr
*)sin
) == 0)
284 return (EADDRNOTAVAIL
);
290 if (ntohs(lport
) < IPPORT_RESERVED
&& p
&&
291 suser(p
->p_ucred
, &p
->p_acflag
))
294 !IN_MULTICAST(ntohl(sin
->sin_addr
.s_addr
))) {
295 t
= in_pcblookup_local(inp
->inp_pcbinfo
,
296 sin
->sin_addr
, lport
, INPLOOKUP_WILDCARD
);
298 (ntohl(sin
->sin_addr
.s_addr
) != INADDR_ANY
||
299 ntohl(t
->inp_laddr
.s_addr
) != INADDR_ANY
||
300 (t
->inp_socket
->so_options
&
301 SO_REUSEPORT
) == 0) &&
302 (so
->so_uid
!= t
->inp_socket
->so_uid
)) {
304 if (ntohl(sin
->sin_addr
.s_addr
) !=
306 ntohl(t
->inp_laddr
.s_addr
) !=
309 INP_SOCKAF(t
->inp_socket
))
310 #endif /* defined(INET6) */
314 t
= in_pcblookup_local(pcbinfo
, sin
->sin_addr
,
317 (reuseport
& t
->inp_socket
->so_options
) == 0) {
319 if (ip6_mapped_addr_on
== 0 ||
320 ntohl(sin
->sin_addr
.s_addr
) !=
322 ntohl(t
->inp_laddr
.s_addr
) !=
325 INP_SOCKAF(t
->inp_socket
))
326 #endif /* defined(INET6) */
330 inp
->inp_laddr
= sin
->sin_addr
;
336 inp
->inp_flags
|= INP_ANONPORT
;
338 if (inp
->inp_flags
& INP_HIGHPORT
) {
339 first
= ipport_hifirstauto
; /* sysctl */
340 last
= ipport_hilastauto
;
341 lastport
= &pcbinfo
->lasthi
;
342 } else if (inp
->inp_flags
& INP_LOWPORT
) {
343 if (p
&& (error
= suser(p
->p_ucred
, &p
->p_acflag
)))
345 first
= ipport_lowfirstauto
; /* 1023 */
346 last
= ipport_lowlastauto
; /* 600 */
347 lastport
= &pcbinfo
->lastlow
;
349 first
= ipport_firstauto
; /* sysctl */
350 last
= ipport_lastauto
;
351 lastport
= &pcbinfo
->lastport
;
354 * Simple check to ensure all ports are not used up causing
357 * We split the two cases (up and down) so that the direction
358 * is not being tested on each round of the loop.
364 count
= first
- last
;
367 if (count
-- < 0) { /* completely used? */
368 inp
->inp_laddr
.s_addr
= INADDR_ANY
;
369 return (EADDRNOTAVAIL
);
372 if (*lastport
> first
|| *lastport
< last
)
374 lport
= htons(*lastport
);
375 } while (in_pcblookup_local(pcbinfo
,
376 inp
->inp_laddr
, lport
, wild
));
381 count
= last
- first
;
384 if (count
-- < 0) { /* completely used? */
385 inp
->inp_laddr
.s_addr
= INADDR_ANY
;
386 return (EADDRNOTAVAIL
);
389 if (*lastport
< first
|| *lastport
> last
)
391 lport
= htons(*lastport
);
392 } while (in_pcblookup_local(pcbinfo
,
393 inp
->inp_laddr
, lport
, wild
));
396 inp
->inp_lport
= lport
;
397 if (in_pcbinshash(inp
) != 0) {
398 inp
->inp_laddr
.s_addr
= INADDR_ANY
;
406 * Transform old in_pcbconnect() into an inner subroutine for new
407 * in_pcbconnect(): Do some validity-checking on the remote
408 * address (in mbuf 'nam') and then determine local host address
409 * (i.e., which interface) to use to access that remote host.
411 * This preserves definition of in_pcbconnect(), while supporting a
412 * slightly different version for T/TCP. (This is more than
413 * a bit of a kludge, but cleaning up the internal interfaces would
414 * have forced minor changes in every protocol).
418 in_pcbladdr(inp
, nam
, plocal_sin
)
419 register struct inpcb
*inp
;
420 struct sockaddr
*nam
;
421 struct sockaddr_in
**plocal_sin
;
423 struct in_ifaddr
*ia
;
424 register struct sockaddr_in
*sin
= (struct sockaddr_in
*)nam
;
426 if (nam
->sa_len
!= sizeof (*sin
))
428 if (sin
->sin_family
!= AF_INET
)
429 return (EAFNOSUPPORT
);
430 if (sin
->sin_port
== 0)
431 return (EADDRNOTAVAIL
);
432 if (!TAILQ_EMPTY(&in_ifaddrhead
)) {
434 * If the destination address is INADDR_ANY,
435 * use the primary local address.
436 * If the supplied address is INADDR_BROADCAST,
437 * and the primary interface supports broadcast,
438 * choose the broadcast address for that interface.
440 #define satosin(sa) ((struct sockaddr_in *)(sa))
441 #define sintosa(sin) ((struct sockaddr *)(sin))
442 #define ifatoia(ifa) ((struct in_ifaddr *)(ifa))
443 if (sin
->sin_addr
.s_addr
== INADDR_ANY
)
444 sin
->sin_addr
= IA_SIN(TAILQ_FIRST(&in_ifaddrhead
))->sin_addr
;
445 else if (sin
->sin_addr
.s_addr
== (u_long
)INADDR_BROADCAST
&&
446 (TAILQ_FIRST(&in_ifaddrhead
)->ia_ifp
->if_flags
& IFF_BROADCAST
))
447 sin
->sin_addr
= satosin(&TAILQ_FIRST(&in_ifaddrhead
)->ia_broadaddr
)->sin_addr
;
449 if (inp
->inp_laddr
.s_addr
== INADDR_ANY
) {
450 register struct route
*ro
;
452 ia
= (struct in_ifaddr
*)0;
454 * If route is known or can be allocated now,
455 * our src addr is taken from the i/f, else punt.
456 * Note that we should check the address family of the cached
457 * destination, in case of sharing the cache with IPv6.
459 ro
= &inp
->inp_route
;
461 (ro
->ro_dst
.sa_family
!= AF_INET
||
462 satosin(&ro
->ro_dst
)->sin_addr
.s_addr
!=
463 sin
->sin_addr
.s_addr
||
464 inp
->inp_socket
->so_options
& SO_DONTROUTE
||
465 ro
->ro_rt
->generation_id
!= route_generation
)) {
467 ro
->ro_rt
= (struct rtentry
*)0;
469 if ((inp
->inp_socket
->so_options
& SO_DONTROUTE
) == 0 && /*XXX*/
470 (ro
->ro_rt
== (struct rtentry
*)0 ||
471 ro
->ro_rt
->rt_ifp
== (struct ifnet
*)0)) {
472 /* No route yet, so try to acquire one */
473 bzero(&ro
->ro_dst
, sizeof(struct sockaddr_in
));
474 ro
->ro_dst
.sa_family
= AF_INET
;
475 ro
->ro_dst
.sa_len
= sizeof(struct sockaddr_in
);
476 ((struct sockaddr_in
*) &ro
->ro_dst
)->sin_addr
=
481 * If we found a route, use the address
482 * corresponding to the outgoing interface
483 * unless it is the loopback (in case a route
484 * to our address on another net goes to loopback).
486 if (ro
->ro_rt
&& !(ro
->ro_rt
->rt_ifp
->if_flags
& IFF_LOOPBACK
))
487 ia
= ifatoia(ro
->ro_rt
->rt_ifa
);
489 u_short fport
= sin
->sin_port
;
492 ia
= ifatoia(ifa_ifwithdstaddr(sintosa(sin
)));
494 ia
= ifatoia(ifa_ifwithnet(sintosa(sin
)));
495 sin
->sin_port
= fport
;
497 ia
= TAILQ_FIRST(&in_ifaddrhead
);
499 return (EADDRNOTAVAIL
);
502 * If the destination address is multicast and an outgoing
503 * interface has been set as a multicast option, use the
504 * address of that interface as our source address.
506 if (IN_MULTICAST(ntohl(sin
->sin_addr
.s_addr
)) &&
507 inp
->inp_moptions
!= NULL
) {
508 struct ip_moptions
*imo
;
511 imo
= inp
->inp_moptions
;
512 if (imo
->imo_multicast_ifp
!= NULL
) {
513 ifp
= imo
->imo_multicast_ifp
;
514 TAILQ_FOREACH(ia
, &in_ifaddrhead
, ia_link
)
515 if (ia
->ia_ifp
== ifp
)
518 return (EADDRNOTAVAIL
);
522 * Don't do pcblookup call here; return interface in plocal_sin
523 * and exit to caller, that will do the lookup.
525 *plocal_sin
= &ia
->ia_addr
;
533 * Connect from a socket to a specified address.
534 * Both address and port must be specified in argument sin.
535 * If don't have a local address for this socket yet,
539 in_pcbconnect(inp
, nam
, p
)
540 register struct inpcb
*inp
;
541 struct sockaddr
*nam
;
544 struct sockaddr_in
*ifaddr
;
545 struct sockaddr_in
*sin
= (struct sockaddr_in
*)nam
;
546 struct sockaddr_in sa
;
550 * Call inner routine, to assign local interface address.
552 if ((error
= in_pcbladdr(inp
, nam
, &ifaddr
)) != 0)
555 if (in_pcblookup_hash(inp
->inp_pcbinfo
, sin
->sin_addr
, sin
->sin_port
,
556 inp
->inp_laddr
.s_addr
? inp
->inp_laddr
: ifaddr
->sin_addr
,
557 inp
->inp_lport
, 0, NULL
) != NULL
) {
560 if (inp
->inp_laddr
.s_addr
== INADDR_ANY
) {
561 if (inp
->inp_lport
== 0) {
562 error
= in_pcbbind(inp
, (struct sockaddr
*)0, p
);
566 inp
->inp_laddr
= ifaddr
->sin_addr
;
567 inp
->inp_flags
|= INP_INADDR_ANY
;
569 inp
->inp_faddr
= sin
->sin_addr
;
570 inp
->inp_fport
= sin
->sin_port
;
576 in_pcbdisconnect(inp
)
580 inp
->inp_faddr
.s_addr
= INADDR_ANY
;
583 if (inp
->inp_socket
->so_state
& SS_NOFDREF
)
591 struct socket
*so
= inp
->inp_socket
;
592 struct inpcbinfo
*ipi
= inp
->inp_pcbinfo
;
593 struct rtentry
*rt
= inp
->inp_route
.ro_rt
;
596 if (so
->so_pcb
== 0) /* we've been called twice, ignore */
600 ipsec4_delete_pcbpolicy(inp
);
602 inp
->inp_gencnt
= ++ipi
->ipi_gencnt
;
606 if (so
->cached_in_sock_layer
)
607 printf("PCB_DETACH for cached socket %x\n", so
);
609 printf("PCB_DETACH for allocated socket %x\n", so
);
614 if (inp
->inp_options
)
615 (void)m_free(inp
->inp_options
);
618 * route deletion requires reference count to be <= zero
620 if ((rt
->rt_flags
& RTF_DELCLONE
) &&
621 (rt
->rt_flags
& RTF_WASCLONED
) &&
622 (rt
->rt_refcnt
<= 1)) {
624 rt
->rt_flags
&= ~RTF_UP
;
625 rtrequest(RTM_DELETE
, rt_key(rt
),
626 rt
->rt_gateway
, rt_mask(rt
),
627 rt
->rt_flags
, (struct rtentry
**)0);
631 inp
->inp_route
.ro_rt
= 0;
634 ip_freemoptions(inp
->inp_moptions
);
636 if (so
->cached_in_sock_layer
)
637 so
->so_saved_pcb
= (caddr_t
) inp
;
639 zfree(ipi
->ipi_zone
, (vm_offset_t
) inp
);
645 * The calling convention of in_setsockaddr() and in_setpeeraddr() was
646 * modified to match the pru_sockaddr() and pru_peeraddr() entry points
647 * in struct pr_usrreqs, so that protocols can just reference then directly
648 * without the need for a wrapper function. The socket must have a valid
649 * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
650 * except through a kernel programming error, so it is acceptable to panic
651 * (or in this case trap) if the PCB is invalid. (Actually, we don't trap
652 * because there actually /is/ a programming error somewhere... XXX)
655 in_setsockaddr(so
, nam
)
657 struct sockaddr
**nam
;
660 register struct inpcb
*inp
;
661 register struct sockaddr_in
*sin
;
664 * Do the malloc first in case it blocks.
666 MALLOC(sin
, struct sockaddr_in
*, sizeof *sin
, M_SONAME
, M_WAITOK
);
669 bzero(sin
, sizeof *sin
);
670 sin
->sin_family
= AF_INET
;
671 sin
->sin_len
= sizeof(*sin
);
680 sin
->sin_port
= inp
->inp_lport
;
681 sin
->sin_addr
= inp
->inp_laddr
;
684 *nam
= (struct sockaddr
*)sin
;
689 in_setpeeraddr(so
, nam
)
691 struct sockaddr
**nam
;
695 register struct sockaddr_in
*sin
;
698 * Do the malloc first in case it blocks.
700 MALLOC(sin
, struct sockaddr_in
*, sizeof *sin
, M_SONAME
, M_WAITOK
);
703 bzero((caddr_t
)sin
, sizeof (*sin
));
704 sin
->sin_family
= AF_INET
;
705 sin
->sin_len
= sizeof(*sin
);
714 sin
->sin_port
= inp
->inp_fport
;
715 sin
->sin_addr
= inp
->inp_faddr
;
718 *nam
= (struct sockaddr
*)sin
;
723 in_pcbnotifyall(head
, faddr
, errno
, notify
)
724 struct inpcbhead
*head
;
725 struct in_addr faddr
;
726 void (*notify
) __P((struct inpcb
*, int));
728 struct inpcb
*inp
, *ninp
;
732 for (inp
= LIST_FIRST(head
); inp
!= NULL
; inp
= ninp
) {
733 ninp
= LIST_NEXT(inp
, inp_list
);
735 if ((inp
->inp_vflag
& INP_IPV4
) == 0)
738 if (inp
->inp_faddr
.s_addr
!= faddr
.s_addr
||
739 inp
->inp_socket
== NULL
)
741 (*notify
)(inp
, errno
);
747 in_pcbpurgeif0(head
, ifp
)
752 struct ip_moptions
*imo
;
755 for (inp
= head
; inp
!= NULL
; inp
= LIST_NEXT(inp
, inp_list
)) {
756 imo
= inp
->inp_moptions
;
757 if ((inp
->inp_vflag
& INP_IPV4
) &&
760 * Unselect the outgoing interface if it is being
763 if (imo
->imo_multicast_ifp
== ifp
)
764 imo
->imo_multicast_ifp
= NULL
;
767 * Drop multicast group membership if we joined
768 * through the interface being detached.
770 for (i
= 0, gap
= 0; i
< imo
->imo_num_memberships
;
772 if (imo
->imo_membership
[i
]->inm_ifp
== ifp
) {
773 in_delmulti(imo
->imo_membership
[i
]);
776 imo
->imo_membership
[i
- gap
] =
777 imo
->imo_membership
[i
];
779 imo
->imo_num_memberships
-= gap
;
785 * Check for alternatives when higher level complains
786 * about service problems. For now, invalidate cached
787 * routing information. If the route was created dynamically
788 * (by a redirect), time to try a default gateway again.
794 register struct rtentry
*rt
;
795 struct rt_addrinfo info
;
797 if ((rt
= inp
->inp_route
.ro_rt
)) {
798 bzero((caddr_t
)&info
, sizeof(info
));
799 info
.rti_info
[RTAX_DST
] =
800 (struct sockaddr
*)&inp
->inp_route
.ro_dst
;
801 info
.rti_info
[RTAX_GATEWAY
] = rt
->rt_gateway
;
802 info
.rti_info
[RTAX_NETMASK
] = rt_mask(rt
);
803 rt_missmsg(RTM_LOSING
, &info
, rt
->rt_flags
, 0);
804 if (rt
->rt_flags
& RTF_DYNAMIC
)
805 (void) rtrequest(RTM_DELETE
, rt_key(rt
),
806 rt
->rt_gateway
, rt_mask(rt
), rt
->rt_flags
,
807 (struct rtentry
**)0);
808 inp
->inp_route
.ro_rt
= 0;
811 * A new route can be allocated
812 * the next time output is attempted.
818 * After a routing change, flush old routing
819 * and allocate a (hopefully) better one.
822 in_rtchange(inp
, errno
)
823 register struct inpcb
*inp
;
826 if (inp
->inp_route
.ro_rt
) {
827 if (ifa_foraddr(inp
->inp_laddr
.s_addr
) == NULL
)
828 return; /* we can't remove the route now. not sure if still ok to use src */
829 rtfree(inp
->inp_route
.ro_rt
);
830 inp
->inp_route
.ro_rt
= 0;
832 * A new route can be allocated the next time
833 * output is attempted.
839 * Lookup a PCB based on the local address and port.
842 in_pcblookup_local(pcbinfo
, laddr
, lport_arg
, wild_okay
)
843 struct inpcbinfo
*pcbinfo
;
844 struct in_addr laddr
;
848 register struct inpcb
*inp
;
849 int matchwild
= 3, wildcard
;
850 u_short lport
= lport_arg
;
852 KERNEL_DEBUG(DBG_FNC_PCB_LOOKUP
| DBG_FUNC_START
, 0,0,0,0,0);
855 struct inpcbhead
*head
;
857 * Look for an unconnected (wildcard foreign addr) PCB that
858 * matches the local address and port we're looking for.
860 head
= &pcbinfo
->hashbase
[INP_PCBHASH(INADDR_ANY
, lport
, 0, pcbinfo
->hashmask
)];
861 LIST_FOREACH(inp
, head
, inp_hash
) {
863 if ((inp
->inp_vflag
& INP_IPV4
) == 0)
866 if (inp
->inp_faddr
.s_addr
== INADDR_ANY
&&
867 inp
->inp_laddr
.s_addr
== laddr
.s_addr
&&
868 inp
->inp_lport
== lport
) {
878 KERNEL_DEBUG(DBG_FNC_PCB_LOOKUP
| DBG_FUNC_END
, 0,0,0,0,0);
881 struct inpcbporthead
*porthash
;
882 struct inpcbport
*phd
;
883 struct inpcb
*match
= NULL
;
885 * Best fit PCB lookup.
887 * First see if this local port is in use by looking on the
890 porthash
= &pcbinfo
->porthashbase
[INP_PCBPORTHASH(lport
,
891 pcbinfo
->porthashmask
)];
892 LIST_FOREACH(phd
, porthash
, phd_hash
) {
893 if (phd
->phd_port
== lport
)
898 * Port is in use by one or more PCBs. Look for best
901 LIST_FOREACH(inp
, &phd
->phd_pcblist
, inp_portlist
) {
904 if ((inp
->inp_vflag
& INP_IPV4
) == 0)
907 if (inp
->inp_faddr
.s_addr
!= INADDR_ANY
)
909 if (inp
->inp_laddr
.s_addr
!= INADDR_ANY
) {
910 if (laddr
.s_addr
== INADDR_ANY
)
912 else if (inp
->inp_laddr
.s_addr
!= laddr
.s_addr
)
915 if (laddr
.s_addr
!= INADDR_ANY
)
918 if (wildcard
< matchwild
) {
920 matchwild
= wildcard
;
921 if (matchwild
== 0) {
927 KERNEL_DEBUG(DBG_FNC_PCB_LOOKUP
| DBG_FUNC_END
, match
,0,0,0,0);
933 * Lookup PCB in hash list.
936 in_pcblookup_hash(pcbinfo
, faddr
, fport_arg
, laddr
, lport_arg
, wildcard
,
938 struct inpcbinfo
*pcbinfo
;
939 struct in_addr faddr
, laddr
;
940 u_int fport_arg
, lport_arg
;
944 struct inpcbhead
*head
;
945 register struct inpcb
*inp
;
946 u_short fport
= fport_arg
, lport
= lport_arg
;
949 * We may have found the pcb in the last lookup - check this first.
952 if ((!IN_MULTICAST(laddr
.s_addr
)) && (pcbinfo
->last_pcb
)) {
953 if (faddr
.s_addr
== pcbinfo
->last_pcb
->inp_faddr
.s_addr
&&
954 laddr
.s_addr
== pcbinfo
->last_pcb
->inp_laddr
.s_addr
&&
955 fport_arg
== pcbinfo
->last_pcb
->inp_fport
&&
956 lport_arg
== pcbinfo
->last_pcb
->inp_lport
) {
960 return (pcbinfo
->last_pcb
);
963 pcbinfo
->last_pcb
= 0;
967 * First look for an exact match.
969 head
= &pcbinfo
->hashbase
[INP_PCBHASH(faddr
.s_addr
, lport
, fport
, pcbinfo
->hashmask
)];
970 LIST_FOREACH(inp
, head
, inp_hash
) {
972 if ((inp
->inp_vflag
& INP_IPV4
) == 0)
975 if (inp
->inp_faddr
.s_addr
== faddr
.s_addr
&&
976 inp
->inp_laddr
.s_addr
== laddr
.s_addr
&&
977 inp
->inp_fport
== fport
&&
978 inp
->inp_lport
== lport
) {
986 struct inpcb
*local_wild
= NULL
;
988 struct inpcb
*local_wild_mapped
= NULL
;
991 head
= &pcbinfo
->hashbase
[INP_PCBHASH(INADDR_ANY
, lport
, 0, pcbinfo
->hashmask
)];
992 LIST_FOREACH(inp
, head
, inp_hash
) {
994 if ((inp
->inp_vflag
& INP_IPV4
) == 0)
997 if (inp
->inp_faddr
.s_addr
== INADDR_ANY
&&
998 inp
->inp_lport
== lport
) {
999 #if defined(NFAITH) && NFAITH > 0
1000 if (ifp
&& ifp
->if_type
== IFT_FAITH
&&
1001 (inp
->inp_flags
& INP_FAITH
) == 0)
1004 if (inp
->inp_laddr
.s_addr
== laddr
.s_addr
)
1006 else if (inp
->inp_laddr
.s_addr
== INADDR_ANY
) {
1008 if (INP_CHECK_SOCKAF(inp
->inp_socket
,
1010 local_wild_mapped
= inp
;
1012 #endif /* defined(INET6) */
1018 if (local_wild
== NULL
)
1019 return (local_wild_mapped
);
1020 #endif /* defined(INET6) */
1021 return (local_wild
);
1031 * Insert PCB onto various hash lists.
1037 struct inpcbhead
*pcbhash
;
1038 struct inpcbporthead
*pcbporthash
;
1039 struct inpcbinfo
*pcbinfo
= inp
->inp_pcbinfo
;
1040 struct inpcbport
*phd
;
1041 u_int32_t hashkey_faddr
;
1044 if (inp
->inp_vflag
& INP_IPV6
)
1045 hashkey_faddr
= inp
->in6p_faddr
.s6_addr32
[3] /* XXX */;
1048 hashkey_faddr
= inp
->inp_faddr
.s_addr
;
1050 pcbhash
= &pcbinfo
->hashbase
[INP_PCBHASH(hashkey_faddr
,
1051 inp
->inp_lport
, inp
->inp_fport
, pcbinfo
->hashmask
)];
1053 pcbporthash
= &pcbinfo
->porthashbase
[INP_PCBPORTHASH(inp
->inp_lport
,
1054 pcbinfo
->porthashmask
)];
1057 * Go through port list and look for a head for this lport.
1059 LIST_FOREACH(phd
, pcbporthash
, phd_hash
) {
1060 if (phd
->phd_port
== inp
->inp_lport
)
1064 * If none exists, malloc one and tack it on.
1067 MALLOC(phd
, struct inpcbport
*, sizeof(struct inpcbport
), M_PCB
, M_WAITOK
);
1069 return (ENOBUFS
); /* XXX */
1071 phd
->phd_port
= inp
->inp_lport
;
1072 LIST_INIT(&phd
->phd_pcblist
);
1073 LIST_INSERT_HEAD(pcbporthash
, phd
, phd_hash
);
1076 LIST_INSERT_HEAD(&phd
->phd_pcblist
, inp
, inp_portlist
);
1077 LIST_INSERT_HEAD(pcbhash
, inp
, inp_hash
);
1079 inp
->hash_element
= INP_PCBHASH(inp
->inp_faddr
.s_addr
, inp
->inp_lport
,
1080 inp
->inp_fport
, pcbinfo
->hashmask
);
1086 * Move PCB to the proper hash bucket when { faddr, fport } have been
1087 * changed. NOTE: This does not handle the case of the lport changing (the
1088 * hashed port list would have to be updated as well), so the lport must
1089 * not change after in_pcbinshash() has been called.
1095 struct inpcbhead
*head
;
1096 u_int32_t hashkey_faddr
;
1099 if (inp
->inp_vflag
& INP_IPV6
)
1100 hashkey_faddr
= inp
->in6p_faddr
.s6_addr32
[3] /* XXX */;
1103 hashkey_faddr
= inp
->inp_faddr
.s_addr
;
1105 head
= &inp
->inp_pcbinfo
->hashbase
[INP_PCBHASH(hashkey_faddr
,
1106 inp
->inp_lport
, inp
->inp_fport
, inp
->inp_pcbinfo
->hashmask
)];
1108 LIST_REMOVE(inp
, inp_hash
);
1109 LIST_INSERT_HEAD(head
, inp
, inp_hash
);
1111 inp
->hash_element
= INP_PCBHASH(inp
->inp_faddr
.s_addr
, inp
->inp_lport
,
1112 inp
->inp_fport
, inp
->inp_pcbinfo
->hashmask
);
1117 * Remove PCB from various lists.
1123 inp
->inp_gencnt
= ++inp
->inp_pcbinfo
->ipi_gencnt
;
1125 if (inp
== inp
->inp_pcbinfo
->last_pcb
)
1126 inp
->inp_pcbinfo
->last_pcb
= 0;
1129 if (inp
->inp_lport
) {
1130 struct inpcbport
*phd
= inp
->inp_phd
;
1132 LIST_REMOVE(inp
, inp_hash
);
1133 LIST_REMOVE(inp
, inp_portlist
);
1134 if (phd
!= NULL
&& (LIST_FIRST(&phd
->phd_pcblist
) == NULL
)) {
1135 LIST_REMOVE(phd
, phd_hash
);
1139 LIST_REMOVE(inp
, inp_list
);
1140 inp
->inp_pcbinfo
->ipi_count
--;
1144 in_pcb_grab_port
__P((struct inpcbinfo
*pcbinfo
,
1146 struct in_addr laddr
,
1148 struct in_addr faddr
,
1154 struct sockaddr_in sin
;
1155 struct proc
*p
= current_proc();
1159 pcbinfo
->nat_dummy_socket
.so_pcb
= 0;
1160 pcbinfo
->nat_dummy_socket
.so_options
= 0;
1162 /* The grabber wants a particular port */
1164 if (faddr
.s_addr
|| fport
) {
1166 * This is either the second half of an active connect, or
1167 * it's from the acceptance of an incoming connection.
1169 if (laddr
.s_addr
== 0) {
1173 if (in_pcblookup_hash(pcbinfo
, faddr
, fport
,
1174 laddr
, *lport
, 0, NULL
) != NULL
) {
1175 if (!(IN_MULTICAST(ntohl(laddr
.s_addr
)))) {
1176 return (EADDRINUSE
);
1180 stat
= in_pcballoc(&pcbinfo
->nat_dummy_socket
, pcbinfo
, p
);
1183 pcb
= sotoinpcb(&pcbinfo
->nat_dummy_socket
);
1184 pcb
->inp_vflag
|= INP_IPV4
;
1186 pcb
->inp_lport
= *lport
;
1187 pcb
->inp_laddr
.s_addr
= laddr
.s_addr
;
1189 pcb
->inp_faddr
= faddr
;
1190 pcb
->inp_fport
= fport
;
1195 * This is either a bind for a passive socket, or it's the
1196 * first part of bind-connect sequence (not likely since an
1197 * ephemeral port is usually used in this case). Or, it's
1198 * the result of a connection acceptance when the foreign
1199 * address/port cannot be provided (which requires the SO_REUSEADDR
1200 * flag if laddr is not multicast).
1203 stat
= in_pcballoc(&pcbinfo
->nat_dummy_socket
, pcbinfo
, p
);
1206 pcb
= sotoinpcb(&pcbinfo
->nat_dummy_socket
);
1207 pcb
->inp_vflag
|= INP_IPV4
;
1209 pcbinfo
->nat_dummy_socket
.so_options
= options
;
1210 bzero(&sin
, sizeof(struct sockaddr_in
));
1211 sin
.sin_len
= sizeof(struct sockaddr_in
);
1212 sin
.sin_family
= AF_INET
;
1213 sin
.sin_addr
.s_addr
= laddr
.s_addr
;
1214 sin
.sin_port
= *lport
;
1216 stat
= in_pcbbind((struct inpcb
*) pcbinfo
->nat_dummy_socket
.so_pcb
,
1217 (struct sockaddr
*) &sin
, p
);
1225 /* The grabber wants an ephemeral port */
1227 stat
= in_pcballoc(&pcbinfo
->nat_dummy_socket
, pcbinfo
, p
);
1230 pcb
= sotoinpcb(&pcbinfo
->nat_dummy_socket
);
1231 pcb
->inp_vflag
|= INP_IPV4
;
1233 bzero(&sin
, sizeof(struct sockaddr_in
));
1234 sin
.sin_len
= sizeof(struct sockaddr_in
);
1235 sin
.sin_family
= AF_INET
;
1236 sin
.sin_addr
.s_addr
= laddr
.s_addr
;
1239 if (faddr
.s_addr
|| fport
) {
1241 * Not sure if this case will be used - could occur when connect
1242 * is called, skipping the bind.
1245 if (laddr
.s_addr
== 0) {
1250 stat
= in_pcbbind((struct inpcb
*) pcbinfo
->nat_dummy_socket
.so_pcb
,
1251 (struct sockaddr
*) &sin
, p
);
1257 if (in_pcblookup_hash(pcbinfo
, faddr
, fport
,
1258 pcb
->inp_laddr
, pcb
->inp_lport
, 0, NULL
) != NULL
) {
1260 return (EADDRINUSE
);
1263 pcb
->inp_faddr
= faddr
;
1264 pcb
->inp_fport
= fport
;
1269 * This is a simple bind of an ephemeral port. The local addr
1270 * may or may not be defined.
1273 stat
= in_pcbbind((struct inpcb
*) pcbinfo
->nat_dummy_socket
.so_pcb
,
1274 (struct sockaddr
*) &sin
, p
);
1280 *lport
= pcb
->inp_lport
;
1284 pcb
->nat_owner
= owner_id
;
1285 pcb
->nat_cookie
= cookie
;
1286 pcb
->inp_ppcb
= (caddr_t
) pcbinfo
->dummy_cb
;
1291 in_pcb_letgo_port
__P((struct inpcbinfo
*pcbinfo
, struct in_addr laddr
, u_short lport
,
1292 struct in_addr faddr
, u_short fport
, u_char owner_id
))
1294 struct inpcbhead
*head
;
1295 register struct inpcb
*inp
;
1299 * First look for an exact match.
1301 head
= &pcbinfo
->hashbase
[INP_PCBHASH(faddr
.s_addr
, lport
, fport
, pcbinfo
->hashmask
)];
1302 for (inp
= head
->lh_first
; inp
!= NULL
; inp
= inp
->inp_hash
.le_next
) {
1303 if (inp
->inp_faddr
.s_addr
== faddr
.s_addr
&&
1304 inp
->inp_laddr
.s_addr
== laddr
.s_addr
&&
1305 inp
->inp_fport
== fport
&&
1306 inp
->inp_lport
== lport
&&
1307 inp
->nat_owner
== owner_id
) {
1320 in_pcb_get_owner(struct inpcbinfo
*pcbinfo
,
1321 struct in_addr laddr
, u_short lport
,
1322 struct in_addr faddr
, u_short fport
,
1327 u_char owner_id
= INPCB_NO_OWNER
;
1328 struct inpcbport
*phd
;
1329 struct inpcbporthead
*porthash
;
1332 if (IN_MULTICAST(laddr
.s_addr
)) {
1334 * Walk through PCB's looking for registered
1338 porthash
= &pcbinfo
->porthashbase
[INP_PCBPORTHASH(lport
,
1339 pcbinfo
->porthashmask
)];
1340 for (phd
= porthash
->lh_first
; phd
!= NULL
; phd
= phd
->phd_hash
.le_next
) {
1341 if (phd
->phd_port
== lport
)
1346 return INPCB_NO_OWNER
;
1349 owner_id
= INPCB_NO_OWNER
;
1350 for (inp
= phd
->phd_pcblist
.lh_first
; inp
!= NULL
;
1351 inp
= inp
->inp_portlist
.le_next
) {
1353 if (inp
->inp_laddr
.s_addr
== laddr
.s_addr
) {
1354 if (inp
->nat_owner
== 0)
1355 owner_id
|= INPCB_OWNED_BY_X
;
1357 owner_id
|= inp
->nat_owner
;
1364 inp
= in_pcblookup_hash(pcbinfo
, faddr
, fport
,
1365 laddr
, lport
, 1, NULL
);
1367 if (inp
->nat_owner
) {
1368 owner_id
= inp
->nat_owner
;
1369 *cookie
= inp
->nat_cookie
;
1372 pcbinfo
->last_pcb
= inp
;
1373 owner_id
= INPCB_OWNED_BY_X
;
1377 owner_id
= INPCB_NO_OWNER
;
1384 in_pcb_new_share_client(struct inpcbinfo
*pcbinfo
, u_char
*owner_id
)
1390 for (i
=0; i
< INPCB_MAX_IDS
; i
++) {
1391 if ((pcbinfo
->all_owners
& (1 << i
)) == 0) {
1392 pcbinfo
->all_owners
|= (1 << i
);
1393 *owner_id
= (1 << i
);
1402 in_pcb_rem_share_client(struct inpcbinfo
*pcbinfo
, u_char owner_id
)
1407 if (pcbinfo
->all_owners
& owner_id
) {
1408 pcbinfo
->all_owners
&= ~owner_id
;
1409 for (inp
= pcbinfo
->listhead
->lh_first
; inp
!= NULL
; inp
= inp
->inp_list
.le_next
) {
1410 if (inp
->nat_owner
& owner_id
) {
1411 if (inp
->nat_owner
== owner_id
)
1413 * Deallocate the pcb
1417 inp
->nat_owner
&= ~owner_id
;
1430 void in_pcb_nat_init(struct inpcbinfo
*pcbinfo
, int afamily
,
1431 int pfamily
, int protocol
)
1433 bzero(&pcbinfo
->nat_dummy_socket
, sizeof(struct socket
));
1434 pcbinfo
->nat_dummy_socket
.so_proto
= pffindproto(afamily
, pfamily
, protocol
);
1435 pcbinfo
->all_owners
= 0;
1440 prison_xinpcb(struct proc
*p
, struct inpcb
*inp
)
1444 if (ntohl(inp
->inp_laddr
.s_addr
) == p
->p_prison
->pr_ip
)