ospfctl.c revision 1.6
1/* $OpenBSD: ospfctl.c,v 1.6 2005/03/12 11:03:05 norby Exp $ */ 2 3/* 4 * Copyright (c) 2005 Claudio Jeker <claudio@openbsd.org> 5 * Copyright (c) 2004, 2005 Esben Norby <norby@openbsd.org> 6 * Copyright (c) 2003 Henning Brauer <henning@openbsd.org> 7 * 8 * Permission to use, copy, modify, and distribute this software for any 9 * purpose with or without fee is hereby granted, provided that the above 10 * copyright notice and this permission notice appear in all copies. 11 * 12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 19 */ 20 21#include <sys/types.h> 22#include <sys/socket.h> 23#include <sys/un.h> 24#include <netinet/in.h> 25#include <arpa/inet.h> 26 27#include <err.h> 28#include <stdio.h> 29#include <stdlib.h> 30#include <string.h> 31#include <unistd.h> 32 33#include "ospf.h" 34#include "ospfd.h" 35#include "ospfe.h" 36#include "parser.h" 37#include "log.h" 38 39__dead void usage(void); 40int show_summary_msg(struct imsg *, int); 41int show_interface_msg(struct imsg *); 42void print_baudrate(u_long); 43const char *print_if_type(enum iface_type type); 44const char *print_if_state(int); 45const char *print_nbr_state(int); 46const char *print_link(int); 47const char *fmt_timeframe(time_t t); 48const char *fmt_timeframe_core(time_t t); 49const char *log_id(u_int32_t ); 50const char *log_adv_rtr(u_int32_t); 51u_int8_t mask2prefixlen(struct in_addr); 52void show_database_head(struct in_addr, u_int8_t); 53int show_database_msg(struct imsg *); 54int show_nbr_msg(struct imsg *); 55const char *print_ospf_options(u_int8_t); 56int show_nbr_detail_msg(struct imsg *); 57int show_rib_msg(struct imsg *); 58void show_rib_head(struct in_addr, u_int8_t, u_int8_t); 59int show_rib_detail_msg(struct imsg *); 60 61struct imsgbuf *ibuf; 62 63__dead void 64usage(void) 65{ 66 extern char *__progname; 67 68 fprintf(stderr, "usage: %s <command> [arg [...]]\n", __progname); 69 exit(1); 70} 71 72void 73imsg_event_add(struct imsgbuf *i) 74{ 75} 76 77int 78main(int argc, char *argv[]) 79{ 80 struct sockaddr_un sun; 81 struct parse_result *res; 82 struct imsg imsg; 83 unsigned int ifidx = 0; 84 int ctl_sock; 85 int nodescr = 0; 86 int done = 0; 87 int n; 88 89 /* parse options */ 90 if ((res = parse(argc - 1, argv + 1)) == NULL) 91 exit(1); 92 93 /* connect to ospfd control socket */ 94 if ((ctl_sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) 95 err(1, "socket"); 96 97 bzero(&sun, sizeof(sun)); 98 sun.sun_family = AF_UNIX; 99 strlcpy(sun.sun_path, OSPFD_SOCKET, sizeof(sun.sun_path)); 100 if (connect(ctl_sock, (struct sockaddr *)&sun, sizeof(sun)) == -1) 101 err(1, "connect: %s", OSPFD_SOCKET); 102 103 if ((ibuf = malloc(sizeof(struct imsgbuf))) == NULL) 104 fatal(NULL); 105 imsg_init(ibuf, ctl_sock, NULL); 106 done = 0; 107 108 /* process user request */ 109 switch (res->action) { 110 case NONE: 111 usage(); 112 /* not reached */ 113 case SHOW: 114 case SHOW_SUMMARY: 115 break; 116 case SHOW_IFACE: 117 if (*res->ifname) { 118 ifidx = if_nametoindex(res->ifname); 119 if (ifidx == 0) 120 errx(1, "no such interface %s", res->ifname); 121 } 122 imsg_compose(ibuf, IMSG_CTL_SHOW_INTERFACE, 0, 0, -1, 123 &ifidx, sizeof(ifidx)); 124 break; 125 case SHOW_NBR: 126 printf("%-15s %-3s %-17s %-9s %-15s %s\n", "ID", "Pri", 127 "State", "DeadTime", "Address", "Interface"); 128 case SHOW_NBR_DTAIL: 129 imsg_compose(ibuf, IMSG_CTL_SHOW_NBR, 0, 0, -1, NULL, 0); 130 break; 131 case SHOW_DB: 132 imsg_compose(ibuf, IMSG_CTL_SHOW_DATABASE, 0, 0, -1, NULL, 0); 133 break; 134 case SHOW_DBBYAREA: 135 imsg_compose(ibuf, IMSG_CTL_SHOW_DATABASE, 0, 0, -1, 136 &res->addr, sizeof(res->addr)); 137 break; 138 case SHOW_RIB: 139 printf("%-20s %-17s %-12s %-9s %-7s\n", "Destination", 140 "Nexthop", "Path Type", "Type", "Cost"); 141 case SHOW_RIB_DTAIL: 142 imsg_compose(ibuf, IMSG_CTL_SHOW_RIB, 0, 0, -1, NULL, 0); 143 break; 144 case RELOAD: 145 imsg_compose(ibuf, IMSG_CTL_RELOAD, 0, 0, -1, NULL, 0); 146 printf("reload request sent.\n"); 147 done = 1; 148 break; 149 } 150 151 while (ibuf->w.queued) 152 if (msgbuf_write(&ibuf->w) < 0) 153 err(1, "write error"); 154 155 while (!done) { 156 if ((n = imsg_read(ibuf)) == -1) 157 errx(1, "imsg_read error"); 158 if (n == 0) 159 errx(1, "pipe closed"); 160 161 while (!done) { 162 if ((n = imsg_get(ibuf, &imsg)) == -1) 163 errx(1, "imsg_get error"); 164 if (n == 0) 165 break; 166 switch (res->action) { 167 case SHOW: 168 case SHOW_SUMMARY: 169 done = show_summary_msg(&imsg, nodescr); 170 break; 171 case SHOW_IFACE: 172 done = show_interface_msg(&imsg); 173 break; 174 case SHOW_NBR: 175 done = show_nbr_msg(&imsg); 176 break; 177 case SHOW_NBR_DTAIL: 178 done = show_nbr_detail_msg(&imsg); 179 break; 180 case SHOW_DB: 181 case SHOW_DBBYAREA: 182 done = show_database_msg(&imsg); 183 break; 184 case SHOW_RIB: 185 done = show_rib_msg(&imsg); 186 break; 187 case SHOW_RIB_DTAIL: 188 done = show_rib_detail_msg(&imsg); 189 break; 190 case NONE: 191 case RELOAD: 192 break; 193 } 194 imsg_free(&imsg); 195 } 196 } 197 close(ctl_sock); 198 free(ibuf); 199 200 return (0); 201} 202 203int 204show_summary_msg(struct imsg *imsg, int nodescr) 205{ 206 207 return (0); 208} 209 210int 211show_interface_msg(struct imsg *imsg) 212{ 213 struct ctl_iface *iface; 214 215 switch (imsg->hdr.type) { 216 case IMSG_CTL_SHOW_INTERFACE: 217 iface = imsg->data; 218 printf("\n"); 219 printf("Interface %s is %d, line protocol is %s\n", 220 iface->name, iface->linkstate, print_link(iface->flags)); 221 printf(" Internet address %s/%d, ", 222 inet_ntoa(iface->addr), 223 mask2prefixlen(iface->mask)); 224 printf("Area %s\n", inet_ntoa(iface->area)); 225 printf(" Router ID %s, network type %s, cost: %d\n", 226 inet_ntoa(iface->rtr_id), 227 print_if_type(iface->type), iface->metric); 228 printf(" Transmit delay is %d sec, state %s, priority %d\n", 229 iface->transfer_delay, print_if_state(iface->state), 230 iface->priority); 231 printf(" Designated Router (ID) %s, ", 232 inet_ntoa(iface->dr_id)); 233 printf("interface address %s\n", inet_ntoa(iface->dr_addr)); 234 printf(" Backup Designated Router (ID) %s, ", 235 inet_ntoa(iface->bdr_id)); 236 printf("interface address %s\n", inet_ntoa(iface->bdr_addr)); 237 printf(" Timer intervals configured, " 238 "hello %d, dead %d, wait %d, retransmit %d\n", 239 iface->hello_interval, iface->dead_interval, 240 iface->dead_interval, iface->rxmt_interval); 241 if (iface->hello_timer < 0) 242 printf(" Hello timer not running\n"); 243 else 244 printf(" Hello timer due in %s\n", 245 fmt_timeframe_core(iface->hello_timer)); 246 printf(" Neighbor count is %d, adjacent neighbor count is " 247 "%d\n", iface->nbr_cnt, iface->adj_cnt); 248 break; 249 case IMSG_CTL_END: 250 printf("\n"); 251 return (1); 252 default: 253 break; 254 } 255 256 return (0); 257} 258 259const char * 260print_if_type(enum iface_type type) 261{ 262 switch (type) { 263 case IF_TYPE_POINTOPOINT: 264 return ("POINTOPOINT"); 265 case IF_TYPE_BROADCAST: 266 return ("BROADCAST"); 267 case IF_TYPE_NBMA: 268 return ("NBMA"); 269 case IF_TYPE_POINTOMULTIPOINT: 270 return ("POINTOMULTIPOINT"); 271 case IF_TYPE_VIRTUALLINK: 272 return ("VIRTUALLINK"); 273 default: 274 return ("UNKNOWN"); 275 } 276} 277 278const char * 279print_if_state(int state) 280{ 281 switch (state) { 282 case IF_STA_DOWN: 283 return ("DOWN"); 284 case IF_STA_LOOPBACK: 285 return ("LOOPBACK"); 286 case IF_STA_WAITING: 287 return ("WAITING"); 288 case IF_STA_POINTTOPOINT: 289 return ("P2P"); 290 case IF_STA_DROTHER: 291 return ("DROTHER"); 292 case IF_STA_BACKUP: 293 return ("BACKUP"); 294 case IF_STA_DR: 295 return ("DR"); 296 default: 297 return ("UNKNOWN"); 298 } 299} 300 301const char * 302print_nbr_state(int state) 303{ 304 switch (state) { 305 case NBR_STA_DOWN: 306 return ("DOWN"); 307 case NBR_STA_ATTEMPT: 308 return ("ATTEMPT"); 309 case NBR_STA_INIT: 310 return ("INIT"); 311 case NBR_STA_2_WAY: 312 return ("2-WAY"); 313 case NBR_STA_XSTRT: 314 return ("EXSTART"); 315 case NBR_STA_SNAP: 316 return ("SNAPSHOT"); 317 case NBR_STA_XCHNG: 318 return ("EXCHANGE"); 319 case NBR_STA_LOAD: 320 return ("LOADING"); 321 case NBR_STA_FULL: 322 return ("FULL"); 323 default: 324 return ("UNKNOWN"); 325 } 326} 327 328const char * 329print_link(int state) 330{ 331 if (state & IFF_UP) 332 return ("UP"); 333 else 334 return ("DOWN"); 335} 336 337#define TF_BUFS 8 338#define TF_LEN 9 339 340const char * 341fmt_timeframe(time_t t) 342{ 343 if (t == 0) 344 return ("Never"); 345 else 346 return (fmt_timeframe_core(time(NULL) - t)); 347} 348 349const char * 350fmt_timeframe_core(time_t t) 351{ 352 char *buf; 353 static char tfbuf[TF_BUFS][TF_LEN]; /* ring buffer */ 354 static int idx = 0; 355 unsigned sec, min, hrs, day, week; 356 357 if (t == 0) 358 return ("Stopped"); 359 360 buf = tfbuf[idx++]; 361 if (idx == TF_BUFS) 362 idx = 0; 363 364 week = t; 365 366 sec = week % 60; 367 week /= 60; 368 min = week % 60; 369 week /= 60; 370 hrs = week % 24; 371 week /= 24; 372 day = week % 7; 373 week /= 7; 374 375 if (week > 0) 376 snprintf(buf, TF_LEN, "%02uw%01ud%02uh", week, day, hrs); 377 else if (day > 0) 378 snprintf(buf, TF_LEN, "%01ud%02uh%02um", day, hrs, min); 379 else 380 snprintf(buf, TF_LEN, "%02u:%02u:%02u", hrs, min, sec); 381 382 return (buf); 383} 384 385const char * 386log_id(u_int32_t id) 387{ 388 static char buf[48]; 389 struct in_addr addr; 390 391 addr.s_addr = id; 392 393 if (inet_ntop(AF_INET, &addr, buf, sizeof(buf)) == NULL) 394 return ("?"); 395 else 396 return (buf); 397} 398 399const char * 400log_adv_rtr(u_int32_t adv_rtr) 401{ 402 static char buf[48]; 403 struct in_addr addr; 404 405 addr.s_addr = adv_rtr; 406 407 if (inet_ntop(AF_INET, &addr, buf, sizeof(buf)) == NULL) 408 return ("?"); 409 else 410 return (buf); 411} 412 413u_int8_t 414mask2prefixlen(struct in_addr ina) 415{ 416 if (ina.s_addr == 0) 417 return (0); 418 else 419 return (33 - ffs(ntohl(ina.s_addr))); 420} 421 422void 423show_database_head(struct in_addr aid, u_int8_t type) 424{ 425 char *header, *format; 426 427 switch (type) { 428 case LSA_TYPE_ROUTER: 429 format = "Router Link States"; 430 break; 431 case LSA_TYPE_NETWORK: 432 format = "Net Link States"; 433 break; 434 case LSA_TYPE_SUM_NETWORK: 435 format = "Summary Net Link States"; 436 break; 437 case LSA_TYPE_SUM_ROUTER: 438 format = "Summary Router Link States"; 439 break; 440 case LSA_TYPE_EXTERNAL: 441 format = NULL; 442 if ((header = strdup("Type-5 AS External Link States")) == NULL) 443 err(1, NULL); 444 break; 445 default: 446 errx(1, "unknown LSA type"); 447 } 448 if (type != LSA_TYPE_EXTERNAL) 449 if (asprintf(&header, "%s (Area %s)", format, 450 inet_ntoa(aid)) == -1) 451 err(1, NULL); 452 453 printf("\n%-15s %s\n", "", header); 454 free(header); 455 456 printf("\n%-15s %-15s %-4s %-10s %-8s\n", "Link ID", "Adv Router", 457 "Age", "Seq#", "Checksum"); 458} 459 460int 461show_database_msg(struct imsg *imsg) 462{ 463 static struct in_addr area_id; 464 static u_int8_t lasttype; 465 struct area *area; 466 struct lsa_hdr *lsa; 467 468 switch (imsg->hdr.type) { 469 case IMSG_CTL_SHOW_DATABASE: 470 lsa = imsg->data; 471 if (lsa->type != lasttype) 472 show_database_head(area_id, lsa->type); 473 printf("%-15s %-15s %-4d 0x%08x 0x%04x\n", 474 log_id(lsa->ls_id), log_adv_rtr(lsa->adv_rtr), 475 ntohs(lsa->age), ntohl(lsa->seq_num), 476 ntohs(lsa->ls_chksum)); 477 lasttype = lsa->type; 478 break; 479 case IMSG_CTL_AREA: 480 area = imsg->data; 481 area_id = area->id; 482 break; 483 case IMSG_CTL_END: 484 return (1); 485 default: 486 break; 487 } 488 489 return (0); 490} 491 492int 493show_nbr_msg(struct imsg *imsg) 494{ 495 struct ctl_nbr *nbr; 496 char *state; 497 498 switch (imsg->hdr.type) { 499 case IMSG_CTL_SHOW_NBR: 500 nbr = imsg->data; 501 if (asprintf(&state, "%s/%s", print_nbr_state(nbr->nbr_state), 502 print_if_state(nbr->iface_state)) == -1) 503 err(1, NULL); 504 printf("%-15s %-3d %-17s %-9s ", inet_ntoa(nbr->id), 505 nbr->priority, state, fmt_timeframe_core(nbr->dead_timer)); 506 printf("%-15s %s\n", inet_ntoa(nbr->addr), nbr->name); 507 free(state); 508 break; 509 case IMSG_CTL_END: 510 printf("\n"); 511 return (1); 512 default: 513 break; 514 } 515 516 return (0); 517} 518 519const char * 520print_ospf_options(u_int8_t opts) 521{ 522 static char optbuf[32]; 523 524 snprintf(optbuf, sizeof(optbuf), "*|*|%s|%s|%s|%s|%s|*", 525 opts & OSPF_OPTION_DC ? "DC" : "-", 526 opts & OSPF_OPTION_EA ? "EA" : "-", 527 opts & OSPF_OPTION_NP ? "N/P" : "-", 528 opts & OSPF_OPTION_MC ? "MC" : "-", 529 opts & OSPF_OPTION_E ? "E" : "-"); 530 return (optbuf); 531} 532 533int 534show_nbr_detail_msg(struct imsg *imsg) 535{ 536 struct ctl_nbr *nbr; 537 538 switch (imsg->hdr.type) { 539 case IMSG_CTL_SHOW_NBR: 540 nbr = imsg->data; 541 printf("\nNeighbor %s, ", inet_ntoa(nbr->id)); 542 printf("interface address %s\n", inet_ntoa(nbr->addr)); 543 printf(" In the area %s via interface %s\n", 544 inet_ntoa(nbr->area), nbr->name); 545 printf(" Neighbor priority is %d, " 546 "State is %s, %d state changes\n", 547 nbr->priority, print_nbr_state(nbr->nbr_state), 548 nbr->state_chng_cnt); 549 printf(" DR is %s, ", inet_ntoa(nbr->dr)); 550 printf("BDR is %s\n", inet_ntoa(nbr->bdr)); 551 printf(" Options is 0x%x %s\n", nbr->options, 552 print_ospf_options(nbr->options)); 553 printf(" Dead timer due in %s\n", 554 fmt_timeframe_core(nbr->dead_timer)); 555 printf(" Database Summary List %d\n", nbr->db_sum_lst_cnt); 556 printf(" Link State Request List %d\n", nbr->ls_req_lst_cnt); 557 printf(" Link State Retransmission List %d\n", 558 nbr->ls_retrans_lst_cnt); 559 break; 560 case IMSG_CTL_END: 561 printf("\n"); 562 return (1); 563 default: 564 break; 565 } 566 567 return (0); 568} 569 570int 571show_rib_msg(struct imsg *imsg) 572{ 573 struct ctl_rt *rt; 574 char *dstnet; 575 576 switch (imsg->hdr.type) { 577 case IMSG_CTL_SHOW_RIB: 578 rt = imsg->data; 579 switch (rt->d_type) { 580 case DT_NET: 581 if (asprintf(&dstnet, "%s/%d", inet_ntoa(rt->prefix), 582 rt->prefixlen) == -1) 583 err(1, NULL); 584 break; 585 case DT_RTR: 586 if (asprintf(&dstnet, "%s", 587 inet_ntoa(rt->prefix)) == -1) 588 err(1, NULL); 589 break; 590 default: 591 errx(1, "Invalid route type"); 592 } 593 594 printf("%-20s %-17s %-12s %-9s %-7d\n", dstnet, 595 inet_ntoa(rt->nexthop), path_type_names[rt->p_type], 596 dst_type_names[rt->d_type], rt->cost); 597 free(dstnet); 598 break; 599 case IMSG_CTL_END: 600 printf("\n"); 601 return (1); 602 default: 603 break; 604 } 605 606 return (0); 607} 608 609void 610show_rib_head(struct in_addr aid, u_int8_t d_type, u_int8_t p_type) 611{ 612 char *header, *format; 613 614 switch (p_type) { 615 case PT_INTRA_AREA: 616 case PT_INTER_AREA: 617 switch (d_type) { 618 case DT_NET: 619 format = "Network Routing Table"; 620 break; 621 case DT_RTR: 622 format = "Router Routing Table"; 623 break; 624 default: 625 errx(1, "unknown route type"); 626 } 627 break; 628 case PT_TYPE1_EXT: 629 case PT_TYPE2_EXT: 630 format = NULL; 631 if ((header = strdup("External Routing Table")) == NULL) 632 err(1, NULL); 633 break; 634 default: 635 errx(1, "unknown route type"); 636 } 637 638 if (p_type != PT_TYPE1_EXT && p_type != PT_TYPE2_EXT) 639 if (asprintf(&header, "%s (Area %s)", format, 640 inet_ntoa(aid)) == -1) 641 err(1, NULL); 642 643 printf("\n%-15s %s\n", "", header); 644 free(header); 645 646 printf("\n%-20s %-17s %-17s %-12s %-8s\n", 647 "Destination", "Nexthop", "Adv Router", "Path type", "Cost"); 648 649} 650 651int 652show_rib_detail_msg(struct imsg *imsg) 653{ 654 static struct in_addr area_id; 655 struct ctl_rt *rt; 656 struct area *area; 657 char *dstnet; 658 static u_int8_t lasttype; 659 660 switch (imsg->hdr.type) { 661 case IMSG_CTL_SHOW_RIB: 662 rt = imsg->data; 663 664 switch (rt->p_type) { 665 case PT_INTRA_AREA: 666 case PT_INTER_AREA: 667 switch (rt->d_type) { 668 case DT_NET: 669 if (lasttype != RIB_NET) 670 show_rib_head(rt->area, rt->d_type, 671 rt->p_type); 672 if (asprintf(&dstnet, "%s/%d", 673 inet_ntoa(rt->prefix), rt->prefixlen) == -1) 674 err(1, NULL); 675 lasttype = RIB_NET; 676 break; 677 case DT_RTR: 678 if (lasttype != RIB_RTR) 679 show_rib_head(rt->area, rt->d_type, 680 rt->p_type); 681 if (asprintf(&dstnet, "%s", 682 inet_ntoa(rt->prefix)) == -1) 683 err(1, NULL); 684 lasttype = RIB_RTR; 685 break; 686 default: 687 errx(1, "unknown route type"); 688 } 689 printf("%-20s %-17s ", dstnet, inet_ntoa(rt->nexthop)); 690 printf("%-17s %-12s %-7d\n", inet_ntoa(rt->adv_rtr), 691 path_type_names[rt->p_type], rt->cost); 692 free(dstnet); 693 break; 694 case PT_TYPE1_EXT: 695 case PT_TYPE2_EXT: 696 /* XXX TODO */ 697 break; 698 default: 699 errx(1, "unknown route type"); 700 } 701 break; 702 case IMSG_CTL_AREA: 703 area = imsg->data; 704 area_id = area->id; 705 break; 706 case IMSG_CTL_END: 707 printf("\n"); 708 return (1); 709 default: 710 break; 711 } 712 713 return (0); 714} 715