1 /* $NetBSD: af_inet6.c,v 1.33 2015/05/12 14:05:29 roy 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.33 2015/05/12 14:05:29 roy 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"
62 static void in6_constructor(void) __attribute__((constructor
));
63 static void in6_alias(const char *, prop_dictionary_t
, prop_dictionary_t
,
65 static void in6_commit_address(prop_dictionary_t
, prop_dictionary_t
);
67 static int setia6eui64_impl(prop_dictionary_t
, struct in6_aliasreq
*);
68 static int setia6flags_impl(prop_dictionary_t
, struct in6_aliasreq
*);
69 static int setia6pltime_impl(prop_dictionary_t
, struct in6_aliasreq
*);
70 static int setia6vltime_impl(prop_dictionary_t
, struct in6_aliasreq
*);
72 static int setia6lifetime(prop_dictionary_t
, int64_t, time_t *, uint32_t *);
74 static void in6_status(prop_dictionary_t
, prop_dictionary_t
, bool);
75 static bool in6_addr_tentative(struct ifaddrs
*ifa
);
77 static struct usage_func usage
;
78 static cmdloop_branch_t branch
[2];
80 static const struct kwinst ia6flagskw
[] = {
81 IFKW("anycast", IN6_IFF_ANYCAST
)
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
,
106 .af_addr_tentative
= in6_addr_tentative
110 prefix(void *val
, int size
)
112 u_char
*pname
= (u_char
*)val
;
113 int byte
, bit
, plen
= 0;
115 for (byte
= 0; byte
< size
; byte
++, plen
+= 8)
116 if (pname
[byte
] != 0xff)
120 for (bit
= 7; bit
!= 0; bit
--, plen
++)
121 if (!(pname
[byte
] & (1 << bit
)))
123 for (; bit
!= 0; bit
--)
124 if (pname
[byte
] & (1 << bit
))
127 for (; byte
< size
; byte
++)
134 setia6flags_impl(prop_dictionary_t env
, struct in6_aliasreq
*ifra
)
138 if (!prop_dictionary_get_int64(env
, "ia6flag", &ia6flag
)) {
145 ifra
->ifra_flags
&= ~ia6flag
;
147 ifra
->ifra_flags
|= ia6flag
;
152 setia6pltime_impl(prop_dictionary_t env
, struct in6_aliasreq
*ifra
)
156 if (!prop_dictionary_get_int64(env
, "pltime", &pltime
)) {
161 return setia6lifetime(env
, pltime
,
162 &ifra
->ifra_lifetime
.ia6t_preferred
,
163 &ifra
->ifra_lifetime
.ia6t_pltime
);
167 setia6vltime_impl(prop_dictionary_t env
, struct in6_aliasreq
*ifra
)
171 if (!prop_dictionary_get_int64(env
, "vltime", &vltime
)) {
176 return setia6lifetime(env
, vltime
,
177 &ifra
->ifra_lifetime
.ia6t_expire
,
178 &ifra
->ifra_lifetime
.ia6t_vltime
);
182 setia6lifetime(prop_dictionary_t env
, int64_t val
, time_t *timep
,
188 if ((af
= getaf(env
)) == -1 || af
!= AF_INET6
) {
190 "inet6 address lifetime not allowed for the AF");
200 setia6eui64_impl(prop_dictionary_t env
, struct in6_aliasreq
*ifra
)
203 struct ifaddrs
*ifap
, *ifa
;
204 const struct sockaddr_in6
*sin6
= NULL
;
205 const struct in6_addr
*lladdr
= NULL
;
206 struct in6_addr
*in6
;
211 if (!prop_dictionary_get_bool(env
, "eui64", &doit
) || !doit
) {
216 if ((ifname
= getifname(env
)) == NULL
)
220 if (af
!= AF_INET6
) {
222 "eui64 address modifier not allowed for the AF");
224 in6
= &ifra
->ifra_addr
.sin6_addr
;
225 if (memcmp(&in6addr_any
.s6_addr
[8], &in6
->s6_addr
[8], 8) != 0) {
227 struct sockaddr_in6 sin6
;
229 } any
= {.sin6
= {.sin6_family
= AF_INET6
}};
230 memcpy(&any
.sin6
.sin6_addr
, &in6addr_any
,
231 sizeof(any
.sin6
.sin6_addr
));
232 (void)sockaddr_snprintf(buf
[0], sizeof(buf
[0]), "%a%%S",
234 (void)sockaddr_snprintf(buf
[1], sizeof(buf
[1]), "%a%%S",
235 (const struct sockaddr
*)&ifra
->ifra_addr
);
236 errx(EXIT_FAILURE
, "interface index is already filled, %s | %s",
239 if (getifaddrs(&ifap
) != 0)
240 err(EXIT_FAILURE
, "getifaddrs");
241 for (ifa
= ifap
; ifa
; ifa
= ifa
->ifa_next
) {
242 if (ifa
->ifa_addr
->sa_family
== AF_INET6
&&
243 strcmp(ifa
->ifa_name
, ifname
) == 0) {
244 sin6
= (const struct sockaddr_in6
*)ifa
->ifa_addr
;
245 if (IN6_IS_ADDR_LINKLOCAL(&sin6
->sin6_addr
)) {
246 lladdr
= &sin6
->sin6_addr
;
252 errx(EXIT_FAILURE
, "could not determine link local address");
254 memcpy(&in6
->s6_addr
[8], &lladdr
->s6_addr
[8], 8);
260 /* XXX not really an alias */
262 in6_alias(const char *ifname
, prop_dictionary_t env
, prop_dictionary_t oenv
,
263 struct in6_ifreq
*creq
)
265 struct in6_ifreq ifr6
;
266 struct sockaddr_in6
*sin6
;
267 char hbuf
[NI_MAXHOST
];
270 const int niflag
= Nflag
? 0 : NI_NUMERICHOST
;
271 unsigned short flags
;
273 /* Get the non-alias address for this interface. */
274 if ((s
= getsock(AF_INET6
)) == -1) {
275 if (errno
== EAFNOSUPPORT
)
277 err(EXIT_FAILURE
, "socket");
280 sin6
= &creq
->ifr_addr
;
282 inet6_getscopeid(sin6
, INET6_IS_ADDR_LINKLOCAL
);
283 scopeid
= sin6
->sin6_scope_id
;
284 if (getnameinfo((const struct sockaddr
*)sin6
, sin6
->sin6_len
,
285 hbuf
, sizeof(hbuf
), NULL
, 0, niflag
))
286 strlcpy(hbuf
, "", sizeof(hbuf
)); /* some message? */
287 printf("\tinet6 %s", hbuf
);
289 if (getifflags(env
, oenv
, &flags
) == -1)
290 err(EXIT_FAILURE
, "%s: getifflags", __func__
);
292 if (flags
& IFF_POINTOPOINT
) {
294 if (prog_ioctl(s
, SIOCGIFDSTADDR_IN6
, &ifr6
) == -1) {
295 if (errno
!= EADDRNOTAVAIL
)
296 warn("SIOCGIFDSTADDR_IN6");
297 memset(&ifr6
.ifr_addr
, 0, sizeof(ifr6
.ifr_addr
));
298 ifr6
.ifr_addr
.sin6_family
= AF_INET6
;
299 ifr6
.ifr_addr
.sin6_len
= sizeof(struct sockaddr_in6
);
301 sin6
= &ifr6
.ifr_addr
;
302 inet6_getscopeid(sin6
, INET6_IS_ADDR_LINKLOCAL
);
304 if (getnameinfo((struct sockaddr
*)sin6
, sin6
->sin6_len
,
305 hbuf
, sizeof(hbuf
), NULL
, 0, niflag
))
306 strlcpy(hbuf
, "", sizeof(hbuf
)); /* some message? */
307 printf(" -> %s", hbuf
);
311 if (prog_ioctl(s
, SIOCGIFNETMASK_IN6
, &ifr6
) == -1) {
312 if (errno
!= EADDRNOTAVAIL
)
313 warn("SIOCGIFNETMASK_IN6");
315 sin6
= &ifr6
.ifr_addr
;
316 printf(" prefixlen %d", prefix(&sin6
->sin6_addr
,
317 sizeof(struct in6_addr
)));
321 if (prog_ioctl(s
, SIOCGIFAFLAG_IN6
, &ifr6
) == -1) {
322 if (errno
!= EADDRNOTAVAIL
)
323 warn("SIOCGIFAFLAG_IN6");
325 if (ifr6
.ifr_ifru
.ifru_flags6
& IN6_IFF_ANYCAST
)
327 if (ifr6
.ifr_ifru
.ifru_flags6
& IN6_IFF_TENTATIVE
)
328 printf(" tentative");
329 if (ifr6
.ifr_ifru
.ifru_flags6
& IN6_IFF_DUPLICATED
)
330 printf(" duplicated");
331 if (ifr6
.ifr_ifru
.ifru_flags6
& IN6_IFF_DETACHED
)
333 if (ifr6
.ifr_ifru
.ifru_flags6
& IN6_IFF_DEPRECATED
)
334 printf(" deprecated");
335 if (ifr6
.ifr_ifru
.ifru_flags6
& IN6_IFF_AUTOCONF
)
337 if (ifr6
.ifr_ifru
.ifru_flags6
& IN6_IFF_TEMPORARY
)
338 printf(" temporary");
342 printf(" scopeid 0x%x", scopeid
);
345 struct in6_addrlifetime
*lifetime
;
347 lifetime
= &ifr6
.ifr_ifru
.ifru_lifetime
;
348 if (prog_ioctl(s
, SIOCGIFALIFETIME_IN6
, &ifr6
) == -1) {
349 if (errno
!= EADDRNOTAVAIL
)
350 warn("SIOCGIFALIFETIME_IN6");
351 } else if (lifetime
->ia6t_preferred
|| lifetime
->ia6t_expire
) {
352 time_t t
= time(NULL
);
354 if (lifetime
->ia6t_preferred
) {
356 (unsigned long)(lifetime
->ia6t_preferred
-
357 MIN(t
, lifetime
->ia6t_preferred
)));
362 if (lifetime
->ia6t_expire
) {
364 (unsigned long)(lifetime
->ia6t_expire
-
365 MIN(t
, lifetime
->ia6t_expire
)));
373 in6_status(prop_dictionary_t env
, prop_dictionary_t oenv
, bool force
)
375 struct ifaddrs
*ifap
, *ifa
;
376 struct in6_ifreq ifr
;
378 bool printprefs
= false;
380 if ((ifname
= getifname(env
)) == NULL
)
381 err(EXIT_FAILURE
, "%s: getifname", __func__
);
383 if (getifaddrs(&ifap
) != 0)
384 err(EXIT_FAILURE
, "getifaddrs");
385 printprefs
= ifa_any_preferences(ifname
, ifap
, AF_INET6
);
386 for (ifa
= ifap
; ifa
; ifa
= ifa
->ifa_next
) {
387 if (strcmp(ifname
, ifa
->ifa_name
) != 0)
389 if (ifa
->ifa_addr
->sa_family
!= AF_INET6
)
391 if (sizeof(ifr
.ifr_addr
) < ifa
->ifa_addr
->sa_len
)
394 memset(&ifr
, 0, sizeof(ifr
));
395 estrlcpy(ifr
.ifr_name
, ifa
->ifa_name
, sizeof(ifr
.ifr_name
));
396 memcpy(&ifr
.ifr_addr
, ifa
->ifa_addr
, ifa
->ifa_addr
->sa_len
);
397 in6_alias(ifname
, env
, oenv
, &ifr
);
399 ifa_print_preference(ifa
->ifa_name
, ifa
->ifa_addr
);
406 in6_pre_aifaddr(prop_dictionary_t env
, const struct afparam
*param
)
408 struct in6_aliasreq
*ifra
= param
->req
.buf
;
410 setia6eui64_impl(env
, ifra
);
411 setia6vltime_impl(env
, ifra
);
412 setia6pltime_impl(env
, ifra
);
413 setia6flags_impl(env
, ifra
);
414 inet6_putscopeid(&ifra
->ifra_addr
, INET6_IS_ADDR_LINKLOCAL
);
415 inet6_putscopeid(&ifra
->ifra_dstaddr
, INET6_IS_ADDR_LINKLOCAL
);
421 in6_commit_address(prop_dictionary_t env
, prop_dictionary_t oenv
)
423 struct in6_ifreq in6_ifr
= {
425 .sin6_family
= AF_INET6
,
426 .sin6_len
= sizeof(in6_ifr
.ifr_addr
),
429 {0xff, 0xff, 0xff, 0xff,
430 0xff, 0xff, 0xff, 0xff}
434 static struct sockaddr_in6 in6_defmask
= {
435 .sin6_family
= AF_INET6
,
436 .sin6_len
= sizeof(in6_defmask
),
438 .s6_addr
= {0xff, 0xff, 0xff, 0xff,
439 0xff, 0xff, 0xff, 0xff}
443 struct in6_aliasreq in6_ifra
= {
445 .sin6_family
= AF_INET6
,
446 .sin6_len
= sizeof(in6_ifra
.ifra_prefixmask
),
449 {0xff, 0xff, 0xff, 0xff,
450 0xff, 0xff, 0xff, 0xff}}},
452 .ia6t_pltime
= ND6_INFINITE_LIFETIME
453 , .ia6t_vltime
= ND6_INFINITE_LIFETIME
456 struct afparam in6param
= {
457 .req
= BUFPARAM(in6_ifra
)
458 , .dgreq
= BUFPARAM(in6_ifr
)
460 {.buf
= in6_ifr
.ifr_name
,
461 .buflen
= sizeof(in6_ifr
.ifr_name
)},
462 {.buf
= in6_ifra
.ifra_name
,
463 .buflen
= sizeof(in6_ifra
.ifra_name
)}
465 , .dgaddr
= BUFPARAM(in6_ifr
.ifr_addr
)
466 , .addr
= BUFPARAM(in6_ifra
.ifra_addr
)
467 , .dst
= BUFPARAM(in6_ifra
.ifra_dstaddr
)
468 , .brd
= BUFPARAM(in6_ifra
.ifra_broadaddr
)
469 , .mask
= BUFPARAM(in6_ifra
.ifra_prefixmask
)
470 , .aifaddr
= IFADDR_PARAM(SIOCAIFADDR_IN6
)
471 , .difaddr
= IFADDR_PARAM(SIOCDIFADDR_IN6
)
472 , .gifaddr
= IFADDR_PARAM(SIOCGIFADDR_IN6
)
473 , .defmask
= BUFPARAM(in6_defmask
)
474 , .pre_aifaddr
= in6_pre_aifaddr
476 commit_address(env
, oenv
, &in6param
);
480 in6_addr_tentative(struct ifaddrs
*ifa
)
483 struct in6_ifreq ifr
;
485 if ((s
= getsock(AF_INET6
)) == -1)
486 err(EXIT_FAILURE
, "%s: getsock", __func__
);
487 memset(&ifr
, 0, sizeof(ifr
));
488 strncpy(ifr
.ifr_name
, ifa
->ifa_name
, sizeof(ifr
.ifr_name
));
489 ifr
.ifr_addr
= *(struct sockaddr_in6
*)ifa
->ifa_addr
;
490 if (prog_ioctl(s
, SIOCGIFAFLAG_IN6
, &ifr
) == -1)
491 err(EXIT_FAILURE
, "SIOCGIFAFLAG_IN6");
492 return ifr
.ifr_ifru
.ifru_flags6
& IN6_IFF_TENTATIVE
? true : false;
496 in6_usage(prop_dictionary_t env
)
499 "\t[ anycast | -anycast ] [ deprecated | -deprecated ]\n"
500 "\t[ pltime n ] [ vltime n ] "
505 in6_constructor(void)
507 if (register_flag('L') != 0)
508 err(EXIT_FAILURE
, __func__
);
509 register_family(&in6af
);
510 usage_func_init(&usage
, in6_usage
);
511 register_usage(&usage
);
512 cmdloop_branch_init(&branch
[0], &ia6flags
.pk_parser
);
513 cmdloop_branch_init(&branch
[1], &inet6
.pk_parser
);
514 register_cmdloop_branch(&branch
[0]);
515 register_cmdloop_branch(&branch
[1]);