1 /* $FreeBSD: src/sys/netinet6/in6_pcb.c,v 1.10.2.9 2003/01/24 05:11:35 sam Exp $ */
2 /* $KAME: in6_pcb.c,v 1.31 2001/05/21 05:45:10 jinmei Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE 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 project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * Copyright (c) 1982, 1986, 1991, 1993
36 * The Regents of the University of California. All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * @(#)in_pcb.c 8.2 (Berkeley) 1/4/94
66 #include "opt_inet6.h"
67 #include "opt_ipsec.h"
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/malloc.h>
73 #include <sys/domain.h>
74 #include <sys/protosw.h>
75 #include <sys/socket.h>
76 #include <sys/socketvar.h>
77 #include <sys/sockio.h>
78 #include <sys/errno.h>
84 #include <sys/thread2.h>
85 #include <sys/msgport2.h>
87 #include <vm/vm_zone.h>
90 #include <net/if_types.h>
91 #include <net/route.h>
92 #include <net/netisr2.h>
94 #include <netinet/in.h>
95 #include <netinet/in_var.h>
96 #include <netinet/in_systm.h>
97 #include <netinet/ip6.h>
98 #include <netinet/ip_var.h>
99 #include <netinet6/ip6_var.h>
100 #include <netinet6/nd6.h>
101 #include <netinet/in_pcb.h>
102 #include <netinet6/in6_pcb.h>
105 #include <netinet6/ipsec.h>
107 #include <netinet6/ipsec6.h>
109 #include <netinet6/ah.h>
111 #include <netinet6/ah6.h>
113 #include <netproto/key/key.h>
117 #include <netproto/ipsec/ipsec.h>
118 #include <netproto/ipsec/ipsec6.h>
119 #include <netproto/ipsec/key.h>
121 #endif /* FAST_IPSEC */
123 struct in6_addr zeroin6_addr;
126 in6_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct thread *td)
128 struct socket *so = inp->inp_socket;
129 struct sockaddr_in6 jsin6;
132 if (!in6_ifaddr) /* XXX broken! */
133 return (EADDRNOTAVAIL);
134 if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
138 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
139 struct inpcbinfo *pcbinfo;
140 struct inpcbportinfo *portinfo;
141 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
142 struct ucred *cred = NULL;
144 u_short lport, lport_ho;
146 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
148 if (td->td_proc != NULL)
149 cred = td->td_proc->p_ucred;
151 if (nam->sa_len != sizeof(*sin6))
156 if (nam->sa_family != AF_INET6)
157 return (EAFNOSUPPORT);
159 if (!prison_replace_wildcards(td, nam))
162 /* KAME hack: embed scopeid */
163 if (in6_embedscope(&sin6->sin6_addr, sin6, inp, NULL) != 0)
165 /* this must be cleared for ifa_ifwithaddr() */
166 sin6->sin6_scope_id = 0;
168 lport = sin6->sin6_port;
169 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
171 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
172 * allow compepte duplication of binding if
173 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
174 * and a multicast address is bound on both
175 * new and duplicated sockets.
177 if (so->so_options & SO_REUSEADDR)
178 reuseport = SO_REUSEADDR|SO_REUSEPORT;
179 } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
180 struct ifaddr *ia = NULL;
182 sin6->sin6_port = 0; /* yech... */
183 if (!prison_replace_wildcards(td, (struct sockaddr *)sin6)) {
184 sin6->sin6_addr = kin6addr_any;
187 if ((ia = ifa_ifwithaddr((struct sockaddr *)sin6)) == NULL)
188 return (EADDRNOTAVAIL);
191 * XXX: bind to an anycast address might accidentally
192 * cause sending a packet with anycast source address.
193 * We should allow to bind to a deprecated address, since
194 * the application dare to use it.
197 ((struct in6_ifaddr *)ia)->ia6_flags &
198 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED))
199 return (EADDRNOTAVAIL);
202 inp->in6p_laddr = sin6->sin6_addr;
206 lport_ho = ntohs(lport);
209 if (lport_ho < IPV6PORT_RESERVED && cred &&
210 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0)) {
211 inp->in6p_laddr = kin6addr_any;
216 * Locate the proper portinfo based on lport
218 pcbinfo = inp->inp_pcbinfo;
220 &pcbinfo->portinfo[lport_ho & pcbinfo->portinfo_mask];
221 KKASSERT((lport_ho & pcbinfo->portinfo_mask) ==
225 * This has to be atomic. If the porthash is shared across
226 * multiple protocol threads (aka tcp) then the token must
229 GET_PORT_TOKEN(portinfo);
231 if (so->so_cred->cr_uid != 0 &&
232 !IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
233 t = in6_pcblookup_local(portinfo,
234 &sin6->sin6_addr, lport, INPLOOKUP_WILDCARD, cred);
236 (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
237 !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) ||
238 (t->inp_socket->so_options & SO_REUSEPORT) == 0) &&
239 (so->so_cred->cr_uid !=
240 t->inp_socket->so_cred->cr_uid)) {
241 inp->in6p_laddr = kin6addr_any;
245 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
246 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
247 struct sockaddr_in sin;
249 in6_sin6_2_sin(&sin, sin6);
250 t = in_pcblookup_local(portinfo,
252 INPLOOKUP_WILDCARD, cred);
254 (so->so_cred->cr_uid !=
255 t->inp_socket->so_cred->cr_uid) &&
256 (ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
258 INP_SOCKAF(t->inp_socket))) {
259 inp->in6p_laddr = kin6addr_any;
265 if (cred && cred->cr_prison &&
266 !prison_replace_wildcards(td, nam)) {
267 inp->in6p_laddr = kin6addr_any;
268 error = EADDRNOTAVAIL;
271 t = in6_pcblookup_local(portinfo, &sin6->sin6_addr, lport,
273 if (t && (reuseport & t->inp_socket->so_options) == 0) {
274 inp->in6p_laddr = kin6addr_any;
278 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
279 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
280 struct sockaddr_in sin;
282 in6_sin6_2_sin(&sin, sin6);
283 t = in_pcblookup_local(portinfo, sin.sin_addr, lport,
285 if (t && (reuseport & t->inp_socket->so_options) == 0 &&
286 (ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
287 INP_SOCKAF(so) == INP_SOCKAF(t->inp_socket))) {
288 inp->in6p_laddr = kin6addr_any;
294 inp->inp_lport = lport;
295 in_pcbinsporthash(portinfo, inp);
298 REL_PORT_TOKEN(portinfo);
302 jsin6.sin6_addr = inp->in6p_laddr;
303 jsin6.sin6_family = AF_INET6;
304 if (!prison_replace_wildcards(td, (struct sockaddr*)&jsin6)) {
305 inp->in6p_laddr = kin6addr_any;
310 return in6_pcbsetlport(&inp->in6p_laddr, inp, td);
315 * Transform old in6_pcbconnect() into an inner subroutine for new
316 * in6_pcbconnect(): Do some validity-checking on the remote
317 * address (in mbuf 'nam') and then determine local host address
318 * (i.e., which interface) to use to access that remote host.
320 * This preserves definition of in6_pcbconnect(), while supporting a
321 * slightly different version for T/TCP. (This is more than
322 * a bit of a kludge, but cleaning up the internal interfaces would
323 * have forced minor changes in every protocol).
327 in6_pcbladdr(struct inpcb *inp, struct sockaddr *nam,
328 struct in6_addr **plocal_addr6, struct thread *td)
330 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
331 struct ifnet *ifp = NULL;
334 if (nam->sa_len != sizeof (*sin6))
336 if (sin6->sin6_family != AF_INET6)
337 return (EAFNOSUPPORT);
338 if (sin6->sin6_port == 0)
339 return (EADDRNOTAVAIL);
341 /* KAME hack: embed scopeid */
342 if (in6_embedscope(&sin6->sin6_addr, sin6, inp, &ifp) != 0)
347 * If the destination address is UNSPECIFIED addr,
348 * use the loopback addr, e.g ::1.
350 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
351 sin6->sin6_addr = kin6addr_loopback;
355 * XXX: in6_selectsrc might replace the bound local address
356 * with the address specified by setsockopt(IPV6_PKTINFO).
357 * Is it the intended behavior?
359 *plocal_addr6 = in6_selectsrc(sin6, inp->in6p_outputopts,
362 &inp->in6p_laddr, &error, td);
363 if (*plocal_addr6 == NULL) {
365 error = EADDRNOTAVAIL;
369 * Don't do pcblookup call here; return interface in
371 * and exit to caller, that will do the lookup.
375 if (inp->in6p_route.ro_rt)
376 ifp = inp->in6p_route.ro_rt->rt_ifp;
383 * Connect from a socket to a specified address.
384 * Both address and port must be specified in argument sin.
385 * If don't have a local address for this socket yet,
389 in6_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct thread *td)
391 struct in6_addr *addr6;
392 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
396 * Call inner routine, to assign local interface address.
397 * in6_pcbladdr() may automatically fill in sin6_scope_id.
399 if ((error = in6_pcbladdr(inp, nam, &addr6, td)) != 0)
402 if (in6_pcblookup_hash(inp->inp_pcbinfo, &sin6->sin6_addr,
404 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
405 ? addr6 : &inp->in6p_laddr,
406 inp->inp_lport, 0, NULL) != NULL) {
409 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
410 if (inp->inp_lport == 0) {
411 error = in6_pcbbind(inp, NULL, td);
415 inp->in6p_laddr = *addr6;
417 inp->in6p_faddr = sin6->sin6_addr;
418 inp->inp_fport = sin6->sin6_port;
419 /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
420 inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
421 if (inp->in6p_flags & IN6P_AUTOFLOWLABEL)
422 inp->in6p_flowinfo |=
423 (htonl(ip6_flow_seq++) & IPV6_FLOWLABEL_MASK);
425 in_pcbinsconnhash(inp);
431 * Return an IPv6 address, which is the most appropriate for given
432 * destination and user specified options.
433 * If necessary, this function lookups the routing table and return
434 * an entry to the caller for later use.
437 in6_selectsrc(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts,
438 struct ip6_moptions *mopts, struct route_in6 *ro,
439 struct in6_addr *laddr, int *errorp, struct thread *td)
441 struct sockaddr_in6 jsin6;
442 struct ucred *cred = NULL;
443 struct in6_addr *dst;
444 struct in6_ifaddr *ia6 = 0;
445 struct in6_pktinfo *pi = NULL;
448 if (td && td->td_proc && td->td_proc->p_ucred)
449 cred = td->td_proc->p_ucred;
450 if (cred && cred->cr_prison)
452 jsin6.sin6_family = AF_INET6;
453 dst = &dstsock->sin6_addr;
457 * If the source address is explicitly specified by the caller,
460 if (opts && (pi = opts->ip6po_pktinfo) &&
461 !IN6_IS_ADDR_UNSPECIFIED(&pi->ipi6_addr)) {
462 jsin6.sin6_addr = pi->ipi6_addr;
463 if (jailed && !jailed_ip(cred->cr_prison,
464 (struct sockaddr *)&jsin6)) {
467 return (&pi->ipi6_addr);
472 * If the source address is not specified but the socket(if any)
473 * is already bound, use the bound address.
475 if (laddr && !IN6_IS_ADDR_UNSPECIFIED(laddr)) {
476 jsin6.sin6_addr = *laddr;
477 if (jailed && !jailed_ip(cred->cr_prison,
478 (struct sockaddr *)&jsin6)) {
486 * If the caller doesn't specify the source address but
487 * the outgoing interface, use an address associated with
490 if (pi && pi->ipi6_ifindex) {
491 /* XXX boundary check is assumed to be already done. */
492 ia6 = in6_ifawithscope(ifindex2ifnet[pi->ipi6_ifindex],
496 jsin6.sin6_addr = (&ia6->ia_addr)->sin6_addr;
497 if (!jailed_ip(cred->cr_prison,
498 (struct sockaddr *)&jsin6))
503 *errorp = EADDRNOTAVAIL;
506 return (&satosin6(&ia6->ia_addr)->sin6_addr);
510 * If the destination address is a link-local unicast address or
511 * a multicast address, and if the outgoing interface is specified
512 * by the sin6_scope_id filed, use an address associated with the
514 * XXX: We're now trying to define more specific semantics of
515 * sin6_scope_id field, so this part will be rewritten in
518 if ((IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MULTICAST(dst)) &&
519 dstsock->sin6_scope_id) {
521 * I'm not sure if boundary check for scope_id is done
524 if (dstsock->sin6_scope_id < 0 ||
525 if_index < dstsock->sin6_scope_id) {
526 *errorp = ENXIO; /* XXX: better error? */
529 ia6 = in6_ifawithscope(ifindex2ifnet[dstsock->sin6_scope_id],
533 jsin6.sin6_addr = (&ia6->ia_addr)->sin6_addr;
534 if (!jailed_ip(cred->cr_prison,
535 (struct sockaddr *)&jsin6))
540 *errorp = EADDRNOTAVAIL;
543 return (&satosin6(&ia6->ia_addr)->sin6_addr);
547 * If the destination address is a multicast address and
548 * the outgoing interface for the address is specified
549 * by the caller, use an address associated with the interface.
550 * There is a sanity check here; if the destination has node-local
551 * scope, the outgoing interfacde should be a loopback address.
552 * Even if the outgoing interface is not specified, we also
553 * choose a loopback interface as the outgoing interface.
555 if (!jailed && IN6_IS_ADDR_MULTICAST(dst)) {
556 struct ifnet *ifp = mopts ? mopts->im6o_multicast_ifp : NULL;
558 if (ifp == NULL && IN6_IS_ADDR_MC_NODELOCAL(dst)) {
563 ia6 = in6_ifawithscope(ifp, dst, cred);
565 *errorp = EADDRNOTAVAIL;
568 return (&ia6->ia_addr.sin6_addr);
573 * If the next hop address for the packet is specified
574 * by caller, use an address associated with the route
578 struct sockaddr_in6 *sin6_next;
581 if (opts && opts->ip6po_nexthop) {
582 sin6_next = satosin6(opts->ip6po_nexthop);
583 rt = nd6_lookup(&sin6_next->sin6_addr, 1, NULL);
585 ia6 = in6_ifawithscope(rt->rt_ifp, dst, cred);
587 ia6 = ifatoia6(rt->rt_ifa);
590 jsin6.sin6_addr = (&ia6->ia_addr)->sin6_addr;
591 if (!jailed_ip(cred->cr_prison,
592 (struct sockaddr *)&jsin6))
597 *errorp = EADDRNOTAVAIL;
600 return (&satosin6(&ia6->ia_addr)->sin6_addr);
605 * If route is known or can be allocated now,
606 * our src addr is taken from the i/f, else punt.
610 !IN6_ARE_ADDR_EQUAL(&satosin6(&ro->ro_dst)->sin6_addr, dst)) {
614 if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp == NULL) {
615 struct sockaddr_in6 *dst6;
617 /* No route yet, so try to acquire one */
618 bzero(&ro->ro_dst, sizeof(struct sockaddr_in6));
620 dst6->sin6_family = AF_INET6;
621 dst6->sin6_len = sizeof(struct sockaddr_in6);
622 dst6->sin6_addr = *dst;
623 if (!jailed && IN6_IS_ADDR_MULTICAST(dst)) {
625 rtpurelookup((struct sockaddr *)&ro->ro_dst);
627 rtalloc((struct route *)ro);
632 * in_pcbconnect() checks out IFF_LOOPBACK to skip using
633 * the address. But we don't know why it does so.
634 * It is necessary to ensure the scope even for lo0
635 * so doesn't check out IFF_LOOPBACK.
639 ia6 = in6_ifawithscope(ro->ro_rt->rt_ifa->ifa_ifp, dst, cred);
641 jsin6.sin6_addr = (&ia6->ia_addr)->sin6_addr;
642 if (!jailed_ip(cred->cr_prison,
643 (struct sockaddr *)&jsin6))
647 if (ia6 == 0) /* xxx scope error ?*/
648 ia6 = ifatoia6(ro->ro_rt->rt_ifa);
651 jsin6.sin6_addr = (&ia6->ia_addr)->sin6_addr;
652 if (!jailed_ip(cred->cr_prison,
653 (struct sockaddr *)&jsin6))
658 *errorp = EHOSTUNREACH; /* no route */
661 return (&satosin6(&ia6->ia_addr)->sin6_addr);
664 *errorp = EADDRNOTAVAIL;
669 * Default hop limit selection. The precedence is as follows:
670 * 1. Hoplimit valued specified via ioctl.
671 * 2. (If the outgoing interface is detected) the current
672 * hop limit of the interface specified by router advertisement.
673 * 3. The system default hoplimit.
676 in6_selecthlim(struct in6pcb *in6p, struct ifnet *ifp)
678 if (in6p && in6p->in6p_hops >= 0)
679 return (in6p->in6p_hops);
681 return (ND_IFINFO(ifp)->chlim);
683 return (ip6_defhlim);
688 in6_pcbdisconnect(struct inpcb *inp)
690 bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr));
692 /* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
693 inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
694 in_pcbremconnhash(inp);
695 if (inp->inp_socket->so_state & SS_NOFDREF)
700 in6_pcbdetach(struct inpcb *inp)
702 struct socket *so = inp->inp_socket;
703 struct inpcbinfo *ipi = inp->inp_pcbinfo;
706 if (inp->in6p_sp != NULL)
707 ipsec6_delete_pcbpolicy(inp);
709 inp->inp_gencnt = ++ipi->ipi_gencnt;
712 KKASSERT((so->so_state & SS_ASSERTINPROG) == 0);
713 sofree(so); /* remove pcb ref */
715 if (inp->in6p_options)
716 m_freem(inp->in6p_options);
717 ip6_freepcbopts(inp->in6p_outputopts);
718 ip6_freemoptions(inp->in6p_moptions);
719 if (inp->in6p_route.ro_rt)
720 rtfree(inp->in6p_route.ro_rt);
721 /* Check and free IPv4 related resources in case of mapped addr */
722 if (inp->inp_options)
723 m_free(inp->inp_options);
724 ip_freemoptions(inp->inp_moptions);
731 * The calling convention of in6_setsockaddr() and in6_setpeeraddr() was
732 * modified to match the pru_sockaddr() and pru_peeraddr() entry points
733 * in struct pr_usrreqs, so that protocols can just reference then directly
734 * without the need for a wrapper function. The socket must have a valid
735 * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
736 * except through a kernel programming error, so it is acceptable to panic
737 * (or in this case trap) if the PCB is invalid. (Actually, we don't trap
738 * because there actually /is/ a programming error somewhere... XXX)
741 in6_setsockaddr_dispatch(netmsg_t msg)
745 error = in6_setsockaddr(msg->sockaddr.base.nm_so, msg->sockaddr.nm_nam);
746 lwkt_replymsg(&msg->sockaddr.base.lmsg, error);
750 in6_setsockaddr(struct socket *so, struct sockaddr **nam)
753 struct sockaddr_in6 *sin6;
756 * Do the malloc first in case it blocks.
758 sin6 = kmalloc(sizeof *sin6, M_SONAME, M_WAITOK | M_ZERO);
759 sin6->sin6_family = AF_INET6;
760 sin6->sin6_len = sizeof(*sin6);
766 kfree(sin6, M_SONAME);
769 sin6->sin6_port = inp->inp_lport;
770 sin6->sin6_addr = inp->in6p_laddr;
772 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
773 sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]);
775 sin6->sin6_scope_id = 0; /*XXX*/
776 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
777 sin6->sin6_addr.s6_addr16[1] = 0;
779 *nam = (struct sockaddr *)sin6;
784 in6_setpeeraddr_dispatch(netmsg_t msg)
788 error = in6_setpeeraddr(msg->peeraddr.base.nm_so, msg->peeraddr.nm_nam);
789 lwkt_replymsg(&msg->peeraddr.base.lmsg, error);
793 in6_setpeeraddr(struct socket *so, struct sockaddr **nam)
796 struct sockaddr_in6 *sin6;
799 * Do the malloc first in case it blocks.
801 sin6 = kmalloc(sizeof(*sin6), M_SONAME, M_WAITOK | M_ZERO);
802 sin6->sin6_family = AF_INET6;
803 sin6->sin6_len = sizeof(struct sockaddr_in6);
809 kfree(sin6, M_SONAME);
812 sin6->sin6_port = inp->inp_fport;
813 sin6->sin6_addr = inp->in6p_faddr;
815 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
816 sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]);
818 sin6->sin6_scope_id = 0; /*XXX*/
819 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
820 sin6->sin6_addr.s6_addr16[1] = 0;
822 *nam = (struct sockaddr *)sin6;
827 in6_mapped_sockaddr_dispatch(netmsg_t msg)
831 error = in6_mapped_sockaddr(msg->sockaddr.base.nm_so,
832 msg->sockaddr.nm_nam);
833 lwkt_replymsg(&msg->sockaddr.base.lmsg, error);
837 in6_mapped_sockaddr(struct socket *so, struct sockaddr **nam)
839 struct inpcb *inp = so->so_pcb;
844 if (inp->inp_vflag & INP_IPV4) {
845 error = in_setsockaddr(so, nam);
847 in6_sin_2_v4mapsin6_in_sock(nam);
849 /* scope issues will be handled in in6_setsockaddr(). */
850 error = in6_setsockaddr(so, nam);
856 in6_mapped_peeraddr(struct socket *so, struct sockaddr **nam)
858 struct inpcb *inp = so->so_pcb;
863 if (inp->inp_vflag & INP_IPV4) {
864 error = in_setpeeraddr(so, nam);
866 in6_sin_2_v4mapsin6_in_sock(nam);
868 /* scope issues will be handled in in6_setpeeraddr(). */
869 error = in6_setpeeraddr(so, nam);
875 in6_mapped_peeraddr_dispatch(netmsg_t msg)
879 error = in6_mapped_peeraddr(msg->base.nm_so, msg->peeraddr.nm_nam);
880 lwkt_replymsg(&msg->base.lmsg, error);
884 * Pass some notification to all connections of a protocol
885 * associated with address dst. The local address and/or port numbers
886 * may be specified to limit the search. The "usual action" will be
887 * taken, depending on the ctlinput cmd. The caller must filter any
888 * cmds that are uninteresting (e.g., no error in the map).
889 * Call the protocol specific routine (if any) to report
890 * any errors for each matching socket.
893 in6_pcbnotify(struct inpcbinfo *pcbinfo, struct sockaddr *dst, in_port_t fport,
894 const struct sockaddr *src, in_port_t lport, int cmd, int arg,
897 struct inpcb *inp, *marker;
898 struct sockaddr_in6 sa6_src, *sa6_dst;
901 if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6)
904 sa6_dst = (struct sockaddr_in6 *)dst;
905 if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
909 * note that src can be NULL when we get notify by local fragmentation.
911 sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
912 flowinfo = sa6_src.sin6_flowinfo;
915 * Redirects go to all references to the destination,
916 * and use in6_rtchange to invalidate the route cache.
917 * Dead host indications: also use in6_rtchange to invalidate
918 * the cache, and deliver the error to all the sockets.
919 * Otherwise, if we have knowledge of the local port and address,
920 * deliver only to that socket.
922 if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
925 bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
927 if (cmd != PRC_HOSTDEAD)
928 notify = in6_rtchange;
930 if (cmd != PRC_MSGSIZE)
931 arg = inet6ctlerrmap[cmd];
933 marker = in_pcbmarker(mycpuid);
935 GET_PCBINFO_TOKEN(pcbinfo);
937 LIST_INSERT_HEAD(&pcbinfo->pcblisthead, marker, inp_list);
938 while ((inp = LIST_NEXT(marker, inp_list)) != NULL) {
939 LIST_REMOVE(marker, inp_list);
940 LIST_INSERT_AFTER(inp, marker, inp_list);
942 if (inp->inp_flags & INP_PLACEMARKER)
945 if ((inp->inp_vflag & INP_IPV6) == 0)
948 * If the error designates a new path MTU for a destination
949 * and the application (associated with this socket) wanted to
950 * know the value, notify. Note that we notify for all
951 * disconnected sockets if the corresponding application
952 * wanted. This is because some UDP applications keep sending
953 * sockets disconnected.
954 * XXX: should we avoid to notify the value to TCP sockets?
956 if (cmd == PRC_MSGSIZE && (inp->inp_flags & IN6P_MTU) != 0 &&
957 (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
958 IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &sa6_dst->sin6_addr))) {
959 ip6_notify_pmtu(inp, (struct sockaddr_in6 *)dst, &arg);
963 * Detect if we should notify the error. If no source and
964 * destination ports are specifed, but non-zero flowinfo and
965 * local address match, notify the error. This is the case
966 * when the error is delivered with an encrypted buffer
967 * by ESP. Otherwise, just compare addresses and ports
970 if (lport == 0 && fport == 0 && flowinfo &&
971 inp->inp_socket != NULL &&
972 flowinfo == (inp->in6p_flowinfo & IPV6_FLOWLABEL_MASK) &&
973 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
975 else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
976 &sa6_dst->sin6_addr) ||
977 inp->inp_socket == 0 ||
978 (lport && inp->inp_lport != lport) ||
979 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
980 !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
981 &sa6_src.sin6_addr)) ||
982 (fport && inp->inp_fport != fport))
989 LIST_REMOVE(marker, inp_list);
991 REL_PCBINFO_TOKEN(pcbinfo);
995 * Lookup a PCB based on the local address and port.
998 in6_pcblookup_local(struct inpcbportinfo *portinfo,
999 const struct in6_addr *laddr, u_int lport_arg, int wild_okay,
1003 int matchwild = 3, wildcard;
1004 u_short lport = lport_arg;
1005 struct inpcbporthead *porthash;
1006 struct inpcbport *phd;
1007 struct inpcb *match = NULL;
1010 * If the porthashbase is shared across several cpus, it must
1013 ASSERT_PORT_TOKEN_HELD(portinfo);
1016 * Best fit PCB lookup.
1018 * First see if this local port is in use by looking on the
1021 porthash = &portinfo->porthashbase[
1022 INP_PCBPORTHASH(lport, portinfo->porthashmask)];
1023 LIST_FOREACH(phd, porthash, phd_hash) {
1024 if (phd->phd_port == lport)
1030 * Port is in use by one or more PCBs. Look for best
1033 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
1035 if ((inp->inp_vflag & INP_IPV6) == 0)
1037 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
1039 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1040 if (IN6_IS_ADDR_UNSPECIFIED(laddr))
1042 else if (!IN6_ARE_ADDR_EQUAL(
1043 &inp->in6p_laddr, laddr))
1046 if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
1049 if (wildcard && !wild_okay)
1051 if (wildcard < matchwild &&
1054 inp->inp_socket->so_cred->cr_prison)) {
1056 matchwild = wildcard;
1060 matchwild = wildcard;
1068 in6_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
1070 struct in6pcb *in6p, *marker;
1071 struct ip6_moptions *im6o;
1072 struct in6_multi_mship *imm, *nimm;
1075 * We only need to make sure that we are in netisr0, where all
1076 * multicast operation happen. We could check inpcbinfo which
1077 * does not belong to netisr0 by holding the inpcbinfo's token.
1078 * In this case, the pcbinfo must be able to be shared, i.e.
1079 * pcbinfo->infotoken is not NULL.
1081 KASSERT(&curthread->td_msgport == netisr_cpuport(0),
1082 ("not in netisr0"));
1083 KASSERT(pcbinfo->cpu == 0 || pcbinfo->infotoken != NULL,
1084 ("pcbinfo could not be shared"));
1087 * Get a marker for the current netisr (netisr0).
1089 * It is possible that the multicast address deletion blocks,
1090 * which could cause temporary token releasing. So we use
1091 * inpcb marker here to get a coherent view of the inpcb list.
1093 * While, on the other hand, moptions are only added and deleted
1094 * in netisr0, so we would not see staled moption or miss moption
1095 * even if the token was released due to the blocking multicast
1098 marker = in_pcbmarker(mycpuid);
1100 GET_PCBINFO_TOKEN(pcbinfo);
1102 LIST_INSERT_HEAD(&pcbinfo->pcblisthead, marker, inp_list);
1103 while ((in6p = LIST_NEXT(marker, inp_list)) != NULL) {
1104 LIST_REMOVE(marker, inp_list);
1105 LIST_INSERT_AFTER(in6p, marker, inp_list);
1107 if (in6p->in6p_flags & INP_PLACEMARKER)
1109 im6o = in6p->in6p_moptions;
1110 if ((in6p->inp_vflag & INP_IPV6) &&
1113 * Unselect the outgoing interface if it is being
1116 if (im6o->im6o_multicast_ifp == ifp)
1117 im6o->im6o_multicast_ifp = NULL;
1120 * Drop multicast group membership if we joined
1121 * through the interface being detached.
1122 * XXX controversial - is it really legal for kernel
1125 for (imm = im6o->im6o_memberships.lh_first;
1126 imm != NULL; imm = nimm) {
1127 nimm = imm->i6mm_chain.le_next;
1128 if (imm->i6mm_maddr->in6m_ifp == ifp) {
1129 LIST_REMOVE(imm, i6mm_chain);
1130 in6_delmulti(imm->i6mm_maddr);
1131 kfree(imm, M_IPMADDR);
1136 LIST_REMOVE(marker, inp_list);
1138 REL_PCBINFO_TOKEN(pcbinfo);
1142 * Check for alternatives when higher level complains
1143 * about service problems. For now, invalidate cached
1144 * routing information. If the route was created dynamically
1145 * (by a redirect), time to try a default gateway again.
1148 in6_losing(struct inpcb *in6p)
1151 struct rt_addrinfo info;
1153 if ((rt = in6p->in6p_route.ro_rt) != NULL) {
1154 bzero((caddr_t)&info, sizeof(info));
1155 info.rti_flags = rt->rt_flags;
1156 info.rti_info[RTAX_DST] = rt_key(rt);
1157 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1158 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1159 rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
1160 if (rt->rt_flags & RTF_DYNAMIC) {
1161 rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1162 rt_mask(rt), rt->rt_flags, NULL);
1164 in6p->in6p_route.ro_rt = NULL;
1167 * A new route can be allocated
1168 * the next time output is attempted.
1174 * After a routing change, flush old routing
1175 * and allocate a (hopefully) better one.
1178 in6_rtchange(struct inpcb *inp, int error)
1180 if (inp->in6p_route.ro_rt) {
1181 rtfree(inp->in6p_route.ro_rt);
1182 inp->in6p_route.ro_rt = 0;
1184 * A new route can be allocated the next time
1185 * output is attempted.
1191 * Lookup PCB in hash list.
1194 in6_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
1195 u_int fport_arg, struct in6_addr *laddr, u_int lport_arg,
1196 int wildcard, struct ifnet *ifp)
1198 struct inpcbhead *head;
1200 struct inpcb *jinp = NULL;
1201 u_short fport = fport_arg, lport = lport_arg;
1204 if (faithprefix_p != NULL)
1205 faith = (*faithprefix_p)(laddr);
1210 * First look for an exact match.
1212 head = &pcbinfo->hashbase[INP_PCBCONNHASH(faddr->s6_addr32[3] /* XXX */,
1214 laddr->s6_addr32[3], /* XXX JH */
1216 pcbinfo->hashmask)];
1217 LIST_FOREACH(inp, head, inp_hash) {
1218 if ((inp->inp_vflag & INP_IPV6) == 0)
1220 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
1221 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
1222 inp->inp_fport == fport &&
1223 inp->inp_lport == lport) {
1227 if (inp->inp_socket == NULL ||
1228 inp->inp_socket->so_cred->cr_prison == NULL) {
1240 struct inpcontainerhead *chead;
1241 struct inpcontainer *ic;
1242 struct inpcb *local_wild = NULL;
1243 struct inpcb *jinp_wild = NULL;
1244 struct sockaddr_in6 jsin6;
1248 * Order of socket selection:
1249 * 1. non-jailed, non-wild.
1250 * 2. non-jailed, wild.
1251 * 3. jailed, non-wild.
1254 jsin6.sin6_family = AF_INET6;
1255 chead = &pcbinfo->wildcardhashbase[INP_PCBWILDCARDHASH(lport,
1256 pcbinfo->wildcardhashmask)];
1258 GET_PCBINFO_TOKEN(pcbinfo);
1259 LIST_FOREACH(ic, chead, ic_list) {
1261 if (inp->inp_flags & INP_PLACEMARKER)
1264 if (!(inp->inp_vflag & INP_IPV6))
1266 if (inp->inp_socket != NULL)
1267 cred = inp->inp_socket->so_cred;
1271 if (cred != NULL && jailed(cred)) {
1275 jsin6.sin6_addr = *laddr;
1276 if (!jailed_ip(cred->cr_prison,
1277 (struct sockaddr *)&jsin6))
1281 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
1282 inp->inp_lport == lport) {
1283 if (faith && (inp->inp_flags & INP_FAITH) == 0)
1285 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
1287 if (cred != NULL && jailed(cred)) {
1290 REL_PCBINFO_TOKEN(pcbinfo);
1293 } else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1294 if (cred != NULL && jailed(cred))
1301 REL_PCBINFO_TOKEN(pcbinfo);
1303 if (local_wild != NULL)
1304 return (local_wild);
1317 init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m)
1321 ip = mtod(m, struct ip6_hdr *);
1322 bzero(sin6, sizeof(*sin6));
1323 sin6->sin6_len = sizeof(*sin6);
1324 sin6->sin6_family = AF_INET6;
1325 sin6->sin6_addr = ip->ip6_src;
1326 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
1327 sin6->sin6_addr.s6_addr16[1] = 0;
1328 sin6->sin6_scope_id =
1329 (m->m_pkthdr.rcvif && IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
1330 ? m->m_pkthdr.rcvif->if_index : 0;
1336 in6_savefaddr(struct socket *so, const struct sockaddr *faddr)
1338 struct sockaddr_in6 *sin6;
1340 KASSERT(faddr->sa_family == AF_INET6,
1341 ("not AF_INET6 faddr %d", faddr->sa_family));
1343 sin6 = kmalloc(sizeof(*sin6), M_SONAME, M_WAITOK | M_ZERO);
1344 sin6->sin6_family = AF_INET6;
1345 sin6->sin6_len = sizeof(*sin6);
1347 sin6->sin6_port = ((const struct sockaddr_in6 *)faddr)->sin6_port;
1348 sin6->sin6_addr = ((const struct sockaddr_in6 *)faddr)->sin6_addr;
1350 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
1351 sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]);
1353 sin6->sin6_scope_id = 0; /*XXX*/
1354 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
1355 sin6->sin6_addr.s6_addr16[1] = 0;
1357 so->so_faddr = (struct sockaddr *)sin6;
1361 in6_mapped_savefaddr(struct socket *so, const struct sockaddr *faddr)
1363 if (faddr->sa_family == AF_INET) {
1364 in_savefaddr(so, faddr);
1365 in6_sin_2_v4mapsin6_in_sock(&so->so_faddr);
1367 in6_savefaddr(so, faddr);