17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*45916cd2Sjpk  * Common Development and Distribution License (the "License").
6*45916cd2Sjpk  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*45916cd2Sjpk  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * Copyright (c) 1990  Mentat Inc.
287c478bd9Sstevel@tonic-gate  * netstat.c 2.2, last change 9/9/91
297c478bd9Sstevel@tonic-gate  * MROUTING Revision 3.5
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate /*
357c478bd9Sstevel@tonic-gate  * simple netstat based on snmp/mib-2 interface to the TCP/IP stack
367c478bd9Sstevel@tonic-gate  *
377c478bd9Sstevel@tonic-gate  * NOTES:
387c478bd9Sstevel@tonic-gate  * 1. A comment "LINTED: (note 1)" appears before certain lines where
397c478bd9Sstevel@tonic-gate  *    lint would have complained, "pointer cast may result in improper
407c478bd9Sstevel@tonic-gate  *    alignment". These are lines where lint had suspected potential
417c478bd9Sstevel@tonic-gate  *    improper alignment of a data structure; in each such situation
427c478bd9Sstevel@tonic-gate  *    we have relied on the kernel guaranteeing proper alignment.
437c478bd9Sstevel@tonic-gate  * 2. Some 'for' loops have been commented as "'for' loop 1", etc
447c478bd9Sstevel@tonic-gate  *    because they have 'continue' or 'break' statements in their
457c478bd9Sstevel@tonic-gate  *    bodies. 'continue' statements have been used inside some loops
467c478bd9Sstevel@tonic-gate  *    where avoiding them would have led to deep levels of indentation.
477c478bd9Sstevel@tonic-gate  *
487c478bd9Sstevel@tonic-gate  * TODO:
497c478bd9Sstevel@tonic-gate  *	Add ability to request subsets from kernel (with level = MIB2_IP;
507c478bd9Sstevel@tonic-gate  *	name = 0 meaning everything for compatibility)
517c478bd9Sstevel@tonic-gate  */
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #include <stdio.h>
547c478bd9Sstevel@tonic-gate #include <stdlib.h>
557c478bd9Sstevel@tonic-gate #include <stdarg.h>
567c478bd9Sstevel@tonic-gate #include <unistd.h>
577c478bd9Sstevel@tonic-gate #include <strings.h>
587c478bd9Sstevel@tonic-gate #include <string.h>
597c478bd9Sstevel@tonic-gate #include <errno.h>
607c478bd9Sstevel@tonic-gate #include <ctype.h>
617c478bd9Sstevel@tonic-gate #include <kstat.h>
627c478bd9Sstevel@tonic-gate #include <assert.h>
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate #include <sys/types.h>
657c478bd9Sstevel@tonic-gate #include <sys/stream.h>
667c478bd9Sstevel@tonic-gate #include <stropts.h>
677c478bd9Sstevel@tonic-gate #include <sys/strstat.h>
687c478bd9Sstevel@tonic-gate #include <sys/tihdr.h>
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate #include <sys/socket.h>
717c478bd9Sstevel@tonic-gate #include <sys/sockio.h>
727c478bd9Sstevel@tonic-gate #include <netinet/in.h>
737c478bd9Sstevel@tonic-gate #include <net/if.h>
747c478bd9Sstevel@tonic-gate #include <net/route.h>
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate #include <inet/common.h>
777c478bd9Sstevel@tonic-gate #include <inet/mib2.h>
787c478bd9Sstevel@tonic-gate #include <inet/ip.h>
797c478bd9Sstevel@tonic-gate #include <inet/arp.h>
807c478bd9Sstevel@tonic-gate #include <inet/tcp.h>
817c478bd9Sstevel@tonic-gate #include <netinet/igmp_var.h>
827c478bd9Sstevel@tonic-gate #include <netinet/ip_mroute.h>
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
857c478bd9Sstevel@tonic-gate #include <netdb.h>
867c478bd9Sstevel@tonic-gate #include <fcntl.h>
877c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
887c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate #include <netinet/dhcp.h>
917c478bd9Sstevel@tonic-gate #include <dhcpagent_ipc.h>
927c478bd9Sstevel@tonic-gate #include <dhcpagent_util.h>
937c478bd9Sstevel@tonic-gate #include <compat.h>
947c478bd9Sstevel@tonic-gate 
95*45916cd2Sjpk #include <libtsnet.h>
96*45916cd2Sjpk #include <tsol/label.h>
97*45916cd2Sjpk 
987c478bd9Sstevel@tonic-gate extern void	unixpr(kstat_ctl_t *kc);
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate #define	STR_EXPAND	4
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate #define	V4MASK_TO_V6(v4, v6)	((v6)._S6_un._S6_u32[0] = 0xfffffffful, \
1037c478bd9Sstevel@tonic-gate 				(v6)._S6_un._S6_u32[1] = 0xfffffffful, \
1047c478bd9Sstevel@tonic-gate 				(v6)._S6_un._S6_u32[2] = 0xfffffffful, \
1057c478bd9Sstevel@tonic-gate 				(v6)._S6_un._S6_u32[3] = (v4))
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate #define	IN6_IS_V4MASK(v6)	((v6)._S6_un._S6_u32[0] == 0xfffffffful && \
1087c478bd9Sstevel@tonic-gate 				(v6)._S6_un._S6_u32[1] == 0xfffffffful && \
1097c478bd9Sstevel@tonic-gate 				(v6)._S6_un._S6_u32[2] == 0xfffffffful)
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate typedef struct mib_item_s {
1127c478bd9Sstevel@tonic-gate 	struct mib_item_s	*next_item;
1137c478bd9Sstevel@tonic-gate 	int			group;
1147c478bd9Sstevel@tonic-gate 	int			mib_id;
1157c478bd9Sstevel@tonic-gate 	int			length;
1167c478bd9Sstevel@tonic-gate 	void			*valp;
1177c478bd9Sstevel@tonic-gate } mib_item_t;
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate struct	ifstat {
1207c478bd9Sstevel@tonic-gate 	uint64_t	ipackets;
1217c478bd9Sstevel@tonic-gate 	uint64_t	ierrors;
1227c478bd9Sstevel@tonic-gate 	uint64_t	opackets;
1237c478bd9Sstevel@tonic-gate 	uint64_t	oerrors;
1247c478bd9Sstevel@tonic-gate 	uint64_t	collisions;
1257c478bd9Sstevel@tonic-gate };
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate struct iflist {
1287c478bd9Sstevel@tonic-gate 	struct iflist	*next_if;
1297c478bd9Sstevel@tonic-gate 	char		ifname[LIFNAMSIZ];
1307c478bd9Sstevel@tonic-gate 	struct ifstat	tot;
1317c478bd9Sstevel@tonic-gate };
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate static	mib_item_t	*mibget(int sd);
1347c478bd9Sstevel@tonic-gate static	void		mibfree(mib_item_t *firstitem);
1357c478bd9Sstevel@tonic-gate static	int		mibopen(void);
1367c478bd9Sstevel@tonic-gate static void		mib_get_constants(mib_item_t *item);
1377c478bd9Sstevel@tonic-gate static mib_item_t	*mib_item_dup(mib_item_t *item);
1387c478bd9Sstevel@tonic-gate static mib_item_t	*mib_item_diff(mib_item_t *item1,
1397c478bd9Sstevel@tonic-gate     mib_item_t *item2);
1407c478bd9Sstevel@tonic-gate static void		mib_item_destroy(mib_item_t **item);
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate static boolean_t	octetstrmatch(const Octet_t *a, const Octet_t *b);
143*45916cd2Sjpk static char		*octetstr(const Octet_t *op, int code,
1447c478bd9Sstevel@tonic-gate 			    char *dst, uint_t dstlen);
1457c478bd9Sstevel@tonic-gate static char		*pr_addr(uint_t addr,
1467c478bd9Sstevel@tonic-gate 			    char *dst, uint_t dstlen);
1477c478bd9Sstevel@tonic-gate static char		*pr_addrnz(ipaddr_t addr, char *dst, uint_t dstlen);
1487c478bd9Sstevel@tonic-gate static char		*pr_addr6(const in6_addr_t *addr,
1497c478bd9Sstevel@tonic-gate 			    char *dst, uint_t dstlen);
1507c478bd9Sstevel@tonic-gate static char		*pr_mask(uint_t addr,
1517c478bd9Sstevel@tonic-gate 			    char *dst, uint_t dstlen);
152*45916cd2Sjpk static char		*pr_prefix6(const struct in6_addr *addr,
153*45916cd2Sjpk 			    uint_t prefixlen, char *dst, uint_t dstlen);
1547c478bd9Sstevel@tonic-gate static char		*pr_ap(uint_t addr, uint_t port,
1557c478bd9Sstevel@tonic-gate 			    char *proto, char *dst, uint_t dstlen);
1567c478bd9Sstevel@tonic-gate static char		*pr_ap6(const in6_addr_t *addr, uint_t port,
1577c478bd9Sstevel@tonic-gate 			    char *proto, char *dst, uint_t dstlen);
1587c478bd9Sstevel@tonic-gate static char		*pr_net(uint_t addr, uint_t mask,
1597c478bd9Sstevel@tonic-gate 			    char *dst, uint_t dstlen);
1607c478bd9Sstevel@tonic-gate static char		*pr_netaddr(uint_t addr, uint_t mask,
1617c478bd9Sstevel@tonic-gate 			    char *dst, uint_t dstlen);
1627c478bd9Sstevel@tonic-gate static char		*pr_netclassless(ipaddr_t addr, ipaddr_t mask,
1637c478bd9Sstevel@tonic-gate 			    char *dst, size_t dstlen);
1647c478bd9Sstevel@tonic-gate static char		*fmodestr(uint_t fmode);
1657c478bd9Sstevel@tonic-gate static char		*portname(uint_t port, char *proto,
1667c478bd9Sstevel@tonic-gate 			    char *dst, uint_t dstlen);
1677c478bd9Sstevel@tonic-gate 
168*45916cd2Sjpk static const char	*mitcp_state(int code,
169*45916cd2Sjpk 			    const mib2_transportMLPEntry_t *attr);
170*45916cd2Sjpk static const char	*miudp_state(int code,
171*45916cd2Sjpk 			    const mib2_transportMLPEntry_t *attr);
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate static void		stat_report(mib_item_t *item);
1747c478bd9Sstevel@tonic-gate static void		mrt_stat_report(mib_item_t *item);
1757c478bd9Sstevel@tonic-gate static void		arp_report(mib_item_t *item);
1767c478bd9Sstevel@tonic-gate static void		ndp_report(mib_item_t *item);
1777c478bd9Sstevel@tonic-gate static void		mrt_report(mib_item_t *item);
1787c478bd9Sstevel@tonic-gate static void		if_stat_total(struct ifstat *oldstats,
1797c478bd9Sstevel@tonic-gate 			    struct ifstat *newstats, struct ifstat *sumstats);
1807c478bd9Sstevel@tonic-gate static void		if_report(mib_item_t *item, char *ifname,
1817c478bd9Sstevel@tonic-gate 			    int Iflag_only, boolean_t once_only);
1827c478bd9Sstevel@tonic-gate static void		if_report_ip4(mib2_ipAddrEntry_t *ap,
1837c478bd9Sstevel@tonic-gate 			    char ifname[], char logintname[],
1847c478bd9Sstevel@tonic-gate 			    struct ifstat *statptr, boolean_t ksp_not_null);
1857c478bd9Sstevel@tonic-gate static void		if_report_ip6(mib2_ipv6AddrEntry_t *ap6,
1867c478bd9Sstevel@tonic-gate 			    char ifname[], char logintname[],
1877c478bd9Sstevel@tonic-gate 			    struct ifstat *statptr, boolean_t ksp_not_null);
188*45916cd2Sjpk static void		ire_report(const mib_item_t *item);
189*45916cd2Sjpk static void		tcp_report(const mib_item_t *item);
190*45916cd2Sjpk static void		udp_report(const mib_item_t *item);
1917c478bd9Sstevel@tonic-gate static void		group_report(mib_item_t *item);
1927c478bd9Sstevel@tonic-gate static void		print_ip_stats(mib2_ip_t *ip);
1937c478bd9Sstevel@tonic-gate static void		print_icmp_stats(mib2_icmp_t *icmp);
1947c478bd9Sstevel@tonic-gate static void		print_ip6_stats(mib2_ipv6IfStatsEntry_t *ip6);
1957c478bd9Sstevel@tonic-gate static void		print_icmp6_stats(mib2_ipv6IfIcmpEntry_t *icmp6);
1967c478bd9Sstevel@tonic-gate static void		print_sctp_stats(mib2_sctp_t *tcp);
1977c478bd9Sstevel@tonic-gate static void		print_tcp_stats(mib2_tcp_t *tcp);
1987c478bd9Sstevel@tonic-gate static void		print_udp_stats(mib2_udp_t *udp);
1997c478bd9Sstevel@tonic-gate static void		print_rawip_stats(mib2_rawip_t *rawip);
2007c478bd9Sstevel@tonic-gate static void		print_igmp_stats(struct igmpstat *igps);
2017c478bd9Sstevel@tonic-gate static void		print_mrt_stats(struct mrtstat *mrts);
202*45916cd2Sjpk static void		sctp_report(const mib_item_t *item);
2037c478bd9Sstevel@tonic-gate static void		sum_ip6_stats(mib2_ipv6IfStatsEntry_t *ip6,
2047c478bd9Sstevel@tonic-gate 			    mib2_ipv6IfStatsEntry_t *sum6);
2057c478bd9Sstevel@tonic-gate static void		sum_icmp6_stats(mib2_ipv6IfIcmpEntry_t *icmp6,
2067c478bd9Sstevel@tonic-gate 			    mib2_ipv6IfIcmpEntry_t *sum6);
2077c478bd9Sstevel@tonic-gate static void		m_report(void);
2087c478bd9Sstevel@tonic-gate static void		dhcp_report(char *);
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	void		fail(int, char *, ...);
2117c478bd9Sstevel@tonic-gate static	uint64_t	kstat_named_value(kstat_t *, char *);
2127c478bd9Sstevel@tonic-gate static	kid_t		safe_kstat_read(kstat_ctl_t *, kstat_t *, void *);
2137c478bd9Sstevel@tonic-gate static int		isnum(char *);
2147c478bd9Sstevel@tonic-gate static char		*plural(int n);
2157c478bd9Sstevel@tonic-gate static char		*pluraly(int n);
2167c478bd9Sstevel@tonic-gate static char		*plurales(int n);
2177c478bd9Sstevel@tonic-gate static void		process_filter(char *arg);
2187c478bd9Sstevel@tonic-gate static boolean_t	family_selected(int family);
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate static void		usage(char *);
2217c478bd9Sstevel@tonic-gate static void 		fatal(int errcode, char *str1, ...);
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate #define	PLURAL(n) plural((int)n)
2247c478bd9Sstevel@tonic-gate #define	PLURALY(n) pluraly((int)n)
2257c478bd9Sstevel@tonic-gate #define	PLURALES(n) plurales((int)n)
2267c478bd9Sstevel@tonic-gate #define	IFLAGMOD(flg, val1, val2)	if (flg == val1) flg = val2
2277c478bd9Sstevel@tonic-gate #define	MDIFF(diff, elem2, elem1, member)	(diff)->member = \
2287c478bd9Sstevel@tonic-gate 	(elem2)->member - (elem1)->member
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate static	boolean_t	Aflag = B_FALSE;	/* All sockets/ifs/rtng-tbls */
2327c478bd9Sstevel@tonic-gate static	boolean_t	Dflag = B_FALSE;	/* Debug Info */
2337c478bd9Sstevel@tonic-gate static	boolean_t	Iflag = B_FALSE;	/* IP Traffic Interfaces */
2347c478bd9Sstevel@tonic-gate static	boolean_t	Mflag = B_FALSE;	/* STREAMS Memory Statistics */
2357c478bd9Sstevel@tonic-gate static	boolean_t	Nflag = B_FALSE;	/* Numeric Network Addresses */
2367c478bd9Sstevel@tonic-gate static	boolean_t	Rflag = B_FALSE;	/* Routing Tables */
237*45916cd2Sjpk static	boolean_t	RSECflag = B_FALSE;	/* Security attributes */
2387c478bd9Sstevel@tonic-gate static	boolean_t	Sflag = B_FALSE;	/* Per-protocol Statistics */
2397c478bd9Sstevel@tonic-gate static	boolean_t	Vflag = B_FALSE;	/* Verbose */
2407c478bd9Sstevel@tonic-gate static	boolean_t	Pflag = B_FALSE;	/* Net to Media Tables */
2417c478bd9Sstevel@tonic-gate static	boolean_t	Gflag = B_FALSE;	/* Multicast group membership */
2427c478bd9Sstevel@tonic-gate static	boolean_t	MMflag = B_FALSE;	/* Multicast routing table */
2437c478bd9Sstevel@tonic-gate static	boolean_t	DHCPflag = B_FALSE;	/* DHCP statistics */
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate static	int	v4compat = 0;	/* Compatible printing format for status */
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate static int	proto = IPPROTO_MAX;	/* all protocols */
2487c478bd9Sstevel@tonic-gate kstat_ctl_t	*kc = NULL;
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate /*
2517c478bd9Sstevel@tonic-gate  * Sizes of data structures extracted from the base mib.
2527c478bd9Sstevel@tonic-gate  * This allows the size of the tables entries to grow while preserving
2537c478bd9Sstevel@tonic-gate  * binary compatibility.
2547c478bd9Sstevel@tonic-gate  */
2557c478bd9Sstevel@tonic-gate static int ipAddrEntrySize;
2567c478bd9Sstevel@tonic-gate static int ipRouteEntrySize;
2577c478bd9Sstevel@tonic-gate static int ipNetToMediaEntrySize;
2587c478bd9Sstevel@tonic-gate static int ipMemberEntrySize;
2597c478bd9Sstevel@tonic-gate static int ipGroupSourceEntrySize;
260*45916cd2Sjpk static int ipRouteAttributeSize;
2617c478bd9Sstevel@tonic-gate static int vifctlSize;
2627c478bd9Sstevel@tonic-gate static int mfcctlSize;
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate static int ipv6IfStatsEntrySize;
2657c478bd9Sstevel@tonic-gate static int ipv6IfIcmpEntrySize;
2667c478bd9Sstevel@tonic-gate static int ipv6AddrEntrySize;
2677c478bd9Sstevel@tonic-gate static int ipv6RouteEntrySize;
2687c478bd9Sstevel@tonic-gate static int ipv6NetToMediaEntrySize;
2697c478bd9Sstevel@tonic-gate static int ipv6MemberEntrySize;
2707c478bd9Sstevel@tonic-gate static int ipv6GroupSourceEntrySize;
2717c478bd9Sstevel@tonic-gate 
272*45916cd2Sjpk static int transportMLPSize;
2737c478bd9Sstevel@tonic-gate static int tcpConnEntrySize;
2747c478bd9Sstevel@tonic-gate static int tcp6ConnEntrySize;
2757c478bd9Sstevel@tonic-gate static int udpEntrySize;
2767c478bd9Sstevel@tonic-gate static int udp6EntrySize;
2777c478bd9Sstevel@tonic-gate static int sctpEntrySize;
2787c478bd9Sstevel@tonic-gate static int sctpLocalEntrySize;
2797c478bd9Sstevel@tonic-gate static int sctpRemoteEntrySize;
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate #define	protocol_selected(p)	(proto == IPPROTO_MAX || proto == (p))
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate /* Machinery used for -f (filter) option */
2847c478bd9Sstevel@tonic-gate #define	FK_AF		0
2857c478bd9Sstevel@tonic-gate #define	FK_INIF		1
2867c478bd9Sstevel@tonic-gate #define	FK_OUTIF	2
2877c478bd9Sstevel@tonic-gate #define	FK_SRC		3
2887c478bd9Sstevel@tonic-gate #define	FK_DST		4
2897c478bd9Sstevel@tonic-gate #define	FK_FLAGS	5
2907c478bd9Sstevel@tonic-gate #define	NFILTERKEYS	6
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate static const char *filter_keys[NFILTERKEYS] = {
2937c478bd9Sstevel@tonic-gate 	"af", "inif", "outif", "src", "dst", "flags"
2947c478bd9Sstevel@tonic-gate };
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate /* Flags on routes */
2977c478bd9Sstevel@tonic-gate #define	FLF_A		0x00000001
2987c478bd9Sstevel@tonic-gate #define	FLF_B		0x00000002
2997c478bd9Sstevel@tonic-gate #define	FLF_D		0x00000004
3007c478bd9Sstevel@tonic-gate #define	FLF_G		0x00000008
3017c478bd9Sstevel@tonic-gate #define	FLF_H		0x00000010
3027c478bd9Sstevel@tonic-gate #define	FLF_L		0x00000020
3037c478bd9Sstevel@tonic-gate #define	FLF_U		0x00000040
3047c478bd9Sstevel@tonic-gate #define	FLF_M		0x00000080
3057c478bd9Sstevel@tonic-gate #define	FLF_S		0x00000100
3067c478bd9Sstevel@tonic-gate static const char flag_list[] = "ABDGHLUMS";
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate typedef struct filter_rule filter_t;
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate struct filter_rule {
3117c478bd9Sstevel@tonic-gate 	filter_t *f_next;
3127c478bd9Sstevel@tonic-gate 	union {
3137c478bd9Sstevel@tonic-gate 		int f_family;
3147c478bd9Sstevel@tonic-gate 		const char *f_ifname;
3157c478bd9Sstevel@tonic-gate 		struct {
3167c478bd9Sstevel@tonic-gate 			struct hostent *f_address;
3177c478bd9Sstevel@tonic-gate 			in6_addr_t f_mask;
3187c478bd9Sstevel@tonic-gate 		} a;
3197c478bd9Sstevel@tonic-gate 		struct {
3207c478bd9Sstevel@tonic-gate 			uint_t f_flagset;
3217c478bd9Sstevel@tonic-gate 			uint_t f_flagclear;
3227c478bd9Sstevel@tonic-gate 		} f;
3237c478bd9Sstevel@tonic-gate 	} u;
3247c478bd9Sstevel@tonic-gate };
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate /*
3277c478bd9Sstevel@tonic-gate  * The user-specified filters are linked into lists separated by
3287c478bd9Sstevel@tonic-gate  * keyword (type of filter).  Thus, the matching algorithm is:
3297c478bd9Sstevel@tonic-gate  *	For each non-empty filter list
3307c478bd9Sstevel@tonic-gate  *		If no filters in the list match
3317c478bd9Sstevel@tonic-gate  *			then stop here; route doesn't match
3327c478bd9Sstevel@tonic-gate  *	If loop above completes, then route does match and will be
3337c478bd9Sstevel@tonic-gate  *	displayed.
3347c478bd9Sstevel@tonic-gate  */
3357c478bd9Sstevel@tonic-gate static filter_t *filters[NFILTERKEYS];
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate int
3387c478bd9Sstevel@tonic-gate main(int argc, char **argv)
3397c478bd9Sstevel@tonic-gate {
3407c478bd9Sstevel@tonic-gate 	char		*name;
3417c478bd9Sstevel@tonic-gate 	mib_item_t	*item = NULL;
3427c478bd9Sstevel@tonic-gate 	mib_item_t	*previtem = NULL;
3437c478bd9Sstevel@tonic-gate 	int		sd = -1;
3447c478bd9Sstevel@tonic-gate 	char	*ifname = NULL;
3457c478bd9Sstevel@tonic-gate 	int	interval = 0;	/* Single time by default */
3467c478bd9Sstevel@tonic-gate 	int	count = -1;	/* Forever */
3477c478bd9Sstevel@tonic-gate 	int	c;
3487c478bd9Sstevel@tonic-gate 	int	d;
3497c478bd9Sstevel@tonic-gate 	/*
3507c478bd9Sstevel@tonic-gate 	 * Possible values of 'Iflag_only':
3517c478bd9Sstevel@tonic-gate 	 * -1, no feature-flags;
3527c478bd9Sstevel@tonic-gate 	 *  0, IFlag and other feature-flags enabled
3537c478bd9Sstevel@tonic-gate 	 *  1, IFlag is the only feature-flag enabled
3547c478bd9Sstevel@tonic-gate 	 * : trinary variable, modified using IFLAGMOD()
3557c478bd9Sstevel@tonic-gate 	 */
3567c478bd9Sstevel@tonic-gate 	int Iflag_only = -1;
3577c478bd9Sstevel@tonic-gate 	boolean_t once_only = B_FALSE; /* '-i' with count > 1 */
3587c478bd9Sstevel@tonic-gate 	extern char	*optarg;
3597c478bd9Sstevel@tonic-gate 	extern int	optind;
3607c478bd9Sstevel@tonic-gate 	char *default_ip_str = NULL;
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	name = argv[0];
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 	v4compat = get_compat_flag(&default_ip_str);
3657c478bd9Sstevel@tonic-gate 	if (v4compat == DEFAULT_PROT_BAD_VALUE)
3667c478bd9Sstevel@tonic-gate 		fatal(2, "%s: %s: Bad value for %s in %s\n", name,
3677c478bd9Sstevel@tonic-gate 		    default_ip_str, DEFAULT_IP, INET_DEFAULT_FILE);
3687c478bd9Sstevel@tonic-gate 	free(default_ip_str);
3697c478bd9Sstevel@tonic-gate 
370*45916cd2Sjpk 	while ((c = getopt(argc, argv, "adimnrspMgvf:P:I:DR")) != -1) {
3717c478bd9Sstevel@tonic-gate 		switch ((char)c) {
3727c478bd9Sstevel@tonic-gate 		case 'a':		/* all connections */
3737c478bd9Sstevel@tonic-gate 			Aflag = B_TRUE;
3747c478bd9Sstevel@tonic-gate 			break;
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 		case 'd':		/* turn on debugging */
3777c478bd9Sstevel@tonic-gate 			Dflag = B_TRUE;
3787c478bd9Sstevel@tonic-gate 			break;
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 		case 'i':		/* interface (ill/ipif report) */
3817c478bd9Sstevel@tonic-gate 			Iflag = B_TRUE;
3827c478bd9Sstevel@tonic-gate 			IFLAGMOD(Iflag_only, -1, 1); /* '-i' exists */
3837c478bd9Sstevel@tonic-gate 			break;
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 		case 'm':		/* streams msg report */
3867c478bd9Sstevel@tonic-gate 			Mflag = B_TRUE;
3877c478bd9Sstevel@tonic-gate 			IFLAGMOD(Iflag_only, 1, 0); /* see macro def'n */
3887c478bd9Sstevel@tonic-gate 			break;
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 		case 'n':		/* numeric format */
3917c478bd9Sstevel@tonic-gate 			Nflag = B_TRUE;
3927c478bd9Sstevel@tonic-gate 			break;
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 		case 'r':		/* route tables */
3957c478bd9Sstevel@tonic-gate 			Rflag = B_TRUE;
3967c478bd9Sstevel@tonic-gate 			IFLAGMOD(Iflag_only, 1, 0); /* see macro def'n */
3977c478bd9Sstevel@tonic-gate 			break;
3987c478bd9Sstevel@tonic-gate 
399*45916cd2Sjpk 		case 'R':		/* security attributes */
400*45916cd2Sjpk 			RSECflag = B_TRUE;
401*45916cd2Sjpk 			IFLAGMOD(Iflag_only, 1, 0); /* see macro def'n */
402*45916cd2Sjpk 			break;
403*45916cd2Sjpk 
4047c478bd9Sstevel@tonic-gate 		case 's':		/* per-protocol statistics */
4057c478bd9Sstevel@tonic-gate 			Sflag = B_TRUE;
4067c478bd9Sstevel@tonic-gate 			IFLAGMOD(Iflag_only, 1, 0); /* see macro def'n */
4077c478bd9Sstevel@tonic-gate 			break;
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 		case 'p':		/* arp/ndp table */
4107c478bd9Sstevel@tonic-gate 			Pflag = B_TRUE;
4117c478bd9Sstevel@tonic-gate 			IFLAGMOD(Iflag_only, 1, 0); /* see macro def'n */
4127c478bd9Sstevel@tonic-gate 			break;
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 		case 'M':		/* multicast routing tables */
4157c478bd9Sstevel@tonic-gate 			MMflag = B_TRUE;
4167c478bd9Sstevel@tonic-gate 			IFLAGMOD(Iflag_only, 1, 0); /* see macro def'n */
4177c478bd9Sstevel@tonic-gate 			break;
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate 		case 'g':		/* multicast group membership */
4207c478bd9Sstevel@tonic-gate 			Gflag = B_TRUE;
4217c478bd9Sstevel@tonic-gate 			IFLAGMOD(Iflag_only, 1, 0); /* see macro def'n */
4227c478bd9Sstevel@tonic-gate 			break;
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate 		case 'v':		/* verbose output format */
4257c478bd9Sstevel@tonic-gate 			Vflag = B_TRUE;
4267c478bd9Sstevel@tonic-gate 			IFLAGMOD(Iflag_only, 1, 0); /* see macro def'n */
4277c478bd9Sstevel@tonic-gate 			break;
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 		case 'f':
4307c478bd9Sstevel@tonic-gate 			process_filter(optarg);
4317c478bd9Sstevel@tonic-gate 			break;
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 		case 'P':
4347c478bd9Sstevel@tonic-gate 			if (strcmp(optarg, "ip") == 0) {
4357c478bd9Sstevel@tonic-gate 				proto = IPPROTO_IP;
4367c478bd9Sstevel@tonic-gate 			} else if (strcmp(optarg, "ipv6") == 0 ||
4377c478bd9Sstevel@tonic-gate 			    strcmp(optarg, "ip6") == 0) {
4387c478bd9Sstevel@tonic-gate 				v4compat = 0;	/* Overridden */
4397c478bd9Sstevel@tonic-gate 				proto = IPPROTO_IPV6;
4407c478bd9Sstevel@tonic-gate 			} else if (strcmp(optarg, "icmp") == 0) {
4417c478bd9Sstevel@tonic-gate 				proto = IPPROTO_ICMP;
4427c478bd9Sstevel@tonic-gate 			} else if (strcmp(optarg, "icmpv6") == 0 ||
4437c478bd9Sstevel@tonic-gate 			    strcmp(optarg, "icmp6") == 0) {
4447c478bd9Sstevel@tonic-gate 				v4compat = 0;	/* Overridden */
4457c478bd9Sstevel@tonic-gate 				proto = IPPROTO_ICMPV6;
4467c478bd9Sstevel@tonic-gate 			} else if (strcmp(optarg, "igmp") == 0) {
4477c478bd9Sstevel@tonic-gate 				proto = IPPROTO_IGMP;
4487c478bd9Sstevel@tonic-gate 			} else if (strcmp(optarg, "udp") == 0) {
4497c478bd9Sstevel@tonic-gate 				proto = IPPROTO_UDP;
4507c478bd9Sstevel@tonic-gate 			} else if (strcmp(optarg, "tcp") == 0) {
4517c478bd9Sstevel@tonic-gate 				proto = IPPROTO_TCP;
4527c478bd9Sstevel@tonic-gate 			} else if (strcmp(optarg, "sctp") == 0) {
4537c478bd9Sstevel@tonic-gate 				proto = IPPROTO_SCTP;
4547c478bd9Sstevel@tonic-gate 			} else if (strcmp(optarg, "raw") == 0 ||
4557c478bd9Sstevel@tonic-gate 			    strcmp(optarg, "rawip") == 0) {
4567c478bd9Sstevel@tonic-gate 				proto = IPPROTO_RAW;
4577c478bd9Sstevel@tonic-gate 			} else {
4587c478bd9Sstevel@tonic-gate 				fatal(1, "%s: unknown protocol.\n", optarg);
4597c478bd9Sstevel@tonic-gate 			}
4607c478bd9Sstevel@tonic-gate 			break;
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 		case 'I':
4637c478bd9Sstevel@tonic-gate 			ifname = optarg;
4647c478bd9Sstevel@tonic-gate 			Iflag = B_TRUE;
4657c478bd9Sstevel@tonic-gate 			IFLAGMOD(Iflag_only, -1, 1); /* see macro def'n */
4667c478bd9Sstevel@tonic-gate 			break;
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate 		case 'D':
4697c478bd9Sstevel@tonic-gate 			DHCPflag = B_TRUE;
4707c478bd9Sstevel@tonic-gate 			Iflag_only = 0;
4717c478bd9Sstevel@tonic-gate 			break;
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 		case '?':
4747c478bd9Sstevel@tonic-gate 		default:
4757c478bd9Sstevel@tonic-gate 			usage(name);
4767c478bd9Sstevel@tonic-gate 		}
4777c478bd9Sstevel@tonic-gate 	}
4787c478bd9Sstevel@tonic-gate 
479*45916cd2Sjpk 	/*
480*45916cd2Sjpk 	 * Make sure -R option is set only on a labeled system.
481*45916cd2Sjpk 	 */
482*45916cd2Sjpk 	if (RSECflag && !is_system_labeled()) {
483*45916cd2Sjpk 		(void) fprintf(stderr, "-R set but labeling is not enabled\n");
484*45916cd2Sjpk 		usage(name);
485*45916cd2Sjpk 	}
486*45916cd2Sjpk 
4877c478bd9Sstevel@tonic-gate 	/*
4887c478bd9Sstevel@tonic-gate 	 * Handle other arguments: find interval, count; the
4897c478bd9Sstevel@tonic-gate 	 * flags that accept 'interval' and 'count' are OR'd
4907c478bd9Sstevel@tonic-gate 	 * in the outermost 'if'; more flags may be added as
4917c478bd9Sstevel@tonic-gate 	 * required
4927c478bd9Sstevel@tonic-gate 	 */
4937c478bd9Sstevel@tonic-gate 	if (Iflag || Sflag || Mflag) {
4947c478bd9Sstevel@tonic-gate 		for (d = optind; d < argc; d++) {
4957c478bd9Sstevel@tonic-gate 			if (isnum(argv[d])) {
4967c478bd9Sstevel@tonic-gate 				interval = atoi(argv[d]);
4977c478bd9Sstevel@tonic-gate 				if (d + 1 < argc &&
4987c478bd9Sstevel@tonic-gate 				    isnum(argv[d + 1])) {
4997c478bd9Sstevel@tonic-gate 					count = atoi(argv[d + 1]);
5007c478bd9Sstevel@tonic-gate 					optind++;
5017c478bd9Sstevel@tonic-gate 				}
5027c478bd9Sstevel@tonic-gate 				optind++;
5037c478bd9Sstevel@tonic-gate 				if (interval == 0 || count == 0)
5047c478bd9Sstevel@tonic-gate 					usage(name);
5057c478bd9Sstevel@tonic-gate 				break;
5067c478bd9Sstevel@tonic-gate 			}
5077c478bd9Sstevel@tonic-gate 		}
5087c478bd9Sstevel@tonic-gate 	}
5097c478bd9Sstevel@tonic-gate 	if (optind < argc) {
5107c478bd9Sstevel@tonic-gate 		if (Iflag && isnum(argv[optind])) {
5117c478bd9Sstevel@tonic-gate 			count = atoi(argv[optind]);
5127c478bd9Sstevel@tonic-gate 			if (count == 0)
5137c478bd9Sstevel@tonic-gate 				usage(name);
5147c478bd9Sstevel@tonic-gate 			optind++;
5157c478bd9Sstevel@tonic-gate 		}
5167c478bd9Sstevel@tonic-gate 	}
5177c478bd9Sstevel@tonic-gate 	if (optind < argc) {
5187c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
5197c478bd9Sstevel@tonic-gate 		    "%s: extra arguments\n", name);
5207c478bd9Sstevel@tonic-gate 		usage(name);
5217c478bd9Sstevel@tonic-gate 	}
5227c478bd9Sstevel@tonic-gate 	if (interval)
5237c478bd9Sstevel@tonic-gate 		setbuf(stdout, NULL);
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 	if (DHCPflag) {
5267c478bd9Sstevel@tonic-gate 		dhcp_report(Iflag ? ifname : NULL);
5277c478bd9Sstevel@tonic-gate 		exit(0);
5287c478bd9Sstevel@tonic-gate 	}
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate 	/* Get data structures: priming before iteration */
5317c478bd9Sstevel@tonic-gate 	if (family_selected(AF_INET) || family_selected(AF_INET6)) {
5327c478bd9Sstevel@tonic-gate 		sd = mibopen();
5337c478bd9Sstevel@tonic-gate 		if (sd == -1)
5347c478bd9Sstevel@tonic-gate 			fatal(1, "can't open mib stream\n");
5357c478bd9Sstevel@tonic-gate 		if ((item = mibget(sd)) == NULL) {
5367c478bd9Sstevel@tonic-gate 			(void) close(sd);
5377c478bd9Sstevel@tonic-gate 			fatal(1, "mibget() failed\n");
5387c478bd9Sstevel@tonic-gate 		}
5397c478bd9Sstevel@tonic-gate 		/* Extract constant sizes - need do once only */
5407c478bd9Sstevel@tonic-gate 		mib_get_constants(item);
5417c478bd9Sstevel@tonic-gate 	}
5427c478bd9Sstevel@tonic-gate 	if ((kc = kstat_open()) == NULL) {
5437c478bd9Sstevel@tonic-gate 		mibfree(item);
5447c478bd9Sstevel@tonic-gate 		(void) close(sd);
5457c478bd9Sstevel@tonic-gate 		fail(1, "kstat_open(): can't open /dev/kstat");
5467c478bd9Sstevel@tonic-gate 	}
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 	if (interval <= 0) {
5497c478bd9Sstevel@tonic-gate 		count = 1;
5507c478bd9Sstevel@tonic-gate 		once_only = B_TRUE;
5517c478bd9Sstevel@tonic-gate 	}
5527c478bd9Sstevel@tonic-gate 	/* 'for' loop 1: */
5537c478bd9Sstevel@tonic-gate 	for (;;) {
5547c478bd9Sstevel@tonic-gate 		mib_item_t *curritem = NULL; /* only for -[M]s */
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate 		/* netstat: AF_INET[6] behaviour */
5577c478bd9Sstevel@tonic-gate 		if (family_selected(AF_INET) || family_selected(AF_INET6)) {
5587c478bd9Sstevel@tonic-gate 			if (Sflag) {
5597c478bd9Sstevel@tonic-gate 				curritem = mib_item_diff(previtem, item);
5607c478bd9Sstevel@tonic-gate 				if (curritem == NULL)
5617c478bd9Sstevel@tonic-gate 					fatal(1, "can't process mib data, "
5627c478bd9Sstevel@tonic-gate 					    "out of memory\n");
5637c478bd9Sstevel@tonic-gate 				mib_item_destroy(&previtem);
5647c478bd9Sstevel@tonic-gate 			}
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 			if (!(Iflag || Rflag || Sflag || Mflag ||
5677c478bd9Sstevel@tonic-gate 			    MMflag || Pflag || Gflag || DHCPflag)) {
5687c478bd9Sstevel@tonic-gate 				if (protocol_selected(IPPROTO_UDP))
5697c478bd9Sstevel@tonic-gate 					udp_report(item);
5707c478bd9Sstevel@tonic-gate 				if (protocol_selected(IPPROTO_TCP))
5717c478bd9Sstevel@tonic-gate 					tcp_report(item);
5727c478bd9Sstevel@tonic-gate 				if (protocol_selected(IPPROTO_SCTP))
5737c478bd9Sstevel@tonic-gate 					sctp_report(item);
5747c478bd9Sstevel@tonic-gate 			}
5757c478bd9Sstevel@tonic-gate 			if (Iflag)
5767c478bd9Sstevel@tonic-gate 				if_report(item, ifname, Iflag_only, once_only);
5777c478bd9Sstevel@tonic-gate 			if (Mflag)
5787c478bd9Sstevel@tonic-gate 				m_report();
5797c478bd9Sstevel@tonic-gate 			if (Rflag)
5807c478bd9Sstevel@tonic-gate 				ire_report(item);
5817c478bd9Sstevel@tonic-gate 			if (Sflag && MMflag) {
5827c478bd9Sstevel@tonic-gate 				mrt_stat_report(curritem);
5837c478bd9Sstevel@tonic-gate 			} else {
5847c478bd9Sstevel@tonic-gate 				if (Sflag)
5857c478bd9Sstevel@tonic-gate 					stat_report(curritem);
5867c478bd9Sstevel@tonic-gate 				if (MMflag)
5877c478bd9Sstevel@tonic-gate 					mrt_report(item);
5887c478bd9Sstevel@tonic-gate 			}
5897c478bd9Sstevel@tonic-gate 			if (Gflag)
5907c478bd9Sstevel@tonic-gate 				group_report(item);
5917c478bd9Sstevel@tonic-gate 			if (Pflag) {
5927c478bd9Sstevel@tonic-gate 				if (family_selected(AF_INET))
5937c478bd9Sstevel@tonic-gate 					arp_report(item);
5947c478bd9Sstevel@tonic-gate 				if (family_selected(AF_INET6))
5957c478bd9Sstevel@tonic-gate 					ndp_report(item);
5967c478bd9Sstevel@tonic-gate 			}
5977c478bd9Sstevel@tonic-gate 			mib_item_destroy(&curritem);
5987c478bd9Sstevel@tonic-gate 		}
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 		/* netstat: AF_UNIX behaviour */
6017c478bd9Sstevel@tonic-gate 		if (family_selected(AF_UNIX) &&
6027c478bd9Sstevel@tonic-gate 		    (!(Iflag || Rflag || Sflag || Mflag ||
6037c478bd9Sstevel@tonic-gate 		    MMflag || Pflag || Gflag)))
6047c478bd9Sstevel@tonic-gate 			unixpr(kc);
6057c478bd9Sstevel@tonic-gate 		(void) kstat_close(kc);
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 		/* iteration handling code */
6087c478bd9Sstevel@tonic-gate 		if (count > 0 && --count == 0)
6097c478bd9Sstevel@tonic-gate 			break;
6107c478bd9Sstevel@tonic-gate 		(void) sleep(interval);
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate 		/* re-populating of data structures */
6137c478bd9Sstevel@tonic-gate 		if (family_selected(AF_INET) || family_selected(AF_INET6)) {
6147c478bd9Sstevel@tonic-gate 			if (Sflag) {
6157c478bd9Sstevel@tonic-gate 				/* previtem is a cut-down list */
6167c478bd9Sstevel@tonic-gate 				previtem = mib_item_dup(item);
6177c478bd9Sstevel@tonic-gate 				if (previtem == NULL)
6187c478bd9Sstevel@tonic-gate 					fatal(1, "can't process mib data, "
6197c478bd9Sstevel@tonic-gate 					    "out of memory\n");
6207c478bd9Sstevel@tonic-gate 			}
6217c478bd9Sstevel@tonic-gate 			mibfree(item);
6227c478bd9Sstevel@tonic-gate 			(void) close(sd);
6237c478bd9Sstevel@tonic-gate 			if ((sd = mibopen()) == -1)
6247c478bd9Sstevel@tonic-gate 				fatal(1, "can't open mib stream anymore\n");
6257c478bd9Sstevel@tonic-gate 			if ((item = mibget(sd)) == NULL) {
6267c478bd9Sstevel@tonic-gate 				(void) close(sd);
6277c478bd9Sstevel@tonic-gate 				fatal(1, "mibget() failed\n");
6287c478bd9Sstevel@tonic-gate 			}
6297c478bd9Sstevel@tonic-gate 		}
6307c478bd9Sstevel@tonic-gate 		if ((kc = kstat_open()) == NULL)
6317c478bd9Sstevel@tonic-gate 			fail(1, "kstat_open(): can't open /dev/kstat");
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate 	} /* 'for' loop 1 ends */
6347c478bd9Sstevel@tonic-gate 	mibfree(item);
6357c478bd9Sstevel@tonic-gate 	(void) close(sd);
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 	return (0);
6387c478bd9Sstevel@tonic-gate }
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate static int
6427c478bd9Sstevel@tonic-gate isnum(char *p)
6437c478bd9Sstevel@tonic-gate {
6447c478bd9Sstevel@tonic-gate 	int	len;
6457c478bd9Sstevel@tonic-gate 	int	i;
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 	len = strlen(p);
6487c478bd9Sstevel@tonic-gate 	for (i = 0; i < len; i++)
6497c478bd9Sstevel@tonic-gate 		if (!isdigit(p[i]))
6507c478bd9Sstevel@tonic-gate 			return (0);
6517c478bd9Sstevel@tonic-gate 	return (1);
6527c478bd9Sstevel@tonic-gate }
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate /* --------------------------------- MIBGET -------------------------------- */
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate static mib_item_t *
6587c478bd9Sstevel@tonic-gate mibget(int sd)
6597c478bd9Sstevel@tonic-gate {
6607c478bd9Sstevel@tonic-gate 	/*
6617c478bd9Sstevel@tonic-gate 	 * buf is an automatic for this function, so the
6627c478bd9Sstevel@tonic-gate 	 * compiler has complete control over its alignment;
6637c478bd9Sstevel@tonic-gate 	 * it is assumed this alignment is satisfactory for
6647c478bd9Sstevel@tonic-gate 	 * it to be casted to certain other struct pointers
6657c478bd9Sstevel@tonic-gate 	 * here, such as struct T_optmgmt_ack * .
6667c478bd9Sstevel@tonic-gate 	 */
6677c478bd9Sstevel@tonic-gate 	uintptr_t		buf[512 / sizeof (uintptr_t)];
6687c478bd9Sstevel@tonic-gate 	int			flags;
6697c478bd9Sstevel@tonic-gate 	int			i, j, getcode;
6707c478bd9Sstevel@tonic-gate 	struct strbuf		ctlbuf, databuf;
6717c478bd9Sstevel@tonic-gate 	struct T_optmgmt_req	*tor = (struct T_optmgmt_req *)buf;
6727c478bd9Sstevel@tonic-gate 	struct T_optmgmt_ack	*toa = (struct T_optmgmt_ack *)buf;
6737c478bd9Sstevel@tonic-gate 	struct T_error_ack	*tea = (struct T_error_ack *)buf;
6747c478bd9Sstevel@tonic-gate 	struct opthdr		*req;
6757c478bd9Sstevel@tonic-gate 	mib_item_t		*first_item = NULL;
6767c478bd9Sstevel@tonic-gate 	mib_item_t		*last_item  = NULL;
6777c478bd9Sstevel@tonic-gate 	mib_item_t		*temp;
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 	tor->PRIM_type = T_SVR4_OPTMGMT_REQ;
6807c478bd9Sstevel@tonic-gate 	tor->OPT_offset = sizeof (struct T_optmgmt_req);
6817c478bd9Sstevel@tonic-gate 	tor->OPT_length = sizeof (struct opthdr);
6827c478bd9Sstevel@tonic-gate 	tor->MGMT_flags = T_CURRENT;
6837c478bd9Sstevel@tonic-gate 	req = (struct opthdr *)&tor[1];
6847c478bd9Sstevel@tonic-gate 	req->level = MIB2_IP;		/* any MIB2_xxx value ok here */
6857c478bd9Sstevel@tonic-gate 	req->name  = 0;
6867c478bd9Sstevel@tonic-gate 	req->len   = 0;
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 	ctlbuf.buf = (char *)buf;
6897c478bd9Sstevel@tonic-gate 	ctlbuf.len = tor->OPT_length + tor->OPT_offset;
6907c478bd9Sstevel@tonic-gate 	flags = 0;
6917c478bd9Sstevel@tonic-gate 	if (putmsg(sd, &ctlbuf, (struct strbuf *)0, flags) == -1) {
6927c478bd9Sstevel@tonic-gate 		perror("mibget: putmsg(ctl) failed");
6937c478bd9Sstevel@tonic-gate 		goto error_exit;
6947c478bd9Sstevel@tonic-gate 	}
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate 	/*
6977c478bd9Sstevel@tonic-gate 	 * Each reply consists of a ctl part for one fixed structure
6987c478bd9Sstevel@tonic-gate 	 * or table, as defined in mib2.h.  The format is a T_OPTMGMT_ACK,
6997c478bd9Sstevel@tonic-gate 	 * containing an opthdr structure.  level/name identify the entry,
7007c478bd9Sstevel@tonic-gate 	 * len is the size of the data part of the message.
7017c478bd9Sstevel@tonic-gate 	 */
7027c478bd9Sstevel@tonic-gate 	req = (struct opthdr *)&toa[1];
7037c478bd9Sstevel@tonic-gate 	ctlbuf.maxlen = sizeof (buf);
7047c478bd9Sstevel@tonic-gate 	j = 1;
7057c478bd9Sstevel@tonic-gate 	for (;;) {
7067c478bd9Sstevel@tonic-gate 		flags = 0;
7077c478bd9Sstevel@tonic-gate 		getcode = getmsg(sd, &ctlbuf, (struct strbuf *)0, &flags);
7087c478bd9Sstevel@tonic-gate 		if (getcode == -1) {
7097c478bd9Sstevel@tonic-gate 			perror("mibget getmsg(ctl) failed");
7107c478bd9Sstevel@tonic-gate 			if (Dflag) {
7117c478bd9Sstevel@tonic-gate 				(void) fputs("#   level   name    len\n",
7127c478bd9Sstevel@tonic-gate 				    stderr);
7137c478bd9Sstevel@tonic-gate 				i = 0;
7147c478bd9Sstevel@tonic-gate 				for (last_item = first_item; last_item;
7157c478bd9Sstevel@tonic-gate 					last_item = last_item->next_item)
7167c478bd9Sstevel@tonic-gate 					(void) printf("%d  %4d   %5d   %d\n",
7177c478bd9Sstevel@tonic-gate 					    ++i,
7187c478bd9Sstevel@tonic-gate 					    last_item->group,
7197c478bd9Sstevel@tonic-gate 					    last_item->mib_id,
7207c478bd9Sstevel@tonic-gate 					    last_item->length);
7217c478bd9Sstevel@tonic-gate 			}
7227c478bd9Sstevel@tonic-gate 			goto error_exit;
7237c478bd9Sstevel@tonic-gate 		}
7247c478bd9Sstevel@tonic-gate 		if (getcode == 0 &&
7257c478bd9Sstevel@tonic-gate 		    ctlbuf.len >= sizeof (struct T_optmgmt_ack) &&
7267c478bd9Sstevel@tonic-gate 		    toa->PRIM_type == T_OPTMGMT_ACK &&
7277c478bd9Sstevel@tonic-gate 		    toa->MGMT_flags == T_SUCCESS &&
7287c478bd9Sstevel@tonic-gate 		    req->len == 0) {
7297c478bd9Sstevel@tonic-gate 			if (Dflag)
7307c478bd9Sstevel@tonic-gate 				(void) printf("mibget getmsg() %d returned "
7317c478bd9Sstevel@tonic-gate 				    "EOD (level %ld, name %ld)\n",
7327c478bd9Sstevel@tonic-gate 				    j, req->level, req->name);
7337c478bd9Sstevel@tonic-gate 			return (first_item);		/* this is EOD msg */
7347c478bd9Sstevel@tonic-gate 		}
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate 		if (ctlbuf.len >= sizeof (struct T_error_ack) &&
7377c478bd9Sstevel@tonic-gate 		    tea->PRIM_type == T_ERROR_ACK) {
7387c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
7397c478bd9Sstevel@tonic-gate 			    "mibget %d gives T_ERROR_ACK: TLI_error = 0x%lx, "
7407c478bd9Sstevel@tonic-gate 			    "UNIX_error = 0x%lx\n",
7417c478bd9Sstevel@tonic-gate 			    j, tea->TLI_error, tea->UNIX_error);
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate 			errno = (tea->TLI_error == TSYSERR) ?
7447c478bd9Sstevel@tonic-gate 			    tea->UNIX_error : EPROTO;
7457c478bd9Sstevel@tonic-gate 			goto error_exit;
7467c478bd9Sstevel@tonic-gate 		}
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate 		if (getcode != MOREDATA ||
7497c478bd9Sstevel@tonic-gate 		    ctlbuf.len < sizeof (struct T_optmgmt_ack) ||
7507c478bd9Sstevel@tonic-gate 		    toa->PRIM_type != T_OPTMGMT_ACK ||
7517c478bd9Sstevel@tonic-gate 		    toa->MGMT_flags != T_SUCCESS) {
7527c478bd9Sstevel@tonic-gate 			(void) printf("mibget getmsg(ctl) %d returned %d, "
7537c478bd9Sstevel@tonic-gate 			    "ctlbuf.len = %d, PRIM_type = %ld\n",
7547c478bd9Sstevel@tonic-gate 			    j, getcode, ctlbuf.len, toa->PRIM_type);
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate 			if (toa->PRIM_type == T_OPTMGMT_ACK)
7577c478bd9Sstevel@tonic-gate 				(void) printf("T_OPTMGMT_ACK: "
7587c478bd9Sstevel@tonic-gate 				    "MGMT_flags = 0x%lx, req->len = %ld\n",
7597c478bd9Sstevel@tonic-gate 				    toa->MGMT_flags, req->len);
7607c478bd9Sstevel@tonic-gate 			errno = ENOMSG;
7617c478bd9Sstevel@tonic-gate 			goto error_exit;
7627c478bd9Sstevel@tonic-gate 		}
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate 		temp = (mib_item_t *)malloc(sizeof (mib_item_t));
7657c478bd9Sstevel@tonic-gate 		if (temp == NULL) {
7667c478bd9Sstevel@tonic-gate 			perror("mibget malloc failed");
7677c478bd9Sstevel@tonic-gate 			goto error_exit;
7687c478bd9Sstevel@tonic-gate 		}
7697c478bd9Sstevel@tonic-gate 		if (last_item != NULL)
7707c478bd9Sstevel@tonic-gate 			last_item->next_item = temp;
7717c478bd9Sstevel@tonic-gate 		else
7727c478bd9Sstevel@tonic-gate 			first_item = temp;
7737c478bd9Sstevel@tonic-gate 		last_item = temp;
7747c478bd9Sstevel@tonic-gate 		last_item->next_item = NULL;
7757c478bd9Sstevel@tonic-gate 		last_item->group = req->level;
7767c478bd9Sstevel@tonic-gate 		last_item->mib_id = req->name;
7777c478bd9Sstevel@tonic-gate 		last_item->length = req->len;
7787c478bd9Sstevel@tonic-gate 		last_item->valp = malloc((int)req->len);
7797c478bd9Sstevel@tonic-gate 		if (last_item->valp == NULL)
7807c478bd9Sstevel@tonic-gate 			goto error_exit;
7817c478bd9Sstevel@tonic-gate 		if (Dflag)
7827c478bd9Sstevel@tonic-gate 			(void) printf("msg %d: group = %4d   mib_id = %5d"
7837c478bd9Sstevel@tonic-gate 			    "length = %d\n",
7847c478bd9Sstevel@tonic-gate 			    j, last_item->group, last_item->mib_id,
7857c478bd9Sstevel@tonic-gate 			    last_item->length);
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate 		databuf.maxlen = last_item->length;
7887c478bd9Sstevel@tonic-gate 		databuf.buf    = (char *)last_item->valp;
7897c478bd9Sstevel@tonic-gate 		databuf.len    = 0;
7907c478bd9Sstevel@tonic-gate 		flags = 0;
7917c478bd9Sstevel@tonic-gate 		getcode = getmsg(sd, (struct strbuf *)0, &databuf, &flags);
7927c478bd9Sstevel@tonic-gate 		if (getcode == -1) {
7937c478bd9Sstevel@tonic-gate 			perror("mibget getmsg(data) failed");
7947c478bd9Sstevel@tonic-gate 			goto error_exit;
7957c478bd9Sstevel@tonic-gate 		} else if (getcode != 0) {
7967c478bd9Sstevel@tonic-gate 			(void) printf("mibget getmsg(data) returned %d, "
7977c478bd9Sstevel@tonic-gate 			    "databuf.maxlen = %d, databuf.len = %d\n",
7987c478bd9Sstevel@tonic-gate 			    getcode, databuf.maxlen, databuf.len);
7997c478bd9Sstevel@tonic-gate 			goto error_exit;
8007c478bd9Sstevel@tonic-gate 		}
8017c478bd9Sstevel@tonic-gate 		j++;
8027c478bd9Sstevel@tonic-gate 	}
8037c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate error_exit:;
8067c478bd9Sstevel@tonic-gate 	mibfree(first_item);
8077c478bd9Sstevel@tonic-gate 	return (NULL);
8087c478bd9Sstevel@tonic-gate }
8097c478bd9Sstevel@tonic-gate 
8107c478bd9Sstevel@tonic-gate /*
8117c478bd9Sstevel@tonic-gate  * mibfree: frees a linked list of type (mib_item_t *)
8127c478bd9Sstevel@tonic-gate  * returned by mibget(); this is NOT THE SAME AS
8137c478bd9Sstevel@tonic-gate  * mib_item_destroy(), so should be used for objects
8147c478bd9Sstevel@tonic-gate  * returned by mibget() only
8157c478bd9Sstevel@tonic-gate  */
8167c478bd9Sstevel@tonic-gate static void
8177c478bd9Sstevel@tonic-gate mibfree(mib_item_t *firstitem)
8187c478bd9Sstevel@tonic-gate {
8197c478bd9Sstevel@tonic-gate 	mib_item_t *lastitem;
8207c478bd9Sstevel@tonic-gate 
8217c478bd9Sstevel@tonic-gate 	while (firstitem != NULL) {
8227c478bd9Sstevel@tonic-gate 		lastitem = firstitem;
8237c478bd9Sstevel@tonic-gate 		firstitem = firstitem->next_item;
8247c478bd9Sstevel@tonic-gate 		if (lastitem->valp != NULL)
8257c478bd9Sstevel@tonic-gate 			free(lastitem->valp);
8267c478bd9Sstevel@tonic-gate 		free(lastitem);
8277c478bd9Sstevel@tonic-gate 	}
8287c478bd9Sstevel@tonic-gate }
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate static int
8317c478bd9Sstevel@tonic-gate mibopen(void)
8327c478bd9Sstevel@tonic-gate {
8337c478bd9Sstevel@tonic-gate 	int	sd;
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate 	sd = open("/dev/arp", O_RDWR);
8367c478bd9Sstevel@tonic-gate 	if (sd == -1) {
8377c478bd9Sstevel@tonic-gate 		perror("arp open");
8387c478bd9Sstevel@tonic-gate 		return (-1);
8397c478bd9Sstevel@tonic-gate 	}
8407c478bd9Sstevel@tonic-gate 	if (ioctl(sd, I_PUSH, "tcp") == -1) {
8417c478bd9Sstevel@tonic-gate 		perror("tcp I_PUSH");
8427c478bd9Sstevel@tonic-gate 		(void) close(sd);
8437c478bd9Sstevel@tonic-gate 		return (-1);
8447c478bd9Sstevel@tonic-gate 	}
8457c478bd9Sstevel@tonic-gate 	if (ioctl(sd, I_PUSH, "udp") == -1) {
8467c478bd9Sstevel@tonic-gate 		perror("udp I_PUSH");
8477c478bd9Sstevel@tonic-gate 		(void) close(sd);
8487c478bd9Sstevel@tonic-gate 		return (-1);
8497c478bd9Sstevel@tonic-gate 	}
8507c478bd9Sstevel@tonic-gate 	if (ioctl(sd, I_PUSH, "icmp") == -1) {
8517c478bd9Sstevel@tonic-gate 		perror("icmp I_PUSH");
8527c478bd9Sstevel@tonic-gate 		(void) close(sd);
8537c478bd9Sstevel@tonic-gate 		return (-1);
8547c478bd9Sstevel@tonic-gate 	}
8557c478bd9Sstevel@tonic-gate 	return (sd);
8567c478bd9Sstevel@tonic-gate }
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate /*
8597c478bd9Sstevel@tonic-gate  * mib_item_dup: returns a clean mib_item_t * linked
8607c478bd9Sstevel@tonic-gate  * list, so that for every element item->mib_id is 0;
8617c478bd9Sstevel@tonic-gate  * to deallocate this linked list, use mib_item_destroy
8627c478bd9Sstevel@tonic-gate  */
8637c478bd9Sstevel@tonic-gate static mib_item_t *
8647c478bd9Sstevel@tonic-gate mib_item_dup(mib_item_t *item)
8657c478bd9Sstevel@tonic-gate {
8667c478bd9Sstevel@tonic-gate 	int	c = 0;
8677c478bd9Sstevel@tonic-gate 	mib_item_t *localp;
8687c478bd9Sstevel@tonic-gate 	mib_item_t *tempp;
8697c478bd9Sstevel@tonic-gate 
8707c478bd9Sstevel@tonic-gate 	for (tempp = item; tempp; tempp = tempp->next_item)
8717c478bd9Sstevel@tonic-gate 		if (tempp->mib_id == 0)
8727c478bd9Sstevel@tonic-gate 			c++;
8737c478bd9Sstevel@tonic-gate 	tempp = NULL;
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate 	localp = (mib_item_t *)malloc(c * sizeof (mib_item_t));
8767c478bd9Sstevel@tonic-gate 	if (localp == NULL)
8777c478bd9Sstevel@tonic-gate 		return (NULL);
8787c478bd9Sstevel@tonic-gate 	c = 0;
8797c478bd9Sstevel@tonic-gate 	for (; item; item = item->next_item) {
8807c478bd9Sstevel@tonic-gate 		if (item->mib_id == 0) {
8817c478bd9Sstevel@tonic-gate 			/* Replicate item in localp */
8827c478bd9Sstevel@tonic-gate 			(localp[c]).next_item = NULL;
8837c478bd9Sstevel@tonic-gate 			(localp[c]).group = item->group;
8847c478bd9Sstevel@tonic-gate 			(localp[c]).mib_id = item->mib_id;
8857c478bd9Sstevel@tonic-gate 			(localp[c]).length = item->length;
8867c478bd9Sstevel@tonic-gate 			(localp[c]).valp = (uintptr_t *)malloc(
8877c478bd9Sstevel@tonic-gate 			    item->length);
8887c478bd9Sstevel@tonic-gate 			if ((localp[c]).valp == NULL) {
8897c478bd9Sstevel@tonic-gate 				mib_item_destroy(&localp);
8907c478bd9Sstevel@tonic-gate 				return (NULL);
8917c478bd9Sstevel@tonic-gate 			}
8927c478bd9Sstevel@tonic-gate 			(void *) memcpy((localp[c]).valp,
8937c478bd9Sstevel@tonic-gate 			    item->valp,
8947c478bd9Sstevel@tonic-gate 			    item->length);
8957c478bd9Sstevel@tonic-gate 			tempp = &(localp[c]);
8967c478bd9Sstevel@tonic-gate 			if (c > 0)
8977c478bd9Sstevel@tonic-gate 				(localp[c - 1]).next_item = tempp;
8987c478bd9Sstevel@tonic-gate 			c++;
8997c478bd9Sstevel@tonic-gate 		}
9007c478bd9Sstevel@tonic-gate 	}
9017c478bd9Sstevel@tonic-gate 	return (localp);
9027c478bd9Sstevel@tonic-gate }
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate /*
9057c478bd9Sstevel@tonic-gate  * mib_item_diff: takes two (mib_item_t *) linked lists
9067c478bd9Sstevel@tonic-gate  * item1 and item2 and computes the difference between
9077c478bd9Sstevel@tonic-gate  * differentiable values in item2 against item1 for every
9087c478bd9Sstevel@tonic-gate  * given member of item2; returns an mib_item_t * linked
9097c478bd9Sstevel@tonic-gate  * list of diff's, or a copy of item2 if item1 is NULL;
9107c478bd9Sstevel@tonic-gate  * will return NULL if system out of memory; works only
9117c478bd9Sstevel@tonic-gate  * for item->mib_id == 0
9127c478bd9Sstevel@tonic-gate  */
9137c478bd9Sstevel@tonic-gate static mib_item_t *
9147c478bd9Sstevel@tonic-gate mib_item_diff(mib_item_t *item1, mib_item_t *item2) {
9157c478bd9Sstevel@tonic-gate 	int	nitems	= 0; /* no. of items in item2 */
9167c478bd9Sstevel@tonic-gate 	mib_item_t *tempp2;  /* walking copy of item2 */
9177c478bd9Sstevel@tonic-gate 	mib_item_t *tempp1;  /* walking copy of item1 */
9187c478bd9Sstevel@tonic-gate 	mib_item_t *diffp;
9197c478bd9Sstevel@tonic-gate 	mib_item_t *diffptr; /* walking copy of diffp */
9207c478bd9Sstevel@tonic-gate 	mib_item_t *prevp = NULL;
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate 	if (item1 == NULL) {
9237c478bd9Sstevel@tonic-gate 		diffp = mib_item_dup(item2);
9247c478bd9Sstevel@tonic-gate 		return (diffp);
9257c478bd9Sstevel@tonic-gate 	}
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate 	for (tempp2 = item2;
9287c478bd9Sstevel@tonic-gate 	    tempp2;
9297c478bd9Sstevel@tonic-gate 	    tempp2 = tempp2->next_item) {
9307c478bd9Sstevel@tonic-gate 		if (tempp2->mib_id == 0)
9317c478bd9Sstevel@tonic-gate 			switch (tempp2->group) {
9327c478bd9Sstevel@tonic-gate 			/*
9337c478bd9Sstevel@tonic-gate 			 * upon adding a case here, the same
9347c478bd9Sstevel@tonic-gate 			 * must also be added in the next
9357c478bd9Sstevel@tonic-gate 			 * switch statement, alongwith
9367c478bd9Sstevel@tonic-gate 			 * appropriate code
9377c478bd9Sstevel@tonic-gate 			 */
9387c478bd9Sstevel@tonic-gate 			case MIB2_IP:
9397c478bd9Sstevel@tonic-gate 			case MIB2_IP6:
9407c478bd9Sstevel@tonic-gate 			case EXPER_DVMRP:
9417c478bd9Sstevel@tonic-gate 			case EXPER_IGMP:
9427c478bd9Sstevel@tonic-gate 			case MIB2_ICMP:
9437c478bd9Sstevel@tonic-gate 			case MIB2_ICMP6:
9447c478bd9Sstevel@tonic-gate 			case MIB2_TCP:
9457c478bd9Sstevel@tonic-gate 			case MIB2_UDP:
9467c478bd9Sstevel@tonic-gate 			case MIB2_SCTP:
9477c478bd9Sstevel@tonic-gate 			case EXPER_RAWIP:
9487c478bd9Sstevel@tonic-gate 				nitems++;
9497c478bd9Sstevel@tonic-gate 			}
9507c478bd9Sstevel@tonic-gate 	}
9517c478bd9Sstevel@tonic-gate 	tempp2 = NULL;
9527c478bd9Sstevel@tonic-gate 	if (nitems == 0) {
9537c478bd9Sstevel@tonic-gate 		diffp = mib_item_dup(item2);
9547c478bd9Sstevel@tonic-gate 		return (diffp);
9557c478bd9Sstevel@tonic-gate 	}
9567c478bd9Sstevel@tonic-gate 
9577c478bd9Sstevel@tonic-gate 	diffp = (mib_item_t *)calloc(nitems, sizeof (mib_item_t));
9587c478bd9Sstevel@tonic-gate 	if (diffp == NULL)
9597c478bd9Sstevel@tonic-gate 		return (NULL);
9607c478bd9Sstevel@tonic-gate 	diffptr = diffp;
9617c478bd9Sstevel@tonic-gate 	/* 'for' loop 1: */
9627c478bd9Sstevel@tonic-gate 	for (tempp2 = item2; tempp2 != NULL; tempp2 = tempp2->next_item) {
9637c478bd9Sstevel@tonic-gate 		if (tempp2->mib_id != 0)
9647c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
9657c478bd9Sstevel@tonic-gate 		/* 'for' loop 2: */
9667c478bd9Sstevel@tonic-gate 		for (tempp1 = item1; tempp1 != NULL;
9677c478bd9Sstevel@tonic-gate 		    tempp1 = tempp1->next_item) {
9687c478bd9Sstevel@tonic-gate 			if (!(tempp1->mib_id == 0 &&
9697c478bd9Sstevel@tonic-gate 			    tempp1->group == tempp2->group &&
9707c478bd9Sstevel@tonic-gate 			    tempp1->mib_id == tempp2->mib_id))
9717c478bd9Sstevel@tonic-gate 				continue; /* 'for' loop 2 */
9727c478bd9Sstevel@tonic-gate 			/* found comparable data sets */
9737c478bd9Sstevel@tonic-gate 			if (prevp != NULL)
9747c478bd9Sstevel@tonic-gate 				prevp->next_item = diffptr;
9757c478bd9Sstevel@tonic-gate 			switch (tempp2->group) {
9767c478bd9Sstevel@tonic-gate 			/*
9777c478bd9Sstevel@tonic-gate 			 * Indenting note: Because of long variable names
9787c478bd9Sstevel@tonic-gate 			 * in cases MIB2_IP6 and MIB2_ICMP6, their contents
9797c478bd9Sstevel@tonic-gate 			 * have been indented by one tab space only
9807c478bd9Sstevel@tonic-gate 			 */
9817c478bd9Sstevel@tonic-gate 			case MIB2_IP: {
9827c478bd9Sstevel@tonic-gate 				mib2_ip_t *i2 = (mib2_ip_t *)tempp2->valp;
9837c478bd9Sstevel@tonic-gate 				mib2_ip_t *i1 = (mib2_ip_t *)tempp1->valp;
9847c478bd9Sstevel@tonic-gate 				mib2_ip_t *d;
9857c478bd9Sstevel@tonic-gate 
9867c478bd9Sstevel@tonic-gate 				diffptr->group = tempp2->group;
9877c478bd9Sstevel@tonic-gate 				diffptr->mib_id = tempp2->mib_id;
9887c478bd9Sstevel@tonic-gate 				diffptr->length = tempp2->length;
9897c478bd9Sstevel@tonic-gate 				d = (mib2_ip_t *)calloc(tempp2->length, 1);
9907c478bd9Sstevel@tonic-gate 				if (d == NULL)
9917c478bd9Sstevel@tonic-gate 					goto mibdiff_out_of_memory;
9927c478bd9Sstevel@tonic-gate 				diffptr->valp = d;
9937c478bd9Sstevel@tonic-gate 				d->ipForwarding = i2->ipForwarding;
9947c478bd9Sstevel@tonic-gate 				d->ipDefaultTTL = i2->ipDefaultTTL;
9957c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipInReceives);
9967c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipInHdrErrors);
9977c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipInAddrErrors);
9987c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipInCksumErrs);
9997c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipForwDatagrams);
10007c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipForwProhibits);
10017c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipInUnknownProtos);
10027c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipInDiscards);
10037c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipInDelivers);
10047c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipOutRequests);
10057c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipOutDiscards);
10067c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipOutNoRoutes);
10077c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipReasmTimeout);
10087c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipReasmReqds);
10097c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipReasmOKs);
10107c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipReasmFails);
10117c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipReasmDuplicates);
10127c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipReasmPartDups);
10137c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipFragOKs);
10147c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipFragFails);
10157c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipFragCreates);
10167c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipRoutingDiscards);
10177c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, tcpInErrs);
10187c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, udpNoPorts);
10197c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, udpInCksumErrs);
10207c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, udpInOverflows);
10217c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, rawipInOverflows);
10227c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipsecInSucceeded);
10237c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipsecInFailed);
10247c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipInIPv6);
10257c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipOutIPv6);
10267c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipOutSwitchIPv6);
10277c478bd9Sstevel@tonic-gate 				prevp = diffptr++;
10287c478bd9Sstevel@tonic-gate 				break;
10297c478bd9Sstevel@tonic-gate 			}
10307c478bd9Sstevel@tonic-gate 			case MIB2_IP6: {
10317c478bd9Sstevel@tonic-gate 			mib2_ipv6IfStatsEntry_t *i2;
10327c478bd9Sstevel@tonic-gate 			mib2_ipv6IfStatsEntry_t *i1;
10337c478bd9Sstevel@tonic-gate 			mib2_ipv6IfStatsEntry_t *d;
10347c478bd9Sstevel@tonic-gate 
10357c478bd9Sstevel@tonic-gate 			i2 = (mib2_ipv6IfStatsEntry_t *)tempp2->valp;
10367c478bd9Sstevel@tonic-gate 			i1 = (mib2_ipv6IfStatsEntry_t *)tempp1->valp;
10377c478bd9Sstevel@tonic-gate 			diffptr->group = tempp2->group;
10387c478bd9Sstevel@tonic-gate 			diffptr->mib_id = tempp2->mib_id;
10397c478bd9Sstevel@tonic-gate 			diffptr->length = tempp2->length;
10407c478bd9Sstevel@tonic-gate 			d = (mib2_ipv6IfStatsEntry_t *)calloc(
10417c478bd9Sstevel@tonic-gate 			    tempp2->length, 1);
10427c478bd9Sstevel@tonic-gate 			if (d == NULL)
10437c478bd9Sstevel@tonic-gate 				goto mibdiff_out_of_memory;
10447c478bd9Sstevel@tonic-gate 			diffptr->valp = d;
10457c478bd9Sstevel@tonic-gate 			d->ipv6Forwarding = i2->ipv6Forwarding;
10467c478bd9Sstevel@tonic-gate 			d->ipv6DefaultHopLimit =
10477c478bd9Sstevel@tonic-gate 			    i2->ipv6DefaultHopLimit;
10487c478bd9Sstevel@tonic-gate 
10497c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6InReceives);
10507c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6InHdrErrors);
10517c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6InTooBigErrors);
10527c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6InNoRoutes);
10537c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6InAddrErrors);
10547c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6InUnknownProtos);
10557c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6InTruncatedPkts);
10567c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6InDiscards);
10577c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6InDelivers);
10587c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6OutForwDatagrams);
10597c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6OutRequests);
10607c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6OutDiscards);
10617c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6OutNoRoutes);
10627c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6OutFragOKs);
10637c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6OutFragFails);
10647c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6OutFragCreates);
10657c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6ReasmReqds);
10667c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6ReasmOKs);
10677c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6ReasmFails);
10687c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6InMcastPkts);
10697c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6OutMcastPkts);
10707c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6ReasmDuplicates);
10717c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6ReasmPartDups);
10727c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6ForwProhibits);
10737c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, udpInCksumErrs);
10747c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, udpInOverflows);
10757c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, rawipInOverflows);
10767c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6InIPv4);
10777c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6OutIPv4);
10787c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6OutSwitchIPv4);
10797c478bd9Sstevel@tonic-gate 			prevp = diffptr++;
10807c478bd9Sstevel@tonic-gate 			break;
10817c478bd9Sstevel@tonic-gate 			}
10827c478bd9Sstevel@tonic-gate 			case EXPER_DVMRP: {
10837c478bd9Sstevel@tonic-gate 				struct mrtstat *m2;
10847c478bd9Sstevel@tonic-gate 				struct mrtstat *m1;
10857c478bd9Sstevel@tonic-gate 				struct mrtstat *d;
10867c478bd9Sstevel@tonic-gate 
10877c478bd9Sstevel@tonic-gate 				m2 = (struct mrtstat *)tempp2->valp;
10887c478bd9Sstevel@tonic-gate 				m1 = (struct mrtstat *)tempp1->valp;
10897c478bd9Sstevel@tonic-gate 				diffptr->group = tempp2->group;
10907c478bd9Sstevel@tonic-gate 				diffptr->mib_id = tempp2->mib_id;
10917c478bd9Sstevel@tonic-gate 				diffptr->length = tempp2->length;
10927c478bd9Sstevel@tonic-gate 				d = (struct mrtstat *)calloc(tempp2->length, 1);
10937c478bd9Sstevel@tonic-gate 				if (d == NULL)
10947c478bd9Sstevel@tonic-gate 					goto mibdiff_out_of_memory;
10957c478bd9Sstevel@tonic-gate 				diffptr->valp = d;
10967c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_mfc_hits);
10977c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_mfc_misses);
10987c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_fwd_in);
10997c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_fwd_out);
11007c478bd9Sstevel@tonic-gate 				d->mrts_upcalls = m2->mrts_upcalls;
11017c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_fwd_drop);
11027c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_bad_tunnel);
11037c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_cant_tunnel);
11047c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_wrong_if);
11057c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_upq_ovflw);
11067c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_cache_cleanups);
11077c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_drop_sel);
11087c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_q_overflow);
11097c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_pkt2large);
11107c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_pim_badversion);
11117c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_pim_rcv_badcsum);
11127c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_pim_badregisters);
11137c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_pim_regforwards);
11147c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_pim_regsend_drops);
11157c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_pim_malformed);
11167c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_pim_nomemory);
11177c478bd9Sstevel@tonic-gate 				prevp = diffptr++;
11187c478bd9Sstevel@tonic-gate 				break;
11197c478bd9Sstevel@tonic-gate 			}
11207c478bd9Sstevel@tonic-gate 			case EXPER_IGMP: {
11217c478bd9Sstevel@tonic-gate 				struct igmpstat *i2;
11227c478bd9Sstevel@tonic-gate 				struct igmpstat *i1;
11237c478bd9Sstevel@tonic-gate 				struct igmpstat *d;
11247c478bd9Sstevel@tonic-gate 
11257c478bd9Sstevel@tonic-gate 				i2 = (struct igmpstat *)tempp2->valp;
11267c478bd9Sstevel@tonic-gate 				i1 = (struct igmpstat *)tempp1->valp;
11277c478bd9Sstevel@tonic-gate 				diffptr->group = tempp2->group;
11287c478bd9Sstevel@tonic-gate 				diffptr->mib_id = tempp2->mib_id;
11297c478bd9Sstevel@tonic-gate 				diffptr->length = tempp2->length;
11307c478bd9Sstevel@tonic-gate 				d = (struct igmpstat *)calloc(
11317c478bd9Sstevel@tonic-gate 				    tempp2->length, 1);
11327c478bd9Sstevel@tonic-gate 				if (d == NULL)
11337c478bd9Sstevel@tonic-gate 					goto mibdiff_out_of_memory;
11347c478bd9Sstevel@tonic-gate 				diffptr->valp = d;
11357c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, igps_rcv_total);
11367c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, igps_rcv_tooshort);
11377c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, igps_rcv_badsum);
11387c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, igps_rcv_queries);
11397c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, igps_rcv_badqueries);
11407c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, igps_rcv_reports);
11417c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, igps_rcv_badreports);
11427c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, igps_rcv_ourreports);
11437c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, igps_snd_reports);
11447c478bd9Sstevel@tonic-gate 				prevp = diffptr++;
11457c478bd9Sstevel@tonic-gate 				break;
11467c478bd9Sstevel@tonic-gate 			}
11477c478bd9Sstevel@tonic-gate 			case MIB2_ICMP: {
11487c478bd9Sstevel@tonic-gate 				mib2_icmp_t *i2;
11497c478bd9Sstevel@tonic-gate 				mib2_icmp_t *i1;
11507c478bd9Sstevel@tonic-gate 				mib2_icmp_t *d;
11517c478bd9Sstevel@tonic-gate 
11527c478bd9Sstevel@tonic-gate 				i2 = (mib2_icmp_t *)tempp2->valp;
11537c478bd9Sstevel@tonic-gate 				i1 = (mib2_icmp_t *)tempp1->valp;
11547c478bd9Sstevel@tonic-gate 				diffptr->group = tempp2->group;
11557c478bd9Sstevel@tonic-gate 				diffptr->mib_id = tempp2->mib_id;
11567c478bd9Sstevel@tonic-gate 				diffptr->length = tempp2->length;
11577c478bd9Sstevel@tonic-gate 				d = (mib2_icmp_t *)calloc(tempp2->length, 1);
11587c478bd9Sstevel@tonic-gate 				if (d == NULL)
11597c478bd9Sstevel@tonic-gate 					goto mibdiff_out_of_memory;
11607c478bd9Sstevel@tonic-gate 				diffptr->valp = d;
11617c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInMsgs);
11627c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInErrors);
11637c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInCksumErrs);
11647c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInUnknowns);
11657c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInDestUnreachs);
11667c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInTimeExcds);
11677c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInParmProbs);
11687c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInSrcQuenchs);
11697c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInRedirects);
11707c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInBadRedirects);
11717c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInEchos);
11727c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInEchoReps);
11737c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInTimestamps);
11747c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInAddrMasks);
11757c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInAddrMaskReps);
11767c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInFragNeeded);
11777c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutMsgs);
11787c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutDrops);
11797c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutErrors);
11807c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutDestUnreachs);
11817c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutTimeExcds);
11827c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutParmProbs);
11837c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutSrcQuenchs);
11847c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutRedirects);
11857c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutEchos);
11867c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutEchoReps);
11877c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutTimestamps);
11887c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutTimestampReps);
11897c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutAddrMasks);
11907c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutAddrMaskReps);
11917c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutFragNeeded);
11927c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInOverflows);
11937c478bd9Sstevel@tonic-gate 				prevp = diffptr++;
11947c478bd9Sstevel@tonic-gate 				break;
11957c478bd9Sstevel@tonic-gate 			}
11967c478bd9Sstevel@tonic-gate 			case MIB2_ICMP6: {
11977c478bd9Sstevel@tonic-gate 	mib2_ipv6IfIcmpEntry_t *i2;
11987c478bd9Sstevel@tonic-gate 	mib2_ipv6IfIcmpEntry_t *i1;
11997c478bd9Sstevel@tonic-gate 	mib2_ipv6IfIcmpEntry_t *d;
12007c478bd9Sstevel@tonic-gate 
12017c478bd9Sstevel@tonic-gate 	i2 = (mib2_ipv6IfIcmpEntry_t *)tempp2->valp;
12027c478bd9Sstevel@tonic-gate 	i1 = (mib2_ipv6IfIcmpEntry_t *)tempp1->valp;
12037c478bd9Sstevel@tonic-gate 	diffptr->group = tempp2->group;
12047c478bd9Sstevel@tonic-gate 	diffptr->mib_id = tempp2->mib_id;
12057c478bd9Sstevel@tonic-gate 	diffptr->length = tempp2->length;
12067c478bd9Sstevel@tonic-gate 	d = (mib2_ipv6IfIcmpEntry_t *)calloc(tempp2->length, 1);
12077c478bd9Sstevel@tonic-gate 	if (d == NULL)
12087c478bd9Sstevel@tonic-gate 		goto mibdiff_out_of_memory;
12097c478bd9Sstevel@tonic-gate 	diffptr->valp = d;
12107c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInMsgs);
12117c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInErrors);
12127c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInDestUnreachs);
12137c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInAdminProhibs);
12147c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInTimeExcds);
12157c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInParmProblems);
12167c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInPktTooBigs);
12177c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInEchos);
12187c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInEchoReplies);
12197c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInRouterSolicits);
12207c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInRouterAdvertisements);
12217c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInNeighborSolicits);
12227c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInNeighborAdvertisements);
12237c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInRedirects);
12247c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInBadRedirects);
12257c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInGroupMembQueries);
12267c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInGroupMembResponses);
12277c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInGroupMembReductions);
12287c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInOverflows);
12297c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutMsgs);
12307c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutErrors);
12317c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutDestUnreachs);
12327c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutAdminProhibs);
12337c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutTimeExcds);
12347c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutParmProblems);
12357c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutPktTooBigs);
12367c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutEchos);
12377c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutEchoReplies);
12387c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutRouterSolicits);
12397c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutRouterAdvertisements);
12407c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutNeighborSolicits);
12417c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutNeighborAdvertisements);
12427c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutRedirects);
12437c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutGroupMembQueries);
12447c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutGroupMembResponses);
12457c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutGroupMembReductions);
12467c478bd9Sstevel@tonic-gate 	prevp = diffptr++;
12477c478bd9Sstevel@tonic-gate 	break;
12487c478bd9Sstevel@tonic-gate 			}
12497c478bd9Sstevel@tonic-gate 			case MIB2_TCP: {
12507c478bd9Sstevel@tonic-gate 				mib2_tcp_t *t2;
12517c478bd9Sstevel@tonic-gate 				mib2_tcp_t *t1;
12527c478bd9Sstevel@tonic-gate 				mib2_tcp_t *d;
12537c478bd9Sstevel@tonic-gate 
12547c478bd9Sstevel@tonic-gate 				t2 = (mib2_tcp_t *)tempp2->valp;
12557c478bd9Sstevel@tonic-gate 				t1 = (mib2_tcp_t *)tempp1->valp;
12567c478bd9Sstevel@tonic-gate 				diffptr->group = tempp2->group;
12577c478bd9Sstevel@tonic-gate 				diffptr->mib_id = tempp2->mib_id;
12587c478bd9Sstevel@tonic-gate 				diffptr->length = tempp2->length;
12597c478bd9Sstevel@tonic-gate 				d = (mib2_tcp_t *)calloc(tempp2->length, 1);
12607c478bd9Sstevel@tonic-gate 				if (d == NULL)
12617c478bd9Sstevel@tonic-gate 					goto mibdiff_out_of_memory;
12627c478bd9Sstevel@tonic-gate 				diffptr->valp = d;
12637c478bd9Sstevel@tonic-gate 				d->tcpRtoMin = t2->tcpRtoMin;
12647c478bd9Sstevel@tonic-gate 				d->tcpRtoMax = t2->tcpRtoMax;
12657c478bd9Sstevel@tonic-gate 				d->tcpMaxConn = t2->tcpMaxConn;
12667c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpActiveOpens);
12677c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpPassiveOpens);
12687c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpAttemptFails);
12697c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpEstabResets);
12707c478bd9Sstevel@tonic-gate 				d->tcpCurrEstab = t2->tcpCurrEstab;
12717c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpOutSegs);
12727c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpOutDataSegs);
12737c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpOutDataBytes);
12747c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpRetransSegs);
12757c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpRetransBytes);
12767c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpOutAck);
12777c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpOutAckDelayed);
12787c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpOutUrg);
12797c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpOutWinUpdate);
12807c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpOutWinProbe);
12817c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpOutControl);
12827c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpOutRsts);
12837c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpOutFastRetrans);
12847c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInSegs);
12857c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInAckSegs);
12867c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInAckBytes);
12877c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInDupAck);
12887c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInAckUnsent);
12897c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInDataInorderSegs);
12907c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInDataInorderBytes);
12917c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInDataUnorderSegs);
12927c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInDataUnorderBytes);
12937c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInDataDupSegs);
12947c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInDataDupBytes);
12957c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInDataPartDupSegs);
12967c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInDataPartDupBytes);
12977c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInDataPastWinSegs);
12987c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInDataPastWinBytes);
12997c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInWinProbe);
13007c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInWinUpdate);
13017c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInClosed);
13027c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpRttNoUpdate);
13037c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpRttUpdate);
13047c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpTimRetrans);
13057c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpTimRetransDrop);
13067c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpTimKeepalive);
13077c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpTimKeepaliveProbe);
13087c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpTimKeepaliveDrop);
13097c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpListenDrop);
13107c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpListenDropQ0);
13117c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpHalfOpenDrop);
13127c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpOutSackRetransSegs);
13137c478bd9Sstevel@tonic-gate 				prevp = diffptr++;
13147c478bd9Sstevel@tonic-gate 				break;
13157c478bd9Sstevel@tonic-gate 			}
13167c478bd9Sstevel@tonic-gate 			case MIB2_UDP: {
13177c478bd9Sstevel@tonic-gate 				mib2_udp_t *u2;
13187c478bd9Sstevel@tonic-gate 				mib2_udp_t *u1;
13197c478bd9Sstevel@tonic-gate 				mib2_udp_t *d;
13207c478bd9Sstevel@tonic-gate 
13217c478bd9Sstevel@tonic-gate 				u2 = (mib2_udp_t *)tempp2->valp;
13227c478bd9Sstevel@tonic-gate 				u1 = (mib2_udp_t *)tempp1->valp;
13237c478bd9Sstevel@tonic-gate 				diffptr->group = tempp2->group;
13247c478bd9Sstevel@tonic-gate 				diffptr->mib_id = tempp2->mib_id;
13257c478bd9Sstevel@tonic-gate 				diffptr->length = tempp2->length;
13267c478bd9Sstevel@tonic-gate 				d = (mib2_udp_t *)calloc(tempp2->length, 1);
13277c478bd9Sstevel@tonic-gate 				if (d == NULL)
13287c478bd9Sstevel@tonic-gate 					goto mibdiff_out_of_memory;
13297c478bd9Sstevel@tonic-gate 				diffptr->valp = d;
13307c478bd9Sstevel@tonic-gate 				MDIFF(d, u2, u1, udpInDatagrams);
13317c478bd9Sstevel@tonic-gate 				MDIFF(d, u2, u1, udpInErrors);
13327c478bd9Sstevel@tonic-gate 				MDIFF(d, u2, u1, udpOutDatagrams);
13337c478bd9Sstevel@tonic-gate 				MDIFF(d, u2, u1, udpOutErrors);
13347c478bd9Sstevel@tonic-gate 				prevp = diffptr++;
13357c478bd9Sstevel@tonic-gate 				break;
13367c478bd9Sstevel@tonic-gate 			}
13377c478bd9Sstevel@tonic-gate 			case MIB2_SCTP: {
13387c478bd9Sstevel@tonic-gate 				mib2_sctp_t *s2;
13397c478bd9Sstevel@tonic-gate 				mib2_sctp_t *s1;
13407c478bd9Sstevel@tonic-gate 				mib2_sctp_t *d;
13417c478bd9Sstevel@tonic-gate 
13427c478bd9Sstevel@tonic-gate 				s2 = (mib2_sctp_t *)tempp2->valp;
13437c478bd9Sstevel@tonic-gate 				s1 = (mib2_sctp_t *)tempp1->valp;
13447c478bd9Sstevel@tonic-gate 				diffptr->group = tempp2->group;
13457c478bd9Sstevel@tonic-gate 				diffptr->mib_id = tempp2->mib_id;
13467c478bd9Sstevel@tonic-gate 				diffptr->length = tempp2->length;
13477c478bd9Sstevel@tonic-gate 				d = (mib2_sctp_t *)calloc(tempp2->length, 1);
13487c478bd9Sstevel@tonic-gate 				if (d == NULL)
13497c478bd9Sstevel@tonic-gate 					goto mibdiff_out_of_memory;
13507c478bd9Sstevel@tonic-gate 				diffptr->valp = d;
13517c478bd9Sstevel@tonic-gate 				d->sctpRtoAlgorithm = s2->sctpRtoAlgorithm;
13527c478bd9Sstevel@tonic-gate 				d->sctpRtoMin = s2->sctpRtoMin;
13537c478bd9Sstevel@tonic-gate 				d->sctpRtoMax = s2->sctpRtoMax;
13547c478bd9Sstevel@tonic-gate 				d->sctpRtoInitial = s2->sctpRtoInitial;
13557c478bd9Sstevel@tonic-gate 				d->sctpMaxAssocs = s2->sctpMaxAssocs;
13567c478bd9Sstevel@tonic-gate 				d->sctpValCookieLife = s2->sctpValCookieLife;
13577c478bd9Sstevel@tonic-gate 				d->sctpMaxInitRetr = s2->sctpMaxInitRetr;
13587c478bd9Sstevel@tonic-gate 				d->sctpCurrEstab = s2->sctpCurrEstab;
13597c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpActiveEstab);
13607c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpPassiveEstab);
13617c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpAborted);
13627c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpShutdowns);
13637c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpOutOfBlue);
13647c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpChecksumError);
13657c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpOutCtrlChunks);
13667c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpOutOrderChunks);
13677c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpOutUnorderChunks);
13687c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpRetransChunks);
13697c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpOutAck);
13707c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpOutAckDelayed);
13717c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpOutWinUpdate);
13727c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpOutFastRetrans);
13737c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpOutWinProbe);
13747c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpInCtrlChunks);
13757c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpInOrderChunks);
13767c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpInUnorderChunks);
13777c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpInAck);
13787c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpInDupAck);
13797c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpInAckUnsent);
13807c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpFragUsrMsgs);
13817c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpReasmUsrMsgs);
13827c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpOutSCTPPkts);
13837c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpInSCTPPkts);
13847c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpInInvalidCookie);
13857c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpTimRetrans);
13867c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpTimRetransDrop);
13877c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpTimHeartBeatProbe);
13887c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpTimHeartBeatDrop);
13897c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpListenDrop);
13907c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpInClosed);
13917c478bd9Sstevel@tonic-gate 				prevp = diffptr++;
13927c478bd9Sstevel@tonic-gate 				break;
13937c478bd9Sstevel@tonic-gate 			}
13947c478bd9Sstevel@tonic-gate 			case EXPER_RAWIP: {
13957c478bd9Sstevel@tonic-gate 				mib2_rawip_t *r2;
13967c478bd9Sstevel@tonic-gate 				mib2_rawip_t *r1;
13977c478bd9Sstevel@tonic-gate 				mib2_rawip_t *d;
13987c478bd9Sstevel@tonic-gate 
13997c478bd9Sstevel@tonic-gate 				r2 = (mib2_rawip_t *)tempp2->valp;
14007c478bd9Sstevel@tonic-gate 				r1 = (mib2_rawip_t *)tempp1->valp;
14017c478bd9Sstevel@tonic-gate 				diffptr->group = tempp2->group;
14027c478bd9Sstevel@tonic-gate 				diffptr->mib_id = tempp2->mib_id;
14037c478bd9Sstevel@tonic-gate 				diffptr->length = tempp2->length;
14047c478bd9Sstevel@tonic-gate 				d = (mib2_rawip_t *)calloc(tempp2->length, 1);
14057c478bd9Sstevel@tonic-gate 				if (d == NULL)
14067c478bd9Sstevel@tonic-gate 					goto mibdiff_out_of_memory;
14077c478bd9Sstevel@tonic-gate 				diffptr->valp = d;
14087c478bd9Sstevel@tonic-gate 				MDIFF(d, r2, r1, rawipInDatagrams);
14097c478bd9Sstevel@tonic-gate 				MDIFF(d, r2, r1, rawipInErrors);
14107c478bd9Sstevel@tonic-gate 				MDIFF(d, r2, r1, rawipInCksumErrs);
14117c478bd9Sstevel@tonic-gate 				MDIFF(d, r2, r1, rawipOutDatagrams);
14127c478bd9Sstevel@tonic-gate 				MDIFF(d, r2, r1, rawipOutErrors);
14137c478bd9Sstevel@tonic-gate 				prevp = diffptr++;
14147c478bd9Sstevel@tonic-gate 				break;
14157c478bd9Sstevel@tonic-gate 			}
14167c478bd9Sstevel@tonic-gate 			/*
14177c478bd9Sstevel@tonic-gate 			 * there are more "group" types but they aren't
14187c478bd9Sstevel@tonic-gate 			 * required for the -s and -Ms options
14197c478bd9Sstevel@tonic-gate 			 */
14207c478bd9Sstevel@tonic-gate 			}
14217c478bd9Sstevel@tonic-gate 		} /* 'for' loop 2 ends */
14227c478bd9Sstevel@tonic-gate 		tempp1 = NULL;
14237c478bd9Sstevel@tonic-gate 	} /* 'for' loop 1 ends */
14247c478bd9Sstevel@tonic-gate 	tempp2 = NULL;
14257c478bd9Sstevel@tonic-gate 	diffptr--;
14267c478bd9Sstevel@tonic-gate 	diffptr->next_item = NULL;
14277c478bd9Sstevel@tonic-gate 	return (diffp);
14287c478bd9Sstevel@tonic-gate 
14297c478bd9Sstevel@tonic-gate mibdiff_out_of_memory:;
14307c478bd9Sstevel@tonic-gate 	mib_item_destroy(&diffp);
14317c478bd9Sstevel@tonic-gate 	return (NULL);
14327c478bd9Sstevel@tonic-gate }
14337c478bd9Sstevel@tonic-gate 
14347c478bd9Sstevel@tonic-gate /*
14357c478bd9Sstevel@tonic-gate  * mib_item_destroy: cleans up a mib_item_t *
14367c478bd9Sstevel@tonic-gate  * that was created by calling mib_item_dup or
14377c478bd9Sstevel@tonic-gate  * mib_item_diff
14387c478bd9Sstevel@tonic-gate  */
14397c478bd9Sstevel@tonic-gate static void
14407c478bd9Sstevel@tonic-gate mib_item_destroy(mib_item_t **itemp) {
14417c478bd9Sstevel@tonic-gate 	int	nitems = 0;
14427c478bd9Sstevel@tonic-gate 	int	c = 0;
14437c478bd9Sstevel@tonic-gate 	mib_item_t *tempp;
14447c478bd9Sstevel@tonic-gate 
14457c478bd9Sstevel@tonic-gate 	if (itemp == NULL || *itemp == NULL)
14467c478bd9Sstevel@tonic-gate 		return;
14477c478bd9Sstevel@tonic-gate 
14487c478bd9Sstevel@tonic-gate 	for (tempp = *itemp; tempp != NULL; tempp = tempp->next_item)
14497c478bd9Sstevel@tonic-gate 		if (tempp->mib_id == 0)
14507c478bd9Sstevel@tonic-gate 			nitems++;
14517c478bd9Sstevel@tonic-gate 		else
14527c478bd9Sstevel@tonic-gate 			return;	/* cannot destroy! */
14537c478bd9Sstevel@tonic-gate 
14547c478bd9Sstevel@tonic-gate 	if (nitems == 0)
14557c478bd9Sstevel@tonic-gate 		return;		/* cannot destroy! */
14567c478bd9Sstevel@tonic-gate 
14577c478bd9Sstevel@tonic-gate 	for (c = nitems - 1; c >= 0; c--) {
14587c478bd9Sstevel@tonic-gate 		if ((itemp[0][c]).valp != NULL)
14597c478bd9Sstevel@tonic-gate 			free((itemp[0][c]).valp);
14607c478bd9Sstevel@tonic-gate 	}
14617c478bd9Sstevel@tonic-gate 	free(*itemp);
14627c478bd9Sstevel@tonic-gate 
14637c478bd9Sstevel@tonic-gate 	*itemp = NULL;
14647c478bd9Sstevel@tonic-gate }
14657c478bd9Sstevel@tonic-gate 
14667c478bd9Sstevel@tonic-gate /* Compare two Octet_ts.  Return B_TRUE if they match, B_FALSE if not. */
14677c478bd9Sstevel@tonic-gate static boolean_t
14687c478bd9Sstevel@tonic-gate octetstrmatch(const Octet_t *a, const Octet_t *b)
14697c478bd9Sstevel@tonic-gate {
14707c478bd9Sstevel@tonic-gate 	if (a == NULL || b == NULL)
14717c478bd9Sstevel@tonic-gate 		return (B_FALSE);
14727c478bd9Sstevel@tonic-gate 
14737c478bd9Sstevel@tonic-gate 	if (a->o_length != b->o_length)
14747c478bd9Sstevel@tonic-gate 		return (B_FALSE);
14757c478bd9Sstevel@tonic-gate 
14767c478bd9Sstevel@tonic-gate 	return (memcmp(a->o_bytes, b->o_bytes, a->o_length) == 0);
14777c478bd9Sstevel@tonic-gate }
14787c478bd9Sstevel@tonic-gate 
14797c478bd9Sstevel@tonic-gate /* If octetstr() changes make an appropriate change to STR_EXPAND */
14807c478bd9Sstevel@tonic-gate static char *
1481*45916cd2Sjpk octetstr(const Octet_t *op, int code, char *dst, uint_t dstlen)
14827c478bd9Sstevel@tonic-gate {
14837c478bd9Sstevel@tonic-gate 	int	i;
14847c478bd9Sstevel@tonic-gate 	char	*cp;
14857c478bd9Sstevel@tonic-gate 
14867c478bd9Sstevel@tonic-gate 	cp = dst;
14877c478bd9Sstevel@tonic-gate 	if (op) {
14887c478bd9Sstevel@tonic-gate 		for (i = 0; i < op->o_length; i++) {
14897c478bd9Sstevel@tonic-gate 			switch (code) {
14907c478bd9Sstevel@tonic-gate 			case 'd':
14917c478bd9Sstevel@tonic-gate 				if (cp - dst + 4 > dstlen) {
14927c478bd9Sstevel@tonic-gate 					*cp = '\0';
14937c478bd9Sstevel@tonic-gate 					return (dst);
14947c478bd9Sstevel@tonic-gate 				}
14957c478bd9Sstevel@tonic-gate 				(void) snprintf(cp, 5, "%d.",
14967c478bd9Sstevel@tonic-gate 				    0xff & op->o_bytes[i]);
14977c478bd9Sstevel@tonic-gate 				cp = strchr(cp, '\0');
14987c478bd9Sstevel@tonic-gate 				break;
14997c478bd9Sstevel@tonic-gate 			case 'a':
15007c478bd9Sstevel@tonic-gate 				if (cp - dst + 1 > dstlen) {
15017c478bd9Sstevel@tonic-gate 					*cp = '\0';
15027c478bd9Sstevel@tonic-gate 					return (dst);
15037c478bd9Sstevel@tonic-gate 				}
15047c478bd9Sstevel@tonic-gate 				*cp++ = op->o_bytes[i];
15057c478bd9Sstevel@tonic-gate 				break;
15067c478bd9Sstevel@tonic-gate 			case 'h':
15077c478bd9Sstevel@tonic-gate 			default:
15087c478bd9Sstevel@tonic-gate 				if (cp - dst + 3 > dstlen) {
15097c478bd9Sstevel@tonic-gate 					*cp = '\0';
15107c478bd9Sstevel@tonic-gate 					return (dst);
15117c478bd9Sstevel@tonic-gate 				}
15127c478bd9Sstevel@tonic-gate 				(void) snprintf(cp, 4, "%02x:",
15137c478bd9Sstevel@tonic-gate 				    0xff & op->o_bytes[i]);
15147c478bd9Sstevel@tonic-gate 				cp += 3;
15157c478bd9Sstevel@tonic-gate 				break;
15167c478bd9Sstevel@tonic-gate 			}
15177c478bd9Sstevel@tonic-gate 		}
15187c478bd9Sstevel@tonic-gate 	}
15197c478bd9Sstevel@tonic-gate 	if (code != 'a' && cp != dst)
15207c478bd9Sstevel@tonic-gate 		cp--;
15217c478bd9Sstevel@tonic-gate 	*cp = '\0';
15227c478bd9Sstevel@tonic-gate 	return (dst);
15237c478bd9Sstevel@tonic-gate }
15247c478bd9Sstevel@tonic-gate 
1525*45916cd2Sjpk static const char *
1526*45916cd2Sjpk mitcp_state(int state, const mib2_transportMLPEntry_t *attr)
15277c478bd9Sstevel@tonic-gate {
1528*45916cd2Sjpk 	static char tcpsbuf[50];
1529*45916cd2Sjpk 	const char *cp;
15307c478bd9Sstevel@tonic-gate 
15317c478bd9Sstevel@tonic-gate 	switch (state) {
15327c478bd9Sstevel@tonic-gate 	case TCPS_CLOSED:
15337c478bd9Sstevel@tonic-gate 		cp = "CLOSED";
15347c478bd9Sstevel@tonic-gate 		break;
15357c478bd9Sstevel@tonic-gate 	case TCPS_IDLE:
15367c478bd9Sstevel@tonic-gate 		cp = "IDLE";
15377c478bd9Sstevel@tonic-gate 		break;
15387c478bd9Sstevel@tonic-gate 	case TCPS_BOUND:
15397c478bd9Sstevel@tonic-gate 		cp = "BOUND";
15407c478bd9Sstevel@tonic-gate 		break;
15417c478bd9Sstevel@tonic-gate 	case TCPS_LISTEN:
15427c478bd9Sstevel@tonic-gate 		cp = "LISTEN";
15437c478bd9Sstevel@tonic-gate 		break;
15447c478bd9Sstevel@tonic-gate 	case TCPS_SYN_SENT:
15457c478bd9Sstevel@tonic-gate 		cp = "SYN_SENT";
15467c478bd9Sstevel@tonic-gate 		break;
15477c478bd9Sstevel@tonic-gate 	case TCPS_SYN_RCVD:
15487c478bd9Sstevel@tonic-gate 		cp = "SYN_RCVD";
15497c478bd9Sstevel@tonic-gate 		break;
15507c478bd9Sstevel@tonic-gate 	case TCPS_ESTABLISHED:
15517c478bd9Sstevel@tonic-gate 		cp = "ESTABLISHED";
15527c478bd9Sstevel@tonic-gate 		break;
15537c478bd9Sstevel@tonic-gate 	case TCPS_CLOSE_WAIT:
15547c478bd9Sstevel@tonic-gate 		cp = "CLOSE_WAIT";
15557c478bd9Sstevel@tonic-gate 		break;
15567c478bd9Sstevel@tonic-gate 	case TCPS_FIN_WAIT_1:
15577c478bd9Sstevel@tonic-gate 		cp = "FIN_WAIT_1";
15587c478bd9Sstevel@tonic-gate 		break;
15597c478bd9Sstevel@tonic-gate 	case TCPS_CLOSING:
15607c478bd9Sstevel@tonic-gate 		cp = "CLOSING";
15617c478bd9Sstevel@tonic-gate 		break;
15627c478bd9Sstevel@tonic-gate 	case TCPS_LAST_ACK:
15637c478bd9Sstevel@tonic-gate 		cp = "LAST_ACK";
15647c478bd9Sstevel@tonic-gate 		break;
15657c478bd9Sstevel@tonic-gate 	case TCPS_FIN_WAIT_2:
15667c478bd9Sstevel@tonic-gate 		cp = "FIN_WAIT_2";
15677c478bd9Sstevel@tonic-gate 		break;
15687c478bd9Sstevel@tonic-gate 	case TCPS_TIME_WAIT:
15697c478bd9Sstevel@tonic-gate 		cp = "TIME_WAIT";
15707c478bd9Sstevel@tonic-gate 		break;
15717c478bd9Sstevel@tonic-gate 	default:
15727c478bd9Sstevel@tonic-gate 		(void) snprintf(tcpsbuf, sizeof (tcpsbuf),
15737c478bd9Sstevel@tonic-gate 		    "UnknownState(%d)", state);
15747c478bd9Sstevel@tonic-gate 		cp = tcpsbuf;
15757c478bd9Sstevel@tonic-gate 		break;
15767c478bd9Sstevel@tonic-gate 	}
1577*45916cd2Sjpk 
1578*45916cd2Sjpk 	if (RSECflag && attr != NULL && attr->tme_flags != 0) {
1579*45916cd2Sjpk 		if (cp != tcpsbuf) {
1580*45916cd2Sjpk 			(void) strlcpy(tcpsbuf, cp, sizeof (tcpsbuf));
1581*45916cd2Sjpk 			cp = tcpsbuf;
1582*45916cd2Sjpk 		}
1583*45916cd2Sjpk 		if (attr->tme_flags & MIB2_TMEF_PRIVATE)
1584*45916cd2Sjpk 			(void) strlcat(tcpsbuf, " P", sizeof (tcpsbuf));
1585*45916cd2Sjpk 		if (attr->tme_flags & MIB2_TMEF_SHARED)
1586*45916cd2Sjpk 			(void) strlcat(tcpsbuf, " S", sizeof (tcpsbuf));
1587*45916cd2Sjpk 	}
1588*45916cd2Sjpk 
1589*45916cd2Sjpk 	return (cp);
1590*45916cd2Sjpk }
1591*45916cd2Sjpk 
1592*45916cd2Sjpk static const char *
1593*45916cd2Sjpk miudp_state(int state, const mib2_transportMLPEntry_t *attr)
1594*45916cd2Sjpk {
1595*45916cd2Sjpk 	static char udpsbuf[50];
1596*45916cd2Sjpk 	const char *cp;
1597*45916cd2Sjpk 
1598*45916cd2Sjpk 	switch (state) {
1599*45916cd2Sjpk 	case MIB2_UDP_unbound:
1600*45916cd2Sjpk 		cp = "Unbound";
1601*45916cd2Sjpk 		break;
1602*45916cd2Sjpk 	case MIB2_UDP_idle:
1603*45916cd2Sjpk 		cp = "Idle";
1604*45916cd2Sjpk 		break;
1605*45916cd2Sjpk 	case MIB2_UDP_connected:
1606*45916cd2Sjpk 		cp = "Connected";
1607*45916cd2Sjpk 		break;
1608*45916cd2Sjpk 	default:
1609*45916cd2Sjpk 		(void) snprintf(udpsbuf, sizeof (udpsbuf),
1610*45916cd2Sjpk 		    "Unknown State(%d)", state);
1611*45916cd2Sjpk 		cp = udpsbuf;
1612*45916cd2Sjpk 		break;
1613*45916cd2Sjpk 	}
1614*45916cd2Sjpk 
1615*45916cd2Sjpk 	if (RSECflag && attr != NULL && attr->tme_flags != 0) {
1616*45916cd2Sjpk 		if (cp != udpsbuf) {
1617*45916cd2Sjpk 			(void) strlcpy(udpsbuf, cp, sizeof (udpsbuf));
1618*45916cd2Sjpk 			cp = udpsbuf;
1619*45916cd2Sjpk 		}
1620*45916cd2Sjpk 		if (attr->tme_flags & MIB2_TMEF_PRIVATE)
1621*45916cd2Sjpk 			(void) strlcat(udpsbuf, " P", sizeof (udpsbuf));
1622*45916cd2Sjpk 		if (attr->tme_flags & MIB2_TMEF_SHARED)
1623*45916cd2Sjpk 			(void) strlcat(udpsbuf, " S", sizeof (udpsbuf));
1624*45916cd2Sjpk 	}
1625*45916cd2Sjpk 
16267c478bd9Sstevel@tonic-gate 	return (cp);
16277c478bd9Sstevel@tonic-gate }
16287c478bd9Sstevel@tonic-gate 
16297c478bd9Sstevel@tonic-gate static int odd;
16307c478bd9Sstevel@tonic-gate 
16317c478bd9Sstevel@tonic-gate static void
16327c478bd9Sstevel@tonic-gate prval_init(void)
16337c478bd9Sstevel@tonic-gate {
16347c478bd9Sstevel@tonic-gate 	odd = 0;
16357c478bd9Sstevel@tonic-gate }
16367c478bd9Sstevel@tonic-gate 
16377c478bd9Sstevel@tonic-gate static void
16387c478bd9Sstevel@tonic-gate prval(char *str, Counter val)
16397c478bd9Sstevel@tonic-gate {
16407c478bd9Sstevel@tonic-gate 	(void) printf("\t%-20s=%6u", str, val);
16417c478bd9Sstevel@tonic-gate 	if (odd++ & 1)
16427c478bd9Sstevel@tonic-gate 		(void) putchar('\n');
16437c478bd9Sstevel@tonic-gate }
16447c478bd9Sstevel@tonic-gate 
16457c478bd9Sstevel@tonic-gate static void
16467c478bd9Sstevel@tonic-gate prval64(char *str, Counter64 val)
16477c478bd9Sstevel@tonic-gate {
16487c478bd9Sstevel@tonic-gate 	(void) printf("\t%-20s=%6llu", str, val);
16497c478bd9Sstevel@tonic-gate 	if (odd++ & 1)
16507c478bd9Sstevel@tonic-gate 		(void) putchar('\n');
16517c478bd9Sstevel@tonic-gate }
16527c478bd9Sstevel@tonic-gate 
16537c478bd9Sstevel@tonic-gate static void
16547c478bd9Sstevel@tonic-gate pr_int_val(char *str, int val)
16557c478bd9Sstevel@tonic-gate {
16567c478bd9Sstevel@tonic-gate 	(void) printf("\t%-20s=%6d", str, val);
16577c478bd9Sstevel@tonic-gate 	if (odd++ & 1)
16587c478bd9Sstevel@tonic-gate 		(void) putchar('\n');
16597c478bd9Sstevel@tonic-gate }
16607c478bd9Sstevel@tonic-gate 
16617c478bd9Sstevel@tonic-gate static void
16627c478bd9Sstevel@tonic-gate pr_sctp_rtoalgo(char *str, int val)
16637c478bd9Sstevel@tonic-gate {
16647c478bd9Sstevel@tonic-gate 	(void) printf("\t%-20s=", str);
16657c478bd9Sstevel@tonic-gate 	switch (val) {
16667c478bd9Sstevel@tonic-gate 		case MIB2_SCTP_RTOALGO_OTHER:
16677c478bd9Sstevel@tonic-gate 			(void) printf("%6.6s", "other");
16687c478bd9Sstevel@tonic-gate 			break;
16697c478bd9Sstevel@tonic-gate 
16707c478bd9Sstevel@tonic-gate 		case MIB2_SCTP_RTOALGO_VANJ:
16717c478bd9Sstevel@tonic-gate 			(void) printf("%6.6s", "vanj");
16727c478bd9Sstevel@tonic-gate 			break;
16737c478bd9Sstevel@tonic-gate 
16747c478bd9Sstevel@tonic-gate 		default:
16757c478bd9Sstevel@tonic-gate 			(void) printf("%6d", val);
16767c478bd9Sstevel@tonic-gate 			break;
16777c478bd9Sstevel@tonic-gate 	}
16787c478bd9Sstevel@tonic-gate 	if (odd++ & 1)
16797c478bd9Sstevel@tonic-gate 		(void) putchar('\n');
16807c478bd9Sstevel@tonic-gate }
16817c478bd9Sstevel@tonic-gate 
16827c478bd9Sstevel@tonic-gate static void
16837c478bd9Sstevel@tonic-gate prval_end(void)
16847c478bd9Sstevel@tonic-gate {
16857c478bd9Sstevel@tonic-gate 	if (odd++ & 1)
16867c478bd9Sstevel@tonic-gate 		(void) putchar('\n');
16877c478bd9Sstevel@tonic-gate }
16887c478bd9Sstevel@tonic-gate 
16897c478bd9Sstevel@tonic-gate /* Extract constant sizes */
16907c478bd9Sstevel@tonic-gate static void
16917c478bd9Sstevel@tonic-gate mib_get_constants(mib_item_t *item)
16927c478bd9Sstevel@tonic-gate {
16937c478bd9Sstevel@tonic-gate 	/* 'for' loop 1: */
16947c478bd9Sstevel@tonic-gate 	for (; item; item = item->next_item) {
16957c478bd9Sstevel@tonic-gate 		if (item->mib_id != 0)
16967c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
16977c478bd9Sstevel@tonic-gate 
16987c478bd9Sstevel@tonic-gate 		switch (item->group) {
16997c478bd9Sstevel@tonic-gate 		case MIB2_IP: {
17007c478bd9Sstevel@tonic-gate 			mib2_ip_t	*ip = (mib2_ip_t *)item->valp;
17017c478bd9Sstevel@tonic-gate 
17027c478bd9Sstevel@tonic-gate 			ipAddrEntrySize = ip->ipAddrEntrySize;
17037c478bd9Sstevel@tonic-gate 			ipRouteEntrySize = ip->ipRouteEntrySize;
17047c478bd9Sstevel@tonic-gate 			ipNetToMediaEntrySize = ip->ipNetToMediaEntrySize;
17057c478bd9Sstevel@tonic-gate 			ipMemberEntrySize = ip->ipMemberEntrySize;
17067c478bd9Sstevel@tonic-gate 			ipGroupSourceEntrySize = ip->ipGroupSourceEntrySize;
1707*45916cd2Sjpk 			ipRouteAttributeSize = ip->ipRouteAttributeSize;
1708*45916cd2Sjpk 			transportMLPSize = ip->transportMLPSize;
17097c478bd9Sstevel@tonic-gate 			assert(IS_P2ALIGNED(ipAddrEntrySize,
17107c478bd9Sstevel@tonic-gate 			    sizeof (mib2_ipAddrEntry_t *)) &&
17117c478bd9Sstevel@tonic-gate 			    IS_P2ALIGNED(ipRouteEntrySize,
17127c478bd9Sstevel@tonic-gate 				sizeof (mib2_ipRouteEntry_t *)) &&
17137c478bd9Sstevel@tonic-gate 			    IS_P2ALIGNED(ipNetToMediaEntrySize,
17147c478bd9Sstevel@tonic-gate 				sizeof (mib2_ipNetToMediaEntry_t *)) &&
17157c478bd9Sstevel@tonic-gate 			    IS_P2ALIGNED(ipMemberEntrySize,
17167c478bd9Sstevel@tonic-gate 				sizeof (ip_member_t *)) &&
17177c478bd9Sstevel@tonic-gate 			    IS_P2ALIGNED(ipGroupSourceEntrySize,
1718*45916cd2Sjpk 				sizeof (ip_grpsrc_t *)) &&
1719*45916cd2Sjpk 			    IS_P2ALIGNED(ipRouteAttributeSize,
1720*45916cd2Sjpk 				sizeof (mib2_ipAttributeEntry_t *)) &&
1721*45916cd2Sjpk 			    IS_P2ALIGNED(transportMLPSize,
1722*45916cd2Sjpk 				sizeof (mib2_transportMLPEntry_t *)));
17237c478bd9Sstevel@tonic-gate 			break;
17247c478bd9Sstevel@tonic-gate 		}
17257c478bd9Sstevel@tonic-gate 		case EXPER_DVMRP: {
17267c478bd9Sstevel@tonic-gate 			struct mrtstat	*mrts = (struct mrtstat *)item->valp;
17277c478bd9Sstevel@tonic-gate 
17287c478bd9Sstevel@tonic-gate 			vifctlSize = mrts->mrts_vifctlSize;
17297c478bd9Sstevel@tonic-gate 			mfcctlSize = mrts->mrts_mfcctlSize;
17307c478bd9Sstevel@tonic-gate 			assert(IS_P2ALIGNED(vifctlSize,
17317c478bd9Sstevel@tonic-gate 			    sizeof (struct vifclt *)) &&
17327c478bd9Sstevel@tonic-gate 			    IS_P2ALIGNED(mfcctlSize, sizeof (struct mfcctl *)));
17337c478bd9Sstevel@tonic-gate 			break;
17347c478bd9Sstevel@tonic-gate 		}
17357c478bd9Sstevel@tonic-gate 		case MIB2_IP6: {
17367c478bd9Sstevel@tonic-gate 			mib2_ipv6IfStatsEntry_t *ip6;
17377c478bd9Sstevel@tonic-gate 			/* Just use the first entry */
17387c478bd9Sstevel@tonic-gate 
17397c478bd9Sstevel@tonic-gate 			ip6 = (mib2_ipv6IfStatsEntry_t *)item->valp;
17407c478bd9Sstevel@tonic-gate 			ipv6IfStatsEntrySize = ip6->ipv6IfStatsEntrySize;
17417c478bd9Sstevel@tonic-gate 			ipv6AddrEntrySize = ip6->ipv6AddrEntrySize;
17427c478bd9Sstevel@tonic-gate 			ipv6RouteEntrySize = ip6->ipv6RouteEntrySize;
17437c478bd9Sstevel@tonic-gate 			ipv6NetToMediaEntrySize = ip6->ipv6NetToMediaEntrySize;
17447c478bd9Sstevel@tonic-gate 			ipv6MemberEntrySize = ip6->ipv6MemberEntrySize;
17457c478bd9Sstevel@tonic-gate 			ipv6GroupSourceEntrySize =
17467c478bd9Sstevel@tonic-gate 			    ip6->ipv6GroupSourceEntrySize;
17477c478bd9Sstevel@tonic-gate 			assert(IS_P2ALIGNED(ipv6IfStatsEntrySize,
17487c478bd9Sstevel@tonic-gate 			    sizeof (mib2_ipv6IfStatsEntry_t *)) &&
17497c478bd9Sstevel@tonic-gate 			    IS_P2ALIGNED(ipv6AddrEntrySize,
17507c478bd9Sstevel@tonic-gate 				sizeof (mib2_ipv6AddrEntry_t *)) &&
17517c478bd9Sstevel@tonic-gate 			    IS_P2ALIGNED(ipv6RouteEntrySize,
17527c478bd9Sstevel@tonic-gate 				sizeof (mib2_ipv6RouteEntry_t *)) &&
17537c478bd9Sstevel@tonic-gate 			    IS_P2ALIGNED(ipv6NetToMediaEntrySize,
17547c478bd9Sstevel@tonic-gate 				sizeof (mib2_ipv6NetToMediaEntry_t *)) &&
17557c478bd9Sstevel@tonic-gate 			    IS_P2ALIGNED(ipv6MemberEntrySize,
17567c478bd9Sstevel@tonic-gate 				sizeof (ipv6_member_t *)) &&
17577c478bd9Sstevel@tonic-gate 			    IS_P2ALIGNED(ipv6GroupSourceEntrySize,
17587c478bd9Sstevel@tonic-gate 				sizeof (ipv6_grpsrc_t *)));
17597c478bd9Sstevel@tonic-gate 			break;
17607c478bd9Sstevel@tonic-gate 		}
17617c478bd9Sstevel@tonic-gate 		case MIB2_ICMP6: {
17627c478bd9Sstevel@tonic-gate 			mib2_ipv6IfIcmpEntry_t *icmp6;
17637c478bd9Sstevel@tonic-gate 			/* Just use the first entry */
17647c478bd9Sstevel@tonic-gate 
17657c478bd9Sstevel@tonic-gate 			icmp6 = (mib2_ipv6IfIcmpEntry_t *)item->valp;
17667c478bd9Sstevel@tonic-gate 			ipv6IfIcmpEntrySize = icmp6->ipv6IfIcmpEntrySize;
17677c478bd9Sstevel@tonic-gate 			assert(IS_P2ALIGNED(ipv6IfIcmpEntrySize,
17687c478bd9Sstevel@tonic-gate 			    sizeof (mib2_ipv6IfIcmpEntry_t *)));
17697c478bd9Sstevel@tonic-gate 			break;
17707c478bd9Sstevel@tonic-gate 		}
17717c478bd9Sstevel@tonic-gate 		case MIB2_TCP: {
17727c478bd9Sstevel@tonic-gate 			mib2_tcp_t	*tcp = (mib2_tcp_t *)item->valp;
17737c478bd9Sstevel@tonic-gate 
17747c478bd9Sstevel@tonic-gate 			tcpConnEntrySize = tcp->tcpConnTableSize;
17757c478bd9Sstevel@tonic-gate 			tcp6ConnEntrySize = tcp->tcp6ConnTableSize;
17767c478bd9Sstevel@tonic-gate 			assert(IS_P2ALIGNED(tcpConnEntrySize,
17777c478bd9Sstevel@tonic-gate 			    sizeof (mib2_tcpConnEntry_t *)) &&
17787c478bd9Sstevel@tonic-gate 			    IS_P2ALIGNED(tcp6ConnEntrySize,
17797c478bd9Sstevel@tonic-gate 				sizeof (mib2_tcp6ConnEntry_t *)));
17807c478bd9Sstevel@tonic-gate 			break;
17817c478bd9Sstevel@tonic-gate 		}
17827c478bd9Sstevel@tonic-gate 		case MIB2_UDP: {
17837c478bd9Sstevel@tonic-gate 			mib2_udp_t	*udp = (mib2_udp_t *)item->valp;
17847c478bd9Sstevel@tonic-gate 
17857c478bd9Sstevel@tonic-gate 			udpEntrySize = udp->udpEntrySize;
17867c478bd9Sstevel@tonic-gate 			udp6EntrySize = udp->udp6EntrySize;
17877c478bd9Sstevel@tonic-gate 			assert(IS_P2ALIGNED(udpEntrySize,
17887c478bd9Sstevel@tonic-gate 			    sizeof (mib2_udpEntry_t *)) &&
17897c478bd9Sstevel@tonic-gate 			    IS_P2ALIGNED(udp6EntrySize,
17907c478bd9Sstevel@tonic-gate 				sizeof (mib2_udp6Entry_t *)));
17917c478bd9Sstevel@tonic-gate 			break;
17927c478bd9Sstevel@tonic-gate 		}
17937c478bd9Sstevel@tonic-gate 		case MIB2_SCTP: {
17947c478bd9Sstevel@tonic-gate 			mib2_sctp_t	*sctp = (mib2_sctp_t *)item->valp;
17957c478bd9Sstevel@tonic-gate 
17967c478bd9Sstevel@tonic-gate 			sctpEntrySize = sctp->sctpEntrySize;
17977c478bd9Sstevel@tonic-gate 			sctpLocalEntrySize = sctp->sctpLocalEntrySize;
17987c478bd9Sstevel@tonic-gate 			sctpRemoteEntrySize = sctp->sctpRemoteEntrySize;
17997c478bd9Sstevel@tonic-gate 			break;
18007c478bd9Sstevel@tonic-gate 		}
18017c478bd9Sstevel@tonic-gate 		}
18027c478bd9Sstevel@tonic-gate 	} /* 'for' loop 1 ends */
18037c478bd9Sstevel@tonic-gate 
18047c478bd9Sstevel@tonic-gate 	if (Dflag) {
18057c478bd9Sstevel@tonic-gate 		(void) puts("mib_get_constants:");
18067c478bd9Sstevel@tonic-gate 		(void) printf("\tipv6IfStatsEntrySize %d\n",
18077c478bd9Sstevel@tonic-gate 		    ipv6IfStatsEntrySize);
18087c478bd9Sstevel@tonic-gate 		(void) printf("\tipAddrEntrySize %d\n", ipAddrEntrySize);
18097c478bd9Sstevel@tonic-gate 		(void) printf("\tipRouteEntrySize %d\n", ipRouteEntrySize);
18107c478bd9Sstevel@tonic-gate 		(void) printf("\tipNetToMediaEntrySize %d\n",
18117c478bd9Sstevel@tonic-gate 		    ipNetToMediaEntrySize);
18127c478bd9Sstevel@tonic-gate 		(void) printf("\tipMemberEntrySize %d\n", ipMemberEntrySize);
1813*45916cd2Sjpk 		(void) printf("\tipRouteAttributeSize %d\n",
1814*45916cd2Sjpk 		    ipRouteAttributeSize);
18157c478bd9Sstevel@tonic-gate 		(void) printf("\tvifctlSize %d\n", vifctlSize);
18167c478bd9Sstevel@tonic-gate 		(void) printf("\tmfcctlSize %d\n", mfcctlSize);
18177c478bd9Sstevel@tonic-gate 
18187c478bd9Sstevel@tonic-gate 		(void) printf("\tipv6AddrEntrySize %d\n", ipv6AddrEntrySize);
18197c478bd9Sstevel@tonic-gate 		(void) printf("\tipv6RouteEntrySize %d\n", ipv6RouteEntrySize);
18207c478bd9Sstevel@tonic-gate 		(void) printf("\tipv6NetToMediaEntrySize %d\n",
18217c478bd9Sstevel@tonic-gate 		    ipv6NetToMediaEntrySize);
18227c478bd9Sstevel@tonic-gate 		(void) printf("\tipv6MemberEntrySize %d\n",
18237c478bd9Sstevel@tonic-gate 		    ipv6MemberEntrySize);
18247c478bd9Sstevel@tonic-gate 		(void) printf("\tipv6IfIcmpEntrySize %d\n",
18257c478bd9Sstevel@tonic-gate 		    ipv6IfIcmpEntrySize);
1826*45916cd2Sjpk 		(void) printf("\ttransportMLPSize %d\n", transportMLPSize);
18277c478bd9Sstevel@tonic-gate 		(void) printf("\ttcpConnEntrySize %d\n", tcpConnEntrySize);
18287c478bd9Sstevel@tonic-gate 		(void) printf("\ttcp6ConnEntrySize %d\n", tcp6ConnEntrySize);
18297c478bd9Sstevel@tonic-gate 		(void) printf("\tudpEntrySize %d\n", udpEntrySize);
18307c478bd9Sstevel@tonic-gate 		(void) printf("\tudp6EntrySize %d\n", udp6EntrySize);
18317c478bd9Sstevel@tonic-gate 		(void) printf("\tsctpEntrySize %d\n", sctpEntrySize);
18327c478bd9Sstevel@tonic-gate 		(void) printf("\tsctpLocalEntrySize %d\n", sctpLocalEntrySize);
18337c478bd9Sstevel@tonic-gate 		(void) printf("\tsctpRemoteEntrySize %d\n",
18347c478bd9Sstevel@tonic-gate 		    sctpRemoteEntrySize);
18357c478bd9Sstevel@tonic-gate 	}
18367c478bd9Sstevel@tonic-gate }
18377c478bd9Sstevel@tonic-gate 
18387c478bd9Sstevel@tonic-gate 
18397c478bd9Sstevel@tonic-gate /* ----------------------------- STAT_REPORT ------------------------------- */
18407c478bd9Sstevel@tonic-gate 
18417c478bd9Sstevel@tonic-gate static void
18427c478bd9Sstevel@tonic-gate stat_report(mib_item_t *item)
18437c478bd9Sstevel@tonic-gate {
18447c478bd9Sstevel@tonic-gate 	int	jtemp = 0;
18457c478bd9Sstevel@tonic-gate 	char	ifname[LIFNAMSIZ + 1];
18467c478bd9Sstevel@tonic-gate 	char	*ifnamep;
18477c478bd9Sstevel@tonic-gate 
18487c478bd9Sstevel@tonic-gate 	/* 'for' loop 1: */
18497c478bd9Sstevel@tonic-gate 	for (; item; item = item->next_item) {
18507c478bd9Sstevel@tonic-gate 		if (Dflag) {
18517c478bd9Sstevel@tonic-gate 			(void) printf("\n--- Entry %d ---\n", ++jtemp);
18527c478bd9Sstevel@tonic-gate 			(void) printf("Group = %d, mib_id = %d, "
18537c478bd9Sstevel@tonic-gate 			    "length = %d, valp = 0x%p\n",
18547c478bd9Sstevel@tonic-gate 			    item->group, item->mib_id,
18557c478bd9Sstevel@tonic-gate 			    item->length, item->valp);
18567c478bd9Sstevel@tonic-gate 		}
18577c478bd9Sstevel@tonic-gate 		if (item->mib_id != 0)
18587c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
18597c478bd9Sstevel@tonic-gate 
18607c478bd9Sstevel@tonic-gate 		switch (item->group) {
18617c478bd9Sstevel@tonic-gate 		case MIB2_IP: {
18627c478bd9Sstevel@tonic-gate 			mib2_ip_t	*ip = (mib2_ip_t *)item->valp;
18637c478bd9Sstevel@tonic-gate 
18647c478bd9Sstevel@tonic-gate 			if (protocol_selected(IPPROTO_IP) &&
18657c478bd9Sstevel@tonic-gate 			    family_selected(AF_INET)) {
18667c478bd9Sstevel@tonic-gate 				(void) fputs(v4compat ? "\nIP" : "\nIPv4",
18677c478bd9Sstevel@tonic-gate 				    stdout);
18687c478bd9Sstevel@tonic-gate 				print_ip_stats(ip);
18697c478bd9Sstevel@tonic-gate 			}
18707c478bd9Sstevel@tonic-gate 			break;
18717c478bd9Sstevel@tonic-gate 		}
18727c478bd9Sstevel@tonic-gate 		case MIB2_ICMP: {
18737c478bd9Sstevel@tonic-gate 			mib2_icmp_t	*icmp =
18747c478bd9Sstevel@tonic-gate 			    (mib2_icmp_t *)item->valp;
18757c478bd9Sstevel@tonic-gate 
18767c478bd9Sstevel@tonic-gate 			if (protocol_selected(IPPROTO_ICMP) &&
18777c478bd9Sstevel@tonic-gate 			    family_selected(AF_INET)) {
18787c478bd9Sstevel@tonic-gate 				(void) fputs(v4compat ? "\nICMP" : "\nICMPv4",
18797c478bd9Sstevel@tonic-gate 				    stdout);
18807c478bd9Sstevel@tonic-gate 				print_icmp_stats(icmp);
18817c478bd9Sstevel@tonic-gate 			}
18827c478bd9Sstevel@tonic-gate 			break;
18837c478bd9Sstevel@tonic-gate 		}
18847c478bd9Sstevel@tonic-gate 		case MIB2_IP6: {
18857c478bd9Sstevel@tonic-gate 			mib2_ipv6IfStatsEntry_t *ip6;
18867c478bd9Sstevel@tonic-gate 			mib2_ipv6IfStatsEntry_t sum6;
18877c478bd9Sstevel@tonic-gate 
18887c478bd9Sstevel@tonic-gate 			if (!(protocol_selected(IPPROTO_IPV6)) ||
18897c478bd9Sstevel@tonic-gate 			    !(family_selected(AF_INET6)))
18907c478bd9Sstevel@tonic-gate 				break;
18917c478bd9Sstevel@tonic-gate 			bzero(&sum6, sizeof (sum6));
18927c478bd9Sstevel@tonic-gate 			/* 'for' loop 2a: */
18937c478bd9Sstevel@tonic-gate 			for (ip6 = (mib2_ipv6IfStatsEntry_t *)item->valp;
18947c478bd9Sstevel@tonic-gate 			    (char *)ip6 < (char *)item->valp
18957c478bd9Sstevel@tonic-gate 			    + item->length;
18967c478bd9Sstevel@tonic-gate 			    /* LINTED: (note 1) */
18977c478bd9Sstevel@tonic-gate 			    ip6 = (mib2_ipv6IfStatsEntry_t *)((char *)ip6 +
18987c478bd9Sstevel@tonic-gate 			    ipv6IfStatsEntrySize)) {
18997c478bd9Sstevel@tonic-gate 
19007c478bd9Sstevel@tonic-gate 				if (ip6->ipv6IfIndex == 0) {
19017c478bd9Sstevel@tonic-gate 					/*
19027c478bd9Sstevel@tonic-gate 					 * The "unknown interface" ip6
19037c478bd9Sstevel@tonic-gate 					 * mib. Just add to the sum.
19047c478bd9Sstevel@tonic-gate 					 */
19057c478bd9Sstevel@tonic-gate 					sum_ip6_stats(ip6, &sum6);
19067c478bd9Sstevel@tonic-gate 					continue; /* 'for' loop 2a */
19077c478bd9Sstevel@tonic-gate 				}
19087c478bd9Sstevel@tonic-gate 				ifnamep = if_indextoname(
19097c478bd9Sstevel@tonic-gate 				    ip6->ipv6IfIndex,
19107c478bd9Sstevel@tonic-gate 				    ifname);
19117c478bd9Sstevel@tonic-gate 				if (ifnamep == NULL) {
19127c478bd9Sstevel@tonic-gate 					(void) printf(
19137c478bd9Sstevel@tonic-gate 					    "Invalid ifindex %d\n",
19147c478bd9Sstevel@tonic-gate 					    ip6->ipv6IfIndex);
19157c478bd9Sstevel@tonic-gate 					continue; /* 'for' loop 2a */
19167c478bd9Sstevel@tonic-gate 				}
19177c478bd9Sstevel@tonic-gate 
19187c478bd9Sstevel@tonic-gate 				if (Aflag) {
19197c478bd9Sstevel@tonic-gate 					(void) printf("\nIPv6 for %s\n",
19207c478bd9Sstevel@tonic-gate 					    ifnamep);
19217c478bd9Sstevel@tonic-gate 					print_ip6_stats(ip6);
19227c478bd9Sstevel@tonic-gate 				}
19237c478bd9Sstevel@tonic-gate 				sum_ip6_stats(ip6, &sum6);
19247c478bd9Sstevel@tonic-gate 			} /* 'for' loop 2a ends */
19257c478bd9Sstevel@tonic-gate 			(void) fputs("\nIPv6", stdout);
19267c478bd9Sstevel@tonic-gate 			print_ip6_stats(&sum6);
19277c478bd9Sstevel@tonic-gate 			break;
19287c478bd9Sstevel@tonic-gate 		}
19297c478bd9Sstevel@tonic-gate 		case MIB2_ICMP6: {
19307c478bd9Sstevel@tonic-gate 			mib2_ipv6IfIcmpEntry_t *icmp6;
19317c478bd9Sstevel@tonic-gate 			mib2_ipv6IfIcmpEntry_t sum6;
19327c478bd9Sstevel@tonic-gate 
19337c478bd9Sstevel@tonic-gate 			if (!(protocol_selected(IPPROTO_ICMPV6)) ||
19347c478bd9Sstevel@tonic-gate 			    !(family_selected(AF_INET6)))
19357c478bd9Sstevel@tonic-gate 				break;
19367c478bd9Sstevel@tonic-gate 			bzero(&sum6, sizeof (sum6));
19377c478bd9Sstevel@tonic-gate 			/* 'for' loop 2b: */
19387c478bd9Sstevel@tonic-gate 			for (icmp6 =
19397c478bd9Sstevel@tonic-gate 			    (mib2_ipv6IfIcmpEntry_t *)item->valp;
19407c478bd9Sstevel@tonic-gate 			    (char *)icmp6 < (char *)item->valp
19417c478bd9Sstevel@tonic-gate 				+ item->length;
19427c478bd9Sstevel@tonic-gate 			    icmp6 =
19437c478bd9Sstevel@tonic-gate 				/* LINTED: (note 1) */
19447c478bd9Sstevel@tonic-gate 				(mib2_ipv6IfIcmpEntry_t *)((char *)icmp6
19457c478bd9Sstevel@tonic-gate 			    + ipv6IfIcmpEntrySize)) {
19467c478bd9Sstevel@tonic-gate 
19477c478bd9Sstevel@tonic-gate 				if (icmp6->ipv6IfIcmpIfIndex == 0) {
19487c478bd9Sstevel@tonic-gate 					/*
19497c478bd9Sstevel@tonic-gate 					 * The "unknown interface" icmp6
19507c478bd9Sstevel@tonic-gate 					 * mib. Just add to the sum.
19517c478bd9Sstevel@tonic-gate 					 */
19527c478bd9Sstevel@tonic-gate 					sum_icmp6_stats(icmp6, &sum6);
19537c478bd9Sstevel@tonic-gate 					continue; /* 'for' loop 2b: */
19547c478bd9Sstevel@tonic-gate 				}
19557c478bd9Sstevel@tonic-gate 				ifnamep = if_indextoname(
19567c478bd9Sstevel@tonic-gate 				    icmp6->ipv6IfIcmpIfIndex, ifname);
19577c478bd9Sstevel@tonic-gate 				if (ifnamep == NULL) {
19587c478bd9Sstevel@tonic-gate 					(void) printf(
19597c478bd9Sstevel@tonic-gate 					    "Invalid ifindex %d\n",
19607c478bd9Sstevel@tonic-gate 					    icmp6->ipv6IfIcmpIfIndex);
19617c478bd9Sstevel@tonic-gate 					continue; /* 'for' loop 2b: */
19627c478bd9Sstevel@tonic-gate 				}
19637c478bd9Sstevel@tonic-gate 
19647c478bd9Sstevel@tonic-gate 				if (Aflag) {
19657c478bd9Sstevel@tonic-gate 					(void) printf(
19667c478bd9Sstevel@tonic-gate 					    "\nICMPv6 for %s\n",
19677c478bd9Sstevel@tonic-gate 					    ifnamep);
19687c478bd9Sstevel@tonic-gate 					print_icmp6_stats(icmp6);
19697c478bd9Sstevel@tonic-gate 				}
19707c478bd9Sstevel@tonic-gate 				sum_icmp6_stats(icmp6, &sum6);
19717c478bd9Sstevel@tonic-gate 			} /* 'for' loop 2b ends */
19727c478bd9Sstevel@tonic-gate 			(void) fputs("\nICMPv6", stdout);
19737c478bd9Sstevel@tonic-gate 			print_icmp6_stats(&sum6);
19747c478bd9Sstevel@tonic-gate 			break;
19757c478bd9Sstevel@tonic-gate 		}
19767c478bd9Sstevel@tonic-gate 		case MIB2_TCP: {
19777c478bd9Sstevel@tonic-gate 			mib2_tcp_t	*tcp = (mib2_tcp_t *)item->valp;
19787c478bd9Sstevel@tonic-gate 
19797c478bd9Sstevel@tonic-gate 			if (protocol_selected(IPPROTO_TCP) &&
19807c478bd9Sstevel@tonic-gate 			    (family_selected(AF_INET) ||
19817c478bd9Sstevel@tonic-gate 			    family_selected(AF_INET6))) {
19827c478bd9Sstevel@tonic-gate 				(void) fputs("\nTCP", stdout);
19837c478bd9Sstevel@tonic-gate 				print_tcp_stats(tcp);
19847c478bd9Sstevel@tonic-gate 			}
19857c478bd9Sstevel@tonic-gate 			break;
19867c478bd9Sstevel@tonic-gate 		}
19877c478bd9Sstevel@tonic-gate 		case MIB2_UDP: {
19887c478bd9Sstevel@tonic-gate 			mib2_udp_t	*udp = (mib2_udp_t *)item->valp;
19897c478bd9Sstevel@tonic-gate 
19907c478bd9Sstevel@tonic-gate 			if (protocol_selected(IPPROTO_UDP) &&
19917c478bd9Sstevel@tonic-gate 			    (family_selected(AF_INET) ||
19927c478bd9Sstevel@tonic-gate 			    family_selected(AF_INET6))) {
19937c478bd9Sstevel@tonic-gate 				(void) fputs("\nUDP", stdout);
19947c478bd9Sstevel@tonic-gate 				print_udp_stats(udp);
19957c478bd9Sstevel@tonic-gate 			}
19967c478bd9Sstevel@tonic-gate 			break;
19977c478bd9Sstevel@tonic-gate 		}
19987c478bd9Sstevel@tonic-gate 		case MIB2_SCTP: {
19997c478bd9Sstevel@tonic-gate 			mib2_sctp_t	*sctp = (mib2_sctp_t *)item->valp;
20007c478bd9Sstevel@tonic-gate 
20017c478bd9Sstevel@tonic-gate 			if (protocol_selected(IPPROTO_SCTP) &&
20027c478bd9Sstevel@tonic-gate 			    (family_selected(AF_INET) ||
20037c478bd9Sstevel@tonic-gate 			    family_selected(AF_INET6))) {
20047c478bd9Sstevel@tonic-gate 				(void) fputs("\nSCTP", stdout);
20057c478bd9Sstevel@tonic-gate 				print_sctp_stats(sctp);
20067c478bd9Sstevel@tonic-gate 			}
20077c478bd9Sstevel@tonic-gate 			break;
20087c478bd9Sstevel@tonic-gate 		}
20097c478bd9Sstevel@tonic-gate 		case EXPER_RAWIP: {
20107c478bd9Sstevel@tonic-gate 			mib2_rawip_t	*rawip =
20117c478bd9Sstevel@tonic-gate 			    (mib2_rawip_t *)item->valp;
20127c478bd9Sstevel@tonic-gate 
20137c478bd9Sstevel@tonic-gate 			if (protocol_selected(IPPROTO_RAW) &&
20147c478bd9Sstevel@tonic-gate 			    (family_selected(AF_INET) ||
20157c478bd9Sstevel@tonic-gate 			    family_selected(AF_INET6))) {
20167c478bd9Sstevel@tonic-gate 				(void) fputs("\nRAWIP", stdout);
20177c478bd9Sstevel@tonic-gate 				print_rawip_stats(rawip);
20187c478bd9Sstevel@tonic-gate 			}
20197c478bd9Sstevel@tonic-gate 			break;
20207c478bd9Sstevel@tonic-gate 		}
20217c478bd9Sstevel@tonic-gate 		case EXPER_IGMP: {
20227c478bd9Sstevel@tonic-gate 			struct igmpstat	*igps =
20237c478bd9Sstevel@tonic-gate 			    (struct igmpstat *)item->valp;
20247c478bd9Sstevel@tonic-gate 
20257c478bd9Sstevel@tonic-gate 			if (protocol_selected(IPPROTO_IGMP) &&
20267c478bd9Sstevel@tonic-gate 			    (family_selected(AF_INET))) {
20277c478bd9Sstevel@tonic-gate 				(void) fputs("\nIGMP:\n", stdout);
20287c478bd9Sstevel@tonic-gate 				print_igmp_stats(igps);
20297c478bd9Sstevel@tonic-gate 			}
20307c478bd9Sstevel@tonic-gate 			break;
20317c478bd9Sstevel@tonic-gate 		}
20327c478bd9Sstevel@tonic-gate 		}
20337c478bd9Sstevel@tonic-gate 	} /* 'for' loop 1 ends */
20347c478bd9Sstevel@tonic-gate 	(void) putchar('\n');
20357c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
20367c478bd9Sstevel@tonic-gate }
20377c478bd9Sstevel@tonic-gate 
20387c478bd9Sstevel@tonic-gate static void
20397c478bd9Sstevel@tonic-gate print_ip_stats(mib2_ip_t *ip)
20407c478bd9Sstevel@tonic-gate {
20417c478bd9Sstevel@tonic-gate 	prval_init();
20427c478bd9Sstevel@tonic-gate 	pr_int_val("ipForwarding",	ip->ipForwarding);
20437c478bd9Sstevel@tonic-gate 	pr_int_val("ipDefaultTTL",	ip->ipDefaultTTL);
20447c478bd9Sstevel@tonic-gate 	prval("ipInReceives",		ip->ipInReceives);
20457c478bd9Sstevel@tonic-gate 	prval("ipInHdrErrors",		ip->ipInHdrErrors);
20467c478bd9Sstevel@tonic-gate 	prval("ipInAddrErrors",		ip->ipInAddrErrors);
20477c478bd9Sstevel@tonic-gate 	prval("ipInCksumErrs",		ip->ipInCksumErrs);
20487c478bd9Sstevel@tonic-gate 	prval("ipForwDatagrams",	ip->ipForwDatagrams);
20497c478bd9Sstevel@tonic-gate 	prval("ipForwProhibits",	ip->ipForwProhibits);
20507c478bd9Sstevel@tonic-gate 	prval("ipInUnknownProtos",	ip->ipInUnknownProtos);
20517c478bd9Sstevel@tonic-gate 	prval("ipInDiscards",		ip->ipInDiscards);
20527c478bd9Sstevel@tonic-gate 	prval("ipInDelivers",		ip->ipInDelivers);
20537c478bd9Sstevel@tonic-gate 	prval("ipOutRequests",		ip->ipOutRequests);
20547c478bd9Sstevel@tonic-gate 	prval("ipOutDiscards",		ip->ipOutDiscards);
20557c478bd9Sstevel@tonic-gate 	prval("ipOutNoRoutes",		ip->ipOutNoRoutes);
20567c478bd9Sstevel@tonic-gate 	pr_int_val("ipReasmTimeout",	ip->ipReasmTimeout);
20577c478bd9Sstevel@tonic-gate 	prval("ipReasmReqds",		ip->ipReasmReqds);
20587c478bd9Sstevel@tonic-gate 	prval("ipReasmOKs",		ip->ipReasmOKs);
20597c478bd9Sstevel@tonic-gate 	prval("ipReasmFails",		ip->ipReasmFails);
20607c478bd9Sstevel@tonic-gate 	prval("ipReasmDuplicates",	ip->ipReasmDuplicates);
20617c478bd9Sstevel@tonic-gate 	prval("ipReasmPartDups",	ip->ipReasmPartDups);
20627c478bd9Sstevel@tonic-gate 	prval("ipFragOKs",		ip->ipFragOKs);
20637c478bd9Sstevel@tonic-gate 	prval("ipFragFails",		ip->ipFragFails);
20647c478bd9Sstevel@tonic-gate 	prval("ipFragCreates",		ip->ipFragCreates);
20657c478bd9Sstevel@tonic-gate 	prval("ipRoutingDiscards",	ip->ipRoutingDiscards);
20667c478bd9Sstevel@tonic-gate 
20677c478bd9Sstevel@tonic-gate 	prval("tcpInErrs",		ip->tcpInErrs);
20687c478bd9Sstevel@tonic-gate 	prval("udpNoPorts",		ip->udpNoPorts);
20697c478bd9Sstevel@tonic-gate 	prval("udpInCksumErrs",		ip->udpInCksumErrs);
20707c478bd9Sstevel@tonic-gate 	prval("udpInOverflows",		ip->udpInOverflows);
20717c478bd9Sstevel@tonic-gate 	prval("rawipInOverflows",	ip->rawipInOverflows);
20727c478bd9Sstevel@tonic-gate 	prval("ipsecInSucceeded",	ip->ipsecInSucceeded);
20737c478bd9Sstevel@tonic-gate 	prval("ipsecInFailed",		ip->ipsecInFailed);
20747c478bd9Sstevel@tonic-gate 	prval("ipInIPv6",		ip->ipInIPv6);
20757c478bd9Sstevel@tonic-gate 	prval("ipOutIPv6",		ip->ipOutIPv6);
20767c478bd9Sstevel@tonic-gate 	prval("ipOutSwitchIPv6",	ip->ipOutSwitchIPv6);
20777c478bd9Sstevel@tonic-gate 	prval_end();
20787c478bd9Sstevel@tonic-gate }
20797c478bd9Sstevel@tonic-gate 
20807c478bd9Sstevel@tonic-gate static void
20817c478bd9Sstevel@tonic-gate print_icmp_stats(mib2_icmp_t *icmp)
20827c478bd9Sstevel@tonic-gate {
20837c478bd9Sstevel@tonic-gate 	prval_init();
20847c478bd9Sstevel@tonic-gate 	prval("icmpInMsgs",		icmp->icmpInMsgs);
20857c478bd9Sstevel@tonic-gate 	prval("icmpInErrors",		icmp->icmpInErrors);
20867c478bd9Sstevel@tonic-gate 	prval("icmpInCksumErrs",	icmp->icmpInCksumErrs);
20877c478bd9Sstevel@tonic-gate 	prval("icmpInUnknowns",		icmp->icmpInUnknowns);
20887c478bd9Sstevel@tonic-gate 	prval("icmpInDestUnreachs",	icmp->icmpInDestUnreachs);
20897c478bd9Sstevel@tonic-gate 	prval("icmpInTimeExcds",	icmp->icmpInTimeExcds);
20907c478bd9Sstevel@tonic-gate 	prval("icmpInParmProbs",	icmp->icmpInParmProbs);
20917c478bd9Sstevel@tonic-gate 	prval("icmpInSrcQuenchs",	icmp->icmpInSrcQuenchs);
20927c478bd9Sstevel@tonic-gate 	prval("icmpInRedirects",	icmp->icmpInRedirects);
20937c478bd9Sstevel@tonic-gate 	prval("icmpInBadRedirects",	icmp->icmpInBadRedirects);
20947c478bd9Sstevel@tonic-gate 	prval("icmpInEchos",		icmp->icmpInEchos);
20957c478bd9Sstevel@tonic-gate 	prval("icmpInEchoReps",		icmp->icmpInEchoReps);
20967c478bd9Sstevel@tonic-gate 	prval("icmpInTimestamps",	icmp->icmpInTimestamps);
20977c478bd9Sstevel@tonic-gate 	prval("icmpInTimestampReps",	icmp->icmpInTimestampReps);
20987c478bd9Sstevel@tonic-gate 	prval("icmpInAddrMasks",	icmp->icmpInAddrMasks);
20997c478bd9Sstevel@tonic-gate 	prval("icmpInAddrMaskReps",	icmp->icmpInAddrMaskReps);
21007c478bd9Sstevel@tonic-gate 	prval("icmpInFragNeeded",	icmp->icmpInFragNeeded);
21017c478bd9Sstevel@tonic-gate 	prval("icmpOutMsgs",		icmp->icmpOutMsgs);
21027c478bd9Sstevel@tonic-gate 	prval("icmpOutDrops",		icmp->icmpOutDrops);
21037c478bd9Sstevel@tonic-gate 	prval("icmpOutErrors",		icmp->icmpOutErrors);
21047c478bd9Sstevel@tonic-gate 	prval("icmpOutDestUnreachs",	icmp->icmpOutDestUnreachs);
21057c478bd9Sstevel@tonic-gate 	prval("icmpOutTimeExcds",	icmp->icmpOutTimeExcds);
21067c478bd9Sstevel@tonic-gate 	prval("icmpOutParmProbs",	icmp->icmpOutParmProbs);
21077c478bd9Sstevel@tonic-gate 	prval("icmpOutSrcQuenchs",	icmp->icmpOutSrcQuenchs);
21087c478bd9Sstevel@tonic-gate 	prval("icmpOutRedirects",	icmp->icmpOutRedirects);
21097c478bd9Sstevel@tonic-gate 	prval("icmpOutEchos",		icmp->icmpOutEchos);
21107c478bd9Sstevel@tonic-gate 	prval("icmpOutEchoReps",	icmp->icmpOutEchoReps);
21117c478bd9Sstevel@tonic-gate 	prval("icmpOutTimestamps",	icmp->icmpOutTimestamps);
21127c478bd9Sstevel@tonic-gate 	prval("icmpOutTimestampReps",	icmp->icmpOutTimestampReps);
21137c478bd9Sstevel@tonic-gate 	prval("icmpOutAddrMasks",	icmp->icmpOutAddrMasks);
21147c478bd9Sstevel@tonic-gate 	prval("icmpOutAddrMaskReps",	icmp->icmpOutAddrMaskReps);
21157c478bd9Sstevel@tonic-gate 	prval("icmpOutFragNeeded",	icmp->icmpOutFragNeeded);
21167c478bd9Sstevel@tonic-gate 	prval("icmpInOverflows",	icmp->icmpInOverflows);
21177c478bd9Sstevel@tonic-gate 	prval_end();
21187c478bd9Sstevel@tonic-gate }
21197c478bd9Sstevel@tonic-gate 
21207c478bd9Sstevel@tonic-gate static void
21217c478bd9Sstevel@tonic-gate print_ip6_stats(mib2_ipv6IfStatsEntry_t *ip6)
21227c478bd9Sstevel@tonic-gate {
21237c478bd9Sstevel@tonic-gate 	prval_init();
21247c478bd9Sstevel@tonic-gate 	prval("ipv6Forwarding",		ip6->ipv6Forwarding);
21257c478bd9Sstevel@tonic-gate 	prval("ipv6DefaultHopLimit",	ip6->ipv6DefaultHopLimit);
21267c478bd9Sstevel@tonic-gate 
21277c478bd9Sstevel@tonic-gate 	prval("ipv6InReceives",		ip6->ipv6InReceives);
21287c478bd9Sstevel@tonic-gate 	prval("ipv6InHdrErrors",	ip6->ipv6InHdrErrors);
21297c478bd9Sstevel@tonic-gate 	prval("ipv6InTooBigErrors",	ip6->ipv6InTooBigErrors);
21307c478bd9Sstevel@tonic-gate 	prval("ipv6InNoRoutes",		ip6->ipv6InNoRoutes);
21317c478bd9Sstevel@tonic-gate 	prval("ipv6InAddrErrors",	ip6->ipv6InAddrErrors);
21327c478bd9Sstevel@tonic-gate 	prval("ipv6InUnknownProtos",	ip6->ipv6InUnknownProtos);
21337c478bd9Sstevel@tonic-gate 	prval("ipv6InTruncatedPkts",	ip6->ipv6InTruncatedPkts);
21347c478bd9Sstevel@tonic-gate 	prval("ipv6InDiscards",		ip6->ipv6InDiscards);
21357c478bd9Sstevel@tonic-gate 	prval("ipv6InDelivers",		ip6->ipv6InDelivers);
21367c478bd9Sstevel@tonic-gate 	prval("ipv6OutForwDatagrams",	ip6->ipv6OutForwDatagrams);
21377c478bd9Sstevel@tonic-gate 	prval("ipv6OutRequests",	ip6->ipv6OutRequests);
21387c478bd9Sstevel@tonic-gate 	prval("ipv6OutDiscards",	ip6->ipv6OutDiscards);
21397c478bd9Sstevel@tonic-gate 	prval("ipv6OutNoRoutes",	ip6->ipv6OutNoRoutes);
21407c478bd9Sstevel@tonic-gate 	prval("ipv6OutFragOKs",		ip6->ipv6OutFragOKs);
21417c478bd9Sstevel@tonic-gate 	prval("ipv6OutFragFails",	ip6->ipv6OutFragFails);
21427c478bd9Sstevel@tonic-gate 	prval("ipv6OutFragCreates",	ip6->ipv6OutFragCreates);
21437c478bd9Sstevel@tonic-gate 	prval("ipv6ReasmReqds",		ip6->ipv6ReasmReqds);
21447c478bd9Sstevel@tonic-gate 	prval("ipv6ReasmOKs",		ip6->ipv6ReasmOKs);
21457c478bd9Sstevel@tonic-gate 	prval("ipv6ReasmFails",		ip6->ipv6ReasmFails);
21467c478bd9Sstevel@tonic-gate 	prval("ipv6InMcastPkts",	ip6->ipv6InMcastPkts);
21477c478bd9Sstevel@tonic-gate 	prval("ipv6OutMcastPkts",	ip6->ipv6OutMcastPkts);
21487c478bd9Sstevel@tonic-gate 	prval("ipv6ReasmDuplicates",	ip6->ipv6ReasmDuplicates);
21497c478bd9Sstevel@tonic-gate 	prval("ipv6ReasmPartDups",	ip6->ipv6ReasmPartDups);
21507c478bd9Sstevel@tonic-gate 	prval("ipv6ForwProhibits",	ip6->ipv6ForwProhibits);
21517c478bd9Sstevel@tonic-gate 	prval("udpInCksumErrs",		ip6->udpInCksumErrs);
21527c478bd9Sstevel@tonic-gate 	prval("udpInOverflows",		ip6->udpInOverflows);
21537c478bd9Sstevel@tonic-gate 	prval("rawipInOverflows",	ip6->rawipInOverflows);
21547c478bd9Sstevel@tonic-gate 	prval("ipv6InIPv4",		ip6->ipv6InIPv4);
21557c478bd9Sstevel@tonic-gate 	prval("ipv6OutIPv4",		ip6->ipv6OutIPv4);
21567c478bd9Sstevel@tonic-gate 	prval("ipv6OutSwitchIPv4",	ip6->ipv6OutSwitchIPv4);
21577c478bd9Sstevel@tonic-gate 	prval_end();
21587c478bd9Sstevel@tonic-gate }
21597c478bd9Sstevel@tonic-gate 
21607c478bd9Sstevel@tonic-gate static void
21617c478bd9Sstevel@tonic-gate print_icmp6_stats(mib2_ipv6IfIcmpEntry_t *icmp6)
21627c478bd9Sstevel@tonic-gate {
21637c478bd9Sstevel@tonic-gate 	prval_init();
21647c478bd9Sstevel@tonic-gate 	prval("icmp6InMsgs",		icmp6->ipv6IfIcmpInMsgs);
21657c478bd9Sstevel@tonic-gate 	prval("icmp6InErrors",		icmp6->ipv6IfIcmpInErrors);
21667c478bd9Sstevel@tonic-gate 	prval("icmp6InDestUnreachs",	icmp6->ipv6IfIcmpInDestUnreachs);
21677c478bd9Sstevel@tonic-gate 	prval("icmp6InAdminProhibs",	icmp6->ipv6IfIcmpInAdminProhibs);
21687c478bd9Sstevel@tonic-gate 	prval("icmp6InTimeExcds",	icmp6->ipv6IfIcmpInTimeExcds);
21697c478bd9Sstevel@tonic-gate 	prval("icmp6InParmProblems",	icmp6->ipv6IfIcmpInParmProblems);
21707c478bd9Sstevel@tonic-gate 	prval("icmp6InPktTooBigs",	icmp6->ipv6IfIcmpInPktTooBigs);
21717c478bd9Sstevel@tonic-gate 	prval("icmp6InEchos",		icmp6->ipv6IfIcmpInEchos);
21727c478bd9Sstevel@tonic-gate 	prval("icmp6InEchoReplies",	icmp6->ipv6IfIcmpInEchoReplies);
21737c478bd9Sstevel@tonic-gate 	prval("icmp6InRouterSols",	icmp6->ipv6IfIcmpInRouterSolicits);
21747c478bd9Sstevel@tonic-gate 	prval("icmp6InRouterAds",
21757c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpInRouterAdvertisements);
21767c478bd9Sstevel@tonic-gate 	prval("icmp6InNeighborSols",	icmp6->ipv6IfIcmpInNeighborSolicits);
21777c478bd9Sstevel@tonic-gate 	prval("icmp6InNeighborAds",
21787c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpInNeighborAdvertisements);
21797c478bd9Sstevel@tonic-gate 	prval("icmp6InRedirects",	icmp6->ipv6IfIcmpInRedirects);
21807c478bd9Sstevel@tonic-gate 	prval("icmp6InBadRedirects",	icmp6->ipv6IfIcmpInBadRedirects);
21817c478bd9Sstevel@tonic-gate 	prval("icmp6InGroupQueries",	icmp6->ipv6IfIcmpInGroupMembQueries);
21827c478bd9Sstevel@tonic-gate 	prval("icmp6InGroupResps",	icmp6->ipv6IfIcmpInGroupMembResponses);
21837c478bd9Sstevel@tonic-gate 	prval("icmp6InGroupReds",	icmp6->ipv6IfIcmpInGroupMembReductions);
21847c478bd9Sstevel@tonic-gate 	prval("icmp6InOverflows",	icmp6->ipv6IfIcmpInOverflows);
21857c478bd9Sstevel@tonic-gate 	prval_end();
21867c478bd9Sstevel@tonic-gate 	prval_init();
21877c478bd9Sstevel@tonic-gate 	prval("icmp6OutMsgs",		icmp6->ipv6IfIcmpOutMsgs);
21887c478bd9Sstevel@tonic-gate 	prval("icmp6OutErrors",		icmp6->ipv6IfIcmpOutErrors);
21897c478bd9Sstevel@tonic-gate 	prval("icmp6OutDestUnreachs",	icmp6->ipv6IfIcmpOutDestUnreachs);
21907c478bd9Sstevel@tonic-gate 	prval("icmp6OutAdminProhibs",	icmp6->ipv6IfIcmpOutAdminProhibs);
21917c478bd9Sstevel@tonic-gate 	prval("icmp6OutTimeExcds",	icmp6->ipv6IfIcmpOutTimeExcds);
21927c478bd9Sstevel@tonic-gate 	prval("icmp6OutParmProblems",	icmp6->ipv6IfIcmpOutParmProblems);
21937c478bd9Sstevel@tonic-gate 	prval("icmp6OutPktTooBigs",	icmp6->ipv6IfIcmpOutPktTooBigs);
21947c478bd9Sstevel@tonic-gate 	prval("icmp6OutEchos",		icmp6->ipv6IfIcmpOutEchos);
21957c478bd9Sstevel@tonic-gate 	prval("icmp6OutEchoReplies",	icmp6->ipv6IfIcmpOutEchoReplies);
21967c478bd9Sstevel@tonic-gate 	prval("icmp6OutRouterSols",	icmp6->ipv6IfIcmpOutRouterSolicits);
21977c478bd9Sstevel@tonic-gate 	prval("icmp6OutRouterAds",
21987c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpOutRouterAdvertisements);
21997c478bd9Sstevel@tonic-gate 	prval("icmp6OutNeighborSols",	icmp6->ipv6IfIcmpOutNeighborSolicits);
22007c478bd9Sstevel@tonic-gate 	prval("icmp6OutNeighborAds",
22017c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpOutNeighborAdvertisements);
22027c478bd9Sstevel@tonic-gate 	prval("icmp6OutRedirects",	icmp6->ipv6IfIcmpOutRedirects);
22037c478bd9Sstevel@tonic-gate 	prval("icmp6OutGroupQueries",	icmp6->ipv6IfIcmpOutGroupMembQueries);
22047c478bd9Sstevel@tonic-gate 	prval("icmp6OutGroupResps",
22057c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpOutGroupMembResponses);
22067c478bd9Sstevel@tonic-gate 	prval("icmp6OutGroupReds",
22077c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpOutGroupMembReductions);
22087c478bd9Sstevel@tonic-gate 	prval_end();
22097c478bd9Sstevel@tonic-gate }
22107c478bd9Sstevel@tonic-gate 
22117c478bd9Sstevel@tonic-gate static void
22127c478bd9Sstevel@tonic-gate print_sctp_stats(mib2_sctp_t *sctp)
22137c478bd9Sstevel@tonic-gate {
22147c478bd9Sstevel@tonic-gate 	prval_init();
22157c478bd9Sstevel@tonic-gate 	pr_sctp_rtoalgo("sctpRtoAlgorithm", sctp->sctpRtoAlgorithm);
22167c478bd9Sstevel@tonic-gate 	prval("sctpRtoMin",		sctp->sctpRtoMin);
22177c478bd9Sstevel@tonic-gate 	prval("sctpRtoMax",		sctp->sctpRtoMax);
22187c478bd9Sstevel@tonic-gate 	prval("sctpRtoInitial",		sctp->sctpRtoInitial);
22197c478bd9Sstevel@tonic-gate 	pr_int_val("sctpMaxAssocs",	sctp->sctpMaxAssocs);
22207c478bd9Sstevel@tonic-gate 	prval("sctpValCookieLife",	sctp->sctpValCookieLife);
22217c478bd9Sstevel@tonic-gate 	prval("sctpMaxInitRetr",	sctp->sctpMaxInitRetr);
22227c478bd9Sstevel@tonic-gate 	prval("sctpCurrEstab",		sctp->sctpCurrEstab);
22237c478bd9Sstevel@tonic-gate 	prval("sctpActiveEstab",	sctp->sctpActiveEstab);
22247c478bd9Sstevel@tonic-gate 	prval("sctpPassiveEstab",	sctp->sctpPassiveEstab);
22257c478bd9Sstevel@tonic-gate 	prval("sctpAborted",		sctp->sctpAborted);
22267c478bd9Sstevel@tonic-gate 	prval("sctpShutdowns",		sctp->sctpShutdowns);
22277c478bd9Sstevel@tonic-gate 	prval("sctpOutOfBlue",		sctp->sctpOutOfBlue);
22287c478bd9Sstevel@tonic-gate 	prval("sctpChecksumError",	sctp->sctpChecksumError);
22297c478bd9Sstevel@tonic-gate 	prval64("sctpOutCtrlChunks",	sctp->sctpOutCtrlChunks);
22307c478bd9Sstevel@tonic-gate 	prval64("sctpOutOrderChunks",	sctp->sctpOutOrderChunks);
22317c478bd9Sstevel@tonic-gate 	prval64("sctpOutUnorderChunks",	sctp->sctpOutUnorderChunks);
22327c478bd9Sstevel@tonic-gate 	prval64("sctpRetransChunks",	sctp->sctpRetransChunks);
22337c478bd9Sstevel@tonic-gate 	prval("sctpOutAck",		sctp->sctpOutAck);
22347c478bd9Sstevel@tonic-gate 	prval("sctpOutAckDelayed",	sctp->sctpOutAckDelayed);
22357c478bd9Sstevel@tonic-gate 	prval("sctpOutWinUpdate",	sctp->sctpOutWinUpdate);
22367c478bd9Sstevel@tonic-gate 	prval("sctpOutFastRetrans",	sctp->sctpOutFastRetrans);
22377c478bd9Sstevel@tonic-gate 	prval("sctpOutWinProbe",	sctp->sctpOutWinProbe);
22387c478bd9Sstevel@tonic-gate 	prval64("sctpInCtrlChunks",	sctp->sctpInCtrlChunks);
22397c478bd9Sstevel@tonic-gate 	prval64("sctpInOrderChunks",	sctp->sctpInOrderChunks);
22407c478bd9Sstevel@tonic-gate 	prval64("sctpInUnorderChunks",	sctp->sctpInUnorderChunks);
22417c478bd9Sstevel@tonic-gate 	prval("sctpInAck",		sctp->sctpInAck);
22427c478bd9Sstevel@tonic-gate 	prval("sctpInDupAck",		sctp->sctpInDupAck);
22437c478bd9Sstevel@tonic-gate 	prval("sctpInAckUnsent",	sctp->sctpInAckUnsent);
22447c478bd9Sstevel@tonic-gate 	prval64("sctpFragUsrMsgs",	sctp->sctpFragUsrMsgs);
22457c478bd9Sstevel@tonic-gate 	prval64("sctpReasmUsrMsgs",	sctp->sctpReasmUsrMsgs);
22467c478bd9Sstevel@tonic-gate 	prval64("sctpOutSCTPPkts",	sctp->sctpOutSCTPPkts);
22477c478bd9Sstevel@tonic-gate 	prval64("sctpInSCTPPkts",	sctp->sctpInSCTPPkts);
22487c478bd9Sstevel@tonic-gate 	prval("sctpInInvalidCookie",	sctp->sctpInInvalidCookie);
22497c478bd9Sstevel@tonic-gate 	prval("sctpTimRetrans",		sctp->sctpTimRetrans);
22507c478bd9Sstevel@tonic-gate 	prval("sctpTimRetransDrop",	sctp->sctpTimRetransDrop);
22517c478bd9Sstevel@tonic-gate 	prval("sctpTimHearBeatProbe",	sctp->sctpTimHeartBeatProbe);
22527c478bd9Sstevel@tonic-gate 	prval("sctpTimHearBeatDrop",	sctp->sctpTimHeartBeatDrop);
22537c478bd9Sstevel@tonic-gate 	prval("sctpListenDrop",		sctp->sctpListenDrop);
22547c478bd9Sstevel@tonic-gate 	prval("sctpInClosed",		sctp->sctpInClosed);
22557c478bd9Sstevel@tonic-gate 	prval_end();
22567c478bd9Sstevel@tonic-gate }
22577c478bd9Sstevel@tonic-gate 
22587c478bd9Sstevel@tonic-gate static void
22597c478bd9Sstevel@tonic-gate print_tcp_stats(mib2_tcp_t *tcp)
22607c478bd9Sstevel@tonic-gate {
22617c478bd9Sstevel@tonic-gate 	prval_init();
22627c478bd9Sstevel@tonic-gate 	pr_int_val("tcpRtoAlgorithm",	tcp->tcpRtoAlgorithm);
22637c478bd9Sstevel@tonic-gate 	pr_int_val("tcpRtoMin",		tcp->tcpRtoMin);
22647c478bd9Sstevel@tonic-gate 	pr_int_val("tcpRtoMax",		tcp->tcpRtoMax);
22657c478bd9Sstevel@tonic-gate 	pr_int_val("tcpMaxConn",	tcp->tcpMaxConn);
22667c478bd9Sstevel@tonic-gate 	prval("tcpActiveOpens",		tcp->tcpActiveOpens);
22677c478bd9Sstevel@tonic-gate 	prval("tcpPassiveOpens",	tcp->tcpPassiveOpens);
22687c478bd9Sstevel@tonic-gate 	prval("tcpAttemptFails",	tcp->tcpAttemptFails);
22697c478bd9Sstevel@tonic-gate 	prval("tcpEstabResets",		tcp->tcpEstabResets);
22707c478bd9Sstevel@tonic-gate 	prval("tcpCurrEstab",		tcp->tcpCurrEstab);
22717c478bd9Sstevel@tonic-gate 	prval("tcpOutSegs",		tcp->tcpOutSegs);
22727c478bd9Sstevel@tonic-gate 	prval("tcpOutDataSegs",		tcp->tcpOutDataSegs);
22737c478bd9Sstevel@tonic-gate 	prval("tcpOutDataBytes",	tcp->tcpOutDataBytes);
22747c478bd9Sstevel@tonic-gate 	prval("tcpRetransSegs",		tcp->tcpRetransSegs);
22757c478bd9Sstevel@tonic-gate 	prval("tcpRetransBytes",	tcp->tcpRetransBytes);
22767c478bd9Sstevel@tonic-gate 	prval("tcpOutAck",		tcp->tcpOutAck);
22777c478bd9Sstevel@tonic-gate 	prval("tcpOutAckDelayed",	tcp->tcpOutAckDelayed);
22787c478bd9Sstevel@tonic-gate 	prval("tcpOutUrg",		tcp->tcpOutUrg);
22797c478bd9Sstevel@tonic-gate 	prval("tcpOutWinUpdate",	tcp->tcpOutWinUpdate);
22807c478bd9Sstevel@tonic-gate 	prval("tcpOutWinProbe",		tcp->tcpOutWinProbe);
22817c478bd9Sstevel@tonic-gate 	prval("tcpOutControl",		tcp->tcpOutControl);
22827c478bd9Sstevel@tonic-gate 	prval("tcpOutRsts",		tcp->tcpOutRsts);
22837c478bd9Sstevel@tonic-gate 	prval("tcpOutFastRetrans",	tcp->tcpOutFastRetrans);
22847c478bd9Sstevel@tonic-gate 	prval("tcpInSegs",		tcp->tcpInSegs);
22857c478bd9Sstevel@tonic-gate 	prval_end();
22867c478bd9Sstevel@tonic-gate 	prval("tcpInAckSegs",		tcp->tcpInAckSegs);
22877c478bd9Sstevel@tonic-gate 	prval("tcpInAckBytes",		tcp->tcpInAckBytes);
22887c478bd9Sstevel@tonic-gate 	prval("tcpInDupAck",		tcp->tcpInDupAck);
22897c478bd9Sstevel@tonic-gate 	prval("tcpInAckUnsent",		tcp->tcpInAckUnsent);
22907c478bd9Sstevel@tonic-gate 	prval("tcpInInorderSegs",	tcp->tcpInDataInorderSegs);
22917c478bd9Sstevel@tonic-gate 	prval("tcpInInorderBytes",	tcp->tcpInDataInorderBytes);
22927c478bd9Sstevel@tonic-gate 	prval("tcpInUnorderSegs",	tcp->tcpInDataUnorderSegs);
22937c478bd9Sstevel@tonic-gate 	prval("tcpInUnorderBytes",	tcp->tcpInDataUnorderBytes);
22947c478bd9Sstevel@tonic-gate 	prval("tcpInDupSegs",		tcp->tcpInDataDupSegs);
22957c478bd9Sstevel@tonic-gate 	prval("tcpInDupBytes",		tcp->tcpInDataDupBytes);
22967c478bd9Sstevel@tonic-gate 	prval("tcpInPartDupSegs",	tcp->tcpInDataPartDupSegs);
22977c478bd9Sstevel@tonic-gate 	prval("tcpInPartDupBytes",	tcp->tcpInDataPartDupBytes);
22987c478bd9Sstevel@tonic-gate 	prval("tcpInPastWinSegs",	tcp->tcpInDataPastWinSegs);
22997c478bd9Sstevel@tonic-gate 	prval("tcpInPastWinBytes",	tcp->tcpInDataPastWinBytes);
23007c478bd9Sstevel@tonic-gate 	prval("tcpInWinProbe",		tcp->tcpInWinProbe);
23017c478bd9Sstevel@tonic-gate 	prval("tcpInWinUpdate",		tcp->tcpInWinUpdate);
23027c478bd9Sstevel@tonic-gate 	prval("tcpInClosed",		tcp->tcpInClosed);
23037c478bd9Sstevel@tonic-gate 	prval("tcpRttNoUpdate",		tcp->tcpRttNoUpdate);
23047c478bd9Sstevel@tonic-gate 	prval("tcpRttUpdate",		tcp->tcpRttUpdate);
23057c478bd9Sstevel@tonic-gate 	prval("tcpTimRetrans",		tcp->tcpTimRetrans);
23067c478bd9Sstevel@tonic-gate 	prval("tcpTimRetransDrop",	tcp->tcpTimRetransDrop);
23077c478bd9Sstevel@tonic-gate 	prval("tcpTimKeepalive",	tcp->tcpTimKeepalive);
23087c478bd9Sstevel@tonic-gate 	prval("tcpTimKeepaliveProbe",	tcp->tcpTimKeepaliveProbe);
23097c478bd9Sstevel@tonic-gate 	prval("tcpTimKeepaliveDrop",	tcp->tcpTimKeepaliveDrop);
23107c478bd9Sstevel@tonic-gate 	prval("tcpListenDrop",		tcp->tcpListenDrop);
23117c478bd9Sstevel@tonic-gate 	prval("tcpListenDropQ0",	tcp->tcpListenDropQ0);
23127c478bd9Sstevel@tonic-gate 	prval("tcpHalfOpenDrop",	tcp->tcpHalfOpenDrop);
23137c478bd9Sstevel@tonic-gate 	prval("tcpOutSackRetrans",	tcp->tcpOutSackRetransSegs);
23147c478bd9Sstevel@tonic-gate 	prval_end();
23157c478bd9Sstevel@tonic-gate 
23167c478bd9Sstevel@tonic-gate }
23177c478bd9Sstevel@tonic-gate 
23187c478bd9Sstevel@tonic-gate static void
23197c478bd9Sstevel@tonic-gate print_udp_stats(mib2_udp_t *udp)
23207c478bd9Sstevel@tonic-gate {
23217c478bd9Sstevel@tonic-gate 	prval_init();
23227c478bd9Sstevel@tonic-gate 	prval("udpInDatagrams",		udp->udpInDatagrams);
23237c478bd9Sstevel@tonic-gate 	prval("udpInErrors",		udp->udpInErrors);
23247c478bd9Sstevel@tonic-gate 	prval("udpOutDatagrams",	udp->udpOutDatagrams);
23257c478bd9Sstevel@tonic-gate 	prval("udpOutErrors",		udp->udpOutErrors);
23267c478bd9Sstevel@tonic-gate 	prval_end();
23277c478bd9Sstevel@tonic-gate }
23287c478bd9Sstevel@tonic-gate 
23297c478bd9Sstevel@tonic-gate static void
23307c478bd9Sstevel@tonic-gate print_rawip_stats(mib2_rawip_t *rawip)
23317c478bd9Sstevel@tonic-gate {
23327c478bd9Sstevel@tonic-gate 	prval_init();
23337c478bd9Sstevel@tonic-gate 	prval("rawipInDatagrams",	rawip->rawipInDatagrams);
23347c478bd9Sstevel@tonic-gate 	prval("rawipInErrors",		rawip->rawipInErrors);
23357c478bd9Sstevel@tonic-gate 	prval("rawipInCksumErrs",	rawip->rawipInCksumErrs);
23367c478bd9Sstevel@tonic-gate 	prval("rawipOutDatagrams",	rawip->rawipOutDatagrams);
23377c478bd9Sstevel@tonic-gate 	prval("rawipOutErrors",		rawip->rawipOutErrors);
23387c478bd9Sstevel@tonic-gate 	prval_end();
23397c478bd9Sstevel@tonic-gate }
23407c478bd9Sstevel@tonic-gate 
23417c478bd9Sstevel@tonic-gate void
23427c478bd9Sstevel@tonic-gate print_igmp_stats(struct igmpstat *igps)
23437c478bd9Sstevel@tonic-gate {
23447c478bd9Sstevel@tonic-gate 	(void) printf(" %10u message%s received\n",
23457c478bd9Sstevel@tonic-gate 	    igps->igps_rcv_total, PLURAL(igps->igps_rcv_total));
23467c478bd9Sstevel@tonic-gate 	(void) printf(" %10u message%s received with too few bytes\n",
23477c478bd9Sstevel@tonic-gate 	    igps->igps_rcv_tooshort, PLURAL(igps->igps_rcv_tooshort));
23487c478bd9Sstevel@tonic-gate 	(void) printf(" %10u message%s received with bad checksum\n",
23497c478bd9Sstevel@tonic-gate 	    igps->igps_rcv_badsum, PLURAL(igps->igps_rcv_badsum));
23507c478bd9Sstevel@tonic-gate 	(void) printf(" %10u membership quer%s received\n",
23517c478bd9Sstevel@tonic-gate 	    igps->igps_rcv_queries, PLURALY(igps->igps_rcv_queries));
23527c478bd9Sstevel@tonic-gate 	(void) printf(" %10u membership quer%s received with invalid "
23537c478bd9Sstevel@tonic-gate 	    "field(s)\n",
23547c478bd9Sstevel@tonic-gate 	    igps->igps_rcv_badqueries, PLURALY(igps->igps_rcv_badqueries));
23557c478bd9Sstevel@tonic-gate 	(void) printf(" %10u membership report%s received\n",
23567c478bd9Sstevel@tonic-gate 	    igps->igps_rcv_reports, PLURAL(igps->igps_rcv_reports));
23577c478bd9Sstevel@tonic-gate 	(void) printf(" %10u membership report%s received with invalid "
23587c478bd9Sstevel@tonic-gate 	    "field(s)\n",
23597c478bd9Sstevel@tonic-gate 	    igps->igps_rcv_badreports, PLURAL(igps->igps_rcv_badreports));
23607c478bd9Sstevel@tonic-gate 	(void) printf(" %10u membership report%s received for groups to "
23617c478bd9Sstevel@tonic-gate 	    "which we belong\n",
23627c478bd9Sstevel@tonic-gate 	    igps->igps_rcv_ourreports, PLURAL(igps->igps_rcv_ourreports));
23637c478bd9Sstevel@tonic-gate 	(void) printf(" %10u membership report%s sent\n",
23647c478bd9Sstevel@tonic-gate 	    igps->igps_snd_reports, PLURAL(igps->igps_snd_reports));
23657c478bd9Sstevel@tonic-gate }
23667c478bd9Sstevel@tonic-gate 
23677c478bd9Sstevel@tonic-gate static void
23687c478bd9Sstevel@tonic-gate print_mrt_stats(struct mrtstat *mrts)
23697c478bd9Sstevel@tonic-gate {
23707c478bd9Sstevel@tonic-gate 	(void) puts("DVMRP multicast routing:");
23717c478bd9Sstevel@tonic-gate 	(void) printf(" %10u hit%s - kernel forwarding cache hits\n",
23727c478bd9Sstevel@tonic-gate 		mrts->mrts_mfc_hits, PLURAL(mrts->mrts_mfc_hits));
23737c478bd9Sstevel@tonic-gate 	(void) printf(" %10u miss%s - kernel forwarding cache misses\n",
23747c478bd9Sstevel@tonic-gate 		mrts->mrts_mfc_misses, PLURALES(mrts->mrts_mfc_misses));
23757c478bd9Sstevel@tonic-gate 	(void) printf(" %10u packet%s potentially forwarded\n",
23767c478bd9Sstevel@tonic-gate 		mrts->mrts_fwd_in, PLURAL(mrts->mrts_fwd_in));
23777c478bd9Sstevel@tonic-gate 	(void) printf(" %10u packet%s actually sent out\n",
23787c478bd9Sstevel@tonic-gate 		mrts->mrts_fwd_out, PLURAL(mrts->mrts_fwd_out));
23797c478bd9Sstevel@tonic-gate 	(void) printf(" %10u upcall%s - upcalls made to mrouted\n",
23807c478bd9Sstevel@tonic-gate 		mrts->mrts_upcalls, PLURAL(mrts->mrts_upcalls));
23817c478bd9Sstevel@tonic-gate 	(void) printf(" %10u packet%s not sent out due to lack of resources\n",
23827c478bd9Sstevel@tonic-gate 		mrts->mrts_fwd_drop, PLURAL(mrts->mrts_fwd_drop));
23837c478bd9Sstevel@tonic-gate 	(void) printf(" %10u datagram%s with malformed tunnel options\n",
23847c478bd9Sstevel@tonic-gate 		mrts->mrts_bad_tunnel, PLURAL(mrts->mrts_bad_tunnel));
23857c478bd9Sstevel@tonic-gate 	(void) printf(" %10u datagram%s with no room for tunnel options\n",
23867c478bd9Sstevel@tonic-gate 		mrts->mrts_cant_tunnel, PLURAL(mrts->mrts_cant_tunnel));
23877c478bd9Sstevel@tonic-gate 	(void) printf(" %10u datagram%s arrived on wrong interface\n",
23887c478bd9Sstevel@tonic-gate 		mrts->mrts_wrong_if, PLURAL(mrts->mrts_wrong_if));
23897c478bd9Sstevel@tonic-gate 	(void) printf(" %10u datagram%s dropped due to upcall Q overflow\n",
23907c478bd9Sstevel@tonic-gate 		mrts->mrts_upq_ovflw, PLURAL(mrts->mrts_upq_ovflw));
23917c478bd9Sstevel@tonic-gate 	(void) printf(" %10u datagram%s cleaned up by the cache\n",
23927c478bd9Sstevel@tonic-gate 		mrts->mrts_cache_cleanups, PLURAL(mrts->mrts_cache_cleanups));
23937c478bd9Sstevel@tonic-gate 	(void) printf(" %10u datagram%s dropped selectively by ratelimiter\n",
23947c478bd9Sstevel@tonic-gate 		mrts->mrts_drop_sel, PLURAL(mrts->mrts_drop_sel));
23957c478bd9Sstevel@tonic-gate 	(void) printf(" %10u datagram%s dropped - bucket Q overflow\n",
23967c478bd9Sstevel@tonic-gate 		mrts->mrts_q_overflow, PLURAL(mrts->mrts_q_overflow));
23977c478bd9Sstevel@tonic-gate 	(void) printf(" %10u datagram%s dropped - larger than bkt size\n",
23987c478bd9Sstevel@tonic-gate 		mrts->mrts_pkt2large, PLURAL(mrts->mrts_pkt2large));
23997c478bd9Sstevel@tonic-gate 	(void) printf("\nPIM multicast routing:\n");
24007c478bd9Sstevel@tonic-gate 	(void) printf(" %10u datagram%s dropped - bad version number\n",
24017c478bd9Sstevel@tonic-gate 		mrts->mrts_pim_badversion, PLURAL(mrts->mrts_pim_badversion));
24027c478bd9Sstevel@tonic-gate 	(void) printf(" %10u datagram%s dropped - bad checksum\n",
24037c478bd9Sstevel@tonic-gate 		mrts->mrts_pim_rcv_badcsum, PLURAL(mrts->mrts_pim_rcv_badcsum));
24047c478bd9Sstevel@tonic-gate 	(void) printf(" %10u datagram%s dropped - bad register packets\n",
24057c478bd9Sstevel@tonic-gate 		mrts->mrts_pim_badregisters,
24067c478bd9Sstevel@tonic-gate 		PLURAL(mrts->mrts_pim_badregisters));
24077c478bd9Sstevel@tonic-gate 	(void) printf(
24087c478bd9Sstevel@tonic-gate 		" %10u datagram%s potentially forwarded - register packets\n",
24097c478bd9Sstevel@tonic-gate 		mrts->mrts_pim_regforwards, PLURAL(mrts->mrts_pim_regforwards));
24107c478bd9Sstevel@tonic-gate 	(void) printf(" %10u datagram%s dropped - register send drops\n",
24117c478bd9Sstevel@tonic-gate 		mrts->mrts_pim_regsend_drops,
24127c478bd9Sstevel@tonic-gate 		PLURAL(mrts->mrts_pim_regsend_drops));
24137c478bd9Sstevel@tonic-gate 	(void) printf(" %10u datagram%s dropped - packet malformed\n",
24147c478bd9Sstevel@tonic-gate 		mrts->mrts_pim_malformed, PLURAL(mrts->mrts_pim_malformed));
24157c478bd9Sstevel@tonic-gate 	(void) printf(" %10u datagram%s dropped - no memory to forward\n",
24167c478bd9Sstevel@tonic-gate 		mrts->mrts_pim_nomemory, PLURAL(mrts->mrts_pim_nomemory));
24177c478bd9Sstevel@tonic-gate }
24187c478bd9Sstevel@tonic-gate 
24197c478bd9Sstevel@tonic-gate static void
24207c478bd9Sstevel@tonic-gate sum_ip6_stats(mib2_ipv6IfStatsEntry_t *ip6, mib2_ipv6IfStatsEntry_t *sum6)
24217c478bd9Sstevel@tonic-gate {
24227c478bd9Sstevel@tonic-gate 	/* First few are not additive */
24237c478bd9Sstevel@tonic-gate 	sum6->ipv6Forwarding = ip6->ipv6Forwarding;
24247c478bd9Sstevel@tonic-gate 	sum6->ipv6DefaultHopLimit = ip6->ipv6DefaultHopLimit;
24257c478bd9Sstevel@tonic-gate 
24267c478bd9Sstevel@tonic-gate 	sum6->ipv6InReceives += ip6->ipv6InReceives;
24277c478bd9Sstevel@tonic-gate 	sum6->ipv6InHdrErrors += ip6->ipv6InHdrErrors;
24287c478bd9Sstevel@tonic-gate 	sum6->ipv6InTooBigErrors += ip6->ipv6InTooBigErrors;
24297c478bd9Sstevel@tonic-gate 	sum6->ipv6InNoRoutes += ip6->ipv6InNoRoutes;
24307c478bd9Sstevel@tonic-gate 	sum6->ipv6InAddrErrors += ip6->ipv6InAddrErrors;
24317c478bd9Sstevel@tonic-gate 	sum6->ipv6InUnknownProtos += ip6->ipv6InUnknownProtos;
24327c478bd9Sstevel@tonic-gate 	sum6->ipv6InTruncatedPkts += ip6->ipv6InTruncatedPkts;
24337c478bd9Sstevel@tonic-gate 	sum6->ipv6InDiscards += ip6->ipv6InDiscards;
24347c478bd9Sstevel@tonic-gate 	sum6->ipv6InDelivers += ip6->ipv6InDelivers;
24357c478bd9Sstevel@tonic-gate 	sum6->ipv6OutForwDatagrams += ip6->ipv6OutForwDatagrams;
24367c478bd9Sstevel@tonic-gate 	sum6->ipv6OutRequests += ip6->ipv6OutRequests;
24377c478bd9Sstevel@tonic-gate 	sum6->ipv6OutDiscards += ip6->ipv6OutDiscards;
24387c478bd9Sstevel@tonic-gate 	sum6->ipv6OutFragOKs += ip6->ipv6OutFragOKs;
24397c478bd9Sstevel@tonic-gate 	sum6->ipv6OutFragFails += ip6->ipv6OutFragFails;
24407c478bd9Sstevel@tonic-gate 	sum6->ipv6OutFragCreates += ip6->ipv6OutFragCreates;
24417c478bd9Sstevel@tonic-gate 	sum6->ipv6ReasmReqds += ip6->ipv6ReasmReqds;
24427c478bd9Sstevel@tonic-gate 	sum6->ipv6ReasmOKs += ip6->ipv6ReasmOKs;
24437c478bd9Sstevel@tonic-gate 	sum6->ipv6ReasmFails += ip6->ipv6ReasmFails;
24447c478bd9Sstevel@tonic-gate 	sum6->ipv6InMcastPkts += ip6->ipv6InMcastPkts;
24457c478bd9Sstevel@tonic-gate 	sum6->ipv6OutMcastPkts += ip6->ipv6OutMcastPkts;
24467c478bd9Sstevel@tonic-gate 	sum6->ipv6OutNoRoutes += ip6->ipv6OutNoRoutes;
24477c478bd9Sstevel@tonic-gate 	sum6->ipv6ReasmDuplicates += ip6->ipv6ReasmDuplicates;
24487c478bd9Sstevel@tonic-gate 	sum6->ipv6ReasmPartDups += ip6->ipv6ReasmPartDups;
24497c478bd9Sstevel@tonic-gate 	sum6->ipv6ForwProhibits += ip6->ipv6ForwProhibits;
24507c478bd9Sstevel@tonic-gate 	sum6->udpInCksumErrs += ip6->udpInCksumErrs;
24517c478bd9Sstevel@tonic-gate 	sum6->udpInOverflows += ip6->udpInOverflows;
24527c478bd9Sstevel@tonic-gate 	sum6->rawipInOverflows += ip6->rawipInOverflows;
24537c478bd9Sstevel@tonic-gate }
24547c478bd9Sstevel@tonic-gate 
24557c478bd9Sstevel@tonic-gate static void
24567c478bd9Sstevel@tonic-gate sum_icmp6_stats(mib2_ipv6IfIcmpEntry_t *icmp6, mib2_ipv6IfIcmpEntry_t *sum6)
24577c478bd9Sstevel@tonic-gate {
24587c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInMsgs += icmp6->ipv6IfIcmpInMsgs;
24597c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInErrors += icmp6->ipv6IfIcmpInErrors;
24607c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInDestUnreachs += icmp6->ipv6IfIcmpInDestUnreachs;
24617c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInAdminProhibs += icmp6->ipv6IfIcmpInAdminProhibs;
24627c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInTimeExcds += icmp6->ipv6IfIcmpInTimeExcds;
24637c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInParmProblems += icmp6->ipv6IfIcmpInParmProblems;
24647c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInPktTooBigs += icmp6->ipv6IfIcmpInPktTooBigs;
24657c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInEchos += icmp6->ipv6IfIcmpInEchos;
24667c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInEchoReplies += icmp6->ipv6IfIcmpInEchoReplies;
24677c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInRouterSolicits += icmp6->ipv6IfIcmpInRouterSolicits;
24687c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInRouterAdvertisements +=
24697c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpInRouterAdvertisements;
24707c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInNeighborSolicits +=
24717c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpInNeighborSolicits;
24727c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInNeighborAdvertisements +=
24737c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpInNeighborAdvertisements;
24747c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInRedirects += icmp6->ipv6IfIcmpInRedirects;
24757c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInGroupMembQueries +=
24767c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpInGroupMembQueries;
24777c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInGroupMembResponses +=
24787c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpInGroupMembResponses;
24797c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInGroupMembReductions +=
24807c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpInGroupMembReductions;
24817c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutMsgs += icmp6->ipv6IfIcmpOutMsgs;
24827c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutErrors += icmp6->ipv6IfIcmpOutErrors;
24837c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutDestUnreachs += icmp6->ipv6IfIcmpOutDestUnreachs;
24847c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutAdminProhibs += icmp6->ipv6IfIcmpOutAdminProhibs;
24857c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutTimeExcds += icmp6->ipv6IfIcmpOutTimeExcds;
24867c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutParmProblems += icmp6->ipv6IfIcmpOutParmProblems;
24877c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutPktTooBigs += icmp6->ipv6IfIcmpOutPktTooBigs;
24887c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutEchos += icmp6->ipv6IfIcmpOutEchos;
24897c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutEchoReplies += icmp6->ipv6IfIcmpOutEchoReplies;
24907c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutRouterSolicits +=
24917c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpOutRouterSolicits;
24927c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutRouterAdvertisements +=
24937c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpOutRouterAdvertisements;
24947c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutNeighborSolicits +=
24957c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpOutNeighborSolicits;
24967c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutNeighborAdvertisements +=
24977c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpOutNeighborAdvertisements;
24987c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutRedirects += icmp6->ipv6IfIcmpOutRedirects;
24997c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutGroupMembQueries +=
25007c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpOutGroupMembQueries;
25017c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutGroupMembResponses +=
25027c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpOutGroupMembResponses;
25037c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutGroupMembReductions +=
25047c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpOutGroupMembReductions;
25057c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInOverflows += icmp6->ipv6IfIcmpInOverflows;
25067c478bd9Sstevel@tonic-gate }
25077c478bd9Sstevel@tonic-gate 
25087c478bd9Sstevel@tonic-gate /* ----------------------------- MRT_STAT_REPORT --------------------------- */
25097c478bd9Sstevel@tonic-gate 
25107c478bd9Sstevel@tonic-gate static void
25117c478bd9Sstevel@tonic-gate mrt_stat_report(mib_item_t *curritem)
25127c478bd9Sstevel@tonic-gate {
25137c478bd9Sstevel@tonic-gate 	int	jtemp = 0;
25147c478bd9Sstevel@tonic-gate 	mib_item_t *tempitem;
25157c478bd9Sstevel@tonic-gate 
25167c478bd9Sstevel@tonic-gate 	if (!(family_selected(AF_INET)))
25177c478bd9Sstevel@tonic-gate 		return;
25187c478bd9Sstevel@tonic-gate 
25197c478bd9Sstevel@tonic-gate 	(void) putchar('\n');
25207c478bd9Sstevel@tonic-gate 	/* 'for' loop 1: */
25217c478bd9Sstevel@tonic-gate 	for (tempitem = curritem;
25227c478bd9Sstevel@tonic-gate 	    tempitem;
25237c478bd9Sstevel@tonic-gate 	    tempitem = tempitem->next_item) {
25247c478bd9Sstevel@tonic-gate 		if (Dflag) {
25257c478bd9Sstevel@tonic-gate 			(void) printf("\n--- Entry %d ---\n", ++jtemp);
25267c478bd9Sstevel@tonic-gate 			(void) printf("Group = %d, mib_id = %d, "
25277c478bd9Sstevel@tonic-gate 			    "length = %d, valp = 0x%p\n",
25287c478bd9Sstevel@tonic-gate 			    tempitem->group, tempitem->mib_id,
25297c478bd9Sstevel@tonic-gate 			    tempitem->length, tempitem->valp);
25307c478bd9Sstevel@tonic-gate 		}
25317c478bd9Sstevel@tonic-gate 
25327c478bd9Sstevel@tonic-gate 		if (tempitem->mib_id == 0) {
25337c478bd9Sstevel@tonic-gate 			switch (tempitem->group) {
25347c478bd9Sstevel@tonic-gate 			case EXPER_DVMRP: {
25357c478bd9Sstevel@tonic-gate 				struct mrtstat	*mrts;
25367c478bd9Sstevel@tonic-gate 				mrts = (struct mrtstat *)tempitem->valp;
25377c478bd9Sstevel@tonic-gate 
25387c478bd9Sstevel@tonic-gate 				if (!(family_selected(AF_INET)))
25397c478bd9Sstevel@tonic-gate 					continue; /* 'for' loop 1 */
25407c478bd9Sstevel@tonic-gate 
25417c478bd9Sstevel@tonic-gate 				print_mrt_stats(mrts);
25427c478bd9Sstevel@tonic-gate 				break;
25437c478bd9Sstevel@tonic-gate 			}
25447c478bd9Sstevel@tonic-gate 			}
25457c478bd9Sstevel@tonic-gate 		}
25467c478bd9Sstevel@tonic-gate 	} /* 'for' loop 1 ends */
25477c478bd9Sstevel@tonic-gate 	(void) putchar('\n');
25487c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
25497c478bd9Sstevel@tonic-gate }
25507c478bd9Sstevel@tonic-gate 
25517c478bd9Sstevel@tonic-gate /*
25527c478bd9Sstevel@tonic-gate  * if_stat_total() - Computes totals for interface statistics
25537c478bd9Sstevel@tonic-gate  *                   and returns result by updating sumstats.
25547c478bd9Sstevel@tonic-gate  */
25557c478bd9Sstevel@tonic-gate static void
25567c478bd9Sstevel@tonic-gate if_stat_total(struct ifstat *oldstats, struct ifstat *newstats,
25577c478bd9Sstevel@tonic-gate     struct ifstat *sumstats)
25587c478bd9Sstevel@tonic-gate {
25597c478bd9Sstevel@tonic-gate 	sumstats->ipackets += newstats->ipackets - oldstats->ipackets;
25607c478bd9Sstevel@tonic-gate 	sumstats->opackets += newstats->opackets - oldstats->opackets;
25617c478bd9Sstevel@tonic-gate 	sumstats->ierrors += newstats->ierrors - oldstats->ierrors;
25627c478bd9Sstevel@tonic-gate 	sumstats->oerrors += newstats->oerrors - oldstats->oerrors;
25637c478bd9Sstevel@tonic-gate 	sumstats->collisions += newstats->collisions - oldstats->collisions;
25647c478bd9Sstevel@tonic-gate }
25657c478bd9Sstevel@tonic-gate 
25667c478bd9Sstevel@tonic-gate /* --------------------- IF_REPORT (netstat -i)  -------------------------- */
25677c478bd9Sstevel@tonic-gate 
25687c478bd9Sstevel@tonic-gate static struct	ifstat	zerostat = {
25697c478bd9Sstevel@tonic-gate 	0LL, 0LL, 0LL, 0LL, 0LL
25707c478bd9Sstevel@tonic-gate };
25717c478bd9Sstevel@tonic-gate 
25727c478bd9Sstevel@tonic-gate static void
25737c478bd9Sstevel@tonic-gate if_report(mib_item_t *item, char *matchname,
25747c478bd9Sstevel@tonic-gate     int Iflag_only, boolean_t once_only)
25757c478bd9Sstevel@tonic-gate {
25767c478bd9Sstevel@tonic-gate 	static boolean_t	reentry = B_FALSE;
25777c478bd9Sstevel@tonic-gate 	boolean_t		alreadydone = B_FALSE;
25787c478bd9Sstevel@tonic-gate 	int			jtemp = 0;
25797c478bd9Sstevel@tonic-gate 	uint32_t		ifindex_v4 = 0;
25807c478bd9Sstevel@tonic-gate 	uint32_t		ifindex_v6 = 0;
25817c478bd9Sstevel@tonic-gate 
25827c478bd9Sstevel@tonic-gate 	/* 'for' loop 1: */
25837c478bd9Sstevel@tonic-gate 	for (; item; item = item->next_item) {
25847c478bd9Sstevel@tonic-gate 		if (Dflag) {
25857c478bd9Sstevel@tonic-gate 			(void) printf("\n--- Entry %d ---\n", ++jtemp);
25867c478bd9Sstevel@tonic-gate 			(void) printf("Group = %d, mib_id = %d, "
25877c478bd9Sstevel@tonic-gate 			    "length = %d, valp = 0x%p\n",
25887c478bd9Sstevel@tonic-gate 			    item->group, item->mib_id, item->length,
25897c478bd9Sstevel@tonic-gate 			    item->valp);
25907c478bd9Sstevel@tonic-gate 		}
25917c478bd9Sstevel@tonic-gate 
25927c478bd9Sstevel@tonic-gate 		switch (item->group) {
25937c478bd9Sstevel@tonic-gate 		case MIB2_IP:
25947c478bd9Sstevel@tonic-gate 		if (item->mib_id != MIB2_IP_ADDR ||
25957c478bd9Sstevel@tonic-gate 		    !family_selected(AF_INET))
25967c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
25977c478bd9Sstevel@tonic-gate 		{
25987c478bd9Sstevel@tonic-gate 			static struct ifstat	old = {0L, 0L, 0L, 0L, 0L};
25997c478bd9Sstevel@tonic-gate 			static struct ifstat	new = {0L, 0L, 0L, 0L, 0L};
26007c478bd9Sstevel@tonic-gate 			struct ifstat		sum;
26017c478bd9Sstevel@tonic-gate 			struct iflist		*newlist = NULL;
26027c478bd9Sstevel@tonic-gate 			static struct iflist	*oldlist = NULL;
26037c478bd9Sstevel@tonic-gate 			kstat_t	 *ksp;
26047c478bd9Sstevel@tonic-gate 
26057c478bd9Sstevel@tonic-gate 			if (once_only) {
26067c478bd9Sstevel@tonic-gate 				char    ifname[LIFNAMSIZ + 1];
26077c478bd9Sstevel@tonic-gate 				char    logintname[LIFNAMSIZ + 1];
26087c478bd9Sstevel@tonic-gate 				mib2_ipAddrEntry_t *ap;
26097c478bd9Sstevel@tonic-gate 				struct ifstat	stat = {0L, 0L, 0L, 0L, 0L};
26107c478bd9Sstevel@tonic-gate 				boolean_t	first = B_TRUE;
26117c478bd9Sstevel@tonic-gate 				uint32_t	new_ifindex;
26127c478bd9Sstevel@tonic-gate 
26137c478bd9Sstevel@tonic-gate 				if (Dflag)
26147c478bd9Sstevel@tonic-gate 					(void) printf("if_report: %d items\n",
26157c478bd9Sstevel@tonic-gate 					    (item->length)
26167c478bd9Sstevel@tonic-gate 					    / sizeof (mib2_ipAddrEntry_t));
26177c478bd9Sstevel@tonic-gate 
26187c478bd9Sstevel@tonic-gate 				/* 'for' loop 2a: */
26197c478bd9Sstevel@tonic-gate 				for (ap = (mib2_ipAddrEntry_t *)item->valp;
26207c478bd9Sstevel@tonic-gate 				    (char *)ap < (char *)item->valp
26217c478bd9Sstevel@tonic-gate 				    + item->length;
26227c478bd9Sstevel@tonic-gate 				    ap++) {
26237c478bd9Sstevel@tonic-gate 					(void) octetstr(&ap->ipAdEntIfIndex,
26247c478bd9Sstevel@tonic-gate 					    'a', logintname,
26257c478bd9Sstevel@tonic-gate 					    sizeof (logintname));
26267c478bd9Sstevel@tonic-gate 					(void) strcpy(ifname, logintname);
26277c478bd9Sstevel@tonic-gate 					(void) strtok(ifname, ":");
26287c478bd9Sstevel@tonic-gate 					if (matchname != NULL &&
26297c478bd9Sstevel@tonic-gate 					    strcmp(matchname, ifname) != 0 &&
26307c478bd9Sstevel@tonic-gate 					    strcmp(matchname, logintname) != 0)
26317c478bd9Sstevel@tonic-gate 						continue; /* 'for' loop 2a */
26327c478bd9Sstevel@tonic-gate 					new_ifindex =
26337c478bd9Sstevel@tonic-gate 					    if_nametoindex(logintname);
26347c478bd9Sstevel@tonic-gate 					if (new_ifindex != ifindex_v4 &&
26357c478bd9Sstevel@tonic-gate 					    (ksp = kstat_lookup(kc, NULL, -1,
26367c478bd9Sstevel@tonic-gate 					    ifname)) != NULL) {
26377c478bd9Sstevel@tonic-gate 						(void) safe_kstat_read(kc, ksp,
26387c478bd9Sstevel@tonic-gate 						    NULL);
26397c478bd9Sstevel@tonic-gate 						stat.ipackets =
26407c478bd9Sstevel@tonic-gate 						    kstat_named_value(ksp,
26417c478bd9Sstevel@tonic-gate 						    "ipackets");
26427c478bd9Sstevel@tonic-gate 						stat.ierrors =
26437c478bd9Sstevel@tonic-gate 						    kstat_named_value(ksp,
26447c478bd9Sstevel@tonic-gate 						    "ierrors");
26457c478bd9Sstevel@tonic-gate 						stat.opackets =
26467c478bd9Sstevel@tonic-gate 						    kstat_named_value(ksp,
26477c478bd9Sstevel@tonic-gate 						    "opackets");
26487c478bd9Sstevel@tonic-gate 						stat.oerrors =
26497c478bd9Sstevel@tonic-gate 						    kstat_named_value(ksp,
26507c478bd9Sstevel@tonic-gate 						    "oerrors");
26517c478bd9Sstevel@tonic-gate 						stat.collisions =
26527c478bd9Sstevel@tonic-gate 						    kstat_named_value(ksp,
26537c478bd9Sstevel@tonic-gate 						    "collisions");
26547c478bd9Sstevel@tonic-gate 						if (first) {
26557c478bd9Sstevel@tonic-gate 						(void) printf(
26567c478bd9Sstevel@tonic-gate 						    "%-5.5s %-5.5s%-13.13s "
26577c478bd9Sstevel@tonic-gate 						    "%-14.14s %-6.6s %-5.5s "
26587c478bd9Sstevel@tonic-gate 						    "%-6.6s %-5.5s %-6.6s "
26597c478bd9Sstevel@tonic-gate 						    "%-6.6s\n",
26607c478bd9Sstevel@tonic-gate 						    "Name", "Mtu", "Net/Dest",
26617c478bd9Sstevel@tonic-gate 						    "Address", "Ipkts",
26627c478bd9Sstevel@tonic-gate 						    "Ierrs", "Opkts", "Oerrs",
26637c478bd9Sstevel@tonic-gate 						    "Collis", "Queue");
26647c478bd9Sstevel@tonic-gate 						first = B_FALSE;
26657c478bd9Sstevel@tonic-gate 						}
26667c478bd9Sstevel@tonic-gate 						if_report_ip4(ap, ifname,
26677c478bd9Sstevel@tonic-gate 						    logintname, &stat, B_TRUE);
26687c478bd9Sstevel@tonic-gate 						ifindex_v4 = new_ifindex;
26697c478bd9Sstevel@tonic-gate 					} else {
26707c478bd9Sstevel@tonic-gate 						if_report_ip4(ap, ifname,
26717c478bd9Sstevel@tonic-gate 						    logintname, &stat, B_FALSE);
26727c478bd9Sstevel@tonic-gate 					}
26737c478bd9Sstevel@tonic-gate 				} /* 'for' loop 2a ends */
26747c478bd9Sstevel@tonic-gate 				if (!first)
26757c478bd9Sstevel@tonic-gate 					(void) putchar('\n');
26767c478bd9Sstevel@tonic-gate 			} else if (!alreadydone) {
26777c478bd9Sstevel@tonic-gate 				char    ifname[LIFNAMSIZ + 1];
26787c478bd9Sstevel@tonic-gate 				char    buf[LIFNAMSIZ + 1];
26797c478bd9Sstevel@tonic-gate 				mib2_ipAddrEntry_t *ap;
26807c478bd9Sstevel@tonic-gate 				struct ifstat   t;
26817c478bd9Sstevel@tonic-gate 				struct iflist	*tlp;
26827c478bd9Sstevel@tonic-gate 				struct iflist	**nextnew = &newlist;
26837c478bd9Sstevel@tonic-gate 				struct iflist	*walkold;
26847c478bd9Sstevel@tonic-gate 				struct iflist	*cleanlist;
26857c478bd9Sstevel@tonic-gate 
26867c478bd9Sstevel@tonic-gate 				alreadydone = B_TRUE; /* ignore other case */
26877c478bd9Sstevel@tonic-gate 				/*
26887c478bd9Sstevel@tonic-gate 				 * 'for' loop 2b: find the "right" entry
26897c478bd9Sstevel@tonic-gate 				 */
26907c478bd9Sstevel@tonic-gate 				for (ap = (mib2_ipAddrEntry_t *)item->valp;
26917c478bd9Sstevel@tonic-gate 				    (char *)ap < (char *)item->valp
26927c478bd9Sstevel@tonic-gate 				    + item->length;
26937c478bd9Sstevel@tonic-gate 				    ap++) {
26947c478bd9Sstevel@tonic-gate 					(void) octetstr(&ap->ipAdEntIfIndex,
26957c478bd9Sstevel@tonic-gate 					'a', ifname, sizeof (ifname));
26967c478bd9Sstevel@tonic-gate 					(void) strtok(ifname, ":");
26977c478bd9Sstevel@tonic-gate 
26987c478bd9Sstevel@tonic-gate 					if (matchname) {
26997c478bd9Sstevel@tonic-gate 						if (strcmp(matchname,
27007c478bd9Sstevel@tonic-gate 						    ifname) == 0)
27017c478bd9Sstevel@tonic-gate 							/* 'for' loop 2b */
27027c478bd9Sstevel@tonic-gate 							break;
27037c478bd9Sstevel@tonic-gate 					} else if (strcmp(ifname, "lo0") != 0) {
27047c478bd9Sstevel@tonic-gate 						matchname = ifname;
27057c478bd9Sstevel@tonic-gate 						break; /* 'for' loop 2b */
27067c478bd9Sstevel@tonic-gate 					}
27077c478bd9Sstevel@tonic-gate 				} /* 'for' loop 2b ends */
27087c478bd9Sstevel@tonic-gate 
27097c478bd9Sstevel@tonic-gate 				if (Iflag_only == 0 || !reentry) {
27107c478bd9Sstevel@tonic-gate 					(void) printf("    input   %-6.6s    "
27117c478bd9Sstevel@tonic-gate 					    "output	",
27127c478bd9Sstevel@tonic-gate 					    matchname);
27137c478bd9Sstevel@tonic-gate 					(void) printf("   input  (Total)    "
27147c478bd9Sstevel@tonic-gate 					"output\n");
27157c478bd9Sstevel@tonic-gate 					(void) printf("%-7.7s %-5.5s %-7.7s "
27167c478bd9Sstevel@tonic-gate 					    "%-5.5s %-6.6s ",
27177c478bd9Sstevel@tonic-gate 					    "packets", "errs", "packets",
27187c478bd9Sstevel@tonic-gate 					    "errs", "colls");
27197c478bd9Sstevel@tonic-gate 					(void) printf("%-7.7s %-5.5s %-7.7s "
27207c478bd9Sstevel@tonic-gate 					    "%-5.5s %-6.6s\n",
27217c478bd9Sstevel@tonic-gate 					    "packets", "errs", "packets",
27227c478bd9Sstevel@tonic-gate 					    "errs", "colls");
27237c478bd9Sstevel@tonic-gate 				}
27247c478bd9Sstevel@tonic-gate 
27257c478bd9Sstevel@tonic-gate 				sum = zerostat;
27267c478bd9Sstevel@tonic-gate 
27277c478bd9Sstevel@tonic-gate 				/* 'for' loop 2c: */
27287c478bd9Sstevel@tonic-gate 				for (ap = (mib2_ipAddrEntry_t *)item->valp;
27297c478bd9Sstevel@tonic-gate 				    (char *)ap < (char *)item->valp
27307c478bd9Sstevel@tonic-gate 				    + item->length;
27317c478bd9Sstevel@tonic-gate 				    ap++) {
27327c478bd9Sstevel@tonic-gate 					(void) octetstr(&ap->ipAdEntIfIndex,
27337c478bd9Sstevel@tonic-gate 					    'a', buf, sizeof (buf));
27347c478bd9Sstevel@tonic-gate 					(void) strtok(buf, ":");
27357c478bd9Sstevel@tonic-gate 					ksp = kstat_lookup(kc, NULL, -1, buf);
27367c478bd9Sstevel@tonic-gate 					if (ksp &&
27377c478bd9Sstevel@tonic-gate 					    ksp->ks_type == KSTAT_TYPE_NAMED)
27387c478bd9Sstevel@tonic-gate 						(void) safe_kstat_read(kc, ksp,
27397c478bd9Sstevel@tonic-gate 						    NULL);
27407c478bd9Sstevel@tonic-gate 
27417c478bd9Sstevel@tonic-gate 					t.ipackets = kstat_named_value(ksp,
27427c478bd9Sstevel@tonic-gate 					    "ipackets");
27437c478bd9Sstevel@tonic-gate 					t.ierrors = kstat_named_value(ksp,
27447c478bd9Sstevel@tonic-gate 					    "ierrors");
27457c478bd9Sstevel@tonic-gate 					t.opackets = kstat_named_value(ksp,
27467c478bd9Sstevel@tonic-gate 					    "opackets");
27477c478bd9Sstevel@tonic-gate 					t.oerrors = kstat_named_value(ksp,
27487c478bd9Sstevel@tonic-gate 					    "oerrors");
27497c478bd9Sstevel@tonic-gate 					t.collisions = kstat_named_value(ksp,
27507c478bd9Sstevel@tonic-gate 					    "collisions");
27517c478bd9Sstevel@tonic-gate 
27527c478bd9Sstevel@tonic-gate 					if (strcmp(buf, matchname) == 0)
27537c478bd9Sstevel@tonic-gate 						new = t;
27547c478bd9Sstevel@tonic-gate 
27557c478bd9Sstevel@tonic-gate 					/* Build the interface list */
27567c478bd9Sstevel@tonic-gate 
27577c478bd9Sstevel@tonic-gate 					tlp = malloc(sizeof (struct iflist));
27587c478bd9Sstevel@tonic-gate 					(void) strlcpy(tlp->ifname, buf,
27597c478bd9Sstevel@tonic-gate 					    sizeof (tlp->ifname));
27607c478bd9Sstevel@tonic-gate 					tlp->tot = t;
27617c478bd9Sstevel@tonic-gate 					*nextnew = tlp;
27627c478bd9Sstevel@tonic-gate 					nextnew = &tlp->next_if;
27637c478bd9Sstevel@tonic-gate 
27647c478bd9Sstevel@tonic-gate 					/*
27657c478bd9Sstevel@tonic-gate 					 * First time through.
27667c478bd9Sstevel@tonic-gate 					 * Just add up the interface stats.
27677c478bd9Sstevel@tonic-gate 					 */
27687c478bd9Sstevel@tonic-gate 
27697c478bd9Sstevel@tonic-gate 					if (oldlist == NULL) {
27707c478bd9Sstevel@tonic-gate 						if_stat_total(&zerostat,
27717c478bd9Sstevel@tonic-gate 						    &t, &sum);
27727c478bd9Sstevel@tonic-gate 						continue;
27737c478bd9Sstevel@tonic-gate 					}
27747c478bd9Sstevel@tonic-gate 
27757c478bd9Sstevel@tonic-gate 					/*
27767c478bd9Sstevel@tonic-gate 					 * Walk old list for the interface.
27777c478bd9Sstevel@tonic-gate 					 *
27787c478bd9Sstevel@tonic-gate 					 * If found, add difference to total.
27797c478bd9Sstevel@tonic-gate 					 *
27807c478bd9Sstevel@tonic-gate 					 * If not, an interface has been plumbed
27817c478bd9Sstevel@tonic-gate 					 * up.  In this case, we will simply
27827c478bd9Sstevel@tonic-gate 					 * ignore the new interface until the
27837c478bd9Sstevel@tonic-gate 					 * next interval; as there's no easy way
27847c478bd9Sstevel@tonic-gate 					 * to acquire statistics between time
27857c478bd9Sstevel@tonic-gate 					 * of the plumb and the next interval
27867c478bd9Sstevel@tonic-gate 					 * boundary.  This results in inaccurate
27877c478bd9Sstevel@tonic-gate 					 * total values for current interval.
27887c478bd9Sstevel@tonic-gate 					 *
27897c478bd9Sstevel@tonic-gate 					 * Note the case when an interface is
27907c478bd9Sstevel@tonic-gate 					 * unplumbed; as similar problems exist.
27917c478bd9Sstevel@tonic-gate 					 * The unplumbed interface is not in the
27927c478bd9Sstevel@tonic-gate 					 * current list, and there's no easy way
27937c478bd9Sstevel@tonic-gate 					 * to account for the statistics between
27947c478bd9Sstevel@tonic-gate 					 * the previous interval and time of the
27957c478bd9Sstevel@tonic-gate 					 * unplumb.  Therefore, we (in a sense)
27967c478bd9Sstevel@tonic-gate 					 * ignore the removed interface by only
27977c478bd9Sstevel@tonic-gate 					 * involving "current" interfaces when
27987c478bd9Sstevel@tonic-gate 					 * computing the total statistics.
27997c478bd9Sstevel@tonic-gate 					 * Unfortunately, this also results in
28007c478bd9Sstevel@tonic-gate 					 * inaccurate values for interval total.
28017c478bd9Sstevel@tonic-gate 					 */
28027c478bd9Sstevel@tonic-gate 
28037c478bd9Sstevel@tonic-gate 					for (walkold = oldlist;
28047c478bd9Sstevel@tonic-gate 					    walkold != NULL;
28057c478bd9Sstevel@tonic-gate 					    walkold = walkold->next_if) {
28067c478bd9Sstevel@tonic-gate 						if (strcmp(walkold->ifname,
28077c478bd9Sstevel@tonic-gate 						    buf) == 0) {
28087c478bd9Sstevel@tonic-gate 							if_stat_total(
28097c478bd9Sstevel@tonic-gate 							    &walkold->tot,
28107c478bd9Sstevel@tonic-gate 							    &t, &sum);
28117c478bd9Sstevel@tonic-gate 							break;
28127c478bd9Sstevel@tonic-gate 						}
28137c478bd9Sstevel@tonic-gate 					}
28147c478bd9Sstevel@tonic-gate 
28157c478bd9Sstevel@tonic-gate 				} /* 'for' loop 2c ends */
28167c478bd9Sstevel@tonic-gate 
28177c478bd9Sstevel@tonic-gate 				*nextnew = NULL;
28187c478bd9Sstevel@tonic-gate 
28197c478bd9Sstevel@tonic-gate 				(void) printf("%-7llu %-5llu %-7llu "
28207c478bd9Sstevel@tonic-gate 				    "%-5llu %-6llu ",
28217c478bd9Sstevel@tonic-gate 				    new.ipackets - old.ipackets,
28227c478bd9Sstevel@tonic-gate 				    new.ierrors - old.ierrors,
28237c478bd9Sstevel@tonic-gate 				    new.opackets - old.opackets,
28247c478bd9Sstevel@tonic-gate 				    new.oerrors - old.oerrors,
28257c478bd9Sstevel@tonic-gate 				    new.collisions - old.collisions);
28267c478bd9Sstevel@tonic-gate 
28277c478bd9Sstevel@tonic-gate 				(void) printf("%-7llu %-5llu %-7llu "
28287c478bd9Sstevel@tonic-gate 				    "%-5llu %-6llu\n", sum.ipackets,
28297c478bd9Sstevel@tonic-gate 				    sum.ierrors, sum.opackets,
28307c478bd9Sstevel@tonic-gate 				    sum.oerrors, sum.collisions);
28317c478bd9Sstevel@tonic-gate 
28327c478bd9Sstevel@tonic-gate 				/*
28337c478bd9Sstevel@tonic-gate 				 * Tidy things up once finished.
28347c478bd9Sstevel@tonic-gate 				 */
28357c478bd9Sstevel@tonic-gate 
28367c478bd9Sstevel@tonic-gate 				old = new;
28377c478bd9Sstevel@tonic-gate 				cleanlist = oldlist;
28387c478bd9Sstevel@tonic-gate 				oldlist = newlist;
28397c478bd9Sstevel@tonic-gate 				while (cleanlist != NULL) {
28407c478bd9Sstevel@tonic-gate 					tlp = cleanlist->next_if;
28417c478bd9Sstevel@tonic-gate 					free(cleanlist);
28427c478bd9Sstevel@tonic-gate 					cleanlist = tlp;
28437c478bd9Sstevel@tonic-gate 				}
28447c478bd9Sstevel@tonic-gate 			}
28457c478bd9Sstevel@tonic-gate 			break;
28467c478bd9Sstevel@tonic-gate 		}
28477c478bd9Sstevel@tonic-gate 		case MIB2_IP6:
28487c478bd9Sstevel@tonic-gate 		if (item->mib_id != MIB2_IP6_ADDR ||
28497c478bd9Sstevel@tonic-gate 		    !family_selected(AF_INET6))
28507c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
28517c478bd9Sstevel@tonic-gate 		{
28527c478bd9Sstevel@tonic-gate 			static struct ifstat	old6 = {0L, 0L, 0L, 0L, 0L};
28537c478bd9Sstevel@tonic-gate 			static struct ifstat	new6 = {0L, 0L, 0L, 0L, 0L};
28547c478bd9Sstevel@tonic-gate 			struct ifstat		sum6;
28557c478bd9Sstevel@tonic-gate 			struct iflist		*newlist6 = NULL;
28567c478bd9Sstevel@tonic-gate 			static struct iflist	*oldlist6 = NULL;
28577c478bd9Sstevel@tonic-gate 			kstat_t	 *ksp;
28587c478bd9Sstevel@tonic-gate 
28597c478bd9Sstevel@tonic-gate 			if (once_only) {
28607c478bd9Sstevel@tonic-gate 				char    ifname[LIFNAMSIZ + 1];
28617c478bd9Sstevel@tonic-gate 				char    logintname[LIFNAMSIZ + 1];
28627c478bd9Sstevel@tonic-gate 				mib2_ipv6AddrEntry_t *ap6;
28637c478bd9Sstevel@tonic-gate 				struct ifstat	stat = {0L, 0L, 0L, 0L, 0L};
28647c478bd9Sstevel@tonic-gate 				boolean_t	first = B_TRUE;
28657c478bd9Sstevel@tonic-gate 				uint32_t	new_ifindex;
28667c478bd9Sstevel@tonic-gate 
28677c478bd9Sstevel@tonic-gate 				if (Dflag)
28687c478bd9Sstevel@tonic-gate 					(void) printf("if_report: %d items\n",
28697c478bd9Sstevel@tonic-gate 					    (item->length)
28707c478bd9Sstevel@tonic-gate 					    / sizeof (mib2_ipv6AddrEntry_t));
28717c478bd9Sstevel@tonic-gate 				/* 'for' loop 2d: */
28727c478bd9Sstevel@tonic-gate 				for (ap6 = (mib2_ipv6AddrEntry_t *)item->valp;
28737c478bd9Sstevel@tonic-gate 				    (char *)ap6 < (char *)item->valp
28747c478bd9Sstevel@tonic-gate 				    + item->length;
28757c478bd9Sstevel@tonic-gate 				    ap6++) {
28767c478bd9Sstevel@tonic-gate 					(void) octetstr(&ap6->ipv6AddrIfIndex,
28777c478bd9Sstevel@tonic-gate 					    'a', logintname,
28787c478bd9Sstevel@tonic-gate 					    sizeof (logintname));
28797c478bd9Sstevel@tonic-gate 					(void) strcpy(ifname, logintname);
28807c478bd9Sstevel@tonic-gate 					(void) strtok(ifname, ":");
28817c478bd9Sstevel@tonic-gate 					if (matchname != NULL &&
28827c478bd9Sstevel@tonic-gate 					    strcmp(matchname, ifname) != 0 &&
28837c478bd9Sstevel@tonic-gate 					    strcmp(matchname, logintname) != 0)
28847c478bd9Sstevel@tonic-gate 						continue; /* 'for' loop 2d */
28857c478bd9Sstevel@tonic-gate 					new_ifindex =
28867c478bd9Sstevel@tonic-gate 					    if_nametoindex(logintname);
28877c478bd9Sstevel@tonic-gate 					if (new_ifindex != ifindex_v6 &&
28887c478bd9Sstevel@tonic-gate 					    (ksp = kstat_lookup(kc, NULL, -1,
28897c478bd9Sstevel@tonic-gate 					    ifname)) != NULL) {
28907c478bd9Sstevel@tonic-gate 						(void) safe_kstat_read(kc, ksp,
28917c478bd9Sstevel@tonic-gate 						    NULL);
28927c478bd9Sstevel@tonic-gate 						stat.ipackets =
28937c478bd9Sstevel@tonic-gate 						    kstat_named_value(ksp,
28947c478bd9Sstevel@tonic-gate 						    "ipackets");
28957c478bd9Sstevel@tonic-gate 						stat.ierrors =
28967c478bd9Sstevel@tonic-gate 						    kstat_named_value(ksp,
28977c478bd9Sstevel@tonic-gate 						    "ierrors");
28987c478bd9Sstevel@tonic-gate 						stat.opackets =
28997c478bd9Sstevel@tonic-gate 						    kstat_named_value(ksp,
29007c478bd9Sstevel@tonic-gate 						    "opackets");
29017c478bd9Sstevel@tonic-gate 						stat.oerrors =
29027c478bd9Sstevel@tonic-gate 						    kstat_named_value(ksp,
29037c478bd9Sstevel@tonic-gate 						    "oerrors");
29047c478bd9Sstevel@tonic-gate 						stat.collisions =
29057c478bd9Sstevel@tonic-gate 						    kstat_named_value(ksp,
29067c478bd9Sstevel@tonic-gate 						    "collisions");
29077c478bd9Sstevel@tonic-gate 						if (first) {
29087c478bd9Sstevel@tonic-gate 							(void) printf(
29097c478bd9Sstevel@tonic-gate 							    "%-5.5s %-5.5s%"
29107c478bd9Sstevel@tonic-gate 							    "-27.27s %-27.27s "
29117c478bd9Sstevel@tonic-gate 							    "%-6.6s %-5.5s "
29127c478bd9Sstevel@tonic-gate 							    "%-6.6s %-5.5s "
29137c478bd9Sstevel@tonic-gate 							    "%-6.6s\n",
29147c478bd9Sstevel@tonic-gate 							    "Name", "Mtu",
29157c478bd9Sstevel@tonic-gate 							    "Net/Dest",
29167c478bd9Sstevel@tonic-gate 							    "Address", "Ipkts",
29177c478bd9Sstevel@tonic-gate 							    "Ierrs", "Opkts",
29187c478bd9Sstevel@tonic-gate 							    "Oerrs", "Collis");
29197c478bd9Sstevel@tonic-gate 							first = B_FALSE;
29207c478bd9Sstevel@tonic-gate 						}
29217c478bd9Sstevel@tonic-gate 						if_report_ip6(ap6, ifname,
29227c478bd9Sstevel@tonic-gate 						    logintname, &stat, B_TRUE);
29237c478bd9Sstevel@tonic-gate 						ifindex_v6 = new_ifindex;
29247c478bd9Sstevel@tonic-gate 					} else {
29257c478bd9Sstevel@tonic-gate 						if_report_ip6(ap6, ifname,
29267c478bd9Sstevel@tonic-gate 						    logintname, &stat, B_FALSE);
29277c478bd9Sstevel@tonic-gate 					}
29287c478bd9Sstevel@tonic-gate 				} /* 'for' loop 2d ends */
29297c478bd9Sstevel@tonic-gate 				if (!first)
29307c478bd9Sstevel@tonic-gate 					(void) putchar('\n');
29317c478bd9Sstevel@tonic-gate 			} else if (!alreadydone) {
29327c478bd9Sstevel@tonic-gate 				char    ifname[LIFNAMSIZ + 1];
29337c478bd9Sstevel@tonic-gate 				char    buf[IFNAMSIZ + 1];
29347c478bd9Sstevel@tonic-gate 				mib2_ipv6AddrEntry_t *ap6;
29357c478bd9Sstevel@tonic-gate 				struct ifstat   t;
29367c478bd9Sstevel@tonic-gate 				struct iflist	*tlp;
29377c478bd9Sstevel@tonic-gate 				struct iflist	**nextnew = &newlist6;
29387c478bd9Sstevel@tonic-gate 				struct iflist	*walkold;
29397c478bd9Sstevel@tonic-gate 				struct iflist	*cleanlist;
29407c478bd9Sstevel@tonic-gate 
29417c478bd9Sstevel@tonic-gate 				alreadydone = B_TRUE; /* ignore other case */
29427c478bd9Sstevel@tonic-gate 				/*
29437c478bd9Sstevel@tonic-gate 				 * 'for' loop 2e: find the "right" entry
29447c478bd9Sstevel@tonic-gate 				 */
29457c478bd9Sstevel@tonic-gate 				for (ap6 = (mib2_ipv6AddrEntry_t *)item->valp;
29467c478bd9Sstevel@tonic-gate 				    (char *)ap6 < (char *)item->valp
29477c478bd9Sstevel@tonic-gate 				    + item->length;
29487c478bd9Sstevel@tonic-gate 				    ap6++) {
29497c478bd9Sstevel@tonic-gate 					(void) octetstr(&ap6->ipv6AddrIfIndex,
29507c478bd9Sstevel@tonic-gate 					    'a', ifname, sizeof (ifname));
29517c478bd9Sstevel@tonic-gate 					(void) strtok(ifname, ":");
29527c478bd9Sstevel@tonic-gate 
29537c478bd9Sstevel@tonic-gate 					if (matchname) {
29547c478bd9Sstevel@tonic-gate 						if (strcmp(matchname, ifname) ==
29557c478bd9Sstevel@tonic-gate 						    0)
29567c478bd9Sstevel@tonic-gate 							/* 'for' loop 2e */
29577c478bd9Sstevel@tonic-gate 							break;
29587c478bd9Sstevel@tonic-gate 					} else if (strcmp(ifname, "lo0") != 0) {
29597c478bd9Sstevel@tonic-gate 						matchname = ifname;
29607c478bd9Sstevel@tonic-gate 						break; /* 'for' loop 2e */
29617c478bd9Sstevel@tonic-gate 					}
29627c478bd9Sstevel@tonic-gate 				} /* 'for' loop 2e ends */
29637c478bd9Sstevel@tonic-gate 
29647c478bd9Sstevel@tonic-gate 				if (Iflag_only == 0 || !reentry) {
29657c478bd9Sstevel@tonic-gate 					(void) printf(
29667c478bd9Sstevel@tonic-gate 					    "    input   %-6.6s"
29677c478bd9Sstevel@tonic-gate 					    "    output	",
29687c478bd9Sstevel@tonic-gate 					    matchname);
29697c478bd9Sstevel@tonic-gate 					(void) printf("   input  (Total)"
29707c478bd9Sstevel@tonic-gate 					    "    output\n");
29717c478bd9Sstevel@tonic-gate 					(void) printf("%-7.7s %-5.5s %-7.7s "
29727c478bd9Sstevel@tonic-gate 					    "%-5.5s %-6.6s ",
29737c478bd9Sstevel@tonic-gate 					    "packets", "errs", "packets",
29747c478bd9Sstevel@tonic-gate 					    "errs", "colls");
29757c478bd9Sstevel@tonic-gate 					(void) printf("%-7.7s %-5.5s %-7.7s "
29767c478bd9Sstevel@tonic-gate 					    "%-5.5s %-6.6s\n",
29777c478bd9Sstevel@tonic-gate 					    "packets", "errs", "packets",
29787c478bd9Sstevel@tonic-gate 					    "errs", "colls");
29797c478bd9Sstevel@tonic-gate 				}
29807c478bd9Sstevel@tonic-gate 
29817c478bd9Sstevel@tonic-gate 				sum6 = zerostat;
29827c478bd9Sstevel@tonic-gate 
29837c478bd9Sstevel@tonic-gate 				/* 'for' loop 2f: */
29847c478bd9Sstevel@tonic-gate 				for (ap6 = (mib2_ipv6AddrEntry_t *)item->valp;
29857c478bd9Sstevel@tonic-gate 				    (char *)ap6 < (char *)item->valp
29867c478bd9Sstevel@tonic-gate 				    + item->length;
29877c478bd9Sstevel@tonic-gate 				    ap6++) {
29887c478bd9Sstevel@tonic-gate 					(void) octetstr(&ap6->ipv6AddrIfIndex,
29897c478bd9Sstevel@tonic-gate 					    'a', buf, sizeof (buf));
29907c478bd9Sstevel@tonic-gate 					(void) strtok(buf, ":");
29917c478bd9Sstevel@tonic-gate 					ksp = kstat_lookup(kc, NULL, -1, buf);
29927c478bd9Sstevel@tonic-gate 					if (ksp && ksp->ks_type ==
29937c478bd9Sstevel@tonic-gate 					    KSTAT_TYPE_NAMED)
29947c478bd9Sstevel@tonic-gate 						(void) safe_kstat_read(kc,
29957c478bd9Sstevel@tonic-gate 						    ksp, NULL);
29967c478bd9Sstevel@tonic-gate 
29977c478bd9Sstevel@tonic-gate 					t.ipackets = kstat_named_value(ksp,
29987c478bd9Sstevel@tonic-gate 					    "ipackets");
29997c478bd9Sstevel@tonic-gate 					t.ierrors = kstat_named_value(ksp,
30007c478bd9Sstevel@tonic-gate 					    "ierrors");
30017c478bd9Sstevel@tonic-gate 					t.opackets = kstat_named_value(ksp,
30027c478bd9Sstevel@tonic-gate 					    "opackets");
30037c478bd9Sstevel@tonic-gate 					t.oerrors = kstat_named_value(ksp,
30047c478bd9Sstevel@tonic-gate 					    "oerrors");
30057c478bd9Sstevel@tonic-gate 					t.collisions = kstat_named_value(ksp,
30067c478bd9Sstevel@tonic-gate 					    "collisions");
30077c478bd9Sstevel@tonic-gate 
30087c478bd9Sstevel@tonic-gate 					if (strcmp(buf, matchname) == 0)
30097c478bd9Sstevel@tonic-gate 						new6 = t;
30107c478bd9Sstevel@tonic-gate 
30117c478bd9Sstevel@tonic-gate 					/* Build the interface list */
30127c478bd9Sstevel@tonic-gate 
30137c478bd9Sstevel@tonic-gate 					tlp = malloc(sizeof (struct iflist));
30147c478bd9Sstevel@tonic-gate 					(void) strlcpy(tlp->ifname, buf,
30157c478bd9Sstevel@tonic-gate 					    sizeof (tlp->ifname));
30167c478bd9Sstevel@tonic-gate 					tlp->tot = t;
30177c478bd9Sstevel@tonic-gate 					*nextnew = tlp;
30187c478bd9Sstevel@tonic-gate 					nextnew = &tlp->next_if;
30197c478bd9Sstevel@tonic-gate 
30207c478bd9Sstevel@tonic-gate 					/*
30217c478bd9Sstevel@tonic-gate 					 * First time through.
30227c478bd9Sstevel@tonic-gate 					 * Just add up the interface stats.
30237c478bd9Sstevel@tonic-gate 					 */
30247c478bd9Sstevel@tonic-gate 
30257c478bd9Sstevel@tonic-gate 					if (oldlist6 == NULL) {
30267c478bd9Sstevel@tonic-gate 						if_stat_total(&zerostat,
30277c478bd9Sstevel@tonic-gate 						    &t, &sum6);
30287c478bd9Sstevel@tonic-gate 						continue;
30297c478bd9Sstevel@tonic-gate 					}
30307c478bd9Sstevel@tonic-gate 
30317c478bd9Sstevel@tonic-gate 					/*
30327c478bd9Sstevel@tonic-gate 					 * Walk old list for the interface.
30337c478bd9Sstevel@tonic-gate 					 *
30347c478bd9Sstevel@tonic-gate 					 * If found, add difference to total.
30357c478bd9Sstevel@tonic-gate 					 *
30367c478bd9Sstevel@tonic-gate 					 * If not, an interface has been plumbed
30377c478bd9Sstevel@tonic-gate 					 * up.  In this case, we will simply
30387c478bd9Sstevel@tonic-gate 					 * ignore the new interface until the
30397c478bd9Sstevel@tonic-gate 					 * next interval; as there's no easy way
30407c478bd9Sstevel@tonic-gate 					 * to acquire statistics between time
30417c478bd9Sstevel@tonic-gate 					 * of the plumb and the next interval
30427c478bd9Sstevel@tonic-gate 					 * boundary.  This results in inaccurate
30437c478bd9Sstevel@tonic-gate 					 * total values for current interval.
30447c478bd9Sstevel@tonic-gate 					 *
30457c478bd9Sstevel@tonic-gate 					 * Note the case when an interface is
30467c478bd9Sstevel@tonic-gate 					 * unplumbed; as similar problems exist.
30477c478bd9Sstevel@tonic-gate 					 * The unplumbed interface is not in the
30487c478bd9Sstevel@tonic-gate 					 * current list, and there's no easy way
30497c478bd9Sstevel@tonic-gate 					 * to account for the statistics between
30507c478bd9Sstevel@tonic-gate 					 * the previous interval and time of the
30517c478bd9Sstevel@tonic-gate 					 * unplumb.  Therefore, we (in a sense)
30527c478bd9Sstevel@tonic-gate 					 * ignore the removed interface by only
30537c478bd9Sstevel@tonic-gate 					 * involving "current" interfaces when
30547c478bd9Sstevel@tonic-gate 					 * computing the total statistics.
30557c478bd9Sstevel@tonic-gate 					 * Unfortunately, this also results in
30567c478bd9Sstevel@tonic-gate 					 * inaccurate values for interval total.
30577c478bd9Sstevel@tonic-gate 					 */
30587c478bd9Sstevel@tonic-gate 
30597c478bd9Sstevel@tonic-gate 					for (walkold = oldlist6;
30607c478bd9Sstevel@tonic-gate 					    walkold != NULL;
30617c478bd9Sstevel@tonic-gate 					    walkold = walkold->next_if) {
30627c478bd9Sstevel@tonic-gate 						if (strcmp(walkold->ifname,
30637c478bd9Sstevel@tonic-gate 						    buf) == 0) {
30647c478bd9Sstevel@tonic-gate 							if_stat_total(
30657c478bd9Sstevel@tonic-gate 							    &walkold->tot,
30667c478bd9Sstevel@tonic-gate 							    &t, &sum6);
30677c478bd9Sstevel@tonic-gate 							break;
30687c478bd9Sstevel@tonic-gate 						}
30697c478bd9Sstevel@tonic-gate 					}
30707c478bd9Sstevel@tonic-gate 
30717c478bd9Sstevel@tonic-gate 				} /* 'for' loop 2f ends */
30727c478bd9Sstevel@tonic-gate 
30737c478bd9Sstevel@tonic-gate 				*nextnew = NULL;
30747c478bd9Sstevel@tonic-gate 
30757c478bd9Sstevel@tonic-gate 				(void) printf("%-7llu %-5llu %-7llu "
30767c478bd9Sstevel@tonic-gate 				    "%-5llu %-6llu ",
30777c478bd9Sstevel@tonic-gate 				    new6.ipackets - old6.ipackets,
30787c478bd9Sstevel@tonic-gate 				    new6.ierrors - old6.ierrors,
30797c478bd9Sstevel@tonic-gate 				    new6.opackets - old6.opackets,
30807c478bd9Sstevel@tonic-gate 				    new6.oerrors - old6.oerrors,
30817c478bd9Sstevel@tonic-gate 				    new6.collisions - old6.collisions);
30827c478bd9Sstevel@tonic-gate 
30837c478bd9Sstevel@tonic-gate 				(void) printf("%-7llu %-5llu %-7llu "
30847c478bd9Sstevel@tonic-gate 				    "%-5llu %-6llu\n", sum6.ipackets,
30857c478bd9Sstevel@tonic-gate 				    sum6.ierrors, sum6.opackets,
30867c478bd9Sstevel@tonic-gate 				    sum6.oerrors, sum6.collisions);
30877c478bd9Sstevel@tonic-gate 
30887c478bd9Sstevel@tonic-gate 				/*
30897c478bd9Sstevel@tonic-gate 				 * Tidy things up once finished.
30907c478bd9Sstevel@tonic-gate 				 */
30917c478bd9Sstevel@tonic-gate 
30927c478bd9Sstevel@tonic-gate 				old6 = new6;
30937c478bd9Sstevel@tonic-gate 				cleanlist = oldlist6;
30947c478bd9Sstevel@tonic-gate 				oldlist6 = newlist6;
30957c478bd9Sstevel@tonic-gate 				while (cleanlist != NULL) {
30967c478bd9Sstevel@tonic-gate 					tlp = cleanlist->next_if;
30977c478bd9Sstevel@tonic-gate 					free(cleanlist);
30987c478bd9Sstevel@tonic-gate 					cleanlist = tlp;
30997c478bd9Sstevel@tonic-gate 				}
31007c478bd9Sstevel@tonic-gate 			}
31017c478bd9Sstevel@tonic-gate 			break;
31027c478bd9Sstevel@tonic-gate 		}
31037c478bd9Sstevel@tonic-gate 		}
31047c478bd9Sstevel@tonic-gate 		if (Iflag_only == 0)
31057c478bd9Sstevel@tonic-gate 		    (void) putchar('\n');
31067c478bd9Sstevel@tonic-gate 		(void) fflush(stdout);
31077c478bd9Sstevel@tonic-gate 	} /* 'for' loop 1 ends */
31087c478bd9Sstevel@tonic-gate 	reentry = B_TRUE;
31097c478bd9Sstevel@tonic-gate }
31107c478bd9Sstevel@tonic-gate 
31117c478bd9Sstevel@tonic-gate static void
31127c478bd9Sstevel@tonic-gate if_report_ip4(mib2_ipAddrEntry_t *ap,
31137c478bd9Sstevel@tonic-gate 	char ifname[], char logintname[], struct ifstat *statptr,
31147c478bd9Sstevel@tonic-gate 	boolean_t ksp_not_null) {
31157c478bd9Sstevel@tonic-gate 
31167c478bd9Sstevel@tonic-gate 	char abuf[MAXHOSTNAMELEN + 1];
31177c478bd9Sstevel@tonic-gate 	char dstbuf[MAXHOSTNAMELEN + 1];
31187c478bd9Sstevel@tonic-gate 
31197c478bd9Sstevel@tonic-gate 	if (ksp_not_null) {
31207c478bd9Sstevel@tonic-gate 		(void) printf("%-5s %-5u",
31217c478bd9Sstevel@tonic-gate 		    ifname, ap->ipAdEntInfo.ae_mtu);
31227c478bd9Sstevel@tonic-gate 		if (ap->ipAdEntInfo.ae_flags & IFF_POINTOPOINT)
31237c478bd9Sstevel@tonic-gate 			(void) pr_addr(ap->ipAdEntInfo.ae_pp_dst_addr,
31247c478bd9Sstevel@tonic-gate 			    abuf, sizeof (abuf));
31257c478bd9Sstevel@tonic-gate 		else
31267c478bd9Sstevel@tonic-gate 			(void) pr_netaddr(ap->ipAdEntAddr,
31277c478bd9Sstevel@tonic-gate 			    ap->ipAdEntNetMask, abuf, sizeof (abuf));
31287c478bd9Sstevel@tonic-gate 		(void) printf("%-13s %-14s %-6llu %-5llu %-6llu %-5llu "
31297c478bd9Sstevel@tonic-gate 		    "%-6llu %-6llu\n",
31307c478bd9Sstevel@tonic-gate 		    abuf, pr_addr(ap->ipAdEntAddr, dstbuf, sizeof (dstbuf)),
31317c478bd9Sstevel@tonic-gate 		    statptr->ipackets, statptr->ierrors,
31327c478bd9Sstevel@tonic-gate 		    statptr->opackets, statptr->oerrors,
31337c478bd9Sstevel@tonic-gate 		    statptr->collisions, 0LL);
31347c478bd9Sstevel@tonic-gate 	}
31357c478bd9Sstevel@tonic-gate 	/*
31367c478bd9Sstevel@tonic-gate 	 * Print logical interface info if Aflag set (including logical unit 0)
31377c478bd9Sstevel@tonic-gate 	 */
31387c478bd9Sstevel@tonic-gate 	if (Aflag) {
31397c478bd9Sstevel@tonic-gate 		*statptr = zerostat;
31407c478bd9Sstevel@tonic-gate 		statptr->ipackets = ap->ipAdEntInfo.ae_ibcnt;
31417c478bd9Sstevel@tonic-gate 		statptr->opackets = ap->ipAdEntInfo.ae_obcnt;
31427c478bd9Sstevel@tonic-gate 
31437c478bd9Sstevel@tonic-gate 		(void) printf("%-5s %-5u", logintname, ap->ipAdEntInfo.ae_mtu);
31447c478bd9Sstevel@tonic-gate 		if (ap->ipAdEntInfo.ae_flags & IFF_POINTOPOINT)
31457c478bd9Sstevel@tonic-gate 			(void) pr_addr(ap->ipAdEntInfo.ae_pp_dst_addr, abuf,
31467c478bd9Sstevel@tonic-gate 			sizeof (abuf));
31477c478bd9Sstevel@tonic-gate 		else
31487c478bd9Sstevel@tonic-gate 			(void) pr_netaddr(ap->ipAdEntAddr, ap->ipAdEntNetMask,
31497c478bd9Sstevel@tonic-gate 			    abuf, sizeof (abuf));
31507c478bd9Sstevel@tonic-gate 
31517c478bd9Sstevel@tonic-gate 		(void) printf("%-13s %-14s %-6llu %-5s %-6llu "
31527c478bd9Sstevel@tonic-gate 		    "%-5s %-6s %-6llu\n", abuf,
31537c478bd9Sstevel@tonic-gate 		    pr_addr(ap->ipAdEntAddr, dstbuf, sizeof (dstbuf)),
31547c478bd9Sstevel@tonic-gate 		    statptr->ipackets, "N/A", statptr->opackets, "N/A", "N/A",
31557c478bd9Sstevel@tonic-gate 		    0LL);
31567c478bd9Sstevel@tonic-gate 	}
31577c478bd9Sstevel@tonic-gate }
31587c478bd9Sstevel@tonic-gate 
31597c478bd9Sstevel@tonic-gate static void
31607c478bd9Sstevel@tonic-gate if_report_ip6(mib2_ipv6AddrEntry_t *ap6,
31617c478bd9Sstevel@tonic-gate 	char ifname[], char logintname[], struct ifstat *statptr,
31627c478bd9Sstevel@tonic-gate 	boolean_t ksp_not_null) {
31637c478bd9Sstevel@tonic-gate 
31647c478bd9Sstevel@tonic-gate 	char abuf[MAXHOSTNAMELEN + 1];
31657c478bd9Sstevel@tonic-gate 	char dstbuf[MAXHOSTNAMELEN + 1];
31667c478bd9Sstevel@tonic-gate 
31677c478bd9Sstevel@tonic-gate 	if (ksp_not_null) {
31687c478bd9Sstevel@tonic-gate 		(void) printf("%-5s %-5u", ifname, ap6->ipv6AddrInfo.ae_mtu);
31697c478bd9Sstevel@tonic-gate 		if (ap6->ipv6AddrInfo.ae_flags &
31707c478bd9Sstevel@tonic-gate 		    IFF_POINTOPOINT) {
31717c478bd9Sstevel@tonic-gate 			(void) pr_addr6(&ap6->ipv6AddrInfo.ae_pp_dst_addr,
31727c478bd9Sstevel@tonic-gate 			    abuf, sizeof (abuf));
31737c478bd9Sstevel@tonic-gate 		} else {
31747c478bd9Sstevel@tonic-gate 			(void) pr_prefix6(&ap6->ipv6AddrAddress,
31757c478bd9Sstevel@tonic-gate 			    ap6->ipv6AddrPfxLength, abuf,
31767c478bd9Sstevel@tonic-gate 			    sizeof (abuf));
31777c478bd9Sstevel@tonic-gate 		}
31787c478bd9Sstevel@tonic-gate 		(void) printf("%-27s %-27s %-6llu %-5llu "
31797c478bd9Sstevel@tonic-gate 		    "%-6llu %-5llu %-6llu\n",
31807c478bd9Sstevel@tonic-gate 		    abuf, pr_addr6(&ap6->ipv6AddrAddress, dstbuf,
31817c478bd9Sstevel@tonic-gate 		    sizeof (dstbuf)),
31827c478bd9Sstevel@tonic-gate 		    statptr->ipackets, statptr->ierrors, statptr->opackets,
31837c478bd9Sstevel@tonic-gate 		    statptr->oerrors, statptr->collisions);
31847c478bd9Sstevel@tonic-gate 	}
31857c478bd9Sstevel@tonic-gate 	/*
31867c478bd9Sstevel@tonic-gate 	 * Print logical interface info if Aflag set (including logical unit 0)
31877c478bd9Sstevel@tonic-gate 	 */
31887c478bd9Sstevel@tonic-gate 	if (Aflag) {
31897c478bd9Sstevel@tonic-gate 		*statptr = zerostat;
31907c478bd9Sstevel@tonic-gate 		statptr->ipackets = ap6->ipv6AddrInfo.ae_ibcnt;
31917c478bd9Sstevel@tonic-gate 		statptr->opackets = ap6->ipv6AddrInfo.ae_obcnt;
31927c478bd9Sstevel@tonic-gate 
31937c478bd9Sstevel@tonic-gate 		(void) printf("%-5s %-5u", logintname,
31947c478bd9Sstevel@tonic-gate 		    ap6->ipv6AddrInfo.ae_mtu);
31957c478bd9Sstevel@tonic-gate 		if (ap6->ipv6AddrInfo.ae_flags & IFF_POINTOPOINT)
31967c478bd9Sstevel@tonic-gate 			(void) pr_addr6(&ap6->ipv6AddrInfo.ae_pp_dst_addr,
31977c478bd9Sstevel@tonic-gate 			    abuf, sizeof (abuf));
31987c478bd9Sstevel@tonic-gate 		else
31997c478bd9Sstevel@tonic-gate 			(void) pr_prefix6(&ap6->ipv6AddrAddress,
32007c478bd9Sstevel@tonic-gate 			    ap6->ipv6AddrPfxLength, abuf, sizeof (abuf));
32017c478bd9Sstevel@tonic-gate 		(void) printf("%-27s %-27s %-6llu %-5s %-6llu %-5s %-6s\n",
32027c478bd9Sstevel@tonic-gate 		    abuf, pr_addr6(&ap6->ipv6AddrAddress, dstbuf,
32037c478bd9Sstevel@tonic-gate 		    sizeof (dstbuf)),
32047c478bd9Sstevel@tonic-gate 		    statptr->ipackets, "N/A",
32057c478bd9Sstevel@tonic-gate 		    statptr->opackets, "N/A", "N/A");
32067c478bd9Sstevel@tonic-gate 	}
32077c478bd9Sstevel@tonic-gate }
32087c478bd9Sstevel@tonic-gate 
32097c478bd9Sstevel@tonic-gate /* --------------------- DHCP_REPORT  (netstat -D) ------------------------- */
32107c478bd9Sstevel@tonic-gate 
32117c478bd9Sstevel@tonic-gate dhcp_ipc_reply_t *
32127c478bd9Sstevel@tonic-gate dhcp_do_ipc(dhcp_ipc_type_t type, const char *ifname)
32137c478bd9Sstevel@tonic-gate {
32147c478bd9Sstevel@tonic-gate 	dhcp_ipc_request_t	*request;
32157c478bd9Sstevel@tonic-gate 	dhcp_ipc_reply_t	*reply;
32167c478bd9Sstevel@tonic-gate 	int			error;
32177c478bd9Sstevel@tonic-gate 
32187c478bd9Sstevel@tonic-gate 	request = dhcp_ipc_alloc_request(type, ifname, NULL, 0, DHCP_TYPE_NONE);
32197c478bd9Sstevel@tonic-gate 	if (request == NULL)
32207c478bd9Sstevel@tonic-gate 		fail(0, "dhcp_do_ipc: out of memory");
32217c478bd9Sstevel@tonic-gate 
32227c478bd9Sstevel@tonic-gate 	error = dhcp_ipc_make_request(request, &reply, DHCP_IPC_WAIT_DEFAULT);
32237c478bd9Sstevel@tonic-gate 	if (error != 0) {
32247c478bd9Sstevel@tonic-gate 		free(request);
32257c478bd9Sstevel@tonic-gate 		fail(0, "dhcp_do_ipc: %s", dhcp_ipc_strerror(error));
32267c478bd9Sstevel@tonic-gate 	}
32277c478bd9Sstevel@tonic-gate 
32287c478bd9Sstevel@tonic-gate 	free(request);
32297c478bd9Sstevel@tonic-gate 	error = reply->return_code;
32307c478bd9Sstevel@tonic-gate 	if (error != 0) {
32317c478bd9Sstevel@tonic-gate 		free(reply);
32327c478bd9Sstevel@tonic-gate 		fail(0, "dhcp_do_ipc: %s", dhcp_ipc_strerror(error));
32337c478bd9Sstevel@tonic-gate 	}
32347c478bd9Sstevel@tonic-gate 
32357c478bd9Sstevel@tonic-gate 	return (reply);
32367c478bd9Sstevel@tonic-gate }
32377c478bd9Sstevel@tonic-gate 
32387c478bd9Sstevel@tonic-gate /*
32397c478bd9Sstevel@tonic-gate  * get_ifnames: return a dynamically allocated string of all interface
32407c478bd9Sstevel@tonic-gate  * names which have all of the IFF_* flags listed in `flags_on' on and
32417c478bd9Sstevel@tonic-gate  * all of the IFF_* flags in `flags_off' off.  If no such interfaces
32427c478bd9Sstevel@tonic-gate  * are found, "" is returned.  If an unexpected failure occurs, NULL
32437c478bd9Sstevel@tonic-gate  * is returned.
32447c478bd9Sstevel@tonic-gate  */
32457c478bd9Sstevel@tonic-gate static char *
32467c478bd9Sstevel@tonic-gate get_ifnames(int flags_on, int flags_off)
32477c478bd9Sstevel@tonic-gate {
32487c478bd9Sstevel@tonic-gate 	struct ifconf	ifc;
32497c478bd9Sstevel@tonic-gate 	int		n_ifs, i, sock_fd;
32507c478bd9Sstevel@tonic-gate 	char		*ifnames;
32517c478bd9Sstevel@tonic-gate 
32527c478bd9Sstevel@tonic-gate 	sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
32537c478bd9Sstevel@tonic-gate 	if (sock_fd == -1)
32547c478bd9Sstevel@tonic-gate 		return (NULL);
32557c478bd9Sstevel@tonic-gate 
32567c478bd9Sstevel@tonic-gate 	if ((ioctl(sock_fd, SIOCGIFNUM, &n_ifs) == -1) || (n_ifs <= 0)) {
32577c478bd9Sstevel@tonic-gate 		(void) close(sock_fd);
32587c478bd9Sstevel@tonic-gate 		return (NULL);
32597c478bd9Sstevel@tonic-gate 	}
32607c478bd9Sstevel@tonic-gate 
32617c478bd9Sstevel@tonic-gate 	ifnames = calloc(1, n_ifs * (IFNAMSIZ + 1));
32627c478bd9Sstevel@tonic-gate 	ifc.ifc_len = n_ifs * sizeof (struct ifreq);
32637c478bd9Sstevel@tonic-gate 	ifc.ifc_req = calloc(n_ifs, sizeof (struct ifreq));
32647c478bd9Sstevel@tonic-gate 	if (ifc.ifc_req != NULL && ifnames != NULL) {
32657c478bd9Sstevel@tonic-gate 
32667c478bd9Sstevel@tonic-gate 		if (ioctl(sock_fd, SIOCGIFCONF, &ifc) == -1) {
32677c478bd9Sstevel@tonic-gate 			(void) close(sock_fd);
32687c478bd9Sstevel@tonic-gate 			free(ifnames);
32697c478bd9Sstevel@tonic-gate 			free(ifc.ifc_req);
32707c478bd9Sstevel@tonic-gate 			return (NULL);
32717c478bd9Sstevel@tonic-gate 		}
32727c478bd9Sstevel@tonic-gate 
32737c478bd9Sstevel@tonic-gate 		/* 'for' loop 1: */
32747c478bd9Sstevel@tonic-gate 		for (i = 0; i < n_ifs; i++) {
32757c478bd9Sstevel@tonic-gate 
32767c478bd9Sstevel@tonic-gate 			if (ioctl(sock_fd, SIOCGIFFLAGS, &ifc.ifc_req[i]) == 0)
32777c478bd9Sstevel@tonic-gate 				if ((ifc.ifc_req[i].ifr_flags &
32787c478bd9Sstevel@tonic-gate 				    (flags_on | flags_off)) != flags_on)
32797c478bd9Sstevel@tonic-gate 					continue; /* 'for' loop 1 */
32807c478bd9Sstevel@tonic-gate 
32817c478bd9Sstevel@tonic-gate 			(void) strcat(ifnames, ifc.ifc_req[i].ifr_name);
32827c478bd9Sstevel@tonic-gate 			(void) strcat(ifnames, " ");
32837c478bd9Sstevel@tonic-gate 		} /* 'for' loop 1 ends */
32847c478bd9Sstevel@tonic-gate 
32857c478bd9Sstevel@tonic-gate 		if (strlen(ifnames) > 1)
32867c478bd9Sstevel@tonic-gate 			ifnames[strlen(ifnames) - 1] = '\0';
32877c478bd9Sstevel@tonic-gate 	}
32887c478bd9Sstevel@tonic-gate 
32897c478bd9Sstevel@tonic-gate 	(void) close(sock_fd);
32907c478bd9Sstevel@tonic-gate 	if (ifc.ifc_req != NULL)
32917c478bd9Sstevel@tonic-gate 		free(ifc.ifc_req);
32927c478bd9Sstevel@tonic-gate 	return (ifnames);
32937c478bd9Sstevel@tonic-gate }
32947c478bd9Sstevel@tonic-gate 
32957c478bd9Sstevel@tonic-gate static void
32967c478bd9Sstevel@tonic-gate dhcp_report(char *ifname)
32977c478bd9Sstevel@tonic-gate {
32987c478bd9Sstevel@tonic-gate 	int			did_alloc = 0;
32997c478bd9Sstevel@tonic-gate 	dhcp_ipc_reply_t	*reply;
33007c478bd9Sstevel@tonic-gate 
33017c478bd9Sstevel@tonic-gate 	if (!(family_selected(AF_INET)))
33027c478bd9Sstevel@tonic-gate 		return;
33037c478bd9Sstevel@tonic-gate 
33047c478bd9Sstevel@tonic-gate 	if (ifname == NULL) {
33057c478bd9Sstevel@tonic-gate 		ifname = get_ifnames(IFF_DHCPRUNNING, 0);
33067c478bd9Sstevel@tonic-gate 		if (ifname == NULL)
33077c478bd9Sstevel@tonic-gate 			fail(0, "dhcp_report: unable to retrieve list of"
33087c478bd9Sstevel@tonic-gate 			    " interfaces using DHCP");
33097c478bd9Sstevel@tonic-gate 		did_alloc++;
33107c478bd9Sstevel@tonic-gate 	}
33117c478bd9Sstevel@tonic-gate 
33127c478bd9Sstevel@tonic-gate 	(void) printf("%s", dhcp_status_hdr_string());
33137c478bd9Sstevel@tonic-gate 
33147c478bd9Sstevel@tonic-gate 	for (ifname = strtok(ifname, " ");
33157c478bd9Sstevel@tonic-gate 	    ifname != NULL;
33167c478bd9Sstevel@tonic-gate 	    ifname = strtok(NULL, " ")) {
33177c478bd9Sstevel@tonic-gate 		reply = dhcp_do_ipc(DHCP_STATUS, ifname);
33187c478bd9Sstevel@tonic-gate 		(void) printf("%s", dhcp_status_reply_to_string(reply));
33197c478bd9Sstevel@tonic-gate 		free(reply);
33207c478bd9Sstevel@tonic-gate 	}
33217c478bd9Sstevel@tonic-gate 
33227c478bd9Sstevel@tonic-gate 	if (did_alloc)
33237c478bd9Sstevel@tonic-gate 		free(ifname);
33247c478bd9Sstevel@tonic-gate }
33257c478bd9Sstevel@tonic-gate 
33267c478bd9Sstevel@tonic-gate /* --------------------- GROUP_REPORT (netstat -g) ------------------------- */
33277c478bd9Sstevel@tonic-gate 
33287c478bd9Sstevel@tonic-gate static void
33297c478bd9Sstevel@tonic-gate group_report(mib_item_t *item)
33307c478bd9Sstevel@tonic-gate {
33317c478bd9Sstevel@tonic-gate 	mib_item_t	*v4grp = NULL, *v4src = NULL;
33327c478bd9Sstevel@tonic-gate 	mib_item_t	*v6grp = NULL, *v6src = NULL;
33337c478bd9Sstevel@tonic-gate 	int		jtemp = 0;
33347c478bd9Sstevel@tonic-gate 	char		ifname[LIFNAMSIZ + 1];
33357c478bd9Sstevel@tonic-gate 	char		abuf[MAXHOSTNAMELEN + 1];
33367c478bd9Sstevel@tonic-gate 	ip_member_t	*ipmp;
33377c478bd9Sstevel@tonic-gate 	ip_grpsrc_t	*ips;
33387c478bd9Sstevel@tonic-gate 	ipv6_member_t	*ipmp6;
33397c478bd9Sstevel@tonic-gate 	ipv6_grpsrc_t	*ips6;
33407c478bd9Sstevel@tonic-gate 	char		*ifnamep;
33417c478bd9Sstevel@tonic-gate 	boolean_t	first, first_src;
33427c478bd9Sstevel@tonic-gate 
33437c478bd9Sstevel@tonic-gate 	/* 'for' loop 1: */
33447c478bd9Sstevel@tonic-gate 	for (; item; item = item->next_item) {
33457c478bd9Sstevel@tonic-gate 		if (Dflag) {
33467c478bd9Sstevel@tonic-gate 			(void) printf("\n--- Entry %d ---\n", ++jtemp);
33477c478bd9Sstevel@tonic-gate 			(void) printf("Group = %d, mib_id = %d, "
33487c478bd9Sstevel@tonic-gate 			    "length = %d, valp = 0x%p\n",
33497c478bd9Sstevel@tonic-gate 			    item->group, item->mib_id, item->length,
33507c478bd9Sstevel@tonic-gate 			    item->valp);
33517c478bd9Sstevel@tonic-gate 		}
33527c478bd9Sstevel@tonic-gate 		if (item->group == MIB2_IP && family_selected(AF_INET)) {
33537c478bd9Sstevel@tonic-gate 			switch (item->mib_id) {
33547c478bd9Sstevel@tonic-gate 			case EXPER_IP_GROUP_MEMBERSHIP:
33557c478bd9Sstevel@tonic-gate 				v4grp = item;
33567c478bd9Sstevel@tonic-gate 				if (Dflag)
33577c478bd9Sstevel@tonic-gate 					(void) printf("item is v4grp info\n");
33587c478bd9Sstevel@tonic-gate 				break;
33597c478bd9Sstevel@tonic-gate 			case EXPER_IP_GROUP_SOURCES:
33607c478bd9Sstevel@tonic-gate 				v4src = item;
33617c478bd9Sstevel@tonic-gate 				if (Dflag)
33627c478bd9Sstevel@tonic-gate 					(void) printf("item is v4src info\n");
33637c478bd9Sstevel@tonic-gate 				break;
33647c478bd9Sstevel@tonic-gate 			default:
33657c478bd9Sstevel@tonic-gate 				continue;
33667c478bd9Sstevel@tonic-gate 			}
33677c478bd9Sstevel@tonic-gate 			continue;
33687c478bd9Sstevel@tonic-gate 		}
33697c478bd9Sstevel@tonic-gate 		if (item->group == MIB2_IP6 && family_selected(AF_INET6)) {
33707c478bd9Sstevel@tonic-gate 			switch (item->mib_id) {
33717c478bd9Sstevel@tonic-gate 			case EXPER_IP6_GROUP_MEMBERSHIP:
33727c478bd9Sstevel@tonic-gate 				v6grp = item;
33737c478bd9Sstevel@tonic-gate 				if (Dflag)
33747c478bd9Sstevel@tonic-gate 					(void) printf("item is v6grp info\n");
33757c478bd9Sstevel@tonic-gate 				break;
33767c478bd9Sstevel@tonic-gate 			case EXPER_IP6_GROUP_SOURCES:
33777c478bd9Sstevel@tonic-gate 				v6src = item;
33787c478bd9Sstevel@tonic-gate 				if (Dflag)
33797c478bd9Sstevel@tonic-gate 					(void) printf("item is v6src info\n");
33807c478bd9Sstevel@tonic-gate 				break;
33817c478bd9Sstevel@tonic-gate 			default:
33827c478bd9Sstevel@tonic-gate 				continue;
33837c478bd9Sstevel@tonic-gate 			}
33847c478bd9Sstevel@tonic-gate 		}
33857c478bd9Sstevel@tonic-gate 	}
33867c478bd9Sstevel@tonic-gate 
33877c478bd9Sstevel@tonic-gate 	if (family_selected(AF_INET) && v4grp != NULL) {
33887c478bd9Sstevel@tonic-gate 		if (Dflag)
33897c478bd9Sstevel@tonic-gate 			(void) printf("%u records for ipGroupMember:\n",
33907c478bd9Sstevel@tonic-gate 			    v4grp->length / sizeof (ip_member_t));
33917c478bd9Sstevel@tonic-gate 
33927c478bd9Sstevel@tonic-gate 		first = B_TRUE;
33937c478bd9Sstevel@tonic-gate 		for (ipmp = (ip_member_t *)v4grp->valp;
33947c478bd9Sstevel@tonic-gate 		    (char *)ipmp < (char *)v4grp->valp + v4grp->length;
33957c478bd9Sstevel@tonic-gate 		    /* LINTED: (note 1) */
33967c478bd9Sstevel@tonic-gate 		    ipmp = (ip_member_t *)((char *)ipmp + ipMemberEntrySize)) {
33977c478bd9Sstevel@tonic-gate 			if (first) {
33987c478bd9Sstevel@tonic-gate 				(void) puts(v4compat ?
33997c478bd9Sstevel@tonic-gate 				    "Group Memberships" :
34007c478bd9Sstevel@tonic-gate 				    "Group Memberships: IPv4");
34017c478bd9Sstevel@tonic-gate 				(void) puts("Interface "
34027c478bd9Sstevel@tonic-gate 				    "Group                RefCnt");
34037c478bd9Sstevel@tonic-gate 				(void) puts("--------- "
34047c478bd9Sstevel@tonic-gate 				    "-------------------- ------");
34057c478bd9Sstevel@tonic-gate 				first = B_FALSE;
34067c478bd9Sstevel@tonic-gate 			}
34077c478bd9Sstevel@tonic-gate 
34087c478bd9Sstevel@tonic-gate 			(void) printf("%-9s %-20s %6u\n",
34097c478bd9Sstevel@tonic-gate 			    octetstr(&ipmp->ipGroupMemberIfIndex, 'a',
34107c478bd9Sstevel@tonic-gate 			    ifname, sizeof (ifname)),
34117c478bd9Sstevel@tonic-gate 			    pr_addr(ipmp->ipGroupMemberAddress,
34127c478bd9Sstevel@tonic-gate 			    abuf, sizeof (abuf)),
34137c478bd9Sstevel@tonic-gate 			    ipmp->ipGroupMemberRefCnt);
34147c478bd9Sstevel@tonic-gate 
34157c478bd9Sstevel@tonic-gate 
34167c478bd9Sstevel@tonic-gate 			if (!Vflag || v4src == NULL)
34177c478bd9Sstevel@tonic-gate 				continue;
34187c478bd9Sstevel@tonic-gate 
34197c478bd9Sstevel@tonic-gate 			if (Dflag)
34207c478bd9Sstevel@tonic-gate 				(void) printf("scanning %u ipGroupSource "
34217c478bd9Sstevel@tonic-gate 				    "records...\n",
34227c478bd9Sstevel@tonic-gate 				    v4src->length/sizeof (ip_grpsrc_t));
34237c478bd9Sstevel@tonic-gate 
34247c478bd9Sstevel@tonic-gate 			first_src = B_TRUE;
34257c478bd9Sstevel@tonic-gate 			for (ips = (ip_grpsrc_t *)v4src->valp;
34267c478bd9Sstevel@tonic-gate 			    (char *)ips < (char *)v4src->valp + v4src->length;
34277c478bd9Sstevel@tonic-gate 			    /* LINTED: (note 1) */
34287c478bd9Sstevel@tonic-gate 			    ips = (ip_grpsrc_t *)((char *)ips +
34297c478bd9Sstevel@tonic-gate 			    ipGroupSourceEntrySize)) {
34307c478bd9Sstevel@tonic-gate 				/*
34317c478bd9Sstevel@tonic-gate 				 * We assume that all source addrs for a given
34327c478bd9Sstevel@tonic-gate 				 * interface/group pair are contiguous, so on
34337c478bd9Sstevel@tonic-gate 				 * the first non-match after we've found at
34347c478bd9Sstevel@tonic-gate 				 * least one, we bail.
34357c478bd9Sstevel@tonic-gate 				 */
34367c478bd9Sstevel@tonic-gate 				if ((ipmp->ipGroupMemberAddress !=
34377c478bd9Sstevel@tonic-gate 				    ips->ipGroupSourceGroup) ||
34387c478bd9Sstevel@tonic-gate 				    (!octetstrmatch(&ipmp->ipGroupMemberIfIndex,
34397c478bd9Sstevel@tonic-gate 				    &ips->ipGroupSourceIfIndex))) {
34407c478bd9Sstevel@tonic-gate 					if (first_src)
34417c478bd9Sstevel@tonic-gate 						continue;
34427c478bd9Sstevel@tonic-gate 					else
34437c478bd9Sstevel@tonic-gate 						break;
34447c478bd9Sstevel@tonic-gate 				}
34457c478bd9Sstevel@tonic-gate 				if (first_src) {
34467c478bd9Sstevel@tonic-gate 					(void) printf("\t%s:    %s\n",
34477c478bd9Sstevel@tonic-gate 					    fmodestr(
34487c478bd9Sstevel@tonic-gate 					    ipmp->ipGroupMemberFilterMode),
34497c478bd9Sstevel@tonic-gate 					    pr_addr(ips->ipGroupSourceAddress,
34507c478bd9Sstevel@tonic-gate 					    abuf, sizeof (abuf)));
34517c478bd9Sstevel@tonic-gate 					first_src = B_FALSE;
34527c478bd9Sstevel@tonic-gate 					continue;
34537c478bd9Sstevel@tonic-gate 				}
34547c478bd9Sstevel@tonic-gate 
34557c478bd9Sstevel@tonic-gate 				(void) printf("\t            %s\n",
34567c478bd9Sstevel@tonic-gate 				    pr_addr(ips->ipGroupSourceAddress, abuf,
34577c478bd9Sstevel@tonic-gate 				    sizeof (abuf)));
34587c478bd9Sstevel@tonic-gate 			}
34597c478bd9Sstevel@tonic-gate 		}
34607c478bd9Sstevel@tonic-gate 		(void) putchar('\n');
34617c478bd9Sstevel@tonic-gate 	}
34627c478bd9Sstevel@tonic-gate 
34637c478bd9Sstevel@tonic-gate 	if (family_selected(AF_INET6) && v6grp != NULL) {
34647c478bd9Sstevel@tonic-gate 		if (Dflag)
34657c478bd9Sstevel@tonic-gate 			(void) printf("%u records for ipv6GroupMember:\n",
34667c478bd9Sstevel@tonic-gate 			    v6grp->length / sizeof (ipv6_member_t));
34677c478bd9Sstevel@tonic-gate 
34687c478bd9Sstevel@tonic-gate 		first = B_TRUE;
34697c478bd9Sstevel@tonic-gate 		for (ipmp6 = (ipv6_member_t *)v6grp->valp;
34707c478bd9Sstevel@tonic-gate 		    (char *)ipmp6 < (char *)v6grp->valp + v6grp->length;
34717c478bd9Sstevel@tonic-gate 		    /* LINTED: (note 1) */
34727c478bd9Sstevel@tonic-gate 		    ipmp6 = (ipv6_member_t *)((char *)ipmp6 +
34737c478bd9Sstevel@tonic-gate 			ipv6MemberEntrySize)) {
34747c478bd9Sstevel@tonic-gate 			if (first) {
34757c478bd9Sstevel@tonic-gate 				(void) puts("Group Memberships: "
34767c478bd9Sstevel@tonic-gate 				    "IPv6");
34777c478bd9Sstevel@tonic-gate 				(void) puts(" If       "
34787c478bd9Sstevel@tonic-gate 				    "Group                   RefCnt");
34797c478bd9Sstevel@tonic-gate 				(void) puts("----- "
34807c478bd9Sstevel@tonic-gate 				    "--------------------------- ------");
34817c478bd9Sstevel@tonic-gate 				first = B_FALSE;
34827c478bd9Sstevel@tonic-gate 			}
34837c478bd9Sstevel@tonic-gate 
34847c478bd9Sstevel@tonic-gate 			ifnamep = if_indextoname(
34857c478bd9Sstevel@tonic-gate 			    ipmp6->ipv6GroupMemberIfIndex, ifname);
34867c478bd9Sstevel@tonic-gate 			if (ifnamep == NULL) {
34877c478bd9Sstevel@tonic-gate 				(void) printf("Invalid ifindex %d\n",
34887c478bd9Sstevel@tonic-gate 				    ipmp6->ipv6GroupMemberIfIndex);
34897c478bd9Sstevel@tonic-gate 				continue;
34907c478bd9Sstevel@tonic-gate 			}
34917c478bd9Sstevel@tonic-gate 			(void) printf("%-5s %-27s %5u\n",
34927c478bd9Sstevel@tonic-gate 			    ifnamep,
34937c478bd9Sstevel@tonic-gate 			    pr_addr6(&ipmp6->ipv6GroupMemberAddress,
34947c478bd9Sstevel@tonic-gate 			    abuf, sizeof (abuf)),
34957c478bd9Sstevel@tonic-gate 			    ipmp6->ipv6GroupMemberRefCnt);
34967c478bd9Sstevel@tonic-gate 
34977c478bd9Sstevel@tonic-gate 			if (!Vflag || v6src == NULL)
34987c478bd9Sstevel@tonic-gate 				continue;
34997c478bd9Sstevel@tonic-gate 
35007c478bd9Sstevel@tonic-gate 			if (Dflag)
35017c478bd9Sstevel@tonic-gate 				(void) printf("scanning %u ipv6GroupSource "
35027c478bd9Sstevel@tonic-gate 				    "records...\n",
35037c478bd9Sstevel@tonic-gate 				    v6src->length/sizeof (ipv6_grpsrc_t));
35047c478bd9Sstevel@tonic-gate 
35057c478bd9Sstevel@tonic-gate 			first_src = B_TRUE;
35067c478bd9Sstevel@tonic-gate 			for (ips6 = (ipv6_grpsrc_t *)v6src->valp;
35077c478bd9Sstevel@tonic-gate 			    (char *)ips6 < (char *)v6src->valp + v6src->length;
35087c478bd9Sstevel@tonic-gate 			    /* LINTED: (note 1) */
35097c478bd9Sstevel@tonic-gate 			    ips6 = (ipv6_grpsrc_t *)((char *)ips6 +
35107c478bd9Sstevel@tonic-gate 			    ipv6GroupSourceEntrySize)) {
35117c478bd9Sstevel@tonic-gate 				/* same assumption as in the v4 case above */
35127c478bd9Sstevel@tonic-gate 				if ((ipmp6->ipv6GroupMemberIfIndex !=
35137c478bd9Sstevel@tonic-gate 				    ips6->ipv6GroupSourceIfIndex) ||
35147c478bd9Sstevel@tonic-gate 				    (!IN6_ARE_ADDR_EQUAL(
35157c478bd9Sstevel@tonic-gate 				    &ipmp6->ipv6GroupMemberAddress,
35167c478bd9Sstevel@tonic-gate 				    &ips6->ipv6GroupSourceGroup))) {
35177c478bd9Sstevel@tonic-gate 					if (first_src)
35187c478bd9Sstevel@tonic-gate 						continue;
35197c478bd9Sstevel@tonic-gate 					else
35207c478bd9Sstevel@tonic-gate 						break;
35217c478bd9Sstevel@tonic-gate 				}
35227c478bd9Sstevel@tonic-gate 				if (first_src) {
35237c478bd9Sstevel@tonic-gate 					(void) printf("\t%s:    %s\n",
35247c478bd9Sstevel@tonic-gate 					    fmodestr(
35257c478bd9Sstevel@tonic-gate 					    ipmp6->ipv6GroupMemberFilterMode),
35267c478bd9Sstevel@tonic-gate 					    pr_addr6(
35277c478bd9Sstevel@tonic-gate 					    &ips6->ipv6GroupSourceAddress,
35287c478bd9Sstevel@tonic-gate 					    abuf, sizeof (abuf)));
35297c478bd9Sstevel@tonic-gate 					first_src = B_FALSE;
35307c478bd9Sstevel@tonic-gate 					continue;
35317c478bd9Sstevel@tonic-gate 				}
35327c478bd9Sstevel@tonic-gate 
35337c478bd9Sstevel@tonic-gate 				(void) printf("\t            %s\n",
35347c478bd9Sstevel@tonic-gate 				    pr_addr6(&ips6->ipv6GroupSourceAddress,
35357c478bd9Sstevel@tonic-gate 				    abuf, sizeof (abuf)));
35367c478bd9Sstevel@tonic-gate 			}
35377c478bd9Sstevel@tonic-gate 		}
35387c478bd9Sstevel@tonic-gate 		(void) putchar('\n');
35397c478bd9Sstevel@tonic-gate 	}
35407c478bd9Sstevel@tonic-gate 
35417c478bd9Sstevel@tonic-gate 	(void) putchar('\n');
35427c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
35437c478bd9Sstevel@tonic-gate }
35447c478bd9Sstevel@tonic-gate 
35457c478bd9Sstevel@tonic-gate /* --------------------- ARP_REPORT (netstat -p) -------------------------- */
35467c478bd9Sstevel@tonic-gate 
35477c478bd9Sstevel@tonic-gate static void
35487c478bd9Sstevel@tonic-gate arp_report(mib_item_t *item)
35497c478bd9Sstevel@tonic-gate {
35507c478bd9Sstevel@tonic-gate 	int		jtemp = 0;
35517c478bd9Sstevel@tonic-gate 	char		ifname[LIFNAMSIZ + 1];
35527c478bd9Sstevel@tonic-gate 	char		abuf[MAXHOSTNAMELEN + 1];
35537c478bd9Sstevel@tonic-gate 	char		maskbuf[STR_EXPAND * OCTET_LENGTH + 1];
35547c478bd9Sstevel@tonic-gate 	char		flbuf[32];	/* ACE_F_ flags */
35557c478bd9Sstevel@tonic-gate 	char		xbuf[STR_EXPAND * OCTET_LENGTH + 1];
35567c478bd9Sstevel@tonic-gate 	mib2_ipNetToMediaEntry_t	*np;
35577c478bd9Sstevel@tonic-gate 	int		flags;
35587c478bd9Sstevel@tonic-gate 	boolean_t	first;
35597c478bd9Sstevel@tonic-gate 
35607c478bd9Sstevel@tonic-gate 	if (!(family_selected(AF_INET)))
35617c478bd9Sstevel@tonic-gate 		return;
35627c478bd9Sstevel@tonic-gate 
35637c478bd9Sstevel@tonic-gate 	/* 'for' loop 1: */
35647c478bd9Sstevel@tonic-gate 	for (; item; item = item->next_item) {
35657c478bd9Sstevel@tonic-gate 		if (Dflag) {
35667c478bd9Sstevel@tonic-gate 			(void) printf("\n--- Entry %d ---\n", ++jtemp);
35677c478bd9Sstevel@tonic-gate 			(void) printf("Group = %d, mib_id = %d, "
35687c478bd9Sstevel@tonic-gate 			    "length = %d, valp = 0x%p\n",
35697c478bd9Sstevel@tonic-gate 			    item->group, item->mib_id, item->length,
35707c478bd9Sstevel@tonic-gate 			    item->valp);
35717c478bd9Sstevel@tonic-gate 		}
35727c478bd9Sstevel@tonic-gate 		if (!(item->group == MIB2_IP && item->mib_id == MIB2_IP_MEDIA))
35737c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
35747c478bd9Sstevel@tonic-gate 
35757c478bd9Sstevel@tonic-gate 		if (Dflag)
35767c478bd9Sstevel@tonic-gate 			(void) printf("%u records for "
35777c478bd9Sstevel@tonic-gate 			    "ipNetToMediaEntryTable:\n",
35787c478bd9Sstevel@tonic-gate 			    item->length/sizeof (mib2_ipNetToMediaEntry_t));
35797c478bd9Sstevel@tonic-gate 
35807c478bd9Sstevel@tonic-gate 		first = B_TRUE;
35817c478bd9Sstevel@tonic-gate 		/* 'for' loop 2: */
35827c478bd9Sstevel@tonic-gate 		for (np = (mib2_ipNetToMediaEntry_t *)item->valp;
35837c478bd9Sstevel@tonic-gate 		    (char *)np < (char *)item->valp + item->length;
35847c478bd9Sstevel@tonic-gate 		    /* LINTED: (note 1) */
35857c478bd9Sstevel@tonic-gate 		    np = (mib2_ipNetToMediaEntry_t *)((char *)np +
35867c478bd9Sstevel@tonic-gate 		    ipNetToMediaEntrySize)) {
35877c478bd9Sstevel@tonic-gate 			if (first) {
35887c478bd9Sstevel@tonic-gate 				(void) puts(v4compat ?
35897c478bd9Sstevel@tonic-gate 				    "Net to Media Table" :
35907c478bd9Sstevel@tonic-gate 				    "Net to Media Table: IPv4");
35917c478bd9Sstevel@tonic-gate 				(void) fputs("Device   "
35927c478bd9Sstevel@tonic-gate 				    "IP Address               Mask      ",
35937c478bd9Sstevel@tonic-gate 				    stdout);
35947c478bd9Sstevel@tonic-gate 				(void) puts("Flags   Phys Addr ");
35957c478bd9Sstevel@tonic-gate 				(void) puts("------ -------------------- "
35967c478bd9Sstevel@tonic-gate 				    "--------------- ----- ---------------");
35977c478bd9Sstevel@tonic-gate 				first = B_FALSE;
35987c478bd9Sstevel@tonic-gate 			}
35997c478bd9Sstevel@tonic-gate 
36007c478bd9Sstevel@tonic-gate 			flbuf[0] = '\0';
36017c478bd9Sstevel@tonic-gate 			flags = np->ipNetToMediaInfo.ntm_flags;
36027c478bd9Sstevel@tonic-gate 			if (flags & ACE_F_PERMANENT)
36037c478bd9Sstevel@tonic-gate 				(void) strcat(flbuf, "S");
36047c478bd9Sstevel@tonic-gate 			if (flags & ACE_F_PUBLISH)
36057c478bd9Sstevel@tonic-gate 				(void) strcat(flbuf, "P");
36067c478bd9Sstevel@tonic-gate 			if (flags & ACE_F_DYING)
36077c478bd9Sstevel@tonic-gate 				(void) strcat(flbuf, "D");
36087c478bd9Sstevel@tonic-gate 			if (!(flags & ACE_F_RESOLVED))
36097c478bd9Sstevel@tonic-gate 				(void) strcat(flbuf, "U");
36107c478bd9Sstevel@tonic-gate 			if (flags & ACE_F_MAPPING)
36117c478bd9Sstevel@tonic-gate 				(void) strcat(flbuf, "M");
36127c478bd9Sstevel@tonic-gate 			(void) printf("%-6s %-20s %-15s %-5s %s\n",
36137c478bd9Sstevel@tonic-gate 			    octetstr(&np->ipNetToMediaIfIndex, 'a',
36147c478bd9Sstevel@tonic-gate 			    ifname, sizeof (ifname)),
36157c478bd9Sstevel@tonic-gate 			    pr_addr(np->ipNetToMediaNetAddress,
36167c478bd9Sstevel@tonic-gate 			    abuf, sizeof (abuf)),
36177c478bd9Sstevel@tonic-gate 			    octetstr(&np->ipNetToMediaInfo.ntm_mask, 'd',
36187c478bd9Sstevel@tonic-gate 			    maskbuf, sizeof (maskbuf)),
36197c478bd9Sstevel@tonic-gate 			    flbuf,
36207c478bd9Sstevel@tonic-gate 			    octetstr(&np->ipNetToMediaPhysAddress, 'h',
36217c478bd9Sstevel@tonic-gate 			    xbuf, sizeof (xbuf)));
36227c478bd9Sstevel@tonic-gate 		} /* 'for' loop 2 ends */
36237c478bd9Sstevel@tonic-gate 	} /* 'for' loop 1 ends */
36247c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
36257c478bd9Sstevel@tonic-gate }
36267c478bd9Sstevel@tonic-gate 
36277c478bd9Sstevel@tonic-gate /* --------------------- NDP_REPORT (netstat -p) -------------------------- */
36287c478bd9Sstevel@tonic-gate 
36297c478bd9Sstevel@tonic-gate static void
36307c478bd9Sstevel@tonic-gate ndp_report(mib_item_t *item)
36317c478bd9Sstevel@tonic-gate {
36327c478bd9Sstevel@tonic-gate 	int		jtemp = 0;
36337c478bd9Sstevel@tonic-gate 	char		abuf[MAXHOSTNAMELEN + 1];
36347c478bd9Sstevel@tonic-gate 	char		*state;
36357c478bd9Sstevel@tonic-gate 	char		*type;
36367c478bd9Sstevel@tonic-gate 	char		xbuf[STR_EXPAND * OCTET_LENGTH + 1];
36377c478bd9Sstevel@tonic-gate 	mib2_ipv6NetToMediaEntry_t	*np6;
36387c478bd9Sstevel@tonic-gate 	char		ifname[LIFNAMSIZ + 1];
36397c478bd9Sstevel@tonic-gate 	char		*ifnamep;
36407c478bd9Sstevel@tonic-gate 	boolean_t	first;
36417c478bd9Sstevel@tonic-gate 
36427c478bd9Sstevel@tonic-gate 	if (!(family_selected(AF_INET6)))
36437c478bd9Sstevel@tonic-gate 		return;
36447c478bd9Sstevel@tonic-gate 
36457c478bd9Sstevel@tonic-gate 	/* 'for' loop 1: */
36467c478bd9Sstevel@tonic-gate 	for (; item; item = item->next_item) {
36477c478bd9Sstevel@tonic-gate 		if (Dflag) {
36487c478bd9Sstevel@tonic-gate 			(void) printf("\n--- Entry %d ---\n", ++jtemp);
36497c478bd9Sstevel@tonic-gate 			(void) printf("Group = %d, mib_id = %d, "
36507c478bd9Sstevel@tonic-gate 			    "length = %d, valp = 0x%p\n",
36517c478bd9Sstevel@tonic-gate 			    item->group, item->mib_id, item->length,
36527c478bd9Sstevel@tonic-gate 			    item->valp);
36537c478bd9Sstevel@tonic-gate 		}
36547c478bd9Sstevel@tonic-gate 		if (!(item->group == MIB2_IP6 &&
36557c478bd9Sstevel@tonic-gate 		    item->mib_id == MIB2_IP6_MEDIA))
36567c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
36577c478bd9Sstevel@tonic-gate 
36587c478bd9Sstevel@tonic-gate 		first = B_TRUE;
36597c478bd9Sstevel@tonic-gate 		/* 'for' loop 2: */
36607c478bd9Sstevel@tonic-gate 		for (np6 = (mib2_ipv6NetToMediaEntry_t *)item->valp;
36617c478bd9Sstevel@tonic-gate 		    (char *)np6 < (char *)item->valp + item->length;
36627c478bd9Sstevel@tonic-gate 		    /* LINTED: (note 1) */
36637c478bd9Sstevel@tonic-gate 		    np6 = (mib2_ipv6NetToMediaEntry_t *)((char *)np6 +
36647c478bd9Sstevel@tonic-gate 		    ipv6NetToMediaEntrySize)) {
36657c478bd9Sstevel@tonic-gate 			if (first) {
36667c478bd9Sstevel@tonic-gate 				(void) puts("\nNet to Media Table: IPv6");
36677c478bd9Sstevel@tonic-gate 				(void) puts(" If   Physical Address   "
36687c478bd9Sstevel@tonic-gate 				    " Type      State      Destination/Mask");
36697c478bd9Sstevel@tonic-gate 				(void) puts("----- -----------------  "
36707c478bd9Sstevel@tonic-gate 				    "------- ------------ "
36717c478bd9Sstevel@tonic-gate 				    "---------------------------");
36727c478bd9Sstevel@tonic-gate 				first = B_FALSE;
36737c478bd9Sstevel@tonic-gate 			}
36747c478bd9Sstevel@tonic-gate 
36757c478bd9Sstevel@tonic-gate 			ifnamep = if_indextoname(np6->ipv6NetToMediaIfIndex,
36767c478bd9Sstevel@tonic-gate 			    ifname);
36777c478bd9Sstevel@tonic-gate 			if (ifnamep == NULL) {
36787c478bd9Sstevel@tonic-gate 				(void) printf("Invalid ifindex %d\n",
36797c478bd9Sstevel@tonic-gate 				    np6->ipv6NetToMediaIfIndex);
36807c478bd9Sstevel@tonic-gate 				continue; /* 'for' loop 2 */
36817c478bd9Sstevel@tonic-gate 			}
36827c478bd9Sstevel@tonic-gate 			switch (np6->ipv6NetToMediaState) {
36837c478bd9Sstevel@tonic-gate 			case ND_INCOMPLETE:
36847c478bd9Sstevel@tonic-gate 				state = "INCOMPLETE";
36857c478bd9Sstevel@tonic-gate 				break;
36867c478bd9Sstevel@tonic-gate 			case ND_REACHABLE:
36877c478bd9Sstevel@tonic-gate 				state = "REACHABLE";
36887c478bd9Sstevel@tonic-gate 				break;
36897c478bd9Sstevel@tonic-gate 			case ND_STALE:
36907c478bd9Sstevel@tonic-gate 				state = "STALE";
36917c478bd9Sstevel@tonic-gate 				break;
36927c478bd9Sstevel@tonic-gate 			case ND_DELAY:
36937c478bd9Sstevel@tonic-gate 				state = "DELAY";
36947c478bd9Sstevel@tonic-gate 				break;
36957c478bd9Sstevel@tonic-gate 			case ND_PROBE:
36967c478bd9Sstevel@tonic-gate 				state = "PROBE";
36977c478bd9Sstevel@tonic-gate 				break;
36987c478bd9Sstevel@tonic-gate 			case ND_UNREACHABLE:
36997c478bd9Sstevel@tonic-gate 				state = "UNREACHABLE";
37007c478bd9Sstevel@tonic-gate 				break;
37017c478bd9Sstevel@tonic-gate 			default:
37027c478bd9Sstevel@tonic-gate 				state = "UNKNOWN";
37037c478bd9Sstevel@tonic-gate 			}
37047c478bd9Sstevel@tonic-gate 
37057c478bd9Sstevel@tonic-gate 			switch (np6->ipv6NetToMediaType) {
37067c478bd9Sstevel@tonic-gate 			case 1:
37077c478bd9Sstevel@tonic-gate 				type = "other";
37087c478bd9Sstevel@tonic-gate 				break;
37097c478bd9Sstevel@tonic-gate 			case 2:
37107c478bd9Sstevel@tonic-gate 				type = "dynamic";
37117c478bd9Sstevel@tonic-gate 				break;
37127c478bd9Sstevel@tonic-gate 			case 3:
37137c478bd9Sstevel@tonic-gate 				type = "static";
37147c478bd9Sstevel@tonic-gate 				break;
37157c478bd9Sstevel@tonic-gate 			case 4:
37167c478bd9Sstevel@tonic-gate 				type = "local";
37177c478bd9Sstevel@tonic-gate 				break;
37187c478bd9Sstevel@tonic-gate 			}
37197c478bd9Sstevel@tonic-gate 			(void) printf("%-5s %-17s  %-7s %-12s %-27s\n",
37207c478bd9Sstevel@tonic-gate 			    ifnamep,
37217c478bd9Sstevel@tonic-gate 			    octetstr(&np6->ipv6NetToMediaPhysAddress, 'h',
37227c478bd9Sstevel@tonic-gate 			    xbuf, sizeof (xbuf)),
37237c478bd9Sstevel@tonic-gate 			    type,
37247c478bd9Sstevel@tonic-gate 			    state,
37257c478bd9Sstevel@tonic-gate 			    pr_addr6(&np6->ipv6NetToMediaNetAddress,
37267c478bd9Sstevel@tonic-gate 			    abuf, sizeof (abuf)));
37277c478bd9Sstevel@tonic-gate 		} /* 'for' loop 2 ends */
37287c478bd9Sstevel@tonic-gate 	} /* 'for' loop 1 ends */
37297c478bd9Sstevel@tonic-gate 	(void) putchar('\n');
37307c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
37317c478bd9Sstevel@tonic-gate }
37327c478bd9Sstevel@tonic-gate 
37337c478bd9Sstevel@tonic-gate /* ------------------------- ire_report (netstat -r) ------------------------ */
37347c478bd9Sstevel@tonic-gate 
3735*45916cd2Sjpk typedef struct sec_attr_list_s {
3736*45916cd2Sjpk 	struct sec_attr_list_s *sal_next;
3737*45916cd2Sjpk 	const mib2_ipAttributeEntry_t *sal_attr;
3738*45916cd2Sjpk } sec_attr_list_t;
3739*45916cd2Sjpk 
3740*45916cd2Sjpk static boolean_t ire_report_item_v4(const mib2_ipRouteEntry_t *, boolean_t,
3741*45916cd2Sjpk     const sec_attr_list_t *);
3742*45916cd2Sjpk static boolean_t ire_report_item_v4src(const mib2_ipRouteEntry_t *, boolean_t,
3743*45916cd2Sjpk     const sec_attr_list_t *);
3744*45916cd2Sjpk static boolean_t ire_report_item_v6(const mib2_ipv6RouteEntry_t *, boolean_t,
3745*45916cd2Sjpk     const sec_attr_list_t *);
3746*45916cd2Sjpk static const char *pr_secattr(const sec_attr_list_t *);
37477c478bd9Sstevel@tonic-gate 
37487c478bd9Sstevel@tonic-gate static void
3749*45916cd2Sjpk ire_report(const mib_item_t *item)
37507c478bd9Sstevel@tonic-gate {
37517c478bd9Sstevel@tonic-gate 	int			jtemp = 0;
37527c478bd9Sstevel@tonic-gate 	boolean_t		print_hdr_once_v4 = B_TRUE;
37537c478bd9Sstevel@tonic-gate 	boolean_t		print_hdr_once_v6 = B_TRUE;
37547c478bd9Sstevel@tonic-gate 	mib2_ipRouteEntry_t	*rp;
37557c478bd9Sstevel@tonic-gate 	mib2_ipv6RouteEntry_t	*rp6;
3756*45916cd2Sjpk 	sec_attr_list_t		**v4_attrs, **v4a;
3757*45916cd2Sjpk 	sec_attr_list_t		**v6_attrs, **v6a;
3758*45916cd2Sjpk 	sec_attr_list_t		*all_attrs, *aptr;
3759*45916cd2Sjpk 	const mib_item_t	*iptr;
3760*45916cd2Sjpk 	int			ipv4_route_count, ipv6_route_count;
3761*45916cd2Sjpk 	int			route_attrs_count;
3762*45916cd2Sjpk 
3763*45916cd2Sjpk 	/*
3764*45916cd2Sjpk 	 * Preparation pass: the kernel returns separate entries for IP routing
3765*45916cd2Sjpk 	 * table entries and security attributes.  We loop through the
3766*45916cd2Sjpk 	 * attributes first and link them into lists.
3767*45916cd2Sjpk 	 */
3768*45916cd2Sjpk 	ipv4_route_count = ipv6_route_count = route_attrs_count = 0;
3769*45916cd2Sjpk 	for (iptr = item; iptr != NULL; iptr = iptr->next_item) {
3770*45916cd2Sjpk 		if (iptr->group == MIB2_IP6 && iptr->mib_id == MIB2_IP6_ROUTE)
3771*45916cd2Sjpk 			ipv6_route_count += iptr->length / ipv6RouteEntrySize;
3772*45916cd2Sjpk 		if (iptr->group == MIB2_IP && iptr->mib_id == MIB2_IP_ROUTE)
3773*45916cd2Sjpk 			ipv4_route_count += iptr->length / ipRouteEntrySize;
3774*45916cd2Sjpk 		if ((iptr->group == MIB2_IP || iptr->group == MIB2_IP6) &&
3775*45916cd2Sjpk 		    iptr->mib_id == EXPER_IP_RTATTR)
3776*45916cd2Sjpk 			route_attrs_count += iptr->length /
3777*45916cd2Sjpk 			    ipRouteAttributeSize;
3778*45916cd2Sjpk 	}
3779*45916cd2Sjpk 	v4_attrs = v6_attrs = NULL;
3780*45916cd2Sjpk 	all_attrs = NULL;
3781*45916cd2Sjpk 	if (family_selected(AF_INET) && ipv4_route_count > 0) {
3782*45916cd2Sjpk 		v4_attrs = calloc(ipv4_route_count, sizeof (*v4_attrs));
3783*45916cd2Sjpk 		if (v4_attrs == NULL) {
3784*45916cd2Sjpk 			perror("ire_report calloc v4_attrs failed");
3785*45916cd2Sjpk 			return;
3786*45916cd2Sjpk 		}
3787*45916cd2Sjpk 	}
3788*45916cd2Sjpk 	if (family_selected(AF_INET6) && ipv6_route_count > 0) {
3789*45916cd2Sjpk 		v6_attrs = calloc(ipv6_route_count, sizeof (*v6_attrs));
3790*45916cd2Sjpk 		if (v6_attrs == NULL) {
3791*45916cd2Sjpk 			perror("ire_report calloc v6_attrs failed");
3792*45916cd2Sjpk 			goto ire_report_done;
3793*45916cd2Sjpk 		}
3794*45916cd2Sjpk 	}
3795*45916cd2Sjpk 	if (route_attrs_count > 0) {
3796*45916cd2Sjpk 		all_attrs = malloc(route_attrs_count * sizeof (*all_attrs));
3797*45916cd2Sjpk 		if (all_attrs == NULL) {
3798*45916cd2Sjpk 			perror("ire_report malloc all_attrs failed");
3799*45916cd2Sjpk 			goto ire_report_done;
3800*45916cd2Sjpk 		}
3801*45916cd2Sjpk 	}
3802*45916cd2Sjpk 	aptr = all_attrs;
3803*45916cd2Sjpk 	for (iptr = item; iptr != NULL; iptr = iptr->next_item) {
3804*45916cd2Sjpk 		mib2_ipAttributeEntry_t *iae;
3805*45916cd2Sjpk 		sec_attr_list_t **alp;
3806*45916cd2Sjpk 
3807*45916cd2Sjpk 		if (v4_attrs != NULL && iptr->group == MIB2_IP &&
3808*45916cd2Sjpk 		    iptr->mib_id == EXPER_IP_RTATTR) {
3809*45916cd2Sjpk 			alp = v4_attrs;
3810*45916cd2Sjpk 		} else if (v6_attrs != NULL && iptr->group == MIB2_IP6 &&
3811*45916cd2Sjpk 		    iptr->mib_id == EXPER_IP_RTATTR) {
3812*45916cd2Sjpk 			alp = v6_attrs;
3813*45916cd2Sjpk 		} else {
3814*45916cd2Sjpk 			continue;
3815*45916cd2Sjpk 		}
3816*45916cd2Sjpk 		for (iae = iptr->valp;
3817*45916cd2Sjpk 		    (char *)iae < (char *)iptr->valp + iptr->length;
3818*45916cd2Sjpk 		    /* LINTED: (note 1) */
3819*45916cd2Sjpk 		    iae = (mib2_ipAttributeEntry_t *)((char *)iae +
3820*45916cd2Sjpk 		    ipRouteAttributeSize)) {
3821*45916cd2Sjpk 			aptr->sal_next = alp[iae->iae_routeidx];
3822*45916cd2Sjpk 			aptr->sal_attr = iae;
3823*45916cd2Sjpk 			alp[iae->iae_routeidx] = aptr++;
3824*45916cd2Sjpk 		}
3825*45916cd2Sjpk 	}
38267c478bd9Sstevel@tonic-gate 
38277c478bd9Sstevel@tonic-gate 	/* 'for' loop 1: */
3828*45916cd2Sjpk 	v4a = v4_attrs;
3829*45916cd2Sjpk 	v6a = v6_attrs;
3830*45916cd2Sjpk 	for (; item != NULL; item = item->next_item) {
38317c478bd9Sstevel@tonic-gate 		if (Dflag) {
38327c478bd9Sstevel@tonic-gate 			(void) printf("\n--- Entry %d ---\n", ++jtemp);
38337c478bd9Sstevel@tonic-gate 			(void) printf("Group = %d, mib_id = %d, "
38347c478bd9Sstevel@tonic-gate 			    "length = %d, valp = 0x%p\n",
38357c478bd9Sstevel@tonic-gate 			    item->group, item->mib_id,
38367c478bd9Sstevel@tonic-gate 			    item->length, item->valp);
38377c478bd9Sstevel@tonic-gate 		}
38387c478bd9Sstevel@tonic-gate 		if (!((item->group == MIB2_IP &&
38397c478bd9Sstevel@tonic-gate 		    item->mib_id == MIB2_IP_ROUTE) ||
38407c478bd9Sstevel@tonic-gate 		    (item->group == MIB2_IP6 &&
38417c478bd9Sstevel@tonic-gate 		    item->mib_id == MIB2_IP6_ROUTE)))
38427c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
38437c478bd9Sstevel@tonic-gate 
38447c478bd9Sstevel@tonic-gate 		if (item->group == MIB2_IP && !family_selected(AF_INET))
38457c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
38467c478bd9Sstevel@tonic-gate 		else if (item->group == MIB2_IP6 && !family_selected(AF_INET6))
38477c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
38487c478bd9Sstevel@tonic-gate 
38497c478bd9Sstevel@tonic-gate 		if (Dflag) {
38507c478bd9Sstevel@tonic-gate 			if (item->group == MIB2_IP) {
38517c478bd9Sstevel@tonic-gate 				(void) printf("%u records for "
38527c478bd9Sstevel@tonic-gate 				    "ipRouteEntryTable:\n",
38537c478bd9Sstevel@tonic-gate 				    item->length/sizeof (mib2_ipRouteEntry_t));
38547c478bd9Sstevel@tonic-gate 			} else {
38557c478bd9Sstevel@tonic-gate 				(void) printf("%u records for "
38567c478bd9Sstevel@tonic-gate 				    "ipv6RouteEntryTable:\n",
38577c478bd9Sstevel@tonic-gate 				    item->length/
38587c478bd9Sstevel@tonic-gate 				    sizeof (mib2_ipv6RouteEntry_t));
38597c478bd9Sstevel@tonic-gate 			}
38607c478bd9Sstevel@tonic-gate 		}
38617c478bd9Sstevel@tonic-gate 
38627c478bd9Sstevel@tonic-gate 		if (item->group == MIB2_IP) {
38637c478bd9Sstevel@tonic-gate 			for (rp = (mib2_ipRouteEntry_t *)item->valp;
38647c478bd9Sstevel@tonic-gate 			    (char *)rp < (char *)item->valp + item->length;
38657c478bd9Sstevel@tonic-gate 			    /* LINTED: (note 1) */
38667c478bd9Sstevel@tonic-gate 			    rp = (mib2_ipRouteEntry_t *)((char *)rp +
38677c478bd9Sstevel@tonic-gate 			    ipRouteEntrySize)) {
3868*45916cd2Sjpk 				aptr = v4a == NULL ? NULL : *v4a++;
38697c478bd9Sstevel@tonic-gate 				print_hdr_once_v4 = ire_report_item_v4(rp,
3870*45916cd2Sjpk 				    print_hdr_once_v4, aptr);
38717c478bd9Sstevel@tonic-gate 			}
3872*45916cd2Sjpk 			if (v4a != NULL)
3873*45916cd2Sjpk 				v4a -= item->length / ipRouteEntrySize;
38747c478bd9Sstevel@tonic-gate 			print_hdr_once_v4 = B_TRUE;
38757c478bd9Sstevel@tonic-gate 			for (rp = (mib2_ipRouteEntry_t *)item->valp;
38767c478bd9Sstevel@tonic-gate 			    (char *)rp < (char *)item->valp + item->length;
38777c478bd9Sstevel@tonic-gate 			    /* LINTED: (note 1) */
38787c478bd9Sstevel@tonic-gate 			    rp = (mib2_ipRouteEntry_t *)((char *)rp +
38797c478bd9Sstevel@tonic-gate 			    ipRouteEntrySize)) {
3880*45916cd2Sjpk 				aptr = v4a == NULL ? NULL : *v4a++;
38817c478bd9Sstevel@tonic-gate 				print_hdr_once_v4 = ire_report_item_v4src(rp,
3882*45916cd2Sjpk 				    print_hdr_once_v4, aptr);
38837c478bd9Sstevel@tonic-gate 			}
38847c478bd9Sstevel@tonic-gate 		} else {
38857c478bd9Sstevel@tonic-gate 			for (rp6 = (mib2_ipv6RouteEntry_t *)item->valp;
38867c478bd9Sstevel@tonic-gate 			    (char *)rp6 < (char *)item->valp + item->length;
38877c478bd9Sstevel@tonic-gate 			    /* LINTED: (note 1) */
38887c478bd9Sstevel@tonic-gate 			    rp6 = (mib2_ipv6RouteEntry_t *)((char *)rp6 +
38897c478bd9Sstevel@tonic-gate 			    ipv6RouteEntrySize)) {
3890*45916cd2Sjpk 				aptr = v6a == NULL ? NULL : *v6a++;
38917c478bd9Sstevel@tonic-gate 				print_hdr_once_v6 = ire_report_item_v6(rp6,
3892*45916cd2Sjpk 				    print_hdr_once_v6, aptr);
38937c478bd9Sstevel@tonic-gate 			}
38947c478bd9Sstevel@tonic-gate 		}
38957c478bd9Sstevel@tonic-gate 	} /* 'for' loop 1 ends */
38967c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
3897*45916cd2Sjpk ire_report_done:
3898*45916cd2Sjpk 	if (v4_attrs != NULL)
3899*45916cd2Sjpk 		free(v4_attrs);
3900*45916cd2Sjpk 	if (v6_attrs != NULL)
3901*45916cd2Sjpk 		free(v6_attrs);
3902*45916cd2Sjpk 	if (all_attrs != NULL)
3903*45916cd2Sjpk 		free(all_attrs);
39047c478bd9Sstevel@tonic-gate }
39057c478bd9Sstevel@tonic-gate 
39067c478bd9Sstevel@tonic-gate /*
39077c478bd9Sstevel@tonic-gate  * Match a user-supplied device name.  We do this by string because
39087c478bd9Sstevel@tonic-gate  * the MIB2 interface gives us interface name strings rather than
39097c478bd9Sstevel@tonic-gate  * ifIndex numbers.  The "none" rule matches only routes with no
39107c478bd9Sstevel@tonic-gate  * interface.  The "any" rule matches routes with any non-blank
39117c478bd9Sstevel@tonic-gate  * interface.  A base name ("hme0") matches all aliases as well
39127c478bd9Sstevel@tonic-gate  * ("hme0:1").
39137c478bd9Sstevel@tonic-gate  */
39147c478bd9Sstevel@tonic-gate static boolean_t
39157c478bd9Sstevel@tonic-gate dev_name_match(const DeviceName *devnam, const char *ifname)
39167c478bd9Sstevel@tonic-gate {
39177c478bd9Sstevel@tonic-gate 	int iflen;
39187c478bd9Sstevel@tonic-gate 
39197c478bd9Sstevel@tonic-gate 	if (ifname == NULL)
39207c478bd9Sstevel@tonic-gate 		return (devnam->o_length == 0);		/* "none" */
39217c478bd9Sstevel@tonic-gate 	if (*ifname == '\0')
39227c478bd9Sstevel@tonic-gate 		return (devnam->o_length != 0);		/* "any" */
39237c478bd9Sstevel@tonic-gate 	iflen = strlen(ifname);
39247c478bd9Sstevel@tonic-gate 	/* The check for ':' here supports interface aliases. */
39257c478bd9Sstevel@tonic-gate 	if (iflen > devnam->o_length ||
39267c478bd9Sstevel@tonic-gate 	    (iflen < devnam->o_length && devnam->o_bytes[iflen] != ':'))
39277c478bd9Sstevel@tonic-gate 		return (B_FALSE);
39287c478bd9Sstevel@tonic-gate 	return (strncmp(ifname, devnam->o_bytes, iflen) == 0);
39297c478bd9Sstevel@tonic-gate }
39307c478bd9Sstevel@tonic-gate 
39317c478bd9Sstevel@tonic-gate /*
39327c478bd9Sstevel@tonic-gate  * Match a user-supplied IP address list.  The "any" rule matches any
39337c478bd9Sstevel@tonic-gate  * non-zero address.  The "none" rule matches only the zero address.
39347c478bd9Sstevel@tonic-gate  * IPv6 addresses supplied by the user are ignored.  If the user
39357c478bd9Sstevel@tonic-gate  * supplies a subnet mask, then match routes that are at least that
39367c478bd9Sstevel@tonic-gate  * specific (use the user's mask).  If the user supplies only an
39377c478bd9Sstevel@tonic-gate  * address, then select any routes that would match (use the route's
39387c478bd9Sstevel@tonic-gate  * mask).
39397c478bd9Sstevel@tonic-gate  */
39407c478bd9Sstevel@tonic-gate static boolean_t
39417c478bd9Sstevel@tonic-gate v4_addr_match(IpAddress addr, IpAddress mask, const filter_t *fp)
39427c478bd9Sstevel@tonic-gate {
39437c478bd9Sstevel@tonic-gate 	char **app;
39447c478bd9Sstevel@tonic-gate 	char *aptr;
39457c478bd9Sstevel@tonic-gate 	in_addr_t faddr, fmask;
39467c478bd9Sstevel@tonic-gate 
39477c478bd9Sstevel@tonic-gate 	if (fp->u.a.f_address == NULL) {
39487c478bd9Sstevel@tonic-gate 		if (IN6_IS_ADDR_UNSPECIFIED(&fp->u.a.f_mask))
39497c478bd9Sstevel@tonic-gate 			return (addr != INADDR_ANY);	/* "any" */
39507c478bd9Sstevel@tonic-gate 		else
39517c478bd9Sstevel@tonic-gate 			return (addr == INADDR_ANY);	/* "none" */
39527c478bd9Sstevel@tonic-gate 	}
39537c478bd9Sstevel@tonic-gate 	if (!IN6_IS_V4MASK(fp->u.a.f_mask))
39547c478bd9Sstevel@tonic-gate 		return (B_FALSE);
39557c478bd9Sstevel@tonic-gate 	IN6_V4MAPPED_TO_IPADDR(&fp->u.a.f_mask, fmask);
39567c478bd9Sstevel@tonic-gate 	if (fmask != IP_HOST_MASK) {
39577c478bd9Sstevel@tonic-gate 		if (fmask > mask)
39587c478bd9Sstevel@tonic-gate 			return (B_FALSE);
39597c478bd9Sstevel@tonic-gate 		mask = fmask;
39607c478bd9Sstevel@tonic-gate 	}
39617c478bd9Sstevel@tonic-gate 	for (app = fp->u.a.f_address->h_addr_list; (aptr = *app) != NULL; app++)
39627c478bd9Sstevel@tonic-gate 		/* LINTED: (note 1) */
39637c478bd9Sstevel@tonic-gate 		if (IN6_IS_ADDR_V4MAPPED((in6_addr_t *)aptr)) {
39647c478bd9Sstevel@tonic-gate 			/* LINTED: (note 1) */
39657c478bd9Sstevel@tonic-gate 			IN6_V4MAPPED_TO_IPADDR((in6_addr_t *)aptr, faddr);
39667c478bd9Sstevel@tonic-gate 			if (((faddr ^ addr) & mask) == 0)
39677c478bd9Sstevel@tonic-gate 				return (B_TRUE);
39687c478bd9Sstevel@tonic-gate 		}
39697c478bd9Sstevel@tonic-gate 	return (B_FALSE);
39707c478bd9Sstevel@tonic-gate }
39717c478bd9Sstevel@tonic-gate 
39727c478bd9Sstevel@tonic-gate /*
39737c478bd9Sstevel@tonic-gate  * Run through the filter list for an IPv4 MIB2 route entry.  If all
39747c478bd9Sstevel@tonic-gate  * filters of a given type fail to match, then the route is filtered
39757c478bd9Sstevel@tonic-gate  * out (not displayed).  If no filter is given or at least one filter
39767c478bd9Sstevel@tonic-gate  * of each type matches, then display the route.
39777c478bd9Sstevel@tonic-gate  */
39787c478bd9Sstevel@tonic-gate static boolean_t
3979*45916cd2Sjpk ire_filter_match_v4(const mib2_ipRouteEntry_t *rp, uint_t flag_b)
39807c478bd9Sstevel@tonic-gate {
39817c478bd9Sstevel@tonic-gate 	filter_t *fp;
39827c478bd9Sstevel@tonic-gate 	int idx;
39837c478bd9Sstevel@tonic-gate 
39847c478bd9Sstevel@tonic-gate 	/* 'for' loop 1: */
39857c478bd9Sstevel@tonic-gate 	for (idx = 0; idx < NFILTERKEYS; idx++)
39867c478bd9Sstevel@tonic-gate 		if ((fp = filters[idx]) != NULL) {
39877c478bd9Sstevel@tonic-gate 			/* 'for' loop 2: */
39887c478bd9Sstevel@tonic-gate 			for (; fp != NULL; fp = fp->f_next) {
39897c478bd9Sstevel@tonic-gate 				switch (idx) {
39907c478bd9Sstevel@tonic-gate 				case FK_AF:
39917c478bd9Sstevel@tonic-gate 					if (fp->u.f_family != AF_INET)
39927c478bd9Sstevel@tonic-gate 						continue; /* 'for' loop 2 */
39937c478bd9Sstevel@tonic-gate 					break;
39947c478bd9Sstevel@tonic-gate 				case FK_INIF:
39957c478bd9Sstevel@tonic-gate 					if (!dev_name_match(&rp->ipRouteInfo.
39967c478bd9Sstevel@tonic-gate 					    re_in_ill, fp->u.f_ifname))
39977c478bd9Sstevel@tonic-gate 						continue; /* 'for' loop 2 */
39987c478bd9Sstevel@tonic-gate 					break;
39997c478bd9Sstevel@tonic-gate 				case FK_OUTIF:
40007c478bd9Sstevel@tonic-gate 					if (!dev_name_match(&rp->ipRouteIfIndex,
40017c478bd9Sstevel@tonic-gate 					    fp->u.f_ifname))
40027c478bd9Sstevel@tonic-gate 						continue; /* 'for' loop 2 */
40037c478bd9Sstevel@tonic-gate 					break;
40047c478bd9Sstevel@tonic-gate 				case FK_SRC:
40057c478bd9Sstevel@tonic-gate 					if (!v4_addr_match(rp->ipRouteInfo.
40067c478bd9Sstevel@tonic-gate 					    re_in_src_addr, IP_HOST_MASK, fp))
40077c478bd9Sstevel@tonic-gate 						continue; /* 'for' loop 2 */
40087c478bd9Sstevel@tonic-gate 					break;
40097c478bd9Sstevel@tonic-gate 				case FK_DST:
40107c478bd9Sstevel@tonic-gate 					if (!v4_addr_match(rp->ipRouteDest,
40117c478bd9Sstevel@tonic-gate 					    rp->ipRouteMask, fp))
40127c478bd9Sstevel@tonic-gate 						continue; /* 'for' loop 2 */
40137c478bd9Sstevel@tonic-gate 					break;
40147c478bd9Sstevel@tonic-gate 				case FK_FLAGS:
40157c478bd9Sstevel@tonic-gate 					if ((flag_b & fp->u.f.f_flagset) !=
40167c478bd9Sstevel@tonic-gate 					    fp->u.f.f_flagset ||
40177c478bd9Sstevel@tonic-gate 					    (flag_b & fp->u.f.f_flagclear))
40187c478bd9Sstevel@tonic-gate 						continue; /* 'for' loop 2 */
40197c478bd9Sstevel@tonic-gate 					break;
40207c478bd9Sstevel@tonic-gate 				}
40217c478bd9Sstevel@tonic-gate 				break;
40227c478bd9Sstevel@tonic-gate 			} /* 'for' loop 2 ends */
40237c478bd9Sstevel@tonic-gate 			if (fp == NULL)
40247c478bd9Sstevel@tonic-gate 				return (B_FALSE);
40257c478bd9Sstevel@tonic-gate 		}
40267c478bd9Sstevel@tonic-gate 	/* 'for' loop 1 ends */
40277c478bd9Sstevel@tonic-gate 	return (B_TRUE);
40287c478bd9Sstevel@tonic-gate }
40297c478bd9Sstevel@tonic-gate 
40307c478bd9Sstevel@tonic-gate /*
40317c478bd9Sstevel@tonic-gate  * Given an IPv4 MIB2 route entry, form the list of flags for the
40327c478bd9Sstevel@tonic-gate  * route.
40337c478bd9Sstevel@tonic-gate  */
40347c478bd9Sstevel@tonic-gate static uint_t
4035*45916cd2Sjpk form_v4_route_flags(const mib2_ipRouteEntry_t *rp, char *flags)
40367c478bd9Sstevel@tonic-gate {
40377c478bd9Sstevel@tonic-gate 	uint_t flag_b;
40387c478bd9Sstevel@tonic-gate 
40397c478bd9Sstevel@tonic-gate 	flag_b = FLF_U;
40407c478bd9Sstevel@tonic-gate 	(void) strcpy(flags, "U");
40417c478bd9Sstevel@tonic-gate 	if (rp->ipRouteInfo.re_ire_type == IRE_DEFAULT ||
40427c478bd9Sstevel@tonic-gate 	    rp->ipRouteInfo.re_ire_type == IRE_PREFIX ||
40437c478bd9Sstevel@tonic-gate 	    rp->ipRouteInfo.re_ire_type == IRE_HOST ||
40447c478bd9Sstevel@tonic-gate 	    rp->ipRouteInfo.re_ire_type == IRE_HOST_REDIRECT) {
40457c478bd9Sstevel@tonic-gate 		(void) strcat(flags, "G");
40467c478bd9Sstevel@tonic-gate 		flag_b |= FLF_G;
40477c478bd9Sstevel@tonic-gate 	}
40487c478bd9Sstevel@tonic-gate 	if (rp->ipRouteMask == IP_HOST_MASK) {
40497c478bd9Sstevel@tonic-gate 		(void) strcat(flags, "H");
40507c478bd9Sstevel@tonic-gate 		flag_b |= FLF_H;
40517c478bd9Sstevel@tonic-gate 	}
40527c478bd9Sstevel@tonic-gate 	if (rp->ipRouteInfo.re_ire_type == IRE_HOST_REDIRECT) {
40537c478bd9Sstevel@tonic-gate 		(void) strcat(flags, "D");
40547c478bd9Sstevel@tonic-gate 		flag_b |= FLF_D;
40557c478bd9Sstevel@tonic-gate 	}
40567c478bd9Sstevel@tonic-gate 	if (rp->ipRouteInfo.re_ire_type == IRE_CACHE) {
40577c478bd9Sstevel@tonic-gate 		/* Address resolution */
40587c478bd9Sstevel@tonic-gate 		(void) strcat(flags, "A");
40597c478bd9Sstevel@tonic-gate 		flag_b |= FLF_A;
40607c478bd9Sstevel@tonic-gate 	}
40617c478bd9Sstevel@tonic-gate 	if (rp->ipRouteInfo.re_ire_type == IRE_BROADCAST) {	/* Broadcast */
40627c478bd9Sstevel@tonic-gate 		(void) strcat(flags, "B");
40637c478bd9Sstevel@tonic-gate 		flag_b |= FLF_B;
40647c478bd9Sstevel@tonic-gate 	}
40657c478bd9Sstevel@tonic-gate 	if (rp->ipRouteInfo.re_ire_type == IRE_LOCAL) {		/* Local */
40667c478bd9Sstevel@tonic-gate 		(void) strcat(flags, "L");
40677c478bd9Sstevel@tonic-gate 		flag_b |= FLF_L;
40687c478bd9Sstevel@tonic-gate 	}
40697c478bd9Sstevel@tonic-gate 	if (rp->ipRouteInfo.re_flags & RTF_MULTIRT) {
40707c478bd9Sstevel@tonic-gate 		(void) strcat(flags, "M");			/* Multiroute */
40717c478bd9Sstevel@tonic-gate 		flag_b |= FLF_M;
40727c478bd9Sstevel@tonic-gate 	}
40737c478bd9Sstevel@tonic-gate 	if (rp->ipRouteInfo.re_flags & RTF_SETSRC) {
40747c478bd9Sstevel@tonic-gate 		(void) strcat(flags, "S");			/* Setsrc */
40757c478bd9Sstevel@tonic-gate 		flag_b |= FLF_S;
40767c478bd9Sstevel@tonic-gate 	}
40777c478bd9Sstevel@tonic-gate 	return (flag_b);
40787c478bd9Sstevel@tonic-gate }
40797c478bd9Sstevel@tonic-gate 
4080*45916cd2Sjpk static const char ire_hdr_v4[] =
4081*45916cd2Sjpk "\n%s Table: IPv4\n";
4082*45916cd2Sjpk static const char ire_hdr_v4_compat[] =
4083*45916cd2Sjpk "\n%s Table:\n";
4084*45916cd2Sjpk static const char ire_hdr_v4_verbose[] =
4085*45916cd2Sjpk "  Destination             Mask           Gateway          Device Mxfrg "
4086*45916cd2Sjpk "Rtt   Ref Flg  Out  In/Fwd %s\n"
4087*45916cd2Sjpk "-------------------- --------------- -------------------- ------ ----- "
4088*45916cd2Sjpk "----- --- --- ----- ------ %s\n";
4089*45916cd2Sjpk 
4090*45916cd2Sjpk static const char ire_hdr_v4_normal[] =
4091*45916cd2Sjpk "  Destination           Gateway           Flags  Ref   Use   Interface %s\n"
4092*45916cd2Sjpk "-------------------- -------------------- ----- ----- ------ --------- %s\n";
4093*45916cd2Sjpk 
40947c478bd9Sstevel@tonic-gate static boolean_t
4095*45916cd2Sjpk ire_report_item_v4(const mib2_ipRouteEntry_t *rp, boolean_t first,
4096*45916cd2Sjpk     const sec_attr_list_t *attrs)
40977c478bd9Sstevel@tonic-gate {
40987c478bd9Sstevel@tonic-gate 	char			dstbuf[MAXHOSTNAMELEN + 1];
40997c478bd9Sstevel@tonic-gate 	char			maskbuf[MAXHOSTNAMELEN + 1];
41007c478bd9Sstevel@tonic-gate 	char			gwbuf[MAXHOSTNAMELEN + 1];
41017c478bd9Sstevel@tonic-gate 	char			ifname[LIFNAMSIZ + 1];
41027c478bd9Sstevel@tonic-gate 	char			flags[10];	/* RTF_ flags */
41037c478bd9Sstevel@tonic-gate 	uint_t			flag_b;
41047c478bd9Sstevel@tonic-gate 
41057c478bd9Sstevel@tonic-gate 	if (rp->ipRouteInfo.re_in_src_addr != 0 ||
41067c478bd9Sstevel@tonic-gate 	    rp->ipRouteInfo.re_in_ill.o_length != 0 ||
41077c478bd9Sstevel@tonic-gate 	    !(Aflag || (rp->ipRouteInfo.re_ire_type != IRE_CACHE &&
41087c478bd9Sstevel@tonic-gate 	    rp->ipRouteInfo.re_ire_type != IRE_BROADCAST &&
41097c478bd9Sstevel@tonic-gate 	    rp->ipRouteInfo.re_ire_type != IRE_LOCAL))) {
41107c478bd9Sstevel@tonic-gate 		return (first);
41117c478bd9Sstevel@tonic-gate 	}
41127c478bd9Sstevel@tonic-gate 
41137c478bd9Sstevel@tonic-gate 	flag_b = form_v4_route_flags(rp, flags);
41147c478bd9Sstevel@tonic-gate 
41157c478bd9Sstevel@tonic-gate 	if (!ire_filter_match_v4(rp, flag_b))
41167c478bd9Sstevel@tonic-gate 		return (first);
41177c478bd9Sstevel@tonic-gate 
41187c478bd9Sstevel@tonic-gate 	if (first) {
4119*45916cd2Sjpk 		(void) printf(v4compat ? ire_hdr_v4_compat : ire_hdr_v4,
4120*45916cd2Sjpk 		    Vflag ? "IRE" : "Routing");
4121*45916cd2Sjpk 		(void) printf(Vflag ? ire_hdr_v4_verbose : ire_hdr_v4_normal,
4122*45916cd2Sjpk 		    RSECflag ? "  Gateway security attributes  " : "",
4123*45916cd2Sjpk 		    RSECflag ? "-------------------------------" : "");
41247c478bd9Sstevel@tonic-gate 		first = B_FALSE;
41257c478bd9Sstevel@tonic-gate 	}
41267c478bd9Sstevel@tonic-gate 
41277c478bd9Sstevel@tonic-gate 	if (flag_b & FLF_H) {
41287c478bd9Sstevel@tonic-gate 		(void) pr_addr(rp->ipRouteDest, dstbuf, sizeof (dstbuf));
41297c478bd9Sstevel@tonic-gate 	} else {
41307c478bd9Sstevel@tonic-gate 		(void) pr_net(rp->ipRouteDest, rp->ipRouteMask,
41317c478bd9Sstevel@tonic-gate 		    dstbuf, sizeof (dstbuf));
41327c478bd9Sstevel@tonic-gate 	}
41337c478bd9Sstevel@tonic-gate 	if (Vflag) {
41347c478bd9Sstevel@tonic-gate 		(void) printf("%-20s %-15s %-20s %-6s %5u%c %4u %3u "
4135*45916cd2Sjpk 		    "%-4s%6u%6u %s\n",
41367c478bd9Sstevel@tonic-gate 		    dstbuf,
41377c478bd9Sstevel@tonic-gate 		    pr_mask(rp->ipRouteMask, maskbuf, sizeof (maskbuf)),
41387c478bd9Sstevel@tonic-gate 		    pr_addrnz(rp->ipRouteNextHop, gwbuf, sizeof (gwbuf)),
41397c478bd9Sstevel@tonic-gate 		    octetstr(&rp->ipRouteIfIndex, 'a', ifname, sizeof (ifname)),
41407c478bd9Sstevel@tonic-gate 		    rp->ipRouteInfo.re_max_frag,
41417c478bd9Sstevel@tonic-gate 		    rp->ipRouteInfo.re_frag_flag ? '*' : ' ',
41427c478bd9Sstevel@tonic-gate 		    rp->ipRouteInfo.re_rtt,
41437c478bd9Sstevel@tonic-gate 		    rp->ipRouteInfo.re_ref,
41447c478bd9Sstevel@tonic-gate 		    flags,
41457c478bd9Sstevel@tonic-gate 		    rp->ipRouteInfo.re_obpkt,
4146*45916cd2Sjpk 		    rp->ipRouteInfo.re_ibpkt,
4147*45916cd2Sjpk 		    pr_secattr(attrs));
41487c478bd9Sstevel@tonic-gate 	} else {
4149*45916cd2Sjpk 		(void) printf("%-20s %-20s %-5s  %4u%7u %-9s %s\n",
41507c478bd9Sstevel@tonic-gate 		    dstbuf,
41517c478bd9Sstevel@tonic-gate 		    pr_addrnz(rp->ipRouteNextHop, gwbuf, sizeof (gwbuf)),
41527c478bd9Sstevel@tonic-gate 		    flags,
41537c478bd9Sstevel@tonic-gate 		    rp->ipRouteInfo.re_ref,
41547c478bd9Sstevel@tonic-gate 		    rp->ipRouteInfo.re_obpkt + rp->ipRouteInfo.re_ibpkt,
41557c478bd9Sstevel@tonic-gate 		    octetstr(&rp->ipRouteIfIndex, 'a',
4156*45916cd2Sjpk 		    ifname, sizeof (ifname)),
4157*45916cd2Sjpk 		    pr_secattr(attrs));
41587c478bd9Sstevel@tonic-gate 	}
41597c478bd9Sstevel@tonic-gate 	return (first);
41607c478bd9Sstevel@tonic-gate }
41617c478bd9Sstevel@tonic-gate 
4162*45916cd2Sjpk static const char ire_hdr_src_v4[] =
4163*45916cd2Sjpk "\n%s Table: IPv4 Source-Specific\n";
4164*45916cd2Sjpk static const char ire_hdr_src_v4_compat[] =
4165*45916cd2Sjpk "\n%s Table: Source-Specific\n";
4166*45916cd2Sjpk static const char ire_hdr_src_v4_verbose[] =
4167*45916cd2Sjpk "  Destination        In If       Source            Gateway         "
4168*45916cd2Sjpk "  Out If    Mxfrg  Rtt  Ref Flg  Out  In/Fwd %s\n"
4169*45916cd2Sjpk "------------------ ----------- ----------------- ----------------- "
4170*45916cd2Sjpk "----------- ----- ----- --- --- ----- ------ %s\n";
4171*45916cd2Sjpk static const char ire_hdr_src_v4_normal[] =
4172*45916cd2Sjpk "  Destination    In If     Source          Gateway       Flags  Use   "
4173*45916cd2Sjpk " Out If  %s\n"
4174*45916cd2Sjpk "--------------- -------- --------------- --------------- ----- ------ "
4175*45916cd2Sjpk "-------- %s\n";
4176*45916cd2Sjpk 
41777c478bd9Sstevel@tonic-gate /*
41787c478bd9Sstevel@tonic-gate  * Report a source-specific route.
41797c478bd9Sstevel@tonic-gate  */
41807c478bd9Sstevel@tonic-gate static boolean_t
4181*45916cd2Sjpk ire_report_item_v4src(const mib2_ipRouteEntry_t *rp, boolean_t first,
4182*45916cd2Sjpk     const sec_attr_list_t *attrs)
41837c478bd9Sstevel@tonic-gate {
41847c478bd9Sstevel@tonic-gate 	char	dstbuf[MAXHOSTNAMELEN + 1];
41857c478bd9Sstevel@tonic-gate 	char	srcbuf[MAXHOSTNAMELEN + 1];
41867c478bd9Sstevel@tonic-gate 	char	gwbuf[MAXHOSTNAMELEN + 1];
41877c478bd9Sstevel@tonic-gate 	char	inif[LIFNAMSIZ + 1];
41887c478bd9Sstevel@tonic-gate 	char	outif[LIFNAMSIZ + 1];
41897c478bd9Sstevel@tonic-gate 	uint_t	flag_b;
41907c478bd9Sstevel@tonic-gate 	char	flags[10];
41917c478bd9Sstevel@tonic-gate 
41927c478bd9Sstevel@tonic-gate 	/*
41937c478bd9Sstevel@tonic-gate 	 * If this isn't a source specific route, or if it's filtered
41947c478bd9Sstevel@tonic-gate 	 * out, then ignore it.
41957c478bd9Sstevel@tonic-gate 	 */
41967c478bd9Sstevel@tonic-gate 	if ((rp->ipRouteInfo.re_in_src_addr == 0 &&
41977c478bd9Sstevel@tonic-gate 	    rp->ipRouteInfo.re_in_ill.o_length == 0) ||
41987c478bd9Sstevel@tonic-gate 	    !(Aflag || (rp->ipRouteInfo.re_ire_type != IRE_CACHE &&
41997c478bd9Sstevel@tonic-gate 	    rp->ipRouteInfo.re_ire_type != IRE_BROADCAST &&
42007c478bd9Sstevel@tonic-gate 	    rp->ipRouteInfo.re_ire_type != IRE_LOCAL))) {
42017c478bd9Sstevel@tonic-gate 		return (first);
42027c478bd9Sstevel@tonic-gate 	}
42037c478bd9Sstevel@tonic-gate 
42047c478bd9Sstevel@tonic-gate 	flag_b = form_v4_route_flags(rp, flags);
42057c478bd9Sstevel@tonic-gate 
42067c478bd9Sstevel@tonic-gate 	if (!ire_filter_match_v4(rp, flag_b))
42077c478bd9Sstevel@tonic-gate 		return (first);
42087c478bd9Sstevel@tonic-gate 
42097c478bd9Sstevel@tonic-gate 	if (first) {
4210*45916cd2Sjpk 		(void) printf(v4compat ? ire_hdr_src_v4_compat :
4211*45916cd2Sjpk 		    ire_hdr_src_v4, Vflag ? "IRE" : "Routing");
4212*45916cd2Sjpk 		(void) printf(Vflag ? ire_hdr_src_v4_verbose :
4213*45916cd2Sjpk 		    ire_hdr_src_v4_normal,
4214*45916cd2Sjpk 		    RSECflag ? "  Gateway security attributes  " : "",
4215*45916cd2Sjpk 		    RSECflag ? "-------------------------------" : "");
42167c478bd9Sstevel@tonic-gate 		first = B_FALSE;
42177c478bd9Sstevel@tonic-gate 	}
42187c478bd9Sstevel@tonic-gate 
42197c478bd9Sstevel@tonic-gate 	/*
42207c478bd9Sstevel@tonic-gate 	 * This is special-cased here because the kernel doesn't actually
42217c478bd9Sstevel@tonic-gate 	 * pay any attention to the destination address on mrtun entries.
42227c478bd9Sstevel@tonic-gate 	 * Saying "default" would be misleading, though technically correct.
42237c478bd9Sstevel@tonic-gate 	 */
42247c478bd9Sstevel@tonic-gate 	if (rp->ipRouteInfo.re_in_src_addr != 0 && rp->ipRouteDest == 0 &&
42257c478bd9Sstevel@tonic-gate 	    rp->ipRouteMask == 0)
42267c478bd9Sstevel@tonic-gate 		(void) strlcpy(dstbuf, "    --", sizeof (dstbuf));
42277c478bd9Sstevel@tonic-gate 	else
42287c478bd9Sstevel@tonic-gate 		(void) pr_netclassless(rp->ipRouteDest, rp->ipRouteMask,
42297c478bd9Sstevel@tonic-gate 		    dstbuf, sizeof (dstbuf));
42307c478bd9Sstevel@tonic-gate 	(void) octetstr(&rp->ipRouteInfo.re_in_ill, 'a', inif, sizeof (inif));
42317c478bd9Sstevel@tonic-gate 	(void) pr_addrnz(rp->ipRouteInfo.re_in_src_addr, srcbuf,
42327c478bd9Sstevel@tonic-gate 	    sizeof (srcbuf));
42337c478bd9Sstevel@tonic-gate 	(void) octetstr(&rp->ipRouteIfIndex, 'a', outif, sizeof (outif));
42347c478bd9Sstevel@tonic-gate 	(void) pr_addrnz(rp->ipRouteNextHop, gwbuf, sizeof (gwbuf));
42357c478bd9Sstevel@tonic-gate 	if (Vflag) {
42367c478bd9Sstevel@tonic-gate 		(void) printf("%-18s %-11s %-17s %-17s %-11s %4u%c %5u %3u "
4237*45916cd2Sjpk 		    "%-3s %5u %6u %s\n",
42387c478bd9Sstevel@tonic-gate 		    dstbuf, inif, srcbuf, gwbuf,  outif,
42397c478bd9Sstevel@tonic-gate 		    rp->ipRouteInfo.re_max_frag,
42407c478bd9Sstevel@tonic-gate 		    rp->ipRouteInfo.re_frag_flag ? '*' : ' ',
42417c478bd9Sstevel@tonic-gate 		    rp->ipRouteInfo.re_rtt, rp->ipRouteInfo.re_ref, flags,
4242*45916cd2Sjpk 		    rp->ipRouteInfo.re_obpkt, rp->ipRouteInfo.re_ibpkt,
4243*45916cd2Sjpk 		    pr_secattr(attrs));
42447c478bd9Sstevel@tonic-gate 	} else {
4245*45916cd2Sjpk 		(void) printf("%-15s %-8s %-15s %-15s %-5s %6u %-8s %s\n",
4246*45916cd2Sjpk 		    dstbuf, inif, srcbuf, gwbuf, flags,
4247*45916cd2Sjpk 		    rp->ipRouteInfo.re_obpkt + rp->ipRouteInfo.re_ibpkt, outif,
4248*45916cd2Sjpk 		    pr_secattr(attrs));
42497c478bd9Sstevel@tonic-gate 	}
42507c478bd9Sstevel@tonic-gate 	return (first);
42517c478bd9Sstevel@tonic-gate }
42527c478bd9Sstevel@tonic-gate 
42537c478bd9Sstevel@tonic-gate /*
42547c478bd9Sstevel@tonic-gate  * Match a user-supplied IP address list against an IPv6 route entry.
42557c478bd9Sstevel@tonic-gate  * If the user specified "any," then any non-zero address matches.  If
42567c478bd9Sstevel@tonic-gate  * the user specified "none," then only the zero address matches.  If
42577c478bd9Sstevel@tonic-gate  * the user specified a subnet mask length, then use that in matching
42587c478bd9Sstevel@tonic-gate  * routes (select routes that are at least as specific).  If the user
42597c478bd9Sstevel@tonic-gate  * specified only an address, then use the route's mask (select routes
42607c478bd9Sstevel@tonic-gate  * that would match that address).  IPv4 addresses are ignored.
42617c478bd9Sstevel@tonic-gate  */
42627c478bd9Sstevel@tonic-gate static boolean_t
42637c478bd9Sstevel@tonic-gate v6_addr_match(const Ip6Address *addr, int masklen, const filter_t *fp)
42647c478bd9Sstevel@tonic-gate {
42657c478bd9Sstevel@tonic-gate 	const uint8_t *ucp;
42667c478bd9Sstevel@tonic-gate 	int fmasklen;
42677c478bd9Sstevel@tonic-gate 	int i;
42687c478bd9Sstevel@tonic-gate 	char **app;
42697c478bd9Sstevel@tonic-gate 	char *aptr;
42707c478bd9Sstevel@tonic-gate 
42717c478bd9Sstevel@tonic-gate 	if (fp->u.a.f_address == NULL) {
42727c478bd9Sstevel@tonic-gate 		if (IN6_IS_ADDR_UNSPECIFIED(&fp->u.a.f_mask))	/* any */
42737c478bd9Sstevel@tonic-gate 			return (!IN6_IS_ADDR_UNSPECIFIED(addr));
42747c478bd9Sstevel@tonic-gate 		return (IN6_IS_ADDR_UNSPECIFIED(addr));		/* "none" */
42757c478bd9Sstevel@tonic-gate 	}
42767c478bd9Sstevel@tonic-gate 	fmasklen = 0;
42777c478bd9Sstevel@tonic-gate 	/* 'for' loop 1a: */
42787c478bd9Sstevel@tonic-gate 	for (ucp = fp->u.a.f_mask.s6_addr;
42797c478bd9Sstevel@tonic-gate 	    ucp < fp->u.a.f_mask.s6_addr + sizeof (fp->u.a.f_mask.s6_addr);
42807c478bd9Sstevel@tonic-gate 	    ucp++) {
42817c478bd9Sstevel@tonic-gate 		if (*ucp != 0xff) {
42827c478bd9Sstevel@tonic-gate 			if (*ucp != 0)
42837c478bd9Sstevel@tonic-gate 				fmasklen += 9 - ffs(*ucp);
42847c478bd9Sstevel@tonic-gate 			break; /* 'for' loop 1a */
42857c478bd9Sstevel@tonic-gate 		}
42867c478bd9Sstevel@tonic-gate 		fmasklen += 8;
42877c478bd9Sstevel@tonic-gate 	} /* 'for' loop 1a ends */
42887c478bd9Sstevel@tonic-gate 	if (fmasklen != IPV6_ABITS) {
42897c478bd9Sstevel@tonic-gate 		if (fmasklen > masklen)
42907c478bd9Sstevel@tonic-gate 			return (B_FALSE);
42917c478bd9Sstevel@tonic-gate 		masklen = fmasklen;
42927c478bd9Sstevel@tonic-gate 	}
42937c478bd9Sstevel@tonic-gate 	/* 'for' loop 1b: */
42947c478bd9Sstevel@tonic-gate 	for (app = fp->u.a.f_address->h_addr_list; (aptr = *app) != NULL;
42957c478bd9Sstevel@tonic-gate 	    app++) {
42967c478bd9Sstevel@tonic-gate 		/* LINTED: (note 1) */
42977c478bd9Sstevel@tonic-gate 		if (IN6_IS_ADDR_V4MAPPED((in6_addr_t *)aptr))
42987c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1b */
42997c478bd9Sstevel@tonic-gate 		ucp = addr->s6_addr;
43007c478bd9Sstevel@tonic-gate 		for (i = masklen; i >= 8; i -= 8)
43017c478bd9Sstevel@tonic-gate 			if (*ucp++ != *aptr++)
43027c478bd9Sstevel@tonic-gate 				break; /* 'for' loop 1b */
43037c478bd9Sstevel@tonic-gate 		if (i == 0 ||
43047c478bd9Sstevel@tonic-gate 		    (i < 8 && ((*ucp ^ *aptr) & ~(0xff >> i)) == 0))
43057c478bd9Sstevel@tonic-gate 			return (B_TRUE);
43067c478bd9Sstevel@tonic-gate 	} /* 'for' loop 1b ends */
43077c478bd9Sstevel@tonic-gate 	return (B_FALSE);
43087c478bd9Sstevel@tonic-gate }
43097c478bd9Sstevel@tonic-gate 
43107c478bd9Sstevel@tonic-gate /*
43117c478bd9Sstevel@tonic-gate  * Run through the filter list for an IPv6 MIB2 IRE.  For a given
43127c478bd9Sstevel@tonic-gate  * type, if there's at least one filter and all filters of that type
43137c478bd9Sstevel@tonic-gate  * fail to match, then the route doesn't match and isn't displayed.
43147c478bd9Sstevel@tonic-gate  * If at least one matches, or none are specified, for each of the
43157c478bd9Sstevel@tonic-gate  * types, then the route is selected and displayed.
43167c478bd9Sstevel@tonic-gate  */
43177c478bd9Sstevel@tonic-gate static boolean_t
4318*45916cd2Sjpk ire_filter_match_v6(const mib2_ipv6RouteEntry_t *rp6, uint_t flag_b)
43197c478bd9Sstevel@tonic-gate {
43207c478bd9Sstevel@tonic-gate 	filter_t *fp;
43217c478bd9Sstevel@tonic-gate 	int idx;
43227c478bd9Sstevel@tonic-gate 
43237c478bd9Sstevel@tonic-gate 	/* 'for' loop 1: */
43247c478bd9Sstevel@tonic-gate 	for (idx = 0; idx < NFILTERKEYS; idx++)
43257c478bd9Sstevel@tonic-gate 		if ((fp = filters[idx]) != NULL) {
43267c478bd9Sstevel@tonic-gate 			/* 'for' loop 2: */
43277c478bd9Sstevel@tonic-gate 			for (; fp != NULL; fp = fp->f_next) {
43287c478bd9Sstevel@tonic-gate 				switch (idx) {
43297c478bd9Sstevel@tonic-gate 				case FK_AF:
43307c478bd9Sstevel@tonic-gate 					if (fp->u.f_family != AF_INET6)
43317c478bd9Sstevel@tonic-gate 						/* 'for' loop 2 */
43327c478bd9Sstevel@tonic-gate 						continue;
43337c478bd9Sstevel@tonic-gate 					break;
43347c478bd9Sstevel@tonic-gate 				case FK_INIF:
43357c478bd9Sstevel@tonic-gate 					if (fp->u.f_ifname != NULL)
43367c478bd9Sstevel@tonic-gate 						/* 'for' loop 2 */
43377c478bd9Sstevel@tonic-gate 						continue;
43387c478bd9Sstevel@tonic-gate 					break;
43397c478bd9Sstevel@tonic-gate 				case FK_OUTIF:
43407c478bd9Sstevel@tonic-gate 					if (!dev_name_match(&rp6->
43417c478bd9Sstevel@tonic-gate 					    ipv6RouteIfIndex, fp->u.f_ifname))
43427c478bd9Sstevel@tonic-gate 						/* 'for' loop 2 */
43437c478bd9Sstevel@tonic-gate 						continue;
43447c478bd9Sstevel@tonic-gate 					break;
43457c478bd9Sstevel@tonic-gate 				case FK_SRC:
43467c478bd9Sstevel@tonic-gate 					if (!v6_addr_match(&rp6->ipv6RouteInfo.
43477c478bd9Sstevel@tonic-gate 					    re_src_addr, IPV6_ABITS, fp))
43487c478bd9Sstevel@tonic-gate 						/* 'for' loop 2 */
43497c478bd9Sstevel@tonic-gate 						continue;
43507c478bd9Sstevel@tonic-gate 					break;
43517c478bd9Sstevel@tonic-gate 				case FK_DST:
43527c478bd9Sstevel@tonic-gate 					if (!v6_addr_match(&rp6->ipv6RouteDest,
43537c478bd9Sstevel@tonic-gate 					    rp6->ipv6RoutePfxLength, fp))
43547c478bd9Sstevel@tonic-gate 						/* 'for' loop 2 */
43557c478bd9Sstevel@tonic-gate 						continue;
43567c478bd9Sstevel@tonic-gate 					break;
43577c478bd9Sstevel@tonic-gate 				case FK_FLAGS:
43587c478bd9Sstevel@tonic-gate 					if ((flag_b & fp->u.f.f_flagset) !=
43597c478bd9Sstevel@tonic-gate 					    fp->u.f.f_flagset ||
43607c478bd9Sstevel@tonic-gate 					    (flag_b & fp->u.f.f_flagclear))
43617c478bd9Sstevel@tonic-gate 						/* 'for' loop 2 */
43627c478bd9Sstevel@tonic-gate 						continue;
43637c478bd9Sstevel@tonic-gate 					break;
43647c478bd9Sstevel@tonic-gate 				}
43657c478bd9Sstevel@tonic-gate 				break;
43667c478bd9Sstevel@tonic-gate 			} /* 'for' loop 2 ends */
43677c478bd9Sstevel@tonic-gate 			if (fp == NULL)
43687c478bd9Sstevel@tonic-gate 				return (B_FALSE);
43697c478bd9Sstevel@tonic-gate 		}
43707c478bd9Sstevel@tonic-gate 	/* 'for' loop 1 ends */
43717c478bd9Sstevel@tonic-gate 	return (B_TRUE);
43727c478bd9Sstevel@tonic-gate }
43737c478bd9Sstevel@tonic-gate 
4374*45916cd2Sjpk static const char ire_hdr_v6[] =
4375*45916cd2Sjpk "\n%s Table: IPv6\n";
4376*45916cd2Sjpk static const char ire_hdr_v6_verbose[] =
4377*45916cd2Sjpk "  Destination/Mask            Gateway                    If    PMTU   Rtt  "
4378*45916cd2Sjpk "Ref Flags  Out   In/Fwd %s\n"
4379*45916cd2Sjpk "--------------------------- --------------------------- ----- ------ ----- "
4380*45916cd2Sjpk "--- ----- ------ ------ %s\n";
4381*45916cd2Sjpk static const char ire_hdr_v6_normal[] =
4382*45916cd2Sjpk "  Destination/Mask            Gateway                   Flags Ref   Use  "
4383*45916cd2Sjpk " If   %s\n"
4384*45916cd2Sjpk "--------------------------- --------------------------- ----- --- ------ "
4385*45916cd2Sjpk "----- %s\n";
4386*45916cd2Sjpk 
43877c478bd9Sstevel@tonic-gate static boolean_t
4388*45916cd2Sjpk ire_report_item_v6(const mib2_ipv6RouteEntry_t *rp6, boolean_t first,
4389*45916cd2Sjpk     const sec_attr_list_t *attrs)
43907c478bd9Sstevel@tonic-gate {
43917c478bd9Sstevel@tonic-gate 	char			dstbuf[MAXHOSTNAMELEN + 1];
43927c478bd9Sstevel@tonic-gate 	char			gwbuf[MAXHOSTNAMELEN + 1];
43937c478bd9Sstevel@tonic-gate 	char			ifname[LIFNAMSIZ + 1];
43947c478bd9Sstevel@tonic-gate 	char			flags[10];	/* RTF_ flags */
43957c478bd9Sstevel@tonic-gate 	uint_t			flag_b;
43967c478bd9Sstevel@tonic-gate 
43977c478bd9Sstevel@tonic-gate 	if (!(Aflag || (rp6->ipv6RouteInfo.re_ire_type != IRE_CACHE &&
43987c478bd9Sstevel@tonic-gate 	    rp6->ipv6RouteInfo.re_ire_type != IRE_LOCAL))) {
43997c478bd9Sstevel@tonic-gate 		return (first);
44007c478bd9Sstevel@tonic-gate 	}
44017c478bd9Sstevel@tonic-gate 
44027c478bd9Sstevel@tonic-gate 	flag_b = FLF_U;
44037c478bd9Sstevel@tonic-gate 	(void) strcpy(flags, "U");
44047c478bd9Sstevel@tonic-gate 	if (rp6->ipv6RouteInfo.re_ire_type == IRE_DEFAULT ||
44057c478bd9Sstevel@tonic-gate 	    rp6->ipv6RouteInfo.re_ire_type == IRE_PREFIX ||
44067c478bd9Sstevel@tonic-gate 	    rp6->ipv6RouteInfo.re_ire_type == IRE_HOST ||
44077c478bd9Sstevel@tonic-gate 	    rp6->ipv6RouteInfo.re_ire_type == IRE_HOST_REDIRECT) {
44087c478bd9Sstevel@tonic-gate 		(void) strcat(flags, "G");
44097c478bd9Sstevel@tonic-gate 		flag_b |= FLF_G;
44107c478bd9Sstevel@tonic-gate 	}
44117c478bd9Sstevel@tonic-gate 
44127c478bd9Sstevel@tonic-gate 	if (rp6->ipv6RoutePfxLength == IPV6_ABITS) {
44137c478bd9Sstevel@tonic-gate 		(void) strcat(flags, "H");
44147c478bd9Sstevel@tonic-gate 		flag_b |= FLF_H;
44157c478bd9Sstevel@tonic-gate 	}
44167c478bd9Sstevel@tonic-gate 
44177c478bd9Sstevel@tonic-gate 	if (rp6->ipv6RouteInfo.re_ire_type == IRE_HOST_REDIRECT) {
44187c478bd9Sstevel@tonic-gate 		(void) strcat(flags, "D");
44197c478bd9Sstevel@tonic-gate 		flag_b |= FLF_D;
44207c478bd9Sstevel@tonic-gate 	}
44217c478bd9Sstevel@tonic-gate 	if (rp6->ipv6RouteInfo.re_ire_type == IRE_CACHE) {
44227c478bd9Sstevel@tonic-gate 		/* Address resolution */
44237c478bd9Sstevel@tonic-gate 		(void) strcat(flags, "A");
44247c478bd9Sstevel@tonic-gate 		flag_b |= FLF_A;
44257c478bd9Sstevel@tonic-gate 	}
44267c478bd9Sstevel@tonic-gate 	if (rp6->ipv6RouteInfo.re_ire_type == IRE_LOCAL) {	/* Local */
44277c478bd9Sstevel@tonic-gate 		(void) strcat(flags, "L");
44287c478bd9Sstevel@tonic-gate 		flag_b |= FLF_L;
44297c478bd9Sstevel@tonic-gate 	}
44307c478bd9Sstevel@tonic-gate 	if (rp6->ipv6RouteInfo.re_flags & RTF_MULTIRT) {
44317c478bd9Sstevel@tonic-gate 		(void) strcat(flags, "M");			/* Multiroute */
44327c478bd9Sstevel@tonic-gate 		flag_b |= FLF_M;
44337c478bd9Sstevel@tonic-gate 	}
44347c478bd9Sstevel@tonic-gate 	if (rp6->ipv6RouteInfo.re_flags & RTF_SETSRC) {
44357c478bd9Sstevel@tonic-gate 		(void) strcat(flags, "S");			/* Setsrc */
44367c478bd9Sstevel@tonic-gate 		flag_b |= FLF_S;
44377c478bd9Sstevel@tonic-gate 	}
44387c478bd9Sstevel@tonic-gate 
44397c478bd9Sstevel@tonic-gate 	if (!ire_filter_match_v6(rp6, flag_b))
44407c478bd9Sstevel@tonic-gate 		return (first);
44417c478bd9Sstevel@tonic-gate 
44427c478bd9Sstevel@tonic-gate 	if (first) {
4443*45916cd2Sjpk 		(void) printf(ire_hdr_v6, Vflag ? "IRE" : "Routing");
4444*45916cd2Sjpk 		(void) printf(Vflag ? ire_hdr_v6_verbose : ire_hdr_v6_normal,
4445*45916cd2Sjpk 		    RSECflag ? "  Gateway security attributes  " : "",
4446*45916cd2Sjpk 		    RSECflag ? "-------------------------------" : "");
44477c478bd9Sstevel@tonic-gate 		first = B_FALSE;
44487c478bd9Sstevel@tonic-gate 	}
44497c478bd9Sstevel@tonic-gate 
44507c478bd9Sstevel@tonic-gate 	if (Vflag) {
44517c478bd9Sstevel@tonic-gate 		(void) printf("%-27s %-27s %-5s %5u%c %5u %3u "
4452*45916cd2Sjpk 		    "%-5s %6u %6u %s\n",
44537c478bd9Sstevel@tonic-gate 		    pr_prefix6(&rp6->ipv6RouteDest,
44547c478bd9Sstevel@tonic-gate 			rp6->ipv6RoutePfxLength, dstbuf, sizeof (dstbuf)),
44557c478bd9Sstevel@tonic-gate 		    IN6_IS_ADDR_UNSPECIFIED(&rp6->ipv6RouteNextHop) ?
44567c478bd9Sstevel@tonic-gate 		    "    --" :
44577c478bd9Sstevel@tonic-gate 		    pr_addr6(&rp6->ipv6RouteNextHop, gwbuf, sizeof (gwbuf)),
44587c478bd9Sstevel@tonic-gate 		    octetstr(&rp6->ipv6RouteIfIndex, 'a',
44597c478bd9Sstevel@tonic-gate 		    ifname, sizeof (ifname)),
44607c478bd9Sstevel@tonic-gate 		    rp6->ipv6RouteInfo.re_max_frag,
44617c478bd9Sstevel@tonic-gate 		    rp6->ipv6RouteInfo.re_frag_flag ? '*' : ' ',
44627c478bd9Sstevel@tonic-gate 		    rp6->ipv6RouteInfo.re_rtt,
44637c478bd9Sstevel@tonic-gate 		    rp6->ipv6RouteInfo.re_ref,
44647c478bd9Sstevel@tonic-gate 		    flags,
44657c478bd9Sstevel@tonic-gate 		    rp6->ipv6RouteInfo.re_obpkt,
4466*45916cd2Sjpk 		    rp6->ipv6RouteInfo.re_ibpkt,
4467*45916cd2Sjpk 		    pr_secattr(attrs));
44687c478bd9Sstevel@tonic-gate 	} else {
4469*45916cd2Sjpk 		(void) printf("%-27s %-27s %-5s %3u %6u %-5s %s\n",
44707c478bd9Sstevel@tonic-gate 		    pr_prefix6(&rp6->ipv6RouteDest,
44717c478bd9Sstevel@tonic-gate 			rp6->ipv6RoutePfxLength, dstbuf, sizeof (dstbuf)),
44727c478bd9Sstevel@tonic-gate 		    IN6_IS_ADDR_UNSPECIFIED(&rp6->ipv6RouteNextHop) ?
44737c478bd9Sstevel@tonic-gate 		    "    --" :
44747c478bd9Sstevel@tonic-gate 		    pr_addr6(&rp6->ipv6RouteNextHop, gwbuf, sizeof (gwbuf)),
44757c478bd9Sstevel@tonic-gate 		    flags,
44767c478bd9Sstevel@tonic-gate 		    rp6->ipv6RouteInfo.re_ref,
44777c478bd9Sstevel@tonic-gate 		    rp6->ipv6RouteInfo.re_obpkt + rp6->ipv6RouteInfo.re_ibpkt,
44787c478bd9Sstevel@tonic-gate 		    octetstr(&rp6->ipv6RouteIfIndex, 'a',
4479*45916cd2Sjpk 		    ifname, sizeof (ifname)),
4480*45916cd2Sjpk 		    pr_secattr(attrs));
44817c478bd9Sstevel@tonic-gate 	}
44827c478bd9Sstevel@tonic-gate 	return (first);
44837c478bd9Sstevel@tonic-gate }
44847c478bd9Sstevel@tonic-gate 
4485*45916cd2Sjpk /*
4486*45916cd2Sjpk  * Common attribute-gathering routine for all transports.
4487*45916cd2Sjpk  */
4488*45916cd2Sjpk static mib2_transportMLPEntry_t **
4489*45916cd2Sjpk gather_attrs(const mib_item_t *item, int group, int mib_id, int esize)
4490*45916cd2Sjpk {
4491*45916cd2Sjpk 	int transport_count = 0;
4492*45916cd2Sjpk 	const mib_item_t *iptr;
4493*45916cd2Sjpk 	mib2_transportMLPEntry_t **attrs, *tme;
4494*45916cd2Sjpk 
4495*45916cd2Sjpk 	for (iptr = item; iptr != NULL; iptr = iptr->next_item) {
4496*45916cd2Sjpk 		if (iptr->group == group && iptr->mib_id == mib_id)
4497*45916cd2Sjpk 			transport_count += iptr->length / esize;
4498*45916cd2Sjpk 	}
4499*45916cd2Sjpk 	if (transport_count <= 0)
4500*45916cd2Sjpk 		return (NULL);
4501*45916cd2Sjpk 	attrs = calloc(transport_count, sizeof (*attrs));
4502*45916cd2Sjpk 	if (attrs == NULL) {
4503*45916cd2Sjpk 		perror("gather_attrs calloc failed");
4504*45916cd2Sjpk 		return (NULL);
4505*45916cd2Sjpk 	}
4506*45916cd2Sjpk 	for (iptr = item; iptr != NULL; iptr = iptr->next_item) {
4507*45916cd2Sjpk 		if (iptr->group == group && iptr->mib_id == EXPER_XPORT_MLP) {
4508*45916cd2Sjpk 			for (tme = iptr->valp;
4509*45916cd2Sjpk 			    (char *)tme < (char *)iptr->valp + iptr->length;
4510*45916cd2Sjpk 			    /* LINTED: (note 1) */
4511*45916cd2Sjpk 			    tme = (mib2_transportMLPEntry_t *)((char *)tme +
4512*45916cd2Sjpk 			    transportMLPSize)) {
4513*45916cd2Sjpk 				attrs[tme->tme_connidx] = tme;
4514*45916cd2Sjpk 			}
4515*45916cd2Sjpk 		}
4516*45916cd2Sjpk 	}
4517*45916cd2Sjpk 	return (attrs);
4518*45916cd2Sjpk }
4519*45916cd2Sjpk 
4520*45916cd2Sjpk static void
4521*45916cd2Sjpk print_transport_label(const mib2_transportMLPEntry_t *attr)
4522*45916cd2Sjpk {
4523*45916cd2Sjpk 	if (!RSECflag || attr == NULL)
4524*45916cd2Sjpk 		return;
4525*45916cd2Sjpk 
4526*45916cd2Sjpk 	if (bisinvalid(&attr->tme_label))
4527*45916cd2Sjpk 		(void) printf("   INVALID\n");
4528*45916cd2Sjpk 	else
4529*45916cd2Sjpk 		(void) printf("   %s\n", sl_to_str(&attr->tme_label));
4530*45916cd2Sjpk }
4531*45916cd2Sjpk 
45327c478bd9Sstevel@tonic-gate /* ------------------------------ TCP_REPORT------------------------------- */
45337c478bd9Sstevel@tonic-gate 
45347c478bd9Sstevel@tonic-gate static const char tcp_hdr_v4[] =
45357c478bd9Sstevel@tonic-gate "\nTCP: IPv4\n";
45367c478bd9Sstevel@tonic-gate static const char tcp_hdr_v4_compat[] =
45377c478bd9Sstevel@tonic-gate "\nTCP\n";
45387c478bd9Sstevel@tonic-gate static const char tcp_hdr_v4_verbose[] =
45397c478bd9Sstevel@tonic-gate "Local/Remote Address Swind  Snext     Suna   Rwind  Rnext     Rack   "
4540*45916cd2Sjpk " Rto   Mss     State\n"
45417c478bd9Sstevel@tonic-gate "-------------------- ----- -------- -------- ----- -------- -------- "
4542*45916cd2Sjpk "----- ----- -----------\n";
45437c478bd9Sstevel@tonic-gate static const char tcp_hdr_v4_normal[] =
4544*45916cd2Sjpk "   Local Address        Remote Address    Swind Send-Q Rwind Recv-Q "
4545*45916cd2Sjpk "   State\n"
4546*45916cd2Sjpk "-------------------- -------------------- ----- ------ ----- ------ "
4547*45916cd2Sjpk "-----------\n";
45487c478bd9Sstevel@tonic-gate 
45497c478bd9Sstevel@tonic-gate static const char tcp_hdr_v6[] =
45507c478bd9Sstevel@tonic-gate "\nTCP: IPv6\n";
45517c478bd9Sstevel@tonic-gate static const char tcp_hdr_v6_verbose[] =
45527c478bd9Sstevel@tonic-gate "Local/Remote Address              Swind  Snext     Suna   Rwind  Rnext   "
4553*45916cd2Sjpk "  Rack    Rto   Mss    State      If\n"
45547c478bd9Sstevel@tonic-gate "--------------------------------- ----- -------- -------- ----- -------- "
45557c478bd9Sstevel@tonic-gate "-------- ----- ----- ----------- -----\n";
45567c478bd9Sstevel@tonic-gate static const char tcp_hdr_v6_normal[] =
45577c478bd9Sstevel@tonic-gate "   Local Address                     Remote Address                 "
4558*45916cd2Sjpk "Swind Send-Q Rwind Recv-Q   State      If\n"
45597c478bd9Sstevel@tonic-gate "--------------------------------- --------------------------------- "
45607c478bd9Sstevel@tonic-gate "----- ------ ----- ------ ----------- -----\n";
45617c478bd9Sstevel@tonic-gate 
4562*45916cd2Sjpk static boolean_t tcp_report_item_v4(const mib2_tcpConnEntry_t *,
4563*45916cd2Sjpk     boolean_t first, const mib2_transportMLPEntry_t *);
4564*45916cd2Sjpk static boolean_t tcp_report_item_v6(const mib2_tcp6ConnEntry_t *,
4565*45916cd2Sjpk     boolean_t first, const mib2_transportMLPEntry_t *);
45667c478bd9Sstevel@tonic-gate 
45677c478bd9Sstevel@tonic-gate static void
4568*45916cd2Sjpk tcp_report(const mib_item_t *item)
45697c478bd9Sstevel@tonic-gate {
45707c478bd9Sstevel@tonic-gate 	int			jtemp = 0;
45717c478bd9Sstevel@tonic-gate 	boolean_t		print_hdr_once_v4 = B_TRUE;
45727c478bd9Sstevel@tonic-gate 	boolean_t		print_hdr_once_v6 = B_TRUE;
45737c478bd9Sstevel@tonic-gate 	mib2_tcpConnEntry_t	*tp;
45747c478bd9Sstevel@tonic-gate 	mib2_tcp6ConnEntry_t	*tp6;
4575*45916cd2Sjpk 	mib2_transportMLPEntry_t **v4_attrs, **v6_attrs;
4576*45916cd2Sjpk 	mib2_transportMLPEntry_t **v4a, **v6a;
4577*45916cd2Sjpk 	mib2_transportMLPEntry_t *aptr;
45787c478bd9Sstevel@tonic-gate 
45797c478bd9Sstevel@tonic-gate 	if (!protocol_selected(IPPROTO_TCP))
45807c478bd9Sstevel@tonic-gate 		return;
45817c478bd9Sstevel@tonic-gate 
4582*45916cd2Sjpk 	/*
4583*45916cd2Sjpk 	 * Preparation pass: the kernel returns separate entries for TCP
4584*45916cd2Sjpk 	 * connection table entries and Multilevel Port attributes.  We loop
4585*45916cd2Sjpk 	 * through the attributes first and set up an array for each address
4586*45916cd2Sjpk 	 * family.
4587*45916cd2Sjpk 	 */
4588*45916cd2Sjpk 	v4_attrs = family_selected(AF_INET) && RSECflag ?
4589*45916cd2Sjpk 	    gather_attrs(item, MIB2_TCP, MIB2_TCP_CONN, tcpConnEntrySize) :
4590*45916cd2Sjpk 	    NULL;
4591*45916cd2Sjpk 	v6_attrs = family_selected(AF_INET6) && RSECflag ?
4592*45916cd2Sjpk 	    gather_attrs(item, MIB2_TCP6, MIB2_TCP6_CONN, tcp6ConnEntrySize) :
4593*45916cd2Sjpk 	    NULL;
4594*45916cd2Sjpk 
45957c478bd9Sstevel@tonic-gate 	/* 'for' loop 1: */
4596*45916cd2Sjpk 	v4a = v4_attrs;
4597*45916cd2Sjpk 	v6a = v6_attrs;
4598*45916cd2Sjpk 	for (; item != NULL; item = item->next_item) {
45997c478bd9Sstevel@tonic-gate 		if (Dflag) {
46007c478bd9Sstevel@tonic-gate 			(void) printf("\n--- Entry %d ---\n", ++jtemp);
46017c478bd9Sstevel@tonic-gate 			(void) printf("Group = %d, mib_id = %d, "
46027c478bd9Sstevel@tonic-gate 			    "length = %d, valp = 0x%p\n",
46037c478bd9Sstevel@tonic-gate 			    item->group, item->mib_id,
46047c478bd9Sstevel@tonic-gate 			    item->length, item->valp);
46057c478bd9Sstevel@tonic-gate 		}
46067c478bd9Sstevel@tonic-gate 
46077c478bd9Sstevel@tonic-gate 		if (!((item->group == MIB2_TCP &&
46087c478bd9Sstevel@tonic-gate 		    item->mib_id == MIB2_TCP_CONN) ||
46097c478bd9Sstevel@tonic-gate 		    (item->group == MIB2_TCP6 &&
46107c478bd9Sstevel@tonic-gate 		    item->mib_id == MIB2_TCP6_CONN)))
46117c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
46127c478bd9Sstevel@tonic-gate 
46137c478bd9Sstevel@tonic-gate 		if (item->group == MIB2_TCP && !family_selected(AF_INET))
46147c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
46157c478bd9Sstevel@tonic-gate 		else if (item->group == MIB2_TCP6 && !family_selected(AF_INET6))
46167c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
46177c478bd9Sstevel@tonic-gate 
46187c478bd9Sstevel@tonic-gate 		if (item->group == MIB2_TCP) {
46197c478bd9Sstevel@tonic-gate 			for (tp = (mib2_tcpConnEntry_t *)item->valp;
46207c478bd9Sstevel@tonic-gate 			    (char *)tp < (char *)item->valp + item->length;
46217c478bd9Sstevel@tonic-gate 			    /* LINTED: (note 1) */
46227c478bd9Sstevel@tonic-gate 			    tp = (mib2_tcpConnEntry_t *)((char *)tp +
46237c478bd9Sstevel@tonic-gate 			    tcpConnEntrySize)) {
4624*45916cd2Sjpk 				aptr = v4a == NULL ? NULL : *v4a++;
46257c478bd9Sstevel@tonic-gate 				print_hdr_once_v4 = tcp_report_item_v4(tp,
4626*45916cd2Sjpk 				    print_hdr_once_v4, aptr);
46277c478bd9Sstevel@tonic-gate 			}
46287c478bd9Sstevel@tonic-gate 		} else {
46297c478bd9Sstevel@tonic-gate 			for (tp6 = (mib2_tcp6ConnEntry_t *)item->valp;
46307c478bd9Sstevel@tonic-gate 			    (char *)tp6 < (char *)item->valp + item->length;
46317c478bd9Sstevel@tonic-gate 			    /* LINTED: (note 1) */
46327c478bd9Sstevel@tonic-gate 			    tp6 = (mib2_tcp6ConnEntry_t *)((char *)tp6 +
46337c478bd9Sstevel@tonic-gate 			    tcp6ConnEntrySize)) {
4634*45916cd2Sjpk 				aptr = v6a == NULL ? NULL : *v6a++;
46357c478bd9Sstevel@tonic-gate 				print_hdr_once_v6 = tcp_report_item_v6(tp6,
4636*45916cd2Sjpk 				    print_hdr_once_v6, aptr);
46377c478bd9Sstevel@tonic-gate 			}
46387c478bd9Sstevel@tonic-gate 		}
46397c478bd9Sstevel@tonic-gate 	} /* 'for' loop 1 ends */
46407c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
4641*45916cd2Sjpk 
4642*45916cd2Sjpk 	if (v4_attrs != NULL)
4643*45916cd2Sjpk 		free(v4_attrs);
4644*45916cd2Sjpk 	if (v6_attrs != NULL)
4645*45916cd2Sjpk 		free(v6_attrs);
46467c478bd9Sstevel@tonic-gate }
46477c478bd9Sstevel@tonic-gate 
46487c478bd9Sstevel@tonic-gate static boolean_t
4649*45916cd2Sjpk tcp_report_item_v4(const mib2_tcpConnEntry_t *tp, boolean_t first,
4650*45916cd2Sjpk     const mib2_transportMLPEntry_t *attr)
46517c478bd9Sstevel@tonic-gate {
46527c478bd9Sstevel@tonic-gate 	/*
46537c478bd9Sstevel@tonic-gate 	 * lname and fname below are for the hostname as well as the portname
46547c478bd9Sstevel@tonic-gate 	 * There is no limit on portname length so we assume MAXHOSTNAMELEN
46557c478bd9Sstevel@tonic-gate 	 * as the limit
46567c478bd9Sstevel@tonic-gate 	 */
46577c478bd9Sstevel@tonic-gate 	char	lname[MAXHOSTNAMELEN + MAXHOSTNAMELEN + 1];
46587c478bd9Sstevel@tonic-gate 	char	fname[MAXHOSTNAMELEN + MAXHOSTNAMELEN + 1];
46597c478bd9Sstevel@tonic-gate 
46607c478bd9Sstevel@tonic-gate 	if (!(Aflag || tp->tcpConnEntryInfo.ce_state >= TCPS_ESTABLISHED))
46617c478bd9Sstevel@tonic-gate 		return (first); /* Nothing to print */
46627c478bd9Sstevel@tonic-gate 
46637c478bd9Sstevel@tonic-gate 	if (first) {
4664*45916cd2Sjpk 		(void) printf(v4compat ? tcp_hdr_v4_compat : tcp_hdr_v4);
4665*45916cd2Sjpk 		(void) printf(Vflag ? tcp_hdr_v4_verbose : tcp_hdr_v4_normal);
46667c478bd9Sstevel@tonic-gate 	}
46677c478bd9Sstevel@tonic-gate 
46687c478bd9Sstevel@tonic-gate 	if (Vflag) {
46697c478bd9Sstevel@tonic-gate 		(void) printf("%-20s\n%-20s %5u %08x %08x %5u %08x %08x "
46707c478bd9Sstevel@tonic-gate 		    "%5u %5u %s\n",
46717c478bd9Sstevel@tonic-gate 		    pr_ap(tp->tcpConnLocalAddress,
46727c478bd9Sstevel@tonic-gate 			tp->tcpConnLocalPort, "tcp", lname, sizeof (lname)),
46737c478bd9Sstevel@tonic-gate 		    pr_ap(tp->tcpConnRemAddress,
46747c478bd9Sstevel@tonic-gate 			tp->tcpConnRemPort, "tcp", fname, sizeof (fname)),
46757c478bd9Sstevel@tonic-gate 		    tp->tcpConnEntryInfo.ce_swnd,
46767c478bd9Sstevel@tonic-gate 		    tp->tcpConnEntryInfo.ce_snxt,
46777c478bd9Sstevel@tonic-gate 		    tp->tcpConnEntryInfo.ce_suna,
46787c478bd9Sstevel@tonic-gate 		    tp->tcpConnEntryInfo.ce_rwnd,
46797c478bd9Sstevel@tonic-gate 		    tp->tcpConnEntryInfo.ce_rnxt,
46807c478bd9Sstevel@tonic-gate 		    tp->tcpConnEntryInfo.ce_rack,
46817c478bd9Sstevel@tonic-gate 		    tp->tcpConnEntryInfo.ce_rto,
46827c478bd9Sstevel@tonic-gate 		    tp->tcpConnEntryInfo.ce_mss,
4683*45916cd2Sjpk 		    mitcp_state(tp->tcpConnEntryInfo.ce_state, attr));
46847c478bd9Sstevel@tonic-gate 	} else {
46857c478bd9Sstevel@tonic-gate 		int sq = (int)tp->tcpConnEntryInfo.ce_snxt -
46867c478bd9Sstevel@tonic-gate 		    (int)tp->tcpConnEntryInfo.ce_suna - 1;
46877c478bd9Sstevel@tonic-gate 		int rq = (int)tp->tcpConnEntryInfo.ce_rnxt -
46887c478bd9Sstevel@tonic-gate 		    (int)tp->tcpConnEntryInfo.ce_rack;
46897c478bd9Sstevel@tonic-gate 
46907c478bd9Sstevel@tonic-gate 		(void) printf("%-20s %-20s %5u %6d %5u %6d %s\n",
46917c478bd9Sstevel@tonic-gate 		    pr_ap(tp->tcpConnLocalAddress,
46927c478bd9Sstevel@tonic-gate 			tp->tcpConnLocalPort, "tcp", lname, sizeof (lname)),
46937c478bd9Sstevel@tonic-gate 		    pr_ap(tp->tcpConnRemAddress,
46947c478bd9Sstevel@tonic-gate 			tp->tcpConnRemPort, "tcp", fname, sizeof (fname)),
46957c478bd9Sstevel@tonic-gate 		    tp->tcpConnEntryInfo.ce_swnd,
46967c478bd9Sstevel@tonic-gate 		    (sq >= 0) ? sq : 0,
46977c478bd9Sstevel@tonic-gate 		    tp->tcpConnEntryInfo.ce_rwnd,
46987c478bd9Sstevel@tonic-gate 		    (rq >= 0) ? rq : 0,
4699*45916cd2Sjpk 		    mitcp_state(tp->tcpConnEntryInfo.ce_state, attr));
47007c478bd9Sstevel@tonic-gate 	}
4701*45916cd2Sjpk 
4702*45916cd2Sjpk 	print_transport_label(attr);
4703*45916cd2Sjpk 
4704*45916cd2Sjpk 	return (B_FALSE);
47057c478bd9Sstevel@tonic-gate }
47067c478bd9Sstevel@tonic-gate 
47077c478bd9Sstevel@tonic-gate static boolean_t
4708*45916cd2Sjpk tcp_report_item_v6(const mib2_tcp6ConnEntry_t *tp6, boolean_t first,
4709*45916cd2Sjpk     const mib2_transportMLPEntry_t *attr)
47107c478bd9Sstevel@tonic-gate {
47117c478bd9Sstevel@tonic-gate 	/*
47127c478bd9Sstevel@tonic-gate 	 * lname and fname below are for the hostname as well as the portname
47137c478bd9Sstevel@tonic-gate 	 * There is no limit on portname length so we assume MAXHOSTNAMELEN
47147c478bd9Sstevel@tonic-gate 	 * as the limit
47157c478bd9Sstevel@tonic-gate 	 */
47167c478bd9Sstevel@tonic-gate 	char	lname[MAXHOSTNAMELEN + MAXHOSTNAMELEN + 1];
47177c478bd9Sstevel@tonic-gate 	char	fname[MAXHOSTNAMELEN + MAXHOSTNAMELEN + 1];
47187c478bd9Sstevel@tonic-gate 	char	ifname[LIFNAMSIZ + 1];
47197c478bd9Sstevel@tonic-gate 	char	*ifnamep;
47207c478bd9Sstevel@tonic-gate 
47217c478bd9Sstevel@tonic-gate 	if (!(Aflag || tp6->tcp6ConnEntryInfo.ce_state >= TCPS_ESTABLISHED))
47227c478bd9Sstevel@tonic-gate 		return (first); /* Nothing to print */
47237c478bd9Sstevel@tonic-gate 
47247c478bd9Sstevel@tonic-gate 	if (first) {
4725*45916cd2Sjpk 		(void) printf(tcp_hdr_v6);
4726*45916cd2Sjpk 		(void) printf(Vflag ? tcp_hdr_v6_verbose : tcp_hdr_v6_normal);
47277c478bd9Sstevel@tonic-gate 	}
47287c478bd9Sstevel@tonic-gate 
47297c478bd9Sstevel@tonic-gate 	ifnamep = (tp6->tcp6ConnIfIndex != 0) ?
47307c478bd9Sstevel@tonic-gate 	    if_indextoname(tp6->tcp6ConnIfIndex, ifname) : NULL;
4731*45916cd2Sjpk 	if (ifnamep == NULL)
4732*45916cd2Sjpk 		ifnamep = "";
47337c478bd9Sstevel@tonic-gate 
47347c478bd9Sstevel@tonic-gate 	if (Vflag) {
47357c478bd9Sstevel@tonic-gate 		(void) printf("%-33s\n%-33s %5u %08x %08x %5u %08x %08x "
4736*45916cd2Sjpk 		    "%5u %5u %-11s %s\n",
47377c478bd9Sstevel@tonic-gate 		    pr_ap6(&tp6->tcp6ConnLocalAddress,
47387c478bd9Sstevel@tonic-gate 			tp6->tcp6ConnLocalPort, "tcp", lname, sizeof (lname)),
47397c478bd9Sstevel@tonic-gate 		    pr_ap6(&tp6->tcp6ConnRemAddress,
47407c478bd9Sstevel@tonic-gate 			tp6->tcp6ConnRemPort, "tcp", fname, sizeof (fname)),
47417c478bd9Sstevel@tonic-gate 		    tp6->tcp6ConnEntryInfo.ce_swnd,
47427c478bd9Sstevel@tonic-gate 		    tp6->tcp6ConnEntryInfo.ce_snxt,
47437c478bd9Sstevel@tonic-gate 		    tp6->tcp6ConnEntryInfo.ce_suna,
47447c478bd9Sstevel@tonic-gate 		    tp6->tcp6ConnEntryInfo.ce_rwnd,
47457c478bd9Sstevel@tonic-gate 		    tp6->tcp6ConnEntryInfo.ce_rnxt,
47467c478bd9Sstevel@tonic-gate 		    tp6->tcp6ConnEntryInfo.ce_rack,
47477c478bd9Sstevel@tonic-gate 		    tp6->tcp6ConnEntryInfo.ce_rto,
47487c478bd9Sstevel@tonic-gate 		    tp6->tcp6ConnEntryInfo.ce_mss,
4749*45916cd2Sjpk 		    mitcp_state(tp6->tcp6ConnEntryInfo.ce_state, attr),
4750*45916cd2Sjpk 		    ifnamep);
47517c478bd9Sstevel@tonic-gate 	} else {
47527c478bd9Sstevel@tonic-gate 		int sq = (int)tp6->tcp6ConnEntryInfo.ce_snxt -
47537c478bd9Sstevel@tonic-gate 		    (int)tp6->tcp6ConnEntryInfo.ce_suna - 1;
47547c478bd9Sstevel@tonic-gate 		int rq = (int)tp6->tcp6ConnEntryInfo.ce_rnxt -
47557c478bd9Sstevel@tonic-gate 		    (int)tp6->tcp6ConnEntryInfo.ce_rack;
47567c478bd9Sstevel@tonic-gate 
4757*45916cd2Sjpk 		(void) printf("%-33s %-33s %5u %6d %5u %6d %-11s %s\n",
47587c478bd9Sstevel@tonic-gate 		    pr_ap6(&tp6->tcp6ConnLocalAddress,
47597c478bd9Sstevel@tonic-gate 			tp6->tcp6ConnLocalPort, "tcp", lname, sizeof (lname)),
47607c478bd9Sstevel@tonic-gate 		    pr_ap6(&tp6->tcp6ConnRemAddress,
47617c478bd9Sstevel@tonic-gate 			tp6->tcp6ConnRemPort, "tcp", fname, sizeof (fname)),
47627c478bd9Sstevel@tonic-gate 		    tp6->tcp6ConnEntryInfo.ce_swnd,
47637c478bd9Sstevel@tonic-gate 		    (sq >= 0) ? sq : 0,
47647c478bd9Sstevel@tonic-gate 		    tp6->tcp6ConnEntryInfo.ce_rwnd,
47657c478bd9Sstevel@tonic-gate 		    (rq >= 0) ? rq : 0,
4766*45916cd2Sjpk 		    mitcp_state(tp6->tcp6ConnEntryInfo.ce_state, attr),
4767*45916cd2Sjpk 		    ifnamep);
47687c478bd9Sstevel@tonic-gate 	}
4769*45916cd2Sjpk 
4770*45916cd2Sjpk 	print_transport_label(attr);
4771*45916cd2Sjpk 
4772*45916cd2Sjpk 	return (B_FALSE);
47737c478bd9Sstevel@tonic-gate }
47747c478bd9Sstevel@tonic-gate 
47757c478bd9Sstevel@tonic-gate /* ------------------------------- UDP_REPORT------------------------------- */
47767c478bd9Sstevel@tonic-gate 
4777*45916cd2Sjpk static boolean_t udp_report_item_v4(const mib2_udpEntry_t *ude,
4778*45916cd2Sjpk     boolean_t first, const mib2_transportMLPEntry_t *attr);
4779*45916cd2Sjpk static boolean_t udp_report_item_v6(const mib2_udp6Entry_t *ude6,
4780*45916cd2Sjpk     boolean_t first, const mib2_transportMLPEntry_t *attr);
4781*45916cd2Sjpk 
4782*45916cd2Sjpk static const char udp_hdr_v4[] =
4783*45916cd2Sjpk "   Local Address        Remote Address      State\n"
4784*45916cd2Sjpk "-------------------- -------------------- ----------\n";
47857c478bd9Sstevel@tonic-gate 
4786*45916cd2Sjpk static const char udp_hdr_v6[] =
47877c478bd9Sstevel@tonic-gate "   Local Address                     Remote Address                 "
4788*45916cd2Sjpk "  State      If\n"
47897c478bd9Sstevel@tonic-gate "--------------------------------- --------------------------------- "
47907c478bd9Sstevel@tonic-gate "---------- -----\n";
47917c478bd9Sstevel@tonic-gate 
47927c478bd9Sstevel@tonic-gate static void
4793*45916cd2Sjpk udp_report(const mib_item_t *item)
47947c478bd9Sstevel@tonic-gate {
47957c478bd9Sstevel@tonic-gate 	int			jtemp = 0;
47967c478bd9Sstevel@tonic-gate 	boolean_t		print_hdr_once_v4 = B_TRUE;
47977c478bd9Sstevel@tonic-gate 	boolean_t		print_hdr_once_v6 = B_TRUE;
47987c478bd9Sstevel@tonic-gate 	mib2_udpEntry_t		*ude;
47997c478bd9Sstevel@tonic-gate 	mib2_udp6Entry_t	*ude6;
4800*45916cd2Sjpk 	mib2_transportMLPEntry_t **v4_attrs, **v6_attrs;
4801*45916cd2Sjpk 	mib2_transportMLPEntry_t **v4a, **v6a;
4802*45916cd2Sjpk 	mib2_transportMLPEntry_t *aptr;
48037c478bd9Sstevel@tonic-gate 
48047c478bd9Sstevel@tonic-gate 	if (!protocol_selected(IPPROTO_UDP))
48057c478bd9Sstevel@tonic-gate 		return;
48067c478bd9Sstevel@tonic-gate 
4807*45916cd2Sjpk 	/*
4808*45916cd2Sjpk 	 * Preparation pass: the kernel returns separate entries for UDP
4809*45916cd2Sjpk 	 * connection table entries and Multilevel Port attributes.  We loop
4810*45916cd2Sjpk 	 * through the attributes first and set up an array for each address
4811*45916cd2Sjpk 	 * family.
4812*45916cd2Sjpk 	 */
4813*45916cd2Sjpk 	v4_attrs = family_selected(AF_INET) && RSECflag ?
4814*45916cd2Sjpk 	    gather_attrs(item, MIB2_UDP, MIB2_UDP_ENTRY, udpEntrySize) : NULL;
4815*45916cd2Sjpk 	v6_attrs = family_selected(AF_INET6) && RSECflag ?
4816*45916cd2Sjpk 	    gather_attrs(item, MIB2_UDP6, MIB2_UDP6_ENTRY, udp6EntrySize) :
4817*45916cd2Sjpk 	    NULL;
4818*45916cd2Sjpk 
4819*45916cd2Sjpk 	v4a = v4_attrs;
4820*45916cd2Sjpk 	v6a = v6_attrs;
48217c478bd9Sstevel@tonic-gate 	/* 'for' loop 1: */
48227c478bd9Sstevel@tonic-gate 	for (; item; item = item->next_item) {
48237c478bd9Sstevel@tonic-gate 		if (Dflag) {
48247c478bd9Sstevel@tonic-gate 			(void) printf("\n--- Entry %d ---\n", ++jtemp);
48257c478bd9Sstevel@tonic-gate 			(void) printf("Group = %d, mib_id = %d, "
48267c478bd9Sstevel@tonic-gate 			    "length = %d, valp = 0x%p\n",
48277c478bd9Sstevel@tonic-gate 			    item->group, item->mib_id,
48287c478bd9Sstevel@tonic-gate 			    item->length, item->valp);
48297c478bd9Sstevel@tonic-gate 		}
48307c478bd9Sstevel@tonic-gate 		if (!((item->group == MIB2_UDP &&
48317c478bd9Sstevel@tonic-gate 		    item->mib_id == MIB2_UDP_ENTRY) ||
48327c478bd9Sstevel@tonic-gate 		    (item->group == MIB2_UDP6 &&
48337c478bd9Sstevel@tonic-gate 		    item->mib_id == MIB2_UDP6_ENTRY)))
48347c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
48357c478bd9Sstevel@tonic-gate 
48367c478bd9Sstevel@tonic-gate 		if (item->group == MIB2_UDP && !family_selected(AF_INET))
48377c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
48387c478bd9Sstevel@tonic-gate 		else if (item->group == MIB2_UDP6 && !family_selected(AF_INET6))
48397c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
48407c478bd9Sstevel@tonic-gate 
48417c478bd9Sstevel@tonic-gate 		/*	xxx.xxx.xxx.xxx,pppp  sss... */
48427c478bd9Sstevel@tonic-gate 		if (item->group == MIB2_UDP) {
48437c478bd9Sstevel@tonic-gate 			for (ude = (mib2_udpEntry_t *)item->valp;
48447c478bd9Sstevel@tonic-gate 			    (char *)ude < (char *)item->valp + item->length;
48457c478bd9Sstevel@tonic-gate 			    /* LINTED: (note 1) */
48467c478bd9Sstevel@tonic-gate 			    ude = (mib2_udpEntry_t *)((char *)ude +
48477c478bd9Sstevel@tonic-gate 			    udpEntrySize)) {
4848*45916cd2Sjpk 				aptr = v4a == NULL ? NULL : *v4a++;
48497c478bd9Sstevel@tonic-gate 				print_hdr_once_v4 = udp_report_item_v4(ude,
4850*45916cd2Sjpk 				    print_hdr_once_v4, aptr);
48517c478bd9Sstevel@tonic-gate 			}
48527c478bd9Sstevel@tonic-gate 		} else {
48537c478bd9Sstevel@tonic-gate 			for (ude6 = (mib2_udp6Entry_t *)item->valp;
48547c478bd9Sstevel@tonic-gate 			    (char *)ude6 < (char *)item->valp + item->length;
48557c478bd9Sstevel@tonic-gate 			    /* LINTED: (note 1) */
48567c478bd9Sstevel@tonic-gate 			    ude6 = (mib2_udp6Entry_t *)((char *)ude6 +
48577c478bd9Sstevel@tonic-gate 			    udp6EntrySize)) {
4858*45916cd2Sjpk 				aptr = v6a == NULL ? NULL : *v6a++;
48597c478bd9Sstevel@tonic-gate 				print_hdr_once_v6 = udp_report_item_v6(ude6,
4860*45916cd2Sjpk 				    print_hdr_once_v6, aptr);
48617c478bd9Sstevel@tonic-gate 			}
48627c478bd9Sstevel@tonic-gate 		}
48637c478bd9Sstevel@tonic-gate 	} /* 'for' loop 1 ends */
48647c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
4865*45916cd2Sjpk 
4866*45916cd2Sjpk 	if (v4_attrs != NULL)
4867*45916cd2Sjpk 		free(v4_attrs);
4868*45916cd2Sjpk 	if (v6_attrs != NULL)
4869*45916cd2Sjpk 		free(v6_attrs);
48707c478bd9Sstevel@tonic-gate }
48717c478bd9Sstevel@tonic-gate 
48727c478bd9Sstevel@tonic-gate static boolean_t
4873*45916cd2Sjpk udp_report_item_v4(const mib2_udpEntry_t *ude, boolean_t first,
4874*45916cd2Sjpk     const mib2_transportMLPEntry_t *attr)
48757c478bd9Sstevel@tonic-gate {
48767c478bd9Sstevel@tonic-gate 	char	lname[MAXHOSTNAMELEN + MAXHOSTNAMELEN + 1];
48777c478bd9Sstevel@tonic-gate 			/* hostname + portname */
48787c478bd9Sstevel@tonic-gate 
48797c478bd9Sstevel@tonic-gate 	if (!(Aflag || ude->udpEntryInfo.ue_state >= MIB2_UDP_connected))
48807c478bd9Sstevel@tonic-gate 		return (first); /* Nothing to print */
48817c478bd9Sstevel@tonic-gate 
48827c478bd9Sstevel@tonic-gate 	if (first) {
4883*45916cd2Sjpk 		(void) printf(v4compat ? "\nUDP\n" : "\nUDP: IPv4\n");
4884*45916cd2Sjpk 		(void) printf(udp_hdr_v4);
48857c478bd9Sstevel@tonic-gate 		first = B_FALSE;
48867c478bd9Sstevel@tonic-gate 	}
48877c478bd9Sstevel@tonic-gate 
48887c478bd9Sstevel@tonic-gate 	(void) printf("%-20s ",
48897c478bd9Sstevel@tonic-gate 	    pr_ap(ude->udpLocalAddress, ude->udpLocalPort, "udp",
48907c478bd9Sstevel@tonic-gate 	    lname, sizeof (lname)));
4891*45916cd2Sjpk 	(void) printf("%-20s %s\n",
4892*45916cd2Sjpk 	    ude->udpEntryInfo.ue_state == MIB2_UDP_connected ?
4893*45916cd2Sjpk 	    pr_ap(ude->udpEntryInfo.ue_RemoteAddress,
4894*45916cd2Sjpk 	    ude->udpEntryInfo.ue_RemotePort, "udp", lname, sizeof (lname)) :
4895*45916cd2Sjpk 	    "",
4896*45916cd2Sjpk 	    miudp_state(ude->udpEntryInfo.ue_state, attr));
4897*45916cd2Sjpk 
4898*45916cd2Sjpk 	/*
4899*45916cd2Sjpk 	 * UDP sockets don't have remote attributes, so there's no need to
4900*45916cd2Sjpk 	 * print them here.
4901*45916cd2Sjpk 	 */
4902*45916cd2Sjpk 
49037c478bd9Sstevel@tonic-gate 	return (first);
49047c478bd9Sstevel@tonic-gate }
49057c478bd9Sstevel@tonic-gate 
49067c478bd9Sstevel@tonic-gate static boolean_t
4907*45916cd2Sjpk udp_report_item_v6(const mib2_udp6Entry_t *ude6, boolean_t first,
4908*45916cd2Sjpk     const mib2_transportMLPEntry_t *attr)
49097c478bd9Sstevel@tonic-gate {
49107c478bd9Sstevel@tonic-gate 	char	lname[MAXHOSTNAMELEN + MAXHOSTNAMELEN + 1];
49117c478bd9Sstevel@tonic-gate 			/* hostname + portname */
49127c478bd9Sstevel@tonic-gate 	char	ifname[LIFNAMSIZ + 1];
4913*45916cd2Sjpk 	const char *ifnamep;
49147c478bd9Sstevel@tonic-gate 
49157c478bd9Sstevel@tonic-gate 	if (!(Aflag || ude6->udp6EntryInfo.ue_state >= MIB2_UDP_connected))
49167c478bd9Sstevel@tonic-gate 		return (first); /* Nothing to print */
49177c478bd9Sstevel@tonic-gate 
49187c478bd9Sstevel@tonic-gate 	if (first) {
4919*45916cd2Sjpk 		(void) printf("\nUDP: IPv6\n");
4920*45916cd2Sjpk 		(void) printf(udp_hdr_v6);
49217c478bd9Sstevel@tonic-gate 		first = B_FALSE;
49227c478bd9Sstevel@tonic-gate 	}
49237c478bd9Sstevel@tonic-gate 
4924*45916cd2Sjpk 	ifnamep = (ude6->udp6IfIndex != 0) ?
4925*45916cd2Sjpk 	    if_indextoname(ude6->udp6IfIndex, ifname) : NULL;
4926*45916cd2Sjpk 
49277c478bd9Sstevel@tonic-gate 	(void) printf("%-33s ",
49287c478bd9Sstevel@tonic-gate 	    pr_ap6(&ude6->udp6LocalAddress,
49297c478bd9Sstevel@tonic-gate 	    ude6->udp6LocalPort, "udp", lname, sizeof (lname)));
4930*45916cd2Sjpk 	(void) printf("%-33s %-10s %s\n",
4931*45916cd2Sjpk 	    ude6->udp6EntryInfo.ue_state == MIB2_UDP_connected ?
4932*45916cd2Sjpk 	    pr_ap6(&ude6->udp6EntryInfo.ue_RemoteAddress,
4933*45916cd2Sjpk 	    ude6->udp6EntryInfo.ue_RemotePort, "udp", lname, sizeof (lname)) :
4934*45916cd2Sjpk 	    "",
4935*45916cd2Sjpk 	    miudp_state(ude6->udp6EntryInfo.ue_state, attr),
4936*45916cd2Sjpk 	    ifnamep == NULL ? "" : ifnamep);
4937*45916cd2Sjpk 
4938*45916cd2Sjpk 	/*
4939*45916cd2Sjpk 	 * UDP sockets don't have remote attributes, so there's no need to
4940*45916cd2Sjpk 	 * print them here.
4941*45916cd2Sjpk 	 */
4942*45916cd2Sjpk 
49437c478bd9Sstevel@tonic-gate 	return (first);
49447c478bd9Sstevel@tonic-gate }
49457c478bd9Sstevel@tonic-gate 
49467c478bd9Sstevel@tonic-gate /* ------------------------------ SCTP_REPORT------------------------------- */
49477c478bd9Sstevel@tonic-gate 
49487c478bd9Sstevel@tonic-gate static const char sctp_hdr[] =
49497c478bd9Sstevel@tonic-gate "\nSCTP:";
49507c478bd9Sstevel@tonic-gate static const char sctp_hdr_normal[] =
49517c478bd9Sstevel@tonic-gate "        Local Address                   Remote Address          "
49527c478bd9Sstevel@tonic-gate "Swind  Send-Q Rwind  Recv-Q StrsI/O  State\n"
49537c478bd9Sstevel@tonic-gate "------------------------------- ------------------------------- "
49547c478bd9Sstevel@tonic-gate "------ ------ ------ ------ ------- -----------";
49557c478bd9Sstevel@tonic-gate 
49567c478bd9Sstevel@tonic-gate static const char *
4957*45916cd2Sjpk nssctp_state(int state, const mib2_transportMLPEntry_t *attr)
49587c478bd9Sstevel@tonic-gate {
4959*45916cd2Sjpk 	static char sctpsbuf[50];
4960*45916cd2Sjpk 	const char *cp;
4961*45916cd2Sjpk 
49627c478bd9Sstevel@tonic-gate 	switch (state) {
49637c478bd9Sstevel@tonic-gate 	case MIB2_SCTP_closed:
4964*45916cd2Sjpk 		cp = "CLOSED";
4965*45916cd2Sjpk 		break;
49667c478bd9Sstevel@tonic-gate 	case MIB2_SCTP_cookieWait:
4967*45916cd2Sjpk 		cp = "COOKIE_WAIT";
4968*45916cd2Sjpk 		break;
49697c478bd9Sstevel@tonic-gate 	case MIB2_SCTP_cookieEchoed:
4970*45916cd2Sjpk 		cp = "COOKIE_ECHOED";
4971*45916cd2Sjpk 		break;
49727c478bd9Sstevel@tonic-gate 	case MIB2_SCTP_established:
4973*45916cd2Sjpk 		cp = "ESTABLISHED";
4974*45916cd2Sjpk 		break;
49757c478bd9Sstevel@tonic-gate 	case MIB2_SCTP_shutdownPending:
4976*45916cd2Sjpk 		cp = "SHUTDOWN_PENDING";
4977*45916cd2Sjpk 		break;
49787c478bd9Sstevel@tonic-gate 	case MIB2_SCTP_shutdownSent:
4979*45916cd2Sjpk 		cp = "SHUTDOWN_SENT";
4980*45916cd2Sjpk 		break;
49817c478bd9Sstevel@tonic-gate 	case MIB2_SCTP_shutdownReceived:
4982*45916cd2Sjpk 		cp = "SHUTDOWN_RECEIVED";
4983*45916cd2Sjpk 		break;
49847c478bd9Sstevel@tonic-gate 	case MIB2_SCTP_shutdownAckSent:
4985*45916cd2Sjpk 		cp = "SHUTDOWN_ACK_SENT";
4986*45916cd2Sjpk 		break;
49877c478bd9Sstevel@tonic-gate 	case MIB2_SCTP_listen:
4988*45916cd2Sjpk 		cp = "LISTEN";
4989*45916cd2Sjpk 		break;
49907c478bd9Sstevel@tonic-gate 	default:
4991*45916cd2Sjpk 		(void) snprintf(sctpsbuf, sizeof (sctpsbuf),
4992*45916cd2Sjpk 		    "UNKNOWN STATE(%d)", state);
4993*45916cd2Sjpk 		cp = sctpsbuf;
4994*45916cd2Sjpk 		break;
4995*45916cd2Sjpk 	}
4996*45916cd2Sjpk 
4997*45916cd2Sjpk 	if (RSECflag && attr != NULL && attr->tme_flags != 0) {
4998*45916cd2Sjpk 		if (cp != sctpsbuf) {
4999*45916cd2Sjpk 			(void) strlcpy(sctpsbuf, cp, sizeof (sctpsbuf));
5000*45916cd2Sjpk 			cp = sctpsbuf;
5001*45916cd2Sjpk 		}
5002*45916cd2Sjpk 		if (attr->tme_flags & MIB2_TMEF_PRIVATE)
5003*45916cd2Sjpk 			(void) strlcat(sctpsbuf, " P", sizeof (sctpsbuf));
5004*45916cd2Sjpk 		if (attr->tme_flags & MIB2_TMEF_SHARED)
5005*45916cd2Sjpk 			(void) strlcat(sctpsbuf, " S", sizeof (sctpsbuf));
50067c478bd9Sstevel@tonic-gate 	}
5007*45916cd2Sjpk 
5008*45916cd2Sjpk 	return (cp);
50097c478bd9Sstevel@tonic-gate }
50107c478bd9Sstevel@tonic-gate 
5011*45916cd2Sjpk static const mib2_sctpConnRemoteEntry_t *
5012*45916cd2Sjpk sctp_getnext_rem(const mib_item_t **itemp,
5013*45916cd2Sjpk     const mib2_sctpConnRemoteEntry_t *current, uint32_t associd)
50147c478bd9Sstevel@tonic-gate {
5015*45916cd2Sjpk 	const mib_item_t *item = *itemp;
5016*45916cd2Sjpk 	const mib2_sctpConnRemoteEntry_t	*sre;
50177c478bd9Sstevel@tonic-gate 
50187c478bd9Sstevel@tonic-gate 	for (; item != NULL; item = item->next_item, current = NULL) {
50197c478bd9Sstevel@tonic-gate 		if (!(item->group == MIB2_SCTP &&
50207c478bd9Sstevel@tonic-gate 		    item->mib_id == MIB2_SCTP_CONN_REMOTE)) {
50217c478bd9Sstevel@tonic-gate 			continue;
50227c478bd9Sstevel@tonic-gate 		}
50237c478bd9Sstevel@tonic-gate 
50247c478bd9Sstevel@tonic-gate 		if (current != NULL) {
50257c478bd9Sstevel@tonic-gate 			/* LINTED: (note 1) */
5026*45916cd2Sjpk 			sre = (const mib2_sctpConnRemoteEntry_t *)
5027*45916cd2Sjpk 			    ((const char *)current + sctpRemoteEntrySize);
50287c478bd9Sstevel@tonic-gate 		} else {
50297c478bd9Sstevel@tonic-gate 			sre = item->valp;
50307c478bd9Sstevel@tonic-gate 		}
50317c478bd9Sstevel@tonic-gate 		for (; (char *)sre < (char *)item->valp + item->length;
5032*45916cd2Sjpk 		    /* LINTED: (note 1) */
5033*45916cd2Sjpk 		    sre = (const mib2_sctpConnRemoteEntry_t *)
5034*45916cd2Sjpk 		    ((const char *)sre + sctpRemoteEntrySize)) {
50357c478bd9Sstevel@tonic-gate 			if (sre->sctpAssocId != associd) {
50367c478bd9Sstevel@tonic-gate 				continue;
50377c478bd9Sstevel@tonic-gate 			}
50387c478bd9Sstevel@tonic-gate 			*itemp = item;
50397c478bd9Sstevel@tonic-gate 			return (sre);
50407c478bd9Sstevel@tonic-gate 		}
50417c478bd9Sstevel@tonic-gate 	}
50427c478bd9Sstevel@tonic-gate 	*itemp = NULL;
50437c478bd9Sstevel@tonic-gate 	return (NULL);
50447c478bd9Sstevel@tonic-gate }
50457c478bd9Sstevel@tonic-gate 
5046*45916cd2Sjpk static const mib2_sctpConnLocalEntry_t *
5047*45916cd2Sjpk sctp_getnext_local(const mib_item_t **itemp,
5048*45916cd2Sjpk     const mib2_sctpConnLocalEntry_t *current, uint32_t associd)
50497c478bd9Sstevel@tonic-gate {
5050*45916cd2Sjpk 	const mib_item_t *item = *itemp;
5051*45916cd2Sjpk 	const mib2_sctpConnLocalEntry_t	*sle;
50527c478bd9Sstevel@tonic-gate 
50537c478bd9Sstevel@tonic-gate 	for (; item != NULL; item = item->next_item, current = NULL) {
50547c478bd9Sstevel@tonic-gate 		if (!(item->group == MIB2_SCTP &&
50557c478bd9Sstevel@tonic-gate 		    item->mib_id == MIB2_SCTP_CONN_LOCAL)) {
50567c478bd9Sstevel@tonic-gate 			continue;
50577c478bd9Sstevel@tonic-gate 		}
50587c478bd9Sstevel@tonic-gate 
50597c478bd9Sstevel@tonic-gate 		if (current != NULL) {
50607c478bd9Sstevel@tonic-gate 			/* LINTED: (note 1) */
5061*45916cd2Sjpk 			sle = (const mib2_sctpConnLocalEntry_t *)
5062*45916cd2Sjpk 			    ((const char *)current + sctpLocalEntrySize);
50637c478bd9Sstevel@tonic-gate 		} else {
50647c478bd9Sstevel@tonic-gate 			sle = item->valp;
50657c478bd9Sstevel@tonic-gate 		}
50667c478bd9Sstevel@tonic-gate 		for (; (char *)sle < (char *)item->valp + item->length;
5067*45916cd2Sjpk 		    /* LINTED: (note 1) */
5068*45916cd2Sjpk 		    sle = (const mib2_sctpConnLocalEntry_t *)
5069*45916cd2Sjpk 		    ((const char *)sle + sctpLocalEntrySize)) {
50707c478bd9Sstevel@tonic-gate 			if (sle->sctpAssocId != associd) {
50717c478bd9Sstevel@tonic-gate 				continue;
50727c478bd9Sstevel@tonic-gate 			}
50737c478bd9Sstevel@tonic-gate 			*itemp = item;
50747c478bd9Sstevel@tonic-gate 			return (sle);
50757c478bd9Sstevel@tonic-gate 		}
50767c478bd9Sstevel@tonic-gate 	}
50777c478bd9Sstevel@tonic-gate 	*itemp = NULL;
50787c478bd9Sstevel@tonic-gate 	return (NULL);
50797c478bd9Sstevel@tonic-gate }
50807c478bd9Sstevel@tonic-gate 
50817c478bd9Sstevel@tonic-gate static void
50827c478bd9Sstevel@tonic-gate sctp_pr_addr(int type, char *name, int namelen, const in6_addr_t *addr,
50837c478bd9Sstevel@tonic-gate     int port)
50847c478bd9Sstevel@tonic-gate {
50857c478bd9Sstevel@tonic-gate 	ipaddr_t	v4addr;
50867c478bd9Sstevel@tonic-gate 	in6_addr_t	v6addr;
50877c478bd9Sstevel@tonic-gate 
50887c478bd9Sstevel@tonic-gate 	/*
50897c478bd9Sstevel@tonic-gate 	 * Address is either a v4 mapped or v6 addr. If
50907c478bd9Sstevel@tonic-gate 	 * it's a v4 mapped, convert to v4 before
50917c478bd9Sstevel@tonic-gate 	 * displaying.
50927c478bd9Sstevel@tonic-gate 	 */
50937c478bd9Sstevel@tonic-gate 	switch (type) {
50947c478bd9Sstevel@tonic-gate 	    case MIB2_SCTP_ADDR_V4:
50957c478bd9Sstevel@tonic-gate 		/* v4 */
50967c478bd9Sstevel@tonic-gate 		v6addr = *addr;
50977c478bd9Sstevel@tonic-gate 
50987c478bd9Sstevel@tonic-gate 		IN6_V4MAPPED_TO_IPADDR(&v6addr, v4addr);
50997c478bd9Sstevel@tonic-gate 		if (port > 0) {
51007c478bd9Sstevel@tonic-gate 			(void) pr_ap(v4addr, port, "sctp", name, namelen);
51017c478bd9Sstevel@tonic-gate 		} else {
51027c478bd9Sstevel@tonic-gate 			(void) pr_addr(v4addr, name, namelen);
51037c478bd9Sstevel@tonic-gate 		}
51047c478bd9Sstevel@tonic-gate 		break;
51057c478bd9Sstevel@tonic-gate 
51067c478bd9Sstevel@tonic-gate 	    case MIB2_SCTP_ADDR_V6:
51077c478bd9Sstevel@tonic-gate 		/* v6 */
51087c478bd9Sstevel@tonic-gate 		if (port > 0) {
51097c478bd9Sstevel@tonic-gate 			(void) pr_ap6(addr, port, "sctp", name, namelen);
51107c478bd9Sstevel@tonic-gate 		} else {
51117c478bd9Sstevel@tonic-gate 			(void) pr_addr6(addr, name, namelen);
51127c478bd9Sstevel@tonic-gate 		}
51137c478bd9Sstevel@tonic-gate 		break;
51147c478bd9Sstevel@tonic-gate 
51157c478bd9Sstevel@tonic-gate 	    default:
51167c478bd9Sstevel@tonic-gate 		(void) snprintf(name, namelen, "<unknown addr type>");
51177c478bd9Sstevel@tonic-gate 		break;
51187c478bd9Sstevel@tonic-gate 	}
51197c478bd9Sstevel@tonic-gate }
51207c478bd9Sstevel@tonic-gate 
51217c478bd9Sstevel@tonic-gate static void
5122*45916cd2Sjpk sctp_conn_report_item(const mib_item_t *head, const mib2_sctpConnEntry_t *sp,
5123*45916cd2Sjpk     const mib2_transportMLPEntry_t *attr)
51247c478bd9Sstevel@tonic-gate {
51257c478bd9Sstevel@tonic-gate 	char		lname[MAXHOSTNAMELEN + MAXHOSTNAMELEN + 1];
51267c478bd9Sstevel@tonic-gate 	char		fname[MAXHOSTNAMELEN + MAXHOSTNAMELEN + 1];
5127*45916cd2Sjpk 	const mib2_sctpConnRemoteEntry_t	*sre = NULL;
5128*45916cd2Sjpk 	const mib2_sctpConnLocalEntry_t	*sle = NULL;
5129*45916cd2Sjpk 	const mib_item_t *local = head;
5130*45916cd2Sjpk 	const mib_item_t *remote = head;
51317c478bd9Sstevel@tonic-gate 	uint32_t	id = sp->sctpAssocId;
51327c478bd9Sstevel@tonic-gate 	boolean_t	printfirst = B_TRUE;
51337c478bd9Sstevel@tonic-gate 
51347c478bd9Sstevel@tonic-gate 	sctp_pr_addr(sp->sctpAssocRemPrimAddrType, fname, sizeof (fname),
51357c478bd9Sstevel@tonic-gate 	    &sp->sctpAssocRemPrimAddr, sp->sctpAssocRemPort);
51367c478bd9Sstevel@tonic-gate 	sctp_pr_addr(sp->sctpAssocRemPrimAddrType, lname, sizeof (lname),
51377c478bd9Sstevel@tonic-gate 	    &sp->sctpAssocLocPrimAddr, sp->sctpAssocLocalPort);
51387c478bd9Sstevel@tonic-gate 
51397c478bd9Sstevel@tonic-gate 	(void) printf("%-31s %-31s %6u %6d %6u %6d %3d/%-3d %s\n",
51407c478bd9Sstevel@tonic-gate 	    lname, fname,
51417c478bd9Sstevel@tonic-gate 	    sp->sctpConnEntryInfo.ce_swnd,
51427c478bd9Sstevel@tonic-gate 	    sp->sctpConnEntryInfo.ce_sendq,
51437c478bd9Sstevel@tonic-gate 	    sp->sctpConnEntryInfo.ce_rwnd,
51447c478bd9Sstevel@tonic-gate 	    sp->sctpConnEntryInfo.ce_recvq,
51457c478bd9Sstevel@tonic-gate 	    sp->sctpAssocInStreams, sp->sctpAssocOutStreams,
5146*45916cd2Sjpk 	    nssctp_state(sp->sctpAssocState, attr));
5147*45916cd2Sjpk 
5148*45916cd2Sjpk 	print_transport_label(attr);
51497c478bd9Sstevel@tonic-gate 
51507c478bd9Sstevel@tonic-gate 	if (!Vflag) {
51517c478bd9Sstevel@tonic-gate 		return;
51527c478bd9Sstevel@tonic-gate 	}
51537c478bd9Sstevel@tonic-gate 
51547c478bd9Sstevel@tonic-gate 	/* Print remote addresses/local addresses on following lines */
51557c478bd9Sstevel@tonic-gate 	while ((sre = sctp_getnext_rem(&remote, sre, id)) != NULL) {
51567c478bd9Sstevel@tonic-gate 		if (!IN6_ARE_ADDR_EQUAL(&sre->sctpAssocRemAddr,
51577c478bd9Sstevel@tonic-gate 		    &sp->sctpAssocRemPrimAddr)) {
51587c478bd9Sstevel@tonic-gate 			if (printfirst == B_TRUE) {
51597c478bd9Sstevel@tonic-gate 				(void) fputs("\t<Remote: ", stdout);
51607c478bd9Sstevel@tonic-gate 				printfirst = B_FALSE;
51617c478bd9Sstevel@tonic-gate 			} else {
51627c478bd9Sstevel@tonic-gate 				(void) fputs(", ", stdout);
51637c478bd9Sstevel@tonic-gate 			}
51647c478bd9Sstevel@tonic-gate 			sctp_pr_addr(sre->sctpAssocRemAddrType, fname,
51657c478bd9Sstevel@tonic-gate 			    sizeof (fname), &sre->sctpAssocRemAddr, -1);
51667c478bd9Sstevel@tonic-gate 			if (sre->sctpAssocRemAddrActive == MIB2_SCTP_ACTIVE) {
51677c478bd9Sstevel@tonic-gate 				(void) fputs(fname, stdout);
51687c478bd9Sstevel@tonic-gate 			} else {
51697c478bd9Sstevel@tonic-gate 				(void) printf("(%s)", fname);
51707c478bd9Sstevel@tonic-gate 			}
51717c478bd9Sstevel@tonic-gate 		}
51727c478bd9Sstevel@tonic-gate 	}
51737c478bd9Sstevel@tonic-gate 	if (printfirst == B_FALSE) {
51747c478bd9Sstevel@tonic-gate 		(void) puts(">");
51757c478bd9Sstevel@tonic-gate 		printfirst = B_TRUE;
51767c478bd9Sstevel@tonic-gate 	}
51777c478bd9Sstevel@tonic-gate 	while ((sle = sctp_getnext_local(&local, sle, id)) != NULL) {
51787c478bd9Sstevel@tonic-gate 		if (!IN6_ARE_ADDR_EQUAL(&sle->sctpAssocLocalAddr,
51797c478bd9Sstevel@tonic-gate 		    &sp->sctpAssocLocPrimAddr)) {
51807c478bd9Sstevel@tonic-gate 			if (printfirst == B_TRUE) {
51817c478bd9Sstevel@tonic-gate 				(void) fputs("\t<Local: ", stdout);
51827c478bd9Sstevel@tonic-gate 				printfirst = B_FALSE;
51837c478bd9Sstevel@tonic-gate 			} else {
51847c478bd9Sstevel@tonic-gate 				(void) fputs(", ", stdout);
51857c478bd9Sstevel@tonic-gate 			}
51867c478bd9Sstevel@tonic-gate 			sctp_pr_addr(sle->sctpAssocLocalAddrType, lname,
51877c478bd9Sstevel@tonic-gate 			    sizeof (lname), &sle->sctpAssocLocalAddr, -1);
51887c478bd9Sstevel@tonic-gate 			(void) fputs(lname, stdout);
51897c478bd9Sstevel@tonic-gate 		}
51907c478bd9Sstevel@tonic-gate 	}
51917c478bd9Sstevel@tonic-gate 	if (printfirst == B_FALSE) {
51927c478bd9Sstevel@tonic-gate 		(void) puts(">");
51937c478bd9Sstevel@tonic-gate 	}
51947c478bd9Sstevel@tonic-gate }
51957c478bd9Sstevel@tonic-gate 
51967c478bd9Sstevel@tonic-gate static void
5197*45916cd2Sjpk sctp_report(const mib_item_t *item)
51987c478bd9Sstevel@tonic-gate {
5199*45916cd2Sjpk 	const mib_item_t		*head;
5200*45916cd2Sjpk 	const mib2_sctpConnEntry_t	*sp;
52017c478bd9Sstevel@tonic-gate 	boolean_t		first = B_TRUE;
5202*45916cd2Sjpk 	mib2_transportMLPEntry_t **attrs, **aptr;
5203*45916cd2Sjpk 	mib2_transportMLPEntry_t *attr;
52047c478bd9Sstevel@tonic-gate 
5205*45916cd2Sjpk 	/*
5206*45916cd2Sjpk 	 * Preparation pass: the kernel returns separate entries for SCTP
5207*45916cd2Sjpk 	 * connection table entries and Multilevel Port attributes.  We loop
5208*45916cd2Sjpk 	 * through the attributes first and set up an array for each address
5209*45916cd2Sjpk 	 * family.
5210*45916cd2Sjpk 	 */
5211*45916cd2Sjpk 	attrs = RSECflag ?
5212*45916cd2Sjpk 	    gather_attrs(item, MIB2_SCTP, MIB2_SCTP_CONN, sctpEntrySize) :
5213*45916cd2Sjpk 	    NULL;
5214*45916cd2Sjpk 
5215*45916cd2Sjpk 	aptr = attrs;
52167c478bd9Sstevel@tonic-gate 	head = item;
52177c478bd9Sstevel@tonic-gate 	for (; item != NULL; item = item->next_item) {
52187c478bd9Sstevel@tonic-gate 
52197c478bd9Sstevel@tonic-gate 		if (!(item->group == MIB2_SCTP &&
52207c478bd9Sstevel@tonic-gate 		    item->mib_id == MIB2_SCTP_CONN))
52217c478bd9Sstevel@tonic-gate 			continue;
52227c478bd9Sstevel@tonic-gate 
52237c478bd9Sstevel@tonic-gate 		for (sp = item->valp;
5224*45916cd2Sjpk 		    (char *)sp < (char *)item->valp + item->length;
5225*45916cd2Sjpk 		    /* LINTED: (note 1) */
5226*45916cd2Sjpk 		    sp = (mib2_sctpConnEntry_t *)((char *)sp + sctpEntrySize)) {
5227*45916cd2Sjpk 			attr = aptr == NULL ? NULL : *aptr++;
52287c478bd9Sstevel@tonic-gate 			if (Aflag ||
52297c478bd9Sstevel@tonic-gate 			    sp->sctpAssocState >= MIB2_SCTP_established) {
52307c478bd9Sstevel@tonic-gate 				if (first == B_TRUE) {
52317c478bd9Sstevel@tonic-gate 					(void) puts(sctp_hdr);
52327c478bd9Sstevel@tonic-gate 					(void) puts(sctp_hdr_normal);
52337c478bd9Sstevel@tonic-gate 					first = B_FALSE;
52347c478bd9Sstevel@tonic-gate 				}
5235*45916cd2Sjpk 				sctp_conn_report_item(head, sp, attr);
52367c478bd9Sstevel@tonic-gate 			}
52377c478bd9Sstevel@tonic-gate 		}
52387c478bd9Sstevel@tonic-gate 	}
5239*45916cd2Sjpk 	if (attrs != NULL)
5240*45916cd2Sjpk 		free(attrs);
52417c478bd9Sstevel@tonic-gate }
52427c478bd9Sstevel@tonic-gate 
52437c478bd9Sstevel@tonic-gate static char *
52447c478bd9Sstevel@tonic-gate plural(int n)
52457c478bd9Sstevel@tonic-gate {
52467c478bd9Sstevel@tonic-gate 	return (n != 1 ? "s" : "");
52477c478bd9Sstevel@tonic-gate }
52487c478bd9Sstevel@tonic-gate 
52497c478bd9Sstevel@tonic-gate static char *
52507c478bd9Sstevel@tonic-gate pluraly(int n)
52517c478bd9Sstevel@tonic-gate {
52527c478bd9Sstevel@tonic-gate 	return (n != 1 ? "ies" : "y");
52537c478bd9Sstevel@tonic-gate }
52547c478bd9Sstevel@tonic-gate 
52557c478bd9Sstevel@tonic-gate static char *
52567c478bd9Sstevel@tonic-gate plurales(int n)
52577c478bd9Sstevel@tonic-gate {
52587c478bd9Sstevel@tonic-gate 	return (n != 1 ? "es" : "");
52597c478bd9Sstevel@tonic-gate }
52607c478bd9Sstevel@tonic-gate 
52617c478bd9Sstevel@tonic-gate static char *
52627c478bd9Sstevel@tonic-gate pktscale(n)
52637c478bd9Sstevel@tonic-gate 	int n;
52647c478bd9Sstevel@tonic-gate {
52657c478bd9Sstevel@tonic-gate 	static char buf[6];
52667c478bd9Sstevel@tonic-gate 	char t;
52677c478bd9Sstevel@tonic-gate 
52687c478bd9Sstevel@tonic-gate 	if (n < 1024) {
52697c478bd9Sstevel@tonic-gate 		t = ' ';
52707c478bd9Sstevel@tonic-gate 	} else if (n < 1024 * 1024) {
52717c478bd9Sstevel@tonic-gate 		t = 'k';
52727c478bd9Sstevel@tonic-gate 		n /= 1024;
52737c478bd9Sstevel@tonic-gate 	} else if (n < 1024 * 1024 * 1024) {
52747c478bd9Sstevel@tonic-gate 		t = 'm';
52757c478bd9Sstevel@tonic-gate 		n /= 1024 * 1024;
52767c478bd9Sstevel@tonic-gate 	} else {
52777c478bd9Sstevel@tonic-gate 		t = 'g';
52787c478bd9Sstevel@tonic-gate 		n /= 1024 * 1024 * 1024;
52797c478bd9Sstevel@tonic-gate 	}
52807c478bd9Sstevel@tonic-gate 
52817c478bd9Sstevel@tonic-gate 	(void) snprintf(buf, sizeof (buf), "%4u%c", n, t);
52827c478bd9Sstevel@tonic-gate 	return (buf);
52837c478bd9Sstevel@tonic-gate }
52847c478bd9Sstevel@tonic-gate 
52857c478bd9Sstevel@tonic-gate /* --------------------- mrt_report (netstat -m) -------------------------- */
52867c478bd9Sstevel@tonic-gate 
52877c478bd9Sstevel@tonic-gate static void
52887c478bd9Sstevel@tonic-gate mrt_report(mib_item_t *item)
52897c478bd9Sstevel@tonic-gate {
52907c478bd9Sstevel@tonic-gate 	int		jtemp = 0;
52917c478bd9Sstevel@tonic-gate 	struct vifctl	*vip;
52927c478bd9Sstevel@tonic-gate 	vifi_t		vifi;
52937c478bd9Sstevel@tonic-gate 	struct mfcctl	*mfccp;
52947c478bd9Sstevel@tonic-gate 	int		numvifs = 0;
52957c478bd9Sstevel@tonic-gate 	int		nmfc = 0;
52967c478bd9Sstevel@tonic-gate 	char		abuf[MAXHOSTNAMELEN + 1];
52977c478bd9Sstevel@tonic-gate 
52987c478bd9Sstevel@tonic-gate 	if (!(family_selected(AF_INET)))
52997c478bd9Sstevel@tonic-gate 		return;
53007c478bd9Sstevel@tonic-gate 
53017c478bd9Sstevel@tonic-gate 	/* 'for' loop 1: */
53027c478bd9Sstevel@tonic-gate 	for (; item; item = item->next_item) {
53037c478bd9Sstevel@tonic-gate 		if (Dflag) {
53047c478bd9Sstevel@tonic-gate 			(void) printf("\n--- Entry %d ---\n", ++jtemp);
53057c478bd9Sstevel@tonic-gate 			(void) printf("Group = %d, mib_id = %d, "
53067c478bd9Sstevel@tonic-gate 			    "length = %d, valp = 0x%p\n",
53077c478bd9Sstevel@tonic-gate 			    item->group, item->mib_id, item->length,
53087c478bd9Sstevel@tonic-gate 			    item->valp);
53097c478bd9Sstevel@tonic-gate 		}
53107c478bd9Sstevel@tonic-gate 		if (item->group != EXPER_DVMRP)
53117c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
53127c478bd9Sstevel@tonic-gate 
53137c478bd9Sstevel@tonic-gate 		switch (item->mib_id) {
53147c478bd9Sstevel@tonic-gate 
53157c478bd9Sstevel@tonic-gate 		case EXPER_DVMRP_VIF:
53167c478bd9Sstevel@tonic-gate 			if (Dflag)
53177c478bd9Sstevel@tonic-gate 				(void) printf("%u records for ipVifTable:\n",
53187c478bd9Sstevel@tonic-gate 				    item->length/sizeof (struct vifctl));
53197c478bd9Sstevel@tonic-gate 			if (item->length/sizeof (struct vifctl) == 0) {
53207c478bd9Sstevel@tonic-gate 				(void) puts("\nVirtual Interface Table is "
53217c478bd9Sstevel@tonic-gate 				    "empty");
53227c478bd9Sstevel@tonic-gate 				break;
53237c478bd9Sstevel@tonic-gate 			}
53247c478bd9Sstevel@tonic-gate 
53257c478bd9Sstevel@tonic-gate 			(void) puts("\nVirtual Interface Table\n"
53267c478bd9Sstevel@tonic-gate 			    " Vif Threshold Rate_Limit Local-Address"
53277c478bd9Sstevel@tonic-gate 			    "   Remote-Address     Pkt_in   Pkt_out");
53287c478bd9Sstevel@tonic-gate 
53297c478bd9Sstevel@tonic-gate 			/* 'for' loop 2: */
53307c478bd9Sstevel@tonic-gate 			for (vip = (struct vifctl *)item->valp;
53317c478bd9Sstevel@tonic-gate 			    (char *)vip < (char *)item->valp + item->length;
53327c478bd9Sstevel@tonic-gate 			    /* LINTED: (note 1) */
53337c478bd9Sstevel@tonic-gate 			    vip = (struct vifctl *)((char *)vip +
53347c478bd9Sstevel@tonic-gate 			    vifctlSize)) {
53357c478bd9Sstevel@tonic-gate 				if (vip->vifc_lcl_addr.s_addr == 0)
53367c478bd9Sstevel@tonic-gate 					continue; /* 'for' loop 2 */
53377c478bd9Sstevel@tonic-gate 				/* numvifs = vip->vifc_vifi; */
53387c478bd9Sstevel@tonic-gate 
53397c478bd9Sstevel@tonic-gate 				numvifs++;
53407c478bd9Sstevel@tonic-gate 				(void) printf("  %2u       %3u       "
53417c478bd9Sstevel@tonic-gate 				    "%4u %-15.15s",
53427c478bd9Sstevel@tonic-gate 				    vip->vifc_vifi,
53437c478bd9Sstevel@tonic-gate 				    vip->vifc_threshold,
53447c478bd9Sstevel@tonic-gate 				    vip->vifc_rate_limit,
53457c478bd9Sstevel@tonic-gate 				    pr_addr(vip->vifc_lcl_addr.s_addr,
53467c478bd9Sstevel@tonic-gate 				    abuf, sizeof (abuf)));
53477c478bd9Sstevel@tonic-gate 				(void) printf(" %-15.15s  %8u  %8u\n",
53487c478bd9Sstevel@tonic-gate 				    (vip->vifc_flags & VIFF_TUNNEL) ?
53497c478bd9Sstevel@tonic-gate 				    pr_addr(vip->vifc_rmt_addr.s_addr,
53507c478bd9Sstevel@tonic-gate 				    abuf, sizeof (abuf)) : "",
53517c478bd9Sstevel@tonic-gate 				    vip->vifc_pkt_in,
53527c478bd9Sstevel@tonic-gate 				    vip->vifc_pkt_out);
53537c478bd9Sstevel@tonic-gate 			} /* 'for' loop 2 ends */
53547c478bd9Sstevel@tonic-gate 
53557c478bd9Sstevel@tonic-gate 			(void) printf("Numvifs: %d\n", numvifs);
53567c478bd9Sstevel@tonic-gate 			break;
53577c478bd9Sstevel@tonic-gate 
53587c478bd9Sstevel@tonic-gate 		case EXPER_DVMRP_MRT:
53597c478bd9Sstevel@tonic-gate 			if (Dflag)
53607c478bd9Sstevel@tonic-gate 				(void) printf("%u records for ipMfcTable:\n",
53617c478bd9Sstevel@tonic-gate 					item->length/sizeof (struct vifctl));
53627c478bd9Sstevel@tonic-gate 			if (item->length/sizeof (struct vifctl) == 0) {
53637c478bd9Sstevel@tonic-gate 				(void) puts("\nMulticast Forwarding Cache is "
53647c478bd9Sstevel@tonic-gate 				    "empty");
53657c478bd9Sstevel@tonic-gate 				break;
53667c478bd9Sstevel@tonic-gate 			}
53677c478bd9Sstevel@tonic-gate 
53687c478bd9Sstevel@tonic-gate 			(void) puts("\nMulticast Forwarding Cache\n"
53697c478bd9Sstevel@tonic-gate 			    "  Origin-Subnet                 Mcastgroup      "
53707c478bd9Sstevel@tonic-gate 			    "# Pkts  In-Vif  Out-vifs/Forw-ttl");
53717c478bd9Sstevel@tonic-gate 
53727c478bd9Sstevel@tonic-gate 			for (mfccp = (struct mfcctl *)item->valp;
53737c478bd9Sstevel@tonic-gate 			    (char *)mfccp < (char *)item->valp + item->length;
53747c478bd9Sstevel@tonic-gate 			    /* LINTED: (note 1) */
53757c478bd9Sstevel@tonic-gate 			    mfccp = (struct mfcctl *)((char *)mfccp +
53767c478bd9Sstevel@tonic-gate 			    mfcctlSize)) {
53777c478bd9Sstevel@tonic-gate 
53787c478bd9Sstevel@tonic-gate 				nmfc++;
53797c478bd9Sstevel@tonic-gate 				(void) printf("  %-30.15s",
53807c478bd9Sstevel@tonic-gate 				    pr_addr(mfccp->mfcc_origin.s_addr,
53817c478bd9Sstevel@tonic-gate 				    abuf, sizeof (abuf)));
53827c478bd9Sstevel@tonic-gate 				(void) printf("%-15.15s  %6s  %3u    ",
53837c478bd9Sstevel@tonic-gate 				    pr_net(mfccp->mfcc_mcastgrp.s_addr,
53847c478bd9Sstevel@tonic-gate 					mfccp->mfcc_mcastgrp.s_addr,
53857c478bd9Sstevel@tonic-gate 					abuf, sizeof (abuf)),
53867c478bd9Sstevel@tonic-gate 				    pktscale((int)mfccp->mfcc_pkt_cnt),
53877c478bd9Sstevel@tonic-gate 					mfccp->mfcc_parent);
53887c478bd9Sstevel@tonic-gate 
53897c478bd9Sstevel@tonic-gate 				for (vifi = 0; vifi < MAXVIFS; ++vifi) {
53907c478bd9Sstevel@tonic-gate 					if (mfccp->mfcc_ttls[vifi]) {
53917c478bd9Sstevel@tonic-gate 						(void) printf("      %u (%u)",
53927c478bd9Sstevel@tonic-gate 						    vifi,
53937c478bd9Sstevel@tonic-gate 						    mfccp->mfcc_ttls[vifi]);
53947c478bd9Sstevel@tonic-gate 					}
53957c478bd9Sstevel@tonic-gate 
53967c478bd9Sstevel@tonic-gate 				}
53977c478bd9Sstevel@tonic-gate 				(void) putchar('\n');
53987c478bd9Sstevel@tonic-gate 			}
53997c478bd9Sstevel@tonic-gate 			(void) printf("\nTotal no. of entries in cache: %d\n",
54007c478bd9Sstevel@tonic-gate 			    nmfc);
54017c478bd9Sstevel@tonic-gate 			break;
54027c478bd9Sstevel@tonic-gate 		}
54037c478bd9Sstevel@tonic-gate 	} /* 'for' loop 1 ends */
54047c478bd9Sstevel@tonic-gate 	(void) putchar('\n');
54057c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
54067c478bd9Sstevel@tonic-gate }
54077c478bd9Sstevel@tonic-gate 
54087c478bd9Sstevel@tonic-gate /*
54097c478bd9Sstevel@tonic-gate  * Get the stats for the cache named 'name'.  If prefix != 0, then
54107c478bd9Sstevel@tonic-gate  * interpret the name as a prefix, and sum up stats for all caches
54117c478bd9Sstevel@tonic-gate  * named 'name*'.
54127c478bd9Sstevel@tonic-gate  */
54137c478bd9Sstevel@tonic-gate static void
54147c478bd9Sstevel@tonic-gate kmem_cache_stats(char *title, char *name, int prefix, int64_t *total_bytes)
54157c478bd9Sstevel@tonic-gate {
54167c478bd9Sstevel@tonic-gate 	int len;
54177c478bd9Sstevel@tonic-gate 	int alloc;
54187c478bd9Sstevel@tonic-gate 	int64_t total_alloc = 0;
54197c478bd9Sstevel@tonic-gate 	int alloc_fail, total_alloc_fail = 0;
54207c478bd9Sstevel@tonic-gate 	int buf_size = 0;
54217c478bd9Sstevel@tonic-gate 	int buf_avail;
54227c478bd9Sstevel@tonic-gate 	int buf_total;
54237c478bd9Sstevel@tonic-gate 	int buf_max, total_buf_max = 0;
54247c478bd9Sstevel@tonic-gate 	int buf_inuse, total_buf_inuse = 0;
54257c478bd9Sstevel@tonic-gate 	kstat_t *ksp;
54267c478bd9Sstevel@tonic-gate 	char buf[256];
54277c478bd9Sstevel@tonic-gate 
54287c478bd9Sstevel@tonic-gate 	len = prefix ? strlen(name) : 256;
54297c478bd9Sstevel@tonic-gate 
54307c478bd9Sstevel@tonic-gate 	/* 'for' loop 1: */
54317c478bd9Sstevel@tonic-gate 	for (ksp = kc->kc_chain; ksp != NULL; ksp = ksp->ks_next) {
54327c478bd9Sstevel@tonic-gate 
54337c478bd9Sstevel@tonic-gate 		if (strcmp(ksp->ks_class, "kmem_cache") != 0)
54347c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
54357c478bd9Sstevel@tonic-gate 
54367c478bd9Sstevel@tonic-gate 		/*
54377c478bd9Sstevel@tonic-gate 		 * Hack alert: because of the way streams messages are
54387c478bd9Sstevel@tonic-gate 		 * allocated, every constructed free dblk has an associated
54397c478bd9Sstevel@tonic-gate 		 * mblk.  From the allocator's viewpoint those mblks are
54407c478bd9Sstevel@tonic-gate 		 * allocated (because they haven't been freed), but from
54417c478bd9Sstevel@tonic-gate 		 * our viewpoint they're actually free (because they're
54427c478bd9Sstevel@tonic-gate 		 * not currently in use).  To account for this caching
54437c478bd9Sstevel@tonic-gate 		 * effect we subtract the total constructed free dblks
54447c478bd9Sstevel@tonic-gate 		 * from the total allocated mblks to derive mblks in use.
54457c478bd9Sstevel@tonic-gate 		 */
54467c478bd9Sstevel@tonic-gate 		if (strcmp(name, "streams_mblk") == 0 &&
54477c478bd9Sstevel@tonic-gate 		    strncmp(ksp->ks_name, "streams_dblk", 12) == 0) {
54487c478bd9Sstevel@tonic-gate 			(void) safe_kstat_read(kc, ksp, NULL);
54497c478bd9Sstevel@tonic-gate 			total_buf_inuse -=
54507c478bd9Sstevel@tonic-gate 				kstat_named_value(ksp, "buf_constructed");
54517c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
54527c478bd9Sstevel@tonic-gate 		}
54537c478bd9Sstevel@tonic-gate 
54547c478bd9Sstevel@tonic-gate 		if (strncmp(ksp->ks_name, name, len) != 0)
54557c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
54567c478bd9Sstevel@tonic-gate 
54577c478bd9Sstevel@tonic-gate 		(void) safe_kstat_read(kc, ksp, NULL);
54587c478bd9Sstevel@tonic-gate 
54597c478bd9Sstevel@tonic-gate 		alloc		= kstat_named_value(ksp, "alloc");
54607c478bd9Sstevel@tonic-gate 		alloc_fail	= kstat_named_value(ksp, "alloc_fail");
54617c478bd9Sstevel@tonic-gate 		buf_size	= kstat_named_value(ksp, "buf_size");
54627c478bd9Sstevel@tonic-gate 		buf_avail	= kstat_named_value(ksp, "buf_avail");
54637c478bd9Sstevel@tonic-gate 		buf_total	= kstat_named_value(ksp, "buf_total");
54647c478bd9Sstevel@tonic-gate 		buf_max		= kstat_named_value(ksp, "buf_max");
54657c478bd9Sstevel@tonic-gate 		buf_inuse	= buf_total - buf_avail;
54667c478bd9Sstevel@tonic-gate 
54677c478bd9Sstevel@tonic-gate 		if (Vflag && prefix) {
54687c478bd9Sstevel@tonic-gate 			(void) snprintf(buf, sizeof (buf), "%s%s", title,
54697c478bd9Sstevel@tonic-gate 			    ksp->ks_name + len);
54707c478bd9Sstevel@tonic-gate 			(void) printf("    %-18s %6u %9u %11u %11u\n",
54717c478bd9Sstevel@tonic-gate 			    buf, buf_inuse, buf_max, alloc, alloc_fail);
54727c478bd9Sstevel@tonic-gate 		}
54737c478bd9Sstevel@tonic-gate 
54747c478bd9Sstevel@tonic-gate 		total_alloc		+= alloc;
54757c478bd9Sstevel@tonic-gate 		total_alloc_fail	+= alloc_fail;
54767c478bd9Sstevel@tonic-gate 		total_buf_max		+= buf_max;
54777c478bd9Sstevel@tonic-gate 		total_buf_inuse		+= buf_inuse;
54787c478bd9Sstevel@tonic-gate 		*total_bytes		+= (int64_t)buf_inuse * buf_size;
54797c478bd9Sstevel@tonic-gate 	} /* 'for' loop 1 ends */
54807c478bd9Sstevel@tonic-gate 
54817c478bd9Sstevel@tonic-gate 	if (buf_size == 0) {
54827c478bd9Sstevel@tonic-gate 		(void) printf("%-22s [couldn't find statistics for %s]\n",
54837c478bd9Sstevel@tonic-gate 			title, name);
54847c478bd9Sstevel@tonic-gate 		return;
54857c478bd9Sstevel@tonic-gate 	}
54867c478bd9Sstevel@tonic-gate 
54877c478bd9Sstevel@tonic-gate 	if (Vflag && prefix)
54887c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (buf), "%s_total", title);
54897c478bd9Sstevel@tonic-gate 	else
54907c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (buf), "%s", title);
54917c478bd9Sstevel@tonic-gate 
54927c478bd9Sstevel@tonic-gate 	(void) printf("%-22s %6d %9d %11lld %11d\n", buf,
54937c478bd9Sstevel@tonic-gate 		total_buf_inuse, total_buf_max, total_alloc, total_alloc_fail);
54947c478bd9Sstevel@tonic-gate }
54957c478bd9Sstevel@tonic-gate 
54967c478bd9Sstevel@tonic-gate static void
54977c478bd9Sstevel@tonic-gate m_report(void)
54987c478bd9Sstevel@tonic-gate {
54997c478bd9Sstevel@tonic-gate 	int64_t total_bytes = 0;
55007c478bd9Sstevel@tonic-gate 
55017c478bd9Sstevel@tonic-gate 	(void) puts("streams allocation:");
55027c478bd9Sstevel@tonic-gate 	(void) printf("%63s\n", "cumulative  allocation");
55037c478bd9Sstevel@tonic-gate 	(void) printf("%63s\n",
55047c478bd9Sstevel@tonic-gate 	    "current   maximum       total    failures");
55057c478bd9Sstevel@tonic-gate 
55067c478bd9Sstevel@tonic-gate 	kmem_cache_stats("streams",
55077c478bd9Sstevel@tonic-gate 	    "stream_head_cache", 0, &total_bytes);
55087c478bd9Sstevel@tonic-gate 	kmem_cache_stats("queues", "queue_cache", 0, &total_bytes);
55097c478bd9Sstevel@tonic-gate 	kmem_cache_stats("mblk", "streams_mblk", 0, &total_bytes);
55107c478bd9Sstevel@tonic-gate 	kmem_cache_stats("dblk", "streams_dblk", 1, &total_bytes);
55117c478bd9Sstevel@tonic-gate 	kmem_cache_stats("linkblk", "linkinfo_cache", 0, &total_bytes);
55127c478bd9Sstevel@tonic-gate 	kmem_cache_stats("syncq", "syncq_cache", 0, &total_bytes);
55137c478bd9Sstevel@tonic-gate 	kmem_cache_stats("qband", "qband_cache", 0, &total_bytes);
55147c478bd9Sstevel@tonic-gate 
55157c478bd9Sstevel@tonic-gate 	(void) printf("\n%lld Kbytes allocated for streams data\n",
55167c478bd9Sstevel@tonic-gate 		total_bytes / 1024);
55177c478bd9Sstevel@tonic-gate 
55187c478bd9Sstevel@tonic-gate 	(void) putchar('\n');
55197c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
55207c478bd9Sstevel@tonic-gate }
55217c478bd9Sstevel@tonic-gate 
55227c478bd9Sstevel@tonic-gate /* --------------------------------- */
55237c478bd9Sstevel@tonic-gate 
55247c478bd9Sstevel@tonic-gate /*
55257c478bd9Sstevel@tonic-gate  * Print an IPv4 address. Remove the matching part of the domain name
55267c478bd9Sstevel@tonic-gate  * from the returned name.
55277c478bd9Sstevel@tonic-gate  */
55287c478bd9Sstevel@tonic-gate static char *
55297c478bd9Sstevel@tonic-gate pr_addr(uint_t addr, char *dst, uint_t dstlen)
55307c478bd9Sstevel@tonic-gate {
55317c478bd9Sstevel@tonic-gate 	char			*cp;
55327c478bd9Sstevel@tonic-gate 	struct hostent		*hp = NULL;
55337c478bd9Sstevel@tonic-gate 	static char		domain[MAXHOSTNAMELEN + 1];
55347c478bd9Sstevel@tonic-gate 	static boolean_t	first = B_TRUE;
55357c478bd9Sstevel@tonic-gate 	int			error_num;
55367c478bd9Sstevel@tonic-gate 
55377c478bd9Sstevel@tonic-gate 	if (first) {
55387c478bd9Sstevel@tonic-gate 		first = B_FALSE;
55397c478bd9Sstevel@tonic-gate 		if (sysinfo(SI_HOSTNAME, domain, MAXHOSTNAMELEN) != -1 &&
55407c478bd9Sstevel@tonic-gate 		    (cp = strchr(domain, '.'))) {
55417c478bd9Sstevel@tonic-gate 			(void) strncpy(domain, cp + 1, sizeof (domain));
55427c478bd9Sstevel@tonic-gate 		} else
55437c478bd9Sstevel@tonic-gate 			domain[0] = 0;
55447c478bd9Sstevel@tonic-gate 	}
55457c478bd9Sstevel@tonic-gate 	cp = NULL;
55467c478bd9Sstevel@tonic-gate 	if (!Nflag) {
55477c478bd9Sstevel@tonic-gate 		hp = getipnodebyaddr((char *)&addr, sizeof (uint_t), AF_INET,
55487c478bd9Sstevel@tonic-gate 		    &error_num);
55497c478bd9Sstevel@tonic-gate 		if (hp) {
55507c478bd9Sstevel@tonic-gate 			if ((cp = strchr(hp->h_name, '.')) != NULL &&
55517c478bd9Sstevel@tonic-gate 			    strcasecmp(cp + 1, domain) == 0)
55527c478bd9Sstevel@tonic-gate 				*cp = 0;
55537c478bd9Sstevel@tonic-gate 			cp = hp->h_name;
55547c478bd9Sstevel@tonic-gate 		}
55557c478bd9Sstevel@tonic-gate 	}
55567c478bd9Sstevel@tonic-gate 	if (cp != NULL) {
55577c478bd9Sstevel@tonic-gate 		(void) strncpy(dst, cp, dstlen);
55587c478bd9Sstevel@tonic-gate 		dst[dstlen - 1] = 0;
55597c478bd9Sstevel@tonic-gate 	} else {
55607c478bd9Sstevel@tonic-gate 		(void) inet_ntop(AF_INET, (char *)&addr, dst, dstlen);
55617c478bd9Sstevel@tonic-gate 	}
55627c478bd9Sstevel@tonic-gate 	if (hp != NULL)
55637c478bd9Sstevel@tonic-gate 		freehostent(hp);
55647c478bd9Sstevel@tonic-gate 	return (dst);
55657c478bd9Sstevel@tonic-gate }
55667c478bd9Sstevel@tonic-gate 
55677c478bd9Sstevel@tonic-gate /*
55687c478bd9Sstevel@tonic-gate  * Print a non-zero IPv4 address.  Print "    --" if the address is zero.
55697c478bd9Sstevel@tonic-gate  */
55707c478bd9Sstevel@tonic-gate static char *
55717c478bd9Sstevel@tonic-gate pr_addrnz(ipaddr_t addr, char *dst, uint_t dstlen)
55727c478bd9Sstevel@tonic-gate {
55737c478bd9Sstevel@tonic-gate 	if (addr == INADDR_ANY) {
55747c478bd9Sstevel@tonic-gate 		(void) strlcpy(dst, "    --", dstlen);
55757c478bd9Sstevel@tonic-gate 		return (dst);
55767c478bd9Sstevel@tonic-gate 	}
55777c478bd9Sstevel@tonic-gate 	return (pr_addr(addr, dst, dstlen));
55787c478bd9Sstevel@tonic-gate }
55797c478bd9Sstevel@tonic-gate 
55807c478bd9Sstevel@tonic-gate /*
55817c478bd9Sstevel@tonic-gate  * Print an IPv6 address. Remove the matching part of the domain name
55827c478bd9Sstevel@tonic-gate  * from the returned name.
55837c478bd9Sstevel@tonic-gate  */
55847c478bd9Sstevel@tonic-gate static char *
55857c478bd9Sstevel@tonic-gate pr_addr6(const struct in6_addr *addr, char *dst, uint_t dstlen)
55867c478bd9Sstevel@tonic-gate {
55877c478bd9Sstevel@tonic-gate 	char			*cp;
55887c478bd9Sstevel@tonic-gate 	struct hostent		*hp = NULL;
55897c478bd9Sstevel@tonic-gate 	static char		domain[MAXHOSTNAMELEN + 1];
55907c478bd9Sstevel@tonic-gate 	static boolean_t	first = B_TRUE;
55917c478bd9Sstevel@tonic-gate 	int			error_num;
55927c478bd9Sstevel@tonic-gate 
55937c478bd9Sstevel@tonic-gate 	if (first) {
55947c478bd9Sstevel@tonic-gate 		first = B_FALSE;
55957c478bd9Sstevel@tonic-gate 		if (sysinfo(SI_HOSTNAME, domain, MAXHOSTNAMELEN) != -1 &&
55967c478bd9Sstevel@tonic-gate 		    (cp = strchr(domain, '.'))) {
55977c478bd9Sstevel@tonic-gate 			(void) strncpy(domain, cp + 1, sizeof (domain));
55987c478bd9Sstevel@tonic-gate 		} else
55997c478bd9Sstevel@tonic-gate 			domain[0] = 0;
56007c478bd9Sstevel@tonic-gate 	}
56017c478bd9Sstevel@tonic-gate 	cp = NULL;
56027c478bd9Sstevel@tonic-gate 	if (!Nflag) {
56037c478bd9Sstevel@tonic-gate 		hp = getipnodebyaddr((char *)addr,
56047c478bd9Sstevel@tonic-gate 		    sizeof (struct in6_addr), AF_INET6, &error_num);
56057c478bd9Sstevel@tonic-gate 		if (hp) {
56067c478bd9Sstevel@tonic-gate 			if ((cp = strchr(hp->h_name, '.')) != NULL &&
56077c478bd9Sstevel@tonic-gate 			    strcasecmp(cp + 1, domain) == 0)
56087c478bd9Sstevel@tonic-gate 				*cp = 0;
56097c478bd9Sstevel@tonic-gate 			cp = hp->h_name;
56107c478bd9Sstevel@tonic-gate 		}
56117c478bd9Sstevel@tonic-gate 	}
56127c478bd9Sstevel@tonic-gate 	if (cp != NULL) {
56137c478bd9Sstevel@tonic-gate 		(void) strncpy(dst, cp, dstlen);
56147c478bd9Sstevel@tonic-gate 		dst[dstlen - 1] = 0;
56157c478bd9Sstevel@tonic-gate 	} else {
56167c478bd9Sstevel@tonic-gate 		(void) inet_ntop(AF_INET6, (void *)addr, dst, dstlen);
56177c478bd9Sstevel@tonic-gate 	}
56187c478bd9Sstevel@tonic-gate 	if (hp != NULL)
56197c478bd9Sstevel@tonic-gate 		freehostent(hp);
56207c478bd9Sstevel@tonic-gate 	return (dst);
56217c478bd9Sstevel@tonic-gate }
56227c478bd9Sstevel@tonic-gate 
56237c478bd9Sstevel@tonic-gate /* For IPv4 masks */
56247c478bd9Sstevel@tonic-gate static char *
56257c478bd9Sstevel@tonic-gate pr_mask(uint_t addr, char *dst, uint_t dstlen)
56267c478bd9Sstevel@tonic-gate {
56277c478bd9Sstevel@tonic-gate 	uint8_t	*ip_addr = (uint8_t *)&addr;
56287c478bd9Sstevel@tonic-gate 
56297c478bd9Sstevel@tonic-gate 	(void) snprintf(dst, dstlen, "%d.%d.%d.%d",
56307c478bd9Sstevel@tonic-gate 	    ip_addr[0], ip_addr[1], ip_addr[2], ip_addr[3]);
56317c478bd9Sstevel@tonic-gate 	return (dst);
56327c478bd9Sstevel@tonic-gate }
56337c478bd9Sstevel@tonic-gate 
56347c478bd9Sstevel@tonic-gate /*
56357c478bd9Sstevel@tonic-gate  * For ipv6 masks format is : dest/mask
56367c478bd9Sstevel@tonic-gate  * Does not print /128 to save space in printout. H flag carries this notion.
56377c478bd9Sstevel@tonic-gate  */
56387c478bd9Sstevel@tonic-gate static char *
5639*45916cd2Sjpk pr_prefix6(const struct in6_addr *addr, uint_t prefixlen, char *dst,
5640*45916cd2Sjpk     uint_t dstlen)
56417c478bd9Sstevel@tonic-gate {
56427c478bd9Sstevel@tonic-gate 	char *cp;
56437c478bd9Sstevel@tonic-gate 
56447c478bd9Sstevel@tonic-gate 	if (IN6_IS_ADDR_UNSPECIFIED(addr) && prefixlen == 0) {
56457c478bd9Sstevel@tonic-gate 		(void) strncpy(dst, "default", dstlen);
56467c478bd9Sstevel@tonic-gate 		dst[dstlen - 1] = 0;
56477c478bd9Sstevel@tonic-gate 		return (dst);
56487c478bd9Sstevel@tonic-gate 	}
56497c478bd9Sstevel@tonic-gate 
56507c478bd9Sstevel@tonic-gate 	(void) pr_addr6(addr, dst, dstlen);
56517c478bd9Sstevel@tonic-gate 	if (prefixlen != IPV6_ABITS) {
56527c478bd9Sstevel@tonic-gate 		/* How much room is left? */
56537c478bd9Sstevel@tonic-gate 		cp = strchr(dst, '\0');
56547c478bd9Sstevel@tonic-gate 		if (dst + dstlen > cp) {
56557c478bd9Sstevel@tonic-gate 			dstlen -= (cp - dst);
56567c478bd9Sstevel@tonic-gate 			(void) snprintf(cp, dstlen, "/%d", prefixlen);
56577c478bd9Sstevel@tonic-gate 		}
56587c478bd9Sstevel@tonic-gate 	}
56597c478bd9Sstevel@tonic-gate 	return (dst);
56607c478bd9Sstevel@tonic-gate }
56617c478bd9Sstevel@tonic-gate 
56627c478bd9Sstevel@tonic-gate /* Print IPv4 address and port */
56637c478bd9Sstevel@tonic-gate static char *
56647c478bd9Sstevel@tonic-gate pr_ap(uint_t addr, uint_t port, char *proto,
56657c478bd9Sstevel@tonic-gate     char *dst, uint_t dstlen)
56667c478bd9Sstevel@tonic-gate {
56677c478bd9Sstevel@tonic-gate 	char *cp;
56687c478bd9Sstevel@tonic-gate 
56697c478bd9Sstevel@tonic-gate 	if (addr == INADDR_ANY) {
56707c478bd9Sstevel@tonic-gate 		(void) strncpy(dst, "      *", dstlen);
56717c478bd9Sstevel@tonic-gate 		dst[dstlen - 1] = 0;
56727c478bd9Sstevel@tonic-gate 	} else {
56737c478bd9Sstevel@tonic-gate 		(void) pr_addr(addr, dst, dstlen);
56747c478bd9Sstevel@tonic-gate 	}
56757c478bd9Sstevel@tonic-gate 	/* How much room is left? */
56767c478bd9Sstevel@tonic-gate 	cp = strchr(dst, '\0');
56777c478bd9Sstevel@tonic-gate 	if (dst + dstlen > cp + 1) {
56787c478bd9Sstevel@tonic-gate 		*cp++ = '.';
56797c478bd9Sstevel@tonic-gate 		dstlen -= (cp - dst);
56807c478bd9Sstevel@tonic-gate 		dstlen--;
56817c478bd9Sstevel@tonic-gate 		(void) portname(port, proto, cp, dstlen);
56827c478bd9Sstevel@tonic-gate 	}
56837c478bd9Sstevel@tonic-gate 	return (dst);
56847c478bd9Sstevel@tonic-gate }
56857c478bd9Sstevel@tonic-gate 
56867c478bd9Sstevel@tonic-gate /* Print IPv6 address and port */
56877c478bd9Sstevel@tonic-gate static char *
56887c478bd9Sstevel@tonic-gate pr_ap6(const in6_addr_t *addr, uint_t port, char *proto,
56897c478bd9Sstevel@tonic-gate     char *dst, uint_t dstlen)
56907c478bd9Sstevel@tonic-gate {
56917c478bd9Sstevel@tonic-gate 	char *cp;
56927c478bd9Sstevel@tonic-gate 
56937c478bd9Sstevel@tonic-gate 	if (IN6_IS_ADDR_UNSPECIFIED(addr)) {
56947c478bd9Sstevel@tonic-gate 		(void) strncpy(dst, "      *", dstlen);
56957c478bd9Sstevel@tonic-gate 		dst[dstlen - 1] = 0;
56967c478bd9Sstevel@tonic-gate 	} else {
56977c478bd9Sstevel@tonic-gate 		(void) pr_addr6(addr, dst, dstlen);
56987c478bd9Sstevel@tonic-gate 	}
56997c478bd9Sstevel@tonic-gate 	/* How much room is left? */
57007c478bd9Sstevel@tonic-gate 	cp = strchr(dst, '\0');
57017c478bd9Sstevel@tonic-gate 	if (dst + dstlen + 1 > cp) {
57027c478bd9Sstevel@tonic-gate 		*cp++ = '.';
57037c478bd9Sstevel@tonic-gate 		dstlen -= (cp - dst);
57047c478bd9Sstevel@tonic-gate 		dstlen--;
57057c478bd9Sstevel@tonic-gate 		(void) portname(port, proto, cp, dstlen);
57067c478bd9Sstevel@tonic-gate 	}
57077c478bd9Sstevel@tonic-gate 	return (dst);
57087c478bd9Sstevel@tonic-gate }
57097c478bd9Sstevel@tonic-gate 
57107c478bd9Sstevel@tonic-gate /*
57117c478bd9Sstevel@tonic-gate  * Return the name of the network whose address is given. The address is
57127c478bd9Sstevel@tonic-gate  * assumed to be that of a net or subnet, not a host.
57137c478bd9Sstevel@tonic-gate  */
57147c478bd9Sstevel@tonic-gate static char *
57157c478bd9Sstevel@tonic-gate pr_net(uint_t addr, uint_t mask, char *dst, uint_t dstlen)
57167c478bd9Sstevel@tonic-gate {
57177c478bd9Sstevel@tonic-gate 	char		*cp = NULL;
57187c478bd9Sstevel@tonic-gate 	struct netent	*np = NULL;
57197c478bd9Sstevel@tonic-gate 	struct hostent	*hp = NULL;
57207c478bd9Sstevel@tonic-gate 	uint_t		net;
57217c478bd9Sstevel@tonic-gate 	int		subnetshift;
57227c478bd9Sstevel@tonic-gate 	int		error_num;
57237c478bd9Sstevel@tonic-gate 
57247c478bd9Sstevel@tonic-gate 	if (addr == INADDR_ANY && mask == INADDR_ANY) {
57257c478bd9Sstevel@tonic-gate 		(void) strncpy(dst, "default", dstlen);
57267c478bd9Sstevel@tonic-gate 		dst[dstlen - 1] = 0;
57277c478bd9Sstevel@tonic-gate 		return (dst);
57287c478bd9Sstevel@tonic-gate 	}
57297c478bd9Sstevel@tonic-gate 
57307c478bd9Sstevel@tonic-gate 	if (!Nflag && addr) {
57317c478bd9Sstevel@tonic-gate 		if (mask == 0) {
57327c478bd9Sstevel@tonic-gate 			if (IN_CLASSA(addr)) {
57337c478bd9Sstevel@tonic-gate 				mask = (uint_t)IN_CLASSA_NET;
57347c478bd9Sstevel@tonic-gate 				subnetshift = 8;
57357c478bd9Sstevel@tonic-gate 			} else if (IN_CLASSB(addr)) {
57367c478bd9Sstevel@tonic-gate 				mask = (uint_t)IN_CLASSB_NET;
57377c478bd9Sstevel@tonic-gate 				subnetshift = 8;
57387c478bd9Sstevel@tonic-gate 			} else {
57397c478bd9Sstevel@tonic-gate 				mask = (uint_t)IN_CLASSC_NET;
57407c478bd9Sstevel@tonic-gate 				subnetshift = 4;
57417c478bd9Sstevel@tonic-gate 			}
57427c478bd9Sstevel@tonic-gate 			/*
57437c478bd9Sstevel@tonic-gate 			 * If there are more bits than the standard mask
57447c478bd9Sstevel@tonic-gate 			 * would suggest, subnets must be in use. Guess at
57457c478bd9Sstevel@tonic-gate 			 * the subnet mask, assuming reasonable width subnet
57467c478bd9Sstevel@tonic-gate 			 * fields.
57477c478bd9Sstevel@tonic-gate 			 */
57487c478bd9Sstevel@tonic-gate 			while (addr & ~mask)
57497c478bd9Sstevel@tonic-gate 				/* compiler doesn't sign extend! */
57507c478bd9Sstevel@tonic-gate 				mask = (mask | ((int)mask >> subnetshift));
57517c478bd9Sstevel@tonic-gate 		}
57527c478bd9Sstevel@tonic-gate 		net = addr & mask;
57537c478bd9Sstevel@tonic-gate 		while ((mask & 1) == 0)
57547c478bd9Sstevel@tonic-gate 			mask >>= 1, net >>= 1;
57557c478bd9Sstevel@tonic-gate 		np = getnetbyaddr(net, AF_INET);
57567c478bd9Sstevel@tonic-gate 		if (np && np->n_net == net)
57577c478bd9Sstevel@tonic-gate 			cp = np->n_name;
57587c478bd9Sstevel@tonic-gate 		else {
57597c478bd9Sstevel@tonic-gate 			/*
57607c478bd9Sstevel@tonic-gate 			 * Look for subnets in hosts map.
57617c478bd9Sstevel@tonic-gate 			 */
57627c478bd9Sstevel@tonic-gate 			hp = getipnodebyaddr((char *)&addr, sizeof (uint_t),
57637c478bd9Sstevel@tonic-gate 			    AF_INET, &error_num);
57647c478bd9Sstevel@tonic-gate 			if (hp)
57657c478bd9Sstevel@tonic-gate 				cp = hp->h_name;
57667c478bd9Sstevel@tonic-gate 		}
57677c478bd9Sstevel@tonic-gate 	}
57687c478bd9Sstevel@tonic-gate 	if (cp != NULL) {
57697c478bd9Sstevel@tonic-gate 		(void) strncpy(dst, cp, dstlen);
57707c478bd9Sstevel@tonic-gate 		dst[dstlen - 1] = 0;
57717c478bd9Sstevel@tonic-gate 	} else {
57727c478bd9Sstevel@tonic-gate 		(void) inet_ntop(AF_INET, (char *)&addr, dst, dstlen);
57737c478bd9Sstevel@tonic-gate 	}
57747c478bd9Sstevel@tonic-gate 	if (hp != NULL)
57757c478bd9Sstevel@tonic-gate 		freehostent(hp);
57767c478bd9Sstevel@tonic-gate 	return (dst);
57777c478bd9Sstevel@tonic-gate }
57787c478bd9Sstevel@tonic-gate 
57797c478bd9Sstevel@tonic-gate /*
57807c478bd9Sstevel@tonic-gate  * Return the name of the network whose address is given.
57817c478bd9Sstevel@tonic-gate  * The address is assumed to be a host address.
57827c478bd9Sstevel@tonic-gate  */
57837c478bd9Sstevel@tonic-gate static char *
57847c478bd9Sstevel@tonic-gate pr_netaddr(uint_t addr, uint_t mask, char *dst, uint_t dstlen)
57857c478bd9Sstevel@tonic-gate {
57867c478bd9Sstevel@tonic-gate 	char		*cp = NULL;
57877c478bd9Sstevel@tonic-gate 	struct netent	*np = NULL;
57887c478bd9Sstevel@tonic-gate 	struct hostent	*hp = NULL;
57897c478bd9Sstevel@tonic-gate 	uint_t		net;
57907c478bd9Sstevel@tonic-gate 	uint_t		netshifted;
57917c478bd9Sstevel@tonic-gate 	int		subnetshift;
57927c478bd9Sstevel@tonic-gate 	struct in_addr in;
57937c478bd9Sstevel@tonic-gate 	int		error_num;
57947c478bd9Sstevel@tonic-gate 	uint_t		nbo_addr = addr;	/* network byte order */
57957c478bd9Sstevel@tonic-gate 
57967c478bd9Sstevel@tonic-gate 	addr = ntohl(addr);
57977c478bd9Sstevel@tonic-gate 	mask = ntohl(mask);
57987c478bd9Sstevel@tonic-gate 	if (addr == INADDR_ANY && mask == INADDR_ANY) {
57997c478bd9Sstevel@tonic-gate 		(void) strncpy(dst, "default", dstlen);
58007c478bd9Sstevel@tonic-gate 		dst[dstlen - 1] = 0;
58017c478bd9Sstevel@tonic-gate 		return (dst);
58027c478bd9Sstevel@tonic-gate 	}
58037c478bd9Sstevel@tonic-gate 
58047c478bd9Sstevel@tonic-gate 	/* Figure out network portion of address (with host portion = 0) */
58057c478bd9Sstevel@tonic-gate 	if (addr) {
58067c478bd9Sstevel@tonic-gate 		/* Try figuring out mask if unknown (all 0s). */
58077c478bd9Sstevel@tonic-gate 		if (mask == 0) {
58087c478bd9Sstevel@tonic-gate 			if (IN_CLASSA(addr)) {
58097c478bd9Sstevel@tonic-gate 				mask = (uint_t)IN_CLASSA_NET;
58107c478bd9Sstevel@tonic-gate 				subnetshift = 8;
58117c478bd9Sstevel@tonic-gate 			} else if (IN_CLASSB(addr)) {
58127c478bd9Sstevel@tonic-gate 				mask = (uint_t)IN_CLASSB_NET;
58137c478bd9Sstevel@tonic-gate 				subnetshift = 8;
58147c478bd9Sstevel@tonic-gate 			} else {
58157c478bd9Sstevel@tonic-gate 				mask = (uint_t)IN_CLASSC_NET;
58167c478bd9Sstevel@tonic-gate 				subnetshift = 4;
58177c478bd9Sstevel@tonic-gate 			}
58187c478bd9Sstevel@tonic-gate 			/*
58197c478bd9Sstevel@tonic-gate 			 * If there are more bits than the standard mask
58207c478bd9Sstevel@tonic-gate 			 * would suggest, subnets must be in use. Guess at
58217c478bd9Sstevel@tonic-gate 			 * the subnet mask, assuming reasonable width subnet
58227c478bd9Sstevel@tonic-gate 			 * fields.
58237c478bd9Sstevel@tonic-gate 			 */
58247c478bd9Sstevel@tonic-gate 			while (addr & ~mask)
58257c478bd9Sstevel@tonic-gate 				/* compiler doesn't sign extend! */
58267c478bd9Sstevel@tonic-gate 				mask = (mask | ((int)mask >> subnetshift));
58277c478bd9Sstevel@tonic-gate 		}
58287c478bd9Sstevel@tonic-gate 		net = netshifted = addr & mask;
58297c478bd9Sstevel@tonic-gate 		while ((mask & 1) == 0)
58307c478bd9Sstevel@tonic-gate 			mask >>= 1, netshifted >>= 1;
58317c478bd9Sstevel@tonic-gate 	}
58327c478bd9Sstevel@tonic-gate 	else
58337c478bd9Sstevel@tonic-gate 		net = netshifted = 0;
58347c478bd9Sstevel@tonic-gate 
58357c478bd9Sstevel@tonic-gate 	/* Try looking up name unless -n was specified. */
58367c478bd9Sstevel@tonic-gate 	if (!Nflag) {
58377c478bd9Sstevel@tonic-gate 		np = getnetbyaddr(netshifted, AF_INET);
58387c478bd9Sstevel@tonic-gate 		if (np && np->n_net == netshifted)
58397c478bd9Sstevel@tonic-gate 			cp = np->n_name;
58407c478bd9Sstevel@tonic-gate 		else {
58417c478bd9Sstevel@tonic-gate 			/*
58427c478bd9Sstevel@tonic-gate 			 * Look for subnets in hosts map.
58437c478bd9Sstevel@tonic-gate 			 */
58447c478bd9Sstevel@tonic-gate 			hp = getipnodebyaddr((char *)&nbo_addr, sizeof (uint_t),
58457c478bd9Sstevel@tonic-gate 			    AF_INET, &error_num);
58467c478bd9Sstevel@tonic-gate 			if (hp)
58477c478bd9Sstevel@tonic-gate 				cp = hp->h_name;
58487c478bd9Sstevel@tonic-gate 		}
58497c478bd9Sstevel@tonic-gate 
58507c478bd9Sstevel@tonic-gate 		if (cp != NULL) {
58517c478bd9Sstevel@tonic-gate 			(void) strncpy(dst, cp, dstlen);
58527c478bd9Sstevel@tonic-gate 			dst[dstlen - 1] = 0;
58537c478bd9Sstevel@tonic-gate 			if (hp != NULL)
58547c478bd9Sstevel@tonic-gate 				freehostent(hp);
58557c478bd9Sstevel@tonic-gate 			return (dst);
58567c478bd9Sstevel@tonic-gate 		}
58577c478bd9Sstevel@tonic-gate 		/*
58587c478bd9Sstevel@tonic-gate 		 * No name found for net: fallthru and return in decimal
58597c478bd9Sstevel@tonic-gate 		 * dot notation.
58607c478bd9Sstevel@tonic-gate 		 */
58617c478bd9Sstevel@tonic-gate 	}
58627c478bd9Sstevel@tonic-gate 
58637c478bd9Sstevel@tonic-gate 	in.s_addr = htonl(net);
58647c478bd9Sstevel@tonic-gate 	(void) inet_ntop(AF_INET, (char *)&in, dst, dstlen);
58657c478bd9Sstevel@tonic-gate 	if (hp != NULL)
58667c478bd9Sstevel@tonic-gate 		freehostent(hp);
58677c478bd9Sstevel@tonic-gate 	return (dst);
58687c478bd9Sstevel@tonic-gate }
58697c478bd9Sstevel@tonic-gate 
58707c478bd9Sstevel@tonic-gate 
58717c478bd9Sstevel@tonic-gate /*
58727c478bd9Sstevel@tonic-gate  * Return the standard IPv4 classess host or network identifier.
58737c478bd9Sstevel@tonic-gate  *
58747c478bd9Sstevel@tonic-gate  *	Returns "default" for the default route.
58757c478bd9Sstevel@tonic-gate  *	Returns "x.x.x.x" or host name if mask is 255.255.255.255.
58767c478bd9Sstevel@tonic-gate  *	Returns "x.x.x.x/y" (y is bit count) if mask is contiguous.
58777c478bd9Sstevel@tonic-gate  *	Otherwise, returns "x.x.x.x/m.m.m.m" (undesirable mask).
58787c478bd9Sstevel@tonic-gate  *
58797c478bd9Sstevel@tonic-gate  * Can also return "****" if inet_ntop fails -- insufficient dst space
58807c478bd9Sstevel@tonic-gate  * available.  (Shouldn't happen otherwise.)
58817c478bd9Sstevel@tonic-gate  */
58827c478bd9Sstevel@tonic-gate static char *
58837c478bd9Sstevel@tonic-gate pr_netclassless(ipaddr_t addr, ipaddr_t mask, char *dst, size_t dstlen)
58847c478bd9Sstevel@tonic-gate {
58857c478bd9Sstevel@tonic-gate 	struct hostent *hp;
58867c478bd9Sstevel@tonic-gate 	int error_num;
58877c478bd9Sstevel@tonic-gate 	struct in_addr in;
58887c478bd9Sstevel@tonic-gate 	char *cp;
58897c478bd9Sstevel@tonic-gate 	int slen;
58907c478bd9Sstevel@tonic-gate 
58917c478bd9Sstevel@tonic-gate 	if (addr == INADDR_ANY && mask == INADDR_ANY) {
58927c478bd9Sstevel@tonic-gate 		(void) strlcpy(dst, "default", dstlen);
58937c478bd9Sstevel@tonic-gate 		return (dst);
58947c478bd9Sstevel@tonic-gate 	}
58957c478bd9Sstevel@tonic-gate 	if (mask == IP_HOST_MASK && !Nflag &&
58967c478bd9Sstevel@tonic-gate 	    (hp = getipnodebyaddr(&addr, sizeof (addr), AF_INET,
58977c478bd9Sstevel@tonic-gate 		&error_num)) != NULL) {
58987c478bd9Sstevel@tonic-gate 		(void) strlcpy(dst, hp->h_name, dstlen);
58997c478bd9Sstevel@tonic-gate 		freehostent(hp);
59007c478bd9Sstevel@tonic-gate 		return (dst);
59017c478bd9Sstevel@tonic-gate 	}
59027c478bd9Sstevel@tonic-gate 	in.s_addr = addr;
59037c478bd9Sstevel@tonic-gate 	if (inet_ntop(AF_INET, &in, dst, dstlen) == NULL) {
59047c478bd9Sstevel@tonic-gate 		(void) strlcpy(dst, "****", dstlen);
59057c478bd9Sstevel@tonic-gate 		return (dst);
59067c478bd9Sstevel@tonic-gate 	}
59077c478bd9Sstevel@tonic-gate 	if (mask != IP_HOST_MASK) {
59087c478bd9Sstevel@tonic-gate 		slen = strlen(dst);
59097c478bd9Sstevel@tonic-gate 		cp = dst + slen;
59107c478bd9Sstevel@tonic-gate 		dstlen -= slen;
59117c478bd9Sstevel@tonic-gate 		if (mask == 0) {
59127c478bd9Sstevel@tonic-gate 			/* Illegal on non-zero addresses */
59137c478bd9Sstevel@tonic-gate 			(void) strlcpy(cp, "/0", dstlen);
59147c478bd9Sstevel@tonic-gate 		} else if ((mask | (mask - 1)) == IP_HOST_MASK) {
59157c478bd9Sstevel@tonic-gate 			(void) snprintf(cp, dstlen, "/%d",
59167c478bd9Sstevel@tonic-gate 			    IP_ABITS - ffs(mask) + 1);
59177c478bd9Sstevel@tonic-gate 		} else {
59187c478bd9Sstevel@tonic-gate 			/* Ungood; non-contiguous mask */
59197c478bd9Sstevel@tonic-gate 			(void) pr_mask(mask, cp, dstlen);
59207c478bd9Sstevel@tonic-gate 		}
59217c478bd9Sstevel@tonic-gate 	}
59227c478bd9Sstevel@tonic-gate 	return (dst);
59237c478bd9Sstevel@tonic-gate }
59247c478bd9Sstevel@tonic-gate 
59257c478bd9Sstevel@tonic-gate /*
59267c478bd9Sstevel@tonic-gate  * Return the filter mode as a string:
59277c478bd9Sstevel@tonic-gate  *	1 => "INCLUDE"
59287c478bd9Sstevel@tonic-gate  *	2 => "EXCLUDE"
59297c478bd9Sstevel@tonic-gate  *	otherwise "<unknown>"
59307c478bd9Sstevel@tonic-gate  */
59317c478bd9Sstevel@tonic-gate static char *
59327c478bd9Sstevel@tonic-gate fmodestr(uint_t fmode)
59337c478bd9Sstevel@tonic-gate {
59347c478bd9Sstevel@tonic-gate 	switch (fmode) {
59357c478bd9Sstevel@tonic-gate 	case 1:
59367c478bd9Sstevel@tonic-gate 		return ("INCLUDE");
59377c478bd9Sstevel@tonic-gate 	case 2:
59387c478bd9Sstevel@tonic-gate 		return ("EXCLUDE");
59397c478bd9Sstevel@tonic-gate 	default:
59407c478bd9Sstevel@tonic-gate 		return ("<unknown>");
59417c478bd9Sstevel@tonic-gate 	}
59427c478bd9Sstevel@tonic-gate }
59437c478bd9Sstevel@tonic-gate 
5944*45916cd2Sjpk #define	MAX_STRING_SIZE	256
5945*45916cd2Sjpk 
5946*45916cd2Sjpk static const char *
5947*45916cd2Sjpk pr_secattr(const sec_attr_list_t *attrs)
5948*45916cd2Sjpk {
5949*45916cd2Sjpk 	int i;
5950*45916cd2Sjpk 	char buf[MAX_STRING_SIZE + 1], *cp;
5951*45916cd2Sjpk 	static char *sbuf;
5952*45916cd2Sjpk 	static size_t sbuf_len;
5953*45916cd2Sjpk 	struct rtsa_s rtsa;
5954*45916cd2Sjpk 	const sec_attr_list_t *aptr;
5955*45916cd2Sjpk 
5956*45916cd2Sjpk 	if (!RSECflag || attrs == NULL)
5957*45916cd2Sjpk 		return ("");
5958*45916cd2Sjpk 
5959*45916cd2Sjpk 	for (aptr = attrs, i = 1; aptr != NULL; aptr = aptr->sal_next)
5960*45916cd2Sjpk 		i += MAX_STRING_SIZE;
5961*45916cd2Sjpk 	if (i > sbuf_len) {
5962*45916cd2Sjpk 		cp = realloc(sbuf, i);
5963*45916cd2Sjpk 		if (cp == NULL) {
5964*45916cd2Sjpk 			perror("realloc security attribute buffer");
5965*45916cd2Sjpk 			return ("");
5966*45916cd2Sjpk 		}
5967*45916cd2Sjpk 		sbuf_len = i;
5968*45916cd2Sjpk 		sbuf = cp;
5969*45916cd2Sjpk 	}
5970*45916cd2Sjpk 
5971*45916cd2Sjpk 	cp = sbuf;
5972*45916cd2Sjpk 	while (attrs != NULL) {
5973*45916cd2Sjpk 		const mib2_ipAttributeEntry_t *iae = attrs->sal_attr;
5974*45916cd2Sjpk 
5975*45916cd2Sjpk 		/* note: effectively hard-coded in rtsa_keyword */
5976*45916cd2Sjpk 		rtsa.rtsa_mask = RTSA_CIPSO | RTSA_SLRANGE | RTSA_DOI;
5977*45916cd2Sjpk 		rtsa.rtsa_slrange = iae->iae_slrange;
5978*45916cd2Sjpk 		rtsa.rtsa_doi = iae->iae_doi;
5979*45916cd2Sjpk 
5980*45916cd2Sjpk 		(void) snprintf(cp, MAX_STRING_SIZE,
5981*45916cd2Sjpk 		    "<%s>%s ", rtsa_to_str(&rtsa, buf, sizeof (buf)),
5982*45916cd2Sjpk 		    attrs->sal_next == NULL ? "" : ",");
5983*45916cd2Sjpk 		cp += strlen(cp);
5984*45916cd2Sjpk 		attrs = attrs->sal_next;
5985*45916cd2Sjpk 	}
5986*45916cd2Sjpk 	*cp = '\0';
5987*45916cd2Sjpk 
5988*45916cd2Sjpk 	return (sbuf);
5989*45916cd2Sjpk }
5990*45916cd2Sjpk 
59917c478bd9Sstevel@tonic-gate /*
59927c478bd9Sstevel@tonic-gate  * Pretty print a port number. If the Nflag was
59937c478bd9Sstevel@tonic-gate  * specified, use numbers instead of names.
59947c478bd9Sstevel@tonic-gate  */
59957c478bd9Sstevel@tonic-gate static char *
59967c478bd9Sstevel@tonic-gate portname(uint_t port, char *proto, char *dst, uint_t dstlen)
59977c478bd9Sstevel@tonic-gate {
59987c478bd9Sstevel@tonic-gate 	struct servent *sp = NULL;
59997c478bd9Sstevel@tonic-gate 
60007c478bd9Sstevel@tonic-gate 	if (!Nflag && port)
60017c478bd9Sstevel@tonic-gate 		sp = getservbyport(htons(port), proto);
60027c478bd9Sstevel@tonic-gate 	if (sp || port == 0)
60037c478bd9Sstevel@tonic-gate 		(void) snprintf(dst, dstlen, "%.*s", MAXHOSTNAMELEN,
60047c478bd9Sstevel@tonic-gate 				sp ? sp->s_name : "*");
60057c478bd9Sstevel@tonic-gate 	else
60067c478bd9Sstevel@tonic-gate 		(void) snprintf(dst, dstlen, "%d", port);
60077c478bd9Sstevel@tonic-gate 	dst[dstlen - 1] = 0;
60087c478bd9Sstevel@tonic-gate 	return (dst);
60097c478bd9Sstevel@tonic-gate }
60107c478bd9Sstevel@tonic-gate 
60117c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/
60127c478bd9Sstevel@tonic-gate void
60137c478bd9Sstevel@tonic-gate fail(int do_perror, char *message, ...)
60147c478bd9Sstevel@tonic-gate {
60157c478bd9Sstevel@tonic-gate 	va_list args;
60167c478bd9Sstevel@tonic-gate 
60177c478bd9Sstevel@tonic-gate 	va_start(args, message);
60187c478bd9Sstevel@tonic-gate 	(void) fputs("netstat: ", stderr);
60197c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, message, args);
60207c478bd9Sstevel@tonic-gate 	va_end(args);
60217c478bd9Sstevel@tonic-gate 	if (do_perror)
60227c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, ": %s", strerror(errno));
60237c478bd9Sstevel@tonic-gate 	(void) fputc('\n', stderr);
60247c478bd9Sstevel@tonic-gate 	exit(2);
60257c478bd9Sstevel@tonic-gate }
60267c478bd9Sstevel@tonic-gate 
60277c478bd9Sstevel@tonic-gate /*
60287c478bd9Sstevel@tonic-gate  * Return value of named statistic for given kstat_named kstat;
60297c478bd9Sstevel@tonic-gate  * return 0LL if named statistic is not in list (use "ll" as a
60307c478bd9Sstevel@tonic-gate  * type qualifier when printing 64-bit int's with printf() )
60317c478bd9Sstevel@tonic-gate  */
60327c478bd9Sstevel@tonic-gate static uint64_t
60337c478bd9Sstevel@tonic-gate kstat_named_value(kstat_t *ksp, char *name)
60347c478bd9Sstevel@tonic-gate {
60357c478bd9Sstevel@tonic-gate 	kstat_named_t *knp;
60367c478bd9Sstevel@tonic-gate 	uint64_t value;
60377c478bd9Sstevel@tonic-gate 
60387c478bd9Sstevel@tonic-gate 	if (ksp == NULL)
60397c478bd9Sstevel@tonic-gate 		return (0LL);
60407c478bd9Sstevel@tonic-gate 
60417c478bd9Sstevel@tonic-gate 	knp = kstat_data_lookup(ksp, name);
60427c478bd9Sstevel@tonic-gate 	if (knp == NULL)
60437c478bd9Sstevel@tonic-gate 		return (0LL);
60447c478bd9Sstevel@tonic-gate 
60457c478bd9Sstevel@tonic-gate 	switch (knp->data_type) {
60467c478bd9Sstevel@tonic-gate 	case KSTAT_DATA_INT32:
60477c478bd9Sstevel@tonic-gate 	case KSTAT_DATA_UINT32:
60487c478bd9Sstevel@tonic-gate 		value = (uint64_t)(knp->value.ui32);
60497c478bd9Sstevel@tonic-gate 		break;
60507c478bd9Sstevel@tonic-gate 	case KSTAT_DATA_INT64:
60517c478bd9Sstevel@tonic-gate 	case KSTAT_DATA_UINT64:
60527c478bd9Sstevel@tonic-gate 		value = knp->value.ui64;
60537c478bd9Sstevel@tonic-gate 		break;
60547c478bd9Sstevel@tonic-gate 	default:
60557c478bd9Sstevel@tonic-gate 		value = 0LL;
60567c478bd9Sstevel@tonic-gate 		break;
60577c478bd9Sstevel@tonic-gate 	}
60587c478bd9Sstevel@tonic-gate 
60597c478bd9Sstevel@tonic-gate 	return (value);
60607c478bd9Sstevel@tonic-gate }
60617c478bd9Sstevel@tonic-gate 
60627c478bd9Sstevel@tonic-gate kid_t
60637c478bd9Sstevel@tonic-gate safe_kstat_read(kstat_ctl_t *kc, kstat_t *ksp, void *data)
60647c478bd9Sstevel@tonic-gate {
60657c478bd9Sstevel@tonic-gate 	kid_t kstat_chain_id = kstat_read(kc, ksp, data);
60667c478bd9Sstevel@tonic-gate 
60677c478bd9Sstevel@tonic-gate 	if (kstat_chain_id == -1)
60687c478bd9Sstevel@tonic-gate 		fail(1, "kstat_read(%p, '%s') failed", (void *)kc,
60697c478bd9Sstevel@tonic-gate 		    ksp->ks_name);
60707c478bd9Sstevel@tonic-gate 	return (kstat_chain_id);
60717c478bd9Sstevel@tonic-gate }
60727c478bd9Sstevel@tonic-gate 
60737c478bd9Sstevel@tonic-gate /*
60747c478bd9Sstevel@tonic-gate  * Parse a list of IRE flag characters into a bit field.
60757c478bd9Sstevel@tonic-gate  */
60767c478bd9Sstevel@tonic-gate static uint_t
60777c478bd9Sstevel@tonic-gate flag_bits(const char *arg)
60787c478bd9Sstevel@tonic-gate {
60797c478bd9Sstevel@tonic-gate 	const char *cp;
60807c478bd9Sstevel@tonic-gate 	uint_t val;
60817c478bd9Sstevel@tonic-gate 
60827c478bd9Sstevel@tonic-gate 	if (*arg == '\0')
60837c478bd9Sstevel@tonic-gate 		fatal(1, "missing flag list\n");
60847c478bd9Sstevel@tonic-gate 
60857c478bd9Sstevel@tonic-gate 	val = 0;
60867c478bd9Sstevel@tonic-gate 	while (*arg != '\0') {
60877c478bd9Sstevel@tonic-gate 		if ((cp = strchr(flag_list, *arg)) == NULL)
60887c478bd9Sstevel@tonic-gate 			fatal(1, "%c: illegal flag\n", *arg);
60897c478bd9Sstevel@tonic-gate 		val |= 1 << (cp - flag_list);
60907c478bd9Sstevel@tonic-gate 		arg++;
60917c478bd9Sstevel@tonic-gate 	}
60927c478bd9Sstevel@tonic-gate 	return (val);
60937c478bd9Sstevel@tonic-gate }
60947c478bd9Sstevel@tonic-gate 
60957c478bd9Sstevel@tonic-gate /*
60967c478bd9Sstevel@tonic-gate  * Handle -f argument.  Validate input format, sort by keyword, and
60977c478bd9Sstevel@tonic-gate  * save off digested results.
60987c478bd9Sstevel@tonic-gate  */
60997c478bd9Sstevel@tonic-gate static void
61007c478bd9Sstevel@tonic-gate process_filter(char *arg)
61017c478bd9Sstevel@tonic-gate {
61027c478bd9Sstevel@tonic-gate 	int idx;
61037c478bd9Sstevel@tonic-gate 	int klen = 0;
61047c478bd9Sstevel@tonic-gate 	char *cp, *cp2;
61057c478bd9Sstevel@tonic-gate 	int val;
61067c478bd9Sstevel@tonic-gate 	filter_t *newf;
61077c478bd9Sstevel@tonic-gate 	struct hostent *hp;
61087c478bd9Sstevel@tonic-gate 	int error_num;
61097c478bd9Sstevel@tonic-gate 	uint8_t *ucp;
61107c478bd9Sstevel@tonic-gate 	int maxv;
61117c478bd9Sstevel@tonic-gate 
61127c478bd9Sstevel@tonic-gate 	/* Look up the keyword first */
61137c478bd9Sstevel@tonic-gate 	if (strchr(arg, ':') == NULL) {
61147c478bd9Sstevel@tonic-gate 		idx = FK_AF;
61157c478bd9Sstevel@tonic-gate 	} else {
61167c478bd9Sstevel@tonic-gate 		for (idx = 0; idx < NFILTERKEYS; idx++) {
61177c478bd9Sstevel@tonic-gate 			klen = strlen(filter_keys[idx]);
61187c478bd9Sstevel@tonic-gate 			if (strncmp(filter_keys[idx], arg, klen) == 0 &&
61197c478bd9Sstevel@tonic-gate 			    arg[klen] == ':')
61207c478bd9Sstevel@tonic-gate 				break;
61217c478bd9Sstevel@tonic-gate 		}
61227c478bd9Sstevel@tonic-gate 		if (idx >= NFILTERKEYS)
61237c478bd9Sstevel@tonic-gate 			fatal(1, "%s: unknown filter keyword\n", arg);
61247c478bd9Sstevel@tonic-gate 
61257c478bd9Sstevel@tonic-gate 		/* Advance past keyword and separator. */
61267c478bd9Sstevel@tonic-gate 		arg += klen + 1;
61277c478bd9Sstevel@tonic-gate 	}
61287c478bd9Sstevel@tonic-gate 
61297c478bd9Sstevel@tonic-gate 	if ((newf = malloc(sizeof (*newf))) == NULL) {
61307c478bd9Sstevel@tonic-gate 		perror("filter");
61317c478bd9Sstevel@tonic-gate 		exit(1);
61327c478bd9Sstevel@tonic-gate 	}
61337c478bd9Sstevel@tonic-gate 	switch (idx) {
61347c478bd9Sstevel@tonic-gate 	case FK_AF:
61357c478bd9Sstevel@tonic-gate 		if (strcmp(arg, "inet") == 0) {
61367c478bd9Sstevel@tonic-gate 			newf->u.f_family = AF_INET;
61377c478bd9Sstevel@tonic-gate 		} else if (strcmp(arg, "inet6") == 0) {
61387c478bd9Sstevel@tonic-gate 			newf->u.f_family = AF_INET6;
61397c478bd9Sstevel@tonic-gate 		} else if (strcmp(arg, "unix") == 0) {
61407c478bd9Sstevel@tonic-gate 			newf->u.f_family = AF_UNIX;
61417c478bd9Sstevel@tonic-gate 		} else {
61427c478bd9Sstevel@tonic-gate 			newf->u.f_family = strtol(arg, &cp, 0);
61437c478bd9Sstevel@tonic-gate 			if (arg == cp || *cp != '\0')
61447c478bd9Sstevel@tonic-gate 				fatal(1, "%s: unknown address family.\n", arg);
61457c478bd9Sstevel@tonic-gate 		}
61467c478bd9Sstevel@tonic-gate 		break;
61477c478bd9Sstevel@tonic-gate 
61487c478bd9Sstevel@tonic-gate 	case FK_INIF:
61497c478bd9Sstevel@tonic-gate 	case FK_OUTIF:
61507c478bd9Sstevel@tonic-gate 		if (strcmp(arg, "none") == 0) {
61517c478bd9Sstevel@tonic-gate 			newf->u.f_ifname = NULL;
61527c478bd9Sstevel@tonic-gate 			break;
61537c478bd9Sstevel@tonic-gate 		}
61547c478bd9Sstevel@tonic-gate 		if (strcmp(arg, "any") == 0) {
61557c478bd9Sstevel@tonic-gate 			newf->u.f_ifname = "";
61567c478bd9Sstevel@tonic-gate 			break;
61577c478bd9Sstevel@tonic-gate 		}
61587c478bd9Sstevel@tonic-gate 		val = strtol(arg, &cp, 0);
61597c478bd9Sstevel@tonic-gate 		if (val <= 0 || arg == cp || cp[0] != '\0') {
61607c478bd9Sstevel@tonic-gate 			if ((val = if_nametoindex(arg)) == 0) {
61617c478bd9Sstevel@tonic-gate 				perror(arg);
61627c478bd9Sstevel@tonic-gate 				exit(1);
61637c478bd9Sstevel@tonic-gate 			}
61647c478bd9Sstevel@tonic-gate 		}
61657c478bd9Sstevel@tonic-gate 		newf->u.f_ifname = arg;
61667c478bd9Sstevel@tonic-gate 		break;
61677c478bd9Sstevel@tonic-gate 
61687c478bd9Sstevel@tonic-gate 	case FK_SRC:
61697c478bd9Sstevel@tonic-gate 	case FK_DST:
61707c478bd9Sstevel@tonic-gate 		V4MASK_TO_V6(IP_HOST_MASK, newf->u.a.f_mask);
61717c478bd9Sstevel@tonic-gate 		if (strcmp(arg, "any") == 0) {
61727c478bd9Sstevel@tonic-gate 			/* Special semantics; any address *but* zero */
61737c478bd9Sstevel@tonic-gate 			newf->u.a.f_address = NULL;
61747c478bd9Sstevel@tonic-gate 			(void) memset(&newf->u.a.f_mask, 0,
61757c478bd9Sstevel@tonic-gate 			    sizeof (newf->u.a.f_mask));
61767c478bd9Sstevel@tonic-gate 			break;
61777c478bd9Sstevel@tonic-gate 		}
61787c478bd9Sstevel@tonic-gate 		if (strcmp(arg, "none") == 0) {
61797c478bd9Sstevel@tonic-gate 			newf->u.a.f_address = NULL;
61807c478bd9Sstevel@tonic-gate 			break;
61817c478bd9Sstevel@tonic-gate 		}
61827c478bd9Sstevel@tonic-gate 		if ((cp = strrchr(arg, '/')) != NULL)
61837c478bd9Sstevel@tonic-gate 			*cp++ = '\0';
61847c478bd9Sstevel@tonic-gate 		hp = getipnodebyname(arg, AF_INET6, AI_V4MAPPED|AI_ALL,
61857c478bd9Sstevel@tonic-gate 		    &error_num);
61867c478bd9Sstevel@tonic-gate 		if (hp == NULL)
61877c478bd9Sstevel@tonic-gate 			fatal(1, "%s: invalid or unknown host address\n", arg);
61887c478bd9Sstevel@tonic-gate 		newf->u.a.f_address = hp;
61897c478bd9Sstevel@tonic-gate 		if (cp == NULL) {
61907c478bd9Sstevel@tonic-gate 			V4MASK_TO_V6(IP_HOST_MASK, newf->u.a.f_mask);
61917c478bd9Sstevel@tonic-gate 		} else {
61927c478bd9Sstevel@tonic-gate 			val = strtol(cp, &cp2, 0);
61937c478bd9Sstevel@tonic-gate 			if (cp != cp2 && cp2[0] == '\0') {
61947c478bd9Sstevel@tonic-gate 				/*
61957c478bd9Sstevel@tonic-gate 				 * If decode as "/n" works, then translate
61967c478bd9Sstevel@tonic-gate 				 * into a mask.
61977c478bd9Sstevel@tonic-gate 				 */
61987c478bd9Sstevel@tonic-gate 				if (hp->h_addr_list[0] != NULL &&
61997c478bd9Sstevel@tonic-gate 				    /* LINTED: (note 1) */
62007c478bd9Sstevel@tonic-gate 				    IN6_IS_ADDR_V4MAPPED((in6_addr_t
62017c478bd9Sstevel@tonic-gate 					*)hp->h_addr_list[0])) {
62027c478bd9Sstevel@tonic-gate 					maxv = IP_ABITS;
62037c478bd9Sstevel@tonic-gate 				} else {
62047c478bd9Sstevel@tonic-gate 					maxv = IPV6_ABITS;
62057c478bd9Sstevel@tonic-gate 				}
62067c478bd9Sstevel@tonic-gate 				if (val < 0 || val >= maxv)
62077c478bd9Sstevel@tonic-gate 					fatal(1, "%d: not in range 0 to %d\n",
62087c478bd9Sstevel@tonic-gate 					    val, maxv - 1);
62097c478bd9Sstevel@tonic-gate 				if (maxv == IP_ABITS)
62107c478bd9Sstevel@tonic-gate 					val += IPV6_ABITS - IP_ABITS;
62117c478bd9Sstevel@tonic-gate 				ucp = newf->u.a.f_mask.s6_addr;
62127c478bd9Sstevel@tonic-gate 				while (val >= 8)
62137c478bd9Sstevel@tonic-gate 					*ucp++ = 0xff, val -= 8;
62147c478bd9Sstevel@tonic-gate 				*ucp++ = (0xff << (8 - val)) & 0xff;
62157c478bd9Sstevel@tonic-gate 				while (ucp < newf->u.a.f_mask.s6_addr +
62167c478bd9Sstevel@tonic-gate 				    sizeof (newf->u.a.f_mask.s6_addr))
62177c478bd9Sstevel@tonic-gate 					*ucp++ = 0;
62187c478bd9Sstevel@tonic-gate 				/* Otherwise, try as numeric address */
62197c478bd9Sstevel@tonic-gate 			} else if (inet_pton(AF_INET6,
62207c478bd9Sstevel@tonic-gate 			    cp, &newf->u.a.f_mask) <= 0) {
62217c478bd9Sstevel@tonic-gate 				fatal(1, "%s: illegal mask format\n", cp);
62227c478bd9Sstevel@tonic-gate 			}
62237c478bd9Sstevel@tonic-gate 		}
62247c478bd9Sstevel@tonic-gate 		break;
62257c478bd9Sstevel@tonic-gate 
62267c478bd9Sstevel@tonic-gate 	case FK_FLAGS:
62277c478bd9Sstevel@tonic-gate 		if (*arg == '+') {
62287c478bd9Sstevel@tonic-gate 			newf->u.f.f_flagset = flag_bits(arg + 1);
62297c478bd9Sstevel@tonic-gate 			newf->u.f.f_flagclear = 0;
62307c478bd9Sstevel@tonic-gate 		} else if (*arg == '-') {
62317c478bd9Sstevel@tonic-gate 			newf->u.f.f_flagset = 0;
62327c478bd9Sstevel@tonic-gate 			newf->u.f.f_flagclear = flag_bits(arg + 1);
62337c478bd9Sstevel@tonic-gate 		} else {
62347c478bd9Sstevel@tonic-gate 			newf->u.f.f_flagset = flag_bits(arg);
62357c478bd9Sstevel@tonic-gate 			newf->u.f.f_flagclear = ~newf->u.f.f_flagset;
62367c478bd9Sstevel@tonic-gate 		}
62377c478bd9Sstevel@tonic-gate 		break;
62387c478bd9Sstevel@tonic-gate 
62397c478bd9Sstevel@tonic-gate 	default:
62407c478bd9Sstevel@tonic-gate 		assert(0);
62417c478bd9Sstevel@tonic-gate 	}
62427c478bd9Sstevel@tonic-gate 	newf->f_next = filters[idx];
62437c478bd9Sstevel@tonic-gate 	filters[idx] = newf;
62447c478bd9Sstevel@tonic-gate }
62457c478bd9Sstevel@tonic-gate 
62467c478bd9Sstevel@tonic-gate /* Determine if user wants this address family printed. */
62477c478bd9Sstevel@tonic-gate static boolean_t
62487c478bd9Sstevel@tonic-gate family_selected(int family)
62497c478bd9Sstevel@tonic-gate {
62507c478bd9Sstevel@tonic-gate 	const filter_t *fp;
62517c478bd9Sstevel@tonic-gate 
62527c478bd9Sstevel@tonic-gate 	if (v4compat && family == AF_INET6)
62537c478bd9Sstevel@tonic-gate 		return (B_FALSE);
62547c478bd9Sstevel@tonic-gate 	if ((fp = filters[FK_AF]) == NULL)
62557c478bd9Sstevel@tonic-gate 		return (B_TRUE);
62567c478bd9Sstevel@tonic-gate 	while (fp != NULL) {
62577c478bd9Sstevel@tonic-gate 		if (fp->u.f_family == family)
62587c478bd9Sstevel@tonic-gate 			return (B_TRUE);
62597c478bd9Sstevel@tonic-gate 		fp = fp->f_next;
62607c478bd9Sstevel@tonic-gate 	}
62617c478bd9Sstevel@tonic-gate 	return (B_FALSE);
62627c478bd9Sstevel@tonic-gate }
62637c478bd9Sstevel@tonic-gate 
62647c478bd9Sstevel@tonic-gate /*
62657c478bd9Sstevel@tonic-gate  * print the usage line
62667c478bd9Sstevel@tonic-gate  */
62677c478bd9Sstevel@tonic-gate static void
62687c478bd9Sstevel@tonic-gate usage(char *cmdname)
62697c478bd9Sstevel@tonic-gate {
62707c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "usage: %s [-anv] [-f address_family]\n",
62717c478bd9Sstevel@tonic-gate 	    cmdname);
62727c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "       %s [-n] [-f address_family] "
62737c478bd9Sstevel@tonic-gate 	    "[-P protocol] [-g | -p | -s [interval [count]]]\n", cmdname);
62747c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "       %s -m [-v] "
62757c478bd9Sstevel@tonic-gate 	    "[interval [count]]\n", cmdname);
62767c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "       %s -i [-I interface] [-an] "
62777c478bd9Sstevel@tonic-gate 	    "[-f address_family] [interval [count]]\n", cmdname);
62787c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "       %s -r [-anv] "
62797c478bd9Sstevel@tonic-gate 	    "[-f address_family|filter]\n", cmdname);
62807c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "       %s -M [-ns] [-f address_family]\n",
62817c478bd9Sstevel@tonic-gate 	    cmdname);
62827c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "       %s -D [-I interface] "
62837c478bd9Sstevel@tonic-gate 	    "[-f address_family]\n", cmdname);
62847c478bd9Sstevel@tonic-gate 	exit(EXIT_FAILURE);
62857c478bd9Sstevel@tonic-gate }
62867c478bd9Sstevel@tonic-gate 
62877c478bd9Sstevel@tonic-gate /*
62887c478bd9Sstevel@tonic-gate  * fatal: print error message to stderr and
62897c478bd9Sstevel@tonic-gate  * call exit(errcode)
62907c478bd9Sstevel@tonic-gate  */
62917c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/
62927c478bd9Sstevel@tonic-gate static void
6293*45916cd2Sjpk fatal(int errcode, char *format, ...)
6294*45916cd2Sjpk {
62957c478bd9Sstevel@tonic-gate 	va_list argp;
62967c478bd9Sstevel@tonic-gate 
62977c478bd9Sstevel@tonic-gate 	if (format == NULL)
62987c478bd9Sstevel@tonic-gate 		return;
62997c478bd9Sstevel@tonic-gate 
63007c478bd9Sstevel@tonic-gate 	va_start(argp, format);
63017c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, format, argp);
63027c478bd9Sstevel@tonic-gate 	va_end(argp);
63037c478bd9Sstevel@tonic-gate 
63047c478bd9Sstevel@tonic-gate 	exit(errcode);
63057c478bd9Sstevel@tonic-gate }
6306