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>
39 #include <sys/malloc.h>
41 #include <sys/domain.h>
42 #include <sys/protosw.h>
43 #include <sys/socket.h>
44 #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 */
83 #include <security/mac/mac_framework.h>
86 * These configure the range of local port addresses assigned to
87 * "unspecified" outgoing connections/packets/whatever.
89 int ipport_lowfirstauto = IPPORT_RESERVED - 1; /* 1023 */
90 int ipport_lowlastauto = IPPORT_RESERVEDSTART; /* 600 */
91 int ipport_firstauto = IPPORT_HIFIRSTAUTO; /* 49152 */
92 int ipport_lastauto = IPPORT_HILASTAUTO; /* 65535 */
93 int ipport_hifirstauto = IPPORT_HIFIRSTAUTO; /* 49152 */
94 int ipport_hilastauto = IPPORT_HILASTAUTO; /* 65535 */
97 * Reserved ports accessible only to root. There are significant
98 * security considerations that must be accounted for when changing these,
99 * but the security benefits can be great. Please be careful.
101 int ipport_reservedhigh = IPPORT_RESERVED - 1; /* 1023 */
102 int ipport_reservedlow = 0;
104 /* Variables dealing with random ephemeral port allocation. */
105 int ipport_randomized = 1; /* user controlled via sysctl */
106 int ipport_randomcps = 10; /* user controlled via sysctl */
107 int ipport_randomtime = 45; /* user controlled via sysctl */
108 int ipport_stoprandom = 0; /* toggled by ipport_tick */
109 int ipport_tcpallocs;
110 int ipport_tcplastcount;
112 #define RANGECHK(var, min, max) \
113 if ((var) < (min)) { (var) = (min); } \
114 else if ((var) > (max)) { (var) = (max); }
117 sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS)
121 error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
123 RANGECHK(ipport_lowfirstauto, 1, IPPORT_RESERVED - 1);
124 RANGECHK(ipport_lowlastauto, 1, IPPORT_RESERVED - 1);
125 RANGECHK(ipport_firstauto, IPPORT_RESERVED, IPPORT_MAX);
126 RANGECHK(ipport_lastauto, IPPORT_RESERVED, IPPORT_MAX);
127 RANGECHK(ipport_hifirstauto, IPPORT_RESERVED, IPPORT_MAX);
128 RANGECHK(ipport_hilastauto, IPPORT_RESERVED, IPPORT_MAX);
135 SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0, "IP Ports");
137 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst, CTLTYPE_INT|CTLFLAG_RW,
138 &ipport_lowfirstauto, 0, &sysctl_net_ipport_check, "I", "");
139 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast, CTLTYPE_INT|CTLFLAG_RW,
140 &ipport_lowlastauto, 0, &sysctl_net_ipport_check, "I", "");
141 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first, CTLTYPE_INT|CTLFLAG_RW,
142 &ipport_firstauto, 0, &sysctl_net_ipport_check, "I", "");
143 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last, CTLTYPE_INT|CTLFLAG_RW,
144 &ipport_lastauto, 0, &sysctl_net_ipport_check, "I", "");
145 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst, CTLTYPE_INT|CTLFLAG_RW,
146 &ipport_hifirstauto, 0, &sysctl_net_ipport_check, "I", "");
147 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast, CTLTYPE_INT|CTLFLAG_RW,
148 &ipport_hilastauto, 0, &sysctl_net_ipport_check, "I", "");
149 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedhigh,
150 CTLFLAG_RW|CTLFLAG_SECURE, &ipport_reservedhigh, 0, "");
151 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, reservedlow,
152 CTLFLAG_RW|CTLFLAG_SECURE, &ipport_reservedlow, 0, "");
153 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomized, CTLFLAG_RW,
154 &ipport_randomized, 0, "Enable random port allocation");
155 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomcps, CTLFLAG_RW,
156 &ipport_randomcps, 0, "Maximum number of random port "
157 "allocations before switching to a sequental one");
158 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomtime, CTLFLAG_RW,
159 &ipport_randomtime, 0, "Minimum time to keep sequental port "
160 "allocation before switching to a random one");
163 * in_pcb.c: manage the Protocol Control Blocks.
165 * NOTE: It is assumed that most of these functions will be called with
166 * the pcbinfo lock held, and often, the inpcb lock held, as these utility
167 * functions often modify hash chains or addresses in pcbs.
171 * Allocate a PCB and associate it with the socket.
172 * On success return with the PCB locked.
175 in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo)
180 INP_INFO_WLOCK_ASSERT(pcbinfo);
182 inp = uma_zalloc(pcbinfo->ipi_zone, M_NOWAIT);
185 bzero(inp, inp_zero_size);
186 inp->inp_pcbinfo = pcbinfo;
187 inp->inp_socket = so;
189 error = mac_init_inpcb(inp, M_NOWAIT);
193 mac_create_inpcb_from_socket(so, inp);
196 #if defined(IPSEC) || defined(FAST_IPSEC)
198 error = ipsec_init_policy(so, &inp->inp_sp);
200 error = ipsec_init_pcbpolicy(so, &inp->inp_sp);
206 if (INP_SOCKAF(so) == AF_INET6) {
207 inp->inp_vflag |= INP_IPV6PROTO;
209 inp->inp_flags |= IN6P_IPV6_V6ONLY;
212 LIST_INSERT_HEAD(pcbinfo->listhead, inp, inp_list);
213 pcbinfo->ipi_count++;
214 so->so_pcb = (caddr_t)inp;
216 if (ip6_auto_flowlabel)
217 inp->inp_flags |= IN6P_AUTOFLOWLABEL;
220 inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
222 #if defined(IPSEC) || defined(FAST_IPSEC) || defined(MAC)
225 uma_zfree(pcbinfo->ipi_zone, inp);
231 in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
235 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
236 INP_LOCK_ASSERT(inp);
238 if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY)
240 anonport = inp->inp_lport == 0 && (nam == NULL ||
241 ((struct sockaddr_in *)nam)->sin_port == 0);
242 error = in_pcbbind_setup(inp, nam, &inp->inp_laddr.s_addr,
243 &inp->inp_lport, cred);
246 if (in_pcbinshash(inp) != 0) {
247 inp->inp_laddr.s_addr = INADDR_ANY;
252 inp->inp_flags |= INP_ANONPORT;
257 * Set up a bind operation on a PCB, performing port allocation
258 * as required, but do not actually modify the PCB. Callers can
259 * either complete the bind by setting inp_laddr/inp_lport and
260 * calling in_pcbinshash(), or they can just use the resulting
261 * port and address to authorise the sending of a once-off packet.
263 * On error, the values of *laddrp and *lportp are not changed.
266 in_pcbbind_setup(struct inpcb *inp, struct sockaddr *nam, in_addr_t *laddrp,
267 u_short *lportp, struct ucred *cred)
269 struct socket *so = inp->inp_socket;
270 unsigned short *lastport;
271 struct sockaddr_in *sin;
272 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
273 struct in_addr laddr;
275 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
276 int error, prison = 0;
279 INP_INFO_WLOCK_ASSERT(pcbinfo);
280 INP_LOCK_ASSERT(inp);
282 if (TAILQ_EMPTY(&in_ifaddrhead)) /* XXX broken! */
283 return (EADDRNOTAVAIL);
284 laddr.s_addr = *laddrp;
285 if (nam != NULL && laddr.s_addr != INADDR_ANY)
287 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
288 wild = INPLOOKUP_WILDCARD;
290 sin = (struct sockaddr_in *)nam;
291 if (nam->sa_len != sizeof (*sin))
295 * We should check the family, but old programs
296 * incorrectly fail to initialize it.
298 if (sin->sin_family != AF_INET)
299 return (EAFNOSUPPORT);
301 if (sin->sin_addr.s_addr != INADDR_ANY)
302 if (prison_ip(cred, 0, &sin->sin_addr.s_addr))
304 if (sin->sin_port != *lportp) {
305 /* Don't allow the port to change. */
308 lport = sin->sin_port;
310 /* NB: lport is left as 0 if the port isn't being changed. */
311 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
313 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
314 * allow complete duplication of binding if
315 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
316 * and a multicast address is bound on both
317 * new and duplicated sockets.
319 if (so->so_options & SO_REUSEADDR)
320 reuseport = SO_REUSEADDR|SO_REUSEPORT;
321 } else if (sin->sin_addr.s_addr != INADDR_ANY) {
322 sin->sin_port = 0; /* yech... */
323 bzero(&sin->sin_zero, sizeof(sin->sin_zero));
324 if (ifa_ifwithaddr((struct sockaddr *)sin) == 0)
325 return (EADDRNOTAVAIL);
327 laddr = sin->sin_addr;
333 if (ntohs(lport) <= ipport_reservedhigh &&
334 ntohs(lport) >= ipport_reservedlow &&
335 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT,
340 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
341 suser_cred(so->so_cred, SUSER_ALLOWJAIL) != 0) {
342 t = in_pcblookup_local(inp->inp_pcbinfo,
343 sin->sin_addr, lport,
344 prison ? 0 : INPLOOKUP_WILDCARD);
347 * This entire block sorely needs a rewrite.
350 ((t->inp_vflag & INP_TIMEWAIT) == 0) &&
351 (so->so_type != SOCK_STREAM ||
352 ntohl(t->inp_faddr.s_addr) == INADDR_ANY) &&
353 (ntohl(sin->sin_addr.s_addr) != INADDR_ANY ||
354 ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
355 (t->inp_socket->so_options &
356 SO_REUSEPORT) == 0) &&
357 (so->so_cred->cr_uid !=
358 t->inp_socket->so_cred->cr_uid))
361 if (prison && prison_ip(cred, 0, &sin->sin_addr.s_addr))
362 return (EADDRNOTAVAIL);
363 t = in_pcblookup_local(pcbinfo, sin->sin_addr,
364 lport, prison ? 0 : wild);
365 if (t && (t->inp_vflag & INP_TIMEWAIT)) {
367 * XXXRW: If an incpb has had its timewait
368 * state recycled, we treat the address as
369 * being in use (for now). This is better
370 * than a panic, but not desirable.
374 (reuseport & tw->tw_so_options) == 0)
377 (reuseport & t->inp_socket->so_options) == 0) {
379 if (ntohl(sin->sin_addr.s_addr) !=
381 ntohl(t->inp_laddr.s_addr) !=
384 INP_SOCKAF(t->inp_socket))
396 if (laddr.s_addr != INADDR_ANY)
397 if (prison_ip(cred, 0, &laddr.s_addr))
400 if (inp->inp_flags & INP_HIGHPORT) {
401 first = ipport_hifirstauto; /* sysctl */
402 last = ipport_hilastauto;
403 lastport = &pcbinfo->lasthi;
404 } else if (inp->inp_flags & INP_LOWPORT) {
405 error = priv_check_cred(cred,
406 PRIV_NETINET_RESERVEDPORT, SUSER_ALLOWJAIL);
409 first = ipport_lowfirstauto; /* 1023 */
410 last = ipport_lowlastauto; /* 600 */
411 lastport = &pcbinfo->lastlow;
413 first = ipport_firstauto; /* sysctl */
414 last = ipport_lastauto;
415 lastport = &pcbinfo->lastport;
418 * For UDP, use random port allocation as long as the user
419 * allows it. For TCP (and as of yet unknown) connections,
420 * use random port allocation only if the user allows it AND
421 * ipport_tick() allows it.
423 if (ipport_randomized &&
424 (!ipport_stoprandom || pcbinfo == &udbinfo))
429 * It makes no sense to do random port allocation if
430 * we have the only port available.
434 /* Make sure to not include UDP packets in the count. */
435 if (pcbinfo != &udbinfo)
438 * Simple check to ensure all ports are not used up causing
441 * We split the two cases (up and down) so that the direction
442 * is not being tested on each round of the loop.
450 (arc4random() % (first - last));
451 count = first - last;
454 if (count-- < 0) /* completely used? */
455 return (EADDRNOTAVAIL);
457 if (*lastport > first || *lastport < last)
459 lport = htons(*lastport);
460 } while (in_pcblookup_local(pcbinfo, laddr, lport,
468 (arc4random() % (last - first));
469 count = last - first;
472 if (count-- < 0) /* completely used? */
473 return (EADDRNOTAVAIL);
475 if (*lastport < first || *lastport > last)
477 lport = htons(*lastport);
478 } while (in_pcblookup_local(pcbinfo, laddr, lport,
482 if (prison_ip(cred, 0, &laddr.s_addr))
484 *laddrp = laddr.s_addr;
490 * Connect from a socket to a specified address.
491 * Both address and port must be specified in argument sin.
492 * If don't have a local address for this socket yet,
496 in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
498 u_short lport, fport;
499 in_addr_t laddr, faddr;
502 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
503 INP_LOCK_ASSERT(inp);
505 lport = inp->inp_lport;
506 laddr = inp->inp_laddr.s_addr;
507 anonport = (lport == 0);
508 error = in_pcbconnect_setup(inp, nam, &laddr, &lport, &faddr, &fport,
513 /* Do the initial binding of the local address if required. */
514 if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) {
515 inp->inp_lport = lport;
516 inp->inp_laddr.s_addr = laddr;
517 if (in_pcbinshash(inp) != 0) {
518 inp->inp_laddr.s_addr = INADDR_ANY;
524 /* Commit the remaining changes. */
525 inp->inp_lport = lport;
526 inp->inp_laddr.s_addr = laddr;
527 inp->inp_faddr.s_addr = faddr;
528 inp->inp_fport = fport;
531 if (inp->inp_socket->so_type == SOCK_STREAM)
532 ipsec_pcbconn(inp->inp_sp);
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;
694 ipsec_pcbdisconn(inp->inp_sp);
699 * In the old world order, in_pcbdetach() served two functions: to detach the
700 * pcb from the socket/potentially free the socket, and to free the pcb
701 * itself. In the new world order, the protocol code is responsible for
702 * managing the relationship with the socket, and this code simply frees the
706 in_pcbdetach(struct inpcb *inp)
709 KASSERT(inp->inp_socket != NULL, ("in_pcbdetach: inp_socket == NULL"));
710 inp->inp_socket->so_pcb = NULL;
711 inp->inp_socket = NULL;
715 in_pcbfree(struct inpcb *inp)
717 struct inpcbinfo *ipi = inp->inp_pcbinfo;
719 KASSERT(inp->inp_socket == NULL, ("in_pcbfree: inp_socket != NULL"));
720 INP_INFO_WLOCK_ASSERT(ipi);
721 INP_LOCK_ASSERT(inp);
723 #if defined(IPSEC) || defined(FAST_IPSEC)
724 ipsec4_delete_pcbpolicy(inp);
726 inp->inp_gencnt = ++ipi->ipi_gencnt;
728 if (inp->inp_options)
729 (void)m_free(inp->inp_options);
730 ip_freemoptions(inp->inp_moptions);
734 mac_destroy_inpcb(inp);
737 uma_zfree(ipi->ipi_zone, inp);
741 * TCP needs to maintain its inpcb structure after the TCP connection has
742 * been torn down. However, it must be disconnected from the inpcb hashes as
743 * it must not prevent binding of future connections to the same port/ip
744 * combination by other inpcbs.
747 in_pcbdrop(struct inpcb *inp)
750 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
751 INP_LOCK_ASSERT(inp);
753 inp->inp_vflag |= INP_DROPPED;
754 if (inp->inp_lport) {
755 struct inpcbport *phd = inp->inp_phd;
757 LIST_REMOVE(inp, inp_hash);
758 LIST_REMOVE(inp, inp_portlist);
759 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
760 LIST_REMOVE(phd, phd_hash);
768 in_sockaddr(in_port_t port, struct in_addr *addr_p)
770 struct sockaddr_in *sin;
772 MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
774 sin->sin_family = AF_INET;
775 sin->sin_len = sizeof(*sin);
776 sin->sin_addr = *addr_p;
777 sin->sin_port = port;
779 return (struct sockaddr *)sin;
783 * The wrapper function will pass down the pcbinfo for this function to lock.
784 * The socket must have a valid
785 * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
786 * except through a kernel programming error, so it is acceptable to panic
787 * (or in this case trap) if the PCB is invalid. (Actually, we don't trap
788 * because there actually /is/ a programming error somewhere... XXX)
791 in_setsockaddr(struct socket *so, struct sockaddr **nam,
792 struct inpcbinfo *pcbinfo)
799 KASSERT(inp != NULL, ("in_setsockaddr: inp == NULL"));
802 port = inp->inp_lport;
803 addr = inp->inp_laddr;
806 *nam = in_sockaddr(port, &addr);
811 * The wrapper function will pass down the pcbinfo for this function to lock.
814 in_setpeeraddr(struct socket *so, struct sockaddr **nam,
815 struct inpcbinfo *pcbinfo)
822 KASSERT(inp != NULL, ("in_setpeeraddr: inp == NULL"));
825 port = inp->inp_fport;
826 addr = inp->inp_faddr;
829 *nam = in_sockaddr(port, &addr);
834 in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno,
835 struct inpcb *(*notify)(struct inpcb *, int))
837 struct inpcb *inp, *ninp;
838 struct inpcbhead *head;
840 INP_INFO_WLOCK(pcbinfo);
841 head = pcbinfo->listhead;
842 for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) {
844 ninp = LIST_NEXT(inp, inp_list);
846 if ((inp->inp_vflag & INP_IPV4) == 0) {
851 if (inp->inp_faddr.s_addr != faddr.s_addr ||
852 inp->inp_socket == NULL) {
856 if ((*notify)(inp, errno))
859 INP_INFO_WUNLOCK(pcbinfo);
863 in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
866 struct ip_moptions *imo;
869 INP_INFO_RLOCK(pcbinfo);
870 LIST_FOREACH(inp, pcbinfo->listhead, inp_list) {
872 imo = inp->inp_moptions;
873 if ((inp->inp_vflag & INP_IPV4) &&
876 * Unselect the outgoing interface if it is being
879 if (imo->imo_multicast_ifp == ifp)
880 imo->imo_multicast_ifp = NULL;
883 * Drop multicast group membership if we joined
884 * through the interface being detached.
886 for (i = 0, gap = 0; i < imo->imo_num_memberships;
888 if (imo->imo_membership[i]->inm_ifp == ifp) {
889 in_delmulti(imo->imo_membership[i]);
892 imo->imo_membership[i - gap] =
893 imo->imo_membership[i];
895 imo->imo_num_memberships -= gap;
899 INP_INFO_RUNLOCK(pcbinfo);
903 * Lookup a PCB based on the local address and port.
905 #define INP_LOOKUP_MAPPED_PCB_COST 3
907 in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr,
908 u_int lport_arg, int wild_okay)
912 int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST;
917 u_short lport = lport_arg;
919 INP_INFO_WLOCK_ASSERT(pcbinfo);
922 struct inpcbhead *head;
924 * Look for an unconnected (wildcard foreign addr) PCB that
925 * matches the local address and port we're looking for.
927 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, pcbinfo->hashmask)];
928 LIST_FOREACH(inp, head, inp_hash) {
930 if ((inp->inp_vflag & INP_IPV4) == 0)
933 if (inp->inp_faddr.s_addr == INADDR_ANY &&
934 inp->inp_laddr.s_addr == laddr.s_addr &&
935 inp->inp_lport == lport) {
947 struct inpcbporthead *porthash;
948 struct inpcbport *phd;
949 struct inpcb *match = NULL;
951 * Best fit PCB lookup.
953 * First see if this local port is in use by looking on the
956 porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport,
957 pcbinfo->porthashmask)];
958 LIST_FOREACH(phd, porthash, phd_hash) {
959 if (phd->phd_port == lport)
964 * Port is in use by one or more PCBs. Look for best
967 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
970 if ((inp->inp_vflag & INP_IPV4) == 0)
973 * We never select the PCB that has
974 * INP_IPV6 flag and is bound to :: if
975 * we have another PCB which is bound
976 * to 0.0.0.0. If a PCB has the
977 * INP_IPV6 flag, then we set its cost
978 * higher than IPv4 only PCBs.
980 * Note that the case only happens
981 * when a socket is bound to ::, under
982 * the condition that the use of the
983 * mapped address is allowed.
985 if ((inp->inp_vflag & INP_IPV6) != 0)
986 wildcard += INP_LOOKUP_MAPPED_PCB_COST;
988 if (inp->inp_faddr.s_addr != INADDR_ANY)
990 if (inp->inp_laddr.s_addr != INADDR_ANY) {
991 if (laddr.s_addr == INADDR_ANY)
993 else if (inp->inp_laddr.s_addr != laddr.s_addr)
996 if (laddr.s_addr != INADDR_ANY)
999 if (wildcard < matchwild) {
1001 matchwild = wildcard;
1002 if (matchwild == 0) {
1011 #undef INP_LOOKUP_MAPPED_PCB_COST
1014 * Lookup PCB in hash list.
1017 in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1018 u_int fport_arg, struct in_addr laddr, u_int lport_arg, int wildcard,
1021 struct inpcbhead *head;
1023 u_short fport = fport_arg, lport = lport_arg;
1025 INP_INFO_RLOCK_ASSERT(pcbinfo);
1028 * First look for an exact match.
1030 head = &pcbinfo->hashbase[INP_PCBHASH(faddr.s_addr, lport, fport,
1031 pcbinfo->hashmask)];
1032 LIST_FOREACH(inp, head, inp_hash) {
1034 if ((inp->inp_vflag & INP_IPV4) == 0)
1037 if (inp->inp_faddr.s_addr == faddr.s_addr &&
1038 inp->inp_laddr.s_addr == laddr.s_addr &&
1039 inp->inp_fport == fport &&
1040 inp->inp_lport == lport)
1045 * Then look for a wildcard match, if requested.
1048 struct inpcb *local_wild = NULL;
1050 struct inpcb *local_wild_mapped = NULL;
1053 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0,
1054 pcbinfo->hashmask)];
1055 LIST_FOREACH(inp, head, inp_hash) {
1057 if ((inp->inp_vflag & INP_IPV4) == 0)
1060 if (inp->inp_faddr.s_addr == INADDR_ANY &&
1061 inp->inp_lport == lport) {
1062 if (ifp && ifp->if_type == IFT_FAITH &&
1063 (inp->inp_flags & INP_FAITH) == 0)
1065 if (inp->inp_laddr.s_addr == laddr.s_addr)
1067 else if (inp->inp_laddr.s_addr == INADDR_ANY) {
1069 if (INP_CHECK_SOCKAF(inp->inp_socket,
1071 local_wild_mapped = inp;
1079 if (local_wild == NULL)
1080 return (local_wild_mapped);
1082 return (local_wild);
1088 * Insert PCB onto various hash lists.
1091 in_pcbinshash(struct inpcb *inp)
1093 struct inpcbhead *pcbhash;
1094 struct inpcbporthead *pcbporthash;
1095 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1096 struct inpcbport *phd;
1097 u_int32_t hashkey_faddr;
1099 INP_INFO_WLOCK_ASSERT(pcbinfo);
1100 INP_LOCK_ASSERT(inp);
1103 if (inp->inp_vflag & INP_IPV6)
1104 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1107 hashkey_faddr = inp->inp_faddr.s_addr;
1109 pcbhash = &pcbinfo->hashbase[INP_PCBHASH(hashkey_faddr,
1110 inp->inp_lport, inp->inp_fport, pcbinfo->hashmask)];
1112 pcbporthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(inp->inp_lport,
1113 pcbinfo->porthashmask)];
1116 * Go through port list and look for a head for this lport.
1118 LIST_FOREACH(phd, pcbporthash, phd_hash) {
1119 if (phd->phd_port == inp->inp_lport)
1123 * If none exists, malloc one and tack it on.
1126 MALLOC(phd, struct inpcbport *, sizeof(struct inpcbport), M_PCB, M_NOWAIT);
1128 return (ENOBUFS); /* XXX */
1130 phd->phd_port = inp->inp_lport;
1131 LIST_INIT(&phd->phd_pcblist);
1132 LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
1135 LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
1136 LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
1141 * Move PCB to the proper hash bucket when { faddr, fport } have been
1142 * changed. NOTE: This does not handle the case of the lport changing (the
1143 * hashed port list would have to be updated as well), so the lport must
1144 * not change after in_pcbinshash() has been called.
1147 in_pcbrehash(struct inpcb *inp)
1149 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1150 struct inpcbhead *head;
1151 u_int32_t hashkey_faddr;
1153 INP_INFO_WLOCK_ASSERT(pcbinfo);
1154 INP_LOCK_ASSERT(inp);
1157 if (inp->inp_vflag & INP_IPV6)
1158 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1161 hashkey_faddr = inp->inp_faddr.s_addr;
1163 head = &pcbinfo->hashbase[INP_PCBHASH(hashkey_faddr,
1164 inp->inp_lport, inp->inp_fport, pcbinfo->hashmask)];
1166 LIST_REMOVE(inp, inp_hash);
1167 LIST_INSERT_HEAD(head, inp, inp_hash);
1171 * Remove PCB from various lists.
1174 in_pcbremlists(struct inpcb *inp)
1176 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1178 INP_INFO_WLOCK_ASSERT(pcbinfo);
1179 INP_LOCK_ASSERT(inp);
1181 inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
1182 if (inp->inp_lport) {
1183 struct inpcbport *phd = inp->inp_phd;
1185 LIST_REMOVE(inp, inp_hash);
1186 LIST_REMOVE(inp, inp_portlist);
1187 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1188 LIST_REMOVE(phd, phd_hash);
1192 LIST_REMOVE(inp, inp_list);
1193 pcbinfo->ipi_count--;
1197 * A set label operation has occurred at the socket layer, propagate the
1198 * label change into the in_pcb for the socket.
1201 in_pcbsosetlabel(struct socket *so)
1206 inp = sotoinpcb(so);
1207 KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL"));
1211 mac_inpcb_sosetlabel(so, inp);
1218 * ipport_tick runs once per second, determining if random port allocation
1219 * should be continued. If more than ipport_randomcps ports have been
1220 * allocated in the last second, then we return to sequential port
1221 * allocation. We return to random allocation only once we drop below
1222 * ipport_randomcps for at least ipport_randomtime seconds.
1225 ipport_tick(void *xtp)
1228 if (ipport_tcpallocs <= ipport_tcplastcount + ipport_randomcps) {
1229 if (ipport_stoprandom > 0)
1230 ipport_stoprandom--;
1232 ipport_stoprandom = ipport_randomtime;
1233 ipport_tcplastcount = ipport_tcpallocs;
1234 callout_reset(&ipport_tick_callout, hz, ipport_tick, NULL);