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