inet6: emit RTM_NEWADDR messages on address flag changes.
[dragonfly.git] / sys / netinet6 / in6_src.c
CommitLineData
984263bc
MD
1/* $FreeBSD: src/sys/netinet6/in6_src.c,v 1.1.2.3 2002/02/26 18:02:06 ume Exp $ */
2/* $KAME: in6_src.c,v 1.37 2001/03/29 05:34:31 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.
dc71b7ab 45 * 3. Neither the name of the University nor the names of its contributors
984263bc
MD
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 "opt_inet.h"
65#include "opt_inet6.h"
66
67#include <sys/param.h>
68#include <sys/systm.h>
3e4150ef 69#include <sys/jail.h>
0ac8345c 70#include <sys/kernel.h>
984263bc
MD
71#include <sys/malloc.h>
72#include <sys/mbuf.h>
73#include <sys/protosw.h>
74#include <sys/socket.h>
75#include <sys/socketvar.h>
02809d29
PA
76#include <sys/sockio.h>
77#include <sys/sysctl.h>
984263bc
MD
78#include <sys/errno.h>
79#include <sys/time.h>
80#include <sys/proc.h>
895c1f85 81#include <sys/priv.h>
984263bc
MD
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
bde3511a 97#include <netinet6/scope6_var.h>
984263bc
MD
98#endif
99
100#include <net/net_osdep.h>
101
02809d29
PA
102#define ADDR_LABEL_NOTAPP (-1)
103struct in6_addrpolicy defaultaddrpolicy;
104
105static void init_policy_queue(void);
106static int add_addrsel_policyent(struct in6_addrpolicy *);
107static int delete_addrsel_policyent(struct in6_addrpolicy *);
108static int walk_addrsel_policy(int (*)(struct in6_addrpolicy *, void *),
109 void *);
110static int dump_addrsel_policyent(struct in6_addrpolicy *, void *);
111
112
984263bc
MD
113/*
114 * Return an IPv6 address, which is the most appropriate for a given
115 * destination and user specified options.
116 * If necessary, this function lookups the routing table and returns
117 * an entry to the caller for later use.
118 */
119struct in6_addr *
122ebd49
CP
120in6_selectsrc(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts,
121 struct ip6_moptions *mopts, struct route_in6 *ro,
3e4150ef 122 struct in6_addr *laddr, int *errorp, struct thread *td)
984263bc 123{
3e4150ef
VBD
124 struct sockaddr_in6 jsin6;
125 struct ucred *cred = NULL;
984263bc 126 struct in6_addr *dst;
4090d6ff 127 struct in6_ifaddr *ia6 = NULL;
984263bc 128 struct in6_pktinfo *pi = NULL;
3e4150ef 129 int jailed = 0;
984263bc 130
3e4150ef
VBD
131 if (td && td->td_proc && td->td_proc->p_ucred)
132 cred = td->td_proc->p_ucred;
133 if (cred && cred->cr_prison)
134 jailed = 1;
135 jsin6.sin6_family = AF_INET6;
984263bc
MD
136 dst = &dstsock->sin6_addr;
137 *errorp = 0;
138
139 /*
140 * If the source address is explicitly specified by the caller,
141 * use it.
142 */
143 if (opts && (pi = opts->ip6po_pktinfo) &&
3e4150ef
VBD
144 !IN6_IS_ADDR_UNSPECIFIED(&pi->ipi6_addr)) {
145 jsin6.sin6_addr = pi->ipi6_addr;
146 if (jailed && !jailed_ip(cred->cr_prison,
147 (struct sockaddr *)&jsin6)) {
148 return(0);
149 } else {
150 return (&pi->ipi6_addr);
151 }
152 }
984263bc
MD
153
154 /*
155 * If the source address is not specified but the socket(if any)
156 * is already bound, use the bound address.
157 */
3e4150ef
VBD
158 if (laddr && !IN6_IS_ADDR_UNSPECIFIED(laddr)) {
159 jsin6.sin6_addr = *laddr;
160 if (jailed && !jailed_ip(cred->cr_prison,
161 (struct sockaddr *)&jsin6)) {
162 return(0);
163 } else {
164 return (laddr);
165 }
166 }
984263bc
MD
167
168 /*
169 * If the caller doesn't specify the source address but
170 * the outgoing interface, use an address associated with
171 * the interface.
172 */
173 if (pi && pi->ipi6_ifindex) {
174 /* XXX boundary check is assumed to be already done. */
175 ia6 = in6_ifawithscope(ifindex2ifnet[pi->ipi6_ifindex],
cfd79120 176 dst, cred);
3e4150ef
VBD
177
178 if (ia6 && jailed) {
179 jsin6.sin6_addr = (&ia6->ia_addr)->sin6_addr;
180 if (!jailed_ip(cred->cr_prison,
181 (struct sockaddr *)&jsin6))
4090d6ff 182 ia6 = NULL;
3e4150ef
VBD
183 }
184
4090d6ff 185 if (ia6 == NULL) {
984263bc 186 *errorp = EADDRNOTAVAIL;
bde3511a 187 return (0);
984263bc 188 }
bde3511a 189 return (&satosin6(&ia6->ia_addr)->sin6_addr);
984263bc
MD
190 }
191
192 /*
193 * If the destination address is a link-local unicast address or
194 * a multicast address, and if the outgoing interface is specified
195 * by the sin6_scope_id filed, use an address associated with the
196 * interface.
197 * XXX: We're now trying to define more specific semantics of
198 * sin6_scope_id field, so this part will be rewritten in
199 * the near future.
200 */
201 if ((IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MULTICAST(dst)) &&
202 dstsock->sin6_scope_id) {
203 /*
204 * I'm not sure if boundary check for scope_id is done
205 * somewhere...
206 */
207 if (dstsock->sin6_scope_id < 0 ||
208 if_index < dstsock->sin6_scope_id) {
209 *errorp = ENXIO; /* XXX: better error? */
bde3511a 210 return (0);
984263bc
MD
211 }
212 ia6 = in6_ifawithscope(ifindex2ifnet[dstsock->sin6_scope_id],
cfd79120 213 dst, cred);
3e4150ef
VBD
214
215 if (ia6 && jailed) {
216 jsin6.sin6_addr = (&ia6->ia_addr)->sin6_addr;
217 if (!jailed_ip(cred->cr_prison,
218 (struct sockaddr *)&jsin6))
4090d6ff 219 ia6 = NULL;
3e4150ef
VBD
220 }
221
4090d6ff 222 if (ia6 == NULL) {
984263bc 223 *errorp = EADDRNOTAVAIL;
bde3511a 224 return (0);
984263bc 225 }
bde3511a 226 return (&satosin6(&ia6->ia_addr)->sin6_addr);
984263bc
MD
227 }
228
229 /*
230 * If the destination address is a multicast address and
231 * the outgoing interface for the address is specified
232 * by the caller, use an address associated with the interface.
233 * There is a sanity check here; if the destination has node-local
234 * scope, the outgoing interfacde should be a loopback address.
235 * Even if the outgoing interface is not specified, we also
236 * choose a loopback interface as the outgoing interface.
237 */
3e4150ef 238 if (!jailed && IN6_IS_ADDR_MULTICAST(dst)) {
984263bc
MD
239 struct ifnet *ifp = mopts ? mopts->im6o_multicast_ifp : NULL;
240
cb3deea5 241 if (ifp == NULL && IN6_IS_ADDR_MC_INTFACELOCAL(dst)) {
860b6b42 242 ifp = loif;
984263bc
MD
243 }
244
245 if (ifp) {
cfd79120 246 ia6 = in6_ifawithscope(ifp, dst, cred);
4090d6ff 247 if (ia6 == NULL) {
984263bc 248 *errorp = EADDRNOTAVAIL;
bde3511a 249 return (0);
984263bc 250 }
bde3511a 251 return (&satosin6(&ia6->ia_addr)->sin6_addr);
984263bc
MD
252 }
253 }
254
255 /*
256 * If the next hop address for the packet is specified
257 * by caller, use an address associated with the route
258 * to the next hop.
259 */
260 {
261 struct sockaddr_in6 *sin6_next;
262 struct rtentry *rt;
263
264 if (opts && opts->ip6po_nexthop) {
265 sin6_next = satosin6(opts->ip6po_nexthop);
266 rt = nd6_lookup(&sin6_next->sin6_addr, 1, NULL);
267 if (rt) {
cfd79120 268 ia6 = in6_ifawithscope(rt->rt_ifp, dst, cred);
4090d6ff 269 if (ia6 == NULL)
984263bc
MD
270 ia6 = ifatoia6(rt->rt_ifa);
271 }
3e4150ef
VBD
272 if (ia6 && jailed) {
273 jsin6.sin6_addr = (&ia6->ia_addr)->sin6_addr;
274 if (!jailed_ip(cred->cr_prison,
275 (struct sockaddr *)&jsin6))
4090d6ff 276 ia6 = NULL;
3e4150ef
VBD
277 }
278
4090d6ff 279 if (ia6 == NULL) {
984263bc 280 *errorp = EADDRNOTAVAIL;
bde3511a 281 return (0);
984263bc 282 }
bde3511a 283 return (&satosin6(&ia6->ia_addr)->sin6_addr);
984263bc
MD
284 }
285 }
286
287 /*
288 * If route is known or can be allocated now,
289 * our src addr is taken from the i/f, else punt.
290 */
291 if (ro) {
292 if (ro->ro_rt &&
293 (!(ro->ro_rt->rt_flags & RTF_UP) ||
bde3511a 294 satosin6(&ro->ro_dst)->sin6_family != AF_INET6 ||
984263bc
MD
295 !IN6_ARE_ADDR_EQUAL(&satosin6(&ro->ro_dst)->sin6_addr,
296 dst))) {
297 RTFREE(ro->ro_rt);
f23061d4 298 ro->ro_rt = NULL;
984263bc 299 }
f23061d4 300 if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp == NULL) {
984263bc
MD
301 struct sockaddr_in6 *sa6;
302
303 /* No route yet, so try to acquire one */
304 bzero(&ro->ro_dst, sizeof(struct sockaddr_in6));
f23061d4 305 sa6 = &ro->ro_dst;
984263bc
MD
306 sa6->sin6_family = AF_INET6;
307 sa6->sin6_len = sizeof(struct sockaddr_in6);
308 sa6->sin6_addr = *dst;
309 sa6->sin6_scope_id = dstsock->sin6_scope_id;
3e4150ef 310 if (!jailed && IN6_IS_ADDR_MULTICAST(dst)) {
f3ed2586
JH
311 ro->ro_rt =
312 rtpurelookup((struct sockaddr *)&ro->ro_dst);
984263bc
MD
313 } else {
314 rtalloc((struct route *)ro);
315 }
316 }
317
318 /*
319 * in_pcbconnect() checks out IFF_LOOPBACK to skip using
320 * the address. But we don't know why it does so.
321 * It is necessary to ensure the scope even for lo0
322 * so doesn't check out IFF_LOOPBACK.
323 */
324
325 if (ro->ro_rt) {
cfd79120 326 ia6 = in6_ifawithscope(ro->ro_rt->rt_ifa->ifa_ifp, dst, cred);
3e4150ef
VBD
327 if (ia6 && jailed) {
328 jsin6.sin6_addr = (&ia6->ia_addr)->sin6_addr;
329 if (!jailed_ip(cred->cr_prison,
330 (struct sockaddr *)&jsin6))
4090d6ff 331 ia6 = NULL;
3e4150ef
VBD
332 }
333
4090d6ff 334 if (ia6 == NULL) /* xxx scope error ?*/
984263bc 335 ia6 = ifatoia6(ro->ro_rt->rt_ifa);
3e4150ef
VBD
336
337 if (ia6 && jailed) {
338 jsin6.sin6_addr = (&ia6->ia_addr)->sin6_addr;
339 if (!jailed_ip(cred->cr_prison,
340 (struct sockaddr *)&jsin6))
4090d6ff 341 ia6 = NULL;
3e4150ef 342 }
984263bc 343 }
4090d6ff 344 if (ia6 == NULL) {
984263bc 345 *errorp = EHOSTUNREACH; /* no route */
bde3511a 346 return (0);
984263bc 347 }
bde3511a 348 return (&satosin6(&ia6->ia_addr)->sin6_addr);
984263bc
MD
349 }
350
351 *errorp = EADDRNOTAVAIL;
bde3511a 352 return (0);
984263bc
MD
353}
354
355/*
356 * Default hop limit selection. The precedence is as follows:
357 * 1. Hoplimit value specified via ioctl.
358 * 2. (If the outgoing interface is detected) the current
359 * hop limit of the interface specified by router advertisement.
360 * 3. The system default hoplimit.
361*/
362int
122ebd49 363in6_selecthlim(struct in6pcb *in6p, struct ifnet *ifp)
984263bc 364{
74ceb998
MD
365 int hlim;
366
367 if (in6p && in6p->in6p_hops >= 0) {
bde3511a 368 return (in6p->in6p_hops);
74ceb998
MD
369 } else if (ifp) {
370 hlim = ND_IFINFO(ifp)->chlim;
371 if (hlim < ip6_minhlim)
372 hlim = ip6_minhlim;
373 } else {
374 hlim = ip6_defhlim;
375 }
376 return (hlim);
984263bc
MD
377}
378
327bf46d
SZ
379static boolean_t
380in6_pcbporthash_update(struct inpcbportinfo *portinfo,
381 struct inpcb *inp, u_short lport, struct ucred *cred, int wild)
382{
0723a285
SZ
383 struct inpcbporthead *porthash;
384
327bf46d
SZ
385 /*
386 * This has to be atomic. If the porthash is shared across multiple
387 * protocol threads, e.g. tcp and udp, then the token must be held.
388 */
0723a285
SZ
389 porthash = in_pcbporthash_head(portinfo, lport);
390 GET_PORTHASH_TOKEN(porthash);
327bf46d 391
0723a285 392 if (in6_pcblookup_local(porthash, &inp->in6p_laddr, lport,
327bf46d 393 wild, cred) != NULL) {
0723a285 394 REL_PORTHASH_TOKEN(porthash);
327bf46d
SZ
395 return FALSE;
396 }
397 inp->inp_lport = lport;
0723a285 398 in_pcbinsporthash(porthash, inp);
327bf46d 399
0723a285 400 REL_PORTHASH_TOKEN(porthash);
327bf46d
SZ
401 return TRUE;
402}
403
984263bc
MD
404/*
405 * XXX: this is borrowed from in6_pcbbind(). If possible, we should
406 * share this function by all *bsd*...
407 */
408int
8fa831cf 409in6_pcbsetlport(struct in6_addr *laddr, struct inpcb *inp, struct thread *td)
984263bc
MD
410{
411 struct socket *so = inp->inp_socket;
3b9ff503 412 uint16_t lport, first, last, step, first0, last0;
984263bc 413 int count, error = 0, wild = 0;
2524c225
SZ
414 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
415 struct inpcbportinfo *portinfo;
3e4150ef 416 struct ucred *cred = NULL;
2524c225 417 int portinfo_first, portinfo_idx;
3b9ff503 418 uint32_t cut;
984263bc
MD
419
420 /* XXX: this is redundant when called from in6_pcbbind */
421 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
422 wild = INPLOOKUP_WILDCARD;
3e4150ef
VBD
423 if (td->td_proc && td->td_proc->p_ucred)
424 cred = td->td_proc->p_ucred;
984263bc
MD
425
426 inp->inp_flags |= INP_ANONPORT;
427
16db446e
SZ
428 step = pcbinfo->portinfo_cnt;
429 portinfo_first = mycpuid % pcbinfo->portinfo_cnt;
2524c225 430 portinfo_idx = portinfo_first;
2524c225 431
984263bc 432 if (inp->inp_flags & INP_HIGHPORT) {
3b9ff503
SZ
433 first0 = ipport_hifirstauto; /* sysctl */
434 last0 = ipport_hilastauto;
984263bc 435 } else if (inp->inp_flags & INP_LOWPORT) {
895c1f85 436 if ((error = priv_check(td, PRIV_ROOT)) != 0)
984263bc 437 return error;
3b9ff503
SZ
438 first0 = ipport_lowfirstauto; /* 1023 */
439 last0 = ipport_lowlastauto; /* 600 */
984263bc 440 } else {
3b9ff503
SZ
441 first0 = ipport_firstauto; /* sysctl */
442 last0 = ipport_lastauto;
443 }
444 if (first0 > last0) {
445 lport = last0;
446 last0 = first0;
447 first0 = lport;
984263bc 448 }
3b9ff503
SZ
449 KKASSERT(last0 >= first0);
450
451 cut = karc4random();
452loop:
453 portinfo = &pcbinfo->portinfo[portinfo_idx];
454 first = first0;
455 last = last0;
8d976fd7 456
984263bc
MD
457 /*
458 * Simple check to ensure all ports are not used up causing
459 * a deadlock here.
984263bc 460 */
3b9ff503
SZ
461 in_pcbportrange(&last, &first, portinfo->offset, step);
462 lport = last - first;
463 count = lport / step;
984263bc 464
3b9ff503
SZ
465 lport = rounddown(cut % lport, step) + first;
466 KKASSERT(lport % step == portinfo->offset);
467
468 for (;;) {
469 if (count-- < 0) { /* completely used? */
470 error = EAGAIN;
471 break;
327bf46d 472 }
984263bc 473
3b9ff503
SZ
474 if (__predict_false(lport < first || lport > last)) {
475 lport = first;
476 KKASSERT(lport % step == portinfo->offset);
477 }
478
479 if (in6_pcbporthash_update(portinfo, inp, htons(lport),
480 cred, wild)) {
481 error = 0;
482 break;
327bf46d 483 }
3b9ff503
SZ
484
485 lport += step;
486 KKASSERT(lport % step == portinfo->offset);
327bf46d 487 }
2524c225
SZ
488
489 if (error) {
490 /* Try next portinfo */
491 portinfo_idx++;
16db446e 492 portinfo_idx %= pcbinfo->portinfo_cnt;
2524c225
SZ
493 if (portinfo_idx != portinfo_first)
494 goto loop;
495
496 /* Undo any address bind that may have occurred above. */
497 inp->in6p_laddr = kin6addr_any;
498 }
8d976fd7 499 return error;
984263bc
MD
500}
501
502/*
1af953ac 503 * Generate kernel-internal form (scopeid embedded into s6_addr16[1]).
984263bc
MD
504 * If the address scope of is link-local, embed the interface index in the
505 * address. The routine determines our precedence
506 * between advanced API scope/interface specification and basic API
507 * specification.
508 *
1af953ac
SZ
509 * This function should be nuked in the future, when we get rid of embedded
510 * scopeid thing.
984263bc
MD
511 *
512 * XXX actually, it is over-specification to return ifp against sin6_scope_id.
513 * there can be multiple interfaces that belong to a particular scope zone
514 * (in specification, we have 1:N mapping between a scope zone and interfaces).
515 * we may want to change the function to return something other than ifp.
516 */
517int
122ebd49
CP
518in6_embedscope(struct in6_addr *in6,
519 const struct sockaddr_in6 *sin6,
984263bc 520#ifdef HAVE_NRL_INPCB
122ebd49
CP
521 struct inpcb *in6p,
522#define in6p_outputopts inp_outputopts6
523#define in6p_moptions inp_moptions6
984263bc 524#else
122ebd49 525 struct in6pcb *in6p,
984263bc 526#endif
122ebd49 527 struct ifnet **ifpp)
984263bc
MD
528{
529 struct ifnet *ifp = NULL;
530 u_int32_t scopeid;
531
532 *in6 = sin6->sin6_addr;
533 scopeid = sin6->sin6_scope_id;
534 if (ifpp)
535 *ifpp = NULL;
536
537 /*
538 * don't try to read sin6->sin6_addr beyond here, since the caller may
539 * ask us to overwrite existing sockaddr_in6
540 */
541
542#ifdef ENABLE_DEFAULT_SCOPE
543 if (scopeid == 0)
544 scopeid = scope6_addr2default(in6);
545#endif
546
547 if (IN6_IS_SCOPE_LINKLOCAL(in6)) {
548 struct in6_pktinfo *pi;
549
550 /*
551 * KAME assumption: link id == interface id
552 */
553
554 if (in6p && in6p->in6p_outputopts &&
555 (pi = in6p->in6p_outputopts->ip6po_pktinfo) &&
556 pi->ipi6_ifindex) {
557 ifp = ifindex2ifnet[pi->ipi6_ifindex];
558 in6->s6_addr16[1] = htons(pi->ipi6_ifindex);
559 } else if (in6p && IN6_IS_ADDR_MULTICAST(in6) &&
560 in6p->in6p_moptions &&
561 in6p->in6p_moptions->im6o_multicast_ifp) {
562 ifp = in6p->in6p_moptions->im6o_multicast_ifp;
563 in6->s6_addr16[1] = htons(ifp->if_index);
564 } else if (scopeid) {
565 /* boundary check */
566 if (scopeid < 0 || if_index < scopeid)
567 return ENXIO; /* XXX EINVAL? */
568 ifp = ifindex2ifnet[scopeid];
1af953ac 569 /* XXX assignment to 16bit from 32bit variable */
984263bc
MD
570 in6->s6_addr16[1] = htons(scopeid & 0xffff);
571 }
572
573 if (ifpp)
574 *ifpp = ifp;
575 }
576
577 return 0;
578}
579#ifdef HAVE_NRL_INPCB
580#undef in6p_outputopts
581#undef in6p_moptions
582#endif
583
584/*
585 * generate standard sockaddr_in6 from embedded form.
586 * touches sin6_addr and sin6_scope_id only.
587 *
588 * this function should be nuked in the future, when we get rid of
589 * embedded scopeid thing.
590 */
591int
122ebd49
CP
592in6_recoverscope(struct sockaddr_in6 *sin6, const struct in6_addr *in6,
593 struct ifnet *ifp)
984263bc 594{
cb3deea5 595 u_int32_t zoneid;
984263bc
MD
596
597 sin6->sin6_addr = *in6;
598
599 /*
600 * don't try to read *in6 beyond here, since the caller may
601 * ask us to overwrite existing sockaddr_in6
602 */
603
604 sin6->sin6_scope_id = 0;
cb3deea5 605 if (IN6_IS_SCOPE_LINKLOCAL(in6) || IN6_IS_ADDR_MC_INTFACELOCAL(in6)) {
984263bc
MD
606 /*
607 * KAME assumption: link id == interface id
608 */
cb3deea5
SZ
609 zoneid = ntohs(sin6->sin6_addr.s6_addr16[1]);
610 if (zoneid) {
984263bc 611 /* sanity check */
cb3deea5 612 if (zoneid < 0 || if_index < zoneid)
984263bc 613 return ENXIO;
cb3deea5 614 if (ifp && ifp->if_index != zoneid)
984263bc
MD
615 return ENXIO;
616 sin6->sin6_addr.s6_addr16[1] = 0;
cb3deea5 617 sin6->sin6_scope_id = zoneid;
984263bc
MD
618 }
619 }
620
621 return 0;
622}
623
624/*
1af953ac 625 * just clear the embedded scope identifier.
984263bc
MD
626 */
627void
122ebd49 628in6_clearscope(struct in6_addr *addr)
984263bc 629{
cb3deea5 630 if (IN6_IS_SCOPE_LINKLOCAL(addr) || IN6_IS_ADDR_MC_INTFACELOCAL(addr))
984263bc
MD
631 addr->s6_addr16[1] = 0;
632}
02809d29
PA
633
634void
635addrsel_policy_init(void)
636{
637
638 init_policy_queue();
639
640 /* initialize the "last resort" policy */
641 bzero(&defaultaddrpolicy, sizeof(defaultaddrpolicy));
642 defaultaddrpolicy.label = ADDR_LABEL_NOTAPP;
643}
644
645/*
646 * Subroutines to manage the address selection policy table via sysctl.
647 */
648struct walkarg {
649 struct sysctl_req *w_req;
650};
651
652static int in6_src_sysctl(SYSCTL_HANDLER_ARGS);
653SYSCTL_DECL(_net_inet6_ip6);
654SYSCTL_NODE(_net_inet6_ip6, IPV6CTL_ADDRCTLPOLICY, addrctlpolicy,
5b8dbdac 655 CTLFLAG_RD, in6_src_sysctl, "Address selection policy");
02809d29
PA
656
657static int
658in6_src_sysctl(SYSCTL_HANDLER_ARGS)
659{
660 struct walkarg w;
661
662 if (req->newptr)
663 return EPERM;
664
665 bzero(&w, sizeof(w));
666 w.w_req = req;
667
668 return (walk_addrsel_policy(dump_addrsel_policyent, &w));
669}
670
671int
672in6_src_ioctl(u_long cmd, caddr_t data)
673{
674 int i;
675 struct in6_addrpolicy ent0;
676
677 if (cmd != SIOCAADDRCTL_POLICY && cmd != SIOCDADDRCTL_POLICY)
678 return (EOPNOTSUPP); /* check for safety */
679
680 ent0 = *(struct in6_addrpolicy *)data;
681
682 if (ent0.label == ADDR_LABEL_NOTAPP)
683 return (EINVAL);
684 /* check if the prefix mask is consecutive. */
685 if (in6_mask2len(&ent0.addrmask.sin6_addr, NULL) < 0)
686 return (EINVAL);
687 /* clear trailing garbages (if any) of the prefix address. */
688 for (i = 0; i < 4; i++) {
689 ent0.addr.sin6_addr.s6_addr32[i] &=
690 ent0.addrmask.sin6_addr.s6_addr32[i];
691 }
692 ent0.use = 0;
693
694 switch (cmd) {
695 case SIOCAADDRCTL_POLICY:
696 return (add_addrsel_policyent(&ent0));
697 case SIOCDADDRCTL_POLICY:
698 return (delete_addrsel_policyent(&ent0));
699 }
700
701 return (0); /* XXX: compromise compilers */
702}
703
704/*
705 * The followings are implementation of the policy table using a
706 * simple tail queue.
707 * XXX such details should be hidden.
708 * XXX implementation using binary tree should be more efficient.
709 */
710struct addrsel_policyent {
711 TAILQ_ENTRY(addrsel_policyent) ape_entry;
712 struct in6_addrpolicy ape_policy;
713};
714
715TAILQ_HEAD(addrsel_policyhead, addrsel_policyent);
716
717struct addrsel_policyhead addrsel_policytab;
718
719static void
720init_policy_queue(void)
721{
722 TAILQ_INIT(&addrsel_policytab);
723}
724
725static int
726add_addrsel_policyent(struct in6_addrpolicy *newpolicy)
727{
728 struct addrsel_policyent *new, *pol;
729
730 /* duplication check */
731 for (pol = TAILQ_FIRST(&addrsel_policytab); pol;
732 pol = TAILQ_NEXT(pol, ape_entry)) {
733 if (SA6_ARE_ADDR_EQUAL(&newpolicy->addr,
734 &pol->ape_policy.addr) &&
735 SA6_ARE_ADDR_EQUAL(&newpolicy->addrmask,
736 &pol->ape_policy.addrmask)) {
737 return (EEXIST); /* or override it? */
738 }
739 }
740
741 new = kmalloc(sizeof(*new), M_IFADDR, M_WAITOK | M_ZERO);
742
743 /* XXX: should validate entry */
744 new->ape_policy = *newpolicy;
745
746 TAILQ_INSERT_TAIL(&addrsel_policytab, new, ape_entry);
747
748 return (0);
749}
750
751static int
752delete_addrsel_policyent(struct in6_addrpolicy *key)
753{
754 struct addrsel_policyent *pol;
755
756 /* search for the entry in the table */
757 for (pol = TAILQ_FIRST(&addrsel_policytab); pol;
758 pol = TAILQ_NEXT(pol, ape_entry)) {
759 if (SA6_ARE_ADDR_EQUAL(&key->addr, &pol->ape_policy.addr) &&
760 SA6_ARE_ADDR_EQUAL(&key->addrmask,
761 &pol->ape_policy.addrmask)) {
762 break;
763 }
764 }
765 if (pol == NULL)
766 return (ESRCH);
767
768 TAILQ_REMOVE(&addrsel_policytab, pol, ape_entry);
769 kfree(pol, M_IFADDR);
770
771 return (0);
772}
773
774static int
775walk_addrsel_policy(int(*callback)(struct in6_addrpolicy *, void *), void *w)
776{
777 struct addrsel_policyent *pol;
778 int error = 0;
779
780 for (pol = TAILQ_FIRST(&addrsel_policytab); pol;
781 pol = TAILQ_NEXT(pol, ape_entry)) {
782 if ((error = (*callback)(&pol->ape_policy, w)) != 0)
783 return (error);
784 }
785
786 return (error);
787}
788
789static int
790dump_addrsel_policyent(struct in6_addrpolicy *pol, void *arg)
791{
792 int error = 0;
793 struct walkarg *w = arg;
794
795 error = SYSCTL_OUT(w->w_req, pol, sizeof(*pol));
796
797 return (error);
798}