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