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 /*
22*550b6e40SSowmini Varadhan  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
237c478bd9Sstevel@tonic-gate  * Copyright (c) 1990  Mentat Inc.
247c478bd9Sstevel@tonic-gate  * netstat.c 2.2, last change 9/9/91
257c478bd9Sstevel@tonic-gate  * MROUTING Revision 3.5
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * simple netstat based on snmp/mib-2 interface to the TCP/IP stack
307c478bd9Sstevel@tonic-gate  *
317c478bd9Sstevel@tonic-gate  * NOTES:
327c478bd9Sstevel@tonic-gate  * 1. A comment "LINTED: (note 1)" appears before certain lines where
337c478bd9Sstevel@tonic-gate  *    lint would have complained, "pointer cast may result in improper
347c478bd9Sstevel@tonic-gate  *    alignment". These are lines where lint had suspected potential
357c478bd9Sstevel@tonic-gate  *    improper alignment of a data structure; in each such situation
367c478bd9Sstevel@tonic-gate  *    we have relied on the kernel guaranteeing proper alignment.
377c478bd9Sstevel@tonic-gate  * 2. Some 'for' loops have been commented as "'for' loop 1", etc
387c478bd9Sstevel@tonic-gate  *    because they have 'continue' or 'break' statements in their
397c478bd9Sstevel@tonic-gate  *    bodies. 'continue' statements have been used inside some loops
407c478bd9Sstevel@tonic-gate  *    where avoiding them would have led to deep levels of indentation.
417c478bd9Sstevel@tonic-gate  *
427c478bd9Sstevel@tonic-gate  * TODO:
437c478bd9Sstevel@tonic-gate  *	Add ability to request subsets from kernel (with level = MIB2_IP;
447c478bd9Sstevel@tonic-gate  *	name = 0 meaning everything for compatibility)
457c478bd9Sstevel@tonic-gate  */
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate #include <stdio.h>
487c478bd9Sstevel@tonic-gate #include <stdlib.h>
497c478bd9Sstevel@tonic-gate #include <stdarg.h>
507c478bd9Sstevel@tonic-gate #include <unistd.h>
517c478bd9Sstevel@tonic-gate #include <strings.h>
527c478bd9Sstevel@tonic-gate #include <string.h>
537c478bd9Sstevel@tonic-gate #include <errno.h>
547c478bd9Sstevel@tonic-gate #include <ctype.h>
557c478bd9Sstevel@tonic-gate #include <kstat.h>
567c478bd9Sstevel@tonic-gate #include <assert.h>
5726fd7700SKrishnendu Sadhukhan - Sun Microsystems #include <locale.h>
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate #include <sys/types.h>
607c478bd9Sstevel@tonic-gate #include <sys/stream.h>
617c478bd9Sstevel@tonic-gate #include <stropts.h>
627c478bd9Sstevel@tonic-gate #include <sys/strstat.h>
637c478bd9Sstevel@tonic-gate #include <sys/tihdr.h>
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate #include <sys/socket.h>
667c478bd9Sstevel@tonic-gate #include <sys/sockio.h>
677c478bd9Sstevel@tonic-gate #include <netinet/in.h>
687c478bd9Sstevel@tonic-gate #include <net/if.h>
697c478bd9Sstevel@tonic-gate #include <net/route.h>
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate #include <inet/mib2.h>
727c478bd9Sstevel@tonic-gate #include <inet/ip.h>
737c478bd9Sstevel@tonic-gate #include <inet/arp.h>
747c478bd9Sstevel@tonic-gate #include <inet/tcp.h>
757c478bd9Sstevel@tonic-gate #include <netinet/igmp_var.h>
767c478bd9Sstevel@tonic-gate #include <netinet/ip_mroute.h>
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
797c478bd9Sstevel@tonic-gate #include <netdb.h>
807c478bd9Sstevel@tonic-gate #include <fcntl.h>
817c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
827c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate #include <netinet/dhcp.h>
857c478bd9Sstevel@tonic-gate #include <dhcpagent_ipc.h>
867c478bd9Sstevel@tonic-gate #include <dhcpagent_util.h>
877c478bd9Sstevel@tonic-gate #include <compat.h>
887c478bd9Sstevel@tonic-gate 
8945916cd2Sjpk #include <libtsnet.h>
9045916cd2Sjpk #include <tsol/label.h>
9145916cd2Sjpk 
9226fd7700SKrishnendu Sadhukhan - Sun Microsystems #include "statcommon.h"
9326fd7700SKrishnendu Sadhukhan - Sun Microsystems 
947c478bd9Sstevel@tonic-gate extern void	unixpr(kstat_ctl_t *kc);
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate #define	STR_EXPAND	4
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate #define	V4MASK_TO_V6(v4, v6)	((v6)._S6_un._S6_u32[0] = 0xfffffffful, \
997c478bd9Sstevel@tonic-gate 				(v6)._S6_un._S6_u32[1] = 0xfffffffful, \
1007c478bd9Sstevel@tonic-gate 				(v6)._S6_un._S6_u32[2] = 0xfffffffful, \
1017c478bd9Sstevel@tonic-gate 				(v6)._S6_un._S6_u32[3] = (v4))
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate #define	IN6_IS_V4MASK(v6)	((v6)._S6_un._S6_u32[0] == 0xfffffffful && \
1047c478bd9Sstevel@tonic-gate 				(v6)._S6_un._S6_u32[1] == 0xfffffffful && \
1057c478bd9Sstevel@tonic-gate 				(v6)._S6_un._S6_u32[2] == 0xfffffffful)
1067c478bd9Sstevel@tonic-gate 
107d04ccbb3Scarlsonj /*
108d04ccbb3Scarlsonj  * This is used as a cushion in the buffer allocation directed by SIOCGLIFNUM.
109d04ccbb3Scarlsonj  * Because there's no locking between SIOCGLIFNUM and SIOCGLIFCONF, it's
110d04ccbb3Scarlsonj  * possible for an administrator to plumb new interfaces between those two
111d04ccbb3Scarlsonj  * calls, resulting in the failure of the latter.  This addition makes that
112d04ccbb3Scarlsonj  * less likely.
113d04ccbb3Scarlsonj  */
114d04ccbb3Scarlsonj #define	LIFN_GUARD_VALUE	10
115d04ccbb3Scarlsonj 
1167c478bd9Sstevel@tonic-gate typedef struct mib_item_s {
1177c478bd9Sstevel@tonic-gate 	struct mib_item_s	*next_item;
1187c478bd9Sstevel@tonic-gate 	int			group;
1197c478bd9Sstevel@tonic-gate 	int			mib_id;
1207c478bd9Sstevel@tonic-gate 	int			length;
1217c478bd9Sstevel@tonic-gate 	void			*valp;
1227c478bd9Sstevel@tonic-gate } mib_item_t;
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate struct	ifstat {
1257c478bd9Sstevel@tonic-gate 	uint64_t	ipackets;
1267c478bd9Sstevel@tonic-gate 	uint64_t	ierrors;
1277c478bd9Sstevel@tonic-gate 	uint64_t	opackets;
1287c478bd9Sstevel@tonic-gate 	uint64_t	oerrors;
1297c478bd9Sstevel@tonic-gate 	uint64_t	collisions;
1307c478bd9Sstevel@tonic-gate };
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate struct iflist {
1337c478bd9Sstevel@tonic-gate 	struct iflist	*next_if;
1347c478bd9Sstevel@tonic-gate 	char		ifname[LIFNAMSIZ];
1357c478bd9Sstevel@tonic-gate 	struct ifstat	tot;
1367c478bd9Sstevel@tonic-gate };
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate static	mib_item_t	*mibget(int sd);
1397c478bd9Sstevel@tonic-gate static	void		mibfree(mib_item_t *firstitem);
1407c478bd9Sstevel@tonic-gate static	int		mibopen(void);
1417c478bd9Sstevel@tonic-gate static void		mib_get_constants(mib_item_t *item);
1427c478bd9Sstevel@tonic-gate static mib_item_t	*mib_item_dup(mib_item_t *item);
1437c478bd9Sstevel@tonic-gate static mib_item_t	*mib_item_diff(mib_item_t *item1,
1447c478bd9Sstevel@tonic-gate     mib_item_t *item2);
1457c478bd9Sstevel@tonic-gate static void		mib_item_destroy(mib_item_t **item);
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate static boolean_t	octetstrmatch(const Octet_t *a, const Octet_t *b);
14845916cd2Sjpk static char		*octetstr(const Octet_t *op, int code,
1497c478bd9Sstevel@tonic-gate 			    char *dst, uint_t dstlen);
1507c478bd9Sstevel@tonic-gate static char		*pr_addr(uint_t addr,
1517c478bd9Sstevel@tonic-gate 			    char *dst, uint_t dstlen);
1527c478bd9Sstevel@tonic-gate static char		*pr_addrnz(ipaddr_t addr, char *dst, uint_t dstlen);
1537c478bd9Sstevel@tonic-gate static char		*pr_addr6(const in6_addr_t *addr,
1547c478bd9Sstevel@tonic-gate 			    char *dst, uint_t dstlen);
1557c478bd9Sstevel@tonic-gate static char		*pr_mask(uint_t addr,
1567c478bd9Sstevel@tonic-gate 			    char *dst, uint_t dstlen);
15745916cd2Sjpk static char		*pr_prefix6(const struct in6_addr *addr,
15845916cd2Sjpk 			    uint_t prefixlen, char *dst, uint_t dstlen);
1597c478bd9Sstevel@tonic-gate static char		*pr_ap(uint_t addr, uint_t port,
1607c478bd9Sstevel@tonic-gate 			    char *proto, char *dst, uint_t dstlen);
1617c478bd9Sstevel@tonic-gate static char		*pr_ap6(const in6_addr_t *addr, uint_t port,
1627c478bd9Sstevel@tonic-gate 			    char *proto, char *dst, uint_t dstlen);
1637c478bd9Sstevel@tonic-gate static char		*pr_net(uint_t addr, uint_t mask,
1647c478bd9Sstevel@tonic-gate 			    char *dst, uint_t dstlen);
1657c478bd9Sstevel@tonic-gate static char		*pr_netaddr(uint_t addr, uint_t mask,
1667c478bd9Sstevel@tonic-gate 			    char *dst, uint_t dstlen);
1677c478bd9Sstevel@tonic-gate static char		*fmodestr(uint_t fmode);
1687c478bd9Sstevel@tonic-gate static char		*portname(uint_t port, char *proto,
1697c478bd9Sstevel@tonic-gate 			    char *dst, uint_t dstlen);
1707c478bd9Sstevel@tonic-gate 
17145916cd2Sjpk static const char	*mitcp_state(int code,
17245916cd2Sjpk 			    const mib2_transportMLPEntry_t *attr);
17345916cd2Sjpk static const char	*miudp_state(int code,
17445916cd2Sjpk 			    const mib2_transportMLPEntry_t *attr);
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate static void		stat_report(mib_item_t *item);
1777c478bd9Sstevel@tonic-gate static void		mrt_stat_report(mib_item_t *item);
1787c478bd9Sstevel@tonic-gate static void		arp_report(mib_item_t *item);
1797c478bd9Sstevel@tonic-gate static void		ndp_report(mib_item_t *item);
1807c478bd9Sstevel@tonic-gate static void		mrt_report(mib_item_t *item);
1817c478bd9Sstevel@tonic-gate static void		if_stat_total(struct ifstat *oldstats,
1827c478bd9Sstevel@tonic-gate 			    struct ifstat *newstats, struct ifstat *sumstats);
1837c478bd9Sstevel@tonic-gate static void		if_report(mib_item_t *item, char *ifname,
1847c478bd9Sstevel@tonic-gate 			    int Iflag_only, boolean_t once_only);
1857c478bd9Sstevel@tonic-gate static void		if_report_ip4(mib2_ipAddrEntry_t *ap,
1867c478bd9Sstevel@tonic-gate 			    char ifname[], char logintname[],
1877c478bd9Sstevel@tonic-gate 			    struct ifstat *statptr, boolean_t ksp_not_null);
1887c478bd9Sstevel@tonic-gate static void		if_report_ip6(mib2_ipv6AddrEntry_t *ap6,
1897c478bd9Sstevel@tonic-gate 			    char ifname[], char logintname[],
1907c478bd9Sstevel@tonic-gate 			    struct ifstat *statptr, boolean_t ksp_not_null);
19145916cd2Sjpk static void		ire_report(const mib_item_t *item);
19245916cd2Sjpk static void		tcp_report(const mib_item_t *item);
19345916cd2Sjpk static void		udp_report(const mib_item_t *item);
1947c478bd9Sstevel@tonic-gate static void		group_report(mib_item_t *item);
195bd670b35SErik Nordmark static void		dce_report(mib_item_t *item);
1967c478bd9Sstevel@tonic-gate static void		print_ip_stats(mib2_ip_t *ip);
1977c478bd9Sstevel@tonic-gate static void		print_icmp_stats(mib2_icmp_t *icmp);
1987c478bd9Sstevel@tonic-gate static void		print_ip6_stats(mib2_ipv6IfStatsEntry_t *ip6);
1997c478bd9Sstevel@tonic-gate static void		print_icmp6_stats(mib2_ipv6IfIcmpEntry_t *icmp6);
2007c478bd9Sstevel@tonic-gate static void		print_sctp_stats(mib2_sctp_t *tcp);
2017c478bd9Sstevel@tonic-gate static void		print_tcp_stats(mib2_tcp_t *tcp);
2027c478bd9Sstevel@tonic-gate static void		print_udp_stats(mib2_udp_t *udp);
2037c478bd9Sstevel@tonic-gate static void		print_rawip_stats(mib2_rawip_t *rawip);
2047c478bd9Sstevel@tonic-gate static void		print_igmp_stats(struct igmpstat *igps);
2057c478bd9Sstevel@tonic-gate static void		print_mrt_stats(struct mrtstat *mrts);
20645916cd2Sjpk static void		sctp_report(const mib_item_t *item);
2077c478bd9Sstevel@tonic-gate static void		sum_ip6_stats(mib2_ipv6IfStatsEntry_t *ip6,
2087c478bd9Sstevel@tonic-gate 			    mib2_ipv6IfStatsEntry_t *sum6);
2097c478bd9Sstevel@tonic-gate static void		sum_icmp6_stats(mib2_ipv6IfIcmpEntry_t *icmp6,
2107c478bd9Sstevel@tonic-gate 			    mib2_ipv6IfIcmpEntry_t *sum6);
2117c478bd9Sstevel@tonic-gate static void		m_report(void);
2127c478bd9Sstevel@tonic-gate static void		dhcp_report(char *);
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate static	uint64_t	kstat_named_value(kstat_t *, char *);
2157c478bd9Sstevel@tonic-gate static	kid_t		safe_kstat_read(kstat_ctl_t *, kstat_t *, void *);
2167c478bd9Sstevel@tonic-gate static int		isnum(char *);
2177c478bd9Sstevel@tonic-gate static char		*plural(int n);
2187c478bd9Sstevel@tonic-gate static char		*pluraly(int n);
2197c478bd9Sstevel@tonic-gate static char		*plurales(int n);
2207c478bd9Sstevel@tonic-gate static void		process_filter(char *arg);
221e11c3f44Smeem static char		*ifindex2str(uint_t, char *);
2227c478bd9Sstevel@tonic-gate static boolean_t	family_selected(int family);
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate static void		usage(char *);
2257c478bd9Sstevel@tonic-gate static void 		fatal(int errcode, char *str1, ...);
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate #define	PLURAL(n) plural((int)n)
2287c478bd9Sstevel@tonic-gate #define	PLURALY(n) pluraly((int)n)
2297c478bd9Sstevel@tonic-gate #define	PLURALES(n) plurales((int)n)
2307c478bd9Sstevel@tonic-gate #define	IFLAGMOD(flg, val1, val2)	if (flg == val1) flg = val2
2317c478bd9Sstevel@tonic-gate #define	MDIFF(diff, elem2, elem1, member)	(diff)->member = \
2327c478bd9Sstevel@tonic-gate 	(elem2)->member - (elem1)->member
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate static	boolean_t	Aflag = B_FALSE;	/* All sockets/ifs/rtng-tbls */
236bd670b35SErik Nordmark static	boolean_t	Dflag = B_FALSE;	/* DCE info */
2377c478bd9Sstevel@tonic-gate static	boolean_t	Iflag = B_FALSE;	/* IP Traffic Interfaces */
2387c478bd9Sstevel@tonic-gate static	boolean_t	Mflag = B_FALSE;	/* STREAMS Memory Statistics */
2397c478bd9Sstevel@tonic-gate static	boolean_t	Nflag = B_FALSE;	/* Numeric Network Addresses */
2407c478bd9Sstevel@tonic-gate static	boolean_t	Rflag = B_FALSE;	/* Routing Tables */
24145916cd2Sjpk static	boolean_t	RSECflag = B_FALSE;	/* Security attributes */
2427c478bd9Sstevel@tonic-gate static	boolean_t	Sflag = B_FALSE;	/* Per-protocol Statistics */
2437c478bd9Sstevel@tonic-gate static	boolean_t	Vflag = B_FALSE;	/* Verbose */
2447c478bd9Sstevel@tonic-gate static	boolean_t	Pflag = B_FALSE;	/* Net to Media Tables */
2457c478bd9Sstevel@tonic-gate static	boolean_t	Gflag = B_FALSE;	/* Multicast group membership */
2467c478bd9Sstevel@tonic-gate static	boolean_t	MMflag = B_FALSE;	/* Multicast routing table */
2477c478bd9Sstevel@tonic-gate static	boolean_t	DHCPflag = B_FALSE;	/* DHCP statistics */
248bd670b35SErik Nordmark static	boolean_t	Xflag = B_FALSE;	/* Debug Info */
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate static	int	v4compat = 0;	/* Compatible printing format for status */
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate static int	proto = IPPROTO_MAX;	/* all protocols */
2537c478bd9Sstevel@tonic-gate kstat_ctl_t	*kc = NULL;
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate /*
2567c478bd9Sstevel@tonic-gate  * Sizes of data structures extracted from the base mib.
2577c478bd9Sstevel@tonic-gate  * This allows the size of the tables entries to grow while preserving
2587c478bd9Sstevel@tonic-gate  * binary compatibility.
2597c478bd9Sstevel@tonic-gate  */
2607c478bd9Sstevel@tonic-gate static int ipAddrEntrySize;
2617c478bd9Sstevel@tonic-gate static int ipRouteEntrySize;
2627c478bd9Sstevel@tonic-gate static int ipNetToMediaEntrySize;
2637c478bd9Sstevel@tonic-gate static int ipMemberEntrySize;
2647c478bd9Sstevel@tonic-gate static int ipGroupSourceEntrySize;
26545916cd2Sjpk static int ipRouteAttributeSize;
2667c478bd9Sstevel@tonic-gate static int vifctlSize;
2677c478bd9Sstevel@tonic-gate static int mfcctlSize;
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate static int ipv6IfStatsEntrySize;
2707c478bd9Sstevel@tonic-gate static int ipv6IfIcmpEntrySize;
2717c478bd9Sstevel@tonic-gate static int ipv6AddrEntrySize;
2727c478bd9Sstevel@tonic-gate static int ipv6RouteEntrySize;
2737c478bd9Sstevel@tonic-gate static int ipv6NetToMediaEntrySize;
2747c478bd9Sstevel@tonic-gate static int ipv6MemberEntrySize;
2757c478bd9Sstevel@tonic-gate static int ipv6GroupSourceEntrySize;
2767c478bd9Sstevel@tonic-gate 
277bd670b35SErik Nordmark static int ipDestEntrySize;
278bd670b35SErik Nordmark 
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
301bd670b35SErik Nordmark #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
309bd670b35SErik Nordmark #define	FLF_C		0x00000200	/* IRE_IF_CLONE */
310bd670b35SErik Nordmark #define	FLF_I		0x00000400	/* RTF_INDIRECT */
311bd670b35SErik Nordmark #define	FLF_R		0x00000800	/* RTF_REJECT */
312bd670b35SErik Nordmark #define	FLF_B		0x00001000	/* RTF_BLACKHOLE */
313*550b6e40SSowmini Varadhan #define	FLF_Z		0x00100000	/* RTF_ZONE */
314bd670b35SErik Nordmark 
315*550b6e40SSowmini Varadhan static const char flag_list[] = "AbDGHLUMSCIRBZ";
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate typedef struct filter_rule filter_t;
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate struct filter_rule {
3207c478bd9Sstevel@tonic-gate 	filter_t *f_next;
3217c478bd9Sstevel@tonic-gate 	union {
3227c478bd9Sstevel@tonic-gate 		int f_family;
3237c478bd9Sstevel@tonic-gate 		const char *f_ifname;
3247c478bd9Sstevel@tonic-gate 		struct {
3257c478bd9Sstevel@tonic-gate 			struct hostent *f_address;
3267c478bd9Sstevel@tonic-gate 			in6_addr_t f_mask;
3277c478bd9Sstevel@tonic-gate 		} a;
3287c478bd9Sstevel@tonic-gate 		struct {
3297c478bd9Sstevel@tonic-gate 			uint_t f_flagset;
3307c478bd9Sstevel@tonic-gate 			uint_t f_flagclear;
3317c478bd9Sstevel@tonic-gate 		} f;
3327c478bd9Sstevel@tonic-gate 	} u;
3337c478bd9Sstevel@tonic-gate };
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate /*
3367c478bd9Sstevel@tonic-gate  * The user-specified filters are linked into lists separated by
3377c478bd9Sstevel@tonic-gate  * keyword (type of filter).  Thus, the matching algorithm is:
3387c478bd9Sstevel@tonic-gate  *	For each non-empty filter list
3397c478bd9Sstevel@tonic-gate  *		If no filters in the list match
3407c478bd9Sstevel@tonic-gate  *			then stop here; route doesn't match
3417c478bd9Sstevel@tonic-gate  *	If loop above completes, then route does match and will be
3427c478bd9Sstevel@tonic-gate  *	displayed.
3437c478bd9Sstevel@tonic-gate  */
3447c478bd9Sstevel@tonic-gate static filter_t *filters[NFILTERKEYS];
3457c478bd9Sstevel@tonic-gate 
34626fd7700SKrishnendu Sadhukhan - Sun Microsystems static uint_t timestamp_fmt = NODATE;
34726fd7700SKrishnendu Sadhukhan - Sun Microsystems 
34826fd7700SKrishnendu Sadhukhan - Sun Microsystems #if !defined(TEXT_DOMAIN)		/* Should be defined by cc -D */
34926fd7700SKrishnendu Sadhukhan - Sun Microsystems #define	TEXT_DOMAIN "SYS_TEST"		/* Use this only if it isn't */
35026fd7700SKrishnendu Sadhukhan - Sun Microsystems #endif
35126fd7700SKrishnendu Sadhukhan - Sun Microsystems 
3527c478bd9Sstevel@tonic-gate int
3537c478bd9Sstevel@tonic-gate main(int argc, char **argv)
3547c478bd9Sstevel@tonic-gate {
3557c478bd9Sstevel@tonic-gate 	char		*name;
3567c478bd9Sstevel@tonic-gate 	mib_item_t	*item = NULL;
3577c478bd9Sstevel@tonic-gate 	mib_item_t	*previtem = NULL;
3587c478bd9Sstevel@tonic-gate 	int		sd = -1;
3597c478bd9Sstevel@tonic-gate 	char	*ifname = NULL;
3607c478bd9Sstevel@tonic-gate 	int	interval = 0;	/* Single time by default */
3617c478bd9Sstevel@tonic-gate 	int	count = -1;	/* Forever */
3627c478bd9Sstevel@tonic-gate 	int	c;
3637c478bd9Sstevel@tonic-gate 	int	d;
3647c478bd9Sstevel@tonic-gate 	/*
3657c478bd9Sstevel@tonic-gate 	 * Possible values of 'Iflag_only':
3667c478bd9Sstevel@tonic-gate 	 * -1, no feature-flags;
3677c478bd9Sstevel@tonic-gate 	 *  0, IFlag and other feature-flags enabled
3687c478bd9Sstevel@tonic-gate 	 *  1, IFlag is the only feature-flag enabled
3697c478bd9Sstevel@tonic-gate 	 * : trinary variable, modified using IFLAGMOD()
3707c478bd9Sstevel@tonic-gate 	 */
3717c478bd9Sstevel@tonic-gate 	int Iflag_only = -1;
3727c478bd9Sstevel@tonic-gate 	boolean_t once_only = B_FALSE; /* '-i' with count > 1 */
3737c478bd9Sstevel@tonic-gate 	extern char	*optarg;
3747c478bd9Sstevel@tonic-gate 	extern int	optind;
3757c478bd9Sstevel@tonic-gate 	char *default_ip_str = NULL;
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 	name = argv[0];
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	v4compat = get_compat_flag(&default_ip_str);
3807c478bd9Sstevel@tonic-gate 	if (v4compat == DEFAULT_PROT_BAD_VALUE)
3817c478bd9Sstevel@tonic-gate 		fatal(2, "%s: %s: Bad value for %s in %s\n", name,
3827c478bd9Sstevel@tonic-gate 		    default_ip_str, DEFAULT_IP, INET_DEFAULT_FILE);
3837c478bd9Sstevel@tonic-gate 	free(default_ip_str);
3847c478bd9Sstevel@tonic-gate 
38526fd7700SKrishnendu Sadhukhan - Sun Microsystems 	(void) setlocale(LC_ALL, "");
38626fd7700SKrishnendu Sadhukhan - Sun Microsystems 	(void) textdomain(TEXT_DOMAIN);
38726fd7700SKrishnendu Sadhukhan - Sun Microsystems 
388bd670b35SErik Nordmark 	while ((c = getopt(argc, argv, "adimnrspMgvxf:P:I:DRT:")) != -1) {
3897c478bd9Sstevel@tonic-gate 		switch ((char)c) {
3907c478bd9Sstevel@tonic-gate 		case 'a':		/* all connections */
3917c478bd9Sstevel@tonic-gate 			Aflag = B_TRUE;
3927c478bd9Sstevel@tonic-gate 			break;
3937c478bd9Sstevel@tonic-gate 
394bd670b35SErik Nordmark 		case 'd':		/* DCE info */
3957c478bd9Sstevel@tonic-gate 			Dflag = B_TRUE;
396bd670b35SErik Nordmark 			IFLAGMOD(Iflag_only, 1, 0); /* see macro def'n */
3977c478bd9Sstevel@tonic-gate 			break;
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 		case 'i':		/* interface (ill/ipif report) */
4007c478bd9Sstevel@tonic-gate 			Iflag = B_TRUE;
4017c478bd9Sstevel@tonic-gate 			IFLAGMOD(Iflag_only, -1, 1); /* '-i' exists */
4027c478bd9Sstevel@tonic-gate 			break;
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 		case 'm':		/* streams msg report */
4057c478bd9Sstevel@tonic-gate 			Mflag = B_TRUE;
4067c478bd9Sstevel@tonic-gate 			IFLAGMOD(Iflag_only, 1, 0); /* see macro def'n */
4077c478bd9Sstevel@tonic-gate 			break;
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 		case 'n':		/* numeric format */
4107c478bd9Sstevel@tonic-gate 			Nflag = B_TRUE;
4117c478bd9Sstevel@tonic-gate 			break;
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 		case 'r':		/* route tables */
4147c478bd9Sstevel@tonic-gate 			Rflag = B_TRUE;
4157c478bd9Sstevel@tonic-gate 			IFLAGMOD(Iflag_only, 1, 0); /* see macro def'n */
4167c478bd9Sstevel@tonic-gate 			break;
4177c478bd9Sstevel@tonic-gate 
41845916cd2Sjpk 		case 'R':		/* security attributes */
41945916cd2Sjpk 			RSECflag = B_TRUE;
42045916cd2Sjpk 			IFLAGMOD(Iflag_only, 1, 0); /* see macro def'n */
42145916cd2Sjpk 			break;
42245916cd2Sjpk 
4237c478bd9Sstevel@tonic-gate 		case 's':		/* per-protocol statistics */
4247c478bd9Sstevel@tonic-gate 			Sflag = B_TRUE;
4257c478bd9Sstevel@tonic-gate 			IFLAGMOD(Iflag_only, 1, 0); /* see macro def'n */
4267c478bd9Sstevel@tonic-gate 			break;
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 		case 'p':		/* arp/ndp table */
4297c478bd9Sstevel@tonic-gate 			Pflag = B_TRUE;
4307c478bd9Sstevel@tonic-gate 			IFLAGMOD(Iflag_only, 1, 0); /* see macro def'n */
4317c478bd9Sstevel@tonic-gate 			break;
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 		case 'M':		/* multicast routing tables */
4347c478bd9Sstevel@tonic-gate 			MMflag = B_TRUE;
4357c478bd9Sstevel@tonic-gate 			IFLAGMOD(Iflag_only, 1, 0); /* see macro def'n */
4367c478bd9Sstevel@tonic-gate 			break;
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 		case 'g':		/* multicast group membership */
4397c478bd9Sstevel@tonic-gate 			Gflag = B_TRUE;
4407c478bd9Sstevel@tonic-gate 			IFLAGMOD(Iflag_only, 1, 0); /* see macro def'n */
4417c478bd9Sstevel@tonic-gate 			break;
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate 		case 'v':		/* verbose output format */
4447c478bd9Sstevel@tonic-gate 			Vflag = B_TRUE;
4457c478bd9Sstevel@tonic-gate 			IFLAGMOD(Iflag_only, 1, 0); /* see macro def'n */
4467c478bd9Sstevel@tonic-gate 			break;
4477c478bd9Sstevel@tonic-gate 
448bd670b35SErik Nordmark 		case 'x':		/* turn on debugging */
449bd670b35SErik Nordmark 			Xflag = B_TRUE;
450bd670b35SErik Nordmark 			break;
451bd670b35SErik Nordmark 
4527c478bd9Sstevel@tonic-gate 		case 'f':
4537c478bd9Sstevel@tonic-gate 			process_filter(optarg);
4547c478bd9Sstevel@tonic-gate 			break;
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 		case 'P':
4577c478bd9Sstevel@tonic-gate 			if (strcmp(optarg, "ip") == 0) {
4587c478bd9Sstevel@tonic-gate 				proto = IPPROTO_IP;
4597c478bd9Sstevel@tonic-gate 			} else if (strcmp(optarg, "ipv6") == 0 ||
4607c478bd9Sstevel@tonic-gate 			    strcmp(optarg, "ip6") == 0) {
4617c478bd9Sstevel@tonic-gate 				v4compat = 0;	/* Overridden */
4627c478bd9Sstevel@tonic-gate 				proto = IPPROTO_IPV6;
4637c478bd9Sstevel@tonic-gate 			} else if (strcmp(optarg, "icmp") == 0) {
4647c478bd9Sstevel@tonic-gate 				proto = IPPROTO_ICMP;
4657c478bd9Sstevel@tonic-gate 			} else if (strcmp(optarg, "icmpv6") == 0 ||
4667c478bd9Sstevel@tonic-gate 			    strcmp(optarg, "icmp6") == 0) {
4677c478bd9Sstevel@tonic-gate 				v4compat = 0;	/* Overridden */
4687c478bd9Sstevel@tonic-gate 				proto = IPPROTO_ICMPV6;
4697c478bd9Sstevel@tonic-gate 			} else if (strcmp(optarg, "igmp") == 0) {
4707c478bd9Sstevel@tonic-gate 				proto = IPPROTO_IGMP;
4717c478bd9Sstevel@tonic-gate 			} else if (strcmp(optarg, "udp") == 0) {
4727c478bd9Sstevel@tonic-gate 				proto = IPPROTO_UDP;
4737c478bd9Sstevel@tonic-gate 			} else if (strcmp(optarg, "tcp") == 0) {
4747c478bd9Sstevel@tonic-gate 				proto = IPPROTO_TCP;
4757c478bd9Sstevel@tonic-gate 			} else if (strcmp(optarg, "sctp") == 0) {
4767c478bd9Sstevel@tonic-gate 				proto = IPPROTO_SCTP;
4777c478bd9Sstevel@tonic-gate 			} else if (strcmp(optarg, "raw") == 0 ||
4787c478bd9Sstevel@tonic-gate 			    strcmp(optarg, "rawip") == 0) {
4797c478bd9Sstevel@tonic-gate 				proto = IPPROTO_RAW;
4807c478bd9Sstevel@tonic-gate 			} else {
4817c478bd9Sstevel@tonic-gate 				fatal(1, "%s: unknown protocol.\n", optarg);
4827c478bd9Sstevel@tonic-gate 			}
4837c478bd9Sstevel@tonic-gate 			break;
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 		case 'I':
4867c478bd9Sstevel@tonic-gate 			ifname = optarg;
4877c478bd9Sstevel@tonic-gate 			Iflag = B_TRUE;
4887c478bd9Sstevel@tonic-gate 			IFLAGMOD(Iflag_only, -1, 1); /* see macro def'n */
4897c478bd9Sstevel@tonic-gate 			break;
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 		case 'D':
4927c478bd9Sstevel@tonic-gate 			DHCPflag = B_TRUE;
4937c478bd9Sstevel@tonic-gate 			Iflag_only = 0;
4947c478bd9Sstevel@tonic-gate 			break;
4957c478bd9Sstevel@tonic-gate 
49626fd7700SKrishnendu Sadhukhan - Sun Microsystems 		case 'T':
49726fd7700SKrishnendu Sadhukhan - Sun Microsystems 			if (optarg) {
49826fd7700SKrishnendu Sadhukhan - Sun Microsystems 				if (*optarg == 'u')
49926fd7700SKrishnendu Sadhukhan - Sun Microsystems 					timestamp_fmt = UDATE;
50026fd7700SKrishnendu Sadhukhan - Sun Microsystems 				else if (*optarg == 'd')
50126fd7700SKrishnendu Sadhukhan - Sun Microsystems 					timestamp_fmt = DDATE;
50226fd7700SKrishnendu Sadhukhan - Sun Microsystems 				else
50326fd7700SKrishnendu Sadhukhan - Sun Microsystems 					usage(name);
50426fd7700SKrishnendu Sadhukhan - Sun Microsystems 			} else {
50526fd7700SKrishnendu Sadhukhan - Sun Microsystems 				usage(name);
50626fd7700SKrishnendu Sadhukhan - Sun Microsystems 			}
50726fd7700SKrishnendu Sadhukhan - Sun Microsystems 			break;
50826fd7700SKrishnendu Sadhukhan - Sun Microsystems 
5097c478bd9Sstevel@tonic-gate 		case '?':
5107c478bd9Sstevel@tonic-gate 		default:
5117c478bd9Sstevel@tonic-gate 			usage(name);
5127c478bd9Sstevel@tonic-gate 		}
5137c478bd9Sstevel@tonic-gate 	}
5147c478bd9Sstevel@tonic-gate 
51545916cd2Sjpk 	/*
51645916cd2Sjpk 	 * Make sure -R option is set only on a labeled system.
51745916cd2Sjpk 	 */
51845916cd2Sjpk 	if (RSECflag && !is_system_labeled()) {
51945916cd2Sjpk 		(void) fprintf(stderr, "-R set but labeling is not enabled\n");
52045916cd2Sjpk 		usage(name);
52145916cd2Sjpk 	}
52245916cd2Sjpk 
5237c478bd9Sstevel@tonic-gate 	/*
5247c478bd9Sstevel@tonic-gate 	 * Handle other arguments: find interval, count; the
5257c478bd9Sstevel@tonic-gate 	 * flags that accept 'interval' and 'count' are OR'd
5267c478bd9Sstevel@tonic-gate 	 * in the outermost 'if'; more flags may be added as
5277c478bd9Sstevel@tonic-gate 	 * required
5287c478bd9Sstevel@tonic-gate 	 */
5297c478bd9Sstevel@tonic-gate 	if (Iflag || Sflag || Mflag) {
5307c478bd9Sstevel@tonic-gate 		for (d = optind; d < argc; d++) {
5317c478bd9Sstevel@tonic-gate 			if (isnum(argv[d])) {
5327c478bd9Sstevel@tonic-gate 				interval = atoi(argv[d]);
5337c478bd9Sstevel@tonic-gate 				if (d + 1 < argc &&
5347c478bd9Sstevel@tonic-gate 				    isnum(argv[d + 1])) {
5357c478bd9Sstevel@tonic-gate 					count = atoi(argv[d + 1]);
5367c478bd9Sstevel@tonic-gate 					optind++;
5377c478bd9Sstevel@tonic-gate 				}
5387c478bd9Sstevel@tonic-gate 				optind++;
5397c478bd9Sstevel@tonic-gate 				if (interval == 0 || count == 0)
5407c478bd9Sstevel@tonic-gate 					usage(name);
5417c478bd9Sstevel@tonic-gate 				break;
5427c478bd9Sstevel@tonic-gate 			}
5437c478bd9Sstevel@tonic-gate 		}
5447c478bd9Sstevel@tonic-gate 	}
5457c478bd9Sstevel@tonic-gate 	if (optind < argc) {
5467c478bd9Sstevel@tonic-gate 		if (Iflag && isnum(argv[optind])) {
5477c478bd9Sstevel@tonic-gate 			count = atoi(argv[optind]);
5487c478bd9Sstevel@tonic-gate 			if (count == 0)
5497c478bd9Sstevel@tonic-gate 				usage(name);
5507c478bd9Sstevel@tonic-gate 			optind++;
5517c478bd9Sstevel@tonic-gate 		}
5527c478bd9Sstevel@tonic-gate 	}
5537c478bd9Sstevel@tonic-gate 	if (optind < argc) {
5547c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
5557c478bd9Sstevel@tonic-gate 		    "%s: extra arguments\n", name);
5567c478bd9Sstevel@tonic-gate 		usage(name);
5577c478bd9Sstevel@tonic-gate 	}
5587c478bd9Sstevel@tonic-gate 	if (interval)
5597c478bd9Sstevel@tonic-gate 		setbuf(stdout, NULL);
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate 	if (DHCPflag) {
5627c478bd9Sstevel@tonic-gate 		dhcp_report(Iflag ? ifname : NULL);
5637c478bd9Sstevel@tonic-gate 		exit(0);
5647c478bd9Sstevel@tonic-gate 	}
5657c478bd9Sstevel@tonic-gate 
5665f9878b0Sken Powell - Sun Microsystem 	/*
5675f9878b0Sken Powell - Sun Microsystem 	 * Get this process's security label if the -R switch is set.
5685f9878b0Sken Powell - Sun Microsystem 	 * We use this label as the current zone's security label.
5695f9878b0Sken Powell - Sun Microsystem 	 */
5705f9878b0Sken Powell - Sun Microsystem 	if (RSECflag) {
5715f9878b0Sken Powell - Sun Microsystem 		zone_security_label = m_label_alloc(MAC_LABEL);
5725f9878b0Sken Powell - Sun Microsystem 		if (zone_security_label == NULL)
5735f9878b0Sken Powell - Sun Microsystem 			fatal(errno, "m_label_alloc() failed");
5745f9878b0Sken Powell - Sun Microsystem 		if (getplabel(zone_security_label) < 0)
5755f9878b0Sken Powell - Sun Microsystem 			fatal(errno, "getplabel() failed");
5765f9878b0Sken Powell - Sun Microsystem 	}
5775f9878b0Sken Powell - Sun Microsystem 
5787c478bd9Sstevel@tonic-gate 	/* Get data structures: priming before iteration */
5797c478bd9Sstevel@tonic-gate 	if (family_selected(AF_INET) || family_selected(AF_INET6)) {
5807c478bd9Sstevel@tonic-gate 		sd = mibopen();
5817c478bd9Sstevel@tonic-gate 		if (sd == -1)
5827c478bd9Sstevel@tonic-gate 			fatal(1, "can't open mib stream\n");
5837c478bd9Sstevel@tonic-gate 		if ((item = mibget(sd)) == NULL) {
5847c478bd9Sstevel@tonic-gate 			(void) close(sd);
5857c478bd9Sstevel@tonic-gate 			fatal(1, "mibget() failed\n");
5867c478bd9Sstevel@tonic-gate 		}
5877c478bd9Sstevel@tonic-gate 		/* Extract constant sizes - need do once only */
5887c478bd9Sstevel@tonic-gate 		mib_get_constants(item);
5897c478bd9Sstevel@tonic-gate 	}
5907c478bd9Sstevel@tonic-gate 	if ((kc = kstat_open()) == NULL) {
5917c478bd9Sstevel@tonic-gate 		mibfree(item);
5927c478bd9Sstevel@tonic-gate 		(void) close(sd);
5937c478bd9Sstevel@tonic-gate 		fail(1, "kstat_open(): can't open /dev/kstat");
5947c478bd9Sstevel@tonic-gate 	}
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 	if (interval <= 0) {
5977c478bd9Sstevel@tonic-gate 		count = 1;
5987c478bd9Sstevel@tonic-gate 		once_only = B_TRUE;
5997c478bd9Sstevel@tonic-gate 	}
6007c478bd9Sstevel@tonic-gate 	/* 'for' loop 1: */
6017c478bd9Sstevel@tonic-gate 	for (;;) {
6027c478bd9Sstevel@tonic-gate 		mib_item_t *curritem = NULL; /* only for -[M]s */
6037c478bd9Sstevel@tonic-gate 
60426fd7700SKrishnendu Sadhukhan - Sun Microsystems 		if (timestamp_fmt != NODATE)
60526fd7700SKrishnendu Sadhukhan - Sun Microsystems 			print_timestamp(timestamp_fmt);
60626fd7700SKrishnendu Sadhukhan - Sun Microsystems 
6077c478bd9Sstevel@tonic-gate 		/* netstat: AF_INET[6] behaviour */
6087c478bd9Sstevel@tonic-gate 		if (family_selected(AF_INET) || family_selected(AF_INET6)) {
6097c478bd9Sstevel@tonic-gate 			if (Sflag) {
6107c478bd9Sstevel@tonic-gate 				curritem = mib_item_diff(previtem, item);
6117c478bd9Sstevel@tonic-gate 				if (curritem == NULL)
6127c478bd9Sstevel@tonic-gate 					fatal(1, "can't process mib data, "
6137c478bd9Sstevel@tonic-gate 					    "out of memory\n");
6147c478bd9Sstevel@tonic-gate 				mib_item_destroy(&previtem);
6157c478bd9Sstevel@tonic-gate 			}
6167c478bd9Sstevel@tonic-gate 
617bd670b35SErik Nordmark 			if (!(Dflag || Iflag || Rflag || Sflag || Mflag ||
6187c478bd9Sstevel@tonic-gate 			    MMflag || Pflag || Gflag || DHCPflag)) {
6197c478bd9Sstevel@tonic-gate 				if (protocol_selected(IPPROTO_UDP))
6207c478bd9Sstevel@tonic-gate 					udp_report(item);
6217c478bd9Sstevel@tonic-gate 				if (protocol_selected(IPPROTO_TCP))
6227c478bd9Sstevel@tonic-gate 					tcp_report(item);
6237c478bd9Sstevel@tonic-gate 				if (protocol_selected(IPPROTO_SCTP))
6247c478bd9Sstevel@tonic-gate 					sctp_report(item);
6257c478bd9Sstevel@tonic-gate 			}
6267c478bd9Sstevel@tonic-gate 			if (Iflag)
6277c478bd9Sstevel@tonic-gate 				if_report(item, ifname, Iflag_only, once_only);
6287c478bd9Sstevel@tonic-gate 			if (Mflag)
6297c478bd9Sstevel@tonic-gate 				m_report();
6307c478bd9Sstevel@tonic-gate 			if (Rflag)
6317c478bd9Sstevel@tonic-gate 				ire_report(item);
6327c478bd9Sstevel@tonic-gate 			if (Sflag && MMflag) {
6337c478bd9Sstevel@tonic-gate 				mrt_stat_report(curritem);
6347c478bd9Sstevel@tonic-gate 			} else {
6357c478bd9Sstevel@tonic-gate 				if (Sflag)
6367c478bd9Sstevel@tonic-gate 					stat_report(curritem);
6377c478bd9Sstevel@tonic-gate 				if (MMflag)
6387c478bd9Sstevel@tonic-gate 					mrt_report(item);
6397c478bd9Sstevel@tonic-gate 			}
6407c478bd9Sstevel@tonic-gate 			if (Gflag)
6417c478bd9Sstevel@tonic-gate 				group_report(item);
6427c478bd9Sstevel@tonic-gate 			if (Pflag) {
6437c478bd9Sstevel@tonic-gate 				if (family_selected(AF_INET))
6447c478bd9Sstevel@tonic-gate 					arp_report(item);
6457c478bd9Sstevel@tonic-gate 				if (family_selected(AF_INET6))
6467c478bd9Sstevel@tonic-gate 					ndp_report(item);
6477c478bd9Sstevel@tonic-gate 			}
648bd670b35SErik Nordmark 			if (Dflag)
649bd670b35SErik Nordmark 				dce_report(item);
6507c478bd9Sstevel@tonic-gate 			mib_item_destroy(&curritem);
6517c478bd9Sstevel@tonic-gate 		}
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate 		/* netstat: AF_UNIX behaviour */
6547c478bd9Sstevel@tonic-gate 		if (family_selected(AF_UNIX) &&
655bd670b35SErik Nordmark 		    (!(Dflag || Iflag || Rflag || Sflag || Mflag ||
6567c478bd9Sstevel@tonic-gate 		    MMflag || Pflag || Gflag)))
6577c478bd9Sstevel@tonic-gate 			unixpr(kc);
6587c478bd9Sstevel@tonic-gate 		(void) kstat_close(kc);
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 		/* iteration handling code */
6617c478bd9Sstevel@tonic-gate 		if (count > 0 && --count == 0)
6627c478bd9Sstevel@tonic-gate 			break;
6637c478bd9Sstevel@tonic-gate 		(void) sleep(interval);
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate 		/* re-populating of data structures */
6667c478bd9Sstevel@tonic-gate 		if (family_selected(AF_INET) || family_selected(AF_INET6)) {
6677c478bd9Sstevel@tonic-gate 			if (Sflag) {
6687c478bd9Sstevel@tonic-gate 				/* previtem is a cut-down list */
6697c478bd9Sstevel@tonic-gate 				previtem = mib_item_dup(item);
6707c478bd9Sstevel@tonic-gate 				if (previtem == NULL)
6717c478bd9Sstevel@tonic-gate 					fatal(1, "can't process mib data, "
6727c478bd9Sstevel@tonic-gate 					    "out of memory\n");
6737c478bd9Sstevel@tonic-gate 			}
6747c478bd9Sstevel@tonic-gate 			mibfree(item);
6757c478bd9Sstevel@tonic-gate 			(void) close(sd);
6767c478bd9Sstevel@tonic-gate 			if ((sd = mibopen()) == -1)
6777c478bd9Sstevel@tonic-gate 				fatal(1, "can't open mib stream anymore\n");
6787c478bd9Sstevel@tonic-gate 			if ((item = mibget(sd)) == NULL) {
6797c478bd9Sstevel@tonic-gate 				(void) close(sd);
6807c478bd9Sstevel@tonic-gate 				fatal(1, "mibget() failed\n");
6817c478bd9Sstevel@tonic-gate 			}
6827c478bd9Sstevel@tonic-gate 		}
6837c478bd9Sstevel@tonic-gate 		if ((kc = kstat_open()) == NULL)
6847c478bd9Sstevel@tonic-gate 			fail(1, "kstat_open(): can't open /dev/kstat");
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate 	} /* 'for' loop 1 ends */
6877c478bd9Sstevel@tonic-gate 	mibfree(item);
6887c478bd9Sstevel@tonic-gate 	(void) close(sd);
6895f9878b0Sken Powell - Sun Microsystem 	if (zone_security_label != NULL)
6905f9878b0Sken Powell - Sun Microsystem 		m_label_free(zone_security_label);
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate 	return (0);
6937c478bd9Sstevel@tonic-gate }
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate static int
6977c478bd9Sstevel@tonic-gate isnum(char *p)
6987c478bd9Sstevel@tonic-gate {
6997c478bd9Sstevel@tonic-gate 	int	len;
7007c478bd9Sstevel@tonic-gate 	int	i;
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 	len = strlen(p);
7037c478bd9Sstevel@tonic-gate 	for (i = 0; i < len; i++)
7047c478bd9Sstevel@tonic-gate 		if (!isdigit(p[i]))
7057c478bd9Sstevel@tonic-gate 			return (0);
7067c478bd9Sstevel@tonic-gate 	return (1);
7077c478bd9Sstevel@tonic-gate }
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate /* --------------------------------- MIBGET -------------------------------- */
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate static mib_item_t *
7137c478bd9Sstevel@tonic-gate mibget(int sd)
7147c478bd9Sstevel@tonic-gate {
7157c478bd9Sstevel@tonic-gate 	/*
7167c478bd9Sstevel@tonic-gate 	 * buf is an automatic for this function, so the
7177c478bd9Sstevel@tonic-gate 	 * compiler has complete control over its alignment;
7187c478bd9Sstevel@tonic-gate 	 * it is assumed this alignment is satisfactory for
7197c478bd9Sstevel@tonic-gate 	 * it to be casted to certain other struct pointers
7207c478bd9Sstevel@tonic-gate 	 * here, such as struct T_optmgmt_ack * .
7217c478bd9Sstevel@tonic-gate 	 */
7227c478bd9Sstevel@tonic-gate 	uintptr_t		buf[512 / sizeof (uintptr_t)];
7237c478bd9Sstevel@tonic-gate 	int			flags;
7247c478bd9Sstevel@tonic-gate 	int			i, j, getcode;
7257c478bd9Sstevel@tonic-gate 	struct strbuf		ctlbuf, databuf;
7267c478bd9Sstevel@tonic-gate 	struct T_optmgmt_req	*tor = (struct T_optmgmt_req *)buf;
7277c478bd9Sstevel@tonic-gate 	struct T_optmgmt_ack	*toa = (struct T_optmgmt_ack *)buf;
7287c478bd9Sstevel@tonic-gate 	struct T_error_ack	*tea = (struct T_error_ack *)buf;
7297c478bd9Sstevel@tonic-gate 	struct opthdr		*req;
7307c478bd9Sstevel@tonic-gate 	mib_item_t		*first_item = NULL;
7317c478bd9Sstevel@tonic-gate 	mib_item_t		*last_item  = NULL;
7327c478bd9Sstevel@tonic-gate 	mib_item_t		*temp;
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate 	tor->PRIM_type = T_SVR4_OPTMGMT_REQ;
7357c478bd9Sstevel@tonic-gate 	tor->OPT_offset = sizeof (struct T_optmgmt_req);
7367c478bd9Sstevel@tonic-gate 	tor->OPT_length = sizeof (struct opthdr);
7377c478bd9Sstevel@tonic-gate 	tor->MGMT_flags = T_CURRENT;
738e11c3f44Smeem 
739e11c3f44Smeem 
740e11c3f44Smeem 	/*
741e11c3f44Smeem 	 * Note: we use the special level value below so that IP will return
742e11c3f44Smeem 	 * us information concerning IRE_MARK_TESTHIDDEN routes.
743e11c3f44Smeem 	 */
7447c478bd9Sstevel@tonic-gate 	req = (struct opthdr *)&tor[1];
745bd670b35SErik Nordmark 	req->level = EXPER_IP_AND_ALL_IRES;
7467c478bd9Sstevel@tonic-gate 	req->name  = 0;
7477c478bd9Sstevel@tonic-gate 	req->len   = 0;
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate 	ctlbuf.buf = (char *)buf;
7507c478bd9Sstevel@tonic-gate 	ctlbuf.len = tor->OPT_length + tor->OPT_offset;
7517c478bd9Sstevel@tonic-gate 	flags = 0;
7527c478bd9Sstevel@tonic-gate 	if (putmsg(sd, &ctlbuf, (struct strbuf *)0, flags) == -1) {
7537c478bd9Sstevel@tonic-gate 		perror("mibget: putmsg(ctl) failed");
7547c478bd9Sstevel@tonic-gate 		goto error_exit;
7557c478bd9Sstevel@tonic-gate 	}
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate 	/*
7587c478bd9Sstevel@tonic-gate 	 * Each reply consists of a ctl part for one fixed structure
7597c478bd9Sstevel@tonic-gate 	 * or table, as defined in mib2.h.  The format is a T_OPTMGMT_ACK,
7607c478bd9Sstevel@tonic-gate 	 * containing an opthdr structure.  level/name identify the entry,
7617c478bd9Sstevel@tonic-gate 	 * len is the size of the data part of the message.
7627c478bd9Sstevel@tonic-gate 	 */
7637c478bd9Sstevel@tonic-gate 	req = (struct opthdr *)&toa[1];
7647c478bd9Sstevel@tonic-gate 	ctlbuf.maxlen = sizeof (buf);
7657c478bd9Sstevel@tonic-gate 	j = 1;
7667c478bd9Sstevel@tonic-gate 	for (;;) {
7677c478bd9Sstevel@tonic-gate 		flags = 0;
7687c478bd9Sstevel@tonic-gate 		getcode = getmsg(sd, &ctlbuf, (struct strbuf *)0, &flags);
7697c478bd9Sstevel@tonic-gate 		if (getcode == -1) {
7707c478bd9Sstevel@tonic-gate 			perror("mibget getmsg(ctl) failed");
771bd670b35SErik Nordmark 			if (Xflag) {
7727c478bd9Sstevel@tonic-gate 				(void) fputs("#   level   name    len\n",
7737c478bd9Sstevel@tonic-gate 				    stderr);
7747c478bd9Sstevel@tonic-gate 				i = 0;
7757c478bd9Sstevel@tonic-gate 				for (last_item = first_item; last_item;
776e11c3f44Smeem 				    last_item = last_item->next_item)
7777c478bd9Sstevel@tonic-gate 					(void) printf("%d  %4d   %5d   %d\n",
7787c478bd9Sstevel@tonic-gate 					    ++i,
7797c478bd9Sstevel@tonic-gate 					    last_item->group,
7807c478bd9Sstevel@tonic-gate 					    last_item->mib_id,
7817c478bd9Sstevel@tonic-gate 					    last_item->length);
7827c478bd9Sstevel@tonic-gate 			}
7837c478bd9Sstevel@tonic-gate 			goto error_exit;
7847c478bd9Sstevel@tonic-gate 		}
7857c478bd9Sstevel@tonic-gate 		if (getcode == 0 &&
7867c478bd9Sstevel@tonic-gate 		    ctlbuf.len >= sizeof (struct T_optmgmt_ack) &&
7877c478bd9Sstevel@tonic-gate 		    toa->PRIM_type == T_OPTMGMT_ACK &&
7887c478bd9Sstevel@tonic-gate 		    toa->MGMT_flags == T_SUCCESS &&
7897c478bd9Sstevel@tonic-gate 		    req->len == 0) {
790bd670b35SErik Nordmark 			if (Xflag)
7917c478bd9Sstevel@tonic-gate 				(void) printf("mibget getmsg() %d returned "
7927c478bd9Sstevel@tonic-gate 				    "EOD (level %ld, name %ld)\n",
7937c478bd9Sstevel@tonic-gate 				    j, req->level, req->name);
7947c478bd9Sstevel@tonic-gate 			return (first_item);		/* this is EOD msg */
7957c478bd9Sstevel@tonic-gate 		}
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate 		if (ctlbuf.len >= sizeof (struct T_error_ack) &&
7987c478bd9Sstevel@tonic-gate 		    tea->PRIM_type == T_ERROR_ACK) {
7997c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
8007c478bd9Sstevel@tonic-gate 			    "mibget %d gives T_ERROR_ACK: TLI_error = 0x%lx, "
8017c478bd9Sstevel@tonic-gate 			    "UNIX_error = 0x%lx\n",
8027c478bd9Sstevel@tonic-gate 			    j, tea->TLI_error, tea->UNIX_error);
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate 			errno = (tea->TLI_error == TSYSERR) ?
8057c478bd9Sstevel@tonic-gate 			    tea->UNIX_error : EPROTO;
8067c478bd9Sstevel@tonic-gate 			goto error_exit;
8077c478bd9Sstevel@tonic-gate 		}
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate 		if (getcode != MOREDATA ||
8107c478bd9Sstevel@tonic-gate 		    ctlbuf.len < sizeof (struct T_optmgmt_ack) ||
8117c478bd9Sstevel@tonic-gate 		    toa->PRIM_type != T_OPTMGMT_ACK ||
8127c478bd9Sstevel@tonic-gate 		    toa->MGMT_flags != T_SUCCESS) {
8137c478bd9Sstevel@tonic-gate 			(void) printf("mibget getmsg(ctl) %d returned %d, "
8147c478bd9Sstevel@tonic-gate 			    "ctlbuf.len = %d, PRIM_type = %ld\n",
8157c478bd9Sstevel@tonic-gate 			    j, getcode, ctlbuf.len, toa->PRIM_type);
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate 			if (toa->PRIM_type == T_OPTMGMT_ACK)
8187c478bd9Sstevel@tonic-gate 				(void) printf("T_OPTMGMT_ACK: "
8197c478bd9Sstevel@tonic-gate 				    "MGMT_flags = 0x%lx, req->len = %ld\n",
8207c478bd9Sstevel@tonic-gate 				    toa->MGMT_flags, req->len);
8217c478bd9Sstevel@tonic-gate 			errno = ENOMSG;
8227c478bd9Sstevel@tonic-gate 			goto error_exit;
8237c478bd9Sstevel@tonic-gate 		}
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate 		temp = (mib_item_t *)malloc(sizeof (mib_item_t));
8267c478bd9Sstevel@tonic-gate 		if (temp == NULL) {
8277c478bd9Sstevel@tonic-gate 			perror("mibget malloc failed");
8287c478bd9Sstevel@tonic-gate 			goto error_exit;
8297c478bd9Sstevel@tonic-gate 		}
8307c478bd9Sstevel@tonic-gate 		if (last_item != NULL)
8317c478bd9Sstevel@tonic-gate 			last_item->next_item = temp;
8327c478bd9Sstevel@tonic-gate 		else
8337c478bd9Sstevel@tonic-gate 			first_item = temp;
8347c478bd9Sstevel@tonic-gate 		last_item = temp;
8357c478bd9Sstevel@tonic-gate 		last_item->next_item = NULL;
8367c478bd9Sstevel@tonic-gate 		last_item->group = req->level;
8377c478bd9Sstevel@tonic-gate 		last_item->mib_id = req->name;
8387c478bd9Sstevel@tonic-gate 		last_item->length = req->len;
8397c478bd9Sstevel@tonic-gate 		last_item->valp = malloc((int)req->len);
8407c478bd9Sstevel@tonic-gate 		if (last_item->valp == NULL)
8417c478bd9Sstevel@tonic-gate 			goto error_exit;
842bd670b35SErik Nordmark 		if (Xflag)
8437c478bd9Sstevel@tonic-gate 			(void) printf("msg %d: group = %4d   mib_id = %5d"
8447c478bd9Sstevel@tonic-gate 			    "length = %d\n",
8457c478bd9Sstevel@tonic-gate 			    j, last_item->group, last_item->mib_id,
8467c478bd9Sstevel@tonic-gate 			    last_item->length);
8477c478bd9Sstevel@tonic-gate 
8487c478bd9Sstevel@tonic-gate 		databuf.maxlen = last_item->length;
8497c478bd9Sstevel@tonic-gate 		databuf.buf    = (char *)last_item->valp;
8507c478bd9Sstevel@tonic-gate 		databuf.len    = 0;
8517c478bd9Sstevel@tonic-gate 		flags = 0;
8527c478bd9Sstevel@tonic-gate 		getcode = getmsg(sd, (struct strbuf *)0, &databuf, &flags);
8537c478bd9Sstevel@tonic-gate 		if (getcode == -1) {
8547c478bd9Sstevel@tonic-gate 			perror("mibget getmsg(data) failed");
8557c478bd9Sstevel@tonic-gate 			goto error_exit;
8567c478bd9Sstevel@tonic-gate 		} else if (getcode != 0) {
8577c478bd9Sstevel@tonic-gate 			(void) printf("mibget getmsg(data) returned %d, "
8587c478bd9Sstevel@tonic-gate 			    "databuf.maxlen = %d, databuf.len = %d\n",
8597c478bd9Sstevel@tonic-gate 			    getcode, databuf.maxlen, databuf.len);
8607c478bd9Sstevel@tonic-gate 			goto error_exit;
8617c478bd9Sstevel@tonic-gate 		}
8627c478bd9Sstevel@tonic-gate 		j++;
8637c478bd9Sstevel@tonic-gate 	}
8647c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
8657c478bd9Sstevel@tonic-gate 
8667c478bd9Sstevel@tonic-gate error_exit:;
8677c478bd9Sstevel@tonic-gate 	mibfree(first_item);
8687c478bd9Sstevel@tonic-gate 	return (NULL);
8697c478bd9Sstevel@tonic-gate }
8707c478bd9Sstevel@tonic-gate 
8717c478bd9Sstevel@tonic-gate /*
8727c478bd9Sstevel@tonic-gate  * mibfree: frees a linked list of type (mib_item_t *)
8737c478bd9Sstevel@tonic-gate  * returned by mibget(); this is NOT THE SAME AS
8747c478bd9Sstevel@tonic-gate  * mib_item_destroy(), so should be used for objects
8757c478bd9Sstevel@tonic-gate  * returned by mibget() only
8767c478bd9Sstevel@tonic-gate  */
8777c478bd9Sstevel@tonic-gate static void
8787c478bd9Sstevel@tonic-gate mibfree(mib_item_t *firstitem)
8797c478bd9Sstevel@tonic-gate {
8807c478bd9Sstevel@tonic-gate 	mib_item_t *lastitem;
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate 	while (firstitem != NULL) {
8837c478bd9Sstevel@tonic-gate 		lastitem = firstitem;
8847c478bd9Sstevel@tonic-gate 		firstitem = firstitem->next_item;
8857c478bd9Sstevel@tonic-gate 		if (lastitem->valp != NULL)
8867c478bd9Sstevel@tonic-gate 			free(lastitem->valp);
8877c478bd9Sstevel@tonic-gate 		free(lastitem);
8887c478bd9Sstevel@tonic-gate 	}
8897c478bd9Sstevel@tonic-gate }
8907c478bd9Sstevel@tonic-gate 
8917c478bd9Sstevel@tonic-gate static int
8927c478bd9Sstevel@tonic-gate mibopen(void)
8937c478bd9Sstevel@tonic-gate {
8947c478bd9Sstevel@tonic-gate 	int	sd;
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate 	sd = open("/dev/arp", O_RDWR);
8977c478bd9Sstevel@tonic-gate 	if (sd == -1) {
8987c478bd9Sstevel@tonic-gate 		perror("arp open");
8997c478bd9Sstevel@tonic-gate 		return (-1);
9007c478bd9Sstevel@tonic-gate 	}
9017c478bd9Sstevel@tonic-gate 	if (ioctl(sd, I_PUSH, "tcp") == -1) {
9027c478bd9Sstevel@tonic-gate 		perror("tcp I_PUSH");
9037c478bd9Sstevel@tonic-gate 		(void) close(sd);
9047c478bd9Sstevel@tonic-gate 		return (-1);
9057c478bd9Sstevel@tonic-gate 	}
9067c478bd9Sstevel@tonic-gate 	if (ioctl(sd, I_PUSH, "udp") == -1) {
9077c478bd9Sstevel@tonic-gate 		perror("udp I_PUSH");
9087c478bd9Sstevel@tonic-gate 		(void) close(sd);
9097c478bd9Sstevel@tonic-gate 		return (-1);
9107c478bd9Sstevel@tonic-gate 	}
9117c478bd9Sstevel@tonic-gate 	if (ioctl(sd, I_PUSH, "icmp") == -1) {
9127c478bd9Sstevel@tonic-gate 		perror("icmp I_PUSH");
9137c478bd9Sstevel@tonic-gate 		(void) close(sd);
9147c478bd9Sstevel@tonic-gate 		return (-1);
9157c478bd9Sstevel@tonic-gate 	}
9167c478bd9Sstevel@tonic-gate 	return (sd);
9177c478bd9Sstevel@tonic-gate }
9187c478bd9Sstevel@tonic-gate 
9197c478bd9Sstevel@tonic-gate /*
9207c478bd9Sstevel@tonic-gate  * mib_item_dup: returns a clean mib_item_t * linked
9217c478bd9Sstevel@tonic-gate  * list, so that for every element item->mib_id is 0;
9227c478bd9Sstevel@tonic-gate  * to deallocate this linked list, use mib_item_destroy
9237c478bd9Sstevel@tonic-gate  */
9247c478bd9Sstevel@tonic-gate static mib_item_t *
9257c478bd9Sstevel@tonic-gate mib_item_dup(mib_item_t *item)
9267c478bd9Sstevel@tonic-gate {
9277c478bd9Sstevel@tonic-gate 	int	c = 0;
9287c478bd9Sstevel@tonic-gate 	mib_item_t *localp;
9297c478bd9Sstevel@tonic-gate 	mib_item_t *tempp;
9307c478bd9Sstevel@tonic-gate 
9317c478bd9Sstevel@tonic-gate 	for (tempp = item; tempp; tempp = tempp->next_item)
9327c478bd9Sstevel@tonic-gate 		if (tempp->mib_id == 0)
9337c478bd9Sstevel@tonic-gate 			c++;
9347c478bd9Sstevel@tonic-gate 	tempp = NULL;
9357c478bd9Sstevel@tonic-gate 
9367c478bd9Sstevel@tonic-gate 	localp = (mib_item_t *)malloc(c * sizeof (mib_item_t));
9377c478bd9Sstevel@tonic-gate 	if (localp == NULL)
9387c478bd9Sstevel@tonic-gate 		return (NULL);
9397c478bd9Sstevel@tonic-gate 	c = 0;
9407c478bd9Sstevel@tonic-gate 	for (; item; item = item->next_item) {
9417c478bd9Sstevel@tonic-gate 		if (item->mib_id == 0) {
9427c478bd9Sstevel@tonic-gate 			/* Replicate item in localp */
9437c478bd9Sstevel@tonic-gate 			(localp[c]).next_item = NULL;
9447c478bd9Sstevel@tonic-gate 			(localp[c]).group = item->group;
9457c478bd9Sstevel@tonic-gate 			(localp[c]).mib_id = item->mib_id;
9467c478bd9Sstevel@tonic-gate 			(localp[c]).length = item->length;
9477c478bd9Sstevel@tonic-gate 			(localp[c]).valp = (uintptr_t *)malloc(
9487c478bd9Sstevel@tonic-gate 			    item->length);
9497c478bd9Sstevel@tonic-gate 			if ((localp[c]).valp == NULL) {
9507c478bd9Sstevel@tonic-gate 				mib_item_destroy(&localp);
9517c478bd9Sstevel@tonic-gate 				return (NULL);
9527c478bd9Sstevel@tonic-gate 			}
9537c478bd9Sstevel@tonic-gate 			(void *) memcpy((localp[c]).valp,
9547c478bd9Sstevel@tonic-gate 			    item->valp,
9557c478bd9Sstevel@tonic-gate 			    item->length);
9567c478bd9Sstevel@tonic-gate 			tempp = &(localp[c]);
9577c478bd9Sstevel@tonic-gate 			if (c > 0)
9587c478bd9Sstevel@tonic-gate 				(localp[c - 1]).next_item = tempp;
9597c478bd9Sstevel@tonic-gate 			c++;
9607c478bd9Sstevel@tonic-gate 		}
9617c478bd9Sstevel@tonic-gate 	}
9627c478bd9Sstevel@tonic-gate 	return (localp);
9637c478bd9Sstevel@tonic-gate }
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate /*
9667c478bd9Sstevel@tonic-gate  * mib_item_diff: takes two (mib_item_t *) linked lists
9677c478bd9Sstevel@tonic-gate  * item1 and item2 and computes the difference between
9687c478bd9Sstevel@tonic-gate  * differentiable values in item2 against item1 for every
9697c478bd9Sstevel@tonic-gate  * given member of item2; returns an mib_item_t * linked
9707c478bd9Sstevel@tonic-gate  * list of diff's, or a copy of item2 if item1 is NULL;
9717c478bd9Sstevel@tonic-gate  * will return NULL if system out of memory; works only
9727c478bd9Sstevel@tonic-gate  * for item->mib_id == 0
9737c478bd9Sstevel@tonic-gate  */
9747c478bd9Sstevel@tonic-gate static mib_item_t *
9757c478bd9Sstevel@tonic-gate mib_item_diff(mib_item_t *item1, mib_item_t *item2) {
9767c478bd9Sstevel@tonic-gate 	int	nitems	= 0; /* no. of items in item2 */
9777c478bd9Sstevel@tonic-gate 	mib_item_t *tempp2;  /* walking copy of item2 */
9787c478bd9Sstevel@tonic-gate 	mib_item_t *tempp1;  /* walking copy of item1 */
9797c478bd9Sstevel@tonic-gate 	mib_item_t *diffp;
9807c478bd9Sstevel@tonic-gate 	mib_item_t *diffptr; /* walking copy of diffp */
9817c478bd9Sstevel@tonic-gate 	mib_item_t *prevp = NULL;
9827c478bd9Sstevel@tonic-gate 
9837c478bd9Sstevel@tonic-gate 	if (item1 == NULL) {
9847c478bd9Sstevel@tonic-gate 		diffp = mib_item_dup(item2);
9857c478bd9Sstevel@tonic-gate 		return (diffp);
9867c478bd9Sstevel@tonic-gate 	}
9877c478bd9Sstevel@tonic-gate 
9887c478bd9Sstevel@tonic-gate 	for (tempp2 = item2;
9897c478bd9Sstevel@tonic-gate 	    tempp2;
9907c478bd9Sstevel@tonic-gate 	    tempp2 = tempp2->next_item) {
9917c478bd9Sstevel@tonic-gate 		if (tempp2->mib_id == 0)
9927c478bd9Sstevel@tonic-gate 			switch (tempp2->group) {
9937c478bd9Sstevel@tonic-gate 			/*
9947c478bd9Sstevel@tonic-gate 			 * upon adding a case here, the same
9957c478bd9Sstevel@tonic-gate 			 * must also be added in the next
9967c478bd9Sstevel@tonic-gate 			 * switch statement, alongwith
9977c478bd9Sstevel@tonic-gate 			 * appropriate code
9987c478bd9Sstevel@tonic-gate 			 */
9997c478bd9Sstevel@tonic-gate 			case MIB2_IP:
10007c478bd9Sstevel@tonic-gate 			case MIB2_IP6:
10017c478bd9Sstevel@tonic-gate 			case EXPER_DVMRP:
10027c478bd9Sstevel@tonic-gate 			case EXPER_IGMP:
10037c478bd9Sstevel@tonic-gate 			case MIB2_ICMP:
10047c478bd9Sstevel@tonic-gate 			case MIB2_ICMP6:
10057c478bd9Sstevel@tonic-gate 			case MIB2_TCP:
10067c478bd9Sstevel@tonic-gate 			case MIB2_UDP:
10077c478bd9Sstevel@tonic-gate 			case MIB2_SCTP:
10087c478bd9Sstevel@tonic-gate 			case EXPER_RAWIP:
10097c478bd9Sstevel@tonic-gate 				nitems++;
10107c478bd9Sstevel@tonic-gate 			}
10117c478bd9Sstevel@tonic-gate 	}
10127c478bd9Sstevel@tonic-gate 	tempp2 = NULL;
10137c478bd9Sstevel@tonic-gate 	if (nitems == 0) {
10147c478bd9Sstevel@tonic-gate 		diffp = mib_item_dup(item2);
10157c478bd9Sstevel@tonic-gate 		return (diffp);
10167c478bd9Sstevel@tonic-gate 	}
10177c478bd9Sstevel@tonic-gate 
10187c478bd9Sstevel@tonic-gate 	diffp = (mib_item_t *)calloc(nitems, sizeof (mib_item_t));
10197c478bd9Sstevel@tonic-gate 	if (diffp == NULL)
10207c478bd9Sstevel@tonic-gate 		return (NULL);
10217c478bd9Sstevel@tonic-gate 	diffptr = diffp;
10227c478bd9Sstevel@tonic-gate 	/* 'for' loop 1: */
10237c478bd9Sstevel@tonic-gate 	for (tempp2 = item2; tempp2 != NULL; tempp2 = tempp2->next_item) {
10247c478bd9Sstevel@tonic-gate 		if (tempp2->mib_id != 0)
10257c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
10267c478bd9Sstevel@tonic-gate 		/* 'for' loop 2: */
10277c478bd9Sstevel@tonic-gate 		for (tempp1 = item1; tempp1 != NULL;
10287c478bd9Sstevel@tonic-gate 		    tempp1 = tempp1->next_item) {
10297c478bd9Sstevel@tonic-gate 			if (!(tempp1->mib_id == 0 &&
10307c478bd9Sstevel@tonic-gate 			    tempp1->group == tempp2->group &&
10317c478bd9Sstevel@tonic-gate 			    tempp1->mib_id == tempp2->mib_id))
10327c478bd9Sstevel@tonic-gate 				continue; /* 'for' loop 2 */
10337c478bd9Sstevel@tonic-gate 			/* found comparable data sets */
10347c478bd9Sstevel@tonic-gate 			if (prevp != NULL)
10357c478bd9Sstevel@tonic-gate 				prevp->next_item = diffptr;
10367c478bd9Sstevel@tonic-gate 			switch (tempp2->group) {
10377c478bd9Sstevel@tonic-gate 			/*
10387c478bd9Sstevel@tonic-gate 			 * Indenting note: Because of long variable names
10397c478bd9Sstevel@tonic-gate 			 * in cases MIB2_IP6 and MIB2_ICMP6, their contents
10407c478bd9Sstevel@tonic-gate 			 * have been indented by one tab space only
10417c478bd9Sstevel@tonic-gate 			 */
10427c478bd9Sstevel@tonic-gate 			case MIB2_IP: {
10437c478bd9Sstevel@tonic-gate 				mib2_ip_t *i2 = (mib2_ip_t *)tempp2->valp;
10447c478bd9Sstevel@tonic-gate 				mib2_ip_t *i1 = (mib2_ip_t *)tempp1->valp;
10457c478bd9Sstevel@tonic-gate 				mib2_ip_t *d;
10467c478bd9Sstevel@tonic-gate 
10477c478bd9Sstevel@tonic-gate 				diffptr->group = tempp2->group;
10487c478bd9Sstevel@tonic-gate 				diffptr->mib_id = tempp2->mib_id;
10497c478bd9Sstevel@tonic-gate 				diffptr->length = tempp2->length;
10507c478bd9Sstevel@tonic-gate 				d = (mib2_ip_t *)calloc(tempp2->length, 1);
10517c478bd9Sstevel@tonic-gate 				if (d == NULL)
10527c478bd9Sstevel@tonic-gate 					goto mibdiff_out_of_memory;
10537c478bd9Sstevel@tonic-gate 				diffptr->valp = d;
10547c478bd9Sstevel@tonic-gate 				d->ipForwarding = i2->ipForwarding;
10557c478bd9Sstevel@tonic-gate 				d->ipDefaultTTL = i2->ipDefaultTTL;
10567c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipInReceives);
10577c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipInHdrErrors);
10587c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipInAddrErrors);
10597c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipInCksumErrs);
10607c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipForwDatagrams);
10617c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipForwProhibits);
10627c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipInUnknownProtos);
10637c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipInDiscards);
10647c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipInDelivers);
10657c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipOutRequests);
10667c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipOutDiscards);
10677c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipOutNoRoutes);
10687c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipReasmTimeout);
10697c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipReasmReqds);
10707c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipReasmOKs);
10717c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipReasmFails);
10727c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipReasmDuplicates);
10737c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipReasmPartDups);
10747c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipFragOKs);
10757c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipFragFails);
10767c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipFragCreates);
10777c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipRoutingDiscards);
10787c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, tcpInErrs);
10797c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, udpNoPorts);
10807c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, udpInCksumErrs);
10817c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, udpInOverflows);
10827c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, rawipInOverflows);
10837c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipsecInSucceeded);
10847c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipsecInFailed);
10857c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipInIPv6);
10867c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipOutIPv6);
10877c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipOutSwitchIPv6);
10887c478bd9Sstevel@tonic-gate 				prevp = diffptr++;
10897c478bd9Sstevel@tonic-gate 				break;
10907c478bd9Sstevel@tonic-gate 			}
10917c478bd9Sstevel@tonic-gate 			case MIB2_IP6: {
10927c478bd9Sstevel@tonic-gate 			mib2_ipv6IfStatsEntry_t *i2;
10937c478bd9Sstevel@tonic-gate 			mib2_ipv6IfStatsEntry_t *i1;
10947c478bd9Sstevel@tonic-gate 			mib2_ipv6IfStatsEntry_t *d;
10957c478bd9Sstevel@tonic-gate 
10967c478bd9Sstevel@tonic-gate 			i2 = (mib2_ipv6IfStatsEntry_t *)tempp2->valp;
10977c478bd9Sstevel@tonic-gate 			i1 = (mib2_ipv6IfStatsEntry_t *)tempp1->valp;
10987c478bd9Sstevel@tonic-gate 			diffptr->group = tempp2->group;
10997c478bd9Sstevel@tonic-gate 			diffptr->mib_id = tempp2->mib_id;
11007c478bd9Sstevel@tonic-gate 			diffptr->length = tempp2->length;
11017c478bd9Sstevel@tonic-gate 			d = (mib2_ipv6IfStatsEntry_t *)calloc(
11027c478bd9Sstevel@tonic-gate 			    tempp2->length, 1);
11037c478bd9Sstevel@tonic-gate 			if (d == NULL)
11047c478bd9Sstevel@tonic-gate 				goto mibdiff_out_of_memory;
11057c478bd9Sstevel@tonic-gate 			diffptr->valp = d;
11067c478bd9Sstevel@tonic-gate 			d->ipv6Forwarding = i2->ipv6Forwarding;
11077c478bd9Sstevel@tonic-gate 			d->ipv6DefaultHopLimit =
11087c478bd9Sstevel@tonic-gate 			    i2->ipv6DefaultHopLimit;
11097c478bd9Sstevel@tonic-gate 
11107c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6InReceives);
11117c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6InHdrErrors);
11127c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6InTooBigErrors);
11137c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6InNoRoutes);
11147c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6InAddrErrors);
11157c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6InUnknownProtos);
11167c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6InTruncatedPkts);
11177c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6InDiscards);
11187c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6InDelivers);
11197c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6OutForwDatagrams);
11207c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6OutRequests);
11217c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6OutDiscards);
11227c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6OutNoRoutes);
11237c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6OutFragOKs);
11247c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6OutFragFails);
11257c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6OutFragCreates);
11267c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6ReasmReqds);
11277c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6ReasmOKs);
11287c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6ReasmFails);
11297c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6InMcastPkts);
11307c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6OutMcastPkts);
11317c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6ReasmDuplicates);
11327c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6ReasmPartDups);
11337c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6ForwProhibits);
11347c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, udpInCksumErrs);
11357c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, udpInOverflows);
11367c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, rawipInOverflows);
11377c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6InIPv4);
11387c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6OutIPv4);
11397c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6OutSwitchIPv4);
11407c478bd9Sstevel@tonic-gate 			prevp = diffptr++;
11417c478bd9Sstevel@tonic-gate 			break;
11427c478bd9Sstevel@tonic-gate 			}
11437c478bd9Sstevel@tonic-gate 			case EXPER_DVMRP: {
11447c478bd9Sstevel@tonic-gate 				struct mrtstat *m2;
11457c478bd9Sstevel@tonic-gate 				struct mrtstat *m1;
11467c478bd9Sstevel@tonic-gate 				struct mrtstat *d;
11477c478bd9Sstevel@tonic-gate 
11487c478bd9Sstevel@tonic-gate 				m2 = (struct mrtstat *)tempp2->valp;
11497c478bd9Sstevel@tonic-gate 				m1 = (struct mrtstat *)tempp1->valp;
11507c478bd9Sstevel@tonic-gate 				diffptr->group = tempp2->group;
11517c478bd9Sstevel@tonic-gate 				diffptr->mib_id = tempp2->mib_id;
11527c478bd9Sstevel@tonic-gate 				diffptr->length = tempp2->length;
11537c478bd9Sstevel@tonic-gate 				d = (struct mrtstat *)calloc(tempp2->length, 1);
11547c478bd9Sstevel@tonic-gate 				if (d == NULL)
11557c478bd9Sstevel@tonic-gate 					goto mibdiff_out_of_memory;
11567c478bd9Sstevel@tonic-gate 				diffptr->valp = d;
11577c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_mfc_hits);
11587c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_mfc_misses);
11597c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_fwd_in);
11607c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_fwd_out);
11617c478bd9Sstevel@tonic-gate 				d->mrts_upcalls = m2->mrts_upcalls;
11627c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_fwd_drop);
11637c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_bad_tunnel);
11647c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_cant_tunnel);
11657c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_wrong_if);
11667c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_upq_ovflw);
11677c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_cache_cleanups);
11687c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_drop_sel);
11697c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_q_overflow);
11707c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_pkt2large);
11717c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_pim_badversion);
11727c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_pim_rcv_badcsum);
11737c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_pim_badregisters);
11747c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_pim_regforwards);
11757c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_pim_regsend_drops);
11767c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_pim_malformed);
11777c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_pim_nomemory);
11787c478bd9Sstevel@tonic-gate 				prevp = diffptr++;
11797c478bd9Sstevel@tonic-gate 				break;
11807c478bd9Sstevel@tonic-gate 			}
11817c478bd9Sstevel@tonic-gate 			case EXPER_IGMP: {
11827c478bd9Sstevel@tonic-gate 				struct igmpstat *i2;
11837c478bd9Sstevel@tonic-gate 				struct igmpstat *i1;
11847c478bd9Sstevel@tonic-gate 				struct igmpstat *d;
11857c478bd9Sstevel@tonic-gate 
11867c478bd9Sstevel@tonic-gate 				i2 = (struct igmpstat *)tempp2->valp;
11877c478bd9Sstevel@tonic-gate 				i1 = (struct igmpstat *)tempp1->valp;
11887c478bd9Sstevel@tonic-gate 				diffptr->group = tempp2->group;
11897c478bd9Sstevel@tonic-gate 				diffptr->mib_id = tempp2->mib_id;
11907c478bd9Sstevel@tonic-gate 				diffptr->length = tempp2->length;
11917c478bd9Sstevel@tonic-gate 				d = (struct igmpstat *)calloc(
11927c478bd9Sstevel@tonic-gate 				    tempp2->length, 1);
11937c478bd9Sstevel@tonic-gate 				if (d == NULL)
11947c478bd9Sstevel@tonic-gate 					goto mibdiff_out_of_memory;
11957c478bd9Sstevel@tonic-gate 				diffptr->valp = d;
11967c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, igps_rcv_total);
11977c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, igps_rcv_tooshort);
11987c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, igps_rcv_badsum);
11997c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, igps_rcv_queries);
12007c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, igps_rcv_badqueries);
12017c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, igps_rcv_reports);
12027c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, igps_rcv_badreports);
12037c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, igps_rcv_ourreports);
12047c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, igps_snd_reports);
12057c478bd9Sstevel@tonic-gate 				prevp = diffptr++;
12067c478bd9Sstevel@tonic-gate 				break;
12077c478bd9Sstevel@tonic-gate 			}
12087c478bd9Sstevel@tonic-gate 			case MIB2_ICMP: {
12097c478bd9Sstevel@tonic-gate 				mib2_icmp_t *i2;
12107c478bd9Sstevel@tonic-gate 				mib2_icmp_t *i1;
12117c478bd9Sstevel@tonic-gate 				mib2_icmp_t *d;
12127c478bd9Sstevel@tonic-gate 
12137c478bd9Sstevel@tonic-gate 				i2 = (mib2_icmp_t *)tempp2->valp;
12147c478bd9Sstevel@tonic-gate 				i1 = (mib2_icmp_t *)tempp1->valp;
12157c478bd9Sstevel@tonic-gate 				diffptr->group = tempp2->group;
12167c478bd9Sstevel@tonic-gate 				diffptr->mib_id = tempp2->mib_id;
12177c478bd9Sstevel@tonic-gate 				diffptr->length = tempp2->length;
12187c478bd9Sstevel@tonic-gate 				d = (mib2_icmp_t *)calloc(tempp2->length, 1);
12197c478bd9Sstevel@tonic-gate 				if (d == NULL)
12207c478bd9Sstevel@tonic-gate 					goto mibdiff_out_of_memory;
12217c478bd9Sstevel@tonic-gate 				diffptr->valp = d;
12227c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInMsgs);
12237c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInErrors);
12247c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInCksumErrs);
12257c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInUnknowns);
12267c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInDestUnreachs);
12277c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInTimeExcds);
12287c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInParmProbs);
12297c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInSrcQuenchs);
12307c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInRedirects);
12317c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInBadRedirects);
12327c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInEchos);
12337c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInEchoReps);
12347c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInTimestamps);
12357c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInAddrMasks);
12367c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInAddrMaskReps);
12377c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInFragNeeded);
12387c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutMsgs);
12397c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutDrops);
12407c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutErrors);
12417c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutDestUnreachs);
12427c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutTimeExcds);
12437c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutParmProbs);
12447c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutSrcQuenchs);
12457c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutRedirects);
12467c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutEchos);
12477c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutEchoReps);
12487c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutTimestamps);
12497c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutTimestampReps);
12507c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutAddrMasks);
12517c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutAddrMaskReps);
12527c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutFragNeeded);
12537c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInOverflows);
12547c478bd9Sstevel@tonic-gate 				prevp = diffptr++;
12557c478bd9Sstevel@tonic-gate 				break;
12567c478bd9Sstevel@tonic-gate 			}
12577c478bd9Sstevel@tonic-gate 			case MIB2_ICMP6: {
12587c478bd9Sstevel@tonic-gate 	mib2_ipv6IfIcmpEntry_t *i2;
12597c478bd9Sstevel@tonic-gate 	mib2_ipv6IfIcmpEntry_t *i1;
12607c478bd9Sstevel@tonic-gate 	mib2_ipv6IfIcmpEntry_t *d;
12617c478bd9Sstevel@tonic-gate 
12627c478bd9Sstevel@tonic-gate 	i2 = (mib2_ipv6IfIcmpEntry_t *)tempp2->valp;
12637c478bd9Sstevel@tonic-gate 	i1 = (mib2_ipv6IfIcmpEntry_t *)tempp1->valp;
12647c478bd9Sstevel@tonic-gate 	diffptr->group = tempp2->group;
12657c478bd9Sstevel@tonic-gate 	diffptr->mib_id = tempp2->mib_id;
12667c478bd9Sstevel@tonic-gate 	diffptr->length = tempp2->length;
12677c478bd9Sstevel@tonic-gate 	d = (mib2_ipv6IfIcmpEntry_t *)calloc(tempp2->length, 1);
12687c478bd9Sstevel@tonic-gate 	if (d == NULL)
12697c478bd9Sstevel@tonic-gate 		goto mibdiff_out_of_memory;
12707c478bd9Sstevel@tonic-gate 	diffptr->valp = d;
12717c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInMsgs);
12727c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInErrors);
12737c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInDestUnreachs);
12747c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInAdminProhibs);
12757c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInTimeExcds);
12767c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInParmProblems);
12777c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInPktTooBigs);
12787c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInEchos);
12797c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInEchoReplies);
12807c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInRouterSolicits);
12817c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInRouterAdvertisements);
12827c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInNeighborSolicits);
12837c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInNeighborAdvertisements);
12847c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInRedirects);
12857c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInBadRedirects);
12867c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInGroupMembQueries);
12877c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInGroupMembResponses);
12887c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInGroupMembReductions);
12897c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInOverflows);
12907c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutMsgs);
12917c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutErrors);
12927c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutDestUnreachs);
12937c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutAdminProhibs);
12947c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutTimeExcds);
12957c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutParmProblems);
12967c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutPktTooBigs);
12977c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutEchos);
12987c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutEchoReplies);
12997c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutRouterSolicits);
13007c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutRouterAdvertisements);
13017c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutNeighborSolicits);
13027c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutNeighborAdvertisements);
13037c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutRedirects);
13047c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutGroupMembQueries);
13057c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutGroupMembResponses);
13067c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutGroupMembReductions);
13077c478bd9Sstevel@tonic-gate 	prevp = diffptr++;
13087c478bd9Sstevel@tonic-gate 	break;
13097c478bd9Sstevel@tonic-gate 			}
13107c478bd9Sstevel@tonic-gate 			case MIB2_TCP: {
13117c478bd9Sstevel@tonic-gate 				mib2_tcp_t *t2;
13127c478bd9Sstevel@tonic-gate 				mib2_tcp_t *t1;
13137c478bd9Sstevel@tonic-gate 				mib2_tcp_t *d;
13147c478bd9Sstevel@tonic-gate 
13157c478bd9Sstevel@tonic-gate 				t2 = (mib2_tcp_t *)tempp2->valp;
13167c478bd9Sstevel@tonic-gate 				t1 = (mib2_tcp_t *)tempp1->valp;
13177c478bd9Sstevel@tonic-gate 				diffptr->group = tempp2->group;
13187c478bd9Sstevel@tonic-gate 				diffptr->mib_id = tempp2->mib_id;
13197c478bd9Sstevel@tonic-gate 				diffptr->length = tempp2->length;
13207c478bd9Sstevel@tonic-gate 				d = (mib2_tcp_t *)calloc(tempp2->length, 1);
13217c478bd9Sstevel@tonic-gate 				if (d == NULL)
13227c478bd9Sstevel@tonic-gate 					goto mibdiff_out_of_memory;
13237c478bd9Sstevel@tonic-gate 				diffptr->valp = d;
13247c478bd9Sstevel@tonic-gate 				d->tcpRtoMin = t2->tcpRtoMin;
13257c478bd9Sstevel@tonic-gate 				d->tcpRtoMax = t2->tcpRtoMax;
13267c478bd9Sstevel@tonic-gate 				d->tcpMaxConn = t2->tcpMaxConn;
13277c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpActiveOpens);
13287c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpPassiveOpens);
13297c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpAttemptFails);
13307c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpEstabResets);
13317c478bd9Sstevel@tonic-gate 				d->tcpCurrEstab = t2->tcpCurrEstab;
13323173664eSapersson 				MDIFF(d, t2, t1, tcpHCOutSegs);
13337c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpOutDataSegs);
13347c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpOutDataBytes);
13357c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpRetransSegs);
13367c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpRetransBytes);
13377c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpOutAck);
13387c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpOutAckDelayed);
13397c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpOutUrg);
13407c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpOutWinUpdate);
13417c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpOutWinProbe);
13427c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpOutControl);
13437c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpOutRsts);
13447c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpOutFastRetrans);
13453173664eSapersson 				MDIFF(d, t2, t1, tcpHCInSegs);
13467c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInAckSegs);
13477c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInAckBytes);
13487c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInDupAck);
13497c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInAckUnsent);
13507c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInDataInorderSegs);
13517c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInDataInorderBytes);
13527c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInDataUnorderSegs);
13537c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInDataUnorderBytes);
13547c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInDataDupSegs);
13557c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInDataDupBytes);
13567c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInDataPartDupSegs);
13577c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInDataPartDupBytes);
13587c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInDataPastWinSegs);
13597c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInDataPastWinBytes);
13607c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInWinProbe);
13617c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInWinUpdate);
13627c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInClosed);
13637c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpRttNoUpdate);
13647c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpRttUpdate);
13657c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpTimRetrans);
13667c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpTimRetransDrop);
13677c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpTimKeepalive);
13687c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpTimKeepaliveProbe);
13697c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpTimKeepaliveDrop);
13707c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpListenDrop);
13717c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpListenDropQ0);
13727c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpHalfOpenDrop);
13737c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpOutSackRetransSegs);
13747c478bd9Sstevel@tonic-gate 				prevp = diffptr++;
13757c478bd9Sstevel@tonic-gate 				break;
13767c478bd9Sstevel@tonic-gate 			}
13777c478bd9Sstevel@tonic-gate 			case MIB2_UDP: {
13787c478bd9Sstevel@tonic-gate 				mib2_udp_t *u2;
13797c478bd9Sstevel@tonic-gate 				mib2_udp_t *u1;
13807c478bd9Sstevel@tonic-gate 				mib2_udp_t *d;
13817c478bd9Sstevel@tonic-gate 
13827c478bd9Sstevel@tonic-gate 				u2 = (mib2_udp_t *)tempp2->valp;
13837c478bd9Sstevel@tonic-gate 				u1 = (mib2_udp_t *)tempp1->valp;
13847c478bd9Sstevel@tonic-gate 				diffptr->group = tempp2->group;
13857c478bd9Sstevel@tonic-gate 				diffptr->mib_id = tempp2->mib_id;
13867c478bd9Sstevel@tonic-gate 				diffptr->length = tempp2->length;
13877c478bd9Sstevel@tonic-gate 				d = (mib2_udp_t *)calloc(tempp2->length, 1);
13887c478bd9Sstevel@tonic-gate 				if (d == NULL)
13897c478bd9Sstevel@tonic-gate 					goto mibdiff_out_of_memory;
13907c478bd9Sstevel@tonic-gate 				diffptr->valp = d;
13913173664eSapersson 				MDIFF(d, u2, u1, udpHCInDatagrams);
13927c478bd9Sstevel@tonic-gate 				MDIFF(d, u2, u1, udpInErrors);
13933173664eSapersson 				MDIFF(d, u2, u1, udpHCOutDatagrams);
13947c478bd9Sstevel@tonic-gate 				MDIFF(d, u2, u1, udpOutErrors);
13957c478bd9Sstevel@tonic-gate 				prevp = diffptr++;
13967c478bd9Sstevel@tonic-gate 				break;
13977c478bd9Sstevel@tonic-gate 			}
13987c478bd9Sstevel@tonic-gate 			case MIB2_SCTP: {
13997c478bd9Sstevel@tonic-gate 				mib2_sctp_t *s2;
14007c478bd9Sstevel@tonic-gate 				mib2_sctp_t *s1;
14017c478bd9Sstevel@tonic-gate 				mib2_sctp_t *d;
14027c478bd9Sstevel@tonic-gate 
14037c478bd9Sstevel@tonic-gate 				s2 = (mib2_sctp_t *)tempp2->valp;
14047c478bd9Sstevel@tonic-gate 				s1 = (mib2_sctp_t *)tempp1->valp;
14057c478bd9Sstevel@tonic-gate 				diffptr->group = tempp2->group;
14067c478bd9Sstevel@tonic-gate 				diffptr->mib_id = tempp2->mib_id;
14077c478bd9Sstevel@tonic-gate 				diffptr->length = tempp2->length;
14087c478bd9Sstevel@tonic-gate 				d = (mib2_sctp_t *)calloc(tempp2->length, 1);
14097c478bd9Sstevel@tonic-gate 				if (d == NULL)
14107c478bd9Sstevel@tonic-gate 					goto mibdiff_out_of_memory;
14117c478bd9Sstevel@tonic-gate 				diffptr->valp = d;
14127c478bd9Sstevel@tonic-gate 				d->sctpRtoAlgorithm = s2->sctpRtoAlgorithm;
14137c478bd9Sstevel@tonic-gate 				d->sctpRtoMin = s2->sctpRtoMin;
14147c478bd9Sstevel@tonic-gate 				d->sctpRtoMax = s2->sctpRtoMax;
14157c478bd9Sstevel@tonic-gate 				d->sctpRtoInitial = s2->sctpRtoInitial;
14167c478bd9Sstevel@tonic-gate 				d->sctpMaxAssocs = s2->sctpMaxAssocs;
14177c478bd9Sstevel@tonic-gate 				d->sctpValCookieLife = s2->sctpValCookieLife;
14187c478bd9Sstevel@tonic-gate 				d->sctpMaxInitRetr = s2->sctpMaxInitRetr;
14197c478bd9Sstevel@tonic-gate 				d->sctpCurrEstab = s2->sctpCurrEstab;
14207c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpActiveEstab);
14217c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpPassiveEstab);
14227c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpAborted);
14237c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpShutdowns);
14247c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpOutOfBlue);
14257c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpChecksumError);
14267c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpOutCtrlChunks);
14277c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpOutOrderChunks);
14287c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpOutUnorderChunks);
14297c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpRetransChunks);
14307c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpOutAck);
14317c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpOutAckDelayed);
14327c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpOutWinUpdate);
14337c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpOutFastRetrans);
14347c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpOutWinProbe);
14357c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpInCtrlChunks);
14367c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpInOrderChunks);
14377c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpInUnorderChunks);
14387c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpInAck);
14397c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpInDupAck);
14407c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpInAckUnsent);
14417c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpFragUsrMsgs);
14427c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpReasmUsrMsgs);
14437c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpOutSCTPPkts);
14447c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpInSCTPPkts);
14457c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpInInvalidCookie);
14467c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpTimRetrans);
14477c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpTimRetransDrop);
14487c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpTimHeartBeatProbe);
14497c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpTimHeartBeatDrop);
14507c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpListenDrop);
14517c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpInClosed);
14527c478bd9Sstevel@tonic-gate 				prevp = diffptr++;
14537c478bd9Sstevel@tonic-gate 				break;
14547c478bd9Sstevel@tonic-gate 			}
14557c478bd9Sstevel@tonic-gate 			case EXPER_RAWIP: {
14567c478bd9Sstevel@tonic-gate 				mib2_rawip_t *r2;
14577c478bd9Sstevel@tonic-gate 				mib2_rawip_t *r1;
14587c478bd9Sstevel@tonic-gate 				mib2_rawip_t *d;
14597c478bd9Sstevel@tonic-gate 
14607c478bd9Sstevel@tonic-gate 				r2 = (mib2_rawip_t *)tempp2->valp;
14617c478bd9Sstevel@tonic-gate 				r1 = (mib2_rawip_t *)tempp1->valp;
14627c478bd9Sstevel@tonic-gate 				diffptr->group = tempp2->group;
14637c478bd9Sstevel@tonic-gate 				diffptr->mib_id = tempp2->mib_id;
14647c478bd9Sstevel@tonic-gate 				diffptr->length = tempp2->length;
14657c478bd9Sstevel@tonic-gate 				d = (mib2_rawip_t *)calloc(tempp2->length, 1);
14667c478bd9Sstevel@tonic-gate 				if (d == NULL)
14677c478bd9Sstevel@tonic-gate 					goto mibdiff_out_of_memory;
14687c478bd9Sstevel@tonic-gate 				diffptr->valp = d;
14697c478bd9Sstevel@tonic-gate 				MDIFF(d, r2, r1, rawipInDatagrams);
14707c478bd9Sstevel@tonic-gate 				MDIFF(d, r2, r1, rawipInErrors);
14717c478bd9Sstevel@tonic-gate 				MDIFF(d, r2, r1, rawipInCksumErrs);
14727c478bd9Sstevel@tonic-gate 				MDIFF(d, r2, r1, rawipOutDatagrams);
14737c478bd9Sstevel@tonic-gate 				MDIFF(d, r2, r1, rawipOutErrors);
14747c478bd9Sstevel@tonic-gate 				prevp = diffptr++;
14757c478bd9Sstevel@tonic-gate 				break;
14767c478bd9Sstevel@tonic-gate 			}
14777c478bd9Sstevel@tonic-gate 			/*
14787c478bd9Sstevel@tonic-gate 			 * there are more "group" types but they aren't
14797c478bd9Sstevel@tonic-gate 			 * required for the -s and -Ms options
14807c478bd9Sstevel@tonic-gate 			 */
14817c478bd9Sstevel@tonic-gate 			}
14827c478bd9Sstevel@tonic-gate 		} /* 'for' loop 2 ends */
14837c478bd9Sstevel@tonic-gate 		tempp1 = NULL;
14847c478bd9Sstevel@tonic-gate 	} /* 'for' loop 1 ends */
14857c478bd9Sstevel@tonic-gate 	tempp2 = NULL;
14867c478bd9Sstevel@tonic-gate 	diffptr--;
14877c478bd9Sstevel@tonic-gate 	diffptr->next_item = NULL;
14887c478bd9Sstevel@tonic-gate 	return (diffp);
14897c478bd9Sstevel@tonic-gate 
14907c478bd9Sstevel@tonic-gate mibdiff_out_of_memory:;
14917c478bd9Sstevel@tonic-gate 	mib_item_destroy(&diffp);
14927c478bd9Sstevel@tonic-gate 	return (NULL);
14937c478bd9Sstevel@tonic-gate }
14947c478bd9Sstevel@tonic-gate 
14957c478bd9Sstevel@tonic-gate /*
14967c478bd9Sstevel@tonic-gate  * mib_item_destroy: cleans up a mib_item_t *
14977c478bd9Sstevel@tonic-gate  * that was created by calling mib_item_dup or
14987c478bd9Sstevel@tonic-gate  * mib_item_diff
14997c478bd9Sstevel@tonic-gate  */
15007c478bd9Sstevel@tonic-gate static void
15017c478bd9Sstevel@tonic-gate mib_item_destroy(mib_item_t **itemp) {
15027c478bd9Sstevel@tonic-gate 	int	nitems = 0;
15037c478bd9Sstevel@tonic-gate 	int	c = 0;
15047c478bd9Sstevel@tonic-gate 	mib_item_t *tempp;
15057c478bd9Sstevel@tonic-gate 
15067c478bd9Sstevel@tonic-gate 	if (itemp == NULL || *itemp == NULL)
15077c478bd9Sstevel@tonic-gate 		return;
15087c478bd9Sstevel@tonic-gate 
15097c478bd9Sstevel@tonic-gate 	for (tempp = *itemp; tempp != NULL; tempp = tempp->next_item)
15107c478bd9Sstevel@tonic-gate 		if (tempp->mib_id == 0)
15117c478bd9Sstevel@tonic-gate 			nitems++;
15127c478bd9Sstevel@tonic-gate 		else
15137c478bd9Sstevel@tonic-gate 			return;	/* cannot destroy! */
15147c478bd9Sstevel@tonic-gate 
15157c478bd9Sstevel@tonic-gate 	if (nitems == 0)
15167c478bd9Sstevel@tonic-gate 		return;		/* cannot destroy! */
15177c478bd9Sstevel@tonic-gate 
15187c478bd9Sstevel@tonic-gate 	for (c = nitems - 1; c >= 0; c--) {
15197c478bd9Sstevel@tonic-gate 		if ((itemp[0][c]).valp != NULL)
15207c478bd9Sstevel@tonic-gate 			free((itemp[0][c]).valp);
15217c478bd9Sstevel@tonic-gate 	}
15227c478bd9Sstevel@tonic-gate 	free(*itemp);
15237c478bd9Sstevel@tonic-gate 
15247c478bd9Sstevel@tonic-gate 	*itemp = NULL;
15257c478bd9Sstevel@tonic-gate }
15267c478bd9Sstevel@tonic-gate 
15277c478bd9Sstevel@tonic-gate /* Compare two Octet_ts.  Return B_TRUE if they match, B_FALSE if not. */
15287c478bd9Sstevel@tonic-gate static boolean_t
15297c478bd9Sstevel@tonic-gate octetstrmatch(const Octet_t *a, const Octet_t *b)
15307c478bd9Sstevel@tonic-gate {
15317c478bd9Sstevel@tonic-gate 	if (a == NULL || b == NULL)
15327c478bd9Sstevel@tonic-gate 		return (B_FALSE);
15337c478bd9Sstevel@tonic-gate 
15347c478bd9Sstevel@tonic-gate 	if (a->o_length != b->o_length)
15357c478bd9Sstevel@tonic-gate 		return (B_FALSE);
15367c478bd9Sstevel@tonic-gate 
15377c478bd9Sstevel@tonic-gate 	return (memcmp(a->o_bytes, b->o_bytes, a->o_length) == 0);
15387c478bd9Sstevel@tonic-gate }
15397c478bd9Sstevel@tonic-gate 
15407c478bd9Sstevel@tonic-gate /* If octetstr() changes make an appropriate change to STR_EXPAND */
15417c478bd9Sstevel@tonic-gate static char *
154245916cd2Sjpk octetstr(const Octet_t *op, int code, char *dst, uint_t dstlen)
15437c478bd9Sstevel@tonic-gate {
15447c478bd9Sstevel@tonic-gate 	int	i;
15457c478bd9Sstevel@tonic-gate 	char	*cp;
15467c478bd9Sstevel@tonic-gate 
15477c478bd9Sstevel@tonic-gate 	cp = dst;
15487c478bd9Sstevel@tonic-gate 	if (op) {
15497c478bd9Sstevel@tonic-gate 		for (i = 0; i < op->o_length; i++) {
15507c478bd9Sstevel@tonic-gate 			switch (code) {
15517c478bd9Sstevel@tonic-gate 			case 'd':
15527c478bd9Sstevel@tonic-gate 				if (cp - dst + 4 > dstlen) {
15537c478bd9Sstevel@tonic-gate 					*cp = '\0';
15547c478bd9Sstevel@tonic-gate 					return (dst);
15557c478bd9Sstevel@tonic-gate 				}
15567c478bd9Sstevel@tonic-gate 				(void) snprintf(cp, 5, "%d.",
15577c478bd9Sstevel@tonic-gate 				    0xff & op->o_bytes[i]);
15587c478bd9Sstevel@tonic-gate 				cp = strchr(cp, '\0');
15597c478bd9Sstevel@tonic-gate 				break;
15607c478bd9Sstevel@tonic-gate 			case 'a':
15617c478bd9Sstevel@tonic-gate 				if (cp - dst + 1 > dstlen) {
15627c478bd9Sstevel@tonic-gate 					*cp = '\0';
15637c478bd9Sstevel@tonic-gate 					return (dst);
15647c478bd9Sstevel@tonic-gate 				}
15657c478bd9Sstevel@tonic-gate 				*cp++ = op->o_bytes[i];
15667c478bd9Sstevel@tonic-gate 				break;
15677c478bd9Sstevel@tonic-gate 			case 'h':
15687c478bd9Sstevel@tonic-gate 			default:
15697c478bd9Sstevel@tonic-gate 				if (cp - dst + 3 > dstlen) {
15707c478bd9Sstevel@tonic-gate 					*cp = '\0';
15717c478bd9Sstevel@tonic-gate 					return (dst);
15727c478bd9Sstevel@tonic-gate 				}
15737c478bd9Sstevel@tonic-gate 				(void) snprintf(cp, 4, "%02x:",
15747c478bd9Sstevel@tonic-gate 				    0xff & op->o_bytes[i]);
15757c478bd9Sstevel@tonic-gate 				cp += 3;
15767c478bd9Sstevel@tonic-gate 				break;
15777c478bd9Sstevel@tonic-gate 			}
15787c478bd9Sstevel@tonic-gate 		}
15797c478bd9Sstevel@tonic-gate 	}
15807c478bd9Sstevel@tonic-gate 	if (code != 'a' && cp != dst)
15817c478bd9Sstevel@tonic-gate 		cp--;
15827c478bd9Sstevel@tonic-gate 	*cp = '\0';
15837c478bd9Sstevel@tonic-gate 	return (dst);
15847c478bd9Sstevel@tonic-gate }
15857c478bd9Sstevel@tonic-gate 
158645916cd2Sjpk static const char *
158745916cd2Sjpk mitcp_state(int state, const mib2_transportMLPEntry_t *attr)
15887c478bd9Sstevel@tonic-gate {
158945916cd2Sjpk 	static char tcpsbuf[50];
159045916cd2Sjpk 	const char *cp;
15917c478bd9Sstevel@tonic-gate 
15927c478bd9Sstevel@tonic-gate 	switch (state) {
15937c478bd9Sstevel@tonic-gate 	case TCPS_CLOSED:
15947c478bd9Sstevel@tonic-gate 		cp = "CLOSED";
15957c478bd9Sstevel@tonic-gate 		break;
15967c478bd9Sstevel@tonic-gate 	case TCPS_IDLE:
15977c478bd9Sstevel@tonic-gate 		cp = "IDLE";
15987c478bd9Sstevel@tonic-gate 		break;
15997c478bd9Sstevel@tonic-gate 	case TCPS_BOUND:
16007c478bd9Sstevel@tonic-gate 		cp = "BOUND";
16017c478bd9Sstevel@tonic-gate 		break;
16027c478bd9Sstevel@tonic-gate 	case TCPS_LISTEN:
16037c478bd9Sstevel@tonic-gate 		cp = "LISTEN";
16047c478bd9Sstevel@tonic-gate 		break;
16057c478bd9Sstevel@tonic-gate 	case TCPS_SYN_SENT:
16067c478bd9Sstevel@tonic-gate 		cp = "SYN_SENT";
16077c478bd9Sstevel@tonic-gate 		break;
16087c478bd9Sstevel@tonic-gate 	case TCPS_SYN_RCVD:
16097c478bd9Sstevel@tonic-gate 		cp = "SYN_RCVD";
16107c478bd9Sstevel@tonic-gate 		break;
16117c478bd9Sstevel@tonic-gate 	case TCPS_ESTABLISHED:
16127c478bd9Sstevel@tonic-gate 		cp = "ESTABLISHED";
16137c478bd9Sstevel@tonic-gate 		break;
16147c478bd9Sstevel@tonic-gate 	case TCPS_CLOSE_WAIT:
16157c478bd9Sstevel@tonic-gate 		cp = "CLOSE_WAIT";
16167c478bd9Sstevel@tonic-gate 		break;
16177c478bd9Sstevel@tonic-gate 	case TCPS_FIN_WAIT_1:
16187c478bd9Sstevel@tonic-gate 		cp = "FIN_WAIT_1";
16197c478bd9Sstevel@tonic-gate 		break;
16207c478bd9Sstevel@tonic-gate 	case TCPS_CLOSING:
16217c478bd9Sstevel@tonic-gate 		cp = "CLOSING";
16227c478bd9Sstevel@tonic-gate 		break;
16237c478bd9Sstevel@tonic-gate 	case TCPS_LAST_ACK:
16247c478bd9Sstevel@tonic-gate 		cp = "LAST_ACK";
16257c478bd9Sstevel@tonic-gate 		break;
16267c478bd9Sstevel@tonic-gate 	case TCPS_FIN_WAIT_2:
16277c478bd9Sstevel@tonic-gate 		cp = "FIN_WAIT_2";
16287c478bd9Sstevel@tonic-gate 		break;
16297c478bd9Sstevel@tonic-gate 	case TCPS_TIME_WAIT:
16307c478bd9Sstevel@tonic-gate 		cp = "TIME_WAIT";
16317c478bd9Sstevel@tonic-gate 		break;
16327c478bd9Sstevel@tonic-gate 	default:
16337c478bd9Sstevel@tonic-gate 		(void) snprintf(tcpsbuf, sizeof (tcpsbuf),
16347c478bd9Sstevel@tonic-gate 		    "UnknownState(%d)", state);
16357c478bd9Sstevel@tonic-gate 		cp = tcpsbuf;
16367c478bd9Sstevel@tonic-gate 		break;
16377c478bd9Sstevel@tonic-gate 	}
163845916cd2Sjpk 
163945916cd2Sjpk 	if (RSECflag && attr != NULL && attr->tme_flags != 0) {
164045916cd2Sjpk 		if (cp != tcpsbuf) {
164145916cd2Sjpk 			(void) strlcpy(tcpsbuf, cp, sizeof (tcpsbuf));
164245916cd2Sjpk 			cp = tcpsbuf;
164345916cd2Sjpk 		}
164445916cd2Sjpk 		if (attr->tme_flags & MIB2_TMEF_PRIVATE)
164545916cd2Sjpk 			(void) strlcat(tcpsbuf, " P", sizeof (tcpsbuf));
164645916cd2Sjpk 		if (attr->tme_flags & MIB2_TMEF_SHARED)
164745916cd2Sjpk 			(void) strlcat(tcpsbuf, " S", sizeof (tcpsbuf));
164845916cd2Sjpk 	}
164945916cd2Sjpk 
165045916cd2Sjpk 	return (cp);
165145916cd2Sjpk }
165245916cd2Sjpk 
165345916cd2Sjpk static const char *
165445916cd2Sjpk miudp_state(int state, const mib2_transportMLPEntry_t *attr)
165545916cd2Sjpk {
165645916cd2Sjpk 	static char udpsbuf[50];
165745916cd2Sjpk 	const char *cp;
165845916cd2Sjpk 
165945916cd2Sjpk 	switch (state) {
166045916cd2Sjpk 	case MIB2_UDP_unbound:
166145916cd2Sjpk 		cp = "Unbound";
166245916cd2Sjpk 		break;
166345916cd2Sjpk 	case MIB2_UDP_idle:
166445916cd2Sjpk 		cp = "Idle";
166545916cd2Sjpk 		break;
166645916cd2Sjpk 	case MIB2_UDP_connected:
166745916cd2Sjpk 		cp = "Connected";
166845916cd2Sjpk 		break;
166945916cd2Sjpk 	default:
167045916cd2Sjpk 		(void) snprintf(udpsbuf, sizeof (udpsbuf),
167145916cd2Sjpk 		    "Unknown State(%d)", state);
167245916cd2Sjpk 		cp = udpsbuf;
167345916cd2Sjpk 		break;
167445916cd2Sjpk 	}
167545916cd2Sjpk 
167645916cd2Sjpk 	if (RSECflag && attr != NULL && attr->tme_flags != 0) {
167745916cd2Sjpk 		if (cp != udpsbuf) {
167845916cd2Sjpk 			(void) strlcpy(udpsbuf, cp, sizeof (udpsbuf));
167945916cd2Sjpk 			cp = udpsbuf;
168045916cd2Sjpk 		}
168145916cd2Sjpk 		if (attr->tme_flags & MIB2_TMEF_PRIVATE)
168245916cd2Sjpk 			(void) strlcat(udpsbuf, " P", sizeof (udpsbuf));
168345916cd2Sjpk 		if (attr->tme_flags & MIB2_TMEF_SHARED)
168445916cd2Sjpk 			(void) strlcat(udpsbuf, " S", sizeof (udpsbuf));
168545916cd2Sjpk 	}
168645916cd2Sjpk 
16877c478bd9Sstevel@tonic-gate 	return (cp);
16887c478bd9Sstevel@tonic-gate }
16897c478bd9Sstevel@tonic-gate 
16907c478bd9Sstevel@tonic-gate static int odd;
16917c478bd9Sstevel@tonic-gate 
16927c478bd9Sstevel@tonic-gate static void
16937c478bd9Sstevel@tonic-gate prval_init(void)
16947c478bd9Sstevel@tonic-gate {
16957c478bd9Sstevel@tonic-gate 	odd = 0;
16967c478bd9Sstevel@tonic-gate }
16977c478bd9Sstevel@tonic-gate 
16987c478bd9Sstevel@tonic-gate static void
16997c478bd9Sstevel@tonic-gate prval(char *str, Counter val)
17007c478bd9Sstevel@tonic-gate {
17017c478bd9Sstevel@tonic-gate 	(void) printf("\t%-20s=%6u", str, val);
17027c478bd9Sstevel@tonic-gate 	if (odd++ & 1)
17037c478bd9Sstevel@tonic-gate 		(void) putchar('\n');
17047c478bd9Sstevel@tonic-gate }
17057c478bd9Sstevel@tonic-gate 
17067c478bd9Sstevel@tonic-gate static void
17077c478bd9Sstevel@tonic-gate prval64(char *str, Counter64 val)
17087c478bd9Sstevel@tonic-gate {
17097c478bd9Sstevel@tonic-gate 	(void) printf("\t%-20s=%6llu", str, val);
17107c478bd9Sstevel@tonic-gate 	if (odd++ & 1)
17117c478bd9Sstevel@tonic-gate 		(void) putchar('\n');
17127c478bd9Sstevel@tonic-gate }
17137c478bd9Sstevel@tonic-gate 
17147c478bd9Sstevel@tonic-gate static void
17157c478bd9Sstevel@tonic-gate pr_int_val(char *str, int val)
17167c478bd9Sstevel@tonic-gate {
17177c478bd9Sstevel@tonic-gate 	(void) printf("\t%-20s=%6d", str, val);
17187c478bd9Sstevel@tonic-gate 	if (odd++ & 1)
17197c478bd9Sstevel@tonic-gate 		(void) putchar('\n');
17207c478bd9Sstevel@tonic-gate }
17217c478bd9Sstevel@tonic-gate 
17227c478bd9Sstevel@tonic-gate static void
17237c478bd9Sstevel@tonic-gate pr_sctp_rtoalgo(char *str, int val)
17247c478bd9Sstevel@tonic-gate {
17257c478bd9Sstevel@tonic-gate 	(void) printf("\t%-20s=", str);
17267c478bd9Sstevel@tonic-gate 	switch (val) {
17277c478bd9Sstevel@tonic-gate 		case MIB2_SCTP_RTOALGO_OTHER:
17287c478bd9Sstevel@tonic-gate 			(void) printf("%6.6s", "other");
17297c478bd9Sstevel@tonic-gate 			break;
17307c478bd9Sstevel@tonic-gate 
17317c478bd9Sstevel@tonic-gate 		case MIB2_SCTP_RTOALGO_VANJ:
17327c478bd9Sstevel@tonic-gate 			(void) printf("%6.6s", "vanj");
17337c478bd9Sstevel@tonic-gate 			break;
17347c478bd9Sstevel@tonic-gate 
17357c478bd9Sstevel@tonic-gate 		default:
17367c478bd9Sstevel@tonic-gate 			(void) printf("%6d", val);
17377c478bd9Sstevel@tonic-gate 			break;
17387c478bd9Sstevel@tonic-gate 	}
17397c478bd9Sstevel@tonic-gate 	if (odd++ & 1)
17407c478bd9Sstevel@tonic-gate 		(void) putchar('\n');
17417c478bd9Sstevel@tonic-gate }
17427c478bd9Sstevel@tonic-gate 
17437c478bd9Sstevel@tonic-gate static void
17447c478bd9Sstevel@tonic-gate prval_end(void)
17457c478bd9Sstevel@tonic-gate {
17467c478bd9Sstevel@tonic-gate 	if (odd++ & 1)
17477c478bd9Sstevel@tonic-gate 		(void) putchar('\n');
17487c478bd9Sstevel@tonic-gate }
17497c478bd9Sstevel@tonic-gate 
17507c478bd9Sstevel@tonic-gate /* Extract constant sizes */
17517c478bd9Sstevel@tonic-gate static void
17527c478bd9Sstevel@tonic-gate mib_get_constants(mib_item_t *item)
17537c478bd9Sstevel@tonic-gate {
17547c478bd9Sstevel@tonic-gate 	/* 'for' loop 1: */
17557c478bd9Sstevel@tonic-gate 	for (; item; item = item->next_item) {
17567c478bd9Sstevel@tonic-gate 		if (item->mib_id != 0)
17577c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
17587c478bd9Sstevel@tonic-gate 
17597c478bd9Sstevel@tonic-gate 		switch (item->group) {
17607c478bd9Sstevel@tonic-gate 		case MIB2_IP: {
17617c478bd9Sstevel@tonic-gate 			mib2_ip_t	*ip = (mib2_ip_t *)item->valp;
17627c478bd9Sstevel@tonic-gate 
17637c478bd9Sstevel@tonic-gate 			ipAddrEntrySize = ip->ipAddrEntrySize;
17647c478bd9Sstevel@tonic-gate 			ipRouteEntrySize = ip->ipRouteEntrySize;
17657c478bd9Sstevel@tonic-gate 			ipNetToMediaEntrySize = ip->ipNetToMediaEntrySize;
17667c478bd9Sstevel@tonic-gate 			ipMemberEntrySize = ip->ipMemberEntrySize;
17677c478bd9Sstevel@tonic-gate 			ipGroupSourceEntrySize = ip->ipGroupSourceEntrySize;
176845916cd2Sjpk 			ipRouteAttributeSize = ip->ipRouteAttributeSize;
176945916cd2Sjpk 			transportMLPSize = ip->transportMLPSize;
1770bd670b35SErik Nordmark 			ipDestEntrySize = ip->ipDestEntrySize;
17717c478bd9Sstevel@tonic-gate 			assert(IS_P2ALIGNED(ipAddrEntrySize,
1772e11c3f44Smeem 			    sizeof (mib2_ipAddrEntry_t *)));
1773e11c3f44Smeem 			assert(IS_P2ALIGNED(ipRouteEntrySize,
1774e11c3f44Smeem 			    sizeof (mib2_ipRouteEntry_t *)));
1775e11c3f44Smeem 			assert(IS_P2ALIGNED(ipNetToMediaEntrySize,
1776e11c3f44Smeem 			    sizeof (mib2_ipNetToMediaEntry_t *)));
1777e11c3f44Smeem 			assert(IS_P2ALIGNED(ipMemberEntrySize,
1778e11c3f44Smeem 			    sizeof (ip_member_t *)));
1779e11c3f44Smeem 			assert(IS_P2ALIGNED(ipGroupSourceEntrySize,
1780e11c3f44Smeem 			    sizeof (ip_grpsrc_t *)));
1781e11c3f44Smeem 			assert(IS_P2ALIGNED(ipRouteAttributeSize,
1782e11c3f44Smeem 			    sizeof (mib2_ipAttributeEntry_t *)));
1783e11c3f44Smeem 			assert(IS_P2ALIGNED(transportMLPSize,
1784e11c3f44Smeem 			    sizeof (mib2_transportMLPEntry_t *)));
17857c478bd9Sstevel@tonic-gate 			break;
17867c478bd9Sstevel@tonic-gate 		}
17877c478bd9Sstevel@tonic-gate 		case EXPER_DVMRP: {
17887c478bd9Sstevel@tonic-gate 			struct mrtstat	*mrts = (struct mrtstat *)item->valp;
17897c478bd9Sstevel@tonic-gate 
17907c478bd9Sstevel@tonic-gate 			vifctlSize = mrts->mrts_vifctlSize;
17917c478bd9Sstevel@tonic-gate 			mfcctlSize = mrts->mrts_mfcctlSize;
17927c478bd9Sstevel@tonic-gate 			assert(IS_P2ALIGNED(vifctlSize,
1793e11c3f44Smeem 			    sizeof (struct vifclt *)));
1794e11c3f44Smeem 			assert(IS_P2ALIGNED(mfcctlSize,
1795e11c3f44Smeem 			    sizeof (struct mfcctl *)));
17967c478bd9Sstevel@tonic-gate 			break;
17977c478bd9Sstevel@tonic-gate 		}
17987c478bd9Sstevel@tonic-gate 		case MIB2_IP6: {
17997c478bd9Sstevel@tonic-gate 			mib2_ipv6IfStatsEntry_t *ip6;
18007c478bd9Sstevel@tonic-gate 			/* Just use the first entry */
18017c478bd9Sstevel@tonic-gate 
18027c478bd9Sstevel@tonic-gate 			ip6 = (mib2_ipv6IfStatsEntry_t *)item->valp;
18037c478bd9Sstevel@tonic-gate 			ipv6IfStatsEntrySize = ip6->ipv6IfStatsEntrySize;
18047c478bd9Sstevel@tonic-gate 			ipv6AddrEntrySize = ip6->ipv6AddrEntrySize;
18057c478bd9Sstevel@tonic-gate 			ipv6RouteEntrySize = ip6->ipv6RouteEntrySize;
18067c478bd9Sstevel@tonic-gate 			ipv6NetToMediaEntrySize = ip6->ipv6NetToMediaEntrySize;
18077c478bd9Sstevel@tonic-gate 			ipv6MemberEntrySize = ip6->ipv6MemberEntrySize;
18087c478bd9Sstevel@tonic-gate 			ipv6GroupSourceEntrySize =
18097c478bd9Sstevel@tonic-gate 			    ip6->ipv6GroupSourceEntrySize;
18107c478bd9Sstevel@tonic-gate 			assert(IS_P2ALIGNED(ipv6IfStatsEntrySize,
1811e11c3f44Smeem 			    sizeof (mib2_ipv6IfStatsEntry_t *)));
1812e11c3f44Smeem 			assert(IS_P2ALIGNED(ipv6AddrEntrySize,
1813e11c3f44Smeem 			    sizeof (mib2_ipv6AddrEntry_t *)));
1814e11c3f44Smeem 			assert(IS_P2ALIGNED(ipv6RouteEntrySize,
1815e11c3f44Smeem 			    sizeof (mib2_ipv6RouteEntry_t *)));
1816e11c3f44Smeem 			assert(IS_P2ALIGNED(ipv6NetToMediaEntrySize,
1817e11c3f44Smeem 			    sizeof (mib2_ipv6NetToMediaEntry_t *)));
1818e11c3f44Smeem 			assert(IS_P2ALIGNED(ipv6MemberEntrySize,
1819e11c3f44Smeem 			    sizeof (ipv6_member_t *)));
1820e11c3f44Smeem 			assert(IS_P2ALIGNED(ipv6GroupSourceEntrySize,
1821e11c3f44Smeem 			    sizeof (ipv6_grpsrc_t *)));
18227c478bd9Sstevel@tonic-gate 			break;
18237c478bd9Sstevel@tonic-gate 		}
18247c478bd9Sstevel@tonic-gate 		case MIB2_ICMP6: {
18257c478bd9Sstevel@tonic-gate 			mib2_ipv6IfIcmpEntry_t *icmp6;
18267c478bd9Sstevel@tonic-gate 			/* Just use the first entry */
18277c478bd9Sstevel@tonic-gate 
18287c478bd9Sstevel@tonic-gate 			icmp6 = (mib2_ipv6IfIcmpEntry_t *)item->valp;
18297c478bd9Sstevel@tonic-gate 			ipv6IfIcmpEntrySize = icmp6->ipv6IfIcmpEntrySize;
18307c478bd9Sstevel@tonic-gate 			assert(IS_P2ALIGNED(ipv6IfIcmpEntrySize,
18317c478bd9Sstevel@tonic-gate 			    sizeof (mib2_ipv6IfIcmpEntry_t *)));
18327c478bd9Sstevel@tonic-gate 			break;
18337c478bd9Sstevel@tonic-gate 		}
18347c478bd9Sstevel@tonic-gate 		case MIB2_TCP: {
18357c478bd9Sstevel@tonic-gate 			mib2_tcp_t	*tcp = (mib2_tcp_t *)item->valp;
18367c478bd9Sstevel@tonic-gate 
18377c478bd9Sstevel@tonic-gate 			tcpConnEntrySize = tcp->tcpConnTableSize;
18387c478bd9Sstevel@tonic-gate 			tcp6ConnEntrySize = tcp->tcp6ConnTableSize;
18397c478bd9Sstevel@tonic-gate 			assert(IS_P2ALIGNED(tcpConnEntrySize,
1840e11c3f44Smeem 			    sizeof (mib2_tcpConnEntry_t *)));
1841e11c3f44Smeem 			assert(IS_P2ALIGNED(tcp6ConnEntrySize,
1842e11c3f44Smeem 			    sizeof (mib2_tcp6ConnEntry_t *)));
18437c478bd9Sstevel@tonic-gate 			break;
18447c478bd9Sstevel@tonic-gate 		}
18457c478bd9Sstevel@tonic-gate 		case MIB2_UDP: {
18467c478bd9Sstevel@tonic-gate 			mib2_udp_t	*udp = (mib2_udp_t *)item->valp;
18477c478bd9Sstevel@tonic-gate 
18487c478bd9Sstevel@tonic-gate 			udpEntrySize = udp->udpEntrySize;
18497c478bd9Sstevel@tonic-gate 			udp6EntrySize = udp->udp6EntrySize;
18507c478bd9Sstevel@tonic-gate 			assert(IS_P2ALIGNED(udpEntrySize,
1851e11c3f44Smeem 			    sizeof (mib2_udpEntry_t *)));
1852e11c3f44Smeem 			assert(IS_P2ALIGNED(udp6EntrySize,
1853e11c3f44Smeem 			    sizeof (mib2_udp6Entry_t *)));
18547c478bd9Sstevel@tonic-gate 			break;
18557c478bd9Sstevel@tonic-gate 		}
18567c478bd9Sstevel@tonic-gate 		case MIB2_SCTP: {
18577c478bd9Sstevel@tonic-gate 			mib2_sctp_t	*sctp = (mib2_sctp_t *)item->valp;
18587c478bd9Sstevel@tonic-gate 
18597c478bd9Sstevel@tonic-gate 			sctpEntrySize = sctp->sctpEntrySize;
18607c478bd9Sstevel@tonic-gate 			sctpLocalEntrySize = sctp->sctpLocalEntrySize;
18617c478bd9Sstevel@tonic-gate 			sctpRemoteEntrySize = sctp->sctpRemoteEntrySize;
18627c478bd9Sstevel@tonic-gate 			break;
18637c478bd9Sstevel@tonic-gate 		}
18647c478bd9Sstevel@tonic-gate 		}
18657c478bd9Sstevel@tonic-gate 	} /* 'for' loop 1 ends */
18667c478bd9Sstevel@tonic-gate 
1867bd670b35SErik Nordmark 	if (Xflag) {
18687c478bd9Sstevel@tonic-gate 		(void) puts("mib_get_constants:");
18697c478bd9Sstevel@tonic-gate 		(void) printf("\tipv6IfStatsEntrySize %d\n",
18707c478bd9Sstevel@tonic-gate 		    ipv6IfStatsEntrySize);
18717c478bd9Sstevel@tonic-gate 		(void) printf("\tipAddrEntrySize %d\n", ipAddrEntrySize);
18727c478bd9Sstevel@tonic-gate 		(void) printf("\tipRouteEntrySize %d\n", ipRouteEntrySize);
18737c478bd9Sstevel@tonic-gate 		(void) printf("\tipNetToMediaEntrySize %d\n",
18747c478bd9Sstevel@tonic-gate 		    ipNetToMediaEntrySize);
18757c478bd9Sstevel@tonic-gate 		(void) printf("\tipMemberEntrySize %d\n", ipMemberEntrySize);
187645916cd2Sjpk 		(void) printf("\tipRouteAttributeSize %d\n",
187745916cd2Sjpk 		    ipRouteAttributeSize);
18787c478bd9Sstevel@tonic-gate 		(void) printf("\tvifctlSize %d\n", vifctlSize);
18797c478bd9Sstevel@tonic-gate 		(void) printf("\tmfcctlSize %d\n", mfcctlSize);
18807c478bd9Sstevel@tonic-gate 
18817c478bd9Sstevel@tonic-gate 		(void) printf("\tipv6AddrEntrySize %d\n", ipv6AddrEntrySize);
18827c478bd9Sstevel@tonic-gate 		(void) printf("\tipv6RouteEntrySize %d\n", ipv6RouteEntrySize);
18837c478bd9Sstevel@tonic-gate 		(void) printf("\tipv6NetToMediaEntrySize %d\n",
18847c478bd9Sstevel@tonic-gate 		    ipv6NetToMediaEntrySize);
18857c478bd9Sstevel@tonic-gate 		(void) printf("\tipv6MemberEntrySize %d\n",
18867c478bd9Sstevel@tonic-gate 		    ipv6MemberEntrySize);
18877c478bd9Sstevel@tonic-gate 		(void) printf("\tipv6IfIcmpEntrySize %d\n",
18887c478bd9Sstevel@tonic-gate 		    ipv6IfIcmpEntrySize);
1889bd670b35SErik Nordmark 		(void) printf("\tipDestEntrySize %d\n", ipDestEntrySize);
189045916cd2Sjpk 		(void) printf("\ttransportMLPSize %d\n", transportMLPSize);
18917c478bd9Sstevel@tonic-gate 		(void) printf("\ttcpConnEntrySize %d\n", tcpConnEntrySize);
18927c478bd9Sstevel@tonic-gate 		(void) printf("\ttcp6ConnEntrySize %d\n", tcp6ConnEntrySize);
18937c478bd9Sstevel@tonic-gate 		(void) printf("\tudpEntrySize %d\n", udpEntrySize);
18947c478bd9Sstevel@tonic-gate 		(void) printf("\tudp6EntrySize %d\n", udp6EntrySize);
18957c478bd9Sstevel@tonic-gate 		(void) printf("\tsctpEntrySize %d\n", sctpEntrySize);
18967c478bd9Sstevel@tonic-gate 		(void) printf("\tsctpLocalEntrySize %d\n", sctpLocalEntrySize);
18977c478bd9Sstevel@tonic-gate 		(void) printf("\tsctpRemoteEntrySize %d\n",
18987c478bd9Sstevel@tonic-gate 		    sctpRemoteEntrySize);
18997c478bd9Sstevel@tonic-gate 	}
19007c478bd9Sstevel@tonic-gate }
19017c478bd9Sstevel@tonic-gate 
19027c478bd9Sstevel@tonic-gate 
19037c478bd9Sstevel@tonic-gate /* ----------------------------- STAT_REPORT ------------------------------- */
19047c478bd9Sstevel@tonic-gate 
19057c478bd9Sstevel@tonic-gate static void
19067c478bd9Sstevel@tonic-gate stat_report(mib_item_t *item)
19077c478bd9Sstevel@tonic-gate {
19087c478bd9Sstevel@tonic-gate 	int	jtemp = 0;
19097c478bd9Sstevel@tonic-gate 	char	ifname[LIFNAMSIZ + 1];
19107c478bd9Sstevel@tonic-gate 
19117c478bd9Sstevel@tonic-gate 	/* 'for' loop 1: */
19127c478bd9Sstevel@tonic-gate 	for (; item; item = item->next_item) {
1913bd670b35SErik Nordmark 		if (Xflag) {
19147c478bd9Sstevel@tonic-gate 			(void) printf("\n--- Entry %d ---\n", ++jtemp);
19157c478bd9Sstevel@tonic-gate 			(void) printf("Group = %d, mib_id = %d, "
19167c478bd9Sstevel@tonic-gate 			    "length = %d, valp = 0x%p\n",
19177c478bd9Sstevel@tonic-gate 			    item->group, item->mib_id,
19187c478bd9Sstevel@tonic-gate 			    item->length, item->valp);
19197c478bd9Sstevel@tonic-gate 		}
19207c478bd9Sstevel@tonic-gate 		if (item->mib_id != 0)
19217c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
19227c478bd9Sstevel@tonic-gate 
19237c478bd9Sstevel@tonic-gate 		switch (item->group) {
19247c478bd9Sstevel@tonic-gate 		case MIB2_IP: {
19257c478bd9Sstevel@tonic-gate 			mib2_ip_t	*ip = (mib2_ip_t *)item->valp;
19267c478bd9Sstevel@tonic-gate 
19277c478bd9Sstevel@tonic-gate 			if (protocol_selected(IPPROTO_IP) &&
19287c478bd9Sstevel@tonic-gate 			    family_selected(AF_INET)) {
19297c478bd9Sstevel@tonic-gate 				(void) fputs(v4compat ? "\nIP" : "\nIPv4",
19307c478bd9Sstevel@tonic-gate 				    stdout);
19317c478bd9Sstevel@tonic-gate 				print_ip_stats(ip);
19327c478bd9Sstevel@tonic-gate 			}
19337c478bd9Sstevel@tonic-gate 			break;
19347c478bd9Sstevel@tonic-gate 		}
19357c478bd9Sstevel@tonic-gate 		case MIB2_ICMP: {
19367c478bd9Sstevel@tonic-gate 			mib2_icmp_t	*icmp =
19377c478bd9Sstevel@tonic-gate 			    (mib2_icmp_t *)item->valp;
19387c478bd9Sstevel@tonic-gate 
19397c478bd9Sstevel@tonic-gate 			if (protocol_selected(IPPROTO_ICMP) &&
19407c478bd9Sstevel@tonic-gate 			    family_selected(AF_INET)) {
19417c478bd9Sstevel@tonic-gate 				(void) fputs(v4compat ? "\nICMP" : "\nICMPv4",
19427c478bd9Sstevel@tonic-gate 				    stdout);
19437c478bd9Sstevel@tonic-gate 				print_icmp_stats(icmp);
19447c478bd9Sstevel@tonic-gate 			}
19457c478bd9Sstevel@tonic-gate 			break;
19467c478bd9Sstevel@tonic-gate 		}
19477c478bd9Sstevel@tonic-gate 		case MIB2_IP6: {
19487c478bd9Sstevel@tonic-gate 			mib2_ipv6IfStatsEntry_t *ip6;
19497c478bd9Sstevel@tonic-gate 			mib2_ipv6IfStatsEntry_t sum6;
19507c478bd9Sstevel@tonic-gate 
19517c478bd9Sstevel@tonic-gate 			if (!(protocol_selected(IPPROTO_IPV6)) ||
19527c478bd9Sstevel@tonic-gate 			    !(family_selected(AF_INET6)))
19537c478bd9Sstevel@tonic-gate 				break;
19547c478bd9Sstevel@tonic-gate 			bzero(&sum6, sizeof (sum6));
19557c478bd9Sstevel@tonic-gate 			/* 'for' loop 2a: */
19567c478bd9Sstevel@tonic-gate 			for (ip6 = (mib2_ipv6IfStatsEntry_t *)item->valp;
1957e11c3f44Smeem 			    (char *)ip6 < (char *)item->valp + item->length;
19587c478bd9Sstevel@tonic-gate 			    /* LINTED: (note 1) */
19597c478bd9Sstevel@tonic-gate 			    ip6 = (mib2_ipv6IfStatsEntry_t *)((char *)ip6 +
19607c478bd9Sstevel@tonic-gate 			    ipv6IfStatsEntrySize)) {
19617c478bd9Sstevel@tonic-gate 				if (ip6->ipv6IfIndex == 0) {
19627c478bd9Sstevel@tonic-gate 					/*
19637c478bd9Sstevel@tonic-gate 					 * The "unknown interface" ip6
19647c478bd9Sstevel@tonic-gate 					 * mib. Just add to the sum.
19657c478bd9Sstevel@tonic-gate 					 */
19667c478bd9Sstevel@tonic-gate 					sum_ip6_stats(ip6, &sum6);
19677c478bd9Sstevel@tonic-gate 					continue; /* 'for' loop 2a */
19687c478bd9Sstevel@tonic-gate 				}
19697c478bd9Sstevel@tonic-gate 				if (Aflag) {
19707c478bd9Sstevel@tonic-gate 					(void) printf("\nIPv6 for %s\n",
1971e11c3f44Smeem 					    ifindex2str(ip6->ipv6IfIndex,
1972e11c3f44Smeem 					    ifname));
19737c478bd9Sstevel@tonic-gate 					print_ip6_stats(ip6);
19747c478bd9Sstevel@tonic-gate 				}
19757c478bd9Sstevel@tonic-gate 				sum_ip6_stats(ip6, &sum6);
19767c478bd9Sstevel@tonic-gate 			} /* 'for' loop 2a ends */
19777c478bd9Sstevel@tonic-gate 			(void) fputs("\nIPv6", stdout);
19787c478bd9Sstevel@tonic-gate 			print_ip6_stats(&sum6);
19797c478bd9Sstevel@tonic-gate 			break;
19807c478bd9Sstevel@tonic-gate 		}
19817c478bd9Sstevel@tonic-gate 		case MIB2_ICMP6: {
19827c478bd9Sstevel@tonic-gate 			mib2_ipv6IfIcmpEntry_t *icmp6;
19837c478bd9Sstevel@tonic-gate 			mib2_ipv6IfIcmpEntry_t sum6;
19847c478bd9Sstevel@tonic-gate 
19857c478bd9Sstevel@tonic-gate 			if (!(protocol_selected(IPPROTO_ICMPV6)) ||
19867c478bd9Sstevel@tonic-gate 			    !(family_selected(AF_INET6)))
19877c478bd9Sstevel@tonic-gate 				break;
19887c478bd9Sstevel@tonic-gate 			bzero(&sum6, sizeof (sum6));
19897c478bd9Sstevel@tonic-gate 			/* 'for' loop 2b: */
1990e11c3f44Smeem 			for (icmp6 = (mib2_ipv6IfIcmpEntry_t *)item->valp;
1991e11c3f44Smeem 			    (char *)icmp6 < (char *)item->valp + item->length;
1992e11c3f44Smeem 			    icmp6 = (void *)((char *)icmp6 +
1993e11c3f44Smeem 			    ipv6IfIcmpEntrySize)) {
19947c478bd9Sstevel@tonic-gate 				if (icmp6->ipv6IfIcmpIfIndex == 0) {
19957c478bd9Sstevel@tonic-gate 					/*
19967c478bd9Sstevel@tonic-gate 					 * The "unknown interface" icmp6
19977c478bd9Sstevel@tonic-gate 					 * mib. Just add to the sum.
19987c478bd9Sstevel@tonic-gate 					 */
19997c478bd9Sstevel@tonic-gate 					sum_icmp6_stats(icmp6, &sum6);
20007c478bd9Sstevel@tonic-gate 					continue; /* 'for' loop 2b: */
20017c478bd9Sstevel@tonic-gate 				}
20027c478bd9Sstevel@tonic-gate 				if (Aflag) {
2003e11c3f44Smeem 					(void) printf("\nICMPv6 for %s\n",
2004e11c3f44Smeem 					    ifindex2str(
2005e11c3f44Smeem 					    icmp6->ipv6IfIcmpIfIndex, ifname));
20067c478bd9Sstevel@tonic-gate 					print_icmp6_stats(icmp6);
20077c478bd9Sstevel@tonic-gate 				}
20087c478bd9Sstevel@tonic-gate 				sum_icmp6_stats(icmp6, &sum6);
20097c478bd9Sstevel@tonic-gate 			} /* 'for' loop 2b ends */
20107c478bd9Sstevel@tonic-gate 			(void) fputs("\nICMPv6", stdout);
20117c478bd9Sstevel@tonic-gate 			print_icmp6_stats(&sum6);
20127c478bd9Sstevel@tonic-gate 			break;
20137c478bd9Sstevel@tonic-gate 		}
20147c478bd9Sstevel@tonic-gate 		case MIB2_TCP: {
20157c478bd9Sstevel@tonic-gate 			mib2_tcp_t	*tcp = (mib2_tcp_t *)item->valp;
20167c478bd9Sstevel@tonic-gate 
20177c478bd9Sstevel@tonic-gate 			if (protocol_selected(IPPROTO_TCP) &&
20187c478bd9Sstevel@tonic-gate 			    (family_selected(AF_INET) ||
20197c478bd9Sstevel@tonic-gate 			    family_selected(AF_INET6))) {
20207c478bd9Sstevel@tonic-gate 				(void) fputs("\nTCP", stdout);
20217c478bd9Sstevel@tonic-gate 				print_tcp_stats(tcp);
20227c478bd9Sstevel@tonic-gate 			}
20237c478bd9Sstevel@tonic-gate 			break;
20247c478bd9Sstevel@tonic-gate 		}
20257c478bd9Sstevel@tonic-gate 		case MIB2_UDP: {
20267c478bd9Sstevel@tonic-gate 			mib2_udp_t	*udp = (mib2_udp_t *)item->valp;
20277c478bd9Sstevel@tonic-gate 
20287c478bd9Sstevel@tonic-gate 			if (protocol_selected(IPPROTO_UDP) &&
20297c478bd9Sstevel@tonic-gate 			    (family_selected(AF_INET) ||
20307c478bd9Sstevel@tonic-gate 			    family_selected(AF_INET6))) {
20317c478bd9Sstevel@tonic-gate 				(void) fputs("\nUDP", stdout);
20327c478bd9Sstevel@tonic-gate 				print_udp_stats(udp);
20337c478bd9Sstevel@tonic-gate 			}
20347c478bd9Sstevel@tonic-gate 			break;
20357c478bd9Sstevel@tonic-gate 		}
20367c478bd9Sstevel@tonic-gate 		case MIB2_SCTP: {
20377c478bd9Sstevel@tonic-gate 			mib2_sctp_t	*sctp = (mib2_sctp_t *)item->valp;
20387c478bd9Sstevel@tonic-gate 
20397c478bd9Sstevel@tonic-gate 			if (protocol_selected(IPPROTO_SCTP) &&
20407c478bd9Sstevel@tonic-gate 			    (family_selected(AF_INET) ||
20417c478bd9Sstevel@tonic-gate 			    family_selected(AF_INET6))) {
20427c478bd9Sstevel@tonic-gate 				(void) fputs("\nSCTP", stdout);
20437c478bd9Sstevel@tonic-gate 				print_sctp_stats(sctp);
20447c478bd9Sstevel@tonic-gate 			}
20457c478bd9Sstevel@tonic-gate 			break;
20467c478bd9Sstevel@tonic-gate 		}
20477c478bd9Sstevel@tonic-gate 		case EXPER_RAWIP: {
20487c478bd9Sstevel@tonic-gate 			mib2_rawip_t	*rawip =
20497c478bd9Sstevel@tonic-gate 			    (mib2_rawip_t *)item->valp;
20507c478bd9Sstevel@tonic-gate 
20517c478bd9Sstevel@tonic-gate 			if (protocol_selected(IPPROTO_RAW) &&
20527c478bd9Sstevel@tonic-gate 			    (family_selected(AF_INET) ||
20537c478bd9Sstevel@tonic-gate 			    family_selected(AF_INET6))) {
20547c478bd9Sstevel@tonic-gate 				(void) fputs("\nRAWIP", stdout);
20557c478bd9Sstevel@tonic-gate 				print_rawip_stats(rawip);
20567c478bd9Sstevel@tonic-gate 			}
20577c478bd9Sstevel@tonic-gate 			break;
20587c478bd9Sstevel@tonic-gate 		}
20597c478bd9Sstevel@tonic-gate 		case EXPER_IGMP: {
20607c478bd9Sstevel@tonic-gate 			struct igmpstat	*igps =
20617c478bd9Sstevel@tonic-gate 			    (struct igmpstat *)item->valp;
20627c478bd9Sstevel@tonic-gate 
20637c478bd9Sstevel@tonic-gate 			if (protocol_selected(IPPROTO_IGMP) &&
20647c478bd9Sstevel@tonic-gate 			    (family_selected(AF_INET))) {
20657c478bd9Sstevel@tonic-gate 				(void) fputs("\nIGMP:\n", stdout);
20667c478bd9Sstevel@tonic-gate 				print_igmp_stats(igps);
20677c478bd9Sstevel@tonic-gate 			}
20687c478bd9Sstevel@tonic-gate 			break;
20697c478bd9Sstevel@tonic-gate 		}
20707c478bd9Sstevel@tonic-gate 		}
20717c478bd9Sstevel@tonic-gate 	} /* 'for' loop 1 ends */
20727c478bd9Sstevel@tonic-gate 	(void) putchar('\n');
20737c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
20747c478bd9Sstevel@tonic-gate }
20757c478bd9Sstevel@tonic-gate 
20767c478bd9Sstevel@tonic-gate static void
20777c478bd9Sstevel@tonic-gate print_ip_stats(mib2_ip_t *ip)
20787c478bd9Sstevel@tonic-gate {
20797c478bd9Sstevel@tonic-gate 	prval_init();
20807c478bd9Sstevel@tonic-gate 	pr_int_val("ipForwarding",	ip->ipForwarding);
20817c478bd9Sstevel@tonic-gate 	pr_int_val("ipDefaultTTL",	ip->ipDefaultTTL);
20827c478bd9Sstevel@tonic-gate 	prval("ipInReceives",		ip->ipInReceives);
20837c478bd9Sstevel@tonic-gate 	prval("ipInHdrErrors",		ip->ipInHdrErrors);
20847c478bd9Sstevel@tonic-gate 	prval("ipInAddrErrors",		ip->ipInAddrErrors);
20857c478bd9Sstevel@tonic-gate 	prval("ipInCksumErrs",		ip->ipInCksumErrs);
20867c478bd9Sstevel@tonic-gate 	prval("ipForwDatagrams",	ip->ipForwDatagrams);
20877c478bd9Sstevel@tonic-gate 	prval("ipForwProhibits",	ip->ipForwProhibits);
20887c478bd9Sstevel@tonic-gate 	prval("ipInUnknownProtos",	ip->ipInUnknownProtos);
20897c478bd9Sstevel@tonic-gate 	prval("ipInDiscards",		ip->ipInDiscards);
20907c478bd9Sstevel@tonic-gate 	prval("ipInDelivers",		ip->ipInDelivers);
20917c478bd9Sstevel@tonic-gate 	prval("ipOutRequests",		ip->ipOutRequests);
20927c478bd9Sstevel@tonic-gate 	prval("ipOutDiscards",		ip->ipOutDiscards);
20937c478bd9Sstevel@tonic-gate 	prval("ipOutNoRoutes",		ip->ipOutNoRoutes);
20947c478bd9Sstevel@tonic-gate 	pr_int_val("ipReasmTimeout",	ip->ipReasmTimeout);
20957c478bd9Sstevel@tonic-gate 	prval("ipReasmReqds",		ip->ipReasmReqds);
20967c478bd9Sstevel@tonic-gate 	prval("ipReasmOKs",		ip->ipReasmOKs);
20977c478bd9Sstevel@tonic-gate 	prval("ipReasmFails",		ip->ipReasmFails);
20987c478bd9Sstevel@tonic-gate 	prval("ipReasmDuplicates",	ip->ipReasmDuplicates);
20997c478bd9Sstevel@tonic-gate 	prval("ipReasmPartDups",	ip->ipReasmPartDups);
21007c478bd9Sstevel@tonic-gate 	prval("ipFragOKs",		ip->ipFragOKs);
21017c478bd9Sstevel@tonic-gate 	prval("ipFragFails",		ip->ipFragFails);
21027c478bd9Sstevel@tonic-gate 	prval("ipFragCreates",		ip->ipFragCreates);
21037c478bd9Sstevel@tonic-gate 	prval("ipRoutingDiscards",	ip->ipRoutingDiscards);
21047c478bd9Sstevel@tonic-gate 
21057c478bd9Sstevel@tonic-gate 	prval("tcpInErrs",		ip->tcpInErrs);
21067c478bd9Sstevel@tonic-gate 	prval("udpNoPorts",		ip->udpNoPorts);
21077c478bd9Sstevel@tonic-gate 	prval("udpInCksumErrs",		ip->udpInCksumErrs);
21087c478bd9Sstevel@tonic-gate 	prval("udpInOverflows",		ip->udpInOverflows);
21097c478bd9Sstevel@tonic-gate 	prval("rawipInOverflows",	ip->rawipInOverflows);
21107c478bd9Sstevel@tonic-gate 	prval("ipsecInSucceeded",	ip->ipsecInSucceeded);
21117c478bd9Sstevel@tonic-gate 	prval("ipsecInFailed",		ip->ipsecInFailed);
21127c478bd9Sstevel@tonic-gate 	prval("ipInIPv6",		ip->ipInIPv6);
21137c478bd9Sstevel@tonic-gate 	prval("ipOutIPv6",		ip->ipOutIPv6);
21147c478bd9Sstevel@tonic-gate 	prval("ipOutSwitchIPv6",	ip->ipOutSwitchIPv6);
21157c478bd9Sstevel@tonic-gate 	prval_end();
21167c478bd9Sstevel@tonic-gate }
21177c478bd9Sstevel@tonic-gate 
21187c478bd9Sstevel@tonic-gate static void
21197c478bd9Sstevel@tonic-gate print_icmp_stats(mib2_icmp_t *icmp)
21207c478bd9Sstevel@tonic-gate {
21217c478bd9Sstevel@tonic-gate 	prval_init();
21227c478bd9Sstevel@tonic-gate 	prval("icmpInMsgs",		icmp->icmpInMsgs);
21237c478bd9Sstevel@tonic-gate 	prval("icmpInErrors",		icmp->icmpInErrors);
21247c478bd9Sstevel@tonic-gate 	prval("icmpInCksumErrs",	icmp->icmpInCksumErrs);
21257c478bd9Sstevel@tonic-gate 	prval("icmpInUnknowns",		icmp->icmpInUnknowns);
21267c478bd9Sstevel@tonic-gate 	prval("icmpInDestUnreachs",	icmp->icmpInDestUnreachs);
21277c478bd9Sstevel@tonic-gate 	prval("icmpInTimeExcds",	icmp->icmpInTimeExcds);
21287c478bd9Sstevel@tonic-gate 	prval("icmpInParmProbs",	icmp->icmpInParmProbs);
21297c478bd9Sstevel@tonic-gate 	prval("icmpInSrcQuenchs",	icmp->icmpInSrcQuenchs);
21307c478bd9Sstevel@tonic-gate 	prval("icmpInRedirects",	icmp->icmpInRedirects);
21317c478bd9Sstevel@tonic-gate 	prval("icmpInBadRedirects",	icmp->icmpInBadRedirects);
21327c478bd9Sstevel@tonic-gate 	prval("icmpInEchos",		icmp->icmpInEchos);
21337c478bd9Sstevel@tonic-gate 	prval("icmpInEchoReps",		icmp->icmpInEchoReps);
21347c478bd9Sstevel@tonic-gate 	prval("icmpInTimestamps",	icmp->icmpInTimestamps);
21357c478bd9Sstevel@tonic-gate 	prval("icmpInTimestampReps",	icmp->icmpInTimestampReps);
21367c478bd9Sstevel@tonic-gate 	prval("icmpInAddrMasks",	icmp->icmpInAddrMasks);
21377c478bd9Sstevel@tonic-gate 	prval("icmpInAddrMaskReps",	icmp->icmpInAddrMaskReps);
21387c478bd9Sstevel@tonic-gate 	prval("icmpInFragNeeded",	icmp->icmpInFragNeeded);
21397c478bd9Sstevel@tonic-gate 	prval("icmpOutMsgs",		icmp->icmpOutMsgs);
21407c478bd9Sstevel@tonic-gate 	prval("icmpOutDrops",		icmp->icmpOutDrops);
21417c478bd9Sstevel@tonic-gate 	prval("icmpOutErrors",		icmp->icmpOutErrors);
21427c478bd9Sstevel@tonic-gate 	prval("icmpOutDestUnreachs",	icmp->icmpOutDestUnreachs);
21437c478bd9Sstevel@tonic-gate 	prval("icmpOutTimeExcds",	icmp->icmpOutTimeExcds);
21447c478bd9Sstevel@tonic-gate 	prval("icmpOutParmProbs",	icmp->icmpOutParmProbs);
21457c478bd9Sstevel@tonic-gate 	prval("icmpOutSrcQuenchs",	icmp->icmpOutSrcQuenchs);
21467c478bd9Sstevel@tonic-gate 	prval("icmpOutRedirects",	icmp->icmpOutRedirects);
21477c478bd9Sstevel@tonic-gate 	prval("icmpOutEchos",		icmp->icmpOutEchos);
21487c478bd9Sstevel@tonic-gate 	prval("icmpOutEchoReps",	icmp->icmpOutEchoReps);
21497c478bd9Sstevel@tonic-gate 	prval("icmpOutTimestamps",	icmp->icmpOutTimestamps);
21507c478bd9Sstevel@tonic-gate 	prval("icmpOutTimestampReps",	icmp->icmpOutTimestampReps);
21517c478bd9Sstevel@tonic-gate 	prval("icmpOutAddrMasks",	icmp->icmpOutAddrMasks);
21527c478bd9Sstevel@tonic-gate 	prval("icmpOutAddrMaskReps",	icmp->icmpOutAddrMaskReps);
21537c478bd9Sstevel@tonic-gate 	prval("icmpOutFragNeeded",	icmp->icmpOutFragNeeded);
21547c478bd9Sstevel@tonic-gate 	prval("icmpInOverflows",	icmp->icmpInOverflows);
21557c478bd9Sstevel@tonic-gate 	prval_end();
21567c478bd9Sstevel@tonic-gate }
21577c478bd9Sstevel@tonic-gate 
21587c478bd9Sstevel@tonic-gate static void
21597c478bd9Sstevel@tonic-gate print_ip6_stats(mib2_ipv6IfStatsEntry_t *ip6)
21607c478bd9Sstevel@tonic-gate {
21617c478bd9Sstevel@tonic-gate 	prval_init();
21627c478bd9Sstevel@tonic-gate 	prval("ipv6Forwarding",		ip6->ipv6Forwarding);
21637c478bd9Sstevel@tonic-gate 	prval("ipv6DefaultHopLimit",	ip6->ipv6DefaultHopLimit);
21647c478bd9Sstevel@tonic-gate 
21657c478bd9Sstevel@tonic-gate 	prval("ipv6InReceives",		ip6->ipv6InReceives);
21667c478bd9Sstevel@tonic-gate 	prval("ipv6InHdrErrors",	ip6->ipv6InHdrErrors);
21677c478bd9Sstevel@tonic-gate 	prval("ipv6InTooBigErrors",	ip6->ipv6InTooBigErrors);
21687c478bd9Sstevel@tonic-gate 	prval("ipv6InNoRoutes",		ip6->ipv6InNoRoutes);
21697c478bd9Sstevel@tonic-gate 	prval("ipv6InAddrErrors",	ip6->ipv6InAddrErrors);
21707c478bd9Sstevel@tonic-gate 	prval("ipv6InUnknownProtos",	ip6->ipv6InUnknownProtos);
21717c478bd9Sstevel@tonic-gate 	prval("ipv6InTruncatedPkts",	ip6->ipv6InTruncatedPkts);
21727c478bd9Sstevel@tonic-gate 	prval("ipv6InDiscards",		ip6->ipv6InDiscards);
21737c478bd9Sstevel@tonic-gate 	prval("ipv6InDelivers",		ip6->ipv6InDelivers);
21747c478bd9Sstevel@tonic-gate 	prval("ipv6OutForwDatagrams",	ip6->ipv6OutForwDatagrams);
21757c478bd9Sstevel@tonic-gate 	prval("ipv6OutRequests",	ip6->ipv6OutRequests);
21767c478bd9Sstevel@tonic-gate 	prval("ipv6OutDiscards",	ip6->ipv6OutDiscards);
21777c478bd9Sstevel@tonic-gate 	prval("ipv6OutNoRoutes",	ip6->ipv6OutNoRoutes);
21787c478bd9Sstevel@tonic-gate 	prval("ipv6OutFragOKs",		ip6->ipv6OutFragOKs);
21797c478bd9Sstevel@tonic-gate 	prval("ipv6OutFragFails",	ip6->ipv6OutFragFails);
21807c478bd9Sstevel@tonic-gate 	prval("ipv6OutFragCreates",	ip6->ipv6OutFragCreates);
21817c478bd9Sstevel@tonic-gate 	prval("ipv6ReasmReqds",		ip6->ipv6ReasmReqds);
21827c478bd9Sstevel@tonic-gate 	prval("ipv6ReasmOKs",		ip6->ipv6ReasmOKs);
21837c478bd9Sstevel@tonic-gate 	prval("ipv6ReasmFails",		ip6->ipv6ReasmFails);
21847c478bd9Sstevel@tonic-gate 	prval("ipv6InMcastPkts",	ip6->ipv6InMcastPkts);
21857c478bd9Sstevel@tonic-gate 	prval("ipv6OutMcastPkts",	ip6->ipv6OutMcastPkts);
21867c478bd9Sstevel@tonic-gate 	prval("ipv6ReasmDuplicates",	ip6->ipv6ReasmDuplicates);
21877c478bd9Sstevel@tonic-gate 	prval("ipv6ReasmPartDups",	ip6->ipv6ReasmPartDups);
21887c478bd9Sstevel@tonic-gate 	prval("ipv6ForwProhibits",	ip6->ipv6ForwProhibits);
21897c478bd9Sstevel@tonic-gate 	prval("udpInCksumErrs",		ip6->udpInCksumErrs);
21907c478bd9Sstevel@tonic-gate 	prval("udpInOverflows",		ip6->udpInOverflows);
21917c478bd9Sstevel@tonic-gate 	prval("rawipInOverflows",	ip6->rawipInOverflows);
21927c478bd9Sstevel@tonic-gate 	prval("ipv6InIPv4",		ip6->ipv6InIPv4);
21937c478bd9Sstevel@tonic-gate 	prval("ipv6OutIPv4",		ip6->ipv6OutIPv4);
21947c478bd9Sstevel@tonic-gate 	prval("ipv6OutSwitchIPv4",	ip6->ipv6OutSwitchIPv4);
21957c478bd9Sstevel@tonic-gate 	prval_end();
21967c478bd9Sstevel@tonic-gate }
21977c478bd9Sstevel@tonic-gate 
21987c478bd9Sstevel@tonic-gate static void
21997c478bd9Sstevel@tonic-gate print_icmp6_stats(mib2_ipv6IfIcmpEntry_t *icmp6)
22007c478bd9Sstevel@tonic-gate {
22017c478bd9Sstevel@tonic-gate 	prval_init();
22027c478bd9Sstevel@tonic-gate 	prval("icmp6InMsgs",		icmp6->ipv6IfIcmpInMsgs);
22037c478bd9Sstevel@tonic-gate 	prval("icmp6InErrors",		icmp6->ipv6IfIcmpInErrors);
22047c478bd9Sstevel@tonic-gate 	prval("icmp6InDestUnreachs",	icmp6->ipv6IfIcmpInDestUnreachs);
22057c478bd9Sstevel@tonic-gate 	prval("icmp6InAdminProhibs",	icmp6->ipv6IfIcmpInAdminProhibs);
22067c478bd9Sstevel@tonic-gate 	prval("icmp6InTimeExcds",	icmp6->ipv6IfIcmpInTimeExcds);
22077c478bd9Sstevel@tonic-gate 	prval("icmp6InParmProblems",	icmp6->ipv6IfIcmpInParmProblems);
22087c478bd9Sstevel@tonic-gate 	prval("icmp6InPktTooBigs",	icmp6->ipv6IfIcmpInPktTooBigs);
22097c478bd9Sstevel@tonic-gate 	prval("icmp6InEchos",		icmp6->ipv6IfIcmpInEchos);
22107c478bd9Sstevel@tonic-gate 	prval("icmp6InEchoReplies",	icmp6->ipv6IfIcmpInEchoReplies);
22117c478bd9Sstevel@tonic-gate 	prval("icmp6InRouterSols",	icmp6->ipv6IfIcmpInRouterSolicits);
22127c478bd9Sstevel@tonic-gate 	prval("icmp6InRouterAds",
22137c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpInRouterAdvertisements);
22147c478bd9Sstevel@tonic-gate 	prval("icmp6InNeighborSols",	icmp6->ipv6IfIcmpInNeighborSolicits);
22157c478bd9Sstevel@tonic-gate 	prval("icmp6InNeighborAds",
22167c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpInNeighborAdvertisements);
22177c478bd9Sstevel@tonic-gate 	prval("icmp6InRedirects",	icmp6->ipv6IfIcmpInRedirects);
22187c478bd9Sstevel@tonic-gate 	prval("icmp6InBadRedirects",	icmp6->ipv6IfIcmpInBadRedirects);
22197c478bd9Sstevel@tonic-gate 	prval("icmp6InGroupQueries",	icmp6->ipv6IfIcmpInGroupMembQueries);
22207c478bd9Sstevel@tonic-gate 	prval("icmp6InGroupResps",	icmp6->ipv6IfIcmpInGroupMembResponses);
22217c478bd9Sstevel@tonic-gate 	prval("icmp6InGroupReds",	icmp6->ipv6IfIcmpInGroupMembReductions);
22227c478bd9Sstevel@tonic-gate 	prval("icmp6InOverflows",	icmp6->ipv6IfIcmpInOverflows);
22237c478bd9Sstevel@tonic-gate 	prval_end();
22247c478bd9Sstevel@tonic-gate 	prval_init();
22257c478bd9Sstevel@tonic-gate 	prval("icmp6OutMsgs",		icmp6->ipv6IfIcmpOutMsgs);
22267c478bd9Sstevel@tonic-gate 	prval("icmp6OutErrors",		icmp6->ipv6IfIcmpOutErrors);
22277c478bd9Sstevel@tonic-gate 	prval("icmp6OutDestUnreachs",	icmp6->ipv6IfIcmpOutDestUnreachs);
22287c478bd9Sstevel@tonic-gate 	prval("icmp6OutAdminProhibs",	icmp6->ipv6IfIcmpOutAdminProhibs);
22297c478bd9Sstevel@tonic-gate 	prval("icmp6OutTimeExcds",	icmp6->ipv6IfIcmpOutTimeExcds);
22307c478bd9Sstevel@tonic-gate 	prval("icmp6OutParmProblems",	icmp6->ipv6IfIcmpOutParmProblems);
22317c478bd9Sstevel@tonic-gate 	prval("icmp6OutPktTooBigs",	icmp6->ipv6IfIcmpOutPktTooBigs);
22327c478bd9Sstevel@tonic-gate 	prval("icmp6OutEchos",		icmp6->ipv6IfIcmpOutEchos);
22337c478bd9Sstevel@tonic-gate 	prval("icmp6OutEchoReplies",	icmp6->ipv6IfIcmpOutEchoReplies);
22347c478bd9Sstevel@tonic-gate 	prval("icmp6OutRouterSols",	icmp6->ipv6IfIcmpOutRouterSolicits);
22357c478bd9Sstevel@tonic-gate 	prval("icmp6OutRouterAds",
22367c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpOutRouterAdvertisements);
22377c478bd9Sstevel@tonic-gate 	prval("icmp6OutNeighborSols",	icmp6->ipv6IfIcmpOutNeighborSolicits);
22387c478bd9Sstevel@tonic-gate 	prval("icmp6OutNeighborAds",
22397c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpOutNeighborAdvertisements);
22407c478bd9Sstevel@tonic-gate 	prval("icmp6OutRedirects",	icmp6->ipv6IfIcmpOutRedirects);
22417c478bd9Sstevel@tonic-gate 	prval("icmp6OutGroupQueries",	icmp6->ipv6IfIcmpOutGroupMembQueries);
22427c478bd9Sstevel@tonic-gate 	prval("icmp6OutGroupResps",
22437c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpOutGroupMembResponses);
22447c478bd9Sstevel@tonic-gate 	prval("icmp6OutGroupReds",
22457c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpOutGroupMembReductions);
22467c478bd9Sstevel@tonic-gate 	prval_end();
22477c478bd9Sstevel@tonic-gate }
22487c478bd9Sstevel@tonic-gate 
22497c478bd9Sstevel@tonic-gate static void
22507c478bd9Sstevel@tonic-gate print_sctp_stats(mib2_sctp_t *sctp)
22517c478bd9Sstevel@tonic-gate {
22527c478bd9Sstevel@tonic-gate 	prval_init();
22537c478bd9Sstevel@tonic-gate 	pr_sctp_rtoalgo("sctpRtoAlgorithm", sctp->sctpRtoAlgorithm);
22547c478bd9Sstevel@tonic-gate 	prval("sctpRtoMin",		sctp->sctpRtoMin);
22557c478bd9Sstevel@tonic-gate 	prval("sctpRtoMax",		sctp->sctpRtoMax);
22567c478bd9Sstevel@tonic-gate 	prval("sctpRtoInitial",		sctp->sctpRtoInitial);
22577c478bd9Sstevel@tonic-gate 	pr_int_val("sctpMaxAssocs",	sctp->sctpMaxAssocs);
22587c478bd9Sstevel@tonic-gate 	prval("sctpValCookieLife",	sctp->sctpValCookieLife);
22597c478bd9Sstevel@tonic-gate 	prval("sctpMaxInitRetr",	sctp->sctpMaxInitRetr);
22607c478bd9Sstevel@tonic-gate 	prval("sctpCurrEstab",		sctp->sctpCurrEstab);
22617c478bd9Sstevel@tonic-gate 	prval("sctpActiveEstab",	sctp->sctpActiveEstab);
22627c478bd9Sstevel@tonic-gate 	prval("sctpPassiveEstab",	sctp->sctpPassiveEstab);
22637c478bd9Sstevel@tonic-gate 	prval("sctpAborted",		sctp->sctpAborted);
22647c478bd9Sstevel@tonic-gate 	prval("sctpShutdowns",		sctp->sctpShutdowns);
22657c478bd9Sstevel@tonic-gate 	prval("sctpOutOfBlue",		sctp->sctpOutOfBlue);
22667c478bd9Sstevel@tonic-gate 	prval("sctpChecksumError",	sctp->sctpChecksumError);
22677c478bd9Sstevel@tonic-gate 	prval64("sctpOutCtrlChunks",	sctp->sctpOutCtrlChunks);
22687c478bd9Sstevel@tonic-gate 	prval64("sctpOutOrderChunks",	sctp->sctpOutOrderChunks);
22697c478bd9Sstevel@tonic-gate 	prval64("sctpOutUnorderChunks",	sctp->sctpOutUnorderChunks);
22707c478bd9Sstevel@tonic-gate 	prval64("sctpRetransChunks",	sctp->sctpRetransChunks);
22717c478bd9Sstevel@tonic-gate 	prval("sctpOutAck",		sctp->sctpOutAck);
22727c478bd9Sstevel@tonic-gate 	prval("sctpOutAckDelayed",	sctp->sctpOutAckDelayed);
22737c478bd9Sstevel@tonic-gate 	prval("sctpOutWinUpdate",	sctp->sctpOutWinUpdate);
22747c478bd9Sstevel@tonic-gate 	prval("sctpOutFastRetrans",	sctp->sctpOutFastRetrans);
22757c478bd9Sstevel@tonic-gate 	prval("sctpOutWinProbe",	sctp->sctpOutWinProbe);
22767c478bd9Sstevel@tonic-gate 	prval64("sctpInCtrlChunks",	sctp->sctpInCtrlChunks);
22777c478bd9Sstevel@tonic-gate 	prval64("sctpInOrderChunks",	sctp->sctpInOrderChunks);
22787c478bd9Sstevel@tonic-gate 	prval64("sctpInUnorderChunks",	sctp->sctpInUnorderChunks);
22797c478bd9Sstevel@tonic-gate 	prval("sctpInAck",		sctp->sctpInAck);
22807c478bd9Sstevel@tonic-gate 	prval("sctpInDupAck",		sctp->sctpInDupAck);
22817c478bd9Sstevel@tonic-gate 	prval("sctpInAckUnsent",	sctp->sctpInAckUnsent);
22827c478bd9Sstevel@tonic-gate 	prval64("sctpFragUsrMsgs",	sctp->sctpFragUsrMsgs);
22837c478bd9Sstevel@tonic-gate 	prval64("sctpReasmUsrMsgs",	sctp->sctpReasmUsrMsgs);
22847c478bd9Sstevel@tonic-gate 	prval64("sctpOutSCTPPkts",	sctp->sctpOutSCTPPkts);
22857c478bd9Sstevel@tonic-gate 	prval64("sctpInSCTPPkts",	sctp->sctpInSCTPPkts);
22867c478bd9Sstevel@tonic-gate 	prval("sctpInInvalidCookie",	sctp->sctpInInvalidCookie);
22877c478bd9Sstevel@tonic-gate 	prval("sctpTimRetrans",		sctp->sctpTimRetrans);
22887c478bd9Sstevel@tonic-gate 	prval("sctpTimRetransDrop",	sctp->sctpTimRetransDrop);
22897c478bd9Sstevel@tonic-gate 	prval("sctpTimHearBeatProbe",	sctp->sctpTimHeartBeatProbe);
22907c478bd9Sstevel@tonic-gate 	prval("sctpTimHearBeatDrop",	sctp->sctpTimHeartBeatDrop);
22917c478bd9Sstevel@tonic-gate 	prval("sctpListenDrop",		sctp->sctpListenDrop);
22927c478bd9Sstevel@tonic-gate 	prval("sctpInClosed",		sctp->sctpInClosed);
22937c478bd9Sstevel@tonic-gate 	prval_end();
22947c478bd9Sstevel@tonic-gate }
22957c478bd9Sstevel@tonic-gate 
22967c478bd9Sstevel@tonic-gate static void
22977c478bd9Sstevel@tonic-gate print_tcp_stats(mib2_tcp_t *tcp)
22987c478bd9Sstevel@tonic-gate {
22997c478bd9Sstevel@tonic-gate 	prval_init();
23007c478bd9Sstevel@tonic-gate 	pr_int_val("tcpRtoAlgorithm",	tcp->tcpRtoAlgorithm);
23017c478bd9Sstevel@tonic-gate 	pr_int_val("tcpRtoMin",		tcp->tcpRtoMin);
23027c478bd9Sstevel@tonic-gate 	pr_int_val("tcpRtoMax",		tcp->tcpRtoMax);
23037c478bd9Sstevel@tonic-gate 	pr_int_val("tcpMaxConn",	tcp->tcpMaxConn);
23047c478bd9Sstevel@tonic-gate 	prval("tcpActiveOpens",		tcp->tcpActiveOpens);
23057c478bd9Sstevel@tonic-gate 	prval("tcpPassiveOpens",	tcp->tcpPassiveOpens);
23067c478bd9Sstevel@tonic-gate 	prval("tcpAttemptFails",	tcp->tcpAttemptFails);
23077c478bd9Sstevel@tonic-gate 	prval("tcpEstabResets",		tcp->tcpEstabResets);
23087c478bd9Sstevel@tonic-gate 	prval("tcpCurrEstab",		tcp->tcpCurrEstab);
23093173664eSapersson 	prval64("tcpOutSegs",		tcp->tcpHCOutSegs);
23107c478bd9Sstevel@tonic-gate 	prval("tcpOutDataSegs",		tcp->tcpOutDataSegs);
23117c478bd9Sstevel@tonic-gate 	prval("tcpOutDataBytes",	tcp->tcpOutDataBytes);
23127c478bd9Sstevel@tonic-gate 	prval("tcpRetransSegs",		tcp->tcpRetransSegs);
23137c478bd9Sstevel@tonic-gate 	prval("tcpRetransBytes",	tcp->tcpRetransBytes);
23147c478bd9Sstevel@tonic-gate 	prval("tcpOutAck",		tcp->tcpOutAck);
23157c478bd9Sstevel@tonic-gate 	prval("tcpOutAckDelayed",	tcp->tcpOutAckDelayed);
23167c478bd9Sstevel@tonic-gate 	prval("tcpOutUrg",		tcp->tcpOutUrg);
23177c478bd9Sstevel@tonic-gate 	prval("tcpOutWinUpdate",	tcp->tcpOutWinUpdate);
23187c478bd9Sstevel@tonic-gate 	prval("tcpOutWinProbe",		tcp->tcpOutWinProbe);
23197c478bd9Sstevel@tonic-gate 	prval("tcpOutControl",		tcp->tcpOutControl);
23207c478bd9Sstevel@tonic-gate 	prval("tcpOutRsts",		tcp->tcpOutRsts);
23217c478bd9Sstevel@tonic-gate 	prval("tcpOutFastRetrans",	tcp->tcpOutFastRetrans);
23223173664eSapersson 	prval64("tcpInSegs",		tcp->tcpHCInSegs);
23237c478bd9Sstevel@tonic-gate 	prval_end();
23247c478bd9Sstevel@tonic-gate 	prval("tcpInAckSegs",		tcp->tcpInAckSegs);
23257c478bd9Sstevel@tonic-gate 	prval("tcpInAckBytes",		tcp->tcpInAckBytes);
23267c478bd9Sstevel@tonic-gate 	prval("tcpInDupAck",		tcp->tcpInDupAck);
23277c478bd9Sstevel@tonic-gate 	prval("tcpInAckUnsent",		tcp->tcpInAckUnsent);
23287c478bd9Sstevel@tonic-gate 	prval("tcpInInorderSegs",	tcp->tcpInDataInorderSegs);
23297c478bd9Sstevel@tonic-gate 	prval("tcpInInorderBytes",	tcp->tcpInDataInorderBytes);
23307c478bd9Sstevel@tonic-gate 	prval("tcpInUnorderSegs",	tcp->tcpInDataUnorderSegs);
23317c478bd9Sstevel@tonic-gate 	prval("tcpInUnorderBytes",	tcp->tcpInDataUnorderBytes);
23327c478bd9Sstevel@tonic-gate 	prval("tcpInDupSegs",		tcp->tcpInDataDupSegs);
23337c478bd9Sstevel@tonic-gate 	prval("tcpInDupBytes",		tcp->tcpInDataDupBytes);
23347c478bd9Sstevel@tonic-gate 	prval("tcpInPartDupSegs",	tcp->tcpInDataPartDupSegs);
23357c478bd9Sstevel@tonic-gate 	prval("tcpInPartDupBytes",	tcp->tcpInDataPartDupBytes);
23367c478bd9Sstevel@tonic-gate 	prval("tcpInPastWinSegs",	tcp->tcpInDataPastWinSegs);
23377c478bd9Sstevel@tonic-gate 	prval("tcpInPastWinBytes",	tcp->tcpInDataPastWinBytes);
23387c478bd9Sstevel@tonic-gate 	prval("tcpInWinProbe",		tcp->tcpInWinProbe);
23397c478bd9Sstevel@tonic-gate 	prval("tcpInWinUpdate",		tcp->tcpInWinUpdate);
23407c478bd9Sstevel@tonic-gate 	prval("tcpInClosed",		tcp->tcpInClosed);
23417c478bd9Sstevel@tonic-gate 	prval("tcpRttNoUpdate",		tcp->tcpRttNoUpdate);
23427c478bd9Sstevel@tonic-gate 	prval("tcpRttUpdate",		tcp->tcpRttUpdate);
23437c478bd9Sstevel@tonic-gate 	prval("tcpTimRetrans",		tcp->tcpTimRetrans);
23447c478bd9Sstevel@tonic-gate 	prval("tcpTimRetransDrop",	tcp->tcpTimRetransDrop);
23457c478bd9Sstevel@tonic-gate 	prval("tcpTimKeepalive",	tcp->tcpTimKeepalive);
23467c478bd9Sstevel@tonic-gate 	prval("tcpTimKeepaliveProbe",	tcp->tcpTimKeepaliveProbe);
23477c478bd9Sstevel@tonic-gate 	prval("tcpTimKeepaliveDrop",	tcp->tcpTimKeepaliveDrop);
23487c478bd9Sstevel@tonic-gate 	prval("tcpListenDrop",		tcp->tcpListenDrop);
23497c478bd9Sstevel@tonic-gate 	prval("tcpListenDropQ0",	tcp->tcpListenDropQ0);
23507c478bd9Sstevel@tonic-gate 	prval("tcpHalfOpenDrop",	tcp->tcpHalfOpenDrop);
23517c478bd9Sstevel@tonic-gate 	prval("tcpOutSackRetrans",	tcp->tcpOutSackRetransSegs);
23527c478bd9Sstevel@tonic-gate 	prval_end();
23537c478bd9Sstevel@tonic-gate 
23547c478bd9Sstevel@tonic-gate }
23557c478bd9Sstevel@tonic-gate 
23567c478bd9Sstevel@tonic-gate static void
23577c478bd9Sstevel@tonic-gate print_udp_stats(mib2_udp_t *udp)
23587c478bd9Sstevel@tonic-gate {
23597c478bd9Sstevel@tonic-gate 	prval_init();
23603173664eSapersson 	prval64("udpInDatagrams",	udp->udpHCInDatagrams);
23617c478bd9Sstevel@tonic-gate 	prval("udpInErrors",		udp->udpInErrors);
23623173664eSapersson 	prval64("udpOutDatagrams",	udp->udpHCOutDatagrams);
23637c478bd9Sstevel@tonic-gate 	prval("udpOutErrors",		udp->udpOutErrors);
23647c478bd9Sstevel@tonic-gate 	prval_end();
23657c478bd9Sstevel@tonic-gate }
23667c478bd9Sstevel@tonic-gate 
23677c478bd9Sstevel@tonic-gate static void
23687c478bd9Sstevel@tonic-gate print_rawip_stats(mib2_rawip_t *rawip)
23697c478bd9Sstevel@tonic-gate {
23707c478bd9Sstevel@tonic-gate 	prval_init();
23717c478bd9Sstevel@tonic-gate 	prval("rawipInDatagrams",	rawip->rawipInDatagrams);
23727c478bd9Sstevel@tonic-gate 	prval("rawipInErrors",		rawip->rawipInErrors);
23737c478bd9Sstevel@tonic-gate 	prval("rawipInCksumErrs",	rawip->rawipInCksumErrs);
23747c478bd9Sstevel@tonic-gate 	prval("rawipOutDatagrams",	rawip->rawipOutDatagrams);
23757c478bd9Sstevel@tonic-gate 	prval("rawipOutErrors",		rawip->rawipOutErrors);
23767c478bd9Sstevel@tonic-gate 	prval_end();
23777c478bd9Sstevel@tonic-gate }
23787c478bd9Sstevel@tonic-gate 
23797c478bd9Sstevel@tonic-gate void
23807c478bd9Sstevel@tonic-gate print_igmp_stats(struct igmpstat *igps)
23817c478bd9Sstevel@tonic-gate {
23827c478bd9Sstevel@tonic-gate 	(void) printf(" %10u message%s received\n",
23837c478bd9Sstevel@tonic-gate 	    igps->igps_rcv_total, PLURAL(igps->igps_rcv_total));
23847c478bd9Sstevel@tonic-gate 	(void) printf(" %10u message%s received with too few bytes\n",
23857c478bd9Sstevel@tonic-gate 	    igps->igps_rcv_tooshort, PLURAL(igps->igps_rcv_tooshort));
23867c478bd9Sstevel@tonic-gate 	(void) printf(" %10u message%s received with bad checksum\n",
23877c478bd9Sstevel@tonic-gate 	    igps->igps_rcv_badsum, PLURAL(igps->igps_rcv_badsum));
23887c478bd9Sstevel@tonic-gate 	(void) printf(" %10u membership quer%s received\n",
23897c478bd9Sstevel@tonic-gate 	    igps->igps_rcv_queries, PLURALY(igps->igps_rcv_queries));
23907c478bd9Sstevel@tonic-gate 	(void) printf(" %10u membership quer%s received with invalid "
23917c478bd9Sstevel@tonic-gate 	    "field(s)\n",
23927c478bd9Sstevel@tonic-gate 	    igps->igps_rcv_badqueries, PLURALY(igps->igps_rcv_badqueries));
23937c478bd9Sstevel@tonic-gate 	(void) printf(" %10u membership report%s received\n",
23947c478bd9Sstevel@tonic-gate 	    igps->igps_rcv_reports, PLURAL(igps->igps_rcv_reports));
23957c478bd9Sstevel@tonic-gate 	(void) printf(" %10u membership report%s received with invalid "
23967c478bd9Sstevel@tonic-gate 	    "field(s)\n",
23977c478bd9Sstevel@tonic-gate 	    igps->igps_rcv_badreports, PLURAL(igps->igps_rcv_badreports));
23987c478bd9Sstevel@tonic-gate 	(void) printf(" %10u membership report%s received for groups to "
23997c478bd9Sstevel@tonic-gate 	    "which we belong\n",
24007c478bd9Sstevel@tonic-gate 	    igps->igps_rcv_ourreports, PLURAL(igps->igps_rcv_ourreports));
24017c478bd9Sstevel@tonic-gate 	(void) printf(" %10u membership report%s sent\n",
24027c478bd9Sstevel@tonic-gate 	    igps->igps_snd_reports, PLURAL(igps->igps_snd_reports));
24037c478bd9Sstevel@tonic-gate }
24047c478bd9Sstevel@tonic-gate 
24057c478bd9Sstevel@tonic-gate static void
24067c478bd9Sstevel@tonic-gate print_mrt_stats(struct mrtstat *mrts)
24077c478bd9Sstevel@tonic-gate {
24087c478bd9Sstevel@tonic-gate 	(void) puts("DVMRP multicast routing:");
24097c478bd9Sstevel@tonic-gate 	(void) printf(" %10u hit%s - kernel forwarding cache hits\n",
2410e11c3f44Smeem 	    mrts->mrts_mfc_hits, PLURAL(mrts->mrts_mfc_hits));
24117c478bd9Sstevel@tonic-gate 	(void) printf(" %10u miss%s - kernel forwarding cache misses\n",
2412e11c3f44Smeem 	    mrts->mrts_mfc_misses, PLURALES(mrts->mrts_mfc_misses));
24137c478bd9Sstevel@tonic-gate 	(void) printf(" %10u packet%s potentially forwarded\n",
2414e11c3f44Smeem 	    mrts->mrts_fwd_in, PLURAL(mrts->mrts_fwd_in));
24157c478bd9Sstevel@tonic-gate 	(void) printf(" %10u packet%s actually sent out\n",
2416e11c3f44Smeem 	    mrts->mrts_fwd_out, PLURAL(mrts->mrts_fwd_out));
24177c478bd9Sstevel@tonic-gate 	(void) printf(" %10u upcall%s - upcalls made to mrouted\n",
2418e11c3f44Smeem 	    mrts->mrts_upcalls, PLURAL(mrts->mrts_upcalls));
24197c478bd9Sstevel@tonic-gate 	(void) printf(" %10u packet%s not sent out due to lack of resources\n",
2420e11c3f44Smeem 	    mrts->mrts_fwd_drop, PLURAL(mrts->mrts_fwd_drop));
24217c478bd9Sstevel@tonic-gate 	(void) printf(" %10u datagram%s with malformed tunnel options\n",
2422e11c3f44Smeem 	    mrts->mrts_bad_tunnel, PLURAL(mrts->mrts_bad_tunnel));
24237c478bd9Sstevel@tonic-gate 	(void) printf(" %10u datagram%s with no room for tunnel options\n",
2424e11c3f44Smeem 	    mrts->mrts_cant_tunnel, PLURAL(mrts->mrts_cant_tunnel));
24257c478bd9Sstevel@tonic-gate 	(void) printf(" %10u datagram%s arrived on wrong interface\n",
2426e11c3f44Smeem 	    mrts->mrts_wrong_if, PLURAL(mrts->mrts_wrong_if));
24277c478bd9Sstevel@tonic-gate 	(void) printf(" %10u datagram%s dropped due to upcall Q overflow\n",
2428e11c3f44Smeem 	    mrts->mrts_upq_ovflw, PLURAL(mrts->mrts_upq_ovflw));
24297c478bd9Sstevel@tonic-gate 	(void) printf(" %10u datagram%s cleaned up by the cache\n",
2430e11c3f44Smeem 	    mrts->mrts_cache_cleanups, PLURAL(mrts->mrts_cache_cleanups));
24317c478bd9Sstevel@tonic-gate 	(void) printf(" %10u datagram%s dropped selectively by ratelimiter\n",
2432e11c3f44Smeem 	    mrts->mrts_drop_sel, PLURAL(mrts->mrts_drop_sel));
24337c478bd9Sstevel@tonic-gate 	(void) printf(" %10u datagram%s dropped - bucket Q overflow\n",
2434e11c3f44Smeem 	    mrts->mrts_q_overflow, PLURAL(mrts->mrts_q_overflow));
24357c478bd9Sstevel@tonic-gate 	(void) printf(" %10u datagram%s dropped - larger than bkt size\n",
2436e11c3f44Smeem 	    mrts->mrts_pkt2large, PLURAL(mrts->mrts_pkt2large));
24377c478bd9Sstevel@tonic-gate 	(void) printf("\nPIM multicast routing:\n");
24387c478bd9Sstevel@tonic-gate 	(void) printf(" %10u datagram%s dropped - bad version number\n",
2439e11c3f44Smeem 	    mrts->mrts_pim_badversion, PLURAL(mrts->mrts_pim_badversion));
24407c478bd9Sstevel@tonic-gate 	(void) printf(" %10u datagram%s dropped - bad checksum\n",
2441e11c3f44Smeem 	    mrts->mrts_pim_rcv_badcsum, PLURAL(mrts->mrts_pim_rcv_badcsum));
24427c478bd9Sstevel@tonic-gate 	(void) printf(" %10u datagram%s dropped - bad register packets\n",
2443e11c3f44Smeem 	    mrts->mrts_pim_badregisters, PLURAL(mrts->mrts_pim_badregisters));
24447c478bd9Sstevel@tonic-gate 	(void) printf(
2445e11c3f44Smeem 	    " %10u datagram%s potentially forwarded - register packets\n",
2446e11c3f44Smeem 	    mrts->mrts_pim_regforwards, PLURAL(mrts->mrts_pim_regforwards));
24477c478bd9Sstevel@tonic-gate 	(void) printf(" %10u datagram%s dropped - register send drops\n",
2448e11c3f44Smeem 	    mrts->mrts_pim_regsend_drops, PLURAL(mrts->mrts_pim_regsend_drops));
24497c478bd9Sstevel@tonic-gate 	(void) printf(" %10u datagram%s dropped - packet malformed\n",
2450e11c3f44Smeem 	    mrts->mrts_pim_malformed, PLURAL(mrts->mrts_pim_malformed));
24517c478bd9Sstevel@tonic-gate 	(void) printf(" %10u datagram%s dropped - no memory to forward\n",
2452e11c3f44Smeem 	    mrts->mrts_pim_nomemory, PLURAL(mrts->mrts_pim_nomemory));
24537c478bd9Sstevel@tonic-gate }
24547c478bd9Sstevel@tonic-gate 
24557c478bd9Sstevel@tonic-gate static void
24567c478bd9Sstevel@tonic-gate sum_ip6_stats(mib2_ipv6IfStatsEntry_t *ip6, mib2_ipv6IfStatsEntry_t *sum6)
24577c478bd9Sstevel@tonic-gate {
24587c478bd9Sstevel@tonic-gate 	/* First few are not additive */
24597c478bd9Sstevel@tonic-gate 	sum6->ipv6Forwarding = ip6->ipv6Forwarding;
24607c478bd9Sstevel@tonic-gate 	sum6->ipv6DefaultHopLimit = ip6->ipv6DefaultHopLimit;
24617c478bd9Sstevel@tonic-gate 
24627c478bd9Sstevel@tonic-gate 	sum6->ipv6InReceives += ip6->ipv6InReceives;
24637c478bd9Sstevel@tonic-gate 	sum6->ipv6InHdrErrors += ip6->ipv6InHdrErrors;
24647c478bd9Sstevel@tonic-gate 	sum6->ipv6InTooBigErrors += ip6->ipv6InTooBigErrors;
24657c478bd9Sstevel@tonic-gate 	sum6->ipv6InNoRoutes += ip6->ipv6InNoRoutes;
24667c478bd9Sstevel@tonic-gate 	sum6->ipv6InAddrErrors += ip6->ipv6InAddrErrors;
24677c478bd9Sstevel@tonic-gate 	sum6->ipv6InUnknownProtos += ip6->ipv6InUnknownProtos;
24687c478bd9Sstevel@tonic-gate 	sum6->ipv6InTruncatedPkts += ip6->ipv6InTruncatedPkts;
24697c478bd9Sstevel@tonic-gate 	sum6->ipv6InDiscards += ip6->ipv6InDiscards;
24707c478bd9Sstevel@tonic-gate 	sum6->ipv6InDelivers += ip6->ipv6InDelivers;
24717c478bd9Sstevel@tonic-gate 	sum6->ipv6OutForwDatagrams += ip6->ipv6OutForwDatagrams;
24727c478bd9Sstevel@tonic-gate 	sum6->ipv6OutRequests += ip6->ipv6OutRequests;
24737c478bd9Sstevel@tonic-gate 	sum6->ipv6OutDiscards += ip6->ipv6OutDiscards;
24747c478bd9Sstevel@tonic-gate 	sum6->ipv6OutFragOKs += ip6->ipv6OutFragOKs;
24757c478bd9Sstevel@tonic-gate 	sum6->ipv6OutFragFails += ip6->ipv6OutFragFails;
24767c478bd9Sstevel@tonic-gate 	sum6->ipv6OutFragCreates += ip6->ipv6OutFragCreates;
24777c478bd9Sstevel@tonic-gate 	sum6->ipv6ReasmReqds += ip6->ipv6ReasmReqds;
24787c478bd9Sstevel@tonic-gate 	sum6->ipv6ReasmOKs += ip6->ipv6ReasmOKs;
24797c478bd9Sstevel@tonic-gate 	sum6->ipv6ReasmFails += ip6->ipv6ReasmFails;
24807c478bd9Sstevel@tonic-gate 	sum6->ipv6InMcastPkts += ip6->ipv6InMcastPkts;
24817c478bd9Sstevel@tonic-gate 	sum6->ipv6OutMcastPkts += ip6->ipv6OutMcastPkts;
24827c478bd9Sstevel@tonic-gate 	sum6->ipv6OutNoRoutes += ip6->ipv6OutNoRoutes;
24837c478bd9Sstevel@tonic-gate 	sum6->ipv6ReasmDuplicates += ip6->ipv6ReasmDuplicates;
24847c478bd9Sstevel@tonic-gate 	sum6->ipv6ReasmPartDups += ip6->ipv6ReasmPartDups;
24857c478bd9Sstevel@tonic-gate 	sum6->ipv6ForwProhibits += ip6->ipv6ForwProhibits;
24867c478bd9Sstevel@tonic-gate 	sum6->udpInCksumErrs += ip6->udpInCksumErrs;
24877c478bd9Sstevel@tonic-gate 	sum6->udpInOverflows += ip6->udpInOverflows;
24887c478bd9Sstevel@tonic-gate 	sum6->rawipInOverflows += ip6->rawipInOverflows;
24897c478bd9Sstevel@tonic-gate }
24907c478bd9Sstevel@tonic-gate 
24917c478bd9Sstevel@tonic-gate static void
24927c478bd9Sstevel@tonic-gate sum_icmp6_stats(mib2_ipv6IfIcmpEntry_t *icmp6, mib2_ipv6IfIcmpEntry_t *sum6)
24937c478bd9Sstevel@tonic-gate {
24947c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInMsgs += icmp6->ipv6IfIcmpInMsgs;
24957c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInErrors += icmp6->ipv6IfIcmpInErrors;
24967c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInDestUnreachs += icmp6->ipv6IfIcmpInDestUnreachs;
24977c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInAdminProhibs += icmp6->ipv6IfIcmpInAdminProhibs;
24987c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInTimeExcds += icmp6->ipv6IfIcmpInTimeExcds;
24997c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInParmProblems += icmp6->ipv6IfIcmpInParmProblems;
25007c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInPktTooBigs += icmp6->ipv6IfIcmpInPktTooBigs;
25017c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInEchos += icmp6->ipv6IfIcmpInEchos;
25027c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInEchoReplies += icmp6->ipv6IfIcmpInEchoReplies;
25037c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInRouterSolicits += icmp6->ipv6IfIcmpInRouterSolicits;
25047c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInRouterAdvertisements +=
25057c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpInRouterAdvertisements;
25067c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInNeighborSolicits +=
25077c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpInNeighborSolicits;
25087c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInNeighborAdvertisements +=
25097c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpInNeighborAdvertisements;
25107c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInRedirects += icmp6->ipv6IfIcmpInRedirects;
25117c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInGroupMembQueries +=
25127c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpInGroupMembQueries;
25137c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInGroupMembResponses +=
25147c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpInGroupMembResponses;
25157c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInGroupMembReductions +=
25167c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpInGroupMembReductions;
25177c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutMsgs += icmp6->ipv6IfIcmpOutMsgs;
25187c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutErrors += icmp6->ipv6IfIcmpOutErrors;
25197c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutDestUnreachs += icmp6->ipv6IfIcmpOutDestUnreachs;
25207c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutAdminProhibs += icmp6->ipv6IfIcmpOutAdminProhibs;
25217c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutTimeExcds += icmp6->ipv6IfIcmpOutTimeExcds;
25227c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutParmProblems += icmp6->ipv6IfIcmpOutParmProblems;
25237c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutPktTooBigs += icmp6->ipv6IfIcmpOutPktTooBigs;
25247c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutEchos += icmp6->ipv6IfIcmpOutEchos;
25257c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutEchoReplies += icmp6->ipv6IfIcmpOutEchoReplies;
25267c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutRouterSolicits +=
25277c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpOutRouterSolicits;
25287c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutRouterAdvertisements +=
25297c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpOutRouterAdvertisements;
25307c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutNeighborSolicits +=
25317c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpOutNeighborSolicits;
25327c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutNeighborAdvertisements +=
25337c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpOutNeighborAdvertisements;
25347c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutRedirects += icmp6->ipv6IfIcmpOutRedirects;
25357c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutGroupMembQueries +=
25367c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpOutGroupMembQueries;
25377c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutGroupMembResponses +=
25387c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpOutGroupMembResponses;
25397c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutGroupMembReductions +=
25407c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpOutGroupMembReductions;
25417c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInOverflows += icmp6->ipv6IfIcmpInOverflows;
25427c478bd9Sstevel@tonic-gate }
25437c478bd9Sstevel@tonic-gate 
25447c478bd9Sstevel@tonic-gate /* ----------------------------- MRT_STAT_REPORT --------------------------- */
25457c478bd9Sstevel@tonic-gate 
25467c478bd9Sstevel@tonic-gate static void
25477c478bd9Sstevel@tonic-gate mrt_stat_report(mib_item_t *curritem)
25487c478bd9Sstevel@tonic-gate {
25497c478bd9Sstevel@tonic-gate 	int	jtemp = 0;
25507c478bd9Sstevel@tonic-gate 	mib_item_t *tempitem;
25517c478bd9Sstevel@tonic-gate 
25527c478bd9Sstevel@tonic-gate 	if (!(family_selected(AF_INET)))
25537c478bd9Sstevel@tonic-gate 		return;
25547c478bd9Sstevel@tonic-gate 
25557c478bd9Sstevel@tonic-gate 	(void) putchar('\n');
25567c478bd9Sstevel@tonic-gate 	/* 'for' loop 1: */
25577c478bd9Sstevel@tonic-gate 	for (tempitem = curritem;
25587c478bd9Sstevel@tonic-gate 	    tempitem;
25597c478bd9Sstevel@tonic-gate 	    tempitem = tempitem->next_item) {
2560bd670b35SErik Nordmark 		if (Xflag) {
25617c478bd9Sstevel@tonic-gate 			(void) printf("\n--- Entry %d ---\n", ++jtemp);
25627c478bd9Sstevel@tonic-gate 			(void) printf("Group = %d, mib_id = %d, "
25637c478bd9Sstevel@tonic-gate 			    "length = %d, valp = 0x%p\n",
25647c478bd9Sstevel@tonic-gate 			    tempitem->group, tempitem->mib_id,
25657c478bd9Sstevel@tonic-gate 			    tempitem->length, tempitem->valp);
25667c478bd9Sstevel@tonic-gate 		}
25677c478bd9Sstevel@tonic-gate 
25687c478bd9Sstevel@tonic-gate 		if (tempitem->mib_id == 0) {
25697c478bd9Sstevel@tonic-gate 			switch (tempitem->group) {
25707c478bd9Sstevel@tonic-gate 			case EXPER_DVMRP: {
25717c478bd9Sstevel@tonic-gate 				struct mrtstat	*mrts;
25727c478bd9Sstevel@tonic-gate 				mrts = (struct mrtstat *)tempitem->valp;
25737c478bd9Sstevel@tonic-gate 
25747c478bd9Sstevel@tonic-gate 				if (!(family_selected(AF_INET)))
25757c478bd9Sstevel@tonic-gate 					continue; /* 'for' loop 1 */
25767c478bd9Sstevel@tonic-gate 
25777c478bd9Sstevel@tonic-gate 				print_mrt_stats(mrts);
25787c478bd9Sstevel@tonic-gate 				break;
25797c478bd9Sstevel@tonic-gate 			}
25807c478bd9Sstevel@tonic-gate 			}
25817c478bd9Sstevel@tonic-gate 		}
25827c478bd9Sstevel@tonic-gate 	} /* 'for' loop 1 ends */
25837c478bd9Sstevel@tonic-gate 	(void) putchar('\n');
25847c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
25857c478bd9Sstevel@tonic-gate }
25867c478bd9Sstevel@tonic-gate 
25877c478bd9Sstevel@tonic-gate /*
25887c478bd9Sstevel@tonic-gate  * if_stat_total() - Computes totals for interface statistics
25897c478bd9Sstevel@tonic-gate  *                   and returns result by updating sumstats.
25907c478bd9Sstevel@tonic-gate  */
25917c478bd9Sstevel@tonic-gate static void
25927c478bd9Sstevel@tonic-gate if_stat_total(struct ifstat *oldstats, struct ifstat *newstats,
25937c478bd9Sstevel@tonic-gate     struct ifstat *sumstats)
25947c478bd9Sstevel@tonic-gate {
25957c478bd9Sstevel@tonic-gate 	sumstats->ipackets += newstats->ipackets - oldstats->ipackets;
25967c478bd9Sstevel@tonic-gate 	sumstats->opackets += newstats->opackets - oldstats->opackets;
25977c478bd9Sstevel@tonic-gate 	sumstats->ierrors += newstats->ierrors - oldstats->ierrors;
25987c478bd9Sstevel@tonic-gate 	sumstats->oerrors += newstats->oerrors - oldstats->oerrors;
25997c478bd9Sstevel@tonic-gate 	sumstats->collisions += newstats->collisions - oldstats->collisions;
26007c478bd9Sstevel@tonic-gate }
26017c478bd9Sstevel@tonic-gate 
26027c478bd9Sstevel@tonic-gate /* --------------------- IF_REPORT (netstat -i)  -------------------------- */
26037c478bd9Sstevel@tonic-gate 
26047c478bd9Sstevel@tonic-gate static struct	ifstat	zerostat = {
26057c478bd9Sstevel@tonic-gate 	0LL, 0LL, 0LL, 0LL, 0LL
26067c478bd9Sstevel@tonic-gate };
26077c478bd9Sstevel@tonic-gate 
26087c478bd9Sstevel@tonic-gate static void
26097c478bd9Sstevel@tonic-gate if_report(mib_item_t *item, char *matchname,
26107c478bd9Sstevel@tonic-gate     int Iflag_only, boolean_t once_only)
26117c478bd9Sstevel@tonic-gate {
26127c478bd9Sstevel@tonic-gate 	static boolean_t	reentry = B_FALSE;
26137c478bd9Sstevel@tonic-gate 	boolean_t		alreadydone = B_FALSE;
26147c478bd9Sstevel@tonic-gate 	int			jtemp = 0;
26157c478bd9Sstevel@tonic-gate 	uint32_t		ifindex_v4 = 0;
26167c478bd9Sstevel@tonic-gate 	uint32_t		ifindex_v6 = 0;
2617ba753d4aSkeerthi 	boolean_t		first_header = B_TRUE;
26187c478bd9Sstevel@tonic-gate 
26197c478bd9Sstevel@tonic-gate 	/* 'for' loop 1: */
26207c478bd9Sstevel@tonic-gate 	for (; item; item = item->next_item) {
2621bd670b35SErik Nordmark 		if (Xflag) {
26227c478bd9Sstevel@tonic-gate 			(void) printf("\n--- Entry %d ---\n", ++jtemp);
26237c478bd9Sstevel@tonic-gate 			(void) printf("Group = %d, mib_id = %d, "
26247c478bd9Sstevel@tonic-gate 			    "length = %d, valp = 0x%p\n",
26257c478bd9Sstevel@tonic-gate 			    item->group, item->mib_id, item->length,
26267c478bd9Sstevel@tonic-gate 			    item->valp);
26277c478bd9Sstevel@tonic-gate 		}
26287c478bd9Sstevel@tonic-gate 
26297c478bd9Sstevel@tonic-gate 		switch (item->group) {
26307c478bd9Sstevel@tonic-gate 		case MIB2_IP:
26317c478bd9Sstevel@tonic-gate 		if (item->mib_id != MIB2_IP_ADDR ||
26327c478bd9Sstevel@tonic-gate 		    !family_selected(AF_INET))
26337c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
26347c478bd9Sstevel@tonic-gate 		{
26357c478bd9Sstevel@tonic-gate 			static struct ifstat	old = {0L, 0L, 0L, 0L, 0L};
26367c478bd9Sstevel@tonic-gate 			static struct ifstat	new = {0L, 0L, 0L, 0L, 0L};
26377c478bd9Sstevel@tonic-gate 			struct ifstat		sum;
26387c478bd9Sstevel@tonic-gate 			struct iflist		*newlist = NULL;
26397c478bd9Sstevel@tonic-gate 			static struct iflist	*oldlist = NULL;
26407c478bd9Sstevel@tonic-gate 			kstat_t	 *ksp;
26417c478bd9Sstevel@tonic-gate 
26427c478bd9Sstevel@tonic-gate 			if (once_only) {
26437c478bd9Sstevel@tonic-gate 				char    ifname[LIFNAMSIZ + 1];
26447c478bd9Sstevel@tonic-gate 				char    logintname[LIFNAMSIZ + 1];
26457c478bd9Sstevel@tonic-gate 				mib2_ipAddrEntry_t *ap;
26467c478bd9Sstevel@tonic-gate 				struct ifstat	stat = {0L, 0L, 0L, 0L, 0L};
26477c478bd9Sstevel@tonic-gate 				boolean_t	first = B_TRUE;
26487c478bd9Sstevel@tonic-gate 				uint32_t	new_ifindex;
26497c478bd9Sstevel@tonic-gate 
2650bd670b35SErik Nordmark 				if (Xflag)
26517c478bd9Sstevel@tonic-gate 					(void) printf("if_report: %d items\n",
26527c478bd9Sstevel@tonic-gate 					    (item->length)
26537c478bd9Sstevel@tonic-gate 					    / sizeof (mib2_ipAddrEntry_t));
26547c478bd9Sstevel@tonic-gate 
26557c478bd9Sstevel@tonic-gate 				/* 'for' loop 2a: */
26567c478bd9Sstevel@tonic-gate 				for (ap = (mib2_ipAddrEntry_t *)item->valp;
26577c478bd9Sstevel@tonic-gate 				    (char *)ap < (char *)item->valp
26587c478bd9Sstevel@tonic-gate 				    + item->length;
26597c478bd9Sstevel@tonic-gate 				    ap++) {
26607c478bd9Sstevel@tonic-gate 					(void) octetstr(&ap->ipAdEntIfIndex,
26617c478bd9Sstevel@tonic-gate 					    'a', logintname,
26627c478bd9Sstevel@tonic-gate 					    sizeof (logintname));
26637c478bd9Sstevel@tonic-gate 					(void) strcpy(ifname, logintname);
26647c478bd9Sstevel@tonic-gate 					(void) strtok(ifname, ":");
26657c478bd9Sstevel@tonic-gate 					if (matchname != NULL &&
26667c478bd9Sstevel@tonic-gate 					    strcmp(matchname, ifname) != 0 &&
26677c478bd9Sstevel@tonic-gate 					    strcmp(matchname, logintname) != 0)
26687c478bd9Sstevel@tonic-gate 						continue; /* 'for' loop 2a */
26697c478bd9Sstevel@tonic-gate 					new_ifindex =
26707c478bd9Sstevel@tonic-gate 					    if_nametoindex(logintname);
2671d62bc4baSyz 					/*
2672d62bc4baSyz 					 * First lookup the "link" kstats in
2673d62bc4baSyz 					 * case the link is renamed. Then
2674d62bc4baSyz 					 * fallback to the legacy kstats for
2675d62bc4baSyz 					 * those non-GLDv3 links.
2676d62bc4baSyz 					 */
26777c478bd9Sstevel@tonic-gate 					if (new_ifindex != ifindex_v4 &&
2678d62bc4baSyz 					    (((ksp = kstat_lookup(kc, "link", 0,
2679d62bc4baSyz 					    ifname)) != NULL) ||
2680d62bc4baSyz 					    ((ksp = kstat_lookup(kc, NULL, -1,
2681d62bc4baSyz 					    ifname)) != NULL))) {
26827c478bd9Sstevel@tonic-gate 						(void) safe_kstat_read(kc, ksp,
26837c478bd9Sstevel@tonic-gate 						    NULL);
26847c478bd9Sstevel@tonic-gate 						stat.ipackets =
26857c478bd9Sstevel@tonic-gate 						    kstat_named_value(ksp,
26867c478bd9Sstevel@tonic-gate 						    "ipackets");
26877c478bd9Sstevel@tonic-gate 						stat.ierrors =
26887c478bd9Sstevel@tonic-gate 						    kstat_named_value(ksp,
26897c478bd9Sstevel@tonic-gate 						    "ierrors");
26907c478bd9Sstevel@tonic-gate 						stat.opackets =
26917c478bd9Sstevel@tonic-gate 						    kstat_named_value(ksp,
26927c478bd9Sstevel@tonic-gate 						    "opackets");
26937c478bd9Sstevel@tonic-gate 						stat.oerrors =
26947c478bd9Sstevel@tonic-gate 						    kstat_named_value(ksp,
26957c478bd9Sstevel@tonic-gate 						    "oerrors");
26967c478bd9Sstevel@tonic-gate 						stat.collisions =
26977c478bd9Sstevel@tonic-gate 						    kstat_named_value(ksp,
26987c478bd9Sstevel@tonic-gate 						    "collisions");
26997c478bd9Sstevel@tonic-gate 						if (first) {
2700ba753d4aSkeerthi 							if (!first_header)
2701ba753d4aSkeerthi 							(void) putchar('\n');
2702ba753d4aSkeerthi 							first_header = B_FALSE;
27037c478bd9Sstevel@tonic-gate 						(void) printf(
27047c478bd9Sstevel@tonic-gate 						    "%-5.5s %-5.5s%-13.13s "
27057c478bd9Sstevel@tonic-gate 						    "%-14.14s %-6.6s %-5.5s "
27067c478bd9Sstevel@tonic-gate 						    "%-6.6s %-5.5s %-6.6s "
27077c478bd9Sstevel@tonic-gate 						    "%-6.6s\n",
27087c478bd9Sstevel@tonic-gate 						    "Name", "Mtu", "Net/Dest",
27097c478bd9Sstevel@tonic-gate 						    "Address", "Ipkts",
27107c478bd9Sstevel@tonic-gate 						    "Ierrs", "Opkts", "Oerrs",
27117c478bd9Sstevel@tonic-gate 						    "Collis", "Queue");
2712ba753d4aSkeerthi 
2713e11c3f44Smeem 						first = B_FALSE;
27147c478bd9Sstevel@tonic-gate 						}
27157c478bd9Sstevel@tonic-gate 						if_report_ip4(ap, ifname,
27167c478bd9Sstevel@tonic-gate 						    logintname, &stat, B_TRUE);
27177c478bd9Sstevel@tonic-gate 						ifindex_v4 = new_ifindex;
27187c478bd9Sstevel@tonic-gate 					} else {
27197c478bd9Sstevel@tonic-gate 						if_report_ip4(ap, ifname,
27207c478bd9Sstevel@tonic-gate 						    logintname, &stat, B_FALSE);
27217c478bd9Sstevel@tonic-gate 					}
27227c478bd9Sstevel@tonic-gate 				} /* 'for' loop 2a ends */
27237c478bd9Sstevel@tonic-gate 			} else if (!alreadydone) {
27247c478bd9Sstevel@tonic-gate 				char    ifname[LIFNAMSIZ + 1];
27257c478bd9Sstevel@tonic-gate 				char    buf[LIFNAMSIZ + 1];
27267c478bd9Sstevel@tonic-gate 				mib2_ipAddrEntry_t *ap;
27277c478bd9Sstevel@tonic-gate 				struct ifstat   t;
2728aecc8c24Sja 				struct iflist	*tlp = NULL;
27297c478bd9Sstevel@tonic-gate 				struct iflist	**nextnew = &newlist;
27307c478bd9Sstevel@tonic-gate 				struct iflist	*walkold;
27317c478bd9Sstevel@tonic-gate 				struct iflist	*cleanlist;
2732aecc8c24Sja 				boolean_t	found_if = B_FALSE;
27337c478bd9Sstevel@tonic-gate 
27347c478bd9Sstevel@tonic-gate 				alreadydone = B_TRUE; /* ignore other case */
2735aecc8c24Sja 
2736aecc8c24Sja 				/*
2737aecc8c24Sja 				 * Check if there is anything to do.
2738aecc8c24Sja 				 */
2739aecc8c24Sja 				if (item->length <
2740aecc8c24Sja 				    sizeof (mib2_ipAddrEntry_t)) {
2741aecc8c24Sja 					fail(0, "No compatible interfaces");
2742aecc8c24Sja 				}
2743aecc8c24Sja 
27447c478bd9Sstevel@tonic-gate 				/*
2745aecc8c24Sja 				 * 'for' loop 2b: find the "right" entry:
2746aecc8c24Sja 				 * If an interface name to match has been
2747aecc8c24Sja 				 * supplied then try and find it, otherwise
2748aecc8c24Sja 				 * match the first non-loopback interface found.
2749aecc8c24Sja 				 * Use lo0 if all else fails.
27507c478bd9Sstevel@tonic-gate 				 */
27517c478bd9Sstevel@tonic-gate 				for (ap = (mib2_ipAddrEntry_t *)item->valp;
27527c478bd9Sstevel@tonic-gate 				    (char *)ap < (char *)item->valp
27537c478bd9Sstevel@tonic-gate 				    + item->length;
27547c478bd9Sstevel@tonic-gate 				    ap++) {
27557c478bd9Sstevel@tonic-gate 					(void) octetstr(&ap->ipAdEntIfIndex,
2756e11c3f44Smeem 					    'a', ifname, sizeof (ifname));
27577c478bd9Sstevel@tonic-gate 					(void) strtok(ifname, ":");
27587c478bd9Sstevel@tonic-gate 
27597c478bd9Sstevel@tonic-gate 					if (matchname) {
27607c478bd9Sstevel@tonic-gate 						if (strcmp(matchname,
2761aecc8c24Sja 						    ifname) == 0) {
27627c478bd9Sstevel@tonic-gate 							/* 'for' loop 2b */
2763aecc8c24Sja 							found_if = B_TRUE;
27647c478bd9Sstevel@tonic-gate 							break;
2765aecc8c24Sja 						}
2766aecc8c24Sja 					} else if (strcmp(ifname, "lo0") != 0)
27677c478bd9Sstevel@tonic-gate 						break; /* 'for' loop 2b */
27687c478bd9Sstevel@tonic-gate 				} /* 'for' loop 2b ends */
27697c478bd9Sstevel@tonic-gate 
2770aecc8c24Sja 				if (matchname == NULL) {
2771aecc8c24Sja 					matchname = ifname;
2772aecc8c24Sja 				} else {
2773aecc8c24Sja 					if (!found_if)
2774aecc8c24Sja 						fail(0, "-I: %s no such "
2775aecc8c24Sja 						    "interface.", matchname);
2776aecc8c24Sja 				}
2777aecc8c24Sja 
27787c478bd9Sstevel@tonic-gate 				if (Iflag_only == 0 || !reentry) {
27797c478bd9Sstevel@tonic-gate 					(void) printf("    input   %-6.6s    "
27807c478bd9Sstevel@tonic-gate 					    "output	",
27817c478bd9Sstevel@tonic-gate 					    matchname);
27827c478bd9Sstevel@tonic-gate 					(void) printf("   input  (Total)    "
27837c478bd9Sstevel@tonic-gate 					"output\n");
27847c478bd9Sstevel@tonic-gate 					(void) printf("%-7.7s %-5.5s %-7.7s "
27857c478bd9Sstevel@tonic-gate 					    "%-5.5s %-6.6s ",
27867c478bd9Sstevel@tonic-gate 					    "packets", "errs", "packets",
27877c478bd9Sstevel@tonic-gate 					    "errs", "colls");
27887c478bd9Sstevel@tonic-gate 					(void) printf("%-7.7s %-5.5s %-7.7s "
27897c478bd9Sstevel@tonic-gate 					    "%-5.5s %-6.6s\n",
27907c478bd9Sstevel@tonic-gate 					    "packets", "errs", "packets",
27917c478bd9Sstevel@tonic-gate 					    "errs", "colls");
27927c478bd9Sstevel@tonic-gate 				}
27937c478bd9Sstevel@tonic-gate 
27947c478bd9Sstevel@tonic-gate 				sum = zerostat;
27957c478bd9Sstevel@tonic-gate 
27967c478bd9Sstevel@tonic-gate 				/* 'for' loop 2c: */
27977c478bd9Sstevel@tonic-gate 				for (ap = (mib2_ipAddrEntry_t *)item->valp;
27987c478bd9Sstevel@tonic-gate 				    (char *)ap < (char *)item->valp
27997c478bd9Sstevel@tonic-gate 				    + item->length;
28007c478bd9Sstevel@tonic-gate 				    ap++) {
28017c478bd9Sstevel@tonic-gate 					(void) octetstr(&ap->ipAdEntIfIndex,
28027c478bd9Sstevel@tonic-gate 					    'a', buf, sizeof (buf));
28037c478bd9Sstevel@tonic-gate 					(void) strtok(buf, ":");
2804aecc8c24Sja 
2805aecc8c24Sja 					/*
2806aecc8c24Sja 					 * We have reduced the IP interface
2807aecc8c24Sja 					 * name, which could have been a
2808aecc8c24Sja 					 * logical, down to a name suitable
2809aecc8c24Sja 					 * for use with kstats.
2810aecc8c24Sja 					 * We treat this name as unique and
2811aecc8c24Sja 					 * only collate statistics for it once
2812aecc8c24Sja 					 * per pass. This is to avoid falsely
2813aecc8c24Sja 					 * amplifying these statistics by the
2814aecc8c24Sja 					 * the number of logical instances.
2815aecc8c24Sja 					 */
2816aecc8c24Sja 					if ((tlp != NULL) &&
2817aecc8c24Sja 					    ((strcmp(buf, tlp->ifname) == 0))) {
2818aecc8c24Sja 						continue;
2819aecc8c24Sja 					}
2820aecc8c24Sja 
2821d62bc4baSyz 					/*
2822d62bc4baSyz 					 * First lookup the "link" kstats in
2823d62bc4baSyz 					 * case the link is renamed. Then
2824d62bc4baSyz 					 * fallback to the legacy kstats for
2825d62bc4baSyz 					 * those non-GLDv3 links.
2826d62bc4baSyz 					 */
2827d62bc4baSyz 					if (((ksp = kstat_lookup(kc, "link",
2828d62bc4baSyz 					    0, buf)) != NULL ||
2829d62bc4baSyz 					    (ksp = kstat_lookup(kc, NULL, -1,
2830d62bc4baSyz 					    buf)) != NULL) && (ksp->ks_type ==
2831d62bc4baSyz 					    KSTAT_TYPE_NAMED)) {
28327c478bd9Sstevel@tonic-gate 						(void) safe_kstat_read(kc, ksp,
28337c478bd9Sstevel@tonic-gate 						    NULL);
2834d62bc4baSyz 					}
28357c478bd9Sstevel@tonic-gate 
28367c478bd9Sstevel@tonic-gate 					t.ipackets = kstat_named_value(ksp,
28377c478bd9Sstevel@tonic-gate 					    "ipackets");
28387c478bd9Sstevel@tonic-gate 					t.ierrors = kstat_named_value(ksp,
28397c478bd9Sstevel@tonic-gate 					    "ierrors");
28407c478bd9Sstevel@tonic-gate 					t.opackets = kstat_named_value(ksp,
28417c478bd9Sstevel@tonic-gate 					    "opackets");
28427c478bd9Sstevel@tonic-gate 					t.oerrors = kstat_named_value(ksp,
28437c478bd9Sstevel@tonic-gate 					    "oerrors");
28447c478bd9Sstevel@tonic-gate 					t.collisions = kstat_named_value(ksp,
28457c478bd9Sstevel@tonic-gate 					    "collisions");
28467c478bd9Sstevel@tonic-gate 
28477c478bd9Sstevel@tonic-gate 					if (strcmp(buf, matchname) == 0)
28487c478bd9Sstevel@tonic-gate 						new = t;
28497c478bd9Sstevel@tonic-gate 
28507c478bd9Sstevel@tonic-gate 					/* Build the interface list */
28517c478bd9Sstevel@tonic-gate 
28527c478bd9Sstevel@tonic-gate 					tlp = malloc(sizeof (struct iflist));
28537c478bd9Sstevel@tonic-gate 					(void) strlcpy(tlp->ifname, buf,
28547c478bd9Sstevel@tonic-gate 					    sizeof (tlp->ifname));
28557c478bd9Sstevel@tonic-gate 					tlp->tot = t;
28567c478bd9Sstevel@tonic-gate 					*nextnew = tlp;
28577c478bd9Sstevel@tonic-gate 					nextnew = &tlp->next_if;
28587c478bd9Sstevel@tonic-gate 
28597c478bd9Sstevel@tonic-gate 					/*
28607c478bd9Sstevel@tonic-gate 					 * First time through.
28617c478bd9Sstevel@tonic-gate 					 * Just add up the interface stats.
28627c478bd9Sstevel@tonic-gate 					 */
28637c478bd9Sstevel@tonic-gate 
28647c478bd9Sstevel@tonic-gate 					if (oldlist == NULL) {
28657c478bd9Sstevel@tonic-gate 						if_stat_total(&zerostat,
28667c478bd9Sstevel@tonic-gate 						    &t, &sum);
28677c478bd9Sstevel@tonic-gate 						continue;
28687c478bd9Sstevel@tonic-gate 					}
28697c478bd9Sstevel@tonic-gate 
28707c478bd9Sstevel@tonic-gate 					/*
28717c478bd9Sstevel@tonic-gate 					 * Walk old list for the interface.
28727c478bd9Sstevel@tonic-gate 					 *
28737c478bd9Sstevel@tonic-gate 					 * If found, add difference to total.
28747c478bd9Sstevel@tonic-gate 					 *
28757c478bd9Sstevel@tonic-gate 					 * If not, an interface has been plumbed
28767c478bd9Sstevel@tonic-gate 					 * up.  In this case, we will simply
28777c478bd9Sstevel@tonic-gate 					 * ignore the new interface until the
28787c478bd9Sstevel@tonic-gate 					 * next interval; as there's no easy way
28797c478bd9Sstevel@tonic-gate 					 * to acquire statistics between time
28807c478bd9Sstevel@tonic-gate 					 * of the plumb and the next interval
28817c478bd9Sstevel@tonic-gate 					 * boundary.  This results in inaccurate
28827c478bd9Sstevel@tonic-gate 					 * total values for current interval.
28837c478bd9Sstevel@tonic-gate 					 *
28847c478bd9Sstevel@tonic-gate 					 * Note the case when an interface is
28857c478bd9Sstevel@tonic-gate 					 * unplumbed; as similar problems exist.
28867c478bd9Sstevel@tonic-gate 					 * The unplumbed interface is not in the
28877c478bd9Sstevel@tonic-gate 					 * current list, and there's no easy way
28887c478bd9Sstevel@tonic-gate 					 * to account for the statistics between
28897c478bd9Sstevel@tonic-gate 					 * the previous interval and time of the
28907c478bd9Sstevel@tonic-gate 					 * unplumb.  Therefore, we (in a sense)
28917c478bd9Sstevel@tonic-gate 					 * ignore the removed interface by only
28927c478bd9Sstevel@tonic-gate 					 * involving "current" interfaces when
28937c478bd9Sstevel@tonic-gate 					 * computing the total statistics.
28947c478bd9Sstevel@tonic-gate 					 * Unfortunately, this also results in
28957c478bd9Sstevel@tonic-gate 					 * inaccurate values for interval total.
28967c478bd9Sstevel@tonic-gate 					 */
28977c478bd9Sstevel@tonic-gate 
28987c478bd9Sstevel@tonic-gate 					for (walkold = oldlist;
28997c478bd9Sstevel@tonic-gate 					    walkold != NULL;
29007c478bd9Sstevel@tonic-gate 					    walkold = walkold->next_if) {
29017c478bd9Sstevel@tonic-gate 						if (strcmp(walkold->ifname,
29027c478bd9Sstevel@tonic-gate 						    buf) == 0) {
29037c478bd9Sstevel@tonic-gate 							if_stat_total(
29047c478bd9Sstevel@tonic-gate 							    &walkold->tot,
29057c478bd9Sstevel@tonic-gate 							    &t, &sum);
29067c478bd9Sstevel@tonic-gate 							break;
29077c478bd9Sstevel@tonic-gate 						}
29087c478bd9Sstevel@tonic-gate 					}
29097c478bd9Sstevel@tonic-gate 
29107c478bd9Sstevel@tonic-gate 				} /* 'for' loop 2c ends */
29117c478bd9Sstevel@tonic-gate 
29127c478bd9Sstevel@tonic-gate 				*nextnew = NULL;
29137c478bd9Sstevel@tonic-gate 
29147c478bd9Sstevel@tonic-gate 				(void) printf("%-7llu %-5llu %-7llu "
29157c478bd9Sstevel@tonic-gate 				    "%-5llu %-6llu ",
29167c478bd9Sstevel@tonic-gate 				    new.ipackets - old.ipackets,
29177c478bd9Sstevel@tonic-gate 				    new.ierrors - old.ierrors,
29187c478bd9Sstevel@tonic-gate 				    new.opackets - old.opackets,
29197c478bd9Sstevel@tonic-gate 				    new.oerrors - old.oerrors,
29207c478bd9Sstevel@tonic-gate 				    new.collisions - old.collisions);
29217c478bd9Sstevel@tonic-gate 
29227c478bd9Sstevel@tonic-gate 				(void) printf("%-7llu %-5llu %-7llu "
29237c478bd9Sstevel@tonic-gate 				    "%-5llu %-6llu\n", sum.ipackets,
29247c478bd9Sstevel@tonic-gate 				    sum.ierrors, sum.opackets,
29257c478bd9Sstevel@tonic-gate 				    sum.oerrors, sum.collisions);
29267c478bd9Sstevel@tonic-gate 
29277c478bd9Sstevel@tonic-gate 				/*
29287c478bd9Sstevel@tonic-gate 				 * Tidy things up once finished.
29297c478bd9Sstevel@tonic-gate 				 */
29307c478bd9Sstevel@tonic-gate 
29317c478bd9Sstevel@tonic-gate 				old = new;
29327c478bd9Sstevel@tonic-gate 				cleanlist = oldlist;
29337c478bd9Sstevel@tonic-gate 				oldlist = newlist;
29347c478bd9Sstevel@tonic-gate 				while (cleanlist != NULL) {
29357c478bd9Sstevel@tonic-gate 					tlp = cleanlist->next_if;
29367c478bd9Sstevel@tonic-gate 					free(cleanlist);
29377c478bd9Sstevel@tonic-gate 					cleanlist = tlp;
29387c478bd9Sstevel@tonic-gate 				}
29397c478bd9Sstevel@tonic-gate 			}
29407c478bd9Sstevel@tonic-gate 			break;
29417c478bd9Sstevel@tonic-gate 		}
29427c478bd9Sstevel@tonic-gate 		case MIB2_IP6:
29437c478bd9Sstevel@tonic-gate 		if (item->mib_id != MIB2_IP6_ADDR ||
29447c478bd9Sstevel@tonic-gate 		    !family_selected(AF_INET6))
29457c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
29467c478bd9Sstevel@tonic-gate 		{
29477c478bd9Sstevel@tonic-gate 			static struct ifstat	old6 = {0L, 0L, 0L, 0L, 0L};
29487c478bd9Sstevel@tonic-gate 			static struct ifstat	new6 = {0L, 0L, 0L, 0L, 0L};
29497c478bd9Sstevel@tonic-gate 			struct ifstat		sum6;
29507c478bd9Sstevel@tonic-gate 			struct iflist		*newlist6 = NULL;
29517c478bd9Sstevel@tonic-gate 			static struct iflist	*oldlist6 = NULL;
29527c478bd9Sstevel@tonic-gate 			kstat_t	 *ksp;
29537c478bd9Sstevel@tonic-gate 
29547c478bd9Sstevel@tonic-gate 			if (once_only) {
29557c478bd9Sstevel@tonic-gate 				char    ifname[LIFNAMSIZ + 1];
29567c478bd9Sstevel@tonic-gate 				char    logintname[LIFNAMSIZ + 1];
29577c478bd9Sstevel@tonic-gate 				mib2_ipv6AddrEntry_t *ap6;
29587c478bd9Sstevel@tonic-gate 				struct ifstat	stat = {0L, 0L, 0L, 0L, 0L};
29597c478bd9Sstevel@tonic-gate 				boolean_t	first = B_TRUE;
29607c478bd9Sstevel@tonic-gate 				uint32_t	new_ifindex;
29617c478bd9Sstevel@tonic-gate 
2962bd670b35SErik Nordmark 				if (Xflag)
29637c478bd9Sstevel@tonic-gate 					(void) printf("if_report: %d items\n",
29647c478bd9Sstevel@tonic-gate 					    (item->length)
29657c478bd9Sstevel@tonic-gate 					    / sizeof (mib2_ipv6AddrEntry_t));
29667c478bd9Sstevel@tonic-gate 				/* 'for' loop 2d: */
29677c478bd9Sstevel@tonic-gate 				for (ap6 = (mib2_ipv6AddrEntry_t *)item->valp;
29687c478bd9Sstevel@tonic-gate 				    (char *)ap6 < (char *)item->valp
29697c478bd9Sstevel@tonic-gate 				    + item->length;
29707c478bd9Sstevel@tonic-gate 				    ap6++) {
29717c478bd9Sstevel@tonic-gate 					(void) octetstr(&ap6->ipv6AddrIfIndex,
29727c478bd9Sstevel@tonic-gate 					    'a', logintname,
29737c478bd9Sstevel@tonic-gate 					    sizeof (logintname));
29747c478bd9Sstevel@tonic-gate 					(void) strcpy(ifname, logintname);
29757c478bd9Sstevel@tonic-gate 					(void) strtok(ifname, ":");
29767c478bd9Sstevel@tonic-gate 					if (matchname != NULL &&
29777c478bd9Sstevel@tonic-gate 					    strcmp(matchname, ifname) != 0 &&
29787c478bd9Sstevel@tonic-gate 					    strcmp(matchname, logintname) != 0)
29797c478bd9Sstevel@tonic-gate 						continue; /* 'for' loop 2d */
29807c478bd9Sstevel@tonic-gate 					new_ifindex =
29817c478bd9Sstevel@tonic-gate 					    if_nametoindex(logintname);
2982d62bc4baSyz 
2983d62bc4baSyz 					/*
2984d62bc4baSyz 					 * First lookup the "link" kstats in
2985d62bc4baSyz 					 * case the link is renamed. Then
2986d62bc4baSyz 					 * fallback to the legacy kstats for
2987d62bc4baSyz 					 * those non-GLDv3 links.
2988d62bc4baSyz 					 */
29897c478bd9Sstevel@tonic-gate 					if (new_ifindex != ifindex_v6 &&
2990d62bc4baSyz 					    ((ksp = kstat_lookup(kc, "link", 0,
2991d62bc4baSyz 					    ifname)) != NULL ||
29927c478bd9Sstevel@tonic-gate 					    (ksp = kstat_lookup(kc, NULL, -1,
2993d62bc4baSyz 					    ifname)) != NULL)) {
29947c478bd9Sstevel@tonic-gate 						(void) safe_kstat_read(kc, ksp,
29957c478bd9Sstevel@tonic-gate 						    NULL);
29967c478bd9Sstevel@tonic-gate 						stat.ipackets =
29977c478bd9Sstevel@tonic-gate 						    kstat_named_value(ksp,
29987c478bd9Sstevel@tonic-gate 						    "ipackets");
29997c478bd9Sstevel@tonic-gate 						stat.ierrors =
30007c478bd9Sstevel@tonic-gate 						    kstat_named_value(ksp,
30017c478bd9Sstevel@tonic-gate 						    "ierrors");
30027c478bd9Sstevel@tonic-gate 						stat.opackets =
30037c478bd9Sstevel@tonic-gate 						    kstat_named_value(ksp,
30047c478bd9Sstevel@tonic-gate 						    "opackets");
30057c478bd9Sstevel@tonic-gate 						stat.oerrors =
30067c478bd9Sstevel@tonic-gate 						    kstat_named_value(ksp,
30077c478bd9Sstevel@tonic-gate 						    "oerrors");
30087c478bd9Sstevel@tonic-gate 						stat.collisions =
30097c478bd9Sstevel@tonic-gate 						    kstat_named_value(ksp,
30107c478bd9Sstevel@tonic-gate 						    "collisions");
30117c478bd9Sstevel@tonic-gate 						if (first) {
3012ba753d4aSkeerthi 							if (!first_header)
3013ba753d4aSkeerthi 							(void) putchar('\n');
3014ba753d4aSkeerthi 							first_header = B_FALSE;
30157c478bd9Sstevel@tonic-gate 							(void) printf(
30167c478bd9Sstevel@tonic-gate 							    "%-5.5s %-5.5s%"
30177c478bd9Sstevel@tonic-gate 							    "-27.27s %-27.27s "
30187c478bd9Sstevel@tonic-gate 							    "%-6.6s %-5.5s "
30197c478bd9Sstevel@tonic-gate 							    "%-6.6s %-5.5s "
30207c478bd9Sstevel@tonic-gate 							    "%-6.6s\n",
30217c478bd9Sstevel@tonic-gate 							    "Name", "Mtu",
30227c478bd9Sstevel@tonic-gate 							    "Net/Dest",
30237c478bd9Sstevel@tonic-gate 							    "Address", "Ipkts",
30247c478bd9Sstevel@tonic-gate 							    "Ierrs", "Opkts",
30257c478bd9Sstevel@tonic-gate 							    "Oerrs", "Collis");
30267c478bd9Sstevel@tonic-gate 							first = B_FALSE;
30277c478bd9Sstevel@tonic-gate 						}
30287c478bd9Sstevel@tonic-gate 						if_report_ip6(ap6, ifname,
30297c478bd9Sstevel@tonic-gate 						    logintname, &stat, B_TRUE);
30307c478bd9Sstevel@tonic-gate 						ifindex_v6 = new_ifindex;
30317c478bd9Sstevel@tonic-gate 					} else {
30327c478bd9Sstevel@tonic-gate 						if_report_ip6(ap6, ifname,
30337c478bd9Sstevel@tonic-gate 						    logintname, &stat, B_FALSE);
30347c478bd9Sstevel@tonic-gate 					}
30357c478bd9Sstevel@tonic-gate 				} /* 'for' loop 2d ends */
30367c478bd9Sstevel@tonic-gate 			} else if (!alreadydone) {
30377c478bd9Sstevel@tonic-gate 				char    ifname[LIFNAMSIZ + 1];
30387c478bd9Sstevel@tonic-gate 				char    buf[IFNAMSIZ + 1];
30397c478bd9Sstevel@tonic-gate 				mib2_ipv6AddrEntry_t *ap6;
30407c478bd9Sstevel@tonic-gate 				struct ifstat   t;
3041aecc8c24Sja 				struct iflist	*tlp = NULL;
30427c478bd9Sstevel@tonic-gate 				struct iflist	**nextnew = &newlist6;
30437c478bd9Sstevel@tonic-gate 				struct iflist	*walkold;
30447c478bd9Sstevel@tonic-gate 				struct iflist	*cleanlist;
3045aecc8c24Sja 				boolean_t	found_if = B_FALSE;
30467c478bd9Sstevel@tonic-gate 
30477c478bd9Sstevel@tonic-gate 				alreadydone = B_TRUE; /* ignore other case */
3048aecc8c24Sja 
30497c478bd9Sstevel@tonic-gate 				/*
3050aecc8c24Sja 				 * Check if there is anything to do.
3051aecc8c24Sja 				 */
3052aecc8c24Sja 				if (item->length <
3053aecc8c24Sja 				    sizeof (mib2_ipv6AddrEntry_t)) {
3054aecc8c24Sja 					fail(0, "No compatible interfaces");
3055aecc8c24Sja 				}
3056aecc8c24Sja 
3057aecc8c24Sja 				/*
3058aecc8c24Sja 				 * 'for' loop 2e: find the "right" entry:
3059aecc8c24Sja 				 * If an interface name to match has been
3060aecc8c24Sja 				 * supplied then try and find it, otherwise
3061aecc8c24Sja 				 * match the first non-loopback interface found.
3062aecc8c24Sja 				 * Use lo0 if all else fails.
30637c478bd9Sstevel@tonic-gate 				 */
30647c478bd9Sstevel@tonic-gate 				for (ap6 = (mib2_ipv6AddrEntry_t *)item->valp;
30657c478bd9Sstevel@tonic-gate 				    (char *)ap6 < (char *)item->valp
30667c478bd9Sstevel@tonic-gate 				    + item->length;
30677c478bd9Sstevel@tonic-gate 				    ap6++) {
30687c478bd9Sstevel@tonic-gate 					(void) octetstr(&ap6->ipv6AddrIfIndex,
30697c478bd9Sstevel@tonic-gate 					    'a', ifname, sizeof (ifname));
30707c478bd9Sstevel@tonic-gate 					(void) strtok(ifname, ":");
30717c478bd9Sstevel@tonic-gate 
30727c478bd9Sstevel@tonic-gate 					if (matchname) {
3073aecc8c24Sja 						if (strcmp(matchname,
3074aecc8c24Sja 						    ifname) == 0) {
30757c478bd9Sstevel@tonic-gate 							/* 'for' loop 2e */
3076aecc8c24Sja 							found_if = B_TRUE;
30777c478bd9Sstevel@tonic-gate 							break;
3078aecc8c24Sja 						}
3079aecc8c24Sja 					} else if (strcmp(ifname, "lo0") != 0)
30807c478bd9Sstevel@tonic-gate 						break; /* 'for' loop 2e */
30817c478bd9Sstevel@tonic-gate 				} /* 'for' loop 2e ends */
30827c478bd9Sstevel@tonic-gate 
3083aecc8c24Sja 				if (matchname == NULL) {
3084aecc8c24Sja 					matchname = ifname;
3085aecc8c24Sja 				} else {
3086aecc8c24Sja 					if (!found_if)
3087aecc8c24Sja 						fail(0, "-I: %s no such "
3088aecc8c24Sja 						    "interface.", matchname);
3089aecc8c24Sja 				}
3090aecc8c24Sja 
30917c478bd9Sstevel@tonic-gate 				if (Iflag_only == 0 || !reentry) {
30927c478bd9Sstevel@tonic-gate 					(void) printf(
30937c478bd9Sstevel@tonic-gate 					    "    input   %-6.6s"
30947c478bd9Sstevel@tonic-gate 					    "    output	",
30957c478bd9Sstevel@tonic-gate 					    matchname);
30967c478bd9Sstevel@tonic-gate 					(void) printf("   input  (Total)"
30977c478bd9Sstevel@tonic-gate 					    "    output\n");
30987c478bd9Sstevel@tonic-gate 					(void) printf("%-7.7s %-5.5s %-7.7s "
30997c478bd9Sstevel@tonic-gate 					    "%-5.5s %-6.6s ",
31007c478bd9Sstevel@tonic-gate 					    "packets", "errs", "packets",
31017c478bd9Sstevel@tonic-gate 					    "errs", "colls");
31027c478bd9Sstevel@tonic-gate 					(void) printf("%-7.7s %-5.5s %-7.7s "
31037c478bd9Sstevel@tonic-gate 					    "%-5.5s %-6.6s\n",
31047c478bd9Sstevel@tonic-gate 					    "packets", "errs", "packets",
31057c478bd9Sstevel@tonic-gate 					    "errs", "colls");
31067c478bd9Sstevel@tonic-gate 				}
31077c478bd9Sstevel@tonic-gate 
31087c478bd9Sstevel@tonic-gate 				sum6 = zerostat;
31097c478bd9Sstevel@tonic-gate 
31107c478bd9Sstevel@tonic-gate 				/* 'for' loop 2f: */
31117c478bd9Sstevel@tonic-gate 				for (ap6 = (mib2_ipv6AddrEntry_t *)item->valp;
31127c478bd9Sstevel@tonic-gate 				    (char *)ap6 < (char *)item->valp
31137c478bd9Sstevel@tonic-gate 				    + item->length;
31147c478bd9Sstevel@tonic-gate 				    ap6++) {
31157c478bd9Sstevel@tonic-gate 					(void) octetstr(&ap6->ipv6AddrIfIndex,
31167c478bd9Sstevel@tonic-gate 					    'a', buf, sizeof (buf));
31177c478bd9Sstevel@tonic-gate 					(void) strtok(buf, ":");
3118aecc8c24Sja 
3119aecc8c24Sja 					/*
3120aecc8c24Sja 					 * We have reduced the IP interface
3121aecc8c24Sja 					 * name, which could have been a
3122aecc8c24Sja 					 * logical, down to a name suitable
3123aecc8c24Sja 					 * for use with kstats.
3124aecc8c24Sja 					 * We treat this name as unique and
3125aecc8c24Sja 					 * only collate statistics for it once
3126aecc8c24Sja 					 * per pass. This is to avoid falsely
3127aecc8c24Sja 					 * amplifying these statistics by the
3128aecc8c24Sja 					 * the number of logical instances.
3129aecc8c24Sja 					 */
3130aecc8c24Sja 
3131aecc8c24Sja 					if ((tlp != NULL) &&
3132aecc8c24Sja 					    ((strcmp(buf, tlp->ifname) == 0))) {
3133aecc8c24Sja 						continue;
3134aecc8c24Sja 					}
3135aecc8c24Sja 
3136d62bc4baSyz 					/*
3137d62bc4baSyz 					 * First lookup the "link" kstats in
3138d62bc4baSyz 					 * case the link is renamed. Then
3139d62bc4baSyz 					 * fallback to the legacy kstats for
3140d62bc4baSyz 					 * those non-GLDv3 links.
3141d62bc4baSyz 					 */
3142d62bc4baSyz 					if (((ksp = kstat_lookup(kc, "link",
3143d62bc4baSyz 					    0, buf)) != NULL ||
3144d62bc4baSyz 					    (ksp = kstat_lookup(kc, NULL, -1,
3145d62bc4baSyz 					    buf)) != NULL) && (ksp->ks_type ==
3146d62bc4baSyz 					    KSTAT_TYPE_NAMED)) {
31477c478bd9Sstevel@tonic-gate 						(void) safe_kstat_read(kc,
31487c478bd9Sstevel@tonic-gate 						    ksp, NULL);
3149d62bc4baSyz 					}
31507c478bd9Sstevel@tonic-gate 
31517c478bd9Sstevel@tonic-gate 					t.ipackets = kstat_named_value(ksp,
31527c478bd9Sstevel@tonic-gate 					    "ipackets");
31537c478bd9Sstevel@tonic-gate 					t.ierrors = kstat_named_value(ksp,
31547c478bd9Sstevel@tonic-gate 					    "ierrors");
31557c478bd9Sstevel@tonic-gate 					t.opackets = kstat_named_value(ksp,
31567c478bd9Sstevel@tonic-gate 					    "opackets");
31577c478bd9Sstevel@tonic-gate 					t.oerrors = kstat_named_value(ksp,
31587c478bd9Sstevel@tonic-gate 					    "oerrors");
31597c478bd9Sstevel@tonic-gate 					t.collisions = kstat_named_value(ksp,
31607c478bd9Sstevel@tonic-gate 					    "collisions");
31617c478bd9Sstevel@tonic-gate 
31627c478bd9Sstevel@tonic-gate 					if (strcmp(buf, matchname) == 0)
31637c478bd9Sstevel@tonic-gate 						new6 = t;
31647c478bd9Sstevel@tonic-gate 
31657c478bd9Sstevel@tonic-gate 					/* Build the interface list */
31667c478bd9Sstevel@tonic-gate 
31677c478bd9Sstevel@tonic-gate 					tlp = malloc(sizeof (struct iflist));
31687c478bd9Sstevel@tonic-gate 					(void) strlcpy(tlp->ifname, buf,
31697c478bd9Sstevel@tonic-gate 					    sizeof (tlp->ifname));
31707c478bd9Sstevel@tonic-gate 					tlp->tot = t;
31717c478bd9Sstevel@tonic-gate 					*nextnew = tlp;
31727c478bd9Sstevel@tonic-gate 					nextnew = &tlp->next_if;
31737c478bd9Sstevel@tonic-gate 
31747c478bd9Sstevel@tonic-gate 					/*
31757c478bd9Sstevel@tonic-gate 					 * First time through.
31767c478bd9Sstevel@tonic-gate 					 * Just add up the interface stats.
31777c478bd9Sstevel@tonic-gate 					 */
31787c478bd9Sstevel@tonic-gate 
31797c478bd9Sstevel@tonic-gate 					if (oldlist6 == NULL) {
31807c478bd9Sstevel@tonic-gate 						if_stat_total(&zerostat,
31817c478bd9Sstevel@tonic-gate 						    &t, &sum6);
31827c478bd9Sstevel@tonic-gate 						continue;
31837c478bd9Sstevel@tonic-gate 					}
31847c478bd9Sstevel@tonic-gate 
31857c478bd9Sstevel@tonic-gate 					/*
31867c478bd9Sstevel@tonic-gate 					 * Walk old list for the interface.
31877c478bd9Sstevel@tonic-gate 					 *
31887c478bd9Sstevel@tonic-gate 					 * If found, add difference to total.
31897c478bd9Sstevel@tonic-gate 					 *
31907c478bd9Sstevel@tonic-gate 					 * If not, an interface has been plumbed
31917c478bd9Sstevel@tonic-gate 					 * up.  In this case, we will simply
31927c478bd9Sstevel@tonic-gate 					 * ignore the new interface until the
31937c478bd9Sstevel@tonic-gate 					 * next interval; as there's no easy way
31947c478bd9Sstevel@tonic-gate 					 * to acquire statistics between time
31957c478bd9Sstevel@tonic-gate 					 * of the plumb and the next interval
31967c478bd9Sstevel@tonic-gate 					 * boundary.  This results in inaccurate
31977c478bd9Sstevel@tonic-gate 					 * total values for current interval.
31987c478bd9Sstevel@tonic-gate 					 *
31997c478bd9Sstevel@tonic-gate 					 * Note the case when an interface is
32007c478bd9Sstevel@tonic-gate 					 * unplumbed; as similar problems exist.
32017c478bd9Sstevel@tonic-gate 					 * The unplumbed interface is not in the
32027c478bd9Sstevel@tonic-gate 					 * current list, and there's no easy way
32037c478bd9Sstevel@tonic-gate 					 * to account for the statistics between
32047c478bd9Sstevel@tonic-gate 					 * the previous interval and time of the
32057c478bd9Sstevel@tonic-gate 					 * unplumb.  Therefore, we (in a sense)
32067c478bd9Sstevel@tonic-gate 					 * ignore the removed interface by only
32077c478bd9Sstevel@tonic-gate 					 * involving "current" interfaces when
32087c478bd9Sstevel@tonic-gate 					 * computing the total statistics.
32097c478bd9Sstevel@tonic-gate 					 * Unfortunately, this also results in
32107c478bd9Sstevel@tonic-gate 					 * inaccurate values for interval total.
32117c478bd9Sstevel@tonic-gate 					 */
32127c478bd9Sstevel@tonic-gate 
32137c478bd9Sstevel@tonic-gate 					for (walkold = oldlist6;
32147c478bd9Sstevel@tonic-gate 					    walkold != NULL;
32157c478bd9Sstevel@tonic-gate 					    walkold = walkold->next_if) {
32167c478bd9Sstevel@tonic-gate 						if (strcmp(walkold->ifname,
32177c478bd9Sstevel@tonic-gate 						    buf) == 0) {
32187c478bd9Sstevel@tonic-gate 							if_stat_total(
32197c478bd9Sstevel@tonic-gate 							    &walkold->tot,
32207c478bd9Sstevel@tonic-gate 							    &t, &sum6);
32217c478bd9Sstevel@tonic-gate 							break;
32227c478bd9Sstevel@tonic-gate 						}
32237c478bd9Sstevel@tonic-gate 					}
32247c478bd9Sstevel@tonic-gate 
32257c478bd9Sstevel@tonic-gate 				} /* 'for' loop 2f ends */
32267c478bd9Sstevel@tonic-gate 
32277c478bd9Sstevel@tonic-gate 				*nextnew = NULL;
32287c478bd9Sstevel@tonic-gate 
32297c478bd9Sstevel@tonic-gate 				(void) printf("%-7llu %-5llu %-7llu "
32307c478bd9Sstevel@tonic-gate 				    "%-5llu %-6llu ",
32317c478bd9Sstevel@tonic-gate 				    new6.ipackets - old6.ipackets,
32327c478bd9Sstevel@tonic-gate 				    new6.ierrors - old6.ierrors,
32337c478bd9Sstevel@tonic-gate 				    new6.opackets - old6.opackets,
32347c478bd9Sstevel@tonic-gate 				    new6.oerrors - old6.oerrors,
32357c478bd9Sstevel@tonic-gate 				    new6.collisions - old6.collisions);
32367c478bd9Sstevel@tonic-gate 
32377c478bd9Sstevel@tonic-gate 				(void) printf("%-7llu %-5llu %-7llu "
32387c478bd9Sstevel@tonic-gate 				    "%-5llu %-6llu\n", sum6.ipackets,
32397c478bd9Sstevel@tonic-gate 				    sum6.ierrors, sum6.opackets,
32407c478bd9Sstevel@tonic-gate 				    sum6.oerrors, sum6.collisions);
32417c478bd9Sstevel@tonic-gate 
32427c478bd9Sstevel@tonic-gate 				/*
32437c478bd9Sstevel@tonic-gate 				 * Tidy things up once finished.
32447c478bd9Sstevel@tonic-gate 				 */
32457c478bd9Sstevel@tonic-gate 
32467c478bd9Sstevel@tonic-gate 				old6 = new6;
32477c478bd9Sstevel@tonic-gate 				cleanlist = oldlist6;
32487c478bd9Sstevel@tonic-gate 				oldlist6 = newlist6;
32497c478bd9Sstevel@tonic-gate 				while (cleanlist != NULL) {
32507c478bd9Sstevel@tonic-gate 					tlp = cleanlist->next_if;
32517c478bd9Sstevel@tonic-gate 					free(cleanlist);
32527c478bd9Sstevel@tonic-gate 					cleanlist = tlp;
32537c478bd9Sstevel@tonic-gate 				}
32547c478bd9Sstevel@tonic-gate 			}
32557c478bd9Sstevel@tonic-gate 			break;
32567c478bd9Sstevel@tonic-gate 		}
32577c478bd9Sstevel@tonic-gate 		}
32587c478bd9Sstevel@tonic-gate 		(void) fflush(stdout);
32597c478bd9Sstevel@tonic-gate 	} /* 'for' loop 1 ends */
3260ba753d4aSkeerthi 	if ((Iflag_only == 0) && (!once_only))
3261ba753d4aSkeerthi 		(void) putchar('\n');
32627c478bd9Sstevel@tonic-gate 	reentry = B_TRUE;
32637c478bd9Sstevel@tonic-gate }
32647c478bd9Sstevel@tonic-gate 
32657c478bd9Sstevel@tonic-gate static void
32667c478bd9Sstevel@tonic-gate if_report_ip4(mib2_ipAddrEntry_t *ap,
32677c478bd9Sstevel@tonic-gate 	char ifname[], char logintname[], struct ifstat *statptr,
32687c478bd9Sstevel@tonic-gate 	boolean_t ksp_not_null) {
32697c478bd9Sstevel@tonic-gate 
32707c478bd9Sstevel@tonic-gate 	char abuf[MAXHOSTNAMELEN + 1];
32717c478bd9Sstevel@tonic-gate 	char dstbuf[MAXHOSTNAMELEN + 1];
32727c478bd9Sstevel@tonic-gate 
32737c478bd9Sstevel@tonic-gate 	if (ksp_not_null) {
32749edb2c23SAjaykumar Venkatesulu 		(void) printf("%-5s %-4u ",
32757c478bd9Sstevel@tonic-gate 		    ifname, ap->ipAdEntInfo.ae_mtu);
32767c478bd9Sstevel@tonic-gate 		if (ap->ipAdEntInfo.ae_flags & IFF_POINTOPOINT)
32777c478bd9Sstevel@tonic-gate 			(void) pr_addr(ap->ipAdEntInfo.ae_pp_dst_addr,
32787c478bd9Sstevel@tonic-gate 			    abuf, sizeof (abuf));
32797c478bd9Sstevel@tonic-gate 		else
32807c478bd9Sstevel@tonic-gate 			(void) pr_netaddr(ap->ipAdEntAddr,
32817c478bd9Sstevel@tonic-gate 			    ap->ipAdEntNetMask, abuf, sizeof (abuf));
32827c478bd9Sstevel@tonic-gate 		(void) printf("%-13s %-14s %-6llu %-5llu %-6llu %-5llu "
32837c478bd9Sstevel@tonic-gate 		    "%-6llu %-6llu\n",
32847c478bd9Sstevel@tonic-gate 		    abuf, pr_addr(ap->ipAdEntAddr, dstbuf, sizeof (dstbuf)),
32857c478bd9Sstevel@tonic-gate 		    statptr->ipackets, statptr->ierrors,
32867c478bd9Sstevel@tonic-gate 		    statptr->opackets, statptr->oerrors,
32877c478bd9Sstevel@tonic-gate 		    statptr->collisions, 0LL);
32887c478bd9Sstevel@tonic-gate 	}
32897c478bd9Sstevel@tonic-gate 	/*
32907c478bd9Sstevel@tonic-gate 	 * Print logical interface info if Aflag set (including logical unit 0)
32917c478bd9Sstevel@tonic-gate 	 */
32927c478bd9Sstevel@tonic-gate 	if (Aflag) {
32937c478bd9Sstevel@tonic-gate 		*statptr = zerostat;
32947c478bd9Sstevel@tonic-gate 		statptr->ipackets = ap->ipAdEntInfo.ae_ibcnt;
32957c478bd9Sstevel@tonic-gate 		statptr->opackets = ap->ipAdEntInfo.ae_obcnt;
32967c478bd9Sstevel@tonic-gate 
32979edb2c23SAjaykumar Venkatesulu 		(void) printf("%-5s %-4u ", logintname, ap->ipAdEntInfo.ae_mtu);
32987c478bd9Sstevel@tonic-gate 		if (ap->ipAdEntInfo.ae_flags & IFF_POINTOPOINT)
32997c478bd9Sstevel@tonic-gate 			(void) pr_addr(ap->ipAdEntInfo.ae_pp_dst_addr, abuf,
33007c478bd9Sstevel@tonic-gate 			sizeof (abuf));
33017c478bd9Sstevel@tonic-gate 		else
33027c478bd9Sstevel@tonic-gate 			(void) pr_netaddr(ap->ipAdEntAddr, ap->ipAdEntNetMask,
33037c478bd9Sstevel@tonic-gate 			    abuf, sizeof (abuf));
33047c478bd9Sstevel@tonic-gate 
3305bd670b35SErik Nordmark 		(void) printf("%-13s %-14s %-6llu %-5s %-6s "
33067c478bd9Sstevel@tonic-gate 		    "%-5s %-6s %-6llu\n", abuf,
33077c478bd9Sstevel@tonic-gate 		    pr_addr(ap->ipAdEntAddr, dstbuf, sizeof (dstbuf)),
3308bd670b35SErik Nordmark 		    statptr->ipackets, "N/A", "N/A", "N/A", "N/A",
33097c478bd9Sstevel@tonic-gate 		    0LL);
33107c478bd9Sstevel@tonic-gate 	}
33117c478bd9Sstevel@tonic-gate }
33127c478bd9Sstevel@tonic-gate 
33137c478bd9Sstevel@tonic-gate static void
33147c478bd9Sstevel@tonic-gate if_report_ip6(mib2_ipv6AddrEntry_t *ap6,
33157c478bd9Sstevel@tonic-gate 	char ifname[], char logintname[], struct ifstat *statptr,
33167c478bd9Sstevel@tonic-gate 	boolean_t ksp_not_null) {
33177c478bd9Sstevel@tonic-gate 
33187c478bd9Sstevel@tonic-gate 	char abuf[MAXHOSTNAMELEN + 1];
33197c478bd9Sstevel@tonic-gate 	char dstbuf[MAXHOSTNAMELEN + 1];
33207c478bd9Sstevel@tonic-gate 
33217c478bd9Sstevel@tonic-gate 	if (ksp_not_null) {
33229edb2c23SAjaykumar Venkatesulu 		(void) printf("%-5s %-4u ", ifname, ap6->ipv6AddrInfo.ae_mtu);
33237c478bd9Sstevel@tonic-gate 		if (ap6->ipv6AddrInfo.ae_flags &
33247c478bd9Sstevel@tonic-gate 		    IFF_POINTOPOINT) {
33257c478bd9Sstevel@tonic-gate 			(void) pr_addr6(&ap6->ipv6AddrInfo.ae_pp_dst_addr,
33267c478bd9Sstevel@tonic-gate 			    abuf, sizeof (abuf));
33277c478bd9Sstevel@tonic-gate 		} else {
33287c478bd9Sstevel@tonic-gate 			(void) pr_prefix6(&ap6->ipv6AddrAddress,
33297c478bd9Sstevel@tonic-gate 			    ap6->ipv6AddrPfxLength, abuf,
33307c478bd9Sstevel@tonic-gate 			    sizeof (abuf));
33317c478bd9Sstevel@tonic-gate 		}
33327c478bd9Sstevel@tonic-gate 		(void) printf("%-27s %-27s %-6llu %-5llu "
33337c478bd9Sstevel@tonic-gate 		    "%-6llu %-5llu %-6llu\n",
33347c478bd9Sstevel@tonic-gate 		    abuf, pr_addr6(&ap6->ipv6AddrAddress, dstbuf,
33357c478bd9Sstevel@tonic-gate 		    sizeof (dstbuf)),
33367c478bd9Sstevel@tonic-gate 		    statptr->ipackets, statptr->ierrors, statptr->opackets,
33377c478bd9Sstevel@tonic-gate 		    statptr->oerrors, statptr->collisions);
33387c478bd9Sstevel@tonic-gate 	}
33397c478bd9Sstevel@tonic-gate 	/*
33407c478bd9Sstevel@tonic-gate 	 * Print logical interface info if Aflag set (including logical unit 0)
33417c478bd9Sstevel@tonic-gate 	 */
33427c478bd9Sstevel@tonic-gate 	if (Aflag) {
33437c478bd9Sstevel@tonic-gate 		*statptr = zerostat;
33447c478bd9Sstevel@tonic-gate 		statptr->ipackets = ap6->ipv6AddrInfo.ae_ibcnt;
33457c478bd9Sstevel@tonic-gate 		statptr->opackets = ap6->ipv6AddrInfo.ae_obcnt;
33467c478bd9Sstevel@tonic-gate 
33479edb2c23SAjaykumar Venkatesulu 		(void) printf("%-5s %-4u ", logintname,
33487c478bd9Sstevel@tonic-gate 		    ap6->ipv6AddrInfo.ae_mtu);
33497c478bd9Sstevel@tonic-gate 		if (ap6->ipv6AddrInfo.ae_flags & IFF_POINTOPOINT)
33507c478bd9Sstevel@tonic-gate 			(void) pr_addr6(&ap6->ipv6AddrInfo.ae_pp_dst_addr,
33517c478bd9Sstevel@tonic-gate 			    abuf, sizeof (abuf));
33527c478bd9Sstevel@tonic-gate 		else
33537c478bd9Sstevel@tonic-gate 			(void) pr_prefix6(&ap6->ipv6AddrAddress,
33547c478bd9Sstevel@tonic-gate 			    ap6->ipv6AddrPfxLength, abuf, sizeof (abuf));
3355bd670b35SErik Nordmark 		(void) printf("%-27s %-27s %-6llu %-5s %-6s %-5s %-6s\n",
33567c478bd9Sstevel@tonic-gate 		    abuf, pr_addr6(&ap6->ipv6AddrAddress, dstbuf,
33577c478bd9Sstevel@tonic-gate 		    sizeof (dstbuf)),
3358bd670b35SErik Nordmark 		    statptr->ipackets, "N/A", "N/A", "N/A", "N/A");
33597c478bd9Sstevel@tonic-gate 	}
33607c478bd9Sstevel@tonic-gate }
33617c478bd9Sstevel@tonic-gate 
33627c478bd9Sstevel@tonic-gate /* --------------------- DHCP_REPORT  (netstat -D) ------------------------- */
33637c478bd9Sstevel@tonic-gate 
3364d04ccbb3Scarlsonj static boolean_t
3365d04ccbb3Scarlsonj dhcp_do_ipc(dhcp_ipc_type_t type, const char *ifname, boolean_t printed_one)
33667c478bd9Sstevel@tonic-gate {
33677c478bd9Sstevel@tonic-gate 	dhcp_ipc_request_t	*request;
33687c478bd9Sstevel@tonic-gate 	dhcp_ipc_reply_t	*reply;
33697c478bd9Sstevel@tonic-gate 	int			error;
33707c478bd9Sstevel@tonic-gate 
33717c478bd9Sstevel@tonic-gate 	request = dhcp_ipc_alloc_request(type, ifname, NULL, 0, DHCP_TYPE_NONE);
33727c478bd9Sstevel@tonic-gate 	if (request == NULL)
33737c478bd9Sstevel@tonic-gate 		fail(0, "dhcp_do_ipc: out of memory");
33747c478bd9Sstevel@tonic-gate 
33757c478bd9Sstevel@tonic-gate 	error = dhcp_ipc_make_request(request, &reply, DHCP_IPC_WAIT_DEFAULT);
33767c478bd9Sstevel@tonic-gate 	if (error != 0) {
33777c478bd9Sstevel@tonic-gate 		free(request);
33787c478bd9Sstevel@tonic-gate 		fail(0, "dhcp_do_ipc: %s", dhcp_ipc_strerror(error));
33797c478bd9Sstevel@tonic-gate 	}
33807c478bd9Sstevel@tonic-gate 
33817c478bd9Sstevel@tonic-gate 	free(request);
33827c478bd9Sstevel@tonic-gate 	error = reply->return_code;
3383d04ccbb3Scarlsonj 	if (error == DHCP_IPC_E_UNKIF) {
3384d04ccbb3Scarlsonj 		free(reply);
3385d04ccbb3Scarlsonj 		return (printed_one);
3386d04ccbb3Scarlsonj 	}
33877c478bd9Sstevel@tonic-gate 	if (error != 0) {
33887c478bd9Sstevel@tonic-gate 		free(reply);
33897c478bd9Sstevel@tonic-gate 		fail(0, "dhcp_do_ipc: %s", dhcp_ipc_strerror(error));
33907c478bd9Sstevel@tonic-gate 	}
33917c478bd9Sstevel@tonic-gate 
339226fd7700SKrishnendu Sadhukhan - Sun Microsystems 	if (timestamp_fmt != NODATE)
339326fd7700SKrishnendu Sadhukhan - Sun Microsystems 		print_timestamp(timestamp_fmt);
339426fd7700SKrishnendu Sadhukhan - Sun Microsystems 
3395d04ccbb3Scarlsonj 	if (!printed_one)
3396d04ccbb3Scarlsonj 		(void) printf("%s", dhcp_status_hdr_string());
3397d04ccbb3Scarlsonj 
3398d04ccbb3Scarlsonj 	(void) printf("%s", dhcp_status_reply_to_string(reply));
3399d04ccbb3Scarlsonj 	free(reply);
3400d04ccbb3Scarlsonj 	return (B_TRUE);
34017c478bd9Sstevel@tonic-gate }
34027c478bd9Sstevel@tonic-gate 
34037c478bd9Sstevel@tonic-gate /*
3404dc918d99Smeem  * dhcp_walk_interfaces: walk the list of interfaces for a given address
3405dc918d99Smeem  * family (af).  For each, print out the DHCP status using dhcp_do_ipc.
34067c478bd9Sstevel@tonic-gate  */
3407d04ccbb3Scarlsonj static boolean_t
3408dc918d99Smeem dhcp_walk_interfaces(int af, boolean_t printed_one)
34097c478bd9Sstevel@tonic-gate {
3410d04ccbb3Scarlsonj 	struct lifnum	lifn;
3411d04ccbb3Scarlsonj 	struct lifconf	lifc;
34127c478bd9Sstevel@tonic-gate 	int		n_ifs, i, sock_fd;
34137c478bd9Sstevel@tonic-gate 
3414d04ccbb3Scarlsonj 	sock_fd = socket(af, SOCK_DGRAM, 0);
34157c478bd9Sstevel@tonic-gate 	if (sock_fd == -1)
3416d04ccbb3Scarlsonj 		return (printed_one);
34177c478bd9Sstevel@tonic-gate 
3418d04ccbb3Scarlsonj 	/*
3419d04ccbb3Scarlsonj 	 * SIOCGLIFNUM is just an estimate.  If the ioctl fails, we don't care;
3420d04ccbb3Scarlsonj 	 * just drive on and use SIOCGLIFCONF with increasing buffer sizes, as
3421d04ccbb3Scarlsonj 	 * is traditional.
3422d04ccbb3Scarlsonj 	 */
3423d04ccbb3Scarlsonj 	(void) memset(&lifn, 0, sizeof (lifn));
3424d04ccbb3Scarlsonj 	lifn.lifn_family = af;
3425e11c3f44Smeem 	lifn.lifn_flags = LIFC_ALLZONES | LIFC_NOXMIT | LIFC_UNDER_IPMP;
3426d04ccbb3Scarlsonj 	if (ioctl(sock_fd, SIOCGLIFNUM, &lifn) == -1)
3427d04ccbb3Scarlsonj 		n_ifs = LIFN_GUARD_VALUE;
3428d04ccbb3Scarlsonj 	else
3429d04ccbb3Scarlsonj 		n_ifs = lifn.lifn_count + LIFN_GUARD_VALUE;
3430d04ccbb3Scarlsonj 
3431d04ccbb3Scarlsonj 	(void) memset(&lifc, 0, sizeof (lifc));
3432d04ccbb3Scarlsonj 	lifc.lifc_family = af;
3433d04ccbb3Scarlsonj 	lifc.lifc_flags = lifn.lifn_flags;
3434d04ccbb3Scarlsonj 	lifc.lifc_len = n_ifs * sizeof (struct lifreq);
3435d04ccbb3Scarlsonj 	lifc.lifc_buf = malloc(lifc.lifc_len);
3436d04ccbb3Scarlsonj 	if (lifc.lifc_buf != NULL) {
3437d04ccbb3Scarlsonj 
3438d04ccbb3Scarlsonj 		if (ioctl(sock_fd, SIOCGLIFCONF, &lifc) == -1) {
34397c478bd9Sstevel@tonic-gate 			(void) close(sock_fd);
3440d04ccbb3Scarlsonj 			free(lifc.lifc_buf);
34417c478bd9Sstevel@tonic-gate 			return (NULL);
34427c478bd9Sstevel@tonic-gate 		}
34437c478bd9Sstevel@tonic-gate 
3444d04ccbb3Scarlsonj 		n_ifs = lifc.lifc_len / sizeof (struct lifreq);
34457c478bd9Sstevel@tonic-gate 
3446d04ccbb3Scarlsonj 		for (i = 0; i < n_ifs; i++) {
3447d04ccbb3Scarlsonj 			printed_one = dhcp_do_ipc(DHCP_STATUS |
3448d04ccbb3Scarlsonj 			    (af == AF_INET6 ? DHCP_V6 : 0),
3449d04ccbb3Scarlsonj 			    lifc.lifc_req[i].lifr_name, printed_one);
3450d04ccbb3Scarlsonj 		}
34517c478bd9Sstevel@tonic-gate 	}
34527c478bd9Sstevel@tonic-gate 	(void) close(sock_fd);
3453d04ccbb3Scarlsonj 	free(lifc.lifc_buf);
3454d04ccbb3Scarlsonj 	return (printed_one);
34557c478bd9Sstevel@tonic-gate }
34567c478bd9Sstevel@tonic-gate 
34577c478bd9Sstevel@tonic-gate static void
34587c478bd9Sstevel@tonic-gate dhcp_report(char *ifname)
34597c478bd9Sstevel@tonic-gate {
3460d04ccbb3Scarlsonj 	boolean_t printed_one;
34617c478bd9Sstevel@tonic-gate 
3462d04ccbb3Scarlsonj 	if (!family_selected(AF_INET) && !family_selected(AF_INET6))
34637c478bd9Sstevel@tonic-gate 		return;
34647c478bd9Sstevel@tonic-gate 
3465d04ccbb3Scarlsonj 	printed_one = B_FALSE;
3466d04ccbb3Scarlsonj 	if (ifname != NULL) {
3467d04ccbb3Scarlsonj 		if (family_selected(AF_INET)) {
3468d04ccbb3Scarlsonj 			printed_one = dhcp_do_ipc(DHCP_STATUS, ifname,
3469d04ccbb3Scarlsonj 			    printed_one);
3470d04ccbb3Scarlsonj 		}
3471d04ccbb3Scarlsonj 		if (family_selected(AF_INET6)) {
3472d04ccbb3Scarlsonj 			printed_one = dhcp_do_ipc(DHCP_STATUS | DHCP_V6,
3473d04ccbb3Scarlsonj 			    ifname, printed_one);
3474d04ccbb3Scarlsonj 		}
3475d04ccbb3Scarlsonj 		if (!printed_one) {
3476d04ccbb3Scarlsonj 			fail(0, "%s: %s", ifname,
3477d04ccbb3Scarlsonj 			    dhcp_ipc_strerror(DHCP_IPC_E_UNKIF));
3478d04ccbb3Scarlsonj 		}
3479d04ccbb3Scarlsonj 	} else {
3480d04ccbb3Scarlsonj 		if (family_selected(AF_INET)) {
3481dc918d99Smeem 			printed_one = dhcp_walk_interfaces(AF_INET,
3482dc918d99Smeem 			    printed_one);
3483d04ccbb3Scarlsonj 		}
3484dc918d99Smeem 		if (family_selected(AF_INET6))
3485dc918d99Smeem 			(void) dhcp_walk_interfaces(AF_INET6, printed_one);
34867c478bd9Sstevel@tonic-gate 	}
34877c478bd9Sstevel@tonic-gate }
34887c478bd9Sstevel@tonic-gate 
34897c478bd9Sstevel@tonic-gate /* --------------------- GROUP_REPORT (netstat -g) ------------------------- */
34907c478bd9Sstevel@tonic-gate 
34917c478bd9Sstevel@tonic-gate static void
34927c478bd9Sstevel@tonic-gate group_report(mib_item_t *item)
34937c478bd9Sstevel@tonic-gate {
34947c478bd9Sstevel@tonic-gate 	mib_item_t	*v4grp = NULL, *v4src = NULL;
34957c478bd9Sstevel@tonic-gate 	mib_item_t	*v6grp = NULL, *v6src = NULL;
34967c478bd9Sstevel@tonic-gate 	int		jtemp = 0;
34977c478bd9Sstevel@tonic-gate 	char		ifname[LIFNAMSIZ + 1];
34987c478bd9Sstevel@tonic-gate 	char		abuf[MAXHOSTNAMELEN + 1];
34997c478bd9Sstevel@tonic-gate 	ip_member_t	*ipmp;
35007c478bd9Sstevel@tonic-gate 	ip_grpsrc_t	*ips;
35017c478bd9Sstevel@tonic-gate 	ipv6_member_t	*ipmp6;
35027c478bd9Sstevel@tonic-gate 	ipv6_grpsrc_t	*ips6;
35037c478bd9Sstevel@tonic-gate 	boolean_t	first, first_src;
35047c478bd9Sstevel@tonic-gate 
35057c478bd9Sstevel@tonic-gate 	/* 'for' loop 1: */
35067c478bd9Sstevel@tonic-gate 	for (; item; item = item->next_item) {
3507bd670b35SErik Nordmark 		if (Xflag) {
35087c478bd9Sstevel@tonic-gate 			(void) printf("\n--- Entry %d ---\n", ++jtemp);
35097c478bd9Sstevel@tonic-gate 			(void) printf("Group = %d, mib_id = %d, "
35107c478bd9Sstevel@tonic-gate 			    "length = %d, valp = 0x%p\n",
35117c478bd9Sstevel@tonic-gate 			    item->group, item->mib_id, item->length,
35127c478bd9Sstevel@tonic-gate 			    item->valp);
35137c478bd9Sstevel@tonic-gate 		}
35147c478bd9Sstevel@tonic-gate 		if (item->group == MIB2_IP && family_selected(AF_INET)) {
35157c478bd9Sstevel@tonic-gate 			switch (item->mib_id) {
35167c478bd9Sstevel@tonic-gate 			case EXPER_IP_GROUP_MEMBERSHIP:
35177c478bd9Sstevel@tonic-gate 				v4grp = item;
3518bd670b35SErik Nordmark 				if (Xflag)
35197c478bd9Sstevel@tonic-gate 					(void) printf("item is v4grp info\n");
35207c478bd9Sstevel@tonic-gate 				break;
35217c478bd9Sstevel@tonic-gate 			case EXPER_IP_GROUP_SOURCES:
35227c478bd9Sstevel@tonic-gate 				v4src = item;
3523bd670b35SErik Nordmark 				if (Xflag)
35247c478bd9Sstevel@tonic-gate 					(void) printf("item is v4src info\n");
35257c478bd9Sstevel@tonic-gate 				break;
35267c478bd9Sstevel@tonic-gate 			default:
35277c478bd9Sstevel@tonic-gate 				continue;
35287c478bd9Sstevel@tonic-gate 			}
35297c478bd9Sstevel@tonic-gate 			continue;
35307c478bd9Sstevel@tonic-gate 		}
35317c478bd9Sstevel@tonic-gate 		if (item->group == MIB2_IP6 && family_selected(AF_INET6)) {
35327c478bd9Sstevel@tonic-gate 			switch (item->mib_id) {
35337c478bd9Sstevel@tonic-gate 			case EXPER_IP6_GROUP_MEMBERSHIP:
35347c478bd9Sstevel@tonic-gate 				v6grp = item;
3535bd670b35SErik Nordmark 				if (Xflag)
35367c478bd9Sstevel@tonic-gate 					(void) printf("item is v6grp info\n");
35377c478bd9Sstevel@tonic-gate 				break;
35387c478bd9Sstevel@tonic-gate 			case EXPER_IP6_GROUP_SOURCES:
35397c478bd9Sstevel@tonic-gate 				v6src = item;
3540bd670b35SErik Nordmark 				if (Xflag)
35417c478bd9Sstevel@tonic-gate 					(void) printf("item is v6src info\n");
35427c478bd9Sstevel@tonic-gate 				break;
35437c478bd9Sstevel@tonic-gate 			default:
35447c478bd9Sstevel@tonic-gate 				continue;
35457c478bd9Sstevel@tonic-gate 			}
35467c478bd9Sstevel@tonic-gate 		}
35477c478bd9Sstevel@tonic-gate 	}
35487c478bd9Sstevel@tonic-gate 
35497c478bd9Sstevel@tonic-gate 	if (family_selected(AF_INET) && v4grp != NULL) {
3550bd670b35SErik Nordmark 		if (Xflag)
35517c478bd9Sstevel@tonic-gate 			(void) printf("%u records for ipGroupMember:\n",
35527c478bd9Sstevel@tonic-gate 			    v4grp->length / sizeof (ip_member_t));
35537c478bd9Sstevel@tonic-gate 
35547c478bd9Sstevel@tonic-gate 		first = B_TRUE;
35557c478bd9Sstevel@tonic-gate 		for (ipmp = (ip_member_t *)v4grp->valp;
35567c478bd9Sstevel@tonic-gate 		    (char *)ipmp < (char *)v4grp->valp + v4grp->length;
35577c478bd9Sstevel@tonic-gate 		    /* LINTED: (note 1) */
35587c478bd9Sstevel@tonic-gate 		    ipmp = (ip_member_t *)((char *)ipmp + ipMemberEntrySize)) {
35597c478bd9Sstevel@tonic-gate 			if (first) {
35607c478bd9Sstevel@tonic-gate 				(void) puts(v4compat ?
35617c478bd9Sstevel@tonic-gate 				    "Group Memberships" :
35627c478bd9Sstevel@tonic-gate 				    "Group Memberships: IPv4");
35637c478bd9Sstevel@tonic-gate 				(void) puts("Interface "
35647c478bd9Sstevel@tonic-gate 				    "Group                RefCnt");
35657c478bd9Sstevel@tonic-gate 				(void) puts("--------- "
35667c478bd9Sstevel@tonic-gate 				    "-------------------- ------");
35677c478bd9Sstevel@tonic-gate 				first = B_FALSE;
35687c478bd9Sstevel@tonic-gate 			}
35697c478bd9Sstevel@tonic-gate 
35707c478bd9Sstevel@tonic-gate 			(void) printf("%-9s %-20s %6u\n",
35717c478bd9Sstevel@tonic-gate 			    octetstr(&ipmp->ipGroupMemberIfIndex, 'a',
35727c478bd9Sstevel@tonic-gate 			    ifname, sizeof (ifname)),
35737c478bd9Sstevel@tonic-gate 			    pr_addr(ipmp->ipGroupMemberAddress,
35747c478bd9Sstevel@tonic-gate 			    abuf, sizeof (abuf)),
35757c478bd9Sstevel@tonic-gate 			    ipmp->ipGroupMemberRefCnt);
35767c478bd9Sstevel@tonic-gate 
35777c478bd9Sstevel@tonic-gate 
35787c478bd9Sstevel@tonic-gate 			if (!Vflag || v4src == NULL)
35797c478bd9Sstevel@tonic-gate 				continue;
35807c478bd9Sstevel@tonic-gate 
3581bd670b35SErik Nordmark 			if (Xflag)
35827c478bd9Sstevel@tonic-gate 				(void) printf("scanning %u ipGroupSource "
35837c478bd9Sstevel@tonic-gate 				    "records...\n",
35847c478bd9Sstevel@tonic-gate 				    v4src->length/sizeof (ip_grpsrc_t));
35857c478bd9Sstevel@tonic-gate 
35867c478bd9Sstevel@tonic-gate 			first_src = B_TRUE;
35877c478bd9Sstevel@tonic-gate 			for (ips = (ip_grpsrc_t *)v4src->valp;
35887c478bd9Sstevel@tonic-gate 			    (char *)ips < (char *)v4src->valp + v4src->length;
35897c478bd9Sstevel@tonic-gate 			    /* LINTED: (note 1) */
35907c478bd9Sstevel@tonic-gate 			    ips = (ip_grpsrc_t *)((char *)ips +
35917c478bd9Sstevel@tonic-gate 			    ipGroupSourceEntrySize)) {
35927c478bd9Sstevel@tonic-gate 				/*
35937c478bd9Sstevel@tonic-gate 				 * We assume that all source addrs for a given
35947c478bd9Sstevel@tonic-gate 				 * interface/group pair are contiguous, so on
35957c478bd9Sstevel@tonic-gate 				 * the first non-match after we've found at
35967c478bd9Sstevel@tonic-gate 				 * least one, we bail.
35977c478bd9Sstevel@tonic-gate 				 */
35987c478bd9Sstevel@tonic-gate 				if ((ipmp->ipGroupMemberAddress !=
35997c478bd9Sstevel@tonic-gate 				    ips->ipGroupSourceGroup) ||
36007c478bd9Sstevel@tonic-gate 				    (!octetstrmatch(&ipmp->ipGroupMemberIfIndex,
36017c478bd9Sstevel@tonic-gate 				    &ips->ipGroupSourceIfIndex))) {
36027c478bd9Sstevel@tonic-gate 					if (first_src)
36037c478bd9Sstevel@tonic-gate 						continue;
36047c478bd9Sstevel@tonic-gate 					else
36057c478bd9Sstevel@tonic-gate 						break;
36067c478bd9Sstevel@tonic-gate 				}
36077c478bd9Sstevel@tonic-gate 				if (first_src) {
36087c478bd9Sstevel@tonic-gate 					(void) printf("\t%s:    %s\n",
36097c478bd9Sstevel@tonic-gate 					    fmodestr(
36107c478bd9Sstevel@tonic-gate 					    ipmp->ipGroupMemberFilterMode),
36117c478bd9Sstevel@tonic-gate 					    pr_addr(ips->ipGroupSourceAddress,
36127c478bd9Sstevel@tonic-gate 					    abuf, sizeof (abuf)));
36137c478bd9Sstevel@tonic-gate 					first_src = B_FALSE;
36147c478bd9Sstevel@tonic-gate 					continue;
36157c478bd9Sstevel@tonic-gate 				}
36167c478bd9Sstevel@tonic-gate 
36177c478bd9Sstevel@tonic-gate 				(void) printf("\t            %s\n",
36187c478bd9Sstevel@tonic-gate 				    pr_addr(ips->ipGroupSourceAddress, abuf,
36197c478bd9Sstevel@tonic-gate 				    sizeof (abuf)));
36207c478bd9Sstevel@tonic-gate 			}
36217c478bd9Sstevel@tonic-gate 		}
36227c478bd9Sstevel@tonic-gate 		(void) putchar('\n');
36237c478bd9Sstevel@tonic-gate 	}
36247c478bd9Sstevel@tonic-gate 
36257c478bd9Sstevel@tonic-gate 	if (family_selected(AF_INET6) && v6grp != NULL) {
3626bd670b35SErik Nordmark 		if (Xflag)
36277c478bd9Sstevel@tonic-gate 			(void) printf("%u records for ipv6GroupMember:\n",
36287c478bd9Sstevel@tonic-gate 			    v6grp->length / sizeof (ipv6_member_t));
36297c478bd9Sstevel@tonic-gate 
36307c478bd9Sstevel@tonic-gate 		first = B_TRUE;
36317c478bd9Sstevel@tonic-gate 		for (ipmp6 = (ipv6_member_t *)v6grp->valp;
36327c478bd9Sstevel@tonic-gate 		    (char *)ipmp6 < (char *)v6grp->valp + v6grp->length;
36337c478bd9Sstevel@tonic-gate 		    /* LINTED: (note 1) */
36347c478bd9Sstevel@tonic-gate 		    ipmp6 = (ipv6_member_t *)((char *)ipmp6 +
3635e11c3f44Smeem 		    ipv6MemberEntrySize)) {
36367c478bd9Sstevel@tonic-gate 			if (first) {
36377c478bd9Sstevel@tonic-gate 				(void) puts("Group Memberships: "
36387c478bd9Sstevel@tonic-gate 				    "IPv6");
36397c478bd9Sstevel@tonic-gate 				(void) puts(" If       "
36407c478bd9Sstevel@tonic-gate 				    "Group                   RefCnt");
36417c478bd9Sstevel@tonic-gate 				(void) puts("----- "
36427c478bd9Sstevel@tonic-gate 				    "--------------------------- ------");
36437c478bd9Sstevel@tonic-gate 				first = B_FALSE;
36447c478bd9Sstevel@tonic-gate 			}
36457c478bd9Sstevel@tonic-gate 
36467c478bd9Sstevel@tonic-gate 			(void) printf("%-5s %-27s %5u\n",
3647e11c3f44Smeem 			    ifindex2str(ipmp6->ipv6GroupMemberIfIndex, ifname),
36487c478bd9Sstevel@tonic-gate 			    pr_addr6(&ipmp6->ipv6GroupMemberAddress,
36497c478bd9Sstevel@tonic-gate 			    abuf, sizeof (abuf)),
36507c478bd9Sstevel@tonic-gate 			    ipmp6->ipv6GroupMemberRefCnt);
36517c478bd9Sstevel@tonic-gate 
36527c478bd9Sstevel@tonic-gate 			if (!Vflag || v6src == NULL)
36537c478bd9Sstevel@tonic-gate 				continue;
36547c478bd9Sstevel@tonic-gate 
3655bd670b35SErik Nordmark 			if (Xflag)
36567c478bd9Sstevel@tonic-gate 				(void) printf("scanning %u ipv6GroupSource "
36577c478bd9Sstevel@tonic-gate 				    "records...\n",
36587c478bd9Sstevel@tonic-gate 				    v6src->length/sizeof (ipv6_grpsrc_t));
36597c478bd9Sstevel@tonic-gate 
36607c478bd9Sstevel@tonic-gate 			first_src = B_TRUE;
36617c478bd9Sstevel@tonic-gate 			for (ips6 = (ipv6_grpsrc_t *)v6src->valp;
36627c478bd9Sstevel@tonic-gate 			    (char *)ips6 < (char *)v6src->valp + v6src->length;
36637c478bd9Sstevel@tonic-gate 			    /* LINTED: (note 1) */
36647c478bd9Sstevel@tonic-gate 			    ips6 = (ipv6_grpsrc_t *)((char *)ips6 +
36657c478bd9Sstevel@tonic-gate 			    ipv6GroupSourceEntrySize)) {
36667c478bd9Sstevel@tonic-gate 				/* same assumption as in the v4 case above */
36677c478bd9Sstevel@tonic-gate 				if ((ipmp6->ipv6GroupMemberIfIndex !=
36687c478bd9Sstevel@tonic-gate 				    ips6->ipv6GroupSourceIfIndex) ||
36697c478bd9Sstevel@tonic-gate 				    (!IN6_ARE_ADDR_EQUAL(
36707c478bd9Sstevel@tonic-gate 				    &ipmp6->ipv6GroupMemberAddress,
36717c478bd9Sstevel@tonic-gate 				    &ips6->ipv6GroupSourceGroup))) {
36727c478bd9Sstevel@tonic-gate 					if (first_src)
36737c478bd9Sstevel@tonic-gate 						continue;
36747c478bd9Sstevel@tonic-gate 					else
36757c478bd9Sstevel@tonic-gate 						break;
36767c478bd9Sstevel@tonic-gate 				}
36777c478bd9Sstevel@tonic-gate 				if (first_src) {
36787c478bd9Sstevel@tonic-gate 					(void) printf("\t%s:    %s\n",
36797c478bd9Sstevel@tonic-gate 					    fmodestr(
36807c478bd9Sstevel@tonic-gate 					    ipmp6->ipv6GroupMemberFilterMode),
36817c478bd9Sstevel@tonic-gate 					    pr_addr6(
36827c478bd9Sstevel@tonic-gate 					    &ips6->ipv6GroupSourceAddress,
36837c478bd9Sstevel@tonic-gate 					    abuf, sizeof (abuf)));
36847c478bd9Sstevel@tonic-gate 					first_src = B_FALSE;
36857c478bd9Sstevel@tonic-gate 					continue;
36867c478bd9Sstevel@tonic-gate 				}
36877c478bd9Sstevel@tonic-gate 
36887c478bd9Sstevel@tonic-gate 				(void) printf("\t            %s\n",
36897c478bd9Sstevel@tonic-gate 				    pr_addr6(&ips6->ipv6GroupSourceAddress,
36907c478bd9Sstevel@tonic-gate 				    abuf, sizeof (abuf)));
36917c478bd9Sstevel@tonic-gate 			}
36927c478bd9Sstevel@tonic-gate 		}
36937c478bd9Sstevel@tonic-gate 		(void) putchar('\n');
36947c478bd9Sstevel@tonic-gate 	}
36957c478bd9Sstevel@tonic-gate 
36967c478bd9Sstevel@tonic-gate 	(void) putchar('\n');
36977c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
36987c478bd9Sstevel@tonic-gate }
36997c478bd9Sstevel@tonic-gate 
3700bd670b35SErik Nordmark /* --------------------- DCE_REPORT (netstat -d) ------------------------- */
3701bd670b35SErik Nordmark 
3702bd670b35SErik Nordmark #define	FLBUFSIZE	8
3703bd670b35SErik Nordmark 
3704bd670b35SErik Nordmark /* Assumes flbuf is at least 5 characters; callers use FLBUFSIZE */
3705bd670b35SErik Nordmark static char *
3706bd670b35SErik Nordmark dceflags2str(uint32_t flags, char *flbuf)
3707bd670b35SErik Nordmark {
3708bd670b35SErik Nordmark 	char *str = flbuf;
3709bd670b35SErik Nordmark 
3710bd670b35SErik Nordmark 	if (flags & DCEF_DEFAULT)
3711bd670b35SErik Nordmark 		*str++ = 'D';
3712bd670b35SErik Nordmark 	if (flags & DCEF_PMTU)
3713bd670b35SErik Nordmark 		*str++ = 'P';
3714bd670b35SErik Nordmark 	if (flags & DCEF_UINFO)
3715bd670b35SErik Nordmark 		*str++ = 'U';
3716bd670b35SErik Nordmark 	if (flags & DCEF_TOO_SMALL_PMTU)
3717bd670b35SErik Nordmark 		*str++ = 'S';
3718bd670b35SErik Nordmark 	*str++ = '\0';
3719bd670b35SErik Nordmark 	return (flbuf);
3720bd670b35SErik Nordmark }
3721bd670b35SErik Nordmark 
3722bd670b35SErik Nordmark static void
3723bd670b35SErik Nordmark dce_report(mib_item_t *item)
3724bd670b35SErik Nordmark {
3725bd670b35SErik Nordmark 	mib_item_t	*v4dce = NULL;
3726bd670b35SErik Nordmark 	mib_item_t	*v6dce = NULL;
3727bd670b35SErik Nordmark 	int		jtemp = 0;
3728bd670b35SErik Nordmark 	char		ifname[LIFNAMSIZ + 1];
3729bd670b35SErik Nordmark 	char		abuf[MAXHOSTNAMELEN + 1];
3730bd670b35SErik Nordmark 	char		flbuf[FLBUFSIZE];
3731bd670b35SErik Nordmark 	boolean_t	first;
3732bd670b35SErik Nordmark 	dest_cache_entry_t *dce;
3733bd670b35SErik Nordmark 
3734bd670b35SErik Nordmark 	/* 'for' loop 1: */
3735bd670b35SErik Nordmark 	for (; item; item = item->next_item) {
3736bd670b35SErik Nordmark 		if (Xflag) {
3737bd670b35SErik Nordmark 			(void) printf("\n--- Entry %d ---\n", ++jtemp);
3738bd670b35SErik Nordmark 			(void) printf("Group = %d, mib_id = %d, "
3739bd670b35SErik Nordmark 			    "length = %d, valp = 0x%p\n",
3740bd670b35SErik Nordmark 			    item->group, item->mib_id, item->length,
3741bd670b35SErik Nordmark 			    item->valp);
3742bd670b35SErik Nordmark 		}
3743bd670b35SErik Nordmark 		if (item->group == MIB2_IP && family_selected(AF_INET) &&
3744bd670b35SErik Nordmark 		    item->mib_id == EXPER_IP_DCE) {
3745bd670b35SErik Nordmark 			v4dce = item;
3746bd670b35SErik Nordmark 			if (Xflag)
3747bd670b35SErik Nordmark 				(void) printf("item is v4dce info\n");
3748bd670b35SErik Nordmark 		}
3749bd670b35SErik Nordmark 		if (item->group == MIB2_IP6 && family_selected(AF_INET6) &&
3750bd670b35SErik Nordmark 		    item->mib_id == EXPER_IP_DCE) {
3751bd670b35SErik Nordmark 			v6dce = item;
3752bd670b35SErik Nordmark 			if (Xflag)
3753bd670b35SErik Nordmark 				(void) printf("item is v6dce info\n");
3754bd670b35SErik Nordmark 		}
3755bd670b35SErik Nordmark 	}
3756bd670b35SErik Nordmark 
3757bd670b35SErik Nordmark 	if (family_selected(AF_INET) && v4dce != NULL) {
3758bd670b35SErik Nordmark 		if (Xflag)
3759bd670b35SErik Nordmark 			(void) printf("%u records for DestCacheEntry:\n",
3760bd670b35SErik Nordmark 			    v4dce->length / ipDestEntrySize);
3761bd670b35SErik Nordmark 
3762bd670b35SErik Nordmark 		first = B_TRUE;
3763bd670b35SErik Nordmark 		for (dce = (dest_cache_entry_t *)v4dce->valp;
3764bd670b35SErik Nordmark 		    (char *)dce < (char *)v4dce->valp + v4dce->length;
3765bd670b35SErik Nordmark 		    /* LINTED: (note 1) */
3766bd670b35SErik Nordmark 		    dce = (dest_cache_entry_t *)((char *)dce +
3767bd670b35SErik Nordmark 		    ipDestEntrySize)) {
3768bd670b35SErik Nordmark 			if (first) {
3769bd670b35SErik Nordmark 				(void) putchar('\n');
3770bd670b35SErik Nordmark 				(void) puts("Destination Cache Entries: IPv4");
3771bd670b35SErik Nordmark 				(void) puts(
3772bd670b35SErik Nordmark 				    "Address               PMTU   Age  Flags");
3773bd670b35SErik Nordmark 				(void) puts(
3774bd670b35SErik Nordmark 				    "-------------------- ------ ----- -----");
3775bd670b35SErik Nordmark 				first = B_FALSE;
3776bd670b35SErik Nordmark 			}
3777bd670b35SErik Nordmark 
3778bd670b35SErik Nordmark 			(void) printf("%-20s %6u %5u %-5s\n",
3779bd670b35SErik Nordmark 			    pr_addr(dce->DestIpv4Address, abuf, sizeof (abuf)),
3780bd670b35SErik Nordmark 			    dce->DestPmtu, dce->DestAge,
3781bd670b35SErik Nordmark 			    dceflags2str(dce->DestFlags, flbuf));
3782bd670b35SErik Nordmark 		}
3783bd670b35SErik Nordmark 	}
3784bd670b35SErik Nordmark 
3785bd670b35SErik Nordmark 	if (family_selected(AF_INET6) && v6dce != NULL) {
3786bd670b35SErik Nordmark 		if (Xflag)
3787bd670b35SErik Nordmark 			(void) printf("%u records for DestCacheEntry:\n",
3788bd670b35SErik Nordmark 			    v6dce->length / ipDestEntrySize);
3789bd670b35SErik Nordmark 
3790bd670b35SErik Nordmark 		first = B_TRUE;
3791bd670b35SErik Nordmark 		for (dce = (dest_cache_entry_t *)v6dce->valp;
3792bd670b35SErik Nordmark 		    (char *)dce < (char *)v6dce->valp + v6dce->length;
3793bd670b35SErik Nordmark 		    /* LINTED: (note 1) */
3794bd670b35SErik Nordmark 		    dce = (dest_cache_entry_t *)((char *)dce +
3795bd670b35SErik Nordmark 		    ipDestEntrySize)) {
3796bd670b35SErik Nordmark 			if (first) {
3797bd670b35SErik Nordmark 				(void) putchar('\n');
3798bd670b35SErik Nordmark 				(void) puts("Destination Cache Entries: IPv6");
3799bd670b35SErik Nordmark 				(void) puts(
3800bd670b35SErik Nordmark 				    "Address                      PMTU  "
3801bd670b35SErik Nordmark 				    " Age Flags If ");
3802bd670b35SErik Nordmark 				(void) puts(
3803bd670b35SErik Nordmark 				    "--------------------------- ------ "
3804bd670b35SErik Nordmark 				    "----- ----- ---");
3805bd670b35SErik Nordmark 				first = B_FALSE;
3806bd670b35SErik Nordmark 			}
3807bd670b35SErik Nordmark 
3808bd670b35SErik Nordmark 			(void) printf("%-27s %6u %5u %-5s %s\n",
3809bd670b35SErik Nordmark 			    pr_addr6(&dce->DestIpv6Address, abuf,
3810bd670b35SErik Nordmark 			    sizeof (abuf)),
3811bd670b35SErik Nordmark 			    dce->DestPmtu, dce->DestAge,
3812bd670b35SErik Nordmark 			    dceflags2str(dce->DestFlags, flbuf),
3813bd670b35SErik Nordmark 			    dce->DestIfindex == 0 ? "" :
3814bd670b35SErik Nordmark 			    ifindex2str(dce->DestIfindex, ifname));
3815bd670b35SErik Nordmark 		}
3816bd670b35SErik Nordmark 	}
3817bd670b35SErik Nordmark 	(void) fflush(stdout);
3818bd670b35SErik Nordmark }
3819bd670b35SErik Nordmark 
38207c478bd9Sstevel@tonic-gate /* --------------------- ARP_REPORT (netstat -p) -------------------------- */
38217c478bd9Sstevel@tonic-gate 
38227c478bd9Sstevel@tonic-gate static void
38237c478bd9Sstevel@tonic-gate arp_report(mib_item_t *item)
38247c478bd9Sstevel@tonic-gate {
38257c478bd9Sstevel@tonic-gate 	int		jtemp = 0;
38267c478bd9Sstevel@tonic-gate 	char		ifname[LIFNAMSIZ + 1];
38277c478bd9Sstevel@tonic-gate 	char		abuf[MAXHOSTNAMELEN + 1];
38287c478bd9Sstevel@tonic-gate 	char		maskbuf[STR_EXPAND * OCTET_LENGTH + 1];
38297c478bd9Sstevel@tonic-gate 	char		flbuf[32];	/* ACE_F_ flags */
38307c478bd9Sstevel@tonic-gate 	char		xbuf[STR_EXPAND * OCTET_LENGTH + 1];
38317c478bd9Sstevel@tonic-gate 	mib2_ipNetToMediaEntry_t	*np;
38327c478bd9Sstevel@tonic-gate 	int		flags;
38337c478bd9Sstevel@tonic-gate 	boolean_t	first;
38347c478bd9Sstevel@tonic-gate 
38357c478bd9Sstevel@tonic-gate 	if (!(family_selected(AF_INET)))
38367c478bd9Sstevel@tonic-gate 		return;
38377c478bd9Sstevel@tonic-gate 
38387c478bd9Sstevel@tonic-gate 	/* 'for' loop 1: */
38397c478bd9Sstevel@tonic-gate 	for (; item; item = item->next_item) {
3840bd670b35SErik Nordmark 		if (Xflag) {
38417c478bd9Sstevel@tonic-gate 			(void) printf("\n--- Entry %d ---\n", ++jtemp);
38427c478bd9Sstevel@tonic-gate 			(void) printf("Group = %d, mib_id = %d, "
38437c478bd9Sstevel@tonic-gate 			    "length = %d, valp = 0x%p\n",
38447c478bd9Sstevel@tonic-gate 			    item->group, item->mib_id, item->length,
38457c478bd9Sstevel@tonic-gate 			    item->valp);
38467c478bd9Sstevel@tonic-gate 		}
38477c478bd9Sstevel@tonic-gate 		if (!(item->group == MIB2_IP && item->mib_id == MIB2_IP_MEDIA))
38487c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
38497c478bd9Sstevel@tonic-gate 
3850bd670b35SErik Nordmark 		if (Xflag)
38517c478bd9Sstevel@tonic-gate 			(void) printf("%u records for "
38527c478bd9Sstevel@tonic-gate 			    "ipNetToMediaEntryTable:\n",
38537c478bd9Sstevel@tonic-gate 			    item->length/sizeof (mib2_ipNetToMediaEntry_t));
38547c478bd9Sstevel@tonic-gate 
38557c478bd9Sstevel@tonic-gate 		first = B_TRUE;
38567c478bd9Sstevel@tonic-gate 		/* 'for' loop 2: */
38577c478bd9Sstevel@tonic-gate 		for (np = (mib2_ipNetToMediaEntry_t *)item->valp;
38587c478bd9Sstevel@tonic-gate 		    (char *)np < (char *)item->valp + item->length;
38597c478bd9Sstevel@tonic-gate 		    /* LINTED: (note 1) */
38607c478bd9Sstevel@tonic-gate 		    np = (mib2_ipNetToMediaEntry_t *)((char *)np +
38617c478bd9Sstevel@tonic-gate 		    ipNetToMediaEntrySize)) {
38627c478bd9Sstevel@tonic-gate 			if (first) {
38637c478bd9Sstevel@tonic-gate 				(void) puts(v4compat ?
38647c478bd9Sstevel@tonic-gate 				    "Net to Media Table" :
38657c478bd9Sstevel@tonic-gate 				    "Net to Media Table: IPv4");
386669bb4bb4Scarlsonj 				(void) puts("Device "
386769bb4bb4Scarlsonj 				    "  IP Address               Mask      "
386869bb4bb4Scarlsonj 				    "Flags      Phys Addr");
386969bb4bb4Scarlsonj 				(void) puts("------ "
387069bb4bb4Scarlsonj 				    "-------------------- --------------- "
387169bb4bb4Scarlsonj 				    "-------- ---------------");
38727c478bd9Sstevel@tonic-gate 				first = B_FALSE;
38737c478bd9Sstevel@tonic-gate 			}
38747c478bd9Sstevel@tonic-gate 
38757c478bd9Sstevel@tonic-gate 			flbuf[0] = '\0';
38767c478bd9Sstevel@tonic-gate 			flags = np->ipNetToMediaInfo.ntm_flags;
387769bb4bb4Scarlsonj 			/*
387869bb4bb4Scarlsonj 			 * Note that not all flags are possible at the same
387969bb4bb4Scarlsonj 			 * time.  Patterns: SPLAy DUo
388069bb4bb4Scarlsonj 			 */
38817c478bd9Sstevel@tonic-gate 			if (flags & ACE_F_PERMANENT)
38827c478bd9Sstevel@tonic-gate 				(void) strcat(flbuf, "S");
38837c478bd9Sstevel@tonic-gate 			if (flags & ACE_F_PUBLISH)
38847c478bd9Sstevel@tonic-gate 				(void) strcat(flbuf, "P");
38857c478bd9Sstevel@tonic-gate 			if (flags & ACE_F_DYING)
38867c478bd9Sstevel@tonic-gate 				(void) strcat(flbuf, "D");
38877c478bd9Sstevel@tonic-gate 			if (!(flags & ACE_F_RESOLVED))
38887c478bd9Sstevel@tonic-gate 				(void) strcat(flbuf, "U");
38897c478bd9Sstevel@tonic-gate 			if (flags & ACE_F_MAPPING)
38907c478bd9Sstevel@tonic-gate 				(void) strcat(flbuf, "M");
389169bb4bb4Scarlsonj 			if (flags & ACE_F_MYADDR)
389269bb4bb4Scarlsonj 				(void) strcat(flbuf, "L");
389369bb4bb4Scarlsonj 			if (flags & ACE_F_UNVERIFIED)
389469bb4bb4Scarlsonj 				(void) strcat(flbuf, "d");
389569bb4bb4Scarlsonj 			if (flags & ACE_F_AUTHORITY)
389669bb4bb4Scarlsonj 				(void) strcat(flbuf, "A");
389769bb4bb4Scarlsonj 			if (flags & ACE_F_OLD)
389869bb4bb4Scarlsonj 				(void) strcat(flbuf, "o");
389969bb4bb4Scarlsonj 			if (flags & ACE_F_DELAYED)
390069bb4bb4Scarlsonj 				(void) strcat(flbuf, "y");
390169bb4bb4Scarlsonj 			(void) printf("%-6s %-20s %-15s %-8s %s\n",
39027c478bd9Sstevel@tonic-gate 			    octetstr(&np->ipNetToMediaIfIndex, 'a',
39037c478bd9Sstevel@tonic-gate 			    ifname, sizeof (ifname)),
39047c478bd9Sstevel@tonic-gate 			    pr_addr(np->ipNetToMediaNetAddress,
39057c478bd9Sstevel@tonic-gate 			    abuf, sizeof (abuf)),
39067c478bd9Sstevel@tonic-gate 			    octetstr(&np->ipNetToMediaInfo.ntm_mask, 'd',
39077c478bd9Sstevel@tonic-gate 			    maskbuf, sizeof (maskbuf)),
39087c478bd9Sstevel@tonic-gate 			    flbuf,
39097c478bd9Sstevel@tonic-gate 			    octetstr(&np->ipNetToMediaPhysAddress, 'h',
39107c478bd9Sstevel@tonic-gate 			    xbuf, sizeof (xbuf)));
39117c478bd9Sstevel@tonic-gate 		} /* 'for' loop 2 ends */
39127c478bd9Sstevel@tonic-gate 	} /* 'for' loop 1 ends */
39137c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
39147c478bd9Sstevel@tonic-gate }
39157c478bd9Sstevel@tonic-gate 
39167c478bd9Sstevel@tonic-gate /* --------------------- NDP_REPORT (netstat -p) -------------------------- */
39177c478bd9Sstevel@tonic-gate 
39187c478bd9Sstevel@tonic-gate static void
39197c478bd9Sstevel@tonic-gate ndp_report(mib_item_t *item)
39207c478bd9Sstevel@tonic-gate {
39217c478bd9Sstevel@tonic-gate 	int		jtemp = 0;
39227c478bd9Sstevel@tonic-gate 	char		abuf[MAXHOSTNAMELEN + 1];
39237c478bd9Sstevel@tonic-gate 	char		*state;
39247c478bd9Sstevel@tonic-gate 	char		*type;
39257c478bd9Sstevel@tonic-gate 	char		xbuf[STR_EXPAND * OCTET_LENGTH + 1];
39267c478bd9Sstevel@tonic-gate 	mib2_ipv6NetToMediaEntry_t	*np6;
39277c478bd9Sstevel@tonic-gate 	char		ifname[LIFNAMSIZ + 1];
39287c478bd9Sstevel@tonic-gate 	boolean_t	first;
39297c478bd9Sstevel@tonic-gate 
39307c478bd9Sstevel@tonic-gate 	if (!(family_selected(AF_INET6)))
39317c478bd9Sstevel@tonic-gate 		return;
39327c478bd9Sstevel@tonic-gate 
39337c478bd9Sstevel@tonic-gate 	/* 'for' loop 1: */
39347c478bd9Sstevel@tonic-gate 	for (; item; item = item->next_item) {
3935bd670b35SErik Nordmark 		if (Xflag) {
39367c478bd9Sstevel@tonic-gate 			(void) printf("\n--- Entry %d ---\n", ++jtemp);
39377c478bd9Sstevel@tonic-gate 			(void) printf("Group = %d, mib_id = %d, "
39387c478bd9Sstevel@tonic-gate 			    "length = %d, valp = 0x%p\n",
39397c478bd9Sstevel@tonic-gate 			    item->group, item->mib_id, item->length,
39407c478bd9Sstevel@tonic-gate 			    item->valp);
39417c478bd9Sstevel@tonic-gate 		}
39427c478bd9Sstevel@tonic-gate 		if (!(item->group == MIB2_IP6 &&
39437c478bd9Sstevel@tonic-gate 		    item->mib_id == MIB2_IP6_MEDIA))
39447c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
39457c478bd9Sstevel@tonic-gate 
39467c478bd9Sstevel@tonic-gate 		first = B_TRUE;
39477c478bd9Sstevel@tonic-gate 		/* 'for' loop 2: */
39487c478bd9Sstevel@tonic-gate 		for (np6 = (mib2_ipv6NetToMediaEntry_t *)item->valp;
39497c478bd9Sstevel@tonic-gate 		    (char *)np6 < (char *)item->valp + item->length;
39507c478bd9Sstevel@tonic-gate 		    /* LINTED: (note 1) */
39517c478bd9Sstevel@tonic-gate 		    np6 = (mib2_ipv6NetToMediaEntry_t *)((char *)np6 +
39527c478bd9Sstevel@tonic-gate 		    ipv6NetToMediaEntrySize)) {
39537c478bd9Sstevel@tonic-gate 			if (first) {
39547c478bd9Sstevel@tonic-gate 				(void) puts("\nNet to Media Table: IPv6");
39557c478bd9Sstevel@tonic-gate 				(void) puts(" If   Physical Address   "
39567c478bd9Sstevel@tonic-gate 				    " Type      State      Destination/Mask");
39577c478bd9Sstevel@tonic-gate 				(void) puts("----- -----------------  "
39587c478bd9Sstevel@tonic-gate 				    "------- ------------ "
39597c478bd9Sstevel@tonic-gate 				    "---------------------------");
39607c478bd9Sstevel@tonic-gate 				first = B_FALSE;
39617c478bd9Sstevel@tonic-gate 			}
39627c478bd9Sstevel@tonic-gate 
39637c478bd9Sstevel@tonic-gate 			switch (np6->ipv6NetToMediaState) {
39647c478bd9Sstevel@tonic-gate 			case ND_INCOMPLETE:
39657c478bd9Sstevel@tonic-gate 				state = "INCOMPLETE";
39667c478bd9Sstevel@tonic-gate 				break;
39677c478bd9Sstevel@tonic-gate 			case ND_REACHABLE:
39687c478bd9Sstevel@tonic-gate 				state = "REACHABLE";
39697c478bd9Sstevel@tonic-gate 				break;
39707c478bd9Sstevel@tonic-gate 			case ND_STALE:
39717c478bd9Sstevel@tonic-gate 				state = "STALE";
39727c478bd9Sstevel@tonic-gate 				break;
39737c478bd9Sstevel@tonic-gate 			case ND_DELAY:
39747c478bd9Sstevel@tonic-gate 				state = "DELAY";
39757c478bd9Sstevel@tonic-gate 				break;
39767c478bd9Sstevel@tonic-gate 			case ND_PROBE:
39777c478bd9Sstevel@tonic-gate 				state = "PROBE";
39787c478bd9Sstevel@tonic-gate 				break;
39797c478bd9Sstevel@tonic-gate 			case ND_UNREACHABLE:
39807c478bd9Sstevel@tonic-gate 				state = "UNREACHABLE";
39817c478bd9Sstevel@tonic-gate 				break;
39827c478bd9Sstevel@tonic-gate 			default:
39837c478bd9Sstevel@tonic-gate 				state = "UNKNOWN";
39847c478bd9Sstevel@tonic-gate 			}
39857c478bd9Sstevel@tonic-gate 
39867c478bd9Sstevel@tonic-gate 			switch (np6->ipv6NetToMediaType) {
39877c478bd9Sstevel@tonic-gate 			case 1:
39887c478bd9Sstevel@tonic-gate 				type = "other";
39897c478bd9Sstevel@tonic-gate 				break;
39907c478bd9Sstevel@tonic-gate 			case 2:
39917c478bd9Sstevel@tonic-gate 				type = "dynamic";
39927c478bd9Sstevel@tonic-gate 				break;
39937c478bd9Sstevel@tonic-gate 			case 3:
39947c478bd9Sstevel@tonic-gate 				type = "static";
39957c478bd9Sstevel@tonic-gate 				break;
39967c478bd9Sstevel@tonic-gate 			case 4:
39977c478bd9Sstevel@tonic-gate 				type = "local";
39987c478bd9Sstevel@tonic-gate 				break;
39997c478bd9Sstevel@tonic-gate 			}
40007c478bd9Sstevel@tonic-gate 			(void) printf("%-5s %-17s  %-7s %-12s %-27s\n",
4001e11c3f44Smeem 			    ifindex2str(np6->ipv6NetToMediaIfIndex, ifname),
40027c478bd9Sstevel@tonic-gate 			    octetstr(&np6->ipv6NetToMediaPhysAddress, 'h',
40037c478bd9Sstevel@tonic-gate 			    xbuf, sizeof (xbuf)),
40047c478bd9Sstevel@tonic-gate 			    type,
40057c478bd9Sstevel@tonic-gate 			    state,
40067c478bd9Sstevel@tonic-gate 			    pr_addr6(&np6->ipv6NetToMediaNetAddress,
40077c478bd9Sstevel@tonic-gate 			    abuf, sizeof (abuf)));
40087c478bd9Sstevel@tonic-gate 		} /* 'for' loop 2 ends */
40097c478bd9Sstevel@tonic-gate 	} /* 'for' loop 1 ends */
40107c478bd9Sstevel@tonic-gate 	(void) putchar('\n');
40117c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
40127c478bd9Sstevel@tonic-gate }
40137c478bd9Sstevel@tonic-gate 
40147c478bd9Sstevel@tonic-gate /* ------------------------- ire_report (netstat -r) ------------------------ */
40157c478bd9Sstevel@tonic-gate 
401645916cd2Sjpk typedef struct sec_attr_list_s {
401745916cd2Sjpk 	struct sec_attr_list_s *sal_next;
401845916cd2Sjpk 	const mib2_ipAttributeEntry_t *sal_attr;
401945916cd2Sjpk } sec_attr_list_t;
402045916cd2Sjpk 
402145916cd2Sjpk static boolean_t ire_report_item_v4(const mib2_ipRouteEntry_t *, boolean_t,
402245916cd2Sjpk     const sec_attr_list_t *);
402345916cd2Sjpk static boolean_t ire_report_item_v6(const mib2_ipv6RouteEntry_t *, boolean_t,
402445916cd2Sjpk     const sec_attr_list_t *);
402545916cd2Sjpk static const char *pr_secattr(const sec_attr_list_t *);
40267c478bd9Sstevel@tonic-gate 
40277c478bd9Sstevel@tonic-gate static void
402845916cd2Sjpk ire_report(const mib_item_t *item)
40297c478bd9Sstevel@tonic-gate {
40307c478bd9Sstevel@tonic-gate 	int			jtemp = 0;
40317c478bd9Sstevel@tonic-gate 	boolean_t		print_hdr_once_v4 = B_TRUE;
40327c478bd9Sstevel@tonic-gate 	boolean_t		print_hdr_once_v6 = B_TRUE;
40337c478bd9Sstevel@tonic-gate 	mib2_ipRouteEntry_t	*rp;
40347c478bd9Sstevel@tonic-gate 	mib2_ipv6RouteEntry_t	*rp6;
403545916cd2Sjpk 	sec_attr_list_t		**v4_attrs, **v4a;
403645916cd2Sjpk 	sec_attr_list_t		**v6_attrs, **v6a;
403745916cd2Sjpk 	sec_attr_list_t		*all_attrs, *aptr;
403845916cd2Sjpk 	const mib_item_t	*iptr;
403945916cd2Sjpk 	int			ipv4_route_count, ipv6_route_count;
404045916cd2Sjpk 	int			route_attrs_count;
404145916cd2Sjpk 
404245916cd2Sjpk 	/*
404345916cd2Sjpk 	 * Preparation pass: the kernel returns separate entries for IP routing
404445916cd2Sjpk 	 * table entries and security attributes.  We loop through the
404545916cd2Sjpk 	 * attributes first and link them into lists.
404645916cd2Sjpk 	 */
404745916cd2Sjpk 	ipv4_route_count = ipv6_route_count = route_attrs_count = 0;
404845916cd2Sjpk 	for (iptr = item; iptr != NULL; iptr = iptr->next_item) {
404945916cd2Sjpk 		if (iptr->group == MIB2_IP6 && iptr->mib_id == MIB2_IP6_ROUTE)
405045916cd2Sjpk 			ipv6_route_count += iptr->length / ipv6RouteEntrySize;
405145916cd2Sjpk 		if (iptr->group == MIB2_IP && iptr->mib_id == MIB2_IP_ROUTE)
405245916cd2Sjpk 			ipv4_route_count += iptr->length / ipRouteEntrySize;
405345916cd2Sjpk 		if ((iptr->group == MIB2_IP || iptr->group == MIB2_IP6) &&
405445916cd2Sjpk 		    iptr->mib_id == EXPER_IP_RTATTR)
405545916cd2Sjpk 			route_attrs_count += iptr->length /
405645916cd2Sjpk 			    ipRouteAttributeSize;
405745916cd2Sjpk 	}
405845916cd2Sjpk 	v4_attrs = v6_attrs = NULL;
405945916cd2Sjpk 	all_attrs = NULL;
406045916cd2Sjpk 	if (family_selected(AF_INET) && ipv4_route_count > 0) {
406145916cd2Sjpk 		v4_attrs = calloc(ipv4_route_count, sizeof (*v4_attrs));
406245916cd2Sjpk 		if (v4_attrs == NULL) {
406345916cd2Sjpk 			perror("ire_report calloc v4_attrs failed");
406445916cd2Sjpk 			return;
406545916cd2Sjpk 		}
406645916cd2Sjpk 	}
406745916cd2Sjpk 	if (family_selected(AF_INET6) && ipv6_route_count > 0) {
406845916cd2Sjpk 		v6_attrs = calloc(ipv6_route_count, sizeof (*v6_attrs));
406945916cd2Sjpk 		if (v6_attrs == NULL) {
407045916cd2Sjpk 			perror("ire_report calloc v6_attrs failed");
407145916cd2Sjpk 			goto ire_report_done;
407245916cd2Sjpk 		}
407345916cd2Sjpk 	}
407445916cd2Sjpk 	if (route_attrs_count > 0) {
407545916cd2Sjpk 		all_attrs = malloc(route_attrs_count * sizeof (*all_attrs));
407645916cd2Sjpk 		if (all_attrs == NULL) {
407745916cd2Sjpk 			perror("ire_report malloc all_attrs failed");
407845916cd2Sjpk 			goto ire_report_done;
407945916cd2Sjpk 		}
408045916cd2Sjpk 	}
408145916cd2Sjpk 	aptr = all_attrs;
408245916cd2Sjpk 	for (iptr = item; iptr != NULL; iptr = iptr->next_item) {
408345916cd2Sjpk 		mib2_ipAttributeEntry_t *iae;
408445916cd2Sjpk 		sec_attr_list_t **alp;
408545916cd2Sjpk 
408645916cd2Sjpk 		if (v4_attrs != NULL && iptr->group == MIB2_IP &&
408745916cd2Sjpk 		    iptr->mib_id == EXPER_IP_RTATTR) {
408845916cd2Sjpk 			alp = v4_attrs;
408945916cd2Sjpk 		} else if (v6_attrs != NULL && iptr->group == MIB2_IP6 &&
409045916cd2Sjpk 		    iptr->mib_id == EXPER_IP_RTATTR) {
409145916cd2Sjpk 			alp = v6_attrs;
409245916cd2Sjpk 		} else {
409345916cd2Sjpk 			continue;
409445916cd2Sjpk 		}
409545916cd2Sjpk 		for (iae = iptr->valp;
409645916cd2Sjpk 		    (char *)iae < (char *)iptr->valp + iptr->length;
409745916cd2Sjpk 		    /* LINTED: (note 1) */
409845916cd2Sjpk 		    iae = (mib2_ipAttributeEntry_t *)((char *)iae +
409945916cd2Sjpk 		    ipRouteAttributeSize)) {
410045916cd2Sjpk 			aptr->sal_next = alp[iae->iae_routeidx];
410145916cd2Sjpk 			aptr->sal_attr = iae;
410245916cd2Sjpk 			alp[iae->iae_routeidx] = aptr++;
410345916cd2Sjpk 		}
410445916cd2Sjpk 	}
41057c478bd9Sstevel@tonic-gate 
41067c478bd9Sstevel@tonic-gate 	/* 'for' loop 1: */
410745916cd2Sjpk 	v4a = v4_attrs;
410845916cd2Sjpk 	v6a = v6_attrs;
410945916cd2Sjpk 	for (; item != NULL; item = item->next_item) {
4110bd670b35SErik Nordmark 		if (Xflag) {
41117c478bd9Sstevel@tonic-gate 			(void) printf("\n--- Entry %d ---\n", ++jtemp);
41127c478bd9Sstevel@tonic-gate 			(void) printf("Group = %d, mib_id = %d, "
41137c478bd9Sstevel@tonic-gate 			    "length = %d, valp = 0x%p\n",
41147c478bd9Sstevel@tonic-gate 			    item->group, item->mib_id,
41157c478bd9Sstevel@tonic-gate 			    item->length, item->valp);
41167c478bd9Sstevel@tonic-gate 		}
41177c478bd9Sstevel@tonic-gate 		if (!((item->group == MIB2_IP &&
41187c478bd9Sstevel@tonic-gate 		    item->mib_id == MIB2_IP_ROUTE) ||
41197c478bd9Sstevel@tonic-gate 		    (item->group == MIB2_IP6 &&
41207c478bd9Sstevel@tonic-gate 		    item->mib_id == MIB2_IP6_ROUTE)))
41217c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
41227c478bd9Sstevel@tonic-gate 
41237c478bd9Sstevel@tonic-gate 		if (item->group == MIB2_IP && !family_selected(AF_INET))
41247c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
41257c478bd9Sstevel@tonic-gate 		else if (item->group == MIB2_IP6 && !family_selected(AF_INET6))
41267c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
41277c478bd9Sstevel@tonic-gate 
4128bd670b35SErik Nordmark 		if (Xflag) {
41297c478bd9Sstevel@tonic-gate 			if (item->group == MIB2_IP) {
41307c478bd9Sstevel@tonic-gate 				(void) printf("%u records for "
41317c478bd9Sstevel@tonic-gate 				    "ipRouteEntryTable:\n",
41327c478bd9Sstevel@tonic-gate 				    item->length/sizeof (mib2_ipRouteEntry_t));
41337c478bd9Sstevel@tonic-gate 			} else {
41347c478bd9Sstevel@tonic-gate 				(void) printf("%u records for "
41357c478bd9Sstevel@tonic-gate 				    "ipv6RouteEntryTable:\n",
41367c478bd9Sstevel@tonic-gate 				    item->length/
41377c478bd9Sstevel@tonic-gate 				    sizeof (mib2_ipv6RouteEntry_t));
41387c478bd9Sstevel@tonic-gate 			}
41397c478bd9Sstevel@tonic-gate 		}
41407c478bd9Sstevel@tonic-gate 
41417c478bd9Sstevel@tonic-gate 		if (item->group == MIB2_IP) {
41427c478bd9Sstevel@tonic-gate 			for (rp = (mib2_ipRouteEntry_t *)item->valp;
41437c478bd9Sstevel@tonic-gate 			    (char *)rp < (char *)item->valp + item->length;
41447c478bd9Sstevel@tonic-gate 			    /* LINTED: (note 1) */
41457c478bd9Sstevel@tonic-gate 			    rp = (mib2_ipRouteEntry_t *)((char *)rp +
41467c478bd9Sstevel@tonic-gate 			    ipRouteEntrySize)) {
414745916cd2Sjpk 				aptr = v4a == NULL ? NULL : *v4a++;
41487c478bd9Sstevel@tonic-gate 				print_hdr_once_v4 = ire_report_item_v4(rp,
414945916cd2Sjpk 				    print_hdr_once_v4, aptr);
41507c478bd9Sstevel@tonic-gate 			}
41517c478bd9Sstevel@tonic-gate 		} else {
41527c478bd9Sstevel@tonic-gate 			for (rp6 = (mib2_ipv6RouteEntry_t *)item->valp;
41537c478bd9Sstevel@tonic-gate 			    (char *)rp6 < (char *)item->valp + item->length;
41547c478bd9Sstevel@tonic-gate 			    /* LINTED: (note 1) */
41557c478bd9Sstevel@tonic-gate 			    rp6 = (mib2_ipv6RouteEntry_t *)((char *)rp6 +
41567c478bd9Sstevel@tonic-gate 			    ipv6RouteEntrySize)) {
415745916cd2Sjpk 				aptr = v6a == NULL ? NULL : *v6a++;
41587c478bd9Sstevel@tonic-gate 				print_hdr_once_v6 = ire_report_item_v6(rp6,
415945916cd2Sjpk 				    print_hdr_once_v6, aptr);
41607c478bd9Sstevel@tonic-gate 			}
41617c478bd9Sstevel@tonic-gate 		}
41627c478bd9Sstevel@tonic-gate 	} /* 'for' loop 1 ends */
41637c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
416445916cd2Sjpk ire_report_done:
416545916cd2Sjpk 	if (v4_attrs != NULL)
416645916cd2Sjpk 		free(v4_attrs);
416745916cd2Sjpk 	if (v6_attrs != NULL)
416845916cd2Sjpk 		free(v6_attrs);
416945916cd2Sjpk 	if (all_attrs != NULL)
417045916cd2Sjpk 		free(all_attrs);
41717c478bd9Sstevel@tonic-gate }
41727c478bd9Sstevel@tonic-gate 
41737c478bd9Sstevel@tonic-gate /*
41747c478bd9Sstevel@tonic-gate  * Match a user-supplied device name.  We do this by string because
41757c478bd9Sstevel@tonic-gate  * the MIB2 interface gives us interface name strings rather than
41767c478bd9Sstevel@tonic-gate  * ifIndex numbers.  The "none" rule matches only routes with no
41777c478bd9Sstevel@tonic-gate  * interface.  The "any" rule matches routes with any non-blank
41787c478bd9Sstevel@tonic-gate  * interface.  A base name ("hme0") matches all aliases as well
41797c478bd9Sstevel@tonic-gate  * ("hme0:1").
41807c478bd9Sstevel@tonic-gate  */
41817c478bd9Sstevel@tonic-gate static boolean_t
41827c478bd9Sstevel@tonic-gate dev_name_match(const DeviceName *devnam, const char *ifname)
41837c478bd9Sstevel@tonic-gate {
41847c478bd9Sstevel@tonic-gate 	int iflen;
41857c478bd9Sstevel@tonic-gate 
41867c478bd9Sstevel@tonic-gate 	if (ifname == NULL)
41877c478bd9Sstevel@tonic-gate 		return (devnam->o_length == 0);		/* "none" */
41887c478bd9Sstevel@tonic-gate 	if (*ifname == '\0')
41897c478bd9Sstevel@tonic-gate 		return (devnam->o_length != 0);		/* "any" */
41907c478bd9Sstevel@tonic-gate 	iflen = strlen(ifname);
41917c478bd9Sstevel@tonic-gate 	/* The check for ':' here supports interface aliases. */
41927c478bd9Sstevel@tonic-gate 	if (iflen > devnam->o_length ||
41937c478bd9Sstevel@tonic-gate 	    (iflen < devnam->o_length && devnam->o_bytes[iflen] != ':'))
41947c478bd9Sstevel@tonic-gate 		return (B_FALSE);
41957c478bd9Sstevel@tonic-gate 	return (strncmp(ifname, devnam->o_bytes, iflen) == 0);
41967c478bd9Sstevel@tonic-gate }
41977c478bd9Sstevel@tonic-gate 
41987c478bd9Sstevel@tonic-gate /*
41997c478bd9Sstevel@tonic-gate  * Match a user-supplied IP address list.  The "any" rule matches any
42007c478bd9Sstevel@tonic-gate  * non-zero address.  The "none" rule matches only the zero address.
42017c478bd9Sstevel@tonic-gate  * IPv6 addresses supplied by the user are ignored.  If the user
42027c478bd9Sstevel@tonic-gate  * supplies a subnet mask, then match routes that are at least that
42037c478bd9Sstevel@tonic-gate  * specific (use the user's mask).  If the user supplies only an
42047c478bd9Sstevel@tonic-gate  * address, then select any routes that would match (use the route's
42057c478bd9Sstevel@tonic-gate  * mask).
42067c478bd9Sstevel@tonic-gate  */
42077c478bd9Sstevel@tonic-gate static boolean_t
42087c478bd9Sstevel@tonic-gate v4_addr_match(IpAddress addr, IpAddress mask, const filter_t *fp)
42097c478bd9Sstevel@tonic-gate {
42107c478bd9Sstevel@tonic-gate 	char **app;
42117c478bd9Sstevel@tonic-gate 	char *aptr;
42127c478bd9Sstevel@tonic-gate 	in_addr_t faddr, fmask;
42137c478bd9Sstevel@tonic-gate 
42147c478bd9Sstevel@tonic-gate 	if (fp->u.a.f_address == NULL) {
42157c478bd9Sstevel@tonic-gate 		if (IN6_IS_ADDR_UNSPECIFIED(&fp->u.a.f_mask))
42167c478bd9Sstevel@tonic-gate 			return (addr != INADDR_ANY);	/* "any" */
42177c478bd9Sstevel@tonic-gate 		else
42187c478bd9Sstevel@tonic-gate 			return (addr == INADDR_ANY);	/* "none" */
42197c478bd9Sstevel@tonic-gate 	}
42207c478bd9Sstevel@tonic-gate 	if (!IN6_IS_V4MASK(fp->u.a.f_mask))
42217c478bd9Sstevel@tonic-gate 		return (B_FALSE);
42227c478bd9Sstevel@tonic-gate 	IN6_V4MAPPED_TO_IPADDR(&fp->u.a.f_mask, fmask);
42237c478bd9Sstevel@tonic-gate 	if (fmask != IP_HOST_MASK) {
42247c478bd9Sstevel@tonic-gate 		if (fmask > mask)
42257c478bd9Sstevel@tonic-gate 			return (B_FALSE);
42267c478bd9Sstevel@tonic-gate 		mask = fmask;
42277c478bd9Sstevel@tonic-gate 	}
42287c478bd9Sstevel@tonic-gate 	for (app = fp->u.a.f_address->h_addr_list; (aptr = *app) != NULL; app++)
42297c478bd9Sstevel@tonic-gate 		/* LINTED: (note 1) */
42307c478bd9Sstevel@tonic-gate 		if (IN6_IS_ADDR_V4MAPPED((in6_addr_t *)aptr)) {
42317c478bd9Sstevel@tonic-gate 			/* LINTED: (note 1) */
42327c478bd9Sstevel@tonic-gate 			IN6_V4MAPPED_TO_IPADDR((in6_addr_t *)aptr, faddr);
42337c478bd9Sstevel@tonic-gate 			if (((faddr ^ addr) & mask) == 0)
42347c478bd9Sstevel@tonic-gate 				return (B_TRUE);
42357c478bd9Sstevel@tonic-gate 		}
42367c478bd9Sstevel@tonic-gate 	return (B_FALSE);
42377c478bd9Sstevel@tonic-gate }
42387c478bd9Sstevel@tonic-gate 
42397c478bd9Sstevel@tonic-gate /*
42407c478bd9Sstevel@tonic-gate  * Run through the filter list for an IPv4 MIB2 route entry.  If all
42417c478bd9Sstevel@tonic-gate  * filters of a given type fail to match, then the route is filtered
42427c478bd9Sstevel@tonic-gate  * out (not displayed).  If no filter is given or at least one filter
42437c478bd9Sstevel@tonic-gate  * of each type matches, then display the route.
42447c478bd9Sstevel@tonic-gate  */
42457c478bd9Sstevel@tonic-gate static boolean_t
424645916cd2Sjpk ire_filter_match_v4(const mib2_ipRouteEntry_t *rp, uint_t flag_b)
42477c478bd9Sstevel@tonic-gate {
42487c478bd9Sstevel@tonic-gate 	filter_t *fp;
42497c478bd9Sstevel@tonic-gate 	int idx;
42507c478bd9Sstevel@tonic-gate 
42517c478bd9Sstevel@tonic-gate 	/* 'for' loop 1: */
42527c478bd9Sstevel@tonic-gate 	for (idx = 0; idx < NFILTERKEYS; idx++)
42537c478bd9Sstevel@tonic-gate 		if ((fp = filters[idx]) != NULL) {
42547c478bd9Sstevel@tonic-gate 			/* 'for' loop 2: */
42557c478bd9Sstevel@tonic-gate 			for (; fp != NULL; fp = fp->f_next) {
42567c478bd9Sstevel@tonic-gate 				switch (idx) {
42577c478bd9Sstevel@tonic-gate 				case FK_AF:
42587c478bd9Sstevel@tonic-gate 					if (fp->u.f_family != AF_INET)
42597c478bd9Sstevel@tonic-gate 						continue; /* 'for' loop 2 */
42607c478bd9Sstevel@tonic-gate 					break;
42617c478bd9Sstevel@tonic-gate 				case FK_OUTIF:
42627c478bd9Sstevel@tonic-gate 					if (!dev_name_match(&rp->ipRouteIfIndex,
42637c478bd9Sstevel@tonic-gate 					    fp->u.f_ifname))
42647c478bd9Sstevel@tonic-gate 						continue; /* 'for' loop 2 */
42657c478bd9Sstevel@tonic-gate 					break;
42667c478bd9Sstevel@tonic-gate 				case FK_DST:
42677c478bd9Sstevel@tonic-gate 					if (!v4_addr_match(rp->ipRouteDest,
42687c478bd9Sstevel@tonic-gate 					    rp->ipRouteMask, fp))
42697c478bd9Sstevel@tonic-gate 						continue; /* 'for' loop 2 */
42707c478bd9Sstevel@tonic-gate 					break;
42717c478bd9Sstevel@tonic-gate 				case FK_FLAGS:
42727c478bd9Sstevel@tonic-gate 					if ((flag_b & fp->u.f.f_flagset) !=
42737c478bd9Sstevel@tonic-gate 					    fp->u.f.f_flagset ||
42747c478bd9Sstevel@tonic-gate 					    (flag_b & fp->u.f.f_flagclear))
42757c478bd9Sstevel@tonic-gate 						continue; /* 'for' loop 2 */
42767c478bd9Sstevel@tonic-gate 					break;
42777c478bd9Sstevel@tonic-gate 				}
42787c478bd9Sstevel@tonic-gate 				break;
42797c478bd9Sstevel@tonic-gate 			} /* 'for' loop 2 ends */
42807c478bd9Sstevel@tonic-gate 			if (fp == NULL)
42817c478bd9Sstevel@tonic-gate 				return (B_FALSE);
42827c478bd9Sstevel@tonic-gate 		}
42837c478bd9Sstevel@tonic-gate 	/* 'for' loop 1 ends */
42847c478bd9Sstevel@tonic-gate 	return (B_TRUE);
42857c478bd9Sstevel@tonic-gate }
42867c478bd9Sstevel@tonic-gate 
42877c478bd9Sstevel@tonic-gate /*
42887c478bd9Sstevel@tonic-gate  * Given an IPv4 MIB2 route entry, form the list of flags for the
42897c478bd9Sstevel@tonic-gate  * route.
42907c478bd9Sstevel@tonic-gate  */
42917c478bd9Sstevel@tonic-gate static uint_t
429245916cd2Sjpk form_v4_route_flags(const mib2_ipRouteEntry_t *rp, char *flags)
42937c478bd9Sstevel@tonic-gate {
42947c478bd9Sstevel@tonic-gate 	uint_t flag_b;
42957c478bd9Sstevel@tonic-gate 
42967c478bd9Sstevel@tonic-gate 	flag_b = FLF_U;
42977c478bd9Sstevel@tonic-gate 	(void) strcpy(flags, "U");
4298bd670b35SErik Nordmark 	/* RTF_INDIRECT wins over RTF_GATEWAY - don't display both */
4299bd670b35SErik Nordmark 	if (rp->ipRouteInfo.re_flags & RTF_INDIRECT) {
4300bd670b35SErik Nordmark 		(void) strcat(flags, "I");
4301bd670b35SErik Nordmark 		flag_b |= FLF_I;
4302bd670b35SErik Nordmark 	} else if (rp->ipRouteInfo.re_ire_type & IRE_OFFLINK) {
43037c478bd9Sstevel@tonic-gate 		(void) strcat(flags, "G");
43047c478bd9Sstevel@tonic-gate 		flag_b |= FLF_G;
43057c478bd9Sstevel@tonic-gate 	}
4306bd670b35SErik Nordmark 	/* IRE_IF_CLONE wins over RTF_HOST - don't display both */
4307bd670b35SErik Nordmark 	if (rp->ipRouteInfo.re_ire_type & IRE_IF_CLONE) {
4308bd670b35SErik Nordmark 		(void) strcat(flags, "C");
4309bd670b35SErik Nordmark 		flag_b |= FLF_C;
4310bd670b35SErik Nordmark 	} else if (rp->ipRouteMask == IP_HOST_MASK) {
43117c478bd9Sstevel@tonic-gate 		(void) strcat(flags, "H");
43127c478bd9Sstevel@tonic-gate 		flag_b |= FLF_H;
43137c478bd9Sstevel@tonic-gate 	}
4314bd670b35SErik Nordmark 	if (rp->ipRouteInfo.re_flags & RTF_DYNAMIC) {
43157c478bd9Sstevel@tonic-gate 		(void) strcat(flags, "D");
43167c478bd9Sstevel@tonic-gate 		flag_b |= FLF_D;
43177c478bd9Sstevel@tonic-gate 	}
43187c478bd9Sstevel@tonic-gate 	if (rp->ipRouteInfo.re_ire_type == IRE_BROADCAST) {	/* Broadcast */
4319bd670b35SErik Nordmark 		(void) strcat(flags, "b");
4320bd670b35SErik Nordmark 		flag_b |= FLF_b;
43217c478bd9Sstevel@tonic-gate 	}
43227c478bd9Sstevel@tonic-gate 	if (rp->ipRouteInfo.re_ire_type == IRE_LOCAL) {		/* Local */
43237c478bd9Sstevel@tonic-gate 		(void) strcat(flags, "L");
43247c478bd9Sstevel@tonic-gate 		flag_b |= FLF_L;
43257c478bd9Sstevel@tonic-gate 	}
43267c478bd9Sstevel@tonic-gate 	if (rp->ipRouteInfo.re_flags & RTF_MULTIRT) {
43277c478bd9Sstevel@tonic-gate 		(void) strcat(flags, "M");			/* Multiroute */
43287c478bd9Sstevel@tonic-gate 		flag_b |= FLF_M;
43297c478bd9Sstevel@tonic-gate 	}
43307c478bd9Sstevel@tonic-gate 	if (rp->ipRouteInfo.re_flags & RTF_SETSRC) {
43317c478bd9Sstevel@tonic-gate 		(void) strcat(flags, "S");			/* Setsrc */
43327c478bd9Sstevel@tonic-gate 		flag_b |= FLF_S;
43337c478bd9Sstevel@tonic-gate 	}
4334bd670b35SErik Nordmark 	if (rp->ipRouteInfo.re_flags & RTF_REJECT) {
4335bd670b35SErik Nordmark 		(void) strcat(flags, "R");
4336bd670b35SErik Nordmark 		flag_b |= FLF_R;
4337bd670b35SErik Nordmark 	}
4338bd670b35SErik Nordmark 	if (rp->ipRouteInfo.re_flags & RTF_BLACKHOLE) {
4339bd670b35SErik Nordmark 		(void) strcat(flags, "B");
4340bd670b35SErik Nordmark 		flag_b |= FLF_B;
4341bd670b35SErik Nordmark 	}
4342*550b6e40SSowmini Varadhan 	if (rp->ipRouteInfo.re_flags & RTF_ZONE) {
4343*550b6e40SSowmini Varadhan 		(void) strcat(flags, "Z");
4344*550b6e40SSowmini Varadhan 		flag_b |= FLF_Z;
4345*550b6e40SSowmini Varadhan 	}
43467c478bd9Sstevel@tonic-gate 	return (flag_b);
43477c478bd9Sstevel@tonic-gate }
43487c478bd9Sstevel@tonic-gate 
434945916cd2Sjpk static const char ire_hdr_v4[] =
435045916cd2Sjpk "\n%s Table: IPv4\n";
435145916cd2Sjpk static const char ire_hdr_v4_compat[] =
435245916cd2Sjpk "\n%s Table:\n";
435345916cd2Sjpk static const char ire_hdr_v4_verbose[] =
4354bd670b35SErik Nordmark "  Destination             Mask           Gateway          Device "
4355bd670b35SErik Nordmark " MTU  Ref Flg  Out  In/Fwd %s\n"
4356bd670b35SErik Nordmark "-------------------- --------------- -------------------- ------ "
435745916cd2Sjpk "----- --- --- ----- ------ %s\n";
435845916cd2Sjpk 
435945916cd2Sjpk static const char ire_hdr_v4_normal[] =
4360aecc8c24Sja "  Destination           Gateway           Flags  Ref     Use     Interface"
4361aecc8c24Sja " %s\n-------------------- -------------------- ----- ----- ---------- "
4362aecc8c24Sja "--------- %s\n";
436345916cd2Sjpk 
43647c478bd9Sstevel@tonic-gate static boolean_t
436545916cd2Sjpk ire_report_item_v4(const mib2_ipRouteEntry_t *rp, boolean_t first,
436645916cd2Sjpk     const sec_attr_list_t *attrs)
43677c478bd9Sstevel@tonic-gate {
43687c478bd9Sstevel@tonic-gate 	char			dstbuf[MAXHOSTNAMELEN + 1];
43697c478bd9Sstevel@tonic-gate 	char			maskbuf[MAXHOSTNAMELEN + 1];
43707c478bd9Sstevel@tonic-gate 	char			gwbuf[MAXHOSTNAMELEN + 1];
43717c478bd9Sstevel@tonic-gate 	char			ifname[LIFNAMSIZ + 1];
43727c478bd9Sstevel@tonic-gate 	char			flags[10];	/* RTF_ flags */
43737c478bd9Sstevel@tonic-gate 	uint_t			flag_b;
43747c478bd9Sstevel@tonic-gate 
4375bd670b35SErik Nordmark 	if (!(Aflag || (rp->ipRouteInfo.re_ire_type != IRE_IF_CLONE &&
43767c478bd9Sstevel@tonic-gate 	    rp->ipRouteInfo.re_ire_type != IRE_BROADCAST &&
4377bd670b35SErik Nordmark 	    rp->ipRouteInfo.re_ire_type != IRE_MULTICAST &&
4378bd670b35SErik Nordmark 	    rp->ipRouteInfo.re_ire_type != IRE_NOROUTE &&
43797c478bd9Sstevel@tonic-gate 	    rp->ipRouteInfo.re_ire_type != IRE_LOCAL))) {
43807c478bd9Sstevel@tonic-gate 		return (first);
43817c478bd9Sstevel@tonic-gate 	}
43827c478bd9Sstevel@tonic-gate 
43837c478bd9Sstevel@tonic-gate 	flag_b = form_v4_route_flags(rp, flags);
43847c478bd9Sstevel@tonic-gate 
43857c478bd9Sstevel@tonic-gate 	if (!ire_filter_match_v4(rp, flag_b))
43867c478bd9Sstevel@tonic-gate 		return (first);
43877c478bd9Sstevel@tonic-gate 
43887c478bd9Sstevel@tonic-gate 	if (first) {
438945916cd2Sjpk 		(void) printf(v4compat ? ire_hdr_v4_compat : ire_hdr_v4,
439045916cd2Sjpk 		    Vflag ? "IRE" : "Routing");
439145916cd2Sjpk 		(void) printf(Vflag ? ire_hdr_v4_verbose : ire_hdr_v4_normal,
439245916cd2Sjpk 		    RSECflag ? "  Gateway security attributes  " : "",
439345916cd2Sjpk 		    RSECflag ? "-------------------------------" : "");
43947c478bd9Sstevel@tonic-gate 		first = B_FALSE;
43957c478bd9Sstevel@tonic-gate 	}
43967c478bd9Sstevel@tonic-gate 
43977c478bd9Sstevel@tonic-gate 	if (flag_b & FLF_H) {
43987c478bd9Sstevel@tonic-gate 		(void) pr_addr(rp->ipRouteDest, dstbuf, sizeof (dstbuf));
43997c478bd9Sstevel@tonic-gate 	} else {
44007c478bd9Sstevel@tonic-gate 		(void) pr_net(rp->ipRouteDest, rp->ipRouteMask,
44017c478bd9Sstevel@tonic-gate 		    dstbuf, sizeof (dstbuf));
44027c478bd9Sstevel@tonic-gate 	}
44037c478bd9Sstevel@tonic-gate 	if (Vflag) {
4404bd670b35SErik Nordmark 		(void) printf("%-20s %-15s %-20s %-6s %5u %3u "
4405aecc8c24Sja 		    "%-4s%6u %6u %s\n",
44067c478bd9Sstevel@tonic-gate 		    dstbuf,
44077c478bd9Sstevel@tonic-gate 		    pr_mask(rp->ipRouteMask, maskbuf, sizeof (maskbuf)),
44087c478bd9Sstevel@tonic-gate 		    pr_addrnz(rp->ipRouteNextHop, gwbuf, sizeof (gwbuf)),
44097c478bd9Sstevel@tonic-gate 		    octetstr(&rp->ipRouteIfIndex, 'a', ifname, sizeof (ifname)),
44107c478bd9Sstevel@tonic-gate 		    rp->ipRouteInfo.re_max_frag,
44117c478bd9Sstevel@tonic-gate 		    rp->ipRouteInfo.re_ref,
44127c478bd9Sstevel@tonic-gate 		    flags,
44137c478bd9Sstevel@tonic-gate 		    rp->ipRouteInfo.re_obpkt,
441445916cd2Sjpk 		    rp->ipRouteInfo.re_ibpkt,
441545916cd2Sjpk 		    pr_secattr(attrs));
44167c478bd9Sstevel@tonic-gate 	} else {
4417aecc8c24Sja 		(void) printf("%-20s %-20s %-5s  %4u %10u %-9s %s\n",
44187c478bd9Sstevel@tonic-gate 		    dstbuf,
44197c478bd9Sstevel@tonic-gate 		    pr_addrnz(rp->ipRouteNextHop, gwbuf, sizeof (gwbuf)),
44207c478bd9Sstevel@tonic-gate 		    flags,
44217c478bd9Sstevel@tonic-gate 		    rp->ipRouteInfo.re_ref,
44227c478bd9Sstevel@tonic-gate 		    rp->ipRouteInfo.re_obpkt + rp->ipRouteInfo.re_ibpkt,
44237c478bd9Sstevel@tonic-gate 		    octetstr(&rp->ipRouteIfIndex, 'a',
442445916cd2Sjpk 		    ifname, sizeof (ifname)),
442545916cd2Sjpk 		    pr_secattr(attrs));
44267c478bd9Sstevel@tonic-gate 	}
44277c478bd9Sstevel@tonic-gate 	return (first);
44287c478bd9Sstevel@tonic-gate }
44297c478bd9Sstevel@tonic-gate 
44307c478bd9Sstevel@tonic-gate /*
44317c478bd9Sstevel@tonic-gate  * Match a user-supplied IP address list against an IPv6 route entry.
44327c478bd9Sstevel@tonic-gate  * If the user specified "any," then any non-zero address matches.  If
44337c478bd9Sstevel@tonic-gate  * the user specified "none," then only the zero address matches.  If
44347c478bd9Sstevel@tonic-gate  * the user specified a subnet mask length, then use that in matching
44357c478bd9Sstevel@tonic-gate  * routes (select routes that are at least as specific).  If the user
44367c478bd9Sstevel@tonic-gate  * specified only an address, then use the route's mask (select routes
44377c478bd9Sstevel@tonic-gate  * that would match that address).  IPv4 addresses are ignored.
44387c478bd9Sstevel@tonic-gate  */
44397c478bd9Sstevel@tonic-gate static boolean_t
44407c478bd9Sstevel@tonic-gate v6_addr_match(const Ip6Address *addr, int masklen, const filter_t *fp)
44417c478bd9Sstevel@tonic-gate {
44427c478bd9Sstevel@tonic-gate 	const uint8_t *ucp;
44437c478bd9Sstevel@tonic-gate 	int fmasklen;
44447c478bd9Sstevel@tonic-gate 	int i;
44457c478bd9Sstevel@tonic-gate 	char **app;
4446265f37a1S 	const uint8_t *aptr;
44477c478bd9Sstevel@tonic-gate 
44487c478bd9Sstevel@tonic-gate 	if (fp->u.a.f_address == NULL) {
44497c478bd9Sstevel@tonic-gate 		if (IN6_IS_ADDR_UNSPECIFIED(&fp->u.a.f_mask))	/* any */
44507c478bd9Sstevel@tonic-gate 			return (!IN6_IS_ADDR_UNSPECIFIED(addr));
44517c478bd9Sstevel@tonic-gate 		return (IN6_IS_ADDR_UNSPECIFIED(addr));		/* "none" */
44527c478bd9Sstevel@tonic-gate 	}
44537c478bd9Sstevel@tonic-gate 	fmasklen = 0;
44547c478bd9Sstevel@tonic-gate 	/* 'for' loop 1a: */
44557c478bd9Sstevel@tonic-gate 	for (ucp = fp->u.a.f_mask.s6_addr;
44567c478bd9Sstevel@tonic-gate 	    ucp < fp->u.a.f_mask.s6_addr + sizeof (fp->u.a.f_mask.s6_addr);
44577c478bd9Sstevel@tonic-gate 	    ucp++) {
44587c478bd9Sstevel@tonic-gate 		if (*ucp != 0xff) {
44597c478bd9Sstevel@tonic-gate 			if (*ucp != 0)
44607c478bd9Sstevel@tonic-gate 				fmasklen += 9 - ffs(*ucp);
44617c478bd9Sstevel@tonic-gate 			break; /* 'for' loop 1a */
44627c478bd9Sstevel@tonic-gate 		}
44637c478bd9Sstevel@tonic-gate 		fmasklen += 8;
44647c478bd9Sstevel@tonic-gate 	} /* 'for' loop 1a ends */
44657c478bd9Sstevel@tonic-gate 	if (fmasklen != IPV6_ABITS) {
44667c478bd9Sstevel@tonic-gate 		if (fmasklen > masklen)
44677c478bd9Sstevel@tonic-gate 			return (B_FALSE);
44687c478bd9Sstevel@tonic-gate 		masklen = fmasklen;
44697c478bd9Sstevel@tonic-gate 	}
44707c478bd9Sstevel@tonic-gate 	/* 'for' loop 1b: */
4471265f37a1S 	for (app = fp->u.a.f_address->h_addr_list;
4472265f37a1S 	    (aptr = (uint8_t *)*app) != NULL; app++) {
44737c478bd9Sstevel@tonic-gate 		/* LINTED: (note 1) */
44747c478bd9Sstevel@tonic-gate 		if (IN6_IS_ADDR_V4MAPPED((in6_addr_t *)aptr))
44757c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1b */
44767c478bd9Sstevel@tonic-gate 		ucp = addr->s6_addr;
44777c478bd9Sstevel@tonic-gate 		for (i = masklen; i >= 8; i -= 8)
44787c478bd9Sstevel@tonic-gate 			if (*ucp++ != *aptr++)
44797c478bd9Sstevel@tonic-gate 				break; /* 'for' loop 1b */
44807c478bd9Sstevel@tonic-gate 		if (i == 0 ||
44817c478bd9Sstevel@tonic-gate 		    (i < 8 && ((*ucp ^ *aptr) & ~(0xff >> i)) == 0))
44827c478bd9Sstevel@tonic-gate 			return (B_TRUE);
44837c478bd9Sstevel@tonic-gate 	} /* 'for' loop 1b ends */
44847c478bd9Sstevel@tonic-gate 	return (B_FALSE);
44857c478bd9Sstevel@tonic-gate }
44867c478bd9Sstevel@tonic-gate 
44877c478bd9Sstevel@tonic-gate /*
44887c478bd9Sstevel@tonic-gate  * Run through the filter list for an IPv6 MIB2 IRE.  For a given
44897c478bd9Sstevel@tonic-gate  * type, if there's at least one filter and all filters of that type
44907c478bd9Sstevel@tonic-gate  * fail to match, then the route doesn't match and isn't displayed.
44917c478bd9Sstevel@tonic-gate  * If at least one matches, or none are specified, for each of the
44927c478bd9Sstevel@tonic-gate  * types, then the route is selected and displayed.
44937c478bd9Sstevel@tonic-gate  */
44947c478bd9Sstevel@tonic-gate static boolean_t
449545916cd2Sjpk ire_filter_match_v6(const mib2_ipv6RouteEntry_t *rp6, uint_t flag_b)
44967c478bd9Sstevel@tonic-gate {
44977c478bd9Sstevel@tonic-gate 	filter_t *fp;
44987c478bd9Sstevel@tonic-gate 	int idx;
44997c478bd9Sstevel@tonic-gate 
45007c478bd9Sstevel@tonic-gate 	/* 'for' loop 1: */
45017c478bd9Sstevel@tonic-gate 	for (idx = 0; idx < NFILTERKEYS; idx++)
45027c478bd9Sstevel@tonic-gate 		if ((fp = filters[idx]) != NULL) {
45037c478bd9Sstevel@tonic-gate 			/* 'for' loop 2: */
45047c478bd9Sstevel@tonic-gate 			for (; fp != NULL; fp = fp->f_next) {
45057c478bd9Sstevel@tonic-gate 				switch (idx) {
45067c478bd9Sstevel@tonic-gate 				case FK_AF:
45077c478bd9Sstevel@tonic-gate 					if (fp->u.f_family != AF_INET6)
45087c478bd9Sstevel@tonic-gate 						/* 'for' loop 2 */
45097c478bd9Sstevel@tonic-gate 						continue;
45107c478bd9Sstevel@tonic-gate 					break;
45117c478bd9Sstevel@tonic-gate 				case FK_OUTIF:
45127c478bd9Sstevel@tonic-gate 					if (!dev_name_match(&rp6->
45137c478bd9Sstevel@tonic-gate 					    ipv6RouteIfIndex, fp->u.f_ifname))
45147c478bd9Sstevel@tonic-gate 						/* 'for' loop 2 */
45157c478bd9Sstevel@tonic-gate 						continue;
45167c478bd9Sstevel@tonic-gate 					break;
45177c478bd9Sstevel@tonic-gate 				case FK_DST:
45187c478bd9Sstevel@tonic-gate 					if (!v6_addr_match(&rp6->ipv6RouteDest,
45197c478bd9Sstevel@tonic-gate 					    rp6->ipv6RoutePfxLength, fp))
45207c478bd9Sstevel@tonic-gate 						/* 'for' loop 2 */
45217c478bd9Sstevel@tonic-gate 						continue;
45227c478bd9Sstevel@tonic-gate 					break;
45237c478bd9Sstevel@tonic-gate 				case FK_FLAGS:
45247c478bd9Sstevel@tonic-gate 					if ((flag_b & fp->u.f.f_flagset) !=
45257c478bd9Sstevel@tonic-gate 					    fp->u.f.f_flagset ||
45267c478bd9Sstevel@tonic-gate 					    (flag_b & fp->u.f.f_flagclear))
45277c478bd9Sstevel@tonic-gate 						/* 'for' loop 2 */
45287c478bd9Sstevel@tonic-gate 						continue;
45297c478bd9Sstevel@tonic-gate 					break;
45307c478bd9Sstevel@tonic-gate 				}
45317c478bd9Sstevel@tonic-gate 				break;
45327c478bd9Sstevel@tonic-gate 			} /* 'for' loop 2 ends */
45337c478bd9Sstevel@tonic-gate 			if (fp == NULL)
45347c478bd9Sstevel@tonic-gate 				return (B_FALSE);
45357c478bd9Sstevel@tonic-gate 		}
45367c478bd9Sstevel@tonic-gate 	/* 'for' loop 1 ends */
45377c478bd9Sstevel@tonic-gate 	return (B_TRUE);
45387c478bd9Sstevel@tonic-gate }
45397c478bd9Sstevel@tonic-gate 
4540bd670b35SErik Nordmark /*
4541bd670b35SErik Nordmark  * Given an IPv6 MIB2 route entry, form the list of flags for the
4542bd670b35SErik Nordmark  * route.
4543bd670b35SErik Nordmark  */
4544bd670b35SErik Nordmark static uint_t
4545bd670b35SErik Nordmark form_v6_route_flags(const mib2_ipv6RouteEntry_t *rp6, char *flags)
45467c478bd9Sstevel@tonic-gate {
4547bd670b35SErik Nordmark 	uint_t flag_b;
45487c478bd9Sstevel@tonic-gate 
45497c478bd9Sstevel@tonic-gate 	flag_b = FLF_U;
45507c478bd9Sstevel@tonic-gate 	(void) strcpy(flags, "U");
4551bd670b35SErik Nordmark 	/* RTF_INDIRECT wins over RTF_GATEWAY - don't display both */
4552bd670b35SErik Nordmark 	if (rp6->ipv6RouteInfo.re_flags & RTF_INDIRECT) {
4553bd670b35SErik Nordmark 		(void) strcat(flags, "I");
4554bd670b35SErik Nordmark 		flag_b |= FLF_I;
4555bd670b35SErik Nordmark 	} else if (rp6->ipv6RouteInfo.re_ire_type & IRE_OFFLINK) {
45567c478bd9Sstevel@tonic-gate 		(void) strcat(flags, "G");
45577c478bd9Sstevel@tonic-gate 		flag_b |= FLF_G;
45587c478bd9Sstevel@tonic-gate 	}
45597c478bd9Sstevel@tonic-gate 
4560bd670b35SErik Nordmark 	/* IRE_IF_CLONE wins over RTF_HOST - don't display both */
4561bd670b35SErik Nordmark 	if (rp6->ipv6RouteInfo.re_ire_type & IRE_IF_CLONE) {
4562bd670b35SErik Nordmark 		(void) strcat(flags, "C");
4563bd670b35SErik Nordmark 		flag_b |= FLF_C;
4564bd670b35SErik Nordmark 	} else if (rp6->ipv6RoutePfxLength == IPV6_ABITS) {
45657c478bd9Sstevel@tonic-gate 		(void) strcat(flags, "H");
45667c478bd9Sstevel@tonic-gate 		flag_b |= FLF_H;
45677c478bd9Sstevel@tonic-gate 	}
45687c478bd9Sstevel@tonic-gate 
4569bd670b35SErik Nordmark 	if (rp6->ipv6RouteInfo.re_flags & RTF_DYNAMIC) {
45707c478bd9Sstevel@tonic-gate 		(void) strcat(flags, "D");
45717c478bd9Sstevel@tonic-gate 		flag_b |= FLF_D;
45727c478bd9Sstevel@tonic-gate 	}
45737c478bd9Sstevel@tonic-gate 	if (rp6->ipv6RouteInfo.re_ire_type == IRE_LOCAL) {	/* Local */
45747c478bd9Sstevel@tonic-gate 		(void) strcat(flags, "L");
45757c478bd9Sstevel@tonic-gate 		flag_b |= FLF_L;
45767c478bd9Sstevel@tonic-gate 	}
45777c478bd9Sstevel@tonic-gate 	if (rp6->ipv6RouteInfo.re_flags & RTF_MULTIRT) {
45787c478bd9Sstevel@tonic-gate 		(void) strcat(flags, "M");			/* Multiroute */
45797c478bd9Sstevel@tonic-gate 		flag_b |= FLF_M;
45807c478bd9Sstevel@tonic-gate 	}
45817c478bd9Sstevel@tonic-gate 	if (rp6->ipv6RouteInfo.re_flags & RTF_SETSRC) {
45827c478bd9Sstevel@tonic-gate 		(void) strcat(flags, "S");			/* Setsrc */
45837c478bd9Sstevel@tonic-gate 		flag_b |= FLF_S;
45847c478bd9Sstevel@tonic-gate 	}
4585bd670b35SErik Nordmark 	if (rp6->ipv6RouteInfo.re_flags & RTF_REJECT) {
4586bd670b35SErik Nordmark 		(void) strcat(flags, "R");
4587bd670b35SErik Nordmark 		flag_b |= FLF_R;
4588bd670b35SErik Nordmark 	}
4589bd670b35SErik Nordmark 	if (rp6->ipv6RouteInfo.re_flags & RTF_BLACKHOLE) {
4590bd670b35SErik Nordmark 		(void) strcat(flags, "B");
4591bd670b35SErik Nordmark 		flag_b |= FLF_B;
4592bd670b35SErik Nordmark 	}
4593*550b6e40SSowmini Varadhan 	if (rp6->ipv6RouteInfo.re_flags & RTF_ZONE) {
4594*550b6e40SSowmini Varadhan 		(void) strcat(flags, "Z");
4595*550b6e40SSowmini Varadhan 		flag_b |= FLF_Z;
4596*550b6e40SSowmini Varadhan 	}
4597bd670b35SErik Nordmark 	return (flag_b);
4598bd670b35SErik Nordmark }
4599bd670b35SErik Nordmark 
4600bd670b35SErik Nordmark static const char ire_hdr_v6[] =
4601bd670b35SErik Nordmark "\n%s Table: IPv6\n";
4602bd670b35SErik Nordmark static const char ire_hdr_v6_verbose[] =
4603bd670b35SErik Nordmark "  Destination/Mask            Gateway                    If    MTU  "
4604bd670b35SErik Nordmark "Ref Flags  Out   In/Fwd %s\n"
4605bd670b35SErik Nordmark "--------------------------- --------------------------- ----- ----- "
4606bd670b35SErik Nordmark "--- ----- ------ ------ %s\n";
4607bd670b35SErik Nordmark static const char ire_hdr_v6_normal[] =
4608bd670b35SErik Nordmark "  Destination/Mask            Gateway                   Flags Ref   Use  "
4609bd670b35SErik Nordmark "  If   %s\n"
4610bd670b35SErik Nordmark "--------------------------- --------------------------- ----- --- ------- "
4611bd670b35SErik Nordmark "----- %s\n";
4612bd670b35SErik Nordmark 
4613bd670b35SErik Nordmark static boolean_t
4614bd670b35SErik Nordmark ire_report_item_v6(const mib2_ipv6RouteEntry_t *rp6, boolean_t first,
4615bd670b35SErik Nordmark     const sec_attr_list_t *attrs)
4616bd670b35SErik Nordmark {
4617bd670b35SErik Nordmark 	char			dstbuf[MAXHOSTNAMELEN + 1];
4618bd670b35SErik Nordmark 	char			gwbuf[MAXHOSTNAMELEN + 1];
4619bd670b35SErik Nordmark 	char			ifname[LIFNAMSIZ + 1];
4620bd670b35SErik Nordmark 	char			flags[10];	/* RTF_ flags */
4621bd670b35SErik Nordmark 	uint_t			flag_b;
4622bd670b35SErik Nordmark 
4623bd670b35SErik Nordmark 	if (!(Aflag || (rp6->ipv6RouteInfo.re_ire_type != IRE_IF_CLONE &&
4624bd670b35SErik Nordmark 	    rp6->ipv6RouteInfo.re_ire_type != IRE_MULTICAST &&
4625bd670b35SErik Nordmark 	    rp6->ipv6RouteInfo.re_ire_type != IRE_NOROUTE &&
4626bd670b35SErik Nordmark 	    rp6->ipv6RouteInfo.re_ire_type != IRE_LOCAL))) {
4627bd670b35SErik Nordmark 		return (first);
4628bd670b35SErik Nordmark 	}
4629bd670b35SErik Nordmark 
4630bd670b35SErik Nordmark 	flag_b = form_v6_route_flags(rp6, flags);
46317c478bd9Sstevel@tonic-gate 
46327c478bd9Sstevel@tonic-gate 	if (!ire_filter_match_v6(rp6, flag_b))
46337c478bd9Sstevel@tonic-gate 		return (first);
46347c478bd9Sstevel@tonic-gate 
46357c478bd9Sstevel@tonic-gate 	if (first) {
463645916cd2Sjpk 		(void) printf(ire_hdr_v6, Vflag ? "IRE" : "Routing");
463745916cd2Sjpk 		(void) printf(Vflag ? ire_hdr_v6_verbose : ire_hdr_v6_normal,
463845916cd2Sjpk 		    RSECflag ? "  Gateway security attributes  " : "",
463945916cd2Sjpk 		    RSECflag ? "-------------------------------" : "");
46407c478bd9Sstevel@tonic-gate 		first = B_FALSE;
46417c478bd9Sstevel@tonic-gate 	}
46427c478bd9Sstevel@tonic-gate 
46437c478bd9Sstevel@tonic-gate 	if (Vflag) {
4644bd670b35SErik Nordmark 		(void) printf("%-27s %-27s %-5s %5u %3u "
464545916cd2Sjpk 		    "%-5s %6u %6u %s\n",
46467c478bd9Sstevel@tonic-gate 		    pr_prefix6(&rp6->ipv6RouteDest,
4647e11c3f44Smeem 		    rp6->ipv6RoutePfxLength, dstbuf, sizeof (dstbuf)),
46487c478bd9Sstevel@tonic-gate 		    IN6_IS_ADDR_UNSPECIFIED(&rp6->ipv6RouteNextHop) ?
46497c478bd9Sstevel@tonic-gate 		    "    --" :
46507c478bd9Sstevel@tonic-gate 		    pr_addr6(&rp6->ipv6RouteNextHop, gwbuf, sizeof (gwbuf)),
46517c478bd9Sstevel@tonic-gate 		    octetstr(&rp6->ipv6RouteIfIndex, 'a',
46527c478bd9Sstevel@tonic-gate 		    ifname, sizeof (ifname)),
46537c478bd9Sstevel@tonic-gate 		    rp6->ipv6RouteInfo.re_max_frag,
46547c478bd9Sstevel@tonic-gate 		    rp6->ipv6RouteInfo.re_ref,
46557c478bd9Sstevel@tonic-gate 		    flags,
46567c478bd9Sstevel@tonic-gate 		    rp6->ipv6RouteInfo.re_obpkt,
465745916cd2Sjpk 		    rp6->ipv6RouteInfo.re_ibpkt,
465845916cd2Sjpk 		    pr_secattr(attrs));
46597c478bd9Sstevel@tonic-gate 	} else {
4660aecc8c24Sja 		(void) printf("%-27s %-27s %-5s %3u %7u %-5s %s\n",
46617c478bd9Sstevel@tonic-gate 		    pr_prefix6(&rp6->ipv6RouteDest,
4662e11c3f44Smeem 		    rp6->ipv6RoutePfxLength, dstbuf, sizeof (dstbuf)),
46637c478bd9Sstevel@tonic-gate 		    IN6_IS_ADDR_UNSPECIFIED(&rp6->ipv6RouteNextHop) ?
46647c478bd9Sstevel@tonic-gate 		    "    --" :
46657c478bd9Sstevel@tonic-gate 		    pr_addr6(&rp6->ipv6RouteNextHop, gwbuf, sizeof (gwbuf)),
46667c478bd9Sstevel@tonic-gate 		    flags,
46677c478bd9Sstevel@tonic-gate 		    rp6->ipv6RouteInfo.re_ref,
46687c478bd9Sstevel@tonic-gate 		    rp6->ipv6RouteInfo.re_obpkt + rp6->ipv6RouteInfo.re_ibpkt,
46697c478bd9Sstevel@tonic-gate 		    octetstr(&rp6->ipv6RouteIfIndex, 'a',
467045916cd2Sjpk 		    ifname, sizeof (ifname)),
467145916cd2Sjpk 		    pr_secattr(attrs));
46727c478bd9Sstevel@tonic-gate 	}
46737c478bd9Sstevel@tonic-gate 	return (first);
46747c478bd9Sstevel@tonic-gate }
46757c478bd9Sstevel@tonic-gate 
467645916cd2Sjpk /*
467745916cd2Sjpk  * Common attribute-gathering routine for all transports.
467845916cd2Sjpk  */
467945916cd2Sjpk static mib2_transportMLPEntry_t **
468045916cd2Sjpk gather_attrs(const mib_item_t *item, int group, int mib_id, int esize)
468145916cd2Sjpk {
468245916cd2Sjpk 	int transport_count = 0;
468345916cd2Sjpk 	const mib_item_t *iptr;
468445916cd2Sjpk 	mib2_transportMLPEntry_t **attrs, *tme;
468545916cd2Sjpk 
468645916cd2Sjpk 	for (iptr = item; iptr != NULL; iptr = iptr->next_item) {
468745916cd2Sjpk 		if (iptr->group == group && iptr->mib_id == mib_id)
468845916cd2Sjpk 			transport_count += iptr->length / esize;
468945916cd2Sjpk 	}
469045916cd2Sjpk 	if (transport_count <= 0)
469145916cd2Sjpk 		return (NULL);
469245916cd2Sjpk 	attrs = calloc(transport_count, sizeof (*attrs));
469345916cd2Sjpk 	if (attrs == NULL) {
469445916cd2Sjpk 		perror("gather_attrs calloc failed");
469545916cd2Sjpk 		return (NULL);
469645916cd2Sjpk 	}
469745916cd2Sjpk 	for (iptr = item; iptr != NULL; iptr = iptr->next_item) {
469845916cd2Sjpk 		if (iptr->group == group && iptr->mib_id == EXPER_XPORT_MLP) {
469945916cd2Sjpk 			for (tme = iptr->valp;
470045916cd2Sjpk 			    (char *)tme < (char *)iptr->valp + iptr->length;
470145916cd2Sjpk 			    /* LINTED: (note 1) */
470245916cd2Sjpk 			    tme = (mib2_transportMLPEntry_t *)((char *)tme +
470345916cd2Sjpk 			    transportMLPSize)) {
470445916cd2Sjpk 				attrs[tme->tme_connidx] = tme;
470545916cd2Sjpk 			}
470645916cd2Sjpk 		}
470745916cd2Sjpk 	}
470845916cd2Sjpk 	return (attrs);
470945916cd2Sjpk }
471045916cd2Sjpk 
471145916cd2Sjpk static void
471245916cd2Sjpk print_transport_label(const mib2_transportMLPEntry_t *attr)
471345916cd2Sjpk {
47145f9878b0Sken Powell - Sun Microsystem 	if (!RSECflag || attr == NULL ||
47155f9878b0Sken Powell - Sun Microsystem 	    !(attr->tme_flags & MIB2_TMEF_IS_LABELED))
471645916cd2Sjpk 		return;
471745916cd2Sjpk 
47187b0bedd4SRic Aleshire 	if (bisinvalid(&attr->tme_label)) {
471945916cd2Sjpk 		(void) printf("   INVALID\n");
47207b0bedd4SRic Aleshire 	} else if (!blequal(&attr->tme_label, zone_security_label)) {
47217b0bedd4SRic Aleshire 		char *sl_str;
47227b0bedd4SRic Aleshire 
47237b0bedd4SRic Aleshire 		sl_str = sl_to_str(&attr->tme_label);
47247b0bedd4SRic Aleshire 		(void) printf("   %s\n", sl_str);
47257b0bedd4SRic Aleshire 		free(sl_str);
47267b0bedd4SRic Aleshire 	}
472745916cd2Sjpk }
472845916cd2Sjpk 
47297c478bd9Sstevel@tonic-gate /* ------------------------------ TCP_REPORT------------------------------- */
47307c478bd9Sstevel@tonic-gate 
47317c478bd9Sstevel@tonic-gate static const char tcp_hdr_v4[] =
47327c478bd9Sstevel@tonic-gate "\nTCP: IPv4\n";
47337c478bd9Sstevel@tonic-gate static const char tcp_hdr_v4_compat[] =
47347c478bd9Sstevel@tonic-gate "\nTCP\n";
47357c478bd9Sstevel@tonic-gate static const char tcp_hdr_v4_verbose[] =
47367c478bd9Sstevel@tonic-gate "Local/Remote Address Swind  Snext     Suna   Rwind  Rnext     Rack   "
473745916cd2Sjpk " Rto   Mss     State\n"
47387c478bd9Sstevel@tonic-gate "-------------------- ----- -------- -------- ----- -------- -------- "
473945916cd2Sjpk "----- ----- -----------\n";
47407c478bd9Sstevel@tonic-gate static const char tcp_hdr_v4_normal[] =
474145916cd2Sjpk "   Local Address        Remote Address    Swind Send-Q Rwind Recv-Q "
474245916cd2Sjpk "   State\n"
474345916cd2Sjpk "-------------------- -------------------- ----- ------ ----- ------ "
474445916cd2Sjpk "-----------\n";
47457c478bd9Sstevel@tonic-gate 
47467c478bd9Sstevel@tonic-gate static const char tcp_hdr_v6[] =
47477c478bd9Sstevel@tonic-gate "\nTCP: IPv6\n";
47487c478bd9Sstevel@tonic-gate static const char tcp_hdr_v6_verbose[] =
47497c478bd9Sstevel@tonic-gate "Local/Remote Address              Swind  Snext     Suna   Rwind  Rnext   "
475045916cd2Sjpk "  Rack    Rto   Mss    State      If\n"
47517c478bd9Sstevel@tonic-gate "--------------------------------- ----- -------- -------- ----- -------- "
47527c478bd9Sstevel@tonic-gate "-------- ----- ----- ----------- -----\n";
47537c478bd9Sstevel@tonic-gate static const char tcp_hdr_v6_normal[] =
47547c478bd9Sstevel@tonic-gate "   Local Address                     Remote Address                 "
475545916cd2Sjpk "Swind Send-Q Rwind Recv-Q   State      If\n"
47567c478bd9Sstevel@tonic-gate "--------------------------------- --------------------------------- "
47577c478bd9Sstevel@tonic-gate "----- ------ ----- ------ ----------- -----\n";
47587c478bd9Sstevel@tonic-gate 
475945916cd2Sjpk static boolean_t tcp_report_item_v4(const mib2_tcpConnEntry_t *,
476045916cd2Sjpk     boolean_t first, const mib2_transportMLPEntry_t *);
476145916cd2Sjpk static boolean_t tcp_report_item_v6(const mib2_tcp6ConnEntry_t *,
476245916cd2Sjpk     boolean_t first, const mib2_transportMLPEntry_t *);
47637c478bd9Sstevel@tonic-gate 
47647c478bd9Sstevel@tonic-gate static void
476545916cd2Sjpk tcp_report(const mib_item_t *item)
47667c478bd9Sstevel@tonic-gate {
47677c478bd9Sstevel@tonic-gate 	int			jtemp = 0;
47687c478bd9Sstevel@tonic-gate 	boolean_t		print_hdr_once_v4 = B_TRUE;
47697c478bd9Sstevel@tonic-gate 	boolean_t		print_hdr_once_v6 = B_TRUE;
47707c478bd9Sstevel@tonic-gate 	mib2_tcpConnEntry_t	*tp;
47717c478bd9Sstevel@tonic-gate 	mib2_tcp6ConnEntry_t	*tp6;
477245916cd2Sjpk 	mib2_transportMLPEntry_t **v4_attrs, **v6_attrs;
477345916cd2Sjpk 	mib2_transportMLPEntry_t **v4a, **v6a;
477445916cd2Sjpk 	mib2_transportMLPEntry_t *aptr;
47757c478bd9Sstevel@tonic-gate 
47767c478bd9Sstevel@tonic-gate 	if (!protocol_selected(IPPROTO_TCP))
47777c478bd9Sstevel@tonic-gate 		return;
47787c478bd9Sstevel@tonic-gate 
477945916cd2Sjpk 	/*
478045916cd2Sjpk 	 * Preparation pass: the kernel returns separate entries for TCP
478145916cd2Sjpk 	 * connection table entries and Multilevel Port attributes.  We loop
478245916cd2Sjpk 	 * through the attributes first and set up an array for each address
478345916cd2Sjpk 	 * family.
478445916cd2Sjpk 	 */
478545916cd2Sjpk 	v4_attrs = family_selected(AF_INET) && RSECflag ?
478645916cd2Sjpk 	    gather_attrs(item, MIB2_TCP, MIB2_TCP_CONN, tcpConnEntrySize) :
478745916cd2Sjpk 	    NULL;
478845916cd2Sjpk 	v6_attrs = family_selected(AF_INET6) && RSECflag ?
478945916cd2Sjpk 	    gather_attrs(item, MIB2_TCP6, MIB2_TCP6_CONN, tcp6ConnEntrySize) :
479045916cd2Sjpk 	    NULL;
479145916cd2Sjpk 
47927c478bd9Sstevel@tonic-gate 	/* 'for' loop 1: */
479345916cd2Sjpk 	v4a = v4_attrs;
479445916cd2Sjpk 	v6a = v6_attrs;
479545916cd2Sjpk 	for (; item != NULL; item = item->next_item) {
4796bd670b35SErik Nordmark 		if (Xflag) {
47977c478bd9Sstevel@tonic-gate 			(void) printf("\n--- Entry %d ---\n", ++jtemp);
47987c478bd9Sstevel@tonic-gate 			(void) printf("Group = %d, mib_id = %d, "
47997c478bd9Sstevel@tonic-gate 			    "length = %d, valp = 0x%p\n",
48007c478bd9Sstevel@tonic-gate 			    item->group, item->mib_id,
48017c478bd9Sstevel@tonic-gate 			    item->length, item->valp);
48027c478bd9Sstevel@tonic-gate 		}
48037c478bd9Sstevel@tonic-gate 
48047c478bd9Sstevel@tonic-gate 		if (!((item->group == MIB2_TCP &&
48057c478bd9Sstevel@tonic-gate 		    item->mib_id == MIB2_TCP_CONN) ||
48067c478bd9Sstevel@tonic-gate 		    (item->group == MIB2_TCP6 &&
48077c478bd9Sstevel@tonic-gate 		    item->mib_id == MIB2_TCP6_CONN)))
48087c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
48097c478bd9Sstevel@tonic-gate 
48107c478bd9Sstevel@tonic-gate 		if (item->group == MIB2_TCP && !family_selected(AF_INET))
48117c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
48127c478bd9Sstevel@tonic-gate 		else if (item->group == MIB2_TCP6 && !family_selected(AF_INET6))
48137c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
48147c478bd9Sstevel@tonic-gate 
48157c478bd9Sstevel@tonic-gate 		if (item->group == MIB2_TCP) {
48167c478bd9Sstevel@tonic-gate 			for (tp = (mib2_tcpConnEntry_t *)item->valp;
48177c478bd9Sstevel@tonic-gate 			    (char *)tp < (char *)item->valp + item->length;
48187c478bd9Sstevel@tonic-gate 			    /* LINTED: (note 1) */
48197c478bd9Sstevel@tonic-gate 			    tp = (mib2_tcpConnEntry_t *)((char *)tp +
48207c478bd9Sstevel@tonic-gate 			    tcpConnEntrySize)) {
482145916cd2Sjpk 				aptr = v4a == NULL ? NULL : *v4a++;
48227c478bd9Sstevel@tonic-gate 				print_hdr_once_v4 = tcp_report_item_v4(tp,
482345916cd2Sjpk 				    print_hdr_once_v4, aptr);
48247c478bd9Sstevel@tonic-gate 			}
48257c478bd9Sstevel@tonic-gate 		} else {
48267c478bd9Sstevel@tonic-gate 			for (tp6 = (mib2_tcp6ConnEntry_t *)item->valp;
48277c478bd9Sstevel@tonic-gate 			    (char *)tp6 < (char *)item->valp + item->length;
48287c478bd9Sstevel@tonic-gate 			    /* LINTED: (note 1) */
48297c478bd9Sstevel@tonic-gate 			    tp6 = (mib2_tcp6ConnEntry_t *)((char *)tp6 +
48307c478bd9Sstevel@tonic-gate 			    tcp6ConnEntrySize)) {
483145916cd2Sjpk 				aptr = v6a == NULL ? NULL : *v6a++;
48327c478bd9Sstevel@tonic-gate 				print_hdr_once_v6 = tcp_report_item_v6(tp6,
483345916cd2Sjpk 				    print_hdr_once_v6, aptr);
48347c478bd9Sstevel@tonic-gate 			}
48357c478bd9Sstevel@tonic-gate 		}
48367c478bd9Sstevel@tonic-gate 	} /* 'for' loop 1 ends */
48377c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
483845916cd2Sjpk 
483945916cd2Sjpk 	if (v4_attrs != NULL)
484045916cd2Sjpk 		free(v4_attrs);
484145916cd2Sjpk 	if (v6_attrs != NULL)
484245916cd2Sjpk 		free(v6_attrs);
48437c478bd9Sstevel@tonic-gate }
48447c478bd9Sstevel@tonic-gate 
48457c478bd9Sstevel@tonic-gate static boolean_t
484645916cd2Sjpk tcp_report_item_v4(const mib2_tcpConnEntry_t *tp, boolean_t first,
484745916cd2Sjpk     const mib2_transportMLPEntry_t *attr)
48487c478bd9Sstevel@tonic-gate {
48497c478bd9Sstevel@tonic-gate 	/*
48507c478bd9Sstevel@tonic-gate 	 * lname and fname below are for the hostname as well as the portname
48517c478bd9Sstevel@tonic-gate 	 * There is no limit on portname length so we assume MAXHOSTNAMELEN
48527c478bd9Sstevel@tonic-gate 	 * as the limit
48537c478bd9Sstevel@tonic-gate 	 */
48547c478bd9Sstevel@tonic-gate 	char	lname[MAXHOSTNAMELEN + MAXHOSTNAMELEN + 1];
48557c478bd9Sstevel@tonic-gate 	char	fname[MAXHOSTNAMELEN + MAXHOSTNAMELEN + 1];
48567c478bd9Sstevel@tonic-gate 
48577c478bd9Sstevel@tonic-gate 	if (!(Aflag || tp->tcpConnEntryInfo.ce_state >= TCPS_ESTABLISHED))
48587c478bd9Sstevel@tonic-gate 		return (first); /* Nothing to print */
48597c478bd9Sstevel@tonic-gate 
48607c478bd9Sstevel@tonic-gate 	if (first) {
486145916cd2Sjpk 		(void) printf(v4compat ? tcp_hdr_v4_compat : tcp_hdr_v4);
486245916cd2Sjpk 		(void) printf(Vflag ? tcp_hdr_v4_verbose : tcp_hdr_v4_normal);
48637c478bd9Sstevel@tonic-gate 	}
48647c478bd9Sstevel@tonic-gate 
48657c478bd9Sstevel@tonic-gate 	if (Vflag) {
48667c478bd9Sstevel@tonic-gate 		(void) printf("%-20s\n%-20s %5u %08x %08x %5u %08x %08x "
48677c478bd9Sstevel@tonic-gate 		    "%5u %5u %s\n",
48687c478bd9Sstevel@tonic-gate 		    pr_ap(tp->tcpConnLocalAddress,
4869e11c3f44Smeem 		    tp->tcpConnLocalPort, "tcp", lname, sizeof (lname)),
48707c478bd9Sstevel@tonic-gate 		    pr_ap(tp->tcpConnRemAddress,
4871e11c3f44Smeem 		    tp->tcpConnRemPort, "tcp", fname, sizeof (fname)),
48727c478bd9Sstevel@tonic-gate 		    tp->tcpConnEntryInfo.ce_swnd,
48737c478bd9Sstevel@tonic-gate 		    tp->tcpConnEntryInfo.ce_snxt,
48747c478bd9Sstevel@tonic-gate 		    tp->tcpConnEntryInfo.ce_suna,
48757c478bd9Sstevel@tonic-gate 		    tp->tcpConnEntryInfo.ce_rwnd,
48767c478bd9Sstevel@tonic-gate 		    tp->tcpConnEntryInfo.ce_rnxt,
48777c478bd9Sstevel@tonic-gate 		    tp->tcpConnEntryInfo.ce_rack,
48787c478bd9Sstevel@tonic-gate 		    tp->tcpConnEntryInfo.ce_rto,
48797c478bd9Sstevel@tonic-gate 		    tp->tcpConnEntryInfo.ce_mss,
488045916cd2Sjpk 		    mitcp_state(tp->tcpConnEntryInfo.ce_state, attr));
48817c478bd9Sstevel@tonic-gate 	} else {
48827c478bd9Sstevel@tonic-gate 		int sq = (int)tp->tcpConnEntryInfo.ce_snxt -
48837c478bd9Sstevel@tonic-gate 		    (int)tp->tcpConnEntryInfo.ce_suna - 1;
48847c478bd9Sstevel@tonic-gate 		int rq = (int)tp->tcpConnEntryInfo.ce_rnxt -
48857c478bd9Sstevel@tonic-gate 		    (int)tp->tcpConnEntryInfo.ce_rack;
48867c478bd9Sstevel@tonic-gate 
48877c478bd9Sstevel@tonic-gate 		(void) printf("%-20s %-20s %5u %6d %5u %6d %s\n",
48887c478bd9Sstevel@tonic-gate 		    pr_ap(tp->tcpConnLocalAddress,
4889e11c3f44Smeem 		    tp->tcpConnLocalPort, "tcp", lname, sizeof (lname)),
48907c478bd9Sstevel@tonic-gate 		    pr_ap(tp->tcpConnRemAddress,
4891e11c3f44Smeem 		    tp->tcpConnRemPort, "tcp", fname, sizeof (fname)),
48927c478bd9Sstevel@tonic-gate 		    tp->tcpConnEntryInfo.ce_swnd,
48937c478bd9Sstevel@tonic-gate 		    (sq >= 0) ? sq : 0,
48947c478bd9Sstevel@tonic-gate 		    tp->tcpConnEntryInfo.ce_rwnd,
48957c478bd9Sstevel@tonic-gate 		    (rq >= 0) ? rq : 0,
489645916cd2Sjpk 		    mitcp_state(tp->tcpConnEntryInfo.ce_state, attr));
48977c478bd9Sstevel@tonic-gate 	}
489845916cd2Sjpk 
489945916cd2Sjpk 	print_transport_label(attr);
490045916cd2Sjpk 
490145916cd2Sjpk 	return (B_FALSE);
49027c478bd9Sstevel@tonic-gate }
49037c478bd9Sstevel@tonic-gate 
49047c478bd9Sstevel@tonic-gate static boolean_t
490545916cd2Sjpk tcp_report_item_v6(const mib2_tcp6ConnEntry_t *tp6, boolean_t first,
490645916cd2Sjpk     const mib2_transportMLPEntry_t *attr)
49077c478bd9Sstevel@tonic-gate {
49087c478bd9Sstevel@tonic-gate 	/*
49097c478bd9Sstevel@tonic-gate 	 * lname and fname below are for the hostname as well as the portname
49107c478bd9Sstevel@tonic-gate 	 * There is no limit on portname length so we assume MAXHOSTNAMELEN
49117c478bd9Sstevel@tonic-gate 	 * as the limit
49127c478bd9Sstevel@tonic-gate 	 */
49137c478bd9Sstevel@tonic-gate 	char	lname[MAXHOSTNAMELEN + MAXHOSTNAMELEN + 1];
49147c478bd9Sstevel@tonic-gate 	char	fname[MAXHOSTNAMELEN + MAXHOSTNAMELEN + 1];
49157c478bd9Sstevel@tonic-gate 	char	ifname[LIFNAMSIZ + 1];
49167c478bd9Sstevel@tonic-gate 	char	*ifnamep;
49177c478bd9Sstevel@tonic-gate 
49187c478bd9Sstevel@tonic-gate 	if (!(Aflag || tp6->tcp6ConnEntryInfo.ce_state >= TCPS_ESTABLISHED))
49197c478bd9Sstevel@tonic-gate 		return (first); /* Nothing to print */
49207c478bd9Sstevel@tonic-gate 
49217c478bd9Sstevel@tonic-gate 	if (first) {
492245916cd2Sjpk 		(void) printf(tcp_hdr_v6);
492345916cd2Sjpk 		(void) printf(Vflag ? tcp_hdr_v6_verbose : tcp_hdr_v6_normal);
49247c478bd9Sstevel@tonic-gate 	}
49257c478bd9Sstevel@tonic-gate 
49267c478bd9Sstevel@tonic-gate 	ifnamep = (tp6->tcp6ConnIfIndex != 0) ?
49277c478bd9Sstevel@tonic-gate 	    if_indextoname(tp6->tcp6ConnIfIndex, ifname) : NULL;
492845916cd2Sjpk 	if (ifnamep == NULL)
492945916cd2Sjpk 		ifnamep = "";
49307c478bd9Sstevel@tonic-gate 
49317c478bd9Sstevel@tonic-gate 	if (Vflag) {
49327c478bd9Sstevel@tonic-gate 		(void) printf("%-33s\n%-33s %5u %08x %08x %5u %08x %08x "
493345916cd2Sjpk 		    "%5u %5u %-11s %s\n",
49347c478bd9Sstevel@tonic-gate 		    pr_ap6(&tp6->tcp6ConnLocalAddress,
4935e11c3f44Smeem 		    tp6->tcp6ConnLocalPort, "tcp", lname, sizeof (lname)),
49367c478bd9Sstevel@tonic-gate 		    pr_ap6(&tp6->tcp6ConnRemAddress,
4937e11c3f44Smeem 		    tp6->tcp6ConnRemPort, "tcp", fname, sizeof (fname)),
49387c478bd9Sstevel@tonic-gate 		    tp6->tcp6ConnEntryInfo.ce_swnd,
49397c478bd9Sstevel@tonic-gate 		    tp6->tcp6ConnEntryInfo.ce_snxt,
49407c478bd9Sstevel@tonic-gate 		    tp6->tcp6ConnEntryInfo.ce_suna,
49417c478bd9Sstevel@tonic-gate 		    tp6->tcp6ConnEntryInfo.ce_rwnd,
49427c478bd9Sstevel@tonic-gate 		    tp6->tcp6ConnEntryInfo.ce_rnxt,
49437c478bd9Sstevel@tonic-gate 		    tp6->tcp6ConnEntryInfo.ce_rack,
49447c478bd9Sstevel@tonic-gate 		    tp6->tcp6ConnEntryInfo.ce_rto,
49457c478bd9Sstevel@tonic-gate 		    tp6->tcp6ConnEntryInfo.ce_mss,
494645916cd2Sjpk 		    mitcp_state(tp6->tcp6ConnEntryInfo.ce_state, attr),
494745916cd2Sjpk 		    ifnamep);
49487c478bd9Sstevel@tonic-gate 	} else {
49497c478bd9Sstevel@tonic-gate 		int sq = (int)tp6->tcp6ConnEntryInfo.ce_snxt -
49507c478bd9Sstevel@tonic-gate 		    (int)tp6->tcp6ConnEntryInfo.ce_suna - 1;
49517c478bd9Sstevel@tonic-gate 		int rq = (int)tp6->tcp6ConnEntryInfo.ce_rnxt -
49527c478bd9Sstevel@tonic-gate 		    (int)tp6->tcp6ConnEntryInfo.ce_rack;
49537c478bd9Sstevel@tonic-gate 
495445916cd2Sjpk 		(void) printf("%-33s %-33s %5u %6d %5u %6d %-11s %s\n",
49557c478bd9Sstevel@tonic-gate 		    pr_ap6(&tp6->tcp6ConnLocalAddress,
4956e11c3f44Smeem 		    tp6->tcp6ConnLocalPort, "tcp", lname, sizeof (lname)),
49577c478bd9Sstevel@tonic-gate 		    pr_ap6(&tp6->tcp6ConnRemAddress,
4958e11c3f44Smeem 		    tp6->tcp6ConnRemPort, "tcp", fname, sizeof (fname)),
49597c478bd9Sstevel@tonic-gate 		    tp6->tcp6ConnEntryInfo.ce_swnd,
49607c478bd9Sstevel@tonic-gate 		    (sq >= 0) ? sq : 0,
49617c478bd9Sstevel@tonic-gate 		    tp6->tcp6ConnEntryInfo.ce_rwnd,
49627c478bd9Sstevel@tonic-gate 		    (rq >= 0) ? rq : 0,
496345916cd2Sjpk 		    mitcp_state(tp6->tcp6ConnEntryInfo.ce_state, attr),
496445916cd2Sjpk 		    ifnamep);
49657c478bd9Sstevel@tonic-gate 	}
496645916cd2Sjpk 
496745916cd2Sjpk 	print_transport_label(attr);
496845916cd2Sjpk 
496945916cd2Sjpk 	return (B_FALSE);
49707c478bd9Sstevel@tonic-gate }
49717c478bd9Sstevel@tonic-gate 
49727c478bd9Sstevel@tonic-gate /* ------------------------------- UDP_REPORT------------------------------- */
49737c478bd9Sstevel@tonic-gate 
497445916cd2Sjpk static boolean_t udp_report_item_v4(const mib2_udpEntry_t *ude,
497545916cd2Sjpk     boolean_t first, const mib2_transportMLPEntry_t *attr);
497645916cd2Sjpk static boolean_t udp_report_item_v6(const mib2_udp6Entry_t *ude6,
497745916cd2Sjpk     boolean_t first, const mib2_transportMLPEntry_t *attr);
497845916cd2Sjpk 
497945916cd2Sjpk static const char udp_hdr_v4[] =
498045916cd2Sjpk "   Local Address        Remote Address      State\n"
498145916cd2Sjpk "-------------------- -------------------- ----------\n";
49827c478bd9Sstevel@tonic-gate 
498345916cd2Sjpk static const char udp_hdr_v6[] =
49847c478bd9Sstevel@tonic-gate "   Local Address                     Remote Address                 "
498545916cd2Sjpk "  State      If\n"
49867c478bd9Sstevel@tonic-gate "--------------------------------- --------------------------------- "
49877c478bd9Sstevel@tonic-gate "---------- -----\n";
49887c478bd9Sstevel@tonic-gate 
49897c478bd9Sstevel@tonic-gate static void
499045916cd2Sjpk udp_report(const mib_item_t *item)
49917c478bd9Sstevel@tonic-gate {
49927c478bd9Sstevel@tonic-gate 	int			jtemp = 0;
49937c478bd9Sstevel@tonic-gate 	boolean_t		print_hdr_once_v4 = B_TRUE;
49947c478bd9Sstevel@tonic-gate 	boolean_t		print_hdr_once_v6 = B_TRUE;
49957c478bd9Sstevel@tonic-gate 	mib2_udpEntry_t		*ude;
49967c478bd9Sstevel@tonic-gate 	mib2_udp6Entry_t	*ude6;
499745916cd2Sjpk 	mib2_transportMLPEntry_t **v4_attrs, **v6_attrs;
499845916cd2Sjpk 	mib2_transportMLPEntry_t **v4a, **v6a;
499945916cd2Sjpk 	mib2_transportMLPEntry_t *aptr;
50007c478bd9Sstevel@tonic-gate 
50017c478bd9Sstevel@tonic-gate 	if (!protocol_selected(IPPROTO_UDP))
50027c478bd9Sstevel@tonic-gate 		return;
50037c478bd9Sstevel@tonic-gate 
500445916cd2Sjpk 	/*
500545916cd2Sjpk 	 * Preparation pass: the kernel returns separate entries for UDP
500645916cd2Sjpk 	 * connection table entries and Multilevel Port attributes.  We loop
500745916cd2Sjpk 	 * through the attributes first and set up an array for each address
500845916cd2Sjpk 	 * family.
500945916cd2Sjpk 	 */
501045916cd2Sjpk 	v4_attrs = family_selected(AF_INET) && RSECflag ?
501145916cd2Sjpk 	    gather_attrs(item, MIB2_UDP, MIB2_UDP_ENTRY, udpEntrySize) : NULL;
501245916cd2Sjpk 	v6_attrs = family_selected(AF_INET6) && RSECflag ?
501345916cd2Sjpk 	    gather_attrs(item, MIB2_UDP6, MIB2_UDP6_ENTRY, udp6EntrySize) :
501445916cd2Sjpk 	    NULL;
501545916cd2Sjpk 
501645916cd2Sjpk 	v4a = v4_attrs;
501745916cd2Sjpk 	v6a = v6_attrs;
50187c478bd9Sstevel@tonic-gate 	/* 'for' loop 1: */
50197c478bd9Sstevel@tonic-gate 	for (; item; item = item->next_item) {
5020bd670b35SErik Nordmark 		if (Xflag) {
50217c478bd9Sstevel@tonic-gate 			(void) printf("\n--- Entry %d ---\n", ++jtemp);
50227c478bd9Sstevel@tonic-gate 			(void) printf("Group = %d, mib_id = %d, "
50237c478bd9Sstevel@tonic-gate 			    "length = %d, valp = 0x%p\n",
50247c478bd9Sstevel@tonic-gate 			    item->group, item->mib_id,
50257c478bd9Sstevel@tonic-gate 			    item->length, item->valp);
50267c478bd9Sstevel@tonic-gate 		}
50277c478bd9Sstevel@tonic-gate 		if (!((item->group == MIB2_UDP &&
50287c478bd9Sstevel@tonic-gate 		    item->mib_id == MIB2_UDP_ENTRY) ||
50297c478bd9Sstevel@tonic-gate 		    (item->group == MIB2_UDP6 &&
50307c478bd9Sstevel@tonic-gate 		    item->mib_id == MIB2_UDP6_ENTRY)))
50317c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
50327c478bd9Sstevel@tonic-gate 
50337c478bd9Sstevel@tonic-gate 		if (item->group == MIB2_UDP && !family_selected(AF_INET))
50347c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
50357c478bd9Sstevel@tonic-gate 		else if (item->group == MIB2_UDP6 && !family_selected(AF_INET6))
50367c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
50377c478bd9Sstevel@tonic-gate 
50387c478bd9Sstevel@tonic-gate 		/*	xxx.xxx.xxx.xxx,pppp  sss... */
50397c478bd9Sstevel@tonic-gate 		if (item->group == MIB2_UDP) {
50407c478bd9Sstevel@tonic-gate 			for (ude = (mib2_udpEntry_t *)item->valp;
50417c478bd9Sstevel@tonic-gate 			    (char *)ude < (char *)item->valp + item->length;
50427c478bd9Sstevel@tonic-gate 			    /* LINTED: (note 1) */
50437c478bd9Sstevel@tonic-gate 			    ude = (mib2_udpEntry_t *)((char *)ude +
50447c478bd9Sstevel@tonic-gate 			    udpEntrySize)) {
504545916cd2Sjpk 				aptr = v4a == NULL ? NULL : *v4a++;
50467c478bd9Sstevel@tonic-gate 				print_hdr_once_v4 = udp_report_item_v4(ude,
504745916cd2Sjpk 				    print_hdr_once_v4, aptr);
50487c478bd9Sstevel@tonic-gate 			}
50497c478bd9Sstevel@tonic-gate 		} else {
50507c478bd9Sstevel@tonic-gate 			for (ude6 = (mib2_udp6Entry_t *)item->valp;
50517c478bd9Sstevel@tonic-gate 			    (char *)ude6 < (char *)item->valp + item->length;
50527c478bd9Sstevel@tonic-gate 			    /* LINTED: (note 1) */
50537c478bd9Sstevel@tonic-gate 			    ude6 = (mib2_udp6Entry_t *)((char *)ude6 +
50547c478bd9Sstevel@tonic-gate 			    udp6EntrySize)) {
505545916cd2Sjpk 				aptr = v6a == NULL ? NULL : *v6a++;
50567c478bd9Sstevel@tonic-gate 				print_hdr_once_v6 = udp_report_item_v6(ude6,
505745916cd2Sjpk 				    print_hdr_once_v6, aptr);
50587c478bd9Sstevel@tonic-gate 			}
50597c478bd9Sstevel@tonic-gate 		}
50607c478bd9Sstevel@tonic-gate 	} /* 'for' loop 1 ends */
50617c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
506245916cd2Sjpk 
506345916cd2Sjpk 	if (v4_attrs != NULL)
506445916cd2Sjpk 		free(v4_attrs);
506545916cd2Sjpk 	if (v6_attrs != NULL)
506645916cd2Sjpk 		free(v6_attrs);
50677c478bd9Sstevel@tonic-gate }
50687c478bd9Sstevel@tonic-gate 
50697c478bd9Sstevel@tonic-gate static boolean_t
507045916cd2Sjpk udp_report_item_v4(const mib2_udpEntry_t *ude, boolean_t first,
507145916cd2Sjpk     const mib2_transportMLPEntry_t *attr)
50727c478bd9Sstevel@tonic-gate {
50737c478bd9Sstevel@tonic-gate 	char	lname[MAXHOSTNAMELEN + MAXHOSTNAMELEN + 1];
50747c478bd9Sstevel@tonic-gate 			/* hostname + portname */
50757c478bd9Sstevel@tonic-gate 
50767c478bd9Sstevel@tonic-gate 	if (!(Aflag || ude->udpEntryInfo.ue_state >= MIB2_UDP_connected))
50777c478bd9Sstevel@tonic-gate 		return (first); /* Nothing to print */
50787c478bd9Sstevel@tonic-gate 
50797c478bd9Sstevel@tonic-gate 	if (first) {
508045916cd2Sjpk 		(void) printf(v4compat ? "\nUDP\n" : "\nUDP: IPv4\n");
508145916cd2Sjpk 		(void) printf(udp_hdr_v4);
50827c478bd9Sstevel@tonic-gate 		first = B_FALSE;
50837c478bd9Sstevel@tonic-gate 	}
50847c478bd9Sstevel@tonic-gate 
50857c478bd9Sstevel@tonic-gate 	(void) printf("%-20s ",
50867c478bd9Sstevel@tonic-gate 	    pr_ap(ude->udpLocalAddress, ude->udpLocalPort, "udp",
50877c478bd9Sstevel@tonic-gate 	    lname, sizeof (lname)));
508845916cd2Sjpk 	(void) printf("%-20s %s\n",
508945916cd2Sjpk 	    ude->udpEntryInfo.ue_state == MIB2_UDP_connected ?
509045916cd2Sjpk 	    pr_ap(ude->udpEntryInfo.ue_RemoteAddress,
509145916cd2Sjpk 	    ude->udpEntryInfo.ue_RemotePort, "udp", lname, sizeof (lname)) :
509245916cd2Sjpk 	    "",
509345916cd2Sjpk 	    miudp_state(ude->udpEntryInfo.ue_state, attr));
509445916cd2Sjpk 
5095bd670b35SErik Nordmark 	print_transport_label(attr);
509645916cd2Sjpk 
50977c478bd9Sstevel@tonic-gate 	return (first);
50987c478bd9Sstevel@tonic-gate }
50997c478bd9Sstevel@tonic-gate 
51007c478bd9Sstevel@tonic-gate static boolean_t
510145916cd2Sjpk udp_report_item_v6(const mib2_udp6Entry_t *ude6, boolean_t first,
510245916cd2Sjpk     const mib2_transportMLPEntry_t *attr)
51037c478bd9Sstevel@tonic-gate {
51047c478bd9Sstevel@tonic-gate 	char	lname[MAXHOSTNAMELEN + MAXHOSTNAMELEN + 1];
51057c478bd9Sstevel@tonic-gate 			/* hostname + portname */
51067c478bd9Sstevel@tonic-gate 	char	ifname[LIFNAMSIZ + 1];
510745916cd2Sjpk 	const char *ifnamep;
51087c478bd9Sstevel@tonic-gate 
51097c478bd9Sstevel@tonic-gate 	if (!(Aflag || ude6->udp6EntryInfo.ue_state >= MIB2_UDP_connected))
51107c478bd9Sstevel@tonic-gate 		return (first); /* Nothing to print */
51117c478bd9Sstevel@tonic-gate 
51127c478bd9Sstevel@tonic-gate 	if (first) {
511345916cd2Sjpk 		(void) printf("\nUDP: IPv6\n");
511445916cd2Sjpk 		(void) printf(udp_hdr_v6);
51157c478bd9Sstevel@tonic-gate 		first = B_FALSE;
51167c478bd9Sstevel@tonic-gate 	}
51177c478bd9Sstevel@tonic-gate 
511845916cd2Sjpk 	ifnamep = (ude6->udp6IfIndex != 0) ?
511945916cd2Sjpk 	    if_indextoname(ude6->udp6IfIndex, ifname) : NULL;
512045916cd2Sjpk 
51217c478bd9Sstevel@tonic-gate 	(void) printf("%-33s ",
51227c478bd9Sstevel@tonic-gate 	    pr_ap6(&ude6->udp6LocalAddress,
51237c478bd9Sstevel@tonic-gate 	    ude6->udp6LocalPort, "udp", lname, sizeof (lname)));
512445916cd2Sjpk 	(void) printf("%-33s %-10s %s\n",
512545916cd2Sjpk 	    ude6->udp6EntryInfo.ue_state == MIB2_UDP_connected ?
512645916cd2Sjpk 	    pr_ap6(&ude6->udp6EntryInfo.ue_RemoteAddress,
512745916cd2Sjpk 	    ude6->udp6EntryInfo.ue_RemotePort, "udp", lname, sizeof (lname)) :
512845916cd2Sjpk 	    "",
512945916cd2Sjpk 	    miudp_state(ude6->udp6EntryInfo.ue_state, attr),
513045916cd2Sjpk 	    ifnamep == NULL ? "" : ifnamep);
513145916cd2Sjpk 
5132bd670b35SErik Nordmark 	print_transport_label(attr);
513345916cd2Sjpk 
51347c478bd9Sstevel@tonic-gate 	return (first);
51357c478bd9Sstevel@tonic-gate }
51367c478bd9Sstevel@tonic-gate 
51377c478bd9Sstevel@tonic-gate /* ------------------------------ SCTP_REPORT------------------------------- */
51387c478bd9Sstevel@tonic-gate 
51397c478bd9Sstevel@tonic-gate static const char sctp_hdr[] =
51407c478bd9Sstevel@tonic-gate "\nSCTP:";
51417c478bd9Sstevel@tonic-gate static const char sctp_hdr_normal[] =
51427c478bd9Sstevel@tonic-gate "        Local Address                   Remote Address          "
51437c478bd9Sstevel@tonic-gate "Swind  Send-Q Rwind  Recv-Q StrsI/O  State\n"
51447c478bd9Sstevel@tonic-gate "------------------------------- ------------------------------- "
51457c478bd9Sstevel@tonic-gate "------ ------ ------ ------ ------- -----------";
51467c478bd9Sstevel@tonic-gate 
51477c478bd9Sstevel@tonic-gate static const char *
514845916cd2Sjpk nssctp_state(int state, const mib2_transportMLPEntry_t *attr)
51497c478bd9Sstevel@tonic-gate {
515045916cd2Sjpk 	static char sctpsbuf[50];
515145916cd2Sjpk 	const char *cp;
515245916cd2Sjpk 
51537c478bd9Sstevel@tonic-gate 	switch (state) {
51547c478bd9Sstevel@tonic-gate 	case MIB2_SCTP_closed:
515545916cd2Sjpk 		cp = "CLOSED";
515645916cd2Sjpk 		break;
51577c478bd9Sstevel@tonic-gate 	case MIB2_SCTP_cookieWait:
515845916cd2Sjpk 		cp = "COOKIE_WAIT";
515945916cd2Sjpk 		break;
51607c478bd9Sstevel@tonic-gate 	case MIB2_SCTP_cookieEchoed:
516145916cd2Sjpk 		cp = "COOKIE_ECHOED";
516245916cd2Sjpk 		break;
51637c478bd9Sstevel@tonic-gate 	case MIB2_SCTP_established:
516445916cd2Sjpk 		cp = "ESTABLISHED";
516545916cd2Sjpk 		break;
51667c478bd9Sstevel@tonic-gate 	case MIB2_SCTP_shutdownPending:
516745916cd2Sjpk 		cp = "SHUTDOWN_PENDING";
516845916cd2Sjpk 		break;
51697c478bd9Sstevel@tonic-gate 	case MIB2_SCTP_shutdownSent:
517045916cd2Sjpk 		cp = "SHUTDOWN_SENT";
517145916cd2Sjpk 		break;
51727c478bd9Sstevel@tonic-gate 	case MIB2_SCTP_shutdownReceived:
517345916cd2Sjpk 		cp = "SHUTDOWN_RECEIVED";
517445916cd2Sjpk 		break;
51757c478bd9Sstevel@tonic-gate 	case MIB2_SCTP_shutdownAckSent:
517645916cd2Sjpk 		cp = "SHUTDOWN_ACK_SENT";
517745916cd2Sjpk 		break;
51787c478bd9Sstevel@tonic-gate 	case MIB2_SCTP_listen:
517945916cd2Sjpk 		cp = "LISTEN";
518045916cd2Sjpk 		break;
51817c478bd9Sstevel@tonic-gate 	default:
518245916cd2Sjpk 		(void) snprintf(sctpsbuf, sizeof (sctpsbuf),
518345916cd2Sjpk 		    "UNKNOWN STATE(%d)", state);
518445916cd2Sjpk 		cp = sctpsbuf;
518545916cd2Sjpk 		break;
518645916cd2Sjpk 	}
518745916cd2Sjpk 
518845916cd2Sjpk 	if (RSECflag && attr != NULL && attr->tme_flags != 0) {
518945916cd2Sjpk 		if (cp != sctpsbuf) {
519045916cd2Sjpk 			(void) strlcpy(sctpsbuf, cp, sizeof (sctpsbuf));
519145916cd2Sjpk 			cp = sctpsbuf;
519245916cd2Sjpk 		}
519345916cd2Sjpk 		if (attr->tme_flags & MIB2_TMEF_PRIVATE)
519445916cd2Sjpk 			(void) strlcat(sctpsbuf, " P", sizeof (sctpsbuf));
519545916cd2Sjpk 		if (attr->tme_flags & MIB2_TMEF_SHARED)
519645916cd2Sjpk 			(void) strlcat(sctpsbuf, " S", sizeof (sctpsbuf));
51977c478bd9Sstevel@tonic-gate 	}
519845916cd2Sjpk 
519945916cd2Sjpk 	return (cp);
52007c478bd9Sstevel@tonic-gate }
52017c478bd9Sstevel@tonic-gate 
520245916cd2Sjpk static const mib2_sctpConnRemoteEntry_t *
520345916cd2Sjpk sctp_getnext_rem(const mib_item_t **itemp,
520445916cd2Sjpk     const mib2_sctpConnRemoteEntry_t *current, uint32_t associd)
52057c478bd9Sstevel@tonic-gate {
520645916cd2Sjpk 	const mib_item_t *item = *itemp;
520745916cd2Sjpk 	const mib2_sctpConnRemoteEntry_t	*sre;
52087c478bd9Sstevel@tonic-gate 
52097c478bd9Sstevel@tonic-gate 	for (; item != NULL; item = item->next_item, current = NULL) {
52107c478bd9Sstevel@tonic-gate 		if (!(item->group == MIB2_SCTP &&
52117c478bd9Sstevel@tonic-gate 		    item->mib_id == MIB2_SCTP_CONN_REMOTE)) {
52127c478bd9Sstevel@tonic-gate 			continue;
52137c478bd9Sstevel@tonic-gate 		}
52147c478bd9Sstevel@tonic-gate 
52157c478bd9Sstevel@tonic-gate 		if (current != NULL) {
52167c478bd9Sstevel@tonic-gate 			/* LINTED: (note 1) */
521745916cd2Sjpk 			sre = (const mib2_sctpConnRemoteEntry_t *)
521845916cd2Sjpk 			    ((const char *)current + sctpRemoteEntrySize);
52197c478bd9Sstevel@tonic-gate 		} else {
52207c478bd9Sstevel@tonic-gate 			sre = item->valp;
52217c478bd9Sstevel@tonic-gate 		}
52227c478bd9Sstevel@tonic-gate 		for (; (char *)sre < (char *)item->valp + item->length;
522345916cd2Sjpk 		    /* LINTED: (note 1) */
522445916cd2Sjpk 		    sre = (const mib2_sctpConnRemoteEntry_t *)
522545916cd2Sjpk 		    ((const char *)sre + sctpRemoteEntrySize)) {
52267c478bd9Sstevel@tonic-gate 			if (sre->sctpAssocId != associd) {
52277c478bd9Sstevel@tonic-gate 				continue;
52287c478bd9Sstevel@tonic-gate 			}
52297c478bd9Sstevel@tonic-gate 			*itemp = item;
52307c478bd9Sstevel@tonic-gate 			return (sre);
52317c478bd9Sstevel@tonic-gate 		}
52327c478bd9Sstevel@tonic-gate 	}
52337c478bd9Sstevel@tonic-gate 	*itemp = NULL;
52347c478bd9Sstevel@tonic-gate 	return (NULL);
52357c478bd9Sstevel@tonic-gate }
52367c478bd9Sstevel@tonic-gate 
523745916cd2Sjpk static const mib2_sctpConnLocalEntry_t *
523845916cd2Sjpk sctp_getnext_local(const mib_item_t **itemp,
523945916cd2Sjpk     const mib2_sctpConnLocalEntry_t *current, uint32_t associd)
52407c478bd9Sstevel@tonic-gate {
524145916cd2Sjpk 	const mib_item_t *item = *itemp;
524245916cd2Sjpk 	const mib2_sctpConnLocalEntry_t	*sle;
52437c478bd9Sstevel@tonic-gate 
52447c478bd9Sstevel@tonic-gate 	for (; item != NULL; item = item->next_item, current = NULL) {
52457c478bd9Sstevel@tonic-gate 		if (!(item->group == MIB2_SCTP &&
52467c478bd9Sstevel@tonic-gate 		    item->mib_id == MIB2_SCTP_CONN_LOCAL)) {
52477c478bd9Sstevel@tonic-gate 			continue;
52487c478bd9Sstevel@tonic-gate 		}
52497c478bd9Sstevel@tonic-gate 
52507c478bd9Sstevel@tonic-gate 		if (current != NULL) {
52517c478bd9Sstevel@tonic-gate 			/* LINTED: (note 1) */
525245916cd2Sjpk 			sle = (const mib2_sctpConnLocalEntry_t *)
525345916cd2Sjpk 			    ((const char *)current + sctpLocalEntrySize);
52547c478bd9Sstevel@tonic-gate 		} else {
52557c478bd9Sstevel@tonic-gate 			sle = item->valp;
52567c478bd9Sstevel@tonic-gate 		}
52577c478bd9Sstevel@tonic-gate 		for (; (char *)sle < (char *)item->valp + item->length;
525845916cd2Sjpk 		    /* LINTED: (note 1) */
525945916cd2Sjpk 		    sle = (const mib2_sctpConnLocalEntry_t *)
526045916cd2Sjpk 		    ((const char *)sle + sctpLocalEntrySize)) {
52617c478bd9Sstevel@tonic-gate 			if (sle->sctpAssocId != associd) {
52627c478bd9Sstevel@tonic-gate 				continue;
52637c478bd9Sstevel@tonic-gate 			}
52647c478bd9Sstevel@tonic-gate 			*itemp = item;
52657c478bd9Sstevel@tonic-gate 			return (sle);
52667c478bd9Sstevel@tonic-gate 		}
52677c478bd9Sstevel@tonic-gate 	}
52687c478bd9Sstevel@tonic-gate 	*itemp = NULL;
52697c478bd9Sstevel@tonic-gate 	return (NULL);
52707c478bd9Sstevel@tonic-gate }
52717c478bd9Sstevel@tonic-gate 
52727c478bd9Sstevel@tonic-gate static void
52737c478bd9Sstevel@tonic-gate sctp_pr_addr(int type, char *name, int namelen, const in6_addr_t *addr,
52747c478bd9Sstevel@tonic-gate     int port)
52757c478bd9Sstevel@tonic-gate {
52767c478bd9Sstevel@tonic-gate 	ipaddr_t	v4addr;
52777c478bd9Sstevel@tonic-gate 	in6_addr_t	v6addr;
52787c478bd9Sstevel@tonic-gate 
52797c478bd9Sstevel@tonic-gate 	/*
52807c478bd9Sstevel@tonic-gate 	 * Address is either a v4 mapped or v6 addr. If
52817c478bd9Sstevel@tonic-gate 	 * it's a v4 mapped, convert to v4 before
52827c478bd9Sstevel@tonic-gate 	 * displaying.
52837c478bd9Sstevel@tonic-gate 	 */
52847c478bd9Sstevel@tonic-gate 	switch (type) {
5285e11c3f44Smeem 	case MIB2_SCTP_ADDR_V4:
52867c478bd9Sstevel@tonic-gate 		/* v4 */
52877c478bd9Sstevel@tonic-gate 		v6addr = *addr;
52887c478bd9Sstevel@tonic-gate 
52897c478bd9Sstevel@tonic-gate 		IN6_V4MAPPED_TO_IPADDR(&v6addr, v4addr);
52907c478bd9Sstevel@tonic-gate 		if (port > 0) {
52917c478bd9Sstevel@tonic-gate 			(void) pr_ap(v4addr, port, "sctp", name, namelen);
52927c478bd9Sstevel@tonic-gate 		} else {
52937c478bd9Sstevel@tonic-gate 			(void) pr_addr(v4addr, name, namelen);
52947c478bd9Sstevel@tonic-gate 		}
52957c478bd9Sstevel@tonic-gate 		break;
52967c478bd9Sstevel@tonic-gate 
5297e11c3f44Smeem 	case MIB2_SCTP_ADDR_V6:
52987c478bd9Sstevel@tonic-gate 		/* v6 */
52997c478bd9Sstevel@tonic-gate 		if (port > 0) {
53007c478bd9Sstevel@tonic-gate 			(void) pr_ap6(addr, port, "sctp", name, namelen);
53017c478bd9Sstevel@tonic-gate 		} else {
53027c478bd9Sstevel@tonic-gate 			(void) pr_addr6(addr, name, namelen);
53037c478bd9Sstevel@tonic-gate 		}
53047c478bd9Sstevel@tonic-gate 		break;
53057c478bd9Sstevel@tonic-gate 
5306e11c3f44Smeem 	default:
53077c478bd9Sstevel@tonic-gate 		(void) snprintf(name, namelen, "<unknown addr type>");
53087c478bd9Sstevel@tonic-gate 		break;
53097c478bd9Sstevel@tonic-gate 	}
53107c478bd9Sstevel@tonic-gate }
53117c478bd9Sstevel@tonic-gate 
53127c478bd9Sstevel@tonic-gate static void
531345916cd2Sjpk sctp_conn_report_item(const mib_item_t *head, const mib2_sctpConnEntry_t *sp,
531445916cd2Sjpk     const mib2_transportMLPEntry_t *attr)
53157c478bd9Sstevel@tonic-gate {
53167c478bd9Sstevel@tonic-gate 	char		lname[MAXHOSTNAMELEN + MAXHOSTNAMELEN + 1];
53177c478bd9Sstevel@tonic-gate 	char		fname[MAXHOSTNAMELEN + MAXHOSTNAMELEN + 1];
531845916cd2Sjpk 	const mib2_sctpConnRemoteEntry_t	*sre = NULL;
531945916cd2Sjpk 	const mib2_sctpConnLocalEntry_t	*sle = NULL;
532045916cd2Sjpk 	const mib_item_t *local = head;
532145916cd2Sjpk 	const mib_item_t *remote = head;
53227c478bd9Sstevel@tonic-gate 	uint32_t	id = sp->sctpAssocId;
53237c478bd9Sstevel@tonic-gate 	boolean_t	printfirst = B_TRUE;
53247c478bd9Sstevel@tonic-gate 
53257c478bd9Sstevel@tonic-gate 	sctp_pr_addr(sp->sctpAssocRemPrimAddrType, fname, sizeof (fname),
53267c478bd9Sstevel@tonic-gate 	    &sp->sctpAssocRemPrimAddr, sp->sctpAssocRemPort);
53277c478bd9Sstevel@tonic-gate 	sctp_pr_addr(sp->sctpAssocRemPrimAddrType, lname, sizeof (lname),
53287c478bd9Sstevel@tonic-gate 	    &sp->sctpAssocLocPrimAddr, sp->sctpAssocLocalPort);
53297c478bd9Sstevel@tonic-gate 
53307c478bd9Sstevel@tonic-gate 	(void) printf("%-31s %-31s %6u %6d %6u %6d %3d/%-3d %s\n",
53317c478bd9Sstevel@tonic-gate 	    lname, fname,
53327c478bd9Sstevel@tonic-gate 	    sp->sctpConnEntryInfo.ce_swnd,
53337c478bd9Sstevel@tonic-gate 	    sp->sctpConnEntryInfo.ce_sendq,
53347c478bd9Sstevel@tonic-gate 	    sp->sctpConnEntryInfo.ce_rwnd,
53357c478bd9Sstevel@tonic-gate 	    sp->sctpConnEntryInfo.ce_recvq,
53367c478bd9Sstevel@tonic-gate 	    sp->sctpAssocInStreams, sp->sctpAssocOutStreams,
533745916cd2Sjpk 	    nssctp_state(sp->sctpAssocState, attr));
533845916cd2Sjpk 
533945916cd2Sjpk 	print_transport_label(attr);
53407c478bd9Sstevel@tonic-gate 
53417c478bd9Sstevel@tonic-gate 	if (!Vflag) {
53427c478bd9Sstevel@tonic-gate 		return;
53437c478bd9Sstevel@tonic-gate 	}
53447c478bd9Sstevel@tonic-gate 
53457c478bd9Sstevel@tonic-gate 	/* Print remote addresses/local addresses on following lines */
53467c478bd9Sstevel@tonic-gate 	while ((sre = sctp_getnext_rem(&remote, sre, id)) != NULL) {
53477c478bd9Sstevel@tonic-gate 		if (!IN6_ARE_ADDR_EQUAL(&sre->sctpAssocRemAddr,
53487c478bd9Sstevel@tonic-gate 		    &sp->sctpAssocRemPrimAddr)) {
53497c478bd9Sstevel@tonic-gate 			if (printfirst == B_TRUE) {
53507c478bd9Sstevel@tonic-gate 				(void) fputs("\t<Remote: ", stdout);
53517c478bd9Sstevel@tonic-gate 				printfirst = B_FALSE;
53527c478bd9Sstevel@tonic-gate 			} else {
53537c478bd9Sstevel@tonic-gate 				(void) fputs(", ", stdout);
53547c478bd9Sstevel@tonic-gate 			}
53557c478bd9Sstevel@tonic-gate 			sctp_pr_addr(sre->sctpAssocRemAddrType, fname,
53567c478bd9Sstevel@tonic-gate 			    sizeof (fname), &sre->sctpAssocRemAddr, -1);
53577c478bd9Sstevel@tonic-gate 			if (sre->sctpAssocRemAddrActive == MIB2_SCTP_ACTIVE) {
53587c478bd9Sstevel@tonic-gate 				(void) fputs(fname, stdout);
53597c478bd9Sstevel@tonic-gate 			} else {
53607c478bd9Sstevel@tonic-gate 				(void) printf("(%s)", fname);
53617c478bd9Sstevel@tonic-gate 			}
53627c478bd9Sstevel@tonic-gate 		}
53637c478bd9Sstevel@tonic-gate 	}
53647c478bd9Sstevel@tonic-gate 	if (printfirst == B_FALSE) {
53657c478bd9Sstevel@tonic-gate 		(void) puts(">");
53667c478bd9Sstevel@tonic-gate 		printfirst = B_TRUE;
53677c478bd9Sstevel@tonic-gate 	}
53687c478bd9Sstevel@tonic-gate 	while ((sle = sctp_getnext_local(&local, sle, id)) != NULL) {
53697c478bd9Sstevel@tonic-gate 		if (!IN6_ARE_ADDR_EQUAL(&sle->sctpAssocLocalAddr,
53707c478bd9Sstevel@tonic-gate 		    &sp->sctpAssocLocPrimAddr)) {
53717c478bd9Sstevel@tonic-gate 			if (printfirst == B_TRUE) {
53727c478bd9Sstevel@tonic-gate 				(void) fputs("\t<Local: ", stdout);
53737c478bd9Sstevel@tonic-gate 				printfirst = B_FALSE;
53747c478bd9Sstevel@tonic-gate 			} else {
53757c478bd9Sstevel@tonic-gate 				(void) fputs(", ", stdout);
53767c478bd9Sstevel@tonic-gate 			}
53777c478bd9Sstevel@tonic-gate 			sctp_pr_addr(sle->sctpAssocLocalAddrType, lname,
53787c478bd9Sstevel@tonic-gate 			    sizeof (lname), &sle->sctpAssocLocalAddr, -1);
53797c478bd9Sstevel@tonic-gate 			(void) fputs(lname, stdout);
53807c478bd9Sstevel@tonic-gate 		}
53817c478bd9Sstevel@tonic-gate 	}
53827c478bd9Sstevel@tonic-gate 	if (printfirst == B_FALSE) {
53837c478bd9Sstevel@tonic-gate 		(void) puts(">");
53847c478bd9Sstevel@tonic-gate 	}
53857c478bd9Sstevel@tonic-gate }
53867c478bd9Sstevel@tonic-gate 
53877c478bd9Sstevel@tonic-gate static void
538845916cd2Sjpk sctp_report(const mib_item_t *item)
53897c478bd9Sstevel@tonic-gate {
539045916cd2Sjpk 	const mib_item_t		*head;
539145916cd2Sjpk 	const mib2_sctpConnEntry_t	*sp;
53927c478bd9Sstevel@tonic-gate 	boolean_t		first = B_TRUE;
539345916cd2Sjpk 	mib2_transportMLPEntry_t **attrs, **aptr;
539445916cd2Sjpk 	mib2_transportMLPEntry_t *attr;
53957c478bd9Sstevel@tonic-gate 
539645916cd2Sjpk 	/*
539745916cd2Sjpk 	 * Preparation pass: the kernel returns separate entries for SCTP
539845916cd2Sjpk 	 * connection table entries and Multilevel Port attributes.  We loop
539945916cd2Sjpk 	 * through the attributes first and set up an array for each address
540045916cd2Sjpk 	 * family.
540145916cd2Sjpk 	 */
540245916cd2Sjpk 	attrs = RSECflag ?
540345916cd2Sjpk 	    gather_attrs(item, MIB2_SCTP, MIB2_SCTP_CONN, sctpEntrySize) :
540445916cd2Sjpk 	    NULL;
540545916cd2Sjpk 
540645916cd2Sjpk 	aptr = attrs;
54077c478bd9Sstevel@tonic-gate 	head = item;
54087c478bd9Sstevel@tonic-gate 	for (; item != NULL; item = item->next_item) {
54097c478bd9Sstevel@tonic-gate 
54107c478bd9Sstevel@tonic-gate 		if (!(item->group == MIB2_SCTP &&
54117c478bd9Sstevel@tonic-gate 		    item->mib_id == MIB2_SCTP_CONN))
54127c478bd9Sstevel@tonic-gate 			continue;
54137c478bd9Sstevel@tonic-gate 
54147c478bd9Sstevel@tonic-gate 		for (sp = item->valp;
541545916cd2Sjpk 		    (char *)sp < (char *)item->valp + item->length;
541645916cd2Sjpk 		    /* LINTED: (note 1) */
541745916cd2Sjpk 		    sp = (mib2_sctpConnEntry_t *)((char *)sp + sctpEntrySize)) {
541845916cd2Sjpk 			attr = aptr == NULL ? NULL : *aptr++;
54197c478bd9Sstevel@tonic-gate 			if (Aflag ||
54207c478bd9Sstevel@tonic-gate 			    sp->sctpAssocState >= MIB2_SCTP_established) {
54217c478bd9Sstevel@tonic-gate 				if (first == B_TRUE) {
54227c478bd9Sstevel@tonic-gate 					(void) puts(sctp_hdr);
54237c478bd9Sstevel@tonic-gate 					(void) puts(sctp_hdr_normal);
54247c478bd9Sstevel@tonic-gate 					first = B_FALSE;
54257c478bd9Sstevel@tonic-gate 				}
542645916cd2Sjpk 				sctp_conn_report_item(head, sp, attr);
54277c478bd9Sstevel@tonic-gate 			}
54287c478bd9Sstevel@tonic-gate 		}
54297c478bd9Sstevel@tonic-gate 	}
543045916cd2Sjpk 	if (attrs != NULL)
543145916cd2Sjpk 		free(attrs);
54327c478bd9Sstevel@tonic-gate }
54337c478bd9Sstevel@tonic-gate 
54347c478bd9Sstevel@tonic-gate static char *
54357c478bd9Sstevel@tonic-gate plural(int n)
54367c478bd9Sstevel@tonic-gate {
54377c478bd9Sstevel@tonic-gate 	return (n != 1 ? "s" : "");
54387c478bd9Sstevel@tonic-gate }
54397c478bd9Sstevel@tonic-gate 
54407c478bd9Sstevel@tonic-gate static char *
54417c478bd9Sstevel@tonic-gate pluraly(int n)
54427c478bd9Sstevel@tonic-gate {
54437c478bd9Sstevel@tonic-gate 	return (n != 1 ? "ies" : "y");
54447c478bd9Sstevel@tonic-gate }
54457c478bd9Sstevel@tonic-gate 
54467c478bd9Sstevel@tonic-gate static char *
54477c478bd9Sstevel@tonic-gate plurales(int n)
54487c478bd9Sstevel@tonic-gate {
54497c478bd9Sstevel@tonic-gate 	return (n != 1 ? "es" : "");
54507c478bd9Sstevel@tonic-gate }
54517c478bd9Sstevel@tonic-gate 
54527c478bd9Sstevel@tonic-gate static char *
54537c478bd9Sstevel@tonic-gate pktscale(n)
54547c478bd9Sstevel@tonic-gate 	int n;
54557c478bd9Sstevel@tonic-gate {
54567c478bd9Sstevel@tonic-gate 	static char buf[6];
54577c478bd9Sstevel@tonic-gate 	char t;
54587c478bd9Sstevel@tonic-gate 
54597c478bd9Sstevel@tonic-gate 	if (n < 1024) {
54607c478bd9Sstevel@tonic-gate 		t = ' ';
54617c478bd9Sstevel@tonic-gate 	} else if (n < 1024 * 1024) {
54627c478bd9Sstevel@tonic-gate 		t = 'k';
54637c478bd9Sstevel@tonic-gate 		n /= 1024;
54647c478bd9Sstevel@tonic-gate 	} else if (n < 1024 * 1024 * 1024) {
54657c478bd9Sstevel@tonic-gate 		t = 'm';
54667c478bd9Sstevel@tonic-gate 		n /= 1024 * 1024;
54677c478bd9Sstevel@tonic-gate 	} else {
54687c478bd9Sstevel@tonic-gate 		t = 'g';
54697c478bd9Sstevel@tonic-gate 		n /= 1024 * 1024 * 1024;
54707c478bd9Sstevel@tonic-gate 	}
54717c478bd9Sstevel@tonic-gate 
54727c478bd9Sstevel@tonic-gate 	(void) snprintf(buf, sizeof (buf), "%4u%c", n, t);
54737c478bd9Sstevel@tonic-gate 	return (buf);
54747c478bd9Sstevel@tonic-gate }
54757c478bd9Sstevel@tonic-gate 
54767c478bd9Sstevel@tonic-gate /* --------------------- mrt_report (netstat -m) -------------------------- */
54777c478bd9Sstevel@tonic-gate 
54787c478bd9Sstevel@tonic-gate static void
54797c478bd9Sstevel@tonic-gate mrt_report(mib_item_t *item)
54807c478bd9Sstevel@tonic-gate {
54817c478bd9Sstevel@tonic-gate 	int		jtemp = 0;
54827c478bd9Sstevel@tonic-gate 	struct vifctl	*vip;
54837c478bd9Sstevel@tonic-gate 	vifi_t		vifi;
54847c478bd9Sstevel@tonic-gate 	struct mfcctl	*mfccp;
54857c478bd9Sstevel@tonic-gate 	int		numvifs = 0;
54867c478bd9Sstevel@tonic-gate 	int		nmfc = 0;
54877c478bd9Sstevel@tonic-gate 	char		abuf[MAXHOSTNAMELEN + 1];
54887c478bd9Sstevel@tonic-gate 
54897c478bd9Sstevel@tonic-gate 	if (!(family_selected(AF_INET)))
54907c478bd9Sstevel@tonic-gate 		return;
54917c478bd9Sstevel@tonic-gate 
54927c478bd9Sstevel@tonic-gate 	/* 'for' loop 1: */
54937c478bd9Sstevel@tonic-gate 	for (; item; item = item->next_item) {
5494bd670b35SErik Nordmark 		if (Xflag) {
54957c478bd9Sstevel@tonic-gate 			(void) printf("\n--- Entry %d ---\n", ++jtemp);
54967c478bd9Sstevel@tonic-gate 			(void) printf("Group = %d, mib_id = %d, "
54977c478bd9Sstevel@tonic-gate 			    "length = %d, valp = 0x%p\n",
54987c478bd9Sstevel@tonic-gate 			    item->group, item->mib_id, item->length,
54997c478bd9Sstevel@tonic-gate 			    item->valp);
55007c478bd9Sstevel@tonic-gate 		}
55017c478bd9Sstevel@tonic-gate 		if (item->group != EXPER_DVMRP)
55027c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
55037c478bd9Sstevel@tonic-gate 
55047c478bd9Sstevel@tonic-gate 		switch (item->mib_id) {
55057c478bd9Sstevel@tonic-gate 
55067c478bd9Sstevel@tonic-gate 		case EXPER_DVMRP_VIF:
5507bd670b35SErik Nordmark 			if (Xflag)
55087c478bd9Sstevel@tonic-gate 				(void) printf("%u records for ipVifTable:\n",
55097c478bd9Sstevel@tonic-gate 				    item->length/sizeof (struct vifctl));
55107c478bd9Sstevel@tonic-gate 			if (item->length/sizeof (struct vifctl) == 0) {
55117c478bd9Sstevel@tonic-gate 				(void) puts("\nVirtual Interface Table is "
55127c478bd9Sstevel@tonic-gate 				    "empty");
55137c478bd9Sstevel@tonic-gate 				break;
55147c478bd9Sstevel@tonic-gate 			}
55157c478bd9Sstevel@tonic-gate 
55167c478bd9Sstevel@tonic-gate 			(void) puts("\nVirtual Interface Table\n"
55177c478bd9Sstevel@tonic-gate 			    " Vif Threshold Rate_Limit Local-Address"
55187c478bd9Sstevel@tonic-gate 			    "   Remote-Address     Pkt_in   Pkt_out");
55197c478bd9Sstevel@tonic-gate 
55207c478bd9Sstevel@tonic-gate 			/* 'for' loop 2: */
55217c478bd9Sstevel@tonic-gate 			for (vip = (struct vifctl *)item->valp;
55227c478bd9Sstevel@tonic-gate 			    (char *)vip < (char *)item->valp + item->length;
55237c478bd9Sstevel@tonic-gate 			    /* LINTED: (note 1) */
55247c478bd9Sstevel@tonic-gate 			    vip = (struct vifctl *)((char *)vip +
55257c478bd9Sstevel@tonic-gate 			    vifctlSize)) {
55267c478bd9Sstevel@tonic-gate 				if (vip->vifc_lcl_addr.s_addr == 0)
55277c478bd9Sstevel@tonic-gate 					continue; /* 'for' loop 2 */
55287c478bd9Sstevel@tonic-gate 				/* numvifs = vip->vifc_vifi; */
55297c478bd9Sstevel@tonic-gate 
55307c478bd9Sstevel@tonic-gate 				numvifs++;
55317c478bd9Sstevel@tonic-gate 				(void) printf("  %2u       %3u       "
55327c478bd9Sstevel@tonic-gate 				    "%4u %-15.15s",
55337c478bd9Sstevel@tonic-gate 				    vip->vifc_vifi,
55347c478bd9Sstevel@tonic-gate 				    vip->vifc_threshold,
55357c478bd9Sstevel@tonic-gate 				    vip->vifc_rate_limit,
55367c478bd9Sstevel@tonic-gate 				    pr_addr(vip->vifc_lcl_addr.s_addr,
55377c478bd9Sstevel@tonic-gate 				    abuf, sizeof (abuf)));
55387c478bd9Sstevel@tonic-gate 				(void) printf(" %-15.15s  %8u  %8u\n",
55397c478bd9Sstevel@tonic-gate 				    (vip->vifc_flags & VIFF_TUNNEL) ?
55407c478bd9Sstevel@tonic-gate 				    pr_addr(vip->vifc_rmt_addr.s_addr,
55417c478bd9Sstevel@tonic-gate 				    abuf, sizeof (abuf)) : "",
55427c478bd9Sstevel@tonic-gate 				    vip->vifc_pkt_in,
55437c478bd9Sstevel@tonic-gate 				    vip->vifc_pkt_out);
55447c478bd9Sstevel@tonic-gate 			} /* 'for' loop 2 ends */
55457c478bd9Sstevel@tonic-gate 
55467c478bd9Sstevel@tonic-gate 			(void) printf("Numvifs: %d\n", numvifs);
55477c478bd9Sstevel@tonic-gate 			break;
55487c478bd9Sstevel@tonic-gate 
55497c478bd9Sstevel@tonic-gate 		case EXPER_DVMRP_MRT:
5550bd670b35SErik Nordmark 			if (Xflag)
55517c478bd9Sstevel@tonic-gate 				(void) printf("%u records for ipMfcTable:\n",
5552e11c3f44Smeem 				    item->length/sizeof (struct vifctl));
55537c478bd9Sstevel@tonic-gate 			if (item->length/sizeof (struct vifctl) == 0) {
55547c478bd9Sstevel@tonic-gate 				(void) puts("\nMulticast Forwarding Cache is "
55557c478bd9Sstevel@tonic-gate 				    "empty");
55567c478bd9Sstevel@tonic-gate 				break;
55577c478bd9Sstevel@tonic-gate 			}
55587c478bd9Sstevel@tonic-gate 
55597c478bd9Sstevel@tonic-gate 			(void) puts("\nMulticast Forwarding Cache\n"
55607c478bd9Sstevel@tonic-gate 			    "  Origin-Subnet                 Mcastgroup      "
55617c478bd9Sstevel@tonic-gate 			    "# Pkts  In-Vif  Out-vifs/Forw-ttl");
55627c478bd9Sstevel@tonic-gate 
55637c478bd9Sstevel@tonic-gate 			for (mfccp = (struct mfcctl *)item->valp;
55647c478bd9Sstevel@tonic-gate 			    (char *)mfccp < (char *)item->valp + item->length;
55657c478bd9Sstevel@tonic-gate 			    /* LINTED: (note 1) */
55667c478bd9Sstevel@tonic-gate 			    mfccp = (struct mfcctl *)((char *)mfccp +
55677c478bd9Sstevel@tonic-gate 			    mfcctlSize)) {
55687c478bd9Sstevel@tonic-gate 
55697c478bd9Sstevel@tonic-gate 				nmfc++;
55707c478bd9Sstevel@tonic-gate 				(void) printf("  %-30.15s",
55717c478bd9Sstevel@tonic-gate 				    pr_addr(mfccp->mfcc_origin.s_addr,
55727c478bd9Sstevel@tonic-gate 				    abuf, sizeof (abuf)));
55737c478bd9Sstevel@tonic-gate 				(void) printf("%-15.15s  %6s  %3u    ",
55747c478bd9Sstevel@tonic-gate 				    pr_net(mfccp->mfcc_mcastgrp.s_addr,
5575e11c3f44Smeem 				    mfccp->mfcc_mcastgrp.s_addr,
5576e11c3f44Smeem 				    abuf, sizeof (abuf)),
55777c478bd9Sstevel@tonic-gate 				    pktscale((int)mfccp->mfcc_pkt_cnt),
5578e11c3f44Smeem 				    mfccp->mfcc_parent);
55797c478bd9Sstevel@tonic-gate 
55807c478bd9Sstevel@tonic-gate 				for (vifi = 0; vifi < MAXVIFS; ++vifi) {
55817c478bd9Sstevel@tonic-gate 					if (mfccp->mfcc_ttls[vifi]) {
55827c478bd9Sstevel@tonic-gate 						(void) printf("      %u (%u)",
55837c478bd9Sstevel@tonic-gate 						    vifi,
55847c478bd9Sstevel@tonic-gate 						    mfccp->mfcc_ttls[vifi]);
55857c478bd9Sstevel@tonic-gate 					}
55867c478bd9Sstevel@tonic-gate 
55877c478bd9Sstevel@tonic-gate 				}
55887c478bd9Sstevel@tonic-gate 				(void) putchar('\n');
55897c478bd9Sstevel@tonic-gate 			}
55907c478bd9Sstevel@tonic-gate 			(void) printf("\nTotal no. of entries in cache: %d\n",
55917c478bd9Sstevel@tonic-gate 			    nmfc);
55927c478bd9Sstevel@tonic-gate 			break;
55937c478bd9Sstevel@tonic-gate 		}
55947c478bd9Sstevel@tonic-gate 	} /* 'for' loop 1 ends */
55957c478bd9Sstevel@tonic-gate 	(void) putchar('\n');
55967c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
55977c478bd9Sstevel@tonic-gate }
55987c478bd9Sstevel@tonic-gate 
55997c478bd9Sstevel@tonic-gate /*
56007c478bd9Sstevel@tonic-gate  * Get the stats for the cache named 'name'.  If prefix != 0, then
56017c478bd9Sstevel@tonic-gate  * interpret the name as a prefix, and sum up stats for all caches
56027c478bd9Sstevel@tonic-gate  * named 'name*'.
56037c478bd9Sstevel@tonic-gate  */
56047c478bd9Sstevel@tonic-gate static void
56057c478bd9Sstevel@tonic-gate kmem_cache_stats(char *title, char *name, int prefix, int64_t *total_bytes)
56067c478bd9Sstevel@tonic-gate {
56077c478bd9Sstevel@tonic-gate 	int len;
56087c478bd9Sstevel@tonic-gate 	int alloc;
56097c478bd9Sstevel@tonic-gate 	int64_t total_alloc = 0;
56107c478bd9Sstevel@tonic-gate 	int alloc_fail, total_alloc_fail = 0;
56117c478bd9Sstevel@tonic-gate 	int buf_size = 0;
56127c478bd9Sstevel@tonic-gate 	int buf_avail;
56137c478bd9Sstevel@tonic-gate 	int buf_total;
56147c478bd9Sstevel@tonic-gate 	int buf_max, total_buf_max = 0;
56157c478bd9Sstevel@tonic-gate 	int buf_inuse, total_buf_inuse = 0;
56167c478bd9Sstevel@tonic-gate 	kstat_t *ksp;
56177c478bd9Sstevel@tonic-gate 	char buf[256];
56187c478bd9Sstevel@tonic-gate 
56197c478bd9Sstevel@tonic-gate 	len = prefix ? strlen(name) : 256;
56207c478bd9Sstevel@tonic-gate 
56217c478bd9Sstevel@tonic-gate 	/* 'for' loop 1: */
56227c478bd9Sstevel@tonic-gate 	for (ksp = kc->kc_chain; ksp != NULL; ksp = ksp->ks_next) {
56237c478bd9Sstevel@tonic-gate 
56247c478bd9Sstevel@tonic-gate 		if (strcmp(ksp->ks_class, "kmem_cache") != 0)
56257c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
56267c478bd9Sstevel@tonic-gate 
56277c478bd9Sstevel@tonic-gate 		/*
56287c478bd9Sstevel@tonic-gate 		 * Hack alert: because of the way streams messages are
56297c478bd9Sstevel@tonic-gate 		 * allocated, every constructed free dblk has an associated
56307c478bd9Sstevel@tonic-gate 		 * mblk.  From the allocator's viewpoint those mblks are
56317c478bd9Sstevel@tonic-gate 		 * allocated (because they haven't been freed), but from
56327c478bd9Sstevel@tonic-gate 		 * our viewpoint they're actually free (because they're
56337c478bd9Sstevel@tonic-gate 		 * not currently in use).  To account for this caching
56347c478bd9Sstevel@tonic-gate 		 * effect we subtract the total constructed free dblks
56357c478bd9Sstevel@tonic-gate 		 * from the total allocated mblks to derive mblks in use.
56367c478bd9Sstevel@tonic-gate 		 */
56377c478bd9Sstevel@tonic-gate 		if (strcmp(name, "streams_mblk") == 0 &&
56387c478bd9Sstevel@tonic-gate 		    strncmp(ksp->ks_name, "streams_dblk", 12) == 0) {
56397c478bd9Sstevel@tonic-gate 			(void) safe_kstat_read(kc, ksp, NULL);
56407c478bd9Sstevel@tonic-gate 			total_buf_inuse -=
5641e11c3f44Smeem 			    kstat_named_value(ksp, "buf_constructed");
56427c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
56437c478bd9Sstevel@tonic-gate 		}
56447c478bd9Sstevel@tonic-gate 
56457c478bd9Sstevel@tonic-gate 		if (strncmp(ksp->ks_name, name, len) != 0)
56467c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
56477c478bd9Sstevel@tonic-gate 
56487c478bd9Sstevel@tonic-gate 		(void) safe_kstat_read(kc, ksp, NULL);
56497c478bd9Sstevel@tonic-gate 
56507c478bd9Sstevel@tonic-gate 		alloc		= kstat_named_value(ksp, "alloc");
56517c478bd9Sstevel@tonic-gate 		alloc_fail	= kstat_named_value(ksp, "alloc_fail");
56527c478bd9Sstevel@tonic-gate 		buf_size	= kstat_named_value(ksp, "buf_size");
56537c478bd9Sstevel@tonic-gate 		buf_avail	= kstat_named_value(ksp, "buf_avail");
56547c478bd9Sstevel@tonic-gate 		buf_total	= kstat_named_value(ksp, "buf_total");
56557c478bd9Sstevel@tonic-gate 		buf_max		= kstat_named_value(ksp, "buf_max");
56567c478bd9Sstevel@tonic-gate 		buf_inuse	= buf_total - buf_avail;
56577c478bd9Sstevel@tonic-gate 
56587c478bd9Sstevel@tonic-gate 		if (Vflag && prefix) {
56597c478bd9Sstevel@tonic-gate 			(void) snprintf(buf, sizeof (buf), "%s%s", title,
56607c478bd9Sstevel@tonic-gate 			    ksp->ks_name + len);
56617c478bd9Sstevel@tonic-gate 			(void) printf("    %-18s %6u %9u %11u %11u\n",
56627c478bd9Sstevel@tonic-gate 			    buf, buf_inuse, buf_max, alloc, alloc_fail);
56637c478bd9Sstevel@tonic-gate 		}
56647c478bd9Sstevel@tonic-gate 
56657c478bd9Sstevel@tonic-gate 		total_alloc		+= alloc;
56667c478bd9Sstevel@tonic-gate 		total_alloc_fail	+= alloc_fail;
56677c478bd9Sstevel@tonic-gate 		total_buf_max		+= buf_max;
56687c478bd9Sstevel@tonic-gate 		total_buf_inuse		+= buf_inuse;
56697c478bd9Sstevel@tonic-gate 		*total_bytes		+= (int64_t)buf_inuse * buf_size;
56707c478bd9Sstevel@tonic-gate 	} /* 'for' loop 1 ends */
56717c478bd9Sstevel@tonic-gate 
56727c478bd9Sstevel@tonic-gate 	if (buf_size == 0) {
56737c478bd9Sstevel@tonic-gate 		(void) printf("%-22s [couldn't find statistics for %s]\n",
5674e11c3f44Smeem 		    title, name);
56757c478bd9Sstevel@tonic-gate 		return;
56767c478bd9Sstevel@tonic-gate 	}
56777c478bd9Sstevel@tonic-gate 
56787c478bd9Sstevel@tonic-gate 	if (Vflag && prefix)
56797c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (buf), "%s_total", title);
56807c478bd9Sstevel@tonic-gate 	else
56817c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (buf), "%s", title);
56827c478bd9Sstevel@tonic-gate 
56837c478bd9Sstevel@tonic-gate 	(void) printf("%-22s %6d %9d %11lld %11d\n", buf,
5684e11c3f44Smeem 	    total_buf_inuse, total_buf_max, total_alloc, total_alloc_fail);
56857c478bd9Sstevel@tonic-gate }
56867c478bd9Sstevel@tonic-gate 
56877c478bd9Sstevel@tonic-gate static void
56887c478bd9Sstevel@tonic-gate m_report(void)
56897c478bd9Sstevel@tonic-gate {
56907c478bd9Sstevel@tonic-gate 	int64_t total_bytes = 0;
56917c478bd9Sstevel@tonic-gate 
56927c478bd9Sstevel@tonic-gate 	(void) puts("streams allocation:");
56937c478bd9Sstevel@tonic-gate 	(void) printf("%63s\n", "cumulative  allocation");
56947c478bd9Sstevel@tonic-gate 	(void) printf("%63s\n",
56957c478bd9Sstevel@tonic-gate 	    "current   maximum       total    failures");
56967c478bd9Sstevel@tonic-gate 
56977c478bd9Sstevel@tonic-gate 	kmem_cache_stats("streams",
56987c478bd9Sstevel@tonic-gate 	    "stream_head_cache", 0, &total_bytes);
56997c478bd9Sstevel@tonic-gate 	kmem_cache_stats("queues", "queue_cache", 0, &total_bytes);
57007c478bd9Sstevel@tonic-gate 	kmem_cache_stats("mblk", "streams_mblk", 0, &total_bytes);
57017c478bd9Sstevel@tonic-gate 	kmem_cache_stats("dblk", "streams_dblk", 1, &total_bytes);
57027c478bd9Sstevel@tonic-gate 	kmem_cache_stats("linkblk", "linkinfo_cache", 0, &total_bytes);
57037c478bd9Sstevel@tonic-gate 	kmem_cache_stats("syncq", "syncq_cache", 0, &total_bytes);
57047c478bd9Sstevel@tonic-gate 	kmem_cache_stats("qband", "qband_cache", 0, &total_bytes);
57057c478bd9Sstevel@tonic-gate 
57067c478bd9Sstevel@tonic-gate 	(void) printf("\n%lld Kbytes allocated for streams data\n",
5707e11c3f44Smeem 	    total_bytes / 1024);
57087c478bd9Sstevel@tonic-gate 
57097c478bd9Sstevel@tonic-gate 	(void) putchar('\n');
57107c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
57117c478bd9Sstevel@tonic-gate }
57127c478bd9Sstevel@tonic-gate 
57137c478bd9Sstevel@tonic-gate /* --------------------------------- */
57147c478bd9Sstevel@tonic-gate 
57157c478bd9Sstevel@tonic-gate /*
57167c478bd9Sstevel@tonic-gate  * Print an IPv4 address. Remove the matching part of the domain name
57177c478bd9Sstevel@tonic-gate  * from the returned name.
57187c478bd9Sstevel@tonic-gate  */
57197c478bd9Sstevel@tonic-gate static char *
57207c478bd9Sstevel@tonic-gate pr_addr(uint_t addr, char *dst, uint_t dstlen)
57217c478bd9Sstevel@tonic-gate {
57227c478bd9Sstevel@tonic-gate 	char			*cp;
57237c478bd9Sstevel@tonic-gate 	struct hostent		*hp = NULL;
57247c478bd9Sstevel@tonic-gate 	static char		domain[MAXHOSTNAMELEN + 1];
57257c478bd9Sstevel@tonic-gate 	static boolean_t	first = B_TRUE;
57267c478bd9Sstevel@tonic-gate 	int			error_num;
57277c478bd9Sstevel@tonic-gate 
57287c478bd9Sstevel@tonic-gate 	if (first) {
57297c478bd9Sstevel@tonic-gate 		first = B_FALSE;
57307c478bd9Sstevel@tonic-gate 		if (sysinfo(SI_HOSTNAME, domain, MAXHOSTNAMELEN) != -1 &&
57317c478bd9Sstevel@tonic-gate 		    (cp = strchr(domain, '.'))) {
57327c478bd9Sstevel@tonic-gate 			(void) strncpy(domain, cp + 1, sizeof (domain));
57337c478bd9Sstevel@tonic-gate 		} else
57347c478bd9Sstevel@tonic-gate 			domain[0] = 0;
57357c478bd9Sstevel@tonic-gate 	}
57367c478bd9Sstevel@tonic-gate 	cp = NULL;
57377c478bd9Sstevel@tonic-gate 	if (!Nflag) {
57387c478bd9Sstevel@tonic-gate 		hp = getipnodebyaddr((char *)&addr, sizeof (uint_t), AF_INET,
57397c478bd9Sstevel@tonic-gate 		    &error_num);
57407c478bd9Sstevel@tonic-gate 		if (hp) {
57417c478bd9Sstevel@tonic-gate 			if ((cp = strchr(hp->h_name, '.')) != NULL &&
57427c478bd9Sstevel@tonic-gate 			    strcasecmp(cp + 1, domain) == 0)
57437c478bd9Sstevel@tonic-gate 				*cp = 0;
57447c478bd9Sstevel@tonic-gate 			cp = hp->h_name;
57457c478bd9Sstevel@tonic-gate 		}
57467c478bd9Sstevel@tonic-gate 	}
57477c478bd9Sstevel@tonic-gate 	if (cp != NULL) {
57487c478bd9Sstevel@tonic-gate 		(void) strncpy(dst, cp, dstlen);
57497c478bd9Sstevel@tonic-gate 		dst[dstlen - 1] = 0;
57507c478bd9Sstevel@tonic-gate 	} else {
57517c478bd9Sstevel@tonic-gate 		(void) inet_ntop(AF_INET, (char *)&addr, dst, dstlen);
57527c478bd9Sstevel@tonic-gate 	}
57537c478bd9Sstevel@tonic-gate 	if (hp != NULL)
57547c478bd9Sstevel@tonic-gate 		freehostent(hp);
57557c478bd9Sstevel@tonic-gate 	return (dst);
57567c478bd9Sstevel@tonic-gate }
57577c478bd9Sstevel@tonic-gate 
57587c478bd9Sstevel@tonic-gate /*
57597c478bd9Sstevel@tonic-gate  * Print a non-zero IPv4 address.  Print "    --" if the address is zero.
57607c478bd9Sstevel@tonic-gate  */
57617c478bd9Sstevel@tonic-gate static char *
57627c478bd9Sstevel@tonic-gate pr_addrnz(ipaddr_t addr, char *dst, uint_t dstlen)
57637c478bd9Sstevel@tonic-gate {
57647c478bd9Sstevel@tonic-gate 	if (addr == INADDR_ANY) {
57657c478bd9Sstevel@tonic-gate 		(void) strlcpy(dst, "    --", dstlen);
57667c478bd9Sstevel@tonic-gate 		return (dst);
57677c478bd9Sstevel@tonic-gate 	}
57687c478bd9Sstevel@tonic-gate 	return (pr_addr(addr, dst, dstlen));
57697c478bd9Sstevel@tonic-gate }
57707c478bd9Sstevel@tonic-gate 
57717c478bd9Sstevel@tonic-gate /*
57727c478bd9Sstevel@tonic-gate  * Print an IPv6 address. Remove the matching part of the domain name
57737c478bd9Sstevel@tonic-gate  * from the returned name.
57747c478bd9Sstevel@tonic-gate  */
57757c478bd9Sstevel@tonic-gate static char *
57767c478bd9Sstevel@tonic-gate pr_addr6(const struct in6_addr *addr, char *dst, uint_t dstlen)
57777c478bd9Sstevel@tonic-gate {
57787c478bd9Sstevel@tonic-gate 	char			*cp;
57797c478bd9Sstevel@tonic-gate 	struct hostent		*hp = NULL;
57807c478bd9Sstevel@tonic-gate 	static char		domain[MAXHOSTNAMELEN + 1];
57817c478bd9Sstevel@tonic-gate 	static boolean_t	first = B_TRUE;
57827c478bd9Sstevel@tonic-gate 	int			error_num;
57837c478bd9Sstevel@tonic-gate 
57847c478bd9Sstevel@tonic-gate 	if (first) {
57857c478bd9Sstevel@tonic-gate 		first = B_FALSE;
57867c478bd9Sstevel@tonic-gate 		if (sysinfo(SI_HOSTNAME, domain, MAXHOSTNAMELEN) != -1 &&
57877c478bd9Sstevel@tonic-gate 		    (cp = strchr(domain, '.'))) {
57887c478bd9Sstevel@tonic-gate 			(void) strncpy(domain, cp + 1, sizeof (domain));
57897c478bd9Sstevel@tonic-gate 		} else
57907c478bd9Sstevel@tonic-gate 			domain[0] = 0;
57917c478bd9Sstevel@tonic-gate 	}
57927c478bd9Sstevel@tonic-gate 	cp = NULL;
57937c478bd9Sstevel@tonic-gate 	if (!Nflag) {
57947c478bd9Sstevel@tonic-gate 		hp = getipnodebyaddr((char *)addr,
57957c478bd9Sstevel@tonic-gate 		    sizeof (struct in6_addr), AF_INET6, &error_num);
57967c478bd9Sstevel@tonic-gate 		if (hp) {
57977c478bd9Sstevel@tonic-gate 			if ((cp = strchr(hp->h_name, '.')) != NULL &&
57987c478bd9Sstevel@tonic-gate 			    strcasecmp(cp + 1, domain) == 0)
57997c478bd9Sstevel@tonic-gate 				*cp = 0;
58007c478bd9Sstevel@tonic-gate 			cp = hp->h_name;
58017c478bd9Sstevel@tonic-gate 		}
58027c478bd9Sstevel@tonic-gate 	}
58037c478bd9Sstevel@tonic-gate 	if (cp != NULL) {
58047c478bd9Sstevel@tonic-gate 		(void) strncpy(dst, cp, dstlen);
58057c478bd9Sstevel@tonic-gate 		dst[dstlen - 1] = 0;
58067c478bd9Sstevel@tonic-gate 	} else {
58077c478bd9Sstevel@tonic-gate 		(void) inet_ntop(AF_INET6, (void *)addr, dst, dstlen);
58087c478bd9Sstevel@tonic-gate 	}
58097c478bd9Sstevel@tonic-gate 	if (hp != NULL)
58107c478bd9Sstevel@tonic-gate 		freehostent(hp);
58117c478bd9Sstevel@tonic-gate 	return (dst);
58127c478bd9Sstevel@tonic-gate }
58137c478bd9Sstevel@tonic-gate 
58147c478bd9Sstevel@tonic-gate /* For IPv4 masks */
58157c478bd9Sstevel@tonic-gate static char *
58167c478bd9Sstevel@tonic-gate pr_mask(uint_t addr, char *dst, uint_t dstlen)
58177c478bd9Sstevel@tonic-gate {
58187c478bd9Sstevel@tonic-gate 	uint8_t	*ip_addr = (uint8_t *)&addr;
58197c478bd9Sstevel@tonic-gate 
58207c478bd9Sstevel@tonic-gate 	(void) snprintf(dst, dstlen, "%d.%d.%d.%d",
58217c478bd9Sstevel@tonic-gate 	    ip_addr[0], ip_addr[1], ip_addr[2], ip_addr[3]);
58227c478bd9Sstevel@tonic-gate 	return (dst);
58237c478bd9Sstevel@tonic-gate }
58247c478bd9Sstevel@tonic-gate 
58257c478bd9Sstevel@tonic-gate /*
58267c478bd9Sstevel@tonic-gate  * For ipv6 masks format is : dest/mask
58277c478bd9Sstevel@tonic-gate  * Does not print /128 to save space in printout. H flag carries this notion.
58287c478bd9Sstevel@tonic-gate  */
58297c478bd9Sstevel@tonic-gate static char *
583045916cd2Sjpk pr_prefix6(const struct in6_addr *addr, uint_t prefixlen, char *dst,
583145916cd2Sjpk     uint_t dstlen)
58327c478bd9Sstevel@tonic-gate {
58337c478bd9Sstevel@tonic-gate 	char *cp;
58347c478bd9Sstevel@tonic-gate 
58357c478bd9Sstevel@tonic-gate 	if (IN6_IS_ADDR_UNSPECIFIED(addr) && prefixlen == 0) {
58367c478bd9Sstevel@tonic-gate 		(void) strncpy(dst, "default", dstlen);
58377c478bd9Sstevel@tonic-gate 		dst[dstlen - 1] = 0;
58387c478bd9Sstevel@tonic-gate 		return (dst);
58397c478bd9Sstevel@tonic-gate 	}
58407c478bd9Sstevel@tonic-gate 
58417c478bd9Sstevel@tonic-gate 	(void) pr_addr6(addr, dst, dstlen);
58427c478bd9Sstevel@tonic-gate 	if (prefixlen != IPV6_ABITS) {
58437c478bd9Sstevel@tonic-gate 		/* How much room is left? */
58447c478bd9Sstevel@tonic-gate 		cp = strchr(dst, '\0');
58457c478bd9Sstevel@tonic-gate 		if (dst + dstlen > cp) {
58467c478bd9Sstevel@tonic-gate 			dstlen -= (cp - dst);
58477c478bd9Sstevel@tonic-gate 			(void) snprintf(cp, dstlen, "/%d", prefixlen);
58487c478bd9Sstevel@tonic-gate 		}
58497c478bd9Sstevel@tonic-gate 	}
58507c478bd9Sstevel@tonic-gate 	return (dst);
58517c478bd9Sstevel@tonic-gate }
58527c478bd9Sstevel@tonic-gate 
58537c478bd9Sstevel@tonic-gate /* Print IPv4 address and port */
58547c478bd9Sstevel@tonic-gate static char *
58557c478bd9Sstevel@tonic-gate pr_ap(uint_t addr, uint_t port, char *proto,
58567c478bd9Sstevel@tonic-gate     char *dst, uint_t dstlen)
58577c478bd9Sstevel@tonic-gate {
58587c478bd9Sstevel@tonic-gate 	char *cp;
58597c478bd9Sstevel@tonic-gate 
58607c478bd9Sstevel@tonic-gate 	if (addr == INADDR_ANY) {
58617c478bd9Sstevel@tonic-gate 		(void) strncpy(dst, "      *", dstlen);
58627c478bd9Sstevel@tonic-gate 		dst[dstlen - 1] = 0;
58637c478bd9Sstevel@tonic-gate 	} else {
58647c478bd9Sstevel@tonic-gate 		(void) pr_addr(addr, dst, dstlen);
58657c478bd9Sstevel@tonic-gate 	}
58667c478bd9Sstevel@tonic-gate 	/* How much room is left? */
58677c478bd9Sstevel@tonic-gate 	cp = strchr(dst, '\0');
58687c478bd9Sstevel@tonic-gate 	if (dst + dstlen > cp + 1) {
58697c478bd9Sstevel@tonic-gate 		*cp++ = '.';
58707c478bd9Sstevel@tonic-gate 		dstlen -= (cp - dst);
58717c478bd9Sstevel@tonic-gate 		dstlen--;
58727c478bd9Sstevel@tonic-gate 		(void) portname(port, proto, cp, dstlen);
58737c478bd9Sstevel@tonic-gate 	}
58747c478bd9Sstevel@tonic-gate 	return (dst);
58757c478bd9Sstevel@tonic-gate }
58767c478bd9Sstevel@tonic-gate 
58777c478bd9Sstevel@tonic-gate /* Print IPv6 address and port */
58787c478bd9Sstevel@tonic-gate static char *
58797c478bd9Sstevel@tonic-gate pr_ap6(const in6_addr_t *addr, uint_t port, char *proto,
58807c478bd9Sstevel@tonic-gate     char *dst, uint_t dstlen)
58817c478bd9Sstevel@tonic-gate {
58827c478bd9Sstevel@tonic-gate 	char *cp;
58837c478bd9Sstevel@tonic-gate 
58847c478bd9Sstevel@tonic-gate 	if (IN6_IS_ADDR_UNSPECIFIED(addr)) {
58857c478bd9Sstevel@tonic-gate 		(void) strncpy(dst, "      *", dstlen);
58867c478bd9Sstevel@tonic-gate 		dst[dstlen - 1] = 0;
58877c478bd9Sstevel@tonic-gate 	} else {
58887c478bd9Sstevel@tonic-gate 		(void) pr_addr6(addr, dst, dstlen);
58897c478bd9Sstevel@tonic-gate 	}
58907c478bd9Sstevel@tonic-gate 	/* How much room is left? */
58917c478bd9Sstevel@tonic-gate 	cp = strchr(dst, '\0');
58927c478bd9Sstevel@tonic-gate 	if (dst + dstlen + 1 > cp) {
58937c478bd9Sstevel@tonic-gate 		*cp++ = '.';
58947c478bd9Sstevel@tonic-gate 		dstlen -= (cp - dst);
58957c478bd9Sstevel@tonic-gate 		dstlen--;
58967c478bd9Sstevel@tonic-gate 		(void) portname(port, proto, cp, dstlen);
58977c478bd9Sstevel@tonic-gate 	}
58987c478bd9Sstevel@tonic-gate 	return (dst);
58997c478bd9Sstevel@tonic-gate }
59007c478bd9Sstevel@tonic-gate 
59017c478bd9Sstevel@tonic-gate /*
59027c478bd9Sstevel@tonic-gate  * Return the name of the network whose address is given. The address is
59037c478bd9Sstevel@tonic-gate  * assumed to be that of a net or subnet, not a host.
59047c478bd9Sstevel@tonic-gate  */
59057c478bd9Sstevel@tonic-gate static char *
59067c478bd9Sstevel@tonic-gate pr_net(uint_t addr, uint_t mask, char *dst, uint_t dstlen)
59077c478bd9Sstevel@tonic-gate {
59087c478bd9Sstevel@tonic-gate 	char		*cp = NULL;
59097c478bd9Sstevel@tonic-gate 	struct netent	*np = NULL;
59107c478bd9Sstevel@tonic-gate 	struct hostent	*hp = NULL;
59117c478bd9Sstevel@tonic-gate 	uint_t		net;
59127c478bd9Sstevel@tonic-gate 	int		subnetshift;
59137c478bd9Sstevel@tonic-gate 	int		error_num;
59147c478bd9Sstevel@tonic-gate 
59157c478bd9Sstevel@tonic-gate 	if (addr == INADDR_ANY && mask == INADDR_ANY) {
59167c478bd9Sstevel@tonic-gate 		(void) strncpy(dst, "default", dstlen);
59177c478bd9Sstevel@tonic-gate 		dst[dstlen - 1] = 0;
59187c478bd9Sstevel@tonic-gate 		return (dst);
59197c478bd9Sstevel@tonic-gate 	}
59207c478bd9Sstevel@tonic-gate 
59217c478bd9Sstevel@tonic-gate 	if (!Nflag && addr) {
59227c478bd9Sstevel@tonic-gate 		if (mask == 0) {
59237c478bd9Sstevel@tonic-gate 			if (IN_CLASSA(addr)) {
59247c478bd9Sstevel@tonic-gate 				mask = (uint_t)IN_CLASSA_NET;
59257c478bd9Sstevel@tonic-gate 				subnetshift = 8;
59267c478bd9Sstevel@tonic-gate 			} else if (IN_CLASSB(addr)) {
59277c478bd9Sstevel@tonic-gate 				mask = (uint_t)IN_CLASSB_NET;
59287c478bd9Sstevel@tonic-gate 				subnetshift = 8;
59297c478bd9Sstevel@tonic-gate 			} else {
59307c478bd9Sstevel@tonic-gate 				mask = (uint_t)IN_CLASSC_NET;
59317c478bd9Sstevel@tonic-gate 				subnetshift = 4;
59327c478bd9Sstevel@tonic-gate 			}
59337c478bd9Sstevel@tonic-gate 			/*
59347c478bd9Sstevel@tonic-gate 			 * If there are more bits than the standard mask
59357c478bd9Sstevel@tonic-gate 			 * would suggest, subnets must be in use. Guess at
59367c478bd9Sstevel@tonic-gate 			 * the subnet mask, assuming reasonable width subnet
59377c478bd9Sstevel@tonic-gate 			 * fields.
59387c478bd9Sstevel@tonic-gate 			 */
59397c478bd9Sstevel@tonic-gate 			while (addr & ~mask)
59407c478bd9Sstevel@tonic-gate 				/* compiler doesn't sign extend! */
59417c478bd9Sstevel@tonic-gate 				mask = (mask | ((int)mask >> subnetshift));
59427c478bd9Sstevel@tonic-gate 		}
59437c478bd9Sstevel@tonic-gate 		net = addr & mask;
59447c478bd9Sstevel@tonic-gate 		while ((mask & 1) == 0)
59457c478bd9Sstevel@tonic-gate 			mask >>= 1, net >>= 1;
59467c478bd9Sstevel@tonic-gate 		np = getnetbyaddr(net, AF_INET);
59477c478bd9Sstevel@tonic-gate 		if (np && np->n_net == net)
59487c478bd9Sstevel@tonic-gate 			cp = np->n_name;
59497c478bd9Sstevel@tonic-gate 		else {
59507c478bd9Sstevel@tonic-gate 			/*
59517c478bd9Sstevel@tonic-gate 			 * Look for subnets in hosts map.
59527c478bd9Sstevel@tonic-gate 			 */
59537c478bd9Sstevel@tonic-gate 			hp = getipnodebyaddr((char *)&addr, sizeof (uint_t),
59547c478bd9Sstevel@tonic-gate 			    AF_INET, &error_num);
59557c478bd9Sstevel@tonic-gate 			if (hp)
59567c478bd9Sstevel@tonic-gate 				cp = hp->h_name;
59577c478bd9Sstevel@tonic-gate 		}
59587c478bd9Sstevel@tonic-gate 	}
59597c478bd9Sstevel@tonic-gate 	if (cp != NULL) {
59607c478bd9Sstevel@tonic-gate 		(void) strncpy(dst, cp, dstlen);
59617c478bd9Sstevel@tonic-gate 		dst[dstlen - 1] = 0;
59627c478bd9Sstevel@tonic-gate 	} else {
59637c478bd9Sstevel@tonic-gate 		(void) inet_ntop(AF_INET, (char *)&addr, dst, dstlen);
59647c478bd9Sstevel@tonic-gate 	}
59657c478bd9Sstevel@tonic-gate 	if (hp != NULL)
59667c478bd9Sstevel@tonic-gate 		freehostent(hp);
59677c478bd9Sstevel@tonic-gate 	return (dst);
59687c478bd9Sstevel@tonic-gate }
59697c478bd9Sstevel@tonic-gate 
59707c478bd9Sstevel@tonic-gate /*
59717c478bd9Sstevel@tonic-gate  * Return the name of the network whose address is given.
59727c478bd9Sstevel@tonic-gate  * The address is assumed to be a host address.
59737c478bd9Sstevel@tonic-gate  */
59747c478bd9Sstevel@tonic-gate static char *
59757c478bd9Sstevel@tonic-gate pr_netaddr(uint_t addr, uint_t mask, char *dst, uint_t dstlen)
59767c478bd9Sstevel@tonic-gate {
59777c478bd9Sstevel@tonic-gate 	char		*cp = NULL;
59787c478bd9Sstevel@tonic-gate 	struct netent	*np = NULL;
59797c478bd9Sstevel@tonic-gate 	struct hostent	*hp = NULL;
59807c478bd9Sstevel@tonic-gate 	uint_t		net;
59817c478bd9Sstevel@tonic-gate 	uint_t		netshifted;
59827c478bd9Sstevel@tonic-gate 	int		subnetshift;
59837c478bd9Sstevel@tonic-gate 	struct in_addr in;
59847c478bd9Sstevel@tonic-gate 	int		error_num;
59857c478bd9Sstevel@tonic-gate 	uint_t		nbo_addr = addr;	/* network byte order */
59867c478bd9Sstevel@tonic-gate 
59877c478bd9Sstevel@tonic-gate 	addr = ntohl(addr);
59887c478bd9Sstevel@tonic-gate 	mask = ntohl(mask);
59897c478bd9Sstevel@tonic-gate 	if (addr == INADDR_ANY && mask == INADDR_ANY) {
59907c478bd9Sstevel@tonic-gate 		(void) strncpy(dst, "default", dstlen);
59917c478bd9Sstevel@tonic-gate 		dst[dstlen - 1] = 0;
59927c478bd9Sstevel@tonic-gate 		return (dst);
59937c478bd9Sstevel@tonic-gate 	}
59947c478bd9Sstevel@tonic-gate 
59957c478bd9Sstevel@tonic-gate 	/* Figure out network portion of address (with host portion = 0) */
59967c478bd9Sstevel@tonic-gate 	if (addr) {
59977c478bd9Sstevel@tonic-gate 		/* Try figuring out mask if unknown (all 0s). */
59987c478bd9Sstevel@tonic-gate 		if (mask == 0) {
59997c478bd9Sstevel@tonic-gate 			if (IN_CLASSA(addr)) {
60007c478bd9Sstevel@tonic-gate 				mask = (uint_t)IN_CLASSA_NET;
60017c478bd9Sstevel@tonic-gate 				subnetshift = 8;
60027c478bd9Sstevel@tonic-gate 			} else if (IN_CLASSB(addr)) {
60037c478bd9Sstevel@tonic-gate 				mask = (uint_t)IN_CLASSB_NET;
60047c478bd9Sstevel@tonic-gate 				subnetshift = 8;
60057c478bd9Sstevel@tonic-gate 			} else {
60067c478bd9Sstevel@tonic-gate 				mask = (uint_t)IN_CLASSC_NET;
60077c478bd9Sstevel@tonic-gate 				subnetshift = 4;
60087c478bd9Sstevel@tonic-gate 			}
60097c478bd9Sstevel@tonic-gate 			/*
60107c478bd9Sstevel@tonic-gate 			 * If there are more bits than the standard mask
60117c478bd9Sstevel@tonic-gate 			 * would suggest, subnets must be in use. Guess at
60127c478bd9Sstevel@tonic-gate 			 * the subnet mask, assuming reasonable width subnet
60137c478bd9Sstevel@tonic-gate 			 * fields.
60147c478bd9Sstevel@tonic-gate 			 */
60157c478bd9Sstevel@tonic-gate 			while (addr & ~mask)
60167c478bd9Sstevel@tonic-gate 				/* compiler doesn't sign extend! */
60177c478bd9Sstevel@tonic-gate 				mask = (mask | ((int)mask >> subnetshift));
60187c478bd9Sstevel@tonic-gate 		}
60197c478bd9Sstevel@tonic-gate 		net = netshifted = addr & mask;
60207c478bd9Sstevel@tonic-gate 		while ((mask & 1) == 0)
60217c478bd9Sstevel@tonic-gate 			mask >>= 1, netshifted >>= 1;
60227c478bd9Sstevel@tonic-gate 	}
60237c478bd9Sstevel@tonic-gate 	else
60247c478bd9Sstevel@tonic-gate 		net = netshifted = 0;
60257c478bd9Sstevel@tonic-gate 
60267c478bd9Sstevel@tonic-gate 	/* Try looking up name unless -n was specified. */
60277c478bd9Sstevel@tonic-gate 	if (!Nflag) {
60287c478bd9Sstevel@tonic-gate 		np = getnetbyaddr(netshifted, AF_INET);
60297c478bd9Sstevel@tonic-gate 		if (np && np->n_net == netshifted)
60307c478bd9Sstevel@tonic-gate 			cp = np->n_name;
60317c478bd9Sstevel@tonic-gate 		else {
60327c478bd9Sstevel@tonic-gate 			/*
60337c478bd9Sstevel@tonic-gate 			 * Look for subnets in hosts map.
60347c478bd9Sstevel@tonic-gate 			 */
60357c478bd9Sstevel@tonic-gate 			hp = getipnodebyaddr((char *)&nbo_addr, sizeof (uint_t),
60367c478bd9Sstevel@tonic-gate 			    AF_INET, &error_num);
60377c478bd9Sstevel@tonic-gate 			if (hp)
60387c478bd9Sstevel@tonic-gate 				cp = hp->h_name;
60397c478bd9Sstevel@tonic-gate 		}
60407c478bd9Sstevel@tonic-gate 
60417c478bd9Sstevel@tonic-gate 		if (cp != NULL) {
60427c478bd9Sstevel@tonic-gate 			(void) strncpy(dst, cp, dstlen);
60437c478bd9Sstevel@tonic-gate 			dst[dstlen - 1] = 0;
60447c478bd9Sstevel@tonic-gate 			if (hp != NULL)
60457c478bd9Sstevel@tonic-gate 				freehostent(hp);
60467c478bd9Sstevel@tonic-gate 			return (dst);
60477c478bd9Sstevel@tonic-gate 		}
60487c478bd9Sstevel@tonic-gate 		/*
60497c478bd9Sstevel@tonic-gate 		 * No name found for net: fallthru and return in decimal
60507c478bd9Sstevel@tonic-gate 		 * dot notation.
60517c478bd9Sstevel@tonic-gate 		 */
60527c478bd9Sstevel@tonic-gate 	}
60537c478bd9Sstevel@tonic-gate 
60547c478bd9Sstevel@tonic-gate 	in.s_addr = htonl(net);
60557c478bd9Sstevel@tonic-gate 	(void) inet_ntop(AF_INET, (char *)&in, dst, dstlen);
60567c478bd9Sstevel@tonic-gate 	if (hp != NULL)
60577c478bd9Sstevel@tonic-gate 		freehostent(hp);
60587c478bd9Sstevel@tonic-gate 	return (dst);
60597c478bd9Sstevel@tonic-gate }
60607c478bd9Sstevel@tonic-gate 
60617c478bd9Sstevel@tonic-gate /*
60627c478bd9Sstevel@tonic-gate  * Return the filter mode as a string:
60637c478bd9Sstevel@tonic-gate  *	1 => "INCLUDE"
60647c478bd9Sstevel@tonic-gate  *	2 => "EXCLUDE"
60657c478bd9Sstevel@tonic-gate  *	otherwise "<unknown>"
60667c478bd9Sstevel@tonic-gate  */
60677c478bd9Sstevel@tonic-gate static char *
60687c478bd9Sstevel@tonic-gate fmodestr(uint_t fmode)
60697c478bd9Sstevel@tonic-gate {
60707c478bd9Sstevel@tonic-gate 	switch (fmode) {
60717c478bd9Sstevel@tonic-gate 	case 1:
60727c478bd9Sstevel@tonic-gate 		return ("INCLUDE");
60737c478bd9Sstevel@tonic-gate 	case 2:
60747c478bd9Sstevel@tonic-gate 		return ("EXCLUDE");
60757c478bd9Sstevel@tonic-gate 	default:
60767c478bd9Sstevel@tonic-gate 		return ("<unknown>");
60777c478bd9Sstevel@tonic-gate 	}
60787c478bd9Sstevel@tonic-gate }
60797c478bd9Sstevel@tonic-gate 
608045916cd2Sjpk #define	MAX_STRING_SIZE	256
608145916cd2Sjpk 
608245916cd2Sjpk static const char *
608345916cd2Sjpk pr_secattr(const sec_attr_list_t *attrs)
608445916cd2Sjpk {
608545916cd2Sjpk 	int i;
608645916cd2Sjpk 	char buf[MAX_STRING_SIZE + 1], *cp;
608745916cd2Sjpk 	static char *sbuf;
608845916cd2Sjpk 	static size_t sbuf_len;
608945916cd2Sjpk 	struct rtsa_s rtsa;
609045916cd2Sjpk 	const sec_attr_list_t *aptr;
609145916cd2Sjpk 
609245916cd2Sjpk 	if (!RSECflag || attrs == NULL)
609345916cd2Sjpk 		return ("");
609445916cd2Sjpk 
609545916cd2Sjpk 	for (aptr = attrs, i = 1; aptr != NULL; aptr = aptr->sal_next)
609645916cd2Sjpk 		i += MAX_STRING_SIZE;
609745916cd2Sjpk 	if (i > sbuf_len) {
609845916cd2Sjpk 		cp = realloc(sbuf, i);
609945916cd2Sjpk 		if (cp == NULL) {
610045916cd2Sjpk 			perror("realloc security attribute buffer");
610145916cd2Sjpk 			return ("");
610245916cd2Sjpk 		}
610345916cd2Sjpk 		sbuf_len = i;
610445916cd2Sjpk 		sbuf = cp;
610545916cd2Sjpk 	}
610645916cd2Sjpk 
610745916cd2Sjpk 	cp = sbuf;
610845916cd2Sjpk 	while (attrs != NULL) {
610945916cd2Sjpk 		const mib2_ipAttributeEntry_t *iae = attrs->sal_attr;
611045916cd2Sjpk 
611145916cd2Sjpk 		/* note: effectively hard-coded in rtsa_keyword */
611245916cd2Sjpk 		rtsa.rtsa_mask = RTSA_CIPSO | RTSA_SLRANGE | RTSA_DOI;
611345916cd2Sjpk 		rtsa.rtsa_slrange = iae->iae_slrange;
611445916cd2Sjpk 		rtsa.rtsa_doi = iae->iae_doi;
611545916cd2Sjpk 
611645916cd2Sjpk 		(void) snprintf(cp, MAX_STRING_SIZE,
611745916cd2Sjpk 		    "<%s>%s ", rtsa_to_str(&rtsa, buf, sizeof (buf)),
611845916cd2Sjpk 		    attrs->sal_next == NULL ? "" : ",");
611945916cd2Sjpk 		cp += strlen(cp);
612045916cd2Sjpk 		attrs = attrs->sal_next;
612145916cd2Sjpk 	}
612245916cd2Sjpk 	*cp = '\0';
612345916cd2Sjpk 
612445916cd2Sjpk 	return (sbuf);
612545916cd2Sjpk }
612645916cd2Sjpk 
61277c478bd9Sstevel@tonic-gate /*
61287c478bd9Sstevel@tonic-gate  * Pretty print a port number. If the Nflag was
61297c478bd9Sstevel@tonic-gate  * specified, use numbers instead of names.
61307c478bd9Sstevel@tonic-gate  */
61317c478bd9Sstevel@tonic-gate static char *
61327c478bd9Sstevel@tonic-gate portname(uint_t port, char *proto, char *dst, uint_t dstlen)
61337c478bd9Sstevel@tonic-gate {
61347c478bd9Sstevel@tonic-gate 	struct servent *sp = NULL;
61357c478bd9Sstevel@tonic-gate 
61367c478bd9Sstevel@tonic-gate 	if (!Nflag && port)
61377c478bd9Sstevel@tonic-gate 		sp = getservbyport(htons(port), proto);
61387c478bd9Sstevel@tonic-gate 	if (sp || port == 0)
61397c478bd9Sstevel@tonic-gate 		(void) snprintf(dst, dstlen, "%.*s", MAXHOSTNAMELEN,
6140e11c3f44Smeem 		    sp ? sp->s_name : "*");
61417c478bd9Sstevel@tonic-gate 	else
61427c478bd9Sstevel@tonic-gate 		(void) snprintf(dst, dstlen, "%d", port);
61437c478bd9Sstevel@tonic-gate 	dst[dstlen - 1] = 0;
61447c478bd9Sstevel@tonic-gate 	return (dst);
61457c478bd9Sstevel@tonic-gate }
61467c478bd9Sstevel@tonic-gate 
61477c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/
61487c478bd9Sstevel@tonic-gate void
61497c478bd9Sstevel@tonic-gate fail(int do_perror, char *message, ...)
61507c478bd9Sstevel@tonic-gate {
61517c478bd9Sstevel@tonic-gate 	va_list args;
61527c478bd9Sstevel@tonic-gate 
61537c478bd9Sstevel@tonic-gate 	va_start(args, message);
61547c478bd9Sstevel@tonic-gate 	(void) fputs("netstat: ", stderr);
61557c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, message, args);
61567c478bd9Sstevel@tonic-gate 	va_end(args);
61577c478bd9Sstevel@tonic-gate 	if (do_perror)
61587c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, ": %s", strerror(errno));
61597c478bd9Sstevel@tonic-gate 	(void) fputc('\n', stderr);
61607c478bd9Sstevel@tonic-gate 	exit(2);
61617c478bd9Sstevel@tonic-gate }
61627c478bd9Sstevel@tonic-gate 
61637c478bd9Sstevel@tonic-gate /*
61647c478bd9Sstevel@tonic-gate  * Return value of named statistic for given kstat_named kstat;
61657c478bd9Sstevel@tonic-gate  * return 0LL if named statistic is not in list (use "ll" as a
61667c478bd9Sstevel@tonic-gate  * type qualifier when printing 64-bit int's with printf() )
61677c478bd9Sstevel@tonic-gate  */
61687c478bd9Sstevel@tonic-gate static uint64_t
61697c478bd9Sstevel@tonic-gate kstat_named_value(kstat_t *ksp, char *name)
61707c478bd9Sstevel@tonic-gate {
61717c478bd9Sstevel@tonic-gate 	kstat_named_t *knp;
61727c478bd9Sstevel@tonic-gate 	uint64_t value;
61737c478bd9Sstevel@tonic-gate 
61747c478bd9Sstevel@tonic-gate 	if (ksp == NULL)
61757c478bd9Sstevel@tonic-gate 		return (0LL);
61767c478bd9Sstevel@tonic-gate 
61777c478bd9Sstevel@tonic-gate 	knp = kstat_data_lookup(ksp, name);
61787c478bd9Sstevel@tonic-gate 	if (knp == NULL)
61797c478bd9Sstevel@tonic-gate 		return (0LL);
61807c478bd9Sstevel@tonic-gate 
61817c478bd9Sstevel@tonic-gate 	switch (knp->data_type) {
61827c478bd9Sstevel@tonic-gate 	case KSTAT_DATA_INT32:
61837c478bd9Sstevel@tonic-gate 	case KSTAT_DATA_UINT32:
61847c478bd9Sstevel@tonic-gate 		value = (uint64_t)(knp->value.ui32);
61857c478bd9Sstevel@tonic-gate 		break;
61867c478bd9Sstevel@tonic-gate 	case KSTAT_DATA_INT64:
61877c478bd9Sstevel@tonic-gate 	case KSTAT_DATA_UINT64:
61887c478bd9Sstevel@tonic-gate 		value = knp->value.ui64;
61897c478bd9Sstevel@tonic-gate 		break;
61907c478bd9Sstevel@tonic-gate 	default:
61917c478bd9Sstevel@tonic-gate 		value = 0LL;
61927c478bd9Sstevel@tonic-gate 		break;
61937c478bd9Sstevel@tonic-gate 	}
61947c478bd9Sstevel@tonic-gate 
61957c478bd9Sstevel@tonic-gate 	return (value);
61967c478bd9Sstevel@tonic-gate }
61977c478bd9Sstevel@tonic-gate 
61987c478bd9Sstevel@tonic-gate kid_t
61997c478bd9Sstevel@tonic-gate safe_kstat_read(kstat_ctl_t *kc, kstat_t *ksp, void *data)
62007c478bd9Sstevel@tonic-gate {
62017c478bd9Sstevel@tonic-gate 	kid_t kstat_chain_id = kstat_read(kc, ksp, data);
62027c478bd9Sstevel@tonic-gate 
62037c478bd9Sstevel@tonic-gate 	if (kstat_chain_id == -1)
62047c478bd9Sstevel@tonic-gate 		fail(1, "kstat_read(%p, '%s') failed", (void *)kc,
62057c478bd9Sstevel@tonic-gate 		    ksp->ks_name);
62067c478bd9Sstevel@tonic-gate 	return (kstat_chain_id);
62077c478bd9Sstevel@tonic-gate }
62087c478bd9Sstevel@tonic-gate 
62097c478bd9Sstevel@tonic-gate /*
62107c478bd9Sstevel@tonic-gate  * Parse a list of IRE flag characters into a bit field.
62117c478bd9Sstevel@tonic-gate  */
62127c478bd9Sstevel@tonic-gate static uint_t
62137c478bd9Sstevel@tonic-gate flag_bits(const char *arg)
62147c478bd9Sstevel@tonic-gate {
62157c478bd9Sstevel@tonic-gate 	const char *cp;
62167c478bd9Sstevel@tonic-gate 	uint_t val;
62177c478bd9Sstevel@tonic-gate 
62187c478bd9Sstevel@tonic-gate 	if (*arg == '\0')
62197c478bd9Sstevel@tonic-gate 		fatal(1, "missing flag list\n");
62207c478bd9Sstevel@tonic-gate 
62217c478bd9Sstevel@tonic-gate 	val = 0;
62227c478bd9Sstevel@tonic-gate 	while (*arg != '\0') {
62237c478bd9Sstevel@tonic-gate 		if ((cp = strchr(flag_list, *arg)) == NULL)
62247c478bd9Sstevel@tonic-gate 			fatal(1, "%c: illegal flag\n", *arg);
62257c478bd9Sstevel@tonic-gate 		val |= 1 << (cp - flag_list);
62267c478bd9Sstevel@tonic-gate 		arg++;
62277c478bd9Sstevel@tonic-gate 	}
62287c478bd9Sstevel@tonic-gate 	return (val);
62297c478bd9Sstevel@tonic-gate }
62307c478bd9Sstevel@tonic-gate 
62317c478bd9Sstevel@tonic-gate /*
62327c478bd9Sstevel@tonic-gate  * Handle -f argument.  Validate input format, sort by keyword, and
62337c478bd9Sstevel@tonic-gate  * save off digested results.
62347c478bd9Sstevel@tonic-gate  */
62357c478bd9Sstevel@tonic-gate static void
62367c478bd9Sstevel@tonic-gate process_filter(char *arg)
62377c478bd9Sstevel@tonic-gate {
62387c478bd9Sstevel@tonic-gate 	int idx;
62397c478bd9Sstevel@tonic-gate 	int klen = 0;
62407c478bd9Sstevel@tonic-gate 	char *cp, *cp2;
62417c478bd9Sstevel@tonic-gate 	int val;
62427c478bd9Sstevel@tonic-gate 	filter_t *newf;
62437c478bd9Sstevel@tonic-gate 	struct hostent *hp;
62447c478bd9Sstevel@tonic-gate 	int error_num;
62457c478bd9Sstevel@tonic-gate 	uint8_t *ucp;
62467c478bd9Sstevel@tonic-gate 	int maxv;
62477c478bd9Sstevel@tonic-gate 
62487c478bd9Sstevel@tonic-gate 	/* Look up the keyword first */
62497c478bd9Sstevel@tonic-gate 	if (strchr(arg, ':') == NULL) {
62507c478bd9Sstevel@tonic-gate 		idx = FK_AF;
62517c478bd9Sstevel@tonic-gate 	} else {
62527c478bd9Sstevel@tonic-gate 		for (idx = 0; idx < NFILTERKEYS; idx++) {
62537c478bd9Sstevel@tonic-gate 			klen = strlen(filter_keys[idx]);
62547c478bd9Sstevel@tonic-gate 			if (strncmp(filter_keys[idx], arg, klen) == 0 &&
62557c478bd9Sstevel@tonic-gate 			    arg[klen] == ':')
62567c478bd9Sstevel@tonic-gate 				break;
62577c478bd9Sstevel@tonic-gate 		}
62587c478bd9Sstevel@tonic-gate 		if (idx >= NFILTERKEYS)
62597c478bd9Sstevel@tonic-gate 			fatal(1, "%s: unknown filter keyword\n", arg);
62607c478bd9Sstevel@tonic-gate 
62617c478bd9Sstevel@tonic-gate 		/* Advance past keyword and separator. */
62627c478bd9Sstevel@tonic-gate 		arg += klen + 1;
62637c478bd9Sstevel@tonic-gate 	}
62647c478bd9Sstevel@tonic-gate 
62657c478bd9Sstevel@tonic-gate 	if ((newf = malloc(sizeof (*newf))) == NULL) {
62667c478bd9Sstevel@tonic-gate 		perror("filter");
62677c478bd9Sstevel@tonic-gate 		exit(1);
62687c478bd9Sstevel@tonic-gate 	}
62697c478bd9Sstevel@tonic-gate 	switch (idx) {
62707c478bd9Sstevel@tonic-gate 	case FK_AF:
62717c478bd9Sstevel@tonic-gate 		if (strcmp(arg, "inet") == 0) {
62727c478bd9Sstevel@tonic-gate 			newf->u.f_family = AF_INET;
62737c478bd9Sstevel@tonic-gate 		} else if (strcmp(arg, "inet6") == 0) {
62747c478bd9Sstevel@tonic-gate 			newf->u.f_family = AF_INET6;
62757c478bd9Sstevel@tonic-gate 		} else if (strcmp(arg, "unix") == 0) {
62767c478bd9Sstevel@tonic-gate 			newf->u.f_family = AF_UNIX;
62777c478bd9Sstevel@tonic-gate 		} else {
62787c478bd9Sstevel@tonic-gate 			newf->u.f_family = strtol(arg, &cp, 0);
62797c478bd9Sstevel@tonic-gate 			if (arg == cp || *cp != '\0')
62807c478bd9Sstevel@tonic-gate 				fatal(1, "%s: unknown address family.\n", arg);
62817c478bd9Sstevel@tonic-gate 		}
62827c478bd9Sstevel@tonic-gate 		break;
62837c478bd9Sstevel@tonic-gate 
62847c478bd9Sstevel@tonic-gate 	case FK_OUTIF:
62857c478bd9Sstevel@tonic-gate 		if (strcmp(arg, "none") == 0) {
62867c478bd9Sstevel@tonic-gate 			newf->u.f_ifname = NULL;
62877c478bd9Sstevel@tonic-gate 			break;
62887c478bd9Sstevel@tonic-gate 		}
62897c478bd9Sstevel@tonic-gate 		if (strcmp(arg, "any") == 0) {
62907c478bd9Sstevel@tonic-gate 			newf->u.f_ifname = "";
62917c478bd9Sstevel@tonic-gate 			break;
62927c478bd9Sstevel@tonic-gate 		}
62937c478bd9Sstevel@tonic-gate 		val = strtol(arg, &cp, 0);
62947c478bd9Sstevel@tonic-gate 		if (val <= 0 || arg == cp || cp[0] != '\0') {
62957c478bd9Sstevel@tonic-gate 			if ((val = if_nametoindex(arg)) == 0) {
62967c478bd9Sstevel@tonic-gate 				perror(arg);
62977c478bd9Sstevel@tonic-gate 				exit(1);
62987c478bd9Sstevel@tonic-gate 			}
62997c478bd9Sstevel@tonic-gate 		}
63007c478bd9Sstevel@tonic-gate 		newf->u.f_ifname = arg;
63017c478bd9Sstevel@tonic-gate 		break;
63027c478bd9Sstevel@tonic-gate 
63037c478bd9Sstevel@tonic-gate 	case FK_DST:
63047c478bd9Sstevel@tonic-gate 		V4MASK_TO_V6(IP_HOST_MASK, newf->u.a.f_mask);
63057c478bd9Sstevel@tonic-gate 		if (strcmp(arg, "any") == 0) {
63067c478bd9Sstevel@tonic-gate 			/* Special semantics; any address *but* zero */
63077c478bd9Sstevel@tonic-gate 			newf->u.a.f_address = NULL;
63087c478bd9Sstevel@tonic-gate 			(void) memset(&newf->u.a.f_mask, 0,
63097c478bd9Sstevel@tonic-gate 			    sizeof (newf->u.a.f_mask));
63107c478bd9Sstevel@tonic-gate 			break;
63117c478bd9Sstevel@tonic-gate 		}
63127c478bd9Sstevel@tonic-gate 		if (strcmp(arg, "none") == 0) {
63137c478bd9Sstevel@tonic-gate 			newf->u.a.f_address = NULL;
63147c478bd9Sstevel@tonic-gate 			break;
63157c478bd9Sstevel@tonic-gate 		}
63167c478bd9Sstevel@tonic-gate 		if ((cp = strrchr(arg, '/')) != NULL)
63177c478bd9Sstevel@tonic-gate 			*cp++ = '\0';
63187c478bd9Sstevel@tonic-gate 		hp = getipnodebyname(arg, AF_INET6, AI_V4MAPPED|AI_ALL,
63197c478bd9Sstevel@tonic-gate 		    &error_num);
63207c478bd9Sstevel@tonic-gate 		if (hp == NULL)
63217c478bd9Sstevel@tonic-gate 			fatal(1, "%s: invalid or unknown host address\n", arg);
63227c478bd9Sstevel@tonic-gate 		newf->u.a.f_address = hp;
63237c478bd9Sstevel@tonic-gate 		if (cp == NULL) {
63247c478bd9Sstevel@tonic-gate 			V4MASK_TO_V6(IP_HOST_MASK, newf->u.a.f_mask);
63257c478bd9Sstevel@tonic-gate 		} else {
63267c478bd9Sstevel@tonic-gate 			val = strtol(cp, &cp2, 0);
63277c478bd9Sstevel@tonic-gate 			if (cp != cp2 && cp2[0] == '\0') {
63287c478bd9Sstevel@tonic-gate 				/*
63297c478bd9Sstevel@tonic-gate 				 * If decode as "/n" works, then translate
63307c478bd9Sstevel@tonic-gate 				 * into a mask.
63317c478bd9Sstevel@tonic-gate 				 */
63327c478bd9Sstevel@tonic-gate 				if (hp->h_addr_list[0] != NULL &&
63337c478bd9Sstevel@tonic-gate 				    /* LINTED: (note 1) */
6334e11c3f44Smeem 				    IN6_IS_ADDR_V4MAPPED((in6_addr_t *)
6335e11c3f44Smeem 				    hp->h_addr_list[0])) {
63367c478bd9Sstevel@tonic-gate 					maxv = IP_ABITS;
63377c478bd9Sstevel@tonic-gate 				} else {
63387c478bd9Sstevel@tonic-gate 					maxv = IPV6_ABITS;
63397c478bd9Sstevel@tonic-gate 				}
63407c478bd9Sstevel@tonic-gate 				if (val < 0 || val >= maxv)
63417c478bd9Sstevel@tonic-gate 					fatal(1, "%d: not in range 0 to %d\n",
63427c478bd9Sstevel@tonic-gate 					    val, maxv - 1);
63437c478bd9Sstevel@tonic-gate 				if (maxv == IP_ABITS)
63447c478bd9Sstevel@tonic-gate 					val += IPV6_ABITS - IP_ABITS;
63457c478bd9Sstevel@tonic-gate 				ucp = newf->u.a.f_mask.s6_addr;
63467c478bd9Sstevel@tonic-gate 				while (val >= 8)
63477c478bd9Sstevel@tonic-gate 					*ucp++ = 0xff, val -= 8;
63487c478bd9Sstevel@tonic-gate 				*ucp++ = (0xff << (8 - val)) & 0xff;
63497c478bd9Sstevel@tonic-gate 				while (ucp < newf->u.a.f_mask.s6_addr +
63507c478bd9Sstevel@tonic-gate 				    sizeof (newf->u.a.f_mask.s6_addr))
63517c478bd9Sstevel@tonic-gate 					*ucp++ = 0;
63527c478bd9Sstevel@tonic-gate 				/* Otherwise, try as numeric address */
63537c478bd9Sstevel@tonic-gate 			} else if (inet_pton(AF_INET6,
63547c478bd9Sstevel@tonic-gate 			    cp, &newf->u.a.f_mask) <= 0) {
63557c478bd9Sstevel@tonic-gate 				fatal(1, "%s: illegal mask format\n", cp);
63567c478bd9Sstevel@tonic-gate 			}
63577c478bd9Sstevel@tonic-gate 		}
63587c478bd9Sstevel@tonic-gate 		break;
63597c478bd9Sstevel@tonic-gate 
63607c478bd9Sstevel@tonic-gate 	case FK_FLAGS:
63617c478bd9Sstevel@tonic-gate 		if (*arg == '+') {
63627c478bd9Sstevel@tonic-gate 			newf->u.f.f_flagset = flag_bits(arg + 1);
63637c478bd9Sstevel@tonic-gate 			newf->u.f.f_flagclear = 0;
63647c478bd9Sstevel@tonic-gate 		} else if (*arg == '-') {
63657c478bd9Sstevel@tonic-gate 			newf->u.f.f_flagset = 0;
63667c478bd9Sstevel@tonic-gate 			newf->u.f.f_flagclear = flag_bits(arg + 1);
63677c478bd9Sstevel@tonic-gate 		} else {
63687c478bd9Sstevel@tonic-gate 			newf->u.f.f_flagset = flag_bits(arg);
63697c478bd9Sstevel@tonic-gate 			newf->u.f.f_flagclear = ~newf->u.f.f_flagset;
63707c478bd9Sstevel@tonic-gate 		}
63717c478bd9Sstevel@tonic-gate 		break;
63727c478bd9Sstevel@tonic-gate 
63737c478bd9Sstevel@tonic-gate 	default:
63747c478bd9Sstevel@tonic-gate 		assert(0);
63757c478bd9Sstevel@tonic-gate 	}
63767c478bd9Sstevel@tonic-gate 	newf->f_next = filters[idx];
63777c478bd9Sstevel@tonic-gate 	filters[idx] = newf;
63787c478bd9Sstevel@tonic-gate }
63797c478bd9Sstevel@tonic-gate 
63807c478bd9Sstevel@tonic-gate /* Determine if user wants this address family printed. */
63817c478bd9Sstevel@tonic-gate static boolean_t
63827c478bd9Sstevel@tonic-gate family_selected(int family)
63837c478bd9Sstevel@tonic-gate {
63847c478bd9Sstevel@tonic-gate 	const filter_t *fp;
63857c478bd9Sstevel@tonic-gate 
63867c478bd9Sstevel@tonic-gate 	if (v4compat && family == AF_INET6)
63877c478bd9Sstevel@tonic-gate 		return (B_FALSE);
63887c478bd9Sstevel@tonic-gate 	if ((fp = filters[FK_AF]) == NULL)
63897c478bd9Sstevel@tonic-gate 		return (B_TRUE);
63907c478bd9Sstevel@tonic-gate 	while (fp != NULL) {
63917c478bd9Sstevel@tonic-gate 		if (fp->u.f_family == family)
63927c478bd9Sstevel@tonic-gate 			return (B_TRUE);
63937c478bd9Sstevel@tonic-gate 		fp = fp->f_next;
63947c478bd9Sstevel@tonic-gate 	}
63957c478bd9Sstevel@tonic-gate 	return (B_FALSE);
63967c478bd9Sstevel@tonic-gate }
63977c478bd9Sstevel@tonic-gate 
6398e11c3f44Smeem /*
6399e11c3f44Smeem  * Convert the interface index to a string using the buffer `ifname', which
6400e11c3f44Smeem  * must be at least LIFNAMSIZ bytes.  We first try to map it to name.  If that
6401e11c3f44Smeem  * fails (e.g., because we're inside a zone and it does not have access to
6402e11c3f44Smeem  * interface for the index in question), just return "if#<num>".
6403e11c3f44Smeem  */
6404e11c3f44Smeem static char *
6405e11c3f44Smeem ifindex2str(uint_t ifindex, char *ifname)
6406e11c3f44Smeem {
6407e11c3f44Smeem 	if (if_indextoname(ifindex, ifname) == NULL)
6408e11c3f44Smeem 		(void) snprintf(ifname, LIFNAMSIZ, "if#%d", ifindex);
6409e11c3f44Smeem 
6410e11c3f44Smeem 	return (ifname);
6411e11c3f44Smeem }
6412e11c3f44Smeem 
64137c478bd9Sstevel@tonic-gate /*
64147c478bd9Sstevel@tonic-gate  * print the usage line
64157c478bd9Sstevel@tonic-gate  */
64167c478bd9Sstevel@tonic-gate static void
64177c478bd9Sstevel@tonic-gate usage(char *cmdname)
64187c478bd9Sstevel@tonic-gate {
641926fd7700SKrishnendu Sadhukhan - Sun Microsystems 	(void) fprintf(stderr, "usage: %s [-anv] [-f address_family] "
642026fd7700SKrishnendu Sadhukhan - Sun Microsystems 	    "[-T d|u]\n", cmdname);
64217c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "       %s [-n] [-f address_family] "
642226fd7700SKrishnendu Sadhukhan - Sun Microsystems 	    "[-P protocol] [-T d|u] [-g | -p | -s [interval [count]]]\n",
642326fd7700SKrishnendu Sadhukhan - Sun Microsystems 	    cmdname);
642426fd7700SKrishnendu Sadhukhan - Sun Microsystems 	(void) fprintf(stderr, "       %s -m [-v] [-T d|u] "
64257c478bd9Sstevel@tonic-gate 	    "[interval [count]]\n", cmdname);
64267c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "       %s -i [-I interface] [-an] "
642726fd7700SKrishnendu Sadhukhan - Sun Microsystems 	    "[-f address_family] [-T d|u] [interval [count]]\n", cmdname);
64287c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "       %s -r [-anv] "
642926fd7700SKrishnendu Sadhukhan - Sun Microsystems 	    "[-f address_family|filter] [-T d|u]\n", cmdname);
643026fd7700SKrishnendu Sadhukhan - Sun Microsystems 	(void) fprintf(stderr, "       %s -M [-ns] [-f address_family] "
643126fd7700SKrishnendu Sadhukhan - Sun Microsystems 	    "[-T d|u]\n", cmdname);
64327c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "       %s -D [-I interface] "
643326fd7700SKrishnendu Sadhukhan - Sun Microsystems 	    "[-f address_family] [-T d|u]\n", cmdname);
64347c478bd9Sstevel@tonic-gate 	exit(EXIT_FAILURE);
64357c478bd9Sstevel@tonic-gate }
64367c478bd9Sstevel@tonic-gate 
64377c478bd9Sstevel@tonic-gate /*
64387c478bd9Sstevel@tonic-gate  * fatal: print error message to stderr and
64397c478bd9Sstevel@tonic-gate  * call exit(errcode)
64407c478bd9Sstevel@tonic-gate  */
64417c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/
64427c478bd9Sstevel@tonic-gate static void
644345916cd2Sjpk fatal(int errcode, char *format, ...)
644445916cd2Sjpk {
64457c478bd9Sstevel@tonic-gate 	va_list argp;
64467c478bd9Sstevel@tonic-gate 
64477c478bd9Sstevel@tonic-gate 	if (format == NULL)
64487c478bd9Sstevel@tonic-gate 		return;
64497c478bd9Sstevel@tonic-gate 
64507c478bd9Sstevel@tonic-gate 	va_start(argp, format);
64517c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, format, argp);
64527c478bd9Sstevel@tonic-gate 	va_end(argp);
64537c478bd9Sstevel@tonic-gate 
64547c478bd9Sstevel@tonic-gate 	exit(errcode);
64557c478bd9Sstevel@tonic-gate }
6456