2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3 * Copyright (c) 2010-2011 Juniper Networks, Inc.
6 * Portions of this software were developed by Robert N. M. Watson under
7 * contract to Juniper Networks, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * $KAME: in6_pcb.c,v 1.31 2001/05/21 05:45:10 jinmei Exp $
37 * Copyright (c) 1982, 1986, 1991, 1993
38 * The Regents of the University of California. All rights reserved.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 4. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * @(#)in_pcb.c 8.2 (Berkeley) 1/4/94
67 #include <sys/cdefs.h>
68 __FBSDID("$FreeBSD$");
71 #include "opt_inet6.h"
72 #include "opt_ipsec.h"
73 #include "opt_pcbgroup.h"
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/malloc.h>
79 #include <sys/domain.h>
80 #include <sys/protosw.h>
81 #include <sys/socket.h>
82 #include <sys/socketvar.h>
83 #include <sys/sockio.h>
84 #include <sys/errno.h>
93 #include <net/if_types.h>
94 #include <net/route.h>
96 #include <netinet/in.h>
97 #include <netinet/in_var.h>
98 #include <netinet/in_systm.h>
99 #include <netinet/tcp_var.h>
100 #include <netinet/ip6.h>
101 #include <netinet/ip_var.h>
103 #include <netinet6/ip6_var.h>
104 #include <netinet6/nd6.h>
105 #include <netinet/in_pcb.h>
106 #include <netinet6/in6_pcb.h>
107 #include <netinet6/scope6_var.h>
110 in6_pcbbind(register struct inpcb *inp, struct sockaddr *nam,
113 struct socket *so = inp->inp_socket;
114 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL;
115 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
117 int error, lookupflags = 0;
118 int reuseport = (so->so_options & SO_REUSEPORT);
120 INP_WLOCK_ASSERT(inp);
121 INP_HASH_WLOCK_ASSERT(pcbinfo);
123 if (TAILQ_EMPTY(&V_in6_ifaddrhead)) /* XXX broken! */
124 return (EADDRNOTAVAIL);
125 if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
127 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
128 lookupflags = INPLOOKUP_WILDCARD;
130 if ((error = prison_local_ip6(cred, &inp->in6p_laddr,
131 ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0)
134 sin6 = (struct sockaddr_in6 *)nam;
135 if (nam->sa_len != sizeof(*sin6))
140 if (nam->sa_family != AF_INET6)
141 return (EAFNOSUPPORT);
143 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
146 if ((error = prison_local_ip6(cred, &sin6->sin6_addr,
147 ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0)
150 lport = sin6->sin6_port;
151 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
153 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
154 * allow compepte duplication of binding if
155 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
156 * and a multicast address is bound on both
157 * new and duplicated sockets.
159 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) != 0)
160 reuseport = SO_REUSEADDR|SO_REUSEPORT;
161 } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
164 sin6->sin6_port = 0; /* yech... */
165 if ((ifa = ifa_ifwithaddr((struct sockaddr *)sin6)) ==
167 (inp->inp_flags & INP_BINDANY) == 0) {
168 return (EADDRNOTAVAIL);
172 * XXX: bind to an anycast address might accidentally
173 * cause sending a packet with anycast source address.
174 * We should allow to bind to a deprecated address, since
175 * the application dares to use it.
178 ((struct in6_ifaddr *)ifa)->ia6_flags &
179 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) {
181 return (EADDRNOTAVAIL);
191 if (ntohs(lport) <= V_ipport_reservedhigh &&
192 ntohs(lport) >= V_ipport_reservedlow &&
193 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT,
196 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) &&
197 priv_check_cred(inp->inp_cred,
198 PRIV_NETINET_REUSEPORT, 0) != 0) {
199 t = in6_pcblookup_local(pcbinfo,
200 &sin6->sin6_addr, lport,
201 INPLOOKUP_WILDCARD, cred);
203 ((t->inp_flags & INP_TIMEWAIT) == 0) &&
204 (so->so_type != SOCK_STREAM ||
205 IN6_IS_ADDR_UNSPECIFIED(&t->in6p_faddr)) &&
206 (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
207 !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) ||
208 (t->inp_flags2 & INP_REUSEPORT) == 0) &&
209 (inp->inp_cred->cr_uid !=
210 t->inp_cred->cr_uid))
213 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
214 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
215 struct sockaddr_in sin;
217 in6_sin6_2_sin(&sin, sin6);
218 t = in_pcblookup_local(pcbinfo,
220 INPLOOKUP_WILDCARD, cred);
223 INP_TIMEWAIT) == 0) &&
224 (so->so_type != SOCK_STREAM ||
225 ntohl(t->inp_faddr.s_addr) ==
227 (inp->inp_cred->cr_uid !=
228 t->inp_cred->cr_uid))
233 t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr,
234 lport, lookupflags, cred);
235 if (t && (t->inp_flags & INP_TIMEWAIT)) {
237 * XXXRW: If an incpb has had its timewait
238 * state recycled, we treat the address as
239 * being in use (for now). This is better
240 * than a panic, but not desirable.
244 (reuseport & tw->tw_so_options) == 0)
246 } else if (t && (reuseport & inp_so_options(t)) == 0) {
250 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
251 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
252 struct sockaddr_in sin;
254 in6_sin6_2_sin(&sin, sin6);
255 t = in_pcblookup_local(pcbinfo, sin.sin_addr,
256 lport, lookupflags, cred);
257 if (t && t->inp_flags & INP_TIMEWAIT) {
261 if ((reuseport & tw->tw_so_options) == 0
262 && (ntohl(t->inp_laddr.s_addr) !=
263 INADDR_ANY || ((inp->inp_vflag &
265 (t->inp_vflag & INP_IPV6PROTO))))
268 (reuseport & inp_so_options(t)) == 0 &&
269 (ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
270 (t->inp_vflag & INP_IPV6PROTO) != 0))
275 inp->in6p_laddr = sin6->sin6_addr;
278 if ((error = in6_pcbsetport(&inp->in6p_laddr, inp, cred)) != 0) {
279 /* Undo an address bind that may have occurred. */
280 inp->in6p_laddr = in6addr_any;
284 inp->inp_lport = lport;
285 if (in_pcbinshash(inp) != 0) {
286 inp->in6p_laddr = in6addr_any;
295 * Transform old in6_pcbconnect() into an inner subroutine for new
296 * in6_pcbconnect(): Do some validity-checking on the remote
297 * address (in mbuf 'nam') and then determine local host address
298 * (i.e., which interface) to use to access that remote host.
300 * This preserves definition of in6_pcbconnect(), while supporting a
301 * slightly different version for T/TCP. (This is more than
302 * a bit of a kludge, but cleaning up the internal interfaces would
303 * have forced minor changes in every protocol).
306 in6_pcbladdr(register struct inpcb *inp, struct sockaddr *nam,
307 struct in6_addr *plocal_addr6)
309 register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
311 struct ifnet *ifp = NULL;
312 int scope_ambiguous = 0;
313 struct in6_addr in6a;
315 INP_WLOCK_ASSERT(inp);
316 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); /* XXXRW: why? */
318 if (nam->sa_len != sizeof (*sin6))
320 if (sin6->sin6_family != AF_INET6)
321 return (EAFNOSUPPORT);
322 if (sin6->sin6_port == 0)
323 return (EADDRNOTAVAIL);
325 if (sin6->sin6_scope_id == 0 && !V_ip6_use_defzone)
327 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
330 if (!TAILQ_EMPTY(&V_in6_ifaddrhead)) {
332 * If the destination address is UNSPECIFIED addr,
333 * use the loopback addr, e.g ::1.
335 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
336 sin6->sin6_addr = in6addr_loopback;
338 if ((error = prison_remote_ip6(inp->inp_cred, &sin6->sin6_addr)) != 0)
341 error = in6_selectsrc(sin6, inp->in6p_outputopts,
342 inp, NULL, inp->inp_cred, &ifp, &in6a);
346 if (ifp && scope_ambiguous &&
347 (error = in6_setscope(&sin6->sin6_addr, ifp, NULL)) != 0) {
352 * Do not update this earlier, in case we return with an error.
354 * XXX: this in6_selectsrc result might replace the bound local
355 * address with the address specified by setsockopt(IPV6_PKTINFO).
356 * Is it the intended behavior?
358 *plocal_addr6 = in6a;
361 * Don't do pcblookup call here; return interface in
363 * and exit to caller, that will do the lookup.
371 * Connect from a socket to a specified address.
372 * Both address and port must be specified in argument sin.
373 * If don't have a local address for this socket yet,
377 in6_pcbconnect_mbuf(register struct inpcb *inp, struct sockaddr *nam,
378 struct ucred *cred, struct mbuf *m)
380 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
381 register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
382 struct in6_addr addr6;
385 INP_WLOCK_ASSERT(inp);
386 INP_HASH_WLOCK_ASSERT(pcbinfo);
389 * Call inner routine, to assign local interface address.
390 * in6_pcbladdr() may automatically fill in sin6_scope_id.
392 if ((error = in6_pcbladdr(inp, nam, &addr6)) != 0)
395 if (in6_pcblookup_hash_locked(pcbinfo, &sin6->sin6_addr,
397 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
398 ? &addr6 : &inp->in6p_laddr,
399 inp->inp_lport, 0, NULL) != NULL) {
402 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
403 if (inp->inp_lport == 0) {
404 error = in6_pcbbind(inp, (struct sockaddr *)0, cred);
408 inp->in6p_laddr = addr6;
410 inp->in6p_faddr = sin6->sin6_addr;
411 inp->inp_fport = sin6->sin6_port;
412 /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
413 inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
414 if (inp->inp_flags & IN6P_AUTOFLOWLABEL)
416 (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
418 in_pcbrehash_mbuf(inp, m);
424 in6_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
427 return (in6_pcbconnect_mbuf(inp, nam, cred, NULL));
431 in6_pcbdisconnect(struct inpcb *inp)
434 INP_WLOCK_ASSERT(inp);
435 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
437 bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr));
439 /* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
440 inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
445 in6_sockaddr(in_port_t port, struct in6_addr *addr_p)
447 struct sockaddr_in6 *sin6;
449 sin6 = malloc(sizeof *sin6, M_SONAME, M_WAITOK);
450 bzero(sin6, sizeof *sin6);
451 sin6->sin6_family = AF_INET6;
452 sin6->sin6_len = sizeof(*sin6);
453 sin6->sin6_port = port;
454 sin6->sin6_addr = *addr_p;
455 (void)sa6_recoverscope(sin6); /* XXX: should catch errors */
457 return (struct sockaddr *)sin6;
461 in6_v4mapsin6_sockaddr(in_port_t port, struct in_addr *addr_p)
463 struct sockaddr_in sin;
464 struct sockaddr_in6 *sin6_p;
466 bzero(&sin, sizeof sin);
467 sin.sin_family = AF_INET;
468 sin.sin_len = sizeof(sin);
470 sin.sin_addr = *addr_p;
472 sin6_p = malloc(sizeof *sin6_p, M_SONAME,
474 in6_sin_2_v4mapsin6(&sin, sin6_p);
476 return (struct sockaddr *)sin6_p;
480 in6_getsockaddr(struct socket *so, struct sockaddr **nam)
482 register struct inpcb *inp;
483 struct in6_addr addr;
487 KASSERT(inp != NULL, ("in6_getsockaddr: inp == NULL"));
490 port = inp->inp_lport;
491 addr = inp->in6p_laddr;
494 *nam = in6_sockaddr(port, &addr);
499 in6_getpeeraddr(struct socket *so, struct sockaddr **nam)
502 struct in6_addr addr;
506 KASSERT(inp != NULL, ("in6_getpeeraddr: inp == NULL"));
509 port = inp->inp_fport;
510 addr = inp->in6p_faddr;
513 *nam = in6_sockaddr(port, &addr);
518 in6_mapped_sockaddr(struct socket *so, struct sockaddr **nam)
524 KASSERT(inp != NULL, ("in6_mapped_sockaddr: inp == NULL"));
527 if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
528 error = in_getsockaddr(so, nam);
530 in6_sin_2_v4mapsin6_in_sock(nam);
534 /* scope issues will be handled in in6_getsockaddr(). */
535 error = in6_getsockaddr(so, nam);
542 in6_mapped_peeraddr(struct socket *so, struct sockaddr **nam)
548 KASSERT(inp != NULL, ("in6_mapped_peeraddr: inp == NULL"));
551 if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
552 error = in_getpeeraddr(so, nam);
554 in6_sin_2_v4mapsin6_in_sock(nam);
557 /* scope issues will be handled in in6_getpeeraddr(). */
558 error = in6_getpeeraddr(so, nam);
564 * Pass some notification to all connections of a protocol
565 * associated with address dst. The local address and/or port numbers
566 * may be specified to limit the search. The "usual action" will be
567 * taken, depending on the ctlinput cmd. The caller must filter any
568 * cmds that are uninteresting (e.g., no error in the map).
569 * Call the protocol specific routine (if any) to report
570 * any errors for each matching socket.
573 in6_pcbnotify(struct inpcbinfo *pcbinfo, struct sockaddr *dst,
574 u_int fport_arg, const struct sockaddr *src, u_int lport_arg,
575 int cmd, void *cmdarg,
576 struct inpcb *(*notify)(struct inpcb *, int))
578 struct inpcb *inp, *inp_temp;
579 struct sockaddr_in6 sa6_src, *sa6_dst;
580 u_short fport = fport_arg, lport = lport_arg;
584 if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6)
587 sa6_dst = (struct sockaddr_in6 *)dst;
588 if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
592 * note that src can be NULL when we get notify by local fragmentation.
594 sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
595 flowinfo = sa6_src.sin6_flowinfo;
598 * Redirects go to all references to the destination,
599 * and use in6_rtchange to invalidate the route cache.
600 * Dead host indications: also use in6_rtchange to invalidate
601 * the cache, and deliver the error to all the sockets.
602 * Otherwise, if we have knowledge of the local port and address,
603 * deliver only to that socket.
605 if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
608 bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
610 if (cmd != PRC_HOSTDEAD)
611 notify = in6_rtchange;
613 errno = inet6ctlerrmap[cmd];
614 INP_INFO_WLOCK(pcbinfo);
615 LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) {
617 if ((inp->inp_vflag & INP_IPV6) == 0) {
623 * If the error designates a new path MTU for a destination
624 * and the application (associated with this socket) wanted to
625 * know the value, notify. Note that we notify for all
626 * disconnected sockets if the corresponding application
627 * wanted. This is because some UDP applications keep sending
628 * sockets disconnected.
629 * XXX: should we avoid to notify the value to TCP sockets?
631 if (cmd == PRC_MSGSIZE && (inp->inp_flags & IN6P_MTU) != 0 &&
632 (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
633 IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &sa6_dst->sin6_addr))) {
634 ip6_notify_pmtu(inp, (struct sockaddr_in6 *)dst,
635 (u_int32_t *)cmdarg);
639 * Detect if we should notify the error. If no source and
640 * destination ports are specifed, but non-zero flowinfo and
641 * local address match, notify the error. This is the case
642 * when the error is delivered with an encrypted buffer
643 * by ESP. Otherwise, just compare addresses and ports
646 if (lport == 0 && fport == 0 && flowinfo &&
647 inp->inp_socket != NULL &&
648 flowinfo == (inp->inp_flow & IPV6_FLOWLABEL_MASK) &&
649 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
651 else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
652 &sa6_dst->sin6_addr) ||
653 inp->inp_socket == 0 ||
654 (lport && inp->inp_lport != lport) ||
655 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
656 !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
657 &sa6_src.sin6_addr)) ||
658 (fport && inp->inp_fport != fport)) {
665 if ((*notify)(inp, errno))
670 INP_INFO_WUNLOCK(pcbinfo);
674 * Lookup a PCB based on the local address and port. Caller must hold the
675 * hash lock. No inpcb locks or references are acquired.
678 in6_pcblookup_local(struct inpcbinfo *pcbinfo, struct in6_addr *laddr,
679 u_short lport, int lookupflags, struct ucred *cred)
681 register struct inpcb *inp;
682 int matchwild = 3, wildcard;
684 KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
685 ("%s: invalid lookup flags %d", __func__, lookupflags));
687 INP_HASH_WLOCK_ASSERT(pcbinfo);
689 if ((lookupflags & INPLOOKUP_WILDCARD) == 0) {
690 struct inpcbhead *head;
692 * Look for an unconnected (wildcard foreign addr) PCB that
693 * matches the local address and port we're looking for.
695 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
696 0, pcbinfo->ipi_hashmask)];
697 LIST_FOREACH(inp, head, inp_hash) {
698 /* XXX inp locking */
699 if ((inp->inp_vflag & INP_IPV6) == 0)
701 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
702 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
703 inp->inp_lport == lport) {
706 prison_equal_ip6(cred->cr_prison,
707 inp->inp_cred->cr_prison))
716 struct inpcbporthead *porthash;
717 struct inpcbport *phd;
718 struct inpcb *match = NULL;
720 * Best fit PCB lookup.
722 * First see if this local port is in use by looking on the
725 porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
726 pcbinfo->ipi_porthashmask)];
727 LIST_FOREACH(phd, porthash, phd_hash) {
728 if (phd->phd_port == lport)
733 * Port is in use by one or more PCBs. Look for best
736 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
739 !prison_equal_ip6(cred->cr_prison,
740 inp->inp_cred->cr_prison))
742 /* XXX inp locking */
743 if ((inp->inp_vflag & INP_IPV6) == 0)
745 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
747 if (!IN6_IS_ADDR_UNSPECIFIED(
749 if (IN6_IS_ADDR_UNSPECIFIED(laddr))
751 else if (!IN6_ARE_ADDR_EQUAL(
752 &inp->in6p_laddr, laddr))
755 if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
758 if (wildcard < matchwild) {
760 matchwild = wildcard;
771 in6_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
774 struct ip6_moptions *im6o;
777 INP_INFO_RLOCK(pcbinfo);
778 LIST_FOREACH(in6p, pcbinfo->ipi_listhead, inp_list) {
780 im6o = in6p->in6p_moptions;
781 if ((in6p->inp_vflag & INP_IPV6) && im6o != NULL) {
783 * Unselect the outgoing ifp for multicast if it
786 if (im6o->im6o_multicast_ifp == ifp)
787 im6o->im6o_multicast_ifp = NULL;
789 * Drop multicast group membership if we joined
790 * through the interface being detached.
793 for (i = 0; i < im6o->im6o_num_memberships; i++) {
794 if (im6o->im6o_membership[i]->in6m_ifp ==
796 in6_mc_leave(im6o->im6o_membership[i],
799 } else if (gap != 0) {
800 im6o->im6o_membership[i - gap] =
801 im6o->im6o_membership[i];
804 im6o->im6o_num_memberships -= gap;
808 INP_INFO_RUNLOCK(pcbinfo);
812 * Check for alternatives when higher level complains
813 * about service problems. For now, invalidate cached
814 * routing information. If the route was created dynamically
815 * (by a redirect), time to try a default gateway again.
818 in6_losing(struct inpcb *in6p)
822 * We don't store route pointers in the routing table anymore
828 * After a routing change, flush old routing
829 * and allocate a (hopefully) better one.
832 in6_rtchange(struct inpcb *inp, int errno)
835 * We don't store route pointers in the routing table anymore
842 * Lookup PCB in hash list, using pcbgroup tables.
844 static struct inpcb *
845 in6_pcblookup_group(struct inpcbinfo *pcbinfo, struct inpcbgroup *pcbgroup,
846 struct in6_addr *faddr, u_int fport_arg, struct in6_addr *laddr,
847 u_int lport_arg, int lookupflags, struct ifnet *ifp)
849 struct inpcbhead *head;
850 struct inpcb *inp, *tmpinp;
851 u_short fport = fport_arg, lport = lport_arg;
854 if (faithprefix_p != NULL)
855 faith = (*faithprefix_p)(laddr);
860 * First look for an exact match.
863 INP_GROUP_LOCK(pcbgroup);
864 head = &pcbgroup->ipg_hashbase[
865 INP_PCBHASH(faddr->s6_addr32[3] /* XXX */, lport, fport,
866 pcbgroup->ipg_hashmask)];
867 LIST_FOREACH(inp, head, inp_pcbgrouphash) {
868 /* XXX inp locking */
869 if ((inp->inp_vflag & INP_IPV6) == 0)
871 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
872 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
873 inp->inp_fport == fport &&
874 inp->inp_lport == lport) {
876 * XXX We should be able to directly return
877 * the inp here, without any checks.
878 * Well unless both bound with SO_REUSEPORT?
880 if (prison_flag(inp->inp_cred, PR_IP6))
886 if (tmpinp != NULL) {
892 * Then look for a wildcard match, if requested.
894 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
895 struct inpcb *local_wild = NULL, *local_exact = NULL;
896 struct inpcb *jail_wild = NULL;
900 * Order of socket selection - we always prefer jails.
901 * 1. jailed, non-wild.
903 * 3. non-jailed, non-wild.
904 * 4. non-jailed, wild.
906 head = &pcbinfo->ipi_wildbase[INP_PCBHASH(INADDR_ANY, lport,
907 0, pcbinfo->ipi_wildmask)];
908 LIST_FOREACH(inp, head, inp_pcbgroup_wild) {
909 /* XXX inp locking */
910 if ((inp->inp_vflag & INP_IPV6) == 0)
913 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
914 inp->inp_lport != lport) {
918 /* XXX inp locking */
919 if (faith && (inp->inp_flags & INP_FAITH) == 0)
922 injail = prison_flag(inp->inp_cred, PR_IP6);
924 if (prison_check_ip6(inp->inp_cred,
928 if (local_exact != NULL)
932 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) {
937 } else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
954 } /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */
955 INP_GROUP_UNLOCK(pcbgroup);
960 INP_GROUP_UNLOCK(pcbgroup);
961 if (lookupflags & INPLOOKUP_WLOCKPCB) {
963 if (in_pcbrele_wlocked(inp))
965 } else if (lookupflags & INPLOOKUP_RLOCKPCB) {
967 if (in_pcbrele_rlocked(inp))
970 panic("%s: locking buf", __func__);
973 #endif /* PCBGROUP */
976 * Lookup PCB in hash list.
979 in6_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
980 u_int fport_arg, struct in6_addr *laddr, u_int lport_arg,
981 int lookupflags, struct ifnet *ifp)
983 struct inpcbhead *head;
984 struct inpcb *inp, *tmpinp;
985 u_short fport = fport_arg, lport = lport_arg;
988 KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
989 ("%s: invalid lookup flags %d", __func__, lookupflags));
991 INP_HASH_LOCK_ASSERT(pcbinfo);
993 if (faithprefix_p != NULL)
994 faith = (*faithprefix_p)(laddr);
999 * First look for an exact match.
1002 head = &pcbinfo->ipi_hashbase[
1003 INP_PCBHASH(faddr->s6_addr32[3] /* XXX */, lport, fport,
1004 pcbinfo->ipi_hashmask)];
1005 LIST_FOREACH(inp, head, inp_hash) {
1006 /* XXX inp locking */
1007 if ((inp->inp_vflag & INP_IPV6) == 0)
1009 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
1010 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
1011 inp->inp_fport == fport &&
1012 inp->inp_lport == lport) {
1014 * XXX We should be able to directly return
1015 * the inp here, without any checks.
1016 * Well unless both bound with SO_REUSEPORT?
1018 if (prison_flag(inp->inp_cred, PR_IP6))
1028 * Then look for a wildcard match, if requested.
1030 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1031 struct inpcb *local_wild = NULL, *local_exact = NULL;
1032 struct inpcb *jail_wild = NULL;
1036 * Order of socket selection - we always prefer jails.
1037 * 1. jailed, non-wild.
1039 * 3. non-jailed, non-wild.
1040 * 4. non-jailed, wild.
1042 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
1043 0, pcbinfo->ipi_hashmask)];
1044 LIST_FOREACH(inp, head, inp_hash) {
1045 /* XXX inp locking */
1046 if ((inp->inp_vflag & INP_IPV6) == 0)
1049 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
1050 inp->inp_lport != lport) {
1054 /* XXX inp locking */
1055 if (faith && (inp->inp_flags & INP_FAITH) == 0)
1058 injail = prison_flag(inp->inp_cred, PR_IP6);
1060 if (prison_check_ip6(inp->inp_cred,
1064 if (local_exact != NULL)
1068 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) {
1073 } else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1079 } /* LIST_FOREACH */
1081 if (jail_wild != NULL)
1083 if (local_exact != NULL)
1084 return (local_exact);
1085 if (local_wild != NULL)
1086 return (local_wild);
1087 } /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */
1096 * Lookup PCB in hash list, using pcbinfo tables. This variation locks the
1097 * hash list lock, and will return the inpcb locked (i.e., requires
1098 * INPLOOKUP_LOCKPCB).
1100 static struct inpcb *
1101 in6_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
1102 u_int fport, struct in6_addr *laddr, u_int lport, int lookupflags,
1107 INP_HASH_RLOCK(pcbinfo);
1108 inp = in6_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport,
1109 (lookupflags & ~(INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)), ifp);
1112 INP_HASH_RUNLOCK(pcbinfo);
1113 if (lookupflags & INPLOOKUP_WLOCKPCB) {
1115 if (in_pcbrele_wlocked(inp))
1117 } else if (lookupflags & INPLOOKUP_RLOCKPCB) {
1119 if (in_pcbrele_rlocked(inp))
1122 panic("%s: locking bug", __func__);
1124 INP_HASH_RUNLOCK(pcbinfo);
1129 * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf
1130 * from which a pre-calculated hash value may be extracted.
1132 * Possibly more of this logic should be in in6_pcbgroup.c.
1135 in6_pcblookup(struct inpcbinfo *pcbinfo, struct in6_addr *faddr, u_int fport,
1136 struct in6_addr *laddr, u_int lport, int lookupflags, struct ifnet *ifp)
1138 #if defined(PCBGROUP)
1139 struct inpcbgroup *pcbgroup;
1142 KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
1143 ("%s: invalid lookup flags %d", __func__, lookupflags));
1144 KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1145 ("%s: LOCKPCB not set", __func__));
1147 #if defined(PCBGROUP)
1148 if (in_pcbgroup_enabled(pcbinfo)) {
1149 pcbgroup = in6_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
1151 return (in6_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
1152 laddr, lport, lookupflags, ifp));
1155 return (in6_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
1160 in6_pcblookup_mbuf(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
1161 u_int fport, struct in6_addr *laddr, u_int lport, int lookupflags,
1162 struct ifnet *ifp, struct mbuf *m)
1165 struct inpcbgroup *pcbgroup;
1168 KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
1169 ("%s: invalid lookup flags %d", __func__, lookupflags));
1170 KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1171 ("%s: LOCKPCB not set", __func__));
1174 if (in_pcbgroup_enabled(pcbinfo)) {
1175 pcbgroup = in6_pcbgroup_byhash(pcbinfo, M_HASHTYPE_GET(m),
1176 m->m_pkthdr.flowid);
1177 if (pcbgroup != NULL)
1178 return (in6_pcblookup_group(pcbinfo, pcbgroup, faddr,
1179 fport, laddr, lport, lookupflags, ifp));
1180 pcbgroup = in6_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
1182 return (in6_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
1183 laddr, lport, lookupflags, ifp));
1186 return (in6_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
1191 init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m)
1195 ip = mtod(m, struct ip6_hdr *);
1196 bzero(sin6, sizeof(*sin6));
1197 sin6->sin6_len = sizeof(*sin6);
1198 sin6->sin6_family = AF_INET6;
1199 sin6->sin6_addr = ip->ip6_src;
1201 (void)sa6_recoverscope(sin6); /* XXX: should catch errors... */