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;
326 if (ntohs(lport) <= ipport_reservedhigh &&
327 ntohs(lport) >= ipport_reservedlow &&
328 suser_cred(cred, SUSER_ALLOWJAIL))
332 if (so->so_cred->cr_uid != 0 &&
333 !IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
334 t = in_pcblookup_local(inp->inp_pcbinfo,
335 sin->sin_addr, lport,
336 prison ? 0 : INPLOOKUP_WILDCARD);
339 * This entire block sorely needs a rewrite.
342 ((t->inp_vflag & INP_TIMEWAIT) == 0) &&
343 (so->so_type != SOCK_STREAM ||
344 ntohl(t->inp_faddr.s_addr) == INADDR_ANY) &&
345 (ntohl(sin->sin_addr.s_addr) != INADDR_ANY ||
346 ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
347 (t->inp_socket->so_options &
348 SO_REUSEPORT) == 0) &&
349 (so->so_cred->cr_uid !=
350 t->inp_socket->so_cred->cr_uid))
353 if (prison && prison_ip(cred, 0, &sin->sin_addr.s_addr))
354 return (EADDRNOTAVAIL);
355 t = in_pcblookup_local(pcbinfo, sin->sin_addr,
356 lport, prison ? 0 : wild);
357 if (t && (t->inp_vflag & INP_TIMEWAIT)) {
358 if ((reuseport & intotw(t)->tw_so_options) == 0)
362 (reuseport & t->inp_socket->so_options) == 0) {
364 if (ntohl(sin->sin_addr.s_addr) !=
366 ntohl(t->inp_laddr.s_addr) !=
369 INP_SOCKAF(t->inp_socket))
370 #endif /* defined(INET6) */
381 if (laddr.s_addr != INADDR_ANY)
382 if (prison_ip(cred, 0, &laddr.s_addr))
385 if (inp->inp_flags & INP_HIGHPORT) {
386 first = ipport_hifirstauto; /* sysctl */
387 last = ipport_hilastauto;
388 lastport = &pcbinfo->lasthi;
389 } else if (inp->inp_flags & INP_LOWPORT) {
390 if ((error = suser_cred(cred, SUSER_ALLOWJAIL)) != 0)
392 first = ipport_lowfirstauto; /* 1023 */
393 last = ipport_lowlastauto; /* 600 */
394 lastport = &pcbinfo->lastlow;
396 first = ipport_firstauto; /* sysctl */
397 last = ipport_lastauto;
398 lastport = &pcbinfo->lastport;
401 * For UDP, use random port allocation as long as the user
402 * allows it. For TCP (and as of yet unknown) connections,
403 * use random port allocation only if the user allows it AND
404 * ipport_tick() allows it.
406 if (ipport_randomized &&
407 (!ipport_stoprandom || pcbinfo == &udbinfo))
412 * It makes no sense to do random port allocation if
413 * we have the only port available.
417 /* Make sure to not include UDP packets in the count. */
418 if (pcbinfo != &udbinfo)
421 * Simple check to ensure all ports are not used up causing
424 * We split the two cases (up and down) so that the direction
425 * is not being tested on each round of the loop.
433 (arc4random() % (first - last));
434 count = first - last;
437 if (count-- < 0) /* completely used? */
438 return (EADDRNOTAVAIL);
440 if (*lastport > first || *lastport < last)
442 lport = htons(*lastport);
443 } while (in_pcblookup_local(pcbinfo, laddr, lport,
451 (arc4random() % (last - first));
452 count = last - first;
455 if (count-- < 0) /* completely used? */
456 return (EADDRNOTAVAIL);
458 if (*lastport < first || *lastport > last)
460 lport = htons(*lastport);
461 } while (in_pcblookup_local(pcbinfo, laddr, lport,
465 if (prison_ip(cred, 0, &laddr.s_addr))
467 *laddrp = laddr.s_addr;
473 * Connect from a socket to a specified address.
474 * Both address and port must be specified in argument sin.
475 * If don't have a local address for this socket yet,
479 in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
481 u_short lport, fport;
482 in_addr_t laddr, faddr;
485 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
486 INP_LOCK_ASSERT(inp);
488 lport = inp->inp_lport;
489 laddr = inp->inp_laddr.s_addr;
490 anonport = (lport == 0);
491 error = in_pcbconnect_setup(inp, nam, &laddr, &lport, &faddr, &fport,
496 /* Do the initial binding of the local address if required. */
497 if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) {
498 inp->inp_lport = lport;
499 inp->inp_laddr.s_addr = laddr;
500 if (in_pcbinshash(inp) != 0) {
501 inp->inp_laddr.s_addr = INADDR_ANY;
507 /* Commit the remaining changes. */
508 inp->inp_lport = lport;
509 inp->inp_laddr.s_addr = laddr;
510 inp->inp_faddr.s_addr = faddr;
511 inp->inp_fport = fport;
514 if (inp->inp_socket->so_type == SOCK_STREAM)
515 ipsec_pcbconn(inp->inp_sp);
518 inp->inp_flags |= INP_ANONPORT;
523 * Set up for a connect from a socket to the specified address.
524 * On entry, *laddrp and *lportp should contain the current local
525 * address and port for the PCB; these are updated to the values
526 * that should be placed in inp_laddr and inp_lport to complete
529 * On success, *faddrp and *fportp will be set to the remote address
530 * and port. These are not updated in the error case.
532 * If the operation fails because the connection already exists,
533 * *oinpp will be set to the PCB of that connection so that the
534 * caller can decide to override it. In all other cases, *oinpp
538 in_pcbconnect_setup(struct inpcb *inp, struct sockaddr *nam,
539 in_addr_t *laddrp, u_short *lportp, in_addr_t *faddrp, u_short *fportp,
540 struct inpcb **oinpp, struct ucred *cred)
542 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
543 struct in_ifaddr *ia;
544 struct sockaddr_in sa;
545 struct ucred *socred;
547 struct in_addr laddr, faddr;
548 u_short lport, fport;
551 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
552 INP_LOCK_ASSERT(inp);
556 if (nam->sa_len != sizeof (*sin))
558 if (sin->sin_family != AF_INET)
559 return (EAFNOSUPPORT);
560 if (sin->sin_port == 0)
561 return (EADDRNOTAVAIL);
562 laddr.s_addr = *laddrp;
564 faddr = sin->sin_addr;
565 fport = sin->sin_port;
566 socred = inp->inp_socket->so_cred;
567 if (laddr.s_addr == INADDR_ANY && jailed(socred)) {
568 bzero(&sa, sizeof(sa));
569 sa.sin_addr.s_addr = htonl(prison_getip(socred));
570 sa.sin_len = sizeof(sa);
571 sa.sin_family = AF_INET;
572 error = in_pcbbind_setup(inp, (struct sockaddr *)&sa,
573 &laddr.s_addr, &lport, cred);
577 if (!TAILQ_EMPTY(&in_ifaddrhead)) {
579 * If the destination address is INADDR_ANY,
580 * use the primary local address.
581 * If the supplied address is INADDR_BROADCAST,
582 * and the primary interface supports broadcast,
583 * choose the broadcast address for that interface.
585 if (faddr.s_addr == INADDR_ANY)
586 faddr = IA_SIN(TAILQ_FIRST(&in_ifaddrhead))->sin_addr;
587 else if (faddr.s_addr == (u_long)INADDR_BROADCAST &&
588 (TAILQ_FIRST(&in_ifaddrhead)->ia_ifp->if_flags &
590 faddr = satosin(&TAILQ_FIRST(
591 &in_ifaddrhead)->ia_broadaddr)->sin_addr;
593 if (laddr.s_addr == INADDR_ANY) {
596 bzero(&sro, sizeof(sro));
597 ia = (struct in_ifaddr *)0;
599 * If route is known our src addr is taken from the i/f,
602 if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0) {
603 /* Find out route to destination */
604 sro.ro_dst.sa_family = AF_INET;
605 sro.ro_dst.sa_len = sizeof(struct sockaddr_in);
606 ((struct sockaddr_in *)&sro.ro_dst)->sin_addr = faddr;
607 rtalloc_ign(&sro, RTF_CLONING);
610 * If we found a route, use the address
611 * corresponding to the outgoing interface.
614 ia = ifatoia(sro.ro_rt->rt_ifa);
618 bzero(&sa, sizeof(sa));
620 sa.sin_len = sizeof(sa);
621 sa.sin_family = AF_INET;
623 ia = ifatoia(ifa_ifwithdstaddr(sintosa(&sa)));
625 ia = ifatoia(ifa_ifwithnet(sintosa(&sa)));
627 return (ENETUNREACH);
630 * If the destination address is multicast and an outgoing
631 * interface has been set as a multicast option, use the
632 * address of that interface as our source address.
634 if (IN_MULTICAST(ntohl(faddr.s_addr)) &&
635 inp->inp_moptions != NULL) {
636 struct ip_moptions *imo;
639 imo = inp->inp_moptions;
640 if (imo->imo_multicast_ifp != NULL) {
641 ifp = imo->imo_multicast_ifp;
642 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
643 if (ia->ia_ifp == ifp)
646 return (EADDRNOTAVAIL);
649 laddr = ia->ia_addr.sin_addr;
652 oinp = in_pcblookup_hash(inp->inp_pcbinfo, faddr, fport, laddr, lport,
660 error = in_pcbbind_setup(inp, NULL, &laddr.s_addr, &lport,
665 *laddrp = laddr.s_addr;
667 *faddrp = faddr.s_addr;
673 in_pcbdisconnect(struct inpcb *inp)
676 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
677 INP_LOCK_ASSERT(inp);
679 inp->inp_faddr.s_addr = INADDR_ANY;
683 ipsec_pcbdisconn(inp->inp_sp);
685 if (inp->inp_socket->so_state & SS_NOFDREF)
690 in_pcbdetach(struct inpcb *inp)
692 struct socket *so = inp->inp_socket;
693 struct inpcbinfo *ipi = inp->inp_pcbinfo;
695 INP_INFO_WLOCK_ASSERT(ipi);
696 INP_LOCK_ASSERT(inp);
698 #if defined(IPSEC) || defined(FAST_IPSEC)
699 ipsec4_delete_pcbpolicy(inp);
701 inp->inp_gencnt = ++ipi->ipi_gencnt;
709 if (inp->inp_options)
710 (void)m_free(inp->inp_options);
711 ip_freemoptions(inp->inp_moptions);
713 INP_LOCK_DESTROY(inp);
715 mac_destroy_inpcb(inp);
717 uma_zfree(ipi->ipi_zone, inp);
721 in_sockaddr(in_port_t port, struct in_addr *addr_p)
723 struct sockaddr_in *sin;
725 MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
727 sin->sin_family = AF_INET;
728 sin->sin_len = sizeof(*sin);
729 sin->sin_addr = *addr_p;
730 sin->sin_port = port;
732 return (struct sockaddr *)sin;
736 * The wrapper function will pass down the pcbinfo for this function to lock.
737 * The socket must have a valid
738 * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
739 * except through a kernel programming error, so it is acceptable to panic
740 * (or in this case trap) if the PCB is invalid. (Actually, we don't trap
741 * because there actually /is/ a programming error somewhere... XXX)
744 in_setsockaddr(struct socket *so, struct sockaddr **nam,
745 struct inpcbinfo *pcbinfo)
751 INP_INFO_RLOCK(pcbinfo);
754 INP_INFO_RUNLOCK(pcbinfo);
758 port = inp->inp_lport;
759 addr = inp->inp_laddr;
761 INP_INFO_RUNLOCK(pcbinfo);
763 *nam = in_sockaddr(port, &addr);
768 * The wrapper function will pass down the pcbinfo for this function to lock.
771 in_setpeeraddr(struct socket *so, struct sockaddr **nam,
772 struct inpcbinfo *pcbinfo)
778 INP_INFO_RLOCK(pcbinfo);
781 INP_INFO_RUNLOCK(pcbinfo);
785 port = inp->inp_fport;
786 addr = inp->inp_faddr;
788 INP_INFO_RUNLOCK(pcbinfo);
790 *nam = in_sockaddr(port, &addr);
795 in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno,
796 struct inpcb *(*notify)(struct inpcb *, int))
798 struct inpcb *inp, *ninp;
799 struct inpcbhead *head;
801 INP_INFO_WLOCK(pcbinfo);
802 head = pcbinfo->listhead;
803 for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) {
805 ninp = LIST_NEXT(inp, inp_list);
807 if ((inp->inp_vflag & INP_IPV4) == 0) {
812 if (inp->inp_faddr.s_addr != faddr.s_addr ||
813 inp->inp_socket == NULL) {
817 if ((*notify)(inp, errno))
820 INP_INFO_WUNLOCK(pcbinfo);
824 in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
827 struct ip_moptions *imo;
830 INP_INFO_RLOCK(pcbinfo);
831 LIST_FOREACH(inp, pcbinfo->listhead, inp_list) {
833 imo = inp->inp_moptions;
834 if ((inp->inp_vflag & INP_IPV4) &&
837 * Unselect the outgoing interface if it is being
840 if (imo->imo_multicast_ifp == ifp)
841 imo->imo_multicast_ifp = NULL;
844 * Drop multicast group membership if we joined
845 * through the interface being detached.
847 for (i = 0, gap = 0; i < imo->imo_num_memberships;
849 if (imo->imo_membership[i]->inm_ifp == ifp) {
850 in_delmulti(imo->imo_membership[i]);
853 imo->imo_membership[i - gap] =
854 imo->imo_membership[i];
856 imo->imo_num_memberships -= gap;
860 INP_INFO_RUNLOCK(pcbinfo);
864 * Lookup a PCB based on the local address and port.
866 #define INP_LOOKUP_MAPPED_PCB_COST 3
868 in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr,
869 u_int lport_arg, int wild_okay)
873 int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST;
878 u_short lport = lport_arg;
880 INP_INFO_WLOCK_ASSERT(pcbinfo);
883 struct inpcbhead *head;
885 * Look for an unconnected (wildcard foreign addr) PCB that
886 * matches the local address and port we're looking for.
888 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, pcbinfo->hashmask)];
889 LIST_FOREACH(inp, head, inp_hash) {
891 if ((inp->inp_vflag & INP_IPV4) == 0)
894 if (inp->inp_faddr.s_addr == INADDR_ANY &&
895 inp->inp_laddr.s_addr == laddr.s_addr &&
896 inp->inp_lport == lport) {
908 struct inpcbporthead *porthash;
909 struct inpcbport *phd;
910 struct inpcb *match = NULL;
912 * Best fit PCB lookup.
914 * First see if this local port is in use by looking on the
918 porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport,
919 pcbinfo->porthashmask)];
920 LIST_FOREACH(phd, porthash, phd_hash) {
921 if (phd->phd_port == lport)
926 * Port is in use by one or more PCBs. Look for best
929 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
932 if ((inp->inp_vflag & INP_IPV4) == 0)
935 * We never select the PCB that has
936 * INP_IPV6 flag and is bound to :: if
937 * we have another PCB which is bound
938 * to 0.0.0.0. If a PCB has the
939 * INP_IPV6 flag, then we set its cost
940 * higher than IPv4 only PCBs.
942 * Note that the case only happens
943 * when a socket is bound to ::, under
944 * the condition that the use of the
945 * mapped address is allowed.
947 if ((inp->inp_vflag & INP_IPV6) != 0)
948 wildcard += INP_LOOKUP_MAPPED_PCB_COST;
951 * Clean out old time_wait sockets if they
952 * are clogging up needed local ports.
954 if ((inp->inp_vflag & INP_TIMEWAIT) != 0) {
955 if (tcp_twrecycleable((struct tcptw *)inp->inp_ppcb)) {
957 tcp_twclose((struct tcptw *)inp->inp_ppcb, 0);
962 if (inp->inp_faddr.s_addr != INADDR_ANY)
964 if (inp->inp_laddr.s_addr != INADDR_ANY) {
965 if (laddr.s_addr == INADDR_ANY)
967 else if (inp->inp_laddr.s_addr != laddr.s_addr)
970 if (laddr.s_addr != INADDR_ANY)
973 if (wildcard < matchwild) {
975 matchwild = wildcard;
976 if (matchwild == 0) {
985 #undef INP_LOOKUP_MAPPED_PCB_COST
988 * Lookup PCB in hash list.
991 in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
992 u_int fport_arg, struct in_addr laddr, u_int lport_arg, int wildcard,
995 struct inpcbhead *head;
997 u_short fport = fport_arg, lport = lport_arg;
999 INP_INFO_RLOCK_ASSERT(pcbinfo);
1001 * First look for an exact match.
1003 head = &pcbinfo->hashbase[INP_PCBHASH(faddr.s_addr, lport, fport, pcbinfo->hashmask)];
1004 LIST_FOREACH(inp, head, inp_hash) {
1006 if ((inp->inp_vflag & INP_IPV4) == 0)
1009 if (inp->inp_faddr.s_addr == faddr.s_addr &&
1010 inp->inp_laddr.s_addr == laddr.s_addr &&
1011 inp->inp_fport == fport &&
1012 inp->inp_lport == lport) {
1020 struct inpcb *local_wild = NULL;
1022 struct inpcb *local_wild_mapped = NULL;
1023 #endif /* defined(INET6) */
1025 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, pcbinfo->hashmask)];
1026 LIST_FOREACH(inp, head, inp_hash) {
1028 if ((inp->inp_vflag & INP_IPV4) == 0)
1031 if (inp->inp_faddr.s_addr == INADDR_ANY &&
1032 inp->inp_lport == lport) {
1033 if (ifp && ifp->if_type == IFT_FAITH &&
1034 (inp->inp_flags & INP_FAITH) == 0)
1036 if (inp->inp_laddr.s_addr == laddr.s_addr)
1038 else if (inp->inp_laddr.s_addr == INADDR_ANY) {
1040 if (INP_CHECK_SOCKAF(inp->inp_socket,
1042 local_wild_mapped = inp;
1044 #endif /* defined(INET6) */
1050 if (local_wild == NULL)
1051 return (local_wild_mapped);
1052 #endif /* defined(INET6) */
1053 return (local_wild);
1063 * Insert PCB onto various hash lists.
1066 in_pcbinshash(struct inpcb *inp)
1068 struct inpcbhead *pcbhash;
1069 struct inpcbporthead *pcbporthash;
1070 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1071 struct inpcbport *phd;
1072 u_int32_t hashkey_faddr;
1074 INP_INFO_WLOCK_ASSERT(pcbinfo);
1076 if (inp->inp_vflag & INP_IPV6)
1077 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1080 hashkey_faddr = inp->inp_faddr.s_addr;
1082 pcbhash = &pcbinfo->hashbase[INP_PCBHASH(hashkey_faddr,
1083 inp->inp_lport, inp->inp_fport, pcbinfo->hashmask)];
1085 pcbporthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(inp->inp_lport,
1086 pcbinfo->porthashmask)];
1089 * Go through port list and look for a head for this lport.
1091 LIST_FOREACH(phd, pcbporthash, phd_hash) {
1092 if (phd->phd_port == inp->inp_lport)
1096 * If none exists, malloc one and tack it on.
1099 MALLOC(phd, struct inpcbport *, sizeof(struct inpcbport), M_PCB, M_NOWAIT);
1101 return (ENOBUFS); /* XXX */
1103 phd->phd_port = inp->inp_lport;
1104 LIST_INIT(&phd->phd_pcblist);
1105 LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
1108 LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
1109 LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
1114 * Move PCB to the proper hash bucket when { faddr, fport } have been
1115 * changed. NOTE: This does not handle the case of the lport changing (the
1116 * hashed port list would have to be updated as well), so the lport must
1117 * not change after in_pcbinshash() has been called.
1120 in_pcbrehash(struct inpcb *inp)
1122 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1123 struct inpcbhead *head;
1124 u_int32_t hashkey_faddr;
1126 INP_INFO_WLOCK_ASSERT(pcbinfo);
1127 INP_LOCK_ASSERT(inp);
1129 if (inp->inp_vflag & INP_IPV6)
1130 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1133 hashkey_faddr = inp->inp_faddr.s_addr;
1135 head = &pcbinfo->hashbase[INP_PCBHASH(hashkey_faddr,
1136 inp->inp_lport, inp->inp_fport, pcbinfo->hashmask)];
1138 LIST_REMOVE(inp, inp_hash);
1139 LIST_INSERT_HEAD(head, inp, inp_hash);
1143 * Remove PCB from various lists.
1146 in_pcbremlists(struct inpcb *inp)
1148 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1150 INP_INFO_WLOCK_ASSERT(pcbinfo);
1151 INP_LOCK_ASSERT(inp);
1153 inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
1154 if (inp->inp_lport) {
1155 struct inpcbport *phd = inp->inp_phd;
1157 LIST_REMOVE(inp, inp_hash);
1158 LIST_REMOVE(inp, inp_portlist);
1159 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1160 LIST_REMOVE(phd, phd_hash);
1164 LIST_REMOVE(inp, inp_list);
1165 pcbinfo->ipi_count--;
1169 * A set label operation has occurred at the socket layer, propagate the
1170 * label change into the in_pcb for the socket.
1173 in_pcbsosetlabel(struct socket *so)
1178 inp = (struct inpcb *)so->so_pcb;
1181 mac_inpcb_sosetlabel(so, inp);
1188 * ipport_tick runs once per second, determining if random port
1189 * allocation should be continued. If more than ipport_randomcps
1190 * ports have been allocated in the last second, then we return to
1191 * sequential port allocation. We return to random allocation only
1192 * once we drop below ipport_randomcps for at least ipport_randomtime
1197 ipport_tick(void *xtp)
1199 if (ipport_tcpallocs > ipport_tcplastcount + ipport_randomcps) {
1200 ipport_stoprandom = ipport_randomtime;
1202 if (ipport_stoprandom > 0)
1203 ipport_stoprandom--;
1205 ipport_tcplastcount = ipport_tcpallocs;
1206 callout_reset(&ipport_tick_callout, hz, ipport_tick, NULL);