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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)in_pcb.c 8.4 (Berkeley) 5/24/95
33 #include "opt_ipsec.h"
34 #include "opt_inet6.h"
37 #include <sys/param.h>
38 #include <sys/systm.h>
40 #include <sys/malloc.h>
42 #include <sys/domain.h>
43 #include <sys/protosw.h>
44 #include <sys/socket.h>
45 #include <sys/socketvar.h>
48 #include <sys/kernel.h>
49 #include <sys/sysctl.h>
54 #include <net/if_types.h>
55 #include <net/route.h>
57 #include <netinet/in.h>
58 #include <netinet/in_pcb.h>
59 #include <netinet/in_var.h>
60 #include <netinet/ip_var.h>
61 #include <netinet/tcp_var.h>
62 #include <netinet/udp.h>
63 #include <netinet/udp_var.h>
65 #include <netinet/ip6.h>
66 #include <netinet6/ip6_var.h>
70 #include <netinet6/ipsec.h>
71 #include <netkey/key.h>
75 #if defined(IPSEC) || defined(IPSEC_ESP)
76 #error "Bad idea: don't compile with both IPSEC and FAST_IPSEC!"
79 #include <netipsec/ipsec.h>
80 #include <netipsec/key.h>
81 #endif /* FAST_IPSEC */
84 * These configure the range of local port addresses assigned to
85 * "unspecified" outgoing connections/packets/whatever.
87 int ipport_lowfirstauto = IPPORT_RESERVED - 1; /* 1023 */
88 int ipport_lowlastauto = IPPORT_RESERVEDSTART; /* 600 */
89 int ipport_firstauto = IPPORT_HIFIRSTAUTO; /* 49152 */
90 int ipport_lastauto = IPPORT_HILASTAUTO; /* 65535 */
91 int ipport_hifirstauto = IPPORT_HIFIRSTAUTO; /* 49152 */
92 int ipport_hilastauto = IPPORT_HILASTAUTO; /* 65535 */
95 * Reserved ports accessible only to root. There are significant
96 * security considerations that must be accounted for when changing these,
97 * but the security benefits can be great. Please be careful.
99 int ipport_reservedhigh = IPPORT_RESERVED - 1; /* 1023 */
100 int ipport_reservedlow = 0;
102 /* Variables dealing with random ephemeral port allocation. */
103 int ipport_randomized = 1; /* user controlled via sysctl */
104 int ipport_randomcps = 10; /* user controlled via sysctl */
105 int ipport_randomtime = 45; /* user controlled via sysctl */
106 int ipport_stoprandom = 0; /* toggled by ipport_tick */
107 int ipport_tcpallocs;
108 int ipport_tcplastcount;
110 #define RANGECHK(var, min, max) \
111 if ((var) < (min)) { (var) = (min); } \
112 else if ((var) > (max)) { (var) = (max); }
115 sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS)
119 error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
121 RANGECHK(ipport_lowfirstauto, 1, IPPORT_RESERVED - 1);
122 RANGECHK(ipport_lowlastauto, 1, IPPORT_RESERVED - 1);
123 RANGECHK(ipport_firstauto, IPPORT_RESERVED, IPPORT_MAX);
124 RANGECHK(ipport_lastauto, IPPORT_RESERVED, IPPORT_MAX);
125 RANGECHK(ipport_hifirstauto, IPPORT_RESERVED, IPPORT_MAX);
126 RANGECHK(ipport_hilastauto, IPPORT_RESERVED, IPPORT_MAX);
133 SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0, "IP Ports");
135 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst, CTLTYPE_INT|CTLFLAG_RW,
136 &ipport_lowfirstauto, 0, &sysctl_net_ipport_check, "I", "");
137 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast, CTLTYPE_INT|CTLFLAG_RW,
138 &ipport_lowlastauto, 0, &sysctl_net_ipport_check, "I", "");
139 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first, CTLTYPE_INT|CTLFLAG_RW,
140 &ipport_firstauto, 0, &sysctl_net_ipport_check, "I", "");
141 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last, CTLTYPE_INT|CTLFLAG_RW,
142 &ipport_lastauto, 0, &sysctl_net_ipport_check, "I", "");
143 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst, CTLTYPE_INT|CTLFLAG_RW,
144 &ipport_hifirstauto, 0, &sysctl_net_ipport_check, "I", "");
145 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast, CTLTYPE_INT|CTLFLAG_RW,
146 &ipport_hilastauto, 0, &sysctl_net_ipport_check, "I", "");
147 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedhigh,
148 CTLFLAG_RW|CTLFLAG_SECURE, &ipport_reservedhigh, 0, "");
149 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedlow,
150 CTLFLAG_RW|CTLFLAG_SECURE, &ipport_reservedlow, 0, "");
151 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomized, CTLFLAG_RW,
152 &ipport_randomized, 0, "Enable random port allocation");
153 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomcps, CTLFLAG_RW,
154 &ipport_randomcps, 0, "Maximum number of random port "
155 "allocations before switching to a sequental one");
156 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomtime, CTLFLAG_RW,
157 &ipport_randomtime, 0, "Minimum time to keep sequental port "
158 "allocation before switching to a random one");
161 * in_pcb.c: manage the Protocol Control Blocks.
163 * NOTE: It is assumed that most of these functions will be called with
164 * the pcbinfo lock held, and often, the inpcb lock held, as these utility
165 * functions often modify hash chains or addresses in pcbs.
169 * Allocate a PCB and associate it with the socket.
170 * On success return with the PCB locked.
173 in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo)
178 INP_INFO_WLOCK_ASSERT(pcbinfo);
180 inp = uma_zalloc(pcbinfo->ipi_zone, M_NOWAIT);
183 bzero(inp,inp_zero_size);
184 inp->inp_pcbinfo = pcbinfo;
185 inp->inp_socket = so;
187 error = mac_init_inpcb(inp, M_NOWAIT);
191 mac_create_inpcb_from_socket(so, inp);
194 #if defined(IPSEC) || defined(FAST_IPSEC)
196 error = ipsec_init_policy(so, &inp->inp_sp);
198 error = ipsec_init_pcbpolicy(so, &inp->inp_sp);
204 if (INP_SOCKAF(so) == AF_INET6) {
205 inp->inp_vflag |= INP_IPV6PROTO;
207 inp->inp_flags |= IN6P_IPV6_V6ONLY;
210 LIST_INSERT_HEAD(pcbinfo->listhead, inp, inp_list);
211 pcbinfo->ipi_count++;
212 so->so_pcb = (caddr_t)inp;
214 if (ip6_auto_flowlabel)
215 inp->inp_flags |= IN6P_AUTOFLOWLABEL;
218 inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
220 #if defined(IPSEC) || defined(FAST_IPSEC) || defined(MAC)
223 uma_zfree(pcbinfo->ipi_zone, inp);
229 in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
233 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
234 INP_LOCK_ASSERT(inp);
236 if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY)
238 anonport = inp->inp_lport == 0 && (nam == NULL ||
239 ((struct sockaddr_in *)nam)->sin_port == 0);
240 error = in_pcbbind_setup(inp, nam, &inp->inp_laddr.s_addr,
241 &inp->inp_lport, cred);
244 if (in_pcbinshash(inp) != 0) {
245 inp->inp_laddr.s_addr = INADDR_ANY;
250 inp->inp_flags |= INP_ANONPORT;
255 * Set up a bind operation on a PCB, performing port allocation
256 * as required, but do not actually modify the PCB. Callers can
257 * either complete the bind by setting inp_laddr/inp_lport and
258 * calling in_pcbinshash(), or they can just use the resulting
259 * port and address to authorise the sending of a once-off packet.
261 * On error, the values of *laddrp and *lportp are not changed.
264 in_pcbbind_setup(struct inpcb *inp, struct sockaddr *nam, in_addr_t *laddrp,
265 u_short *lportp, struct ucred *cred)
267 struct socket *so = inp->inp_socket;
268 unsigned short *lastport;
269 struct sockaddr_in *sin;
270 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
271 struct in_addr laddr;
273 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
274 int error, prison = 0;
277 INP_INFO_WLOCK_ASSERT(pcbinfo);
278 INP_LOCK_ASSERT(inp);
280 if (TAILQ_EMPTY(&in_ifaddrhead)) /* XXX broken! */
281 return (EADDRNOTAVAIL);
282 laddr.s_addr = *laddrp;
283 if (nam != NULL && laddr.s_addr != INADDR_ANY)
285 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
286 wild = INPLOOKUP_WILDCARD;
288 sin = (struct sockaddr_in *)nam;
289 if (nam->sa_len != sizeof (*sin))
293 * We should check the family, but old programs
294 * incorrectly fail to initialize it.
296 if (sin->sin_family != AF_INET)
297 return (EAFNOSUPPORT);
299 if (sin->sin_addr.s_addr != INADDR_ANY)
300 if (prison_ip(cred, 0, &sin->sin_addr.s_addr))
302 if (sin->sin_port != *lportp) {
303 /* Don't allow the port to change. */
306 lport = sin->sin_port;
308 /* NB: lport is left as 0 if the port isn't being changed. */
309 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
311 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
312 * allow complete duplication of binding if
313 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
314 * and a multicast address is bound on both
315 * new and duplicated sockets.
317 if (so->so_options & SO_REUSEADDR)
318 reuseport = SO_REUSEADDR|SO_REUSEPORT;
319 } else if (sin->sin_addr.s_addr != INADDR_ANY) {
320 sin->sin_port = 0; /* yech... */
321 bzero(&sin->sin_zero, sizeof(sin->sin_zero));
322 if (ifa_ifwithaddr((struct sockaddr *)sin) == 0)
323 return (EADDRNOTAVAIL);
325 laddr = sin->sin_addr;
331 if (ntohs(lport) <= ipport_reservedhigh &&
332 ntohs(lport) >= ipport_reservedlow &&
333 suser_cred(cred, SUSER_ALLOWJAIL))
337 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
338 suser_cred(so->so_cred, SUSER_ALLOWJAIL) != 0) {
339 t = in_pcblookup_local(inp->inp_pcbinfo,
340 sin->sin_addr, lport,
341 prison ? 0 : INPLOOKUP_WILDCARD);
344 * This entire block sorely needs a rewrite.
347 ((t->inp_vflag & INP_TIMEWAIT) == 0) &&
348 (so->so_type != SOCK_STREAM ||
349 ntohl(t->inp_faddr.s_addr) == INADDR_ANY) &&
350 (ntohl(sin->sin_addr.s_addr) != INADDR_ANY ||
351 ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
352 (t->inp_socket->so_options &
353 SO_REUSEPORT) == 0) &&
354 (so->so_cred->cr_uid !=
355 t->inp_socket->so_cred->cr_uid))
358 if (prison && prison_ip(cred, 0, &sin->sin_addr.s_addr))
359 return (EADDRNOTAVAIL);
360 t = in_pcblookup_local(pcbinfo, sin->sin_addr,
361 lport, prison ? 0 : wild);
362 if (t && (t->inp_vflag & INP_TIMEWAIT)) {
364 * XXXRW: If an incpb has had its timewait
365 * state recycled, we treat the address as
366 * being in use (for now). This is better
367 * than a panic, but not desirable.
371 (reuseport & tw->tw_so_options) == 0)
374 (reuseport & t->inp_socket->so_options) == 0) {
376 if (ntohl(sin->sin_addr.s_addr) !=
378 ntohl(t->inp_laddr.s_addr) !=
381 INP_SOCKAF(t->inp_socket))
382 #endif /* defined(INET6) */
393 if (laddr.s_addr != INADDR_ANY)
394 if (prison_ip(cred, 0, &laddr.s_addr))
397 if (inp->inp_flags & INP_HIGHPORT) {
398 first = ipport_hifirstauto; /* sysctl */
399 last = ipport_hilastauto;
400 lastport = &pcbinfo->lasthi;
401 } else if (inp->inp_flags & INP_LOWPORT) {
402 if ((error = suser_cred(cred, SUSER_ALLOWJAIL)) != 0)
404 first = ipport_lowfirstauto; /* 1023 */
405 last = ipport_lowlastauto; /* 600 */
406 lastport = &pcbinfo->lastlow;
408 first = ipport_firstauto; /* sysctl */
409 last = ipport_lastauto;
410 lastport = &pcbinfo->lastport;
413 * For UDP, use random port allocation as long as the user
414 * allows it. For TCP (and as of yet unknown) connections,
415 * use random port allocation only if the user allows it AND
416 * ipport_tick() allows it.
418 if (ipport_randomized &&
419 (!ipport_stoprandom || pcbinfo == &udbinfo))
424 * It makes no sense to do random port allocation if
425 * we have the only port available.
429 /* Make sure to not include UDP packets in the count. */
430 if (pcbinfo != &udbinfo)
433 * Simple check to ensure all ports are not used up causing
436 * We split the two cases (up and down) so that the direction
437 * is not being tested on each round of the loop.
445 (arc4random() % (first - last));
446 count = first - last;
449 if (count-- < 0) /* completely used? */
450 return (EADDRNOTAVAIL);
452 if (*lastport > first || *lastport < last)
454 lport = htons(*lastport);
455 } while (in_pcblookup_local(pcbinfo, laddr, lport,
463 (arc4random() % (last - first));
464 count = last - first;
467 if (count-- < 0) /* completely used? */
468 return (EADDRNOTAVAIL);
470 if (*lastport < first || *lastport > last)
472 lport = htons(*lastport);
473 } while (in_pcblookup_local(pcbinfo, laddr, lport,
477 if (prison_ip(cred, 0, &laddr.s_addr))
479 *laddrp = laddr.s_addr;
485 * Connect from a socket to a specified address.
486 * Both address and port must be specified in argument sin.
487 * If don't have a local address for this socket yet,
491 in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
493 u_short lport, fport;
494 in_addr_t laddr, faddr;
497 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
498 INP_LOCK_ASSERT(inp);
500 lport = inp->inp_lport;
501 laddr = inp->inp_laddr.s_addr;
502 anonport = (lport == 0);
503 error = in_pcbconnect_setup(inp, nam, &laddr, &lport, &faddr, &fport,
508 /* Do the initial binding of the local address if required. */
509 if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) {
510 inp->inp_lport = lport;
511 inp->inp_laddr.s_addr = laddr;
512 if (in_pcbinshash(inp) != 0) {
513 inp->inp_laddr.s_addr = INADDR_ANY;
519 /* Commit the remaining changes. */
520 inp->inp_lport = lport;
521 inp->inp_laddr.s_addr = laddr;
522 inp->inp_faddr.s_addr = faddr;
523 inp->inp_fport = fport;
526 if (inp->inp_socket->so_type == SOCK_STREAM)
527 ipsec_pcbconn(inp->inp_sp);
530 inp->inp_flags |= INP_ANONPORT;
535 * Set up for a connect from a socket to the specified address.
536 * On entry, *laddrp and *lportp should contain the current local
537 * address and port for the PCB; these are updated to the values
538 * that should be placed in inp_laddr and inp_lport to complete
541 * On success, *faddrp and *fportp will be set to the remote address
542 * and port. These are not updated in the error case.
544 * If the operation fails because the connection already exists,
545 * *oinpp will be set to the PCB of that connection so that the
546 * caller can decide to override it. In all other cases, *oinpp
550 in_pcbconnect_setup(struct inpcb *inp, struct sockaddr *nam,
551 in_addr_t *laddrp, u_short *lportp, in_addr_t *faddrp, u_short *fportp,
552 struct inpcb **oinpp, struct ucred *cred)
554 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
555 struct in_ifaddr *ia;
556 struct sockaddr_in sa;
557 struct ucred *socred;
559 struct in_addr laddr, faddr;
560 u_short lport, fport;
563 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
564 INP_LOCK_ASSERT(inp);
568 if (nam->sa_len != sizeof (*sin))
570 if (sin->sin_family != AF_INET)
571 return (EAFNOSUPPORT);
572 if (sin->sin_port == 0)
573 return (EADDRNOTAVAIL);
574 laddr.s_addr = *laddrp;
576 faddr = sin->sin_addr;
577 fport = sin->sin_port;
578 socred = inp->inp_socket->so_cred;
579 if (laddr.s_addr == INADDR_ANY && jailed(socred)) {
580 bzero(&sa, sizeof(sa));
581 sa.sin_addr.s_addr = htonl(prison_getip(socred));
582 sa.sin_len = sizeof(sa);
583 sa.sin_family = AF_INET;
584 error = in_pcbbind_setup(inp, (struct sockaddr *)&sa,
585 &laddr.s_addr, &lport, cred);
589 if (!TAILQ_EMPTY(&in_ifaddrhead)) {
591 * If the destination address is INADDR_ANY,
592 * use the primary local address.
593 * If the supplied address is INADDR_BROADCAST,
594 * and the primary interface supports broadcast,
595 * choose the broadcast address for that interface.
597 if (faddr.s_addr == INADDR_ANY)
598 faddr = IA_SIN(TAILQ_FIRST(&in_ifaddrhead))->sin_addr;
599 else if (faddr.s_addr == (u_long)INADDR_BROADCAST &&
600 (TAILQ_FIRST(&in_ifaddrhead)->ia_ifp->if_flags &
602 faddr = satosin(&TAILQ_FIRST(
603 &in_ifaddrhead)->ia_broadaddr)->sin_addr;
605 if (laddr.s_addr == INADDR_ANY) {
606 ia = (struct in_ifaddr *)0;
608 * If route is known our src addr is taken from the i/f,
611 * Find out route to destination
613 if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0)
614 ia = ip_rtaddr(faddr);
616 * If we found a route, use the address corresponding to
617 * the outgoing interface.
619 * Otherwise assume faddr is reachable on a directly connected
620 * network and try to find a corresponding interface to take
621 * the source address from.
624 bzero(&sa, sizeof(sa));
626 sa.sin_len = sizeof(sa);
627 sa.sin_family = AF_INET;
629 ia = ifatoia(ifa_ifwithdstaddr(sintosa(&sa)));
631 ia = ifatoia(ifa_ifwithnet(sintosa(&sa)));
633 return (ENETUNREACH);
636 * If the destination address is multicast and an outgoing
637 * interface has been set as a multicast option, use the
638 * address of that interface as our source address.
640 if (IN_MULTICAST(ntohl(faddr.s_addr)) &&
641 inp->inp_moptions != NULL) {
642 struct ip_moptions *imo;
645 imo = inp->inp_moptions;
646 if (imo->imo_multicast_ifp != NULL) {
647 ifp = imo->imo_multicast_ifp;
648 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
649 if (ia->ia_ifp == ifp)
652 return (EADDRNOTAVAIL);
655 laddr = ia->ia_addr.sin_addr;
658 oinp = in_pcblookup_hash(inp->inp_pcbinfo, faddr, fport, laddr, lport,
666 error = in_pcbbind_setup(inp, NULL, &laddr.s_addr, &lport,
671 *laddrp = laddr.s_addr;
673 *faddrp = faddr.s_addr;
679 in_pcbdisconnect(struct inpcb *inp)
682 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
683 INP_LOCK_ASSERT(inp);
685 inp->inp_faddr.s_addr = INADDR_ANY;
689 ipsec_pcbdisconn(inp->inp_sp);
694 * In the old world order, in_pcbdetach() served two functions: to detach the
695 * pcb from the socket/potentially free the socket, and to free the pcb
696 * itself. In the new world order, the protocol code is responsible for
697 * managing the relationship with the socket, and this code simply frees the
701 in_pcbdetach(struct inpcb *inp)
704 KASSERT(inp->inp_socket != NULL, ("in_pcbdetach: inp_socket == NULL"));
705 inp->inp_socket->so_pcb = NULL;
706 inp->inp_socket = NULL;
710 in_pcbfree(struct inpcb *inp)
712 struct inpcbinfo *ipi = inp->inp_pcbinfo;
714 KASSERT(inp->inp_socket == NULL, ("in_pcbfree: inp_socket != NULL"));
715 INP_INFO_WLOCK_ASSERT(ipi);
716 INP_LOCK_ASSERT(inp);
718 #if defined(IPSEC) || defined(FAST_IPSEC)
719 ipsec4_delete_pcbpolicy(inp);
721 inp->inp_gencnt = ++ipi->ipi_gencnt;
723 if (inp->inp_options)
724 (void)m_free(inp->inp_options);
725 ip_freemoptions(inp->inp_moptions);
729 mac_destroy_inpcb(inp);
732 uma_zfree(ipi->ipi_zone, inp);
736 * TCP needs to maintain its inpcb structure after the TCP connection has
737 * been torn down. However, it must be disconnected from the inpcb hashes as
738 * it must not prevent binding of future connections to the same port/ip
739 * combination by other inpcbs.
742 in_pcbdrop(struct inpcb *inp)
745 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
746 INP_LOCK_ASSERT(inp);
748 inp->inp_vflag |= INP_DROPPED;
749 if (inp->inp_lport) {
750 struct inpcbport *phd = inp->inp_phd;
752 LIST_REMOVE(inp, inp_hash);
753 LIST_REMOVE(inp, inp_portlist);
754 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
755 LIST_REMOVE(phd, phd_hash);
763 in_sockaddr(in_port_t port, struct in_addr *addr_p)
765 struct sockaddr_in *sin;
767 MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
769 sin->sin_family = AF_INET;
770 sin->sin_len = sizeof(*sin);
771 sin->sin_addr = *addr_p;
772 sin->sin_port = port;
774 return (struct sockaddr *)sin;
778 * The wrapper function will pass down the pcbinfo for this function to lock.
779 * The socket must have a valid
780 * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
781 * except through a kernel programming error, so it is acceptable to panic
782 * (or in this case trap) if the PCB is invalid. (Actually, we don't trap
783 * because there actually /is/ a programming error somewhere... XXX)
786 in_setsockaddr(struct socket *so, struct sockaddr **nam,
787 struct inpcbinfo *pcbinfo)
794 KASSERT(inp != NULL, ("in_setsockaddr: inp == NULL"));
797 port = inp->inp_lport;
798 addr = inp->inp_laddr;
801 *nam = in_sockaddr(port, &addr);
806 * The wrapper function will pass down the pcbinfo for this function to lock.
809 in_setpeeraddr(struct socket *so, struct sockaddr **nam,
810 struct inpcbinfo *pcbinfo)
817 KASSERT(inp != NULL, ("in_setpeeraddr: inp == NULL"));
820 port = inp->inp_fport;
821 addr = inp->inp_faddr;
824 *nam = in_sockaddr(port, &addr);
829 in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno,
830 struct inpcb *(*notify)(struct inpcb *, int))
832 struct inpcb *inp, *ninp;
833 struct inpcbhead *head;
835 INP_INFO_WLOCK(pcbinfo);
836 head = pcbinfo->listhead;
837 for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) {
839 ninp = LIST_NEXT(inp, inp_list);
841 if ((inp->inp_vflag & INP_IPV4) == 0) {
846 if (inp->inp_faddr.s_addr != faddr.s_addr ||
847 inp->inp_socket == NULL) {
851 if ((*notify)(inp, errno))
854 INP_INFO_WUNLOCK(pcbinfo);
858 in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
861 struct ip_moptions *imo;
864 INP_INFO_RLOCK(pcbinfo);
865 LIST_FOREACH(inp, pcbinfo->listhead, inp_list) {
867 imo = inp->inp_moptions;
868 if ((inp->inp_vflag & INP_IPV4) &&
871 * Unselect the outgoing interface if it is being
874 if (imo->imo_multicast_ifp == ifp)
875 imo->imo_multicast_ifp = NULL;
878 * Drop multicast group membership if we joined
879 * through the interface being detached.
881 for (i = 0, gap = 0; i < imo->imo_num_memberships;
883 if (imo->imo_membership[i]->inm_ifp == ifp) {
884 in_delmulti(imo->imo_membership[i]);
887 imo->imo_membership[i - gap] =
888 imo->imo_membership[i];
890 imo->imo_num_memberships -= gap;
894 INP_INFO_RUNLOCK(pcbinfo);
898 * Lookup a PCB based on the local address and port.
900 #define INP_LOOKUP_MAPPED_PCB_COST 3
902 in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr,
903 u_int lport_arg, int wild_okay)
908 int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST;
913 u_short lport = lport_arg;
915 INP_INFO_WLOCK_ASSERT(pcbinfo);
918 struct inpcbhead *head;
920 * Look for an unconnected (wildcard foreign addr) PCB that
921 * matches the local address and port we're looking for.
923 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, pcbinfo->hashmask)];
924 LIST_FOREACH(inp, head, inp_hash) {
926 if ((inp->inp_vflag & INP_IPV4) == 0)
929 if (inp->inp_faddr.s_addr == INADDR_ANY &&
930 inp->inp_laddr.s_addr == laddr.s_addr &&
931 inp->inp_lport == lport) {
943 struct inpcbporthead *porthash;
944 struct inpcbport *phd;
945 struct inpcb *match = NULL;
947 * Best fit PCB lookup.
949 * First see if this local port is in use by looking on the
953 porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport,
954 pcbinfo->porthashmask)];
955 LIST_FOREACH(phd, porthash, phd_hash) {
956 if (phd->phd_port == lport)
961 * Port is in use by one or more PCBs. Look for best
964 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
967 if ((inp->inp_vflag & INP_IPV4) == 0)
970 * We never select the PCB that has
971 * INP_IPV6 flag and is bound to :: if
972 * we have another PCB which is bound
973 * to 0.0.0.0. If a PCB has the
974 * INP_IPV6 flag, then we set its cost
975 * higher than IPv4 only PCBs.
977 * Note that the case only happens
978 * when a socket is bound to ::, under
979 * the condition that the use of the
980 * mapped address is allowed.
982 if ((inp->inp_vflag & INP_IPV6) != 0)
983 wildcard += INP_LOOKUP_MAPPED_PCB_COST;
986 * Clean out old time_wait sockets if they
987 * are clogging up needed local ports.
989 if ((inp->inp_vflag & INP_TIMEWAIT) != 0) {
992 tcp_twrecycleable(tw)) {
999 if (inp->inp_faddr.s_addr != INADDR_ANY)
1001 if (inp->inp_laddr.s_addr != INADDR_ANY) {
1002 if (laddr.s_addr == INADDR_ANY)
1004 else if (inp->inp_laddr.s_addr != laddr.s_addr)
1007 if (laddr.s_addr != INADDR_ANY)
1010 if (wildcard < matchwild) {
1012 matchwild = wildcard;
1013 if (matchwild == 0) {
1022 #undef INP_LOOKUP_MAPPED_PCB_COST
1025 * Lookup PCB in hash list.
1028 in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1029 u_int fport_arg, struct in_addr laddr, u_int lport_arg, int wildcard,
1032 struct inpcbhead *head;
1034 u_short fport = fport_arg, lport = lport_arg;
1036 INP_INFO_RLOCK_ASSERT(pcbinfo);
1039 * First look for an exact match.
1041 head = &pcbinfo->hashbase[INP_PCBHASH(faddr.s_addr, lport, fport, pcbinfo->hashmask)];
1042 LIST_FOREACH(inp, head, inp_hash) {
1044 if ((inp->inp_vflag & INP_IPV4) == 0)
1047 if (inp->inp_faddr.s_addr == faddr.s_addr &&
1048 inp->inp_laddr.s_addr == laddr.s_addr &&
1049 inp->inp_fport == fport &&
1050 inp->inp_lport == lport) {
1058 struct inpcb *local_wild = NULL;
1060 struct inpcb *local_wild_mapped = NULL;
1061 #endif /* defined(INET6) */
1063 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, pcbinfo->hashmask)];
1064 LIST_FOREACH(inp, head, inp_hash) {
1066 if ((inp->inp_vflag & INP_IPV4) == 0)
1069 if (inp->inp_faddr.s_addr == INADDR_ANY &&
1070 inp->inp_lport == lport) {
1071 if (ifp && ifp->if_type == IFT_FAITH &&
1072 (inp->inp_flags & INP_FAITH) == 0)
1074 if (inp->inp_laddr.s_addr == laddr.s_addr)
1076 else if (inp->inp_laddr.s_addr == INADDR_ANY) {
1078 if (INP_CHECK_SOCKAF(inp->inp_socket,
1080 local_wild_mapped = inp;
1082 #endif /* defined(INET6) */
1088 if (local_wild == NULL)
1089 return (local_wild_mapped);
1090 #endif /* defined(INET6) */
1091 return (local_wild);
1101 * Insert PCB onto various hash lists.
1104 in_pcbinshash(struct inpcb *inp)
1106 struct inpcbhead *pcbhash;
1107 struct inpcbporthead *pcbporthash;
1108 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1109 struct inpcbport *phd;
1110 u_int32_t hashkey_faddr;
1112 INP_INFO_WLOCK_ASSERT(pcbinfo);
1113 INP_LOCK_ASSERT(inp);
1116 if (inp->inp_vflag & INP_IPV6)
1117 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1120 hashkey_faddr = inp->inp_faddr.s_addr;
1122 pcbhash = &pcbinfo->hashbase[INP_PCBHASH(hashkey_faddr,
1123 inp->inp_lport, inp->inp_fport, pcbinfo->hashmask)];
1125 pcbporthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(inp->inp_lport,
1126 pcbinfo->porthashmask)];
1129 * Go through port list and look for a head for this lport.
1131 LIST_FOREACH(phd, pcbporthash, phd_hash) {
1132 if (phd->phd_port == inp->inp_lport)
1136 * If none exists, malloc one and tack it on.
1139 MALLOC(phd, struct inpcbport *, sizeof(struct inpcbport), M_PCB, M_NOWAIT);
1141 return (ENOBUFS); /* XXX */
1143 phd->phd_port = inp->inp_lport;
1144 LIST_INIT(&phd->phd_pcblist);
1145 LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
1148 LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
1149 LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
1154 * Move PCB to the proper hash bucket when { faddr, fport } have been
1155 * changed. NOTE: This does not handle the case of the lport changing (the
1156 * hashed port list would have to be updated as well), so the lport must
1157 * not change after in_pcbinshash() has been called.
1160 in_pcbrehash(struct inpcb *inp)
1162 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1163 struct inpcbhead *head;
1164 u_int32_t hashkey_faddr;
1166 INP_INFO_WLOCK_ASSERT(pcbinfo);
1167 INP_LOCK_ASSERT(inp);
1170 if (inp->inp_vflag & INP_IPV6)
1171 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1174 hashkey_faddr = inp->inp_faddr.s_addr;
1176 head = &pcbinfo->hashbase[INP_PCBHASH(hashkey_faddr,
1177 inp->inp_lport, inp->inp_fport, pcbinfo->hashmask)];
1179 LIST_REMOVE(inp, inp_hash);
1180 LIST_INSERT_HEAD(head, inp, inp_hash);
1184 * Remove PCB from various lists.
1187 in_pcbremlists(struct inpcb *inp)
1189 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1191 INP_INFO_WLOCK_ASSERT(pcbinfo);
1192 INP_LOCK_ASSERT(inp);
1194 inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
1195 if (inp->inp_lport) {
1196 struct inpcbport *phd = inp->inp_phd;
1198 LIST_REMOVE(inp, inp_hash);
1199 LIST_REMOVE(inp, inp_portlist);
1200 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1201 LIST_REMOVE(phd, phd_hash);
1205 LIST_REMOVE(inp, inp_list);
1206 pcbinfo->ipi_count--;
1210 * A set label operation has occurred at the socket layer, propagate the
1211 * label change into the in_pcb for the socket.
1214 in_pcbsosetlabel(struct socket *so)
1219 inp = sotoinpcb(so);
1220 KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL"));
1224 mac_inpcb_sosetlabel(so, inp);
1231 * ipport_tick runs once per second, determining if random port allocation
1232 * should be continued. If more than ipport_randomcps ports have been
1233 * allocated in the last second, then we return to sequential port
1234 * allocation. We return to random allocation only once we drop below
1235 * ipport_randomcps for at least ipport_randomtime seconds.
1238 ipport_tick(void *xtp)
1241 if (ipport_tcpallocs <= ipport_tcplastcount + ipport_randomcps) {
1242 if (ipport_stoprandom > 0)
1243 ipport_stoprandom--;
1245 ipport_stoprandom = ipport_randomtime;
1246 ipport_tcplastcount = ipport_tcpallocs;
1247 callout_reset(&ipport_tick_callout, hz, ipport_tick, NULL);