1 //==========================================================================
3 // src/sys/netinet6/in6_pcb.c
5 //==========================================================================
6 //####BSDCOPYRIGHTBEGIN####
8 // -------------------------------------------
10 // Portions of this software may have been derived from OpenBSD,
11 // FreeBSD or other sources, and are covered by the appropriate
12 // copyright disclaimers included herein.
14 // Portions created by Red Hat are
15 // Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
17 // -------------------------------------------
19 //####BSDCOPYRIGHTEND####
20 //==========================================================================
22 /* $FreeBSD: src/sys/netinet6/in6_pcb.c,v 1.10.2.4 2001/08/13 16:26:17 ume Exp $ */
23 /* $KAME: in6_pcb.c,v 1.42 2001/11/13 03:09:44 jinmei Exp $ */
26 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
27 * All rights reserved.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. Neither the name of the project nor the names of its contributors
38 * may be used to endorse or promote products derived from this software
39 * without specific prior written permission.
41 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 * Copyright (c) 1982, 1986, 1991, 1993
57 * The Regents of the University of California. All rights reserved.
59 * Redistribution and use in source and binary forms, with or without
60 * modification, are permitted provided that the following conditions
62 * 1. Redistributions of source code must retain the above copyright
63 * notice, this list of conditions and the following disclaimer.
64 * 2. Redistributions in binary form must reproduce the above copyright
65 * notice, this list of conditions and the following disclaimer in the
66 * documentation and/or other materials provided with the distribution.
67 * 3. All advertising materials mentioning features or use of this software
68 * must display the following acknowledgement:
69 * This product includes software developed by the University of
70 * California, Berkeley and its contributors.
71 * 4. Neither the name of the University nor the names of its contributors
72 * may be used to endorse or promote products derived from this software
73 * without specific prior written permission.
75 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
76 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
77 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
78 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
79 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
80 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
81 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
82 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
83 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
84 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
87 * @(#)in_pcb.c 8.2 (Berkeley) 1/4/94
90 #include <sys/param.h>
91 #include <sys/malloc.h>
93 #include <sys/domain.h>
94 #include <sys/protosw.h>
95 #include <sys/socket.h>
96 #include <sys/socketvar.h>
97 #include <sys/sockio.h>
98 #include <sys/errno.h>
101 #include <net/if_types.h>
102 #include <net/route.h>
104 #include <netinet/in.h>
105 #include <netinet/in_var.h>
106 #include <netinet/in_systm.h>
107 #include <netinet/ip6.h>
108 #include <netinet/ip_var.h>
109 #include <netinet6/ip6_var.h>
110 #include <netinet6/nd6.h>
111 #include <netinet/in_pcb.h>
112 #include <netinet6/in6_pcb.h>
113 #include <netinet6/scope6_var.h>
116 #include <netinet6/ipsec.h>
117 #include <netinet6/ah.h>
118 #include <netkey/key.h>
121 struct in6_addr zeroin6_addr;
124 in6_pcbbind(inp, nam, p)
125 register struct inpcb *inp;
126 struct sockaddr *nam;
129 struct socket *so = inp->inp_socket;
130 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL;
131 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
133 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
135 if (!in6_ifaddr) /* XXX broken! */
136 return (EADDRNOTAVAIL);
137 if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
139 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
144 sin6 = (struct sockaddr_in6 *)nam;
145 if (nam->sa_len != sizeof(*sin6))
150 if (nam->sa_family != AF_INET6)
151 return(EAFNOSUPPORT);
153 if ((error = scope6_check_id(sin6, ip6_use_defzone)) != 0)
155 #ifndef SCOPEDROUTING
156 /* this must be cleared for ifa_ifwithaddr() */
157 sin6->sin6_scope_id = 0;
160 lport = sin6->sin6_port;
161 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
163 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
164 * allow compepte duplication of binding if
165 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
166 * and a multicast address is bound on both
167 * new and duplicated sockets.
169 if (so->so_options & SO_REUSEADDR)
170 reuseport = SO_REUSEADDR|SO_REUSEPORT;
171 } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
172 struct ifaddr *ia = NULL;
174 sin6->sin6_port = 0; /* yech... */
175 if ((ia = ifa_ifwithaddr((struct sockaddr *)sin6)) == 0)
176 return(EADDRNOTAVAIL);
179 * XXX: bind to an anycast address might accidentally
180 * cause sending a packet with anycast source address.
181 * We should allow to bind to a deprecated address, since
182 * the application dare to use it.
185 ((struct in6_ifaddr *)ia)->ia6_flags &
186 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) {
187 return(EADDRNOTAVAIL);
194 t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr,
196 if (t && (reuseport & t->inp_socket->so_options) == 0)
198 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
199 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
200 struct sockaddr_in sin;
202 in6_sin6_2_sin(&sin, sin6);
203 t = in_pcblookup_local(pcbinfo, sin.sin_addr,
206 (reuseport & t->inp_socket->so_options)
208 (ntohl(t->inp_laddr.s_addr)
211 INP_SOCKAF(t->inp_socket)))
215 inp->in6p_laddr = sin6->sin6_addr;
219 if ((e = in6_pcbsetport(&inp->in6p_laddr, inp, p)) != 0)
223 inp->inp_lport = lport;
224 if (in_pcbinshash(inp) != 0) {
225 inp->in6p_laddr = in6addr_any;
234 * Transform old in6_pcbconnect() into an inner subroutine for new
235 * in6_pcbconnect(): Do some validity-checking on the remote
236 * address (in mbuf 'nam') and then determine local host address
237 * (i.e., which interface) to use to access that remote host.
239 * This preserves definition of in6_pcbconnect(), while supporting a
240 * slightly different version for T/TCP. (This is more than
241 * a bit of a kludge, but cleaning up the internal interfaces would
242 * have forced minor changes in every protocol).
246 in6_pcbladdr(inp, nam, plocal_addr6)
247 register struct inpcb *inp;
248 struct sockaddr *nam;
249 struct in6_addr **plocal_addr6;
251 register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
254 if (nam->sa_len != sizeof (*sin6))
256 if (sin6->sin6_family != AF_INET6)
257 return (EAFNOSUPPORT);
258 if (sin6->sin6_port == 0)
259 return (EADDRNOTAVAIL);
261 if ((error = scope6_check_id(sin6, ip6_use_defzone)) != 0)
263 #ifndef SCOPEDROUTING
264 sin6->sin6_scope_id = 0;
269 * If the destination address is UNSPECIFIED addr,
270 * use the loopback addr, e.g ::1.
272 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
273 sin6->sin6_addr = in6addr_loopback;
276 struct ifnet *ifp = NULL;
278 *plocal_addr6 = in6_selectsrc(sin6, inp->in6p_outputopts,
281 &ifp, &inp->in6p_laddr, &error);
282 if (ifp && sin6->sin6_scope_id == 0 &&
283 (error = scope6_setzoneid(ifp, sin6)) != 0) { /* XXX */
287 if (*plocal_addr6 == 0) {
289 error = EADDRNOTAVAIL;
293 * Don't do pcblookup call here; return interface in
295 * and exit to caller, that will do the lookup.
304 * Connect from a socket to a specified address.
305 * Both address and port must be specified in argument sin.
306 * If don't have a local address for this socket yet,
310 in6_pcbconnect(inp, nam, p)
311 register struct inpcb *inp;
312 struct sockaddr *nam;
315 struct in6_addr *addr6;
316 register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
320 * Call inner routine, to assign local interface address.
321 * in6_pcbladdr() may automatically fill in sin6_scope_id.
323 if ((error = in6_pcbladdr(inp, nam, &addr6)) != 0)
326 if (in6_pcblookup_hash(inp->inp_pcbinfo, &sin6->sin6_addr,
328 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
329 ? addr6 : &inp->in6p_laddr,
330 inp->inp_lport, 0, NULL) != NULL) {
333 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
334 if (inp->inp_lport == 0) {
335 error = in6_pcbbind(inp, (struct sockaddr *)0, p);
339 inp->in6p_laddr = *addr6;
341 inp->in6p_faddr = sin6->sin6_addr;
342 inp->inp_fport = sin6->sin6_port;
343 /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
344 inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
345 if (inp->in6p_flags & IN6P_AUTOFLOWLABEL)
346 inp->in6p_flowinfo |=
347 (htonl(ip6_flow_seq++) & IPV6_FLOWLABEL_MASK);
355 * Return an IPv6 address, which is the most appropriate for given
356 * destination and user specified options.
357 * If necessary, this function lookups the routing table and return
358 * an entry to the caller for later use.
361 in6_selectsrc(dstsock, opts, mopts, ro, laddr, errorp)
362 struct sockaddr_in6 *dstsock;
363 struct ip6_pktopts *opts;
364 struct ip6_moptions *mopts;
365 #ifdef NEW_STRUCT_ROUTE
368 struct route_in6 *ro;
370 struct in6_addr *laddr;
373 struct in6_addr *dst;
374 struct in6_ifaddr *ia6 = 0;
375 struct in6_pktinfo *pi = NULL;
377 dst = &dstsock->sin6_addr;
381 * If the source address is explicitly specified by the caller,
384 if (opts && (pi = opts->ip6po_pktinfo) &&
385 !IN6_IS_ADDR_UNSPECIFIED(&pi->ipi6_addr))
386 return(&pi->ipi6_addr);
389 * If the source address is not specified but the socket(if any)
390 * is already bound, use the bound address.
392 if (laddr && !IN6_IS_ADDR_UNSPECIFIED(laddr))
396 * If the caller doesn't specify the source address but
397 * the outgoing interface, use an address associated with
400 if (pi && pi->ipi6_ifindex) {
401 /* XXX boundary check is assumed to be already done. */
402 ia6 = in6_ifawithscope(ifindex2ifnet[pi->ipi6_ifindex],
405 *errorp = EADDRNOTAVAIL;
408 return(&satosin6(&ia6->ia_addr)->sin6_addr);
412 * If the destination address is a link-local unicast address or
413 * a multicast address, and if the outgoing interface is specified
414 * by the sin6_scope_id filed, use an address associated with the
416 * XXX: We're now trying to define more specific semantics of
417 * sin6_scope_id field, so this part will be rewritten in
420 if ((IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MULTICAST(dst)) &&
421 dstsock->sin6_scope_id) {
423 * I'm not sure if boundary check for scope_id is done
426 if (dstsock->sin6_scope_id < 0 ||
427 if_index < dstsock->sin6_scope_id) {
428 *errorp = ENXIO; /* XXX: better error? */
431 ia6 = in6_ifawithscope(ifindex2ifnet[dstsock->sin6_scope_id],
434 *errorp = EADDRNOTAVAIL;
437 return(&satosin6(&ia6->ia_addr)->sin6_addr);
441 * If the destination address is a multicast address and
442 * the outgoing interface for the address is specified
443 * by the caller, use an address associated with the interface.
444 * There is a sanity check here; if the destination has node-local
445 * scope, the outgoing interfacde should be a loopback address.
446 * Even if the outgoing interface is not specified, we also
447 * choose a loopback interface as the outgoing interface.
449 if (IN6_IS_ADDR_MULTICAST(dst)) {
450 struct ifnet *ifp = mopts ? mopts->im6o_multicast_ifp : NULL;
452 if (ifp == NULL && IN6_IS_ADDR_MC_NODELOCAL(dst)) {
457 ia6 = in6_ifawithscope(ifp, dst);
459 *errorp = EADDRNOTAVAIL;
462 return(&ia6->ia_addr.sin6_addr);
467 * If the next hop address for the packet is specified
468 * by caller, use an address associated with the route
472 struct sockaddr_in6 *sin6_next;
475 if (opts && opts->ip6po_nexthop) {
476 sin6_next = satosin6(opts->ip6po_nexthop);
477 rt = nd6_lookup(&sin6_next->sin6_addr, 1, NULL);
479 ia6 = in6_ifawithscope(rt->rt_ifp, dst);
481 ia6 = ifatoia6(rt->rt_ifa);
484 *errorp = EADDRNOTAVAIL;
487 return(&satosin6(&ia6->ia_addr)->sin6_addr);
492 * If route is known or can be allocated now,
493 * our src addr is taken from the i/f, else punt.
497 !IN6_ARE_ADDR_EQUAL(&satosin6(&ro->ro_dst)->sin6_addr, dst)) {
499 ro->ro_rt = (struct rtentry *)0;
501 if (ro->ro_rt == (struct rtentry *)0 ||
502 ro->ro_rt->rt_ifp == (struct ifnet *)0) {
503 struct sockaddr_in6 *dst6;
505 /* No route yet, so try to acquire one */
506 bzero(&ro->ro_dst, sizeof(struct sockaddr_in6));
507 dst6 = (struct sockaddr_in6 *)&ro->ro_dst;
508 dst6->sin6_family = AF_INET6;
509 dst6->sin6_len = sizeof(struct sockaddr_in6);
510 dst6->sin6_addr = *dst;
511 if (IN6_IS_ADDR_MULTICAST(dst)) {
512 ro->ro_rt = rtalloc1(&((struct route *)ro)
515 rtalloc((struct route *)ro);
520 * in_pcbconnect() checks out IFF_LOOPBACK to skip using
521 * the address. But we don't know why it does so.
522 * It is necessary to ensure the scope even for lo0
523 * so doesn't check out IFF_LOOPBACK.
527 ia6 = in6_ifawithscope(ro->ro_rt->rt_ifa->ifa_ifp, dst);
528 if (ia6 == 0) /* xxx scope error ?*/
529 ia6 = ifatoia6(ro->ro_rt->rt_ifa);
532 *errorp = EHOSTUNREACH; /* no route */
535 return(&satosin6(&ia6->ia_addr)->sin6_addr);
538 *errorp = EADDRNOTAVAIL;
543 * Default hop limit selection. The precedence is as follows:
544 * 1. Hoplimit valued specified via ioctl.
545 * 2. (If the outgoing interface is detected) the current
546 * hop limit of the interface specified by router advertisement.
547 * 3. The system default hoplimit.
550 in6_selecthlim(in6p, ifp)
554 if (in6p && in6p->in6p_hops >= 0)
555 return(in6p->in6p_hops);
557 return(nd_ifinfo[ifp->if_index].chlim);
564 in6_pcbdisconnect(inp)
567 bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr));
569 /* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
570 inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
572 if (inp->inp_socket->so_state & SS_NOFDREF)
580 struct socket *so = inp->inp_socket;
581 struct inpcbinfo *ipi = inp->inp_pcbinfo;
584 if (inp->in6p_sp != NULL)
585 ipsec6_delete_pcbpolicy(inp);
587 inp->inp_gencnt = ++ipi->ipi_gencnt;
592 if (inp->in6p_inputopts) /* Free all received options. */
593 m_freem(inp->in6p_inputopts->head); /* this is safe */
594 ip6_freepcbopts(inp->in6p_outputopts);
595 ip6_freemoptions(inp->in6p_moptions);
596 if (inp->in6p_route.ro_rt)
597 rtfree(inp->in6p_route.ro_rt);
598 /* Check and free IPv4 related resources in case of mapped addr */
599 if (inp->inp_options)
600 (void)m_free(inp->inp_options);
601 ip_freemoptions(inp->inp_moptions);
604 zfreei(ipi->ipi_zone, inp);
608 * The calling convention of in6_setsockaddr() and in6_setpeeraddr() was
609 * modified to match the pru_sockaddr() and pru_peeraddr() entry points
610 * in struct pr_usrreqs, so that protocols can just reference then directly
611 * without the need for a wrapper function. The socket must have a valid
612 * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
613 * except through a kernel programming error, so it is acceptable to panic
614 * (or in this case trap) if the PCB is invalid. (Actually, we don't trap
615 * because there actually /is/ a programming error somewhere... XXX)
618 in6_setsockaddr(so, nam)
620 struct sockaddr **nam;
623 register struct inpcb *inp;
624 register struct sockaddr_in6 *sin6;
627 * Do the malloc first in case it blocks.
629 MALLOC(sin6, struct sockaddr_in6 *, sizeof *sin6, M_SONAME, M_WAITOK);
630 bzero(sin6, sizeof *sin6);
631 sin6->sin6_family = AF_INET6;
632 sin6->sin6_len = sizeof(*sin6);
638 free(sin6, M_SONAME);
641 sin6->sin6_port = inp->inp_lport;
642 sin6->sin6_addr = inp->in6p_laddr;
645 #ifndef SCOPEDROUTING
646 in6_recoverscope(sin6, &inp->in6p_laddr, NULL);
649 *nam = (struct sockaddr *)sin6;
654 in6_setpeeraddr(so, nam)
656 struct sockaddr **nam;
660 register struct sockaddr_in6 *sin6;
663 * Do the malloc first in case it blocks.
665 MALLOC(sin6, struct sockaddr_in6 *, sizeof(*sin6), M_SONAME, M_WAITOK);
666 bzero((caddr_t)sin6, sizeof (*sin6));
667 sin6->sin6_family = AF_INET6;
668 sin6->sin6_len = sizeof(struct sockaddr_in6);
674 free(sin6, M_SONAME);
677 sin6->sin6_port = inp->inp_fport;
678 sin6->sin6_addr = inp->in6p_faddr;
681 #ifndef SCOPEDROUTING
682 in6_recoverscope(sin6, &inp->in6p_faddr, NULL);
685 *nam = (struct sockaddr *)sin6;
690 in6_mapped_sockaddr(struct socket *so, struct sockaddr **nam)
692 struct inpcb *inp = sotoinpcb(so);
697 if (inp->inp_vflag & INP_IPV4) {
698 error = in_setsockaddr(so, nam);
700 in6_sin_2_v4mapsin6_in_sock(nam);
702 /* scope issues will be handled in in6_setsockaddr(). */
703 error = in6_setsockaddr(so, nam);
709 in6_mapped_peeraddr(struct socket *so, struct sockaddr **nam)
711 struct inpcb *inp = sotoinpcb(so);
716 if (inp->inp_vflag & INP_IPV4) {
717 error = in_setpeeraddr(so, nam);
719 in6_sin_2_v4mapsin6_in_sock(nam);
721 /* scope issues will be handled in in6_setpeeraddr(). */
722 error = in6_setpeeraddr(so, nam);
728 * Pass some notification to all connections of a protocol
729 * associated with address dst. The local address and/or port numbers
730 * may be specified to limit the search. The "usual action" will be
731 * taken, depending on the ctlinput cmd. The caller must filter any
732 * cmds that are uninteresting (e.g., no error in the map).
733 * Call the protocol specific routine (if any) to report
734 * any errors for each matching socket.
736 * Must be called at splnet.
739 in6_pcbnotify(head, dst, fport_arg, src, lport_arg, cmd, cmdarg, notify)
740 struct inpcbhead *head;
741 struct sockaddr *dst, *src;
742 u_int fport_arg, lport_arg;
745 void (*notify) __P((struct inpcb *, int));
747 struct inpcb *inp, *ninp;
748 struct sockaddr_in6 sa6_src, *sa6_dst;
749 u_short fport = fport_arg, lport = lport_arg;
753 if ((unsigned)cmd > PRC_NCMDS || dst->sa_family != AF_INET6)
756 sa6_dst = (struct sockaddr_in6 *)dst;
757 if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
761 * note that src can be NULL when we get notify by local fragmentation.
763 sa6_src = (src == NULL) ? sa6_any : *(struct sockaddr_in6 *)src;
764 flowinfo = sa6_src.sin6_flowinfo;
767 * Redirects go to all references to the destination,
768 * and use in6_rtchange to invalidate the route cache.
769 * Dead host indications: also use in6_rtchange to invalidate
770 * the cache, and deliver the error to all the sockets.
771 * Otherwise, if we have knowledge of the local port and address,
772 * deliver only to that socket.
774 if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
777 bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
779 if (cmd != PRC_HOSTDEAD)
780 notify = in6_rtchange;
782 _errno = inet6ctlerrmap[cmd];
784 for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) {
785 ninp = LIST_NEXT(inp, inp_list);
787 if ((inp->inp_vflag & INP_IPV6) == 0)
791 * If the error designates a new path MTU for a destination
792 * and the application (associated with this socket) wanted to
793 * know the value, notify. Note that we notify for all
794 * disconnected sockets if the corresponding application
795 * wanted. This is because some UDP applications keep sending
796 * sockets disconnected.
797 * XXX: should we avoid to notify the value to TCP sockets?
799 if (cmd == PRC_MSGSIZE && (inp->inp_flags & IN6P_MTU) != 0 &&
800 (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
801 IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
802 &sa6_dst->sin6_addr))) {
803 ip6_notify_pmtu(inp, (struct sockaddr_in6 *)dst,
804 (u_int32_t *)cmdarg);
808 * Detect if we should notify the error. If no source and
809 * destination ports are specifed, but non-zero flowinfo and
810 * local address match, notify the error. This is the case
811 * when the error is delivered with an encrypted buffer
812 * by ESP. Otherwise, just compare addresses and ports
815 if (lport == 0 && fport == 0 && flowinfo &&
816 inp->inp_socket != NULL &&
817 flowinfo == (inp->in6p_flowinfo & IPV6_FLOWLABEL_MASK) &&
818 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
820 else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
821 &sa6_dst->sin6_addr) ||
822 inp->inp_socket == 0 ||
823 (lport && inp->inp_lport != lport) ||
824 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
825 !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
826 &sa6_src.sin6_addr)) ||
827 (fport && inp->inp_fport != fport))
832 (*notify)(inp, _errno);
838 * Lookup a PCB based on the local address and port.
841 in6_pcblookup_local(pcbinfo, laddr, lport_arg, wild_okay)
842 struct inpcbinfo *pcbinfo;
843 struct in6_addr *laddr;
847 register struct inpcb *inp;
848 int matchwild = 3, wildcard;
849 u_short lport = lport_arg;
852 struct inpcbhead *head;
854 * Look for an unconnected (wildcard foreign addr) PCB that
855 * matches the local address and port we're looking for.
857 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0,
859 LIST_FOREACH(inp, head, inp_hash) {
860 if ((inp->inp_vflag & INP_IPV6) == 0)
862 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
863 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
864 inp->inp_lport == lport) {
876 struct inpcbporthead *porthash;
877 struct inpcbport *phd;
878 struct inpcb *match = NULL;
880 * Best fit PCB lookup.
882 * First see if this local port is in use by looking on the
885 porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport,
886 pcbinfo->porthashmask)];
887 LIST_FOREACH(phd, porthash, phd_hash) {
888 if (phd->phd_port == lport)
893 * Port is in use by one or more PCBs. Look for best
896 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
898 if ((inp->inp_vflag & INP_IPV6) == 0)
900 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
902 if (!IN6_IS_ADDR_UNSPECIFIED(
904 if (IN6_IS_ADDR_UNSPECIFIED(laddr))
906 else if (!IN6_ARE_ADDR_EQUAL(
907 &inp->in6p_laddr, laddr))
910 if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
913 if (wildcard < matchwild) {
915 matchwild = wildcard;
916 if (matchwild == 0) {
927 in6_pcbpurgeif0(head, ifp)
932 struct ip6_moptions *im6o;
933 struct in6_multi_mship *imm, *nimm;
935 for (in6p = head; in6p != NULL; in6p = LIST_NEXT(in6p, inp_list)) {
936 im6o = in6p->in6p_moptions;
937 if ((in6p->inp_vflag & INP_IPV6) &&
940 * Unselect the outgoing interface if it is being
943 if (im6o->im6o_multicast_ifp == ifp)
944 im6o->im6o_multicast_ifp = NULL;
947 * Drop multicast group membership if we joined
948 * through the interface being detached.
949 * XXX controversial - is it really legal for kernel
952 for (imm = im6o->im6o_memberships.lh_first;
953 imm != NULL; imm = nimm) {
954 nimm = imm->i6mm_chain.le_next;
955 if (imm->i6mm_maddr->in6m_ifp == ifp) {
956 LIST_REMOVE(imm, i6mm_chain);
957 in6_delmulti(imm->i6mm_maddr);
958 free(imm, M_IPMADDR);
966 * Check for alternatives when higher level complains
967 * about service problems. For now, invalidate cached
968 * routing information. If the route was created dynamically
969 * (by a redirect), time to try a default gateway again.
976 struct rt_addrinfo info;
978 if ((rt = in6p->in6p_route.ro_rt) != NULL) {
979 bzero((caddr_t)&info, sizeof(info));
980 info.rti_info[RTAX_DST] =
981 (struct sockaddr *)&in6p->in6p_route.ro_dst;
982 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
983 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
984 rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
985 if (rt->rt_flags & RTF_DYNAMIC)
986 (void)rtrequest(RTM_DELETE, rt_key(rt),
987 rt->rt_gateway, rt_mask(rt), rt->rt_flags,
988 (struct rtentry **)0);
989 in6p->in6p_route.ro_rt = NULL;
992 * A new route can be allocated
993 * the next time output is attempted.
999 * After a routing change, flush old routing
1000 * and allocate a (hopefully) better one.
1003 in6_rtchange(inp, _errno)
1007 if (inp->in6p_route.ro_rt) {
1008 rtfree(inp->in6p_route.ro_rt);
1009 inp->in6p_route.ro_rt = 0;
1011 * A new route can be allocated the next time
1012 * output is attempted.
1018 * Lookup PCB in hash list.
1021 in6_pcblookup_hash(pcbinfo, faddr, fport_arg, laddr, lport_arg, wildcard, ifp)
1022 struct inpcbinfo *pcbinfo;
1023 struct in6_addr *faddr, *laddr;
1024 u_int fport_arg, lport_arg;
1028 struct inpcbhead *head;
1029 register struct inpcb *inp;
1030 u_short fport = fport_arg, lport = lport_arg;
1033 #if defined(NFAITH) && NFAITH > 0
1034 faith = faithprefix(laddr);
1040 * First look for an exact match.
1042 head = &pcbinfo->hashbase[INP_PCBHASH(faddr->s6_addr32[3] /* XXX */,
1044 pcbinfo->hashmask)];
1045 LIST_FOREACH(inp, head, inp_hash) {
1046 if ((inp->inp_vflag & INP_IPV6) == 0)
1048 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
1049 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
1050 inp->inp_fport == fport &&
1051 inp->inp_lport == lport) {
1059 struct inpcb *local_wild = NULL;
1061 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0,
1062 pcbinfo->hashmask)];
1063 LIST_FOREACH(inp, head, inp_hash) {
1064 if ((inp->inp_vflag & INP_IPV6) == 0)
1066 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
1067 inp->inp_lport == lport) {
1068 if (faith && (inp->inp_flags & INP_FAITH) == 0)
1070 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
1073 else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
1077 return (local_wild);
1087 init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m)
1091 ip = mtod(m, struct ip6_hdr *);
1092 bzero(sin6, sizeof(*sin6));
1093 sin6->sin6_len = sizeof(*sin6);
1094 sin6->sin6_family = AF_INET6;
1095 sin6->sin6_addr = ip->ip6_src;
1096 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
1097 sin6->sin6_addr.s6_addr16[1] = 0;
1098 sin6->sin6_scope_id =
1099 (m->m_pkthdr.rcvif && IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
1100 ? m->m_pkthdr.rcvif->if_index : 0;