1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 /* 27 * Copyright (c) 1990 Mentat Inc. 28 * netstat.c 2.2, last change 9/9/91 29 * MROUTING Revision 3.5 30 */ 31 32 #pragma ident "%Z%%M% %I% %E% SMI" 33 34 /* 35 * simple netstat based on snmp/mib-2 interface to the TCP/IP stack 36 * 37 * NOTES: 38 * 1. A comment "LINTED: (note 1)" appears before certain lines where 39 * lint would have complained, "pointer cast may result in improper 40 * alignment". These are lines where lint had suspected potential 41 * improper alignment of a data structure; in each such situation 42 * we have relied on the kernel guaranteeing proper alignment. 43 * 2. Some 'for' loops have been commented as "'for' loop 1", etc 44 * because they have 'continue' or 'break' statements in their 45 * bodies. 'continue' statements have been used inside some loops 46 * where avoiding them would have led to deep levels of indentation. 47 * 48 * TODO: 49 * Add ability to request subsets from kernel (with level = MIB2_IP; 50 * name = 0 meaning everything for compatibility) 51 */ 52 53 #include <stdio.h> 54 #include <stdlib.h> 55 #include <stdarg.h> 56 #include <unistd.h> 57 #include <strings.h> 58 #include <string.h> 59 #include <errno.h> 60 #include <ctype.h> 61 #include <kstat.h> 62 #include <assert.h> 63 64 #include <sys/types.h> 65 #include <sys/stream.h> 66 #include <stropts.h> 67 #include <sys/strstat.h> 68 #include <sys/tihdr.h> 69 70 #include <sys/socket.h> 71 #include <sys/sockio.h> 72 #include <netinet/in.h> 73 #include <net/if.h> 74 #include <net/route.h> 75 76 #include <inet/mib2.h> 77 #include <inet/ip.h> 78 #include <inet/arp.h> 79 #include <inet/tcp.h> 80 #include <netinet/igmp_var.h> 81 #include <netinet/ip_mroute.h> 82 83 #include <arpa/inet.h> 84 #include <netdb.h> 85 #include <fcntl.h> 86 #include <sys/systeminfo.h> 87 #include <arpa/inet.h> 88 89 #include <netinet/dhcp.h> 90 #include <dhcpagent_ipc.h> 91 #include <dhcpagent_util.h> 92 #include <compat.h> 93 94 #include <libtsnet.h> 95 #include <tsol/label.h> 96 97 extern void unixpr(kstat_ctl_t *kc); 98 99 #define STR_EXPAND 4 100 101 #define V4MASK_TO_V6(v4, v6) ((v6)._S6_un._S6_u32[0] = 0xfffffffful, \ 102 (v6)._S6_un._S6_u32[1] = 0xfffffffful, \ 103 (v6)._S6_un._S6_u32[2] = 0xfffffffful, \ 104 (v6)._S6_un._S6_u32[3] = (v4)) 105 106 #define IN6_IS_V4MASK(v6) ((v6)._S6_un._S6_u32[0] == 0xfffffffful && \ 107 (v6)._S6_un._S6_u32[1] == 0xfffffffful && \ 108 (v6)._S6_un._S6_u32[2] == 0xfffffffful) 109 110 /* 111 * This is used as a cushion in the buffer allocation directed by SIOCGLIFNUM. 112 * Because there's no locking between SIOCGLIFNUM and SIOCGLIFCONF, it's 113 * possible for an administrator to plumb new interfaces between those two 114 * calls, resulting in the failure of the latter. This addition makes that 115 * less likely. 116 */ 117 #define LIFN_GUARD_VALUE 10 118 119 typedef struct mib_item_s { 120 struct mib_item_s *next_item; 121 int group; 122 int mib_id; 123 int length; 124 void *valp; 125 } mib_item_t; 126 127 struct ifstat { 128 uint64_t ipackets; 129 uint64_t ierrors; 130 uint64_t opackets; 131 uint64_t oerrors; 132 uint64_t collisions; 133 }; 134 135 struct iflist { 136 struct iflist *next_if; 137 char ifname[LIFNAMSIZ]; 138 struct ifstat tot; 139 }; 140 141 static mib_item_t *mibget(int sd); 142 static void mibfree(mib_item_t *firstitem); 143 static int mibopen(void); 144 static void mib_get_constants(mib_item_t *item); 145 static mib_item_t *mib_item_dup(mib_item_t *item); 146 static mib_item_t *mib_item_diff(mib_item_t *item1, 147 mib_item_t *item2); 148 static void mib_item_destroy(mib_item_t **item); 149 150 static boolean_t octetstrmatch(const Octet_t *a, const Octet_t *b); 151 static char *octetstr(const Octet_t *op, int code, 152 char *dst, uint_t dstlen); 153 static char *pr_addr(uint_t addr, 154 char *dst, uint_t dstlen); 155 static char *pr_addrnz(ipaddr_t addr, char *dst, uint_t dstlen); 156 static char *pr_addr6(const in6_addr_t *addr, 157 char *dst, uint_t dstlen); 158 static char *pr_mask(uint_t addr, 159 char *dst, uint_t dstlen); 160 static char *pr_prefix6(const struct in6_addr *addr, 161 uint_t prefixlen, char *dst, uint_t dstlen); 162 static char *pr_ap(uint_t addr, uint_t port, 163 char *proto, char *dst, uint_t dstlen); 164 static char *pr_ap6(const in6_addr_t *addr, uint_t port, 165 char *proto, char *dst, uint_t dstlen); 166 static char *pr_net(uint_t addr, uint_t mask, 167 char *dst, uint_t dstlen); 168 static char *pr_netaddr(uint_t addr, uint_t mask, 169 char *dst, uint_t dstlen); 170 static char *fmodestr(uint_t fmode); 171 static char *portname(uint_t port, char *proto, 172 char *dst, uint_t dstlen); 173 174 static const char *mitcp_state(int code, 175 const mib2_transportMLPEntry_t *attr); 176 static const char *miudp_state(int code, 177 const mib2_transportMLPEntry_t *attr); 178 179 static void stat_report(mib_item_t *item); 180 static void mrt_stat_report(mib_item_t *item); 181 static void arp_report(mib_item_t *item); 182 static void ndp_report(mib_item_t *item); 183 static void mrt_report(mib_item_t *item); 184 static void if_stat_total(struct ifstat *oldstats, 185 struct ifstat *newstats, struct ifstat *sumstats); 186 static void if_report(mib_item_t *item, char *ifname, 187 int Iflag_only, boolean_t once_only); 188 static void if_report_ip4(mib2_ipAddrEntry_t *ap, 189 char ifname[], char logintname[], 190 struct ifstat *statptr, boolean_t ksp_not_null); 191 static void if_report_ip6(mib2_ipv6AddrEntry_t *ap6, 192 char ifname[], char logintname[], 193 struct ifstat *statptr, boolean_t ksp_not_null); 194 static void ire_report(const mib_item_t *item); 195 static void tcp_report(const mib_item_t *item); 196 static void udp_report(const mib_item_t *item); 197 static void group_report(mib_item_t *item); 198 static void print_ip_stats(mib2_ip_t *ip); 199 static void print_icmp_stats(mib2_icmp_t *icmp); 200 static void print_ip6_stats(mib2_ipv6IfStatsEntry_t *ip6); 201 static void print_icmp6_stats(mib2_ipv6IfIcmpEntry_t *icmp6); 202 static void print_sctp_stats(mib2_sctp_t *tcp); 203 static void print_tcp_stats(mib2_tcp_t *tcp); 204 static void print_udp_stats(mib2_udp_t *udp); 205 static void print_rawip_stats(mib2_rawip_t *rawip); 206 static void print_igmp_stats(struct igmpstat *igps); 207 static void print_mrt_stats(struct mrtstat *mrts); 208 static void sctp_report(const mib_item_t *item); 209 static void sum_ip6_stats(mib2_ipv6IfStatsEntry_t *ip6, 210 mib2_ipv6IfStatsEntry_t *sum6); 211 static void sum_icmp6_stats(mib2_ipv6IfIcmpEntry_t *icmp6, 212 mib2_ipv6IfIcmpEntry_t *sum6); 213 static void m_report(void); 214 static void dhcp_report(char *); 215 216 void fail(int, char *, ...); 217 static uint64_t kstat_named_value(kstat_t *, char *); 218 static kid_t safe_kstat_read(kstat_ctl_t *, kstat_t *, void *); 219 static int isnum(char *); 220 static char *plural(int n); 221 static char *pluraly(int n); 222 static char *plurales(int n); 223 static void process_filter(char *arg); 224 static boolean_t family_selected(int family); 225 226 static void usage(char *); 227 static void fatal(int errcode, char *str1, ...); 228 229 #define PLURAL(n) plural((int)n) 230 #define PLURALY(n) pluraly((int)n) 231 #define PLURALES(n) plurales((int)n) 232 #define IFLAGMOD(flg, val1, val2) if (flg == val1) flg = val2 233 #define MDIFF(diff, elem2, elem1, member) (diff)->member = \ 234 (elem2)->member - (elem1)->member 235 236 237 static boolean_t Aflag = B_FALSE; /* All sockets/ifs/rtng-tbls */ 238 static boolean_t Dflag = B_FALSE; /* Debug Info */ 239 static boolean_t Iflag = B_FALSE; /* IP Traffic Interfaces */ 240 static boolean_t Mflag = B_FALSE; /* STREAMS Memory Statistics */ 241 static boolean_t Nflag = B_FALSE; /* Numeric Network Addresses */ 242 static boolean_t Rflag = B_FALSE; /* Routing Tables */ 243 static boolean_t RSECflag = B_FALSE; /* Security attributes */ 244 static boolean_t Sflag = B_FALSE; /* Per-protocol Statistics */ 245 static boolean_t Vflag = B_FALSE; /* Verbose */ 246 static boolean_t Pflag = B_FALSE; /* Net to Media Tables */ 247 static boolean_t Gflag = B_FALSE; /* Multicast group membership */ 248 static boolean_t MMflag = B_FALSE; /* Multicast routing table */ 249 static boolean_t DHCPflag = B_FALSE; /* DHCP statistics */ 250 251 static int v4compat = 0; /* Compatible printing format for status */ 252 253 static int proto = IPPROTO_MAX; /* all protocols */ 254 kstat_ctl_t *kc = NULL; 255 256 /* 257 * Sizes of data structures extracted from the base mib. 258 * This allows the size of the tables entries to grow while preserving 259 * binary compatibility. 260 */ 261 static int ipAddrEntrySize; 262 static int ipRouteEntrySize; 263 static int ipNetToMediaEntrySize; 264 static int ipMemberEntrySize; 265 static int ipGroupSourceEntrySize; 266 static int ipRouteAttributeSize; 267 static int vifctlSize; 268 static int mfcctlSize; 269 270 static int ipv6IfStatsEntrySize; 271 static int ipv6IfIcmpEntrySize; 272 static int ipv6AddrEntrySize; 273 static int ipv6RouteEntrySize; 274 static int ipv6NetToMediaEntrySize; 275 static int ipv6MemberEntrySize; 276 static int ipv6GroupSourceEntrySize; 277 278 static int transportMLPSize; 279 static int tcpConnEntrySize; 280 static int tcp6ConnEntrySize; 281 static int udpEntrySize; 282 static int udp6EntrySize; 283 static int sctpEntrySize; 284 static int sctpLocalEntrySize; 285 static int sctpRemoteEntrySize; 286 287 #define protocol_selected(p) (proto == IPPROTO_MAX || proto == (p)) 288 289 /* Machinery used for -f (filter) option */ 290 enum { FK_AF = 0, FK_OUTIF, FK_DST, FK_FLAGS, NFILTERKEYS }; 291 292 static const char *filter_keys[NFILTERKEYS] = { 293 "af", "outif", "dst", "flags" 294 }; 295 296 /* Flags on routes */ 297 #define FLF_A 0x00000001 298 #define FLF_B 0x00000002 299 #define FLF_D 0x00000004 300 #define FLF_G 0x00000008 301 #define FLF_H 0x00000010 302 #define FLF_L 0x00000020 303 #define FLF_U 0x00000040 304 #define FLF_M 0x00000080 305 #define FLF_S 0x00000100 306 static const char flag_list[] = "ABDGHLUMS"; 307 308 typedef struct filter_rule filter_t; 309 310 struct filter_rule { 311 filter_t *f_next; 312 union { 313 int f_family; 314 const char *f_ifname; 315 struct { 316 struct hostent *f_address; 317 in6_addr_t f_mask; 318 } a; 319 struct { 320 uint_t f_flagset; 321 uint_t f_flagclear; 322 } f; 323 } u; 324 }; 325 326 /* 327 * The user-specified filters are linked into lists separated by 328 * keyword (type of filter). Thus, the matching algorithm is: 329 * For each non-empty filter list 330 * If no filters in the list match 331 * then stop here; route doesn't match 332 * If loop above completes, then route does match and will be 333 * displayed. 334 */ 335 static filter_t *filters[NFILTERKEYS]; 336 337 int 338 main(int argc, char **argv) 339 { 340 char *name; 341 mib_item_t *item = NULL; 342 mib_item_t *previtem = NULL; 343 int sd = -1; 344 char *ifname = NULL; 345 int interval = 0; /* Single time by default */ 346 int count = -1; /* Forever */ 347 int c; 348 int d; 349 /* 350 * Possible values of 'Iflag_only': 351 * -1, no feature-flags; 352 * 0, IFlag and other feature-flags enabled 353 * 1, IFlag is the only feature-flag enabled 354 * : trinary variable, modified using IFLAGMOD() 355 */ 356 int Iflag_only = -1; 357 boolean_t once_only = B_FALSE; /* '-i' with count > 1 */ 358 extern char *optarg; 359 extern int optind; 360 char *default_ip_str = NULL; 361 362 name = argv[0]; 363 364 v4compat = get_compat_flag(&default_ip_str); 365 if (v4compat == DEFAULT_PROT_BAD_VALUE) 366 fatal(2, "%s: %s: Bad value for %s in %s\n", name, 367 default_ip_str, DEFAULT_IP, INET_DEFAULT_FILE); 368 free(default_ip_str); 369 370 while ((c = getopt(argc, argv, "adimnrspMgvf:P:I:DR")) != -1) { 371 switch ((char)c) { 372 case 'a': /* all connections */ 373 Aflag = B_TRUE; 374 break; 375 376 case 'd': /* turn on debugging */ 377 Dflag = B_TRUE; 378 break; 379 380 case 'i': /* interface (ill/ipif report) */ 381 Iflag = B_TRUE; 382 IFLAGMOD(Iflag_only, -1, 1); /* '-i' exists */ 383 break; 384 385 case 'm': /* streams msg report */ 386 Mflag = B_TRUE; 387 IFLAGMOD(Iflag_only, 1, 0); /* see macro def'n */ 388 break; 389 390 case 'n': /* numeric format */ 391 Nflag = B_TRUE; 392 break; 393 394 case 'r': /* route tables */ 395 Rflag = B_TRUE; 396 IFLAGMOD(Iflag_only, 1, 0); /* see macro def'n */ 397 break; 398 399 case 'R': /* security attributes */ 400 RSECflag = B_TRUE; 401 IFLAGMOD(Iflag_only, 1, 0); /* see macro def'n */ 402 break; 403 404 case 's': /* per-protocol statistics */ 405 Sflag = B_TRUE; 406 IFLAGMOD(Iflag_only, 1, 0); /* see macro def'n */ 407 break; 408 409 case 'p': /* arp/ndp table */ 410 Pflag = B_TRUE; 411 IFLAGMOD(Iflag_only, 1, 0); /* see macro def'n */ 412 break; 413 414 case 'M': /* multicast routing tables */ 415 MMflag = B_TRUE; 416 IFLAGMOD(Iflag_only, 1, 0); /* see macro def'n */ 417 break; 418 419 case 'g': /* multicast group membership */ 420 Gflag = B_TRUE; 421 IFLAGMOD(Iflag_only, 1, 0); /* see macro def'n */ 422 break; 423 424 case 'v': /* verbose output format */ 425 Vflag = B_TRUE; 426 IFLAGMOD(Iflag_only, 1, 0); /* see macro def'n */ 427 break; 428 429 case 'f': 430 process_filter(optarg); 431 break; 432 433 case 'P': 434 if (strcmp(optarg, "ip") == 0) { 435 proto = IPPROTO_IP; 436 } else if (strcmp(optarg, "ipv6") == 0 || 437 strcmp(optarg, "ip6") == 0) { 438 v4compat = 0; /* Overridden */ 439 proto = IPPROTO_IPV6; 440 } else if (strcmp(optarg, "icmp") == 0) { 441 proto = IPPROTO_ICMP; 442 } else if (strcmp(optarg, "icmpv6") == 0 || 443 strcmp(optarg, "icmp6") == 0) { 444 v4compat = 0; /* Overridden */ 445 proto = IPPROTO_ICMPV6; 446 } else if (strcmp(optarg, "igmp") == 0) { 447 proto = IPPROTO_IGMP; 448 } else if (strcmp(optarg, "udp") == 0) { 449 proto = IPPROTO_UDP; 450 } else if (strcmp(optarg, "tcp") == 0) { 451 proto = IPPROTO_TCP; 452 } else if (strcmp(optarg, "sctp") == 0) { 453 proto = IPPROTO_SCTP; 454 } else if (strcmp(optarg, "raw") == 0 || 455 strcmp(optarg, "rawip") == 0) { 456 proto = IPPROTO_RAW; 457 } else { 458 fatal(1, "%s: unknown protocol.\n", optarg); 459 } 460 break; 461 462 case 'I': 463 ifname = optarg; 464 Iflag = B_TRUE; 465 IFLAGMOD(Iflag_only, -1, 1); /* see macro def'n */ 466 break; 467 468 case 'D': 469 DHCPflag = B_TRUE; 470 Iflag_only = 0; 471 break; 472 473 case '?': 474 default: 475 usage(name); 476 } 477 } 478 479 /* 480 * Make sure -R option is set only on a labeled system. 481 */ 482 if (RSECflag && !is_system_labeled()) { 483 (void) fprintf(stderr, "-R set but labeling is not enabled\n"); 484 usage(name); 485 } 486 487 /* 488 * Handle other arguments: find interval, count; the 489 * flags that accept 'interval' and 'count' are OR'd 490 * in the outermost 'if'; more flags may be added as 491 * required 492 */ 493 if (Iflag || Sflag || Mflag) { 494 for (d = optind; d < argc; d++) { 495 if (isnum(argv[d])) { 496 interval = atoi(argv[d]); 497 if (d + 1 < argc && 498 isnum(argv[d + 1])) { 499 count = atoi(argv[d + 1]); 500 optind++; 501 } 502 optind++; 503 if (interval == 0 || count == 0) 504 usage(name); 505 break; 506 } 507 } 508 } 509 if (optind < argc) { 510 if (Iflag && isnum(argv[optind])) { 511 count = atoi(argv[optind]); 512 if (count == 0) 513 usage(name); 514 optind++; 515 } 516 } 517 if (optind < argc) { 518 (void) fprintf(stderr, 519 "%s: extra arguments\n", name); 520 usage(name); 521 } 522 if (interval) 523 setbuf(stdout, NULL); 524 525 if (DHCPflag) { 526 dhcp_report(Iflag ? ifname : NULL); 527 exit(0); 528 } 529 530 /* Get data structures: priming before iteration */ 531 if (family_selected(AF_INET) || family_selected(AF_INET6)) { 532 sd = mibopen(); 533 if (sd == -1) 534 fatal(1, "can't open mib stream\n"); 535 if ((item = mibget(sd)) == NULL) { 536 (void) close(sd); 537 fatal(1, "mibget() failed\n"); 538 } 539 /* Extract constant sizes - need do once only */ 540 mib_get_constants(item); 541 } 542 if ((kc = kstat_open()) == NULL) { 543 mibfree(item); 544 (void) close(sd); 545 fail(1, "kstat_open(): can't open /dev/kstat"); 546 } 547 548 if (interval <= 0) { 549 count = 1; 550 once_only = B_TRUE; 551 } 552 /* 'for' loop 1: */ 553 for (;;) { 554 mib_item_t *curritem = NULL; /* only for -[M]s */ 555 556 /* netstat: AF_INET[6] behaviour */ 557 if (family_selected(AF_INET) || family_selected(AF_INET6)) { 558 if (Sflag) { 559 curritem = mib_item_diff(previtem, item); 560 if (curritem == NULL) 561 fatal(1, "can't process mib data, " 562 "out of memory\n"); 563 mib_item_destroy(&previtem); 564 } 565 566 if (!(Iflag || Rflag || Sflag || Mflag || 567 MMflag || Pflag || Gflag || DHCPflag)) { 568 if (protocol_selected(IPPROTO_UDP)) 569 udp_report(item); 570 if (protocol_selected(IPPROTO_TCP)) 571 tcp_report(item); 572 if (protocol_selected(IPPROTO_SCTP)) 573 sctp_report(item); 574 } 575 if (Iflag) 576 if_report(item, ifname, Iflag_only, once_only); 577 if (Mflag) 578 m_report(); 579 if (Rflag) 580 ire_report(item); 581 if (Sflag && MMflag) { 582 mrt_stat_report(curritem); 583 } else { 584 if (Sflag) 585 stat_report(curritem); 586 if (MMflag) 587 mrt_report(item); 588 } 589 if (Gflag) 590 group_report(item); 591 if (Pflag) { 592 if (family_selected(AF_INET)) 593 arp_report(item); 594 if (family_selected(AF_INET6)) 595 ndp_report(item); 596 } 597 mib_item_destroy(&curritem); 598 } 599 600 /* netstat: AF_UNIX behaviour */ 601 if (family_selected(AF_UNIX) && 602 (!(Iflag || Rflag || Sflag || Mflag || 603 MMflag || Pflag || Gflag))) 604 unixpr(kc); 605 (void) kstat_close(kc); 606 607 /* iteration handling code */ 608 if (count > 0 && --count == 0) 609 break; 610 (void) sleep(interval); 611 612 /* re-populating of data structures */ 613 if (family_selected(AF_INET) || family_selected(AF_INET6)) { 614 if (Sflag) { 615 /* previtem is a cut-down list */ 616 previtem = mib_item_dup(item); 617 if (previtem == NULL) 618 fatal(1, "can't process mib data, " 619 "out of memory\n"); 620 } 621 mibfree(item); 622 (void) close(sd); 623 if ((sd = mibopen()) == -1) 624 fatal(1, "can't open mib stream anymore\n"); 625 if ((item = mibget(sd)) == NULL) { 626 (void) close(sd); 627 fatal(1, "mibget() failed\n"); 628 } 629 } 630 if ((kc = kstat_open()) == NULL) 631 fail(1, "kstat_open(): can't open /dev/kstat"); 632 633 } /* 'for' loop 1 ends */ 634 mibfree(item); 635 (void) close(sd); 636 637 return (0); 638 } 639 640 641 static int 642 isnum(char *p) 643 { 644 int len; 645 int i; 646 647 len = strlen(p); 648 for (i = 0; i < len; i++) 649 if (!isdigit(p[i])) 650 return (0); 651 return (1); 652 } 653 654 655 /* --------------------------------- MIBGET -------------------------------- */ 656 657 static mib_item_t * 658 mibget(int sd) 659 { 660 /* 661 * buf is an automatic for this function, so the 662 * compiler has complete control over its alignment; 663 * it is assumed this alignment is satisfactory for 664 * it to be casted to certain other struct pointers 665 * here, such as struct T_optmgmt_ack * . 666 */ 667 uintptr_t buf[512 / sizeof (uintptr_t)]; 668 int flags; 669 int i, j, getcode; 670 struct strbuf ctlbuf, databuf; 671 struct T_optmgmt_req *tor = (struct T_optmgmt_req *)buf; 672 struct T_optmgmt_ack *toa = (struct T_optmgmt_ack *)buf; 673 struct T_error_ack *tea = (struct T_error_ack *)buf; 674 struct opthdr *req; 675 mib_item_t *first_item = NULL; 676 mib_item_t *last_item = NULL; 677 mib_item_t *temp; 678 679 tor->PRIM_type = T_SVR4_OPTMGMT_REQ; 680 tor->OPT_offset = sizeof (struct T_optmgmt_req); 681 tor->OPT_length = sizeof (struct opthdr); 682 tor->MGMT_flags = T_CURRENT; 683 req = (struct opthdr *)&tor[1]; 684 req->level = MIB2_IP; /* any MIB2_xxx value ok here */ 685 req->name = 0; 686 req->len = 0; 687 688 ctlbuf.buf = (char *)buf; 689 ctlbuf.len = tor->OPT_length + tor->OPT_offset; 690 flags = 0; 691 if (putmsg(sd, &ctlbuf, (struct strbuf *)0, flags) == -1) { 692 perror("mibget: putmsg(ctl) failed"); 693 goto error_exit; 694 } 695 696 /* 697 * Each reply consists of a ctl part for one fixed structure 698 * or table, as defined in mib2.h. The format is a T_OPTMGMT_ACK, 699 * containing an opthdr structure. level/name identify the entry, 700 * len is the size of the data part of the message. 701 */ 702 req = (struct opthdr *)&toa[1]; 703 ctlbuf.maxlen = sizeof (buf); 704 j = 1; 705 for (;;) { 706 flags = 0; 707 getcode = getmsg(sd, &ctlbuf, (struct strbuf *)0, &flags); 708 if (getcode == -1) { 709 perror("mibget getmsg(ctl) failed"); 710 if (Dflag) { 711 (void) fputs("# level name len\n", 712 stderr); 713 i = 0; 714 for (last_item = first_item; last_item; 715 last_item = last_item->next_item) 716 (void) printf("%d %4d %5d %d\n", 717 ++i, 718 last_item->group, 719 last_item->mib_id, 720 last_item->length); 721 } 722 goto error_exit; 723 } 724 if (getcode == 0 && 725 ctlbuf.len >= sizeof (struct T_optmgmt_ack) && 726 toa->PRIM_type == T_OPTMGMT_ACK && 727 toa->MGMT_flags == T_SUCCESS && 728 req->len == 0) { 729 if (Dflag) 730 (void) printf("mibget getmsg() %d returned " 731 "EOD (level %ld, name %ld)\n", 732 j, req->level, req->name); 733 return (first_item); /* this is EOD msg */ 734 } 735 736 if (ctlbuf.len >= sizeof (struct T_error_ack) && 737 tea->PRIM_type == T_ERROR_ACK) { 738 (void) fprintf(stderr, 739 "mibget %d gives T_ERROR_ACK: TLI_error = 0x%lx, " 740 "UNIX_error = 0x%lx\n", 741 j, tea->TLI_error, tea->UNIX_error); 742 743 errno = (tea->TLI_error == TSYSERR) ? 744 tea->UNIX_error : EPROTO; 745 goto error_exit; 746 } 747 748 if (getcode != MOREDATA || 749 ctlbuf.len < sizeof (struct T_optmgmt_ack) || 750 toa->PRIM_type != T_OPTMGMT_ACK || 751 toa->MGMT_flags != T_SUCCESS) { 752 (void) printf("mibget getmsg(ctl) %d returned %d, " 753 "ctlbuf.len = %d, PRIM_type = %ld\n", 754 j, getcode, ctlbuf.len, toa->PRIM_type); 755 756 if (toa->PRIM_type == T_OPTMGMT_ACK) 757 (void) printf("T_OPTMGMT_ACK: " 758 "MGMT_flags = 0x%lx, req->len = %ld\n", 759 toa->MGMT_flags, req->len); 760 errno = ENOMSG; 761 goto error_exit; 762 } 763 764 temp = (mib_item_t *)malloc(sizeof (mib_item_t)); 765 if (temp == NULL) { 766 perror("mibget malloc failed"); 767 goto error_exit; 768 } 769 if (last_item != NULL) 770 last_item->next_item = temp; 771 else 772 first_item = temp; 773 last_item = temp; 774 last_item->next_item = NULL; 775 last_item->group = req->level; 776 last_item->mib_id = req->name; 777 last_item->length = req->len; 778 last_item->valp = malloc((int)req->len); 779 if (last_item->valp == NULL) 780 goto error_exit; 781 if (Dflag) 782 (void) printf("msg %d: group = %4d mib_id = %5d" 783 "length = %d\n", 784 j, last_item->group, last_item->mib_id, 785 last_item->length); 786 787 databuf.maxlen = last_item->length; 788 databuf.buf = (char *)last_item->valp; 789 databuf.len = 0; 790 flags = 0; 791 getcode = getmsg(sd, (struct strbuf *)0, &databuf, &flags); 792 if (getcode == -1) { 793 perror("mibget getmsg(data) failed"); 794 goto error_exit; 795 } else if (getcode != 0) { 796 (void) printf("mibget getmsg(data) returned %d, " 797 "databuf.maxlen = %d, databuf.len = %d\n", 798 getcode, databuf.maxlen, databuf.len); 799 goto error_exit; 800 } 801 j++; 802 } 803 /* NOTREACHED */ 804 805 error_exit:; 806 mibfree(first_item); 807 return (NULL); 808 } 809 810 /* 811 * mibfree: frees a linked list of type (mib_item_t *) 812 * returned by mibget(); this is NOT THE SAME AS 813 * mib_item_destroy(), so should be used for objects 814 * returned by mibget() only 815 */ 816 static void 817 mibfree(mib_item_t *firstitem) 818 { 819 mib_item_t *lastitem; 820 821 while (firstitem != NULL) { 822 lastitem = firstitem; 823 firstitem = firstitem->next_item; 824 if (lastitem->valp != NULL) 825 free(lastitem->valp); 826 free(lastitem); 827 } 828 } 829 830 static int 831 mibopen(void) 832 { 833 int sd; 834 835 sd = open("/dev/arp", O_RDWR); 836 if (sd == -1) { 837 perror("arp open"); 838 return (-1); 839 } 840 if (ioctl(sd, I_PUSH, "tcp") == -1) { 841 perror("tcp I_PUSH"); 842 (void) close(sd); 843 return (-1); 844 } 845 if (ioctl(sd, I_PUSH, "udp") == -1) { 846 perror("udp I_PUSH"); 847 (void) close(sd); 848 return (-1); 849 } 850 if (ioctl(sd, I_PUSH, "icmp") == -1) { 851 perror("icmp I_PUSH"); 852 (void) close(sd); 853 return (-1); 854 } 855 return (sd); 856 } 857 858 /* 859 * mib_item_dup: returns a clean mib_item_t * linked 860 * list, so that for every element item->mib_id is 0; 861 * to deallocate this linked list, use mib_item_destroy 862 */ 863 static mib_item_t * 864 mib_item_dup(mib_item_t *item) 865 { 866 int c = 0; 867 mib_item_t *localp; 868 mib_item_t *tempp; 869 870 for (tempp = item; tempp; tempp = tempp->next_item) 871 if (tempp->mib_id == 0) 872 c++; 873 tempp = NULL; 874 875 localp = (mib_item_t *)malloc(c * sizeof (mib_item_t)); 876 if (localp == NULL) 877 return (NULL); 878 c = 0; 879 for (; item; item = item->next_item) { 880 if (item->mib_id == 0) { 881 /* Replicate item in localp */ 882 (localp[c]).next_item = NULL; 883 (localp[c]).group = item->group; 884 (localp[c]).mib_id = item->mib_id; 885 (localp[c]).length = item->length; 886 (localp[c]).valp = (uintptr_t *)malloc( 887 item->length); 888 if ((localp[c]).valp == NULL) { 889 mib_item_destroy(&localp); 890 return (NULL); 891 } 892 (void *) memcpy((localp[c]).valp, 893 item->valp, 894 item->length); 895 tempp = &(localp[c]); 896 if (c > 0) 897 (localp[c - 1]).next_item = tempp; 898 c++; 899 } 900 } 901 return (localp); 902 } 903 904 /* 905 * mib_item_diff: takes two (mib_item_t *) linked lists 906 * item1 and item2 and computes the difference between 907 * differentiable values in item2 against item1 for every 908 * given member of item2; returns an mib_item_t * linked 909 * list of diff's, or a copy of item2 if item1 is NULL; 910 * will return NULL if system out of memory; works only 911 * for item->mib_id == 0 912 */ 913 static mib_item_t * 914 mib_item_diff(mib_item_t *item1, mib_item_t *item2) { 915 int nitems = 0; /* no. of items in item2 */ 916 mib_item_t *tempp2; /* walking copy of item2 */ 917 mib_item_t *tempp1; /* walking copy of item1 */ 918 mib_item_t *diffp; 919 mib_item_t *diffptr; /* walking copy of diffp */ 920 mib_item_t *prevp = NULL; 921 922 if (item1 == NULL) { 923 diffp = mib_item_dup(item2); 924 return (diffp); 925 } 926 927 for (tempp2 = item2; 928 tempp2; 929 tempp2 = tempp2->next_item) { 930 if (tempp2->mib_id == 0) 931 switch (tempp2->group) { 932 /* 933 * upon adding a case here, the same 934 * must also be added in the next 935 * switch statement, alongwith 936 * appropriate code 937 */ 938 case MIB2_IP: 939 case MIB2_IP6: 940 case EXPER_DVMRP: 941 case EXPER_IGMP: 942 case MIB2_ICMP: 943 case MIB2_ICMP6: 944 case MIB2_TCP: 945 case MIB2_UDP: 946 case MIB2_SCTP: 947 case EXPER_RAWIP: 948 nitems++; 949 } 950 } 951 tempp2 = NULL; 952 if (nitems == 0) { 953 diffp = mib_item_dup(item2); 954 return (diffp); 955 } 956 957 diffp = (mib_item_t *)calloc(nitems, sizeof (mib_item_t)); 958 if (diffp == NULL) 959 return (NULL); 960 diffptr = diffp; 961 /* 'for' loop 1: */ 962 for (tempp2 = item2; tempp2 != NULL; tempp2 = tempp2->next_item) { 963 if (tempp2->mib_id != 0) 964 continue; /* 'for' loop 1 */ 965 /* 'for' loop 2: */ 966 for (tempp1 = item1; tempp1 != NULL; 967 tempp1 = tempp1->next_item) { 968 if (!(tempp1->mib_id == 0 && 969 tempp1->group == tempp2->group && 970 tempp1->mib_id == tempp2->mib_id)) 971 continue; /* 'for' loop 2 */ 972 /* found comparable data sets */ 973 if (prevp != NULL) 974 prevp->next_item = diffptr; 975 switch (tempp2->group) { 976 /* 977 * Indenting note: Because of long variable names 978 * in cases MIB2_IP6 and MIB2_ICMP6, their contents 979 * have been indented by one tab space only 980 */ 981 case MIB2_IP: { 982 mib2_ip_t *i2 = (mib2_ip_t *)tempp2->valp; 983 mib2_ip_t *i1 = (mib2_ip_t *)tempp1->valp; 984 mib2_ip_t *d; 985 986 diffptr->group = tempp2->group; 987 diffptr->mib_id = tempp2->mib_id; 988 diffptr->length = tempp2->length; 989 d = (mib2_ip_t *)calloc(tempp2->length, 1); 990 if (d == NULL) 991 goto mibdiff_out_of_memory; 992 diffptr->valp = d; 993 d->ipForwarding = i2->ipForwarding; 994 d->ipDefaultTTL = i2->ipDefaultTTL; 995 MDIFF(d, i2, i1, ipInReceives); 996 MDIFF(d, i2, i1, ipInHdrErrors); 997 MDIFF(d, i2, i1, ipInAddrErrors); 998 MDIFF(d, i2, i1, ipInCksumErrs); 999 MDIFF(d, i2, i1, ipForwDatagrams); 1000 MDIFF(d, i2, i1, ipForwProhibits); 1001 MDIFF(d, i2, i1, ipInUnknownProtos); 1002 MDIFF(d, i2, i1, ipInDiscards); 1003 MDIFF(d, i2, i1, ipInDelivers); 1004 MDIFF(d, i2, i1, ipOutRequests); 1005 MDIFF(d, i2, i1, ipOutDiscards); 1006 MDIFF(d, i2, i1, ipOutNoRoutes); 1007 MDIFF(d, i2, i1, ipReasmTimeout); 1008 MDIFF(d, i2, i1, ipReasmReqds); 1009 MDIFF(d, i2, i1, ipReasmOKs); 1010 MDIFF(d, i2, i1, ipReasmFails); 1011 MDIFF(d, i2, i1, ipReasmDuplicates); 1012 MDIFF(d, i2, i1, ipReasmPartDups); 1013 MDIFF(d, i2, i1, ipFragOKs); 1014 MDIFF(d, i2, i1, ipFragFails); 1015 MDIFF(d, i2, i1, ipFragCreates); 1016 MDIFF(d, i2, i1, ipRoutingDiscards); 1017 MDIFF(d, i2, i1, tcpInErrs); 1018 MDIFF(d, i2, i1, udpNoPorts); 1019 MDIFF(d, i2, i1, udpInCksumErrs); 1020 MDIFF(d, i2, i1, udpInOverflows); 1021 MDIFF(d, i2, i1, rawipInOverflows); 1022 MDIFF(d, i2, i1, ipsecInSucceeded); 1023 MDIFF(d, i2, i1, ipsecInFailed); 1024 MDIFF(d, i2, i1, ipInIPv6); 1025 MDIFF(d, i2, i1, ipOutIPv6); 1026 MDIFF(d, i2, i1, ipOutSwitchIPv6); 1027 prevp = diffptr++; 1028 break; 1029 } 1030 case MIB2_IP6: { 1031 mib2_ipv6IfStatsEntry_t *i2; 1032 mib2_ipv6IfStatsEntry_t *i1; 1033 mib2_ipv6IfStatsEntry_t *d; 1034 1035 i2 = (mib2_ipv6IfStatsEntry_t *)tempp2->valp; 1036 i1 = (mib2_ipv6IfStatsEntry_t *)tempp1->valp; 1037 diffptr->group = tempp2->group; 1038 diffptr->mib_id = tempp2->mib_id; 1039 diffptr->length = tempp2->length; 1040 d = (mib2_ipv6IfStatsEntry_t *)calloc( 1041 tempp2->length, 1); 1042 if (d == NULL) 1043 goto mibdiff_out_of_memory; 1044 diffptr->valp = d; 1045 d->ipv6Forwarding = i2->ipv6Forwarding; 1046 d->ipv6DefaultHopLimit = 1047 i2->ipv6DefaultHopLimit; 1048 1049 MDIFF(d, i2, i1, ipv6InReceives); 1050 MDIFF(d, i2, i1, ipv6InHdrErrors); 1051 MDIFF(d, i2, i1, ipv6InTooBigErrors); 1052 MDIFF(d, i2, i1, ipv6InNoRoutes); 1053 MDIFF(d, i2, i1, ipv6InAddrErrors); 1054 MDIFF(d, i2, i1, ipv6InUnknownProtos); 1055 MDIFF(d, i2, i1, ipv6InTruncatedPkts); 1056 MDIFF(d, i2, i1, ipv6InDiscards); 1057 MDIFF(d, i2, i1, ipv6InDelivers); 1058 MDIFF(d, i2, i1, ipv6OutForwDatagrams); 1059 MDIFF(d, i2, i1, ipv6OutRequests); 1060 MDIFF(d, i2, i1, ipv6OutDiscards); 1061 MDIFF(d, i2, i1, ipv6OutNoRoutes); 1062 MDIFF(d, i2, i1, ipv6OutFragOKs); 1063 MDIFF(d, i2, i1, ipv6OutFragFails); 1064 MDIFF(d, i2, i1, ipv6OutFragCreates); 1065 MDIFF(d, i2, i1, ipv6ReasmReqds); 1066 MDIFF(d, i2, i1, ipv6ReasmOKs); 1067 MDIFF(d, i2, i1, ipv6ReasmFails); 1068 MDIFF(d, i2, i1, ipv6InMcastPkts); 1069 MDIFF(d, i2, i1, ipv6OutMcastPkts); 1070 MDIFF(d, i2, i1, ipv6ReasmDuplicates); 1071 MDIFF(d, i2, i1, ipv6ReasmPartDups); 1072 MDIFF(d, i2, i1, ipv6ForwProhibits); 1073 MDIFF(d, i2, i1, udpInCksumErrs); 1074 MDIFF(d, i2, i1, udpInOverflows); 1075 MDIFF(d, i2, i1, rawipInOverflows); 1076 MDIFF(d, i2, i1, ipv6InIPv4); 1077 MDIFF(d, i2, i1, ipv6OutIPv4); 1078 MDIFF(d, i2, i1, ipv6OutSwitchIPv4); 1079 prevp = diffptr++; 1080 break; 1081 } 1082 case EXPER_DVMRP: { 1083 struct mrtstat *m2; 1084 struct mrtstat *m1; 1085 struct mrtstat *d; 1086 1087 m2 = (struct mrtstat *)tempp2->valp; 1088 m1 = (struct mrtstat *)tempp1->valp; 1089 diffptr->group = tempp2->group; 1090 diffptr->mib_id = tempp2->mib_id; 1091 diffptr->length = tempp2->length; 1092 d = (struct mrtstat *)calloc(tempp2->length, 1); 1093 if (d == NULL) 1094 goto mibdiff_out_of_memory; 1095 diffptr->valp = d; 1096 MDIFF(d, m2, m1, mrts_mfc_hits); 1097 MDIFF(d, m2, m1, mrts_mfc_misses); 1098 MDIFF(d, m2, m1, mrts_fwd_in); 1099 MDIFF(d, m2, m1, mrts_fwd_out); 1100 d->mrts_upcalls = m2->mrts_upcalls; 1101 MDIFF(d, m2, m1, mrts_fwd_drop); 1102 MDIFF(d, m2, m1, mrts_bad_tunnel); 1103 MDIFF(d, m2, m1, mrts_cant_tunnel); 1104 MDIFF(d, m2, m1, mrts_wrong_if); 1105 MDIFF(d, m2, m1, mrts_upq_ovflw); 1106 MDIFF(d, m2, m1, mrts_cache_cleanups); 1107 MDIFF(d, m2, m1, mrts_drop_sel); 1108 MDIFF(d, m2, m1, mrts_q_overflow); 1109 MDIFF(d, m2, m1, mrts_pkt2large); 1110 MDIFF(d, m2, m1, mrts_pim_badversion); 1111 MDIFF(d, m2, m1, mrts_pim_rcv_badcsum); 1112 MDIFF(d, m2, m1, mrts_pim_badregisters); 1113 MDIFF(d, m2, m1, mrts_pim_regforwards); 1114 MDIFF(d, m2, m1, mrts_pim_regsend_drops); 1115 MDIFF(d, m2, m1, mrts_pim_malformed); 1116 MDIFF(d, m2, m1, mrts_pim_nomemory); 1117 prevp = diffptr++; 1118 break; 1119 } 1120 case EXPER_IGMP: { 1121 struct igmpstat *i2; 1122 struct igmpstat *i1; 1123 struct igmpstat *d; 1124 1125 i2 = (struct igmpstat *)tempp2->valp; 1126 i1 = (struct igmpstat *)tempp1->valp; 1127 diffptr->group = tempp2->group; 1128 diffptr->mib_id = tempp2->mib_id; 1129 diffptr->length = tempp2->length; 1130 d = (struct igmpstat *)calloc( 1131 tempp2->length, 1); 1132 if (d == NULL) 1133 goto mibdiff_out_of_memory; 1134 diffptr->valp = d; 1135 MDIFF(d, i2, i1, igps_rcv_total); 1136 MDIFF(d, i2, i1, igps_rcv_tooshort); 1137 MDIFF(d, i2, i1, igps_rcv_badsum); 1138 MDIFF(d, i2, i1, igps_rcv_queries); 1139 MDIFF(d, i2, i1, igps_rcv_badqueries); 1140 MDIFF(d, i2, i1, igps_rcv_reports); 1141 MDIFF(d, i2, i1, igps_rcv_badreports); 1142 MDIFF(d, i2, i1, igps_rcv_ourreports); 1143 MDIFF(d, i2, i1, igps_snd_reports); 1144 prevp = diffptr++; 1145 break; 1146 } 1147 case MIB2_ICMP: { 1148 mib2_icmp_t *i2; 1149 mib2_icmp_t *i1; 1150 mib2_icmp_t *d; 1151 1152 i2 = (mib2_icmp_t *)tempp2->valp; 1153 i1 = (mib2_icmp_t *)tempp1->valp; 1154 diffptr->group = tempp2->group; 1155 diffptr->mib_id = tempp2->mib_id; 1156 diffptr->length = tempp2->length; 1157 d = (mib2_icmp_t *)calloc(tempp2->length, 1); 1158 if (d == NULL) 1159 goto mibdiff_out_of_memory; 1160 diffptr->valp = d; 1161 MDIFF(d, i2, i1, icmpInMsgs); 1162 MDIFF(d, i2, i1, icmpInErrors); 1163 MDIFF(d, i2, i1, icmpInCksumErrs); 1164 MDIFF(d, i2, i1, icmpInUnknowns); 1165 MDIFF(d, i2, i1, icmpInDestUnreachs); 1166 MDIFF(d, i2, i1, icmpInTimeExcds); 1167 MDIFF(d, i2, i1, icmpInParmProbs); 1168 MDIFF(d, i2, i1, icmpInSrcQuenchs); 1169 MDIFF(d, i2, i1, icmpInRedirects); 1170 MDIFF(d, i2, i1, icmpInBadRedirects); 1171 MDIFF(d, i2, i1, icmpInEchos); 1172 MDIFF(d, i2, i1, icmpInEchoReps); 1173 MDIFF(d, i2, i1, icmpInTimestamps); 1174 MDIFF(d, i2, i1, icmpInAddrMasks); 1175 MDIFF(d, i2, i1, icmpInAddrMaskReps); 1176 MDIFF(d, i2, i1, icmpInFragNeeded); 1177 MDIFF(d, i2, i1, icmpOutMsgs); 1178 MDIFF(d, i2, i1, icmpOutDrops); 1179 MDIFF(d, i2, i1, icmpOutErrors); 1180 MDIFF(d, i2, i1, icmpOutDestUnreachs); 1181 MDIFF(d, i2, i1, icmpOutTimeExcds); 1182 MDIFF(d, i2, i1, icmpOutParmProbs); 1183 MDIFF(d, i2, i1, icmpOutSrcQuenchs); 1184 MDIFF(d, i2, i1, icmpOutRedirects); 1185 MDIFF(d, i2, i1, icmpOutEchos); 1186 MDIFF(d, i2, i1, icmpOutEchoReps); 1187 MDIFF(d, i2, i1, icmpOutTimestamps); 1188 MDIFF(d, i2, i1, icmpOutTimestampReps); 1189 MDIFF(d, i2, i1, icmpOutAddrMasks); 1190 MDIFF(d, i2, i1, icmpOutAddrMaskReps); 1191 MDIFF(d, i2, i1, icmpOutFragNeeded); 1192 MDIFF(d, i2, i1, icmpInOverflows); 1193 prevp = diffptr++; 1194 break; 1195 } 1196 case MIB2_ICMP6: { 1197 mib2_ipv6IfIcmpEntry_t *i2; 1198 mib2_ipv6IfIcmpEntry_t *i1; 1199 mib2_ipv6IfIcmpEntry_t *d; 1200 1201 i2 = (mib2_ipv6IfIcmpEntry_t *)tempp2->valp; 1202 i1 = (mib2_ipv6IfIcmpEntry_t *)tempp1->valp; 1203 diffptr->group = tempp2->group; 1204 diffptr->mib_id = tempp2->mib_id; 1205 diffptr->length = tempp2->length; 1206 d = (mib2_ipv6IfIcmpEntry_t *)calloc(tempp2->length, 1); 1207 if (d == NULL) 1208 goto mibdiff_out_of_memory; 1209 diffptr->valp = d; 1210 MDIFF(d, i2, i1, ipv6IfIcmpInMsgs); 1211 MDIFF(d, i2, i1, ipv6IfIcmpInErrors); 1212 MDIFF(d, i2, i1, ipv6IfIcmpInDestUnreachs); 1213 MDIFF(d, i2, i1, ipv6IfIcmpInAdminProhibs); 1214 MDIFF(d, i2, i1, ipv6IfIcmpInTimeExcds); 1215 MDIFF(d, i2, i1, ipv6IfIcmpInParmProblems); 1216 MDIFF(d, i2, i1, ipv6IfIcmpInPktTooBigs); 1217 MDIFF(d, i2, i1, ipv6IfIcmpInEchos); 1218 MDIFF(d, i2, i1, ipv6IfIcmpInEchoReplies); 1219 MDIFF(d, i2, i1, ipv6IfIcmpInRouterSolicits); 1220 MDIFF(d, i2, i1, ipv6IfIcmpInRouterAdvertisements); 1221 MDIFF(d, i2, i1, ipv6IfIcmpInNeighborSolicits); 1222 MDIFF(d, i2, i1, ipv6IfIcmpInNeighborAdvertisements); 1223 MDIFF(d, i2, i1, ipv6IfIcmpInRedirects); 1224 MDIFF(d, i2, i1, ipv6IfIcmpInBadRedirects); 1225 MDIFF(d, i2, i1, ipv6IfIcmpInGroupMembQueries); 1226 MDIFF(d, i2, i1, ipv6IfIcmpInGroupMembResponses); 1227 MDIFF(d, i2, i1, ipv6IfIcmpInGroupMembReductions); 1228 MDIFF(d, i2, i1, ipv6IfIcmpInOverflows); 1229 MDIFF(d, i2, i1, ipv6IfIcmpOutMsgs); 1230 MDIFF(d, i2, i1, ipv6IfIcmpOutErrors); 1231 MDIFF(d, i2, i1, ipv6IfIcmpOutDestUnreachs); 1232 MDIFF(d, i2, i1, ipv6IfIcmpOutAdminProhibs); 1233 MDIFF(d, i2, i1, ipv6IfIcmpOutTimeExcds); 1234 MDIFF(d, i2, i1, ipv6IfIcmpOutParmProblems); 1235 MDIFF(d, i2, i1, ipv6IfIcmpOutPktTooBigs); 1236 MDIFF(d, i2, i1, ipv6IfIcmpOutEchos); 1237 MDIFF(d, i2, i1, ipv6IfIcmpOutEchoReplies); 1238 MDIFF(d, i2, i1, ipv6IfIcmpOutRouterSolicits); 1239 MDIFF(d, i2, i1, ipv6IfIcmpOutRouterAdvertisements); 1240 MDIFF(d, i2, i1, ipv6IfIcmpOutNeighborSolicits); 1241 MDIFF(d, i2, i1, ipv6IfIcmpOutNeighborAdvertisements); 1242 MDIFF(d, i2, i1, ipv6IfIcmpOutRedirects); 1243 MDIFF(d, i2, i1, ipv6IfIcmpOutGroupMembQueries); 1244 MDIFF(d, i2, i1, ipv6IfIcmpOutGroupMembResponses); 1245 MDIFF(d, i2, i1, ipv6IfIcmpOutGroupMembReductions); 1246 prevp = diffptr++; 1247 break; 1248 } 1249 case MIB2_TCP: { 1250 mib2_tcp_t *t2; 1251 mib2_tcp_t *t1; 1252 mib2_tcp_t *d; 1253 1254 t2 = (mib2_tcp_t *)tempp2->valp; 1255 t1 = (mib2_tcp_t *)tempp1->valp; 1256 diffptr->group = tempp2->group; 1257 diffptr->mib_id = tempp2->mib_id; 1258 diffptr->length = tempp2->length; 1259 d = (mib2_tcp_t *)calloc(tempp2->length, 1); 1260 if (d == NULL) 1261 goto mibdiff_out_of_memory; 1262 diffptr->valp = d; 1263 d->tcpRtoMin = t2->tcpRtoMin; 1264 d->tcpRtoMax = t2->tcpRtoMax; 1265 d->tcpMaxConn = t2->tcpMaxConn; 1266 MDIFF(d, t2, t1, tcpActiveOpens); 1267 MDIFF(d, t2, t1, tcpPassiveOpens); 1268 MDIFF(d, t2, t1, tcpAttemptFails); 1269 MDIFF(d, t2, t1, tcpEstabResets); 1270 d->tcpCurrEstab = t2->tcpCurrEstab; 1271 MDIFF(d, t2, t1, tcpHCOutSegs); 1272 MDIFF(d, t2, t1, tcpOutDataSegs); 1273 MDIFF(d, t2, t1, tcpOutDataBytes); 1274 MDIFF(d, t2, t1, tcpRetransSegs); 1275 MDIFF(d, t2, t1, tcpRetransBytes); 1276 MDIFF(d, t2, t1, tcpOutAck); 1277 MDIFF(d, t2, t1, tcpOutAckDelayed); 1278 MDIFF(d, t2, t1, tcpOutUrg); 1279 MDIFF(d, t2, t1, tcpOutWinUpdate); 1280 MDIFF(d, t2, t1, tcpOutWinProbe); 1281 MDIFF(d, t2, t1, tcpOutControl); 1282 MDIFF(d, t2, t1, tcpOutRsts); 1283 MDIFF(d, t2, t1, tcpOutFastRetrans); 1284 MDIFF(d, t2, t1, tcpHCInSegs); 1285 MDIFF(d, t2, t1, tcpInAckSegs); 1286 MDIFF(d, t2, t1, tcpInAckBytes); 1287 MDIFF(d, t2, t1, tcpInDupAck); 1288 MDIFF(d, t2, t1, tcpInAckUnsent); 1289 MDIFF(d, t2, t1, tcpInDataInorderSegs); 1290 MDIFF(d, t2, t1, tcpInDataInorderBytes); 1291 MDIFF(d, t2, t1, tcpInDataUnorderSegs); 1292 MDIFF(d, t2, t1, tcpInDataUnorderBytes); 1293 MDIFF(d, t2, t1, tcpInDataDupSegs); 1294 MDIFF(d, t2, t1, tcpInDataDupBytes); 1295 MDIFF(d, t2, t1, tcpInDataPartDupSegs); 1296 MDIFF(d, t2, t1, tcpInDataPartDupBytes); 1297 MDIFF(d, t2, t1, tcpInDataPastWinSegs); 1298 MDIFF(d, t2, t1, tcpInDataPastWinBytes); 1299 MDIFF(d, t2, t1, tcpInWinProbe); 1300 MDIFF(d, t2, t1, tcpInWinUpdate); 1301 MDIFF(d, t2, t1, tcpInClosed); 1302 MDIFF(d, t2, t1, tcpRttNoUpdate); 1303 MDIFF(d, t2, t1, tcpRttUpdate); 1304 MDIFF(d, t2, t1, tcpTimRetrans); 1305 MDIFF(d, t2, t1, tcpTimRetransDrop); 1306 MDIFF(d, t2, t1, tcpTimKeepalive); 1307 MDIFF(d, t2, t1, tcpTimKeepaliveProbe); 1308 MDIFF(d, t2, t1, tcpTimKeepaliveDrop); 1309 MDIFF(d, t2, t1, tcpListenDrop); 1310 MDIFF(d, t2, t1, tcpListenDropQ0); 1311 MDIFF(d, t2, t1, tcpHalfOpenDrop); 1312 MDIFF(d, t2, t1, tcpOutSackRetransSegs); 1313 prevp = diffptr++; 1314 break; 1315 } 1316 case MIB2_UDP: { 1317 mib2_udp_t *u2; 1318 mib2_udp_t *u1; 1319 mib2_udp_t *d; 1320 1321 u2 = (mib2_udp_t *)tempp2->valp; 1322 u1 = (mib2_udp_t *)tempp1->valp; 1323 diffptr->group = tempp2->group; 1324 diffptr->mib_id = tempp2->mib_id; 1325 diffptr->length = tempp2->length; 1326 d = (mib2_udp_t *)calloc(tempp2->length, 1); 1327 if (d == NULL) 1328 goto mibdiff_out_of_memory; 1329 diffptr->valp = d; 1330 MDIFF(d, u2, u1, udpHCInDatagrams); 1331 MDIFF(d, u2, u1, udpInErrors); 1332 MDIFF(d, u2, u1, udpHCOutDatagrams); 1333 MDIFF(d, u2, u1, udpOutErrors); 1334 prevp = diffptr++; 1335 break; 1336 } 1337 case MIB2_SCTP: { 1338 mib2_sctp_t *s2; 1339 mib2_sctp_t *s1; 1340 mib2_sctp_t *d; 1341 1342 s2 = (mib2_sctp_t *)tempp2->valp; 1343 s1 = (mib2_sctp_t *)tempp1->valp; 1344 diffptr->group = tempp2->group; 1345 diffptr->mib_id = tempp2->mib_id; 1346 diffptr->length = tempp2->length; 1347 d = (mib2_sctp_t *)calloc(tempp2->length, 1); 1348 if (d == NULL) 1349 goto mibdiff_out_of_memory; 1350 diffptr->valp = d; 1351 d->sctpRtoAlgorithm = s2->sctpRtoAlgorithm; 1352 d->sctpRtoMin = s2->sctpRtoMin; 1353 d->sctpRtoMax = s2->sctpRtoMax; 1354 d->sctpRtoInitial = s2->sctpRtoInitial; 1355 d->sctpMaxAssocs = s2->sctpMaxAssocs; 1356 d->sctpValCookieLife = s2->sctpValCookieLife; 1357 d->sctpMaxInitRetr = s2->sctpMaxInitRetr; 1358 d->sctpCurrEstab = s2->sctpCurrEstab; 1359 MDIFF(d, s2, s1, sctpActiveEstab); 1360 MDIFF(d, s2, s1, sctpPassiveEstab); 1361 MDIFF(d, s2, s1, sctpAborted); 1362 MDIFF(d, s2, s1, sctpShutdowns); 1363 MDIFF(d, s2, s1, sctpOutOfBlue); 1364 MDIFF(d, s2, s1, sctpChecksumError); 1365 MDIFF(d, s2, s1, sctpOutCtrlChunks); 1366 MDIFF(d, s2, s1, sctpOutOrderChunks); 1367 MDIFF(d, s2, s1, sctpOutUnorderChunks); 1368 MDIFF(d, s2, s1, sctpRetransChunks); 1369 MDIFF(d, s2, s1, sctpOutAck); 1370 MDIFF(d, s2, s1, sctpOutAckDelayed); 1371 MDIFF(d, s2, s1, sctpOutWinUpdate); 1372 MDIFF(d, s2, s1, sctpOutFastRetrans); 1373 MDIFF(d, s2, s1, sctpOutWinProbe); 1374 MDIFF(d, s2, s1, sctpInCtrlChunks); 1375 MDIFF(d, s2, s1, sctpInOrderChunks); 1376 MDIFF(d, s2, s1, sctpInUnorderChunks); 1377 MDIFF(d, s2, s1, sctpInAck); 1378 MDIFF(d, s2, s1, sctpInDupAck); 1379 MDIFF(d, s2, s1, sctpInAckUnsent); 1380 MDIFF(d, s2, s1, sctpFragUsrMsgs); 1381 MDIFF(d, s2, s1, sctpReasmUsrMsgs); 1382 MDIFF(d, s2, s1, sctpOutSCTPPkts); 1383 MDIFF(d, s2, s1, sctpInSCTPPkts); 1384 MDIFF(d, s2, s1, sctpInInvalidCookie); 1385 MDIFF(d, s2, s1, sctpTimRetrans); 1386 MDIFF(d, s2, s1, sctpTimRetransDrop); 1387 MDIFF(d, s2, s1, sctpTimHeartBeatProbe); 1388 MDIFF(d, s2, s1, sctpTimHeartBeatDrop); 1389 MDIFF(d, s2, s1, sctpListenDrop); 1390 MDIFF(d, s2, s1, sctpInClosed); 1391 prevp = diffptr++; 1392 break; 1393 } 1394 case EXPER_RAWIP: { 1395 mib2_rawip_t *r2; 1396 mib2_rawip_t *r1; 1397 mib2_rawip_t *d; 1398 1399 r2 = (mib2_rawip_t *)tempp2->valp; 1400 r1 = (mib2_rawip_t *)tempp1->valp; 1401 diffptr->group = tempp2->group; 1402 diffptr->mib_id = tempp2->mib_id; 1403 diffptr->length = tempp2->length; 1404 d = (mib2_rawip_t *)calloc(tempp2->length, 1); 1405 if (d == NULL) 1406 goto mibdiff_out_of_memory; 1407 diffptr->valp = d; 1408 MDIFF(d, r2, r1, rawipInDatagrams); 1409 MDIFF(d, r2, r1, rawipInErrors); 1410 MDIFF(d, r2, r1, rawipInCksumErrs); 1411 MDIFF(d, r2, r1, rawipOutDatagrams); 1412 MDIFF(d, r2, r1, rawipOutErrors); 1413 prevp = diffptr++; 1414 break; 1415 } 1416 /* 1417 * there are more "group" types but they aren't 1418 * required for the -s and -Ms options 1419 */ 1420 } 1421 } /* 'for' loop 2 ends */ 1422 tempp1 = NULL; 1423 } /* 'for' loop 1 ends */ 1424 tempp2 = NULL; 1425 diffptr--; 1426 diffptr->next_item = NULL; 1427 return (diffp); 1428 1429 mibdiff_out_of_memory:; 1430 mib_item_destroy(&diffp); 1431 return (NULL); 1432 } 1433 1434 /* 1435 * mib_item_destroy: cleans up a mib_item_t * 1436 * that was created by calling mib_item_dup or 1437 * mib_item_diff 1438 */ 1439 static void 1440 mib_item_destroy(mib_item_t **itemp) { 1441 int nitems = 0; 1442 int c = 0; 1443 mib_item_t *tempp; 1444 1445 if (itemp == NULL || *itemp == NULL) 1446 return; 1447 1448 for (tempp = *itemp; tempp != NULL; tempp = tempp->next_item) 1449 if (tempp->mib_id == 0) 1450 nitems++; 1451 else 1452 return; /* cannot destroy! */ 1453 1454 if (nitems == 0) 1455 return; /* cannot destroy! */ 1456 1457 for (c = nitems - 1; c >= 0; c--) { 1458 if ((itemp[0][c]).valp != NULL) 1459 free((itemp[0][c]).valp); 1460 } 1461 free(*itemp); 1462 1463 *itemp = NULL; 1464 } 1465 1466 /* Compare two Octet_ts. Return B_TRUE if they match, B_FALSE if not. */ 1467 static boolean_t 1468 octetstrmatch(const Octet_t *a, const Octet_t *b) 1469 { 1470 if (a == NULL || b == NULL) 1471 return (B_FALSE); 1472 1473 if (a->o_length != b->o_length) 1474 return (B_FALSE); 1475 1476 return (memcmp(a->o_bytes, b->o_bytes, a->o_length) == 0); 1477 } 1478 1479 /* If octetstr() changes make an appropriate change to STR_EXPAND */ 1480 static char * 1481 octetstr(const Octet_t *op, int code, char *dst, uint_t dstlen) 1482 { 1483 int i; 1484 char *cp; 1485 1486 cp = dst; 1487 if (op) { 1488 for (i = 0; i < op->o_length; i++) { 1489 switch (code) { 1490 case 'd': 1491 if (cp - dst + 4 > dstlen) { 1492 *cp = '\0'; 1493 return (dst); 1494 } 1495 (void) snprintf(cp, 5, "%d.", 1496 0xff & op->o_bytes[i]); 1497 cp = strchr(cp, '\0'); 1498 break; 1499 case 'a': 1500 if (cp - dst + 1 > dstlen) { 1501 *cp = '\0'; 1502 return (dst); 1503 } 1504 *cp++ = op->o_bytes[i]; 1505 break; 1506 case 'h': 1507 default: 1508 if (cp - dst + 3 > dstlen) { 1509 *cp = '\0'; 1510 return (dst); 1511 } 1512 (void) snprintf(cp, 4, "%02x:", 1513 0xff & op->o_bytes[i]); 1514 cp += 3; 1515 break; 1516 } 1517 } 1518 } 1519 if (code != 'a' && cp != dst) 1520 cp--; 1521 *cp = '\0'; 1522 return (dst); 1523 } 1524 1525 static const char * 1526 mitcp_state(int state, const mib2_transportMLPEntry_t *attr) 1527 { 1528 static char tcpsbuf[50]; 1529 const char *cp; 1530 1531 switch (state) { 1532 case TCPS_CLOSED: 1533 cp = "CLOSED"; 1534 break; 1535 case TCPS_IDLE: 1536 cp = "IDLE"; 1537 break; 1538 case TCPS_BOUND: 1539 cp = "BOUND"; 1540 break; 1541 case TCPS_LISTEN: 1542 cp = "LISTEN"; 1543 break; 1544 case TCPS_SYN_SENT: 1545 cp = "SYN_SENT"; 1546 break; 1547 case TCPS_SYN_RCVD: 1548 cp = "SYN_RCVD"; 1549 break; 1550 case TCPS_ESTABLISHED: 1551 cp = "ESTABLISHED"; 1552 break; 1553 case TCPS_CLOSE_WAIT: 1554 cp = "CLOSE_WAIT"; 1555 break; 1556 case TCPS_FIN_WAIT_1: 1557 cp = "FIN_WAIT_1"; 1558 break; 1559 case TCPS_CLOSING: 1560 cp = "CLOSING"; 1561 break; 1562 case TCPS_LAST_ACK: 1563 cp = "LAST_ACK"; 1564 break; 1565 case TCPS_FIN_WAIT_2: 1566 cp = "FIN_WAIT_2"; 1567 break; 1568 case TCPS_TIME_WAIT: 1569 cp = "TIME_WAIT"; 1570 break; 1571 default: 1572 (void) snprintf(tcpsbuf, sizeof (tcpsbuf), 1573 "UnknownState(%d)", state); 1574 cp = tcpsbuf; 1575 break; 1576 } 1577 1578 if (RSECflag && attr != NULL && attr->tme_flags != 0) { 1579 if (cp != tcpsbuf) { 1580 (void) strlcpy(tcpsbuf, cp, sizeof (tcpsbuf)); 1581 cp = tcpsbuf; 1582 } 1583 if (attr->tme_flags & MIB2_TMEF_PRIVATE) 1584 (void) strlcat(tcpsbuf, " P", sizeof (tcpsbuf)); 1585 if (attr->tme_flags & MIB2_TMEF_SHARED) 1586 (void) strlcat(tcpsbuf, " S", sizeof (tcpsbuf)); 1587 } 1588 1589 return (cp); 1590 } 1591 1592 static const char * 1593 miudp_state(int state, const mib2_transportMLPEntry_t *attr) 1594 { 1595 static char udpsbuf[50]; 1596 const char *cp; 1597 1598 switch (state) { 1599 case MIB2_UDP_unbound: 1600 cp = "Unbound"; 1601 break; 1602 case MIB2_UDP_idle: 1603 cp = "Idle"; 1604 break; 1605 case MIB2_UDP_connected: 1606 cp = "Connected"; 1607 break; 1608 default: 1609 (void) snprintf(udpsbuf, sizeof (udpsbuf), 1610 "Unknown State(%d)", state); 1611 cp = udpsbuf; 1612 break; 1613 } 1614 1615 if (RSECflag && attr != NULL && attr->tme_flags != 0) { 1616 if (cp != udpsbuf) { 1617 (void) strlcpy(udpsbuf, cp, sizeof (udpsbuf)); 1618 cp = udpsbuf; 1619 } 1620 if (attr->tme_flags & MIB2_TMEF_PRIVATE) 1621 (void) strlcat(udpsbuf, " P", sizeof (udpsbuf)); 1622 if (attr->tme_flags & MIB2_TMEF_SHARED) 1623 (void) strlcat(udpsbuf, " S", sizeof (udpsbuf)); 1624 } 1625 1626 return (cp); 1627 } 1628 1629 static int odd; 1630 1631 static void 1632 prval_init(void) 1633 { 1634 odd = 0; 1635 } 1636 1637 static void 1638 prval(char *str, Counter val) 1639 { 1640 (void) printf("\t%-20s=%6u", str, val); 1641 if (odd++ & 1) 1642 (void) putchar('\n'); 1643 } 1644 1645 static void 1646 prval64(char *str, Counter64 val) 1647 { 1648 (void) printf("\t%-20s=%6llu", str, val); 1649 if (odd++ & 1) 1650 (void) putchar('\n'); 1651 } 1652 1653 static void 1654 pr_int_val(char *str, int val) 1655 { 1656 (void) printf("\t%-20s=%6d", str, val); 1657 if (odd++ & 1) 1658 (void) putchar('\n'); 1659 } 1660 1661 static void 1662 pr_sctp_rtoalgo(char *str, int val) 1663 { 1664 (void) printf("\t%-20s=", str); 1665 switch (val) { 1666 case MIB2_SCTP_RTOALGO_OTHER: 1667 (void) printf("%6.6s", "other"); 1668 break; 1669 1670 case MIB2_SCTP_RTOALGO_VANJ: 1671 (void) printf("%6.6s", "vanj"); 1672 break; 1673 1674 default: 1675 (void) printf("%6d", val); 1676 break; 1677 } 1678 if (odd++ & 1) 1679 (void) putchar('\n'); 1680 } 1681 1682 static void 1683 prval_end(void) 1684 { 1685 if (odd++ & 1) 1686 (void) putchar('\n'); 1687 } 1688 1689 /* Extract constant sizes */ 1690 static void 1691 mib_get_constants(mib_item_t *item) 1692 { 1693 /* 'for' loop 1: */ 1694 for (; item; item = item->next_item) { 1695 if (item->mib_id != 0) 1696 continue; /* 'for' loop 1 */ 1697 1698 switch (item->group) { 1699 case MIB2_IP: { 1700 mib2_ip_t *ip = (mib2_ip_t *)item->valp; 1701 1702 ipAddrEntrySize = ip->ipAddrEntrySize; 1703 ipRouteEntrySize = ip->ipRouteEntrySize; 1704 ipNetToMediaEntrySize = ip->ipNetToMediaEntrySize; 1705 ipMemberEntrySize = ip->ipMemberEntrySize; 1706 ipGroupSourceEntrySize = ip->ipGroupSourceEntrySize; 1707 ipRouteAttributeSize = ip->ipRouteAttributeSize; 1708 transportMLPSize = ip->transportMLPSize; 1709 assert(IS_P2ALIGNED(ipAddrEntrySize, 1710 sizeof (mib2_ipAddrEntry_t *)) && 1711 IS_P2ALIGNED(ipRouteEntrySize, 1712 sizeof (mib2_ipRouteEntry_t *)) && 1713 IS_P2ALIGNED(ipNetToMediaEntrySize, 1714 sizeof (mib2_ipNetToMediaEntry_t *)) && 1715 IS_P2ALIGNED(ipMemberEntrySize, 1716 sizeof (ip_member_t *)) && 1717 IS_P2ALIGNED(ipGroupSourceEntrySize, 1718 sizeof (ip_grpsrc_t *)) && 1719 IS_P2ALIGNED(ipRouteAttributeSize, 1720 sizeof (mib2_ipAttributeEntry_t *)) && 1721 IS_P2ALIGNED(transportMLPSize, 1722 sizeof (mib2_transportMLPEntry_t *))); 1723 break; 1724 } 1725 case EXPER_DVMRP: { 1726 struct mrtstat *mrts = (struct mrtstat *)item->valp; 1727 1728 vifctlSize = mrts->mrts_vifctlSize; 1729 mfcctlSize = mrts->mrts_mfcctlSize; 1730 assert(IS_P2ALIGNED(vifctlSize, 1731 sizeof (struct vifclt *)) && 1732 IS_P2ALIGNED(mfcctlSize, sizeof (struct mfcctl *))); 1733 break; 1734 } 1735 case MIB2_IP6: { 1736 mib2_ipv6IfStatsEntry_t *ip6; 1737 /* Just use the first entry */ 1738 1739 ip6 = (mib2_ipv6IfStatsEntry_t *)item->valp; 1740 ipv6IfStatsEntrySize = ip6->ipv6IfStatsEntrySize; 1741 ipv6AddrEntrySize = ip6->ipv6AddrEntrySize; 1742 ipv6RouteEntrySize = ip6->ipv6RouteEntrySize; 1743 ipv6NetToMediaEntrySize = ip6->ipv6NetToMediaEntrySize; 1744 ipv6MemberEntrySize = ip6->ipv6MemberEntrySize; 1745 ipv6GroupSourceEntrySize = 1746 ip6->ipv6GroupSourceEntrySize; 1747 assert(IS_P2ALIGNED(ipv6IfStatsEntrySize, 1748 sizeof (mib2_ipv6IfStatsEntry_t *)) && 1749 IS_P2ALIGNED(ipv6AddrEntrySize, 1750 sizeof (mib2_ipv6AddrEntry_t *)) && 1751 IS_P2ALIGNED(ipv6RouteEntrySize, 1752 sizeof (mib2_ipv6RouteEntry_t *)) && 1753 IS_P2ALIGNED(ipv6NetToMediaEntrySize, 1754 sizeof (mib2_ipv6NetToMediaEntry_t *)) && 1755 IS_P2ALIGNED(ipv6MemberEntrySize, 1756 sizeof (ipv6_member_t *)) && 1757 IS_P2ALIGNED(ipv6GroupSourceEntrySize, 1758 sizeof (ipv6_grpsrc_t *))); 1759 break; 1760 } 1761 case MIB2_ICMP6: { 1762 mib2_ipv6IfIcmpEntry_t *icmp6; 1763 /* Just use the first entry */ 1764 1765 icmp6 = (mib2_ipv6IfIcmpEntry_t *)item->valp; 1766 ipv6IfIcmpEntrySize = icmp6->ipv6IfIcmpEntrySize; 1767 assert(IS_P2ALIGNED(ipv6IfIcmpEntrySize, 1768 sizeof (mib2_ipv6IfIcmpEntry_t *))); 1769 break; 1770 } 1771 case MIB2_TCP: { 1772 mib2_tcp_t *tcp = (mib2_tcp_t *)item->valp; 1773 1774 tcpConnEntrySize = tcp->tcpConnTableSize; 1775 tcp6ConnEntrySize = tcp->tcp6ConnTableSize; 1776 assert(IS_P2ALIGNED(tcpConnEntrySize, 1777 sizeof (mib2_tcpConnEntry_t *)) && 1778 IS_P2ALIGNED(tcp6ConnEntrySize, 1779 sizeof (mib2_tcp6ConnEntry_t *))); 1780 break; 1781 } 1782 case MIB2_UDP: { 1783 mib2_udp_t *udp = (mib2_udp_t *)item->valp; 1784 1785 udpEntrySize = udp->udpEntrySize; 1786 udp6EntrySize = udp->udp6EntrySize; 1787 assert(IS_P2ALIGNED(udpEntrySize, 1788 sizeof (mib2_udpEntry_t *)) && 1789 IS_P2ALIGNED(udp6EntrySize, 1790 sizeof (mib2_udp6Entry_t *))); 1791 break; 1792 } 1793 case MIB2_SCTP: { 1794 mib2_sctp_t *sctp = (mib2_sctp_t *)item->valp; 1795 1796 sctpEntrySize = sctp->sctpEntrySize; 1797 sctpLocalEntrySize = sctp->sctpLocalEntrySize; 1798 sctpRemoteEntrySize = sctp->sctpRemoteEntrySize; 1799 break; 1800 } 1801 } 1802 } /* 'for' loop 1 ends */ 1803 1804 if (Dflag) { 1805 (void) puts("mib_get_constants:"); 1806 (void) printf("\tipv6IfStatsEntrySize %d\n", 1807 ipv6IfStatsEntrySize); 1808 (void) printf("\tipAddrEntrySize %d\n", ipAddrEntrySize); 1809 (void) printf("\tipRouteEntrySize %d\n", ipRouteEntrySize); 1810 (void) printf("\tipNetToMediaEntrySize %d\n", 1811 ipNetToMediaEntrySize); 1812 (void) printf("\tipMemberEntrySize %d\n", ipMemberEntrySize); 1813 (void) printf("\tipRouteAttributeSize %d\n", 1814 ipRouteAttributeSize); 1815 (void) printf("\tvifctlSize %d\n", vifctlSize); 1816 (void) printf("\tmfcctlSize %d\n", mfcctlSize); 1817 1818 (void) printf("\tipv6AddrEntrySize %d\n", ipv6AddrEntrySize); 1819 (void) printf("\tipv6RouteEntrySize %d\n", ipv6RouteEntrySize); 1820 (void) printf("\tipv6NetToMediaEntrySize %d\n", 1821 ipv6NetToMediaEntrySize); 1822 (void) printf("\tipv6MemberEntrySize %d\n", 1823 ipv6MemberEntrySize); 1824 (void) printf("\tipv6IfIcmpEntrySize %d\n", 1825 ipv6IfIcmpEntrySize); 1826 (void) printf("\ttransportMLPSize %d\n", transportMLPSize); 1827 (void) printf("\ttcpConnEntrySize %d\n", tcpConnEntrySize); 1828 (void) printf("\ttcp6ConnEntrySize %d\n", tcp6ConnEntrySize); 1829 (void) printf("\tudpEntrySize %d\n", udpEntrySize); 1830 (void) printf("\tudp6EntrySize %d\n", udp6EntrySize); 1831 (void) printf("\tsctpEntrySize %d\n", sctpEntrySize); 1832 (void) printf("\tsctpLocalEntrySize %d\n", sctpLocalEntrySize); 1833 (void) printf("\tsctpRemoteEntrySize %d\n", 1834 sctpRemoteEntrySize); 1835 } 1836 } 1837 1838 1839 /* ----------------------------- STAT_REPORT ------------------------------- */ 1840 1841 static void 1842 stat_report(mib_item_t *item) 1843 { 1844 int jtemp = 0; 1845 char ifname[LIFNAMSIZ + 1]; 1846 char *ifnamep; 1847 1848 /* 'for' loop 1: */ 1849 for (; item; item = item->next_item) { 1850 if (Dflag) { 1851 (void) printf("\n--- Entry %d ---\n", ++jtemp); 1852 (void) printf("Group = %d, mib_id = %d, " 1853 "length = %d, valp = 0x%p\n", 1854 item->group, item->mib_id, 1855 item->length, item->valp); 1856 } 1857 if (item->mib_id != 0) 1858 continue; /* 'for' loop 1 */ 1859 1860 switch (item->group) { 1861 case MIB2_IP: { 1862 mib2_ip_t *ip = (mib2_ip_t *)item->valp; 1863 1864 if (protocol_selected(IPPROTO_IP) && 1865 family_selected(AF_INET)) { 1866 (void) fputs(v4compat ? "\nIP" : "\nIPv4", 1867 stdout); 1868 print_ip_stats(ip); 1869 } 1870 break; 1871 } 1872 case MIB2_ICMP: { 1873 mib2_icmp_t *icmp = 1874 (mib2_icmp_t *)item->valp; 1875 1876 if (protocol_selected(IPPROTO_ICMP) && 1877 family_selected(AF_INET)) { 1878 (void) fputs(v4compat ? "\nICMP" : "\nICMPv4", 1879 stdout); 1880 print_icmp_stats(icmp); 1881 } 1882 break; 1883 } 1884 case MIB2_IP6: { 1885 mib2_ipv6IfStatsEntry_t *ip6; 1886 mib2_ipv6IfStatsEntry_t sum6; 1887 1888 if (!(protocol_selected(IPPROTO_IPV6)) || 1889 !(family_selected(AF_INET6))) 1890 break; 1891 bzero(&sum6, sizeof (sum6)); 1892 /* 'for' loop 2a: */ 1893 for (ip6 = (mib2_ipv6IfStatsEntry_t *)item->valp; 1894 (char *)ip6 < (char *)item->valp 1895 + item->length; 1896 /* LINTED: (note 1) */ 1897 ip6 = (mib2_ipv6IfStatsEntry_t *)((char *)ip6 + 1898 ipv6IfStatsEntrySize)) { 1899 1900 if (ip6->ipv6IfIndex == 0) { 1901 /* 1902 * The "unknown interface" ip6 1903 * mib. Just add to the sum. 1904 */ 1905 sum_ip6_stats(ip6, &sum6); 1906 continue; /* 'for' loop 2a */ 1907 } 1908 ifnamep = if_indextoname( 1909 ip6->ipv6IfIndex, 1910 ifname); 1911 if (ifnamep == NULL) { 1912 (void) printf( 1913 "Invalid ifindex %d\n", 1914 ip6->ipv6IfIndex); 1915 continue; /* 'for' loop 2a */ 1916 } 1917 1918 if (Aflag) { 1919 (void) printf("\nIPv6 for %s\n", 1920 ifnamep); 1921 print_ip6_stats(ip6); 1922 } 1923 sum_ip6_stats(ip6, &sum6); 1924 } /* 'for' loop 2a ends */ 1925 (void) fputs("\nIPv6", stdout); 1926 print_ip6_stats(&sum6); 1927 break; 1928 } 1929 case MIB2_ICMP6: { 1930 mib2_ipv6IfIcmpEntry_t *icmp6; 1931 mib2_ipv6IfIcmpEntry_t sum6; 1932 1933 if (!(protocol_selected(IPPROTO_ICMPV6)) || 1934 !(family_selected(AF_INET6))) 1935 break; 1936 bzero(&sum6, sizeof (sum6)); 1937 /* 'for' loop 2b: */ 1938 for (icmp6 = 1939 (mib2_ipv6IfIcmpEntry_t *)item->valp; 1940 (char *)icmp6 < (char *)item->valp 1941 + item->length; 1942 icmp6 = 1943 /* LINTED: (note 1) */ 1944 (mib2_ipv6IfIcmpEntry_t *)((char *)icmp6 1945 + ipv6IfIcmpEntrySize)) { 1946 1947 if (icmp6->ipv6IfIcmpIfIndex == 0) { 1948 /* 1949 * The "unknown interface" icmp6 1950 * mib. Just add to the sum. 1951 */ 1952 sum_icmp6_stats(icmp6, &sum6); 1953 continue; /* 'for' loop 2b: */ 1954 } 1955 ifnamep = if_indextoname( 1956 icmp6->ipv6IfIcmpIfIndex, ifname); 1957 if (ifnamep == NULL) { 1958 (void) printf( 1959 "Invalid ifindex %d\n", 1960 icmp6->ipv6IfIcmpIfIndex); 1961 continue; /* 'for' loop 2b: */ 1962 } 1963 1964 if (Aflag) { 1965 (void) printf( 1966 "\nICMPv6 for %s\n", 1967 ifnamep); 1968 print_icmp6_stats(icmp6); 1969 } 1970 sum_icmp6_stats(icmp6, &sum6); 1971 } /* 'for' loop 2b ends */ 1972 (void) fputs("\nICMPv6", stdout); 1973 print_icmp6_stats(&sum6); 1974 break; 1975 } 1976 case MIB2_TCP: { 1977 mib2_tcp_t *tcp = (mib2_tcp_t *)item->valp; 1978 1979 if (protocol_selected(IPPROTO_TCP) && 1980 (family_selected(AF_INET) || 1981 family_selected(AF_INET6))) { 1982 (void) fputs("\nTCP", stdout); 1983 print_tcp_stats(tcp); 1984 } 1985 break; 1986 } 1987 case MIB2_UDP: { 1988 mib2_udp_t *udp = (mib2_udp_t *)item->valp; 1989 1990 if (protocol_selected(IPPROTO_UDP) && 1991 (family_selected(AF_INET) || 1992 family_selected(AF_INET6))) { 1993 (void) fputs("\nUDP", stdout); 1994 print_udp_stats(udp); 1995 } 1996 break; 1997 } 1998 case MIB2_SCTP: { 1999 mib2_sctp_t *sctp = (mib2_sctp_t *)item->valp; 2000 2001 if (protocol_selected(IPPROTO_SCTP) && 2002 (family_selected(AF_INET) || 2003 family_selected(AF_INET6))) { 2004 (void) fputs("\nSCTP", stdout); 2005 print_sctp_stats(sctp); 2006 } 2007 break; 2008 } 2009 case EXPER_RAWIP: { 2010 mib2_rawip_t *rawip = 2011 (mib2_rawip_t *)item->valp; 2012 2013 if (protocol_selected(IPPROTO_RAW) && 2014 (family_selected(AF_INET) || 2015 family_selected(AF_INET6))) { 2016 (void) fputs("\nRAWIP", stdout); 2017 print_rawip_stats(rawip); 2018 } 2019 break; 2020 } 2021 case EXPER_IGMP: { 2022 struct igmpstat *igps = 2023 (struct igmpstat *)item->valp; 2024 2025 if (protocol_selected(IPPROTO_IGMP) && 2026 (family_selected(AF_INET))) { 2027 (void) fputs("\nIGMP:\n", stdout); 2028 print_igmp_stats(igps); 2029 } 2030 break; 2031 } 2032 } 2033 } /* 'for' loop 1 ends */ 2034 (void) putchar('\n'); 2035 (void) fflush(stdout); 2036 } 2037 2038 static void 2039 print_ip_stats(mib2_ip_t *ip) 2040 { 2041 prval_init(); 2042 pr_int_val("ipForwarding", ip->ipForwarding); 2043 pr_int_val("ipDefaultTTL", ip->ipDefaultTTL); 2044 prval("ipInReceives", ip->ipInReceives); 2045 prval("ipInHdrErrors", ip->ipInHdrErrors); 2046 prval("ipInAddrErrors", ip->ipInAddrErrors); 2047 prval("ipInCksumErrs", ip->ipInCksumErrs); 2048 prval("ipForwDatagrams", ip->ipForwDatagrams); 2049 prval("ipForwProhibits", ip->ipForwProhibits); 2050 prval("ipInUnknownProtos", ip->ipInUnknownProtos); 2051 prval("ipInDiscards", ip->ipInDiscards); 2052 prval("ipInDelivers", ip->ipInDelivers); 2053 prval("ipOutRequests", ip->ipOutRequests); 2054 prval("ipOutDiscards", ip->ipOutDiscards); 2055 prval("ipOutNoRoutes", ip->ipOutNoRoutes); 2056 pr_int_val("ipReasmTimeout", ip->ipReasmTimeout); 2057 prval("ipReasmReqds", ip->ipReasmReqds); 2058 prval("ipReasmOKs", ip->ipReasmOKs); 2059 prval("ipReasmFails", ip->ipReasmFails); 2060 prval("ipReasmDuplicates", ip->ipReasmDuplicates); 2061 prval("ipReasmPartDups", ip->ipReasmPartDups); 2062 prval("ipFragOKs", ip->ipFragOKs); 2063 prval("ipFragFails", ip->ipFragFails); 2064 prval("ipFragCreates", ip->ipFragCreates); 2065 prval("ipRoutingDiscards", ip->ipRoutingDiscards); 2066 2067 prval("tcpInErrs", ip->tcpInErrs); 2068 prval("udpNoPorts", ip->udpNoPorts); 2069 prval("udpInCksumErrs", ip->udpInCksumErrs); 2070 prval("udpInOverflows", ip->udpInOverflows); 2071 prval("rawipInOverflows", ip->rawipInOverflows); 2072 prval("ipsecInSucceeded", ip->ipsecInSucceeded); 2073 prval("ipsecInFailed", ip->ipsecInFailed); 2074 prval("ipInIPv6", ip->ipInIPv6); 2075 prval("ipOutIPv6", ip->ipOutIPv6); 2076 prval("ipOutSwitchIPv6", ip->ipOutSwitchIPv6); 2077 prval_end(); 2078 } 2079 2080 static void 2081 print_icmp_stats(mib2_icmp_t *icmp) 2082 { 2083 prval_init(); 2084 prval("icmpInMsgs", icmp->icmpInMsgs); 2085 prval("icmpInErrors", icmp->icmpInErrors); 2086 prval("icmpInCksumErrs", icmp->icmpInCksumErrs); 2087 prval("icmpInUnknowns", icmp->icmpInUnknowns); 2088 prval("icmpInDestUnreachs", icmp->icmpInDestUnreachs); 2089 prval("icmpInTimeExcds", icmp->icmpInTimeExcds); 2090 prval("icmpInParmProbs", icmp->icmpInParmProbs); 2091 prval("icmpInSrcQuenchs", icmp->icmpInSrcQuenchs); 2092 prval("icmpInRedirects", icmp->icmpInRedirects); 2093 prval("icmpInBadRedirects", icmp->icmpInBadRedirects); 2094 prval("icmpInEchos", icmp->icmpInEchos); 2095 prval("icmpInEchoReps", icmp->icmpInEchoReps); 2096 prval("icmpInTimestamps", icmp->icmpInTimestamps); 2097 prval("icmpInTimestampReps", icmp->icmpInTimestampReps); 2098 prval("icmpInAddrMasks", icmp->icmpInAddrMasks); 2099 prval("icmpInAddrMaskReps", icmp->icmpInAddrMaskReps); 2100 prval("icmpInFragNeeded", icmp->icmpInFragNeeded); 2101 prval("icmpOutMsgs", icmp->icmpOutMsgs); 2102 prval("icmpOutDrops", icmp->icmpOutDrops); 2103 prval("icmpOutErrors", icmp->icmpOutErrors); 2104 prval("icmpOutDestUnreachs", icmp->icmpOutDestUnreachs); 2105 prval("icmpOutTimeExcds", icmp->icmpOutTimeExcds); 2106 prval("icmpOutParmProbs", icmp->icmpOutParmProbs); 2107 prval("icmpOutSrcQuenchs", icmp->icmpOutSrcQuenchs); 2108 prval("icmpOutRedirects", icmp->icmpOutRedirects); 2109 prval("icmpOutEchos", icmp->icmpOutEchos); 2110 prval("icmpOutEchoReps", icmp->icmpOutEchoReps); 2111 prval("icmpOutTimestamps", icmp->icmpOutTimestamps); 2112 prval("icmpOutTimestampReps", icmp->icmpOutTimestampReps); 2113 prval("icmpOutAddrMasks", icmp->icmpOutAddrMasks); 2114 prval("icmpOutAddrMaskReps", icmp->icmpOutAddrMaskReps); 2115 prval("icmpOutFragNeeded", icmp->icmpOutFragNeeded); 2116 prval("icmpInOverflows", icmp->icmpInOverflows); 2117 prval_end(); 2118 } 2119 2120 static void 2121 print_ip6_stats(mib2_ipv6IfStatsEntry_t *ip6) 2122 { 2123 prval_init(); 2124 prval("ipv6Forwarding", ip6->ipv6Forwarding); 2125 prval("ipv6DefaultHopLimit", ip6->ipv6DefaultHopLimit); 2126 2127 prval("ipv6InReceives", ip6->ipv6InReceives); 2128 prval("ipv6InHdrErrors", ip6->ipv6InHdrErrors); 2129 prval("ipv6InTooBigErrors", ip6->ipv6InTooBigErrors); 2130 prval("ipv6InNoRoutes", ip6->ipv6InNoRoutes); 2131 prval("ipv6InAddrErrors", ip6->ipv6InAddrErrors); 2132 prval("ipv6InUnknownProtos", ip6->ipv6InUnknownProtos); 2133 prval("ipv6InTruncatedPkts", ip6->ipv6InTruncatedPkts); 2134 prval("ipv6InDiscards", ip6->ipv6InDiscards); 2135 prval("ipv6InDelivers", ip6->ipv6InDelivers); 2136 prval("ipv6OutForwDatagrams", ip6->ipv6OutForwDatagrams); 2137 prval("ipv6OutRequests", ip6->ipv6OutRequests); 2138 prval("ipv6OutDiscards", ip6->ipv6OutDiscards); 2139 prval("ipv6OutNoRoutes", ip6->ipv6OutNoRoutes); 2140 prval("ipv6OutFragOKs", ip6->ipv6OutFragOKs); 2141 prval("ipv6OutFragFails", ip6->ipv6OutFragFails); 2142 prval("ipv6OutFragCreates", ip6->ipv6OutFragCreates); 2143 prval("ipv6ReasmReqds", ip6->ipv6ReasmReqds); 2144 prval("ipv6ReasmOKs", ip6->ipv6ReasmOKs); 2145 prval("ipv6ReasmFails", ip6->ipv6ReasmFails); 2146 prval("ipv6InMcastPkts", ip6->ipv6InMcastPkts); 2147 prval("ipv6OutMcastPkts", ip6->ipv6OutMcastPkts); 2148 prval("ipv6ReasmDuplicates", ip6->ipv6ReasmDuplicates); 2149 prval("ipv6ReasmPartDups", ip6->ipv6ReasmPartDups); 2150 prval("ipv6ForwProhibits", ip6->ipv6ForwProhibits); 2151 prval("udpInCksumErrs", ip6->udpInCksumErrs); 2152 prval("udpInOverflows", ip6->udpInOverflows); 2153 prval("rawipInOverflows", ip6->rawipInOverflows); 2154 prval("ipv6InIPv4", ip6->ipv6InIPv4); 2155 prval("ipv6OutIPv4", ip6->ipv6OutIPv4); 2156 prval("ipv6OutSwitchIPv4", ip6->ipv6OutSwitchIPv4); 2157 prval_end(); 2158 } 2159 2160 static void 2161 print_icmp6_stats(mib2_ipv6IfIcmpEntry_t *icmp6) 2162 { 2163 prval_init(); 2164 prval("icmp6InMsgs", icmp6->ipv6IfIcmpInMsgs); 2165 prval("icmp6InErrors", icmp6->ipv6IfIcmpInErrors); 2166 prval("icmp6InDestUnreachs", icmp6->ipv6IfIcmpInDestUnreachs); 2167 prval("icmp6InAdminProhibs", icmp6->ipv6IfIcmpInAdminProhibs); 2168 prval("icmp6InTimeExcds", icmp6->ipv6IfIcmpInTimeExcds); 2169 prval("icmp6InParmProblems", icmp6->ipv6IfIcmpInParmProblems); 2170 prval("icmp6InPktTooBigs", icmp6->ipv6IfIcmpInPktTooBigs); 2171 prval("icmp6InEchos", icmp6->ipv6IfIcmpInEchos); 2172 prval("icmp6InEchoReplies", icmp6->ipv6IfIcmpInEchoReplies); 2173 prval("icmp6InRouterSols", icmp6->ipv6IfIcmpInRouterSolicits); 2174 prval("icmp6InRouterAds", 2175 icmp6->ipv6IfIcmpInRouterAdvertisements); 2176 prval("icmp6InNeighborSols", icmp6->ipv6IfIcmpInNeighborSolicits); 2177 prval("icmp6InNeighborAds", 2178 icmp6->ipv6IfIcmpInNeighborAdvertisements); 2179 prval("icmp6InRedirects", icmp6->ipv6IfIcmpInRedirects); 2180 prval("icmp6InBadRedirects", icmp6->ipv6IfIcmpInBadRedirects); 2181 prval("icmp6InGroupQueries", icmp6->ipv6IfIcmpInGroupMembQueries); 2182 prval("icmp6InGroupResps", icmp6->ipv6IfIcmpInGroupMembResponses); 2183 prval("icmp6InGroupReds", icmp6->ipv6IfIcmpInGroupMembReductions); 2184 prval("icmp6InOverflows", icmp6->ipv6IfIcmpInOverflows); 2185 prval_end(); 2186 prval_init(); 2187 prval("icmp6OutMsgs", icmp6->ipv6IfIcmpOutMsgs); 2188 prval("icmp6OutErrors", icmp6->ipv6IfIcmpOutErrors); 2189 prval("icmp6OutDestUnreachs", icmp6->ipv6IfIcmpOutDestUnreachs); 2190 prval("icmp6OutAdminProhibs", icmp6->ipv6IfIcmpOutAdminProhibs); 2191 prval("icmp6OutTimeExcds", icmp6->ipv6IfIcmpOutTimeExcds); 2192 prval("icmp6OutParmProblems", icmp6->ipv6IfIcmpOutParmProblems); 2193 prval("icmp6OutPktTooBigs", icmp6->ipv6IfIcmpOutPktTooBigs); 2194 prval("icmp6OutEchos", icmp6->ipv6IfIcmpOutEchos); 2195 prval("icmp6OutEchoReplies", icmp6->ipv6IfIcmpOutEchoReplies); 2196 prval("icmp6OutRouterSols", icmp6->ipv6IfIcmpOutRouterSolicits); 2197 prval("icmp6OutRouterAds", 2198 icmp6->ipv6IfIcmpOutRouterAdvertisements); 2199 prval("icmp6OutNeighborSols", icmp6->ipv6IfIcmpOutNeighborSolicits); 2200 prval("icmp6OutNeighborAds", 2201 icmp6->ipv6IfIcmpOutNeighborAdvertisements); 2202 prval("icmp6OutRedirects", icmp6->ipv6IfIcmpOutRedirects); 2203 prval("icmp6OutGroupQueries", icmp6->ipv6IfIcmpOutGroupMembQueries); 2204 prval("icmp6OutGroupResps", 2205 icmp6->ipv6IfIcmpOutGroupMembResponses); 2206 prval("icmp6OutGroupReds", 2207 icmp6->ipv6IfIcmpOutGroupMembReductions); 2208 prval_end(); 2209 } 2210 2211 static void 2212 print_sctp_stats(mib2_sctp_t *sctp) 2213 { 2214 prval_init(); 2215 pr_sctp_rtoalgo("sctpRtoAlgorithm", sctp->sctpRtoAlgorithm); 2216 prval("sctpRtoMin", sctp->sctpRtoMin); 2217 prval("sctpRtoMax", sctp->sctpRtoMax); 2218 prval("sctpRtoInitial", sctp->sctpRtoInitial); 2219 pr_int_val("sctpMaxAssocs", sctp->sctpMaxAssocs); 2220 prval("sctpValCookieLife", sctp->sctpValCookieLife); 2221 prval("sctpMaxInitRetr", sctp->sctpMaxInitRetr); 2222 prval("sctpCurrEstab", sctp->sctpCurrEstab); 2223 prval("sctpActiveEstab", sctp->sctpActiveEstab); 2224 prval("sctpPassiveEstab", sctp->sctpPassiveEstab); 2225 prval("sctpAborted", sctp->sctpAborted); 2226 prval("sctpShutdowns", sctp->sctpShutdowns); 2227 prval("sctpOutOfBlue", sctp->sctpOutOfBlue); 2228 prval("sctpChecksumError", sctp->sctpChecksumError); 2229 prval64("sctpOutCtrlChunks", sctp->sctpOutCtrlChunks); 2230 prval64("sctpOutOrderChunks", sctp->sctpOutOrderChunks); 2231 prval64("sctpOutUnorderChunks", sctp->sctpOutUnorderChunks); 2232 prval64("sctpRetransChunks", sctp->sctpRetransChunks); 2233 prval("sctpOutAck", sctp->sctpOutAck); 2234 prval("sctpOutAckDelayed", sctp->sctpOutAckDelayed); 2235 prval("sctpOutWinUpdate", sctp->sctpOutWinUpdate); 2236 prval("sctpOutFastRetrans", sctp->sctpOutFastRetrans); 2237 prval("sctpOutWinProbe", sctp->sctpOutWinProbe); 2238 prval64("sctpInCtrlChunks", sctp->sctpInCtrlChunks); 2239 prval64("sctpInOrderChunks", sctp->sctpInOrderChunks); 2240 prval64("sctpInUnorderChunks", sctp->sctpInUnorderChunks); 2241 prval("sctpInAck", sctp->sctpInAck); 2242 prval("sctpInDupAck", sctp->sctpInDupAck); 2243 prval("sctpInAckUnsent", sctp->sctpInAckUnsent); 2244 prval64("sctpFragUsrMsgs", sctp->sctpFragUsrMsgs); 2245 prval64("sctpReasmUsrMsgs", sctp->sctpReasmUsrMsgs); 2246 prval64("sctpOutSCTPPkts", sctp->sctpOutSCTPPkts); 2247 prval64("sctpInSCTPPkts", sctp->sctpInSCTPPkts); 2248 prval("sctpInInvalidCookie", sctp->sctpInInvalidCookie); 2249 prval("sctpTimRetrans", sctp->sctpTimRetrans); 2250 prval("sctpTimRetransDrop", sctp->sctpTimRetransDrop); 2251 prval("sctpTimHearBeatProbe", sctp->sctpTimHeartBeatProbe); 2252 prval("sctpTimHearBeatDrop", sctp->sctpTimHeartBeatDrop); 2253 prval("sctpListenDrop", sctp->sctpListenDrop); 2254 prval("sctpInClosed", sctp->sctpInClosed); 2255 prval_end(); 2256 } 2257 2258 static void 2259 print_tcp_stats(mib2_tcp_t *tcp) 2260 { 2261 prval_init(); 2262 pr_int_val("tcpRtoAlgorithm", tcp->tcpRtoAlgorithm); 2263 pr_int_val("tcpRtoMin", tcp->tcpRtoMin); 2264 pr_int_val("tcpRtoMax", tcp->tcpRtoMax); 2265 pr_int_val("tcpMaxConn", tcp->tcpMaxConn); 2266 prval("tcpActiveOpens", tcp->tcpActiveOpens); 2267 prval("tcpPassiveOpens", tcp->tcpPassiveOpens); 2268 prval("tcpAttemptFails", tcp->tcpAttemptFails); 2269 prval("tcpEstabResets", tcp->tcpEstabResets); 2270 prval("tcpCurrEstab", tcp->tcpCurrEstab); 2271 prval64("tcpOutSegs", tcp->tcpHCOutSegs); 2272 prval("tcpOutDataSegs", tcp->tcpOutDataSegs); 2273 prval("tcpOutDataBytes", tcp->tcpOutDataBytes); 2274 prval("tcpRetransSegs", tcp->tcpRetransSegs); 2275 prval("tcpRetransBytes", tcp->tcpRetransBytes); 2276 prval("tcpOutAck", tcp->tcpOutAck); 2277 prval("tcpOutAckDelayed", tcp->tcpOutAckDelayed); 2278 prval("tcpOutUrg", tcp->tcpOutUrg); 2279 prval("tcpOutWinUpdate", tcp->tcpOutWinUpdate); 2280 prval("tcpOutWinProbe", tcp->tcpOutWinProbe); 2281 prval("tcpOutControl", tcp->tcpOutControl); 2282 prval("tcpOutRsts", tcp->tcpOutRsts); 2283 prval("tcpOutFastRetrans", tcp->tcpOutFastRetrans); 2284 prval64("tcpInSegs", tcp->tcpHCInSegs); 2285 prval_end(); 2286 prval("tcpInAckSegs", tcp->tcpInAckSegs); 2287 prval("tcpInAckBytes", tcp->tcpInAckBytes); 2288 prval("tcpInDupAck", tcp->tcpInDupAck); 2289 prval("tcpInAckUnsent", tcp->tcpInAckUnsent); 2290 prval("tcpInInorderSegs", tcp->tcpInDataInorderSegs); 2291 prval("tcpInInorderBytes", tcp->tcpInDataInorderBytes); 2292 prval("tcpInUnorderSegs", tcp->tcpInDataUnorderSegs); 2293 prval("tcpInUnorderBytes", tcp->tcpInDataUnorderBytes); 2294 prval("tcpInDupSegs", tcp->tcpInDataDupSegs); 2295 prval("tcpInDupBytes", tcp->tcpInDataDupBytes); 2296 prval("tcpInPartDupSegs", tcp->tcpInDataPartDupSegs); 2297 prval("tcpInPartDupBytes", tcp->tcpInDataPartDupBytes); 2298 prval("tcpInPastWinSegs", tcp->tcpInDataPastWinSegs); 2299 prval("tcpInPastWinBytes", tcp->tcpInDataPastWinBytes); 2300 prval("tcpInWinProbe", tcp->tcpInWinProbe); 2301 prval("tcpInWinUpdate", tcp->tcpInWinUpdate); 2302 prval("tcpInClosed", tcp->tcpInClosed); 2303 prval("tcpRttNoUpdate", tcp->tcpRttNoUpdate); 2304 prval("tcpRttUpdate", tcp->tcpRttUpdate); 2305 prval("tcpTimRetrans", tcp->tcpTimRetrans); 2306 prval("tcpTimRetransDrop", tcp->tcpTimRetransDrop); 2307 prval("tcpTimKeepalive", tcp->tcpTimKeepalive); 2308 prval("tcpTimKeepaliveProbe", tcp->tcpTimKeepaliveProbe); 2309 prval("tcpTimKeepaliveDrop", tcp->tcpTimKeepaliveDrop); 2310 prval("tcpListenDrop", tcp->tcpListenDrop); 2311 prval("tcpListenDropQ0", tcp->tcpListenDropQ0); 2312 prval("tcpHalfOpenDrop", tcp->tcpHalfOpenDrop); 2313 prval("tcpOutSackRetrans", tcp->tcpOutSackRetransSegs); 2314 prval_end(); 2315 2316 } 2317 2318 static void 2319 print_udp_stats(mib2_udp_t *udp) 2320 { 2321 prval_init(); 2322 prval64("udpInDatagrams", udp->udpHCInDatagrams); 2323 prval("udpInErrors", udp->udpInErrors); 2324 prval64("udpOutDatagrams", udp->udpHCOutDatagrams); 2325 prval("udpOutErrors", udp->udpOutErrors); 2326 prval_end(); 2327 } 2328 2329 static void 2330 print_rawip_stats(mib2_rawip_t *rawip) 2331 { 2332 prval_init(); 2333 prval("rawipInDatagrams", rawip->rawipInDatagrams); 2334 prval("rawipInErrors", rawip->rawipInErrors); 2335 prval("rawipInCksumErrs", rawip->rawipInCksumErrs); 2336 prval("rawipOutDatagrams", rawip->rawipOutDatagrams); 2337 prval("rawipOutErrors", rawip->rawipOutErrors); 2338 prval_end(); 2339 } 2340 2341 void 2342 print_igmp_stats(struct igmpstat *igps) 2343 { 2344 (void) printf(" %10u message%s received\n", 2345 igps->igps_rcv_total, PLURAL(igps->igps_rcv_total)); 2346 (void) printf(" %10u message%s received with too few bytes\n", 2347 igps->igps_rcv_tooshort, PLURAL(igps->igps_rcv_tooshort)); 2348 (void) printf(" %10u message%s received with bad checksum\n", 2349 igps->igps_rcv_badsum, PLURAL(igps->igps_rcv_badsum)); 2350 (void) printf(" %10u membership quer%s received\n", 2351 igps->igps_rcv_queries, PLURALY(igps->igps_rcv_queries)); 2352 (void) printf(" %10u membership quer%s received with invalid " 2353 "field(s)\n", 2354 igps->igps_rcv_badqueries, PLURALY(igps->igps_rcv_badqueries)); 2355 (void) printf(" %10u membership report%s received\n", 2356 igps->igps_rcv_reports, PLURAL(igps->igps_rcv_reports)); 2357 (void) printf(" %10u membership report%s received with invalid " 2358 "field(s)\n", 2359 igps->igps_rcv_badreports, PLURAL(igps->igps_rcv_badreports)); 2360 (void) printf(" %10u membership report%s received for groups to " 2361 "which we belong\n", 2362 igps->igps_rcv_ourreports, PLURAL(igps->igps_rcv_ourreports)); 2363 (void) printf(" %10u membership report%s sent\n", 2364 igps->igps_snd_reports, PLURAL(igps->igps_snd_reports)); 2365 } 2366 2367 static void 2368 print_mrt_stats(struct mrtstat *mrts) 2369 { 2370 (void) puts("DVMRP multicast routing:"); 2371 (void) printf(" %10u hit%s - kernel forwarding cache hits\n", 2372 mrts->mrts_mfc_hits, PLURAL(mrts->mrts_mfc_hits)); 2373 (void) printf(" %10u miss%s - kernel forwarding cache misses\n", 2374 mrts->mrts_mfc_misses, PLURALES(mrts->mrts_mfc_misses)); 2375 (void) printf(" %10u packet%s potentially forwarded\n", 2376 mrts->mrts_fwd_in, PLURAL(mrts->mrts_fwd_in)); 2377 (void) printf(" %10u packet%s actually sent out\n", 2378 mrts->mrts_fwd_out, PLURAL(mrts->mrts_fwd_out)); 2379 (void) printf(" %10u upcall%s - upcalls made to mrouted\n", 2380 mrts->mrts_upcalls, PLURAL(mrts->mrts_upcalls)); 2381 (void) printf(" %10u packet%s not sent out due to lack of resources\n", 2382 mrts->mrts_fwd_drop, PLURAL(mrts->mrts_fwd_drop)); 2383 (void) printf(" %10u datagram%s with malformed tunnel options\n", 2384 mrts->mrts_bad_tunnel, PLURAL(mrts->mrts_bad_tunnel)); 2385 (void) printf(" %10u datagram%s with no room for tunnel options\n", 2386 mrts->mrts_cant_tunnel, PLURAL(mrts->mrts_cant_tunnel)); 2387 (void) printf(" %10u datagram%s arrived on wrong interface\n", 2388 mrts->mrts_wrong_if, PLURAL(mrts->mrts_wrong_if)); 2389 (void) printf(" %10u datagram%s dropped due to upcall Q overflow\n", 2390 mrts->mrts_upq_ovflw, PLURAL(mrts->mrts_upq_ovflw)); 2391 (void) printf(" %10u datagram%s cleaned up by the cache\n", 2392 mrts->mrts_cache_cleanups, PLURAL(mrts->mrts_cache_cleanups)); 2393 (void) printf(" %10u datagram%s dropped selectively by ratelimiter\n", 2394 mrts->mrts_drop_sel, PLURAL(mrts->mrts_drop_sel)); 2395 (void) printf(" %10u datagram%s dropped - bucket Q overflow\n", 2396 mrts->mrts_q_overflow, PLURAL(mrts->mrts_q_overflow)); 2397 (void) printf(" %10u datagram%s dropped - larger than bkt size\n", 2398 mrts->mrts_pkt2large, PLURAL(mrts->mrts_pkt2large)); 2399 (void) printf("\nPIM multicast routing:\n"); 2400 (void) printf(" %10u datagram%s dropped - bad version number\n", 2401 mrts->mrts_pim_badversion, PLURAL(mrts->mrts_pim_badversion)); 2402 (void) printf(" %10u datagram%s dropped - bad checksum\n", 2403 mrts->mrts_pim_rcv_badcsum, PLURAL(mrts->mrts_pim_rcv_badcsum)); 2404 (void) printf(" %10u datagram%s dropped - bad register packets\n", 2405 mrts->mrts_pim_badregisters, 2406 PLURAL(mrts->mrts_pim_badregisters)); 2407 (void) printf( 2408 " %10u datagram%s potentially forwarded - register packets\n", 2409 mrts->mrts_pim_regforwards, PLURAL(mrts->mrts_pim_regforwards)); 2410 (void) printf(" %10u datagram%s dropped - register send drops\n", 2411 mrts->mrts_pim_regsend_drops, 2412 PLURAL(mrts->mrts_pim_regsend_drops)); 2413 (void) printf(" %10u datagram%s dropped - packet malformed\n", 2414 mrts->mrts_pim_malformed, PLURAL(mrts->mrts_pim_malformed)); 2415 (void) printf(" %10u datagram%s dropped - no memory to forward\n", 2416 mrts->mrts_pim_nomemory, PLURAL(mrts->mrts_pim_nomemory)); 2417 } 2418 2419 static void 2420 sum_ip6_stats(mib2_ipv6IfStatsEntry_t *ip6, mib2_ipv6IfStatsEntry_t *sum6) 2421 { 2422 /* First few are not additive */ 2423 sum6->ipv6Forwarding = ip6->ipv6Forwarding; 2424 sum6->ipv6DefaultHopLimit = ip6->ipv6DefaultHopLimit; 2425 2426 sum6->ipv6InReceives += ip6->ipv6InReceives; 2427 sum6->ipv6InHdrErrors += ip6->ipv6InHdrErrors; 2428 sum6->ipv6InTooBigErrors += ip6->ipv6InTooBigErrors; 2429 sum6->ipv6InNoRoutes += ip6->ipv6InNoRoutes; 2430 sum6->ipv6InAddrErrors += ip6->ipv6InAddrErrors; 2431 sum6->ipv6InUnknownProtos += ip6->ipv6InUnknownProtos; 2432 sum6->ipv6InTruncatedPkts += ip6->ipv6InTruncatedPkts; 2433 sum6->ipv6InDiscards += ip6->ipv6InDiscards; 2434 sum6->ipv6InDelivers += ip6->ipv6InDelivers; 2435 sum6->ipv6OutForwDatagrams += ip6->ipv6OutForwDatagrams; 2436 sum6->ipv6OutRequests += ip6->ipv6OutRequests; 2437 sum6->ipv6OutDiscards += ip6->ipv6OutDiscards; 2438 sum6->ipv6OutFragOKs += ip6->ipv6OutFragOKs; 2439 sum6->ipv6OutFragFails += ip6->ipv6OutFragFails; 2440 sum6->ipv6OutFragCreates += ip6->ipv6OutFragCreates; 2441 sum6->ipv6ReasmReqds += ip6->ipv6ReasmReqds; 2442 sum6->ipv6ReasmOKs += ip6->ipv6ReasmOKs; 2443 sum6->ipv6ReasmFails += ip6->ipv6ReasmFails; 2444 sum6->ipv6InMcastPkts += ip6->ipv6InMcastPkts; 2445 sum6->ipv6OutMcastPkts += ip6->ipv6OutMcastPkts; 2446 sum6->ipv6OutNoRoutes += ip6->ipv6OutNoRoutes; 2447 sum6->ipv6ReasmDuplicates += ip6->ipv6ReasmDuplicates; 2448 sum6->ipv6ReasmPartDups += ip6->ipv6ReasmPartDups; 2449 sum6->ipv6ForwProhibits += ip6->ipv6ForwProhibits; 2450 sum6->udpInCksumErrs += ip6->udpInCksumErrs; 2451 sum6->udpInOverflows += ip6->udpInOverflows; 2452 sum6->rawipInOverflows += ip6->rawipInOverflows; 2453 } 2454 2455 static void 2456 sum_icmp6_stats(mib2_ipv6IfIcmpEntry_t *icmp6, mib2_ipv6IfIcmpEntry_t *sum6) 2457 { 2458 sum6->ipv6IfIcmpInMsgs += icmp6->ipv6IfIcmpInMsgs; 2459 sum6->ipv6IfIcmpInErrors += icmp6->ipv6IfIcmpInErrors; 2460 sum6->ipv6IfIcmpInDestUnreachs += icmp6->ipv6IfIcmpInDestUnreachs; 2461 sum6->ipv6IfIcmpInAdminProhibs += icmp6->ipv6IfIcmpInAdminProhibs; 2462 sum6->ipv6IfIcmpInTimeExcds += icmp6->ipv6IfIcmpInTimeExcds; 2463 sum6->ipv6IfIcmpInParmProblems += icmp6->ipv6IfIcmpInParmProblems; 2464 sum6->ipv6IfIcmpInPktTooBigs += icmp6->ipv6IfIcmpInPktTooBigs; 2465 sum6->ipv6IfIcmpInEchos += icmp6->ipv6IfIcmpInEchos; 2466 sum6->ipv6IfIcmpInEchoReplies += icmp6->ipv6IfIcmpInEchoReplies; 2467 sum6->ipv6IfIcmpInRouterSolicits += icmp6->ipv6IfIcmpInRouterSolicits; 2468 sum6->ipv6IfIcmpInRouterAdvertisements += 2469 icmp6->ipv6IfIcmpInRouterAdvertisements; 2470 sum6->ipv6IfIcmpInNeighborSolicits += 2471 icmp6->ipv6IfIcmpInNeighborSolicits; 2472 sum6->ipv6IfIcmpInNeighborAdvertisements += 2473 icmp6->ipv6IfIcmpInNeighborAdvertisements; 2474 sum6->ipv6IfIcmpInRedirects += icmp6->ipv6IfIcmpInRedirects; 2475 sum6->ipv6IfIcmpInGroupMembQueries += 2476 icmp6->ipv6IfIcmpInGroupMembQueries; 2477 sum6->ipv6IfIcmpInGroupMembResponses += 2478 icmp6->ipv6IfIcmpInGroupMembResponses; 2479 sum6->ipv6IfIcmpInGroupMembReductions += 2480 icmp6->ipv6IfIcmpInGroupMembReductions; 2481 sum6->ipv6IfIcmpOutMsgs += icmp6->ipv6IfIcmpOutMsgs; 2482 sum6->ipv6IfIcmpOutErrors += icmp6->ipv6IfIcmpOutErrors; 2483 sum6->ipv6IfIcmpOutDestUnreachs += icmp6->ipv6IfIcmpOutDestUnreachs; 2484 sum6->ipv6IfIcmpOutAdminProhibs += icmp6->ipv6IfIcmpOutAdminProhibs; 2485 sum6->ipv6IfIcmpOutTimeExcds += icmp6->ipv6IfIcmpOutTimeExcds; 2486 sum6->ipv6IfIcmpOutParmProblems += icmp6->ipv6IfIcmpOutParmProblems; 2487 sum6->ipv6IfIcmpOutPktTooBigs += icmp6->ipv6IfIcmpOutPktTooBigs; 2488 sum6->ipv6IfIcmpOutEchos += icmp6->ipv6IfIcmpOutEchos; 2489 sum6->ipv6IfIcmpOutEchoReplies += icmp6->ipv6IfIcmpOutEchoReplies; 2490 sum6->ipv6IfIcmpOutRouterSolicits += 2491 icmp6->ipv6IfIcmpOutRouterSolicits; 2492 sum6->ipv6IfIcmpOutRouterAdvertisements += 2493 icmp6->ipv6IfIcmpOutRouterAdvertisements; 2494 sum6->ipv6IfIcmpOutNeighborSolicits += 2495 icmp6->ipv6IfIcmpOutNeighborSolicits; 2496 sum6->ipv6IfIcmpOutNeighborAdvertisements += 2497 icmp6->ipv6IfIcmpOutNeighborAdvertisements; 2498 sum6->ipv6IfIcmpOutRedirects += icmp6->ipv6IfIcmpOutRedirects; 2499 sum6->ipv6IfIcmpOutGroupMembQueries += 2500 icmp6->ipv6IfIcmpOutGroupMembQueries; 2501 sum6->ipv6IfIcmpOutGroupMembResponses += 2502 icmp6->ipv6IfIcmpOutGroupMembResponses; 2503 sum6->ipv6IfIcmpOutGroupMembReductions += 2504 icmp6->ipv6IfIcmpOutGroupMembReductions; 2505 sum6->ipv6IfIcmpInOverflows += icmp6->ipv6IfIcmpInOverflows; 2506 } 2507 2508 /* ----------------------------- MRT_STAT_REPORT --------------------------- */ 2509 2510 static void 2511 mrt_stat_report(mib_item_t *curritem) 2512 { 2513 int jtemp = 0; 2514 mib_item_t *tempitem; 2515 2516 if (!(family_selected(AF_INET))) 2517 return; 2518 2519 (void) putchar('\n'); 2520 /* 'for' loop 1: */ 2521 for (tempitem = curritem; 2522 tempitem; 2523 tempitem = tempitem->next_item) { 2524 if (Dflag) { 2525 (void) printf("\n--- Entry %d ---\n", ++jtemp); 2526 (void) printf("Group = %d, mib_id = %d, " 2527 "length = %d, valp = 0x%p\n", 2528 tempitem->group, tempitem->mib_id, 2529 tempitem->length, tempitem->valp); 2530 } 2531 2532 if (tempitem->mib_id == 0) { 2533 switch (tempitem->group) { 2534 case EXPER_DVMRP: { 2535 struct mrtstat *mrts; 2536 mrts = (struct mrtstat *)tempitem->valp; 2537 2538 if (!(family_selected(AF_INET))) 2539 continue; /* 'for' loop 1 */ 2540 2541 print_mrt_stats(mrts); 2542 break; 2543 } 2544 } 2545 } 2546 } /* 'for' loop 1 ends */ 2547 (void) putchar('\n'); 2548 (void) fflush(stdout); 2549 } 2550 2551 /* 2552 * if_stat_total() - Computes totals for interface statistics 2553 * and returns result by updating sumstats. 2554 */ 2555 static void 2556 if_stat_total(struct ifstat *oldstats, struct ifstat *newstats, 2557 struct ifstat *sumstats) 2558 { 2559 sumstats->ipackets += newstats->ipackets - oldstats->ipackets; 2560 sumstats->opackets += newstats->opackets - oldstats->opackets; 2561 sumstats->ierrors += newstats->ierrors - oldstats->ierrors; 2562 sumstats->oerrors += newstats->oerrors - oldstats->oerrors; 2563 sumstats->collisions += newstats->collisions - oldstats->collisions; 2564 } 2565 2566 /* --------------------- IF_REPORT (netstat -i) -------------------------- */ 2567 2568 static struct ifstat zerostat = { 2569 0LL, 0LL, 0LL, 0LL, 0LL 2570 }; 2571 2572 static void 2573 if_report(mib_item_t *item, char *matchname, 2574 int Iflag_only, boolean_t once_only) 2575 { 2576 static boolean_t reentry = B_FALSE; 2577 boolean_t alreadydone = B_FALSE; 2578 int jtemp = 0; 2579 uint32_t ifindex_v4 = 0; 2580 uint32_t ifindex_v6 = 0; 2581 boolean_t first_header = B_TRUE; 2582 2583 /* 'for' loop 1: */ 2584 for (; item; item = item->next_item) { 2585 if (Dflag) { 2586 (void) printf("\n--- Entry %d ---\n", ++jtemp); 2587 (void) printf("Group = %d, mib_id = %d, " 2588 "length = %d, valp = 0x%p\n", 2589 item->group, item->mib_id, item->length, 2590 item->valp); 2591 } 2592 2593 switch (item->group) { 2594 case MIB2_IP: 2595 if (item->mib_id != MIB2_IP_ADDR || 2596 !family_selected(AF_INET)) 2597 continue; /* 'for' loop 1 */ 2598 { 2599 static struct ifstat old = {0L, 0L, 0L, 0L, 0L}; 2600 static struct ifstat new = {0L, 0L, 0L, 0L, 0L}; 2601 struct ifstat sum; 2602 struct iflist *newlist = NULL; 2603 static struct iflist *oldlist = NULL; 2604 kstat_t *ksp; 2605 2606 if (once_only) { 2607 char ifname[LIFNAMSIZ + 1]; 2608 char logintname[LIFNAMSIZ + 1]; 2609 mib2_ipAddrEntry_t *ap; 2610 struct ifstat stat = {0L, 0L, 0L, 0L, 0L}; 2611 boolean_t first = B_TRUE; 2612 uint32_t new_ifindex; 2613 2614 if (Dflag) 2615 (void) printf("if_report: %d items\n", 2616 (item->length) 2617 / sizeof (mib2_ipAddrEntry_t)); 2618 2619 /* 'for' loop 2a: */ 2620 for (ap = (mib2_ipAddrEntry_t *)item->valp; 2621 (char *)ap < (char *)item->valp 2622 + item->length; 2623 ap++) { 2624 (void) octetstr(&ap->ipAdEntIfIndex, 2625 'a', logintname, 2626 sizeof (logintname)); 2627 (void) strcpy(ifname, logintname); 2628 (void) strtok(ifname, ":"); 2629 if (matchname != NULL && 2630 strcmp(matchname, ifname) != 0 && 2631 strcmp(matchname, logintname) != 0) 2632 continue; /* 'for' loop 2a */ 2633 new_ifindex = 2634 if_nametoindex(logintname); 2635 if (new_ifindex != ifindex_v4 && 2636 (ksp = kstat_lookup(kc, NULL, -1, 2637 ifname)) != NULL) { 2638 (void) safe_kstat_read(kc, ksp, 2639 NULL); 2640 stat.ipackets = 2641 kstat_named_value(ksp, 2642 "ipackets"); 2643 stat.ierrors = 2644 kstat_named_value(ksp, 2645 "ierrors"); 2646 stat.opackets = 2647 kstat_named_value(ksp, 2648 "opackets"); 2649 stat.oerrors = 2650 kstat_named_value(ksp, 2651 "oerrors"); 2652 stat.collisions = 2653 kstat_named_value(ksp, 2654 "collisions"); 2655 if (first) { 2656 if (!first_header) 2657 (void) putchar('\n'); 2658 first_header = B_FALSE; 2659 (void) printf( 2660 "%-5.5s %-5.5s%-13.13s " 2661 "%-14.14s %-6.6s %-5.5s " 2662 "%-6.6s %-5.5s %-6.6s " 2663 "%-6.6s\n", 2664 "Name", "Mtu", "Net/Dest", 2665 "Address", "Ipkts", 2666 "Ierrs", "Opkts", "Oerrs", 2667 "Collis", "Queue"); 2668 2669 first = B_FALSE; 2670 } 2671 if_report_ip4(ap, ifname, 2672 logintname, &stat, B_TRUE); 2673 ifindex_v4 = new_ifindex; 2674 } else { 2675 if_report_ip4(ap, ifname, 2676 logintname, &stat, B_FALSE); 2677 } 2678 } /* 'for' loop 2a ends */ 2679 } else if (!alreadydone) { 2680 char ifname[LIFNAMSIZ + 1]; 2681 char buf[LIFNAMSIZ + 1]; 2682 mib2_ipAddrEntry_t *ap; 2683 struct ifstat t; 2684 struct iflist *tlp = NULL; 2685 struct iflist **nextnew = &newlist; 2686 struct iflist *walkold; 2687 struct iflist *cleanlist; 2688 boolean_t found_if = B_FALSE; 2689 2690 alreadydone = B_TRUE; /* ignore other case */ 2691 2692 /* 2693 * Check if there is anything to do. 2694 */ 2695 if (item->length < 2696 sizeof (mib2_ipAddrEntry_t)) { 2697 fail(0, "No compatible interfaces"); 2698 } 2699 2700 /* 2701 * 'for' loop 2b: find the "right" entry: 2702 * If an interface name to match has been 2703 * supplied then try and find it, otherwise 2704 * match the first non-loopback interface found. 2705 * Use lo0 if all else fails. 2706 */ 2707 for (ap = (mib2_ipAddrEntry_t *)item->valp; 2708 (char *)ap < (char *)item->valp 2709 + item->length; 2710 ap++) { 2711 (void) octetstr(&ap->ipAdEntIfIndex, 2712 'a', ifname, sizeof (ifname)); 2713 (void) strtok(ifname, ":"); 2714 2715 if (matchname) { 2716 if (strcmp(matchname, 2717 ifname) == 0) { 2718 /* 'for' loop 2b */ 2719 found_if = B_TRUE; 2720 break; 2721 } 2722 } else if (strcmp(ifname, "lo0") != 0) 2723 break; /* 'for' loop 2b */ 2724 } /* 'for' loop 2b ends */ 2725 2726 if (matchname == NULL) { 2727 matchname = ifname; 2728 } else { 2729 if (!found_if) 2730 fail(0, "-I: %s no such " 2731 "interface.", matchname); 2732 } 2733 2734 if (Iflag_only == 0 || !reentry) { 2735 (void) printf(" input %-6.6s " 2736 "output ", 2737 matchname); 2738 (void) printf(" input (Total) " 2739 "output\n"); 2740 (void) printf("%-7.7s %-5.5s %-7.7s " 2741 "%-5.5s %-6.6s ", 2742 "packets", "errs", "packets", 2743 "errs", "colls"); 2744 (void) printf("%-7.7s %-5.5s %-7.7s " 2745 "%-5.5s %-6.6s\n", 2746 "packets", "errs", "packets", 2747 "errs", "colls"); 2748 } 2749 2750 sum = zerostat; 2751 2752 /* 'for' loop 2c: */ 2753 for (ap = (mib2_ipAddrEntry_t *)item->valp; 2754 (char *)ap < (char *)item->valp 2755 + item->length; 2756 ap++) { 2757 (void) octetstr(&ap->ipAdEntIfIndex, 2758 'a', buf, sizeof (buf)); 2759 (void) strtok(buf, ":"); 2760 2761 /* 2762 * We have reduced the IP interface 2763 * name, which could have been a 2764 * logical, down to a name suitable 2765 * for use with kstats. 2766 * We treat this name as unique and 2767 * only collate statistics for it once 2768 * per pass. This is to avoid falsely 2769 * amplifying these statistics by the 2770 * the number of logical instances. 2771 */ 2772 if ((tlp != NULL) && 2773 ((strcmp(buf, tlp->ifname) == 0))) { 2774 continue; 2775 } 2776 2777 ksp = kstat_lookup(kc, NULL, -1, buf); 2778 if (ksp && 2779 ksp->ks_type == KSTAT_TYPE_NAMED) 2780 (void) safe_kstat_read(kc, ksp, 2781 NULL); 2782 2783 t.ipackets = kstat_named_value(ksp, 2784 "ipackets"); 2785 t.ierrors = kstat_named_value(ksp, 2786 "ierrors"); 2787 t.opackets = kstat_named_value(ksp, 2788 "opackets"); 2789 t.oerrors = kstat_named_value(ksp, 2790 "oerrors"); 2791 t.collisions = kstat_named_value(ksp, 2792 "collisions"); 2793 2794 if (strcmp(buf, matchname) == 0) 2795 new = t; 2796 2797 /* Build the interface list */ 2798 2799 tlp = malloc(sizeof (struct iflist)); 2800 (void) strlcpy(tlp->ifname, buf, 2801 sizeof (tlp->ifname)); 2802 tlp->tot = t; 2803 *nextnew = tlp; 2804 nextnew = &tlp->next_if; 2805 2806 /* 2807 * First time through. 2808 * Just add up the interface stats. 2809 */ 2810 2811 if (oldlist == NULL) { 2812 if_stat_total(&zerostat, 2813 &t, &sum); 2814 continue; 2815 } 2816 2817 /* 2818 * Walk old list for the interface. 2819 * 2820 * If found, add difference to total. 2821 * 2822 * If not, an interface has been plumbed 2823 * up. In this case, we will simply 2824 * ignore the new interface until the 2825 * next interval; as there's no easy way 2826 * to acquire statistics between time 2827 * of the plumb and the next interval 2828 * boundary. This results in inaccurate 2829 * total values for current interval. 2830 * 2831 * Note the case when an interface is 2832 * unplumbed; as similar problems exist. 2833 * The unplumbed interface is not in the 2834 * current list, and there's no easy way 2835 * to account for the statistics between 2836 * the previous interval and time of the 2837 * unplumb. Therefore, we (in a sense) 2838 * ignore the removed interface by only 2839 * involving "current" interfaces when 2840 * computing the total statistics. 2841 * Unfortunately, this also results in 2842 * inaccurate values for interval total. 2843 */ 2844 2845 for (walkold = oldlist; 2846 walkold != NULL; 2847 walkold = walkold->next_if) { 2848 if (strcmp(walkold->ifname, 2849 buf) == 0) { 2850 if_stat_total( 2851 &walkold->tot, 2852 &t, &sum); 2853 break; 2854 } 2855 } 2856 2857 } /* 'for' loop 2c ends */ 2858 2859 *nextnew = NULL; 2860 2861 (void) printf("%-7llu %-5llu %-7llu " 2862 "%-5llu %-6llu ", 2863 new.ipackets - old.ipackets, 2864 new.ierrors - old.ierrors, 2865 new.opackets - old.opackets, 2866 new.oerrors - old.oerrors, 2867 new.collisions - old.collisions); 2868 2869 (void) printf("%-7llu %-5llu %-7llu " 2870 "%-5llu %-6llu\n", sum.ipackets, 2871 sum.ierrors, sum.opackets, 2872 sum.oerrors, sum.collisions); 2873 2874 /* 2875 * Tidy things up once finished. 2876 */ 2877 2878 old = new; 2879 cleanlist = oldlist; 2880 oldlist = newlist; 2881 while (cleanlist != NULL) { 2882 tlp = cleanlist->next_if; 2883 free(cleanlist); 2884 cleanlist = tlp; 2885 } 2886 } 2887 break; 2888 } 2889 case MIB2_IP6: 2890 if (item->mib_id != MIB2_IP6_ADDR || 2891 !family_selected(AF_INET6)) 2892 continue; /* 'for' loop 1 */ 2893 { 2894 static struct ifstat old6 = {0L, 0L, 0L, 0L, 0L}; 2895 static struct ifstat new6 = {0L, 0L, 0L, 0L, 0L}; 2896 struct ifstat sum6; 2897 struct iflist *newlist6 = NULL; 2898 static struct iflist *oldlist6 = NULL; 2899 kstat_t *ksp; 2900 2901 if (once_only) { 2902 char ifname[LIFNAMSIZ + 1]; 2903 char logintname[LIFNAMSIZ + 1]; 2904 mib2_ipv6AddrEntry_t *ap6; 2905 struct ifstat stat = {0L, 0L, 0L, 0L, 0L}; 2906 boolean_t first = B_TRUE; 2907 uint32_t new_ifindex; 2908 2909 if (Dflag) 2910 (void) printf("if_report: %d items\n", 2911 (item->length) 2912 / sizeof (mib2_ipv6AddrEntry_t)); 2913 /* 'for' loop 2d: */ 2914 for (ap6 = (mib2_ipv6AddrEntry_t *)item->valp; 2915 (char *)ap6 < (char *)item->valp 2916 + item->length; 2917 ap6++) { 2918 (void) octetstr(&ap6->ipv6AddrIfIndex, 2919 'a', logintname, 2920 sizeof (logintname)); 2921 (void) strcpy(ifname, logintname); 2922 (void) strtok(ifname, ":"); 2923 if (matchname != NULL && 2924 strcmp(matchname, ifname) != 0 && 2925 strcmp(matchname, logintname) != 0) 2926 continue; /* 'for' loop 2d */ 2927 new_ifindex = 2928 if_nametoindex(logintname); 2929 if (new_ifindex != ifindex_v6 && 2930 (ksp = kstat_lookup(kc, NULL, -1, 2931 ifname)) != NULL) { 2932 (void) safe_kstat_read(kc, ksp, 2933 NULL); 2934 stat.ipackets = 2935 kstat_named_value(ksp, 2936 "ipackets"); 2937 stat.ierrors = 2938 kstat_named_value(ksp, 2939 "ierrors"); 2940 stat.opackets = 2941 kstat_named_value(ksp, 2942 "opackets"); 2943 stat.oerrors = 2944 kstat_named_value(ksp, 2945 "oerrors"); 2946 stat.collisions = 2947 kstat_named_value(ksp, 2948 "collisions"); 2949 if (first) { 2950 if (!first_header) 2951 (void) putchar('\n'); 2952 first_header = B_FALSE; 2953 (void) printf( 2954 "%-5.5s %-5.5s%" 2955 "-27.27s %-27.27s " 2956 "%-6.6s %-5.5s " 2957 "%-6.6s %-5.5s " 2958 "%-6.6s\n", 2959 "Name", "Mtu", 2960 "Net/Dest", 2961 "Address", "Ipkts", 2962 "Ierrs", "Opkts", 2963 "Oerrs", "Collis"); 2964 first = B_FALSE; 2965 } 2966 if_report_ip6(ap6, ifname, 2967 logintname, &stat, B_TRUE); 2968 ifindex_v6 = new_ifindex; 2969 } else { 2970 if_report_ip6(ap6, ifname, 2971 logintname, &stat, B_FALSE); 2972 } 2973 } /* 'for' loop 2d ends */ 2974 } else if (!alreadydone) { 2975 char ifname[LIFNAMSIZ + 1]; 2976 char buf[IFNAMSIZ + 1]; 2977 mib2_ipv6AddrEntry_t *ap6; 2978 struct ifstat t; 2979 struct iflist *tlp = NULL; 2980 struct iflist **nextnew = &newlist6; 2981 struct iflist *walkold; 2982 struct iflist *cleanlist; 2983 boolean_t found_if = B_FALSE; 2984 2985 alreadydone = B_TRUE; /* ignore other case */ 2986 2987 /* 2988 * Check if there is anything to do. 2989 */ 2990 if (item->length < 2991 sizeof (mib2_ipv6AddrEntry_t)) { 2992 fail(0, "No compatible interfaces"); 2993 } 2994 2995 /* 2996 * 'for' loop 2e: find the "right" entry: 2997 * If an interface name to match has been 2998 * supplied then try and find it, otherwise 2999 * match the first non-loopback interface found. 3000 * Use lo0 if all else fails. 3001 */ 3002 for (ap6 = (mib2_ipv6AddrEntry_t *)item->valp; 3003 (char *)ap6 < (char *)item->valp 3004 + item->length; 3005 ap6++) { 3006 (void) octetstr(&ap6->ipv6AddrIfIndex, 3007 'a', ifname, sizeof (ifname)); 3008 (void) strtok(ifname, ":"); 3009 3010 if (matchname) { 3011 if (strcmp(matchname, 3012 ifname) == 0) { 3013 /* 'for' loop 2e */ 3014 found_if = B_TRUE; 3015 break; 3016 } 3017 } else if (strcmp(ifname, "lo0") != 0) 3018 break; /* 'for' loop 2e */ 3019 } /* 'for' loop 2e ends */ 3020 3021 if (matchname == NULL) { 3022 matchname = ifname; 3023 } else { 3024 if (!found_if) 3025 fail(0, "-I: %s no such " 3026 "interface.", matchname); 3027 } 3028 3029 if (Iflag_only == 0 || !reentry) { 3030 (void) printf( 3031 " input %-6.6s" 3032 " output ", 3033 matchname); 3034 (void) printf(" input (Total)" 3035 " output\n"); 3036 (void) printf("%-7.7s %-5.5s %-7.7s " 3037 "%-5.5s %-6.6s ", 3038 "packets", "errs", "packets", 3039 "errs", "colls"); 3040 (void) printf("%-7.7s %-5.5s %-7.7s " 3041 "%-5.5s %-6.6s\n", 3042 "packets", "errs", "packets", 3043 "errs", "colls"); 3044 } 3045 3046 sum6 = zerostat; 3047 3048 /* 'for' loop 2f: */ 3049 for (ap6 = (mib2_ipv6AddrEntry_t *)item->valp; 3050 (char *)ap6 < (char *)item->valp 3051 + item->length; 3052 ap6++) { 3053 (void) octetstr(&ap6->ipv6AddrIfIndex, 3054 'a', buf, sizeof (buf)); 3055 (void) strtok(buf, ":"); 3056 3057 /* 3058 * We have reduced the IP interface 3059 * name, which could have been a 3060 * logical, down to a name suitable 3061 * for use with kstats. 3062 * We treat this name as unique and 3063 * only collate statistics for it once 3064 * per pass. This is to avoid falsely 3065 * amplifying these statistics by the 3066 * the number of logical instances. 3067 */ 3068 3069 if ((tlp != NULL) && 3070 ((strcmp(buf, tlp->ifname) == 0))) { 3071 continue; 3072 } 3073 3074 ksp = kstat_lookup(kc, NULL, -1, buf); 3075 if (ksp && ksp->ks_type == 3076 KSTAT_TYPE_NAMED) 3077 (void) safe_kstat_read(kc, 3078 ksp, NULL); 3079 3080 t.ipackets = kstat_named_value(ksp, 3081 "ipackets"); 3082 t.ierrors = kstat_named_value(ksp, 3083 "ierrors"); 3084 t.opackets = kstat_named_value(ksp, 3085 "opackets"); 3086 t.oerrors = kstat_named_value(ksp, 3087 "oerrors"); 3088 t.collisions = kstat_named_value(ksp, 3089 "collisions"); 3090 3091 if (strcmp(buf, matchname) == 0) 3092 new6 = t; 3093 3094 /* Build the interface list */ 3095 3096 tlp = malloc(sizeof (struct iflist)); 3097 (void) strlcpy(tlp->ifname, buf, 3098 sizeof (tlp->ifname)); 3099 tlp->tot = t; 3100 *nextnew = tlp; 3101 nextnew = &tlp->next_if; 3102 3103 /* 3104 * First time through. 3105 * Just add up the interface stats. 3106 */ 3107 3108 if (oldlist6 == NULL) { 3109 if_stat_total(&zerostat, 3110 &t, &sum6); 3111 continue; 3112 } 3113 3114 /* 3115 * Walk old list for the interface. 3116 * 3117 * If found, add difference to total. 3118 * 3119 * If not, an interface has been plumbed 3120 * up. In this case, we will simply 3121 * ignore the new interface until the 3122 * next interval; as there's no easy way 3123 * to acquire statistics between time 3124 * of the plumb and the next interval 3125 * boundary. This results in inaccurate 3126 * total values for current interval. 3127 * 3128 * Note the case when an interface is 3129 * unplumbed; as similar problems exist. 3130 * The unplumbed interface is not in the 3131 * current list, and there's no easy way 3132 * to account for the statistics between 3133 * the previous interval and time of the 3134 * unplumb. Therefore, we (in a sense) 3135 * ignore the removed interface by only 3136 * involving "current" interfaces when 3137 * computing the total statistics. 3138 * Unfortunately, this also results in 3139 * inaccurate values for interval total. 3140 */ 3141 3142 for (walkold = oldlist6; 3143 walkold != NULL; 3144 walkold = walkold->next_if) { 3145 if (strcmp(walkold->ifname, 3146 buf) == 0) { 3147 if_stat_total( 3148 &walkold->tot, 3149 &t, &sum6); 3150 break; 3151 } 3152 } 3153 3154 } /* 'for' loop 2f ends */ 3155 3156 *nextnew = NULL; 3157 3158 (void) printf("%-7llu %-5llu %-7llu " 3159 "%-5llu %-6llu ", 3160 new6.ipackets - old6.ipackets, 3161 new6.ierrors - old6.ierrors, 3162 new6.opackets - old6.opackets, 3163 new6.oerrors - old6.oerrors, 3164 new6.collisions - old6.collisions); 3165 3166 (void) printf("%-7llu %-5llu %-7llu " 3167 "%-5llu %-6llu\n", sum6.ipackets, 3168 sum6.ierrors, sum6.opackets, 3169 sum6.oerrors, sum6.collisions); 3170 3171 /* 3172 * Tidy things up once finished. 3173 */ 3174 3175 old6 = new6; 3176 cleanlist = oldlist6; 3177 oldlist6 = newlist6; 3178 while (cleanlist != NULL) { 3179 tlp = cleanlist->next_if; 3180 free(cleanlist); 3181 cleanlist = tlp; 3182 } 3183 } 3184 break; 3185 } 3186 } 3187 (void) fflush(stdout); 3188 } /* 'for' loop 1 ends */ 3189 if ((Iflag_only == 0) && (!once_only)) 3190 (void) putchar('\n'); 3191 reentry = B_TRUE; 3192 } 3193 3194 static void 3195 if_report_ip4(mib2_ipAddrEntry_t *ap, 3196 char ifname[], char logintname[], struct ifstat *statptr, 3197 boolean_t ksp_not_null) { 3198 3199 char abuf[MAXHOSTNAMELEN + 1]; 3200 char dstbuf[MAXHOSTNAMELEN + 1]; 3201 3202 if (ksp_not_null) { 3203 (void) printf("%-5s %-5u", 3204 ifname, ap->ipAdEntInfo.ae_mtu); 3205 if (ap->ipAdEntInfo.ae_flags & IFF_POINTOPOINT) 3206 (void) pr_addr(ap->ipAdEntInfo.ae_pp_dst_addr, 3207 abuf, sizeof (abuf)); 3208 else 3209 (void) pr_netaddr(ap->ipAdEntAddr, 3210 ap->ipAdEntNetMask, abuf, sizeof (abuf)); 3211 (void) printf("%-13s %-14s %-6llu %-5llu %-6llu %-5llu " 3212 "%-6llu %-6llu\n", 3213 abuf, pr_addr(ap->ipAdEntAddr, dstbuf, sizeof (dstbuf)), 3214 statptr->ipackets, statptr->ierrors, 3215 statptr->opackets, statptr->oerrors, 3216 statptr->collisions, 0LL); 3217 } 3218 /* 3219 * Print logical interface info if Aflag set (including logical unit 0) 3220 */ 3221 if (Aflag) { 3222 *statptr = zerostat; 3223 statptr->ipackets = ap->ipAdEntInfo.ae_ibcnt; 3224 statptr->opackets = ap->ipAdEntInfo.ae_obcnt; 3225 3226 (void) printf("%-5s %-5u", logintname, ap->ipAdEntInfo.ae_mtu); 3227 if (ap->ipAdEntInfo.ae_flags & IFF_POINTOPOINT) 3228 (void) pr_addr(ap->ipAdEntInfo.ae_pp_dst_addr, abuf, 3229 sizeof (abuf)); 3230 else 3231 (void) pr_netaddr(ap->ipAdEntAddr, ap->ipAdEntNetMask, 3232 abuf, sizeof (abuf)); 3233 3234 (void) printf("%-13s %-14s %-6llu %-5s %-6llu " 3235 "%-5s %-6s %-6llu\n", abuf, 3236 pr_addr(ap->ipAdEntAddr, dstbuf, sizeof (dstbuf)), 3237 statptr->ipackets, "N/A", statptr->opackets, "N/A", "N/A", 3238 0LL); 3239 } 3240 } 3241 3242 static void 3243 if_report_ip6(mib2_ipv6AddrEntry_t *ap6, 3244 char ifname[], char logintname[], struct ifstat *statptr, 3245 boolean_t ksp_not_null) { 3246 3247 char abuf[MAXHOSTNAMELEN + 1]; 3248 char dstbuf[MAXHOSTNAMELEN + 1]; 3249 3250 if (ksp_not_null) { 3251 (void) printf("%-5s %-5u", ifname, ap6->ipv6AddrInfo.ae_mtu); 3252 if (ap6->ipv6AddrInfo.ae_flags & 3253 IFF_POINTOPOINT) { 3254 (void) pr_addr6(&ap6->ipv6AddrInfo.ae_pp_dst_addr, 3255 abuf, sizeof (abuf)); 3256 } else { 3257 (void) pr_prefix6(&ap6->ipv6AddrAddress, 3258 ap6->ipv6AddrPfxLength, abuf, 3259 sizeof (abuf)); 3260 } 3261 (void) printf("%-27s %-27s %-6llu %-5llu " 3262 "%-6llu %-5llu %-6llu\n", 3263 abuf, pr_addr6(&ap6->ipv6AddrAddress, dstbuf, 3264 sizeof (dstbuf)), 3265 statptr->ipackets, statptr->ierrors, statptr->opackets, 3266 statptr->oerrors, statptr->collisions); 3267 } 3268 /* 3269 * Print logical interface info if Aflag set (including logical unit 0) 3270 */ 3271 if (Aflag) { 3272 *statptr = zerostat; 3273 statptr->ipackets = ap6->ipv6AddrInfo.ae_ibcnt; 3274 statptr->opackets = ap6->ipv6AddrInfo.ae_obcnt; 3275 3276 (void) printf("%-5s %-5u", logintname, 3277 ap6->ipv6AddrInfo.ae_mtu); 3278 if (ap6->ipv6AddrInfo.ae_flags & IFF_POINTOPOINT) 3279 (void) pr_addr6(&ap6->ipv6AddrInfo.ae_pp_dst_addr, 3280 abuf, sizeof (abuf)); 3281 else 3282 (void) pr_prefix6(&ap6->ipv6AddrAddress, 3283 ap6->ipv6AddrPfxLength, abuf, sizeof (abuf)); 3284 (void) printf("%-27s %-27s %-6llu %-5s %-6llu %-5s %-6s\n", 3285 abuf, pr_addr6(&ap6->ipv6AddrAddress, dstbuf, 3286 sizeof (dstbuf)), 3287 statptr->ipackets, "N/A", 3288 statptr->opackets, "N/A", "N/A"); 3289 } 3290 } 3291 3292 /* --------------------- DHCP_REPORT (netstat -D) ------------------------- */ 3293 3294 static boolean_t 3295 dhcp_do_ipc(dhcp_ipc_type_t type, const char *ifname, boolean_t printed_one) 3296 { 3297 dhcp_ipc_request_t *request; 3298 dhcp_ipc_reply_t *reply; 3299 int error; 3300 3301 request = dhcp_ipc_alloc_request(type, ifname, NULL, 0, DHCP_TYPE_NONE); 3302 if (request == NULL) 3303 fail(0, "dhcp_do_ipc: out of memory"); 3304 3305 error = dhcp_ipc_make_request(request, &reply, DHCP_IPC_WAIT_DEFAULT); 3306 if (error != 0) { 3307 free(request); 3308 fail(0, "dhcp_do_ipc: %s", dhcp_ipc_strerror(error)); 3309 } 3310 3311 free(request); 3312 error = reply->return_code; 3313 if (error == DHCP_IPC_E_UNKIF) { 3314 free(reply); 3315 return (printed_one); 3316 } 3317 if (error != 0) { 3318 free(reply); 3319 fail(0, "dhcp_do_ipc: %s", dhcp_ipc_strerror(error)); 3320 } 3321 3322 if (!printed_one) 3323 (void) printf("%s", dhcp_status_hdr_string()); 3324 3325 (void) printf("%s", dhcp_status_reply_to_string(reply)); 3326 free(reply); 3327 return (B_TRUE); 3328 } 3329 3330 /* 3331 * dhcp_walk_interfaces: walk the list of interfaces that have a given set of 3332 * flags turned on (flags_on) and a given set turned off (flags_off) for a 3333 * given address family (af). For each, print out the DHCP status using 3334 * dhcp_do_ipc. 3335 */ 3336 static boolean_t 3337 dhcp_walk_interfaces(uint_t flags_on, uint_t flags_off, int af, 3338 boolean_t printed_one) 3339 { 3340 struct lifnum lifn; 3341 struct lifconf lifc; 3342 int n_ifs, i, sock_fd; 3343 3344 sock_fd = socket(af, SOCK_DGRAM, 0); 3345 if (sock_fd == -1) 3346 return (printed_one); 3347 3348 /* 3349 * SIOCGLIFNUM is just an estimate. If the ioctl fails, we don't care; 3350 * just drive on and use SIOCGLIFCONF with increasing buffer sizes, as 3351 * is traditional. 3352 */ 3353 (void) memset(&lifn, 0, sizeof (lifn)); 3354 lifn.lifn_family = af; 3355 lifn.lifn_flags = LIFC_ALLZONES | LIFC_NOXMIT; 3356 if (ioctl(sock_fd, SIOCGLIFNUM, &lifn) == -1) 3357 n_ifs = LIFN_GUARD_VALUE; 3358 else 3359 n_ifs = lifn.lifn_count + LIFN_GUARD_VALUE; 3360 3361 (void) memset(&lifc, 0, sizeof (lifc)); 3362 lifc.lifc_family = af; 3363 lifc.lifc_flags = lifn.lifn_flags; 3364 lifc.lifc_len = n_ifs * sizeof (struct lifreq); 3365 lifc.lifc_buf = malloc(lifc.lifc_len); 3366 if (lifc.lifc_buf != NULL) { 3367 3368 if (ioctl(sock_fd, SIOCGLIFCONF, &lifc) == -1) { 3369 (void) close(sock_fd); 3370 free(lifc.lifc_buf); 3371 return (NULL); 3372 } 3373 3374 n_ifs = lifc.lifc_len / sizeof (struct lifreq); 3375 3376 for (i = 0; i < n_ifs; i++) { 3377 if (ioctl(sock_fd, SIOCGLIFFLAGS, &lifc.lifc_req[i]) == 3378 0 && (lifc.lifc_req[i].lifr_flags & (flags_on | 3379 flags_off)) != flags_on) 3380 continue; 3381 printed_one = dhcp_do_ipc(DHCP_STATUS | 3382 (af == AF_INET6 ? DHCP_V6 : 0), 3383 lifc.lifc_req[i].lifr_name, printed_one); 3384 } 3385 } 3386 (void) close(sock_fd); 3387 free(lifc.lifc_buf); 3388 return (printed_one); 3389 } 3390 3391 static void 3392 dhcp_report(char *ifname) 3393 { 3394 boolean_t printed_one; 3395 3396 if (!family_selected(AF_INET) && !family_selected(AF_INET6)) 3397 return; 3398 3399 printed_one = B_FALSE; 3400 if (ifname != NULL) { 3401 if (family_selected(AF_INET)) { 3402 printed_one = dhcp_do_ipc(DHCP_STATUS, ifname, 3403 printed_one); 3404 } 3405 if (family_selected(AF_INET6)) { 3406 printed_one = dhcp_do_ipc(DHCP_STATUS | DHCP_V6, 3407 ifname, printed_one); 3408 } 3409 if (!printed_one) { 3410 fail(0, "%s: %s", ifname, 3411 dhcp_ipc_strerror(DHCP_IPC_E_UNKIF)); 3412 } 3413 } else { 3414 if (family_selected(AF_INET)) { 3415 printed_one = dhcp_walk_interfaces(IFF_DHCPRUNNING, 3416 0, AF_INET, printed_one); 3417 } 3418 if (family_selected(AF_INET6)) { 3419 (void) dhcp_walk_interfaces(IFF_DHCPRUNNING, 3420 IFF_ADDRCONF, AF_INET6, printed_one); 3421 } 3422 } 3423 } 3424 3425 /* --------------------- GROUP_REPORT (netstat -g) ------------------------- */ 3426 3427 static void 3428 group_report(mib_item_t *item) 3429 { 3430 mib_item_t *v4grp = NULL, *v4src = NULL; 3431 mib_item_t *v6grp = NULL, *v6src = NULL; 3432 int jtemp = 0; 3433 char ifname[LIFNAMSIZ + 1]; 3434 char abuf[MAXHOSTNAMELEN + 1]; 3435 ip_member_t *ipmp; 3436 ip_grpsrc_t *ips; 3437 ipv6_member_t *ipmp6; 3438 ipv6_grpsrc_t *ips6; 3439 char *ifnamep; 3440 boolean_t first, first_src; 3441 3442 /* 'for' loop 1: */ 3443 for (; item; item = item->next_item) { 3444 if (Dflag) { 3445 (void) printf("\n--- Entry %d ---\n", ++jtemp); 3446 (void) printf("Group = %d, mib_id = %d, " 3447 "length = %d, valp = 0x%p\n", 3448 item->group, item->mib_id, item->length, 3449 item->valp); 3450 } 3451 if (item->group == MIB2_IP && family_selected(AF_INET)) { 3452 switch (item->mib_id) { 3453 case EXPER_IP_GROUP_MEMBERSHIP: 3454 v4grp = item; 3455 if (Dflag) 3456 (void) printf("item is v4grp info\n"); 3457 break; 3458 case EXPER_IP_GROUP_SOURCES: 3459 v4src = item; 3460 if (Dflag) 3461 (void) printf("item is v4src info\n"); 3462 break; 3463 default: 3464 continue; 3465 } 3466 continue; 3467 } 3468 if (item->group == MIB2_IP6 && family_selected(AF_INET6)) { 3469 switch (item->mib_id) { 3470 case EXPER_IP6_GROUP_MEMBERSHIP: 3471 v6grp = item; 3472 if (Dflag) 3473 (void) printf("item is v6grp info\n"); 3474 break; 3475 case EXPER_IP6_GROUP_SOURCES: 3476 v6src = item; 3477 if (Dflag) 3478 (void) printf("item is v6src info\n"); 3479 break; 3480 default: 3481 continue; 3482 } 3483 } 3484 } 3485 3486 if (family_selected(AF_INET) && v4grp != NULL) { 3487 if (Dflag) 3488 (void) printf("%u records for ipGroupMember:\n", 3489 v4grp->length / sizeof (ip_member_t)); 3490 3491 first = B_TRUE; 3492 for (ipmp = (ip_member_t *)v4grp->valp; 3493 (char *)ipmp < (char *)v4grp->valp + v4grp->length; 3494 /* LINTED: (note 1) */ 3495 ipmp = (ip_member_t *)((char *)ipmp + ipMemberEntrySize)) { 3496 if (first) { 3497 (void) puts(v4compat ? 3498 "Group Memberships" : 3499 "Group Memberships: IPv4"); 3500 (void) puts("Interface " 3501 "Group RefCnt"); 3502 (void) puts("--------- " 3503 "-------------------- ------"); 3504 first = B_FALSE; 3505 } 3506 3507 (void) printf("%-9s %-20s %6u\n", 3508 octetstr(&ipmp->ipGroupMemberIfIndex, 'a', 3509 ifname, sizeof (ifname)), 3510 pr_addr(ipmp->ipGroupMemberAddress, 3511 abuf, sizeof (abuf)), 3512 ipmp->ipGroupMemberRefCnt); 3513 3514 3515 if (!Vflag || v4src == NULL) 3516 continue; 3517 3518 if (Dflag) 3519 (void) printf("scanning %u ipGroupSource " 3520 "records...\n", 3521 v4src->length/sizeof (ip_grpsrc_t)); 3522 3523 first_src = B_TRUE; 3524 for (ips = (ip_grpsrc_t *)v4src->valp; 3525 (char *)ips < (char *)v4src->valp + v4src->length; 3526 /* LINTED: (note 1) */ 3527 ips = (ip_grpsrc_t *)((char *)ips + 3528 ipGroupSourceEntrySize)) { 3529 /* 3530 * We assume that all source addrs for a given 3531 * interface/group pair are contiguous, so on 3532 * the first non-match after we've found at 3533 * least one, we bail. 3534 */ 3535 if ((ipmp->ipGroupMemberAddress != 3536 ips->ipGroupSourceGroup) || 3537 (!octetstrmatch(&ipmp->ipGroupMemberIfIndex, 3538 &ips->ipGroupSourceIfIndex))) { 3539 if (first_src) 3540 continue; 3541 else 3542 break; 3543 } 3544 if (first_src) { 3545 (void) printf("\t%s: %s\n", 3546 fmodestr( 3547 ipmp->ipGroupMemberFilterMode), 3548 pr_addr(ips->ipGroupSourceAddress, 3549 abuf, sizeof (abuf))); 3550 first_src = B_FALSE; 3551 continue; 3552 } 3553 3554 (void) printf("\t %s\n", 3555 pr_addr(ips->ipGroupSourceAddress, abuf, 3556 sizeof (abuf))); 3557 } 3558 } 3559 (void) putchar('\n'); 3560 } 3561 3562 if (family_selected(AF_INET6) && v6grp != NULL) { 3563 if (Dflag) 3564 (void) printf("%u records for ipv6GroupMember:\n", 3565 v6grp->length / sizeof (ipv6_member_t)); 3566 3567 first = B_TRUE; 3568 for (ipmp6 = (ipv6_member_t *)v6grp->valp; 3569 (char *)ipmp6 < (char *)v6grp->valp + v6grp->length; 3570 /* LINTED: (note 1) */ 3571 ipmp6 = (ipv6_member_t *)((char *)ipmp6 + 3572 ipv6MemberEntrySize)) { 3573 if (first) { 3574 (void) puts("Group Memberships: " 3575 "IPv6"); 3576 (void) puts(" If " 3577 "Group RefCnt"); 3578 (void) puts("----- " 3579 "--------------------------- ------"); 3580 first = B_FALSE; 3581 } 3582 3583 ifnamep = if_indextoname( 3584 ipmp6->ipv6GroupMemberIfIndex, ifname); 3585 if (ifnamep == NULL) { 3586 (void) printf("Invalid ifindex %d\n", 3587 ipmp6->ipv6GroupMemberIfIndex); 3588 continue; 3589 } 3590 (void) printf("%-5s %-27s %5u\n", 3591 ifnamep, 3592 pr_addr6(&ipmp6->ipv6GroupMemberAddress, 3593 abuf, sizeof (abuf)), 3594 ipmp6->ipv6GroupMemberRefCnt); 3595 3596 if (!Vflag || v6src == NULL) 3597 continue; 3598 3599 if (Dflag) 3600 (void) printf("scanning %u ipv6GroupSource " 3601 "records...\n", 3602 v6src->length/sizeof (ipv6_grpsrc_t)); 3603 3604 first_src = B_TRUE; 3605 for (ips6 = (ipv6_grpsrc_t *)v6src->valp; 3606 (char *)ips6 < (char *)v6src->valp + v6src->length; 3607 /* LINTED: (note 1) */ 3608 ips6 = (ipv6_grpsrc_t *)((char *)ips6 + 3609 ipv6GroupSourceEntrySize)) { 3610 /* same assumption as in the v4 case above */ 3611 if ((ipmp6->ipv6GroupMemberIfIndex != 3612 ips6->ipv6GroupSourceIfIndex) || 3613 (!IN6_ARE_ADDR_EQUAL( 3614 &ipmp6->ipv6GroupMemberAddress, 3615 &ips6->ipv6GroupSourceGroup))) { 3616 if (first_src) 3617 continue; 3618 else 3619 break; 3620 } 3621 if (first_src) { 3622 (void) printf("\t%s: %s\n", 3623 fmodestr( 3624 ipmp6->ipv6GroupMemberFilterMode), 3625 pr_addr6( 3626 &ips6->ipv6GroupSourceAddress, 3627 abuf, sizeof (abuf))); 3628 first_src = B_FALSE; 3629 continue; 3630 } 3631 3632 (void) printf("\t %s\n", 3633 pr_addr6(&ips6->ipv6GroupSourceAddress, 3634 abuf, sizeof (abuf))); 3635 } 3636 } 3637 (void) putchar('\n'); 3638 } 3639 3640 (void) putchar('\n'); 3641 (void) fflush(stdout); 3642 } 3643 3644 /* --------------------- ARP_REPORT (netstat -p) -------------------------- */ 3645 3646 static void 3647 arp_report(mib_item_t *item) 3648 { 3649 int jtemp = 0; 3650 char ifname[LIFNAMSIZ + 1]; 3651 char abuf[MAXHOSTNAMELEN + 1]; 3652 char maskbuf[STR_EXPAND * OCTET_LENGTH + 1]; 3653 char flbuf[32]; /* ACE_F_ flags */ 3654 char xbuf[STR_EXPAND * OCTET_LENGTH + 1]; 3655 mib2_ipNetToMediaEntry_t *np; 3656 int flags; 3657 boolean_t first; 3658 3659 if (!(family_selected(AF_INET))) 3660 return; 3661 3662 /* 'for' loop 1: */ 3663 for (; item; item = item->next_item) { 3664 if (Dflag) { 3665 (void) printf("\n--- Entry %d ---\n", ++jtemp); 3666 (void) printf("Group = %d, mib_id = %d, " 3667 "length = %d, valp = 0x%p\n", 3668 item->group, item->mib_id, item->length, 3669 item->valp); 3670 } 3671 if (!(item->group == MIB2_IP && item->mib_id == MIB2_IP_MEDIA)) 3672 continue; /* 'for' loop 1 */ 3673 3674 if (Dflag) 3675 (void) printf("%u records for " 3676 "ipNetToMediaEntryTable:\n", 3677 item->length/sizeof (mib2_ipNetToMediaEntry_t)); 3678 3679 first = B_TRUE; 3680 /* 'for' loop 2: */ 3681 for (np = (mib2_ipNetToMediaEntry_t *)item->valp; 3682 (char *)np < (char *)item->valp + item->length; 3683 /* LINTED: (note 1) */ 3684 np = (mib2_ipNetToMediaEntry_t *)((char *)np + 3685 ipNetToMediaEntrySize)) { 3686 if (first) { 3687 (void) puts(v4compat ? 3688 "Net to Media Table" : 3689 "Net to Media Table: IPv4"); 3690 (void) puts("Device " 3691 " IP Address Mask " 3692 "Flags Phys Addr"); 3693 (void) puts("------ " 3694 "-------------------- --------------- " 3695 "-------- ---------------"); 3696 first = B_FALSE; 3697 } 3698 3699 flbuf[0] = '\0'; 3700 flags = np->ipNetToMediaInfo.ntm_flags; 3701 /* 3702 * Note that not all flags are possible at the same 3703 * time. Patterns: SPLAy DUo 3704 */ 3705 if (flags & ACE_F_PERMANENT) 3706 (void) strcat(flbuf, "S"); 3707 if (flags & ACE_F_PUBLISH) 3708 (void) strcat(flbuf, "P"); 3709 if (flags & ACE_F_DYING) 3710 (void) strcat(flbuf, "D"); 3711 if (!(flags & ACE_F_RESOLVED)) 3712 (void) strcat(flbuf, "U"); 3713 if (flags & ACE_F_MAPPING) 3714 (void) strcat(flbuf, "M"); 3715 if (flags & ACE_F_MYADDR) 3716 (void) strcat(flbuf, "L"); 3717 if (flags & ACE_F_UNVERIFIED) 3718 (void) strcat(flbuf, "d"); 3719 if (flags & ACE_F_AUTHORITY) 3720 (void) strcat(flbuf, "A"); 3721 if (flags & ACE_F_OLD) 3722 (void) strcat(flbuf, "o"); 3723 if (flags & ACE_F_DELAYED) 3724 (void) strcat(flbuf, "y"); 3725 (void) printf("%-6s %-20s %-15s %-8s %s\n", 3726 octetstr(&np->ipNetToMediaIfIndex, 'a', 3727 ifname, sizeof (ifname)), 3728 pr_addr(np->ipNetToMediaNetAddress, 3729 abuf, sizeof (abuf)), 3730 octetstr(&np->ipNetToMediaInfo.ntm_mask, 'd', 3731 maskbuf, sizeof (maskbuf)), 3732 flbuf, 3733 octetstr(&np->ipNetToMediaPhysAddress, 'h', 3734 xbuf, sizeof (xbuf))); 3735 } /* 'for' loop 2 ends */ 3736 } /* 'for' loop 1 ends */ 3737 (void) fflush(stdout); 3738 } 3739 3740 /* --------------------- NDP_REPORT (netstat -p) -------------------------- */ 3741 3742 static void 3743 ndp_report(mib_item_t *item) 3744 { 3745 int jtemp = 0; 3746 char abuf[MAXHOSTNAMELEN + 1]; 3747 char *state; 3748 char *type; 3749 char xbuf[STR_EXPAND * OCTET_LENGTH + 1]; 3750 mib2_ipv6NetToMediaEntry_t *np6; 3751 char ifname[LIFNAMSIZ + 1]; 3752 char *ifnamep; 3753 boolean_t first; 3754 3755 if (!(family_selected(AF_INET6))) 3756 return; 3757 3758 /* 'for' loop 1: */ 3759 for (; item; item = item->next_item) { 3760 if (Dflag) { 3761 (void) printf("\n--- Entry %d ---\n", ++jtemp); 3762 (void) printf("Group = %d, mib_id = %d, " 3763 "length = %d, valp = 0x%p\n", 3764 item->group, item->mib_id, item->length, 3765 item->valp); 3766 } 3767 if (!(item->group == MIB2_IP6 && 3768 item->mib_id == MIB2_IP6_MEDIA)) 3769 continue; /* 'for' loop 1 */ 3770 3771 first = B_TRUE; 3772 /* 'for' loop 2: */ 3773 for (np6 = (mib2_ipv6NetToMediaEntry_t *)item->valp; 3774 (char *)np6 < (char *)item->valp + item->length; 3775 /* LINTED: (note 1) */ 3776 np6 = (mib2_ipv6NetToMediaEntry_t *)((char *)np6 + 3777 ipv6NetToMediaEntrySize)) { 3778 if (first) { 3779 (void) puts("\nNet to Media Table: IPv6"); 3780 (void) puts(" If Physical Address " 3781 " Type State Destination/Mask"); 3782 (void) puts("----- ----------------- " 3783 "------- ------------ " 3784 "---------------------------"); 3785 first = B_FALSE; 3786 } 3787 3788 ifnamep = if_indextoname(np6->ipv6NetToMediaIfIndex, 3789 ifname); 3790 if (ifnamep == NULL) { 3791 (void) printf("Invalid ifindex %d\n", 3792 np6->ipv6NetToMediaIfIndex); 3793 continue; /* 'for' loop 2 */ 3794 } 3795 switch (np6->ipv6NetToMediaState) { 3796 case ND_INCOMPLETE: 3797 state = "INCOMPLETE"; 3798 break; 3799 case ND_REACHABLE: 3800 state = "REACHABLE"; 3801 break; 3802 case ND_STALE: 3803 state = "STALE"; 3804 break; 3805 case ND_DELAY: 3806 state = "DELAY"; 3807 break; 3808 case ND_PROBE: 3809 state = "PROBE"; 3810 break; 3811 case ND_UNREACHABLE: 3812 state = "UNREACHABLE"; 3813 break; 3814 default: 3815 state = "UNKNOWN"; 3816 } 3817 3818 switch (np6->ipv6NetToMediaType) { 3819 case 1: 3820 type = "other"; 3821 break; 3822 case 2: 3823 type = "dynamic"; 3824 break; 3825 case 3: 3826 type = "static"; 3827 break; 3828 case 4: 3829 type = "local"; 3830 break; 3831 } 3832 (void) printf("%-5s %-17s %-7s %-12s %-27s\n", 3833 ifnamep, 3834 octetstr(&np6->ipv6NetToMediaPhysAddress, 'h', 3835 xbuf, sizeof (xbuf)), 3836 type, 3837 state, 3838 pr_addr6(&np6->ipv6NetToMediaNetAddress, 3839 abuf, sizeof (abuf))); 3840 } /* 'for' loop 2 ends */ 3841 } /* 'for' loop 1 ends */ 3842 (void) putchar('\n'); 3843 (void) fflush(stdout); 3844 } 3845 3846 /* ------------------------- ire_report (netstat -r) ------------------------ */ 3847 3848 typedef struct sec_attr_list_s { 3849 struct sec_attr_list_s *sal_next; 3850 const mib2_ipAttributeEntry_t *sal_attr; 3851 } sec_attr_list_t; 3852 3853 static boolean_t ire_report_item_v4(const mib2_ipRouteEntry_t *, boolean_t, 3854 const sec_attr_list_t *); 3855 static boolean_t ire_report_item_v6(const mib2_ipv6RouteEntry_t *, boolean_t, 3856 const sec_attr_list_t *); 3857 static const char *pr_secattr(const sec_attr_list_t *); 3858 3859 static void 3860 ire_report(const mib_item_t *item) 3861 { 3862 int jtemp = 0; 3863 boolean_t print_hdr_once_v4 = B_TRUE; 3864 boolean_t print_hdr_once_v6 = B_TRUE; 3865 mib2_ipRouteEntry_t *rp; 3866 mib2_ipv6RouteEntry_t *rp6; 3867 sec_attr_list_t **v4_attrs, **v4a; 3868 sec_attr_list_t **v6_attrs, **v6a; 3869 sec_attr_list_t *all_attrs, *aptr; 3870 const mib_item_t *iptr; 3871 int ipv4_route_count, ipv6_route_count; 3872 int route_attrs_count; 3873 3874 /* 3875 * Preparation pass: the kernel returns separate entries for IP routing 3876 * table entries and security attributes. We loop through the 3877 * attributes first and link them into lists. 3878 */ 3879 ipv4_route_count = ipv6_route_count = route_attrs_count = 0; 3880 for (iptr = item; iptr != NULL; iptr = iptr->next_item) { 3881 if (iptr->group == MIB2_IP6 && iptr->mib_id == MIB2_IP6_ROUTE) 3882 ipv6_route_count += iptr->length / ipv6RouteEntrySize; 3883 if (iptr->group == MIB2_IP && iptr->mib_id == MIB2_IP_ROUTE) 3884 ipv4_route_count += iptr->length / ipRouteEntrySize; 3885 if ((iptr->group == MIB2_IP || iptr->group == MIB2_IP6) && 3886 iptr->mib_id == EXPER_IP_RTATTR) 3887 route_attrs_count += iptr->length / 3888 ipRouteAttributeSize; 3889 } 3890 v4_attrs = v6_attrs = NULL; 3891 all_attrs = NULL; 3892 if (family_selected(AF_INET) && ipv4_route_count > 0) { 3893 v4_attrs = calloc(ipv4_route_count, sizeof (*v4_attrs)); 3894 if (v4_attrs == NULL) { 3895 perror("ire_report calloc v4_attrs failed"); 3896 return; 3897 } 3898 } 3899 if (family_selected(AF_INET6) && ipv6_route_count > 0) { 3900 v6_attrs = calloc(ipv6_route_count, sizeof (*v6_attrs)); 3901 if (v6_attrs == NULL) { 3902 perror("ire_report calloc v6_attrs failed"); 3903 goto ire_report_done; 3904 } 3905 } 3906 if (route_attrs_count > 0) { 3907 all_attrs = malloc(route_attrs_count * sizeof (*all_attrs)); 3908 if (all_attrs == NULL) { 3909 perror("ire_report malloc all_attrs failed"); 3910 goto ire_report_done; 3911 } 3912 } 3913 aptr = all_attrs; 3914 for (iptr = item; iptr != NULL; iptr = iptr->next_item) { 3915 mib2_ipAttributeEntry_t *iae; 3916 sec_attr_list_t **alp; 3917 3918 if (v4_attrs != NULL && iptr->group == MIB2_IP && 3919 iptr->mib_id == EXPER_IP_RTATTR) { 3920 alp = v4_attrs; 3921 } else if (v6_attrs != NULL && iptr->group == MIB2_IP6 && 3922 iptr->mib_id == EXPER_IP_RTATTR) { 3923 alp = v6_attrs; 3924 } else { 3925 continue; 3926 } 3927 for (iae = iptr->valp; 3928 (char *)iae < (char *)iptr->valp + iptr->length; 3929 /* LINTED: (note 1) */ 3930 iae = (mib2_ipAttributeEntry_t *)((char *)iae + 3931 ipRouteAttributeSize)) { 3932 aptr->sal_next = alp[iae->iae_routeidx]; 3933 aptr->sal_attr = iae; 3934 alp[iae->iae_routeidx] = aptr++; 3935 } 3936 } 3937 3938 /* 'for' loop 1: */ 3939 v4a = v4_attrs; 3940 v6a = v6_attrs; 3941 for (; item != NULL; item = item->next_item) { 3942 if (Dflag) { 3943 (void) printf("\n--- Entry %d ---\n", ++jtemp); 3944 (void) printf("Group = %d, mib_id = %d, " 3945 "length = %d, valp = 0x%p\n", 3946 item->group, item->mib_id, 3947 item->length, item->valp); 3948 } 3949 if (!((item->group == MIB2_IP && 3950 item->mib_id == MIB2_IP_ROUTE) || 3951 (item->group == MIB2_IP6 && 3952 item->mib_id == MIB2_IP6_ROUTE))) 3953 continue; /* 'for' loop 1 */ 3954 3955 if (item->group == MIB2_IP && !family_selected(AF_INET)) 3956 continue; /* 'for' loop 1 */ 3957 else if (item->group == MIB2_IP6 && !family_selected(AF_INET6)) 3958 continue; /* 'for' loop 1 */ 3959 3960 if (Dflag) { 3961 if (item->group == MIB2_IP) { 3962 (void) printf("%u records for " 3963 "ipRouteEntryTable:\n", 3964 item->length/sizeof (mib2_ipRouteEntry_t)); 3965 } else { 3966 (void) printf("%u records for " 3967 "ipv6RouteEntryTable:\n", 3968 item->length/ 3969 sizeof (mib2_ipv6RouteEntry_t)); 3970 } 3971 } 3972 3973 if (item->group == MIB2_IP) { 3974 for (rp = (mib2_ipRouteEntry_t *)item->valp; 3975 (char *)rp < (char *)item->valp + item->length; 3976 /* LINTED: (note 1) */ 3977 rp = (mib2_ipRouteEntry_t *)((char *)rp + 3978 ipRouteEntrySize)) { 3979 aptr = v4a == NULL ? NULL : *v4a++; 3980 print_hdr_once_v4 = ire_report_item_v4(rp, 3981 print_hdr_once_v4, aptr); 3982 } 3983 } else { 3984 for (rp6 = (mib2_ipv6RouteEntry_t *)item->valp; 3985 (char *)rp6 < (char *)item->valp + item->length; 3986 /* LINTED: (note 1) */ 3987 rp6 = (mib2_ipv6RouteEntry_t *)((char *)rp6 + 3988 ipv6RouteEntrySize)) { 3989 aptr = v6a == NULL ? NULL : *v6a++; 3990 print_hdr_once_v6 = ire_report_item_v6(rp6, 3991 print_hdr_once_v6, aptr); 3992 } 3993 } 3994 } /* 'for' loop 1 ends */ 3995 (void) fflush(stdout); 3996 ire_report_done: 3997 if (v4_attrs != NULL) 3998 free(v4_attrs); 3999 if (v6_attrs != NULL) 4000 free(v6_attrs); 4001 if (all_attrs != NULL) 4002 free(all_attrs); 4003 } 4004 4005 /* 4006 * Match a user-supplied device name. We do this by string because 4007 * the MIB2 interface gives us interface name strings rather than 4008 * ifIndex numbers. The "none" rule matches only routes with no 4009 * interface. The "any" rule matches routes with any non-blank 4010 * interface. A base name ("hme0") matches all aliases as well 4011 * ("hme0:1"). 4012 */ 4013 static boolean_t 4014 dev_name_match(const DeviceName *devnam, const char *ifname) 4015 { 4016 int iflen; 4017 4018 if (ifname == NULL) 4019 return (devnam->o_length == 0); /* "none" */ 4020 if (*ifname == '\0') 4021 return (devnam->o_length != 0); /* "any" */ 4022 iflen = strlen(ifname); 4023 /* The check for ':' here supports interface aliases. */ 4024 if (iflen > devnam->o_length || 4025 (iflen < devnam->o_length && devnam->o_bytes[iflen] != ':')) 4026 return (B_FALSE); 4027 return (strncmp(ifname, devnam->o_bytes, iflen) == 0); 4028 } 4029 4030 /* 4031 * Match a user-supplied IP address list. The "any" rule matches any 4032 * non-zero address. The "none" rule matches only the zero address. 4033 * IPv6 addresses supplied by the user are ignored. If the user 4034 * supplies a subnet mask, then match routes that are at least that 4035 * specific (use the user's mask). If the user supplies only an 4036 * address, then select any routes that would match (use the route's 4037 * mask). 4038 */ 4039 static boolean_t 4040 v4_addr_match(IpAddress addr, IpAddress mask, const filter_t *fp) 4041 { 4042 char **app; 4043 char *aptr; 4044 in_addr_t faddr, fmask; 4045 4046 if (fp->u.a.f_address == NULL) { 4047 if (IN6_IS_ADDR_UNSPECIFIED(&fp->u.a.f_mask)) 4048 return (addr != INADDR_ANY); /* "any" */ 4049 else 4050 return (addr == INADDR_ANY); /* "none" */ 4051 } 4052 if (!IN6_IS_V4MASK(fp->u.a.f_mask)) 4053 return (B_FALSE); 4054 IN6_V4MAPPED_TO_IPADDR(&fp->u.a.f_mask, fmask); 4055 if (fmask != IP_HOST_MASK) { 4056 if (fmask > mask) 4057 return (B_FALSE); 4058 mask = fmask; 4059 } 4060 for (app = fp->u.a.f_address->h_addr_list; (aptr = *app) != NULL; app++) 4061 /* LINTED: (note 1) */ 4062 if (IN6_IS_ADDR_V4MAPPED((in6_addr_t *)aptr)) { 4063 /* LINTED: (note 1) */ 4064 IN6_V4MAPPED_TO_IPADDR((in6_addr_t *)aptr, faddr); 4065 if (((faddr ^ addr) & mask) == 0) 4066 return (B_TRUE); 4067 } 4068 return (B_FALSE); 4069 } 4070 4071 /* 4072 * Run through the filter list for an IPv4 MIB2 route entry. If all 4073 * filters of a given type fail to match, then the route is filtered 4074 * out (not displayed). If no filter is given or at least one filter 4075 * of each type matches, then display the route. 4076 */ 4077 static boolean_t 4078 ire_filter_match_v4(const mib2_ipRouteEntry_t *rp, uint_t flag_b) 4079 { 4080 filter_t *fp; 4081 int idx; 4082 4083 /* 'for' loop 1: */ 4084 for (idx = 0; idx < NFILTERKEYS; idx++) 4085 if ((fp = filters[idx]) != NULL) { 4086 /* 'for' loop 2: */ 4087 for (; fp != NULL; fp = fp->f_next) { 4088 switch (idx) { 4089 case FK_AF: 4090 if (fp->u.f_family != AF_INET) 4091 continue; /* 'for' loop 2 */ 4092 break; 4093 case FK_OUTIF: 4094 if (!dev_name_match(&rp->ipRouteIfIndex, 4095 fp->u.f_ifname)) 4096 continue; /* 'for' loop 2 */ 4097 break; 4098 case FK_DST: 4099 if (!v4_addr_match(rp->ipRouteDest, 4100 rp->ipRouteMask, fp)) 4101 continue; /* 'for' loop 2 */ 4102 break; 4103 case FK_FLAGS: 4104 if ((flag_b & fp->u.f.f_flagset) != 4105 fp->u.f.f_flagset || 4106 (flag_b & fp->u.f.f_flagclear)) 4107 continue; /* 'for' loop 2 */ 4108 break; 4109 } 4110 break; 4111 } /* 'for' loop 2 ends */ 4112 if (fp == NULL) 4113 return (B_FALSE); 4114 } 4115 /* 'for' loop 1 ends */ 4116 return (B_TRUE); 4117 } 4118 4119 /* 4120 * Given an IPv4 MIB2 route entry, form the list of flags for the 4121 * route. 4122 */ 4123 static uint_t 4124 form_v4_route_flags(const mib2_ipRouteEntry_t *rp, char *flags) 4125 { 4126 uint_t flag_b; 4127 4128 flag_b = FLF_U; 4129 (void) strcpy(flags, "U"); 4130 if (rp->ipRouteInfo.re_ire_type == IRE_DEFAULT || 4131 rp->ipRouteInfo.re_ire_type == IRE_PREFIX || 4132 rp->ipRouteInfo.re_ire_type == IRE_HOST || 4133 rp->ipRouteInfo.re_ire_type == IRE_HOST_REDIRECT) { 4134 (void) strcat(flags, "G"); 4135 flag_b |= FLF_G; 4136 } 4137 if (rp->ipRouteMask == IP_HOST_MASK) { 4138 (void) strcat(flags, "H"); 4139 flag_b |= FLF_H; 4140 } 4141 if (rp->ipRouteInfo.re_ire_type == IRE_HOST_REDIRECT) { 4142 (void) strcat(flags, "D"); 4143 flag_b |= FLF_D; 4144 } 4145 if (rp->ipRouteInfo.re_ire_type == IRE_CACHE) { 4146 /* Address resolution */ 4147 (void) strcat(flags, "A"); 4148 flag_b |= FLF_A; 4149 } 4150 if (rp->ipRouteInfo.re_ire_type == IRE_BROADCAST) { /* Broadcast */ 4151 (void) strcat(flags, "B"); 4152 flag_b |= FLF_B; 4153 } 4154 if (rp->ipRouteInfo.re_ire_type == IRE_LOCAL) { /* Local */ 4155 (void) strcat(flags, "L"); 4156 flag_b |= FLF_L; 4157 } 4158 if (rp->ipRouteInfo.re_flags & RTF_MULTIRT) { 4159 (void) strcat(flags, "M"); /* Multiroute */ 4160 flag_b |= FLF_M; 4161 } 4162 if (rp->ipRouteInfo.re_flags & RTF_SETSRC) { 4163 (void) strcat(flags, "S"); /* Setsrc */ 4164 flag_b |= FLF_S; 4165 } 4166 return (flag_b); 4167 } 4168 4169 static const char ire_hdr_v4[] = 4170 "\n%s Table: IPv4\n"; 4171 static const char ire_hdr_v4_compat[] = 4172 "\n%s Table:\n"; 4173 static const char ire_hdr_v4_verbose[] = 4174 " Destination Mask Gateway Device Mxfrg " 4175 "Rtt Ref Flg Out In/Fwd %s\n" 4176 "-------------------- --------------- -------------------- ------ ----- " 4177 "----- --- --- ----- ------ %s\n"; 4178 4179 static const char ire_hdr_v4_normal[] = 4180 " Destination Gateway Flags Ref Use Interface" 4181 " %s\n-------------------- -------------------- ----- ----- ---------- " 4182 "--------- %s\n"; 4183 4184 static boolean_t 4185 ire_report_item_v4(const mib2_ipRouteEntry_t *rp, boolean_t first, 4186 const sec_attr_list_t *attrs) 4187 { 4188 char dstbuf[MAXHOSTNAMELEN + 1]; 4189 char maskbuf[MAXHOSTNAMELEN + 1]; 4190 char gwbuf[MAXHOSTNAMELEN + 1]; 4191 char ifname[LIFNAMSIZ + 1]; 4192 char flags[10]; /* RTF_ flags */ 4193 uint_t flag_b; 4194 4195 if (!(Aflag || (rp->ipRouteInfo.re_ire_type != IRE_CACHE && 4196 rp->ipRouteInfo.re_ire_type != IRE_BROADCAST && 4197 rp->ipRouteInfo.re_ire_type != IRE_LOCAL))) { 4198 return (first); 4199 } 4200 4201 flag_b = form_v4_route_flags(rp, flags); 4202 4203 if (!ire_filter_match_v4(rp, flag_b)) 4204 return (first); 4205 4206 if (first) { 4207 (void) printf(v4compat ? ire_hdr_v4_compat : ire_hdr_v4, 4208 Vflag ? "IRE" : "Routing"); 4209 (void) printf(Vflag ? ire_hdr_v4_verbose : ire_hdr_v4_normal, 4210 RSECflag ? " Gateway security attributes " : "", 4211 RSECflag ? "-------------------------------" : ""); 4212 first = B_FALSE; 4213 } 4214 4215 if (flag_b & FLF_H) { 4216 (void) pr_addr(rp->ipRouteDest, dstbuf, sizeof (dstbuf)); 4217 } else { 4218 (void) pr_net(rp->ipRouteDest, rp->ipRouteMask, 4219 dstbuf, sizeof (dstbuf)); 4220 } 4221 if (Vflag) { 4222 (void) printf("%-20s %-15s %-20s %-6s %5u%c %4u %3u " 4223 "%-4s%6u %6u %s\n", 4224 dstbuf, 4225 pr_mask(rp->ipRouteMask, maskbuf, sizeof (maskbuf)), 4226 pr_addrnz(rp->ipRouteNextHop, gwbuf, sizeof (gwbuf)), 4227 octetstr(&rp->ipRouteIfIndex, 'a', ifname, sizeof (ifname)), 4228 rp->ipRouteInfo.re_max_frag, 4229 rp->ipRouteInfo.re_frag_flag ? '*' : ' ', 4230 rp->ipRouteInfo.re_rtt, 4231 rp->ipRouteInfo.re_ref, 4232 flags, 4233 rp->ipRouteInfo.re_obpkt, 4234 rp->ipRouteInfo.re_ibpkt, 4235 pr_secattr(attrs)); 4236 } else { 4237 (void) printf("%-20s %-20s %-5s %4u %10u %-9s %s\n", 4238 dstbuf, 4239 pr_addrnz(rp->ipRouteNextHop, gwbuf, sizeof (gwbuf)), 4240 flags, 4241 rp->ipRouteInfo.re_ref, 4242 rp->ipRouteInfo.re_obpkt + rp->ipRouteInfo.re_ibpkt, 4243 octetstr(&rp->ipRouteIfIndex, 'a', 4244 ifname, sizeof (ifname)), 4245 pr_secattr(attrs)); 4246 } 4247 return (first); 4248 } 4249 4250 /* 4251 * Match a user-supplied IP address list against an IPv6 route entry. 4252 * If the user specified "any," then any non-zero address matches. If 4253 * the user specified "none," then only the zero address matches. If 4254 * the user specified a subnet mask length, then use that in matching 4255 * routes (select routes that are at least as specific). If the user 4256 * specified only an address, then use the route's mask (select routes 4257 * that would match that address). IPv4 addresses are ignored. 4258 */ 4259 static boolean_t 4260 v6_addr_match(const Ip6Address *addr, int masklen, const filter_t *fp) 4261 { 4262 const uint8_t *ucp; 4263 int fmasklen; 4264 int i; 4265 char **app; 4266 char *aptr; 4267 4268 if (fp->u.a.f_address == NULL) { 4269 if (IN6_IS_ADDR_UNSPECIFIED(&fp->u.a.f_mask)) /* any */ 4270 return (!IN6_IS_ADDR_UNSPECIFIED(addr)); 4271 return (IN6_IS_ADDR_UNSPECIFIED(addr)); /* "none" */ 4272 } 4273 fmasklen = 0; 4274 /* 'for' loop 1a: */ 4275 for (ucp = fp->u.a.f_mask.s6_addr; 4276 ucp < fp->u.a.f_mask.s6_addr + sizeof (fp->u.a.f_mask.s6_addr); 4277 ucp++) { 4278 if (*ucp != 0xff) { 4279 if (*ucp != 0) 4280 fmasklen += 9 - ffs(*ucp); 4281 break; /* 'for' loop 1a */ 4282 } 4283 fmasklen += 8; 4284 } /* 'for' loop 1a ends */ 4285 if (fmasklen != IPV6_ABITS) { 4286 if (fmasklen > masklen) 4287 return (B_FALSE); 4288 masklen = fmasklen; 4289 } 4290 /* 'for' loop 1b: */ 4291 for (app = fp->u.a.f_address->h_addr_list; (aptr = *app) != NULL; 4292 app++) { 4293 /* LINTED: (note 1) */ 4294 if (IN6_IS_ADDR_V4MAPPED((in6_addr_t *)aptr)) 4295 continue; /* 'for' loop 1b */ 4296 ucp = addr->s6_addr; 4297 for (i = masklen; i >= 8; i -= 8) 4298 if (*ucp++ != *aptr++) 4299 break; /* 'for' loop 1b */ 4300 if (i == 0 || 4301 (i < 8 && ((*ucp ^ *aptr) & ~(0xff >> i)) == 0)) 4302 return (B_TRUE); 4303 } /* 'for' loop 1b ends */ 4304 return (B_FALSE); 4305 } 4306 4307 /* 4308 * Run through the filter list for an IPv6 MIB2 IRE. For a given 4309 * type, if there's at least one filter and all filters of that type 4310 * fail to match, then the route doesn't match and isn't displayed. 4311 * If at least one matches, or none are specified, for each of the 4312 * types, then the route is selected and displayed. 4313 */ 4314 static boolean_t 4315 ire_filter_match_v6(const mib2_ipv6RouteEntry_t *rp6, uint_t flag_b) 4316 { 4317 filter_t *fp; 4318 int idx; 4319 4320 /* 'for' loop 1: */ 4321 for (idx = 0; idx < NFILTERKEYS; idx++) 4322 if ((fp = filters[idx]) != NULL) { 4323 /* 'for' loop 2: */ 4324 for (; fp != NULL; fp = fp->f_next) { 4325 switch (idx) { 4326 case FK_AF: 4327 if (fp->u.f_family != AF_INET6) 4328 /* 'for' loop 2 */ 4329 continue; 4330 break; 4331 case FK_OUTIF: 4332 if (!dev_name_match(&rp6-> 4333 ipv6RouteIfIndex, fp->u.f_ifname)) 4334 /* 'for' loop 2 */ 4335 continue; 4336 break; 4337 case FK_DST: 4338 if (!v6_addr_match(&rp6->ipv6RouteDest, 4339 rp6->ipv6RoutePfxLength, fp)) 4340 /* 'for' loop 2 */ 4341 continue; 4342 break; 4343 case FK_FLAGS: 4344 if ((flag_b & fp->u.f.f_flagset) != 4345 fp->u.f.f_flagset || 4346 (flag_b & fp->u.f.f_flagclear)) 4347 /* 'for' loop 2 */ 4348 continue; 4349 break; 4350 } 4351 break; 4352 } /* 'for' loop 2 ends */ 4353 if (fp == NULL) 4354 return (B_FALSE); 4355 } 4356 /* 'for' loop 1 ends */ 4357 return (B_TRUE); 4358 } 4359 4360 static const char ire_hdr_v6[] = 4361 "\n%s Table: IPv6\n"; 4362 static const char ire_hdr_v6_verbose[] = 4363 " Destination/Mask Gateway If PMTU Rtt " 4364 "Ref Flags Out In/Fwd %s\n" 4365 "--------------------------- --------------------------- ----- ------ ----- " 4366 "--- ----- ------ ------ %s\n"; 4367 static const char ire_hdr_v6_normal[] = 4368 " Destination/Mask Gateway Flags Ref Use " 4369 " If %s\n" 4370 "--------------------------- --------------------------- ----- --- ------- " 4371 "----- %s\n"; 4372 4373 static boolean_t 4374 ire_report_item_v6(const mib2_ipv6RouteEntry_t *rp6, boolean_t first, 4375 const sec_attr_list_t *attrs) 4376 { 4377 char dstbuf[MAXHOSTNAMELEN + 1]; 4378 char gwbuf[MAXHOSTNAMELEN + 1]; 4379 char ifname[LIFNAMSIZ + 1]; 4380 char flags[10]; /* RTF_ flags */ 4381 uint_t flag_b; 4382 4383 if (!(Aflag || (rp6->ipv6RouteInfo.re_ire_type != IRE_CACHE && 4384 rp6->ipv6RouteInfo.re_ire_type != IRE_LOCAL))) { 4385 return (first); 4386 } 4387 4388 flag_b = FLF_U; 4389 (void) strcpy(flags, "U"); 4390 if (rp6->ipv6RouteInfo.re_ire_type == IRE_DEFAULT || 4391 rp6->ipv6RouteInfo.re_ire_type == IRE_PREFIX || 4392 rp6->ipv6RouteInfo.re_ire_type == IRE_HOST || 4393 rp6->ipv6RouteInfo.re_ire_type == IRE_HOST_REDIRECT) { 4394 (void) strcat(flags, "G"); 4395 flag_b |= FLF_G; 4396 } 4397 4398 if (rp6->ipv6RoutePfxLength == IPV6_ABITS) { 4399 (void) strcat(flags, "H"); 4400 flag_b |= FLF_H; 4401 } 4402 4403 if (rp6->ipv6RouteInfo.re_ire_type == IRE_HOST_REDIRECT) { 4404 (void) strcat(flags, "D"); 4405 flag_b |= FLF_D; 4406 } 4407 if (rp6->ipv6RouteInfo.re_ire_type == IRE_CACHE) { 4408 /* Address resolution */ 4409 (void) strcat(flags, "A"); 4410 flag_b |= FLF_A; 4411 } 4412 if (rp6->ipv6RouteInfo.re_ire_type == IRE_LOCAL) { /* Local */ 4413 (void) strcat(flags, "L"); 4414 flag_b |= FLF_L; 4415 } 4416 if (rp6->ipv6RouteInfo.re_flags & RTF_MULTIRT) { 4417 (void) strcat(flags, "M"); /* Multiroute */ 4418 flag_b |= FLF_M; 4419 } 4420 if (rp6->ipv6RouteInfo.re_flags & RTF_SETSRC) { 4421 (void) strcat(flags, "S"); /* Setsrc */ 4422 flag_b |= FLF_S; 4423 } 4424 4425 if (!ire_filter_match_v6(rp6, flag_b)) 4426 return (first); 4427 4428 if (first) { 4429 (void) printf(ire_hdr_v6, Vflag ? "IRE" : "Routing"); 4430 (void) printf(Vflag ? ire_hdr_v6_verbose : ire_hdr_v6_normal, 4431 RSECflag ? " Gateway security attributes " : "", 4432 RSECflag ? "-------------------------------" : ""); 4433 first = B_FALSE; 4434 } 4435 4436 if (Vflag) { 4437 (void) printf("%-27s %-27s %-5s %5u%c %5u %3u " 4438 "%-5s %6u %6u %s\n", 4439 pr_prefix6(&rp6->ipv6RouteDest, 4440 rp6->ipv6RoutePfxLength, dstbuf, sizeof (dstbuf)), 4441 IN6_IS_ADDR_UNSPECIFIED(&rp6->ipv6RouteNextHop) ? 4442 " --" : 4443 pr_addr6(&rp6->ipv6RouteNextHop, gwbuf, sizeof (gwbuf)), 4444 octetstr(&rp6->ipv6RouteIfIndex, 'a', 4445 ifname, sizeof (ifname)), 4446 rp6->ipv6RouteInfo.re_max_frag, 4447 rp6->ipv6RouteInfo.re_frag_flag ? '*' : ' ', 4448 rp6->ipv6RouteInfo.re_rtt, 4449 rp6->ipv6RouteInfo.re_ref, 4450 flags, 4451 rp6->ipv6RouteInfo.re_obpkt, 4452 rp6->ipv6RouteInfo.re_ibpkt, 4453 pr_secattr(attrs)); 4454 } else { 4455 (void) printf("%-27s %-27s %-5s %3u %7u %-5s %s\n", 4456 pr_prefix6(&rp6->ipv6RouteDest, 4457 rp6->ipv6RoutePfxLength, dstbuf, sizeof (dstbuf)), 4458 IN6_IS_ADDR_UNSPECIFIED(&rp6->ipv6RouteNextHop) ? 4459 " --" : 4460 pr_addr6(&rp6->ipv6RouteNextHop, gwbuf, sizeof (gwbuf)), 4461 flags, 4462 rp6->ipv6RouteInfo.re_ref, 4463 rp6->ipv6RouteInfo.re_obpkt + rp6->ipv6RouteInfo.re_ibpkt, 4464 octetstr(&rp6->ipv6RouteIfIndex, 'a', 4465 ifname, sizeof (ifname)), 4466 pr_secattr(attrs)); 4467 } 4468 return (first); 4469 } 4470 4471 /* 4472 * Common attribute-gathering routine for all transports. 4473 */ 4474 static mib2_transportMLPEntry_t ** 4475 gather_attrs(const mib_item_t *item, int group, int mib_id, int esize) 4476 { 4477 int transport_count = 0; 4478 const mib_item_t *iptr; 4479 mib2_transportMLPEntry_t **attrs, *tme; 4480 4481 for (iptr = item; iptr != NULL; iptr = iptr->next_item) { 4482 if (iptr->group == group && iptr->mib_id == mib_id) 4483 transport_count += iptr->length / esize; 4484 } 4485 if (transport_count <= 0) 4486 return (NULL); 4487 attrs = calloc(transport_count, sizeof (*attrs)); 4488 if (attrs == NULL) { 4489 perror("gather_attrs calloc failed"); 4490 return (NULL); 4491 } 4492 for (iptr = item; iptr != NULL; iptr = iptr->next_item) { 4493 if (iptr->group == group && iptr->mib_id == EXPER_XPORT_MLP) { 4494 for (tme = iptr->valp; 4495 (char *)tme < (char *)iptr->valp + iptr->length; 4496 /* LINTED: (note 1) */ 4497 tme = (mib2_transportMLPEntry_t *)((char *)tme + 4498 transportMLPSize)) { 4499 attrs[tme->tme_connidx] = tme; 4500 } 4501 } 4502 } 4503 return (attrs); 4504 } 4505 4506 static void 4507 print_transport_label(const mib2_transportMLPEntry_t *attr) 4508 { 4509 if (!RSECflag || attr == NULL) 4510 return; 4511 4512 if (bisinvalid(&attr->tme_label)) 4513 (void) printf(" INVALID\n"); 4514 else 4515 (void) printf(" %s\n", sl_to_str(&attr->tme_label)); 4516 } 4517 4518 /* ------------------------------ TCP_REPORT------------------------------- */ 4519 4520 static const char tcp_hdr_v4[] = 4521 "\nTCP: IPv4\n"; 4522 static const char tcp_hdr_v4_compat[] = 4523 "\nTCP\n"; 4524 static const char tcp_hdr_v4_verbose[] = 4525 "Local/Remote Address Swind Snext Suna Rwind Rnext Rack " 4526 " Rto Mss State\n" 4527 "-------------------- ----- -------- -------- ----- -------- -------- " 4528 "----- ----- -----------\n"; 4529 static const char tcp_hdr_v4_normal[] = 4530 " Local Address Remote Address Swind Send-Q Rwind Recv-Q " 4531 " State\n" 4532 "-------------------- -------------------- ----- ------ ----- ------ " 4533 "-----------\n"; 4534 4535 static const char tcp_hdr_v6[] = 4536 "\nTCP: IPv6\n"; 4537 static const char tcp_hdr_v6_verbose[] = 4538 "Local/Remote Address Swind Snext Suna Rwind Rnext " 4539 " Rack Rto Mss State If\n" 4540 "--------------------------------- ----- -------- -------- ----- -------- " 4541 "-------- ----- ----- ----------- -----\n"; 4542 static const char tcp_hdr_v6_normal[] = 4543 " Local Address Remote Address " 4544 "Swind Send-Q Rwind Recv-Q State If\n" 4545 "--------------------------------- --------------------------------- " 4546 "----- ------ ----- ------ ----------- -----\n"; 4547 4548 static boolean_t tcp_report_item_v4(const mib2_tcpConnEntry_t *, 4549 boolean_t first, const mib2_transportMLPEntry_t *); 4550 static boolean_t tcp_report_item_v6(const mib2_tcp6ConnEntry_t *, 4551 boolean_t first, const mib2_transportMLPEntry_t *); 4552 4553 static void 4554 tcp_report(const mib_item_t *item) 4555 { 4556 int jtemp = 0; 4557 boolean_t print_hdr_once_v4 = B_TRUE; 4558 boolean_t print_hdr_once_v6 = B_TRUE; 4559 mib2_tcpConnEntry_t *tp; 4560 mib2_tcp6ConnEntry_t *tp6; 4561 mib2_transportMLPEntry_t **v4_attrs, **v6_attrs; 4562 mib2_transportMLPEntry_t **v4a, **v6a; 4563 mib2_transportMLPEntry_t *aptr; 4564 4565 if (!protocol_selected(IPPROTO_TCP)) 4566 return; 4567 4568 /* 4569 * Preparation pass: the kernel returns separate entries for TCP 4570 * connection table entries and Multilevel Port attributes. We loop 4571 * through the attributes first and set up an array for each address 4572 * family. 4573 */ 4574 v4_attrs = family_selected(AF_INET) && RSECflag ? 4575 gather_attrs(item, MIB2_TCP, MIB2_TCP_CONN, tcpConnEntrySize) : 4576 NULL; 4577 v6_attrs = family_selected(AF_INET6) && RSECflag ? 4578 gather_attrs(item, MIB2_TCP6, MIB2_TCP6_CONN, tcp6ConnEntrySize) : 4579 NULL; 4580 4581 /* 'for' loop 1: */ 4582 v4a = v4_attrs; 4583 v6a = v6_attrs; 4584 for (; item != NULL; item = item->next_item) { 4585 if (Dflag) { 4586 (void) printf("\n--- Entry %d ---\n", ++jtemp); 4587 (void) printf("Group = %d, mib_id = %d, " 4588 "length = %d, valp = 0x%p\n", 4589 item->group, item->mib_id, 4590 item->length, item->valp); 4591 } 4592 4593 if (!((item->group == MIB2_TCP && 4594 item->mib_id == MIB2_TCP_CONN) || 4595 (item->group == MIB2_TCP6 && 4596 item->mib_id == MIB2_TCP6_CONN))) 4597 continue; /* 'for' loop 1 */ 4598 4599 if (item->group == MIB2_TCP && !family_selected(AF_INET)) 4600 continue; /* 'for' loop 1 */ 4601 else if (item->group == MIB2_TCP6 && !family_selected(AF_INET6)) 4602 continue; /* 'for' loop 1 */ 4603 4604 if (item->group == MIB2_TCP) { 4605 for (tp = (mib2_tcpConnEntry_t *)item->valp; 4606 (char *)tp < (char *)item->valp + item->length; 4607 /* LINTED: (note 1) */ 4608 tp = (mib2_tcpConnEntry_t *)((char *)tp + 4609 tcpConnEntrySize)) { 4610 aptr = v4a == NULL ? NULL : *v4a++; 4611 print_hdr_once_v4 = tcp_report_item_v4(tp, 4612 print_hdr_once_v4, aptr); 4613 } 4614 } else { 4615 for (tp6 = (mib2_tcp6ConnEntry_t *)item->valp; 4616 (char *)tp6 < (char *)item->valp + item->length; 4617 /* LINTED: (note 1) */ 4618 tp6 = (mib2_tcp6ConnEntry_t *)((char *)tp6 + 4619 tcp6ConnEntrySize)) { 4620 aptr = v6a == NULL ? NULL : *v6a++; 4621 print_hdr_once_v6 = tcp_report_item_v6(tp6, 4622 print_hdr_once_v6, aptr); 4623 } 4624 } 4625 } /* 'for' loop 1 ends */ 4626 (void) fflush(stdout); 4627 4628 if (v4_attrs != NULL) 4629 free(v4_attrs); 4630 if (v6_attrs != NULL) 4631 free(v6_attrs); 4632 } 4633 4634 static boolean_t 4635 tcp_report_item_v4(const mib2_tcpConnEntry_t *tp, boolean_t first, 4636 const mib2_transportMLPEntry_t *attr) 4637 { 4638 /* 4639 * lname and fname below are for the hostname as well as the portname 4640 * There is no limit on portname length so we assume MAXHOSTNAMELEN 4641 * as the limit 4642 */ 4643 char lname[MAXHOSTNAMELEN + MAXHOSTNAMELEN + 1]; 4644 char fname[MAXHOSTNAMELEN + MAXHOSTNAMELEN + 1]; 4645 4646 if (!(Aflag || tp->tcpConnEntryInfo.ce_state >= TCPS_ESTABLISHED)) 4647 return (first); /* Nothing to print */ 4648 4649 if (first) { 4650 (void) printf(v4compat ? tcp_hdr_v4_compat : tcp_hdr_v4); 4651 (void) printf(Vflag ? tcp_hdr_v4_verbose : tcp_hdr_v4_normal); 4652 } 4653 4654 if (Vflag) { 4655 (void) printf("%-20s\n%-20s %5u %08x %08x %5u %08x %08x " 4656 "%5u %5u %s\n", 4657 pr_ap(tp->tcpConnLocalAddress, 4658 tp->tcpConnLocalPort, "tcp", lname, sizeof (lname)), 4659 pr_ap(tp->tcpConnRemAddress, 4660 tp->tcpConnRemPort, "tcp", fname, sizeof (fname)), 4661 tp->tcpConnEntryInfo.ce_swnd, 4662 tp->tcpConnEntryInfo.ce_snxt, 4663 tp->tcpConnEntryInfo.ce_suna, 4664 tp->tcpConnEntryInfo.ce_rwnd, 4665 tp->tcpConnEntryInfo.ce_rnxt, 4666 tp->tcpConnEntryInfo.ce_rack, 4667 tp->tcpConnEntryInfo.ce_rto, 4668 tp->tcpConnEntryInfo.ce_mss, 4669 mitcp_state(tp->tcpConnEntryInfo.ce_state, attr)); 4670 } else { 4671 int sq = (int)tp->tcpConnEntryInfo.ce_snxt - 4672 (int)tp->tcpConnEntryInfo.ce_suna - 1; 4673 int rq = (int)tp->tcpConnEntryInfo.ce_rnxt - 4674 (int)tp->tcpConnEntryInfo.ce_rack; 4675 4676 (void) printf("%-20s %-20s %5u %6d %5u %6d %s\n", 4677 pr_ap(tp->tcpConnLocalAddress, 4678 tp->tcpConnLocalPort, "tcp", lname, sizeof (lname)), 4679 pr_ap(tp->tcpConnRemAddress, 4680 tp->tcpConnRemPort, "tcp", fname, sizeof (fname)), 4681 tp->tcpConnEntryInfo.ce_swnd, 4682 (sq >= 0) ? sq : 0, 4683 tp->tcpConnEntryInfo.ce_rwnd, 4684 (rq >= 0) ? rq : 0, 4685 mitcp_state(tp->tcpConnEntryInfo.ce_state, attr)); 4686 } 4687 4688 print_transport_label(attr); 4689 4690 return (B_FALSE); 4691 } 4692 4693 static boolean_t 4694 tcp_report_item_v6(const mib2_tcp6ConnEntry_t *tp6, boolean_t first, 4695 const mib2_transportMLPEntry_t *attr) 4696 { 4697 /* 4698 * lname and fname below are for the hostname as well as the portname 4699 * There is no limit on portname length so we assume MAXHOSTNAMELEN 4700 * as the limit 4701 */ 4702 char lname[MAXHOSTNAMELEN + MAXHOSTNAMELEN + 1]; 4703 char fname[MAXHOSTNAMELEN + MAXHOSTNAMELEN + 1]; 4704 char ifname[LIFNAMSIZ + 1]; 4705 char *ifnamep; 4706 4707 if (!(Aflag || tp6->tcp6ConnEntryInfo.ce_state >= TCPS_ESTABLISHED)) 4708 return (first); /* Nothing to print */ 4709 4710 if (first) { 4711 (void) printf(tcp_hdr_v6); 4712 (void) printf(Vflag ? tcp_hdr_v6_verbose : tcp_hdr_v6_normal); 4713 } 4714 4715 ifnamep = (tp6->tcp6ConnIfIndex != 0) ? 4716 if_indextoname(tp6->tcp6ConnIfIndex, ifname) : NULL; 4717 if (ifnamep == NULL) 4718 ifnamep = ""; 4719 4720 if (Vflag) { 4721 (void) printf("%-33s\n%-33s %5u %08x %08x %5u %08x %08x " 4722 "%5u %5u %-11s %s\n", 4723 pr_ap6(&tp6->tcp6ConnLocalAddress, 4724 tp6->tcp6ConnLocalPort, "tcp", lname, sizeof (lname)), 4725 pr_ap6(&tp6->tcp6ConnRemAddress, 4726 tp6->tcp6ConnRemPort, "tcp", fname, sizeof (fname)), 4727 tp6->tcp6ConnEntryInfo.ce_swnd, 4728 tp6->tcp6ConnEntryInfo.ce_snxt, 4729 tp6->tcp6ConnEntryInfo.ce_suna, 4730 tp6->tcp6ConnEntryInfo.ce_rwnd, 4731 tp6->tcp6ConnEntryInfo.ce_rnxt, 4732 tp6->tcp6ConnEntryInfo.ce_rack, 4733 tp6->tcp6ConnEntryInfo.ce_rto, 4734 tp6->tcp6ConnEntryInfo.ce_mss, 4735 mitcp_state(tp6->tcp6ConnEntryInfo.ce_state, attr), 4736 ifnamep); 4737 } else { 4738 int sq = (int)tp6->tcp6ConnEntryInfo.ce_snxt - 4739 (int)tp6->tcp6ConnEntryInfo.ce_suna - 1; 4740 int rq = (int)tp6->tcp6ConnEntryInfo.ce_rnxt - 4741 (int)tp6->tcp6ConnEntryInfo.ce_rack; 4742 4743 (void) printf("%-33s %-33s %5u %6d %5u %6d %-11s %s\n", 4744 pr_ap6(&tp6->tcp6ConnLocalAddress, 4745 tp6->tcp6ConnLocalPort, "tcp", lname, sizeof (lname)), 4746 pr_ap6(&tp6->tcp6ConnRemAddress, 4747 tp6->tcp6ConnRemPort, "tcp", fname, sizeof (fname)), 4748 tp6->tcp6ConnEntryInfo.ce_swnd, 4749 (sq >= 0) ? sq : 0, 4750 tp6->tcp6ConnEntryInfo.ce_rwnd, 4751 (rq >= 0) ? rq : 0, 4752 mitcp_state(tp6->tcp6ConnEntryInfo.ce_state, attr), 4753 ifnamep); 4754 } 4755 4756 print_transport_label(attr); 4757 4758 return (B_FALSE); 4759 } 4760 4761 /* ------------------------------- UDP_REPORT------------------------------- */ 4762 4763 static boolean_t udp_report_item_v4(const mib2_udpEntry_t *ude, 4764 boolean_t first, const mib2_transportMLPEntry_t *attr); 4765 static boolean_t udp_report_item_v6(const mib2_udp6Entry_t *ude6, 4766 boolean_t first, const mib2_transportMLPEntry_t *attr); 4767 4768 static const char udp_hdr_v4[] = 4769 " Local Address Remote Address State\n" 4770 "-------------------- -------------------- ----------\n"; 4771 4772 static const char udp_hdr_v6[] = 4773 " Local Address Remote Address " 4774 " State If\n" 4775 "--------------------------------- --------------------------------- " 4776 "---------- -----\n"; 4777 4778 static void 4779 udp_report(const mib_item_t *item) 4780 { 4781 int jtemp = 0; 4782 boolean_t print_hdr_once_v4 = B_TRUE; 4783 boolean_t print_hdr_once_v6 = B_TRUE; 4784 mib2_udpEntry_t *ude; 4785 mib2_udp6Entry_t *ude6; 4786 mib2_transportMLPEntry_t **v4_attrs, **v6_attrs; 4787 mib2_transportMLPEntry_t **v4a, **v6a; 4788 mib2_transportMLPEntry_t *aptr; 4789 4790 if (!protocol_selected(IPPROTO_UDP)) 4791 return; 4792 4793 /* 4794 * Preparation pass: the kernel returns separate entries for UDP 4795 * connection table entries and Multilevel Port attributes. We loop 4796 * through the attributes first and set up an array for each address 4797 * family. 4798 */ 4799 v4_attrs = family_selected(AF_INET) && RSECflag ? 4800 gather_attrs(item, MIB2_UDP, MIB2_UDP_ENTRY, udpEntrySize) : NULL; 4801 v6_attrs = family_selected(AF_INET6) && RSECflag ? 4802 gather_attrs(item, MIB2_UDP6, MIB2_UDP6_ENTRY, udp6EntrySize) : 4803 NULL; 4804 4805 v4a = v4_attrs; 4806 v6a = v6_attrs; 4807 /* 'for' loop 1: */ 4808 for (; item; item = item->next_item) { 4809 if (Dflag) { 4810 (void) printf("\n--- Entry %d ---\n", ++jtemp); 4811 (void) printf("Group = %d, mib_id = %d, " 4812 "length = %d, valp = 0x%p\n", 4813 item->group, item->mib_id, 4814 item->length, item->valp); 4815 } 4816 if (!((item->group == MIB2_UDP && 4817 item->mib_id == MIB2_UDP_ENTRY) || 4818 (item->group == MIB2_UDP6 && 4819 item->mib_id == MIB2_UDP6_ENTRY))) 4820 continue; /* 'for' loop 1 */ 4821 4822 if (item->group == MIB2_UDP && !family_selected(AF_INET)) 4823 continue; /* 'for' loop 1 */ 4824 else if (item->group == MIB2_UDP6 && !family_selected(AF_INET6)) 4825 continue; /* 'for' loop 1 */ 4826 4827 /* xxx.xxx.xxx.xxx,pppp sss... */ 4828 if (item->group == MIB2_UDP) { 4829 for (ude = (mib2_udpEntry_t *)item->valp; 4830 (char *)ude < (char *)item->valp + item->length; 4831 /* LINTED: (note 1) */ 4832 ude = (mib2_udpEntry_t *)((char *)ude + 4833 udpEntrySize)) { 4834 aptr = v4a == NULL ? NULL : *v4a++; 4835 print_hdr_once_v4 = udp_report_item_v4(ude, 4836 print_hdr_once_v4, aptr); 4837 } 4838 } else { 4839 for (ude6 = (mib2_udp6Entry_t *)item->valp; 4840 (char *)ude6 < (char *)item->valp + item->length; 4841 /* LINTED: (note 1) */ 4842 ude6 = (mib2_udp6Entry_t *)((char *)ude6 + 4843 udp6EntrySize)) { 4844 aptr = v6a == NULL ? NULL : *v6a++; 4845 print_hdr_once_v6 = udp_report_item_v6(ude6, 4846 print_hdr_once_v6, aptr); 4847 } 4848 } 4849 } /* 'for' loop 1 ends */ 4850 (void) fflush(stdout); 4851 4852 if (v4_attrs != NULL) 4853 free(v4_attrs); 4854 if (v6_attrs != NULL) 4855 free(v6_attrs); 4856 } 4857 4858 static boolean_t 4859 udp_report_item_v4(const mib2_udpEntry_t *ude, boolean_t first, 4860 const mib2_transportMLPEntry_t *attr) 4861 { 4862 char lname[MAXHOSTNAMELEN + MAXHOSTNAMELEN + 1]; 4863 /* hostname + portname */ 4864 4865 if (!(Aflag || ude->udpEntryInfo.ue_state >= MIB2_UDP_connected)) 4866 return (first); /* Nothing to print */ 4867 4868 if (first) { 4869 (void) printf(v4compat ? "\nUDP\n" : "\nUDP: IPv4\n"); 4870 (void) printf(udp_hdr_v4); 4871 first = B_FALSE; 4872 } 4873 4874 (void) printf("%-20s ", 4875 pr_ap(ude->udpLocalAddress, ude->udpLocalPort, "udp", 4876 lname, sizeof (lname))); 4877 (void) printf("%-20s %s\n", 4878 ude->udpEntryInfo.ue_state == MIB2_UDP_connected ? 4879 pr_ap(ude->udpEntryInfo.ue_RemoteAddress, 4880 ude->udpEntryInfo.ue_RemotePort, "udp", lname, sizeof (lname)) : 4881 "", 4882 miudp_state(ude->udpEntryInfo.ue_state, attr)); 4883 4884 /* 4885 * UDP sockets don't have remote attributes, so there's no need to 4886 * print them here. 4887 */ 4888 4889 return (first); 4890 } 4891 4892 static boolean_t 4893 udp_report_item_v6(const mib2_udp6Entry_t *ude6, boolean_t first, 4894 const mib2_transportMLPEntry_t *attr) 4895 { 4896 char lname[MAXHOSTNAMELEN + MAXHOSTNAMELEN + 1]; 4897 /* hostname + portname */ 4898 char ifname[LIFNAMSIZ + 1]; 4899 const char *ifnamep; 4900 4901 if (!(Aflag || ude6->udp6EntryInfo.ue_state >= MIB2_UDP_connected)) 4902 return (first); /* Nothing to print */ 4903 4904 if (first) { 4905 (void) printf("\nUDP: IPv6\n"); 4906 (void) printf(udp_hdr_v6); 4907 first = B_FALSE; 4908 } 4909 4910 ifnamep = (ude6->udp6IfIndex != 0) ? 4911 if_indextoname(ude6->udp6IfIndex, ifname) : NULL; 4912 4913 (void) printf("%-33s ", 4914 pr_ap6(&ude6->udp6LocalAddress, 4915 ude6->udp6LocalPort, "udp", lname, sizeof (lname))); 4916 (void) printf("%-33s %-10s %s\n", 4917 ude6->udp6EntryInfo.ue_state == MIB2_UDP_connected ? 4918 pr_ap6(&ude6->udp6EntryInfo.ue_RemoteAddress, 4919 ude6->udp6EntryInfo.ue_RemotePort, "udp", lname, sizeof (lname)) : 4920 "", 4921 miudp_state(ude6->udp6EntryInfo.ue_state, attr), 4922 ifnamep == NULL ? "" : ifnamep); 4923 4924 /* 4925 * UDP sockets don't have remote attributes, so there's no need to 4926 * print them here. 4927 */ 4928 4929 return (first); 4930 } 4931 4932 /* ------------------------------ SCTP_REPORT------------------------------- */ 4933 4934 static const char sctp_hdr[] = 4935 "\nSCTP:"; 4936 static const char sctp_hdr_normal[] = 4937 " Local Address Remote Address " 4938 "Swind Send-Q Rwind Recv-Q StrsI/O State\n" 4939 "------------------------------- ------------------------------- " 4940 "------ ------ ------ ------ ------- -----------"; 4941 4942 static const char * 4943 nssctp_state(int state, const mib2_transportMLPEntry_t *attr) 4944 { 4945 static char sctpsbuf[50]; 4946 const char *cp; 4947 4948 switch (state) { 4949 case MIB2_SCTP_closed: 4950 cp = "CLOSED"; 4951 break; 4952 case MIB2_SCTP_cookieWait: 4953 cp = "COOKIE_WAIT"; 4954 break; 4955 case MIB2_SCTP_cookieEchoed: 4956 cp = "COOKIE_ECHOED"; 4957 break; 4958 case MIB2_SCTP_established: 4959 cp = "ESTABLISHED"; 4960 break; 4961 case MIB2_SCTP_shutdownPending: 4962 cp = "SHUTDOWN_PENDING"; 4963 break; 4964 case MIB2_SCTP_shutdownSent: 4965 cp = "SHUTDOWN_SENT"; 4966 break; 4967 case MIB2_SCTP_shutdownReceived: 4968 cp = "SHUTDOWN_RECEIVED"; 4969 break; 4970 case MIB2_SCTP_shutdownAckSent: 4971 cp = "SHUTDOWN_ACK_SENT"; 4972 break; 4973 case MIB2_SCTP_listen: 4974 cp = "LISTEN"; 4975 break; 4976 default: 4977 (void) snprintf(sctpsbuf, sizeof (sctpsbuf), 4978 "UNKNOWN STATE(%d)", state); 4979 cp = sctpsbuf; 4980 break; 4981 } 4982 4983 if (RSECflag && attr != NULL && attr->tme_flags != 0) { 4984 if (cp != sctpsbuf) { 4985 (void) strlcpy(sctpsbuf, cp, sizeof (sctpsbuf)); 4986 cp = sctpsbuf; 4987 } 4988 if (attr->tme_flags & MIB2_TMEF_PRIVATE) 4989 (void) strlcat(sctpsbuf, " P", sizeof (sctpsbuf)); 4990 if (attr->tme_flags & MIB2_TMEF_SHARED) 4991 (void) strlcat(sctpsbuf, " S", sizeof (sctpsbuf)); 4992 } 4993 4994 return (cp); 4995 } 4996 4997 static const mib2_sctpConnRemoteEntry_t * 4998 sctp_getnext_rem(const mib_item_t **itemp, 4999 const mib2_sctpConnRemoteEntry_t *current, uint32_t associd) 5000 { 5001 const mib_item_t *item = *itemp; 5002 const mib2_sctpConnRemoteEntry_t *sre; 5003 5004 for (; item != NULL; item = item->next_item, current = NULL) { 5005 if (!(item->group == MIB2_SCTP && 5006 item->mib_id == MIB2_SCTP_CONN_REMOTE)) { 5007 continue; 5008 } 5009 5010 if (current != NULL) { 5011 /* LINTED: (note 1) */ 5012 sre = (const mib2_sctpConnRemoteEntry_t *) 5013 ((const char *)current + sctpRemoteEntrySize); 5014 } else { 5015 sre = item->valp; 5016 } 5017 for (; (char *)sre < (char *)item->valp + item->length; 5018 /* LINTED: (note 1) */ 5019 sre = (const mib2_sctpConnRemoteEntry_t *) 5020 ((const char *)sre + sctpRemoteEntrySize)) { 5021 if (sre->sctpAssocId != associd) { 5022 continue; 5023 } 5024 *itemp = item; 5025 return (sre); 5026 } 5027 } 5028 *itemp = NULL; 5029 return (NULL); 5030 } 5031 5032 static const mib2_sctpConnLocalEntry_t * 5033 sctp_getnext_local(const mib_item_t **itemp, 5034 const mib2_sctpConnLocalEntry_t *current, uint32_t associd) 5035 { 5036 const mib_item_t *item = *itemp; 5037 const mib2_sctpConnLocalEntry_t *sle; 5038 5039 for (; item != NULL; item = item->next_item, current = NULL) { 5040 if (!(item->group == MIB2_SCTP && 5041 item->mib_id == MIB2_SCTP_CONN_LOCAL)) { 5042 continue; 5043 } 5044 5045 if (current != NULL) { 5046 /* LINTED: (note 1) */ 5047 sle = (const mib2_sctpConnLocalEntry_t *) 5048 ((const char *)current + sctpLocalEntrySize); 5049 } else { 5050 sle = item->valp; 5051 } 5052 for (; (char *)sle < (char *)item->valp + item->length; 5053 /* LINTED: (note 1) */ 5054 sle = (const mib2_sctpConnLocalEntry_t *) 5055 ((const char *)sle + sctpLocalEntrySize)) { 5056 if (sle->sctpAssocId != associd) { 5057 continue; 5058 } 5059 *itemp = item; 5060 return (sle); 5061 } 5062 } 5063 *itemp = NULL; 5064 return (NULL); 5065 } 5066 5067 static void 5068 sctp_pr_addr(int type, char *name, int namelen, const in6_addr_t *addr, 5069 int port) 5070 { 5071 ipaddr_t v4addr; 5072 in6_addr_t v6addr; 5073 5074 /* 5075 * Address is either a v4 mapped or v6 addr. If 5076 * it's a v4 mapped, convert to v4 before 5077 * displaying. 5078 */ 5079 switch (type) { 5080 case MIB2_SCTP_ADDR_V4: 5081 /* v4 */ 5082 v6addr = *addr; 5083 5084 IN6_V4MAPPED_TO_IPADDR(&v6addr, v4addr); 5085 if (port > 0) { 5086 (void) pr_ap(v4addr, port, "sctp", name, namelen); 5087 } else { 5088 (void) pr_addr(v4addr, name, namelen); 5089 } 5090 break; 5091 5092 case MIB2_SCTP_ADDR_V6: 5093 /* v6 */ 5094 if (port > 0) { 5095 (void) pr_ap6(addr, port, "sctp", name, namelen); 5096 } else { 5097 (void) pr_addr6(addr, name, namelen); 5098 } 5099 break; 5100 5101 default: 5102 (void) snprintf(name, namelen, "<unknown addr type>"); 5103 break; 5104 } 5105 } 5106 5107 static void 5108 sctp_conn_report_item(const mib_item_t *head, const mib2_sctpConnEntry_t *sp, 5109 const mib2_transportMLPEntry_t *attr) 5110 { 5111 char lname[MAXHOSTNAMELEN + MAXHOSTNAMELEN + 1]; 5112 char fname[MAXHOSTNAMELEN + MAXHOSTNAMELEN + 1]; 5113 const mib2_sctpConnRemoteEntry_t *sre = NULL; 5114 const mib2_sctpConnLocalEntry_t *sle = NULL; 5115 const mib_item_t *local = head; 5116 const mib_item_t *remote = head; 5117 uint32_t id = sp->sctpAssocId; 5118 boolean_t printfirst = B_TRUE; 5119 5120 sctp_pr_addr(sp->sctpAssocRemPrimAddrType, fname, sizeof (fname), 5121 &sp->sctpAssocRemPrimAddr, sp->sctpAssocRemPort); 5122 sctp_pr_addr(sp->sctpAssocRemPrimAddrType, lname, sizeof (lname), 5123 &sp->sctpAssocLocPrimAddr, sp->sctpAssocLocalPort); 5124 5125 (void) printf("%-31s %-31s %6u %6d %6u %6d %3d/%-3d %s\n", 5126 lname, fname, 5127 sp->sctpConnEntryInfo.ce_swnd, 5128 sp->sctpConnEntryInfo.ce_sendq, 5129 sp->sctpConnEntryInfo.ce_rwnd, 5130 sp->sctpConnEntryInfo.ce_recvq, 5131 sp->sctpAssocInStreams, sp->sctpAssocOutStreams, 5132 nssctp_state(sp->sctpAssocState, attr)); 5133 5134 print_transport_label(attr); 5135 5136 if (!Vflag) { 5137 return; 5138 } 5139 5140 /* Print remote addresses/local addresses on following lines */ 5141 while ((sre = sctp_getnext_rem(&remote, sre, id)) != NULL) { 5142 if (!IN6_ARE_ADDR_EQUAL(&sre->sctpAssocRemAddr, 5143 &sp->sctpAssocRemPrimAddr)) { 5144 if (printfirst == B_TRUE) { 5145 (void) fputs("\t<Remote: ", stdout); 5146 printfirst = B_FALSE; 5147 } else { 5148 (void) fputs(", ", stdout); 5149 } 5150 sctp_pr_addr(sre->sctpAssocRemAddrType, fname, 5151 sizeof (fname), &sre->sctpAssocRemAddr, -1); 5152 if (sre->sctpAssocRemAddrActive == MIB2_SCTP_ACTIVE) { 5153 (void) fputs(fname, stdout); 5154 } else { 5155 (void) printf("(%s)", fname); 5156 } 5157 } 5158 } 5159 if (printfirst == B_FALSE) { 5160 (void) puts(">"); 5161 printfirst = B_TRUE; 5162 } 5163 while ((sle = sctp_getnext_local(&local, sle, id)) != NULL) { 5164 if (!IN6_ARE_ADDR_EQUAL(&sle->sctpAssocLocalAddr, 5165 &sp->sctpAssocLocPrimAddr)) { 5166 if (printfirst == B_TRUE) { 5167 (void) fputs("\t<Local: ", stdout); 5168 printfirst = B_FALSE; 5169 } else { 5170 (void) fputs(", ", stdout); 5171 } 5172 sctp_pr_addr(sle->sctpAssocLocalAddrType, lname, 5173 sizeof (lname), &sle->sctpAssocLocalAddr, -1); 5174 (void) fputs(lname, stdout); 5175 } 5176 } 5177 if (printfirst == B_FALSE) { 5178 (void) puts(">"); 5179 } 5180 } 5181 5182 static void 5183 sctp_report(const mib_item_t *item) 5184 { 5185 const mib_item_t *head; 5186 const mib2_sctpConnEntry_t *sp; 5187 boolean_t first = B_TRUE; 5188 mib2_transportMLPEntry_t **attrs, **aptr; 5189 mib2_transportMLPEntry_t *attr; 5190 5191 /* 5192 * Preparation pass: the kernel returns separate entries for SCTP 5193 * connection table entries and Multilevel Port attributes. We loop 5194 * through the attributes first and set up an array for each address 5195 * family. 5196 */ 5197 attrs = RSECflag ? 5198 gather_attrs(item, MIB2_SCTP, MIB2_SCTP_CONN, sctpEntrySize) : 5199 NULL; 5200 5201 aptr = attrs; 5202 head = item; 5203 for (; item != NULL; item = item->next_item) { 5204 5205 if (!(item->group == MIB2_SCTP && 5206 item->mib_id == MIB2_SCTP_CONN)) 5207 continue; 5208 5209 for (sp = item->valp; 5210 (char *)sp < (char *)item->valp + item->length; 5211 /* LINTED: (note 1) */ 5212 sp = (mib2_sctpConnEntry_t *)((char *)sp + sctpEntrySize)) { 5213 attr = aptr == NULL ? NULL : *aptr++; 5214 if (Aflag || 5215 sp->sctpAssocState >= MIB2_SCTP_established) { 5216 if (first == B_TRUE) { 5217 (void) puts(sctp_hdr); 5218 (void) puts(sctp_hdr_normal); 5219 first = B_FALSE; 5220 } 5221 sctp_conn_report_item(head, sp, attr); 5222 } 5223 } 5224 } 5225 if (attrs != NULL) 5226 free(attrs); 5227 } 5228 5229 static char * 5230 plural(int n) 5231 { 5232 return (n != 1 ? "s" : ""); 5233 } 5234 5235 static char * 5236 pluraly(int n) 5237 { 5238 return (n != 1 ? "ies" : "y"); 5239 } 5240 5241 static char * 5242 plurales(int n) 5243 { 5244 return (n != 1 ? "es" : ""); 5245 } 5246 5247 static char * 5248 pktscale(n) 5249 int n; 5250 { 5251 static char buf[6]; 5252 char t; 5253 5254 if (n < 1024) { 5255 t = ' '; 5256 } else if (n < 1024 * 1024) { 5257 t = 'k'; 5258 n /= 1024; 5259 } else if (n < 1024 * 1024 * 1024) { 5260 t = 'm'; 5261 n /= 1024 * 1024; 5262 } else { 5263 t = 'g'; 5264 n /= 1024 * 1024 * 1024; 5265 } 5266 5267 (void) snprintf(buf, sizeof (buf), "%4u%c", n, t); 5268 return (buf); 5269 } 5270 5271 /* --------------------- mrt_report (netstat -m) -------------------------- */ 5272 5273 static void 5274 mrt_report(mib_item_t *item) 5275 { 5276 int jtemp = 0; 5277 struct vifctl *vip; 5278 vifi_t vifi; 5279 struct mfcctl *mfccp; 5280 int numvifs = 0; 5281 int nmfc = 0; 5282 char abuf[MAXHOSTNAMELEN + 1]; 5283 5284 if (!(family_selected(AF_INET))) 5285 return; 5286 5287 /* 'for' loop 1: */ 5288 for (; item; item = item->next_item) { 5289 if (Dflag) { 5290 (void) printf("\n--- Entry %d ---\n", ++jtemp); 5291 (void) printf("Group = %d, mib_id = %d, " 5292 "length = %d, valp = 0x%p\n", 5293 item->group, item->mib_id, item->length, 5294 item->valp); 5295 } 5296 if (item->group != EXPER_DVMRP) 5297 continue; /* 'for' loop 1 */ 5298 5299 switch (item->mib_id) { 5300 5301 case EXPER_DVMRP_VIF: 5302 if (Dflag) 5303 (void) printf("%u records for ipVifTable:\n", 5304 item->length/sizeof (struct vifctl)); 5305 if (item->length/sizeof (struct vifctl) == 0) { 5306 (void) puts("\nVirtual Interface Table is " 5307 "empty"); 5308 break; 5309 } 5310 5311 (void) puts("\nVirtual Interface Table\n" 5312 " Vif Threshold Rate_Limit Local-Address" 5313 " Remote-Address Pkt_in Pkt_out"); 5314 5315 /* 'for' loop 2: */ 5316 for (vip = (struct vifctl *)item->valp; 5317 (char *)vip < (char *)item->valp + item->length; 5318 /* LINTED: (note 1) */ 5319 vip = (struct vifctl *)((char *)vip + 5320 vifctlSize)) { 5321 if (vip->vifc_lcl_addr.s_addr == 0) 5322 continue; /* 'for' loop 2 */ 5323 /* numvifs = vip->vifc_vifi; */ 5324 5325 numvifs++; 5326 (void) printf(" %2u %3u " 5327 "%4u %-15.15s", 5328 vip->vifc_vifi, 5329 vip->vifc_threshold, 5330 vip->vifc_rate_limit, 5331 pr_addr(vip->vifc_lcl_addr.s_addr, 5332 abuf, sizeof (abuf))); 5333 (void) printf(" %-15.15s %8u %8u\n", 5334 (vip->vifc_flags & VIFF_TUNNEL) ? 5335 pr_addr(vip->vifc_rmt_addr.s_addr, 5336 abuf, sizeof (abuf)) : "", 5337 vip->vifc_pkt_in, 5338 vip->vifc_pkt_out); 5339 } /* 'for' loop 2 ends */ 5340 5341 (void) printf("Numvifs: %d\n", numvifs); 5342 break; 5343 5344 case EXPER_DVMRP_MRT: 5345 if (Dflag) 5346 (void) printf("%u records for ipMfcTable:\n", 5347 item->length/sizeof (struct vifctl)); 5348 if (item->length/sizeof (struct vifctl) == 0) { 5349 (void) puts("\nMulticast Forwarding Cache is " 5350 "empty"); 5351 break; 5352 } 5353 5354 (void) puts("\nMulticast Forwarding Cache\n" 5355 " Origin-Subnet Mcastgroup " 5356 "# Pkts In-Vif Out-vifs/Forw-ttl"); 5357 5358 for (mfccp = (struct mfcctl *)item->valp; 5359 (char *)mfccp < (char *)item->valp + item->length; 5360 /* LINTED: (note 1) */ 5361 mfccp = (struct mfcctl *)((char *)mfccp + 5362 mfcctlSize)) { 5363 5364 nmfc++; 5365 (void) printf(" %-30.15s", 5366 pr_addr(mfccp->mfcc_origin.s_addr, 5367 abuf, sizeof (abuf))); 5368 (void) printf("%-15.15s %6s %3u ", 5369 pr_net(mfccp->mfcc_mcastgrp.s_addr, 5370 mfccp->mfcc_mcastgrp.s_addr, 5371 abuf, sizeof (abuf)), 5372 pktscale((int)mfccp->mfcc_pkt_cnt), 5373 mfccp->mfcc_parent); 5374 5375 for (vifi = 0; vifi < MAXVIFS; ++vifi) { 5376 if (mfccp->mfcc_ttls[vifi]) { 5377 (void) printf(" %u (%u)", 5378 vifi, 5379 mfccp->mfcc_ttls[vifi]); 5380 } 5381 5382 } 5383 (void) putchar('\n'); 5384 } 5385 (void) printf("\nTotal no. of entries in cache: %d\n", 5386 nmfc); 5387 break; 5388 } 5389 } /* 'for' loop 1 ends */ 5390 (void) putchar('\n'); 5391 (void) fflush(stdout); 5392 } 5393 5394 /* 5395 * Get the stats for the cache named 'name'. If prefix != 0, then 5396 * interpret the name as a prefix, and sum up stats for all caches 5397 * named 'name*'. 5398 */ 5399 static void 5400 kmem_cache_stats(char *title, char *name, int prefix, int64_t *total_bytes) 5401 { 5402 int len; 5403 int alloc; 5404 int64_t total_alloc = 0; 5405 int alloc_fail, total_alloc_fail = 0; 5406 int buf_size = 0; 5407 int buf_avail; 5408 int buf_total; 5409 int buf_max, total_buf_max = 0; 5410 int buf_inuse, total_buf_inuse = 0; 5411 kstat_t *ksp; 5412 char buf[256]; 5413 5414 len = prefix ? strlen(name) : 256; 5415 5416 /* 'for' loop 1: */ 5417 for (ksp = kc->kc_chain; ksp != NULL; ksp = ksp->ks_next) { 5418 5419 if (strcmp(ksp->ks_class, "kmem_cache") != 0) 5420 continue; /* 'for' loop 1 */ 5421 5422 /* 5423 * Hack alert: because of the way streams messages are 5424 * allocated, every constructed free dblk has an associated 5425 * mblk. From the allocator's viewpoint those mblks are 5426 * allocated (because they haven't been freed), but from 5427 * our viewpoint they're actually free (because they're 5428 * not currently in use). To account for this caching 5429 * effect we subtract the total constructed free dblks 5430 * from the total allocated mblks to derive mblks in use. 5431 */ 5432 if (strcmp(name, "streams_mblk") == 0 && 5433 strncmp(ksp->ks_name, "streams_dblk", 12) == 0) { 5434 (void) safe_kstat_read(kc, ksp, NULL); 5435 total_buf_inuse -= 5436 kstat_named_value(ksp, "buf_constructed"); 5437 continue; /* 'for' loop 1 */ 5438 } 5439 5440 if (strncmp(ksp->ks_name, name, len) != 0) 5441 continue; /* 'for' loop 1 */ 5442 5443 (void) safe_kstat_read(kc, ksp, NULL); 5444 5445 alloc = kstat_named_value(ksp, "alloc"); 5446 alloc_fail = kstat_named_value(ksp, "alloc_fail"); 5447 buf_size = kstat_named_value(ksp, "buf_size"); 5448 buf_avail = kstat_named_value(ksp, "buf_avail"); 5449 buf_total = kstat_named_value(ksp, "buf_total"); 5450 buf_max = kstat_named_value(ksp, "buf_max"); 5451 buf_inuse = buf_total - buf_avail; 5452 5453 if (Vflag && prefix) { 5454 (void) snprintf(buf, sizeof (buf), "%s%s", title, 5455 ksp->ks_name + len); 5456 (void) printf(" %-18s %6u %9u %11u %11u\n", 5457 buf, buf_inuse, buf_max, alloc, alloc_fail); 5458 } 5459 5460 total_alloc += alloc; 5461 total_alloc_fail += alloc_fail; 5462 total_buf_max += buf_max; 5463 total_buf_inuse += buf_inuse; 5464 *total_bytes += (int64_t)buf_inuse * buf_size; 5465 } /* 'for' loop 1 ends */ 5466 5467 if (buf_size == 0) { 5468 (void) printf("%-22s [couldn't find statistics for %s]\n", 5469 title, name); 5470 return; 5471 } 5472 5473 if (Vflag && prefix) 5474 (void) snprintf(buf, sizeof (buf), "%s_total", title); 5475 else 5476 (void) snprintf(buf, sizeof (buf), "%s", title); 5477 5478 (void) printf("%-22s %6d %9d %11lld %11d\n", buf, 5479 total_buf_inuse, total_buf_max, total_alloc, total_alloc_fail); 5480 } 5481 5482 static void 5483 m_report(void) 5484 { 5485 int64_t total_bytes = 0; 5486 5487 (void) puts("streams allocation:"); 5488 (void) printf("%63s\n", "cumulative allocation"); 5489 (void) printf("%63s\n", 5490 "current maximum total failures"); 5491 5492 kmem_cache_stats("streams", 5493 "stream_head_cache", 0, &total_bytes); 5494 kmem_cache_stats("queues", "queue_cache", 0, &total_bytes); 5495 kmem_cache_stats("mblk", "streams_mblk", 0, &total_bytes); 5496 kmem_cache_stats("dblk", "streams_dblk", 1, &total_bytes); 5497 kmem_cache_stats("linkblk", "linkinfo_cache", 0, &total_bytes); 5498 kmem_cache_stats("syncq", "syncq_cache", 0, &total_bytes); 5499 kmem_cache_stats("qband", "qband_cache", 0, &total_bytes); 5500 5501 (void) printf("\n%lld Kbytes allocated for streams data\n", 5502 total_bytes / 1024); 5503 5504 (void) putchar('\n'); 5505 (void) fflush(stdout); 5506 } 5507 5508 /* --------------------------------- */ 5509 5510 /* 5511 * Print an IPv4 address. Remove the matching part of the domain name 5512 * from the returned name. 5513 */ 5514 static char * 5515 pr_addr(uint_t addr, char *dst, uint_t dstlen) 5516 { 5517 char *cp; 5518 struct hostent *hp = NULL; 5519 static char domain[MAXHOSTNAMELEN + 1]; 5520 static boolean_t first = B_TRUE; 5521 int error_num; 5522 5523 if (first) { 5524 first = B_FALSE; 5525 if (sysinfo(SI_HOSTNAME, domain, MAXHOSTNAMELEN) != -1 && 5526 (cp = strchr(domain, '.'))) { 5527 (void) strncpy(domain, cp + 1, sizeof (domain)); 5528 } else 5529 domain[0] = 0; 5530 } 5531 cp = NULL; 5532 if (!Nflag) { 5533 hp = getipnodebyaddr((char *)&addr, sizeof (uint_t), AF_INET, 5534 &error_num); 5535 if (hp) { 5536 if ((cp = strchr(hp->h_name, '.')) != NULL && 5537 strcasecmp(cp + 1, domain) == 0) 5538 *cp = 0; 5539 cp = hp->h_name; 5540 } 5541 } 5542 if (cp != NULL) { 5543 (void) strncpy(dst, cp, dstlen); 5544 dst[dstlen - 1] = 0; 5545 } else { 5546 (void) inet_ntop(AF_INET, (char *)&addr, dst, dstlen); 5547 } 5548 if (hp != NULL) 5549 freehostent(hp); 5550 return (dst); 5551 } 5552 5553 /* 5554 * Print a non-zero IPv4 address. Print " --" if the address is zero. 5555 */ 5556 static char * 5557 pr_addrnz(ipaddr_t addr, char *dst, uint_t dstlen) 5558 { 5559 if (addr == INADDR_ANY) { 5560 (void) strlcpy(dst, " --", dstlen); 5561 return (dst); 5562 } 5563 return (pr_addr(addr, dst, dstlen)); 5564 } 5565 5566 /* 5567 * Print an IPv6 address. Remove the matching part of the domain name 5568 * from the returned name. 5569 */ 5570 static char * 5571 pr_addr6(const struct in6_addr *addr, char *dst, uint_t dstlen) 5572 { 5573 char *cp; 5574 struct hostent *hp = NULL; 5575 static char domain[MAXHOSTNAMELEN + 1]; 5576 static boolean_t first = B_TRUE; 5577 int error_num; 5578 5579 if (first) { 5580 first = B_FALSE; 5581 if (sysinfo(SI_HOSTNAME, domain, MAXHOSTNAMELEN) != -1 && 5582 (cp = strchr(domain, '.'))) { 5583 (void) strncpy(domain, cp + 1, sizeof (domain)); 5584 } else 5585 domain[0] = 0; 5586 } 5587 cp = NULL; 5588 if (!Nflag) { 5589 hp = getipnodebyaddr((char *)addr, 5590 sizeof (struct in6_addr), AF_INET6, &error_num); 5591 if (hp) { 5592 if ((cp = strchr(hp->h_name, '.')) != NULL && 5593 strcasecmp(cp + 1, domain) == 0) 5594 *cp = 0; 5595 cp = hp->h_name; 5596 } 5597 } 5598 if (cp != NULL) { 5599 (void) strncpy(dst, cp, dstlen); 5600 dst[dstlen - 1] = 0; 5601 } else { 5602 (void) inet_ntop(AF_INET6, (void *)addr, dst, dstlen); 5603 } 5604 if (hp != NULL) 5605 freehostent(hp); 5606 return (dst); 5607 } 5608 5609 /* For IPv4 masks */ 5610 static char * 5611 pr_mask(uint_t addr, char *dst, uint_t dstlen) 5612 { 5613 uint8_t *ip_addr = (uint8_t *)&addr; 5614 5615 (void) snprintf(dst, dstlen, "%d.%d.%d.%d", 5616 ip_addr[0], ip_addr[1], ip_addr[2], ip_addr[3]); 5617 return (dst); 5618 } 5619 5620 /* 5621 * For ipv6 masks format is : dest/mask 5622 * Does not print /128 to save space in printout. H flag carries this notion. 5623 */ 5624 static char * 5625 pr_prefix6(const struct in6_addr *addr, uint_t prefixlen, char *dst, 5626 uint_t dstlen) 5627 { 5628 char *cp; 5629 5630 if (IN6_IS_ADDR_UNSPECIFIED(addr) && prefixlen == 0) { 5631 (void) strncpy(dst, "default", dstlen); 5632 dst[dstlen - 1] = 0; 5633 return (dst); 5634 } 5635 5636 (void) pr_addr6(addr, dst, dstlen); 5637 if (prefixlen != IPV6_ABITS) { 5638 /* How much room is left? */ 5639 cp = strchr(dst, '\0'); 5640 if (dst + dstlen > cp) { 5641 dstlen -= (cp - dst); 5642 (void) snprintf(cp, dstlen, "/%d", prefixlen); 5643 } 5644 } 5645 return (dst); 5646 } 5647 5648 /* Print IPv4 address and port */ 5649 static char * 5650 pr_ap(uint_t addr, uint_t port, char *proto, 5651 char *dst, uint_t dstlen) 5652 { 5653 char *cp; 5654 5655 if (addr == INADDR_ANY) { 5656 (void) strncpy(dst, " *", dstlen); 5657 dst[dstlen - 1] = 0; 5658 } else { 5659 (void) pr_addr(addr, dst, dstlen); 5660 } 5661 /* How much room is left? */ 5662 cp = strchr(dst, '\0'); 5663 if (dst + dstlen > cp + 1) { 5664 *cp++ = '.'; 5665 dstlen -= (cp - dst); 5666 dstlen--; 5667 (void) portname(port, proto, cp, dstlen); 5668 } 5669 return (dst); 5670 } 5671 5672 /* Print IPv6 address and port */ 5673 static char * 5674 pr_ap6(const in6_addr_t *addr, uint_t port, char *proto, 5675 char *dst, uint_t dstlen) 5676 { 5677 char *cp; 5678 5679 if (IN6_IS_ADDR_UNSPECIFIED(addr)) { 5680 (void) strncpy(dst, " *", dstlen); 5681 dst[dstlen - 1] = 0; 5682 } else { 5683 (void) pr_addr6(addr, dst, dstlen); 5684 } 5685 /* How much room is left? */ 5686 cp = strchr(dst, '\0'); 5687 if (dst + dstlen + 1 > cp) { 5688 *cp++ = '.'; 5689 dstlen -= (cp - dst); 5690 dstlen--; 5691 (void) portname(port, proto, cp, dstlen); 5692 } 5693 return (dst); 5694 } 5695 5696 /* 5697 * Return the name of the network whose address is given. The address is 5698 * assumed to be that of a net or subnet, not a host. 5699 */ 5700 static char * 5701 pr_net(uint_t addr, uint_t mask, char *dst, uint_t dstlen) 5702 { 5703 char *cp = NULL; 5704 struct netent *np = NULL; 5705 struct hostent *hp = NULL; 5706 uint_t net; 5707 int subnetshift; 5708 int error_num; 5709 5710 if (addr == INADDR_ANY && mask == INADDR_ANY) { 5711 (void) strncpy(dst, "default", dstlen); 5712 dst[dstlen - 1] = 0; 5713 return (dst); 5714 } 5715 5716 if (!Nflag && addr) { 5717 if (mask == 0) { 5718 if (IN_CLASSA(addr)) { 5719 mask = (uint_t)IN_CLASSA_NET; 5720 subnetshift = 8; 5721 } else if (IN_CLASSB(addr)) { 5722 mask = (uint_t)IN_CLASSB_NET; 5723 subnetshift = 8; 5724 } else { 5725 mask = (uint_t)IN_CLASSC_NET; 5726 subnetshift = 4; 5727 } 5728 /* 5729 * If there are more bits than the standard mask 5730 * would suggest, subnets must be in use. Guess at 5731 * the subnet mask, assuming reasonable width subnet 5732 * fields. 5733 */ 5734 while (addr & ~mask) 5735 /* compiler doesn't sign extend! */ 5736 mask = (mask | ((int)mask >> subnetshift)); 5737 } 5738 net = addr & mask; 5739 while ((mask & 1) == 0) 5740 mask >>= 1, net >>= 1; 5741 np = getnetbyaddr(net, AF_INET); 5742 if (np && np->n_net == net) 5743 cp = np->n_name; 5744 else { 5745 /* 5746 * Look for subnets in hosts map. 5747 */ 5748 hp = getipnodebyaddr((char *)&addr, sizeof (uint_t), 5749 AF_INET, &error_num); 5750 if (hp) 5751 cp = hp->h_name; 5752 } 5753 } 5754 if (cp != NULL) { 5755 (void) strncpy(dst, cp, dstlen); 5756 dst[dstlen - 1] = 0; 5757 } else { 5758 (void) inet_ntop(AF_INET, (char *)&addr, dst, dstlen); 5759 } 5760 if (hp != NULL) 5761 freehostent(hp); 5762 return (dst); 5763 } 5764 5765 /* 5766 * Return the name of the network whose address is given. 5767 * The address is assumed to be a host address. 5768 */ 5769 static char * 5770 pr_netaddr(uint_t addr, uint_t mask, char *dst, uint_t dstlen) 5771 { 5772 char *cp = NULL; 5773 struct netent *np = NULL; 5774 struct hostent *hp = NULL; 5775 uint_t net; 5776 uint_t netshifted; 5777 int subnetshift; 5778 struct in_addr in; 5779 int error_num; 5780 uint_t nbo_addr = addr; /* network byte order */ 5781 5782 addr = ntohl(addr); 5783 mask = ntohl(mask); 5784 if (addr == INADDR_ANY && mask == INADDR_ANY) { 5785 (void) strncpy(dst, "default", dstlen); 5786 dst[dstlen - 1] = 0; 5787 return (dst); 5788 } 5789 5790 /* Figure out network portion of address (with host portion = 0) */ 5791 if (addr) { 5792 /* Try figuring out mask if unknown (all 0s). */ 5793 if (mask == 0) { 5794 if (IN_CLASSA(addr)) { 5795 mask = (uint_t)IN_CLASSA_NET; 5796 subnetshift = 8; 5797 } else if (IN_CLASSB(addr)) { 5798 mask = (uint_t)IN_CLASSB_NET; 5799 subnetshift = 8; 5800 } else { 5801 mask = (uint_t)IN_CLASSC_NET; 5802 subnetshift = 4; 5803 } 5804 /* 5805 * If there are more bits than the standard mask 5806 * would suggest, subnets must be in use. Guess at 5807 * the subnet mask, assuming reasonable width subnet 5808 * fields. 5809 */ 5810 while (addr & ~mask) 5811 /* compiler doesn't sign extend! */ 5812 mask = (mask | ((int)mask >> subnetshift)); 5813 } 5814 net = netshifted = addr & mask; 5815 while ((mask & 1) == 0) 5816 mask >>= 1, netshifted >>= 1; 5817 } 5818 else 5819 net = netshifted = 0; 5820 5821 /* Try looking up name unless -n was specified. */ 5822 if (!Nflag) { 5823 np = getnetbyaddr(netshifted, AF_INET); 5824 if (np && np->n_net == netshifted) 5825 cp = np->n_name; 5826 else { 5827 /* 5828 * Look for subnets in hosts map. 5829 */ 5830 hp = getipnodebyaddr((char *)&nbo_addr, sizeof (uint_t), 5831 AF_INET, &error_num); 5832 if (hp) 5833 cp = hp->h_name; 5834 } 5835 5836 if (cp != NULL) { 5837 (void) strncpy(dst, cp, dstlen); 5838 dst[dstlen - 1] = 0; 5839 if (hp != NULL) 5840 freehostent(hp); 5841 return (dst); 5842 } 5843 /* 5844 * No name found for net: fallthru and return in decimal 5845 * dot notation. 5846 */ 5847 } 5848 5849 in.s_addr = htonl(net); 5850 (void) inet_ntop(AF_INET, (char *)&in, dst, dstlen); 5851 if (hp != NULL) 5852 freehostent(hp); 5853 return (dst); 5854 } 5855 5856 /* 5857 * Return the filter mode as a string: 5858 * 1 => "INCLUDE" 5859 * 2 => "EXCLUDE" 5860 * otherwise "<unknown>" 5861 */ 5862 static char * 5863 fmodestr(uint_t fmode) 5864 { 5865 switch (fmode) { 5866 case 1: 5867 return ("INCLUDE"); 5868 case 2: 5869 return ("EXCLUDE"); 5870 default: 5871 return ("<unknown>"); 5872 } 5873 } 5874 5875 #define MAX_STRING_SIZE 256 5876 5877 static const char * 5878 pr_secattr(const sec_attr_list_t *attrs) 5879 { 5880 int i; 5881 char buf[MAX_STRING_SIZE + 1], *cp; 5882 static char *sbuf; 5883 static size_t sbuf_len; 5884 struct rtsa_s rtsa; 5885 const sec_attr_list_t *aptr; 5886 5887 if (!RSECflag || attrs == NULL) 5888 return (""); 5889 5890 for (aptr = attrs, i = 1; aptr != NULL; aptr = aptr->sal_next) 5891 i += MAX_STRING_SIZE; 5892 if (i > sbuf_len) { 5893 cp = realloc(sbuf, i); 5894 if (cp == NULL) { 5895 perror("realloc security attribute buffer"); 5896 return (""); 5897 } 5898 sbuf_len = i; 5899 sbuf = cp; 5900 } 5901 5902 cp = sbuf; 5903 while (attrs != NULL) { 5904 const mib2_ipAttributeEntry_t *iae = attrs->sal_attr; 5905 5906 /* note: effectively hard-coded in rtsa_keyword */ 5907 rtsa.rtsa_mask = RTSA_CIPSO | RTSA_SLRANGE | RTSA_DOI; 5908 rtsa.rtsa_slrange = iae->iae_slrange; 5909 rtsa.rtsa_doi = iae->iae_doi; 5910 5911 (void) snprintf(cp, MAX_STRING_SIZE, 5912 "<%s>%s ", rtsa_to_str(&rtsa, buf, sizeof (buf)), 5913 attrs->sal_next == NULL ? "" : ","); 5914 cp += strlen(cp); 5915 attrs = attrs->sal_next; 5916 } 5917 *cp = '\0'; 5918 5919 return (sbuf); 5920 } 5921 5922 /* 5923 * Pretty print a port number. If the Nflag was 5924 * specified, use numbers instead of names. 5925 */ 5926 static char * 5927 portname(uint_t port, char *proto, char *dst, uint_t dstlen) 5928 { 5929 struct servent *sp = NULL; 5930 5931 if (!Nflag && port) 5932 sp = getservbyport(htons(port), proto); 5933 if (sp || port == 0) 5934 (void) snprintf(dst, dstlen, "%.*s", MAXHOSTNAMELEN, 5935 sp ? sp->s_name : "*"); 5936 else 5937 (void) snprintf(dst, dstlen, "%d", port); 5938 dst[dstlen - 1] = 0; 5939 return (dst); 5940 } 5941 5942 /*PRINTFLIKE2*/ 5943 void 5944 fail(int do_perror, char *message, ...) 5945 { 5946 va_list args; 5947 5948 va_start(args, message); 5949 (void) fputs("netstat: ", stderr); 5950 (void) vfprintf(stderr, message, args); 5951 va_end(args); 5952 if (do_perror) 5953 (void) fprintf(stderr, ": %s", strerror(errno)); 5954 (void) fputc('\n', stderr); 5955 exit(2); 5956 } 5957 5958 /* 5959 * Return value of named statistic for given kstat_named kstat; 5960 * return 0LL if named statistic is not in list (use "ll" as a 5961 * type qualifier when printing 64-bit int's with printf() ) 5962 */ 5963 static uint64_t 5964 kstat_named_value(kstat_t *ksp, char *name) 5965 { 5966 kstat_named_t *knp; 5967 uint64_t value; 5968 5969 if (ksp == NULL) 5970 return (0LL); 5971 5972 knp = kstat_data_lookup(ksp, name); 5973 if (knp == NULL) 5974 return (0LL); 5975 5976 switch (knp->data_type) { 5977 case KSTAT_DATA_INT32: 5978 case KSTAT_DATA_UINT32: 5979 value = (uint64_t)(knp->value.ui32); 5980 break; 5981 case KSTAT_DATA_INT64: 5982 case KSTAT_DATA_UINT64: 5983 value = knp->value.ui64; 5984 break; 5985 default: 5986 value = 0LL; 5987 break; 5988 } 5989 5990 return (value); 5991 } 5992 5993 kid_t 5994 safe_kstat_read(kstat_ctl_t *kc, kstat_t *ksp, void *data) 5995 { 5996 kid_t kstat_chain_id = kstat_read(kc, ksp, data); 5997 5998 if (kstat_chain_id == -1) 5999 fail(1, "kstat_read(%p, '%s') failed", (void *)kc, 6000 ksp->ks_name); 6001 return (kstat_chain_id); 6002 } 6003 6004 /* 6005 * Parse a list of IRE flag characters into a bit field. 6006 */ 6007 static uint_t 6008 flag_bits(const char *arg) 6009 { 6010 const char *cp; 6011 uint_t val; 6012 6013 if (*arg == '\0') 6014 fatal(1, "missing flag list\n"); 6015 6016 val = 0; 6017 while (*arg != '\0') { 6018 if ((cp = strchr(flag_list, *arg)) == NULL) 6019 fatal(1, "%c: illegal flag\n", *arg); 6020 val |= 1 << (cp - flag_list); 6021 arg++; 6022 } 6023 return (val); 6024 } 6025 6026 /* 6027 * Handle -f argument. Validate input format, sort by keyword, and 6028 * save off digested results. 6029 */ 6030 static void 6031 process_filter(char *arg) 6032 { 6033 int idx; 6034 int klen = 0; 6035 char *cp, *cp2; 6036 int val; 6037 filter_t *newf; 6038 struct hostent *hp; 6039 int error_num; 6040 uint8_t *ucp; 6041 int maxv; 6042 6043 /* Look up the keyword first */ 6044 if (strchr(arg, ':') == NULL) { 6045 idx = FK_AF; 6046 } else { 6047 for (idx = 0; idx < NFILTERKEYS; idx++) { 6048 klen = strlen(filter_keys[idx]); 6049 if (strncmp(filter_keys[idx], arg, klen) == 0 && 6050 arg[klen] == ':') 6051 break; 6052 } 6053 if (idx >= NFILTERKEYS) 6054 fatal(1, "%s: unknown filter keyword\n", arg); 6055 6056 /* Advance past keyword and separator. */ 6057 arg += klen + 1; 6058 } 6059 6060 if ((newf = malloc(sizeof (*newf))) == NULL) { 6061 perror("filter"); 6062 exit(1); 6063 } 6064 switch (idx) { 6065 case FK_AF: 6066 if (strcmp(arg, "inet") == 0) { 6067 newf->u.f_family = AF_INET; 6068 } else if (strcmp(arg, "inet6") == 0) { 6069 newf->u.f_family = AF_INET6; 6070 } else if (strcmp(arg, "unix") == 0) { 6071 newf->u.f_family = AF_UNIX; 6072 } else { 6073 newf->u.f_family = strtol(arg, &cp, 0); 6074 if (arg == cp || *cp != '\0') 6075 fatal(1, "%s: unknown address family.\n", arg); 6076 } 6077 break; 6078 6079 case FK_OUTIF: 6080 if (strcmp(arg, "none") == 0) { 6081 newf->u.f_ifname = NULL; 6082 break; 6083 } 6084 if (strcmp(arg, "any") == 0) { 6085 newf->u.f_ifname = ""; 6086 break; 6087 } 6088 val = strtol(arg, &cp, 0); 6089 if (val <= 0 || arg == cp || cp[0] != '\0') { 6090 if ((val = if_nametoindex(arg)) == 0) { 6091 perror(arg); 6092 exit(1); 6093 } 6094 } 6095 newf->u.f_ifname = arg; 6096 break; 6097 6098 case FK_DST: 6099 V4MASK_TO_V6(IP_HOST_MASK, newf->u.a.f_mask); 6100 if (strcmp(arg, "any") == 0) { 6101 /* Special semantics; any address *but* zero */ 6102 newf->u.a.f_address = NULL; 6103 (void) memset(&newf->u.a.f_mask, 0, 6104 sizeof (newf->u.a.f_mask)); 6105 break; 6106 } 6107 if (strcmp(arg, "none") == 0) { 6108 newf->u.a.f_address = NULL; 6109 break; 6110 } 6111 if ((cp = strrchr(arg, '/')) != NULL) 6112 *cp++ = '\0'; 6113 hp = getipnodebyname(arg, AF_INET6, AI_V4MAPPED|AI_ALL, 6114 &error_num); 6115 if (hp == NULL) 6116 fatal(1, "%s: invalid or unknown host address\n", arg); 6117 newf->u.a.f_address = hp; 6118 if (cp == NULL) { 6119 V4MASK_TO_V6(IP_HOST_MASK, newf->u.a.f_mask); 6120 } else { 6121 val = strtol(cp, &cp2, 0); 6122 if (cp != cp2 && cp2[0] == '\0') { 6123 /* 6124 * If decode as "/n" works, then translate 6125 * into a mask. 6126 */ 6127 if (hp->h_addr_list[0] != NULL && 6128 /* LINTED: (note 1) */ 6129 IN6_IS_ADDR_V4MAPPED((in6_addr_t 6130 *)hp->h_addr_list[0])) { 6131 maxv = IP_ABITS; 6132 } else { 6133 maxv = IPV6_ABITS; 6134 } 6135 if (val < 0 || val >= maxv) 6136 fatal(1, "%d: not in range 0 to %d\n", 6137 val, maxv - 1); 6138 if (maxv == IP_ABITS) 6139 val += IPV6_ABITS - IP_ABITS; 6140 ucp = newf->u.a.f_mask.s6_addr; 6141 while (val >= 8) 6142 *ucp++ = 0xff, val -= 8; 6143 *ucp++ = (0xff << (8 - val)) & 0xff; 6144 while (ucp < newf->u.a.f_mask.s6_addr + 6145 sizeof (newf->u.a.f_mask.s6_addr)) 6146 *ucp++ = 0; 6147 /* Otherwise, try as numeric address */ 6148 } else if (inet_pton(AF_INET6, 6149 cp, &newf->u.a.f_mask) <= 0) { 6150 fatal(1, "%s: illegal mask format\n", cp); 6151 } 6152 } 6153 break; 6154 6155 case FK_FLAGS: 6156 if (*arg == '+') { 6157 newf->u.f.f_flagset = flag_bits(arg + 1); 6158 newf->u.f.f_flagclear = 0; 6159 } else if (*arg == '-') { 6160 newf->u.f.f_flagset = 0; 6161 newf->u.f.f_flagclear = flag_bits(arg + 1); 6162 } else { 6163 newf->u.f.f_flagset = flag_bits(arg); 6164 newf->u.f.f_flagclear = ~newf->u.f.f_flagset; 6165 } 6166 break; 6167 6168 default: 6169 assert(0); 6170 } 6171 newf->f_next = filters[idx]; 6172 filters[idx] = newf; 6173 } 6174 6175 /* Determine if user wants this address family printed. */ 6176 static boolean_t 6177 family_selected(int family) 6178 { 6179 const filter_t *fp; 6180 6181 if (v4compat && family == AF_INET6) 6182 return (B_FALSE); 6183 if ((fp = filters[FK_AF]) == NULL) 6184 return (B_TRUE); 6185 while (fp != NULL) { 6186 if (fp->u.f_family == family) 6187 return (B_TRUE); 6188 fp = fp->f_next; 6189 } 6190 return (B_FALSE); 6191 } 6192 6193 /* 6194 * print the usage line 6195 */ 6196 static void 6197 usage(char *cmdname) 6198 { 6199 (void) fprintf(stderr, "usage: %s [-anv] [-f address_family]\n", 6200 cmdname); 6201 (void) fprintf(stderr, " %s [-n] [-f address_family] " 6202 "[-P protocol] [-g | -p | -s [interval [count]]]\n", cmdname); 6203 (void) fprintf(stderr, " %s -m [-v] " 6204 "[interval [count]]\n", cmdname); 6205 (void) fprintf(stderr, " %s -i [-I interface] [-an] " 6206 "[-f address_family] [interval [count]]\n", cmdname); 6207 (void) fprintf(stderr, " %s -r [-anv] " 6208 "[-f address_family|filter]\n", cmdname); 6209 (void) fprintf(stderr, " %s -M [-ns] [-f address_family]\n", 6210 cmdname); 6211 (void) fprintf(stderr, " %s -D [-I interface] " 6212 "[-f address_family]\n", cmdname); 6213 exit(EXIT_FAILURE); 6214 } 6215 6216 /* 6217 * fatal: print error message to stderr and 6218 * call exit(errcode) 6219 */ 6220 /*PRINTFLIKE2*/ 6221 static void 6222 fatal(int errcode, char *format, ...) 6223 { 6224 va_list argp; 6225 6226 if (format == NULL) 6227 return; 6228 6229 va_start(argp, format); 6230 (void) vfprintf(stderr, format, argp); 6231 va_end(argp); 6232 6233 exit(errcode); 6234 } 6235