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.27 2004/01/02 04:06:42 ambrisko Exp $
35 * $DragonFly: src/sys/netinet/in_pcb.c,v 1.10 2004/02/27 09:40:36 hsu Exp $
38 #include "opt_ipsec.h"
39 #include "opt_inet6.h"
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/malloc.h>
45 #include <sys/domain.h>
46 #include <sys/protosw.h>
47 #include <sys/socket.h>
48 #include <sys/socketvar.h>
51 #include <sys/kernel.h>
52 #include <sys/sysctl.h>
54 #include <machine/limits.h>
56 #include <vm/vm_zone.h>
59 #include <net/if_types.h>
60 #include <net/route.h>
62 #include <netinet/in.h>
63 #include <netinet/in_pcb.h>
64 #include <netinet/in_var.h>
65 #include <netinet/ip_var.h>
67 #include <netinet/ip6.h>
68 #include <netinet6/ip6_var.h>
72 #include <netinet6/ipsec.h>
73 #include <netproto/key/key.h>
77 #if defined(IPSEC) || defined(IPSEC_ESP)
78 #error "Bad idea: don't compile with both IPSEC and FAST_IPSEC!"
81 #include <netipsec/ipsec.h>
82 #include <netipsec/key.h>
84 #endif /* FAST_IPSEC */
86 struct in_addr zeroin_addr;
89 * These configure the range of local port addresses assigned to
90 * "unspecified" outgoing connections/packets/whatever.
92 int ipport_lowfirstauto = IPPORT_RESERVED - 1; /* 1023 */
93 int ipport_lowlastauto = IPPORT_RESERVEDSTART; /* 600 */
95 int ipport_firstauto = IPPORT_RESERVED; /* 1024 */
96 int ipport_lastauto = IPPORT_USERRESERVED; /* 5000 */
98 int ipport_hifirstauto = IPPORT_HIFIRSTAUTO; /* 49152 */
99 int ipport_hilastauto = IPPORT_HILASTAUTO; /* 65535 */
102 RANGECHK(int var, int min, int max)
111 sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS)
115 error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
117 RANGECHK(ipport_lowfirstauto, 1, IPPORT_RESERVED - 1);
118 RANGECHK(ipport_lowlastauto, 1, IPPORT_RESERVED - 1);
119 RANGECHK(ipport_firstauto, IPPORT_RESERVED, USHRT_MAX);
120 RANGECHK(ipport_lastauto, IPPORT_RESERVED, USHRT_MAX);
121 RANGECHK(ipport_hifirstauto, IPPORT_RESERVED, USHRT_MAX);
122 RANGECHK(ipport_hilastauto, IPPORT_RESERVED, USHRT_MAX);
127 SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0, "IP Ports");
129 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst, CTLTYPE_INT|CTLFLAG_RW,
130 &ipport_lowfirstauto, 0, &sysctl_net_ipport_check, "I", "");
131 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast, CTLTYPE_INT|CTLFLAG_RW,
132 &ipport_lowlastauto, 0, &sysctl_net_ipport_check, "I", "");
133 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first, CTLTYPE_INT|CTLFLAG_RW,
134 &ipport_firstauto, 0, &sysctl_net_ipport_check, "I", "");
135 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last, CTLTYPE_INT|CTLFLAG_RW,
136 &ipport_lastauto, 0, &sysctl_net_ipport_check, "I", "");
137 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst, CTLTYPE_INT|CTLFLAG_RW,
138 &ipport_hifirstauto, 0, &sysctl_net_ipport_check, "I", "");
139 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast, CTLTYPE_INT|CTLFLAG_RW,
140 &ipport_hilastauto, 0, &sysctl_net_ipport_check, "I", "");
143 * in_pcb.c: manage the Protocol Control Blocks.
145 * NOTE: It is assumed that most of these functions will be called at
146 * splnet(). XXX - There are, unfortunately, a few exceptions to this
147 * rule that should be fixed.
151 * Allocate a PCB and associate it with the socket.
154 in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo, struct thread *td)
161 inp = zalloc(pcbinfo->ipi_zone);
164 bzero((caddr_t)inp, sizeof *inp);
165 inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
166 inp->inp_pcbinfo = pcbinfo;
167 inp->inp_socket = so;
169 error = ipsec_init_policy(so, &inp->inp_sp);
171 zfree(pcbinfo->ipi_zone, inp);
176 if (INP_SOCKAF(so) == AF_INET6 && ip6_v6only)
177 inp->inp_flags |= IN6P_IPV6_V6ONLY;
178 if (ip6_auto_flowlabel)
179 inp->inp_flags |= IN6P_AUTOFLOWLABEL;
181 LIST_INSERT_HEAD(pcbinfo->listhead, inp, inp_list);
182 pcbinfo->ipi_count++;
183 so->so_pcb = (caddr_t)inp;
188 in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct thread *td)
190 struct socket *so = inp->inp_socket;
191 struct proc *p = td->td_proc;
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;
201 if (TAILQ_EMPTY(&in_ifaddrhead)) /* XXX broken! */
202 return (EADDRNOTAVAIL);
203 if (inp->inp_lport || inp->inp_laddr.s_addr != INADDR_ANY)
205 if (!(so->so_options & (SO_REUSEADDR|SO_REUSEPORT)))
208 sin = (struct sockaddr_in *)nam;
209 if (nam->sa_len != sizeof *sin)
213 * We should check the family, but old programs
214 * incorrectly fail to initialize it.
216 if (sin->sin_family != AF_INET)
217 return (EAFNOSUPPORT);
219 if (sin->sin_addr.s_addr != INADDR_ANY)
220 if (prison_ip(td, 0, &sin->sin_addr.s_addr))
222 lport = sin->sin_port;
223 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
225 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
226 * allow complete duplication of binding if
227 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
228 * and a multicast address is bound on both
229 * new and duplicated sockets.
231 if (so->so_options & SO_REUSEADDR)
232 reuseport = SO_REUSEADDR|SO_REUSEPORT;
233 } else if (sin->sin_addr.s_addr != INADDR_ANY) {
234 sin->sin_port = 0; /* yech... */
235 bzero(&sin->sin_zero, sizeof sin->sin_zero);
236 if (ifa_ifwithaddr((struct sockaddr *)sin) == NULL)
237 return (EADDRNOTAVAIL);
243 if (ntohs(lport) < IPPORT_RESERVED && p &&
244 suser_cred(p->p_ucred, PRISON_ROOT))
246 if (p && p->p_ucred->cr_prison)
248 if (so->so_cred->cr_uid != 0 &&
249 !IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
250 t = in_pcblookup_local(inp->inp_pcbinfo,
251 sin->sin_addr, lport,
252 prison ? 0 : INPLOOKUP_WILDCARD);
254 (!in_nullhost(sin->sin_addr) ||
255 !in_nullhost(t->inp_laddr) ||
256 (t->inp_socket->so_options &
257 SO_REUSEPORT) == 0) &&
258 (so->so_cred->cr_uid !=
259 t->inp_socket->so_cred->cr_uid)) {
261 if (!in_nullhost(sin->sin_addr) ||
262 !in_nullhost(t->inp_laddr) ||
264 INP_SOCKAF(t->inp_socket))
269 if (prison && prison_ip(td, 0, &sin->sin_addr.s_addr))
270 return (EADDRNOTAVAIL);
271 t = in_pcblookup_local(pcbinfo, sin->sin_addr,
272 lport, prison ? 0 : wild);
273 if (t && !(reuseport & t->inp_socket->so_options)) {
275 if (!in_nullhost(sin->sin_addr) ||
276 !in_nullhost(t->inp_laddr) ||
277 INP_SOCKAF(so) == INP_SOCKAF(t->inp_socket))
282 inp->inp_laddr = sin->sin_addr;
288 if (inp->inp_laddr.s_addr != INADDR_ANY)
289 if (prison_ip(td, 0, &inp->inp_laddr.s_addr )) {
290 inp->inp_laddr.s_addr = INADDR_ANY;
293 inp->inp_flags |= INP_ANONPORT;
295 if (inp->inp_flags & INP_HIGHPORT) {
296 first = ipport_hifirstauto; /* sysctl */
297 last = ipport_hilastauto;
298 lastport = &pcbinfo->lasthi;
299 } else if (inp->inp_flags & INP_LOWPORT) {
301 (error = suser_cred(p->p_ucred, PRISON_ROOT))) {
302 inp->inp_laddr.s_addr = INADDR_ANY;
305 first = ipport_lowfirstauto; /* 1023 */
306 last = ipport_lowlastauto; /* 600 */
307 lastport = &pcbinfo->lastlow;
309 first = ipport_firstauto; /* sysctl */
310 last = ipport_lastauto;
311 lastport = &pcbinfo->lastport;
314 * Simple check to ensure all ports are not used up causing
317 * We split the two cases (up and down) so that the direction
318 * is not being tested on each round of the loop.
324 count = first - last;
327 if (count-- < 0) { /* completely used? */
328 inp->inp_laddr.s_addr = INADDR_ANY;
329 return (EADDRNOTAVAIL);
332 if (*lastport > first || *lastport < last)
334 lport = htons(*lastport);
335 } while (in_pcblookup_local(pcbinfo,
336 inp->inp_laddr, lport, wild));
341 count = last - first;
344 if (count-- < 0) { /* completely used? */
345 inp->inp_laddr.s_addr = INADDR_ANY;
346 return (EADDRNOTAVAIL);
349 if (*lastport < first || *lastport > last)
351 lport = htons(*lastport);
352 } while (in_pcblookup_local(pcbinfo,
353 inp->inp_laddr, lport, wild));
356 inp->inp_lport = lport;
357 if (prison_ip(td, 0, &inp->inp_laddr.s_addr)) {
358 inp->inp_laddr.s_addr = INADDR_ANY;
362 if (in_pcbinshash(inp) != 0) {
363 inp->inp_laddr.s_addr = INADDR_ANY;
371 * Transform old in_pcbconnect() into an inner subroutine for new
372 * in_pcbconnect(): Do some validity-checking on the remote
373 * address (in mbuf 'nam') and then determine local host address
374 * (i.e., which interface) to use to access that remote host.
376 * This preserves definition of in_pcbconnect(), while supporting a
377 * slightly different version for T/TCP. (This is more than
378 * a bit of a kludge, but cleaning up the internal interfaces would
379 * have forced minor changes in every protocol).
383 in_pcbladdr(inp, nam, plocal_sin)
385 struct sockaddr *nam;
386 struct sockaddr_in **plocal_sin;
388 struct in_ifaddr *ia;
389 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
391 if (nam->sa_len != sizeof *sin)
393 if (sin->sin_family != AF_INET)
394 return (EAFNOSUPPORT);
395 if (sin->sin_port == 0)
396 return (EADDRNOTAVAIL);
397 if (!TAILQ_EMPTY(&in_ifaddrhead)) {
398 ia = TAILQ_FIRST(&in_ifaddrhead);
400 * If the destination address is INADDR_ANY,
401 * use the primary local address.
402 * If the supplied address is INADDR_BROADCAST,
403 * and the primary interface supports broadcast,
404 * choose the broadcast address for that interface.
406 if (sin->sin_addr.s_addr == INADDR_ANY)
407 sin->sin_addr = IA_SIN(ia)->sin_addr;
408 else if (sin->sin_addr.s_addr == (u_long)INADDR_BROADCAST &&
409 (ia->ia_ifp->if_flags & IFF_BROADCAST))
410 sin->sin_addr = satosin(&ia->ia_broadaddr)->sin_addr;
412 if (inp->inp_laddr.s_addr == INADDR_ANY) {
415 ia = (struct in_ifaddr *)NULL;
417 * If route is known or can be allocated now,
418 * our src addr is taken from the i/f, else punt.
419 * Note that we should check the address family of the cached
420 * destination, in case of sharing the cache with IPv6.
422 ro = &inp->inp_route;
424 (!(ro->ro_rt->rt_flags & RTF_UP) ||
425 ro->ro_dst.sa_family != AF_INET ||
426 satosin(&ro->ro_dst)->sin_addr.s_addr !=
427 sin->sin_addr.s_addr ||
428 inp->inp_socket->so_options & SO_DONTROUTE)) {
430 ro->ro_rt = (struct rtentry *)NULL;
432 if (!(inp->inp_socket->so_options & SO_DONTROUTE) && /*XXX*/
433 (ro->ro_rt == (struct rtentry *)NULL ||
434 ro->ro_rt->rt_ifp == (struct ifnet *)NULL)) {
435 /* No route yet, so try to acquire one */
436 bzero(&ro->ro_dst, sizeof(struct sockaddr_in));
437 ro->ro_dst.sa_family = AF_INET;
438 ro->ro_dst.sa_len = sizeof(struct sockaddr_in);
439 ((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
444 * If we found a route, use the address
445 * corresponding to the outgoing interface
446 * unless it is the loopback (in case a route
447 * to our address on another net goes to loopback).
449 if (ro->ro_rt && !(ro->ro_rt->rt_ifp->if_flags & IFF_LOOPBACK))
450 ia = ifatoia(ro->ro_rt->rt_ifa);
452 u_short fport = sin->sin_port;
455 ia = ifatoia(ifa_ifwithdstaddr(sintosa(sin)));
457 ia = ifatoia(ifa_ifwithnet(sintosa(sin)));
458 sin->sin_port = fport;
460 ia = TAILQ_FIRST(&in_ifaddrhead);
462 return (EADDRNOTAVAIL);
465 * If the destination address is multicast and an outgoing
466 * interface has been set as a multicast option, use the
467 * address of that interface as our source address.
469 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
470 inp->inp_moptions != NULL) {
471 struct ip_moptions *imo;
474 imo = inp->inp_moptions;
475 if (imo->imo_multicast_ifp != NULL) {
476 ifp = imo->imo_multicast_ifp;
477 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
478 if (ia->ia_ifp == ifp)
481 return (EADDRNOTAVAIL);
485 * Don't do pcblookup call here; return interface in plocal_sin
486 * and exit to caller, that will do the lookup.
488 *plocal_sin = &ia->ia_addr;
496 * Connect from a socket to a specified address.
497 * Both address and port must be specified in argument sin.
498 * If don't have a local address for this socket yet,
502 in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct thread *td)
504 struct sockaddr_in *ifaddr;
505 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
506 struct sockaddr_in sa;
507 struct ucred *cr = td->td_proc ? td->td_proc->p_ucred : NULL;
510 if (in_nullhost(inp->inp_laddr) && cr && cr->cr_prison != NULL) {
511 bzero(&sa, sizeof sa);
512 sa.sin_addr.s_addr = htonl(cr->cr_prison->pr_ip);
513 sa.sin_len = sizeof sa;
514 sa.sin_family = AF_INET;
515 error = in_pcbbind(inp, (struct sockaddr *)&sa, td);
520 /* Call inner routine to assign local interface address. */
521 if ((error = in_pcbladdr(inp, nam, &ifaddr)) != 0)
524 if (in_pcblookup_hash(inp->inp_pcbinfo, sin->sin_addr, sin->sin_port,
525 inp->inp_laddr.s_addr ? inp->inp_laddr : ifaddr->sin_addr,
526 inp->inp_lport, FALSE, NULL) != NULL) {
529 if (inp->inp_laddr.s_addr == INADDR_ANY) {
530 if (inp->inp_lport == 0) {
531 error = in_pcbbind(inp, (struct sockaddr *)0, td);
535 inp->inp_laddr = ifaddr->sin_addr;
537 inp->inp_faddr = sin->sin_addr;
538 inp->inp_fport = sin->sin_port;
544 in_pcbdisconnect(inp)
548 inp->inp_faddr.s_addr = INADDR_ANY;
551 if (inp->inp_socket->so_state & SS_NOFDREF)
559 struct socket *so = inp->inp_socket;
560 struct inpcbinfo *ipi = inp->inp_pcbinfo;
563 ipsec4_delete_pcbpolicy(inp);
565 inp->inp_gencnt = ++ipi->ipi_gencnt;
569 if (inp->inp_options)
570 (void)m_free(inp->inp_options);
571 if (inp->inp_route.ro_rt)
572 rtfree(inp->inp_route.ro_rt);
573 ip_freemoptions(inp->inp_moptions);
575 zfree(ipi->ipi_zone, inp);
579 * The calling convention of in_setsockaddr() and in_setpeeraddr() was
580 * modified to match the pru_sockaddr() and pru_peeraddr() entry points
581 * in struct pr_usrreqs, so that protocols can just reference then directly
582 * without the need for a wrapper function. The socket must have a valid
583 * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
584 * except through a kernel programming error, so it is acceptable to panic
585 * (or in this case trap) if the PCB is invalid. (Actually, we don't trap
586 * because there actually /is/ a programming error somewhere... XXX)
589 in_setsockaddr(so, nam)
591 struct sockaddr **nam;
595 struct sockaddr_in *sin;
598 * Do the malloc first in case it blocks.
600 MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
602 sin->sin_family = AF_INET;
603 sin->sin_len = sizeof *sin;
612 sin->sin_port = inp->inp_lport;
613 sin->sin_addr = inp->inp_laddr;
616 *nam = (struct sockaddr *)sin;
621 in_setpeeraddr(so, nam)
623 struct sockaddr **nam;
627 struct sockaddr_in *sin;
630 * Do the malloc first in case it blocks.
632 MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
634 sin->sin_family = AF_INET;
635 sin->sin_len = sizeof *sin;
644 sin->sin_port = inp->inp_fport;
645 sin->sin_addr = inp->inp_faddr;
648 *nam = (struct sockaddr *)sin;
653 in_pcbnotifyall(head, faddr, errno, notify)
654 struct inpcbhead *head;
655 struct in_addr faddr;
656 void (*notify) (struct inpcb *, int);
658 struct inpcb *inp, *ninp;
662 for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) {
663 ninp = LIST_NEXT(inp, inp_list);
665 if (!(inp->inp_vflag & INP_IPV4))
668 if (inp->inp_faddr.s_addr != faddr.s_addr ||
669 inp->inp_socket == NULL)
671 (*notify)(inp, errno);
677 in_pcbpurgeif0(head, ifp)
682 struct ip_moptions *imo;
685 for (inp = head; inp != NULL; inp = LIST_NEXT(inp, inp_list)) {
686 imo = inp->inp_moptions;
687 if ((inp->inp_vflag & INP_IPV4) && imo != NULL) {
689 * Unselect the outgoing interface if it is being
692 if (imo->imo_multicast_ifp == ifp)
693 imo->imo_multicast_ifp = NULL;
696 * Drop multicast group membership if we joined
697 * through the interface being detached.
699 for (i = 0, gap = 0; i < imo->imo_num_memberships;
701 if (imo->imo_membership[i]->inm_ifp == ifp) {
702 in_delmulti(imo->imo_membership[i]);
705 imo->imo_membership[i - gap] =
706 imo->imo_membership[i];
708 imo->imo_num_memberships -= gap;
714 * Check for alternatives when higher level complains
715 * about service problems. For now, invalidate cached
716 * routing information. If the route was created dynamically
717 * (by a redirect), time to try a default gateway again.
724 struct rt_addrinfo info;
726 if ((rt = inp->inp_route.ro_rt)) {
727 bzero((caddr_t)&info, sizeof info);
728 info.rti_flags = rt->rt_flags;
729 info.rti_info[RTAX_DST] = rt_key(rt);
730 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
731 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
732 rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
733 if (rt->rt_flags & RTF_DYNAMIC)
734 (void) rtrequest1(RTM_DELETE, &info, NULL);
735 inp->inp_route.ro_rt = NULL;
738 * A new route can be allocated
739 * the next time output is attempted.
745 * After a routing change, flush old routing
746 * and allocate a (hopefully) better one.
749 in_rtchange(inp, errno)
753 if (inp->inp_route.ro_rt) {
754 rtfree(inp->inp_route.ro_rt);
755 inp->inp_route.ro_rt = 0;
757 * A new route can be allocated the next time
758 * output is attempted.
764 * Lookup a PCB based on the local address and port.
767 in_pcblookup_local(pcbinfo, laddr, lport_arg, wild_okay)
768 struct inpcbinfo *pcbinfo;
769 struct in_addr laddr;
774 int matchwild = 3, wildcard;
775 u_short lport = lport_arg;
778 struct inpcbhead *head;
780 * Look for an unconnected (wildcard foreign addr) PCB that
781 * matches the local address and port we're looking for.
783 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, pcbinfo->hashmask)];
784 LIST_FOREACH(inp, head, inp_hash) {
786 if ((inp->inp_vflag & INP_IPV4) == 0)
789 if (inp->inp_faddr.s_addr == INADDR_ANY &&
790 inp->inp_laddr.s_addr == laddr.s_addr &&
791 inp->inp_lport == lport) {
803 struct inpcbporthead *porthash;
804 struct inpcbport *phd;
805 struct inpcb *match = NULL;
807 * Best fit PCB lookup.
809 * First see if this local port is in use by looking on the
812 porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport,
813 pcbinfo->porthashmask)];
814 LIST_FOREACH(phd, porthash, phd_hash) {
815 if (phd->phd_port == lport)
820 * Port is in use by one or more PCBs. Look for best
823 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
826 if ((inp->inp_vflag & INP_IPV4) == 0)
829 if (inp->inp_faddr.s_addr != INADDR_ANY)
831 if (inp->inp_laddr.s_addr != INADDR_ANY) {
832 if (laddr.s_addr == INADDR_ANY)
834 else if (inp->inp_laddr.s_addr != laddr.s_addr)
837 if (laddr.s_addr != INADDR_ANY)
840 if (wildcard < matchwild) {
842 matchwild = wildcard;
843 if (matchwild == 0) {
854 * Lookup PCB in hash list.
857 in_pcblookup_hash(pcbinfo, faddr, fport_arg, laddr, lport_arg, wildcard, ifp)
858 struct inpcbinfo *pcbinfo;
859 struct in_addr faddr, laddr;
860 u_int fport_arg, lport_arg;
864 struct inpcbhead *head;
866 u_short fport = fport_arg, lport = lport_arg;
869 * First look for an exact match.
871 head = &pcbinfo->hashbase[INP_PCBHASH(faddr.s_addr, lport, fport,
873 LIST_FOREACH(inp, head, inp_hash) {
875 if (!(inp->inp_vflag & INP_IPV4))
878 if (in_hosteq(inp->inp_faddr, faddr) &&
879 in_hosteq(inp->inp_laddr, laddr) &&
880 inp->inp_fport == fport && inp->inp_lport == lport) {
887 struct inpcb *local_wild = NULL;
889 struct inpcb *local_wild_mapped = NULL;
892 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0,
894 LIST_FOREACH(inp, head, inp_hash) {
896 if (!(inp->inp_vflag & INP_IPV4))
899 if (inp->inp_faddr.s_addr == INADDR_ANY &&
900 inp->inp_lport == lport) {
901 if (ifp && ifp->if_type == IFT_FAITH &&
902 !(inp->inp_flags & INP_FAITH))
904 if (inp->inp_laddr.s_addr == laddr.s_addr)
906 else if (inp->inp_laddr.s_addr == INADDR_ANY) {
908 if (INP_CHECK_SOCKAF(inp->inp_socket,
910 local_wild_mapped = inp;
918 if (local_wild == NULL)
919 return (local_wild_mapped);
931 * Insert PCB onto various hash lists.
937 struct inpcbhead *pcbhash;
938 struct inpcbporthead *pcbporthash;
939 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
940 struct inpcbport *phd;
941 u_int32_t hashkey_faddr;
944 if (inp->inp_vflag & INP_IPV6)
945 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
948 hashkey_faddr = inp->inp_faddr.s_addr;
950 pcbhash = &pcbinfo->hashbase[INP_PCBHASH(hashkey_faddr,
951 inp->inp_lport, inp->inp_fport, pcbinfo->hashmask)];
952 pcbporthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(inp->inp_lport,
953 pcbinfo->porthashmask)];
955 /* Go through port list and look for a head for this lport. */
956 LIST_FOREACH(phd, pcbporthash, phd_hash)
957 if (phd->phd_port == inp->inp_lport)
960 /* If none exists, malloc one and tack it on. */
962 MALLOC(phd, struct inpcbport *, sizeof(struct inpcbport),
965 return (ENOBUFS); /* XXX */
966 phd->phd_port = inp->inp_lport;
967 LIST_INIT(&phd->phd_pcblist);
968 LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
972 LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
973 LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
978 * Move PCB to the proper hash bucket when { faddr, fport } have been
979 * changed. NOTE: This does not handle the case of the lport changing (the
980 * hashed port list would have to be updated as well), so the lport must
981 * not change after in_pcbinshash() has been called.
987 struct inpcbhead *head;
988 u_int32_t hashkey_faddr;
991 if (inp->inp_vflag & INP_IPV6)
992 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
995 hashkey_faddr = inp->inp_faddr.s_addr;
997 head = &inp->inp_pcbinfo->hashbase[INP_PCBHASH(hashkey_faddr,
998 inp->inp_lport, inp->inp_fport, inp->inp_pcbinfo->hashmask)];
1000 LIST_REMOVE(inp, inp_hash);
1001 LIST_INSERT_HEAD(head, inp, inp_hash);
1005 * Remove PCB from various lists.
1011 inp->inp_gencnt = ++inp->inp_pcbinfo->ipi_gencnt;
1012 if (inp->inp_lport) {
1013 struct inpcbport *phd = inp->inp_phd;
1015 LIST_REMOVE(inp, inp_hash);
1016 LIST_REMOVE(inp, inp_portlist);
1017 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1018 LIST_REMOVE(phd, phd_hash);
1022 LIST_REMOVE(inp, inp_list);
1023 inp->inp_pcbinfo->ipi_count--;
1027 prison_xinpcb(struct thread *td, struct inpcb *inp)
1031 if (td->td_proc == NULL)
1033 cr = td->td_proc->p_ucred;
1034 if (cr->cr_prison == NULL)
1036 if (ntohl(inp->inp_laddr.s_addr) == cr->cr_prison->pr_ip)