1 /* 2 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 3 * Use is subject to license terms. 4 */ 5 6 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 7 /* All Rights Reserved */ 8 9 /* Copyright (c) 1990 Mentat Inc. */ 10 11 /* 12 * 13 * Copyright (c) 1983, 1989, 1991, 1993 14 * The Regents of the University of California. All rights reserved. 15 * 16 * Redistribution and use in source and binary forms, with or without 17 * modification, are permitted provided that the following conditions 18 * are met: 19 * 1. Redistributions of source code must retain the above copyright 20 * notice, this list of conditions and the following disclaimer. 21 * 2. Redistributions in binary form must reproduce the above copyright 22 * notice, this list of conditions and the following disclaimer in the 23 * documentation and/or other materials provided with the distribution. 24 * 3. All advertising materials mentioning features or use of this software 25 * must display the following acknowledgement: 26 * This product includes software developed by the University of 27 * California, Berkeley and its contributors. 28 * 4. Neither the name of the University nor the names of its contributors 29 * may be used to endorse or promote products derived from this software 30 * without specific prior written permission. 31 * 32 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 33 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 34 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 35 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 36 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 40 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 41 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 42 * SUCH DAMAGE. 43 * 44 * @(#)route.c 8.6 (Berkeley) 4/28/95 45 * @(#)linkaddr.c 8.1 (Berkeley) 6/4/93 46 */ 47 48 #pragma ident "%Z%%M% %I% %E% SMI" 49 50 #include <sys/param.h> 51 #include <sys/file.h> 52 #include <sys/socket.h> 53 #include <sys/ioctl.h> 54 #include <sys/stat.h> 55 #include <sys/stream.h> 56 #include <sys/sysmacros.h> 57 #include <sys/tihdr.h> 58 #include <sys/types.h> 59 #include <sys/ccompile.h> 60 61 #include <net/if.h> 62 #include <net/route.h> 63 #include <net/if_dl.h> 64 #include <netinet/in.h> 65 #include <arpa/inet.h> 66 #include <netdb.h> 67 #include <inet/mib2.h> 68 #include <inet/ip.h> 69 70 #include <limits.h> 71 #include <locale.h> 72 73 #include <errno.h> 74 #include <unistd.h> 75 #include <stdio.h> 76 #include <stdlib.h> 77 #include <stddef.h> 78 #include <string.h> 79 #include <stropts.h> 80 #include <fcntl.h> 81 #include <stdarg.h> 82 #include <assert.h> 83 #include <strings.h> 84 85 #include <libtsnet.h> 86 #include <tsol/label.h> 87 88 static struct keytab { 89 char *kt_cp; 90 int kt_i; 91 } keywords[] = { 92 #define K_ADD 1 93 {"add", K_ADD}, 94 #define K_BLACKHOLE 2 95 {"blackhole", K_BLACKHOLE}, 96 #define K_CHANGE 3 97 {"change", K_CHANGE}, 98 #define K_CLONING 4 99 {"cloning", K_CLONING}, 100 #define K_DELETE 5 101 {"delete", K_DELETE}, 102 #define K_DST 6 103 {"dst", K_DST}, 104 #define K_EXPIRE 7 105 {"expire", K_EXPIRE}, 106 #define K_FLUSH 8 107 {"flush", K_FLUSH}, 108 #define K_GATEWAY 9 109 {"gateway", K_GATEWAY}, 110 #define K_GET 11 111 {"get", K_GET}, 112 #define K_HOPCOUNT 12 113 {"hopcount", K_HOPCOUNT}, 114 #define K_HOST 13 115 {"host", K_HOST}, 116 #define K_IFA 14 117 {"ifa", K_IFA}, 118 #define K_IFACE 15 119 {"iface", K_IFACE}, 120 #define K_IFP 16 121 {"ifp", K_IFP}, 122 #define K_INET 17 123 {"inet", K_INET}, 124 #define K_INET6 18 125 {"inet6", K_INET6}, 126 #define K_INTERFACE 19 127 {"interface", K_INTERFACE}, 128 #define K_LINK 20 129 {"link", K_LINK}, 130 #define K_LOCK 21 131 {"lock", K_LOCK}, 132 #define K_LOCKREST 22 133 {"lockrest", K_LOCKREST}, 134 #define K_MASK 23 135 {"mask", K_MASK}, 136 #define K_MONITOR 24 137 {"monitor", K_MONITOR}, 138 #define K_MTU 25 139 {"mtu", K_MTU}, 140 #define K_NET 26 141 {"net", K_NET}, 142 #define K_NETMASK 27 143 {"netmask", K_NETMASK}, 144 #define K_NOSTATIC 28 145 {"nostatic", K_NOSTATIC}, 146 #define K_PRIVATE 29 147 {"private", K_PRIVATE}, 148 #define K_PROTO1 30 149 {"proto1", K_PROTO1}, 150 #define K_PROTO2 31 151 {"proto2", K_PROTO2}, 152 #define K_RECVPIPE 32 153 {"recvpipe", K_RECVPIPE}, 154 #define K_REJECT 33 155 {"reject", K_REJECT}, 156 #define K_RTT 34 157 {"rtt", K_RTT}, 158 #define K_RTTVAR 35 159 {"rttvar", K_RTTVAR}, 160 #define K_SA 36 161 {"sa", K_SA}, 162 #define K_SENDPIPE 37 163 {"sendpipe", K_SENDPIPE}, 164 #define K_SSTHRESH 38 165 {"ssthresh", K_SSTHRESH}, 166 #define K_STATIC 39 167 {"static", K_STATIC}, 168 #define K_XRESOLVE 40 169 {"xresolve", K_XRESOLVE}, 170 #define K_MULTIRT 41 171 {"multirt", K_MULTIRT}, 172 #define K_SETSRC 42 173 {"setsrc", K_SETSRC}, 174 #define K_SHOW 43 175 {"show", K_SHOW}, 176 #define K_SECATTR 43 177 {"secattr", K_SECATTR}, 178 {0, 0} 179 }; 180 181 /* 182 * Size of buffers used to hold command lines from the saved route file as 183 * well as error strings. 184 */ 185 #define BUF_SIZE 2048 186 187 typedef union sockunion { 188 struct sockaddr sa; 189 struct sockaddr_in sin; 190 struct sockaddr_dl sdl; 191 struct sockaddr_in6 sin6; 192 } su_t; 193 194 /* 195 * This structure represents the digested information from parsing arguments 196 * to route add, change, delete, and get. 197 * 198 */ 199 typedef struct rtcmd_irep { 200 int ri_cmd; 201 int ri_flags; 202 int ri_af; 203 ulong_t ri_inits; 204 struct rt_metrics ri_metrics; 205 int ri_addrs; 206 su_t ri_dst; 207 char *ri_dest_str; 208 su_t ri_src; 209 su_t ri_gate; 210 struct hostent *ri_gate_hp; 211 char *ri_gate_str; 212 su_t ri_mask; 213 su_t ri_ifa; 214 su_t ri_ifp; 215 char *ri_ifp_str; 216 int ri_rtsa_cnt; /* number of gateway security attributes */ 217 struct rtsa_s ri_rtsa; /* enough space for one attribute */ 218 } rtcmd_irep_t; 219 220 typedef struct mib_item_s { 221 struct mib_item_s *next_item; 222 long group; 223 long mib_id; 224 long length; 225 intmax_t *valp; 226 } mib_item_t; 227 228 typedef enum { 229 ADDR_TYPE_ANY, 230 ADDR_TYPE_HOST, 231 ADDR_TYPE_NET 232 } addr_type_t; 233 234 typedef enum { 235 SEARCH_MODE_NULL, 236 SEARCH_MODE_PRINT, 237 SEARCH_MODE_DEL 238 } search_mode_t; 239 240 static boolean_t args_to_rtcmd(rtcmd_irep_t *rcip, char **argv, 241 char *cmd_string); 242 static void bprintf(FILE *fp, int b, char *s); 243 static boolean_t compare_rtcmd(rtcmd_irep_t *srch_rt, 244 rtcmd_irep_t *file_rt); 245 static void delRouteEntry(mib2_ipRouteEntry_t *rp, 246 mib2_ipv6RouteEntry_t *rp6, int seqno); 247 static void del_rtcmd_irep(rtcmd_irep_t *rcip); 248 static void flushroutes(int argc, char *argv[]); 249 static boolean_t getaddr(rtcmd_irep_t *rcip, int which, char *s, 250 addr_type_t atype); 251 static boolean_t in6_getaddr(char *s, struct sockaddr_in6 *sin6, 252 int *plenp, struct hostent **hpp); 253 static boolean_t in_getaddr(char *s, struct sockaddr_in *sin, 254 int *plenp, int which, struct hostent **hpp, addr_type_t atype, 255 rtcmd_irep_t *rcip); 256 static int in_getprefixlen(char *addr, int max_plen); 257 static boolean_t in_prefixlentomask(int prefixlen, int maxlen, 258 uchar_t *mask); 259 static void inet_makenetandmask(rtcmd_irep_t *rcip, in_addr_t net, 260 struct sockaddr_in *sin); 261 static in_addr_t inet_makesubnetmask(in_addr_t addr, in_addr_t mask); 262 static int keyword(const char *cp); 263 static void link_addr(const char *addr, struct sockaddr_dl *sdl); 264 static char *link_ntoa(const struct sockaddr_dl *sdl); 265 static mib_item_t *mibget(int sd); 266 static char *netname(struct sockaddr *sa); 267 static int newroute(char **argv); 268 static rtcmd_irep_t *new_rtcmd_irep(void); 269 static void pmsg_addrs(const char *cp, size_t len, uint_t addrs); 270 static void pmsg_common(const struct rt_msghdr *rtm, size_t len); 271 static void print_getmsg(rtcmd_irep_t *req_rt, 272 struct rt_msghdr *rtm, int msglen); 273 static void print_rtcmd_short(FILE *to, rtcmd_irep_t *rcip, 274 boolean_t gw_good, boolean_t to_saved); 275 static void print_rtmsg(struct rt_msghdr *rtm, int msglen); 276 static void quit(char *s, int err) __NORETURN; 277 static char *routename(const struct sockaddr *sa); 278 static void rtmonitor(int argc, char *argv[]); 279 static int rtmsg(rtcmd_irep_t *rcip); 280 static int salen(const struct sockaddr *sa); 281 static void save_route(int argc, char **argv, int do_flush); 282 static void save_string(char **dst, char *src); 283 static int search_rtfile(FILE *fp, FILE *temp_fp, rtcmd_irep_t *rt, 284 search_mode_t mode); 285 static void set_metric(rtcmd_irep_t *rcip, char *value, int key, 286 boolean_t lock); 287 static int show_saved_routes(int argc); 288 static void sockaddr(char *addr, struct sockaddr *sa); 289 static void sodump(su_t *su, char *which); 290 static void syntax_arg_missing(char *keyword); 291 static void syntax_bad_keyword(char *keyword); 292 static void syntax_error(char *err, ...); 293 static void usage(char *cp); 294 static void write_to_rtfile(FILE *fp, int argc, char **argv); 295 static void pmsg_secattr(const char *, size_t, const char *); 296 297 static pid_t pid; 298 static int s; 299 static boolean_t nflag; 300 static int af = AF_INET; 301 static boolean_t qflag, tflag; 302 static boolean_t verbose; 303 static boolean_t debugonly; 304 static boolean_t fflag; 305 static boolean_t update_table; 306 static boolean_t perm_flag; 307 static boolean_t early_v6_keyword; 308 static char perm_file_sfx[] = "/etc/inet/static_routes"; 309 static char *perm_file; 310 static char temp_file_sfx[] = "/etc/inet/static_routes.tmp"; 311 static char *temp_file; 312 static struct in6_addr in6_host_mask = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 313 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; 314 /* 315 * WARNING: 316 * This next variable indicates whether certain functions exit when an error 317 * is detected in the user input. Currently, exit_on_error is only set false 318 * in search_rtfile(), when argument are being parsed. Only those functions 319 * used by search_rtfile() to parse its arguments are designed to work in 320 * both modes. Take particular care in setting this false to ensure that any 321 * functions you call that might act on this flag properly return errors when 322 * exit_on_error is false. 323 */ 324 static int exit_on_error = B_TRUE; 325 326 static struct { 327 struct rt_msghdr m_rtm; 328 char m_space[BUF_SIZE]; 329 } m_rtmsg; 330 331 /* 332 * Sizes of data structures extracted from the base mib. 333 * This allows the size of the tables entries to grow while preserving 334 * binary compatibility. 335 */ 336 static int ipRouteEntrySize; 337 static int ipv6RouteEntrySize; 338 339 #define ROUNDUP_LONG(a) \ 340 ((a) > 0 ? (1 + (((a) - 1) | (sizeof (long) - 1))) : sizeof (long)) 341 #define ADVANCE(x, n) ((x) += ROUNDUP_LONG(salen(n))) 342 #define C(x) ((x) & 0xff) 343 344 /* 345 * return values from in_getprefixlen() 346 */ 347 #define BAD_ADDR -1 /* prefix is invalid */ 348 #define NO_PREFIX -2 /* no prefix was found */ 349 350 void 351 usage(char *cp) 352 { 353 if (cp != NULL) { 354 (void) fprintf(stderr, gettext("route: botched keyword: %s\n"), 355 cp); 356 } 357 (void) fprintf(stderr, gettext("usage: route [ -fnpqv ] " 358 "[ -R <root-dir> ] cmd [[ -<qualifers> ] args ]\n")); 359 exit(1); 360 /* NOTREACHED */ 361 } 362 363 /*PRINTFLIKE1*/ 364 void 365 syntax_error(char *err, ...) 366 { 367 va_list args; 368 369 if (exit_on_error) { 370 va_start(args, err); 371 (void) vfprintf(stderr, err, args); 372 va_end(args); 373 exit(1); 374 } 375 /* NOTREACHED */ 376 } 377 378 void 379 syntax_bad_keyword(char *keyword) 380 { 381 syntax_error(gettext("route: botched keyword: %s\n"), keyword); 382 } 383 384 void 385 syntax_arg_missing(char *keyword) 386 { 387 syntax_error(gettext("route: argument required following keyword %s\n"), 388 keyword); 389 } 390 391 void 392 quit(char *s, int sverrno) 393 { 394 (void) fprintf(stderr, "route: "); 395 if (s != NULL) 396 (void) fprintf(stderr, "%s: ", s); 397 (void) fprintf(stderr, "%s\n", strerror(sverrno)); 398 exit(sverrno); 399 /* NOTREACHED */ 400 } 401 402 int 403 main(int argc, char **argv) 404 { 405 extern int optind; 406 extern char *optarg; 407 int ch; 408 int rval; 409 size_t size; 410 const char *root_dir = NULL; 411 412 (void) setlocale(LC_ALL, ""); 413 414 #if !defined(TEXT_DOMAIN) 415 #define TEXT_DOMAIN "SYS_TEST" 416 #endif 417 (void) textdomain(TEXT_DOMAIN); 418 419 if (argc < 2) 420 usage(NULL); 421 422 while ((ch = getopt(argc, argv, "R:nqdtvfp")) != EOF) { 423 switch (ch) { 424 case 'n': 425 nflag = B_TRUE; 426 break; 427 case 'q': 428 qflag = B_TRUE; 429 break; 430 case 'v': 431 verbose = B_TRUE; 432 break; 433 case 't': 434 tflag = B_TRUE; 435 break; 436 case 'd': 437 debugonly = B_TRUE; 438 break; 439 case 'f': 440 fflag = B_TRUE; 441 break; 442 case 'p': 443 perm_flag = B_TRUE; 444 break; 445 case 'R': 446 root_dir = optarg; 447 break; 448 case '?': 449 default: 450 usage(NULL); 451 /* NOTREACHED */ 452 } 453 } 454 argc -= optind; 455 argv += optind; 456 457 pid = getpid(); 458 if (tflag) 459 s = open("/dev/null", O_WRONLY); 460 else 461 s = socket(PF_ROUTE, SOCK_RAW, 0); 462 if (s < 0) 463 quit("socket", errno); 464 465 /* 466 * Handle the -p and -R flags. The -R flag only applies 467 * when the -p flag is set. 468 */ 469 if (root_dir == NULL) { 470 perm_file = perm_file_sfx; 471 temp_file = temp_file_sfx; 472 } else { 473 size = strlen(root_dir) + sizeof (perm_file_sfx); 474 perm_file = malloc(size); 475 if (perm_file == NULL) 476 quit("malloc", errno); 477 (void) snprintf(perm_file, size, "%s%s", root_dir, 478 perm_file_sfx); 479 size = strlen(root_dir) + sizeof (temp_file_sfx); 480 temp_file = malloc(size); 481 if (temp_file == NULL) 482 quit("malloc", errno); 483 (void) snprintf(temp_file, size, "%s%s", root_dir, 484 temp_file_sfx); 485 } 486 /* 487 * Whether or not to act on the routing table. The only time the 488 * routing table is not modified is when both -p and -R are present. 489 */ 490 update_table = (!perm_flag || root_dir == NULL); 491 if (tflag) 492 perm_flag = 0; 493 494 if (fflag) { 495 /* 496 * Accept an address family keyword after the -f. Since the 497 * default address family is AF_INET, reassign af only for the 498 * other valid address families. 499 */ 500 if (*argv != NULL) { 501 switch (keyword(*argv)) { 502 case K_INET6: 503 af = AF_INET6; 504 early_v6_keyword = B_TRUE; 505 /* fallthrough */ 506 case K_INET: 507 /* Skip over the address family parameter. */ 508 argc--; 509 argv++; 510 break; 511 } 512 } 513 flushroutes(0, NULL); 514 } 515 516 if (*argv != NULL) { 517 switch (keyword(*argv)) { 518 case K_GET: 519 case K_CHANGE: 520 case K_ADD: 521 case K_DELETE: 522 rval = 0; 523 if (update_table) { 524 rval = newroute(argv); 525 } 526 if (perm_flag && (rval == 0 || rval == EEXIST || 527 rval == ESRCH)) { 528 save_route(argc, argv, B_FALSE); 529 return (0); 530 } 531 return (rval); 532 case K_SHOW: 533 if (perm_flag) { 534 return (show_saved_routes(argc)); 535 } else { 536 syntax_error(gettext( 537 "route: show command requires -p\n")); 538 } 539 /* NOTREACHED */ 540 case K_MONITOR: 541 rtmonitor(argc, argv); 542 /* NOTREACHED */ 543 544 case K_FLUSH: 545 flushroutes(argc, argv); 546 return (0); 547 } 548 } 549 if (!fflag) 550 usage(*argv); 551 return (0); 552 } 553 554 /* 555 * Purge all entries in the routing tables not 556 * associated with network interfaces. 557 */ 558 void 559 flushroutes(int argc, char *argv[]) 560 { 561 int seqno; 562 int sd; /* mib stream */ 563 mib_item_t *item; 564 mib2_ipRouteEntry_t *rp; 565 mib2_ipv6RouteEntry_t *rp6; 566 int oerrno; 567 int off = 0; 568 int on = 1; 569 570 if (argc > 1) { 571 argv++; 572 if (argc == 2 && **argv == '-') { 573 /* 574 * The address family (preceded by a dash) may be used 575 * to flush the routes of that particular family. 576 */ 577 switch (keyword(*argv + 1)) { 578 case K_INET: 579 af = AF_INET; 580 break; 581 case K_LINK: 582 af = AF_LINK; 583 break; 584 case K_INET6: 585 af = AF_INET6; 586 break; 587 default: 588 usage(*argv); 589 /* NOTREACHED */ 590 } 591 } else { 592 usage(*argv); 593 } 594 } 595 if (perm_flag) { 596 /* This flushes the persistent route file */ 597 save_route(0, NULL, B_TRUE); 598 } 599 if (!update_table) { 600 return; 601 } 602 603 if (setsockopt(s, SOL_SOCKET, SO_USELOOPBACK, (char *)&off, 604 sizeof (off)) < 0) 605 quit("setsockopt", errno); 606 607 sd = open("/dev/ip", O_RDWR); 608 oerrno = errno; 609 if (sd < 0) { 610 switch (errno) { 611 case EACCES: 612 (void) fprintf(stderr, 613 gettext("route: flush: insufficient privileges\n")); 614 exit(oerrno); 615 /* NOTREACHED */ 616 default: 617 quit(gettext("can't open mib stream"), oerrno); 618 /* NOTREACHED */ 619 } 620 } 621 if ((item = mibget(sd)) == NULL) 622 quit("mibget", errno); 623 if (verbose) { 624 (void) printf("Examining routing table from " 625 "T_SVR4_OPTMGMT_REQ\n"); 626 } 627 seqno = 0; /* ??? */ 628 switch (af) { 629 case AF_INET: 630 /* Extract ipRouteEntrySize */ 631 for (; item != NULL; item = item->next_item) { 632 if (item->mib_id != 0) 633 continue; 634 if (item->group == MIB2_IP) { 635 ipRouteEntrySize = 636 ((mib2_ip_t *)item->valp)->ipRouteEntrySize; 637 assert(IS_P2ALIGNED(ipRouteEntrySize, 638 sizeof (mib2_ipRouteEntry_t *))); 639 break; 640 } 641 } 642 if (ipRouteEntrySize == 0) { 643 (void) fprintf(stderr, 644 gettext("ipRouteEntrySize can't be determined.\n")); 645 exit(1); 646 } 647 for (; item != NULL; item = item->next_item) { 648 /* 649 * skip all the other trash that comes up the mib stream 650 */ 651 if (item->group != MIB2_IP || 652 item->mib_id != MIB2_IP_ROUTE) 653 continue; 654 for (rp = (mib2_ipRouteEntry_t *)item->valp; 655 (char *)rp < (char *)item->valp + item->length; 656 /* LINTED */ 657 rp = (mib2_ipRouteEntry_t *) 658 ((char *)rp + ipRouteEntrySize)) { 659 delRouteEntry(rp, NULL, seqno); 660 seqno++; 661 } 662 break; 663 } 664 break; 665 case AF_INET6: 666 /* Extract ipv6RouteEntrySize */ 667 for (; item != NULL; item = item->next_item) { 668 if (item->mib_id != 0) 669 continue; 670 if (item->group == MIB2_IP6) { 671 ipv6RouteEntrySize = 672 ((mib2_ipv6IfStatsEntry_t *)item->valp)-> 673 ipv6RouteEntrySize; 674 assert(IS_P2ALIGNED(ipv6RouteEntrySize, 675 sizeof (mib2_ipv6RouteEntry_t *))); 676 break; 677 } 678 } 679 if (ipv6RouteEntrySize == 0) { 680 (void) fprintf(stderr, gettext( 681 "ipv6RouteEntrySize cannot be determined.\n")); 682 exit(1); 683 } 684 for (; item != NULL; item = item->next_item) { 685 /* 686 * skip all the other trash that comes up the mib stream 687 */ 688 if (item->group != MIB2_IP6 || 689 item->mib_id != MIB2_IP6_ROUTE) 690 continue; 691 for (rp6 = (mib2_ipv6RouteEntry_t *)item->valp; 692 (char *)rp6 < (char *)item->valp + item->length; 693 /* LINTED */ 694 rp6 = (mib2_ipv6RouteEntry_t *) 695 ((char *)rp6 + ipv6RouteEntrySize)) { 696 delRouteEntry(NULL, rp6, seqno); 697 seqno++; 698 } 699 break; 700 } 701 break; 702 } 703 704 if (setsockopt(s, SOL_SOCKET, SO_USELOOPBACK, (char *)&on, 705 sizeof (on)) < 0) 706 quit("setsockopt", errno); 707 } 708 709 /* 710 * Given the contents of a mib_item_t of id type MIB2_IP_ROUTE or 711 * MIB2_IP6_ROUTE, construct and send an RTM_DELETE routing socket message in 712 * order to facilitate the flushing of RTF_GATEWAY routes. 713 */ 714 static void 715 delRouteEntry(mib2_ipRouteEntry_t *rp, mib2_ipv6RouteEntry_t *rp6, int seqno) 716 { 717 char *cp; 718 int ire_type; 719 int rlen; 720 struct rt_msghdr *rtm; 721 struct sockaddr_in sin; 722 struct sockaddr_in6 sin6; 723 int slen; 724 725 if (rp != NULL) 726 ire_type = rp->ipRouteInfo.re_ire_type; 727 else 728 ire_type = rp6->ipv6RouteInfo.re_ire_type; 729 if (ire_type != IRE_DEFAULT && 730 ire_type != IRE_PREFIX && 731 ire_type != IRE_HOST && 732 ire_type != IRE_HOST_REDIRECT) 733 return; 734 735 rtm = &m_rtmsg.m_rtm; 736 (void) memset(rtm, 0, sizeof (m_rtmsg)); 737 rtm->rtm_type = RTM_DELETE; 738 rtm->rtm_seq = seqno; 739 rtm->rtm_flags |= RTF_GATEWAY; 740 rtm->rtm_version = RTM_VERSION; 741 rtm->rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK; 742 cp = m_rtmsg.m_space; 743 if (rp != NULL) { 744 slen = sizeof (struct sockaddr_in); 745 if (rp->ipRouteMask == IP_HOST_MASK) 746 rtm->rtm_flags |= RTF_HOST; 747 (void) memset(&sin, 0, slen); 748 sin.sin_family = AF_INET; 749 sin.sin_addr.s_addr = rp->ipRouteDest; 750 (void) memmove(cp, &sin, slen); 751 cp += slen; 752 sin.sin_addr.s_addr = rp->ipRouteNextHop; 753 (void) memmove(cp, &sin, slen); 754 cp += slen; 755 sin.sin_addr.s_addr = rp->ipRouteMask; 756 (void) memmove(cp, &sin, slen); 757 cp += slen; 758 } else { 759 slen = sizeof (struct sockaddr_in6); 760 if (rp6->ipv6RoutePfxLength == IPV6_ABITS) 761 rtm->rtm_flags |= RTF_HOST; 762 (void) memset(&sin6, 0, slen); 763 sin6.sin6_family = AF_INET6; 764 sin6.sin6_addr = rp6->ipv6RouteDest; 765 (void) memmove(cp, &sin6, slen); 766 cp += slen; 767 sin6.sin6_addr = rp6->ipv6RouteNextHop; 768 (void) memmove(cp, &sin6, slen); 769 cp += slen; 770 (void) memset(&sin6.sin6_addr, 0, sizeof (sin6.sin6_addr)); 771 (void) in_prefixlentomask(rp6->ipv6RoutePfxLength, IPV6_ABITS, 772 (uchar_t *)&sin6.sin6_addr.s6_addr); 773 (void) memmove(cp, &sin6, slen); 774 cp += slen; 775 } 776 rtm->rtm_msglen = cp - (char *)&m_rtmsg; 777 if (debugonly) { 778 /* 779 * In debugonly mode, the routing socket message to delete the 780 * current entry is not actually sent. However if verbose is 781 * also set, the routing socket message that would have been 782 * is printed. 783 */ 784 if (verbose) 785 print_rtmsg(rtm, rtm->rtm_msglen); 786 return; 787 } 788 789 rlen = write(s, (char *)&m_rtmsg, rtm->rtm_msglen); 790 if (rlen < (int)rtm->rtm_msglen) { 791 if (rlen < 0) { 792 (void) fprintf(stderr, 793 gettext("route: write to routing socket: %s\n"), 794 strerror(errno)); 795 } else { 796 (void) fprintf(stderr, gettext("route: write to " 797 "routing socket got only %d for rlen\n"), rlen); 798 } 799 return; 800 } 801 if (qflag) { 802 /* 803 * In quiet mode, nothing is printed at all (unless the write() 804 * itself failed. 805 */ 806 return; 807 } 808 if (verbose) { 809 print_rtmsg(rtm, rlen); 810 } else { 811 struct sockaddr *sa = (struct sockaddr *)(rtm + 1); 812 813 (void) printf("%-20.20s ", 814 rtm->rtm_flags & RTF_HOST ? routename(sa) : 815 netname(sa)); 816 /* LINTED */ 817 sa = (struct sockaddr *)(salen(sa) + (char *)sa); 818 (void) printf("%-20.20s ", routename(sa)); 819 (void) printf("done\n"); 820 } 821 } 822 823 /* 824 * Return the name of the host whose address is given. 825 */ 826 char * 827 routename(const struct sockaddr *sa) 828 { 829 char *cp; 830 static char line[MAXHOSTNAMELEN + 1]; 831 struct hostent *hp = NULL; 832 static char domain[MAXHOSTNAMELEN + 1]; 833 static boolean_t first = B_TRUE; 834 struct in_addr in; 835 struct in6_addr in6; 836 int error_num; 837 ushort_t *s; 838 ushort_t *slim; 839 840 if (first) { 841 first = B_FALSE; 842 if (gethostname(domain, MAXHOSTNAMELEN) == 0 && 843 (cp = strchr(domain, '.'))) 844 (void) strcpy(domain, cp + 1); 845 else 846 domain[0] = 0; 847 } 848 849 if (salen(sa) == 0) { 850 (void) strcpy(line, "default"); 851 return (line); 852 } 853 switch (sa->sa_family) { 854 855 case AF_INET: 856 /* LINTED */ 857 in = ((struct sockaddr_in *)sa)->sin_addr; 858 859 cp = NULL; 860 if (in.s_addr == INADDR_ANY) 861 cp = "default"; 862 if (cp == NULL && !nflag) { 863 hp = gethostbyaddr((char *)&in, sizeof (struct in_addr), 864 AF_INET); 865 if (hp != NULL) { 866 if (((cp = strchr(hp->h_name, '.')) != NULL) && 867 (strcmp(cp + 1, domain) == 0)) 868 *cp = 0; 869 cp = hp->h_name; 870 } 871 } 872 if (cp != NULL) { 873 (void) strncpy(line, cp, MAXHOSTNAMELEN); 874 line[MAXHOSTNAMELEN] = '\0'; 875 } else { 876 in.s_addr = ntohl(in.s_addr); 877 (void) sprintf(line, "%u.%u.%u.%u", C(in.s_addr >> 24), 878 C(in.s_addr >> 16), C(in.s_addr >> 8), 879 C(in.s_addr)); 880 } 881 break; 882 883 case AF_LINK: 884 return (link_ntoa((struct sockaddr_dl *)sa)); 885 886 case AF_INET6: 887 /* LINTED */ 888 in6 = ((struct sockaddr_in6 *)sa)->sin6_addr; 889 890 cp = NULL; 891 if (IN6_IS_ADDR_UNSPECIFIED(&in6)) 892 cp = "default"; 893 if (cp == NULL && !nflag) { 894 hp = getipnodebyaddr((char *)&in6, 895 sizeof (struct in6_addr), AF_INET6, &error_num); 896 if (hp != NULL) { 897 if (((cp = strchr(hp->h_name, '.')) != NULL) && 898 (strcmp(cp + 1, domain) == 0)) 899 *cp = 0; 900 cp = hp->h_name; 901 } 902 } 903 if (cp != NULL) { 904 (void) strncpy(line, cp, MAXHOSTNAMELEN); 905 line[MAXHOSTNAMELEN] = '\0'; 906 } else { 907 (void) inet_ntop(AF_INET6, (void *)&in6, line, 908 INET6_ADDRSTRLEN); 909 } 910 if (hp != NULL) 911 freehostent(hp); 912 913 break; 914 915 default: 916 s = (ushort_t *)sa; 917 918 slim = s + ((salen(sa) + 1) >> 1); 919 cp = line + sprintf(line, "(%d)", sa->sa_family); 920 921 while (++s < slim) /* start with sa->sa_data */ 922 cp += sprintf(cp, " %x", *s); 923 break; 924 } 925 return (line); 926 } 927 928 /* 929 * Return the name of the network whose address is given. 930 * The address is assumed to be that of a net or subnet, not a host. 931 */ 932 static char * 933 netname(struct sockaddr *sa) 934 { 935 char *cp = NULL; 936 static char line[MAXHOSTNAMELEN + 1]; 937 struct netent *np; 938 in_addr_t net, mask; 939 int subnetshift; 940 struct in_addr in; 941 ushort_t *s; 942 ushort_t *slim; 943 944 switch (sa->sa_family) { 945 946 case AF_INET: 947 /* LINTED */ 948 in = ((struct sockaddr_in *)sa)->sin_addr; 949 950 in.s_addr = ntohl(in.s_addr); 951 if (in.s_addr == INADDR_ANY) { 952 cp = "default"; 953 } else if (!nflag) { 954 if (IN_CLASSA(in.s_addr)) { 955 mask = IN_CLASSA_NET; 956 subnetshift = 8; 957 } else if (IN_CLASSB(in.s_addr)) { 958 mask = IN_CLASSB_NET; 959 subnetshift = 8; 960 } else { 961 mask = IN_CLASSC_NET; 962 subnetshift = 4; 963 } 964 /* 965 * If there are more bits than the standard mask 966 * would suggest, subnets must be in use. 967 * Guess at the subnet mask, assuming reasonable 968 * width subnet fields. 969 */ 970 while (in.s_addr &~ mask) 971 mask = (long)mask >> subnetshift; 972 net = in.s_addr & mask; 973 while ((mask & 1) == 0) 974 mask >>= 1, net >>= 1; 975 np = getnetbyaddr(net, AF_INET); 976 if (np != NULL) 977 cp = np->n_name; 978 } 979 if (cp != NULL) { 980 (void) strncpy(line, cp, MAXHOSTNAMELEN); 981 line[MAXHOSTNAMELEN] = '\0'; 982 } else if ((in.s_addr & 0xffffff) == 0) { 983 (void) sprintf(line, "%u", C(in.s_addr >> 24)); 984 } else if ((in.s_addr & 0xffff) == 0) { 985 (void) sprintf(line, "%u.%u", C(in.s_addr >> 24), 986 C(in.s_addr >> 16)); 987 } else if ((in.s_addr & 0xff) == 0) { 988 (void) sprintf(line, "%u.%u.%u", C(in.s_addr >> 24), 989 C(in.s_addr >> 16), C(in.s_addr >> 8)); 990 } else { 991 (void) sprintf(line, "%u.%u.%u.%u", C(in.s_addr >> 24), 992 C(in.s_addr >> 16), C(in.s_addr >> 8), 993 C(in.s_addr)); 994 } 995 break; 996 997 case AF_LINK: 998 return (link_ntoa((struct sockaddr_dl *)sa)); 999 1000 case AF_INET6: 1001 return (routename(sa)); 1002 1003 default: 1004 /* LINTED */ 1005 s = (ushort_t *)sa->sa_data; 1006 1007 slim = s + ((salen(sa) + 1) >> 1); 1008 cp = line + sprintf(line, "af %d:", sa->sa_family); 1009 1010 while (s < slim) 1011 cp += sprintf(cp, " %x", *s++); 1012 break; 1013 } 1014 return (line); 1015 } 1016 1017 /* 1018 * Initialize a new structure. Keep in mind that ri_dst_str, ri_gate_str and 1019 * ri_ifp_str will be freed by det_rtcmd_irep, so they should either be NULL 1020 * or point to dynamically allocated memory. 1021 */ 1022 rtcmd_irep_t * 1023 new_rtcmd_irep(void) 1024 { 1025 rtcmd_irep_t *rcip; 1026 1027 rcip = calloc(1, sizeof (rtcmd_irep_t)); 1028 if (rcip == NULL) { 1029 quit("calloc", errno); 1030 } 1031 rcip->ri_af = af; 1032 rcip->ri_flags = RTF_STATIC; 1033 return (rcip); 1034 } 1035 1036 void 1037 del_rtcmd_irep(rtcmd_irep_t *rcip) 1038 { 1039 free(rcip->ri_dest_str); 1040 free(rcip->ri_gate_str); 1041 free(rcip->ri_ifp_str); 1042 if (rcip->ri_gate_hp != NULL) { 1043 freehostent(rcip->ri_gate_hp); 1044 } 1045 free(rcip); 1046 } 1047 1048 void 1049 save_string(char **dst, char *src) 1050 { 1051 free(*dst); 1052 *dst = strdup(src); 1053 if (*dst == NULL) { 1054 quit("malloc", errno); 1055 } 1056 } 1057 1058 /* 1059 * Print the short form summary of a route command. 1060 * Eg. "add net default: gateway 10.0.0.1" 1061 * The final newline is not added, allowing the caller to append additional 1062 * information. 1063 */ 1064 void 1065 print_rtcmd_short(FILE *to, rtcmd_irep_t *rcip, boolean_t gw_good, 1066 boolean_t to_saved) 1067 { 1068 char *cmd; 1069 char obuf[INET6_ADDRSTRLEN]; 1070 1071 switch (rcip->ri_cmd) { 1072 case RTM_ADD: 1073 cmd = "add"; 1074 break; 1075 case RTM_CHANGE: 1076 cmd = "change"; 1077 break; 1078 case RTM_DELETE: 1079 cmd = "delete"; 1080 break; 1081 case RTM_GET: 1082 cmd = "get"; 1083 break; 1084 default: 1085 assert(0); 1086 } 1087 1088 (void) fprintf(to, "%s%s %s %s", cmd, 1089 (to_saved) ? " persistent" : "", 1090 (rcip->ri_flags & RTF_HOST) ? "host" : "net", 1091 (rcip->ri_dest_str == NULL) ? "NULL" : rcip->ri_dest_str); 1092 1093 if (rcip->ri_gate_str != NULL) { 1094 switch (rcip->ri_af) { 1095 case AF_INET: 1096 if (nflag) { 1097 (void) fprintf(to, ": gateway %s", 1098 inet_ntoa(rcip->ri_gate.sin.sin_addr)); 1099 } else if (gw_good && 1100 rcip->ri_gate_hp != NULL && 1101 rcip->ri_gate_hp->h_addr_list[1] != NULL) { 1102 /* 1103 * Print the actual address used in the case 1104 * where there was more than one address 1105 * available for the name, and one was used 1106 * successfully. 1107 */ 1108 (void) fprintf(to, ": gateway %s (%s)", 1109 rcip->ri_gate_str, 1110 inet_ntoa(rcip->ri_gate.sin.sin_addr)); 1111 } else { 1112 (void) fprintf(to, ": gateway %s", 1113 rcip->ri_gate_str); 1114 } 1115 break; 1116 case AF_INET6: 1117 if (inet_ntop(AF_INET6, 1118 &rcip->ri_gate.sin6.sin6_addr, obuf, 1119 INET6_ADDRSTRLEN) != NULL) { 1120 if (nflag) { 1121 (void) fprintf(to, ": gateway %s", 1122 obuf); 1123 break; 1124 } 1125 if (gw_good && 1126 rcip->ri_gate_hp->h_addr_list[1] != NULL) { 1127 (void) fprintf(to, ": gateway %s (%s)", 1128 rcip->ri_gate_str, obuf); 1129 break; 1130 } 1131 } 1132 /* FALLTHROUGH */ 1133 default: 1134 (void) fprintf(to, ": gateway %s", 1135 rcip->ri_gate_str); 1136 break; 1137 } 1138 } 1139 } 1140 1141 void 1142 set_metric(rtcmd_irep_t *rcip, char *value, int key, boolean_t lock) 1143 { 1144 int flag = 0; 1145 uint_t noval, *valp = &noval; 1146 1147 switch (key) { 1148 #define caseof(x, y, z) \ 1149 case (x): valp = &(rcip->ri_metrics.z); flag = (y); break 1150 1151 caseof(K_MTU, RTV_MTU, rmx_mtu); 1152 caseof(K_HOPCOUNT, RTV_HOPCOUNT, rmx_hopcount); 1153 caseof(K_EXPIRE, RTV_EXPIRE, rmx_expire); 1154 caseof(K_RECVPIPE, RTV_RPIPE, rmx_recvpipe); 1155 caseof(K_SENDPIPE, RTV_SPIPE, rmx_sendpipe); 1156 caseof(K_SSTHRESH, RTV_SSTHRESH, rmx_ssthresh); 1157 caseof(K_RTT, RTV_RTT, rmx_rtt); 1158 caseof(K_RTTVAR, RTV_RTTVAR, rmx_rttvar); 1159 #undef caseof 1160 } 1161 rcip->ri_inits |= flag; 1162 if (lock) 1163 rcip->ri_metrics.rmx_locks |= flag; 1164 *valp = atoi(value); 1165 } 1166 1167 /* 1168 * Parse the options give in argv[], filling in rcip with the results. 1169 * If cmd_string is non-null, argc and argv are ignored, and cmd_string is 1170 * tokenized to produce the command line. Cmd_string is tokenized using 1171 * strtok, which will overwrite whitespace in the string with nulls. 1172 * 1173 * Returns B_TRUE on success and B_FALSE on failure. 1174 */ 1175 boolean_t 1176 args_to_rtcmd(rtcmd_irep_t *rcip, char **argv, char *cmd_string) 1177 { 1178 const char *ws = "\f\n\r\t\v "; 1179 char *tok = cmd_string; 1180 char *keyword_str; 1181 addr_type_t atype = ADDR_TYPE_ANY; 1182 boolean_t iflag = B_FALSE; 1183 boolean_t locknext = B_FALSE; 1184 boolean_t lockrest = B_FALSE; 1185 boolean_t dash_keyword; 1186 int key; 1187 char *err; 1188 1189 if (cmd_string == NULL) { 1190 tok = argv[0]; 1191 } else { 1192 tok = strtok(cmd_string, ws); 1193 } 1194 1195 /* 1196 * The command keywords are already fully checked by main() or 1197 * search_rtfile(). 1198 */ 1199 switch (*tok) { 1200 case 'a': 1201 rcip->ri_cmd = RTM_ADD; 1202 break; 1203 case 'c': 1204 rcip->ri_cmd = RTM_CHANGE; 1205 break; 1206 case 'd': 1207 rcip->ri_cmd = RTM_DELETE; 1208 break; 1209 case 'g': 1210 rcip->ri_cmd = RTM_GET; 1211 break; 1212 default: 1213 /* NOTREACHED */ 1214 quit(gettext("Internal Error"), EINVAL); 1215 /* NOTREACHED */ 1216 } 1217 1218 #define NEXTTOKEN \ 1219 ((tok = (cmd_string == NULL ? *++argv : strtok(NULL, ws))) != NULL) 1220 1221 while (NEXTTOKEN) { 1222 keyword_str = tok; 1223 if (*tok == '-') { 1224 dash_keyword = B_TRUE; 1225 key = keyword(tok + 1); 1226 } else { 1227 dash_keyword = B_FALSE; 1228 key = keyword(tok); 1229 if (key != K_HOST && key != K_NET) { 1230 /* All others must be preceded by '-' */ 1231 key = 0; 1232 } 1233 } 1234 switch (key) { 1235 case K_HOST: 1236 if (atype == ADDR_TYPE_NET) { 1237 syntax_error(gettext("route: -host and -net " 1238 "are mutually exclusive\n")); 1239 return (B_FALSE); 1240 } 1241 atype = ADDR_TYPE_HOST; 1242 break; 1243 case K_NET: 1244 if (atype == ADDR_TYPE_HOST) { 1245 syntax_error(gettext("route: -host and -net " 1246 "are mutually exclusive\n")); 1247 return (B_FALSE); 1248 } 1249 atype = ADDR_TYPE_NET; 1250 break; 1251 case K_LINK: 1252 rcip->ri_af = AF_LINK; 1253 break; 1254 case K_INET: 1255 rcip->ri_af = AF_INET; 1256 break; 1257 case K_SA: 1258 rcip->ri_af = PF_ROUTE; 1259 break; 1260 case K_INET6: 1261 rcip->ri_af = AF_INET6; 1262 break; 1263 case K_IFACE: 1264 case K_INTERFACE: 1265 iflag = B_TRUE; 1266 /* fallthrough */ 1267 case K_NOSTATIC: 1268 rcip->ri_flags &= ~RTF_STATIC; 1269 break; 1270 case K_LOCK: 1271 locknext = B_TRUE; 1272 break; 1273 case K_LOCKREST: 1274 lockrest = B_TRUE; 1275 break; 1276 case K_REJECT: 1277 rcip->ri_flags |= RTF_REJECT; 1278 break; 1279 case K_BLACKHOLE: 1280 rcip->ri_flags |= RTF_BLACKHOLE; 1281 break; 1282 case K_PROTO1: 1283 rcip->ri_flags |= RTF_PROTO1; 1284 break; 1285 case K_PROTO2: 1286 rcip->ri_flags |= RTF_PROTO2; 1287 break; 1288 case K_CLONING: 1289 rcip->ri_flags |= RTF_CLONING; 1290 break; 1291 case K_XRESOLVE: 1292 rcip->ri_flags |= RTF_XRESOLVE; 1293 break; 1294 case K_STATIC: 1295 rcip->ri_flags |= RTF_STATIC; 1296 break; 1297 case K_IFA: 1298 if (!NEXTTOKEN) { 1299 syntax_arg_missing(keyword_str); 1300 return (B_FALSE); 1301 } 1302 if (!getaddr(rcip, RTA_IFA, tok, atype)) { 1303 return (B_FALSE); 1304 } 1305 break; 1306 case K_IFP: 1307 if (!NEXTTOKEN) { 1308 syntax_arg_missing(keyword_str); 1309 return (B_FALSE); 1310 } 1311 if (!getaddr(rcip, RTA_IFP, tok, atype)) { 1312 return (B_FALSE); 1313 } 1314 break; 1315 case K_GATEWAY: 1316 if (!NEXTTOKEN) { 1317 syntax_arg_missing(keyword_str); 1318 return (B_FALSE); 1319 } 1320 if (!getaddr(rcip, RTA_GATEWAY, tok, atype)) { 1321 return (B_FALSE); 1322 } 1323 break; 1324 case K_DST: 1325 if (!NEXTTOKEN) { 1326 syntax_arg_missing(keyword_str); 1327 return (B_FALSE); 1328 } 1329 if (!getaddr(rcip, RTA_DST, tok, atype)) { 1330 return (B_FALSE); 1331 } 1332 break; 1333 case K_NETMASK: 1334 if (!NEXTTOKEN) { 1335 syntax_arg_missing(keyword_str); 1336 return (B_FALSE); 1337 } 1338 if (!getaddr(rcip, RTA_NETMASK, tok, atype)) { 1339 return (B_FALSE); 1340 } 1341 atype = ADDR_TYPE_NET; 1342 break; 1343 case K_MTU: 1344 case K_HOPCOUNT: 1345 case K_EXPIRE: 1346 case K_RECVPIPE: 1347 case K_SENDPIPE: 1348 case K_SSTHRESH: 1349 case K_RTT: 1350 case K_RTTVAR: 1351 if (!NEXTTOKEN) { 1352 syntax_arg_missing(keyword_str); 1353 return (B_FALSE); 1354 } 1355 set_metric(rcip, tok, key, locknext || lockrest); 1356 locknext = B_FALSE; 1357 break; 1358 case K_PRIVATE: 1359 rcip->ri_flags |= RTF_PRIVATE; 1360 break; 1361 case K_MULTIRT: 1362 rcip->ri_flags |= RTF_MULTIRT; 1363 break; 1364 case K_SETSRC: 1365 if (!NEXTTOKEN) { 1366 syntax_arg_missing(keyword_str); 1367 return (B_FALSE); 1368 } 1369 if (!getaddr(rcip, RTA_SRC, tok, atype)) { 1370 return (B_FALSE); 1371 } 1372 rcip->ri_flags |= RTF_SETSRC; 1373 break; 1374 case K_SECATTR: 1375 if (!NEXTTOKEN) { 1376 syntax_arg_missing(keyword_str); 1377 return (B_FALSE); 1378 } 1379 if (is_system_labeled()) { 1380 int err; 1381 1382 if (rcip->ri_rtsa_cnt >= 1) { 1383 syntax_error(gettext("route: can't " 1384 "specify more than one security " 1385 "attribute\n")); 1386 return (B_FALSE); 1387 } 1388 if (!rtsa_keyword(tok, &rcip->ri_rtsa, &err, 1389 NULL)) { 1390 syntax_error(gettext("route: " 1391 "bad security attribute: %s\n"), 1392 tsol_strerror(err, errno)); 1393 return (B_FALSE); 1394 } 1395 rcip->ri_rtsa_cnt++; 1396 } else { 1397 syntax_error(gettext("route: " 1398 "system is not labeled; cannot specify " 1399 "security attributes.\n")); 1400 return (B_FALSE); 1401 } 1402 break; 1403 default: 1404 if (dash_keyword) { 1405 syntax_bad_keyword(tok + 1); 1406 return (B_FALSE); 1407 } 1408 if ((rcip->ri_addrs & RTA_DST) == 0) { 1409 if (!getaddr(rcip, RTA_DST, tok, atype)) { 1410 return (B_FALSE); 1411 } 1412 } else if ((rcip->ri_addrs & RTA_GATEWAY) == 0) { 1413 /* 1414 * For the gateway parameter, retrieve the 1415 * pointer to the struct hostent so that all 1416 * possible addresses can be tried until one 1417 * is successful. 1418 */ 1419 if (!getaddr(rcip, RTA_GATEWAY, tok, atype)) { 1420 return (B_FALSE); 1421 } 1422 } else { 1423 ulong_t metric; 1424 /* 1425 * Assume that a regular number is a metric. 1426 * Needed for compatibility with old route 1427 * command syntax. 1428 */ 1429 errno = 0; 1430 metric = strtoul(tok, &err, 10); 1431 if (errno == 0 && *err == '\0' && 1432 metric < 0x80000000ul) { 1433 iflag = (metric == 0); 1434 if (verbose) { 1435 (void) printf("old usage of " 1436 "trailing number, assuming " 1437 "route %s\n", iflag ? 1438 "to if" : "via gateway"); 1439 } 1440 continue; 1441 } 1442 if (!getaddr(rcip, RTA_NETMASK, tok, atype)) { 1443 return (B_FALSE); 1444 } 1445 } 1446 } 1447 } 1448 #undef NEXTTOKEN 1449 1450 if ((rcip->ri_addrs & RTA_DST) == 0) { 1451 syntax_error(gettext("route: destination required\n")); 1452 return (B_FALSE); 1453 } else if ((rcip->ri_cmd == RTM_ADD || rcip->ri_cmd == RTM_DELETE) && 1454 (rcip->ri_addrs & RTA_GATEWAY) == 0) { 1455 syntax_error(gettext( 1456 "route: gateway required for add or delete command\n")); 1457 return (B_FALSE); 1458 } 1459 1460 if (!iflag) { 1461 rcip->ri_flags |= RTF_GATEWAY; 1462 } 1463 1464 if (atype != ADDR_TYPE_NET) { 1465 if (rcip->ri_addrs & RTA_NETMASK) { 1466 /* 1467 * We know the netmask, so we can set the host flag 1468 * based on whether the netmask is the host netmask. 1469 */ 1470 if (rcip->ri_af == AF_INET && 1471 rcip->ri_mask.sin.sin_addr.s_addr == 1472 IP_HOST_MASK) { 1473 rcip->ri_flags |= RTF_HOST; 1474 } 1475 if (rcip->ri_af == AF_INET6 && 1476 memcmp(&rcip->ri_mask.sin6.sin6_addr, 1477 &in6_host_mask, 1478 sizeof (struct in6_addr)) == 0) { 1479 rcip->ri_flags |= RTF_HOST; 1480 } 1481 } else { 1482 /* 1483 * If no prefix mask has been saved at this point, it 1484 * only makes sense to treat the destination address 1485 * as a host address. 1486 */ 1487 rcip->ri_flags |= RTF_HOST; 1488 } 1489 } 1490 return (B_TRUE); 1491 } 1492 1493 /* 1494 * This command always seeks to the end of the file prior to writing. 1495 */ 1496 void 1497 write_to_rtfile(FILE *fp, int argc, char **argv) 1498 { 1499 char file_line[BUF_SIZE]; 1500 int len; 1501 int i; 1502 1503 len = 0; 1504 if (early_v6_keyword) { 1505 /* 1506 * This flag is set when "inet6" was seen as an 1507 * argument to the -f flag. Normally, when writing 1508 * routes to the persistent route file, everything on 1509 * the command line after "add" is saved verbatim. 1510 * In this case, the arguments after "add" may not be 1511 * sufficient, as the ipv6 keyword came before "add", 1512 * yet must be present in the persistent route file. 1513 */ 1514 len += snprintf(file_line, BUF_SIZE, "-inet6 "); 1515 } 1516 for (i = 0; argc > 0 && len < BUF_SIZE; i++, argc--) { 1517 len += snprintf(&file_line[len], BUF_SIZE - len, "%s ", 1518 argv[i]); 1519 } 1520 if (len >= BUF_SIZE) 1521 quit(gettext("Internal Error"), EINVAL); 1522 file_line[len - 1] = '\n'; 1523 if (fseek(fp, 0, SEEK_END) != 0 || 1524 fputs(file_line, fp) == EOF) { 1525 quit(gettext("failed to write to route file"), 1526 errno); 1527 } 1528 } 1529 1530 boolean_t 1531 compare_rtcmd(rtcmd_irep_t *srch_rt, rtcmd_irep_t *file_rt) 1532 { 1533 if (strcmp(srch_rt->ri_dest_str, file_rt->ri_dest_str) != 0 || 1534 memcmp(&srch_rt->ri_mask, &file_rt->ri_mask, sizeof (su_t)) != 0) { 1535 return (B_FALSE); 1536 } 1537 return (srch_rt->ri_gate_str == NULL || 1538 strcmp(srch_rt->ri_gate_str, file_rt->ri_gate_str) == 0); 1539 } 1540 1541 /* 1542 * Search the route file for routes matching the supplied route. There are 3 1543 * modes of operation: 1544 * SEARCH_MODE_RET - no side effects. 1545 * SEARCH_MODE_PRINT - prints each matching line. 1546 * SEARCH_MODE_DEL - copies all valid, non-matching lines to tmp_fp. 1547 * 1548 * In all cases, the number of matches is returned. If rt is NULL, all routes 1549 * matching the global af value are considered matching. 1550 */ 1551 int 1552 search_rtfile(FILE *fp, FILE *temp_fp, rtcmd_irep_t *rt, search_mode_t mode) 1553 { 1554 char *tmp_buf; 1555 int match_cnt; 1556 boolean_t match; 1557 char file_line[BUF_SIZE + 4] = "add "; 1558 rtcmd_irep_t *thisrt; 1559 1560 match_cnt = 0; 1561 1562 /* 1563 * Leave space at the beginning of file_line for "add ". 1564 */ 1565 while (fgets(file_line + 4, BUF_SIZE, fp) != NULL) { 1566 1567 if (file_line[4] == '#' || file_line[4] == '\n') { 1568 /* Handle comments and blank lines */ 1569 if (mode == SEARCH_MODE_DEL && 1570 fputs(file_line + 4, temp_fp) == EOF) { 1571 quit(gettext( 1572 "route: failed to write to temp file"), 1573 errno); 1574 } 1575 continue; 1576 } 1577 thisrt = new_rtcmd_irep(); 1578 /* 1579 * thisrt->ri_af defaults to whatever address family happens 1580 * to be set in the global af, but routes in the persistent 1581 * route file must be treated as AF_INET by default. 1582 */ 1583 thisrt->ri_af = AF_INET; 1584 1585 exit_on_error = B_FALSE; 1586 tmp_buf = strdup(file_line); 1587 /* args_to_rtcmd() will mangle the string passed. */ 1588 if (!args_to_rtcmd(thisrt, NULL, tmp_buf)) { 1589 /* There was an error in args_to_rtcmd() or helpers */ 1590 del_rtcmd_irep(thisrt); 1591 free(tmp_buf); 1592 continue; 1593 } 1594 exit_on_error = B_TRUE; 1595 free(tmp_buf); 1596 1597 if (thisrt->ri_gate_str == NULL) { 1598 del_rtcmd_irep(thisrt); 1599 continue; 1600 } 1601 match = (rt == NULL) ? (thisrt->ri_af == af) : 1602 compare_rtcmd(rt, thisrt); 1603 1604 if (match) match_cnt++; 1605 if (match && mode == SEARCH_MODE_PRINT) { 1606 (void) printf("persistent: route %s", file_line); 1607 } 1608 if (match && mode == SEARCH_MODE_DEL) { 1609 thisrt->ri_cmd = RTM_DELETE; 1610 print_rtcmd_short(stdout, thisrt, B_FALSE, B_TRUE); 1611 (void) printf("\n"); 1612 } 1613 del_rtcmd_irep(thisrt); 1614 1615 if (!match && mode == SEARCH_MODE_DEL && 1616 fputs(file_line + 4, temp_fp) == EOF) { 1617 quit(gettext("failed to write to temp file"), 1618 errno); 1619 } 1620 } 1621 return (match_cnt); 1622 } 1623 1624 /* 1625 * Perform the route operation given in argv on the persistent route file. 1626 * If do_flush is set, the persistent route file is flushed of all routes 1627 * matching the global family, and the arguments are ignored. 1628 */ 1629 void 1630 save_route(int argc, char **argv, int do_flush) 1631 { 1632 rtcmd_irep_t *rt; 1633 int perm_fd; 1634 FILE *perm_fp; 1635 FILE *temp_fp; 1636 mode_t fmode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; 1637 struct flock lock; 1638 struct stat st; 1639 const char commentstr[] = 1640 "# File generated by route(1M) - do not edit.\n"; 1641 1642 perm_fd = open(perm_file, O_RDWR | O_CREAT, fmode); 1643 if (perm_fd == -1 || fstat(perm_fd, &st) == -1) 1644 quit("failed to open route file", errno); 1645 1646 lock.l_type = F_WRLCK; 1647 lock.l_whence = SEEK_SET; 1648 lock.l_start = 0; 1649 lock.l_len = 0; 1650 if (fcntl(perm_fd, F_SETLK, &lock) != 0) { 1651 quit(gettext("failed to lock route file"), errno); 1652 /* NOTREACHED */ 1653 } 1654 if (st.st_size == 0 && 1655 write(perm_fd, commentstr, sizeof (commentstr) - 1) != 1656 sizeof (commentstr) - 1) 1657 quit(gettext("failed to open route file"), errno); 1658 1659 if ((perm_fp = fdopen(perm_fd, "r+")) == NULL) { 1660 quit(gettext("failed to open route file"), errno); 1661 /* NOTREACHED */ 1662 } 1663 1664 if (!do_flush) { 1665 rt = new_rtcmd_irep(); 1666 (void) args_to_rtcmd(rt, argv, NULL); 1667 } 1668 if (do_flush || rt->ri_cmd == RTM_DELETE) { 1669 if ((temp_fp = fopen(temp_file, "w")) == NULL) { 1670 quit(gettext("failed to open temp file"), errno); 1671 /* NOTREACHED */ 1672 } 1673 } 1674 if (do_flush) { 1675 (void) search_rtfile(perm_fp, temp_fp, NULL, SEARCH_MODE_DEL); 1676 if (fclose(temp_fp) != 0 || rename(temp_file, perm_file) != 0) { 1677 quit(gettext("failed to update route file"), errno); 1678 /* NOTREACHED */ 1679 } 1680 (void) fclose(perm_fp); 1681 return; 1682 } 1683 1684 switch (rt->ri_cmd) { 1685 case RTM_ADD: 1686 if (search_rtfile(perm_fp, NULL, rt, SEARCH_MODE_NULL) > 0) { 1687 /* Route is already in the file */ 1688 print_rtcmd_short(stderr, rt, B_FALSE, B_TRUE); 1689 (void) fprintf(stderr, ": entry exists\n"); 1690 exit(1); 1691 } 1692 write_to_rtfile(perm_fp, argc - 1, argv + 1); 1693 print_rtcmd_short(stdout, rt, B_FALSE, B_TRUE); 1694 (void) printf("\n"); 1695 break; 1696 1697 case RTM_CHANGE: 1698 syntax_error( 1699 gettext("route: change command not supported with -p\n")); 1700 /* NOTREACHED */ 1701 1702 case RTM_DELETE: 1703 if (search_rtfile(perm_fp, temp_fp, rt, SEARCH_MODE_DEL) <= 0) { 1704 /* Route not found */ 1705 print_rtcmd_short(stderr, rt, B_FALSE, B_TRUE); 1706 (void) fprintf(stderr, gettext(": not in file\n")); 1707 exit(1); 1708 } 1709 if (fclose(temp_fp) != 0 || rename(temp_file, perm_file) != 0) { 1710 quit(gettext("failed to update route file"), errno); 1711 /* NOTREACHED */ 1712 } 1713 break; 1714 1715 case RTM_GET: 1716 if (search_rtfile(perm_fp, temp_fp, rt, SEARCH_MODE_PRINT) <= 1717 0) { 1718 print_rtcmd_short(stdout, rt, B_FALSE, B_TRUE); 1719 (void) printf(gettext(": not in file\n")); 1720 } 1721 break; 1722 1723 default: 1724 quit(gettext("Internal Error"), EINVAL); 1725 /* NOTREACHED */ 1726 } 1727 1728 /* 1729 * Closing the file unlocks it. 1730 */ 1731 (void) fclose(perm_fp); 1732 } 1733 1734 int 1735 show_saved_routes(int argc) 1736 { 1737 int perm_fd; 1738 FILE *perm_fp; 1739 struct flock lock; 1740 int count = 0; 1741 1742 if (argc != 1) { 1743 syntax_error(gettext("route: invalid arguments for show\n")); 1744 } 1745 1746 perm_fd = open(perm_file, O_RDONLY, 0); 1747 1748 if (perm_fd == -1) { 1749 if (errno == ENOENT) { 1750 (void) printf("No persistent routes are defined\n"); 1751 return (0); 1752 } else { 1753 quit(gettext("failed to open route file"), errno); 1754 } 1755 } 1756 lock.l_type = F_RDLCK; 1757 lock.l_whence = SEEK_SET; 1758 lock.l_start = 0; 1759 lock.l_len = 0; 1760 if (fcntl(perm_fd, F_SETLK, &lock) != 0) { 1761 quit(gettext("failed to lock route file"), 1762 errno); 1763 /* NOTREACHED */ 1764 } 1765 if ((perm_fp = fdopen(perm_fd, "r")) == NULL) { 1766 quit(gettext("failed to open route file"), errno); 1767 /* NOTREACHED */ 1768 } 1769 count += search_rtfile(perm_fp, NULL, NULL, SEARCH_MODE_PRINT); 1770 (void) fseek(perm_fp, 0, SEEK_SET); 1771 af = AF_INET6; 1772 count += search_rtfile(perm_fp, NULL, NULL, SEARCH_MODE_PRINT); 1773 1774 if (count == 0) 1775 (void) printf("No persistent routes are defined\n"); 1776 1777 (void) fclose(perm_fp); 1778 return (0); 1779 } 1780 1781 int 1782 newroute(char **argv) 1783 { 1784 rtcmd_irep_t *newrt; 1785 int ret, attempts, oerrno; 1786 char *err; 1787 char obuf[INET6_ADDRSTRLEN]; 1788 #define hp (newrt->ri_gate_hp) 1789 1790 newrt = new_rtcmd_irep(); 1791 (void) args_to_rtcmd(newrt, argv, NULL); 1792 1793 if (newrt->ri_cmd != RTM_GET && !tflag) { 1794 /* Don't want to read back our messages */ 1795 (void) shutdown(s, 0); 1796 } 1797 if (newrt->ri_addrs & RTA_IFP) { 1798 newrt->ri_ifp.sdl.sdl_index = if_nametoindex(newrt->ri_ifp_str); 1799 if (newrt->ri_ifp.sdl.sdl_index == 0) { 1800 if (errno != ENXIO) { 1801 quit("if_nametoindex", errno); 1802 } else { 1803 (void) fprintf(stderr, 1804 gettext("route: %s: no such interface\n"), 1805 newrt->ri_ifp_str); 1806 exit(1); 1807 } 1808 } 1809 newrt->ri_ifp.sdl.sdl_family = AF_LINK; 1810 } 1811 for (attempts = 1; ; attempts++) { 1812 errno = 0; 1813 if ((ret = rtmsg(newrt)) == 0) 1814 break; 1815 if (errno != ENETUNREACH && errno != ESRCH) 1816 break; 1817 if ((newrt->ri_addrs & RTA_GATEWAY) && hp != NULL && 1818 hp->h_addr_list[attempts] != NULL) { 1819 switch (af) { 1820 case AF_INET: 1821 (void) memmove(&newrt->ri_gate.sin.sin_addr, 1822 hp->h_addr_list[attempts], hp->h_length); 1823 continue; 1824 case AF_INET6: 1825 (void) memmove(&newrt->ri_gate.sin6.sin6_addr, 1826 hp->h_addr_list[attempts], hp->h_length); 1827 continue; 1828 } 1829 } 1830 break; 1831 } 1832 oerrno = errno; 1833 1834 if (newrt->ri_cmd != RTM_GET) { 1835 print_rtcmd_short(stdout, newrt, (ret == 0), B_FALSE); 1836 if (ret == 0) 1837 (void) printf("\n"); 1838 } else if (ret != 0) { 1839 /* 1840 * Note: there is nothing additional to print for get 1841 * if ret == 0. 1842 */ 1843 if (nflag) { 1844 switch (newrt->ri_af) { 1845 case AF_INET: 1846 (void) printf(" %s", 1847 inet_ntoa(newrt->ri_dst.sin.sin_addr)); 1848 break; 1849 case AF_INET6: 1850 if (inet_ntop(AF_INET6, 1851 (void *)&newrt->ri_dst.sin6.sin6_addr, 1852 obuf, INET6_ADDRSTRLEN) != NULL) { 1853 (void) printf(" %s", obuf); 1854 break; 1855 } 1856 /* FALLTHROUGH */ 1857 default: 1858 (void) printf("%s", newrt->ri_dest_str); 1859 break; 1860 } 1861 } else { 1862 (void) printf("%s", newrt->ri_dest_str); 1863 } 1864 } 1865 1866 if (ret != 0) { 1867 switch (oerrno) { 1868 case ESRCH: 1869 err = "not in table"; 1870 break; 1871 case EBUSY: 1872 err = "entry in use"; 1873 break; 1874 case ENOBUFS: 1875 err = "routing table overflow"; 1876 break; 1877 case EEXIST: 1878 err = "entry exists"; 1879 break; 1880 case EPERM: 1881 err = "insufficient privileges"; 1882 break; 1883 default: 1884 err = strerror(oerrno); 1885 break; 1886 } 1887 (void) printf(": %s\n", err); 1888 } 1889 1890 del_rtcmd_irep(newrt); 1891 1892 return (oerrno); 1893 #undef hp 1894 } 1895 1896 1897 /* 1898 * Convert a network number to the corresponding IP address. 1899 * If the RTA_NETMASK hasn't been specified yet set it based 1900 * on the class of address. 1901 */ 1902 static void 1903 inet_makenetandmask(rtcmd_irep_t *rcip, in_addr_t net, struct sockaddr_in *sin) 1904 { 1905 in_addr_t addr, mask; 1906 1907 if (net == 0) { 1908 mask = addr = 0; 1909 } else if (net < 128) { 1910 addr = net << IN_CLASSA_NSHIFT; 1911 mask = IN_CLASSA_NET; 1912 } else if (net < 65536) { 1913 addr = net << IN_CLASSB_NSHIFT; 1914 mask = IN_CLASSB_NET; 1915 } else if (net < 16777216L) { 1916 addr = net << IN_CLASSC_NSHIFT; 1917 mask = IN_CLASSC_NET; 1918 } else { 1919 addr = net; 1920 if ((addr & IN_CLASSA_HOST) == 0) 1921 mask = IN_CLASSA_NET; 1922 else if ((addr & IN_CLASSB_HOST) == 0) 1923 mask = IN_CLASSB_NET; 1924 else if ((addr & IN_CLASSC_HOST) == 0) 1925 mask = IN_CLASSC_NET; 1926 else { 1927 if (IN_CLASSA(addr)) 1928 mask = IN_CLASSA_NET; 1929 else if (IN_CLASSB(addr)) 1930 mask = IN_CLASSB_NET; 1931 else if (IN_CLASSC(addr)) 1932 mask = IN_CLASSC_NET; 1933 else 1934 mask = IP_HOST_MASK; 1935 mask = inet_makesubnetmask(addr, mask); 1936 } 1937 } 1938 sin->sin_addr.s_addr = htonl(addr); 1939 1940 if (!(rcip->ri_addrs & RTA_NETMASK)) { 1941 rcip->ri_addrs |= RTA_NETMASK; 1942 sin = &rcip->ri_mask.sin; 1943 sin->sin_addr.s_addr = htonl(mask); 1944 sin->sin_family = AF_INET; 1945 } 1946 } 1947 1948 static in_addr_t 1949 inet_makesubnetmask(in_addr_t addr, in_addr_t mask) 1950 { 1951 int n; 1952 struct ifconf ifc; 1953 struct ifreq ifreq; 1954 struct ifreq *ifr; 1955 struct sockaddr_in *sin; 1956 char *buf; 1957 int numifs; 1958 size_t bufsize; 1959 int iosoc; 1960 in_addr_t if_addr, if_mask; 1961 in_addr_t if_subnetmask = 0; 1962 short if_flags; 1963 1964 if (mask == 0) 1965 return (0); 1966 if ((iosoc = socket(AF_INET, SOCK_DGRAM, 0)) < 0) 1967 quit("socket", errno); 1968 if (ioctl(iosoc, SIOCGIFNUM, (char *)&numifs) < 0) 1969 quit("ioctl", errno); 1970 bufsize = numifs * sizeof (struct ifreq); 1971 buf = malloc(bufsize); 1972 if (buf == NULL) 1973 quit("malloc", errno); 1974 (void) memset(&ifc, 0, sizeof (ifc)); 1975 ifc.ifc_len = bufsize; 1976 ifc.ifc_buf = buf; 1977 if (ioctl(iosoc, SIOCGIFCONF, (char *)&ifc) < 0) 1978 quit("ioctl (get interface configuration)", errno); 1979 /* Let's check to see if this is maybe a local subnet route. */ 1980 ifr = ifc.ifc_req; 1981 for (n = ifc.ifc_len / sizeof (struct ifreq); n > 0; n--, ifr++) { 1982 ifreq = *ifr; 1983 /* LINTED */ 1984 sin = (struct sockaddr_in *)&ifr->ifr_addr; 1985 if_addr = ntohl(sin->sin_addr.s_addr); 1986 1987 if (ioctl(iosoc, SIOCGIFFLAGS, (char *)&ifreq) < 0) 1988 quit("ioctl (get interface flags)", errno); 1989 if ((ifreq.ifr_flags & IFF_UP) == 0) 1990 continue; 1991 if_flags = ifreq.ifr_flags; 1992 1993 if (ioctl(iosoc, SIOCGIFNETMASK, (char *)&ifreq) < 0) 1994 quit("ioctl (get netmask)", errno); 1995 /* LINTED */ 1996 sin = (struct sockaddr_in *)&ifreq.ifr_addr; 1997 if_mask = ntohl(sin->sin_addr.s_addr); 1998 if ((if_addr & mask) == (addr & mask)) { 1999 /* 2000 * Don't trust pt-pt interfaces if there are 2001 * other interfaces. 2002 */ 2003 if (if_flags & IFF_POINTOPOINT) { 2004 if_subnetmask = if_mask; 2005 continue; 2006 } 2007 /* 2008 * Fine. Just assume the same net mask as the 2009 * directly attached subnet interface is using. 2010 */ 2011 return (if_mask); 2012 } 2013 } 2014 if (if_subnetmask != 0) 2015 return (if_subnetmask); 2016 return (mask); 2017 } 2018 2019 /* 2020 * Interpret an argument as a network address of some kind. 2021 * 2022 * If the address family is one looked up in getaddr() using one of the 2023 * getipnodebyX() functions (currently only AF_INET6), then callers should 2024 * freehostent() the returned "struct hostent" pointer if one was passed in. 2025 * 2026 * If exit_on_error is true, this function will cause route to exit on error by 2027 * calling syntax_error(). Otherwise, it returns B_TRUE on success or B_FALSE 2028 * on failure. 2029 */ 2030 static boolean_t 2031 getaddr(rtcmd_irep_t *rcip, int which, char *s, addr_type_t atype) 2032 { 2033 su_t *su; 2034 struct hostent **hpp; 2035 struct hostent *hp; 2036 int masklen; 2037 2038 if (which == RTA_GATEWAY) { 2039 hpp = &(rcip->ri_gate_hp); 2040 } else { 2041 hpp = &hp; 2042 } 2043 *hpp = NULL; 2044 2045 rcip->ri_addrs |= which; 2046 switch (which) { 2047 case RTA_DST: 2048 save_string(&rcip->ri_dest_str, s); 2049 su = &rcip->ri_dst; 2050 su->sa.sa_family = rcip->ri_af; 2051 break; 2052 case RTA_GATEWAY: 2053 save_string(&rcip->ri_gate_str, s); 2054 su = &rcip->ri_gate; 2055 su->sa.sa_family = rcip->ri_af; 2056 break; 2057 case RTA_NETMASK: 2058 su = &rcip->ri_mask; 2059 su->sa.sa_family = rcip->ri_af; 2060 break; 2061 case RTA_IFP: 2062 save_string(&rcip->ri_ifp_str, s); 2063 return (B_TRUE); 2064 /* 2065 * RTA_SRC has overloaded meaning. It can represent the 2066 * src address of incoming or outgoing packets. 2067 */ 2068 case RTA_IFA: 2069 su = &rcip->ri_ifa; 2070 su->sa.sa_family = rcip->ri_af; 2071 break; 2072 case RTA_SRC: 2073 su = &rcip->ri_src; 2074 su->sa.sa_family = rcip->ri_af; 2075 break; 2076 default: 2077 /* NOTREACHED */ 2078 quit(gettext("Internal Error"), EINVAL); 2079 /* NOTREACHED */ 2080 } 2081 if (strcmp(s, "default") == 0) { 2082 if (which == RTA_DST) { 2083 return (getaddr(rcip, RTA_NETMASK, s, ADDR_TYPE_NET)); 2084 } 2085 if (which == RTA_SRC) { 2086 return (B_TRUE); 2087 } 2088 return (B_TRUE); 2089 } 2090 switch (rcip->ri_af) { 2091 case AF_LINK: 2092 link_addr(s, &su->sdl); 2093 return (B_TRUE); 2094 case PF_ROUTE: 2095 sockaddr(s, &su->sa); 2096 return (B_TRUE); 2097 case AF_INET6: 2098 switch (which) { 2099 case RTA_DST: 2100 if (!in6_getaddr(s, &su->sin6, &masklen, hpp)) { 2101 return (B_FALSE); 2102 } 2103 if (masklen != NO_PREFIX) { 2104 (void) memset(&rcip->ri_mask.sin6.sin6_addr, 0, 2105 sizeof (rcip->ri_mask.sin6.sin6_addr)); 2106 if (!in_prefixlentomask(masklen, IPV6_ABITS, 2107 (uchar_t *)&rcip->ri_mask.sin6.sin6_addr)) { 2108 syntax_error(gettext( 2109 "route: bad prefix length: %d\n"), 2110 masklen); 2111 return (B_FALSE); 2112 } 2113 rcip->ri_mask.sin6.sin6_family = rcip->ri_af; 2114 rcip->ri_addrs |= RTA_NETMASK; 2115 } 2116 return (B_TRUE); 2117 case RTA_GATEWAY: 2118 case RTA_IFA: 2119 case RTA_SRC: 2120 return (in6_getaddr(s, &su->sin6, NULL, hpp)); 2121 case RTA_NETMASK: 2122 syntax_error( 2123 gettext("route: -netmask not supported for IPv6: " 2124 "use <prefix>/<prefix-length> instead\n")); 2125 return (B_FALSE); 2126 default: 2127 quit(gettext("Internal Error"), EINVAL); 2128 /* NOTREACHED */ 2129 } 2130 case AF_INET: 2131 switch (which) { 2132 case RTA_DST: 2133 if (!in_getaddr(s, &su->sin, &masklen, which, hpp, 2134 atype, rcip)) { 2135 return (B_FALSE); 2136 } 2137 if (masklen != NO_PREFIX) { 2138 (void) memset(&rcip->ri_mask.sin.sin_addr, 0, 2139 sizeof (rcip->ri_mask.sin.sin_addr)); 2140 if (!in_prefixlentomask(masklen, IP_ABITS, 2141 (uchar_t *)&rcip->ri_mask.sin.sin_addr)) { 2142 syntax_error(gettext( 2143 "route: bad prefix length: %d\n"), 2144 masklen); 2145 return (B_FALSE); 2146 } 2147 rcip->ri_mask.sin.sin_family = rcip->ri_af; 2148 rcip->ri_addrs |= RTA_NETMASK; 2149 } 2150 return (B_TRUE); 2151 case RTA_GATEWAY: 2152 case RTA_IFA: 2153 case RTA_NETMASK: 2154 case RTA_SRC: 2155 return (in_getaddr(s, &su->sin, NULL, which, hpp, atype, 2156 rcip)); 2157 default: 2158 quit(gettext("Internal Error"), EINVAL); 2159 /* NOTREACHED */ 2160 } 2161 default: 2162 quit(gettext("Internal Error"), EINVAL); 2163 /* NOTREACHED */ 2164 } 2165 return (B_TRUE); 2166 } 2167 2168 /* 2169 * Interpret an argument as an IPv4 network address of some kind, 2170 * returning B_TRUE on success or B_FALSE on failure. 2171 * This function will cause an exit() on failure if exit_on_failure is set. 2172 * 2173 * Note that this tries host interpretation before network interpretation, 2174 * except when -net has been given and the destination address is being parsed. 2175 * 2176 * If the plenp argument is non-NULL, allow <addr>/<n> syntax and 2177 * pass out <n> in *plenp. 2178 * If <n> doesn't parse return BAD_ADDR as *plenp. 2179 * If no /<n> is present return NO_PREFIX as *plenp. 2180 */ 2181 static boolean_t 2182 in_getaddr(char *s, struct sockaddr_in *sin, int *plenp, int which, 2183 struct hostent **hpp, addr_type_t atype, rtcmd_irep_t *rcip) 2184 { 2185 struct hostent *hp; 2186 struct netent *np; 2187 in_addr_t val; 2188 char str[BUFSIZ]; 2189 2190 (void) strlcpy(str, s, sizeof (str)); 2191 2192 /* 2193 * If plenp is non-NULL, /<n> syntax for netmask is allowed. 2194 */ 2195 if (plenp != NULL) { 2196 char *cp; 2197 2198 *plenp = in_getprefixlen(str, IP_ABITS); 2199 if (*plenp == BAD_ADDR) 2200 return (B_FALSE); 2201 cp = strchr(str, '/'); 2202 if (cp != NULL) 2203 *cp = '\0'; 2204 } else if (strchr(str, '/') != NULL) { 2205 syntax_error(gettext("route: %s: unexpected '/'\n"), str); 2206 return (B_FALSE); 2207 } 2208 2209 (void) memset(sin, 0, sizeof (*sin)); 2210 sin->sin_family = AF_INET; 2211 2212 /* 2213 * Handle 255.255.255.255 as a special case first. 2214 */ 2215 if (strcmp(str, "255.255.255.255") == 0) { 2216 sin->sin_addr.s_addr = INADDR_BROADCAST; 2217 return (B_TRUE); 2218 } 2219 2220 val = inet_addr(str); 2221 if (val != (in_addr_t)-1) { 2222 /* Numeric address */ 2223 sin->sin_addr.s_addr = val; 2224 if (which == RTA_DST) { 2225 if (atype == ADDR_TYPE_NET || 2226 (atype == ADDR_TYPE_ANY && 2227 inet_lnaof(sin->sin_addr) == INADDR_ANY)) { 2228 /* This looks like a network address. */ 2229 inet_makenetandmask(rcip, ntohl(val), 2230 sin); 2231 } 2232 } 2233 return (B_TRUE); 2234 } 2235 /* Host or net name */ 2236 if (which != RTA_DST || atype != ADDR_TYPE_NET) { 2237 /* A host name is allowed. */ 2238 if ((hp = gethostbyname(str)) != NULL) { 2239 *hpp = hp; 2240 (void) memmove(&sin->sin_addr, hp->h_addr, 2241 hp->h_length); 2242 return (B_TRUE); 2243 } 2244 } 2245 if (atype != ADDR_TYPE_HOST) { 2246 /* A network name is allowed */ 2247 if ((np = getnetbyname(str)) != NULL && 2248 (val = np->n_net) != 0) { 2249 if (which == RTA_DST) { 2250 inet_makenetandmask(rcip, val, sin); 2251 } 2252 return (B_TRUE); 2253 } 2254 } 2255 syntax_error(gettext("%s: bad value\n"), s); 2256 return (B_FALSE); 2257 } 2258 2259 /* 2260 * Interpret an argument as an IPv6 network address of some kind, 2261 * returning B_TRUE on success or B_FALSE on failure. 2262 * This function will cause an exit() on failure if exit_on_failure is set. 2263 * 2264 * If the last argument is non-NULL allow a <addr>/<n> syntax and 2265 * pass out <n> in *plenp. 2266 * If <n> doesn't parse return BAD_ADDR as *plenp. 2267 * If no /<n> is present return NO_PREFIX as *plenp. 2268 */ 2269 static boolean_t 2270 in6_getaddr(char *s, struct sockaddr_in6 *sin6, int *plenp, 2271 struct hostent **hpp) 2272 { 2273 struct hostent *hp; 2274 char str[BUFSIZ]; 2275 int error_num; 2276 2277 (void) strlcpy(str, s, sizeof (str)); 2278 2279 /* 2280 * If plenp is non-NULL, /<n> syntax for netmask is allowed. 2281 */ 2282 if (plenp != NULL) { 2283 char *cp; 2284 2285 *plenp = in_getprefixlen(str, IPV6_ABITS); 2286 if (*plenp == BAD_ADDR) 2287 return (B_FALSE); 2288 cp = strchr(str, '/'); 2289 if (cp != NULL) 2290 *cp = '\0'; 2291 } else if (strchr(str, '/') != NULL) { 2292 syntax_error(gettext("route: %s: unexpected '/'\n"), str); 2293 return (B_FALSE); 2294 } 2295 2296 (void) memset(sin6, 0, sizeof (struct sockaddr_in6)); 2297 sin6->sin6_family = AF_INET6; 2298 2299 hp = getipnodebyname(str, AF_INET6, 0, &error_num); 2300 if (hp != NULL) { 2301 *hpp = hp; 2302 (void) memmove(&sin6->sin6_addr, hp->h_addr, hp->h_length); 2303 return (B_TRUE); 2304 } 2305 if (error_num == TRY_AGAIN) { 2306 /* 2307 * This isn't a problem if we aren't going to use the address 2308 * right away. 2309 */ 2310 if (!exit_on_error) { 2311 return (B_TRUE); 2312 } 2313 syntax_error(gettext("route: %s: bad address (try " 2314 "again later)\n"), s); 2315 return (B_FALSE); 2316 } 2317 syntax_error(gettext("route: %s: bad address\n"), s); 2318 return (B_FALSE); 2319 } 2320 2321 /* 2322 * Parse <addr>/<n> syntax and return the integer n. 2323 * If <addr> is missing or <n> is not a valid integer, this function calls 2324 * syntax_error() and returns BAD_ADDR. 2325 * if n is not between 0 and max_plen inclusive, this functions calls 2326 * syntax_error() and returns BAD_ADDR. 2327 * If /<n> is not present, this function returns NO_PREFIX. 2328 * The string addr is not modified. 2329 */ 2330 int 2331 in_getprefixlen(char *addr, int max_plen) 2332 { 2333 int prefixlen; 2334 char *str, *end; 2335 2336 str = strchr(addr, '/'); 2337 if (str == addr) { 2338 syntax_error(gettext("route: %s: unexpected '/'\n"), addr); 2339 return (BAD_ADDR); 2340 } 2341 if (str == NULL) 2342 return (NO_PREFIX); 2343 str++; 2344 2345 errno = 0; 2346 prefixlen = strtoul(str, &end, 10); 2347 if (errno != 0 || str == end) { 2348 syntax_error(gettext("route: bad prefix length %s\n"), str); 2349 return (BAD_ADDR); 2350 } 2351 if (prefixlen > max_plen) { 2352 syntax_error(gettext("route: prefix length %s out of range\n"), 2353 str); 2354 return (BAD_ADDR); 2355 } 2356 return (prefixlen); 2357 } 2358 2359 /* 2360 * Convert a prefix length to a mask. 2361 * Returns B_TRUE if ok. B_FALSE otherwise. 2362 * Assumes the mask array is zeroed by the caller. 2363 */ 2364 boolean_t 2365 in_prefixlentomask(int prefixlen, int maxlen, uchar_t *mask) 2366 { 2367 if (prefixlen < 0 || prefixlen > maxlen) 2368 return (B_FALSE); 2369 2370 while (prefixlen > 0) { 2371 if (prefixlen >= 8) { 2372 *mask++ = 0xFF; 2373 prefixlen -= 8; 2374 continue; 2375 } 2376 *mask |= 1 << (8 - prefixlen); 2377 prefixlen--; 2378 } 2379 return (B_TRUE); 2380 } 2381 2382 void 2383 rtmonitor(int argc, char *argv[]) 2384 { 2385 int n; 2386 intmax_t msg[2048 / sizeof (intmax_t)]; 2387 2388 if (tflag) 2389 exit(0); 2390 verbose = B_TRUE; 2391 if (argc > 1) { 2392 argv++; 2393 if (argc == 2 && **argv == '-') { 2394 switch (keyword(*argv + 1)) { 2395 case K_INET: 2396 af = AF_INET; 2397 break; 2398 case K_LINK: 2399 af = AF_LINK; 2400 break; 2401 case K_INET6: 2402 af = AF_INET6; 2403 break; 2404 default: 2405 usage(*argv); 2406 /* NOTREACHED */ 2407 } 2408 } else { 2409 usage(*argv); 2410 } 2411 (void) close(s); 2412 s = socket(PF_ROUTE, SOCK_RAW, af); 2413 if (s < 0) 2414 quit("socket", errno); 2415 } 2416 for (;;) { 2417 n = read(s, msg, sizeof (msg)); 2418 if (n <= 0) 2419 quit("read", errno); 2420 (void) printf("got message of size %d\n", n); 2421 print_rtmsg((struct rt_msghdr *)msg, n); 2422 } 2423 } 2424 2425 int 2426 rtmsg(rtcmd_irep_t *newrt) 2427 { 2428 static int seq; 2429 int rlen; 2430 char *cp = m_rtmsg.m_space; 2431 int l; 2432 2433 errno = 0; 2434 (void) memset(&m_rtmsg, 0, sizeof (m_rtmsg)); 2435 2436 if (newrt->ri_cmd == RTM_GET) { 2437 newrt->ri_ifp.sa.sa_family = AF_LINK; 2438 newrt->ri_addrs |= RTA_IFP; 2439 } 2440 2441 #define rtm m_rtmsg.m_rtm 2442 rtm.rtm_type = newrt->ri_cmd; 2443 rtm.rtm_flags = newrt->ri_flags; 2444 rtm.rtm_version = RTM_VERSION; 2445 rtm.rtm_seq = ++seq; 2446 rtm.rtm_addrs = newrt->ri_addrs; 2447 rtm.rtm_rmx = newrt->ri_metrics; 2448 rtm.rtm_inits = newrt->ri_inits; 2449 2450 #define NEXTADDR(w, u) \ 2451 if (newrt->ri_addrs & (w)) { \ 2452 l = ROUNDUP_LONG(salen(&u.sa)); \ 2453 (void) memmove(cp, &(u), l); \ 2454 cp += l; \ 2455 if (verbose) \ 2456 sodump(&(u), #u); \ 2457 } 2458 NEXTADDR(RTA_DST, newrt->ri_dst); 2459 NEXTADDR(RTA_GATEWAY, newrt->ri_gate); 2460 NEXTADDR(RTA_NETMASK, newrt->ri_mask); 2461 NEXTADDR(RTA_IFP, newrt->ri_ifp); 2462 NEXTADDR(RTA_IFA, newrt->ri_ifa); 2463 /* 2464 * RTA_SRC has overloaded meaning. It can represent the 2465 * src address of incoming or outgoing packets. 2466 */ 2467 NEXTADDR(RTA_SRC, newrt->ri_src); 2468 #undef NEXTADDR 2469 2470 if (newrt->ri_rtsa_cnt > 0) { 2471 /* LINTED: aligned */ 2472 rtm_ext_t *rtm_ext = (rtm_ext_t *)cp; 2473 tsol_rtsecattr_t *rtsecattr; 2474 2475 rtm_ext->rtmex_type = RTMEX_GATEWAY_SECATTR; 2476 rtm_ext->rtmex_len = TSOL_RTSECATTR_SIZE(1); 2477 2478 rtsecattr = (tsol_rtsecattr_t *)(rtm_ext + 1); 2479 rtsecattr->rtsa_cnt = 1; 2480 2481 bcopy(&newrt->ri_rtsa, rtsecattr->rtsa_attr, 2482 sizeof (newrt->ri_rtsa)); 2483 cp = (char *)(rtsecattr->rtsa_attr + 1); 2484 } 2485 2486 rtm.rtm_msglen = l = cp - (char *)&m_rtmsg; 2487 2488 if (verbose) 2489 print_rtmsg(&rtm, l); 2490 if (debugonly) 2491 return (0); 2492 if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) { 2493 switch (errno) { 2494 case ESRCH: 2495 case EBUSY: 2496 case ENOBUFS: 2497 case EEXIST: 2498 case ENETUNREACH: 2499 case EHOSTUNREACH: 2500 case EPERM: 2501 break; 2502 default: 2503 perror(gettext("writing to routing socket")); 2504 break; 2505 } 2506 return (-1); 2507 } else if (rlen < (int)rtm.rtm_msglen) { 2508 (void) fprintf(stderr, 2509 gettext("route: write to routing socket got only %d for " 2510 "len\n"), rlen); 2511 return (-1); 2512 } 2513 if (newrt->ri_cmd == RTM_GET) { 2514 do { 2515 l = read(s, (char *)&m_rtmsg, sizeof (m_rtmsg)); 2516 } while (l > 0 && (rtm.rtm_seq != seq || rtm.rtm_pid != pid)); 2517 if (l < 0) { 2518 (void) fprintf(stderr, 2519 gettext("route: read from routing socket: %s\n"), 2520 strerror(errno)); 2521 } else { 2522 print_getmsg(newrt, &rtm, l); 2523 } 2524 } 2525 #undef rtm 2526 return (0); 2527 } 2528 2529 static char *msgtypes[] = { 2530 "", 2531 "RTM_ADD: Add Route", 2532 "RTM_DELETE: Delete Route", 2533 "RTM_CHANGE: Change Metrics or flags", 2534 "RTM_GET: Report Metrics", 2535 "RTM_LOSING: Kernel Suspects Partitioning", 2536 "RTM_REDIRECT: Told to use different route", 2537 "RTM_MISS: Lookup failed on this address", 2538 "RTM_LOCK: fix specified metrics", 2539 "RTM_OLDADD: caused by SIOCADDRT", 2540 "RTM_OLDDEL: caused by SIOCDELRT", 2541 "RTM_RESOLVE: Route created by cloning", 2542 "RTM_NEWADDR: address being added to iface", 2543 "RTM_DELADDR: address being removed from iface", 2544 "RTM_IFINFO: iface status change", 2545 0, 2546 }; 2547 2548 #define NMSGTYPES (sizeof (msgtypes) / sizeof (msgtypes[0])) 2549 2550 static char metricnames[] = 2551 "\011pksent\010rttvar\7rtt\6ssthresh\5sendpipe\4recvpipe\3expire\2hopcount" 2552 "\1mtu"; 2553 static char routeflags[] = 2554 "\1UP\2GATEWAY\3HOST\4REJECT\5DYNAMIC\6MODIFIED\7DONE\010MASK_PRESENT" 2555 "\011CLONING\012XRESOLVE\013LLINFO\014STATIC\015BLACKHOLE" 2556 "\016PRIVATE\017PROTO2\020PROTO1\021MULTIRT\022SETSRC"; 2557 static char ifnetflags[] = 2558 "\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5PTP\6NOTRAILERS\7RUNNING\010NOARP" 2559 "\011PPROMISC\012ALLMULTI\013INTELLIGENT\014MULTICAST" 2560 "\015MULTI_BCAST\016UNNUMBERED\017DHCP\020PRIVATE" 2561 "\021NOXMIT\022NOLOCAL\023DEPRECATED\024ADDRCONF" 2562 "\025ROUTER\026NONUD\027ANYCAST\030NORTEXCH\031IPv4\032IPv6" 2563 "\033MIP\034NOFAILOVER\035FAILED\036STANDBY\037INACTIVE\040OFFLINE" 2564 "\041XRESOLV\042COS\043PREFERRED\044TEMPORARY"; 2565 static char addrnames[] = 2566 "\1DST\2GATEWAY\3NETMASK\4GENMASK\5IFP\6IFA\7AUTHOR\010BRD\011SRC"; 2567 2568 void 2569 print_rtmsg(struct rt_msghdr *rtm, int msglen) 2570 { 2571 struct if_msghdr *ifm; 2572 struct ifa_msghdr *ifam; 2573 2574 if (!verbose) 2575 return; 2576 if (rtm->rtm_version != RTM_VERSION) { 2577 (void) printf("routing message version %d not understood\n", 2578 rtm->rtm_version); 2579 return; 2580 } 2581 if (rtm->rtm_msglen != msglen) { 2582 (void) printf("message length mismatch, in packet %d, " 2583 "returned %d\n", 2584 rtm->rtm_msglen, msglen); 2585 if (msglen > rtm->rtm_msglen) 2586 msglen = rtm->rtm_msglen; 2587 } 2588 /* 2589 * Since rtm->rtm_type is unsigned, we'll just check the case of zero 2590 * and the upper-bound of (NMSGTYPES - 1). 2591 */ 2592 if (rtm->rtm_type == 0 || rtm->rtm_type >= (NMSGTYPES - 1)) { 2593 (void) printf("routing message type %d not understood\n", 2594 rtm->rtm_type); 2595 return; 2596 } 2597 (void) printf("%s: len %d, ", msgtypes[rtm->rtm_type], msglen); 2598 switch (rtm->rtm_type) { 2599 case RTM_IFINFO: 2600 ifm = (struct if_msghdr *)rtm; 2601 (void) printf("if# %d, flags:", ifm->ifm_index); 2602 bprintf(stdout, ifm->ifm_flags, ifnetflags); 2603 pmsg_addrs((const char *)(ifm + 1), msglen - sizeof (*ifm), 2604 ifm->ifm_addrs); 2605 break; 2606 case RTM_NEWADDR: 2607 case RTM_DELADDR: 2608 ifam = (struct ifa_msghdr *)rtm; 2609 (void) printf("metric %d, flags:", ifam->ifam_metric); 2610 bprintf(stdout, ifam->ifam_flags, routeflags); 2611 pmsg_addrs((const char *)(ifam + 1), msglen - sizeof (*ifam), 2612 ifam->ifam_addrs); 2613 break; 2614 default: 2615 (void) printf("pid: %ld, seq %d, errno %d, flags:", 2616 rtm->rtm_pid, rtm->rtm_seq, rtm->rtm_errno); 2617 bprintf(stdout, rtm->rtm_flags, routeflags); 2618 pmsg_common(rtm, msglen); 2619 break; 2620 } 2621 } 2622 2623 void 2624 print_getmsg(rtcmd_irep_t *req_rt, struct rt_msghdr *rtm, int msglen) 2625 { 2626 struct sockaddr *dst = NULL, *gate = NULL, *mask = NULL, *src = NULL; 2627 struct sockaddr_dl *ifp = NULL; 2628 struct sockaddr *sa; 2629 char *cp; 2630 int i; 2631 2632 (void) printf(" route to: %s\n", routename(&req_rt->ri_dst.sa)); 2633 if (rtm->rtm_version != RTM_VERSION) { 2634 (void) fprintf(stderr, 2635 gettext("routing message version %d not understood\n"), 2636 rtm->rtm_version); 2637 return; 2638 } 2639 if (rtm->rtm_msglen > (ushort_t)msglen) { 2640 (void) fprintf(stderr, 2641 gettext("message length mismatch, in packet %d, " 2642 "returned %d\n"), rtm->rtm_msglen, msglen); 2643 } 2644 if (rtm->rtm_errno) { 2645 (void) fprintf(stderr, "RTM_GET: %s (errno %d)\n", 2646 strerror(rtm->rtm_errno), rtm->rtm_errno); 2647 return; 2648 } 2649 cp = ((char *)(rtm + 1)); 2650 if (rtm->rtm_addrs != 0) { 2651 for (i = 1; i != 0; i <<= 1) { 2652 if (i & rtm->rtm_addrs) { 2653 /* LINTED */ 2654 sa = (struct sockaddr *)cp; 2655 switch (i) { 2656 case RTA_DST: 2657 dst = sa; 2658 break; 2659 case RTA_GATEWAY: 2660 gate = sa; 2661 break; 2662 case RTA_NETMASK: 2663 mask = sa; 2664 break; 2665 case RTA_IFP: 2666 if (sa->sa_family == AF_LINK && 2667 ((struct sockaddr_dl *)sa)-> 2668 sdl_nlen != 0) 2669 ifp = (struct sockaddr_dl *)sa; 2670 break; 2671 case RTA_SRC: 2672 src = sa; 2673 break; 2674 } 2675 ADVANCE(cp, sa); 2676 } 2677 } 2678 } 2679 if (dst != NULL && mask != NULL) 2680 mask->sa_family = dst->sa_family; /* XXX */ 2681 if (dst != NULL) 2682 (void) printf("destination: %s\n", routename(dst)); 2683 if (mask != NULL) { 2684 boolean_t savenflag = nflag; 2685 2686 nflag = B_TRUE; 2687 (void) printf(" mask: %s\n", routename(mask)); 2688 nflag = savenflag; 2689 } 2690 if (gate != NULL && rtm->rtm_flags & RTF_GATEWAY) 2691 (void) printf(" gateway: %s\n", routename(gate)); 2692 if (src != NULL && rtm->rtm_flags & RTF_SETSRC) 2693 (void) printf(" setsrc: %s\n", routename(src)); 2694 if (ifp != NULL) { 2695 if (verbose) { 2696 int i; 2697 2698 (void) printf(" interface: %.*s index %d address ", 2699 ifp->sdl_nlen, ifp->sdl_data, ifp->sdl_index); 2700 for (i = ifp->sdl_nlen; 2701 i < ifp->sdl_nlen + ifp->sdl_alen; 2702 i++) { 2703 (void) printf("%02x ", 2704 ifp->sdl_data[i] & 0xFF); 2705 } 2706 (void) printf("\n"); 2707 } else { 2708 (void) printf(" interface: %.*s\n", 2709 ifp->sdl_nlen, ifp->sdl_data); 2710 } 2711 } 2712 (void) printf(" flags: "); 2713 bprintf(stdout, rtm->rtm_flags, routeflags); 2714 2715 #define lock(f) ((rtm->rtm_rmx.rmx_locks & RTV_ ## f) ? 'L' : ' ') 2716 #define msec(u) (((u) + 500) / 1000) /* usec to msec */ 2717 2718 (void) printf("\n%s\n", " recvpipe sendpipe ssthresh rtt,ms " 2719 "rttvar,ms hopcount mtu expire"); 2720 (void) printf("%8d%c ", rtm->rtm_rmx.rmx_recvpipe, lock(RPIPE)); 2721 (void) printf("%8d%c ", rtm->rtm_rmx.rmx_sendpipe, lock(SPIPE)); 2722 (void) printf("%8d%c ", rtm->rtm_rmx.rmx_ssthresh, lock(SSTHRESH)); 2723 (void) printf("%8d%c ", msec(rtm->rtm_rmx.rmx_rtt), lock(RTT)); 2724 (void) printf("%8d%c ", msec(rtm->rtm_rmx.rmx_rttvar), lock(RTTVAR)); 2725 (void) printf("%8d%c ", rtm->rtm_rmx.rmx_hopcount, lock(HOPCOUNT)); 2726 (void) printf("%8d%c ", rtm->rtm_rmx.rmx_mtu, lock(MTU)); 2727 if (rtm->rtm_rmx.rmx_expire) 2728 rtm->rtm_rmx.rmx_expire -= time(0); 2729 (void) printf("%8d%c", rtm->rtm_rmx.rmx_expire, lock(EXPIRE)); 2730 #undef lock 2731 #undef msec 2732 #define RTA_IGN \ 2733 (RTA_DST|RTA_GATEWAY|RTA_NETMASK|RTA_IFP|RTA_IFA|RTA_BRD|RTA_SRC) 2734 if (verbose) { 2735 pmsg_common(rtm, msglen); 2736 } else { 2737 const char *sptr, *endptr; 2738 const struct sockaddr *sa; 2739 uint_t addrs; 2740 2741 /* Not verbose; just print out the exceptional cases */ 2742 if (rtm->rtm_addrs &~ RTA_IGN) { 2743 (void) printf("\nsockaddrs: "); 2744 bprintf(stdout, rtm->rtm_addrs, addrnames); 2745 } 2746 sptr = (const char *)(rtm + 1); 2747 endptr = (const char *)rtm + msglen; 2748 addrs = rtm->rtm_addrs; 2749 while (addrs != 0 && sptr + sizeof (*sa) <= endptr) { 2750 addrs &= addrs - 1; 2751 /* LINTED */ 2752 sa = (const struct sockaddr *)sptr; 2753 ADVANCE(sptr, sa); 2754 } 2755 if (addrs == 0) 2756 pmsg_secattr(sptr, endptr - sptr, " secattr: "); 2757 (void) putchar('\n'); 2758 } 2759 #undef RTA_IGN 2760 } 2761 2762 static void 2763 pmsg_common(const struct rt_msghdr *rtm, size_t msglen) 2764 { 2765 (void) printf("\nlocks: "); 2766 bprintf(stdout, (int)rtm->rtm_rmx.rmx_locks, metricnames); 2767 (void) printf(" inits: "); 2768 bprintf(stdout, (int)rtm->rtm_inits, metricnames); 2769 pmsg_addrs((const char *)(rtm + 1), msglen - sizeof (*rtm), 2770 rtm->rtm_addrs); 2771 } 2772 2773 static void 2774 pmsg_addrs(const char *cp, size_t msglen, uint_t addrs) 2775 { 2776 const struct sockaddr *sa; 2777 const char *maxptr; 2778 int i; 2779 2780 if (addrs != 0) { 2781 (void) printf("\nsockaddrs: "); 2782 bprintf(stdout, addrs, addrnames); 2783 (void) putchar('\n'); 2784 maxptr = cp + msglen; 2785 for (i = 1; i != 0 && cp + sizeof (*sa) <= maxptr; i <<= 1) { 2786 if (i & addrs) { 2787 /* LINTED */ 2788 sa = (const struct sockaddr *)cp; 2789 (void) printf(" %s", routename(sa)); 2790 ADVANCE(cp, sa); 2791 } 2792 } 2793 if (i != 0) 2794 msglen = 0; 2795 else 2796 msglen = maxptr - cp; 2797 } 2798 pmsg_secattr(cp, msglen, "secattr: "); 2799 (void) putchar('\n'); 2800 (void) fflush(stdout); 2801 } 2802 2803 void 2804 bprintf(FILE *fp, int b, char *s) 2805 { 2806 int i; 2807 boolean_t gotsome = B_FALSE; 2808 2809 if (b == 0) 2810 return; 2811 while ((i = *s++) != 0) { 2812 if (b & (1 << (i - 1))) { 2813 if (!gotsome) 2814 i = '<'; 2815 else 2816 i = ','; 2817 (void) putc(i, fp); 2818 gotsome = B_TRUE; 2819 for (; (i = *s) > ' '; s++) 2820 (void) putc(i, fp); 2821 } else { 2822 while (*s > ' ') 2823 s++; 2824 } 2825 } 2826 if (gotsome) 2827 (void) putc('>', fp); 2828 } 2829 2830 int 2831 keyword(const char *cp) 2832 { 2833 struct keytab *kt = keywords; 2834 2835 while (kt->kt_cp && strcmp(kt->kt_cp, cp)) 2836 kt++; 2837 return (kt->kt_i); 2838 } 2839 2840 void 2841 sodump(su_t *su, char *which) 2842 { 2843 static char obuf[INET6_ADDRSTRLEN]; 2844 2845 switch (su->sa.sa_family) { 2846 case AF_LINK: 2847 (void) printf("%s: link %s; ", 2848 which, link_ntoa(&su->sdl)); 2849 break; 2850 case AF_INET: 2851 (void) printf("%s: inet %s; ", 2852 which, inet_ntoa(su->sin.sin_addr)); 2853 break; 2854 case AF_INET6: 2855 if (inet_ntop(AF_INET6, (void *)&su->sin6.sin6_addr, obuf, 2856 INET6_ADDRSTRLEN) != NULL) { 2857 (void) printf("%s: inet6 %s; ", which, obuf); 2858 break; 2859 } 2860 /* FALLTHROUGH */ 2861 default: 2862 quit(gettext("Internal Error"), EINVAL); 2863 /* NOTREACHED */ 2864 } 2865 (void) fflush(stdout); 2866 } 2867 2868 /* States */ 2869 #define VIRGIN 0 2870 #define GOTONE 1 2871 #define GOTTWO 2 2872 #define RESET 3 2873 /* Inputs */ 2874 #define DIGIT (4*0) 2875 #define END (4*1) 2876 #define DELIM (4*2) 2877 #define LETTER (4*3) 2878 2879 void 2880 sockaddr(char *addr, struct sockaddr *sa) 2881 { 2882 char *cp = (char *)sa; 2883 int size = salen(sa); 2884 char *cplim = cp + size; 2885 int byte = 0, state = VIRGIN, new; 2886 2887 (void) memset(cp, 0, size); 2888 cp++; 2889 do { 2890 if ((*addr >= '0') && (*addr <= '9')) { 2891 new = *addr - '0'; 2892 } else if ((*addr >= 'a') && (*addr <= 'f')) { 2893 new = *addr - 'a' + 10; 2894 } else if ((*addr >= 'A') && (*addr <= 'F')) { 2895 new = *addr - 'A' + 10; 2896 } else if (*addr == 0) { 2897 state |= END; 2898 } else { 2899 state |= DELIM; 2900 } 2901 addr++; 2902 switch (state /* | INPUT */) { 2903 case GOTTWO | DIGIT: 2904 *cp++ = byte; 2905 /* FALLTHROUGH */ 2906 case VIRGIN | DIGIT: 2907 state = GOTONE; byte = new; continue; 2908 case GOTONE | DIGIT: 2909 state = GOTTWO; byte = new + (byte << 4); continue; 2910 default: /* | DELIM */ 2911 state = VIRGIN; *cp++ = byte; byte = 0; continue; 2912 case GOTONE | END: 2913 case GOTTWO | END: 2914 *cp++ = byte; 2915 /* FALLTHROUGH */ 2916 case VIRGIN | END: 2917 break; 2918 } 2919 break; 2920 } while (cp < cplim); 2921 } 2922 2923 int 2924 salen(const struct sockaddr *sa) 2925 { 2926 switch (sa->sa_family) { 2927 case AF_INET: 2928 return (sizeof (struct sockaddr_in)); 2929 case AF_LINK: 2930 return (sizeof (struct sockaddr_dl)); 2931 case AF_INET6: 2932 return (sizeof (struct sockaddr_in6)); 2933 default: 2934 return (sizeof (struct sockaddr)); 2935 } 2936 } 2937 2938 void 2939 link_addr(const char *addr, struct sockaddr_dl *sdl) 2940 { 2941 char *cp = sdl->sdl_data; 2942 char *cplim = sizeof (struct sockaddr_dl) + (char *)sdl; 2943 int byte = 0, state = VIRGIN, new; 2944 2945 (void) memset(sdl, 0, sizeof (struct sockaddr_dl)); 2946 sdl->sdl_family = AF_LINK; 2947 do { 2948 state &= ~LETTER; 2949 if ((*addr >= '0') && (*addr <= '9')) { 2950 new = *addr - '0'; 2951 } else if ((*addr >= 'a') && (*addr <= 'f')) { 2952 new = *addr - 'a' + 10; 2953 } else if ((*addr >= 'A') && (*addr <= 'F')) { 2954 new = *addr - 'A' + 10; 2955 } else if (*addr == 0) { 2956 state |= END; 2957 } else if (state == VIRGIN && 2958 (((*addr >= 'A') && (*addr <= 'Z')) || 2959 ((*addr >= 'a') && (*addr <= 'z')))) { 2960 state |= LETTER; 2961 } else { 2962 state |= DELIM; 2963 } 2964 addr++; 2965 switch (state /* | INPUT */) { 2966 case VIRGIN | DIGIT: 2967 case VIRGIN | LETTER: 2968 *cp++ = addr[-1]; 2969 continue; 2970 case VIRGIN | DELIM: 2971 state = RESET; 2972 sdl->sdl_nlen = cp - sdl->sdl_data; 2973 continue; 2974 case GOTTWO | DIGIT: 2975 *cp++ = byte; 2976 /* FALLTHROUGH */ 2977 case RESET | DIGIT: 2978 state = GOTONE; 2979 byte = new; 2980 continue; 2981 case GOTONE | DIGIT: 2982 state = GOTTWO; 2983 byte = new + (byte << 4); 2984 continue; 2985 default: /* | DELIM */ 2986 state = RESET; 2987 *cp++ = byte; 2988 byte = 0; 2989 continue; 2990 case GOTONE | END: 2991 case GOTTWO | END: 2992 *cp++ = byte; 2993 /* FALLTHROUGH */ 2994 case RESET | END: 2995 break; 2996 } 2997 break; 2998 } while (cp < cplim); 2999 sdl->sdl_alen = cp - LLADDR(sdl); 3000 } 3001 3002 static char hexlist[] = "0123456789abcdef"; 3003 3004 char * 3005 link_ntoa(const struct sockaddr_dl *sdl) 3006 { 3007 static char obuf[64]; 3008 char *out = obuf; 3009 int i; 3010 uchar_t *in = (uchar_t *)LLADDR(sdl); 3011 uchar_t *inlim = in + sdl->sdl_alen; 3012 boolean_t firsttime = B_TRUE; 3013 3014 if (sdl->sdl_nlen) { 3015 (void) memcpy(obuf, sdl->sdl_data, sdl->sdl_nlen); 3016 out += sdl->sdl_nlen; 3017 if (sdl->sdl_alen) 3018 *out++ = ':'; 3019 } 3020 while (in < inlim) { 3021 if (firsttime) 3022 firsttime = B_FALSE; 3023 else 3024 *out++ = '.'; 3025 i = *in++; 3026 if (i > 0xf) { 3027 out[1] = hexlist[i & 0xf]; 3028 i >>= 4; 3029 out[0] = hexlist[i]; 3030 out += 2; 3031 } else { 3032 *out++ = hexlist[i]; 3033 } 3034 } 3035 *out = 0; 3036 return (obuf); 3037 } 3038 3039 static mib_item_t * 3040 mibget(int sd) 3041 { 3042 intmax_t buf[512 / sizeof (intmax_t)]; 3043 int flags; 3044 int i, j, getcode; 3045 struct strbuf ctlbuf, databuf; 3046 struct T_optmgmt_req *tor = (struct T_optmgmt_req *)buf; 3047 struct T_optmgmt_ack *toa = (struct T_optmgmt_ack *)buf; 3048 struct T_error_ack *tea = (struct T_error_ack *)buf; 3049 struct opthdr *req; 3050 mib_item_t *first_item = NULL; 3051 mib_item_t *last_item = NULL; 3052 mib_item_t *temp; 3053 3054 tor->PRIM_type = T_SVR4_OPTMGMT_REQ; 3055 tor->OPT_offset = sizeof (struct T_optmgmt_req); 3056 tor->OPT_length = sizeof (struct opthdr); 3057 tor->MGMT_flags = T_CURRENT; 3058 req = (struct opthdr *)&tor[1]; 3059 req->level = MIB2_IP; /* any MIB2_xxx value ok here */ 3060 req->name = 0; 3061 req->len = 0; 3062 3063 ctlbuf.buf = (char *)buf; 3064 ctlbuf.len = tor->OPT_length + tor->OPT_offset; 3065 flags = 0; 3066 if (putmsg(sd, &ctlbuf, NULL, flags) < 0) { 3067 perror("mibget: putmsg (ctl)"); 3068 return (NULL); 3069 } 3070 /* 3071 * each reply consists of a ctl part for one fixed structure 3072 * or table, as defined in mib2.h. The format is a T_OPTMGMT_ACK, 3073 * containing an opthdr structure. level/name identify the entry, 3074 * len is the size of the data part of the message. 3075 */ 3076 req = (struct opthdr *)&toa[1]; 3077 ctlbuf.maxlen = sizeof (buf); 3078 for (j = 1; ; j++) { 3079 flags = 0; 3080 getcode = getmsg(sd, &ctlbuf, NULL, &flags); 3081 if (getcode < 0) { 3082 perror("mibget: getmsg (ctl)"); 3083 if (verbose) { 3084 (void) fprintf(stderr, 3085 "# level name len\n"); 3086 i = 0; 3087 for (last_item = first_item; last_item != NULL; 3088 last_item = last_item->next_item) { 3089 (void) printf("%d %4ld %5ld %ld\n", 3090 ++i, last_item->group, 3091 last_item->mib_id, 3092 last_item->length); 3093 } 3094 } 3095 break; 3096 } 3097 if (getcode == 0 && 3098 ctlbuf.len >= sizeof (struct T_optmgmt_ack) && 3099 toa->PRIM_type == T_OPTMGMT_ACK && 3100 toa->MGMT_flags == T_SUCCESS && 3101 req->len == 0) { 3102 if (verbose) { 3103 (void) printf("mibget getmsg() %d returned EOD " 3104 "(level %lu, name %lu)\n", j, req->level, 3105 req->name); 3106 } 3107 return (first_item); /* this is EOD msg */ 3108 } 3109 3110 if (ctlbuf.len >= sizeof (struct T_error_ack) && 3111 tea->PRIM_type == T_ERROR_ACK) { 3112 (void) fprintf(stderr, gettext("mibget %d gives " 3113 "T_ERROR_ACK: TLI_error = 0x%lx, UNIX_error = " 3114 "0x%lx\n"), j, tea->TLI_error, tea->UNIX_error); 3115 errno = (tea->TLI_error == TSYSERR) 3116 ? tea->UNIX_error : EPROTO; 3117 break; 3118 } 3119 3120 if (getcode != MOREDATA || 3121 ctlbuf.len < sizeof (struct T_optmgmt_ack) || 3122 toa->PRIM_type != T_OPTMGMT_ACK || 3123 toa->MGMT_flags != T_SUCCESS) { 3124 (void) printf("mibget getmsg(ctl) %d returned %d, " 3125 "ctlbuf.len = %d, PRIM_type = %ld\n", 3126 j, getcode, ctlbuf.len, toa->PRIM_type); 3127 if (toa->PRIM_type == T_OPTMGMT_ACK) { 3128 (void) printf("T_OPTMGMT_ACK: " 3129 "MGMT_flags = 0x%lx, req->len = %ld\n", 3130 toa->MGMT_flags, req->len); 3131 } 3132 errno = ENOMSG; 3133 break; 3134 } 3135 3136 temp = malloc(sizeof (mib_item_t)); 3137 if (temp == NULL) { 3138 perror("mibget: malloc"); 3139 break; 3140 } 3141 if (last_item != NULL) 3142 last_item->next_item = temp; 3143 else 3144 first_item = temp; 3145 last_item = temp; 3146 last_item->next_item = NULL; 3147 last_item->group = req->level; 3148 last_item->mib_id = req->name; 3149 last_item->length = req->len; 3150 last_item->valp = malloc(req->len); 3151 if (verbose) { 3152 (void) printf("msg %d: group = %4ld mib_id = %5ld " 3153 "length = %ld\n", 3154 j, last_item->group, last_item->mib_id, 3155 last_item->length); 3156 } 3157 3158 databuf.maxlen = last_item->length; 3159 databuf.buf = (char *)last_item->valp; 3160 databuf.len = 0; 3161 flags = 0; 3162 getcode = getmsg(sd, NULL, &databuf, &flags); 3163 if (getcode < 0) { 3164 perror("mibget: getmsg (data)"); 3165 break; 3166 } else if (getcode != 0) { 3167 (void) printf("mibget getmsg(data) returned %d, " 3168 "databuf.maxlen = %d, databuf.len = %d\n", 3169 getcode, databuf.maxlen, databuf.len); 3170 break; 3171 } 3172 } 3173 3174 /* 3175 * On error, free all the allocated mib_item_t objects. 3176 */ 3177 while (first_item != NULL) { 3178 last_item = first_item; 3179 first_item = first_item->next_item; 3180 free(last_item); 3181 } 3182 return (NULL); 3183 } 3184 3185 /* 3186 * print label security attributes for gateways. 3187 */ 3188 static void 3189 pmsg_secattr(const char *sptr, size_t msglen, const char *labelstr) 3190 { 3191 rtm_ext_t rtm_ext; 3192 tsol_rtsecattr_t sp; 3193 struct rtsa_s *rtsa = &sp.rtsa_attr[0]; 3194 const char *endptr; 3195 char buf[256]; 3196 int i; 3197 3198 if (!is_system_labeled()) 3199 return; 3200 3201 endptr = sptr + msglen; 3202 3203 for (;;) { 3204 if (sptr + sizeof (rtm_ext_t) + sizeof (sp) > endptr) 3205 return; 3206 3207 bcopy(sptr, &rtm_ext, sizeof (rtm_ext)); 3208 sptr += sizeof (rtm_ext); 3209 if (rtm_ext.rtmex_type == RTMEX_GATEWAY_SECATTR) 3210 break; 3211 sptr += rtm_ext.rtmex_len; 3212 } 3213 3214 /* bail if this entry is corrupt or overruns buffer length */ 3215 if (rtm_ext.rtmex_len < sizeof (sp) || 3216 sptr + rtm_ext.rtmex_len > endptr) 3217 return; 3218 3219 /* run up just to the end of this extension */ 3220 endptr = sptr + rtm_ext.rtmex_len; 3221 3222 bcopy(sptr, &sp, sizeof (sp)); 3223 sptr += sizeof (sp); 3224 3225 if (sptr + (sp.rtsa_cnt - 1) * sizeof (*rtsa) != endptr) 3226 return; 3227 3228 for (i = 0; i < sp.rtsa_cnt; i++) { 3229 if (i > 0) { 3230 /* first element is part of sp initalized above */ 3231 bcopy(sptr, rtsa, sizeof (*rtsa)); 3232 sptr += sizeof (*rtsa); 3233 } 3234 (void) printf("\n%s%s", labelstr, rtsa_to_str(rtsa, buf, 3235 sizeof (buf))); 3236 } 3237 } 3238