2 * Copyright (c) 2004 Jeffrey M. Hsu. All rights reserved.
3 * Copyright (c) 2004 The DragonFly Project. All rights reserved.
5 * This code is derived from software contributed to The DragonFly Project
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of The DragonFly Project nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific, prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * Copyright (c) 2004 Jeffrey M. Hsu. All rights reserved.
37 * License terms: all terms for the DragonFly license above plus the following:
39 * 4. All advertising materials mentioning features or use of this software
40 * must display the following acknowledgement:
42 * This product includes software developed by Jeffrey M. Hsu
43 * for the DragonFly Project.
45 * This requirement may be waived with permission from Jeffrey Hsu.
46 * This requirement will sunset and may be removed on July 8 2005,
47 * after which the standard DragonFly license (as shown above) will
52 * Copyright (c) 1982, 1986, 1991, 1993, 1995
53 * The Regents of the University of California. All rights reserved.
55 * Redistribution and use in source and binary forms, with or without
56 * modification, are permitted provided that the following conditions
58 * 1. Redistributions of source code must retain the above copyright
59 * notice, this list of conditions and the following disclaimer.
60 * 2. Redistributions in binary form must reproduce the above copyright
61 * notice, this list of conditions and the following disclaimer in the
62 * documentation and/or other materials provided with the distribution.
63 * 3. All advertising materials mentioning features or use of this software
64 * must display the following acknowledgement:
65 * This product includes software developed by the University of
66 * California, Berkeley and its contributors.
67 * 4. Neither the name of the University nor the names of its contributors
68 * may be used to endorse or promote products derived from this software
69 * without specific prior written permission.
71 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
72 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
73 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
74 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
75 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
76 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
77 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
78 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
79 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
80 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
83 * @(#)in_pcb.c 8.4 (Berkeley) 5/24/95
84 * $FreeBSD: src/sys/netinet/in_pcb.c,v 1.59.2.27 2004/01/02 04:06:42 ambrisko Exp $
85 * $DragonFly: src/sys/netinet/in_pcb.c,v 1.25 2004/08/11 02:36:22 dillon Exp $
88 #include "opt_ipsec.h"
89 #include "opt_inet6.h"
91 #include <sys/param.h>
92 #include <sys/systm.h>
93 #include <sys/malloc.h>
95 #include <sys/domain.h>
96 #include <sys/protosw.h>
97 #include <sys/socket.h>
98 #include <sys/socketvar.h>
100 #include <sys/jail.h>
101 #include <sys/kernel.h>
102 #include <sys/sysctl.h>
104 #include <machine/limits.h>
106 #include <vm/vm_zone.h>
109 #include <net/if_types.h>
110 #include <net/route.h>
112 #include <netinet/in.h>
113 #include <netinet/in_pcb.h>
114 #include <netinet/in_var.h>
115 #include <netinet/ip_var.h>
117 #include <netinet/ip6.h>
118 #include <netinet6/ip6_var.h>
122 #include <netinet6/ipsec.h>
123 #include <netproto/key/key.h>
127 #if defined(IPSEC) || defined(IPSEC_ESP)
128 #error "Bad idea: don't compile with both IPSEC and FAST_IPSEC!"
131 #include <netipsec/ipsec.h>
132 #include <netipsec/key.h>
134 #endif /* FAST_IPSEC */
136 struct in_addr zeroin_addr;
139 * These configure the range of local port addresses assigned to
140 * "unspecified" outgoing connections/packets/whatever.
142 int ipport_lowfirstauto = IPPORT_RESERVED - 1; /* 1023 */
143 int ipport_lowlastauto = IPPORT_RESERVEDSTART; /* 600 */
145 int ipport_firstauto = IPPORT_RESERVED; /* 1024 */
146 int ipport_lastauto = IPPORT_USERRESERVED; /* 5000 */
148 int ipport_hifirstauto = IPPORT_HIFIRSTAUTO; /* 49152 */
149 int ipport_hilastauto = IPPORT_HILASTAUTO; /* 65535 */
151 /* Allocate ephermal source ports in random order. */
152 int ipport_randomized = 1;
155 RANGECHK(int var, int min, int max)
164 sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS)
168 error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
170 RANGECHK(ipport_lowfirstauto, 1, IPPORT_RESERVED - 1);
171 RANGECHK(ipport_lowlastauto, 1, IPPORT_RESERVED - 1);
173 RANGECHK(ipport_firstauto, IPPORT_RESERVED, USHRT_MAX);
174 RANGECHK(ipport_lastauto, IPPORT_RESERVED, USHRT_MAX);
176 RANGECHK(ipport_hifirstauto, IPPORT_RESERVED, USHRT_MAX);
177 RANGECHK(ipport_hilastauto, IPPORT_RESERVED, USHRT_MAX);
182 SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0, "IP Ports");
184 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst, CTLTYPE_INT|CTLFLAG_RW,
185 &ipport_lowfirstauto, 0, &sysctl_net_ipport_check, "I", "");
186 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast, CTLTYPE_INT|CTLFLAG_RW,
187 &ipport_lowlastauto, 0, &sysctl_net_ipport_check, "I", "");
188 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first, CTLTYPE_INT|CTLFLAG_RW,
189 &ipport_firstauto, 0, &sysctl_net_ipport_check, "I", "");
190 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last, CTLTYPE_INT|CTLFLAG_RW,
191 &ipport_lastauto, 0, &sysctl_net_ipport_check, "I", "");
192 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst, CTLTYPE_INT|CTLFLAG_RW,
193 &ipport_hifirstauto, 0, &sysctl_net_ipport_check, "I", "");
194 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast, CTLTYPE_INT|CTLFLAG_RW,
195 &ipport_hilastauto, 0, &sysctl_net_ipport_check, "I", "");
196 SYSCTL_INT(_net_inet_ip_portrange, OID_AUTO, randomized, CTLFLAG_RW,
197 &ipport_randomized, 0, "");
200 * in_pcb.c: manage the Protocol Control Blocks.
202 * NOTE: It is assumed that most of these functions will be called at
203 * splnet(). XXX - There are, unfortunately, a few exceptions to this
204 * rule that should be fixed.
206 * NOTE: The caller should initialize the cpu field to the cpu running the
207 * protocol stack associated with this inpcbinfo.
211 in_pcbinfo_init(struct inpcbinfo *pcbinfo)
213 LIST_INIT(&pcbinfo->pcblisthead);
218 * Allocate a PCB and associate it with the socket.
221 in_pcballoc(struct socket *so, struct inpcbinfo *pcbinfo)
228 inp = zalloc(pcbinfo->ipi_zone);
231 bzero((caddr_t)inp, sizeof *inp);
232 inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
233 inp->inp_pcbinfo = inp->inp_cpcbinfo = pcbinfo;
234 inp->inp_socket = so;
236 error = ipsec_init_policy(so, &inp->inp_sp);
238 zfree(pcbinfo->ipi_zone, inp);
243 if (INP_SOCKAF(so) == AF_INET6 && ip6_v6only)
244 inp->inp_flags |= IN6P_IPV6_V6ONLY;
245 if (ip6_auto_flowlabel)
246 inp->inp_flags |= IN6P_AUTOFLOWLABEL;
248 so->so_pcb = (caddr_t)inp;
249 LIST_INSERT_HEAD(&pcbinfo->pcblisthead, inp, inp_list);
250 pcbinfo->ipi_count++;
255 in_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct thread *td)
257 struct socket *so = inp->inp_socket;
258 struct proc *p = td->td_proc;
259 unsigned short *lastport;
260 struct sockaddr_in *sin;
261 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
263 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
264 int error, prison = 0;
268 if (TAILQ_EMPTY(&in_ifaddrhead)) /* XXX broken! */
269 return (EADDRNOTAVAIL);
270 if (inp->inp_lport != 0 || inp->inp_laddr.s_addr != INADDR_ANY)
271 return (EINVAL); /* already bound */
272 if (!(so->so_options & (SO_REUSEADDR|SO_REUSEPORT)))
273 wild = 1; /* neither SO_REUSEADDR nor SO_REUSEPORT is set */
275 sin = (struct sockaddr_in *)nam;
276 if (nam->sa_len != sizeof *sin)
280 * We should check the family, but old programs
281 * incorrectly fail to initialize it.
283 if (sin->sin_family != AF_INET)
284 return (EAFNOSUPPORT);
286 if (sin->sin_addr.s_addr != INADDR_ANY &&
287 prison_ip(td, 0, &sin->sin_addr.s_addr))
289 lport = sin->sin_port;
290 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
292 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
293 * allow complete duplication of binding if
294 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
295 * and a multicast address is bound on both
296 * new and duplicated sockets.
298 if (so->so_options & SO_REUSEADDR)
299 reuseport = SO_REUSEADDR | SO_REUSEPORT;
300 } else if (sin->sin_addr.s_addr != INADDR_ANY) {
301 sin->sin_port = 0; /* yech... */
302 bzero(&sin->sin_zero, sizeof sin->sin_zero);
303 if (ifa_ifwithaddr((struct sockaddr *)sin) == NULL)
304 return (EADDRNOTAVAIL);
310 if (ntohs(lport) < IPPORT_RESERVED &&
311 p && suser_cred(p->p_ucred, PRISON_ROOT))
313 if (p && p->p_ucred->cr_prison)
315 if (so->so_cred->cr_uid != 0 &&
316 !IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
317 t = in_pcblookup_local(inp->inp_pcbinfo,
318 sin->sin_addr, lport,
319 prison ? 0 : INPLOOKUP_WILDCARD);
321 (!in_nullhost(sin->sin_addr) ||
322 !in_nullhost(t->inp_laddr) ||
323 (t->inp_socket->so_options &
324 SO_REUSEPORT) == 0) &&
325 (so->so_cred->cr_uid !=
326 t->inp_socket->so_cred->cr_uid)) {
328 if (!in_nullhost(sin->sin_addr) ||
329 !in_nullhost(t->inp_laddr) ||
331 INP_SOCKAF(t->inp_socket))
336 if (prison && prison_ip(td, 0, &sin->sin_addr.s_addr))
337 return (EADDRNOTAVAIL);
338 t = in_pcblookup_local(pcbinfo, sin->sin_addr,
339 lport, prison ? 0 : wild);
340 if (t && !(reuseport & t->inp_socket->so_options)) {
342 if (!in_nullhost(sin->sin_addr) ||
343 !in_nullhost(t->inp_laddr) ||
344 INP_SOCKAF(so) == INP_SOCKAF(t->inp_socket))
349 inp->inp_laddr = sin->sin_addr;
355 if (inp->inp_laddr.s_addr != INADDR_ANY &&
356 prison_ip(td, 0, &inp->inp_laddr.s_addr )) {
357 inp->inp_laddr.s_addr = INADDR_ANY;
360 inp->inp_flags |= INP_ANONPORT;
362 if (inp->inp_flags & INP_HIGHPORT) {
363 first = ipport_hifirstauto; /* sysctl */
364 last = ipport_hilastauto;
365 lastport = &pcbinfo->lasthi;
366 } else if (inp->inp_flags & INP_LOWPORT) {
368 (error = suser_cred(p->p_ucred, PRISON_ROOT))) {
369 inp->inp_laddr.s_addr = INADDR_ANY;
372 first = ipport_lowfirstauto; /* 1023 */
373 last = ipport_lowlastauto; /* 600 */
374 lastport = &pcbinfo->lastlow;
376 first = ipport_firstauto; /* sysctl */
377 last = ipport_lastauto;
378 lastport = &pcbinfo->lastport;
381 * Simple check to ensure all ports are not used up causing
384 * We split the two cases (up and down) so that the direction
385 * is not being tested on each round of the loop.
391 if (ipport_randomized)
393 (arc4random() % (first - last));
394 count = first - last;
397 if (count-- < 0) { /* completely used? */
398 inp->inp_laddr.s_addr = INADDR_ANY;
399 return (EADDRNOTAVAIL);
402 if (*lastport > first || *lastport < last)
404 lport = htons(*lastport);
405 } while (in_pcblookup_local(pcbinfo,
406 inp->inp_laddr, lport, wild));
411 if (ipport_randomized)
413 (arc4random() % (last - first));
414 count = last - first;
417 if (count-- < 0) { /* completely used? */
418 inp->inp_laddr.s_addr = INADDR_ANY;
419 return (EADDRNOTAVAIL);
422 if (*lastport < first || *lastport > last)
424 lport = htons(*lastport);
425 } while (in_pcblookup_local(pcbinfo,
426 inp->inp_laddr, lport, wild));
429 inp->inp_lport = lport;
430 if (prison_ip(td, 0, &inp->inp_laddr.s_addr)) {
431 inp->inp_laddr.s_addr = INADDR_ANY;
435 if (in_pcbinsporthash(inp) != 0) {
436 inp->inp_laddr.s_addr = INADDR_ANY;
444 * Transform old in_pcbconnect() into an inner subroutine for new
445 * in_pcbconnect(): Do some validity-checking on the remote
446 * address (in mbuf 'nam') and then determine local host address
447 * (i.e., which interface) to use to access that remote host.
449 * This preserves definition of in_pcbconnect(), while supporting a
450 * slightly different version for T/TCP. (This is more than
451 * a bit of a kludge, but cleaning up the internal interfaces would
452 * have forced minor changes in every protocol).
455 in_pcbladdr(inp, nam, plocal_sin)
457 struct sockaddr *nam;
458 struct sockaddr_in **plocal_sin;
460 struct in_ifaddr *ia;
461 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
463 if (nam->sa_len != sizeof *sin)
465 if (sin->sin_family != AF_INET)
466 return (EAFNOSUPPORT);
467 if (sin->sin_port == 0)
468 return (EADDRNOTAVAIL);
469 if (!TAILQ_EMPTY(&in_ifaddrhead)) {
470 ia = TAILQ_FIRST(&in_ifaddrhead);
472 * If the destination address is INADDR_ANY,
473 * use the primary local address.
474 * If the supplied address is INADDR_BROADCAST,
475 * and the primary interface supports broadcast,
476 * choose the broadcast address for that interface.
478 if (sin->sin_addr.s_addr == INADDR_ANY)
479 sin->sin_addr = IA_SIN(ia)->sin_addr;
480 else if (sin->sin_addr.s_addr == (u_long)INADDR_BROADCAST &&
481 (ia->ia_ifp->if_flags & IFF_BROADCAST))
482 sin->sin_addr = satosin(&ia->ia_broadaddr)->sin_addr;
484 if (inp->inp_laddr.s_addr == INADDR_ANY) {
487 ia = (struct in_ifaddr *)NULL;
489 * If route is known or can be allocated now,
490 * our src addr is taken from the i/f, else punt.
491 * Note that we should check the address family of the cached
492 * destination, in case of sharing the cache with IPv6.
494 ro = &inp->inp_route;
496 (!(ro->ro_rt->rt_flags & RTF_UP) ||
497 ro->ro_dst.sa_family != AF_INET ||
498 satosin(&ro->ro_dst)->sin_addr.s_addr !=
499 sin->sin_addr.s_addr ||
500 inp->inp_socket->so_options & SO_DONTROUTE)) {
502 ro->ro_rt = (struct rtentry *)NULL;
504 if (!(inp->inp_socket->so_options & SO_DONTROUTE) && /*XXX*/
505 (ro->ro_rt == (struct rtentry *)NULL ||
506 ro->ro_rt->rt_ifp == (struct ifnet *)NULL)) {
507 /* No route yet, so try to acquire one */
508 bzero(&ro->ro_dst, sizeof(struct sockaddr_in));
509 ro->ro_dst.sa_family = AF_INET;
510 ro->ro_dst.sa_len = sizeof(struct sockaddr_in);
511 ((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
516 * If we found a route, use the address
517 * corresponding to the outgoing interface
518 * unless it is the loopback (in case a route
519 * to our address on another net goes to loopback).
521 if (ro->ro_rt && !(ro->ro_rt->rt_ifp->if_flags & IFF_LOOPBACK))
522 ia = ifatoia(ro->ro_rt->rt_ifa);
524 u_short fport = sin->sin_port;
527 ia = ifatoia(ifa_ifwithdstaddr(sintosa(sin)));
529 ia = ifatoia(ifa_ifwithnet(sintosa(sin)));
530 sin->sin_port = fport;
532 ia = TAILQ_FIRST(&in_ifaddrhead);
534 return (EADDRNOTAVAIL);
537 * If the destination address is multicast and an outgoing
538 * interface has been set as a multicast option, use the
539 * address of that interface as our source address.
541 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
542 inp->inp_moptions != NULL) {
543 struct ip_moptions *imo;
546 imo = inp->inp_moptions;
547 if (imo->imo_multicast_ifp != NULL) {
548 ifp = imo->imo_multicast_ifp;
549 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
550 if (ia->ia_ifp == ifp)
553 return (EADDRNOTAVAIL);
557 * Don't do pcblookup call here; return interface in plocal_sin
558 * and exit to caller, that will do the lookup.
560 *plocal_sin = &ia->ia_addr;
568 * Connect from a socket to a specified address.
569 * Both address and port must be specified in argument sin.
570 * If don't have a local address for this socket yet,
574 in_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct thread *td)
576 struct sockaddr_in *if_sin;
577 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
578 struct sockaddr_in sa;
579 struct ucred *cr = td->td_proc ? td->td_proc->p_ucred : NULL;
582 if (cr && cr->cr_prison != NULL && in_nullhost(inp->inp_laddr)) {
583 bzero(&sa, sizeof sa);
584 sa.sin_addr.s_addr = htonl(cr->cr_prison->pr_ip);
585 sa.sin_len = sizeof sa;
586 sa.sin_family = AF_INET;
587 error = in_pcbbind(inp, (struct sockaddr *)&sa, td);
592 /* Call inner routine to assign local interface address. */
593 if ((error = in_pcbladdr(inp, nam, &if_sin)) != 0)
596 if (in_pcblookup_hash(inp->inp_cpcbinfo, sin->sin_addr, sin->sin_port,
597 inp->inp_laddr.s_addr ? inp->inp_laddr : if_sin->sin_addr,
598 inp->inp_lport, FALSE, NULL) != NULL) {
601 if (inp->inp_laddr.s_addr == INADDR_ANY) {
602 if (inp->inp_lport == 0) {
603 error = in_pcbbind(inp, (struct sockaddr *)NULL, td);
607 inp->inp_laddr = if_sin->sin_addr;
609 inp->inp_faddr = sin->sin_addr;
610 inp->inp_fport = sin->sin_port;
611 in_pcbinsconnhash(inp);
616 in_pcbdisconnect(inp)
620 inp->inp_faddr.s_addr = INADDR_ANY;
622 in_pcbremconnhash(inp);
623 if (inp->inp_socket->so_state & SS_NOFDREF)
631 struct socket *so = inp->inp_socket;
632 struct inpcbinfo *ipi = inp->inp_pcbinfo;
635 ipsec4_delete_pcbpolicy(inp);
637 inp->inp_gencnt = ++ipi->ipi_gencnt;
641 if (inp->inp_options)
642 (void)m_free(inp->inp_options);
643 if (inp->inp_route.ro_rt)
644 rtfree(inp->inp_route.ro_rt);
645 ip_freemoptions(inp->inp_moptions);
647 zfree(ipi->ipi_zone, inp);
651 * The calling convention of in_setsockaddr() and in_setpeeraddr() was
652 * modified to match the pru_sockaddr() and pru_peeraddr() entry points
653 * in struct pr_usrreqs, so that protocols can just reference then directly
654 * without the need for a wrapper function. The socket must have a valid
655 * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
656 * except through a kernel programming error, so it is acceptable to panic
657 * (or in this case trap) if the PCB is invalid. (Actually, we don't trap
658 * because there actually /is/ a programming error somewhere... XXX)
661 in_setsockaddr(so, nam)
663 struct sockaddr **nam;
667 struct sockaddr_in *sin;
670 * Do the malloc first in case it blocks.
672 MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
674 sin->sin_family = AF_INET;
675 sin->sin_len = sizeof *sin;
684 sin->sin_port = inp->inp_lport;
685 sin->sin_addr = inp->inp_laddr;
688 *nam = (struct sockaddr *)sin;
693 in_setpeeraddr(so, nam)
695 struct sockaddr **nam;
699 struct sockaddr_in *sin;
702 * Do the malloc first in case it blocks.
704 MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
706 sin->sin_family = AF_INET;
707 sin->sin_len = sizeof *sin;
716 sin->sin_port = inp->inp_fport;
717 sin->sin_addr = inp->inp_faddr;
720 *nam = (struct sockaddr *)sin;
725 in_pcbnotifyall(head, faddr, errno, notify)
726 struct inpcbhead *head;
727 struct in_addr faddr;
728 void (*notify) (struct inpcb *, int);
730 struct inpcb *inp, *ninp;
734 * note: if INP_PLACEMARKER is set we must ignore the rest of
735 * the structure and skip it.
738 for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) {
739 ninp = LIST_NEXT(inp, inp_list);
740 if (inp->inp_flags & INP_PLACEMARKER)
743 if (!(inp->inp_vflag & INP_IPV4))
746 if (inp->inp_faddr.s_addr != faddr.s_addr ||
747 inp->inp_socket == NULL)
749 (*notify)(inp, errno);
755 in_pcbpurgeif0(head, ifp)
760 struct ip_moptions *imo;
763 for (inp = head; inp != NULL; inp = LIST_NEXT(inp, inp_list)) {
764 if (inp->inp_flags & INP_PLACEMARKER)
766 imo = inp->inp_moptions;
767 if ((inp->inp_vflag & INP_IPV4) && imo != NULL) {
769 * Unselect the outgoing interface if it is being
772 if (imo->imo_multicast_ifp == ifp)
773 imo->imo_multicast_ifp = NULL;
776 * Drop multicast group membership if we joined
777 * through the interface being detached.
779 for (i = 0, gap = 0; i < imo->imo_num_memberships;
781 if (imo->imo_membership[i]->inm_ifp == ifp) {
782 in_delmulti(imo->imo_membership[i]);
785 imo->imo_membership[i - gap] =
786 imo->imo_membership[i];
788 imo->imo_num_memberships -= gap;
794 * Check for alternatives when higher level complains
795 * about service problems. For now, invalidate cached
796 * routing information. If the route was created dynamically
797 * (by a redirect), time to try a default gateway again.
804 struct rt_addrinfo info;
806 if ((rt = inp->inp_route.ro_rt)) {
807 bzero((caddr_t)&info, sizeof info);
808 info.rti_flags = rt->rt_flags;
809 info.rti_info[RTAX_DST] = rt_key(rt);
810 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
811 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
812 rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
813 if (rt->rt_flags & RTF_DYNAMIC)
814 (void) rtrequest1(RTM_DELETE, &info, NULL);
815 inp->inp_route.ro_rt = NULL;
818 * A new route can be allocated
819 * the next time output is attempted.
825 * After a routing change, flush old routing
826 * and allocate a (hopefully) better one.
829 in_rtchange(inp, errno)
833 if (inp->inp_route.ro_rt) {
834 rtfree(inp->inp_route.ro_rt);
835 inp->inp_route.ro_rt = 0;
837 * A new route can be allocated the next time
838 * output is attempted.
844 * Lookup a PCB based on the local address and port.
847 in_pcblookup_local(pcbinfo, laddr, lport_arg, wild_okay)
848 struct inpcbinfo *pcbinfo;
849 struct in_addr laddr;
854 int matchwild = 3, wildcard;
855 u_short lport = lport_arg;
857 struct inpcbporthead *porthash;
858 struct inpcbport *phd;
859 struct inpcb *match = NULL;
862 * Best fit PCB lookup.
864 * First see if this local port is in use by looking on the
867 porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport,
868 pcbinfo->porthashmask)];
869 LIST_FOREACH(phd, porthash, phd_hash) {
870 if (phd->phd_port == lport)
875 * Port is in use by one or more PCBs. Look for best
878 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
881 if ((inp->inp_vflag & INP_IPV4) == 0)
884 if (inp->inp_faddr.s_addr != INADDR_ANY)
886 if (inp->inp_laddr.s_addr != INADDR_ANY) {
887 if (laddr.s_addr == INADDR_ANY)
889 else if (inp->inp_laddr.s_addr != laddr.s_addr)
892 if (laddr.s_addr != INADDR_ANY)
895 if (wildcard && !wild_okay)
897 if (wildcard < matchwild) {
899 matchwild = wildcard;
900 if (matchwild == 0) {
910 * Lookup PCB in hash list.
913 in_pcblookup_hash(pcbinfo, faddr, fport_arg, laddr, lport_arg, wildcard, ifp)
914 struct inpcbinfo *pcbinfo;
915 struct in_addr faddr, laddr;
916 u_int fport_arg, lport_arg;
920 struct inpcbhead *head;
922 u_short fport = fport_arg, lport = lport_arg;
925 * First look for an exact match.
927 head = &pcbinfo->hashbase[INP_PCBCONNHASH(faddr.s_addr, fport,
928 laddr.s_addr, lport, pcbinfo->hashmask)];
929 LIST_FOREACH(inp, head, inp_hash) {
931 if (!(inp->inp_vflag & INP_IPV4))
934 if (in_hosteq(inp->inp_faddr, faddr) &&
935 in_hosteq(inp->inp_laddr, laddr) &&
936 inp->inp_fport == fport && inp->inp_lport == lport) {
943 struct inpcb *local_wild = NULL;
945 struct inpcb *local_wild_mapped = NULL;
947 struct inpcontainer *ic;
948 struct inpcontainerhead *chead;
950 chead = &pcbinfo->wildcardhashbase[
951 INP_PCBWILDCARDHASH(lport, pcbinfo->wildcardhashmask)];
952 LIST_FOREACH(ic, chead, ic_list) {
955 if (!(inp->inp_vflag & INP_IPV4))
958 if (inp->inp_lport == lport) {
959 if (ifp && ifp->if_type == IFT_FAITH &&
960 !(inp->inp_flags & INP_FAITH))
962 if (inp->inp_laddr.s_addr == laddr.s_addr)
964 if (inp->inp_laddr.s_addr == INADDR_ANY) {
966 if (INP_CHECK_SOCKAF(inp->inp_socket,
968 local_wild_mapped = inp;
976 if (local_wild == NULL)
977 return (local_wild_mapped);
989 * Insert PCB into connection hash table.
992 in_pcbinsconnhash(struct inpcb *inp)
994 struct inpcbinfo *pcbinfo = inp->inp_cpcbinfo;
995 struct inpcbhead *bucket;
996 u_int32_t hashkey_faddr, hashkey_laddr;
999 if (inp->inp_vflag & INP_IPV6) {
1000 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX JH */;
1001 hashkey_laddr = inp->in6p_laddr.s6_addr32[3] /* XXX JH */;
1004 hashkey_faddr = inp->inp_faddr.s_addr;
1005 hashkey_laddr = inp->inp_laddr.s_addr;
1010 KASSERT(!(inp->inp_flags & INP_CONNECTED), ("already on hash list"));
1011 inp->inp_flags |= INP_CONNECTED;
1014 * Insert into the connection hash table.
1016 bucket = &pcbinfo->hashbase[INP_PCBCONNHASH(hashkey_faddr,
1017 inp->inp_fport, hashkey_laddr, inp->inp_lport, pcbinfo->hashmask)];
1018 LIST_INSERT_HEAD(bucket, inp, inp_hash);
1022 * Remove PCB from connection hash table.
1025 in_pcbremconnhash(struct inpcb *inp)
1027 KASSERT(inp->inp_flags & INP_CONNECTED, ("inp not connected"));
1028 LIST_REMOVE(inp, inp_hash);
1029 inp->inp_flags &= ~INP_CONNECTED;
1033 * Insert PCB into port hash table.
1036 in_pcbinsporthash(struct inpcb *inp)
1038 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1039 struct inpcbporthead *pcbporthash;
1040 struct inpcbport *phd;
1043 * Insert into the port hash table.
1045 pcbporthash = &pcbinfo->porthashbase[
1046 INP_PCBPORTHASH(inp->inp_lport, pcbinfo->porthashmask)];
1048 /* Go through port list and look for a head for this lport. */
1049 LIST_FOREACH(phd, pcbporthash, phd_hash)
1050 if (phd->phd_port == inp->inp_lport)
1053 /* If none exists, malloc one and tack it on. */
1055 MALLOC(phd, struct inpcbport *, sizeof(struct inpcbport),
1056 M_PCB, M_INTWAIT | M_NULLOK);
1058 return (ENOBUFS); /* XXX */
1059 phd->phd_port = inp->inp_lport;
1060 LIST_INIT(&phd->phd_pcblist);
1061 LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
1065 LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
1071 in_pcbinswildcardhash_oncpu(struct inpcb *inp, struct inpcbinfo *pcbinfo)
1073 struct inpcontainer *ic;
1074 struct inpcontainerhead *bucket;
1076 bucket = &pcbinfo->wildcardhashbase[
1077 INP_PCBWILDCARDHASH(inp->inp_lport, pcbinfo->wildcardhashmask)];
1079 ic = malloc(sizeof(struct inpcontainer), M_TEMP, M_INTWAIT);
1081 LIST_INSERT_HEAD(bucket, ic, ic_list);
1085 * Insert PCB into wildcard hash table.
1088 in_pcbinswildcardhash(struct inpcb *inp)
1090 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1092 KKASSERT(pcbinfo != NULL);
1094 in_pcbinswildcardhash_oncpu(inp, pcbinfo);
1095 inp->inp_flags |= INP_WILDCARD;
1099 in_pcbremwildcardhash_oncpu(struct inpcb *inp, struct inpcbinfo *pcbinfo)
1101 struct inpcontainer *ic;
1102 struct inpcontainerhead *head;
1105 head = &pcbinfo->wildcardhashbase[
1106 INP_PCBWILDCARDHASH(inp->inp_lport, pcbinfo->wildcardhashmask)];
1108 LIST_FOREACH(ic, head, ic_list) {
1109 if (ic->ic_inp == inp)
1112 return; /* not found! */
1115 LIST_REMOVE(ic, ic_list); /* remove container from bucket chain */
1116 free(ic, M_TEMP); /* deallocate container */
1120 * Remove PCB from wildcard hash table.
1123 in_pcbremwildcardhash(struct inpcb *inp)
1125 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1127 KASSERT(inp->inp_flags & INP_WILDCARD, ("inp not wildcard"));
1128 in_pcbremwildcardhash_oncpu(inp, pcbinfo);
1129 inp->inp_flags &= ~INP_WILDCARD;
1133 * Remove PCB from various lists.
1139 if (inp->inp_lport) {
1140 struct inpcbport *phd = inp->inp_phd;
1142 LIST_REMOVE(inp, inp_portlist);
1143 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1144 LIST_REMOVE(phd, phd_hash);
1148 if (inp->inp_flags & INP_WILDCARD) {
1149 in_pcbremwildcardhash(inp);
1150 } else if (inp->inp_flags & INP_CONNECTED) {
1151 in_pcbremconnhash(inp);
1153 LIST_REMOVE(inp, inp_list);
1154 inp->inp_pcbinfo->ipi_count--;
1158 prison_xinpcb(struct thread *td, struct inpcb *inp)
1162 if (td->td_proc == NULL)
1164 cr = td->td_proc->p_ucred;
1165 if (cr->cr_prison == NULL)
1167 if (ntohl(inp->inp_laddr.s_addr) == cr->cr_prison->pr_ip)