1 //==========================================================================
3 // src/sys/netinet/in_pcb.c
5 //==========================================================================
6 //####BSDCOPYRIGHTBEGIN####
8 // -------------------------------------------
10 // Portions of this software may have been derived from OpenBSD,
11 // FreeBSD or other sources, and are covered by the appropriate
12 // copyright disclaimers included herein.
14 // Portions created by Red Hat are
15 // Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
17 // -------------------------------------------
19 //####BSDCOPYRIGHTEND####
20 //==========================================================================
23 * Copyright (c) 1982, 1986, 1991, 1993, 1995
24 * The Regents of the University of California. All rights reserved.
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * 3. All advertising materials mentioning features or use of this software
35 * must display the following acknowledgement:
36 * This product includes software developed by the University of
37 * California, Berkeley and its contributors.
38 * 4. Neither the name of the University nor the names of its contributors
39 * may be used to endorse or promote products derived from this software
40 * without specific prior written permission.
42 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * @(#)in_pcb.c 8.4 (Berkeley) 5/24/95
55 * $FreeBSD: src/sys/netinet/in_pcb.c,v 1.59.2.17 2001/08/13 16:26:17 ume Exp $
58 #include <sys/param.h>
59 #include <sys/malloc.h>
61 #include <sys/domain.h>
62 #include <sys/protosw.h>
63 #include <sys/socket.h>
64 #include <sys/socketvar.h>
65 #include <sys/sysctl.h>
68 #include <net/if_types.h>
69 #include <net/route.h>
71 #include <netinet/in.h>
72 #include <netinet/in_pcb.h>
73 #include <netinet/in_var.h>
74 #include <netinet/ip_var.h>
76 #include <netinet/ip6.h>
77 #include <netinet6/ip6_var.h>
81 #include <netinet6/ipsec.h>
82 #include <netkey/key.h>
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); }
102 #ifdef CYGPKG_NET_FREEBSD_SYSCTL
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(so, pcbinfo, p)
150 struct inpcbinfo *pcbinfo;
153 register struct inpcb *inp;
158 inp = zalloci(pcbinfo->ipi_zone);
161 bzero((caddr_t)inp, sizeof(*inp));
162 inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
163 inp->inp_pcbinfo = pcbinfo;
164 inp->inp_socket = so;
166 error = ipsec_init_policy(so, &inp->inp_sp);
168 zfreei(pcbinfo->ipi_zone, inp);
173 if (INP_SOCKAF(so) == AF_INET6 && !ip6_mapped_addr_on)
174 inp->inp_flags |= IN6P_IPV6_V6ONLY;
176 LIST_INSERT_HEAD(pcbinfo->listhead, inp, inp_list);
177 pcbinfo->ipi_count++;
178 so->so_pcb = (caddr_t)inp;
180 if (ip6_auto_flowlabel)
181 inp->inp_flags |= IN6P_AUTOFLOWLABEL;
187 in_pcbbind(inp, nam, p)
188 register struct inpcb *inp;
189 struct sockaddr *nam;
192 register struct socket *so = inp->inp_socket;
193 unsigned short *lastport;
194 struct sockaddr_in *sin;
195 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
197 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
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;
208 if (nam->sa_len == 0) nam->sa_len = sizeof(*sin);
210 if (nam->sa_len != sizeof (*sin))
214 * We should check the family, but old programs
215 * incorrectly fail to initialize it.
217 if (sin->sin_family != AF_INET)
218 return (EAFNOSUPPORT);
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 if (ifa_ifwithaddr((struct sockaddr *)sin) == 0)
234 return (EADDRNOTAVAIL);
240 t = in_pcblookup_local(pcbinfo, sin->sin_addr, lport, wild);
242 (reuseport & t->inp_socket->so_options) == 0) {
244 if (ntohl(sin->sin_addr.s_addr) !=
246 ntohl(t->inp_laddr.s_addr) !=
249 INP_SOCKAF(t->inp_socket))
250 #endif /* defined(INET6) */
254 inp->inp_laddr = sin->sin_addr;
260 inp->inp_flags |= INP_ANONPORT;
262 if (inp->inp_flags & INP_HIGHPORT) {
263 first = ipport_hifirstauto; /* sysctl */
264 last = ipport_hilastauto;
265 lastport = &pcbinfo->lasthi;
266 } else if (inp->inp_flags & INP_LOWPORT) {
267 first = ipport_lowfirstauto; /* 1023 */
268 last = ipport_lowlastauto; /* 600 */
269 lastport = &pcbinfo->lastlow;
271 first = ipport_firstauto; /* sysctl */
272 last = ipport_lastauto;
273 lastport = &pcbinfo->lastport;
276 * Simple check to ensure all ports are not used up causing
279 * We split the two cases (up and down) so that the direction
280 * is not being tested on each round of the loop.
287 #ifdef CYGPKG_NET_RANDOM_PORTS
288 *lastport = first - (arc4random() % (first - last));
290 count = first - last;
293 if (count-- < 0) { /* completely used? */
294 inp->inp_laddr.s_addr = INADDR_ANY;
295 return (EADDRNOTAVAIL);
298 if (*lastport > first || *lastport < last)
300 lport = htons(*lastport);
301 } while (in_pcblookup_local(pcbinfo,
302 inp->inp_laddr, lport, wild));
307 #ifdef CYGPKG_NET_RANDOM_PORTS
308 *lastport = first + (arc4random() % (last - first));
310 count = last - first;
313 if (count-- < 0) { /* completely used? */
314 inp->inp_laddr.s_addr = INADDR_ANY;
315 return (EADDRNOTAVAIL);
318 if (*lastport < first || *lastport > last)
320 lport = htons(*lastport);
321 } while (in_pcblookup_local(pcbinfo,
322 inp->inp_laddr, lport, wild));
325 inp->inp_lport = lport;
326 if (in_pcbinshash(inp) != 0) {
327 inp->inp_laddr.s_addr = INADDR_ANY;
335 * Transform old in_pcbconnect() into an inner subroutine for new
336 * in_pcbconnect(): Do some validity-checking on the remote
337 * address (in mbuf 'nam') and then determine local host address
338 * (i.e., which interface) to use to access that remote host.
340 * This preserves definition of in_pcbconnect(), while supporting a
341 * slightly different version for T/TCP. (This is more than
342 * a bit of a kludge, but cleaning up the internal interfaces would
343 * have forced minor changes in every protocol).
347 in_pcbladdr(inp, nam, plocal_sin)
348 register struct inpcb *inp;
349 struct sockaddr *nam;
350 struct sockaddr_in **plocal_sin;
352 struct in_ifaddr *ia;
353 register struct sockaddr_in *sin = (struct sockaddr_in *)nam;
355 if (nam->sa_len != sizeof (*sin))
357 if (sin->sin_family != AF_INET)
358 return (EAFNOSUPPORT);
359 if (sin->sin_port == 0)
360 return (EADDRNOTAVAIL);
361 if (!TAILQ_EMPTY(&in_ifaddrhead)) {
363 * If the destination address is INADDR_ANY,
364 * use the primary local address.
365 * If the supplied address is INADDR_BROADCAST,
366 * and the primary interface supports broadcast,
367 * choose the broadcast address for that interface.
369 #define satosin(sa) ((struct sockaddr_in *)(sa))
370 #define sintosa(sin) ((struct sockaddr *)(sin))
371 #define ifatoia(ifa) ((struct in_ifaddr *)(ifa))
372 if (sin->sin_addr.s_addr == INADDR_ANY)
373 sin->sin_addr = IA_SIN(TAILQ_FIRST(&in_ifaddrhead))->sin_addr;
374 else if (sin->sin_addr.s_addr == (u_long)INADDR_BROADCAST &&
375 (TAILQ_FIRST(&in_ifaddrhead)->ia_ifp->if_flags & IFF_BROADCAST))
376 sin->sin_addr = satosin(&TAILQ_FIRST(&in_ifaddrhead)->ia_broadaddr)->sin_addr;
378 if (inp->inp_laddr.s_addr == INADDR_ANY) {
379 register struct route *ro;
381 ia = (struct in_ifaddr *)0;
383 * If route is known or can be allocated now,
384 * our src addr is taken from the i/f, else punt.
386 ro = &inp->inp_route;
388 (satosin(&ro->ro_dst)->sin_addr.s_addr !=
389 sin->sin_addr.s_addr ||
390 inp->inp_socket->so_options & SO_DONTROUTE)) {
392 ro->ro_rt = (struct rtentry *)0;
394 if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0 && /*XXX*/
395 (ro->ro_rt == (struct rtentry *)0 ||
396 ro->ro_rt->rt_ifp == (struct ifnet *)0)) {
397 /* No route yet, so try to acquire one */
398 ro->ro_dst.sa_family = AF_INET;
399 ro->ro_dst.sa_len = sizeof(struct sockaddr_in);
400 ((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
405 * If we found a route, use the address
406 * corresponding to the outgoing interface
407 * unless it is the loopback (in case a route
408 * to our address on another net goes to loopback).
410 if (ro->ro_rt && !(ro->ro_rt->rt_ifp->if_flags & IFF_LOOPBACK))
411 ia = ifatoia(ro->ro_rt->rt_ifa);
413 u_short fport = sin->sin_port;
416 ia = ifatoia(ifa_ifwithdstaddr(sintosa(sin)));
418 ia = ifatoia(ifa_ifwithnet(sintosa(sin)));
419 sin->sin_port = fport;
421 ia = TAILQ_FIRST(&in_ifaddrhead);
423 return (EADDRNOTAVAIL);
426 * If the destination address is multicast and an outgoing
427 * interface has been set as a multicast option, use the
428 * address of that interface as our source address.
430 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
431 inp->inp_moptions != NULL) {
432 struct ip_moptions *imo;
435 imo = inp->inp_moptions;
436 if (imo->imo_multicast_ifp != NULL) {
437 ifp = imo->imo_multicast_ifp;
438 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
439 if (ia->ia_ifp == ifp)
442 return (EADDRNOTAVAIL);
446 * Don't do pcblookup call here; return interface in plocal_sin
447 * and exit to caller, that will do the lookup.
449 *plocal_sin = &ia->ia_addr;
457 * Connect from a socket to a specified address.
458 * Both address and port must be specified in argument sin.
459 * If don't have a local address for this socket yet,
463 in_pcbconnect(inp, nam, p)
464 register struct inpcb *inp;
465 struct sockaddr *nam;
468 struct sockaddr_in *ifaddr;
469 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
473 * Call inner routine, to assign local interface address.
475 if ((error = in_pcbladdr(inp, nam, &ifaddr)) != 0)
478 if (in_pcblookup_hash(inp->inp_pcbinfo, sin->sin_addr, sin->sin_port,
479 inp->inp_laddr.s_addr ? inp->inp_laddr : ifaddr->sin_addr,
480 inp->inp_lport, 0, NULL) != NULL) {
483 if (inp->inp_laddr.s_addr == INADDR_ANY) {
484 if (inp->inp_lport == 0) {
485 error = in_pcbbind(inp, (struct sockaddr *)0, p);
489 inp->inp_laddr = ifaddr->sin_addr;
491 inp->inp_faddr = sin->sin_addr;
492 inp->inp_fport = sin->sin_port;
495 if (inp->inp_socket->so_type == SOCK_STREAM)
496 ipsec_pcbconn(inp->inp_sp);
502 in_pcbdisconnect(inp)
506 inp->inp_faddr.s_addr = INADDR_ANY;
509 if (inp->inp_socket->so_state & SS_NOFDREF)
512 ipsec_pcbdisconn(inp->inp_sp);
520 struct socket *so = inp->inp_socket;
521 struct inpcbinfo *ipi = inp->inp_pcbinfo;
522 struct rtentry *rt = inp->inp_route.ro_rt;
525 ipsec4_delete_pcbpolicy(inp);
527 inp->inp_gencnt = ++ipi->ipi_gencnt;
531 if (inp->inp_options)
532 (void)m_free(inp->inp_options);
535 * route deletion requires reference count to be <= zero
537 if ((rt->rt_flags & RTF_DELCLONE) &&
538 (rt->rt_flags & RTF_WASCLONED) &&
539 (rt->rt_refcnt <= 1)) {
541 rt->rt_flags &= ~RTF_UP;
542 rtrequest(RTM_DELETE, rt_key(rt),
543 rt->rt_gateway, rt_mask(rt),
544 rt->rt_flags, (struct rtentry **)0);
549 ip_freemoptions(inp->inp_moptions);
551 zfreei(ipi->ipi_zone, inp);
555 * The calling convention of in_setsockaddr() and in_setpeeraddr() was
556 * modified to match the pru_sockaddr() and pru_peeraddr() entry points
557 * in struct pr_usrreqs, so that protocols can just reference then directly
558 * without the need for a wrapper function. The socket must have a valid
559 * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
560 * except through a kernel programming error, so it is acceptable to panic
561 * (or in this case trap) if the PCB is invalid. (Actually, we don't trap
562 * because there actually /is/ a programming error somewhere... XXX)
565 in_setsockaddr(so, nam)
567 struct sockaddr **nam;
570 register struct inpcb *inp;
571 register struct sockaddr_in *sin;
574 * Do the malloc first in case it blocks.
576 MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
578 sin->sin_family = AF_INET;
579 sin->sin_len = sizeof(*sin);
588 sin->sin_port = inp->inp_lport;
589 sin->sin_addr = inp->inp_laddr;
592 *nam = (struct sockaddr *)sin;
597 in_setpeeraddr(so, nam)
599 struct sockaddr **nam;
603 register struct sockaddr_in *sin;
606 * Do the malloc first in case it blocks.
608 MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
610 sin->sin_family = AF_INET;
611 sin->sin_len = sizeof(*sin);
620 sin->sin_port = inp->inp_fport;
621 sin->sin_addr = inp->inp_faddr;
624 *nam = (struct sockaddr *)sin;
629 in_pcbnotifyall(head, faddr, _errno, notify)
630 struct inpcbhead *head;
631 struct in_addr faddr;
632 void (*notify) __P((struct inpcb *, int));
634 struct inpcb *inp, *ninp;
638 for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) {
639 ninp = LIST_NEXT(inp, inp_list);
641 if ((inp->inp_vflag & INP_IPV4) == 0)
644 if (inp->inp_faddr.s_addr != faddr.s_addr ||
645 inp->inp_socket == NULL)
647 (*notify)(inp, _errno);
653 in_pcbpurgeif0(head, ifp)
658 struct ip_moptions *imo;
661 for (inp = head; inp != NULL; inp = LIST_NEXT(inp, inp_list)) {
662 imo = inp->inp_moptions;
663 if ((inp->inp_vflag & INP_IPV4) &&
666 * Unselect the outgoing interface if it is being
669 if (imo->imo_multicast_ifp == ifp)
670 imo->imo_multicast_ifp = NULL;
673 * Drop multicast group membership if we joined
674 * through the interface being detached.
676 for (i = 0, gap = 0; i < imo->imo_num_memberships;
678 if (imo->imo_membership[i]->inm_ifp == ifp) {
679 in_delmulti(imo->imo_membership[i]);
682 imo->imo_membership[i - gap] =
683 imo->imo_membership[i];
685 imo->imo_num_memberships -= gap;
691 * Check for alternatives when higher level complains
692 * about service problems. For now, invalidate cached
693 * routing information. If the route was created dynamically
694 * (by a redirect), time to try a default gateway again.
700 register struct rtentry *rt;
701 struct rt_addrinfo info;
703 if ((rt = inp->inp_route.ro_rt)) {
704 bzero((caddr_t)&info, sizeof(info));
705 info.rti_info[RTAX_DST] =
706 (struct sockaddr *)&inp->inp_route.ro_dst;
707 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
708 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
709 rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
710 if (rt->rt_flags & RTF_DYNAMIC)
711 (void) rtrequest(RTM_DELETE, rt_key(rt),
712 rt->rt_gateway, rt_mask(rt), rt->rt_flags,
713 (struct rtentry **)0);
714 inp->inp_route.ro_rt = 0;
717 * A new route can be allocated
718 * the next time output is attempted.
724 * After a routing change, flush old routing
725 * and allocate a (hopefully) better one.
728 in_rtchange(inp, _errno)
729 register struct inpcb *inp;
732 if (inp->inp_route.ro_rt) {
733 rtfree(inp->inp_route.ro_rt);
734 inp->inp_route.ro_rt = 0;
736 * A new route can be allocated the next time
737 * output is attempted.
743 * Lookup a PCB based on the local address and port.
746 in_pcblookup_local(pcbinfo, laddr, lport_arg, wild_okay)
747 struct inpcbinfo *pcbinfo;
748 struct in_addr laddr;
752 register struct inpcb *inp;
753 int matchwild = 3, wildcard;
754 u_short lport = lport_arg;
757 struct inpcbhead *head;
759 * Look for an unconnected (wildcard foreign addr) PCB that
760 * matches the local address and port we're looking for.
762 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, pcbinfo->hashmask)];
763 LIST_FOREACH(inp, head, inp_hash) {
765 if ((inp->inp_vflag & INP_IPV4) == 0)
768 if (inp->inp_faddr.s_addr == INADDR_ANY &&
769 inp->inp_laddr.s_addr == laddr.s_addr &&
770 inp->inp_lport == lport) {
782 struct inpcbporthead *porthash;
783 struct inpcbport *phd;
784 struct inpcb *match = NULL;
786 * Best fit PCB lookup.
788 * First see if this local port is in use by looking on the
791 porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport,
792 pcbinfo->porthashmask)];
793 LIST_FOREACH(phd, porthash, phd_hash) {
794 if (phd->phd_port == lport)
799 * Port is in use by one or more PCBs. Look for best
802 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
805 if ((inp->inp_vflag & INP_IPV4) == 0)
808 if (inp->inp_faddr.s_addr != INADDR_ANY)
810 if (inp->inp_laddr.s_addr != INADDR_ANY) {
811 if (laddr.s_addr == INADDR_ANY)
813 else if (inp->inp_laddr.s_addr != laddr.s_addr)
816 if (laddr.s_addr != INADDR_ANY)
819 if (wildcard < matchwild) {
821 matchwild = wildcard;
822 if (matchwild == 0) {
833 * Lookup PCB in hash list.
836 in_pcblookup_hash(pcbinfo, faddr, fport_arg, laddr, lport_arg, wildcard,
838 struct inpcbinfo *pcbinfo;
839 struct in_addr faddr, laddr;
840 u_int fport_arg, lport_arg;
844 struct inpcbhead *head;
845 register struct inpcb *inp;
846 u_short fport = fport_arg, lport = lport_arg;
849 * First look for an exact match.
851 head = &pcbinfo->hashbase[INP_PCBHASH(faddr.s_addr, lport, fport, pcbinfo->hashmask)];
852 LIST_FOREACH(inp, head, inp_hash) {
854 if ((inp->inp_vflag & INP_IPV4) == 0)
857 if (inp->inp_faddr.s_addr == faddr.s_addr &&
858 inp->inp_laddr.s_addr == laddr.s_addr &&
859 inp->inp_fport == fport &&
860 inp->inp_lport == lport) {
868 struct inpcb *local_wild = NULL;
870 struct inpcb *local_wild_mapped = NULL;
871 #endif /* defined(INET6) */
873 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, pcbinfo->hashmask)];
874 LIST_FOREACH(inp, head, inp_hash) {
876 if ((inp->inp_vflag & INP_IPV4) == 0)
879 if (inp->inp_faddr.s_addr == INADDR_ANY &&
880 inp->inp_lport == lport) {
881 #if defined(NFAITH) && NFAITH > 0
882 if (ifp && ifp->if_type == IFT_FAITH &&
883 (inp->inp_flags & INP_FAITH) == 0)
886 if (inp->inp_laddr.s_addr == laddr.s_addr)
888 else if (inp->inp_laddr.s_addr == INADDR_ANY) {
890 if (INP_CHECK_SOCKAF(inp->inp_socket,
892 local_wild_mapped = inp;
894 #endif /* defined(INET6) */
900 if (local_wild == NULL)
901 return (local_wild_mapped);
902 #endif /* defined(INET6) */
913 * Insert PCB onto various hash lists.
919 struct inpcbhead *pcbhash;
920 struct inpcbporthead *pcbporthash;
921 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
922 struct inpcbport *phd;
923 u_int32_t hashkey_faddr;
926 if (inp->inp_vflag & INP_IPV6)
927 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
930 hashkey_faddr = inp->inp_faddr.s_addr;
932 pcbhash = &pcbinfo->hashbase[INP_PCBHASH(hashkey_faddr,
933 inp->inp_lport, inp->inp_fport, pcbinfo->hashmask)];
935 pcbporthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(inp->inp_lport,
936 pcbinfo->porthashmask)];
939 * Go through port list and look for a head for this lport.
941 LIST_FOREACH(phd, pcbporthash, phd_hash) {
942 if (phd->phd_port == inp->inp_lport)
946 * If none exists, malloc one and tack it on.
949 MALLOC(phd, struct inpcbport *, sizeof(struct inpcbport), M_PCB, M_NOWAIT);
951 return (ENOBUFS); /* XXX */
953 phd->phd_port = inp->inp_lport;
954 LIST_INIT(&phd->phd_pcblist);
955 LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
958 LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
959 LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
964 * Move PCB to the proper hash bucket when { faddr, fport } have been
965 * changed. NOTE: This does not handle the case of the lport changing (the
966 * hashed port list would have to be updated as well), so the lport must
967 * not change after in_pcbinshash() has been called.
973 struct inpcbhead *head;
974 u_int32_t hashkey_faddr;
977 if (inp->inp_vflag & INP_IPV6)
978 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
981 hashkey_faddr = inp->inp_faddr.s_addr;
983 head = &inp->inp_pcbinfo->hashbase[INP_PCBHASH(hashkey_faddr,
984 inp->inp_lport, inp->inp_fport, inp->inp_pcbinfo->hashmask)];
986 LIST_REMOVE(inp, inp_hash);
987 LIST_INSERT_HEAD(head, inp, inp_hash);
991 * Remove PCB from various lists.
997 inp->inp_gencnt = ++inp->inp_pcbinfo->ipi_gencnt;
998 if (inp->inp_lport) {
999 struct inpcbport *phd = inp->inp_phd;
1001 LIST_REMOVE(inp, inp_hash);
1002 LIST_REMOVE(inp, inp_portlist);
1003 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1004 LIST_REMOVE(phd, phd_hash);
1008 LIST_REMOVE(inp, inp_list);
1009 inp->inp_pcbinfo->ipi_count--;