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