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);
202 mac_destroy_inpcb(inp);
208 if (INP_SOCKAF(so) == AF_INET6) {
209 inp->inp_vflag |= INP_IPV6PROTO;
211 inp->inp_flags |= IN6P_IPV6_V6ONLY;
214 LIST_INSERT_HEAD(pcbinfo->listhead, inp, inp_list);
215 pcbinfo->ipi_count++;
216 so->so_pcb = (caddr_t)inp;
218 if (ip6_auto_flowlabel)
219 inp->inp_flags |= IN6P_AUTOFLOWLABEL;
222 inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
224 #if defined(IPSEC) || defined(FAST_IPSEC) || defined(MAC)
227 uma_zfree(pcbinfo->ipi_zone, inp);
233 in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
237 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
238 INP_LOCK_ASSERT(inp);
240 if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY)
242 anonport = inp->inp_lport == 0 && (nam == NULL ||
243 ((struct sockaddr_in *)nam)->sin_port == 0);
244 error = in_pcbbind_setup(inp, nam, &inp->inp_laddr.s_addr,
245 &inp->inp_lport, cred);
248 if (in_pcbinshash(inp) != 0) {
249 inp->inp_laddr.s_addr = INADDR_ANY;
254 inp->inp_flags |= INP_ANONPORT;
259 * Set up a bind operation on a PCB, performing port allocation
260 * as required, but do not actually modify the PCB. Callers can
261 * either complete the bind by setting inp_laddr/inp_lport and
262 * calling in_pcbinshash(), or they can just use the resulting
263 * port and address to authorise the sending of a once-off packet.
265 * On error, the values of *laddrp and *lportp are not changed.
268 in_pcbbind_setup(struct inpcb *inp, struct sockaddr *nam, in_addr_t *laddrp,
269 u_short *lportp, struct ucred *cred)
271 struct socket *so = inp->inp_socket;
272 unsigned short *lastport;
273 struct sockaddr_in *sin;
274 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
275 struct in_addr laddr;
277 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
278 int error, prison = 0;
281 INP_INFO_WLOCK_ASSERT(pcbinfo);
282 INP_LOCK_ASSERT(inp);
284 if (TAILQ_EMPTY(&in_ifaddrhead)) /* XXX broken! */
285 return (EADDRNOTAVAIL);
286 laddr.s_addr = *laddrp;
287 if (nam != NULL && laddr.s_addr != INADDR_ANY)
289 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
292 sin = (struct sockaddr_in *)nam;
293 if (nam->sa_len != sizeof (*sin))
297 * We should check the family, but old programs
298 * incorrectly fail to initialize it.
300 if (sin->sin_family != AF_INET)
301 return (EAFNOSUPPORT);
303 if (sin->sin_addr.s_addr != INADDR_ANY)
304 if (prison_ip(cred, 0, &sin->sin_addr.s_addr))
306 if (sin->sin_port != *lportp) {
307 /* Don't allow the port to change. */
310 lport = sin->sin_port;
312 /* NB: lport is left as 0 if the port isn't being changed. */
313 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
315 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
316 * allow complete duplication of binding if
317 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
318 * and a multicast address is bound on both
319 * new and duplicated sockets.
321 if (so->so_options & SO_REUSEADDR)
322 reuseport = SO_REUSEADDR|SO_REUSEPORT;
323 } else if (sin->sin_addr.s_addr != INADDR_ANY) {
324 sin->sin_port = 0; /* yech... */
325 bzero(&sin->sin_zero, sizeof(sin->sin_zero));
326 if (ifa_ifwithaddr((struct sockaddr *)sin) == 0)
327 return (EADDRNOTAVAIL);
329 laddr = sin->sin_addr;
333 if (ntohs(lport) <= ipport_reservedhigh &&
334 ntohs(lport) >= ipport_reservedlow &&
335 suser_cred(cred, SUSER_ALLOWJAIL))
339 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
340 suser_cred(so->so_cred, SUSER_ALLOWJAIL) != 0) {
341 t = in_pcblookup_local(inp->inp_pcbinfo,
342 sin->sin_addr, lport,
343 prison ? 0 : INPLOOKUP_WILDCARD);
346 * This entire block sorely needs a rewrite.
349 ((t->inp_vflag & INP_TIMEWAIT) == 0) &&
350 (so->so_type != SOCK_STREAM ||
351 ntohl(t->inp_faddr.s_addr) == INADDR_ANY) &&
352 (ntohl(sin->sin_addr.s_addr) != INADDR_ANY ||
353 ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
354 (t->inp_socket->so_options &
355 SO_REUSEPORT) == 0) &&
356 (so->so_cred->cr_uid !=
357 t->inp_socket->so_cred->cr_uid))
360 if (prison && prison_ip(cred, 0, &sin->sin_addr.s_addr))
361 return (EADDRNOTAVAIL);
362 t = in_pcblookup_local(pcbinfo, sin->sin_addr,
363 lport, prison ? 0 : wild);
364 if (t && (t->inp_vflag & INP_TIMEWAIT)) {
365 if ((reuseport & intotw(t)->tw_so_options) == 0)
369 (reuseport & t->inp_socket->so_options) == 0) {
371 if (ntohl(sin->sin_addr.s_addr) !=
373 ntohl(t->inp_laddr.s_addr) !=
376 INP_SOCKAF(t->inp_socket))
377 #endif /* defined(INET6) */
388 if (laddr.s_addr != INADDR_ANY)
389 if (prison_ip(cred, 0, &laddr.s_addr))
392 if (inp->inp_flags & INP_HIGHPORT) {
393 first = ipport_hifirstauto; /* sysctl */
394 last = ipport_hilastauto;
395 lastport = &pcbinfo->lasthi;
396 } else if (inp->inp_flags & INP_LOWPORT) {
397 if ((error = suser_cred(cred, SUSER_ALLOWJAIL)) != 0)
399 first = ipport_lowfirstauto; /* 1023 */
400 last = ipport_lowlastauto; /* 600 */
401 lastport = &pcbinfo->lastlow;
403 first = ipport_firstauto; /* sysctl */
404 last = ipport_lastauto;
405 lastport = &pcbinfo->lastport;
408 * For UDP, use random port allocation as long as the user
409 * allows it. For TCP (and as of yet unknown) connections,
410 * use random port allocation only if the user allows it AND
411 * ipport_tick() allows it.
413 if (ipport_randomized &&
414 (!ipport_stoprandom || pcbinfo == &udbinfo))
419 * It makes no sense to do random port allocation if
420 * we have the only port available.
424 /* Make sure to not include UDP packets in the count. */
425 if (pcbinfo != &udbinfo)
428 * Simple check to ensure all ports are not used up causing
431 * We split the two cases (up and down) so that the direction
432 * is not being tested on each round of the loop.
440 (arc4random() % (first - last));
441 count = first - last;
444 if (count-- < 0) /* completely used? */
445 return (EADDRNOTAVAIL);
447 if (*lastport > first || *lastport < last)
449 lport = htons(*lastport);
450 } while (in_pcblookup_local(pcbinfo, laddr, lport,
458 (arc4random() % (last - first));
459 count = last - first;
462 if (count-- < 0) /* completely used? */
463 return (EADDRNOTAVAIL);
465 if (*lastport < first || *lastport > last)
467 lport = htons(*lastport);
468 } while (in_pcblookup_local(pcbinfo, laddr, lport,
472 if (prison_ip(cred, 0, &laddr.s_addr))
474 *laddrp = laddr.s_addr;
480 * Connect from a socket to a specified address.
481 * Both address and port must be specified in argument sin.
482 * If don't have a local address for this socket yet,
486 in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
488 u_short lport, fport;
489 in_addr_t laddr, faddr;
492 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
493 INP_LOCK_ASSERT(inp);
495 lport = inp->inp_lport;
496 laddr = inp->inp_laddr.s_addr;
497 anonport = (lport == 0);
498 error = in_pcbconnect_setup(inp, nam, &laddr, &lport, &faddr, &fport,
503 /* Do the initial binding of the local address if required. */
504 if (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0) {
505 inp->inp_lport = lport;
506 inp->inp_laddr.s_addr = laddr;
507 if (in_pcbinshash(inp) != 0) {
508 inp->inp_laddr.s_addr = INADDR_ANY;
514 /* Commit the remaining changes. */
515 inp->inp_lport = lport;
516 inp->inp_laddr.s_addr = laddr;
517 inp->inp_faddr.s_addr = faddr;
518 inp->inp_fport = fport;
521 if (inp->inp_socket->so_type == SOCK_STREAM)
522 ipsec_pcbconn(inp->inp_sp);
525 inp->inp_flags |= INP_ANONPORT;
530 * Set up for a connect from a socket to the specified address.
531 * On entry, *laddrp and *lportp should contain the current local
532 * address and port for the PCB; these are updated to the values
533 * that should be placed in inp_laddr and inp_lport to complete
536 * On success, *faddrp and *fportp will be set to the remote address
537 * and port. These are not updated in the error case.
539 * If the operation fails because the connection already exists,
540 * *oinpp will be set to the PCB of that connection so that the
541 * caller can decide to override it. In all other cases, *oinpp
545 in_pcbconnect_setup(struct inpcb *inp, struct sockaddr *nam,
546 in_addr_t *laddrp, u_short *lportp, in_addr_t *faddrp, u_short *fportp,
547 struct inpcb **oinpp, struct ucred *cred)
549 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
550 struct in_ifaddr *ia;
551 struct sockaddr_in sa;
552 struct ucred *socred;
554 struct in_addr laddr, faddr;
555 u_short lport, fport;
558 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
559 INP_LOCK_ASSERT(inp);
563 if (nam->sa_len != sizeof (*sin))
565 if (sin->sin_family != AF_INET)
566 return (EAFNOSUPPORT);
567 if (sin->sin_port == 0)
568 return (EADDRNOTAVAIL);
569 laddr.s_addr = *laddrp;
571 faddr = sin->sin_addr;
572 fport = sin->sin_port;
573 socred = inp->inp_socket->so_cred;
574 if (laddr.s_addr == INADDR_ANY && jailed(socred)) {
575 bzero(&sa, sizeof(sa));
576 sa.sin_addr.s_addr = htonl(prison_getip(socred));
577 sa.sin_len = sizeof(sa);
578 sa.sin_family = AF_INET;
579 error = in_pcbbind_setup(inp, (struct sockaddr *)&sa,
580 &laddr.s_addr, &lport, cred);
584 if (!TAILQ_EMPTY(&in_ifaddrhead)) {
586 * If the destination address is INADDR_ANY,
587 * use the primary local address.
588 * If the supplied address is INADDR_BROADCAST,
589 * and the primary interface supports broadcast,
590 * choose the broadcast address for that interface.
592 if (faddr.s_addr == INADDR_ANY)
593 faddr = IA_SIN(TAILQ_FIRST(&in_ifaddrhead))->sin_addr;
594 else if (faddr.s_addr == (u_long)INADDR_BROADCAST &&
595 (TAILQ_FIRST(&in_ifaddrhead)->ia_ifp->if_flags &
597 faddr = satosin(&TAILQ_FIRST(
598 &in_ifaddrhead)->ia_broadaddr)->sin_addr;
600 if (laddr.s_addr == INADDR_ANY) {
603 bzero(&sro, sizeof(sro));
604 ia = (struct in_ifaddr *)0;
606 * If route is known our src addr is taken from the i/f,
609 if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0) {
610 /* Find out route to destination */
611 sro.ro_dst.sa_family = AF_INET;
612 sro.ro_dst.sa_len = sizeof(struct sockaddr_in);
613 ((struct sockaddr_in *)&sro.ro_dst)->sin_addr = faddr;
614 rtalloc_ign(&sro, RTF_CLONING);
617 * If we found a route, use the address
618 * corresponding to the outgoing interface.
621 ia = ifatoia(sro.ro_rt->rt_ifa);
625 bzero(&sa, sizeof(sa));
627 sa.sin_len = sizeof(sa);
628 sa.sin_family = AF_INET;
630 ia = ifatoia(ifa_ifwithdstaddr(sintosa(&sa)));
632 ia = ifatoia(ifa_ifwithnet(sintosa(&sa)));
634 return (ENETUNREACH);
637 * If the destination address is multicast and an outgoing
638 * interface has been set as a multicast option, use the
639 * address of that interface as our source address.
641 if (IN_MULTICAST(ntohl(faddr.s_addr)) &&
642 inp->inp_moptions != NULL) {
643 struct ip_moptions *imo;
646 imo = inp->inp_moptions;
647 if (imo->imo_multicast_ifp != NULL) {
648 ifp = imo->imo_multicast_ifp;
649 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
650 if (ia->ia_ifp == ifp)
653 return (EADDRNOTAVAIL);
656 laddr = ia->ia_addr.sin_addr;
659 oinp = in_pcblookup_hash(inp->inp_pcbinfo, faddr, fport, laddr, lport,
667 error = in_pcbbind_setup(inp, NULL, &laddr.s_addr, &lport,
672 *laddrp = laddr.s_addr;
674 *faddrp = faddr.s_addr;
680 in_pcbdisconnect(struct inpcb *inp)
683 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
684 INP_LOCK_ASSERT(inp);
686 inp->inp_faddr.s_addr = INADDR_ANY;
690 ipsec_pcbdisconn(inp->inp_sp);
692 if (inp->inp_socket->so_state & SS_NOFDREF)
697 in_pcbdetach(struct inpcb *inp)
699 struct socket *so = inp->inp_socket;
700 struct inpcbinfo *ipi = inp->inp_pcbinfo;
702 INP_INFO_WLOCK_ASSERT(ipi);
703 INP_LOCK_ASSERT(inp);
705 #if defined(IPSEC) || defined(FAST_IPSEC)
706 ipsec4_delete_pcbpolicy(inp);
708 inp->inp_gencnt = ++ipi->ipi_gencnt;
716 if (inp->inp_options)
717 (void)m_free(inp->inp_options);
718 ip_freemoptions(inp->inp_moptions);
722 mac_destroy_inpcb(inp);
725 uma_zfree(ipi->ipi_zone, inp);
729 in_sockaddr(in_port_t port, struct in_addr *addr_p)
731 struct sockaddr_in *sin;
733 MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
735 sin->sin_family = AF_INET;
736 sin->sin_len = sizeof(*sin);
737 sin->sin_addr = *addr_p;
738 sin->sin_port = port;
740 return (struct sockaddr *)sin;
744 * The wrapper function will pass down the pcbinfo for this function to lock.
745 * The socket must have a valid
746 * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
747 * except through a kernel programming error, so it is acceptable to panic
748 * (or in this case trap) if the PCB is invalid. (Actually, we don't trap
749 * because there actually /is/ a programming error somewhere... XXX)
752 in_setsockaddr(struct socket *so, struct sockaddr **nam,
753 struct inpcbinfo *pcbinfo)
759 INP_INFO_RLOCK(pcbinfo);
762 INP_INFO_RUNLOCK(pcbinfo);
766 port = inp->inp_lport;
767 addr = inp->inp_laddr;
769 INP_INFO_RUNLOCK(pcbinfo);
771 *nam = in_sockaddr(port, &addr);
776 * The wrapper function will pass down the pcbinfo for this function to lock.
779 in_setpeeraddr(struct socket *so, struct sockaddr **nam,
780 struct inpcbinfo *pcbinfo)
786 INP_INFO_RLOCK(pcbinfo);
789 INP_INFO_RUNLOCK(pcbinfo);
793 port = inp->inp_fport;
794 addr = inp->inp_faddr;
796 INP_INFO_RUNLOCK(pcbinfo);
798 *nam = in_sockaddr(port, &addr);
803 in_pcbnotifyall(struct inpcbinfo *pcbinfo, struct in_addr faddr, int errno,
804 struct inpcb *(*notify)(struct inpcb *, int))
806 struct inpcb *inp, *ninp;
807 struct inpcbhead *head;
809 INP_INFO_WLOCK(pcbinfo);
810 head = pcbinfo->listhead;
811 for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) {
813 ninp = LIST_NEXT(inp, inp_list);
815 if ((inp->inp_vflag & INP_IPV4) == 0) {
820 if (inp->inp_faddr.s_addr != faddr.s_addr ||
821 inp->inp_socket == NULL) {
825 if ((*notify)(inp, errno))
828 INP_INFO_WUNLOCK(pcbinfo);
832 in_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
835 struct ip_moptions *imo;
838 INP_INFO_RLOCK(pcbinfo);
839 LIST_FOREACH(inp, pcbinfo->listhead, inp_list) {
841 imo = inp->inp_moptions;
842 if ((inp->inp_vflag & INP_IPV4) &&
845 * Unselect the outgoing interface if it is being
848 if (imo->imo_multicast_ifp == ifp)
849 imo->imo_multicast_ifp = NULL;
852 * Drop multicast group membership if we joined
853 * through the interface being detached.
855 for (i = 0, gap = 0; i < imo->imo_num_memberships;
857 if (imo->imo_membership[i]->inm_ifp == ifp) {
858 in_delmulti(imo->imo_membership[i]);
861 imo->imo_membership[i - gap] =
862 imo->imo_membership[i];
864 imo->imo_num_memberships -= gap;
868 INP_INFO_RUNLOCK(pcbinfo);
872 * Lookup a PCB based on the local address and port.
874 #define INP_LOOKUP_MAPPED_PCB_COST 3
876 in_pcblookup_local(struct inpcbinfo *pcbinfo, struct in_addr laddr,
877 u_int lport_arg, int wild_okay)
881 int matchwild = 3 + INP_LOOKUP_MAPPED_PCB_COST;
886 u_short lport = lport_arg;
888 INP_INFO_WLOCK_ASSERT(pcbinfo);
891 struct inpcbhead *head;
893 * Look for an unconnected (wildcard foreign addr) PCB that
894 * matches the local address and port we're looking for.
896 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, pcbinfo->hashmask)];
897 LIST_FOREACH(inp, head, inp_hash) {
899 if ((inp->inp_vflag & INP_IPV4) == 0)
902 if (inp->inp_faddr.s_addr == INADDR_ANY &&
903 inp->inp_laddr.s_addr == laddr.s_addr &&
904 inp->inp_lport == lport) {
916 struct inpcbporthead *porthash;
917 struct inpcbport *phd;
918 struct inpcb *match = NULL;
920 * Best fit PCB lookup.
922 * First see if this local port is in use by looking on the
925 porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport,
926 pcbinfo->porthashmask)];
927 LIST_FOREACH(phd, porthash, phd_hash) {
928 if (phd->phd_port == lport)
933 * Port is in use by one or more PCBs. Look for best
936 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
939 if ((inp->inp_vflag & INP_IPV4) == 0)
942 * We never select the PCB that has
943 * INP_IPV6 flag and is bound to :: if
944 * we have another PCB which is bound
945 * to 0.0.0.0. If a PCB has the
946 * INP_IPV6 flag, then we set its cost
947 * higher than IPv4 only PCBs.
949 * Note that the case only happens
950 * when a socket is bound to ::, under
951 * the condition that the use of the
952 * mapped address is allowed.
954 if ((inp->inp_vflag & INP_IPV6) != 0)
955 wildcard += INP_LOOKUP_MAPPED_PCB_COST;
957 if (inp->inp_faddr.s_addr != INADDR_ANY)
959 if (inp->inp_laddr.s_addr != INADDR_ANY) {
960 if (laddr.s_addr == INADDR_ANY)
962 else if (inp->inp_laddr.s_addr != laddr.s_addr)
965 if (laddr.s_addr != INADDR_ANY)
968 if (wildcard < matchwild) {
970 matchwild = wildcard;
971 if (matchwild == 0) {
980 #undef INP_LOOKUP_MAPPED_PCB_COST
983 * Lookup PCB in hash list.
986 in_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in_addr faddr,
987 u_int fport_arg, struct in_addr laddr, u_int lport_arg, int wildcard,
990 struct inpcbhead *head;
992 u_short fport = fport_arg, lport = lport_arg;
994 INP_INFO_RLOCK_ASSERT(pcbinfo);
996 * First look for an exact match.
998 head = &pcbinfo->hashbase[INP_PCBHASH(faddr.s_addr, lport, fport, pcbinfo->hashmask)];
999 LIST_FOREACH(inp, head, inp_hash) {
1001 if ((inp->inp_vflag & INP_IPV4) == 0)
1004 if (inp->inp_faddr.s_addr == faddr.s_addr &&
1005 inp->inp_laddr.s_addr == laddr.s_addr &&
1006 inp->inp_fport == fport &&
1007 inp->inp_lport == lport) {
1015 struct inpcb *local_wild = NULL;
1017 struct inpcb *local_wild_mapped = NULL;
1018 #endif /* defined(INET6) */
1020 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, pcbinfo->hashmask)];
1021 LIST_FOREACH(inp, head, inp_hash) {
1023 if ((inp->inp_vflag & INP_IPV4) == 0)
1026 if (inp->inp_faddr.s_addr == INADDR_ANY &&
1027 inp->inp_lport == lport) {
1028 if (ifp && ifp->if_type == IFT_FAITH &&
1029 (inp->inp_flags & INP_FAITH) == 0)
1031 if (inp->inp_laddr.s_addr == laddr.s_addr)
1033 else if (inp->inp_laddr.s_addr == INADDR_ANY) {
1035 if (INP_CHECK_SOCKAF(inp->inp_socket,
1037 local_wild_mapped = inp;
1039 #endif /* defined(INET6) */
1045 if (local_wild == NULL)
1046 return (local_wild_mapped);
1047 #endif /* defined(INET6) */
1048 return (local_wild);
1058 * Insert PCB onto various hash lists.
1061 in_pcbinshash(struct inpcb *inp)
1063 struct inpcbhead *pcbhash;
1064 struct inpcbporthead *pcbporthash;
1065 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1066 struct inpcbport *phd;
1067 u_int32_t hashkey_faddr;
1069 INP_INFO_WLOCK_ASSERT(pcbinfo);
1071 if (inp->inp_vflag & INP_IPV6)
1072 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1075 hashkey_faddr = inp->inp_faddr.s_addr;
1077 pcbhash = &pcbinfo->hashbase[INP_PCBHASH(hashkey_faddr,
1078 inp->inp_lport, inp->inp_fport, pcbinfo->hashmask)];
1080 pcbporthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(inp->inp_lport,
1081 pcbinfo->porthashmask)];
1084 * Go through port list and look for a head for this lport.
1086 LIST_FOREACH(phd, pcbporthash, phd_hash) {
1087 if (phd->phd_port == inp->inp_lport)
1091 * If none exists, malloc one and tack it on.
1094 MALLOC(phd, struct inpcbport *, sizeof(struct inpcbport), M_PCB, M_NOWAIT);
1096 return (ENOBUFS); /* XXX */
1098 phd->phd_port = inp->inp_lport;
1099 LIST_INIT(&phd->phd_pcblist);
1100 LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
1103 LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
1104 LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
1109 * Move PCB to the proper hash bucket when { faddr, fport } have been
1110 * changed. NOTE: This does not handle the case of the lport changing (the
1111 * hashed port list would have to be updated as well), so the lport must
1112 * not change after in_pcbinshash() has been called.
1115 in_pcbrehash(struct inpcb *inp)
1117 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1118 struct inpcbhead *head;
1119 u_int32_t hashkey_faddr;
1121 INP_INFO_WLOCK_ASSERT(pcbinfo);
1122 INP_LOCK_ASSERT(inp);
1124 if (inp->inp_vflag & INP_IPV6)
1125 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1128 hashkey_faddr = inp->inp_faddr.s_addr;
1130 head = &pcbinfo->hashbase[INP_PCBHASH(hashkey_faddr,
1131 inp->inp_lport, inp->inp_fport, pcbinfo->hashmask)];
1133 LIST_REMOVE(inp, inp_hash);
1134 LIST_INSERT_HEAD(head, inp, inp_hash);
1138 * Remove PCB from various lists.
1141 in_pcbremlists(struct inpcb *inp)
1143 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1145 INP_INFO_WLOCK_ASSERT(pcbinfo);
1146 INP_LOCK_ASSERT(inp);
1148 inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
1149 if (inp->inp_lport) {
1150 struct inpcbport *phd = inp->inp_phd;
1152 LIST_REMOVE(inp, inp_hash);
1153 LIST_REMOVE(inp, inp_portlist);
1154 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1155 LIST_REMOVE(phd, phd_hash);
1159 LIST_REMOVE(inp, inp_list);
1160 pcbinfo->ipi_count--;
1164 * A set label operation has occurred at the socket layer, propagate the
1165 * label change into the in_pcb for the socket.
1168 in_pcbsosetlabel(struct socket *so)
1173 inp = (struct inpcb *)so->so_pcb;
1176 mac_inpcb_sosetlabel(so, inp);
1183 * ipport_tick runs once per second, determining if random port
1184 * allocation should be continued. If more than ipport_randomcps
1185 * ports have been allocated in the last second, then we return to
1186 * sequential port allocation. We return to random allocation only
1187 * once we drop below ipport_randomcps for at least ipport_randomtime
1192 ipport_tick(void *xtp)
1194 if (ipport_tcpallocs > ipport_tcplastcount + ipport_randomcps) {
1195 ipport_stoprandom = ipport_randomtime;
1197 if (ipport_stoprandom > 0)
1198 ipport_stoprandom--;
1200 ipport_tcplastcount = ipport_tcpallocs;
1201 callout_reset(&ipport_tick_callout, hz, ipport_tick, NULL);