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