1 /* $OpenBSD: ldpctl.c,v 1.9 2010/04/13 15:42:09 michele Exp $ 2 * 3 * Copyright (c) 2009 Michele Marchetto <michele@openbsd.org> 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 #include <net/if_media.h> 27 #include <net/if_types.h> 28 #include <netmpls/mpls.h> 29 30 #include <err.h> 31 #include <stdio.h> 32 #include <stdlib.h> 33 #include <string.h> 34 #include <unistd.h> 35 36 #include "ldp.h" 37 #include "ldpd.h" 38 #include "ldpe.h" 39 #include "parser.h" 40 41 __dead void usage(void); 42 const char *fmt_timeframe_core(time_t); 43 const char *get_linkstate(int, int); 44 int show_interface_msg(struct imsg *); 45 int get_ifms_type(int); 46 int show_lib_msg(struct imsg *); 47 int show_nbr_msg(struct imsg *); 48 void show_lfib_head(void); 49 int show_lfib_msg(struct imsg *); 50 void show_interface_head(void); 51 int show_lfib_interface_msg(struct imsg *); 52 const char *get_media_descr(int); 53 void print_baudrate(u_int64_t); 54 55 struct imsgbuf *ibuf; 56 57 __dead void 58 usage(void) 59 { 60 extern char *__progname; 61 62 fprintf(stderr, "usage: %s command [argument ...]\n", __progname); 63 exit(1); 64 } 65 66 int 67 main(int argc, char *argv[]) 68 { 69 struct sockaddr_un sun; 70 struct parse_result *res; 71 struct imsg imsg; 72 unsigned int ifidx = 0; 73 int ctl_sock; 74 int done = 0, verbose = 0; 75 int n; 76 77 /* parse options */ 78 if ((res = parse(argc - 1, argv + 1)) == NULL) 79 exit(1); 80 81 /* connect to ldpd control socket */ 82 if ((ctl_sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) 83 err(1, "socket"); 84 85 bzero(&sun, sizeof(sun)); 86 sun.sun_family = AF_UNIX; 87 strlcpy(sun.sun_path, LDPD_SOCKET, sizeof(sun.sun_path)); 88 if (connect(ctl_sock, (struct sockaddr *)&sun, sizeof(sun)) == -1) 89 err(1, "connect: %s", LDPD_SOCKET); 90 91 if ((ibuf = malloc(sizeof(struct imsgbuf))) == NULL) 92 err(1, NULL); 93 imsg_init(ibuf, ctl_sock); 94 done = 0; 95 96 /* process user request */ 97 switch (res->action) { 98 case NONE: 99 usage(); 100 /* not reached */ 101 case SHOW: 102 case SHOW_IFACE: 103 printf("%-11s %-18s %-10s %-10s %-8s\n", 104 "Interface", "Address", "State", "Linkstate", 105 "Uptime"); 106 if (*res->ifname) { 107 ifidx = if_nametoindex(res->ifname); 108 if (ifidx == 0) 109 errx(1, "no such interface %s", res->ifname); 110 } 111 imsg_compose(ibuf, IMSG_CTL_SHOW_INTERFACE, 0, 0, -1, 112 &ifidx, sizeof(ifidx)); 113 break; 114 case SHOW_NBR: 115 printf("%-15s %-18s %-15s %-9s %-10s\n", "ID", 116 "State", "Address", "Iface", "Uptime"); 117 imsg_compose(ibuf, IMSG_CTL_SHOW_NBR, 0, 0, -1, NULL, 0); 118 break; 119 case SHOW_LIB: 120 printf("%-20s %-17s %-14s %-14s %-10s\n", "Destination", 121 "Nexthop", "Local Label", "Remote Label", "In Use"); 122 imsg_compose(ibuf, IMSG_CTL_SHOW_LIB, 0, 0, -1, NULL, 0); 123 break; 124 case SHOW_LFIB: 125 if (!res->addr.s_addr) 126 imsg_compose(ibuf, IMSG_CTL_KROUTE, 0, 0, -1, 127 &res->flags, sizeof(res->flags)); 128 else 129 imsg_compose(ibuf, IMSG_CTL_KROUTE_ADDR, 0, 0, -1, 130 &res->addr, sizeof(res->addr)); 131 show_lfib_head(); 132 break; 133 case SHOW_LFIB_IFACE: 134 if (*res->ifname) 135 imsg_compose(ibuf, IMSG_CTL_IFINFO, 0, 0, -1, 136 res->ifname, sizeof(res->ifname)); 137 else 138 imsg_compose(ibuf, IMSG_CTL_IFINFO, 0, 0, -1, NULL, 0); 139 show_interface_head(); 140 break; 141 case LFIB: 142 errx(1, "lfib couple|decouple"); 143 break; 144 case LFIB_COUPLE: 145 imsg_compose(ibuf, IMSG_CTL_LFIB_COUPLE, 0, 0, -1, NULL, 0); 146 printf("couple request sent.\n"); 147 done = 1; 148 break; 149 case LFIB_DECOUPLE: 150 imsg_compose(ibuf, IMSG_CTL_LFIB_DECOUPLE, 0, 0, -1, NULL, 0); 151 printf("decouple request sent.\n"); 152 done = 1; 153 break; 154 case LOG_VERBOSE: 155 verbose = 1; 156 /* FALLTHROUGH */ 157 case LOG_BRIEF: 158 imsg_compose(ibuf, IMSG_CTL_LOG_VERBOSE, 0, 0, -1, 159 &verbose, sizeof(verbose)); 160 printf("logging request sent.\n"); 161 done = 1; 162 break; 163 case RELOAD: 164 imsg_compose(ibuf, IMSG_CTL_RELOAD, 0, 0, -1, NULL, 0); 165 printf("reload request sent.\n"); 166 done = 1; 167 break; 168 } 169 170 while (ibuf->w.queued) 171 if (msgbuf_write(&ibuf->w) < 0) 172 err(1, "write error"); 173 174 while (!done) { 175 if ((n = imsg_read(ibuf)) == -1) 176 errx(1, "imsg_read error"); 177 if (n == 0) 178 errx(1, "pipe closed"); 179 180 while (!done) { 181 if ((n = imsg_get(ibuf, &imsg)) == -1) 182 errx(1, "imsg_get error"); 183 if (n == 0) 184 break; 185 switch (res->action) { 186 case SHOW: 187 case SHOW_IFACE: 188 done = show_interface_msg(&imsg); 189 break; 190 case SHOW_NBR: 191 done = show_nbr_msg(&imsg); 192 break; 193 case SHOW_LIB: 194 done = show_lib_msg(&imsg); 195 break; 196 case SHOW_LFIB: 197 done = show_lfib_msg(&imsg); 198 break; 199 case SHOW_LFIB_IFACE: 200 done = show_lfib_interface_msg(&imsg); 201 break; 202 case NONE: 203 case LFIB: 204 case LFIB_COUPLE: 205 case LFIB_DECOUPLE: 206 case LOG_VERBOSE: 207 case LOG_BRIEF: 208 case RELOAD: 209 break; 210 } 211 imsg_free(&imsg); 212 } 213 } 214 close(ctl_sock); 215 free(ibuf); 216 217 return (0); 218 } 219 220 int 221 get_ifms_type(int mediatype) 222 { 223 switch (mediatype) { 224 case IFT_ETHER: 225 return (IFM_ETHER); 226 break; 227 case IFT_FDDI: 228 return (IFM_FDDI); 229 break; 230 case IFT_CARP: 231 return (IFM_CARP); 232 break; 233 default: 234 return (0); 235 break; 236 } 237 } 238 239 #define TF_BUFS 8 240 #define TF_LEN 9 241 242 const char * 243 fmt_timeframe_core(time_t t) 244 { 245 char *buf; 246 static char tfbuf[TF_BUFS][TF_LEN]; /* ring buffer */ 247 static int idx = 0; 248 unsigned int sec, min, hrs, day, week; 249 250 if (t == 0) 251 return ("Stopped"); 252 253 buf = tfbuf[idx++]; 254 if (idx == TF_BUFS) 255 idx = 0; 256 257 week = t; 258 259 sec = week % 60; 260 week /= 60; 261 min = week % 60; 262 week /= 60; 263 hrs = week % 24; 264 week /= 24; 265 day = week % 7; 266 week /= 7; 267 268 if (week > 0) 269 snprintf(buf, TF_LEN, "%02uw%01ud%02uh", week, day, hrs); 270 else if (day > 0) 271 snprintf(buf, TF_LEN, "%01ud%02uh%02um", day, hrs, min); 272 else 273 snprintf(buf, TF_LEN, "%02u:%02u:%02u", hrs, min, sec); 274 275 return (buf); 276 } 277 278 /* prototype defined in ldpd.h and shared with the kroute.c version */ 279 u_int8_t 280 mask2prefixlen(in_addr_t ina) 281 { 282 if (ina == 0) 283 return (0); 284 else 285 return (33 - ffs(ntohl(ina))); 286 } 287 288 int 289 show_interface_msg(struct imsg *imsg) 290 { 291 struct ctl_iface *iface; 292 char *netid; 293 294 switch (imsg->hdr.type) { 295 case IMSG_CTL_SHOW_INTERFACE: 296 iface = imsg->data; 297 298 if (asprintf(&netid, "%s/%d", inet_ntoa(iface->addr), 299 mask2prefixlen(iface->mask.s_addr)) == -1) 300 err(1, NULL); 301 printf("%-11s %-18s %-10s %-10s %-8s\n", 302 iface->name, netid, if_state_name(iface->state), 303 get_linkstate(iface->mediatype, iface->linkstate), 304 iface->uptime == 0 ? "00:00:00" : 305 fmt_timeframe_core(iface->uptime)); 306 free(netid); 307 break; 308 case IMSG_CTL_END: 309 printf("\n"); 310 return (1); 311 default: 312 break; 313 } 314 315 return (0); 316 } 317 318 int 319 show_lib_msg(struct imsg *imsg) 320 { 321 struct ctl_rt *rt; 322 char *dstnet, *remote; 323 int remote_label; 324 325 switch (imsg->hdr.type) { 326 case IMSG_CTL_SHOW_LIB: 327 rt = imsg->data; 328 if (asprintf(&dstnet, "%s/%d", inet_ntoa(rt->prefix), 329 rt->prefixlen) == -1) 330 err(1, NULL); 331 remote_label = ntohl(rt->remote_label) >> MPLS_LABEL_OFFSET; 332 if (!rt->in_use) { 333 if (asprintf(&remote, "-") == -1) 334 err(1, NULL); 335 } else if (rt->connected || 336 (remote_label == (NO_LABEL >> MPLS_LABEL_OFFSET))) { 337 if (asprintf(&remote, "Untagged") == -1) 338 err(1, NULL); 339 } else if (remote_label == MPLS_LABEL_IMPLNULL) { 340 if (asprintf(&remote, "Pop tag") == -1) 341 err(1, NULL); 342 } else { 343 if (asprintf(&remote, "%u", remote_label) == -1) 344 err(1, NULL); 345 } 346 347 printf("%-20s %-17s %-14u %-14s %s\n", dstnet, 348 inet_ntoa(rt->nexthop), 349 (ntohl(rt->local_label) >> MPLS_LABEL_OFFSET), 350 remote, rt->in_use ? "yes" : "no"); 351 free(remote); 352 free(dstnet); 353 354 break; 355 case IMSG_CTL_END: 356 printf("\n"); 357 return (1); 358 default: 359 break; 360 } 361 362 return (0); 363 } 364 365 int 366 show_nbr_msg(struct imsg *imsg) 367 { 368 struct ctl_nbr *nbr; 369 char *state; 370 371 switch (imsg->hdr.type) { 372 case IMSG_CTL_SHOW_NBR: 373 nbr = imsg->data; 374 if (asprintf(&state, "%s/%s", nbr_state_name(nbr->nbr_state), 375 if_state_name(nbr->iface_state)) == -1) 376 err(1, NULL); 377 printf("%-15s %-19s", inet_ntoa(nbr->id), 378 state); 379 printf("%-15s %-10s", inet_ntoa(nbr->addr), nbr->name); 380 printf("%-15s\n", nbr->uptime == 0 ? "-" : 381 fmt_timeframe_core(nbr->uptime)); 382 free(state); 383 break; 384 case IMSG_CTL_END: 385 printf("\n"); 386 return (1); 387 default: 388 break; 389 } 390 391 return (0); 392 } 393 394 void 395 show_lfib_head(void) 396 { 397 printf("flags: * = valid, C = Connected, S = Static\n"); 398 printf("%-6s %-20s %-17s %-17s %s\n", "Flags", "Destination", "Nexthop", 399 "Local Label", "Remote Label"); 400 } 401 402 int 403 show_lfib_msg(struct imsg *imsg) 404 { 405 struct kroute *k; 406 char *p; 407 408 switch (imsg->hdr.type) { 409 case IMSG_CTL_KROUTE: 410 if (imsg->hdr.len < IMSG_HEADER_SIZE + sizeof(struct kroute)) 411 errx(1, "wrong imsg len"); 412 k = imsg->data; 413 414 if (k->flags & F_DOWN) 415 printf(" "); 416 else 417 printf("*"); 418 419 if (!(k->flags & F_KERNEL)) 420 printf("R"); 421 else if (k->flags & F_CONNECTED) 422 printf("C"); 423 else if (k->flags & F_STATIC) 424 printf("S"); 425 else 426 printf(" "); 427 428 printf(" "); 429 if (asprintf(&p, "%s/%u", inet_ntoa(k->prefix), 430 k->prefixlen) == -1) 431 err(1, NULL); 432 printf("%-20s ", p); 433 free(p); 434 435 if (k->nexthop.s_addr) 436 printf("%-18s", inet_ntoa(k->nexthop)); 437 else if (k->flags & F_CONNECTED) 438 printf("link#%-13u", k->ifindex); 439 440 if (k->local_label == NO_LABEL) { 441 printf("%-18s", "-"); 442 } else if (ntohl(k->local_label) >> MPLS_LABEL_OFFSET == 443 MPLS_LABEL_IMPLNULL) { 444 printf("%-18s", "imp-null"); 445 } else 446 printf("%-18u", (ntohl(k->local_label) >> 447 MPLS_LABEL_OFFSET)); 448 449 if (k->remote_label == NO_LABEL) { 450 printf("-"); 451 } else if (htonl(k->remote_label) >> MPLS_LABEL_OFFSET == 452 MPLS_LABEL_IMPLNULL) { 453 printf("Pop"); 454 } else { 455 printf("%u", (ntohl(k->remote_label) >> 456 MPLS_LABEL_OFFSET)); 457 } 458 459 printf("\n"); 460 461 break; 462 case IMSG_CTL_END: 463 printf("\n"); 464 return (1); 465 default: 466 break; 467 } 468 469 return (0); 470 } 471 472 void 473 show_interface_head(void) 474 { 475 printf("%-15s%-15s%s\n", "Interface", "Flags", 476 "Link state"); 477 } 478 479 int 480 show_lfib_interface_msg(struct imsg *imsg) 481 { 482 struct kif *k; 483 int ifms_type; 484 485 switch (imsg->hdr.type) { 486 case IMSG_CTL_IFINFO: 487 k = imsg->data; 488 printf("%-15s", k->ifname); 489 printf("%-15s", k->flags & IFF_UP ? "UP" : ""); 490 ifms_type = get_ifms_type(k->media_type); 491 if (ifms_type) 492 printf("%s, ", get_media_descr(ifms_type)); 493 494 printf("%s", get_linkstate(k->media_type, k->link_state)); 495 496 if (k->link_state != LINK_STATE_DOWN && k->baudrate > 0) { 497 printf(", "); 498 print_baudrate(k->baudrate); 499 } 500 printf("\n"); 501 break; 502 case IMSG_CTL_END: 503 printf("\n"); 504 return (1); 505 default: 506 break; 507 } 508 509 return (0); 510 } 511 512 const struct if_status_description 513 if_status_descriptions[] = LINK_STATE_DESCRIPTIONS; 514 const struct ifmedia_description 515 ifm_type_descriptions[] = IFM_TYPE_DESCRIPTIONS; 516 517 const char * 518 get_media_descr(int media_type) 519 { 520 const struct ifmedia_description *p; 521 522 for (p = ifm_type_descriptions; p->ifmt_string != NULL; p++) 523 if (media_type == p->ifmt_word) 524 return (p->ifmt_string); 525 526 return ("unknown"); 527 } 528 529 const char * 530 get_linkstate(int media_type, int link_state) 531 { 532 const struct if_status_description *p; 533 static char buf[8]; 534 535 for (p = if_status_descriptions; p->ifs_string != NULL; p++) { 536 if (LINK_STATE_DESC_MATCH(p, media_type, link_state)) 537 return (p->ifs_string); 538 } 539 snprintf(buf, sizeof(buf), "[#%d]", link_state); 540 return (buf); 541 } 542 543 void 544 print_baudrate(u_int64_t baudrate) 545 { 546 if (baudrate > IF_Gbps(1)) 547 printf("%llu GBit/s", baudrate / IF_Gbps(1)); 548 else if (baudrate > IF_Mbps(1)) 549 printf("%llu MBit/s", baudrate / IF_Mbps(1)); 550 else if (baudrate > IF_Kbps(1)) 551 printf("%llu KBit/s", baudrate / IF_Kbps(1)); 552 else 553 printf("%llu Bit/s", baudrate); 554 } 555