]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netinet6/in6_pcb.c
2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * Copyright (c) 1982, 1986, 1991, 1993
33 * The Regents of the University of California. All rights reserved.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. All advertising materials mentioning features or use of this software
44 * must display the following acknowledgement:
45 * This product includes software developed by the University of
46 * California, Berkeley and its contributors.
47 * 4. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * @(#)in_pcb.c 8.2 (Berkeley) 1/4/94
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/malloc.h>
70 #include <sys/domain.h>
71 #include <sys/protosw.h>
72 #include <sys/socket.h>
73 #include <sys/socketvar.h>
74 #include <sys/sockio.h>
75 #include <sys/errno.h>
80 #include <net/if_types.h>
81 #include <net/route.h>
83 #include <netinet/in.h>
84 #include <netinet/in_var.h>
85 #include <netinet/in_systm.h>
86 #include <netinet/ip6.h>
87 #include <netinet/ip_var.h>
88 #include <netinet6/ip6_var.h>
89 #include <netinet6/nd6.h>
90 #include <netinet/in_pcb.h>
91 #include <netinet6/in6_pcb.h>
92 #include <net/if_types.h>
95 #if defined(NFAITH) && NFAITH > 0
96 #include <net/if_faith.h>
100 #include <netinet6/ipsec.h>
102 #include <netinet6/ipsec6.h>
104 #include <netinet6/ah.h>
106 #include <netinet6/ah6.h>
108 #include <netkey/key.h>
111 struct in6_addr zeroin6_addr
;
114 in6_pcbbind(inp
, nam
, p
)
115 register struct inpcb
*inp
;
116 struct sockaddr
*nam
;
119 struct socket
*so
= inp
->inp_socket
;
120 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)NULL
;
121 struct inpcbinfo
*pcbinfo
= inp
->inp_pcbinfo
;
123 int wild
= 0, reuseport
= (so
->so_options
& SO_REUSEPORT
);
125 if (!in6_ifaddr
) /* XXX broken! */
126 return (EADDRNOTAVAIL
);
127 if (inp
->inp_lport
|| !IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
))
129 if ((so
->so_options
& (SO_REUSEADDR
|SO_REUSEPORT
)) == 0)
132 sin6
= (struct sockaddr_in6
*)nam
;
133 if (nam
->sa_len
!= sizeof(*sin6
))
138 if (nam
->sa_family
!= AF_INET6
)
139 return(EAFNOSUPPORT
);
141 /* KAME hack: embed scopeid */
142 if (in6_embedscope(&sin6
->sin6_addr
, sin6
, inp
, NULL
) != 0)
144 /* this must be cleared for ifa_ifwithaddr() */
145 sin6
->sin6_scope_id
= 0;
147 lport
= sin6
->sin6_port
;
148 if (IN6_IS_ADDR_MULTICAST(&sin6
->sin6_addr
)) {
150 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
151 * allow compepte duplication of binding if
152 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
153 * and a multicast address is bound on both
154 * new and duplicated sockets.
156 if (so
->so_options
& SO_REUSEADDR
)
157 reuseport
= SO_REUSEADDR
|SO_REUSEPORT
;
158 } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6
->sin6_addr
)) {
159 struct ifaddr
*ia
= NULL
;
161 sin6
->sin6_port
= 0; /* yech... */
162 if ((ia
= ifa_ifwithaddr((struct sockaddr
*)sin6
)) == 0)
163 return(EADDRNOTAVAIL
);
166 * XXX: bind to an anycast address might accidentally
167 * cause sending a packet with anycast source address.
168 * We should allow to bind to a deprecated address, since
169 * the application dare to use it.
172 ((struct in6_ifaddr
*)ia
)->ia6_flags
&
173 (IN6_IFF_ANYCAST
|IN6_IFF_NOTREADY
|IN6_IFF_DETACHED
)) {
174 return(EADDRNOTAVAIL
);
181 if (ntohs(lport
) < IPV6PORT_RESERVED
&& p
&&
183 suser(p
->p_ucred
, &p
->p_acflag
))
185 ((so
->so_state
& SS_PRIV
) == 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_uid
!= t
->inp_socket
->so_uid
)
201 if ((inp
->inp_flags
& IN6P_IPV6_V6ONLY
) == 0 &&
202 IN6_IS_ADDR_UNSPECIFIED(&sin6
->sin6_addr
)) {
203 struct sockaddr_in sin
;
205 in6_sin6_2_sin(&sin
, sin6
);
206 t
= in_pcblookup_local(pcbinfo
,
211 t
->inp_socket
->so_uid
) &&
212 (ntohl(t
->inp_laddr
.s_addr
) !=
215 INP_SOCKAF(t
->inp_socket
)))
219 t
= in6_pcblookup_local(pcbinfo
, &sin6
->sin6_addr
,
221 if (t
&& (reuseport
& t
->inp_socket
->so_options
) == 0)
223 if ((inp
->inp_flags
& IN6P_IPV6_V6ONLY
) == 0 &&
224 IN6_IS_ADDR_UNSPECIFIED(&sin6
->sin6_addr
)) {
225 struct sockaddr_in sin
;
227 in6_sin6_2_sin(&sin
, sin6
);
228 t
= in_pcblookup_local(pcbinfo
, sin
.sin_addr
,
231 (reuseport
& t
->inp_socket
->so_options
)
233 (ntohl(t
->inp_laddr
.s_addr
)
236 INP_SOCKAF(t
->inp_socket
)))
240 inp
->in6p_laddr
= sin6
->sin6_addr
;
244 if ((e
= in6_pcbsetport(&inp
->in6p_laddr
, inp
, p
)) != 0)
248 inp
->inp_lport
= lport
;
249 if (in_pcbinshash(inp
) != 0) {
250 inp
->in6p_laddr
= in6addr_any
;
259 * Transform old in6_pcbconnect() into an inner subroutine for new
260 * in6_pcbconnect(): Do some validity-checking on the remote
261 * address (in mbuf 'nam') and then determine local host address
262 * (i.e., which interface) to use to access that remote host.
264 * This preserves definition of in6_pcbconnect(), while supporting a
265 * slightly different version for T/TCP. (This is more than
266 * a bit of a kludge, but cleaning up the internal interfaces would
267 * have forced minor changes in every protocol).
271 in6_pcbladdr(inp
, nam
, plocal_addr6
)
272 register struct inpcb
*inp
;
273 struct sockaddr
*nam
;
274 struct in6_addr
**plocal_addr6
;
276 register struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)nam
;
277 struct ifnet
*ifp
= NULL
;
280 if (nam
->sa_len
!= sizeof (*sin6
))
282 if (sin6
->sin6_family
!= AF_INET6
)
283 return (EAFNOSUPPORT
);
284 if (sin6
->sin6_port
== 0)
285 return (EADDRNOTAVAIL
);
287 /* KAME hack: embed scopeid */
288 if (in6_embedscope(&sin6
->sin6_addr
, sin6
, inp
, &ifp
) != 0)
293 * If the destination address is UNSPECIFIED addr,
294 * use the loopback addr, e.g ::1.
296 if (IN6_IS_ADDR_UNSPECIFIED(&sin6
->sin6_addr
))
297 sin6
->sin6_addr
= in6addr_loopback
;
301 * XXX: in6_selectsrc might replace the bound local address
302 * with the address specified by setsockopt(IPV6_PKTINFO).
303 * Is it the intended behavior?
305 *plocal_addr6
= in6_selectsrc(sin6
, inp
->in6p_outputopts
,
308 &inp
->in6p_laddr
, &error
);
309 if (*plocal_addr6
== 0) {
311 error
= EADDRNOTAVAIL
;
315 * Don't do pcblookup call here; return interface in
317 * and exit to caller, that will do the lookup.
321 if (inp
->in6p_route
.ro_rt
)
322 ifp
= inp
->in6p_route
.ro_rt
->rt_ifp
;
329 * Connect from a socket to a specified address.
330 * Both address and port must be specified in argument sin.
331 * If don't have a local address for this socket yet,
335 in6_pcbconnect(inp
, nam
, p
)
336 register struct inpcb
*inp
;
337 struct sockaddr
*nam
;
340 struct in6_addr
*addr6
;
341 register struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)nam
;
345 * Call inner routine, to assign local interface address.
347 if ((error
= in6_pcbladdr(inp
, nam
, &addr6
)) != 0)
350 if (in6_pcblookup_hash(inp
->inp_pcbinfo
, &sin6
->sin6_addr
,
352 IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
)
353 ? addr6
: &inp
->in6p_laddr
,
354 inp
->inp_lport
, 0, NULL
) != NULL
) {
357 if (IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
)) {
358 if (inp
->inp_lport
== 0) {
359 error
= in6_pcbbind(inp
, (struct sockaddr
*)0, p
);
363 inp
->in6p_laddr
= *addr6
;
365 inp
->in6p_faddr
= sin6
->sin6_addr
;
366 inp
->inp_fport
= sin6
->sin6_port
;
367 /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
368 inp
->in6p_flowinfo
&= ~IPV6_FLOWLABEL_MASK
;
369 if (inp
->in6p_flags
& IN6P_AUTOFLOWLABEL
)
370 inp
->in6p_flowinfo
|=
371 (htonl(ip6_flow_seq
++) & IPV6_FLOWLABEL_MASK
);
379 * Return an IPv6 address, which is the most appropriate for given
380 * destination and user specified options.
381 * If necessary, this function lookups the routing table and return
382 * an entry to the caller for later use.
385 in6_selectsrc(dstsock
, opts
, mopts
, ro
, laddr
, errorp
)
386 struct sockaddr_in6
*dstsock
;
387 struct ip6_pktopts
*opts
;
388 struct ip6_moptions
*mopts
;
389 struct route_in6
*ro
;
390 struct in6_addr
*laddr
;
393 struct in6_addr
*dst
;
394 struct in6_ifaddr
*ia6
= 0;
395 struct in6_pktinfo
*pi
= NULL
;
397 dst
= &dstsock
->sin6_addr
;
401 * If the source address is explicitly specified by the caller,
404 if (opts
&& (pi
= opts
->ip6po_pktinfo
) &&
405 !IN6_IS_ADDR_UNSPECIFIED(&pi
->ipi6_addr
))
406 return(&pi
->ipi6_addr
);
409 * If the source address is not specified but the socket(if any)
410 * is already bound, use the bound address.
412 if (laddr
&& !IN6_IS_ADDR_UNSPECIFIED(laddr
))
416 * If the caller doesn't specify the source address but
417 * the outgoing interface, use an address associated with
420 if (pi
&& pi
->ipi6_ifindex
) {
421 /* XXX boundary check is assumed to be already done. */
422 ia6
= in6_ifawithscope(ifindex2ifnet
[pi
->ipi6_ifindex
],
425 *errorp
= EADDRNOTAVAIL
;
428 return(&satosin6(&ia6
->ia_addr
)->sin6_addr
);
432 * If the destination address is a link-local unicast address or
433 * a multicast address, and if the outgoing interface is specified
434 * by the sin6_scope_id filed, use an address associated with the
436 * XXX: We're now trying to define more specific semantics of
437 * sin6_scope_id field, so this part will be rewritten in
440 if ((IN6_IS_ADDR_LINKLOCAL(dst
) || IN6_IS_ADDR_MULTICAST(dst
)) &&
441 dstsock
->sin6_scope_id
) {
443 * I'm not sure if boundary check for scope_id is done
446 if (dstsock
->sin6_scope_id
< 0 ||
447 if_index
< dstsock
->sin6_scope_id
) {
448 *errorp
= ENXIO
; /* XXX: better error? */
451 ia6
= in6_ifawithscope(ifindex2ifnet
[dstsock
->sin6_scope_id
],
454 *errorp
= EADDRNOTAVAIL
;
457 return(&satosin6(&ia6
->ia_addr
)->sin6_addr
);
461 * If the destination address is a multicast address and
462 * the outgoing interface for the address is specified
463 * by the caller, use an address associated with the interface.
464 * There is a sanity check here; if the destination has node-local
465 * scope, the outgoing interfacde should be a loopback address.
466 * Even if the outgoing interface is not specified, we also
467 * choose a loopback interface as the outgoing interface.
469 if (IN6_IS_ADDR_MULTICAST(dst
)) {
470 struct ifnet
*ifp
= mopts
? mopts
->im6o_multicast_ifp
: NULL
;
472 if (ifp
== NULL
&& IN6_IS_ADDR_MC_NODELOCAL(dst
)) {
477 ia6
= in6_ifawithscope(ifp
, dst
);
479 *errorp
= EADDRNOTAVAIL
;
482 return(&ia6
->ia_addr
.sin6_addr
);
487 * If the next hop address for the packet is specified
488 * by caller, use an address associated with the route
492 struct sockaddr_in6
*sin6_next
;
495 if (opts
&& opts
->ip6po_nexthop
) {
496 sin6_next
= satosin6(opts
->ip6po_nexthop
);
497 rt
= nd6_lookup(&sin6_next
->sin6_addr
, 1, NULL
);
499 ia6
= in6_ifawithscope(rt
->rt_ifp
, dst
);
501 ia6
= ifatoia6(rt
->rt_ifa
);
504 *errorp
= EADDRNOTAVAIL
;
507 return(&satosin6(&ia6
->ia_addr
)->sin6_addr
);
512 * If route is known or can be allocated now,
513 * our src addr is taken from the i/f, else punt.
517 !IN6_ARE_ADDR_EQUAL(&satosin6(&ro
->ro_dst
)->sin6_addr
, dst
)) {
519 ro
->ro_rt
= (struct rtentry
*)0;
521 if (ro
->ro_rt
== (struct rtentry
*)0 ||
522 ro
->ro_rt
->rt_ifp
== (struct ifnet
*)0) {
523 struct sockaddr_in6
*dst6
;
525 /* No route yet, so try to acquire one */
526 bzero(&ro
->ro_dst
, sizeof(struct sockaddr_in6
));
527 dst6
= (struct sockaddr_in6
*)&ro
->ro_dst
;
528 dst6
->sin6_family
= AF_INET6
;
529 dst6
->sin6_len
= sizeof(struct sockaddr_in6
);
530 dst6
->sin6_addr
= *dst
;
531 if (IN6_IS_ADDR_MULTICAST(dst
)) {
532 ro
->ro_rt
= rtalloc1(&((struct route
*)ro
)
535 rtalloc((struct route
*)ro
);
540 * in_pcbconnect() checks out IFF_LOOPBACK to skip using
541 * the address. But we don't know why it does so.
542 * It is necessary to ensure the scope even for lo0
543 * so doesn't check out IFF_LOOPBACK.
547 ia6
= in6_ifawithscope(ro
->ro_rt
->rt_ifa
->ifa_ifp
, dst
);
548 if (ia6
== 0) /* xxx scope error ?*/
549 ia6
= ifatoia6(ro
->ro_rt
->rt_ifa
);
552 *errorp
= EHOSTUNREACH
; /* no route */
555 return(&satosin6(&ia6
->ia_addr
)->sin6_addr
);
558 *errorp
= EADDRNOTAVAIL
;
563 * Default hop limit selection. The precedence is as follows:
564 * 1. Hoplimit valued specified via ioctl.
565 * 2. (If the outgoing interface is detected) the current
566 * hop limit of the interface specified by router advertisement.
567 * 3. The system default hoplimit.
570 in6_selecthlim(in6p
, ifp
)
574 if (in6p
&& in6p
->in6p_hops
>= 0)
575 return(in6p
->in6p_hops
);
577 return(nd_ifinfo
[ifp
->if_index
].chlim
);
584 in6_pcbdisconnect(inp
)
587 bzero((caddr_t
)&inp
->in6p_faddr
, sizeof(inp
->in6p_faddr
));
589 /* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
590 inp
->in6p_flowinfo
&= ~IPV6_FLOWLABEL_MASK
;
592 if (inp
->inp_socket
->so_state
& SS_NOFDREF
)
600 struct socket
*so
= inp
->inp_socket
;
601 struct inpcbinfo
*ipi
= inp
->inp_pcbinfo
;
604 if (inp
->in6p_sp
!= NULL
)
605 ipsec6_delete_pcbpolicy(inp
);
607 inp
->inp_gencnt
= ++ipi
->ipi_gencnt
;
612 if (inp
->in6p_options
)
613 m_freem(inp
->in6p_options
);
614 ip6_freepcbopts(inp
->in6p_outputopts
);
615 ip6_freemoptions(inp
->in6p_moptions
);
616 if (inp
->in6p_route
.ro_rt
)
617 rtfree(inp
->in6p_route
.ro_rt
);
618 /* Check and free IPv4 related resources in case of mapped addr */
619 if (inp
->inp_options
)
620 (void)m_free(inp
->inp_options
);
621 ip_freemoptions(inp
->inp_moptions
);
624 zfree(ipi
->ipi_zone
, inp
);
628 * The calling convention of in6_setsockaddr() and in6_setpeeraddr() was
629 * modified to match the pru_sockaddr() and pru_peeraddr() entry points
630 * in struct pr_usrreqs, so that protocols can just reference then directly
631 * without the need for a wrapper function. The socket must have a valid
632 * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
633 * except through a kernel programming error, so it is acceptable to panic
634 * (or in this case trap) if the PCB is invalid. (Actually, we don't trap
635 * because there actually /is/ a programming error somewhere... XXX)
638 in6_setsockaddr(so
, nam
)
640 struct sockaddr
**nam
;
643 register struct inpcb
*inp
;
644 register struct sockaddr_in6
*sin6
;
647 * Do the malloc first in case it blocks.
649 MALLOC(sin6
, struct sockaddr_in6
*, sizeof *sin6
, M_SONAME
, M_WAITOK
);
650 bzero(sin6
, sizeof *sin6
);
651 sin6
->sin6_family
= AF_INET6
;
652 sin6
->sin6_len
= sizeof(*sin6
);
658 _FREE(sin6
, M_SONAME
);
661 sin6
->sin6_port
= inp
->inp_lport
;
662 sin6
->sin6_addr
= inp
->in6p_laddr
;
664 if (IN6_IS_SCOPE_LINKLOCAL(&sin6
->sin6_addr
))
665 sin6
->sin6_scope_id
= ntohs(sin6
->sin6_addr
.s6_addr16
[1]);
667 sin6
->sin6_scope_id
= 0; /*XXX*/
668 if (IN6_IS_SCOPE_LINKLOCAL(&sin6
->sin6_addr
))
669 sin6
->sin6_addr
.s6_addr16
[1] = 0;
671 *nam
= (struct sockaddr
*)sin6
;
676 in6_setpeeraddr(so
, nam
)
678 struct sockaddr
**nam
;
682 register struct sockaddr_in6
*sin6
;
685 * Do the malloc first in case it blocks.
687 MALLOC(sin6
, struct sockaddr_in6
*, sizeof(*sin6
), M_SONAME
, M_WAITOK
);
688 bzero((caddr_t
)sin6
, sizeof (*sin6
));
689 sin6
->sin6_family
= AF_INET6
;
690 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
696 _FREE(sin6
, M_SONAME
);
699 sin6
->sin6_port
= inp
->inp_fport
;
700 sin6
->sin6_addr
= inp
->in6p_faddr
;
702 if (IN6_IS_SCOPE_LINKLOCAL(&sin6
->sin6_addr
))
703 sin6
->sin6_scope_id
= ntohs(sin6
->sin6_addr
.s6_addr16
[1]);
705 sin6
->sin6_scope_id
= 0; /*XXX*/
706 if (IN6_IS_SCOPE_LINKLOCAL(&sin6
->sin6_addr
))
707 sin6
->sin6_addr
.s6_addr16
[1] = 0;
709 *nam
= (struct sockaddr
*)sin6
;
714 in6_mapped_sockaddr(struct socket
*so
, struct sockaddr
**nam
)
716 struct inpcb
*inp
= sotoinpcb(so
);
721 if (inp
->inp_vflag
& INP_IPV4
) {
722 error
= in_setsockaddr(so
, nam
);
724 in6_sin_2_v4mapsin6_in_sock(nam
);
726 error
= in6_setsockaddr(so
, nam
);
732 in6_mapped_peeraddr(struct socket
*so
, struct sockaddr
**nam
)
734 struct inpcb
*inp
= sotoinpcb(so
);
739 if (inp
->inp_vflag
& INP_IPV4
) {
740 error
= in_setpeeraddr(so
, nam
);
742 in6_sin_2_v4mapsin6_in_sock(nam
);
744 error
= in6_setpeeraddr(so
, nam
);
750 * Pass some notification to all connections of a protocol
751 * associated with address dst. The local address and/or port numbers
752 * may be specified to limit the search. The "usual action" will be
753 * taken, depending on the ctlinput cmd. The caller must filter any
754 * cmds that are uninteresting (e.g., no error in the map).
755 * Call the protocol specific routine (if any) to report
756 * any errors for each matching socket.
758 * Must be called at splnet.
761 in6_pcbnotify(head
, dst
, fport_arg
, src
, lport_arg
, cmd
, notify
)
762 struct inpcbhead
*head
;
763 struct sockaddr
*dst
, *src
;
764 u_int fport_arg
, lport_arg
;
766 void (*notify
) __P((struct inpcb
*, int));
768 struct inpcb
*inp
, *ninp
;
769 struct sockaddr_in6 sa6_src
, *sa6_dst
;
770 u_short fport
= fport_arg
, lport
= lport_arg
;
774 if ((unsigned)cmd
> PRC_NCMDS
|| dst
->sa_family
!= AF_INET6
)
777 sa6_dst
= (struct sockaddr_in6
*)dst
;
778 if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst
->sin6_addr
))
782 * note that src can be NULL when we get notify by local fragmentation.
784 sa6_src
= (src
== NULL
) ? sa6_any
: *(struct sockaddr_in6
*)src
;
785 flowinfo
= sa6_src
.sin6_flowinfo
;
788 * Redirects go to all references to the destination,
789 * and use in6_rtchange to invalidate the route cache.
790 * Dead host indications: also use in6_rtchange to invalidate
791 * the cache, and deliver the error to all the sockets.
792 * Otherwise, if we have knowledge of the local port and address,
793 * deliver only to that socket.
795 if (PRC_IS_REDIRECT(cmd
) || cmd
== PRC_HOSTDEAD
) {
798 bzero((caddr_t
)&sa6_src
.sin6_addr
, sizeof(sa6_src
.sin6_addr
));
800 if (cmd
!= PRC_HOSTDEAD
)
801 notify
= in6_rtchange
;
803 errno
= inet6ctlerrmap
[cmd
];
805 for (inp
= LIST_FIRST(head
); inp
!= NULL
; inp
= ninp
) {
806 ninp
= LIST_NEXT(inp
, inp_list
);
808 if ((inp
->inp_vflag
& INP_IPV6
) == 0)
812 * Detect if we should notify the error. If no source and
813 * destination ports are specifed, but non-zero flowinfo and
814 * local address match, notify the error. This is the case
815 * when the error is delivered with an encrypted buffer
816 * by ESP. Otherwise, just compare addresses and ports
819 if (lport
== 0 && fport
== 0 && flowinfo
&&
820 inp
->inp_socket
!= NULL
&&
821 flowinfo
== (inp
->in6p_flowinfo
& IPV6_FLOWLABEL_MASK
) &&
822 IN6_ARE_ADDR_EQUAL(&inp
->in6p_laddr
, &sa6_src
.sin6_addr
))
824 else if (!IN6_ARE_ADDR_EQUAL(&inp
->in6p_faddr
,
825 &sa6_dst
->sin6_addr
) ||
826 inp
->inp_socket
== 0 ||
827 (lport
&& inp
->inp_lport
!= lport
) ||
828 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src
.sin6_addr
) &&
829 !IN6_ARE_ADDR_EQUAL(&inp
->in6p_laddr
,
830 &sa6_src
.sin6_addr
)) ||
831 (fport
&& inp
->inp_fport
!= fport
))
836 (*notify
)(inp
, errno
);
842 * Lookup a PCB based on the local address and port.
845 in6_pcblookup_local(pcbinfo
, laddr
, lport_arg
, wild_okay
)
846 struct inpcbinfo
*pcbinfo
;
847 struct in6_addr
*laddr
;
851 register struct inpcb
*inp
;
852 int matchwild
= 3, wildcard
;
853 u_short lport
= lport_arg
;
856 struct inpcbhead
*head
;
858 * Look for an unconnected (wildcard foreign addr) PCB that
859 * matches the local address and port we're looking for.
861 head
= &pcbinfo
->hashbase
[INP_PCBHASH(INADDR_ANY
, lport
, 0,
863 LIST_FOREACH(inp
, head
, inp_hash
) {
864 if ((inp
->inp_vflag
& INP_IPV6
) == 0)
866 if (IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_faddr
) &&
867 IN6_ARE_ADDR_EQUAL(&inp
->in6p_laddr
, laddr
) &&
868 inp
->inp_lport
== lport
) {
880 struct inpcbporthead
*porthash
;
881 struct inpcbport
*phd
;
882 struct inpcb
*match
= NULL
;
884 * Best fit PCB lookup.
886 * First see if this local port is in use by looking on the
889 porthash
= &pcbinfo
->porthashbase
[INP_PCBPORTHASH(lport
,
890 pcbinfo
->porthashmask
)];
891 LIST_FOREACH(phd
, porthash
, phd_hash
) {
892 if (phd
->phd_port
== lport
)
897 * Port is in use by one or more PCBs. Look for best
900 LIST_FOREACH(inp
, &phd
->phd_pcblist
, inp_portlist
) {
902 if ((inp
->inp_vflag
& INP_IPV6
) == 0)
904 if (!IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_faddr
))
906 if (!IN6_IS_ADDR_UNSPECIFIED(
908 if (IN6_IS_ADDR_UNSPECIFIED(laddr
))
910 else if (!IN6_ARE_ADDR_EQUAL(
911 &inp
->in6p_laddr
, laddr
))
914 if (!IN6_IS_ADDR_UNSPECIFIED(laddr
))
917 if (wildcard
< matchwild
) {
919 matchwild
= wildcard
;
920 if (matchwild
== 0) {
931 in6_pcbpurgeif0(head
, ifp
)
936 struct ip6_moptions
*im6o
;
937 struct in6_multi_mship
*imm
, *nimm
;
939 for (in6p
= head
; in6p
!= NULL
; in6p
= LIST_NEXT(in6p
, inp_list
)) {
940 im6o
= in6p
->in6p_moptions
;
941 if ((in6p
->inp_vflag
& INP_IPV6
) &&
944 * Unselect the outgoing interface if it is being
947 if (im6o
->im6o_multicast_ifp
== ifp
)
948 im6o
->im6o_multicast_ifp
= NULL
;
951 * Drop multicast group membership if we joined
952 * through the interface being detached.
953 * XXX controversial - is it really legal for kernel
956 for (imm
= im6o
->im6o_memberships
.lh_first
;
957 imm
!= NULL
; imm
= nimm
) {
958 nimm
= imm
->i6mm_chain
.le_next
;
959 if (imm
->i6mm_maddr
->in6m_ifp
== ifp
) {
960 LIST_REMOVE(imm
, i6mm_chain
);
961 in6_delmulti(imm
->i6mm_maddr
);
962 FREE(imm
, M_IPMADDR
);
970 * Check for alternatives when higher level complains
971 * about service problems. For now, invalidate cached
972 * routing information. If the route was created dynamically
973 * (by a redirect), time to try a default gateway again.
980 struct rt_addrinfo info
;
982 if ((rt
= in6p
->in6p_route
.ro_rt
) != NULL
) {
983 in6p
->in6p_route
.ro_rt
= 0;
984 bzero((caddr_t
)&info
, sizeof(info
));
985 info
.rti_info
[RTAX_DST
] =
986 (struct sockaddr
*)&in6p
->in6p_route
.ro_dst
;
987 info
.rti_info
[RTAX_GATEWAY
] = rt
->rt_gateway
;
988 info
.rti_info
[RTAX_NETMASK
] = rt_mask(rt
);
989 rt_missmsg(RTM_LOSING
, &info
, rt
->rt_flags
, 0);
990 if (rt
->rt_flags
& RTF_DYNAMIC
)
991 (void)rtrequest(RTM_DELETE
, rt_key(rt
),
992 rt
->rt_gateway
, rt_mask(rt
), rt
->rt_flags
,
993 (struct rtentry
**)0);
996 * A new route can be allocated
997 * the next time output is attempted.
1004 * After a routing change, flush old routing
1005 * and allocate a (hopefully) better one.
1008 in6_rtchange(inp
, errno
)
1012 if (inp
->in6p_route
.ro_rt
) {
1013 rtfree(inp
->in6p_route
.ro_rt
);
1014 inp
->in6p_route
.ro_rt
= 0;
1016 * A new route can be allocated the next time
1017 * output is attempted.
1023 * Lookup PCB in hash list.
1026 in6_pcblookup_hash(pcbinfo
, faddr
, fport_arg
, laddr
, lport_arg
, wildcard
, ifp
)
1027 struct inpcbinfo
*pcbinfo
;
1028 struct in6_addr
*faddr
, *laddr
;
1029 u_int fport_arg
, lport_arg
;
1033 struct inpcbhead
*head
;
1034 register struct inpcb
*inp
;
1035 u_short fport
= fport_arg
, lport
= lport_arg
;
1038 #if defined(NFAITH) && NFAITH > 0
1039 faith
= faithprefix(laddr
);
1045 * First look for an exact match.
1047 head
= &pcbinfo
->hashbase
[INP_PCBHASH(faddr
->s6_addr32
[3] /* XXX */,
1049 pcbinfo
->hashmask
)];
1050 LIST_FOREACH(inp
, head
, inp_hash
) {
1051 if ((inp
->inp_vflag
& INP_IPV6
) == 0)
1053 if (IN6_ARE_ADDR_EQUAL(&inp
->in6p_faddr
, faddr
) &&
1054 IN6_ARE_ADDR_EQUAL(&inp
->in6p_laddr
, laddr
) &&
1055 inp
->inp_fport
== fport
&&
1056 inp
->inp_lport
== lport
) {
1064 struct inpcb
*local_wild
= NULL
;
1066 head
= &pcbinfo
->hashbase
[INP_PCBHASH(INADDR_ANY
, lport
, 0,
1067 pcbinfo
->hashmask
)];
1068 LIST_FOREACH(inp
, head
, inp_hash
) {
1069 if ((inp
->inp_vflag
& INP_IPV6
) == 0)
1071 if (IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_faddr
) &&
1072 inp
->inp_lport
== lport
) {
1073 if (faith
&& (inp
->inp_flags
& INP_FAITH
) == 0)
1075 if (IN6_ARE_ADDR_EQUAL(&inp
->in6p_laddr
,
1078 else if (IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
))
1082 return (local_wild
);
1092 init_sin6(struct sockaddr_in6
*sin6
, struct mbuf
*m
)
1096 ip
= mtod(m
, struct ip6_hdr
*);
1097 bzero(sin6
, sizeof(*sin6
));
1098 sin6
->sin6_len
= sizeof(*sin6
);
1099 sin6
->sin6_family
= AF_INET6
;
1100 sin6
->sin6_addr
= ip
->ip6_src
;
1101 if (IN6_IS_SCOPE_LINKLOCAL(&sin6
->sin6_addr
))
1102 sin6
->sin6_addr
.s6_addr16
[1] = 0;
1103 sin6
->sin6_scope_id
=
1104 (m
->m_pkthdr
.rcvif
&& IN6_IS_SCOPE_LINKLOCAL(&sin6
->sin6_addr
))
1105 ? m
->m_pkthdr
.rcvif
->if_index
: 0;