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.
172 in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo, const char *type)
177 INP_INFO_WLOCK_ASSERT(pcbinfo);
179 inp = uma_zalloc(pcbinfo->ipi_zone, M_NOWAIT | M_ZERO);
182 inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
183 inp->inp_pcbinfo = pcbinfo;
184 inp->inp_socket = so;
186 error = mac_init_inpcb(inp, M_NOWAIT);
190 mac_create_inpcb_from_socket(so, inp);
193 #if defined(IPSEC) || defined(FAST_IPSEC)
195 error = ipsec_init_policy(so, &inp->inp_sp);
197 error = ipsec_init_pcbpolicy(so, &inp->inp_sp);
203 if (INP_SOCKAF(so) == AF_INET6) {
204 inp->inp_vflag |= INP_IPV6PROTO;
206 inp->inp_flags |= IN6P_IPV6_V6ONLY;
209 LIST_INSERT_HEAD(pcbinfo->listhead, inp, inp_list);
210 pcbinfo->ipi_count++;
211 so->so_pcb = (caddr_t)inp;
212 INP_LOCK_INIT(inp, "inp", type);
214 if (ip6_auto_flowlabel)
215 inp->inp_flags |= IN6P_AUTOFLOWLABEL;
217 #if defined(IPSEC) || defined(FAST_IPSEC) || defined(MAC)
220 uma_zfree(pcbinfo->ipi_zone, inp);
226 in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
230 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
231 INP_LOCK_ASSERT(inp);
233 if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY)
235 anonport = inp->inp_lport == 0 && (nam == NULL ||
236 ((struct sockaddr_in *)nam)->sin_port == 0);
237 error = in_pcbbind_setup(inp, nam, &inp->inp_laddr.s_addr,
238 &inp->inp_lport, cred);
241 if (in_pcbinshash(inp) != 0) {
242 inp->inp_laddr.s_addr = INADDR_ANY;
247 inp->inp_flags |= INP_ANONPORT;
252 * Set up a bind operation on a PCB, performing port allocation
253 * as required, but do not actually modify the PCB. Callers can
254 * either complete the bind by setting inp_laddr/inp_lport and
255 * calling in_pcbinshash(), or they can just use the resulting
256 * port and address to authorise the sending of a once-off packet.
258 * On error, the values of *laddrp and *lportp are not changed.
261 in_pcbbind_setup(struct inpcb *inp, struct sockaddr *nam, in_addr_t *laddrp,
262 u_short *lportp, struct ucred *cred)
264 struct socket *so = inp->inp_socket;
265 unsigned short *lastport;
266 struct sockaddr_in *sin;
267 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
268 struct in_addr laddr;
270 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
271 int error, prison = 0;
274 INP_INFO_WLOCK_ASSERT(pcbinfo);
275 INP_LOCK_ASSERT(inp);
277 if (TAILQ_EMPTY(&in_ifaddrhead)) /* XXX broken! */
278 return (EADDRNOTAVAIL);
279 laddr.s_addr = *laddrp;
280 if (nam != NULL && laddr.s_addr != INADDR_ANY)
282 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
285 sin = (struct sockaddr_in *)nam;
286 if (nam->sa_len != sizeof (*sin))
290 * We should check the family, but old programs
291 * incorrectly fail to initialize it.
293 if (sin->sin_family != AF_INET)
294 return (EAFNOSUPPORT);
296 if (sin->sin_addr.s_addr != INADDR_ANY)
297 if (prison_ip(cred, 0, &sin->sin_addr.s_addr))
299 if (sin->sin_port != *lportp) {
300 /* Don't allow the port to change. */
303 lport = sin->sin_port;
305 /* NB: lport is left as 0 if the port isn't being changed. */
306 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
308 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
309 * allow complete duplication of binding if
310 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
311 * and a multicast address is bound on both
312 * new and duplicated sockets.
314 if (so->so_options & SO_REUSEADDR)
315 reuseport = SO_REUSEADDR|SO_REUSEPORT;
316 } else if (sin->sin_addr.s_addr != INADDR_ANY) {
317 sin->sin_port = 0; /* yech... */
318 bzero(&sin->sin_zero, sizeof(sin->sin_zero));
319 if (ifa_ifwithaddr((struct sockaddr *)sin) == 0)
320 return (EADDRNOTAVAIL);
322 laddr = sin->sin_addr;
328 if (ntohs(lport) <= ipport_reservedhigh &&
329 ntohs(lport) >= ipport_reservedlow &&
330 suser_cred(cred, SUSER_ALLOWJAIL))
334 if (so->so_cred->cr_uid != 0 &&
335 !IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
336 t = in_pcblookup_local(inp->inp_pcbinfo,
337 sin->sin_addr, lport,
338 prison ? 0 : INPLOOKUP_WILDCARD);
341 * This entire block sorely needs a rewrite.
344 ((t->inp_vflag & INP_TIMEWAIT) == 0) &&
345 (so->so_type != SOCK_STREAM ||
346 ntohl(t->inp_faddr.s_addr) == INADDR_ANY) &&
347 (ntohl(sin->sin_addr.s_addr) != INADDR_ANY ||
348 ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
349 (t->inp_socket->so_options &
350 SO_REUSEPORT) == 0) &&
351 (so->so_cred->cr_uid !=
352 t->inp_socket->so_cred->cr_uid))
355 if (prison && prison_ip(cred, 0, &sin->sin_addr.s_addr))
356 return (EADDRNOTAVAIL);
357 t = in_pcblookup_local(pcbinfo, sin->sin_addr,
358 lport, prison ? 0 : wild);
359 if (t && (t->inp_vflag & INP_TIMEWAIT)) {
361 * XXXRW: If an incpb has had its timewait
362 * state recycled, we treat the address as
363 * being in use (for now). This is better
364 * than a panic, but not desirable.
368 (reuseport & tw->tw_so_options) == 0)
371 (reuseport & t->inp_socket->so_options) == 0) {
373 if (ntohl(sin->sin_addr.s_addr) !=
375 ntohl(t->inp_laddr.s_addr) !=
378 INP_SOCKAF(t->inp_socket))
379 #endif /* defined(INET6) */
390 if (laddr.s_addr != INADDR_ANY)
391 if (prison_ip(cred, 0, &laddr.s_addr))
394 if (inp->inp_flags & INP_HIGHPORT) {
395 first = ipport_hifirstauto; /* sysctl */
396 last = ipport_hilastauto;
397 lastport = &pcbinfo->lasthi;
398 } else if (inp->inp_flags & INP_LOWPORT) {
399 if ((error = suser_cred(cred, SUSER_ALLOWJAIL)) != 0)
401 first = ipport_lowfirstauto; /* 1023 */
402 last = ipport_lowlastauto; /* 600 */
403 lastport = &pcbinfo->lastlow;
405 first = ipport_firstauto; /* sysctl */
406 last = ipport_lastauto;
407 lastport = &pcbinfo->lastport;
410 * For UDP, use random port allocation as long as the user
411 * allows it. For TCP (and as of yet unknown) connections,
412 * use random port allocation only if the user allows it AND
413 * ipport_tick() allows it.
415 if (ipport_randomized &&
416 (!ipport_stoprandom || pcbinfo == &udbinfo))
421 * It makes no sense to do random port allocation if
422 * we have the only port available.
426 /* Make sure to not include UDP packets in the count. */
427 if (pcbinfo != &udbinfo)
430 * Simple check to ensure all ports are not used up causing
433 * We split the two cases (up and down) so that the direction
434 * is not being tested on each round of the loop.
442 (arc4random() % (first - last));
443 count = first - last;
446 if (count-- < 0) /* completely used? */
447 return (EADDRNOTAVAIL);
449 if (*lastport > first || *lastport < last)
451 lport = htons(*lastport);
452 } while (in_pcblookup_local(pcbinfo, laddr, lport,
460 (arc4random() % (last - first));
461 count = last - first;
464 if (count-- < 0) /* completely used? */
465 return (EADDRNOTAVAIL);
467 if (*lastport < first || *lastport > last)
469 lport = htons(*lastport);
470 } while (in_pcblookup_local(pcbinfo, laddr, lport,
474 if (prison_ip(cred, 0, &laddr.s_addr))
476 *laddrp = laddr.s_addr;
482 * Connect from a socket to a specified address.
483 * Both address and port must be specified in argument sin.
484 * If don't have a local address for this socket yet,
488 in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
490 u_short lport, fport;
491 in_addr_t laddr, faddr;
494 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
495 INP_LOCK_ASSERT(inp);
497 lport = inp->inp_lport;
498 laddr = inp->inp_laddr.s_addr;
499 anonport = (lport == 0);
500 error = in_pcbconnect_setup(inp, nam, &laddr, &lport, &faddr, &fport,
505 /* Do the initial binding of the local address if required. */
506 if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) {
507 inp->inp_lport = lport;
508 inp->inp_laddr.s_addr = laddr;
509 if (in_pcbinshash(inp) != 0) {
510 inp->inp_laddr.s_addr = INADDR_ANY;
516 /* Commit the remaining changes. */
517 inp->inp_lport = lport;
518 inp->inp_laddr.s_addr = laddr;
519 inp->inp_faddr.s_addr = faddr;
520 inp->inp_fport = fport;
523 if (inp->inp_socket->so_type == SOCK_STREAM)
524 ipsec_pcbconn(inp->inp_sp);
527 inp->inp_flags |= INP_ANONPORT;
532 * Set up for a connect from a socket to the specified address.
533 * On entry, *laddrp and *lportp should contain the current local
534 * address and port for the PCB; these are updated to the values
535 * that should be placed in inp_laddr and inp_lport to complete
538 * On success, *faddrp and *fportp will be set to the remote address
539 * and port. These are not updated in the error case.
541 * If the operation fails because the connection already exists,
542 * *oinpp will be set to the PCB of that connection so that the
543 * caller can decide to override it. In all other cases, *oinpp
547 in_pcbconnect_setup(struct inpcb *inp, struct sockaddr *nam,
548 in_addr_t *laddrp, u_short *lportp, in_addr_t *faddrp, u_short *fportp,
549 struct inpcb **oinpp, struct ucred *cred)
551 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
552 struct in_ifaddr *ia;
553 struct sockaddr_in sa;
554 struct ucred *socred;
556 struct in_addr laddr, faddr;
557 u_short lport, fport;
560 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
561 INP_LOCK_ASSERT(inp);
565 if (nam->sa_len != sizeof (*sin))
567 if (sin->sin_family != AF_INET)
568 return (EAFNOSUPPORT);
569 if (sin->sin_port == 0)
570 return (EADDRNOTAVAIL);
571 laddr.s_addr = *laddrp;
573 faddr = sin->sin_addr;
574 fport = sin->sin_port;
575 socred = inp->inp_socket->so_cred;
576 if (laddr.s_addr == INADDR_ANY && jailed(socred)) {
577 bzero(&sa, sizeof(sa));
578 sa.sin_addr.s_addr = htonl(prison_getip(socred));
579 sa.sin_len = sizeof(sa);
580 sa.sin_family = AF_INET;
581 error = in_pcbbind_setup(inp, (struct sockaddr *)&sa,
582 &laddr.s_addr, &lport, cred);
586 if (!TAILQ_EMPTY(&in_ifaddrhead)) {
588 * If the destination address is INADDR_ANY,
589 * use the primary local address.
590 * If the supplied address is INADDR_BROADCAST,
591 * and the primary interface supports broadcast,
592 * choose the broadcast address for that interface.
594 if (faddr.s_addr == INADDR_ANY)
595 faddr = IA_SIN(TAILQ_FIRST(&in_ifaddrhead))->sin_addr;
596 else if (faddr.s_addr == (u_long)INADDR_BROADCAST &&
597 (TAILQ_FIRST(&in_ifaddrhead)->ia_ifp->if_flags &
599 faddr = satosin(&TAILQ_FIRST(
600 &in_ifaddrhead)->ia_broadaddr)->sin_addr;
602 if (laddr.s_addr == INADDR_ANY) {
603 ia = (struct in_ifaddr *)0;
605 * If route is known our src addr is taken from the i/f,
608 * Find out route to destination
610 if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0)
611 ia = ip_rtaddr(faddr);
613 * If we found a route, use the address corresponding to
614 * the outgoing interface.
616 * Otherwise assume faddr is reachable on a directly connected
617 * network and try to find a corresponding interface to take
618 * the source address from.
621 bzero(&sa, sizeof(sa));
623 sa.sin_len = sizeof(sa);
624 sa.sin_family = AF_INET;
626 ia = ifatoia(ifa_ifwithdstaddr(sintosa(&sa)));
628 ia = ifatoia(ifa_ifwithnet(sintosa(&sa)));
630 return (ENETUNREACH);
633 * If the destination address is multicast and an outgoing
634 * interface has been set as a multicast option, use the
635 * address of that interface as our source address.
637 if (IN_MULTICAST(ntohl(faddr.s_addr)) &&
638 inp->inp_moptions != NULL) {
639 struct ip_moptions *imo;
642 imo = inp->inp_moptions;
643 if (imo->imo_multicast_ifp != NULL) {
644 ifp = imo->imo_multicast_ifp;
645 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
646 if (ia->ia_ifp == ifp)
649 return (EADDRNOTAVAIL);
652 laddr = ia->ia_addr.sin_addr;
655 oinp = in_pcblookup_hash(inp->inp_pcbinfo, faddr, fport, laddr, lport,
663 error = in_pcbbind_setup(inp, NULL, &laddr.s_addr, &lport,
668 *laddrp = laddr.s_addr;
670 *faddrp = faddr.s_addr;
676 in_pcbdisconnect(struct inpcb *inp)
679 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
680 INP_LOCK_ASSERT(inp);
682 inp->inp_faddr.s_addr = INADDR_ANY;
686 ipsec_pcbdisconn(inp->inp_sp);
691 * In the old world order, in_pcbdetach() served two functions: to detach the
692 * pcb from the socket/potentially free the socket, and to free the pcb
693 * itself. In the new world order, the protocol code is responsible for
694 * managing the relationship with the socket, and this code simply frees the
698 in_pcbdetach(struct inpcb *inp)
701 KASSERT(inp->inp_socket != NULL, ("in_pcbdetach: inp_socket == NULL"));
702 inp->inp_socket->so_pcb = NULL;
703 inp->inp_socket = NULL;
707 in_pcbfree(struct inpcb *inp)
709 struct inpcbinfo *ipi = inp->inp_pcbinfo;
711 KASSERT(inp->inp_socket == NULL, ("in_pcbfree: inp_socket != NULL"));
712 INP_INFO_WLOCK_ASSERT(ipi);
713 INP_LOCK_ASSERT(inp);
715 #if defined(IPSEC) || defined(FAST_IPSEC)
716 ipsec4_delete_pcbpolicy(inp);
718 inp->inp_gencnt = ++ipi->ipi_gencnt;
720 if (inp->inp_options)
721 (void)m_free(inp->inp_options);
722 ip_freemoptions(inp->inp_moptions);
724 INP_LOCK_DESTROY(inp);
726 mac_destroy_inpcb(inp);
728 uma_zfree(ipi->ipi_zone, inp);
732 * TCP needs to maintain its inpcb structure after the TCP connection has
733 * been torn down. However, it must be disconnected from the inpcb hashes as
734 * it must not prevent binding of future connections to the same port/ip
735 * combination by other inpcbs.
738 in_pcbdrop(struct inpcb *inp)
741 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
742 INP_LOCK_ASSERT(inp);
744 inp->inp_vflag |= INP_DROPPED;
745 if (inp->inp_lport) {
746 struct inpcbport *phd = inp->inp_phd;
748 LIST_REMOVE(inp, inp_hash);
749 LIST_REMOVE(inp, inp_portlist);
750 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
751 LIST_REMOVE(phd, phd_hash);
759 in_sockaddr(in_port_t port, struct in_addr *addr_p)
761 struct sockaddr_in *sin;
763 MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
765 sin->sin_family = AF_INET;
766 sin->sin_len = sizeof(*sin);
767 sin->sin_addr = *addr_p;
768 sin->sin_port = port;
770 return (struct sockaddr *)sin;
774 * The wrapper function will pass down the pcbinfo for this function to lock.
775 * The socket must have a valid
776 * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
777 * except through a kernel programming error, so it is acceptable to panic
778 * (or in this case trap) if the PCB is invalid. (Actually, we don't trap
779 * because there actually /is/ a programming error somewhere... XXX)
782 in_setsockaddr(struct socket *so, struct sockaddr **nam,
783 struct inpcbinfo *pcbinfo)
790 KASSERT(inp != NULL, ("in_setsockaddr: inp == NULL"));
793 port = inp->inp_lport;
794 addr = inp->inp_laddr;
797 *nam = in_sockaddr(port, &addr);
802 * The wrapper function will pass down the pcbinfo for this function to lock.
805 in_setpeeraddr(struct socket *so, struct sockaddr **nam,
806 struct inpcbinfo *pcbinfo)
813 KASSERT(inp != NULL, ("in_setpeeraddr: inp == NULL"));
816 port = inp->inp_fport;
817 addr = inp->inp_faddr;
820 *nam = in_sockaddr(port, &addr);
825 in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno,
826 struct inpcb *(*notify)(struct inpcb *, int))
828 struct inpcb *inp, *ninp;
829 struct inpcbhead *head;
831 INP_INFO_WLOCK(pcbinfo);
832 head = pcbinfo->listhead;
833 for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) {
835 ninp = LIST_NEXT(inp, inp_list);
837 if ((inp->inp_vflag & INP_IPV4) == 0) {
842 if (inp->inp_faddr.s_addr != faddr.s_addr ||
843 inp->inp_socket == NULL) {
847 if ((*notify)(inp, errno))
850 INP_INFO_WUNLOCK(pcbinfo);
854 in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
857 struct ip_moptions *imo;
860 INP_INFO_RLOCK(pcbinfo);
861 LIST_FOREACH(inp, pcbinfo->listhead, inp_list) {
863 imo = inp->inp_moptions;
864 if ((inp->inp_vflag & INP_IPV4) &&
867 * Unselect the outgoing interface if it is being
870 if (imo->imo_multicast_ifp == ifp)
871 imo->imo_multicast_ifp = NULL;
874 * Drop multicast group membership if we joined
875 * through the interface being detached.
877 for (i = 0, gap = 0; i < imo->imo_num_memberships;
879 if (imo->imo_membership[i]->inm_ifp == ifp) {
880 in_delmulti(imo->imo_membership[i]);
883 imo->imo_membership[i - gap] =
884 imo->imo_membership[i];
886 imo->imo_num_memberships -= gap;
890 INP_INFO_RUNLOCK(pcbinfo);
894 * Lookup a PCB based on the local address and port.
896 #define INP_LOOKUP_MAPPED_PCB_COST 3
898 in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr,
899 u_int lport_arg, int wild_okay)
904 int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST;
909 u_short lport = lport_arg;
911 INP_INFO_WLOCK_ASSERT(pcbinfo);
914 struct inpcbhead *head;
916 * Look for an unconnected (wildcard foreign addr) PCB that
917 * matches the local address and port we're looking for.
919 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, pcbinfo->hashmask)];
920 LIST_FOREACH(inp, head, inp_hash) {
922 if ((inp->inp_vflag & INP_IPV4) == 0)
925 if (inp->inp_faddr.s_addr == INADDR_ANY &&
926 inp->inp_laddr.s_addr == laddr.s_addr &&
927 inp->inp_lport == lport) {
939 struct inpcbporthead *porthash;
940 struct inpcbport *phd;
941 struct inpcb *match = NULL;
943 * Best fit PCB lookup.
945 * First see if this local port is in use by looking on the
949 porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport,
950 pcbinfo->porthashmask)];
951 LIST_FOREACH(phd, porthash, phd_hash) {
952 if (phd->phd_port == lport)
957 * Port is in use by one or more PCBs. Look for best
960 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
963 if ((inp->inp_vflag & INP_IPV4) == 0)
966 * We never select the PCB that has
967 * INP_IPV6 flag and is bound to :: if
968 * we have another PCB which is bound
969 * to 0.0.0.0. If a PCB has the
970 * INP_IPV6 flag, then we set its cost
971 * higher than IPv4 only PCBs.
973 * Note that the case only happens
974 * when a socket is bound to ::, under
975 * the condition that the use of the
976 * mapped address is allowed.
978 if ((inp->inp_vflag & INP_IPV6) != 0)
979 wildcard += INP_LOOKUP_MAPPED_PCB_COST;
982 * Clean out old time_wait sockets if they
983 * are clogging up needed local ports.
985 if ((inp->inp_vflag & INP_TIMEWAIT) != 0) {
988 tcp_twrecycleable(tw)) {
995 if (inp->inp_faddr.s_addr != INADDR_ANY)
997 if (inp->inp_laddr.s_addr != INADDR_ANY) {
998 if (laddr.s_addr == INADDR_ANY)
1000 else if (inp->inp_laddr.s_addr != laddr.s_addr)
1003 if (laddr.s_addr != INADDR_ANY)
1006 if (wildcard < matchwild) {
1008 matchwild = wildcard;
1009 if (matchwild == 0) {
1018 #undef INP_LOOKUP_MAPPED_PCB_COST
1021 * Lookup PCB in hash list.
1024 in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
1025 u_int fport_arg, struct in_addr laddr, u_int lport_arg, int wildcard,
1028 struct inpcbhead *head;
1030 u_short fport = fport_arg, lport = lport_arg;
1032 INP_INFO_RLOCK_ASSERT(pcbinfo);
1035 * First look for an exact match.
1037 head = &pcbinfo->hashbase[INP_PCBHASH(faddr.s_addr, lport, fport, pcbinfo->hashmask)];
1038 LIST_FOREACH(inp, head, inp_hash) {
1040 if ((inp->inp_vflag & INP_IPV4) == 0)
1043 if (inp->inp_faddr.s_addr == faddr.s_addr &&
1044 inp->inp_laddr.s_addr == laddr.s_addr &&
1045 inp->inp_fport == fport &&
1046 inp->inp_lport == lport) {
1054 struct inpcb *local_wild = NULL;
1056 struct inpcb *local_wild_mapped = NULL;
1057 #endif /* defined(INET6) */
1059 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, pcbinfo->hashmask)];
1060 LIST_FOREACH(inp, head, inp_hash) {
1062 if ((inp->inp_vflag & INP_IPV4) == 0)
1065 if (inp->inp_faddr.s_addr == INADDR_ANY &&
1066 inp->inp_lport == lport) {
1067 if (ifp && ifp->if_type == IFT_FAITH &&
1068 (inp->inp_flags & INP_FAITH) == 0)
1070 if (inp->inp_laddr.s_addr == laddr.s_addr)
1072 else if (inp->inp_laddr.s_addr == INADDR_ANY) {
1074 if (INP_CHECK_SOCKAF(inp->inp_socket,
1076 local_wild_mapped = inp;
1078 #endif /* defined(INET6) */
1084 if (local_wild == NULL)
1085 return (local_wild_mapped);
1086 #endif /* defined(INET6) */
1087 return (local_wild);
1097 * Insert PCB onto various hash lists.
1100 in_pcbinshash(struct inpcb *inp)
1102 struct inpcbhead *pcbhash;
1103 struct inpcbporthead *pcbporthash;
1104 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1105 struct inpcbport *phd;
1106 u_int32_t hashkey_faddr;
1108 INP_INFO_WLOCK_ASSERT(pcbinfo);
1109 INP_LOCK_ASSERT(inp);
1112 if (inp->inp_vflag & INP_IPV6)
1113 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1116 hashkey_faddr = inp->inp_faddr.s_addr;
1118 pcbhash = &pcbinfo->hashbase[INP_PCBHASH(hashkey_faddr,
1119 inp->inp_lport, inp->inp_fport, pcbinfo->hashmask)];
1121 pcbporthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(inp->inp_lport,
1122 pcbinfo->porthashmask)];
1125 * Go through port list and look for a head for this lport.
1127 LIST_FOREACH(phd, pcbporthash, phd_hash) {
1128 if (phd->phd_port == inp->inp_lport)
1132 * If none exists, malloc one and tack it on.
1135 MALLOC(phd, struct inpcbport *, sizeof(struct inpcbport), M_PCB, M_NOWAIT);
1137 return (ENOBUFS); /* XXX */
1139 phd->phd_port = inp->inp_lport;
1140 LIST_INIT(&phd->phd_pcblist);
1141 LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
1144 LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
1145 LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
1150 * Move PCB to the proper hash bucket when { faddr, fport } have been
1151 * changed. NOTE: This does not handle the case of the lport changing (the
1152 * hashed port list would have to be updated as well), so the lport must
1153 * not change after in_pcbinshash() has been called.
1156 in_pcbrehash(struct inpcb *inp)
1158 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1159 struct inpcbhead *head;
1160 u_int32_t hashkey_faddr;
1162 INP_INFO_WLOCK_ASSERT(pcbinfo);
1163 INP_LOCK_ASSERT(inp);
1166 if (inp->inp_vflag & INP_IPV6)
1167 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1170 hashkey_faddr = inp->inp_faddr.s_addr;
1172 head = &pcbinfo->hashbase[INP_PCBHASH(hashkey_faddr,
1173 inp->inp_lport, inp->inp_fport, pcbinfo->hashmask)];
1175 LIST_REMOVE(inp, inp_hash);
1176 LIST_INSERT_HEAD(head, inp, inp_hash);
1180 * Remove PCB from various lists.
1183 in_pcbremlists(struct inpcb *inp)
1185 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1187 INP_INFO_WLOCK_ASSERT(pcbinfo);
1188 INP_LOCK_ASSERT(inp);
1190 inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
1191 if (inp->inp_lport) {
1192 struct inpcbport *phd = inp->inp_phd;
1194 LIST_REMOVE(inp, inp_hash);
1195 LIST_REMOVE(inp, inp_portlist);
1196 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1197 LIST_REMOVE(phd, phd_hash);
1201 LIST_REMOVE(inp, inp_list);
1202 pcbinfo->ipi_count--;
1206 * A set label operation has occurred at the socket layer, propagate the
1207 * label change into the in_pcb for the socket.
1210 in_pcbsosetlabel(struct socket *so)
1215 inp = sotoinpcb(so);
1216 KASSERT(inp != NULL, ("in_pcbsosetlabel: so->so_pcb == NULL"));
1220 mac_inpcb_sosetlabel(so, inp);
1227 * ipport_tick runs once per second, determining if random port allocation
1228 * should be continued. If more than ipport_randomcps ports have been
1229 * allocated in the last second, then we return to sequential port
1230 * allocation. We return to random allocation only once we drop below
1231 * ipport_randomcps for at least ipport_randomtime seconds.
1234 ipport_tick(void *xtp)
1237 if (ipport_tcpallocs <= ipport_tcplastcount + ipport_randomcps) {
1238 if (ipport_stoprandom > 0)
1239 ipport_stoprandom--;
1241 ipport_stoprandom = ipport_randomtime;
1242 ipport_tcplastcount = ipport_tcpallocs;
1243 callout_reset(&ipport_tick_callout, hz, ipport_tick, NULL);