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 $
35 * $DragonFly: src/sys/netinet/in_pcb.c,v 1.8 2003/08/23 11:18:00 rob 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 */
94 int ipport_firstauto = IPPORT_RESERVED; /* 1024 */
95 int ipport_lastauto = IPPORT_USERRESERVED; /* 5000 */
96 int ipport_hifirstauto = IPPORT_HIFIRSTAUTO; /* 49152 */
97 int ipport_hilastauto = IPPORT_HILASTAUTO; /* 65535 */
99 #define RANGECHK(var, min, max) \
100 if ((var) < (min)) { (var) = (min); } \
101 else if ((var) > (max)) { (var) = (max); }
104 sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS)
106 int error = sysctl_handle_int(oidp,
107 oidp->oid_arg1, oidp->oid_arg2, req);
109 RANGECHK(ipport_lowfirstauto, 1, IPPORT_RESERVED - 1);
110 RANGECHK(ipport_lowlastauto, 1, IPPORT_RESERVED - 1);
111 RANGECHK(ipport_firstauto, IPPORT_RESERVED, USHRT_MAX);
112 RANGECHK(ipport_lastauto, IPPORT_RESERVED, USHRT_MAX);
113 RANGECHK(ipport_hifirstauto, IPPORT_RESERVED, USHRT_MAX);
114 RANGECHK(ipport_hilastauto, IPPORT_RESERVED, USHRT_MAX);
121 SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0, "IP Ports");
123 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst, CTLTYPE_INT|CTLFLAG_RW,
124 &ipport_lowfirstauto, 0, &sysctl_net_ipport_check, "I", "");
125 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast, CTLTYPE_INT|CTLFLAG_RW,
126 &ipport_lowlastauto, 0, &sysctl_net_ipport_check, "I", "");
127 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first, CTLTYPE_INT|CTLFLAG_RW,
128 &ipport_firstauto, 0, &sysctl_net_ipport_check, "I", "");
129 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last, CTLTYPE_INT|CTLFLAG_RW,
130 &ipport_lastauto, 0, &sysctl_net_ipport_check, "I", "");
131 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst, CTLTYPE_INT|CTLFLAG_RW,
132 &ipport_hifirstauto, 0, &sysctl_net_ipport_check, "I", "");
133 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast, CTLTYPE_INT|CTLFLAG_RW,
134 &ipport_hilastauto, 0, &sysctl_net_ipport_check, "I", "");
137 * in_pcb.c: manage the Protocol Control Blocks.
139 * NOTE: It is assumed that most of these functions will be called at
140 * splnet(). XXX - There are, unfortunately, a few exceptions to this
141 * rule that should be fixed.
145 * Allocate a PCB and associate it with the socket.
148 in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo, struct thread *td)
155 inp = zalloc(pcbinfo->ipi_zone);
158 bzero((caddr_t)inp, sizeof(*inp));
159 inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
160 inp->inp_pcbinfo = pcbinfo;
161 inp->inp_socket = so;
163 error = ipsec_init_policy(so, &inp->inp_sp);
165 zfree(pcbinfo->ipi_zone, inp);
170 if (INP_SOCKAF(so) == AF_INET6 && ip6_v6only)
171 inp->inp_flags |= IN6P_IPV6_V6ONLY;
173 LIST_INSERT_HEAD(pcbinfo->listhead, inp, inp_list);
174 pcbinfo->ipi_count++;
175 so->so_pcb = (caddr_t)inp;
177 if (ip6_auto_flowlabel)
178 inp->inp_flags |= IN6P_AUTOFLOWLABEL;
184 in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct thread *td)
186 struct socket *so = inp->inp_socket;
187 struct proc *p = td->td_proc;
188 unsigned short *lastport;
189 struct sockaddr_in *sin;
190 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
192 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
193 int error, prison = 0;
197 if (TAILQ_EMPTY(&in_ifaddrhead)) /* XXX broken! */
198 return (EADDRNOTAVAIL);
199 if (inp->inp_lport || inp->inp_laddr.s_addr != INADDR_ANY)
201 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
204 sin = (struct sockaddr_in *)nam;
205 if (nam->sa_len != sizeof (*sin))
209 * We should check the family, but old programs
210 * incorrectly fail to initialize it.
212 if (sin->sin_family != AF_INET)
213 return (EAFNOSUPPORT);
215 if (sin->sin_addr.s_addr != INADDR_ANY)
216 if (prison_ip(td, 0, &sin->sin_addr.s_addr))
218 lport = sin->sin_port;
219 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
221 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
222 * allow complete duplication of binding if
223 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
224 * and a multicast address is bound on both
225 * new and duplicated sockets.
227 if (so->so_options & SO_REUSEADDR)
228 reuseport = SO_REUSEADDR|SO_REUSEPORT;
229 } else if (sin->sin_addr.s_addr != INADDR_ANY) {
230 sin->sin_port = 0; /* yech... */
231 bzero(&sin->sin_zero, sizeof(sin->sin_zero));
232 if (ifa_ifwithaddr((struct sockaddr *)sin) == 0)
233 return (EADDRNOTAVAIL);
239 if (ntohs(lport) < IPPORT_RESERVED && p &&
240 suser_cred(p->p_ucred, PRISON_ROOT))
242 if (p && p->p_ucred->cr_prison)
244 if (so->so_cred->cr_uid != 0 &&
245 !IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
246 t = in_pcblookup_local(inp->inp_pcbinfo,
247 sin->sin_addr, lport,
248 prison ? 0 : INPLOOKUP_WILDCARD);
250 (ntohl(sin->sin_addr.s_addr) != INADDR_ANY ||
251 ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
252 (t->inp_socket->so_options &
253 SO_REUSEPORT) == 0) &&
254 (so->so_cred->cr_uid !=
255 t->inp_socket->so_cred->cr_uid)) {
257 if (ntohl(sin->sin_addr.s_addr) !=
259 ntohl(t->inp_laddr.s_addr) !=
262 INP_SOCKAF(t->inp_socket))
263 #endif /* defined(INET6) */
268 prison_ip(td, 0, &sin->sin_addr.s_addr))
269 return (EADDRNOTAVAIL);
270 t = in_pcblookup_local(pcbinfo, sin->sin_addr,
271 lport, prison ? 0 : wild);
273 (reuseport & t->inp_socket->so_options) == 0) {
275 if (ntohl(sin->sin_addr.s_addr) !=
277 ntohl(t->inp_laddr.s_addr) !=
280 INP_SOCKAF(t->inp_socket))
281 #endif /* defined(INET6) */
285 inp->inp_laddr = sin->sin_addr;
291 if (inp->inp_laddr.s_addr != INADDR_ANY)
292 if (prison_ip(td, 0, &inp->inp_laddr.s_addr )) {
293 inp->inp_laddr.s_addr = INADDR_ANY;
296 inp->inp_flags |= INP_ANONPORT;
298 if (inp->inp_flags & INP_HIGHPORT) {
299 first = ipport_hifirstauto; /* sysctl */
300 last = ipport_hilastauto;
301 lastport = &pcbinfo->lasthi;
302 } else if (inp->inp_flags & INP_LOWPORT) {
303 if (p && (error = suser_cred(p->p_ucred, PRISON_ROOT))) {
304 inp->inp_laddr.s_addr = INADDR_ANY;
307 first = ipport_lowfirstauto; /* 1023 */
308 last = ipport_lowlastauto; /* 600 */
309 lastport = &pcbinfo->lastlow;
311 first = ipport_firstauto; /* sysctl */
312 last = ipport_lastauto;
313 lastport = &pcbinfo->lastport;
316 * Simple check to ensure all ports are not used up causing
319 * We split the two cases (up and down) so that the direction
320 * is not being tested on each round of the loop.
326 count = first - last;
329 if (count-- < 0) { /* completely used? */
330 inp->inp_laddr.s_addr = INADDR_ANY;
331 return (EADDRNOTAVAIL);
334 if (*lastport > first || *lastport < last)
336 lport = htons(*lastport);
337 } while (in_pcblookup_local(pcbinfo,
338 inp->inp_laddr, lport, wild));
343 count = last - first;
346 if (count-- < 0) { /* completely used? */
347 inp->inp_laddr.s_addr = INADDR_ANY;
348 return (EADDRNOTAVAIL);
351 if (*lastport < first || *lastport > last)
353 lport = htons(*lastport);
354 } while (in_pcblookup_local(pcbinfo,
355 inp->inp_laddr, lport, wild));
358 inp->inp_lport = lport;
359 if (prison_ip(td, 0, &inp->inp_laddr.s_addr)) {
360 inp->inp_laddr.s_addr = INADDR_ANY;
364 if (in_pcbinshash(inp) != 0) {
365 inp->inp_laddr.s_addr = INADDR_ANY;
373 * Transform old in_pcbconnect() into an inner subroutine for new
374 * in_pcbconnect(): Do some validity-checking on the remote
375 * address (in mbuf 'nam') and then determine local host address
376 * (i.e., which interface) to use to access that remote host.
378 * This preserves definition of in_pcbconnect(), while supporting a
379 * slightly different version for T/TCP. (This is more than
380 * a bit of a kludge, but cleaning up the internal interfaces would
381 * have forced minor changes in every protocol).
385 in_pcbladdr(inp, nam, plocal_sin)
387 struct sockaddr *nam;
388 struct sockaddr_in **plocal_sin;
390 struct in_ifaddr *ia;
391 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
393 if (nam->sa_len != sizeof (*sin))
395 if (sin->sin_family != AF_INET)
396 return (EAFNOSUPPORT);
397 if (sin->sin_port == 0)
398 return (EADDRNOTAVAIL);
399 if (!TAILQ_EMPTY(&in_ifaddrhead)) {
401 * If the destination address is INADDR_ANY,
402 * use the primary local address.
403 * If the supplied address is INADDR_BROADCAST,
404 * and the primary interface supports broadcast,
405 * choose the broadcast address for that interface.
407 if (sin->sin_addr.s_addr == INADDR_ANY)
408 sin->sin_addr = IA_SIN(TAILQ_FIRST(&in_ifaddrhead))->sin_addr;
409 else if (sin->sin_addr.s_addr == (u_long)INADDR_BROADCAST &&
410 (TAILQ_FIRST(&in_ifaddrhead)->ia_ifp->if_flags & IFF_BROADCAST))
411 sin->sin_addr = satosin(&TAILQ_FIRST(&in_ifaddrhead)->ia_broadaddr)->sin_addr;
413 if (inp->inp_laddr.s_addr == INADDR_ANY) {
416 ia = (struct in_ifaddr *)0;
418 * If route is known or can be allocated now,
419 * our src addr is taken from the i/f, else punt.
420 * Note that we should check the address family of the cached
421 * destination, in case of sharing the cache with IPv6.
423 ro = &inp->inp_route;
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 *)0;
432 if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0 && /*XXX*/
433 (ro->ro_rt == (struct rtentry *)0 ||
434 ro->ro_rt->rt_ifp == (struct ifnet *)0)) {
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 (inp->inp_laddr.s_addr == INADDR_ANY && 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.
522 if ((error = in_pcbladdr(inp, nam, &ifaddr)) != 0)
525 if (in_pcblookup_hash(inp->inp_pcbinfo, sin->sin_addr, sin->sin_port,
526 inp->inp_laddr.s_addr ? inp->inp_laddr : ifaddr->sin_addr,
527 inp->inp_lport, 0, NULL) != NULL) {
530 if (inp->inp_laddr.s_addr == INADDR_ANY) {
531 if (inp->inp_lport == 0) {
532 error = in_pcbbind(inp, (struct sockaddr *)0, td);
536 inp->inp_laddr = ifaddr->sin_addr;
538 inp->inp_faddr = sin->sin_addr;
539 inp->inp_fport = sin->sin_port;
545 in_pcbdisconnect(inp)
549 inp->inp_faddr.s_addr = INADDR_ANY;
552 if (inp->inp_socket->so_state & SS_NOFDREF)
560 struct socket *so = inp->inp_socket;
561 struct inpcbinfo *ipi = inp->inp_pcbinfo;
564 ipsec4_delete_pcbpolicy(inp);
566 inp->inp_gencnt = ++ipi->ipi_gencnt;
570 if (inp->inp_options)
571 (void)m_free(inp->inp_options);
572 if (inp->inp_route.ro_rt)
573 rtfree(inp->inp_route.ro_rt);
574 ip_freemoptions(inp->inp_moptions);
576 zfree(ipi->ipi_zone, inp);
580 * The calling convention of in_setsockaddr() and in_setpeeraddr() was
581 * modified to match the pru_sockaddr() and pru_peeraddr() entry points
582 * in struct pr_usrreqs, so that protocols can just reference then directly
583 * without the need for a wrapper function. The socket must have a valid
584 * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
585 * except through a kernel programming error, so it is acceptable to panic
586 * (or in this case trap) if the PCB is invalid. (Actually, we don't trap
587 * because there actually /is/ a programming error somewhere... XXX)
590 in_setsockaddr(so, nam)
592 struct sockaddr **nam;
596 struct sockaddr_in *sin;
599 * Do the malloc first in case it blocks.
601 MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
603 sin->sin_family = AF_INET;
604 sin->sin_len = sizeof(*sin);
613 sin->sin_port = inp->inp_lport;
614 sin->sin_addr = inp->inp_laddr;
617 *nam = (struct sockaddr *)sin;
622 in_setpeeraddr(so, nam)
624 struct sockaddr **nam;
628 struct sockaddr_in *sin;
631 * Do the malloc first in case it blocks.
633 MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
635 sin->sin_family = AF_INET;
636 sin->sin_len = sizeof(*sin);
645 sin->sin_port = inp->inp_fport;
646 sin->sin_addr = inp->inp_faddr;
649 *nam = (struct sockaddr *)sin;
654 in_pcbnotifyall(head, faddr, errno, notify)
655 struct inpcbhead *head;
656 struct in_addr faddr;
657 void (*notify) (struct inpcb *, int);
659 struct inpcb *inp, *ninp;
663 for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) {
664 ninp = LIST_NEXT(inp, inp_list);
666 if ((inp->inp_vflag & INP_IPV4) == 0)
669 if (inp->inp_faddr.s_addr != faddr.s_addr ||
670 inp->inp_socket == NULL)
672 (*notify)(inp, errno);
678 in_pcbpurgeif0(head, ifp)
683 struct ip_moptions *imo;
686 for (inp = head; inp != NULL; inp = LIST_NEXT(inp, inp_list)) {
687 imo = inp->inp_moptions;
688 if ((inp->inp_vflag & INP_IPV4) &&
691 * Unselect the outgoing interface if it is being
694 if (imo->imo_multicast_ifp == ifp)
695 imo->imo_multicast_ifp = NULL;
698 * Drop multicast group membership if we joined
699 * through the interface being detached.
701 for (i = 0, gap = 0; i < imo->imo_num_memberships;
703 if (imo->imo_membership[i]->inm_ifp == ifp) {
704 in_delmulti(imo->imo_membership[i]);
707 imo->imo_membership[i - gap] =
708 imo->imo_membership[i];
710 imo->imo_num_memberships -= gap;
716 * Check for alternatives when higher level complains
717 * about service problems. For now, invalidate cached
718 * routing information. If the route was created dynamically
719 * (by a redirect), time to try a default gateway again.
726 struct rt_addrinfo info;
728 if ((rt = inp->inp_route.ro_rt)) {
729 bzero((caddr_t)&info, sizeof(info));
730 info.rti_flags = rt->rt_flags;
731 info.rti_info[RTAX_DST] = rt_key(rt);
732 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
733 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
734 rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
735 if (rt->rt_flags & RTF_DYNAMIC)
736 (void) rtrequest1(RTM_DELETE, &info, NULL);
737 inp->inp_route.ro_rt = NULL;
740 * A new route can be allocated
741 * the next time output is attempted.
747 * After a routing change, flush old routing
748 * and allocate a (hopefully) better one.
751 in_rtchange(inp, errno)
755 if (inp->inp_route.ro_rt) {
756 rtfree(inp->inp_route.ro_rt);
757 inp->inp_route.ro_rt = 0;
759 * A new route can be allocated the next time
760 * output is attempted.
766 * Lookup a PCB based on the local address and port.
769 in_pcblookup_local(pcbinfo, laddr, lport_arg, wild_okay)
770 struct inpcbinfo *pcbinfo;
771 struct in_addr laddr;
776 int matchwild = 3, wildcard;
777 u_short lport = lport_arg;
780 struct inpcbhead *head;
782 * Look for an unconnected (wildcard foreign addr) PCB that
783 * matches the local address and port we're looking for.
785 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, pcbinfo->hashmask)];
786 LIST_FOREACH(inp, head, inp_hash) {
788 if ((inp->inp_vflag & INP_IPV4) == 0)
791 if (inp->inp_faddr.s_addr == INADDR_ANY &&
792 inp->inp_laddr.s_addr == laddr.s_addr &&
793 inp->inp_lport == lport) {
805 struct inpcbporthead *porthash;
806 struct inpcbport *phd;
807 struct inpcb *match = NULL;
809 * Best fit PCB lookup.
811 * First see if this local port is in use by looking on the
814 porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport,
815 pcbinfo->porthashmask)];
816 LIST_FOREACH(phd, porthash, phd_hash) {
817 if (phd->phd_port == lport)
822 * Port is in use by one or more PCBs. Look for best
825 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
828 if ((inp->inp_vflag & INP_IPV4) == 0)
831 if (inp->inp_faddr.s_addr != INADDR_ANY)
833 if (inp->inp_laddr.s_addr != INADDR_ANY) {
834 if (laddr.s_addr == INADDR_ANY)
836 else if (inp->inp_laddr.s_addr != laddr.s_addr)
839 if (laddr.s_addr != INADDR_ANY)
842 if (wildcard < matchwild) {
844 matchwild = wildcard;
845 if (matchwild == 0) {
856 * Lookup PCB in hash list.
859 in_pcblookup_hash(pcbinfo, faddr, fport_arg, laddr, lport_arg, wildcard,
861 struct inpcbinfo *pcbinfo;
862 struct in_addr faddr, laddr;
863 u_int fport_arg, lport_arg;
867 struct inpcbhead *head;
869 u_short fport = fport_arg, lport = lport_arg;
872 * First look for an exact match.
874 head = &pcbinfo->hashbase[INP_PCBHASH(faddr.s_addr, lport, fport, pcbinfo->hashmask)];
875 LIST_FOREACH(inp, head, inp_hash) {
877 if ((inp->inp_vflag & INP_IPV4) == 0)
880 if (inp->inp_faddr.s_addr == faddr.s_addr &&
881 inp->inp_laddr.s_addr == laddr.s_addr &&
882 inp->inp_fport == fport &&
883 inp->inp_lport == lport) {
891 struct inpcb *local_wild = NULL;
893 struct inpcb *local_wild_mapped = NULL;
894 #endif /* defined(INET6) */
896 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, pcbinfo->hashmask)];
897 LIST_FOREACH(inp, head, inp_hash) {
899 if ((inp->inp_vflag & INP_IPV4) == 0)
902 if (inp->inp_faddr.s_addr == INADDR_ANY &&
903 inp->inp_lport == lport) {
904 if (ifp && ifp->if_type == IFT_FAITH &&
905 (inp->inp_flags & INP_FAITH) == 0)
907 if (inp->inp_laddr.s_addr == laddr.s_addr)
909 else if (inp->inp_laddr.s_addr == INADDR_ANY) {
911 if (INP_CHECK_SOCKAF(inp->inp_socket,
913 local_wild_mapped = inp;
915 #endif /* defined(INET6) */
921 if (local_wild == NULL)
922 return (local_wild_mapped);
923 #endif /* defined(INET6) */
934 * Insert PCB onto various hash lists.
940 struct inpcbhead *pcbhash;
941 struct inpcbporthead *pcbporthash;
942 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
943 struct inpcbport *phd;
944 u_int32_t hashkey_faddr;
947 if (inp->inp_vflag & INP_IPV6)
948 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
951 hashkey_faddr = inp->inp_faddr.s_addr;
953 pcbhash = &pcbinfo->hashbase[INP_PCBHASH(hashkey_faddr,
954 inp->inp_lport, inp->inp_fport, pcbinfo->hashmask)];
956 pcbporthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(inp->inp_lport,
957 pcbinfo->porthashmask)];
960 * Go through port list and look for a head for this lport.
962 LIST_FOREACH(phd, pcbporthash, phd_hash) {
963 if (phd->phd_port == inp->inp_lport)
967 * If none exists, malloc one and tack it on.
970 MALLOC(phd, struct inpcbport *, sizeof(struct inpcbport), M_PCB, M_NOWAIT);
972 return (ENOBUFS); /* XXX */
974 phd->phd_port = inp->inp_lport;
975 LIST_INIT(&phd->phd_pcblist);
976 LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
979 LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
980 LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
985 * Move PCB to the proper hash bucket when { faddr, fport } have been
986 * changed. NOTE: This does not handle the case of the lport changing (the
987 * hashed port list would have to be updated as well), so the lport must
988 * not change after in_pcbinshash() has been called.
994 struct inpcbhead *head;
995 u_int32_t hashkey_faddr;
998 if (inp->inp_vflag & INP_IPV6)
999 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1002 hashkey_faddr = inp->inp_faddr.s_addr;
1004 head = &inp->inp_pcbinfo->hashbase[INP_PCBHASH(hashkey_faddr,
1005 inp->inp_lport, inp->inp_fport, inp->inp_pcbinfo->hashmask)];
1007 LIST_REMOVE(inp, inp_hash);
1008 LIST_INSERT_HEAD(head, inp, inp_hash);
1012 * Remove PCB from various lists.
1018 inp->inp_gencnt = ++inp->inp_pcbinfo->ipi_gencnt;
1019 if (inp->inp_lport) {
1020 struct inpcbport *phd = inp->inp_phd;
1022 LIST_REMOVE(inp, inp_hash);
1023 LIST_REMOVE(inp, inp_portlist);
1024 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1025 LIST_REMOVE(phd, phd_hash);
1029 LIST_REMOVE(inp, inp_list);
1030 inp->inp_pcbinfo->ipi_count--;
1034 prison_xinpcb(struct thread *td, struct inpcb *inp)
1038 if (td->td_proc == NULL)
1040 cr = td->td_proc->p_ucred;
1041 if (cr->cr_prison == NULL)
1043 if (ntohl(inp->inp_laddr.s_addr) == cr->cr_prison->pr_ip)