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. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. 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
70 #include "opt_inet6.h"
71 #include "opt_ipsec.h"
73 #include <sys/param.h>
74 #include <sys/systm.h>
75 #include <sys/malloc.h>
77 #include <sys/domain.h>
78 #include <sys/protosw.h>
79 #include <sys/socket.h>
80 #include <sys/socketvar.h>
81 #include <sys/sockio.h>
82 #include <sys/errno.h>
90 #include <net/if_types.h>
91 #include <net/route.h>
93 #include <netinet/in.h>
94 #include <netinet/in_var.h>
95 #include <netinet/in_systm.h>
96 #include <netinet/tcp_var.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 <netkey/key.h>
116 struct in6_addr zeroin6_addr;
119 in6_pcbbind(inp, nam, td)
120 register struct inpcb *inp;
121 struct sockaddr *nam;
124 struct socket *so = inp->inp_socket;
125 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL;
126 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
128 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
130 if (!in6_ifaddr) /* XXX broken! */
131 return (EADDRNOTAVAIL);
132 if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
134 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
137 sin6 = (struct sockaddr_in6 *)nam;
138 if (nam->sa_len != sizeof(*sin6))
143 if (nam->sa_family != AF_INET6)
144 return(EAFNOSUPPORT);
146 /* KAME hack: embed scopeid */
147 if (in6_embedscope(&sin6->sin6_addr, sin6, inp, NULL) != 0)
149 /* this must be cleared for ifa_ifwithaddr() */
150 sin6->sin6_scope_id = 0;
152 lport = sin6->sin6_port;
153 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
155 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
156 * allow compepte duplication of binding if
157 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
158 * and a multicast address is bound on both
159 * new and duplicated sockets.
161 if (so->so_options & SO_REUSEADDR)
162 reuseport = SO_REUSEADDR|SO_REUSEPORT;
163 } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
164 struct ifaddr *ia = NULL;
166 sin6->sin6_port = 0; /* yech... */
167 if ((ia = ifa_ifwithaddr((struct sockaddr *)sin6)) == 0)
168 return(EADDRNOTAVAIL);
171 * XXX: bind to an anycast address might accidentally
172 * cause sending a packet with anycast source address.
173 * We should allow to bind to a deprecated address, since
174 * the application dare to use it.
177 ((struct in6_ifaddr *)ia)->ia6_flags &
178 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) {
179 return(EADDRNOTAVAIL);
186 if (ntohs(lport) < IPV6PORT_RESERVED && td &&
187 suser_cred(td->td_ucred, PRISON_ROOT))
189 if (so->so_cred->cr_uid != 0 &&
190 !IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
191 t = in6_pcblookup_local(pcbinfo,
192 &sin6->sin6_addr, lport,
195 (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
196 !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) ||
197 (t->inp_socket->so_options &
198 SO_REUSEPORT) == 0) &&
199 (so->so_cred->cr_uid !=
200 t->inp_socket->so_cred->cr_uid))
202 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
203 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
204 struct sockaddr_in sin;
206 in6_sin6_2_sin(&sin, sin6);
207 t = in_pcblookup_local(pcbinfo,
211 (so->so_cred->cr_uid !=
212 t->inp_socket->so_cred->cr_uid) &&
213 (ntohl(t->inp_laddr.s_addr) !=
216 INP_SOCKAF(t->inp_socket)))
220 t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr,
222 if (t && (reuseport & t->inp_socket->so_options) == 0)
224 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
225 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
226 struct sockaddr_in sin;
228 in6_sin6_2_sin(&sin, sin6);
229 t = in_pcblookup_local(pcbinfo, sin.sin_addr,
232 (reuseport & t->inp_socket->so_options)
234 (ntohl(t->inp_laddr.s_addr)
237 INP_SOCKAF(t->inp_socket)))
241 inp->in6p_laddr = sin6->sin6_addr;
245 if ((e = in6_pcbsetport(&inp->in6p_laddr, inp, td)) != 0)
249 inp->inp_lport = lport;
250 if (in_pcbinshash(inp) != 0) {
251 inp->in6p_laddr = in6addr_any;
260 * Transform old in6_pcbconnect() into an inner subroutine for new
261 * in6_pcbconnect(): Do some validity-checking on the remote
262 * address (in mbuf 'nam') and then determine local host address
263 * (i.e., which interface) to use to access that remote host.
265 * This preserves definition of in6_pcbconnect(), while supporting a
266 * slightly different version for T/TCP. (This is more than
267 * a bit of a kludge, but cleaning up the internal interfaces would
268 * have forced minor changes in every protocol).
272 in6_pcbladdr(inp, nam, plocal_addr6)
273 register struct inpcb *inp;
274 struct sockaddr *nam;
275 struct in6_addr **plocal_addr6;
277 register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
278 struct ifnet *ifp = NULL;
281 if (nam->sa_len != sizeof (*sin6))
283 if (sin6->sin6_family != AF_INET6)
284 return (EAFNOSUPPORT);
285 if (sin6->sin6_port == 0)
286 return (EADDRNOTAVAIL);
288 /* KAME hack: embed scopeid */
289 if (in6_embedscope(&sin6->sin6_addr, sin6, inp, &ifp) != 0)
294 * If the destination address is UNSPECIFIED addr,
295 * use the loopback addr, e.g ::1.
297 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
298 sin6->sin6_addr = in6addr_loopback;
302 * XXX: in6_selectsrc might replace the bound local address
303 * with the address specified by setsockopt(IPV6_PKTINFO).
304 * Is it the intended behavior?
306 *plocal_addr6 = in6_selectsrc(sin6, inp->in6p_outputopts,
309 &inp->in6p_laddr, &error);
310 if (*plocal_addr6 == 0) {
312 error = EADDRNOTAVAIL;
316 * Don't do pcblookup call here; return interface in
318 * and exit to caller, that will do the lookup.
322 if (inp->in6p_route.ro_rt)
323 ifp = inp->in6p_route.ro_rt->rt_ifp;
330 * Connect from a socket to a specified address.
331 * Both address and port must be specified in argument sin.
332 * If don't have a local address for this socket yet,
336 in6_pcbconnect(inp, nam, td)
337 register struct inpcb *inp;
338 struct sockaddr *nam;
341 struct in6_addr *addr6;
342 register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
346 * Call inner routine, to assign local interface address.
347 * in6_pcbladdr() may automatically fill in sin6_scope_id.
349 if ((error = in6_pcbladdr(inp, nam, &addr6)) != 0)
352 if (in6_pcblookup_hash(inp->inp_pcbinfo, &sin6->sin6_addr,
354 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
355 ? addr6 : &inp->in6p_laddr,
356 inp->inp_lport, 0, NULL) != NULL) {
359 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
360 if (inp->inp_lport == 0) {
361 error = in6_pcbbind(inp, (struct sockaddr *)0, td);
365 inp->in6p_laddr = *addr6;
367 inp->in6p_faddr = sin6->sin6_addr;
368 inp->inp_fport = sin6->sin6_port;
369 /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
370 inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
371 if (inp->in6p_flags & IN6P_AUTOFLOWLABEL)
372 inp->in6p_flowinfo |=
373 (htonl(ip6_flow_seq++) & IPV6_FLOWLABEL_MASK);
381 * Return an IPv6 address, which is the most appropriate for given
382 * destination and user specified options.
383 * If necessary, this function lookups the routing table and return
384 * an entry to the caller for later use.
387 in6_selectsrc(dstsock, opts, mopts, ro, laddr, errorp)
388 struct sockaddr_in6 *dstsock;
389 struct ip6_pktopts *opts;
390 struct ip6_moptions *mopts;
391 struct route_in6 *ro;
392 struct in6_addr *laddr;
395 struct in6_addr *dst;
396 struct in6_ifaddr *ia6 = 0;
397 struct in6_pktinfo *pi = NULL;
399 dst = &dstsock->sin6_addr;
403 * If the source address is explicitly specified by the caller,
406 if (opts && (pi = opts->ip6po_pktinfo) &&
407 !IN6_IS_ADDR_UNSPECIFIED(&pi->ipi6_addr))
408 return(&pi->ipi6_addr);
411 * If the source address is not specified but the socket(if any)
412 * is already bound, use the bound address.
414 if (laddr && !IN6_IS_ADDR_UNSPECIFIED(laddr))
418 * If the caller doesn't specify the source address but
419 * the outgoing interface, use an address associated with
422 if (pi && pi->ipi6_ifindex) {
423 /* XXX boundary check is assumed to be already done. */
424 ia6 = in6_ifawithscope(ifnet_byindex(pi->ipi6_ifindex), dst);
426 *errorp = EADDRNOTAVAIL;
429 return(&satosin6(&ia6->ia_addr)->sin6_addr);
433 * If the destination address is a link-local unicast address or
434 * a multicast address, and if the outgoing interface is specified
435 * by the sin6_scope_id filed, use an address associated with the
437 * XXX: We're now trying to define more specific semantics of
438 * sin6_scope_id field, so this part will be rewritten in
441 if ((IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MULTICAST(dst)) &&
442 dstsock->sin6_scope_id) {
444 * I'm not sure if boundary check for scope_id is done
447 if (dstsock->sin6_scope_id < 0 ||
448 if_index < dstsock->sin6_scope_id) {
449 *errorp = ENXIO; /* XXX: better error? */
452 ia6 = in6_ifawithscope(ifnet_byindex(dstsock->sin6_scope_id),
455 *errorp = EADDRNOTAVAIL;
458 return(&satosin6(&ia6->ia_addr)->sin6_addr);
462 * If the destination address is a multicast address and
463 * the outgoing interface for the address is specified
464 * by the caller, use an address associated with the interface.
465 * There is a sanity check here; if the destination has node-local
466 * scope, the outgoing interfacde should be a loopback address.
467 * Even if the outgoing interface is not specified, we also
468 * choose a loopback interface as the outgoing interface.
470 if (IN6_IS_ADDR_MULTICAST(dst)) {
471 struct ifnet *ifp = mopts ? mopts->im6o_multicast_ifp : NULL;
473 if (ifp == NULL && IN6_IS_ADDR_MC_NODELOCAL(dst)) {
478 ia6 = in6_ifawithscope(ifp, dst);
480 *errorp = EADDRNOTAVAIL;
483 return(&ia6->ia_addr.sin6_addr);
488 * If the next hop address for the packet is specified
489 * by caller, use an address associated with the route
493 struct sockaddr_in6 *sin6_next;
496 if (opts && opts->ip6po_nexthop) {
497 sin6_next = satosin6(opts->ip6po_nexthop);
498 rt = nd6_lookup(&sin6_next->sin6_addr, 1, NULL);
500 ia6 = in6_ifawithscope(rt->rt_ifp, dst);
502 ia6 = ifatoia6(rt->rt_ifa);
505 *errorp = EADDRNOTAVAIL;
508 return(&satosin6(&ia6->ia_addr)->sin6_addr);
513 * If route is known or can be allocated now,
514 * our src addr is taken from the i/f, else punt.
518 !IN6_ARE_ADDR_EQUAL(&satosin6(&ro->ro_dst)->sin6_addr, dst)) {
520 ro->ro_rt = (struct rtentry *)0;
522 if (ro->ro_rt == (struct rtentry *)0 ||
523 ro->ro_rt->rt_ifp == (struct ifnet *)0) {
524 struct sockaddr_in6 *dst6;
526 /* No route yet, so try to acquire one */
527 bzero(&ro->ro_dst, sizeof(struct sockaddr_in6));
528 dst6 = (struct sockaddr_in6 *)&ro->ro_dst;
529 dst6->sin6_family = AF_INET6;
530 dst6->sin6_len = sizeof(struct sockaddr_in6);
531 dst6->sin6_addr = *dst;
532 if (IN6_IS_ADDR_MULTICAST(dst)) {
533 ro->ro_rt = rtalloc1(&((struct route *)ro)
536 rtalloc((struct route *)ro);
541 * in_pcbconnect() checks out IFF_LOOPBACK to skip using
542 * the address. But we don't know why it does so.
543 * It is necessary to ensure the scope even for lo0
544 * so doesn't check out IFF_LOOPBACK.
548 ia6 = in6_ifawithscope(ro->ro_rt->rt_ifa->ifa_ifp, dst);
549 if (ia6 == 0) /* xxx scope error ?*/
550 ia6 = ifatoia6(ro->ro_rt->rt_ifa);
553 *errorp = EHOSTUNREACH; /* no route */
556 return(&satosin6(&ia6->ia_addr)->sin6_addr);
559 *errorp = EADDRNOTAVAIL;
564 * Default hop limit selection. The precedence is as follows:
565 * 1. Hoplimit valued specified via ioctl.
566 * 2. (If the outgoing interface is detected) the current
567 * hop limit of the interface specified by router advertisement.
568 * 3. The system default hoplimit.
571 in6_selecthlim(in6p, ifp)
575 if (in6p && in6p->in6p_hops >= 0)
576 return(in6p->in6p_hops);
578 return(nd_ifinfo[ifp->if_index].chlim);
585 in6_pcbdisconnect(inp)
588 bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr));
590 /* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
591 inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
593 if (inp->inp_socket->so_state & SS_NOFDREF)
601 struct socket *so = inp->inp_socket;
602 struct inpcbinfo *ipi = inp->inp_pcbinfo;
605 if (inp->in6p_sp != NULL)
606 ipsec6_delete_pcbpolicy(inp);
608 inp->inp_gencnt = ++ipi->ipi_gencnt;
613 if (inp->in6p_options)
614 m_freem(inp->in6p_options);
615 ip6_freepcbopts(inp->in6p_outputopts);
616 ip6_freemoptions(inp->in6p_moptions);
617 if (inp->in6p_route.ro_rt)
618 rtfree(inp->in6p_route.ro_rt);
619 /* Check and free IPv4 related resources in case of mapped addr */
620 if (inp->inp_options)
621 (void)m_free(inp->inp_options);
622 ip_freemoptions(inp->inp_moptions);
625 INP_LOCK_DESTROY(inp);
626 uma_zfree(ipi->ipi_zone, inp);
630 * The calling convention of in6_setsockaddr() and in6_setpeeraddr() was
631 * modified to match the pru_sockaddr() and pru_peeraddr() entry points
632 * in struct pr_usrreqs, so that protocols can just reference then directly
633 * without the need for a wrapper function. The socket must have a valid
634 * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
635 * except through a kernel programming error, so it is acceptable to panic
636 * (or in this case trap) if the PCB is invalid. (Actually, we don't trap
637 * because there actually /is/ a programming error somewhere... XXX)
640 in6_setsockaddr(so, nam)
642 struct sockaddr **nam;
645 register struct inpcb *inp;
646 register struct sockaddr_in6 *sin6;
649 * Do the malloc first in case it blocks.
651 MALLOC(sin6, struct sockaddr_in6 *, sizeof *sin6, M_SONAME, M_WAITOK);
652 bzero(sin6, sizeof *sin6);
653 sin6->sin6_family = AF_INET6;
654 sin6->sin6_len = sizeof(*sin6);
660 free(sin6, M_SONAME);
663 sin6->sin6_port = inp->inp_lport;
664 sin6->sin6_addr = inp->in6p_laddr;
666 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
667 sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]);
669 sin6->sin6_scope_id = 0; /*XXX*/
670 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
671 sin6->sin6_addr.s6_addr16[1] = 0;
673 *nam = (struct sockaddr *)sin6;
678 in6_setpeeraddr(so, nam)
680 struct sockaddr **nam;
684 register struct sockaddr_in6 *sin6;
687 * Do the malloc first in case it blocks.
689 MALLOC(sin6, struct sockaddr_in6 *, sizeof(*sin6), M_SONAME, M_WAITOK);
690 bzero((caddr_t)sin6, sizeof (*sin6));
691 sin6->sin6_family = AF_INET6;
692 sin6->sin6_len = sizeof(struct sockaddr_in6);
698 free(sin6, M_SONAME);
701 sin6->sin6_port = inp->inp_fport;
702 sin6->sin6_addr = inp->in6p_faddr;
704 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
705 sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]);
707 sin6->sin6_scope_id = 0; /*XXX*/
708 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
709 sin6->sin6_addr.s6_addr16[1] = 0;
711 *nam = (struct sockaddr *)sin6;
716 in6_mapped_sockaddr(struct socket *so, struct sockaddr **nam)
718 struct inpcb *inp = sotoinpcb(so);
723 if (inp->inp_vflag & INP_IPV4) {
724 error = in_setsockaddr(so, nam, &tcbinfo);
726 in6_sin_2_v4mapsin6_in_sock(nam);
728 /* scope issues will be handled in in6_setsockaddr(). */
729 error = in6_setsockaddr(so, nam);
735 in6_mapped_peeraddr(struct socket *so, struct sockaddr **nam)
737 struct inpcb *inp = sotoinpcb(so);
742 if (inp->inp_vflag & INP_IPV4) {
743 error = in_setpeeraddr(so, nam, &tcbinfo);
745 in6_sin_2_v4mapsin6_in_sock(nam);
747 /* scope issues will be handled in in6_setpeeraddr(). */
748 error = in6_setpeeraddr(so, nam);
754 * Pass some notification to all connections of a protocol
755 * associated with address dst. The local address and/or port numbers
756 * may be specified to limit the search. The "usual action" will be
757 * taken, depending on the ctlinput cmd. The caller must filter any
758 * cmds that are uninteresting (e.g., no error in the map).
759 * Call the protocol specific routine (if any) to report
760 * any errors for each matching socket.
762 * Must be called at splnet.
765 in6_pcbnotify(head, dst, fport_arg, src, lport_arg, cmd, notify)
766 struct inpcbhead *head;
767 struct sockaddr *dst;
768 const struct sockaddr *src;
769 u_int fport_arg, lport_arg;
771 struct inpcb *(*notify) __P((struct inpcb *, int));
773 struct inpcb *inp, *ninp;
774 struct sockaddr_in6 sa6_src, *sa6_dst;
775 u_short fport = fport_arg, lport = lport_arg;
779 if ((unsigned)cmd > PRC_NCMDS || dst->sa_family != AF_INET6)
782 sa6_dst = (struct sockaddr_in6 *)dst;
783 if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
787 * note that src can be NULL when we get notify by local fragmentation.
789 sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
790 flowinfo = sa6_src.sin6_flowinfo;
793 * Redirects go to all references to the destination,
794 * and use in6_rtchange to invalidate the route cache.
795 * Dead host indications: also use in6_rtchange to invalidate
796 * the cache, and deliver the error to all the sockets.
797 * Otherwise, if we have knowledge of the local port and address,
798 * deliver only to that socket.
800 if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
803 bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
805 if (cmd != PRC_HOSTDEAD)
806 notify = in6_rtchange;
808 errno = inet6ctlerrmap[cmd];
810 for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) {
811 ninp = LIST_NEXT(inp, inp_list);
813 if ((inp->inp_vflag & INP_IPV6) == 0)
817 * Detect if we should notify the error. If no source and
818 * destination ports are specifed, but non-zero flowinfo and
819 * local address match, notify the error. This is the case
820 * when the error is delivered with an encrypted buffer
821 * by ESP. Otherwise, just compare addresses and ports
824 if (lport == 0 && fport == 0 && flowinfo &&
825 inp->inp_socket != NULL &&
826 flowinfo == (inp->in6p_flowinfo & IPV6_FLOWLABEL_MASK) &&
827 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
829 else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
830 &sa6_dst->sin6_addr) ||
831 inp->inp_socket == 0 ||
832 (lport && inp->inp_lport != lport) ||
833 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
834 !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
835 &sa6_src.sin6_addr)) ||
836 (fport && inp->inp_fport != fport))
841 (*notify)(inp, errno);
847 * Lookup a PCB based on the local address and port.
850 in6_pcblookup_local(pcbinfo, laddr, lport_arg, wild_okay)
851 struct inpcbinfo *pcbinfo;
852 struct in6_addr *laddr;
856 register struct inpcb *inp;
857 int matchwild = 3, wildcard;
858 u_short lport = lport_arg;
861 struct inpcbhead *head;
863 * Look for an unconnected (wildcard foreign addr) PCB that
864 * matches the local address and port we're looking for.
866 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0,
868 LIST_FOREACH(inp, head, inp_hash) {
869 if ((inp->inp_vflag & INP_IPV6) == 0)
871 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
872 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
873 inp->inp_lport == lport) {
885 struct inpcbporthead *porthash;
886 struct inpcbport *phd;
887 struct inpcb *match = NULL;
889 * Best fit PCB lookup.
891 * First see if this local port is in use by looking on the
894 porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport,
895 pcbinfo->porthashmask)];
896 LIST_FOREACH(phd, porthash, phd_hash) {
897 if (phd->phd_port == lport)
902 * Port is in use by one or more PCBs. Look for best
905 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
907 if ((inp->inp_vflag & INP_IPV6) == 0)
909 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
911 if (!IN6_IS_ADDR_UNSPECIFIED(
913 if (IN6_IS_ADDR_UNSPECIFIED(laddr))
915 else if (!IN6_ARE_ADDR_EQUAL(
916 &inp->in6p_laddr, laddr))
919 if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
922 if (wildcard < matchwild) {
924 matchwild = wildcard;
925 if (matchwild == 0) {
936 in6_pcbpurgeif0(head, ifp)
941 struct ip6_moptions *im6o;
942 struct in6_multi_mship *imm, *nimm;
944 for (in6p = head; in6p != NULL; in6p = LIST_NEXT(in6p, inp_list)) {
945 im6o = in6p->in6p_moptions;
946 if ((in6p->inp_vflag & INP_IPV6) &&
949 * Unselect the outgoing interface if it is being
952 if (im6o->im6o_multicast_ifp == ifp)
953 im6o->im6o_multicast_ifp = NULL;
956 * Drop multicast group membership if we joined
957 * through the interface being detached.
958 * XXX controversial - is it really legal for kernel
961 for (imm = im6o->im6o_memberships.lh_first;
962 imm != NULL; imm = nimm) {
963 nimm = imm->i6mm_chain.le_next;
964 if (imm->i6mm_maddr->in6m_ifp == ifp) {
965 LIST_REMOVE(imm, i6mm_chain);
966 in6_delmulti(imm->i6mm_maddr);
967 free(imm, M_IPMADDR);
975 * Check for alternatives when higher level complains
976 * about service problems. For now, invalidate cached
977 * routing information. If the route was created dynamically
978 * (by a redirect), time to try a default gateway again.
985 struct rt_addrinfo info;
987 if ((rt = in6p->in6p_route.ro_rt) != NULL) {
988 bzero((caddr_t)&info, sizeof(info));
989 info.rti_flags = rt->rt_flags;
990 info.rti_info[RTAX_DST] = rt_key(rt);
991 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
992 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
993 rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
994 if (rt->rt_flags & RTF_DYNAMIC)
995 (void)rtrequest1(RTM_DELETE, &info, NULL);
996 in6p->in6p_route.ro_rt = NULL;
999 * A new route can be allocated
1000 * the next time output is attempted.
1006 * After a routing change, flush old routing
1007 * and allocate a (hopefully) better one.
1010 in6_rtchange(inp, errno)
1014 if (inp->in6p_route.ro_rt) {
1015 rtfree(inp->in6p_route.ro_rt);
1016 inp->in6p_route.ro_rt = 0;
1018 * A new route can be allocated the next time
1019 * output is attempted.
1026 * Lookup PCB in hash list.
1029 in6_pcblookup_hash(pcbinfo, faddr, fport_arg, laddr, lport_arg, wildcard, ifp)
1030 struct inpcbinfo *pcbinfo;
1031 struct in6_addr *faddr, *laddr;
1032 u_int fport_arg, lport_arg;
1036 struct inpcbhead *head;
1037 register struct inpcb *inp;
1038 u_short fport = fport_arg, lport = lport_arg;
1041 if (faithprefix_p != NULL)
1042 faith = (*faithprefix_p)(laddr);
1047 * First look for an exact match.
1049 head = &pcbinfo->hashbase[INP_PCBHASH(faddr->s6_addr32[3] /* XXX */,
1051 pcbinfo->hashmask)];
1052 LIST_FOREACH(inp, head, inp_hash) {
1053 if ((inp->inp_vflag & INP_IPV6) == 0)
1055 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
1056 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
1057 inp->inp_fport == fport &&
1058 inp->inp_lport == lport) {
1066 struct inpcb *local_wild = NULL;
1068 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0,
1069 pcbinfo->hashmask)];
1070 LIST_FOREACH(inp, head, inp_hash) {
1071 if ((inp->inp_vflag & INP_IPV6) == 0)
1073 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
1074 inp->inp_lport == lport) {
1075 if (faith && (inp->inp_flags & INP_FAITH) == 0)
1077 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
1080 else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
1084 return (local_wild);
1094 init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m)
1098 ip = mtod(m, struct ip6_hdr *);
1099 bzero(sin6, sizeof(*sin6));
1100 sin6->sin6_len = sizeof(*sin6);
1101 sin6->sin6_family = AF_INET6;
1102 sin6->sin6_addr = ip->ip6_src;
1103 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
1104 sin6->sin6_addr.s6_addr16[1] = 0;
1105 sin6->sin6_scope_id =
1106 (m->m_pkthdr.rcvif && IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
1107 ? m->m_pkthdr.rcvif->if_index : 0;