1 /* $NetBSD: af_inet6.c,v 1.24 2009/08/07 18:53:37 dyoung Exp $ */
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
34 __RCSID("$NetBSD: af_inet6.c,v 1.24 2009/08/07 18:53:37 dyoung Exp $");
37 #include <sys/param.h>
38 #include <sys/ioctl.h>
39 #include <sys/socket.h>
42 #include <netinet/in.h>
43 #include <netinet/in_var.h>
44 #include <netinet6/nd6.h>
59 #include "af_inetany.h"
61 static void in6_constructor(void) __attribute__((constructor
));
62 static void in6_alias(const char *, prop_dictionary_t
, prop_dictionary_t
,
64 static void in6_commit_address(prop_dictionary_t
, prop_dictionary_t
);
66 static int setia6eui64_impl(prop_dictionary_t
, struct in6_aliasreq
*);
67 static int setia6flags_impl(prop_dictionary_t
, struct in6_aliasreq
*);
68 static int setia6pltime_impl(prop_dictionary_t
, struct in6_aliasreq
*);
69 static int setia6vltime_impl(prop_dictionary_t
, struct in6_aliasreq
*);
71 static int setia6lifetime(prop_dictionary_t
, int64_t, time_t *, uint32_t *);
73 static void in6_delscopeid(struct sockaddr_in6
*sin6
);
74 static void in6_status(prop_dictionary_t
, prop_dictionary_t
, bool);
76 static struct usage_func usage
;
77 static cmdloop_branch_t branch
[2];
79 static const struct kwinst ia6flagskw
[] = {
80 IFKW("anycast", IN6_IFF_ANYCAST
)
81 , IFKW("tentative", IN6_IFF_TENTATIVE
)
82 , IFKW("deprecated", IN6_IFF_DEPRECATED
)
85 static struct pinteger parse_pltime
= PINTEGER_INITIALIZER(&parse_pltime
,
86 "pltime", 0, NULL
, "pltime", &command_root
.pb_parser
);
88 static struct pinteger parse_vltime
= PINTEGER_INITIALIZER(&parse_vltime
,
89 "vltime", 0, NULL
, "vltime", &command_root
.pb_parser
);
91 static const struct kwinst inet6kw
[] = {
92 {.k_word
= "pltime", .k_nextparser
= &parse_pltime
.pi_parser
}
93 , {.k_word
= "vltime", .k_nextparser
= &parse_vltime
.pi_parser
}
94 , {.k_word
= "eui64", .k_key
= "eui64", .k_type
= KW_T_BOOL
,
95 .k_bool
= true, .k_nextparser
= &command_root
.pb_parser
}
98 struct pkw ia6flags
= PKW_INITIALIZER(&ia6flags
, "ia6flags", NULL
,
99 "ia6flag", ia6flagskw
, __arraycount(ia6flagskw
), &command_root
.pb_parser
);
100 struct pkw inet6
= PKW_INITIALIZER(&inet6
, "IPv6 keywords", NULL
,
101 NULL
, inet6kw
, __arraycount(inet6kw
), NULL
);
103 static struct afswtch in6af
= {
104 .af_name
= "inet6", .af_af
= AF_INET6
, .af_status
= in6_status
,
105 .af_addr_commit
= in6_commit_address
109 prefix(void *val
, int size
)
111 u_char
*pname
= (u_char
*)val
;
112 int byte
, bit
, plen
= 0;
114 for (byte
= 0; byte
< size
; byte
++, plen
+= 8)
115 if (pname
[byte
] != 0xff)
119 for (bit
= 7; bit
!= 0; bit
--, plen
++)
120 if (!(pname
[byte
] & (1 << bit
)))
122 for (; bit
!= 0; bit
--)
123 if (pname
[byte
] & (1 << bit
))
126 for (; byte
< size
; byte
++)
133 setia6flags_impl(prop_dictionary_t env
, struct in6_aliasreq
*ifra
)
137 if (!prop_dictionary_get_int64(env
, "ia6flag", &ia6flag
)) {
144 ifra
->ifra_flags
&= ~ia6flag
;
146 ifra
->ifra_flags
|= ia6flag
;
151 setia6pltime_impl(prop_dictionary_t env
, struct in6_aliasreq
*ifra
)
155 if (!prop_dictionary_get_int64(env
, "pltime", &pltime
)) {
160 return setia6lifetime(env
, pltime
,
161 &ifra
->ifra_lifetime
.ia6t_preferred
,
162 &ifra
->ifra_lifetime
.ia6t_pltime
);
166 setia6vltime_impl(prop_dictionary_t env
, struct in6_aliasreq
*ifra
)
170 if (!prop_dictionary_get_int64(env
, "vltime", &vltime
)) {
175 return setia6lifetime(env
, vltime
,
176 &ifra
->ifra_lifetime
.ia6t_expire
,
177 &ifra
->ifra_lifetime
.ia6t_vltime
);
181 setia6lifetime(prop_dictionary_t env
, int64_t val
, time_t *timep
,
187 if ((af
= getaf(env
)) == -1 || af
!= AF_INET6
) {
189 "inet6 address lifetime not allowed for the AF");
199 setia6eui64_impl(prop_dictionary_t env
, struct in6_aliasreq
*ifra
)
202 struct ifaddrs
*ifap
, *ifa
;
203 const struct sockaddr_in6
*sin6
= NULL
;
204 const struct in6_addr
*lladdr
= NULL
;
205 struct in6_addr
*in6
;
210 if (!prop_dictionary_get_bool(env
, "eui64", &doit
) || !doit
) {
215 if ((ifname
= getifname(env
)) == NULL
)
219 if (af
!= AF_INET6
) {
221 "eui64 address modifier not allowed for the AF");
223 in6
= &ifra
->ifra_addr
.sin6_addr
;
224 if (memcmp(&in6addr_any
.s6_addr
[8], &in6
->s6_addr
[8], 8) != 0) {
226 struct sockaddr_in6 sin6
;
228 } any
= {.sin6
= {.sin6_family
= AF_INET6
}};
229 memcpy(&any
.sin6
.sin6_addr
, &in6addr_any
,
230 sizeof(any
.sin6
.sin6_addr
));
231 (void)sockaddr_snprintf(buf
[0], sizeof(buf
[0]), "%a%%S",
233 (void)sockaddr_snprintf(buf
[1], sizeof(buf
[1]), "%a%%S",
234 (const struct sockaddr
*)&ifra
->ifra_addr
);
235 errx(EXIT_FAILURE
, "interface index is already filled, %s | %s",
238 if (getifaddrs(&ifap
) != 0)
239 err(EXIT_FAILURE
, "getifaddrs");
240 for (ifa
= ifap
; ifa
; ifa
= ifa
->ifa_next
) {
241 if (ifa
->ifa_addr
->sa_family
== AF_INET6
&&
242 strcmp(ifa
->ifa_name
, ifname
) == 0) {
243 sin6
= (const struct sockaddr_in6
*)ifa
->ifa_addr
;
244 if (IN6_IS_ADDR_LINKLOCAL(&sin6
->sin6_addr
)) {
245 lladdr
= &sin6
->sin6_addr
;
251 errx(EXIT_FAILURE
, "could not determine link local address");
253 memcpy(&in6
->s6_addr
[8], &lladdr
->s6_addr
[8], 8);
259 /* KAME idiosyncrasy */
261 in6_delscopeid(struct sockaddr_in6
*sin6
)
263 if (!IN6_IS_ADDR_LINKLOCAL(&sin6
->sin6_addr
) ||
264 sin6
->sin6_scope_id
== 0)
267 *(u_int16_t
*)&sin6
->sin6_addr
.s6_addr
[2] = htons(sin6
->sin6_scope_id
);
268 sin6
->sin6_scope_id
= 0;
271 /* XXX not really an alias */
273 in6_alias(const char *ifname
, prop_dictionary_t env
, prop_dictionary_t oenv
,
274 struct in6_ifreq
*creq
)
276 struct in6_ifreq ifr6
;
277 struct sockaddr_in6
*sin6
;
278 char hbuf
[NI_MAXHOST
];
281 const int niflag
= Nflag
? 0 : NI_NUMERICHOST
;
282 unsigned short flags
;
284 /* Get the non-alias address for this interface. */
285 if ((s
= getsock(AF_INET6
)) == -1) {
286 if (errno
== EAFNOSUPPORT
)
288 err(EXIT_FAILURE
, "socket");
291 sin6
= &creq
->ifr_addr
;
293 in6_fillscopeid(sin6
);
294 scopeid
= sin6
->sin6_scope_id
;
295 if (getnameinfo((const struct sockaddr
*)sin6
, sin6
->sin6_len
,
296 hbuf
, sizeof(hbuf
), NULL
, 0, niflag
))
297 strlcpy(hbuf
, "", sizeof(hbuf
)); /* some message? */
298 printf("\tinet6 %s", hbuf
);
300 if (getifflags(env
, oenv
, &flags
) == -1)
301 err(EXIT_FAILURE
, "%s: getifflags", __func__
);
303 if (flags
& IFF_POINTOPOINT
) {
305 if (ioctl(s
, SIOCGIFDSTADDR_IN6
, &ifr6
) == -1) {
306 if (errno
!= EADDRNOTAVAIL
)
307 warn("SIOCGIFDSTADDR_IN6");
308 memset(&ifr6
.ifr_addr
, 0, sizeof(ifr6
.ifr_addr
));
309 ifr6
.ifr_addr
.sin6_family
= AF_INET6
;
310 ifr6
.ifr_addr
.sin6_len
= sizeof(struct sockaddr_in6
);
312 sin6
= &ifr6
.ifr_addr
;
313 in6_fillscopeid(sin6
);
315 if (getnameinfo((struct sockaddr
*)sin6
, sin6
->sin6_len
,
316 hbuf
, sizeof(hbuf
), NULL
, 0, niflag
))
317 strlcpy(hbuf
, "", sizeof(hbuf
)); /* some message? */
318 printf(" -> %s", hbuf
);
322 if (ioctl(s
, SIOCGIFNETMASK_IN6
, &ifr6
) == -1) {
323 if (errno
!= EADDRNOTAVAIL
)
324 warn("SIOCGIFNETMASK_IN6");
326 sin6
= &ifr6
.ifr_addr
;
327 printf(" prefixlen %d", prefix(&sin6
->sin6_addr
,
328 sizeof(struct in6_addr
)));
332 if (ioctl(s
, SIOCGIFAFLAG_IN6
, &ifr6
) == -1) {
333 if (errno
!= EADDRNOTAVAIL
)
334 warn("SIOCGIFAFLAG_IN6");
336 if (ifr6
.ifr_ifru
.ifru_flags6
& IN6_IFF_ANYCAST
)
338 if (ifr6
.ifr_ifru
.ifru_flags6
& IN6_IFF_TENTATIVE
)
339 printf(" tentative");
340 if (ifr6
.ifr_ifru
.ifru_flags6
& IN6_IFF_DUPLICATED
)
341 printf(" duplicated");
342 if (ifr6
.ifr_ifru
.ifru_flags6
& IN6_IFF_DETACHED
)
344 if (ifr6
.ifr_ifru
.ifru_flags6
& IN6_IFF_DEPRECATED
)
345 printf(" deprecated");
349 printf(" scopeid 0x%x", scopeid
);
352 struct in6_addrlifetime
*lifetime
;
354 lifetime
= &ifr6
.ifr_ifru
.ifru_lifetime
;
355 if (ioctl(s
, SIOCGIFALIFETIME_IN6
, &ifr6
) == -1) {
356 if (errno
!= EADDRNOTAVAIL
)
357 warn("SIOCGIFALIFETIME_IN6");
358 } else if (lifetime
->ia6t_preferred
|| lifetime
->ia6t_expire
) {
359 time_t t
= time(NULL
);
361 if (lifetime
->ia6t_preferred
) {
363 (unsigned long)(lifetime
->ia6t_preferred
-
364 MIN(t
, lifetime
->ia6t_preferred
)));
369 if (lifetime
->ia6t_expire
) {
371 (unsigned long)(lifetime
->ia6t_expire
-
372 MIN(t
, lifetime
->ia6t_expire
)));
380 in6_status(prop_dictionary_t env
, prop_dictionary_t oenv
, bool force
)
382 struct ifaddrs
*ifap
, *ifa
;
383 struct in6_ifreq ifr
;
385 bool printprefs
= false;
387 if ((ifname
= getifname(env
)) == NULL
)
388 err(EXIT_FAILURE
, "%s: getifname", __func__
);
390 if (getifaddrs(&ifap
) != 0)
391 err(EXIT_FAILURE
, "getifaddrs");
392 printprefs
= ifa_any_preferences(ifname
, ifap
, AF_INET6
);
393 for (ifa
= ifap
; ifa
; ifa
= ifa
->ifa_next
) {
394 if (strcmp(ifname
, ifa
->ifa_name
) != 0)
396 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
398 if (sizeof(ifr
.ifr_addr
) < ifa
->ifa_addr
->sa_len
)
401 memset(&ifr
, 0, sizeof(ifr
));
402 estrlcpy(ifr
.ifr_name
, ifa
->ifa_name
, sizeof(ifr
.ifr_name
));
403 memcpy(&ifr
.ifr_addr
, ifa
->ifa_addr
, ifa
->ifa_addr
->sa_len
);
404 in6_alias(ifname
, env
, oenv
, &ifr
);
406 ifa_print_preference(ifa
->ifa_name
, ifa
->ifa_addr
);
413 in6_pre_aifaddr(prop_dictionary_t env
, const struct afparam
*param
)
415 struct in6_aliasreq
*ifra
= param
->req
.buf
;
417 setia6eui64_impl(env
, ifra
);
418 setia6vltime_impl(env
, ifra
);
419 setia6pltime_impl(env
, ifra
);
420 setia6flags_impl(env
, ifra
);
421 in6_delscopeid(&ifra
->ifra_addr
);
422 in6_delscopeid(&ifra
->ifra_dstaddr
);
428 in6_commit_address(prop_dictionary_t env
, prop_dictionary_t oenv
)
430 struct in6_ifreq in6_ifr
= {
432 .sin6_family
= AF_INET6
,
433 .sin6_len
= sizeof(in6_ifr
.ifr_addr
),
436 {0xff, 0xff, 0xff, 0xff,
437 0xff, 0xff, 0xff, 0xff}
441 static struct sockaddr_in6 in6_defmask
= {
442 .sin6_family
= AF_INET6
,
443 .sin6_len
= sizeof(in6_defmask
),
445 .s6_addr
= {0xff, 0xff, 0xff, 0xff,
446 0xff, 0xff, 0xff, 0xff}
450 struct in6_aliasreq in6_ifra
= {
452 .sin6_family
= AF_INET6
,
453 .sin6_len
= sizeof(in6_ifra
.ifra_prefixmask
),
456 {0xff, 0xff, 0xff, 0xff,
457 0xff, 0xff, 0xff, 0xff}}},
459 .ia6t_pltime
= ND6_INFINITE_LIFETIME
460 , .ia6t_vltime
= ND6_INFINITE_LIFETIME
463 struct afparam in6param
= {
464 .req
= BUFPARAM(in6_ifra
)
465 , .dgreq
= BUFPARAM(in6_ifr
)
467 {.buf
= in6_ifr
.ifr_name
,
468 .buflen
= sizeof(in6_ifr
.ifr_name
)},
469 {.buf
= in6_ifra
.ifra_name
,
470 .buflen
= sizeof(in6_ifra
.ifra_name
)}
472 , .dgaddr
= BUFPARAM(in6_ifr
.ifr_addr
)
473 , .addr
= BUFPARAM(in6_ifra
.ifra_addr
)
474 , .dst
= BUFPARAM(in6_ifra
.ifra_dstaddr
)
475 , .brd
= BUFPARAM(in6_ifra
.ifra_broadaddr
)
476 , .mask
= BUFPARAM(in6_ifra
.ifra_prefixmask
)
477 , .aifaddr
= IFADDR_PARAM(SIOCAIFADDR_IN6
)
478 , .difaddr
= IFADDR_PARAM(SIOCDIFADDR_IN6
)
479 , .gifaddr
= IFADDR_PARAM(SIOCGIFADDR_IN6
)
480 , .defmask
= BUFPARAM(in6_defmask
)
481 , .pre_aifaddr
= in6_pre_aifaddr
483 commit_address(env
, oenv
, &in6param
);
487 in6_usage(prop_dictionary_t env
)
490 "\t[ anycast | -anycast ] [ deprecated | -deprecated ]\n"
491 "\t[ tentative | -tentative ] [ pltime n ] [ vltime n ] "
496 in6_constructor(void)
498 if (register_flag('L') != 0)
499 err(EXIT_FAILURE
, __func__
);
500 register_family(&in6af
);
501 usage_func_init(&usage
, in6_usage
);
502 register_usage(&usage
);
503 cmdloop_branch_init(&branch
[0], &ia6flags
.pk_parser
);
504 cmdloop_branch_init(&branch
[1], &inet6
.pk_parser
);
505 register_cmdloop_branch(&branch
[0]);
506 register_cmdloop_branch(&branch
[1]);