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