inet6: Take route reference before announcing address
[dragonfly.git] / sys / netinet6 / in6.c
CommitLineData
984263bc
MD
1/* $FreeBSD: src/sys/netinet6/in6.c,v 1.7.2.9 2002/04/28 05:40:26 suz Exp $ */
2/* $KAME: in6.c,v 1.259 2002/01/21 11:37:50 keiichi 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.c 8.2 (Berkeley) 11/15/93
62 */
63
64#include "opt_inet.h"
65#include "opt_inet6.h"
66
67#include <sys/param.h>
68#include <sys/errno.h>
69#include <sys/malloc.h>
70#include <sys/socket.h>
71#include <sys/socketvar.h>
72#include <sys/sockio.h>
73#include <sys/systm.h>
74#include <sys/proc.h>
895c1f85 75#include <sys/priv.h>
984263bc
MD
76#include <sys/time.h>
77#include <sys/kernel.h>
78#include <sys/syslog.h>
cfd79120 79#include <sys/jail.h>
002c1265 80
abf782b3 81#include <sys/thread2.h>
002c1265 82#include <sys/msgport2.h>
984263bc
MD
83
84#include <net/if.h>
85#include <net/if_types.h>
86#include <net/route.h>
87#include <net/if_dl.h>
c2549e6c
SZ
88#include <net/netmsg2.h>
89#include <net/netisr2.h>
984263bc
MD
90
91#include <netinet/in.h>
92#include <netinet/in_var.h>
93#include <netinet/if_ether.h>
984263bc
MD
94#include <netinet/in_systm.h>
95#include <netinet/ip.h>
96#include <netinet/in_pcb.h>
984263bc
MD
97
98#include <netinet/ip6.h>
99#include <netinet6/ip6_var.h>
100#include <netinet6/nd6.h>
101#include <netinet6/mld6_var.h>
102#include <netinet6/ip6_mroute.h>
103#include <netinet6/in6_ifattach.h>
104#include <netinet6/scope6_var.h>
984263bc 105#include <netinet6/in6_pcb.h>
c66faf37 106#include <netinet6/in6_var.h>
984263bc
MD
107
108#include <net/net_osdep.h>
109
984263bc
MD
110/*
111 * Definitions of some costant IP6 addresses.
112 */
84204577
MD
113const struct in6_addr kin6addr_any = IN6ADDR_ANY_INIT;
114const struct in6_addr kin6addr_loopback = IN6ADDR_LOOPBACK_INIT;
115const struct in6_addr kin6addr_nodelocal_allnodes =
984263bc 116 IN6ADDR_NODELOCAL_ALLNODES_INIT;
84204577 117const struct in6_addr kin6addr_linklocal_allnodes =
984263bc 118 IN6ADDR_LINKLOCAL_ALLNODES_INIT;
84204577 119const struct in6_addr kin6addr_linklocal_allrouters =
984263bc
MD
120 IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
121
122const struct in6_addr in6mask0 = IN6MASK0;
123const struct in6_addr in6mask32 = IN6MASK32;
124const struct in6_addr in6mask64 = IN6MASK64;
125const struct in6_addr in6mask96 = IN6MASK96;
126const struct in6_addr in6mask128 = IN6MASK128;
127
128const struct sockaddr_in6 sa6_any = {sizeof(sa6_any), AF_INET6,
129 0, 0, IN6ADDR_ANY_INIT, 0};
130
2501b0ea
SZ
131static int in6_lifaddr_ioctl (u_long, caddr_t, struct ifnet *,
132 struct thread *);
56d702cc
RG
133static int in6_ifinit (struct ifnet *, struct in6_ifaddr *,
134 struct sockaddr_in6 *, int);
135static void in6_unlink_ifa (struct in6_ifaddr *, struct ifnet *);
ecdefdda 136static void in6_ifloop_request_callback(int, int, struct rt_addrinfo *, struct rtentry *, void *);
984263bc 137
c2549e6c
SZ
138static void in6_control_internal_dispatch(netmsg_t);
139static int in6_control_internal(u_long, caddr_t, struct ifnet *,
140 struct thread *);
141
984263bc
MD
142struct in6_multihead in6_multihead; /* XXX BSS initialization */
143
984263bc
MD
144/*
145 * Subroutine for in6_ifaddloop() and in6_ifremloop().
146 * This routine does actual work.
147 */
148static void
5785f5fb
RM
149in6_ifloop_request(int cmd, struct ifaddr *ifa,
150 void (*callback)(int, int, struct rt_addrinfo *, struct rtentry *, void *))
984263bc
MD
151{
152 struct sockaddr_in6 all1_sa;
ecdefdda
MD
153 struct rt_addrinfo rtinfo;
154 int error;
bde3511a 155
984263bc
MD
156 bzero(&all1_sa, sizeof(all1_sa));
157 all1_sa.sin6_family = AF_INET6;
158 all1_sa.sin6_len = sizeof(struct sockaddr_in6);
159 all1_sa.sin6_addr = in6mask128;
bde3511a 160
984263bc
MD
161 /*
162 * We specify the address itself as the gateway, and set the
163 * RTF_LLINFO flag, so that the corresponding host route would have
164 * the flag, and thus applications that assume traditional behavior
165 * would be happy. Note that we assume the caller of the function
166 * (probably implicitly) set nd6_rtrequest() to ifa->ifa_rtrequest,
167 * which changes the outgoing interface to the loopback interface.
168 */
ecdefdda
MD
169 bzero(&rtinfo, sizeof(struct rt_addrinfo));
170 rtinfo.rti_info[RTAX_DST] = ifa->ifa_addr;
171 rtinfo.rti_info[RTAX_GATEWAY] = ifa->ifa_addr;
172 rtinfo.rti_info[RTAX_NETMASK] = (struct sockaddr *)&all1_sa;
173 rtinfo.rti_flags = RTF_UP|RTF_HOST|RTF_LLINFO;
174
5785f5fb 175 error = rtrequest1_global(cmd, &rtinfo, callback, ifa, RTREQ_PRIO_NORM);
ecdefdda 176 if (error != 0) {
984263bc
MD
177 log(LOG_ERR, "in6_ifloop_request: "
178 "%s operation failed for %s (errno=%d)\n",
5785f5fb 179 cmd == RTM_ADD ? "ADD" : cmd == RTM_DELETE ? "DELETE" : "GET",
984263bc 180 ip6_sprintf(&((struct in6_ifaddr *)ifa)->ia_addr.sin6_addr),
ecdefdda 181 error);
984263bc 182 }
ecdefdda
MD
183}
184
185static void
186in6_ifloop_request_callback(int cmd, int error, struct rt_addrinfo *rtinfo,
187 struct rtentry *rt, void *arg)
188{
189 struct ifaddr *ifa = arg;
190
191 if (error)
192 goto done;
984263bc
MD
193
194 /*
195 * Make sure rt_ifa be equal to IFA, the second argument of the
196 * function.
197 * We need this because when we refer to rt_ifa->ia6_flags in
198 * ip6_input, we assume that the rt_ifa points to the address instead
199 * of the loopback address.
200 */
ecdefdda
MD
201 if (cmd == RTM_ADD && rt && ifa != rt->rt_ifa) {
202 ++rt->rt_refcnt;
203 IFAFREE(rt->rt_ifa);
984263bc 204 IFAREF(ifa);
ecdefdda
MD
205 rt->rt_ifa = ifa;
206 --rt->rt_refcnt;
984263bc
MD
207 }
208
209 /*
5785f5fb
RM
210 * Report the addition/removal of the address to the routing socket,
211 * unless the address is marked as tentative, where it will be reported
212 * once DAD completes.
984263bc
MD
213 * XXX: since we called rtinit for a p2p interface with a destination,
214 * we end up reporting twice in such a case. Should we rather
215 * omit the second report?
216 */
ecdefdda 217 if (rt) {
5785f5fb
RM
218 if (mycpuid == 0) {
219 struct in6_ifaddr *ia6 = (struct in6_ifaddr *)ifa;
220
221 if (cmd != RTM_ADD ||
222 !(ia6->ia6_flags & IN6_IFF_TENTATIVE))
223 rt_newaddrmsg(cmd, ifa, error, rt);
224 }
984263bc 225 if (cmd == RTM_DELETE) {
ecdefdda
MD
226 if (rt->rt_refcnt == 0) {
227 ++rt->rt_refcnt;
228 rtfree(rt);
984263bc 229 }
984263bc
MD
230 }
231 }
ecdefdda
MD
232done:
233 /* no way to return any new error */
234 ;
984263bc
MD
235}
236
5785f5fb
RM
237static void
238in6_newaddrmsg_callback(int cmd, int error, struct rt_addrinfo *rtinfo,
239 struct rtentry *rt, void *arg)
240{
241 struct ifaddr *ifa = arg;
242
83989241
RM
243 if (error == 0 && rt != NULL && mycpuid == 0) {
244 ++rt->rt_refcnt;
5785f5fb 245 rt_newaddrmsg(RTM_ADD, ifa, error, rt);
83989241
RM
246 --rt->rt_refcnt;
247 }
5785f5fb
RM
248}
249
250void
251in6_newaddrmsg(struct ifaddr *ifa)
252{
253 in6_ifloop_request(RTM_GET, ifa, in6_newaddrmsg_callback);
254}
255
984263bc
MD
256/*
257 * Add ownaddr as loopback rtentry. We previously add the route only if
258 * necessary (ex. on a p2p link). However, since we now manage addresses
259 * separately from prefixes, we should always add the route. We can't
260 * rely on the cloning mechanism from the corresponding interface route
261 * any more.
262 */
0d16ba1d 263void
984263bc
MD
264in6_ifaddloop(struct ifaddr *ifa)
265{
266 struct rtentry *rt;
267
268 /* If there is no loopback entry, allocate one. */
f3ed2586 269 rt = rtpurelookup(ifa->ifa_addr);
f23061d4
JH
270 if (rt == NULL || !(rt->rt_flags & RTF_HOST) ||
271 !(rt->rt_ifp->if_flags & IFF_LOOPBACK))
5785f5fb 272 in6_ifloop_request(RTM_ADD, ifa, in6_ifloop_request_callback);
0c3c561c 273 if (rt != NULL)
984263bc
MD
274 rt->rt_refcnt--;
275}
276
277/*
278 * Remove loopback rtentry of ownaddr generated by in6_ifaddloop(),
279 * if it exists.
280 */
0d16ba1d 281void
984263bc
MD
282in6_ifremloop(struct ifaddr *ifa)
283{
284 struct in6_ifaddr *ia;
285 struct rtentry *rt;
286 int ia_count = 0;
287
288 /*
289 * Some of BSD variants do not remove cloned routes
290 * from an interface direct route, when removing the direct route
291 * (see comments in net/net_osdep.h). Even for variants that do remove
292 * cloned routes, they could fail to remove the cloned routes when
293 * we handle multple addresses that share a common prefix.
294 * So, we should remove the route corresponding to the deleted address
295 * regardless of the result of in6_is_ifloop_auto().
296 */
297
298 /*
299 * Delete the entry only if exact one ifa exists. More than one ifa
300 * can exist if we assign a same single address to multiple
301 * (probably p2p) interfaces.
302 * XXX: we should avoid such a configuration in IPv6...
303 */
304 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
305 if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa), &ia->ia_addr.sin6_addr)) {
306 ia_count++;
307 if (ia_count > 1)
308 break;
309 }
310 }
311
312 if (ia_count == 1) {
313 /*
314 * Before deleting, check if a corresponding loopbacked host
315 * route surely exists. With this check, we can avoid to
316 * delete an interface direct route whose destination is same
317 * as the address being removed. This can happen when remofing
318 * a subnet-router anycast address on an interface attahced
319 * to a shared medium.
320 */
f3ed2586 321 rt = rtpurelookup(ifa->ifa_addr);
f23061d4
JH
322 if (rt != NULL && (rt->rt_flags & RTF_HOST) &&
323 (rt->rt_ifp->if_flags & IFF_LOOPBACK)) {
984263bc 324 rt->rt_refcnt--;
5785f5fb
RM
325 in6_ifloop_request(RTM_DELETE, ifa,
326 in6_ifloop_request_callback);
984263bc
MD
327 }
328 }
329}
330
984263bc 331int
611e7d72 332in6_mask2len(const struct in6_addr *mask, const u_char *lim0)
984263bc
MD
333{
334 int x = 0, y;
611e7d72 335 const u_char *lim = lim0, *p;
984263bc
MD
336
337 if (lim0 == NULL ||
611e7d72
SZ
338 lim0 - (const u_char *)mask > sizeof(*mask)) {
339 /* Ignore the scope_id part */
340 lim = (const u_char *)mask + sizeof(*mask);
341 }
342 for (p = (const u_char *)mask; p < lim; x++, p++) {
984263bc
MD
343 if (*p != 0xff)
344 break;
345 }
346 y = 0;
347 if (p < lim) {
348 for (y = 0; y < 8; y++) {
349 if ((*p & (0x80 >> y)) == 0)
350 break;
351 }
352 }
353
354 /*
611e7d72 355 * When the limit pointer is given, do a stricter check on the
984263bc
MD
356 * remaining bits.
357 */
358 if (p < lim) {
359 if (y != 0 && (*p & (0x00ff >> y)) != 0)
bde3511a 360 return (-1);
984263bc
MD
361 for (p = p + 1; p < lim; p++)
362 if (*p != 0)
bde3511a 363 return (-1);
984263bc 364 }
bde3511a 365
984263bc
MD
366 return x * 8 + y;
367}
368
984263bc
MD
369#define ifa2ia6(ifa) ((struct in6_ifaddr *)(ifa))
370#define ia62ifa(ia6) (&((ia6)->ia_ifa))
371
002c1265
MD
372void
373in6_control_dispatch(netmsg_t msg)
374{
375 int error;
376
2501b0ea 377 error = in6_control(msg->control.nm_cmd,
002c1265
MD
378 msg->control.nm_data,
379 msg->control.nm_ifp,
380 msg->control.nm_td);
381 lwkt_replymsg(&msg->control.base.lmsg, error);
382}
383
984263bc 384int
2501b0ea 385in6_control(u_long cmd, caddr_t data, struct ifnet *ifp, struct thread *td)
984263bc 386{
c2549e6c 387 struct netmsg_pru_control msg;
984263bc 388
bc0dc273
SZ
389 switch (cmd) {
390 case SIOCSIFPREFIX_IN6:
391 case SIOCDIFPREFIX_IN6:
392 case SIOCAIFPREFIX_IN6:
393 case SIOCCIFPREFIX_IN6:
394 case SIOCSGIFPREFIX_IN6:
395 case SIOCGIFPREFIX_IN6:
396 log(LOG_NOTICE, "prefix ioctls are now invalidated. "
397 "please use ifconfig.\n");
398 return (EOPNOTSUPP);
399
400 case SIOCSIFADDR_IN6:
401 case SIOCSIFDSTADDR_IN6:
402 case SIOCSIFNETMASK_IN6:
403 /*
404 * Since IPv6 allows a node to assign multiple addresses
405 * on a single interface, SIOCSIFxxx ioctls are not suitable
406 * and should be unused.
407 */
6eef13e5 408 /* We decided to obsolete this command (20000704) */
bc0dc273
SZ
409 return (EINVAL);
410
411 case SIOCSIFADDR:
412 case SIOCSIFDSTADDR:
413 case SIOCSIFBRDADDR:
414 case SIOCSIFNETMASK:
415 /*
416 * Do not pass those ioctl to driver handler since they are not
417 * properly setup. Instead just error out.
418 */
419 return (EOPNOTSUPP);
c2549e6c 420
c2549e6c
SZ
421 /* mroute */
422 case SIOCGETSGCNT_IN6:
423 case SIOCGETMIFCNT_IN6:
424 /* srcsel policy */
425 case SIOCAADDRCTL_POLICY:
426 case SIOCDADDRCTL_POLICY:
427 /* nd6 */
428 case SIOCSNDFLUSH_IN6:
429 case SIOCSPFXFLUSH_IN6:
430 case SIOCSRTRFLUSH_IN6:
431 case SIOCSDEFIFACE_IN6:
432 case SIOCSIFINFO_FLAGS:
433 case OSIOCGIFINFO_IN6:
434 case SIOCGIFINFO_IN6:
435 case SIOCGDRLST_IN6:
436 case SIOCGPRLST_IN6:
437 case SIOCGNBRINFO_IN6:
438 case SIOCGDEFIFACE_IN6:
439 /* scope6 */
440 case SIOCSSCOPE6:
441 case SIOCGSCOPE6:
442 case SIOCGSCOPE6DEF:
443 /* change address */
2501b0ea
SZ
444 case SIOCALIFADDR:
445 case SIOCDLIFADDR:
c2549e6c
SZ
446 case SIOCSIFALIFETIME_IN6:
447 case SIOCAIFADDR_IN6:
448 case SIOCDIFADDR_IN6:
449 /*
46e269be 450 * Dispatch these SIOCs to netisr0.
c2549e6c
SZ
451 */
452 netmsg_init(&msg.base, NULL, &curthread->td_msgport, 0,
453 in6_control_internal_dispatch);
454 msg.nm_cmd = cmd;
455 msg.nm_data = data;
456 msg.nm_ifp = ifp;
457 msg.nm_td = td;
458 lwkt_domsg(netisr_cpuport(0), &msg.base.lmsg, 0);
459 return msg.base.lmsg.ms_error;
460
461 default:
462 return in6_control_internal(cmd, data, ifp, td);
bc0dc273 463 }
c2549e6c
SZ
464}
465
466static void
467in6_control_internal_dispatch(netmsg_t msg)
468{
469 int error;
470
471 error = in6_control_internal(msg->control.nm_cmd, msg->control.nm_data,
472 msg->control.nm_ifp, msg->control.nm_td);
473 lwkt_replymsg(&msg->lmsg, error);
474}
475
476static int
477in6_control_internal(u_long cmd, caddr_t data, struct ifnet *ifp,
478 struct thread *td)
479{
6eef13e5
SZ
480 struct in6_ifreq *ifr = (struct in6_ifreq *)data;
481 struct in6_ifaddr *ia = NULL;
482 struct in6_aliasreq *ifra = (struct in6_aliasreq *)data;
483 struct in6_ifextra *xtra;
484 boolean_t privileged;
c2549e6c 485 int error;
bc0dc273 486
6eef13e5 487 privileged = FALSE;
895c1f85 488 if (priv_check(td, PRIV_ROOT) == 0)
6eef13e5 489 privileged = TRUE;
984263bc 490
2501b0ea
SZ
491 switch (cmd) {
492 case SIOCALIFADDR:
493 case SIOCDLIFADDR:
494 if (!privileged)
495 return (EPERM);
496 /* FALLTHROUGH */
497 case SIOCGLIFADDR:
498 if (ifp == NULL)
499 return (EOPNOTSUPP);
500 return in6_lifaddr_ioctl(cmd, data, ifp, td);
501 }
502
984263bc
MD
503 switch (cmd) {
504 case SIOCGETSGCNT_IN6:
505 case SIOCGETMIFCNT_IN6:
506 return (mrt6_ioctl(cmd, data));
507 }
508
02809d29
PA
509 switch(cmd) {
510 case SIOCAADDRCTL_POLICY:
511 case SIOCDADDRCTL_POLICY:
512 if (!privileged)
513 return (EPERM);
514 return (in6_src_ioctl(cmd, data));
515 }
516
984263bc 517 if (ifp == NULL)
bde3511a 518 return (EOPNOTSUPP);
984263bc
MD
519
520 switch (cmd) {
521 case SIOCSNDFLUSH_IN6:
522 case SIOCSPFXFLUSH_IN6:
523 case SIOCSRTRFLUSH_IN6:
524 case SIOCSDEFIFACE_IN6:
525 case SIOCSIFINFO_FLAGS:
526 if (!privileged)
bde3511a 527 return (EPERM);
6eef13e5 528 /* FALLTHROUGH */
984263bc
MD
529 case OSIOCGIFINFO_IN6:
530 case SIOCGIFINFO_IN6:
531 case SIOCGDRLST_IN6:
532 case SIOCGPRLST_IN6:
533 case SIOCGNBRINFO_IN6:
534 case SIOCGDEFIFACE_IN6:
bde3511a 535 return (nd6_ioctl(cmd, data, ifp));
984263bc
MD
536 }
537
984263bc
MD
538 switch (cmd) {
539 case SIOCSSCOPE6:
540 if (!privileged)
bde3511a
JH
541 return (EPERM);
542 return (scope6_set(ifp,
698ac46c 543 (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
6eef13e5 544
984263bc 545 case SIOCGSCOPE6:
bde3511a 546 return (scope6_get(ifp,
698ac46c 547 (struct scope6_id *)ifr->ifr_ifru.ifru_scope_id));
6eef13e5 548
984263bc 549 case SIOCGSCOPE6DEF:
bde3511a 550 return (scope6_get_default((struct scope6_id *)
698ac46c 551 ifr->ifr_ifru.ifru_scope_id));
984263bc
MD
552 }
553
984263bc
MD
554 /*
555 * Find address for this interface, if it exists.
556 */
557 if (ifra->ifra_addr.sin6_family == AF_INET6) { /* XXX */
558 struct sockaddr_in6 *sa6 =
6eef13e5 559 (struct sockaddr_in6 *)&ifra->ifra_addr;
984263bc
MD
560
561 if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr)) {
562 if (sa6->sin6_addr.s6_addr16[1] == 0) {
6eef13e5 563 /* Link ID is not embedded by the user */
984263bc 564 sa6->sin6_addr.s6_addr16[1] =
6eef13e5 565 htons(ifp->if_index);
984263bc 566 } else if (sa6->sin6_addr.s6_addr16[1] !=
6eef13e5
SZ
567 htons(ifp->if_index)) {
568 /* Link ID contradicts */
569 return (EINVAL);
984263bc
MD
570 }
571 if (sa6->sin6_scope_id) {
572 if (sa6->sin6_scope_id !=
573 (u_int32_t)ifp->if_index)
bde3511a 574 return (EINVAL);
984263bc
MD
575 sa6->sin6_scope_id = 0; /* XXX: good way? */
576 }
577 }
578 ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr);
579 }
580
581 switch (cmd) {
984263bc
MD
582 case SIOCDIFADDR_IN6:
583 /*
6eef13e5 584 * For IPv4, we look for existing in_ifaddr here to allow
984263bc
MD
585 * "ifconfig if0 delete" to remove first IPv4 address on the
586 * interface. For IPv6, as the spec allow multiple interface
587 * address from the day one, we consider "remove the first one"
588 * semantics to be not preferable.
589 */
590 if (ia == NULL)
bde3511a 591 return (EADDRNOTAVAIL);
984263bc
MD
592 /* FALLTHROUGH */
593 case SIOCAIFADDR_IN6:
594 /*
595 * We always require users to specify a valid IPv6 address for
596 * the corresponding operation.
597 */
598 if (ifra->ifra_addr.sin6_family != AF_INET6 ||
599 ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6))
bde3511a 600 return (EAFNOSUPPORT);
984263bc 601 if (!privileged)
bde3511a 602 return (EPERM);
984263bc
MD
603 break;
604
605 case SIOCGIFADDR_IN6:
6eef13e5
SZ
606 /* This interface is basically deprecated. Use SIOCGIFCONF. */
607 /* FALLTHROUGH */
984263bc
MD
608 case SIOCGIFAFLAG_IN6:
609 case SIOCGIFNETMASK_IN6:
610 case SIOCGIFDSTADDR_IN6:
611 case SIOCGIFALIFETIME_IN6:
6eef13e5 612 /* Must think again about its semantics */
984263bc 613 if (ia == NULL)
bde3511a 614 return (EADDRNOTAVAIL);
984263bc 615 break;
6eef13e5 616
984263bc
MD
617 case SIOCSIFALIFETIME_IN6:
618 {
6eef13e5 619 const struct in6_addrlifetime *lt;
984263bc
MD
620
621 if (!privileged)
bde3511a 622 return (EPERM);
984263bc 623 if (ia == NULL)
bde3511a 624 return (EADDRNOTAVAIL);
6eef13e5 625 /* Sanity for overflow - beware unsigned */
984263bc 626 lt = &ifr->ifr_ifru.ifru_lifetime;
6eef13e5
SZ
627 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME &&
628 lt->ia6t_vltime + time_uptime < time_uptime)
984263bc 629 return EINVAL;
6eef13e5
SZ
630 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME &&
631 lt->ia6t_pltime + time_uptime < time_uptime)
984263bc 632 return EINVAL;
984263bc
MD
633 break;
634 }
635 }
636
637 switch (cmd) {
984263bc
MD
638 case SIOCGIFADDR_IN6:
639 ifr->ifr_addr = ia->ia_addr;
640 break;
641
642 case SIOCGIFDSTADDR_IN6:
bde3511a
JH
643 if (!(ifp->if_flags & IFF_POINTOPOINT))
644 return (EINVAL);
984263bc 645 /*
6eef13e5 646 * XXX: Should we check if ifa_dstaddr is NULL and return
984263bc
MD
647 * an error?
648 */
649 ifr->ifr_dstaddr = ia->ia_dstaddr;
650 break;
651
652 case SIOCGIFNETMASK_IN6:
653 ifr->ifr_addr = ia->ia_prefixmask;
654 break;
655
656 case SIOCGIFAFLAG_IN6:
657 ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags;
658 break;
659
660 case SIOCGIFSTAT_IN6:
6eef13e5 661 if ((xtra = ifp->if_afdata[AF_INET6]) == NULL)
984263bc 662 return EINVAL;
698ac46c 663 bzero(&ifr->ifr_ifru.ifru_stat,
6eef13e5 664 sizeof(ifr->ifr_ifru.ifru_stat));
8e13abe7 665 ifr->ifr_ifru.ifru_stat = *xtra->in6_ifstat;
984263bc
MD
666 break;
667
668 case SIOCGIFSTAT_ICMP6:
6eef13e5 669 if ((xtra = ifp->if_afdata[AF_INET6]) == NULL)
8e13abe7 670 return EINVAL;
698ac46c 671 bzero(&ifr->ifr_ifru.ifru_stat,
6eef13e5 672 sizeof(ifr->ifr_ifru.ifru_icmp6stat));
8e13abe7 673 ifr->ifr_ifru.ifru_icmp6stat = *xtra->icmp6_ifstat;
984263bc
MD
674 break;
675
676 case SIOCGIFALIFETIME_IN6:
677 ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime;
678 break;
679
680 case SIOCSIFALIFETIME_IN6:
681 ia->ia6_lifetime = ifr->ifr_ifru.ifru_lifetime;
984263bc
MD
682 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
683 ia->ia6_lifetime.ia6t_expire =
6eef13e5
SZ
684 time_uptime + ia->ia6_lifetime.ia6t_vltime;
685 } else {
984263bc 686 ia->ia6_lifetime.ia6t_expire = 0;
6eef13e5 687 }
984263bc
MD
688 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
689 ia->ia6_lifetime.ia6t_preferred =
6eef13e5
SZ
690 time_uptime + ia->ia6_lifetime.ia6t_pltime;
691 } else {
984263bc 692 ia->ia6_lifetime.ia6t_preferred = 0;
6eef13e5 693 }
984263bc
MD
694 break;
695
696 case SIOCAIFADDR_IN6:
697 {
5bd4422e 698 int i, error = 0, iaIsNew;
984263bc
MD
699 struct nd_prefix pr0, *pr;
700
5bd4422e
SZ
701 if (ia != NULL)
702 iaIsNew = 0;
703 else
704 iaIsNew = 1;
705
984263bc 706 /*
6eef13e5 707 * First, make or update the interface address structure,
984263bc
MD
708 * and link it to the list.
709 */
710 if ((error = in6_update_ifa(ifp, ifra, ia)) != 0)
bde3511a 711 return (error);
984263bc
MD
712
713 /*
6eef13e5
SZ
714 * Then, make the prefix on-link on the interface.
715 * XXX: We'd rather create the prefix before the address, but
984263bc
MD
716 * we need at least one address to install the corresponding
717 * interface route, so we configure the address first.
718 */
719
720 /*
6eef13e5 721 * Convert mask to prefix length (prefixmask has already
984263bc
MD
722 * been validated in in6_update_ifa().
723 */
724 bzero(&pr0, sizeof(pr0));
725 pr0.ndpr_ifp = ifp;
726 pr0.ndpr_plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
6eef13e5 727 NULL);
984263bc 728 if (pr0.ndpr_plen == 128)
6eef13e5 729 break; /* no need to install a host route. */
984263bc
MD
730 pr0.ndpr_prefix = ifra->ifra_addr;
731 pr0.ndpr_mask = ifra->ifra_prefixmask.sin6_addr;
6eef13e5 732 /* Apply the mask for safety. */
984263bc
MD
733 for (i = 0; i < 4; i++) {
734 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
6eef13e5 735 ifra->ifra_prefixmask.sin6_addr.s6_addr32[i];
984263bc
MD
736 }
737 /*
6eef13e5 738 * XXX: Since we don't have an API to set prefix (not address)
984263bc
MD
739 * lifetimes, we just use the same lifetimes as addresses.
740 * The (temporarily) installed lifetimes can be overridden by
741 * later advertised RAs (when accept_rtadv is non 0), which is
742 * an intended behavior.
743 */
744 pr0.ndpr_raf_onlink = 1; /* should be configurable? */
745 pr0.ndpr_raf_auto =
6eef13e5 746 ((ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0);
984263bc
MD
747 pr0.ndpr_vltime = ifra->ifra_lifetime.ia6t_vltime;
748 pr0.ndpr_pltime = ifra->ifra_lifetime.ia6t_pltime;
749
6eef13e5 750 /* Add the prefix if there's one. */
984263bc
MD
751 if ((pr = nd6_prefix_lookup(&pr0)) == NULL) {
752 /*
753 * nd6_prelist_add will install the corresponding
754 * interface route.
755 */
756 if ((error = nd6_prelist_add(&pr0, NULL, &pr)) != 0)
bde3511a 757 return (error);
984263bc 758 if (pr == NULL) {
0ca0cd25 759 log(LOG_ERR, "nd6_prelist_add succeeded but "
984263bc 760 "no prefix\n");
bde3511a 761 return (EINVAL); /* XXX panic here? */
984263bc
MD
762 }
763 }
6eef13e5
SZ
764
765 ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr);
766 if (ia == NULL) {
767 /* XXX: This should not happen! */
984263bc
MD
768 log(LOG_ERR, "in6_control: addition succeeded, but"
769 " no ifaddr\n");
770 } else {
bde3511a 771 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) &&
6eef13e5
SZ
772 ia->ia6_ndpr == NULL) {
773 /*
774 * New autoconf address
775 */
984263bc
MD
776 ia->ia6_ndpr = pr;
777 pr->ndpr_refcnt++;
778
779 /*
780 * If this is the first autoconf address from
781 * the prefix, create a temporary address
782 * as well (when specified).
783 */
6eef13e5 784 if (ip6_use_tempaddr && pr->ndpr_refcnt == 1) {
984263bc 785 int e;
6eef13e5 786
984263bc
MD
787 if ((e = in6_tmpifadd(ia, 1)) != 0) {
788 log(LOG_NOTICE, "in6_control: "
789 "failed to create a "
790 "temporary address, "
6eef13e5 791 "errno=%d\n", e);
984263bc
MD
792 }
793 }
794 }
795
796 /*
6eef13e5 797 * This might affect the status of autoconfigured
984263bc
MD
798 * addresses, that is, this address might make
799 * other addresses detached.
800 */
801 pfxlist_onlink_check();
802 }
5bd4422e
SZ
803 if (error == 0 && ia) {
804 EVENTHANDLER_INVOKE(ifaddr_event, ifp,
6eef13e5
SZ
805 iaIsNew ? IFADDR_EVENT_ADD : IFADDR_EVENT_CHANGE,
806 &ia->ia_ifa);
5bd4422e 807 }
984263bc
MD
808 break;
809 }
810
811 case SIOCDIFADDR_IN6:
812 {
813 int i = 0;
814 struct nd_prefix pr0, *pr;
815
816 /*
817 * If the address being deleted is the only one that owns
818 * the corresponding prefix, expire the prefix as well.
6eef13e5 819 * XXX: Theoretically, we don't have to warry about such
984263bc
MD
820 * relationship, since we separate the address management
821 * and the prefix management. We do this, however, to provide
822 * as much backward compatibility as possible in terms of
823 * the ioctl operation.
824 */
825 bzero(&pr0, sizeof(pr0));
826 pr0.ndpr_ifp = ifp;
827 pr0.ndpr_plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr,
6eef13e5 828 NULL);
984263bc
MD
829 if (pr0.ndpr_plen == 128)
830 goto purgeaddr;
831 pr0.ndpr_prefix = ia->ia_addr;
832 pr0.ndpr_mask = ia->ia_prefixmask.sin6_addr;
833 for (i = 0; i < 4; i++) {
834 pr0.ndpr_prefix.sin6_addr.s6_addr32[i] &=
6eef13e5 835 ia->ia_prefixmask.sin6_addr.s6_addr32[i];
984263bc
MD
836 }
837 /*
838 * The logic of the following condition is a bit complicated.
839 * We expire the prefix when
6eef13e5 840 * 1. The address obeys autoconfiguration and it is the
984263bc 841 * only owner of the associated prefix, or
6eef13e5 842 * 2. The address does not obey autoconf and there is no
984263bc
MD
843 * other owner of the prefix.
844 */
845 if ((pr = nd6_prefix_lookup(&pr0)) != NULL &&
bde3511a 846 (((ia->ia6_flags & IN6_IFF_AUTOCONF) &&
6eef13e5 847 pr->ndpr_refcnt == 1) ||
bde3511a 848 (!(ia->ia6_flags & IN6_IFF_AUTOCONF) &&
6eef13e5 849 pr->ndpr_refcnt == 0)))
984263bc 850 pr->ndpr_expire = 1; /* XXX: just for expiration */
984263bc 851
5bd4422e
SZ
852purgeaddr:
853 EVENTHANDLER_INVOKE(ifaddr_event, ifp, IFADDR_EVENT_DELETE,
6eef13e5 854 &ia->ia_ifa);
984263bc
MD
855 in6_purgeaddr(&ia->ia_ifa);
856 break;
857 }
858
859 default:
6eef13e5 860 if (ifp->if_ioctl == NULL)
bde3511a 861 return (EOPNOTSUPP);
a3dd34d2 862 ifnet_serialize_all(ifp);
78195a76 863 error = ifp->if_ioctl(ifp, cmd, data, td->td_proc->p_ucred);
a3dd34d2 864 ifnet_deserialize_all(ifp);
78195a76 865 return (error);
984263bc
MD
866 }
867
bde3511a 868 return (0);
984263bc
MD
869}
870
871/*
872 * Update parameters of an IPv6 interface address.
873 * If necessary, a new entry is created and linked into address chains.
874 * This function is separated from in6_control().
875 * XXX: should this be performed under splnet()?
876 */
877int
122ebd49
CP
878in6_update_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra,
879 struct in6_ifaddr *ia)
984263bc 880{
5785f5fb 881 int error = 0, hostIsNew = 0, was_tentative, plen = -1;
984263bc
MD
882 struct in6_ifaddr *oia;
883 struct sockaddr_in6 dst6;
884 struct in6_addrlifetime *lt;
885
886 /* Validate parameters */
887 if (ifp == NULL || ifra == NULL) /* this maybe redundant */
bde3511a 888 return (EINVAL);
984263bc
MD
889
890 /*
891 * The destination address for a p2p link must have a family
892 * of AF_UNSPEC or AF_INET6.
893 */
bde3511a 894 if ((ifp->if_flags & IFF_POINTOPOINT) &&
984263bc
MD
895 ifra->ifra_dstaddr.sin6_family != AF_INET6 &&
896 ifra->ifra_dstaddr.sin6_family != AF_UNSPEC)
bde3511a 897 return (EAFNOSUPPORT);
984263bc
MD
898 /*
899 * validate ifra_prefixmask. don't check sin6_family, netmask
900 * does not carry fields other than sin6_len.
901 */
902 if (ifra->ifra_prefixmask.sin6_len > sizeof(struct sockaddr_in6))
bde3511a 903 return (EINVAL);
984263bc
MD
904 /*
905 * Because the IPv6 address architecture is classless, we require
906 * users to specify a (non 0) prefix length (mask) for a new address.
907 * We also require the prefix (when specified) mask is valid, and thus
908 * reject a non-consecutive mask.
909 */
910 if (ia == NULL && ifra->ifra_prefixmask.sin6_len == 0)
bde3511a 911 return (EINVAL);
984263bc
MD
912 if (ifra->ifra_prefixmask.sin6_len != 0) {
913 plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr,
914 (u_char *)&ifra->ifra_prefixmask +
915 ifra->ifra_prefixmask.sin6_len);
916 if (plen <= 0)
bde3511a 917 return (EINVAL);
984263bc
MD
918 }
919 else {
920 /*
921 * In this case, ia must not be NULL. We just use its prefix
922 * length.
923 */
924 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL);
925 }
926 /*
927 * If the destination address on a p2p interface is specified,
928 * and the address is a scoped one, validate/set the scope
929 * zone identifier.
930 */
931 dst6 = ifra->ifra_dstaddr;
932 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) &&
933 (dst6.sin6_family == AF_INET6)) {
934 int scopeid;
935
984263bc
MD
936 if ((error = in6_recoverscope(&dst6,
937 &ifra->ifra_dstaddr.sin6_addr,
938 ifp)) != 0)
bde3511a 939 return (error);
cb3deea5
SZ
940 if (in6_addr2zoneid(ifp, &dst6.sin6_addr, &scopeid))
941 return (EINVAL);
984263bc
MD
942 if (dst6.sin6_scope_id == 0) /* user omit to specify the ID. */
943 dst6.sin6_scope_id = scopeid;
944 else if (dst6.sin6_scope_id != scopeid)
bde3511a 945 return (EINVAL); /* scope ID mismatch. */
984263bc
MD
946 if ((error = in6_embedscope(&dst6.sin6_addr, &dst6, NULL, NULL))
947 != 0)
bde3511a 948 return (error);
984263bc 949 dst6.sin6_scope_id = 0; /* XXX */
984263bc
MD
950 }
951 /*
952 * The destination address can be specified only for a p2p or a
953 * loopback interface. If specified, the corresponding prefix length
954 * must be 128.
955 */
956 if (ifra->ifra_dstaddr.sin6_family == AF_INET6) {
bde3511a 957 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) == 0) {
984263bc
MD
958 /* XXX: noisy message */
959 log(LOG_INFO, "in6_update_ifa: a destination can be "
960 "specified for a p2p or a loopback IF only\n");
bde3511a 961 return (EINVAL);
984263bc
MD
962 }
963 if (plen != 128) {
964 /*
965 * The following message seems noisy, but we dare to
966 * add it for diagnosis.
967 */
968 log(LOG_INFO, "in6_update_ifa: prefixlen must be 128 "
969 "when dstaddr is specified\n");
bde3511a 970 return (EINVAL);
984263bc
MD
971 }
972 }
973 /* lifetime consistency check */
974 lt = &ifra->ifra_lifetime;
975 if (lt->ia6t_vltime != ND6_INFINITE_LIFETIME
cec73927 976 && lt->ia6t_vltime + time_uptime < time_uptime) {
984263bc
MD
977 return EINVAL;
978 }
979 if (lt->ia6t_vltime == 0) {
980 /*
981 * the following log might be noisy, but this is a typical
982 * configuration mistake or a tool's bug.
983 */
984 log(LOG_INFO,
985 "in6_update_ifa: valid lifetime is 0 for %s\n",
986 ip6_sprintf(&ifra->ifra_addr.sin6_addr));
987 }
988 if (lt->ia6t_pltime != ND6_INFINITE_LIFETIME
cec73927 989 && lt->ia6t_pltime + time_uptime < time_uptime) {
984263bc
MD
990 return EINVAL;
991 }
992
993 /*
994 * If this is a new address, allocate a new ifaddr and link it
995 * into chains.
996 */
997 if (ia == NULL) {
998 hostIsNew = 1;
52fbd92a 999 ia = ifa_create(sizeof(*ia));
70e5139b 1000
984263bc
MD
1001 /* Initialize the address and masks */
1002 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
1003 ia->ia_addr.sin6_family = AF_INET6;
1004 ia->ia_addr.sin6_len = sizeof(ia->ia_addr);
1005 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) {
1006 /*
1007 * XXX: some functions expect that ifa_dstaddr is not
1008 * NULL for p2p interfaces.
1009 */
1010 ia->ia_ifa.ifa_dstaddr
1011 = (struct sockaddr *)&ia->ia_dstaddr;
1012 } else {
1013 ia->ia_ifa.ifa_dstaddr = NULL;
1014 }
1015 ia->ia_ifa.ifa_netmask
1016 = (struct sockaddr *)&ia->ia_prefixmask;
1017
1018 ia->ia_ifp = ifp;
1019 if ((oia = in6_ifaddr) != NULL) {
1020 for ( ; oia->ia_next; oia = oia->ia_next)
1021 continue;
1022 oia->ia_next = ia;
1023 } else
1024 in6_ifaddr = ia;
1025
b2632176 1026 ifa_iflink(&ia->ia_ifa, ifp, 1);
984263bc
MD
1027 }
1028
1029 /* set prefix mask */
1030 if (ifra->ifra_prefixmask.sin6_len) {
1031 /*
1032 * We prohibit changing the prefix length of an existing
1033 * address, because
1034 * + such an operation should be rare in IPv6, and
1035 * + the operation would confuse prefix management.
1036 */
1037 if (ia->ia_prefixmask.sin6_len &&
1038 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) != plen) {
1039 log(LOG_INFO, "in6_update_ifa: the prefix length of an"
1040 " existing (%s) address should not be changed\n",
1041 ip6_sprintf(&ia->ia_addr.sin6_addr));
1042 error = EINVAL;
1043 goto unlink;
1044 }
1045 ia->ia_prefixmask = ifra->ifra_prefixmask;
1046 }
1047
1048 /*
1049 * If a new destination address is specified, scrub the old one and
1050 * install the new destination. Note that the interface must be
bde3511a 1051 * p2p or loopback (see the check above.)
984263bc
MD
1052 */
1053 if (dst6.sin6_family == AF_INET6 &&
1054 !IN6_ARE_ADDR_EQUAL(&dst6.sin6_addr,
1055 &ia->ia_dstaddr.sin6_addr)) {
1056 int e;
1057
bde3511a 1058 if ((ia->ia_flags & IFA_ROUTE) &&
984263bc
MD
1059 (e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST))
1060 != 0) {
1061 log(LOG_ERR, "in6_update_ifa: failed to remove "
1062 "a route to the old destination: %s\n",
1063 ip6_sprintf(&ia->ia_addr.sin6_addr));
1064 /* proceed anyway... */
1065 }
1066 else
1067 ia->ia_flags &= ~IFA_ROUTE;
1068 ia->ia_dstaddr = dst6;
1069 }
1070
5785f5fb
RM
1071 was_tentative = ia->ia6_flags & (IN6_IFF_TENTATIVE|IN6_IFF_DUPLICATED);
1072 ia->ia6_flags = ifra->ifra_flags;
1073 ia->ia6_flags &= ~IN6_IFF_DUPLICATED; /*safety*/
1074 ia->ia6_flags &= ~IN6_IFF_NODAD; /* Mobile IPv6 */
1075 if ((hostIsNew || was_tentative) &&
1076 in6if_do_dad(ifp) &&
1077 !(ifra->ifra_flags & IN6_IFF_NODAD))
1078 ia->ia6_flags |= IN6_IFF_TENTATIVE;
1079
1080 ia->ia6_lifetime = ifra->ifra_lifetime;
1081 /* for sanity */
1082 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) {
1083 ia->ia6_lifetime.ia6t_expire =
1084 time_uptime + ia->ia6_lifetime.ia6t_vltime;
1085 } else
1086 ia->ia6_lifetime.ia6t_expire = 0;
1087 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) {
1088 ia->ia6_lifetime.ia6t_preferred =
1089 time_uptime + ia->ia6_lifetime.ia6t_pltime;
1090 } else
1091 ia->ia6_lifetime.ia6t_preferred = 0;
1092
984263bc
MD
1093 /* reset the interface and routing table appropriately. */
1094 if ((error = in6_ifinit(ifp, ia, &ifra->ifra_addr, hostIsNew)) != 0)
1095 goto unlink;
1096
1097 /*
1098 * Beyond this point, we should call in6_purgeaddr upon an error,
bde3511a 1099 * not just go to unlink.
984263bc
MD
1100 */
1101
bde3511a 1102 if (ifp->if_flags & IFF_MULTICAST) {
984263bc
MD
1103 struct sockaddr_in6 mltaddr, mltmask;
1104 struct in6_multi *in6m;
1105
1106 if (hostIsNew) {
1107 /*
1108 * join solicited multicast addr for new host id
1109 */
1110 struct in6_addr llsol;
1111 bzero(&llsol, sizeof(struct in6_addr));
1112 llsol.s6_addr16[0] = htons(0xff02);
1113 llsol.s6_addr16[1] = htons(ifp->if_index);
1114 llsol.s6_addr32[1] = 0;
1115 llsol.s6_addr32[2] = htonl(1);
1116 llsol.s6_addr32[3] =
1117 ifra->ifra_addr.sin6_addr.s6_addr32[3];
1118 llsol.s6_addr8[12] = 0xff;
0bdb1448 1119 in6_addmulti(&llsol, ifp, &error);
984263bc
MD
1120 if (error != 0) {
1121 log(LOG_WARNING,
1122 "in6_update_ifa: addmulti failed for "
1123 "%s on %s (errno=%d)\n",
1124 ip6_sprintf(&llsol), if_name(ifp),
1125 error);
1126 in6_purgeaddr((struct ifaddr *)ia);
bde3511a 1127 return (error);
984263bc
MD
1128 }
1129 }
1130
1131 bzero(&mltmask, sizeof(mltmask));
1132 mltmask.sin6_len = sizeof(struct sockaddr_in6);
1133 mltmask.sin6_family = AF_INET6;
1134 mltmask.sin6_addr = in6mask32;
1135
1136 /*
1137 * join link-local all-nodes address
1138 */
1139 bzero(&mltaddr, sizeof(mltaddr));
1140 mltaddr.sin6_len = sizeof(struct sockaddr_in6);
1141 mltaddr.sin6_family = AF_INET6;
84204577 1142 mltaddr.sin6_addr = kin6addr_linklocal_allnodes;
984263bc
MD
1143 mltaddr.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
1144
72659ed0 1145 in6m = IN6_LOOKUP_MULTI(&mltaddr.sin6_addr, ifp);
984263bc 1146 if (in6m == NULL) {
ecdefdda 1147 rtrequest_global(RTM_ADD,
984263bc
MD
1148 (struct sockaddr *)&mltaddr,
1149 (struct sockaddr *)&ia->ia_addr,
1150 (struct sockaddr *)&mltmask,
ecdefdda 1151 RTF_UP|RTF_CLONING); /* xxx */
0bdb1448 1152 in6_addmulti(&mltaddr.sin6_addr, ifp, &error);
984263bc
MD
1153 if (error != 0) {
1154 log(LOG_WARNING,
1155 "in6_update_ifa: addmulti failed for "
1156 "%s on %s (errno=%d)\n",
bde3511a 1157 ip6_sprintf(&mltaddr.sin6_addr),
984263bc
MD
1158 if_name(ifp), error);
1159 }
1160 }
1161
1162 /*
1163 * join node information group address
1164 */
1165#define hostnamelen strlen(hostname)
1166 if (in6_nigroup(ifp, hostname, hostnamelen, &mltaddr.sin6_addr)
1167 == 0) {
72659ed0 1168 in6m = IN6_LOOKUP_MULTI(&mltaddr.sin6_addr, ifp);
984263bc 1169 if (in6m == NULL && ia != NULL) {
0bdb1448 1170 in6_addmulti(&mltaddr.sin6_addr, ifp, &error);
984263bc
MD
1171 if (error != 0) {
1172 log(LOG_WARNING, "in6_update_ifa: "
1173 "addmulti failed for "
1174 "%s on %s (errno=%d)\n",
bde3511a 1175 ip6_sprintf(&mltaddr.sin6_addr),
984263bc
MD
1176 if_name(ifp), error);
1177 }
1178 }
1179 }
1180#undef hostnamelen
1181
1182 /*
1183 * join node-local all-nodes address, on loopback.
1184 * XXX: since "node-local" is obsoleted by interface-local,
1185 * we have to join the group on every interface with
1186 * some interface-boundary restriction.
1187 */
1188 if (ifp->if_flags & IFF_LOOPBACK) {
1189 struct in6_ifaddr *ia_loop;
1190
84204577 1191 struct in6_addr loop6 = kin6addr_loopback;
984263bc
MD
1192 ia_loop = in6ifa_ifpwithaddr(ifp, &loop6);
1193
84204577 1194 mltaddr.sin6_addr = kin6addr_nodelocal_allnodes;
984263bc 1195
72659ed0 1196 in6m = IN6_LOOKUP_MULTI(&mltaddr.sin6_addr, ifp);
984263bc 1197 if (in6m == NULL && ia_loop != NULL) {
ecdefdda 1198 rtrequest_global(RTM_ADD,
984263bc
MD
1199 (struct sockaddr *)&mltaddr,
1200 (struct sockaddr *)&ia_loop->ia_addr,
1201 (struct sockaddr *)&mltmask,
ecdefdda 1202 RTF_UP);
0bdb1448 1203 in6_addmulti(&mltaddr.sin6_addr, ifp, &error);
984263bc
MD
1204 if (error != 0) {
1205 log(LOG_WARNING, "in6_update_ifa: "
1206 "addmulti failed for %s on %s "
1207 "(errno=%d)\n",
bde3511a 1208 ip6_sprintf(&mltaddr.sin6_addr),
984263bc
MD
1209 if_name(ifp), error);
1210 }
1211 }
1212 }
1213 }
1214
984263bc
MD
1215 /*
1216 * Perform DAD, if needed.
1217 * XXX It may be of use, if we can administratively
1218 * disable DAD.
1219 */
5785f5fb
RM
1220 if (in6if_do_dad(ifp) &&
1221 !(ifra->ifra_flags & IN6_IFF_NODAD) &&
1222 ia->ia6_flags & IN6_IFF_TENTATIVE)
984263bc 1223 nd6_dad_start((struct ifaddr *)ia, NULL);
984263bc 1224
bde3511a 1225 return (error);
984263bc 1226
bde3511a 1227unlink:
984263bc
MD
1228 /*
1229 * XXX: if a change of an existing address failed, keep the entry
1230 * anyway.
1231 */
1232 if (hostIsNew)
1233 in6_unlink_ifa(ia, ifp);
bde3511a 1234 return (error);
984263bc
MD
1235}
1236
1237void
122ebd49 1238in6_purgeaddr(struct ifaddr *ifa)
984263bc
MD
1239{
1240 struct ifnet *ifp = ifa->ifa_ifp;
1241 struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa;
1242
1243 /* stop DAD processing */
1244 nd6_dad_stop(ifa);
1245
1246 /*
1247 * delete route to the destination of the address being purged.
1248 * The interface must be p2p or loopback in this case.
1249 */
bde3511a 1250 if ((ia->ia_flags & IFA_ROUTE) && ia->ia_dstaddr.sin6_len != 0) {
984263bc
MD
1251 int e;
1252
1253 if ((e = rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST))
1254 != 0) {
1255 log(LOG_ERR, "in6_purgeaddr: failed to remove "
1256 "a route to the p2p destination: %s on %s, "
1257 "errno=%d\n",
1258 ip6_sprintf(&ia->ia_addr.sin6_addr), if_name(ifp),
1259 e);
1260 /* proceed anyway... */
1261 }
1262 else
1263 ia->ia_flags &= ~IFA_ROUTE;
1264 }
1265
1266 /* Remove ownaddr's loopback rtentry, if it exists. */
1267 in6_ifremloop(&(ia->ia_ifa));
1268
1269 if (ifp->if_flags & IFF_MULTICAST) {
1270 /*
1271 * delete solicited multicast addr for deleting host id
1272 */
1273 struct in6_multi *in6m;
1274 struct in6_addr llsol;
1275 bzero(&llsol, sizeof(struct in6_addr));
1276 llsol.s6_addr16[0] = htons(0xff02);
1277 llsol.s6_addr16[1] = htons(ifp->if_index);
1278 llsol.s6_addr32[1] = 0;
1279 llsol.s6_addr32[2] = htonl(1);
1280 llsol.s6_addr32[3] =
1281 ia->ia_addr.sin6_addr.s6_addr32[3];
1282 llsol.s6_addr8[12] = 0xff;
1283
72659ed0 1284 in6m = IN6_LOOKUP_MULTI(&llsol, ifp);
984263bc
MD
1285 if (in6m)
1286 in6_delmulti(in6m);
1287 }
1288
1289 in6_unlink_ifa(ia, ifp);
1290}
1291
1292static void
122ebd49 1293in6_unlink_ifa(struct in6_ifaddr *ia, struct ifnet *ifp)
984263bc 1294{
984263bc 1295 struct in6_ifaddr *oia;
abf782b3
EN
1296
1297 crit_enter();
984263bc 1298
b2632176 1299 ifa_ifunlink(&ia->ia_ifa, ifp);
984263bc
MD
1300
1301 oia = ia;
1302 if (oia == (ia = in6_ifaddr))
1303 in6_ifaddr = ia->ia_next;
1304 else {
1305 while (ia->ia_next && (ia->ia_next != oia))
1306 ia = ia->ia_next;
1307 if (ia->ia_next)
1308 ia->ia_next = oia->ia_next;
1309 else {
1310 /* search failed */
a6ec04bc 1311 kprintf("Couldn't unlink in6_ifaddr from in6_ifaddr\n");
984263bc
MD
1312 }
1313 }
1314
984263bc
MD
1315 /*
1316 * When an autoconfigured address is being removed, release the
1317 * reference to the base prefix. Also, since the release might
1318 * affect the status of other (detached) addresses, call
1319 * pfxlist_onlink_check().
1320 */
bde3511a 1321 if (oia->ia6_flags & IN6_IFF_AUTOCONF) {
984263bc
MD
1322 if (oia->ia6_ndpr == NULL) {
1323 log(LOG_NOTICE, "in6_unlink_ifa: autoconf'ed address "
1324 "%p has no prefix\n", oia);
1325 } else {
1326 oia->ia6_ndpr->ndpr_refcnt--;
1327 oia->ia6_flags &= ~IN6_IFF_AUTOCONF;
1328 oia->ia6_ndpr = NULL;
1329 }
1330
1331 pfxlist_onlink_check();
1332 }
1333
1334 /*
1335 * release another refcnt for the link from in6_ifaddr.
1336 * Note that we should decrement the refcnt at least once for all *BSD.
1337 */
b2632176 1338 ifa_destroy(&oia->ia_ifa);
984263bc 1339
abf782b3 1340 crit_exit();
984263bc
MD
1341}
1342
1343void
122ebd49 1344in6_purgeif(struct ifnet *ifp)
984263bc 1345{
b2632176 1346 struct ifaddr_container *ifac, *next;
984263bc 1347
b2632176
SZ
1348 TAILQ_FOREACH_MUTABLE(ifac, &ifp->if_addrheads[mycpuid],
1349 ifa_link, next) {
1350 if (ifac->ifa->ifa_addr->sa_family != AF_INET6)
984263bc 1351 continue;
b2632176 1352 in6_purgeaddr(ifac->ifa);
984263bc
MD
1353 }
1354
1355 in6_ifdetach(ifp);
1356}
1357
1358/*
1359 * SIOC[GAD]LIFADDR.
1360 * SIOCGLIFADDR: get first address. (?)
1361 * SIOCGLIFADDR with IFLR_PREFIX:
1362 * get first address that matches the specified prefix.
1363 * SIOCALIFADDR: add the specified address.
1364 * SIOCALIFADDR with IFLR_PREFIX:
1365 * add the specified prefix, filling hostid part from
1366 * the first link-local address. prefixlen must be <= 64.
1367 * SIOCDLIFADDR: delete the specified address.
1368 * SIOCDLIFADDR with IFLR_PREFIX:
1369 * delete the first address that matches the specified prefix.
1370 * return values:
1371 * EINVAL on invalid parameters
1372 * EADDRNOTAVAIL on prefix match failed/specified address not found
1373 * other values may be returned from in6_ioctl()
1374 *
1375 * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64.
1376 * this is to accomodate address naming scheme other than RFC2374,
1377 * in the future.
1378 * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374
1379 * address encoding scheme. (see figure on page 8)
1380 */
1381static int
2501b0ea
SZ
1382in6_lifaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp,
1383 struct thread *td)
984263bc
MD
1384{
1385 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
984263bc
MD
1386 struct sockaddr *sa;
1387
1388 /* sanity checks */
1389 if (!data || !ifp) {
1390 panic("invalid argument to in6_lifaddr_ioctl");
1391 /*NOTRECHED*/
1392 }
1393
1394 switch (cmd) {
1395 case SIOCGLIFADDR:
1396 /* address must be specified on GET with IFLR_PREFIX */
bde3511a 1397 if (!(iflr->flags & IFLR_PREFIX))
984263bc
MD
1398 break;
1399 /* FALLTHROUGH */
1400 case SIOCALIFADDR:
1401 case SIOCDLIFADDR:
1402 /* address must be specified on ADD and DELETE */
1403 sa = (struct sockaddr *)&iflr->addr;
1404 if (sa->sa_family != AF_INET6)
1405 return EINVAL;
1406 if (sa->sa_len != sizeof(struct sockaddr_in6))
1407 return EINVAL;
1408 /* XXX need improvement */
1409 sa = (struct sockaddr *)&iflr->dstaddr;
1410 if (sa->sa_family && sa->sa_family != AF_INET6)
1411 return EINVAL;
1412 if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6))
1413 return EINVAL;
1414 break;
1415 default: /* shouldn't happen */
1416#if 0
1417 panic("invalid cmd to in6_lifaddr_ioctl");
1418 /* NOTREACHED */
1419#else
1420 return EOPNOTSUPP;
1421#endif
1422 }
1423 if (sizeof(struct in6_addr) * 8 < iflr->prefixlen)
1424 return EINVAL;
1425
1426 switch (cmd) {
1427 case SIOCALIFADDR:
1428 {
1429 struct in6_aliasreq ifra;
1430 struct in6_addr *hostid = NULL;
1431 int prefixlen;
1432
bde3511a 1433 if (iflr->flags & IFLR_PREFIX) {
b2632176 1434 struct ifaddr *ifa;
984263bc
MD
1435 struct sockaddr_in6 *sin6;
1436
1437 /*
1438 * hostid is to fill in the hostid part of the
1439 * address. hostid points to the first link-local
1440 * address attached to the interface.
1441 */
1442 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0);
1443 if (!ifa)
1444 return EADDRNOTAVAIL;
1445 hostid = IFA_IN6(ifa);
1446
1447 /* prefixlen must be <= 64. */
1448 if (64 < iflr->prefixlen)
1449 return EINVAL;
1450 prefixlen = iflr->prefixlen;
1451
1452 /* hostid part must be zero. */
1453 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1454 if (sin6->sin6_addr.s6_addr32[2] != 0
1455 || sin6->sin6_addr.s6_addr32[3] != 0) {
1456 return EINVAL;
1457 }
1458 } else
1459 prefixlen = iflr->prefixlen;
1460
1461 /* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
1462 bzero(&ifra, sizeof(ifra));
1463 bcopy(iflr->iflr_name, ifra.ifra_name,
1464 sizeof(ifra.ifra_name));
1465
1466 bcopy(&iflr->addr, &ifra.ifra_addr,
1467 ((struct sockaddr *)&iflr->addr)->sa_len);
1468 if (hostid) {
1469 /* fill in hostid part */
1470 ifra.ifra_addr.sin6_addr.s6_addr32[2] =
1471 hostid->s6_addr32[2];
1472 ifra.ifra_addr.sin6_addr.s6_addr32[3] =
1473 hostid->s6_addr32[3];
1474 }
1475
1476 if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /*XXX*/
1477 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
1478 ((struct sockaddr *)&iflr->dstaddr)->sa_len);
1479 if (hostid) {
1480 ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] =
1481 hostid->s6_addr32[2];
1482 ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] =
1483 hostid->s6_addr32[3];
1484 }
1485 }
1486
1487 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
b51cd13b 1488 in6_prefixlen2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen);
984263bc
MD
1489
1490 ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX;
2501b0ea
SZ
1491 return in6_control_internal(SIOCAIFADDR_IN6, (caddr_t)&ifra,
1492 ifp, td);
984263bc
MD
1493 }
1494 case SIOCGLIFADDR:
1495 case SIOCDLIFADDR:
1496 {
b2632176 1497 struct ifaddr_container *ifac;
984263bc
MD
1498 struct in6_ifaddr *ia;
1499 struct in6_addr mask, candidate, match;
1500 struct sockaddr_in6 *sin6;
1501 int cmp;
1502
1503 bzero(&mask, sizeof(mask));
1504 if (iflr->flags & IFLR_PREFIX) {
1505 /* lookup a prefix rather than address. */
b51cd13b 1506 in6_prefixlen2mask(&mask, iflr->prefixlen);
984263bc
MD
1507
1508 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1509 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1510 match.s6_addr32[0] &= mask.s6_addr32[0];
1511 match.s6_addr32[1] &= mask.s6_addr32[1];
1512 match.s6_addr32[2] &= mask.s6_addr32[2];
1513 match.s6_addr32[3] &= mask.s6_addr32[3];
1514
1515 /* if you set extra bits, that's wrong */
1516 if (bcmp(&match, &sin6->sin6_addr, sizeof(match)))
1517 return EINVAL;
1518
1519 cmp = 1;
1520 } else {
1521 if (cmd == SIOCGLIFADDR) {
1522 /* on getting an address, take the 1st match */
1523 cmp = 0; /* XXX */
1524 } else {
1525 /* on deleting an address, do exact match */
b51cd13b 1526 in6_prefixlen2mask(&mask, 128);
984263bc
MD
1527 sin6 = (struct sockaddr_in6 *)&iflr->addr;
1528 bcopy(&sin6->sin6_addr, &match, sizeof(match));
1529
1530 cmp = 1;
1531 }
1532 }
1533
b2632176
SZ
1534 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1535 struct ifaddr *ifa = ifac->ifa;
1536
984263bc
MD
1537 if (ifa->ifa_addr->sa_family != AF_INET6)
1538 continue;
1539 if (!cmp)
1540 break;
1541
1542 bcopy(IFA_IN6(ifa), &candidate, sizeof(candidate));
984263bc
MD
1543 /*
1544 * XXX: this is adhoc, but is necessary to allow
1545 * a user to specify fe80::/64 (not /10) for a
1546 * link-local address.
1547 */
1548 if (IN6_IS_ADDR_LINKLOCAL(&candidate))
1549 candidate.s6_addr16[1] = 0;
984263bc
MD
1550 candidate.s6_addr32[0] &= mask.s6_addr32[0];
1551 candidate.s6_addr32[1] &= mask.s6_addr32[1];
1552 candidate.s6_addr32[2] &= mask.s6_addr32[2];
1553 candidate.s6_addr32[3] &= mask.s6_addr32[3];
1554 if (IN6_ARE_ADDR_EQUAL(&candidate, &match))
1555 break;
1556 }
b2632176 1557 if (ifac == NULL)
984263bc 1558 return EADDRNOTAVAIL;
b2632176 1559 ia = ifa2ia6(ifac->ifa);
984263bc
MD
1560
1561 if (cmd == SIOCGLIFADDR) {
984263bc 1562 struct sockaddr_in6 *s6;
984263bc
MD
1563
1564 /* fill in the if_laddrreq structure */
1565 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin6_len);
984263bc
MD
1566 s6 = (struct sockaddr_in6 *)&iflr->addr;
1567 if (IN6_IS_ADDR_LINKLOCAL(&s6->sin6_addr)) {
1568 s6->sin6_addr.s6_addr16[1] = 0;
cb3deea5
SZ
1569 if (in6_addr2zoneid(ifp, &s6->sin6_addr,
1570 &s6->sin6_scope_id))
1571 return (EINVAL);/* XXX */
984263bc 1572 }
bde3511a 1573 if (ifp->if_flags & IFF_POINTOPOINT) {
984263bc
MD
1574 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
1575 ia->ia_dstaddr.sin6_len);
984263bc
MD
1576 s6 = (struct sockaddr_in6 *)&iflr->dstaddr;
1577 if (IN6_IS_ADDR_LINKLOCAL(&s6->sin6_addr)) {
1578 s6->sin6_addr.s6_addr16[1] = 0;
cb3deea5
SZ
1579 if (in6_addr2zoneid(ifp,
1580 &s6->sin6_addr, &s6->sin6_scope_id))
1581 return (EINVAL); /* EINVAL */
984263bc 1582 }
984263bc
MD
1583 } else
1584 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
1585
1586 iflr->prefixlen =
1587 in6_mask2len(&ia->ia_prefixmask.sin6_addr,
1588 NULL);
1589
1590 iflr->flags = ia->ia6_flags; /* XXX */
1591
1592 return 0;
1593 } else {
1594 struct in6_aliasreq ifra;
1595
1596 /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
1597 bzero(&ifra, sizeof(ifra));
1598 bcopy(iflr->iflr_name, ifra.ifra_name,
bde3511a 1599 sizeof(ifra.ifra_name));
984263bc
MD
1600
1601 bcopy(&ia->ia_addr, &ifra.ifra_addr,
bde3511a
JH
1602 ia->ia_addr.sin6_len);
1603 if (ifp->if_flags & IFF_POINTOPOINT)
984263bc 1604 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
bde3511a
JH
1605 ia->ia_dstaddr.sin6_len);
1606 else
984263bc 1607 bzero(&ifra.ifra_dstaddr,
bde3511a 1608 sizeof(ifra.ifra_dstaddr));
984263bc 1609 bcopy(&ia->ia_prefixmask, &ifra.ifra_dstaddr,
bde3511a 1610 ia->ia_prefixmask.sin6_len);
984263bc
MD
1611
1612 ifra.ifra_flags = ia->ia6_flags;
2501b0ea
SZ
1613 return in6_control_internal(SIOCDIFADDR_IN6,
1614 (caddr_t)&ifra, ifp, td);
984263bc
MD
1615 }
1616 }
1617 }
1618
1619 return EOPNOTSUPP; /* just for safety */
1620}
1621
1622/*
1623 * Initialize an interface's intetnet6 address
1624 * and routing table entry.
1625 */
1626static int
122ebd49
CP
1627in6_ifinit(struct ifnet *ifp, struct in6_ifaddr *ia, struct sockaddr_in6 *sin6,
1628 int newhost)
984263bc 1629{
f88a4ba1 1630 int error = 0, plen;
b2632176 1631
984263bc
MD
1632 ia->ia_addr = *sin6;
1633
f88a4ba1
SZ
1634 if (ifp->if_ioctl != NULL) {
1635 ifnet_serialize_all(ifp);
1636 error = ifp->if_ioctl(ifp, SIOCSIFADDR, (caddr_t)ia, NULL);
a3dd34d2 1637 ifnet_deserialize_all(ifp);
f88a4ba1
SZ
1638 if (error)
1639 return (error);
984263bc 1640 }
a3dd34d2 1641
984263bc
MD
1642 ia->ia_ifa.ifa_metric = ifp->if_metric;
1643
1644 /* we could do in(6)_socktrim here, but just omit it at this moment. */
1645
1646 /*
1647 * Special case:
1648 * If the destination address is specified for a point-to-point
1649 * interface, install a route to the destination as an interface
1650 * direct route.
1651 */
1652 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */
1653 if (plen == 128 && ia->ia_dstaddr.sin6_family == AF_INET6) {
1654 if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD,
1655 RTF_UP | RTF_HOST)) != 0)
bde3511a 1656 return (error);
984263bc
MD
1657 ia->ia_flags |= IFA_ROUTE;
1658 }
1659 if (plen < 128) {
1660 /*
1661 * The RTF_CLONING flag is necessary for in6_is_ifloop_auto().
1662 */
1663 ia->ia_ifa.ifa_flags |= RTF_CLONING;
1664 }
1665
1666 /* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */
1667 if (newhost) {
1668 /* set the rtrequest function to create llinfo */
1669 ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
1670 in6_ifaddloop(&(ia->ia_ifa));
1671 }
1672
bde3511a 1673 return (error);
984263bc
MD
1674}
1675
0d16ba1d 1676struct in6_multi_mship *
773330af 1677in6_joingroup(struct ifnet *ifp, struct in6_addr *addr, int *errorp)
0d16ba1d
MD
1678{
1679 struct in6_multi_mship *imm;
1680
1681 imm = kmalloc(sizeof(*imm), M_IPMADDR, M_NOWAIT);
1682 if (!imm) {
1683 *errorp = ENOBUFS;
1684 return NULL;
1685 }
1686 imm->i6mm_maddr = in6_addmulti(addr, ifp, errorp);
1687 if (!imm->i6mm_maddr) {
1688 /* *errorp is alrady set */
1689 kfree(imm, M_IPMADDR);
1690 return NULL;
1691 }
1692 return imm;
1693}
1694
1695int
773330af 1696in6_leavegroup(struct in6_multi_mship *imm)
0d16ba1d
MD
1697{
1698
1699 if (imm->i6mm_maddr)
1700 in6_delmulti(imm->i6mm_maddr);
1701 kfree(imm, M_IPMADDR);
1702 return 0;
1703}
1704
984263bc
MD
1705/*
1706 * Add an address to the list of IP6 multicast addresses for a
1707 * given interface.
1708 */
1709struct in6_multi *
122ebd49 1710in6_addmulti(struct in6_addr *maddr6, struct ifnet *ifp, int *errorp)
984263bc
MD
1711{
1712 struct in6_multi *in6m;
1713 struct sockaddr_in6 sin6;
1714 struct ifmultiaddr *ifma;
984263bc
MD
1715
1716 *errorp = 0;
1717
abf782b3
EN
1718 crit_enter();
1719
984263bc
MD
1720 /*
1721 * Call generic routine to add membership or increment
1722 * refcount. It wants addresses in the form of a sockaddr,
1723 * so we build one here (being careful to zero the unused bytes).
1724 */
1725 bzero(&sin6, sizeof sin6);
1726 sin6.sin6_family = AF_INET6;
1727 sin6.sin6_len = sizeof sin6;
1728 sin6.sin6_addr = *maddr6;
1729 *errorp = if_addmulti(ifp, (struct sockaddr *)&sin6, &ifma);
1730 if (*errorp) {
abf782b3 1731 crit_exit();
984263bc
MD
1732 return 0;
1733 }
1734
1735 /*
1736 * If ifma->ifma_protospec is null, then if_addmulti() created
1737 * a new record. Otherwise, we are done.
1738 */
d8061892 1739 if (ifma->ifma_protospec != NULL) {
edbd2e19 1740 crit_exit();
984263bc 1741 return ifma->ifma_protospec;
edbd2e19 1742 }
984263bc 1743
c1e12ca9 1744 in6m = kmalloc(sizeof(*in6m), M_IPMADDR, M_INTWAIT | M_ZERO);
984263bc
MD
1745 in6m->in6m_addr = *maddr6;
1746 in6m->in6m_ifp = ifp;
1747 in6m->in6m_ifma = ifma;
1748 ifma->ifma_protospec = in6m;
1749 LIST_INSERT_HEAD(&in6_multihead, in6m, in6m_entry);
1750
1751 /*
1752 * Let MLD6 know that we have joined a new IP6 multicast
1753 * group.
1754 */
1755 mld6_start_listening(in6m);
abf782b3 1756 crit_exit();
bde3511a 1757 return (in6m);
984263bc
MD
1758}
1759
1760/*
1761 * Delete a multicast address record.
1762 */
1763void
122ebd49 1764in6_delmulti(struct in6_multi *in6m)
984263bc
MD
1765{
1766 struct ifmultiaddr *ifma = in6m->in6m_ifma;
abf782b3
EN
1767
1768 crit_enter();
984263bc
MD
1769
1770 if (ifma->ifma_refcount == 1) {
1771 /*
1772 * No remaining claims to this record; let MLD6 know
1773 * that we are leaving the multicast group.
1774 */
1775 mld6_stop_listening(in6m);
d8061892 1776 ifma->ifma_protospec = NULL;
984263bc 1777 LIST_REMOVE(in6m, in6m_entry);
efda3bd0 1778 kfree(in6m, M_IPMADDR);
984263bc
MD
1779 }
1780 /* XXX - should be separate API for when we have an ifma? */
1781 if_delmulti(ifma->ifma_ifp, ifma->ifma_addr);
abf782b3 1782 crit_exit();
984263bc
MD
1783}
1784
1785/*
1786 * Find an IPv6 interface link-local address specific to an interface.
1787 */
1788struct in6_ifaddr *
122ebd49 1789in6ifa_ifpforlinklocal(struct ifnet *ifp, int ignoreflags)
984263bc 1790{
f71077cb 1791 const struct ifaddr_container *ifac;
b2632176
SZ
1792
1793 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1794 struct ifaddr *ifa = ifac->ifa;
984263bc 1795
984263bc
MD
1796 if (ifa->ifa_addr == NULL)
1797 continue; /* just for safety */
1798 if (ifa->ifa_addr->sa_family != AF_INET6)
1799 continue;
1800 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) {
1801 if ((((struct in6_ifaddr *)ifa)->ia6_flags &
1802 ignoreflags) != 0)
1803 continue;
f71077cb 1804 return (struct in6_ifaddr *)ifa;
984263bc
MD
1805 }
1806 }
f71077cb 1807 return NULL;
984263bc
MD
1808}
1809
1810
1811/*
1812 * find the internet address corresponding to a given interface and address.
1813 */
1814struct in6_ifaddr *
122ebd49 1815in6ifa_ifpwithaddr(struct ifnet *ifp, struct in6_addr *addr)
984263bc 1816{
c6d39ccf 1817 const struct ifaddr_container *ifac;
b2632176
SZ
1818
1819 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1820 struct ifaddr *ifa = ifac->ifa;
984263bc 1821
984263bc
MD
1822 if (ifa->ifa_addr == NULL)
1823 continue; /* just for safety */
1824 if (ifa->ifa_addr->sa_family != AF_INET6)
1825 continue;
1826 if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa)))
c6d39ccf 1827 return (struct in6_ifaddr *)ifa;
984263bc 1828 }
c6d39ccf 1829 return NULL;
50b1e235
MP
1830}
1831
1832/*
1833 * Find a link-local scoped address on ifp and return it if any.
1834 */
1835struct in6_ifaddr *
1836in6ifa_llaonifp(struct ifnet *ifp)
1837{
f71077cb 1838 const struct ifaddr_container *ifac;
50b1e235 1839
f71077cb
SZ
1840 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1841 const struct sockaddr_in6 *sin6;
50b1e235
MP
1842 struct ifaddr *ifa = ifac->ifa;
1843
f71077cb
SZ
1844 if (ifa->ifa_addr->sa_family != AF_INET6)
1845 continue;
1846 sin6 = (const struct sockaddr_in6 *)ifa->ifa_addr;
1847 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr) ||
1848 /* XXX why are mcast addresses ifp address list? */
1849 IN6_IS_ADDR_MC_INTFACELOCAL(&sin6->sin6_addr) ||
1850 IN6_IS_ADDR_MC_NODELOCAL(&sin6->sin6_addr))
1851 return (struct in6_ifaddr *)ifa;
1852 }
1853 return NULL;
984263bc
MD
1854}
1855
100c1c44
HT
1856/*
1857 * find the internet address on a given interface corresponding to a neighbor's
1858 * address.
1859 */
1860struct in6_ifaddr *
1861in6ifa_ifplocaladdr(const struct ifnet *ifp, const struct in6_addr *addr)
1862{
1863 struct ifaddr *ifa;
1864 struct in6_ifaddr *ia;
1865 struct ifaddr_container *ifac;
1866
1867 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1868 ifa = ifac->ifa;
1869
1870 if (ifa->ifa_addr == NULL)
1871 continue; /* just for safety */
1872 if (ifa->ifa_addr->sa_family != AF_INET6)
1873 continue;
1874 ia = (struct in6_ifaddr *)ifa;
1875 if (IN6_ARE_MASKED_ADDR_EQUAL(addr,
1876 &ia->ia_addr.sin6_addr,
1877 &ia->ia_prefixmask.sin6_addr))
1878 return ia;
1879 }
1880
1881 return NULL;
1882}
1883
984263bc
MD
1884/*
1885 * Convert IP6 address to printable (loggable) representation.
1886 */
1887static char digits[] = "0123456789abcdef";
1888static int ip6round = 0;
1889char *
122ebd49 1890ip6_sprintf(const struct in6_addr *addr)
984263bc
MD
1891{
1892 static char ip6buf[8][48];
1893 int i;
1894 char *cp;
ab1c1714
MD
1895 const u_short *a = (const u_short *)addr;
1896 const u_char *d;
984263bc
MD
1897 int dcolon = 0;
1898
1899 ip6round = (ip6round + 1) & 7;
1900 cp = ip6buf[ip6round];
1901
1902 for (i = 0; i < 8; i++) {
1903 if (dcolon == 1) {
1904 if (*a == 0) {
1905 if (i == 7)
1906 *cp++ = ':';
1907 a++;
1908 continue;
1909 } else
1910 dcolon = 2;
1911 }
1912 if (*a == 0) {
1913 if (dcolon == 0 && *(a + 1) == 0) {
1914 if (i == 0)
1915 *cp++ = ':';
1916 *cp++ = ':';
1917 dcolon = 1;
1918 } else {
1919 *cp++ = '0';
1920 *cp++ = ':';
1921 }
1922 a++;
1923 continue;
1924 }
ab1c1714 1925 d = (const u_char *)a;
984263bc
MD
1926 *cp++ = digits[*d >> 4];
1927 *cp++ = digits[*d++ & 0xf];
1928 *cp++ = digits[*d >> 4];
1929 *cp++ = digits[*d & 0xf];
1930 *cp++ = ':';
1931 a++;
1932 }
1933 *--cp = 0;
bde3511a 1934 return (ip6buf[ip6round]);
984263bc
MD
1935}
1936
1937int
122ebd49 1938in6_localaddr(struct in6_addr *in6)
984263bc
MD
1939{
1940 struct in6_ifaddr *ia;
1941
1942 if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6))
1943 return 1;
1944
1945 for (ia = in6_ifaddr; ia; ia = ia->ia_next)
1946 if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr,
1947 &ia->ia_prefixmask.sin6_addr))
1948 return 1;
1949
1950 return (0);
1951}
1952
1953int
122ebd49 1954in6_is_addr_deprecated(struct sockaddr_in6 *sa6)
984263bc
MD
1955{
1956 struct in6_ifaddr *ia;
1957
1958 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
1959 if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr,
1960 &sa6->sin6_addr) &&
bde3511a
JH
1961 (ia->ia6_flags & IN6_IFF_DEPRECATED))
1962 return (1); /* true */
984263bc
MD
1963
1964 /* XXX: do we still have to go thru the rest of the list? */
1965 }
1966
bde3511a 1967 return (0); /* false */
984263bc
MD
1968}
1969
1970/*
1971 * return length of part which dst and src are equal
1972 * hard coding...
1973 */
1974int
122ebd49 1975in6_matchlen(struct in6_addr *src, struct in6_addr *dst)
984263bc
MD
1976{
1977 int match = 0;
1978 u_char *s = (u_char *)src, *d = (u_char *)dst;
1979 u_char *lim = s + 16, r;
1980
1981 while (s < lim)
1982 if ((r = (*d++ ^ *s++)) != 0) {
1983 while (r < 128) {
1984 match++;
1985 r <<= 1;
1986 }
1987 break;
1988 } else
1989 match += 8;
1990 return match;
1991}
1992
1993/* XXX: to be scope conscious */
1994int
122ebd49 1995in6_are_prefix_equal(struct in6_addr *p1, struct in6_addr *p2, int len)
984263bc
MD
1996{
1997 int bytelen, bitlen;
1998
1999 /* sanity check */
2000 if (0 > len || len > 128) {
2001 log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n",
2002 len);
bde3511a 2003 return (0);
984263bc
MD
2004 }
2005
2006 bytelen = len / 8;
2007 bitlen = len % 8;
2008
2009 if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen))
bde3511a 2010 return (0);
984263bc
MD
2011 if (p1->s6_addr[bytelen] >> (8 - bitlen) !=
2012 p2->s6_addr[bytelen] >> (8 - bitlen))
bde3511a 2013 return (0);
984263bc 2014
bde3511a 2015 return (1);
984263bc
MD
2016}
2017
2018void
122ebd49 2019in6_prefixlen2mask(struct in6_addr *maskp, int len)
984263bc
MD
2020{
2021 u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
2022 int bytelen, bitlen, i;
2023
2024 /* sanity check */
2025 if (0 > len || len > 128) {
2026 log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n",
2027 len);
2028 return;
2029 }
2030
2031 bzero(maskp, sizeof(*maskp));
2032 bytelen = len / 8;
2033 bitlen = len % 8;
2034 for (i = 0; i < bytelen; i++)
2035 maskp->s6_addr[i] = 0xff;
2036 if (bitlen)
2037 maskp->s6_addr[bytelen] = maskarray[bitlen - 1];
2038}
2039
2040/*
2041 * return the best address out of the same scope
2042 */
2043struct in6_ifaddr *
cfd79120 2044in6_ifawithscope(struct ifnet *oifp, struct in6_addr *dst, struct ucred *cred)
984263bc
MD
2045{
2046 int dst_scope = in6_addrscope(dst), src_scope, best_scope = 0;
2047 int blen = -1;
984263bc 2048 struct in6_ifaddr *ifa_best = NULL;
cb3deea5 2049 u_int32_t dstzone, odstzone;
bdd66735 2050 int jailed = 0;
b4051e25
SZ
2051 const struct ifnet_array *arr;
2052 int i;
bdd66735
MP
2053
2054 if(cred && cred->cr_prison)
2055 jailed = 1;
bde3511a 2056
cb3deea5
SZ
2057 if (oifp == NULL)
2058 return (NULL);
2059
2060 if (in6_addr2zoneid(oifp, dst, &odstzone))
bde3511a 2061 return (NULL);
984263bc
MD
2062
2063 /*
2064 * We search for all addresses on all interfaces from the beginning.
2065 * Comparing an interface with the outgoing interface will be done
2066 * only at the final stage of tiebreaking.
2067 */
b4051e25
SZ
2068 arr = ifnet_array_get();
2069 for (i = 0; i < arr->ifnet_count; ++i) {
2070 struct ifnet *ifp = arr->ifnet_arr[i];
b2632176
SZ
2071 struct ifaddr_container *ifac;
2072
984263bc
MD
2073 /*
2074 * We can never take an address that breaks the scope zone
2075 * of the destination.
2076 */
8e13abe7
MD
2077 if (ifp->if_afdata[AF_INET6] == NULL)
2078 continue;
cb3deea5 2079 if (in6_addr2zoneid(ifp, dst, &dstzone) || dstzone != odstzone)
984263bc
MD
2080 continue;
2081
b2632176 2082 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
984263bc 2083 int tlen = -1, dscopecmp, bscopecmp, matchcmp;
b2632176 2084 struct ifaddr *ifa = ifac->ifa;
984263bc
MD
2085
2086 if (ifa->ifa_addr->sa_family != AF_INET6)
2087 continue;
2088
2089 src_scope = in6_addrscope(IFA_IN6(ifa));
2090
2091 /*
2092 * Don't use an address before completing DAD
2093 * nor a duplicated address.
2094 */
2095 if (((struct in6_ifaddr *)ifa)->ia6_flags &
2096 IN6_IFF_NOTREADY)
2097 continue;
2098
2099 /* XXX: is there any case to allow anycasts? */
2100 if (((struct in6_ifaddr *)ifa)->ia6_flags &
2101 IN6_IFF_ANYCAST)
2102 continue;
2103
2104 if (((struct in6_ifaddr *)ifa)->ia6_flags &
2105 IN6_IFF_DETACHED)
2106 continue;
2107
cfd79120 2108 /* Skip adresses not valid for current jail */
bdd66735 2109 if (jailed &&
eecf6c3c 2110 !(jailed_ip(cred->cr_prison, (struct sockaddr *)(ifa->ifa_addr)) != 0))
cfd79120
MP
2111 continue;
2112
984263bc
MD
2113 /*
2114 * If this is the first address we find,
2115 * keep it anyway.
2116 */
2117 if (ifa_best == NULL)
2118 goto replace;
2119
2120 /*
2121 * ifa_best is never NULL beyond this line except
2122 * within the block labeled "replace".
2123 */
2124
2125 /*
2126 * If ifa_best has a smaller scope than dst and
2127 * the current address has a larger one than
2128 * (or equal to) dst, always replace ifa_best.
2129 * Also, if the current address has a smaller scope
2130 * than dst, ignore it unless ifa_best also has a
2131 * smaller scope.
2132 * Consequently, after the two if-clause below,
2133 * the followings must be satisfied:
2134 * (scope(src) < scope(dst) &&
2135 * scope(best) < scope(dst))
2136 * OR
2137 * (scope(best) >= scope(dst) &&
2138 * scope(src) >= scope(dst))
2139 */
2140 if (IN6_ARE_SCOPE_CMP(best_scope, dst_scope) < 0 &&
2141 IN6_ARE_SCOPE_CMP(src_scope, dst_scope) >= 0)
2142 goto replace; /* (A) */
2143 if (IN6_ARE_SCOPE_CMP(src_scope, dst_scope) < 0 &&
2144 IN6_ARE_SCOPE_CMP(best_scope, dst_scope) >= 0)
2145 continue; /* (B) */
2146
2147 /*
2148 * A deprecated address SHOULD NOT be used in new
2149 * communications if an alternate (non-deprecated)
2150 * address is available and has sufficient scope.
2151 * RFC 2462, Section 5.5.4.
2152 */
2153 if (((struct in6_ifaddr *)ifa)->ia6_flags &
2154 IN6_IFF_DEPRECATED) {
2155 /*
2156 * Ignore any deprecated addresses if
2157 * specified by configuration.
2158 */
2159 if (!ip6_use_deprecated)
2160 continue;
2161
2162 /*
2163 * If we have already found a non-deprecated
2164 * candidate, just ignore deprecated addresses.
2165 */
bde3511a 2166 if (!(ifa_best->ia6_flags & IN6_IFF_DEPRECATED))
984263bc
MD
2167 continue;
2168 }
2169
2170 /*
2171 * A non-deprecated address is always preferred
2172 * to a deprecated one regardless of scopes and
2173 * address matching (Note invariants ensured by the
2174 * conditions (A) and (B) above.)
2175 */
2176 if ((ifa_best->ia6_flags & IN6_IFF_DEPRECATED) &&
bde3511a
JH
2177 !(((struct in6_ifaddr *)ifa)->ia6_flags &
2178 IN6_IFF_DEPRECATED))
984263bc
MD
2179 goto replace;
2180
2181 /*
2182 * When we use temporary addresses described in
2183 * RFC 3041, we prefer temporary addresses to
2184 * public autoconf addresses. Again, note the
2185 * invariants from (A) and (B). Also note that we
2186 * don't have any preference between static addresses
2187 * and autoconf addresses (despite of whether or not
2188 * the latter is temporary or public.)
2189 */
2190 if (ip6_use_tempaddr) {
2191 struct in6_ifaddr *ifat;
2192
2193 ifat = (struct in6_ifaddr *)ifa;
2194 if ((ifa_best->ia6_flags &
2195 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2196 == IN6_IFF_AUTOCONF &&
2197 (ifat->ia6_flags &
2198 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2199 == (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY)) {
2200 goto replace;
2201 }
2202 if ((ifa_best->ia6_flags &
2203 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2204 == (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY) &&
2205 (ifat->ia6_flags &
2206 (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY))
2207 == IN6_IFF_AUTOCONF) {
2208 continue;
2209 }
2210 }
2211
2212 /*
2213 * At this point, we have two cases:
2214 * 1. we are looking at a non-deprecated address,
2215 * and ifa_best is also non-deprecated.
2216 * 2. we are looking at a deprecated address,
2217 * and ifa_best is also deprecated.
2218 * Also, we do not have to consider a case where
2219 * the scope of if_best is larger(smaller) than dst and
2220 * the scope of the current address is smaller(larger)
2221 * than dst. Such a case has already been covered.
2222 * Tiebreaking is done according to the following
2223 * items:
2224 * - the scope comparison between the address and
2225 * dst (dscopecmp)
2226 * - the scope comparison between the address and
2227 * ifa_best (bscopecmp)
2228 * - if the address match dst longer than ifa_best
2229 * (matchcmp)
2230 * - if the address is on the outgoing I/F (outI/F)
2231 *
2232 * Roughly speaking, the selection policy is
2233 * - the most important item is scope. The same scope
2234 * is best. Then search for a larger scope.
2235 * Smaller scopes are the last resort.
2236 * - A deprecated address is chosen only when we have
2237 * no address that has an enough scope, but is
2238 * prefered to any addresses of smaller scopes
2239 * (this must be already done above.)
2240 * - addresses on the outgoing I/F are preferred to
2241 * ones on other interfaces if none of above
2242 * tiebreaks. In the table below, the column "bI"
2243 * means if the best_ifa is on the outgoing
2244 * interface, and the column "sI" means if the ifa
2245 * is on the outgoing interface.
2246 * - If there is no other reasons to choose one,
2247 * longest address match against dst is considered.
2248 *
2249 * The precise decision table is as follows:
2250 * dscopecmp bscopecmp match bI oI | replace?
2251 * N/A equal N/A Y N | No (1)
2252 * N/A equal N/A N Y | Yes (2)
2253 * N/A equal larger N/A | Yes (3)
2254 * N/A equal !larger N/A | No (4)
2255 * larger larger N/A N/A | No (5)
2256 * larger smaller N/A N/A | Yes (6)
2257 * smaller larger N/A N/A | Yes (7)
2258 * smaller smaller N/A N/A | No (8)
2259 * equal smaller N/A N/A | Yes (9)
2260 * equal larger (already done at A above)
2261 */
2262 dscopecmp = IN6_ARE_SCOPE_CMP(src_scope, dst_scope);
2263 bscopecmp = IN6_ARE_SCOPE_CMP(src_scope, best_scope);
2264
2265 if (bscopecmp == 0) {
2266 struct ifnet *bifp = ifa_best->ia_ifp;
2267
2268 if (bifp == oifp && ifp != oifp) /* (1) */
2269 continue;
2270 if (bifp != oifp && ifp == oifp) /* (2) */
2271 goto replace;
2272
2273 /*
2274 * Both bifp and ifp are on the outgoing
2275 * interface, or both two are on a different
2276 * interface from the outgoing I/F.
2277 * now we need address matching against dst
2278 * for tiebreaking.
2279 */
2280 tlen = in6_matchlen(IFA_IN6(ifa), dst);
2281 matchcmp = tlen - blen;
2282 if (matchcmp > 0) /* (3) */
2283 goto replace;
2284 continue; /* (4) */
2285 }
2286 if (dscopecmp > 0) {
2287 if (bscopecmp > 0) /* (5) */
2288 continue;
2289 goto replace; /* (6) */
2290 }
2291 if (dscopecmp < 0) {
2292 if (bscopecmp > 0) /* (7) */
2293 goto replace;
2294 continue; /* (8) */
2295 }
2296
2297 /* now dscopecmp must be 0 */
2298 if (bscopecmp < 0)
2299 goto replace; /* (9) */
2300
8f706258 2301replace:
984263bc
MD
2302 ifa_best = (struct in6_ifaddr *)ifa;
2303 blen = tlen >= 0 ? tlen :
2304 in6_matchlen(IFA_IN6(ifa), dst);
2305 best_scope = in6_addrscope(&ifa_best->ia_addr.sin6_addr);
2306 }
2307 }
2308
2309 /* count statistics for future improvements */
2310 if (ifa_best == NULL)
2311 ip6stat.ip6s_sources_none++;
2312 else {
2313 if (oifp == ifa_best->ia_ifp)
2314 ip6stat.ip6s_sources_sameif[best_scope]++;
2315 else
2316 ip6stat.ip6s_sources_otherif[best_scope]++;
2317
2318 if (best_scope == dst_scope)
2319 ip6stat.ip6s_sources_samescope[best_scope]++;
2320 else
2321 ip6stat.ip6s_sources_otherscope[best_scope]++;
2322
bde3511a 2323 if (ifa_best->ia6_flags & IN6_IFF_DEPRECATED)
984263bc
MD
2324 ip6stat.ip6s_sources_deprecated[best_scope]++;
2325 }
2326
bde3511a 2327 return (ifa_best);
984263bc
MD
2328}
2329
2330/*
2331 * return the best address out of the same scope. if no address was
2332 * found, return the first valid address from designated IF.
2333 */
2334struct in6_ifaddr *
122ebd49 2335in6_ifawithifp(struct ifnet *ifp, struct in6_addr *dst)
984263bc
MD
2336{
2337 int dst_scope = in6_addrscope(dst), blen = -1, tlen;
b2632176 2338 struct ifaddr_container *ifac;
4090d6ff 2339 struct in6_ifaddr *besta = NULL;
984263bc
MD
2340 struct in6_ifaddr *dep[2]; /* last-resort: deprecated */
2341
2342 dep[0] = dep[1] = NULL;
2343
2344 /*
2345 * We first look for addresses in the same scope.
2346 * If there is one, return it.
2347 * If two or more, return one which matches the dst longest.
2348 * If none, return one of global addresses assigned other ifs.
2349 */
b2632176
SZ
2350 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2351 struct ifaddr *ifa = ifac->ifa;
2352
984263bc
MD
2353 if (ifa->ifa_addr->sa_family != AF_INET6)
2354 continue;
2355 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2356 continue; /* XXX: is there any case to allow anycast? */
2357 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2358 continue; /* don't use this interface */
2359 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2360 continue;
2361 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2362 if (ip6_use_deprecated)
2363 dep[0] = (struct in6_ifaddr *)ifa;
2364 continue;
2365 }
2366
2367 if (dst_scope == in6_addrscope(IFA_IN6(ifa))) {
2368 /*
2369 * call in6_matchlen() as few as possible
2370 */
2371 if (besta) {
2372 if (blen == -1)
2373 blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst);
2374 tlen = in6_matchlen(IFA_IN6(ifa), dst);
2375 if (tlen > blen) {
2376 blen = tlen;
2377 besta = (struct in6_ifaddr *)ifa;
2378 }
2379 } else
2380 besta = (struct in6_ifaddr *)ifa;
2381 }
2382 }
2383 if (besta)
bde3511a 2384 return (besta);
984263bc 2385
b2632176
SZ
2386 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2387 struct ifaddr *ifa = ifac->ifa;
2388
984263bc
MD
2389 if (ifa->ifa_addr->sa_family != AF_INET6)
2390 continue;
2391 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST)
2392 continue; /* XXX: is there any case to allow anycast? */
2393 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY)
2394 continue; /* don't use this interface */
2395 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED)
2396 continue;
2397 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) {
2398 if (ip6_use_deprecated)
2399 dep[1] = (struct in6_ifaddr *)ifa;
2400 continue;
2401 }
2402
2403 return (struct in6_ifaddr *)ifa;
2404 }
2405
2406 /* use the last-resort values, that are, deprecated addresses */
2407 if (dep[0])
2408 return dep[0];
2409 if (dep[1])
2410 return dep[1];
2411
2412 return NULL;
2413}
2414
2415/*
2416 * perform DAD when interface becomes IFF_UP.
2417 */
83167ee5
SZ
2418static void
2419in6_if_up_dispatch(netmsg_t nmsg)
984263bc 2420{
5204e13c 2421 struct ifnet *ifp = nmsg->lmsg.u.ms_resultp;
b2632176 2422 struct ifaddr_container *ifac;
984263bc
MD
2423 struct in6_ifaddr *ia;
2424 int dad_delay; /* delay ticks before DAD output */
2425
5204e13c 2426 ASSERT_NETISR0;
83167ee5 2427
984263bc
MD
2428 /*
2429 * special cases, like 6to4, are handled in in6_ifattach
2430 */
2431 in6_ifattach(ifp, NULL);
2432
2433 dad_delay = 0;
b2632176
SZ
2434 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
2435 struct ifaddr *ifa = ifac->ifa;
2436
984263bc
MD
2437 if (ifa->ifa_addr->sa_family != AF_INET6)
2438 continue;
2439 ia = (struct in6_ifaddr *)ifa;
2440 if (ia->ia6_flags & IN6_IFF_TENTATIVE)
2441 nd6_dad_start(ifa, &dad_delay);
2442 }
83167ee5 2443
5204e13c 2444 netisr_replymsg(&nmsg->base, 0);
83167ee5
SZ
2445}
2446
2447void
2448in6_if_up(struct ifnet *ifp)
2449{
2450 struct netmsg_base nmsg;
83167ee5
SZ
2451
2452 netmsg_init(&nmsg, NULL, &curthread->td_msgport, 0, in6_if_up_dispatch);
5204e13c
SZ
2453 nmsg.lmsg.u.ms_resultp = ifp;
2454 netisr_domsg(&nmsg, 0);
984263bc
MD
2455}
2456
2457int
122ebd49 2458in6if_do_dad(struct ifnet *ifp)
984263bc 2459{
bde3511a
JH
2460 if (ifp->if_flags & IFF_LOOPBACK)
2461 return (0);
984263bc
MD
2462
2463 switch (ifp->if_type) {
2464#ifdef IFT_DUMMY
2465 case IFT_DUMMY:
bde3511a 2466 return (0);
06937ef9 2467#endif
984263bc
MD
2468 default:
2469 /*
2470 * Our DAD routine requires the interface up and running.
2471 * However, some interfaces can be up before the RUNNING
2472 * status. Additionaly, users may try to assign addresses
2473 * before the interface becomes up (or running).
2474 * We simply skip DAD in such a case as a work around.
2475 * XXX: we should rather mark "tentative" on such addresses,
2476 * and do DAD after the interface becomes ready.
2477 */
2478 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) !=
2479 (IFF_UP|IFF_RUNNING))
bde3511a 2480 return (0);
984263bc 2481
bde3511a 2482 return (1);
984263bc
MD
2483 }
2484}
2485
2486/*
2487 * Calculate max IPv6 MTU through all the interfaces and store it
2488 * to in6_maxmtu.
2489 */
2490void
122ebd49 2491in6_setmaxmtu(void)
984263bc
MD
2492{
2493 unsigned long maxmtu = 0;
b4051e25
SZ
2494 const struct ifnet_array *arr;
2495 int i;
984263bc 2496
5204e13c 2497 ASSERT_NETISR0;
20c14016 2498
b4051e25
SZ
2499 arr = ifnet_array_get();
2500 for (i = 0; i < arr->ifnet_count; ++i) {
2501 struct ifnet *ifp = arr->ifnet_arr[i];
2502
0fd7bed5 2503 /* this function can be called during ifnet initialization */
8e13abe7 2504 if (ifp->if_afdata[AF_INET6] == NULL)
0fd7bed5 2505 continue;
8e13abe7 2506 if ((ifp->if_flags & IFF_LOOPBACK) == 0 &&
304e70d2
SZ
2507 IN6_LINKMTU(ifp) > maxmtu)
2508 maxmtu = IN6_LINKMTU(ifp);
984263bc
MD
2509 }
2510 if (maxmtu) /* update only when maxmtu is positive */
2511 in6_maxmtu = maxmtu;
2512}
2513
698ac46c
HS
2514void *
2515in6_domifattach(struct ifnet *ifp)
2516{
2517 struct in6_ifextra *ext;
2518
efda3bd0 2519 ext = (struct in6_ifextra *)kmalloc(sizeof(*ext), M_IFADDR, M_WAITOK);
698ac46c
HS
2520 bzero(ext, sizeof(*ext));
2521
77652cad 2522 ext->in6_ifstat = (struct in6_ifstat *)kmalloc(sizeof(struct in6_ifstat),
698ac46c
HS
2523 M_IFADDR, M_WAITOK);
2524 bzero(ext->in6_ifstat, sizeof(*ext->in6_ifstat));
2525
2526 ext->icmp6_ifstat =
77652cad 2527 (struct icmp6_ifstat *)kmalloc(sizeof(struct icmp6_ifstat),
698ac46c
HS
2528 M_IFADDR, M_WAITOK);
2529 bzero(ext->icmp6_ifstat, sizeof(*ext->icmp6_ifstat));
2530
2531 ext->nd_ifinfo = nd6_ifattach(ifp);
2532 ext->scope6_id = scope6_ifattach(ifp);
2533 return ext;
2534}
2535
2536void
2537in6_domifdetach(struct ifnet *ifp, void *aux)
2538{
2539 struct in6_ifextra *ext = (struct in6_ifextra *)aux;
2540 scope6_ifdetach(ext->scope6_id);
2541 nd6_ifdetach(ext->nd_ifinfo);
efda3bd0
MD
2542 kfree(ext->in6_ifstat, M_IFADDR);
2543 kfree(ext->icmp6_ifstat, M_IFADDR);
2544 kfree(ext, M_IFADDR);
698ac46c 2545}