in6_pcb.c revision 1.1.2.2 1 1.1.2.1 itojun /*
2 1.1.2.1 itojun * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3 1.1.2.1 itojun * All rights reserved.
4 1.1.2.1 itojun *
5 1.1.2.1 itojun * Redistribution and use in source and binary forms, with or without
6 1.1.2.1 itojun * modification, are permitted provided that the following conditions
7 1.1.2.1 itojun * are met:
8 1.1.2.1 itojun * 1. Redistributions of source code must retain the above copyright
9 1.1.2.1 itojun * notice, this list of conditions and the following disclaimer.
10 1.1.2.1 itojun * 2. Redistributions in binary form must reproduce the above copyright
11 1.1.2.1 itojun * notice, this list of conditions and the following disclaimer in the
12 1.1.2.1 itojun * documentation and/or other materials provided with the distribution.
13 1.1.2.1 itojun * 3. Neither the name of the project nor the names of its contributors
14 1.1.2.1 itojun * may be used to endorse or promote products derived from this software
15 1.1.2.1 itojun * without specific prior written permission.
16 1.1.2.1 itojun *
17 1.1.2.1 itojun * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 1.1.2.1 itojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 1.1.2.1 itojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 1.1.2.1 itojun * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 1.1.2.1 itojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 1.1.2.1 itojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 1.1.2.1 itojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 1.1.2.1 itojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 1.1.2.1 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 1.1.2.1 itojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 1.1.2.1 itojun * SUCH DAMAGE.
28 1.1.2.1 itojun */
29 1.1.2.1 itojun
30 1.1.2.1 itojun /*
31 1.1.2.1 itojun * Copyright (c) 1982, 1986, 1991, 1993
32 1.1.2.1 itojun * The Regents of the University of California. All rights reserved.
33 1.1.2.1 itojun *
34 1.1.2.1 itojun * Redistribution and use in source and binary forms, with or without
35 1.1.2.1 itojun * modification, are permitted provided that the following conditions
36 1.1.2.1 itojun * are met:
37 1.1.2.1 itojun * 1. Redistributions of source code must retain the above copyright
38 1.1.2.1 itojun * notice, this list of conditions and the following disclaimer.
39 1.1.2.1 itojun * 2. Redistributions in binary form must reproduce the above copyright
40 1.1.2.1 itojun * notice, this list of conditions and the following disclaimer in the
41 1.1.2.1 itojun * documentation and/or other materials provided with the distribution.
42 1.1.2.1 itojun * 3. All advertising materials mentioning features or use of this software
43 1.1.2.1 itojun * must display the following acknowledgement:
44 1.1.2.1 itojun * This product includes software developed by the University of
45 1.1.2.1 itojun * California, Berkeley and its contributors.
46 1.1.2.1 itojun * 4. Neither the name of the University nor the names of its contributors
47 1.1.2.1 itojun * may be used to endorse or promote products derived from this software
48 1.1.2.1 itojun * without specific prior written permission.
49 1.1.2.1 itojun *
50 1.1.2.1 itojun * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 1.1.2.1 itojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 1.1.2.1 itojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 1.1.2.1 itojun * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 1.1.2.1 itojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 1.1.2.1 itojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 1.1.2.1 itojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 1.1.2.1 itojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 1.1.2.1 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 1.1.2.1 itojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 1.1.2.1 itojun * SUCH DAMAGE.
61 1.1.2.1 itojun *
62 1.1.2.1 itojun * @(#)in_pcb.c 8.2 (Berkeley) 1/4/94
63 1.1.2.1 itojun */
64 1.1.2.1 itojun
65 1.1.2.1 itojun #include <sys/param.h>
66 1.1.2.1 itojun #include <sys/systm.h>
67 1.1.2.1 itojun #include <sys/malloc.h>
68 1.1.2.1 itojun #include <sys/mbuf.h>
69 1.1.2.1 itojun #include <sys/protosw.h>
70 1.1.2.1 itojun #include <sys/socket.h>
71 1.1.2.1 itojun #include <sys/socketvar.h>
72 1.1.2.1 itojun #include <sys/ioctl.h>
73 1.1.2.1 itojun #include <sys/errno.h>
74 1.1.2.1 itojun #include <sys/time.h>
75 1.1.2.1 itojun #include <sys/proc.h>
76 1.1.2.1 itojun
77 1.1.2.1 itojun #include <net/if.h>
78 1.1.2.1 itojun #include <net/route.h>
79 1.1.2.1 itojun
80 1.1.2.1 itojun #include <netinet/in.h>
81 1.1.2.1 itojun #include <netinet/in_var.h>
82 1.1.2.1 itojun #include <netinet/in_systm.h>
83 1.1.2.1 itojun #include <netinet/ip.h>
84 1.1.2.1 itojun #include <netinet/in_pcb.h>
85 1.1.2.1 itojun #include <netinet6/ip6.h>
86 1.1.2.1 itojun #include <netinet6/in6_pcb.h>
87 1.1.2.1 itojun #include <netinet6/ip6_var.h>
88 1.1.2.1 itojun #include <netinet6/nd6.h>
89 1.1.2.1 itojun
90 1.1.2.1 itojun #include "loop.h"
91 1.1.2.1 itojun #ifdef __NetBSD__
92 1.1.2.1 itojun extern struct ifnet loif[NLOOP];
93 1.1.2.1 itojun #endif
94 1.1.2.1 itojun
95 1.1.2.1 itojun #ifdef IPSEC
96 1.1.2.1 itojun #include <netinet6/ipsec.h>
97 1.1.2.1 itojun #include <netkey/key.h>
98 1.1.2.1 itojun #include <netkey/key_debug.h>
99 1.1.2.1 itojun #endif /* IPSEC */
100 1.1.2.1 itojun
101 1.1.2.1 itojun struct in6_addr zeroin6_addr;
102 1.1.2.1 itojun
103 1.1.2.1 itojun int
104 1.1.2.1 itojun in6_pcballoc(so, head)
105 1.1.2.1 itojun struct socket *so;
106 1.1.2.1 itojun struct in6pcb *head;
107 1.1.2.1 itojun {
108 1.1.2.1 itojun struct in6pcb *in6p;
109 1.1.2.1 itojun
110 1.1.2.1 itojun MALLOC(in6p, struct in6pcb *, sizeof(*in6p), M_PCB, M_NOWAIT);
111 1.1.2.1 itojun if (in6p == NULL)
112 1.1.2.1 itojun return(ENOBUFS);
113 1.1.2.1 itojun bzero((caddr_t)in6p, sizeof(*in6p));
114 1.1.2.1 itojun in6p->in6p_head = head;
115 1.1.2.1 itojun in6p->in6p_socket = so;
116 1.1.2.1 itojun in6p->in6p_hops = -1; /* use kernel default */
117 1.1.2.1 itojun in6p->in6p_icmp6filt = NULL;
118 1.1.2.1 itojun insque(in6p, head);
119 1.1.2.1 itojun so->so_pcb = (caddr_t)in6p;
120 1.1.2.1 itojun return(0);
121 1.1.2.1 itojun }
122 1.1.2.1 itojun
123 1.1.2.1 itojun int
124 1.1.2.1 itojun in6_pcbbind(in6p, nam)
125 1.1.2.1 itojun register struct in6pcb *in6p;
126 1.1.2.1 itojun struct mbuf *nam;
127 1.1.2.1 itojun {
128 1.1.2.1 itojun struct socket *so = in6p->in6p_socket;
129 1.1.2.1 itojun struct in6pcb *head = in6p->in6p_head;
130 1.1.2.1 itojun struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL;
131 1.1.2.1 itojun struct proc *p = curproc; /* XXX */
132 1.1.2.1 itojun u_short lport = 0;
133 1.1.2.1 itojun int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
134 1.1.2.1 itojun int error;
135 1.1.2.1 itojun
136 1.1.2.1 itojun if (in6p->in6p_lport || !IN6_IS_ADDR_ANY(&in6p->in6p_laddr))
137 1.1.2.1 itojun return(EINVAL);
138 1.1.2.1 itojun if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0 &&
139 1.1.2.1 itojun ((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0 ||
140 1.1.2.1 itojun (so->so_options & SO_ACCEPTCONN) == 0))
141 1.1.2.1 itojun wild = IN6PLOOKUP_WILDCARD;
142 1.1.2.1 itojun if (nam) {
143 1.1.2.1 itojun sin6 = mtod(nam, struct sockaddr_in6 *);
144 1.1.2.1 itojun if (nam->m_len != sizeof(*sin6))
145 1.1.2.1 itojun return(EINVAL);
146 1.1.2.1 itojun /*
147 1.1.2.1 itojun * We should check the family, but old programs
148 1.1.2.1 itojun * incorrectly fail to intialize it.
149 1.1.2.1 itojun */
150 1.1.2.1 itojun if (sin6->sin6_family != AF_INET6)
151 1.1.2.1 itojun return(EAFNOSUPPORT);
152 1.1.2.1 itojun
153 1.1.2.1 itojun /*
154 1.1.2.1 itojun * If the scope of the destination is link-local, embed the
155 1.1.2.1 itojun * interface index in the address.
156 1.1.2.1 itojun */
157 1.1.2.1 itojun if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) {
158 1.1.2.1 itojun /* XXX boundary check is assumed to be already done. */
159 1.1.2.1 itojun /* XXX sin6_scope_id is weaker than advanced-api. */
160 1.1.2.1 itojun struct in6_pktinfo *pi;
161 1.1.2.1 itojun if (in6p->in6p_outputopts &&
162 1.1.2.1 itojun (pi = in6p->in6p_outputopts->ip6po_pktinfo) &&
163 1.1.2.1 itojun pi->ipi6_ifindex) {
164 1.1.2.1 itojun sin6->sin6_addr.s6_addr16[1]
165 1.1.2.1 itojun = htons(pi->ipi6_ifindex);
166 1.1.2.1 itojun } else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)
167 1.1.2.1 itojun && in6p->in6p_moptions
168 1.1.2.1 itojun && in6p->in6p_moptions->im6o_multicast_ifp) {
169 1.1.2.1 itojun sin6->sin6_addr.s6_addr16[1] =
170 1.1.2.1 itojun htons(in6p->in6p_moptions->im6o_multicast_ifp->if_index);
171 1.1.2.1 itojun } else if (sin6->sin6_scope_id) {
172 1.1.2.1 itojun /* boundary check */
173 1.1.2.1 itojun if (sin6->sin6_scope_id < 0
174 1.1.2.1 itojun || if_index < sin6->sin6_scope_id) {
175 1.1.2.1 itojun return ENXIO; /* XXX EINVAL? */
176 1.1.2.1 itojun }
177 1.1.2.1 itojun sin6->sin6_addr.s6_addr16[1]
178 1.1.2.1 itojun = htons(sin6->sin6_scope_id & 0xffff);/*XXX*/
179 1.1.2.1 itojun /* this must be cleared for ifa_ifwithaddr() */
180 1.1.2.1 itojun sin6->sin6_scope_id = 0;
181 1.1.2.1 itojun }
182 1.1.2.1 itojun }
183 1.1.2.1 itojun
184 1.1.2.1 itojun lport = sin6->sin6_port;
185 1.1.2.1 itojun if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
186 1.1.2.1 itojun /*
187 1.1.2.1 itojun * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
188 1.1.2.1 itojun * allow compepte duplication of binding if
189 1.1.2.1 itojun * SO_REUSEPORT is set, or if SO_REUSEADDR is set
190 1.1.2.1 itojun * and a multicast address is bound on both
191 1.1.2.1 itojun * new and duplicated sockets.
192 1.1.2.1 itojun */
193 1.1.2.1 itojun if (so->so_options & SO_REUSEADDR)
194 1.1.2.1 itojun reuseport = SO_REUSEADDR|SO_REUSEPORT;
195 1.1.2.1 itojun } else if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
196 1.1.2.1 itojun struct sockaddr_in sin;
197 1.1.2.1 itojun
198 1.1.2.1 itojun bzero(&sin, sizeof(sin));
199 1.1.2.1 itojun sin.sin_len = sizeof(sin);
200 1.1.2.1 itojun sin.sin_family = AF_INET;
201 1.1.2.1 itojun bcopy(&sin6->sin6_addr.s6_addr32[3], &sin.sin_addr,
202 1.1.2.1 itojun sizeof(sin.sin_addr));
203 1.1.2.1 itojun if (ifa_ifwithaddr((struct sockaddr *)&sin) == 0)
204 1.1.2.1 itojun return EADDRNOTAVAIL;
205 1.1.2.1 itojun } else if (!IN6_IS_ADDR_ANY(&sin6->sin6_addr)) {
206 1.1.2.1 itojun struct ifaddr *ia = NULL;
207 1.1.2.1 itojun
208 1.1.2.1 itojun sin6->sin6_port = 0; /* yech... */
209 1.1.2.1 itojun if ((ia = ifa_ifwithaddr((struct sockaddr *)sin6)) == 0)
210 1.1.2.1 itojun return(EADDRNOTAVAIL);
211 1.1.2.1 itojun
212 1.1.2.1 itojun /*
213 1.1.2.1 itojun * XXX: bind to an anycast address might accidentally
214 1.1.2.1 itojun * cause sending a packet with anycast source address.
215 1.1.2.1 itojun */
216 1.1.2.1 itojun if (ia &&
217 1.1.2.1 itojun ((struct in6_ifaddr *)ia)->ia6_flags &
218 1.1.2.1 itojun (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|
219 1.1.2.1 itojun IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) {
220 1.1.2.1 itojun return(EADDRNOTAVAIL);
221 1.1.2.1 itojun }
222 1.1.2.1 itojun }
223 1.1.2.1 itojun if (lport) {
224 1.1.2.1 itojun /* GROSS */
225 1.1.2.1 itojun if (ntohs(lport) < IPV6PORT_RESERVED &&
226 1.1.2.1 itojun (error = suser(p->p_ucred, &p->p_acflag)))
227 1.1.2.1 itojun return(EACCES);
228 1.1.2.1 itojun
229 1.1.2.1 itojun if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
230 1.1.2.1 itojun /* should check this but we can't ... */
231 1.1.2.1 itojun #if 0
232 1.1.2.1 itojun struct inpcb *t;
233 1.1.2.1 itojun
234 1.1.2.1 itojun t = in_pcblookup_bind(&tcbtable,
235 1.1.2.1 itojun (struct in_addr *)&sin6->sin6_addr.s6_addr32[3],
236 1.1.2.1 itojun lport);
237 1.1.2.1 itojun if (t && (reuseport & t->inp_socket->so_options) == 0)
238 1.1.2.1 itojun return EADDRINUSE;
239 1.1.2.1 itojun #endif
240 1.1.2.1 itojun } else {
241 1.1.2.1 itojun struct in6pcb *t;
242 1.1.2.1 itojun
243 1.1.2.1 itojun t = in6_pcblookup(head, &zeroin6_addr, 0,
244 1.1.2.1 itojun &sin6->sin6_addr, lport, wild);
245 1.1.2.1 itojun if (t && (reuseport & t->in6p_socket->so_options) == 0)
246 1.1.2.1 itojun return(EADDRINUSE);
247 1.1.2.1 itojun }
248 1.1.2.1 itojun }
249 1.1.2.1 itojun in6p->in6p_laddr = sin6->sin6_addr;
250 1.1.2.1 itojun }
251 1.1.2.1 itojun if (lport == 0) {
252 1.1.2.1 itojun u_short last_port;
253 1.1.2.1 itojun void *t;
254 1.1.2.1 itojun
255 1.1.2.1 itojun /* XXX IN6P_LOWPORT */
256 1.1.2.1 itojun
257 1.1.2.1 itojun /* value out of range */
258 1.1.2.1 itojun if (head->in6p_lport < IPV6PORT_ANONMIN)
259 1.1.2.1 itojun head->in6p_lport = IPV6PORT_ANONMIN;
260 1.1.2.1 itojun else if (head->in6p_lport > IPV6PORT_ANONMAX)
261 1.1.2.1 itojun head->in6p_lport = IPV6PORT_ANONMIN;
262 1.1.2.1 itojun last_port = head->in6p_lport;
263 1.1.2.2 itojun goto startover;
264 1.1.2.1 itojun for (;;) {
265 1.1.2.1 itojun lport = htons(head->in6p_lport);
266 1.1.2.1 itojun if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)) {
267 1.1.2.1 itojun #if 0
268 1.1.2.1 itojun t = in_pcblookup_bind(&tcbtable,
269 1.1.2.1 itojun (struct in_addr *)&in6p->in6p_laddr.s6_addr32[3],
270 1.1.2.1 itojun lport);
271 1.1.2.1 itojun #else
272 1.1.2.1 itojun t = NULL;
273 1.1.2.1 itojun #endif
274 1.1.2.1 itojun } else {
275 1.1.2.1 itojun t = in6_pcblookup(head, &zeroin6_addr, 0,
276 1.1.2.1 itojun &in6p->in6p_laddr, lport, wild);
277 1.1.2.1 itojun }
278 1.1.2.1 itojun if (t == 0)
279 1.1.2.1 itojun break;
280 1.1.2.2 itojun startover:
281 1.1.2.1 itojun if (head->in6p_lport >= IPV6PORT_ANONMAX)
282 1.1.2.1 itojun head->in6p_lport = IPV6PORT_ANONMIN;
283 1.1.2.1 itojun else
284 1.1.2.1 itojun head->in6p_lport++;
285 1.1.2.1 itojun if (head->in6p_lport == last_port)
286 1.1.2.1 itojun return (EADDRINUSE);
287 1.1.2.1 itojun }
288 1.1.2.1 itojun }
289 1.1.2.1 itojun in6p->in6p_lport = lport;
290 1.1.2.1 itojun in6p->in6p_flowinfo = sin6 ? sin6->sin6_flowinfo : 0; /*XXX*/
291 1.1.2.1 itojun return(0);
292 1.1.2.1 itojun }
293 1.1.2.1 itojun
294 1.1.2.1 itojun /*
295 1.1.2.1 itojun * Connect from a socket to a specified address.
296 1.1.2.1 itojun * Both address and port must be specified in argument sin6.
297 1.1.2.1 itojun * If don't have a local address for this socket yet,
298 1.1.2.1 itojun * then pick one.
299 1.1.2.1 itojun */
300 1.1.2.1 itojun int
301 1.1.2.1 itojun in6_pcbconnect(in6p, nam)
302 1.1.2.1 itojun struct in6pcb *in6p;
303 1.1.2.1 itojun struct mbuf *nam;
304 1.1.2.1 itojun {
305 1.1.2.1 itojun struct in6_addr *in6a = NULL;
306 1.1.2.1 itojun struct sockaddr_in6 *sin6 = mtod(nam, struct sockaddr_in6 *);
307 1.1.2.1 itojun struct in6_pktinfo *pi;
308 1.1.2.1 itojun struct ifnet *ifp = NULL; /* outgoing interface */
309 1.1.2.1 itojun int error = 0;
310 1.1.2.1 itojun struct in6_addr mapped;
311 1.1.2.1 itojun
312 1.1.2.1 itojun (void)&in6a; /* XXX fool gcc */
313 1.1.2.1 itojun
314 1.1.2.1 itojun if (nam->m_len != sizeof(*sin6))
315 1.1.2.1 itojun return(EINVAL);
316 1.1.2.1 itojun if (sin6->sin6_family != AF_INET6)
317 1.1.2.1 itojun return(EAFNOSUPPORT);
318 1.1.2.1 itojun if (sin6->sin6_port == 0)
319 1.1.2.1 itojun return(EADDRNOTAVAIL);
320 1.1.2.1 itojun
321 1.1.2.1 itojun /* sanity check for mapped address case */
322 1.1.2.1 itojun if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
323 1.1.2.1 itojun if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr))
324 1.1.2.1 itojun in6p->in6p_laddr.s6_addr16[5] = htons(0xffff);
325 1.1.2.1 itojun if (!IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr))
326 1.1.2.1 itojun return EINVAL;
327 1.1.2.1 itojun } else {
328 1.1.2.1 itojun if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr))
329 1.1.2.1 itojun return EINVAL;
330 1.1.2.1 itojun }
331 1.1.2.1 itojun
332 1.1.2.1 itojun /*
333 1.1.2.1 itojun * If the scope of the destination is link-local, embed the interface
334 1.1.2.1 itojun * index in the address.
335 1.1.2.1 itojun */
336 1.1.2.1 itojun if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) {
337 1.1.2.1 itojun /* XXX boundary check is assumed to be already done. */
338 1.1.2.1 itojun /* XXX sin6_scope_id is weaker than advanced-api. */
339 1.1.2.1 itojun if (in6p->in6p_outputopts &&
340 1.1.2.1 itojun (pi = in6p->in6p_outputopts->ip6po_pktinfo) &&
341 1.1.2.1 itojun pi->ipi6_ifindex) {
342 1.1.2.1 itojun sin6->sin6_addr.s6_addr16[1] = htons(pi->ipi6_ifindex);
343 1.1.2.1 itojun ifp = ifindex2ifnet[pi->ipi6_ifindex];
344 1.1.2.1 itojun }
345 1.1.2.1 itojun else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) &&
346 1.1.2.1 itojun in6p->in6p_moptions &&
347 1.1.2.1 itojun in6p->in6p_moptions->im6o_multicast_ifp) {
348 1.1.2.1 itojun sin6->sin6_addr.s6_addr16[1] =
349 1.1.2.1 itojun htons(in6p->in6p_moptions->im6o_multicast_ifp->if_index);
350 1.1.2.1 itojun ifp = ifindex2ifnet[in6p->in6p_moptions->im6o_multicast_ifp->if_index];
351 1.1.2.1 itojun } else if (sin6->sin6_scope_id) {
352 1.1.2.1 itojun /* boundary check */
353 1.1.2.1 itojun if (sin6->sin6_scope_id < 0
354 1.1.2.1 itojun || if_index < sin6->sin6_scope_id) {
355 1.1.2.1 itojun return ENXIO; /* XXX EINVAL? */
356 1.1.2.1 itojun }
357 1.1.2.1 itojun sin6->sin6_addr.s6_addr16[1]
358 1.1.2.1 itojun = htons(sin6->sin6_scope_id & 0xffff);/*XXX*/
359 1.1.2.1 itojun ifp = ifindex2ifnet[sin6->sin6_scope_id];
360 1.1.2.1 itojun }
361 1.1.2.1 itojun }
362 1.1.2.1 itojun
363 1.1.2.1 itojun /* Source address selection. */
364 1.1.2.1 itojun if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)
365 1.1.2.1 itojun && in6p->in6p_laddr.s6_addr32[3] == 0) {
366 1.1.2.1 itojun struct sockaddr_in sin, *sinp;
367 1.1.2.1 itojun
368 1.1.2.1 itojun bzero(&sin, sizeof(sin));
369 1.1.2.1 itojun sin.sin_len = sizeof(sin);
370 1.1.2.1 itojun sin.sin_family = AF_INET;
371 1.1.2.1 itojun bcopy(&sin6->sin6_addr.s6_addr32[3], &sin.sin_addr,
372 1.1.2.1 itojun sizeof(sin.sin_addr));
373 1.1.2.1 itojun sinp = in_selectsrc(&sin, (struct route *)&in6p->in6p_route,
374 1.1.2.1 itojun in6p->in6p_socket->so_options, NULL, &error);
375 1.1.2.1 itojun if (sinp == 0) {
376 1.1.2.1 itojun if (error == 0)
377 1.1.2.1 itojun error = EADDRNOTAVAIL;
378 1.1.2.1 itojun return(error);
379 1.1.2.1 itojun }
380 1.1.2.1 itojun bzero(&mapped, sizeof(mapped));
381 1.1.2.1 itojun mapped.s6_addr16[5] = htons(0xffff);
382 1.1.2.1 itojun bcopy(&sinp->sin_addr, &mapped.s6_addr32[3], sizeof(sinp->sin_addr));
383 1.1.2.1 itojun in6a = &mapped;
384 1.1.2.1 itojun } else if (IN6_IS_ADDR_ANY(&in6p->in6p_laddr)) {
385 1.1.2.1 itojun in6a = in6_selectsrc(sin6, in6p->in6p_outputopts,
386 1.1.2.1 itojun in6p->in6p_moptions, &in6p->in6p_route,
387 1.1.2.1 itojun &error);
388 1.1.2.1 itojun if (in6a == 0) {
389 1.1.2.1 itojun if (error == 0)
390 1.1.2.1 itojun error = EADDRNOTAVAIL;
391 1.1.2.1 itojun return(error);
392 1.1.2.1 itojun }
393 1.1.2.1 itojun }
394 1.1.2.1 itojun if (in6p->in6p_route.ro_rt)
395 1.1.2.1 itojun ifp = in6p->in6p_route.ro_rt->rt_ifp;
396 1.1.2.1 itojun
397 1.1.2.1 itojun /*
398 1.1.2.1 itojun * Default hop limit selection. If a hoplimit was specified via ioctl,
399 1.1.2.1 itojun * use it. Else if the outgoing interface is detected and the current
400 1.1.2.1 itojun * hop limit of the interface was specified by router advertisement,
401 1.1.2.1 itojun * use the value.
402 1.1.2.1 itojun * Otherwise, use the system default hoplimit.
403 1.1.2.1 itojun */
404 1.1.2.1 itojun if (in6p->in6p_hops >= 0)
405 1.1.2.1 itojun in6p->in6p_ip6.ip6_hlim = (u_int8_t)in6p->in6p_hops;
406 1.1.2.1 itojun else if (ifp)
407 1.1.2.1 itojun in6p->in6p_ip6.ip6_hlim = nd_ifinfo[ifp->if_index].chlim;
408 1.1.2.1 itojun else
409 1.1.2.1 itojun in6p->in6p_ip6.ip6_hlim = ip6_defhlim;
410 1.1.2.1 itojun
411 1.1.2.1 itojun if (in6_pcblookup(in6p->in6p_head,
412 1.1.2.1 itojun &sin6->sin6_addr,
413 1.1.2.1 itojun sin6->sin6_port,
414 1.1.2.1 itojun IN6_IS_ADDR_ANY(&in6p->in6p_laddr) ?
415 1.1.2.1 itojun in6a : &in6p->in6p_laddr,
416 1.1.2.1 itojun in6p->in6p_lport,
417 1.1.2.1 itojun 0))
418 1.1.2.1 itojun return(EADDRINUSE);
419 1.1.2.1 itojun if (IN6_IS_ADDR_ANY(&in6p->in6p_laddr)
420 1.1.2.1 itojun || (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)
421 1.1.2.1 itojun && in6p->in6p_laddr.s6_addr32[3] == 0)) {
422 1.1.2.1 itojun if (in6p->in6p_lport == 0)
423 1.1.2.1 itojun (void)in6_pcbbind(in6p, (struct mbuf *)0);
424 1.1.2.1 itojun in6p->in6p_laddr = *in6a;
425 1.1.2.1 itojun }
426 1.1.2.1 itojun in6p->in6p_faddr = sin6->sin6_addr;
427 1.1.2.1 itojun in6p->in6p_fport = sin6->sin6_port;
428 1.1.2.1 itojun /*
429 1.1.2.1 itojun * xxx kazu flowlabel is necessary for connect?
430 1.1.2.1 itojun * but if this line is missing, the garbage value remains.
431 1.1.2.1 itojun */
432 1.1.2.1 itojun in6p->in6p_flowinfo = sin6->sin6_flowinfo;
433 1.1.2.1 itojun return(0);
434 1.1.2.1 itojun }
435 1.1.2.1 itojun
436 1.1.2.1 itojun /*
437 1.1.2.1 itojun * Return an IPv6 address, which is the most appropriate for given
438 1.1.2.1 itojun * destination and user specified options.
439 1.1.2.1 itojun * If necessary, this function lookups the routing table and return
440 1.1.2.1 itojun * an entry to the caller for later use.
441 1.1.2.1 itojun */
442 1.1.2.1 itojun struct in6_addr *
443 1.1.2.1 itojun in6_selectsrc(dstsock, opts, mopts, ro, errorp)
444 1.1.2.1 itojun struct sockaddr_in6 *dstsock;
445 1.1.2.1 itojun struct ip6_pktopts *opts;
446 1.1.2.1 itojun struct ip6_moptions *mopts;
447 1.1.2.1 itojun struct route_in6 *ro;
448 1.1.2.1 itojun int *errorp;
449 1.1.2.1 itojun {
450 1.1.2.1 itojun struct in6_addr *dst;
451 1.1.2.1 itojun struct in6_ifaddr *ia6 = 0;
452 1.1.2.1 itojun struct in6_pktinfo *pi;
453 1.1.2.1 itojun
454 1.1.2.1 itojun dst = &dstsock->sin6_addr;
455 1.1.2.1 itojun *errorp = 0;
456 1.1.2.1 itojun
457 1.1.2.1 itojun /*
458 1.1.2.1 itojun * If the source address is explicitly specified by the caller,
459 1.1.2.1 itojun * use it.
460 1.1.2.1 itojun * If the caller doesn't specify the source address but
461 1.1.2.1 itojun * the outgoing interface, use an address associated with
462 1.1.2.1 itojun * the interface.
463 1.1.2.1 itojun */
464 1.1.2.1 itojun if (opts && (pi = opts->ip6po_pktinfo)) {
465 1.1.2.1 itojun if (!IN6_IS_ADDR_ANY(&pi->ipi6_addr))
466 1.1.2.1 itojun return(&pi->ipi6_addr);
467 1.1.2.1 itojun else if (pi->ipi6_ifindex) {
468 1.1.2.1 itojun /* XXX boundary check is assumed to be already done. */
469 1.1.2.1 itojun ia6 = in6_ifawithscope(ifindex2ifnet[pi->ipi6_ifindex],
470 1.1.2.1 itojun dst);
471 1.1.2.1 itojun if (ia6 == 0) {
472 1.1.2.1 itojun *errorp = EADDRNOTAVAIL;
473 1.1.2.1 itojun return(0);
474 1.1.2.1 itojun }
475 1.1.2.1 itojun return(&satosin6(&ia6->ia_addr)->sin6_addr);
476 1.1.2.1 itojun }
477 1.1.2.1 itojun }
478 1.1.2.1 itojun
479 1.1.2.1 itojun /*
480 1.1.2.1 itojun * If the destination address is a multicast address and
481 1.1.2.1 itojun * the outgoing interface for the address is specified
482 1.1.2.1 itojun * by the caller, use an address associated with the interface.
483 1.1.2.1 itojun * There is a sanity check here; if the destination has node-local
484 1.1.2.1 itojun * scope, the outgoing interfacde should be a loopback address.
485 1.1.2.1 itojun * Even if the outgoing interface is not specified, we also
486 1.1.2.1 itojun * choose a loopback interface as the outgoing interface.
487 1.1.2.1 itojun */
488 1.1.2.1 itojun if (IN6_IS_ADDR_MULTICAST(dst)) {
489 1.1.2.1 itojun struct ifnet *ifp = mopts ? mopts->im6o_multicast_ifp : NULL;
490 1.1.2.1 itojun #ifdef __bsdi__
491 1.1.2.1 itojun extern struct ifnet loif;
492 1.1.2.1 itojun #endif
493 1.1.2.1 itojun
494 1.1.2.1 itojun if (ifp == NULL && IN6_IS_ADDR_MC_NODELOCAL(dst)) {
495 1.1.2.1 itojun #ifdef __bsdi__
496 1.1.2.1 itojun ifp = &loif;
497 1.1.2.1 itojun #else
498 1.1.2.1 itojun ifp = &loif[0];
499 1.1.2.1 itojun #endif
500 1.1.2.1 itojun }
501 1.1.2.1 itojun
502 1.1.2.1 itojun if (ifp) {
503 1.1.2.1 itojun ia6 = in6_ifawithscope(ifp, dst);
504 1.1.2.1 itojun if (ia6 == 0) {
505 1.1.2.1 itojun *errorp = EADDRNOTAVAIL;
506 1.1.2.1 itojun return(0);
507 1.1.2.1 itojun }
508 1.1.2.1 itojun return(&satosin6(&ia6->ia_addr)->sin6_addr);
509 1.1.2.1 itojun }
510 1.1.2.1 itojun }
511 1.1.2.1 itojun
512 1.1.2.1 itojun /*
513 1.1.2.1 itojun * XXX How should we use sin6_scope_id???
514 1.1.2.1 itojun */
515 1.1.2.1 itojun
516 1.1.2.1 itojun /*
517 1.1.2.1 itojun * If the next hop address for the packet is specified
518 1.1.2.1 itojun * by caller, use an address associated with the route
519 1.1.2.1 itojun * to the next hop.
520 1.1.2.1 itojun */
521 1.1.2.1 itojun {
522 1.1.2.1 itojun struct sockaddr_in6 *sin6_next;
523 1.1.2.1 itojun struct rtentry *rt;
524 1.1.2.1 itojun
525 1.1.2.1 itojun if (opts && opts->ip6po_nexthop) {
526 1.1.2.1 itojun sin6_next = satosin6(opts->ip6po_nexthop);
527 1.1.2.1 itojun rt = nd6_lookup(&sin6_next->sin6_addr, 1, NULL);
528 1.1.2.1 itojun if (rt) {
529 1.1.2.1 itojun ia6 = in6_ifawithscope(rt->rt_ifp, dst);
530 1.1.2.1 itojun if (ia6 == 0)
531 1.1.2.1 itojun ia6 = ifatoia6(rt->rt_ifa);
532 1.1.2.1 itojun }
533 1.1.2.1 itojun if (ia6 == 0) {
534 1.1.2.1 itojun *errorp = EADDRNOTAVAIL;
535 1.1.2.1 itojun return(0);
536 1.1.2.1 itojun }
537 1.1.2.1 itojun return(&satosin6(&ia6->ia_addr)->sin6_addr);
538 1.1.2.1 itojun }
539 1.1.2.1 itojun }
540 1.1.2.1 itojun
541 1.1.2.1 itojun /*
542 1.1.2.1 itojun * If route is known or can be allocated now,
543 1.1.2.1 itojun * our src addr is taken from the i/f, else punt.
544 1.1.2.1 itojun */
545 1.1.2.1 itojun if (ro) {
546 1.1.2.1 itojun if (ro->ro_rt &&
547 1.1.2.1 itojun !IN6_ARE_ADDR_EQUAL(&satosin6(&ro->ro_dst)->sin6_addr, dst)) {
548 1.1.2.1 itojun RTFREE(ro->ro_rt);
549 1.1.2.1 itojun ro->ro_rt = (struct rtentry *)0;
550 1.1.2.1 itojun }
551 1.1.2.1 itojun if (ro->ro_rt == (struct rtentry *)0 ||
552 1.1.2.1 itojun ro->ro_rt->rt_ifp == (struct ifnet *)0) {
553 1.1.2.1 itojun /* No route yet, so try to acquire one */
554 1.1.2.1 itojun bzero(&ro->ro_dst, sizeof(struct sockaddr_in6));
555 1.1.2.1 itojun ro->ro_dst.sin6_family = AF_INET6;
556 1.1.2.1 itojun ro->ro_dst.sin6_len = sizeof(struct sockaddr_in6);
557 1.1.2.1 itojun ro->ro_dst.sin6_addr = *dst;
558 1.1.2.1 itojun if (IN6_IS_ADDR_MULTICAST(dst)) {
559 1.1.2.1 itojun #ifdef __FreeBSD__
560 1.1.2.1 itojun ro->ro_rt = rtalloc1(&((struct route *)ro)
561 1.1.2.1 itojun ->ro_dst, 0, 0UL);
562 1.1.2.1 itojun #endif /*__FreeBSD__*/
563 1.1.2.1 itojun #if defined(__bsdi__) || defined(__NetBSD__)
564 1.1.2.1 itojun ro->ro_rt = rtalloc1(&((struct route *)ro)
565 1.1.2.1 itojun ->ro_dst, 0);
566 1.1.2.1 itojun #endif /*__bsdi__*/
567 1.1.2.1 itojun } else {
568 1.1.2.1 itojun #if 0 /* XXX Is this correct? */
569 1.1.2.1 itojun rtcalloc((struct route *)ro);
570 1.1.2.1 itojun #else
571 1.1.2.1 itojun rtalloc((struct route *)ro);
572 1.1.2.1 itojun #endif
573 1.1.2.1 itojun }
574 1.1.2.1 itojun }
575 1.1.2.1 itojun
576 1.1.2.1 itojun /*
577 1.1.2.1 itojun * in_pcbconnect() checks out IFF_LOOPBACK to skip using
578 1.1.2.1 itojun * the address. But we don't know why it does so.
579 1.1.2.1 itojun * It is necessary to ensure the scope even for lo0
580 1.1.2.1 itojun * so doesn't check out IFF_LOOPBACK.
581 1.1.2.1 itojun */
582 1.1.2.1 itojun
583 1.1.2.1 itojun if (ro->ro_rt) {
584 1.1.2.1 itojun ia6 = in6_ifawithscope(ro->ro_rt->rt_ifa->ifa_ifp, dst);
585 1.1.2.1 itojun if (ia6 == 0) /* xxx scope error ?*/
586 1.1.2.1 itojun ia6 = ifatoia6(ro->ro_rt->rt_ifa);
587 1.1.2.1 itojun }
588 1.1.2.1 itojun #if 0
589 1.1.2.1 itojun /*
590 1.1.2.1 itojun * xxx The followings are necessary? (kazu)
591 1.1.2.1 itojun * I don't think so.
592 1.1.2.1 itojun * It's for SO_DONTROUTE option in IPv4.(jinmei)
593 1.1.2.1 itojun */
594 1.1.2.1 itojun if (ia6 == 0) {
595 1.1.2.1 itojun struct sockaddr_in6 sin6 = {sizeof(sin6), AF_INET6, 0};
596 1.1.2.1 itojun
597 1.1.2.1 itojun sin6->sin6_addr = *dst;
598 1.1.2.1 itojun
599 1.1.2.1 itojun ia6 = ifatoia6(ifa_ifwithdstaddr(sin6tosa(&sin6)));
600 1.1.2.1 itojun if (ia6 == 0)
601 1.1.2.1 itojun ia6 = ifatoia6(ifa_ifwithnet(sin6tosa(&sin6)));
602 1.1.2.1 itojun if (ia6 == 0)
603 1.1.2.1 itojun return(0);
604 1.1.2.1 itojun return(&satosin6(&ia6->ia_addr)->sin6_addr);
605 1.1.2.1 itojun }
606 1.1.2.1 itojun #endif /* 0 */
607 1.1.2.1 itojun if (ia6 == 0) {
608 1.1.2.1 itojun *errorp = EHOSTUNREACH; /* no route */
609 1.1.2.1 itojun return(0);
610 1.1.2.1 itojun }
611 1.1.2.1 itojun return(&satosin6(&ia6->ia_addr)->sin6_addr);
612 1.1.2.1 itojun }
613 1.1.2.1 itojun
614 1.1.2.1 itojun *errorp = EADDRNOTAVAIL;
615 1.1.2.1 itojun return(0);
616 1.1.2.1 itojun }
617 1.1.2.1 itojun
618 1.1.2.1 itojun void
619 1.1.2.1 itojun in6_pcbdisconnect(in6p)
620 1.1.2.1 itojun struct in6pcb *in6p;
621 1.1.2.1 itojun {
622 1.1.2.1 itojun bzero((caddr_t)&in6p->in6p_faddr, sizeof(in6p->in6p_faddr));
623 1.1.2.1 itojun in6p->in6p_fport = 0;
624 1.1.2.1 itojun if (in6p->in6p_socket->so_state & SS_NOFDREF)
625 1.1.2.1 itojun in6_pcbdetach(in6p);
626 1.1.2.1 itojun }
627 1.1.2.1 itojun
628 1.1.2.1 itojun void
629 1.1.2.1 itojun in6_pcbdetach(in6p)
630 1.1.2.1 itojun struct in6pcb *in6p;
631 1.1.2.1 itojun {
632 1.1.2.1 itojun struct socket *so = in6p->in6p_socket;
633 1.1.2.1 itojun
634 1.1.2.1 itojun #ifdef IPSEC
635 1.1.2.1 itojun if (sotoin6pcb(so) != 0)
636 1.1.2.1 itojun key_freeso(so);
637 1.1.2.1 itojun ipsec6_delete_pcbpolicy(in6p);
638 1.1.2.1 itojun #endif /* IPSEC */
639 1.1.2.1 itojun sotoin6pcb(so) = 0;
640 1.1.2.1 itojun sofree(so);
641 1.1.2.1 itojun if (in6p->in6p_options)
642 1.1.2.1 itojun m_freem(in6p->in6p_options);
643 1.1.2.1 itojun if (in6p->in6p_outputopts) {
644 1.1.2.1 itojun if (in6p->in6p_outputopts->ip6po_rthdr &&
645 1.1.2.1 itojun in6p->in6p_outputopts->ip6po_route.ro_rt)
646 1.1.2.1 itojun RTFREE(in6p->in6p_outputopts->ip6po_route.ro_rt);
647 1.1.2.1 itojun if (in6p->in6p_outputopts->ip6po_m)
648 1.1.2.1 itojun (void)m_free(in6p->in6p_outputopts->ip6po_m);
649 1.1.2.1 itojun free(in6p->in6p_outputopts, M_IP6OPT);
650 1.1.2.1 itojun }
651 1.1.2.1 itojun if (in6p->in6p_route.ro_rt)
652 1.1.2.1 itojun rtfree(in6p->in6p_route.ro_rt);
653 1.1.2.1 itojun ip6_freemoptions(in6p->in6p_moptions);
654 1.1.2.1 itojun remque(in6p);
655 1.1.2.1 itojun FREE(in6p, M_PCB);
656 1.1.2.1 itojun }
657 1.1.2.1 itojun
658 1.1.2.1 itojun void
659 1.1.2.1 itojun in6_setsockaddr(in6p, nam)
660 1.1.2.1 itojun struct in6pcb *in6p;
661 1.1.2.1 itojun struct mbuf *nam;
662 1.1.2.1 itojun {
663 1.1.2.1 itojun struct sockaddr_in6 *sin6;
664 1.1.2.1 itojun
665 1.1.2.1 itojun nam->m_len = sizeof(*sin6);
666 1.1.2.1 itojun sin6 = mtod(nam, struct sockaddr_in6 *);
667 1.1.2.1 itojun bzero((caddr_t)sin6, sizeof(*sin6));
668 1.1.2.1 itojun sin6->sin6_family = AF_INET6;
669 1.1.2.1 itojun sin6->sin6_len = sizeof(struct sockaddr_in6);
670 1.1.2.1 itojun sin6->sin6_port = in6p->in6p_lport;
671 1.1.2.1 itojun sin6->sin6_addr = in6p->in6p_laddr;
672 1.1.2.1 itojun if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
673 1.1.2.1 itojun sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]);
674 1.1.2.1 itojun else
675 1.1.2.1 itojun sin6->sin6_scope_id = 0; /*XXX*/
676 1.1.2.1 itojun if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
677 1.1.2.1 itojun sin6->sin6_addr.s6_addr16[1] = 0;
678 1.1.2.1 itojun }
679 1.1.2.1 itojun
680 1.1.2.1 itojun void
681 1.1.2.1 itojun in6_setpeeraddr(in6p, nam)
682 1.1.2.1 itojun struct in6pcb *in6p;
683 1.1.2.1 itojun struct mbuf *nam;
684 1.1.2.1 itojun {
685 1.1.2.1 itojun struct sockaddr_in6 *sin6;
686 1.1.2.1 itojun
687 1.1.2.1 itojun nam->m_len = sizeof(*sin6);
688 1.1.2.1 itojun sin6 = mtod(nam, struct sockaddr_in6 *);
689 1.1.2.1 itojun bzero((caddr_t)sin6, sizeof(*sin6));
690 1.1.2.1 itojun sin6->sin6_family = AF_INET6;
691 1.1.2.1 itojun sin6->sin6_len = sizeof(struct sockaddr_in6);
692 1.1.2.1 itojun sin6->sin6_port = in6p->in6p_fport;
693 1.1.2.1 itojun sin6->sin6_addr = in6p->in6p_faddr;
694 1.1.2.1 itojun if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
695 1.1.2.1 itojun sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]);
696 1.1.2.1 itojun else
697 1.1.2.1 itojun sin6->sin6_scope_id = 0; /*XXX*/
698 1.1.2.1 itojun if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
699 1.1.2.1 itojun sin6->sin6_addr.s6_addr16[1] = 0;
700 1.1.2.1 itojun }
701 1.1.2.1 itojun
702 1.1.2.1 itojun /*
703 1.1.2.1 itojun * Pass some notification to all connections of a protocol
704 1.1.2.1 itojun * associated with address dst. The local address and/or port numbers
705 1.1.2.1 itojun * may be specified to limit the search. The "usual action" will be
706 1.1.2.1 itojun * taken, depending on the ctlinput cmd. The caller must filter any
707 1.1.2.1 itojun * cmds that are uninteresting (e.g., no error in the map).
708 1.1.2.1 itojun * Call the protocol specific routine (if any) to report
709 1.1.2.1 itojun * any errors for each matching socket.
710 1.1.2.1 itojun *
711 1.1.2.2 itojun * Must be called at splsoftnet.
712 1.1.2.1 itojun */
713 1.1.2.1 itojun int
714 1.1.2.1 itojun in6_pcbnotify(head, dst, fport_arg, laddr6, lport_arg, cmd, notify)
715 1.1.2.1 itojun struct in6pcb *head;
716 1.1.2.1 itojun struct sockaddr *dst;
717 1.1.2.1 itojun u_int fport_arg, lport_arg;
718 1.1.2.1 itojun struct in6_addr *laddr6;
719 1.1.2.1 itojun int cmd;
720 1.1.2.1 itojun void (*notify) __P((struct in6pcb *, int));
721 1.1.2.1 itojun {
722 1.1.2.1 itojun struct in6pcb *in6p, *oin6p;
723 1.1.2.1 itojun struct in6_addr faddr6;
724 1.1.2.1 itojun u_short fport = fport_arg, lport = lport_arg;
725 1.1.2.1 itojun int errno;
726 1.1.2.1 itojun int nmatch = 0;
727 1.1.2.1 itojun
728 1.1.2.1 itojun if ((unsigned)cmd > PRC_NCMDS || dst->sa_family != AF_INET6)
729 1.1.2.1 itojun return 0;
730 1.1.2.1 itojun faddr6 = ((struct sockaddr_in6 *)dst)->sin6_addr;
731 1.1.2.1 itojun if (IN6_IS_ADDR_ANY(&faddr6))
732 1.1.2.1 itojun return 0;
733 1.1.2.1 itojun
734 1.1.2.1 itojun /*
735 1.1.2.1 itojun * Redirects go to all references to the destination,
736 1.1.2.1 itojun * and use in_rtchange to invalidate the route cache.
737 1.1.2.1 itojun * Dead host indications: notify all references to the destination.
738 1.1.2.1 itojun * Otherwise, if we have knowledge of the local port and address,
739 1.1.2.1 itojun * deliver only to that socket.
740 1.1.2.1 itojun */
741 1.1.2.1 itojun if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
742 1.1.2.1 itojun fport = 0;
743 1.1.2.1 itojun lport = 0;
744 1.1.2.1 itojun bzero((caddr_t)laddr6, sizeof(*laddr6));
745 1.1.2.1 itojun if (cmd != PRC_HOSTDEAD)
746 1.1.2.1 itojun notify = in6_rtchange;
747 1.1.2.1 itojun }
748 1.1.2.1 itojun if (notify == NULL)
749 1.1.2.1 itojun return 0;
750 1.1.2.1 itojun errno = inet6ctlerrmap[cmd];
751 1.1.2.1 itojun for (in6p = head->in6p_next; in6p != head;) {
752 1.1.2.1 itojun if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,&faddr6) ||
753 1.1.2.1 itojun in6p->in6p_socket == 0 ||
754 1.1.2.1 itojun (lport && in6p->in6p_lport != lport) ||
755 1.1.2.1 itojun (!IN6_IS_ADDR_ANY(laddr6) &&
756 1.1.2.1 itojun !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6)) ||
757 1.1.2.1 itojun (fport && in6p->in6p_fport != fport)) {
758 1.1.2.1 itojun in6p = in6p->in6p_next;
759 1.1.2.1 itojun continue;
760 1.1.2.1 itojun }
761 1.1.2.1 itojun oin6p = in6p;
762 1.1.2.1 itojun in6p = in6p->in6p_next;
763 1.1.2.1 itojun (*notify)(oin6p, errno);
764 1.1.2.1 itojun nmatch++;
765 1.1.2.1 itojun }
766 1.1.2.1 itojun return nmatch;
767 1.1.2.1 itojun }
768 1.1.2.1 itojun
769 1.1.2.1 itojun /*
770 1.1.2.1 itojun * Check for alternatives when higher level complains
771 1.1.2.1 itojun * about service problems. For now, invalidate cached
772 1.1.2.1 itojun * routing information. If the route was created dynamically
773 1.1.2.1 itojun * (by a redirect), time to try a default gateway again.
774 1.1.2.1 itojun */
775 1.1.2.1 itojun void
776 1.1.2.1 itojun in6_losing(in6p)
777 1.1.2.1 itojun struct in6pcb *in6p;
778 1.1.2.1 itojun {
779 1.1.2.1 itojun struct rtentry *rt;
780 1.1.2.1 itojun struct rt_addrinfo info;
781 1.1.2.1 itojun
782 1.1.2.1 itojun if ((rt = in6p->in6p_route.ro_rt) != NULL) {
783 1.1.2.1 itojun in6p->in6p_route.ro_rt = 0;
784 1.1.2.1 itojun bzero((caddr_t)&info, sizeof(info));
785 1.1.2.1 itojun info.rti_info[RTAX_DST] =
786 1.1.2.1 itojun (struct sockaddr *)&in6p->in6p_route.ro_dst;
787 1.1.2.1 itojun info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
788 1.1.2.1 itojun info.rti_info[RTAX_NETMASK] = rt_mask(rt);
789 1.1.2.1 itojun rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
790 1.1.2.1 itojun if (rt->rt_flags & RTF_DYNAMIC)
791 1.1.2.1 itojun (void)rtrequest(RTM_DELETE, rt_key(rt),
792 1.1.2.1 itojun rt->rt_gateway, rt_mask(rt), rt->rt_flags,
793 1.1.2.1 itojun (struct rtentry **)0);
794 1.1.2.1 itojun else
795 1.1.2.1 itojun /*
796 1.1.2.1 itojun * A new route can be allocated
797 1.1.2.1 itojun * the next time output is attempted.
798 1.1.2.1 itojun */
799 1.1.2.1 itojun rtfree(rt);
800 1.1.2.1 itojun }
801 1.1.2.1 itojun }
802 1.1.2.1 itojun
803 1.1.2.1 itojun /*
804 1.1.2.1 itojun * After a routing change, flush old routing
805 1.1.2.1 itojun * and allocate a (hopefully) better one.
806 1.1.2.1 itojun */
807 1.1.2.1 itojun void
808 1.1.2.1 itojun in6_rtchange(in6p, errno)
809 1.1.2.1 itojun struct in6pcb *in6p;
810 1.1.2.1 itojun int errno;
811 1.1.2.1 itojun {
812 1.1.2.1 itojun if (in6p->in6p_route.ro_rt) {
813 1.1.2.1 itojun rtfree(in6p->in6p_route.ro_rt);
814 1.1.2.1 itojun in6p->in6p_route.ro_rt = 0;
815 1.1.2.1 itojun /*
816 1.1.2.1 itojun * A new route can be allocated the next time
817 1.1.2.1 itojun * output is attempted.
818 1.1.2.1 itojun */
819 1.1.2.1 itojun }
820 1.1.2.1 itojun }
821 1.1.2.1 itojun
822 1.1.2.1 itojun struct in6pcb *
823 1.1.2.1 itojun in6_pcblookup(head, faddr6, fport_arg, laddr6, lport_arg, flags)
824 1.1.2.1 itojun struct in6pcb *head;
825 1.1.2.1 itojun struct in6_addr *faddr6, *laddr6;
826 1.1.2.1 itojun u_int fport_arg, lport_arg;
827 1.1.2.1 itojun int flags;
828 1.1.2.1 itojun {
829 1.1.2.1 itojun struct in6pcb *in6p, *match = 0;
830 1.1.2.1 itojun int matchwild = 3, wildcard;
831 1.1.2.1 itojun u_short fport = fport_arg, lport = lport_arg;
832 1.1.2.1 itojun
833 1.1.2.1 itojun for (in6p = head->in6p_next; in6p != head; in6p = in6p->in6p_next) {
834 1.1.2.1 itojun if (in6p->in6p_lport != lport)
835 1.1.2.1 itojun continue;
836 1.1.2.1 itojun wildcard = 0;
837 1.1.2.1 itojun if (!IN6_IS_ADDR_ANY(&in6p->in6p_laddr)) {
838 1.1.2.1 itojun if (IN6_IS_ADDR_ANY(laddr6))
839 1.1.2.1 itojun wildcard++;
840 1.1.2.1 itojun else if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6))
841 1.1.2.1 itojun continue;
842 1.1.2.1 itojun } else {
843 1.1.2.1 itojun if (!IN6_IS_ADDR_ANY(laddr6))
844 1.1.2.1 itojun wildcard++;
845 1.1.2.1 itojun }
846 1.1.2.1 itojun if (!IN6_IS_ADDR_ANY(&in6p->in6p_faddr)) {
847 1.1.2.1 itojun if (IN6_IS_ADDR_ANY(faddr6))
848 1.1.2.1 itojun wildcard++;
849 1.1.2.1 itojun else if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, faddr6)
850 1.1.2.1 itojun || in6p->in6p_fport != fport)
851 1.1.2.1 itojun continue;
852 1.1.2.1 itojun } else {
853 1.1.2.1 itojun if (!IN6_IS_ADDR_ANY(faddr6))
854 1.1.2.1 itojun wildcard++;
855 1.1.2.1 itojun }
856 1.1.2.1 itojun if (wildcard && (flags & IN6PLOOKUP_WILDCARD) == 0)
857 1.1.2.1 itojun continue;
858 1.1.2.1 itojun if (wildcard < matchwild) {
859 1.1.2.1 itojun match = in6p;
860 1.1.2.1 itojun matchwild = wildcard;
861 1.1.2.1 itojun if (matchwild == 0)
862 1.1.2.1 itojun break;
863 1.1.2.1 itojun }
864 1.1.2.1 itojun }
865 1.1.2.1 itojun return(match);
866 1.1.2.1 itojun }
867 1.1.2.1 itojun
868 1.1.2.1 itojun #ifndef TCP6
869 1.1.2.1 itojun struct rtentry *
870 1.1.2.1 itojun in6_pcbrtentry(in6p)
871 1.1.2.1 itojun struct in6pcb *in6p;
872 1.1.2.1 itojun {
873 1.1.2.1 itojun struct route_in6 *ro;
874 1.1.2.1 itojun
875 1.1.2.1 itojun ro = &in6p->in6p_route;
876 1.1.2.1 itojun
877 1.1.2.1 itojun if (ro->ro_rt == NULL) {
878 1.1.2.1 itojun /*
879 1.1.2.1 itojun * No route yet, so try to acquire one.
880 1.1.2.1 itojun */
881 1.1.2.1 itojun if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
882 1.1.2.1 itojun bzero(&ro->ro_dst, sizeof(ro->ro_dst));
883 1.1.2.1 itojun ro->ro_dst.sin6_family = AF_INET6;
884 1.1.2.1 itojun ro->ro_dst.sin6_len = sizeof(struct sockaddr_in6);
885 1.1.2.1 itojun satosin6(&ro->ro_dst)->sin6_addr = in6p->in6p_faddr;
886 1.1.2.1 itojun rtalloc((struct route *)ro);
887 1.1.2.1 itojun }
888 1.1.2.1 itojun }
889 1.1.2.1 itojun return (ro->ro_rt);
890 1.1.2.1 itojun }
891 1.1.2.1 itojun
892 1.1.2.1 itojun struct in6pcb *
893 1.1.2.1 itojun in6_pcblookup_connect(head, faddr6, fport_arg, laddr6, lport_arg)
894 1.1.2.1 itojun struct in6pcb *head;
895 1.1.2.1 itojun struct in6_addr *faddr6, *laddr6;
896 1.1.2.1 itojun u_int fport_arg, lport_arg;
897 1.1.2.1 itojun {
898 1.1.2.1 itojun struct in6pcb *in6p;
899 1.1.2.1 itojun u_short fport = fport_arg, lport = lport_arg;
900 1.1.2.1 itojun
901 1.1.2.1 itojun for (in6p = head->in6p_next; in6p != head; in6p = in6p->in6p_next) {
902 1.1.2.1 itojun /* find exact match on both source and dest */
903 1.1.2.1 itojun if (in6p->in6p_fport != fport)
904 1.1.2.1 itojun continue;
905 1.1.2.1 itojun if (in6p->in6p_lport != lport)
906 1.1.2.1 itojun continue;
907 1.1.2.1 itojun if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr))
908 1.1.2.1 itojun continue;
909 1.1.2.1 itojun if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, faddr6))
910 1.1.2.1 itojun continue;
911 1.1.2.1 itojun if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr))
912 1.1.2.1 itojun continue;
913 1.1.2.1 itojun if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6))
914 1.1.2.1 itojun continue;
915 1.1.2.1 itojun return in6p;
916 1.1.2.1 itojun }
917 1.1.2.1 itojun return NULL;
918 1.1.2.1 itojun }
919 1.1.2.1 itojun
920 1.1.2.1 itojun struct in6pcb *
921 1.1.2.1 itojun in6_pcblookup_bind(head, laddr6, lport_arg)
922 1.1.2.1 itojun struct in6pcb *head;
923 1.1.2.1 itojun struct in6_addr *laddr6;
924 1.1.2.1 itojun u_int lport_arg;
925 1.1.2.1 itojun {
926 1.1.2.1 itojun struct in6pcb *in6p, *match;
927 1.1.2.1 itojun u_short lport = lport_arg;
928 1.1.2.1 itojun
929 1.1.2.1 itojun match = NULL;
930 1.1.2.1 itojun for (in6p = head->in6p_next; in6p != head; in6p = in6p->in6p_next) {
931 1.1.2.1 itojun /*
932 1.1.2.1 itojun * find destination match. exact match is preferred
933 1.1.2.1 itojun * against wildcard match.
934 1.1.2.1 itojun */
935 1.1.2.1 itojun if (in6p->in6p_fport != 0)
936 1.1.2.1 itojun continue;
937 1.1.2.1 itojun if (in6p->in6p_lport != lport)
938 1.1.2.1 itojun continue;
939 1.1.2.1 itojun if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr))
940 1.1.2.1 itojun match = in6p;
941 1.1.2.1 itojun else if (IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6))
942 1.1.2.1 itojun return in6p;
943 1.1.2.1 itojun }
944 1.1.2.1 itojun return match;
945 1.1.2.1 itojun }
946 1.1.2.1 itojun #endif
947