]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netinet6/in6_pcb.c
daa61ee2fe2ea159480082f41ab2d1e0d837c9b7
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
31 * Copyright (c) 1982, 1986, 1991, 1993
32 * The Regents of the University of California. All rights reserved.
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 3. All advertising materials mentioning features or use of this software
43 * must display the following acknowledgement:
44 * This product includes software developed by the University of
45 * California, Berkeley and its contributors.
46 * 4. 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
65 #ifdef __NetBSD__ /*XXX*/
66 #include "opt_ipsec.h"
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/malloc.h>
73 #include <sys/protosw.h>
74 #include <sys/socket.h>
75 #include <sys/socketvar.h>
76 #include <sys/sockio.h>
77 #include <sys/errno.h>
82 #include <net/if_types.h>
83 #include <net/route.h>
85 #include <netinet/in.h>
86 #include <netinet/in_var.h>
87 #include <netinet/in_systm.h>
88 #include <netinet/ip6.h>
89 #include <netinet6/ip6_var.h>
90 #include <netinet6/nd6.h>
91 #include <netinet/in_pcb.h>
92 #include <netinet6/in6_pcb.h>
93 #include <net/if_types.h>
98 #include <netinet6/ipsec.h>
99 #include <netkey/key.h>
100 #include <netkey/key_debug.h>
103 struct in6_addr zeroin6_addr
;
106 in6_pcbbind(inp
, nam
, p
)
107 register struct inpcb
*inp
;
108 struct sockaddr
*nam
;
111 struct socket
*so
= inp
->inp_socket
;
112 unsigned short *lastport
;
113 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)NULL
;
114 struct inpcbinfo
*pcbinfo
= inp
->inp_pcbinfo
;
116 int wild
= 0, reuseport
= (so
->so_options
& SO_REUSEPORT
);
119 if (!in6_ifaddr
) /* XXX broken! */
120 return (EADDRNOTAVAIL
);
121 if (inp
->inp_lport
|| !IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
))
123 if ((so
->so_options
& (SO_REUSEADDR
|SO_REUSEPORT
)) == 0)
124 wild
= INPLOOKUP_WILDCARD
;
126 if (nam
->sa_len
!= sizeof(*sin6
))
129 * We should check the family, but old programs
130 * incorrectly fail to intialize it.
132 if (nam
->sa_family
!= AF_INET6
)
133 return(EAFNOSUPPORT
);
134 sin6
= (struct sockaddr_in6
*)nam
;
137 * If the scope of the destination is link-local, embed the
138 * interface index in the address.
140 if (IN6_IS_SCOPE_LINKLOCAL(&sin6
->sin6_addr
)) {
141 /* XXX boundary check is assumed to be already done. */
142 /* XXX sin6_scope_id is weaker than advanced-api. */
143 struct in6_pktinfo
*pi
;
144 if (inp
->in6p_outputopts
&&
145 (pi
= inp
->in6p_outputopts
->ip6po_pktinfo
) &&
147 sin6
->sin6_addr
.s6_addr16
[1]
148 = htons(pi
->ipi6_ifindex
);
149 } else if (IN6_IS_ADDR_MULTICAST(&sin6
->sin6_addr
)
150 && inp
->in6p_moptions
151 && inp
->in6p_moptions
->im6o_multicast_ifp
) {
152 sin6
->sin6_addr
.s6_addr16
[1] =
153 htons(inp
->in6p_moptions
->im6o_multicast_ifp
->if_index
);
154 } else if (sin6
->sin6_scope_id
) {
156 if (sin6
->sin6_scope_id
< 0
157 || if_index
< sin6
->sin6_scope_id
) {
158 return ENXIO
; /* XXX EINVAL? */
160 sin6
->sin6_addr
.s6_addr16
[1]
161 = htons(sin6
->sin6_scope_id
& 0xffff);/*XXX*/
162 /* this must be cleared for ifa_ifwithaddr() */
163 sin6
->sin6_scope_id
= 0;
167 lport
= sin6
->sin6_port
;
168 if (IN6_IS_ADDR_MULTICAST(&sin6
->sin6_addr
)) {
170 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
171 * allow compepte duplication of binding if
172 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
173 * and a multicast address is bound on both
174 * new and duplicated sockets.
176 if (so
->so_options
& SO_REUSEADDR
)
177 reuseport
= SO_REUSEADDR
|SO_REUSEPORT
;
178 } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6
->sin6_addr
)) {
179 struct ifaddr
*ia
= NULL
;
181 sin6
->sin6_port
= 0; /* yech... */
182 if ((ia
= ifa_ifwithaddr((struct sockaddr
*)sin6
)) == 0)
183 return(EADDRNOTAVAIL
);
186 * XXX: bind to an anycast address might accidentally
187 * cause sending a packet with anycast source address.
190 ((struct in6_ifaddr
*)ia
)->ia6_flags
&
191 (IN6_IFF_ANYCAST
|IN6_IFF_NOTREADY
|
192 IN6_IFF_DETACHED
|IN6_IFF_DEPRECATED
)) {
193 return(EADDRNOTAVAIL
);
200 if (ntohs(lport
) < IPV6PORT_RESERVED
&& p
&&
202 suser(p
->p_ucred
, &p
->p_acflag
))
204 ((so
->so_state
& SS_PRIV
) == 0))
209 !IN6_IS_ADDR_MULTICAST(&sin6
->sin6_addr
)) {
210 t
= in6_pcblookup_local(inp
->inp_pcbinfo
,
215 (!IN6_IS_ADDR_UNSPECIFIED(&sin6
->sin6_addr
) ||
216 !IN6_IS_ADDR_UNSPECIFIED(&t
->in6p_laddr
) ||
217 (t
->inp_socket
->so_options
&
218 SO_REUSEPORT
) == 0) &&
219 so
->so_uid
!= t
->inp_socket
->so_uid
)
222 t
= in6_pcblookup_local(pcbinfo
, &sin6
->sin6_addr
,
224 if (t
&& (reuseport
& t
->inp_socket
->so_options
) == 0)
227 inp
->in6p_laddr
= sin6
->sin6_addr
;
233 inp
->inp_flags
|= INP_ANONPORT
;
235 if (inp
->inp_flags
& INP_HIGHPORT
) {
236 first
= ipport_hifirstauto
; /* sysctl */
237 last
= ipport_hilastauto
;
238 lastport
= &pcbinfo
->lasthi
;
239 } else if (inp
->inp_flags
& INP_LOWPORT
) {
241 if (p
&& (error
= suser(p
->p_ucred
, &p
->p_acflag
)))
244 if ((so
->so_state
& SS_PRIV
) == 0)
247 first
= ipport_lowfirstauto
; /* 1023 */
248 last
= ipport_lowlastauto
; /* 600 */
249 lastport
= &pcbinfo
->lastlow
;
251 first
= ipport_firstauto
; /* sysctl */
252 last
= ipport_lastauto
;
253 lastport
= &pcbinfo
->lastport
;
256 * Simple check to ensure all ports are not used up causing
259 * We split the two cases (up and down) so that the direction
260 * is not being tested on each round of the loop.
266 count
= first
- last
;
269 if (count
-- < 0) { /* completely used? */
271 * Undo any address bind that may have
274 inp
->in6p_laddr
= in6addr_any
;
278 if (*lastport
> first
|| *lastport
< last
)
280 lport
= htons(*lastport
);
281 } while (in6_pcblookup_local(pcbinfo
,
282 &inp
->in6p_laddr
, lport
, wild
));
287 count
= last
- first
;
290 if (count
-- < 0) { /* completely used? */
292 * Undo any address bind that may have
295 inp
->in6p_laddr
= in6addr_any
;
299 if (*lastport
< first
|| *lastport
> last
)
301 lport
= htons(*lastport
);
302 } while (in6_pcblookup_local(pcbinfo
,
303 &inp
->in6p_laddr
, lport
, wild
));
306 inp
->inp_lport
= lport
;
307 if (in_pcbinshash(inp
) != 0) {
308 inp
->in6p_laddr
= in6addr_any
;
312 inp
->in6p_flowinfo
= sin6
? sin6
->sin6_flowinfo
: 0; /*XXX*/
317 * Transform old in6_pcbconnect() into an inner subroutine for new
318 * in6_pcbconnect(): Do some validity-checking on the remote
319 * address (in mbuf 'nam') and then determine local host address
320 * (i.e., which interface) to use to access that remote host.
322 * This preserves definition of in6_pcbconnect(), while supporting a
323 * slightly different version for T/TCP. (This is more than
324 * a bit of a kludge, but cleaning up the internal interfaces would
325 * have forced minor changes in every protocol).
329 in6_pcbladdr(inp
, nam
, plocal_addr6
)
330 register struct inpcb
*inp
;
331 struct sockaddr
*nam
;
332 struct in6_addr
**plocal_addr6
;
334 register struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)nam
;
335 struct in6_pktinfo
*pi
;
336 struct ifnet
*ifp
= NULL
;
339 if (nam
->sa_len
!= sizeof (*sin6
))
341 if (sin6
->sin6_family
!= AF_INET6
)
342 return (EAFNOSUPPORT
);
343 if (sin6
->sin6_port
== 0)
344 return (EADDRNOTAVAIL
);
347 * If the scope of the destination is link-local, embed the interface
348 * index in the address.
350 if (IN6_IS_SCOPE_LINKLOCAL(&sin6
->sin6_addr
)) {
351 /* XXX boundary check is assumed to be already done. */
352 /* XXX sin6_scope_id is weaker than advanced-api. */
353 if (inp
->in6p_outputopts
&&
354 (pi
= inp
->in6p_outputopts
->ip6po_pktinfo
) &&
356 sin6
->sin6_addr
.s6_addr16
[1] = htons(pi
->ipi6_ifindex
);
357 ifp
= ifindex2ifnet
[pi
->ipi6_ifindex
];
359 else if (IN6_IS_ADDR_MULTICAST(&sin6
->sin6_addr
) &&
360 inp
->in6p_moptions
&&
361 inp
->in6p_moptions
->im6o_multicast_ifp
) {
362 sin6
->sin6_addr
.s6_addr16
[1] =
363 htons(inp
->in6p_moptions
->im6o_multicast_ifp
->if_index
);
364 ifp
= ifindex2ifnet
[inp
->in6p_moptions
->im6o_multicast_ifp
->if_index
];
365 } else if (sin6
->sin6_scope_id
) {
367 if (sin6
->sin6_scope_id
< 0
368 || if_index
< sin6
->sin6_scope_id
) {
369 return ENXIO
; /* XXX EINVAL? */
371 sin6
->sin6_addr
.s6_addr16
[1]
372 = htons(sin6
->sin6_scope_id
& 0xffff);/*XXX*/
373 ifp
= ifindex2ifnet
[sin6
->sin6_scope_id
];
379 * If the destination address is UNSPECIFIED addr,
380 * use the loopback addr, e.g ::1.
382 #define satosin6(sa) ((struct sockaddr_in6 *)(sa))
383 #define ifatoia6(ifa) ((struct in6_ifaddr *)(ifa))
384 if (IN6_IS_ADDR_UNSPECIFIED(&sin6
->sin6_addr
))
385 sin6
->sin6_addr
= in6addr_loopback
;
389 * XXX: in6_selectsrc might replace the bound local address
390 * with the address specified by setsockopt(IPV6_PKTINFO).
391 * Is it the intended behavior?
393 *plocal_addr6
= in6_selectsrc(sin6
, inp
->in6p_outputopts
,
396 &inp
->in6p_laddr
, &error
);
397 if (*plocal_addr6
== 0) {
399 error
= EADDRNOTAVAIL
;
403 * Don't do pcblookup call here; return interface in
405 * and exit to caller, that will do the lookup.
409 if (inp
->in6p_route
.ro_rt
)
410 ifp
= inp
->in6p_route
.ro_rt
->rt_ifp
;
417 * Connect from a socket to a specified address.
418 * Both address and port must be specified in argument sin.
419 * If don't have a local address for this socket yet,
423 in6_pcbconnect(inp
, nam
, p
)
424 register struct inpcb
*inp
;
425 struct sockaddr
*nam
;
428 struct in6_addr
*addr6
;
429 register struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)nam
;
433 * Call inner routine, to assign local interface address.
435 if (error
= in6_pcbladdr(inp
, nam
, &addr6
))
438 if (in6_pcblookup_hash(inp
->inp_pcbinfo
, &sin6
->sin6_addr
,
440 IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
)
441 ? addr6
: &inp
->in6p_laddr
,
442 inp
->inp_lport
, 0, NULL
) != NULL
) {
445 if (IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
)) {
446 if (inp
->inp_lport
== 0) {
447 error
= in6_pcbbind(inp
, (struct sockaddr
*)0, p
);
451 inp
->in6p_laddr
= *addr6
;
453 inp
->in6p_faddr
= sin6
->sin6_addr
;
454 inp
->inp_fport
= sin6
->sin6_port
;
456 * xxx kazu flowlabel is necessary for connect?
457 * but if this line is missing, the garbage value remains.
459 inp
->in6p_flowinfo
= sin6
->sin6_flowinfo
;
467 * Return an IPv6 address, which is the most appropriate for given
468 * destination and user specified options.
469 * If necessary, this function lookups the routing table and return
470 * an entry to the caller for later use.
473 in6_selectsrc(dstsock
, opts
, mopts
, ro
, laddr
, errorp
)
474 struct sockaddr_in6
*dstsock
;
475 struct ip6_pktopts
*opts
;
476 struct ip6_moptions
*mopts
;
477 struct route_in6
*ro
;
478 struct in6_addr
*laddr
;
481 struct in6_addr
*dst
;
482 struct in6_ifaddr
*ia6
= 0;
483 struct in6_pktinfo
*pi
= NULL
;
485 dst
= &dstsock
->sin6_addr
;
489 * If the source address is explicitly specified by the caller,
492 if (opts
&& (pi
= opts
->ip6po_pktinfo
) &&
493 !IN6_IS_ADDR_UNSPECIFIED(&pi
->ipi6_addr
))
494 return(&pi
->ipi6_addr
);
497 * If the source address is not specified but the socket(if any)
498 * is already bound, use the bound address.
500 if (laddr
&& !IN6_IS_ADDR_UNSPECIFIED(laddr
))
504 * If the caller doesn't specify the source address but
505 * the outgoing interface, use an address associated with
508 if (pi
&& pi
->ipi6_ifindex
) {
509 /* XXX boundary check is assumed to be already done. */
510 ia6
= in6_ifawithscope(ifindex2ifnet
[pi
->ipi6_ifindex
],
513 *errorp
= EADDRNOTAVAIL
;
516 return(&satosin6(&ia6
->ia_addr
)->sin6_addr
);
520 * If the destination address is a link-local unicast address or
521 * a multicast address, and if the outgoing interface is specified
522 * by the sin6_scope_id filed, use an address associated with the
524 * XXX: We're now trying to define more specific semantics of
525 * sin6_scope_id field, so this part will be rewritten in
528 if ((IN6_IS_ADDR_LINKLOCAL(dst
) || IN6_IS_ADDR_MULTICAST(dst
)) &&
529 dstsock
->sin6_scope_id
) {
531 * I'm not sure if boundary check for scope_id is done
534 if (dstsock
->sin6_scope_id
< 0 ||
535 if_index
< dstsock
->sin6_scope_id
) {
536 *errorp
= ENXIO
; /* XXX: better error? */
539 ia6
= in6_ifawithscope(ifindex2ifnet
[dstsock
->sin6_scope_id
],
542 *errorp
= EADDRNOTAVAIL
;
545 return(&satosin6(&ia6
->ia_addr
)->sin6_addr
);
549 * If the destination address is a multicast address and
550 * the outgoing interface for the address is specified
551 * by the caller, use an address associated with the interface.
552 * There is a sanity check here; if the destination has node-local
553 * scope, the outgoing interfacde should be a loopback address.
554 * Even if the outgoing interface is not specified, we also
555 * choose a loopback interface as the outgoing interface.
557 if (IN6_IS_ADDR_MULTICAST(dst
)) {
558 struct ifnet
*ifp
= mopts
? mopts
->im6o_multicast_ifp
: NULL
;
560 extern struct ifnet loif
;
563 if (ifp
== NULL
&& IN6_IS_ADDR_MC_NODELOCAL(dst
)) {
572 ia6
= in6_ifawithscope(ifp
, dst
);
574 *errorp
= EADDRNOTAVAIL
;
577 return(&ia6
->ia_addr
.sin6_addr
);
582 * If the next hop address for the packet is specified
583 * by caller, use an address associated with the route
587 struct sockaddr_in6
*sin6_next
;
590 if (opts
&& opts
->ip6po_nexthop
) {
591 sin6_next
= satosin6(opts
->ip6po_nexthop
);
592 rt
= nd6_lookup(&sin6_next
->sin6_addr
, 1, NULL
);
594 ia6
= in6_ifawithscope(rt
->rt_ifp
, dst
);
596 ia6
= ifatoia6(rt
->rt_ifa
);
599 *errorp
= EADDRNOTAVAIL
;
602 return(&satosin6(&ia6
->ia_addr
)->sin6_addr
);
607 * If route is known or can be allocated now,
608 * our src addr is taken from the i/f, else punt.
612 !IN6_ARE_ADDR_EQUAL(&satosin6(&ro
->ro_dst
)->sin6_addr
, dst
)) {
614 ro
->ro_rt
= (struct rtentry
*)0;
616 if (ro
->ro_rt
== (struct rtentry
*)0 ||
617 ro
->ro_rt
->rt_ifp
== (struct ifnet
*)0) {
618 /* No route yet, so try to acquire one */
619 bzero(&ro
->ro_dst
, sizeof(struct sockaddr_in6
));
620 ro
->ro_dst
.sin6_family
= AF_INET6
;
621 ro
->ro_dst
.sin6_len
= sizeof(struct sockaddr_in6
);
622 ro
->ro_dst
.sin6_addr
= *dst
;
623 if (IN6_IS_ADDR_MULTICAST(dst
)) {
624 #if defined(__FreeBSD__) || defined (__APPLE__)
625 ro
->ro_rt
= rtalloc1(&((struct route
*)ro
)
627 #endif /*__FreeBSD__*/
628 #if defined(__bsdi__) || defined(__NetBSD__)
629 ro
->ro_rt
= rtalloc1(&((struct route
*)ro
)
633 rtalloc((struct route
*)ro
);
638 * in_pcbconnect() checks out IFF_LOOPBACK to skip using
639 * the address. But we don't know why it does so.
640 * It is necessary to ensure the scope even for lo0
641 * so doesn't check out IFF_LOOPBACK.
645 ia6
= in6_ifawithscope(ro
->ro_rt
->rt_ifa
->ifa_ifp
, dst
);
646 if (ia6
== 0) /* xxx scope error ?*/
647 ia6
= ifatoia6(ro
->ro_rt
->rt_ifa
);
651 * xxx The followings are necessary? (kazu)
653 * It's for SO_DONTROUTE option in IPv4.(jinmei)
656 struct sockaddr_in6 sin6
= {sizeof(sin6
), AF_INET6
, 0};
658 sin6
->sin6_addr
= *dst
;
660 ia6
= ifatoia6(ifa_ifwithdstaddr(sin6tosa(&sin6
)));
662 ia6
= ifatoia6(ifa_ifwithnet(sin6tosa(&sin6
)));
664 *errorp
= EHOSTUNREACH
; /* no route */
667 return(&satosin6(&ia6
->ia_addr
)->sin6_addr
);
671 *errorp
= EHOSTUNREACH
; /* no route */
674 return(&satosin6(&ia6
->ia_addr
)->sin6_addr
);
677 *errorp
= EADDRNOTAVAIL
;
682 * Default hop limit selection. The precedence is as follows:
683 * 1. Hoplimit valued specified via ioctl.
684 * 2. (If the outgoing interface is detected) the current
685 * hop limit of the interface specified by router advertisement.
686 * 3. The system default hoplimit.
689 in6_selecthlim(in6p
, ifp
)
693 if (in6p
&& in6p
->in6p_hops
>= 0)
694 return(in6p
->in6p_hops
);
696 return(nd_ifinfo
[ifp
->if_index
].chlim
);
703 in6_pcbdisconnect(inp
)
706 bzero((caddr_t
)&inp
->in6p_faddr
, sizeof(inp
->in6p_faddr
));
709 if (inp
->inp_socket
->so_state
& SS_NOFDREF
)
717 struct socket
*so
= inp
->inp_socket
;
718 struct inpcbinfo
*ipi
= inp
->inp_pcbinfo
;
721 ipsec6_delete_pcbpolicy(inp
);
723 inp
->inp_gencnt
= ++ipi
->ipi_gencnt
;
728 if (inp
->in6p_inputopts
) /* Free all received options. */
729 m_freem(inp
->in6p_inputopts
->head
); /* this is safe */
730 ip6_freepcbopts(inp
->in6p_outputopts
);
731 ip6_freemoptions(inp
->in6p_moptions
);
733 if (inp
->in6p_route
.ro_rt
)
734 rtfree(inp
->in6p_route
.ro_rt
);
736 zfree(ipi
->ipi_zone
, inp
);
740 * The calling convention of in6_setsockaddr() and in6_setpeeraddr() was
741 * modified to match the pru_sockaddr() and pru_peeraddr() entry points
742 * in struct pr_usrreqs, so that protocols can just reference then directly
743 * without the need for a wrapper function. The socket must have a valid
744 * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
745 * except through a kernel programming error, so it is acceptable to panic
746 * (or in this case trap) if the PCB is invalid. (Actually, we don't trap
747 * because there actually /is/ a programming error somewhere... XXX)
750 in6_setsockaddr(so
, nam
)
752 struct sockaddr
**nam
;
755 register struct inpcb
*inp
;
756 register struct sockaddr_in6
*sin6
;
759 * Do the malloc first in case it blocks.
761 MALLOC(sin6
, struct sockaddr_in6
*, sizeof *sin6
, M_SONAME
, M_WAITOK
);
762 bzero(sin6
, sizeof *sin6
);
763 sin6
->sin6_family
= AF_INET6
;
764 sin6
->sin6_len
= sizeof(*sin6
);
770 _FREE(sin6
, M_SONAME
);
773 sin6
->sin6_port
= inp
->inp_lport
;
774 sin6
->sin6_addr
= inp
->in6p_laddr
;
776 if (IN6_IS_SCOPE_LINKLOCAL(&sin6
->sin6_addr
))
777 sin6
->sin6_scope_id
= ntohs(sin6
->sin6_addr
.s6_addr16
[1]);
779 sin6
->sin6_scope_id
= 0; /*XXX*/
780 if (IN6_IS_SCOPE_LINKLOCAL(&sin6
->sin6_addr
))
781 sin6
->sin6_addr
.s6_addr16
[1] = 0;
783 *nam
= (struct sockaddr
*)sin6
;
788 in6_setpeeraddr(so
, nam
)
790 struct sockaddr
**nam
;
794 register struct sockaddr_in6
*sin6
;
797 * Do the malloc first in case it blocks.
799 MALLOC(sin6
, struct sockaddr_in6
*, sizeof(*sin6
), M_SONAME
, M_WAITOK
);
800 bzero((caddr_t
)sin6
, sizeof (*sin6
));
801 sin6
->sin6_family
= AF_INET6
;
802 sin6
->sin6_len
= sizeof(struct sockaddr_in6
);
808 _FREE(sin6
, M_SONAME
);
811 sin6
->sin6_port
= inp
->inp_fport
;
812 sin6
->sin6_addr
= inp
->in6p_faddr
;
814 if (IN6_IS_SCOPE_LINKLOCAL(&sin6
->sin6_addr
))
815 sin6
->sin6_scope_id
= ntohs(sin6
->sin6_addr
.s6_addr16
[1]);
817 sin6
->sin6_scope_id
= 0; /*XXX*/
818 if (IN6_IS_SCOPE_LINKLOCAL(&sin6
->sin6_addr
))
819 sin6
->sin6_addr
.s6_addr16
[1] = 0;
821 *nam
= (struct sockaddr
*)sin6
;
826 in6_mapped_sockaddr(struct socket
*so
, struct sockaddr
**nam
)
828 struct inpcb
*inp
= sotoinpcb(so
);
833 if (inp
->inp_vflag
& INP_IPV4
) {
834 error
= in_setsockaddr(so
, nam
);
836 in6_sin_2_v4mapsin6_in_sock(nam
);
838 error
= in6_setsockaddr(so
, nam
);
844 in6_mapped_peeraddr(struct socket
*so
, struct sockaddr
**nam
)
846 struct inpcb
*inp
= sotoinpcb(so
);
851 if (inp
->inp_vflag
& INP_IPV4
) {
852 error
= in_setpeeraddr(so
, nam
);
854 in6_sin_2_v4mapsin6_in_sock(nam
);
856 error
= in6_setpeeraddr(so
, nam
);
862 * Pass some notification to all connections of a protocol
863 * associated with address dst. The local address and/or port numbers
864 * may be specified to limit the search. The "usual action" will be
865 * taken, depending on the ctlinput cmd. The caller must filter any
866 * cmds that are uninteresting (e.g., no error in the map).
867 * Call the protocol specific routine (if any) to report
868 * any errors for each matching socket.
870 * Must be called at splnet.
873 in6_pcbnotify(head
, dst
, fport_arg
, laddr6
, lport_arg
, cmd
, notify
)
874 struct inpcbhead
*head
;
875 struct sockaddr
*dst
;
876 u_int fport_arg
, lport_arg
;
877 struct in6_addr
*laddr6
;
879 void (*notify
) __P((struct inpcb
*, int));
881 struct inpcb
*inp
, *ninp
;
882 struct in6_addr faddr6
;
883 u_short fport
= fport_arg
, lport
= lport_arg
;
885 void (*notify2
) __P((struct inpcb
*, int));
889 if ((unsigned)cmd
> PRC_NCMDS
|| dst
->sa_family
!= AF_INET6
)
891 faddr6
= ((struct sockaddr_in6
*)dst
)->sin6_addr
;
892 if (IN6_IS_ADDR_UNSPECIFIED(&faddr6
))
896 * Redirects go to all references to the destination,
897 * and use in6_rtchange to invalidate the route cache.
898 * Dead host indications: also use in6_rtchange to invalidate
899 * the cache, and deliver the error to all the sockets.
900 * Otherwise, if we have knowledge of the local port and address,
901 * deliver only to that socket.
903 if (PRC_IS_REDIRECT(cmd
) || cmd
== PRC_HOSTDEAD
) {
906 bzero((caddr_t
)laddr6
, sizeof(*laddr6
));
909 * Keep the old notify function to store a soft error
912 if (cmd
== PRC_HOSTDEAD
&& notify
!= in6_rtchange
)
915 notify
= in6_rtchange
;
917 errno
= inet6ctlerrmap
[cmd
];
919 for (inp
= LIST_FIRST(head
); inp
!= NULL
; inp
= ninp
) {
920 ninp
= LIST_NEXT(inp
, inp_list
);
922 if ((inp
->inp_vflag
& INP_IPV6
) == 0)
925 if (notify
== in6_rtchange
) {
927 * Since a non-connected PCB might have a cached route,
928 * we always call in6_rtchange without matching
929 * the PCB to the src/dst pair.
931 * XXX: we assume in6_rtchange does not free the PCB.
933 if (IN6_ARE_ADDR_EQUAL(&inp
->in6p_route
.ro_dst
.sin6_addr
,
935 in6_rtchange(inp
, errno
);
943 if (!IN6_ARE_ADDR_EQUAL(&inp
->in6p_faddr
, &faddr6
) ||
944 inp
->inp_socket
== 0 ||
945 (lport
&& inp
->inp_lport
!= lport
) ||
946 (!IN6_IS_ADDR_UNSPECIFIED(laddr6
) &&
947 !IN6_ARE_ADDR_EQUAL(&inp
->in6p_laddr
, laddr6
)) ||
948 (fport
&& inp
->inp_fport
!= fport
))
952 (*notify
)(inp
, errno
);
958 * Lookup a PCB based on the local address and port.
961 in6_pcblookup_local(pcbinfo
, laddr
, lport_arg
, wild_okay
)
962 struct inpcbinfo
*pcbinfo
;
963 struct in6_addr
*laddr
;
967 register struct inpcb
*inp
;
968 int matchwild
= 3, wildcard
;
969 u_short lport
= lport_arg
;
972 struct inpcbhead
*head
;
974 * Look for an unconnected (wildcard foreign addr) PCB that
975 * matches the local address and port we're looking for.
977 head
= &pcbinfo
->hashbase
[INP_PCBHASH(INADDR_ANY
, lport
, 0,
979 LIST_FOREACH(inp
, head
, inp_hash
) {
980 if ((inp
->inp_vflag
& INP_IPV6
) == 0)
982 if (IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_faddr
) &&
983 IN6_ARE_ADDR_EQUAL(&inp
->in6p_laddr
, laddr
) &&
984 inp
->inp_lport
== lport
) {
996 struct inpcbporthead
*porthash
;
997 struct inpcbport
*phd
;
998 struct inpcb
*match
= NULL
;
1000 * Best fit PCB lookup.
1002 * First see if this local port is in use by looking on the
1005 porthash
= &pcbinfo
->porthashbase
[INP_PCBPORTHASH(lport
,
1006 pcbinfo
->porthashmask
)];
1007 LIST_FOREACH(phd
, porthash
, phd_hash
) {
1008 if (phd
->phd_port
== lport
)
1013 * Port is in use by one or more PCBs. Look for best
1016 LIST_FOREACH(inp
, &phd
->phd_pcblist
, inp_portlist
) {
1018 if ((inp
->inp_vflag
& INP_IPV6
) == 0)
1020 if (!IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_faddr
))
1022 if (!IN6_IS_ADDR_UNSPECIFIED(
1023 &inp
->in6p_laddr
)) {
1024 if (IN6_IS_ADDR_UNSPECIFIED(laddr
))
1026 else if (!IN6_ARE_ADDR_EQUAL(
1027 &inp
->in6p_laddr
, laddr
))
1030 if (!IN6_IS_ADDR_UNSPECIFIED(laddr
))
1033 if (wildcard
< matchwild
) {
1035 matchwild
= wildcard
;
1036 if (matchwild
== 0) {
1047 * Check for alternatives when higher level complains
1048 * about service problems. For now, invalidate cached
1049 * routing information. If the route was created dynamically
1050 * (by a redirect), time to try a default gateway again.
1057 struct rt_addrinfo info
;
1059 if ((rt
= in6p
->in6p_route
.ro_rt
) != NULL
) {
1060 in6p
->in6p_route
.ro_rt
= 0;
1061 bzero((caddr_t
)&info
, sizeof(info
));
1062 info
.rti_info
[RTAX_DST
] =
1063 (struct sockaddr
*)&in6p
->in6p_route
.ro_dst
;
1064 info
.rti_info
[RTAX_GATEWAY
] = rt
->rt_gateway
;
1065 info
.rti_info
[RTAX_NETMASK
] = rt_mask(rt
);
1066 rt_missmsg(RTM_LOSING
, &info
, rt
->rt_flags
, 0);
1067 if (rt
->rt_flags
& RTF_DYNAMIC
)
1068 (void)rtrequest(RTM_DELETE
, rt_key(rt
),
1069 rt
->rt_gateway
, rt_mask(rt
), rt
->rt_flags
,
1070 (struct rtentry
**)0);
1073 * A new route can be allocated
1074 * the next time output is attempted.
1081 * After a routing change, flush old routing
1082 * and allocate a (hopefully) better one.
1085 in6_rtchange(inp
, errno
)
1089 if (inp
->in6p_route
.ro_rt
) {
1090 rtfree(inp
->in6p_route
.ro_rt
);
1091 inp
->in6p_route
.ro_rt
= 0;
1093 * A new route can be allocated the next time
1094 * output is attempted.
1100 * Lookup PCB in hash list.
1103 in6_pcblookup_hash(pcbinfo
, faddr
, fport_arg
, laddr
, lport_arg
, wildcard
, ifp
)
1104 struct inpcbinfo
*pcbinfo
;
1105 struct in6_addr
*faddr
, *laddr
;
1106 u_int fport_arg
, lport_arg
;
1110 struct inpcbhead
*head
;
1111 register struct inpcb
*inp
;
1112 u_short fport
= fport_arg
, lport
= lport_arg
;
1115 * First look for an exact match.
1117 head
= &pcbinfo
->hashbase
[INP_PCBHASH(faddr
->s6_addr32
[3] /* XXX */,
1119 pcbinfo
->hashmask
)];
1120 for (inp
= head
->lh_first
; inp
!= NULL
; inp
= inp
->inp_hash
.le_next
) {
1121 if ((inp
->inp_vflag
& INP_IPV6
) == 0)
1123 if (IN6_ARE_ADDR_EQUAL(&inp
->in6p_faddr
, faddr
) &&
1124 IN6_ARE_ADDR_EQUAL(&inp
->in6p_laddr
, laddr
) &&
1125 inp
->inp_fport
== fport
&&
1126 inp
->inp_lport
== lport
) {
1134 struct inpcb
*local_wild
= NULL
;
1136 head
= &pcbinfo
->hashbase
[INP_PCBHASH(INADDR_ANY
, lport
, 0,
1137 pcbinfo
->hashmask
)];
1138 for (inp
= head
->lh_first
; inp
!= NULL
;
1139 inp
= inp
->inp_hash
.le_next
) {
1140 if ((inp
->inp_vflag
& INP_IPV6
) == 0)
1142 if (IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_faddr
) &&
1143 inp
->inp_lport
== lport
) {
1144 #if defined(NFAITH) && NFAITH > 0
1145 if (ifp
&& ifp
->if_type
== IFT_FAITH
&&
1146 (inp
->inp_flags
& INP_FAITH
) == 0)
1149 if (IN6_ARE_ADDR_EQUAL(&inp
->in6p_laddr
,
1152 else if (IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_laddr
))
1156 return (local_wild
);
1166 init_sin6(struct sockaddr_in6
*sin6
, struct mbuf
*m
)
1170 ip
= mtod(m
, struct ip6_hdr
*);
1171 bzero(sin6
, sizeof(*sin6
));
1172 sin6
->sin6_len
= sizeof(*sin6
);
1173 sin6
->sin6_family
= AF_INET6
;
1174 sin6
->sin6_addr
= ip
->ip6_src
;
1175 if (IN6_IS_SCOPE_LINKLOCAL(&sin6
->sin6_addr
))
1176 sin6
->sin6_addr
.s6_addr16
[1] = 0;
1177 sin6
->sin6_scope_id
=
1178 (m
->m_pkthdr
.rcvif
&& IN6_IS_SCOPE_LINKLOCAL(&sin6
->sin6_addr
))
1179 ? m
->m_pkthdr
.rcvif
->if_index
: 0;