2 * Copyright (c) 1982, 1986, 1991, 1993, 1995
3 * The Regents of the University of California.
4 * Copyright (c) 2007 Robert N. M. Watson
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 4. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * @(#)in_pcb.c 8.4 (Berkeley) 5/24/95
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
38 #include "opt_ipsec.h"
39 #include "opt_inet6.h"
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/malloc.h>
46 #include <sys/domain.h>
47 #include <sys/protosw.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
53 #include <sys/kernel.h>
54 #include <sys/sysctl.h>
63 #include <net/if_types.h>
64 #include <net/route.h>
66 #include <netinet/in.h>
67 #include <netinet/in_pcb.h>
68 #include <netinet/in_var.h>
69 #include <netinet/ip_var.h>
70 #include <netinet/tcp_var.h>
71 #include <netinet/udp.h>
72 #include <netinet/udp_var.h>
74 #include <netinet/ip6.h>
75 #include <netinet6/ip6_var.h>
80 #include <netipsec/ipsec.h>
81 #include <netipsec/key.h>
84 #include <security/mac/mac_framework.h>
87 * These configure the range of local port addresses assigned to
88 * "unspecified" outgoing connections/packets/whatever.
90 int ipport_lowfirstauto = IPPORT_RESERVED - 1; /* 1023 */
91 int ipport_lowlastauto = IPPORT_RESERVEDSTART; /* 600 */
92 int ipport_firstauto = IPPORT_HIFIRSTAUTO; /* 49152 */
93 int ipport_lastauto = IPPORT_HILASTAUTO; /* 65535 */
94 int ipport_hifirstauto = IPPORT_HIFIRSTAUTO; /* 49152 */
95 int ipport_hilastauto = IPPORT_HILASTAUTO; /* 65535 */
98 * Reserved ports accessible only to root. There are significant
99 * security considerations that must be accounted for when changing these,
100 * but the security benefits can be great. Please be careful.
102 int ipport_reservedhigh = IPPORT_RESERVED - 1; /* 1023 */
103 int ipport_reservedlow = 0;
105 /* Variables dealing with random ephemeral port allocation. */
106 int ipport_randomized = 1; /* user controlled via sysctl */
107 int ipport_randomcps = 10; /* user controlled via sysctl */
108 int ipport_randomtime = 45; /* user controlled via sysctl */
109 int ipport_stoprandom = 0; /* toggled by ipport_tick */
110 int ipport_tcpallocs;
111 int ipport_tcplastcount;
113 #define RANGECHK(var, min, max) \
114 if ((var) < (min)) { (var) = (min); } \
115 else if ((var) > (max)) { (var) = (max); }
118 sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS)
122 error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
124 RANGECHK(ipport_lowfirstauto, 1, IPPORT_RESERVED - 1);
125 RANGECHK(ipport_lowlastauto, 1, IPPORT_RESERVED - 1);
126 RANGECHK(ipport_firstauto, IPPORT_RESERVED, IPPORT_MAX);
127 RANGECHK(ipport_lastauto, IPPORT_RESERVED, IPPORT_MAX);
128 RANGECHK(ipport_hifirstauto, IPPORT_RESERVED, IPPORT_MAX);
129 RANGECHK(ipport_hilastauto, IPPORT_RESERVED, IPPORT_MAX);
136 SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0, "IP Ports");
138 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst, CTLTYPE_INT|CTLFLAG_RW,
139 &ipport_lowfirstauto, 0, &sysctl_net_ipport_check, "I", "");
140 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast, CTLTYPE_INT|CTLFLAG_RW,
141 &ipport_lowlastauto, 0, &sysctl_net_ipport_check, "I", "");
142 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first, CTLTYPE_INT|CTLFLAG_RW,
143 &ipport_firstauto, 0, &sysctl_net_ipport_check, "I", "");
144 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last, CTLTYPE_INT|CTLFLAG_RW,
145 &ipport_lastauto, 0, &sysctl_net_ipport_check, "I", "");
146 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst, CTLTYPE_INT|CTLFLAG_RW,
147 &ipport_hifirstauto, 0, &sysctl_net_ipport_check, "I", "");
148 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast, CTLTYPE_INT|CTLFLAG_RW,
149 &ipport_hilastauto, 0, &sysctl_net_ipport_check, "I", "");
150 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedhigh,
151 CTLFLAG_RW|CTLFLAG_SECURE, &ipport_reservedhigh, 0, "");
152 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedlow,
153 CTLFLAG_RW|CTLFLAG_SECURE, &ipport_reservedlow, 0, "");
154 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomized, CTLFLAG_RW,
155 &ipport_randomized, 0, "Enable random port allocation");
156 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomcps, CTLFLAG_RW,
157 &ipport_randomcps, 0, "Maximum number of random port "
158 "allocations before switching to a sequental one");
159 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomtime, CTLFLAG_RW,
160 &ipport_randomtime, 0, "Minimum time to keep sequental port "
161 "allocation before switching to a random one");
164 * in_pcb.c: manage the Protocol Control Blocks.
166 * NOTE: It is assumed that most of these functions will be called with
167 * the pcbinfo lock held, and often, the inpcb lock held, as these utility
168 * functions often modify hash chains or addresses in pcbs.
172 * Allocate a PCB and associate it with the socket.
173 * On success return with the PCB locked.
176 in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo)
181 INP_INFO_WLOCK_ASSERT(pcbinfo);
183 inp = uma_zalloc(pcbinfo->ipi_zone, M_NOWAIT);
186 bzero(inp, inp_zero_size);
187 inp->inp_pcbinfo = pcbinfo;
188 inp->inp_socket = so;
190 error = mac_inpcb_init(inp, M_NOWAIT);
194 mac_inpcb_create(so, inp);
199 error = ipsec_init_policy(so, &inp->inp_sp);
202 mac_inpcb_destroy(inp);
208 if (INP_SOCKAF(so) == AF_INET6) {
209 inp->inp_vflag |= INP_IPV6PROTO;
211 inp->inp_flags |= IN6P_IPV6_V6ONLY;
214 LIST_INSERT_HEAD(pcbinfo->ipi_listhead, inp, inp_list);
215 pcbinfo->ipi_count++;
216 so->so_pcb = (caddr_t)inp;
218 if (ip6_auto_flowlabel)
219 inp->inp_flags |= IN6P_AUTOFLOWLABEL;
222 inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
224 #if defined(IPSEC) || defined(MAC)
227 uma_zfree(pcbinfo->ipi_zone, inp);
233 in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
237 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
238 INP_LOCK_ASSERT(inp);
240 if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY)
242 anonport = inp->inp_lport == 0 && (nam == NULL ||
243 ((struct sockaddr_in *)nam)->sin_port == 0);
244 error = in_pcbbind_setup(inp, nam, &inp->inp_laddr.s_addr,
245 &inp->inp_lport, cred);
248 if (in_pcbinshash(inp) != 0) {
249 inp->inp_laddr.s_addr = INADDR_ANY;
254 inp->inp_flags |= INP_ANONPORT;
259 * Set up a bind operation on a PCB, performing port allocation
260 * as required, but do not actually modify the PCB. Callers can
261 * either complete the bind by setting inp_laddr/inp_lport and
262 * calling in_pcbinshash(), or they can just use the resulting
263 * port and address to authorise the sending of a once-off packet.
265 * On error, the values of *laddrp and *lportp are not changed.
268 in_pcbbind_setup(struct inpcb *inp, struct sockaddr *nam, in_addr_t *laddrp,
269 u_short *lportp, struct ucred *cred)
271 struct socket *so = inp->inp_socket;
272 unsigned short *lastport;
273 struct sockaddr_in *sin;
274 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
275 struct in_addr laddr;
277 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
278 int error, prison = 0;
281 INP_INFO_WLOCK_ASSERT(pcbinfo);
282 INP_LOCK_ASSERT(inp);
284 if (TAILQ_EMPTY(&in_ifaddrhead)) /* XXX broken! */
285 return (EADDRNOTAVAIL);
286 laddr.s_addr = *laddrp;
287 if (nam != NULL && laddr.s_addr != INADDR_ANY)
289 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
290 wild = INPLOOKUP_WILDCARD;
292 sin = (struct sockaddr_in *)nam;
293 if (nam->sa_len != sizeof (*sin))
297 * We should check the family, but old programs
298 * incorrectly fail to initialize it.
300 if (sin->sin_family != AF_INET)
301 return (EAFNOSUPPORT);
303 if (sin->sin_addr.s_addr != INADDR_ANY)
304 if (prison_ip(cred, 0, &sin->sin_addr.s_addr))
306 if (sin->sin_port != *lportp) {
307 /* Don't allow the port to change. */
310 lport = sin->sin_port;
312 /* NB: lport is left as 0 if the port isn't being changed. */
313 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
315 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
316 * allow complete duplication of binding if
317 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
318 * and a multicast address is bound on both
319 * new and duplicated sockets.
321 if (so->so_options & SO_REUSEADDR)
322 reuseport = SO_REUSEADDR|SO_REUSEPORT;
323 } else if (sin->sin_addr.s_addr != INADDR_ANY) {
324 sin->sin_port = 0; /* yech... */
325 bzero(&sin->sin_zero, sizeof(sin->sin_zero));
326 if (ifa_ifwithaddr((struct sockaddr *)sin) == 0)
327 return (EADDRNOTAVAIL);
329 laddr = sin->sin_addr;
335 if (ntohs(lport) <= ipport_reservedhigh &&
336 ntohs(lport) >= ipport_reservedlow &&
337 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT,
342 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
343 priv_check_cred(so->so_cred,
344 PRIV_NETINET_REUSEPORT, 0) != 0) {
345 t = in_pcblookup_local(inp->inp_pcbinfo,
346 sin->sin_addr, lport,
347 prison ? 0 : INPLOOKUP_WILDCARD);
350 * This entire block sorely needs a rewrite.
353 ((t->inp_vflag & INP_TIMEWAIT) == 0) &&
354 (so->so_type != SOCK_STREAM ||
355 ntohl(t->inp_faddr.s_addr) == INADDR_ANY) &&
356 (ntohl(sin->sin_addr.s_addr) != INADDR_ANY ||
357 ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
358 (t->inp_socket->so_options &
359 SO_REUSEPORT) == 0) &&
360 (so->so_cred->cr_uid !=
361 t->inp_socket->so_cred->cr_uid))
364 if (prison && prison_ip(cred, 0, &sin->sin_addr.s_addr))
365 return (EADDRNOTAVAIL);
366 t = in_pcblookup_local(pcbinfo, sin->sin_addr,
367 lport, prison ? 0 : wild);
368 if (t && (t->inp_vflag & INP_TIMEWAIT)) {
370 * XXXRW: If an incpb has had its timewait
371 * state recycled, we treat the address as
372 * being in use (for now). This is better
373 * than a panic, but not desirable.
377 (reuseport & tw->tw_so_options) == 0)
380 (reuseport & t->inp_socket->so_options) == 0) {
382 if (ntohl(sin->sin_addr.s_addr) !=
384 ntohl(t->inp_laddr.s_addr) !=
387 INP_SOCKAF(t->inp_socket))
399 if (laddr.s_addr != INADDR_ANY)
400 if (prison_ip(cred, 0, &laddr.s_addr))
403 if (inp->inp_flags & INP_HIGHPORT) {
404 first = ipport_hifirstauto; /* sysctl */
405 last = ipport_hilastauto;
406 lastport = &pcbinfo->ipi_lasthi;
407 } else if (inp->inp_flags & INP_LOWPORT) {
408 error = priv_check_cred(cred,
409 PRIV_NETINET_RESERVEDPORT, 0);
412 first = ipport_lowfirstauto; /* 1023 */
413 last = ipport_lowlastauto; /* 600 */
414 lastport = &pcbinfo->ipi_lastlow;
416 first = ipport_firstauto; /* sysctl */
417 last = ipport_lastauto;
418 lastport = &pcbinfo->ipi_lastport;
421 * For UDP, use random port allocation as long as the user
422 * allows it. For TCP (and as of yet unknown) connections,
423 * use random port allocation only if the user allows it AND
424 * ipport_tick() allows it.
426 if (ipport_randomized &&
427 (!ipport_stoprandom || pcbinfo == &udbinfo))
432 * It makes no sense to do random port allocation if
433 * we have the only port available.
437 /* Make sure to not include UDP packets in the count. */
438 if (pcbinfo != &udbinfo)
441 * Simple check to ensure all ports are not used up causing
444 * We split the two cases (up and down) so that the direction
445 * is not being tested on each round of the loop.
453 (arc4random() % (first - last));
454 count = first - last;
457 if (count-- < 0) /* completely used? */
458 return (EADDRNOTAVAIL);
460 if (*lastport > first || *lastport < last)
462 lport = htons(*lastport);
463 } while (in_pcblookup_local(pcbinfo, laddr, lport,
471 (arc4random() % (last - first));
472 count = last - first;
475 if (count-- < 0) /* completely used? */
476 return (EADDRNOTAVAIL);
478 if (*lastport < first || *lastport > last)
480 lport = htons(*lastport);
481 } while (in_pcblookup_local(pcbinfo, laddr, lport,
485 if (prison_ip(cred, 0, &laddr.s_addr))
487 *laddrp = laddr.s_addr;
493 * Connect from a socket to a specified address.
494 * Both address and port must be specified in argument sin.
495 * If don't have a local address for this socket yet,
499 in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
501 u_short lport, fport;
502 in_addr_t laddr, faddr;
505 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
506 INP_LOCK_ASSERT(inp);
508 lport = inp->inp_lport;
509 laddr = inp->inp_laddr.s_addr;
510 anonport = (lport == 0);
511 error = in_pcbconnect_setup(inp, nam, &laddr, &lport, &faddr, &fport,
516 /* Do the initial binding of the local address if required. */
517 if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) {
518 inp->inp_lport = lport;
519 inp->inp_laddr.s_addr = laddr;
520 if (in_pcbinshash(inp) != 0) {
521 inp->inp_laddr.s_addr = INADDR_ANY;
527 /* Commit the remaining changes. */
528 inp->inp_lport = lport;
529 inp->inp_laddr.s_addr = laddr;
530 inp->inp_faddr.s_addr = faddr;
531 inp->inp_fport = fport;
535 inp->inp_flags |= INP_ANONPORT;
540 * Set up for a connect from a socket to the specified address.
541 * On entry, *laddrp and *lportp should contain the current local
542 * address and port for the PCB; these are updated to the values
543 * that should be placed in inp_laddr and inp_lport to complete
546 * On success, *faddrp and *fportp will be set to the remote address
547 * and port. These are not updated in the error case.
549 * If the operation fails because the connection already exists,
550 * *oinpp will be set to the PCB of that connection so that the
551 * caller can decide to override it. In all other cases, *oinpp
555 in_pcbconnect_setup(struct inpcb *inp, struct sockaddr *nam,
556 in_addr_t *laddrp, u_short *lportp, in_addr_t *faddrp, u_short *fportp,
557 struct inpcb **oinpp, struct ucred *cred)
559 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
560 struct in_ifaddr *ia;
561 struct sockaddr_in sa;
562 struct ucred *socred;
564 struct in_addr laddr, faddr;
565 u_short lport, fport;
568 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
569 INP_LOCK_ASSERT(inp);
573 if (nam->sa_len != sizeof (*sin))
575 if (sin->sin_family != AF_INET)
576 return (EAFNOSUPPORT);
577 if (sin->sin_port == 0)
578 return (EADDRNOTAVAIL);
579 laddr.s_addr = *laddrp;
581 faddr = sin->sin_addr;
582 fport = sin->sin_port;
583 socred = inp->inp_socket->so_cred;
584 if (laddr.s_addr == INADDR_ANY && jailed(socred)) {
585 bzero(&sa, sizeof(sa));
586 sa.sin_addr.s_addr = htonl(prison_getip(socred));
587 sa.sin_len = sizeof(sa);
588 sa.sin_family = AF_INET;
589 error = in_pcbbind_setup(inp, (struct sockaddr *)&sa,
590 &laddr.s_addr, &lport, cred);
594 if (!TAILQ_EMPTY(&in_ifaddrhead)) {
596 * If the destination address is INADDR_ANY,
597 * use the primary local address.
598 * If the supplied address is INADDR_BROADCAST,
599 * and the primary interface supports broadcast,
600 * choose the broadcast address for that interface.
602 if (faddr.s_addr == INADDR_ANY)
603 faddr = IA_SIN(TAILQ_FIRST(&in_ifaddrhead))->sin_addr;
604 else if (faddr.s_addr == (u_long)INADDR_BROADCAST &&
605 (TAILQ_FIRST(&in_ifaddrhead)->ia_ifp->if_flags &
607 faddr = satosin(&TAILQ_FIRST(
608 &in_ifaddrhead)->ia_broadaddr)->sin_addr;
610 if (laddr.s_addr == INADDR_ANY) {
611 ia = (struct in_ifaddr *)0;
613 * If route is known our src addr is taken from the i/f,
616 * Find out route to destination
618 if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0)
619 ia = ip_rtaddr(faddr);
621 * If we found a route, use the address corresponding to
622 * the outgoing interface.
624 * Otherwise assume faddr is reachable on a directly connected
625 * network and try to find a corresponding interface to take
626 * the source address from.
629 bzero(&sa, sizeof(sa));
631 sa.sin_len = sizeof(sa);
632 sa.sin_family = AF_INET;
634 ia = ifatoia(ifa_ifwithdstaddr(sintosa(&sa)));
636 ia = ifatoia(ifa_ifwithnet(sintosa(&sa)));
638 return (ENETUNREACH);
641 * If the destination address is multicast and an outgoing
642 * interface has been set as a multicast option, use the
643 * address of that interface as our source address.
645 if (IN_MULTICAST(ntohl(faddr.s_addr)) &&
646 inp->inp_moptions != NULL) {
647 struct ip_moptions *imo;
650 imo = inp->inp_moptions;
651 if (imo->imo_multicast_ifp != NULL) {
652 ifp = imo->imo_multicast_ifp;
653 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
654 if (ia->ia_ifp == ifp)
657 return (EADDRNOTAVAIL);
660 laddr = ia->ia_addr.sin_addr;
663 oinp = in_pcblookup_hash(inp->inp_pcbinfo, faddr, fport, laddr, lport,
671 error = in_pcbbind_setup(inp, NULL, &laddr.s_addr, &lport,
676 *laddrp = laddr.s_addr;
678 *faddrp = faddr.s_addr;
684 in_pcbdisconnect(struct inpcb *inp)
687 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
688 INP_LOCK_ASSERT(inp);
690 inp->inp_faddr.s_addr = INADDR_ANY;
696 * In the old world order, in_pcbdetach() served two functions: to detach the
697 * pcb from the socket/potentially free the socket, and to free the pcb
698 * itself. In the new world order, the protocol code is responsible for
699 * managing the relationship with the socket, and this code simply frees the
703 in_pcbdetach(struct inpcb *inp)
706 KASSERT(inp->inp_socket != NULL, ("in_pcbdetach: inp_socket == NULL"));
707 inp->inp_socket->so_pcb = NULL;
708 inp->inp_socket = NULL;
712 in_pcbfree(struct inpcb *inp)
714 struct inpcbinfo *ipi = inp->inp_pcbinfo;
716 KASSERT(inp->inp_socket == NULL, ("in_pcbfree: inp_socket != NULL"));
717 INP_INFO_WLOCK_ASSERT(ipi);
718 INP_LOCK_ASSERT(inp);
721 ipsec4_delete_pcbpolicy(inp);
723 inp->inp_gencnt = ++ipi->ipi_gencnt;
725 if (inp->inp_options)
726 (void)m_free(inp->inp_options);
727 if (inp->inp_moptions != NULL)
728 inp_freemoptions(inp->inp_moptions);
732 mac_inpcb_destroy(inp);
735 uma_zfree(ipi->ipi_zone, inp);
739 * TCP needs to maintain its inpcb structure after the TCP connection has
740 * been torn down. However, it must be disconnected from the inpcb hashes as
741 * it must not prevent binding of future connections to the same port/ip
742 * combination by other inpcbs.
745 in_pcbdrop(struct inpcb *inp)
748 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
749 INP_LOCK_ASSERT(inp);
751 inp->inp_vflag |= INP_DROPPED;
752 if (inp->inp_lport) {
753 struct inpcbport *phd = inp->inp_phd;
755 LIST_REMOVE(inp, inp_hash);
756 LIST_REMOVE(inp, inp_portlist);
757 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
758 LIST_REMOVE(phd, phd_hash);
766 * Common routines to return the socket addresses associated with inpcbs.
769 in_sockaddr(in_port_t port, struct in_addr *addr_p)
771 struct sockaddr_in *sin;
773 MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
775 sin->sin_family = AF_INET;
776 sin->sin_len = sizeof(*sin);
777 sin->sin_addr = *addr_p;
778 sin->sin_port = port;
780 return (struct sockaddr *)sin;
784 in_getsockaddr(struct socket *so, struct sockaddr **nam)
791 KASSERT(inp != NULL, ("in_getsockaddr: inp == NULL"));
794 port = inp->inp_lport;
795 addr = inp->inp_laddr;
798 *nam = in_sockaddr(port, &addr);
803 in_getpeeraddr(struct socket *so, struct sockaddr **nam)
810 KASSERT(inp != NULL, ("in_getpeeraddr: inp == NULL"));
813 port = inp->inp_fport;
814 addr = inp->inp_faddr;
817 *nam = in_sockaddr(port, &addr);
822 in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno,
823 struct inpcb *(*notify)(struct inpcb *, int))
825 struct inpcb *inp, *ninp;
826 struct inpcbhead *head;
828 INP_INFO_WLOCK(pcbinfo);
829 head = pcbinfo->ipi_listhead;
830 for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) {
832 ninp = LIST_NEXT(inp, inp_list);
834 if ((inp->inp_vflag & INP_IPV4) == 0) {
839 if (inp->inp_faddr.s_addr != faddr.s_addr ||
840 inp->inp_socket == NULL) {
844 if ((*notify)(inp, errno))
847 INP_INFO_WUNLOCK(pcbinfo);
851 in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
854 struct ip_moptions *imo;
857 INP_INFO_RLOCK(pcbinfo);
858 LIST_FOREACH(inp, pcbinfo->ipi_listhead, inp_list) {
860 imo = inp->inp_moptions;
861 if ((inp->inp_vflag & INP_IPV4) &&
864 * Unselect the outgoing interface if it is being
867 if (imo->imo_multicast_ifp == ifp)
868 imo->imo_multicast_ifp = NULL;
871 * Drop multicast group membership if we joined
872 * through the interface being detached.
874 for (i = 0, gap = 0; i < imo->imo_num_memberships;
876 if (imo->imo_membership[i]->inm_ifp == ifp) {
877 in_delmulti(imo->imo_membership[i]);
880 imo->imo_membership[i - gap] =
881 imo->imo_membership[i];
883 imo->imo_num_memberships -= gap;
887 INP_INFO_RUNLOCK(pcbinfo);
891 * Lookup a PCB based on the local address and port.
893 #define INP_LOOKUP_MAPPED_PCB_COST 3
895 in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr,
896 u_int lport_arg, int wild_okay)
900 int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST;
905 u_short lport = lport_arg;
907 INP_INFO_WLOCK_ASSERT(pcbinfo);
910 struct inpcbhead *head;
912 * Look for an unconnected (wildcard foreign addr) PCB that
913 * matches the local address and port we're looking for.
915 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
916 0, pcbinfo->ipi_hashmask)];
917 LIST_FOREACH(inp, head, inp_hash) {
919 if ((inp->inp_vflag & INP_IPV4) == 0)
922 if (inp->inp_faddr.s_addr == INADDR_ANY &&
923 inp->inp_laddr.s_addr == laddr.s_addr &&
924 inp->inp_lport == lport) {
936 struct inpcbporthead *porthash;
937 struct inpcbport *phd;
938 struct inpcb *match = NULL;
940 * Best fit PCB lookup.
942 * First see if this local port is in use by looking on the
945 porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
946 pcbinfo->ipi_porthashmask)];
947 LIST_FOREACH(phd, porthash, phd_hash) {
948 if (phd->phd_port == lport)
953 * Port is in use by one or more PCBs. Look for best
956 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
959 if ((inp->inp_vflag & INP_IPV4) == 0)
962 * We never select the PCB that has
963 * INP_IPV6 flag and is bound to :: if
964 * we have another PCB which is bound
965 * to 0.0.0.0. If a PCB has the
966 * INP_IPV6 flag, then we set its cost
967 * higher than IPv4 only PCBs.
969 * Note that the case only happens
970 * when a socket is bound to ::, under
971 * the condition that the use of the
972 * mapped address is allowed.
974 if ((inp->inp_vflag & INP_IPV6) != 0)
975 wildcard += INP_LOOKUP_MAPPED_PCB_COST;
977 if (inp->inp_faddr.s_addr != INADDR_ANY)
979 if (inp->inp_laddr.s_addr != INADDR_ANY) {
980 if (laddr.s_addr == INADDR_ANY)
982 else if (inp->inp_laddr.s_addr != laddr.s_addr)
985 if (laddr.s_addr != INADDR_ANY)
988 if (wildcard < matchwild) {
990 matchwild = wildcard;
991 if (matchwild == 0) {
1000 #undef INP_LOOKUP_MAPPED_PCB_COST
1003 * Lookup PCB in hash list.
1006 in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1007 u_int fport_arg, struct in_addr laddr, u_int lport_arg, int wildcard,
1010 struct inpcbhead *head;
1012 u_short fport = fport_arg, lport = lport_arg;
1014 INP_INFO_RLOCK_ASSERT(pcbinfo);
1017 * First look for an exact match.
1019 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(faddr.s_addr, lport, fport,
1020 pcbinfo->ipi_hashmask)];
1021 LIST_FOREACH(inp, head, inp_hash) {
1023 if ((inp->inp_vflag & INP_IPV4) == 0)
1026 if (inp->inp_faddr.s_addr == faddr.s_addr &&
1027 inp->inp_laddr.s_addr == laddr.s_addr &&
1028 inp->inp_fport == fport &&
1029 inp->inp_lport == lport)
1034 * Then look for a wildcard match, if requested.
1037 struct inpcb *local_wild = NULL;
1039 struct inpcb *local_wild_mapped = NULL;
1042 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
1043 0, pcbinfo->ipi_hashmask)];
1044 LIST_FOREACH(inp, head, inp_hash) {
1046 if ((inp->inp_vflag & INP_IPV4) == 0)
1049 if (inp->inp_faddr.s_addr == INADDR_ANY &&
1050 inp->inp_lport == lport) {
1051 if (ifp && ifp->if_type == IFT_FAITH &&
1052 (inp->inp_flags & INP_FAITH) == 0)
1054 if (inp->inp_laddr.s_addr == laddr.s_addr)
1056 else if (inp->inp_laddr.s_addr == INADDR_ANY) {
1058 if (INP_CHECK_SOCKAF(inp->inp_socket,
1060 local_wild_mapped = inp;
1068 if (local_wild == NULL)
1069 return (local_wild_mapped);
1071 return (local_wild);
1077 * Insert PCB onto various hash lists.
1080 in_pcbinshash(struct inpcb *inp)
1082 struct inpcbhead *pcbhash;
1083 struct inpcbporthead *pcbporthash;
1084 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1085 struct inpcbport *phd;
1086 u_int32_t hashkey_faddr;
1088 INP_INFO_WLOCK_ASSERT(pcbinfo);
1089 INP_LOCK_ASSERT(inp);
1092 if (inp->inp_vflag & INP_IPV6)
1093 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1096 hashkey_faddr = inp->inp_faddr.s_addr;
1098 pcbhash = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
1099 inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
1101 pcbporthash = &pcbinfo->ipi_porthashbase[
1102 INP_PCBPORTHASH(inp->inp_lport, pcbinfo->ipi_porthashmask)];
1105 * Go through port list and look for a head for this lport.
1107 LIST_FOREACH(phd, pcbporthash, phd_hash) {
1108 if (phd->phd_port == inp->inp_lport)
1112 * If none exists, malloc one and tack it on.
1115 MALLOC(phd, struct inpcbport *, sizeof(struct inpcbport), M_PCB, M_NOWAIT);
1117 return (ENOBUFS); /* XXX */
1119 phd->phd_port = inp->inp_lport;
1120 LIST_INIT(&phd->phd_pcblist);
1121 LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
1124 LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
1125 LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
1130 * Move PCB to the proper hash bucket when { faddr, fport } have been
1131 * changed. NOTE: This does not handle the case of the lport changing (the
1132 * hashed port list would have to be updated as well), so the lport must
1133 * not change after in_pcbinshash() has been called.
1136 in_pcbrehash(struct inpcb *inp)
1138 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1139 struct inpcbhead *head;
1140 u_int32_t hashkey_faddr;
1142 INP_INFO_WLOCK_ASSERT(pcbinfo);
1143 INP_LOCK_ASSERT(inp);
1146 if (inp->inp_vflag & INP_IPV6)
1147 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1150 hashkey_faddr = inp->inp_faddr.s_addr;
1152 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(hashkey_faddr,
1153 inp->inp_lport, inp->inp_fport, pcbinfo->ipi_hashmask)];
1155 LIST_REMOVE(inp, inp_hash);
1156 LIST_INSERT_HEAD(head, inp, inp_hash);
1160 * Remove PCB from various lists.
1163 in_pcbremlists(struct inpcb *inp)
1165 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1167 INP_INFO_WLOCK_ASSERT(pcbinfo);
1168 INP_LOCK_ASSERT(inp);
1170 inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
1171 if (inp->inp_lport) {
1172 struct inpcbport *phd = inp->inp_phd;
1174 LIST_REMOVE(inp, inp_hash);
1175 LIST_REMOVE(inp, inp_portlist);
1176 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1177 LIST_REMOVE(phd, phd_hash);
1181 LIST_REMOVE(inp, inp_list);
1182 pcbinfo->ipi_count--;
1186 * A set label operation has occurred at the socket layer, propagate the
1187 * label change into the in_pcb for the socket.
1190 in_pcbsosetlabel(struct socket *so)
1195 inp = sotoinpcb(so);
1196 KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL"));
1200 mac_inpcb_sosetlabel(so, inp);
1207 * ipport_tick runs once per second, determining if random port allocation
1208 * should be continued. If more than ipport_randomcps ports have been
1209 * allocated in the last second, then we return to sequential port
1210 * allocation. We return to random allocation only once we drop below
1211 * ipport_randomcps for at least ipport_randomtime seconds.
1214 ipport_tick(void *xtp)
1217 if (ipport_tcpallocs <= ipport_tcplastcount + ipport_randomcps) {
1218 if (ipport_stoprandom > 0)
1219 ipport_stoprandom--;
1221 ipport_stoprandom = ipport_randomtime;
1222 ipport_tcplastcount = ipport_tcpallocs;
1223 callout_reset(&ipport_tick_callout, hz, ipport_tick, NULL);
1228 db_print_indent(int indent)
1232 for (i = 0; i < indent; i++)
1237 db_print_inconninfo(struct in_conninfo *inc, const char *name, int indent)
1239 char faddr_str[48], laddr_str[48];
1241 db_print_indent(indent);
1242 db_printf("%s at %p\n", name, inc);
1247 if (inc->inc_flags == 1) {
1249 ip6_sprintf(laddr_str, &inc->inc6_laddr);
1250 ip6_sprintf(faddr_str, &inc->inc6_faddr);
1254 inet_ntoa_r(inc->inc_laddr, laddr_str);
1255 inet_ntoa_r(inc->inc_faddr, faddr_str);
1259 db_print_indent(indent);
1260 db_printf("inc_laddr %s inc_lport %u\n", laddr_str,
1261 ntohs(inc->inc_lport));
1262 db_print_indent(indent);
1263 db_printf("inc_faddr %s inc_fport %u\n", faddr_str,
1264 ntohs(inc->inc_fport));
1268 db_print_inpflags(int inp_flags)
1273 if (inp_flags & INP_RECVOPTS) {
1274 db_printf("%sINP_RECVOPTS", comma ? ", " : "");
1277 if (inp_flags & INP_RECVRETOPTS) {
1278 db_printf("%sINP_RECVRETOPTS", comma ? ", " : "");
1281 if (inp_flags & INP_RECVDSTADDR) {
1282 db_printf("%sINP_RECVDSTADDR", comma ? ", " : "");
1285 if (inp_flags & INP_HDRINCL) {
1286 db_printf("%sINP_HDRINCL", comma ? ", " : "");
1289 if (inp_flags & INP_HIGHPORT) {
1290 db_printf("%sINP_HIGHPORT", comma ? ", " : "");
1293 if (inp_flags & INP_LOWPORT) {
1294 db_printf("%sINP_LOWPORT", comma ? ", " : "");
1297 if (inp_flags & INP_ANONPORT) {
1298 db_printf("%sINP_ANONPORT", comma ? ", " : "");
1301 if (inp_flags & INP_RECVIF) {
1302 db_printf("%sINP_RECVIF", comma ? ", " : "");
1305 if (inp_flags & INP_MTUDISC) {
1306 db_printf("%sINP_MTUDISC", comma ? ", " : "");
1309 if (inp_flags & INP_FAITH) {
1310 db_printf("%sINP_FAITH", comma ? ", " : "");
1313 if (inp_flags & INP_RECVTTL) {
1314 db_printf("%sINP_RECVTTL", comma ? ", " : "");
1317 if (inp_flags & INP_DONTFRAG) {
1318 db_printf("%sINP_DONTFRAG", comma ? ", " : "");
1321 if (inp_flags & IN6P_IPV6_V6ONLY) {
1322 db_printf("%sIN6P_IPV6_V6ONLY", comma ? ", " : "");
1325 if (inp_flags & IN6P_PKTINFO) {
1326 db_printf("%sIN6P_PKTINFO", comma ? ", " : "");
1329 if (inp_flags & IN6P_HOPLIMIT) {
1330 db_printf("%sIN6P_HOPLIMIT", comma ? ", " : "");
1333 if (inp_flags & IN6P_HOPOPTS) {
1334 db_printf("%sIN6P_HOPOPTS", comma ? ", " : "");
1337 if (inp_flags & IN6P_DSTOPTS) {
1338 db_printf("%sIN6P_DSTOPTS", comma ? ", " : "");
1341 if (inp_flags & IN6P_RTHDR) {
1342 db_printf("%sIN6P_RTHDR", comma ? ", " : "");
1345 if (inp_flags & IN6P_RTHDRDSTOPTS) {
1346 db_printf("%sIN6P_RTHDRDSTOPTS", comma ? ", " : "");
1349 if (inp_flags & IN6P_TCLASS) {
1350 db_printf("%sIN6P_TCLASS", comma ? ", " : "");
1353 if (inp_flags & IN6P_AUTOFLOWLABEL) {
1354 db_printf("%sIN6P_AUTOFLOWLABEL", comma ? ", " : "");
1357 if (inp_flags & IN6P_RFC2292) {
1358 db_printf("%sIN6P_RFC2292", comma ? ", " : "");
1361 if (inp_flags & IN6P_MTU) {
1362 db_printf("IN6P_MTU%s", comma ? ", " : "");
1368 db_print_inpvflag(u_char inp_vflag)
1373 if (inp_vflag & INP_IPV4) {
1374 db_printf("%sINP_IPV4", comma ? ", " : "");
1377 if (inp_vflag & INP_IPV6) {
1378 db_printf("%sINP_IPV6", comma ? ", " : "");
1381 if (inp_vflag & INP_IPV6PROTO) {
1382 db_printf("%sINP_IPV6PROTO", comma ? ", " : "");
1385 if (inp_vflag & INP_TIMEWAIT) {
1386 db_printf("%sINP_TIMEWAIT", comma ? ", " : "");
1389 if (inp_vflag & INP_ONESBCAST) {
1390 db_printf("%sINP_ONESBCAST", comma ? ", " : "");
1393 if (inp_vflag & INP_DROPPED) {
1394 db_printf("%sINP_DROPPED", comma ? ", " : "");
1397 if (inp_vflag & INP_SOCKREF) {
1398 db_printf("%sINP_SOCKREF", comma ? ", " : "");
1404 db_print_inpcb(struct inpcb *inp, const char *name, int indent)
1407 db_print_indent(indent);
1408 db_printf("%s at %p\n", name, inp);
1412 db_print_indent(indent);
1413 db_printf("inp_flow: 0x%x\n", inp->inp_flow);
1415 db_print_inconninfo(&inp->inp_inc, "inp_conninfo", indent);
1417 db_print_indent(indent);
1418 db_printf("inp_ppcb: %p inp_pcbinfo: %p inp_socket: %p\n",
1419 inp->inp_ppcb, inp->inp_pcbinfo, inp->inp_socket);
1421 db_print_indent(indent);
1422 db_printf("inp_label: %p inp_flags: 0x%x (",
1423 inp->inp_label, inp->inp_flags);
1424 db_print_inpflags(inp->inp_flags);
1427 db_print_indent(indent);
1428 db_printf("inp_sp: %p inp_vflag: 0x%x (", inp->inp_sp,
1430 db_print_inpvflag(inp->inp_vflag);
1433 db_print_indent(indent);
1434 db_printf("inp_ip_ttl: %d inp_ip_p: %d inp_ip_minttl: %d\n",
1435 inp->inp_ip_ttl, inp->inp_ip_p, inp->inp_ip_minttl);
1437 db_print_indent(indent);
1439 if (inp->inp_vflag & INP_IPV6) {
1440 db_printf("in6p_options: %p in6p_outputopts: %p "
1441 "in6p_moptions: %p\n", inp->in6p_options,
1442 inp->in6p_outputopts, inp->in6p_moptions);
1443 db_printf("in6p_icmp6filt: %p in6p_cksum %d "
1444 "in6p_hops %u\n", inp->in6p_icmp6filt, inp->in6p_cksum,
1449 db_printf("inp_ip_tos: %d inp_ip_options: %p "
1450 "inp_ip_moptions: %p\n", inp->inp_ip_tos,
1451 inp->inp_options, inp->inp_moptions);
1454 db_print_indent(indent);
1455 db_printf("inp_phd: %p inp_gencnt: %ju\n", inp->inp_phd,
1456 (uintmax_t)inp->inp_gencnt);
1459 DB_SHOW_COMMAND(inpcb, db_show_inpcb)
1464 db_printf("usage: show inpcb <addr>\n");
1467 inp = (struct inpcb *)addr;
1469 db_print_inpcb(inp, "inpcb", 0);