10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51676Sjpk  * Common Development and Distribution License (the "License").
61676Sjpk  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*3431Scarlsonj  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  * Copyright (c) 1990  Mentat Inc.
280Sstevel@tonic-gate  * netstat.c 2.2, last change 9/9/91
290Sstevel@tonic-gate  * MROUTING Revision 3.5
300Sstevel@tonic-gate  */
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
330Sstevel@tonic-gate 
340Sstevel@tonic-gate /*
350Sstevel@tonic-gate  * simple netstat based on snmp/mib-2 interface to the TCP/IP stack
360Sstevel@tonic-gate  *
370Sstevel@tonic-gate  * NOTES:
380Sstevel@tonic-gate  * 1. A comment "LINTED: (note 1)" appears before certain lines where
390Sstevel@tonic-gate  *    lint would have complained, "pointer cast may result in improper
400Sstevel@tonic-gate  *    alignment". These are lines where lint had suspected potential
410Sstevel@tonic-gate  *    improper alignment of a data structure; in each such situation
420Sstevel@tonic-gate  *    we have relied on the kernel guaranteeing proper alignment.
430Sstevel@tonic-gate  * 2. Some 'for' loops have been commented as "'for' loop 1", etc
440Sstevel@tonic-gate  *    because they have 'continue' or 'break' statements in their
450Sstevel@tonic-gate  *    bodies. 'continue' statements have been used inside some loops
460Sstevel@tonic-gate  *    where avoiding them would have led to deep levels of indentation.
470Sstevel@tonic-gate  *
480Sstevel@tonic-gate  * TODO:
490Sstevel@tonic-gate  *	Add ability to request subsets from kernel (with level = MIB2_IP;
500Sstevel@tonic-gate  *	name = 0 meaning everything for compatibility)
510Sstevel@tonic-gate  */
520Sstevel@tonic-gate 
530Sstevel@tonic-gate #include <stdio.h>
540Sstevel@tonic-gate #include <stdlib.h>
550Sstevel@tonic-gate #include <stdarg.h>
560Sstevel@tonic-gate #include <unistd.h>
570Sstevel@tonic-gate #include <strings.h>
580Sstevel@tonic-gate #include <string.h>
590Sstevel@tonic-gate #include <errno.h>
600Sstevel@tonic-gate #include <ctype.h>
610Sstevel@tonic-gate #include <kstat.h>
620Sstevel@tonic-gate #include <assert.h>
630Sstevel@tonic-gate 
640Sstevel@tonic-gate #include <sys/types.h>
650Sstevel@tonic-gate #include <sys/stream.h>
660Sstevel@tonic-gate #include <stropts.h>
670Sstevel@tonic-gate #include <sys/strstat.h>
680Sstevel@tonic-gate #include <sys/tihdr.h>
690Sstevel@tonic-gate 
700Sstevel@tonic-gate #include <sys/socket.h>
710Sstevel@tonic-gate #include <sys/sockio.h>
720Sstevel@tonic-gate #include <netinet/in.h>
730Sstevel@tonic-gate #include <net/if.h>
740Sstevel@tonic-gate #include <net/route.h>
750Sstevel@tonic-gate 
760Sstevel@tonic-gate #include <inet/mib2.h>
770Sstevel@tonic-gate #include <inet/ip.h>
780Sstevel@tonic-gate #include <inet/arp.h>
790Sstevel@tonic-gate #include <inet/tcp.h>
800Sstevel@tonic-gate #include <netinet/igmp_var.h>
810Sstevel@tonic-gate #include <netinet/ip_mroute.h>
820Sstevel@tonic-gate 
830Sstevel@tonic-gate #include <arpa/inet.h>
840Sstevel@tonic-gate #include <netdb.h>
850Sstevel@tonic-gate #include <fcntl.h>
860Sstevel@tonic-gate #include <sys/systeminfo.h>
870Sstevel@tonic-gate #include <arpa/inet.h>
880Sstevel@tonic-gate 
890Sstevel@tonic-gate #include <netinet/dhcp.h>
900Sstevel@tonic-gate #include <dhcpagent_ipc.h>
910Sstevel@tonic-gate #include <dhcpagent_util.h>
920Sstevel@tonic-gate #include <compat.h>
930Sstevel@tonic-gate 
941676Sjpk #include <libtsnet.h>
951676Sjpk #include <tsol/label.h>
961676Sjpk 
970Sstevel@tonic-gate extern void	unixpr(kstat_ctl_t *kc);
980Sstevel@tonic-gate 
990Sstevel@tonic-gate #define	STR_EXPAND	4
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate #define	V4MASK_TO_V6(v4, v6)	((v6)._S6_un._S6_u32[0] = 0xfffffffful, \
1020Sstevel@tonic-gate 				(v6)._S6_un._S6_u32[1] = 0xfffffffful, \
1030Sstevel@tonic-gate 				(v6)._S6_un._S6_u32[2] = 0xfffffffful, \
1040Sstevel@tonic-gate 				(v6)._S6_un._S6_u32[3] = (v4))
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate #define	IN6_IS_V4MASK(v6)	((v6)._S6_un._S6_u32[0] == 0xfffffffful && \
1070Sstevel@tonic-gate 				(v6)._S6_un._S6_u32[1] == 0xfffffffful && \
1080Sstevel@tonic-gate 				(v6)._S6_un._S6_u32[2] == 0xfffffffful)
1090Sstevel@tonic-gate 
110*3431Scarlsonj /*
111*3431Scarlsonj  * This is used as a cushion in the buffer allocation directed by SIOCGLIFNUM.
112*3431Scarlsonj  * Because there's no locking between SIOCGLIFNUM and SIOCGLIFCONF, it's
113*3431Scarlsonj  * possible for an administrator to plumb new interfaces between those two
114*3431Scarlsonj  * calls, resulting in the failure of the latter.  This addition makes that
115*3431Scarlsonj  * less likely.
116*3431Scarlsonj  */
117*3431Scarlsonj #define	LIFN_GUARD_VALUE	10
118*3431Scarlsonj 
1190Sstevel@tonic-gate typedef struct mib_item_s {
1200Sstevel@tonic-gate 	struct mib_item_s	*next_item;
1210Sstevel@tonic-gate 	int			group;
1220Sstevel@tonic-gate 	int			mib_id;
1230Sstevel@tonic-gate 	int			length;
1240Sstevel@tonic-gate 	void			*valp;
1250Sstevel@tonic-gate } mib_item_t;
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate struct	ifstat {
1280Sstevel@tonic-gate 	uint64_t	ipackets;
1290Sstevel@tonic-gate 	uint64_t	ierrors;
1300Sstevel@tonic-gate 	uint64_t	opackets;
1310Sstevel@tonic-gate 	uint64_t	oerrors;
1320Sstevel@tonic-gate 	uint64_t	collisions;
1330Sstevel@tonic-gate };
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate struct iflist {
1360Sstevel@tonic-gate 	struct iflist	*next_if;
1370Sstevel@tonic-gate 	char		ifname[LIFNAMSIZ];
1380Sstevel@tonic-gate 	struct ifstat	tot;
1390Sstevel@tonic-gate };
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate static	mib_item_t	*mibget(int sd);
1420Sstevel@tonic-gate static	void		mibfree(mib_item_t *firstitem);
1430Sstevel@tonic-gate static	int		mibopen(void);
1440Sstevel@tonic-gate static void		mib_get_constants(mib_item_t *item);
1450Sstevel@tonic-gate static mib_item_t	*mib_item_dup(mib_item_t *item);
1460Sstevel@tonic-gate static mib_item_t	*mib_item_diff(mib_item_t *item1,
1470Sstevel@tonic-gate     mib_item_t *item2);
1480Sstevel@tonic-gate static void		mib_item_destroy(mib_item_t **item);
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate static boolean_t	octetstrmatch(const Octet_t *a, const Octet_t *b);
1511676Sjpk static char		*octetstr(const Octet_t *op, int code,
1520Sstevel@tonic-gate 			    char *dst, uint_t dstlen);
1530Sstevel@tonic-gate static char		*pr_addr(uint_t addr,
1540Sstevel@tonic-gate 			    char *dst, uint_t dstlen);
1550Sstevel@tonic-gate static char		*pr_addrnz(ipaddr_t addr, char *dst, uint_t dstlen);
1560Sstevel@tonic-gate static char		*pr_addr6(const in6_addr_t *addr,
1570Sstevel@tonic-gate 			    char *dst, uint_t dstlen);
1580Sstevel@tonic-gate static char		*pr_mask(uint_t addr,
1590Sstevel@tonic-gate 			    char *dst, uint_t dstlen);
1601676Sjpk static char		*pr_prefix6(const struct in6_addr *addr,
1611676Sjpk 			    uint_t prefixlen, char *dst, uint_t dstlen);
1620Sstevel@tonic-gate static char		*pr_ap(uint_t addr, uint_t port,
1630Sstevel@tonic-gate 			    char *proto, char *dst, uint_t dstlen);
1640Sstevel@tonic-gate static char		*pr_ap6(const in6_addr_t *addr, uint_t port,
1650Sstevel@tonic-gate 			    char *proto, char *dst, uint_t dstlen);
1660Sstevel@tonic-gate static char		*pr_net(uint_t addr, uint_t mask,
1670Sstevel@tonic-gate 			    char *dst, uint_t dstlen);
1680Sstevel@tonic-gate static char		*pr_netaddr(uint_t addr, uint_t mask,
1690Sstevel@tonic-gate 			    char *dst, uint_t dstlen);
1700Sstevel@tonic-gate static char		*pr_netclassless(ipaddr_t addr, ipaddr_t mask,
1710Sstevel@tonic-gate 			    char *dst, size_t dstlen);
1720Sstevel@tonic-gate static char		*fmodestr(uint_t fmode);
1730Sstevel@tonic-gate static char		*portname(uint_t port, char *proto,
1740Sstevel@tonic-gate 			    char *dst, uint_t dstlen);
1750Sstevel@tonic-gate 
1761676Sjpk static const char	*mitcp_state(int code,
1771676Sjpk 			    const mib2_transportMLPEntry_t *attr);
1781676Sjpk static const char	*miudp_state(int code,
1791676Sjpk 			    const mib2_transportMLPEntry_t *attr);
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate static void		stat_report(mib_item_t *item);
1820Sstevel@tonic-gate static void		mrt_stat_report(mib_item_t *item);
1830Sstevel@tonic-gate static void		arp_report(mib_item_t *item);
1840Sstevel@tonic-gate static void		ndp_report(mib_item_t *item);
1850Sstevel@tonic-gate static void		mrt_report(mib_item_t *item);
1860Sstevel@tonic-gate static void		if_stat_total(struct ifstat *oldstats,
1870Sstevel@tonic-gate 			    struct ifstat *newstats, struct ifstat *sumstats);
1880Sstevel@tonic-gate static void		if_report(mib_item_t *item, char *ifname,
1890Sstevel@tonic-gate 			    int Iflag_only, boolean_t once_only);
1900Sstevel@tonic-gate static void		if_report_ip4(mib2_ipAddrEntry_t *ap,
1910Sstevel@tonic-gate 			    char ifname[], char logintname[],
1920Sstevel@tonic-gate 			    struct ifstat *statptr, boolean_t ksp_not_null);
1930Sstevel@tonic-gate static void		if_report_ip6(mib2_ipv6AddrEntry_t *ap6,
1940Sstevel@tonic-gate 			    char ifname[], char logintname[],
1950Sstevel@tonic-gate 			    struct ifstat *statptr, boolean_t ksp_not_null);
1961676Sjpk static void		ire_report(const mib_item_t *item);
1971676Sjpk static void		tcp_report(const mib_item_t *item);
1981676Sjpk static void		udp_report(const mib_item_t *item);
1990Sstevel@tonic-gate static void		group_report(mib_item_t *item);
2000Sstevel@tonic-gate static void		print_ip_stats(mib2_ip_t *ip);
2010Sstevel@tonic-gate static void		print_icmp_stats(mib2_icmp_t *icmp);
2020Sstevel@tonic-gate static void		print_ip6_stats(mib2_ipv6IfStatsEntry_t *ip6);
2030Sstevel@tonic-gate static void		print_icmp6_stats(mib2_ipv6IfIcmpEntry_t *icmp6);
2040Sstevel@tonic-gate static void		print_sctp_stats(mib2_sctp_t *tcp);
2050Sstevel@tonic-gate static void		print_tcp_stats(mib2_tcp_t *tcp);
2060Sstevel@tonic-gate static void		print_udp_stats(mib2_udp_t *udp);
2070Sstevel@tonic-gate static void		print_rawip_stats(mib2_rawip_t *rawip);
2080Sstevel@tonic-gate static void		print_igmp_stats(struct igmpstat *igps);
2090Sstevel@tonic-gate static void		print_mrt_stats(struct mrtstat *mrts);
2101676Sjpk static void		sctp_report(const mib_item_t *item);
2110Sstevel@tonic-gate static void		sum_ip6_stats(mib2_ipv6IfStatsEntry_t *ip6,
2120Sstevel@tonic-gate 			    mib2_ipv6IfStatsEntry_t *sum6);
2130Sstevel@tonic-gate static void		sum_icmp6_stats(mib2_ipv6IfIcmpEntry_t *icmp6,
2140Sstevel@tonic-gate 			    mib2_ipv6IfIcmpEntry_t *sum6);
2150Sstevel@tonic-gate static void		m_report(void);
2160Sstevel@tonic-gate static void		dhcp_report(char *);
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 	void		fail(int, char *, ...);
2190Sstevel@tonic-gate static	uint64_t	kstat_named_value(kstat_t *, char *);
2200Sstevel@tonic-gate static	kid_t		safe_kstat_read(kstat_ctl_t *, kstat_t *, void *);
2210Sstevel@tonic-gate static int		isnum(char *);
2220Sstevel@tonic-gate static char		*plural(int n);
2230Sstevel@tonic-gate static char		*pluraly(int n);
2240Sstevel@tonic-gate static char		*plurales(int n);
2250Sstevel@tonic-gate static void		process_filter(char *arg);
2260Sstevel@tonic-gate static boolean_t	family_selected(int family);
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate static void		usage(char *);
2290Sstevel@tonic-gate static void 		fatal(int errcode, char *str1, ...);
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate #define	PLURAL(n) plural((int)n)
2320Sstevel@tonic-gate #define	PLURALY(n) pluraly((int)n)
2330Sstevel@tonic-gate #define	PLURALES(n) plurales((int)n)
2340Sstevel@tonic-gate #define	IFLAGMOD(flg, val1, val2)	if (flg == val1) flg = val2
2350Sstevel@tonic-gate #define	MDIFF(diff, elem2, elem1, member)	(diff)->member = \
2360Sstevel@tonic-gate 	(elem2)->member - (elem1)->member
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate static	boolean_t	Aflag = B_FALSE;	/* All sockets/ifs/rtng-tbls */
2400Sstevel@tonic-gate static	boolean_t	Dflag = B_FALSE;	/* Debug Info */
2410Sstevel@tonic-gate static	boolean_t	Iflag = B_FALSE;	/* IP Traffic Interfaces */
2420Sstevel@tonic-gate static	boolean_t	Mflag = B_FALSE;	/* STREAMS Memory Statistics */
2430Sstevel@tonic-gate static	boolean_t	Nflag = B_FALSE;	/* Numeric Network Addresses */
2440Sstevel@tonic-gate static	boolean_t	Rflag = B_FALSE;	/* Routing Tables */
2451676Sjpk static	boolean_t	RSECflag = B_FALSE;	/* Security attributes */
2460Sstevel@tonic-gate static	boolean_t	Sflag = B_FALSE;	/* Per-protocol Statistics */
2470Sstevel@tonic-gate static	boolean_t	Vflag = B_FALSE;	/* Verbose */
2480Sstevel@tonic-gate static	boolean_t	Pflag = B_FALSE;	/* Net to Media Tables */
2490Sstevel@tonic-gate static	boolean_t	Gflag = B_FALSE;	/* Multicast group membership */
2500Sstevel@tonic-gate static	boolean_t	MMflag = B_FALSE;	/* Multicast routing table */
2510Sstevel@tonic-gate static	boolean_t	DHCPflag = B_FALSE;	/* DHCP statistics */
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate static	int	v4compat = 0;	/* Compatible printing format for status */
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate static int	proto = IPPROTO_MAX;	/* all protocols */
2560Sstevel@tonic-gate kstat_ctl_t	*kc = NULL;
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate /*
2590Sstevel@tonic-gate  * Sizes of data structures extracted from the base mib.
2600Sstevel@tonic-gate  * This allows the size of the tables entries to grow while preserving
2610Sstevel@tonic-gate  * binary compatibility.
2620Sstevel@tonic-gate  */
2630Sstevel@tonic-gate static int ipAddrEntrySize;
2640Sstevel@tonic-gate static int ipRouteEntrySize;
2650Sstevel@tonic-gate static int ipNetToMediaEntrySize;
2660Sstevel@tonic-gate static int ipMemberEntrySize;
2670Sstevel@tonic-gate static int ipGroupSourceEntrySize;
2681676Sjpk static int ipRouteAttributeSize;
2690Sstevel@tonic-gate static int vifctlSize;
2700Sstevel@tonic-gate static int mfcctlSize;
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate static int ipv6IfStatsEntrySize;
2730Sstevel@tonic-gate static int ipv6IfIcmpEntrySize;
2740Sstevel@tonic-gate static int ipv6AddrEntrySize;
2750Sstevel@tonic-gate static int ipv6RouteEntrySize;
2760Sstevel@tonic-gate static int ipv6NetToMediaEntrySize;
2770Sstevel@tonic-gate static int ipv6MemberEntrySize;
2780Sstevel@tonic-gate static int ipv6GroupSourceEntrySize;
2790Sstevel@tonic-gate 
2801676Sjpk static int transportMLPSize;
2810Sstevel@tonic-gate static int tcpConnEntrySize;
2820Sstevel@tonic-gate static int tcp6ConnEntrySize;
2830Sstevel@tonic-gate static int udpEntrySize;
2840Sstevel@tonic-gate static int udp6EntrySize;
2850Sstevel@tonic-gate static int sctpEntrySize;
2860Sstevel@tonic-gate static int sctpLocalEntrySize;
2870Sstevel@tonic-gate static int sctpRemoteEntrySize;
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate #define	protocol_selected(p)	(proto == IPPROTO_MAX || proto == (p))
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate /* Machinery used for -f (filter) option */
2920Sstevel@tonic-gate #define	FK_AF		0
2930Sstevel@tonic-gate #define	FK_INIF		1
2940Sstevel@tonic-gate #define	FK_OUTIF	2
2950Sstevel@tonic-gate #define	FK_SRC		3
2960Sstevel@tonic-gate #define	FK_DST		4
2970Sstevel@tonic-gate #define	FK_FLAGS	5
2980Sstevel@tonic-gate #define	NFILTERKEYS	6
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate static const char *filter_keys[NFILTERKEYS] = {
3010Sstevel@tonic-gate 	"af", "inif", "outif", "src", "dst", "flags"
3020Sstevel@tonic-gate };
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate /* Flags on routes */
3050Sstevel@tonic-gate #define	FLF_A		0x00000001
3060Sstevel@tonic-gate #define	FLF_B		0x00000002
3070Sstevel@tonic-gate #define	FLF_D		0x00000004
3080Sstevel@tonic-gate #define	FLF_G		0x00000008
3090Sstevel@tonic-gate #define	FLF_H		0x00000010
3100Sstevel@tonic-gate #define	FLF_L		0x00000020
3110Sstevel@tonic-gate #define	FLF_U		0x00000040
3120Sstevel@tonic-gate #define	FLF_M		0x00000080
3130Sstevel@tonic-gate #define	FLF_S		0x00000100
3140Sstevel@tonic-gate static const char flag_list[] = "ABDGHLUMS";
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate typedef struct filter_rule filter_t;
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate struct filter_rule {
3190Sstevel@tonic-gate 	filter_t *f_next;
3200Sstevel@tonic-gate 	union {
3210Sstevel@tonic-gate 		int f_family;
3220Sstevel@tonic-gate 		const char *f_ifname;
3230Sstevel@tonic-gate 		struct {
3240Sstevel@tonic-gate 			struct hostent *f_address;
3250Sstevel@tonic-gate 			in6_addr_t f_mask;
3260Sstevel@tonic-gate 		} a;
3270Sstevel@tonic-gate 		struct {
3280Sstevel@tonic-gate 			uint_t f_flagset;
3290Sstevel@tonic-gate 			uint_t f_flagclear;
3300Sstevel@tonic-gate 		} f;
3310Sstevel@tonic-gate 	} u;
3320Sstevel@tonic-gate };
3330Sstevel@tonic-gate 
3340Sstevel@tonic-gate /*
3350Sstevel@tonic-gate  * The user-specified filters are linked into lists separated by
3360Sstevel@tonic-gate  * keyword (type of filter).  Thus, the matching algorithm is:
3370Sstevel@tonic-gate  *	For each non-empty filter list
3380Sstevel@tonic-gate  *		If no filters in the list match
3390Sstevel@tonic-gate  *			then stop here; route doesn't match
3400Sstevel@tonic-gate  *	If loop above completes, then route does match and will be
3410Sstevel@tonic-gate  *	displayed.
3420Sstevel@tonic-gate  */
3430Sstevel@tonic-gate static filter_t *filters[NFILTERKEYS];
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate int
3460Sstevel@tonic-gate main(int argc, char **argv)
3470Sstevel@tonic-gate {
3480Sstevel@tonic-gate 	char		*name;
3490Sstevel@tonic-gate 	mib_item_t	*item = NULL;
3500Sstevel@tonic-gate 	mib_item_t	*previtem = NULL;
3510Sstevel@tonic-gate 	int		sd = -1;
3520Sstevel@tonic-gate 	char	*ifname = NULL;
3530Sstevel@tonic-gate 	int	interval = 0;	/* Single time by default */
3540Sstevel@tonic-gate 	int	count = -1;	/* Forever */
3550Sstevel@tonic-gate 	int	c;
3560Sstevel@tonic-gate 	int	d;
3570Sstevel@tonic-gate 	/*
3580Sstevel@tonic-gate 	 * Possible values of 'Iflag_only':
3590Sstevel@tonic-gate 	 * -1, no feature-flags;
3600Sstevel@tonic-gate 	 *  0, IFlag and other feature-flags enabled
3610Sstevel@tonic-gate 	 *  1, IFlag is the only feature-flag enabled
3620Sstevel@tonic-gate 	 * : trinary variable, modified using IFLAGMOD()
3630Sstevel@tonic-gate 	 */
3640Sstevel@tonic-gate 	int Iflag_only = -1;
3650Sstevel@tonic-gate 	boolean_t once_only = B_FALSE; /* '-i' with count > 1 */
3660Sstevel@tonic-gate 	extern char	*optarg;
3670Sstevel@tonic-gate 	extern int	optind;
3680Sstevel@tonic-gate 	char *default_ip_str = NULL;
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate 	name = argv[0];
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate 	v4compat = get_compat_flag(&default_ip_str);
3730Sstevel@tonic-gate 	if (v4compat == DEFAULT_PROT_BAD_VALUE)
3740Sstevel@tonic-gate 		fatal(2, "%s: %s: Bad value for %s in %s\n", name,
3750Sstevel@tonic-gate 		    default_ip_str, DEFAULT_IP, INET_DEFAULT_FILE);
3760Sstevel@tonic-gate 	free(default_ip_str);
3770Sstevel@tonic-gate 
3781676Sjpk 	while ((c = getopt(argc, argv, "adimnrspMgvf:P:I:DR")) != -1) {
3790Sstevel@tonic-gate 		switch ((char)c) {
3800Sstevel@tonic-gate 		case 'a':		/* all connections */
3810Sstevel@tonic-gate 			Aflag = B_TRUE;
3820Sstevel@tonic-gate 			break;
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate 		case 'd':		/* turn on debugging */
3850Sstevel@tonic-gate 			Dflag = B_TRUE;
3860Sstevel@tonic-gate 			break;
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate 		case 'i':		/* interface (ill/ipif report) */
3890Sstevel@tonic-gate 			Iflag = B_TRUE;
3900Sstevel@tonic-gate 			IFLAGMOD(Iflag_only, -1, 1); /* '-i' exists */
3910Sstevel@tonic-gate 			break;
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate 		case 'm':		/* streams msg report */
3940Sstevel@tonic-gate 			Mflag = B_TRUE;
3950Sstevel@tonic-gate 			IFLAGMOD(Iflag_only, 1, 0); /* see macro def'n */
3960Sstevel@tonic-gate 			break;
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate 		case 'n':		/* numeric format */
3990Sstevel@tonic-gate 			Nflag = B_TRUE;
4000Sstevel@tonic-gate 			break;
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate 		case 'r':		/* route tables */
4030Sstevel@tonic-gate 			Rflag = B_TRUE;
4040Sstevel@tonic-gate 			IFLAGMOD(Iflag_only, 1, 0); /* see macro def'n */
4050Sstevel@tonic-gate 			break;
4060Sstevel@tonic-gate 
4071676Sjpk 		case 'R':		/* security attributes */
4081676Sjpk 			RSECflag = B_TRUE;
4091676Sjpk 			IFLAGMOD(Iflag_only, 1, 0); /* see macro def'n */
4101676Sjpk 			break;
4111676Sjpk 
4120Sstevel@tonic-gate 		case 's':		/* per-protocol statistics */
4130Sstevel@tonic-gate 			Sflag = B_TRUE;
4140Sstevel@tonic-gate 			IFLAGMOD(Iflag_only, 1, 0); /* see macro def'n */
4150Sstevel@tonic-gate 			break;
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 		case 'p':		/* arp/ndp table */
4180Sstevel@tonic-gate 			Pflag = B_TRUE;
4190Sstevel@tonic-gate 			IFLAGMOD(Iflag_only, 1, 0); /* see macro def'n */
4200Sstevel@tonic-gate 			break;
4210Sstevel@tonic-gate 
4220Sstevel@tonic-gate 		case 'M':		/* multicast routing tables */
4230Sstevel@tonic-gate 			MMflag = B_TRUE;
4240Sstevel@tonic-gate 			IFLAGMOD(Iflag_only, 1, 0); /* see macro def'n */
4250Sstevel@tonic-gate 			break;
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate 		case 'g':		/* multicast group membership */
4280Sstevel@tonic-gate 			Gflag = B_TRUE;
4290Sstevel@tonic-gate 			IFLAGMOD(Iflag_only, 1, 0); /* see macro def'n */
4300Sstevel@tonic-gate 			break;
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate 		case 'v':		/* verbose output format */
4330Sstevel@tonic-gate 			Vflag = B_TRUE;
4340Sstevel@tonic-gate 			IFLAGMOD(Iflag_only, 1, 0); /* see macro def'n */
4350Sstevel@tonic-gate 			break;
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate 		case 'f':
4380Sstevel@tonic-gate 			process_filter(optarg);
4390Sstevel@tonic-gate 			break;
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate 		case 'P':
4420Sstevel@tonic-gate 			if (strcmp(optarg, "ip") == 0) {
4430Sstevel@tonic-gate 				proto = IPPROTO_IP;
4440Sstevel@tonic-gate 			} else if (strcmp(optarg, "ipv6") == 0 ||
4450Sstevel@tonic-gate 			    strcmp(optarg, "ip6") == 0) {
4460Sstevel@tonic-gate 				v4compat = 0;	/* Overridden */
4470Sstevel@tonic-gate 				proto = IPPROTO_IPV6;
4480Sstevel@tonic-gate 			} else if (strcmp(optarg, "icmp") == 0) {
4490Sstevel@tonic-gate 				proto = IPPROTO_ICMP;
4500Sstevel@tonic-gate 			} else if (strcmp(optarg, "icmpv6") == 0 ||
4510Sstevel@tonic-gate 			    strcmp(optarg, "icmp6") == 0) {
4520Sstevel@tonic-gate 				v4compat = 0;	/* Overridden */
4530Sstevel@tonic-gate 				proto = IPPROTO_ICMPV6;
4540Sstevel@tonic-gate 			} else if (strcmp(optarg, "igmp") == 0) {
4550Sstevel@tonic-gate 				proto = IPPROTO_IGMP;
4560Sstevel@tonic-gate 			} else if (strcmp(optarg, "udp") == 0) {
4570Sstevel@tonic-gate 				proto = IPPROTO_UDP;
4580Sstevel@tonic-gate 			} else if (strcmp(optarg, "tcp") == 0) {
4590Sstevel@tonic-gate 				proto = IPPROTO_TCP;
4600Sstevel@tonic-gate 			} else if (strcmp(optarg, "sctp") == 0) {
4610Sstevel@tonic-gate 				proto = IPPROTO_SCTP;
4620Sstevel@tonic-gate 			} else if (strcmp(optarg, "raw") == 0 ||
4630Sstevel@tonic-gate 			    strcmp(optarg, "rawip") == 0) {
4640Sstevel@tonic-gate 				proto = IPPROTO_RAW;
4650Sstevel@tonic-gate 			} else {
4660Sstevel@tonic-gate 				fatal(1, "%s: unknown protocol.\n", optarg);
4670Sstevel@tonic-gate 			}
4680Sstevel@tonic-gate 			break;
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate 		case 'I':
4710Sstevel@tonic-gate 			ifname = optarg;
4720Sstevel@tonic-gate 			Iflag = B_TRUE;
4730Sstevel@tonic-gate 			IFLAGMOD(Iflag_only, -1, 1); /* see macro def'n */
4740Sstevel@tonic-gate 			break;
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate 		case 'D':
4770Sstevel@tonic-gate 			DHCPflag = B_TRUE;
4780Sstevel@tonic-gate 			Iflag_only = 0;
4790Sstevel@tonic-gate 			break;
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate 		case '?':
4820Sstevel@tonic-gate 		default:
4830Sstevel@tonic-gate 			usage(name);
4840Sstevel@tonic-gate 		}
4850Sstevel@tonic-gate 	}
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate 	/*
4881676Sjpk 	 * Make sure -R option is set only on a labeled system.
4891676Sjpk 	 */
4901676Sjpk 	if (RSECflag && !is_system_labeled()) {
4911676Sjpk 		(void) fprintf(stderr, "-R set but labeling is not enabled\n");
4921676Sjpk 		usage(name);
4931676Sjpk 	}
4941676Sjpk 
4951676Sjpk 	/*
4960Sstevel@tonic-gate 	 * Handle other arguments: find interval, count; the
4970Sstevel@tonic-gate 	 * flags that accept 'interval' and 'count' are OR'd
4980Sstevel@tonic-gate 	 * in the outermost 'if'; more flags may be added as
4990Sstevel@tonic-gate 	 * required
5000Sstevel@tonic-gate 	 */
5010Sstevel@tonic-gate 	if (Iflag || Sflag || Mflag) {
5020Sstevel@tonic-gate 		for (d = optind; d < argc; d++) {
5030Sstevel@tonic-gate 			if (isnum(argv[d])) {
5040Sstevel@tonic-gate 				interval = atoi(argv[d]);
5050Sstevel@tonic-gate 				if (d + 1 < argc &&
5060Sstevel@tonic-gate 				    isnum(argv[d + 1])) {
5070Sstevel@tonic-gate 					count = atoi(argv[d + 1]);
5080Sstevel@tonic-gate 					optind++;
5090Sstevel@tonic-gate 				}
5100Sstevel@tonic-gate 				optind++;
5110Sstevel@tonic-gate 				if (interval == 0 || count == 0)
5120Sstevel@tonic-gate 					usage(name);
5130Sstevel@tonic-gate 				break;
5140Sstevel@tonic-gate 			}
5150Sstevel@tonic-gate 		}
5160Sstevel@tonic-gate 	}
5170Sstevel@tonic-gate 	if (optind < argc) {
5180Sstevel@tonic-gate 		if (Iflag && isnum(argv[optind])) {
5190Sstevel@tonic-gate 			count = atoi(argv[optind]);
5200Sstevel@tonic-gate 			if (count == 0)
5210Sstevel@tonic-gate 				usage(name);
5220Sstevel@tonic-gate 			optind++;
5230Sstevel@tonic-gate 		}
5240Sstevel@tonic-gate 	}
5250Sstevel@tonic-gate 	if (optind < argc) {
5260Sstevel@tonic-gate 		(void) fprintf(stderr,
5270Sstevel@tonic-gate 		    "%s: extra arguments\n", name);
5280Sstevel@tonic-gate 		usage(name);
5290Sstevel@tonic-gate 	}
5300Sstevel@tonic-gate 	if (interval)
5310Sstevel@tonic-gate 		setbuf(stdout, NULL);
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate 	if (DHCPflag) {
5340Sstevel@tonic-gate 		dhcp_report(Iflag ? ifname : NULL);
5350Sstevel@tonic-gate 		exit(0);
5360Sstevel@tonic-gate 	}
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate 	/* Get data structures: priming before iteration */
5390Sstevel@tonic-gate 	if (family_selected(AF_INET) || family_selected(AF_INET6)) {
5400Sstevel@tonic-gate 		sd = mibopen();
5410Sstevel@tonic-gate 		if (sd == -1)
5420Sstevel@tonic-gate 			fatal(1, "can't open mib stream\n");
5430Sstevel@tonic-gate 		if ((item = mibget(sd)) == NULL) {
5440Sstevel@tonic-gate 			(void) close(sd);
5450Sstevel@tonic-gate 			fatal(1, "mibget() failed\n");
5460Sstevel@tonic-gate 		}
5470Sstevel@tonic-gate 		/* Extract constant sizes - need do once only */
5480Sstevel@tonic-gate 		mib_get_constants(item);
5490Sstevel@tonic-gate 	}
5500Sstevel@tonic-gate 	if ((kc = kstat_open()) == NULL) {
5510Sstevel@tonic-gate 		mibfree(item);
5520Sstevel@tonic-gate 		(void) close(sd);
5530Sstevel@tonic-gate 		fail(1, "kstat_open(): can't open /dev/kstat");
5540Sstevel@tonic-gate 	}
5550Sstevel@tonic-gate 
5560Sstevel@tonic-gate 	if (interval <= 0) {
5570Sstevel@tonic-gate 		count = 1;
5580Sstevel@tonic-gate 		once_only = B_TRUE;
5590Sstevel@tonic-gate 	}
5600Sstevel@tonic-gate 	/* 'for' loop 1: */
5610Sstevel@tonic-gate 	for (;;) {
5620Sstevel@tonic-gate 		mib_item_t *curritem = NULL; /* only for -[M]s */
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate 		/* netstat: AF_INET[6] behaviour */
5650Sstevel@tonic-gate 		if (family_selected(AF_INET) || family_selected(AF_INET6)) {
5660Sstevel@tonic-gate 			if (Sflag) {
5670Sstevel@tonic-gate 				curritem = mib_item_diff(previtem, item);
5680Sstevel@tonic-gate 				if (curritem == NULL)
5690Sstevel@tonic-gate 					fatal(1, "can't process mib data, "
5700Sstevel@tonic-gate 					    "out of memory\n");
5710Sstevel@tonic-gate 				mib_item_destroy(&previtem);
5720Sstevel@tonic-gate 			}
5730Sstevel@tonic-gate 
5740Sstevel@tonic-gate 			if (!(Iflag || Rflag || Sflag || Mflag ||
5750Sstevel@tonic-gate 			    MMflag || Pflag || Gflag || DHCPflag)) {
5760Sstevel@tonic-gate 				if (protocol_selected(IPPROTO_UDP))
5770Sstevel@tonic-gate 					udp_report(item);
5780Sstevel@tonic-gate 				if (protocol_selected(IPPROTO_TCP))
5790Sstevel@tonic-gate 					tcp_report(item);
5800Sstevel@tonic-gate 				if (protocol_selected(IPPROTO_SCTP))
5810Sstevel@tonic-gate 					sctp_report(item);
5820Sstevel@tonic-gate 			}
5830Sstevel@tonic-gate 			if (Iflag)
5840Sstevel@tonic-gate 				if_report(item, ifname, Iflag_only, once_only);
5850Sstevel@tonic-gate 			if (Mflag)
5860Sstevel@tonic-gate 				m_report();
5870Sstevel@tonic-gate 			if (Rflag)
5880Sstevel@tonic-gate 				ire_report(item);
5890Sstevel@tonic-gate 			if (Sflag && MMflag) {
5900Sstevel@tonic-gate 				mrt_stat_report(curritem);
5910Sstevel@tonic-gate 			} else {
5920Sstevel@tonic-gate 				if (Sflag)
5930Sstevel@tonic-gate 					stat_report(curritem);
5940Sstevel@tonic-gate 				if (MMflag)
5950Sstevel@tonic-gate 					mrt_report(item);
5960Sstevel@tonic-gate 			}
5970Sstevel@tonic-gate 			if (Gflag)
5980Sstevel@tonic-gate 				group_report(item);
5990Sstevel@tonic-gate 			if (Pflag) {
6000Sstevel@tonic-gate 				if (family_selected(AF_INET))
6010Sstevel@tonic-gate 					arp_report(item);
6020Sstevel@tonic-gate 				if (family_selected(AF_INET6))
6030Sstevel@tonic-gate 					ndp_report(item);
6040Sstevel@tonic-gate 			}
6050Sstevel@tonic-gate 			mib_item_destroy(&curritem);
6060Sstevel@tonic-gate 		}
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate 		/* netstat: AF_UNIX behaviour */
6090Sstevel@tonic-gate 		if (family_selected(AF_UNIX) &&
6100Sstevel@tonic-gate 		    (!(Iflag || Rflag || Sflag || Mflag ||
6110Sstevel@tonic-gate 		    MMflag || Pflag || Gflag)))
6120Sstevel@tonic-gate 			unixpr(kc);
6130Sstevel@tonic-gate 		(void) kstat_close(kc);
6140Sstevel@tonic-gate 
6150Sstevel@tonic-gate 		/* iteration handling code */
6160Sstevel@tonic-gate 		if (count > 0 && --count == 0)
6170Sstevel@tonic-gate 			break;
6180Sstevel@tonic-gate 		(void) sleep(interval);
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 		/* re-populating of data structures */
6210Sstevel@tonic-gate 		if (family_selected(AF_INET) || family_selected(AF_INET6)) {
6220Sstevel@tonic-gate 			if (Sflag) {
6230Sstevel@tonic-gate 				/* previtem is a cut-down list */
6240Sstevel@tonic-gate 				previtem = mib_item_dup(item);
6250Sstevel@tonic-gate 				if (previtem == NULL)
6260Sstevel@tonic-gate 					fatal(1, "can't process mib data, "
6270Sstevel@tonic-gate 					    "out of memory\n");
6280Sstevel@tonic-gate 			}
6290Sstevel@tonic-gate 			mibfree(item);
6300Sstevel@tonic-gate 			(void) close(sd);
6310Sstevel@tonic-gate 			if ((sd = mibopen()) == -1)
6320Sstevel@tonic-gate 				fatal(1, "can't open mib stream anymore\n");
6330Sstevel@tonic-gate 			if ((item = mibget(sd)) == NULL) {
6340Sstevel@tonic-gate 				(void) close(sd);
6350Sstevel@tonic-gate 				fatal(1, "mibget() failed\n");
6360Sstevel@tonic-gate 			}
6370Sstevel@tonic-gate 		}
6380Sstevel@tonic-gate 		if ((kc = kstat_open()) == NULL)
6390Sstevel@tonic-gate 			fail(1, "kstat_open(): can't open /dev/kstat");
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate 	} /* 'for' loop 1 ends */
6420Sstevel@tonic-gate 	mibfree(item);
6430Sstevel@tonic-gate 	(void) close(sd);
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate 	return (0);
6460Sstevel@tonic-gate }
6470Sstevel@tonic-gate 
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate static int
6500Sstevel@tonic-gate isnum(char *p)
6510Sstevel@tonic-gate {
6520Sstevel@tonic-gate 	int	len;
6530Sstevel@tonic-gate 	int	i;
6540Sstevel@tonic-gate 
6550Sstevel@tonic-gate 	len = strlen(p);
6560Sstevel@tonic-gate 	for (i = 0; i < len; i++)
6570Sstevel@tonic-gate 		if (!isdigit(p[i]))
6580Sstevel@tonic-gate 			return (0);
6590Sstevel@tonic-gate 	return (1);
6600Sstevel@tonic-gate }
6610Sstevel@tonic-gate 
6620Sstevel@tonic-gate 
6630Sstevel@tonic-gate /* --------------------------------- MIBGET -------------------------------- */
6640Sstevel@tonic-gate 
6650Sstevel@tonic-gate static mib_item_t *
6660Sstevel@tonic-gate mibget(int sd)
6670Sstevel@tonic-gate {
6680Sstevel@tonic-gate 	/*
6690Sstevel@tonic-gate 	 * buf is an automatic for this function, so the
6700Sstevel@tonic-gate 	 * compiler has complete control over its alignment;
6710Sstevel@tonic-gate 	 * it is assumed this alignment is satisfactory for
6720Sstevel@tonic-gate 	 * it to be casted to certain other struct pointers
6730Sstevel@tonic-gate 	 * here, such as struct T_optmgmt_ack * .
6740Sstevel@tonic-gate 	 */
6750Sstevel@tonic-gate 	uintptr_t		buf[512 / sizeof (uintptr_t)];
6760Sstevel@tonic-gate 	int			flags;
6770Sstevel@tonic-gate 	int			i, j, getcode;
6780Sstevel@tonic-gate 	struct strbuf		ctlbuf, databuf;
6790Sstevel@tonic-gate 	struct T_optmgmt_req	*tor = (struct T_optmgmt_req *)buf;
6800Sstevel@tonic-gate 	struct T_optmgmt_ack	*toa = (struct T_optmgmt_ack *)buf;
6810Sstevel@tonic-gate 	struct T_error_ack	*tea = (struct T_error_ack *)buf;
6820Sstevel@tonic-gate 	struct opthdr		*req;
6830Sstevel@tonic-gate 	mib_item_t		*first_item = NULL;
6840Sstevel@tonic-gate 	mib_item_t		*last_item  = NULL;
6850Sstevel@tonic-gate 	mib_item_t		*temp;
6860Sstevel@tonic-gate 
6870Sstevel@tonic-gate 	tor->PRIM_type = T_SVR4_OPTMGMT_REQ;
6880Sstevel@tonic-gate 	tor->OPT_offset = sizeof (struct T_optmgmt_req);
6890Sstevel@tonic-gate 	tor->OPT_length = sizeof (struct opthdr);
6900Sstevel@tonic-gate 	tor->MGMT_flags = T_CURRENT;
6910Sstevel@tonic-gate 	req = (struct opthdr *)&tor[1];
6920Sstevel@tonic-gate 	req->level = MIB2_IP;		/* any MIB2_xxx value ok here */
6930Sstevel@tonic-gate 	req->name  = 0;
6940Sstevel@tonic-gate 	req->len   = 0;
6950Sstevel@tonic-gate 
6960Sstevel@tonic-gate 	ctlbuf.buf = (char *)buf;
6970Sstevel@tonic-gate 	ctlbuf.len = tor->OPT_length + tor->OPT_offset;
6980Sstevel@tonic-gate 	flags = 0;
6990Sstevel@tonic-gate 	if (putmsg(sd, &ctlbuf, (struct strbuf *)0, flags) == -1) {
7000Sstevel@tonic-gate 		perror("mibget: putmsg(ctl) failed");
7010Sstevel@tonic-gate 		goto error_exit;
7020Sstevel@tonic-gate 	}
7030Sstevel@tonic-gate 
7040Sstevel@tonic-gate 	/*
7050Sstevel@tonic-gate 	 * Each reply consists of a ctl part for one fixed structure
7060Sstevel@tonic-gate 	 * or table, as defined in mib2.h.  The format is a T_OPTMGMT_ACK,
7070Sstevel@tonic-gate 	 * containing an opthdr structure.  level/name identify the entry,
7080Sstevel@tonic-gate 	 * len is the size of the data part of the message.
7090Sstevel@tonic-gate 	 */
7100Sstevel@tonic-gate 	req = (struct opthdr *)&toa[1];
7110Sstevel@tonic-gate 	ctlbuf.maxlen = sizeof (buf);
7120Sstevel@tonic-gate 	j = 1;
7130Sstevel@tonic-gate 	for (;;) {
7140Sstevel@tonic-gate 		flags = 0;
7150Sstevel@tonic-gate 		getcode = getmsg(sd, &ctlbuf, (struct strbuf *)0, &flags);
7160Sstevel@tonic-gate 		if (getcode == -1) {
7170Sstevel@tonic-gate 			perror("mibget getmsg(ctl) failed");
7180Sstevel@tonic-gate 			if (Dflag) {
7190Sstevel@tonic-gate 				(void) fputs("#   level   name    len\n",
7200Sstevel@tonic-gate 				    stderr);
7210Sstevel@tonic-gate 				i = 0;
7220Sstevel@tonic-gate 				for (last_item = first_item; last_item;
7230Sstevel@tonic-gate 					last_item = last_item->next_item)
7240Sstevel@tonic-gate 					(void) printf("%d  %4d   %5d   %d\n",
7250Sstevel@tonic-gate 					    ++i,
7260Sstevel@tonic-gate 					    last_item->group,
7270Sstevel@tonic-gate 					    last_item->mib_id,
7280Sstevel@tonic-gate 					    last_item->length);
7290Sstevel@tonic-gate 			}
7300Sstevel@tonic-gate 			goto error_exit;
7310Sstevel@tonic-gate 		}
7320Sstevel@tonic-gate 		if (getcode == 0 &&
7330Sstevel@tonic-gate 		    ctlbuf.len >= sizeof (struct T_optmgmt_ack) &&
7340Sstevel@tonic-gate 		    toa->PRIM_type == T_OPTMGMT_ACK &&
7350Sstevel@tonic-gate 		    toa->MGMT_flags == T_SUCCESS &&
7360Sstevel@tonic-gate 		    req->len == 0) {
7370Sstevel@tonic-gate 			if (Dflag)
7380Sstevel@tonic-gate 				(void) printf("mibget getmsg() %d returned "
7390Sstevel@tonic-gate 				    "EOD (level %ld, name %ld)\n",
7400Sstevel@tonic-gate 				    j, req->level, req->name);
7410Sstevel@tonic-gate 			return (first_item);		/* this is EOD msg */
7420Sstevel@tonic-gate 		}
7430Sstevel@tonic-gate 
7440Sstevel@tonic-gate 		if (ctlbuf.len >= sizeof (struct T_error_ack) &&
7450Sstevel@tonic-gate 		    tea->PRIM_type == T_ERROR_ACK) {
7460Sstevel@tonic-gate 			(void) fprintf(stderr,
7470Sstevel@tonic-gate 			    "mibget %d gives T_ERROR_ACK: TLI_error = 0x%lx, "
7480Sstevel@tonic-gate 			    "UNIX_error = 0x%lx\n",
7490Sstevel@tonic-gate 			    j, tea->TLI_error, tea->UNIX_error);
7500Sstevel@tonic-gate 
7510Sstevel@tonic-gate 			errno = (tea->TLI_error == TSYSERR) ?
7520Sstevel@tonic-gate 			    tea->UNIX_error : EPROTO;
7530Sstevel@tonic-gate 			goto error_exit;
7540Sstevel@tonic-gate 		}
7550Sstevel@tonic-gate 
7560Sstevel@tonic-gate 		if (getcode != MOREDATA ||
7570Sstevel@tonic-gate 		    ctlbuf.len < sizeof (struct T_optmgmt_ack) ||
7580Sstevel@tonic-gate 		    toa->PRIM_type != T_OPTMGMT_ACK ||
7590Sstevel@tonic-gate 		    toa->MGMT_flags != T_SUCCESS) {
7600Sstevel@tonic-gate 			(void) printf("mibget getmsg(ctl) %d returned %d, "
7610Sstevel@tonic-gate 			    "ctlbuf.len = %d, PRIM_type = %ld\n",
7620Sstevel@tonic-gate 			    j, getcode, ctlbuf.len, toa->PRIM_type);
7630Sstevel@tonic-gate 
7640Sstevel@tonic-gate 			if (toa->PRIM_type == T_OPTMGMT_ACK)
7650Sstevel@tonic-gate 				(void) printf("T_OPTMGMT_ACK: "
7660Sstevel@tonic-gate 				    "MGMT_flags = 0x%lx, req->len = %ld\n",
7670Sstevel@tonic-gate 				    toa->MGMT_flags, req->len);
7680Sstevel@tonic-gate 			errno = ENOMSG;
7690Sstevel@tonic-gate 			goto error_exit;
7700Sstevel@tonic-gate 		}
7710Sstevel@tonic-gate 
7720Sstevel@tonic-gate 		temp = (mib_item_t *)malloc(sizeof (mib_item_t));
7730Sstevel@tonic-gate 		if (temp == NULL) {
7740Sstevel@tonic-gate 			perror("mibget malloc failed");
7750Sstevel@tonic-gate 			goto error_exit;
7760Sstevel@tonic-gate 		}
7770Sstevel@tonic-gate 		if (last_item != NULL)
7780Sstevel@tonic-gate 			last_item->next_item = temp;
7790Sstevel@tonic-gate 		else
7800Sstevel@tonic-gate 			first_item = temp;
7810Sstevel@tonic-gate 		last_item = temp;
7820Sstevel@tonic-gate 		last_item->next_item = NULL;
7830Sstevel@tonic-gate 		last_item->group = req->level;
7840Sstevel@tonic-gate 		last_item->mib_id = req->name;
7850Sstevel@tonic-gate 		last_item->length = req->len;
7860Sstevel@tonic-gate 		last_item->valp = malloc((int)req->len);
7870Sstevel@tonic-gate 		if (last_item->valp == NULL)
7880Sstevel@tonic-gate 			goto error_exit;
7890Sstevel@tonic-gate 		if (Dflag)
7900Sstevel@tonic-gate 			(void) printf("msg %d: group = %4d   mib_id = %5d"
7910Sstevel@tonic-gate 			    "length = %d\n",
7920Sstevel@tonic-gate 			    j, last_item->group, last_item->mib_id,
7930Sstevel@tonic-gate 			    last_item->length);
7940Sstevel@tonic-gate 
7950Sstevel@tonic-gate 		databuf.maxlen = last_item->length;
7960Sstevel@tonic-gate 		databuf.buf    = (char *)last_item->valp;
7970Sstevel@tonic-gate 		databuf.len    = 0;
7980Sstevel@tonic-gate 		flags = 0;
7990Sstevel@tonic-gate 		getcode = getmsg(sd, (struct strbuf *)0, &databuf, &flags);
8000Sstevel@tonic-gate 		if (getcode == -1) {
8010Sstevel@tonic-gate 			perror("mibget getmsg(data) failed");
8020Sstevel@tonic-gate 			goto error_exit;
8030Sstevel@tonic-gate 		} else if (getcode != 0) {
8040Sstevel@tonic-gate 			(void) printf("mibget getmsg(data) returned %d, "
8050Sstevel@tonic-gate 			    "databuf.maxlen = %d, databuf.len = %d\n",
8060Sstevel@tonic-gate 			    getcode, databuf.maxlen, databuf.len);
8070Sstevel@tonic-gate 			goto error_exit;
8080Sstevel@tonic-gate 		}
8090Sstevel@tonic-gate 		j++;
8100Sstevel@tonic-gate 	}
8110Sstevel@tonic-gate 	/* NOTREACHED */
8120Sstevel@tonic-gate 
8130Sstevel@tonic-gate error_exit:;
8140Sstevel@tonic-gate 	mibfree(first_item);
8150Sstevel@tonic-gate 	return (NULL);
8160Sstevel@tonic-gate }
8170Sstevel@tonic-gate 
8180Sstevel@tonic-gate /*
8190Sstevel@tonic-gate  * mibfree: frees a linked list of type (mib_item_t *)
8200Sstevel@tonic-gate  * returned by mibget(); this is NOT THE SAME AS
8210Sstevel@tonic-gate  * mib_item_destroy(), so should be used for objects
8220Sstevel@tonic-gate  * returned by mibget() only
8230Sstevel@tonic-gate  */
8240Sstevel@tonic-gate static void
8250Sstevel@tonic-gate mibfree(mib_item_t *firstitem)
8260Sstevel@tonic-gate {
8270Sstevel@tonic-gate 	mib_item_t *lastitem;
8280Sstevel@tonic-gate 
8290Sstevel@tonic-gate 	while (firstitem != NULL) {
8300Sstevel@tonic-gate 		lastitem = firstitem;
8310Sstevel@tonic-gate 		firstitem = firstitem->next_item;
8320Sstevel@tonic-gate 		if (lastitem->valp != NULL)
8330Sstevel@tonic-gate 			free(lastitem->valp);
8340Sstevel@tonic-gate 		free(lastitem);
8350Sstevel@tonic-gate 	}
8360Sstevel@tonic-gate }
8370Sstevel@tonic-gate 
8380Sstevel@tonic-gate static int
8390Sstevel@tonic-gate mibopen(void)
8400Sstevel@tonic-gate {
8410Sstevel@tonic-gate 	int	sd;
8420Sstevel@tonic-gate 
8430Sstevel@tonic-gate 	sd = open("/dev/arp", O_RDWR);
8440Sstevel@tonic-gate 	if (sd == -1) {
8450Sstevel@tonic-gate 		perror("arp open");
8460Sstevel@tonic-gate 		return (-1);
8470Sstevel@tonic-gate 	}
8480Sstevel@tonic-gate 	if (ioctl(sd, I_PUSH, "tcp") == -1) {
8490Sstevel@tonic-gate 		perror("tcp I_PUSH");
8500Sstevel@tonic-gate 		(void) close(sd);
8510Sstevel@tonic-gate 		return (-1);
8520Sstevel@tonic-gate 	}
8530Sstevel@tonic-gate 	if (ioctl(sd, I_PUSH, "udp") == -1) {
8540Sstevel@tonic-gate 		perror("udp I_PUSH");
8550Sstevel@tonic-gate 		(void) close(sd);
8560Sstevel@tonic-gate 		return (-1);
8570Sstevel@tonic-gate 	}
8580Sstevel@tonic-gate 	if (ioctl(sd, I_PUSH, "icmp") == -1) {
8590Sstevel@tonic-gate 		perror("icmp I_PUSH");
8600Sstevel@tonic-gate 		(void) close(sd);
8610Sstevel@tonic-gate 		return (-1);
8620Sstevel@tonic-gate 	}
8630Sstevel@tonic-gate 	return (sd);
8640Sstevel@tonic-gate }
8650Sstevel@tonic-gate 
8660Sstevel@tonic-gate /*
8670Sstevel@tonic-gate  * mib_item_dup: returns a clean mib_item_t * linked
8680Sstevel@tonic-gate  * list, so that for every element item->mib_id is 0;
8690Sstevel@tonic-gate  * to deallocate this linked list, use mib_item_destroy
8700Sstevel@tonic-gate  */
8710Sstevel@tonic-gate static mib_item_t *
8720Sstevel@tonic-gate mib_item_dup(mib_item_t *item)
8730Sstevel@tonic-gate {
8740Sstevel@tonic-gate 	int	c = 0;
8750Sstevel@tonic-gate 	mib_item_t *localp;
8760Sstevel@tonic-gate 	mib_item_t *tempp;
8770Sstevel@tonic-gate 
8780Sstevel@tonic-gate 	for (tempp = item; tempp; tempp = tempp->next_item)
8790Sstevel@tonic-gate 		if (tempp->mib_id == 0)
8800Sstevel@tonic-gate 			c++;
8810Sstevel@tonic-gate 	tempp = NULL;
8820Sstevel@tonic-gate 
8830Sstevel@tonic-gate 	localp = (mib_item_t *)malloc(c * sizeof (mib_item_t));
8840Sstevel@tonic-gate 	if (localp == NULL)
8850Sstevel@tonic-gate 		return (NULL);
8860Sstevel@tonic-gate 	c = 0;
8870Sstevel@tonic-gate 	for (; item; item = item->next_item) {
8880Sstevel@tonic-gate 		if (item->mib_id == 0) {
8890Sstevel@tonic-gate 			/* Replicate item in localp */
8900Sstevel@tonic-gate 			(localp[c]).next_item = NULL;
8910Sstevel@tonic-gate 			(localp[c]).group = item->group;
8920Sstevel@tonic-gate 			(localp[c]).mib_id = item->mib_id;
8930Sstevel@tonic-gate 			(localp[c]).length = item->length;
8940Sstevel@tonic-gate 			(localp[c]).valp = (uintptr_t *)malloc(
8950Sstevel@tonic-gate 			    item->length);
8960Sstevel@tonic-gate 			if ((localp[c]).valp == NULL) {
8970Sstevel@tonic-gate 				mib_item_destroy(&localp);
8980Sstevel@tonic-gate 				return (NULL);
8990Sstevel@tonic-gate 			}
9000Sstevel@tonic-gate 			(void *) memcpy((localp[c]).valp,
9010Sstevel@tonic-gate 			    item->valp,
9020Sstevel@tonic-gate 			    item->length);
9030Sstevel@tonic-gate 			tempp = &(localp[c]);
9040Sstevel@tonic-gate 			if (c > 0)
9050Sstevel@tonic-gate 				(localp[c - 1]).next_item = tempp;
9060Sstevel@tonic-gate 			c++;
9070Sstevel@tonic-gate 		}
9080Sstevel@tonic-gate 	}
9090Sstevel@tonic-gate 	return (localp);
9100Sstevel@tonic-gate }
9110Sstevel@tonic-gate 
9120Sstevel@tonic-gate /*
9130Sstevel@tonic-gate  * mib_item_diff: takes two (mib_item_t *) linked lists
9140Sstevel@tonic-gate  * item1 and item2 and computes the difference between
9150Sstevel@tonic-gate  * differentiable values in item2 against item1 for every
9160Sstevel@tonic-gate  * given member of item2; returns an mib_item_t * linked
9170Sstevel@tonic-gate  * list of diff's, or a copy of item2 if item1 is NULL;
9180Sstevel@tonic-gate  * will return NULL if system out of memory; works only
9190Sstevel@tonic-gate  * for item->mib_id == 0
9200Sstevel@tonic-gate  */
9210Sstevel@tonic-gate static mib_item_t *
9220Sstevel@tonic-gate mib_item_diff(mib_item_t *item1, mib_item_t *item2) {
9230Sstevel@tonic-gate 	int	nitems	= 0; /* no. of items in item2 */
9240Sstevel@tonic-gate 	mib_item_t *tempp2;  /* walking copy of item2 */
9250Sstevel@tonic-gate 	mib_item_t *tempp1;  /* walking copy of item1 */
9260Sstevel@tonic-gate 	mib_item_t *diffp;
9270Sstevel@tonic-gate 	mib_item_t *diffptr; /* walking copy of diffp */
9280Sstevel@tonic-gate 	mib_item_t *prevp = NULL;
9290Sstevel@tonic-gate 
9300Sstevel@tonic-gate 	if (item1 == NULL) {
9310Sstevel@tonic-gate 		diffp = mib_item_dup(item2);
9320Sstevel@tonic-gate 		return (diffp);
9330Sstevel@tonic-gate 	}
9340Sstevel@tonic-gate 
9350Sstevel@tonic-gate 	for (tempp2 = item2;
9360Sstevel@tonic-gate 	    tempp2;
9370Sstevel@tonic-gate 	    tempp2 = tempp2->next_item) {
9380Sstevel@tonic-gate 		if (tempp2->mib_id == 0)
9390Sstevel@tonic-gate 			switch (tempp2->group) {
9400Sstevel@tonic-gate 			/*
9410Sstevel@tonic-gate 			 * upon adding a case here, the same
9420Sstevel@tonic-gate 			 * must also be added in the next
9430Sstevel@tonic-gate 			 * switch statement, alongwith
9440Sstevel@tonic-gate 			 * appropriate code
9450Sstevel@tonic-gate 			 */
9460Sstevel@tonic-gate 			case MIB2_IP:
9470Sstevel@tonic-gate 			case MIB2_IP6:
9480Sstevel@tonic-gate 			case EXPER_DVMRP:
9490Sstevel@tonic-gate 			case EXPER_IGMP:
9500Sstevel@tonic-gate 			case MIB2_ICMP:
9510Sstevel@tonic-gate 			case MIB2_ICMP6:
9520Sstevel@tonic-gate 			case MIB2_TCP:
9530Sstevel@tonic-gate 			case MIB2_UDP:
9540Sstevel@tonic-gate 			case MIB2_SCTP:
9550Sstevel@tonic-gate 			case EXPER_RAWIP:
9560Sstevel@tonic-gate 				nitems++;
9570Sstevel@tonic-gate 			}
9580Sstevel@tonic-gate 	}
9590Sstevel@tonic-gate 	tempp2 = NULL;
9600Sstevel@tonic-gate 	if (nitems == 0) {
9610Sstevel@tonic-gate 		diffp = mib_item_dup(item2);
9620Sstevel@tonic-gate 		return (diffp);
9630Sstevel@tonic-gate 	}
9640Sstevel@tonic-gate 
9650Sstevel@tonic-gate 	diffp = (mib_item_t *)calloc(nitems, sizeof (mib_item_t));
9660Sstevel@tonic-gate 	if (diffp == NULL)
9670Sstevel@tonic-gate 		return (NULL);
9680Sstevel@tonic-gate 	diffptr = diffp;
9690Sstevel@tonic-gate 	/* 'for' loop 1: */
9700Sstevel@tonic-gate 	for (tempp2 = item2; tempp2 != NULL; tempp2 = tempp2->next_item) {
9710Sstevel@tonic-gate 		if (tempp2->mib_id != 0)
9720Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
9730Sstevel@tonic-gate 		/* 'for' loop 2: */
9740Sstevel@tonic-gate 		for (tempp1 = item1; tempp1 != NULL;
9750Sstevel@tonic-gate 		    tempp1 = tempp1->next_item) {
9760Sstevel@tonic-gate 			if (!(tempp1->mib_id == 0 &&
9770Sstevel@tonic-gate 			    tempp1->group == tempp2->group &&
9780Sstevel@tonic-gate 			    tempp1->mib_id == tempp2->mib_id))
9790Sstevel@tonic-gate 				continue; /* 'for' loop 2 */
9800Sstevel@tonic-gate 			/* found comparable data sets */
9810Sstevel@tonic-gate 			if (prevp != NULL)
9820Sstevel@tonic-gate 				prevp->next_item = diffptr;
9830Sstevel@tonic-gate 			switch (tempp2->group) {
9840Sstevel@tonic-gate 			/*
9850Sstevel@tonic-gate 			 * Indenting note: Because of long variable names
9860Sstevel@tonic-gate 			 * in cases MIB2_IP6 and MIB2_ICMP6, their contents
9870Sstevel@tonic-gate 			 * have been indented by one tab space only
9880Sstevel@tonic-gate 			 */
9890Sstevel@tonic-gate 			case MIB2_IP: {
9900Sstevel@tonic-gate 				mib2_ip_t *i2 = (mib2_ip_t *)tempp2->valp;
9910Sstevel@tonic-gate 				mib2_ip_t *i1 = (mib2_ip_t *)tempp1->valp;
9920Sstevel@tonic-gate 				mib2_ip_t *d;
9930Sstevel@tonic-gate 
9940Sstevel@tonic-gate 				diffptr->group = tempp2->group;
9950Sstevel@tonic-gate 				diffptr->mib_id = tempp2->mib_id;
9960Sstevel@tonic-gate 				diffptr->length = tempp2->length;
9970Sstevel@tonic-gate 				d = (mib2_ip_t *)calloc(tempp2->length, 1);
9980Sstevel@tonic-gate 				if (d == NULL)
9990Sstevel@tonic-gate 					goto mibdiff_out_of_memory;
10000Sstevel@tonic-gate 				diffptr->valp = d;
10010Sstevel@tonic-gate 				d->ipForwarding = i2->ipForwarding;
10020Sstevel@tonic-gate 				d->ipDefaultTTL = i2->ipDefaultTTL;
10030Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipInReceives);
10040Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipInHdrErrors);
10050Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipInAddrErrors);
10060Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipInCksumErrs);
10070Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipForwDatagrams);
10080Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipForwProhibits);
10090Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipInUnknownProtos);
10100Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipInDiscards);
10110Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipInDelivers);
10120Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipOutRequests);
10130Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipOutDiscards);
10140Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipOutNoRoutes);
10150Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipReasmTimeout);
10160Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipReasmReqds);
10170Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipReasmOKs);
10180Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipReasmFails);
10190Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipReasmDuplicates);
10200Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipReasmPartDups);
10210Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipFragOKs);
10220Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipFragFails);
10230Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipFragCreates);
10240Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipRoutingDiscards);
10250Sstevel@tonic-gate 				MDIFF(d, i2, i1, tcpInErrs);
10260Sstevel@tonic-gate 				MDIFF(d, i2, i1, udpNoPorts);
10270Sstevel@tonic-gate 				MDIFF(d, i2, i1, udpInCksumErrs);
10280Sstevel@tonic-gate 				MDIFF(d, i2, i1, udpInOverflows);
10290Sstevel@tonic-gate 				MDIFF(d, i2, i1, rawipInOverflows);
10300Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipsecInSucceeded);
10310Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipsecInFailed);
10320Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipInIPv6);
10330Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipOutIPv6);
10340Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipOutSwitchIPv6);
10350Sstevel@tonic-gate 				prevp = diffptr++;
10360Sstevel@tonic-gate 				break;
10370Sstevel@tonic-gate 			}
10380Sstevel@tonic-gate 			case MIB2_IP6: {
10390Sstevel@tonic-gate 			mib2_ipv6IfStatsEntry_t *i2;
10400Sstevel@tonic-gate 			mib2_ipv6IfStatsEntry_t *i1;
10410Sstevel@tonic-gate 			mib2_ipv6IfStatsEntry_t *d;
10420Sstevel@tonic-gate 
10430Sstevel@tonic-gate 			i2 = (mib2_ipv6IfStatsEntry_t *)tempp2->valp;
10440Sstevel@tonic-gate 			i1 = (mib2_ipv6IfStatsEntry_t *)tempp1->valp;
10450Sstevel@tonic-gate 			diffptr->group = tempp2->group;
10460Sstevel@tonic-gate 			diffptr->mib_id = tempp2->mib_id;
10470Sstevel@tonic-gate 			diffptr->length = tempp2->length;
10480Sstevel@tonic-gate 			d = (mib2_ipv6IfStatsEntry_t *)calloc(
10490Sstevel@tonic-gate 			    tempp2->length, 1);
10500Sstevel@tonic-gate 			if (d == NULL)
10510Sstevel@tonic-gate 				goto mibdiff_out_of_memory;
10520Sstevel@tonic-gate 			diffptr->valp = d;
10530Sstevel@tonic-gate 			d->ipv6Forwarding = i2->ipv6Forwarding;
10540Sstevel@tonic-gate 			d->ipv6DefaultHopLimit =
10550Sstevel@tonic-gate 			    i2->ipv6DefaultHopLimit;
10560Sstevel@tonic-gate 
10570Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6InReceives);
10580Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6InHdrErrors);
10590Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6InTooBigErrors);
10600Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6InNoRoutes);
10610Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6InAddrErrors);
10620Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6InUnknownProtos);
10630Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6InTruncatedPkts);
10640Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6InDiscards);
10650Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6InDelivers);
10660Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6OutForwDatagrams);
10670Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6OutRequests);
10680Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6OutDiscards);
10690Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6OutNoRoutes);
10700Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6OutFragOKs);
10710Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6OutFragFails);
10720Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6OutFragCreates);
10730Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6ReasmReqds);
10740Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6ReasmOKs);
10750Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6ReasmFails);
10760Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6InMcastPkts);
10770Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6OutMcastPkts);
10780Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6ReasmDuplicates);
10790Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6ReasmPartDups);
10800Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6ForwProhibits);
10810Sstevel@tonic-gate 			MDIFF(d, i2, i1, udpInCksumErrs);
10820Sstevel@tonic-gate 			MDIFF(d, i2, i1, udpInOverflows);
10830Sstevel@tonic-gate 			MDIFF(d, i2, i1, rawipInOverflows);
10840Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6InIPv4);
10850Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6OutIPv4);
10860Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6OutSwitchIPv4);
10870Sstevel@tonic-gate 			prevp = diffptr++;
10880Sstevel@tonic-gate 			break;
10890Sstevel@tonic-gate 			}
10900Sstevel@tonic-gate 			case EXPER_DVMRP: {
10910Sstevel@tonic-gate 				struct mrtstat *m2;
10920Sstevel@tonic-gate 				struct mrtstat *m1;
10930Sstevel@tonic-gate 				struct mrtstat *d;
10940Sstevel@tonic-gate 
10950Sstevel@tonic-gate 				m2 = (struct mrtstat *)tempp2->valp;
10960Sstevel@tonic-gate 				m1 = (struct mrtstat *)tempp1->valp;
10970Sstevel@tonic-gate 				diffptr->group = tempp2->group;
10980Sstevel@tonic-gate 				diffptr->mib_id = tempp2->mib_id;
10990Sstevel@tonic-gate 				diffptr->length = tempp2->length;
11000Sstevel@tonic-gate 				d = (struct mrtstat *)calloc(tempp2->length, 1);
11010Sstevel@tonic-gate 				if (d == NULL)
11020Sstevel@tonic-gate 					goto mibdiff_out_of_memory;
11030Sstevel@tonic-gate 				diffptr->valp = d;
11040Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_mfc_hits);
11050Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_mfc_misses);
11060Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_fwd_in);
11070Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_fwd_out);
11080Sstevel@tonic-gate 				d->mrts_upcalls = m2->mrts_upcalls;
11090Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_fwd_drop);
11100Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_bad_tunnel);
11110Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_cant_tunnel);
11120Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_wrong_if);
11130Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_upq_ovflw);
11140Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_cache_cleanups);
11150Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_drop_sel);
11160Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_q_overflow);
11170Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_pkt2large);
11180Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_pim_badversion);
11190Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_pim_rcv_badcsum);
11200Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_pim_badregisters);
11210Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_pim_regforwards);
11220Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_pim_regsend_drops);
11230Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_pim_malformed);
11240Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_pim_nomemory);
11250Sstevel@tonic-gate 				prevp = diffptr++;
11260Sstevel@tonic-gate 				break;
11270Sstevel@tonic-gate 			}
11280Sstevel@tonic-gate 			case EXPER_IGMP: {
11290Sstevel@tonic-gate 				struct igmpstat *i2;
11300Sstevel@tonic-gate 				struct igmpstat *i1;
11310Sstevel@tonic-gate 				struct igmpstat *d;
11320Sstevel@tonic-gate 
11330Sstevel@tonic-gate 				i2 = (struct igmpstat *)tempp2->valp;
11340Sstevel@tonic-gate 				i1 = (struct igmpstat *)tempp1->valp;
11350Sstevel@tonic-gate 				diffptr->group = tempp2->group;
11360Sstevel@tonic-gate 				diffptr->mib_id = tempp2->mib_id;
11370Sstevel@tonic-gate 				diffptr->length = tempp2->length;
11380Sstevel@tonic-gate 				d = (struct igmpstat *)calloc(
11390Sstevel@tonic-gate 				    tempp2->length, 1);
11400Sstevel@tonic-gate 				if (d == NULL)
11410Sstevel@tonic-gate 					goto mibdiff_out_of_memory;
11420Sstevel@tonic-gate 				diffptr->valp = d;
11430Sstevel@tonic-gate 				MDIFF(d, i2, i1, igps_rcv_total);
11440Sstevel@tonic-gate 				MDIFF(d, i2, i1, igps_rcv_tooshort);
11450Sstevel@tonic-gate 				MDIFF(d, i2, i1, igps_rcv_badsum);
11460Sstevel@tonic-gate 				MDIFF(d, i2, i1, igps_rcv_queries);
11470Sstevel@tonic-gate 				MDIFF(d, i2, i1, igps_rcv_badqueries);
11480Sstevel@tonic-gate 				MDIFF(d, i2, i1, igps_rcv_reports);
11490Sstevel@tonic-gate 				MDIFF(d, i2, i1, igps_rcv_badreports);
11500Sstevel@tonic-gate 				MDIFF(d, i2, i1, igps_rcv_ourreports);
11510Sstevel@tonic-gate 				MDIFF(d, i2, i1, igps_snd_reports);
11520Sstevel@tonic-gate 				prevp = diffptr++;
11530Sstevel@tonic-gate 				break;
11540Sstevel@tonic-gate 			}
11550Sstevel@tonic-gate 			case MIB2_ICMP: {
11560Sstevel@tonic-gate 				mib2_icmp_t *i2;
11570Sstevel@tonic-gate 				mib2_icmp_t *i1;
11580Sstevel@tonic-gate 				mib2_icmp_t *d;
11590Sstevel@tonic-gate 
11600Sstevel@tonic-gate 				i2 = (mib2_icmp_t *)tempp2->valp;
11610Sstevel@tonic-gate 				i1 = (mib2_icmp_t *)tempp1->valp;
11620Sstevel@tonic-gate 				diffptr->group = tempp2->group;
11630Sstevel@tonic-gate 				diffptr->mib_id = tempp2->mib_id;
11640Sstevel@tonic-gate 				diffptr->length = tempp2->length;
11650Sstevel@tonic-gate 				d = (mib2_icmp_t *)calloc(tempp2->length, 1);
11660Sstevel@tonic-gate 				if (d == NULL)
11670Sstevel@tonic-gate 					goto mibdiff_out_of_memory;
11680Sstevel@tonic-gate 				diffptr->valp = d;
11690Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInMsgs);
11700Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInErrors);
11710Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInCksumErrs);
11720Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInUnknowns);
11730Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInDestUnreachs);
11740Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInTimeExcds);
11750Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInParmProbs);
11760Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInSrcQuenchs);
11770Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInRedirects);
11780Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInBadRedirects);
11790Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInEchos);
11800Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInEchoReps);
11810Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInTimestamps);
11820Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInAddrMasks);
11830Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInAddrMaskReps);
11840Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInFragNeeded);
11850Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutMsgs);
11860Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutDrops);
11870Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutErrors);
11880Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutDestUnreachs);
11890Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutTimeExcds);
11900Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutParmProbs);
11910Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutSrcQuenchs);
11920Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutRedirects);
11930Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutEchos);
11940Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutEchoReps);
11950Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutTimestamps);
11960Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutTimestampReps);
11970Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutAddrMasks);
11980Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutAddrMaskReps);
11990Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutFragNeeded);
12000Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInOverflows);
12010Sstevel@tonic-gate 				prevp = diffptr++;
12020Sstevel@tonic-gate 				break;
12030Sstevel@tonic-gate 			}
12040Sstevel@tonic-gate 			case MIB2_ICMP6: {
12050Sstevel@tonic-gate 	mib2_ipv6IfIcmpEntry_t *i2;
12060Sstevel@tonic-gate 	mib2_ipv6IfIcmpEntry_t *i1;
12070Sstevel@tonic-gate 	mib2_ipv6IfIcmpEntry_t *d;
12080Sstevel@tonic-gate 
12090Sstevel@tonic-gate 	i2 = (mib2_ipv6IfIcmpEntry_t *)tempp2->valp;
12100Sstevel@tonic-gate 	i1 = (mib2_ipv6IfIcmpEntry_t *)tempp1->valp;
12110Sstevel@tonic-gate 	diffptr->group = tempp2->group;
12120Sstevel@tonic-gate 	diffptr->mib_id = tempp2->mib_id;
12130Sstevel@tonic-gate 	diffptr->length = tempp2->length;
12140Sstevel@tonic-gate 	d = (mib2_ipv6IfIcmpEntry_t *)calloc(tempp2->length, 1);
12150Sstevel@tonic-gate 	if (d == NULL)
12160Sstevel@tonic-gate 		goto mibdiff_out_of_memory;
12170Sstevel@tonic-gate 	diffptr->valp = d;
12180Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInMsgs);
12190Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInErrors);
12200Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInDestUnreachs);
12210Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInAdminProhibs);
12220Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInTimeExcds);
12230Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInParmProblems);
12240Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInPktTooBigs);
12250Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInEchos);
12260Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInEchoReplies);
12270Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInRouterSolicits);
12280Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInRouterAdvertisements);
12290Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInNeighborSolicits);
12300Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInNeighborAdvertisements);
12310Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInRedirects);
12320Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInBadRedirects);
12330Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInGroupMembQueries);
12340Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInGroupMembResponses);
12350Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInGroupMembReductions);
12360Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInOverflows);
12370Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutMsgs);
12380Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutErrors);
12390Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutDestUnreachs);
12400Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutAdminProhibs);
12410Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutTimeExcds);
12420Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutParmProblems);
12430Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutPktTooBigs);
12440Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutEchos);
12450Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutEchoReplies);
12460Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutRouterSolicits);
12470Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutRouterAdvertisements);
12480Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutNeighborSolicits);
12490Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutNeighborAdvertisements);
12500Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutRedirects);
12510Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutGroupMembQueries);
12520Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutGroupMembResponses);
12530Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutGroupMembReductions);
12540Sstevel@tonic-gate 	prevp = diffptr++;
12550Sstevel@tonic-gate 	break;
12560Sstevel@tonic-gate 			}
12570Sstevel@tonic-gate 			case MIB2_TCP: {
12580Sstevel@tonic-gate 				mib2_tcp_t *t2;
12590Sstevel@tonic-gate 				mib2_tcp_t *t1;
12600Sstevel@tonic-gate 				mib2_tcp_t *d;
12610Sstevel@tonic-gate 
12620Sstevel@tonic-gate 				t2 = (mib2_tcp_t *)tempp2->valp;
12630Sstevel@tonic-gate 				t1 = (mib2_tcp_t *)tempp1->valp;
12640Sstevel@tonic-gate 				diffptr->group = tempp2->group;
12650Sstevel@tonic-gate 				diffptr->mib_id = tempp2->mib_id;
12660Sstevel@tonic-gate 				diffptr->length = tempp2->length;
12670Sstevel@tonic-gate 				d = (mib2_tcp_t *)calloc(tempp2->length, 1);
12680Sstevel@tonic-gate 				if (d == NULL)
12690Sstevel@tonic-gate 					goto mibdiff_out_of_memory;
12700Sstevel@tonic-gate 				diffptr->valp = d;
12710Sstevel@tonic-gate 				d->tcpRtoMin = t2->tcpRtoMin;
12720Sstevel@tonic-gate 				d->tcpRtoMax = t2->tcpRtoMax;
12730Sstevel@tonic-gate 				d->tcpMaxConn = t2->tcpMaxConn;
12740Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpActiveOpens);
12750Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpPassiveOpens);
12760Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpAttemptFails);
12770Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpEstabResets);
12780Sstevel@tonic-gate 				d->tcpCurrEstab = t2->tcpCurrEstab;
12793284Sapersson 				MDIFF(d, t2, t1, tcpHCOutSegs);
12800Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpOutDataSegs);
12810Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpOutDataBytes);
12820Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpRetransSegs);
12830Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpRetransBytes);
12840Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpOutAck);
12850Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpOutAckDelayed);
12860Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpOutUrg);
12870Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpOutWinUpdate);
12880Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpOutWinProbe);
12890Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpOutControl);
12900Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpOutRsts);
12910Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpOutFastRetrans);
12923284Sapersson 				MDIFF(d, t2, t1, tcpHCInSegs);
12930Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInAckSegs);
12940Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInAckBytes);
12950Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInDupAck);
12960Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInAckUnsent);
12970Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInDataInorderSegs);
12980Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInDataInorderBytes);
12990Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInDataUnorderSegs);
13000Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInDataUnorderBytes);
13010Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInDataDupSegs);
13020Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInDataDupBytes);
13030Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInDataPartDupSegs);
13040Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInDataPartDupBytes);
13050Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInDataPastWinSegs);
13060Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInDataPastWinBytes);
13070Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInWinProbe);
13080Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInWinUpdate);
13090Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInClosed);
13100Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpRttNoUpdate);
13110Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpRttUpdate);
13120Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpTimRetrans);
13130Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpTimRetransDrop);
13140Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpTimKeepalive);
13150Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpTimKeepaliveProbe);
13160Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpTimKeepaliveDrop);
13170Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpListenDrop);
13180Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpListenDropQ0);
13190Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpHalfOpenDrop);
13200Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpOutSackRetransSegs);
13210Sstevel@tonic-gate 				prevp = diffptr++;
13220Sstevel@tonic-gate 				break;
13230Sstevel@tonic-gate 			}
13240Sstevel@tonic-gate 			case MIB2_UDP: {
13250Sstevel@tonic-gate 				mib2_udp_t *u2;
13260Sstevel@tonic-gate 				mib2_udp_t *u1;
13270Sstevel@tonic-gate 				mib2_udp_t *d;
13280Sstevel@tonic-gate 
13290Sstevel@tonic-gate 				u2 = (mib2_udp_t *)tempp2->valp;
13300Sstevel@tonic-gate 				u1 = (mib2_udp_t *)tempp1->valp;
13310Sstevel@tonic-gate 				diffptr->group = tempp2->group;
13320Sstevel@tonic-gate 				diffptr->mib_id = tempp2->mib_id;
13330Sstevel@tonic-gate 				diffptr->length = tempp2->length;
13340Sstevel@tonic-gate 				d = (mib2_udp_t *)calloc(tempp2->length, 1);
13350Sstevel@tonic-gate 				if (d == NULL)
13360Sstevel@tonic-gate 					goto mibdiff_out_of_memory;
13370Sstevel@tonic-gate 				diffptr->valp = d;
13383284Sapersson 				MDIFF(d, u2, u1, udpHCInDatagrams);
13390Sstevel@tonic-gate 				MDIFF(d, u2, u1, udpInErrors);
13403284Sapersson 				MDIFF(d, u2, u1, udpHCOutDatagrams);
13410Sstevel@tonic-gate 				MDIFF(d, u2, u1, udpOutErrors);
13420Sstevel@tonic-gate 				prevp = diffptr++;
13430Sstevel@tonic-gate 				break;
13440Sstevel@tonic-gate 			}
13450Sstevel@tonic-gate 			case MIB2_SCTP: {
13460Sstevel@tonic-gate 				mib2_sctp_t *s2;
13470Sstevel@tonic-gate 				mib2_sctp_t *s1;
13480Sstevel@tonic-gate 				mib2_sctp_t *d;
13490Sstevel@tonic-gate 
13500Sstevel@tonic-gate 				s2 = (mib2_sctp_t *)tempp2->valp;
13510Sstevel@tonic-gate 				s1 = (mib2_sctp_t *)tempp1->valp;
13520Sstevel@tonic-gate 				diffptr->group = tempp2->group;
13530Sstevel@tonic-gate 				diffptr->mib_id = tempp2->mib_id;
13540Sstevel@tonic-gate 				diffptr->length = tempp2->length;
13550Sstevel@tonic-gate 				d = (mib2_sctp_t *)calloc(tempp2->length, 1);
13560Sstevel@tonic-gate 				if (d == NULL)
13570Sstevel@tonic-gate 					goto mibdiff_out_of_memory;
13580Sstevel@tonic-gate 				diffptr->valp = d;
13590Sstevel@tonic-gate 				d->sctpRtoAlgorithm = s2->sctpRtoAlgorithm;
13600Sstevel@tonic-gate 				d->sctpRtoMin = s2->sctpRtoMin;
13610Sstevel@tonic-gate 				d->sctpRtoMax = s2->sctpRtoMax;
13620Sstevel@tonic-gate 				d->sctpRtoInitial = s2->sctpRtoInitial;
13630Sstevel@tonic-gate 				d->sctpMaxAssocs = s2->sctpMaxAssocs;
13640Sstevel@tonic-gate 				d->sctpValCookieLife = s2->sctpValCookieLife;
13650Sstevel@tonic-gate 				d->sctpMaxInitRetr = s2->sctpMaxInitRetr;
13660Sstevel@tonic-gate 				d->sctpCurrEstab = s2->sctpCurrEstab;
13670Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpActiveEstab);
13680Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpPassiveEstab);
13690Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpAborted);
13700Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpShutdowns);
13710Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpOutOfBlue);
13720Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpChecksumError);
13730Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpOutCtrlChunks);
13740Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpOutOrderChunks);
13750Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpOutUnorderChunks);
13760Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpRetransChunks);
13770Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpOutAck);
13780Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpOutAckDelayed);
13790Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpOutWinUpdate);
13800Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpOutFastRetrans);
13810Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpOutWinProbe);
13820Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpInCtrlChunks);
13830Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpInOrderChunks);
13840Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpInUnorderChunks);
13850Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpInAck);
13860Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpInDupAck);
13870Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpInAckUnsent);
13880Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpFragUsrMsgs);
13890Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpReasmUsrMsgs);
13900Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpOutSCTPPkts);
13910Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpInSCTPPkts);
13920Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpInInvalidCookie);
13930Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpTimRetrans);
13940Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpTimRetransDrop);
13950Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpTimHeartBeatProbe);
13960Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpTimHeartBeatDrop);
13970Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpListenDrop);
13980Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpInClosed);
13990Sstevel@tonic-gate 				prevp = diffptr++;
14000Sstevel@tonic-gate 				break;
14010Sstevel@tonic-gate 			}
14020Sstevel@tonic-gate 			case EXPER_RAWIP: {
14030Sstevel@tonic-gate 				mib2_rawip_t *r2;
14040Sstevel@tonic-gate 				mib2_rawip_t *r1;
14050Sstevel@tonic-gate 				mib2_rawip_t *d;
14060Sstevel@tonic-gate 
14070Sstevel@tonic-gate 				r2 = (mib2_rawip_t *)tempp2->valp;
14080Sstevel@tonic-gate 				r1 = (mib2_rawip_t *)tempp1->valp;
14090Sstevel@tonic-gate 				diffptr->group = tempp2->group;
14100Sstevel@tonic-gate 				diffptr->mib_id = tempp2->mib_id;
14110Sstevel@tonic-gate 				diffptr->length = tempp2->length;
14120Sstevel@tonic-gate 				d = (mib2_rawip_t *)calloc(tempp2->length, 1);
14130Sstevel@tonic-gate 				if (d == NULL)
14140Sstevel@tonic-gate 					goto mibdiff_out_of_memory;
14150Sstevel@tonic-gate 				diffptr->valp = d;
14160Sstevel@tonic-gate 				MDIFF(d, r2, r1, rawipInDatagrams);
14170Sstevel@tonic-gate 				MDIFF(d, r2, r1, rawipInErrors);
14180Sstevel@tonic-gate 				MDIFF(d, r2, r1, rawipInCksumErrs);
14190Sstevel@tonic-gate 				MDIFF(d, r2, r1, rawipOutDatagrams);
14200Sstevel@tonic-gate 				MDIFF(d, r2, r1, rawipOutErrors);
14210Sstevel@tonic-gate 				prevp = diffptr++;
14220Sstevel@tonic-gate 				break;
14230Sstevel@tonic-gate 			}
14240Sstevel@tonic-gate 			/*
14250Sstevel@tonic-gate 			 * there are more "group" types but they aren't
14260Sstevel@tonic-gate 			 * required for the -s and -Ms options
14270Sstevel@tonic-gate 			 */
14280Sstevel@tonic-gate 			}
14290Sstevel@tonic-gate 		} /* 'for' loop 2 ends */
14300Sstevel@tonic-gate 		tempp1 = NULL;
14310Sstevel@tonic-gate 	} /* 'for' loop 1 ends */
14320Sstevel@tonic-gate 	tempp2 = NULL;
14330Sstevel@tonic-gate 	diffptr--;
14340Sstevel@tonic-gate 	diffptr->next_item = NULL;
14350Sstevel@tonic-gate 	return (diffp);
14360Sstevel@tonic-gate 
14370Sstevel@tonic-gate mibdiff_out_of_memory:;
14380Sstevel@tonic-gate 	mib_item_destroy(&diffp);
14390Sstevel@tonic-gate 	return (NULL);
14400Sstevel@tonic-gate }
14410Sstevel@tonic-gate 
14420Sstevel@tonic-gate /*
14430Sstevel@tonic-gate  * mib_item_destroy: cleans up a mib_item_t *
14440Sstevel@tonic-gate  * that was created by calling mib_item_dup or
14450Sstevel@tonic-gate  * mib_item_diff
14460Sstevel@tonic-gate  */
14470Sstevel@tonic-gate static void
14480Sstevel@tonic-gate mib_item_destroy(mib_item_t **itemp) {
14490Sstevel@tonic-gate 	int	nitems = 0;
14500Sstevel@tonic-gate 	int	c = 0;
14510Sstevel@tonic-gate 	mib_item_t *tempp;
14520Sstevel@tonic-gate 
14530Sstevel@tonic-gate 	if (itemp == NULL || *itemp == NULL)
14540Sstevel@tonic-gate 		return;
14550Sstevel@tonic-gate 
14560Sstevel@tonic-gate 	for (tempp = *itemp; tempp != NULL; tempp = tempp->next_item)
14570Sstevel@tonic-gate 		if (tempp->mib_id == 0)
14580Sstevel@tonic-gate 			nitems++;
14590Sstevel@tonic-gate 		else
14600Sstevel@tonic-gate 			return;	/* cannot destroy! */
14610Sstevel@tonic-gate 
14620Sstevel@tonic-gate 	if (nitems == 0)
14630Sstevel@tonic-gate 		return;		/* cannot destroy! */
14640Sstevel@tonic-gate 
14650Sstevel@tonic-gate 	for (c = nitems - 1; c >= 0; c--) {
14660Sstevel@tonic-gate 		if ((itemp[0][c]).valp != NULL)
14670Sstevel@tonic-gate 			free((itemp[0][c]).valp);
14680Sstevel@tonic-gate 	}
14690Sstevel@tonic-gate 	free(*itemp);
14700Sstevel@tonic-gate 
14710Sstevel@tonic-gate 	*itemp = NULL;
14720Sstevel@tonic-gate }
14730Sstevel@tonic-gate 
14740Sstevel@tonic-gate /* Compare two Octet_ts.  Return B_TRUE if they match, B_FALSE if not. */
14750Sstevel@tonic-gate static boolean_t
14760Sstevel@tonic-gate octetstrmatch(const Octet_t *a, const Octet_t *b)
14770Sstevel@tonic-gate {
14780Sstevel@tonic-gate 	if (a == NULL || b == NULL)
14790Sstevel@tonic-gate 		return (B_FALSE);
14800Sstevel@tonic-gate 
14810Sstevel@tonic-gate 	if (a->o_length != b->o_length)
14820Sstevel@tonic-gate 		return (B_FALSE);
14830Sstevel@tonic-gate 
14840Sstevel@tonic-gate 	return (memcmp(a->o_bytes, b->o_bytes, a->o_length) == 0);
14850Sstevel@tonic-gate }
14860Sstevel@tonic-gate 
14870Sstevel@tonic-gate /* If octetstr() changes make an appropriate change to STR_EXPAND */
14880Sstevel@tonic-gate static char *
14891676Sjpk octetstr(const Octet_t *op, int code, char *dst, uint_t dstlen)
14900Sstevel@tonic-gate {
14910Sstevel@tonic-gate 	int	i;
14920Sstevel@tonic-gate 	char	*cp;
14930Sstevel@tonic-gate 
14940Sstevel@tonic-gate 	cp = dst;
14950Sstevel@tonic-gate 	if (op) {
14960Sstevel@tonic-gate 		for (i = 0; i < op->o_length; i++) {
14970Sstevel@tonic-gate 			switch (code) {
14980Sstevel@tonic-gate 			case 'd':
14990Sstevel@tonic-gate 				if (cp - dst + 4 > dstlen) {
15000Sstevel@tonic-gate 					*cp = '\0';
15010Sstevel@tonic-gate 					return (dst);
15020Sstevel@tonic-gate 				}
15030Sstevel@tonic-gate 				(void) snprintf(cp, 5, "%d.",
15040Sstevel@tonic-gate 				    0xff & op->o_bytes[i]);
15050Sstevel@tonic-gate 				cp = strchr(cp, '\0');
15060Sstevel@tonic-gate 				break;
15070Sstevel@tonic-gate 			case 'a':
15080Sstevel@tonic-gate 				if (cp - dst + 1 > dstlen) {
15090Sstevel@tonic-gate 					*cp = '\0';
15100Sstevel@tonic-gate 					return (dst);
15110Sstevel@tonic-gate 				}
15120Sstevel@tonic-gate 				*cp++ = op->o_bytes[i];
15130Sstevel@tonic-gate 				break;
15140Sstevel@tonic-gate 			case 'h':
15150Sstevel@tonic-gate 			default:
15160Sstevel@tonic-gate 				if (cp - dst + 3 > dstlen) {
15170Sstevel@tonic-gate 					*cp = '\0';
15180Sstevel@tonic-gate 					return (dst);
15190Sstevel@tonic-gate 				}
15200Sstevel@tonic-gate 				(void) snprintf(cp, 4, "%02x:",
15210Sstevel@tonic-gate 				    0xff & op->o_bytes[i]);
15220Sstevel@tonic-gate 				cp += 3;
15230Sstevel@tonic-gate 				break;
15240Sstevel@tonic-gate 			}
15250Sstevel@tonic-gate 		}
15260Sstevel@tonic-gate 	}
15270Sstevel@tonic-gate 	if (code != 'a' && cp != dst)
15280Sstevel@tonic-gate 		cp--;
15290Sstevel@tonic-gate 	*cp = '\0';
15300Sstevel@tonic-gate 	return (dst);
15310Sstevel@tonic-gate }
15320Sstevel@tonic-gate 
15331676Sjpk static const char *
15341676Sjpk mitcp_state(int state, const mib2_transportMLPEntry_t *attr)
15350Sstevel@tonic-gate {
15361676Sjpk 	static char tcpsbuf[50];
15371676Sjpk 	const char *cp;
15380Sstevel@tonic-gate 
15390Sstevel@tonic-gate 	switch (state) {
15400Sstevel@tonic-gate 	case TCPS_CLOSED:
15410Sstevel@tonic-gate 		cp = "CLOSED";
15420Sstevel@tonic-gate 		break;
15430Sstevel@tonic-gate 	case TCPS_IDLE:
15440Sstevel@tonic-gate 		cp = "IDLE";
15450Sstevel@tonic-gate 		break;
15460Sstevel@tonic-gate 	case TCPS_BOUND:
15470Sstevel@tonic-gate 		cp = "BOUND";
15480Sstevel@tonic-gate 		break;
15490Sstevel@tonic-gate 	case TCPS_LISTEN:
15500Sstevel@tonic-gate 		cp = "LISTEN";
15510Sstevel@tonic-gate 		break;
15520Sstevel@tonic-gate 	case TCPS_SYN_SENT:
15530Sstevel@tonic-gate 		cp = "SYN_SENT";
15540Sstevel@tonic-gate 		break;
15550Sstevel@tonic-gate 	case TCPS_SYN_RCVD:
15560Sstevel@tonic-gate 		cp = "SYN_RCVD";
15570Sstevel@tonic-gate 		break;
15580Sstevel@tonic-gate 	case TCPS_ESTABLISHED:
15590Sstevel@tonic-gate 		cp = "ESTABLISHED";
15600Sstevel@tonic-gate 		break;
15610Sstevel@tonic-gate 	case TCPS_CLOSE_WAIT:
15620Sstevel@tonic-gate 		cp = "CLOSE_WAIT";
15630Sstevel@tonic-gate 		break;
15640Sstevel@tonic-gate 	case TCPS_FIN_WAIT_1:
15650Sstevel@tonic-gate 		cp = "FIN_WAIT_1";
15660Sstevel@tonic-gate 		break;
15670Sstevel@tonic-gate 	case TCPS_CLOSING:
15680Sstevel@tonic-gate 		cp = "CLOSING";
15690Sstevel@tonic-gate 		break;
15700Sstevel@tonic-gate 	case TCPS_LAST_ACK:
15710Sstevel@tonic-gate 		cp = "LAST_ACK";
15720Sstevel@tonic-gate 		break;
15730Sstevel@tonic-gate 	case TCPS_FIN_WAIT_2:
15740Sstevel@tonic-gate 		cp = "FIN_WAIT_2";
15750Sstevel@tonic-gate 		break;
15760Sstevel@tonic-gate 	case TCPS_TIME_WAIT:
15770Sstevel@tonic-gate 		cp = "TIME_WAIT";
15780Sstevel@tonic-gate 		break;
15790Sstevel@tonic-gate 	default:
15800Sstevel@tonic-gate 		(void) snprintf(tcpsbuf, sizeof (tcpsbuf),
15810Sstevel@tonic-gate 		    "UnknownState(%d)", state);
15820Sstevel@tonic-gate 		cp = tcpsbuf;
15830Sstevel@tonic-gate 		break;
15840Sstevel@tonic-gate 	}
15851676Sjpk 
15861676Sjpk 	if (RSECflag && attr != NULL && attr->tme_flags != 0) {
15871676Sjpk 		if (cp != tcpsbuf) {
15881676Sjpk 			(void) strlcpy(tcpsbuf, cp, sizeof (tcpsbuf));
15891676Sjpk 			cp = tcpsbuf;
15901676Sjpk 		}
15911676Sjpk 		if (attr->tme_flags & MIB2_TMEF_PRIVATE)
15921676Sjpk 			(void) strlcat(tcpsbuf, " P", sizeof (tcpsbuf));
15931676Sjpk 		if (attr->tme_flags & MIB2_TMEF_SHARED)
15941676Sjpk 			(void) strlcat(tcpsbuf, " S", sizeof (tcpsbuf));
15951676Sjpk 	}
15961676Sjpk 
15971676Sjpk 	return (cp);
15981676Sjpk }
15991676Sjpk 
16001676Sjpk static const char *
16011676Sjpk miudp_state(int state, const mib2_transportMLPEntry_t *attr)
16021676Sjpk {
16031676Sjpk 	static char udpsbuf[50];
16041676Sjpk 	const char *cp;
16051676Sjpk 
16061676Sjpk 	switch (state) {
16071676Sjpk 	case MIB2_UDP_unbound:
16081676Sjpk 		cp = "Unbound";
16091676Sjpk 		break;
16101676Sjpk 	case MIB2_UDP_idle:
16111676Sjpk 		cp = "Idle";
16121676Sjpk 		break;
16131676Sjpk 	case MIB2_UDP_connected:
16141676Sjpk 		cp = "Connected";
16151676Sjpk 		break;
16161676Sjpk 	default:
16171676Sjpk 		(void) snprintf(udpsbuf, sizeof (udpsbuf),
16181676Sjpk 		    "Unknown State(%d)", state);
16191676Sjpk 		cp = udpsbuf;
16201676Sjpk 		break;
16211676Sjpk 	}
16221676Sjpk 
16231676Sjpk 	if (RSECflag && attr != NULL && attr->tme_flags != 0) {
16241676Sjpk 		if (cp != udpsbuf) {
16251676Sjpk 			(void) strlcpy(udpsbuf, cp, sizeof (udpsbuf));
16261676Sjpk 			cp = udpsbuf;
16271676Sjpk 		}
16281676Sjpk 		if (attr->tme_flags & MIB2_TMEF_PRIVATE)
16291676Sjpk 			(void) strlcat(udpsbuf, " P", sizeof (udpsbuf));
16301676Sjpk 		if (attr->tme_flags & MIB2_TMEF_SHARED)
16311676Sjpk 			(void) strlcat(udpsbuf, " S", sizeof (udpsbuf));
16321676Sjpk 	}
16331676Sjpk 
16340Sstevel@tonic-gate 	return (cp);
16350Sstevel@tonic-gate }
16360Sstevel@tonic-gate 
16370Sstevel@tonic-gate static int odd;
16380Sstevel@tonic-gate 
16390Sstevel@tonic-gate static void
16400Sstevel@tonic-gate prval_init(void)
16410Sstevel@tonic-gate {
16420Sstevel@tonic-gate 	odd = 0;
16430Sstevel@tonic-gate }
16440Sstevel@tonic-gate 
16450Sstevel@tonic-gate static void
16460Sstevel@tonic-gate prval(char *str, Counter val)
16470Sstevel@tonic-gate {
16480Sstevel@tonic-gate 	(void) printf("\t%-20s=%6u", str, val);
16490Sstevel@tonic-gate 	if (odd++ & 1)
16500Sstevel@tonic-gate 		(void) putchar('\n');
16510Sstevel@tonic-gate }
16520Sstevel@tonic-gate 
16530Sstevel@tonic-gate static void
16540Sstevel@tonic-gate prval64(char *str, Counter64 val)
16550Sstevel@tonic-gate {
16560Sstevel@tonic-gate 	(void) printf("\t%-20s=%6llu", str, val);
16570Sstevel@tonic-gate 	if (odd++ & 1)
16580Sstevel@tonic-gate 		(void) putchar('\n');
16590Sstevel@tonic-gate }
16600Sstevel@tonic-gate 
16610Sstevel@tonic-gate static void
16620Sstevel@tonic-gate pr_int_val(char *str, int val)
16630Sstevel@tonic-gate {
16640Sstevel@tonic-gate 	(void) printf("\t%-20s=%6d", str, val);
16650Sstevel@tonic-gate 	if (odd++ & 1)
16660Sstevel@tonic-gate 		(void) putchar('\n');
16670Sstevel@tonic-gate }
16680Sstevel@tonic-gate 
16690Sstevel@tonic-gate static void
16700Sstevel@tonic-gate pr_sctp_rtoalgo(char *str, int val)
16710Sstevel@tonic-gate {
16720Sstevel@tonic-gate 	(void) printf("\t%-20s=", str);
16730Sstevel@tonic-gate 	switch (val) {
16740Sstevel@tonic-gate 		case MIB2_SCTP_RTOALGO_OTHER:
16750Sstevel@tonic-gate 			(void) printf("%6.6s", "other");
16760Sstevel@tonic-gate 			break;
16770Sstevel@tonic-gate 
16780Sstevel@tonic-gate 		case MIB2_SCTP_RTOALGO_VANJ:
16790Sstevel@tonic-gate 			(void) printf("%6.6s", "vanj");
16800Sstevel@tonic-gate 			break;
16810Sstevel@tonic-gate 
16820Sstevel@tonic-gate 		default:
16830Sstevel@tonic-gate 			(void) printf("%6d", val);
16840Sstevel@tonic-gate 			break;
16850Sstevel@tonic-gate 	}
16860Sstevel@tonic-gate 	if (odd++ & 1)
16870Sstevel@tonic-gate 		(void) putchar('\n');
16880Sstevel@tonic-gate }
16890Sstevel@tonic-gate 
16900Sstevel@tonic-gate static void
16910Sstevel@tonic-gate prval_end(void)
16920Sstevel@tonic-gate {
16930Sstevel@tonic-gate 	if (odd++ & 1)
16940Sstevel@tonic-gate 		(void) putchar('\n');
16950Sstevel@tonic-gate }
16960Sstevel@tonic-gate 
16970Sstevel@tonic-gate /* Extract constant sizes */
16980Sstevel@tonic-gate static void
16990Sstevel@tonic-gate mib_get_constants(mib_item_t *item)
17000Sstevel@tonic-gate {
17010Sstevel@tonic-gate 	/* 'for' loop 1: */
17020Sstevel@tonic-gate 	for (; item; item = item->next_item) {
17030Sstevel@tonic-gate 		if (item->mib_id != 0)
17040Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
17050Sstevel@tonic-gate 
17060Sstevel@tonic-gate 		switch (item->group) {
17070Sstevel@tonic-gate 		case MIB2_IP: {
17080Sstevel@tonic-gate 			mib2_ip_t	*ip = (mib2_ip_t *)item->valp;
17090Sstevel@tonic-gate 
17100Sstevel@tonic-gate 			ipAddrEntrySize = ip->ipAddrEntrySize;
17110Sstevel@tonic-gate 			ipRouteEntrySize = ip->ipRouteEntrySize;
17120Sstevel@tonic-gate 			ipNetToMediaEntrySize = ip->ipNetToMediaEntrySize;
17130Sstevel@tonic-gate 			ipMemberEntrySize = ip->ipMemberEntrySize;
17140Sstevel@tonic-gate 			ipGroupSourceEntrySize = ip->ipGroupSourceEntrySize;
17151676Sjpk 			ipRouteAttributeSize = ip->ipRouteAttributeSize;
17161676Sjpk 			transportMLPSize = ip->transportMLPSize;
17170Sstevel@tonic-gate 			assert(IS_P2ALIGNED(ipAddrEntrySize,
17180Sstevel@tonic-gate 			    sizeof (mib2_ipAddrEntry_t *)) &&
17190Sstevel@tonic-gate 			    IS_P2ALIGNED(ipRouteEntrySize,
17200Sstevel@tonic-gate 				sizeof (mib2_ipRouteEntry_t *)) &&
17210Sstevel@tonic-gate 			    IS_P2ALIGNED(ipNetToMediaEntrySize,
17220Sstevel@tonic-gate 				sizeof (mib2_ipNetToMediaEntry_t *)) &&
17230Sstevel@tonic-gate 			    IS_P2ALIGNED(ipMemberEntrySize,
17240Sstevel@tonic-gate 				sizeof (ip_member_t *)) &&
17250Sstevel@tonic-gate 			    IS_P2ALIGNED(ipGroupSourceEntrySize,
17261676Sjpk 				sizeof (ip_grpsrc_t *)) &&
17271676Sjpk 			    IS_P2ALIGNED(ipRouteAttributeSize,
17281676Sjpk 				sizeof (mib2_ipAttributeEntry_t *)) &&
17291676Sjpk 			    IS_P2ALIGNED(transportMLPSize,
17301676Sjpk 				sizeof (mib2_transportMLPEntry_t *)));
17310Sstevel@tonic-gate 			break;
17320Sstevel@tonic-gate 		}
17330Sstevel@tonic-gate 		case EXPER_DVMRP: {
17340Sstevel@tonic-gate 			struct mrtstat	*mrts = (struct mrtstat *)item->valp;
17350Sstevel@tonic-gate 
17360Sstevel@tonic-gate 			vifctlSize = mrts->mrts_vifctlSize;
17370Sstevel@tonic-gate 			mfcctlSize = mrts->mrts_mfcctlSize;
17380Sstevel@tonic-gate 			assert(IS_P2ALIGNED(vifctlSize,
17390Sstevel@tonic-gate 			    sizeof (struct vifclt *)) &&
17400Sstevel@tonic-gate 			    IS_P2ALIGNED(mfcctlSize, sizeof (struct mfcctl *)));
17410Sstevel@tonic-gate 			break;
17420Sstevel@tonic-gate 		}
17430Sstevel@tonic-gate 		case MIB2_IP6: {
17440Sstevel@tonic-gate 			mib2_ipv6IfStatsEntry_t *ip6;
17450Sstevel@tonic-gate 			/* Just use the first entry */
17460Sstevel@tonic-gate 
17470Sstevel@tonic-gate 			ip6 = (mib2_ipv6IfStatsEntry_t *)item->valp;
17480Sstevel@tonic-gate 			ipv6IfStatsEntrySize = ip6->ipv6IfStatsEntrySize;
17490Sstevel@tonic-gate 			ipv6AddrEntrySize = ip6->ipv6AddrEntrySize;
17500Sstevel@tonic-gate 			ipv6RouteEntrySize = ip6->ipv6RouteEntrySize;
17510Sstevel@tonic-gate 			ipv6NetToMediaEntrySize = ip6->ipv6NetToMediaEntrySize;
17520Sstevel@tonic-gate 			ipv6MemberEntrySize = ip6->ipv6MemberEntrySize;
17530Sstevel@tonic-gate 			ipv6GroupSourceEntrySize =
17540Sstevel@tonic-gate 			    ip6->ipv6GroupSourceEntrySize;
17550Sstevel@tonic-gate 			assert(IS_P2ALIGNED(ipv6IfStatsEntrySize,
17560Sstevel@tonic-gate 			    sizeof (mib2_ipv6IfStatsEntry_t *)) &&
17570Sstevel@tonic-gate 			    IS_P2ALIGNED(ipv6AddrEntrySize,
17580Sstevel@tonic-gate 				sizeof (mib2_ipv6AddrEntry_t *)) &&
17590Sstevel@tonic-gate 			    IS_P2ALIGNED(ipv6RouteEntrySize,
17600Sstevel@tonic-gate 				sizeof (mib2_ipv6RouteEntry_t *)) &&
17610Sstevel@tonic-gate 			    IS_P2ALIGNED(ipv6NetToMediaEntrySize,
17620Sstevel@tonic-gate 				sizeof (mib2_ipv6NetToMediaEntry_t *)) &&
17630Sstevel@tonic-gate 			    IS_P2ALIGNED(ipv6MemberEntrySize,
17640Sstevel@tonic-gate 				sizeof (ipv6_member_t *)) &&
17650Sstevel@tonic-gate 			    IS_P2ALIGNED(ipv6GroupSourceEntrySize,
17660Sstevel@tonic-gate 				sizeof (ipv6_grpsrc_t *)));
17670Sstevel@tonic-gate 			break;
17680Sstevel@tonic-gate 		}
17690Sstevel@tonic-gate 		case MIB2_ICMP6: {
17700Sstevel@tonic-gate 			mib2_ipv6IfIcmpEntry_t *icmp6;
17710Sstevel@tonic-gate 			/* Just use the first entry */
17720Sstevel@tonic-gate 
17730Sstevel@tonic-gate 			icmp6 = (mib2_ipv6IfIcmpEntry_t *)item->valp;
17740Sstevel@tonic-gate 			ipv6IfIcmpEntrySize = icmp6->ipv6IfIcmpEntrySize;
17750Sstevel@tonic-gate 			assert(IS_P2ALIGNED(ipv6IfIcmpEntrySize,
17760Sstevel@tonic-gate 			    sizeof (mib2_ipv6IfIcmpEntry_t *)));
17770Sstevel@tonic-gate 			break;
17780Sstevel@tonic-gate 		}
17790Sstevel@tonic-gate 		case MIB2_TCP: {
17800Sstevel@tonic-gate 			mib2_tcp_t	*tcp = (mib2_tcp_t *)item->valp;
17810Sstevel@tonic-gate 
17820Sstevel@tonic-gate 			tcpConnEntrySize = tcp->tcpConnTableSize;
17830Sstevel@tonic-gate 			tcp6ConnEntrySize = tcp->tcp6ConnTableSize;
17840Sstevel@tonic-gate 			assert(IS_P2ALIGNED(tcpConnEntrySize,
17850Sstevel@tonic-gate 			    sizeof (mib2_tcpConnEntry_t *)) &&
17860Sstevel@tonic-gate 			    IS_P2ALIGNED(tcp6ConnEntrySize,
17870Sstevel@tonic-gate 				sizeof (mib2_tcp6ConnEntry_t *)));
17880Sstevel@tonic-gate 			break;
17890Sstevel@tonic-gate 		}
17900Sstevel@tonic-gate 		case MIB2_UDP: {
17910Sstevel@tonic-gate 			mib2_udp_t	*udp = (mib2_udp_t *)item->valp;
17920Sstevel@tonic-gate 
17930Sstevel@tonic-gate 			udpEntrySize = udp->udpEntrySize;
17940Sstevel@tonic-gate 			udp6EntrySize = udp->udp6EntrySize;
17950Sstevel@tonic-gate 			assert(IS_P2ALIGNED(udpEntrySize,
17960Sstevel@tonic-gate 			    sizeof (mib2_udpEntry_t *)) &&
17970Sstevel@tonic-gate 			    IS_P2ALIGNED(udp6EntrySize,
17980Sstevel@tonic-gate 				sizeof (mib2_udp6Entry_t *)));
17990Sstevel@tonic-gate 			break;
18000Sstevel@tonic-gate 		}
18010Sstevel@tonic-gate 		case MIB2_SCTP: {
18020Sstevel@tonic-gate 			mib2_sctp_t	*sctp = (mib2_sctp_t *)item->valp;
18030Sstevel@tonic-gate 
18040Sstevel@tonic-gate 			sctpEntrySize = sctp->sctpEntrySize;
18050Sstevel@tonic-gate 			sctpLocalEntrySize = sctp->sctpLocalEntrySize;
18060Sstevel@tonic-gate 			sctpRemoteEntrySize = sctp->sctpRemoteEntrySize;
18070Sstevel@tonic-gate 			break;
18080Sstevel@tonic-gate 		}
18090Sstevel@tonic-gate 		}
18100Sstevel@tonic-gate 	} /* 'for' loop 1 ends */
18110Sstevel@tonic-gate 
18120Sstevel@tonic-gate 	if (Dflag) {
18130Sstevel@tonic-gate 		(void) puts("mib_get_constants:");
18140Sstevel@tonic-gate 		(void) printf("\tipv6IfStatsEntrySize %d\n",
18150Sstevel@tonic-gate 		    ipv6IfStatsEntrySize);
18160Sstevel@tonic-gate 		(void) printf("\tipAddrEntrySize %d\n", ipAddrEntrySize);
18170Sstevel@tonic-gate 		(void) printf("\tipRouteEntrySize %d\n", ipRouteEntrySize);
18180Sstevel@tonic-gate 		(void) printf("\tipNetToMediaEntrySize %d\n",
18190Sstevel@tonic-gate 		    ipNetToMediaEntrySize);
18200Sstevel@tonic-gate 		(void) printf("\tipMemberEntrySize %d\n", ipMemberEntrySize);
18211676Sjpk 		(void) printf("\tipRouteAttributeSize %d\n",
18221676Sjpk 		    ipRouteAttributeSize);
18230Sstevel@tonic-gate 		(void) printf("\tvifctlSize %d\n", vifctlSize);
18240Sstevel@tonic-gate 		(void) printf("\tmfcctlSize %d\n", mfcctlSize);
18250Sstevel@tonic-gate 
18260Sstevel@tonic-gate 		(void) printf("\tipv6AddrEntrySize %d\n", ipv6AddrEntrySize);
18270Sstevel@tonic-gate 		(void) printf("\tipv6RouteEntrySize %d\n", ipv6RouteEntrySize);
18280Sstevel@tonic-gate 		(void) printf("\tipv6NetToMediaEntrySize %d\n",
18290Sstevel@tonic-gate 		    ipv6NetToMediaEntrySize);
18300Sstevel@tonic-gate 		(void) printf("\tipv6MemberEntrySize %d\n",
18310Sstevel@tonic-gate 		    ipv6MemberEntrySize);
18320Sstevel@tonic-gate 		(void) printf("\tipv6IfIcmpEntrySize %d\n",
18330Sstevel@tonic-gate 		    ipv6IfIcmpEntrySize);
18341676Sjpk 		(void) printf("\ttransportMLPSize %d\n", transportMLPSize);
18350Sstevel@tonic-gate 		(void) printf("\ttcpConnEntrySize %d\n", tcpConnEntrySize);
18360Sstevel@tonic-gate 		(void) printf("\ttcp6ConnEntrySize %d\n", tcp6ConnEntrySize);
18370Sstevel@tonic-gate 		(void) printf("\tudpEntrySize %d\n", udpEntrySize);
18380Sstevel@tonic-gate 		(void) printf("\tudp6EntrySize %d\n", udp6EntrySize);
18390Sstevel@tonic-gate 		(void) printf("\tsctpEntrySize %d\n", sctpEntrySize);
18400Sstevel@tonic-gate 		(void) printf("\tsctpLocalEntrySize %d\n", sctpLocalEntrySize);
18410Sstevel@tonic-gate 		(void) printf("\tsctpRemoteEntrySize %d\n",
18420Sstevel@tonic-gate 		    sctpRemoteEntrySize);
18430Sstevel@tonic-gate 	}
18440Sstevel@tonic-gate }
18450Sstevel@tonic-gate 
18460Sstevel@tonic-gate 
18470Sstevel@tonic-gate /* ----------------------------- STAT_REPORT ------------------------------- */
18480Sstevel@tonic-gate 
18490Sstevel@tonic-gate static void
18500Sstevel@tonic-gate stat_report(mib_item_t *item)
18510Sstevel@tonic-gate {
18520Sstevel@tonic-gate 	int	jtemp = 0;
18530Sstevel@tonic-gate 	char	ifname[LIFNAMSIZ + 1];
18540Sstevel@tonic-gate 	char	*ifnamep;
18550Sstevel@tonic-gate 
18560Sstevel@tonic-gate 	/* 'for' loop 1: */
18570Sstevel@tonic-gate 	for (; item; item = item->next_item) {
18580Sstevel@tonic-gate 		if (Dflag) {
18590Sstevel@tonic-gate 			(void) printf("\n--- Entry %d ---\n", ++jtemp);
18600Sstevel@tonic-gate 			(void) printf("Group = %d, mib_id = %d, "
18610Sstevel@tonic-gate 			    "length = %d, valp = 0x%p\n",
18620Sstevel@tonic-gate 			    item->group, item->mib_id,
18630Sstevel@tonic-gate 			    item->length, item->valp);
18640Sstevel@tonic-gate 		}
18650Sstevel@tonic-gate 		if (item->mib_id != 0)
18660Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
18670Sstevel@tonic-gate 
18680Sstevel@tonic-gate 		switch (item->group) {
18690Sstevel@tonic-gate 		case MIB2_IP: {
18700Sstevel@tonic-gate 			mib2_ip_t	*ip = (mib2_ip_t *)item->valp;
18710Sstevel@tonic-gate 
18720Sstevel@tonic-gate 			if (protocol_selected(IPPROTO_IP) &&
18730Sstevel@tonic-gate 			    family_selected(AF_INET)) {
18740Sstevel@tonic-gate 				(void) fputs(v4compat ? "\nIP" : "\nIPv4",
18750Sstevel@tonic-gate 				    stdout);
18760Sstevel@tonic-gate 				print_ip_stats(ip);
18770Sstevel@tonic-gate 			}
18780Sstevel@tonic-gate 			break;
18790Sstevel@tonic-gate 		}
18800Sstevel@tonic-gate 		case MIB2_ICMP: {
18810Sstevel@tonic-gate 			mib2_icmp_t	*icmp =
18820Sstevel@tonic-gate 			    (mib2_icmp_t *)item->valp;
18830Sstevel@tonic-gate 
18840Sstevel@tonic-gate 			if (protocol_selected(IPPROTO_ICMP) &&
18850Sstevel@tonic-gate 			    family_selected(AF_INET)) {
18860Sstevel@tonic-gate 				(void) fputs(v4compat ? "\nICMP" : "\nICMPv4",
18870Sstevel@tonic-gate 				    stdout);
18880Sstevel@tonic-gate 				print_icmp_stats(icmp);
18890Sstevel@tonic-gate 			}
18900Sstevel@tonic-gate 			break;
18910Sstevel@tonic-gate 		}
18920Sstevel@tonic-gate 		case MIB2_IP6: {
18930Sstevel@tonic-gate 			mib2_ipv6IfStatsEntry_t *ip6;
18940Sstevel@tonic-gate 			mib2_ipv6IfStatsEntry_t sum6;
18950Sstevel@tonic-gate 
18960Sstevel@tonic-gate 			if (!(protocol_selected(IPPROTO_IPV6)) ||
18970Sstevel@tonic-gate 			    !(family_selected(AF_INET6)))
18980Sstevel@tonic-gate 				break;
18990Sstevel@tonic-gate 			bzero(&sum6, sizeof (sum6));
19000Sstevel@tonic-gate 			/* 'for' loop 2a: */
19010Sstevel@tonic-gate 			for (ip6 = (mib2_ipv6IfStatsEntry_t *)item->valp;
19020Sstevel@tonic-gate 			    (char *)ip6 < (char *)item->valp
19030Sstevel@tonic-gate 			    + item->length;
19040Sstevel@tonic-gate 			    /* LINTED: (note 1) */
19050Sstevel@tonic-gate 			    ip6 = (mib2_ipv6IfStatsEntry_t *)((char *)ip6 +
19060Sstevel@tonic-gate 			    ipv6IfStatsEntrySize)) {
19070Sstevel@tonic-gate 
19080Sstevel@tonic-gate 				if (ip6->ipv6IfIndex == 0) {
19090Sstevel@tonic-gate 					/*
19100Sstevel@tonic-gate 					 * The "unknown interface" ip6
19110Sstevel@tonic-gate 					 * mib. Just add to the sum.
19120Sstevel@tonic-gate 					 */
19130Sstevel@tonic-gate 					sum_ip6_stats(ip6, &sum6);
19140Sstevel@tonic-gate 					continue; /* 'for' loop 2a */
19150Sstevel@tonic-gate 				}
19160Sstevel@tonic-gate 				ifnamep = if_indextoname(
19170Sstevel@tonic-gate 				    ip6->ipv6IfIndex,
19180Sstevel@tonic-gate 				    ifname);
19190Sstevel@tonic-gate 				if (ifnamep == NULL) {
19200Sstevel@tonic-gate 					(void) printf(
19210Sstevel@tonic-gate 					    "Invalid ifindex %d\n",
19220Sstevel@tonic-gate 					    ip6->ipv6IfIndex);
19230Sstevel@tonic-gate 					continue; /* 'for' loop 2a */
19240Sstevel@tonic-gate 				}
19250Sstevel@tonic-gate 
19260Sstevel@tonic-gate 				if (Aflag) {
19270Sstevel@tonic-gate 					(void) printf("\nIPv6 for %s\n",
19280Sstevel@tonic-gate 					    ifnamep);
19290Sstevel@tonic-gate 					print_ip6_stats(ip6);
19300Sstevel@tonic-gate 				}
19310Sstevel@tonic-gate 				sum_ip6_stats(ip6, &sum6);
19320Sstevel@tonic-gate 			} /* 'for' loop 2a ends */
19330Sstevel@tonic-gate 			(void) fputs("\nIPv6", stdout);
19340Sstevel@tonic-gate 			print_ip6_stats(&sum6);
19350Sstevel@tonic-gate 			break;
19360Sstevel@tonic-gate 		}
19370Sstevel@tonic-gate 		case MIB2_ICMP6: {
19380Sstevel@tonic-gate 			mib2_ipv6IfIcmpEntry_t *icmp6;
19390Sstevel@tonic-gate 			mib2_ipv6IfIcmpEntry_t sum6;
19400Sstevel@tonic-gate 
19410Sstevel@tonic-gate 			if (!(protocol_selected(IPPROTO_ICMPV6)) ||
19420Sstevel@tonic-gate 			    !(family_selected(AF_INET6)))
19430Sstevel@tonic-gate 				break;
19440Sstevel@tonic-gate 			bzero(&sum6, sizeof (sum6));
19450Sstevel@tonic-gate 			/* 'for' loop 2b: */
19460Sstevel@tonic-gate 			for (icmp6 =
19470Sstevel@tonic-gate 			    (mib2_ipv6IfIcmpEntry_t *)item->valp;
19480Sstevel@tonic-gate 			    (char *)icmp6 < (char *)item->valp
19490Sstevel@tonic-gate 				+ item->length;
19500Sstevel@tonic-gate 			    icmp6 =
19510Sstevel@tonic-gate 				/* LINTED: (note 1) */
19520Sstevel@tonic-gate 				(mib2_ipv6IfIcmpEntry_t *)((char *)icmp6
19530Sstevel@tonic-gate 			    + ipv6IfIcmpEntrySize)) {
19540Sstevel@tonic-gate 
19550Sstevel@tonic-gate 				if (icmp6->ipv6IfIcmpIfIndex == 0) {
19560Sstevel@tonic-gate 					/*
19570Sstevel@tonic-gate 					 * The "unknown interface" icmp6
19580Sstevel@tonic-gate 					 * mib. Just add to the sum.
19590Sstevel@tonic-gate 					 */
19600Sstevel@tonic-gate 					sum_icmp6_stats(icmp6, &sum6);
19610Sstevel@tonic-gate 					continue; /* 'for' loop 2b: */
19620Sstevel@tonic-gate 				}
19630Sstevel@tonic-gate 				ifnamep = if_indextoname(
19640Sstevel@tonic-gate 				    icmp6->ipv6IfIcmpIfIndex, ifname);
19650Sstevel@tonic-gate 				if (ifnamep == NULL) {
19660Sstevel@tonic-gate 					(void) printf(
19670Sstevel@tonic-gate 					    "Invalid ifindex %d\n",
19680Sstevel@tonic-gate 					    icmp6->ipv6IfIcmpIfIndex);
19690Sstevel@tonic-gate 					continue; /* 'for' loop 2b: */
19700Sstevel@tonic-gate 				}
19710Sstevel@tonic-gate 
19720Sstevel@tonic-gate 				if (Aflag) {
19730Sstevel@tonic-gate 					(void) printf(
19740Sstevel@tonic-gate 					    "\nICMPv6 for %s\n",
19750Sstevel@tonic-gate 					    ifnamep);
19760Sstevel@tonic-gate 					print_icmp6_stats(icmp6);
19770Sstevel@tonic-gate 				}
19780Sstevel@tonic-gate 				sum_icmp6_stats(icmp6, &sum6);
19790Sstevel@tonic-gate 			} /* 'for' loop 2b ends */
19800Sstevel@tonic-gate 			(void) fputs("\nICMPv6", stdout);
19810Sstevel@tonic-gate 			print_icmp6_stats(&sum6);
19820Sstevel@tonic-gate 			break;
19830Sstevel@tonic-gate 		}
19840Sstevel@tonic-gate 		case MIB2_TCP: {
19850Sstevel@tonic-gate 			mib2_tcp_t	*tcp = (mib2_tcp_t *)item->valp;
19860Sstevel@tonic-gate 
19870Sstevel@tonic-gate 			if (protocol_selected(IPPROTO_TCP) &&
19880Sstevel@tonic-gate 			    (family_selected(AF_INET) ||
19890Sstevel@tonic-gate 			    family_selected(AF_INET6))) {
19900Sstevel@tonic-gate 				(void) fputs("\nTCP", stdout);
19910Sstevel@tonic-gate 				print_tcp_stats(tcp);
19920Sstevel@tonic-gate 			}
19930Sstevel@tonic-gate 			break;
19940Sstevel@tonic-gate 		}
19950Sstevel@tonic-gate 		case MIB2_UDP: {
19960Sstevel@tonic-gate 			mib2_udp_t	*udp = (mib2_udp_t *)item->valp;
19970Sstevel@tonic-gate 
19980Sstevel@tonic-gate 			if (protocol_selected(IPPROTO_UDP) &&
19990Sstevel@tonic-gate 			    (family_selected(AF_INET) ||
20000Sstevel@tonic-gate 			    family_selected(AF_INET6))) {
20010Sstevel@tonic-gate 				(void) fputs("\nUDP", stdout);
20020Sstevel@tonic-gate 				print_udp_stats(udp);
20030Sstevel@tonic-gate 			}
20040Sstevel@tonic-gate 			break;
20050Sstevel@tonic-gate 		}
20060Sstevel@tonic-gate 		case MIB2_SCTP: {
20070Sstevel@tonic-gate 			mib2_sctp_t	*sctp = (mib2_sctp_t *)item->valp;
20080Sstevel@tonic-gate 
20090Sstevel@tonic-gate 			if (protocol_selected(IPPROTO_SCTP) &&
20100Sstevel@tonic-gate 			    (family_selected(AF_INET) ||
20110Sstevel@tonic-gate 			    family_selected(AF_INET6))) {
20120Sstevel@tonic-gate 				(void) fputs("\nSCTP", stdout);
20130Sstevel@tonic-gate 				print_sctp_stats(sctp);
20140Sstevel@tonic-gate 			}
20150Sstevel@tonic-gate 			break;
20160Sstevel@tonic-gate 		}
20170Sstevel@tonic-gate 		case EXPER_RAWIP: {
20180Sstevel@tonic-gate 			mib2_rawip_t	*rawip =
20190Sstevel@tonic-gate 			    (mib2_rawip_t *)item->valp;
20200Sstevel@tonic-gate 
20210Sstevel@tonic-gate 			if (protocol_selected(IPPROTO_RAW) &&
20220Sstevel@tonic-gate 			    (family_selected(AF_INET) ||
20230Sstevel@tonic-gate 			    family_selected(AF_INET6))) {
20240Sstevel@tonic-gate 				(void) fputs("\nRAWIP", stdout);
20250Sstevel@tonic-gate 				print_rawip_stats(rawip);
20260Sstevel@tonic-gate 			}
20270Sstevel@tonic-gate 			break;
20280Sstevel@tonic-gate 		}
20290Sstevel@tonic-gate 		case EXPER_IGMP: {
20300Sstevel@tonic-gate 			struct igmpstat	*igps =
20310Sstevel@tonic-gate 			    (struct igmpstat *)item->valp;
20320Sstevel@tonic-gate 
20330Sstevel@tonic-gate 			if (protocol_selected(IPPROTO_IGMP) &&
20340Sstevel@tonic-gate 			    (family_selected(AF_INET))) {
20350Sstevel@tonic-gate 				(void) fputs("\nIGMP:\n", stdout);
20360Sstevel@tonic-gate 				print_igmp_stats(igps);
20370Sstevel@tonic-gate 			}
20380Sstevel@tonic-gate 			break;
20390Sstevel@tonic-gate 		}
20400Sstevel@tonic-gate 		}
20410Sstevel@tonic-gate 	} /* 'for' loop 1 ends */
20420Sstevel@tonic-gate 	(void) putchar('\n');
20430Sstevel@tonic-gate 	(void) fflush(stdout);
20440Sstevel@tonic-gate }
20450Sstevel@tonic-gate 
20460Sstevel@tonic-gate static void
20470Sstevel@tonic-gate print_ip_stats(mib2_ip_t *ip)
20480Sstevel@tonic-gate {
20490Sstevel@tonic-gate 	prval_init();
20500Sstevel@tonic-gate 	pr_int_val("ipForwarding",	ip->ipForwarding);
20510Sstevel@tonic-gate 	pr_int_val("ipDefaultTTL",	ip->ipDefaultTTL);
20520Sstevel@tonic-gate 	prval("ipInReceives",		ip->ipInReceives);
20530Sstevel@tonic-gate 	prval("ipInHdrErrors",		ip->ipInHdrErrors);
20540Sstevel@tonic-gate 	prval("ipInAddrErrors",		ip->ipInAddrErrors);
20550Sstevel@tonic-gate 	prval("ipInCksumErrs",		ip->ipInCksumErrs);
20560Sstevel@tonic-gate 	prval("ipForwDatagrams",	ip->ipForwDatagrams);
20570Sstevel@tonic-gate 	prval("ipForwProhibits",	ip->ipForwProhibits);
20580Sstevel@tonic-gate 	prval("ipInUnknownProtos",	ip->ipInUnknownProtos);
20590Sstevel@tonic-gate 	prval("ipInDiscards",		ip->ipInDiscards);
20600Sstevel@tonic-gate 	prval("ipInDelivers",		ip->ipInDelivers);
20610Sstevel@tonic-gate 	prval("ipOutRequests",		ip->ipOutRequests);
20620Sstevel@tonic-gate 	prval("ipOutDiscards",		ip->ipOutDiscards);
20630Sstevel@tonic-gate 	prval("ipOutNoRoutes",		ip->ipOutNoRoutes);
20640Sstevel@tonic-gate 	pr_int_val("ipReasmTimeout",	ip->ipReasmTimeout);
20650Sstevel@tonic-gate 	prval("ipReasmReqds",		ip->ipReasmReqds);
20660Sstevel@tonic-gate 	prval("ipReasmOKs",		ip->ipReasmOKs);
20670Sstevel@tonic-gate 	prval("ipReasmFails",		ip->ipReasmFails);
20680Sstevel@tonic-gate 	prval("ipReasmDuplicates",	ip->ipReasmDuplicates);
20690Sstevel@tonic-gate 	prval("ipReasmPartDups",	ip->ipReasmPartDups);
20700Sstevel@tonic-gate 	prval("ipFragOKs",		ip->ipFragOKs);
20710Sstevel@tonic-gate 	prval("ipFragFails",		ip->ipFragFails);
20720Sstevel@tonic-gate 	prval("ipFragCreates",		ip->ipFragCreates);
20730Sstevel@tonic-gate 	prval("ipRoutingDiscards",	ip->ipRoutingDiscards);
20740Sstevel@tonic-gate 
20750Sstevel@tonic-gate 	prval("tcpInErrs",		ip->tcpInErrs);
20760Sstevel@tonic-gate 	prval("udpNoPorts",		ip->udpNoPorts);
20770Sstevel@tonic-gate 	prval("udpInCksumErrs",		ip->udpInCksumErrs);
20780Sstevel@tonic-gate 	prval("udpInOverflows",		ip->udpInOverflows);
20790Sstevel@tonic-gate 	prval("rawipInOverflows",	ip->rawipInOverflows);
20800Sstevel@tonic-gate 	prval("ipsecInSucceeded",	ip->ipsecInSucceeded);
20810Sstevel@tonic-gate 	prval("ipsecInFailed",		ip->ipsecInFailed);
20820Sstevel@tonic-gate 	prval("ipInIPv6",		ip->ipInIPv6);
20830Sstevel@tonic-gate 	prval("ipOutIPv6",		ip->ipOutIPv6);
20840Sstevel@tonic-gate 	prval("ipOutSwitchIPv6",	ip->ipOutSwitchIPv6);
20850Sstevel@tonic-gate 	prval_end();
20860Sstevel@tonic-gate }
20870Sstevel@tonic-gate 
20880Sstevel@tonic-gate static void
20890Sstevel@tonic-gate print_icmp_stats(mib2_icmp_t *icmp)
20900Sstevel@tonic-gate {
20910Sstevel@tonic-gate 	prval_init();
20920Sstevel@tonic-gate 	prval("icmpInMsgs",		icmp->icmpInMsgs);
20930Sstevel@tonic-gate 	prval("icmpInErrors",		icmp->icmpInErrors);
20940Sstevel@tonic-gate 	prval("icmpInCksumErrs",	icmp->icmpInCksumErrs);
20950Sstevel@tonic-gate 	prval("icmpInUnknowns",		icmp->icmpInUnknowns);
20960Sstevel@tonic-gate 	prval("icmpInDestUnreachs",	icmp->icmpInDestUnreachs);
20970Sstevel@tonic-gate 	prval("icmpInTimeExcds",	icmp->icmpInTimeExcds);
20980Sstevel@tonic-gate 	prval("icmpInParmProbs",	icmp->icmpInParmProbs);
20990Sstevel@tonic-gate 	prval("icmpInSrcQuenchs",	icmp->icmpInSrcQuenchs);
21000Sstevel@tonic-gate 	prval("icmpInRedirects",	icmp->icmpInRedirects);
21010Sstevel@tonic-gate 	prval("icmpInBadRedirects",	icmp->icmpInBadRedirects);
21020Sstevel@tonic-gate 	prval("icmpInEchos",		icmp->icmpInEchos);
21030Sstevel@tonic-gate 	prval("icmpInEchoReps",		icmp->icmpInEchoReps);
21040Sstevel@tonic-gate 	prval("icmpInTimestamps",	icmp->icmpInTimestamps);
21050Sstevel@tonic-gate 	prval("icmpInTimestampReps",	icmp->icmpInTimestampReps);
21060Sstevel@tonic-gate 	prval("icmpInAddrMasks",	icmp->icmpInAddrMasks);
21070Sstevel@tonic-gate 	prval("icmpInAddrMaskReps",	icmp->icmpInAddrMaskReps);
21080Sstevel@tonic-gate 	prval("icmpInFragNeeded",	icmp->icmpInFragNeeded);
21090Sstevel@tonic-gate 	prval("icmpOutMsgs",		icmp->icmpOutMsgs);
21100Sstevel@tonic-gate 	prval("icmpOutDrops",		icmp->icmpOutDrops);
21110Sstevel@tonic-gate 	prval("icmpOutErrors",		icmp->icmpOutErrors);
21120Sstevel@tonic-gate 	prval("icmpOutDestUnreachs",	icmp->icmpOutDestUnreachs);
21130Sstevel@tonic-gate 	prval("icmpOutTimeExcds",	icmp->icmpOutTimeExcds);
21140Sstevel@tonic-gate 	prval("icmpOutParmProbs",	icmp->icmpOutParmProbs);
21150Sstevel@tonic-gate 	prval("icmpOutSrcQuenchs",	icmp->icmpOutSrcQuenchs);
21160Sstevel@tonic-gate 	prval("icmpOutRedirects",	icmp->icmpOutRedirects);
21170Sstevel@tonic-gate 	prval("icmpOutEchos",		icmp->icmpOutEchos);
21180Sstevel@tonic-gate 	prval("icmpOutEchoReps",	icmp->icmpOutEchoReps);
21190Sstevel@tonic-gate 	prval("icmpOutTimestamps",	icmp->icmpOutTimestamps);
21200Sstevel@tonic-gate 	prval("icmpOutTimestampReps",	icmp->icmpOutTimestampReps);
21210Sstevel@tonic-gate 	prval("icmpOutAddrMasks",	icmp->icmpOutAddrMasks);
21220Sstevel@tonic-gate 	prval("icmpOutAddrMaskReps",	icmp->icmpOutAddrMaskReps);
21230Sstevel@tonic-gate 	prval("icmpOutFragNeeded",	icmp->icmpOutFragNeeded);
21240Sstevel@tonic-gate 	prval("icmpInOverflows",	icmp->icmpInOverflows);
21250Sstevel@tonic-gate 	prval_end();
21260Sstevel@tonic-gate }
21270Sstevel@tonic-gate 
21280Sstevel@tonic-gate static void
21290Sstevel@tonic-gate print_ip6_stats(mib2_ipv6IfStatsEntry_t *ip6)
21300Sstevel@tonic-gate {
21310Sstevel@tonic-gate 	prval_init();
21320Sstevel@tonic-gate 	prval("ipv6Forwarding",		ip6->ipv6Forwarding);
21330Sstevel@tonic-gate 	prval("ipv6DefaultHopLimit",	ip6->ipv6DefaultHopLimit);
21340Sstevel@tonic-gate 
21350Sstevel@tonic-gate 	prval("ipv6InReceives",		ip6->ipv6InReceives);
21360Sstevel@tonic-gate 	prval("ipv6InHdrErrors",	ip6->ipv6InHdrErrors);
21370Sstevel@tonic-gate 	prval("ipv6InTooBigErrors",	ip6->ipv6InTooBigErrors);
21380Sstevel@tonic-gate 	prval("ipv6InNoRoutes",		ip6->ipv6InNoRoutes);
21390Sstevel@tonic-gate 	prval("ipv6InAddrErrors",	ip6->ipv6InAddrErrors);
21400Sstevel@tonic-gate 	prval("ipv6InUnknownProtos",	ip6->ipv6InUnknownProtos);
21410Sstevel@tonic-gate 	prval("ipv6InTruncatedPkts",	ip6->ipv6InTruncatedPkts);
21420Sstevel@tonic-gate 	prval("ipv6InDiscards",		ip6->ipv6InDiscards);
21430Sstevel@tonic-gate 	prval("ipv6InDelivers",		ip6->ipv6InDelivers);
21440Sstevel@tonic-gate 	prval("ipv6OutForwDatagrams",	ip6->ipv6OutForwDatagrams);
21450Sstevel@tonic-gate 	prval("ipv6OutRequests",	ip6->ipv6OutRequests);
21460Sstevel@tonic-gate 	prval("ipv6OutDiscards",	ip6->ipv6OutDiscards);
21470Sstevel@tonic-gate 	prval("ipv6OutNoRoutes",	ip6->ipv6OutNoRoutes);
21480Sstevel@tonic-gate 	prval("ipv6OutFragOKs",		ip6->ipv6OutFragOKs);
21490Sstevel@tonic-gate 	prval("ipv6OutFragFails",	ip6->ipv6OutFragFails);
21500Sstevel@tonic-gate 	prval("ipv6OutFragCreates",	ip6->ipv6OutFragCreates);
21510Sstevel@tonic-gate 	prval("ipv6ReasmReqds",		ip6->ipv6ReasmReqds);
21520Sstevel@tonic-gate 	prval("ipv6ReasmOKs",		ip6->ipv6ReasmOKs);
21530Sstevel@tonic-gate 	prval("ipv6ReasmFails",		ip6->ipv6ReasmFails);
21540Sstevel@tonic-gate 	prval("ipv6InMcastPkts",	ip6->ipv6InMcastPkts);
21550Sstevel@tonic-gate 	prval("ipv6OutMcastPkts",	ip6->ipv6OutMcastPkts);
21560Sstevel@tonic-gate 	prval("ipv6ReasmDuplicates",	ip6->ipv6ReasmDuplicates);
21570Sstevel@tonic-gate 	prval("ipv6ReasmPartDups",	ip6->ipv6ReasmPartDups);
21580Sstevel@tonic-gate 	prval("ipv6ForwProhibits",	ip6->ipv6ForwProhibits);
21590Sstevel@tonic-gate 	prval("udpInCksumErrs",		ip6->udpInCksumErrs);
21600Sstevel@tonic-gate 	prval("udpInOverflows",		ip6->udpInOverflows);
21610Sstevel@tonic-gate 	prval("rawipInOverflows",	ip6->rawipInOverflows);
21620Sstevel@tonic-gate 	prval("ipv6InIPv4",		ip6->ipv6InIPv4);
21630Sstevel@tonic-gate 	prval("ipv6OutIPv4",		ip6->ipv6OutIPv4);
21640Sstevel@tonic-gate 	prval("ipv6OutSwitchIPv4",	ip6->ipv6OutSwitchIPv4);
21650Sstevel@tonic-gate 	prval_end();
21660Sstevel@tonic-gate }
21670Sstevel@tonic-gate 
21680Sstevel@tonic-gate static void
21690Sstevel@tonic-gate print_icmp6_stats(mib2_ipv6IfIcmpEntry_t *icmp6)
21700Sstevel@tonic-gate {
21710Sstevel@tonic-gate 	prval_init();
21720Sstevel@tonic-gate 	prval("icmp6InMsgs",		icmp6->ipv6IfIcmpInMsgs);
21730Sstevel@tonic-gate 	prval("icmp6InErrors",		icmp6->ipv6IfIcmpInErrors);
21740Sstevel@tonic-gate 	prval("icmp6InDestUnreachs",	icmp6->ipv6IfIcmpInDestUnreachs);
21750Sstevel@tonic-gate 	prval("icmp6InAdminProhibs",	icmp6->ipv6IfIcmpInAdminProhibs);
21760Sstevel@tonic-gate 	prval("icmp6InTimeExcds",	icmp6->ipv6IfIcmpInTimeExcds);
21770Sstevel@tonic-gate 	prval("icmp6InParmProblems",	icmp6->ipv6IfIcmpInParmProblems);
21780Sstevel@tonic-gate 	prval("icmp6InPktTooBigs",	icmp6->ipv6IfIcmpInPktTooBigs);
21790Sstevel@tonic-gate 	prval("icmp6InEchos",		icmp6->ipv6IfIcmpInEchos);
21800Sstevel@tonic-gate 	prval("icmp6InEchoReplies",	icmp6->ipv6IfIcmpInEchoReplies);
21810Sstevel@tonic-gate 	prval("icmp6InRouterSols",	icmp6->ipv6IfIcmpInRouterSolicits);
21820Sstevel@tonic-gate 	prval("icmp6InRouterAds",
21830Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpInRouterAdvertisements);
21840Sstevel@tonic-gate 	prval("icmp6InNeighborSols",	icmp6->ipv6IfIcmpInNeighborSolicits);
21850Sstevel@tonic-gate 	prval("icmp6InNeighborAds",
21860Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpInNeighborAdvertisements);
21870Sstevel@tonic-gate 	prval("icmp6InRedirects",	icmp6->ipv6IfIcmpInRedirects);
21880Sstevel@tonic-gate 	prval("icmp6InBadRedirects",	icmp6->ipv6IfIcmpInBadRedirects);
21890Sstevel@tonic-gate 	prval("icmp6InGroupQueries",	icmp6->ipv6IfIcmpInGroupMembQueries);
21900Sstevel@tonic-gate 	prval("icmp6InGroupResps",	icmp6->ipv6IfIcmpInGroupMembResponses);
21910Sstevel@tonic-gate 	prval("icmp6InGroupReds",	icmp6->ipv6IfIcmpInGroupMembReductions);
21920Sstevel@tonic-gate 	prval("icmp6InOverflows",	icmp6->ipv6IfIcmpInOverflows);
21930Sstevel@tonic-gate 	prval_end();
21940Sstevel@tonic-gate 	prval_init();
21950Sstevel@tonic-gate 	prval("icmp6OutMsgs",		icmp6->ipv6IfIcmpOutMsgs);
21960Sstevel@tonic-gate 	prval("icmp6OutErrors",		icmp6->ipv6IfIcmpOutErrors);
21970Sstevel@tonic-gate 	prval("icmp6OutDestUnreachs",	icmp6->ipv6IfIcmpOutDestUnreachs);
21980Sstevel@tonic-gate 	prval("icmp6OutAdminProhibs",	icmp6->ipv6IfIcmpOutAdminProhibs);
21990Sstevel@tonic-gate 	prval("icmp6OutTimeExcds",	icmp6->ipv6IfIcmpOutTimeExcds);
22000Sstevel@tonic-gate 	prval("icmp6OutParmProblems",	icmp6->ipv6IfIcmpOutParmProblems);
22010Sstevel@tonic-gate 	prval("icmp6OutPktTooBigs",	icmp6->ipv6IfIcmpOutPktTooBigs);
22020Sstevel@tonic-gate 	prval("icmp6OutEchos",		icmp6->ipv6IfIcmpOutEchos);
22030Sstevel@tonic-gate 	prval("icmp6OutEchoReplies",	icmp6->ipv6IfIcmpOutEchoReplies);
22040Sstevel@tonic-gate 	prval("icmp6OutRouterSols",	icmp6->ipv6IfIcmpOutRouterSolicits);
22050Sstevel@tonic-gate 	prval("icmp6OutRouterAds",
22060Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpOutRouterAdvertisements);
22070Sstevel@tonic-gate 	prval("icmp6OutNeighborSols",	icmp6->ipv6IfIcmpOutNeighborSolicits);
22080Sstevel@tonic-gate 	prval("icmp6OutNeighborAds",
22090Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpOutNeighborAdvertisements);
22100Sstevel@tonic-gate 	prval("icmp6OutRedirects",	icmp6->ipv6IfIcmpOutRedirects);
22110Sstevel@tonic-gate 	prval("icmp6OutGroupQueries",	icmp6->ipv6IfIcmpOutGroupMembQueries);
22120Sstevel@tonic-gate 	prval("icmp6OutGroupResps",
22130Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpOutGroupMembResponses);
22140Sstevel@tonic-gate 	prval("icmp6OutGroupReds",
22150Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpOutGroupMembReductions);
22160Sstevel@tonic-gate 	prval_end();
22170Sstevel@tonic-gate }
22180Sstevel@tonic-gate 
22190Sstevel@tonic-gate static void
22200Sstevel@tonic-gate print_sctp_stats(mib2_sctp_t *sctp)
22210Sstevel@tonic-gate {
22220Sstevel@tonic-gate 	prval_init();
22230Sstevel@tonic-gate 	pr_sctp_rtoalgo("sctpRtoAlgorithm", sctp->sctpRtoAlgorithm);
22240Sstevel@tonic-gate 	prval("sctpRtoMin",		sctp->sctpRtoMin);
22250Sstevel@tonic-gate 	prval("sctpRtoMax",		sctp->sctpRtoMax);
22260Sstevel@tonic-gate 	prval("sctpRtoInitial",		sctp->sctpRtoInitial);
22270Sstevel@tonic-gate 	pr_int_val("sctpMaxAssocs",	sctp->sctpMaxAssocs);
22280Sstevel@tonic-gate 	prval("sctpValCookieLife",	sctp->sctpValCookieLife);
22290Sstevel@tonic-gate 	prval("sctpMaxInitRetr",	sctp->sctpMaxInitRetr);
22300Sstevel@tonic-gate 	prval("sctpCurrEstab",		sctp->sctpCurrEstab);
22310Sstevel@tonic-gate 	prval("sctpActiveEstab",	sctp->sctpActiveEstab);
22320Sstevel@tonic-gate 	prval("sctpPassiveEstab",	sctp->sctpPassiveEstab);
22330Sstevel@tonic-gate 	prval("sctpAborted",		sctp->sctpAborted);
22340Sstevel@tonic-gate 	prval("sctpShutdowns",		sctp->sctpShutdowns);
22350Sstevel@tonic-gate 	prval("sctpOutOfBlue",		sctp->sctpOutOfBlue);
22360Sstevel@tonic-gate 	prval("sctpChecksumError",	sctp->sctpChecksumError);
22370Sstevel@tonic-gate 	prval64("sctpOutCtrlChunks",	sctp->sctpOutCtrlChunks);
22380Sstevel@tonic-gate 	prval64("sctpOutOrderChunks",	sctp->sctpOutOrderChunks);
22390Sstevel@tonic-gate 	prval64("sctpOutUnorderChunks",	sctp->sctpOutUnorderChunks);
22400Sstevel@tonic-gate 	prval64("sctpRetransChunks",	sctp->sctpRetransChunks);
22410Sstevel@tonic-gate 	prval("sctpOutAck",		sctp->sctpOutAck);
22420Sstevel@tonic-gate 	prval("sctpOutAckDelayed",	sctp->sctpOutAckDelayed);
22430Sstevel@tonic-gate 	prval("sctpOutWinUpdate",	sctp->sctpOutWinUpdate);
22440Sstevel@tonic-gate 	prval("sctpOutFastRetrans",	sctp->sctpOutFastRetrans);
22450Sstevel@tonic-gate 	prval("sctpOutWinProbe",	sctp->sctpOutWinProbe);
22460Sstevel@tonic-gate 	prval64("sctpInCtrlChunks",	sctp->sctpInCtrlChunks);
22470Sstevel@tonic-gate 	prval64("sctpInOrderChunks",	sctp->sctpInOrderChunks);
22480Sstevel@tonic-gate 	prval64("sctpInUnorderChunks",	sctp->sctpInUnorderChunks);
22490Sstevel@tonic-gate 	prval("sctpInAck",		sctp->sctpInAck);
22500Sstevel@tonic-gate 	prval("sctpInDupAck",		sctp->sctpInDupAck);
22510Sstevel@tonic-gate 	prval("sctpInAckUnsent",	sctp->sctpInAckUnsent);
22520Sstevel@tonic-gate 	prval64("sctpFragUsrMsgs",	sctp->sctpFragUsrMsgs);
22530Sstevel@tonic-gate 	prval64("sctpReasmUsrMsgs",	sctp->sctpReasmUsrMsgs);
22540Sstevel@tonic-gate 	prval64("sctpOutSCTPPkts",	sctp->sctpOutSCTPPkts);
22550Sstevel@tonic-gate 	prval64("sctpInSCTPPkts",	sctp->sctpInSCTPPkts);
22560Sstevel@tonic-gate 	prval("sctpInInvalidCookie",	sctp->sctpInInvalidCookie);
22570Sstevel@tonic-gate 	prval("sctpTimRetrans",		sctp->sctpTimRetrans);
22580Sstevel@tonic-gate 	prval("sctpTimRetransDrop",	sctp->sctpTimRetransDrop);
22590Sstevel@tonic-gate 	prval("sctpTimHearBeatProbe",	sctp->sctpTimHeartBeatProbe);
22600Sstevel@tonic-gate 	prval("sctpTimHearBeatDrop",	sctp->sctpTimHeartBeatDrop);
22610Sstevel@tonic-gate 	prval("sctpListenDrop",		sctp->sctpListenDrop);
22620Sstevel@tonic-gate 	prval("sctpInClosed",		sctp->sctpInClosed);
22630Sstevel@tonic-gate 	prval_end();
22640Sstevel@tonic-gate }
22650Sstevel@tonic-gate 
22660Sstevel@tonic-gate static void
22670Sstevel@tonic-gate print_tcp_stats(mib2_tcp_t *tcp)
22680Sstevel@tonic-gate {
22690Sstevel@tonic-gate 	prval_init();
22700Sstevel@tonic-gate 	pr_int_val("tcpRtoAlgorithm",	tcp->tcpRtoAlgorithm);
22710Sstevel@tonic-gate 	pr_int_val("tcpRtoMin",		tcp->tcpRtoMin);
22720Sstevel@tonic-gate 	pr_int_val("tcpRtoMax",		tcp->tcpRtoMax);
22730Sstevel@tonic-gate 	pr_int_val("tcpMaxConn",	tcp->tcpMaxConn);
22740Sstevel@tonic-gate 	prval("tcpActiveOpens",		tcp->tcpActiveOpens);
22750Sstevel@tonic-gate 	prval("tcpPassiveOpens",	tcp->tcpPassiveOpens);
22760Sstevel@tonic-gate 	prval("tcpAttemptFails",	tcp->tcpAttemptFails);
22770Sstevel@tonic-gate 	prval("tcpEstabResets",		tcp->tcpEstabResets);
22780Sstevel@tonic-gate 	prval("tcpCurrEstab",		tcp->tcpCurrEstab);
22793284Sapersson 	prval64("tcpOutSegs",		tcp->tcpHCOutSegs);
22800Sstevel@tonic-gate 	prval("tcpOutDataSegs",		tcp->tcpOutDataSegs);
22810Sstevel@tonic-gate 	prval("tcpOutDataBytes",	tcp->tcpOutDataBytes);
22820Sstevel@tonic-gate 	prval("tcpRetransSegs",		tcp->tcpRetransSegs);
22830Sstevel@tonic-gate 	prval("tcpRetransBytes",	tcp->tcpRetransBytes);
22840Sstevel@tonic-gate 	prval("tcpOutAck",		tcp->tcpOutAck);
22850Sstevel@tonic-gate 	prval("tcpOutAckDelayed",	tcp->tcpOutAckDelayed);
22860Sstevel@tonic-gate 	prval("tcpOutUrg",		tcp->tcpOutUrg);
22870Sstevel@tonic-gate 	prval("tcpOutWinUpdate",	tcp->tcpOutWinUpdate);
22880Sstevel@tonic-gate 	prval("tcpOutWinProbe",		tcp->tcpOutWinProbe);
22890Sstevel@tonic-gate 	prval("tcpOutControl",		tcp->tcpOutControl);
22900Sstevel@tonic-gate 	prval("tcpOutRsts",		tcp->tcpOutRsts);
22910Sstevel@tonic-gate 	prval("tcpOutFastRetrans",	tcp->tcpOutFastRetrans);
22923284Sapersson 	prval64("tcpInSegs",		tcp->tcpHCInSegs);
22930Sstevel@tonic-gate 	prval_end();
22940Sstevel@tonic-gate 	prval("tcpInAckSegs",		tcp->tcpInAckSegs);
22950Sstevel@tonic-gate 	prval("tcpInAckBytes",		tcp->tcpInAckBytes);
22960Sstevel@tonic-gate 	prval("tcpInDupAck",		tcp->tcpInDupAck);
22970Sstevel@tonic-gate 	prval("tcpInAckUnsent",		tcp->tcpInAckUnsent);
22980Sstevel@tonic-gate 	prval("tcpInInorderSegs",	tcp->tcpInDataInorderSegs);
22990Sstevel@tonic-gate 	prval("tcpInInorderBytes",	tcp->tcpInDataInorderBytes);
23000Sstevel@tonic-gate 	prval("tcpInUnorderSegs",	tcp->tcpInDataUnorderSegs);
23010Sstevel@tonic-gate 	prval("tcpInUnorderBytes",	tcp->tcpInDataUnorderBytes);
23020Sstevel@tonic-gate 	prval("tcpInDupSegs",		tcp->tcpInDataDupSegs);
23030Sstevel@tonic-gate 	prval("tcpInDupBytes",		tcp->tcpInDataDupBytes);
23040Sstevel@tonic-gate 	prval("tcpInPartDupSegs",	tcp->tcpInDataPartDupSegs);
23050Sstevel@tonic-gate 	prval("tcpInPartDupBytes",	tcp->tcpInDataPartDupBytes);
23060Sstevel@tonic-gate 	prval("tcpInPastWinSegs",	tcp->tcpInDataPastWinSegs);
23070Sstevel@tonic-gate 	prval("tcpInPastWinBytes",	tcp->tcpInDataPastWinBytes);
23080Sstevel@tonic-gate 	prval("tcpInWinProbe",		tcp->tcpInWinProbe);
23090Sstevel@tonic-gate 	prval("tcpInWinUpdate",		tcp->tcpInWinUpdate);
23100Sstevel@tonic-gate 	prval("tcpInClosed",		tcp->tcpInClosed);
23110Sstevel@tonic-gate 	prval("tcpRttNoUpdate",		tcp->tcpRttNoUpdate);
23120Sstevel@tonic-gate 	prval("tcpRttUpdate",		tcp->tcpRttUpdate);
23130Sstevel@tonic-gate 	prval("tcpTimRetrans",		tcp->tcpTimRetrans);
23140Sstevel@tonic-gate 	prval("tcpTimRetransDrop",	tcp->tcpTimRetransDrop);
23150Sstevel@tonic-gate 	prval("tcpTimKeepalive",	tcp->tcpTimKeepalive);
23160Sstevel@tonic-gate 	prval("tcpTimKeepaliveProbe",	tcp->tcpTimKeepaliveProbe);
23170Sstevel@tonic-gate 	prval("tcpTimKeepaliveDrop",	tcp->tcpTimKeepaliveDrop);
23180Sstevel@tonic-gate 	prval("tcpListenDrop",		tcp->tcpListenDrop);
23190Sstevel@tonic-gate 	prval("tcpListenDropQ0",	tcp->tcpListenDropQ0);
23200Sstevel@tonic-gate 	prval("tcpHalfOpenDrop",	tcp->tcpHalfOpenDrop);
23210Sstevel@tonic-gate 	prval("tcpOutSackRetrans",	tcp->tcpOutSackRetransSegs);
23220Sstevel@tonic-gate 	prval_end();
23230Sstevel@tonic-gate 
23240Sstevel@tonic-gate }
23250Sstevel@tonic-gate 
23260Sstevel@tonic-gate static void
23270Sstevel@tonic-gate print_udp_stats(mib2_udp_t *udp)
23280Sstevel@tonic-gate {
23290Sstevel@tonic-gate 	prval_init();
23303284Sapersson 	prval64("udpInDatagrams",	udp->udpHCInDatagrams);
23310Sstevel@tonic-gate 	prval("udpInErrors",		udp->udpInErrors);
23323284Sapersson 	prval64("udpOutDatagrams",	udp->udpHCOutDatagrams);
23330Sstevel@tonic-gate 	prval("udpOutErrors",		udp->udpOutErrors);
23340Sstevel@tonic-gate 	prval_end();
23350Sstevel@tonic-gate }
23360Sstevel@tonic-gate 
23370Sstevel@tonic-gate static void
23380Sstevel@tonic-gate print_rawip_stats(mib2_rawip_t *rawip)
23390Sstevel@tonic-gate {
23400Sstevel@tonic-gate 	prval_init();
23410Sstevel@tonic-gate 	prval("rawipInDatagrams",	rawip->rawipInDatagrams);
23420Sstevel@tonic-gate 	prval("rawipInErrors",		rawip->rawipInErrors);
23430Sstevel@tonic-gate 	prval("rawipInCksumErrs",	rawip->rawipInCksumErrs);
23440Sstevel@tonic-gate 	prval("rawipOutDatagrams",	rawip->rawipOutDatagrams);
23450Sstevel@tonic-gate 	prval("rawipOutErrors",		rawip->rawipOutErrors);
23460Sstevel@tonic-gate 	prval_end();
23470Sstevel@tonic-gate }
23480Sstevel@tonic-gate 
23490Sstevel@tonic-gate void
23500Sstevel@tonic-gate print_igmp_stats(struct igmpstat *igps)
23510Sstevel@tonic-gate {
23520Sstevel@tonic-gate 	(void) printf(" %10u message%s received\n",
23530Sstevel@tonic-gate 	    igps->igps_rcv_total, PLURAL(igps->igps_rcv_total));
23540Sstevel@tonic-gate 	(void) printf(" %10u message%s received with too few bytes\n",
23550Sstevel@tonic-gate 	    igps->igps_rcv_tooshort, PLURAL(igps->igps_rcv_tooshort));
23560Sstevel@tonic-gate 	(void) printf(" %10u message%s received with bad checksum\n",
23570Sstevel@tonic-gate 	    igps->igps_rcv_badsum, PLURAL(igps->igps_rcv_badsum));
23580Sstevel@tonic-gate 	(void) printf(" %10u membership quer%s received\n",
23590Sstevel@tonic-gate 	    igps->igps_rcv_queries, PLURALY(igps->igps_rcv_queries));
23600Sstevel@tonic-gate 	(void) printf(" %10u membership quer%s received with invalid "
23610Sstevel@tonic-gate 	    "field(s)\n",
23620Sstevel@tonic-gate 	    igps->igps_rcv_badqueries, PLURALY(igps->igps_rcv_badqueries));
23630Sstevel@tonic-gate 	(void) printf(" %10u membership report%s received\n",
23640Sstevel@tonic-gate 	    igps->igps_rcv_reports, PLURAL(igps->igps_rcv_reports));
23650Sstevel@tonic-gate 	(void) printf(" %10u membership report%s received with invalid "
23660Sstevel@tonic-gate 	    "field(s)\n",
23670Sstevel@tonic-gate 	    igps->igps_rcv_badreports, PLURAL(igps->igps_rcv_badreports));
23680Sstevel@tonic-gate 	(void) printf(" %10u membership report%s received for groups to "
23690Sstevel@tonic-gate 	    "which we belong\n",
23700Sstevel@tonic-gate 	    igps->igps_rcv_ourreports, PLURAL(igps->igps_rcv_ourreports));
23710Sstevel@tonic-gate 	(void) printf(" %10u membership report%s sent\n",
23720Sstevel@tonic-gate 	    igps->igps_snd_reports, PLURAL(igps->igps_snd_reports));
23730Sstevel@tonic-gate }
23740Sstevel@tonic-gate 
23750Sstevel@tonic-gate static void
23760Sstevel@tonic-gate print_mrt_stats(struct mrtstat *mrts)
23770Sstevel@tonic-gate {
23780Sstevel@tonic-gate 	(void) puts("DVMRP multicast routing:");
23790Sstevel@tonic-gate 	(void) printf(" %10u hit%s - kernel forwarding cache hits\n",
23800Sstevel@tonic-gate 		mrts->mrts_mfc_hits, PLURAL(mrts->mrts_mfc_hits));
23810Sstevel@tonic-gate 	(void) printf(" %10u miss%s - kernel forwarding cache misses\n",
23820Sstevel@tonic-gate 		mrts->mrts_mfc_misses, PLURALES(mrts->mrts_mfc_misses));
23830Sstevel@tonic-gate 	(void) printf(" %10u packet%s potentially forwarded\n",
23840Sstevel@tonic-gate 		mrts->mrts_fwd_in, PLURAL(mrts->mrts_fwd_in));
23850Sstevel@tonic-gate 	(void) printf(" %10u packet%s actually sent out\n",
23860Sstevel@tonic-gate 		mrts->mrts_fwd_out, PLURAL(mrts->mrts_fwd_out));
23870Sstevel@tonic-gate 	(void) printf(" %10u upcall%s - upcalls made to mrouted\n",
23880Sstevel@tonic-gate 		mrts->mrts_upcalls, PLURAL(mrts->mrts_upcalls));
23890Sstevel@tonic-gate 	(void) printf(" %10u packet%s not sent out due to lack of resources\n",
23900Sstevel@tonic-gate 		mrts->mrts_fwd_drop, PLURAL(mrts->mrts_fwd_drop));
23910Sstevel@tonic-gate 	(void) printf(" %10u datagram%s with malformed tunnel options\n",
23920Sstevel@tonic-gate 		mrts->mrts_bad_tunnel, PLURAL(mrts->mrts_bad_tunnel));
23930Sstevel@tonic-gate 	(void) printf(" %10u datagram%s with no room for tunnel options\n",
23940Sstevel@tonic-gate 		mrts->mrts_cant_tunnel, PLURAL(mrts->mrts_cant_tunnel));
23950Sstevel@tonic-gate 	(void) printf(" %10u datagram%s arrived on wrong interface\n",
23960Sstevel@tonic-gate 		mrts->mrts_wrong_if, PLURAL(mrts->mrts_wrong_if));
23970Sstevel@tonic-gate 	(void) printf(" %10u datagram%s dropped due to upcall Q overflow\n",
23980Sstevel@tonic-gate 		mrts->mrts_upq_ovflw, PLURAL(mrts->mrts_upq_ovflw));
23990Sstevel@tonic-gate 	(void) printf(" %10u datagram%s cleaned up by the cache\n",
24000Sstevel@tonic-gate 		mrts->mrts_cache_cleanups, PLURAL(mrts->mrts_cache_cleanups));
24010Sstevel@tonic-gate 	(void) printf(" %10u datagram%s dropped selectively by ratelimiter\n",
24020Sstevel@tonic-gate 		mrts->mrts_drop_sel, PLURAL(mrts->mrts_drop_sel));
24030Sstevel@tonic-gate 	(void) printf(" %10u datagram%s dropped - bucket Q overflow\n",
24040Sstevel@tonic-gate 		mrts->mrts_q_overflow, PLURAL(mrts->mrts_q_overflow));
24050Sstevel@tonic-gate 	(void) printf(" %10u datagram%s dropped - larger than bkt size\n",
24060Sstevel@tonic-gate 		mrts->mrts_pkt2large, PLURAL(mrts->mrts_pkt2large));
24070Sstevel@tonic-gate 	(void) printf("\nPIM multicast routing:\n");
24080Sstevel@tonic-gate 	(void) printf(" %10u datagram%s dropped - bad version number\n",
24090Sstevel@tonic-gate 		mrts->mrts_pim_badversion, PLURAL(mrts->mrts_pim_badversion));
24100Sstevel@tonic-gate 	(void) printf(" %10u datagram%s dropped - bad checksum\n",
24110Sstevel@tonic-gate 		mrts->mrts_pim_rcv_badcsum, PLURAL(mrts->mrts_pim_rcv_badcsum));
24120Sstevel@tonic-gate 	(void) printf(" %10u datagram%s dropped - bad register packets\n",
24130Sstevel@tonic-gate 		mrts->mrts_pim_badregisters,
24140Sstevel@tonic-gate 		PLURAL(mrts->mrts_pim_badregisters));
24150Sstevel@tonic-gate 	(void) printf(
24160Sstevel@tonic-gate 		" %10u datagram%s potentially forwarded - register packets\n",
24170Sstevel@tonic-gate 		mrts->mrts_pim_regforwards, PLURAL(mrts->mrts_pim_regforwards));
24180Sstevel@tonic-gate 	(void) printf(" %10u datagram%s dropped - register send drops\n",
24190Sstevel@tonic-gate 		mrts->mrts_pim_regsend_drops,
24200Sstevel@tonic-gate 		PLURAL(mrts->mrts_pim_regsend_drops));
24210Sstevel@tonic-gate 	(void) printf(" %10u datagram%s dropped - packet malformed\n",
24220Sstevel@tonic-gate 		mrts->mrts_pim_malformed, PLURAL(mrts->mrts_pim_malformed));
24230Sstevel@tonic-gate 	(void) printf(" %10u datagram%s dropped - no memory to forward\n",
24240Sstevel@tonic-gate 		mrts->mrts_pim_nomemory, PLURAL(mrts->mrts_pim_nomemory));
24250Sstevel@tonic-gate }
24260Sstevel@tonic-gate 
24270Sstevel@tonic-gate static void
24280Sstevel@tonic-gate sum_ip6_stats(mib2_ipv6IfStatsEntry_t *ip6, mib2_ipv6IfStatsEntry_t *sum6)
24290Sstevel@tonic-gate {
24300Sstevel@tonic-gate 	/* First few are not additive */
24310Sstevel@tonic-gate 	sum6->ipv6Forwarding = ip6->ipv6Forwarding;
24320Sstevel@tonic-gate 	sum6->ipv6DefaultHopLimit = ip6->ipv6DefaultHopLimit;
24330Sstevel@tonic-gate 
24340Sstevel@tonic-gate 	sum6->ipv6InReceives += ip6->ipv6InReceives;
24350Sstevel@tonic-gate 	sum6->ipv6InHdrErrors += ip6->ipv6InHdrErrors;
24360Sstevel@tonic-gate 	sum6->ipv6InTooBigErrors += ip6->ipv6InTooBigErrors;
24370Sstevel@tonic-gate 	sum6->ipv6InNoRoutes += ip6->ipv6InNoRoutes;
24380Sstevel@tonic-gate 	sum6->ipv6InAddrErrors += ip6->ipv6InAddrErrors;
24390Sstevel@tonic-gate 	sum6->ipv6InUnknownProtos += ip6->ipv6InUnknownProtos;
24400Sstevel@tonic-gate 	sum6->ipv6InTruncatedPkts += ip6->ipv6InTruncatedPkts;
24410Sstevel@tonic-gate 	sum6->ipv6InDiscards += ip6->ipv6InDiscards;
24420Sstevel@tonic-gate 	sum6->ipv6InDelivers += ip6->ipv6InDelivers;
24430Sstevel@tonic-gate 	sum6->ipv6OutForwDatagrams += ip6->ipv6OutForwDatagrams;
24440Sstevel@tonic-gate 	sum6->ipv6OutRequests += ip6->ipv6OutRequests;
24450Sstevel@tonic-gate 	sum6->ipv6OutDiscards += ip6->ipv6OutDiscards;
24460Sstevel@tonic-gate 	sum6->ipv6OutFragOKs += ip6->ipv6OutFragOKs;
24470Sstevel@tonic-gate 	sum6->ipv6OutFragFails += ip6->ipv6OutFragFails;
24480Sstevel@tonic-gate 	sum6->ipv6OutFragCreates += ip6->ipv6OutFragCreates;
24490Sstevel@tonic-gate 	sum6->ipv6ReasmReqds += ip6->ipv6ReasmReqds;
24500Sstevel@tonic-gate 	sum6->ipv6ReasmOKs += ip6->ipv6ReasmOKs;
24510Sstevel@tonic-gate 	sum6->ipv6ReasmFails += ip6->ipv6ReasmFails;
24520Sstevel@tonic-gate 	sum6->ipv6InMcastPkts += ip6->ipv6InMcastPkts;
24530Sstevel@tonic-gate 	sum6->ipv6OutMcastPkts += ip6->ipv6OutMcastPkts;
24540Sstevel@tonic-gate 	sum6->ipv6OutNoRoutes += ip6->ipv6OutNoRoutes;
24550Sstevel@tonic-gate 	sum6->ipv6ReasmDuplicates += ip6->ipv6ReasmDuplicates;
24560Sstevel@tonic-gate 	sum6->ipv6ReasmPartDups += ip6->ipv6ReasmPartDups;
24570Sstevel@tonic-gate 	sum6->ipv6ForwProhibits += ip6->ipv6ForwProhibits;
24580Sstevel@tonic-gate 	sum6->udpInCksumErrs += ip6->udpInCksumErrs;
24590Sstevel@tonic-gate 	sum6->udpInOverflows += ip6->udpInOverflows;
24600Sstevel@tonic-gate 	sum6->rawipInOverflows += ip6->rawipInOverflows;
24610Sstevel@tonic-gate }
24620Sstevel@tonic-gate 
24630Sstevel@tonic-gate static void
24640Sstevel@tonic-gate sum_icmp6_stats(mib2_ipv6IfIcmpEntry_t *icmp6, mib2_ipv6IfIcmpEntry_t *sum6)
24650Sstevel@tonic-gate {
24660Sstevel@tonic-gate 	sum6->ipv6IfIcmpInMsgs += icmp6->ipv6IfIcmpInMsgs;
24670Sstevel@tonic-gate 	sum6->ipv6IfIcmpInErrors += icmp6->ipv6IfIcmpInErrors;
24680Sstevel@tonic-gate 	sum6->ipv6IfIcmpInDestUnreachs += icmp6->ipv6IfIcmpInDestUnreachs;
24690Sstevel@tonic-gate 	sum6->ipv6IfIcmpInAdminProhibs += icmp6->ipv6IfIcmpInAdminProhibs;
24700Sstevel@tonic-gate 	sum6->ipv6IfIcmpInTimeExcds += icmp6->ipv6IfIcmpInTimeExcds;
24710Sstevel@tonic-gate 	sum6->ipv6IfIcmpInParmProblems += icmp6->ipv6IfIcmpInParmProblems;
24720Sstevel@tonic-gate 	sum6->ipv6IfIcmpInPktTooBigs += icmp6->ipv6IfIcmpInPktTooBigs;
24730Sstevel@tonic-gate 	sum6->ipv6IfIcmpInEchos += icmp6->ipv6IfIcmpInEchos;
24740Sstevel@tonic-gate 	sum6->ipv6IfIcmpInEchoReplies += icmp6->ipv6IfIcmpInEchoReplies;
24750Sstevel@tonic-gate 	sum6->ipv6IfIcmpInRouterSolicits += icmp6->ipv6IfIcmpInRouterSolicits;
24760Sstevel@tonic-gate 	sum6->ipv6IfIcmpInRouterAdvertisements +=
24770Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpInRouterAdvertisements;
24780Sstevel@tonic-gate 	sum6->ipv6IfIcmpInNeighborSolicits +=
24790Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpInNeighborSolicits;
24800Sstevel@tonic-gate 	sum6->ipv6IfIcmpInNeighborAdvertisements +=
24810Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpInNeighborAdvertisements;
24820Sstevel@tonic-gate 	sum6->ipv6IfIcmpInRedirects += icmp6->ipv6IfIcmpInRedirects;
24830Sstevel@tonic-gate 	sum6->ipv6IfIcmpInGroupMembQueries +=
24840Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpInGroupMembQueries;
24850Sstevel@tonic-gate 	sum6->ipv6IfIcmpInGroupMembResponses +=
24860Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpInGroupMembResponses;
24870Sstevel@tonic-gate 	sum6->ipv6IfIcmpInGroupMembReductions +=
24880Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpInGroupMembReductions;
24890Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutMsgs += icmp6->ipv6IfIcmpOutMsgs;
24900Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutErrors += icmp6->ipv6IfIcmpOutErrors;
24910Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutDestUnreachs += icmp6->ipv6IfIcmpOutDestUnreachs;
24920Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutAdminProhibs += icmp6->ipv6IfIcmpOutAdminProhibs;
24930Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutTimeExcds += icmp6->ipv6IfIcmpOutTimeExcds;
24940Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutParmProblems += icmp6->ipv6IfIcmpOutParmProblems;
24950Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutPktTooBigs += icmp6->ipv6IfIcmpOutPktTooBigs;
24960Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutEchos += icmp6->ipv6IfIcmpOutEchos;
24970Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutEchoReplies += icmp6->ipv6IfIcmpOutEchoReplies;
24980Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutRouterSolicits +=
24990Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpOutRouterSolicits;
25000Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutRouterAdvertisements +=
25010Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpOutRouterAdvertisements;
25020Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutNeighborSolicits +=
25030Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpOutNeighborSolicits;
25040Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutNeighborAdvertisements +=
25050Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpOutNeighborAdvertisements;
25060Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutRedirects += icmp6->ipv6IfIcmpOutRedirects;
25070Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutGroupMembQueries +=
25080Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpOutGroupMembQueries;
25090Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutGroupMembResponses +=
25100Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpOutGroupMembResponses;
25110Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutGroupMembReductions +=
25120Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpOutGroupMembReductions;
25130Sstevel@tonic-gate 	sum6->ipv6IfIcmpInOverflows += icmp6->ipv6IfIcmpInOverflows;
25140Sstevel@tonic-gate }
25150Sstevel@tonic-gate 
25160Sstevel@tonic-gate /* ----------------------------- MRT_STAT_REPORT --------------------------- */
25170Sstevel@tonic-gate 
25180Sstevel@tonic-gate static void
25190Sstevel@tonic-gate mrt_stat_report(mib_item_t *curritem)
25200Sstevel@tonic-gate {
25210Sstevel@tonic-gate 	int	jtemp = 0;
25220Sstevel@tonic-gate 	mib_item_t *tempitem;
25230Sstevel@tonic-gate 
25240Sstevel@tonic-gate 	if (!(family_selected(AF_INET)))
25250Sstevel@tonic-gate 		return;
25260Sstevel@tonic-gate 
25270Sstevel@tonic-gate 	(void) putchar('\n');
25280Sstevel@tonic-gate 	/* 'for' loop 1: */
25290Sstevel@tonic-gate 	for (tempitem = curritem;
25300Sstevel@tonic-gate 	    tempitem;
25310Sstevel@tonic-gate 	    tempitem = tempitem->next_item) {
25320Sstevel@tonic-gate 		if (Dflag) {
25330Sstevel@tonic-gate 			(void) printf("\n--- Entry %d ---\n", ++jtemp);
25340Sstevel@tonic-gate 			(void) printf("Group = %d, mib_id = %d, "
25350Sstevel@tonic-gate 			    "length = %d, valp = 0x%p\n",
25360Sstevel@tonic-gate 			    tempitem->group, tempitem->mib_id,
25370Sstevel@tonic-gate 			    tempitem->length, tempitem->valp);
25380Sstevel@tonic-gate 		}
25390Sstevel@tonic-gate 
25400Sstevel@tonic-gate 		if (tempitem->mib_id == 0) {
25410Sstevel@tonic-gate 			switch (tempitem->group) {
25420Sstevel@tonic-gate 			case EXPER_DVMRP: {
25430Sstevel@tonic-gate 				struct mrtstat	*mrts;
25440Sstevel@tonic-gate 				mrts = (struct mrtstat *)tempitem->valp;
25450Sstevel@tonic-gate 
25460Sstevel@tonic-gate 				if (!(family_selected(AF_INET)))
25470Sstevel@tonic-gate 					continue; /* 'for' loop 1 */
25480Sstevel@tonic-gate 
25490Sstevel@tonic-gate 				print_mrt_stats(mrts);
25500Sstevel@tonic-gate 				break;
25510Sstevel@tonic-gate 			}
25520Sstevel@tonic-gate 			}
25530Sstevel@tonic-gate 		}
25540Sstevel@tonic-gate 	} /* 'for' loop 1 ends */
25550Sstevel@tonic-gate 	(void) putchar('\n');
25560Sstevel@tonic-gate 	(void) fflush(stdout);
25570Sstevel@tonic-gate }
25580Sstevel@tonic-gate 
25590Sstevel@tonic-gate /*
25600Sstevel@tonic-gate  * if_stat_total() - Computes totals for interface statistics
25610Sstevel@tonic-gate  *                   and returns result by updating sumstats.
25620Sstevel@tonic-gate  */
25630Sstevel@tonic-gate static void
25640Sstevel@tonic-gate if_stat_total(struct ifstat *oldstats, struct ifstat *newstats,
25650Sstevel@tonic-gate     struct ifstat *sumstats)
25660Sstevel@tonic-gate {
25670Sstevel@tonic-gate 	sumstats->ipackets += newstats->ipackets - oldstats->ipackets;
25680Sstevel@tonic-gate 	sumstats->opackets += newstats->opackets - oldstats->opackets;
25690Sstevel@tonic-gate 	sumstats->ierrors += newstats->ierrors - oldstats->ierrors;
25700Sstevel@tonic-gate 	sumstats->oerrors += newstats->oerrors - oldstats->oerrors;
25710Sstevel@tonic-gate 	sumstats->collisions += newstats->collisions - oldstats->collisions;
25720Sstevel@tonic-gate }
25730Sstevel@tonic-gate 
25740Sstevel@tonic-gate /* --------------------- IF_REPORT (netstat -i)  -------------------------- */
25750Sstevel@tonic-gate 
25760Sstevel@tonic-gate static struct	ifstat	zerostat = {
25770Sstevel@tonic-gate 	0LL, 0LL, 0LL, 0LL, 0LL
25780Sstevel@tonic-gate };
25790Sstevel@tonic-gate 
25800Sstevel@tonic-gate static void
25810Sstevel@tonic-gate if_report(mib_item_t *item, char *matchname,
25820Sstevel@tonic-gate     int Iflag_only, boolean_t once_only)
25830Sstevel@tonic-gate {
25840Sstevel@tonic-gate 	static boolean_t	reentry = B_FALSE;
25850Sstevel@tonic-gate 	boolean_t		alreadydone = B_FALSE;
25860Sstevel@tonic-gate 	int			jtemp = 0;
25870Sstevel@tonic-gate 	uint32_t		ifindex_v4 = 0;
25880Sstevel@tonic-gate 	uint32_t		ifindex_v6 = 0;
25890Sstevel@tonic-gate 
25900Sstevel@tonic-gate 	/* 'for' loop 1: */
25910Sstevel@tonic-gate 	for (; item; item = item->next_item) {
25920Sstevel@tonic-gate 		if (Dflag) {
25930Sstevel@tonic-gate 			(void) printf("\n--- Entry %d ---\n", ++jtemp);
25940Sstevel@tonic-gate 			(void) printf("Group = %d, mib_id = %d, "
25950Sstevel@tonic-gate 			    "length = %d, valp = 0x%p\n",
25960Sstevel@tonic-gate 			    item->group, item->mib_id, item->length,
25970Sstevel@tonic-gate 			    item->valp);
25980Sstevel@tonic-gate 		}
25990Sstevel@tonic-gate 
26000Sstevel@tonic-gate 		switch (item->group) {
26010Sstevel@tonic-gate 		case MIB2_IP:
26020Sstevel@tonic-gate 		if (item->mib_id != MIB2_IP_ADDR ||
26030Sstevel@tonic-gate 		    !family_selected(AF_INET))
26040Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
26050Sstevel@tonic-gate 		{
26060Sstevel@tonic-gate 			static struct ifstat	old = {0L, 0L, 0L, 0L, 0L};
26070Sstevel@tonic-gate 			static struct ifstat	new = {0L, 0L, 0L, 0L, 0L};
26080Sstevel@tonic-gate 			struct ifstat		sum;
26090Sstevel@tonic-gate 			struct iflist		*newlist = NULL;
26100Sstevel@tonic-gate 			static struct iflist	*oldlist = NULL;
26110Sstevel@tonic-gate 			kstat_t	 *ksp;
26120Sstevel@tonic-gate 
26130Sstevel@tonic-gate 			if (once_only) {
26140Sstevel@tonic-gate 				char    ifname[LIFNAMSIZ + 1];
26150Sstevel@tonic-gate 				char    logintname[LIFNAMSIZ + 1];
26160Sstevel@tonic-gate 				mib2_ipAddrEntry_t *ap;
26170Sstevel@tonic-gate 				struct ifstat	stat = {0L, 0L, 0L, 0L, 0L};
26180Sstevel@tonic-gate 				boolean_t	first = B_TRUE;
26190Sstevel@tonic-gate 				uint32_t	new_ifindex;
26200Sstevel@tonic-gate 
26210Sstevel@tonic-gate 				if (Dflag)
26220Sstevel@tonic-gate 					(void) printf("if_report: %d items\n",
26230Sstevel@tonic-gate 					    (item->length)
26240Sstevel@tonic-gate 					    / sizeof (mib2_ipAddrEntry_t));
26250Sstevel@tonic-gate 
26260Sstevel@tonic-gate 				/* 'for' loop 2a: */
26270Sstevel@tonic-gate 				for (ap = (mib2_ipAddrEntry_t *)item->valp;
26280Sstevel@tonic-gate 				    (char *)ap < (char *)item->valp
26290Sstevel@tonic-gate 				    + item->length;
26300Sstevel@tonic-gate 				    ap++) {
26310Sstevel@tonic-gate 					(void) octetstr(&ap->ipAdEntIfIndex,
26320Sstevel@tonic-gate 					    'a', logintname,
26330Sstevel@tonic-gate 					    sizeof (logintname));
26340Sstevel@tonic-gate 					(void) strcpy(ifname, logintname);
26350Sstevel@tonic-gate 					(void) strtok(ifname, ":");
26360Sstevel@tonic-gate 					if (matchname != NULL &&
26370Sstevel@tonic-gate 					    strcmp(matchname, ifname) != 0 &&
26380Sstevel@tonic-gate 					    strcmp(matchname, logintname) != 0)
26390Sstevel@tonic-gate 						continue; /* 'for' loop 2a */
26400Sstevel@tonic-gate 					new_ifindex =
26410Sstevel@tonic-gate 					    if_nametoindex(logintname);
26420Sstevel@tonic-gate 					if (new_ifindex != ifindex_v4 &&
26430Sstevel@tonic-gate 					    (ksp = kstat_lookup(kc, NULL, -1,
26440Sstevel@tonic-gate 					    ifname)) != NULL) {
26450Sstevel@tonic-gate 						(void) safe_kstat_read(kc, ksp,
26460Sstevel@tonic-gate 						    NULL);
26470Sstevel@tonic-gate 						stat.ipackets =
26480Sstevel@tonic-gate 						    kstat_named_value(ksp,
26490Sstevel@tonic-gate 						    "ipackets");
26500Sstevel@tonic-gate 						stat.ierrors =
26510Sstevel@tonic-gate 						    kstat_named_value(ksp,
26520Sstevel@tonic-gate 						    "ierrors");
26530Sstevel@tonic-gate 						stat.opackets =
26540Sstevel@tonic-gate 						    kstat_named_value(ksp,
26550Sstevel@tonic-gate 						    "opackets");
26560Sstevel@tonic-gate 						stat.oerrors =
26570Sstevel@tonic-gate 						    kstat_named_value(ksp,
26580Sstevel@tonic-gate 						    "oerrors");
26590Sstevel@tonic-gate 						stat.collisions =
26600Sstevel@tonic-gate 						    kstat_named_value(ksp,
26610Sstevel@tonic-gate 						    "collisions");
26620Sstevel@tonic-gate 						if (first) {
26630Sstevel@tonic-gate 						(void) printf(
26640Sstevel@tonic-gate 						    "%-5.5s %-5.5s%-13.13s "
26650Sstevel@tonic-gate 						    "%-14.14s %-6.6s %-5.5s "
26660Sstevel@tonic-gate 						    "%-6.6s %-5.5s %-6.6s "
26670Sstevel@tonic-gate 						    "%-6.6s\n",
26680Sstevel@tonic-gate 						    "Name", "Mtu", "Net/Dest",
26690Sstevel@tonic-gate 						    "Address", "Ipkts",
26700Sstevel@tonic-gate 						    "Ierrs", "Opkts", "Oerrs",
26710Sstevel@tonic-gate 						    "Collis", "Queue");
26720Sstevel@tonic-gate 						first = B_FALSE;
26730Sstevel@tonic-gate 						}
26740Sstevel@tonic-gate 						if_report_ip4(ap, ifname,
26750Sstevel@tonic-gate 						    logintname, &stat, B_TRUE);
26760Sstevel@tonic-gate 						ifindex_v4 = new_ifindex;
26770Sstevel@tonic-gate 					} else {
26780Sstevel@tonic-gate 						if_report_ip4(ap, ifname,
26790Sstevel@tonic-gate 						    logintname, &stat, B_FALSE);
26800Sstevel@tonic-gate 					}
26810Sstevel@tonic-gate 				} /* 'for' loop 2a ends */
26820Sstevel@tonic-gate 				if (!first)
26830Sstevel@tonic-gate 					(void) putchar('\n');
26840Sstevel@tonic-gate 			} else if (!alreadydone) {
26850Sstevel@tonic-gate 				char    ifname[LIFNAMSIZ + 1];
26860Sstevel@tonic-gate 				char    buf[LIFNAMSIZ + 1];
26870Sstevel@tonic-gate 				mib2_ipAddrEntry_t *ap;
26880Sstevel@tonic-gate 				struct ifstat   t;
26893129Sja97890 				struct iflist	*tlp = NULL;
26900Sstevel@tonic-gate 				struct iflist	**nextnew = &newlist;
26910Sstevel@tonic-gate 				struct iflist	*walkold;
26920Sstevel@tonic-gate 				struct iflist	*cleanlist;
26933129Sja97890 				boolean_t	found_if = B_FALSE;
26940Sstevel@tonic-gate 
26950Sstevel@tonic-gate 				alreadydone = B_TRUE; /* ignore other case */
26963129Sja97890 
26970Sstevel@tonic-gate 				/*
26983129Sja97890 				 * Check if there is anything to do.
26993129Sja97890 				 */
27003129Sja97890 				if (item->length <
27013129Sja97890 				    sizeof (mib2_ipAddrEntry_t)) {
27023129Sja97890 					fail(0, "No compatible interfaces");
27033129Sja97890 				}
27043129Sja97890 
27053129Sja97890 				/*
27063129Sja97890 				 * 'for' loop 2b: find the "right" entry:
27073129Sja97890 				 * If an interface name to match has been
27083129Sja97890 				 * supplied then try and find it, otherwise
27093129Sja97890 				 * match the first non-loopback interface found.
27103129Sja97890 				 * Use lo0 if all else fails.
27110Sstevel@tonic-gate 				 */
27120Sstevel@tonic-gate 				for (ap = (mib2_ipAddrEntry_t *)item->valp;
27130Sstevel@tonic-gate 				    (char *)ap < (char *)item->valp
27140Sstevel@tonic-gate 				    + item->length;
27150Sstevel@tonic-gate 				    ap++) {
27160Sstevel@tonic-gate 					(void) octetstr(&ap->ipAdEntIfIndex,
27170Sstevel@tonic-gate 					'a', ifname, sizeof (ifname));
27180Sstevel@tonic-gate 					(void) strtok(ifname, ":");
27190Sstevel@tonic-gate 
27200Sstevel@tonic-gate 					if (matchname) {
27210Sstevel@tonic-gate 						if (strcmp(matchname,
27223129Sja97890 						    ifname) == 0) {
27230Sstevel@tonic-gate 							/* 'for' loop 2b */
27243129Sja97890 							found_if = B_TRUE;
27250Sstevel@tonic-gate 							break;
27263129Sja97890 						}
27273129Sja97890 					} else if (strcmp(ifname, "lo0") != 0)
27280Sstevel@tonic-gate 						break; /* 'for' loop 2b */
27290Sstevel@tonic-gate 				} /* 'for' loop 2b ends */
27300Sstevel@tonic-gate 
27313129Sja97890 				if (matchname == NULL) {
27323129Sja97890 					matchname = ifname;
27333129Sja97890 				} else {
27343129Sja97890 					if (!found_if)
27353129Sja97890 						fail(0, "-I: %s no such "
27363129Sja97890 						    "interface.", matchname);
27373129Sja97890 				}
27383129Sja97890 
27390Sstevel@tonic-gate 				if (Iflag_only == 0 || !reentry) {
27400Sstevel@tonic-gate 					(void) printf("    input   %-6.6s    "
27410Sstevel@tonic-gate 					    "output	",
27420Sstevel@tonic-gate 					    matchname);
27430Sstevel@tonic-gate 					(void) printf("   input  (Total)    "
27440Sstevel@tonic-gate 					"output\n");
27450Sstevel@tonic-gate 					(void) printf("%-7.7s %-5.5s %-7.7s "
27460Sstevel@tonic-gate 					    "%-5.5s %-6.6s ",
27470Sstevel@tonic-gate 					    "packets", "errs", "packets",
27480Sstevel@tonic-gate 					    "errs", "colls");
27490Sstevel@tonic-gate 					(void) printf("%-7.7s %-5.5s %-7.7s "
27500Sstevel@tonic-gate 					    "%-5.5s %-6.6s\n",
27510Sstevel@tonic-gate 					    "packets", "errs", "packets",
27520Sstevel@tonic-gate 					    "errs", "colls");
27530Sstevel@tonic-gate 				}
27540Sstevel@tonic-gate 
27550Sstevel@tonic-gate 				sum = zerostat;
27560Sstevel@tonic-gate 
27570Sstevel@tonic-gate 				/* 'for' loop 2c: */
27580Sstevel@tonic-gate 				for (ap = (mib2_ipAddrEntry_t *)item->valp;
27590Sstevel@tonic-gate 				    (char *)ap < (char *)item->valp
27600Sstevel@tonic-gate 				    + item->length;
27610Sstevel@tonic-gate 				    ap++) {
27620Sstevel@tonic-gate 					(void) octetstr(&ap->ipAdEntIfIndex,
27630Sstevel@tonic-gate 					    'a', buf, sizeof (buf));
27640Sstevel@tonic-gate 					(void) strtok(buf, ":");
27653129Sja97890 
27663129Sja97890 					/*
27673129Sja97890 					 * We have reduced the IP interface
27683129Sja97890 					 * name, which could have been a
27693129Sja97890 					 * logical, down to a name suitable
27703129Sja97890 					 * for use with kstats.
27713129Sja97890 					 * We treat this name as unique and
27723129Sja97890 					 * only collate statistics for it once
27733129Sja97890 					 * per pass. This is to avoid falsely
27743129Sja97890 					 * amplifying these statistics by the
27753129Sja97890 					 * the number of logical instances.
27763129Sja97890 					 */
27773129Sja97890 					if ((tlp != NULL) &&
27783129Sja97890 					    ((strcmp(buf, tlp->ifname) == 0))) {
27793129Sja97890 						continue;
27803129Sja97890 					}
27813129Sja97890 
27820Sstevel@tonic-gate 					ksp = kstat_lookup(kc, NULL, -1, buf);
27830Sstevel@tonic-gate 					if (ksp &&
27840Sstevel@tonic-gate 					    ksp->ks_type == KSTAT_TYPE_NAMED)
27850Sstevel@tonic-gate 						(void) safe_kstat_read(kc, ksp,
27860Sstevel@tonic-gate 						    NULL);
27870Sstevel@tonic-gate 
27880Sstevel@tonic-gate 					t.ipackets = kstat_named_value(ksp,
27890Sstevel@tonic-gate 					    "ipackets");
27900Sstevel@tonic-gate 					t.ierrors = kstat_named_value(ksp,
27910Sstevel@tonic-gate 					    "ierrors");
27920Sstevel@tonic-gate 					t.opackets = kstat_named_value(ksp,
27930Sstevel@tonic-gate 					    "opackets");
27940Sstevel@tonic-gate 					t.oerrors = kstat_named_value(ksp,
27950Sstevel@tonic-gate 					    "oerrors");
27960Sstevel@tonic-gate 					t.collisions = kstat_named_value(ksp,
27970Sstevel@tonic-gate 					    "collisions");
27980Sstevel@tonic-gate 
27990Sstevel@tonic-gate 					if (strcmp(buf, matchname) == 0)
28000Sstevel@tonic-gate 						new = t;
28010Sstevel@tonic-gate 
28020Sstevel@tonic-gate 					/* Build the interface list */
28030Sstevel@tonic-gate 
28040Sstevel@tonic-gate 					tlp = malloc(sizeof (struct iflist));
28050Sstevel@tonic-gate 					(void) strlcpy(tlp->ifname, buf,
28060Sstevel@tonic-gate 					    sizeof (tlp->ifname));
28070Sstevel@tonic-gate 					tlp->tot = t;
28080Sstevel@tonic-gate 					*nextnew = tlp;
28090Sstevel@tonic-gate 					nextnew = &tlp->next_if;
28100Sstevel@tonic-gate 
28110Sstevel@tonic-gate 					/*
28120Sstevel@tonic-gate 					 * First time through.
28130Sstevel@tonic-gate 					 * Just add up the interface stats.
28140Sstevel@tonic-gate 					 */
28150Sstevel@tonic-gate 
28160Sstevel@tonic-gate 					if (oldlist == NULL) {
28170Sstevel@tonic-gate 						if_stat_total(&zerostat,
28180Sstevel@tonic-gate 						    &t, &sum);
28190Sstevel@tonic-gate 						continue;
28200Sstevel@tonic-gate 					}
28210Sstevel@tonic-gate 
28220Sstevel@tonic-gate 					/*
28230Sstevel@tonic-gate 					 * Walk old list for the interface.
28240Sstevel@tonic-gate 					 *
28250Sstevel@tonic-gate 					 * If found, add difference to total.
28260Sstevel@tonic-gate 					 *
28270Sstevel@tonic-gate 					 * If not, an interface has been plumbed
28280Sstevel@tonic-gate 					 * up.  In this case, we will simply
28290Sstevel@tonic-gate 					 * ignore the new interface until the
28300Sstevel@tonic-gate 					 * next interval; as there's no easy way
28310Sstevel@tonic-gate 					 * to acquire statistics between time
28320Sstevel@tonic-gate 					 * of the plumb and the next interval
28330Sstevel@tonic-gate 					 * boundary.  This results in inaccurate
28340Sstevel@tonic-gate 					 * total values for current interval.
28350Sstevel@tonic-gate 					 *
28360Sstevel@tonic-gate 					 * Note the case when an interface is
28370Sstevel@tonic-gate 					 * unplumbed; as similar problems exist.
28380Sstevel@tonic-gate 					 * The unplumbed interface is not in the
28390Sstevel@tonic-gate 					 * current list, and there's no easy way
28400Sstevel@tonic-gate 					 * to account for the statistics between
28410Sstevel@tonic-gate 					 * the previous interval and time of the
28420Sstevel@tonic-gate 					 * unplumb.  Therefore, we (in a sense)
28430Sstevel@tonic-gate 					 * ignore the removed interface by only
28440Sstevel@tonic-gate 					 * involving "current" interfaces when
28450Sstevel@tonic-gate 					 * computing the total statistics.
28460Sstevel@tonic-gate 					 * Unfortunately, this also results in
28470Sstevel@tonic-gate 					 * inaccurate values for interval total.
28480Sstevel@tonic-gate 					 */
28490Sstevel@tonic-gate 
28500Sstevel@tonic-gate 					for (walkold = oldlist;
28510Sstevel@tonic-gate 					    walkold != NULL;
28520Sstevel@tonic-gate 					    walkold = walkold->next_if) {
28530Sstevel@tonic-gate 						if (strcmp(walkold->ifname,
28540Sstevel@tonic-gate 						    buf) == 0) {
28550Sstevel@tonic-gate 							if_stat_total(
28560Sstevel@tonic-gate 							    &walkold->tot,
28570Sstevel@tonic-gate 							    &t, &sum);
28580Sstevel@tonic-gate 							break;
28590Sstevel@tonic-gate 						}
28600Sstevel@tonic-gate 					}
28610Sstevel@tonic-gate 
28620Sstevel@tonic-gate 				} /* 'for' loop 2c ends */
28630Sstevel@tonic-gate 
28640Sstevel@tonic-gate 				*nextnew = NULL;
28650Sstevel@tonic-gate 
28660Sstevel@tonic-gate 				(void) printf("%-7llu %-5llu %-7llu "
28670Sstevel@tonic-gate 				    "%-5llu %-6llu ",
28680Sstevel@tonic-gate 				    new.ipackets - old.ipackets,
28690Sstevel@tonic-gate 				    new.ierrors - old.ierrors,
28700Sstevel@tonic-gate 				    new.opackets - old.opackets,
28710Sstevel@tonic-gate 				    new.oerrors - old.oerrors,
28720Sstevel@tonic-gate 				    new.collisions - old.collisions);
28730Sstevel@tonic-gate 
28740Sstevel@tonic-gate 				(void) printf("%-7llu %-5llu %-7llu "
28750Sstevel@tonic-gate 				    "%-5llu %-6llu\n", sum.ipackets,
28760Sstevel@tonic-gate 				    sum.ierrors, sum.opackets,
28770Sstevel@tonic-gate 				    sum.oerrors, sum.collisions);
28780Sstevel@tonic-gate 
28790Sstevel@tonic-gate 				/*
28800Sstevel@tonic-gate 				 * Tidy things up once finished.
28810Sstevel@tonic-gate 				 */
28820Sstevel@tonic-gate 
28830Sstevel@tonic-gate 				old = new;
28840Sstevel@tonic-gate 				cleanlist = oldlist;
28850Sstevel@tonic-gate 				oldlist = newlist;
28860Sstevel@tonic-gate 				while (cleanlist != NULL) {
28870Sstevel@tonic-gate 					tlp = cleanlist->next_if;
28880Sstevel@tonic-gate 					free(cleanlist);
28890Sstevel@tonic-gate 					cleanlist = tlp;
28900Sstevel@tonic-gate 				}
28910Sstevel@tonic-gate 			}
28920Sstevel@tonic-gate 			break;
28930Sstevel@tonic-gate 		}
28940Sstevel@tonic-gate 		case MIB2_IP6:
28950Sstevel@tonic-gate 		if (item->mib_id != MIB2_IP6_ADDR ||
28960Sstevel@tonic-gate 		    !family_selected(AF_INET6))
28970Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
28980Sstevel@tonic-gate 		{
28990Sstevel@tonic-gate 			static struct ifstat	old6 = {0L, 0L, 0L, 0L, 0L};
29000Sstevel@tonic-gate 			static struct ifstat	new6 = {0L, 0L, 0L, 0L, 0L};
29010Sstevel@tonic-gate 			struct ifstat		sum6;
29020Sstevel@tonic-gate 			struct iflist		*newlist6 = NULL;
29030Sstevel@tonic-gate 			static struct iflist	*oldlist6 = NULL;
29040Sstevel@tonic-gate 			kstat_t	 *ksp;
29050Sstevel@tonic-gate 
29060Sstevel@tonic-gate 			if (once_only) {
29070Sstevel@tonic-gate 				char    ifname[LIFNAMSIZ + 1];
29080Sstevel@tonic-gate 				char    logintname[LIFNAMSIZ + 1];
29090Sstevel@tonic-gate 				mib2_ipv6AddrEntry_t *ap6;
29100Sstevel@tonic-gate 				struct ifstat	stat = {0L, 0L, 0L, 0L, 0L};
29110Sstevel@tonic-gate 				boolean_t	first = B_TRUE;
29120Sstevel@tonic-gate 				uint32_t	new_ifindex;
29130Sstevel@tonic-gate 
29140Sstevel@tonic-gate 				if (Dflag)
29150Sstevel@tonic-gate 					(void) printf("if_report: %d items\n",
29160Sstevel@tonic-gate 					    (item->length)
29170Sstevel@tonic-gate 					    / sizeof (mib2_ipv6AddrEntry_t));
29180Sstevel@tonic-gate 				/* 'for' loop 2d: */
29190Sstevel@tonic-gate 				for (ap6 = (mib2_ipv6AddrEntry_t *)item->valp;
29200Sstevel@tonic-gate 				    (char *)ap6 < (char *)item->valp
29210Sstevel@tonic-gate 				    + item->length;
29220Sstevel@tonic-gate 				    ap6++) {
29230Sstevel@tonic-gate 					(void) octetstr(&ap6->ipv6AddrIfIndex,
29240Sstevel@tonic-gate 					    'a', logintname,
29250Sstevel@tonic-gate 					    sizeof (logintname));
29260Sstevel@tonic-gate 					(void) strcpy(ifname, logintname);
29270Sstevel@tonic-gate 					(void) strtok(ifname, ":");
29280Sstevel@tonic-gate 					if (matchname != NULL &&
29290Sstevel@tonic-gate 					    strcmp(matchname, ifname) != 0 &&
29300Sstevel@tonic-gate 					    strcmp(matchname, logintname) != 0)
29310Sstevel@tonic-gate 						continue; /* 'for' loop 2d */
29320Sstevel@tonic-gate 					new_ifindex =
29330Sstevel@tonic-gate 					    if_nametoindex(logintname);
29340Sstevel@tonic-gate 					if (new_ifindex != ifindex_v6 &&
29350Sstevel@tonic-gate 					    (ksp = kstat_lookup(kc, NULL, -1,
29360Sstevel@tonic-gate 					    ifname)) != NULL) {
29370Sstevel@tonic-gate 						(void) safe_kstat_read(kc, ksp,
29380Sstevel@tonic-gate 						    NULL);
29390Sstevel@tonic-gate 						stat.ipackets =
29400Sstevel@tonic-gate 						    kstat_named_value(ksp,
29410Sstevel@tonic-gate 						    "ipackets");
29420Sstevel@tonic-gate 						stat.ierrors =
29430Sstevel@tonic-gate 						    kstat_named_value(ksp,
29440Sstevel@tonic-gate 						    "ierrors");
29450Sstevel@tonic-gate 						stat.opackets =
29460Sstevel@tonic-gate 						    kstat_named_value(ksp,
29470Sstevel@tonic-gate 						    "opackets");
29480Sstevel@tonic-gate 						stat.oerrors =
29490Sstevel@tonic-gate 						    kstat_named_value(ksp,
29500Sstevel@tonic-gate 						    "oerrors");
29510Sstevel@tonic-gate 						stat.collisions =
29520Sstevel@tonic-gate 						    kstat_named_value(ksp,
29530Sstevel@tonic-gate 						    "collisions");
29540Sstevel@tonic-gate 						if (first) {
29550Sstevel@tonic-gate 							(void) printf(
29560Sstevel@tonic-gate 							    "%-5.5s %-5.5s%"
29570Sstevel@tonic-gate 							    "-27.27s %-27.27s "
29580Sstevel@tonic-gate 							    "%-6.6s %-5.5s "
29590Sstevel@tonic-gate 							    "%-6.6s %-5.5s "
29600Sstevel@tonic-gate 							    "%-6.6s\n",
29610Sstevel@tonic-gate 							    "Name", "Mtu",
29620Sstevel@tonic-gate 							    "Net/Dest",
29630Sstevel@tonic-gate 							    "Address", "Ipkts",
29640Sstevel@tonic-gate 							    "Ierrs", "Opkts",
29650Sstevel@tonic-gate 							    "Oerrs", "Collis");
29660Sstevel@tonic-gate 							first = B_FALSE;
29670Sstevel@tonic-gate 						}
29680Sstevel@tonic-gate 						if_report_ip6(ap6, ifname,
29690Sstevel@tonic-gate 						    logintname, &stat, B_TRUE);
29700Sstevel@tonic-gate 						ifindex_v6 = new_ifindex;
29710Sstevel@tonic-gate 					} else {
29720Sstevel@tonic-gate 						if_report_ip6(ap6, ifname,
29730Sstevel@tonic-gate 						    logintname, &stat, B_FALSE);
29740Sstevel@tonic-gate 					}
29750Sstevel@tonic-gate 				} /* 'for' loop 2d ends */
29760Sstevel@tonic-gate 				if (!first)
29770Sstevel@tonic-gate 					(void) putchar('\n');
29780Sstevel@tonic-gate 			} else if (!alreadydone) {
29790Sstevel@tonic-gate 				char    ifname[LIFNAMSIZ + 1];
29800Sstevel@tonic-gate 				char    buf[IFNAMSIZ + 1];
29810Sstevel@tonic-gate 				mib2_ipv6AddrEntry_t *ap6;
29820Sstevel@tonic-gate 				struct ifstat   t;
29833129Sja97890 				struct iflist	*tlp = NULL;
29840Sstevel@tonic-gate 				struct iflist	**nextnew = &newlist6;
29850Sstevel@tonic-gate 				struct iflist	*walkold;
29860Sstevel@tonic-gate 				struct iflist	*cleanlist;
29873129Sja97890 				boolean_t	found_if = B_FALSE;
29880Sstevel@tonic-gate 
29890Sstevel@tonic-gate 				alreadydone = B_TRUE; /* ignore other case */
29903129Sja97890 
29910Sstevel@tonic-gate 				/*
29923129Sja97890 				 * Check if there is anything to do.
29933129Sja97890 				 */
29943129Sja97890 				if (item->length <
29953129Sja97890 				    sizeof (mib2_ipv6AddrEntry_t)) {
29963129Sja97890 					fail(0, "No compatible interfaces");
29973129Sja97890 				}
29983129Sja97890 
29993129Sja97890 				/*
30003129Sja97890 				 * 'for' loop 2e: find the "right" entry:
30013129Sja97890 				 * If an interface name to match has been
30023129Sja97890 				 * supplied then try and find it, otherwise
30033129Sja97890 				 * match the first non-loopback interface found.
30043129Sja97890 				 * Use lo0 if all else fails.
30050Sstevel@tonic-gate 				 */
30060Sstevel@tonic-gate 				for (ap6 = (mib2_ipv6AddrEntry_t *)item->valp;
30070Sstevel@tonic-gate 				    (char *)ap6 < (char *)item->valp
30080Sstevel@tonic-gate 				    + item->length;
30090Sstevel@tonic-gate 				    ap6++) {
30100Sstevel@tonic-gate 					(void) octetstr(&ap6->ipv6AddrIfIndex,
30110Sstevel@tonic-gate 					    'a', ifname, sizeof (ifname));
30120Sstevel@tonic-gate 					(void) strtok(ifname, ":");
30130Sstevel@tonic-gate 
30140Sstevel@tonic-gate 					if (matchname) {
30153129Sja97890 						if (strcmp(matchname,
30163129Sja97890 						    ifname) == 0) {
30170Sstevel@tonic-gate 							/* 'for' loop 2e */
30183129Sja97890 							found_if = B_TRUE;
30190Sstevel@tonic-gate 							break;
30203129Sja97890 						}
30213129Sja97890 					} else if (strcmp(ifname, "lo0") != 0)
30220Sstevel@tonic-gate 						break; /* 'for' loop 2e */
30230Sstevel@tonic-gate 				} /* 'for' loop 2e ends */
30240Sstevel@tonic-gate 
30253129Sja97890 				if (matchname == NULL) {
30263129Sja97890 					matchname = ifname;
30273129Sja97890 				} else {
30283129Sja97890 					if (!found_if)
30293129Sja97890 						fail(0, "-I: %s no such "
30303129Sja97890 						    "interface.", matchname);
30313129Sja97890 				}
30323129Sja97890 
30330Sstevel@tonic-gate 				if (Iflag_only == 0 || !reentry) {
30340Sstevel@tonic-gate 					(void) printf(
30350Sstevel@tonic-gate 					    "    input   %-6.6s"
30360Sstevel@tonic-gate 					    "    output	",
30370Sstevel@tonic-gate 					    matchname);
30380Sstevel@tonic-gate 					(void) printf("   input  (Total)"
30390Sstevel@tonic-gate 					    "    output\n");
30400Sstevel@tonic-gate 					(void) printf("%-7.7s %-5.5s %-7.7s "
30410Sstevel@tonic-gate 					    "%-5.5s %-6.6s ",
30420Sstevel@tonic-gate 					    "packets", "errs", "packets",
30430Sstevel@tonic-gate 					    "errs", "colls");
30440Sstevel@tonic-gate 					(void) printf("%-7.7s %-5.5s %-7.7s "
30450Sstevel@tonic-gate 					    "%-5.5s %-6.6s\n",
30460Sstevel@tonic-gate 					    "packets", "errs", "packets",
30470Sstevel@tonic-gate 					    "errs", "colls");
30480Sstevel@tonic-gate 				}
30490Sstevel@tonic-gate 
30500Sstevel@tonic-gate 				sum6 = zerostat;
30510Sstevel@tonic-gate 
30520Sstevel@tonic-gate 				/* 'for' loop 2f: */
30530Sstevel@tonic-gate 				for (ap6 = (mib2_ipv6AddrEntry_t *)item->valp;
30540Sstevel@tonic-gate 				    (char *)ap6 < (char *)item->valp
30550Sstevel@tonic-gate 				    + item->length;
30560Sstevel@tonic-gate 				    ap6++) {
30570Sstevel@tonic-gate 					(void) octetstr(&ap6->ipv6AddrIfIndex,
30580Sstevel@tonic-gate 					    'a', buf, sizeof (buf));
30590Sstevel@tonic-gate 					(void) strtok(buf, ":");
30603129Sja97890 
30613129Sja97890 					/*
30623129Sja97890 					 * We have reduced the IP interface
30633129Sja97890 					 * name, which could have been a
30643129Sja97890 					 * logical, down to a name suitable
30653129Sja97890 					 * for use with kstats.
30663129Sja97890 					 * We treat this name as unique and
30673129Sja97890 					 * only collate statistics for it once
30683129Sja97890 					 * per pass. This is to avoid falsely
30693129Sja97890 					 * amplifying these statistics by the
30703129Sja97890 					 * the number of logical instances.
30713129Sja97890 					 */
30723129Sja97890 
30733129Sja97890 					if ((tlp != NULL) &&
30743129Sja97890 					    ((strcmp(buf, tlp->ifname) == 0))) {
30753129Sja97890 						continue;
30763129Sja97890 					}
30773129Sja97890 
30780Sstevel@tonic-gate 					ksp = kstat_lookup(kc, NULL, -1, buf);
30790Sstevel@tonic-gate 					if (ksp && ksp->ks_type ==
30800Sstevel@tonic-gate 					    KSTAT_TYPE_NAMED)
30810Sstevel@tonic-gate 						(void) safe_kstat_read(kc,
30820Sstevel@tonic-gate 						    ksp, NULL);
30830Sstevel@tonic-gate 
30840Sstevel@tonic-gate 					t.ipackets = kstat_named_value(ksp,
30850Sstevel@tonic-gate 					    "ipackets");
30860Sstevel@tonic-gate 					t.ierrors = kstat_named_value(ksp,
30870Sstevel@tonic-gate 					    "ierrors");
30880Sstevel@tonic-gate 					t.opackets = kstat_named_value(ksp,
30890Sstevel@tonic-gate 					    "opackets");
30900Sstevel@tonic-gate 					t.oerrors = kstat_named_value(ksp,
30910Sstevel@tonic-gate 					    "oerrors");
30920Sstevel@tonic-gate 					t.collisions = kstat_named_value(ksp,
30930Sstevel@tonic-gate 					    "collisions");
30940Sstevel@tonic-gate 
30950Sstevel@tonic-gate 					if (strcmp(buf, matchname) == 0)
30960Sstevel@tonic-gate 						new6 = t;
30970Sstevel@tonic-gate 
30980Sstevel@tonic-gate 					/* Build the interface list */
30990Sstevel@tonic-gate 
31000Sstevel@tonic-gate 					tlp = malloc(sizeof (struct iflist));
31010Sstevel@tonic-gate 					(void) strlcpy(tlp->ifname, buf,
31020Sstevel@tonic-gate 					    sizeof (tlp->ifname));
31030Sstevel@tonic-gate 					tlp->tot = t;
31040Sstevel@tonic-gate 					*nextnew = tlp;
31050Sstevel@tonic-gate 					nextnew = &tlp->next_if;
31060Sstevel@tonic-gate 
31070Sstevel@tonic-gate 					/*
31080Sstevel@tonic-gate 					 * First time through.
31090Sstevel@tonic-gate 					 * Just add up the interface stats.
31100Sstevel@tonic-gate 					 */
31110Sstevel@tonic-gate 
31120Sstevel@tonic-gate 					if (oldlist6 == NULL) {
31130Sstevel@tonic-gate 						if_stat_total(&zerostat,
31140Sstevel@tonic-gate 						    &t, &sum6);
31150Sstevel@tonic-gate 						continue;
31160Sstevel@tonic-gate 					}
31170Sstevel@tonic-gate 
31180Sstevel@tonic-gate 					/*
31190Sstevel@tonic-gate 					 * Walk old list for the interface.
31200Sstevel@tonic-gate 					 *
31210Sstevel@tonic-gate 					 * If found, add difference to total.
31220Sstevel@tonic-gate 					 *
31230Sstevel@tonic-gate 					 * If not, an interface has been plumbed
31240Sstevel@tonic-gate 					 * up.  In this case, we will simply
31250Sstevel@tonic-gate 					 * ignore the new interface until the
31260Sstevel@tonic-gate 					 * next interval; as there's no easy way
31270Sstevel@tonic-gate 					 * to acquire statistics between time
31280Sstevel@tonic-gate 					 * of the plumb and the next interval
31290Sstevel@tonic-gate 					 * boundary.  This results in inaccurate
31300Sstevel@tonic-gate 					 * total values for current interval.
31310Sstevel@tonic-gate 					 *
31320Sstevel@tonic-gate 					 * Note the case when an interface is
31330Sstevel@tonic-gate 					 * unplumbed; as similar problems exist.
31340Sstevel@tonic-gate 					 * The unplumbed interface is not in the
31350Sstevel@tonic-gate 					 * current list, and there's no easy way
31360Sstevel@tonic-gate 					 * to account for the statistics between
31370Sstevel@tonic-gate 					 * the previous interval and time of the
31380Sstevel@tonic-gate 					 * unplumb.  Therefore, we (in a sense)
31390Sstevel@tonic-gate 					 * ignore the removed interface by only
31400Sstevel@tonic-gate 					 * involving "current" interfaces when
31410Sstevel@tonic-gate 					 * computing the total statistics.
31420Sstevel@tonic-gate 					 * Unfortunately, this also results in
31430Sstevel@tonic-gate 					 * inaccurate values for interval total.
31440Sstevel@tonic-gate 					 */
31450Sstevel@tonic-gate 
31460Sstevel@tonic-gate 					for (walkold = oldlist6;
31470Sstevel@tonic-gate 					    walkold != NULL;
31480Sstevel@tonic-gate 					    walkold = walkold->next_if) {
31490Sstevel@tonic-gate 						if (strcmp(walkold->ifname,
31500Sstevel@tonic-gate 						    buf) == 0) {
31510Sstevel@tonic-gate 							if_stat_total(
31520Sstevel@tonic-gate 							    &walkold->tot,
31530Sstevel@tonic-gate 							    &t, &sum6);
31540Sstevel@tonic-gate 							break;
31550Sstevel@tonic-gate 						}
31560Sstevel@tonic-gate 					}
31570Sstevel@tonic-gate 
31580Sstevel@tonic-gate 				} /* 'for' loop 2f ends */
31590Sstevel@tonic-gate 
31600Sstevel@tonic-gate 				*nextnew = NULL;
31610Sstevel@tonic-gate 
31620Sstevel@tonic-gate 				(void) printf("%-7llu %-5llu %-7llu "
31630Sstevel@tonic-gate 				    "%-5llu %-6llu ",
31640Sstevel@tonic-gate 				    new6.ipackets - old6.ipackets,
31650Sstevel@tonic-gate 				    new6.ierrors - old6.ierrors,
31660Sstevel@tonic-gate 				    new6.opackets - old6.opackets,
31670Sstevel@tonic-gate 				    new6.oerrors - old6.oerrors,
31680Sstevel@tonic-gate 				    new6.collisions - old6.collisions);
31690Sstevel@tonic-gate 
31700Sstevel@tonic-gate 				(void) printf("%-7llu %-5llu %-7llu "
31710Sstevel@tonic-gate 				    "%-5llu %-6llu\n", sum6.ipackets,
31720Sstevel@tonic-gate 				    sum6.ierrors, sum6.opackets,
31730Sstevel@tonic-gate 				    sum6.oerrors, sum6.collisions);
31740Sstevel@tonic-gate 
31750Sstevel@tonic-gate 				/*
31760Sstevel@tonic-gate 				 * Tidy things up once finished.
31770Sstevel@tonic-gate 				 */
31780Sstevel@tonic-gate 
31790Sstevel@tonic-gate 				old6 = new6;
31800Sstevel@tonic-gate 				cleanlist = oldlist6;
31810Sstevel@tonic-gate 				oldlist6 = newlist6;
31820Sstevel@tonic-gate 				while (cleanlist != NULL) {
31830Sstevel@tonic-gate 					tlp = cleanlist->next_if;
31840Sstevel@tonic-gate 					free(cleanlist);
31850Sstevel@tonic-gate 					cleanlist = tlp;
31860Sstevel@tonic-gate 				}
31870Sstevel@tonic-gate 			}
31880Sstevel@tonic-gate 			break;
31890Sstevel@tonic-gate 		}
31900Sstevel@tonic-gate 		}
31910Sstevel@tonic-gate 		if (Iflag_only == 0)
31920Sstevel@tonic-gate 		    (void) putchar('\n');
31930Sstevel@tonic-gate 		(void) fflush(stdout);
31940Sstevel@tonic-gate 	} /* 'for' loop 1 ends */
31950Sstevel@tonic-gate 	reentry = B_TRUE;
31960Sstevel@tonic-gate }
31970Sstevel@tonic-gate 
31980Sstevel@tonic-gate static void
31990Sstevel@tonic-gate if_report_ip4(mib2_ipAddrEntry_t *ap,
32000Sstevel@tonic-gate 	char ifname[], char logintname[], struct ifstat *statptr,
32010Sstevel@tonic-gate 	boolean_t ksp_not_null) {
32020Sstevel@tonic-gate 
32030Sstevel@tonic-gate 	char abuf[MAXHOSTNAMELEN + 1];
32040Sstevel@tonic-gate 	char dstbuf[MAXHOSTNAMELEN + 1];
32050Sstevel@tonic-gate 
32060Sstevel@tonic-gate 	if (ksp_not_null) {
32070Sstevel@tonic-gate 		(void) printf("%-5s %-5u",
32080Sstevel@tonic-gate 		    ifname, ap->ipAdEntInfo.ae_mtu);
32090Sstevel@tonic-gate 		if (ap->ipAdEntInfo.ae_flags & IFF_POINTOPOINT)
32100Sstevel@tonic-gate 			(void) pr_addr(ap->ipAdEntInfo.ae_pp_dst_addr,
32110Sstevel@tonic-gate 			    abuf, sizeof (abuf));
32120Sstevel@tonic-gate 		else
32130Sstevel@tonic-gate 			(void) pr_netaddr(ap->ipAdEntAddr,
32140Sstevel@tonic-gate 			    ap->ipAdEntNetMask, abuf, sizeof (abuf));
32150Sstevel@tonic-gate 		(void) printf("%-13s %-14s %-6llu %-5llu %-6llu %-5llu "
32160Sstevel@tonic-gate 		    "%-6llu %-6llu\n",
32170Sstevel@tonic-gate 		    abuf, pr_addr(ap->ipAdEntAddr, dstbuf, sizeof (dstbuf)),
32180Sstevel@tonic-gate 		    statptr->ipackets, statptr->ierrors,
32190Sstevel@tonic-gate 		    statptr->opackets, statptr->oerrors,
32200Sstevel@tonic-gate 		    statptr->collisions, 0LL);
32210Sstevel@tonic-gate 	}
32220Sstevel@tonic-gate 	/*
32230Sstevel@tonic-gate 	 * Print logical interface info if Aflag set (including logical unit 0)
32240Sstevel@tonic-gate 	 */
32250Sstevel@tonic-gate 	if (Aflag) {
32260Sstevel@tonic-gate 		*statptr = zerostat;
32270Sstevel@tonic-gate 		statptr->ipackets = ap->ipAdEntInfo.ae_ibcnt;
32280Sstevel@tonic-gate 		statptr->opackets = ap->ipAdEntInfo.ae_obcnt;
32290Sstevel@tonic-gate 
32300Sstevel@tonic-gate 		(void) printf("%-5s %-5u", logintname, ap->ipAdEntInfo.ae_mtu);
32310Sstevel@tonic-gate 		if (ap->ipAdEntInfo.ae_flags & IFF_POINTOPOINT)
32320Sstevel@tonic-gate 			(void) pr_addr(ap->ipAdEntInfo.ae_pp_dst_addr, abuf,
32330Sstevel@tonic-gate 			sizeof (abuf));
32340Sstevel@tonic-gate 		else
32350Sstevel@tonic-gate 			(void) pr_netaddr(ap->ipAdEntAddr, ap->ipAdEntNetMask,
32360Sstevel@tonic-gate 			    abuf, sizeof (abuf));
32370Sstevel@tonic-gate 
32380Sstevel@tonic-gate 		(void) printf("%-13s %-14s %-6llu %-5s %-6llu "
32390Sstevel@tonic-gate 		    "%-5s %-6s %-6llu\n", abuf,
32400Sstevel@tonic-gate 		    pr_addr(ap->ipAdEntAddr, dstbuf, sizeof (dstbuf)),
32410Sstevel@tonic-gate 		    statptr->ipackets, "N/A", statptr->opackets, "N/A", "N/A",
32420Sstevel@tonic-gate 		    0LL);
32430Sstevel@tonic-gate 	}
32440Sstevel@tonic-gate }
32450Sstevel@tonic-gate 
32460Sstevel@tonic-gate static void
32470Sstevel@tonic-gate if_report_ip6(mib2_ipv6AddrEntry_t *ap6,
32480Sstevel@tonic-gate 	char ifname[], char logintname[], struct ifstat *statptr,
32490Sstevel@tonic-gate 	boolean_t ksp_not_null) {
32500Sstevel@tonic-gate 
32510Sstevel@tonic-gate 	char abuf[MAXHOSTNAMELEN + 1];
32520Sstevel@tonic-gate 	char dstbuf[MAXHOSTNAMELEN + 1];
32530Sstevel@tonic-gate 
32540Sstevel@tonic-gate 	if (ksp_not_null) {
32550Sstevel@tonic-gate 		(void) printf("%-5s %-5u", ifname, ap6->ipv6AddrInfo.ae_mtu);
32560Sstevel@tonic-gate 		if (ap6->ipv6AddrInfo.ae_flags &
32570Sstevel@tonic-gate 		    IFF_POINTOPOINT) {
32580Sstevel@tonic-gate 			(void) pr_addr6(&ap6->ipv6AddrInfo.ae_pp_dst_addr,
32590Sstevel@tonic-gate 			    abuf, sizeof (abuf));
32600Sstevel@tonic-gate 		} else {
32610Sstevel@tonic-gate 			(void) pr_prefix6(&ap6->ipv6AddrAddress,
32620Sstevel@tonic-gate 			    ap6->ipv6AddrPfxLength, abuf,
32630Sstevel@tonic-gate 			    sizeof (abuf));
32640Sstevel@tonic-gate 		}
32650Sstevel@tonic-gate 		(void) printf("%-27s %-27s %-6llu %-5llu "
32660Sstevel@tonic-gate 		    "%-6llu %-5llu %-6llu\n",
32670Sstevel@tonic-gate 		    abuf, pr_addr6(&ap6->ipv6AddrAddress, dstbuf,
32680Sstevel@tonic-gate 		    sizeof (dstbuf)),
32690Sstevel@tonic-gate 		    statptr->ipackets, statptr->ierrors, statptr->opackets,
32700Sstevel@tonic-gate 		    statptr->oerrors, statptr->collisions);
32710Sstevel@tonic-gate 	}
32720Sstevel@tonic-gate 	/*
32730Sstevel@tonic-gate 	 * Print logical interface info if Aflag set (including logical unit 0)
32740Sstevel@tonic-gate 	 */
32750Sstevel@tonic-gate 	if (Aflag) {
32760Sstevel@tonic-gate 		*statptr = zerostat;
32770Sstevel@tonic-gate 		statptr->ipackets = ap6->ipv6AddrInfo.ae_ibcnt;
32780Sstevel@tonic-gate 		statptr->opackets = ap6->ipv6AddrInfo.ae_obcnt;
32790Sstevel@tonic-gate 
32800Sstevel@tonic-gate 		(void) printf("%-5s %-5u", logintname,
32810Sstevel@tonic-gate 		    ap6->ipv6AddrInfo.ae_mtu);
32820Sstevel@tonic-gate 		if (ap6->ipv6AddrInfo.ae_flags & IFF_POINTOPOINT)
32830Sstevel@tonic-gate 			(void) pr_addr6(&ap6->ipv6AddrInfo.ae_pp_dst_addr,
32840Sstevel@tonic-gate 			    abuf, sizeof (abuf));
32850Sstevel@tonic-gate 		else
32860Sstevel@tonic-gate 			(void) pr_prefix6(&ap6->ipv6AddrAddress,
32870Sstevel@tonic-gate 			    ap6->ipv6AddrPfxLength, abuf, sizeof (abuf));
32880Sstevel@tonic-gate 		(void) printf("%-27s %-27s %-6llu %-5s %-6llu %-5s %-6s\n",
32890Sstevel@tonic-gate 		    abuf, pr_addr6(&ap6->ipv6AddrAddress, dstbuf,
32900Sstevel@tonic-gate 		    sizeof (dstbuf)),
32910Sstevel@tonic-gate 		    statptr->ipackets, "N/A",
32920Sstevel@tonic-gate 		    statptr->opackets, "N/A", "N/A");
32930Sstevel@tonic-gate 	}
32940Sstevel@tonic-gate }
32950Sstevel@tonic-gate 
32960Sstevel@tonic-gate /* --------------------- DHCP_REPORT  (netstat -D) ------------------------- */
32970Sstevel@tonic-gate 
3298*3431Scarlsonj static boolean_t
3299*3431Scarlsonj dhcp_do_ipc(dhcp_ipc_type_t type, const char *ifname, boolean_t printed_one)
33000Sstevel@tonic-gate {
33010Sstevel@tonic-gate 	dhcp_ipc_request_t	*request;
33020Sstevel@tonic-gate 	dhcp_ipc_reply_t	*reply;
33030Sstevel@tonic-gate 	int			error;
33040Sstevel@tonic-gate 
33050Sstevel@tonic-gate 	request = dhcp_ipc_alloc_request(type, ifname, NULL, 0, DHCP_TYPE_NONE);
33060Sstevel@tonic-gate 	if (request == NULL)
33070Sstevel@tonic-gate 		fail(0, "dhcp_do_ipc: out of memory");
33080Sstevel@tonic-gate 
33090Sstevel@tonic-gate 	error = dhcp_ipc_make_request(request, &reply, DHCP_IPC_WAIT_DEFAULT);
33100Sstevel@tonic-gate 	if (error != 0) {
33110Sstevel@tonic-gate 		free(request);
33120Sstevel@tonic-gate 		fail(0, "dhcp_do_ipc: %s", dhcp_ipc_strerror(error));
33130Sstevel@tonic-gate 	}
33140Sstevel@tonic-gate 
33150Sstevel@tonic-gate 	free(request);
33160Sstevel@tonic-gate 	error = reply->return_code;
3317*3431Scarlsonj 	if (error == DHCP_IPC_E_UNKIF) {
3318*3431Scarlsonj 		free(reply);
3319*3431Scarlsonj 		return (printed_one);
3320*3431Scarlsonj 	}
33210Sstevel@tonic-gate 	if (error != 0) {
33220Sstevel@tonic-gate 		free(reply);
33230Sstevel@tonic-gate 		fail(0, "dhcp_do_ipc: %s", dhcp_ipc_strerror(error));
33240Sstevel@tonic-gate 	}
33250Sstevel@tonic-gate 
3326*3431Scarlsonj 	if (!printed_one)
3327*3431Scarlsonj 		(void) printf("%s", dhcp_status_hdr_string());
3328*3431Scarlsonj 
3329*3431Scarlsonj 	(void) printf("%s", dhcp_status_reply_to_string(reply));
3330*3431Scarlsonj 	free(reply);
3331*3431Scarlsonj 	return (B_TRUE);
33320Sstevel@tonic-gate }
33330Sstevel@tonic-gate 
33340Sstevel@tonic-gate /*
3335*3431Scarlsonj  * dhcp_walk_interfaces: walk the list of interfaces that have a given set of
3336*3431Scarlsonj  * flags turned on (flags_on) and a given set turned off (flags_off) for a
3337*3431Scarlsonj  * given address family (af).  For each, print out the DHCP status using
3338*3431Scarlsonj  * dhcp_do_ipc.
33390Sstevel@tonic-gate  */
3340*3431Scarlsonj static boolean_t
3341*3431Scarlsonj dhcp_walk_interfaces(uint_t flags_on, uint_t flags_off, int af,
3342*3431Scarlsonj     boolean_t printed_one)
33430Sstevel@tonic-gate {
3344*3431Scarlsonj 	struct lifnum	lifn;
3345*3431Scarlsonj 	struct lifconf	lifc;
33460Sstevel@tonic-gate 	int		n_ifs, i, sock_fd;
3347*3431Scarlsonj 
3348*3431Scarlsonj 	sock_fd = socket(af, SOCK_DGRAM, 0);
33490Sstevel@tonic-gate 	if (sock_fd == -1)
3350*3431Scarlsonj 		return (printed_one);
3351*3431Scarlsonj 
3352*3431Scarlsonj 	/*
3353*3431Scarlsonj 	 * SIOCGLIFNUM is just an estimate.  If the ioctl fails, we don't care;
3354*3431Scarlsonj 	 * just drive on and use SIOCGLIFCONF with increasing buffer sizes, as
3355*3431Scarlsonj 	 * is traditional.
3356*3431Scarlsonj 	 */
3357*3431Scarlsonj 	(void) memset(&lifn, 0, sizeof (lifn));
3358*3431Scarlsonj 	lifn.lifn_family = af;
3359*3431Scarlsonj 	lifn.lifn_flags = LIFC_ALLZONES | LIFC_NOXMIT;
3360*3431Scarlsonj 	if (ioctl(sock_fd, SIOCGLIFNUM, &lifn) == -1)
3361*3431Scarlsonj 		n_ifs = LIFN_GUARD_VALUE;
3362*3431Scarlsonj 	else
3363*3431Scarlsonj 		n_ifs = lifn.lifn_count + LIFN_GUARD_VALUE;
3364*3431Scarlsonj 
3365*3431Scarlsonj 	(void) memset(&lifc, 0, sizeof (lifc));
3366*3431Scarlsonj 	lifc.lifc_family = af;
3367*3431Scarlsonj 	lifc.lifc_flags = lifn.lifn_flags;
3368*3431Scarlsonj 	lifc.lifc_len = n_ifs * sizeof (struct lifreq);
3369*3431Scarlsonj 	lifc.lifc_buf = malloc(lifc.lifc_len);
3370*3431Scarlsonj 	if (lifc.lifc_buf != NULL) {
3371*3431Scarlsonj 
3372*3431Scarlsonj 		if (ioctl(sock_fd, SIOCGLIFCONF, &lifc) == -1) {
33730Sstevel@tonic-gate 			(void) close(sock_fd);
3374*3431Scarlsonj 			free(lifc.lifc_buf);
33750Sstevel@tonic-gate 			return (NULL);
33760Sstevel@tonic-gate 		}
33770Sstevel@tonic-gate 
3378*3431Scarlsonj 		n_ifs = lifc.lifc_len / sizeof (struct lifreq);
3379*3431Scarlsonj 
33800Sstevel@tonic-gate 		for (i = 0; i < n_ifs; i++) {
3381*3431Scarlsonj 			if (ioctl(sock_fd, SIOCGLIFFLAGS, &lifc.lifc_req[i]) ==
3382*3431Scarlsonj 			    0 && (lifc.lifc_req[i].lifr_flags & (flags_on |
3383*3431Scarlsonj 			    flags_off)) != flags_on)
3384*3431Scarlsonj 				continue;
3385*3431Scarlsonj 			printed_one = dhcp_do_ipc(DHCP_STATUS |
3386*3431Scarlsonj 			    (af == AF_INET6 ? DHCP_V6 : 0),
3387*3431Scarlsonj 			    lifc.lifc_req[i].lifr_name, printed_one);
3388*3431Scarlsonj 		}
33890Sstevel@tonic-gate 	}
33900Sstevel@tonic-gate 	(void) close(sock_fd);
3391*3431Scarlsonj 	free(lifc.lifc_buf);
3392*3431Scarlsonj 	return (printed_one);
33930Sstevel@tonic-gate }
33940Sstevel@tonic-gate 
33950Sstevel@tonic-gate static void
33960Sstevel@tonic-gate dhcp_report(char *ifname)
33970Sstevel@tonic-gate {
3398*3431Scarlsonj 	boolean_t printed_one;
3399*3431Scarlsonj 
3400*3431Scarlsonj 	if (!family_selected(AF_INET) && !family_selected(AF_INET6))
34010Sstevel@tonic-gate 		return;
34020Sstevel@tonic-gate 
3403*3431Scarlsonj 	printed_one = B_FALSE;
3404*3431Scarlsonj 	if (ifname != NULL) {
3405*3431Scarlsonj 		if (family_selected(AF_INET)) {
3406*3431Scarlsonj 			printed_one = dhcp_do_ipc(DHCP_STATUS, ifname,
3407*3431Scarlsonj 			    printed_one);
3408*3431Scarlsonj 		}
3409*3431Scarlsonj 		if (family_selected(AF_INET6)) {
3410*3431Scarlsonj 			printed_one = dhcp_do_ipc(DHCP_STATUS | DHCP_V6,
3411*3431Scarlsonj 			    ifname, printed_one);
3412*3431Scarlsonj 		}
3413*3431Scarlsonj 		if (!printed_one) {
3414*3431Scarlsonj 			fail(0, "%s: %s", ifname,
3415*3431Scarlsonj 			    dhcp_ipc_strerror(DHCP_IPC_E_UNKIF));
3416*3431Scarlsonj 		}
3417*3431Scarlsonj 	} else {
3418*3431Scarlsonj 		if (family_selected(AF_INET)) {
3419*3431Scarlsonj 			printed_one = dhcp_walk_interfaces(IFF_DHCPRUNNING,
3420*3431Scarlsonj 			    0, AF_INET, printed_one);
3421*3431Scarlsonj 		}
3422*3431Scarlsonj 		if (family_selected(AF_INET6)) {
3423*3431Scarlsonj 			(void) dhcp_walk_interfaces(IFF_DHCPRUNNING,
3424*3431Scarlsonj 			    IFF_ADDRCONF, AF_INET6, printed_one);
3425*3431Scarlsonj 		}
34260Sstevel@tonic-gate 	}
34270Sstevel@tonic-gate }
34280Sstevel@tonic-gate 
34290Sstevel@tonic-gate /* --------------------- GROUP_REPORT (netstat -g) ------------------------- */
34300Sstevel@tonic-gate 
34310Sstevel@tonic-gate static void
34320Sstevel@tonic-gate group_report(mib_item_t *item)
34330Sstevel@tonic-gate {
34340Sstevel@tonic-gate 	mib_item_t	*v4grp = NULL, *v4src = NULL;
34350Sstevel@tonic-gate 	mib_item_t	*v6grp = NULL, *v6src = NULL;
34360Sstevel@tonic-gate 	int		jtemp = 0;
34370Sstevel@tonic-gate 	char		ifname[LIFNAMSIZ + 1];
34380Sstevel@tonic-gate 	char		abuf[MAXHOSTNAMELEN + 1];
34390Sstevel@tonic-gate 	ip_member_t	*ipmp;
34400Sstevel@tonic-gate 	ip_grpsrc_t	*ips;
34410Sstevel@tonic-gate 	ipv6_member_t	*ipmp6;
34420Sstevel@tonic-gate 	ipv6_grpsrc_t	*ips6;
34430Sstevel@tonic-gate 	char		*ifnamep;
34440Sstevel@tonic-gate 	boolean_t	first, first_src;
34450Sstevel@tonic-gate 
34460Sstevel@tonic-gate 	/* 'for' loop 1: */
34470Sstevel@tonic-gate 	for (; item; item = item->next_item) {
34480Sstevel@tonic-gate 		if (Dflag) {
34490Sstevel@tonic-gate 			(void) printf("\n--- Entry %d ---\n", ++jtemp);
34500Sstevel@tonic-gate 			(void) printf("Group = %d, mib_id = %d, "
34510Sstevel@tonic-gate 			    "length = %d, valp = 0x%p\n",
34520Sstevel@tonic-gate 			    item->group, item->mib_id, item->length,
34530Sstevel@tonic-gate 			    item->valp);
34540Sstevel@tonic-gate 		}
34550Sstevel@tonic-gate 		if (item->group == MIB2_IP && family_selected(AF_INET)) {
34560Sstevel@tonic-gate 			switch (item->mib_id) {
34570Sstevel@tonic-gate 			case EXPER_IP_GROUP_MEMBERSHIP:
34580Sstevel@tonic-gate 				v4grp = item;
34590Sstevel@tonic-gate 				if (Dflag)
34600Sstevel@tonic-gate 					(void) printf("item is v4grp info\n");
34610Sstevel@tonic-gate 				break;
34620Sstevel@tonic-gate 			case EXPER_IP_GROUP_SOURCES:
34630Sstevel@tonic-gate 				v4src = item;
34640Sstevel@tonic-gate 				if (Dflag)
34650Sstevel@tonic-gate 					(void) printf("item is v4src info\n");
34660Sstevel@tonic-gate 				break;
34670Sstevel@tonic-gate 			default:
34680Sstevel@tonic-gate 				continue;
34690Sstevel@tonic-gate 			}
34700Sstevel@tonic-gate 			continue;
34710Sstevel@tonic-gate 		}
34720Sstevel@tonic-gate 		if (item->group == MIB2_IP6 && family_selected(AF_INET6)) {
34730Sstevel@tonic-gate 			switch (item->mib_id) {
34740Sstevel@tonic-gate 			case EXPER_IP6_GROUP_MEMBERSHIP:
34750Sstevel@tonic-gate 				v6grp = item;
34760Sstevel@tonic-gate 				if (Dflag)
34770Sstevel@tonic-gate 					(void) printf("item is v6grp info\n");
34780Sstevel@tonic-gate 				break;
34790Sstevel@tonic-gate 			case EXPER_IP6_GROUP_SOURCES:
34800Sstevel@tonic-gate 				v6src = item;
34810Sstevel@tonic-gate 				if (Dflag)
34820Sstevel@tonic-gate 					(void) printf("item is v6src info\n");
34830Sstevel@tonic-gate 				break;
34840Sstevel@tonic-gate 			default:
34850Sstevel@tonic-gate 				continue;
34860Sstevel@tonic-gate 			}
34870Sstevel@tonic-gate 		}
34880Sstevel@tonic-gate 	}
34890Sstevel@tonic-gate 
34900Sstevel@tonic-gate 	if (family_selected(AF_INET) && v4grp != NULL) {
34910Sstevel@tonic-gate 		if (Dflag)
34920Sstevel@tonic-gate 			(void) printf("%u records for ipGroupMember:\n",
34930Sstevel@tonic-gate 			    v4grp->length / sizeof (ip_member_t));
34940Sstevel@tonic-gate 
34950Sstevel@tonic-gate 		first = B_TRUE;
34960Sstevel@tonic-gate 		for (ipmp = (ip_member_t *)v4grp->valp;
34970Sstevel@tonic-gate 		    (char *)ipmp < (char *)v4grp->valp + v4grp->length;
34980Sstevel@tonic-gate 		    /* LINTED: (note 1) */
34990Sstevel@tonic-gate 		    ipmp = (ip_member_t *)((char *)ipmp + ipMemberEntrySize)) {
35000Sstevel@tonic-gate 			if (first) {
35010Sstevel@tonic-gate 				(void) puts(v4compat ?
35020Sstevel@tonic-gate 				    "Group Memberships" :
35030Sstevel@tonic-gate 				    "Group Memberships: IPv4");
35040Sstevel@tonic-gate 				(void) puts("Interface "
35050Sstevel@tonic-gate 				    "Group                RefCnt");
35060Sstevel@tonic-gate 				(void) puts("--------- "
35070Sstevel@tonic-gate 				    "-------------------- ------");
35080Sstevel@tonic-gate 				first = B_FALSE;
35090Sstevel@tonic-gate 			}
35100Sstevel@tonic-gate 
35110Sstevel@tonic-gate 			(void) printf("%-9s %-20s %6u\n",
35120Sstevel@tonic-gate 			    octetstr(&ipmp->ipGroupMemberIfIndex, 'a',
35130Sstevel@tonic-gate 			    ifname, sizeof (ifname)),
35140Sstevel@tonic-gate 			    pr_addr(ipmp->ipGroupMemberAddress,
35150Sstevel@tonic-gate 			    abuf, sizeof (abuf)),
35160Sstevel@tonic-gate 			    ipmp->ipGroupMemberRefCnt);
35170Sstevel@tonic-gate 
35180Sstevel@tonic-gate 
35190Sstevel@tonic-gate 			if (!Vflag || v4src == NULL)
35200Sstevel@tonic-gate 				continue;
35210Sstevel@tonic-gate 
35220Sstevel@tonic-gate 			if (Dflag)
35230Sstevel@tonic-gate 				(void) printf("scanning %u ipGroupSource "
35240Sstevel@tonic-gate 				    "records...\n",
35250Sstevel@tonic-gate 				    v4src->length/sizeof (ip_grpsrc_t));
35260Sstevel@tonic-gate 
35270Sstevel@tonic-gate 			first_src = B_TRUE;
35280Sstevel@tonic-gate 			for (ips = (ip_grpsrc_t *)v4src->valp;
35290Sstevel@tonic-gate 			    (char *)ips < (char *)v4src->valp + v4src->length;
35300Sstevel@tonic-gate 			    /* LINTED: (note 1) */
35310Sstevel@tonic-gate 			    ips = (ip_grpsrc_t *)((char *)ips +
35320Sstevel@tonic-gate 			    ipGroupSourceEntrySize)) {
35330Sstevel@tonic-gate 				/*
35340Sstevel@tonic-gate 				 * We assume that all source addrs for a given
35350Sstevel@tonic-gate 				 * interface/group pair are contiguous, so on
35360Sstevel@tonic-gate 				 * the first non-match after we've found at
35370Sstevel@tonic-gate 				 * least one, we bail.
35380Sstevel@tonic-gate 				 */
35390Sstevel@tonic-gate 				if ((ipmp->ipGroupMemberAddress !=
35400Sstevel@tonic-gate 				    ips->ipGroupSourceGroup) ||
35410Sstevel@tonic-gate 				    (!octetstrmatch(&ipmp->ipGroupMemberIfIndex,
35420Sstevel@tonic-gate 				    &ips->ipGroupSourceIfIndex))) {
35430Sstevel@tonic-gate 					if (first_src)
35440Sstevel@tonic-gate 						continue;
35450Sstevel@tonic-gate 					else
35460Sstevel@tonic-gate 						break;
35470Sstevel@tonic-gate 				}
35480Sstevel@tonic-gate 				if (first_src) {
35490Sstevel@tonic-gate 					(void) printf("\t%s:    %s\n",
35500Sstevel@tonic-gate 					    fmodestr(
35510Sstevel@tonic-gate 					    ipmp->ipGroupMemberFilterMode),
35520Sstevel@tonic-gate 					    pr_addr(ips->ipGroupSourceAddress,
35530Sstevel@tonic-gate 					    abuf, sizeof (abuf)));
35540Sstevel@tonic-gate 					first_src = B_FALSE;
35550Sstevel@tonic-gate 					continue;
35560Sstevel@tonic-gate 				}
35570Sstevel@tonic-gate 
35580Sstevel@tonic-gate 				(void) printf("\t            %s\n",
35590Sstevel@tonic-gate 				    pr_addr(ips->ipGroupSourceAddress, abuf,
35600Sstevel@tonic-gate 				    sizeof (abuf)));
35610Sstevel@tonic-gate 			}
35620Sstevel@tonic-gate 		}
35630Sstevel@tonic-gate 		(void) putchar('\n');
35640Sstevel@tonic-gate 	}
35650Sstevel@tonic-gate 
35660Sstevel@tonic-gate 	if (family_selected(AF_INET6) && v6grp != NULL) {
35670Sstevel@tonic-gate 		if (Dflag)
35680Sstevel@tonic-gate 			(void) printf("%u records for ipv6GroupMember:\n",
35690Sstevel@tonic-gate 			    v6grp->length / sizeof (ipv6_member_t));
35700Sstevel@tonic-gate 
35710Sstevel@tonic-gate 		first = B_TRUE;
35720Sstevel@tonic-gate 		for (ipmp6 = (ipv6_member_t *)v6grp->valp;
35730Sstevel@tonic-gate 		    (char *)ipmp6 < (char *)v6grp->valp + v6grp->length;
35740Sstevel@tonic-gate 		    /* LINTED: (note 1) */
35750Sstevel@tonic-gate 		    ipmp6 = (ipv6_member_t *)((char *)ipmp6 +
35760Sstevel@tonic-gate 			ipv6MemberEntrySize)) {
35770Sstevel@tonic-gate 			if (first) {
35780Sstevel@tonic-gate 				(void) puts("Group Memberships: "
35790Sstevel@tonic-gate 				    "IPv6");
35800Sstevel@tonic-gate 				(void) puts(" If       "
35810Sstevel@tonic-gate 				    "Group                   RefCnt");
35820Sstevel@tonic-gate 				(void) puts("----- "
35830Sstevel@tonic-gate 				    "--------------------------- ------");
35840Sstevel@tonic-gate 				first = B_FALSE;
35850Sstevel@tonic-gate 			}
35860Sstevel@tonic-gate 
35870Sstevel@tonic-gate 			ifnamep = if_indextoname(
35880Sstevel@tonic-gate 			    ipmp6->ipv6GroupMemberIfIndex, ifname);
35890Sstevel@tonic-gate 			if (ifnamep == NULL) {
35900Sstevel@tonic-gate 				(void) printf("Invalid ifindex %d\n",
35910Sstevel@tonic-gate 				    ipmp6->ipv6GroupMemberIfIndex);
35920Sstevel@tonic-gate 				continue;
35930Sstevel@tonic-gate 			}
35940Sstevel@tonic-gate 			(void) printf("%-5s %-27s %5u\n",
35950Sstevel@tonic-gate 			    ifnamep,
35960Sstevel@tonic-gate 			    pr_addr6(&ipmp6->ipv6GroupMemberAddress,
35970Sstevel@tonic-gate 			    abuf, sizeof (abuf)),
35980Sstevel@tonic-gate 			    ipmp6->ipv6GroupMemberRefCnt);
35990Sstevel@tonic-gate 
36000Sstevel@tonic-gate 			if (!Vflag || v6src == NULL)
36010Sstevel@tonic-gate 				continue;
36020Sstevel@tonic-gate 
36030Sstevel@tonic-gate 			if (Dflag)
36040Sstevel@tonic-gate 				(void) printf("scanning %u ipv6GroupSource "
36050Sstevel@tonic-gate 				    "records...\n",
36060Sstevel@tonic-gate 				    v6src->length/sizeof (ipv6_grpsrc_t));
36070Sstevel@tonic-gate 
36080Sstevel@tonic-gate 			first_src = B_TRUE;
36090Sstevel@tonic-gate 			for (ips6 = (ipv6_grpsrc_t *)v6src->valp;
36100Sstevel@tonic-gate 			    (char *)ips6 < (char *)v6src->valp + v6src->length;
36110Sstevel@tonic-gate 			    /* LINTED: (note 1) */
36120Sstevel@tonic-gate 			    ips6 = (ipv6_grpsrc_t *)((char *)ips6 +
36130Sstevel@tonic-gate 			    ipv6GroupSourceEntrySize)) {
36140Sstevel@tonic-gate 				/* same assumption as in the v4 case above */
36150Sstevel@tonic-gate 				if ((ipmp6->ipv6GroupMemberIfIndex !=
36160Sstevel@tonic-gate 				    ips6->ipv6GroupSourceIfIndex) ||
36170Sstevel@tonic-gate 				    (!IN6_ARE_ADDR_EQUAL(
36180Sstevel@tonic-gate 				    &ipmp6->ipv6GroupMemberAddress,
36190Sstevel@tonic-gate 				    &ips6->ipv6GroupSourceGroup))) {
36200Sstevel@tonic-gate 					if (first_src)
36210Sstevel@tonic-gate 						continue;
36220Sstevel@tonic-gate 					else
36230Sstevel@tonic-gate 						break;
36240Sstevel@tonic-gate 				}
36250Sstevel@tonic-gate 				if (first_src) {
36260Sstevel@tonic-gate 					(void) printf("\t%s:    %s\n",
36270Sstevel@tonic-gate 					    fmodestr(
36280Sstevel@tonic-gate 					    ipmp6->ipv6GroupMemberFilterMode),
36290Sstevel@tonic-gate 					    pr_addr6(
36300Sstevel@tonic-gate 					    &ips6->ipv6GroupSourceAddress,
36310Sstevel@tonic-gate 					    abuf, sizeof (abuf)));
36320Sstevel@tonic-gate 					first_src = B_FALSE;
36330Sstevel@tonic-gate 					continue;
36340Sstevel@tonic-gate 				}
36350Sstevel@tonic-gate 
36360Sstevel@tonic-gate 				(void) printf("\t            %s\n",
36370Sstevel@tonic-gate 				    pr_addr6(&ips6->ipv6GroupSourceAddress,
36380Sstevel@tonic-gate 				    abuf, sizeof (abuf)));
36390Sstevel@tonic-gate 			}
36400Sstevel@tonic-gate 		}
36410Sstevel@tonic-gate 		(void) putchar('\n');
36420Sstevel@tonic-gate 	}
36430Sstevel@tonic-gate 
36440Sstevel@tonic-gate 	(void) putchar('\n');
36450Sstevel@tonic-gate 	(void) fflush(stdout);
36460Sstevel@tonic-gate }
36470Sstevel@tonic-gate 
36480Sstevel@tonic-gate /* --------------------- ARP_REPORT (netstat -p) -------------------------- */
36490Sstevel@tonic-gate 
36500Sstevel@tonic-gate static void
36510Sstevel@tonic-gate arp_report(mib_item_t *item)
36520Sstevel@tonic-gate {
36530Sstevel@tonic-gate 	int		jtemp = 0;
36540Sstevel@tonic-gate 	char		ifname[LIFNAMSIZ + 1];
36550Sstevel@tonic-gate 	char		abuf[MAXHOSTNAMELEN + 1];
36560Sstevel@tonic-gate 	char		maskbuf[STR_EXPAND * OCTET_LENGTH + 1];
36570Sstevel@tonic-gate 	char		flbuf[32];	/* ACE_F_ flags */
36580Sstevel@tonic-gate 	char		xbuf[STR_EXPAND * OCTET_LENGTH + 1];
36590Sstevel@tonic-gate 	mib2_ipNetToMediaEntry_t	*np;
36600Sstevel@tonic-gate 	int		flags;
36610Sstevel@tonic-gate 	boolean_t	first;
36620Sstevel@tonic-gate 
36630Sstevel@tonic-gate 	if (!(family_selected(AF_INET)))
36640Sstevel@tonic-gate 		return;
36650Sstevel@tonic-gate 
36660Sstevel@tonic-gate 	/* 'for' loop 1: */
36670Sstevel@tonic-gate 	for (; item; item = item->next_item) {
36680Sstevel@tonic-gate 		if (Dflag) {
36690Sstevel@tonic-gate 			(void) printf("\n--- Entry %d ---\n", ++jtemp);
36700Sstevel@tonic-gate 			(void) printf("Group = %d, mib_id = %d, "
36710Sstevel@tonic-gate 			    "length = %d, valp = 0x%p\n",
36720Sstevel@tonic-gate 			    item->group, item->mib_id, item->length,
36730Sstevel@tonic-gate 			    item->valp);
36740Sstevel@tonic-gate 		}
36750Sstevel@tonic-gate 		if (!(item->group == MIB2_IP && item->mib_id == MIB2_IP_MEDIA))
36760Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
36770Sstevel@tonic-gate 
36780Sstevel@tonic-gate 		if (Dflag)
36790Sstevel@tonic-gate 			(void) printf("%u records for "
36800Sstevel@tonic-gate 			    "ipNetToMediaEntryTable:\n",
36810Sstevel@tonic-gate 			    item->length/sizeof (mib2_ipNetToMediaEntry_t));
36820Sstevel@tonic-gate 
36830Sstevel@tonic-gate 		first = B_TRUE;
36840Sstevel@tonic-gate 		/* 'for' loop 2: */
36850Sstevel@tonic-gate 		for (np = (mib2_ipNetToMediaEntry_t *)item->valp;
36860Sstevel@tonic-gate 		    (char *)np < (char *)item->valp + item->length;
36870Sstevel@tonic-gate 		    /* LINTED: (note 1) */
36880Sstevel@tonic-gate 		    np = (mib2_ipNetToMediaEntry_t *)((char *)np +
36890Sstevel@tonic-gate 		    ipNetToMediaEntrySize)) {
36900Sstevel@tonic-gate 			if (first) {
36910Sstevel@tonic-gate 				(void) puts(v4compat ?
36920Sstevel@tonic-gate 				    "Net to Media Table" :
36930Sstevel@tonic-gate 				    "Net to Media Table: IPv4");
36942546Scarlsonj 				(void) puts("Device "
36952546Scarlsonj 				    "  IP Address               Mask      "
36962546Scarlsonj 				    "Flags      Phys Addr");
36972546Scarlsonj 				(void) puts("------ "
36982546Scarlsonj 				    "-------------------- --------------- "
36992546Scarlsonj 				    "-------- ---------------");
37000Sstevel@tonic-gate 				first = B_FALSE;
37010Sstevel@tonic-gate 			}
37020Sstevel@tonic-gate 
37030Sstevel@tonic-gate 			flbuf[0] = '\0';
37040Sstevel@tonic-gate 			flags = np->ipNetToMediaInfo.ntm_flags;
37052546Scarlsonj 			/*
37062546Scarlsonj 			 * Note that not all flags are possible at the same
37072546Scarlsonj 			 * time.  Patterns: SPLAy DUo
37082546Scarlsonj 			 */
37090Sstevel@tonic-gate 			if (flags & ACE_F_PERMANENT)
37100Sstevel@tonic-gate 				(void) strcat(flbuf, "S");
37110Sstevel@tonic-gate 			if (flags & ACE_F_PUBLISH)
37120Sstevel@tonic-gate 				(void) strcat(flbuf, "P");
37130Sstevel@tonic-gate 			if (flags & ACE_F_DYING)
37140Sstevel@tonic-gate 				(void) strcat(flbuf, "D");
37150Sstevel@tonic-gate 			if (!(flags & ACE_F_RESOLVED))
37160Sstevel@tonic-gate 				(void) strcat(flbuf, "U");
37170Sstevel@tonic-gate 			if (flags & ACE_F_MAPPING)
37180Sstevel@tonic-gate 				(void) strcat(flbuf, "M");
37192546Scarlsonj 			if (flags & ACE_F_MYADDR)
37202546Scarlsonj 				(void) strcat(flbuf, "L");
37212546Scarlsonj 			if (flags & ACE_F_UNVERIFIED)
37222546Scarlsonj 				(void) strcat(flbuf, "d");
37232546Scarlsonj 			if (flags & ACE_F_AUTHORITY)
37242546Scarlsonj 				(void) strcat(flbuf, "A");
37252546Scarlsonj 			if (flags & ACE_F_OLD)
37262546Scarlsonj 				(void) strcat(flbuf, "o");
37272546Scarlsonj 			if (flags & ACE_F_DELAYED)
37282546Scarlsonj 				(void) strcat(flbuf, "y");
37292546Scarlsonj 			(void) printf("%-6s %-20s %-15s %-8s %s\n",
37300Sstevel@tonic-gate 			    octetstr(&np->ipNetToMediaIfIndex, 'a',
37310Sstevel@tonic-gate 			    ifname, sizeof (ifname)),
37320Sstevel@tonic-gate 			    pr_addr(np->ipNetToMediaNetAddress,
37330Sstevel@tonic-gate 			    abuf, sizeof (abuf)),
37340Sstevel@tonic-gate 			    octetstr(&np->ipNetToMediaInfo.ntm_mask, 'd',
37350Sstevel@tonic-gate 			    maskbuf, sizeof (maskbuf)),
37360Sstevel@tonic-gate 			    flbuf,
37370Sstevel@tonic-gate 			    octetstr(&np->ipNetToMediaPhysAddress, 'h',
37380Sstevel@tonic-gate 			    xbuf, sizeof (xbuf)));
37390Sstevel@tonic-gate 		} /* 'for' loop 2 ends */
37400Sstevel@tonic-gate 	} /* 'for' loop 1 ends */
37410Sstevel@tonic-gate 	(void) fflush(stdout);
37420Sstevel@tonic-gate }
37430Sstevel@tonic-gate 
37440Sstevel@tonic-gate /* --------------------- NDP_REPORT (netstat -p) -------------------------- */
37450Sstevel@tonic-gate 
37460Sstevel@tonic-gate static void
37470Sstevel@tonic-gate ndp_report(mib_item_t *item)
37480Sstevel@tonic-gate {
37490Sstevel@tonic-gate 	int		jtemp = 0;
37500Sstevel@tonic-gate 	char		abuf[MAXHOSTNAMELEN + 1];
37510Sstevel@tonic-gate 	char		*state;
37520Sstevel@tonic-gate 	char		*type;
37530Sstevel@tonic-gate 	char		xbuf[STR_EXPAND * OCTET_LENGTH + 1];
37540Sstevel@tonic-gate 	mib2_ipv6NetToMediaEntry_t	*np6;
37550Sstevel@tonic-gate 	char		ifname[LIFNAMSIZ + 1];
37560Sstevel@tonic-gate 	char		*ifnamep;
37570Sstevel@tonic-gate 	boolean_t	first;
37580Sstevel@tonic-gate 
37590Sstevel@tonic-gate 	if (!(family_selected(AF_INET6)))
37600Sstevel@tonic-gate 		return;
37610Sstevel@tonic-gate 
37620Sstevel@tonic-gate 	/* 'for' loop 1: */
37630Sstevel@tonic-gate 	for (; item; item = item->next_item) {
37640Sstevel@tonic-gate 		if (Dflag) {
37650Sstevel@tonic-gate 			(void) printf("\n--- Entry %d ---\n", ++jtemp);
37660Sstevel@tonic-gate 			(void) printf("Group = %d, mib_id = %d, "
37670Sstevel@tonic-gate 			    "length = %d, valp = 0x%p\n",
37680Sstevel@tonic-gate 			    item->group, item->mib_id, item->length,
37690Sstevel@tonic-gate 			    item->valp);
37700Sstevel@tonic-gate 		}
37710Sstevel@tonic-gate 		if (!(item->group == MIB2_IP6 &&
37720Sstevel@tonic-gate 		    item->mib_id == MIB2_IP6_MEDIA))
37730Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
37740Sstevel@tonic-gate 
37750Sstevel@tonic-gate 		first = B_TRUE;
37760Sstevel@tonic-gate 		/* 'for' loop 2: */
37770Sstevel@tonic-gate 		for (np6 = (mib2_ipv6NetToMediaEntry_t *)item->valp;
37780Sstevel@tonic-gate 		    (char *)np6 < (char *)item->valp + item->length;
37790Sstevel@tonic-gate 		    /* LINTED: (note 1) */
37800Sstevel@tonic-gate 		    np6 = (mib2_ipv6NetToMediaEntry_t *)((char *)np6 +
37810Sstevel@tonic-gate 		    ipv6NetToMediaEntrySize)) {
37820Sstevel@tonic-gate 			if (first) {
37830Sstevel@tonic-gate 				(void) puts("\nNet to Media Table: IPv6");
37840Sstevel@tonic-gate 				(void) puts(" If   Physical Address   "
37850Sstevel@tonic-gate 				    " Type      State      Destination/Mask");
37860Sstevel@tonic-gate 				(void) puts("----- -----------------  "
37870Sstevel@tonic-gate 				    "------- ------------ "
37880Sstevel@tonic-gate 				    "---------------------------");
37890Sstevel@tonic-gate 				first = B_FALSE;
37900Sstevel@tonic-gate 			}
37910Sstevel@tonic-gate 
37920Sstevel@tonic-gate 			ifnamep = if_indextoname(np6->ipv6NetToMediaIfIndex,
37930Sstevel@tonic-gate 			    ifname);
37940Sstevel@tonic-gate 			if (ifnamep == NULL) {
37950Sstevel@tonic-gate 				(void) printf("Invalid ifindex %d\n",
37960Sstevel@tonic-gate 				    np6->ipv6NetToMediaIfIndex);
37970Sstevel@tonic-gate 				continue; /* 'for' loop 2 */
37980Sstevel@tonic-gate 			}
37990Sstevel@tonic-gate 			switch (np6->ipv6NetToMediaState) {
38000Sstevel@tonic-gate 			case ND_INCOMPLETE:
38010Sstevel@tonic-gate 				state = "INCOMPLETE";
38020Sstevel@tonic-gate 				break;
38030Sstevel@tonic-gate 			case ND_REACHABLE:
38040Sstevel@tonic-gate 				state = "REACHABLE";
38050Sstevel@tonic-gate 				break;
38060Sstevel@tonic-gate 			case ND_STALE:
38070Sstevel@tonic-gate 				state = "STALE";
38080Sstevel@tonic-gate 				break;
38090Sstevel@tonic-gate 			case ND_DELAY:
38100Sstevel@tonic-gate 				state = "DELAY";
38110Sstevel@tonic-gate 				break;
38120Sstevel@tonic-gate 			case ND_PROBE:
38130Sstevel@tonic-gate 				state = "PROBE";
38140Sstevel@tonic-gate 				break;
38150Sstevel@tonic-gate 			case ND_UNREACHABLE:
38160Sstevel@tonic-gate 				state = "UNREACHABLE";
38170Sstevel@tonic-gate 				break;
38180Sstevel@tonic-gate 			default:
38190Sstevel@tonic-gate 				state = "UNKNOWN";
38200Sstevel@tonic-gate 			}
38210Sstevel@tonic-gate 
38220Sstevel@tonic-gate 			switch (np6->ipv6NetToMediaType) {
38230Sstevel@tonic-gate 			case 1:
38240Sstevel@tonic-gate 				type = "other";
38250Sstevel@tonic-gate 				break;
38260Sstevel@tonic-gate 			case 2:
38270Sstevel@tonic-gate 				type = "dynamic";
38280Sstevel@tonic-gate 				break;
38290Sstevel@tonic-gate 			case 3:
38300Sstevel@tonic-gate 				type = "static";
38310Sstevel@tonic-gate 				break;
38320Sstevel@tonic-gate 			case 4:
38330Sstevel@tonic-gate 				type = "local";
38340Sstevel@tonic-gate 				break;
38350Sstevel@tonic-gate 			}
38360Sstevel@tonic-gate 			(void) printf("%-5s %-17s  %-7s %-12s %-27s\n",
38370Sstevel@tonic-gate 			    ifnamep,
38380Sstevel@tonic-gate 			    octetstr(&np6->ipv6NetToMediaPhysAddress, 'h',
38390Sstevel@tonic-gate 			    xbuf, sizeof (xbuf)),
38400Sstevel@tonic-gate 			    type,
38410Sstevel@tonic-gate 			    state,
38420Sstevel@tonic-gate 			    pr_addr6(&np6->ipv6NetToMediaNetAddress,
38430Sstevel@tonic-gate 			    abuf, sizeof (abuf)));
38440Sstevel@tonic-gate 		} /* 'for' loop 2 ends */
38450Sstevel@tonic-gate 	} /* 'for' loop 1 ends */
38460Sstevel@tonic-gate 	(void) putchar('\n');
38470Sstevel@tonic-gate 	(void) fflush(stdout);
38480Sstevel@tonic-gate }
38490Sstevel@tonic-gate 
38500Sstevel@tonic-gate /* ------------------------- ire_report (netstat -r) ------------------------ */
38510Sstevel@tonic-gate 
38521676Sjpk typedef struct sec_attr_list_s {
38531676Sjpk 	struct sec_attr_list_s *sal_next;
38541676Sjpk 	const mib2_ipAttributeEntry_t *sal_attr;
38551676Sjpk } sec_attr_list_t;
38561676Sjpk 
38571676Sjpk static boolean_t ire_report_item_v4(const mib2_ipRouteEntry_t *, boolean_t,
38581676Sjpk     const sec_attr_list_t *);
38591676Sjpk static boolean_t ire_report_item_v4src(const mib2_ipRouteEntry_t *, boolean_t,
38601676Sjpk     const sec_attr_list_t *);
38611676Sjpk static boolean_t ire_report_item_v6(const mib2_ipv6RouteEntry_t *, boolean_t,
38621676Sjpk     const sec_attr_list_t *);
38631676Sjpk static const char *pr_secattr(const sec_attr_list_t *);
38640Sstevel@tonic-gate 
38650Sstevel@tonic-gate static void
38661676Sjpk ire_report(const mib_item_t *item)
38670Sstevel@tonic-gate {
38680Sstevel@tonic-gate 	int			jtemp = 0;
38690Sstevel@tonic-gate 	boolean_t		print_hdr_once_v4 = B_TRUE;
38700Sstevel@tonic-gate 	boolean_t		print_hdr_once_v6 = B_TRUE;
38710Sstevel@tonic-gate 	mib2_ipRouteEntry_t	*rp;
38720Sstevel@tonic-gate 	mib2_ipv6RouteEntry_t	*rp6;
38731676Sjpk 	sec_attr_list_t		**v4_attrs, **v4a;
38741676Sjpk 	sec_attr_list_t		**v6_attrs, **v6a;
38751676Sjpk 	sec_attr_list_t		*all_attrs, *aptr;
38761676Sjpk 	const mib_item_t	*iptr;
38771676Sjpk 	int			ipv4_route_count, ipv6_route_count;
38781676Sjpk 	int			route_attrs_count;
38791676Sjpk 
38801676Sjpk 	/*
38811676Sjpk 	 * Preparation pass: the kernel returns separate entries for IP routing
38821676Sjpk 	 * table entries and security attributes.  We loop through the
38831676Sjpk 	 * attributes first and link them into lists.
38841676Sjpk 	 */
38851676Sjpk 	ipv4_route_count = ipv6_route_count = route_attrs_count = 0;
38861676Sjpk 	for (iptr = item; iptr != NULL; iptr = iptr->next_item) {
38871676Sjpk 		if (iptr->group == MIB2_IP6 && iptr->mib_id == MIB2_IP6_ROUTE)
38881676Sjpk 			ipv6_route_count += iptr->length / ipv6RouteEntrySize;
38891676Sjpk 		if (iptr->group == MIB2_IP && iptr->mib_id == MIB2_IP_ROUTE)
38901676Sjpk 			ipv4_route_count += iptr->length / ipRouteEntrySize;
38911676Sjpk 		if ((iptr->group == MIB2_IP || iptr->group == MIB2_IP6) &&
38921676Sjpk 		    iptr->mib_id == EXPER_IP_RTATTR)
38931676Sjpk 			route_attrs_count += iptr->length /
38941676Sjpk 			    ipRouteAttributeSize;
38951676Sjpk 	}
38961676Sjpk 	v4_attrs = v6_attrs = NULL;
38971676Sjpk 	all_attrs = NULL;
38981676Sjpk 	if (family_selected(AF_INET) && ipv4_route_count > 0) {
38991676Sjpk 		v4_attrs = calloc(ipv4_route_count, sizeof (*v4_attrs));
39001676Sjpk 		if (v4_attrs == NULL) {
39011676Sjpk 			perror("ire_report calloc v4_attrs failed");
39021676Sjpk 			return;
39031676Sjpk 		}
39041676Sjpk 	}
39051676Sjpk 	if (family_selected(AF_INET6) && ipv6_route_count > 0) {
39061676Sjpk 		v6_attrs = calloc(ipv6_route_count, sizeof (*v6_attrs));
39071676Sjpk 		if (v6_attrs == NULL) {
39081676Sjpk 			perror("ire_report calloc v6_attrs failed");
39091676Sjpk 			goto ire_report_done;
39101676Sjpk 		}
39111676Sjpk 	}
39121676Sjpk 	if (route_attrs_count > 0) {
39131676Sjpk 		all_attrs = malloc(route_attrs_count * sizeof (*all_attrs));
39141676Sjpk 		if (all_attrs == NULL) {
39151676Sjpk 			perror("ire_report malloc all_attrs failed");
39161676Sjpk 			goto ire_report_done;
39171676Sjpk 		}
39181676Sjpk 	}
39191676Sjpk 	aptr = all_attrs;
39201676Sjpk 	for (iptr = item; iptr != NULL; iptr = iptr->next_item) {
39211676Sjpk 		mib2_ipAttributeEntry_t *iae;
39221676Sjpk 		sec_attr_list_t **alp;
39231676Sjpk 
39241676Sjpk 		if (v4_attrs != NULL && iptr->group == MIB2_IP &&
39251676Sjpk 		    iptr->mib_id == EXPER_IP_RTATTR) {
39261676Sjpk 			alp = v4_attrs;
39271676Sjpk 		} else if (v6_attrs != NULL && iptr->group == MIB2_IP6 &&
39281676Sjpk 		    iptr->mib_id == EXPER_IP_RTATTR) {
39291676Sjpk 			alp = v6_attrs;
39301676Sjpk 		} else {
39311676Sjpk 			continue;
39321676Sjpk 		}
39331676Sjpk 		for (iae = iptr->valp;
39341676Sjpk 		    (char *)iae < (char *)iptr->valp + iptr->length;
39351676Sjpk 		    /* LINTED: (note 1) */
39361676Sjpk 		    iae = (mib2_ipAttributeEntry_t *)((char *)iae +
39371676Sjpk 		    ipRouteAttributeSize)) {
39381676Sjpk 			aptr->sal_next = alp[iae->iae_routeidx];
39391676Sjpk 			aptr->sal_attr = iae;
39401676Sjpk 			alp[iae->iae_routeidx] = aptr++;
39411676Sjpk 		}
39421676Sjpk 	}
39430Sstevel@tonic-gate 
39440Sstevel@tonic-gate 	/* 'for' loop 1: */
39451676Sjpk 	v4a = v4_attrs;
39461676Sjpk 	v6a = v6_attrs;
39471676Sjpk 	for (; item != NULL; item = item->next_item) {
39480Sstevel@tonic-gate 		if (Dflag) {
39490Sstevel@tonic-gate 			(void) printf("\n--- Entry %d ---\n", ++jtemp);
39500Sstevel@tonic-gate 			(void) printf("Group = %d, mib_id = %d, "
39510Sstevel@tonic-gate 			    "length = %d, valp = 0x%p\n",
39520Sstevel@tonic-gate 			    item->group, item->mib_id,
39530Sstevel@tonic-gate 			    item->length, item->valp);
39540Sstevel@tonic-gate 		}
39550Sstevel@tonic-gate 		if (!((item->group == MIB2_IP &&
39560Sstevel@tonic-gate 		    item->mib_id == MIB2_IP_ROUTE) ||
39570Sstevel@tonic-gate 		    (item->group == MIB2_IP6 &&
39580Sstevel@tonic-gate 		    item->mib_id == MIB2_IP6_ROUTE)))
39590Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
39600Sstevel@tonic-gate 
39610Sstevel@tonic-gate 		if (item->group == MIB2_IP && !family_selected(AF_INET))
39620Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
39630Sstevel@tonic-gate 		else if (item->group == MIB2_IP6 && !family_selected(AF_INET6))
39640Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
39650Sstevel@tonic-gate 
39660Sstevel@tonic-gate 		if (Dflag) {
39670Sstevel@tonic-gate 			if (item->group == MIB2_IP) {
39680Sstevel@tonic-gate 				(void) printf("%u records for "
39690Sstevel@tonic-gate 				    "ipRouteEntryTable:\n",
39700Sstevel@tonic-gate 				    item->length/sizeof (mib2_ipRouteEntry_t));
39710Sstevel@tonic-gate 			} else {
39720Sstevel@tonic-gate 				(void) printf("%u records for "
39730Sstevel@tonic-gate 				    "ipv6RouteEntryTable:\n",
39740Sstevel@tonic-gate 				    item->length/
39750Sstevel@tonic-gate 				    sizeof (mib2_ipv6RouteEntry_t));
39760Sstevel@tonic-gate 			}
39770Sstevel@tonic-gate 		}
39780Sstevel@tonic-gate 
39790Sstevel@tonic-gate 		if (item->group == MIB2_IP) {
39800Sstevel@tonic-gate 			for (rp = (mib2_ipRouteEntry_t *)item->valp;
39810Sstevel@tonic-gate 			    (char *)rp < (char *)item->valp + item->length;
39820Sstevel@tonic-gate 			    /* LINTED: (note 1) */
39830Sstevel@tonic-gate 			    rp = (mib2_ipRouteEntry_t *)((char *)rp +
39840Sstevel@tonic-gate 			    ipRouteEntrySize)) {
39851676Sjpk 				aptr = v4a == NULL ? NULL : *v4a++;
39860Sstevel@tonic-gate 				print_hdr_once_v4 = ire_report_item_v4(rp,
39871676Sjpk 				    print_hdr_once_v4, aptr);
39880Sstevel@tonic-gate 			}
39891676Sjpk 			if (v4a != NULL)
39901676Sjpk 				v4a -= item->length / ipRouteEntrySize;
39910Sstevel@tonic-gate 			print_hdr_once_v4 = B_TRUE;
39920Sstevel@tonic-gate 			for (rp = (mib2_ipRouteEntry_t *)item->valp;
39930Sstevel@tonic-gate 			    (char *)rp < (char *)item->valp + item->length;
39940Sstevel@tonic-gate 			    /* LINTED: (note 1) */
39950Sstevel@tonic-gate 			    rp = (mib2_ipRouteEntry_t *)((char *)rp +
39960Sstevel@tonic-gate 			    ipRouteEntrySize)) {
39971676Sjpk 				aptr = v4a == NULL ? NULL : *v4a++;
39980Sstevel@tonic-gate 				print_hdr_once_v4 = ire_report_item_v4src(rp,
39991676Sjpk 				    print_hdr_once_v4, aptr);
40000Sstevel@tonic-gate 			}
40010Sstevel@tonic-gate 		} else {
40020Sstevel@tonic-gate 			for (rp6 = (mib2_ipv6RouteEntry_t *)item->valp;
40030Sstevel@tonic-gate 			    (char *)rp6 < (char *)item->valp + item->length;
40040Sstevel@tonic-gate 			    /* LINTED: (note 1) */
40050Sstevel@tonic-gate 			    rp6 = (mib2_ipv6RouteEntry_t *)((char *)rp6 +
40060Sstevel@tonic-gate 			    ipv6RouteEntrySize)) {
40071676Sjpk 				aptr = v6a == NULL ? NULL : *v6a++;
40080Sstevel@tonic-gate 				print_hdr_once_v6 = ire_report_item_v6(rp6,
40091676Sjpk 				    print_hdr_once_v6, aptr);
40100Sstevel@tonic-gate 			}
40110Sstevel@tonic-gate 		}
40120Sstevel@tonic-gate 	} /* 'for' loop 1 ends */
40130Sstevel@tonic-gate 	(void) fflush(stdout);
40141676Sjpk ire_report_done:
40151676Sjpk 	if (v4_attrs != NULL)
40161676Sjpk 		free(v4_attrs);
40171676Sjpk 	if (v6_attrs != NULL)
40181676Sjpk 		free(v6_attrs);
40191676Sjpk 	if (all_attrs != NULL)
40201676Sjpk 		free(all_attrs);
40210Sstevel@tonic-gate }
40220Sstevel@tonic-gate 
40230Sstevel@tonic-gate /*
40240Sstevel@tonic-gate  * Match a user-supplied device name.  We do this by string because
40250Sstevel@tonic-gate  * the MIB2 interface gives us interface name strings rather than
40260Sstevel@tonic-gate  * ifIndex numbers.  The "none" rule matches only routes with no
40270Sstevel@tonic-gate  * interface.  The "any" rule matches routes with any non-blank
40280Sstevel@tonic-gate  * interface.  A base name ("hme0") matches all aliases as well
40290Sstevel@tonic-gate  * ("hme0:1").
40300Sstevel@tonic-gate  */
40310Sstevel@tonic-gate static boolean_t
40320Sstevel@tonic-gate dev_name_match(const DeviceName *devnam, const char *ifname)
40330Sstevel@tonic-gate {
40340Sstevel@tonic-gate 	int iflen;
40350Sstevel@tonic-gate 
40360Sstevel@tonic-gate 	if (ifname == NULL)
40370Sstevel@tonic-gate 		return (devnam->o_length == 0);		/* "none" */
40380Sstevel@tonic-gate 	if (*ifname == '\0')
40390Sstevel@tonic-gate 		return (devnam->o_length != 0);		/* "any" */
40400Sstevel@tonic-gate 	iflen = strlen(ifname);
40410Sstevel@tonic-gate 	/* The check for ':' here supports interface aliases. */
40420Sstevel@tonic-gate 	if (iflen > devnam->o_length ||
40430Sstevel@tonic-gate 	    (iflen < devnam->o_length && devnam->o_bytes[iflen] != ':'))
40440Sstevel@tonic-gate 		return (B_FALSE);
40450Sstevel@tonic-gate 	return (strncmp(ifname, devnam->o_bytes, iflen) == 0);
40460Sstevel@tonic-gate }
40470Sstevel@tonic-gate 
40480Sstevel@tonic-gate /*
40490Sstevel@tonic-gate  * Match a user-supplied IP address list.  The "any" rule matches any
40500Sstevel@tonic-gate  * non-zero address.  The "none" rule matches only the zero address.
40510Sstevel@tonic-gate  * IPv6 addresses supplied by the user are ignored.  If the user
40520Sstevel@tonic-gate  * supplies a subnet mask, then match routes that are at least that
40530Sstevel@tonic-gate  * specific (use the user's mask).  If the user supplies only an
40540Sstevel@tonic-gate  * address, then select any routes that would match (use the route's
40550Sstevel@tonic-gate  * mask).
40560Sstevel@tonic-gate  */
40570Sstevel@tonic-gate static boolean_t
40580Sstevel@tonic-gate v4_addr_match(IpAddress addr, IpAddress mask, const filter_t *fp)
40590Sstevel@tonic-gate {
40600Sstevel@tonic-gate 	char **app;
40610Sstevel@tonic-gate 	char *aptr;
40620Sstevel@tonic-gate 	in_addr_t faddr, fmask;
40630Sstevel@tonic-gate 
40640Sstevel@tonic-gate 	if (fp->u.a.f_address == NULL) {
40650Sstevel@tonic-gate 		if (IN6_IS_ADDR_UNSPECIFIED(&fp->u.a.f_mask))
40660Sstevel@tonic-gate 			return (addr != INADDR_ANY);	/* "any" */
40670Sstevel@tonic-gate 		else
40680Sstevel@tonic-gate 			return (addr == INADDR_ANY);	/* "none" */
40690Sstevel@tonic-gate 	}
40700Sstevel@tonic-gate 	if (!IN6_IS_V4MASK(fp->u.a.f_mask))
40710Sstevel@tonic-gate 		return (B_FALSE);
40720Sstevel@tonic-gate 	IN6_V4MAPPED_TO_IPADDR(&fp->u.a.f_mask, fmask);
40730Sstevel@tonic-gate 	if (fmask != IP_HOST_MASK) {
40740Sstevel@tonic-gate 		if (fmask > mask)
40750Sstevel@tonic-gate 			return (B_FALSE);
40760Sstevel@tonic-gate 		mask = fmask;
40770Sstevel@tonic-gate 	}
40780Sstevel@tonic-gate 	for (app = fp->u.a.f_address->h_addr_list; (aptr = *app) != NULL; app++)
40790Sstevel@tonic-gate 		/* LINTED: (note 1) */
40800Sstevel@tonic-gate 		if (IN6_IS_ADDR_V4MAPPED((in6_addr_t *)aptr)) {
40810Sstevel@tonic-gate 			/* LINTED: (note 1) */
40820Sstevel@tonic-gate 			IN6_V4MAPPED_TO_IPADDR((in6_addr_t *)aptr, faddr);
40830Sstevel@tonic-gate 			if (((faddr ^ addr) & mask) == 0)
40840Sstevel@tonic-gate 				return (B_TRUE);
40850Sstevel@tonic-gate 		}
40860Sstevel@tonic-gate 	return (B_FALSE);
40870Sstevel@tonic-gate }
40880Sstevel@tonic-gate 
40890Sstevel@tonic-gate /*
40900Sstevel@tonic-gate  * Run through the filter list for an IPv4 MIB2 route entry.  If all
40910Sstevel@tonic-gate  * filters of a given type fail to match, then the route is filtered
40920Sstevel@tonic-gate  * out (not displayed).  If no filter is given or at least one filter
40930Sstevel@tonic-gate  * of each type matches, then display the route.
40940Sstevel@tonic-gate  */
40950Sstevel@tonic-gate static boolean_t
40961676Sjpk ire_filter_match_v4(const mib2_ipRouteEntry_t *rp, uint_t flag_b)
40970Sstevel@tonic-gate {
40980Sstevel@tonic-gate 	filter_t *fp;
40990Sstevel@tonic-gate 	int idx;
41000Sstevel@tonic-gate 
41010Sstevel@tonic-gate 	/* 'for' loop 1: */
41020Sstevel@tonic-gate 	for (idx = 0; idx < NFILTERKEYS; idx++)
41030Sstevel@tonic-gate 		if ((fp = filters[idx]) != NULL) {
41040Sstevel@tonic-gate 			/* 'for' loop 2: */
41050Sstevel@tonic-gate 			for (; fp != NULL; fp = fp->f_next) {
41060Sstevel@tonic-gate 				switch (idx) {
41070Sstevel@tonic-gate 				case FK_AF:
41080Sstevel@tonic-gate 					if (fp->u.f_family != AF_INET)
41090Sstevel@tonic-gate 						continue; /* 'for' loop 2 */
41100Sstevel@tonic-gate 					break;
41110Sstevel@tonic-gate 				case FK_INIF:
41120Sstevel@tonic-gate 					if (!dev_name_match(&rp->ipRouteInfo.
41130Sstevel@tonic-gate 					    re_in_ill, fp->u.f_ifname))
41140Sstevel@tonic-gate 						continue; /* 'for' loop 2 */
41150Sstevel@tonic-gate 					break;
41160Sstevel@tonic-gate 				case FK_OUTIF:
41170Sstevel@tonic-gate 					if (!dev_name_match(&rp->ipRouteIfIndex,
41180Sstevel@tonic-gate 					    fp->u.f_ifname))
41190Sstevel@tonic-gate 						continue; /* 'for' loop 2 */
41200Sstevel@tonic-gate 					break;
41210Sstevel@tonic-gate 				case FK_SRC:
41220Sstevel@tonic-gate 					if (!v4_addr_match(rp->ipRouteInfo.
41230Sstevel@tonic-gate 					    re_in_src_addr, IP_HOST_MASK, fp))
41240Sstevel@tonic-gate 						continue; /* 'for' loop 2 */
41250Sstevel@tonic-gate 					break;
41260Sstevel@tonic-gate 				case FK_DST:
41270Sstevel@tonic-gate 					if (!v4_addr_match(rp->ipRouteDest,
41280Sstevel@tonic-gate 					    rp->ipRouteMask, fp))
41290Sstevel@tonic-gate 						continue; /* 'for' loop 2 */
41300Sstevel@tonic-gate 					break;
41310Sstevel@tonic-gate 				case FK_FLAGS:
41320Sstevel@tonic-gate 					if ((flag_b & fp->u.f.f_flagset) !=
41330Sstevel@tonic-gate 					    fp->u.f.f_flagset ||
41340Sstevel@tonic-gate 					    (flag_b & fp->u.f.f_flagclear))
41350Sstevel@tonic-gate 						continue; /* 'for' loop 2 */
41360Sstevel@tonic-gate 					break;
41370Sstevel@tonic-gate 				}
41380Sstevel@tonic-gate 				break;
41390Sstevel@tonic-gate 			} /* 'for' loop 2 ends */
41400Sstevel@tonic-gate 			if (fp == NULL)
41410Sstevel@tonic-gate 				return (B_FALSE);
41420Sstevel@tonic-gate 		}
41430Sstevel@tonic-gate 	/* 'for' loop 1 ends */
41440Sstevel@tonic-gate 	return (B_TRUE);
41450Sstevel@tonic-gate }
41460Sstevel@tonic-gate 
41470Sstevel@tonic-gate /*
41480Sstevel@tonic-gate  * Given an IPv4 MIB2 route entry, form the list of flags for the
41490Sstevel@tonic-gate  * route.
41500Sstevel@tonic-gate  */
41510Sstevel@tonic-gate static uint_t
41521676Sjpk form_v4_route_flags(const mib2_ipRouteEntry_t *rp, char *flags)
41530Sstevel@tonic-gate {
41540Sstevel@tonic-gate 	uint_t flag_b;
41550Sstevel@tonic-gate 
41560Sstevel@tonic-gate 	flag_b = FLF_U;
41570Sstevel@tonic-gate 	(void) strcpy(flags, "U");
41580Sstevel@tonic-gate 	if (rp->ipRouteInfo.re_ire_type == IRE_DEFAULT ||
41590Sstevel@tonic-gate 	    rp->ipRouteInfo.re_ire_type == IRE_PREFIX ||
41600Sstevel@tonic-gate 	    rp->ipRouteInfo.re_ire_type == IRE_HOST ||
41610Sstevel@tonic-gate 	    rp->ipRouteInfo.re_ire_type == IRE_HOST_REDIRECT) {
41620Sstevel@tonic-gate 		(void) strcat(flags, "G");
41630Sstevel@tonic-gate 		flag_b |= FLF_G;
41640Sstevel@tonic-gate 	}
41650Sstevel@tonic-gate 	if (rp->ipRouteMask == IP_HOST_MASK) {
41660Sstevel@tonic-gate 		(void) strcat(flags, "H");
41670Sstevel@tonic-gate 		flag_b |= FLF_H;
41680Sstevel@tonic-gate 	}
41690Sstevel@tonic-gate 	if (rp->ipRouteInfo.re_ire_type == IRE_HOST_REDIRECT) {
41700Sstevel@tonic-gate 		(void) strcat(flags, "D");
41710Sstevel@tonic-gate 		flag_b |= FLF_D;
41720Sstevel@tonic-gate 	}
41730Sstevel@tonic-gate 	if (rp->ipRouteInfo.re_ire_type == IRE_CACHE) {
41740Sstevel@tonic-gate 		/* Address resolution */
41750Sstevel@tonic-gate 		(void) strcat(flags, "A");
41760Sstevel@tonic-gate 		flag_b |= FLF_A;
41770Sstevel@tonic-gate 	}
41780Sstevel@tonic-gate 	if (rp->ipRouteInfo.re_ire_type == IRE_BROADCAST) {	/* Broadcast */
41790Sstevel@tonic-gate 		(void) strcat(flags, "B");
41800Sstevel@tonic-gate 		flag_b |= FLF_B;
41810Sstevel@tonic-gate 	}
41820Sstevel@tonic-gate 	if (rp->ipRouteInfo.re_ire_type == IRE_LOCAL) {		/* Local */
41830Sstevel@tonic-gate 		(void) strcat(flags, "L");
41840Sstevel@tonic-gate 		flag_b |= FLF_L;
41850Sstevel@tonic-gate 	}
41860Sstevel@tonic-gate 	if (rp->ipRouteInfo.re_flags & RTF_MULTIRT) {
41870Sstevel@tonic-gate 		(void) strcat(flags, "M");			/* Multiroute */
41880Sstevel@tonic-gate 		flag_b |= FLF_M;
41890Sstevel@tonic-gate 	}
41900Sstevel@tonic-gate 	if (rp->ipRouteInfo.re_flags & RTF_SETSRC) {
41910Sstevel@tonic-gate 		(void) strcat(flags, "S");			/* Setsrc */
41920Sstevel@tonic-gate 		flag_b |= FLF_S;
41930Sstevel@tonic-gate 	}
41940Sstevel@tonic-gate 	return (flag_b);
41950Sstevel@tonic-gate }
41960Sstevel@tonic-gate 
41971676Sjpk static const char ire_hdr_v4[] =
41981676Sjpk "\n%s Table: IPv4\n";
41991676Sjpk static const char ire_hdr_v4_compat[] =
42001676Sjpk "\n%s Table:\n";
42011676Sjpk static const char ire_hdr_v4_verbose[] =
42021676Sjpk "  Destination             Mask           Gateway          Device Mxfrg "
42031676Sjpk "Rtt   Ref Flg  Out  In/Fwd %s\n"
42041676Sjpk "-------------------- --------------- -------------------- ------ ----- "
42051676Sjpk "----- --- --- ----- ------ %s\n";
42061676Sjpk 
42071676Sjpk static const char ire_hdr_v4_normal[] =
42083129Sja97890 "  Destination           Gateway           Flags  Ref     Use     Interface"
42093129Sja97890 " %s\n-------------------- -------------------- ----- ----- ---------- "
42103129Sja97890 "--------- %s\n";
42111676Sjpk 
42120Sstevel@tonic-gate static boolean_t
42131676Sjpk ire_report_item_v4(const mib2_ipRouteEntry_t *rp, boolean_t first,
42141676Sjpk     const sec_attr_list_t *attrs)
42150Sstevel@tonic-gate {
42160Sstevel@tonic-gate 	char			dstbuf[MAXHOSTNAMELEN + 1];
42170Sstevel@tonic-gate 	char			maskbuf[MAXHOSTNAMELEN + 1];
42180Sstevel@tonic-gate 	char			gwbuf[MAXHOSTNAMELEN + 1];
42190Sstevel@tonic-gate 	char			ifname[LIFNAMSIZ + 1];
42200Sstevel@tonic-gate 	char			flags[10];	/* RTF_ flags */
42210Sstevel@tonic-gate 	uint_t			flag_b;
42220Sstevel@tonic-gate 
42230Sstevel@tonic-gate 	if (rp->ipRouteInfo.re_in_src_addr != 0 ||
42240Sstevel@tonic-gate 	    rp->ipRouteInfo.re_in_ill.o_length != 0 ||
42250Sstevel@tonic-gate 	    !(Aflag || (rp->ipRouteInfo.re_ire_type != IRE_CACHE &&
42260Sstevel@tonic-gate 	    rp->ipRouteInfo.re_ire_type != IRE_BROADCAST &&
42270Sstevel@tonic-gate 	    rp->ipRouteInfo.re_ire_type != IRE_LOCAL))) {
42280Sstevel@tonic-gate 		return (first);
42290Sstevel@tonic-gate 	}
42300Sstevel@tonic-gate 
42310Sstevel@tonic-gate 	flag_b = form_v4_route_flags(rp, flags);
42320Sstevel@tonic-gate 
42330Sstevel@tonic-gate 	if (!ire_filter_match_v4(rp, flag_b))
42340Sstevel@tonic-gate 		return (first);
42350Sstevel@tonic-gate 
42360Sstevel@tonic-gate 	if (first) {
42371676Sjpk 		(void) printf(v4compat ? ire_hdr_v4_compat : ire_hdr_v4,
42381676Sjpk 		    Vflag ? "IRE" : "Routing");
42391676Sjpk 		(void) printf(Vflag ? ire_hdr_v4_verbose : ire_hdr_v4_normal,
42401676Sjpk 		    RSECflag ? "  Gateway security attributes  " : "",
42411676Sjpk 		    RSECflag ? "-------------------------------" : "");
42420Sstevel@tonic-gate 		first = B_FALSE;
42430Sstevel@tonic-gate 	}
42440Sstevel@tonic-gate 
42450Sstevel@tonic-gate 	if (flag_b & FLF_H) {
42460Sstevel@tonic-gate 		(void) pr_addr(rp->ipRouteDest, dstbuf, sizeof (dstbuf));
42470Sstevel@tonic-gate 	} else {
42480Sstevel@tonic-gate 		(void) pr_net(rp->ipRouteDest, rp->ipRouteMask,
42490Sstevel@tonic-gate 		    dstbuf, sizeof (dstbuf));
42500Sstevel@tonic-gate 	}
42510Sstevel@tonic-gate 	if (Vflag) {
42520Sstevel@tonic-gate 		(void) printf("%-20s %-15s %-20s %-6s %5u%c %4u %3u "
42533129Sja97890 		    "%-4s%6u %6u %s\n",
42540Sstevel@tonic-gate 		    dstbuf,
42550Sstevel@tonic-gate 		    pr_mask(rp->ipRouteMask, maskbuf, sizeof (maskbuf)),
42560Sstevel@tonic-gate 		    pr_addrnz(rp->ipRouteNextHop, gwbuf, sizeof (gwbuf)),
42570Sstevel@tonic-gate 		    octetstr(&rp->ipRouteIfIndex, 'a', ifname, sizeof (ifname)),
42580Sstevel@tonic-gate 		    rp->ipRouteInfo.re_max_frag,
42590Sstevel@tonic-gate 		    rp->ipRouteInfo.re_frag_flag ? '*' : ' ',
42600Sstevel@tonic-gate 		    rp->ipRouteInfo.re_rtt,
42610Sstevel@tonic-gate 		    rp->ipRouteInfo.re_ref,
42620Sstevel@tonic-gate 		    flags,
42630Sstevel@tonic-gate 		    rp->ipRouteInfo.re_obpkt,
42641676Sjpk 		    rp->ipRouteInfo.re_ibpkt,
42651676Sjpk 		    pr_secattr(attrs));
42660Sstevel@tonic-gate 	} else {
42673129Sja97890 		(void) printf("%-20s %-20s %-5s  %4u %10u %-9s %s\n",
42680Sstevel@tonic-gate 		    dstbuf,
42690Sstevel@tonic-gate 		    pr_addrnz(rp->ipRouteNextHop, gwbuf, sizeof (gwbuf)),
42700Sstevel@tonic-gate 		    flags,
42710Sstevel@tonic-gate 		    rp->ipRouteInfo.re_ref,
42720Sstevel@tonic-gate 		    rp->ipRouteInfo.re_obpkt + rp->ipRouteInfo.re_ibpkt,
42730Sstevel@tonic-gate 		    octetstr(&rp->ipRouteIfIndex, 'a',
42741676Sjpk 		    ifname, sizeof (ifname)),
42751676Sjpk 		    pr_secattr(attrs));
42760Sstevel@tonic-gate 	}
42770Sstevel@tonic-gate 	return (first);
42780Sstevel@tonic-gate }
42790Sstevel@tonic-gate 
42801676Sjpk static const char ire_hdr_src_v4[] =
42811676Sjpk "\n%s Table: IPv4 Source-Specific\n";
42821676Sjpk static const char ire_hdr_src_v4_compat[] =
42831676Sjpk "\n%s Table: Source-Specific\n";
42841676Sjpk static const char ire_hdr_src_v4_verbose[] =
42851676Sjpk "  Destination        In If       Source            Gateway         "
42861676Sjpk "  Out If    Mxfrg  Rtt  Ref Flg  Out  In/Fwd %s\n"
42871676Sjpk "------------------ ----------- ----------------- ----------------- "
42881676Sjpk "----------- ----- ----- --- --- ----- ------ %s\n";
42891676Sjpk static const char ire_hdr_src_v4_normal[] =
42901676Sjpk "  Destination    In If     Source          Gateway       Flags  Use   "
42911676Sjpk " Out If  %s\n"
42921676Sjpk "--------------- -------- --------------- --------------- ----- ------ "
42931676Sjpk "-------- %s\n";
42941676Sjpk 
42950Sstevel@tonic-gate /*
42960Sstevel@tonic-gate  * Report a source-specific route.
42970Sstevel@tonic-gate  */
42980Sstevel@tonic-gate static boolean_t
42991676Sjpk ire_report_item_v4src(const mib2_ipRouteEntry_t *rp, boolean_t first,
43001676Sjpk     const sec_attr_list_t *attrs)
43010Sstevel@tonic-gate {
43020Sstevel@tonic-gate 	char	dstbuf[MAXHOSTNAMELEN + 1];
43030Sstevel@tonic-gate 	char	srcbuf[MAXHOSTNAMELEN + 1];
43040Sstevel@tonic-gate 	char	gwbuf[MAXHOSTNAMELEN + 1];
43050Sstevel@tonic-gate 	char	inif[LIFNAMSIZ + 1];
43060Sstevel@tonic-gate 	char	outif[LIFNAMSIZ + 1];
43070Sstevel@tonic-gate 	uint_t	flag_b;
43080Sstevel@tonic-gate 	char	flags[10];
43090Sstevel@tonic-gate 
43100Sstevel@tonic-gate 	/*
43110Sstevel@tonic-gate 	 * If this isn't a source specific route, or if it's filtered
43120Sstevel@tonic-gate 	 * out, then ignore it.
43130Sstevel@tonic-gate 	 */
43140Sstevel@tonic-gate 	if ((rp->ipRouteInfo.re_in_src_addr == 0 &&
43150Sstevel@tonic-gate 	    rp->ipRouteInfo.re_in_ill.o_length == 0) ||
43160Sstevel@tonic-gate 	    !(Aflag || (rp->ipRouteInfo.re_ire_type != IRE_CACHE &&
43170Sstevel@tonic-gate 	    rp->ipRouteInfo.re_ire_type != IRE_BROADCAST &&
43180Sstevel@tonic-gate 	    rp->ipRouteInfo.re_ire_type != IRE_LOCAL))) {
43190Sstevel@tonic-gate 		return (first);
43200Sstevel@tonic-gate 	}
43210Sstevel@tonic-gate 
43220Sstevel@tonic-gate 	flag_b = form_v4_route_flags(rp, flags);
43230Sstevel@tonic-gate 
43240Sstevel@tonic-gate 	if (!ire_filter_match_v4(rp, flag_b))
43250Sstevel@tonic-gate 		return (first);
43260Sstevel@tonic-gate 
43270Sstevel@tonic-gate 	if (first) {
43281676Sjpk 		(void) printf(v4compat ? ire_hdr_src_v4_compat :
43291676Sjpk 		    ire_hdr_src_v4, Vflag ? "IRE" : "Routing");
43301676Sjpk 		(void) printf(Vflag ? ire_hdr_src_v4_verbose :
43311676Sjpk 		    ire_hdr_src_v4_normal,
43321676Sjpk 		    RSECflag ? "  Gateway security attributes  " : "",
43331676Sjpk 		    RSECflag ? "-------------------------------" : "");
43340Sstevel@tonic-gate 		first = B_FALSE;
43350Sstevel@tonic-gate 	}
43360Sstevel@tonic-gate 
43370Sstevel@tonic-gate 	/*
43380Sstevel@tonic-gate 	 * This is special-cased here because the kernel doesn't actually
43390Sstevel@tonic-gate 	 * pay any attention to the destination address on mrtun entries.
43400Sstevel@tonic-gate 	 * Saying "default" would be misleading, though technically correct.
43410Sstevel@tonic-gate 	 */
43420Sstevel@tonic-gate 	if (rp->ipRouteInfo.re_in_src_addr != 0 && rp->ipRouteDest == 0 &&
43430Sstevel@tonic-gate 	    rp->ipRouteMask == 0)
43440Sstevel@tonic-gate 		(void) strlcpy(dstbuf, "    --", sizeof (dstbuf));
43450Sstevel@tonic-gate 	else
43460Sstevel@tonic-gate 		(void) pr_netclassless(rp->ipRouteDest, rp->ipRouteMask,
43470Sstevel@tonic-gate 		    dstbuf, sizeof (dstbuf));
43480Sstevel@tonic-gate 	(void) octetstr(&rp->ipRouteInfo.re_in_ill, 'a', inif, sizeof (inif));
43490Sstevel@tonic-gate 	(void) pr_addrnz(rp->ipRouteInfo.re_in_src_addr, srcbuf,
43500Sstevel@tonic-gate 	    sizeof (srcbuf));
43510Sstevel@tonic-gate 	(void) octetstr(&rp->ipRouteIfIndex, 'a', outif, sizeof (outif));
43520Sstevel@tonic-gate 	(void) pr_addrnz(rp->ipRouteNextHop, gwbuf, sizeof (gwbuf));
43530Sstevel@tonic-gate 	if (Vflag) {
43540Sstevel@tonic-gate 		(void) printf("%-18s %-11s %-17s %-17s %-11s %4u%c %5u %3u "
43551676Sjpk 		    "%-3s %5u %6u %s\n",
43560Sstevel@tonic-gate 		    dstbuf, inif, srcbuf, gwbuf,  outif,
43570Sstevel@tonic-gate 		    rp->ipRouteInfo.re_max_frag,
43580Sstevel@tonic-gate 		    rp->ipRouteInfo.re_frag_flag ? '*' : ' ',
43590Sstevel@tonic-gate 		    rp->ipRouteInfo.re_rtt, rp->ipRouteInfo.re_ref, flags,
43601676Sjpk 		    rp->ipRouteInfo.re_obpkt, rp->ipRouteInfo.re_ibpkt,
43611676Sjpk 		    pr_secattr(attrs));
43620Sstevel@tonic-gate 	} else {
43631676Sjpk 		(void) printf("%-15s %-8s %-15s %-15s %-5s %6u %-8s %s\n",
43641676Sjpk 		    dstbuf, inif, srcbuf, gwbuf, flags,
43651676Sjpk 		    rp->ipRouteInfo.re_obpkt + rp->ipRouteInfo.re_ibpkt, outif,
43661676Sjpk 		    pr_secattr(attrs));
43670Sstevel@tonic-gate 	}
43680Sstevel@tonic-gate 	return (first);
43690Sstevel@tonic-gate }
43700Sstevel@tonic-gate 
43710Sstevel@tonic-gate /*
43720Sstevel@tonic-gate  * Match a user-supplied IP address list against an IPv6 route entry.
43730Sstevel@tonic-gate  * If the user specified "any," then any non-zero address matches.  If
43740Sstevel@tonic-gate  * the user specified "none," then only the zero address matches.  If
43750Sstevel@tonic-gate  * the user specified a subnet mask length, then use that in matching
43760Sstevel@tonic-gate  * routes (select routes that are at least as specific).  If the user
43770Sstevel@tonic-gate  * specified only an address, then use the route's mask (select routes
43780Sstevel@tonic-gate  * that would match that address).  IPv4 addresses are ignored.
43790Sstevel@tonic-gate  */
43800Sstevel@tonic-gate static boolean_t
43810Sstevel@tonic-gate v6_addr_match(const Ip6Address *addr, int masklen, const filter_t *fp)
43820Sstevel@tonic-gate {
43830Sstevel@tonic-gate 	const uint8_t *ucp;
43840Sstevel@tonic-gate 	int fmasklen;
43850Sstevel@tonic-gate 	int i;
43860Sstevel@tonic-gate 	char **app;
43870Sstevel@tonic-gate 	char *aptr;
43880Sstevel@tonic-gate 
43890Sstevel@tonic-gate 	if (fp->u.a.f_address == NULL) {
43900Sstevel@tonic-gate 		if (IN6_IS_ADDR_UNSPECIFIED(&fp->u.a.f_mask))	/* any */
43910Sstevel@tonic-gate 			return (!IN6_IS_ADDR_UNSPECIFIED(addr));
43920Sstevel@tonic-gate 		return (IN6_IS_ADDR_UNSPECIFIED(addr));		/* "none" */
43930Sstevel@tonic-gate 	}
43940Sstevel@tonic-gate 	fmasklen = 0;
43950Sstevel@tonic-gate 	/* 'for' loop 1a: */
43960Sstevel@tonic-gate 	for (ucp = fp->u.a.f_mask.s6_addr;
43970Sstevel@tonic-gate 	    ucp < fp->u.a.f_mask.s6_addr + sizeof (fp->u.a.f_mask.s6_addr);
43980Sstevel@tonic-gate 	    ucp++) {
43990Sstevel@tonic-gate 		if (*ucp != 0xff) {
44000Sstevel@tonic-gate 			if (*ucp != 0)
44010Sstevel@tonic-gate 				fmasklen += 9 - ffs(*ucp);
44020Sstevel@tonic-gate 			break; /* 'for' loop 1a */
44030Sstevel@tonic-gate 		}
44040Sstevel@tonic-gate 		fmasklen += 8;
44050Sstevel@tonic-gate 	} /* 'for' loop 1a ends */
44060Sstevel@tonic-gate 	if (fmasklen != IPV6_ABITS) {
44070Sstevel@tonic-gate 		if (fmasklen > masklen)
44080Sstevel@tonic-gate 			return (B_FALSE);
44090Sstevel@tonic-gate 		masklen = fmasklen;
44100Sstevel@tonic-gate 	}
44110Sstevel@tonic-gate 	/* 'for' loop 1b: */
44120Sstevel@tonic-gate 	for (app = fp->u.a.f_address->h_addr_list; (aptr = *app) != NULL;
44130Sstevel@tonic-gate 	    app++) {
44140Sstevel@tonic-gate 		/* LINTED: (note 1) */
44150Sstevel@tonic-gate 		if (IN6_IS_ADDR_V4MAPPED((in6_addr_t *)aptr))
44160Sstevel@tonic-gate 			continue; /* 'for' loop 1b */
44170Sstevel@tonic-gate 		ucp = addr->s6_addr;
44180Sstevel@tonic-gate 		for (i = masklen; i >= 8; i -= 8)
44190Sstevel@tonic-gate 			if (*ucp++ != *aptr++)
44200Sstevel@tonic-gate 				break; /* 'for' loop 1b */
44210Sstevel@tonic-gate 		if (i == 0 ||
44220Sstevel@tonic-gate 		    (i < 8 && ((*ucp ^ *aptr) & ~(0xff >> i)) == 0))
44230Sstevel@tonic-gate 			return (B_TRUE);
44240Sstevel@tonic-gate 	} /* 'for' loop 1b ends */
44250Sstevel@tonic-gate 	return (B_FALSE);
44260Sstevel@tonic-gate }
44270Sstevel@tonic-gate 
44280Sstevel@tonic-gate /*
44290Sstevel@tonic-gate  * Run through the filter list for an IPv6 MIB2 IRE.  For a given
44300Sstevel@tonic-gate  * type, if there's at least one filter and all filters of that type
44310Sstevel@tonic-gate  * fail to match, then the route doesn't match and isn't displayed.
44320Sstevel@tonic-gate  * If at least one matches, or none are specified, for each of the
44330Sstevel@tonic-gate  * types, then the route is selected and displayed.
44340Sstevel@tonic-gate  */
44350Sstevel@tonic-gate static boolean_t
44361676Sjpk ire_filter_match_v6(const mib2_ipv6RouteEntry_t *rp6, uint_t flag_b)
44370Sstevel@tonic-gate {
44380Sstevel@tonic-gate 	filter_t *fp;
44390Sstevel@tonic-gate 	int idx;
44400Sstevel@tonic-gate 
44410Sstevel@tonic-gate 	/* 'for' loop 1: */
44420Sstevel@tonic-gate 	for (idx = 0; idx < NFILTERKEYS; idx++)
44430Sstevel@tonic-gate 		if ((fp = filters[idx]) != NULL) {
44440Sstevel@tonic-gate 			/* 'for' loop 2: */
44450Sstevel@tonic-gate 			for (; fp != NULL; fp = fp->f_next) {
44460Sstevel@tonic-gate 				switch (idx) {
44470Sstevel@tonic-gate 				case FK_AF:
44480Sstevel@tonic-gate 					if (fp->u.f_family != AF_INET6)
44490Sstevel@tonic-gate 						/* 'for' loop 2 */
44500Sstevel@tonic-gate 						continue;
44510Sstevel@tonic-gate 					break;
44520Sstevel@tonic-gate 				case FK_INIF:
44530Sstevel@tonic-gate 					if (fp->u.f_ifname != NULL)
44540Sstevel@tonic-gate 						/* 'for' loop 2 */
44550Sstevel@tonic-gate 						continue;
44560Sstevel@tonic-gate 					break;
44570Sstevel@tonic-gate 				case FK_OUTIF:
44580Sstevel@tonic-gate 					if (!dev_name_match(&rp6->
44590Sstevel@tonic-gate 					    ipv6RouteIfIndex, fp->u.f_ifname))
44600Sstevel@tonic-gate 						/* 'for' loop 2 */
44610Sstevel@tonic-gate 						continue;
44620Sstevel@tonic-gate 					break;
44630Sstevel@tonic-gate 				case FK_SRC:
44640Sstevel@tonic-gate 					if (!v6_addr_match(&rp6->ipv6RouteInfo.
44650Sstevel@tonic-gate 					    re_src_addr, IPV6_ABITS, fp))
44660Sstevel@tonic-gate 						/* 'for' loop 2 */
44670Sstevel@tonic-gate 						continue;
44680Sstevel@tonic-gate 					break;
44690Sstevel@tonic-gate 				case FK_DST:
44700Sstevel@tonic-gate 					if (!v6_addr_match(&rp6->ipv6RouteDest,
44710Sstevel@tonic-gate 					    rp6->ipv6RoutePfxLength, fp))
44720Sstevel@tonic-gate 						/* 'for' loop 2 */
44730Sstevel@tonic-gate 						continue;
44740Sstevel@tonic-gate 					break;
44750Sstevel@tonic-gate 				case FK_FLAGS:
44760Sstevel@tonic-gate 					if ((flag_b & fp->u.f.f_flagset) !=
44770Sstevel@tonic-gate 					    fp->u.f.f_flagset ||
44780Sstevel@tonic-gate 					    (flag_b & fp->u.f.f_flagclear))
44790Sstevel@tonic-gate 						/* 'for' loop 2 */
44800Sstevel@tonic-gate 						continue;
44810Sstevel@tonic-gate 					break;
44820Sstevel@tonic-gate 				}
44830Sstevel@tonic-gate 				break;
44840Sstevel@tonic-gate 			} /* 'for' loop 2 ends */
44850Sstevel@tonic-gate 			if (fp == NULL)
44860Sstevel@tonic-gate 				return (B_FALSE);
44870Sstevel@tonic-gate 		}
44880Sstevel@tonic-gate 	/* 'for' loop 1 ends */
44890Sstevel@tonic-gate 	return (B_TRUE);
44900Sstevel@tonic-gate }
44910Sstevel@tonic-gate 
44921676Sjpk static const char ire_hdr_v6[] =
44931676Sjpk "\n%s Table: IPv6\n";
44941676Sjpk static const char ire_hdr_v6_verbose[] =
44951676Sjpk "  Destination/Mask            Gateway                    If    PMTU   Rtt  "
44961676Sjpk "Ref Flags  Out   In/Fwd %s\n"
44971676Sjpk "--------------------------- --------------------------- ----- ------ ----- "
44981676Sjpk "--- ----- ------ ------ %s\n";
44991676Sjpk static const char ire_hdr_v6_normal[] =
45001676Sjpk "  Destination/Mask            Gateway                   Flags Ref   Use  "
45013129Sja97890 "  If   %s\n"
45023129Sja97890 "--------------------------- --------------------------- ----- --- ------- "
45031676Sjpk "----- %s\n";
45041676Sjpk 
45050Sstevel@tonic-gate static boolean_t
45061676Sjpk ire_report_item_v6(const mib2_ipv6RouteEntry_t *rp6, boolean_t first,
45071676Sjpk     const sec_attr_list_t *attrs)
45080Sstevel@tonic-gate {
45090Sstevel@tonic-gate 	char			dstbuf[MAXHOSTNAMELEN + 1];
45100Sstevel@tonic-gate 	char			gwbuf[MAXHOSTNAMELEN + 1];
45110Sstevel@tonic-gate 	char			ifname[LIFNAMSIZ + 1];
45120Sstevel@tonic-gate 	char			flags[10];	/* RTF_ flags */
45130Sstevel@tonic-gate 	uint_t			flag_b;
45140Sstevel@tonic-gate 
45150Sstevel@tonic-gate 	if (!(Aflag || (rp6->ipv6RouteInfo.re_ire_type != IRE_CACHE &&
45160Sstevel@tonic-gate 	    rp6->ipv6RouteInfo.re_ire_type != IRE_LOCAL))) {
45170Sstevel@tonic-gate 		return (first);
45180Sstevel@tonic-gate 	}
45190Sstevel@tonic-gate 
45200Sstevel@tonic-gate 	flag_b = FLF_U;
45210Sstevel@tonic-gate 	(void) strcpy(flags, "U");
45220Sstevel@tonic-gate 	if (rp6->ipv6RouteInfo.re_ire_type == IRE_DEFAULT ||
45230Sstevel@tonic-gate 	    rp6->ipv6RouteInfo.re_ire_type == IRE_PREFIX ||
45240Sstevel@tonic-gate 	    rp6->ipv6RouteInfo.re_ire_type == IRE_HOST ||
45250Sstevel@tonic-gate 	    rp6->ipv6RouteInfo.re_ire_type == IRE_HOST_REDIRECT) {
45260Sstevel@tonic-gate 		(void) strcat(flags, "G");
45270Sstevel@tonic-gate 		flag_b |= FLF_G;
45280Sstevel@tonic-gate 	}
45290Sstevel@tonic-gate 
45300Sstevel@tonic-gate 	if (rp6->ipv6RoutePfxLength == IPV6_ABITS) {
45310Sstevel@tonic-gate 		(void) strcat(flags, "H");
45320Sstevel@tonic-gate 		flag_b |= FLF_H;
45330Sstevel@tonic-gate 	}
45340Sstevel@tonic-gate 
45350Sstevel@tonic-gate 	if (rp6->ipv6RouteInfo.re_ire_type == IRE_HOST_REDIRECT) {
45360Sstevel@tonic-gate 		(void) strcat(flags, "D");
45370Sstevel@tonic-gate 		flag_b |= FLF_D;
45380Sstevel@tonic-gate 	}
45390Sstevel@tonic-gate 	if (rp6->ipv6RouteInfo.re_ire_type == IRE_CACHE) {
45400Sstevel@tonic-gate 		/* Address resolution */
45410Sstevel@tonic-gate 		(void) strcat(flags, "A");
45420Sstevel@tonic-gate 		flag_b |= FLF_A;
45430Sstevel@tonic-gate 	}
45440Sstevel@tonic-gate 	if (rp6->ipv6RouteInfo.re_ire_type == IRE_LOCAL) {	/* Local */
45450Sstevel@tonic-gate 		(void) strcat(flags, "L");
45460Sstevel@tonic-gate 		flag_b |= FLF_L;
45470Sstevel@tonic-gate 	}
45480Sstevel@tonic-gate 	if (rp6->ipv6RouteInfo.re_flags & RTF_MULTIRT) {
45490Sstevel@tonic-gate 		(void) strcat(flags, "M");			/* Multiroute */
45500Sstevel@tonic-gate 		flag_b |= FLF_M;
45510Sstevel@tonic-gate 	}
45520Sstevel@tonic-gate 	if (rp6->ipv6RouteInfo.re_flags & RTF_SETSRC) {
45530Sstevel@tonic-gate 		(void) strcat(flags, "S");			/* Setsrc */
45540Sstevel@tonic-gate 		flag_b |= FLF_S;
45550Sstevel@tonic-gate 	}
45560Sstevel@tonic-gate 
45570Sstevel@tonic-gate 	if (!ire_filter_match_v6(rp6, flag_b))
45580Sstevel@tonic-gate 		return (first);
45590Sstevel@tonic-gate 
45600Sstevel@tonic-gate 	if (first) {
45611676Sjpk 		(void) printf(ire_hdr_v6, Vflag ? "IRE" : "Routing");
45621676Sjpk 		(void) printf(Vflag ? ire_hdr_v6_verbose : ire_hdr_v6_normal,
45631676Sjpk 		    RSECflag ? "  Gateway security attributes  " : "",
45641676Sjpk 		    RSECflag ? "-------------------------------" : "");
45650Sstevel@tonic-gate 		first = B_FALSE;
45660Sstevel@tonic-gate 	}
45670Sstevel@tonic-gate 
45680Sstevel@tonic-gate 	if (Vflag) {
45690Sstevel@tonic-gate 		(void) printf("%-27s %-27s %-5s %5u%c %5u %3u "
45701676Sjpk 		    "%-5s %6u %6u %s\n",
45710Sstevel@tonic-gate 		    pr_prefix6(&rp6->ipv6RouteDest,
45720Sstevel@tonic-gate 			rp6->ipv6RoutePfxLength, dstbuf, sizeof (dstbuf)),
45730Sstevel@tonic-gate 		    IN6_IS_ADDR_UNSPECIFIED(&rp6->ipv6RouteNextHop) ?
45740Sstevel@tonic-gate 		    "    --" :
45750Sstevel@tonic-gate 		    pr_addr6(&rp6->ipv6RouteNextHop, gwbuf, sizeof (gwbuf)),
45760Sstevel@tonic-gate 		    octetstr(&rp6->ipv6RouteIfIndex, 'a',
45770Sstevel@tonic-gate 		    ifname, sizeof (ifname)),
45780Sstevel@tonic-gate 		    rp6->ipv6RouteInfo.re_max_frag,
45790Sstevel@tonic-gate 		    rp6->ipv6RouteInfo.re_frag_flag ? '*' : ' ',
45800Sstevel@tonic-gate 		    rp6->ipv6RouteInfo.re_rtt,
45810Sstevel@tonic-gate 		    rp6->ipv6RouteInfo.re_ref,
45820Sstevel@tonic-gate 		    flags,
45830Sstevel@tonic-gate 		    rp6->ipv6RouteInfo.re_obpkt,
45841676Sjpk 		    rp6->ipv6RouteInfo.re_ibpkt,
45851676Sjpk 		    pr_secattr(attrs));
45860Sstevel@tonic-gate 	} else {
45873129Sja97890 		(void) printf("%-27s %-27s %-5s %3u %7u %-5s %s\n",
45880Sstevel@tonic-gate 		    pr_prefix6(&rp6->ipv6RouteDest,
45890Sstevel@tonic-gate 			rp6->ipv6RoutePfxLength, dstbuf, sizeof (dstbuf)),
45900Sstevel@tonic-gate 		    IN6_IS_ADDR_UNSPECIFIED(&rp6->ipv6RouteNextHop) ?
45910Sstevel@tonic-gate 		    "    --" :
45920Sstevel@tonic-gate 		    pr_addr6(&rp6->ipv6RouteNextHop, gwbuf, sizeof (gwbuf)),
45930Sstevel@tonic-gate 		    flags,
45940Sstevel@tonic-gate 		    rp6->ipv6RouteInfo.re_ref,
45950Sstevel@tonic-gate 		    rp6->ipv6RouteInfo.re_obpkt + rp6->ipv6RouteInfo.re_ibpkt,
45960Sstevel@tonic-gate 		    octetstr(&rp6->ipv6RouteIfIndex, 'a',
45971676Sjpk 		    ifname, sizeof (ifname)),
45981676Sjpk 		    pr_secattr(attrs));
45990Sstevel@tonic-gate 	}
46000Sstevel@tonic-gate 	return (first);
46010Sstevel@tonic-gate }
46020Sstevel@tonic-gate 
46031676Sjpk /*
46041676Sjpk  * Common attribute-gathering routine for all transports.
46051676Sjpk  */
46061676Sjpk static mib2_transportMLPEntry_t **
46071676Sjpk gather_attrs(const mib_item_t *item, int group, int mib_id, int esize)
46081676Sjpk {
46091676Sjpk 	int transport_count = 0;
46101676Sjpk 	const mib_item_t *iptr;
46111676Sjpk 	mib2_transportMLPEntry_t **attrs, *tme;
46121676Sjpk 
46131676Sjpk 	for (iptr = item; iptr != NULL; iptr = iptr->next_item) {
46141676Sjpk 		if (iptr->group == group && iptr->mib_id == mib_id)
46151676Sjpk 			transport_count += iptr->length / esize;
46161676Sjpk 	}
46171676Sjpk 	if (transport_count <= 0)
46181676Sjpk 		return (NULL);
46191676Sjpk 	attrs = calloc(transport_count, sizeof (*attrs));
46201676Sjpk 	if (attrs == NULL) {
46211676Sjpk 		perror("gather_attrs calloc failed");
46221676Sjpk 		return (NULL);
46231676Sjpk 	}
46241676Sjpk 	for (iptr = item; iptr != NULL; iptr = iptr->next_item) {
46251676Sjpk 		if (iptr->group == group && iptr->mib_id == EXPER_XPORT_MLP) {
46261676Sjpk 			for (tme = iptr->valp;
46271676Sjpk 			    (char *)tme < (char *)iptr->valp + iptr->length;
46281676Sjpk 			    /* LINTED: (note 1) */
46291676Sjpk 			    tme = (mib2_transportMLPEntry_t *)((char *)tme +
46301676Sjpk 			    transportMLPSize)) {
46311676Sjpk 				attrs[tme->tme_connidx] = tme;
46321676Sjpk 			}
46331676Sjpk 		}
46341676Sjpk 	}
46351676Sjpk 	return (attrs);
46361676Sjpk }
46371676Sjpk 
46381676Sjpk static void
46391676Sjpk print_transport_label(const mib2_transportMLPEntry_t *attr)
46401676Sjpk {
46411676Sjpk 	if (!RSECflag || attr == NULL)
46421676Sjpk 		return;
46431676Sjpk 
46441676Sjpk 	if (bisinvalid(&attr->tme_label))
46451676Sjpk 		(void) printf("   INVALID\n");
46461676Sjpk 	else
46471676Sjpk 		(void) printf("   %s\n", sl_to_str(&attr->tme_label));
46481676Sjpk }
46491676Sjpk 
46500Sstevel@tonic-gate /* ------------------------------ TCP_REPORT------------------------------- */
46510Sstevel@tonic-gate 
46520Sstevel@tonic-gate static const char tcp_hdr_v4[] =
46530Sstevel@tonic-gate "\nTCP: IPv4\n";
46540Sstevel@tonic-gate static const char tcp_hdr_v4_compat[] =
46550Sstevel@tonic-gate "\nTCP\n";
46560Sstevel@tonic-gate static const char tcp_hdr_v4_verbose[] =
46570Sstevel@tonic-gate "Local/Remote Address Swind  Snext     Suna   Rwind  Rnext     Rack   "
46581676Sjpk " Rto   Mss     State\n"
46590Sstevel@tonic-gate "-------------------- ----- -------- -------- ----- -------- -------- "
46601676Sjpk "----- ----- -----------\n";
46610Sstevel@tonic-gate static const char tcp_hdr_v4_normal[] =
46621676Sjpk "   Local Address        Remote Address    Swind Send-Q Rwind Recv-Q "
46631676Sjpk "   State\n"
46641676Sjpk "-------------------- -------------------- ----- ------ ----- ------ "
46651676Sjpk "-----------\n";
46660Sstevel@tonic-gate 
46670Sstevel@tonic-gate static const char tcp_hdr_v6[] =
46680Sstevel@tonic-gate "\nTCP: IPv6\n";
46690Sstevel@tonic-gate static const char tcp_hdr_v6_verbose[] =
46700Sstevel@tonic-gate "Local/Remote Address              Swind  Snext     Suna   Rwind  Rnext   "
46711676Sjpk "  Rack    Rto   Mss    State      If\n"
46720Sstevel@tonic-gate "--------------------------------- ----- -------- -------- ----- -------- "
46730Sstevel@tonic-gate "-------- ----- ----- ----------- -----\n";
46740Sstevel@tonic-gate static const char tcp_hdr_v6_normal[] =
46750Sstevel@tonic-gate "   Local Address                     Remote Address                 "
46761676Sjpk "Swind Send-Q Rwind Recv-Q   State      If\n"
46770Sstevel@tonic-gate "--------------------------------- --------------------------------- "
46780Sstevel@tonic-gate "----- ------ ----- ------ ----------- -----\n";
46790Sstevel@tonic-gate 
46801676Sjpk static boolean_t tcp_report_item_v4(const mib2_tcpConnEntry_t *,
46811676Sjpk     boolean_t first, const mib2_transportMLPEntry_t *);
46821676Sjpk static boolean_t tcp_report_item_v6(const mib2_tcp6ConnEntry_t *,
46831676Sjpk     boolean_t first, const mib2_transportMLPEntry_t *);
46840Sstevel@tonic-gate 
46850Sstevel@tonic-gate static void
46861676Sjpk tcp_report(const mib_item_t *item)
46870Sstevel@tonic-gate {
46880Sstevel@tonic-gate 	int			jtemp = 0;
46890Sstevel@tonic-gate 	boolean_t		print_hdr_once_v4 = B_TRUE;
46900Sstevel@tonic-gate 	boolean_t		print_hdr_once_v6 = B_TRUE;
46910Sstevel@tonic-gate 	mib2_tcpConnEntry_t	*tp;
46920Sstevel@tonic-gate 	mib2_tcp6ConnEntry_t	*tp6;
46931676Sjpk 	mib2_transportMLPEntry_t **v4_attrs, **v6_attrs;
46941676Sjpk 	mib2_transportMLPEntry_t **v4a, **v6a;
46951676Sjpk 	mib2_transportMLPEntry_t *aptr;
46960Sstevel@tonic-gate 
46970Sstevel@tonic-gate 	if (!protocol_selected(IPPROTO_TCP))
46980Sstevel@tonic-gate 		return;
46990Sstevel@tonic-gate 
47001676Sjpk 	/*
47011676Sjpk 	 * Preparation pass: the kernel returns separate entries for TCP
47021676Sjpk 	 * connection table entries and Multilevel Port attributes.  We loop
47031676Sjpk 	 * through the attributes first and set up an array for each address
47041676Sjpk 	 * family.
47051676Sjpk 	 */
47061676Sjpk 	v4_attrs = family_selected(AF_INET) && RSECflag ?
47071676Sjpk 	    gather_attrs(item, MIB2_TCP, MIB2_TCP_CONN, tcpConnEntrySize) :
47081676Sjpk 	    NULL;
47091676Sjpk 	v6_attrs = family_selected(AF_INET6) && RSECflag ?
47101676Sjpk 	    gather_attrs(item, MIB2_TCP6, MIB2_TCP6_CONN, tcp6ConnEntrySize) :
47111676Sjpk 	    NULL;
47121676Sjpk 
47130Sstevel@tonic-gate 	/* 'for' loop 1: */
47141676Sjpk 	v4a = v4_attrs;
47151676Sjpk 	v6a = v6_attrs;
47161676Sjpk 	for (; item != NULL; item = item->next_item) {
47170Sstevel@tonic-gate 		if (Dflag) {
47180Sstevel@tonic-gate 			(void) printf("\n--- Entry %d ---\n", ++jtemp);
47190Sstevel@tonic-gate 			(void) printf("Group = %d, mib_id = %d, "
47200Sstevel@tonic-gate 			    "length = %d, valp = 0x%p\n",
47210Sstevel@tonic-gate 			    item->group, item->mib_id,
47220Sstevel@tonic-gate 			    item->length, item->valp);
47230Sstevel@tonic-gate 		}
47240Sstevel@tonic-gate 
47250Sstevel@tonic-gate 		if (!((item->group == MIB2_TCP &&
47260Sstevel@tonic-gate 		    item->mib_id == MIB2_TCP_CONN) ||
47270Sstevel@tonic-gate 		    (item->group == MIB2_TCP6 &&
47280Sstevel@tonic-gate 		    item->mib_id == MIB2_TCP6_CONN)))
47290Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
47300Sstevel@tonic-gate 
47310Sstevel@tonic-gate 		if (item->group == MIB2_TCP && !family_selected(AF_INET))
47320Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
47330Sstevel@tonic-gate 		else if (item->group == MIB2_TCP6 && !family_selected(AF_INET6))
47340Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
47350Sstevel@tonic-gate 
47360Sstevel@tonic-gate 		if (item->group == MIB2_TCP) {
47370Sstevel@tonic-gate 			for (tp = (mib2_tcpConnEntry_t *)item->valp;
47380Sstevel@tonic-gate 			    (char *)tp < (char *)item->valp + item->length;
47390Sstevel@tonic-gate 			    /* LINTED: (note 1) */
47400Sstevel@tonic-gate 			    tp = (mib2_tcpConnEntry_t *)((char *)tp +
47410Sstevel@tonic-gate 			    tcpConnEntrySize)) {
47421676Sjpk 				aptr = v4a == NULL ? NULL : *v4a++;
47430Sstevel@tonic-gate 				print_hdr_once_v4 = tcp_report_item_v4(tp,
47441676Sjpk 				    print_hdr_once_v4, aptr);
47450Sstevel@tonic-gate 			}
47460Sstevel@tonic-gate 		} else {
47470Sstevel@tonic-gate 			for (tp6 = (mib2_tcp6ConnEntry_t *)item->valp;
47480Sstevel@tonic-gate 			    (char *)tp6 < (char *)item->valp + item->length;
47490Sstevel@tonic-gate 			    /* LINTED: (note 1) */
47500Sstevel@tonic-gate 			    tp6 = (mib2_tcp6ConnEntry_t *)((char *)tp6 +
47510Sstevel@tonic-gate 			    tcp6ConnEntrySize)) {
47521676Sjpk 				aptr = v6a == NULL ? NULL : *v6a++;
47530Sstevel@tonic-gate 				print_hdr_once_v6 = tcp_report_item_v6(tp6,
47541676Sjpk 				    print_hdr_once_v6, aptr);
47550Sstevel@tonic-gate 			}
47560Sstevel@tonic-gate 		}
47570Sstevel@tonic-gate 	} /* 'for' loop 1 ends */
47580Sstevel@tonic-gate 	(void) fflush(stdout);
47591676Sjpk 
47601676Sjpk 	if (v4_attrs != NULL)
47611676Sjpk 		free(v4_attrs);
47621676Sjpk 	if (v6_attrs != NULL)
47631676Sjpk 		free(v6_attrs);
47640Sstevel@tonic-gate }
47650Sstevel@tonic-gate 
47660Sstevel@tonic-gate static boolean_t
47671676Sjpk tcp_report_item_v4(const mib2_tcpConnEntry_t *tp, boolean_t first,
47681676Sjpk     const mib2_transportMLPEntry_t *attr)
47690Sstevel@tonic-gate {
47700Sstevel@tonic-gate 	/*
47710Sstevel@tonic-gate 	 * lname and fname below are for the hostname as well as the portname
47720Sstevel@tonic-gate 	 * There is no limit on portname length so we assume MAXHOSTNAMELEN
47730Sstevel@tonic-gate 	 * as the limit
47740Sstevel@tonic-gate 	 */
47750Sstevel@tonic-gate 	char	lname[MAXHOSTNAMELEN + MAXHOSTNAMELEN + 1];
47760Sstevel@tonic-gate 	char	fname[MAXHOSTNAMELEN + MAXHOSTNAMELEN + 1];
47770Sstevel@tonic-gate 
47780Sstevel@tonic-gate 	if (!(Aflag || tp->tcpConnEntryInfo.ce_state >= TCPS_ESTABLISHED))
47790Sstevel@tonic-gate 		return (first); /* Nothing to print */
47800Sstevel@tonic-gate 
47810Sstevel@tonic-gate 	if (first) {
47821676Sjpk 		(void) printf(v4compat ? tcp_hdr_v4_compat : tcp_hdr_v4);
47831676Sjpk 		(void) printf(Vflag ? tcp_hdr_v4_verbose : tcp_hdr_v4_normal);
47840Sstevel@tonic-gate 	}
47850Sstevel@tonic-gate 
47860Sstevel@tonic-gate 	if (Vflag) {
47870Sstevel@tonic-gate 		(void) printf("%-20s\n%-20s %5u %08x %08x %5u %08x %08x "
47880Sstevel@tonic-gate 		    "%5u %5u %s\n",
47890Sstevel@tonic-gate 		    pr_ap(tp->tcpConnLocalAddress,
47900Sstevel@tonic-gate 			tp->tcpConnLocalPort, "tcp", lname, sizeof (lname)),
47910Sstevel@tonic-gate 		    pr_ap(tp->tcpConnRemAddress,
47920Sstevel@tonic-gate 			tp->tcpConnRemPort, "tcp", fname, sizeof (fname)),
47930Sstevel@tonic-gate 		    tp->tcpConnEntryInfo.ce_swnd,
47940Sstevel@tonic-gate 		    tp->tcpConnEntryInfo.ce_snxt,
47950Sstevel@tonic-gate 		    tp->tcpConnEntryInfo.ce_suna,
47960Sstevel@tonic-gate 		    tp->tcpConnEntryInfo.ce_rwnd,
47970Sstevel@tonic-gate 		    tp->tcpConnEntryInfo.ce_rnxt,
47980Sstevel@tonic-gate 		    tp->tcpConnEntryInfo.ce_rack,
47990Sstevel@tonic-gate 		    tp->tcpConnEntryInfo.ce_rto,
48000Sstevel@tonic-gate 		    tp->tcpConnEntryInfo.ce_mss,
48011676Sjpk 		    mitcp_state(tp->tcpConnEntryInfo.ce_state, attr));
48020Sstevel@tonic-gate 	} else {
48030Sstevel@tonic-gate 		int sq = (int)tp->tcpConnEntryInfo.ce_snxt -
48040Sstevel@tonic-gate 		    (int)tp->tcpConnEntryInfo.ce_suna - 1;
48050Sstevel@tonic-gate 		int rq = (int)tp->tcpConnEntryInfo.ce_rnxt -
48060Sstevel@tonic-gate 		    (int)tp->tcpConnEntryInfo.ce_rack;
48070Sstevel@tonic-gate 
48080Sstevel@tonic-gate 		(void) printf("%-20s %-20s %5u %6d %5u %6d %s\n",
48090Sstevel@tonic-gate 		    pr_ap(tp->tcpConnLocalAddress,
48100Sstevel@tonic-gate 			tp->tcpConnLocalPort, "tcp", lname, sizeof (lname)),
48110Sstevel@tonic-gate 		    pr_ap(tp->tcpConnRemAddress,
48120Sstevel@tonic-gate 			tp->tcpConnRemPort, "tcp", fname, sizeof (fname)),
48130Sstevel@tonic-gate 		    tp->tcpConnEntryInfo.ce_swnd,
48140Sstevel@tonic-gate 		    (sq >= 0) ? sq : 0,
48150Sstevel@tonic-gate 		    tp->tcpConnEntryInfo.ce_rwnd,
48160Sstevel@tonic-gate 		    (rq >= 0) ? rq : 0,
48171676Sjpk 		    mitcp_state(tp->tcpConnEntryInfo.ce_state, attr));
48180Sstevel@tonic-gate 	}
48191676Sjpk 
48201676Sjpk 	print_transport_label(attr);
48211676Sjpk 
48221676Sjpk 	return (B_FALSE);
48230Sstevel@tonic-gate }
48240Sstevel@tonic-gate 
48250Sstevel@tonic-gate static boolean_t
48261676Sjpk tcp_report_item_v6(const mib2_tcp6ConnEntry_t *tp6, boolean_t first,
48271676Sjpk     const mib2_transportMLPEntry_t *attr)
48280Sstevel@tonic-gate {
48290Sstevel@tonic-gate 	/*
48300Sstevel@tonic-gate 	 * lname and fname below are for the hostname as well as the portname
48310Sstevel@tonic-gate 	 * There is no limit on portname length so we assume MAXHOSTNAMELEN
48320Sstevel@tonic-gate 	 * as the limit
48330Sstevel@tonic-gate 	 */
48340Sstevel@tonic-gate 	char	lname[MAXHOSTNAMELEN + MAXHOSTNAMELEN + 1];
48350Sstevel@tonic-gate 	char	fname[MAXHOSTNAMELEN + MAXHOSTNAMELEN + 1];
48360Sstevel@tonic-gate 	char	ifname[LIFNAMSIZ + 1];
48370Sstevel@tonic-gate 	char	*ifnamep;
48380Sstevel@tonic-gate 
48390Sstevel@tonic-gate 	if (!(Aflag || tp6->tcp6ConnEntryInfo.ce_state >= TCPS_ESTABLISHED))
48400Sstevel@tonic-gate 		return (first); /* Nothing to print */
48410Sstevel@tonic-gate 
48420Sstevel@tonic-gate 	if (first) {
48431676Sjpk 		(void) printf(tcp_hdr_v6);
48441676Sjpk 		(void) printf(Vflag ? tcp_hdr_v6_verbose : tcp_hdr_v6_normal);
48450Sstevel@tonic-gate 	}
48460Sstevel@tonic-gate 
48470Sstevel@tonic-gate 	ifnamep = (tp6->tcp6ConnIfIndex != 0) ?
48480Sstevel@tonic-gate 	    if_indextoname(tp6->tcp6ConnIfIndex, ifname) : NULL;
48491676Sjpk 	if (ifnamep == NULL)
48501676Sjpk 		ifnamep = "";
48510Sstevel@tonic-gate 
48520Sstevel@tonic-gate 	if (Vflag) {
48530Sstevel@tonic-gate 		(void) printf("%-33s\n%-33s %5u %08x %08x %5u %08x %08x "
48541676Sjpk 		    "%5u %5u %-11s %s\n",
48550Sstevel@tonic-gate 		    pr_ap6(&tp6->tcp6ConnLocalAddress,
48560Sstevel@tonic-gate 			tp6->tcp6ConnLocalPort, "tcp", lname, sizeof (lname)),
48570Sstevel@tonic-gate 		    pr_ap6(&tp6->tcp6ConnRemAddress,
48580Sstevel@tonic-gate 			tp6->tcp6ConnRemPort, "tcp", fname, sizeof (fname)),
48590Sstevel@tonic-gate 		    tp6->tcp6ConnEntryInfo.ce_swnd,
48600Sstevel@tonic-gate 		    tp6->tcp6ConnEntryInfo.ce_snxt,
48610Sstevel@tonic-gate 		    tp6->tcp6ConnEntryInfo.ce_suna,
48620Sstevel@tonic-gate 		    tp6->tcp6ConnEntryInfo.ce_rwnd,
48630Sstevel@tonic-gate 		    tp6->tcp6ConnEntryInfo.ce_rnxt,
48640Sstevel@tonic-gate 		    tp6->tcp6ConnEntryInfo.ce_rack,
48650Sstevel@tonic-gate 		    tp6->tcp6ConnEntryInfo.ce_rto,
48660Sstevel@tonic-gate 		    tp6->tcp6ConnEntryInfo.ce_mss,
48671676Sjpk 		    mitcp_state(tp6->tcp6ConnEntryInfo.ce_state, attr),
48681676Sjpk 		    ifnamep);
48690Sstevel@tonic-gate 	} else {
48700Sstevel@tonic-gate 		int sq = (int)tp6->tcp6ConnEntryInfo.ce_snxt -
48710Sstevel@tonic-gate 		    (int)tp6->tcp6ConnEntryInfo.ce_suna - 1;
48720Sstevel@tonic-gate 		int rq = (int)tp6->tcp6ConnEntryInfo.ce_rnxt -
48730Sstevel@tonic-gate 		    (int)tp6->tcp6ConnEntryInfo.ce_rack;
48740Sstevel@tonic-gate 
48751676Sjpk 		(void) printf("%-33s %-33s %5u %6d %5u %6d %-11s %s\n",
48760Sstevel@tonic-gate 		    pr_ap6(&tp6->tcp6ConnLocalAddress,
48770Sstevel@tonic-gate 			tp6->tcp6ConnLocalPort, "tcp", lname, sizeof (lname)),
48780Sstevel@tonic-gate 		    pr_ap6(&tp6->tcp6ConnRemAddress,
48790Sstevel@tonic-gate 			tp6->tcp6ConnRemPort, "tcp", fname, sizeof (fname)),
48800Sstevel@tonic-gate 		    tp6->tcp6ConnEntryInfo.ce_swnd,
48810Sstevel@tonic-gate 		    (sq >= 0) ? sq : 0,
48820Sstevel@tonic-gate 		    tp6->tcp6ConnEntryInfo.ce_rwnd,
48830Sstevel@tonic-gate 		    (rq >= 0) ? rq : 0,
48841676Sjpk 		    mitcp_state(tp6->tcp6ConnEntryInfo.ce_state, attr),
48851676Sjpk 		    ifnamep);
48860Sstevel@tonic-gate 	}
48871676Sjpk 
48881676Sjpk 	print_transport_label(attr);
48891676Sjpk 
48901676Sjpk 	return (B_FALSE);
48910Sstevel@tonic-gate }
48920Sstevel@tonic-gate 
48930Sstevel@tonic-gate /* ------------------------------- UDP_REPORT------------------------------- */
48940Sstevel@tonic-gate 
48951676Sjpk static boolean_t udp_report_item_v4(const mib2_udpEntry_t *ude,
48961676Sjpk     boolean_t first, const mib2_transportMLPEntry_t *attr);
48971676Sjpk static boolean_t udp_report_item_v6(const mib2_udp6Entry_t *ude6,
48981676Sjpk     boolean_t first, const mib2_transportMLPEntry_t *attr);
48991676Sjpk 
49001676Sjpk static const char udp_hdr_v4[] =
49011676Sjpk "   Local Address        Remote Address      State\n"
49021676Sjpk "-------------------- -------------------- ----------\n";
49031676Sjpk 
49041676Sjpk static const char udp_hdr_v6[] =
49050Sstevel@tonic-gate "   Local Address                     Remote Address                 "
49061676Sjpk "  State      If\n"
49070Sstevel@tonic-gate "--------------------------------- --------------------------------- "
49080Sstevel@tonic-gate "---------- -----\n";
49090Sstevel@tonic-gate 
49100Sstevel@tonic-gate static void
49111676Sjpk udp_report(const mib_item_t *item)
49120Sstevel@tonic-gate {
49130Sstevel@tonic-gate 	int			jtemp = 0;
49140Sstevel@tonic-gate 	boolean_t		print_hdr_once_v4 = B_TRUE;
49150Sstevel@tonic-gate 	boolean_t		print_hdr_once_v6 = B_TRUE;
49160Sstevel@tonic-gate 	mib2_udpEntry_t		*ude;
49170Sstevel@tonic-gate 	mib2_udp6Entry_t	*ude6;
49181676Sjpk 	mib2_transportMLPEntry_t **v4_attrs, **v6_attrs;
49191676Sjpk 	mib2_transportMLPEntry_t **v4a, **v6a;
49201676Sjpk 	mib2_transportMLPEntry_t *aptr;
49210Sstevel@tonic-gate 
49220Sstevel@tonic-gate 	if (!protocol_selected(IPPROTO_UDP))
49230Sstevel@tonic-gate 		return;
49240Sstevel@tonic-gate 
49251676Sjpk 	/*
49261676Sjpk 	 * Preparation pass: the kernel returns separate entries for UDP
49271676Sjpk 	 * connection table entries and Multilevel Port attributes.  We loop
49281676Sjpk 	 * through the attributes first and set up an array for each address
49291676Sjpk 	 * family.
49301676Sjpk 	 */
49311676Sjpk 	v4_attrs = family_selected(AF_INET) && RSECflag ?
49321676Sjpk 	    gather_attrs(item, MIB2_UDP, MIB2_UDP_ENTRY, udpEntrySize) : NULL;
49331676Sjpk 	v6_attrs = family_selected(AF_INET6) && RSECflag ?
49341676Sjpk 	    gather_attrs(item, MIB2_UDP6, MIB2_UDP6_ENTRY, udp6EntrySize) :
49351676Sjpk 	    NULL;
49361676Sjpk 
49371676Sjpk 	v4a = v4_attrs;
49381676Sjpk 	v6a = v6_attrs;
49390Sstevel@tonic-gate 	/* 'for' loop 1: */
49400Sstevel@tonic-gate 	for (; item; item = item->next_item) {
49410Sstevel@tonic-gate 		if (Dflag) {
49420Sstevel@tonic-gate 			(void) printf("\n--- Entry %d ---\n", ++jtemp);
49430Sstevel@tonic-gate 			(void) printf("Group = %d, mib_id = %d, "
49440Sstevel@tonic-gate 			    "length = %d, valp = 0x%p\n",
49450Sstevel@tonic-gate 			    item->group, item->mib_id,
49460Sstevel@tonic-gate 			    item->length, item->valp);
49470Sstevel@tonic-gate 		}
49480Sstevel@tonic-gate 		if (!((item->group == MIB2_UDP &&
49490Sstevel@tonic-gate 		    item->mib_id == MIB2_UDP_ENTRY) ||
49500Sstevel@tonic-gate 		    (item->group == MIB2_UDP6 &&
49510Sstevel@tonic-gate 		    item->mib_id == MIB2_UDP6_ENTRY)))
49520Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
49530Sstevel@tonic-gate 
49540Sstevel@tonic-gate 		if (item->group == MIB2_UDP && !family_selected(AF_INET))
49550Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
49560Sstevel@tonic-gate 		else if (item->group == MIB2_UDP6 && !family_selected(AF_INET6))
49570Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
49580Sstevel@tonic-gate 
49590Sstevel@tonic-gate 		/*	xxx.xxx.xxx.xxx,pppp  sss... */
49600Sstevel@tonic-gate 		if (item->group == MIB2_UDP) {
49610Sstevel@tonic-gate 			for (ude = (mib2_udpEntry_t *)item->valp;
49620Sstevel@tonic-gate 			    (char *)ude < (char *)item->valp + item->length;
49630Sstevel@tonic-gate 			    /* LINTED: (note 1) */
49640Sstevel@tonic-gate 			    ude = (mib2_udpEntry_t *)((char *)ude +
49650Sstevel@tonic-gate 			    udpEntrySize)) {
49661676Sjpk 				aptr = v4a == NULL ? NULL : *v4a++;
49670Sstevel@tonic-gate 				print_hdr_once_v4 = udp_report_item_v4(ude,
49681676Sjpk 				    print_hdr_once_v4, aptr);
49690Sstevel@tonic-gate 			}
49700Sstevel@tonic-gate 		} else {
49710Sstevel@tonic-gate 			for (ude6 = (mib2_udp6Entry_t *)item->valp;
49720Sstevel@tonic-gate 			    (char *)ude6 < (char *)item->valp + item->length;
49730Sstevel@tonic-gate 			    /* LINTED: (note 1) */
49740Sstevel@tonic-gate 			    ude6 = (mib2_udp6Entry_t *)((char *)ude6 +
49750Sstevel@tonic-gate 			    udp6EntrySize)) {
49761676Sjpk 				aptr = v6a == NULL ? NULL : *v6a++;
49770Sstevel@tonic-gate 				print_hdr_once_v6 = udp_report_item_v6(ude6,
49781676Sjpk 				    print_hdr_once_v6, aptr);
49790Sstevel@tonic-gate 			}
49800Sstevel@tonic-gate 		}
49810Sstevel@tonic-gate 	} /* 'for' loop 1 ends */
49820Sstevel@tonic-gate 	(void) fflush(stdout);
49831676Sjpk 
49841676Sjpk 	if (v4_attrs != NULL)
49851676Sjpk 		free(v4_attrs);
49861676Sjpk 	if (v6_attrs != NULL)
49871676Sjpk 		free(v6_attrs);
49880Sstevel@tonic-gate }
49890Sstevel@tonic-gate 
49900Sstevel@tonic-gate static boolean_t
49911676Sjpk udp_report_item_v4(const mib2_udpEntry_t *ude, boolean_t first,
49921676Sjpk     const mib2_transportMLPEntry_t *attr)
49930Sstevel@tonic-gate {
49940Sstevel@tonic-gate 	char	lname[MAXHOSTNAMELEN + MAXHOSTNAMELEN + 1];
49950Sstevel@tonic-gate 			/* hostname + portname */
49960Sstevel@tonic-gate 
49970Sstevel@tonic-gate 	if (!(Aflag || ude->udpEntryInfo.ue_state >= MIB2_UDP_connected))
49980Sstevel@tonic-gate 		return (first); /* Nothing to print */
49990Sstevel@tonic-gate 
50000Sstevel@tonic-gate 	if (first) {
50011676Sjpk 		(void) printf(v4compat ? "\nUDP\n" : "\nUDP: IPv4\n");
50021676Sjpk 		(void) printf(udp_hdr_v4);
50030Sstevel@tonic-gate 		first = B_FALSE;
50040Sstevel@tonic-gate 	}
50050Sstevel@tonic-gate 
50060Sstevel@tonic-gate 	(void) printf("%-20s ",
50070Sstevel@tonic-gate 	    pr_ap(ude->udpLocalAddress, ude->udpLocalPort, "udp",
50080Sstevel@tonic-gate 	    lname, sizeof (lname)));
50091676Sjpk 	(void) printf("%-20s %s\n",
50101676Sjpk 	    ude->udpEntryInfo.ue_state == MIB2_UDP_connected ?
50111676Sjpk 	    pr_ap(ude->udpEntryInfo.ue_RemoteAddress,
50121676Sjpk 	    ude->udpEntryInfo.ue_RemotePort, "udp", lname, sizeof (lname)) :
50131676Sjpk 	    "",
50141676Sjpk 	    miudp_state(ude->udpEntryInfo.ue_state, attr));
50151676Sjpk 
50161676Sjpk 	/*
50171676Sjpk 	 * UDP sockets don't have remote attributes, so there's no need to
50181676Sjpk 	 * print them here.
50191676Sjpk 	 */
50201676Sjpk 
50210Sstevel@tonic-gate 	return (first);
50220Sstevel@tonic-gate }
50230Sstevel@tonic-gate 
50240Sstevel@tonic-gate static boolean_t
50251676Sjpk udp_report_item_v6(const mib2_udp6Entry_t *ude6, boolean_t first,
50261676Sjpk     const mib2_transportMLPEntry_t *attr)
50270Sstevel@tonic-gate {
50280Sstevel@tonic-gate 	char	lname[MAXHOSTNAMELEN + MAXHOSTNAMELEN + 1];
50290Sstevel@tonic-gate 			/* hostname + portname */
50300Sstevel@tonic-gate 	char	ifname[LIFNAMSIZ + 1];
50311676Sjpk 	const char *ifnamep;
50320Sstevel@tonic-gate 
50330Sstevel@tonic-gate 	if (!(Aflag || ude6->udp6EntryInfo.ue_state >= MIB2_UDP_connected))
50340Sstevel@tonic-gate 		return (first); /* Nothing to print */
50350Sstevel@tonic-gate 
50360Sstevel@tonic-gate 	if (first) {
50371676Sjpk 		(void) printf("\nUDP: IPv6\n");
50381676Sjpk 		(void) printf(udp_hdr_v6);
50390Sstevel@tonic-gate 		first = B_FALSE;
50400Sstevel@tonic-gate 	}
50410Sstevel@tonic-gate 
50421676Sjpk 	ifnamep = (ude6->udp6IfIndex != 0) ?
50431676Sjpk 	    if_indextoname(ude6->udp6IfIndex, ifname) : NULL;
50441676Sjpk 
50450Sstevel@tonic-gate 	(void) printf("%-33s ",
50460Sstevel@tonic-gate 	    pr_ap6(&ude6->udp6LocalAddress,
50470Sstevel@tonic-gate 	    ude6->udp6LocalPort, "udp", lname, sizeof (lname)));
50481676Sjpk 	(void) printf("%-33s %-10s %s\n",
50491676Sjpk 	    ude6->udp6EntryInfo.ue_state == MIB2_UDP_connected ?
50501676Sjpk 	    pr_ap6(&ude6->udp6EntryInfo.ue_RemoteAddress,
50511676Sjpk 	    ude6->udp6EntryInfo.ue_RemotePort, "udp", lname, sizeof (lname)) :
50521676Sjpk 	    "",
50531676Sjpk 	    miudp_state(ude6->udp6EntryInfo.ue_state, attr),
50541676Sjpk 	    ifnamep == NULL ? "" : ifnamep);
50551676Sjpk 
50561676Sjpk 	/*
50571676Sjpk 	 * UDP sockets don't have remote attributes, so there's no need to
50581676Sjpk 	 * print them here.
50591676Sjpk 	 */
50601676Sjpk 
50610Sstevel@tonic-gate 	return (first);
50620Sstevel@tonic-gate }
50630Sstevel@tonic-gate 
50640Sstevel@tonic-gate /* ------------------------------ SCTP_REPORT------------------------------- */
50650Sstevel@tonic-gate 
50660Sstevel@tonic-gate static const char sctp_hdr[] =
50670Sstevel@tonic-gate "\nSCTP:";
50680Sstevel@tonic-gate static const char sctp_hdr_normal[] =
50690Sstevel@tonic-gate "        Local Address                   Remote Address          "
50700Sstevel@tonic-gate "Swind  Send-Q Rwind  Recv-Q StrsI/O  State\n"
50710Sstevel@tonic-gate "------------------------------- ------------------------------- "
50720Sstevel@tonic-gate "------ ------ ------ ------ ------- -----------";
50730Sstevel@tonic-gate 
50740Sstevel@tonic-gate static const char *
50751676Sjpk nssctp_state(int state, const mib2_transportMLPEntry_t *attr)
50760Sstevel@tonic-gate {
50771676Sjpk 	static char sctpsbuf[50];
50781676Sjpk 	const char *cp;
50791676Sjpk 
50800Sstevel@tonic-gate 	switch (state) {
50810Sstevel@tonic-gate 	case MIB2_SCTP_closed:
50821676Sjpk 		cp = "CLOSED";
50831676Sjpk 		break;
50840Sstevel@tonic-gate 	case MIB2_SCTP_cookieWait:
50851676Sjpk 		cp = "COOKIE_WAIT";
50861676Sjpk 		break;
50870Sstevel@tonic-gate 	case MIB2_SCTP_cookieEchoed:
50881676Sjpk 		cp = "COOKIE_ECHOED";
50891676Sjpk 		break;
50900Sstevel@tonic-gate 	case MIB2_SCTP_established:
50911676Sjpk 		cp = "ESTABLISHED";
50921676Sjpk 		break;
50930Sstevel@tonic-gate 	case MIB2_SCTP_shutdownPending:
50941676Sjpk 		cp = "SHUTDOWN_PENDING";
50951676Sjpk 		break;
50960Sstevel@tonic-gate 	case MIB2_SCTP_shutdownSent:
50971676Sjpk 		cp = "SHUTDOWN_SENT";
50981676Sjpk 		break;
50990Sstevel@tonic-gate 	case MIB2_SCTP_shutdownReceived:
51001676Sjpk 		cp = "SHUTDOWN_RECEIVED";
51011676Sjpk 		break;
51020Sstevel@tonic-gate 	case MIB2_SCTP_shutdownAckSent:
51031676Sjpk 		cp = "SHUTDOWN_ACK_SENT";
51041676Sjpk 		break;
51050Sstevel@tonic-gate 	case MIB2_SCTP_listen:
51061676Sjpk 		cp = "LISTEN";
51071676Sjpk 		break;
51080Sstevel@tonic-gate 	default:
51091676Sjpk 		(void) snprintf(sctpsbuf, sizeof (sctpsbuf),
51101676Sjpk 		    "UNKNOWN STATE(%d)", state);
51111676Sjpk 		cp = sctpsbuf;
51121676Sjpk 		break;
51130Sstevel@tonic-gate 	}
51141676Sjpk 
51151676Sjpk 	if (RSECflag && attr != NULL && attr->tme_flags != 0) {
51161676Sjpk 		if (cp != sctpsbuf) {
51171676Sjpk 			(void) strlcpy(sctpsbuf, cp, sizeof (sctpsbuf));
51181676Sjpk 			cp = sctpsbuf;
51191676Sjpk 		}
51201676Sjpk 		if (attr->tme_flags & MIB2_TMEF_PRIVATE)
51211676Sjpk 			(void) strlcat(sctpsbuf, " P", sizeof (sctpsbuf));
51221676Sjpk 		if (attr->tme_flags & MIB2_TMEF_SHARED)
51231676Sjpk 			(void) strlcat(sctpsbuf, " S", sizeof (sctpsbuf));
51241676Sjpk 	}
51251676Sjpk 
51261676Sjpk 	return (cp);
51270Sstevel@tonic-gate }
51280Sstevel@tonic-gate 
51291676Sjpk static const mib2_sctpConnRemoteEntry_t *
51301676Sjpk sctp_getnext_rem(const mib_item_t **itemp,
51311676Sjpk     const mib2_sctpConnRemoteEntry_t *current, uint32_t associd)
51320Sstevel@tonic-gate {
51331676Sjpk 	const mib_item_t *item = *itemp;
51341676Sjpk 	const mib2_sctpConnRemoteEntry_t	*sre;
51350Sstevel@tonic-gate 
51360Sstevel@tonic-gate 	for (; item != NULL; item = item->next_item, current = NULL) {
51370Sstevel@tonic-gate 		if (!(item->group == MIB2_SCTP &&
51380Sstevel@tonic-gate 		    item->mib_id == MIB2_SCTP_CONN_REMOTE)) {
51390Sstevel@tonic-gate 			continue;
51400Sstevel@tonic-gate 		}
51410Sstevel@tonic-gate 
51420Sstevel@tonic-gate 		if (current != NULL) {
51430Sstevel@tonic-gate 			/* LINTED: (note 1) */
51441676Sjpk 			sre = (const mib2_sctpConnRemoteEntry_t *)
51451676Sjpk 			    ((const char *)current + sctpRemoteEntrySize);
51460Sstevel@tonic-gate 		} else {
51470Sstevel@tonic-gate 			sre = item->valp;
51480Sstevel@tonic-gate 		}
51490Sstevel@tonic-gate 		for (; (char *)sre < (char *)item->valp + item->length;
51501676Sjpk 		    /* LINTED: (note 1) */
51511676Sjpk 		    sre = (const mib2_sctpConnRemoteEntry_t *)
51521676Sjpk 		    ((const char *)sre + sctpRemoteEntrySize)) {
51530Sstevel@tonic-gate 			if (sre->sctpAssocId != associd) {
51540Sstevel@tonic-gate 				continue;
51550Sstevel@tonic-gate 			}
51560Sstevel@tonic-gate 			*itemp = item;
51570Sstevel@tonic-gate 			return (sre);
51580Sstevel@tonic-gate 		}
51590Sstevel@tonic-gate 	}
51600Sstevel@tonic-gate 	*itemp = NULL;
51610Sstevel@tonic-gate 	return (NULL);
51620Sstevel@tonic-gate }
51630Sstevel@tonic-gate 
51641676Sjpk static const mib2_sctpConnLocalEntry_t *
51651676Sjpk sctp_getnext_local(const mib_item_t **itemp,
51661676Sjpk     const mib2_sctpConnLocalEntry_t *current, uint32_t associd)
51670Sstevel@tonic-gate {
51681676Sjpk 	const mib_item_t *item = *itemp;
51691676Sjpk 	const mib2_sctpConnLocalEntry_t	*sle;
51700Sstevel@tonic-gate 
51710Sstevel@tonic-gate 	for (; item != NULL; item = item->next_item, current = NULL) {
51720Sstevel@tonic-gate 		if (!(item->group == MIB2_SCTP &&
51730Sstevel@tonic-gate 		    item->mib_id == MIB2_SCTP_CONN_LOCAL)) {
51740Sstevel@tonic-gate 			continue;
51750Sstevel@tonic-gate 		}
51760Sstevel@tonic-gate 
51770Sstevel@tonic-gate 		if (current != NULL) {
51780Sstevel@tonic-gate 			/* LINTED: (note 1) */
51791676Sjpk 			sle = (const mib2_sctpConnLocalEntry_t *)
51801676Sjpk 			    ((const char *)current + sctpLocalEntrySize);
51810Sstevel@tonic-gate 		} else {
51820Sstevel@tonic-gate 			sle = item->valp;
51830Sstevel@tonic-gate 		}
51840Sstevel@tonic-gate 		for (; (char *)sle < (char *)item->valp + item->length;
51851676Sjpk 		    /* LINTED: (note 1) */
51861676Sjpk 		    sle = (const mib2_sctpConnLocalEntry_t *)
51871676Sjpk 		    ((const char *)sle + sctpLocalEntrySize)) {
51880Sstevel@tonic-gate 			if (sle->sctpAssocId != associd) {
51890Sstevel@tonic-gate 				continue;
51900Sstevel@tonic-gate 			}
51910Sstevel@tonic-gate 			*itemp = item;
51920Sstevel@tonic-gate 			return (sle);
51930Sstevel@tonic-gate 		}
51940Sstevel@tonic-gate 	}
51950Sstevel@tonic-gate 	*itemp = NULL;
51960Sstevel@tonic-gate 	return (NULL);
51970Sstevel@tonic-gate }
51980Sstevel@tonic-gate 
51990Sstevel@tonic-gate static void
52000Sstevel@tonic-gate sctp_pr_addr(int type, char *name, int namelen, const in6_addr_t *addr,
52010Sstevel@tonic-gate     int port)
52020Sstevel@tonic-gate {
52030Sstevel@tonic-gate 	ipaddr_t	v4addr;
52040Sstevel@tonic-gate 	in6_addr_t	v6addr;
52050Sstevel@tonic-gate 
52060Sstevel@tonic-gate 	/*
52070Sstevel@tonic-gate 	 * Address is either a v4 mapped or v6 addr. If
52080Sstevel@tonic-gate 	 * it's a v4 mapped, convert to v4 before
52090Sstevel@tonic-gate 	 * displaying.
52100Sstevel@tonic-gate 	 */
52110Sstevel@tonic-gate 	switch (type) {
52120Sstevel@tonic-gate 	    case MIB2_SCTP_ADDR_V4:
52130Sstevel@tonic-gate 		/* v4 */
52140Sstevel@tonic-gate 		v6addr = *addr;
52150Sstevel@tonic-gate 
52160Sstevel@tonic-gate 		IN6_V4MAPPED_TO_IPADDR(&v6addr, v4addr);
52170Sstevel@tonic-gate 		if (port > 0) {
52180Sstevel@tonic-gate 			(void) pr_ap(v4addr, port, "sctp", name, namelen);
52190Sstevel@tonic-gate 		} else {
52200Sstevel@tonic-gate 			(void) pr_addr(v4addr, name, namelen);
52210Sstevel@tonic-gate 		}
52220Sstevel@tonic-gate 		break;
52230Sstevel@tonic-gate 
52240Sstevel@tonic-gate 	    case MIB2_SCTP_ADDR_V6:
52250Sstevel@tonic-gate 		/* v6 */
52260Sstevel@tonic-gate 		if (port > 0) {
52270Sstevel@tonic-gate 			(void) pr_ap6(addr, port, "sctp", name, namelen);
52280Sstevel@tonic-gate 		} else {
52290Sstevel@tonic-gate 			(void) pr_addr6(addr, name, namelen);
52300Sstevel@tonic-gate 		}
52310Sstevel@tonic-gate 		break;
52320Sstevel@tonic-gate 
52330Sstevel@tonic-gate 	    default:
52340Sstevel@tonic-gate 		(void) snprintf(name, namelen, "<unknown addr type>");
52350Sstevel@tonic-gate 		break;
52360Sstevel@tonic-gate 	}
52370Sstevel@tonic-gate }
52380Sstevel@tonic-gate 
52390Sstevel@tonic-gate static void
52401676Sjpk sctp_conn_report_item(const mib_item_t *head, const mib2_sctpConnEntry_t *sp,
52411676Sjpk     const mib2_transportMLPEntry_t *attr)
52420Sstevel@tonic-gate {
52430Sstevel@tonic-gate 	char		lname[MAXHOSTNAMELEN + MAXHOSTNAMELEN + 1];
52440Sstevel@tonic-gate 	char		fname[MAXHOSTNAMELEN + MAXHOSTNAMELEN + 1];
52451676Sjpk 	const mib2_sctpConnRemoteEntry_t	*sre = NULL;
52461676Sjpk 	const mib2_sctpConnLocalEntry_t	*sle = NULL;
52471676Sjpk 	const mib_item_t *local = head;
52481676Sjpk 	const mib_item_t *remote = head;
52490Sstevel@tonic-gate 	uint32_t	id = sp->sctpAssocId;
52500Sstevel@tonic-gate 	boolean_t	printfirst = B_TRUE;
52510Sstevel@tonic-gate 
52520Sstevel@tonic-gate 	sctp_pr_addr(sp->sctpAssocRemPrimAddrType, fname, sizeof (fname),
52530Sstevel@tonic-gate 	    &sp->sctpAssocRemPrimAddr, sp->sctpAssocRemPort);
52540Sstevel@tonic-gate 	sctp_pr_addr(sp->sctpAssocRemPrimAddrType, lname, sizeof (lname),
52550Sstevel@tonic-gate 	    &sp->sctpAssocLocPrimAddr, sp->sctpAssocLocalPort);
52560Sstevel@tonic-gate 
52570Sstevel@tonic-gate 	(void) printf("%-31s %-31s %6u %6d %6u %6d %3d/%-3d %s\n",
52580Sstevel@tonic-gate 	    lname, fname,
52590Sstevel@tonic-gate 	    sp->sctpConnEntryInfo.ce_swnd,
52600Sstevel@tonic-gate 	    sp->sctpConnEntryInfo.ce_sendq,
52610Sstevel@tonic-gate 	    sp->sctpConnEntryInfo.ce_rwnd,
52620Sstevel@tonic-gate 	    sp->sctpConnEntryInfo.ce_recvq,
52630Sstevel@tonic-gate 	    sp->sctpAssocInStreams, sp->sctpAssocOutStreams,
52641676Sjpk 	    nssctp_state(sp->sctpAssocState, attr));
52651676Sjpk 
52661676Sjpk 	print_transport_label(attr);
52670Sstevel@tonic-gate 
52680Sstevel@tonic-gate 	if (!Vflag) {
52690Sstevel@tonic-gate 		return;
52700Sstevel@tonic-gate 	}
52710Sstevel@tonic-gate 
52720Sstevel@tonic-gate 	/* Print remote addresses/local addresses on following lines */
52730Sstevel@tonic-gate 	while ((sre = sctp_getnext_rem(&remote, sre, id)) != NULL) {
52740Sstevel@tonic-gate 		if (!IN6_ARE_ADDR_EQUAL(&sre->sctpAssocRemAddr,
52750Sstevel@tonic-gate 		    &sp->sctpAssocRemPrimAddr)) {
52760Sstevel@tonic-gate 			if (printfirst == B_TRUE) {
52770Sstevel@tonic-gate 				(void) fputs("\t<Remote: ", stdout);
52780Sstevel@tonic-gate 				printfirst = B_FALSE;
52790Sstevel@tonic-gate 			} else {
52800Sstevel@tonic-gate 				(void) fputs(", ", stdout);
52810Sstevel@tonic-gate 			}
52820Sstevel@tonic-gate 			sctp_pr_addr(sre->sctpAssocRemAddrType, fname,
52830Sstevel@tonic-gate 			    sizeof (fname), &sre->sctpAssocRemAddr, -1);
52840Sstevel@tonic-gate 			if (sre->sctpAssocRemAddrActive == MIB2_SCTP_ACTIVE) {
52850Sstevel@tonic-gate 				(void) fputs(fname, stdout);
52860Sstevel@tonic-gate 			} else {
52870Sstevel@tonic-gate 				(void) printf("(%s)", fname);
52880Sstevel@tonic-gate 			}
52890Sstevel@tonic-gate 		}
52900Sstevel@tonic-gate 	}
52910Sstevel@tonic-gate 	if (printfirst == B_FALSE) {
52920Sstevel@tonic-gate 		(void) puts(">");
52930Sstevel@tonic-gate 		printfirst = B_TRUE;
52940Sstevel@tonic-gate 	}
52950Sstevel@tonic-gate 	while ((sle = sctp_getnext_local(&local, sle, id)) != NULL) {
52960Sstevel@tonic-gate 		if (!IN6_ARE_ADDR_EQUAL(&sle->sctpAssocLocalAddr,
52970Sstevel@tonic-gate 		    &sp->sctpAssocLocPrimAddr)) {
52980Sstevel@tonic-gate 			if (printfirst == B_TRUE) {
52990Sstevel@tonic-gate 				(void) fputs("\t<Local: ", stdout);
53000Sstevel@tonic-gate 				printfirst = B_FALSE;
53010Sstevel@tonic-gate 			} else {
53020Sstevel@tonic-gate 				(void) fputs(", ", stdout);
53030Sstevel@tonic-gate 			}
53040Sstevel@tonic-gate 			sctp_pr_addr(sle->sctpAssocLocalAddrType, lname,
53050Sstevel@tonic-gate 			    sizeof (lname), &sle->sctpAssocLocalAddr, -1);
53060Sstevel@tonic-gate 			(void) fputs(lname, stdout);
53070Sstevel@tonic-gate 		}
53080Sstevel@tonic-gate 	}
53090Sstevel@tonic-gate 	if (printfirst == B_FALSE) {
53100Sstevel@tonic-gate 		(void) puts(">");
53110Sstevel@tonic-gate 	}
53120Sstevel@tonic-gate }
53130Sstevel@tonic-gate 
53140Sstevel@tonic-gate static void
53151676Sjpk sctp_report(const mib_item_t *item)
53160Sstevel@tonic-gate {
53171676Sjpk 	const mib_item_t		*head;
53181676Sjpk 	const mib2_sctpConnEntry_t	*sp;
53190Sstevel@tonic-gate 	boolean_t		first = B_TRUE;
53201676Sjpk 	mib2_transportMLPEntry_t **attrs, **aptr;
53211676Sjpk 	mib2_transportMLPEntry_t *attr;
53221676Sjpk 
53231676Sjpk 	/*
53241676Sjpk 	 * Preparation pass: the kernel returns separate entries for SCTP
53251676Sjpk 	 * connection table entries and Multilevel Port attributes.  We loop
53261676Sjpk 	 * through the attributes first and set up an array for each address
53271676Sjpk 	 * family.
53281676Sjpk 	 */
53291676Sjpk 	attrs = RSECflag ?
53301676Sjpk 	    gather_attrs(item, MIB2_SCTP, MIB2_SCTP_CONN, sctpEntrySize) :
53311676Sjpk 	    NULL;
53321676Sjpk 
53331676Sjpk 	aptr = attrs;
53340Sstevel@tonic-gate 	head = item;
53350Sstevel@tonic-gate 	for (; item != NULL; item = item->next_item) {
53360Sstevel@tonic-gate 
53370Sstevel@tonic-gate 		if (!(item->group == MIB2_SCTP &&
53380Sstevel@tonic-gate 		    item->mib_id == MIB2_SCTP_CONN))
53390Sstevel@tonic-gate 			continue;
53400Sstevel@tonic-gate 
53410Sstevel@tonic-gate 		for (sp = item->valp;
53421676Sjpk 		    (char *)sp < (char *)item->valp + item->length;
53431676Sjpk 		    /* LINTED: (note 1) */
53441676Sjpk 		    sp = (mib2_sctpConnEntry_t *)((char *)sp + sctpEntrySize)) {
53451676Sjpk 			attr = aptr == NULL ? NULL : *aptr++;
53460Sstevel@tonic-gate 			if (Aflag ||
53470Sstevel@tonic-gate 			    sp->sctpAssocState >= MIB2_SCTP_established) {
53480Sstevel@tonic-gate 				if (first == B_TRUE) {
53490Sstevel@tonic-gate 					(void) puts(sctp_hdr);
53500Sstevel@tonic-gate 					(void) puts(sctp_hdr_normal);
53510Sstevel@tonic-gate 					first = B_FALSE;
53520Sstevel@tonic-gate 				}
53531676Sjpk 				sctp_conn_report_item(head, sp, attr);
53540Sstevel@tonic-gate 			}
53550Sstevel@tonic-gate 		}
53560Sstevel@tonic-gate 	}
53571676Sjpk 	if (attrs != NULL)
53581676Sjpk 		free(attrs);
53590Sstevel@tonic-gate }
53600Sstevel@tonic-gate 
53610Sstevel@tonic-gate static char *
53620Sstevel@tonic-gate plural(int n)
53630Sstevel@tonic-gate {
53640Sstevel@tonic-gate 	return (n != 1 ? "s" : "");
53650Sstevel@tonic-gate }
53660Sstevel@tonic-gate 
53670Sstevel@tonic-gate static char *
53680Sstevel@tonic-gate pluraly(int n)
53690Sstevel@tonic-gate {
53700Sstevel@tonic-gate 	return (n != 1 ? "ies" : "y");
53710Sstevel@tonic-gate }
53720Sstevel@tonic-gate 
53730Sstevel@tonic-gate static char *
53740Sstevel@tonic-gate plurales(int n)
53750Sstevel@tonic-gate {
53760Sstevel@tonic-gate 	return (n != 1 ? "es" : "");
53770Sstevel@tonic-gate }
53780Sstevel@tonic-gate 
53790Sstevel@tonic-gate static char *
53800Sstevel@tonic-gate pktscale(n)
53810Sstevel@tonic-gate 	int n;
53820Sstevel@tonic-gate {
53830Sstevel@tonic-gate 	static char buf[6];
53840Sstevel@tonic-gate 	char t;
53850Sstevel@tonic-gate 
53860Sstevel@tonic-gate 	if (n < 1024) {
53870Sstevel@tonic-gate 		t = ' ';
53880Sstevel@tonic-gate 	} else if (n < 1024 * 1024) {
53890Sstevel@tonic-gate 		t = 'k';
53900Sstevel@tonic-gate 		n /= 1024;
53910Sstevel@tonic-gate 	} else if (n < 1024 * 1024 * 1024) {
53920Sstevel@tonic-gate 		t = 'm';
53930Sstevel@tonic-gate 		n /= 1024 * 1024;
53940Sstevel@tonic-gate 	} else {
53950Sstevel@tonic-gate 		t = 'g';
53960Sstevel@tonic-gate 		n /= 1024 * 1024 * 1024;
53970Sstevel@tonic-gate 	}
53980Sstevel@tonic-gate 
53990Sstevel@tonic-gate 	(void) snprintf(buf, sizeof (buf), "%4u%c", n, t);
54000Sstevel@tonic-gate 	return (buf);
54010Sstevel@tonic-gate }
54020Sstevel@tonic-gate 
54030Sstevel@tonic-gate /* --------------------- mrt_report (netstat -m) -------------------------- */
54040Sstevel@tonic-gate 
54050Sstevel@tonic-gate static void
54060Sstevel@tonic-gate mrt_report(mib_item_t *item)
54070Sstevel@tonic-gate {
54080Sstevel@tonic-gate 	int		jtemp = 0;
54090Sstevel@tonic-gate 	struct vifctl	*vip;
54100Sstevel@tonic-gate 	vifi_t		vifi;
54110Sstevel@tonic-gate 	struct mfcctl	*mfccp;
54120Sstevel@tonic-gate 	int		numvifs = 0;
54130Sstevel@tonic-gate 	int		nmfc = 0;
54140Sstevel@tonic-gate 	char		abuf[MAXHOSTNAMELEN + 1];
54150Sstevel@tonic-gate 
54160Sstevel@tonic-gate 	if (!(family_selected(AF_INET)))
54170Sstevel@tonic-gate 		return;
54180Sstevel@tonic-gate 
54190Sstevel@tonic-gate 	/* 'for' loop 1: */
54200Sstevel@tonic-gate 	for (; item; item = item->next_item) {
54210Sstevel@tonic-gate 		if (Dflag) {
54220Sstevel@tonic-gate 			(void) printf("\n--- Entry %d ---\n", ++jtemp);
54230Sstevel@tonic-gate 			(void) printf("Group = %d, mib_id = %d, "
54240Sstevel@tonic-gate 			    "length = %d, valp = 0x%p\n",
54250Sstevel@tonic-gate 			    item->group, item->mib_id, item->length,
54260Sstevel@tonic-gate 			    item->valp);
54270Sstevel@tonic-gate 		}
54280Sstevel@tonic-gate 		if (item->group != EXPER_DVMRP)
54290Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
54300Sstevel@tonic-gate 
54310Sstevel@tonic-gate 		switch (item->mib_id) {
54320Sstevel@tonic-gate 
54330Sstevel@tonic-gate 		case EXPER_DVMRP_VIF:
54340Sstevel@tonic-gate 			if (Dflag)
54350Sstevel@tonic-gate 				(void) printf("%u records for ipVifTable:\n",
54360Sstevel@tonic-gate 				    item->length/sizeof (struct vifctl));
54370Sstevel@tonic-gate 			if (item->length/sizeof (struct vifctl) == 0) {
54380Sstevel@tonic-gate 				(void) puts("\nVirtual Interface Table is "
54390Sstevel@tonic-gate 				    "empty");
54400Sstevel@tonic-gate 				break;
54410Sstevel@tonic-gate 			}
54420Sstevel@tonic-gate 
54430Sstevel@tonic-gate 			(void) puts("\nVirtual Interface Table\n"
54440Sstevel@tonic-gate 			    " Vif Threshold Rate_Limit Local-Address"
54450Sstevel@tonic-gate 			    "   Remote-Address     Pkt_in   Pkt_out");
54460Sstevel@tonic-gate 
54470Sstevel@tonic-gate 			/* 'for' loop 2: */
54480Sstevel@tonic-gate 			for (vip = (struct vifctl *)item->valp;
54490Sstevel@tonic-gate 			    (char *)vip < (char *)item->valp + item->length;
54500Sstevel@tonic-gate 			    /* LINTED: (note 1) */
54510Sstevel@tonic-gate 			    vip = (struct vifctl *)((char *)vip +
54520Sstevel@tonic-gate 			    vifctlSize)) {
54530Sstevel@tonic-gate 				if (vip->vifc_lcl_addr.s_addr == 0)
54540Sstevel@tonic-gate 					continue; /* 'for' loop 2 */
54550Sstevel@tonic-gate 				/* numvifs = vip->vifc_vifi; */
54560Sstevel@tonic-gate 
54570Sstevel@tonic-gate 				numvifs++;
54580Sstevel@tonic-gate 				(void) printf("  %2u       %3u       "
54590Sstevel@tonic-gate 				    "%4u %-15.15s",
54600Sstevel@tonic-gate 				    vip->vifc_vifi,
54610Sstevel@tonic-gate 				    vip->vifc_threshold,
54620Sstevel@tonic-gate 				    vip->vifc_rate_limit,
54630Sstevel@tonic-gate 				    pr_addr(vip->vifc_lcl_addr.s_addr,
54640Sstevel@tonic-gate 				    abuf, sizeof (abuf)));
54650Sstevel@tonic-gate 				(void) printf(" %-15.15s  %8u  %8u\n",
54660Sstevel@tonic-gate 				    (vip->vifc_flags & VIFF_TUNNEL) ?
54670Sstevel@tonic-gate 				    pr_addr(vip->vifc_rmt_addr.s_addr,
54680Sstevel@tonic-gate 				    abuf, sizeof (abuf)) : "",
54690Sstevel@tonic-gate 				    vip->vifc_pkt_in,
54700Sstevel@tonic-gate 				    vip->vifc_pkt_out);
54710Sstevel@tonic-gate 			} /* 'for' loop 2 ends */
54720Sstevel@tonic-gate 
54730Sstevel@tonic-gate 			(void) printf("Numvifs: %d\n", numvifs);
54740Sstevel@tonic-gate 			break;
54750Sstevel@tonic-gate 
54760Sstevel@tonic-gate 		case EXPER_DVMRP_MRT:
54770Sstevel@tonic-gate 			if (Dflag)
54780Sstevel@tonic-gate 				(void) printf("%u records for ipMfcTable:\n",
54790Sstevel@tonic-gate 					item->length/sizeof (struct vifctl));
54800Sstevel@tonic-gate 			if (item->length/sizeof (struct vifctl) == 0) {
54810Sstevel@tonic-gate 				(void) puts("\nMulticast Forwarding Cache is "
54820Sstevel@tonic-gate 				    "empty");
54830Sstevel@tonic-gate 				break;
54840Sstevel@tonic-gate 			}
54850Sstevel@tonic-gate 
54860Sstevel@tonic-gate 			(void) puts("\nMulticast Forwarding Cache\n"
54870Sstevel@tonic-gate 			    "  Origin-Subnet                 Mcastgroup      "
54880Sstevel@tonic-gate 			    "# Pkts  In-Vif  Out-vifs/Forw-ttl");
54890Sstevel@tonic-gate 
54900Sstevel@tonic-gate 			for (mfccp = (struct mfcctl *)item->valp;
54910Sstevel@tonic-gate 			    (char *)mfccp < (char *)item->valp + item->length;
54920Sstevel@tonic-gate 			    /* LINTED: (note 1) */
54930Sstevel@tonic-gate 			    mfccp = (struct mfcctl *)((char *)mfccp +
54940Sstevel@tonic-gate 			    mfcctlSize)) {
54950Sstevel@tonic-gate 
54960Sstevel@tonic-gate 				nmfc++;
54970Sstevel@tonic-gate 				(void) printf("  %-30.15s",
54980Sstevel@tonic-gate 				    pr_addr(mfccp->mfcc_origin.s_addr,
54990Sstevel@tonic-gate 				    abuf, sizeof (abuf)));
55000Sstevel@tonic-gate 				(void) printf("%-15.15s  %6s  %3u    ",
55010Sstevel@tonic-gate 				    pr_net(mfccp->mfcc_mcastgrp.s_addr,
55020Sstevel@tonic-gate 					mfccp->mfcc_mcastgrp.s_addr,
55030Sstevel@tonic-gate 					abuf, sizeof (abuf)),
55040Sstevel@tonic-gate 				    pktscale((int)mfccp->mfcc_pkt_cnt),
55050Sstevel@tonic-gate 					mfccp->mfcc_parent);
55060Sstevel@tonic-gate 
55070Sstevel@tonic-gate 				for (vifi = 0; vifi < MAXVIFS; ++vifi) {
55080Sstevel@tonic-gate 					if (mfccp->mfcc_ttls[vifi]) {
55090Sstevel@tonic-gate 						(void) printf("      %u (%u)",
55100Sstevel@tonic-gate 						    vifi,
55110Sstevel@tonic-gate 						    mfccp->mfcc_ttls[vifi]);
55120Sstevel@tonic-gate 					}
55130Sstevel@tonic-gate 
55140Sstevel@tonic-gate 				}
55150Sstevel@tonic-gate 				(void) putchar('\n');
55160Sstevel@tonic-gate 			}
55170Sstevel@tonic-gate 			(void) printf("\nTotal no. of entries in cache: %d\n",
55180Sstevel@tonic-gate 			    nmfc);
55190Sstevel@tonic-gate 			break;
55200Sstevel@tonic-gate 		}
55210Sstevel@tonic-gate 	} /* 'for' loop 1 ends */
55220Sstevel@tonic-gate 	(void) putchar('\n');
55230Sstevel@tonic-gate 	(void) fflush(stdout);
55240Sstevel@tonic-gate }
55250Sstevel@tonic-gate 
55260Sstevel@tonic-gate /*
55270Sstevel@tonic-gate  * Get the stats for the cache named 'name'.  If prefix != 0, then
55280Sstevel@tonic-gate  * interpret the name as a prefix, and sum up stats for all caches
55290Sstevel@tonic-gate  * named 'name*'.
55300Sstevel@tonic-gate  */
55310Sstevel@tonic-gate static void
55320Sstevel@tonic-gate kmem_cache_stats(char *title, char *name, int prefix, int64_t *total_bytes)
55330Sstevel@tonic-gate {
55340Sstevel@tonic-gate 	int len;
55350Sstevel@tonic-gate 	int alloc;
55360Sstevel@tonic-gate 	int64_t total_alloc = 0;
55370Sstevel@tonic-gate 	int alloc_fail, total_alloc_fail = 0;
55380Sstevel@tonic-gate 	int buf_size = 0;
55390Sstevel@tonic-gate 	int buf_avail;
55400Sstevel@tonic-gate 	int buf_total;
55410Sstevel@tonic-gate 	int buf_max, total_buf_max = 0;
55420Sstevel@tonic-gate 	int buf_inuse, total_buf_inuse = 0;
55430Sstevel@tonic-gate 	kstat_t *ksp;
55440Sstevel@tonic-gate 	char buf[256];
55450Sstevel@tonic-gate 
55460Sstevel@tonic-gate 	len = prefix ? strlen(name) : 256;
55470Sstevel@tonic-gate 
55480Sstevel@tonic-gate 	/* 'for' loop 1: */
55490Sstevel@tonic-gate 	for (ksp = kc->kc_chain; ksp != NULL; ksp = ksp->ks_next) {
55500Sstevel@tonic-gate 
55510Sstevel@tonic-gate 		if (strcmp(ksp->ks_class, "kmem_cache") != 0)
55520Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
55530Sstevel@tonic-gate 
55540Sstevel@tonic-gate 		/*
55550Sstevel@tonic-gate 		 * Hack alert: because of the way streams messages are
55560Sstevel@tonic-gate 		 * allocated, every constructed free dblk has an associated
55570Sstevel@tonic-gate 		 * mblk.  From the allocator's viewpoint those mblks are
55580Sstevel@tonic-gate 		 * allocated (because they haven't been freed), but from
55590Sstevel@tonic-gate 		 * our viewpoint they're actually free (because they're
55600Sstevel@tonic-gate 		 * not currently in use).  To account for this caching
55610Sstevel@tonic-gate 		 * effect we subtract the total constructed free dblks
55620Sstevel@tonic-gate 		 * from the total allocated mblks to derive mblks in use.
55630Sstevel@tonic-gate 		 */
55640Sstevel@tonic-gate 		if (strcmp(name, "streams_mblk") == 0 &&
55650Sstevel@tonic-gate 		    strncmp(ksp->ks_name, "streams_dblk", 12) == 0) {
55660Sstevel@tonic-gate 			(void) safe_kstat_read(kc, ksp, NULL);
55670Sstevel@tonic-gate 			total_buf_inuse -=
55680Sstevel@tonic-gate 				kstat_named_value(ksp, "buf_constructed");
55690Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
55700Sstevel@tonic-gate 		}
55710Sstevel@tonic-gate 
55720Sstevel@tonic-gate 		if (strncmp(ksp->ks_name, name, len) != 0)
55730Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
55740Sstevel@tonic-gate 
55750Sstevel@tonic-gate 		(void) safe_kstat_read(kc, ksp, NULL);
55760Sstevel@tonic-gate 
55770Sstevel@tonic-gate 		alloc		= kstat_named_value(ksp, "alloc");
55780Sstevel@tonic-gate 		alloc_fail	= kstat_named_value(ksp, "alloc_fail");
55790Sstevel@tonic-gate 		buf_size	= kstat_named_value(ksp, "buf_size");
55800Sstevel@tonic-gate 		buf_avail	= kstat_named_value(ksp, "buf_avail");
55810Sstevel@tonic-gate 		buf_total	= kstat_named_value(ksp, "buf_total");
55820Sstevel@tonic-gate 		buf_max		= kstat_named_value(ksp, "buf_max");
55830Sstevel@tonic-gate 		buf_inuse	= buf_total - buf_avail;
55840Sstevel@tonic-gate 
55850Sstevel@tonic-gate 		if (Vflag && prefix) {
55860Sstevel@tonic-gate 			(void) snprintf(buf, sizeof (buf), "%s%s", title,
55870Sstevel@tonic-gate 			    ksp->ks_name + len);
55880Sstevel@tonic-gate 			(void) printf("    %-18s %6u %9u %11u %11u\n",
55890Sstevel@tonic-gate 			    buf, buf_inuse, buf_max, alloc, alloc_fail);
55900Sstevel@tonic-gate 		}
55910Sstevel@tonic-gate 
55920Sstevel@tonic-gate 		total_alloc		+= alloc;
55930Sstevel@tonic-gate 		total_alloc_fail	+= alloc_fail;
55940Sstevel@tonic-gate 		total_buf_max		+= buf_max;
55950Sstevel@tonic-gate 		total_buf_inuse		+= buf_inuse;
55960Sstevel@tonic-gate 		*total_bytes		+= (int64_t)buf_inuse * buf_size;
55970Sstevel@tonic-gate 	} /* 'for' loop 1 ends */
55980Sstevel@tonic-gate 
55990Sstevel@tonic-gate 	if (buf_size == 0) {
56000Sstevel@tonic-gate 		(void) printf("%-22s [couldn't find statistics for %s]\n",
56010Sstevel@tonic-gate 			title, name);
56020Sstevel@tonic-gate 		return;
56030Sstevel@tonic-gate 	}
56040Sstevel@tonic-gate 
56050Sstevel@tonic-gate 	if (Vflag && prefix)
56060Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (buf), "%s_total", title);
56070Sstevel@tonic-gate 	else
56080Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (buf), "%s", title);
56090Sstevel@tonic-gate 
56100Sstevel@tonic-gate 	(void) printf("%-22s %6d %9d %11lld %11d\n", buf,
56110Sstevel@tonic-gate 		total_buf_inuse, total_buf_max, total_alloc, total_alloc_fail);
56120Sstevel@tonic-gate }
56130Sstevel@tonic-gate 
56140Sstevel@tonic-gate static void
56150Sstevel@tonic-gate m_report(void)
56160Sstevel@tonic-gate {
56170Sstevel@tonic-gate 	int64_t total_bytes = 0;
56180Sstevel@tonic-gate 
56190Sstevel@tonic-gate 	(void) puts("streams allocation:");
56200Sstevel@tonic-gate 	(void) printf("%63s\n", "cumulative  allocation");
56210Sstevel@tonic-gate 	(void) printf("%63s\n",
56220Sstevel@tonic-gate 	    "current   maximum       total    failures");
56230Sstevel@tonic-gate 
56240Sstevel@tonic-gate 	kmem_cache_stats("streams",
56250Sstevel@tonic-gate 	    "stream_head_cache", 0, &total_bytes);
56260Sstevel@tonic-gate 	kmem_cache_stats("queues", "queue_cache", 0, &total_bytes);
56270Sstevel@tonic-gate 	kmem_cache_stats("mblk", "streams_mblk", 0, &total_bytes);
56280Sstevel@tonic-gate 	kmem_cache_stats("dblk", "streams_dblk", 1, &total_bytes);
56290Sstevel@tonic-gate 	kmem_cache_stats("linkblk", "linkinfo_cache", 0, &total_bytes);
56300Sstevel@tonic-gate 	kmem_cache_stats("syncq", "syncq_cache", 0, &total_bytes);
56310Sstevel@tonic-gate 	kmem_cache_stats("qband", "qband_cache", 0, &total_bytes);
56320Sstevel@tonic-gate 
56330Sstevel@tonic-gate 	(void) printf("\n%lld Kbytes allocated for streams data\n",
56340Sstevel@tonic-gate 		total_bytes / 1024);
56350Sstevel@tonic-gate 
56360Sstevel@tonic-gate 	(void) putchar('\n');
56370Sstevel@tonic-gate 	(void) fflush(stdout);
56380Sstevel@tonic-gate }
56390Sstevel@tonic-gate 
56400Sstevel@tonic-gate /* --------------------------------- */
56410Sstevel@tonic-gate 
56420Sstevel@tonic-gate /*
56430Sstevel@tonic-gate  * Print an IPv4 address. Remove the matching part of the domain name
56440Sstevel@tonic-gate  * from the returned name.
56450Sstevel@tonic-gate  */
56460Sstevel@tonic-gate static char *
56470Sstevel@tonic-gate pr_addr(uint_t addr, char *dst, uint_t dstlen)
56480Sstevel@tonic-gate {
56490Sstevel@tonic-gate 	char			*cp;
56500Sstevel@tonic-gate 	struct hostent		*hp = NULL;
56510Sstevel@tonic-gate 	static char		domain[MAXHOSTNAMELEN + 1];
56520Sstevel@tonic-gate 	static boolean_t	first = B_TRUE;
56530Sstevel@tonic-gate 	int			error_num;
56540Sstevel@tonic-gate 
56550Sstevel@tonic-gate 	if (first) {
56560Sstevel@tonic-gate 		first = B_FALSE;
56570Sstevel@tonic-gate 		if (sysinfo(SI_HOSTNAME, domain, MAXHOSTNAMELEN) != -1 &&
56580Sstevel@tonic-gate 		    (cp = strchr(domain, '.'))) {
56590Sstevel@tonic-gate 			(void) strncpy(domain, cp + 1, sizeof (domain));
56600Sstevel@tonic-gate 		} else
56610Sstevel@tonic-gate 			domain[0] = 0;
56620Sstevel@tonic-gate 	}
56630Sstevel@tonic-gate 	cp = NULL;
56640Sstevel@tonic-gate 	if (!Nflag) {
56650Sstevel@tonic-gate 		hp = getipnodebyaddr((char *)&addr, sizeof (uint_t), AF_INET,
56660Sstevel@tonic-gate 		    &error_num);
56670Sstevel@tonic-gate 		if (hp) {
56680Sstevel@tonic-gate 			if ((cp = strchr(hp->h_name, '.')) != NULL &&
56690Sstevel@tonic-gate 			    strcasecmp(cp + 1, domain) == 0)
56700Sstevel@tonic-gate 				*cp = 0;
56710Sstevel@tonic-gate 			cp = hp->h_name;
56720Sstevel@tonic-gate 		}
56730Sstevel@tonic-gate 	}
56740Sstevel@tonic-gate 	if (cp != NULL) {
56750Sstevel@tonic-gate 		(void) strncpy(dst, cp, dstlen);
56760Sstevel@tonic-gate 		dst[dstlen - 1] = 0;
56770Sstevel@tonic-gate 	} else {
56780Sstevel@tonic-gate 		(void) inet_ntop(AF_INET, (char *)&addr, dst, dstlen);
56790Sstevel@tonic-gate 	}
56800Sstevel@tonic-gate 	if (hp != NULL)
56810Sstevel@tonic-gate 		freehostent(hp);
56820Sstevel@tonic-gate 	return (dst);
56830Sstevel@tonic-gate }
56840Sstevel@tonic-gate 
56850Sstevel@tonic-gate /*
56860Sstevel@tonic-gate  * Print a non-zero IPv4 address.  Print "    --" if the address is zero.
56870Sstevel@tonic-gate  */
56880Sstevel@tonic-gate static char *
56890Sstevel@tonic-gate pr_addrnz(ipaddr_t addr, char *dst, uint_t dstlen)
56900Sstevel@tonic-gate {
56910Sstevel@tonic-gate 	if (addr == INADDR_ANY) {
56920Sstevel@tonic-gate 		(void) strlcpy(dst, "    --", dstlen);
56930Sstevel@tonic-gate 		return (dst);
56940Sstevel@tonic-gate 	}
56950Sstevel@tonic-gate 	return (pr_addr(addr, dst, dstlen));
56960Sstevel@tonic-gate }
56970Sstevel@tonic-gate 
56980Sstevel@tonic-gate /*
56990Sstevel@tonic-gate  * Print an IPv6 address. Remove the matching part of the domain name
57000Sstevel@tonic-gate  * from the returned name.
57010Sstevel@tonic-gate  */
57020Sstevel@tonic-gate static char *
57030Sstevel@tonic-gate pr_addr6(const struct in6_addr *addr, char *dst, uint_t dstlen)
57040Sstevel@tonic-gate {
57050Sstevel@tonic-gate 	char			*cp;
57060Sstevel@tonic-gate 	struct hostent		*hp = NULL;
57070Sstevel@tonic-gate 	static char		domain[MAXHOSTNAMELEN + 1];
57080Sstevel@tonic-gate 	static boolean_t	first = B_TRUE;
57090Sstevel@tonic-gate 	int			error_num;
57100Sstevel@tonic-gate 
57110Sstevel@tonic-gate 	if (first) {
57120Sstevel@tonic-gate 		first = B_FALSE;
57130Sstevel@tonic-gate 		if (sysinfo(SI_HOSTNAME, domain, MAXHOSTNAMELEN) != -1 &&
57140Sstevel@tonic-gate 		    (cp = strchr(domain, '.'))) {
57150Sstevel@tonic-gate 			(void) strncpy(domain, cp + 1, sizeof (domain));
57160Sstevel@tonic-gate 		} else
57170Sstevel@tonic-gate 			domain[0] = 0;
57180Sstevel@tonic-gate 	}
57190Sstevel@tonic-gate 	cp = NULL;
57200Sstevel@tonic-gate 	if (!Nflag) {
57210Sstevel@tonic-gate 		hp = getipnodebyaddr((char *)addr,
57220Sstevel@tonic-gate 		    sizeof (struct in6_addr), AF_INET6, &error_num);
57230Sstevel@tonic-gate 		if (hp) {
57240Sstevel@tonic-gate 			if ((cp = strchr(hp->h_name, '.')) != NULL &&
57250Sstevel@tonic-gate 			    strcasecmp(cp + 1, domain) == 0)
57260Sstevel@tonic-gate 				*cp = 0;
57270Sstevel@tonic-gate 			cp = hp->h_name;
57280Sstevel@tonic-gate 		}
57290Sstevel@tonic-gate 	}
57300Sstevel@tonic-gate 	if (cp != NULL) {
57310Sstevel@tonic-gate 		(void) strncpy(dst, cp, dstlen);
57320Sstevel@tonic-gate 		dst[dstlen - 1] = 0;
57330Sstevel@tonic-gate 	} else {
57340Sstevel@tonic-gate 		(void) inet_ntop(AF_INET6, (void *)addr, dst, dstlen);
57350Sstevel@tonic-gate 	}
57360Sstevel@tonic-gate 	if (hp != NULL)
57370Sstevel@tonic-gate 		freehostent(hp);
57380Sstevel@tonic-gate 	return (dst);
57390Sstevel@tonic-gate }
57400Sstevel@tonic-gate 
57410Sstevel@tonic-gate /* For IPv4 masks */
57420Sstevel@tonic-gate static char *
57430Sstevel@tonic-gate pr_mask(uint_t addr, char *dst, uint_t dstlen)
57440Sstevel@tonic-gate {
57450Sstevel@tonic-gate 	uint8_t	*ip_addr = (uint8_t *)&addr;
57460Sstevel@tonic-gate 
57470Sstevel@tonic-gate 	(void) snprintf(dst, dstlen, "%d.%d.%d.%d",
57480Sstevel@tonic-gate 	    ip_addr[0], ip_addr[1], ip_addr[2], ip_addr[3]);
57490Sstevel@tonic-gate 	return (dst);
57500Sstevel@tonic-gate }
57510Sstevel@tonic-gate 
57520Sstevel@tonic-gate /*
57530Sstevel@tonic-gate  * For ipv6 masks format is : dest/mask
57540Sstevel@tonic-gate  * Does not print /128 to save space in printout. H flag carries this notion.
57550Sstevel@tonic-gate  */
57560Sstevel@tonic-gate static char *
57571676Sjpk pr_prefix6(const struct in6_addr *addr, uint_t prefixlen, char *dst,
57581676Sjpk     uint_t dstlen)
57590Sstevel@tonic-gate {
57600Sstevel@tonic-gate 	char *cp;
57610Sstevel@tonic-gate 
57620Sstevel@tonic-gate 	if (IN6_IS_ADDR_UNSPECIFIED(addr) && prefixlen == 0) {
57630Sstevel@tonic-gate 		(void) strncpy(dst, "default", dstlen);
57640Sstevel@tonic-gate 		dst[dstlen - 1] = 0;
57650Sstevel@tonic-gate 		return (dst);
57660Sstevel@tonic-gate 	}
57670Sstevel@tonic-gate 
57680Sstevel@tonic-gate 	(void) pr_addr6(addr, dst, dstlen);
57690Sstevel@tonic-gate 	if (prefixlen != IPV6_ABITS) {
57700Sstevel@tonic-gate 		/* How much room is left? */
57710Sstevel@tonic-gate 		cp = strchr(dst, '\0');
57720Sstevel@tonic-gate 		if (dst + dstlen > cp) {
57730Sstevel@tonic-gate 			dstlen -= (cp - dst);
57740Sstevel@tonic-gate 			(void) snprintf(cp, dstlen, "/%d", prefixlen);
57750Sstevel@tonic-gate 		}
57760Sstevel@tonic-gate 	}
57770Sstevel@tonic-gate 	return (dst);
57780Sstevel@tonic-gate }
57790Sstevel@tonic-gate 
57800Sstevel@tonic-gate /* Print IPv4 address and port */
57810Sstevel@tonic-gate static char *
57820Sstevel@tonic-gate pr_ap(uint_t addr, uint_t port, char *proto,
57830Sstevel@tonic-gate     char *dst, uint_t dstlen)
57840Sstevel@tonic-gate {
57850Sstevel@tonic-gate 	char *cp;
57860Sstevel@tonic-gate 
57870Sstevel@tonic-gate 	if (addr == INADDR_ANY) {
57880Sstevel@tonic-gate 		(void) strncpy(dst, "      *", dstlen);
57890Sstevel@tonic-gate 		dst[dstlen - 1] = 0;
57900Sstevel@tonic-gate 	} else {
57910Sstevel@tonic-gate 		(void) pr_addr(addr, dst, dstlen);
57920Sstevel@tonic-gate 	}
57930Sstevel@tonic-gate 	/* How much room is left? */
57940Sstevel@tonic-gate 	cp = strchr(dst, '\0');
57950Sstevel@tonic-gate 	if (dst + dstlen > cp + 1) {
57960Sstevel@tonic-gate 		*cp++ = '.';
57970Sstevel@tonic-gate 		dstlen -= (cp - dst);
57980Sstevel@tonic-gate 		dstlen--;
57990Sstevel@tonic-gate 		(void) portname(port, proto, cp, dstlen);
58000Sstevel@tonic-gate 	}
58010Sstevel@tonic-gate 	return (dst);
58020Sstevel@tonic-gate }
58030Sstevel@tonic-gate 
58040Sstevel@tonic-gate /* Print IPv6 address and port */
58050Sstevel@tonic-gate static char *
58060Sstevel@tonic-gate pr_ap6(const in6_addr_t *addr, uint_t port, char *proto,
58070Sstevel@tonic-gate     char *dst, uint_t dstlen)
58080Sstevel@tonic-gate {
58090Sstevel@tonic-gate 	char *cp;
58100Sstevel@tonic-gate 
58110Sstevel@tonic-gate 	if (IN6_IS_ADDR_UNSPECIFIED(addr)) {
58120Sstevel@tonic-gate 		(void) strncpy(dst, "      *", dstlen);
58130Sstevel@tonic-gate 		dst[dstlen - 1] = 0;
58140Sstevel@tonic-gate 	} else {
58150Sstevel@tonic-gate 		(void) pr_addr6(addr, dst, dstlen);
58160Sstevel@tonic-gate 	}
58170Sstevel@tonic-gate 	/* How much room is left? */
58180Sstevel@tonic-gate 	cp = strchr(dst, '\0');
58190Sstevel@tonic-gate 	if (dst + dstlen + 1 > cp) {
58200Sstevel@tonic-gate 		*cp++ = '.';
58210Sstevel@tonic-gate 		dstlen -= (cp - dst);
58220Sstevel@tonic-gate 		dstlen--;
58230Sstevel@tonic-gate 		(void) portname(port, proto, cp, dstlen);
58240Sstevel@tonic-gate 	}
58250Sstevel@tonic-gate 	return (dst);
58260Sstevel@tonic-gate }
58270Sstevel@tonic-gate 
58280Sstevel@tonic-gate /*
58290Sstevel@tonic-gate  * Return the name of the network whose address is given. The address is
58300Sstevel@tonic-gate  * assumed to be that of a net or subnet, not a host.
58310Sstevel@tonic-gate  */
58320Sstevel@tonic-gate static char *
58330Sstevel@tonic-gate pr_net(uint_t addr, uint_t mask, char *dst, uint_t dstlen)
58340Sstevel@tonic-gate {
58350Sstevel@tonic-gate 	char		*cp = NULL;
58360Sstevel@tonic-gate 	struct netent	*np = NULL;
58370Sstevel@tonic-gate 	struct hostent	*hp = NULL;
58380Sstevel@tonic-gate 	uint_t		net;
58390Sstevel@tonic-gate 	int		subnetshift;
58400Sstevel@tonic-gate 	int		error_num;
58410Sstevel@tonic-gate 
58420Sstevel@tonic-gate 	if (addr == INADDR_ANY && mask == INADDR_ANY) {
58430Sstevel@tonic-gate 		(void) strncpy(dst, "default", dstlen);
58440Sstevel@tonic-gate 		dst[dstlen - 1] = 0;
58450Sstevel@tonic-gate 		return (dst);
58460Sstevel@tonic-gate 	}
58470Sstevel@tonic-gate 
58480Sstevel@tonic-gate 	if (!Nflag && addr) {
58490Sstevel@tonic-gate 		if (mask == 0) {
58500Sstevel@tonic-gate 			if (IN_CLASSA(addr)) {
58510Sstevel@tonic-gate 				mask = (uint_t)IN_CLASSA_NET;
58520Sstevel@tonic-gate 				subnetshift = 8;
58530Sstevel@tonic-gate 			} else if (IN_CLASSB(addr)) {
58540Sstevel@tonic-gate 				mask = (uint_t)IN_CLASSB_NET;
58550Sstevel@tonic-gate 				subnetshift = 8;
58560Sstevel@tonic-gate 			} else {
58570Sstevel@tonic-gate 				mask = (uint_t)IN_CLASSC_NET;
58580Sstevel@tonic-gate 				subnetshift = 4;
58590Sstevel@tonic-gate 			}
58600Sstevel@tonic-gate 			/*
58610Sstevel@tonic-gate 			 * If there are more bits than the standard mask
58620Sstevel@tonic-gate 			 * would suggest, subnets must be in use. Guess at
58630Sstevel@tonic-gate 			 * the subnet mask, assuming reasonable width subnet
58640Sstevel@tonic-gate 			 * fields.
58650Sstevel@tonic-gate 			 */
58660Sstevel@tonic-gate 			while (addr & ~mask)
58670Sstevel@tonic-gate 				/* compiler doesn't sign extend! */
58680Sstevel@tonic-gate 				mask = (mask | ((int)mask >> subnetshift));
58690Sstevel@tonic-gate 		}
58700Sstevel@tonic-gate 		net = addr & mask;
58710Sstevel@tonic-gate 		while ((mask & 1) == 0)
58720Sstevel@tonic-gate 			mask >>= 1, net >>= 1;
58730Sstevel@tonic-gate 		np = getnetbyaddr(net, AF_INET);
58740Sstevel@tonic-gate 		if (np && np->n_net == net)
58750Sstevel@tonic-gate 			cp = np->n_name;
58760Sstevel@tonic-gate 		else {
58770Sstevel@tonic-gate 			/*
58780Sstevel@tonic-gate 			 * Look for subnets in hosts map.
58790Sstevel@tonic-gate 			 */
58800Sstevel@tonic-gate 			hp = getipnodebyaddr((char *)&addr, sizeof (uint_t),
58810Sstevel@tonic-gate 			    AF_INET, &error_num);
58820Sstevel@tonic-gate 			if (hp)
58830Sstevel@tonic-gate 				cp = hp->h_name;
58840Sstevel@tonic-gate 		}
58850Sstevel@tonic-gate 	}
58860Sstevel@tonic-gate 	if (cp != NULL) {
58870Sstevel@tonic-gate 		(void) strncpy(dst, cp, dstlen);
58880Sstevel@tonic-gate 		dst[dstlen - 1] = 0;
58890Sstevel@tonic-gate 	} else {
58900Sstevel@tonic-gate 		(void) inet_ntop(AF_INET, (char *)&addr, dst, dstlen);
58910Sstevel@tonic-gate 	}
58920Sstevel@tonic-gate 	if (hp != NULL)
58930Sstevel@tonic-gate 		freehostent(hp);
58940Sstevel@tonic-gate 	return (dst);
58950Sstevel@tonic-gate }
58960Sstevel@tonic-gate 
58970Sstevel@tonic-gate /*
58980Sstevel@tonic-gate  * Return the name of the network whose address is given.
58990Sstevel@tonic-gate  * The address is assumed to be a host address.
59000Sstevel@tonic-gate  */
59010Sstevel@tonic-gate static char *
59020Sstevel@tonic-gate pr_netaddr(uint_t addr, uint_t mask, char *dst, uint_t dstlen)
59030Sstevel@tonic-gate {
59040Sstevel@tonic-gate 	char		*cp = NULL;
59050Sstevel@tonic-gate 	struct netent	*np = NULL;
59060Sstevel@tonic-gate 	struct hostent	*hp = NULL;
59070Sstevel@tonic-gate 	uint_t		net;
59080Sstevel@tonic-gate 	uint_t		netshifted;
59090Sstevel@tonic-gate 	int		subnetshift;
59100Sstevel@tonic-gate 	struct in_addr in;
59110Sstevel@tonic-gate 	int		error_num;
59120Sstevel@tonic-gate 	uint_t		nbo_addr = addr;	/* network byte order */
59130Sstevel@tonic-gate 
59140Sstevel@tonic-gate 	addr = ntohl(addr);
59150Sstevel@tonic-gate 	mask = ntohl(mask);
59160Sstevel@tonic-gate 	if (addr == INADDR_ANY && mask == INADDR_ANY) {
59170Sstevel@tonic-gate 		(void) strncpy(dst, "default", dstlen);
59180Sstevel@tonic-gate 		dst[dstlen - 1] = 0;
59190Sstevel@tonic-gate 		return (dst);
59200Sstevel@tonic-gate 	}
59210Sstevel@tonic-gate 
59220Sstevel@tonic-gate 	/* Figure out network portion of address (with host portion = 0) */
59230Sstevel@tonic-gate 	if (addr) {
59240Sstevel@tonic-gate 		/* Try figuring out mask if unknown (all 0s). */
59250Sstevel@tonic-gate 		if (mask == 0) {
59260Sstevel@tonic-gate 			if (IN_CLASSA(addr)) {
59270Sstevel@tonic-gate 				mask = (uint_t)IN_CLASSA_NET;
59280Sstevel@tonic-gate 				subnetshift = 8;
59290Sstevel@tonic-gate 			} else if (IN_CLASSB(addr)) {
59300Sstevel@tonic-gate 				mask = (uint_t)IN_CLASSB_NET;
59310Sstevel@tonic-gate 				subnetshift = 8;
59320Sstevel@tonic-gate 			} else {
59330Sstevel@tonic-gate 				mask = (uint_t)IN_CLASSC_NET;
59340Sstevel@tonic-gate 				subnetshift = 4;
59350Sstevel@tonic-gate 			}
59360Sstevel@tonic-gate 			/*
59370Sstevel@tonic-gate 			 * If there are more bits than the standard mask
59380Sstevel@tonic-gate 			 * would suggest, subnets must be in use. Guess at
59390Sstevel@tonic-gate 			 * the subnet mask, assuming reasonable width subnet
59400Sstevel@tonic-gate 			 * fields.
59410Sstevel@tonic-gate 			 */
59420Sstevel@tonic-gate 			while (addr & ~mask)
59430Sstevel@tonic-gate 				/* compiler doesn't sign extend! */
59440Sstevel@tonic-gate 				mask = (mask | ((int)mask >> subnetshift));
59450Sstevel@tonic-gate 		}
59460Sstevel@tonic-gate 		net = netshifted = addr & mask;
59470Sstevel@tonic-gate 		while ((mask & 1) == 0)
59480Sstevel@tonic-gate 			mask >>= 1, netshifted >>= 1;
59490Sstevel@tonic-gate 	}
59500Sstevel@tonic-gate 	else
59510Sstevel@tonic-gate 		net = netshifted = 0;
59520Sstevel@tonic-gate 
59530Sstevel@tonic-gate 	/* Try looking up name unless -n was specified. */
59540Sstevel@tonic-gate 	if (!Nflag) {
59550Sstevel@tonic-gate 		np = getnetbyaddr(netshifted, AF_INET);
59560Sstevel@tonic-gate 		if (np && np->n_net == netshifted)
59570Sstevel@tonic-gate 			cp = np->n_name;
59580Sstevel@tonic-gate 		else {
59590Sstevel@tonic-gate 			/*
59600Sstevel@tonic-gate 			 * Look for subnets in hosts map.
59610Sstevel@tonic-gate 			 */
59620Sstevel@tonic-gate 			hp = getipnodebyaddr((char *)&nbo_addr, sizeof (uint_t),
59630Sstevel@tonic-gate 			    AF_INET, &error_num);
59640Sstevel@tonic-gate 			if (hp)
59650Sstevel@tonic-gate 				cp = hp->h_name;
59660Sstevel@tonic-gate 		}
59670Sstevel@tonic-gate 
59680Sstevel@tonic-gate 		if (cp != NULL) {
59690Sstevel@tonic-gate 			(void) strncpy(dst, cp, dstlen);
59700Sstevel@tonic-gate 			dst[dstlen - 1] = 0;
59710Sstevel@tonic-gate 			if (hp != NULL)
59720Sstevel@tonic-gate 				freehostent(hp);
59730Sstevel@tonic-gate 			return (dst);
59740Sstevel@tonic-gate 		}
59750Sstevel@tonic-gate 		/*
59760Sstevel@tonic-gate 		 * No name found for net: fallthru and return in decimal
59770Sstevel@tonic-gate 		 * dot notation.
59780Sstevel@tonic-gate 		 */
59790Sstevel@tonic-gate 	}
59800Sstevel@tonic-gate 
59810Sstevel@tonic-gate 	in.s_addr = htonl(net);
59820Sstevel@tonic-gate 	(void) inet_ntop(AF_INET, (char *)&in, dst, dstlen);
59830Sstevel@tonic-gate 	if (hp != NULL)
59840Sstevel@tonic-gate 		freehostent(hp);
59850Sstevel@tonic-gate 	return (dst);
59860Sstevel@tonic-gate }
59870Sstevel@tonic-gate 
59880Sstevel@tonic-gate 
59890Sstevel@tonic-gate /*
59900Sstevel@tonic-gate  * Return the standard IPv4 classess host or network identifier.
59910Sstevel@tonic-gate  *
59920Sstevel@tonic-gate  *	Returns "default" for the default route.
59930Sstevel@tonic-gate  *	Returns "x.x.x.x" or host name if mask is 255.255.255.255.
59940Sstevel@tonic-gate  *	Returns "x.x.x.x/y" (y is bit count) if mask is contiguous.
59950Sstevel@tonic-gate  *	Otherwise, returns "x.x.x.x/m.m.m.m" (undesirable mask).
59960Sstevel@tonic-gate  *
59970Sstevel@tonic-gate  * Can also return "****" if inet_ntop fails -- insufficient dst space
59980Sstevel@tonic-gate  * available.  (Shouldn't happen otherwise.)
59990Sstevel@tonic-gate  */
60000Sstevel@tonic-gate static char *
60010Sstevel@tonic-gate pr_netclassless(ipaddr_t addr, ipaddr_t mask, char *dst, size_t dstlen)
60020Sstevel@tonic-gate {
60030Sstevel@tonic-gate 	struct hostent *hp;
60040Sstevel@tonic-gate 	int error_num;
60050Sstevel@tonic-gate 	struct in_addr in;
60060Sstevel@tonic-gate 	char *cp;
60070Sstevel@tonic-gate 	int slen;
60080Sstevel@tonic-gate 
60090Sstevel@tonic-gate 	if (addr == INADDR_ANY && mask == INADDR_ANY) {
60100Sstevel@tonic-gate 		(void) strlcpy(dst, "default", dstlen);
60110Sstevel@tonic-gate 		return (dst);
60120Sstevel@tonic-gate 	}
60130Sstevel@tonic-gate 	if (mask == IP_HOST_MASK && !Nflag &&
60140Sstevel@tonic-gate 	    (hp = getipnodebyaddr(&addr, sizeof (addr), AF_INET,
60150Sstevel@tonic-gate 		&error_num)) != NULL) {
60160Sstevel@tonic-gate 		(void) strlcpy(dst, hp->h_name, dstlen);
60170Sstevel@tonic-gate 		freehostent(hp);
60180Sstevel@tonic-gate 		return (dst);
60190Sstevel@tonic-gate 	}
60200Sstevel@tonic-gate 	in.s_addr = addr;
60210Sstevel@tonic-gate 	if (inet_ntop(AF_INET, &in, dst, dstlen) == NULL) {
60220Sstevel@tonic-gate 		(void) strlcpy(dst, "****", dstlen);
60230Sstevel@tonic-gate 		return (dst);
60240Sstevel@tonic-gate 	}
60250Sstevel@tonic-gate 	if (mask != IP_HOST_MASK) {
60260Sstevel@tonic-gate 		slen = strlen(dst);
60270Sstevel@tonic-gate 		cp = dst + slen;
60280Sstevel@tonic-gate 		dstlen -= slen;
60290Sstevel@tonic-gate 		if (mask == 0) {
60300Sstevel@tonic-gate 			/* Illegal on non-zero addresses */
60310Sstevel@tonic-gate 			(void) strlcpy(cp, "/0", dstlen);
60320Sstevel@tonic-gate 		} else if ((mask | (mask - 1)) == IP_HOST_MASK) {
60330Sstevel@tonic-gate 			(void) snprintf(cp, dstlen, "/%d",
60340Sstevel@tonic-gate 			    IP_ABITS - ffs(mask) + 1);
60350Sstevel@tonic-gate 		} else {
60360Sstevel@tonic-gate 			/* Ungood; non-contiguous mask */
60370Sstevel@tonic-gate 			(void) pr_mask(mask, cp, dstlen);
60380Sstevel@tonic-gate 		}
60390Sstevel@tonic-gate 	}
60400Sstevel@tonic-gate 	return (dst);
60410Sstevel@tonic-gate }
60420Sstevel@tonic-gate 
60430Sstevel@tonic-gate /*
60440Sstevel@tonic-gate  * Return the filter mode as a string:
60450Sstevel@tonic-gate  *	1 => "INCLUDE"
60460Sstevel@tonic-gate  *	2 => "EXCLUDE"
60470Sstevel@tonic-gate  *	otherwise "<unknown>"
60480Sstevel@tonic-gate  */
60490Sstevel@tonic-gate static char *
60500Sstevel@tonic-gate fmodestr(uint_t fmode)
60510Sstevel@tonic-gate {
60520Sstevel@tonic-gate 	switch (fmode) {
60530Sstevel@tonic-gate 	case 1:
60540Sstevel@tonic-gate 		return ("INCLUDE");
60550Sstevel@tonic-gate 	case 2:
60560Sstevel@tonic-gate 		return ("EXCLUDE");
60570Sstevel@tonic-gate 	default:
60580Sstevel@tonic-gate 		return ("<unknown>");
60590Sstevel@tonic-gate 	}
60600Sstevel@tonic-gate }
60610Sstevel@tonic-gate 
60621676Sjpk #define	MAX_STRING_SIZE	256
60631676Sjpk 
60641676Sjpk static const char *
60651676Sjpk pr_secattr(const sec_attr_list_t *attrs)
60661676Sjpk {
60671676Sjpk 	int i;
60681676Sjpk 	char buf[MAX_STRING_SIZE + 1], *cp;
60691676Sjpk 	static char *sbuf;
60701676Sjpk 	static size_t sbuf_len;
60711676Sjpk 	struct rtsa_s rtsa;
60721676Sjpk 	const sec_attr_list_t *aptr;
60731676Sjpk 
60741676Sjpk 	if (!RSECflag || attrs == NULL)
60751676Sjpk 		return ("");
60761676Sjpk 
60771676Sjpk 	for (aptr = attrs, i = 1; aptr != NULL; aptr = aptr->sal_next)
60781676Sjpk 		i += MAX_STRING_SIZE;
60791676Sjpk 	if (i > sbuf_len) {
60801676Sjpk 		cp = realloc(sbuf, i);
60811676Sjpk 		if (cp == NULL) {
60821676Sjpk 			perror("realloc security attribute buffer");
60831676Sjpk 			return ("");
60841676Sjpk 		}
60851676Sjpk 		sbuf_len = i;
60861676Sjpk 		sbuf = cp;
60871676Sjpk 	}
60881676Sjpk 
60891676Sjpk 	cp = sbuf;
60901676Sjpk 	while (attrs != NULL) {
60911676Sjpk 		const mib2_ipAttributeEntry_t *iae = attrs->sal_attr;
60921676Sjpk 
60931676Sjpk 		/* note: effectively hard-coded in rtsa_keyword */
60941676Sjpk 		rtsa.rtsa_mask = RTSA_CIPSO | RTSA_SLRANGE | RTSA_DOI;
60951676Sjpk 		rtsa.rtsa_slrange = iae->iae_slrange;
60961676Sjpk 		rtsa.rtsa_doi = iae->iae_doi;
60971676Sjpk 
60981676Sjpk 		(void) snprintf(cp, MAX_STRING_SIZE,
60991676Sjpk 		    "<%s>%s ", rtsa_to_str(&rtsa, buf, sizeof (buf)),
61001676Sjpk 		    attrs->sal_next == NULL ? "" : ",");
61011676Sjpk 		cp += strlen(cp);
61021676Sjpk 		attrs = attrs->sal_next;
61031676Sjpk 	}
61041676Sjpk 	*cp = '\0';
61051676Sjpk 
61061676Sjpk 	return (sbuf);
61071676Sjpk }
61081676Sjpk 
61090Sstevel@tonic-gate /*
61100Sstevel@tonic-gate  * Pretty print a port number. If the Nflag was
61110Sstevel@tonic-gate  * specified, use numbers instead of names.
61120Sstevel@tonic-gate  */
61130Sstevel@tonic-gate static char *
61140Sstevel@tonic-gate portname(uint_t port, char *proto, char *dst, uint_t dstlen)
61150Sstevel@tonic-gate {
61160Sstevel@tonic-gate 	struct servent *sp = NULL;
61170Sstevel@tonic-gate 
61180Sstevel@tonic-gate 	if (!Nflag && port)
61190Sstevel@tonic-gate 		sp = getservbyport(htons(port), proto);
61200Sstevel@tonic-gate 	if (sp || port == 0)
61210Sstevel@tonic-gate 		(void) snprintf(dst, dstlen, "%.*s", MAXHOSTNAMELEN,
61220Sstevel@tonic-gate 				sp ? sp->s_name : "*");
61230Sstevel@tonic-gate 	else
61240Sstevel@tonic-gate 		(void) snprintf(dst, dstlen, "%d", port);
61250Sstevel@tonic-gate 	dst[dstlen - 1] = 0;
61260Sstevel@tonic-gate 	return (dst);
61270Sstevel@tonic-gate }
61280Sstevel@tonic-gate 
61290Sstevel@tonic-gate /*PRINTFLIKE2*/
61300Sstevel@tonic-gate void
61310Sstevel@tonic-gate fail(int do_perror, char *message, ...)
61320Sstevel@tonic-gate {
61330Sstevel@tonic-gate 	va_list args;
61340Sstevel@tonic-gate 
61350Sstevel@tonic-gate 	va_start(args, message);
61360Sstevel@tonic-gate 	(void) fputs("netstat: ", stderr);
61370Sstevel@tonic-gate 	(void) vfprintf(stderr, message, args);
61380Sstevel@tonic-gate 	va_end(args);
61390Sstevel@tonic-gate 	if (do_perror)
61400Sstevel@tonic-gate 		(void) fprintf(stderr, ": %s", strerror(errno));
61410Sstevel@tonic-gate 	(void) fputc('\n', stderr);
61420Sstevel@tonic-gate 	exit(2);
61430Sstevel@tonic-gate }
61440Sstevel@tonic-gate 
61450Sstevel@tonic-gate /*
61460Sstevel@tonic-gate  * Return value of named statistic for given kstat_named kstat;
61470Sstevel@tonic-gate  * return 0LL if named statistic is not in list (use "ll" as a
61480Sstevel@tonic-gate  * type qualifier when printing 64-bit int's with printf() )
61490Sstevel@tonic-gate  */
61500Sstevel@tonic-gate static uint64_t
61510Sstevel@tonic-gate kstat_named_value(kstat_t *ksp, char *name)
61520Sstevel@tonic-gate {
61530Sstevel@tonic-gate 	kstat_named_t *knp;
61540Sstevel@tonic-gate 	uint64_t value;
61550Sstevel@tonic-gate 
61560Sstevel@tonic-gate 	if (ksp == NULL)
61570Sstevel@tonic-gate 		return (0LL);
61580Sstevel@tonic-gate 
61590Sstevel@tonic-gate 	knp = kstat_data_lookup(ksp, name);
61600Sstevel@tonic-gate 	if (knp == NULL)
61610Sstevel@tonic-gate 		return (0LL);
61620Sstevel@tonic-gate 
61630Sstevel@tonic-gate 	switch (knp->data_type) {
61640Sstevel@tonic-gate 	case KSTAT_DATA_INT32:
61650Sstevel@tonic-gate 	case KSTAT_DATA_UINT32:
61660Sstevel@tonic-gate 		value = (uint64_t)(knp->value.ui32);
61670Sstevel@tonic-gate 		break;
61680Sstevel@tonic-gate 	case KSTAT_DATA_INT64:
61690Sstevel@tonic-gate 	case KSTAT_DATA_UINT64:
61700Sstevel@tonic-gate 		value = knp->value.ui64;
61710Sstevel@tonic-gate 		break;
61720Sstevel@tonic-gate 	default:
61730Sstevel@tonic-gate 		value = 0LL;
61740Sstevel@tonic-gate 		break;
61750Sstevel@tonic-gate 	}
61760Sstevel@tonic-gate 
61770Sstevel@tonic-gate 	return (value);
61780Sstevel@tonic-gate }
61790Sstevel@tonic-gate 
61800Sstevel@tonic-gate kid_t
61810Sstevel@tonic-gate safe_kstat_read(kstat_ctl_t *kc, kstat_t *ksp, void *data)
61820Sstevel@tonic-gate {
61830Sstevel@tonic-gate 	kid_t kstat_chain_id = kstat_read(kc, ksp, data);
61840Sstevel@tonic-gate 
61850Sstevel@tonic-gate 	if (kstat_chain_id == -1)
61860Sstevel@tonic-gate 		fail(1, "kstat_read(%p, '%s') failed", (void *)kc,
61870Sstevel@tonic-gate 		    ksp->ks_name);
61880Sstevel@tonic-gate 	return (kstat_chain_id);
61890Sstevel@tonic-gate }
61900Sstevel@tonic-gate 
61910Sstevel@tonic-gate /*
61920Sstevel@tonic-gate  * Parse a list of IRE flag characters into a bit field.
61930Sstevel@tonic-gate  */
61940Sstevel@tonic-gate static uint_t
61950Sstevel@tonic-gate flag_bits(const char *arg)
61960Sstevel@tonic-gate {
61970Sstevel@tonic-gate 	const char *cp;
61980Sstevel@tonic-gate 	uint_t val;
61990Sstevel@tonic-gate 
62000Sstevel@tonic-gate 	if (*arg == '\0')
62010Sstevel@tonic-gate 		fatal(1, "missing flag list\n");
62020Sstevel@tonic-gate 
62030Sstevel@tonic-gate 	val = 0;
62040Sstevel@tonic-gate 	while (*arg != '\0') {
62050Sstevel@tonic-gate 		if ((cp = strchr(flag_list, *arg)) == NULL)
62060Sstevel@tonic-gate 			fatal(1, "%c: illegal flag\n", *arg);
62070Sstevel@tonic-gate 		val |= 1 << (cp - flag_list);
62080Sstevel@tonic-gate 		arg++;
62090Sstevel@tonic-gate 	}
62100Sstevel@tonic-gate 	return (val);
62110Sstevel@tonic-gate }
62120Sstevel@tonic-gate 
62130Sstevel@tonic-gate /*
62140Sstevel@tonic-gate  * Handle -f argument.  Validate input format, sort by keyword, and
62150Sstevel@tonic-gate  * save off digested results.
62160Sstevel@tonic-gate  */
62170Sstevel@tonic-gate static void
62180Sstevel@tonic-gate process_filter(char *arg)
62190Sstevel@tonic-gate {
62200Sstevel@tonic-gate 	int idx;
62210Sstevel@tonic-gate 	int klen = 0;
62220Sstevel@tonic-gate 	char *cp, *cp2;
62230Sstevel@tonic-gate 	int val;
62240Sstevel@tonic-gate 	filter_t *newf;
62250Sstevel@tonic-gate 	struct hostent *hp;
62260Sstevel@tonic-gate 	int error_num;
62270Sstevel@tonic-gate 	uint8_t *ucp;
62280Sstevel@tonic-gate 	int maxv;
62290Sstevel@tonic-gate 
62300Sstevel@tonic-gate 	/* Look up the keyword first */
62310Sstevel@tonic-gate 	if (strchr(arg, ':') == NULL) {
62320Sstevel@tonic-gate 		idx = FK_AF;
62330Sstevel@tonic-gate 	} else {
62340Sstevel@tonic-gate 		for (idx = 0; idx < NFILTERKEYS; idx++) {
62350Sstevel@tonic-gate 			klen = strlen(filter_keys[idx]);
62360Sstevel@tonic-gate 			if (strncmp(filter_keys[idx], arg, klen) == 0 &&
62370Sstevel@tonic-gate 			    arg[klen] == ':')
62380Sstevel@tonic-gate 				break;
62390Sstevel@tonic-gate 		}
62400Sstevel@tonic-gate 		if (idx >= NFILTERKEYS)
62410Sstevel@tonic-gate 			fatal(1, "%s: unknown filter keyword\n", arg);
62420Sstevel@tonic-gate 
62430Sstevel@tonic-gate 		/* Advance past keyword and separator. */
62440Sstevel@tonic-gate 		arg += klen + 1;
62450Sstevel@tonic-gate 	}
62460Sstevel@tonic-gate 
62470Sstevel@tonic-gate 	if ((newf = malloc(sizeof (*newf))) == NULL) {
62480Sstevel@tonic-gate 		perror("filter");
62490Sstevel@tonic-gate 		exit(1);
62500Sstevel@tonic-gate 	}
62510Sstevel@tonic-gate 	switch (idx) {
62520Sstevel@tonic-gate 	case FK_AF:
62530Sstevel@tonic-gate 		if (strcmp(arg, "inet") == 0) {
62540Sstevel@tonic-gate 			newf->u.f_family = AF_INET;
62550Sstevel@tonic-gate 		} else if (strcmp(arg, "inet6") == 0) {
62560Sstevel@tonic-gate 			newf->u.f_family = AF_INET6;
62570Sstevel@tonic-gate 		} else if (strcmp(arg, "unix") == 0) {
62580Sstevel@tonic-gate 			newf->u.f_family = AF_UNIX;
62590Sstevel@tonic-gate 		} else {
62600Sstevel@tonic-gate 			newf->u.f_family = strtol(arg, &cp, 0);
62610Sstevel@tonic-gate 			if (arg == cp || *cp != '\0')
62620Sstevel@tonic-gate 				fatal(1, "%s: unknown address family.\n", arg);
62630Sstevel@tonic-gate 		}
62640Sstevel@tonic-gate 		break;
62650Sstevel@tonic-gate 
62660Sstevel@tonic-gate 	case FK_INIF:
62670Sstevel@tonic-gate 	case FK_OUTIF:
62680Sstevel@tonic-gate 		if (strcmp(arg, "none") == 0) {
62690Sstevel@tonic-gate 			newf->u.f_ifname = NULL;
62700Sstevel@tonic-gate 			break;
62710Sstevel@tonic-gate 		}
62720Sstevel@tonic-gate 		if (strcmp(arg, "any") == 0) {
62730Sstevel@tonic-gate 			newf->u.f_ifname = "";
62740Sstevel@tonic-gate 			break;
62750Sstevel@tonic-gate 		}
62760Sstevel@tonic-gate 		val = strtol(arg, &cp, 0);
62770Sstevel@tonic-gate 		if (val <= 0 || arg == cp || cp[0] != '\0') {
62780Sstevel@tonic-gate 			if ((val = if_nametoindex(arg)) == 0) {
62790Sstevel@tonic-gate 				perror(arg);
62800Sstevel@tonic-gate 				exit(1);
62810Sstevel@tonic-gate 			}
62820Sstevel@tonic-gate 		}
62830Sstevel@tonic-gate 		newf->u.f_ifname = arg;
62840Sstevel@tonic-gate 		break;
62850Sstevel@tonic-gate 
62860Sstevel@tonic-gate 	case FK_SRC:
62870Sstevel@tonic-gate 	case FK_DST:
62880Sstevel@tonic-gate 		V4MASK_TO_V6(IP_HOST_MASK, newf->u.a.f_mask);
62890Sstevel@tonic-gate 		if (strcmp(arg, "any") == 0) {
62900Sstevel@tonic-gate 			/* Special semantics; any address *but* zero */
62910Sstevel@tonic-gate 			newf->u.a.f_address = NULL;
62920Sstevel@tonic-gate 			(void) memset(&newf->u.a.f_mask, 0,
62930Sstevel@tonic-gate 			    sizeof (newf->u.a.f_mask));
62940Sstevel@tonic-gate 			break;
62950Sstevel@tonic-gate 		}
62960Sstevel@tonic-gate 		if (strcmp(arg, "none") == 0) {
62970Sstevel@tonic-gate 			newf->u.a.f_address = NULL;
62980Sstevel@tonic-gate 			break;
62990Sstevel@tonic-gate 		}
63000Sstevel@tonic-gate 		if ((cp = strrchr(arg, '/')) != NULL)
63010Sstevel@tonic-gate 			*cp++ = '\0';
63020Sstevel@tonic-gate 		hp = getipnodebyname(arg, AF_INET6, AI_V4MAPPED|AI_ALL,
63030Sstevel@tonic-gate 		    &error_num);
63040Sstevel@tonic-gate 		if (hp == NULL)
63050Sstevel@tonic-gate 			fatal(1, "%s: invalid or unknown host address\n", arg);
63060Sstevel@tonic-gate 		newf->u.a.f_address = hp;
63070Sstevel@tonic-gate 		if (cp == NULL) {
63080Sstevel@tonic-gate 			V4MASK_TO_V6(IP_HOST_MASK, newf->u.a.f_mask);
63090Sstevel@tonic-gate 		} else {
63100Sstevel@tonic-gate 			val = strtol(cp, &cp2, 0);
63110Sstevel@tonic-gate 			if (cp != cp2 && cp2[0] == '\0') {
63120Sstevel@tonic-gate 				/*
63130Sstevel@tonic-gate 				 * If decode as "/n" works, then translate
63140Sstevel@tonic-gate 				 * into a mask.
63150Sstevel@tonic-gate 				 */
63160Sstevel@tonic-gate 				if (hp->h_addr_list[0] != NULL &&
63170Sstevel@tonic-gate 				    /* LINTED: (note 1) */
63180Sstevel@tonic-gate 				    IN6_IS_ADDR_V4MAPPED((in6_addr_t
63190Sstevel@tonic-gate 					*)hp->h_addr_list[0])) {
63200Sstevel@tonic-gate 					maxv = IP_ABITS;
63210Sstevel@tonic-gate 				} else {
63220Sstevel@tonic-gate 					maxv = IPV6_ABITS;
63230Sstevel@tonic-gate 				}
63240Sstevel@tonic-gate 				if (val < 0 || val >= maxv)
63250Sstevel@tonic-gate 					fatal(1, "%d: not in range 0 to %d\n",
63260Sstevel@tonic-gate 					    val, maxv - 1);
63270Sstevel@tonic-gate 				if (maxv == IP_ABITS)
63280Sstevel@tonic-gate 					val += IPV6_ABITS - IP_ABITS;
63290Sstevel@tonic-gate 				ucp = newf->u.a.f_mask.s6_addr;
63300Sstevel@tonic-gate 				while (val >= 8)
63310Sstevel@tonic-gate 					*ucp++ = 0xff, val -= 8;
63320Sstevel@tonic-gate 				*ucp++ = (0xff << (8 - val)) & 0xff;
63330Sstevel@tonic-gate 				while (ucp < newf->u.a.f_mask.s6_addr +
63340Sstevel@tonic-gate 				    sizeof (newf->u.a.f_mask.s6_addr))
63350Sstevel@tonic-gate 					*ucp++ = 0;
63360Sstevel@tonic-gate 				/* Otherwise, try as numeric address */
63370Sstevel@tonic-gate 			} else if (inet_pton(AF_INET6,
63380Sstevel@tonic-gate 			    cp, &newf->u.a.f_mask) <= 0) {
63390Sstevel@tonic-gate 				fatal(1, "%s: illegal mask format\n", cp);
63400Sstevel@tonic-gate 			}
63410Sstevel@tonic-gate 		}
63420Sstevel@tonic-gate 		break;
63430Sstevel@tonic-gate 
63440Sstevel@tonic-gate 	case FK_FLAGS:
63450Sstevel@tonic-gate 		if (*arg == '+') {
63460Sstevel@tonic-gate 			newf->u.f.f_flagset = flag_bits(arg + 1);
63470Sstevel@tonic-gate 			newf->u.f.f_flagclear = 0;
63480Sstevel@tonic-gate 		} else if (*arg == '-') {
63490Sstevel@tonic-gate 			newf->u.f.f_flagset = 0;
63500Sstevel@tonic-gate 			newf->u.f.f_flagclear = flag_bits(arg + 1);
63510Sstevel@tonic-gate 		} else {
63520Sstevel@tonic-gate 			newf->u.f.f_flagset = flag_bits(arg);
63530Sstevel@tonic-gate 			newf->u.f.f_flagclear = ~newf->u.f.f_flagset;
63540Sstevel@tonic-gate 		}
63550Sstevel@tonic-gate 		break;
63560Sstevel@tonic-gate 
63570Sstevel@tonic-gate 	default:
63580Sstevel@tonic-gate 		assert(0);
63590Sstevel@tonic-gate 	}
63600Sstevel@tonic-gate 	newf->f_next = filters[idx];
63610Sstevel@tonic-gate 	filters[idx] = newf;
63620Sstevel@tonic-gate }
63630Sstevel@tonic-gate 
63640Sstevel@tonic-gate /* Determine if user wants this address family printed. */
63650Sstevel@tonic-gate static boolean_t
63660Sstevel@tonic-gate family_selected(int family)
63670Sstevel@tonic-gate {
63680Sstevel@tonic-gate 	const filter_t *fp;
63690Sstevel@tonic-gate 
63700Sstevel@tonic-gate 	if (v4compat && family == AF_INET6)
63710Sstevel@tonic-gate 		return (B_FALSE);
63720Sstevel@tonic-gate 	if ((fp = filters[FK_AF]) == NULL)
63730Sstevel@tonic-gate 		return (B_TRUE);
63740Sstevel@tonic-gate 	while (fp != NULL) {
63750Sstevel@tonic-gate 		if (fp->u.f_family == family)
63760Sstevel@tonic-gate 			return (B_TRUE);
63770Sstevel@tonic-gate 		fp = fp->f_next;
63780Sstevel@tonic-gate 	}
63790Sstevel@tonic-gate 	return (B_FALSE);
63800Sstevel@tonic-gate }
63810Sstevel@tonic-gate 
63820Sstevel@tonic-gate /*
63830Sstevel@tonic-gate  * print the usage line
63840Sstevel@tonic-gate  */
63850Sstevel@tonic-gate static void
63860Sstevel@tonic-gate usage(char *cmdname)
63870Sstevel@tonic-gate {
63880Sstevel@tonic-gate 	(void) fprintf(stderr, "usage: %s [-anv] [-f address_family]\n",
63890Sstevel@tonic-gate 	    cmdname);
63900Sstevel@tonic-gate 	(void) fprintf(stderr, "       %s [-n] [-f address_family] "
63910Sstevel@tonic-gate 	    "[-P protocol] [-g | -p | -s [interval [count]]]\n", cmdname);
63920Sstevel@tonic-gate 	(void) fprintf(stderr, "       %s -m [-v] "
63930Sstevel@tonic-gate 	    "[interval [count]]\n", cmdname);
63940Sstevel@tonic-gate 	(void) fprintf(stderr, "       %s -i [-I interface] [-an] "
63950Sstevel@tonic-gate 	    "[-f address_family] [interval [count]]\n", cmdname);
63960Sstevel@tonic-gate 	(void) fprintf(stderr, "       %s -r [-anv] "
63970Sstevel@tonic-gate 	    "[-f address_family|filter]\n", cmdname);
63980Sstevel@tonic-gate 	(void) fprintf(stderr, "       %s -M [-ns] [-f address_family]\n",
63990Sstevel@tonic-gate 	    cmdname);
64000Sstevel@tonic-gate 	(void) fprintf(stderr, "       %s -D [-I interface] "
64010Sstevel@tonic-gate 	    "[-f address_family]\n", cmdname);
64020Sstevel@tonic-gate 	exit(EXIT_FAILURE);
64030Sstevel@tonic-gate }
64040Sstevel@tonic-gate 
64050Sstevel@tonic-gate /*
64060Sstevel@tonic-gate  * fatal: print error message to stderr and
64070Sstevel@tonic-gate  * call exit(errcode)
64080Sstevel@tonic-gate  */
64090Sstevel@tonic-gate /*PRINTFLIKE2*/
64100Sstevel@tonic-gate static void
64111676Sjpk fatal(int errcode, char *format, ...)
64121676Sjpk {
64130Sstevel@tonic-gate 	va_list argp;
64140Sstevel@tonic-gate 
64150Sstevel@tonic-gate 	if (format == NULL)
64160Sstevel@tonic-gate 		return;
64170Sstevel@tonic-gate 
64180Sstevel@tonic-gate 	va_start(argp, format);
64190Sstevel@tonic-gate 	(void) vfprintf(stderr, format, argp);
64200Sstevel@tonic-gate 	va_end(argp);
64210Sstevel@tonic-gate 
64220Sstevel@tonic-gate 	exit(errcode);
64230Sstevel@tonic-gate }
6424