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", pl + now.tv_sec, &now);
379 print_lifetime("vltime", vl + now.tv_sec, &now);
380 }
381
382 static void
in6_status_nl(if_ctx * ctx __unused,if_link_t * link __unused,if_addr_t * ifa)383 in6_status_nl(if_ctx *ctx __unused, if_link_t *link __unused, if_addr_t *ifa)
384 {
385 int plen = ifa->ifa_prefixlen;
386 uint32_t scopeid;
387
388 if (ifa->ifa_local == NULL) {
389 /* Non-P2P address */
390 scopeid = satosin6(ifa->ifa_address)->sin6_scope_id;
391 print_addr(satosin6(ifa->ifa_address));
392 } else {
393 scopeid = satosin6(ifa->ifa_local)->sin6_scope_id;
394 print_addr(satosin6(ifa->ifa_local));
395 print_p2p(satosin6(ifa->ifa_address));
396 }
397
398 print_mask(plen);
399 print_flags(ifa->ifaf_flags);
400
401 if (scopeid != 0)
402 printf(" scopeid 0x%x", scopeid);
403
404 show_lifetime(ifa->ifa_cacheinfo);
405
406 if (ifa->ifaf_vhid != 0)
407 printf(" vhid %d", ifa->ifaf_vhid);
408
409 putchar('\n');
410 }
411
412 static struct in6_px *sin6tab_nl[] = {
413 &in6_del.addr, /* RIDADDR */
414 &in6_add.addr, /* ADDR */
415 NULL, /* MASK */
416 &in6_add.dst_addr, /* DSTADDR*/
417 };
418
419 static void
in6_copyaddr(if_ctx * ctx __unused,int to,int from)420 in6_copyaddr(if_ctx *ctx __unused, int to, int from)
421 {
422 sin6tab_nl[to]->addr = sin6tab_nl[from]->addr;
423 sin6tab_nl[to]->set = sin6tab_nl[from]->set;
424 }
425
426 static void
in6_getaddr(const char * addr_str,int which)427 in6_getaddr(const char *addr_str, int which)
428 {
429 struct in6_px *px = sin6tab_nl[which];
430
431 if (which == MASK)
432 errx(1, "netmask: invalid option for inet6");
433 if (which == BRDADDR)
434 errx(1, "broadcast: invalid option for inet6");
435
436 px->set = true;
437 px->plen = 128;
438 if (which == ADDR) {
439 char *p = NULL;
440 if((p = strrchr(addr_str, '/')) != NULL) {
441 *p = '\0';
442 int plen = strtol(p + 1, NULL, 10);
443 if (plen < 0 || plen > 128)
444 errx(1, "%s: bad value", p + 1);
445 px->plen = plen;
446 explicit_prefix = 1;
447 }
448 }
449
450 struct addrinfo hints = { .ai_family = AF_INET6 };
451 struct addrinfo *res;
452
453 int error = getaddrinfo(addr_str, NULL, &hints, &res);
454 if (error != 0) {
455 if (inet_pton(AF_INET6, addr_str, &px->addr) != 1)
456 errx(1, "%s: bad value", addr_str);
457 } else {
458 struct sockaddr_in6 *sin6;
459
460 sin6 = (struct sockaddr_in6 *)(void *)res->ai_addr;
461 px->addr = sin6->sin6_addr;
462 freeaddrinfo(res);
463 }
464 }
465
466 static int
in6_exec_nl(if_ctx * ctx,unsigned long action,void * data)467 in6_exec_nl(if_ctx *ctx, unsigned long action, void *data)
468 {
469 struct in6_pdata *pdata = (struct in6_pdata *)data;
470 struct snl_writer nw = {};
471
472 snl_init_writer(ctx->io_ss, &nw);
473 struct nlmsghdr *hdr = snl_create_msg_request(&nw, action);
474 struct ifaddrmsg *ifahdr = snl_reserve_msg_object(&nw, struct ifaddrmsg);
475
476 ifahdr->ifa_family = AF_INET6;
477 ifahdr->ifa_prefixlen = pdata->addr.plen;
478 ifahdr->ifa_index = if_nametoindex_nl(ctx->io_ss, ctx->ifname);
479
480 snl_add_msg_attr_ip6(&nw, IFA_LOCAL, &pdata->addr.addr);
481 if (action == NL_RTM_NEWADDR && pdata->dst_addr.set)
482 snl_add_msg_attr_ip6(&nw, IFA_ADDRESS, &pdata->dst_addr.addr);
483
484 struct ifa_cacheinfo ci = {
485 .ifa_prefered = pdata->lifetime.ia6t_pltime,
486 .ifa_valid = pdata->lifetime.ia6t_vltime,
487 };
488 snl_add_msg_attr(&nw, IFA_CACHEINFO, sizeof(ci), &ci);
489
490 int off = snl_add_msg_attr_nested(&nw, IFA_FREEBSD);
491 snl_add_msg_attr_u32(&nw, IFAF_FLAGS, pdata->flags);
492 if (pdata->vhid != 0)
493 snl_add_msg_attr_u32(&nw, IFAF_VHID, pdata->vhid);
494 snl_end_attr_nested(&nw, off);
495
496 if (! (hdr = snl_finalize_msg(&nw)) || !snl_send_message(ctx->io_ss, hdr))
497 return (0);
498
499 struct snl_errmsg_data e = {};
500 snl_read_reply_code(ctx->io_ss, hdr->nlmsg_seq, &e);
501
502 return (e.error);
503 }
504 #endif
505
506 #ifdef WITHOUT_NETLINK
507 static struct sockaddr_in6 *sin6tab[] = {
508 &in6_ridreq.ifr_addr, &in6_addreq.ifra_addr,
509 &in6_addreq.ifra_prefixmask, &in6_addreq.ifra_dstaddr
510 };
511
512 static void
in6_copyaddr(if_ctx * ctx __unused,int to,int from)513 in6_copyaddr(if_ctx *ctx __unused, int to, int from)
514 {
515 memcpy(sin6tab[to], sin6tab[from], sizeof(struct sockaddr_in6));
516 }
517
518 static void
in6_getprefix(const char * plen,int which)519 in6_getprefix(const char *plen, int which)
520 {
521 struct sockaddr_in6 *sin = sin6tab[which];
522 u_char *cp;
523 int len = atoi(plen);
524
525 if ((len < 0) || (len > 128))
526 errx(1, "%s: bad value", plen);
527 sin->sin6_len = sizeof(*sin);
528 if (which != MASK)
529 sin->sin6_family = AF_INET6;
530 if ((len == 0) || (len == 128)) {
531 memset(&sin->sin6_addr, 0xff, sizeof(struct in6_addr));
532 return;
533 }
534 memset((void *)&sin->sin6_addr, 0x00, sizeof(sin->sin6_addr));
535 for (cp = (u_char *)&sin->sin6_addr; len > 7; len -= 8)
536 *cp++ = 0xff;
537 *cp = 0xff << (8 - len);
538 }
539
540 static void
in6_getaddr(const char * s,int which)541 in6_getaddr(const char *s, int which)
542 {
543 struct sockaddr_in6 *sin = sin6tab[which];
544 struct addrinfo hints, *res;
545 int error = -1;
546
547 sin->sin6_len = sizeof(*sin);
548 if (which != MASK)
549 sin->sin6_family = AF_INET6;
550
551 if (which == ADDR) {
552 char *p = NULL;
553 if((p = strrchr(s, '/')) != NULL) {
554 *p = '\0';
555 in6_getprefix(p + 1, MASK);
556 explicit_prefix = 1;
557 }
558 }
559
560 if (sin->sin6_family == AF_INET6) {
561 bzero(&hints, sizeof(struct addrinfo));
562 hints.ai_family = AF_INET6;
563 error = getaddrinfo(s, NULL, &hints, &res);
564 if (error != 0) {
565 if (inet_pton(AF_INET6, s, &sin->sin6_addr) != 1)
566 errx(1, "%s: bad value", s);
567 } else {
568 bcopy(res->ai_addr, sin, res->ai_addrlen);
569 freeaddrinfo(res);
570 }
571 }
572 }
573
574 static int
prefix(void * val,int size)575 prefix(void *val, int size)
576 {
577 u_char *name = (u_char *)val;
578 int byte, bit, plen = 0;
579
580 for (byte = 0; byte < size; byte++, plen += 8)
581 if (name[byte] != 0xff)
582 break;
583 if (byte == size)
584 return (plen);
585 for (bit = 7; bit != 0; bit--, plen++)
586 if (!(name[byte] & (1 << bit)))
587 break;
588 for (; bit != 0; bit--)
589 if (name[byte] & (1 << bit))
590 return(0);
591 byte++;
592 for (; byte < size; byte++)
593 if (name[byte])
594 return(0);
595 return (plen);
596 }
597 #endif
598
599 static char *
sec2str(time_t total)600 sec2str(time_t total)
601 {
602 static char result[256];
603 int days, hours, mins, secs;
604 int first = 1;
605 char *p = result;
606
607 if (0) {
608 days = total / 3600 / 24;
609 hours = (total / 3600) % 24;
610 mins = (total / 60) % 60;
611 secs = total % 60;
612
613 if (days) {
614 first = 0;
615 p += sprintf(p, "%dd", days);
616 }
617 if (!first || hours) {
618 first = 0;
619 p += sprintf(p, "%dh", hours);
620 }
621 if (!first || mins) {
622 first = 0;
623 p += sprintf(p, "%dm", mins);
624 }
625 sprintf(p, "%ds", secs);
626 } else
627 sprintf(result, "%lu", (unsigned long)total);
628
629 return(result);
630 }
631
632 static void
in6_postproc(if_ctx * ctx,int newaddr __unused,int ifflags __unused)633 in6_postproc(if_ctx *ctx, int newaddr __unused,
634 int ifflags __unused)
635 {
636 if (explicit_prefix == 0) {
637 /* Aggregatable address architecture defines all prefixes
638 are 64. So, it is convenient to set prefixlen to 64 if
639 it is not specified. */
640 setifprefixlen(ctx, "64", 0);
641 /* in6_getprefix("64", MASK) if MASK is available here... */
642 }
643 }
644
645 static void
in6_status_tunnel(if_ctx * ctx)646 in6_status_tunnel(if_ctx *ctx)
647 {
648 char src[NI_MAXHOST];
649 char dst[NI_MAXHOST];
650 struct in6_ifreq in6_ifr;
651 const struct sockaddr *sa = (const struct sockaddr *) &in6_ifr.ifr_addr;
652
653 memset(&in6_ifr, 0, sizeof(in6_ifr));
654 strlcpy(in6_ifr.ifr_name, ctx->ifname, sizeof(in6_ifr.ifr_name));
655
656 if (ioctl_ctx(ctx, SIOCGIFPSRCADDR_IN6, (caddr_t)&in6_ifr) < 0)
657 return;
658 if (sa->sa_family != AF_INET6)
659 return;
660 if (getnameinfo(sa, sa->sa_len, src, sizeof(src), 0, 0,
661 NI_NUMERICHOST) != 0)
662 src[0] = '\0';
663
664 if (ioctl_ctx(ctx, SIOCGIFPDSTADDR_IN6, (caddr_t)&in6_ifr) < 0)
665 return;
666 if (sa->sa_family != AF_INET6)
667 return;
668 if (getnameinfo(sa, sa->sa_len, dst, sizeof(dst), 0, 0,
669 NI_NUMERICHOST) != 0)
670 dst[0] = '\0';
671
672 printf("\ttunnel inet6 %s --> %s\n", src, dst);
673 }
674
675 static void
in6_set_tunnel(if_ctx * ctx,struct addrinfo * srcres,struct addrinfo * dstres)676 in6_set_tunnel(if_ctx *ctx, struct addrinfo *srcres, struct addrinfo *dstres)
677 {
678 struct in6_aliasreq in6_req = {};
679
680 strlcpy(in6_req.ifra_name, ctx->ifname, sizeof(in6_req.ifra_name));
681 memcpy(&in6_req.ifra_addr, srcres->ai_addr, srcres->ai_addr->sa_len);
682 memcpy(&in6_req.ifra_dstaddr, dstres->ai_addr,
683 dstres->ai_addr->sa_len);
684
685 if (ioctl_ctx(ctx, SIOCSIFPHYADDR_IN6, &in6_req) < 0)
686 warn("SIOCSIFPHYADDR_IN6");
687 }
688
689 static void
in6_set_vhid(int vhid)690 in6_set_vhid(int vhid)
691 {
692 #ifdef WITHOUT_NETLINK
693 in6_addreq.ifra_vhid = vhid;
694 #else
695 in6_add.vhid = (uint32_t)vhid;
696 #endif
697 }
698
699 static struct cmd inet6_cmds[] = {
700 DEF_CMD_ARG("prefixlen", setifprefixlen),
701 DEF_CMD("anycast", IN6_IFF_ANYCAST, setip6flags),
702 DEF_CMD("tentative", IN6_IFF_TENTATIVE, setip6flags),
703 DEF_CMD("-tentative", -IN6_IFF_TENTATIVE, setip6flags),
704 DEF_CMD("deprecated", IN6_IFF_DEPRECATED, setip6flags),
705 DEF_CMD("-deprecated", -IN6_IFF_DEPRECATED, setip6flags),
706 DEF_CMD("autoconf", IN6_IFF_AUTOCONF, setip6flags),
707 DEF_CMD("-autoconf", -IN6_IFF_AUTOCONF, setip6flags),
708 DEF_CMD("prefer_source",IN6_IFF_PREFER_SOURCE, setip6flags),
709 DEF_CMD("-prefer_source",-IN6_IFF_PREFER_SOURCE,setip6flags),
710 DEF_CMD("accept_rtadv", ND6_IFF_ACCEPT_RTADV, setnd6flags),
711 DEF_CMD("-accept_rtadv",-ND6_IFF_ACCEPT_RTADV, setnd6flags),
712 DEF_CMD("no_radr", ND6_IFF_NO_RADR, setnd6flags),
713 DEF_CMD("-no_radr", -ND6_IFF_NO_RADR, setnd6flags),
714 DEF_CMD("defaultif", 1, setnd6defif),
715 DEF_CMD("-defaultif", -1, setnd6defif),
716 DEF_CMD("ifdisabled", ND6_IFF_IFDISABLED, setnd6flags),
717 DEF_CMD("-ifdisabled", -ND6_IFF_IFDISABLED, setnd6flags),
718 DEF_CMD("nud", ND6_IFF_PERFORMNUD, setnd6flags),
719 DEF_CMD("-nud", -ND6_IFF_PERFORMNUD, setnd6flags),
720 DEF_CMD("auto_linklocal",ND6_IFF_AUTO_LINKLOCAL,setnd6flags),
721 DEF_CMD("-auto_linklocal",-ND6_IFF_AUTO_LINKLOCAL,setnd6flags),
722 DEF_CMD("no_prefer_iface",ND6_IFF_NO_PREFER_IFACE,setnd6flags),
723 DEF_CMD("-no_prefer_iface",-ND6_IFF_NO_PREFER_IFACE,setnd6flags),
724 DEF_CMD("no_dad", ND6_IFF_NO_DAD, setnd6flags),
725 DEF_CMD("-no_dad", -ND6_IFF_NO_DAD, setnd6flags),
726 DEF_CMD_ARG("pltime", setip6pltime),
727 DEF_CMD_ARG("vltime", setip6vltime),
728 DEF_CMD("eui64", 0, setip6eui64),
729 #ifdef EXPERIMENTAL
730 DEF_CMD("ipv6_only", ND6_IFF_IPV6_ONLY_MANUAL,setnd6flags),
731 DEF_CMD("-ipv6_only", -ND6_IFF_IPV6_ONLY_MANUAL,setnd6flags),
732 #endif
733 };
734
735 static struct afswtch af_inet6 = {
736 .af_name = "inet6",
737 .af_af = AF_INET6,
738 #ifdef WITHOUT_NETLINK
739 .af_status = in6_status,
740 #else
741 .af_status = in6_status_nl,
742 #endif
743 .af_getaddr = in6_getaddr,
744 .af_copyaddr = in6_copyaddr,
745 #ifdef WITHOUT_NETLINK
746 .af_getprefix = in6_getprefix,
747 #endif
748 .af_other_status = nd6_status,
749 .af_postproc = in6_postproc,
750 .af_status_tunnel = in6_status_tunnel,
751 .af_settunnel = in6_set_tunnel,
752 .af_setvhid = in6_set_vhid,
753 #ifdef WITHOUT_NETLINK
754 .af_difaddr = SIOCDIFADDR_IN6,
755 .af_aifaddr = SIOCAIFADDR_IN6,
756 .af_ridreq = &in6_addreq,
757 .af_addreq = &in6_addreq,
758 .af_exec = af_exec_ioctl,
759 #else
760 .af_difaddr = NL_RTM_DELADDR,
761 .af_aifaddr = NL_RTM_NEWADDR,
762 .af_ridreq = &in6_add,
763 .af_addreq = &in6_add,
764 .af_exec = in6_exec_nl,
765 #endif
766 };
767
768 static void
in6_Lopt_cb(const char * arg __unused)769 in6_Lopt_cb(const char *arg __unused)
770 {
771 ip6lifetime++; /* print IPv6 address lifetime */
772 }
773 static struct option in6_Lopt = {
774 .opt = "L",
775 .opt_usage = "[-L]",
776 .cb = in6_Lopt_cb
777 };
778
779 static __constructor void
inet6_ctor(void)780 inet6_ctor(void)
781 {
782 size_t i;
783
784 #ifndef RESCUE
785 if (!feature_present("inet6"))
786 return;
787 #endif
788
789 for (i = 0; i < nitems(inet6_cmds); i++)
790 cmd_register(&inet6_cmds[i]);
791 af_register(&af_inet6);
792 opt_register(&in6_Lopt);
793 }
794