17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
545916cd2Sjpk  * Common Development and Distribution License (the "License").
645916cd2Sjpk  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22e11c3f44Smeem  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * Copyright (c) 1990  Mentat Inc.
287c478bd9Sstevel@tonic-gate  * netstat.c 2.2, last change 9/9/91
297c478bd9Sstevel@tonic-gate  * MROUTING Revision 3.5
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate  * simple netstat based on snmp/mib-2 interface to the TCP/IP stack
347c478bd9Sstevel@tonic-gate  *
357c478bd9Sstevel@tonic-gate  * NOTES:
367c478bd9Sstevel@tonic-gate  * 1. A comment "LINTED: (note 1)" appears before certain lines where
377c478bd9Sstevel@tonic-gate  *    lint would have complained, "pointer cast may result in improper
387c478bd9Sstevel@tonic-gate  *    alignment". These are lines where lint had suspected potential
397c478bd9Sstevel@tonic-gate  *    improper alignment of a data structure; in each such situation
407c478bd9Sstevel@tonic-gate  *    we have relied on the kernel guaranteeing proper alignment.
417c478bd9Sstevel@tonic-gate  * 2. Some 'for' loops have been commented as "'for' loop 1", etc
427c478bd9Sstevel@tonic-gate  *    because they have 'continue' or 'break' statements in their
437c478bd9Sstevel@tonic-gate  *    bodies. 'continue' statements have been used inside some loops
447c478bd9Sstevel@tonic-gate  *    where avoiding them would have led to deep levels of indentation.
457c478bd9Sstevel@tonic-gate  *
467c478bd9Sstevel@tonic-gate  * TODO:
477c478bd9Sstevel@tonic-gate  *	Add ability to request subsets from kernel (with level = MIB2_IP;
487c478bd9Sstevel@tonic-gate  *	name = 0 meaning everything for compatibility)
497c478bd9Sstevel@tonic-gate  */
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate #include <stdio.h>
527c478bd9Sstevel@tonic-gate #include <stdlib.h>
537c478bd9Sstevel@tonic-gate #include <stdarg.h>
547c478bd9Sstevel@tonic-gate #include <unistd.h>
557c478bd9Sstevel@tonic-gate #include <strings.h>
567c478bd9Sstevel@tonic-gate #include <string.h>
577c478bd9Sstevel@tonic-gate #include <errno.h>
587c478bd9Sstevel@tonic-gate #include <ctype.h>
597c478bd9Sstevel@tonic-gate #include <kstat.h>
607c478bd9Sstevel@tonic-gate #include <assert.h>
61*26fd7700SKrishnendu Sadhukhan - Sun Microsystems #include <locale.h>
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate #include <sys/types.h>
647c478bd9Sstevel@tonic-gate #include <sys/stream.h>
657c478bd9Sstevel@tonic-gate #include <stropts.h>
667c478bd9Sstevel@tonic-gate #include <sys/strstat.h>
677c478bd9Sstevel@tonic-gate #include <sys/tihdr.h>
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate #include <sys/socket.h>
707c478bd9Sstevel@tonic-gate #include <sys/sockio.h>
717c478bd9Sstevel@tonic-gate #include <netinet/in.h>
727c478bd9Sstevel@tonic-gate #include <net/if.h>
737c478bd9Sstevel@tonic-gate #include <net/route.h>
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate #include <inet/mib2.h>
767c478bd9Sstevel@tonic-gate #include <inet/ip.h>
777c478bd9Sstevel@tonic-gate #include <inet/arp.h>
787c478bd9Sstevel@tonic-gate #include <inet/tcp.h>
797c478bd9Sstevel@tonic-gate #include <netinet/igmp_var.h>
807c478bd9Sstevel@tonic-gate #include <netinet/ip_mroute.h>
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
837c478bd9Sstevel@tonic-gate #include <netdb.h>
847c478bd9Sstevel@tonic-gate #include <fcntl.h>
857c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
867c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate #include <netinet/dhcp.h>
897c478bd9Sstevel@tonic-gate #include <dhcpagent_ipc.h>
907c478bd9Sstevel@tonic-gate #include <dhcpagent_util.h>
917c478bd9Sstevel@tonic-gate #include <compat.h>
927c478bd9Sstevel@tonic-gate 
9345916cd2Sjpk #include <libtsnet.h>
9445916cd2Sjpk #include <tsol/label.h>
9545916cd2Sjpk 
96*26fd7700SKrishnendu Sadhukhan - Sun Microsystems #include "statcommon.h"
97*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 
987c478bd9Sstevel@tonic-gate extern void	unixpr(kstat_ctl_t *kc);
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate #define	STR_EXPAND	4
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate #define	V4MASK_TO_V6(v4, v6)	((v6)._S6_un._S6_u32[0] = 0xfffffffful, \
1037c478bd9Sstevel@tonic-gate 				(v6)._S6_un._S6_u32[1] = 0xfffffffful, \
1047c478bd9Sstevel@tonic-gate 				(v6)._S6_un._S6_u32[2] = 0xfffffffful, \
1057c478bd9Sstevel@tonic-gate 				(v6)._S6_un._S6_u32[3] = (v4))
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate #define	IN6_IS_V4MASK(v6)	((v6)._S6_un._S6_u32[0] == 0xfffffffful && \
1087c478bd9Sstevel@tonic-gate 				(v6)._S6_un._S6_u32[1] == 0xfffffffful && \
1097c478bd9Sstevel@tonic-gate 				(v6)._S6_un._S6_u32[2] == 0xfffffffful)
1107c478bd9Sstevel@tonic-gate 
111d04ccbb3Scarlsonj /*
112d04ccbb3Scarlsonj  * This is used as a cushion in the buffer allocation directed by SIOCGLIFNUM.
113d04ccbb3Scarlsonj  * Because there's no locking between SIOCGLIFNUM and SIOCGLIFCONF, it's
114d04ccbb3Scarlsonj  * possible for an administrator to plumb new interfaces between those two
115d04ccbb3Scarlsonj  * calls, resulting in the failure of the latter.  This addition makes that
116d04ccbb3Scarlsonj  * less likely.
117d04ccbb3Scarlsonj  */
118d04ccbb3Scarlsonj #define	LIFN_GUARD_VALUE	10
119d04ccbb3Scarlsonj 
1207c478bd9Sstevel@tonic-gate typedef struct mib_item_s {
1217c478bd9Sstevel@tonic-gate 	struct mib_item_s	*next_item;
1227c478bd9Sstevel@tonic-gate 	int			group;
1237c478bd9Sstevel@tonic-gate 	int			mib_id;
1247c478bd9Sstevel@tonic-gate 	int			length;
1257c478bd9Sstevel@tonic-gate 	void			*valp;
1267c478bd9Sstevel@tonic-gate } mib_item_t;
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate struct	ifstat {
1297c478bd9Sstevel@tonic-gate 	uint64_t	ipackets;
1307c478bd9Sstevel@tonic-gate 	uint64_t	ierrors;
1317c478bd9Sstevel@tonic-gate 	uint64_t	opackets;
1327c478bd9Sstevel@tonic-gate 	uint64_t	oerrors;
1337c478bd9Sstevel@tonic-gate 	uint64_t	collisions;
1347c478bd9Sstevel@tonic-gate };
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate struct iflist {
1377c478bd9Sstevel@tonic-gate 	struct iflist	*next_if;
1387c478bd9Sstevel@tonic-gate 	char		ifname[LIFNAMSIZ];
1397c478bd9Sstevel@tonic-gate 	struct ifstat	tot;
1407c478bd9Sstevel@tonic-gate };
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate static	mib_item_t	*mibget(int sd);
1437c478bd9Sstevel@tonic-gate static	void		mibfree(mib_item_t *firstitem);
1447c478bd9Sstevel@tonic-gate static	int		mibopen(void);
1457c478bd9Sstevel@tonic-gate static void		mib_get_constants(mib_item_t *item);
1467c478bd9Sstevel@tonic-gate static mib_item_t	*mib_item_dup(mib_item_t *item);
1477c478bd9Sstevel@tonic-gate static mib_item_t	*mib_item_diff(mib_item_t *item1,
1487c478bd9Sstevel@tonic-gate     mib_item_t *item2);
1497c478bd9Sstevel@tonic-gate static void		mib_item_destroy(mib_item_t **item);
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate static boolean_t	octetstrmatch(const Octet_t *a, const Octet_t *b);
15245916cd2Sjpk static char		*octetstr(const Octet_t *op, int code,
1537c478bd9Sstevel@tonic-gate 			    char *dst, uint_t dstlen);
1547c478bd9Sstevel@tonic-gate static char		*pr_addr(uint_t addr,
1557c478bd9Sstevel@tonic-gate 			    char *dst, uint_t dstlen);
1567c478bd9Sstevel@tonic-gate static char		*pr_addrnz(ipaddr_t addr, char *dst, uint_t dstlen);
1577c478bd9Sstevel@tonic-gate static char		*pr_addr6(const in6_addr_t *addr,
1587c478bd9Sstevel@tonic-gate 			    char *dst, uint_t dstlen);
1597c478bd9Sstevel@tonic-gate static char		*pr_mask(uint_t addr,
1607c478bd9Sstevel@tonic-gate 			    char *dst, uint_t dstlen);
16145916cd2Sjpk static char		*pr_prefix6(const struct in6_addr *addr,
16245916cd2Sjpk 			    uint_t prefixlen, char *dst, uint_t dstlen);
1637c478bd9Sstevel@tonic-gate static char		*pr_ap(uint_t addr, uint_t port,
1647c478bd9Sstevel@tonic-gate 			    char *proto, char *dst, uint_t dstlen);
1657c478bd9Sstevel@tonic-gate static char		*pr_ap6(const in6_addr_t *addr, uint_t port,
1667c478bd9Sstevel@tonic-gate 			    char *proto, char *dst, uint_t dstlen);
1677c478bd9Sstevel@tonic-gate static char		*pr_net(uint_t addr, uint_t mask,
1687c478bd9Sstevel@tonic-gate 			    char *dst, uint_t dstlen);
1697c478bd9Sstevel@tonic-gate static char		*pr_netaddr(uint_t addr, uint_t mask,
1707c478bd9Sstevel@tonic-gate 			    char *dst, uint_t dstlen);
1717c478bd9Sstevel@tonic-gate static char		*fmodestr(uint_t fmode);
1727c478bd9Sstevel@tonic-gate static char		*portname(uint_t port, char *proto,
1737c478bd9Sstevel@tonic-gate 			    char *dst, uint_t dstlen);
1747c478bd9Sstevel@tonic-gate 
17545916cd2Sjpk static const char	*mitcp_state(int code,
17645916cd2Sjpk 			    const mib2_transportMLPEntry_t *attr);
17745916cd2Sjpk static const char	*miudp_state(int code,
17845916cd2Sjpk 			    const mib2_transportMLPEntry_t *attr);
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate static void		stat_report(mib_item_t *item);
1817c478bd9Sstevel@tonic-gate static void		mrt_stat_report(mib_item_t *item);
1827c478bd9Sstevel@tonic-gate static void		arp_report(mib_item_t *item);
1837c478bd9Sstevel@tonic-gate static void		ndp_report(mib_item_t *item);
1847c478bd9Sstevel@tonic-gate static void		mrt_report(mib_item_t *item);
1857c478bd9Sstevel@tonic-gate static void		if_stat_total(struct ifstat *oldstats,
1867c478bd9Sstevel@tonic-gate 			    struct ifstat *newstats, struct ifstat *sumstats);
1877c478bd9Sstevel@tonic-gate static void		if_report(mib_item_t *item, char *ifname,
1887c478bd9Sstevel@tonic-gate 			    int Iflag_only, boolean_t once_only);
1897c478bd9Sstevel@tonic-gate static void		if_report_ip4(mib2_ipAddrEntry_t *ap,
1907c478bd9Sstevel@tonic-gate 			    char ifname[], char logintname[],
1917c478bd9Sstevel@tonic-gate 			    struct ifstat *statptr, boolean_t ksp_not_null);
1927c478bd9Sstevel@tonic-gate static void		if_report_ip6(mib2_ipv6AddrEntry_t *ap6,
1937c478bd9Sstevel@tonic-gate 			    char ifname[], char logintname[],
1947c478bd9Sstevel@tonic-gate 			    struct ifstat *statptr, boolean_t ksp_not_null);
19545916cd2Sjpk static void		ire_report(const mib_item_t *item);
19645916cd2Sjpk static void		tcp_report(const mib_item_t *item);
19745916cd2Sjpk static void		udp_report(const mib_item_t *item);
1987c478bd9Sstevel@tonic-gate static void		group_report(mib_item_t *item);
1997c478bd9Sstevel@tonic-gate static void		print_ip_stats(mib2_ip_t *ip);
2007c478bd9Sstevel@tonic-gate static void		print_icmp_stats(mib2_icmp_t *icmp);
2017c478bd9Sstevel@tonic-gate static void		print_ip6_stats(mib2_ipv6IfStatsEntry_t *ip6);
2027c478bd9Sstevel@tonic-gate static void		print_icmp6_stats(mib2_ipv6IfIcmpEntry_t *icmp6);
2037c478bd9Sstevel@tonic-gate static void		print_sctp_stats(mib2_sctp_t *tcp);
2047c478bd9Sstevel@tonic-gate static void		print_tcp_stats(mib2_tcp_t *tcp);
2057c478bd9Sstevel@tonic-gate static void		print_udp_stats(mib2_udp_t *udp);
2067c478bd9Sstevel@tonic-gate static void		print_rawip_stats(mib2_rawip_t *rawip);
2077c478bd9Sstevel@tonic-gate static void		print_igmp_stats(struct igmpstat *igps);
2087c478bd9Sstevel@tonic-gate static void		print_mrt_stats(struct mrtstat *mrts);
20945916cd2Sjpk static void		sctp_report(const mib_item_t *item);
2107c478bd9Sstevel@tonic-gate static void		sum_ip6_stats(mib2_ipv6IfStatsEntry_t *ip6,
2117c478bd9Sstevel@tonic-gate 			    mib2_ipv6IfStatsEntry_t *sum6);
2127c478bd9Sstevel@tonic-gate static void		sum_icmp6_stats(mib2_ipv6IfIcmpEntry_t *icmp6,
2137c478bd9Sstevel@tonic-gate 			    mib2_ipv6IfIcmpEntry_t *sum6);
2147c478bd9Sstevel@tonic-gate static void		m_report(void);
2157c478bd9Sstevel@tonic-gate static void		dhcp_report(char *);
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate static	uint64_t	kstat_named_value(kstat_t *, char *);
2187c478bd9Sstevel@tonic-gate static	kid_t		safe_kstat_read(kstat_ctl_t *, kstat_t *, void *);
2197c478bd9Sstevel@tonic-gate static int		isnum(char *);
2207c478bd9Sstevel@tonic-gate static char		*plural(int n);
2217c478bd9Sstevel@tonic-gate static char		*pluraly(int n);
2227c478bd9Sstevel@tonic-gate static char		*plurales(int n);
2237c478bd9Sstevel@tonic-gate static void		process_filter(char *arg);
224e11c3f44Smeem static char		*ifindex2str(uint_t, char *);
2257c478bd9Sstevel@tonic-gate static boolean_t	family_selected(int family);
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate static void		usage(char *);
2287c478bd9Sstevel@tonic-gate static void 		fatal(int errcode, char *str1, ...);
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate #define	PLURAL(n) plural((int)n)
2317c478bd9Sstevel@tonic-gate #define	PLURALY(n) pluraly((int)n)
2327c478bd9Sstevel@tonic-gate #define	PLURALES(n) plurales((int)n)
2337c478bd9Sstevel@tonic-gate #define	IFLAGMOD(flg, val1, val2)	if (flg == val1) flg = val2
2347c478bd9Sstevel@tonic-gate #define	MDIFF(diff, elem2, elem1, member)	(diff)->member = \
2357c478bd9Sstevel@tonic-gate 	(elem2)->member - (elem1)->member
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate static	boolean_t	Aflag = B_FALSE;	/* All sockets/ifs/rtng-tbls */
2397c478bd9Sstevel@tonic-gate static	boolean_t	Dflag = B_FALSE;	/* Debug Info */
2407c478bd9Sstevel@tonic-gate static	boolean_t	Iflag = B_FALSE;	/* IP Traffic Interfaces */
2417c478bd9Sstevel@tonic-gate static	boolean_t	Mflag = B_FALSE;	/* STREAMS Memory Statistics */
2427c478bd9Sstevel@tonic-gate static	boolean_t	Nflag = B_FALSE;	/* Numeric Network Addresses */
2437c478bd9Sstevel@tonic-gate static	boolean_t	Rflag = B_FALSE;	/* Routing Tables */
24445916cd2Sjpk static	boolean_t	RSECflag = B_FALSE;	/* Security attributes */
2457c478bd9Sstevel@tonic-gate static	boolean_t	Sflag = B_FALSE;	/* Per-protocol Statistics */
2467c478bd9Sstevel@tonic-gate static	boolean_t	Vflag = B_FALSE;	/* Verbose */
2477c478bd9Sstevel@tonic-gate static	boolean_t	Pflag = B_FALSE;	/* Net to Media Tables */
2487c478bd9Sstevel@tonic-gate static	boolean_t	Gflag = B_FALSE;	/* Multicast group membership */
2497c478bd9Sstevel@tonic-gate static	boolean_t	MMflag = B_FALSE;	/* Multicast routing table */
2507c478bd9Sstevel@tonic-gate static	boolean_t	DHCPflag = B_FALSE;	/* DHCP statistics */
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate static	int	v4compat = 0;	/* Compatible printing format for status */
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate static int	proto = IPPROTO_MAX;	/* all protocols */
2557c478bd9Sstevel@tonic-gate kstat_ctl_t	*kc = NULL;
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate /*
2587c478bd9Sstevel@tonic-gate  * Sizes of data structures extracted from the base mib.
2597c478bd9Sstevel@tonic-gate  * This allows the size of the tables entries to grow while preserving
2607c478bd9Sstevel@tonic-gate  * binary compatibility.
2617c478bd9Sstevel@tonic-gate  */
2627c478bd9Sstevel@tonic-gate static int ipAddrEntrySize;
2637c478bd9Sstevel@tonic-gate static int ipRouteEntrySize;
2647c478bd9Sstevel@tonic-gate static int ipNetToMediaEntrySize;
2657c478bd9Sstevel@tonic-gate static int ipMemberEntrySize;
2667c478bd9Sstevel@tonic-gate static int ipGroupSourceEntrySize;
26745916cd2Sjpk static int ipRouteAttributeSize;
2687c478bd9Sstevel@tonic-gate static int vifctlSize;
2697c478bd9Sstevel@tonic-gate static int mfcctlSize;
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate static int ipv6IfStatsEntrySize;
2727c478bd9Sstevel@tonic-gate static int ipv6IfIcmpEntrySize;
2737c478bd9Sstevel@tonic-gate static int ipv6AddrEntrySize;
2747c478bd9Sstevel@tonic-gate static int ipv6RouteEntrySize;
2757c478bd9Sstevel@tonic-gate static int ipv6NetToMediaEntrySize;
2767c478bd9Sstevel@tonic-gate static int ipv6MemberEntrySize;
2777c478bd9Sstevel@tonic-gate static int ipv6GroupSourceEntrySize;
2787c478bd9Sstevel@tonic-gate 
27945916cd2Sjpk static int transportMLPSize;
2807c478bd9Sstevel@tonic-gate static int tcpConnEntrySize;
2817c478bd9Sstevel@tonic-gate static int tcp6ConnEntrySize;
2827c478bd9Sstevel@tonic-gate static int udpEntrySize;
2837c478bd9Sstevel@tonic-gate static int udp6EntrySize;
2847c478bd9Sstevel@tonic-gate static int sctpEntrySize;
2857c478bd9Sstevel@tonic-gate static int sctpLocalEntrySize;
2867c478bd9Sstevel@tonic-gate static int sctpRemoteEntrySize;
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate #define	protocol_selected(p)	(proto == IPPROTO_MAX || proto == (p))
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate /* Machinery used for -f (filter) option */
2915c0b7edeSseb enum { FK_AF = 0, FK_OUTIF, FK_DST, FK_FLAGS, NFILTERKEYS };
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate static const char *filter_keys[NFILTERKEYS] = {
2945c0b7edeSseb 	"af", "outif", "dst", "flags"
2957c478bd9Sstevel@tonic-gate };
2967c478bd9Sstevel@tonic-gate 
2975f9878b0Sken Powell - Sun Microsystem static m_label_t *zone_security_label = NULL;
2985f9878b0Sken Powell - Sun Microsystem 
2997c478bd9Sstevel@tonic-gate /* Flags on routes */
3007c478bd9Sstevel@tonic-gate #define	FLF_A		0x00000001
3017c478bd9Sstevel@tonic-gate #define	FLF_B		0x00000002
3027c478bd9Sstevel@tonic-gate #define	FLF_D		0x00000004
3037c478bd9Sstevel@tonic-gate #define	FLF_G		0x00000008
3047c478bd9Sstevel@tonic-gate #define	FLF_H		0x00000010
3057c478bd9Sstevel@tonic-gate #define	FLF_L		0x00000020
3067c478bd9Sstevel@tonic-gate #define	FLF_U		0x00000040
3077c478bd9Sstevel@tonic-gate #define	FLF_M		0x00000080
3087c478bd9Sstevel@tonic-gate #define	FLF_S		0x00000100
3097c478bd9Sstevel@tonic-gate static const char flag_list[] = "ABDGHLUMS";
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate typedef struct filter_rule filter_t;
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate struct filter_rule {
3147c478bd9Sstevel@tonic-gate 	filter_t *f_next;
3157c478bd9Sstevel@tonic-gate 	union {
3167c478bd9Sstevel@tonic-gate 		int f_family;
3177c478bd9Sstevel@tonic-gate 		const char *f_ifname;
3187c478bd9Sstevel@tonic-gate 		struct {
3197c478bd9Sstevel@tonic-gate 			struct hostent *f_address;
3207c478bd9Sstevel@tonic-gate 			in6_addr_t f_mask;
3217c478bd9Sstevel@tonic-gate 		} a;
3227c478bd9Sstevel@tonic-gate 		struct {
3237c478bd9Sstevel@tonic-gate 			uint_t f_flagset;
3247c478bd9Sstevel@tonic-gate 			uint_t f_flagclear;
3257c478bd9Sstevel@tonic-gate 		} f;
3267c478bd9Sstevel@tonic-gate 	} u;
3277c478bd9Sstevel@tonic-gate };
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate /*
3307c478bd9Sstevel@tonic-gate  * The user-specified filters are linked into lists separated by
3317c478bd9Sstevel@tonic-gate  * keyword (type of filter).  Thus, the matching algorithm is:
3327c478bd9Sstevel@tonic-gate  *	For each non-empty filter list
3337c478bd9Sstevel@tonic-gate  *		If no filters in the list match
3347c478bd9Sstevel@tonic-gate  *			then stop here; route doesn't match
3357c478bd9Sstevel@tonic-gate  *	If loop above completes, then route does match and will be
3367c478bd9Sstevel@tonic-gate  *	displayed.
3377c478bd9Sstevel@tonic-gate  */
3387c478bd9Sstevel@tonic-gate static filter_t *filters[NFILTERKEYS];
3397c478bd9Sstevel@tonic-gate 
340*26fd7700SKrishnendu Sadhukhan - Sun Microsystems static uint_t timestamp_fmt = NODATE;
341*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 
342*26fd7700SKrishnendu Sadhukhan - Sun Microsystems #if !defined(TEXT_DOMAIN)		/* Should be defined by cc -D */
343*26fd7700SKrishnendu Sadhukhan - Sun Microsystems #define	TEXT_DOMAIN "SYS_TEST"		/* Use this only if it isn't */
344*26fd7700SKrishnendu Sadhukhan - Sun Microsystems #endif
345*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 
3467c478bd9Sstevel@tonic-gate int
3477c478bd9Sstevel@tonic-gate main(int argc, char **argv)
3487c478bd9Sstevel@tonic-gate {
3497c478bd9Sstevel@tonic-gate 	char		*name;
3507c478bd9Sstevel@tonic-gate 	mib_item_t	*item = NULL;
3517c478bd9Sstevel@tonic-gate 	mib_item_t	*previtem = NULL;
3527c478bd9Sstevel@tonic-gate 	int		sd = -1;
3537c478bd9Sstevel@tonic-gate 	char	*ifname = NULL;
3547c478bd9Sstevel@tonic-gate 	int	interval = 0;	/* Single time by default */
3557c478bd9Sstevel@tonic-gate 	int	count = -1;	/* Forever */
3567c478bd9Sstevel@tonic-gate 	int	c;
3577c478bd9Sstevel@tonic-gate 	int	d;
3587c478bd9Sstevel@tonic-gate 	/*
3597c478bd9Sstevel@tonic-gate 	 * Possible values of 'Iflag_only':
3607c478bd9Sstevel@tonic-gate 	 * -1, no feature-flags;
3617c478bd9Sstevel@tonic-gate 	 *  0, IFlag and other feature-flags enabled
3627c478bd9Sstevel@tonic-gate 	 *  1, IFlag is the only feature-flag enabled
3637c478bd9Sstevel@tonic-gate 	 * : trinary variable, modified using IFLAGMOD()
3647c478bd9Sstevel@tonic-gate 	 */
3657c478bd9Sstevel@tonic-gate 	int Iflag_only = -1;
3667c478bd9Sstevel@tonic-gate 	boolean_t once_only = B_FALSE; /* '-i' with count > 1 */
3677c478bd9Sstevel@tonic-gate 	extern char	*optarg;
3687c478bd9Sstevel@tonic-gate 	extern int	optind;
3697c478bd9Sstevel@tonic-gate 	char *default_ip_str = NULL;
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 	name = argv[0];
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 	v4compat = get_compat_flag(&default_ip_str);
3747c478bd9Sstevel@tonic-gate 	if (v4compat == DEFAULT_PROT_BAD_VALUE)
3757c478bd9Sstevel@tonic-gate 		fatal(2, "%s: %s: Bad value for %s in %s\n", name,
3767c478bd9Sstevel@tonic-gate 		    default_ip_str, DEFAULT_IP, INET_DEFAULT_FILE);
3777c478bd9Sstevel@tonic-gate 	free(default_ip_str);
3787c478bd9Sstevel@tonic-gate 
379*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 	(void) setlocale(LC_ALL, "");
380*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 	(void) textdomain(TEXT_DOMAIN);
381*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 
382*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 	while ((c = getopt(argc, argv, "adimnrspMgvf:P:I:DRT:")) != -1) {
3837c478bd9Sstevel@tonic-gate 		switch ((char)c) {
3847c478bd9Sstevel@tonic-gate 		case 'a':		/* all connections */
3857c478bd9Sstevel@tonic-gate 			Aflag = B_TRUE;
3867c478bd9Sstevel@tonic-gate 			break;
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 		case 'd':		/* turn on debugging */
3897c478bd9Sstevel@tonic-gate 			Dflag = B_TRUE;
3907c478bd9Sstevel@tonic-gate 			break;
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 		case 'i':		/* interface (ill/ipif report) */
3937c478bd9Sstevel@tonic-gate 			Iflag = B_TRUE;
3947c478bd9Sstevel@tonic-gate 			IFLAGMOD(Iflag_only, -1, 1); /* '-i' exists */
3957c478bd9Sstevel@tonic-gate 			break;
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 		case 'm':		/* streams msg report */
3987c478bd9Sstevel@tonic-gate 			Mflag = B_TRUE;
3997c478bd9Sstevel@tonic-gate 			IFLAGMOD(Iflag_only, 1, 0); /* see macro def'n */
4007c478bd9Sstevel@tonic-gate 			break;
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 		case 'n':		/* numeric format */
4037c478bd9Sstevel@tonic-gate 			Nflag = B_TRUE;
4047c478bd9Sstevel@tonic-gate 			break;
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 		case 'r':		/* route tables */
4077c478bd9Sstevel@tonic-gate 			Rflag = B_TRUE;
4087c478bd9Sstevel@tonic-gate 			IFLAGMOD(Iflag_only, 1, 0); /* see macro def'n */
4097c478bd9Sstevel@tonic-gate 			break;
4107c478bd9Sstevel@tonic-gate 
41145916cd2Sjpk 		case 'R':		/* security attributes */
41245916cd2Sjpk 			RSECflag = B_TRUE;
41345916cd2Sjpk 			IFLAGMOD(Iflag_only, 1, 0); /* see macro def'n */
41445916cd2Sjpk 			break;
41545916cd2Sjpk 
4167c478bd9Sstevel@tonic-gate 		case 's':		/* per-protocol statistics */
4177c478bd9Sstevel@tonic-gate 			Sflag = B_TRUE;
4187c478bd9Sstevel@tonic-gate 			IFLAGMOD(Iflag_only, 1, 0); /* see macro def'n */
4197c478bd9Sstevel@tonic-gate 			break;
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 		case 'p':		/* arp/ndp table */
4227c478bd9Sstevel@tonic-gate 			Pflag = B_TRUE;
4237c478bd9Sstevel@tonic-gate 			IFLAGMOD(Iflag_only, 1, 0); /* see macro def'n */
4247c478bd9Sstevel@tonic-gate 			break;
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 		case 'M':		/* multicast routing tables */
4277c478bd9Sstevel@tonic-gate 			MMflag = B_TRUE;
4287c478bd9Sstevel@tonic-gate 			IFLAGMOD(Iflag_only, 1, 0); /* see macro def'n */
4297c478bd9Sstevel@tonic-gate 			break;
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 		case 'g':		/* multicast group membership */
4327c478bd9Sstevel@tonic-gate 			Gflag = B_TRUE;
4337c478bd9Sstevel@tonic-gate 			IFLAGMOD(Iflag_only, 1, 0); /* see macro def'n */
4347c478bd9Sstevel@tonic-gate 			break;
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 		case 'v':		/* verbose output format */
4377c478bd9Sstevel@tonic-gate 			Vflag = B_TRUE;
4387c478bd9Sstevel@tonic-gate 			IFLAGMOD(Iflag_only, 1, 0); /* see macro def'n */
4397c478bd9Sstevel@tonic-gate 			break;
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 		case 'f':
4427c478bd9Sstevel@tonic-gate 			process_filter(optarg);
4437c478bd9Sstevel@tonic-gate 			break;
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 		case 'P':
4467c478bd9Sstevel@tonic-gate 			if (strcmp(optarg, "ip") == 0) {
4477c478bd9Sstevel@tonic-gate 				proto = IPPROTO_IP;
4487c478bd9Sstevel@tonic-gate 			} else if (strcmp(optarg, "ipv6") == 0 ||
4497c478bd9Sstevel@tonic-gate 			    strcmp(optarg, "ip6") == 0) {
4507c478bd9Sstevel@tonic-gate 				v4compat = 0;	/* Overridden */
4517c478bd9Sstevel@tonic-gate 				proto = IPPROTO_IPV6;
4527c478bd9Sstevel@tonic-gate 			} else if (strcmp(optarg, "icmp") == 0) {
4537c478bd9Sstevel@tonic-gate 				proto = IPPROTO_ICMP;
4547c478bd9Sstevel@tonic-gate 			} else if (strcmp(optarg, "icmpv6") == 0 ||
4557c478bd9Sstevel@tonic-gate 			    strcmp(optarg, "icmp6") == 0) {
4567c478bd9Sstevel@tonic-gate 				v4compat = 0;	/* Overridden */
4577c478bd9Sstevel@tonic-gate 				proto = IPPROTO_ICMPV6;
4587c478bd9Sstevel@tonic-gate 			} else if (strcmp(optarg, "igmp") == 0) {
4597c478bd9Sstevel@tonic-gate 				proto = IPPROTO_IGMP;
4607c478bd9Sstevel@tonic-gate 			} else if (strcmp(optarg, "udp") == 0) {
4617c478bd9Sstevel@tonic-gate 				proto = IPPROTO_UDP;
4627c478bd9Sstevel@tonic-gate 			} else if (strcmp(optarg, "tcp") == 0) {
4637c478bd9Sstevel@tonic-gate 				proto = IPPROTO_TCP;
4647c478bd9Sstevel@tonic-gate 			} else if (strcmp(optarg, "sctp") == 0) {
4657c478bd9Sstevel@tonic-gate 				proto = IPPROTO_SCTP;
4667c478bd9Sstevel@tonic-gate 			} else if (strcmp(optarg, "raw") == 0 ||
4677c478bd9Sstevel@tonic-gate 			    strcmp(optarg, "rawip") == 0) {
4687c478bd9Sstevel@tonic-gate 				proto = IPPROTO_RAW;
4697c478bd9Sstevel@tonic-gate 			} else {
4707c478bd9Sstevel@tonic-gate 				fatal(1, "%s: unknown protocol.\n", optarg);
4717c478bd9Sstevel@tonic-gate 			}
4727c478bd9Sstevel@tonic-gate 			break;
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate 		case 'I':
4757c478bd9Sstevel@tonic-gate 			ifname = optarg;
4767c478bd9Sstevel@tonic-gate 			Iflag = B_TRUE;
4777c478bd9Sstevel@tonic-gate 			IFLAGMOD(Iflag_only, -1, 1); /* see macro def'n */
4787c478bd9Sstevel@tonic-gate 			break;
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 		case 'D':
4817c478bd9Sstevel@tonic-gate 			DHCPflag = B_TRUE;
4827c478bd9Sstevel@tonic-gate 			Iflag_only = 0;
4837c478bd9Sstevel@tonic-gate 			break;
4847c478bd9Sstevel@tonic-gate 
485*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 		case 'T':
486*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 			if (optarg) {
487*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 				if (*optarg == 'u')
488*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 					timestamp_fmt = UDATE;
489*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 				else if (*optarg == 'd')
490*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 					timestamp_fmt = DDATE;
491*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 				else
492*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 					usage(name);
493*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 			} else {
494*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 				usage(name);
495*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 			}
496*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 			break;
497*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 
4987c478bd9Sstevel@tonic-gate 		case '?':
4997c478bd9Sstevel@tonic-gate 		default:
5007c478bd9Sstevel@tonic-gate 			usage(name);
5017c478bd9Sstevel@tonic-gate 		}
5027c478bd9Sstevel@tonic-gate 	}
5037c478bd9Sstevel@tonic-gate 
50445916cd2Sjpk 	/*
50545916cd2Sjpk 	 * Make sure -R option is set only on a labeled system.
50645916cd2Sjpk 	 */
50745916cd2Sjpk 	if (RSECflag && !is_system_labeled()) {
50845916cd2Sjpk 		(void) fprintf(stderr, "-R set but labeling is not enabled\n");
50945916cd2Sjpk 		usage(name);
51045916cd2Sjpk 	}
51145916cd2Sjpk 
5127c478bd9Sstevel@tonic-gate 	/*
5137c478bd9Sstevel@tonic-gate 	 * Handle other arguments: find interval, count; the
5147c478bd9Sstevel@tonic-gate 	 * flags that accept 'interval' and 'count' are OR'd
5157c478bd9Sstevel@tonic-gate 	 * in the outermost 'if'; more flags may be added as
5167c478bd9Sstevel@tonic-gate 	 * required
5177c478bd9Sstevel@tonic-gate 	 */
5187c478bd9Sstevel@tonic-gate 	if (Iflag || Sflag || Mflag) {
5197c478bd9Sstevel@tonic-gate 		for (d = optind; d < argc; d++) {
5207c478bd9Sstevel@tonic-gate 			if (isnum(argv[d])) {
5217c478bd9Sstevel@tonic-gate 				interval = atoi(argv[d]);
5227c478bd9Sstevel@tonic-gate 				if (d + 1 < argc &&
5237c478bd9Sstevel@tonic-gate 				    isnum(argv[d + 1])) {
5247c478bd9Sstevel@tonic-gate 					count = atoi(argv[d + 1]);
5257c478bd9Sstevel@tonic-gate 					optind++;
5267c478bd9Sstevel@tonic-gate 				}
5277c478bd9Sstevel@tonic-gate 				optind++;
5287c478bd9Sstevel@tonic-gate 				if (interval == 0 || count == 0)
5297c478bd9Sstevel@tonic-gate 					usage(name);
5307c478bd9Sstevel@tonic-gate 				break;
5317c478bd9Sstevel@tonic-gate 			}
5327c478bd9Sstevel@tonic-gate 		}
5337c478bd9Sstevel@tonic-gate 	}
5347c478bd9Sstevel@tonic-gate 	if (optind < argc) {
5357c478bd9Sstevel@tonic-gate 		if (Iflag && isnum(argv[optind])) {
5367c478bd9Sstevel@tonic-gate 			count = atoi(argv[optind]);
5377c478bd9Sstevel@tonic-gate 			if (count == 0)
5387c478bd9Sstevel@tonic-gate 				usage(name);
5397c478bd9Sstevel@tonic-gate 			optind++;
5407c478bd9Sstevel@tonic-gate 		}
5417c478bd9Sstevel@tonic-gate 	}
5427c478bd9Sstevel@tonic-gate 	if (optind < argc) {
5437c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
5447c478bd9Sstevel@tonic-gate 		    "%s: extra arguments\n", name);
5457c478bd9Sstevel@tonic-gate 		usage(name);
5467c478bd9Sstevel@tonic-gate 	}
5477c478bd9Sstevel@tonic-gate 	if (interval)
5487c478bd9Sstevel@tonic-gate 		setbuf(stdout, NULL);
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate 	if (DHCPflag) {
5517c478bd9Sstevel@tonic-gate 		dhcp_report(Iflag ? ifname : NULL);
5527c478bd9Sstevel@tonic-gate 		exit(0);
5537c478bd9Sstevel@tonic-gate 	}
5547c478bd9Sstevel@tonic-gate 
5555f9878b0Sken Powell - Sun Microsystem 	/*
5565f9878b0Sken Powell - Sun Microsystem 	 * Get this process's security label if the -R switch is set.
5575f9878b0Sken Powell - Sun Microsystem 	 * We use this label as the current zone's security label.
5585f9878b0Sken Powell - Sun Microsystem 	 */
5595f9878b0Sken Powell - Sun Microsystem 	if (RSECflag) {
5605f9878b0Sken Powell - Sun Microsystem 		zone_security_label = m_label_alloc(MAC_LABEL);
5615f9878b0Sken Powell - Sun Microsystem 		if (zone_security_label == NULL)
5625f9878b0Sken Powell - Sun Microsystem 			fatal(errno, "m_label_alloc() failed");
5635f9878b0Sken Powell - Sun Microsystem 		if (getplabel(zone_security_label) < 0)
5645f9878b0Sken Powell - Sun Microsystem 			fatal(errno, "getplabel() failed");
5655f9878b0Sken Powell - Sun Microsystem 	}
5665f9878b0Sken Powell - Sun Microsystem 
5677c478bd9Sstevel@tonic-gate 	/* Get data structures: priming before iteration */
5687c478bd9Sstevel@tonic-gate 	if (family_selected(AF_INET) || family_selected(AF_INET6)) {
5697c478bd9Sstevel@tonic-gate 		sd = mibopen();
5707c478bd9Sstevel@tonic-gate 		if (sd == -1)
5717c478bd9Sstevel@tonic-gate 			fatal(1, "can't open mib stream\n");
5727c478bd9Sstevel@tonic-gate 		if ((item = mibget(sd)) == NULL) {
5737c478bd9Sstevel@tonic-gate 			(void) close(sd);
5747c478bd9Sstevel@tonic-gate 			fatal(1, "mibget() failed\n");
5757c478bd9Sstevel@tonic-gate 		}
5767c478bd9Sstevel@tonic-gate 		/* Extract constant sizes - need do once only */
5777c478bd9Sstevel@tonic-gate 		mib_get_constants(item);
5787c478bd9Sstevel@tonic-gate 	}
5797c478bd9Sstevel@tonic-gate 	if ((kc = kstat_open()) == NULL) {
5807c478bd9Sstevel@tonic-gate 		mibfree(item);
5817c478bd9Sstevel@tonic-gate 		(void) close(sd);
5827c478bd9Sstevel@tonic-gate 		fail(1, "kstat_open(): can't open /dev/kstat");
5837c478bd9Sstevel@tonic-gate 	}
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate 	if (interval <= 0) {
5867c478bd9Sstevel@tonic-gate 		count = 1;
5877c478bd9Sstevel@tonic-gate 		once_only = B_TRUE;
5887c478bd9Sstevel@tonic-gate 	}
5897c478bd9Sstevel@tonic-gate 	/* 'for' loop 1: */
5907c478bd9Sstevel@tonic-gate 	for (;;) {
5917c478bd9Sstevel@tonic-gate 		mib_item_t *curritem = NULL; /* only for -[M]s */
5927c478bd9Sstevel@tonic-gate 
593*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 		if (timestamp_fmt != NODATE)
594*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 			print_timestamp(timestamp_fmt);
595*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 
5967c478bd9Sstevel@tonic-gate 		/* netstat: AF_INET[6] behaviour */
5977c478bd9Sstevel@tonic-gate 		if (family_selected(AF_INET) || family_selected(AF_INET6)) {
5987c478bd9Sstevel@tonic-gate 			if (Sflag) {
5997c478bd9Sstevel@tonic-gate 				curritem = mib_item_diff(previtem, item);
6007c478bd9Sstevel@tonic-gate 				if (curritem == NULL)
6017c478bd9Sstevel@tonic-gate 					fatal(1, "can't process mib data, "
6027c478bd9Sstevel@tonic-gate 					    "out of memory\n");
6037c478bd9Sstevel@tonic-gate 				mib_item_destroy(&previtem);
6047c478bd9Sstevel@tonic-gate 			}
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 			if (!(Iflag || Rflag || Sflag || Mflag ||
6077c478bd9Sstevel@tonic-gate 			    MMflag || Pflag || Gflag || DHCPflag)) {
6087c478bd9Sstevel@tonic-gate 				if (protocol_selected(IPPROTO_UDP))
6097c478bd9Sstevel@tonic-gate 					udp_report(item);
6107c478bd9Sstevel@tonic-gate 				if (protocol_selected(IPPROTO_TCP))
6117c478bd9Sstevel@tonic-gate 					tcp_report(item);
6127c478bd9Sstevel@tonic-gate 				if (protocol_selected(IPPROTO_SCTP))
6137c478bd9Sstevel@tonic-gate 					sctp_report(item);
6147c478bd9Sstevel@tonic-gate 			}
6157c478bd9Sstevel@tonic-gate 			if (Iflag)
6167c478bd9Sstevel@tonic-gate 				if_report(item, ifname, Iflag_only, once_only);
6177c478bd9Sstevel@tonic-gate 			if (Mflag)
6187c478bd9Sstevel@tonic-gate 				m_report();
6197c478bd9Sstevel@tonic-gate 			if (Rflag)
6207c478bd9Sstevel@tonic-gate 				ire_report(item);
6217c478bd9Sstevel@tonic-gate 			if (Sflag && MMflag) {
6227c478bd9Sstevel@tonic-gate 				mrt_stat_report(curritem);
6237c478bd9Sstevel@tonic-gate 			} else {
6247c478bd9Sstevel@tonic-gate 				if (Sflag)
6257c478bd9Sstevel@tonic-gate 					stat_report(curritem);
6267c478bd9Sstevel@tonic-gate 				if (MMflag)
6277c478bd9Sstevel@tonic-gate 					mrt_report(item);
6287c478bd9Sstevel@tonic-gate 			}
6297c478bd9Sstevel@tonic-gate 			if (Gflag)
6307c478bd9Sstevel@tonic-gate 				group_report(item);
6317c478bd9Sstevel@tonic-gate 			if (Pflag) {
6327c478bd9Sstevel@tonic-gate 				if (family_selected(AF_INET))
6337c478bd9Sstevel@tonic-gate 					arp_report(item);
6347c478bd9Sstevel@tonic-gate 				if (family_selected(AF_INET6))
6357c478bd9Sstevel@tonic-gate 					ndp_report(item);
6367c478bd9Sstevel@tonic-gate 			}
6377c478bd9Sstevel@tonic-gate 			mib_item_destroy(&curritem);
6387c478bd9Sstevel@tonic-gate 		}
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate 		/* netstat: AF_UNIX behaviour */
6417c478bd9Sstevel@tonic-gate 		if (family_selected(AF_UNIX) &&
6427c478bd9Sstevel@tonic-gate 		    (!(Iflag || Rflag || Sflag || Mflag ||
6437c478bd9Sstevel@tonic-gate 		    MMflag || Pflag || Gflag)))
6447c478bd9Sstevel@tonic-gate 			unixpr(kc);
6457c478bd9Sstevel@tonic-gate 		(void) kstat_close(kc);
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 		/* iteration handling code */
6487c478bd9Sstevel@tonic-gate 		if (count > 0 && --count == 0)
6497c478bd9Sstevel@tonic-gate 			break;
6507c478bd9Sstevel@tonic-gate 		(void) sleep(interval);
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate 		/* re-populating of data structures */
6537c478bd9Sstevel@tonic-gate 		if (family_selected(AF_INET) || family_selected(AF_INET6)) {
6547c478bd9Sstevel@tonic-gate 			if (Sflag) {
6557c478bd9Sstevel@tonic-gate 				/* previtem is a cut-down list */
6567c478bd9Sstevel@tonic-gate 				previtem = mib_item_dup(item);
6577c478bd9Sstevel@tonic-gate 				if (previtem == NULL)
6587c478bd9Sstevel@tonic-gate 					fatal(1, "can't process mib data, "
6597c478bd9Sstevel@tonic-gate 					    "out of memory\n");
6607c478bd9Sstevel@tonic-gate 			}
6617c478bd9Sstevel@tonic-gate 			mibfree(item);
6627c478bd9Sstevel@tonic-gate 			(void) close(sd);
6637c478bd9Sstevel@tonic-gate 			if ((sd = mibopen()) == -1)
6647c478bd9Sstevel@tonic-gate 				fatal(1, "can't open mib stream anymore\n");
6657c478bd9Sstevel@tonic-gate 			if ((item = mibget(sd)) == NULL) {
6667c478bd9Sstevel@tonic-gate 				(void) close(sd);
6677c478bd9Sstevel@tonic-gate 				fatal(1, "mibget() failed\n");
6687c478bd9Sstevel@tonic-gate 			}
6697c478bd9Sstevel@tonic-gate 		}
6707c478bd9Sstevel@tonic-gate 		if ((kc = kstat_open()) == NULL)
6717c478bd9Sstevel@tonic-gate 			fail(1, "kstat_open(): can't open /dev/kstat");
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 	} /* 'for' loop 1 ends */
6747c478bd9Sstevel@tonic-gate 	mibfree(item);
6757c478bd9Sstevel@tonic-gate 	(void) close(sd);
6765f9878b0Sken Powell - Sun Microsystem 	if (zone_security_label != NULL)
6775f9878b0Sken Powell - Sun Microsystem 		m_label_free(zone_security_label);
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 	return (0);
6807c478bd9Sstevel@tonic-gate }
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate static int
6847c478bd9Sstevel@tonic-gate isnum(char *p)
6857c478bd9Sstevel@tonic-gate {
6867c478bd9Sstevel@tonic-gate 	int	len;
6877c478bd9Sstevel@tonic-gate 	int	i;
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 	len = strlen(p);
6907c478bd9Sstevel@tonic-gate 	for (i = 0; i < len; i++)
6917c478bd9Sstevel@tonic-gate 		if (!isdigit(p[i]))
6927c478bd9Sstevel@tonic-gate 			return (0);
6937c478bd9Sstevel@tonic-gate 	return (1);
6947c478bd9Sstevel@tonic-gate }
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate /* --------------------------------- MIBGET -------------------------------- */
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate static mib_item_t *
7007c478bd9Sstevel@tonic-gate mibget(int sd)
7017c478bd9Sstevel@tonic-gate {
7027c478bd9Sstevel@tonic-gate 	/*
7037c478bd9Sstevel@tonic-gate 	 * buf is an automatic for this function, so the
7047c478bd9Sstevel@tonic-gate 	 * compiler has complete control over its alignment;
7057c478bd9Sstevel@tonic-gate 	 * it is assumed this alignment is satisfactory for
7067c478bd9Sstevel@tonic-gate 	 * it to be casted to certain other struct pointers
7077c478bd9Sstevel@tonic-gate 	 * here, such as struct T_optmgmt_ack * .
7087c478bd9Sstevel@tonic-gate 	 */
7097c478bd9Sstevel@tonic-gate 	uintptr_t		buf[512 / sizeof (uintptr_t)];
7107c478bd9Sstevel@tonic-gate 	int			flags;
7117c478bd9Sstevel@tonic-gate 	int			i, j, getcode;
7127c478bd9Sstevel@tonic-gate 	struct strbuf		ctlbuf, databuf;
7137c478bd9Sstevel@tonic-gate 	struct T_optmgmt_req	*tor = (struct T_optmgmt_req *)buf;
7147c478bd9Sstevel@tonic-gate 	struct T_optmgmt_ack	*toa = (struct T_optmgmt_ack *)buf;
7157c478bd9Sstevel@tonic-gate 	struct T_error_ack	*tea = (struct T_error_ack *)buf;
7167c478bd9Sstevel@tonic-gate 	struct opthdr		*req;
7177c478bd9Sstevel@tonic-gate 	mib_item_t		*first_item = NULL;
7187c478bd9Sstevel@tonic-gate 	mib_item_t		*last_item  = NULL;
7197c478bd9Sstevel@tonic-gate 	mib_item_t		*temp;
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate 	tor->PRIM_type = T_SVR4_OPTMGMT_REQ;
7227c478bd9Sstevel@tonic-gate 	tor->OPT_offset = sizeof (struct T_optmgmt_req);
7237c478bd9Sstevel@tonic-gate 	tor->OPT_length = sizeof (struct opthdr);
7247c478bd9Sstevel@tonic-gate 	tor->MGMT_flags = T_CURRENT;
725e11c3f44Smeem 
726e11c3f44Smeem 
727e11c3f44Smeem 	/*
728e11c3f44Smeem 	 * Note: we use the special level value below so that IP will return
729e11c3f44Smeem 	 * us information concerning IRE_MARK_TESTHIDDEN routes.
730e11c3f44Smeem 	 */
7317c478bd9Sstevel@tonic-gate 	req = (struct opthdr *)&tor[1];
732e11c3f44Smeem 	req->level = EXPER_IP_AND_TESTHIDDEN;
7337c478bd9Sstevel@tonic-gate 	req->name  = 0;
7347c478bd9Sstevel@tonic-gate 	req->len   = 0;
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate 	ctlbuf.buf = (char *)buf;
7377c478bd9Sstevel@tonic-gate 	ctlbuf.len = tor->OPT_length + tor->OPT_offset;
7387c478bd9Sstevel@tonic-gate 	flags = 0;
7397c478bd9Sstevel@tonic-gate 	if (putmsg(sd, &ctlbuf, (struct strbuf *)0, flags) == -1) {
7407c478bd9Sstevel@tonic-gate 		perror("mibget: putmsg(ctl) failed");
7417c478bd9Sstevel@tonic-gate 		goto error_exit;
7427c478bd9Sstevel@tonic-gate 	}
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 	/*
7457c478bd9Sstevel@tonic-gate 	 * Each reply consists of a ctl part for one fixed structure
7467c478bd9Sstevel@tonic-gate 	 * or table, as defined in mib2.h.  The format is a T_OPTMGMT_ACK,
7477c478bd9Sstevel@tonic-gate 	 * containing an opthdr structure.  level/name identify the entry,
7487c478bd9Sstevel@tonic-gate 	 * len is the size of the data part of the message.
7497c478bd9Sstevel@tonic-gate 	 */
7507c478bd9Sstevel@tonic-gate 	req = (struct opthdr *)&toa[1];
7517c478bd9Sstevel@tonic-gate 	ctlbuf.maxlen = sizeof (buf);
7527c478bd9Sstevel@tonic-gate 	j = 1;
7537c478bd9Sstevel@tonic-gate 	for (;;) {
7547c478bd9Sstevel@tonic-gate 		flags = 0;
7557c478bd9Sstevel@tonic-gate 		getcode = getmsg(sd, &ctlbuf, (struct strbuf *)0, &flags);
7567c478bd9Sstevel@tonic-gate 		if (getcode == -1) {
7577c478bd9Sstevel@tonic-gate 			perror("mibget getmsg(ctl) failed");
7587c478bd9Sstevel@tonic-gate 			if (Dflag) {
7597c478bd9Sstevel@tonic-gate 				(void) fputs("#   level   name    len\n",
7607c478bd9Sstevel@tonic-gate 				    stderr);
7617c478bd9Sstevel@tonic-gate 				i = 0;
7627c478bd9Sstevel@tonic-gate 				for (last_item = first_item; last_item;
763e11c3f44Smeem 				    last_item = last_item->next_item)
7647c478bd9Sstevel@tonic-gate 					(void) printf("%d  %4d   %5d   %d\n",
7657c478bd9Sstevel@tonic-gate 					    ++i,
7667c478bd9Sstevel@tonic-gate 					    last_item->group,
7677c478bd9Sstevel@tonic-gate 					    last_item->mib_id,
7687c478bd9Sstevel@tonic-gate 					    last_item->length);
7697c478bd9Sstevel@tonic-gate 			}
7707c478bd9Sstevel@tonic-gate 			goto error_exit;
7717c478bd9Sstevel@tonic-gate 		}
7727c478bd9Sstevel@tonic-gate 		if (getcode == 0 &&
7737c478bd9Sstevel@tonic-gate 		    ctlbuf.len >= sizeof (struct T_optmgmt_ack) &&
7747c478bd9Sstevel@tonic-gate 		    toa->PRIM_type == T_OPTMGMT_ACK &&
7757c478bd9Sstevel@tonic-gate 		    toa->MGMT_flags == T_SUCCESS &&
7767c478bd9Sstevel@tonic-gate 		    req->len == 0) {
7777c478bd9Sstevel@tonic-gate 			if (Dflag)
7787c478bd9Sstevel@tonic-gate 				(void) printf("mibget getmsg() %d returned "
7797c478bd9Sstevel@tonic-gate 				    "EOD (level %ld, name %ld)\n",
7807c478bd9Sstevel@tonic-gate 				    j, req->level, req->name);
7817c478bd9Sstevel@tonic-gate 			return (first_item);		/* this is EOD msg */
7827c478bd9Sstevel@tonic-gate 		}
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 		if (ctlbuf.len >= sizeof (struct T_error_ack) &&
7857c478bd9Sstevel@tonic-gate 		    tea->PRIM_type == T_ERROR_ACK) {
7867c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
7877c478bd9Sstevel@tonic-gate 			    "mibget %d gives T_ERROR_ACK: TLI_error = 0x%lx, "
7887c478bd9Sstevel@tonic-gate 			    "UNIX_error = 0x%lx\n",
7897c478bd9Sstevel@tonic-gate 			    j, tea->TLI_error, tea->UNIX_error);
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate 			errno = (tea->TLI_error == TSYSERR) ?
7927c478bd9Sstevel@tonic-gate 			    tea->UNIX_error : EPROTO;
7937c478bd9Sstevel@tonic-gate 			goto error_exit;
7947c478bd9Sstevel@tonic-gate 		}
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate 		if (getcode != MOREDATA ||
7977c478bd9Sstevel@tonic-gate 		    ctlbuf.len < sizeof (struct T_optmgmt_ack) ||
7987c478bd9Sstevel@tonic-gate 		    toa->PRIM_type != T_OPTMGMT_ACK ||
7997c478bd9Sstevel@tonic-gate 		    toa->MGMT_flags != T_SUCCESS) {
8007c478bd9Sstevel@tonic-gate 			(void) printf("mibget getmsg(ctl) %d returned %d, "
8017c478bd9Sstevel@tonic-gate 			    "ctlbuf.len = %d, PRIM_type = %ld\n",
8027c478bd9Sstevel@tonic-gate 			    j, getcode, ctlbuf.len, toa->PRIM_type);
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate 			if (toa->PRIM_type == T_OPTMGMT_ACK)
8057c478bd9Sstevel@tonic-gate 				(void) printf("T_OPTMGMT_ACK: "
8067c478bd9Sstevel@tonic-gate 				    "MGMT_flags = 0x%lx, req->len = %ld\n",
8077c478bd9Sstevel@tonic-gate 				    toa->MGMT_flags, req->len);
8087c478bd9Sstevel@tonic-gate 			errno = ENOMSG;
8097c478bd9Sstevel@tonic-gate 			goto error_exit;
8107c478bd9Sstevel@tonic-gate 		}
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate 		temp = (mib_item_t *)malloc(sizeof (mib_item_t));
8137c478bd9Sstevel@tonic-gate 		if (temp == NULL) {
8147c478bd9Sstevel@tonic-gate 			perror("mibget malloc failed");
8157c478bd9Sstevel@tonic-gate 			goto error_exit;
8167c478bd9Sstevel@tonic-gate 		}
8177c478bd9Sstevel@tonic-gate 		if (last_item != NULL)
8187c478bd9Sstevel@tonic-gate 			last_item->next_item = temp;
8197c478bd9Sstevel@tonic-gate 		else
8207c478bd9Sstevel@tonic-gate 			first_item = temp;
8217c478bd9Sstevel@tonic-gate 		last_item = temp;
8227c478bd9Sstevel@tonic-gate 		last_item->next_item = NULL;
8237c478bd9Sstevel@tonic-gate 		last_item->group = req->level;
8247c478bd9Sstevel@tonic-gate 		last_item->mib_id = req->name;
8257c478bd9Sstevel@tonic-gate 		last_item->length = req->len;
8267c478bd9Sstevel@tonic-gate 		last_item->valp = malloc((int)req->len);
8277c478bd9Sstevel@tonic-gate 		if (last_item->valp == NULL)
8287c478bd9Sstevel@tonic-gate 			goto error_exit;
8297c478bd9Sstevel@tonic-gate 		if (Dflag)
8307c478bd9Sstevel@tonic-gate 			(void) printf("msg %d: group = %4d   mib_id = %5d"
8317c478bd9Sstevel@tonic-gate 			    "length = %d\n",
8327c478bd9Sstevel@tonic-gate 			    j, last_item->group, last_item->mib_id,
8337c478bd9Sstevel@tonic-gate 			    last_item->length);
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate 		databuf.maxlen = last_item->length;
8367c478bd9Sstevel@tonic-gate 		databuf.buf    = (char *)last_item->valp;
8377c478bd9Sstevel@tonic-gate 		databuf.len    = 0;
8387c478bd9Sstevel@tonic-gate 		flags = 0;
8397c478bd9Sstevel@tonic-gate 		getcode = getmsg(sd, (struct strbuf *)0, &databuf, &flags);
8407c478bd9Sstevel@tonic-gate 		if (getcode == -1) {
8417c478bd9Sstevel@tonic-gate 			perror("mibget getmsg(data) failed");
8427c478bd9Sstevel@tonic-gate 			goto error_exit;
8437c478bd9Sstevel@tonic-gate 		} else if (getcode != 0) {
8447c478bd9Sstevel@tonic-gate 			(void) printf("mibget getmsg(data) returned %d, "
8457c478bd9Sstevel@tonic-gate 			    "databuf.maxlen = %d, databuf.len = %d\n",
8467c478bd9Sstevel@tonic-gate 			    getcode, databuf.maxlen, databuf.len);
8477c478bd9Sstevel@tonic-gate 			goto error_exit;
8487c478bd9Sstevel@tonic-gate 		}
8497c478bd9Sstevel@tonic-gate 		j++;
8507c478bd9Sstevel@tonic-gate 	}
8517c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate error_exit:;
8547c478bd9Sstevel@tonic-gate 	mibfree(first_item);
8557c478bd9Sstevel@tonic-gate 	return (NULL);
8567c478bd9Sstevel@tonic-gate }
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate /*
8597c478bd9Sstevel@tonic-gate  * mibfree: frees a linked list of type (mib_item_t *)
8607c478bd9Sstevel@tonic-gate  * returned by mibget(); this is NOT THE SAME AS
8617c478bd9Sstevel@tonic-gate  * mib_item_destroy(), so should be used for objects
8627c478bd9Sstevel@tonic-gate  * returned by mibget() only
8637c478bd9Sstevel@tonic-gate  */
8647c478bd9Sstevel@tonic-gate static void
8657c478bd9Sstevel@tonic-gate mibfree(mib_item_t *firstitem)
8667c478bd9Sstevel@tonic-gate {
8677c478bd9Sstevel@tonic-gate 	mib_item_t *lastitem;
8687c478bd9Sstevel@tonic-gate 
8697c478bd9Sstevel@tonic-gate 	while (firstitem != NULL) {
8707c478bd9Sstevel@tonic-gate 		lastitem = firstitem;
8717c478bd9Sstevel@tonic-gate 		firstitem = firstitem->next_item;
8727c478bd9Sstevel@tonic-gate 		if (lastitem->valp != NULL)
8737c478bd9Sstevel@tonic-gate 			free(lastitem->valp);
8747c478bd9Sstevel@tonic-gate 		free(lastitem);
8757c478bd9Sstevel@tonic-gate 	}
8767c478bd9Sstevel@tonic-gate }
8777c478bd9Sstevel@tonic-gate 
8787c478bd9Sstevel@tonic-gate static int
8797c478bd9Sstevel@tonic-gate mibopen(void)
8807c478bd9Sstevel@tonic-gate {
8817c478bd9Sstevel@tonic-gate 	int	sd;
8827c478bd9Sstevel@tonic-gate 
8837c478bd9Sstevel@tonic-gate 	sd = open("/dev/arp", O_RDWR);
8847c478bd9Sstevel@tonic-gate 	if (sd == -1) {
8857c478bd9Sstevel@tonic-gate 		perror("arp open");
8867c478bd9Sstevel@tonic-gate 		return (-1);
8877c478bd9Sstevel@tonic-gate 	}
8887c478bd9Sstevel@tonic-gate 	if (ioctl(sd, I_PUSH, "tcp") == -1) {
8897c478bd9Sstevel@tonic-gate 		perror("tcp I_PUSH");
8907c478bd9Sstevel@tonic-gate 		(void) close(sd);
8917c478bd9Sstevel@tonic-gate 		return (-1);
8927c478bd9Sstevel@tonic-gate 	}
8937c478bd9Sstevel@tonic-gate 	if (ioctl(sd, I_PUSH, "udp") == -1) {
8947c478bd9Sstevel@tonic-gate 		perror("udp I_PUSH");
8957c478bd9Sstevel@tonic-gate 		(void) close(sd);
8967c478bd9Sstevel@tonic-gate 		return (-1);
8977c478bd9Sstevel@tonic-gate 	}
8987c478bd9Sstevel@tonic-gate 	if (ioctl(sd, I_PUSH, "icmp") == -1) {
8997c478bd9Sstevel@tonic-gate 		perror("icmp I_PUSH");
9007c478bd9Sstevel@tonic-gate 		(void) close(sd);
9017c478bd9Sstevel@tonic-gate 		return (-1);
9027c478bd9Sstevel@tonic-gate 	}
9037c478bd9Sstevel@tonic-gate 	return (sd);
9047c478bd9Sstevel@tonic-gate }
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate /*
9077c478bd9Sstevel@tonic-gate  * mib_item_dup: returns a clean mib_item_t * linked
9087c478bd9Sstevel@tonic-gate  * list, so that for every element item->mib_id is 0;
9097c478bd9Sstevel@tonic-gate  * to deallocate this linked list, use mib_item_destroy
9107c478bd9Sstevel@tonic-gate  */
9117c478bd9Sstevel@tonic-gate static mib_item_t *
9127c478bd9Sstevel@tonic-gate mib_item_dup(mib_item_t *item)
9137c478bd9Sstevel@tonic-gate {
9147c478bd9Sstevel@tonic-gate 	int	c = 0;
9157c478bd9Sstevel@tonic-gate 	mib_item_t *localp;
9167c478bd9Sstevel@tonic-gate 	mib_item_t *tempp;
9177c478bd9Sstevel@tonic-gate 
9187c478bd9Sstevel@tonic-gate 	for (tempp = item; tempp; tempp = tempp->next_item)
9197c478bd9Sstevel@tonic-gate 		if (tempp->mib_id == 0)
9207c478bd9Sstevel@tonic-gate 			c++;
9217c478bd9Sstevel@tonic-gate 	tempp = NULL;
9227c478bd9Sstevel@tonic-gate 
9237c478bd9Sstevel@tonic-gate 	localp = (mib_item_t *)malloc(c * sizeof (mib_item_t));
9247c478bd9Sstevel@tonic-gate 	if (localp == NULL)
9257c478bd9Sstevel@tonic-gate 		return (NULL);
9267c478bd9Sstevel@tonic-gate 	c = 0;
9277c478bd9Sstevel@tonic-gate 	for (; item; item = item->next_item) {
9287c478bd9Sstevel@tonic-gate 		if (item->mib_id == 0) {
9297c478bd9Sstevel@tonic-gate 			/* Replicate item in localp */
9307c478bd9Sstevel@tonic-gate 			(localp[c]).next_item = NULL;
9317c478bd9Sstevel@tonic-gate 			(localp[c]).group = item->group;
9327c478bd9Sstevel@tonic-gate 			(localp[c]).mib_id = item->mib_id;
9337c478bd9Sstevel@tonic-gate 			(localp[c]).length = item->length;
9347c478bd9Sstevel@tonic-gate 			(localp[c]).valp = (uintptr_t *)malloc(
9357c478bd9Sstevel@tonic-gate 			    item->length);
9367c478bd9Sstevel@tonic-gate 			if ((localp[c]).valp == NULL) {
9377c478bd9Sstevel@tonic-gate 				mib_item_destroy(&localp);
9387c478bd9Sstevel@tonic-gate 				return (NULL);
9397c478bd9Sstevel@tonic-gate 			}
9407c478bd9Sstevel@tonic-gate 			(void *) memcpy((localp[c]).valp,
9417c478bd9Sstevel@tonic-gate 			    item->valp,
9427c478bd9Sstevel@tonic-gate 			    item->length);
9437c478bd9Sstevel@tonic-gate 			tempp = &(localp[c]);
9447c478bd9Sstevel@tonic-gate 			if (c > 0)
9457c478bd9Sstevel@tonic-gate 				(localp[c - 1]).next_item = tempp;
9467c478bd9Sstevel@tonic-gate 			c++;
9477c478bd9Sstevel@tonic-gate 		}
9487c478bd9Sstevel@tonic-gate 	}
9497c478bd9Sstevel@tonic-gate 	return (localp);
9507c478bd9Sstevel@tonic-gate }
9517c478bd9Sstevel@tonic-gate 
9527c478bd9Sstevel@tonic-gate /*
9537c478bd9Sstevel@tonic-gate  * mib_item_diff: takes two (mib_item_t *) linked lists
9547c478bd9Sstevel@tonic-gate  * item1 and item2 and computes the difference between
9557c478bd9Sstevel@tonic-gate  * differentiable values in item2 against item1 for every
9567c478bd9Sstevel@tonic-gate  * given member of item2; returns an mib_item_t * linked
9577c478bd9Sstevel@tonic-gate  * list of diff's, or a copy of item2 if item1 is NULL;
9587c478bd9Sstevel@tonic-gate  * will return NULL if system out of memory; works only
9597c478bd9Sstevel@tonic-gate  * for item->mib_id == 0
9607c478bd9Sstevel@tonic-gate  */
9617c478bd9Sstevel@tonic-gate static mib_item_t *
9627c478bd9Sstevel@tonic-gate mib_item_diff(mib_item_t *item1, mib_item_t *item2) {
9637c478bd9Sstevel@tonic-gate 	int	nitems	= 0; /* no. of items in item2 */
9647c478bd9Sstevel@tonic-gate 	mib_item_t *tempp2;  /* walking copy of item2 */
9657c478bd9Sstevel@tonic-gate 	mib_item_t *tempp1;  /* walking copy of item1 */
9667c478bd9Sstevel@tonic-gate 	mib_item_t *diffp;
9677c478bd9Sstevel@tonic-gate 	mib_item_t *diffptr; /* walking copy of diffp */
9687c478bd9Sstevel@tonic-gate 	mib_item_t *prevp = NULL;
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate 	if (item1 == NULL) {
9717c478bd9Sstevel@tonic-gate 		diffp = mib_item_dup(item2);
9727c478bd9Sstevel@tonic-gate 		return (diffp);
9737c478bd9Sstevel@tonic-gate 	}
9747c478bd9Sstevel@tonic-gate 
9757c478bd9Sstevel@tonic-gate 	for (tempp2 = item2;
9767c478bd9Sstevel@tonic-gate 	    tempp2;
9777c478bd9Sstevel@tonic-gate 	    tempp2 = tempp2->next_item) {
9787c478bd9Sstevel@tonic-gate 		if (tempp2->mib_id == 0)
9797c478bd9Sstevel@tonic-gate 			switch (tempp2->group) {
9807c478bd9Sstevel@tonic-gate 			/*
9817c478bd9Sstevel@tonic-gate 			 * upon adding a case here, the same
9827c478bd9Sstevel@tonic-gate 			 * must also be added in the next
9837c478bd9Sstevel@tonic-gate 			 * switch statement, alongwith
9847c478bd9Sstevel@tonic-gate 			 * appropriate code
9857c478bd9Sstevel@tonic-gate 			 */
9867c478bd9Sstevel@tonic-gate 			case MIB2_IP:
9877c478bd9Sstevel@tonic-gate 			case MIB2_IP6:
9887c478bd9Sstevel@tonic-gate 			case EXPER_DVMRP:
9897c478bd9Sstevel@tonic-gate 			case EXPER_IGMP:
9907c478bd9Sstevel@tonic-gate 			case MIB2_ICMP:
9917c478bd9Sstevel@tonic-gate 			case MIB2_ICMP6:
9927c478bd9Sstevel@tonic-gate 			case MIB2_TCP:
9937c478bd9Sstevel@tonic-gate 			case MIB2_UDP:
9947c478bd9Sstevel@tonic-gate 			case MIB2_SCTP:
9957c478bd9Sstevel@tonic-gate 			case EXPER_RAWIP:
9967c478bd9Sstevel@tonic-gate 				nitems++;
9977c478bd9Sstevel@tonic-gate 			}
9987c478bd9Sstevel@tonic-gate 	}
9997c478bd9Sstevel@tonic-gate 	tempp2 = NULL;
10007c478bd9Sstevel@tonic-gate 	if (nitems == 0) {
10017c478bd9Sstevel@tonic-gate 		diffp = mib_item_dup(item2);
10027c478bd9Sstevel@tonic-gate 		return (diffp);
10037c478bd9Sstevel@tonic-gate 	}
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate 	diffp = (mib_item_t *)calloc(nitems, sizeof (mib_item_t));
10067c478bd9Sstevel@tonic-gate 	if (diffp == NULL)
10077c478bd9Sstevel@tonic-gate 		return (NULL);
10087c478bd9Sstevel@tonic-gate 	diffptr = diffp;
10097c478bd9Sstevel@tonic-gate 	/* 'for' loop 1: */
10107c478bd9Sstevel@tonic-gate 	for (tempp2 = item2; tempp2 != NULL; tempp2 = tempp2->next_item) {
10117c478bd9Sstevel@tonic-gate 		if (tempp2->mib_id != 0)
10127c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
10137c478bd9Sstevel@tonic-gate 		/* 'for' loop 2: */
10147c478bd9Sstevel@tonic-gate 		for (tempp1 = item1; tempp1 != NULL;
10157c478bd9Sstevel@tonic-gate 		    tempp1 = tempp1->next_item) {
10167c478bd9Sstevel@tonic-gate 			if (!(tempp1->mib_id == 0 &&
10177c478bd9Sstevel@tonic-gate 			    tempp1->group == tempp2->group &&
10187c478bd9Sstevel@tonic-gate 			    tempp1->mib_id == tempp2->mib_id))
10197c478bd9Sstevel@tonic-gate 				continue; /* 'for' loop 2 */
10207c478bd9Sstevel@tonic-gate 			/* found comparable data sets */
10217c478bd9Sstevel@tonic-gate 			if (prevp != NULL)
10227c478bd9Sstevel@tonic-gate 				prevp->next_item = diffptr;
10237c478bd9Sstevel@tonic-gate 			switch (tempp2->group) {
10247c478bd9Sstevel@tonic-gate 			/*
10257c478bd9Sstevel@tonic-gate 			 * Indenting note: Because of long variable names
10267c478bd9Sstevel@tonic-gate 			 * in cases MIB2_IP6 and MIB2_ICMP6, their contents
10277c478bd9Sstevel@tonic-gate 			 * have been indented by one tab space only
10287c478bd9Sstevel@tonic-gate 			 */
10297c478bd9Sstevel@tonic-gate 			case MIB2_IP: {
10307c478bd9Sstevel@tonic-gate 				mib2_ip_t *i2 = (mib2_ip_t *)tempp2->valp;
10317c478bd9Sstevel@tonic-gate 				mib2_ip_t *i1 = (mib2_ip_t *)tempp1->valp;
10327c478bd9Sstevel@tonic-gate 				mib2_ip_t *d;
10337c478bd9Sstevel@tonic-gate 
10347c478bd9Sstevel@tonic-gate 				diffptr->group = tempp2->group;
10357c478bd9Sstevel@tonic-gate 				diffptr->mib_id = tempp2->mib_id;
10367c478bd9Sstevel@tonic-gate 				diffptr->length = tempp2->length;
10377c478bd9Sstevel@tonic-gate 				d = (mib2_ip_t *)calloc(tempp2->length, 1);
10387c478bd9Sstevel@tonic-gate 				if (d == NULL)
10397c478bd9Sstevel@tonic-gate 					goto mibdiff_out_of_memory;
10407c478bd9Sstevel@tonic-gate 				diffptr->valp = d;
10417c478bd9Sstevel@tonic-gate 				d->ipForwarding = i2->ipForwarding;
10427c478bd9Sstevel@tonic-gate 				d->ipDefaultTTL = i2->ipDefaultTTL;
10437c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipInReceives);
10447c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipInHdrErrors);
10457c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipInAddrErrors);
10467c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipInCksumErrs);
10477c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipForwDatagrams);
10487c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipForwProhibits);
10497c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipInUnknownProtos);
10507c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipInDiscards);
10517c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipInDelivers);
10527c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipOutRequests);
10537c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipOutDiscards);
10547c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipOutNoRoutes);
10557c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipReasmTimeout);
10567c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipReasmReqds);
10577c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipReasmOKs);
10587c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipReasmFails);
10597c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipReasmDuplicates);
10607c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipReasmPartDups);
10617c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipFragOKs);
10627c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipFragFails);
10637c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipFragCreates);
10647c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipRoutingDiscards);
10657c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, tcpInErrs);
10667c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, udpNoPorts);
10677c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, udpInCksumErrs);
10687c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, udpInOverflows);
10697c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, rawipInOverflows);
10707c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipsecInSucceeded);
10717c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipsecInFailed);
10727c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipInIPv6);
10737c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipOutIPv6);
10747c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipOutSwitchIPv6);
10757c478bd9Sstevel@tonic-gate 				prevp = diffptr++;
10767c478bd9Sstevel@tonic-gate 				break;
10777c478bd9Sstevel@tonic-gate 			}
10787c478bd9Sstevel@tonic-gate 			case MIB2_IP6: {
10797c478bd9Sstevel@tonic-gate 			mib2_ipv6IfStatsEntry_t *i2;
10807c478bd9Sstevel@tonic-gate 			mib2_ipv6IfStatsEntry_t *i1;
10817c478bd9Sstevel@tonic-gate 			mib2_ipv6IfStatsEntry_t *d;
10827c478bd9Sstevel@tonic-gate 
10837c478bd9Sstevel@tonic-gate 			i2 = (mib2_ipv6IfStatsEntry_t *)tempp2->valp;
10847c478bd9Sstevel@tonic-gate 			i1 = (mib2_ipv6IfStatsEntry_t *)tempp1->valp;
10857c478bd9Sstevel@tonic-gate 			diffptr->group = tempp2->group;
10867c478bd9Sstevel@tonic-gate 			diffptr->mib_id = tempp2->mib_id;
10877c478bd9Sstevel@tonic-gate 			diffptr->length = tempp2->length;
10887c478bd9Sstevel@tonic-gate 			d = (mib2_ipv6IfStatsEntry_t *)calloc(
10897c478bd9Sstevel@tonic-gate 			    tempp2->length, 1);
10907c478bd9Sstevel@tonic-gate 			if (d == NULL)
10917c478bd9Sstevel@tonic-gate 				goto mibdiff_out_of_memory;
10927c478bd9Sstevel@tonic-gate 			diffptr->valp = d;
10937c478bd9Sstevel@tonic-gate 			d->ipv6Forwarding = i2->ipv6Forwarding;
10947c478bd9Sstevel@tonic-gate 			d->ipv6DefaultHopLimit =
10957c478bd9Sstevel@tonic-gate 			    i2->ipv6DefaultHopLimit;
10967c478bd9Sstevel@tonic-gate 
10977c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6InReceives);
10987c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6InHdrErrors);
10997c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6InTooBigErrors);
11007c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6InNoRoutes);
11017c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6InAddrErrors);
11027c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6InUnknownProtos);
11037c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6InTruncatedPkts);
11047c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6InDiscards);
11057c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6InDelivers);
11067c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6OutForwDatagrams);
11077c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6OutRequests);
11087c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6OutDiscards);
11097c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6OutNoRoutes);
11107c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6OutFragOKs);
11117c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6OutFragFails);
11127c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6OutFragCreates);
11137c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6ReasmReqds);
11147c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6ReasmOKs);
11157c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6ReasmFails);
11167c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6InMcastPkts);
11177c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6OutMcastPkts);
11187c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6ReasmDuplicates);
11197c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6ReasmPartDups);
11207c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6ForwProhibits);
11217c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, udpInCksumErrs);
11227c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, udpInOverflows);
11237c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, rawipInOverflows);
11247c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6InIPv4);
11257c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6OutIPv4);
11267c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6OutSwitchIPv4);
11277c478bd9Sstevel@tonic-gate 			prevp = diffptr++;
11287c478bd9Sstevel@tonic-gate 			break;
11297c478bd9Sstevel@tonic-gate 			}
11307c478bd9Sstevel@tonic-gate 			case EXPER_DVMRP: {
11317c478bd9Sstevel@tonic-gate 				struct mrtstat *m2;
11327c478bd9Sstevel@tonic-gate 				struct mrtstat *m1;
11337c478bd9Sstevel@tonic-gate 				struct mrtstat *d;
11347c478bd9Sstevel@tonic-gate 
11357c478bd9Sstevel@tonic-gate 				m2 = (struct mrtstat *)tempp2->valp;
11367c478bd9Sstevel@tonic-gate 				m1 = (struct mrtstat *)tempp1->valp;
11377c478bd9Sstevel@tonic-gate 				diffptr->group = tempp2->group;
11387c478bd9Sstevel@tonic-gate 				diffptr->mib_id = tempp2->mib_id;
11397c478bd9Sstevel@tonic-gate 				diffptr->length = tempp2->length;
11407c478bd9Sstevel@tonic-gate 				d = (struct mrtstat *)calloc(tempp2->length, 1);
11417c478bd9Sstevel@tonic-gate 				if (d == NULL)
11427c478bd9Sstevel@tonic-gate 					goto mibdiff_out_of_memory;
11437c478bd9Sstevel@tonic-gate 				diffptr->valp = d;
11447c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_mfc_hits);
11457c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_mfc_misses);
11467c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_fwd_in);
11477c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_fwd_out);
11487c478bd9Sstevel@tonic-gate 				d->mrts_upcalls = m2->mrts_upcalls;
11497c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_fwd_drop);
11507c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_bad_tunnel);
11517c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_cant_tunnel);
11527c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_wrong_if);
11537c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_upq_ovflw);
11547c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_cache_cleanups);
11557c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_drop_sel);
11567c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_q_overflow);
11577c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_pkt2large);
11587c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_pim_badversion);
11597c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_pim_rcv_badcsum);
11607c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_pim_badregisters);
11617c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_pim_regforwards);
11627c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_pim_regsend_drops);
11637c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_pim_malformed);
11647c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_pim_nomemory);
11657c478bd9Sstevel@tonic-gate 				prevp = diffptr++;
11667c478bd9Sstevel@tonic-gate 				break;
11677c478bd9Sstevel@tonic-gate 			}
11687c478bd9Sstevel@tonic-gate 			case EXPER_IGMP: {
11697c478bd9Sstevel@tonic-gate 				struct igmpstat *i2;
11707c478bd9Sstevel@tonic-gate 				struct igmpstat *i1;
11717c478bd9Sstevel@tonic-gate 				struct igmpstat *d;
11727c478bd9Sstevel@tonic-gate 
11737c478bd9Sstevel@tonic-gate 				i2 = (struct igmpstat *)tempp2->valp;
11747c478bd9Sstevel@tonic-gate 				i1 = (struct igmpstat *)tempp1->valp;
11757c478bd9Sstevel@tonic-gate 				diffptr->group = tempp2->group;
11767c478bd9Sstevel@tonic-gate 				diffptr->mib_id = tempp2->mib_id;
11777c478bd9Sstevel@tonic-gate 				diffptr->length = tempp2->length;
11787c478bd9Sstevel@tonic-gate 				d = (struct igmpstat *)calloc(
11797c478bd9Sstevel@tonic-gate 				    tempp2->length, 1);
11807c478bd9Sstevel@tonic-gate 				if (d == NULL)
11817c478bd9Sstevel@tonic-gate 					goto mibdiff_out_of_memory;
11827c478bd9Sstevel@tonic-gate 				diffptr->valp = d;
11837c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, igps_rcv_total);
11847c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, igps_rcv_tooshort);
11857c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, igps_rcv_badsum);
11867c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, igps_rcv_queries);
11877c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, igps_rcv_badqueries);
11887c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, igps_rcv_reports);
11897c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, igps_rcv_badreports);
11907c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, igps_rcv_ourreports);
11917c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, igps_snd_reports);
11927c478bd9Sstevel@tonic-gate 				prevp = diffptr++;
11937c478bd9Sstevel@tonic-gate 				break;
11947c478bd9Sstevel@tonic-gate 			}
11957c478bd9Sstevel@tonic-gate 			case MIB2_ICMP: {
11967c478bd9Sstevel@tonic-gate 				mib2_icmp_t *i2;
11977c478bd9Sstevel@tonic-gate 				mib2_icmp_t *i1;
11987c478bd9Sstevel@tonic-gate 				mib2_icmp_t *d;
11997c478bd9Sstevel@tonic-gate 
12007c478bd9Sstevel@tonic-gate 				i2 = (mib2_icmp_t *)tempp2->valp;
12017c478bd9Sstevel@tonic-gate 				i1 = (mib2_icmp_t *)tempp1->valp;
12027c478bd9Sstevel@tonic-gate 				diffptr->group = tempp2->group;
12037c478bd9Sstevel@tonic-gate 				diffptr->mib_id = tempp2->mib_id;
12047c478bd9Sstevel@tonic-gate 				diffptr->length = tempp2->length;
12057c478bd9Sstevel@tonic-gate 				d = (mib2_icmp_t *)calloc(tempp2->length, 1);
12067c478bd9Sstevel@tonic-gate 				if (d == NULL)
12077c478bd9Sstevel@tonic-gate 					goto mibdiff_out_of_memory;
12087c478bd9Sstevel@tonic-gate 				diffptr->valp = d;
12097c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInMsgs);
12107c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInErrors);
12117c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInCksumErrs);
12127c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInUnknowns);
12137c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInDestUnreachs);
12147c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInTimeExcds);
12157c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInParmProbs);
12167c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInSrcQuenchs);
12177c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInRedirects);
12187c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInBadRedirects);
12197c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInEchos);
12207c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInEchoReps);
12217c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInTimestamps);
12227c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInAddrMasks);
12237c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInAddrMaskReps);
12247c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInFragNeeded);
12257c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutMsgs);
12267c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutDrops);
12277c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutErrors);
12287c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutDestUnreachs);
12297c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutTimeExcds);
12307c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutParmProbs);
12317c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutSrcQuenchs);
12327c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutRedirects);
12337c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutEchos);
12347c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutEchoReps);
12357c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutTimestamps);
12367c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutTimestampReps);
12377c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutAddrMasks);
12387c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutAddrMaskReps);
12397c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutFragNeeded);
12407c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInOverflows);
12417c478bd9Sstevel@tonic-gate 				prevp = diffptr++;
12427c478bd9Sstevel@tonic-gate 				break;
12437c478bd9Sstevel@tonic-gate 			}
12447c478bd9Sstevel@tonic-gate 			case MIB2_ICMP6: {
12457c478bd9Sstevel@tonic-gate 	mib2_ipv6IfIcmpEntry_t *i2;
12467c478bd9Sstevel@tonic-gate 	mib2_ipv6IfIcmpEntry_t *i1;
12477c478bd9Sstevel@tonic-gate 	mib2_ipv6IfIcmpEntry_t *d;
12487c478bd9Sstevel@tonic-gate 
12497c478bd9Sstevel@tonic-gate 	i2 = (mib2_ipv6IfIcmpEntry_t *)tempp2->valp;
12507c478bd9Sstevel@tonic-gate 	i1 = (mib2_ipv6IfIcmpEntry_t *)tempp1->valp;
12517c478bd9Sstevel@tonic-gate 	diffptr->group = tempp2->group;
12527c478bd9Sstevel@tonic-gate 	diffptr->mib_id = tempp2->mib_id;
12537c478bd9Sstevel@tonic-gate 	diffptr->length = tempp2->length;
12547c478bd9Sstevel@tonic-gate 	d = (mib2_ipv6IfIcmpEntry_t *)calloc(tempp2->length, 1);
12557c478bd9Sstevel@tonic-gate 	if (d == NULL)
12567c478bd9Sstevel@tonic-gate 		goto mibdiff_out_of_memory;
12577c478bd9Sstevel@tonic-gate 	diffptr->valp = d;
12587c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInMsgs);
12597c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInErrors);
12607c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInDestUnreachs);
12617c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInAdminProhibs);
12627c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInTimeExcds);
12637c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInParmProblems);
12647c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInPktTooBigs);
12657c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInEchos);
12667c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInEchoReplies);
12677c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInRouterSolicits);
12687c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInRouterAdvertisements);
12697c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInNeighborSolicits);
12707c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInNeighborAdvertisements);
12717c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInRedirects);
12727c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInBadRedirects);
12737c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInGroupMembQueries);
12747c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInGroupMembResponses);
12757c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInGroupMembReductions);
12767c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInOverflows);
12777c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutMsgs);
12787c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutErrors);
12797c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutDestUnreachs);
12807c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutAdminProhibs);
12817c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutTimeExcds);
12827c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutParmProblems);
12837c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutPktTooBigs);
12847c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutEchos);
12857c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutEchoReplies);
12867c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutRouterSolicits);
12877c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutRouterAdvertisements);
12887c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutNeighborSolicits);
12897c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutNeighborAdvertisements);
12907c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutRedirects);
12917c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutGroupMembQueries);
12927c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutGroupMembResponses);
12937c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutGroupMembReductions);
12947c478bd9Sstevel@tonic-gate 	prevp = diffptr++;
12957c478bd9Sstevel@tonic-gate 	break;
12967c478bd9Sstevel@tonic-gate 			}
12977c478bd9Sstevel@tonic-gate 			case MIB2_TCP: {
12987c478bd9Sstevel@tonic-gate 				mib2_tcp_t *t2;
12997c478bd9Sstevel@tonic-gate 				mib2_tcp_t *t1;
13007c478bd9Sstevel@tonic-gate 				mib2_tcp_t *d;
13017c478bd9Sstevel@tonic-gate 
13027c478bd9Sstevel@tonic-gate 				t2 = (mib2_tcp_t *)tempp2->valp;
13037c478bd9Sstevel@tonic-gate 				t1 = (mib2_tcp_t *)tempp1->valp;
13047c478bd9Sstevel@tonic-gate 				diffptr->group = tempp2->group;
13057c478bd9Sstevel@tonic-gate 				diffptr->mib_id = tempp2->mib_id;
13067c478bd9Sstevel@tonic-gate 				diffptr->length = tempp2->length;
13077c478bd9Sstevel@tonic-gate 				d = (mib2_tcp_t *)calloc(tempp2->length, 1);
13087c478bd9Sstevel@tonic-gate 				if (d == NULL)
13097c478bd9Sstevel@tonic-gate 					goto mibdiff_out_of_memory;
13107c478bd9Sstevel@tonic-gate 				diffptr->valp = d;
13117c478bd9Sstevel@tonic-gate 				d->tcpRtoMin = t2->tcpRtoMin;
13127c478bd9Sstevel@tonic-gate 				d->tcpRtoMax = t2->tcpRtoMax;
13137c478bd9Sstevel@tonic-gate 				d->tcpMaxConn = t2->tcpMaxConn;
13147c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpActiveOpens);
13157c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpPassiveOpens);
13167c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpAttemptFails);
13177c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpEstabResets);
13187c478bd9Sstevel@tonic-gate 				d->tcpCurrEstab = t2->tcpCurrEstab;
13193173664eSapersson 				MDIFF(d, t2, t1, tcpHCOutSegs);
13207c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpOutDataSegs);
13217c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpOutDataBytes);
13227c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpRetransSegs);
13237c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpRetransBytes);
13247c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpOutAck);
13257c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpOutAckDelayed);
13267c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpOutUrg);
13277c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpOutWinUpdate);
13287c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpOutWinProbe);
13297c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpOutControl);
13307c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpOutRsts);
13317c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpOutFastRetrans);
13323173664eSapersson 				MDIFF(d, t2, t1, tcpHCInSegs);
13337c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInAckSegs);
13347c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInAckBytes);
13357c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInDupAck);
13367c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInAckUnsent);
13377c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInDataInorderSegs);
13387c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInDataInorderBytes);
13397c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInDataUnorderSegs);
13407c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInDataUnorderBytes);
13417c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInDataDupSegs);
13427c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInDataDupBytes);
13437c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInDataPartDupSegs);
13447c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInDataPartDupBytes);
13457c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInDataPastWinSegs);
13467c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInDataPastWinBytes);
13477c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInWinProbe);
13487c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInWinUpdate);
13497c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInClosed);
13507c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpRttNoUpdate);
13517c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpRttUpdate);
13527c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpTimRetrans);
13537c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpTimRetransDrop);
13547c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpTimKeepalive);
13557c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpTimKeepaliveProbe);
13567c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpTimKeepaliveDrop);
13577c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpListenDrop);
13587c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpListenDropQ0);
13597c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpHalfOpenDrop);
13607c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpOutSackRetransSegs);
13617c478bd9Sstevel@tonic-gate 				prevp = diffptr++;
13627c478bd9Sstevel@tonic-gate 				break;
13637c478bd9Sstevel@tonic-gate 			}
13647c478bd9Sstevel@tonic-gate 			case MIB2_UDP: {
13657c478bd9Sstevel@tonic-gate 				mib2_udp_t *u2;
13667c478bd9Sstevel@tonic-gate 				mib2_udp_t *u1;
13677c478bd9Sstevel@tonic-gate 				mib2_udp_t *d;
13687c478bd9Sstevel@tonic-gate 
13697c478bd9Sstevel@tonic-gate 				u2 = (mib2_udp_t *)tempp2->valp;
13707c478bd9Sstevel@tonic-gate 				u1 = (mib2_udp_t *)tempp1->valp;
13717c478bd9Sstevel@tonic-gate 				diffptr->group = tempp2->group;
13727c478bd9Sstevel@tonic-gate 				diffptr->mib_id = tempp2->mib_id;
13737c478bd9Sstevel@tonic-gate 				diffptr->length = tempp2->length;
13747c478bd9Sstevel@tonic-gate 				d = (mib2_udp_t *)calloc(tempp2->length, 1);
13757c478bd9Sstevel@tonic-gate 				if (d == NULL)
13767c478bd9Sstevel@tonic-gate 					goto mibdiff_out_of_memory;
13777c478bd9Sstevel@tonic-gate 				diffptr->valp = d;
13783173664eSapersson 				MDIFF(d, u2, u1, udpHCInDatagrams);
13797c478bd9Sstevel@tonic-gate 				MDIFF(d, u2, u1, udpInErrors);
13803173664eSapersson 				MDIFF(d, u2, u1, udpHCOutDatagrams);
13817c478bd9Sstevel@tonic-gate 				MDIFF(d, u2, u1, udpOutErrors);
13827c478bd9Sstevel@tonic-gate 				prevp = diffptr++;
13837c478bd9Sstevel@tonic-gate 				break;
13847c478bd9Sstevel@tonic-gate 			}
13857c478bd9Sstevel@tonic-gate 			case MIB2_SCTP: {
13867c478bd9Sstevel@tonic-gate 				mib2_sctp_t *s2;
13877c478bd9Sstevel@tonic-gate 				mib2_sctp_t *s1;
13887c478bd9Sstevel@tonic-gate 				mib2_sctp_t *d;
13897c478bd9Sstevel@tonic-gate 
13907c478bd9Sstevel@tonic-gate 				s2 = (mib2_sctp_t *)tempp2->valp;
13917c478bd9Sstevel@tonic-gate 				s1 = (mib2_sctp_t *)tempp1->valp;
13927c478bd9Sstevel@tonic-gate 				diffptr->group = tempp2->group;
13937c478bd9Sstevel@tonic-gate 				diffptr->mib_id = tempp2->mib_id;
13947c478bd9Sstevel@tonic-gate 				diffptr->length = tempp2->length;
13957c478bd9Sstevel@tonic-gate 				d = (mib2_sctp_t *)calloc(tempp2->length, 1);
13967c478bd9Sstevel@tonic-gate 				if (d == NULL)
13977c478bd9Sstevel@tonic-gate 					goto mibdiff_out_of_memory;
13987c478bd9Sstevel@tonic-gate 				diffptr->valp = d;
13997c478bd9Sstevel@tonic-gate 				d->sctpRtoAlgorithm = s2->sctpRtoAlgorithm;
14007c478bd9Sstevel@tonic-gate 				d->sctpRtoMin = s2->sctpRtoMin;
14017c478bd9Sstevel@tonic-gate 				d->sctpRtoMax = s2->sctpRtoMax;
14027c478bd9Sstevel@tonic-gate 				d->sctpRtoInitial = s2->sctpRtoInitial;
14037c478bd9Sstevel@tonic-gate 				d->sctpMaxAssocs = s2->sctpMaxAssocs;
14047c478bd9Sstevel@tonic-gate 				d->sctpValCookieLife = s2->sctpValCookieLife;
14057c478bd9Sstevel@tonic-gate 				d->sctpMaxInitRetr = s2->sctpMaxInitRetr;
14067c478bd9Sstevel@tonic-gate 				d->sctpCurrEstab = s2->sctpCurrEstab;
14077c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpActiveEstab);
14087c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpPassiveEstab);
14097c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpAborted);
14107c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpShutdowns);
14117c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpOutOfBlue);
14127c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpChecksumError);
14137c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpOutCtrlChunks);
14147c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpOutOrderChunks);
14157c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpOutUnorderChunks);
14167c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpRetransChunks);
14177c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpOutAck);
14187c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpOutAckDelayed);
14197c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpOutWinUpdate);
14207c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpOutFastRetrans);
14217c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpOutWinProbe);
14227c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpInCtrlChunks);
14237c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpInOrderChunks);
14247c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpInUnorderChunks);
14257c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpInAck);
14267c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpInDupAck);
14277c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpInAckUnsent);
14287c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpFragUsrMsgs);
14297c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpReasmUsrMsgs);
14307c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpOutSCTPPkts);
14317c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpInSCTPPkts);
14327c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpInInvalidCookie);
14337c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpTimRetrans);
14347c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpTimRetransDrop);
14357c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpTimHeartBeatProbe);
14367c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpTimHeartBeatDrop);
14377c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpListenDrop);
14387c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpInClosed);
14397c478bd9Sstevel@tonic-gate 				prevp = diffptr++;
14407c478bd9Sstevel@tonic-gate 				break;
14417c478bd9Sstevel@tonic-gate 			}
14427c478bd9Sstevel@tonic-gate 			case EXPER_RAWIP: {
14437c478bd9Sstevel@tonic-gate 				mib2_rawip_t *r2;
14447c478bd9Sstevel@tonic-gate 				mib2_rawip_t *r1;
14457c478bd9Sstevel@tonic-gate 				mib2_rawip_t *d;
14467c478bd9Sstevel@tonic-gate 
14477c478bd9Sstevel@tonic-gate 				r2 = (mib2_rawip_t *)tempp2->valp;
14487c478bd9Sstevel@tonic-gate 				r1 = (mib2_rawip_t *)tempp1->valp;
14497c478bd9Sstevel@tonic-gate 				diffptr->group = tempp2->group;
14507c478bd9Sstevel@tonic-gate 				diffptr->mib_id = tempp2->mib_id;
14517c478bd9Sstevel@tonic-gate 				diffptr->length = tempp2->length;
14527c478bd9Sstevel@tonic-gate 				d = (mib2_rawip_t *)calloc(tempp2->length, 1);
14537c478bd9Sstevel@tonic-gate 				if (d == NULL)
14547c478bd9Sstevel@tonic-gate 					goto mibdiff_out_of_memory;
14557c478bd9Sstevel@tonic-gate 				diffptr->valp = d;
14567c478bd9Sstevel@tonic-gate 				MDIFF(d, r2, r1, rawipInDatagrams);
14577c478bd9Sstevel@tonic-gate 				MDIFF(d, r2, r1, rawipInErrors);
14587c478bd9Sstevel@tonic-gate 				MDIFF(d, r2, r1, rawipInCksumErrs);
14597c478bd9Sstevel@tonic-gate 				MDIFF(d, r2, r1, rawipOutDatagrams);
14607c478bd9Sstevel@tonic-gate 				MDIFF(d, r2, r1, rawipOutErrors);
14617c478bd9Sstevel@tonic-gate 				prevp = diffptr++;
14627c478bd9Sstevel@tonic-gate 				break;
14637c478bd9Sstevel@tonic-gate 			}
14647c478bd9Sstevel@tonic-gate 			/*
14657c478bd9Sstevel@tonic-gate 			 * there are more "group" types but they aren't
14667c478bd9Sstevel@tonic-gate 			 * required for the -s and -Ms options
14677c478bd9Sstevel@tonic-gate 			 */
14687c478bd9Sstevel@tonic-gate 			}
14697c478bd9Sstevel@tonic-gate 		} /* 'for' loop 2 ends */
14707c478bd9Sstevel@tonic-gate 		tempp1 = NULL;
14717c478bd9Sstevel@tonic-gate 	} /* 'for' loop 1 ends */
14727c478bd9Sstevel@tonic-gate 	tempp2 = NULL;
14737c478bd9Sstevel@tonic-gate 	diffptr--;
14747c478bd9Sstevel@tonic-gate 	diffptr->next_item = NULL;
14757c478bd9Sstevel@tonic-gate 	return (diffp);
14767c478bd9Sstevel@tonic-gate 
14777c478bd9Sstevel@tonic-gate mibdiff_out_of_memory:;
14787c478bd9Sstevel@tonic-gate 	mib_item_destroy(&diffp);
14797c478bd9Sstevel@tonic-gate 	return (NULL);
14807c478bd9Sstevel@tonic-gate }
14817c478bd9Sstevel@tonic-gate 
14827c478bd9Sstevel@tonic-gate /*
14837c478bd9Sstevel@tonic-gate  * mib_item_destroy: cleans up a mib_item_t *
14847c478bd9Sstevel@tonic-gate  * that was created by calling mib_item_dup or
14857c478bd9Sstevel@tonic-gate  * mib_item_diff
14867c478bd9Sstevel@tonic-gate  */
14877c478bd9Sstevel@tonic-gate static void
14887c478bd9Sstevel@tonic-gate mib_item_destroy(mib_item_t **itemp) {
14897c478bd9Sstevel@tonic-gate 	int	nitems = 0;
14907c478bd9Sstevel@tonic-gate 	int	c = 0;
14917c478bd9Sstevel@tonic-gate 	mib_item_t *tempp;
14927c478bd9Sstevel@tonic-gate 
14937c478bd9Sstevel@tonic-gate 	if (itemp == NULL || *itemp == NULL)
14947c478bd9Sstevel@tonic-gate 		return;
14957c478bd9Sstevel@tonic-gate 
14967c478bd9Sstevel@tonic-gate 	for (tempp = *itemp; tempp != NULL; tempp = tempp->next_item)
14977c478bd9Sstevel@tonic-gate 		if (tempp->mib_id == 0)
14987c478bd9Sstevel@tonic-gate 			nitems++;
14997c478bd9Sstevel@tonic-gate 		else
15007c478bd9Sstevel@tonic-gate 			return;	/* cannot destroy! */
15017c478bd9Sstevel@tonic-gate 
15027c478bd9Sstevel@tonic-gate 	if (nitems == 0)
15037c478bd9Sstevel@tonic-gate 		return;		/* cannot destroy! */
15047c478bd9Sstevel@tonic-gate 
15057c478bd9Sstevel@tonic-gate 	for (c = nitems - 1; c >= 0; c--) {
15067c478bd9Sstevel@tonic-gate 		if ((itemp[0][c]).valp != NULL)
15077c478bd9Sstevel@tonic-gate 			free((itemp[0][c]).valp);
15087c478bd9Sstevel@tonic-gate 	}
15097c478bd9Sstevel@tonic-gate 	free(*itemp);
15107c478bd9Sstevel@tonic-gate 
15117c478bd9Sstevel@tonic-gate 	*itemp = NULL;
15127c478bd9Sstevel@tonic-gate }
15137c478bd9Sstevel@tonic-gate 
15147c478bd9Sstevel@tonic-gate /* Compare two Octet_ts.  Return B_TRUE if they match, B_FALSE if not. */
15157c478bd9Sstevel@tonic-gate static boolean_t
15167c478bd9Sstevel@tonic-gate octetstrmatch(const Octet_t *a, const Octet_t *b)
15177c478bd9Sstevel@tonic-gate {
15187c478bd9Sstevel@tonic-gate 	if (a == NULL || b == NULL)
15197c478bd9Sstevel@tonic-gate 		return (B_FALSE);
15207c478bd9Sstevel@tonic-gate 
15217c478bd9Sstevel@tonic-gate 	if (a->o_length != b->o_length)
15227c478bd9Sstevel@tonic-gate 		return (B_FALSE);
15237c478bd9Sstevel@tonic-gate 
15247c478bd9Sstevel@tonic-gate 	return (memcmp(a->o_bytes, b->o_bytes, a->o_length) == 0);
15257c478bd9Sstevel@tonic-gate }
15267c478bd9Sstevel@tonic-gate 
15277c478bd9Sstevel@tonic-gate /* If octetstr() changes make an appropriate change to STR_EXPAND */
15287c478bd9Sstevel@tonic-gate static char *
152945916cd2Sjpk octetstr(const Octet_t *op, int code, char *dst, uint_t dstlen)
15307c478bd9Sstevel@tonic-gate {
15317c478bd9Sstevel@tonic-gate 	int	i;
15327c478bd9Sstevel@tonic-gate 	char	*cp;
15337c478bd9Sstevel@tonic-gate 
15347c478bd9Sstevel@tonic-gate 	cp = dst;
15357c478bd9Sstevel@tonic-gate 	if (op) {
15367c478bd9Sstevel@tonic-gate 		for (i = 0; i < op->o_length; i++) {
15377c478bd9Sstevel@tonic-gate 			switch (code) {
15387c478bd9Sstevel@tonic-gate 			case 'd':
15397c478bd9Sstevel@tonic-gate 				if (cp - dst + 4 > dstlen) {
15407c478bd9Sstevel@tonic-gate 					*cp = '\0';
15417c478bd9Sstevel@tonic-gate 					return (dst);
15427c478bd9Sstevel@tonic-gate 				}
15437c478bd9Sstevel@tonic-gate 				(void) snprintf(cp, 5, "%d.",
15447c478bd9Sstevel@tonic-gate 				    0xff & op->o_bytes[i]);
15457c478bd9Sstevel@tonic-gate 				cp = strchr(cp, '\0');
15467c478bd9Sstevel@tonic-gate 				break;
15477c478bd9Sstevel@tonic-gate 			case 'a':
15487c478bd9Sstevel@tonic-gate 				if (cp - dst + 1 > dstlen) {
15497c478bd9Sstevel@tonic-gate 					*cp = '\0';
15507c478bd9Sstevel@tonic-gate 					return (dst);
15517c478bd9Sstevel@tonic-gate 				}
15527c478bd9Sstevel@tonic-gate 				*cp++ = op->o_bytes[i];
15537c478bd9Sstevel@tonic-gate 				break;
15547c478bd9Sstevel@tonic-gate 			case 'h':
15557c478bd9Sstevel@tonic-gate 			default:
15567c478bd9Sstevel@tonic-gate 				if (cp - dst + 3 > dstlen) {
15577c478bd9Sstevel@tonic-gate 					*cp = '\0';
15587c478bd9Sstevel@tonic-gate 					return (dst);
15597c478bd9Sstevel@tonic-gate 				}
15607c478bd9Sstevel@tonic-gate 				(void) snprintf(cp, 4, "%02x:",
15617c478bd9Sstevel@tonic-gate 				    0xff & op->o_bytes[i]);
15627c478bd9Sstevel@tonic-gate 				cp += 3;
15637c478bd9Sstevel@tonic-gate 				break;
15647c478bd9Sstevel@tonic-gate 			}
15657c478bd9Sstevel@tonic-gate 		}
15667c478bd9Sstevel@tonic-gate 	}
15677c478bd9Sstevel@tonic-gate 	if (code != 'a' && cp != dst)
15687c478bd9Sstevel@tonic-gate 		cp--;
15697c478bd9Sstevel@tonic-gate 	*cp = '\0';
15707c478bd9Sstevel@tonic-gate 	return (dst);
15717c478bd9Sstevel@tonic-gate }
15727c478bd9Sstevel@tonic-gate 
157345916cd2Sjpk static const char *
157445916cd2Sjpk mitcp_state(int state, const mib2_transportMLPEntry_t *attr)
15757c478bd9Sstevel@tonic-gate {
157645916cd2Sjpk 	static char tcpsbuf[50];
157745916cd2Sjpk 	const char *cp;
15787c478bd9Sstevel@tonic-gate 
15797c478bd9Sstevel@tonic-gate 	switch (state) {
15807c478bd9Sstevel@tonic-gate 	case TCPS_CLOSED:
15817c478bd9Sstevel@tonic-gate 		cp = "CLOSED";
15827c478bd9Sstevel@tonic-gate 		break;
15837c478bd9Sstevel@tonic-gate 	case TCPS_IDLE:
15847c478bd9Sstevel@tonic-gate 		cp = "IDLE";
15857c478bd9Sstevel@tonic-gate 		break;
15867c478bd9Sstevel@tonic-gate 	case TCPS_BOUND:
15877c478bd9Sstevel@tonic-gate 		cp = "BOUND";
15887c478bd9Sstevel@tonic-gate 		break;
15897c478bd9Sstevel@tonic-gate 	case TCPS_LISTEN:
15907c478bd9Sstevel@tonic-gate 		cp = "LISTEN";
15917c478bd9Sstevel@tonic-gate 		break;
15927c478bd9Sstevel@tonic-gate 	case TCPS_SYN_SENT:
15937c478bd9Sstevel@tonic-gate 		cp = "SYN_SENT";
15947c478bd9Sstevel@tonic-gate 		break;
15957c478bd9Sstevel@tonic-gate 	case TCPS_SYN_RCVD:
15967c478bd9Sstevel@tonic-gate 		cp = "SYN_RCVD";
15977c478bd9Sstevel@tonic-gate 		break;
15987c478bd9Sstevel@tonic-gate 	case TCPS_ESTABLISHED:
15997c478bd9Sstevel@tonic-gate 		cp = "ESTABLISHED";
16007c478bd9Sstevel@tonic-gate 		break;
16017c478bd9Sstevel@tonic-gate 	case TCPS_CLOSE_WAIT:
16027c478bd9Sstevel@tonic-gate 		cp = "CLOSE_WAIT";
16037c478bd9Sstevel@tonic-gate 		break;
16047c478bd9Sstevel@tonic-gate 	case TCPS_FIN_WAIT_1:
16057c478bd9Sstevel@tonic-gate 		cp = "FIN_WAIT_1";
16067c478bd9Sstevel@tonic-gate 		break;
16077c478bd9Sstevel@tonic-gate 	case TCPS_CLOSING:
16087c478bd9Sstevel@tonic-gate 		cp = "CLOSING";
16097c478bd9Sstevel@tonic-gate 		break;
16107c478bd9Sstevel@tonic-gate 	case TCPS_LAST_ACK:
16117c478bd9Sstevel@tonic-gate 		cp = "LAST_ACK";
16127c478bd9Sstevel@tonic-gate 		break;
16137c478bd9Sstevel@tonic-gate 	case TCPS_FIN_WAIT_2:
16147c478bd9Sstevel@tonic-gate 		cp = "FIN_WAIT_2";
16157c478bd9Sstevel@tonic-gate 		break;
16167c478bd9Sstevel@tonic-gate 	case TCPS_TIME_WAIT:
16177c478bd9Sstevel@tonic-gate 		cp = "TIME_WAIT";
16187c478bd9Sstevel@tonic-gate 		break;
16197c478bd9Sstevel@tonic-gate 	default:
16207c478bd9Sstevel@tonic-gate 		(void) snprintf(tcpsbuf, sizeof (tcpsbuf),
16217c478bd9Sstevel@tonic-gate 		    "UnknownState(%d)", state);
16227c478bd9Sstevel@tonic-gate 		cp = tcpsbuf;
16237c478bd9Sstevel@tonic-gate 		break;
16247c478bd9Sstevel@tonic-gate 	}
162545916cd2Sjpk 
162645916cd2Sjpk 	if (RSECflag && attr != NULL && attr->tme_flags != 0) {
162745916cd2Sjpk 		if (cp != tcpsbuf) {
162845916cd2Sjpk 			(void) strlcpy(tcpsbuf, cp, sizeof (tcpsbuf));
162945916cd2Sjpk 			cp = tcpsbuf;
163045916cd2Sjpk 		}
163145916cd2Sjpk 		if (attr->tme_flags & MIB2_TMEF_PRIVATE)
163245916cd2Sjpk 			(void) strlcat(tcpsbuf, " P", sizeof (tcpsbuf));
163345916cd2Sjpk 		if (attr->tme_flags & MIB2_TMEF_SHARED)
163445916cd2Sjpk 			(void) strlcat(tcpsbuf, " S", sizeof (tcpsbuf));
163545916cd2Sjpk 	}
163645916cd2Sjpk 
163745916cd2Sjpk 	return (cp);
163845916cd2Sjpk }
163945916cd2Sjpk 
164045916cd2Sjpk static const char *
164145916cd2Sjpk miudp_state(int state, const mib2_transportMLPEntry_t *attr)
164245916cd2Sjpk {
164345916cd2Sjpk 	static char udpsbuf[50];
164445916cd2Sjpk 	const char *cp;
164545916cd2Sjpk 
164645916cd2Sjpk 	switch (state) {
164745916cd2Sjpk 	case MIB2_UDP_unbound:
164845916cd2Sjpk 		cp = "Unbound";
164945916cd2Sjpk 		break;
165045916cd2Sjpk 	case MIB2_UDP_idle:
165145916cd2Sjpk 		cp = "Idle";
165245916cd2Sjpk 		break;
165345916cd2Sjpk 	case MIB2_UDP_connected:
165445916cd2Sjpk 		cp = "Connected";
165545916cd2Sjpk 		break;
165645916cd2Sjpk 	default:
165745916cd2Sjpk 		(void) snprintf(udpsbuf, sizeof (udpsbuf),
165845916cd2Sjpk 		    "Unknown State(%d)", state);
165945916cd2Sjpk 		cp = udpsbuf;
166045916cd2Sjpk 		break;
166145916cd2Sjpk 	}
166245916cd2Sjpk 
166345916cd2Sjpk 	if (RSECflag && attr != NULL && attr->tme_flags != 0) {
166445916cd2Sjpk 		if (cp != udpsbuf) {
166545916cd2Sjpk 			(void) strlcpy(udpsbuf, cp, sizeof (udpsbuf));
166645916cd2Sjpk 			cp = udpsbuf;
166745916cd2Sjpk 		}
166845916cd2Sjpk 		if (attr->tme_flags & MIB2_TMEF_PRIVATE)
166945916cd2Sjpk 			(void) strlcat(udpsbuf, " P", sizeof (udpsbuf));
167045916cd2Sjpk 		if (attr->tme_flags & MIB2_TMEF_SHARED)
167145916cd2Sjpk 			(void) strlcat(udpsbuf, " S", sizeof (udpsbuf));
167245916cd2Sjpk 	}
167345916cd2Sjpk 
16747c478bd9Sstevel@tonic-gate 	return (cp);
16757c478bd9Sstevel@tonic-gate }
16767c478bd9Sstevel@tonic-gate 
16777c478bd9Sstevel@tonic-gate static int odd;
16787c478bd9Sstevel@tonic-gate 
16797c478bd9Sstevel@tonic-gate static void
16807c478bd9Sstevel@tonic-gate prval_init(void)
16817c478bd9Sstevel@tonic-gate {
16827c478bd9Sstevel@tonic-gate 	odd = 0;
16837c478bd9Sstevel@tonic-gate }
16847c478bd9Sstevel@tonic-gate 
16857c478bd9Sstevel@tonic-gate static void
16867c478bd9Sstevel@tonic-gate prval(char *str, Counter val)
16877c478bd9Sstevel@tonic-gate {
16887c478bd9Sstevel@tonic-gate 	(void) printf("\t%-20s=%6u", str, val);
16897c478bd9Sstevel@tonic-gate 	if (odd++ & 1)
16907c478bd9Sstevel@tonic-gate 		(void) putchar('\n');
16917c478bd9Sstevel@tonic-gate }
16927c478bd9Sstevel@tonic-gate 
16937c478bd9Sstevel@tonic-gate static void
16947c478bd9Sstevel@tonic-gate prval64(char *str, Counter64 val)
16957c478bd9Sstevel@tonic-gate {
16967c478bd9Sstevel@tonic-gate 	(void) printf("\t%-20s=%6llu", str, val);
16977c478bd9Sstevel@tonic-gate 	if (odd++ & 1)
16987c478bd9Sstevel@tonic-gate 		(void) putchar('\n');
16997c478bd9Sstevel@tonic-gate }
17007c478bd9Sstevel@tonic-gate 
17017c478bd9Sstevel@tonic-gate static void
17027c478bd9Sstevel@tonic-gate pr_int_val(char *str, int val)
17037c478bd9Sstevel@tonic-gate {
17047c478bd9Sstevel@tonic-gate 	(void) printf("\t%-20s=%6d", str, val);
17057c478bd9Sstevel@tonic-gate 	if (odd++ & 1)
17067c478bd9Sstevel@tonic-gate 		(void) putchar('\n');
17077c478bd9Sstevel@tonic-gate }
17087c478bd9Sstevel@tonic-gate 
17097c478bd9Sstevel@tonic-gate static void
17107c478bd9Sstevel@tonic-gate pr_sctp_rtoalgo(char *str, int val)
17117c478bd9Sstevel@tonic-gate {
17127c478bd9Sstevel@tonic-gate 	(void) printf("\t%-20s=", str);
17137c478bd9Sstevel@tonic-gate 	switch (val) {
17147c478bd9Sstevel@tonic-gate 		case MIB2_SCTP_RTOALGO_OTHER:
17157c478bd9Sstevel@tonic-gate 			(void) printf("%6.6s", "other");
17167c478bd9Sstevel@tonic-gate 			break;
17177c478bd9Sstevel@tonic-gate 
17187c478bd9Sstevel@tonic-gate 		case MIB2_SCTP_RTOALGO_VANJ:
17197c478bd9Sstevel@tonic-gate 			(void) printf("%6.6s", "vanj");
17207c478bd9Sstevel@tonic-gate 			break;
17217c478bd9Sstevel@tonic-gate 
17227c478bd9Sstevel@tonic-gate 		default:
17237c478bd9Sstevel@tonic-gate 			(void) printf("%6d", val);
17247c478bd9Sstevel@tonic-gate 			break;
17257c478bd9Sstevel@tonic-gate 	}
17267c478bd9Sstevel@tonic-gate 	if (odd++ & 1)
17277c478bd9Sstevel@tonic-gate 		(void) putchar('\n');
17287c478bd9Sstevel@tonic-gate }
17297c478bd9Sstevel@tonic-gate 
17307c478bd9Sstevel@tonic-gate static void
17317c478bd9Sstevel@tonic-gate prval_end(void)
17327c478bd9Sstevel@tonic-gate {
17337c478bd9Sstevel@tonic-gate 	if (odd++ & 1)
17347c478bd9Sstevel@tonic-gate 		(void) putchar('\n');
17357c478bd9Sstevel@tonic-gate }
17367c478bd9Sstevel@tonic-gate 
17377c478bd9Sstevel@tonic-gate /* Extract constant sizes */
17387c478bd9Sstevel@tonic-gate static void
17397c478bd9Sstevel@tonic-gate mib_get_constants(mib_item_t *item)
17407c478bd9Sstevel@tonic-gate {
17417c478bd9Sstevel@tonic-gate 	/* 'for' loop 1: */
17427c478bd9Sstevel@tonic-gate 	for (; item; item = item->next_item) {
17437c478bd9Sstevel@tonic-gate 		if (item->mib_id != 0)
17447c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
17457c478bd9Sstevel@tonic-gate 
17467c478bd9Sstevel@tonic-gate 		switch (item->group) {
17477c478bd9Sstevel@tonic-gate 		case MIB2_IP: {
17487c478bd9Sstevel@tonic-gate 			mib2_ip_t	*ip = (mib2_ip_t *)item->valp;
17497c478bd9Sstevel@tonic-gate 
17507c478bd9Sstevel@tonic-gate 			ipAddrEntrySize = ip->ipAddrEntrySize;
17517c478bd9Sstevel@tonic-gate 			ipRouteEntrySize = ip->ipRouteEntrySize;
17527c478bd9Sstevel@tonic-gate 			ipNetToMediaEntrySize = ip->ipNetToMediaEntrySize;
17537c478bd9Sstevel@tonic-gate 			ipMemberEntrySize = ip->ipMemberEntrySize;
17547c478bd9Sstevel@tonic-gate 			ipGroupSourceEntrySize = ip->ipGroupSourceEntrySize;
175545916cd2Sjpk 			ipRouteAttributeSize = ip->ipRouteAttributeSize;
175645916cd2Sjpk 			transportMLPSize = ip->transportMLPSize;
17577c478bd9Sstevel@tonic-gate 			assert(IS_P2ALIGNED(ipAddrEntrySize,
1758e11c3f44Smeem 			    sizeof (mib2_ipAddrEntry_t *)));
1759e11c3f44Smeem 			assert(IS_P2ALIGNED(ipRouteEntrySize,
1760e11c3f44Smeem 			    sizeof (mib2_ipRouteEntry_t *)));
1761e11c3f44Smeem 			assert(IS_P2ALIGNED(ipNetToMediaEntrySize,
1762e11c3f44Smeem 			    sizeof (mib2_ipNetToMediaEntry_t *)));
1763e11c3f44Smeem 			assert(IS_P2ALIGNED(ipMemberEntrySize,
1764e11c3f44Smeem 			    sizeof (ip_member_t *)));
1765e11c3f44Smeem 			assert(IS_P2ALIGNED(ipGroupSourceEntrySize,
1766e11c3f44Smeem 			    sizeof (ip_grpsrc_t *)));
1767e11c3f44Smeem 			assert(IS_P2ALIGNED(ipRouteAttributeSize,
1768e11c3f44Smeem 			    sizeof (mib2_ipAttributeEntry_t *)));
1769e11c3f44Smeem 			assert(IS_P2ALIGNED(transportMLPSize,
1770e11c3f44Smeem 			    sizeof (mib2_transportMLPEntry_t *)));
17717c478bd9Sstevel@tonic-gate 			break;
17727c478bd9Sstevel@tonic-gate 		}
17737c478bd9Sstevel@tonic-gate 		case EXPER_DVMRP: {
17747c478bd9Sstevel@tonic-gate 			struct mrtstat	*mrts = (struct mrtstat *)item->valp;
17757c478bd9Sstevel@tonic-gate 
17767c478bd9Sstevel@tonic-gate 			vifctlSize = mrts->mrts_vifctlSize;
17777c478bd9Sstevel@tonic-gate 			mfcctlSize = mrts->mrts_mfcctlSize;
17787c478bd9Sstevel@tonic-gate 			assert(IS_P2ALIGNED(vifctlSize,
1779e11c3f44Smeem 			    sizeof (struct vifclt *)));
1780e11c3f44Smeem 			assert(IS_P2ALIGNED(mfcctlSize,
1781e11c3f44Smeem 			    sizeof (struct mfcctl *)));
17827c478bd9Sstevel@tonic-gate 			break;
17837c478bd9Sstevel@tonic-gate 		}
17847c478bd9Sstevel@tonic-gate 		case MIB2_IP6: {
17857c478bd9Sstevel@tonic-gate 			mib2_ipv6IfStatsEntry_t *ip6;
17867c478bd9Sstevel@tonic-gate 			/* Just use the first entry */
17877c478bd9Sstevel@tonic-gate 
17887c478bd9Sstevel@tonic-gate 			ip6 = (mib2_ipv6IfStatsEntry_t *)item->valp;
17897c478bd9Sstevel@tonic-gate 			ipv6IfStatsEntrySize = ip6->ipv6IfStatsEntrySize;
17907c478bd9Sstevel@tonic-gate 			ipv6AddrEntrySize = ip6->ipv6AddrEntrySize;
17917c478bd9Sstevel@tonic-gate 			ipv6RouteEntrySize = ip6->ipv6RouteEntrySize;
17927c478bd9Sstevel@tonic-gate 			ipv6NetToMediaEntrySize = ip6->ipv6NetToMediaEntrySize;
17937c478bd9Sstevel@tonic-gate 			ipv6MemberEntrySize = ip6->ipv6MemberEntrySize;
17947c478bd9Sstevel@tonic-gate 			ipv6GroupSourceEntrySize =
17957c478bd9Sstevel@tonic-gate 			    ip6->ipv6GroupSourceEntrySize;
17967c478bd9Sstevel@tonic-gate 			assert(IS_P2ALIGNED(ipv6IfStatsEntrySize,
1797e11c3f44Smeem 			    sizeof (mib2_ipv6IfStatsEntry_t *)));
1798e11c3f44Smeem 			assert(IS_P2ALIGNED(ipv6AddrEntrySize,
1799e11c3f44Smeem 			    sizeof (mib2_ipv6AddrEntry_t *)));
1800e11c3f44Smeem 			assert(IS_P2ALIGNED(ipv6RouteEntrySize,
1801e11c3f44Smeem 			    sizeof (mib2_ipv6RouteEntry_t *)));
1802e11c3f44Smeem 			assert(IS_P2ALIGNED(ipv6NetToMediaEntrySize,
1803e11c3f44Smeem 			    sizeof (mib2_ipv6NetToMediaEntry_t *)));
1804e11c3f44Smeem 			assert(IS_P2ALIGNED(ipv6MemberEntrySize,
1805e11c3f44Smeem 			    sizeof (ipv6_member_t *)));
1806e11c3f44Smeem 			assert(IS_P2ALIGNED(ipv6GroupSourceEntrySize,
1807e11c3f44Smeem 			    sizeof (ipv6_grpsrc_t *)));
18087c478bd9Sstevel@tonic-gate 			break;
18097c478bd9Sstevel@tonic-gate 		}
18107c478bd9Sstevel@tonic-gate 		case MIB2_ICMP6: {
18117c478bd9Sstevel@tonic-gate 			mib2_ipv6IfIcmpEntry_t *icmp6;
18127c478bd9Sstevel@tonic-gate 			/* Just use the first entry */
18137c478bd9Sstevel@tonic-gate 
18147c478bd9Sstevel@tonic-gate 			icmp6 = (mib2_ipv6IfIcmpEntry_t *)item->valp;
18157c478bd9Sstevel@tonic-gate 			ipv6IfIcmpEntrySize = icmp6->ipv6IfIcmpEntrySize;
18167c478bd9Sstevel@tonic-gate 			assert(IS_P2ALIGNED(ipv6IfIcmpEntrySize,
18177c478bd9Sstevel@tonic-gate 			    sizeof (mib2_ipv6IfIcmpEntry_t *)));
18187c478bd9Sstevel@tonic-gate 			break;
18197c478bd9Sstevel@tonic-gate 		}
18207c478bd9Sstevel@tonic-gate 		case MIB2_TCP: {
18217c478bd9Sstevel@tonic-gate 			mib2_tcp_t	*tcp = (mib2_tcp_t *)item->valp;
18227c478bd9Sstevel@tonic-gate 
18237c478bd9Sstevel@tonic-gate 			tcpConnEntrySize = tcp->tcpConnTableSize;
18247c478bd9Sstevel@tonic-gate 			tcp6ConnEntrySize = tcp->tcp6ConnTableSize;
18257c478bd9Sstevel@tonic-gate 			assert(IS_P2ALIGNED(tcpConnEntrySize,
1826e11c3f44Smeem 			    sizeof (mib2_tcpConnEntry_t *)));
1827e11c3f44Smeem 			assert(IS_P2ALIGNED(tcp6ConnEntrySize,
1828e11c3f44Smeem 			    sizeof (mib2_tcp6ConnEntry_t *)));
18297c478bd9Sstevel@tonic-gate 			break;
18307c478bd9Sstevel@tonic-gate 		}
18317c478bd9Sstevel@tonic-gate 		case MIB2_UDP: {
18327c478bd9Sstevel@tonic-gate 			mib2_udp_t	*udp = (mib2_udp_t *)item->valp;
18337c478bd9Sstevel@tonic-gate 
18347c478bd9Sstevel@tonic-gate 			udpEntrySize = udp->udpEntrySize;
18357c478bd9Sstevel@tonic-gate 			udp6EntrySize = udp->udp6EntrySize;
18367c478bd9Sstevel@tonic-gate 			assert(IS_P2ALIGNED(udpEntrySize,
1837e11c3f44Smeem 			    sizeof (mib2_udpEntry_t *)));
1838e11c3f44Smeem 			assert(IS_P2ALIGNED(udp6EntrySize,
1839e11c3f44Smeem 			    sizeof (mib2_udp6Entry_t *)));
18407c478bd9Sstevel@tonic-gate 			break;
18417c478bd9Sstevel@tonic-gate 		}
18427c478bd9Sstevel@tonic-gate 		case MIB2_SCTP: {
18437c478bd9Sstevel@tonic-gate 			mib2_sctp_t	*sctp = (mib2_sctp_t *)item->valp;
18447c478bd9Sstevel@tonic-gate 
18457c478bd9Sstevel@tonic-gate 			sctpEntrySize = sctp->sctpEntrySize;
18467c478bd9Sstevel@tonic-gate 			sctpLocalEntrySize = sctp->sctpLocalEntrySize;
18477c478bd9Sstevel@tonic-gate 			sctpRemoteEntrySize = sctp->sctpRemoteEntrySize;
18487c478bd9Sstevel@tonic-gate 			break;
18497c478bd9Sstevel@tonic-gate 		}
18507c478bd9Sstevel@tonic-gate 		}
18517c478bd9Sstevel@tonic-gate 	} /* 'for' loop 1 ends */
18527c478bd9Sstevel@tonic-gate 
18537c478bd9Sstevel@tonic-gate 	if (Dflag) {
18547c478bd9Sstevel@tonic-gate 		(void) puts("mib_get_constants:");
18557c478bd9Sstevel@tonic-gate 		(void) printf("\tipv6IfStatsEntrySize %d\n",
18567c478bd9Sstevel@tonic-gate 		    ipv6IfStatsEntrySize);
18577c478bd9Sstevel@tonic-gate 		(void) printf("\tipAddrEntrySize %d\n", ipAddrEntrySize);
18587c478bd9Sstevel@tonic-gate 		(void) printf("\tipRouteEntrySize %d\n", ipRouteEntrySize);
18597c478bd9Sstevel@tonic-gate 		(void) printf("\tipNetToMediaEntrySize %d\n",
18607c478bd9Sstevel@tonic-gate 		    ipNetToMediaEntrySize);
18617c478bd9Sstevel@tonic-gate 		(void) printf("\tipMemberEntrySize %d\n", ipMemberEntrySize);
186245916cd2Sjpk 		(void) printf("\tipRouteAttributeSize %d\n",
186345916cd2Sjpk 		    ipRouteAttributeSize);
18647c478bd9Sstevel@tonic-gate 		(void) printf("\tvifctlSize %d\n", vifctlSize);
18657c478bd9Sstevel@tonic-gate 		(void) printf("\tmfcctlSize %d\n", mfcctlSize);
18667c478bd9Sstevel@tonic-gate 
18677c478bd9Sstevel@tonic-gate 		(void) printf("\tipv6AddrEntrySize %d\n", ipv6AddrEntrySize);
18687c478bd9Sstevel@tonic-gate 		(void) printf("\tipv6RouteEntrySize %d\n", ipv6RouteEntrySize);
18697c478bd9Sstevel@tonic-gate 		(void) printf("\tipv6NetToMediaEntrySize %d\n",
18707c478bd9Sstevel@tonic-gate 		    ipv6NetToMediaEntrySize);
18717c478bd9Sstevel@tonic-gate 		(void) printf("\tipv6MemberEntrySize %d\n",
18727c478bd9Sstevel@tonic-gate 		    ipv6MemberEntrySize);
18737c478bd9Sstevel@tonic-gate 		(void) printf("\tipv6IfIcmpEntrySize %d\n",
18747c478bd9Sstevel@tonic-gate 		    ipv6IfIcmpEntrySize);
187545916cd2Sjpk 		(void) printf("\ttransportMLPSize %d\n", transportMLPSize);
18767c478bd9Sstevel@tonic-gate 		(void) printf("\ttcpConnEntrySize %d\n", tcpConnEntrySize);
18777c478bd9Sstevel@tonic-gate 		(void) printf("\ttcp6ConnEntrySize %d\n", tcp6ConnEntrySize);
18787c478bd9Sstevel@tonic-gate 		(void) printf("\tudpEntrySize %d\n", udpEntrySize);
18797c478bd9Sstevel@tonic-gate 		(void) printf("\tudp6EntrySize %d\n", udp6EntrySize);
18807c478bd9Sstevel@tonic-gate 		(void) printf("\tsctpEntrySize %d\n", sctpEntrySize);
18817c478bd9Sstevel@tonic-gate 		(void) printf("\tsctpLocalEntrySize %d\n", sctpLocalEntrySize);
18827c478bd9Sstevel@tonic-gate 		(void) printf("\tsctpRemoteEntrySize %d\n",
18837c478bd9Sstevel@tonic-gate 		    sctpRemoteEntrySize);
18847c478bd9Sstevel@tonic-gate 	}
18857c478bd9Sstevel@tonic-gate }
18867c478bd9Sstevel@tonic-gate 
18877c478bd9Sstevel@tonic-gate 
18887c478bd9Sstevel@tonic-gate /* ----------------------------- STAT_REPORT ------------------------------- */
18897c478bd9Sstevel@tonic-gate 
18907c478bd9Sstevel@tonic-gate static void
18917c478bd9Sstevel@tonic-gate stat_report(mib_item_t *item)
18927c478bd9Sstevel@tonic-gate {
18937c478bd9Sstevel@tonic-gate 	int	jtemp = 0;
18947c478bd9Sstevel@tonic-gate 	char	ifname[LIFNAMSIZ + 1];
18957c478bd9Sstevel@tonic-gate 
18967c478bd9Sstevel@tonic-gate 	/* 'for' loop 1: */
18977c478bd9Sstevel@tonic-gate 	for (; item; item = item->next_item) {
18987c478bd9Sstevel@tonic-gate 		if (Dflag) {
18997c478bd9Sstevel@tonic-gate 			(void) printf("\n--- Entry %d ---\n", ++jtemp);
19007c478bd9Sstevel@tonic-gate 			(void) printf("Group = %d, mib_id = %d, "
19017c478bd9Sstevel@tonic-gate 			    "length = %d, valp = 0x%p\n",
19027c478bd9Sstevel@tonic-gate 			    item->group, item->mib_id,
19037c478bd9Sstevel@tonic-gate 			    item->length, item->valp);
19047c478bd9Sstevel@tonic-gate 		}
19057c478bd9Sstevel@tonic-gate 		if (item->mib_id != 0)
19067c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
19077c478bd9Sstevel@tonic-gate 
19087c478bd9Sstevel@tonic-gate 		switch (item->group) {
19097c478bd9Sstevel@tonic-gate 		case MIB2_IP: {
19107c478bd9Sstevel@tonic-gate 			mib2_ip_t	*ip = (mib2_ip_t *)item->valp;
19117c478bd9Sstevel@tonic-gate 
19127c478bd9Sstevel@tonic-gate 			if (protocol_selected(IPPROTO_IP) &&
19137c478bd9Sstevel@tonic-gate 			    family_selected(AF_INET)) {
19147c478bd9Sstevel@tonic-gate 				(void) fputs(v4compat ? "\nIP" : "\nIPv4",
19157c478bd9Sstevel@tonic-gate 				    stdout);
19167c478bd9Sstevel@tonic-gate 				print_ip_stats(ip);
19177c478bd9Sstevel@tonic-gate 			}
19187c478bd9Sstevel@tonic-gate 			break;
19197c478bd9Sstevel@tonic-gate 		}
19207c478bd9Sstevel@tonic-gate 		case MIB2_ICMP: {
19217c478bd9Sstevel@tonic-gate 			mib2_icmp_t	*icmp =
19227c478bd9Sstevel@tonic-gate 			    (mib2_icmp_t *)item->valp;
19237c478bd9Sstevel@tonic-gate 
19247c478bd9Sstevel@tonic-gate 			if (protocol_selected(IPPROTO_ICMP) &&
19257c478bd9Sstevel@tonic-gate 			    family_selected(AF_INET)) {
19267c478bd9Sstevel@tonic-gate 				(void) fputs(v4compat ? "\nICMP" : "\nICMPv4",
19277c478bd9Sstevel@tonic-gate 				    stdout);
19287c478bd9Sstevel@tonic-gate 				print_icmp_stats(icmp);
19297c478bd9Sstevel@tonic-gate 			}
19307c478bd9Sstevel@tonic-gate 			break;
19317c478bd9Sstevel@tonic-gate 		}
19327c478bd9Sstevel@tonic-gate 		case MIB2_IP6: {
19337c478bd9Sstevel@tonic-gate 			mib2_ipv6IfStatsEntry_t *ip6;
19347c478bd9Sstevel@tonic-gate 			mib2_ipv6IfStatsEntry_t sum6;
19357c478bd9Sstevel@tonic-gate 
19367c478bd9Sstevel@tonic-gate 			if (!(protocol_selected(IPPROTO_IPV6)) ||
19377c478bd9Sstevel@tonic-gate 			    !(family_selected(AF_INET6)))
19387c478bd9Sstevel@tonic-gate 				break;
19397c478bd9Sstevel@tonic-gate 			bzero(&sum6, sizeof (sum6));
19407c478bd9Sstevel@tonic-gate 			/* 'for' loop 2a: */
19417c478bd9Sstevel@tonic-gate 			for (ip6 = (mib2_ipv6IfStatsEntry_t *)item->valp;
1942e11c3f44Smeem 			    (char *)ip6 < (char *)item->valp + item->length;
19437c478bd9Sstevel@tonic-gate 			    /* LINTED: (note 1) */
19447c478bd9Sstevel@tonic-gate 			    ip6 = (mib2_ipv6IfStatsEntry_t *)((char *)ip6 +
19457c478bd9Sstevel@tonic-gate 			    ipv6IfStatsEntrySize)) {
19467c478bd9Sstevel@tonic-gate 				if (ip6->ipv6IfIndex == 0) {
19477c478bd9Sstevel@tonic-gate 					/*
19487c478bd9Sstevel@tonic-gate 					 * The "unknown interface" ip6
19497c478bd9Sstevel@tonic-gate 					 * mib. Just add to the sum.
19507c478bd9Sstevel@tonic-gate 					 */
19517c478bd9Sstevel@tonic-gate 					sum_ip6_stats(ip6, &sum6);
19527c478bd9Sstevel@tonic-gate 					continue; /* 'for' loop 2a */
19537c478bd9Sstevel@tonic-gate 				}
19547c478bd9Sstevel@tonic-gate 				if (Aflag) {
19557c478bd9Sstevel@tonic-gate 					(void) printf("\nIPv6 for %s\n",
1956e11c3f44Smeem 					    ifindex2str(ip6->ipv6IfIndex,
1957e11c3f44Smeem 					    ifname));
19587c478bd9Sstevel@tonic-gate 					print_ip6_stats(ip6);
19597c478bd9Sstevel@tonic-gate 				}
19607c478bd9Sstevel@tonic-gate 				sum_ip6_stats(ip6, &sum6);
19617c478bd9Sstevel@tonic-gate 			} /* 'for' loop 2a ends */
19627c478bd9Sstevel@tonic-gate 			(void) fputs("\nIPv6", stdout);
19637c478bd9Sstevel@tonic-gate 			print_ip6_stats(&sum6);
19647c478bd9Sstevel@tonic-gate 			break;
19657c478bd9Sstevel@tonic-gate 		}
19667c478bd9Sstevel@tonic-gate 		case MIB2_ICMP6: {
19677c478bd9Sstevel@tonic-gate 			mib2_ipv6IfIcmpEntry_t *icmp6;
19687c478bd9Sstevel@tonic-gate 			mib2_ipv6IfIcmpEntry_t sum6;
19697c478bd9Sstevel@tonic-gate 
19707c478bd9Sstevel@tonic-gate 			if (!(protocol_selected(IPPROTO_ICMPV6)) ||
19717c478bd9Sstevel@tonic-gate 			    !(family_selected(AF_INET6)))
19727c478bd9Sstevel@tonic-gate 				break;
19737c478bd9Sstevel@tonic-gate 			bzero(&sum6, sizeof (sum6));
19747c478bd9Sstevel@tonic-gate 			/* 'for' loop 2b: */
1975e11c3f44Smeem 			for (icmp6 = (mib2_ipv6IfIcmpEntry_t *)item->valp;
1976e11c3f44Smeem 			    (char *)icmp6 < (char *)item->valp + item->length;
1977e11c3f44Smeem 			    icmp6 = (void *)((char *)icmp6 +
1978e11c3f44Smeem 			    ipv6IfIcmpEntrySize)) {
19797c478bd9Sstevel@tonic-gate 				if (icmp6->ipv6IfIcmpIfIndex == 0) {
19807c478bd9Sstevel@tonic-gate 					/*
19817c478bd9Sstevel@tonic-gate 					 * The "unknown interface" icmp6
19827c478bd9Sstevel@tonic-gate 					 * mib. Just add to the sum.
19837c478bd9Sstevel@tonic-gate 					 */
19847c478bd9Sstevel@tonic-gate 					sum_icmp6_stats(icmp6, &sum6);
19857c478bd9Sstevel@tonic-gate 					continue; /* 'for' loop 2b: */
19867c478bd9Sstevel@tonic-gate 				}
19877c478bd9Sstevel@tonic-gate 				if (Aflag) {
1988e11c3f44Smeem 					(void) printf("\nICMPv6 for %s\n",
1989e11c3f44Smeem 					    ifindex2str(
1990e11c3f44Smeem 					    icmp6->ipv6IfIcmpIfIndex, ifname));
19917c478bd9Sstevel@tonic-gate 					print_icmp6_stats(icmp6);
19927c478bd9Sstevel@tonic-gate 				}
19937c478bd9Sstevel@tonic-gate 				sum_icmp6_stats(icmp6, &sum6);
19947c478bd9Sstevel@tonic-gate 			} /* 'for' loop 2b ends */
19957c478bd9Sstevel@tonic-gate 			(void) fputs("\nICMPv6", stdout);
19967c478bd9Sstevel@tonic-gate 			print_icmp6_stats(&sum6);
19977c478bd9Sstevel@tonic-gate 			break;
19987c478bd9Sstevel@tonic-gate 		}
19997c478bd9Sstevel@tonic-gate 		case MIB2_TCP: {
20007c478bd9Sstevel@tonic-gate 			mib2_tcp_t	*tcp = (mib2_tcp_t *)item->valp;
20017c478bd9Sstevel@tonic-gate 
20027c478bd9Sstevel@tonic-gate 			if (protocol_selected(IPPROTO_TCP) &&
20037c478bd9Sstevel@tonic-gate 			    (family_selected(AF_INET) ||
20047c478bd9Sstevel@tonic-gate 			    family_selected(AF_INET6))) {
20057c478bd9Sstevel@tonic-gate 				(void) fputs("\nTCP", stdout);
20067c478bd9Sstevel@tonic-gate 				print_tcp_stats(tcp);
20077c478bd9Sstevel@tonic-gate 			}
20087c478bd9Sstevel@tonic-gate 			break;
20097c478bd9Sstevel@tonic-gate 		}
20107c478bd9Sstevel@tonic-gate 		case MIB2_UDP: {
20117c478bd9Sstevel@tonic-gate 			mib2_udp_t	*udp = (mib2_udp_t *)item->valp;
20127c478bd9Sstevel@tonic-gate 
20137c478bd9Sstevel@tonic-gate 			if (protocol_selected(IPPROTO_UDP) &&
20147c478bd9Sstevel@tonic-gate 			    (family_selected(AF_INET) ||
20157c478bd9Sstevel@tonic-gate 			    family_selected(AF_INET6))) {
20167c478bd9Sstevel@tonic-gate 				(void) fputs("\nUDP", stdout);
20177c478bd9Sstevel@tonic-gate 				print_udp_stats(udp);
20187c478bd9Sstevel@tonic-gate 			}
20197c478bd9Sstevel@tonic-gate 			break;
20207c478bd9Sstevel@tonic-gate 		}
20217c478bd9Sstevel@tonic-gate 		case MIB2_SCTP: {
20227c478bd9Sstevel@tonic-gate 			mib2_sctp_t	*sctp = (mib2_sctp_t *)item->valp;
20237c478bd9Sstevel@tonic-gate 
20247c478bd9Sstevel@tonic-gate 			if (protocol_selected(IPPROTO_SCTP) &&
20257c478bd9Sstevel@tonic-gate 			    (family_selected(AF_INET) ||
20267c478bd9Sstevel@tonic-gate 			    family_selected(AF_INET6))) {
20277c478bd9Sstevel@tonic-gate 				(void) fputs("\nSCTP", stdout);
20287c478bd9Sstevel@tonic-gate 				print_sctp_stats(sctp);
20297c478bd9Sstevel@tonic-gate 			}
20307c478bd9Sstevel@tonic-gate 			break;
20317c478bd9Sstevel@tonic-gate 		}
20327c478bd9Sstevel@tonic-gate 		case EXPER_RAWIP: {
20337c478bd9Sstevel@tonic-gate 			mib2_rawip_t	*rawip =
20347c478bd9Sstevel@tonic-gate 			    (mib2_rawip_t *)item->valp;
20357c478bd9Sstevel@tonic-gate 
20367c478bd9Sstevel@tonic-gate 			if (protocol_selected(IPPROTO_RAW) &&
20377c478bd9Sstevel@tonic-gate 			    (family_selected(AF_INET) ||
20387c478bd9Sstevel@tonic-gate 			    family_selected(AF_INET6))) {
20397c478bd9Sstevel@tonic-gate 				(void) fputs("\nRAWIP", stdout);
20407c478bd9Sstevel@tonic-gate 				print_rawip_stats(rawip);
20417c478bd9Sstevel@tonic-gate 			}
20427c478bd9Sstevel@tonic-gate 			break;
20437c478bd9Sstevel@tonic-gate 		}
20447c478bd9Sstevel@tonic-gate 		case EXPER_IGMP: {
20457c478bd9Sstevel@tonic-gate 			struct igmpstat	*igps =
20467c478bd9Sstevel@tonic-gate 			    (struct igmpstat *)item->valp;
20477c478bd9Sstevel@tonic-gate 
20487c478bd9Sstevel@tonic-gate 			if (protocol_selected(IPPROTO_IGMP) &&
20497c478bd9Sstevel@tonic-gate 			    (family_selected(AF_INET))) {
20507c478bd9Sstevel@tonic-gate 				(void) fputs("\nIGMP:\n", stdout);
20517c478bd9Sstevel@tonic-gate 				print_igmp_stats(igps);
20527c478bd9Sstevel@tonic-gate 			}
20537c478bd9Sstevel@tonic-gate 			break;
20547c478bd9Sstevel@tonic-gate 		}
20557c478bd9Sstevel@tonic-gate 		}
20567c478bd9Sstevel@tonic-gate 	} /* 'for' loop 1 ends */
20577c478bd9Sstevel@tonic-gate 	(void) putchar('\n');
20587c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
20597c478bd9Sstevel@tonic-gate }
20607c478bd9Sstevel@tonic-gate 
20617c478bd9Sstevel@tonic-gate static void
20627c478bd9Sstevel@tonic-gate print_ip_stats(mib2_ip_t *ip)
20637c478bd9Sstevel@tonic-gate {
20647c478bd9Sstevel@tonic-gate 	prval_init();
20657c478bd9Sstevel@tonic-gate 	pr_int_val("ipForwarding",	ip->ipForwarding);
20667c478bd9Sstevel@tonic-gate 	pr_int_val("ipDefaultTTL",	ip->ipDefaultTTL);
20677c478bd9Sstevel@tonic-gate 	prval("ipInReceives",		ip->ipInReceives);
20687c478bd9Sstevel@tonic-gate 	prval("ipInHdrErrors",		ip->ipInHdrErrors);
20697c478bd9Sstevel@tonic-gate 	prval("ipInAddrErrors",		ip->ipInAddrErrors);
20707c478bd9Sstevel@tonic-gate 	prval("ipInCksumErrs",		ip->ipInCksumErrs);
20717c478bd9Sstevel@tonic-gate 	prval("ipForwDatagrams",	ip->ipForwDatagrams);
20727c478bd9Sstevel@tonic-gate 	prval("ipForwProhibits",	ip->ipForwProhibits);
20737c478bd9Sstevel@tonic-gate 	prval("ipInUnknownProtos",	ip->ipInUnknownProtos);
20747c478bd9Sstevel@tonic-gate 	prval("ipInDiscards",		ip->ipInDiscards);
20757c478bd9Sstevel@tonic-gate 	prval("ipInDelivers",		ip->ipInDelivers);
20767c478bd9Sstevel@tonic-gate 	prval("ipOutRequests",		ip->ipOutRequests);
20777c478bd9Sstevel@tonic-gate 	prval("ipOutDiscards",		ip->ipOutDiscards);
20787c478bd9Sstevel@tonic-gate 	prval("ipOutNoRoutes",		ip->ipOutNoRoutes);
20797c478bd9Sstevel@tonic-gate 	pr_int_val("ipReasmTimeout",	ip->ipReasmTimeout);
20807c478bd9Sstevel@tonic-gate 	prval("ipReasmReqds",		ip->ipReasmReqds);
20817c478bd9Sstevel@tonic-gate 	prval("ipReasmOKs",		ip->ipReasmOKs);
20827c478bd9Sstevel@tonic-gate 	prval("ipReasmFails",		ip->ipReasmFails);
20837c478bd9Sstevel@tonic-gate 	prval("ipReasmDuplicates",	ip->ipReasmDuplicates);
20847c478bd9Sstevel@tonic-gate 	prval("ipReasmPartDups",	ip->ipReasmPartDups);
20857c478bd9Sstevel@tonic-gate 	prval("ipFragOKs",		ip->ipFragOKs);
20867c478bd9Sstevel@tonic-gate 	prval("ipFragFails",		ip->ipFragFails);
20877c478bd9Sstevel@tonic-gate 	prval("ipFragCreates",		ip->ipFragCreates);
20887c478bd9Sstevel@tonic-gate 	prval("ipRoutingDiscards",	ip->ipRoutingDiscards);
20897c478bd9Sstevel@tonic-gate 
20907c478bd9Sstevel@tonic-gate 	prval("tcpInErrs",		ip->tcpInErrs);
20917c478bd9Sstevel@tonic-gate 	prval("udpNoPorts",		ip->udpNoPorts);
20927c478bd9Sstevel@tonic-gate 	prval("udpInCksumErrs",		ip->udpInCksumErrs);
20937c478bd9Sstevel@tonic-gate 	prval("udpInOverflows",		ip->udpInOverflows);
20947c478bd9Sstevel@tonic-gate 	prval("rawipInOverflows",	ip->rawipInOverflows);
20957c478bd9Sstevel@tonic-gate 	prval("ipsecInSucceeded",	ip->ipsecInSucceeded);
20967c478bd9Sstevel@tonic-gate 	prval("ipsecInFailed",		ip->ipsecInFailed);
20977c478bd9Sstevel@tonic-gate 	prval("ipInIPv6",		ip->ipInIPv6);
20987c478bd9Sstevel@tonic-gate 	prval("ipOutIPv6",		ip->ipOutIPv6);
20997c478bd9Sstevel@tonic-gate 	prval("ipOutSwitchIPv6",	ip->ipOutSwitchIPv6);
21007c478bd9Sstevel@tonic-gate 	prval_end();
21017c478bd9Sstevel@tonic-gate }
21027c478bd9Sstevel@tonic-gate 
21037c478bd9Sstevel@tonic-gate static void
21047c478bd9Sstevel@tonic-gate print_icmp_stats(mib2_icmp_t *icmp)
21057c478bd9Sstevel@tonic-gate {
21067c478bd9Sstevel@tonic-gate 	prval_init();
21077c478bd9Sstevel@tonic-gate 	prval("icmpInMsgs",		icmp->icmpInMsgs);
21087c478bd9Sstevel@tonic-gate 	prval("icmpInErrors",		icmp->icmpInErrors);
21097c478bd9Sstevel@tonic-gate 	prval("icmpInCksumErrs",	icmp->icmpInCksumErrs);
21107c478bd9Sstevel@tonic-gate 	prval("icmpInUnknowns",		icmp->icmpInUnknowns);
21117c478bd9Sstevel@tonic-gate 	prval("icmpInDestUnreachs",	icmp->icmpInDestUnreachs);
21127c478bd9Sstevel@tonic-gate 	prval("icmpInTimeExcds",	icmp->icmpInTimeExcds);
21137c478bd9Sstevel@tonic-gate 	prval("icmpInParmProbs",	icmp->icmpInParmProbs);
21147c478bd9Sstevel@tonic-gate 	prval("icmpInSrcQuenchs",	icmp->icmpInSrcQuenchs);
21157c478bd9Sstevel@tonic-gate 	prval("icmpInRedirects",	icmp->icmpInRedirects);
21167c478bd9Sstevel@tonic-gate 	prval("icmpInBadRedirects",	icmp->icmpInBadRedirects);
21177c478bd9Sstevel@tonic-gate 	prval("icmpInEchos",		icmp->icmpInEchos);
21187c478bd9Sstevel@tonic-gate 	prval("icmpInEchoReps",		icmp->icmpInEchoReps);
21197c478bd9Sstevel@tonic-gate 	prval("icmpInTimestamps",	icmp->icmpInTimestamps);
21207c478bd9Sstevel@tonic-gate 	prval("icmpInTimestampReps",	icmp->icmpInTimestampReps);
21217c478bd9Sstevel@tonic-gate 	prval("icmpInAddrMasks",	icmp->icmpInAddrMasks);
21227c478bd9Sstevel@tonic-gate 	prval("icmpInAddrMaskReps",	icmp->icmpInAddrMaskReps);
21237c478bd9Sstevel@tonic-gate 	prval("icmpInFragNeeded",	icmp->icmpInFragNeeded);
21247c478bd9Sstevel@tonic-gate 	prval("icmpOutMsgs",		icmp->icmpOutMsgs);
21257c478bd9Sstevel@tonic-gate 	prval("icmpOutDrops",		icmp->icmpOutDrops);
21267c478bd9Sstevel@tonic-gate 	prval("icmpOutErrors",		icmp->icmpOutErrors);
21277c478bd9Sstevel@tonic-gate 	prval("icmpOutDestUnreachs",	icmp->icmpOutDestUnreachs);
21287c478bd9Sstevel@tonic-gate 	prval("icmpOutTimeExcds",	icmp->icmpOutTimeExcds);
21297c478bd9Sstevel@tonic-gate 	prval("icmpOutParmProbs",	icmp->icmpOutParmProbs);
21307c478bd9Sstevel@tonic-gate 	prval("icmpOutSrcQuenchs",	icmp->icmpOutSrcQuenchs);
21317c478bd9Sstevel@tonic-gate 	prval("icmpOutRedirects",	icmp->icmpOutRedirects);
21327c478bd9Sstevel@tonic-gate 	prval("icmpOutEchos",		icmp->icmpOutEchos);
21337c478bd9Sstevel@tonic-gate 	prval("icmpOutEchoReps",	icmp->icmpOutEchoReps);
21347c478bd9Sstevel@tonic-gate 	prval("icmpOutTimestamps",	icmp->icmpOutTimestamps);
21357c478bd9Sstevel@tonic-gate 	prval("icmpOutTimestampReps",	icmp->icmpOutTimestampReps);
21367c478bd9Sstevel@tonic-gate 	prval("icmpOutAddrMasks",	icmp->icmpOutAddrMasks);
21377c478bd9Sstevel@tonic-gate 	prval("icmpOutAddrMaskReps",	icmp->icmpOutAddrMaskReps);
21387c478bd9Sstevel@tonic-gate 	prval("icmpOutFragNeeded",	icmp->icmpOutFragNeeded);
21397c478bd9Sstevel@tonic-gate 	prval("icmpInOverflows",	icmp->icmpInOverflows);
21407c478bd9Sstevel@tonic-gate 	prval_end();
21417c478bd9Sstevel@tonic-gate }
21427c478bd9Sstevel@tonic-gate 
21437c478bd9Sstevel@tonic-gate static void
21447c478bd9Sstevel@tonic-gate print_ip6_stats(mib2_ipv6IfStatsEntry_t *ip6)
21457c478bd9Sstevel@tonic-gate {
21467c478bd9Sstevel@tonic-gate 	prval_init();
21477c478bd9Sstevel@tonic-gate 	prval("ipv6Forwarding",		ip6->ipv6Forwarding);
21487c478bd9Sstevel@tonic-gate 	prval("ipv6DefaultHopLimit",	ip6->ipv6DefaultHopLimit);
21497c478bd9Sstevel@tonic-gate 
21507c478bd9Sstevel@tonic-gate 	prval("ipv6InReceives",		ip6->ipv6InReceives);
21517c478bd9Sstevel@tonic-gate 	prval("ipv6InHdrErrors",	ip6->ipv6InHdrErrors);
21527c478bd9Sstevel@tonic-gate 	prval("ipv6InTooBigErrors",	ip6->ipv6InTooBigErrors);
21537c478bd9Sstevel@tonic-gate 	prval("ipv6InNoRoutes",		ip6->ipv6InNoRoutes);
21547c478bd9Sstevel@tonic-gate 	prval("ipv6InAddrErrors",	ip6->ipv6InAddrErrors);
21557c478bd9Sstevel@tonic-gate 	prval("ipv6InUnknownProtos",	ip6->ipv6InUnknownProtos);
21567c478bd9Sstevel@tonic-gate 	prval("ipv6InTruncatedPkts",	ip6->ipv6InTruncatedPkts);
21577c478bd9Sstevel@tonic-gate 	prval("ipv6InDiscards",		ip6->ipv6InDiscards);
21587c478bd9Sstevel@tonic-gate 	prval("ipv6InDelivers",		ip6->ipv6InDelivers);
21597c478bd9Sstevel@tonic-gate 	prval("ipv6OutForwDatagrams",	ip6->ipv6OutForwDatagrams);
21607c478bd9Sstevel@tonic-gate 	prval("ipv6OutRequests",	ip6->ipv6OutRequests);
21617c478bd9Sstevel@tonic-gate 	prval("ipv6OutDiscards",	ip6->ipv6OutDiscards);
21627c478bd9Sstevel@tonic-gate 	prval("ipv6OutNoRoutes",	ip6->ipv6OutNoRoutes);
21637c478bd9Sstevel@tonic-gate 	prval("ipv6OutFragOKs",		ip6->ipv6OutFragOKs);
21647c478bd9Sstevel@tonic-gate 	prval("ipv6OutFragFails",	ip6->ipv6OutFragFails);
21657c478bd9Sstevel@tonic-gate 	prval("ipv6OutFragCreates",	ip6->ipv6OutFragCreates);
21667c478bd9Sstevel@tonic-gate 	prval("ipv6ReasmReqds",		ip6->ipv6ReasmReqds);
21677c478bd9Sstevel@tonic-gate 	prval("ipv6ReasmOKs",		ip6->ipv6ReasmOKs);
21687c478bd9Sstevel@tonic-gate 	prval("ipv6ReasmFails",		ip6->ipv6ReasmFails);
21697c478bd9Sstevel@tonic-gate 	prval("ipv6InMcastPkts",	ip6->ipv6InMcastPkts);
21707c478bd9Sstevel@tonic-gate 	prval("ipv6OutMcastPkts",	ip6->ipv6OutMcastPkts);
21717c478bd9Sstevel@tonic-gate 	prval("ipv6ReasmDuplicates",	ip6->ipv6ReasmDuplicates);
21727c478bd9Sstevel@tonic-gate 	prval("ipv6ReasmPartDups",	ip6->ipv6ReasmPartDups);
21737c478bd9Sstevel@tonic-gate 	prval("ipv6ForwProhibits",	ip6->ipv6ForwProhibits);
21747c478bd9Sstevel@tonic-gate 	prval("udpInCksumErrs",		ip6->udpInCksumErrs);
21757c478bd9Sstevel@tonic-gate 	prval("udpInOverflows",		ip6->udpInOverflows);
21767c478bd9Sstevel@tonic-gate 	prval("rawipInOverflows",	ip6->rawipInOverflows);
21777c478bd9Sstevel@tonic-gate 	prval("ipv6InIPv4",		ip6->ipv6InIPv4);
21787c478bd9Sstevel@tonic-gate 	prval("ipv6OutIPv4",		ip6->ipv6OutIPv4);
21797c478bd9Sstevel@tonic-gate 	prval("ipv6OutSwitchIPv4",	ip6->ipv6OutSwitchIPv4);
21807c478bd9Sstevel@tonic-gate 	prval_end();
21817c478bd9Sstevel@tonic-gate }
21827c478bd9Sstevel@tonic-gate 
21837c478bd9Sstevel@tonic-gate static void
21847c478bd9Sstevel@tonic-gate print_icmp6_stats(mib2_ipv6IfIcmpEntry_t *icmp6)
21857c478bd9Sstevel@tonic-gate {
21867c478bd9Sstevel@tonic-gate 	prval_init();
21877c478bd9Sstevel@tonic-gate 	prval("icmp6InMsgs",		icmp6->ipv6IfIcmpInMsgs);
21887c478bd9Sstevel@tonic-gate 	prval("icmp6InErrors",		icmp6->ipv6IfIcmpInErrors);
21897c478bd9Sstevel@tonic-gate 	prval("icmp6InDestUnreachs",	icmp6->ipv6IfIcmpInDestUnreachs);
21907c478bd9Sstevel@tonic-gate 	prval("icmp6InAdminProhibs",	icmp6->ipv6IfIcmpInAdminProhibs);
21917c478bd9Sstevel@tonic-gate 	prval("icmp6InTimeExcds",	icmp6->ipv6IfIcmpInTimeExcds);
21927c478bd9Sstevel@tonic-gate 	prval("icmp6InParmProblems",	icmp6->ipv6IfIcmpInParmProblems);
21937c478bd9Sstevel@tonic-gate 	prval("icmp6InPktTooBigs",	icmp6->ipv6IfIcmpInPktTooBigs);
21947c478bd9Sstevel@tonic-gate 	prval("icmp6InEchos",		icmp6->ipv6IfIcmpInEchos);
21957c478bd9Sstevel@tonic-gate 	prval("icmp6InEchoReplies",	icmp6->ipv6IfIcmpInEchoReplies);
21967c478bd9Sstevel@tonic-gate 	prval("icmp6InRouterSols",	icmp6->ipv6IfIcmpInRouterSolicits);
21977c478bd9Sstevel@tonic-gate 	prval("icmp6InRouterAds",
21987c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpInRouterAdvertisements);
21997c478bd9Sstevel@tonic-gate 	prval("icmp6InNeighborSols",	icmp6->ipv6IfIcmpInNeighborSolicits);
22007c478bd9Sstevel@tonic-gate 	prval("icmp6InNeighborAds",
22017c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpInNeighborAdvertisements);
22027c478bd9Sstevel@tonic-gate 	prval("icmp6InRedirects",	icmp6->ipv6IfIcmpInRedirects);
22037c478bd9Sstevel@tonic-gate 	prval("icmp6InBadRedirects",	icmp6->ipv6IfIcmpInBadRedirects);
22047c478bd9Sstevel@tonic-gate 	prval("icmp6InGroupQueries",	icmp6->ipv6IfIcmpInGroupMembQueries);
22057c478bd9Sstevel@tonic-gate 	prval("icmp6InGroupResps",	icmp6->ipv6IfIcmpInGroupMembResponses);
22067c478bd9Sstevel@tonic-gate 	prval("icmp6InGroupReds",	icmp6->ipv6IfIcmpInGroupMembReductions);
22077c478bd9Sstevel@tonic-gate 	prval("icmp6InOverflows",	icmp6->ipv6IfIcmpInOverflows);
22087c478bd9Sstevel@tonic-gate 	prval_end();
22097c478bd9Sstevel@tonic-gate 	prval_init();
22107c478bd9Sstevel@tonic-gate 	prval("icmp6OutMsgs",		icmp6->ipv6IfIcmpOutMsgs);
22117c478bd9Sstevel@tonic-gate 	prval("icmp6OutErrors",		icmp6->ipv6IfIcmpOutErrors);
22127c478bd9Sstevel@tonic-gate 	prval("icmp6OutDestUnreachs",	icmp6->ipv6IfIcmpOutDestUnreachs);
22137c478bd9Sstevel@tonic-gate 	prval("icmp6OutAdminProhibs",	icmp6->ipv6IfIcmpOutAdminProhibs);
22147c478bd9Sstevel@tonic-gate 	prval("icmp6OutTimeExcds",	icmp6->ipv6IfIcmpOutTimeExcds);
22157c478bd9Sstevel@tonic-gate 	prval("icmp6OutParmProblems",	icmp6->ipv6IfIcmpOutParmProblems);
22167c478bd9Sstevel@tonic-gate 	prval("icmp6OutPktTooBigs",	icmp6->ipv6IfIcmpOutPktTooBigs);
22177c478bd9Sstevel@tonic-gate 	prval("icmp6OutEchos",		icmp6->ipv6IfIcmpOutEchos);
22187c478bd9Sstevel@tonic-gate 	prval("icmp6OutEchoReplies",	icmp6->ipv6IfIcmpOutEchoReplies);
22197c478bd9Sstevel@tonic-gate 	prval("icmp6OutRouterSols",	icmp6->ipv6IfIcmpOutRouterSolicits);
22207c478bd9Sstevel@tonic-gate 	prval("icmp6OutRouterAds",
22217c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpOutRouterAdvertisements);
22227c478bd9Sstevel@tonic-gate 	prval("icmp6OutNeighborSols",	icmp6->ipv6IfIcmpOutNeighborSolicits);
22237c478bd9Sstevel@tonic-gate 	prval("icmp6OutNeighborAds",
22247c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpOutNeighborAdvertisements);
22257c478bd9Sstevel@tonic-gate 	prval("icmp6OutRedirects",	icmp6->ipv6IfIcmpOutRedirects);
22267c478bd9Sstevel@tonic-gate 	prval("icmp6OutGroupQueries",	icmp6->ipv6IfIcmpOutGroupMembQueries);
22277c478bd9Sstevel@tonic-gate 	prval("icmp6OutGroupResps",
22287c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpOutGroupMembResponses);
22297c478bd9Sstevel@tonic-gate 	prval("icmp6OutGroupReds",
22307c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpOutGroupMembReductions);
22317c478bd9Sstevel@tonic-gate 	prval_end();
22327c478bd9Sstevel@tonic-gate }
22337c478bd9Sstevel@tonic-gate 
22347c478bd9Sstevel@tonic-gate static void
22357c478bd9Sstevel@tonic-gate print_sctp_stats(mib2_sctp_t *sctp)
22367c478bd9Sstevel@tonic-gate {
22377c478bd9Sstevel@tonic-gate 	prval_init();
22387c478bd9Sstevel@tonic-gate 	pr_sctp_rtoalgo("sctpRtoAlgorithm", sctp->sctpRtoAlgorithm);
22397c478bd9Sstevel@tonic-gate 	prval("sctpRtoMin",		sctp->sctpRtoMin);
22407c478bd9Sstevel@tonic-gate 	prval("sctpRtoMax",		sctp->sctpRtoMax);
22417c478bd9Sstevel@tonic-gate 	prval("sctpRtoInitial",		sctp->sctpRtoInitial);
22427c478bd9Sstevel@tonic-gate 	pr_int_val("sctpMaxAssocs",	sctp->sctpMaxAssocs);
22437c478bd9Sstevel@tonic-gate 	prval("sctpValCookieLife",	sctp->sctpValCookieLife);
22447c478bd9Sstevel@tonic-gate 	prval("sctpMaxInitRetr",	sctp->sctpMaxInitRetr);
22457c478bd9Sstevel@tonic-gate 	prval("sctpCurrEstab",		sctp->sctpCurrEstab);
22467c478bd9Sstevel@tonic-gate 	prval("sctpActiveEstab",	sctp->sctpActiveEstab);
22477c478bd9Sstevel@tonic-gate 	prval("sctpPassiveEstab",	sctp->sctpPassiveEstab);
22487c478bd9Sstevel@tonic-gate 	prval("sctpAborted",		sctp->sctpAborted);
22497c478bd9Sstevel@tonic-gate 	prval("sctpShutdowns",		sctp->sctpShutdowns);
22507c478bd9Sstevel@tonic-gate 	prval("sctpOutOfBlue",		sctp->sctpOutOfBlue);
22517c478bd9Sstevel@tonic-gate 	prval("sctpChecksumError",	sctp->sctpChecksumError);
22527c478bd9Sstevel@tonic-gate 	prval64("sctpOutCtrlChunks",	sctp->sctpOutCtrlChunks);
22537c478bd9Sstevel@tonic-gate 	prval64("sctpOutOrderChunks",	sctp->sctpOutOrderChunks);
22547c478bd9Sstevel@tonic-gate 	prval64("sctpOutUnorderChunks",	sctp->sctpOutUnorderChunks);
22557c478bd9Sstevel@tonic-gate 	prval64("sctpRetransChunks",	sctp->sctpRetransChunks);
22567c478bd9Sstevel@tonic-gate 	prval("sctpOutAck",		sctp->sctpOutAck);
22577c478bd9Sstevel@tonic-gate 	prval("sctpOutAckDelayed",	sctp->sctpOutAckDelayed);
22587c478bd9Sstevel@tonic-gate 	prval("sctpOutWinUpdate",	sctp->sctpOutWinUpdate);
22597c478bd9Sstevel@tonic-gate 	prval("sctpOutFastRetrans",	sctp->sctpOutFastRetrans);
22607c478bd9Sstevel@tonic-gate 	prval("sctpOutWinProbe",	sctp->sctpOutWinProbe);
22617c478bd9Sstevel@tonic-gate 	prval64("sctpInCtrlChunks",	sctp->sctpInCtrlChunks);
22627c478bd9Sstevel@tonic-gate 	prval64("sctpInOrderChunks",	sctp->sctpInOrderChunks);
22637c478bd9Sstevel@tonic-gate 	prval64("sctpInUnorderChunks",	sctp->sctpInUnorderChunks);
22647c478bd9Sstevel@tonic-gate 	prval("sctpInAck",		sctp->sctpInAck);
22657c478bd9Sstevel@tonic-gate 	prval("sctpInDupAck",		sctp->sctpInDupAck);
22667c478bd9Sstevel@tonic-gate 	prval("sctpInAckUnsent",	sctp->sctpInAckUnsent);
22677c478bd9Sstevel@tonic-gate 	prval64("sctpFragUsrMsgs",	sctp->sctpFragUsrMsgs);
22687c478bd9Sstevel@tonic-gate 	prval64("sctpReasmUsrMsgs",	sctp->sctpReasmUsrMsgs);
22697c478bd9Sstevel@tonic-gate 	prval64("sctpOutSCTPPkts",	sctp->sctpOutSCTPPkts);
22707c478bd9Sstevel@tonic-gate 	prval64("sctpInSCTPPkts",	sctp->sctpInSCTPPkts);
22717c478bd9Sstevel@tonic-gate 	prval("sctpInInvalidCookie",	sctp->sctpInInvalidCookie);
22727c478bd9Sstevel@tonic-gate 	prval("sctpTimRetrans",		sctp->sctpTimRetrans);
22737c478bd9Sstevel@tonic-gate 	prval("sctpTimRetransDrop",	sctp->sctpTimRetransDrop);
22747c478bd9Sstevel@tonic-gate 	prval("sctpTimHearBeatProbe",	sctp->sctpTimHeartBeatProbe);
22757c478bd9Sstevel@tonic-gate 	prval("sctpTimHearBeatDrop",	sctp->sctpTimHeartBeatDrop);
22767c478bd9Sstevel@tonic-gate 	prval("sctpListenDrop",		sctp->sctpListenDrop);
22777c478bd9Sstevel@tonic-gate 	prval("sctpInClosed",		sctp->sctpInClosed);
22787c478bd9Sstevel@tonic-gate 	prval_end();
22797c478bd9Sstevel@tonic-gate }
22807c478bd9Sstevel@tonic-gate 
22817c478bd9Sstevel@tonic-gate static void
22827c478bd9Sstevel@tonic-gate print_tcp_stats(mib2_tcp_t *tcp)
22837c478bd9Sstevel@tonic-gate {
22847c478bd9Sstevel@tonic-gate 	prval_init();
22857c478bd9Sstevel@tonic-gate 	pr_int_val("tcpRtoAlgorithm",	tcp->tcpRtoAlgorithm);
22867c478bd9Sstevel@tonic-gate 	pr_int_val("tcpRtoMin",		tcp->tcpRtoMin);
22877c478bd9Sstevel@tonic-gate 	pr_int_val("tcpRtoMax",		tcp->tcpRtoMax);
22887c478bd9Sstevel@tonic-gate 	pr_int_val("tcpMaxConn",	tcp->tcpMaxConn);
22897c478bd9Sstevel@tonic-gate 	prval("tcpActiveOpens",		tcp->tcpActiveOpens);
22907c478bd9Sstevel@tonic-gate 	prval("tcpPassiveOpens",	tcp->tcpPassiveOpens);
22917c478bd9Sstevel@tonic-gate 	prval("tcpAttemptFails",	tcp->tcpAttemptFails);
22927c478bd9Sstevel@tonic-gate 	prval("tcpEstabResets",		tcp->tcpEstabResets);
22937c478bd9Sstevel@tonic-gate 	prval("tcpCurrEstab",		tcp->tcpCurrEstab);
22943173664eSapersson 	prval64("tcpOutSegs",		tcp->tcpHCOutSegs);
22957c478bd9Sstevel@tonic-gate 	prval("tcpOutDataSegs",		tcp->tcpOutDataSegs);
22967c478bd9Sstevel@tonic-gate 	prval("tcpOutDataBytes",	tcp->tcpOutDataBytes);
22977c478bd9Sstevel@tonic-gate 	prval("tcpRetransSegs",		tcp->tcpRetransSegs);
22987c478bd9Sstevel@tonic-gate 	prval("tcpRetransBytes",	tcp->tcpRetransBytes);
22997c478bd9Sstevel@tonic-gate 	prval("tcpOutAck",		tcp->tcpOutAck);
23007c478bd9Sstevel@tonic-gate 	prval("tcpOutAckDelayed",	tcp->tcpOutAckDelayed);
23017c478bd9Sstevel@tonic-gate 	prval("tcpOutUrg",		tcp->tcpOutUrg);
23027c478bd9Sstevel@tonic-gate 	prval("tcpOutWinUpdate",	tcp->tcpOutWinUpdate);
23037c478bd9Sstevel@tonic-gate 	prval("tcpOutWinProbe",		tcp->tcpOutWinProbe);
23047c478bd9Sstevel@tonic-gate 	prval("tcpOutControl",		tcp->tcpOutControl);
23057c478bd9Sstevel@tonic-gate 	prval("tcpOutRsts",		tcp->tcpOutRsts);
23067c478bd9Sstevel@tonic-gate 	prval("tcpOutFastRetrans",	tcp->tcpOutFastRetrans);
23073173664eSapersson 	prval64("tcpInSegs",		tcp->tcpHCInSegs);
23087c478bd9Sstevel@tonic-gate 	prval_end();
23097c478bd9Sstevel@tonic-gate 	prval("tcpInAckSegs",		tcp->tcpInAckSegs);
23107c478bd9Sstevel@tonic-gate 	prval("tcpInAckBytes",		tcp->tcpInAckBytes);
23117c478bd9Sstevel@tonic-gate 	prval("tcpInDupAck",		tcp->tcpInDupAck);
23127c478bd9Sstevel@tonic-gate 	prval("tcpInAckUnsent",		tcp->tcpInAckUnsent);
23137c478bd9Sstevel@tonic-gate 	prval("tcpInInorderSegs",	tcp->tcpInDataInorderSegs);
23147c478bd9Sstevel@tonic-gate 	prval("tcpInInorderBytes",	tcp->tcpInDataInorderBytes);
23157c478bd9Sstevel@tonic-gate 	prval("tcpInUnorderSegs",	tcp->tcpInDataUnorderSegs);
23167c478bd9Sstevel@tonic-gate 	prval("tcpInUnorderBytes",	tcp->tcpInDataUnorderBytes);
23177c478bd9Sstevel@tonic-gate 	prval("tcpInDupSegs",		tcp->tcpInDataDupSegs);
23187c478bd9Sstevel@tonic-gate 	prval("tcpInDupBytes",		tcp->tcpInDataDupBytes);
23197c478bd9Sstevel@tonic-gate 	prval("tcpInPartDupSegs",	tcp->tcpInDataPartDupSegs);
23207c478bd9Sstevel@tonic-gate 	prval("tcpInPartDupBytes",	tcp->tcpInDataPartDupBytes);
23217c478bd9Sstevel@tonic-gate 	prval("tcpInPastWinSegs",	tcp->tcpInDataPastWinSegs);
23227c478bd9Sstevel@tonic-gate 	prval("tcpInPastWinBytes",	tcp->tcpInDataPastWinBytes);
23237c478bd9Sstevel@tonic-gate 	prval("tcpInWinProbe",		tcp->tcpInWinProbe);
23247c478bd9Sstevel@tonic-gate 	prval("tcpInWinUpdate",		tcp->tcpInWinUpdate);
23257c478bd9Sstevel@tonic-gate 	prval("tcpInClosed",		tcp->tcpInClosed);
23267c478bd9Sstevel@tonic-gate 	prval("tcpRttNoUpdate",		tcp->tcpRttNoUpdate);
23277c478bd9Sstevel@tonic-gate 	prval("tcpRttUpdate",		tcp->tcpRttUpdate);
23287c478bd9Sstevel@tonic-gate 	prval("tcpTimRetrans",		tcp->tcpTimRetrans);
23297c478bd9Sstevel@tonic-gate 	prval("tcpTimRetransDrop",	tcp->tcpTimRetransDrop);
23307c478bd9Sstevel@tonic-gate 	prval("tcpTimKeepalive",	tcp->tcpTimKeepalive);
23317c478bd9Sstevel@tonic-gate 	prval("tcpTimKeepaliveProbe",	tcp->tcpTimKeepaliveProbe);
23327c478bd9Sstevel@tonic-gate 	prval("tcpTimKeepaliveDrop",	tcp->tcpTimKeepaliveDrop);
23337c478bd9Sstevel@tonic-gate 	prval("tcpListenDrop",		tcp->tcpListenDrop);
23347c478bd9Sstevel@tonic-gate 	prval("tcpListenDropQ0",	tcp->tcpListenDropQ0);
23357c478bd9Sstevel@tonic-gate 	prval("tcpHalfOpenDrop",	tcp->tcpHalfOpenDrop);
23367c478bd9Sstevel@tonic-gate 	prval("tcpOutSackRetrans",	tcp->tcpOutSackRetransSegs);
23377c478bd9Sstevel@tonic-gate 	prval_end();
23387c478bd9Sstevel@tonic-gate 
23397c478bd9Sstevel@tonic-gate }
23407c478bd9Sstevel@tonic-gate 
23417c478bd9Sstevel@tonic-gate static void
23427c478bd9Sstevel@tonic-gate print_udp_stats(mib2_udp_t *udp)
23437c478bd9Sstevel@tonic-gate {
23447c478bd9Sstevel@tonic-gate 	prval_init();
23453173664eSapersson 	prval64("udpInDatagrams",	udp->udpHCInDatagrams);
23467c478bd9Sstevel@tonic-gate 	prval("udpInErrors",		udp->udpInErrors);
23473173664eSapersson 	prval64("udpOutDatagrams",	udp->udpHCOutDatagrams);
23487c478bd9Sstevel@tonic-gate 	prval("udpOutErrors",		udp->udpOutErrors);
23497c478bd9Sstevel@tonic-gate 	prval_end();
23507c478bd9Sstevel@tonic-gate }
23517c478bd9Sstevel@tonic-gate 
23527c478bd9Sstevel@tonic-gate static void
23537c478bd9Sstevel@tonic-gate print_rawip_stats(mib2_rawip_t *rawip)
23547c478bd9Sstevel@tonic-gate {
23557c478bd9Sstevel@tonic-gate 	prval_init();
23567c478bd9Sstevel@tonic-gate 	prval("rawipInDatagrams",	rawip->rawipInDatagrams);
23577c478bd9Sstevel@tonic-gate 	prval("rawipInErrors",		rawip->rawipInErrors);
23587c478bd9Sstevel@tonic-gate 	prval("rawipInCksumErrs",	rawip->rawipInCksumErrs);
23597c478bd9Sstevel@tonic-gate 	prval("rawipOutDatagrams",	rawip->rawipOutDatagrams);
23607c478bd9Sstevel@tonic-gate 	prval("rawipOutErrors",		rawip->rawipOutErrors);
23617c478bd9Sstevel@tonic-gate 	prval_end();
23627c478bd9Sstevel@tonic-gate }
23637c478bd9Sstevel@tonic-gate 
23647c478bd9Sstevel@tonic-gate void
23657c478bd9Sstevel@tonic-gate print_igmp_stats(struct igmpstat *igps)
23667c478bd9Sstevel@tonic-gate {
23677c478bd9Sstevel@tonic-gate 	(void) printf(" %10u message%s received\n",
23687c478bd9Sstevel@tonic-gate 	    igps->igps_rcv_total, PLURAL(igps->igps_rcv_total));
23697c478bd9Sstevel@tonic-gate 	(void) printf(" %10u message%s received with too few bytes\n",
23707c478bd9Sstevel@tonic-gate 	    igps->igps_rcv_tooshort, PLURAL(igps->igps_rcv_tooshort));
23717c478bd9Sstevel@tonic-gate 	(void) printf(" %10u message%s received with bad checksum\n",
23727c478bd9Sstevel@tonic-gate 	    igps->igps_rcv_badsum, PLURAL(igps->igps_rcv_badsum));
23737c478bd9Sstevel@tonic-gate 	(void) printf(" %10u membership quer%s received\n",
23747c478bd9Sstevel@tonic-gate 	    igps->igps_rcv_queries, PLURALY(igps->igps_rcv_queries));
23757c478bd9Sstevel@tonic-gate 	(void) printf(" %10u membership quer%s received with invalid "
23767c478bd9Sstevel@tonic-gate 	    "field(s)\n",
23777c478bd9Sstevel@tonic-gate 	    igps->igps_rcv_badqueries, PLURALY(igps->igps_rcv_badqueries));
23787c478bd9Sstevel@tonic-gate 	(void) printf(" %10u membership report%s received\n",
23797c478bd9Sstevel@tonic-gate 	    igps->igps_rcv_reports, PLURAL(igps->igps_rcv_reports));
23807c478bd9Sstevel@tonic-gate 	(void) printf(" %10u membership report%s received with invalid "
23817c478bd9Sstevel@tonic-gate 	    "field(s)\n",
23827c478bd9Sstevel@tonic-gate 	    igps->igps_rcv_badreports, PLURAL(igps->igps_rcv_badreports));
23837c478bd9Sstevel@tonic-gate 	(void) printf(" %10u membership report%s received for groups to "
23847c478bd9Sstevel@tonic-gate 	    "which we belong\n",
23857c478bd9Sstevel@tonic-gate 	    igps->igps_rcv_ourreports, PLURAL(igps->igps_rcv_ourreports));
23867c478bd9Sstevel@tonic-gate 	(void) printf(" %10u membership report%s sent\n",
23877c478bd9Sstevel@tonic-gate 	    igps->igps_snd_reports, PLURAL(igps->igps_snd_reports));
23887c478bd9Sstevel@tonic-gate }
23897c478bd9Sstevel@tonic-gate 
23907c478bd9Sstevel@tonic-gate static void
23917c478bd9Sstevel@tonic-gate print_mrt_stats(struct mrtstat *mrts)
23927c478bd9Sstevel@tonic-gate {
23937c478bd9Sstevel@tonic-gate 	(void) puts("DVMRP multicast routing:");
23947c478bd9Sstevel@tonic-gate 	(void) printf(" %10u hit%s - kernel forwarding cache hits\n",
2395e11c3f44Smeem 	    mrts->mrts_mfc_hits, PLURAL(mrts->mrts_mfc_hits));
23967c478bd9Sstevel@tonic-gate 	(void) printf(" %10u miss%s - kernel forwarding cache misses\n",
2397e11c3f44Smeem 	    mrts->mrts_mfc_misses, PLURALES(mrts->mrts_mfc_misses));
23987c478bd9Sstevel@tonic-gate 	(void) printf(" %10u packet%s potentially forwarded\n",
2399e11c3f44Smeem 	    mrts->mrts_fwd_in, PLURAL(mrts->mrts_fwd_in));
24007c478bd9Sstevel@tonic-gate 	(void) printf(" %10u packet%s actually sent out\n",
2401e11c3f44Smeem 	    mrts->mrts_fwd_out, PLURAL(mrts->mrts_fwd_out));
24027c478bd9Sstevel@tonic-gate 	(void) printf(" %10u upcall%s - upcalls made to mrouted\n",
2403e11c3f44Smeem 	    mrts->mrts_upcalls, PLURAL(mrts->mrts_upcalls));
24047c478bd9Sstevel@tonic-gate 	(void) printf(" %10u packet%s not sent out due to lack of resources\n",
2405e11c3f44Smeem 	    mrts->mrts_fwd_drop, PLURAL(mrts->mrts_fwd_drop));
24067c478bd9Sstevel@tonic-gate 	(void) printf(" %10u datagram%s with malformed tunnel options\n",
2407e11c3f44Smeem 	    mrts->mrts_bad_tunnel, PLURAL(mrts->mrts_bad_tunnel));
24087c478bd9Sstevel@tonic-gate 	(void) printf(" %10u datagram%s with no room for tunnel options\n",
2409e11c3f44Smeem 	    mrts->mrts_cant_tunnel, PLURAL(mrts->mrts_cant_tunnel));
24107c478bd9Sstevel@tonic-gate 	(void) printf(" %10u datagram%s arrived on wrong interface\n",
2411e11c3f44Smeem 	    mrts->mrts_wrong_if, PLURAL(mrts->mrts_wrong_if));
24127c478bd9Sstevel@tonic-gate 	(void) printf(" %10u datagram%s dropped due to upcall Q overflow\n",
2413e11c3f44Smeem 	    mrts->mrts_upq_ovflw, PLURAL(mrts->mrts_upq_ovflw));
24147c478bd9Sstevel@tonic-gate 	(void) printf(" %10u datagram%s cleaned up by the cache\n",
2415e11c3f44Smeem 	    mrts->mrts_cache_cleanups, PLURAL(mrts->mrts_cache_cleanups));
24167c478bd9Sstevel@tonic-gate 	(void) printf(" %10u datagram%s dropped selectively by ratelimiter\n",
2417e11c3f44Smeem 	    mrts->mrts_drop_sel, PLURAL(mrts->mrts_drop_sel));
24187c478bd9Sstevel@tonic-gate 	(void) printf(" %10u datagram%s dropped - bucket Q overflow\n",
2419e11c3f44Smeem 	    mrts->mrts_q_overflow, PLURAL(mrts->mrts_q_overflow));
24207c478bd9Sstevel@tonic-gate 	(void) printf(" %10u datagram%s dropped - larger than bkt size\n",
2421e11c3f44Smeem 	    mrts->mrts_pkt2large, PLURAL(mrts->mrts_pkt2large));
24227c478bd9Sstevel@tonic-gate 	(void) printf("\nPIM multicast routing:\n");
24237c478bd9Sstevel@tonic-gate 	(void) printf(" %10u datagram%s dropped - bad version number\n",
2424e11c3f44Smeem 	    mrts->mrts_pim_badversion, PLURAL(mrts->mrts_pim_badversion));
24257c478bd9Sstevel@tonic-gate 	(void) printf(" %10u datagram%s dropped - bad checksum\n",
2426e11c3f44Smeem 	    mrts->mrts_pim_rcv_badcsum, PLURAL(mrts->mrts_pim_rcv_badcsum));
24277c478bd9Sstevel@tonic-gate 	(void) printf(" %10u datagram%s dropped - bad register packets\n",
2428e11c3f44Smeem 	    mrts->mrts_pim_badregisters, PLURAL(mrts->mrts_pim_badregisters));
24297c478bd9Sstevel@tonic-gate 	(void) printf(
2430e11c3f44Smeem 	    " %10u datagram%s potentially forwarded - register packets\n",
2431e11c3f44Smeem 	    mrts->mrts_pim_regforwards, PLURAL(mrts->mrts_pim_regforwards));
24327c478bd9Sstevel@tonic-gate 	(void) printf(" %10u datagram%s dropped - register send drops\n",
2433e11c3f44Smeem 	    mrts->mrts_pim_regsend_drops, PLURAL(mrts->mrts_pim_regsend_drops));
24347c478bd9Sstevel@tonic-gate 	(void) printf(" %10u datagram%s dropped - packet malformed\n",
2435e11c3f44Smeem 	    mrts->mrts_pim_malformed, PLURAL(mrts->mrts_pim_malformed));
24367c478bd9Sstevel@tonic-gate 	(void) printf(" %10u datagram%s dropped - no memory to forward\n",
2437e11c3f44Smeem 	    mrts->mrts_pim_nomemory, PLURAL(mrts->mrts_pim_nomemory));
24387c478bd9Sstevel@tonic-gate }
24397c478bd9Sstevel@tonic-gate 
24407c478bd9Sstevel@tonic-gate static void
24417c478bd9Sstevel@tonic-gate sum_ip6_stats(mib2_ipv6IfStatsEntry_t *ip6, mib2_ipv6IfStatsEntry_t *sum6)
24427c478bd9Sstevel@tonic-gate {
24437c478bd9Sstevel@tonic-gate 	/* First few are not additive */
24447c478bd9Sstevel@tonic-gate 	sum6->ipv6Forwarding = ip6->ipv6Forwarding;
24457c478bd9Sstevel@tonic-gate 	sum6->ipv6DefaultHopLimit = ip6->ipv6DefaultHopLimit;
24467c478bd9Sstevel@tonic-gate 
24477c478bd9Sstevel@tonic-gate 	sum6->ipv6InReceives += ip6->ipv6InReceives;
24487c478bd9Sstevel@tonic-gate 	sum6->ipv6InHdrErrors += ip6->ipv6InHdrErrors;
24497c478bd9Sstevel@tonic-gate 	sum6->ipv6InTooBigErrors += ip6->ipv6InTooBigErrors;
24507c478bd9Sstevel@tonic-gate 	sum6->ipv6InNoRoutes += ip6->ipv6InNoRoutes;
24517c478bd9Sstevel@tonic-gate 	sum6->ipv6InAddrErrors += ip6->ipv6InAddrErrors;
24527c478bd9Sstevel@tonic-gate 	sum6->ipv6InUnknownProtos += ip6->ipv6InUnknownProtos;
24537c478bd9Sstevel@tonic-gate 	sum6->ipv6InTruncatedPkts += ip6->ipv6InTruncatedPkts;
24547c478bd9Sstevel@tonic-gate 	sum6->ipv6InDiscards += ip6->ipv6InDiscards;
24557c478bd9Sstevel@tonic-gate 	sum6->ipv6InDelivers += ip6->ipv6InDelivers;
24567c478bd9Sstevel@tonic-gate 	sum6->ipv6OutForwDatagrams += ip6->ipv6OutForwDatagrams;
24577c478bd9Sstevel@tonic-gate 	sum6->ipv6OutRequests += ip6->ipv6OutRequests;
24587c478bd9Sstevel@tonic-gate 	sum6->ipv6OutDiscards += ip6->ipv6OutDiscards;
24597c478bd9Sstevel@tonic-gate 	sum6->ipv6OutFragOKs += ip6->ipv6OutFragOKs;
24607c478bd9Sstevel@tonic-gate 	sum6->ipv6OutFragFails += ip6->ipv6OutFragFails;
24617c478bd9Sstevel@tonic-gate 	sum6->ipv6OutFragCreates += ip6->ipv6OutFragCreates;
24627c478bd9Sstevel@tonic-gate 	sum6->ipv6ReasmReqds += ip6->ipv6ReasmReqds;
24637c478bd9Sstevel@tonic-gate 	sum6->ipv6ReasmOKs += ip6->ipv6ReasmOKs;
24647c478bd9Sstevel@tonic-gate 	sum6->ipv6ReasmFails += ip6->ipv6ReasmFails;
24657c478bd9Sstevel@tonic-gate 	sum6->ipv6InMcastPkts += ip6->ipv6InMcastPkts;
24667c478bd9Sstevel@tonic-gate 	sum6->ipv6OutMcastPkts += ip6->ipv6OutMcastPkts;
24677c478bd9Sstevel@tonic-gate 	sum6->ipv6OutNoRoutes += ip6->ipv6OutNoRoutes;
24687c478bd9Sstevel@tonic-gate 	sum6->ipv6ReasmDuplicates += ip6->ipv6ReasmDuplicates;
24697c478bd9Sstevel@tonic-gate 	sum6->ipv6ReasmPartDups += ip6->ipv6ReasmPartDups;
24707c478bd9Sstevel@tonic-gate 	sum6->ipv6ForwProhibits += ip6->ipv6ForwProhibits;
24717c478bd9Sstevel@tonic-gate 	sum6->udpInCksumErrs += ip6->udpInCksumErrs;
24727c478bd9Sstevel@tonic-gate 	sum6->udpInOverflows += ip6->udpInOverflows;
24737c478bd9Sstevel@tonic-gate 	sum6->rawipInOverflows += ip6->rawipInOverflows;
24747c478bd9Sstevel@tonic-gate }
24757c478bd9Sstevel@tonic-gate 
24767c478bd9Sstevel@tonic-gate static void
24777c478bd9Sstevel@tonic-gate sum_icmp6_stats(mib2_ipv6IfIcmpEntry_t *icmp6, mib2_ipv6IfIcmpEntry_t *sum6)
24787c478bd9Sstevel@tonic-gate {
24797c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInMsgs += icmp6->ipv6IfIcmpInMsgs;
24807c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInErrors += icmp6->ipv6IfIcmpInErrors;
24817c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInDestUnreachs += icmp6->ipv6IfIcmpInDestUnreachs;
24827c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInAdminProhibs += icmp6->ipv6IfIcmpInAdminProhibs;
24837c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInTimeExcds += icmp6->ipv6IfIcmpInTimeExcds;
24847c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInParmProblems += icmp6->ipv6IfIcmpInParmProblems;
24857c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInPktTooBigs += icmp6->ipv6IfIcmpInPktTooBigs;
24867c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInEchos += icmp6->ipv6IfIcmpInEchos;
24877c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInEchoReplies += icmp6->ipv6IfIcmpInEchoReplies;
24887c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInRouterSolicits += icmp6->ipv6IfIcmpInRouterSolicits;
24897c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInRouterAdvertisements +=
24907c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpInRouterAdvertisements;
24917c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInNeighborSolicits +=
24927c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpInNeighborSolicits;
24937c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInNeighborAdvertisements +=
24947c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpInNeighborAdvertisements;
24957c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInRedirects += icmp6->ipv6IfIcmpInRedirects;
24967c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInGroupMembQueries +=
24977c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpInGroupMembQueries;
24987c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInGroupMembResponses +=
24997c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpInGroupMembResponses;
25007c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInGroupMembReductions +=
25017c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpInGroupMembReductions;
25027c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutMsgs += icmp6->ipv6IfIcmpOutMsgs;
25037c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutErrors += icmp6->ipv6IfIcmpOutErrors;
25047c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutDestUnreachs += icmp6->ipv6IfIcmpOutDestUnreachs;
25057c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutAdminProhibs += icmp6->ipv6IfIcmpOutAdminProhibs;
25067c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutTimeExcds += icmp6->ipv6IfIcmpOutTimeExcds;
25077c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutParmProblems += icmp6->ipv6IfIcmpOutParmProblems;
25087c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutPktTooBigs += icmp6->ipv6IfIcmpOutPktTooBigs;
25097c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutEchos += icmp6->ipv6IfIcmpOutEchos;
25107c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutEchoReplies += icmp6->ipv6IfIcmpOutEchoReplies;
25117c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutRouterSolicits +=
25127c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpOutRouterSolicits;
25137c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutRouterAdvertisements +=
25147c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpOutRouterAdvertisements;
25157c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutNeighborSolicits +=
25167c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpOutNeighborSolicits;
25177c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutNeighborAdvertisements +=
25187c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpOutNeighborAdvertisements;
25197c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutRedirects += icmp6->ipv6IfIcmpOutRedirects;
25207c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutGroupMembQueries +=
25217c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpOutGroupMembQueries;
25227c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutGroupMembResponses +=
25237c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpOutGroupMembResponses;
25247c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutGroupMembReductions +=
25257c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpOutGroupMembReductions;
25267c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInOverflows += icmp6->ipv6IfIcmpInOverflows;
25277c478bd9Sstevel@tonic-gate }
25287c478bd9Sstevel@tonic-gate 
25297c478bd9Sstevel@tonic-gate /* ----------------------------- MRT_STAT_REPORT --------------------------- */
25307c478bd9Sstevel@tonic-gate 
25317c478bd9Sstevel@tonic-gate static void
25327c478bd9Sstevel@tonic-gate mrt_stat_report(mib_item_t *curritem)
25337c478bd9Sstevel@tonic-gate {
25347c478bd9Sstevel@tonic-gate 	int	jtemp = 0;
25357c478bd9Sstevel@tonic-gate 	mib_item_t *tempitem;
25367c478bd9Sstevel@tonic-gate 
25377c478bd9Sstevel@tonic-gate 	if (!(family_selected(AF_INET)))
25387c478bd9Sstevel@tonic-gate 		return;
25397c478bd9Sstevel@tonic-gate 
25407c478bd9Sstevel@tonic-gate 	(void) putchar('\n');
25417c478bd9Sstevel@tonic-gate 	/* 'for' loop 1: */
25427c478bd9Sstevel@tonic-gate 	for (tempitem = curritem;
25437c478bd9Sstevel@tonic-gate 	    tempitem;
25447c478bd9Sstevel@tonic-gate 	    tempitem = tempitem->next_item) {
25457c478bd9Sstevel@tonic-gate 		if (Dflag) {
25467c478bd9Sstevel@tonic-gate 			(void) printf("\n--- Entry %d ---\n", ++jtemp);
25477c478bd9Sstevel@tonic-gate 			(void) printf("Group = %d, mib_id = %d, "
25487c478bd9Sstevel@tonic-gate 			    "length = %d, valp = 0x%p\n",
25497c478bd9Sstevel@tonic-gate 			    tempitem->group, tempitem->mib_id,
25507c478bd9Sstevel@tonic-gate 			    tempitem->length, tempitem->valp);
25517c478bd9Sstevel@tonic-gate 		}
25527c478bd9Sstevel@tonic-gate 
25537c478bd9Sstevel@tonic-gate 		if (tempitem->mib_id == 0) {
25547c478bd9Sstevel@tonic-gate 			switch (tempitem->group) {
25557c478bd9Sstevel@tonic-gate 			case EXPER_DVMRP: {
25567c478bd9Sstevel@tonic-gate 				struct mrtstat	*mrts;
25577c478bd9Sstevel@tonic-gate 				mrts = (struct mrtstat *)tempitem->valp;
25587c478bd9Sstevel@tonic-gate 
25597c478bd9Sstevel@tonic-gate 				if (!(family_selected(AF_INET)))
25607c478bd9Sstevel@tonic-gate 					continue; /* 'for' loop 1 */
25617c478bd9Sstevel@tonic-gate 
25627c478bd9Sstevel@tonic-gate 				print_mrt_stats(mrts);
25637c478bd9Sstevel@tonic-gate 				break;
25647c478bd9Sstevel@tonic-gate 			}
25657c478bd9Sstevel@tonic-gate 			}
25667c478bd9Sstevel@tonic-gate 		}
25677c478bd9Sstevel@tonic-gate 	} /* 'for' loop 1 ends */
25687c478bd9Sstevel@tonic-gate 	(void) putchar('\n');
25697c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
25707c478bd9Sstevel@tonic-gate }
25717c478bd9Sstevel@tonic-gate 
25727c478bd9Sstevel@tonic-gate /*
25737c478bd9Sstevel@tonic-gate  * if_stat_total() - Computes totals for interface statistics
25747c478bd9Sstevel@tonic-gate  *                   and returns result by updating sumstats.
25757c478bd9Sstevel@tonic-gate  */
25767c478bd9Sstevel@tonic-gate static void
25777c478bd9Sstevel@tonic-gate if_stat_total(struct ifstat *oldstats, struct ifstat *newstats,
25787c478bd9Sstevel@tonic-gate     struct ifstat *sumstats)
25797c478bd9Sstevel@tonic-gate {
25807c478bd9Sstevel@tonic-gate 	sumstats->ipackets += newstats->ipackets - oldstats->ipackets;
25817c478bd9Sstevel@tonic-gate 	sumstats->opackets += newstats->opackets - oldstats->opackets;
25827c478bd9Sstevel@tonic-gate 	sumstats->ierrors += newstats->ierrors - oldstats->ierrors;
25837c478bd9Sstevel@tonic-gate 	sumstats->oerrors += newstats->oerrors - oldstats->oerrors;
25847c478bd9Sstevel@tonic-gate 	sumstats->collisions += newstats->collisions - oldstats->collisions;
25857c478bd9Sstevel@tonic-gate }
25867c478bd9Sstevel@tonic-gate 
25877c478bd9Sstevel@tonic-gate /* --------------------- IF_REPORT (netstat -i)  -------------------------- */
25887c478bd9Sstevel@tonic-gate 
25897c478bd9Sstevel@tonic-gate static struct	ifstat	zerostat = {
25907c478bd9Sstevel@tonic-gate 	0LL, 0LL, 0LL, 0LL, 0LL
25917c478bd9Sstevel@tonic-gate };
25927c478bd9Sstevel@tonic-gate 
25937c478bd9Sstevel@tonic-gate static void
25947c478bd9Sstevel@tonic-gate if_report(mib_item_t *item, char *matchname,
25957c478bd9Sstevel@tonic-gate     int Iflag_only, boolean_t once_only)
25967c478bd9Sstevel@tonic-gate {
25977c478bd9Sstevel@tonic-gate 	static boolean_t	reentry = B_FALSE;
25987c478bd9Sstevel@tonic-gate 	boolean_t		alreadydone = B_FALSE;
25997c478bd9Sstevel@tonic-gate 	int			jtemp = 0;
26007c478bd9Sstevel@tonic-gate 	uint32_t		ifindex_v4 = 0;
26017c478bd9Sstevel@tonic-gate 	uint32_t		ifindex_v6 = 0;
2602ba753d4aSkeerthi 	boolean_t		first_header = B_TRUE;
26037c478bd9Sstevel@tonic-gate 
26047c478bd9Sstevel@tonic-gate 	/* 'for' loop 1: */
26057c478bd9Sstevel@tonic-gate 	for (; item; item = item->next_item) {
26067c478bd9Sstevel@tonic-gate 		if (Dflag) {
26077c478bd9Sstevel@tonic-gate 			(void) printf("\n--- Entry %d ---\n", ++jtemp);
26087c478bd9Sstevel@tonic-gate 			(void) printf("Group = %d, mib_id = %d, "
26097c478bd9Sstevel@tonic-gate 			    "length = %d, valp = 0x%p\n",
26107c478bd9Sstevel@tonic-gate 			    item->group, item->mib_id, item->length,
26117c478bd9Sstevel@tonic-gate 			    item->valp);
26127c478bd9Sstevel@tonic-gate 		}
26137c478bd9Sstevel@tonic-gate 
26147c478bd9Sstevel@tonic-gate 		switch (item->group) {
26157c478bd9Sstevel@tonic-gate 		case MIB2_IP:
26167c478bd9Sstevel@tonic-gate 		if (item->mib_id != MIB2_IP_ADDR ||
26177c478bd9Sstevel@tonic-gate 		    !family_selected(AF_INET))
26187c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
26197c478bd9Sstevel@tonic-gate 		{
26207c478bd9Sstevel@tonic-gate 			static struct ifstat	old = {0L, 0L, 0L, 0L, 0L};
26217c478bd9Sstevel@tonic-gate 			static struct ifstat	new = {0L, 0L, 0L, 0L, 0L};
26227c478bd9Sstevel@tonic-gate 			struct ifstat		sum;
26237c478bd9Sstevel@tonic-gate 			struct iflist		*newlist = NULL;
26247c478bd9Sstevel@tonic-gate 			static struct iflist	*oldlist = NULL;
26257c478bd9Sstevel@tonic-gate 			kstat_t	 *ksp;
26267c478bd9Sstevel@tonic-gate 
26277c478bd9Sstevel@tonic-gate 			if (once_only) {
26287c478bd9Sstevel@tonic-gate 				char    ifname[LIFNAMSIZ + 1];
26297c478bd9Sstevel@tonic-gate 				char    logintname[LIFNAMSIZ + 1];
26307c478bd9Sstevel@tonic-gate 				mib2_ipAddrEntry_t *ap;
26317c478bd9Sstevel@tonic-gate 				struct ifstat	stat = {0L, 0L, 0L, 0L, 0L};
26327c478bd9Sstevel@tonic-gate 				boolean_t	first = B_TRUE;
26337c478bd9Sstevel@tonic-gate 				uint32_t	new_ifindex;
26347c478bd9Sstevel@tonic-gate 
26357c478bd9Sstevel@tonic-gate 				if (Dflag)
26367c478bd9Sstevel@tonic-gate 					(void) printf("if_report: %d items\n",
26377c478bd9Sstevel@tonic-gate 					    (item->length)
26387c478bd9Sstevel@tonic-gate 					    / sizeof (mib2_ipAddrEntry_t));
26397c478bd9Sstevel@tonic-gate 
26407c478bd9Sstevel@tonic-gate 				/* 'for' loop 2a: */
26417c478bd9Sstevel@tonic-gate 				for (ap = (mib2_ipAddrEntry_t *)item->valp;
26427c478bd9Sstevel@tonic-gate 				    (char *)ap < (char *)item->valp
26437c478bd9Sstevel@tonic-gate 				    + item->length;
26447c478bd9Sstevel@tonic-gate 				    ap++) {
26457c478bd9Sstevel@tonic-gate 					(void) octetstr(&ap->ipAdEntIfIndex,
26467c478bd9Sstevel@tonic-gate 					    'a', logintname,
26477c478bd9Sstevel@tonic-gate 					    sizeof (logintname));
26487c478bd9Sstevel@tonic-gate 					(void) strcpy(ifname, logintname);
26497c478bd9Sstevel@tonic-gate 					(void) strtok(ifname, ":");
26507c478bd9Sstevel@tonic-gate 					if (matchname != NULL &&
26517c478bd9Sstevel@tonic-gate 					    strcmp(matchname, ifname) != 0 &&
26527c478bd9Sstevel@tonic-gate 					    strcmp(matchname, logintname) != 0)
26537c478bd9Sstevel@tonic-gate 						continue; /* 'for' loop 2a */
26547c478bd9Sstevel@tonic-gate 					new_ifindex =
26557c478bd9Sstevel@tonic-gate 					    if_nametoindex(logintname);
2656d62bc4baSyz 					/*
2657d62bc4baSyz 					 * First lookup the "link" kstats in
2658d62bc4baSyz 					 * case the link is renamed. Then
2659d62bc4baSyz 					 * fallback to the legacy kstats for
2660d62bc4baSyz 					 * those non-GLDv3 links.
2661d62bc4baSyz 					 */
26627c478bd9Sstevel@tonic-gate 					if (new_ifindex != ifindex_v4 &&
2663d62bc4baSyz 					    (((ksp = kstat_lookup(kc, "link", 0,
2664d62bc4baSyz 					    ifname)) != NULL) ||
2665d62bc4baSyz 					    ((ksp = kstat_lookup(kc, NULL, -1,
2666d62bc4baSyz 					    ifname)) != NULL))) {
26677c478bd9Sstevel@tonic-gate 						(void) safe_kstat_read(kc, ksp,
26687c478bd9Sstevel@tonic-gate 						    NULL);
26697c478bd9Sstevel@tonic-gate 						stat.ipackets =
26707c478bd9Sstevel@tonic-gate 						    kstat_named_value(ksp,
26717c478bd9Sstevel@tonic-gate 						    "ipackets");
26727c478bd9Sstevel@tonic-gate 						stat.ierrors =
26737c478bd9Sstevel@tonic-gate 						    kstat_named_value(ksp,
26747c478bd9Sstevel@tonic-gate 						    "ierrors");
26757c478bd9Sstevel@tonic-gate 						stat.opackets =
26767c478bd9Sstevel@tonic-gate 						    kstat_named_value(ksp,
26777c478bd9Sstevel@tonic-gate 						    "opackets");
26787c478bd9Sstevel@tonic-gate 						stat.oerrors =
26797c478bd9Sstevel@tonic-gate 						    kstat_named_value(ksp,
26807c478bd9Sstevel@tonic-gate 						    "oerrors");
26817c478bd9Sstevel@tonic-gate 						stat.collisions =
26827c478bd9Sstevel@tonic-gate 						    kstat_named_value(ksp,
26837c478bd9Sstevel@tonic-gate 						    "collisions");
26847c478bd9Sstevel@tonic-gate 						if (first) {
2685ba753d4aSkeerthi 							if (!first_header)
2686ba753d4aSkeerthi 							(void) putchar('\n');
2687ba753d4aSkeerthi 							first_header = B_FALSE;
26887c478bd9Sstevel@tonic-gate 						(void) printf(
26897c478bd9Sstevel@tonic-gate 						    "%-5.5s %-5.5s%-13.13s "
26907c478bd9Sstevel@tonic-gate 						    "%-14.14s %-6.6s %-5.5s "
26917c478bd9Sstevel@tonic-gate 						    "%-6.6s %-5.5s %-6.6s "
26927c478bd9Sstevel@tonic-gate 						    "%-6.6s\n",
26937c478bd9Sstevel@tonic-gate 						    "Name", "Mtu", "Net/Dest",
26947c478bd9Sstevel@tonic-gate 						    "Address", "Ipkts",
26957c478bd9Sstevel@tonic-gate 						    "Ierrs", "Opkts", "Oerrs",
26967c478bd9Sstevel@tonic-gate 						    "Collis", "Queue");
2697ba753d4aSkeerthi 
2698e11c3f44Smeem 						first = B_FALSE;
26997c478bd9Sstevel@tonic-gate 						}
27007c478bd9Sstevel@tonic-gate 						if_report_ip4(ap, ifname,
27017c478bd9Sstevel@tonic-gate 						    logintname, &stat, B_TRUE);
27027c478bd9Sstevel@tonic-gate 						ifindex_v4 = new_ifindex;
27037c478bd9Sstevel@tonic-gate 					} else {
27047c478bd9Sstevel@tonic-gate 						if_report_ip4(ap, ifname,
27057c478bd9Sstevel@tonic-gate 						    logintname, &stat, B_FALSE);
27067c478bd9Sstevel@tonic-gate 					}
27077c478bd9Sstevel@tonic-gate 				} /* 'for' loop 2a ends */
27087c478bd9Sstevel@tonic-gate 			} else if (!alreadydone) {
27097c478bd9Sstevel@tonic-gate 				char    ifname[LIFNAMSIZ + 1];
27107c478bd9Sstevel@tonic-gate 				char    buf[LIFNAMSIZ + 1];
27117c478bd9Sstevel@tonic-gate 				mib2_ipAddrEntry_t *ap;
27127c478bd9Sstevel@tonic-gate 				struct ifstat   t;
2713aecc8c24Sja 				struct iflist	*tlp = NULL;
27147c478bd9Sstevel@tonic-gate 				struct iflist	**nextnew = &newlist;
27157c478bd9Sstevel@tonic-gate 				struct iflist	*walkold;
27167c478bd9Sstevel@tonic-gate 				struct iflist	*cleanlist;
2717aecc8c24Sja 				boolean_t	found_if = B_FALSE;
27187c478bd9Sstevel@tonic-gate 
27197c478bd9Sstevel@tonic-gate 				alreadydone = B_TRUE; /* ignore other case */
2720aecc8c24Sja 
2721aecc8c24Sja 				/*
2722aecc8c24Sja 				 * Check if there is anything to do.
2723aecc8c24Sja 				 */
2724aecc8c24Sja 				if (item->length <
2725aecc8c24Sja 				    sizeof (mib2_ipAddrEntry_t)) {
2726aecc8c24Sja 					fail(0, "No compatible interfaces");
2727aecc8c24Sja 				}
2728aecc8c24Sja 
27297c478bd9Sstevel@tonic-gate 				/*
2730aecc8c24Sja 				 * 'for' loop 2b: find the "right" entry:
2731aecc8c24Sja 				 * If an interface name to match has been
2732aecc8c24Sja 				 * supplied then try and find it, otherwise
2733aecc8c24Sja 				 * match the first non-loopback interface found.
2734aecc8c24Sja 				 * Use lo0 if all else fails.
27357c478bd9Sstevel@tonic-gate 				 */
27367c478bd9Sstevel@tonic-gate 				for (ap = (mib2_ipAddrEntry_t *)item->valp;
27377c478bd9Sstevel@tonic-gate 				    (char *)ap < (char *)item->valp
27387c478bd9Sstevel@tonic-gate 				    + item->length;
27397c478bd9Sstevel@tonic-gate 				    ap++) {
27407c478bd9Sstevel@tonic-gate 					(void) octetstr(&ap->ipAdEntIfIndex,
2741e11c3f44Smeem 					    'a', ifname, sizeof (ifname));
27427c478bd9Sstevel@tonic-gate 					(void) strtok(ifname, ":");
27437c478bd9Sstevel@tonic-gate 
27447c478bd9Sstevel@tonic-gate 					if (matchname) {
27457c478bd9Sstevel@tonic-gate 						if (strcmp(matchname,
2746aecc8c24Sja 						    ifname) == 0) {
27477c478bd9Sstevel@tonic-gate 							/* 'for' loop 2b */
2748aecc8c24Sja 							found_if = B_TRUE;
27497c478bd9Sstevel@tonic-gate 							break;
2750aecc8c24Sja 						}
2751aecc8c24Sja 					} else if (strcmp(ifname, "lo0") != 0)
27527c478bd9Sstevel@tonic-gate 						break; /* 'for' loop 2b */
27537c478bd9Sstevel@tonic-gate 				} /* 'for' loop 2b ends */
27547c478bd9Sstevel@tonic-gate 
2755aecc8c24Sja 				if (matchname == NULL) {
2756aecc8c24Sja 					matchname = ifname;
2757aecc8c24Sja 				} else {
2758aecc8c24Sja 					if (!found_if)
2759aecc8c24Sja 						fail(0, "-I: %s no such "
2760aecc8c24Sja 						    "interface.", matchname);
2761aecc8c24Sja 				}
2762aecc8c24Sja 
27637c478bd9Sstevel@tonic-gate 				if (Iflag_only == 0 || !reentry) {
27647c478bd9Sstevel@tonic-gate 					(void) printf("    input   %-6.6s    "
27657c478bd9Sstevel@tonic-gate 					    "output	",
27667c478bd9Sstevel@tonic-gate 					    matchname);
27677c478bd9Sstevel@tonic-gate 					(void) printf("   input  (Total)    "
27687c478bd9Sstevel@tonic-gate 					"output\n");
27697c478bd9Sstevel@tonic-gate 					(void) printf("%-7.7s %-5.5s %-7.7s "
27707c478bd9Sstevel@tonic-gate 					    "%-5.5s %-6.6s ",
27717c478bd9Sstevel@tonic-gate 					    "packets", "errs", "packets",
27727c478bd9Sstevel@tonic-gate 					    "errs", "colls");
27737c478bd9Sstevel@tonic-gate 					(void) printf("%-7.7s %-5.5s %-7.7s "
27747c478bd9Sstevel@tonic-gate 					    "%-5.5s %-6.6s\n",
27757c478bd9Sstevel@tonic-gate 					    "packets", "errs", "packets",
27767c478bd9Sstevel@tonic-gate 					    "errs", "colls");
27777c478bd9Sstevel@tonic-gate 				}
27787c478bd9Sstevel@tonic-gate 
27797c478bd9Sstevel@tonic-gate 				sum = zerostat;
27807c478bd9Sstevel@tonic-gate 
27817c478bd9Sstevel@tonic-gate 				/* 'for' loop 2c: */
27827c478bd9Sstevel@tonic-gate 				for (ap = (mib2_ipAddrEntry_t *)item->valp;
27837c478bd9Sstevel@tonic-gate 				    (char *)ap < (char *)item->valp
27847c478bd9Sstevel@tonic-gate 				    + item->length;
27857c478bd9Sstevel@tonic-gate 				    ap++) {
27867c478bd9Sstevel@tonic-gate 					(void) octetstr(&ap->ipAdEntIfIndex,
27877c478bd9Sstevel@tonic-gate 					    'a', buf, sizeof (buf));
27887c478bd9Sstevel@tonic-gate 					(void) strtok(buf, ":");
2789aecc8c24Sja 
2790aecc8c24Sja 					/*
2791aecc8c24Sja 					 * We have reduced the IP interface
2792aecc8c24Sja 					 * name, which could have been a
2793aecc8c24Sja 					 * logical, down to a name suitable
2794aecc8c24Sja 					 * for use with kstats.
2795aecc8c24Sja 					 * We treat this name as unique and
2796aecc8c24Sja 					 * only collate statistics for it once
2797aecc8c24Sja 					 * per pass. This is to avoid falsely
2798aecc8c24Sja 					 * amplifying these statistics by the
2799aecc8c24Sja 					 * the number of logical instances.
2800aecc8c24Sja 					 */
2801aecc8c24Sja 					if ((tlp != NULL) &&
2802aecc8c24Sja 					    ((strcmp(buf, tlp->ifname) == 0))) {
2803aecc8c24Sja 						continue;
2804aecc8c24Sja 					}
2805aecc8c24Sja 
2806d62bc4baSyz 					/*
2807d62bc4baSyz 					 * First lookup the "link" kstats in
2808d62bc4baSyz 					 * case the link is renamed. Then
2809d62bc4baSyz 					 * fallback to the legacy kstats for
2810d62bc4baSyz 					 * those non-GLDv3 links.
2811d62bc4baSyz 					 */
2812d62bc4baSyz 					if (((ksp = kstat_lookup(kc, "link",
2813d62bc4baSyz 					    0, buf)) != NULL ||
2814d62bc4baSyz 					    (ksp = kstat_lookup(kc, NULL, -1,
2815d62bc4baSyz 					    buf)) != NULL) && (ksp->ks_type ==
2816d62bc4baSyz 					    KSTAT_TYPE_NAMED)) {
28177c478bd9Sstevel@tonic-gate 						(void) safe_kstat_read(kc, ksp,
28187c478bd9Sstevel@tonic-gate 						    NULL);
2819d62bc4baSyz 					}
28207c478bd9Sstevel@tonic-gate 
28217c478bd9Sstevel@tonic-gate 					t.ipackets = kstat_named_value(ksp,
28227c478bd9Sstevel@tonic-gate 					    "ipackets");
28237c478bd9Sstevel@tonic-gate 					t.ierrors = kstat_named_value(ksp,
28247c478bd9Sstevel@tonic-gate 					    "ierrors");
28257c478bd9Sstevel@tonic-gate 					t.opackets = kstat_named_value(ksp,
28267c478bd9Sstevel@tonic-gate 					    "opackets");
28277c478bd9Sstevel@tonic-gate 					t.oerrors = kstat_named_value(ksp,
28287c478bd9Sstevel@tonic-gate 					    "oerrors");
28297c478bd9Sstevel@tonic-gate 					t.collisions = kstat_named_value(ksp,
28307c478bd9Sstevel@tonic-gate 					    "collisions");
28317c478bd9Sstevel@tonic-gate 
28327c478bd9Sstevel@tonic-gate 					if (strcmp(buf, matchname) == 0)
28337c478bd9Sstevel@tonic-gate 						new = t;
28347c478bd9Sstevel@tonic-gate 
28357c478bd9Sstevel@tonic-gate 					/* Build the interface list */
28367c478bd9Sstevel@tonic-gate 
28377c478bd9Sstevel@tonic-gate 					tlp = malloc(sizeof (struct iflist));
28387c478bd9Sstevel@tonic-gate 					(void) strlcpy(tlp->ifname, buf,
28397c478bd9Sstevel@tonic-gate 					    sizeof (tlp->ifname));
28407c478bd9Sstevel@tonic-gate 					tlp->tot = t;
28417c478bd9Sstevel@tonic-gate 					*nextnew = tlp;
28427c478bd9Sstevel@tonic-gate 					nextnew = &tlp->next_if;
28437c478bd9Sstevel@tonic-gate 
28447c478bd9Sstevel@tonic-gate 					/*
28457c478bd9Sstevel@tonic-gate 					 * First time through.
28467c478bd9Sstevel@tonic-gate 					 * Just add up the interface stats.
28477c478bd9Sstevel@tonic-gate 					 */
28487c478bd9Sstevel@tonic-gate 
28497c478bd9Sstevel@tonic-gate 					if (oldlist == NULL) {
28507c478bd9Sstevel@tonic-gate 						if_stat_total(&zerostat,
28517c478bd9Sstevel@tonic-gate 						    &t, &sum);
28527c478bd9Sstevel@tonic-gate 						continue;
28537c478bd9Sstevel@tonic-gate 					}
28547c478bd9Sstevel@tonic-gate 
28557c478bd9Sstevel@tonic-gate 					/*
28567c478bd9Sstevel@tonic-gate 					 * Walk old list for the interface.
28577c478bd9Sstevel@tonic-gate 					 *
28587c478bd9Sstevel@tonic-gate 					 * If found, add difference to total.
28597c478bd9Sstevel@tonic-gate 					 *
28607c478bd9Sstevel@tonic-gate 					 * If not, an interface has been plumbed
28617c478bd9Sstevel@tonic-gate 					 * up.  In this case, we will simply
28627c478bd9Sstevel@tonic-gate 					 * ignore the new interface until the
28637c478bd9Sstevel@tonic-gate 					 * next interval; as there's no easy way
28647c478bd9Sstevel@tonic-gate 					 * to acquire statistics between time
28657c478bd9Sstevel@tonic-gate 					 * of the plumb and the next interval
28667c478bd9Sstevel@tonic-gate 					 * boundary.  This results in inaccurate
28677c478bd9Sstevel@tonic-gate 					 * total values for current interval.
28687c478bd9Sstevel@tonic-gate 					 *
28697c478bd9Sstevel@tonic-gate 					 * Note the case when an interface is
28707c478bd9Sstevel@tonic-gate 					 * unplumbed; as similar problems exist.
28717c478bd9Sstevel@tonic-gate 					 * The unplumbed interface is not in the
28727c478bd9Sstevel@tonic-gate 					 * current list, and there's no easy way
28737c478bd9Sstevel@tonic-gate 					 * to account for the statistics between
28747c478bd9Sstevel@tonic-gate 					 * the previous interval and time of the
28757c478bd9Sstevel@tonic-gate 					 * unplumb.  Therefore, we (in a sense)
28767c478bd9Sstevel@tonic-gate 					 * ignore the removed interface by only
28777c478bd9Sstevel@tonic-gate 					 * involving "current" interfaces when
28787c478bd9Sstevel@tonic-gate 					 * computing the total statistics.
28797c478bd9Sstevel@tonic-gate 					 * Unfortunately, this also results in
28807c478bd9Sstevel@tonic-gate 					 * inaccurate values for interval total.
28817c478bd9Sstevel@tonic-gate 					 */
28827c478bd9Sstevel@tonic-gate 
28837c478bd9Sstevel@tonic-gate 					for (walkold = oldlist;
28847c478bd9Sstevel@tonic-gate 					    walkold != NULL;
28857c478bd9Sstevel@tonic-gate 					    walkold = walkold->next_if) {
28867c478bd9Sstevel@tonic-gate 						if (strcmp(walkold->ifname,
28877c478bd9Sstevel@tonic-gate 						    buf) == 0) {
28887c478bd9Sstevel@tonic-gate 							if_stat_total(
28897c478bd9Sstevel@tonic-gate 							    &walkold->tot,
28907c478bd9Sstevel@tonic-gate 							    &t, &sum);
28917c478bd9Sstevel@tonic-gate 							break;
28927c478bd9Sstevel@tonic-gate 						}
28937c478bd9Sstevel@tonic-gate 					}
28947c478bd9Sstevel@tonic-gate 
28957c478bd9Sstevel@tonic-gate 				} /* 'for' loop 2c ends */
28967c478bd9Sstevel@tonic-gate 
28977c478bd9Sstevel@tonic-gate 				*nextnew = NULL;
28987c478bd9Sstevel@tonic-gate 
28997c478bd9Sstevel@tonic-gate 				(void) printf("%-7llu %-5llu %-7llu "
29007c478bd9Sstevel@tonic-gate 				    "%-5llu %-6llu ",
29017c478bd9Sstevel@tonic-gate 				    new.ipackets - old.ipackets,
29027c478bd9Sstevel@tonic-gate 				    new.ierrors - old.ierrors,
29037c478bd9Sstevel@tonic-gate 				    new.opackets - old.opackets,
29047c478bd9Sstevel@tonic-gate 				    new.oerrors - old.oerrors,
29057c478bd9Sstevel@tonic-gate 				    new.collisions - old.collisions);
29067c478bd9Sstevel@tonic-gate 
29077c478bd9Sstevel@tonic-gate 				(void) printf("%-7llu %-5llu %-7llu "
29087c478bd9Sstevel@tonic-gate 				    "%-5llu %-6llu\n", sum.ipackets,
29097c478bd9Sstevel@tonic-gate 				    sum.ierrors, sum.opackets,
29107c478bd9Sstevel@tonic-gate 				    sum.oerrors, sum.collisions);
29117c478bd9Sstevel@tonic-gate 
29127c478bd9Sstevel@tonic-gate 				/*
29137c478bd9Sstevel@tonic-gate 				 * Tidy things up once finished.
29147c478bd9Sstevel@tonic-gate 				 */
29157c478bd9Sstevel@tonic-gate 
29167c478bd9Sstevel@tonic-gate 				old = new;
29177c478bd9Sstevel@tonic-gate 				cleanlist = oldlist;
29187c478bd9Sstevel@tonic-gate 				oldlist = newlist;
29197c478bd9Sstevel@tonic-gate 				while (cleanlist != NULL) {
29207c478bd9Sstevel@tonic-gate 					tlp = cleanlist->next_if;
29217c478bd9Sstevel@tonic-gate 					free(cleanlist);
29227c478bd9Sstevel@tonic-gate 					cleanlist = tlp;
29237c478bd9Sstevel@tonic-gate 				}
29247c478bd9Sstevel@tonic-gate 			}
29257c478bd9Sstevel@tonic-gate 			break;
29267c478bd9Sstevel@tonic-gate 		}
29277c478bd9Sstevel@tonic-gate 		case MIB2_IP6:
29287c478bd9Sstevel@tonic-gate 		if (item->mib_id != MIB2_IP6_ADDR ||
29297c478bd9Sstevel@tonic-gate 		    !family_selected(AF_INET6))
29307c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
29317c478bd9Sstevel@tonic-gate 		{
29327c478bd9Sstevel@tonic-gate 			static struct ifstat	old6 = {0L, 0L, 0L, 0L, 0L};
29337c478bd9Sstevel@tonic-gate 			static struct ifstat	new6 = {0L, 0L, 0L, 0L, 0L};
29347c478bd9Sstevel@tonic-gate 			struct ifstat		sum6;
29357c478bd9Sstevel@tonic-gate 			struct iflist		*newlist6 = NULL;
29367c478bd9Sstevel@tonic-gate 			static struct iflist	*oldlist6 = NULL;
29377c478bd9Sstevel@tonic-gate 			kstat_t	 *ksp;
29387c478bd9Sstevel@tonic-gate 
29397c478bd9Sstevel@tonic-gate 			if (once_only) {
29407c478bd9Sstevel@tonic-gate 				char    ifname[LIFNAMSIZ + 1];
29417c478bd9Sstevel@tonic-gate 				char    logintname[LIFNAMSIZ + 1];
29427c478bd9Sstevel@tonic-gate 				mib2_ipv6AddrEntry_t *ap6;
29437c478bd9Sstevel@tonic-gate 				struct ifstat	stat = {0L, 0L, 0L, 0L, 0L};
29447c478bd9Sstevel@tonic-gate 				boolean_t	first = B_TRUE;
29457c478bd9Sstevel@tonic-gate 				uint32_t	new_ifindex;
29467c478bd9Sstevel@tonic-gate 
29477c478bd9Sstevel@tonic-gate 				if (Dflag)
29487c478bd9Sstevel@tonic-gate 					(void) printf("if_report: %d items\n",
29497c478bd9Sstevel@tonic-gate 					    (item->length)
29507c478bd9Sstevel@tonic-gate 					    / sizeof (mib2_ipv6AddrEntry_t));
29517c478bd9Sstevel@tonic-gate 				/* 'for' loop 2d: */
29527c478bd9Sstevel@tonic-gate 				for (ap6 = (mib2_ipv6AddrEntry_t *)item->valp;
29537c478bd9Sstevel@tonic-gate 				    (char *)ap6 < (char *)item->valp
29547c478bd9Sstevel@tonic-gate 				    + item->length;
29557c478bd9Sstevel@tonic-gate 				    ap6++) {
29567c478bd9Sstevel@tonic-gate 					(void) octetstr(&ap6->ipv6AddrIfIndex,
29577c478bd9Sstevel@tonic-gate 					    'a', logintname,
29587c478bd9Sstevel@tonic-gate 					    sizeof (logintname));
29597c478bd9Sstevel@tonic-gate 					(void) strcpy(ifname, logintname);
29607c478bd9Sstevel@tonic-gate 					(void) strtok(ifname, ":");
29617c478bd9Sstevel@tonic-gate 					if (matchname != NULL &&
29627c478bd9Sstevel@tonic-gate 					    strcmp(matchname, ifname) != 0 &&
29637c478bd9Sstevel@tonic-gate 					    strcmp(matchname, logintname) != 0)
29647c478bd9Sstevel@tonic-gate 						continue; /* 'for' loop 2d */
29657c478bd9Sstevel@tonic-gate 					new_ifindex =
29667c478bd9Sstevel@tonic-gate 					    if_nametoindex(logintname);
2967d62bc4baSyz 
2968d62bc4baSyz 					/*
2969d62bc4baSyz 					 * First lookup the "link" kstats in
2970d62bc4baSyz 					 * case the link is renamed. Then
2971d62bc4baSyz 					 * fallback to the legacy kstats for
2972d62bc4baSyz 					 * those non-GLDv3 links.
2973d62bc4baSyz 					 */
29747c478bd9Sstevel@tonic-gate 					if (new_ifindex != ifindex_v6 &&
2975d62bc4baSyz 					    ((ksp = kstat_lookup(kc, "link", 0,
2976d62bc4baSyz 					    ifname)) != NULL ||
29777c478bd9Sstevel@tonic-gate 					    (ksp = kstat_lookup(kc, NULL, -1,
2978d62bc4baSyz 					    ifname)) != NULL)) {
29797c478bd9Sstevel@tonic-gate 						(void) safe_kstat_read(kc, ksp,
29807c478bd9Sstevel@tonic-gate 						    NULL);
29817c478bd9Sstevel@tonic-gate 						stat.ipackets =
29827c478bd9Sstevel@tonic-gate 						    kstat_named_value(ksp,
29837c478bd9Sstevel@tonic-gate 						    "ipackets");
29847c478bd9Sstevel@tonic-gate 						stat.ierrors =
29857c478bd9Sstevel@tonic-gate 						    kstat_named_value(ksp,
29867c478bd9Sstevel@tonic-gate 						    "ierrors");
29877c478bd9Sstevel@tonic-gate 						stat.opackets =
29887c478bd9Sstevel@tonic-gate 						    kstat_named_value(ksp,
29897c478bd9Sstevel@tonic-gate 						    "opackets");
29907c478bd9Sstevel@tonic-gate 						stat.oerrors =
29917c478bd9Sstevel@tonic-gate 						    kstat_named_value(ksp,
29927c478bd9Sstevel@tonic-gate 						    "oerrors");
29937c478bd9Sstevel@tonic-gate 						stat.collisions =
29947c478bd9Sstevel@tonic-gate 						    kstat_named_value(ksp,
29957c478bd9Sstevel@tonic-gate 						    "collisions");
29967c478bd9Sstevel@tonic-gate 						if (first) {
2997ba753d4aSkeerthi 							if (!first_header)
2998ba753d4aSkeerthi 							(void) putchar('\n');
2999ba753d4aSkeerthi 							first_header = B_FALSE;
30007c478bd9Sstevel@tonic-gate 							(void) printf(
30017c478bd9Sstevel@tonic-gate 							    "%-5.5s %-5.5s%"
30027c478bd9Sstevel@tonic-gate 							    "-27.27s %-27.27s "
30037c478bd9Sstevel@tonic-gate 							    "%-6.6s %-5.5s "
30047c478bd9Sstevel@tonic-gate 							    "%-6.6s %-5.5s "
30057c478bd9Sstevel@tonic-gate 							    "%-6.6s\n",
30067c478bd9Sstevel@tonic-gate 							    "Name", "Mtu",
30077c478bd9Sstevel@tonic-gate 							    "Net/Dest",
30087c478bd9Sstevel@tonic-gate 							    "Address", "Ipkts",
30097c478bd9Sstevel@tonic-gate 							    "Ierrs", "Opkts",
30107c478bd9Sstevel@tonic-gate 							    "Oerrs", "Collis");
30117c478bd9Sstevel@tonic-gate 							first = B_FALSE;
30127c478bd9Sstevel@tonic-gate 						}
30137c478bd9Sstevel@tonic-gate 						if_report_ip6(ap6, ifname,
30147c478bd9Sstevel@tonic-gate 						    logintname, &stat, B_TRUE);
30157c478bd9Sstevel@tonic-gate 						ifindex_v6 = new_ifindex;
30167c478bd9Sstevel@tonic-gate 					} else {
30177c478bd9Sstevel@tonic-gate 						if_report_ip6(ap6, ifname,
30187c478bd9Sstevel@tonic-gate 						    logintname, &stat, B_FALSE);
30197c478bd9Sstevel@tonic-gate 					}
30207c478bd9Sstevel@tonic-gate 				} /* 'for' loop 2d ends */
30217c478bd9Sstevel@tonic-gate 			} else if (!alreadydone) {
30227c478bd9Sstevel@tonic-gate 				char    ifname[LIFNAMSIZ + 1];
30237c478bd9Sstevel@tonic-gate 				char    buf[IFNAMSIZ + 1];
30247c478bd9Sstevel@tonic-gate 				mib2_ipv6AddrEntry_t *ap6;
30257c478bd9Sstevel@tonic-gate 				struct ifstat   t;
3026aecc8c24Sja 				struct iflist	*tlp = NULL;
30277c478bd9Sstevel@tonic-gate 				struct iflist	**nextnew = &newlist6;
30287c478bd9Sstevel@tonic-gate 				struct iflist	*walkold;
30297c478bd9Sstevel@tonic-gate 				struct iflist	*cleanlist;
3030aecc8c24Sja 				boolean_t	found_if = B_FALSE;
30317c478bd9Sstevel@tonic-gate 
30327c478bd9Sstevel@tonic-gate 				alreadydone = B_TRUE; /* ignore other case */
3033aecc8c24Sja 
30347c478bd9Sstevel@tonic-gate 				/*
3035aecc8c24Sja 				 * Check if there is anything to do.
3036aecc8c24Sja 				 */
3037aecc8c24Sja 				if (item->length <
3038aecc8c24Sja 				    sizeof (mib2_ipv6AddrEntry_t)) {
3039aecc8c24Sja 					fail(0, "No compatible interfaces");
3040aecc8c24Sja 				}
3041aecc8c24Sja 
3042aecc8c24Sja 				/*
3043aecc8c24Sja 				 * 'for' loop 2e: find the "right" entry:
3044aecc8c24Sja 				 * If an interface name to match has been
3045aecc8c24Sja 				 * supplied then try and find it, otherwise
3046aecc8c24Sja 				 * match the first non-loopback interface found.
3047aecc8c24Sja 				 * Use lo0 if all else fails.
30487c478bd9Sstevel@tonic-gate 				 */
30497c478bd9Sstevel@tonic-gate 				for (ap6 = (mib2_ipv6AddrEntry_t *)item->valp;
30507c478bd9Sstevel@tonic-gate 				    (char *)ap6 < (char *)item->valp
30517c478bd9Sstevel@tonic-gate 				    + item->length;
30527c478bd9Sstevel@tonic-gate 				    ap6++) {
30537c478bd9Sstevel@tonic-gate 					(void) octetstr(&ap6->ipv6AddrIfIndex,
30547c478bd9Sstevel@tonic-gate 					    'a', ifname, sizeof (ifname));
30557c478bd9Sstevel@tonic-gate 					(void) strtok(ifname, ":");
30567c478bd9Sstevel@tonic-gate 
30577c478bd9Sstevel@tonic-gate 					if (matchname) {
3058aecc8c24Sja 						if (strcmp(matchname,
3059aecc8c24Sja 						    ifname) == 0) {
30607c478bd9Sstevel@tonic-gate 							/* 'for' loop 2e */
3061aecc8c24Sja 							found_if = B_TRUE;
30627c478bd9Sstevel@tonic-gate 							break;
3063aecc8c24Sja 						}
3064aecc8c24Sja 					} else if (strcmp(ifname, "lo0") != 0)
30657c478bd9Sstevel@tonic-gate 						break; /* 'for' loop 2e */
30667c478bd9Sstevel@tonic-gate 				} /* 'for' loop 2e ends */
30677c478bd9Sstevel@tonic-gate 
3068aecc8c24Sja 				if (matchname == NULL) {
3069aecc8c24Sja 					matchname = ifname;
3070aecc8c24Sja 				} else {
3071aecc8c24Sja 					if (!found_if)
3072aecc8c24Sja 						fail(0, "-I: %s no such "
3073aecc8c24Sja 						    "interface.", matchname);
3074aecc8c24Sja 				}
3075aecc8c24Sja 
30767c478bd9Sstevel@tonic-gate 				if (Iflag_only == 0 || !reentry) {
30777c478bd9Sstevel@tonic-gate 					(void) printf(
30787c478bd9Sstevel@tonic-gate 					    "    input   %-6.6s"
30797c478bd9Sstevel@tonic-gate 					    "    output	",
30807c478bd9Sstevel@tonic-gate 					    matchname);
30817c478bd9Sstevel@tonic-gate 					(void) printf("   input  (Total)"
30827c478bd9Sstevel@tonic-gate 					    "    output\n");
30837c478bd9Sstevel@tonic-gate 					(void) printf("%-7.7s %-5.5s %-7.7s "
30847c478bd9Sstevel@tonic-gate 					    "%-5.5s %-6.6s ",
30857c478bd9Sstevel@tonic-gate 					    "packets", "errs", "packets",
30867c478bd9Sstevel@tonic-gate 					    "errs", "colls");
30877c478bd9Sstevel@tonic-gate 					(void) printf("%-7.7s %-5.5s %-7.7s "
30887c478bd9Sstevel@tonic-gate 					    "%-5.5s %-6.6s\n",
30897c478bd9Sstevel@tonic-gate 					    "packets", "errs", "packets",
30907c478bd9Sstevel@tonic-gate 					    "errs", "colls");
30917c478bd9Sstevel@tonic-gate 				}
30927c478bd9Sstevel@tonic-gate 
30937c478bd9Sstevel@tonic-gate 				sum6 = zerostat;
30947c478bd9Sstevel@tonic-gate 
30957c478bd9Sstevel@tonic-gate 				/* 'for' loop 2f: */
30967c478bd9Sstevel@tonic-gate 				for (ap6 = (mib2_ipv6AddrEntry_t *)item->valp;
30977c478bd9Sstevel@tonic-gate 				    (char *)ap6 < (char *)item->valp
30987c478bd9Sstevel@tonic-gate 				    + item->length;
30997c478bd9Sstevel@tonic-gate 				    ap6++) {
31007c478bd9Sstevel@tonic-gate 					(void) octetstr(&ap6->ipv6AddrIfIndex,
31017c478bd9Sstevel@tonic-gate 					    'a', buf, sizeof (buf));
31027c478bd9Sstevel@tonic-gate 					(void) strtok(buf, ":");
3103aecc8c24Sja 
3104aecc8c24Sja 					/*
3105aecc8c24Sja 					 * We have reduced the IP interface
3106aecc8c24Sja 					 * name, which could have been a
3107aecc8c24Sja 					 * logical, down to a name suitable
3108aecc8c24Sja 					 * for use with kstats.
3109aecc8c24Sja 					 * We treat this name as unique and
3110aecc8c24Sja 					 * only collate statistics for it once
3111aecc8c24Sja 					 * per pass. This is to avoid falsely
3112aecc8c24Sja 					 * amplifying these statistics by the
3113aecc8c24Sja 					 * the number of logical instances.
3114aecc8c24Sja 					 */
3115aecc8c24Sja 
3116aecc8c24Sja 					if ((tlp != NULL) &&
3117aecc8c24Sja 					    ((strcmp(buf, tlp->ifname) == 0))) {
3118aecc8c24Sja 						continue;
3119aecc8c24Sja 					}
3120aecc8c24Sja 
3121d62bc4baSyz 					/*
3122d62bc4baSyz 					 * First lookup the "link" kstats in
3123d62bc4baSyz 					 * case the link is renamed. Then
3124d62bc4baSyz 					 * fallback to the legacy kstats for
3125d62bc4baSyz 					 * those non-GLDv3 links.
3126d62bc4baSyz 					 */
3127d62bc4baSyz 					if (((ksp = kstat_lookup(kc, "link",
3128d62bc4baSyz 					    0, buf)) != NULL ||
3129d62bc4baSyz 					    (ksp = kstat_lookup(kc, NULL, -1,
3130d62bc4baSyz 					    buf)) != NULL) && (ksp->ks_type ==
3131d62bc4baSyz 					    KSTAT_TYPE_NAMED)) {
31327c478bd9Sstevel@tonic-gate 						(void) safe_kstat_read(kc,
31337c478bd9Sstevel@tonic-gate 						    ksp, NULL);
3134d62bc4baSyz 					}
31357c478bd9Sstevel@tonic-gate 
31367c478bd9Sstevel@tonic-gate 					t.ipackets = kstat_named_value(ksp,
31377c478bd9Sstevel@tonic-gate 					    "ipackets");
31387c478bd9Sstevel@tonic-gate 					t.ierrors = kstat_named_value(ksp,
31397c478bd9Sstevel@tonic-gate 					    "ierrors");
31407c478bd9Sstevel@tonic-gate 					t.opackets = kstat_named_value(ksp,
31417c478bd9Sstevel@tonic-gate 					    "opackets");
31427c478bd9Sstevel@tonic-gate 					t.oerrors = kstat_named_value(ksp,
31437c478bd9Sstevel@tonic-gate 					    "oerrors");
31447c478bd9Sstevel@tonic-gate 					t.collisions = kstat_named_value(ksp,
31457c478bd9Sstevel@tonic-gate 					    "collisions");
31467c478bd9Sstevel@tonic-gate 
31477c478bd9Sstevel@tonic-gate 					if (strcmp(buf, matchname) == 0)
31487c478bd9Sstevel@tonic-gate 						new6 = t;
31497c478bd9Sstevel@tonic-gate 
31507c478bd9Sstevel@tonic-gate 					/* Build the interface list */
31517c478bd9Sstevel@tonic-gate 
31527c478bd9Sstevel@tonic-gate 					tlp = malloc(sizeof (struct iflist));
31537c478bd9Sstevel@tonic-gate 					(void) strlcpy(tlp->ifname, buf,
31547c478bd9Sstevel@tonic-gate 					    sizeof (tlp->ifname));
31557c478bd9Sstevel@tonic-gate 					tlp->tot = t;
31567c478bd9Sstevel@tonic-gate 					*nextnew = tlp;
31577c478bd9Sstevel@tonic-gate 					nextnew = &tlp->next_if;
31587c478bd9Sstevel@tonic-gate 
31597c478bd9Sstevel@tonic-gate 					/*
31607c478bd9Sstevel@tonic-gate 					 * First time through.
31617c478bd9Sstevel@tonic-gate 					 * Just add up the interface stats.
31627c478bd9Sstevel@tonic-gate 					 */
31637c478bd9Sstevel@tonic-gate 
31647c478bd9Sstevel@tonic-gate 					if (oldlist6 == NULL) {
31657c478bd9Sstevel@tonic-gate 						if_stat_total(&zerostat,
31667c478bd9Sstevel@tonic-gate 						    &t, &sum6);
31677c478bd9Sstevel@tonic-gate 						continue;
31687c478bd9Sstevel@tonic-gate 					}
31697c478bd9Sstevel@tonic-gate 
31707c478bd9Sstevel@tonic-gate 					/*
31717c478bd9Sstevel@tonic-gate 					 * Walk old list for the interface.
31727c478bd9Sstevel@tonic-gate 					 *
31737c478bd9Sstevel@tonic-gate 					 * If found, add difference to total.
31747c478bd9Sstevel@tonic-gate 					 *
31757c478bd9Sstevel@tonic-gate 					 * If not, an interface has been plumbed
31767c478bd9Sstevel@tonic-gate 					 * up.  In this case, we will simply
31777c478bd9Sstevel@tonic-gate 					 * ignore the new interface until the
31787c478bd9Sstevel@tonic-gate 					 * next interval; as there's no easy way
31797c478bd9Sstevel@tonic-gate 					 * to acquire statistics between time
31807c478bd9Sstevel@tonic-gate 					 * of the plumb and the next interval
31817c478bd9Sstevel@tonic-gate 					 * boundary.  This results in inaccurate
31827c478bd9Sstevel@tonic-gate 					 * total values for current interval.
31837c478bd9Sstevel@tonic-gate 					 *
31847c478bd9Sstevel@tonic-gate 					 * Note the case when an interface is
31857c478bd9Sstevel@tonic-gate 					 * unplumbed; as similar problems exist.
31867c478bd9Sstevel@tonic-gate 					 * The unplumbed interface is not in the
31877c478bd9Sstevel@tonic-gate 					 * current list, and there's no easy way
31887c478bd9Sstevel@tonic-gate 					 * to account for the statistics between
31897c478bd9Sstevel@tonic-gate 					 * the previous interval and time of the
31907c478bd9Sstevel@tonic-gate 					 * unplumb.  Therefore, we (in a sense)
31917c478bd9Sstevel@tonic-gate 					 * ignore the removed interface by only
31927c478bd9Sstevel@tonic-gate 					 * involving "current" interfaces when
31937c478bd9Sstevel@tonic-gate 					 * computing the total statistics.
31947c478bd9Sstevel@tonic-gate 					 * Unfortunately, this also results in
31957c478bd9Sstevel@tonic-gate 					 * inaccurate values for interval total.
31967c478bd9Sstevel@tonic-gate 					 */
31977c478bd9Sstevel@tonic-gate 
31987c478bd9Sstevel@tonic-gate 					for (walkold = oldlist6;
31997c478bd9Sstevel@tonic-gate 					    walkold != NULL;
32007c478bd9Sstevel@tonic-gate 					    walkold = walkold->next_if) {
32017c478bd9Sstevel@tonic-gate 						if (strcmp(walkold->ifname,
32027c478bd9Sstevel@tonic-gate 						    buf) == 0) {
32037c478bd9Sstevel@tonic-gate 							if_stat_total(
32047c478bd9Sstevel@tonic-gate 							    &walkold->tot,
32057c478bd9Sstevel@tonic-gate 							    &t, &sum6);
32067c478bd9Sstevel@tonic-gate 							break;
32077c478bd9Sstevel@tonic-gate 						}
32087c478bd9Sstevel@tonic-gate 					}
32097c478bd9Sstevel@tonic-gate 
32107c478bd9Sstevel@tonic-gate 				} /* 'for' loop 2f ends */
32117c478bd9Sstevel@tonic-gate 
32127c478bd9Sstevel@tonic-gate 				*nextnew = NULL;
32137c478bd9Sstevel@tonic-gate 
32147c478bd9Sstevel@tonic-gate 				(void) printf("%-7llu %-5llu %-7llu "
32157c478bd9Sstevel@tonic-gate 				    "%-5llu %-6llu ",
32167c478bd9Sstevel@tonic-gate 				    new6.ipackets - old6.ipackets,
32177c478bd9Sstevel@tonic-gate 				    new6.ierrors - old6.ierrors,
32187c478bd9Sstevel@tonic-gate 				    new6.opackets - old6.opackets,
32197c478bd9Sstevel@tonic-gate 				    new6.oerrors - old6.oerrors,
32207c478bd9Sstevel@tonic-gate 				    new6.collisions - old6.collisions);
32217c478bd9Sstevel@tonic-gate 
32227c478bd9Sstevel@tonic-gate 				(void) printf("%-7llu %-5llu %-7llu "
32237c478bd9Sstevel@tonic-gate 				    "%-5llu %-6llu\n", sum6.ipackets,
32247c478bd9Sstevel@tonic-gate 				    sum6.ierrors, sum6.opackets,
32257c478bd9Sstevel@tonic-gate 				    sum6.oerrors, sum6.collisions);
32267c478bd9Sstevel@tonic-gate 
32277c478bd9Sstevel@tonic-gate 				/*
32287c478bd9Sstevel@tonic-gate 				 * Tidy things up once finished.
32297c478bd9Sstevel@tonic-gate 				 */
32307c478bd9Sstevel@tonic-gate 
32317c478bd9Sstevel@tonic-gate 				old6 = new6;
32327c478bd9Sstevel@tonic-gate 				cleanlist = oldlist6;
32337c478bd9Sstevel@tonic-gate 				oldlist6 = newlist6;
32347c478bd9Sstevel@tonic-gate 				while (cleanlist != NULL) {
32357c478bd9Sstevel@tonic-gate 					tlp = cleanlist->next_if;
32367c478bd9Sstevel@tonic-gate 					free(cleanlist);
32377c478bd9Sstevel@tonic-gate 					cleanlist = tlp;
32387c478bd9Sstevel@tonic-gate 				}
32397c478bd9Sstevel@tonic-gate 			}
32407c478bd9Sstevel@tonic-gate 			break;
32417c478bd9Sstevel@tonic-gate 		}
32427c478bd9Sstevel@tonic-gate 		}
32437c478bd9Sstevel@tonic-gate 		(void) fflush(stdout);
32447c478bd9Sstevel@tonic-gate 	} /* 'for' loop 1 ends */
3245ba753d4aSkeerthi 	if ((Iflag_only == 0) && (!once_only))
3246ba753d4aSkeerthi 		(void) putchar('\n');
32477c478bd9Sstevel@tonic-gate 	reentry = B_TRUE;
32487c478bd9Sstevel@tonic-gate }
32497c478bd9Sstevel@tonic-gate 
32507c478bd9Sstevel@tonic-gate static void
32517c478bd9Sstevel@tonic-gate if_report_ip4(mib2_ipAddrEntry_t *ap,
32527c478bd9Sstevel@tonic-gate 	char ifname[], char logintname[], struct ifstat *statptr,
32537c478bd9Sstevel@tonic-gate 	boolean_t ksp_not_null) {
32547c478bd9Sstevel@tonic-gate 
32557c478bd9Sstevel@tonic-gate 	char abuf[MAXHOSTNAMELEN + 1];
32567c478bd9Sstevel@tonic-gate 	char dstbuf[MAXHOSTNAMELEN + 1];
32577c478bd9Sstevel@tonic-gate 
32587c478bd9Sstevel@tonic-gate 	if (ksp_not_null) {
32599edb2c23SAjaykumar Venkatesulu 		(void) printf("%-5s %-4u ",
32607c478bd9Sstevel@tonic-gate 		    ifname, ap->ipAdEntInfo.ae_mtu);
32617c478bd9Sstevel@tonic-gate 		if (ap->ipAdEntInfo.ae_flags & IFF_POINTOPOINT)
32627c478bd9Sstevel@tonic-gate 			(void) pr_addr(ap->ipAdEntInfo.ae_pp_dst_addr,
32637c478bd9Sstevel@tonic-gate 			    abuf, sizeof (abuf));
32647c478bd9Sstevel@tonic-gate 		else
32657c478bd9Sstevel@tonic-gate 			(void) pr_netaddr(ap->ipAdEntAddr,
32667c478bd9Sstevel@tonic-gate 			    ap->ipAdEntNetMask, abuf, sizeof (abuf));
32677c478bd9Sstevel@tonic-gate 		(void) printf("%-13s %-14s %-6llu %-5llu %-6llu %-5llu "
32687c478bd9Sstevel@tonic-gate 		    "%-6llu %-6llu\n",
32697c478bd9Sstevel@tonic-gate 		    abuf, pr_addr(ap->ipAdEntAddr, dstbuf, sizeof (dstbuf)),
32707c478bd9Sstevel@tonic-gate 		    statptr->ipackets, statptr->ierrors,
32717c478bd9Sstevel@tonic-gate 		    statptr->opackets, statptr->oerrors,
32727c478bd9Sstevel@tonic-gate 		    statptr->collisions, 0LL);
32737c478bd9Sstevel@tonic-gate 	}
32747c478bd9Sstevel@tonic-gate 	/*
32757c478bd9Sstevel@tonic-gate 	 * Print logical interface info if Aflag set (including logical unit 0)
32767c478bd9Sstevel@tonic-gate 	 */
32777c478bd9Sstevel@tonic-gate 	if (Aflag) {
32787c478bd9Sstevel@tonic-gate 		*statptr = zerostat;
32797c478bd9Sstevel@tonic-gate 		statptr->ipackets = ap->ipAdEntInfo.ae_ibcnt;
32807c478bd9Sstevel@tonic-gate 		statptr->opackets = ap->ipAdEntInfo.ae_obcnt;
32817c478bd9Sstevel@tonic-gate 
32829edb2c23SAjaykumar Venkatesulu 		(void) printf("%-5s %-4u ", logintname, ap->ipAdEntInfo.ae_mtu);
32837c478bd9Sstevel@tonic-gate 		if (ap->ipAdEntInfo.ae_flags & IFF_POINTOPOINT)
32847c478bd9Sstevel@tonic-gate 			(void) pr_addr(ap->ipAdEntInfo.ae_pp_dst_addr, abuf,
32857c478bd9Sstevel@tonic-gate 			sizeof (abuf));
32867c478bd9Sstevel@tonic-gate 		else
32877c478bd9Sstevel@tonic-gate 			(void) pr_netaddr(ap->ipAdEntAddr, ap->ipAdEntNetMask,
32887c478bd9Sstevel@tonic-gate 			    abuf, sizeof (abuf));
32897c478bd9Sstevel@tonic-gate 
32907c478bd9Sstevel@tonic-gate 		(void) printf("%-13s %-14s %-6llu %-5s %-6llu "
32917c478bd9Sstevel@tonic-gate 		    "%-5s %-6s %-6llu\n", abuf,
32927c478bd9Sstevel@tonic-gate 		    pr_addr(ap->ipAdEntAddr, dstbuf, sizeof (dstbuf)),
32937c478bd9Sstevel@tonic-gate 		    statptr->ipackets, "N/A", statptr->opackets, "N/A", "N/A",
32947c478bd9Sstevel@tonic-gate 		    0LL);
32957c478bd9Sstevel@tonic-gate 	}
32967c478bd9Sstevel@tonic-gate }
32977c478bd9Sstevel@tonic-gate 
32987c478bd9Sstevel@tonic-gate static void
32997c478bd9Sstevel@tonic-gate if_report_ip6(mib2_ipv6AddrEntry_t *ap6,
33007c478bd9Sstevel@tonic-gate 	char ifname[], char logintname[], struct ifstat *statptr,
33017c478bd9Sstevel@tonic-gate 	boolean_t ksp_not_null) {
33027c478bd9Sstevel@tonic-gate 
33037c478bd9Sstevel@tonic-gate 	char abuf[MAXHOSTNAMELEN + 1];
33047c478bd9Sstevel@tonic-gate 	char dstbuf[MAXHOSTNAMELEN + 1];
33057c478bd9Sstevel@tonic-gate 
33067c478bd9Sstevel@tonic-gate 	if (ksp_not_null) {
33079edb2c23SAjaykumar Venkatesulu 		(void) printf("%-5s %-4u ", ifname, ap6->ipv6AddrInfo.ae_mtu);
33087c478bd9Sstevel@tonic-gate 		if (ap6->ipv6AddrInfo.ae_flags &
33097c478bd9Sstevel@tonic-gate 		    IFF_POINTOPOINT) {
33107c478bd9Sstevel@tonic-gate 			(void) pr_addr6(&ap6->ipv6AddrInfo.ae_pp_dst_addr,
33117c478bd9Sstevel@tonic-gate 			    abuf, sizeof (abuf));
33127c478bd9Sstevel@tonic-gate 		} else {
33137c478bd9Sstevel@tonic-gate 			(void) pr_prefix6(&ap6->ipv6AddrAddress,
33147c478bd9Sstevel@tonic-gate 			    ap6->ipv6AddrPfxLength, abuf,
33157c478bd9Sstevel@tonic-gate 			    sizeof (abuf));
33167c478bd9Sstevel@tonic-gate 		}
33177c478bd9Sstevel@tonic-gate 		(void) printf("%-27s %-27s %-6llu %-5llu "
33187c478bd9Sstevel@tonic-gate 		    "%-6llu %-5llu %-6llu\n",
33197c478bd9Sstevel@tonic-gate 		    abuf, pr_addr6(&ap6->ipv6AddrAddress, dstbuf,
33207c478bd9Sstevel@tonic-gate 		    sizeof (dstbuf)),
33217c478bd9Sstevel@tonic-gate 		    statptr->ipackets, statptr->ierrors, statptr->opackets,
33227c478bd9Sstevel@tonic-gate 		    statptr->oerrors, statptr->collisions);
33237c478bd9Sstevel@tonic-gate 	}
33247c478bd9Sstevel@tonic-gate 	/*
33257c478bd9Sstevel@tonic-gate 	 * Print logical interface info if Aflag set (including logical unit 0)
33267c478bd9Sstevel@tonic-gate 	 */
33277c478bd9Sstevel@tonic-gate 	if (Aflag) {
33287c478bd9Sstevel@tonic-gate 		*statptr = zerostat;
33297c478bd9Sstevel@tonic-gate 		statptr->ipackets = ap6->ipv6AddrInfo.ae_ibcnt;
33307c478bd9Sstevel@tonic-gate 		statptr->opackets = ap6->ipv6AddrInfo.ae_obcnt;
33317c478bd9Sstevel@tonic-gate 
33329edb2c23SAjaykumar Venkatesulu 		(void) printf("%-5s %-4u ", logintname,
33337c478bd9Sstevel@tonic-gate 		    ap6->ipv6AddrInfo.ae_mtu);
33347c478bd9Sstevel@tonic-gate 		if (ap6->ipv6AddrInfo.ae_flags & IFF_POINTOPOINT)
33357c478bd9Sstevel@tonic-gate 			(void) pr_addr6(&ap6->ipv6AddrInfo.ae_pp_dst_addr,
33367c478bd9Sstevel@tonic-gate 			    abuf, sizeof (abuf));
33377c478bd9Sstevel@tonic-gate 		else
33387c478bd9Sstevel@tonic-gate 			(void) pr_prefix6(&ap6->ipv6AddrAddress,
33397c478bd9Sstevel@tonic-gate 			    ap6->ipv6AddrPfxLength, abuf, sizeof (abuf));
33407c478bd9Sstevel@tonic-gate 		(void) printf("%-27s %-27s %-6llu %-5s %-6llu %-5s %-6s\n",
33417c478bd9Sstevel@tonic-gate 		    abuf, pr_addr6(&ap6->ipv6AddrAddress, dstbuf,
33427c478bd9Sstevel@tonic-gate 		    sizeof (dstbuf)),
33437c478bd9Sstevel@tonic-gate 		    statptr->ipackets, "N/A",
33447c478bd9Sstevel@tonic-gate 		    statptr->opackets, "N/A", "N/A");
33457c478bd9Sstevel@tonic-gate 	}
33467c478bd9Sstevel@tonic-gate }
33477c478bd9Sstevel@tonic-gate 
33487c478bd9Sstevel@tonic-gate /* --------------------- DHCP_REPORT  (netstat -D) ------------------------- */
33497c478bd9Sstevel@tonic-gate 
3350d04ccbb3Scarlsonj static boolean_t
3351d04ccbb3Scarlsonj dhcp_do_ipc(dhcp_ipc_type_t type, const char *ifname, boolean_t printed_one)
33527c478bd9Sstevel@tonic-gate {
33537c478bd9Sstevel@tonic-gate 	dhcp_ipc_request_t	*request;
33547c478bd9Sstevel@tonic-gate 	dhcp_ipc_reply_t	*reply;
33557c478bd9Sstevel@tonic-gate 	int			error;
33567c478bd9Sstevel@tonic-gate 
33577c478bd9Sstevel@tonic-gate 	request = dhcp_ipc_alloc_request(type, ifname, NULL, 0, DHCP_TYPE_NONE);
33587c478bd9Sstevel@tonic-gate 	if (request == NULL)
33597c478bd9Sstevel@tonic-gate 		fail(0, "dhcp_do_ipc: out of memory");
33607c478bd9Sstevel@tonic-gate 
33617c478bd9Sstevel@tonic-gate 	error = dhcp_ipc_make_request(request, &reply, DHCP_IPC_WAIT_DEFAULT);
33627c478bd9Sstevel@tonic-gate 	if (error != 0) {
33637c478bd9Sstevel@tonic-gate 		free(request);
33647c478bd9Sstevel@tonic-gate 		fail(0, "dhcp_do_ipc: %s", dhcp_ipc_strerror(error));
33657c478bd9Sstevel@tonic-gate 	}
33667c478bd9Sstevel@tonic-gate 
33677c478bd9Sstevel@tonic-gate 	free(request);
33687c478bd9Sstevel@tonic-gate 	error = reply->return_code;
3369d04ccbb3Scarlsonj 	if (error == DHCP_IPC_E_UNKIF) {
3370d04ccbb3Scarlsonj 		free(reply);
3371d04ccbb3Scarlsonj 		return (printed_one);
3372d04ccbb3Scarlsonj 	}
33737c478bd9Sstevel@tonic-gate 	if (error != 0) {
33747c478bd9Sstevel@tonic-gate 		free(reply);
33757c478bd9Sstevel@tonic-gate 		fail(0, "dhcp_do_ipc: %s", dhcp_ipc_strerror(error));
33767c478bd9Sstevel@tonic-gate 	}
33777c478bd9Sstevel@tonic-gate 
3378*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 	if (timestamp_fmt != NODATE)
3379*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 		print_timestamp(timestamp_fmt);
3380*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 
3381d04ccbb3Scarlsonj 	if (!printed_one)
3382d04ccbb3Scarlsonj 		(void) printf("%s", dhcp_status_hdr_string());
3383d04ccbb3Scarlsonj 
3384d04ccbb3Scarlsonj 	(void) printf("%s", dhcp_status_reply_to_string(reply));
3385d04ccbb3Scarlsonj 	free(reply);
3386d04ccbb3Scarlsonj 	return (B_TRUE);
33877c478bd9Sstevel@tonic-gate }
33887c478bd9Sstevel@tonic-gate 
33897c478bd9Sstevel@tonic-gate /*
3390d04ccbb3Scarlsonj  * dhcp_walk_interfaces: walk the list of interfaces that have a given set of
3391d04ccbb3Scarlsonj  * flags turned on (flags_on) and a given set turned off (flags_off) for a
3392d04ccbb3Scarlsonj  * given address family (af).  For each, print out the DHCP status using
3393d04ccbb3Scarlsonj  * dhcp_do_ipc.
33947c478bd9Sstevel@tonic-gate  */
3395d04ccbb3Scarlsonj static boolean_t
3396d04ccbb3Scarlsonj dhcp_walk_interfaces(uint_t flags_on, uint_t flags_off, int af,
3397d04ccbb3Scarlsonj     boolean_t printed_one)
33987c478bd9Sstevel@tonic-gate {
3399d04ccbb3Scarlsonj 	struct lifnum	lifn;
3400d04ccbb3Scarlsonj 	struct lifconf	lifc;
34017c478bd9Sstevel@tonic-gate 	int		n_ifs, i, sock_fd;
34027c478bd9Sstevel@tonic-gate 
3403d04ccbb3Scarlsonj 	sock_fd = socket(af, SOCK_DGRAM, 0);
34047c478bd9Sstevel@tonic-gate 	if (sock_fd == -1)
3405d04ccbb3Scarlsonj 		return (printed_one);
34067c478bd9Sstevel@tonic-gate 
3407d04ccbb3Scarlsonj 	/*
3408d04ccbb3Scarlsonj 	 * SIOCGLIFNUM is just an estimate.  If the ioctl fails, we don't care;
3409d04ccbb3Scarlsonj 	 * just drive on and use SIOCGLIFCONF with increasing buffer sizes, as
3410d04ccbb3Scarlsonj 	 * is traditional.
3411d04ccbb3Scarlsonj 	 */
3412d04ccbb3Scarlsonj 	(void) memset(&lifn, 0, sizeof (lifn));
3413d04ccbb3Scarlsonj 	lifn.lifn_family = af;
3414e11c3f44Smeem 	lifn.lifn_flags = LIFC_ALLZONES | LIFC_NOXMIT | LIFC_UNDER_IPMP;
3415d04ccbb3Scarlsonj 	if (ioctl(sock_fd, SIOCGLIFNUM, &lifn) == -1)
3416d04ccbb3Scarlsonj 		n_ifs = LIFN_GUARD_VALUE;
3417d04ccbb3Scarlsonj 	else
3418d04ccbb3Scarlsonj 		n_ifs = lifn.lifn_count + LIFN_GUARD_VALUE;
3419d04ccbb3Scarlsonj 
3420d04ccbb3Scarlsonj 	(void) memset(&lifc, 0, sizeof (lifc));
3421d04ccbb3Scarlsonj 	lifc.lifc_family = af;
3422d04ccbb3Scarlsonj 	lifc.lifc_flags = lifn.lifn_flags;
3423d04ccbb3Scarlsonj 	lifc.lifc_len = n_ifs * sizeof (struct lifreq);
3424d04ccbb3Scarlsonj 	lifc.lifc_buf = malloc(lifc.lifc_len);
3425d04ccbb3Scarlsonj 	if (lifc.lifc_buf != NULL) {
3426d04ccbb3Scarlsonj 
3427d04ccbb3Scarlsonj 		if (ioctl(sock_fd, SIOCGLIFCONF, &lifc) == -1) {
34287c478bd9Sstevel@tonic-gate 			(void) close(sock_fd);
3429d04ccbb3Scarlsonj 			free(lifc.lifc_buf);
34307c478bd9Sstevel@tonic-gate 			return (NULL);
34317c478bd9Sstevel@tonic-gate 		}
34327c478bd9Sstevel@tonic-gate 
3433d04ccbb3Scarlsonj 		n_ifs = lifc.lifc_len / sizeof (struct lifreq);
34347c478bd9Sstevel@tonic-gate 
3435d04ccbb3Scarlsonj 		for (i = 0; i < n_ifs; i++) {
3436d04ccbb3Scarlsonj 			if (ioctl(sock_fd, SIOCGLIFFLAGS, &lifc.lifc_req[i]) ==
3437d04ccbb3Scarlsonj 			    0 && (lifc.lifc_req[i].lifr_flags & (flags_on |
3438d04ccbb3Scarlsonj 			    flags_off)) != flags_on)
3439d04ccbb3Scarlsonj 				continue;
3440d04ccbb3Scarlsonj 			printed_one = dhcp_do_ipc(DHCP_STATUS |
3441d04ccbb3Scarlsonj 			    (af == AF_INET6 ? DHCP_V6 : 0),
3442d04ccbb3Scarlsonj 			    lifc.lifc_req[i].lifr_name, printed_one);
3443d04ccbb3Scarlsonj 		}
34447c478bd9Sstevel@tonic-gate 	}
34457c478bd9Sstevel@tonic-gate 	(void) close(sock_fd);
3446d04ccbb3Scarlsonj 	free(lifc.lifc_buf);
3447d04ccbb3Scarlsonj 	return (printed_one);
34487c478bd9Sstevel@tonic-gate }
34497c478bd9Sstevel@tonic-gate 
34507c478bd9Sstevel@tonic-gate static void
34517c478bd9Sstevel@tonic-gate dhcp_report(char *ifname)
34527c478bd9Sstevel@tonic-gate {
3453d04ccbb3Scarlsonj 	boolean_t printed_one;
34547c478bd9Sstevel@tonic-gate 
3455d04ccbb3Scarlsonj 	if (!family_selected(AF_INET) && !family_selected(AF_INET6))
34567c478bd9Sstevel@tonic-gate 		return;
34577c478bd9Sstevel@tonic-gate 
3458d04ccbb3Scarlsonj 	printed_one = B_FALSE;
3459d04ccbb3Scarlsonj 	if (ifname != NULL) {
3460d04ccbb3Scarlsonj 		if (family_selected(AF_INET)) {
3461d04ccbb3Scarlsonj 			printed_one = dhcp_do_ipc(DHCP_STATUS, ifname,
3462d04ccbb3Scarlsonj 			    printed_one);
3463d04ccbb3Scarlsonj 		}
3464d04ccbb3Scarlsonj 		if (family_selected(AF_INET6)) {
3465d04ccbb3Scarlsonj 			printed_one = dhcp_do_ipc(DHCP_STATUS | DHCP_V6,
3466d04ccbb3Scarlsonj 			    ifname, printed_one);
3467d04ccbb3Scarlsonj 		}
3468d04ccbb3Scarlsonj 		if (!printed_one) {
3469d04ccbb3Scarlsonj 			fail(0, "%s: %s", ifname,
3470d04ccbb3Scarlsonj 			    dhcp_ipc_strerror(DHCP_IPC_E_UNKIF));
3471d04ccbb3Scarlsonj 		}
3472d04ccbb3Scarlsonj 	} else {
3473d04ccbb3Scarlsonj 		if (family_selected(AF_INET)) {
3474d04ccbb3Scarlsonj 			printed_one = dhcp_walk_interfaces(IFF_DHCPRUNNING,
3475d04ccbb3Scarlsonj 			    0, AF_INET, printed_one);
3476d04ccbb3Scarlsonj 		}
3477d04ccbb3Scarlsonj 		if (family_selected(AF_INET6)) {
3478d04ccbb3Scarlsonj 			(void) dhcp_walk_interfaces(IFF_DHCPRUNNING,
3479d04ccbb3Scarlsonj 			    IFF_ADDRCONF, AF_INET6, printed_one);
3480d04ccbb3Scarlsonj 		}
34817c478bd9Sstevel@tonic-gate 	}
34827c478bd9Sstevel@tonic-gate }
34837c478bd9Sstevel@tonic-gate 
34847c478bd9Sstevel@tonic-gate /* --------------------- GROUP_REPORT (netstat -g) ------------------------- */
34857c478bd9Sstevel@tonic-gate 
34867c478bd9Sstevel@tonic-gate static void
34877c478bd9Sstevel@tonic-gate group_report(mib_item_t *item)
34887c478bd9Sstevel@tonic-gate {
34897c478bd9Sstevel@tonic-gate 	mib_item_t	*v4grp = NULL, *v4src = NULL;
34907c478bd9Sstevel@tonic-gate 	mib_item_t	*v6grp = NULL, *v6src = NULL;
34917c478bd9Sstevel@tonic-gate 	int		jtemp = 0;
34927c478bd9Sstevel@tonic-gate 	char		ifname[LIFNAMSIZ + 1];
34937c478bd9Sstevel@tonic-gate 	char		abuf[MAXHOSTNAMELEN + 1];
34947c478bd9Sstevel@tonic-gate 	ip_member_t	*ipmp;
34957c478bd9Sstevel@tonic-gate 	ip_grpsrc_t	*ips;
34967c478bd9Sstevel@tonic-gate 	ipv6_member_t	*ipmp6;
34977c478bd9Sstevel@tonic-gate 	ipv6_grpsrc_t	*ips6;
34987c478bd9Sstevel@tonic-gate 	boolean_t	first, first_src;
34997c478bd9Sstevel@tonic-gate 
35007c478bd9Sstevel@tonic-gate 	/* 'for' loop 1: */
35017c478bd9Sstevel@tonic-gate 	for (; item; item = item->next_item) {
35027c478bd9Sstevel@tonic-gate 		if (Dflag) {
35037c478bd9Sstevel@tonic-gate 			(void) printf("\n--- Entry %d ---\n", ++jtemp);
35047c478bd9Sstevel@tonic-gate 			(void) printf("Group = %d, mib_id = %d, "
35057c478bd9Sstevel@tonic-gate 			    "length = %d, valp = 0x%p\n",
35067c478bd9Sstevel@tonic-gate 			    item->group, item->mib_id, item->length,
35077c478bd9Sstevel@tonic-gate 			    item->valp);
35087c478bd9Sstevel@tonic-gate 		}
35097c478bd9Sstevel@tonic-gate 		if (item->group == MIB2_IP && family_selected(AF_INET)) {
35107c478bd9Sstevel@tonic-gate 			switch (item->mib_id) {
35117c478bd9Sstevel@tonic-gate 			case EXPER_IP_GROUP_MEMBERSHIP:
35127c478bd9Sstevel@tonic-gate 				v4grp = item;
35137c478bd9Sstevel@tonic-gate 				if (Dflag)
35147c478bd9Sstevel@tonic-gate 					(void) printf("item is v4grp info\n");
35157c478bd9Sstevel@tonic-gate 				break;
35167c478bd9Sstevel@tonic-gate 			case EXPER_IP_GROUP_SOURCES:
35177c478bd9Sstevel@tonic-gate 				v4src = item;
35187c478bd9Sstevel@tonic-gate 				if (Dflag)
35197c478bd9Sstevel@tonic-gate 					(void) printf("item is v4src info\n");
35207c478bd9Sstevel@tonic-gate 				break;
35217c478bd9Sstevel@tonic-gate 			default:
35227c478bd9Sstevel@tonic-gate 				continue;
35237c478bd9Sstevel@tonic-gate 			}
35247c478bd9Sstevel@tonic-gate 			continue;
35257c478bd9Sstevel@tonic-gate 		}
35267c478bd9Sstevel@tonic-gate 		if (item->group == MIB2_IP6 && family_selected(AF_INET6)) {
35277c478bd9Sstevel@tonic-gate 			switch (item->mib_id) {
35287c478bd9Sstevel@tonic-gate 			case EXPER_IP6_GROUP_MEMBERSHIP:
35297c478bd9Sstevel@tonic-gate 				v6grp = item;
35307c478bd9Sstevel@tonic-gate 				if (Dflag)
35317c478bd9Sstevel@tonic-gate 					(void) printf("item is v6grp info\n");
35327c478bd9Sstevel@tonic-gate 				break;
35337c478bd9Sstevel@tonic-gate 			case EXPER_IP6_GROUP_SOURCES:
35347c478bd9Sstevel@tonic-gate 				v6src = item;
35357c478bd9Sstevel@tonic-gate 				if (Dflag)
35367c478bd9Sstevel@tonic-gate 					(void) printf("item is v6src info\n");
35377c478bd9Sstevel@tonic-gate 				break;
35387c478bd9Sstevel@tonic-gate 			default:
35397c478bd9Sstevel@tonic-gate 				continue;
35407c478bd9Sstevel@tonic-gate 			}
35417c478bd9Sstevel@tonic-gate 		}
35427c478bd9Sstevel@tonic-gate 	}
35437c478bd9Sstevel@tonic-gate 
35447c478bd9Sstevel@tonic-gate 	if (family_selected(AF_INET) && v4grp != NULL) {
35457c478bd9Sstevel@tonic-gate 		if (Dflag)
35467c478bd9Sstevel@tonic-gate 			(void) printf("%u records for ipGroupMember:\n",
35477c478bd9Sstevel@tonic-gate 			    v4grp->length / sizeof (ip_member_t));
35487c478bd9Sstevel@tonic-gate 
35497c478bd9Sstevel@tonic-gate 		first = B_TRUE;
35507c478bd9Sstevel@tonic-gate 		for (ipmp = (ip_member_t *)v4grp->valp;
35517c478bd9Sstevel@tonic-gate 		    (char *)ipmp < (char *)v4grp->valp + v4grp->length;
35527c478bd9Sstevel@tonic-gate 		    /* LINTED: (note 1) */
35537c478bd9Sstevel@tonic-gate 		    ipmp = (ip_member_t *)((char *)ipmp + ipMemberEntrySize)) {
35547c478bd9Sstevel@tonic-gate 			if (first) {
35557c478bd9Sstevel@tonic-gate 				(void) puts(v4compat ?
35567c478bd9Sstevel@tonic-gate 				    "Group Memberships" :
35577c478bd9Sstevel@tonic-gate 				    "Group Memberships: IPv4");
35587c478bd9Sstevel@tonic-gate 				(void) puts("Interface "
35597c478bd9Sstevel@tonic-gate 				    "Group                RefCnt");
35607c478bd9Sstevel@tonic-gate 				(void) puts("--------- "
35617c478bd9Sstevel@tonic-gate 				    "-------------------- ------");
35627c478bd9Sstevel@tonic-gate 				first = B_FALSE;
35637c478bd9Sstevel@tonic-gate 			}
35647c478bd9Sstevel@tonic-gate 
35657c478bd9Sstevel@tonic-gate 			(void) printf("%-9s %-20s %6u\n",
35667c478bd9Sstevel@tonic-gate 			    octetstr(&ipmp->ipGroupMemberIfIndex, 'a',
35677c478bd9Sstevel@tonic-gate 			    ifname, sizeof (ifname)),
35687c478bd9Sstevel@tonic-gate 			    pr_addr(ipmp->ipGroupMemberAddress,
35697c478bd9Sstevel@tonic-gate 			    abuf, sizeof (abuf)),
35707c478bd9Sstevel@tonic-gate 			    ipmp->ipGroupMemberRefCnt);
35717c478bd9Sstevel@tonic-gate 
35727c478bd9Sstevel@tonic-gate 
35737c478bd9Sstevel@tonic-gate 			if (!Vflag || v4src == NULL)
35747c478bd9Sstevel@tonic-gate 				continue;
35757c478bd9Sstevel@tonic-gate 
35767c478bd9Sstevel@tonic-gate 			if (Dflag)
35777c478bd9Sstevel@tonic-gate 				(void) printf("scanning %u ipGroupSource "
35787c478bd9Sstevel@tonic-gate 				    "records...\n",
35797c478bd9Sstevel@tonic-gate 				    v4src->length/sizeof (ip_grpsrc_t));
35807c478bd9Sstevel@tonic-gate 
35817c478bd9Sstevel@tonic-gate 			first_src = B_TRUE;
35827c478bd9Sstevel@tonic-gate 			for (ips = (ip_grpsrc_t *)v4src->valp;
35837c478bd9Sstevel@tonic-gate 			    (char *)ips < (char *)v4src->valp + v4src->length;
35847c478bd9Sstevel@tonic-gate 			    /* LINTED: (note 1) */
35857c478bd9Sstevel@tonic-gate 			    ips = (ip_grpsrc_t *)((char *)ips +
35867c478bd9Sstevel@tonic-gate 			    ipGroupSourceEntrySize)) {
35877c478bd9Sstevel@tonic-gate 				/*
35887c478bd9Sstevel@tonic-gate 				 * We assume that all source addrs for a given
35897c478bd9Sstevel@tonic-gate 				 * interface/group pair are contiguous, so on
35907c478bd9Sstevel@tonic-gate 				 * the first non-match after we've found at
35917c478bd9Sstevel@tonic-gate 				 * least one, we bail.
35927c478bd9Sstevel@tonic-gate 				 */
35937c478bd9Sstevel@tonic-gate 				if ((ipmp->ipGroupMemberAddress !=
35947c478bd9Sstevel@tonic-gate 				    ips->ipGroupSourceGroup) ||
35957c478bd9Sstevel@tonic-gate 				    (!octetstrmatch(&ipmp->ipGroupMemberIfIndex,
35967c478bd9Sstevel@tonic-gate 				    &ips->ipGroupSourceIfIndex))) {
35977c478bd9Sstevel@tonic-gate 					if (first_src)
35987c478bd9Sstevel@tonic-gate 						continue;
35997c478bd9Sstevel@tonic-gate 					else
36007c478bd9Sstevel@tonic-gate 						break;
36017c478bd9Sstevel@tonic-gate 				}
36027c478bd9Sstevel@tonic-gate 				if (first_src) {
36037c478bd9Sstevel@tonic-gate 					(void) printf("\t%s:    %s\n",
36047c478bd9Sstevel@tonic-gate 					    fmodestr(
36057c478bd9Sstevel@tonic-gate 					    ipmp->ipGroupMemberFilterMode),
36067c478bd9Sstevel@tonic-gate 					    pr_addr(ips->ipGroupSourceAddress,
36077c478bd9Sstevel@tonic-gate 					    abuf, sizeof (abuf)));
36087c478bd9Sstevel@tonic-gate 					first_src = B_FALSE;
36097c478bd9Sstevel@tonic-gate 					continue;
36107c478bd9Sstevel@tonic-gate 				}
36117c478bd9Sstevel@tonic-gate 
36127c478bd9Sstevel@tonic-gate 				(void) printf("\t            %s\n",
36137c478bd9Sstevel@tonic-gate 				    pr_addr(ips->ipGroupSourceAddress, abuf,
36147c478bd9Sstevel@tonic-gate 				    sizeof (abuf)));
36157c478bd9Sstevel@tonic-gate 			}
36167c478bd9Sstevel@tonic-gate 		}
36177c478bd9Sstevel@tonic-gate 		(void) putchar('\n');
36187c478bd9Sstevel@tonic-gate 	}
36197c478bd9Sstevel@tonic-gate 
36207c478bd9Sstevel@tonic-gate 	if (family_selected(AF_INET6) && v6grp != NULL) {
36217c478bd9Sstevel@tonic-gate 		if (Dflag)
36227c478bd9Sstevel@tonic-gate 			(void) printf("%u records for ipv6GroupMember:\n",
36237c478bd9Sstevel@tonic-gate 			    v6grp->length / sizeof (ipv6_member_t));
36247c478bd9Sstevel@tonic-gate 
36257c478bd9Sstevel@tonic-gate 		first = B_TRUE;
36267c478bd9Sstevel@tonic-gate 		for (ipmp6 = (ipv6_member_t *)v6grp->valp;
36277c478bd9Sstevel@tonic-gate 		    (char *)ipmp6 < (char *)v6grp->valp + v6grp->length;
36287c478bd9Sstevel@tonic-gate 		    /* LINTED: (note 1) */
36297c478bd9Sstevel@tonic-gate 		    ipmp6 = (ipv6_member_t *)((char *)ipmp6 +
3630e11c3f44Smeem 		    ipv6MemberEntrySize)) {
36317c478bd9Sstevel@tonic-gate 			if (first) {
36327c478bd9Sstevel@tonic-gate 				(void) puts("Group Memberships: "
36337c478bd9Sstevel@tonic-gate 				    "IPv6");
36347c478bd9Sstevel@tonic-gate 				(void) puts(" If       "
36357c478bd9Sstevel@tonic-gate 				    "Group                   RefCnt");
36367c478bd9Sstevel@tonic-gate 				(void) puts("----- "
36377c478bd9Sstevel@tonic-gate 				    "--------------------------- ------");
36387c478bd9Sstevel@tonic-gate 				first = B_FALSE;
36397c478bd9Sstevel@tonic-gate 			}
36407c478bd9Sstevel@tonic-gate 
36417c478bd9Sstevel@tonic-gate 			(void) printf("%-5s %-27s %5u\n",
3642e11c3f44Smeem 			    ifindex2str(ipmp6->ipv6GroupMemberIfIndex, ifname),
36437c478bd9Sstevel@tonic-gate 			    pr_addr6(&ipmp6->ipv6GroupMemberAddress,
36447c478bd9Sstevel@tonic-gate 			    abuf, sizeof (abuf)),
36457c478bd9Sstevel@tonic-gate 			    ipmp6->ipv6GroupMemberRefCnt);
36467c478bd9Sstevel@tonic-gate 
36477c478bd9Sstevel@tonic-gate 			if (!Vflag || v6src == NULL)
36487c478bd9Sstevel@tonic-gate 				continue;
36497c478bd9Sstevel@tonic-gate 
36507c478bd9Sstevel@tonic-gate 			if (Dflag)
36517c478bd9Sstevel@tonic-gate 				(void) printf("scanning %u ipv6GroupSource "
36527c478bd9Sstevel@tonic-gate 				    "records...\n",
36537c478bd9Sstevel@tonic-gate 				    v6src->length/sizeof (ipv6_grpsrc_t));
36547c478bd9Sstevel@tonic-gate 
36557c478bd9Sstevel@tonic-gate 			first_src = B_TRUE;
36567c478bd9Sstevel@tonic-gate 			for (ips6 = (ipv6_grpsrc_t *)v6src->valp;
36577c478bd9Sstevel@tonic-gate 			    (char *)ips6 < (char *)v6src->valp + v6src->length;
36587c478bd9Sstevel@tonic-gate 			    /* LINTED: (note 1) */
36597c478bd9Sstevel@tonic-gate 			    ips6 = (ipv6_grpsrc_t *)((char *)ips6 +
36607c478bd9Sstevel@tonic-gate 			    ipv6GroupSourceEntrySize)) {
36617c478bd9Sstevel@tonic-gate 				/* same assumption as in the v4 case above */
36627c478bd9Sstevel@tonic-gate 				if ((ipmp6->ipv6GroupMemberIfIndex !=
36637c478bd9Sstevel@tonic-gate 				    ips6->ipv6GroupSourceIfIndex) ||
36647c478bd9Sstevel@tonic-gate 				    (!IN6_ARE_ADDR_EQUAL(
36657c478bd9Sstevel@tonic-gate 				    &ipmp6->ipv6GroupMemberAddress,
36667c478bd9Sstevel@tonic-gate 				    &ips6->ipv6GroupSourceGroup))) {
36677c478bd9Sstevel@tonic-gate 					if (first_src)
36687c478bd9Sstevel@tonic-gate 						continue;
36697c478bd9Sstevel@tonic-gate 					else
36707c478bd9Sstevel@tonic-gate 						break;
36717c478bd9Sstevel@tonic-gate 				}
36727c478bd9Sstevel@tonic-gate 				if (first_src) {
36737c478bd9Sstevel@tonic-gate 					(void) printf("\t%s:    %s\n",
36747c478bd9Sstevel@tonic-gate 					    fmodestr(
36757c478bd9Sstevel@tonic-gate 					    ipmp6->ipv6GroupMemberFilterMode),
36767c478bd9Sstevel@tonic-gate 					    pr_addr6(
36777c478bd9Sstevel@tonic-gate 					    &ips6->ipv6GroupSourceAddress,
36787c478bd9Sstevel@tonic-gate 					    abuf, sizeof (abuf)));
36797c478bd9Sstevel@tonic-gate 					first_src = B_FALSE;
36807c478bd9Sstevel@tonic-gate 					continue;
36817c478bd9Sstevel@tonic-gate 				}
36827c478bd9Sstevel@tonic-gate 
36837c478bd9Sstevel@tonic-gate 				(void) printf("\t            %s\n",
36847c478bd9Sstevel@tonic-gate 				    pr_addr6(&ips6->ipv6GroupSourceAddress,
36857c478bd9Sstevel@tonic-gate 				    abuf, sizeof (abuf)));
36867c478bd9Sstevel@tonic-gate 			}
36877c478bd9Sstevel@tonic-gate 		}
36887c478bd9Sstevel@tonic-gate 		(void) putchar('\n');
36897c478bd9Sstevel@tonic-gate 	}
36907c478bd9Sstevel@tonic-gate 
36917c478bd9Sstevel@tonic-gate 	(void) putchar('\n');
36927c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
36937c478bd9Sstevel@tonic-gate }
36947c478bd9Sstevel@tonic-gate 
36957c478bd9Sstevel@tonic-gate /* --------------------- ARP_REPORT (netstat -p) -------------------------- */
36967c478bd9Sstevel@tonic-gate 
36977c478bd9Sstevel@tonic-gate static void
36987c478bd9Sstevel@tonic-gate arp_report(mib_item_t *item)
36997c478bd9Sstevel@tonic-gate {
37007c478bd9Sstevel@tonic-gate 	int		jtemp = 0;
37017c478bd9Sstevel@tonic-gate 	char		ifname[LIFNAMSIZ + 1];
37027c478bd9Sstevel@tonic-gate 	char		abuf[MAXHOSTNAMELEN + 1];
37037c478bd9Sstevel@tonic-gate 	char		maskbuf[STR_EXPAND * OCTET_LENGTH + 1];
37047c478bd9Sstevel@tonic-gate 	char		flbuf[32];	/* ACE_F_ flags */
37057c478bd9Sstevel@tonic-gate 	char		xbuf[STR_EXPAND * OCTET_LENGTH + 1];
37067c478bd9Sstevel@tonic-gate 	mib2_ipNetToMediaEntry_t	*np;
37077c478bd9Sstevel@tonic-gate 	int		flags;
37087c478bd9Sstevel@tonic-gate 	boolean_t	first;
37097c478bd9Sstevel@tonic-gate 
37107c478bd9Sstevel@tonic-gate 	if (!(family_selected(AF_INET)))
37117c478bd9Sstevel@tonic-gate 		return;
37127c478bd9Sstevel@tonic-gate 
37137c478bd9Sstevel@tonic-gate 	/* 'for' loop 1: */
37147c478bd9Sstevel@tonic-gate 	for (; item; item = item->next_item) {
37157c478bd9Sstevel@tonic-gate 		if (Dflag) {
37167c478bd9Sstevel@tonic-gate 			(void) printf("\n--- Entry %d ---\n", ++jtemp);
37177c478bd9Sstevel@tonic-gate 			(void) printf("Group = %d, mib_id = %d, "
37187c478bd9Sstevel@tonic-gate 			    "length = %d, valp = 0x%p\n",
37197c478bd9Sstevel@tonic-gate 			    item->group, item->mib_id, item->length,
37207c478bd9Sstevel@tonic-gate 			    item->valp);
37217c478bd9Sstevel@tonic-gate 		}
37227c478bd9Sstevel@tonic-gate 		if (!(item->group == MIB2_IP && item->mib_id == MIB2_IP_MEDIA))
37237c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
37247c478bd9Sstevel@tonic-gate 
37257c478bd9Sstevel@tonic-gate 		if (Dflag)
37267c478bd9Sstevel@tonic-gate 			(void) printf("%u records for "
37277c478bd9Sstevel@tonic-gate 			    "ipNetToMediaEntryTable:\n",
37287c478bd9Sstevel@tonic-gate 			    item->length/sizeof (mib2_ipNetToMediaEntry_t));
37297c478bd9Sstevel@tonic-gate 
37307c478bd9Sstevel@tonic-gate 		first = B_TRUE;
37317c478bd9Sstevel@tonic-gate 		/* 'for' loop 2: */
37327c478bd9Sstevel@tonic-gate 		for (np = (mib2_ipNetToMediaEntry_t *)item->valp;
37337c478bd9Sstevel@tonic-gate 		    (char *)np < (char *)item->valp + item->length;
37347c478bd9Sstevel@tonic-gate 		    /* LINTED: (note 1) */
37357c478bd9Sstevel@tonic-gate 		    np = (mib2_ipNetToMediaEntry_t *)((char *)np +
37367c478bd9Sstevel@tonic-gate 		    ipNetToMediaEntrySize)) {
37377c478bd9Sstevel@tonic-gate 			if (first) {
37387c478bd9Sstevel@tonic-gate 				(void) puts(v4compat ?
37397c478bd9Sstevel@tonic-gate 				    "Net to Media Table" :
37407c478bd9Sstevel@tonic-gate 				    "Net to Media Table: IPv4");
374169bb4bb4Scarlsonj 				(void) puts("Device "
374269bb4bb4Scarlsonj 				    "  IP Address               Mask      "
374369bb4bb4Scarlsonj 				    "Flags      Phys Addr");
374469bb4bb4Scarlsonj 				(void) puts("------ "
374569bb4bb4Scarlsonj 				    "-------------------- --------------- "
374669bb4bb4Scarlsonj 				    "-------- ---------------");
37477c478bd9Sstevel@tonic-gate 				first = B_FALSE;
37487c478bd9Sstevel@tonic-gate 			}
37497c478bd9Sstevel@tonic-gate 
37507c478bd9Sstevel@tonic-gate 			flbuf[0] = '\0';
37517c478bd9Sstevel@tonic-gate 			flags = np->ipNetToMediaInfo.ntm_flags;
375269bb4bb4Scarlsonj 			/*
375369bb4bb4Scarlsonj 			 * Note that not all flags are possible at the same
375469bb4bb4Scarlsonj 			 * time.  Patterns: SPLAy DUo
375569bb4bb4Scarlsonj 			 */
37567c478bd9Sstevel@tonic-gate 			if (flags & ACE_F_PERMANENT)
37577c478bd9Sstevel@tonic-gate 				(void) strcat(flbuf, "S");
37587c478bd9Sstevel@tonic-gate 			if (flags & ACE_F_PUBLISH)
37597c478bd9Sstevel@tonic-gate 				(void) strcat(flbuf, "P");
37607c478bd9Sstevel@tonic-gate 			if (flags & ACE_F_DYING)
37617c478bd9Sstevel@tonic-gate 				(void) strcat(flbuf, "D");
37627c478bd9Sstevel@tonic-gate 			if (!(flags & ACE_F_RESOLVED))
37637c478bd9Sstevel@tonic-gate 				(void) strcat(flbuf, "U");
37647c478bd9Sstevel@tonic-gate 			if (flags & ACE_F_MAPPING)
37657c478bd9Sstevel@tonic-gate 				(void) strcat(flbuf, "M");
376669bb4bb4Scarlsonj 			if (flags & ACE_F_MYADDR)
376769bb4bb4Scarlsonj 				(void) strcat(flbuf, "L");
376869bb4bb4Scarlsonj 			if (flags & ACE_F_UNVERIFIED)
376969bb4bb4Scarlsonj 				(void) strcat(flbuf, "d");
377069bb4bb4Scarlsonj 			if (flags & ACE_F_AUTHORITY)
377169bb4bb4Scarlsonj 				(void) strcat(flbuf, "A");
377269bb4bb4Scarlsonj 			if (flags & ACE_F_OLD)
377369bb4bb4Scarlsonj 				(void) strcat(flbuf, "o");
377469bb4bb4Scarlsonj 			if (flags & ACE_F_DELAYED)
377569bb4bb4Scarlsonj 				(void) strcat(flbuf, "y");
377669bb4bb4Scarlsonj 			(void) printf("%-6s %-20s %-15s %-8s %s\n",
37777c478bd9Sstevel@tonic-gate 			    octetstr(&np->ipNetToMediaIfIndex, 'a',
37787c478bd9Sstevel@tonic-gate 			    ifname, sizeof (ifname)),
37797c478bd9Sstevel@tonic-gate 			    pr_addr(np->ipNetToMediaNetAddress,
37807c478bd9Sstevel@tonic-gate 			    abuf, sizeof (abuf)),
37817c478bd9Sstevel@tonic-gate 			    octetstr(&np->ipNetToMediaInfo.ntm_mask, 'd',
37827c478bd9Sstevel@tonic-gate 			    maskbuf, sizeof (maskbuf)),
37837c478bd9Sstevel@tonic-gate 			    flbuf,
37847c478bd9Sstevel@tonic-gate 			    octetstr(&np->ipNetToMediaPhysAddress, 'h',
37857c478bd9Sstevel@tonic-gate 			    xbuf, sizeof (xbuf)));
37867c478bd9Sstevel@tonic-gate 		} /* 'for' loop 2 ends */
37877c478bd9Sstevel@tonic-gate 	} /* 'for' loop 1 ends */
37887c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
37897c478bd9Sstevel@tonic-gate }
37907c478bd9Sstevel@tonic-gate 
37917c478bd9Sstevel@tonic-gate /* --------------------- NDP_REPORT (netstat -p) -------------------------- */
37927c478bd9Sstevel@tonic-gate 
37937c478bd9Sstevel@tonic-gate static void
37947c478bd9Sstevel@tonic-gate ndp_report(mib_item_t *item)
37957c478bd9Sstevel@tonic-gate {
37967c478bd9Sstevel@tonic-gate 	int		jtemp = 0;
37977c478bd9Sstevel@tonic-gate 	char		abuf[MAXHOSTNAMELEN + 1];
37987c478bd9Sstevel@tonic-gate 	char		*state;
37997c478bd9Sstevel@tonic-gate 	char		*type;
38007c478bd9Sstevel@tonic-gate 	char		xbuf[STR_EXPAND * OCTET_LENGTH + 1];
38017c478bd9Sstevel@tonic-gate 	mib2_ipv6NetToMediaEntry_t	*np6;
38027c478bd9Sstevel@tonic-gate 	char		ifname[LIFNAMSIZ + 1];
38037c478bd9Sstevel@tonic-gate 	boolean_t	first;
38047c478bd9Sstevel@tonic-gate 
38057c478bd9Sstevel@tonic-gate 	if (!(family_selected(AF_INET6)))
38067c478bd9Sstevel@tonic-gate 		return;
38077c478bd9Sstevel@tonic-gate 
38087c478bd9Sstevel@tonic-gate 	/* 'for' loop 1: */
38097c478bd9Sstevel@tonic-gate 	for (; item; item = item->next_item) {
38107c478bd9Sstevel@tonic-gate 		if (Dflag) {
38117c478bd9Sstevel@tonic-gate 			(void) printf("\n--- Entry %d ---\n", ++jtemp);
38127c478bd9Sstevel@tonic-gate 			(void) printf("Group = %d, mib_id = %d, "
38137c478bd9Sstevel@tonic-gate 			    "length = %d, valp = 0x%p\n",
38147c478bd9Sstevel@tonic-gate 			    item->group, item->mib_id, item->length,
38157c478bd9Sstevel@tonic-gate 			    item->valp);
38167c478bd9Sstevel@tonic-gate 		}
38177c478bd9Sstevel@tonic-gate 		if (!(item->group == MIB2_IP6 &&
38187c478bd9Sstevel@tonic-gate 		    item->mib_id == MIB2_IP6_MEDIA))
38197c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
38207c478bd9Sstevel@tonic-gate 
38217c478bd9Sstevel@tonic-gate 		first = B_TRUE;
38227c478bd9Sstevel@tonic-gate 		/* 'for' loop 2: */
38237c478bd9Sstevel@tonic-gate 		for (np6 = (mib2_ipv6NetToMediaEntry_t *)item->valp;
38247c478bd9Sstevel@tonic-gate 		    (char *)np6 < (char *)item->valp + item->length;
38257c478bd9Sstevel@tonic-gate 		    /* LINTED: (note 1) */
38267c478bd9Sstevel@tonic-gate 		    np6 = (mib2_ipv6NetToMediaEntry_t *)((char *)np6 +
38277c478bd9Sstevel@tonic-gate 		    ipv6NetToMediaEntrySize)) {
38287c478bd9Sstevel@tonic-gate 			if (first) {
38297c478bd9Sstevel@tonic-gate 				(void) puts("\nNet to Media Table: IPv6");
38307c478bd9Sstevel@tonic-gate 				(void) puts(" If   Physical Address   "
38317c478bd9Sstevel@tonic-gate 				    " Type      State      Destination/Mask");
38327c478bd9Sstevel@tonic-gate 				(void) puts("----- -----------------  "
38337c478bd9Sstevel@tonic-gate 				    "------- ------------ "
38347c478bd9Sstevel@tonic-gate 				    "---------------------------");
38357c478bd9Sstevel@tonic-gate 				first = B_FALSE;
38367c478bd9Sstevel@tonic-gate 			}
38377c478bd9Sstevel@tonic-gate 
38387c478bd9Sstevel@tonic-gate 			switch (np6->ipv6NetToMediaState) {
38397c478bd9Sstevel@tonic-gate 			case ND_INCOMPLETE:
38407c478bd9Sstevel@tonic-gate 				state = "INCOMPLETE";
38417c478bd9Sstevel@tonic-gate 				break;
38427c478bd9Sstevel@tonic-gate 			case ND_REACHABLE:
38437c478bd9Sstevel@tonic-gate 				state = "REACHABLE";
38447c478bd9Sstevel@tonic-gate 				break;
38457c478bd9Sstevel@tonic-gate 			case ND_STALE:
38467c478bd9Sstevel@tonic-gate 				state = "STALE";
38477c478bd9Sstevel@tonic-gate 				break;
38487c478bd9Sstevel@tonic-gate 			case ND_DELAY:
38497c478bd9Sstevel@tonic-gate 				state = "DELAY";
38507c478bd9Sstevel@tonic-gate 				break;
38517c478bd9Sstevel@tonic-gate 			case ND_PROBE:
38527c478bd9Sstevel@tonic-gate 				state = "PROBE";
38537c478bd9Sstevel@tonic-gate 				break;
38547c478bd9Sstevel@tonic-gate 			case ND_UNREACHABLE:
38557c478bd9Sstevel@tonic-gate 				state = "UNREACHABLE";
38567c478bd9Sstevel@tonic-gate 				break;
38577c478bd9Sstevel@tonic-gate 			default:
38587c478bd9Sstevel@tonic-gate 				state = "UNKNOWN";
38597c478bd9Sstevel@tonic-gate 			}
38607c478bd9Sstevel@tonic-gate 
38617c478bd9Sstevel@tonic-gate 			switch (np6->ipv6NetToMediaType) {
38627c478bd9Sstevel@tonic-gate 			case 1:
38637c478bd9Sstevel@tonic-gate 				type = "other";
38647c478bd9Sstevel@tonic-gate 				break;
38657c478bd9Sstevel@tonic-gate 			case 2:
38667c478bd9Sstevel@tonic-gate 				type = "dynamic";
38677c478bd9Sstevel@tonic-gate 				break;
38687c478bd9Sstevel@tonic-gate 			case 3:
38697c478bd9Sstevel@tonic-gate 				type = "static";
38707c478bd9Sstevel@tonic-gate 				break;
38717c478bd9Sstevel@tonic-gate 			case 4:
38727c478bd9Sstevel@tonic-gate 				type = "local";
38737c478bd9Sstevel@tonic-gate 				break;
38747c478bd9Sstevel@tonic-gate 			}
38757c478bd9Sstevel@tonic-gate 			(void) printf("%-5s %-17s  %-7s %-12s %-27s\n",
3876e11c3f44Smeem 			    ifindex2str(np6->ipv6NetToMediaIfIndex, ifname),
38777c478bd9Sstevel@tonic-gate 			    octetstr(&np6->ipv6NetToMediaPhysAddress, 'h',
38787c478bd9Sstevel@tonic-gate 			    xbuf, sizeof (xbuf)),
38797c478bd9Sstevel@tonic-gate 			    type,
38807c478bd9Sstevel@tonic-gate 			    state,
38817c478bd9Sstevel@tonic-gate 			    pr_addr6(&np6->ipv6NetToMediaNetAddress,
38827c478bd9Sstevel@tonic-gate 			    abuf, sizeof (abuf)));
38837c478bd9Sstevel@tonic-gate 		} /* 'for' loop 2 ends */
38847c478bd9Sstevel@tonic-gate 	} /* 'for' loop 1 ends */
38857c478bd9Sstevel@tonic-gate 	(void) putchar('\n');
38867c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
38877c478bd9Sstevel@tonic-gate }
38887c478bd9Sstevel@tonic-gate 
38897c478bd9Sstevel@tonic-gate /* ------------------------- ire_report (netstat -r) ------------------------ */
38907c478bd9Sstevel@tonic-gate 
389145916cd2Sjpk typedef struct sec_attr_list_s {
389245916cd2Sjpk 	struct sec_attr_list_s *sal_next;
389345916cd2Sjpk 	const mib2_ipAttributeEntry_t *sal_attr;
389445916cd2Sjpk } sec_attr_list_t;
389545916cd2Sjpk 
389645916cd2Sjpk static boolean_t ire_report_item_v4(const mib2_ipRouteEntry_t *, boolean_t,
389745916cd2Sjpk     const sec_attr_list_t *);
389845916cd2Sjpk static boolean_t ire_report_item_v6(const mib2_ipv6RouteEntry_t *, boolean_t,
389945916cd2Sjpk     const sec_attr_list_t *);
390045916cd2Sjpk static const char *pr_secattr(const sec_attr_list_t *);
39017c478bd9Sstevel@tonic-gate 
39027c478bd9Sstevel@tonic-gate static void
390345916cd2Sjpk ire_report(const mib_item_t *item)
39047c478bd9Sstevel@tonic-gate {
39057c478bd9Sstevel@tonic-gate 	int			jtemp = 0;
39067c478bd9Sstevel@tonic-gate 	boolean_t		print_hdr_once_v4 = B_TRUE;
39077c478bd9Sstevel@tonic-gate 	boolean_t		print_hdr_once_v6 = B_TRUE;
39087c478bd9Sstevel@tonic-gate 	mib2_ipRouteEntry_t	*rp;
39097c478bd9Sstevel@tonic-gate 	mib2_ipv6RouteEntry_t	*rp6;
391045916cd2Sjpk 	sec_attr_list_t		**v4_attrs, **v4a;
391145916cd2Sjpk 	sec_attr_list_t		**v6_attrs, **v6a;
391245916cd2Sjpk 	sec_attr_list_t		*all_attrs, *aptr;
391345916cd2Sjpk 	const mib_item_t	*iptr;
391445916cd2Sjpk 	int			ipv4_route_count, ipv6_route_count;
391545916cd2Sjpk 	int			route_attrs_count;
391645916cd2Sjpk 
391745916cd2Sjpk 	/*
391845916cd2Sjpk 	 * Preparation pass: the kernel returns separate entries for IP routing
391945916cd2Sjpk 	 * table entries and security attributes.  We loop through the
392045916cd2Sjpk 	 * attributes first and link them into lists.
392145916cd2Sjpk 	 */
392245916cd2Sjpk 	ipv4_route_count = ipv6_route_count = route_attrs_count = 0;
392345916cd2Sjpk 	for (iptr = item; iptr != NULL; iptr = iptr->next_item) {
392445916cd2Sjpk 		if (iptr->group == MIB2_IP6 && iptr->mib_id == MIB2_IP6_ROUTE)
392545916cd2Sjpk 			ipv6_route_count += iptr->length / ipv6RouteEntrySize;
392645916cd2Sjpk 		if (iptr->group == MIB2_IP && iptr->mib_id == MIB2_IP_ROUTE)
392745916cd2Sjpk 			ipv4_route_count += iptr->length / ipRouteEntrySize;
392845916cd2Sjpk 		if ((iptr->group == MIB2_IP || iptr->group == MIB2_IP6) &&
392945916cd2Sjpk 		    iptr->mib_id == EXPER_IP_RTATTR)
393045916cd2Sjpk 			route_attrs_count += iptr->length /
393145916cd2Sjpk 			    ipRouteAttributeSize;
393245916cd2Sjpk 	}
393345916cd2Sjpk 	v4_attrs = v6_attrs = NULL;
393445916cd2Sjpk 	all_attrs = NULL;
393545916cd2Sjpk 	if (family_selected(AF_INET) && ipv4_route_count > 0) {
393645916cd2Sjpk 		v4_attrs = calloc(ipv4_route_count, sizeof (*v4_attrs));
393745916cd2Sjpk 		if (v4_attrs == NULL) {
393845916cd2Sjpk 			perror("ire_report calloc v4_attrs failed");
393945916cd2Sjpk 			return;
394045916cd2Sjpk 		}
394145916cd2Sjpk 	}
394245916cd2Sjpk 	if (family_selected(AF_INET6) && ipv6_route_count > 0) {
394345916cd2Sjpk 		v6_attrs = calloc(ipv6_route_count, sizeof (*v6_attrs));
394445916cd2Sjpk 		if (v6_attrs == NULL) {
394545916cd2Sjpk 			perror("ire_report calloc v6_attrs failed");
394645916cd2Sjpk 			goto ire_report_done;
394745916cd2Sjpk 		}
394845916cd2Sjpk 	}
394945916cd2Sjpk 	if (route_attrs_count > 0) {
395045916cd2Sjpk 		all_attrs = malloc(route_attrs_count * sizeof (*all_attrs));
395145916cd2Sjpk 		if (all_attrs == NULL) {
395245916cd2Sjpk 			perror("ire_report malloc all_attrs failed");
395345916cd2Sjpk 			goto ire_report_done;
395445916cd2Sjpk 		}
395545916cd2Sjpk 	}
395645916cd2Sjpk 	aptr = all_attrs;
395745916cd2Sjpk 	for (iptr = item; iptr != NULL; iptr = iptr->next_item) {
395845916cd2Sjpk 		mib2_ipAttributeEntry_t *iae;
395945916cd2Sjpk 		sec_attr_list_t **alp;
396045916cd2Sjpk 
396145916cd2Sjpk 		if (v4_attrs != NULL && iptr->group == MIB2_IP &&
396245916cd2Sjpk 		    iptr->mib_id == EXPER_IP_RTATTR) {
396345916cd2Sjpk 			alp = v4_attrs;
396445916cd2Sjpk 		} else if (v6_attrs != NULL && iptr->group == MIB2_IP6 &&
396545916cd2Sjpk 		    iptr->mib_id == EXPER_IP_RTATTR) {
396645916cd2Sjpk 			alp = v6_attrs;
396745916cd2Sjpk 		} else {
396845916cd2Sjpk 			continue;
396945916cd2Sjpk 		}
397045916cd2Sjpk 		for (iae = iptr->valp;
397145916cd2Sjpk 		    (char *)iae < (char *)iptr->valp + iptr->length;
397245916cd2Sjpk 		    /* LINTED: (note 1) */
397345916cd2Sjpk 		    iae = (mib2_ipAttributeEntry_t *)((char *)iae +
397445916cd2Sjpk 		    ipRouteAttributeSize)) {
397545916cd2Sjpk 			aptr->sal_next = alp[iae->iae_routeidx];
397645916cd2Sjpk 			aptr->sal_attr = iae;
397745916cd2Sjpk 			alp[iae->iae_routeidx] = aptr++;
397845916cd2Sjpk 		}
397945916cd2Sjpk 	}
39807c478bd9Sstevel@tonic-gate 
39817c478bd9Sstevel@tonic-gate 	/* 'for' loop 1: */
398245916cd2Sjpk 	v4a = v4_attrs;
398345916cd2Sjpk 	v6a = v6_attrs;
398445916cd2Sjpk 	for (; item != NULL; item = item->next_item) {
39857c478bd9Sstevel@tonic-gate 		if (Dflag) {
39867c478bd9Sstevel@tonic-gate 			(void) printf("\n--- Entry %d ---\n", ++jtemp);
39877c478bd9Sstevel@tonic-gate 			(void) printf("Group = %d, mib_id = %d, "
39887c478bd9Sstevel@tonic-gate 			    "length = %d, valp = 0x%p\n",
39897c478bd9Sstevel@tonic-gate 			    item->group, item->mib_id,
39907c478bd9Sstevel@tonic-gate 			    item->length, item->valp);
39917c478bd9Sstevel@tonic-gate 		}
39927c478bd9Sstevel@tonic-gate 		if (!((item->group == MIB2_IP &&
39937c478bd9Sstevel@tonic-gate 		    item->mib_id == MIB2_IP_ROUTE) ||
39947c478bd9Sstevel@tonic-gate 		    (item->group == MIB2_IP6 &&
39957c478bd9Sstevel@tonic-gate 		    item->mib_id == MIB2_IP6_ROUTE)))
39967c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
39977c478bd9Sstevel@tonic-gate 
39987c478bd9Sstevel@tonic-gate 		if (item->group == MIB2_IP && !family_selected(AF_INET))
39997c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
40007c478bd9Sstevel@tonic-gate 		else if (item->group == MIB2_IP6 && !family_selected(AF_INET6))
40017c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
40027c478bd9Sstevel@tonic-gate 
40037c478bd9Sstevel@tonic-gate 		if (Dflag) {
40047c478bd9Sstevel@tonic-gate 			if (item->group == MIB2_IP) {
40057c478bd9Sstevel@tonic-gate 				(void) printf("%u records for "
40067c478bd9Sstevel@tonic-gate 				    "ipRouteEntryTable:\n",
40077c478bd9Sstevel@tonic-gate 				    item->length/sizeof (mib2_ipRouteEntry_t));
40087c478bd9Sstevel@tonic-gate 			} else {
40097c478bd9Sstevel@tonic-gate 				(void) printf("%u records for "
40107c478bd9Sstevel@tonic-gate 				    "ipv6RouteEntryTable:\n",
40117c478bd9Sstevel@tonic-gate 				    item->length/
40127c478bd9Sstevel@tonic-gate 				    sizeof (mib2_ipv6RouteEntry_t));
40137c478bd9Sstevel@tonic-gate 			}
40147c478bd9Sstevel@tonic-gate 		}
40157c478bd9Sstevel@tonic-gate 
40167c478bd9Sstevel@tonic-gate 		if (item->group == MIB2_IP) {
40177c478bd9Sstevel@tonic-gate 			for (rp = (mib2_ipRouteEntry_t *)item->valp;
40187c478bd9Sstevel@tonic-gate 			    (char *)rp < (char *)item->valp + item->length;
40197c478bd9Sstevel@tonic-gate 			    /* LINTED: (note 1) */
40207c478bd9Sstevel@tonic-gate 			    rp = (mib2_ipRouteEntry_t *)((char *)rp +
40217c478bd9Sstevel@tonic-gate 			    ipRouteEntrySize)) {
402245916cd2Sjpk 				aptr = v4a == NULL ? NULL : *v4a++;
40237c478bd9Sstevel@tonic-gate 				print_hdr_once_v4 = ire_report_item_v4(rp,
402445916cd2Sjpk 				    print_hdr_once_v4, aptr);
40257c478bd9Sstevel@tonic-gate 			}
40267c478bd9Sstevel@tonic-gate 		} else {
40277c478bd9Sstevel@tonic-gate 			for (rp6 = (mib2_ipv6RouteEntry_t *)item->valp;
40287c478bd9Sstevel@tonic-gate 			    (char *)rp6 < (char *)item->valp + item->length;
40297c478bd9Sstevel@tonic-gate 			    /* LINTED: (note 1) */
40307c478bd9Sstevel@tonic-gate 			    rp6 = (mib2_ipv6RouteEntry_t *)((char *)rp6 +
40317c478bd9Sstevel@tonic-gate 			    ipv6RouteEntrySize)) {
403245916cd2Sjpk 				aptr = v6a == NULL ? NULL : *v6a++;
40337c478bd9Sstevel@tonic-gate 				print_hdr_once_v6 = ire_report_item_v6(rp6,
403445916cd2Sjpk 				    print_hdr_once_v6, aptr);
40357c478bd9Sstevel@tonic-gate 			}
40367c478bd9Sstevel@tonic-gate 		}
40377c478bd9Sstevel@tonic-gate 	} /* 'for' loop 1 ends */
40387c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
403945916cd2Sjpk ire_report_done:
404045916cd2Sjpk 	if (v4_attrs != NULL)
404145916cd2Sjpk 		free(v4_attrs);
404245916cd2Sjpk 	if (v6_attrs != NULL)
404345916cd2Sjpk 		free(v6_attrs);
404445916cd2Sjpk 	if (all_attrs != NULL)
404545916cd2Sjpk 		free(all_attrs);
40467c478bd9Sstevel@tonic-gate }
40477c478bd9Sstevel@tonic-gate 
40487c478bd9Sstevel@tonic-gate /*
40497c478bd9Sstevel@tonic-gate  * Match a user-supplied device name.  We do this by string because
40507c478bd9Sstevel@tonic-gate  * the MIB2 interface gives us interface name strings rather than
40517c478bd9Sstevel@tonic-gate  * ifIndex numbers.  The "none" rule matches only routes with no
40527c478bd9Sstevel@tonic-gate  * interface.  The "any" rule matches routes with any non-blank
40537c478bd9Sstevel@tonic-gate  * interface.  A base name ("hme0") matches all aliases as well
40547c478bd9Sstevel@tonic-gate  * ("hme0:1").
40557c478bd9Sstevel@tonic-gate  */
40567c478bd9Sstevel@tonic-gate static boolean_t
40577c478bd9Sstevel@tonic-gate dev_name_match(const DeviceName *devnam, const char *ifname)
40587c478bd9Sstevel@tonic-gate {
40597c478bd9Sstevel@tonic-gate 	int iflen;
40607c478bd9Sstevel@tonic-gate 
40617c478bd9Sstevel@tonic-gate 	if (ifname == NULL)
40627c478bd9Sstevel@tonic-gate 		return (devnam->o_length == 0);		/* "none" */
40637c478bd9Sstevel@tonic-gate 	if (*ifname == '\0')
40647c478bd9Sstevel@tonic-gate 		return (devnam->o_length != 0);		/* "any" */
40657c478bd9Sstevel@tonic-gate 	iflen = strlen(ifname);
40667c478bd9Sstevel@tonic-gate 	/* The check for ':' here supports interface aliases. */
40677c478bd9Sstevel@tonic-gate 	if (iflen > devnam->o_length ||
40687c478bd9Sstevel@tonic-gate 	    (iflen < devnam->o_length && devnam->o_bytes[iflen] != ':'))
40697c478bd9Sstevel@tonic-gate 		return (B_FALSE);
40707c478bd9Sstevel@tonic-gate 	return (strncmp(ifname, devnam->o_bytes, iflen) == 0);
40717c478bd9Sstevel@tonic-gate }
40727c478bd9Sstevel@tonic-gate 
40737c478bd9Sstevel@tonic-gate /*
40747c478bd9Sstevel@tonic-gate  * Match a user-supplied IP address list.  The "any" rule matches any
40757c478bd9Sstevel@tonic-gate  * non-zero address.  The "none" rule matches only the zero address.
40767c478bd9Sstevel@tonic-gate  * IPv6 addresses supplied by the user are ignored.  If the user
40777c478bd9Sstevel@tonic-gate  * supplies a subnet mask, then match routes that are at least that
40787c478bd9Sstevel@tonic-gate  * specific (use the user's mask).  If the user supplies only an
40797c478bd9Sstevel@tonic-gate  * address, then select any routes that would match (use the route's
40807c478bd9Sstevel@tonic-gate  * mask).
40817c478bd9Sstevel@tonic-gate  */
40827c478bd9Sstevel@tonic-gate static boolean_t
40837c478bd9Sstevel@tonic-gate v4_addr_match(IpAddress addr, IpAddress mask, const filter_t *fp)
40847c478bd9Sstevel@tonic-gate {
40857c478bd9Sstevel@tonic-gate 	char **app;
40867c478bd9Sstevel@tonic-gate 	char *aptr;
40877c478bd9Sstevel@tonic-gate 	in_addr_t faddr, fmask;
40887c478bd9Sstevel@tonic-gate 
40897c478bd9Sstevel@tonic-gate 	if (fp->u.a.f_address == NULL) {
40907c478bd9Sstevel@tonic-gate 		if (IN6_IS_ADDR_UNSPECIFIED(&fp->u.a.f_mask))
40917c478bd9Sstevel@tonic-gate 			return (addr != INADDR_ANY);	/* "any" */
40927c478bd9Sstevel@tonic-gate 		else
40937c478bd9Sstevel@tonic-gate 			return (addr == INADDR_ANY);	/* "none" */
40947c478bd9Sstevel@tonic-gate 	}
40957c478bd9Sstevel@tonic-gate 	if (!IN6_IS_V4MASK(fp->u.a.f_mask))
40967c478bd9Sstevel@tonic-gate 		return (B_FALSE);
40977c478bd9Sstevel@tonic-gate 	IN6_V4MAPPED_TO_IPADDR(&fp->u.a.f_mask, fmask);
40987c478bd9Sstevel@tonic-gate 	if (fmask != IP_HOST_MASK) {
40997c478bd9Sstevel@tonic-gate 		if (fmask > mask)
41007c478bd9Sstevel@tonic-gate 			return (B_FALSE);
41017c478bd9Sstevel@tonic-gate 		mask = fmask;
41027c478bd9Sstevel@tonic-gate 	}
41037c478bd9Sstevel@tonic-gate 	for (app = fp->u.a.f_address->h_addr_list; (aptr = *app) != NULL; app++)
41047c478bd9Sstevel@tonic-gate 		/* LINTED: (note 1) */
41057c478bd9Sstevel@tonic-gate 		if (IN6_IS_ADDR_V4MAPPED((in6_addr_t *)aptr)) {
41067c478bd9Sstevel@tonic-gate 			/* LINTED: (note 1) */
41077c478bd9Sstevel@tonic-gate 			IN6_V4MAPPED_TO_IPADDR((in6_addr_t *)aptr, faddr);
41087c478bd9Sstevel@tonic-gate 			if (((faddr ^ addr) & mask) == 0)
41097c478bd9Sstevel@tonic-gate 				return (B_TRUE);
41107c478bd9Sstevel@tonic-gate 		}
41117c478bd9Sstevel@tonic-gate 	return (B_FALSE);
41127c478bd9Sstevel@tonic-gate }
41137c478bd9Sstevel@tonic-gate 
41147c478bd9Sstevel@tonic-gate /*
41157c478bd9Sstevel@tonic-gate  * Run through the filter list for an IPv4 MIB2 route entry.  If all
41167c478bd9Sstevel@tonic-gate  * filters of a given type fail to match, then the route is filtered
41177c478bd9Sstevel@tonic-gate  * out (not displayed).  If no filter is given or at least one filter
41187c478bd9Sstevel@tonic-gate  * of each type matches, then display the route.
41197c478bd9Sstevel@tonic-gate  */
41207c478bd9Sstevel@tonic-gate static boolean_t
412145916cd2Sjpk ire_filter_match_v4(const mib2_ipRouteEntry_t *rp, uint_t flag_b)
41227c478bd9Sstevel@tonic-gate {
41237c478bd9Sstevel@tonic-gate 	filter_t *fp;
41247c478bd9Sstevel@tonic-gate 	int idx;
41257c478bd9Sstevel@tonic-gate 
41267c478bd9Sstevel@tonic-gate 	/* 'for' loop 1: */
41277c478bd9Sstevel@tonic-gate 	for (idx = 0; idx < NFILTERKEYS; idx++)
41287c478bd9Sstevel@tonic-gate 		if ((fp = filters[idx]) != NULL) {
41297c478bd9Sstevel@tonic-gate 			/* 'for' loop 2: */
41307c478bd9Sstevel@tonic-gate 			for (; fp != NULL; fp = fp->f_next) {
41317c478bd9Sstevel@tonic-gate 				switch (idx) {
41327c478bd9Sstevel@tonic-gate 				case FK_AF:
41337c478bd9Sstevel@tonic-gate 					if (fp->u.f_family != AF_INET)
41347c478bd9Sstevel@tonic-gate 						continue; /* 'for' loop 2 */
41357c478bd9Sstevel@tonic-gate 					break;
41367c478bd9Sstevel@tonic-gate 				case FK_OUTIF:
41377c478bd9Sstevel@tonic-gate 					if (!dev_name_match(&rp->ipRouteIfIndex,
41387c478bd9Sstevel@tonic-gate 					    fp->u.f_ifname))
41397c478bd9Sstevel@tonic-gate 						continue; /* 'for' loop 2 */
41407c478bd9Sstevel@tonic-gate 					break;
41417c478bd9Sstevel@tonic-gate 				case FK_DST:
41427c478bd9Sstevel@tonic-gate 					if (!v4_addr_match(rp->ipRouteDest,
41437c478bd9Sstevel@tonic-gate 					    rp->ipRouteMask, fp))
41447c478bd9Sstevel@tonic-gate 						continue; /* 'for' loop 2 */
41457c478bd9Sstevel@tonic-gate 					break;
41467c478bd9Sstevel@tonic-gate 				case FK_FLAGS:
41477c478bd9Sstevel@tonic-gate 					if ((flag_b & fp->u.f.f_flagset) !=
41487c478bd9Sstevel@tonic-gate 					    fp->u.f.f_flagset ||
41497c478bd9Sstevel@tonic-gate 					    (flag_b & fp->u.f.f_flagclear))
41507c478bd9Sstevel@tonic-gate 						continue; /* 'for' loop 2 */
41517c478bd9Sstevel@tonic-gate 					break;
41527c478bd9Sstevel@tonic-gate 				}
41537c478bd9Sstevel@tonic-gate 				break;
41547c478bd9Sstevel@tonic-gate 			} /* 'for' loop 2 ends */
41557c478bd9Sstevel@tonic-gate 			if (fp == NULL)
41567c478bd9Sstevel@tonic-gate 				return (B_FALSE);
41577c478bd9Sstevel@tonic-gate 		}
41587c478bd9Sstevel@tonic-gate 	/* 'for' loop 1 ends */
41597c478bd9Sstevel@tonic-gate 	return (B_TRUE);
41607c478bd9Sstevel@tonic-gate }
41617c478bd9Sstevel@tonic-gate 
41627c478bd9Sstevel@tonic-gate /*
41637c478bd9Sstevel@tonic-gate  * Given an IPv4 MIB2 route entry, form the list of flags for the
41647c478bd9Sstevel@tonic-gate  * route.
41657c478bd9Sstevel@tonic-gate  */
41667c478bd9Sstevel@tonic-gate static uint_t
416745916cd2Sjpk form_v4_route_flags(const mib2_ipRouteEntry_t *rp, char *flags)
41687c478bd9Sstevel@tonic-gate {
41697c478bd9Sstevel@tonic-gate 	uint_t flag_b;
41707c478bd9Sstevel@tonic-gate 
41717c478bd9Sstevel@tonic-gate 	flag_b = FLF_U;
41727c478bd9Sstevel@tonic-gate 	(void) strcpy(flags, "U");
41737c478bd9Sstevel@tonic-gate 	if (rp->ipRouteInfo.re_ire_type == IRE_DEFAULT ||
41747c478bd9Sstevel@tonic-gate 	    rp->ipRouteInfo.re_ire_type == IRE_PREFIX ||
41757c478bd9Sstevel@tonic-gate 	    rp->ipRouteInfo.re_ire_type == IRE_HOST ||
41767c478bd9Sstevel@tonic-gate 	    rp->ipRouteInfo.re_ire_type == IRE_HOST_REDIRECT) {
41777c478bd9Sstevel@tonic-gate 		(void) strcat(flags, "G");
41787c478bd9Sstevel@tonic-gate 		flag_b |= FLF_G;
41797c478bd9Sstevel@tonic-gate 	}
41807c478bd9Sstevel@tonic-gate 	if (rp->ipRouteMask == IP_HOST_MASK) {
41817c478bd9Sstevel@tonic-gate 		(void) strcat(flags, "H");
41827c478bd9Sstevel@tonic-gate 		flag_b |= FLF_H;
41837c478bd9Sstevel@tonic-gate 	}
41847c478bd9Sstevel@tonic-gate 	if (rp->ipRouteInfo.re_ire_type == IRE_HOST_REDIRECT) {
41857c478bd9Sstevel@tonic-gate 		(void) strcat(flags, "D");
41867c478bd9Sstevel@tonic-gate 		flag_b |= FLF_D;
41877c478bd9Sstevel@tonic-gate 	}
41887c478bd9Sstevel@tonic-gate 	if (rp->ipRouteInfo.re_ire_type == IRE_CACHE) {
41897c478bd9Sstevel@tonic-gate 		/* Address resolution */
41907c478bd9Sstevel@tonic-gate 		(void) strcat(flags, "A");
41917c478bd9Sstevel@tonic-gate 		flag_b |= FLF_A;
41927c478bd9Sstevel@tonic-gate 	}
41937c478bd9Sstevel@tonic-gate 	if (rp->ipRouteInfo.re_ire_type == IRE_BROADCAST) {	/* Broadcast */
41947c478bd9Sstevel@tonic-gate 		(void) strcat(flags, "B");
41957c478bd9Sstevel@tonic-gate 		flag_b |= FLF_B;
41967c478bd9Sstevel@tonic-gate 	}
41977c478bd9Sstevel@tonic-gate 	if (rp->ipRouteInfo.re_ire_type == IRE_LOCAL) {		/* Local */
41987c478bd9Sstevel@tonic-gate 		(void) strcat(flags, "L");
41997c478bd9Sstevel@tonic-gate 		flag_b |= FLF_L;
42007c478bd9Sstevel@tonic-gate 	}
42017c478bd9Sstevel@tonic-gate 	if (rp->ipRouteInfo.re_flags & RTF_MULTIRT) {
42027c478bd9Sstevel@tonic-gate 		(void) strcat(flags, "M");			/* Multiroute */
42037c478bd9Sstevel@tonic-gate 		flag_b |= FLF_M;
42047c478bd9Sstevel@tonic-gate 	}
42057c478bd9Sstevel@tonic-gate 	if (rp->ipRouteInfo.re_flags & RTF_SETSRC) {
42067c478bd9Sstevel@tonic-gate 		(void) strcat(flags, "S");			/* Setsrc */
42077c478bd9Sstevel@tonic-gate 		flag_b |= FLF_S;
42087c478bd9Sstevel@tonic-gate 	}
42097c478bd9Sstevel@tonic-gate 	return (flag_b);
42107c478bd9Sstevel@tonic-gate }
42117c478bd9Sstevel@tonic-gate 
421245916cd2Sjpk static const char ire_hdr_v4[] =
421345916cd2Sjpk "\n%s Table: IPv4\n";
421445916cd2Sjpk static const char ire_hdr_v4_compat[] =
421545916cd2Sjpk "\n%s Table:\n";
421645916cd2Sjpk static const char ire_hdr_v4_verbose[] =
421745916cd2Sjpk "  Destination             Mask           Gateway          Device Mxfrg "
421845916cd2Sjpk "Rtt   Ref Flg  Out  In/Fwd %s\n"
421945916cd2Sjpk "-------------------- --------------- -------------------- ------ ----- "
422045916cd2Sjpk "----- --- --- ----- ------ %s\n";
422145916cd2Sjpk 
422245916cd2Sjpk static const char ire_hdr_v4_normal[] =
4223aecc8c24Sja "  Destination           Gateway           Flags  Ref     Use     Interface"
4224aecc8c24Sja " %s\n-------------------- -------------------- ----- ----- ---------- "
4225aecc8c24Sja "--------- %s\n";
422645916cd2Sjpk 
42277c478bd9Sstevel@tonic-gate static boolean_t
422845916cd2Sjpk ire_report_item_v4(const mib2_ipRouteEntry_t *rp, boolean_t first,
422945916cd2Sjpk     const sec_attr_list_t *attrs)
42307c478bd9Sstevel@tonic-gate {
42317c478bd9Sstevel@tonic-gate 	char			dstbuf[MAXHOSTNAMELEN + 1];
42327c478bd9Sstevel@tonic-gate 	char			maskbuf[MAXHOSTNAMELEN + 1];
42337c478bd9Sstevel@tonic-gate 	char			gwbuf[MAXHOSTNAMELEN + 1];
42347c478bd9Sstevel@tonic-gate 	char			ifname[LIFNAMSIZ + 1];
42357c478bd9Sstevel@tonic-gate 	char			flags[10];	/* RTF_ flags */
42367c478bd9Sstevel@tonic-gate 	uint_t			flag_b;
42377c478bd9Sstevel@tonic-gate 
42385c0b7edeSseb 	if (!(Aflag || (rp->ipRouteInfo.re_ire_type != IRE_CACHE &&
42397c478bd9Sstevel@tonic-gate 	    rp->ipRouteInfo.re_ire_type != IRE_BROADCAST &&
42407c478bd9Sstevel@tonic-gate 	    rp->ipRouteInfo.re_ire_type != IRE_LOCAL))) {
42417c478bd9Sstevel@tonic-gate 		return (first);
42427c478bd9Sstevel@tonic-gate 	}
42437c478bd9Sstevel@tonic-gate 
42447c478bd9Sstevel@tonic-gate 	flag_b = form_v4_route_flags(rp, flags);
42457c478bd9Sstevel@tonic-gate 
42467c478bd9Sstevel@tonic-gate 	if (!ire_filter_match_v4(rp, flag_b))
42477c478bd9Sstevel@tonic-gate 		return (first);
42487c478bd9Sstevel@tonic-gate 
42497c478bd9Sstevel@tonic-gate 	if (first) {
425045916cd2Sjpk 		(void) printf(v4compat ? ire_hdr_v4_compat : ire_hdr_v4,
425145916cd2Sjpk 		    Vflag ? "IRE" : "Routing");
425245916cd2Sjpk 		(void) printf(Vflag ? ire_hdr_v4_verbose : ire_hdr_v4_normal,
425345916cd2Sjpk 		    RSECflag ? "  Gateway security attributes  " : "",
425445916cd2Sjpk 		    RSECflag ? "-------------------------------" : "");
42557c478bd9Sstevel@tonic-gate 		first = B_FALSE;
42567c478bd9Sstevel@tonic-gate 	}
42577c478bd9Sstevel@tonic-gate 
42587c478bd9Sstevel@tonic-gate 	if (flag_b & FLF_H) {
42597c478bd9Sstevel@tonic-gate 		(void) pr_addr(rp->ipRouteDest, dstbuf, sizeof (dstbuf));
42607c478bd9Sstevel@tonic-gate 	} else {
42617c478bd9Sstevel@tonic-gate 		(void) pr_net(rp->ipRouteDest, rp->ipRouteMask,
42627c478bd9Sstevel@tonic-gate 		    dstbuf, sizeof (dstbuf));
42637c478bd9Sstevel@tonic-gate 	}
42647c478bd9Sstevel@tonic-gate 	if (Vflag) {
42657c478bd9Sstevel@tonic-gate 		(void) printf("%-20s %-15s %-20s %-6s %5u%c %4u %3u "
4266aecc8c24Sja 		    "%-4s%6u %6u %s\n",
42677c478bd9Sstevel@tonic-gate 		    dstbuf,
42687c478bd9Sstevel@tonic-gate 		    pr_mask(rp->ipRouteMask, maskbuf, sizeof (maskbuf)),
42697c478bd9Sstevel@tonic-gate 		    pr_addrnz(rp->ipRouteNextHop, gwbuf, sizeof (gwbuf)),
42707c478bd9Sstevel@tonic-gate 		    octetstr(&rp->ipRouteIfIndex, 'a', ifname, sizeof (ifname)),
42717c478bd9Sstevel@tonic-gate 		    rp->ipRouteInfo.re_max_frag,
42727c478bd9Sstevel@tonic-gate 		    rp->ipRouteInfo.re_frag_flag ? '*' : ' ',
42737c478bd9Sstevel@tonic-gate 		    rp->ipRouteInfo.re_rtt,
42747c478bd9Sstevel@tonic-gate 		    rp->ipRouteInfo.re_ref,
42757c478bd9Sstevel@tonic-gate 		    flags,
42767c478bd9Sstevel@tonic-gate 		    rp->ipRouteInfo.re_obpkt,
427745916cd2Sjpk 		    rp->ipRouteInfo.re_ibpkt,
427845916cd2Sjpk 		    pr_secattr(attrs));
42797c478bd9Sstevel@tonic-gate 	} else {
4280aecc8c24Sja 		(void) printf("%-20s %-20s %-5s  %4u %10u %-9s %s\n",
42817c478bd9Sstevel@tonic-gate 		    dstbuf,
42827c478bd9Sstevel@tonic-gate 		    pr_addrnz(rp->ipRouteNextHop, gwbuf, sizeof (gwbuf)),
42837c478bd9Sstevel@tonic-gate 		    flags,
42847c478bd9Sstevel@tonic-gate 		    rp->ipRouteInfo.re_ref,
42857c478bd9Sstevel@tonic-gate 		    rp->ipRouteInfo.re_obpkt + rp->ipRouteInfo.re_ibpkt,
42867c478bd9Sstevel@tonic-gate 		    octetstr(&rp->ipRouteIfIndex, 'a',
428745916cd2Sjpk 		    ifname, sizeof (ifname)),
428845916cd2Sjpk 		    pr_secattr(attrs));
42897c478bd9Sstevel@tonic-gate 	}
42907c478bd9Sstevel@tonic-gate 	return (first);
42917c478bd9Sstevel@tonic-gate }
42927c478bd9Sstevel@tonic-gate 
42937c478bd9Sstevel@tonic-gate /*
42947c478bd9Sstevel@tonic-gate  * Match a user-supplied IP address list against an IPv6 route entry.
42957c478bd9Sstevel@tonic-gate  * If the user specified "any," then any non-zero address matches.  If
42967c478bd9Sstevel@tonic-gate  * the user specified "none," then only the zero address matches.  If
42977c478bd9Sstevel@tonic-gate  * the user specified a subnet mask length, then use that in matching
42987c478bd9Sstevel@tonic-gate  * routes (select routes that are at least as specific).  If the user
42997c478bd9Sstevel@tonic-gate  * specified only an address, then use the route's mask (select routes
43007c478bd9Sstevel@tonic-gate  * that would match that address).  IPv4 addresses are ignored.
43017c478bd9Sstevel@tonic-gate  */
43027c478bd9Sstevel@tonic-gate static boolean_t
43037c478bd9Sstevel@tonic-gate v6_addr_match(const Ip6Address *addr, int masklen, const filter_t *fp)
43047c478bd9Sstevel@tonic-gate {
43057c478bd9Sstevel@tonic-gate 	const uint8_t *ucp;
43067c478bd9Sstevel@tonic-gate 	int fmasklen;
43077c478bd9Sstevel@tonic-gate 	int i;
43087c478bd9Sstevel@tonic-gate 	char **app;
4309265f37a1S 	const uint8_t *aptr;
43107c478bd9Sstevel@tonic-gate 
43117c478bd9Sstevel@tonic-gate 	if (fp->u.a.f_address == NULL) {
43127c478bd9Sstevel@tonic-gate 		if (IN6_IS_ADDR_UNSPECIFIED(&fp->u.a.f_mask))	/* any */
43137c478bd9Sstevel@tonic-gate 			return (!IN6_IS_ADDR_UNSPECIFIED(addr));
43147c478bd9Sstevel@tonic-gate 		return (IN6_IS_ADDR_UNSPECIFIED(addr));		/* "none" */
43157c478bd9Sstevel@tonic-gate 	}
43167c478bd9Sstevel@tonic-gate 	fmasklen = 0;
43177c478bd9Sstevel@tonic-gate 	/* 'for' loop 1a: */
43187c478bd9Sstevel@tonic-gate 	for (ucp = fp->u.a.f_mask.s6_addr;
43197c478bd9Sstevel@tonic-gate 	    ucp < fp->u.a.f_mask.s6_addr + sizeof (fp->u.a.f_mask.s6_addr);
43207c478bd9Sstevel@tonic-gate 	    ucp++) {
43217c478bd9Sstevel@tonic-gate 		if (*ucp != 0xff) {
43227c478bd9Sstevel@tonic-gate 			if (*ucp != 0)
43237c478bd9Sstevel@tonic-gate 				fmasklen += 9 - ffs(*ucp);
43247c478bd9Sstevel@tonic-gate 			break; /* 'for' loop 1a */
43257c478bd9Sstevel@tonic-gate 		}
43267c478bd9Sstevel@tonic-gate 		fmasklen += 8;
43277c478bd9Sstevel@tonic-gate 	} /* 'for' loop 1a ends */
43287c478bd9Sstevel@tonic-gate 	if (fmasklen != IPV6_ABITS) {
43297c478bd9Sstevel@tonic-gate 		if (fmasklen > masklen)
43307c478bd9Sstevel@tonic-gate 			return (B_FALSE);
43317c478bd9Sstevel@tonic-gate 		masklen = fmasklen;
43327c478bd9Sstevel@tonic-gate 	}
43337c478bd9Sstevel@tonic-gate 	/* 'for' loop 1b: */
4334265f37a1S 	for (app = fp->u.a.f_address->h_addr_list;
4335265f37a1S 	    (aptr = (uint8_t *)*app) != NULL; app++) {
43367c478bd9Sstevel@tonic-gate 		/* LINTED: (note 1) */
43377c478bd9Sstevel@tonic-gate 		if (IN6_IS_ADDR_V4MAPPED((in6_addr_t *)aptr))
43387c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1b */
43397c478bd9Sstevel@tonic-gate 		ucp = addr->s6_addr;
43407c478bd9Sstevel@tonic-gate 		for (i = masklen; i >= 8; i -= 8)
43417c478bd9Sstevel@tonic-gate 			if (*ucp++ != *aptr++)
43427c478bd9Sstevel@tonic-gate 				break; /* 'for' loop 1b */
43437c478bd9Sstevel@tonic-gate 		if (i == 0 ||
43447c478bd9Sstevel@tonic-gate 		    (i < 8 && ((*ucp ^ *aptr) & ~(0xff >> i)) == 0))
43457c478bd9Sstevel@tonic-gate 			return (B_TRUE);
43467c478bd9Sstevel@tonic-gate 	} /* 'for' loop 1b ends */
43477c478bd9Sstevel@tonic-gate 	return (B_FALSE);
43487c478bd9Sstevel@tonic-gate }
43497c478bd9Sstevel@tonic-gate 
43507c478bd9Sstevel@tonic-gate /*
43517c478bd9Sstevel@tonic-gate  * Run through the filter list for an IPv6 MIB2 IRE.  For a given
43527c478bd9Sstevel@tonic-gate  * type, if there's at least one filter and all filters of that type
43537c478bd9Sstevel@tonic-gate  * fail to match, then the route doesn't match and isn't displayed.
43547c478bd9Sstevel@tonic-gate  * If at least one matches, or none are specified, for each of the
43557c478bd9Sstevel@tonic-gate  * types, then the route is selected and displayed.
43567c478bd9Sstevel@tonic-gate  */
43577c478bd9Sstevel@tonic-gate static boolean_t
435845916cd2Sjpk ire_filter_match_v6(const mib2_ipv6RouteEntry_t *rp6, uint_t flag_b)
43597c478bd9Sstevel@tonic-gate {
43607c478bd9Sstevel@tonic-gate 	filter_t *fp;
43617c478bd9Sstevel@tonic-gate 	int idx;
43627c478bd9Sstevel@tonic-gate 
43637c478bd9Sstevel@tonic-gate 	/* 'for' loop 1: */
43647c478bd9Sstevel@tonic-gate 	for (idx = 0; idx < NFILTERKEYS; idx++)
43657c478bd9Sstevel@tonic-gate 		if ((fp = filters[idx]) != NULL) {
43667c478bd9Sstevel@tonic-gate 			/* 'for' loop 2: */
43677c478bd9Sstevel@tonic-gate 			for (; fp != NULL; fp = fp->f_next) {
43687c478bd9Sstevel@tonic-gate 				switch (idx) {
43697c478bd9Sstevel@tonic-gate 				case FK_AF:
43707c478bd9Sstevel@tonic-gate 					if (fp->u.f_family != AF_INET6)
43717c478bd9Sstevel@tonic-gate 						/* 'for' loop 2 */
43727c478bd9Sstevel@tonic-gate 						continue;
43737c478bd9Sstevel@tonic-gate 					break;
43747c478bd9Sstevel@tonic-gate 				case FK_OUTIF:
43757c478bd9Sstevel@tonic-gate 					if (!dev_name_match(&rp6->
43767c478bd9Sstevel@tonic-gate 					    ipv6RouteIfIndex, fp->u.f_ifname))
43777c478bd9Sstevel@tonic-gate 						/* 'for' loop 2 */
43787c478bd9Sstevel@tonic-gate 						continue;
43797c478bd9Sstevel@tonic-gate 					break;
43807c478bd9Sstevel@tonic-gate 				case FK_DST:
43817c478bd9Sstevel@tonic-gate 					if (!v6_addr_match(&rp6->ipv6RouteDest,
43827c478bd9Sstevel@tonic-gate 					    rp6->ipv6RoutePfxLength, fp))
43837c478bd9Sstevel@tonic-gate 						/* 'for' loop 2 */
43847c478bd9Sstevel@tonic-gate 						continue;
43857c478bd9Sstevel@tonic-gate 					break;
43867c478bd9Sstevel@tonic-gate 				case FK_FLAGS:
43877c478bd9Sstevel@tonic-gate 					if ((flag_b & fp->u.f.f_flagset) !=
43887c478bd9Sstevel@tonic-gate 					    fp->u.f.f_flagset ||
43897c478bd9Sstevel@tonic-gate 					    (flag_b & fp->u.f.f_flagclear))
43907c478bd9Sstevel@tonic-gate 						/* 'for' loop 2 */
43917c478bd9Sstevel@tonic-gate 						continue;
43927c478bd9Sstevel@tonic-gate 					break;
43937c478bd9Sstevel@tonic-gate 				}
43947c478bd9Sstevel@tonic-gate 				break;
43957c478bd9Sstevel@tonic-gate 			} /* 'for' loop 2 ends */
43967c478bd9Sstevel@tonic-gate 			if (fp == NULL)
43977c478bd9Sstevel@tonic-gate 				return (B_FALSE);
43987c478bd9Sstevel@tonic-gate 		}
43997c478bd9Sstevel@tonic-gate 	/* 'for' loop 1 ends */
44007c478bd9Sstevel@tonic-gate 	return (B_TRUE);
44017c478bd9Sstevel@tonic-gate }
44027c478bd9Sstevel@tonic-gate 
440345916cd2Sjpk static const char ire_hdr_v6[] =
440445916cd2Sjpk "\n%s Table: IPv6\n";
440545916cd2Sjpk static const char ire_hdr_v6_verbose[] =
440645916cd2Sjpk "  Destination/Mask            Gateway                    If    PMTU   Rtt  "
440745916cd2Sjpk "Ref Flags  Out   In/Fwd %s\n"
440845916cd2Sjpk "--------------------------- --------------------------- ----- ------ ----- "
440945916cd2Sjpk "--- ----- ------ ------ %s\n";
441045916cd2Sjpk static const char ire_hdr_v6_normal[] =
441145916cd2Sjpk "  Destination/Mask            Gateway                   Flags Ref   Use  "
4412aecc8c24Sja "  If   %s\n"
4413aecc8c24Sja "--------------------------- --------------------------- ----- --- ------- "
441445916cd2Sjpk "----- %s\n";
441545916cd2Sjpk 
44167c478bd9Sstevel@tonic-gate static boolean_t
441745916cd2Sjpk ire_report_item_v6(const mib2_ipv6RouteEntry_t *rp6, boolean_t first,
441845916cd2Sjpk     const sec_attr_list_t *attrs)
44197c478bd9Sstevel@tonic-gate {
44207c478bd9Sstevel@tonic-gate 	char			dstbuf[MAXHOSTNAMELEN + 1];
44217c478bd9Sstevel@tonic-gate 	char			gwbuf[MAXHOSTNAMELEN + 1];
44227c478bd9Sstevel@tonic-gate 	char			ifname[LIFNAMSIZ + 1];
44237c478bd9Sstevel@tonic-gate 	char			flags[10];	/* RTF_ flags */
44247c478bd9Sstevel@tonic-gate 	uint_t			flag_b;
44257c478bd9Sstevel@tonic-gate 
44267c478bd9Sstevel@tonic-gate 	if (!(Aflag || (rp6->ipv6RouteInfo.re_ire_type != IRE_CACHE &&
44277c478bd9Sstevel@tonic-gate 	    rp6->ipv6RouteInfo.re_ire_type != IRE_LOCAL))) {
44287c478bd9Sstevel@tonic-gate 		return (first);
44297c478bd9Sstevel@tonic-gate 	}
44307c478bd9Sstevel@tonic-gate 
44317c478bd9Sstevel@tonic-gate 	flag_b = FLF_U;
44327c478bd9Sstevel@tonic-gate 	(void) strcpy(flags, "U");
44337c478bd9Sstevel@tonic-gate 	if (rp6->ipv6RouteInfo.re_ire_type == IRE_DEFAULT ||
44347c478bd9Sstevel@tonic-gate 	    rp6->ipv6RouteInfo.re_ire_type == IRE_PREFIX ||
44357c478bd9Sstevel@tonic-gate 	    rp6->ipv6RouteInfo.re_ire_type == IRE_HOST ||
44367c478bd9Sstevel@tonic-gate 	    rp6->ipv6RouteInfo.re_ire_type == IRE_HOST_REDIRECT) {
44377c478bd9Sstevel@tonic-gate 		(void) strcat(flags, "G");
44387c478bd9Sstevel@tonic-gate 		flag_b |= FLF_G;
44397c478bd9Sstevel@tonic-gate 	}
44407c478bd9Sstevel@tonic-gate 
44417c478bd9Sstevel@tonic-gate 	if (rp6->ipv6RoutePfxLength == IPV6_ABITS) {
44427c478bd9Sstevel@tonic-gate 		(void) strcat(flags, "H");
44437c478bd9Sstevel@tonic-gate 		flag_b |= FLF_H;
44447c478bd9Sstevel@tonic-gate 	}
44457c478bd9Sstevel@tonic-gate 
44467c478bd9Sstevel@tonic-gate 	if (rp6->ipv6RouteInfo.re_ire_type == IRE_HOST_REDIRECT) {
44477c478bd9Sstevel@tonic-gate 		(void) strcat(flags, "D");
44487c478bd9Sstevel@tonic-gate 		flag_b |= FLF_D;
44497c478bd9Sstevel@tonic-gate 	}
44507c478bd9Sstevel@tonic-gate 	if (rp6->ipv6RouteInfo.re_ire_type == IRE_CACHE) {
44517c478bd9Sstevel@tonic-gate 		/* Address resolution */
44527c478bd9Sstevel@tonic-gate 		(void) strcat(flags, "A");
44537c478bd9Sstevel@tonic-gate 		flag_b |= FLF_A;
44547c478bd9Sstevel@tonic-gate 	}
44557c478bd9Sstevel@tonic-gate 	if (rp6->ipv6RouteInfo.re_ire_type == IRE_LOCAL) {	/* Local */
44567c478bd9Sstevel@tonic-gate 		(void) strcat(flags, "L");
44577c478bd9Sstevel@tonic-gate 		flag_b |= FLF_L;
44587c478bd9Sstevel@tonic-gate 	}
44597c478bd9Sstevel@tonic-gate 	if (rp6->ipv6RouteInfo.re_flags & RTF_MULTIRT) {
44607c478bd9Sstevel@tonic-gate 		(void) strcat(flags, "M");			/* Multiroute */
44617c478bd9Sstevel@tonic-gate 		flag_b |= FLF_M;
44627c478bd9Sstevel@tonic-gate 	}
44637c478bd9Sstevel@tonic-gate 	if (rp6->ipv6RouteInfo.re_flags & RTF_SETSRC) {
44647c478bd9Sstevel@tonic-gate 		(void) strcat(flags, "S");			/* Setsrc */
44657c478bd9Sstevel@tonic-gate 		flag_b |= FLF_S;
44667c478bd9Sstevel@tonic-gate 	}
44677c478bd9Sstevel@tonic-gate 
44687c478bd9Sstevel@tonic-gate 	if (!ire_filter_match_v6(rp6, flag_b))
44697c478bd9Sstevel@tonic-gate 		return (first);
44707c478bd9Sstevel@tonic-gate 
44717c478bd9Sstevel@tonic-gate 	if (first) {
447245916cd2Sjpk 		(void) printf(ire_hdr_v6, Vflag ? "IRE" : "Routing");
447345916cd2Sjpk 		(void) printf(Vflag ? ire_hdr_v6_verbose : ire_hdr_v6_normal,
447445916cd2Sjpk 		    RSECflag ? "  Gateway security attributes  " : "",
447545916cd2Sjpk 		    RSECflag ? "-------------------------------" : "");
44767c478bd9Sstevel@tonic-gate 		first = B_FALSE;
44777c478bd9Sstevel@tonic-gate 	}
44787c478bd9Sstevel@tonic-gate 
44797c478bd9Sstevel@tonic-gate 	if (Vflag) {
44807c478bd9Sstevel@tonic-gate 		(void) printf("%-27s %-27s %-5s %5u%c %5u %3u "
448145916cd2Sjpk 		    "%-5s %6u %6u %s\n",
44827c478bd9Sstevel@tonic-gate 		    pr_prefix6(&rp6->ipv6RouteDest,
4483e11c3f44Smeem 		    rp6->ipv6RoutePfxLength, dstbuf, sizeof (dstbuf)),
44847c478bd9Sstevel@tonic-gate 		    IN6_IS_ADDR_UNSPECIFIED(&rp6->ipv6RouteNextHop) ?
44857c478bd9Sstevel@tonic-gate 		    "    --" :
44867c478bd9Sstevel@tonic-gate 		    pr_addr6(&rp6->ipv6RouteNextHop, gwbuf, sizeof (gwbuf)),
44877c478bd9Sstevel@tonic-gate 		    octetstr(&rp6->ipv6RouteIfIndex, 'a',
44887c478bd9Sstevel@tonic-gate 		    ifname, sizeof (ifname)),
44897c478bd9Sstevel@tonic-gate 		    rp6->ipv6RouteInfo.re_max_frag,
44907c478bd9Sstevel@tonic-gate 		    rp6->ipv6RouteInfo.re_frag_flag ? '*' : ' ',
44917c478bd9Sstevel@tonic-gate 		    rp6->ipv6RouteInfo.re_rtt,
44927c478bd9Sstevel@tonic-gate 		    rp6->ipv6RouteInfo.re_ref,
44937c478bd9Sstevel@tonic-gate 		    flags,
44947c478bd9Sstevel@tonic-gate 		    rp6->ipv6RouteInfo.re_obpkt,
449545916cd2Sjpk 		    rp6->ipv6RouteInfo.re_ibpkt,
449645916cd2Sjpk 		    pr_secattr(attrs));
44977c478bd9Sstevel@tonic-gate 	} else {
4498aecc8c24Sja 		(void) printf("%-27s %-27s %-5s %3u %7u %-5s %s\n",
44997c478bd9Sstevel@tonic-gate 		    pr_prefix6(&rp6->ipv6RouteDest,
4500e11c3f44Smeem 		    rp6->ipv6RoutePfxLength, dstbuf, sizeof (dstbuf)),
45017c478bd9Sstevel@tonic-gate 		    IN6_IS_ADDR_UNSPECIFIED(&rp6->ipv6RouteNextHop) ?
45027c478bd9Sstevel@tonic-gate 		    "    --" :
45037c478bd9Sstevel@tonic-gate 		    pr_addr6(&rp6->ipv6RouteNextHop, gwbuf, sizeof (gwbuf)),
45047c478bd9Sstevel@tonic-gate 		    flags,
45057c478bd9Sstevel@tonic-gate 		    rp6->ipv6RouteInfo.re_ref,
45067c478bd9Sstevel@tonic-gate 		    rp6->ipv6RouteInfo.re_obpkt + rp6->ipv6RouteInfo.re_ibpkt,
45077c478bd9Sstevel@tonic-gate 		    octetstr(&rp6->ipv6RouteIfIndex, 'a',
450845916cd2Sjpk 		    ifname, sizeof (ifname)),
450945916cd2Sjpk 		    pr_secattr(attrs));
45107c478bd9Sstevel@tonic-gate 	}
45117c478bd9Sstevel@tonic-gate 	return (first);
45127c478bd9Sstevel@tonic-gate }
45137c478bd9Sstevel@tonic-gate 
451445916cd2Sjpk /*
451545916cd2Sjpk  * Common attribute-gathering routine for all transports.
451645916cd2Sjpk  */
451745916cd2Sjpk static mib2_transportMLPEntry_t **
451845916cd2Sjpk gather_attrs(const mib_item_t *item, int group, int mib_id, int esize)
451945916cd2Sjpk {
452045916cd2Sjpk 	int transport_count = 0;
452145916cd2Sjpk 	const mib_item_t *iptr;
452245916cd2Sjpk 	mib2_transportMLPEntry_t **attrs, *tme;
452345916cd2Sjpk 
452445916cd2Sjpk 	for (iptr = item; iptr != NULL; iptr = iptr->next_item) {
452545916cd2Sjpk 		if (iptr->group == group && iptr->mib_id == mib_id)
452645916cd2Sjpk 			transport_count += iptr->length / esize;
452745916cd2Sjpk 	}
452845916cd2Sjpk 	if (transport_count <= 0)
452945916cd2Sjpk 		return (NULL);
453045916cd2Sjpk 	attrs = calloc(transport_count, sizeof (*attrs));
453145916cd2Sjpk 	if (attrs == NULL) {
453245916cd2Sjpk 		perror("gather_attrs calloc failed");
453345916cd2Sjpk 		return (NULL);
453445916cd2Sjpk 	}
453545916cd2Sjpk 	for (iptr = item; iptr != NULL; iptr = iptr->next_item) {
453645916cd2Sjpk 		if (iptr->group == group && iptr->mib_id == EXPER_XPORT_MLP) {
453745916cd2Sjpk 			for (tme = iptr->valp;
453845916cd2Sjpk 			    (char *)tme < (char *)iptr->valp + iptr->length;
453945916cd2Sjpk 			    /* LINTED: (note 1) */
454045916cd2Sjpk 			    tme = (mib2_transportMLPEntry_t *)((char *)tme +
454145916cd2Sjpk 			    transportMLPSize)) {
454245916cd2Sjpk 				attrs[tme->tme_connidx] = tme;
454345916cd2Sjpk 			}
454445916cd2Sjpk 		}
454545916cd2Sjpk 	}
454645916cd2Sjpk 	return (attrs);
454745916cd2Sjpk }
454845916cd2Sjpk 
454945916cd2Sjpk static void
455045916cd2Sjpk print_transport_label(const mib2_transportMLPEntry_t *attr)
455145916cd2Sjpk {
45525f9878b0Sken Powell - Sun Microsystem 	if (!RSECflag || attr == NULL ||
45535f9878b0Sken Powell - Sun Microsystem 	    !(attr->tme_flags & MIB2_TMEF_IS_LABELED))
455445916cd2Sjpk 		return;
455545916cd2Sjpk 
455645916cd2Sjpk 	if (bisinvalid(&attr->tme_label))
455745916cd2Sjpk 		(void) printf("   INVALID\n");
45585f9878b0Sken Powell - Sun Microsystem 	else if (!blequal(&attr->tme_label, zone_security_label))
455945916cd2Sjpk 		(void) printf("   %s\n", sl_to_str(&attr->tme_label));
456045916cd2Sjpk }
456145916cd2Sjpk 
45627c478bd9Sstevel@tonic-gate /* ------------------------------ TCP_REPORT------------------------------- */
45637c478bd9Sstevel@tonic-gate 
45647c478bd9Sstevel@tonic-gate static const char tcp_hdr_v4[] =
45657c478bd9Sstevel@tonic-gate "\nTCP: IPv4\n";
45667c478bd9Sstevel@tonic-gate static const char tcp_hdr_v4_compat[] =
45677c478bd9Sstevel@tonic-gate "\nTCP\n";
45687c478bd9Sstevel@tonic-gate static const char tcp_hdr_v4_verbose[] =
45697c478bd9Sstevel@tonic-gate "Local/Remote Address Swind  Snext     Suna   Rwind  Rnext     Rack   "
457045916cd2Sjpk " Rto   Mss     State\n"
45717c478bd9Sstevel@tonic-gate "-------------------- ----- -------- -------- ----- -------- -------- "
457245916cd2Sjpk "----- ----- -----------\n";
45737c478bd9Sstevel@tonic-gate static const char tcp_hdr_v4_normal[] =
457445916cd2Sjpk "   Local Address        Remote Address    Swind Send-Q Rwind Recv-Q "
457545916cd2Sjpk "   State\n"
457645916cd2Sjpk "-------------------- -------------------- ----- ------ ----- ------ "
457745916cd2Sjpk "-----------\n";
45787c478bd9Sstevel@tonic-gate 
45797c478bd9Sstevel@tonic-gate static const char tcp_hdr_v6[] =
45807c478bd9Sstevel@tonic-gate "\nTCP: IPv6\n";
45817c478bd9Sstevel@tonic-gate static const char tcp_hdr_v6_verbose[] =
45827c478bd9Sstevel@tonic-gate "Local/Remote Address              Swind  Snext     Suna   Rwind  Rnext   "
458345916cd2Sjpk "  Rack    Rto   Mss    State      If\n"
45847c478bd9Sstevel@tonic-gate "--------------------------------- ----- -------- -------- ----- -------- "
45857c478bd9Sstevel@tonic-gate "-------- ----- ----- ----------- -----\n";
45867c478bd9Sstevel@tonic-gate static const char tcp_hdr_v6_normal[] =
45877c478bd9Sstevel@tonic-gate "   Local Address                     Remote Address                 "
458845916cd2Sjpk "Swind Send-Q Rwind Recv-Q   State      If\n"
45897c478bd9Sstevel@tonic-gate "--------------------------------- --------------------------------- "
45907c478bd9Sstevel@tonic-gate "----- ------ ----- ------ ----------- -----\n";
45917c478bd9Sstevel@tonic-gate 
459245916cd2Sjpk static boolean_t tcp_report_item_v4(const mib2_tcpConnEntry_t *,
459345916cd2Sjpk     boolean_t first, const mib2_transportMLPEntry_t *);
459445916cd2Sjpk static boolean_t tcp_report_item_v6(const mib2_tcp6ConnEntry_t *,
459545916cd2Sjpk     boolean_t first, const mib2_transportMLPEntry_t *);
45967c478bd9Sstevel@tonic-gate 
45977c478bd9Sstevel@tonic-gate static void
459845916cd2Sjpk tcp_report(const mib_item_t *item)
45997c478bd9Sstevel@tonic-gate {
46007c478bd9Sstevel@tonic-gate 	int			jtemp = 0;
46017c478bd9Sstevel@tonic-gate 	boolean_t		print_hdr_once_v4 = B_TRUE;
46027c478bd9Sstevel@tonic-gate 	boolean_t		print_hdr_once_v6 = B_TRUE;
46037c478bd9Sstevel@tonic-gate 	mib2_tcpConnEntry_t	*tp;
46047c478bd9Sstevel@tonic-gate 	mib2_tcp6ConnEntry_t	*tp6;
460545916cd2Sjpk 	mib2_transportMLPEntry_t **v4_attrs, **v6_attrs;
460645916cd2Sjpk 	mib2_transportMLPEntry_t **v4a, **v6a;
460745916cd2Sjpk 	mib2_transportMLPEntry_t *aptr;
46087c478bd9Sstevel@tonic-gate 
46097c478bd9Sstevel@tonic-gate 	if (!protocol_selected(IPPROTO_TCP))
46107c478bd9Sstevel@tonic-gate 		return;
46117c478bd9Sstevel@tonic-gate 
461245916cd2Sjpk 	/*
461345916cd2Sjpk 	 * Preparation pass: the kernel returns separate entries for TCP
461445916cd2Sjpk 	 * connection table entries and Multilevel Port attributes.  We loop
461545916cd2Sjpk 	 * through the attributes first and set up an array for each address
461645916cd2Sjpk 	 * family.
461745916cd2Sjpk 	 */
461845916cd2Sjpk 	v4_attrs = family_selected(AF_INET) && RSECflag ?
461945916cd2Sjpk 	    gather_attrs(item, MIB2_TCP, MIB2_TCP_CONN, tcpConnEntrySize) :
462045916cd2Sjpk 	    NULL;
462145916cd2Sjpk 	v6_attrs = family_selected(AF_INET6) && RSECflag ?
462245916cd2Sjpk 	    gather_attrs(item, MIB2_TCP6, MIB2_TCP6_CONN, tcp6ConnEntrySize) :
462345916cd2Sjpk 	    NULL;
462445916cd2Sjpk 
46257c478bd9Sstevel@tonic-gate 	/* 'for' loop 1: */
462645916cd2Sjpk 	v4a = v4_attrs;
462745916cd2Sjpk 	v6a = v6_attrs;
462845916cd2Sjpk 	for (; item != NULL; item = item->next_item) {
46297c478bd9Sstevel@tonic-gate 		if (Dflag) {
46307c478bd9Sstevel@tonic-gate 			(void) printf("\n--- Entry %d ---\n", ++jtemp);
46317c478bd9Sstevel@tonic-gate 			(void) printf("Group = %d, mib_id = %d, "
46327c478bd9Sstevel@tonic-gate 			    "length = %d, valp = 0x%p\n",
46337c478bd9Sstevel@tonic-gate 			    item->group, item->mib_id,
46347c478bd9Sstevel@tonic-gate 			    item->length, item->valp);
46357c478bd9Sstevel@tonic-gate 		}
46367c478bd9Sstevel@tonic-gate 
46377c478bd9Sstevel@tonic-gate 		if (!((item->group == MIB2_TCP &&
46387c478bd9Sstevel@tonic-gate 		    item->mib_id == MIB2_TCP_CONN) ||
46397c478bd9Sstevel@tonic-gate 		    (item->group == MIB2_TCP6 &&
46407c478bd9Sstevel@tonic-gate 		    item->mib_id == MIB2_TCP6_CONN)))
46417c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
46427c478bd9Sstevel@tonic-gate 
46437c478bd9Sstevel@tonic-gate 		if (item->group == MIB2_TCP && !family_selected(AF_INET))
46447c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
46457c478bd9Sstevel@tonic-gate 		else if (item->group == MIB2_TCP6 && !family_selected(AF_INET6))
46467c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
46477c478bd9Sstevel@tonic-gate 
46487c478bd9Sstevel@tonic-gate 		if (item->group == MIB2_TCP) {
46497c478bd9Sstevel@tonic-gate 			for (tp = (mib2_tcpConnEntry_t *)item->valp;
46507c478bd9Sstevel@tonic-gate 			    (char *)tp < (char *)item->valp + item->length;
46517c478bd9Sstevel@tonic-gate 			    /* LINTED: (note 1) */
46527c478bd9Sstevel@tonic-gate 			    tp = (mib2_tcpConnEntry_t *)((char *)tp +
46537c478bd9Sstevel@tonic-gate 			    tcpConnEntrySize)) {
465445916cd2Sjpk 				aptr = v4a == NULL ? NULL : *v4a++;
46557c478bd9Sstevel@tonic-gate 				print_hdr_once_v4 = tcp_report_item_v4(tp,
465645916cd2Sjpk 				    print_hdr_once_v4, aptr);
46577c478bd9Sstevel@tonic-gate 			}
46587c478bd9Sstevel@tonic-gate 		} else {
46597c478bd9Sstevel@tonic-gate 			for (tp6 = (mib2_tcp6ConnEntry_t *)item->valp;
46607c478bd9Sstevel@tonic-gate 			    (char *)tp6 < (char *)item->valp + item->length;
46617c478bd9Sstevel@tonic-gate 			    /* LINTED: (note 1) */
46627c478bd9Sstevel@tonic-gate 			    tp6 = (mib2_tcp6ConnEntry_t *)((char *)tp6 +
46637c478bd9Sstevel@tonic-gate 			    tcp6ConnEntrySize)) {
466445916cd2Sjpk 				aptr = v6a == NULL ? NULL : *v6a++;
46657c478bd9Sstevel@tonic-gate 				print_hdr_once_v6 = tcp_report_item_v6(tp6,
466645916cd2Sjpk 				    print_hdr_once_v6, aptr);
46677c478bd9Sstevel@tonic-gate 			}
46687c478bd9Sstevel@tonic-gate 		}
46697c478bd9Sstevel@tonic-gate 	} /* 'for' loop 1 ends */
46707c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
467145916cd2Sjpk 
467245916cd2Sjpk 	if (v4_attrs != NULL)
467345916cd2Sjpk 		free(v4_attrs);
467445916cd2Sjpk 	if (v6_attrs != NULL)
467545916cd2Sjpk 		free(v6_attrs);
46767c478bd9Sstevel@tonic-gate }
46777c478bd9Sstevel@tonic-gate 
46787c478bd9Sstevel@tonic-gate static boolean_t
467945916cd2Sjpk tcp_report_item_v4(const mib2_tcpConnEntry_t *tp, boolean_t first,
468045916cd2Sjpk     const mib2_transportMLPEntry_t *attr)
46817c478bd9Sstevel@tonic-gate {
46827c478bd9Sstevel@tonic-gate 	/*
46837c478bd9Sstevel@tonic-gate 	 * lname and fname below are for the hostname as well as the portname
46847c478bd9Sstevel@tonic-gate 	 * There is no limit on portname length so we assume MAXHOSTNAMELEN
46857c478bd9Sstevel@tonic-gate 	 * as the limit
46867c478bd9Sstevel@tonic-gate 	 */
46877c478bd9Sstevel@tonic-gate 	char	lname[MAXHOSTNAMELEN + MAXHOSTNAMELEN + 1];
46887c478bd9Sstevel@tonic-gate 	char	fname[MAXHOSTNAMELEN + MAXHOSTNAMELEN + 1];
46897c478bd9Sstevel@tonic-gate 
46907c478bd9Sstevel@tonic-gate 	if (!(Aflag || tp->tcpConnEntryInfo.ce_state >= TCPS_ESTABLISHED))
46917c478bd9Sstevel@tonic-gate 		return (first); /* Nothing to print */
46927c478bd9Sstevel@tonic-gate 
46937c478bd9Sstevel@tonic-gate 	if (first) {
469445916cd2Sjpk 		(void) printf(v4compat ? tcp_hdr_v4_compat : tcp_hdr_v4);
469545916cd2Sjpk 		(void) printf(Vflag ? tcp_hdr_v4_verbose : tcp_hdr_v4_normal);
46967c478bd9Sstevel@tonic-gate 	}
46977c478bd9Sstevel@tonic-gate 
46987c478bd9Sstevel@tonic-gate 	if (Vflag) {
46997c478bd9Sstevel@tonic-gate 		(void) printf("%-20s\n%-20s %5u %08x %08x %5u %08x %08x "
47007c478bd9Sstevel@tonic-gate 		    "%5u %5u %s\n",
47017c478bd9Sstevel@tonic-gate 		    pr_ap(tp->tcpConnLocalAddress,
4702e11c3f44Smeem 		    tp->tcpConnLocalPort, "tcp", lname, sizeof (lname)),
47037c478bd9Sstevel@tonic-gate 		    pr_ap(tp->tcpConnRemAddress,
4704e11c3f44Smeem 		    tp->tcpConnRemPort, "tcp", fname, sizeof (fname)),
47057c478bd9Sstevel@tonic-gate 		    tp->tcpConnEntryInfo.ce_swnd,
47067c478bd9Sstevel@tonic-gate 		    tp->tcpConnEntryInfo.ce_snxt,
47077c478bd9Sstevel@tonic-gate 		    tp->tcpConnEntryInfo.ce_suna,
47087c478bd9Sstevel@tonic-gate 		    tp->tcpConnEntryInfo.ce_rwnd,
47097c478bd9Sstevel@tonic-gate 		    tp->tcpConnEntryInfo.ce_rnxt,
47107c478bd9Sstevel@tonic-gate 		    tp->tcpConnEntryInfo.ce_rack,
47117c478bd9Sstevel@tonic-gate 		    tp->tcpConnEntryInfo.ce_rto,
47127c478bd9Sstevel@tonic-gate 		    tp->tcpConnEntryInfo.ce_mss,
471345916cd2Sjpk 		    mitcp_state(tp->tcpConnEntryInfo.ce_state, attr));
47147c478bd9Sstevel@tonic-gate 	} else {
47157c478bd9Sstevel@tonic-gate 		int sq = (int)tp->tcpConnEntryInfo.ce_snxt -
47167c478bd9Sstevel@tonic-gate 		    (int)tp->tcpConnEntryInfo.ce_suna - 1;
47177c478bd9Sstevel@tonic-gate 		int rq = (int)tp->tcpConnEntryInfo.ce_rnxt -
47187c478bd9Sstevel@tonic-gate 		    (int)tp->tcpConnEntryInfo.ce_rack;
47197c478bd9Sstevel@tonic-gate 
47207c478bd9Sstevel@tonic-gate 		(void) printf("%-20s %-20s %5u %6d %5u %6d %s\n",
47217c478bd9Sstevel@tonic-gate 		    pr_ap(tp->tcpConnLocalAddress,
4722e11c3f44Smeem 		    tp->tcpConnLocalPort, "tcp", lname, sizeof (lname)),
47237c478bd9Sstevel@tonic-gate 		    pr_ap(tp->tcpConnRemAddress,
4724e11c3f44Smeem 		    tp->tcpConnRemPort, "tcp", fname, sizeof (fname)),
47257c478bd9Sstevel@tonic-gate 		    tp->tcpConnEntryInfo.ce_swnd,
47267c478bd9Sstevel@tonic-gate 		    (sq >= 0) ? sq : 0,
47277c478bd9Sstevel@tonic-gate 		    tp->tcpConnEntryInfo.ce_rwnd,
47287c478bd9Sstevel@tonic-gate 		    (rq >= 0) ? rq : 0,
472945916cd2Sjpk 		    mitcp_state(tp->tcpConnEntryInfo.ce_state, attr));
47307c478bd9Sstevel@tonic-gate 	}
473145916cd2Sjpk 
473245916cd2Sjpk 	print_transport_label(attr);
473345916cd2Sjpk 
473445916cd2Sjpk 	return (B_FALSE);
47357c478bd9Sstevel@tonic-gate }
47367c478bd9Sstevel@tonic-gate 
47377c478bd9Sstevel@tonic-gate static boolean_t
473845916cd2Sjpk tcp_report_item_v6(const mib2_tcp6ConnEntry_t *tp6, boolean_t first,
473945916cd2Sjpk     const mib2_transportMLPEntry_t *attr)
47407c478bd9Sstevel@tonic-gate {
47417c478bd9Sstevel@tonic-gate 	/*
47427c478bd9Sstevel@tonic-gate 	 * lname and fname below are for the hostname as well as the portname
47437c478bd9Sstevel@tonic-gate 	 * There is no limit on portname length so we assume MAXHOSTNAMELEN
47447c478bd9Sstevel@tonic-gate 	 * as the limit
47457c478bd9Sstevel@tonic-gate 	 */
47467c478bd9Sstevel@tonic-gate 	char	lname[MAXHOSTNAMELEN + MAXHOSTNAMELEN + 1];
47477c478bd9Sstevel@tonic-gate 	char	fname[MAXHOSTNAMELEN + MAXHOSTNAMELEN + 1];
47487c478bd9Sstevel@tonic-gate 	char	ifname[LIFNAMSIZ + 1];
47497c478bd9Sstevel@tonic-gate 	char	*ifnamep;
47507c478bd9Sstevel@tonic-gate 
47517c478bd9Sstevel@tonic-gate 	if (!(Aflag || tp6->tcp6ConnEntryInfo.ce_state >= TCPS_ESTABLISHED))
47527c478bd9Sstevel@tonic-gate 		return (first); /* Nothing to print */
47537c478bd9Sstevel@tonic-gate 
47547c478bd9Sstevel@tonic-gate 	if (first) {
475545916cd2Sjpk 		(void) printf(tcp_hdr_v6);
475645916cd2Sjpk 		(void) printf(Vflag ? tcp_hdr_v6_verbose : tcp_hdr_v6_normal);
47577c478bd9Sstevel@tonic-gate 	}
47587c478bd9Sstevel@tonic-gate 
47597c478bd9Sstevel@tonic-gate 	ifnamep = (tp6->tcp6ConnIfIndex != 0) ?
47607c478bd9Sstevel@tonic-gate 	    if_indextoname(tp6->tcp6ConnIfIndex, ifname) : NULL;
476145916cd2Sjpk 	if (ifnamep == NULL)
476245916cd2Sjpk 		ifnamep = "";
47637c478bd9Sstevel@tonic-gate 
47647c478bd9Sstevel@tonic-gate 	if (Vflag) {
47657c478bd9Sstevel@tonic-gate 		(void) printf("%-33s\n%-33s %5u %08x %08x %5u %08x %08x "
476645916cd2Sjpk 		    "%5u %5u %-11s %s\n",
47677c478bd9Sstevel@tonic-gate 		    pr_ap6(&tp6->tcp6ConnLocalAddress,
4768e11c3f44Smeem 		    tp6->tcp6ConnLocalPort, "tcp", lname, sizeof (lname)),
47697c478bd9Sstevel@tonic-gate 		    pr_ap6(&tp6->tcp6ConnRemAddress,
4770e11c3f44Smeem 		    tp6->tcp6ConnRemPort, "tcp", fname, sizeof (fname)),
47717c478bd9Sstevel@tonic-gate 		    tp6->tcp6ConnEntryInfo.ce_swnd,
47727c478bd9Sstevel@tonic-gate 		    tp6->tcp6ConnEntryInfo.ce_snxt,
47737c478bd9Sstevel@tonic-gate 		    tp6->tcp6ConnEntryInfo.ce_suna,
47747c478bd9Sstevel@tonic-gate 		    tp6->tcp6ConnEntryInfo.ce_rwnd,
47757c478bd9Sstevel@tonic-gate 		    tp6->tcp6ConnEntryInfo.ce_rnxt,
47767c478bd9Sstevel@tonic-gate 		    tp6->tcp6ConnEntryInfo.ce_rack,
47777c478bd9Sstevel@tonic-gate 		    tp6->tcp6ConnEntryInfo.ce_rto,
47787c478bd9Sstevel@tonic-gate 		    tp6->tcp6ConnEntryInfo.ce_mss,
477945916cd2Sjpk 		    mitcp_state(tp6->tcp6ConnEntryInfo.ce_state, attr),
478045916cd2Sjpk 		    ifnamep);
47817c478bd9Sstevel@tonic-gate 	} else {
47827c478bd9Sstevel@tonic-gate 		int sq = (int)tp6->tcp6ConnEntryInfo.ce_snxt -
47837c478bd9Sstevel@tonic-gate 		    (int)tp6->tcp6ConnEntryInfo.ce_suna - 1;
47847c478bd9Sstevel@tonic-gate 		int rq = (int)tp6->tcp6ConnEntryInfo.ce_rnxt -
47857c478bd9Sstevel@tonic-gate 		    (int)tp6->tcp6ConnEntryInfo.ce_rack;
47867c478bd9Sstevel@tonic-gate 
478745916cd2Sjpk 		(void) printf("%-33s %-33s %5u %6d %5u %6d %-11s %s\n",
47887c478bd9Sstevel@tonic-gate 		    pr_ap6(&tp6->tcp6ConnLocalAddress,
4789e11c3f44Smeem 		    tp6->tcp6ConnLocalPort, "tcp", lname, sizeof (lname)),
47907c478bd9Sstevel@tonic-gate 		    pr_ap6(&tp6->tcp6ConnRemAddress,
4791e11c3f44Smeem 		    tp6->tcp6ConnRemPort, "tcp", fname, sizeof (fname)),
47927c478bd9Sstevel@tonic-gate 		    tp6->tcp6ConnEntryInfo.ce_swnd,
47937c478bd9Sstevel@tonic-gate 		    (sq >= 0) ? sq : 0,
47947c478bd9Sstevel@tonic-gate 		    tp6->tcp6ConnEntryInfo.ce_rwnd,
47957c478bd9Sstevel@tonic-gate 		    (rq >= 0) ? rq : 0,
479645916cd2Sjpk 		    mitcp_state(tp6->tcp6ConnEntryInfo.ce_state, attr),
479745916cd2Sjpk 		    ifnamep);
47987c478bd9Sstevel@tonic-gate 	}
479945916cd2Sjpk 
480045916cd2Sjpk 	print_transport_label(attr);
480145916cd2Sjpk 
480245916cd2Sjpk 	return (B_FALSE);
48037c478bd9Sstevel@tonic-gate }
48047c478bd9Sstevel@tonic-gate 
48057c478bd9Sstevel@tonic-gate /* ------------------------------- UDP_REPORT------------------------------- */
48067c478bd9Sstevel@tonic-gate 
480745916cd2Sjpk static boolean_t udp_report_item_v4(const mib2_udpEntry_t *ude,
480845916cd2Sjpk     boolean_t first, const mib2_transportMLPEntry_t *attr);
480945916cd2Sjpk static boolean_t udp_report_item_v6(const mib2_udp6Entry_t *ude6,
481045916cd2Sjpk     boolean_t first, const mib2_transportMLPEntry_t *attr);
481145916cd2Sjpk 
481245916cd2Sjpk static const char udp_hdr_v4[] =
481345916cd2Sjpk "   Local Address        Remote Address      State\n"
481445916cd2Sjpk "-------------------- -------------------- ----------\n";
48157c478bd9Sstevel@tonic-gate 
481645916cd2Sjpk static const char udp_hdr_v6[] =
48177c478bd9Sstevel@tonic-gate "   Local Address                     Remote Address                 "
481845916cd2Sjpk "  State      If\n"
48197c478bd9Sstevel@tonic-gate "--------------------------------- --------------------------------- "
48207c478bd9Sstevel@tonic-gate "---------- -----\n";
48217c478bd9Sstevel@tonic-gate 
48227c478bd9Sstevel@tonic-gate static void
482345916cd2Sjpk udp_report(const mib_item_t *item)
48247c478bd9Sstevel@tonic-gate {
48257c478bd9Sstevel@tonic-gate 	int			jtemp = 0;
48267c478bd9Sstevel@tonic-gate 	boolean_t		print_hdr_once_v4 = B_TRUE;
48277c478bd9Sstevel@tonic-gate 	boolean_t		print_hdr_once_v6 = B_TRUE;
48287c478bd9Sstevel@tonic-gate 	mib2_udpEntry_t		*ude;
48297c478bd9Sstevel@tonic-gate 	mib2_udp6Entry_t	*ude6;
483045916cd2Sjpk 	mib2_transportMLPEntry_t **v4_attrs, **v6_attrs;
483145916cd2Sjpk 	mib2_transportMLPEntry_t **v4a, **v6a;
483245916cd2Sjpk 	mib2_transportMLPEntry_t *aptr;
48337c478bd9Sstevel@tonic-gate 
48347c478bd9Sstevel@tonic-gate 	if (!protocol_selected(IPPROTO_UDP))
48357c478bd9Sstevel@tonic-gate 		return;
48367c478bd9Sstevel@tonic-gate 
483745916cd2Sjpk 	/*
483845916cd2Sjpk 	 * Preparation pass: the kernel returns separate entries for UDP
483945916cd2Sjpk 	 * connection table entries and Multilevel Port attributes.  We loop
484045916cd2Sjpk 	 * through the attributes first and set up an array for each address
484145916cd2Sjpk 	 * family.
484245916cd2Sjpk 	 */
484345916cd2Sjpk 	v4_attrs = family_selected(AF_INET) && RSECflag ?
484445916cd2Sjpk 	    gather_attrs(item, MIB2_UDP, MIB2_UDP_ENTRY, udpEntrySize) : NULL;
484545916cd2Sjpk 	v6_attrs = family_selected(AF_INET6) && RSECflag ?
484645916cd2Sjpk 	    gather_attrs(item, MIB2_UDP6, MIB2_UDP6_ENTRY, udp6EntrySize) :
484745916cd2Sjpk 	    NULL;
484845916cd2Sjpk 
484945916cd2Sjpk 	v4a = v4_attrs;
485045916cd2Sjpk 	v6a = v6_attrs;
48517c478bd9Sstevel@tonic-gate 	/* 'for' loop 1: */
48527c478bd9Sstevel@tonic-gate 	for (; item; item = item->next_item) {
48537c478bd9Sstevel@tonic-gate 		if (Dflag) {
48547c478bd9Sstevel@tonic-gate 			(void) printf("\n--- Entry %d ---\n", ++jtemp);
48557c478bd9Sstevel@tonic-gate 			(void) printf("Group = %d, mib_id = %d, "
48567c478bd9Sstevel@tonic-gate 			    "length = %d, valp = 0x%p\n",
48577c478bd9Sstevel@tonic-gate 			    item->group, item->mib_id,
48587c478bd9Sstevel@tonic-gate 			    item->length, item->valp);
48597c478bd9Sstevel@tonic-gate 		}
48607c478bd9Sstevel@tonic-gate 		if (!((item->group == MIB2_UDP &&
48617c478bd9Sstevel@tonic-gate 		    item->mib_id == MIB2_UDP_ENTRY) ||
48627c478bd9Sstevel@tonic-gate 		    (item->group == MIB2_UDP6 &&
48637c478bd9Sstevel@tonic-gate 		    item->mib_id == MIB2_UDP6_ENTRY)))
48647c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
48657c478bd9Sstevel@tonic-gate 
48667c478bd9Sstevel@tonic-gate 		if (item->group == MIB2_UDP && !family_selected(AF_INET))
48677c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
48687c478bd9Sstevel@tonic-gate 		else if (item->group == MIB2_UDP6 && !family_selected(AF_INET6))
48697c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
48707c478bd9Sstevel@tonic-gate 
48717c478bd9Sstevel@tonic-gate 		/*	xxx.xxx.xxx.xxx,pppp  sss... */
48727c478bd9Sstevel@tonic-gate 		if (item->group == MIB2_UDP) {
48737c478bd9Sstevel@tonic-gate 			for (ude = (mib2_udpEntry_t *)item->valp;
48747c478bd9Sstevel@tonic-gate 			    (char *)ude < (char *)item->valp + item->length;
48757c478bd9Sstevel@tonic-gate 			    /* LINTED: (note 1) */
48767c478bd9Sstevel@tonic-gate 			    ude = (mib2_udpEntry_t *)((char *)ude +
48777c478bd9Sstevel@tonic-gate 			    udpEntrySize)) {
487845916cd2Sjpk 				aptr = v4a == NULL ? NULL : *v4a++;
48797c478bd9Sstevel@tonic-gate 				print_hdr_once_v4 = udp_report_item_v4(ude,
488045916cd2Sjpk 				    print_hdr_once_v4, aptr);
48817c478bd9Sstevel@tonic-gate 			}
48827c478bd9Sstevel@tonic-gate 		} else {
48837c478bd9Sstevel@tonic-gate 			for (ude6 = (mib2_udp6Entry_t *)item->valp;
48847c478bd9Sstevel@tonic-gate 			    (char *)ude6 < (char *)item->valp + item->length;
48857c478bd9Sstevel@tonic-gate 			    /* LINTED: (note 1) */
48867c478bd9Sstevel@tonic-gate 			    ude6 = (mib2_udp6Entry_t *)((char *)ude6 +
48877c478bd9Sstevel@tonic-gate 			    udp6EntrySize)) {
488845916cd2Sjpk 				aptr = v6a == NULL ? NULL : *v6a++;
48897c478bd9Sstevel@tonic-gate 				print_hdr_once_v6 = udp_report_item_v6(ude6,
489045916cd2Sjpk 				    print_hdr_once_v6, aptr);
48917c478bd9Sstevel@tonic-gate 			}
48927c478bd9Sstevel@tonic-gate 		}
48937c478bd9Sstevel@tonic-gate 	} /* 'for' loop 1 ends */
48947c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
489545916cd2Sjpk 
489645916cd2Sjpk 	if (v4_attrs != NULL)
489745916cd2Sjpk 		free(v4_attrs);
489845916cd2Sjpk 	if (v6_attrs != NULL)
489945916cd2Sjpk 		free(v6_attrs);
49007c478bd9Sstevel@tonic-gate }
49017c478bd9Sstevel@tonic-gate 
49027c478bd9Sstevel@tonic-gate static boolean_t
490345916cd2Sjpk udp_report_item_v4(const mib2_udpEntry_t *ude, boolean_t first,
490445916cd2Sjpk     const mib2_transportMLPEntry_t *attr)
49057c478bd9Sstevel@tonic-gate {
49067c478bd9Sstevel@tonic-gate 	char	lname[MAXHOSTNAMELEN + MAXHOSTNAMELEN + 1];
49077c478bd9Sstevel@tonic-gate 			/* hostname + portname */
49087c478bd9Sstevel@tonic-gate 
49097c478bd9Sstevel@tonic-gate 	if (!(Aflag || ude->udpEntryInfo.ue_state >= MIB2_UDP_connected))
49107c478bd9Sstevel@tonic-gate 		return (first); /* Nothing to print */
49117c478bd9Sstevel@tonic-gate 
49127c478bd9Sstevel@tonic-gate 	if (first) {
491345916cd2Sjpk 		(void) printf(v4compat ? "\nUDP\n" : "\nUDP: IPv4\n");
491445916cd2Sjpk 		(void) printf(udp_hdr_v4);
49157c478bd9Sstevel@tonic-gate 		first = B_FALSE;
49167c478bd9Sstevel@tonic-gate 	}
49177c478bd9Sstevel@tonic-gate 
49187c478bd9Sstevel@tonic-gate 	(void) printf("%-20s ",
49197c478bd9Sstevel@tonic-gate 	    pr_ap(ude->udpLocalAddress, ude->udpLocalPort, "udp",
49207c478bd9Sstevel@tonic-gate 	    lname, sizeof (lname)));
492145916cd2Sjpk 	(void) printf("%-20s %s\n",
492245916cd2Sjpk 	    ude->udpEntryInfo.ue_state == MIB2_UDP_connected ?
492345916cd2Sjpk 	    pr_ap(ude->udpEntryInfo.ue_RemoteAddress,
492445916cd2Sjpk 	    ude->udpEntryInfo.ue_RemotePort, "udp", lname, sizeof (lname)) :
492545916cd2Sjpk 	    "",
492645916cd2Sjpk 	    miudp_state(ude->udpEntryInfo.ue_state, attr));
492745916cd2Sjpk 
492845916cd2Sjpk 	/*
492945916cd2Sjpk 	 * UDP sockets don't have remote attributes, so there's no need to
493045916cd2Sjpk 	 * print them here.
493145916cd2Sjpk 	 */
493245916cd2Sjpk 
49337c478bd9Sstevel@tonic-gate 	return (first);
49347c478bd9Sstevel@tonic-gate }
49357c478bd9Sstevel@tonic-gate 
49367c478bd9Sstevel@tonic-gate static boolean_t
493745916cd2Sjpk udp_report_item_v6(const mib2_udp6Entry_t *ude6, boolean_t first,
493845916cd2Sjpk     const mib2_transportMLPEntry_t *attr)
49397c478bd9Sstevel@tonic-gate {
49407c478bd9Sstevel@tonic-gate 	char	lname[MAXHOSTNAMELEN + MAXHOSTNAMELEN + 1];
49417c478bd9Sstevel@tonic-gate 			/* hostname + portname */
49427c478bd9Sstevel@tonic-gate 	char	ifname[LIFNAMSIZ + 1];
494345916cd2Sjpk 	const char *ifnamep;
49447c478bd9Sstevel@tonic-gate 
49457c478bd9Sstevel@tonic-gate 	if (!(Aflag || ude6->udp6EntryInfo.ue_state >= MIB2_UDP_connected))
49467c478bd9Sstevel@tonic-gate 		return (first); /* Nothing to print */
49477c478bd9Sstevel@tonic-gate 
49487c478bd9Sstevel@tonic-gate 	if (first) {
494945916cd2Sjpk 		(void) printf("\nUDP: IPv6\n");
495045916cd2Sjpk 		(void) printf(udp_hdr_v6);
49517c478bd9Sstevel@tonic-gate 		first = B_FALSE;
49527c478bd9Sstevel@tonic-gate 	}
49537c478bd9Sstevel@tonic-gate 
495445916cd2Sjpk 	ifnamep = (ude6->udp6IfIndex != 0) ?
495545916cd2Sjpk 	    if_indextoname(ude6->udp6IfIndex, ifname) : NULL;
495645916cd2Sjpk 
49577c478bd9Sstevel@tonic-gate 	(void) printf("%-33s ",
49587c478bd9Sstevel@tonic-gate 	    pr_ap6(&ude6->udp6LocalAddress,
49597c478bd9Sstevel@tonic-gate 	    ude6->udp6LocalPort, "udp", lname, sizeof (lname)));
496045916cd2Sjpk 	(void) printf("%-33s %-10s %s\n",
496145916cd2Sjpk 	    ude6->udp6EntryInfo.ue_state == MIB2_UDP_connected ?
496245916cd2Sjpk 	    pr_ap6(&ude6->udp6EntryInfo.ue_RemoteAddress,
496345916cd2Sjpk 	    ude6->udp6EntryInfo.ue_RemotePort, "udp", lname, sizeof (lname)) :
496445916cd2Sjpk 	    "",
496545916cd2Sjpk 	    miudp_state(ude6->udp6EntryInfo.ue_state, attr),
496645916cd2Sjpk 	    ifnamep == NULL ? "" : ifnamep);
496745916cd2Sjpk 
496845916cd2Sjpk 	/*
496945916cd2Sjpk 	 * UDP sockets don't have remote attributes, so there's no need to
497045916cd2Sjpk 	 * print them here.
497145916cd2Sjpk 	 */
497245916cd2Sjpk 
49737c478bd9Sstevel@tonic-gate 	return (first);
49747c478bd9Sstevel@tonic-gate }
49757c478bd9Sstevel@tonic-gate 
49767c478bd9Sstevel@tonic-gate /* ------------------------------ SCTP_REPORT------------------------------- */
49777c478bd9Sstevel@tonic-gate 
49787c478bd9Sstevel@tonic-gate static const char sctp_hdr[] =
49797c478bd9Sstevel@tonic-gate "\nSCTP:";
49807c478bd9Sstevel@tonic-gate static const char sctp_hdr_normal[] =
49817c478bd9Sstevel@tonic-gate "        Local Address                   Remote Address          "
49827c478bd9Sstevel@tonic-gate "Swind  Send-Q Rwind  Recv-Q StrsI/O  State\n"
49837c478bd9Sstevel@tonic-gate "------------------------------- ------------------------------- "
49847c478bd9Sstevel@tonic-gate "------ ------ ------ ------ ------- -----------";
49857c478bd9Sstevel@tonic-gate 
49867c478bd9Sstevel@tonic-gate static const char *
498745916cd2Sjpk nssctp_state(int state, const mib2_transportMLPEntry_t *attr)
49887c478bd9Sstevel@tonic-gate {
498945916cd2Sjpk 	static char sctpsbuf[50];
499045916cd2Sjpk 	const char *cp;
499145916cd2Sjpk 
49927c478bd9Sstevel@tonic-gate 	switch (state) {
49937c478bd9Sstevel@tonic-gate 	case MIB2_SCTP_closed:
499445916cd2Sjpk 		cp = "CLOSED";
499545916cd2Sjpk 		break;
49967c478bd9Sstevel@tonic-gate 	case MIB2_SCTP_cookieWait:
499745916cd2Sjpk 		cp = "COOKIE_WAIT";
499845916cd2Sjpk 		break;
49997c478bd9Sstevel@tonic-gate 	case MIB2_SCTP_cookieEchoed:
500045916cd2Sjpk 		cp = "COOKIE_ECHOED";
500145916cd2Sjpk 		break;
50027c478bd9Sstevel@tonic-gate 	case MIB2_SCTP_established:
500345916cd2Sjpk 		cp = "ESTABLISHED";
500445916cd2Sjpk 		break;
50057c478bd9Sstevel@tonic-gate 	case MIB2_SCTP_shutdownPending:
500645916cd2Sjpk 		cp = "SHUTDOWN_PENDING";
500745916cd2Sjpk 		break;
50087c478bd9Sstevel@tonic-gate 	case MIB2_SCTP_shutdownSent:
500945916cd2Sjpk 		cp = "SHUTDOWN_SENT";
501045916cd2Sjpk 		break;
50117c478bd9Sstevel@tonic-gate 	case MIB2_SCTP_shutdownReceived:
501245916cd2Sjpk 		cp = "SHUTDOWN_RECEIVED";
501345916cd2Sjpk 		break;
50147c478bd9Sstevel@tonic-gate 	case MIB2_SCTP_shutdownAckSent:
501545916cd2Sjpk 		cp = "SHUTDOWN_ACK_SENT";
501645916cd2Sjpk 		break;
50177c478bd9Sstevel@tonic-gate 	case MIB2_SCTP_listen:
501845916cd2Sjpk 		cp = "LISTEN";
501945916cd2Sjpk 		break;
50207c478bd9Sstevel@tonic-gate 	default:
502145916cd2Sjpk 		(void) snprintf(sctpsbuf, sizeof (sctpsbuf),
502245916cd2Sjpk 		    "UNKNOWN STATE(%d)", state);
502345916cd2Sjpk 		cp = sctpsbuf;
502445916cd2Sjpk 		break;
502545916cd2Sjpk 	}
502645916cd2Sjpk 
502745916cd2Sjpk 	if (RSECflag && attr != NULL && attr->tme_flags != 0) {
502845916cd2Sjpk 		if (cp != sctpsbuf) {
502945916cd2Sjpk 			(void) strlcpy(sctpsbuf, cp, sizeof (sctpsbuf));
503045916cd2Sjpk 			cp = sctpsbuf;
503145916cd2Sjpk 		}
503245916cd2Sjpk 		if (attr->tme_flags & MIB2_TMEF_PRIVATE)
503345916cd2Sjpk 			(void) strlcat(sctpsbuf, " P", sizeof (sctpsbuf));
503445916cd2Sjpk 		if (attr->tme_flags & MIB2_TMEF_SHARED)
503545916cd2Sjpk 			(void) strlcat(sctpsbuf, " S", sizeof (sctpsbuf));
50367c478bd9Sstevel@tonic-gate 	}
503745916cd2Sjpk 
503845916cd2Sjpk 	return (cp);
50397c478bd9Sstevel@tonic-gate }
50407c478bd9Sstevel@tonic-gate 
504145916cd2Sjpk static const mib2_sctpConnRemoteEntry_t *
504245916cd2Sjpk sctp_getnext_rem(const mib_item_t **itemp,
504345916cd2Sjpk     const mib2_sctpConnRemoteEntry_t *current, uint32_t associd)
50447c478bd9Sstevel@tonic-gate {
504545916cd2Sjpk 	const mib_item_t *item = *itemp;
504645916cd2Sjpk 	const mib2_sctpConnRemoteEntry_t	*sre;
50477c478bd9Sstevel@tonic-gate 
50487c478bd9Sstevel@tonic-gate 	for (; item != NULL; item = item->next_item, current = NULL) {
50497c478bd9Sstevel@tonic-gate 		if (!(item->group == MIB2_SCTP &&
50507c478bd9Sstevel@tonic-gate 		    item->mib_id == MIB2_SCTP_CONN_REMOTE)) {
50517c478bd9Sstevel@tonic-gate 			continue;
50527c478bd9Sstevel@tonic-gate 		}
50537c478bd9Sstevel@tonic-gate 
50547c478bd9Sstevel@tonic-gate 		if (current != NULL) {
50557c478bd9Sstevel@tonic-gate 			/* LINTED: (note 1) */
505645916cd2Sjpk 			sre = (const mib2_sctpConnRemoteEntry_t *)
505745916cd2Sjpk 			    ((const char *)current + sctpRemoteEntrySize);
50587c478bd9Sstevel@tonic-gate 		} else {
50597c478bd9Sstevel@tonic-gate 			sre = item->valp;
50607c478bd9Sstevel@tonic-gate 		}
50617c478bd9Sstevel@tonic-gate 		for (; (char *)sre < (char *)item->valp + item->length;
506245916cd2Sjpk 		    /* LINTED: (note 1) */
506345916cd2Sjpk 		    sre = (const mib2_sctpConnRemoteEntry_t *)
506445916cd2Sjpk 		    ((const char *)sre + sctpRemoteEntrySize)) {
50657c478bd9Sstevel@tonic-gate 			if (sre->sctpAssocId != associd) {
50667c478bd9Sstevel@tonic-gate 				continue;
50677c478bd9Sstevel@tonic-gate 			}
50687c478bd9Sstevel@tonic-gate 			*itemp = item;
50697c478bd9Sstevel@tonic-gate 			return (sre);
50707c478bd9Sstevel@tonic-gate 		}
50717c478bd9Sstevel@tonic-gate 	}
50727c478bd9Sstevel@tonic-gate 	*itemp = NULL;
50737c478bd9Sstevel@tonic-gate 	return (NULL);
50747c478bd9Sstevel@tonic-gate }
50757c478bd9Sstevel@tonic-gate 
507645916cd2Sjpk static const mib2_sctpConnLocalEntry_t *
507745916cd2Sjpk sctp_getnext_local(const mib_item_t **itemp,
507845916cd2Sjpk     const mib2_sctpConnLocalEntry_t *current, uint32_t associd)
50797c478bd9Sstevel@tonic-gate {
508045916cd2Sjpk 	const mib_item_t *item = *itemp;
508145916cd2Sjpk 	const mib2_sctpConnLocalEntry_t	*sle;
50827c478bd9Sstevel@tonic-gate 
50837c478bd9Sstevel@tonic-gate 	for (; item != NULL; item = item->next_item, current = NULL) {
50847c478bd9Sstevel@tonic-gate 		if (!(item->group == MIB2_SCTP &&
50857c478bd9Sstevel@tonic-gate 		    item->mib_id == MIB2_SCTP_CONN_LOCAL)) {
50867c478bd9Sstevel@tonic-gate 			continue;
50877c478bd9Sstevel@tonic-gate 		}
50887c478bd9Sstevel@tonic-gate 
50897c478bd9Sstevel@tonic-gate 		if (current != NULL) {
50907c478bd9Sstevel@tonic-gate 			/* LINTED: (note 1) */
509145916cd2Sjpk 			sle = (const mib2_sctpConnLocalEntry_t *)
509245916cd2Sjpk 			    ((const char *)current + sctpLocalEntrySize);
50937c478bd9Sstevel@tonic-gate 		} else {
50947c478bd9Sstevel@tonic-gate 			sle = item->valp;
50957c478bd9Sstevel@tonic-gate 		}
50967c478bd9Sstevel@tonic-gate 		for (; (char *)sle < (char *)item->valp + item->length;
509745916cd2Sjpk 		    /* LINTED: (note 1) */
509845916cd2Sjpk 		    sle = (const mib2_sctpConnLocalEntry_t *)
509945916cd2Sjpk 		    ((const char *)sle + sctpLocalEntrySize)) {
51007c478bd9Sstevel@tonic-gate 			if (sle->sctpAssocId != associd) {
51017c478bd9Sstevel@tonic-gate 				continue;
51027c478bd9Sstevel@tonic-gate 			}
51037c478bd9Sstevel@tonic-gate 			*itemp = item;
51047c478bd9Sstevel@tonic-gate 			return (sle);
51057c478bd9Sstevel@tonic-gate 		}
51067c478bd9Sstevel@tonic-gate 	}
51077c478bd9Sstevel@tonic-gate 	*itemp = NULL;
51087c478bd9Sstevel@tonic-gate 	return (NULL);
51097c478bd9Sstevel@tonic-gate }
51107c478bd9Sstevel@tonic-gate 
51117c478bd9Sstevel@tonic-gate static void
51127c478bd9Sstevel@tonic-gate sctp_pr_addr(int type, char *name, int namelen, const in6_addr_t *addr,
51137c478bd9Sstevel@tonic-gate     int port)
51147c478bd9Sstevel@tonic-gate {
51157c478bd9Sstevel@tonic-gate 	ipaddr_t	v4addr;
51167c478bd9Sstevel@tonic-gate 	in6_addr_t	v6addr;
51177c478bd9Sstevel@tonic-gate 
51187c478bd9Sstevel@tonic-gate 	/*
51197c478bd9Sstevel@tonic-gate 	 * Address is either a v4 mapped or v6 addr. If
51207c478bd9Sstevel@tonic-gate 	 * it's a v4 mapped, convert to v4 before
51217c478bd9Sstevel@tonic-gate 	 * displaying.
51227c478bd9Sstevel@tonic-gate 	 */
51237c478bd9Sstevel@tonic-gate 	switch (type) {
5124e11c3f44Smeem 	case MIB2_SCTP_ADDR_V4:
51257c478bd9Sstevel@tonic-gate 		/* v4 */
51267c478bd9Sstevel@tonic-gate 		v6addr = *addr;
51277c478bd9Sstevel@tonic-gate 
51287c478bd9Sstevel@tonic-gate 		IN6_V4MAPPED_TO_IPADDR(&v6addr, v4addr);
51297c478bd9Sstevel@tonic-gate 		if (port > 0) {
51307c478bd9Sstevel@tonic-gate 			(void) pr_ap(v4addr, port, "sctp", name, namelen);
51317c478bd9Sstevel@tonic-gate 		} else {
51327c478bd9Sstevel@tonic-gate 			(void) pr_addr(v4addr, name, namelen);
51337c478bd9Sstevel@tonic-gate 		}
51347c478bd9Sstevel@tonic-gate 		break;
51357c478bd9Sstevel@tonic-gate 
5136e11c3f44Smeem 	case MIB2_SCTP_ADDR_V6:
51377c478bd9Sstevel@tonic-gate 		/* v6 */
51387c478bd9Sstevel@tonic-gate 		if (port > 0) {
51397c478bd9Sstevel@tonic-gate 			(void) pr_ap6(addr, port, "sctp", name, namelen);
51407c478bd9Sstevel@tonic-gate 		} else {
51417c478bd9Sstevel@tonic-gate 			(void) pr_addr6(addr, name, namelen);
51427c478bd9Sstevel@tonic-gate 		}
51437c478bd9Sstevel@tonic-gate 		break;
51447c478bd9Sstevel@tonic-gate 
5145e11c3f44Smeem 	default:
51467c478bd9Sstevel@tonic-gate 		(void) snprintf(name, namelen, "<unknown addr type>");
51477c478bd9Sstevel@tonic-gate 		break;
51487c478bd9Sstevel@tonic-gate 	}
51497c478bd9Sstevel@tonic-gate }
51507c478bd9Sstevel@tonic-gate 
51517c478bd9Sstevel@tonic-gate static void
515245916cd2Sjpk sctp_conn_report_item(const mib_item_t *head, const mib2_sctpConnEntry_t *sp,
515345916cd2Sjpk     const mib2_transportMLPEntry_t *attr)
51547c478bd9Sstevel@tonic-gate {
51557c478bd9Sstevel@tonic-gate 	char		lname[MAXHOSTNAMELEN + MAXHOSTNAMELEN + 1];
51567c478bd9Sstevel@tonic-gate 	char		fname[MAXHOSTNAMELEN + MAXHOSTNAMELEN + 1];
515745916cd2Sjpk 	const mib2_sctpConnRemoteEntry_t	*sre = NULL;
515845916cd2Sjpk 	const mib2_sctpConnLocalEntry_t	*sle = NULL;
515945916cd2Sjpk 	const mib_item_t *local = head;
516045916cd2Sjpk 	const mib_item_t *remote = head;
51617c478bd9Sstevel@tonic-gate 	uint32_t	id = sp->sctpAssocId;
51627c478bd9Sstevel@tonic-gate 	boolean_t	printfirst = B_TRUE;
51637c478bd9Sstevel@tonic-gate 
51647c478bd9Sstevel@tonic-gate 	sctp_pr_addr(sp->sctpAssocRemPrimAddrType, fname, sizeof (fname),
51657c478bd9Sstevel@tonic-gate 	    &sp->sctpAssocRemPrimAddr, sp->sctpAssocRemPort);
51667c478bd9Sstevel@tonic-gate 	sctp_pr_addr(sp->sctpAssocRemPrimAddrType, lname, sizeof (lname),
51677c478bd9Sstevel@tonic-gate 	    &sp->sctpAssocLocPrimAddr, sp->sctpAssocLocalPort);
51687c478bd9Sstevel@tonic-gate 
51697c478bd9Sstevel@tonic-gate 	(void) printf("%-31s %-31s %6u %6d %6u %6d %3d/%-3d %s\n",
51707c478bd9Sstevel@tonic-gate 	    lname, fname,
51717c478bd9Sstevel@tonic-gate 	    sp->sctpConnEntryInfo.ce_swnd,
51727c478bd9Sstevel@tonic-gate 	    sp->sctpConnEntryInfo.ce_sendq,
51737c478bd9Sstevel@tonic-gate 	    sp->sctpConnEntryInfo.ce_rwnd,
51747c478bd9Sstevel@tonic-gate 	    sp->sctpConnEntryInfo.ce_recvq,
51757c478bd9Sstevel@tonic-gate 	    sp->sctpAssocInStreams, sp->sctpAssocOutStreams,
517645916cd2Sjpk 	    nssctp_state(sp->sctpAssocState, attr));
517745916cd2Sjpk 
517845916cd2Sjpk 	print_transport_label(attr);
51797c478bd9Sstevel@tonic-gate 
51807c478bd9Sstevel@tonic-gate 	if (!Vflag) {
51817c478bd9Sstevel@tonic-gate 		return;
51827c478bd9Sstevel@tonic-gate 	}
51837c478bd9Sstevel@tonic-gate 
51847c478bd9Sstevel@tonic-gate 	/* Print remote addresses/local addresses on following lines */
51857c478bd9Sstevel@tonic-gate 	while ((sre = sctp_getnext_rem(&remote, sre, id)) != NULL) {
51867c478bd9Sstevel@tonic-gate 		if (!IN6_ARE_ADDR_EQUAL(&sre->sctpAssocRemAddr,
51877c478bd9Sstevel@tonic-gate 		    &sp->sctpAssocRemPrimAddr)) {
51887c478bd9Sstevel@tonic-gate 			if (printfirst == B_TRUE) {
51897c478bd9Sstevel@tonic-gate 				(void) fputs("\t<Remote: ", stdout);
51907c478bd9Sstevel@tonic-gate 				printfirst = B_FALSE;
51917c478bd9Sstevel@tonic-gate 			} else {
51927c478bd9Sstevel@tonic-gate 				(void) fputs(", ", stdout);
51937c478bd9Sstevel@tonic-gate 			}
51947c478bd9Sstevel@tonic-gate 			sctp_pr_addr(sre->sctpAssocRemAddrType, fname,
51957c478bd9Sstevel@tonic-gate 			    sizeof (fname), &sre->sctpAssocRemAddr, -1);
51967c478bd9Sstevel@tonic-gate 			if (sre->sctpAssocRemAddrActive == MIB2_SCTP_ACTIVE) {
51977c478bd9Sstevel@tonic-gate 				(void) fputs(fname, stdout);
51987c478bd9Sstevel@tonic-gate 			} else {
51997c478bd9Sstevel@tonic-gate 				(void) printf("(%s)", fname);
52007c478bd9Sstevel@tonic-gate 			}
52017c478bd9Sstevel@tonic-gate 		}
52027c478bd9Sstevel@tonic-gate 	}
52037c478bd9Sstevel@tonic-gate 	if (printfirst == B_FALSE) {
52047c478bd9Sstevel@tonic-gate 		(void) puts(">");
52057c478bd9Sstevel@tonic-gate 		printfirst = B_TRUE;
52067c478bd9Sstevel@tonic-gate 	}
52077c478bd9Sstevel@tonic-gate 	while ((sle = sctp_getnext_local(&local, sle, id)) != NULL) {
52087c478bd9Sstevel@tonic-gate 		if (!IN6_ARE_ADDR_EQUAL(&sle->sctpAssocLocalAddr,
52097c478bd9Sstevel@tonic-gate 		    &sp->sctpAssocLocPrimAddr)) {
52107c478bd9Sstevel@tonic-gate 			if (printfirst == B_TRUE) {
52117c478bd9Sstevel@tonic-gate 				(void) fputs("\t<Local: ", stdout);
52127c478bd9Sstevel@tonic-gate 				printfirst = B_FALSE;
52137c478bd9Sstevel@tonic-gate 			} else {
52147c478bd9Sstevel@tonic-gate 				(void) fputs(", ", stdout);
52157c478bd9Sstevel@tonic-gate 			}
52167c478bd9Sstevel@tonic-gate 			sctp_pr_addr(sle->sctpAssocLocalAddrType, lname,
52177c478bd9Sstevel@tonic-gate 			    sizeof (lname), &sle->sctpAssocLocalAddr, -1);
52187c478bd9Sstevel@tonic-gate 			(void) fputs(lname, stdout);
52197c478bd9Sstevel@tonic-gate 		}
52207c478bd9Sstevel@tonic-gate 	}
52217c478bd9Sstevel@tonic-gate 	if (printfirst == B_FALSE) {
52227c478bd9Sstevel@tonic-gate 		(void) puts(">");
52237c478bd9Sstevel@tonic-gate 	}
52247c478bd9Sstevel@tonic-gate }
52257c478bd9Sstevel@tonic-gate 
52267c478bd9Sstevel@tonic-gate static void
522745916cd2Sjpk sctp_report(const mib_item_t *item)
52287c478bd9Sstevel@tonic-gate {
522945916cd2Sjpk 	const mib_item_t		*head;
523045916cd2Sjpk 	const mib2_sctpConnEntry_t	*sp;
52317c478bd9Sstevel@tonic-gate 	boolean_t		first = B_TRUE;
523245916cd2Sjpk 	mib2_transportMLPEntry_t **attrs, **aptr;
523345916cd2Sjpk 	mib2_transportMLPEntry_t *attr;
52347c478bd9Sstevel@tonic-gate 
523545916cd2Sjpk 	/*
523645916cd2Sjpk 	 * Preparation pass: the kernel returns separate entries for SCTP
523745916cd2Sjpk 	 * connection table entries and Multilevel Port attributes.  We loop
523845916cd2Sjpk 	 * through the attributes first and set up an array for each address
523945916cd2Sjpk 	 * family.
524045916cd2Sjpk 	 */
524145916cd2Sjpk 	attrs = RSECflag ?
524245916cd2Sjpk 	    gather_attrs(item, MIB2_SCTP, MIB2_SCTP_CONN, sctpEntrySize) :
524345916cd2Sjpk 	    NULL;
524445916cd2Sjpk 
524545916cd2Sjpk 	aptr = attrs;
52467c478bd9Sstevel@tonic-gate 	head = item;
52477c478bd9Sstevel@tonic-gate 	for (; item != NULL; item = item->next_item) {
52487c478bd9Sstevel@tonic-gate 
52497c478bd9Sstevel@tonic-gate 		if (!(item->group == MIB2_SCTP &&
52507c478bd9Sstevel@tonic-gate 		    item->mib_id == MIB2_SCTP_CONN))
52517c478bd9Sstevel@tonic-gate 			continue;
52527c478bd9Sstevel@tonic-gate 
52537c478bd9Sstevel@tonic-gate 		for (sp = item->valp;
525445916cd2Sjpk 		    (char *)sp < (char *)item->valp + item->length;
525545916cd2Sjpk 		    /* LINTED: (note 1) */
525645916cd2Sjpk 		    sp = (mib2_sctpConnEntry_t *)((char *)sp + sctpEntrySize)) {
525745916cd2Sjpk 			attr = aptr == NULL ? NULL : *aptr++;
52587c478bd9Sstevel@tonic-gate 			if (Aflag ||
52597c478bd9Sstevel@tonic-gate 			    sp->sctpAssocState >= MIB2_SCTP_established) {
52607c478bd9Sstevel@tonic-gate 				if (first == B_TRUE) {
52617c478bd9Sstevel@tonic-gate 					(void) puts(sctp_hdr);
52627c478bd9Sstevel@tonic-gate 					(void) puts(sctp_hdr_normal);
52637c478bd9Sstevel@tonic-gate 					first = B_FALSE;
52647c478bd9Sstevel@tonic-gate 				}
526545916cd2Sjpk 				sctp_conn_report_item(head, sp, attr);
52667c478bd9Sstevel@tonic-gate 			}
52677c478bd9Sstevel@tonic-gate 		}
52687c478bd9Sstevel@tonic-gate 	}
526945916cd2Sjpk 	if (attrs != NULL)
527045916cd2Sjpk 		free(attrs);
52717c478bd9Sstevel@tonic-gate }
52727c478bd9Sstevel@tonic-gate 
52737c478bd9Sstevel@tonic-gate static char *
52747c478bd9Sstevel@tonic-gate plural(int n)
52757c478bd9Sstevel@tonic-gate {
52767c478bd9Sstevel@tonic-gate 	return (n != 1 ? "s" : "");
52777c478bd9Sstevel@tonic-gate }
52787c478bd9Sstevel@tonic-gate 
52797c478bd9Sstevel@tonic-gate static char *
52807c478bd9Sstevel@tonic-gate pluraly(int n)
52817c478bd9Sstevel@tonic-gate {
52827c478bd9Sstevel@tonic-gate 	return (n != 1 ? "ies" : "y");
52837c478bd9Sstevel@tonic-gate }
52847c478bd9Sstevel@tonic-gate 
52857c478bd9Sstevel@tonic-gate static char *
52867c478bd9Sstevel@tonic-gate plurales(int n)
52877c478bd9Sstevel@tonic-gate {
52887c478bd9Sstevel@tonic-gate 	return (n != 1 ? "es" : "");
52897c478bd9Sstevel@tonic-gate }
52907c478bd9Sstevel@tonic-gate 
52917c478bd9Sstevel@tonic-gate static char *
52927c478bd9Sstevel@tonic-gate pktscale(n)
52937c478bd9Sstevel@tonic-gate 	int n;
52947c478bd9Sstevel@tonic-gate {
52957c478bd9Sstevel@tonic-gate 	static char buf[6];
52967c478bd9Sstevel@tonic-gate 	char t;
52977c478bd9Sstevel@tonic-gate 
52987c478bd9Sstevel@tonic-gate 	if (n < 1024) {
52997c478bd9Sstevel@tonic-gate 		t = ' ';
53007c478bd9Sstevel@tonic-gate 	} else if (n < 1024 * 1024) {
53017c478bd9Sstevel@tonic-gate 		t = 'k';
53027c478bd9Sstevel@tonic-gate 		n /= 1024;
53037c478bd9Sstevel@tonic-gate 	} else if (n < 1024 * 1024 * 1024) {
53047c478bd9Sstevel@tonic-gate 		t = 'm';
53057c478bd9Sstevel@tonic-gate 		n /= 1024 * 1024;
53067c478bd9Sstevel@tonic-gate 	} else {
53077c478bd9Sstevel@tonic-gate 		t = 'g';
53087c478bd9Sstevel@tonic-gate 		n /= 1024 * 1024 * 1024;
53097c478bd9Sstevel@tonic-gate 	}
53107c478bd9Sstevel@tonic-gate 
53117c478bd9Sstevel@tonic-gate 	(void) snprintf(buf, sizeof (buf), "%4u%c", n, t);
53127c478bd9Sstevel@tonic-gate 	return (buf);
53137c478bd9Sstevel@tonic-gate }
53147c478bd9Sstevel@tonic-gate 
53157c478bd9Sstevel@tonic-gate /* --------------------- mrt_report (netstat -m) -------------------------- */
53167c478bd9Sstevel@tonic-gate 
53177c478bd9Sstevel@tonic-gate static void
53187c478bd9Sstevel@tonic-gate mrt_report(mib_item_t *item)
53197c478bd9Sstevel@tonic-gate {
53207c478bd9Sstevel@tonic-gate 	int		jtemp = 0;
53217c478bd9Sstevel@tonic-gate 	struct vifctl	*vip;
53227c478bd9Sstevel@tonic-gate 	vifi_t		vifi;
53237c478bd9Sstevel@tonic-gate 	struct mfcctl	*mfccp;
53247c478bd9Sstevel@tonic-gate 	int		numvifs = 0;
53257c478bd9Sstevel@tonic-gate 	int		nmfc = 0;
53267c478bd9Sstevel@tonic-gate 	char		abuf[MAXHOSTNAMELEN + 1];
53277c478bd9Sstevel@tonic-gate 
53287c478bd9Sstevel@tonic-gate 	if (!(family_selected(AF_INET)))
53297c478bd9Sstevel@tonic-gate 		return;
53307c478bd9Sstevel@tonic-gate 
53317c478bd9Sstevel@tonic-gate 	/* 'for' loop 1: */
53327c478bd9Sstevel@tonic-gate 	for (; item; item = item->next_item) {
53337c478bd9Sstevel@tonic-gate 		if (Dflag) {
53347c478bd9Sstevel@tonic-gate 			(void) printf("\n--- Entry %d ---\n", ++jtemp);
53357c478bd9Sstevel@tonic-gate 			(void) printf("Group = %d, mib_id = %d, "
53367c478bd9Sstevel@tonic-gate 			    "length = %d, valp = 0x%p\n",
53377c478bd9Sstevel@tonic-gate 			    item->group, item->mib_id, item->length,
53387c478bd9Sstevel@tonic-gate 			    item->valp);
53397c478bd9Sstevel@tonic-gate 		}
53407c478bd9Sstevel@tonic-gate 		if (item->group != EXPER_DVMRP)
53417c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
53427c478bd9Sstevel@tonic-gate 
53437c478bd9Sstevel@tonic-gate 		switch (item->mib_id) {
53447c478bd9Sstevel@tonic-gate 
53457c478bd9Sstevel@tonic-gate 		case EXPER_DVMRP_VIF:
53467c478bd9Sstevel@tonic-gate 			if (Dflag)
53477c478bd9Sstevel@tonic-gate 				(void) printf("%u records for ipVifTable:\n",
53487c478bd9Sstevel@tonic-gate 				    item->length/sizeof (struct vifctl));
53497c478bd9Sstevel@tonic-gate 			if (item->length/sizeof (struct vifctl) == 0) {
53507c478bd9Sstevel@tonic-gate 				(void) puts("\nVirtual Interface Table is "
53517c478bd9Sstevel@tonic-gate 				    "empty");
53527c478bd9Sstevel@tonic-gate 				break;
53537c478bd9Sstevel@tonic-gate 			}
53547c478bd9Sstevel@tonic-gate 
53557c478bd9Sstevel@tonic-gate 			(void) puts("\nVirtual Interface Table\n"
53567c478bd9Sstevel@tonic-gate 			    " Vif Threshold Rate_Limit Local-Address"
53577c478bd9Sstevel@tonic-gate 			    "   Remote-Address     Pkt_in   Pkt_out");
53587c478bd9Sstevel@tonic-gate 
53597c478bd9Sstevel@tonic-gate 			/* 'for' loop 2: */
53607c478bd9Sstevel@tonic-gate 			for (vip = (struct vifctl *)item->valp;
53617c478bd9Sstevel@tonic-gate 			    (char *)vip < (char *)item->valp + item->length;
53627c478bd9Sstevel@tonic-gate 			    /* LINTED: (note 1) */
53637c478bd9Sstevel@tonic-gate 			    vip = (struct vifctl *)((char *)vip +
53647c478bd9Sstevel@tonic-gate 			    vifctlSize)) {
53657c478bd9Sstevel@tonic-gate 				if (vip->vifc_lcl_addr.s_addr == 0)
53667c478bd9Sstevel@tonic-gate 					continue; /* 'for' loop 2 */
53677c478bd9Sstevel@tonic-gate 				/* numvifs = vip->vifc_vifi; */
53687c478bd9Sstevel@tonic-gate 
53697c478bd9Sstevel@tonic-gate 				numvifs++;
53707c478bd9Sstevel@tonic-gate 				(void) printf("  %2u       %3u       "
53717c478bd9Sstevel@tonic-gate 				    "%4u %-15.15s",
53727c478bd9Sstevel@tonic-gate 				    vip->vifc_vifi,
53737c478bd9Sstevel@tonic-gate 				    vip->vifc_threshold,
53747c478bd9Sstevel@tonic-gate 				    vip->vifc_rate_limit,
53757c478bd9Sstevel@tonic-gate 				    pr_addr(vip->vifc_lcl_addr.s_addr,
53767c478bd9Sstevel@tonic-gate 				    abuf, sizeof (abuf)));
53777c478bd9Sstevel@tonic-gate 				(void) printf(" %-15.15s  %8u  %8u\n",
53787c478bd9Sstevel@tonic-gate 				    (vip->vifc_flags & VIFF_TUNNEL) ?
53797c478bd9Sstevel@tonic-gate 				    pr_addr(vip->vifc_rmt_addr.s_addr,
53807c478bd9Sstevel@tonic-gate 				    abuf, sizeof (abuf)) : "",
53817c478bd9Sstevel@tonic-gate 				    vip->vifc_pkt_in,
53827c478bd9Sstevel@tonic-gate 				    vip->vifc_pkt_out);
53837c478bd9Sstevel@tonic-gate 			} /* 'for' loop 2 ends */
53847c478bd9Sstevel@tonic-gate 
53857c478bd9Sstevel@tonic-gate 			(void) printf("Numvifs: %d\n", numvifs);
53867c478bd9Sstevel@tonic-gate 			break;
53877c478bd9Sstevel@tonic-gate 
53887c478bd9Sstevel@tonic-gate 		case EXPER_DVMRP_MRT:
53897c478bd9Sstevel@tonic-gate 			if (Dflag)
53907c478bd9Sstevel@tonic-gate 				(void) printf("%u records for ipMfcTable:\n",
5391e11c3f44Smeem 				    item->length/sizeof (struct vifctl));
53927c478bd9Sstevel@tonic-gate 			if (item->length/sizeof (struct vifctl) == 0) {
53937c478bd9Sstevel@tonic-gate 				(void) puts("\nMulticast Forwarding Cache is "
53947c478bd9Sstevel@tonic-gate 				    "empty");
53957c478bd9Sstevel@tonic-gate 				break;
53967c478bd9Sstevel@tonic-gate 			}
53977c478bd9Sstevel@tonic-gate 
53987c478bd9Sstevel@tonic-gate 			(void) puts("\nMulticast Forwarding Cache\n"
53997c478bd9Sstevel@tonic-gate 			    "  Origin-Subnet                 Mcastgroup      "
54007c478bd9Sstevel@tonic-gate 			    "# Pkts  In-Vif  Out-vifs/Forw-ttl");
54017c478bd9Sstevel@tonic-gate 
54027c478bd9Sstevel@tonic-gate 			for (mfccp = (struct mfcctl *)item->valp;
54037c478bd9Sstevel@tonic-gate 			    (char *)mfccp < (char *)item->valp + item->length;
54047c478bd9Sstevel@tonic-gate 			    /* LINTED: (note 1) */
54057c478bd9Sstevel@tonic-gate 			    mfccp = (struct mfcctl *)((char *)mfccp +
54067c478bd9Sstevel@tonic-gate 			    mfcctlSize)) {
54077c478bd9Sstevel@tonic-gate 
54087c478bd9Sstevel@tonic-gate 				nmfc++;
54097c478bd9Sstevel@tonic-gate 				(void) printf("  %-30.15s",
54107c478bd9Sstevel@tonic-gate 				    pr_addr(mfccp->mfcc_origin.s_addr,
54117c478bd9Sstevel@tonic-gate 				    abuf, sizeof (abuf)));
54127c478bd9Sstevel@tonic-gate 				(void) printf("%-15.15s  %6s  %3u    ",
54137c478bd9Sstevel@tonic-gate 				    pr_net(mfccp->mfcc_mcastgrp.s_addr,
5414e11c3f44Smeem 				    mfccp->mfcc_mcastgrp.s_addr,
5415e11c3f44Smeem 				    abuf, sizeof (abuf)),
54167c478bd9Sstevel@tonic-gate 				    pktscale((int)mfccp->mfcc_pkt_cnt),
5417e11c3f44Smeem 				    mfccp->mfcc_parent);
54187c478bd9Sstevel@tonic-gate 
54197c478bd9Sstevel@tonic-gate 				for (vifi = 0; vifi < MAXVIFS; ++vifi) {
54207c478bd9Sstevel@tonic-gate 					if (mfccp->mfcc_ttls[vifi]) {
54217c478bd9Sstevel@tonic-gate 						(void) printf("      %u (%u)",
54227c478bd9Sstevel@tonic-gate 						    vifi,
54237c478bd9Sstevel@tonic-gate 						    mfccp->mfcc_ttls[vifi]);
54247c478bd9Sstevel@tonic-gate 					}
54257c478bd9Sstevel@tonic-gate 
54267c478bd9Sstevel@tonic-gate 				}
54277c478bd9Sstevel@tonic-gate 				(void) putchar('\n');
54287c478bd9Sstevel@tonic-gate 			}
54297c478bd9Sstevel@tonic-gate 			(void) printf("\nTotal no. of entries in cache: %d\n",
54307c478bd9Sstevel@tonic-gate 			    nmfc);
54317c478bd9Sstevel@tonic-gate 			break;
54327c478bd9Sstevel@tonic-gate 		}
54337c478bd9Sstevel@tonic-gate 	} /* 'for' loop 1 ends */
54347c478bd9Sstevel@tonic-gate 	(void) putchar('\n');
54357c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
54367c478bd9Sstevel@tonic-gate }
54377c478bd9Sstevel@tonic-gate 
54387c478bd9Sstevel@tonic-gate /*
54397c478bd9Sstevel@tonic-gate  * Get the stats for the cache named 'name'.  If prefix != 0, then
54407c478bd9Sstevel@tonic-gate  * interpret the name as a prefix, and sum up stats for all caches
54417c478bd9Sstevel@tonic-gate  * named 'name*'.
54427c478bd9Sstevel@tonic-gate  */
54437c478bd9Sstevel@tonic-gate static void
54447c478bd9Sstevel@tonic-gate kmem_cache_stats(char *title, char *name, int prefix, int64_t *total_bytes)
54457c478bd9Sstevel@tonic-gate {
54467c478bd9Sstevel@tonic-gate 	int len;
54477c478bd9Sstevel@tonic-gate 	int alloc;
54487c478bd9Sstevel@tonic-gate 	int64_t total_alloc = 0;
54497c478bd9Sstevel@tonic-gate 	int alloc_fail, total_alloc_fail = 0;
54507c478bd9Sstevel@tonic-gate 	int buf_size = 0;
54517c478bd9Sstevel@tonic-gate 	int buf_avail;
54527c478bd9Sstevel@tonic-gate 	int buf_total;
54537c478bd9Sstevel@tonic-gate 	int buf_max, total_buf_max = 0;
54547c478bd9Sstevel@tonic-gate 	int buf_inuse, total_buf_inuse = 0;
54557c478bd9Sstevel@tonic-gate 	kstat_t *ksp;
54567c478bd9Sstevel@tonic-gate 	char buf[256];
54577c478bd9Sstevel@tonic-gate 
54587c478bd9Sstevel@tonic-gate 	len = prefix ? strlen(name) : 256;
54597c478bd9Sstevel@tonic-gate 
54607c478bd9Sstevel@tonic-gate 	/* 'for' loop 1: */
54617c478bd9Sstevel@tonic-gate 	for (ksp = kc->kc_chain; ksp != NULL; ksp = ksp->ks_next) {
54627c478bd9Sstevel@tonic-gate 
54637c478bd9Sstevel@tonic-gate 		if (strcmp(ksp->ks_class, "kmem_cache") != 0)
54647c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
54657c478bd9Sstevel@tonic-gate 
54667c478bd9Sstevel@tonic-gate 		/*
54677c478bd9Sstevel@tonic-gate 		 * Hack alert: because of the way streams messages are
54687c478bd9Sstevel@tonic-gate 		 * allocated, every constructed free dblk has an associated
54697c478bd9Sstevel@tonic-gate 		 * mblk.  From the allocator's viewpoint those mblks are
54707c478bd9Sstevel@tonic-gate 		 * allocated (because they haven't been freed), but from
54717c478bd9Sstevel@tonic-gate 		 * our viewpoint they're actually free (because they're
54727c478bd9Sstevel@tonic-gate 		 * not currently in use).  To account for this caching
54737c478bd9Sstevel@tonic-gate 		 * effect we subtract the total constructed free dblks
54747c478bd9Sstevel@tonic-gate 		 * from the total allocated mblks to derive mblks in use.
54757c478bd9Sstevel@tonic-gate 		 */
54767c478bd9Sstevel@tonic-gate 		if (strcmp(name, "streams_mblk") == 0 &&
54777c478bd9Sstevel@tonic-gate 		    strncmp(ksp->ks_name, "streams_dblk", 12) == 0) {
54787c478bd9Sstevel@tonic-gate 			(void) safe_kstat_read(kc, ksp, NULL);
54797c478bd9Sstevel@tonic-gate 			total_buf_inuse -=
5480e11c3f44Smeem 			    kstat_named_value(ksp, "buf_constructed");
54817c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
54827c478bd9Sstevel@tonic-gate 		}
54837c478bd9Sstevel@tonic-gate 
54847c478bd9Sstevel@tonic-gate 		if (strncmp(ksp->ks_name, name, len) != 0)
54857c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
54867c478bd9Sstevel@tonic-gate 
54877c478bd9Sstevel@tonic-gate 		(void) safe_kstat_read(kc, ksp, NULL);
54887c478bd9Sstevel@tonic-gate 
54897c478bd9Sstevel@tonic-gate 		alloc		= kstat_named_value(ksp, "alloc");
54907c478bd9Sstevel@tonic-gate 		alloc_fail	= kstat_named_value(ksp, "alloc_fail");
54917c478bd9Sstevel@tonic-gate 		buf_size	= kstat_named_value(ksp, "buf_size");
54927c478bd9Sstevel@tonic-gate 		buf_avail	= kstat_named_value(ksp, "buf_avail");
54937c478bd9Sstevel@tonic-gate 		buf_total	= kstat_named_value(ksp, "buf_total");
54947c478bd9Sstevel@tonic-gate 		buf_max		= kstat_named_value(ksp, "buf_max");
54957c478bd9Sstevel@tonic-gate 		buf_inuse	= buf_total - buf_avail;
54967c478bd9Sstevel@tonic-gate 
54977c478bd9Sstevel@tonic-gate 		if (Vflag && prefix) {
54987c478bd9Sstevel@tonic-gate 			(void) snprintf(buf, sizeof (buf), "%s%s", title,
54997c478bd9Sstevel@tonic-gate 			    ksp->ks_name + len);
55007c478bd9Sstevel@tonic-gate 			(void) printf("    %-18s %6u %9u %11u %11u\n",
55017c478bd9Sstevel@tonic-gate 			    buf, buf_inuse, buf_max, alloc, alloc_fail);
55027c478bd9Sstevel@tonic-gate 		}
55037c478bd9Sstevel@tonic-gate 
55047c478bd9Sstevel@tonic-gate 		total_alloc		+= alloc;
55057c478bd9Sstevel@tonic-gate 		total_alloc_fail	+= alloc_fail;
55067c478bd9Sstevel@tonic-gate 		total_buf_max		+= buf_max;
55077c478bd9Sstevel@tonic-gate 		total_buf_inuse		+= buf_inuse;
55087c478bd9Sstevel@tonic-gate 		*total_bytes		+= (int64_t)buf_inuse * buf_size;
55097c478bd9Sstevel@tonic-gate 	} /* 'for' loop 1 ends */
55107c478bd9Sstevel@tonic-gate 
55117c478bd9Sstevel@tonic-gate 	if (buf_size == 0) {
55127c478bd9Sstevel@tonic-gate 		(void) printf("%-22s [couldn't find statistics for %s]\n",
5513e11c3f44Smeem 		    title, name);
55147c478bd9Sstevel@tonic-gate 		return;
55157c478bd9Sstevel@tonic-gate 	}
55167c478bd9Sstevel@tonic-gate 
55177c478bd9Sstevel@tonic-gate 	if (Vflag && prefix)
55187c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (buf), "%s_total", title);
55197c478bd9Sstevel@tonic-gate 	else
55207c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (buf), "%s", title);
55217c478bd9Sstevel@tonic-gate 
55227c478bd9Sstevel@tonic-gate 	(void) printf("%-22s %6d %9d %11lld %11d\n", buf,
5523e11c3f44Smeem 	    total_buf_inuse, total_buf_max, total_alloc, total_alloc_fail);
55247c478bd9Sstevel@tonic-gate }
55257c478bd9Sstevel@tonic-gate 
55267c478bd9Sstevel@tonic-gate static void
55277c478bd9Sstevel@tonic-gate m_report(void)
55287c478bd9Sstevel@tonic-gate {
55297c478bd9Sstevel@tonic-gate 	int64_t total_bytes = 0;
55307c478bd9Sstevel@tonic-gate 
55317c478bd9Sstevel@tonic-gate 	(void) puts("streams allocation:");
55327c478bd9Sstevel@tonic-gate 	(void) printf("%63s\n", "cumulative  allocation");
55337c478bd9Sstevel@tonic-gate 	(void) printf("%63s\n",
55347c478bd9Sstevel@tonic-gate 	    "current   maximum       total    failures");
55357c478bd9Sstevel@tonic-gate 
55367c478bd9Sstevel@tonic-gate 	kmem_cache_stats("streams",
55377c478bd9Sstevel@tonic-gate 	    "stream_head_cache", 0, &total_bytes);
55387c478bd9Sstevel@tonic-gate 	kmem_cache_stats("queues", "queue_cache", 0, &total_bytes);
55397c478bd9Sstevel@tonic-gate 	kmem_cache_stats("mblk", "streams_mblk", 0, &total_bytes);
55407c478bd9Sstevel@tonic-gate 	kmem_cache_stats("dblk", "streams_dblk", 1, &total_bytes);
55417c478bd9Sstevel@tonic-gate 	kmem_cache_stats("linkblk", "linkinfo_cache", 0, &total_bytes);
55427c478bd9Sstevel@tonic-gate 	kmem_cache_stats("syncq", "syncq_cache", 0, &total_bytes);
55437c478bd9Sstevel@tonic-gate 	kmem_cache_stats("qband", "qband_cache", 0, &total_bytes);
55447c478bd9Sstevel@tonic-gate 
55457c478bd9Sstevel@tonic-gate 	(void) printf("\n%lld Kbytes allocated for streams data\n",
5546e11c3f44Smeem 	    total_bytes / 1024);
55477c478bd9Sstevel@tonic-gate 
55487c478bd9Sstevel@tonic-gate 	(void) putchar('\n');
55497c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
55507c478bd9Sstevel@tonic-gate }
55517c478bd9Sstevel@tonic-gate 
55527c478bd9Sstevel@tonic-gate /* --------------------------------- */
55537c478bd9Sstevel@tonic-gate 
55547c478bd9Sstevel@tonic-gate /*
55557c478bd9Sstevel@tonic-gate  * Print an IPv4 address. Remove the matching part of the domain name
55567c478bd9Sstevel@tonic-gate  * from the returned name.
55577c478bd9Sstevel@tonic-gate  */
55587c478bd9Sstevel@tonic-gate static char *
55597c478bd9Sstevel@tonic-gate pr_addr(uint_t addr, char *dst, uint_t dstlen)
55607c478bd9Sstevel@tonic-gate {
55617c478bd9Sstevel@tonic-gate 	char			*cp;
55627c478bd9Sstevel@tonic-gate 	struct hostent		*hp = NULL;
55637c478bd9Sstevel@tonic-gate 	static char		domain[MAXHOSTNAMELEN + 1];
55647c478bd9Sstevel@tonic-gate 	static boolean_t	first = B_TRUE;
55657c478bd9Sstevel@tonic-gate 	int			error_num;
55667c478bd9Sstevel@tonic-gate 
55677c478bd9Sstevel@tonic-gate 	if (first) {
55687c478bd9Sstevel@tonic-gate 		first = B_FALSE;
55697c478bd9Sstevel@tonic-gate 		if (sysinfo(SI_HOSTNAME, domain, MAXHOSTNAMELEN) != -1 &&
55707c478bd9Sstevel@tonic-gate 		    (cp = strchr(domain, '.'))) {
55717c478bd9Sstevel@tonic-gate 			(void) strncpy(domain, cp + 1, sizeof (domain));
55727c478bd9Sstevel@tonic-gate 		} else
55737c478bd9Sstevel@tonic-gate 			domain[0] = 0;
55747c478bd9Sstevel@tonic-gate 	}
55757c478bd9Sstevel@tonic-gate 	cp = NULL;
55767c478bd9Sstevel@tonic-gate 	if (!Nflag) {
55777c478bd9Sstevel@tonic-gate 		hp = getipnodebyaddr((char *)&addr, sizeof (uint_t), AF_INET,
55787c478bd9Sstevel@tonic-gate 		    &error_num);
55797c478bd9Sstevel@tonic-gate 		if (hp) {
55807c478bd9Sstevel@tonic-gate 			if ((cp = strchr(hp->h_name, '.')) != NULL &&
55817c478bd9Sstevel@tonic-gate 			    strcasecmp(cp + 1, domain) == 0)
55827c478bd9Sstevel@tonic-gate 				*cp = 0;
55837c478bd9Sstevel@tonic-gate 			cp = hp->h_name;
55847c478bd9Sstevel@tonic-gate 		}
55857c478bd9Sstevel@tonic-gate 	}
55867c478bd9Sstevel@tonic-gate 	if (cp != NULL) {
55877c478bd9Sstevel@tonic-gate 		(void) strncpy(dst, cp, dstlen);
55887c478bd9Sstevel@tonic-gate 		dst[dstlen - 1] = 0;
55897c478bd9Sstevel@tonic-gate 	} else {
55907c478bd9Sstevel@tonic-gate 		(void) inet_ntop(AF_INET, (char *)&addr, dst, dstlen);
55917c478bd9Sstevel@tonic-gate 	}
55927c478bd9Sstevel@tonic-gate 	if (hp != NULL)
55937c478bd9Sstevel@tonic-gate 		freehostent(hp);
55947c478bd9Sstevel@tonic-gate 	return (dst);
55957c478bd9Sstevel@tonic-gate }
55967c478bd9Sstevel@tonic-gate 
55977c478bd9Sstevel@tonic-gate /*
55987c478bd9Sstevel@tonic-gate  * Print a non-zero IPv4 address.  Print "    --" if the address is zero.
55997c478bd9Sstevel@tonic-gate  */
56007c478bd9Sstevel@tonic-gate static char *
56017c478bd9Sstevel@tonic-gate pr_addrnz(ipaddr_t addr, char *dst, uint_t dstlen)
56027c478bd9Sstevel@tonic-gate {
56037c478bd9Sstevel@tonic-gate 	if (addr == INADDR_ANY) {
56047c478bd9Sstevel@tonic-gate 		(void) strlcpy(dst, "    --", dstlen);
56057c478bd9Sstevel@tonic-gate 		return (dst);
56067c478bd9Sstevel@tonic-gate 	}
56077c478bd9Sstevel@tonic-gate 	return (pr_addr(addr, dst, dstlen));
56087c478bd9Sstevel@tonic-gate }
56097c478bd9Sstevel@tonic-gate 
56107c478bd9Sstevel@tonic-gate /*
56117c478bd9Sstevel@tonic-gate  * Print an IPv6 address. Remove the matching part of the domain name
56127c478bd9Sstevel@tonic-gate  * from the returned name.
56137c478bd9Sstevel@tonic-gate  */
56147c478bd9Sstevel@tonic-gate static char *
56157c478bd9Sstevel@tonic-gate pr_addr6(const struct in6_addr *addr, char *dst, uint_t dstlen)
56167c478bd9Sstevel@tonic-gate {
56177c478bd9Sstevel@tonic-gate 	char			*cp;
56187c478bd9Sstevel@tonic-gate 	struct hostent		*hp = NULL;
56197c478bd9Sstevel@tonic-gate 	static char		domain[MAXHOSTNAMELEN + 1];
56207c478bd9Sstevel@tonic-gate 	static boolean_t	first = B_TRUE;
56217c478bd9Sstevel@tonic-gate 	int			error_num;
56227c478bd9Sstevel@tonic-gate 
56237c478bd9Sstevel@tonic-gate 	if (first) {
56247c478bd9Sstevel@tonic-gate 		first = B_FALSE;
56257c478bd9Sstevel@tonic-gate 		if (sysinfo(SI_HOSTNAME, domain, MAXHOSTNAMELEN) != -1 &&
56267c478bd9Sstevel@tonic-gate 		    (cp = strchr(domain, '.'))) {
56277c478bd9Sstevel@tonic-gate 			(void) strncpy(domain, cp + 1, sizeof (domain));
56287c478bd9Sstevel@tonic-gate 		} else
56297c478bd9Sstevel@tonic-gate 			domain[0] = 0;
56307c478bd9Sstevel@tonic-gate 	}
56317c478bd9Sstevel@tonic-gate 	cp = NULL;
56327c478bd9Sstevel@tonic-gate 	if (!Nflag) {
56337c478bd9Sstevel@tonic-gate 		hp = getipnodebyaddr((char *)addr,
56347c478bd9Sstevel@tonic-gate 		    sizeof (struct in6_addr), AF_INET6, &error_num);
56357c478bd9Sstevel@tonic-gate 		if (hp) {
56367c478bd9Sstevel@tonic-gate 			if ((cp = strchr(hp->h_name, '.')) != NULL &&
56377c478bd9Sstevel@tonic-gate 			    strcasecmp(cp + 1, domain) == 0)
56387c478bd9Sstevel@tonic-gate 				*cp = 0;
56397c478bd9Sstevel@tonic-gate 			cp = hp->h_name;
56407c478bd9Sstevel@tonic-gate 		}
56417c478bd9Sstevel@tonic-gate 	}
56427c478bd9Sstevel@tonic-gate 	if (cp != NULL) {
56437c478bd9Sstevel@tonic-gate 		(void) strncpy(dst, cp, dstlen);
56447c478bd9Sstevel@tonic-gate 		dst[dstlen - 1] = 0;
56457c478bd9Sstevel@tonic-gate 	} else {
56467c478bd9Sstevel@tonic-gate 		(void) inet_ntop(AF_INET6, (void *)addr, dst, dstlen);
56477c478bd9Sstevel@tonic-gate 	}
56487c478bd9Sstevel@tonic-gate 	if (hp != NULL)
56497c478bd9Sstevel@tonic-gate 		freehostent(hp);
56507c478bd9Sstevel@tonic-gate 	return (dst);
56517c478bd9Sstevel@tonic-gate }
56527c478bd9Sstevel@tonic-gate 
56537c478bd9Sstevel@tonic-gate /* For IPv4 masks */
56547c478bd9Sstevel@tonic-gate static char *
56557c478bd9Sstevel@tonic-gate pr_mask(uint_t addr, char *dst, uint_t dstlen)
56567c478bd9Sstevel@tonic-gate {
56577c478bd9Sstevel@tonic-gate 	uint8_t	*ip_addr = (uint8_t *)&addr;
56587c478bd9Sstevel@tonic-gate 
56597c478bd9Sstevel@tonic-gate 	(void) snprintf(dst, dstlen, "%d.%d.%d.%d",
56607c478bd9Sstevel@tonic-gate 	    ip_addr[0], ip_addr[1], ip_addr[2], ip_addr[3]);
56617c478bd9Sstevel@tonic-gate 	return (dst);
56627c478bd9Sstevel@tonic-gate }
56637c478bd9Sstevel@tonic-gate 
56647c478bd9Sstevel@tonic-gate /*
56657c478bd9Sstevel@tonic-gate  * For ipv6 masks format is : dest/mask
56667c478bd9Sstevel@tonic-gate  * Does not print /128 to save space in printout. H flag carries this notion.
56677c478bd9Sstevel@tonic-gate  */
56687c478bd9Sstevel@tonic-gate static char *
566945916cd2Sjpk pr_prefix6(const struct in6_addr *addr, uint_t prefixlen, char *dst,
567045916cd2Sjpk     uint_t dstlen)
56717c478bd9Sstevel@tonic-gate {
56727c478bd9Sstevel@tonic-gate 	char *cp;
56737c478bd9Sstevel@tonic-gate 
56747c478bd9Sstevel@tonic-gate 	if (IN6_IS_ADDR_UNSPECIFIED(addr) && prefixlen == 0) {
56757c478bd9Sstevel@tonic-gate 		(void) strncpy(dst, "default", dstlen);
56767c478bd9Sstevel@tonic-gate 		dst[dstlen - 1] = 0;
56777c478bd9Sstevel@tonic-gate 		return (dst);
56787c478bd9Sstevel@tonic-gate 	}
56797c478bd9Sstevel@tonic-gate 
56807c478bd9Sstevel@tonic-gate 	(void) pr_addr6(addr, dst, dstlen);
56817c478bd9Sstevel@tonic-gate 	if (prefixlen != IPV6_ABITS) {
56827c478bd9Sstevel@tonic-gate 		/* How much room is left? */
56837c478bd9Sstevel@tonic-gate 		cp = strchr(dst, '\0');
56847c478bd9Sstevel@tonic-gate 		if (dst + dstlen > cp) {
56857c478bd9Sstevel@tonic-gate 			dstlen -= (cp - dst);
56867c478bd9Sstevel@tonic-gate 			(void) snprintf(cp, dstlen, "/%d", prefixlen);
56877c478bd9Sstevel@tonic-gate 		}
56887c478bd9Sstevel@tonic-gate 	}
56897c478bd9Sstevel@tonic-gate 	return (dst);
56907c478bd9Sstevel@tonic-gate }
56917c478bd9Sstevel@tonic-gate 
56927c478bd9Sstevel@tonic-gate /* Print IPv4 address and port */
56937c478bd9Sstevel@tonic-gate static char *
56947c478bd9Sstevel@tonic-gate pr_ap(uint_t addr, uint_t port, char *proto,
56957c478bd9Sstevel@tonic-gate     char *dst, uint_t dstlen)
56967c478bd9Sstevel@tonic-gate {
56977c478bd9Sstevel@tonic-gate 	char *cp;
56987c478bd9Sstevel@tonic-gate 
56997c478bd9Sstevel@tonic-gate 	if (addr == INADDR_ANY) {
57007c478bd9Sstevel@tonic-gate 		(void) strncpy(dst, "      *", dstlen);
57017c478bd9Sstevel@tonic-gate 		dst[dstlen - 1] = 0;
57027c478bd9Sstevel@tonic-gate 	} else {
57037c478bd9Sstevel@tonic-gate 		(void) pr_addr(addr, dst, dstlen);
57047c478bd9Sstevel@tonic-gate 	}
57057c478bd9Sstevel@tonic-gate 	/* How much room is left? */
57067c478bd9Sstevel@tonic-gate 	cp = strchr(dst, '\0');
57077c478bd9Sstevel@tonic-gate 	if (dst + dstlen > cp + 1) {
57087c478bd9Sstevel@tonic-gate 		*cp++ = '.';
57097c478bd9Sstevel@tonic-gate 		dstlen -= (cp - dst);
57107c478bd9Sstevel@tonic-gate 		dstlen--;
57117c478bd9Sstevel@tonic-gate 		(void) portname(port, proto, cp, dstlen);
57127c478bd9Sstevel@tonic-gate 	}
57137c478bd9Sstevel@tonic-gate 	return (dst);
57147c478bd9Sstevel@tonic-gate }
57157c478bd9Sstevel@tonic-gate 
57167c478bd9Sstevel@tonic-gate /* Print IPv6 address and port */
57177c478bd9Sstevel@tonic-gate static char *
57187c478bd9Sstevel@tonic-gate pr_ap6(const in6_addr_t *addr, uint_t port, char *proto,
57197c478bd9Sstevel@tonic-gate     char *dst, uint_t dstlen)
57207c478bd9Sstevel@tonic-gate {
57217c478bd9Sstevel@tonic-gate 	char *cp;
57227c478bd9Sstevel@tonic-gate 
57237c478bd9Sstevel@tonic-gate 	if (IN6_IS_ADDR_UNSPECIFIED(addr)) {
57247c478bd9Sstevel@tonic-gate 		(void) strncpy(dst, "      *", dstlen);
57257c478bd9Sstevel@tonic-gate 		dst[dstlen - 1] = 0;
57267c478bd9Sstevel@tonic-gate 	} else {
57277c478bd9Sstevel@tonic-gate 		(void) pr_addr6(addr, dst, dstlen);
57287c478bd9Sstevel@tonic-gate 	}
57297c478bd9Sstevel@tonic-gate 	/* How much room is left? */
57307c478bd9Sstevel@tonic-gate 	cp = strchr(dst, '\0');
57317c478bd9Sstevel@tonic-gate 	if (dst + dstlen + 1 > cp) {
57327c478bd9Sstevel@tonic-gate 		*cp++ = '.';
57337c478bd9Sstevel@tonic-gate 		dstlen -= (cp - dst);
57347c478bd9Sstevel@tonic-gate 		dstlen--;
57357c478bd9Sstevel@tonic-gate 		(void) portname(port, proto, cp, dstlen);
57367c478bd9Sstevel@tonic-gate 	}
57377c478bd9Sstevel@tonic-gate 	return (dst);
57387c478bd9Sstevel@tonic-gate }
57397c478bd9Sstevel@tonic-gate 
57407c478bd9Sstevel@tonic-gate /*
57417c478bd9Sstevel@tonic-gate  * Return the name of the network whose address is given. The address is
57427c478bd9Sstevel@tonic-gate  * assumed to be that of a net or subnet, not a host.
57437c478bd9Sstevel@tonic-gate  */
57447c478bd9Sstevel@tonic-gate static char *
57457c478bd9Sstevel@tonic-gate pr_net(uint_t addr, uint_t mask, char *dst, uint_t dstlen)
57467c478bd9Sstevel@tonic-gate {
57477c478bd9Sstevel@tonic-gate 	char		*cp = NULL;
57487c478bd9Sstevel@tonic-gate 	struct netent	*np = NULL;
57497c478bd9Sstevel@tonic-gate 	struct hostent	*hp = NULL;
57507c478bd9Sstevel@tonic-gate 	uint_t		net;
57517c478bd9Sstevel@tonic-gate 	int		subnetshift;
57527c478bd9Sstevel@tonic-gate 	int		error_num;
57537c478bd9Sstevel@tonic-gate 
57547c478bd9Sstevel@tonic-gate 	if (addr == INADDR_ANY && mask == INADDR_ANY) {
57557c478bd9Sstevel@tonic-gate 		(void) strncpy(dst, "default", dstlen);
57567c478bd9Sstevel@tonic-gate 		dst[dstlen - 1] = 0;
57577c478bd9Sstevel@tonic-gate 		return (dst);
57587c478bd9Sstevel@tonic-gate 	}
57597c478bd9Sstevel@tonic-gate 
57607c478bd9Sstevel@tonic-gate 	if (!Nflag && addr) {
57617c478bd9Sstevel@tonic-gate 		if (mask == 0) {
57627c478bd9Sstevel@tonic-gate 			if (IN_CLASSA(addr)) {
57637c478bd9Sstevel@tonic-gate 				mask = (uint_t)IN_CLASSA_NET;
57647c478bd9Sstevel@tonic-gate 				subnetshift = 8;
57657c478bd9Sstevel@tonic-gate 			} else if (IN_CLASSB(addr)) {
57667c478bd9Sstevel@tonic-gate 				mask = (uint_t)IN_CLASSB_NET;
57677c478bd9Sstevel@tonic-gate 				subnetshift = 8;
57687c478bd9Sstevel@tonic-gate 			} else {
57697c478bd9Sstevel@tonic-gate 				mask = (uint_t)IN_CLASSC_NET;
57707c478bd9Sstevel@tonic-gate 				subnetshift = 4;
57717c478bd9Sstevel@tonic-gate 			}
57727c478bd9Sstevel@tonic-gate 			/*
57737c478bd9Sstevel@tonic-gate 			 * If there are more bits than the standard mask
57747c478bd9Sstevel@tonic-gate 			 * would suggest, subnets must be in use. Guess at
57757c478bd9Sstevel@tonic-gate 			 * the subnet mask, assuming reasonable width subnet
57767c478bd9Sstevel@tonic-gate 			 * fields.
57777c478bd9Sstevel@tonic-gate 			 */
57787c478bd9Sstevel@tonic-gate 			while (addr & ~mask)
57797c478bd9Sstevel@tonic-gate 				/* compiler doesn't sign extend! */
57807c478bd9Sstevel@tonic-gate 				mask = (mask | ((int)mask >> subnetshift));
57817c478bd9Sstevel@tonic-gate 		}
57827c478bd9Sstevel@tonic-gate 		net = addr & mask;
57837c478bd9Sstevel@tonic-gate 		while ((mask & 1) == 0)
57847c478bd9Sstevel@tonic-gate 			mask >>= 1, net >>= 1;
57857c478bd9Sstevel@tonic-gate 		np = getnetbyaddr(net, AF_INET);
57867c478bd9Sstevel@tonic-gate 		if (np && np->n_net == net)
57877c478bd9Sstevel@tonic-gate 			cp = np->n_name;
57887c478bd9Sstevel@tonic-gate 		else {
57897c478bd9Sstevel@tonic-gate 			/*
57907c478bd9Sstevel@tonic-gate 			 * Look for subnets in hosts map.
57917c478bd9Sstevel@tonic-gate 			 */
57927c478bd9Sstevel@tonic-gate 			hp = getipnodebyaddr((char *)&addr, sizeof (uint_t),
57937c478bd9Sstevel@tonic-gate 			    AF_INET, &error_num);
57947c478bd9Sstevel@tonic-gate 			if (hp)
57957c478bd9Sstevel@tonic-gate 				cp = hp->h_name;
57967c478bd9Sstevel@tonic-gate 		}
57977c478bd9Sstevel@tonic-gate 	}
57987c478bd9Sstevel@tonic-gate 	if (cp != NULL) {
57997c478bd9Sstevel@tonic-gate 		(void) strncpy(dst, cp, dstlen);
58007c478bd9Sstevel@tonic-gate 		dst[dstlen - 1] = 0;
58017c478bd9Sstevel@tonic-gate 	} else {
58027c478bd9Sstevel@tonic-gate 		(void) inet_ntop(AF_INET, (char *)&addr, dst, dstlen);
58037c478bd9Sstevel@tonic-gate 	}
58047c478bd9Sstevel@tonic-gate 	if (hp != NULL)
58057c478bd9Sstevel@tonic-gate 		freehostent(hp);
58067c478bd9Sstevel@tonic-gate 	return (dst);
58077c478bd9Sstevel@tonic-gate }
58087c478bd9Sstevel@tonic-gate 
58097c478bd9Sstevel@tonic-gate /*
58107c478bd9Sstevel@tonic-gate  * Return the name of the network whose address is given.
58117c478bd9Sstevel@tonic-gate  * The address is assumed to be a host address.
58127c478bd9Sstevel@tonic-gate  */
58137c478bd9Sstevel@tonic-gate static char *
58147c478bd9Sstevel@tonic-gate pr_netaddr(uint_t addr, uint_t mask, char *dst, uint_t dstlen)
58157c478bd9Sstevel@tonic-gate {
58167c478bd9Sstevel@tonic-gate 	char		*cp = NULL;
58177c478bd9Sstevel@tonic-gate 	struct netent	*np = NULL;
58187c478bd9Sstevel@tonic-gate 	struct hostent	*hp = NULL;
58197c478bd9Sstevel@tonic-gate 	uint_t		net;
58207c478bd9Sstevel@tonic-gate 	uint_t		netshifted;
58217c478bd9Sstevel@tonic-gate 	int		subnetshift;
58227c478bd9Sstevel@tonic-gate 	struct in_addr in;
58237c478bd9Sstevel@tonic-gate 	int		error_num;
58247c478bd9Sstevel@tonic-gate 	uint_t		nbo_addr = addr;	/* network byte order */
58257c478bd9Sstevel@tonic-gate 
58267c478bd9Sstevel@tonic-gate 	addr = ntohl(addr);
58277c478bd9Sstevel@tonic-gate 	mask = ntohl(mask);
58287c478bd9Sstevel@tonic-gate 	if (addr == INADDR_ANY && mask == INADDR_ANY) {
58297c478bd9Sstevel@tonic-gate 		(void) strncpy(dst, "default", dstlen);
58307c478bd9Sstevel@tonic-gate 		dst[dstlen - 1] = 0;
58317c478bd9Sstevel@tonic-gate 		return (dst);
58327c478bd9Sstevel@tonic-gate 	}
58337c478bd9Sstevel@tonic-gate 
58347c478bd9Sstevel@tonic-gate 	/* Figure out network portion of address (with host portion = 0) */
58357c478bd9Sstevel@tonic-gate 	if (addr) {
58367c478bd9Sstevel@tonic-gate 		/* Try figuring out mask if unknown (all 0s). */
58377c478bd9Sstevel@tonic-gate 		if (mask == 0) {
58387c478bd9Sstevel@tonic-gate 			if (IN_CLASSA(addr)) {
58397c478bd9Sstevel@tonic-gate 				mask = (uint_t)IN_CLASSA_NET;
58407c478bd9Sstevel@tonic-gate 				subnetshift = 8;
58417c478bd9Sstevel@tonic-gate 			} else if (IN_CLASSB(addr)) {
58427c478bd9Sstevel@tonic-gate 				mask = (uint_t)IN_CLASSB_NET;
58437c478bd9Sstevel@tonic-gate 				subnetshift = 8;
58447c478bd9Sstevel@tonic-gate 			} else {
58457c478bd9Sstevel@tonic-gate 				mask = (uint_t)IN_CLASSC_NET;
58467c478bd9Sstevel@tonic-gate 				subnetshift = 4;
58477c478bd9Sstevel@tonic-gate 			}
58487c478bd9Sstevel@tonic-gate 			/*
58497c478bd9Sstevel@tonic-gate 			 * If there are more bits than the standard mask
58507c478bd9Sstevel@tonic-gate 			 * would suggest, subnets must be in use. Guess at
58517c478bd9Sstevel@tonic-gate 			 * the subnet mask, assuming reasonable width subnet
58527c478bd9Sstevel@tonic-gate 			 * fields.
58537c478bd9Sstevel@tonic-gate 			 */
58547c478bd9Sstevel@tonic-gate 			while (addr & ~mask)
58557c478bd9Sstevel@tonic-gate 				/* compiler doesn't sign extend! */
58567c478bd9Sstevel@tonic-gate 				mask = (mask | ((int)mask >> subnetshift));
58577c478bd9Sstevel@tonic-gate 		}
58587c478bd9Sstevel@tonic-gate 		net = netshifted = addr & mask;
58597c478bd9Sstevel@tonic-gate 		while ((mask & 1) == 0)
58607c478bd9Sstevel@tonic-gate 			mask >>= 1, netshifted >>= 1;
58617c478bd9Sstevel@tonic-gate 	}
58627c478bd9Sstevel@tonic-gate 	else
58637c478bd9Sstevel@tonic-gate 		net = netshifted = 0;
58647c478bd9Sstevel@tonic-gate 
58657c478bd9Sstevel@tonic-gate 	/* Try looking up name unless -n was specified. */
58667c478bd9Sstevel@tonic-gate 	if (!Nflag) {
58677c478bd9Sstevel@tonic-gate 		np = getnetbyaddr(netshifted, AF_INET);
58687c478bd9Sstevel@tonic-gate 		if (np && np->n_net == netshifted)
58697c478bd9Sstevel@tonic-gate 			cp = np->n_name;
58707c478bd9Sstevel@tonic-gate 		else {
58717c478bd9Sstevel@tonic-gate 			/*
58727c478bd9Sstevel@tonic-gate 			 * Look for subnets in hosts map.
58737c478bd9Sstevel@tonic-gate 			 */
58747c478bd9Sstevel@tonic-gate 			hp = getipnodebyaddr((char *)&nbo_addr, sizeof (uint_t),
58757c478bd9Sstevel@tonic-gate 			    AF_INET, &error_num);
58767c478bd9Sstevel@tonic-gate 			if (hp)
58777c478bd9Sstevel@tonic-gate 				cp = hp->h_name;
58787c478bd9Sstevel@tonic-gate 		}
58797c478bd9Sstevel@tonic-gate 
58807c478bd9Sstevel@tonic-gate 		if (cp != NULL) {
58817c478bd9Sstevel@tonic-gate 			(void) strncpy(dst, cp, dstlen);
58827c478bd9Sstevel@tonic-gate 			dst[dstlen - 1] = 0;
58837c478bd9Sstevel@tonic-gate 			if (hp != NULL)
58847c478bd9Sstevel@tonic-gate 				freehostent(hp);
58857c478bd9Sstevel@tonic-gate 			return (dst);
58867c478bd9Sstevel@tonic-gate 		}
58877c478bd9Sstevel@tonic-gate 		/*
58887c478bd9Sstevel@tonic-gate 		 * No name found for net: fallthru and return in decimal
58897c478bd9Sstevel@tonic-gate 		 * dot notation.
58907c478bd9Sstevel@tonic-gate 		 */
58917c478bd9Sstevel@tonic-gate 	}
58927c478bd9Sstevel@tonic-gate 
58937c478bd9Sstevel@tonic-gate 	in.s_addr = htonl(net);
58947c478bd9Sstevel@tonic-gate 	(void) inet_ntop(AF_INET, (char *)&in, dst, dstlen);
58957c478bd9Sstevel@tonic-gate 	if (hp != NULL)
58967c478bd9Sstevel@tonic-gate 		freehostent(hp);
58977c478bd9Sstevel@tonic-gate 	return (dst);
58987c478bd9Sstevel@tonic-gate }
58997c478bd9Sstevel@tonic-gate 
59007c478bd9Sstevel@tonic-gate /*
59017c478bd9Sstevel@tonic-gate  * Return the filter mode as a string:
59027c478bd9Sstevel@tonic-gate  *	1 => "INCLUDE"
59037c478bd9Sstevel@tonic-gate  *	2 => "EXCLUDE"
59047c478bd9Sstevel@tonic-gate  *	otherwise "<unknown>"
59057c478bd9Sstevel@tonic-gate  */
59067c478bd9Sstevel@tonic-gate static char *
59077c478bd9Sstevel@tonic-gate fmodestr(uint_t fmode)
59087c478bd9Sstevel@tonic-gate {
59097c478bd9Sstevel@tonic-gate 	switch (fmode) {
59107c478bd9Sstevel@tonic-gate 	case 1:
59117c478bd9Sstevel@tonic-gate 		return ("INCLUDE");
59127c478bd9Sstevel@tonic-gate 	case 2:
59137c478bd9Sstevel@tonic-gate 		return ("EXCLUDE");
59147c478bd9Sstevel@tonic-gate 	default:
59157c478bd9Sstevel@tonic-gate 		return ("<unknown>");
59167c478bd9Sstevel@tonic-gate 	}
59177c478bd9Sstevel@tonic-gate }
59187c478bd9Sstevel@tonic-gate 
591945916cd2Sjpk #define	MAX_STRING_SIZE	256
592045916cd2Sjpk 
592145916cd2Sjpk static const char *
592245916cd2Sjpk pr_secattr(const sec_attr_list_t *attrs)
592345916cd2Sjpk {
592445916cd2Sjpk 	int i;
592545916cd2Sjpk 	char buf[MAX_STRING_SIZE + 1], *cp;
592645916cd2Sjpk 	static char *sbuf;
592745916cd2Sjpk 	static size_t sbuf_len;
592845916cd2Sjpk 	struct rtsa_s rtsa;
592945916cd2Sjpk 	const sec_attr_list_t *aptr;
593045916cd2Sjpk 
593145916cd2Sjpk 	if (!RSECflag || attrs == NULL)
593245916cd2Sjpk 		return ("");
593345916cd2Sjpk 
593445916cd2Sjpk 	for (aptr = attrs, i = 1; aptr != NULL; aptr = aptr->sal_next)
593545916cd2Sjpk 		i += MAX_STRING_SIZE;
593645916cd2Sjpk 	if (i > sbuf_len) {
593745916cd2Sjpk 		cp = realloc(sbuf, i);
593845916cd2Sjpk 		if (cp == NULL) {
593945916cd2Sjpk 			perror("realloc security attribute buffer");
594045916cd2Sjpk 			return ("");
594145916cd2Sjpk 		}
594245916cd2Sjpk 		sbuf_len = i;
594345916cd2Sjpk 		sbuf = cp;
594445916cd2Sjpk 	}
594545916cd2Sjpk 
594645916cd2Sjpk 	cp = sbuf;
594745916cd2Sjpk 	while (attrs != NULL) {
594845916cd2Sjpk 		const mib2_ipAttributeEntry_t *iae = attrs->sal_attr;
594945916cd2Sjpk 
595045916cd2Sjpk 		/* note: effectively hard-coded in rtsa_keyword */
595145916cd2Sjpk 		rtsa.rtsa_mask = RTSA_CIPSO | RTSA_SLRANGE | RTSA_DOI;
595245916cd2Sjpk 		rtsa.rtsa_slrange = iae->iae_slrange;
595345916cd2Sjpk 		rtsa.rtsa_doi = iae->iae_doi;
595445916cd2Sjpk 
595545916cd2Sjpk 		(void) snprintf(cp, MAX_STRING_SIZE,
595645916cd2Sjpk 		    "<%s>%s ", rtsa_to_str(&rtsa, buf, sizeof (buf)),
595745916cd2Sjpk 		    attrs->sal_next == NULL ? "" : ",");
595845916cd2Sjpk 		cp += strlen(cp);
595945916cd2Sjpk 		attrs = attrs->sal_next;
596045916cd2Sjpk 	}
596145916cd2Sjpk 	*cp = '\0';
596245916cd2Sjpk 
596345916cd2Sjpk 	return (sbuf);
596445916cd2Sjpk }
596545916cd2Sjpk 
59667c478bd9Sstevel@tonic-gate /*
59677c478bd9Sstevel@tonic-gate  * Pretty print a port number. If the Nflag was
59687c478bd9Sstevel@tonic-gate  * specified, use numbers instead of names.
59697c478bd9Sstevel@tonic-gate  */
59707c478bd9Sstevel@tonic-gate static char *
59717c478bd9Sstevel@tonic-gate portname(uint_t port, char *proto, char *dst, uint_t dstlen)
59727c478bd9Sstevel@tonic-gate {
59737c478bd9Sstevel@tonic-gate 	struct servent *sp = NULL;
59747c478bd9Sstevel@tonic-gate 
59757c478bd9Sstevel@tonic-gate 	if (!Nflag && port)
59767c478bd9Sstevel@tonic-gate 		sp = getservbyport(htons(port), proto);
59777c478bd9Sstevel@tonic-gate 	if (sp || port == 0)
59787c478bd9Sstevel@tonic-gate 		(void) snprintf(dst, dstlen, "%.*s", MAXHOSTNAMELEN,
5979e11c3f44Smeem 		    sp ? sp->s_name : "*");
59807c478bd9Sstevel@tonic-gate 	else
59817c478bd9Sstevel@tonic-gate 		(void) snprintf(dst, dstlen, "%d", port);
59827c478bd9Sstevel@tonic-gate 	dst[dstlen - 1] = 0;
59837c478bd9Sstevel@tonic-gate 	return (dst);
59847c478bd9Sstevel@tonic-gate }
59857c478bd9Sstevel@tonic-gate 
59867c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/
59877c478bd9Sstevel@tonic-gate void
59887c478bd9Sstevel@tonic-gate fail(int do_perror, char *message, ...)
59897c478bd9Sstevel@tonic-gate {
59907c478bd9Sstevel@tonic-gate 	va_list args;
59917c478bd9Sstevel@tonic-gate 
59927c478bd9Sstevel@tonic-gate 	va_start(args, message);
59937c478bd9Sstevel@tonic-gate 	(void) fputs("netstat: ", stderr);
59947c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, message, args);
59957c478bd9Sstevel@tonic-gate 	va_end(args);
59967c478bd9Sstevel@tonic-gate 	if (do_perror)
59977c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, ": %s", strerror(errno));
59987c478bd9Sstevel@tonic-gate 	(void) fputc('\n', stderr);
59997c478bd9Sstevel@tonic-gate 	exit(2);
60007c478bd9Sstevel@tonic-gate }
60017c478bd9Sstevel@tonic-gate 
60027c478bd9Sstevel@tonic-gate /*
60037c478bd9Sstevel@tonic-gate  * Return value of named statistic for given kstat_named kstat;
60047c478bd9Sstevel@tonic-gate  * return 0LL if named statistic is not in list (use "ll" as a
60057c478bd9Sstevel@tonic-gate  * type qualifier when printing 64-bit int's with printf() )
60067c478bd9Sstevel@tonic-gate  */
60077c478bd9Sstevel@tonic-gate static uint64_t
60087c478bd9Sstevel@tonic-gate kstat_named_value(kstat_t *ksp, char *name)
60097c478bd9Sstevel@tonic-gate {
60107c478bd9Sstevel@tonic-gate 	kstat_named_t *knp;
60117c478bd9Sstevel@tonic-gate 	uint64_t value;
60127c478bd9Sstevel@tonic-gate 
60137c478bd9Sstevel@tonic-gate 	if (ksp == NULL)
60147c478bd9Sstevel@tonic-gate 		return (0LL);
60157c478bd9Sstevel@tonic-gate 
60167c478bd9Sstevel@tonic-gate 	knp = kstat_data_lookup(ksp, name);
60177c478bd9Sstevel@tonic-gate 	if (knp == NULL)
60187c478bd9Sstevel@tonic-gate 		return (0LL);
60197c478bd9Sstevel@tonic-gate 
60207c478bd9Sstevel@tonic-gate 	switch (knp->data_type) {
60217c478bd9Sstevel@tonic-gate 	case KSTAT_DATA_INT32:
60227c478bd9Sstevel@tonic-gate 	case KSTAT_DATA_UINT32:
60237c478bd9Sstevel@tonic-gate 		value = (uint64_t)(knp->value.ui32);
60247c478bd9Sstevel@tonic-gate 		break;
60257c478bd9Sstevel@tonic-gate 	case KSTAT_DATA_INT64:
60267c478bd9Sstevel@tonic-gate 	case KSTAT_DATA_UINT64:
60277c478bd9Sstevel@tonic-gate 		value = knp->value.ui64;
60287c478bd9Sstevel@tonic-gate 		break;
60297c478bd9Sstevel@tonic-gate 	default:
60307c478bd9Sstevel@tonic-gate 		value = 0LL;
60317c478bd9Sstevel@tonic-gate 		break;
60327c478bd9Sstevel@tonic-gate 	}
60337c478bd9Sstevel@tonic-gate 
60347c478bd9Sstevel@tonic-gate 	return (value);
60357c478bd9Sstevel@tonic-gate }
60367c478bd9Sstevel@tonic-gate 
60377c478bd9Sstevel@tonic-gate kid_t
60387c478bd9Sstevel@tonic-gate safe_kstat_read(kstat_ctl_t *kc, kstat_t *ksp, void *data)
60397c478bd9Sstevel@tonic-gate {
60407c478bd9Sstevel@tonic-gate 	kid_t kstat_chain_id = kstat_read(kc, ksp, data);
60417c478bd9Sstevel@tonic-gate 
60427c478bd9Sstevel@tonic-gate 	if (kstat_chain_id == -1)
60437c478bd9Sstevel@tonic-gate 		fail(1, "kstat_read(%p, '%s') failed", (void *)kc,
60447c478bd9Sstevel@tonic-gate 		    ksp->ks_name);
60457c478bd9Sstevel@tonic-gate 	return (kstat_chain_id);
60467c478bd9Sstevel@tonic-gate }
60477c478bd9Sstevel@tonic-gate 
60487c478bd9Sstevel@tonic-gate /*
60497c478bd9Sstevel@tonic-gate  * Parse a list of IRE flag characters into a bit field.
60507c478bd9Sstevel@tonic-gate  */
60517c478bd9Sstevel@tonic-gate static uint_t
60527c478bd9Sstevel@tonic-gate flag_bits(const char *arg)
60537c478bd9Sstevel@tonic-gate {
60547c478bd9Sstevel@tonic-gate 	const char *cp;
60557c478bd9Sstevel@tonic-gate 	uint_t val;
60567c478bd9Sstevel@tonic-gate 
60577c478bd9Sstevel@tonic-gate 	if (*arg == '\0')
60587c478bd9Sstevel@tonic-gate 		fatal(1, "missing flag list\n");
60597c478bd9Sstevel@tonic-gate 
60607c478bd9Sstevel@tonic-gate 	val = 0;
60617c478bd9Sstevel@tonic-gate 	while (*arg != '\0') {
60627c478bd9Sstevel@tonic-gate 		if ((cp = strchr(flag_list, *arg)) == NULL)
60637c478bd9Sstevel@tonic-gate 			fatal(1, "%c: illegal flag\n", *arg);
60647c478bd9Sstevel@tonic-gate 		val |= 1 << (cp - flag_list);
60657c478bd9Sstevel@tonic-gate 		arg++;
60667c478bd9Sstevel@tonic-gate 	}
60677c478bd9Sstevel@tonic-gate 	return (val);
60687c478bd9Sstevel@tonic-gate }
60697c478bd9Sstevel@tonic-gate 
60707c478bd9Sstevel@tonic-gate /*
60717c478bd9Sstevel@tonic-gate  * Handle -f argument.  Validate input format, sort by keyword, and
60727c478bd9Sstevel@tonic-gate  * save off digested results.
60737c478bd9Sstevel@tonic-gate  */
60747c478bd9Sstevel@tonic-gate static void
60757c478bd9Sstevel@tonic-gate process_filter(char *arg)
60767c478bd9Sstevel@tonic-gate {
60777c478bd9Sstevel@tonic-gate 	int idx;
60787c478bd9Sstevel@tonic-gate 	int klen = 0;
60797c478bd9Sstevel@tonic-gate 	char *cp, *cp2;
60807c478bd9Sstevel@tonic-gate 	int val;
60817c478bd9Sstevel@tonic-gate 	filter_t *newf;
60827c478bd9Sstevel@tonic-gate 	struct hostent *hp;
60837c478bd9Sstevel@tonic-gate 	int error_num;
60847c478bd9Sstevel@tonic-gate 	uint8_t *ucp;
60857c478bd9Sstevel@tonic-gate 	int maxv;
60867c478bd9Sstevel@tonic-gate 
60877c478bd9Sstevel@tonic-gate 	/* Look up the keyword first */
60887c478bd9Sstevel@tonic-gate 	if (strchr(arg, ':') == NULL) {
60897c478bd9Sstevel@tonic-gate 		idx = FK_AF;
60907c478bd9Sstevel@tonic-gate 	} else {
60917c478bd9Sstevel@tonic-gate 		for (idx = 0; idx < NFILTERKEYS; idx++) {
60927c478bd9Sstevel@tonic-gate 			klen = strlen(filter_keys[idx]);
60937c478bd9Sstevel@tonic-gate 			if (strncmp(filter_keys[idx], arg, klen) == 0 &&
60947c478bd9Sstevel@tonic-gate 			    arg[klen] == ':')
60957c478bd9Sstevel@tonic-gate 				break;
60967c478bd9Sstevel@tonic-gate 		}
60977c478bd9Sstevel@tonic-gate 		if (idx >= NFILTERKEYS)
60987c478bd9Sstevel@tonic-gate 			fatal(1, "%s: unknown filter keyword\n", arg);
60997c478bd9Sstevel@tonic-gate 
61007c478bd9Sstevel@tonic-gate 		/* Advance past keyword and separator. */
61017c478bd9Sstevel@tonic-gate 		arg += klen + 1;
61027c478bd9Sstevel@tonic-gate 	}
61037c478bd9Sstevel@tonic-gate 
61047c478bd9Sstevel@tonic-gate 	if ((newf = malloc(sizeof (*newf))) == NULL) {
61057c478bd9Sstevel@tonic-gate 		perror("filter");
61067c478bd9Sstevel@tonic-gate 		exit(1);
61077c478bd9Sstevel@tonic-gate 	}
61087c478bd9Sstevel@tonic-gate 	switch (idx) {
61097c478bd9Sstevel@tonic-gate 	case FK_AF:
61107c478bd9Sstevel@tonic-gate 		if (strcmp(arg, "inet") == 0) {
61117c478bd9Sstevel@tonic-gate 			newf->u.f_family = AF_INET;
61127c478bd9Sstevel@tonic-gate 		} else if (strcmp(arg, "inet6") == 0) {
61137c478bd9Sstevel@tonic-gate 			newf->u.f_family = AF_INET6;
61147c478bd9Sstevel@tonic-gate 		} else if (strcmp(arg, "unix") == 0) {
61157c478bd9Sstevel@tonic-gate 			newf->u.f_family = AF_UNIX;
61167c478bd9Sstevel@tonic-gate 		} else {
61177c478bd9Sstevel@tonic-gate 			newf->u.f_family = strtol(arg, &cp, 0);
61187c478bd9Sstevel@tonic-gate 			if (arg == cp || *cp != '\0')
61197c478bd9Sstevel@tonic-gate 				fatal(1, "%s: unknown address family.\n", arg);
61207c478bd9Sstevel@tonic-gate 		}
61217c478bd9Sstevel@tonic-gate 		break;
61227c478bd9Sstevel@tonic-gate 
61237c478bd9Sstevel@tonic-gate 	case FK_OUTIF:
61247c478bd9Sstevel@tonic-gate 		if (strcmp(arg, "none") == 0) {
61257c478bd9Sstevel@tonic-gate 			newf->u.f_ifname = NULL;
61267c478bd9Sstevel@tonic-gate 			break;
61277c478bd9Sstevel@tonic-gate 		}
61287c478bd9Sstevel@tonic-gate 		if (strcmp(arg, "any") == 0) {
61297c478bd9Sstevel@tonic-gate 			newf->u.f_ifname = "";
61307c478bd9Sstevel@tonic-gate 			break;
61317c478bd9Sstevel@tonic-gate 		}
61327c478bd9Sstevel@tonic-gate 		val = strtol(arg, &cp, 0);
61337c478bd9Sstevel@tonic-gate 		if (val <= 0 || arg == cp || cp[0] != '\0') {
61347c478bd9Sstevel@tonic-gate 			if ((val = if_nametoindex(arg)) == 0) {
61357c478bd9Sstevel@tonic-gate 				perror(arg);
61367c478bd9Sstevel@tonic-gate 				exit(1);
61377c478bd9Sstevel@tonic-gate 			}
61387c478bd9Sstevel@tonic-gate 		}
61397c478bd9Sstevel@tonic-gate 		newf->u.f_ifname = arg;
61407c478bd9Sstevel@tonic-gate 		break;
61417c478bd9Sstevel@tonic-gate 
61427c478bd9Sstevel@tonic-gate 	case FK_DST:
61437c478bd9Sstevel@tonic-gate 		V4MASK_TO_V6(IP_HOST_MASK, newf->u.a.f_mask);
61447c478bd9Sstevel@tonic-gate 		if (strcmp(arg, "any") == 0) {
61457c478bd9Sstevel@tonic-gate 			/* Special semantics; any address *but* zero */
61467c478bd9Sstevel@tonic-gate 			newf->u.a.f_address = NULL;
61477c478bd9Sstevel@tonic-gate 			(void) memset(&newf->u.a.f_mask, 0,
61487c478bd9Sstevel@tonic-gate 			    sizeof (newf->u.a.f_mask));
61497c478bd9Sstevel@tonic-gate 			break;
61507c478bd9Sstevel@tonic-gate 		}
61517c478bd9Sstevel@tonic-gate 		if (strcmp(arg, "none") == 0) {
61527c478bd9Sstevel@tonic-gate 			newf->u.a.f_address = NULL;
61537c478bd9Sstevel@tonic-gate 			break;
61547c478bd9Sstevel@tonic-gate 		}
61557c478bd9Sstevel@tonic-gate 		if ((cp = strrchr(arg, '/')) != NULL)
61567c478bd9Sstevel@tonic-gate 			*cp++ = '\0';
61577c478bd9Sstevel@tonic-gate 		hp = getipnodebyname(arg, AF_INET6, AI_V4MAPPED|AI_ALL,
61587c478bd9Sstevel@tonic-gate 		    &error_num);
61597c478bd9Sstevel@tonic-gate 		if (hp == NULL)
61607c478bd9Sstevel@tonic-gate 			fatal(1, "%s: invalid or unknown host address\n", arg);
61617c478bd9Sstevel@tonic-gate 		newf->u.a.f_address = hp;
61627c478bd9Sstevel@tonic-gate 		if (cp == NULL) {
61637c478bd9Sstevel@tonic-gate 			V4MASK_TO_V6(IP_HOST_MASK, newf->u.a.f_mask);
61647c478bd9Sstevel@tonic-gate 		} else {
61657c478bd9Sstevel@tonic-gate 			val = strtol(cp, &cp2, 0);
61667c478bd9Sstevel@tonic-gate 			if (cp != cp2 && cp2[0] == '\0') {
61677c478bd9Sstevel@tonic-gate 				/*
61687c478bd9Sstevel@tonic-gate 				 * If decode as "/n" works, then translate
61697c478bd9Sstevel@tonic-gate 				 * into a mask.
61707c478bd9Sstevel@tonic-gate 				 */
61717c478bd9Sstevel@tonic-gate 				if (hp->h_addr_list[0] != NULL &&
61727c478bd9Sstevel@tonic-gate 				    /* LINTED: (note 1) */
6173e11c3f44Smeem 				    IN6_IS_ADDR_V4MAPPED((in6_addr_t *)
6174e11c3f44Smeem 				    hp->h_addr_list[0])) {
61757c478bd9Sstevel@tonic-gate 					maxv = IP_ABITS;
61767c478bd9Sstevel@tonic-gate 				} else {
61777c478bd9Sstevel@tonic-gate 					maxv = IPV6_ABITS;
61787c478bd9Sstevel@tonic-gate 				}
61797c478bd9Sstevel@tonic-gate 				if (val < 0 || val >= maxv)
61807c478bd9Sstevel@tonic-gate 					fatal(1, "%d: not in range 0 to %d\n",
61817c478bd9Sstevel@tonic-gate 					    val, maxv - 1);
61827c478bd9Sstevel@tonic-gate 				if (maxv == IP_ABITS)
61837c478bd9Sstevel@tonic-gate 					val += IPV6_ABITS - IP_ABITS;
61847c478bd9Sstevel@tonic-gate 				ucp = newf->u.a.f_mask.s6_addr;
61857c478bd9Sstevel@tonic-gate 				while (val >= 8)
61867c478bd9Sstevel@tonic-gate 					*ucp++ = 0xff, val -= 8;
61877c478bd9Sstevel@tonic-gate 				*ucp++ = (0xff << (8 - val)) & 0xff;
61887c478bd9Sstevel@tonic-gate 				while (ucp < newf->u.a.f_mask.s6_addr +
61897c478bd9Sstevel@tonic-gate 				    sizeof (newf->u.a.f_mask.s6_addr))
61907c478bd9Sstevel@tonic-gate 					*ucp++ = 0;
61917c478bd9Sstevel@tonic-gate 				/* Otherwise, try as numeric address */
61927c478bd9Sstevel@tonic-gate 			} else if (inet_pton(AF_INET6,
61937c478bd9Sstevel@tonic-gate 			    cp, &newf->u.a.f_mask) <= 0) {
61947c478bd9Sstevel@tonic-gate 				fatal(1, "%s: illegal mask format\n", cp);
61957c478bd9Sstevel@tonic-gate 			}
61967c478bd9Sstevel@tonic-gate 		}
61977c478bd9Sstevel@tonic-gate 		break;
61987c478bd9Sstevel@tonic-gate 
61997c478bd9Sstevel@tonic-gate 	case FK_FLAGS:
62007c478bd9Sstevel@tonic-gate 		if (*arg == '+') {
62017c478bd9Sstevel@tonic-gate 			newf->u.f.f_flagset = flag_bits(arg + 1);
62027c478bd9Sstevel@tonic-gate 			newf->u.f.f_flagclear = 0;
62037c478bd9Sstevel@tonic-gate 		} else if (*arg == '-') {
62047c478bd9Sstevel@tonic-gate 			newf->u.f.f_flagset = 0;
62057c478bd9Sstevel@tonic-gate 			newf->u.f.f_flagclear = flag_bits(arg + 1);
62067c478bd9Sstevel@tonic-gate 		} else {
62077c478bd9Sstevel@tonic-gate 			newf->u.f.f_flagset = flag_bits(arg);
62087c478bd9Sstevel@tonic-gate 			newf->u.f.f_flagclear = ~newf->u.f.f_flagset;
62097c478bd9Sstevel@tonic-gate 		}
62107c478bd9Sstevel@tonic-gate 		break;
62117c478bd9Sstevel@tonic-gate 
62127c478bd9Sstevel@tonic-gate 	default:
62137c478bd9Sstevel@tonic-gate 		assert(0);
62147c478bd9Sstevel@tonic-gate 	}
62157c478bd9Sstevel@tonic-gate 	newf->f_next = filters[idx];
62167c478bd9Sstevel@tonic-gate 	filters[idx] = newf;
62177c478bd9Sstevel@tonic-gate }
62187c478bd9Sstevel@tonic-gate 
62197c478bd9Sstevel@tonic-gate /* Determine if user wants this address family printed. */
62207c478bd9Sstevel@tonic-gate static boolean_t
62217c478bd9Sstevel@tonic-gate family_selected(int family)
62227c478bd9Sstevel@tonic-gate {
62237c478bd9Sstevel@tonic-gate 	const filter_t *fp;
62247c478bd9Sstevel@tonic-gate 
62257c478bd9Sstevel@tonic-gate 	if (v4compat && family == AF_INET6)
62267c478bd9Sstevel@tonic-gate 		return (B_FALSE);
62277c478bd9Sstevel@tonic-gate 	if ((fp = filters[FK_AF]) == NULL)
62287c478bd9Sstevel@tonic-gate 		return (B_TRUE);
62297c478bd9Sstevel@tonic-gate 	while (fp != NULL) {
62307c478bd9Sstevel@tonic-gate 		if (fp->u.f_family == family)
62317c478bd9Sstevel@tonic-gate 			return (B_TRUE);
62327c478bd9Sstevel@tonic-gate 		fp = fp->f_next;
62337c478bd9Sstevel@tonic-gate 	}
62347c478bd9Sstevel@tonic-gate 	return (B_FALSE);
62357c478bd9Sstevel@tonic-gate }
62367c478bd9Sstevel@tonic-gate 
6237e11c3f44Smeem /*
6238e11c3f44Smeem  * Convert the interface index to a string using the buffer `ifname', which
6239e11c3f44Smeem  * must be at least LIFNAMSIZ bytes.  We first try to map it to name.  If that
6240e11c3f44Smeem  * fails (e.g., because we're inside a zone and it does not have access to
6241e11c3f44Smeem  * interface for the index in question), just return "if#<num>".
6242e11c3f44Smeem  */
6243e11c3f44Smeem static char *
6244e11c3f44Smeem ifindex2str(uint_t ifindex, char *ifname)
6245e11c3f44Smeem {
6246e11c3f44Smeem 	if (if_indextoname(ifindex, ifname) == NULL)
6247e11c3f44Smeem 		(void) snprintf(ifname, LIFNAMSIZ, "if#%d", ifindex);
6248e11c3f44Smeem 
6249e11c3f44Smeem 	return (ifname);
6250e11c3f44Smeem }
6251e11c3f44Smeem 
62527c478bd9Sstevel@tonic-gate /*
62537c478bd9Sstevel@tonic-gate  * print the usage line
62547c478bd9Sstevel@tonic-gate  */
62557c478bd9Sstevel@tonic-gate static void
62567c478bd9Sstevel@tonic-gate usage(char *cmdname)
62577c478bd9Sstevel@tonic-gate {
6258*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 	(void) fprintf(stderr, "usage: %s [-anv] [-f address_family] "
6259*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 	    "[-T d|u]\n", cmdname);
62607c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "       %s [-n] [-f address_family] "
6261*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 	    "[-P protocol] [-T d|u] [-g | -p | -s [interval [count]]]\n",
6262*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 	    cmdname);
6263*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 	(void) fprintf(stderr, "       %s -m [-v] [-T d|u] "
62647c478bd9Sstevel@tonic-gate 	    "[interval [count]]\n", cmdname);
62657c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "       %s -i [-I interface] [-an] "
6266*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 	    "[-f address_family] [-T d|u] [interval [count]]\n", cmdname);
62677c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "       %s -r [-anv] "
6268*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 	    "[-f address_family|filter] [-T d|u]\n", cmdname);
6269*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 	(void) fprintf(stderr, "       %s -M [-ns] [-f address_family] "
6270*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 	    "[-T d|u]\n", cmdname);
62717c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "       %s -D [-I interface] "
6272*26fd7700SKrishnendu Sadhukhan - Sun Microsystems 	    "[-f address_family] [-T d|u]\n", cmdname);
62737c478bd9Sstevel@tonic-gate 	exit(EXIT_FAILURE);
62747c478bd9Sstevel@tonic-gate }
62757c478bd9Sstevel@tonic-gate 
62767c478bd9Sstevel@tonic-gate /*
62777c478bd9Sstevel@tonic-gate  * fatal: print error message to stderr and
62787c478bd9Sstevel@tonic-gate  * call exit(errcode)
62797c478bd9Sstevel@tonic-gate  */
62807c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/
62817c478bd9Sstevel@tonic-gate static void
628245916cd2Sjpk fatal(int errcode, char *format, ...)
628345916cd2Sjpk {
62847c478bd9Sstevel@tonic-gate 	va_list argp;
62857c478bd9Sstevel@tonic-gate 
62867c478bd9Sstevel@tonic-gate 	if (format == NULL)
62877c478bd9Sstevel@tonic-gate 		return;
62887c478bd9Sstevel@tonic-gate 
62897c478bd9Sstevel@tonic-gate 	va_start(argp, format);
62907c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, format, argp);
62917c478bd9Sstevel@tonic-gate 	va_end(argp);
62927c478bd9Sstevel@tonic-gate 
62937c478bd9Sstevel@tonic-gate 	exit(errcode);
62947c478bd9Sstevel@tonic-gate }
6295