af_inet6.c revision 1.37 1 /* $NetBSD: af_inet6.c,v 1.37 2016/09/30 16:47:56 roy Exp $ */
2
3 /*
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
18 *
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
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __RCSID("$NetBSD: af_inet6.c,v 1.37 2016/09/30 16:47:56 roy Exp $");
35 #endif /* not lint */
36
37 #include <sys/param.h>
38 #include <sys/ioctl.h>
39 #include <sys/socket.h>
40
41 #include <net/if.h>
42 #include <netinet/in.h>
43 #include <netinet/in_var.h>
44 #include <netinet6/nd6.h>
45
46 #include <err.h>
47 #include <errno.h>
48 #include <ifaddrs.h>
49 #include <netdb.h>
50 #include <string.h>
51 #include <stdlib.h>
52 #include <stdio.h>
53 #include <util.h>
54
55 #include "env.h"
56 #include "extern.h"
57 #include "parse.h"
58 #include "extern.h"
59 #include "af_inetany.h"
60 #include "prog_ops.h"
61
62 static void in6_constructor(void) __attribute__((constructor));
63 static void in6_alias(struct ifaddrs *, prop_dictionary_t, prop_dictionary_t);
64 static void in6_commit_address(prop_dictionary_t, prop_dictionary_t);
65
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 *);
70
71 static int setia6lifetime(prop_dictionary_t, int64_t, time_t *, uint32_t *);
72
73 static void in6_status(prop_dictionary_t, prop_dictionary_t, bool);
74 static bool in6_addr_tentative(struct ifaddrs *ifa);
75 static bool in6_addr_tentative_or_detached(struct ifaddrs *ifa);
76
77 static struct usage_func usage;
78 static cmdloop_branch_t branch[2];
79
80 static const struct kwinst ia6flagskw[] = {
81 IFKW("anycast", IN6_IFF_ANYCAST)
82 , IFKW("deprecated", IN6_IFF_DEPRECATED)
83 };
84
85 static struct pinteger parse_pltime = PINTEGER_INITIALIZER(&parse_pltime,
86 "pltime", 0, NULL, "pltime", &command_root.pb_parser);
87
88 static struct pinteger parse_vltime = PINTEGER_INITIALIZER(&parse_vltime,
89 "vltime", 0, NULL, "vltime", &command_root.pb_parser);
90
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}
96 };
97
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);
102
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,
107 .af_addr_tentative_or_detached = in6_addr_tentative_or_detached
108 };
109
110 static int
111 prefix(void *val, int size)
112 {
113 u_char *pname = (u_char *)val;
114 int byte, bit, plen = 0;
115
116 for (byte = 0; byte < size; byte++, plen += 8)
117 if (pname[byte] != 0xff)
118 break;
119 if (byte == size)
120 return (plen);
121 for (bit = 7; bit != 0; bit--, plen++)
122 if (!(pname[byte] & (1 << bit)))
123 break;
124 for (; bit != 0; bit--)
125 if (pname[byte] & (1 << bit))
126 return(0);
127 byte++;
128 for (; byte < size; byte++)
129 if (pname[byte])
130 return(0);
131 return (plen);
132 }
133
134 int
135 setia6flags_impl(prop_dictionary_t env, struct in6_aliasreq *ifra)
136 {
137 int64_t ia6flag;
138
139 if (!prop_dictionary_get_int64(env, "ia6flag", &ia6flag)) {
140 errno = ENOENT;
141 return -1;
142 }
143
144 if (ia6flag < 0) {
145 ia6flag = -ia6flag;
146 ifra->ifra_flags &= ~ia6flag;
147 } else
148 ifra->ifra_flags |= ia6flag;
149 return 0;
150 }
151
152 int
153 setia6pltime_impl(prop_dictionary_t env, struct in6_aliasreq *ifra)
154 {
155 int64_t pltime;
156
157 if (!prop_dictionary_get_int64(env, "pltime", &pltime)) {
158 errno = ENOENT;
159 return -1;
160 }
161
162 return setia6lifetime(env, pltime,
163 &ifra->ifra_lifetime.ia6t_preferred,
164 &ifra->ifra_lifetime.ia6t_pltime);
165 }
166
167 int
168 setia6vltime_impl(prop_dictionary_t env, struct in6_aliasreq *ifra)
169 {
170 int64_t vltime;
171
172 if (!prop_dictionary_get_int64(env, "vltime", &vltime)) {
173 errno = ENOENT;
174 return -1;
175 }
176
177 return setia6lifetime(env, vltime,
178 &ifra->ifra_lifetime.ia6t_expire,
179 &ifra->ifra_lifetime.ia6t_vltime);
180 }
181
182 static int
183 setia6lifetime(prop_dictionary_t env, int64_t val, time_t *timep,
184 uint32_t *ivalp)
185 {
186 time_t t;
187 int af;
188
189 if ((af = getaf(env)) == -1 || af != AF_INET6) {
190 errx(EXIT_FAILURE,
191 "inet6 address lifetime not allowed for the AF");
192 }
193
194 t = time(NULL);
195 *timep = t + val;
196 *ivalp = val;
197 return 0;
198 }
199
200 int
201 setia6eui64_impl(prop_dictionary_t env, struct in6_aliasreq *ifra)
202 {
203 char buf[2][80];
204 struct ifaddrs *ifap, *ifa;
205 const struct sockaddr_in6 *sin6 = NULL;
206 const struct in6_addr *lladdr = NULL;
207 struct in6_addr *in6;
208 const char *ifname;
209 bool doit = false;
210 int af;
211
212 if (!prop_dictionary_get_bool(env, "eui64", &doit) || !doit) {
213 errno = ENOENT;
214 return -1;
215 }
216
217 if ((ifname = getifname(env)) == NULL)
218 return -1;
219
220 af = getaf(env);
221 if (af != AF_INET6) {
222 errx(EXIT_FAILURE,
223 "eui64 address modifier not allowed for the AF");
224 }
225 in6 = &ifra->ifra_addr.sin6_addr;
226 if (memcmp(&in6addr_any.s6_addr[8], &in6->s6_addr[8], 8) != 0) {
227 union {
228 struct sockaddr_in6 sin6;
229 struct sockaddr sa;
230 } any = {.sin6 = {.sin6_family = AF_INET6}};
231 memcpy(&any.sin6.sin6_addr, &in6addr_any,
232 sizeof(any.sin6.sin6_addr));
233 (void)sockaddr_snprintf(buf[0], sizeof(buf[0]), "%a%%S",
234 &any.sa);
235 (void)sockaddr_snprintf(buf[1], sizeof(buf[1]), "%a%%S",
236 (const struct sockaddr *)&ifra->ifra_addr);
237 errx(EXIT_FAILURE, "interface index is already filled, %s | %s",
238 buf[0], buf[1]);
239 }
240 if (getifaddrs(&ifap) != 0)
241 err(EXIT_FAILURE, "getifaddrs");
242 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
243 if (ifa->ifa_addr->sa_family == AF_INET6 &&
244 strcmp(ifa->ifa_name, ifname) == 0) {
245 sin6 = (const struct sockaddr_in6 *)ifa->ifa_addr;
246 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
247 lladdr = &sin6->sin6_addr;
248 break;
249 }
250 }
251 }
252 if (lladdr == NULL)
253 errx(EXIT_FAILURE, "could not determine link local address");
254
255 memcpy(&in6->s6_addr[8], &lladdr->s6_addr[8], 8);
256
257 freeifaddrs(ifap);
258 return 0;
259 }
260
261 /* XXX not really an alias */
262 void
263 in6_alias(struct ifaddrs *ifa, prop_dictionary_t env, prop_dictionary_t oenv)
264 {
265 struct sockaddr_in6 *sin6;
266 char hbuf[NI_MAXHOST];
267 u_int32_t scopeid;
268 const int niflag = Nflag ? 0 : NI_NUMERICHOST;
269 char fbuf[1024];
270
271 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
272 inet6_getscopeid(sin6, INET6_IS_ADDR_LINKLOCAL);
273 scopeid = sin6->sin6_scope_id;
274 if (getnameinfo((const struct sockaddr *)sin6, sin6->sin6_len,
275 hbuf, sizeof(hbuf), NULL, 0, niflag))
276 strlcpy(hbuf, "", sizeof(hbuf)); /* some message? */
277 printf("\tinet6 %s", hbuf);
278 inet6_putscopeid(sin6, INET6_IS_ADDR_LINKLOCAL);
279
280 if (ifa->ifa_flags & IFF_POINTOPOINT) {
281 sin6 = (struct sockaddr_in6 *)ifa->ifa_dstaddr;
282 inet6_getscopeid(sin6, INET6_IS_ADDR_LINKLOCAL);
283 hbuf[0] = '\0';
284 if (getnameinfo((struct sockaddr *)sin6, sin6->sin6_len,
285 hbuf, sizeof(hbuf), NULL, 0, niflag))
286 strlcpy(hbuf, "", sizeof(hbuf)); /* some message? */
287 printf(" -> %s", hbuf);
288 }
289
290 sin6 = (struct sockaddr_in6 *)ifa->ifa_netmask;
291 printf(" prefixlen %d", prefix(&sin6->sin6_addr,
292 sizeof(struct in6_addr)));
293
294 (void)snprintb(fbuf, sizeof(fbuf), IN6_IFFBITS, ifa->ifa_addrflags);
295 printf(" flags %s", fbuf);
296
297 if (scopeid)
298 printf(" scopeid 0x%x", scopeid);
299
300 if (get_flag('L')) {
301 int s;
302 struct in6_ifreq ifr6;
303 struct in6_addrlifetime *lifetime;
304
305 if ((s = getsock(AF_INET6)) == -1) {
306 if (errno == EAFNOSUPPORT)
307 return;
308 err(EXIT_FAILURE, "socket");
309 }
310
311 memset(&ifr6, 0, sizeof(ifr6));
312 estrlcpy(ifr6.ifr_name, ifa->ifa_name, sizeof(ifr6.ifr_name));
313 memcpy(&ifr6.ifr_addr, ifa->ifa_addr, ifa->ifa_addr->sa_len);
314 lifetime = &ifr6.ifr_ifru.ifru_lifetime;
315 if (prog_ioctl(s, SIOCGIFALIFETIME_IN6, &ifr6) == -1) {
316 if (errno != EADDRNOTAVAIL)
317 warn("SIOCGIFALIFETIME_IN6");
318 } else if (lifetime->ia6t_preferred || lifetime->ia6t_expire) {
319 time_t t = time(NULL);
320 printf(" pltime ");
321 if (lifetime->ia6t_preferred) {
322 printf("%lu",
323 (unsigned long)(lifetime->ia6t_preferred -
324 MIN(t, lifetime->ia6t_preferred)));
325 } else
326 printf("infty");
327
328 printf(" vltime ");
329 if (lifetime->ia6t_expire) {
330 printf("%lu",
331 (unsigned long)(lifetime->ia6t_expire -
332 MIN(t, lifetime->ia6t_expire)));
333 } else
334 printf("infty");
335 }
336 }
337 }
338
339 static void
340 in6_status(prop_dictionary_t env, prop_dictionary_t oenv, bool force)
341 {
342 struct ifaddrs *ifap, *ifa;
343 const char *ifname;
344 bool printprefs = false;
345
346 if ((ifname = getifname(env)) == NULL)
347 err(EXIT_FAILURE, "%s: getifname", __func__);
348
349 if (getifaddrs(&ifap) != 0)
350 err(EXIT_FAILURE, "getifaddrs");
351 printprefs = ifa_any_preferences(ifname, ifap, AF_INET6);
352 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
353 if (strcmp(ifname, ifa->ifa_name) != 0)
354 continue;
355 if (ifa->ifa_addr->sa_family != AF_INET6)
356 continue;
357 in6_alias(ifa, env, oenv);
358 if (printprefs)
359 ifa_print_preference(ifa->ifa_name, ifa->ifa_addr);
360 printf("\n");
361 }
362 freeifaddrs(ifap);
363 }
364
365 static int
366 in6_pre_aifaddr(prop_dictionary_t env, const struct afparam *param)
367 {
368 struct in6_aliasreq *ifra = param->req.buf;
369
370 setia6eui64_impl(env, ifra);
371 setia6vltime_impl(env, ifra);
372 setia6pltime_impl(env, ifra);
373 setia6flags_impl(env, ifra);
374 inet6_putscopeid(&ifra->ifra_addr, INET6_IS_ADDR_LINKLOCAL);
375 inet6_putscopeid(&ifra->ifra_dstaddr, INET6_IS_ADDR_LINKLOCAL);
376
377 return 0;
378 }
379
380 static void
381 in6_commit_address(prop_dictionary_t env, prop_dictionary_t oenv)
382 {
383 struct in6_ifreq in6_ifr = {
384 .ifr_addr = {
385 .sin6_family = AF_INET6,
386 .sin6_len = sizeof(in6_ifr.ifr_addr),
387 .sin6_addr = {
388 .s6_addr =
389 {0xff, 0xff, 0xff, 0xff,
390 0xff, 0xff, 0xff, 0xff}
391 }
392 }
393 };
394 static struct sockaddr_in6 in6_defmask = {
395 .sin6_family = AF_INET6,
396 .sin6_len = sizeof(in6_defmask),
397 .sin6_addr = {
398 .s6_addr = {0xff, 0xff, 0xff, 0xff,
399 0xff, 0xff, 0xff, 0xff}
400 }
401 };
402
403 struct in6_aliasreq in6_ifra = {
404 .ifra_prefixmask = {
405 .sin6_family = AF_INET6,
406 .sin6_len = sizeof(in6_ifra.ifra_prefixmask),
407 .sin6_addr = {
408 .s6_addr =
409 {0xff, 0xff, 0xff, 0xff,
410 0xff, 0xff, 0xff, 0xff}}},
411 .ifra_lifetime = {
412 .ia6t_pltime = ND6_INFINITE_LIFETIME
413 , .ia6t_vltime = ND6_INFINITE_LIFETIME
414 }
415 };
416 struct afparam in6param = {
417 .req = BUFPARAM(in6_ifra)
418 , .dgreq = BUFPARAM(in6_ifr)
419 , .name = {
420 {.buf = in6_ifr.ifr_name,
421 .buflen = sizeof(in6_ifr.ifr_name)},
422 {.buf = in6_ifra.ifra_name,
423 .buflen = sizeof(in6_ifra.ifra_name)}
424 }
425 , .dgaddr = BUFPARAM(in6_ifr.ifr_addr)
426 , .addr = BUFPARAM(in6_ifra.ifra_addr)
427 , .dst = BUFPARAM(in6_ifra.ifra_dstaddr)
428 , .brd = BUFPARAM(in6_ifra.ifra_broadaddr)
429 , .mask = BUFPARAM(in6_ifra.ifra_prefixmask)
430 , .aifaddr = IFADDR_PARAM(SIOCAIFADDR_IN6)
431 , .difaddr = IFADDR_PARAM(SIOCDIFADDR_IN6)
432 , .gifaddr = IFADDR_PARAM(SIOCGIFADDR_IN6)
433 , .defmask = BUFPARAM(in6_defmask)
434 , .pre_aifaddr = in6_pre_aifaddr
435 };
436 commit_address(env, oenv, &in6param);
437 }
438
439 static bool
440 in6_addr_flags(struct ifaddrs *ifa, int flags)
441 {
442 int s;
443 struct in6_ifreq ifr;
444
445 if ((s = getsock(AF_INET6)) == -1)
446 err(EXIT_FAILURE, "%s: getsock", __func__);
447 memset(&ifr, 0, sizeof(ifr));
448 estrlcpy(ifr.ifr_name, ifa->ifa_name, sizeof(ifr.ifr_name));
449 ifr.ifr_addr = *(struct sockaddr_in6 *)ifa->ifa_addr;
450 if (prog_ioctl(s, SIOCGIFAFLAG_IN6, &ifr) == -1)
451 err(EXIT_FAILURE, "SIOCGIFAFLAG_IN6");
452 return ifr.ifr_ifru.ifru_flags6 & flags ? true : false;
453 }
454
455 static bool
456 in6_addr_tentative(struct ifaddrs *ifa)
457 {
458
459 return in6_addr_flags(ifa, IN6_IFF_TENTATIVE);
460 }
461
462 static bool
463 in6_addr_tentative_or_detached(struct ifaddrs *ifa)
464 {
465
466 return in6_addr_flags(ifa, IN6_IFF_TENTATIVE | IN6_IFF_DETACHED);
467 }
468
469 static void
470 in6_usage(prop_dictionary_t env)
471 {
472 fprintf(stderr,
473 "\t[ anycast | -anycast ] [ deprecated | -deprecated ]\n"
474 "\t[ pltime n ] [ vltime n ] "
475 "[ eui64 ]\n");
476 }
477
478 static void
479 in6_constructor(void)
480 {
481 if (register_flag('L') != 0)
482 err(EXIT_FAILURE, __func__);
483 register_family(&in6af);
484 usage_func_init(&usage, in6_usage);
485 register_usage(&usage);
486 cmdloop_branch_init(&branch[0], &ia6flags.pk_parser);
487 cmdloop_branch_init(&branch[1], &inet6.pk_parser);
488 register_cmdloop_branch(&branch[0]);
489 register_cmdloop_branch(&branch[1]);
490 }
491