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