xref: /openbsd-src/sys/netinet6/in6_src.c (revision a0747c9f67a4ae71ccb71e62a28d1ea19e06a63c)
1 /*	$OpenBSD: in6_src.c,v 1.85 2021/03/10 10:21:49 jsg 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/mbuf.h>
67 #include <sys/protosw.h>
68 #include <sys/socket.h>
69 #include <sys/socketvar.h>
70 #include <sys/ioctl.h>
71 #include <sys/errno.h>
72 #include <sys/time.h>
73 
74 #include <net/if.h>
75 #include <net/if_var.h>
76 #include <net/route.h>
77 
78 #include <netinet/in.h>
79 #include <netinet/ip.h>
80 #include <netinet/in_pcb.h>
81 #include <netinet6/in6_var.h>
82 #include <netinet/ip6.h>
83 #include <netinet6/ip6_var.h>
84 #include <netinet6/nd6.h>
85 
86 int in6_selectif(struct sockaddr_in6 *, struct ip6_pktopts *,
87     struct ip6_moptions *, struct route_in6 *, struct ifnet **, u_int);
88 
89 /*
90  * Return an IPv6 address, which is the most appropriate for a given
91  * destination and pcb. We need the additional opt parameter because
92  * the values set at pcb level can be overridden via cmsg.
93  */
94 int
95 in6_pcbselsrc(struct in6_addr **in6src, struct sockaddr_in6 *dstsock,
96     struct inpcb *inp, struct ip6_pktopts *opts)
97 {
98 	struct ip6_moptions *mopts = inp->inp_moptions6;
99 	struct route_in6 *ro = &inp->inp_route6;
100 	struct in6_addr *laddr = &inp->inp_laddr6;
101 	u_int rtableid = inp->inp_rtableid;
102 	struct ifnet *ifp = NULL;
103 	struct sockaddr	*ip6_source = NULL;
104 	struct in6_addr *dst;
105 	struct in6_ifaddr *ia6 = NULL;
106 	struct in6_pktinfo *pi = NULL;
107 	int	error;
108 
109 	dst = &dstsock->sin6_addr;
110 
111 	/*
112 	 * If the source address is explicitly specified by the caller,
113 	 * check if the requested source address is indeed a unicast address
114 	 * assigned to the node, and can be used as the packet's source
115 	 * address.  If everything is okay, use the address as source.
116 	 */
117 	if (opts && (pi = opts->ip6po_pktinfo) &&
118 	    !IN6_IS_ADDR_UNSPECIFIED(&pi->ipi6_addr)) {
119 		struct sockaddr_in6 sa6;
120 
121 		/* get the outgoing interface */
122 		error = in6_selectif(dstsock, opts, mopts, ro, &ifp, rtableid);
123 		if (error)
124 			return (error);
125 
126 		bzero(&sa6, sizeof(sa6));
127 		sa6.sin6_family = AF_INET6;
128 		sa6.sin6_len = sizeof(sa6);
129 		sa6.sin6_addr = pi->ipi6_addr;
130 
131 		if (ifp && IN6_IS_SCOPE_EMBED(&sa6.sin6_addr))
132 			sa6.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
133 		if_put(ifp); /* put reference from in6_selectif */
134 
135 		ia6 = ifatoia6(ifa_ifwithaddr(sin6tosa(&sa6), rtableid));
136 		if (ia6 == NULL || (ia6->ia6_flags &
137 		     (IN6_IFF_ANYCAST|IN6_IFF_TENTATIVE|IN6_IFF_DUPLICATED)))
138 			return (EADDRNOTAVAIL);
139 
140 		pi->ipi6_addr = sa6.sin6_addr; /* XXX: this overrides pi */
141 
142 		*in6src = &pi->ipi6_addr;
143 		return (0);
144 	}
145 
146 	/*
147 	 * If the source address is not specified but the socket(if any)
148 	 * is already bound, use the bound address.
149 	 */
150 	if (laddr && !IN6_IS_ADDR_UNSPECIFIED(laddr)) {
151 		*in6src = laddr;
152 		return (0);
153 	}
154 
155 	/*
156 	 * If the caller doesn't specify the source address but
157 	 * the outgoing interface, use an address associated with
158 	 * the interface.
159 	 */
160 	if (pi && pi->ipi6_ifindex) {
161 		ifp = if_get(pi->ipi6_ifindex);
162 		if (ifp == NULL)
163 			return (ENXIO); /* XXX: better error? */
164 
165 		ia6 = in6_ifawithscope(ifp, dst, rtableid);
166 		if_put(ifp);
167 
168 		if (ia6 == NULL)
169 			return (EADDRNOTAVAIL);
170 
171 		*in6src = &ia6->ia_addr.sin6_addr;
172 		return (0);
173 	}
174 
175 	error = in6_selectsrc(in6src, dstsock, mopts, rtableid);
176 	if (error != EADDRNOTAVAIL)
177 		return (error);
178 
179 	/*
180 	 * If route is known or can be allocated now,
181 	 * our src addr is taken from the i/f, else punt.
182 	 */
183 	if (!rtisvalid(ro->ro_rt) || (ro->ro_tableid != rtableid) ||
184 	    !IN6_ARE_ADDR_EQUAL(&ro->ro_dst.sin6_addr, dst)) {
185 		rtfree(ro->ro_rt);
186 		ro->ro_rt = NULL;
187 	}
188 	if (ro->ro_rt == NULL) {
189 		struct sockaddr_in6 *sa6;
190 
191 		/* No route yet, so try to acquire one */
192 		bzero(&ro->ro_dst, sizeof(struct sockaddr_in6));
193 		ro->ro_tableid = rtableid;
194 		sa6 = &ro->ro_dst;
195 		sa6->sin6_family = AF_INET6;
196 		sa6->sin6_len = sizeof(struct sockaddr_in6);
197 		sa6->sin6_addr = *dst;
198 		sa6->sin6_scope_id = dstsock->sin6_scope_id;
199 		ro->ro_rt = rtalloc(sin6tosa(&ro->ro_dst),
200 		    RT_RESOLVE, ro->ro_tableid);
201 	}
202 
203 	/*
204 	 * in_pcbconnect() checks out IFF_LOOPBACK to skip using
205 	 * the address. But we don't know why it does so.
206 	 * It is necessary to ensure the scope even for lo0
207 	 * so doesn't check out IFF_LOOPBACK.
208 	 */
209 
210 	if (ro->ro_rt) {
211 		ifp = if_get(ro->ro_rt->rt_ifidx);
212 		if (ifp != NULL) {
213 			ia6 = in6_ifawithscope(ifp, dst, rtableid);
214 			if_put(ifp);
215 		}
216 		if (ia6 == NULL) /* xxx scope error ?*/
217 			ia6 = ifatoia6(ro->ro_rt->rt_ifa);
218 	}
219 
220 	/*
221 	 * Use preferred source address if :
222 	 * - destination is not onlink
223 	 * - preferred source address is set
224 	 * - output interface is UP
225 	 */
226 	if (ro->ro_rt && !(ro->ro_rt->rt_flags & RTF_LLINFO) &&
227 	    !(ro->ro_rt->rt_flags & RTF_HOST)) {
228 		ip6_source = rtable_getsource(rtableid, AF_INET6);
229 		if (ip6_source != NULL) {
230 			struct ifaddr *ifa;
231 			if ((ifa = ifa_ifwithaddr(ip6_source, rtableid)) !=
232 			    NULL && ISSET(ifa->ifa_ifp->if_flags, IFF_UP)) {
233 				*in6src = &satosin6(ip6_source)->sin6_addr;
234 				return (0);
235 			}
236 		}
237 	}
238 
239 	if (ia6 == NULL)
240 		return (EHOSTUNREACH);	/* no route */
241 
242 	*in6src = &ia6->ia_addr.sin6_addr;
243 	return (0);
244 }
245 
246 /*
247  * Return an IPv6 address, which is the most appropriate for a given
248  * destination and multicast options.
249  * If necessary, this function lookups the routing table and returns
250  * an entry to the caller for later use.
251  */
252 int
253 in6_selectsrc(struct in6_addr **in6src, struct sockaddr_in6 *dstsock,
254     struct ip6_moptions *mopts, unsigned int rtableid)
255 {
256 	struct ifnet *ifp = NULL;
257 	struct in6_addr *dst;
258 	struct in6_ifaddr *ia6 = NULL;
259 
260 	dst = &dstsock->sin6_addr;
261 
262 	/*
263 	 * If the destination address is a link-local unicast address or
264 	 * a link/interface-local multicast address, and if the outgoing
265 	 * interface is specified by the sin6_scope_id filed, use an address
266 	 * associated with the interface.
267 	 * XXX: We're now trying to define more specific semantics of
268 	 *      sin6_scope_id field, so this part will be rewritten in
269 	 *      the near future.
270 	 */
271 	if ((IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MC_LINKLOCAL(dst) ||
272 	     IN6_IS_ADDR_MC_INTFACELOCAL(dst)) && dstsock->sin6_scope_id) {
273 		ifp = if_get(dstsock->sin6_scope_id);
274 		if (ifp == NULL)
275 			return (ENXIO); /* XXX: better error? */
276 
277 		ia6 = in6_ifawithscope(ifp, dst, rtableid);
278 		if_put(ifp);
279 
280 		if (ia6 == NULL)
281 			return (EADDRNOTAVAIL);
282 
283 		*in6src = &ia6->ia_addr.sin6_addr;
284 		return (0);
285 	}
286 
287 	/*
288 	 * If the destination address is a multicast address and
289 	 * the outgoing interface for the address is specified
290 	 * by the caller, use an address associated with the interface.
291 	 * Even if the outgoing interface is not specified, we also
292 	 * choose a loopback interface as the outgoing interface.
293 	 */
294 	if (IN6_IS_ADDR_MULTICAST(dst)) {
295 		ifp = mopts ? if_get(mopts->im6o_ifidx) : NULL;
296 
297 		if (!ifp && dstsock->sin6_scope_id)
298 			ifp = if_get(htons(dstsock->sin6_scope_id));
299 
300 		if (ifp) {
301 			ia6 = in6_ifawithscope(ifp, dst, rtableid);
302 			if_put(ifp);
303 
304 			if (ia6 == NULL)
305 				return (EADDRNOTAVAIL);
306 
307 			*in6src = &ia6->ia_addr.sin6_addr;
308 			return (0);
309 		}
310 	}
311 
312 	return (EADDRNOTAVAIL);
313 }
314 
315 struct rtentry *
316 in6_selectroute(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts,
317     struct route_in6 *ro, unsigned int rtableid)
318 {
319 	struct in6_addr *dst;
320 
321 	dst = &dstsock->sin6_addr;
322 
323 	/*
324 	 * Use a cached route if it exists and is valid, else try to allocate
325 	 * a new one.
326 	 */
327 	if (ro) {
328 		if (rtisvalid(ro->ro_rt))
329 			KASSERT(sin6tosa(&ro->ro_dst)->sa_family == AF_INET6);
330 		if (!rtisvalid(ro->ro_rt) ||
331 		    !IN6_ARE_ADDR_EQUAL(&ro->ro_dst.sin6_addr, dst)) {
332 			rtfree(ro->ro_rt);
333 			ro->ro_rt = NULL;
334 		}
335 		if (ro->ro_rt == NULL) {
336 			struct sockaddr_in6 *sa6;
337 
338 			/* No route yet, so try to acquire one */
339 			bzero(&ro->ro_dst, sizeof(struct sockaddr_in6));
340 			ro->ro_tableid = rtableid;
341 			sa6 = &ro->ro_dst;
342 			*sa6 = *dstsock;
343 			sa6->sin6_scope_id = 0;
344 			ro->ro_tableid = rtableid;
345 			ro->ro_rt = rtalloc_mpath(sin6tosa(&ro->ro_dst),
346 			    NULL, ro->ro_tableid);
347 		}
348 
349 		/*
350 		 * Check if the outgoing interface conflicts with
351 		 * the interface specified by ipi6_ifindex (if specified).
352 		 * Note that loopback interface is always okay.
353 		 * (this may happen when we are sending a packet to one of
354 		 *  our own addresses.)
355 		 */
356 		if (opts && opts->ip6po_pktinfo &&
357 		    opts->ip6po_pktinfo->ipi6_ifindex) {
358 			if (ro->ro_rt != NULL &&
359 			    !ISSET(ro->ro_rt->rt_flags, RTF_LOCAL) &&
360 			    ro->ro_rt->rt_ifidx !=
361 			    opts->ip6po_pktinfo->ipi6_ifindex) {
362 			    	return (NULL);
363 			}
364 		}
365 
366 		return (ro->ro_rt);
367 	}
368 
369 	return (NULL);
370 }
371 
372 int
373 in6_selectif(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts,
374     struct ip6_moptions *mopts, struct route_in6 *ro, struct ifnet **retifp,
375     u_int rtableid)
376 {
377 	struct rtentry *rt = NULL;
378 	struct in6_pktinfo *pi = NULL;
379 
380 	/* If the caller specify the outgoing interface explicitly, use it. */
381 	if (opts && (pi = opts->ip6po_pktinfo) != NULL && pi->ipi6_ifindex) {
382 		*retifp = if_get(pi->ipi6_ifindex);
383 		if (*retifp != NULL)
384 			return (0);
385 	}
386 
387 	/*
388 	 * If the destination address is a multicast address and the outgoing
389 	 * interface for the address is specified by the caller, use it.
390 	 */
391 	if (IN6_IS_ADDR_MULTICAST(&dstsock->sin6_addr) &&
392 	    mopts != NULL && (*retifp = if_get(mopts->im6o_ifidx)) != NULL)
393 	    	return (0);
394 
395 	rt = in6_selectroute(dstsock, opts, ro, rtableid);
396 	if (rt == NULL)
397 		return (EHOSTUNREACH);
398 
399 	/*
400 	 * do not use a rejected or black hole route.
401 	 * XXX: this check should be done in the L2 output routine.
402 	 * However, if we skipped this check here, we'd see the following
403 	 * scenario:
404 	 * - install a rejected route for a scoped address prefix
405 	 *   (like fe80::/10)
406 	 * - send a packet to a destination that matches the scoped prefix,
407 	 *   with ambiguity about the scope zone.
408 	 * - pick the outgoing interface from the route, and disambiguate the
409 	 *   scope zone with the interface.
410 	 * - ip6_output() would try to get another route with the "new"
411 	 *   destination, which may be valid.
412 	 * - we'd see no error on output.
413 	 * Although this may not be very harmful, it should still be confusing.
414 	 * We thus reject the case here.
415 	 */
416 	if (rt && (rt->rt_flags & (RTF_REJECT | RTF_BLACKHOLE)))
417 		return (rt->rt_flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
418 
419 	if (rt != NULL)
420 		*retifp = if_get(rt->rt_ifidx);
421 
422 	return (0);
423 }
424 
425 int
426 in6_selecthlim(struct inpcb *in6p)
427 {
428 	if (in6p && in6p->inp_hops >= 0)
429 		return (in6p->inp_hops);
430 
431 	return (ip6_defhlim);
432 }
433 
434 /*
435  * generate kernel-internal form (scopeid embedded into s6_addr16[1]).
436  * If the address scope of is link-local, embed the interface index in the
437  * address.  The routine determines our precedence
438  * between advanced API scope/interface specification and basic API
439  * specification.
440  *
441  * this function should be nuked in the future, when we get rid of
442  * embedded scopeid thing.
443  *
444  * XXX actually, it is over-specification to return ifp against sin6_scope_id.
445  * there can be multiple interfaces that belong to a particular scope zone
446  * (in specification, we have 1:N mapping between a scope zone and interfaces).
447  * we may want to change the function to return something other than ifp.
448  */
449 int
450 in6_embedscope(struct in6_addr *in6, const struct sockaddr_in6 *sin6,
451     struct inpcb *in6p)
452 {
453 	struct ifnet *ifp = NULL;
454 	u_int32_t scopeid;
455 
456 	*in6 = sin6->sin6_addr;
457 	scopeid = sin6->sin6_scope_id;
458 
459 	/*
460 	 * don't try to read sin6->sin6_addr beyond here, since the caller may
461 	 * ask us to overwrite existing sockaddr_in6
462 	 */
463 
464 	if (IN6_IS_SCOPE_EMBED(in6)) {
465 		struct in6_pktinfo *pi;
466 
467 		/*
468 		 * KAME assumption: link id == interface id
469 		 */
470 
471 		if (in6p && in6p->inp_outputopts6 &&
472 		    (pi = in6p->inp_outputopts6->ip6po_pktinfo) &&
473 		    pi->ipi6_ifindex) {
474 			ifp = if_get(pi->ipi6_ifindex);
475 			if (ifp == NULL)
476 				return ENXIO;  /* XXX EINVAL? */
477 			in6->s6_addr16[1] = htons(pi->ipi6_ifindex);
478 		} else if (in6p && IN6_IS_ADDR_MULTICAST(in6) &&
479 		    in6p->inp_moptions6 &&
480 		    (ifp = if_get(in6p->inp_moptions6->im6o_ifidx))) {
481 			in6->s6_addr16[1] = htons(ifp->if_index);
482 		} else if (scopeid) {
483 			ifp = if_get(scopeid);
484 			if (ifp == NULL)
485 				return ENXIO;  /* XXX EINVAL? */
486 			/*XXX assignment to 16bit from 32bit variable */
487 			in6->s6_addr16[1] = htons(scopeid & 0xffff);
488 		}
489 		if_put(ifp);
490 	}
491 
492 	return 0;
493 }
494 
495 /*
496  * generate standard sockaddr_in6 from embedded form.
497  * touches sin6_addr and sin6_scope_id only.
498  *
499  * this function should be nuked in the future, when we get rid of
500  * embedded scopeid thing.
501  */
502 void
503 in6_recoverscope(struct sockaddr_in6 *sin6, const struct in6_addr *in6)
504 {
505 	u_int32_t scopeid;
506 
507 	sin6->sin6_addr = *in6;
508 
509 	/*
510 	 * don't try to read *in6 beyond here, since the caller may
511 	 * ask us to overwrite existing sockaddr_in6
512 	 */
513 
514 	sin6->sin6_scope_id = 0;
515 	if (IN6_IS_SCOPE_EMBED(in6)) {
516 		/*
517 		 * KAME assumption: link id == interface id
518 		 */
519 		scopeid = ntohs(sin6->sin6_addr.s6_addr16[1]);
520 		if (scopeid) {
521 			sin6->sin6_addr.s6_addr16[1] = 0;
522 			sin6->sin6_scope_id = scopeid;
523 		}
524 	}
525 }
526 
527 /*
528  * just clear the embedded scope identifier.
529  */
530 void
531 in6_clearscope(struct in6_addr *addr)
532 {
533 	if (IN6_IS_SCOPE_EMBED(addr))
534 		addr->s6_addr16[1] = 0;
535 }
536