rtadvctl.c revision 224144
1/*- 2 * Copyright (C) 2011 Hiroki Sato <hrs@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS 18 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 21 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 22 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 24 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 * 26 * $FreeBSD: head/usr.sbin/rtadvctl/rtadvctl.c 224144 2011-07-17 19:24:54Z hrs $ 27 * 28 */ 29 30#include <sys/queue.h> 31#include <sys/types.h> 32#include <sys/socket.h> 33#include <sys/stat.h> 34#include <sys/un.h> 35#include <sys/uio.h> 36#include <net/if.h> 37#include <net/if_dl.h> 38#include <net/if_types.h> 39#include <net/if_var.h> 40#include <net/ethernet.h> 41#include <netinet/in.h> 42#include <netinet/ip6.h> 43#include <netinet/icmp6.h> 44#include <netinet6/in6_var.h> 45#include <netinet6/nd6.h> 46#include <arpa/inet.h> 47#include <fcntl.h> 48#include <errno.h> 49#include <inttypes.h> 50#include <netdb.h> 51#include <unistd.h> 52#include <string.h> 53#include <stdarg.h> 54#include <stdio.h> 55#include <stdlib.h> 56#include <stdarg.h> 57#include <syslog.h> 58#include <err.h> 59 60#include "pathnames.h" 61#include "rtadvd.h" 62#include "if.h" 63#include "timer_subr.h" 64#include "timer.h" 65#include "control.h" 66#include "control_client.h" 67 68#define RA_IFSTATUS_INACTIVE 0 69#define RA_IFSTATUS_RA_RECV 1 70#define RA_IFSTATUS_RA_SEND 2 71 72static int vflag = LOG_ERR; 73 74static void usage(void); 75 76static int action_propset(char *); 77static int action_propget(char *, struct ctrl_msg_pl *); 78static int action_plgeneric(int, char *, char *); 79 80static int action_enable(int, char **); 81static int action_disable(int, char **); 82static int action_reload(int, char **); 83static int action_echo(int, char **); 84static int action_version(int, char **); 85static int action_shutdown(int, char **); 86 87static int action_show(int, char **); 88static int action_show_prefix(struct prefix *); 89static int action_show_rtinfo(struct rtinfo *); 90static int action_show_rdnss(void *); 91static int action_show_dnssl(void *); 92 93static int csock_client_open(struct sockinfo *); 94static size_t dname_labeldec(char *, size_t, const char *); 95static void mysyslog(int, const char *, ...); 96 97static const char *rtpref_str[] = { 98 "medium", /* 00 */ 99 "high", /* 01 */ 100 "rsv", /* 10 */ 101 "low" /* 11 */ 102}; 103 104static struct dispatch_table { 105 const char *dt_comm; 106 int (*dt_act)(int, char **); 107} dtable[] = { 108 { "show", action_show }, 109 { "reload", action_reload }, 110 { "shutdown", action_shutdown }, 111 { "enable", action_enable }, 112 { "disable", action_disable }, 113 { NULL, NULL }, 114 { "echo", action_echo }, 115 { "version", action_version }, 116 { NULL, NULL }, 117}; 118 119static char errmsgbuf[1024]; 120static char *errmsg = NULL; 121 122static void 123mysyslog(int priority, const char * restrict fmt, ...) 124{ 125 va_list ap; 126 127 if (vflag >= priority) { 128 va_start(ap, fmt); 129 vfprintf(stderr, fmt, ap); 130 fprintf(stderr, "\n"); 131 va_end(ap); 132 } 133} 134 135static void 136usage(void) 137{ 138 int i; 139 140 for (i = 0; (size_t)i < sizeof(dtable)/sizeof(dtable[0]); i++) { 141 if (dtable[i].dt_comm == NULL) 142 break; 143 printf("%s\n", dtable[i].dt_comm); 144 } 145 146 exit(1); 147} 148 149int 150main(int argc, char *argv[]) 151{ 152 int i; 153 int ch; 154 int (*action)(int, char **) = NULL; 155 int error; 156 157 while ((ch = getopt(argc, argv, "Dv")) != -1) { 158 switch (ch) { 159 case 'D': 160 vflag = LOG_DEBUG; 161 break; 162 case 'v': 163 vflag++; 164 break; 165 default: 166 usage(); 167 } 168 } 169 argc -= optind; 170 argv += optind; 171 172 if (argc == 0) 173 usage(); 174 175 for (i = 0; (size_t)i < sizeof(dtable)/sizeof(dtable[0]); i++) { 176 if (dtable[i].dt_comm == NULL || 177 strcmp(dtable[i].dt_comm, argv[0]) == 0) { 178 action = dtable[i].dt_act; 179 break; 180 } 181 } 182 183 if (action == NULL) 184 usage(); 185 186 error = (dtable[i].dt_act)(--argc, ++argv); 187 if (error) { 188 fprintf(stderr, "%s failed", dtable[i].dt_comm); 189 if (errmsg != NULL) 190 fprintf(stderr, ": %s", errmsg); 191 fprintf(stderr, ".\n"); 192 } 193 194 return (error); 195} 196 197static int 198csock_client_open(struct sockinfo *s) 199{ 200 struct sockaddr_un sun; 201 202 if ((s->si_fd = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) 203 err(1, "cannot open control socket."); 204 205 memset(&sun, 0, sizeof(sun)); 206 sun.sun_family = AF_UNIX; 207 sun.sun_len = sizeof(sun); 208 strlcpy(sun.sun_path, s->si_name, sizeof(sun.sun_path)); 209 210 if (connect(s->si_fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) 211 err(1, "connect: %s", s->si_name); 212 213 mysyslog(LOG_DEBUG, 214 "<%s> connected to %s", __func__, sun.sun_path); 215 216 return (0); 217} 218 219static int 220action_plgeneric(int action, char *plstr, char *buf) 221{ 222 struct ctrl_msg_hdr *cm; 223 struct ctrl_msg_pl cp; 224 struct sockinfo *s; 225 char *msg; 226 char *p; 227 char *q; 228 229 s = &ctrlsock; 230 csock_client_open(s); 231 232 cm = (struct ctrl_msg_hdr *)buf; 233 msg = (char *)buf + sizeof(*cm); 234 235 cm->cm_version = CM_VERSION; 236 cm->cm_type = action; 237 cm->cm_len = sizeof(*cm); 238 239 if (plstr != NULL) { 240 memset(&cp, 0, sizeof(cp)); 241 p = strchr(plstr, ':'); 242 q = strchr(plstr, '='); 243 if (p != NULL && q != NULL && p > q) 244 return (1); 245 246 if (p == NULL) { /* No : */ 247 cp.cp_ifname = NULL; 248 cp.cp_key = plstr; 249 } else if (p == plstr) { /* empty */ 250 cp.cp_ifname = NULL; 251 cp.cp_key = plstr + 1; 252 } else { 253 *p++ = '\0'; 254 cp.cp_ifname = plstr; 255 cp.cp_key = p; 256 } 257 if (q == NULL) 258 cp.cp_val = NULL; 259 else { 260 *q++ = '\0'; 261 cp.cp_val = q; 262 } 263 cm->cm_len += cmsg_pl2bin(msg, &cp); 264 265 mysyslog(LOG_DEBUG, "<%s> key=%s, val_len=%d, ifname=%s", 266 __func__,cp.cp_key, cp.cp_val_len, cp.cp_ifname); 267 } 268 269 return (cmsg_handler_client(s->si_fd, CM_STATE_MSG_DISPATCH, buf)); 270} 271 272static int 273action_propget(char *argv, struct ctrl_msg_pl *cp) 274{ 275 int error; 276 struct ctrl_msg_hdr *cm; 277 char buf[CM_MSG_MAXLEN]; 278 char *msg; 279 280 memset(cp, 0, sizeof(*cp)); 281 cm = (struct ctrl_msg_hdr *)buf; 282 msg = (char *)buf + sizeof(*cm); 283 284 error = action_plgeneric(CM_TYPE_REQ_GET_PROP, argv, buf); 285 if (error || cm->cm_len <= sizeof(*cm)) 286 return (1); 287 288 cmsg_bin2pl(msg, cp); 289 mysyslog(LOG_DEBUG, "<%s> type=%d, len=%d", 290 __func__, cm->cm_type, cm->cm_len); 291 mysyslog(LOG_DEBUG, "<%s> key=%s, val_len=%d, ifname=%s", 292 __func__,cp->cp_key, cp->cp_val_len, cp->cp_ifname); 293 294 return (0); 295} 296 297static int 298action_propset(char *argv) 299{ 300 char buf[CM_MSG_MAXLEN]; 301 302 return (action_plgeneric(CM_TYPE_REQ_SET_PROP, argv, buf)); 303} 304 305static int 306action_disable(int argc, char **argv) 307{ 308 char *action_argv; 309 char argv_disable[IFNAMSIZ + sizeof(":disable=")]; 310 int i; 311 int error; 312 313 if (argc < 1) 314 return (1); 315 316 error = 0; 317 for (i = 0; i < argc; i++) { 318 sprintf(argv_disable, "%s:disable=", argv[i]); 319 action_argv = argv_disable; 320 error += action_propset(action_argv); 321 } 322 323 return (error); 324} 325 326static int 327action_enable(int argc, char **argv) 328{ 329 char *action_argv; 330 char argv_enable[IFNAMSIZ + sizeof(":enable=")]; 331 int i; 332 int error; 333 334 if (argc < 1) 335 return (1); 336 337 error = 0; 338 for (i = 0; i < argc; i++) { 339 sprintf(argv_enable, "%s:enable=", argv[i]); 340 action_argv = argv_enable; 341 error += action_propset(action_argv); 342 } 343 344 return (error); 345} 346 347static int 348action_reload(int argc, char **argv) 349{ 350 char *action_argv; 351 char argv_reload[IFNAMSIZ + sizeof(":reload=")]; 352 int i; 353 int error; 354 355 if (argc == 0) { 356 action_argv = strdup(":reload="); 357 return (action_propset(action_argv)); 358 } 359 360 error = 0; 361 for (i = 0; i < argc; i++) { 362 sprintf(argv_reload, "%s:reload=", argv[i]); 363 action_argv = argv_reload; 364 error += action_propset(action_argv); 365 } 366 367 return (error); 368} 369 370static int 371action_echo(int argc __unused, char **argv __unused) 372{ 373 char *action_argv; 374 375 action_argv = strdup("echo"); 376 return (action_propset(action_argv)); 377} 378 379static int 380action_shutdown(int argc __unused, char **argv __unused) 381{ 382 char *action_argv; 383 384 action_argv = strdup("shutdown"); 385 return (action_propset(action_argv)); 386} 387 388/* XXX */ 389static int 390action_version(int argc __unused, char **argv __unused) 391{ 392 char *action_argv; 393 struct ctrl_msg_pl cp; 394 int error; 395 396 action_argv = strdup(":version="); 397 error = action_propget(action_argv, &cp); 398 if (error) 399 return (error); 400 401 printf("version=%s\n", cp.cp_val); 402 return (0); 403} 404 405static int 406action_show(int argc, char **argv) 407{ 408 char *action_argv; 409 char argv_ifilist[sizeof(":ifilist=")] = ":ifilist="; 410 char argv_ifi[IFNAMSIZ + sizeof(":ifi=")]; 411 char argv_rai[IFNAMSIZ + sizeof(":rai=")]; 412 char argv_rti[IFNAMSIZ + sizeof(":rti=")]; 413 char argv_pfx[IFNAMSIZ + sizeof(":pfx=")]; 414 char argv_ifi_ra_timer[IFNAMSIZ + sizeof(":ifi_ra_timer=")]; 415 char argv_rdnss[IFNAMSIZ + sizeof(":rdnss=")]; 416 char argv_dnssl[IFNAMSIZ + sizeof(":dnssl=")]; 417 char ssbuf[SSBUFLEN]; 418 419 struct ctrl_msg_pl cp; 420 struct ifinfo *ifi; 421 TAILQ_HEAD(, ifinfo) ifl = TAILQ_HEAD_INITIALIZER(ifl); 422 char *endp; 423 char *p; 424 int error; 425 int i; 426 int len; 427 428 if (argc == 0) { 429 action_argv = argv_ifilist; 430 error = action_propget(action_argv, &cp); 431 if (error) 432 return (error); 433 434 p = cp.cp_val; 435 endp = p + cp.cp_val_len; 436 while (p < endp) { 437 ifi = malloc(sizeof(*ifi)); 438 if (ifi == NULL) 439 return (1); 440 memset(ifi, 0, sizeof(*ifi)); 441 442 strcpy(ifi->ifi_ifname, p); 443 ifi->ifi_ifindex = if_nametoindex(ifi->ifi_ifname); 444 TAILQ_INSERT_TAIL(&ifl, ifi, ifi_next); 445 p += strlen(ifi->ifi_ifname) + 1; 446 } 447 } else { 448 for (i = 0; i < argc; i++) { 449 ifi = malloc(sizeof(*ifi)); 450 if (ifi == NULL) 451 return (1); 452 memset(ifi, 0, sizeof(*ifi)); 453 454 strcpy(ifi->ifi_ifname, argv[i]); 455 ifi->ifi_ifindex = if_nametoindex(ifi->ifi_ifname); 456 if (ifi->ifi_ifindex == 0) { 457 sprintf(errmsgbuf, "invalid interface %s", 458 ifi->ifi_ifname); 459 errmsg = errmsgbuf; 460 return (1); 461 } 462 463 TAILQ_INSERT_TAIL(&ifl, ifi, ifi_next); 464 } 465 } 466 467 TAILQ_FOREACH(ifi, &ifl, ifi_next) { 468 struct ifinfo *ifi_s; 469 struct rtadvd_timer *rat; 470 struct rainfo *rai; 471 struct rtinfo *rti; 472 struct prefix *pfx; 473 int c; 474 int ra_ifstatus; 475 476 sprintf(argv_ifi, "%s:ifi=", ifi->ifi_ifname); 477 action_argv = argv_ifi; 478 error = action_propget(action_argv, &cp); 479 if (error) 480 return (error); 481 ifi_s = (struct ifinfo *)cp.cp_val; 482 483 if (!(ifi_s->ifi_persist) && vflag < LOG_NOTICE) 484 continue; 485 486 printf("%s: flags=<", ifi->ifi_ifname); 487 488 c = 0; 489 if (ifi_s->ifi_ifindex == 0) 490 c += printf("NONEXISTENT"); 491 else 492 c += printf("%s", (ifi_s->ifi_flags & IFF_UP) ? 493 "UP" : "DOWN"); 494 switch (ifi_s->ifi_state) { 495 case IFI_STATE_CONFIGURED: 496 c += printf("%s%s", (c) ? "," : "", "CONFIGURED"); 497 break; 498 case IFI_STATE_TRANSITIVE: 499 c += printf("%s%s", (c) ? "," : "", "TRANSITIVE"); 500 break; 501 } 502 if (ifi_s->ifi_persist) 503 c += printf("%s%s", (c) ? "," : "", "PERSIST"); 504 printf(">"); 505 506 ra_ifstatus = RA_IFSTATUS_INACTIVE; 507 if ((ifi_s->ifi_flags & IFF_UP) && 508 ((ifi_s->ifi_state == IFI_STATE_CONFIGURED) || 509 (ifi_s->ifi_state == IFI_STATE_TRANSITIVE))) { 510#if (__FreeBSD_version < 900000) 511 /* 512 * RA_RECV: !ip6.forwarding && ip6.accept_rtadv 513 * RA_SEND: ip6.forwarding 514 */ 515 if (getinet6sysctl(IPV6CTL_FORWARDING) == 0) { 516 if (getinet6sysctl(IPV6CTL_ACCEPT_RTADV)) 517 ra_ifstatus = RA_IFSTATUS_RA_RECV; 518 else 519 ra_ifstatus = RA_IFSTATUS_INACTIVE; 520 } else 521 ra_ifstatus = RA_IFSTATUS_RA_SEND; 522#else 523 /* 524 * RA_RECV: ND6_IFF_ACCEPT_RTADV 525 * RA_SEND: ip6.forwarding 526 */ 527 if (ifi_s->ifi_nd_flags & ND6_IFF_ACCEPT_RTADV) 528 ra_ifstatus = RA_IFSTATUS_RA_RECV; 529 else if (getinet6sysctl(IPV6CTL_FORWARDING)) 530 ra_ifstatus = RA_IFSTATUS_RA_SEND; 531 else 532 ra_ifstatus = RA_IFSTATUS_INACTIVE; 533#endif 534 } 535 536 c = 0; 537 printf(" status=<"); 538 if (ra_ifstatus == RA_IFSTATUS_INACTIVE) 539 printf("%s%s", (c) ? "," : "", "INACTIVE"); 540 else if (ra_ifstatus == RA_IFSTATUS_RA_RECV) 541 printf("%s%s", (c) ? "," : "", "RA_RECV"); 542 else if (ra_ifstatus == RA_IFSTATUS_RA_SEND) 543 printf("%s%s", (c) ? "," : "", "RA_SEND"); 544 printf("> "); 545 546 switch (ifi_s->ifi_state) { 547 case IFI_STATE_CONFIGURED: 548 case IFI_STATE_TRANSITIVE: 549 break; 550 default: 551 printf("\n"); 552 continue; 553 } 554 555 printf("mtu %d\n", ifi_s->ifi_phymtu); 556 557 sprintf(argv_rai, "%s:rai=", ifi->ifi_ifname); 558 action_argv = argv_rai; 559 560 error = action_propget(action_argv, &cp); 561 if (error) 562 continue; 563 564 rai = (struct rainfo *)cp.cp_val; 565 566 printf("\tDefaultLifetime: %s", 567 sec2str(rai->rai_lifetime, ssbuf)); 568 if (ra_ifstatus != RA_IFSTATUS_RA_SEND && 569 rai->rai_lifetime == 0) 570 printf(" (RAs will be sent with zero lifetime)"); 571 572 printf("\n"); 573 574 printf("\tMinAdvInterval/MaxAdvInterval: %s/%s\n", 575 sec2str(rai->rai_mininterval, ssbuf), 576 sec2str(rai->rai_maxinterval, ssbuf)); 577 if (rai->rai_linkmtu) 578 printf("\tAdvLinkMTU: %d", rai->rai_linkmtu); 579 else 580 printf("\tAdvLinkMTU: <none>"); 581 582 printf(", "); 583 584 printf("Flags: "); 585 if (rai->rai_managedflg || rai->rai_otherflg) { 586 printf("%s", rai->rai_managedflg ? "M" : ""); 587 printf("%s", rai->rai_otherflg ? "O" : ""); 588 } else 589 printf("<none>"); 590 591 printf(", "); 592 593 printf("Preference: %s\n", 594 rtpref_str[(rai->rai_rtpref >> 3) & 0xff]); 595 596 printf("\t" 597 "ReachableTime: %s, " 598 "RetransTimer: %s, " 599 "CurHopLimit: %d\n", 600 sec2str(rai->rai_reachabletime, ssbuf), 601 sec2str(rai->rai_retranstimer, ssbuf), 602 rai->rai_hoplimit); 603 printf("\tAdvIfPrefixes: %s\n", 604 rai->rai_advifprefix ? "yes" : "no"); 605 606 /* RA timer */ 607 rat = NULL; 608 if (ifi_s->ifi_ra_timer != NULL) { 609 sprintf(argv_ifi_ra_timer, "%s:ifi_ra_timer=", 610 ifi->ifi_ifname); 611 action_argv = argv_ifi_ra_timer; 612 613 error = action_propget(action_argv, &cp); 614 if (error) 615 return (error); 616 617 rat = (struct rtadvd_timer *)cp.cp_val; 618 } 619 printf("\tNext RA send: %s", 620 (rat == NULL) ? "never\n" : 621 ctime((time_t *)&rat->rat_tm.tv_sec)); 622 printf("\tLast RA sent: %s", 623 (ifi_s->ifi_ra_lastsent.tv_sec == 0) ? "never\n" : 624 ctime((time_t *)&ifi_s->ifi_ra_lastsent.tv_sec)); 625 if (rai->rai_clockskew) 626 printf("\tClock skew: %" PRIu16 "sec\n", 627 rai->rai_clockskew); 628 629 if (vflag < LOG_WARNING) 630 continue; 631 632 /* route information */ 633 sprintf(argv_rti, "%s:rti=", ifi->ifi_ifname); 634 action_argv = argv_rti; 635 error = action_propget(action_argv, &cp); 636 if (error) 637 return (error); 638 639 rti = (struct rtinfo *)cp.cp_val; 640 len = cp.cp_val_len / sizeof(*rti); 641 if (len > 0) { 642 printf("\tRoute Info:\n"); 643 644 for (i = 0; i < len; i++) 645 action_show_rtinfo(&rti[i]); 646 } 647 648 /* prefix information */ 649 sprintf(argv_pfx, "%s:pfx=", ifi->ifi_ifname); 650 action_argv = argv_pfx; 651 652 error = action_propget(action_argv, &cp); 653 if (error) 654 continue; 655 656 pfx = (struct prefix *)cp.cp_val; 657 len = cp.cp_val_len / sizeof(*pfx); 658 659 if (len > 0) { 660 printf("\tPrefixes (%d):\n", len); 661 662 for (i = 0; i < len; i++) 663 action_show_prefix(&pfx[i]); 664 } 665 666 /* RDNSS information */ 667 sprintf(argv_rdnss, "%s:rdnss=", ifi->ifi_ifname); 668 action_argv = argv_rdnss; 669 670 error = action_propget(action_argv, &cp); 671 if (error) 672 continue; 673 674 len = *((uint16_t *)cp.cp_val); 675 676 if (len > 0) { 677 printf("\tRDNSS entries:\n"); 678 action_show_rdnss(cp.cp_val); 679 } 680 681 /* DNSSL information */ 682 sprintf(argv_dnssl, "%s:dnssl=", ifi->ifi_ifname); 683 action_argv = argv_dnssl; 684 685 error = action_propget(action_argv, &cp); 686 if (error) 687 continue; 688 689 len = *((uint16_t *)cp.cp_val); 690 691 if (len > 0) { 692 printf("\tDNSSL entries:\n"); 693 action_show_dnssl(cp.cp_val); 694 } 695 696 if (vflag < LOG_NOTICE) 697 continue; 698 699 printf("\n"); 700 701 printf("\tCounters\n" 702 "\t RA burst counts: %" PRIu16 " (interval: %s)\n" 703 "\t RS wait counts: %" PRIu16 "\n", 704 ifi_s->ifi_burstcount, 705 sec2str(ifi_s->ifi_burstinterval, ssbuf), 706 ifi_s->ifi_rs_waitcount); 707 708 printf("\tOutputs\n" 709 "\t RA: %" PRIu64 "\n", ifi_s->ifi_raoutput); 710 711 printf("\tInputs\n" 712 "\t RA: %" PRIu64 " (normal)\n" 713 "\t RA: %" PRIu64 " (inconsistent)\n" 714 "\t RS: %" PRIu64 "\n", 715 ifi_s->ifi_rainput, 716 ifi_s->ifi_rainconsistent, 717 ifi_s->ifi_rsinput); 718 719 printf("\n"); 720 721#if 0 /* Not implemented yet */ 722 printf("\tReceived RAs:\n"); 723#endif 724 } 725 726 return (0); 727} 728 729static int 730action_show_rtinfo(struct rtinfo *rti) 731{ 732 char ntopbuf[INET6_ADDRSTRLEN]; 733 char ssbuf[SSBUFLEN]; 734 735 printf("\t %s/%d (pref: %s, ltime: %s)\n", 736 inet_ntop(AF_INET6, &rti->rti_prefix, 737 ntopbuf, sizeof(ntopbuf)), 738 rti->rti_prefixlen, 739 rtpref_str[0xff & (rti->rti_rtpref >> 3)], 740 (rti->rti_ltime == ND6_INFINITE_LIFETIME) ? 741 "infinity" : sec2str(rti->rti_ltime, ssbuf)); 742 743 return (0); 744} 745 746static int 747action_show_prefix(struct prefix *pfx) 748{ 749 char ntopbuf[INET6_ADDRSTRLEN]; 750 char ssbuf[SSBUFLEN]; 751 struct timeval now; 752 753 gettimeofday(&now, NULL); 754 printf("\t %s/%d", inet_ntop(AF_INET6, &pfx->pfx_prefix, 755 ntopbuf, sizeof(ntopbuf)), pfx->pfx_prefixlen); 756 757 printf(" ("); 758 switch (pfx->pfx_origin) { 759 case PREFIX_FROM_KERNEL: 760 printf("KERNEL"); 761 break; 762 case PREFIX_FROM_CONFIG: 763 printf("CONFIG"); 764 break; 765 case PREFIX_FROM_DYNAMIC: 766 printf("DYNAMIC"); 767 break; 768 } 769 770 printf(","); 771 772 printf(" vltime=%s", 773 (pfx->pfx_validlifetime == ND6_INFINITE_LIFETIME) ? 774 "infinity" : sec2str(pfx->pfx_validlifetime, ssbuf)); 775 776 if (pfx->pfx_vltimeexpire > 0) 777 printf("(expire: %s)", 778 ((long)pfx->pfx_vltimeexpire > now.tv_sec) ? 779 sec2str(pfx->pfx_vltimeexpire - now.tv_sec, ssbuf) : 780 "0"); 781 782 printf(","); 783 784 printf(" pltime=%s", 785 (pfx->pfx_preflifetime == ND6_INFINITE_LIFETIME) ? 786 "infinity" : sec2str(pfx->pfx_preflifetime, ssbuf)); 787 788 if (pfx->pfx_pltimeexpire > 0) 789 printf("(expire %s)", 790 ((long)pfx->pfx_pltimeexpire > now.tv_sec) ? 791 sec2str(pfx->pfx_pltimeexpire - now.tv_sec, ssbuf) : 792 "0"); 793 794 printf(","); 795 796 printf(" flags="); 797 if (pfx->pfx_onlinkflg || pfx->pfx_autoconfflg) { 798 printf("%s", pfx->pfx_onlinkflg ? "L" : ""); 799 printf("%s", pfx->pfx_autoconfflg ? "A" : ""); 800 } else 801 printf("<none>"); 802 803 if (pfx->pfx_timer) { 804 struct timeval *rest; 805 806 rest = rtadvd_timer_rest(pfx->pfx_timer); 807 if (rest) { /* XXX: what if not? */ 808 printf(" expire=%s", sec2str(rest->tv_sec, ssbuf)); 809 } 810 } 811 812 printf(")\n"); 813 814 return (0); 815} 816 817static int 818action_show_rdnss(void *msg) 819{ 820 struct rdnss *rdn; 821 struct rdnss_addr *rda; 822 uint16_t *rdn_cnt; 823 uint16_t *rda_cnt; 824 int i; 825 int j; 826 char *p; 827 uint32_t ltime; 828 char ntopbuf[INET6_ADDRSTRLEN]; 829 char ssbuf[SSBUFLEN]; 830 831 p = msg; 832 rdn_cnt = (uint16_t *)p; 833 p += sizeof(*rdn_cnt); 834 835 if (*rdn_cnt > 0) { 836 for (i = 0; i < *rdn_cnt; i++) { 837 rdn = (struct rdnss *)p; 838 ltime = rdn->rd_ltime; 839 p += sizeof(*rdn); 840 841 rda_cnt = (uint16_t *)p; 842 p += sizeof(*rda_cnt); 843 if (*rda_cnt > 0) 844 for (j = 0; j < *rda_cnt; j++) { 845 rda = (struct rdnss_addr *)p; 846 printf("\t %s (ltime=%s)\n", 847 inet_ntop(AF_INET6, 848 &rda->ra_dns, 849 ntopbuf, 850 sizeof(ntopbuf)), 851 sec2str(ltime, ssbuf)); 852 p += sizeof(*rda); 853 } 854 } 855 } 856 857 return (0); 858} 859 860static int 861action_show_dnssl(void *msg) 862{ 863 struct dnssl *dns; 864 struct dnssl_addr *dna; 865 uint16_t *dns_cnt; 866 uint16_t *dna_cnt; 867 int i; 868 int j; 869 char *p; 870 uint32_t ltime; 871 char hbuf[NI_MAXHOST]; 872 char ssbuf[SSBUFLEN]; 873 874 p = msg; 875 dns_cnt = (uint16_t *)p; 876 p += sizeof(*dns_cnt); 877 878 if (*dns_cnt > 0) { 879 for (i = 0; i < *dns_cnt; i++) { 880 dns = (struct dnssl *)p; 881 ltime = dns->dn_ltime; 882 p += sizeof(*dns); 883 884 dna_cnt = (uint16_t *)p; 885 p += sizeof(*dna_cnt); 886 if (*dna_cnt > 0) 887 for (j = 0; j < *dna_cnt; j++) { 888 dna = (struct dnssl_addr *)p; 889 dname_labeldec(hbuf, sizeof(hbuf), 890 dna->da_dom); 891 printf("\t %s (ltime=%s)\n", 892 hbuf, sec2str(ltime, ssbuf)); 893 p += sizeof(*dna); 894 } 895 } 896 } 897 898 return (0); 899} 900 901/* Decode domain name label encoding in RFC 1035 Section 3.1 */ 902static size_t 903dname_labeldec(char *dst, size_t dlen, const char *src) 904{ 905 size_t len; 906 const char *src_origin; 907 const char *src_last; 908 const char *dst_origin; 909 910 src_origin = src; 911 src_last = strchr(src, '\0'); 912 dst_origin = dst; 913 memset(dst, '\0', dlen); 914 while (src && (len = (uint8_t)(*src++) & 0x3f) && 915 (src + len) <= src_last) { 916 if (dst != dst_origin) 917 *dst++ = '.'; 918 mysyslog(LOG_DEBUG, "<%s> labellen = %zd", __func__, len); 919 memcpy(dst, src, len); 920 src += len; 921 dst += len; 922 } 923 *dst = '\0'; 924 925 return (src - src_origin); 926} 927