xref: /dragonfly/sys/netinet6/in6_src.c (revision 6e285212)
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.2 2003/06/17 04:28:52 dillon 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 "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(dstsock, opts, mopts, ro, laddr, errorp)
112 	struct sockaddr_in6 *dstsock;
113 	struct ip6_pktopts *opts;
114 	struct ip6_moptions *mopts;
115 	struct route_in6 *ro;
116 	struct in6_addr *laddr;
117 	int *errorp;
118 {
119 	struct in6_addr *dst;
120 	struct in6_ifaddr *ia6 = 0;
121 	struct in6_pktinfo *pi = NULL;
122 
123 	dst = &dstsock->sin6_addr;
124 	*errorp = 0;
125 
126 	/*
127 	 * If the source address is explicitly specified by the caller,
128 	 * use it.
129 	 */
130 	if (opts && (pi = opts->ip6po_pktinfo) &&
131 	    !IN6_IS_ADDR_UNSPECIFIED(&pi->ipi6_addr))
132 		return(&pi->ipi6_addr);
133 
134 	/*
135 	 * If the source address is not specified but the socket(if any)
136 	 * is already bound, use the bound address.
137 	 */
138 	if (laddr && !IN6_IS_ADDR_UNSPECIFIED(laddr))
139 		return(laddr);
140 
141 	/*
142 	 * If the caller doesn't specify the source address but
143 	 * the outgoing interface, use an address associated with
144 	 * the interface.
145 	 */
146 	if (pi && pi->ipi6_ifindex) {
147 		/* XXX boundary check is assumed to be already done. */
148 		ia6 = in6_ifawithscope(ifindex2ifnet[pi->ipi6_ifindex],
149 				       dst);
150 		if (ia6 == 0) {
151 			*errorp = EADDRNOTAVAIL;
152 			return(0);
153 		}
154 		return(&satosin6(&ia6->ia_addr)->sin6_addr);
155 	}
156 
157 	/*
158 	 * If the destination address is a link-local unicast address or
159 	 * a multicast address, and if the outgoing interface is specified
160 	 * by the sin6_scope_id filed, use an address associated with the
161 	 * interface.
162 	 * XXX: We're now trying to define more specific semantics of
163 	 *      sin6_scope_id field, so this part will be rewritten in
164 	 *      the near future.
165 	 */
166 	if ((IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MULTICAST(dst)) &&
167 	    dstsock->sin6_scope_id) {
168 		/*
169 		 * I'm not sure if boundary check for scope_id is done
170 		 * somewhere...
171 		 */
172 		if (dstsock->sin6_scope_id < 0 ||
173 		    if_index < dstsock->sin6_scope_id) {
174 			*errorp = ENXIO; /* XXX: better error? */
175 			return(0);
176 		}
177 		ia6 = in6_ifawithscope(ifindex2ifnet[dstsock->sin6_scope_id],
178 				       dst);
179 		if (ia6 == 0) {
180 			*errorp = EADDRNOTAVAIL;
181 			return(0);
182 		}
183 		return(&satosin6(&ia6->ia_addr)->sin6_addr);
184 	}
185 
186 	/*
187 	 * If the destination address is a multicast address and
188 	 * the outgoing interface for the address is specified
189 	 * by the caller, use an address associated with the interface.
190 	 * There is a sanity check here; if the destination has node-local
191 	 * scope, the outgoing interfacde should be a loopback address.
192 	 * Even if the outgoing interface is not specified, we also
193 	 * choose a loopback interface as the outgoing interface.
194 	 */
195 	if (IN6_IS_ADDR_MULTICAST(dst)) {
196 		struct ifnet *ifp = mopts ? mopts->im6o_multicast_ifp : NULL;
197 
198 		if (ifp == NULL && IN6_IS_ADDR_MC_NODELOCAL(dst)) {
199 			ifp = &loif[0];
200 		}
201 
202 		if (ifp) {
203 			ia6 = in6_ifawithscope(ifp, dst);
204 			if (ia6 == 0) {
205 				*errorp = EADDRNOTAVAIL;
206 				return(0);
207 			}
208 			return(&satosin6(&ia6->ia_addr)->sin6_addr);
209 		}
210 	}
211 
212 	/*
213 	 * If the next hop address for the packet is specified
214 	 * by caller, use an address associated with the route
215 	 * to the next hop.
216 	 */
217 	{
218 		struct sockaddr_in6 *sin6_next;
219 		struct rtentry *rt;
220 
221 		if (opts && opts->ip6po_nexthop) {
222 			sin6_next = satosin6(opts->ip6po_nexthop);
223 			rt = nd6_lookup(&sin6_next->sin6_addr, 1, NULL);
224 			if (rt) {
225 				ia6 = in6_ifawithscope(rt->rt_ifp, dst);
226 				if (ia6 == 0)
227 					ia6 = ifatoia6(rt->rt_ifa);
228 			}
229 			if (ia6 == 0) {
230 				*errorp = EADDRNOTAVAIL;
231 				return(0);
232 			}
233 			return(&satosin6(&ia6->ia_addr)->sin6_addr);
234 		}
235 	}
236 
237 	/*
238 	 * If route is known or can be allocated now,
239 	 * our src addr is taken from the i/f, else punt.
240 	 */
241 	if (ro) {
242 		if (ro->ro_rt &&
243 		    (!(ro->ro_rt->rt_flags & RTF_UP) ||
244 		     satosin6(&ro->ro_dst)->sin6_family != AF_INET6 ||
245 		     !IN6_ARE_ADDR_EQUAL(&satosin6(&ro->ro_dst)->sin6_addr,
246 					 dst))) {
247 			RTFREE(ro->ro_rt);
248 			ro->ro_rt = (struct rtentry *)0;
249 		}
250 		if (ro->ro_rt == (struct rtentry *)0 ||
251 		    ro->ro_rt->rt_ifp == (struct ifnet *)0) {
252 			struct sockaddr_in6 *sa6;
253 
254 			/* No route yet, so try to acquire one */
255 			bzero(&ro->ro_dst, sizeof(struct sockaddr_in6));
256 			sa6 = (struct sockaddr_in6 *)&ro->ro_dst;
257 			sa6->sin6_family = AF_INET6;
258 			sa6->sin6_len = sizeof(struct sockaddr_in6);
259 			sa6->sin6_addr = *dst;
260 			sa6->sin6_scope_id = dstsock->sin6_scope_id;
261 			if (IN6_IS_ADDR_MULTICAST(dst)) {
262 				ro->ro_rt = rtalloc1(&((struct route *)ro)
263 						     ->ro_dst, 0, 0UL);
264 			} else {
265 				rtalloc((struct route *)ro);
266 			}
267 		}
268 
269 		/*
270 		 * in_pcbconnect() checks out IFF_LOOPBACK to skip using
271 		 * the address. But we don't know why it does so.
272 		 * It is necessary to ensure the scope even for lo0
273 		 * so doesn't check out IFF_LOOPBACK.
274 		 */
275 
276 		if (ro->ro_rt) {
277 			ia6 = in6_ifawithscope(ro->ro_rt->rt_ifa->ifa_ifp, dst);
278 			if (ia6 == 0) /* xxx scope error ?*/
279 				ia6 = ifatoia6(ro->ro_rt->rt_ifa);
280 		}
281 #if 0
282 		/*
283 		 * xxx The followings are necessary? (kazu)
284 		 * I don't think so.
285 		 * It's for SO_DONTROUTE option in IPv4.(jinmei)
286 		 */
287 		if (ia6 == 0) {
288 			struct sockaddr_in6 sin6 = {sizeof(sin6), AF_INET6, 0};
289 
290 			sin6->sin6_addr = *dst;
291 
292 			ia6 = ifatoia6(ifa_ifwithdstaddr(sin6tosa(&sin6)));
293 			if (ia6 == 0)
294 				ia6 = ifatoia6(ifa_ifwithnet(sin6tosa(&sin6)));
295 			if (ia6 == 0)
296 				return(0);
297 			return(&satosin6(&ia6->ia_addr)->sin6_addr);
298 		}
299 #endif /* 0 */
300 		if (ia6 == 0) {
301 			*errorp = EHOSTUNREACH;	/* no route */
302 			return(0);
303 		}
304 		return(&satosin6(&ia6->ia_addr)->sin6_addr);
305 	}
306 
307 	*errorp = EADDRNOTAVAIL;
308 	return(0);
309 }
310 
311 /*
312  * Default hop limit selection. The precedence is as follows:
313  * 1. Hoplimit value specified via ioctl.
314  * 2. (If the outgoing interface is detected) the current
315  *     hop limit of the interface specified by router advertisement.
316  * 3. The system default hoplimit.
317 */
318 int
319 in6_selecthlim(in6p, ifp)
320 	struct in6pcb *in6p;
321 	struct ifnet *ifp;
322 {
323 	if (in6p && in6p->in6p_hops >= 0)
324 		return(in6p->in6p_hops);
325 	else if (ifp)
326 		return(nd_ifinfo[ifp->if_index].chlim);
327 	else
328 		return(ip6_defhlim);
329 }
330 
331 /*
332  * XXX: this is borrowed from in6_pcbbind(). If possible, we should
333  * share this function by all *bsd*...
334  */
335 int
336 in6_pcbsetport(laddr, inp, p)
337 	struct in6_addr *laddr;
338 	struct inpcb *inp;
339 	struct proc *p;
340 {
341 	struct socket *so = inp->inp_socket;
342 	u_int16_t lport = 0, first, last, *lastport;
343 	int count, error = 0, wild = 0;
344 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
345 
346 	/* XXX: this is redundant when called from in6_pcbbind */
347 	if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
348 		wild = INPLOOKUP_WILDCARD;
349 
350 	inp->inp_flags |= INP_ANONPORT;
351 
352 	if (inp->inp_flags & INP_HIGHPORT) {
353 		first = ipport_hifirstauto;	/* sysctl */
354 		last  = ipport_hilastauto;
355 		lastport = &pcbinfo->lasthi;
356 	} else if (inp->inp_flags & INP_LOWPORT) {
357 		if (p && (error = suser(p)))
358 			return error;
359 		first = ipport_lowfirstauto;	/* 1023 */
360 		last  = ipport_lowlastauto;	/* 600 */
361 		lastport = &pcbinfo->lastlow;
362 	} else {
363 		first = ipport_firstauto;	/* sysctl */
364 		last  = ipport_lastauto;
365 		lastport = &pcbinfo->lastport;
366 	}
367 	/*
368 	 * Simple check to ensure all ports are not used up causing
369 	 * a deadlock here.
370 	 *
371 	 * We split the two cases (up and down) so that the direction
372 	 * is not being tested on each round of the loop.
373 	 */
374 	if (first > last) {
375 		/*
376 		 * counting down
377 		 */
378 		count = first - last;
379 
380 		do {
381 			if (count-- < 0) {	/* completely used? */
382 				/*
383 				 * Undo any address bind that may have
384 				 * occurred above.
385 				 */
386 				inp->in6p_laddr = in6addr_any;
387 				return (EAGAIN);
388 			}
389 			--*lastport;
390 			if (*lastport > first || *lastport < last)
391 				*lastport = first;
392 			lport = htons(*lastport);
393 		} while (in6_pcblookup_local(pcbinfo,
394 					     &inp->in6p_laddr, lport, wild));
395 	} else {
396 		/*
397 			 * counting up
398 			 */
399 		count = last - first;
400 
401 		do {
402 			if (count-- < 0) {	/* completely used? */
403 				/*
404 				 * Undo any address bind that may have
405 				 * occurred above.
406 				 */
407 				inp->in6p_laddr = in6addr_any;
408 				return (EAGAIN);
409 			}
410 			++*lastport;
411 			if (*lastport < first || *lastport > last)
412 				*lastport = first;
413 			lport = htons(*lastport);
414 		} while (in6_pcblookup_local(pcbinfo,
415 					     &inp->in6p_laddr, lport, wild));
416 	}
417 
418 	inp->inp_lport = lport;
419 	if (in_pcbinshash(inp) != 0) {
420 		inp->in6p_laddr = in6addr_any;
421 		inp->inp_lport = 0;
422 		return (EAGAIN);
423 	}
424 
425 	return(0);
426 }
427 
428 /*
429  * generate kernel-internal form (scopeid embedded into s6_addr16[1]).
430  * If the address scope of is link-local, embed the interface index in the
431  * address.  The routine determines our precedence
432  * between advanced API scope/interface specification and basic API
433  * specification.
434  *
435  * this function should be nuked in the future, when we get rid of
436  * embedded scopeid thing.
437  *
438  * XXX actually, it is over-specification to return ifp against sin6_scope_id.
439  * there can be multiple interfaces that belong to a particular scope zone
440  * (in specification, we have 1:N mapping between a scope zone and interfaces).
441  * we may want to change the function to return something other than ifp.
442  */
443 int
444 in6_embedscope(in6, sin6, in6p, ifpp)
445 	struct in6_addr *in6;
446 	const struct sockaddr_in6 *sin6;
447 #ifdef HAVE_NRL_INPCB
448 	struct inpcb *in6p;
449 #define in6p_outputopts	inp_outputopts6
450 #define in6p_moptions	inp_moptions6
451 #else
452 	struct in6pcb *in6p;
453 #endif
454 	struct ifnet **ifpp;
455 {
456 	struct ifnet *ifp = NULL;
457 	u_int32_t scopeid;
458 
459 	*in6 = sin6->sin6_addr;
460 	scopeid = sin6->sin6_scope_id;
461 	if (ifpp)
462 		*ifpp = NULL;
463 
464 	/*
465 	 * don't try to read sin6->sin6_addr beyond here, since the caller may
466 	 * ask us to overwrite existing sockaddr_in6
467 	 */
468 
469 #ifdef ENABLE_DEFAULT_SCOPE
470 	if (scopeid == 0)
471 		scopeid = scope6_addr2default(in6);
472 #endif
473 
474 	if (IN6_IS_SCOPE_LINKLOCAL(in6)) {
475 		struct in6_pktinfo *pi;
476 
477 		/*
478 		 * KAME assumption: link id == interface id
479 		 */
480 
481 		if (in6p && in6p->in6p_outputopts &&
482 		    (pi = in6p->in6p_outputopts->ip6po_pktinfo) &&
483 		    pi->ipi6_ifindex) {
484 			ifp = ifindex2ifnet[pi->ipi6_ifindex];
485 			in6->s6_addr16[1] = htons(pi->ipi6_ifindex);
486 		} else if (in6p && IN6_IS_ADDR_MULTICAST(in6) &&
487 			   in6p->in6p_moptions &&
488 			   in6p->in6p_moptions->im6o_multicast_ifp) {
489 			ifp = in6p->in6p_moptions->im6o_multicast_ifp;
490 			in6->s6_addr16[1] = htons(ifp->if_index);
491 		} else if (scopeid) {
492 			/* boundary check */
493 			if (scopeid < 0 || if_index < scopeid)
494 				return ENXIO;  /* XXX EINVAL? */
495 			ifp = ifindex2ifnet[scopeid];
496 			/*XXX assignment to 16bit from 32bit variable */
497 			in6->s6_addr16[1] = htons(scopeid & 0xffff);
498 		}
499 
500 		if (ifpp)
501 			*ifpp = ifp;
502 	}
503 
504 	return 0;
505 }
506 #ifdef HAVE_NRL_INPCB
507 #undef in6p_outputopts
508 #undef in6p_moptions
509 #endif
510 
511 /*
512  * generate standard sockaddr_in6 from embedded form.
513  * touches sin6_addr and sin6_scope_id only.
514  *
515  * this function should be nuked in the future, when we get rid of
516  * embedded scopeid thing.
517  */
518 int
519 in6_recoverscope(sin6, in6, ifp)
520 	struct sockaddr_in6 *sin6;
521 	const struct in6_addr *in6;
522 	struct ifnet *ifp;
523 {
524 	u_int32_t scopeid;
525 
526 	sin6->sin6_addr = *in6;
527 
528 	/*
529 	 * don't try to read *in6 beyond here, since the caller may
530 	 * ask us to overwrite existing sockaddr_in6
531 	 */
532 
533 	sin6->sin6_scope_id = 0;
534 	if (IN6_IS_SCOPE_LINKLOCAL(in6)) {
535 		/*
536 		 * KAME assumption: link id == interface id
537 		 */
538 		scopeid = ntohs(sin6->sin6_addr.s6_addr16[1]);
539 		if (scopeid) {
540 			/* sanity check */
541 			if (scopeid < 0 || if_index < scopeid)
542 				return ENXIO;
543 			if (ifp && ifp->if_index != scopeid)
544 				return ENXIO;
545 			sin6->sin6_addr.s6_addr16[1] = 0;
546 			sin6->sin6_scope_id = scopeid;
547 		}
548 	}
549 
550 	return 0;
551 }
552 
553 /*
554  * just clear the embedded scope identifer.
555  * XXX: currently used for bsdi4 only as a supplement function.
556  */
557 void
558 in6_clearscope(addr)
559 	struct in6_addr *addr;
560 {
561 	if (IN6_IS_SCOPE_LINKLOCAL(addr))
562 		addr->s6_addr16[1] = 0;
563 }
564