1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1983, 1989, 1991, 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 #ifndef lint 33 static const char copyright[] = 34 "@(#) Copyright (c) 1983, 1989, 1991, 1993\n\ 35 The Regents of the University of California. All rights reserved.\n"; 36 #endif /* not lint */ 37 38 #ifndef lint 39 #if 0 40 static char sccsid[] = "@(#)route.c 8.6 (Berkeley) 4/28/95"; 41 #endif 42 #endif /* not lint */ 43 44 #include <sys/cdefs.h> 45 #include <sys/param.h> 46 #include <sys/file.h> 47 #include <sys/socket.h> 48 #include <sys/ioctl.h> 49 #ifdef JAIL 50 #include <sys/jail.h> 51 #endif 52 #include <sys/sysctl.h> 53 #include <sys/types.h> 54 #include <sys/queue.h> 55 56 #include <net/if.h> 57 #include <net/route.h> 58 #include <net/if_dl.h> 59 #include <netinet/in.h> 60 #include <netinet/if_ether.h> 61 #include <arpa/inet.h> 62 #include <netdb.h> 63 64 #include <ctype.h> 65 #include <err.h> 66 #include <errno.h> 67 #ifdef JAIL 68 #include <jail.h> 69 #endif 70 #include <paths.h> 71 #include <signal.h> 72 #include <stdbool.h> 73 #include <stdio.h> 74 #include <stdlib.h> 75 #include <string.h> 76 #include <sysexits.h> 77 #include <time.h> 78 #include <unistd.h> 79 #include <ifaddrs.h> 80 81 struct fibl { 82 TAILQ_ENTRY(fibl) fl_next; 83 84 int fl_num; 85 int fl_error; 86 int fl_errno; 87 }; 88 89 static struct keytab { 90 const char *kt_cp; 91 int kt_i; 92 } const keywords[] = { 93 #include "keywords.h" 94 {0, 0} 95 }; 96 97 int verbose, debugonly; 98 #ifdef JAIL 99 char * jail_name; 100 #endif 101 static struct sockaddr_storage so[RTAX_MAX]; 102 static int pid, rtm_addrs; 103 static int nflag, af, aflen, qflag, tflag; 104 static int locking, lockrest; 105 static struct rt_metrics rt_metrics; 106 static u_long rtm_inits; 107 static uid_t uid; 108 static int defaultfib; 109 static int numfibs; 110 static char domain[MAXHOSTNAMELEN + 1]; 111 static bool domain_initialized; 112 static char rt_line[NI_MAXHOST]; 113 static char net_line[MAXHOSTNAMELEN + 1]; 114 115 #ifdef WITHOUT_NETLINK 116 static int s; 117 static int rtm_seq; 118 119 static struct { 120 struct rt_msghdr m_rtm; 121 char m_space[512]; 122 } m_rtmsg; 123 124 static int rtmsg_rtsock(int, int, int); 125 static int flushroutes_fib_rtsock(int); 126 static void monitor_rtsock(void); 127 #else 128 int rtmsg_nl(int, int, int, int, struct sockaddr_storage *, struct rt_metrics *); 129 int flushroutes_fib_nl(int, int); 130 void monitor_nl(int); 131 #endif 132 133 static TAILQ_HEAD(fibl_head_t, fibl) fibl_head; 134 135 void printb(int, const char *); 136 static void flushroutes(int argc, char *argv[]); 137 static int flushroutes_fib(int); 138 static int getaddr(int, char *, int); 139 static int keyword(const char *); 140 #ifdef INET 141 static void inet_makemask(struct sockaddr_in *, u_long); 142 #endif 143 #ifdef INET6 144 static int inet6_makenetandmask(struct sockaddr_in6 *, const char *); 145 #endif 146 static void interfaces(void); 147 static void monitor(int, char*[]); 148 const char *netname(struct sockaddr *); 149 static void newroute(int, char **); 150 static int newroute_fib(int, char *, int); 151 static void pmsg_addrs(char *, int, size_t); 152 static void pmsg_common(struct rt_msghdr *, size_t); 153 static int prefixlen(const char *); 154 static void print_getmsg(struct rt_msghdr *, int, int); 155 static void print_rtmsg(struct rt_msghdr *, size_t); 156 const char *routename(struct sockaddr *); 157 static int rtmsg(int, int, int); 158 static void set_metric(char *, int); 159 static int set_sofib(int); 160 static void sockaddr(char *, struct sockaddr *, size_t); 161 static void sodump(struct sockaddr *, const char *); 162 static int fiboptlist_csv(const char *, struct fibl_head_t *); 163 static int fiboptlist_range(const char *, struct fibl_head_t *); 164 165 static void usage(const char *) __dead2; 166 167 #define READ_TIMEOUT 10 168 static volatile sig_atomic_t stop_read; 169 170 static void 171 stopit(int sig __unused) 172 { 173 174 stop_read = 1; 175 } 176 177 static void 178 usage(const char *cp) 179 { 180 if (cp != NULL) 181 warnx("bad keyword: %s", cp); 182 errx(EX_USAGE, "usage: route [-j jail] [-46dnqtv] command [[modifiers] args]"); 183 /* NOTREACHED */ 184 } 185 186 int 187 main(int argc, char **argv) 188 { 189 int ch; 190 #ifdef JAIL 191 int jid; 192 #endif 193 size_t len; 194 195 if (argc < 2) 196 usage(NULL); 197 198 while ((ch = getopt(argc, argv, "46nqdtvj:")) != -1) 199 switch(ch) { 200 case '4': 201 #ifdef INET 202 af = AF_INET; 203 aflen = sizeof(struct sockaddr_in); 204 #else 205 errx(1, "IPv4 support is not compiled in"); 206 #endif 207 break; 208 case '6': 209 #ifdef INET6 210 af = AF_INET6; 211 aflen = sizeof(struct sockaddr_in6); 212 #else 213 errx(1, "IPv6 support is not compiled in"); 214 #endif 215 break; 216 case 'n': 217 nflag = 1; 218 break; 219 case 'q': 220 qflag = 1; 221 break; 222 case 'v': 223 verbose = 1; 224 break; 225 case 't': 226 tflag = 1; 227 break; 228 case 'd': 229 debugonly = 1; 230 break; 231 case 'j': 232 #ifdef JAIL 233 if (optarg == NULL) 234 usage(NULL); 235 jail_name = optarg; 236 #else 237 errx(1, "Jail support is not compiled in"); 238 #endif 239 break; 240 case '?': 241 default: 242 usage(NULL); 243 } 244 argc -= optind; 245 argv += optind; 246 247 pid = getpid(); 248 uid = geteuid(); 249 250 #ifdef JAIL 251 if (jail_name != NULL) { 252 jid = jail_getid(jail_name); 253 if (jid == -1) 254 errx(1, "Jail not found"); 255 if (jail_attach(jid) != 0) 256 errx(1, "Cannot attach to jail"); 257 } 258 #endif 259 260 #ifdef WITHOUT_NETLINK 261 if (tflag) 262 s = open(_PATH_DEVNULL, O_WRONLY, 0); 263 else 264 s = socket(PF_ROUTE, SOCK_RAW, 0); 265 if (s < 0) 266 err(EX_OSERR, "socket"); 267 #endif 268 269 len = sizeof(numfibs); 270 if (sysctlbyname("net.fibs", (void *)&numfibs, &len, NULL, 0) == -1) 271 numfibs = -1; 272 273 len = sizeof(defaultfib); 274 if (numfibs != -1 && 275 sysctlbyname("net.my_fibnum", (void *)&defaultfib, &len, NULL, 276 0) == -1) 277 defaultfib = -1; 278 279 if (*argv != NULL) 280 switch (keyword(*argv)) { 281 case K_GET: 282 case K_SHOW: 283 uid = 0; 284 /* FALLTHROUGH */ 285 286 case K_CHANGE: 287 case K_ADD: 288 case K_DEL: 289 case K_DELETE: 290 newroute(argc, argv); 291 /* NOTREACHED */ 292 293 case K_MONITOR: 294 monitor(argc, argv); 295 /* NOTREACHED */ 296 297 case K_FLUSH: 298 flushroutes(argc, argv); 299 exit(0); 300 /* NOTREACHED */ 301 } 302 usage(*argv); 303 /* NOTREACHED */ 304 } 305 306 static int 307 set_sofib(int fib) 308 { 309 310 #ifdef WITHOUT_NETLINK 311 if (fib < 0) 312 return (0); 313 return (setsockopt(s, SOL_SOCKET, SO_SETFIB, (void *)&fib, 314 sizeof(fib))); 315 #else 316 return (0); 317 #endif 318 } 319 320 static int 321 fiboptlist_range(const char *arg, struct fibl_head_t *flh) 322 { 323 struct fibl *fl; 324 char *str0, *str, *token, *endptr; 325 int fib[2], i, error; 326 327 str0 = str = strdup(arg); 328 error = 0; 329 i = 0; 330 while ((token = strsep(&str, "-")) != NULL) { 331 switch (i) { 332 case 0: 333 case 1: 334 errno = 0; 335 fib[i] = strtol(token, &endptr, 0); 336 if (errno == 0) { 337 if (*endptr != '\0' || 338 fib[i] < 0 || 339 (numfibs != -1 && fib[i] > numfibs - 1)) 340 errno = EINVAL; 341 } 342 if (errno) 343 error = 1; 344 break; 345 default: 346 error = 1; 347 } 348 if (error) 349 goto fiboptlist_range_ret; 350 i++; 351 } 352 if (fib[0] >= fib[1]) { 353 error = 1; 354 goto fiboptlist_range_ret; 355 } 356 for (i = fib[0]; i <= fib[1]; i++) { 357 fl = calloc(1, sizeof(*fl)); 358 if (fl == NULL) { 359 error = 1; 360 goto fiboptlist_range_ret; 361 } 362 fl->fl_num = i; 363 TAILQ_INSERT_TAIL(flh, fl, fl_next); 364 } 365 fiboptlist_range_ret: 366 free(str0); 367 return (error); 368 } 369 370 #define ALLSTRLEN 64 371 static int 372 fiboptlist_csv(const char *arg, struct fibl_head_t *flh) 373 { 374 struct fibl *fl; 375 char *str0, *str, *token, *endptr; 376 int fib, error; 377 378 str0 = str = NULL; 379 if (strcmp("all", arg) == 0) { 380 str = calloc(1, ALLSTRLEN); 381 if (str == NULL) { 382 error = 1; 383 goto fiboptlist_csv_ret; 384 } 385 if (numfibs > 1) 386 snprintf(str, ALLSTRLEN - 1, "%d-%d", 0, numfibs - 1); 387 else 388 snprintf(str, ALLSTRLEN - 1, "%d", 0); 389 } else if (strcmp("default", arg) == 0) { 390 str0 = str = calloc(1, ALLSTRLEN); 391 if (str == NULL) { 392 error = 1; 393 goto fiboptlist_csv_ret; 394 } 395 snprintf(str, ALLSTRLEN - 1, "%d", defaultfib); 396 } else 397 str0 = str = strdup(arg); 398 399 error = 0; 400 while ((token = strsep(&str, ",")) != NULL) { 401 if (*token != '-' && strchr(token, '-') != NULL) { 402 error = fiboptlist_range(token, flh); 403 if (error) 404 goto fiboptlist_csv_ret; 405 } else { 406 errno = 0; 407 fib = strtol(token, &endptr, 0); 408 if (errno == 0) { 409 if (*endptr != '\0' || 410 fib < 0 || 411 (numfibs != -1 && fib > numfibs - 1)) 412 errno = EINVAL; 413 } 414 if (errno) { 415 error = 1; 416 goto fiboptlist_csv_ret; 417 } 418 fl = calloc(1, sizeof(*fl)); 419 if (fl == NULL) { 420 error = 1; 421 goto fiboptlist_csv_ret; 422 } 423 fl->fl_num = fib; 424 TAILQ_INSERT_TAIL(flh, fl, fl_next); 425 } 426 } 427 fiboptlist_csv_ret: 428 if (str0 != NULL) 429 free(str0); 430 return (error); 431 } 432 433 /* 434 * Purge all entries in the routing tables not 435 * associated with network interfaces. 436 */ 437 static void 438 flushroutes(int argc, char *argv[]) 439 { 440 struct fibl *fl; 441 int error; 442 443 if (uid != 0 && !debugonly && !tflag) 444 errx(EX_NOPERM, "must be root to alter routing table"); 445 #ifdef WITHOUT_NETLINK 446 shutdown(s, SHUT_RD); /* Don't want to read back our messages */ 447 #endif 448 449 TAILQ_INIT(&fibl_head); 450 while (argc > 1) { 451 argc--; 452 argv++; 453 if (**argv != '-') 454 usage(*argv); 455 switch (keyword(*argv + 1)) { 456 #ifdef INET 457 case K_4: 458 case K_INET: 459 af = AF_INET; 460 break; 461 #endif 462 #ifdef INET6 463 case K_6: 464 case K_INET6: 465 af = AF_INET6; 466 break; 467 #endif 468 case K_LINK: 469 af = AF_LINK; 470 break; 471 case K_FIB: 472 if (!--argc) 473 usage(*argv); 474 error = fiboptlist_csv(*++argv, &fibl_head); 475 if (error) 476 errx(EX_USAGE, "invalid fib number: %s", *argv); 477 break; 478 default: 479 usage(*argv); 480 } 481 } 482 if (TAILQ_EMPTY(&fibl_head)) { 483 error = fiboptlist_csv("default", &fibl_head); 484 if (error) 485 errx(EX_OSERR, "fiboptlist_csv failed."); 486 } 487 TAILQ_FOREACH(fl, &fibl_head, fl_next) 488 flushroutes_fib(fl->fl_num); 489 } 490 491 static int 492 flushroutes_fib(int fib) 493 { 494 #ifdef WITHOUT_NETLINK 495 return (flushroutes_fib_rtsock(fib)); 496 #else 497 return (flushroutes_fib_nl(fib, af)); 498 #endif 499 } 500 501 #ifdef WITHOUT_NETLINK 502 static int 503 flushroutes_fib_rtsock(int fib) 504 { 505 struct rt_msghdr *rtm; 506 size_t needed; 507 char *buf, *next, *lim; 508 int mib[7], rlen, seqno, count = 0; 509 int error; 510 511 error = set_sofib(fib); 512 if (error) { 513 warn("fib number %d is ignored", fib); 514 return (error); 515 } 516 517 retry: 518 mib[0] = CTL_NET; 519 mib[1] = PF_ROUTE; 520 mib[2] = 0; /* protocol */ 521 mib[3] = AF_UNSPEC; 522 mib[4] = NET_RT_DUMP; 523 mib[5] = 0; /* no flags */ 524 mib[6] = fib; 525 if (sysctl(mib, nitems(mib), NULL, &needed, NULL, 0) < 0) 526 err(EX_OSERR, "route-sysctl-estimate"); 527 if ((buf = malloc(needed)) == NULL) 528 errx(EX_OSERR, "malloc failed"); 529 if (sysctl(mib, nitems(mib), buf, &needed, NULL, 0) < 0) { 530 if (errno == ENOMEM && count++ < 10) { 531 warnx("Routing table grew, retrying"); 532 sleep(1); 533 free(buf); 534 goto retry; 535 } 536 err(EX_OSERR, "route-sysctl-get"); 537 } 538 lim = buf + needed; 539 if (verbose) 540 (void)printf("Examining routing table from sysctl\n"); 541 seqno = 0; /* ??? */ 542 for (next = buf; next < lim; next += rtm->rtm_msglen) { 543 rtm = (struct rt_msghdr *)(void *)next; 544 if (verbose) 545 print_rtmsg(rtm, rtm->rtm_msglen); 546 if ((rtm->rtm_flags & RTF_GATEWAY) == 0) 547 continue; 548 if (af != 0) { 549 struct sockaddr *sa = (struct sockaddr *)(rtm + 1); 550 551 if (sa->sa_family != af) 552 continue; 553 } 554 if (debugonly) 555 continue; 556 rtm->rtm_type = RTM_DELETE; 557 rtm->rtm_seq = seqno; 558 rlen = write(s, next, rtm->rtm_msglen); 559 if (rlen < 0 && errno == EPERM) 560 err(1, "write to routing socket"); 561 if (rlen < (int)rtm->rtm_msglen) { 562 warn("write to routing socket"); 563 (void)printf("got only %d for rlen\n", rlen); 564 free(buf); 565 goto retry; 566 break; 567 } 568 seqno++; 569 if (qflag) 570 continue; 571 if (verbose) 572 print_rtmsg(rtm, rlen); 573 else { 574 struct sockaddr *sa = (struct sockaddr *)(rtm + 1); 575 576 printf("%-20.20s ", rtm->rtm_flags & RTF_HOST ? 577 routename(sa) : netname(sa)); 578 sa = (struct sockaddr *)(SA_SIZE(sa) + (char *)sa); 579 printf("%-20.20s ", routename(sa)); 580 if (fib >= 0) 581 printf("-fib %-3d ", fib); 582 printf("done\n"); 583 } 584 } 585 free(buf); 586 return (error); 587 } 588 #endif 589 590 const char * 591 routename(struct sockaddr *sa) 592 { 593 struct sockaddr_dl *sdl; 594 const char *cp; 595 int n; 596 597 if (!domain_initialized) { 598 domain_initialized = true; 599 if (gethostname(domain, MAXHOSTNAMELEN) == 0 && 600 (cp = strchr(domain, '.'))) { 601 domain[MAXHOSTNAMELEN] = '\0'; 602 (void)strcpy(domain, cp + 1); 603 } else 604 domain[0] = '\0'; 605 } 606 607 /* If the address is zero-filled, use "default". */ 608 if (sa->sa_len == 0 && nflag == 0) 609 return ("default"); 610 #if defined(INET) || defined(INET6) 611 switch (sa->sa_family) { 612 #ifdef INET 613 case AF_INET: 614 /* If the address is zero-filled, use "default". */ 615 if (nflag == 0 && 616 ((struct sockaddr_in *)(void *)sa)->sin_addr.s_addr == 617 INADDR_ANY) 618 return("default"); 619 break; 620 #endif 621 #ifdef INET6 622 case AF_INET6: 623 /* If the address is zero-filled, use "default". */ 624 if (nflag == 0 && 625 IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *)(void *)sa)->sin6_addr)) 626 return("default"); 627 break; 628 #endif 629 } 630 #endif 631 632 switch (sa->sa_family) { 633 #if defined(INET) || defined(INET6) 634 #ifdef INET 635 case AF_INET: 636 #endif 637 #ifdef INET6 638 case AF_INET6: 639 #endif 640 { 641 struct sockaddr_storage ss; 642 int error; 643 char *p; 644 645 memset(&ss, 0, sizeof(ss)); 646 if (sa->sa_len == 0) 647 ss.ss_family = sa->sa_family; 648 else 649 memcpy(&ss, sa, sa->sa_len); 650 /* Expand sa->sa_len because it could be shortened. */ 651 if (sa->sa_family == AF_INET) 652 ss.ss_len = sizeof(struct sockaddr_in); 653 else if (sa->sa_family == AF_INET6) 654 ss.ss_len = sizeof(struct sockaddr_in6); 655 error = getnameinfo((struct sockaddr *)&ss, ss.ss_len, 656 rt_line, sizeof(rt_line), NULL, 0, 657 (nflag == 0) ? 0 : NI_NUMERICHOST); 658 if (error) { 659 warnx("getnameinfo(): %s", gai_strerror(error)); 660 strncpy(rt_line, "invalid", sizeof(rt_line)); 661 } 662 663 /* Remove the domain part if any. */ 664 p = strchr(rt_line, '.'); 665 if (p != NULL && strcmp(p + 1, domain) == 0) 666 *p = '\0'; 667 668 return (rt_line); 669 break; 670 } 671 #endif 672 case AF_LINK: 673 sdl = (struct sockaddr_dl *)(void *)sa; 674 675 if (sdl->sdl_nlen == 0 && 676 sdl->sdl_alen == 0 && 677 sdl->sdl_slen == 0) { 678 n = snprintf(rt_line, sizeof(rt_line), "link#%d", 679 sdl->sdl_index); 680 if (n > (int)sizeof(rt_line)) 681 rt_line[0] = '\0'; 682 return (rt_line); 683 } else 684 return (link_ntoa(sdl)); 685 break; 686 687 default: 688 { 689 u_short *sp = (u_short *)(void *)sa; 690 u_short *splim = sp + ((sa->sa_len + 1) >> 1); 691 char *cps = rt_line + sprintf(rt_line, "(%d)", sa->sa_family); 692 char *cpe = rt_line + sizeof(rt_line); 693 694 while (++sp < splim && cps < cpe) /* start with sa->sa_data */ 695 if ((n = snprintf(cps, cpe - cps, " %x", *sp)) > 0) 696 cps += n; 697 else 698 *cps = '\0'; 699 break; 700 } 701 } 702 return (rt_line); 703 } 704 705 /* 706 * Return the name of the network whose address is given. 707 * The address is assumed to be that of a net, not a host. 708 */ 709 const char * 710 netname(struct sockaddr *sa) 711 { 712 struct sockaddr_dl *sdl; 713 int n; 714 #ifdef INET 715 struct netent *np = NULL; 716 const char *cp = NULL; 717 u_long i; 718 #endif 719 720 switch (sa->sa_family) { 721 #ifdef INET 722 case AF_INET: 723 { 724 struct in_addr in; 725 726 in = ((struct sockaddr_in *)(void *)sa)->sin_addr; 727 i = in.s_addr = ntohl(in.s_addr); 728 if (in.s_addr == 0) 729 cp = "default"; 730 else if (!nflag) { 731 np = getnetbyaddr(i, AF_INET); 732 if (np != NULL) 733 cp = np->n_name; 734 } 735 #define C(x) (unsigned)((x) & 0xff) 736 if (cp != NULL) 737 strncpy(net_line, cp, sizeof(net_line)); 738 else if ((in.s_addr & 0xffffff) == 0) 739 (void)sprintf(net_line, "%u", C(in.s_addr >> 24)); 740 else if ((in.s_addr & 0xffff) == 0) 741 (void)sprintf(net_line, "%u.%u", C(in.s_addr >> 24), 742 C(in.s_addr >> 16)); 743 else if ((in.s_addr & 0xff) == 0) 744 (void)sprintf(net_line, "%u.%u.%u", C(in.s_addr >> 24), 745 C(in.s_addr >> 16), C(in.s_addr >> 8)); 746 else 747 (void)sprintf(net_line, "%u.%u.%u.%u", C(in.s_addr >> 24), 748 C(in.s_addr >> 16), C(in.s_addr >> 8), 749 C(in.s_addr)); 750 #undef C 751 break; 752 } 753 #endif 754 #ifdef INET6 755 case AF_INET6: 756 { 757 struct sockaddr_in6 sin6; 758 int niflags = 0; 759 760 memset(&sin6, 0, sizeof(sin6)); 761 memcpy(&sin6, sa, sa->sa_len); 762 sin6.sin6_len = sizeof(sin6); 763 sin6.sin6_family = AF_INET6; 764 if (nflag) 765 niflags |= NI_NUMERICHOST; 766 if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len, 767 net_line, sizeof(net_line), NULL, 0, niflags) != 0) 768 strncpy(net_line, "invalid", sizeof(net_line)); 769 770 return(net_line); 771 } 772 #endif 773 case AF_LINK: 774 sdl = (struct sockaddr_dl *)(void *)sa; 775 776 if (sdl->sdl_nlen == 0 && 777 sdl->sdl_alen == 0 && 778 sdl->sdl_slen == 0) { 779 n = snprintf(net_line, sizeof(net_line), "link#%d", 780 sdl->sdl_index); 781 if (n > (int)sizeof(net_line)) 782 net_line[0] = '\0'; 783 return (net_line); 784 } else 785 return (link_ntoa(sdl)); 786 break; 787 788 default: 789 { 790 u_short *sp = (u_short *)(void *)sa->sa_data; 791 u_short *splim = sp + ((sa->sa_len + 1)>>1); 792 char *cps = net_line + sprintf(net_line, "af %d:", sa->sa_family); 793 char *cpe = net_line + sizeof(net_line); 794 795 while (sp < splim && cps < cpe) 796 if ((n = snprintf(cps, cpe - cps, " %x", *sp++)) > 0) 797 cps += n; 798 else 799 *cps = '\0'; 800 break; 801 } 802 } 803 return (net_line); 804 } 805 806 static void 807 set_metric(char *value, int key) 808 { 809 int flag = 0; 810 char *endptr; 811 u_long noval, *valp = &noval; 812 813 switch (key) { 814 #define caseof(x, y, z) case x: valp = &rt_metrics.z; flag = y; break 815 caseof(K_MTU, RTV_MTU, rmx_mtu); 816 caseof(K_HOPCOUNT, RTV_HOPCOUNT, rmx_hopcount); 817 caseof(K_EXPIRE, RTV_EXPIRE, rmx_expire); 818 caseof(K_RECVPIPE, RTV_RPIPE, rmx_recvpipe); 819 caseof(K_SENDPIPE, RTV_SPIPE, rmx_sendpipe); 820 caseof(K_SSTHRESH, RTV_SSTHRESH, rmx_ssthresh); 821 caseof(K_RTT, RTV_RTT, rmx_rtt); 822 caseof(K_RTTVAR, RTV_RTTVAR, rmx_rttvar); 823 caseof(K_WEIGHT, RTV_WEIGHT, rmx_weight); 824 } 825 rtm_inits |= flag; 826 if (lockrest || locking) 827 rt_metrics.rmx_locks |= flag; 828 if (locking) 829 locking = 0; 830 errno = 0; 831 *valp = strtol(value, &endptr, 0); 832 if (errno == 0 && *endptr != '\0') 833 errno = EINVAL; 834 if (errno) 835 err(EX_USAGE, "%s", value); 836 if (flag & RTV_EXPIRE && (value[0] == '+' || value[0] == '-')) { 837 struct timespec ts; 838 839 clock_gettime(CLOCK_REALTIME_FAST, &ts); 840 *valp += ts.tv_sec; 841 } 842 } 843 844 #define F_ISHOST 0x01 845 #define F_FORCENET 0x02 846 #define F_FORCEHOST 0x04 847 #define F_PROXY 0x08 848 #define F_INTERFACE 0x10 849 850 static void 851 newroute(int argc, char **argv) 852 { 853 struct sigaction sa; 854 struct fibl *fl; 855 char *cmd; 856 const char *dest, *gateway, *errmsg; 857 int key, error, flags, nrflags, fibnum; 858 859 if (uid != 0 && !debugonly && !tflag) 860 errx(EX_NOPERM, "must be root to alter routing table"); 861 dest = NULL; 862 gateway = NULL; 863 flags = RTF_STATIC; 864 nrflags = 0; 865 TAILQ_INIT(&fibl_head); 866 867 sigemptyset(&sa.sa_mask); 868 sa.sa_flags = 0; 869 sa.sa_handler = stopit; 870 if (sigaction(SIGALRM, &sa, 0) == -1) 871 warn("sigaction SIGALRM"); 872 873 cmd = argv[0]; 874 #ifdef WITHOUT_NETLINK 875 if (*cmd != 'g' && *cmd != 's') 876 shutdown(s, SHUT_RD); /* Don't want to read back our messages */ 877 #endif 878 while (--argc > 0) { 879 if (**(++argv)== '-') { 880 switch (key = keyword(1 + *argv)) { 881 case K_LINK: 882 af = AF_LINK; 883 aflen = sizeof(struct sockaddr_dl); 884 break; 885 #ifdef INET 886 case K_4: 887 case K_INET: 888 af = AF_INET; 889 aflen = sizeof(struct sockaddr_in); 890 break; 891 #endif 892 #ifdef INET6 893 case K_6: 894 case K_INET6: 895 af = AF_INET6; 896 aflen = sizeof(struct sockaddr_in6); 897 break; 898 #endif 899 case K_SA: 900 af = PF_ROUTE; 901 aflen = sizeof(struct sockaddr_storage); 902 break; 903 case K_IFACE: 904 case K_INTERFACE: 905 nrflags |= F_INTERFACE; 906 break; 907 case K_NOSTATIC: 908 flags &= ~RTF_STATIC; 909 break; 910 case K_LOCK: 911 locking = 1; 912 break; 913 case K_LOCKREST: 914 lockrest = 1; 915 break; 916 case K_HOST: 917 nrflags |= F_FORCEHOST; 918 break; 919 case K_REJECT: 920 flags |= RTF_REJECT; 921 break; 922 case K_BLACKHOLE: 923 flags |= RTF_BLACKHOLE; 924 break; 925 case K_PROTO1: 926 flags |= RTF_PROTO1; 927 break; 928 case K_PROTO2: 929 flags |= RTF_PROTO2; 930 break; 931 case K_PROXY: 932 nrflags |= F_PROXY; 933 break; 934 case K_XRESOLVE: 935 flags |= RTF_XRESOLVE; 936 break; 937 case K_STATIC: 938 flags |= RTF_STATIC; 939 break; 940 case K_STICKY: 941 flags |= RTF_STICKY; 942 break; 943 case K_NOSTICK: 944 flags &= ~RTF_STICKY; 945 break; 946 case K_FIB: 947 if (!--argc) 948 usage(NULL); 949 error = fiboptlist_csv(*++argv, &fibl_head); 950 if (error) 951 errx(EX_USAGE, 952 "invalid fib number: %s", *argv); 953 break; 954 case K_IFA: 955 if (!--argc) 956 usage(NULL); 957 getaddr(RTAX_IFA, *++argv, nrflags); 958 break; 959 case K_IFP: 960 if (!--argc) 961 usage(NULL); 962 getaddr(RTAX_IFP, *++argv, nrflags); 963 break; 964 case K_GENMASK: 965 if (!--argc) 966 usage(NULL); 967 getaddr(RTAX_GENMASK, *++argv, nrflags); 968 break; 969 case K_GATEWAY: 970 if (!--argc) 971 usage(NULL); 972 getaddr(RTAX_GATEWAY, *++argv, nrflags); 973 gateway = *argv; 974 break; 975 case K_DST: 976 if (!--argc) 977 usage(NULL); 978 if (getaddr(RTAX_DST, *++argv, nrflags)) 979 nrflags |= F_ISHOST; 980 dest = *argv; 981 break; 982 case K_NETMASK: 983 if (!--argc) 984 usage(NULL); 985 getaddr(RTAX_NETMASK, *++argv, nrflags); 986 /* FALLTHROUGH */ 987 case K_NET: 988 nrflags |= F_FORCENET; 989 break; 990 case K_PREFIXLEN: 991 if (!--argc) 992 usage(NULL); 993 if (prefixlen(*++argv) == -1) { 994 nrflags &= ~F_FORCENET; 995 nrflags |= F_ISHOST; 996 } else { 997 nrflags |= F_FORCENET; 998 nrflags &= ~F_ISHOST; 999 } 1000 break; 1001 case K_MTU: 1002 case K_HOPCOUNT: 1003 case K_EXPIRE: 1004 case K_RECVPIPE: 1005 case K_SENDPIPE: 1006 case K_SSTHRESH: 1007 case K_RTT: 1008 case K_RTTVAR: 1009 case K_WEIGHT: 1010 if (!--argc) 1011 usage(NULL); 1012 set_metric(*++argv, key); 1013 break; 1014 default: 1015 usage(1+*argv); 1016 } 1017 } else { 1018 if ((rtm_addrs & RTA_DST) == 0) { 1019 dest = *argv; 1020 if (getaddr(RTAX_DST, *argv, nrflags)) 1021 nrflags |= F_ISHOST; 1022 } else if ((rtm_addrs & RTA_GATEWAY) == 0) { 1023 gateway = *argv; 1024 getaddr(RTAX_GATEWAY, *argv, nrflags); 1025 } else { 1026 getaddr(RTAX_NETMASK, *argv, nrflags); 1027 nrflags |= F_FORCENET; 1028 } 1029 } 1030 } 1031 1032 /* Do some sanity checks on resulting request */ 1033 if (so[RTAX_DST].ss_len == 0) { 1034 warnx("destination parameter required"); 1035 usage(NULL); 1036 } 1037 1038 if (so[RTAX_NETMASK].ss_len != 0 && 1039 so[RTAX_DST].ss_family != so[RTAX_NETMASK].ss_family) { 1040 warnx("destination and netmask family need to be the same"); 1041 usage(NULL); 1042 } 1043 1044 if (nrflags & F_FORCEHOST) { 1045 nrflags |= F_ISHOST; 1046 #ifdef INET6 1047 if (af == AF_INET6) { 1048 rtm_addrs &= ~RTA_NETMASK; 1049 memset(&so[RTAX_NETMASK], 0, sizeof(so[RTAX_NETMASK])); 1050 } 1051 #endif 1052 } 1053 if (nrflags & F_FORCENET) 1054 nrflags &= ~F_ISHOST; 1055 flags |= RTF_UP; 1056 if (nrflags & F_ISHOST) 1057 flags |= RTF_HOST; 1058 if ((nrflags & F_INTERFACE) == 0) 1059 flags |= RTF_GATEWAY; 1060 if (nrflags & F_PROXY) 1061 flags |= RTF_ANNOUNCE; 1062 if (dest == NULL) 1063 dest = ""; 1064 if (gateway == NULL) 1065 gateway = ""; 1066 1067 if (TAILQ_EMPTY(&fibl_head)) { 1068 error = fiboptlist_csv("default", &fibl_head); 1069 if (error) 1070 errx(EX_OSERR, "fiboptlist_csv failed."); 1071 } 1072 error = 0; 1073 TAILQ_FOREACH(fl, &fibl_head, fl_next) { 1074 fl->fl_error = newroute_fib(fl->fl_num, cmd, flags); 1075 if (fl->fl_error) 1076 fl->fl_errno = errno; 1077 error += fl->fl_error; 1078 } 1079 if (*cmd == 'g' || *cmd == 's') 1080 exit(error); 1081 1082 error = 0; 1083 if (!qflag) { 1084 fibnum = 0; 1085 TAILQ_FOREACH(fl, &fibl_head, fl_next) { 1086 if (fl->fl_error == 0) 1087 fibnum++; 1088 } 1089 if (fibnum > 0) { 1090 int firstfib = 1; 1091 1092 printf("%s %s %s", cmd, 1093 (nrflags & F_ISHOST) ? "host" : "net", dest); 1094 if (*gateway) 1095 printf(": gateway %s", gateway); 1096 1097 if (numfibs > 1) { 1098 TAILQ_FOREACH(fl, &fibl_head, fl_next) { 1099 if (fl->fl_error == 0 1100 && fl->fl_num >= 0) { 1101 if (firstfib) { 1102 printf(" fib "); 1103 firstfib = 0; 1104 } 1105 printf("%d", fl->fl_num); 1106 if (fibnum-- > 1) 1107 printf(","); 1108 } 1109 } 1110 } 1111 printf("\n"); 1112 } 1113 } 1114 1115 fibnum = 0; 1116 TAILQ_FOREACH(fl, &fibl_head, fl_next) { 1117 if (fl->fl_error != 0) { 1118 error = 1; 1119 if (!qflag) { 1120 printf("%s %s %s", cmd, (nrflags & F_ISHOST) 1121 ? "host" : "net", dest); 1122 if (*gateway) 1123 printf(": gateway %s", gateway); 1124 1125 if (fl->fl_num >= 0) 1126 printf(" fib %d", fl->fl_num); 1127 1128 switch (fl->fl_errno) { 1129 case ESRCH: 1130 errmsg = "not in table"; 1131 break; 1132 case EBUSY: 1133 errmsg = "entry in use"; 1134 break; 1135 case ENOBUFS: 1136 errmsg = "not enough memory"; 1137 break; 1138 case EADDRINUSE: 1139 /* 1140 * handle recursion avoidance 1141 * in rt_setgate() 1142 */ 1143 errmsg = "gateway uses the same route"; 1144 break; 1145 case EEXIST: 1146 errmsg = "route already in table"; 1147 break; 1148 default: 1149 errmsg = strerror(fl->fl_errno); 1150 break; 1151 } 1152 printf(": %s\n", errmsg); 1153 } 1154 } 1155 } 1156 exit(error); 1157 } 1158 1159 static int 1160 newroute_fib(int fib, char *cmd, int flags) 1161 { 1162 int error; 1163 1164 error = set_sofib(fib); 1165 if (error) { 1166 warn("fib number %d is ignored", fib); 1167 return (error); 1168 } 1169 1170 error = rtmsg(*cmd, flags, fib); 1171 return (error); 1172 } 1173 1174 #ifdef INET 1175 static void 1176 inet_makemask(struct sockaddr_in *sin_mask, u_long bits) 1177 { 1178 u_long mask = 0; 1179 1180 rtm_addrs |= RTA_NETMASK; 1181 1182 if (bits != 0) 1183 mask = 0xffffffff << (32 - bits); 1184 1185 sin_mask->sin_addr.s_addr = htonl(mask); 1186 sin_mask->sin_len = sizeof(struct sockaddr_in); 1187 sin_mask->sin_family = AF_INET; 1188 } 1189 #endif 1190 1191 #ifdef INET6 1192 /* 1193 * XXX the function may need more improvement... 1194 */ 1195 static int 1196 inet6_makenetandmask(struct sockaddr_in6 *sin6, const char *plen) 1197 { 1198 1199 if (plen == NULL) { 1200 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) && 1201 sin6->sin6_scope_id == 0) 1202 plen = "0"; 1203 } 1204 1205 if (plen == NULL || strcmp(plen, "128") == 0) 1206 return (1); 1207 rtm_addrs |= RTA_NETMASK; 1208 prefixlen(plen); 1209 return (0); 1210 } 1211 #endif 1212 1213 /* 1214 * Interpret an argument as a network address of some kind, 1215 * returning 1 if a host address, 0 if a network address. 1216 */ 1217 static int 1218 getaddr(int idx, char *str, int nrflags) 1219 { 1220 struct sockaddr *sa; 1221 #if defined(INET) 1222 struct sockaddr_in *sin; 1223 struct hostent *hp; 1224 char *q; 1225 #elif defined(INET6) 1226 char *q; 1227 #endif 1228 1229 if (idx < 0 || idx >= RTAX_MAX) 1230 usage("internal error"); 1231 if (af == 0) { 1232 #if defined(INET) 1233 af = AF_INET; 1234 aflen = sizeof(struct sockaddr_in); 1235 #elif defined(INET6) 1236 af = AF_INET6; 1237 aflen = sizeof(struct sockaddr_in6); 1238 #else 1239 af = AF_LINK; 1240 aflen = sizeof(struct sockaddr_dl); 1241 #endif 1242 } 1243 rtm_addrs |= (1 << idx); 1244 sa = (struct sockaddr *)&so[idx]; 1245 sa->sa_family = af; 1246 sa->sa_len = aflen; 1247 1248 switch (idx) { 1249 case RTAX_GATEWAY: 1250 if (nrflags & F_INTERFACE) { 1251 struct ifaddrs *ifap, *ifa; 1252 struct sockaddr_dl *sdl0 = (struct sockaddr_dl *)(void *)sa; 1253 struct sockaddr_dl *sdl = NULL; 1254 1255 if (getifaddrs(&ifap)) 1256 err(EX_OSERR, "getifaddrs"); 1257 1258 for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) { 1259 if (ifa->ifa_addr->sa_family != AF_LINK) 1260 continue; 1261 1262 if (strcmp(str, ifa->ifa_name) != 0) 1263 continue; 1264 1265 sdl = (struct sockaddr_dl *)(void *)ifa->ifa_addr; 1266 } 1267 /* If we found it, then use it */ 1268 if (sdl != NULL) { 1269 /* 1270 * Note that we need to copy before calling 1271 * freeifaddrs(). 1272 */ 1273 memcpy(sdl0, sdl, sdl->sdl_len); 1274 } 1275 freeifaddrs(ifap); 1276 if (sdl != NULL) 1277 return(1); 1278 else 1279 errx(EX_DATAERR, 1280 "interface '%s' does not exist", str); 1281 } 1282 break; 1283 case RTAX_IFP: 1284 sa->sa_family = AF_LINK; 1285 break; 1286 } 1287 if (strcmp(str, "default") == 0) { 1288 /* 1289 * Default is net 0.0.0.0/0 1290 */ 1291 switch (idx) { 1292 case RTAX_DST: 1293 nrflags |= F_FORCENET; 1294 getaddr(RTAX_NETMASK, str, nrflags); 1295 break; 1296 } 1297 return (0); 1298 } 1299 switch (sa->sa_family) { 1300 #ifdef INET6 1301 case AF_INET6: 1302 { 1303 struct addrinfo hints, *res; 1304 int ecode; 1305 1306 q = NULL; 1307 if (idx == RTAX_DST && (q = strchr(str, '/')) != NULL) 1308 *q = '\0'; 1309 memset(&hints, 0, sizeof(hints)); 1310 hints.ai_family = sa->sa_family; 1311 hints.ai_socktype = SOCK_DGRAM; 1312 ecode = getaddrinfo(str, NULL, &hints, &res); 1313 if (ecode != 0 || res->ai_family != AF_INET6 || 1314 res->ai_addrlen != sizeof(struct sockaddr_in6)) 1315 errx(EX_OSERR, "%s: %s", str, gai_strerror(ecode)); 1316 memcpy(sa, res->ai_addr, res->ai_addrlen); 1317 freeaddrinfo(res); 1318 if (q != NULL) 1319 *q++ = '/'; 1320 if (idx == RTAX_DST) 1321 return (inet6_makenetandmask((struct sockaddr_in6 *)(void *)sa, q)); 1322 return (0); 1323 } 1324 #endif /* INET6 */ 1325 case AF_LINK: 1326 link_addr(str, (struct sockaddr_dl *)(void *)sa); 1327 return (1); 1328 1329 case PF_ROUTE: 1330 sockaddr(str, sa, sizeof(struct sockaddr_storage)); 1331 return (1); 1332 #ifdef INET 1333 case AF_INET: 1334 #endif 1335 default: 1336 break; 1337 } 1338 1339 #ifdef INET 1340 sin = (struct sockaddr_in *)(void *)sa; 1341 1342 q = strchr(str,'/'); 1343 if (q != NULL && idx == RTAX_DST) { 1344 /* A.B.C.D/NUM */ 1345 *q = '\0'; 1346 if (inet_aton(str, &sin->sin_addr) == 0) 1347 errx(EX_NOHOST, "bad address: %s", str); 1348 1349 int masklen = strtol(q + 1, NULL, 10); 1350 if (masklen < 0 || masklen > 32) 1351 errx(EX_NOHOST, "bad mask length: %s", q + 1); 1352 1353 inet_makemask((struct sockaddr_in *)&so[RTAX_NETMASK],masklen); 1354 return (0); 1355 } 1356 if (inet_aton(str, &sin->sin_addr) != 0) 1357 return (1); 1358 1359 hp = gethostbyname(str); 1360 if (hp != NULL) { 1361 sin->sin_family = hp->h_addrtype; 1362 memmove((char *)&sin->sin_addr, hp->h_addr, 1363 MIN((size_t)hp->h_length, sizeof(sin->sin_addr))); 1364 return (1); 1365 } 1366 #endif 1367 errx(EX_NOHOST, "bad address: %s", str); 1368 } 1369 1370 static int 1371 prefixlen(const char *str) 1372 { 1373 int len = atoi(str), q, r; 1374 int max; 1375 char *p; 1376 1377 rtm_addrs |= RTA_NETMASK; 1378 switch (af) { 1379 #ifdef INET6 1380 case AF_INET6: 1381 { 1382 struct sockaddr_in6 *sin6 = 1383 (struct sockaddr_in6 *)&so[RTAX_NETMASK]; 1384 1385 max = 128; 1386 p = (char *)&sin6->sin6_addr; 1387 sin6->sin6_family = AF_INET6; 1388 sin6->sin6_len = sizeof(*sin6); 1389 break; 1390 } 1391 #endif 1392 #ifdef INET 1393 case AF_INET: 1394 { 1395 struct sockaddr_in *sin = 1396 (struct sockaddr_in *)&so[RTAX_NETMASK]; 1397 1398 max = 32; 1399 p = (char *)&sin->sin_addr; 1400 sin->sin_family = AF_INET; 1401 sin->sin_len = sizeof(*sin); 1402 break; 1403 } 1404 #endif 1405 default: 1406 errx(EX_OSERR, "prefixlen not supported in this af"); 1407 } 1408 1409 if (len < 0 || max < len) 1410 errx(EX_USAGE, "%s: invalid prefixlen", str); 1411 1412 q = len >> 3; 1413 r = len & 7; 1414 memset((void *)p, 0, max / 8); 1415 if (q > 0) 1416 memset((void *)p, 0xff, q); 1417 if (r > 0) 1418 *((u_char *)p + q) = (0xff00 >> r) & 0xff; 1419 if (len == max) 1420 return (-1); 1421 else 1422 return (len); 1423 } 1424 1425 static void 1426 interfaces(void) 1427 { 1428 size_t needed; 1429 int mib[6]; 1430 char *buf, *lim, *next, count = 0; 1431 struct rt_msghdr *rtm; 1432 1433 retry2: 1434 mib[0] = CTL_NET; 1435 mib[1] = PF_ROUTE; 1436 mib[2] = 0; /* protocol */ 1437 mib[3] = AF_UNSPEC; 1438 mib[4] = NET_RT_IFLIST; 1439 mib[5] = 0; /* no flags */ 1440 if (sysctl(mib, nitems(mib), NULL, &needed, NULL, 0) < 0) 1441 err(EX_OSERR, "route-sysctl-estimate"); 1442 if ((buf = malloc(needed)) == NULL) 1443 errx(EX_OSERR, "malloc failed"); 1444 if (sysctl(mib, nitems(mib), buf, &needed, NULL, 0) < 0) { 1445 if (errno == ENOMEM && count++ < 10) { 1446 warnx("Routing table grew, retrying"); 1447 sleep(1); 1448 free(buf); 1449 goto retry2; 1450 } 1451 err(EX_OSERR, "actual retrieval of interface table"); 1452 } 1453 lim = buf + needed; 1454 for (next = buf; next < lim; next += rtm->rtm_msglen) { 1455 rtm = (struct rt_msghdr *)(void *)next; 1456 print_rtmsg(rtm, rtm->rtm_msglen); 1457 } 1458 free(buf); 1459 } 1460 1461 static void 1462 monitor(int argc, char *argv[]) 1463 { 1464 int fib, error; 1465 char *endptr; 1466 1467 fib = defaultfib; 1468 while (argc > 1) { 1469 argc--; 1470 argv++; 1471 if (**argv != '-') 1472 usage(*argv); 1473 switch (keyword(*argv + 1)) { 1474 case K_FIB: 1475 if (!--argc) 1476 usage(*argv); 1477 errno = 0; 1478 fib = strtol(*++argv, &endptr, 0); 1479 if (errno == 0) { 1480 if (*endptr != '\0' || 1481 fib < 0 || 1482 (numfibs != -1 && fib > numfibs - 1)) 1483 errno = EINVAL; 1484 } 1485 if (errno) 1486 errx(EX_USAGE, "invalid fib number: %s", *argv); 1487 break; 1488 default: 1489 usage(*argv); 1490 } 1491 } 1492 error = set_sofib(fib); 1493 if (error) 1494 errx(EX_USAGE, "invalid fib number: %d", fib); 1495 1496 verbose = 1; 1497 if (debugonly) { 1498 interfaces(); 1499 exit(0); 1500 } 1501 #ifdef WITHOUT_NETLINK 1502 monitor_rtsock(); 1503 #else 1504 monitor_nl(fib); 1505 #endif 1506 } 1507 1508 #ifdef WITHOUT_NETLINK 1509 static void 1510 monitor_rtsock(void) 1511 { 1512 char msg[2048]; 1513 int n; 1514 1515 #ifdef SO_RERROR 1516 n = 1; 1517 if (setsockopt(s, SOL_SOCKET, SO_RERROR, &n, sizeof(n)) == -1) 1518 warn("SO_RERROR"); 1519 #endif 1520 1521 for (;;) { 1522 time_t now; 1523 n = read(s, msg, sizeof(msg)); 1524 if (n == -1) { 1525 warn("read"); 1526 continue; 1527 } 1528 now = time(NULL); 1529 (void)printf("\ngot message of size %d on %s", n, ctime(&now)); 1530 print_rtmsg((struct rt_msghdr *)(void *)msg, n); 1531 } 1532 } 1533 #endif 1534 1535 static int 1536 rtmsg(int cmd, int flags, int fib) 1537 { 1538 errno = 0; 1539 if (cmd == 'a') 1540 cmd = RTM_ADD; 1541 else if (cmd == 'c') 1542 cmd = RTM_CHANGE; 1543 else if (cmd == 'g' || cmd == 's') { 1544 cmd = RTM_GET; 1545 if (so[RTAX_IFP].ss_family == 0) { 1546 so[RTAX_IFP].ss_family = AF_LINK; 1547 so[RTAX_IFP].ss_len = sizeof(struct sockaddr_dl); 1548 rtm_addrs |= RTA_IFP; 1549 } 1550 } else { 1551 cmd = RTM_DELETE; 1552 flags |= RTF_PINNED; 1553 } 1554 #ifdef WITHOUT_NETLINK 1555 return (rtmsg_rtsock(cmd, flags, fib)); 1556 #else 1557 errno = rtmsg_nl(cmd, flags, fib, rtm_addrs, so, &rt_metrics); 1558 return (errno == 0 ? 0 : -1); 1559 #endif 1560 } 1561 1562 #ifdef WITHOUT_NETLINK 1563 static int 1564 rtmsg_rtsock(int cmd, int flags, int fib) 1565 { 1566 int rlen; 1567 char *cp = m_rtmsg.m_space; 1568 int l; 1569 1570 memset(&m_rtmsg, 0, sizeof(m_rtmsg)); 1571 1572 #define NEXTADDR(w, u) \ 1573 if (rtm_addrs & (w)) { \ 1574 l = SA_SIZE(&(u)); \ 1575 memmove(cp, (char *)&(u), l); \ 1576 cp += l; \ 1577 if (verbose) \ 1578 sodump((struct sockaddr *)&(u), #w); \ 1579 } 1580 1581 #define rtm m_rtmsg.m_rtm 1582 rtm.rtm_type = cmd; 1583 rtm.rtm_flags = flags; 1584 rtm.rtm_version = RTM_VERSION; 1585 rtm.rtm_seq = ++rtm_seq; 1586 rtm.rtm_addrs = rtm_addrs; 1587 rtm.rtm_rmx = rt_metrics; 1588 rtm.rtm_inits = rtm_inits; 1589 1590 NEXTADDR(RTA_DST, so[RTAX_DST]); 1591 NEXTADDR(RTA_GATEWAY, so[RTAX_GATEWAY]); 1592 NEXTADDR(RTA_NETMASK, so[RTAX_NETMASK]); 1593 NEXTADDR(RTA_GENMASK, so[RTAX_GENMASK]); 1594 NEXTADDR(RTA_IFP, so[RTAX_IFP]); 1595 NEXTADDR(RTA_IFA, so[RTAX_IFA]); 1596 rtm.rtm_msglen = l = cp - (char *)&m_rtmsg; 1597 if (verbose) 1598 print_rtmsg(&rtm, l); 1599 if (debugonly) 1600 return (0); 1601 if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) { 1602 switch (errno) { 1603 case EPERM: 1604 err(1, "writing to routing socket"); 1605 break; 1606 case ESRCH: 1607 warnx("route has not been found"); 1608 break; 1609 case EEXIST: 1610 /* Handled by newroute() */ 1611 break; 1612 default: 1613 warn("writing to routing socket"); 1614 } 1615 return (-1); 1616 } 1617 if (cmd == RTM_GET) { 1618 stop_read = 0; 1619 alarm(READ_TIMEOUT); 1620 do { 1621 l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg)); 1622 } while (l > 0 && stop_read == 0 && 1623 (rtm.rtm_type != RTM_GET || rtm.rtm_seq != rtm_seq || 1624 rtm.rtm_pid != pid)); 1625 if (stop_read != 0) { 1626 warnx("read from routing socket timed out"); 1627 return (-1); 1628 } else 1629 alarm(0); 1630 if (l < 0) 1631 warn("read from routing socket"); 1632 else 1633 print_getmsg(&rtm, l, fib); 1634 } 1635 #undef rtm 1636 return (0); 1637 } 1638 #endif 1639 1640 static const char *const msgtypes[] = { 1641 "", 1642 "RTM_ADD: Add Route", 1643 "RTM_DELETE: Delete Route", 1644 "RTM_CHANGE: Change Metrics or flags", 1645 "RTM_GET: Report Metrics", 1646 "RTM_LOSING: Kernel Suspects Partitioning", 1647 "RTM_REDIRECT: Told to use different route", 1648 "RTM_MISS: Lookup failed on this address", 1649 "RTM_LOCK: fix specified metrics", 1650 "RTM_OLDADD: caused by SIOCADDRT", 1651 "RTM_OLDDEL: caused by SIOCDELRT", 1652 "RTM_RESOLVE: Route created by cloning", 1653 "RTM_NEWADDR: address being added to iface", 1654 "RTM_DELADDR: address being removed from iface", 1655 "RTM_IFINFO: iface status change", 1656 "RTM_NEWMADDR: new multicast group membership on iface", 1657 "RTM_DELMADDR: multicast group membership removed from iface", 1658 "RTM_IFANNOUNCE: interface arrival/departure", 1659 "RTM_IEEE80211: IEEE 802.11 wireless event", 1660 }; 1661 1662 static const char metricnames[] = 1663 "\011weight\010rttvar\7rtt\6ssthresh\5sendpipe\4recvpipe\3expire" 1664 "\1mtu"; 1665 const char routeflags[] = 1666 "\1UP\2GATEWAY\3HOST\4REJECT\5DYNAMIC\6MODIFIED\7DONE" 1667 "\012XRESOLVE\013LLINFO\014STATIC\015BLACKHOLE" 1668 "\017PROTO2\020PROTO1\021PRCLONING\022WASCLONED\023PROTO3" 1669 "\024FIXEDMTU\025PINNED\026LOCAL\027BROADCAST\030MULTICAST\035STICKY"; 1670 static const char ifnetflags[] = 1671 "\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5PTP\6b6\7RUNNING\010NOARP" 1672 "\011PPROMISC\012ALLMULTI\013OACTIVE\014SIMPLEX\015LINK0\016LINK1" 1673 "\017LINK2\020MULTICAST"; 1674 static const char addrnames[] = 1675 "\1DST\2GATEWAY\3NETMASK\4GENMASK\5IFP\6IFA\7AUTHOR\010BRD"; 1676 1677 static const char errfmt[] = 1678 "\n%s: truncated route message, only %zu bytes left\n"; 1679 1680 static void 1681 print_rtmsg(struct rt_msghdr *rtm, size_t msglen) 1682 { 1683 struct if_msghdr *ifm; 1684 struct ifa_msghdr *ifam; 1685 #ifdef RTM_NEWMADDR 1686 struct ifma_msghdr *ifmam; 1687 #endif 1688 struct if_announcemsghdr *ifan; 1689 const char *state; 1690 1691 if (verbose == 0) 1692 return; 1693 if (rtm->rtm_version != RTM_VERSION) { 1694 (void)printf("routing message version %d not understood\n", 1695 rtm->rtm_version); 1696 return; 1697 } 1698 if (rtm->rtm_type < nitems(msgtypes)) 1699 (void)printf("%s: ", msgtypes[rtm->rtm_type]); 1700 else 1701 (void)printf("unknown type %d: ", rtm->rtm_type); 1702 (void)printf("len %d, ", rtm->rtm_msglen); 1703 1704 #define REQUIRE(x) do { \ 1705 if (msglen < sizeof(x)) \ 1706 goto badlen; \ 1707 else \ 1708 msglen -= sizeof(x); \ 1709 } while (0) 1710 1711 switch (rtm->rtm_type) { 1712 case RTM_IFINFO: 1713 REQUIRE(struct if_msghdr); 1714 ifm = (struct if_msghdr *)rtm; 1715 (void)printf("if# %d, ", ifm->ifm_index); 1716 switch (ifm->ifm_data.ifi_link_state) { 1717 case LINK_STATE_DOWN: 1718 state = "down"; 1719 break; 1720 case LINK_STATE_UP: 1721 state = "up"; 1722 break; 1723 default: 1724 state = "unknown"; 1725 break; 1726 } 1727 (void)printf("link: %s, flags:", state); 1728 printb(ifm->ifm_flags, ifnetflags); 1729 pmsg_addrs((char *)(ifm + 1), ifm->ifm_addrs, msglen); 1730 break; 1731 case RTM_NEWADDR: 1732 case RTM_DELADDR: 1733 REQUIRE(struct ifa_msghdr); 1734 ifam = (struct ifa_msghdr *)rtm; 1735 (void)printf("metric %d, flags:", ifam->ifam_metric); 1736 printb(ifam->ifam_flags, routeflags); 1737 pmsg_addrs((char *)(ifam + 1), ifam->ifam_addrs, msglen); 1738 break; 1739 #ifdef RTM_NEWMADDR 1740 case RTM_NEWMADDR: 1741 case RTM_DELMADDR: 1742 REQUIRE(struct ifma_msghdr); 1743 ifmam = (struct ifma_msghdr *)rtm; 1744 pmsg_addrs((char *)(ifmam + 1), ifmam->ifmam_addrs, msglen); 1745 break; 1746 #endif 1747 case RTM_IFANNOUNCE: 1748 REQUIRE(struct if_announcemsghdr); 1749 ifan = (struct if_announcemsghdr *)rtm; 1750 (void)printf("if# %d, what: ", ifan->ifan_index); 1751 switch (ifan->ifan_what) { 1752 case IFAN_ARRIVAL: 1753 (void)printf("arrival"); 1754 break; 1755 case IFAN_DEPARTURE: 1756 printf("departure"); 1757 break; 1758 default: 1759 printf("#%d", ifan->ifan_what); 1760 break; 1761 } 1762 printf("\n"); 1763 fflush(stdout); 1764 break; 1765 1766 default: 1767 if (rtm->rtm_type <= RTM_RESOLVE) { 1768 printf("pid: %ld, seq %d, errno %d, flags:", 1769 (long)rtm->rtm_pid, rtm->rtm_seq, rtm->rtm_errno); 1770 printb(rtm->rtm_flags, routeflags); 1771 pmsg_common(rtm, msglen); 1772 } else 1773 printf("type: %u, len: %zu\n", rtm->rtm_type, msglen); 1774 } 1775 1776 return; 1777 1778 badlen: 1779 (void)printf(errfmt, __func__, msglen); 1780 #undef REQUIRE 1781 } 1782 1783 static void 1784 print_getmsg(struct rt_msghdr *rtm, int msglen, int fib) 1785 { 1786 struct sockaddr *sp[RTAX_MAX]; 1787 struct timespec ts; 1788 char *cp; 1789 int i; 1790 1791 memset(sp, 0, sizeof(sp)); 1792 (void)printf(" route to: %s\n", 1793 routename((struct sockaddr *)&so[RTAX_DST])); 1794 if (rtm->rtm_version != RTM_VERSION) { 1795 warnx("routing message version %d not understood", 1796 rtm->rtm_version); 1797 return; 1798 } 1799 if (rtm->rtm_msglen > msglen) { 1800 warnx("message length mismatch, in packet %d, returned %d", 1801 rtm->rtm_msglen, msglen); 1802 return; 1803 } 1804 if (rtm->rtm_errno) { 1805 errno = rtm->rtm_errno; 1806 warn("message indicates error %d", errno); 1807 return; 1808 } 1809 cp = ((char *)(rtm + 1)); 1810 for (i = 0; i < RTAX_MAX; i++) 1811 if (rtm->rtm_addrs & (1 << i)) { 1812 sp[i] = (struct sockaddr *)cp; 1813 cp += SA_SIZE((struct sockaddr *)cp); 1814 } 1815 if ((rtm->rtm_addrs & RTA_IFP) && 1816 (sp[RTAX_IFP]->sa_family != AF_LINK || 1817 ((struct sockaddr_dl *)(void *)sp[RTAX_IFP])->sdl_nlen == 0)) 1818 sp[RTAX_IFP] = NULL; 1819 if (sp[RTAX_DST]) 1820 (void)printf("destination: %s\n", routename(sp[RTAX_DST])); 1821 if (sp[RTAX_NETMASK]) 1822 (void)printf(" mask: %s\n", routename(sp[RTAX_NETMASK])); 1823 if (sp[RTAX_GATEWAY] && (rtm->rtm_flags & RTF_GATEWAY)) 1824 (void)printf(" gateway: %s\n", routename(sp[RTAX_GATEWAY])); 1825 if (fib >= 0) 1826 (void)printf(" fib: %u\n", (unsigned int)fib); 1827 if (sp[RTAX_IFP]) 1828 (void)printf(" interface: %.*s\n", 1829 ((struct sockaddr_dl *)(void *)sp[RTAX_IFP])->sdl_nlen, 1830 ((struct sockaddr_dl *)(void *)sp[RTAX_IFP])->sdl_data); 1831 (void)printf(" flags: "); 1832 printb(rtm->rtm_flags, routeflags); 1833 1834 #define lock(f) ((rtm->rtm_rmx.rmx_locks & __CONCAT(RTV_,f)) ? 'L' : ' ') 1835 #define msec(u) (((u) + 500) / 1000) /* usec to msec */ 1836 printf("\n%9s %9s %9s %9s %9s %10s %9s\n", "recvpipe", 1837 "sendpipe", "ssthresh", "rtt,msec", "mtu ", "weight", "expire"); 1838 printf("%8lu%c ", rtm->rtm_rmx.rmx_recvpipe, lock(RPIPE)); 1839 printf("%8lu%c ", rtm->rtm_rmx.rmx_sendpipe, lock(SPIPE)); 1840 printf("%8lu%c ", rtm->rtm_rmx.rmx_ssthresh, lock(SSTHRESH)); 1841 printf("%8lu%c ", msec(rtm->rtm_rmx.rmx_rtt), lock(RTT)); 1842 printf("%8lu%c ", rtm->rtm_rmx.rmx_mtu, lock(MTU)); 1843 printf("%8lu%c ", rtm->rtm_rmx.rmx_weight, lock(WEIGHT)); 1844 if (rtm->rtm_rmx.rmx_expire > 0) 1845 clock_gettime(CLOCK_REALTIME_FAST, &ts); 1846 else 1847 ts.tv_sec = 0; 1848 printf("%8ld%c\n", (long)(rtm->rtm_rmx.rmx_expire - ts.tv_sec), 1849 lock(EXPIRE)); 1850 #undef lock 1851 #undef msec 1852 #define RTA_IGN (RTA_DST|RTA_GATEWAY|RTA_NETMASK|RTA_IFP|RTA_IFA|RTA_BRD) 1853 if (verbose) 1854 pmsg_common(rtm, msglen); 1855 else if (rtm->rtm_addrs &~ RTA_IGN) { 1856 (void)printf("sockaddrs: "); 1857 printb(rtm->rtm_addrs, addrnames); 1858 putchar('\n'); 1859 } 1860 #undef RTA_IGN 1861 } 1862 1863 static void 1864 pmsg_common(struct rt_msghdr *rtm, size_t msglen) 1865 { 1866 1867 (void)printf("\nlocks: "); 1868 printb(rtm->rtm_rmx.rmx_locks, metricnames); 1869 (void)printf(" inits: "); 1870 printb(rtm->rtm_inits, metricnames); 1871 if (msglen > sizeof(struct rt_msghdr)) 1872 pmsg_addrs(((char *)(rtm + 1)), rtm->rtm_addrs, 1873 msglen - sizeof(struct rt_msghdr)); 1874 else 1875 (void)fflush(stdout); 1876 } 1877 1878 static void 1879 pmsg_addrs(char *cp, int addrs, size_t len) 1880 { 1881 struct sockaddr *sa; 1882 int i; 1883 1884 if (addrs == 0) { 1885 (void)putchar('\n'); 1886 return; 1887 } 1888 (void)printf("\nsockaddrs: "); 1889 printb(addrs, addrnames); 1890 putchar('\n'); 1891 for (i = 0; i < RTAX_MAX; i++) 1892 if (addrs & (1 << i)) { 1893 sa = (struct sockaddr *)cp; 1894 if (len == 0 || len < SA_SIZE(sa)) { 1895 (void)printf(errfmt, __func__, len); 1896 break; 1897 } 1898 (void)printf(" %s", routename(sa)); 1899 len -= SA_SIZE(sa); 1900 cp += SA_SIZE(sa); 1901 } 1902 (void)putchar('\n'); 1903 (void)fflush(stdout); 1904 } 1905 1906 void 1907 printb(int b, const char *str) 1908 { 1909 int i; 1910 int gotsome = 0; 1911 1912 if (b == 0) 1913 return; 1914 while ((i = *str++) != 0) { 1915 if (b & (1 << (i-1))) { 1916 if (gotsome == 0) 1917 i = '<'; 1918 else 1919 i = ','; 1920 putchar(i); 1921 gotsome = 1; 1922 for (; (i = *str) > 32; str++) 1923 putchar(i); 1924 } else 1925 while (*str > 32) 1926 str++; 1927 } 1928 if (gotsome) 1929 putchar('>'); 1930 } 1931 1932 int 1933 keyword(const char *cp) 1934 { 1935 const struct keytab *kt = keywords; 1936 1937 while (kt->kt_cp != NULL && strcmp(kt->kt_cp, cp) != 0) 1938 kt++; 1939 return (kt->kt_i); 1940 } 1941 1942 static void 1943 sodump(struct sockaddr *sa, const char *which) 1944 { 1945 #ifdef INET6 1946 char nbuf[INET6_ADDRSTRLEN]; 1947 #endif 1948 1949 switch (sa->sa_family) { 1950 case AF_LINK: 1951 (void)printf("%s: link %s; ", which, 1952 link_ntoa((struct sockaddr_dl *)(void *)sa)); 1953 break; 1954 #ifdef INET 1955 case AF_INET: 1956 (void)printf("%s: inet %s; ", which, 1957 inet_ntoa(((struct sockaddr_in *)(void *)sa)->sin_addr)); 1958 break; 1959 #endif 1960 #ifdef INET6 1961 case AF_INET6: 1962 (void)printf("%s: inet6 %s; ", which, inet_ntop(sa->sa_family, 1963 &((struct sockaddr_in6 *)(void *)sa)->sin6_addr, nbuf, 1964 sizeof(nbuf))); 1965 break; 1966 #endif 1967 } 1968 (void)fflush(stdout); 1969 } 1970 1971 /* States*/ 1972 #define VIRGIN 0 1973 #define GOTONE 1 1974 #define GOTTWO 2 1975 /* Inputs */ 1976 #define DIGIT (4*0) 1977 #define END (4*1) 1978 #define DELIM (4*2) 1979 1980 static void 1981 sockaddr(char *addr, struct sockaddr *sa, size_t size) 1982 { 1983 char *cp = (char *)sa; 1984 char *cplim = cp + size; 1985 int byte = 0, state = VIRGIN, new = 0 /* foil gcc */; 1986 1987 memset(cp, 0, size); 1988 cp++; 1989 do { 1990 if ((*addr >= '0') && (*addr <= '9')) { 1991 new = *addr - '0'; 1992 } else if ((*addr >= 'a') && (*addr <= 'f')) { 1993 new = *addr - 'a' + 10; 1994 } else if ((*addr >= 'A') && (*addr <= 'F')) { 1995 new = *addr - 'A' + 10; 1996 } else if (*addr == '\0') 1997 state |= END; 1998 else 1999 state |= DELIM; 2000 addr++; 2001 switch (state /* | INPUT */) { 2002 case GOTTWO | DIGIT: 2003 *cp++ = byte; /*FALLTHROUGH*/ 2004 case VIRGIN | DIGIT: 2005 state = GOTONE; byte = new; continue; 2006 case GOTONE | DIGIT: 2007 state = GOTTWO; byte = new + (byte << 4); continue; 2008 default: /* | DELIM */ 2009 state = VIRGIN; *cp++ = byte; byte = 0; continue; 2010 case GOTONE | END: 2011 case GOTTWO | END: 2012 *cp++ = byte; /* FALLTHROUGH */ 2013 case VIRGIN | END: 2014 break; 2015 } 2016 break; 2017 } while (cp < cplim); 2018 sa->sa_len = cp - (char *)sa; 2019 } 2020