1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
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/param.h>
33 #include <sys/ioctl.h>
34 #include <sys/socket.h>
35 #include <net/if.h>
36
37 #include <err.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42 #include <time.h>
43 #include <ifaddrs.h>
44
45 #include <arpa/inet.h>
46
47 #include <netinet/in.h>
48 #include <netinet/in_var.h>
49 #include <arpa/inet.h>
50 #include <netdb.h>
51
52 #include <netinet6/nd6.h> /* Define ND6_INFINITE_LIFETIME */
53
54 #include "ifconfig.h"
55 #include "ifconfig_netlink.h"
56
57 #ifndef WITHOUT_NETLINK
58 struct in6_px {
59 struct in6_addr addr;
60 int plen;
61 bool set;
62 };
63 struct in6_pdata {
64 struct in6_px addr;
65 struct in6_px dst_addr;
66 struct in6_addrlifetime lifetime;
67 uint32_t flags;
68 uint32_t vhid;
69 };
70
71 static struct in6_pdata in6_del;
72 static struct in6_pdata in6_add = {
73 .lifetime = { 0, 0, ND6_INFINITE_LIFETIME, ND6_INFINITE_LIFETIME },
74 };
75 #else
76 static struct in6_ifreq in6_ridreq;
77 static struct in6_aliasreq in6_addreq =
78 { .ifra_flags = 0,
79 .ifra_lifetime = { 0, 0, ND6_INFINITE_LIFETIME, ND6_INFINITE_LIFETIME } };
80 #endif
81 static int ip6lifetime;
82
83 #ifdef WITHOUT_NETLINK
84 static int prefix(void *, int);
85 #endif
86 static char *sec2str(time_t);
87 static int explicit_prefix = 0;
88 extern char *f_inet6, *f_addr;
89
90 extern void setnd6flags(if_ctx *, const char *, int);
91 extern void setnd6defif(if_ctx *,const char *, int);
92 extern void nd6_status(if_ctx *);
93
94 static char addr_buf[NI_MAXHOST]; /*for getnameinfo()*/
95
96 static void
setifprefixlen(if_ctx * ctx __netlink_unused,const char * addr,int dummy __unused)97 setifprefixlen(if_ctx *ctx __netlink_unused, const char *addr, int dummy __unused)
98 {
99 #ifdef WITHOUT_NETLINK
100 const struct afswtch *afp = ctx->afp;
101
102 if (afp->af_getprefix != NULL)
103 afp->af_getprefix(addr, MASK);
104 #else
105 int plen = strtol(addr, NULL, 10);
106
107 if ((plen < 0) || (plen > 128))
108 errx(1, "%s: bad value", addr);
109 in6_add.addr.plen = plen;
110 #endif
111 explicit_prefix = 1;
112 }
113
114 static void
setip6flags(if_ctx * ctx,const char * dummyaddr __unused,int flag)115 setip6flags(if_ctx *ctx, const char *dummyaddr __unused, int flag)
116 {
117 const struct afswtch *afp = ctx->afp;
118
119 if (afp->af_af != AF_INET6)
120 err(1, "address flags can be set only for inet6 addresses");
121
122 #ifdef WITHOUT_NETLINK
123 if (flag < 0)
124 in6_addreq.ifra_flags &= ~(-flag);
125 else
126 in6_addreq.ifra_flags |= flag;
127 #else
128 if (flag < 0)
129 in6_add.flags &= ~(-flag);
130 else
131 in6_add.flags |= flag;
132 #endif
133 }
134
135 static void
setip6lifetime(if_ctx * ctx,const char * cmd,const char * val)136 setip6lifetime(if_ctx *ctx, const char *cmd, const char *val)
137 {
138 const struct afswtch *afp = ctx->afp;
139 struct timespec now;
140 time_t newval;
141 char *ep;
142 #ifdef WITHOUT_NETLINK
143 struct in6_addrlifetime *lifetime = &in6_addreq.ifra_lifetime;
144 #else
145 struct in6_addrlifetime *lifetime = &in6_add.lifetime;
146 #endif
147
148 clock_gettime(CLOCK_MONOTONIC_FAST, &now);
149 newval = (time_t)strtoul(val, &ep, 0);
150 if (val == ep)
151 errx(1, "invalid %s", cmd);
152 if (afp->af_af != AF_INET6)
153 errx(1, "%s not allowed for the AF", cmd);
154 if (strcmp(cmd, "vltime") == 0) {
155 lifetime->ia6t_expire = now.tv_sec + newval;
156 lifetime->ia6t_vltime = newval;
157 } else if (strcmp(cmd, "pltime") == 0) {
158 lifetime->ia6t_preferred = now.tv_sec + newval;
159 lifetime->ia6t_pltime = newval;
160 }
161 }
162
163 static void
setip6pltime(if_ctx * ctx,const char * seconds,int dummy __unused)164 setip6pltime(if_ctx *ctx, const char *seconds, int dummy __unused)
165 {
166 setip6lifetime(ctx, "pltime", seconds);
167 }
168
169 static void
setip6vltime(if_ctx * ctx,const char * seconds,int dummy __unused)170 setip6vltime(if_ctx *ctx, const char *seconds, int dummy __unused)
171 {
172 setip6lifetime(ctx, "vltime", seconds);
173 }
174
175 static void
setip6eui64(if_ctx * ctx,const char * cmd,int dummy __unused)176 setip6eui64(if_ctx *ctx, const char *cmd, int dummy __unused)
177 {
178 const struct afswtch *afp = ctx->afp;
179 struct ifaddrs *ifap, *ifa;
180 const struct sockaddr_in6 *sin6 = NULL;
181 const struct in6_addr *lladdr = NULL;
182 struct in6_addr *in6;
183
184 if (afp->af_af != AF_INET6)
185 errx(EXIT_FAILURE, "%s not allowed for the AF", cmd);
186 #ifdef WITHOUT_NETLINK
187 in6 = (struct in6_addr *)&in6_addreq.ifra_addr.sin6_addr;
188 #else
189 in6 = &in6_add.addr.addr;
190 #endif
191 if (memcmp(&in6addr_any.s6_addr[8], &in6->s6_addr[8], 8) != 0)
192 errx(EXIT_FAILURE, "interface index is already filled");
193 if (getifaddrs(&ifap) != 0)
194 err(EXIT_FAILURE, "getifaddrs");
195 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
196 if (ifa->ifa_addr->sa_family == AF_INET6 &&
197 strcmp(ifa->ifa_name, ctx->ifname) == 0) {
198 sin6 = (const struct sockaddr_in6 *)satosin6(ifa->ifa_addr);
199 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
200 lladdr = &sin6->sin6_addr;
201 break;
202 }
203 }
204 }
205 if (!lladdr)
206 errx(EXIT_FAILURE, "could not determine link local address");
207
208 memcpy(&in6->s6_addr[8], &lladdr->s6_addr[8], 8);
209
210 freeifaddrs(ifap);
211 }
212
213 static void
print_addr(struct sockaddr_in6 * sin)214 print_addr(struct sockaddr_in6 *sin)
215 {
216 int error, n_flags;
217
218 if (f_addr != NULL && strcmp(f_addr, "fqdn") == 0)
219 n_flags = 0;
220 else if (f_addr != NULL && strcmp(f_addr, "host") == 0)
221 n_flags = NI_NOFQDN;
222 else
223 n_flags = NI_NUMERICHOST;
224 error = getnameinfo((struct sockaddr *)sin, sin->sin6_len,
225 addr_buf, sizeof(addr_buf), NULL, 0,
226 n_flags);
227 if (error != 0)
228 inet_ntop(AF_INET6, &sin->sin6_addr, addr_buf,
229 sizeof(addr_buf));
230 printf("\tinet6 %s", addr_buf);
231 }
232
233 static void
print_p2p(struct sockaddr_in6 * sin)234 print_p2p(struct sockaddr_in6 *sin)
235 {
236 int error;
237
238 error = getnameinfo((struct sockaddr *)sin, sin->sin6_len, addr_buf,
239 sizeof(addr_buf), NULL, 0, NI_NUMERICHOST);
240
241 if (error != 0)
242 inet_ntop(AF_INET6, &sin->sin6_addr, addr_buf, sizeof(addr_buf));
243 printf(" --> %s", addr_buf);
244 }
245
246 static void
print_mask(int plen)247 print_mask(int plen)
248 {
249 if (f_inet6 != NULL && strcmp(f_inet6, "cidr") == 0)
250 printf("/%d", plen);
251 else
252 printf(" prefixlen %d", plen);
253 }
254
255 static void
print_flags(int flags6)256 print_flags(int flags6)
257 {
258 if ((flags6 & IN6_IFF_ANYCAST) != 0)
259 printf(" anycast");
260 if ((flags6 & IN6_IFF_TENTATIVE) != 0)
261 printf(" tentative");
262 if ((flags6 & IN6_IFF_DUPLICATED) != 0)
263 printf(" duplicated");
264 if ((flags6 & IN6_IFF_DETACHED) != 0)
265 printf(" detached");
266 if ((flags6 & IN6_IFF_DEPRECATED) != 0)
267 printf(" deprecated");
268 if ((flags6 & IN6_IFF_AUTOCONF) != 0)
269 printf(" autoconf");
270 if ((flags6 & IN6_IFF_TEMPORARY) != 0)
271 printf(" temporary");
272 if ((flags6 & IN6_IFF_PREFER_SOURCE) != 0)
273 printf(" prefer_source");
274
275 }
276
277 static void
print_lifetime(const char * prepend,time_t px_time,struct timespec * now)278 print_lifetime(const char *prepend, time_t px_time, struct timespec *now)
279 {
280 printf(" %s", prepend);
281 if (px_time == 0)
282 printf(" infty");
283
284 printf(" %s", px_time < now->tv_sec ? "0" : sec2str(px_time - now->tv_sec));
285 }
286
287 #ifdef WITHOUT_NETLINK
288 static void
in6_status(if_ctx * ctx,const struct ifaddrs * ifa)289 in6_status(if_ctx *ctx, const struct ifaddrs *ifa)
290 {
291 struct sockaddr_in6 *sin, null_sin = {};
292 struct in6_ifreq ifr6;
293 int s6;
294 u_int32_t flags6;
295 struct in6_addrlifetime lifetime;
296
297 sin = satosin6(ifa->ifa_addr);
298 if (sin == NULL)
299 return;
300
301 strlcpy(ifr6.ifr_name, ctx->ifname, sizeof(ifr6.ifr_name));
302 if ((s6 = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
303 warn("socket(AF_INET6,SOCK_DGRAM)");
304 return;
305 }
306 ifr6.ifr_addr = *sin;
307 if (ioctl(s6, SIOCGIFAFLAG_IN6, &ifr6) < 0) {
308 warn("ioctl(SIOCGIFAFLAG_IN6)");
309 close(s6);
310 return;
311 }
312 flags6 = ifr6.ifr_ifru.ifru_flags6;
313 memset(&lifetime, 0, sizeof(lifetime));
314 ifr6.ifr_addr = *sin;
315 if (ioctl(s6, SIOCGIFALIFETIME_IN6, &ifr6) < 0) {
316 warn("ioctl(SIOCGIFALIFETIME_IN6)");
317 close(s6);
318 return;
319 }
320 lifetime = ifr6.ifr_ifru.ifru_lifetime;
321 close(s6);
322
323 print_addr(sin);
324
325 if (ifa->ifa_flags & IFF_POINTOPOINT) {
326 sin = satosin6(ifa->ifa_dstaddr);
327 /*
328 * some of the interfaces do not have valid destination
329 * address.
330 */
331 if (sin != NULL && sin->sin6_family == AF_INET6)
332 print_p2p(sin);
333 }
334
335 sin = satosin6(ifa->ifa_netmask);
336 if (sin == NULL)
337 sin = &null_sin;
338 print_mask(prefix(&sin->sin6_addr, sizeof(struct in6_addr)));
339
340 print_flags(flags6);
341
342 if ((satosin6(ifa->ifa_addr))->sin6_scope_id)
343 printf(" scopeid 0x%x",
344 (satosin6(ifa->ifa_addr))->sin6_scope_id);
345
346 if (ip6lifetime && (lifetime.ia6t_preferred || lifetime.ia6t_expire)) {
347 struct timespec now;
348
349 clock_gettime(CLOCK_MONOTONIC_FAST, &now);
350 print_lifetime("pltime", lifetime.ia6t_preferred, &now);
351 print_lifetime("vltime", lifetime.ia6t_expire, &now);
352 }
353
354 print_vhid(ifa);
355
356 putchar('\n');
357 }
358
359 #else
360 static void
show_lifetime(struct ifa_cacheinfo * ci)361 show_lifetime(struct ifa_cacheinfo *ci)
362 {
363 struct timespec now;
364 uint32_t pl, vl;
365
366 if (ci == NULL)
367 return;
368
369 int count = ci->ifa_prefered != ND6_INFINITE_LIFETIME;
370 count += ci->ifa_valid != ND6_INFINITE_LIFETIME;
371 if (count == 0)
372 return;
373
374 pl = (ci->ifa_prefered == ND6_INFINITE_LIFETIME) ? 0 : ci->ifa_prefered;
375 vl = (ci->ifa_valid == ND6_INFINITE_LIFETIME) ? 0 : ci->ifa_valid;
376
377 clock_gettime(CLOCK_MONOTONIC_FAST, &now);
378 print_lifetime("pltime",
379 pl + (ip6lifetime ? ci->tstamp / 1000 : now.tv_sec), &now);
380 print_lifetime("vltime",
381 vl + (ip6lifetime ? ci->tstamp / 1000 : now.tv_sec), &now);
382 }
383
384 static void
in6_status_nl(if_ctx * ctx __unused,if_link_t * link __unused,if_addr_t * ifa)385 in6_status_nl(if_ctx *ctx __unused, if_link_t *link __unused, if_addr_t *ifa)
386 {
387 int plen = ifa->ifa_prefixlen;
388 uint32_t scopeid;
389
390 if (ifa->ifa_local == NULL) {
391 /* Non-P2P address */
392 scopeid = satosin6(ifa->ifa_address)->sin6_scope_id;
393 print_addr(satosin6(ifa->ifa_address));
394 } else {
395 scopeid = satosin6(ifa->ifa_local)->sin6_scope_id;
396 print_addr(satosin6(ifa->ifa_local));
397 print_p2p(satosin6(ifa->ifa_address));
398 }
399
400 print_mask(plen);
401 print_flags(ifa->ifaf_flags);
402
403 if (scopeid != 0)
404 printf(" scopeid 0x%x", scopeid);
405
406 show_lifetime(ifa->ifa_cacheinfo);
407
408 if (ifa->ifaf_vhid != 0)
409 printf(" vhid %d", ifa->ifaf_vhid);
410
411 putchar('\n');
412 }
413
414 static struct in6_px *sin6tab_nl[] = {
415 &in6_del.addr, /* RIDADDR */
416 &in6_add.addr, /* ADDR */
417 NULL, /* MASK */
418 &in6_add.dst_addr, /* DSTADDR*/
419 };
420
421 static void
in6_copyaddr(if_ctx * ctx __unused,int to,int from)422 in6_copyaddr(if_ctx *ctx __unused, int to, int from)
423 {
424 sin6tab_nl[to]->addr = sin6tab_nl[from]->addr;
425 sin6tab_nl[to]->set = sin6tab_nl[from]->set;
426 }
427
428 static void
in6_getaddr(const char * addr_str,int which)429 in6_getaddr(const char *addr_str, int which)
430 {
431 struct in6_px *px = sin6tab_nl[which];
432
433 if (which == MASK)
434 errx(1, "netmask: invalid option for inet6");
435 if (which == BRDADDR)
436 errx(1, "broadcast: invalid option for inet6");
437
438 px->set = true;
439 px->plen = 128;
440 if (which == ADDR) {
441 char *p = NULL;
442 if((p = strrchr(addr_str, '/')) != NULL) {
443 *p = '\0';
444 int plen = strtol(p + 1, NULL, 10);
445 if (plen < 0 || plen > 128)
446 errx(1, "%s: bad value", p + 1);
447 px->plen = plen;
448 explicit_prefix = 1;
449 }
450 }
451
452 struct addrinfo hints = { .ai_family = AF_INET6 };
453 struct addrinfo *res;
454
455 int error = getaddrinfo(addr_str, NULL, &hints, &res);
456 if (error != 0) {
457 if (inet_pton(AF_INET6, addr_str, &px->addr) != 1)
458 errx(1, "%s: bad value", addr_str);
459 } else {
460 struct sockaddr_in6 *sin6;
461
462 sin6 = (struct sockaddr_in6 *)(void *)res->ai_addr;
463 px->addr = sin6->sin6_addr;
464 freeaddrinfo(res);
465 }
466 }
467
468 static int
in6_exec_nl(if_ctx * ctx,unsigned long action,void * data)469 in6_exec_nl(if_ctx *ctx, unsigned long action, void *data)
470 {
471 struct in6_pdata *pdata = (struct in6_pdata *)data;
472 struct snl_writer nw = {};
473
474 snl_init_writer(ctx->io_ss, &nw);
475 struct nlmsghdr *hdr = snl_create_msg_request(&nw, action);
476 struct ifaddrmsg *ifahdr = snl_reserve_msg_object(&nw, struct ifaddrmsg);
477
478 ifahdr->ifa_family = AF_INET6;
479 ifahdr->ifa_prefixlen = pdata->addr.plen;
480 ifahdr->ifa_index = if_nametoindex_nl(ctx->io_ss, ctx->ifname);
481
482 snl_add_msg_attr_ip6(&nw, IFA_LOCAL, &pdata->addr.addr);
483 if (action == NL_RTM_NEWADDR && pdata->dst_addr.set)
484 snl_add_msg_attr_ip6(&nw, IFA_ADDRESS, &pdata->dst_addr.addr);
485
486 struct ifa_cacheinfo ci = {
487 .ifa_prefered = pdata->lifetime.ia6t_pltime,
488 .ifa_valid = pdata->lifetime.ia6t_vltime,
489 };
490 snl_add_msg_attr(&nw, IFA_CACHEINFO, sizeof(ci), &ci);
491
492 int off = snl_add_msg_attr_nested(&nw, IFA_FREEBSD);
493 snl_add_msg_attr_u32(&nw, IFAF_FLAGS, pdata->flags);
494 if (pdata->vhid != 0)
495 snl_add_msg_attr_u32(&nw, IFAF_VHID, pdata->vhid);
496 snl_end_attr_nested(&nw, off);
497
498 if (! (hdr = snl_finalize_msg(&nw)) || !snl_send_message(ctx->io_ss, hdr))
499 return (0);
500
501 struct snl_errmsg_data e = {};
502 snl_read_reply_code(ctx->io_ss, hdr->nlmsg_seq, &e);
503
504 return (e.error);
505 }
506 #endif
507
508 #ifdef WITHOUT_NETLINK
509 static struct sockaddr_in6 *sin6tab[] = {
510 &in6_ridreq.ifr_addr, &in6_addreq.ifra_addr,
511 &in6_addreq.ifra_prefixmask, &in6_addreq.ifra_dstaddr
512 };
513
514 static void
in6_copyaddr(if_ctx * ctx __unused,int to,int from)515 in6_copyaddr(if_ctx *ctx __unused, int to, int from)
516 {
517 memcpy(sin6tab[to], sin6tab[from], sizeof(struct sockaddr_in6));
518 }
519
520 static void
in6_getprefix(const char * plen,int which)521 in6_getprefix(const char *plen, int which)
522 {
523 struct sockaddr_in6 *sin = sin6tab[which];
524 u_char *cp;
525 int len = atoi(plen);
526
527 if ((len < 0) || (len > 128))
528 errx(1, "%s: bad value", plen);
529 sin->sin6_len = sizeof(*sin);
530 if (which != MASK)
531 sin->sin6_family = AF_INET6;
532 if ((len == 0) || (len == 128)) {
533 memset(&sin->sin6_addr, 0xff, sizeof(struct in6_addr));
534 return;
535 }
536 memset((void *)&sin->sin6_addr, 0x00, sizeof(sin->sin6_addr));
537 for (cp = (u_char *)&sin->sin6_addr; len > 7; len -= 8)
538 *cp++ = 0xff;
539 *cp = 0xff << (8 - len);
540 }
541
542 static void
in6_getaddr(const char * s,int which)543 in6_getaddr(const char *s, int which)
544 {
545 struct sockaddr_in6 *sin = sin6tab[which];
546 struct addrinfo hints, *res;
547 int error = -1;
548
549 sin->sin6_len = sizeof(*sin);
550 if (which != MASK)
551 sin->sin6_family = AF_INET6;
552
553 if (which == ADDR) {
554 char *p = NULL;
555 if((p = strrchr(s, '/')) != NULL) {
556 *p = '\0';
557 in6_getprefix(p + 1, MASK);
558 explicit_prefix = 1;
559 }
560 }
561
562 if (sin->sin6_family == AF_INET6) {
563 bzero(&hints, sizeof(struct addrinfo));
564 hints.ai_family = AF_INET6;
565 error = getaddrinfo(s, NULL, &hints, &res);
566 if (error != 0) {
567 if (inet_pton(AF_INET6, s, &sin->sin6_addr) != 1)
568 errx(1, "%s: bad value", s);
569 } else {
570 bcopy(res->ai_addr, sin, res->ai_addrlen);
571 freeaddrinfo(res);
572 }
573 }
574 }
575
576 static int
prefix(void * val,int size)577 prefix(void *val, int size)
578 {
579 u_char *name = (u_char *)val;
580 int byte, bit, plen = 0;
581
582 for (byte = 0; byte < size; byte++, plen += 8)
583 if (name[byte] != 0xff)
584 break;
585 if (byte == size)
586 return (plen);
587 for (bit = 7; bit != 0; bit--, plen++)
588 if (!(name[byte] & (1 << bit)))
589 break;
590 for (; bit != 0; bit--)
591 if (name[byte] & (1 << bit))
592 return(0);
593 byte++;
594 for (; byte < size; byte++)
595 if (name[byte])
596 return(0);
597 return (plen);
598 }
599 #endif
600
601 static char *
sec2str(time_t total)602 sec2str(time_t total)
603 {
604 static char result[256];
605 int days, hours, mins, secs;
606 int first = 1;
607 char *p = result;
608
609 if (0) {
610 days = total / 3600 / 24;
611 hours = (total / 3600) % 24;
612 mins = (total / 60) % 60;
613 secs = total % 60;
614
615 if (days) {
616 first = 0;
617 p += sprintf(p, "%dd", days);
618 }
619 if (!first || hours) {
620 first = 0;
621 p += sprintf(p, "%dh", hours);
622 }
623 if (!first || mins) {
624 first = 0;
625 p += sprintf(p, "%dm", mins);
626 }
627 sprintf(p, "%ds", secs);
628 } else
629 sprintf(result, "%lu", (unsigned long)total);
630
631 return(result);
632 }
633
634 static void
in6_postproc(if_ctx * ctx,int newaddr __unused,int ifflags __unused)635 in6_postproc(if_ctx *ctx, int newaddr __unused,
636 int ifflags __unused)
637 {
638 if (explicit_prefix == 0) {
639 /* Aggregatable address architecture defines all prefixes
640 are 64. So, it is convenient to set prefixlen to 64 if
641 it is not specified. */
642 setifprefixlen(ctx, "64", 0);
643 /* in6_getprefix("64", MASK) if MASK is available here... */
644 }
645 }
646
647 static void
in6_status_tunnel(if_ctx * ctx)648 in6_status_tunnel(if_ctx *ctx)
649 {
650 char src[NI_MAXHOST];
651 char dst[NI_MAXHOST];
652 struct in6_ifreq in6_ifr;
653 const struct sockaddr *sa = (const struct sockaddr *) &in6_ifr.ifr_addr;
654
655 memset(&in6_ifr, 0, sizeof(in6_ifr));
656 strlcpy(in6_ifr.ifr_name, ctx->ifname, sizeof(in6_ifr.ifr_name));
657
658 if (ioctl_ctx(ctx, SIOCGIFPSRCADDR_IN6, (caddr_t)&in6_ifr) < 0)
659 return;
660 if (sa->sa_family != AF_INET6)
661 return;
662 if (getnameinfo(sa, sa->sa_len, src, sizeof(src), 0, 0,
663 NI_NUMERICHOST) != 0)
664 src[0] = '\0';
665
666 if (ioctl_ctx(ctx, SIOCGIFPDSTADDR_IN6, (caddr_t)&in6_ifr) < 0)
667 return;
668 if (sa->sa_family != AF_INET6)
669 return;
670 if (getnameinfo(sa, sa->sa_len, dst, sizeof(dst), 0, 0,
671 NI_NUMERICHOST) != 0)
672 dst[0] = '\0';
673
674 printf("\ttunnel inet6 %s --> %s\n", src, dst);
675 }
676
677 static void
in6_set_tunnel(if_ctx * ctx,struct addrinfo * srcres,struct addrinfo * dstres)678 in6_set_tunnel(if_ctx *ctx, struct addrinfo *srcres, struct addrinfo *dstres)
679 {
680 struct in6_aliasreq in6_req = {};
681
682 strlcpy(in6_req.ifra_name, ctx->ifname, sizeof(in6_req.ifra_name));
683 memcpy(&in6_req.ifra_addr, srcres->ai_addr, srcres->ai_addr->sa_len);
684 memcpy(&in6_req.ifra_dstaddr, dstres->ai_addr,
685 dstres->ai_addr->sa_len);
686
687 if (ioctl_ctx(ctx, SIOCSIFPHYADDR_IN6, &in6_req) < 0)
688 warn("SIOCSIFPHYADDR_IN6");
689 }
690
691 static void
in6_set_vhid(int vhid)692 in6_set_vhid(int vhid)
693 {
694 #ifdef WITHOUT_NETLINK
695 in6_addreq.ifra_vhid = vhid;
696 #else
697 in6_add.vhid = (uint32_t)vhid;
698 #endif
699 }
700
701 static struct cmd inet6_cmds[] = {
702 DEF_CMD_ARG("prefixlen", setifprefixlen),
703 DEF_CMD("anycast", IN6_IFF_ANYCAST, setip6flags),
704 DEF_CMD("tentative", IN6_IFF_TENTATIVE, setip6flags),
705 DEF_CMD("-tentative", -IN6_IFF_TENTATIVE, setip6flags),
706 DEF_CMD("deprecated", IN6_IFF_DEPRECATED, setip6flags),
707 DEF_CMD("-deprecated", -IN6_IFF_DEPRECATED, setip6flags),
708 DEF_CMD("autoconf", IN6_IFF_AUTOCONF, setip6flags),
709 DEF_CMD("-autoconf", -IN6_IFF_AUTOCONF, setip6flags),
710 DEF_CMD("prefer_source",IN6_IFF_PREFER_SOURCE, setip6flags),
711 DEF_CMD("-prefer_source",-IN6_IFF_PREFER_SOURCE,setip6flags),
712 DEF_CMD("accept_rtadv", ND6_IFF_ACCEPT_RTADV, setnd6flags),
713 DEF_CMD("-accept_rtadv",-ND6_IFF_ACCEPT_RTADV, setnd6flags),
714 DEF_CMD("no_radr", ND6_IFF_NO_RADR, setnd6flags),
715 DEF_CMD("-no_radr", -ND6_IFF_NO_RADR, setnd6flags),
716 DEF_CMD("defaultif", 1, setnd6defif),
717 DEF_CMD("-defaultif", -1, setnd6defif),
718 DEF_CMD("ifdisabled", ND6_IFF_IFDISABLED, setnd6flags),
719 DEF_CMD("-ifdisabled", -ND6_IFF_IFDISABLED, setnd6flags),
720 DEF_CMD("nud", ND6_IFF_PERFORMNUD, setnd6flags),
721 DEF_CMD("-nud", -ND6_IFF_PERFORMNUD, setnd6flags),
722 DEF_CMD("auto_linklocal",ND6_IFF_AUTO_LINKLOCAL,setnd6flags),
723 DEF_CMD("-auto_linklocal",-ND6_IFF_AUTO_LINKLOCAL,setnd6flags),
724 DEF_CMD("no_prefer_iface",ND6_IFF_NO_PREFER_IFACE,setnd6flags),
725 DEF_CMD("-no_prefer_iface",-ND6_IFF_NO_PREFER_IFACE,setnd6flags),
726 DEF_CMD("no_dad", ND6_IFF_NO_DAD, setnd6flags),
727 DEF_CMD("-no_dad", -ND6_IFF_NO_DAD, setnd6flags),
728 DEF_CMD_ARG("pltime", setip6pltime),
729 DEF_CMD_ARG("vltime", setip6vltime),
730 DEF_CMD("eui64", 0, setip6eui64),
731 DEF_CMD("stableaddr", ND6_IFF_STABLEADDR, setnd6flags),
732 DEF_CMD("-stableaddr", -ND6_IFF_STABLEADDR, setnd6flags),
733 #ifdef EXPERIMENTAL
734 DEF_CMD("ipv6_only", ND6_IFF_IPV6_ONLY_MANUAL,setnd6flags),
735 DEF_CMD("-ipv6_only", -ND6_IFF_IPV6_ONLY_MANUAL,setnd6flags),
736 #endif
737 };
738
739 static struct afswtch af_inet6 = {
740 .af_name = "inet6",
741 .af_af = AF_INET6,
742 #ifdef WITHOUT_NETLINK
743 .af_status = in6_status,
744 #else
745 .af_status = in6_status_nl,
746 #endif
747 .af_getaddr = in6_getaddr,
748 .af_copyaddr = in6_copyaddr,
749 #ifdef WITHOUT_NETLINK
750 .af_getprefix = in6_getprefix,
751 #endif
752 .af_other_status = nd6_status,
753 .af_postproc = in6_postproc,
754 .af_status_tunnel = in6_status_tunnel,
755 .af_settunnel = in6_set_tunnel,
756 .af_setvhid = in6_set_vhid,
757 #ifdef WITHOUT_NETLINK
758 .af_difaddr = SIOCDIFADDR_IN6,
759 .af_aifaddr = SIOCAIFADDR_IN6,
760 .af_ridreq = &in6_ridreq,
761 .af_addreq = &in6_addreq,
762 .af_exec = af_exec_ioctl,
763 #else
764 .af_difaddr = NL_RTM_DELADDR,
765 .af_aifaddr = NL_RTM_NEWADDR,
766 .af_ridreq = &in6_del,
767 .af_addreq = &in6_add,
768 .af_exec = in6_exec_nl,
769 #endif
770 };
771
772 static void
in6_Lopt_cb(const char * arg __unused)773 in6_Lopt_cb(const char *arg __unused)
774 {
775 ip6lifetime++; /* print IPv6 address lifetime */
776 }
777 static struct option in6_Lopt = {
778 .opt = "L",
779 .opt_usage = "[-L]",
780 .cb = in6_Lopt_cb
781 };
782
783 static __constructor void
inet6_ctor(void)784 inet6_ctor(void)
785 {
786 size_t i;
787
788 #ifndef RESCUE
789 if (!feature_present("inet6"))
790 return;
791 #endif
792
793 for (i = 0; i < nitems(inet6_cmds); i++)
794 cmd_register(&inet6_cmds[i]);
795 af_register(&af_inet6);
796 opt_register(&in6_Lopt);
797 }
798