1 /* $OpenBSD: in6_src.c,v 1.16 2003/12/10 03:30:21 itojun Exp $ */
2 /* $KAME: in6_src.c,v 1.36 2001/02/06 04:08:17 itojun Exp $ */
3
4 /*
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 /*
34 * Copyright (c) 1982, 1986, 1991, 1993
35 * The Regents of the University of California. All rights reserved.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * SUCH DAMAGE.
60 *
61 * @(#)in_pcb.c 8.2 (Berkeley) 1/4/94
62 */
63
64 #include <sys/param.h>
65 #include <sys/systm.h>
66 #include <sys/malloc.h>
67 #include <sys/mbuf.h>
68 #include <sys/protosw.h>
69 #include <sys/socket.h>
70 #include <sys/socketvar.h>
71 #include <sys/ioctl.h>
72 #include <sys/errno.h>
73 #include <sys/time.h>
74 #include <sys/proc.h>
75
76 #include <net/if.h>
77 #include <net/route.h>
78
79 #include <netinet/in.h>
80 #include <netinet/in_var.h>
81 #include <netinet/in_systm.h>
82 #include <netinet/ip.h>
83 #include <netinet/in_pcb.h>
84 #include <netinet6/in6_var.h>
85 #include <netinet/ip6.h>
86 #include <netinet6/ip6_var.h>
87 #include <netinet6/nd6.h>
88
89 /*
90 * Return an IPv6 address, which is the most appropriate for a given
91 * destination and user specified options.
92 * If necessary, this function lookups the routing table and returns
93 * an entry to the caller for later use.
94 */
95 struct in6_addr *
in6_selectsrc(dstsock,opts,mopts,ro,laddr,errorp)96 in6_selectsrc(dstsock, opts, mopts, ro, laddr, errorp)
97 struct sockaddr_in6 *dstsock;
98 struct ip6_pktopts *opts;
99 struct ip6_moptions *mopts;
100 struct route_in6 *ro;
101 struct in6_addr *laddr;
102 int *errorp;
103 {
104 struct in6_addr *dst;
105 struct in6_ifaddr *ia6 = 0;
106 struct in6_pktinfo *pi = NULL;
107
108 dst = &dstsock->sin6_addr;
109 *errorp = 0;
110
111 /*
112 * If the source address is explicitly specified by the caller,
113 * use it.
114 */
115 if (opts && (pi = opts->ip6po_pktinfo) &&
116 !IN6_IS_ADDR_UNSPECIFIED(&pi->ipi6_addr))
117 return (&pi->ipi6_addr);
118
119 /*
120 * If the source address is not specified but the socket(if any)
121 * is already bound, use the bound address.
122 */
123 if (laddr && !IN6_IS_ADDR_UNSPECIFIED(laddr))
124 return (laddr);
125
126 /*
127 * If the caller doesn't specify the source address but
128 * the outgoing interface, use an address associated with
129 * the interface.
130 */
131 if (pi && pi->ipi6_ifindex) {
132 /* XXX boundary check is assumed to be already done. */
133 ia6 = in6_ifawithscope(ifindex2ifnet[pi->ipi6_ifindex],
134 dst);
135 if (ia6 == 0) {
136 *errorp = EADDRNOTAVAIL;
137 return (0);
138 }
139 return (&satosin6(&ia6->ia_addr)->sin6_addr);
140 }
141
142 /*
143 * If the destination address is a link-local unicast address or
144 * a multicast address, and if the outgoing interface is specified
145 * by the sin6_scope_id filed, use an address associated with the
146 * interface.
147 * XXX: We're now trying to define more specific semantics of
148 * sin6_scope_id field, so this part will be rewritten in
149 * the near future.
150 */
151 if ((IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MULTICAST(dst)) &&
152 dstsock->sin6_scope_id) {
153 /*
154 * I'm not sure if boundary check for scope_id is done
155 * somewhere...
156 */
157 if (dstsock->sin6_scope_id < 0 ||
158 if_indexlim <= dstsock->sin6_scope_id ||
159 !ifindex2ifnet[dstsock->sin6_scope_id]) {
160 *errorp = ENXIO; /* XXX: better error? */
161 return (0);
162 }
163 ia6 = in6_ifawithscope(ifindex2ifnet[dstsock->sin6_scope_id],
164 dst);
165 if (ia6 == 0) {
166 *errorp = EADDRNOTAVAIL;
167 return (0);
168 }
169 return (&satosin6(&ia6->ia_addr)->sin6_addr);
170 }
171
172 /*
173 * If the destination address is a multicast address and
174 * the outgoing interface for the address is specified
175 * by the caller, use an address associated with the interface.
176 * There is a sanity check here; if the destination has node-local
177 * scope, the outgoing interfacde should be a loopback address.
178 * Even if the outgoing interface is not specified, we also
179 * choose a loopback interface as the outgoing interface.
180 */
181 if (IN6_IS_ADDR_MULTICAST(dst)) {
182 struct ifnet *ifp = mopts ? mopts->im6o_multicast_ifp : NULL;
183
184 if (ifp == NULL && IN6_IS_ADDR_MC_NODELOCAL(dst)) {
185 ifp = lo0ifp;
186 }
187
188 if (ifp) {
189 ia6 = in6_ifawithscope(ifp, dst);
190 if (ia6 == 0) {
191 *errorp = EADDRNOTAVAIL;
192 return (0);
193 }
194 return (&satosin6(&ia6->ia_addr)->sin6_addr);
195 }
196 }
197
198 /*
199 * If the next hop address for the packet is specified
200 * by caller, use an address associated with the route
201 * to the next hop.
202 */
203 {
204 struct sockaddr_in6 *sin6_next;
205 struct rtentry *rt;
206
207 if (opts && opts->ip6po_nexthop) {
208 sin6_next = satosin6(opts->ip6po_nexthop);
209 rt = nd6_lookup(&sin6_next->sin6_addr, 1, NULL);
210 if (rt) {
211 ia6 = in6_ifawithscope(rt->rt_ifp, dst);
212 if (ia6 == 0)
213 ia6 = ifatoia6(rt->rt_ifa);
214 }
215 if (ia6 == 0) {
216 *errorp = EADDRNOTAVAIL;
217 return (0);
218 }
219 return (&satosin6(&ia6->ia_addr)->sin6_addr);
220 }
221 }
222
223 /*
224 * If route is known or can be allocated now,
225 * our src addr is taken from the i/f, else punt.
226 */
227 if (ro) {
228 if (ro->ro_rt &&
229 !IN6_ARE_ADDR_EQUAL(&satosin6(&ro->ro_dst)->sin6_addr, dst)) {
230 RTFREE(ro->ro_rt);
231 ro->ro_rt = (struct rtentry *)0;
232 }
233 if (ro->ro_rt == (struct rtentry *)0 ||
234 ro->ro_rt->rt_ifp == (struct ifnet *)0) {
235 struct sockaddr_in6 *sa6;
236
237 /* No route yet, so try to acquire one */
238 bzero(&ro->ro_dst, sizeof(struct sockaddr_in6));
239 sa6 = (struct sockaddr_in6 *)&ro->ro_dst;
240 sa6->sin6_family = AF_INET6;
241 sa6->sin6_len = sizeof(struct sockaddr_in6);
242 sa6->sin6_addr = *dst;
243 sa6->sin6_scope_id = dstsock->sin6_scope_id;
244 if (IN6_IS_ADDR_MULTICAST(dst)) {
245 ro->ro_rt = rtalloc1(&((struct route *)ro)
246 ->ro_dst, 0);
247 } else {
248 rtalloc((struct route *)ro);
249 }
250 }
251
252 /*
253 * in_pcbconnect() checks out IFF_LOOPBACK to skip using
254 * the address. But we don't know why it does so.
255 * It is necessary to ensure the scope even for lo0
256 * so doesn't check out IFF_LOOPBACK.
257 */
258
259 if (ro->ro_rt) {
260 ia6 = in6_ifawithscope(ro->ro_rt->rt_ifa->ifa_ifp, dst);
261 if (ia6 == 0) /* xxx scope error ?*/
262 ia6 = ifatoia6(ro->ro_rt->rt_ifa);
263 }
264 #if 0
265 /*
266 * xxx The followings are necessary? (kazu)
267 * I don't think so.
268 * It's for SO_DONTROUTE option in IPv4.(jinmei)
269 */
270 if (ia6 == 0) {
271 struct sockaddr_in6 sin6 = {sizeof(sin6), AF_INET6, 0};
272
273 sin6->sin6_addr = *dst;
274
275 ia6 = ifatoia6(ifa_ifwithdstaddr(sin6tosa(&sin6)));
276 if (ia6 == 0)
277 ia6 = ifatoia6(ifa_ifwithnet(sin6tosa(&sin6)));
278 if (ia6 == 0)
279 return (0);
280 return (&satosin6(&ia6->ia_addr)->sin6_addr);
281 }
282 #endif /* 0 */
283 if (ia6 == 0) {
284 *errorp = EHOSTUNREACH; /* no route */
285 return (0);
286 }
287 return (&satosin6(&ia6->ia_addr)->sin6_addr);
288 }
289
290 *errorp = EADDRNOTAVAIL;
291 return (0);
292 }
293
294 /*
295 * Default hop limit selection. The precedence is as follows:
296 * 1. Hoplimit value specified via ioctl.
297 * 2. (If the outgoing interface is detected) the current
298 * hop limit of the interface specified by router advertisement.
299 * 3. The system default hoplimit.
300 */
301 #define in6pcb inpcb
302 #define in6p_hops inp_hops
303 int
in6_selecthlim(in6p,ifp)304 in6_selecthlim(in6p, ifp)
305 struct in6pcb *in6p;
306 struct ifnet *ifp;
307 {
308 if (in6p && in6p->in6p_hops >= 0)
309 return (in6p->in6p_hops);
310 else if (ifp)
311 return (ND_IFINFO(ifp)->chlim);
312 else
313 return (ip6_defhlim);
314 }
315 #undef in6pcb
316 #undef in6p_hops
317
318 /*
319 * generate kernel-internal form (scopeid embedded into s6_addr16[1]).
320 * If the address scope of is link-local, embed the interface index in the
321 * address. The routine determines our precedence
322 * between advanced API scope/interface specification and basic API
323 * specification.
324 *
325 * this function should be nuked in the future, when we get rid of
326 * embedded scopeid thing.
327 *
328 * XXX actually, it is over-specification to return ifp against sin6_scope_id.
329 * there can be multiple interfaces that belong to a particular scope zone
330 * (in specification, we have 1:N mapping between a scope zone and interfaces).
331 * we may want to change the function to return something other than ifp.
332 */
333 int
in6_embedscope(in6,sin6,in6p,ifpp)334 in6_embedscope(in6, sin6, in6p, ifpp)
335 struct in6_addr *in6;
336 const struct sockaddr_in6 *sin6;
337 struct inpcb *in6p;
338 #define in6p_outputopts inp_outputopts6
339 #define in6p_moptions inp_moptions6
340 struct ifnet **ifpp;
341 {
342 struct ifnet *ifp = NULL;
343 u_int32_t scopeid;
344
345 *in6 = sin6->sin6_addr;
346 scopeid = sin6->sin6_scope_id;
347 if (ifpp)
348 *ifpp = NULL;
349
350 /*
351 * don't try to read sin6->sin6_addr beyond here, since the caller may
352 * ask us to overwrite existing sockaddr_in6
353 */
354
355 if (IN6_IS_SCOPE_LINKLOCAL(in6)) {
356 struct in6_pktinfo *pi;
357
358 /*
359 * KAME assumption: link id == interface id
360 */
361
362 if (in6p && in6p->in6p_outputopts &&
363 (pi = in6p->in6p_outputopts->ip6po_pktinfo) &&
364 pi->ipi6_ifindex) {
365 ifp = ifindex2ifnet[pi->ipi6_ifindex];
366 in6->s6_addr16[1] = htons(pi->ipi6_ifindex);
367 } else if (in6p && IN6_IS_ADDR_MULTICAST(in6) &&
368 in6p->in6p_moptions &&
369 in6p->in6p_moptions->im6o_multicast_ifp) {
370 ifp = in6p->in6p_moptions->im6o_multicast_ifp;
371 in6->s6_addr16[1] = htons(ifp->if_index);
372 } else if (scopeid) {
373 /* boundary check */
374 if (scopeid < 0 || if_indexlim <= scopeid ||
375 !ifindex2ifnet[scopeid])
376 return ENXIO; /* XXX EINVAL? */
377 ifp = ifindex2ifnet[scopeid];
378 /*XXX assignment to 16bit from 32bit variable */
379 in6->s6_addr16[1] = htons(scopeid & 0xffff);
380 }
381
382 if (ifpp)
383 *ifpp = ifp;
384 }
385
386 return 0;
387 }
388 #undef in6p_outputopts
389 #undef in6p_moptions
390
391 /*
392 * generate standard sockaddr_in6 from embedded form.
393 * touches sin6_addr and sin6_scope_id only.
394 *
395 * this function should be nuked in the future, when we get rid of
396 * embedded scopeid thing.
397 */
398 int
in6_recoverscope(sin6,in6,ifp)399 in6_recoverscope(sin6, in6, ifp)
400 struct sockaddr_in6 *sin6;
401 const struct in6_addr *in6;
402 struct ifnet *ifp;
403 {
404 u_int32_t scopeid;
405
406 sin6->sin6_addr = *in6;
407
408 /*
409 * don't try to read *in6 beyond here, since the caller may
410 * ask us to overwrite existing sockaddr_in6
411 */
412
413 sin6->sin6_scope_id = 0;
414 if (IN6_IS_SCOPE_LINKLOCAL(in6)) {
415 /*
416 * KAME assumption: link id == interface id
417 */
418 scopeid = ntohs(sin6->sin6_addr.s6_addr16[1]);
419 if (scopeid) {
420 /* sanity check */
421 if (scopeid < 0 || if_indexlim <= scopeid ||
422 !ifindex2ifnet[scopeid])
423 return ENXIO;
424 if (ifp && ifp->if_index != scopeid)
425 return ENXIO;
426 sin6->sin6_addr.s6_addr16[1] = 0;
427 sin6->sin6_scope_id = scopeid;
428 }
429 }
430
431 return 0;
432 }
433
434 /*
435 * just clear the embedded scope identifer.
436 */
437 void
in6_clearscope(addr)438 in6_clearscope(addr)
439 struct in6_addr *addr;
440 {
441 if (IN6_IS_SCOPE_LINKLOCAL(addr))
442 addr->s6_addr16[1] = 0;
443 }
444