2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * Copyright (c) 2010-2011 Juniper Networks, Inc.
8 * Portions of this software were developed by Robert N. M. Watson under
9 * contract to Juniper Networks, Inc.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the project nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * $KAME: in6_pcb.c,v 1.31 2001/05/21 05:45:10 jinmei Exp $
39 * Copyright (c) 1982, 1986, 1991, 1993
40 * The Regents of the University of California. All rights reserved.
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * @(#)in_pcb.c 8.2 (Berkeley) 1/4/94
69 #include <sys/cdefs.h>
70 __FBSDID("$FreeBSD$");
73 #include "opt_inet6.h"
74 #include "opt_ipsec.h"
75 #include "opt_route.h"
79 #include <sys/param.h>
80 #include <sys/systm.h>
81 #include <sys/malloc.h>
83 #include <sys/domain.h>
84 #include <sys/protosw.h>
85 #include <sys/socket.h>
86 #include <sys/socketvar.h>
87 #include <sys/sockio.h>
88 #include <sys/errno.h>
97 #include <net/if_var.h>
98 #include <net/if_llatbl.h>
99 #include <net/if_types.h>
100 #include <net/route.h>
101 #include <net/route/nhop.h>
103 #include <netinet/in.h>
104 #include <netinet/in_var.h>
105 #include <netinet/in_systm.h>
106 #include <netinet/tcp_var.h>
107 #include <netinet/ip6.h>
108 #include <netinet/ip_var.h>
110 #include <netinet6/ip6_var.h>
111 #include <netinet6/nd6.h>
112 #include <netinet/in_pcb.h>
113 #include <netinet/in_pcb_var.h>
114 #include <netinet6/in6_pcb.h>
115 #include <netinet6/in6_fib.h>
116 #include <netinet6/scope6_var.h>
119 in6_pcbsetport(struct in6_addr *laddr, struct inpcb *inp, struct ucred *cred)
121 struct socket *so = inp->inp_socket;
123 int error, lookupflags = 0;
125 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
128 INP_WLOCK_ASSERT(inp);
129 INP_HASH_WLOCK_ASSERT(pcbinfo);
131 error = prison_local_ip6(cred, laddr,
132 ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0));
136 /* XXX: this is redundant when called from in6_pcbbind */
137 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT|SO_REUSEPORT_LB)) == 0)
138 lookupflags = INPLOOKUP_WILDCARD;
140 inp->inp_flags |= INP_ANONPORT;
142 error = in_pcb_lport(inp, NULL, &lport, cred, lookupflags);
146 inp->inp_lport = lport;
147 if (in_pcbinshash(inp) != 0) {
148 inp->in6p_laddr = in6addr_any;
157 in6_pcbbind(struct inpcb *inp, struct sockaddr *nam,
160 struct socket *so = inp->inp_socket;
161 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL;
162 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
164 int error, lookupflags = 0;
165 int reuseport = (so->so_options & SO_REUSEPORT);
168 * XXX: Maybe we could let SO_REUSEPORT_LB set SO_REUSEPORT bit here
169 * so that we don't have to add to the (already messy) code below.
171 int reuseport_lb = (so->so_options & SO_REUSEPORT_LB);
173 INP_WLOCK_ASSERT(inp);
174 INP_HASH_WLOCK_ASSERT(pcbinfo);
176 if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
178 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT|SO_REUSEPORT_LB)) == 0)
179 lookupflags = INPLOOKUP_WILDCARD;
181 if ((error = prison_local_ip6(cred, &inp->in6p_laddr,
182 ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0)
185 sin6 = (struct sockaddr_in6 *)nam;
186 KASSERT(sin6->sin6_family == AF_INET6,
187 ("%s: invalid address family for %p", __func__, sin6));
188 KASSERT(sin6->sin6_len == sizeof(*sin6),
189 ("%s: invalid address length for %p", __func__, sin6));
191 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
194 if ((error = prison_local_ip6(cred, &sin6->sin6_addr,
195 ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0)
198 lport = sin6->sin6_port;
199 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
201 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
202 * allow compepte duplication of binding if
203 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
204 * and a multicast address is bound on both
205 * new and duplicated sockets.
207 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) != 0)
208 reuseport = SO_REUSEADDR|SO_REUSEPORT;
210 * XXX: How to deal with SO_REUSEPORT_LB here?
211 * Treat same as SO_REUSEPORT for now.
213 if ((so->so_options &
214 (SO_REUSEADDR|SO_REUSEPORT_LB)) != 0)
215 reuseport_lb = SO_REUSEADDR|SO_REUSEPORT_LB;
216 } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
217 struct epoch_tracker et;
220 sin6->sin6_port = 0; /* yech... */
222 if ((ifa = ifa_ifwithaddr((struct sockaddr *)sin6)) ==
224 (inp->inp_flags & INP_BINDANY) == 0) {
226 return (EADDRNOTAVAIL);
230 * XXX: bind to an anycast address might accidentally
231 * cause sending a packet with anycast source address.
232 * We should allow to bind to a deprecated address, since
233 * the application dares to use it.
236 ((struct in6_ifaddr *)ifa)->ia6_flags &
237 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) {
239 return (EADDRNOTAVAIL);
248 if (ntohs(lport) <= V_ipport_reservedhigh &&
249 ntohs(lport) >= V_ipport_reservedlow &&
250 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT))
252 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) &&
253 priv_check_cred(inp->inp_cred, PRIV_NETINET_REUSEPORT) != 0) {
254 t = in6_pcblookup_local(pcbinfo,
255 &sin6->sin6_addr, lport,
256 INPLOOKUP_WILDCARD, cred);
258 ((inp->inp_flags2 & INP_BINDMULTI) == 0) &&
259 ((t->inp_flags & INP_TIMEWAIT) == 0) &&
260 (so->so_type != SOCK_STREAM ||
261 IN6_IS_ADDR_UNSPECIFIED(&t->in6p_faddr)) &&
262 (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
263 !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) ||
264 (t->inp_flags2 & INP_REUSEPORT) ||
265 (t->inp_flags2 & INP_REUSEPORT_LB) == 0) &&
266 (inp->inp_cred->cr_uid !=
267 t->inp_cred->cr_uid))
271 * If the socket is a BINDMULTI socket, then
272 * the credentials need to match and the
273 * original socket also has to have been bound
276 if (t && (! in_pcbbind_check_bindmulti(inp, t)))
280 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
281 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
282 struct sockaddr_in sin;
284 in6_sin6_2_sin(&sin, sin6);
285 t = in_pcblookup_local(pcbinfo,
287 INPLOOKUP_WILDCARD, cred);
289 ((inp->inp_flags2 & INP_BINDMULTI) == 0) &&
291 INP_TIMEWAIT) == 0) &&
292 (so->so_type != SOCK_STREAM ||
293 ntohl(t->inp_faddr.s_addr) ==
295 (inp->inp_cred->cr_uid !=
296 t->inp_cred->cr_uid))
299 if (t && (! in_pcbbind_check_bindmulti(inp, t)))
304 t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr,
305 lport, lookupflags, cred);
306 if (t && (t->inp_flags & INP_TIMEWAIT)) {
308 * XXXRW: If an incpb has had its timewait
309 * state recycled, we treat the address as
310 * being in use (for now). This is better
311 * than a panic, but not desirable.
315 ((reuseport & tw->tw_so_options) == 0 &&
316 (reuseport_lb & tw->tw_so_options) == 0))
318 } else if (t && (reuseport & inp_so_options(t)) == 0 &&
319 (reuseport_lb & inp_so_options(t)) == 0) {
323 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
324 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
325 struct sockaddr_in sin;
327 in6_sin6_2_sin(&sin, sin6);
328 t = in_pcblookup_local(pcbinfo, sin.sin_addr,
329 lport, lookupflags, cred);
330 if (t && t->inp_flags & INP_TIMEWAIT) {
334 if ((reuseport & tw->tw_so_options) == 0
335 && (reuseport_lb & tw->tw_so_options) == 0
336 && (ntohl(t->inp_laddr.s_addr) !=
337 INADDR_ANY || ((inp->inp_vflag &
339 (t->inp_vflag & INP_IPV6PROTO))))
342 (reuseport & inp_so_options(t)) == 0 &&
343 (reuseport_lb & inp_so_options(t)) == 0 &&
344 (ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
345 (t->inp_vflag & INP_IPV6PROTO) != 0)) {
351 inp->in6p_laddr = sin6->sin6_addr;
354 if ((error = in6_pcbsetport(&inp->in6p_laddr, inp, cred)) != 0) {
355 /* Undo an address bind that may have occurred. */
356 inp->in6p_laddr = in6addr_any;
360 inp->inp_lport = lport;
361 if (in_pcbinshash(inp) != 0) {
362 inp->in6p_laddr = in6addr_any;
371 * Transform old in6_pcbconnect() into an inner subroutine for new
372 * in6_pcbconnect(): Do some validity-checking on the remote
373 * address (in mbuf 'nam') and then determine local host address
374 * (i.e., which interface) to use to access that remote host.
376 * This preserves definition of in6_pcbconnect(), while supporting a
377 * slightly different version for T/TCP. (This is more than
378 * a bit of a kludge, but cleaning up the internal interfaces would
379 * have forced minor changes in every protocol).
382 in6_pcbladdr(struct inpcb *inp, struct sockaddr_in6 *sin6,
383 struct in6_addr *plocal_addr6)
386 int scope_ambiguous = 0;
387 struct in6_addr in6a;
388 struct epoch_tracker et;
390 INP_WLOCK_ASSERT(inp);
391 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); /* XXXRW: why? */
393 if (sin6->sin6_port == 0)
394 return (EADDRNOTAVAIL);
396 if (sin6->sin6_scope_id == 0 && !V_ip6_use_defzone)
398 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
401 if (!CK_STAILQ_EMPTY(&V_in6_ifaddrhead)) {
403 * If the destination address is UNSPECIFIED addr,
404 * use the loopback addr, e.g ::1.
406 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
407 sin6->sin6_addr = in6addr_loopback;
409 if ((error = prison_remote_ip6(inp->inp_cred, &sin6->sin6_addr)) != 0)
413 error = in6_selectsrc_socket(sin6, inp->in6p_outputopts,
414 inp, inp->inp_cred, scope_ambiguous, &in6a, NULL);
420 * Do not update this earlier, in case we return with an error.
422 * XXX: this in6_selectsrc_socket result might replace the bound local
423 * address with the address specified by setsockopt(IPV6_PKTINFO).
424 * Is it the intended behavior?
426 *plocal_addr6 = in6a;
429 * Don't do pcblookup call here; return interface in
431 * and exit to caller, that will do the lookup.
439 * Connect from a socket to a specified address.
440 * Both address and port must be specified in argument sin.
441 * If don't have a local address for this socket yet,
445 in6_pcbconnect_mbuf(struct inpcb *inp, struct sockaddr *nam,
446 struct ucred *cred, struct mbuf *m, bool rehash)
448 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
449 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
450 struct sockaddr_in6 laddr6;
453 KASSERT(sin6->sin6_family == AF_INET6,
454 ("%s: invalid address family for %p", __func__, sin6));
455 KASSERT(sin6->sin6_len == sizeof(*sin6),
456 ("%s: invalid address length for %p", __func__, sin6));
458 bzero(&laddr6, sizeof(laddr6));
459 laddr6.sin6_family = AF_INET6;
461 INP_WLOCK_ASSERT(inp);
462 INP_HASH_WLOCK_ASSERT(pcbinfo);
465 if (CALC_FLOWID_OUTBOUND) {
466 uint32_t hash_type, hash_val;
468 hash_val = fib6_calc_software_hash(&inp->in6p_laddr,
469 &sin6->sin6_addr, 0, sin6->sin6_port,
470 inp->inp_socket->so_proto->pr_protocol, &hash_type);
471 inp->inp_flowid = hash_val;
472 inp->inp_flowtype = hash_type;
476 * Call inner routine, to assign local interface address.
477 * in6_pcbladdr() may automatically fill in sin6_scope_id.
479 if ((error = in6_pcbladdr(inp, sin6, &laddr6.sin6_addr)) != 0)
482 if (in6_pcblookup_hash_locked(pcbinfo, &sin6->sin6_addr,
484 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
485 ? &laddr6.sin6_addr : &inp->in6p_laddr,
486 inp->inp_lport, 0, NULL, M_NODOM) != NULL) {
489 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
490 if (inp->inp_lport == 0) {
492 * rehash was required to be true in the past for
493 * this case; retain that convention. However,
494 * we now call in_pcb_lport_dest rather than
495 * in6_pcbbind; the former does not insert into
496 * the hash table, the latter does. Change rehash
497 * to false to do the in_pcbinshash below.
499 KASSERT(rehash == true,
500 ("Rehashing required for unbound inps"));
502 error = in_pcb_lport_dest(inp,
503 (struct sockaddr *) &laddr6, &inp->inp_lport,
504 (struct sockaddr *) sin6, sin6->sin6_port, cred,
509 inp->in6p_laddr = laddr6.sin6_addr;
511 inp->in6p_faddr = sin6->sin6_addr;
512 inp->inp_fport = sin6->sin6_port;
513 /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
514 inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
515 if (inp->inp_flags & IN6P_AUTOFLOWLABEL)
517 (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
529 in6_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
532 return (in6_pcbconnect_mbuf(inp, nam, cred, NULL, true));
536 in6_pcbdisconnect(struct inpcb *inp)
539 INP_WLOCK_ASSERT(inp);
540 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
542 bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr));
544 /* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
545 inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
550 in6_sockaddr(in_port_t port, struct in6_addr *addr_p)
552 struct sockaddr_in6 *sin6;
554 sin6 = malloc(sizeof *sin6, M_SONAME, M_WAITOK);
555 bzero(sin6, sizeof *sin6);
556 sin6->sin6_family = AF_INET6;
557 sin6->sin6_len = sizeof(*sin6);
558 sin6->sin6_port = port;
559 sin6->sin6_addr = *addr_p;
560 (void)sa6_recoverscope(sin6); /* XXX: should catch errors */
562 return (struct sockaddr *)sin6;
566 in6_v4mapsin6_sockaddr(in_port_t port, struct in_addr *addr_p)
568 struct sockaddr_in sin;
569 struct sockaddr_in6 *sin6_p;
571 bzero(&sin, sizeof sin);
572 sin.sin_family = AF_INET;
573 sin.sin_len = sizeof(sin);
575 sin.sin_addr = *addr_p;
577 sin6_p = malloc(sizeof *sin6_p, M_SONAME,
579 in6_sin_2_v4mapsin6(&sin, sin6_p);
581 return (struct sockaddr *)sin6_p;
585 in6_getsockaddr(struct socket *so, struct sockaddr **nam)
588 struct in6_addr addr;
592 KASSERT(inp != NULL, ("in6_getsockaddr: inp == NULL"));
595 port = inp->inp_lport;
596 addr = inp->in6p_laddr;
599 *nam = in6_sockaddr(port, &addr);
604 in6_getpeeraddr(struct socket *so, struct sockaddr **nam)
607 struct in6_addr addr;
611 KASSERT(inp != NULL, ("in6_getpeeraddr: inp == NULL"));
614 port = inp->inp_fport;
615 addr = inp->in6p_faddr;
618 *nam = in6_sockaddr(port, &addr);
623 in6_mapped_sockaddr(struct socket *so, struct sockaddr **nam)
629 KASSERT(inp != NULL, ("in6_mapped_sockaddr: inp == NULL"));
632 if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
633 error = in_getsockaddr(so, nam);
635 in6_sin_2_v4mapsin6_in_sock(nam);
639 /* scope issues will be handled in in6_getsockaddr(). */
640 error = in6_getsockaddr(so, nam);
647 in6_mapped_peeraddr(struct socket *so, struct sockaddr **nam)
653 KASSERT(inp != NULL, ("in6_mapped_peeraddr: inp == NULL"));
656 if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
657 error = in_getpeeraddr(so, nam);
659 in6_sin_2_v4mapsin6_in_sock(nam);
662 /* scope issues will be handled in in6_getpeeraddr(). */
663 error = in6_getpeeraddr(so, nam);
669 * Pass some notification to all connections of a protocol
670 * associated with address dst. The local address and/or port numbers
671 * may be specified to limit the search. The "usual action" will be
672 * taken, depending on the ctlinput cmd. The caller must filter any
673 * cmds that are uninteresting (e.g., no error in the map).
674 * Call the protocol specific routine (if any) to report
675 * any errors for each matching socket.
678 inp_match6(const struct inpcb *inp, void *v __unused)
681 return ((inp->inp_vflag & INP_IPV6) != 0);
684 in6_pcbnotify(struct inpcbinfo *pcbinfo, struct sockaddr *dst,
685 u_int fport_arg, const struct sockaddr *src, u_int lport_arg,
686 int cmd, void *cmdarg,
687 struct inpcb *(*notify)(struct inpcb *, int))
689 struct inpcb_iterator inpi = INP_ITERATOR(pcbinfo, INPLOOKUP_WLOCKPCB,
692 struct sockaddr_in6 sa6_src, *sa6_dst;
693 u_short fport = fport_arg, lport = lport_arg;
697 if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6)
700 sa6_dst = (struct sockaddr_in6 *)dst;
701 if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
705 * note that src can be NULL when we get notify by local fragmentation.
707 sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
708 flowinfo = sa6_src.sin6_flowinfo;
711 * Redirects go to all references to the destination,
712 * and use in6_rtchange to invalidate the route cache.
713 * Dead host indications: also use in6_rtchange to invalidate
714 * the cache, and deliver the error to all the sockets.
715 * Otherwise, if we have knowledge of the local port and address,
716 * deliver only to that socket.
718 if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
721 bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
723 if (cmd != PRC_HOSTDEAD)
724 notify = in6_rtchange;
726 errno = inet6ctlerrmap[cmd];
727 while ((inp = inp_next(&inpi)) != NULL) {
728 INP_WLOCK_ASSERT(inp);
730 * If the error designates a new path MTU for a destination
731 * and the application (associated with this socket) wanted to
732 * know the value, notify.
733 * XXX: should we avoid to notify the value to TCP sockets?
735 if (cmd == PRC_MSGSIZE && cmdarg != NULL)
736 ip6_notify_pmtu(inp, (struct sockaddr_in6 *)dst,
737 *(u_int32_t *)cmdarg);
740 * Detect if we should notify the error. If no source and
741 * destination ports are specified, but non-zero flowinfo and
742 * local address match, notify the error. This is the case
743 * when the error is delivered with an encrypted buffer
744 * by ESP. Otherwise, just compare addresses and ports
747 if (lport == 0 && fport == 0 && flowinfo &&
748 inp->inp_socket != NULL &&
749 flowinfo == (inp->inp_flow & IPV6_FLOWLABEL_MASK) &&
750 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
752 else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
753 &sa6_dst->sin6_addr) ||
754 inp->inp_socket == 0 ||
755 (lport && inp->inp_lport != lport) ||
756 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
757 !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
758 &sa6_src.sin6_addr)) ||
759 (fport && inp->inp_fport != fport)) {
765 (*notify)(inp, errno);
770 * Lookup a PCB based on the local address and port. Caller must hold the
771 * hash lock. No inpcb locks or references are acquired.
774 in6_pcblookup_local(struct inpcbinfo *pcbinfo, struct in6_addr *laddr,
775 u_short lport, int lookupflags, struct ucred *cred)
778 int matchwild = 3, wildcard;
780 KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
781 ("%s: invalid lookup flags %d", __func__, lookupflags));
783 INP_HASH_LOCK_ASSERT(pcbinfo);
785 if ((lookupflags & INPLOOKUP_WILDCARD) == 0) {
786 struct inpcbhead *head;
788 * Look for an unconnected (wildcard foreign addr) PCB that
789 * matches the local address and port we're looking for.
791 head = &pcbinfo->ipi_hashbase[INP_PCBHASH_WILD(lport,
792 pcbinfo->ipi_hashmask)];
793 CK_LIST_FOREACH(inp, head, inp_hash) {
794 /* XXX inp locking */
795 if ((inp->inp_vflag & INP_IPV6) == 0)
797 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
798 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
799 inp->inp_lport == lport) {
802 prison_equal_ip6(cred->cr_prison,
803 inp->inp_cred->cr_prison))
812 struct inpcbporthead *porthash;
813 struct inpcbport *phd;
814 struct inpcb *match = NULL;
816 * Best fit PCB lookup.
818 * First see if this local port is in use by looking on the
821 porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
822 pcbinfo->ipi_porthashmask)];
823 CK_LIST_FOREACH(phd, porthash, phd_hash) {
824 if (phd->phd_port == lport)
829 * Port is in use by one or more PCBs. Look for best
832 CK_LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
835 !prison_equal_ip6(cred->cr_prison,
836 inp->inp_cred->cr_prison))
838 /* XXX inp locking */
839 if ((inp->inp_vflag & INP_IPV6) == 0)
841 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
843 if (!IN6_IS_ADDR_UNSPECIFIED(
845 if (IN6_IS_ADDR_UNSPECIFIED(laddr))
847 else if (!IN6_ARE_ADDR_EQUAL(
848 &inp->in6p_laddr, laddr))
851 if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
854 if (wildcard < matchwild) {
856 matchwild = wildcard;
867 in6_multi_match(const struct inpcb *inp, void *v __unused)
870 if ((inp->inp_vflag & INP_IPV6) && inp->in6p_moptions != NULL)
877 in6_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
879 struct inpcb_iterator inpi = INP_ITERATOR(pcbinfo, INPLOOKUP_RLOCKPCB,
880 in6_multi_match, NULL);
882 struct in6_multi *inm;
883 struct in6_mfilter *imf;
884 struct ip6_moptions *im6o;
886 IN6_MULTI_LOCK_ASSERT();
888 while ((inp = inp_next(&inpi)) != NULL) {
889 INP_RLOCK_ASSERT(inp);
891 im6o = inp->in6p_moptions;
893 * Unselect the outgoing ifp for multicast if it
896 if (im6o->im6o_multicast_ifp == ifp)
897 im6o->im6o_multicast_ifp = NULL;
899 * Drop multicast group membership if we joined
900 * through the interface being detached.
903 IP6_MFILTER_FOREACH(imf, &im6o->im6o_head) {
904 if ((inm = imf->im6f_in6m) == NULL)
906 if (inm->in6m_ifp != ifp)
908 ip6_mfilter_remove(&im6o->im6o_head, imf);
909 in6_leavegroup_locked(inm, NULL);
910 ip6_mfilter_free(imf);
917 * Check for alternatives when higher level complains
918 * about service problems. For now, invalidate cached
919 * routing information. If the route was created dynamically
920 * (by a redirect), time to try a default gateway again.
923 in6_losing(struct inpcb *inp)
926 RO_INVALIDATE_CACHE(&inp->inp_route6);
930 * After a routing change, flush old routing
931 * and allocate a (hopefully) better one.
934 in6_rtchange(struct inpcb *inp, int errno __unused)
937 RO_INVALIDATE_CACHE(&inp->inp_route6);
941 static struct inpcb *
942 in6_pcblookup_lbgroup(const struct inpcbinfo *pcbinfo,
943 const struct in6_addr *laddr, uint16_t lport, const struct in6_addr *faddr,
944 uint16_t fport, int lookupflags, uint8_t numa_domain)
946 struct inpcb *local_wild, *numa_wild;
947 const struct inpcblbgrouphead *hdr;
948 struct inpcblbgroup *grp;
951 INP_HASH_LOCK_ASSERT(pcbinfo);
953 hdr = &pcbinfo->ipi_lbgrouphashbase[
954 INP_PCBPORTHASH(lport, pcbinfo->ipi_lbgrouphashmask)];
957 * Order of socket selection:
959 * 2. wild (if lookupflags contains INPLOOKUP_WILDCARD).
962 * - Load balanced group does not contain jailed sockets.
963 * - Load balanced does not contain IPv4 mapped INET6 wild sockets.
967 CK_LIST_FOREACH(grp, hdr, il_list) {
969 if (!(grp->il_vflag & INP_IPV6))
972 if (grp->il_lport != lport)
975 idx = INP6_PCBLBGROUP_PKTHASH(faddr, lport, fport) %
977 if (IN6_ARE_ADDR_EQUAL(&grp->il6_laddr, laddr)) {
978 if (numa_domain == M_NODOM ||
979 grp->il_numa_domain == numa_domain) {
980 return (grp->il_inp[idx]);
983 numa_wild = grp->il_inp[idx];
985 if (IN6_IS_ADDR_UNSPECIFIED(&grp->il6_laddr) &&
986 (lookupflags & INPLOOKUP_WILDCARD) != 0 &&
987 (local_wild == NULL || numa_domain == M_NODOM ||
988 grp->il_numa_domain == numa_domain)) {
989 local_wild = grp->il_inp[idx];
992 if (numa_wild != NULL)
998 * Lookup PCB in hash list. Used in in_pcb.c as well as here.
1001 in6_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
1002 u_int fport_arg, struct in6_addr *laddr, u_int lport_arg,
1003 int lookupflags, struct ifnet *ifp, uint8_t numa_domain)
1005 struct inpcbhead *head;
1006 struct inpcb *inp, *tmpinp;
1007 u_short fport = fport_arg, lport = lport_arg;
1009 KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
1010 ("%s: invalid lookup flags %d", __func__, lookupflags));
1012 INP_HASH_LOCK_ASSERT(pcbinfo);
1015 * First look for an exact match.
1018 head = &pcbinfo->ipi_hashbase[INP6_PCBHASH(faddr, lport, fport,
1019 pcbinfo->ipi_hashmask)];
1020 CK_LIST_FOREACH(inp, head, inp_hash) {
1021 /* XXX inp locking */
1022 if ((inp->inp_vflag & INP_IPV6) == 0)
1024 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
1025 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
1026 inp->inp_fport == fport &&
1027 inp->inp_lport == lport) {
1029 * XXX We should be able to directly return
1030 * the inp here, without any checks.
1031 * Well unless both bound with SO_REUSEPORT?
1033 if (prison_flag(inp->inp_cred, PR_IP6))
1043 * Then look in lb group (for wildcard match).
1045 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1046 inp = in6_pcblookup_lbgroup(pcbinfo, laddr, lport, faddr,
1047 fport, lookupflags, numa_domain);
1053 * Then look for a wildcard match, if requested.
1055 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1056 struct inpcb *local_wild = NULL, *local_exact = NULL;
1057 struct inpcb *jail_wild = NULL;
1061 * Order of socket selection - we always prefer jails.
1062 * 1. jailed, non-wild.
1064 * 3. non-jailed, non-wild.
1065 * 4. non-jailed, wild.
1067 head = &pcbinfo->ipi_hashbase[INP_PCBHASH_WILD(lport,
1068 pcbinfo->ipi_hashmask)];
1069 CK_LIST_FOREACH(inp, head, inp_hash) {
1070 /* XXX inp locking */
1071 if ((inp->inp_vflag & INP_IPV6) == 0)
1074 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
1075 inp->inp_lport != lport) {
1079 injail = prison_flag(inp->inp_cred, PR_IP6);
1081 if (prison_check_ip6_locked(
1082 inp->inp_cred->cr_prison, laddr) != 0)
1085 if (local_exact != NULL)
1089 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) {
1094 } else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1100 } /* LIST_FOREACH */
1102 if (jail_wild != NULL)
1104 if (local_exact != NULL)
1105 return (local_exact);
1106 if (local_wild != NULL)
1107 return (local_wild);
1108 } /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */
1117 * Lookup PCB in hash list, using pcbinfo tables. This variation locks the
1118 * hash list lock, and will return the inpcb locked (i.e., requires
1119 * INPLOOKUP_LOCKPCB).
1121 static struct inpcb *
1122 in6_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
1123 u_int fport, struct in6_addr *laddr, u_int lport, int lookupflags,
1124 struct ifnet *ifp, uint8_t numa_domain)
1128 smr_enter(pcbinfo->ipi_smr);
1129 inp = in6_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport,
1130 lookupflags & INPLOOKUP_WILDCARD, ifp, numa_domain);
1132 if (__predict_false(inp_smr_lock(inp,
1133 (lookupflags & INPLOOKUP_LOCKMASK)) == false))
1136 smr_exit(pcbinfo->ipi_smr);
1142 * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf
1143 * from which a pre-calculated hash value may be extracted.
1146 in6_pcblookup(struct inpcbinfo *pcbinfo, struct in6_addr *faddr, u_int fport,
1147 struct in6_addr *laddr, u_int lport, int lookupflags, struct ifnet *ifp)
1150 KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
1151 ("%s: invalid lookup flags %d", __func__, lookupflags));
1152 KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1153 ("%s: LOCKPCB not set", __func__));
1155 return (in6_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
1156 lookupflags, ifp, M_NODOM));
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 KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
1166 ("%s: invalid lookup flags %d", __func__, lookupflags));
1167 KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1168 ("%s: LOCKPCB not set", __func__));
1170 return (in6_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
1171 lookupflags, ifp, m->m_pkthdr.numa_domain));
1175 init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m, int srcordst)
1179 ip = mtod(m, struct ip6_hdr *);
1180 bzero(sin6, sizeof(*sin6));
1181 sin6->sin6_len = sizeof(*sin6);
1182 sin6->sin6_family = AF_INET6;
1183 sin6->sin6_addr = srcordst ? ip->ip6_dst : ip->ip6_src;
1185 (void)sa6_recoverscope(sin6); /* XXX: should catch errors... */