2 * Copyright (c) 1982, 1986, 1991, 1993, 1995
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)in_pcb.c 8.4 (Berkeley) 5/24/95
34 * $FreeBSD: src/sys/netinet/in_pcb.c,v 1.59.2.26 2003/01/24 05:11:33 sam Exp $
37 #include "opt_ipsec.h"
38 #include "opt_inet6.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/malloc.h>
44 #include <sys/domain.h>
45 #include <sys/protosw.h>
46 #include <sys/socket.h>
47 #include <sys/socketvar.h>
50 #include <sys/kernel.h>
51 #include <sys/sysctl.h>
53 #include <machine/limits.h>
55 #include <vm/vm_zone.h>
58 #include <net/if_types.h>
59 #include <net/route.h>
61 #include <netinet/in.h>
62 #include <netinet/in_pcb.h>
63 #include <netinet/in_var.h>
64 #include <netinet/ip_var.h>
66 #include <netinet/ip6.h>
67 #include <netinet6/ip6_var.h>
71 #include <netinet6/ipsec.h>
72 #include <netkey/key.h>
76 #if defined(IPSEC) || defined(IPSEC_ESP)
77 #error "Bad idea: don't compile with both IPSEC and FAST_IPSEC!"
80 #include <netipsec/ipsec.h>
81 #include <netipsec/key.h>
83 #endif /* FAST_IPSEC */
85 struct in_addr zeroin_addr;
88 * These configure the range of local port addresses assigned to
89 * "unspecified" outgoing connections/packets/whatever.
91 int ipport_lowfirstauto = IPPORT_RESERVED - 1; /* 1023 */
92 int ipport_lowlastauto = IPPORT_RESERVEDSTART; /* 600 */
93 int ipport_firstauto = IPPORT_RESERVED; /* 1024 */
94 int ipport_lastauto = IPPORT_USERRESERVED; /* 5000 */
95 int ipport_hifirstauto = IPPORT_HIFIRSTAUTO; /* 49152 */
96 int ipport_hilastauto = IPPORT_HILASTAUTO; /* 65535 */
98 #define RANGECHK(var, min, max) \
99 if ((var) < (min)) { (var) = (min); } \
100 else if ((var) > (max)) { (var) = (max); }
103 sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS)
105 int error = sysctl_handle_int(oidp,
106 oidp->oid_arg1, oidp->oid_arg2, req);
108 RANGECHK(ipport_lowfirstauto, 1, IPPORT_RESERVED - 1);
109 RANGECHK(ipport_lowlastauto, 1, IPPORT_RESERVED - 1);
110 RANGECHK(ipport_firstauto, IPPORT_RESERVED, USHRT_MAX);
111 RANGECHK(ipport_lastauto, IPPORT_RESERVED, USHRT_MAX);
112 RANGECHK(ipport_hifirstauto, IPPORT_RESERVED, USHRT_MAX);
113 RANGECHK(ipport_hilastauto, IPPORT_RESERVED, USHRT_MAX);
120 SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0, "IP Ports");
122 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst, CTLTYPE_INT|CTLFLAG_RW,
123 &ipport_lowfirstauto, 0, &sysctl_net_ipport_check, "I", "");
124 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast, CTLTYPE_INT|CTLFLAG_RW,
125 &ipport_lowlastauto, 0, &sysctl_net_ipport_check, "I", "");
126 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first, CTLTYPE_INT|CTLFLAG_RW,
127 &ipport_firstauto, 0, &sysctl_net_ipport_check, "I", "");
128 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last, CTLTYPE_INT|CTLFLAG_RW,
129 &ipport_lastauto, 0, &sysctl_net_ipport_check, "I", "");
130 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst, CTLTYPE_INT|CTLFLAG_RW,
131 &ipport_hifirstauto, 0, &sysctl_net_ipport_check, "I", "");
132 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast, CTLTYPE_INT|CTLFLAG_RW,
133 &ipport_hilastauto, 0, &sysctl_net_ipport_check, "I", "");
136 * in_pcb.c: manage the Protocol Control Blocks.
138 * NOTE: It is assumed that most of these functions will be called at
139 * splnet(). XXX - There are, unfortunately, a few exceptions to this
140 * rule that should be fixed.
144 * Allocate a PCB and associate it with the socket.
147 in_pcballoc(so, pcbinfo, p)
149 struct inpcbinfo *pcbinfo;
152 register struct inpcb *inp;
157 inp = zalloci(pcbinfo->ipi_zone);
160 bzero((caddr_t)inp, sizeof(*inp));
161 inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
162 inp->inp_pcbinfo = pcbinfo;
163 inp->inp_socket = so;
165 error = ipsec_init_policy(so, &inp->inp_sp);
167 zfreei(pcbinfo->ipi_zone, inp);
172 if (INP_SOCKAF(so) == AF_INET6 && ip6_v6only)
173 inp->inp_flags |= IN6P_IPV6_V6ONLY;
175 LIST_INSERT_HEAD(pcbinfo->listhead, inp, inp_list);
176 pcbinfo->ipi_count++;
177 so->so_pcb = (caddr_t)inp;
179 if (ip6_auto_flowlabel)
180 inp->inp_flags |= IN6P_AUTOFLOWLABEL;
186 in_pcbbind(inp, nam, p)
187 register struct inpcb *inp;
188 struct sockaddr *nam;
191 register struct socket *so = inp->inp_socket;
192 unsigned short *lastport;
193 struct sockaddr_in *sin;
194 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
196 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
197 int error, prison = 0;
199 if (TAILQ_EMPTY(&in_ifaddrhead)) /* XXX broken! */
200 return (EADDRNOTAVAIL);
201 if (inp->inp_lport || inp->inp_laddr.s_addr != INADDR_ANY)
203 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
206 sin = (struct sockaddr_in *)nam;
207 if (nam->sa_len != sizeof (*sin))
211 * We should check the family, but old programs
212 * incorrectly fail to initialize it.
214 if (sin->sin_family != AF_INET)
215 return (EAFNOSUPPORT);
217 if (sin->sin_addr.s_addr != INADDR_ANY)
218 if (prison_ip(p, 0, &sin->sin_addr.s_addr))
220 lport = sin->sin_port;
221 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
223 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
224 * allow complete duplication of binding if
225 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
226 * and a multicast address is bound on both
227 * new and duplicated sockets.
229 if (so->so_options & SO_REUSEADDR)
230 reuseport = SO_REUSEADDR|SO_REUSEPORT;
231 } else if (sin->sin_addr.s_addr != INADDR_ANY) {
232 sin->sin_port = 0; /* yech... */
233 bzero(&sin->sin_zero, sizeof(sin->sin_zero));
234 if (ifa_ifwithaddr((struct sockaddr *)sin) == 0)
235 return (EADDRNOTAVAIL);
241 if (ntohs(lport) < IPPORT_RESERVED && p &&
242 suser_xxx(0, p, PRISON_ROOT))
244 if (p && p->p_prison)
246 if (so->so_cred->cr_uid != 0 &&
247 !IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
248 t = in_pcblookup_local(inp->inp_pcbinfo,
249 sin->sin_addr, lport,
250 prison ? 0 : INPLOOKUP_WILDCARD);
252 (ntohl(sin->sin_addr.s_addr) != INADDR_ANY ||
253 ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
254 (t->inp_socket->so_options &
255 SO_REUSEPORT) == 0) &&
256 (so->so_cred->cr_uid !=
257 t->inp_socket->so_cred->cr_uid)) {
259 if (ntohl(sin->sin_addr.s_addr) !=
261 ntohl(t->inp_laddr.s_addr) !=
264 INP_SOCKAF(t->inp_socket))
265 #endif /* defined(INET6) */
270 prison_ip(p, 0, &sin->sin_addr.s_addr))
271 return (EADDRNOTAVAIL);
272 t = in_pcblookup_local(pcbinfo, sin->sin_addr,
273 lport, prison ? 0 : wild);
275 (reuseport & t->inp_socket->so_options) == 0) {
277 if (ntohl(sin->sin_addr.s_addr) !=
279 ntohl(t->inp_laddr.s_addr) !=
282 INP_SOCKAF(t->inp_socket))
283 #endif /* defined(INET6) */
287 inp->inp_laddr = sin->sin_addr;
293 if (inp->inp_laddr.s_addr != INADDR_ANY)
294 if (prison_ip(p, 0, &inp->inp_laddr.s_addr )) {
295 inp->inp_laddr.s_addr = INADDR_ANY;
298 inp->inp_flags |= INP_ANONPORT;
300 if (inp->inp_flags & INP_HIGHPORT) {
301 first = ipport_hifirstauto; /* sysctl */
302 last = ipport_hilastauto;
303 lastport = &pcbinfo->lasthi;
304 } else if (inp->inp_flags & INP_LOWPORT) {
305 if (p && (error = suser_xxx(0, p, PRISON_ROOT))) {
306 inp->inp_laddr.s_addr = INADDR_ANY;
309 first = ipport_lowfirstauto; /* 1023 */
310 last = ipport_lowlastauto; /* 600 */
311 lastport = &pcbinfo->lastlow;
313 first = ipport_firstauto; /* sysctl */
314 last = ipport_lastauto;
315 lastport = &pcbinfo->lastport;
318 * Simple check to ensure all ports are not used up causing
321 * We split the two cases (up and down) so that the direction
322 * is not being tested on each round of the loop.
328 count = first - last;
331 if (count-- < 0) { /* completely used? */
332 inp->inp_laddr.s_addr = INADDR_ANY;
333 return (EADDRNOTAVAIL);
336 if (*lastport > first || *lastport < last)
338 lport = htons(*lastport);
339 } while (in_pcblookup_local(pcbinfo,
340 inp->inp_laddr, lport, wild));
345 count = last - first;
348 if (count-- < 0) { /* completely used? */
349 inp->inp_laddr.s_addr = INADDR_ANY;
350 return (EADDRNOTAVAIL);
353 if (*lastport < first || *lastport > last)
355 lport = htons(*lastport);
356 } while (in_pcblookup_local(pcbinfo,
357 inp->inp_laddr, lport, wild));
360 inp->inp_lport = lport;
361 if (prison_ip(p, 0, &inp->inp_laddr.s_addr)) {
362 inp->inp_laddr.s_addr = INADDR_ANY;
366 if (in_pcbinshash(inp) != 0) {
367 inp->inp_laddr.s_addr = INADDR_ANY;
375 * Transform old in_pcbconnect() into an inner subroutine for new
376 * in_pcbconnect(): Do some validity-checking on the remote
377 * address (in mbuf 'nam') and then determine local host address
378 * (i.e., which interface) to use to access that remote host.
380 * This preserves definition of in_pcbconnect(), while supporting a
381 * slightly different version for T/TCP. (This is more than
382 * a bit of a kludge, but cleaning up the internal interfaces would
383 * have forced minor changes in every protocol).
387 in_pcbladdr(inp, nam, plocal_sin)
388 register struct inpcb *inp;
389 struct sockaddr *nam;
390 struct sockaddr_in **plocal_sin;
392 struct in_ifaddr *ia;
393 register struct sockaddr_in *sin = (struct sockaddr_in *)nam;
395 if (nam->sa_len != sizeof (*sin))
397 if (sin->sin_family != AF_INET)
398 return (EAFNOSUPPORT);
399 if (sin->sin_port == 0)
400 return (EADDRNOTAVAIL);
401 if (!TAILQ_EMPTY(&in_ifaddrhead)) {
403 * If the destination address is INADDR_ANY,
404 * use the primary local address.
405 * If the supplied address is INADDR_BROADCAST,
406 * and the primary interface supports broadcast,
407 * choose the broadcast address for that interface.
409 if (sin->sin_addr.s_addr == INADDR_ANY)
410 sin->sin_addr = IA_SIN(TAILQ_FIRST(&in_ifaddrhead))->sin_addr;
411 else if (sin->sin_addr.s_addr == (u_long)INADDR_BROADCAST &&
412 (TAILQ_FIRST(&in_ifaddrhead)->ia_ifp->if_flags & IFF_BROADCAST))
413 sin->sin_addr = satosin(&TAILQ_FIRST(&in_ifaddrhead)->ia_broadaddr)->sin_addr;
415 if (inp->inp_laddr.s_addr == INADDR_ANY) {
416 register struct route *ro;
418 ia = (struct in_ifaddr *)0;
420 * If route is known or can be allocated now,
421 * our src addr is taken from the i/f, else punt.
422 * Note that we should check the address family of the cached
423 * destination, in case of sharing the cache with IPv6.
425 ro = &inp->inp_route;
427 (ro->ro_dst.sa_family != AF_INET ||
428 satosin(&ro->ro_dst)->sin_addr.s_addr !=
429 sin->sin_addr.s_addr ||
430 inp->inp_socket->so_options & SO_DONTROUTE)) {
432 ro->ro_rt = (struct rtentry *)0;
434 if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0 && /*XXX*/
435 (ro->ro_rt == (struct rtentry *)0 ||
436 ro->ro_rt->rt_ifp == (struct ifnet *)0)) {
437 /* No route yet, so try to acquire one */
438 bzero(&ro->ro_dst, sizeof(struct sockaddr_in));
439 ro->ro_dst.sa_family = AF_INET;
440 ro->ro_dst.sa_len = sizeof(struct sockaddr_in);
441 ((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
446 * If we found a route, use the address
447 * corresponding to the outgoing interface
448 * unless it is the loopback (in case a route
449 * to our address on another net goes to loopback).
451 if (ro->ro_rt && !(ro->ro_rt->rt_ifp->if_flags & IFF_LOOPBACK))
452 ia = ifatoia(ro->ro_rt->rt_ifa);
454 u_short fport = sin->sin_port;
457 ia = ifatoia(ifa_ifwithdstaddr(sintosa(sin)));
459 ia = ifatoia(ifa_ifwithnet(sintosa(sin)));
460 sin->sin_port = fport;
462 ia = TAILQ_FIRST(&in_ifaddrhead);
464 return (EADDRNOTAVAIL);
467 * If the destination address is multicast and an outgoing
468 * interface has been set as a multicast option, use the
469 * address of that interface as our source address.
471 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
472 inp->inp_moptions != NULL) {
473 struct ip_moptions *imo;
476 imo = inp->inp_moptions;
477 if (imo->imo_multicast_ifp != NULL) {
478 ifp = imo->imo_multicast_ifp;
479 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
480 if (ia->ia_ifp == ifp)
483 return (EADDRNOTAVAIL);
487 * Don't do pcblookup call here; return interface in plocal_sin
488 * and exit to caller, that will do the lookup.
490 *plocal_sin = &ia->ia_addr;
498 * Connect from a socket to a specified address.
499 * Both address and port must be specified in argument sin.
500 * If don't have a local address for this socket yet,
504 in_pcbconnect(inp, nam, p)
505 register struct inpcb *inp;
506 struct sockaddr *nam;
509 struct sockaddr_in *ifaddr;
510 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
511 struct sockaddr_in sa;
514 if (inp->inp_laddr.s_addr == INADDR_ANY && p->p_prison != NULL) {
515 bzero(&sa, sizeof (sa));
516 sa.sin_addr.s_addr = htonl(p->p_prison->pr_ip);
517 sa.sin_len=sizeof (sa);
518 sa.sin_family = AF_INET;
519 error = in_pcbbind(inp, (struct sockaddr *)&sa, p);
524 * Call inner routine, to assign local interface address.
526 if ((error = in_pcbladdr(inp, nam, &ifaddr)) != 0)
529 if (in_pcblookup_hash(inp->inp_pcbinfo, sin->sin_addr, sin->sin_port,
530 inp->inp_laddr.s_addr ? inp->inp_laddr : ifaddr->sin_addr,
531 inp->inp_lport, 0, NULL) != NULL) {
534 if (inp->inp_laddr.s_addr == INADDR_ANY) {
535 if (inp->inp_lport == 0) {
536 error = in_pcbbind(inp, (struct sockaddr *)0, p);
540 inp->inp_laddr = ifaddr->sin_addr;
542 inp->inp_faddr = sin->sin_addr;
543 inp->inp_fport = sin->sin_port;
549 in_pcbdisconnect(inp)
553 inp->inp_faddr.s_addr = INADDR_ANY;
556 if (inp->inp_socket->so_state & SS_NOFDREF)
564 struct socket *so = inp->inp_socket;
565 struct inpcbinfo *ipi = inp->inp_pcbinfo;
568 ipsec4_delete_pcbpolicy(inp);
570 inp->inp_gencnt = ++ipi->ipi_gencnt;
574 if (inp->inp_options)
575 (void)m_free(inp->inp_options);
576 if (inp->inp_route.ro_rt)
577 rtfree(inp->inp_route.ro_rt);
578 ip_freemoptions(inp->inp_moptions);
580 zfreei(ipi->ipi_zone, inp);
584 * The calling convention of in_setsockaddr() and in_setpeeraddr() was
585 * modified to match the pru_sockaddr() and pru_peeraddr() entry points
586 * in struct pr_usrreqs, so that protocols can just reference then directly
587 * without the need for a wrapper function. The socket must have a valid
588 * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
589 * except through a kernel programming error, so it is acceptable to panic
590 * (or in this case trap) if the PCB is invalid. (Actually, we don't trap
591 * because there actually /is/ a programming error somewhere... XXX)
594 in_setsockaddr(so, nam)
596 struct sockaddr **nam;
599 register struct inpcb *inp;
600 register struct sockaddr_in *sin;
603 * Do the malloc first in case it blocks.
605 MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
607 sin->sin_family = AF_INET;
608 sin->sin_len = sizeof(*sin);
617 sin->sin_port = inp->inp_lport;
618 sin->sin_addr = inp->inp_laddr;
621 *nam = (struct sockaddr *)sin;
626 in_setpeeraddr(so, nam)
628 struct sockaddr **nam;
632 register struct sockaddr_in *sin;
635 * Do the malloc first in case it blocks.
637 MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
639 sin->sin_family = AF_INET;
640 sin->sin_len = sizeof(*sin);
649 sin->sin_port = inp->inp_fport;
650 sin->sin_addr = inp->inp_faddr;
653 *nam = (struct sockaddr *)sin;
658 in_pcbnotifyall(head, faddr, errno, notify)
659 struct inpcbhead *head;
660 struct in_addr faddr;
661 void (*notify) __P((struct inpcb *, int));
663 struct inpcb *inp, *ninp;
667 for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) {
668 ninp = LIST_NEXT(inp, inp_list);
670 if ((inp->inp_vflag & INP_IPV4) == 0)
673 if (inp->inp_faddr.s_addr != faddr.s_addr ||
674 inp->inp_socket == NULL)
676 (*notify)(inp, errno);
682 in_pcbpurgeif0(head, ifp)
687 struct ip_moptions *imo;
690 for (inp = head; inp != NULL; inp = LIST_NEXT(inp, inp_list)) {
691 imo = inp->inp_moptions;
692 if ((inp->inp_vflag & INP_IPV4) &&
695 * Unselect the outgoing interface if it is being
698 if (imo->imo_multicast_ifp == ifp)
699 imo->imo_multicast_ifp = NULL;
702 * Drop multicast group membership if we joined
703 * through the interface being detached.
705 for (i = 0, gap = 0; i < imo->imo_num_memberships;
707 if (imo->imo_membership[i]->inm_ifp == ifp) {
708 in_delmulti(imo->imo_membership[i]);
711 imo->imo_membership[i - gap] =
712 imo->imo_membership[i];
714 imo->imo_num_memberships -= gap;
720 * Check for alternatives when higher level complains
721 * about service problems. For now, invalidate cached
722 * routing information. If the route was created dynamically
723 * (by a redirect), time to try a default gateway again.
729 register struct rtentry *rt;
730 struct rt_addrinfo info;
732 if ((rt = inp->inp_route.ro_rt)) {
733 bzero((caddr_t)&info, sizeof(info));
734 info.rti_flags = rt->rt_flags;
735 info.rti_info[RTAX_DST] = rt_key(rt);
736 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
737 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
738 rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
739 if (rt->rt_flags & RTF_DYNAMIC)
740 (void) rtrequest1(RTM_DELETE, &info, NULL);
741 inp->inp_route.ro_rt = NULL;
744 * A new route can be allocated
745 * the next time output is attempted.
751 * After a routing change, flush old routing
752 * and allocate a (hopefully) better one.
755 in_rtchange(inp, errno)
756 register struct inpcb *inp;
759 if (inp->inp_route.ro_rt) {
760 rtfree(inp->inp_route.ro_rt);
761 inp->inp_route.ro_rt = 0;
763 * A new route can be allocated the next time
764 * output is attempted.
770 * Lookup a PCB based on the local address and port.
773 in_pcblookup_local(pcbinfo, laddr, lport_arg, wild_okay)
774 struct inpcbinfo *pcbinfo;
775 struct in_addr laddr;
779 register struct inpcb *inp;
780 int matchwild = 3, wildcard;
781 u_short lport = lport_arg;
784 struct inpcbhead *head;
786 * Look for an unconnected (wildcard foreign addr) PCB that
787 * matches the local address and port we're looking for.
789 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, pcbinfo->hashmask)];
790 LIST_FOREACH(inp, head, inp_hash) {
792 if ((inp->inp_vflag & INP_IPV4) == 0)
795 if (inp->inp_faddr.s_addr == INADDR_ANY &&
796 inp->inp_laddr.s_addr == laddr.s_addr &&
797 inp->inp_lport == lport) {
809 struct inpcbporthead *porthash;
810 struct inpcbport *phd;
811 struct inpcb *match = NULL;
813 * Best fit PCB lookup.
815 * First see if this local port is in use by looking on the
818 porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport,
819 pcbinfo->porthashmask)];
820 LIST_FOREACH(phd, porthash, phd_hash) {
821 if (phd->phd_port == lport)
826 * Port is in use by one or more PCBs. Look for best
829 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
832 if ((inp->inp_vflag & INP_IPV4) == 0)
835 if (inp->inp_faddr.s_addr != INADDR_ANY)
837 if (inp->inp_laddr.s_addr != INADDR_ANY) {
838 if (laddr.s_addr == INADDR_ANY)
840 else if (inp->inp_laddr.s_addr != laddr.s_addr)
843 if (laddr.s_addr != INADDR_ANY)
846 if (wildcard < matchwild) {
848 matchwild = wildcard;
849 if (matchwild == 0) {
860 * Lookup PCB in hash list.
863 in_pcblookup_hash(pcbinfo, faddr, fport_arg, laddr, lport_arg, wildcard,
865 struct inpcbinfo *pcbinfo;
866 struct in_addr faddr, laddr;
867 u_int fport_arg, lport_arg;
871 struct inpcbhead *head;
872 register struct inpcb *inp;
873 u_short fport = fport_arg, lport = lport_arg;
876 * First look for an exact match.
878 head = &pcbinfo->hashbase[INP_PCBHASH(faddr.s_addr, lport, fport, pcbinfo->hashmask)];
879 LIST_FOREACH(inp, head, inp_hash) {
881 if ((inp->inp_vflag & INP_IPV4) == 0)
884 if (inp->inp_faddr.s_addr == faddr.s_addr &&
885 inp->inp_laddr.s_addr == laddr.s_addr &&
886 inp->inp_fport == fport &&
887 inp->inp_lport == lport) {
895 struct inpcb *local_wild = NULL;
897 struct inpcb *local_wild_mapped = NULL;
898 #endif /* defined(INET6) */
900 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, pcbinfo->hashmask)];
901 LIST_FOREACH(inp, head, inp_hash) {
903 if ((inp->inp_vflag & INP_IPV4) == 0)
906 if (inp->inp_faddr.s_addr == INADDR_ANY &&
907 inp->inp_lport == lport) {
908 if (ifp && ifp->if_type == IFT_FAITH &&
909 (inp->inp_flags & INP_FAITH) == 0)
911 if (inp->inp_laddr.s_addr == laddr.s_addr)
913 else if (inp->inp_laddr.s_addr == INADDR_ANY) {
915 if (INP_CHECK_SOCKAF(inp->inp_socket,
917 local_wild_mapped = inp;
919 #endif /* defined(INET6) */
925 if (local_wild == NULL)
926 return (local_wild_mapped);
927 #endif /* defined(INET6) */
938 * Insert PCB onto various hash lists.
944 struct inpcbhead *pcbhash;
945 struct inpcbporthead *pcbporthash;
946 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
947 struct inpcbport *phd;
948 u_int32_t hashkey_faddr;
951 if (inp->inp_vflag & INP_IPV6)
952 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
955 hashkey_faddr = inp->inp_faddr.s_addr;
957 pcbhash = &pcbinfo->hashbase[INP_PCBHASH(hashkey_faddr,
958 inp->inp_lport, inp->inp_fport, pcbinfo->hashmask)];
960 pcbporthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(inp->inp_lport,
961 pcbinfo->porthashmask)];
964 * Go through port list and look for a head for this lport.
966 LIST_FOREACH(phd, pcbporthash, phd_hash) {
967 if (phd->phd_port == inp->inp_lport)
971 * If none exists, malloc one and tack it on.
974 MALLOC(phd, struct inpcbport *, sizeof(struct inpcbport), M_PCB, M_NOWAIT);
976 return (ENOBUFS); /* XXX */
978 phd->phd_port = inp->inp_lport;
979 LIST_INIT(&phd->phd_pcblist);
980 LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
983 LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
984 LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
989 * Move PCB to the proper hash bucket when { faddr, fport } have been
990 * changed. NOTE: This does not handle the case of the lport changing (the
991 * hashed port list would have to be updated as well), so the lport must
992 * not change after in_pcbinshash() has been called.
998 struct inpcbhead *head;
999 u_int32_t hashkey_faddr;
1002 if (inp->inp_vflag & INP_IPV6)
1003 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1006 hashkey_faddr = inp->inp_faddr.s_addr;
1008 head = &inp->inp_pcbinfo->hashbase[INP_PCBHASH(hashkey_faddr,
1009 inp->inp_lport, inp->inp_fport, inp->inp_pcbinfo->hashmask)];
1011 LIST_REMOVE(inp, inp_hash);
1012 LIST_INSERT_HEAD(head, inp, inp_hash);
1016 * Remove PCB from various lists.
1022 inp->inp_gencnt = ++inp->inp_pcbinfo->ipi_gencnt;
1023 if (inp->inp_lport) {
1024 struct inpcbport *phd = inp->inp_phd;
1026 LIST_REMOVE(inp, inp_hash);
1027 LIST_REMOVE(inp, inp_portlist);
1028 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1029 LIST_REMOVE(phd, phd_hash);
1033 LIST_REMOVE(inp, inp_list);
1034 inp->inp_pcbinfo->ipi_count--;
1038 prison_xinpcb(struct proc *p, struct inpcb *inp)
1042 if (ntohl(inp->inp_laddr.s_addr) == p->p_prison->pr_ip)