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 /*
22d04ccbb3Scarlsonj  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * Copyright (c) 1990  Mentat Inc.
287c478bd9Sstevel@tonic-gate  * netstat.c 2.2, last change 9/9/91
297c478bd9Sstevel@tonic-gate  * MROUTING Revision 3.5
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate /*
357c478bd9Sstevel@tonic-gate  * simple netstat based on snmp/mib-2 interface to the TCP/IP stack
367c478bd9Sstevel@tonic-gate  *
377c478bd9Sstevel@tonic-gate  * NOTES:
387c478bd9Sstevel@tonic-gate  * 1. A comment "LINTED: (note 1)" appears before certain lines where
397c478bd9Sstevel@tonic-gate  *    lint would have complained, "pointer cast may result in improper
407c478bd9Sstevel@tonic-gate  *    alignment". These are lines where lint had suspected potential
417c478bd9Sstevel@tonic-gate  *    improper alignment of a data structure; in each such situation
427c478bd9Sstevel@tonic-gate  *    we have relied on the kernel guaranteeing proper alignment.
437c478bd9Sstevel@tonic-gate  * 2. Some 'for' loops have been commented as "'for' loop 1", etc
447c478bd9Sstevel@tonic-gate  *    because they have 'continue' or 'break' statements in their
457c478bd9Sstevel@tonic-gate  *    bodies. 'continue' statements have been used inside some loops
467c478bd9Sstevel@tonic-gate  *    where avoiding them would have led to deep levels of indentation.
477c478bd9Sstevel@tonic-gate  *
487c478bd9Sstevel@tonic-gate  * TODO:
497c478bd9Sstevel@tonic-gate  *	Add ability to request subsets from kernel (with level = MIB2_IP;
507c478bd9Sstevel@tonic-gate  *	name = 0 meaning everything for compatibility)
517c478bd9Sstevel@tonic-gate  */
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #include <stdio.h>
547c478bd9Sstevel@tonic-gate #include <stdlib.h>
557c478bd9Sstevel@tonic-gate #include <stdarg.h>
567c478bd9Sstevel@tonic-gate #include <unistd.h>
577c478bd9Sstevel@tonic-gate #include <strings.h>
587c478bd9Sstevel@tonic-gate #include <string.h>
597c478bd9Sstevel@tonic-gate #include <errno.h>
607c478bd9Sstevel@tonic-gate #include <ctype.h>
617c478bd9Sstevel@tonic-gate #include <kstat.h>
627c478bd9Sstevel@tonic-gate #include <assert.h>
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate #include <sys/types.h>
657c478bd9Sstevel@tonic-gate #include <sys/stream.h>
667c478bd9Sstevel@tonic-gate #include <stropts.h>
677c478bd9Sstevel@tonic-gate #include <sys/strstat.h>
687c478bd9Sstevel@tonic-gate #include <sys/tihdr.h>
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate #include <sys/socket.h>
717c478bd9Sstevel@tonic-gate #include <sys/sockio.h>
727c478bd9Sstevel@tonic-gate #include <netinet/in.h>
737c478bd9Sstevel@tonic-gate #include <net/if.h>
747c478bd9Sstevel@tonic-gate #include <net/route.h>
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate #include <inet/mib2.h>
777c478bd9Sstevel@tonic-gate #include <inet/ip.h>
787c478bd9Sstevel@tonic-gate #include <inet/arp.h>
797c478bd9Sstevel@tonic-gate #include <inet/tcp.h>
807c478bd9Sstevel@tonic-gate #include <netinet/igmp_var.h>
817c478bd9Sstevel@tonic-gate #include <netinet/ip_mroute.h>
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
847c478bd9Sstevel@tonic-gate #include <netdb.h>
857c478bd9Sstevel@tonic-gate #include <fcntl.h>
867c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
877c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate #include <netinet/dhcp.h>
907c478bd9Sstevel@tonic-gate #include <dhcpagent_ipc.h>
917c478bd9Sstevel@tonic-gate #include <dhcpagent_util.h>
927c478bd9Sstevel@tonic-gate #include <compat.h>
937c478bd9Sstevel@tonic-gate 
9445916cd2Sjpk #include <libtsnet.h>
9545916cd2Sjpk #include <tsol/label.h>
9645916cd2Sjpk 
977c478bd9Sstevel@tonic-gate extern void	unixpr(kstat_ctl_t *kc);
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate #define	STR_EXPAND	4
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate #define	V4MASK_TO_V6(v4, v6)	((v6)._S6_un._S6_u32[0] = 0xfffffffful, \
1027c478bd9Sstevel@tonic-gate 				(v6)._S6_un._S6_u32[1] = 0xfffffffful, \
1037c478bd9Sstevel@tonic-gate 				(v6)._S6_un._S6_u32[2] = 0xfffffffful, \
1047c478bd9Sstevel@tonic-gate 				(v6)._S6_un._S6_u32[3] = (v4))
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate #define	IN6_IS_V4MASK(v6)	((v6)._S6_un._S6_u32[0] == 0xfffffffful && \
1077c478bd9Sstevel@tonic-gate 				(v6)._S6_un._S6_u32[1] == 0xfffffffful && \
1087c478bd9Sstevel@tonic-gate 				(v6)._S6_un._S6_u32[2] == 0xfffffffful)
1097c478bd9Sstevel@tonic-gate 
110d04ccbb3Scarlsonj /*
111d04ccbb3Scarlsonj  * This is used as a cushion in the buffer allocation directed by SIOCGLIFNUM.
112d04ccbb3Scarlsonj  * Because there's no locking between SIOCGLIFNUM and SIOCGLIFCONF, it's
113d04ccbb3Scarlsonj  * possible for an administrator to plumb new interfaces between those two
114d04ccbb3Scarlsonj  * calls, resulting in the failure of the latter.  This addition makes that
115d04ccbb3Scarlsonj  * less likely.
116d04ccbb3Scarlsonj  */
117d04ccbb3Scarlsonj #define	LIFN_GUARD_VALUE	10
118d04ccbb3Scarlsonj 
1197c478bd9Sstevel@tonic-gate typedef struct mib_item_s {
1207c478bd9Sstevel@tonic-gate 	struct mib_item_s	*next_item;
1217c478bd9Sstevel@tonic-gate 	int			group;
1227c478bd9Sstevel@tonic-gate 	int			mib_id;
1237c478bd9Sstevel@tonic-gate 	int			length;
1247c478bd9Sstevel@tonic-gate 	void			*valp;
1257c478bd9Sstevel@tonic-gate } mib_item_t;
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate struct	ifstat {
1287c478bd9Sstevel@tonic-gate 	uint64_t	ipackets;
1297c478bd9Sstevel@tonic-gate 	uint64_t	ierrors;
1307c478bd9Sstevel@tonic-gate 	uint64_t	opackets;
1317c478bd9Sstevel@tonic-gate 	uint64_t	oerrors;
1327c478bd9Sstevel@tonic-gate 	uint64_t	collisions;
1337c478bd9Sstevel@tonic-gate };
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate struct iflist {
1367c478bd9Sstevel@tonic-gate 	struct iflist	*next_if;
1377c478bd9Sstevel@tonic-gate 	char		ifname[LIFNAMSIZ];
1387c478bd9Sstevel@tonic-gate 	struct ifstat	tot;
1397c478bd9Sstevel@tonic-gate };
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate static	mib_item_t	*mibget(int sd);
1427c478bd9Sstevel@tonic-gate static	void		mibfree(mib_item_t *firstitem);
1437c478bd9Sstevel@tonic-gate static	int		mibopen(void);
1447c478bd9Sstevel@tonic-gate static void		mib_get_constants(mib_item_t *item);
1457c478bd9Sstevel@tonic-gate static mib_item_t	*mib_item_dup(mib_item_t *item);
1467c478bd9Sstevel@tonic-gate static mib_item_t	*mib_item_diff(mib_item_t *item1,
1477c478bd9Sstevel@tonic-gate     mib_item_t *item2);
1487c478bd9Sstevel@tonic-gate static void		mib_item_destroy(mib_item_t **item);
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate static boolean_t	octetstrmatch(const Octet_t *a, const Octet_t *b);
15145916cd2Sjpk static char		*octetstr(const Octet_t *op, int code,
1527c478bd9Sstevel@tonic-gate 			    char *dst, uint_t dstlen);
1537c478bd9Sstevel@tonic-gate static char		*pr_addr(uint_t addr,
1547c478bd9Sstevel@tonic-gate 			    char *dst, uint_t dstlen);
1557c478bd9Sstevel@tonic-gate static char		*pr_addrnz(ipaddr_t addr, char *dst, uint_t dstlen);
1567c478bd9Sstevel@tonic-gate static char		*pr_addr6(const in6_addr_t *addr,
1577c478bd9Sstevel@tonic-gate 			    char *dst, uint_t dstlen);
1587c478bd9Sstevel@tonic-gate static char		*pr_mask(uint_t addr,
1597c478bd9Sstevel@tonic-gate 			    char *dst, uint_t dstlen);
16045916cd2Sjpk static char		*pr_prefix6(const struct in6_addr *addr,
16145916cd2Sjpk 			    uint_t prefixlen, char *dst, uint_t dstlen);
1627c478bd9Sstevel@tonic-gate static char		*pr_ap(uint_t addr, uint_t port,
1637c478bd9Sstevel@tonic-gate 			    char *proto, char *dst, uint_t dstlen);
1647c478bd9Sstevel@tonic-gate static char		*pr_ap6(const in6_addr_t *addr, uint_t port,
1657c478bd9Sstevel@tonic-gate 			    char *proto, char *dst, uint_t dstlen);
1667c478bd9Sstevel@tonic-gate static char		*pr_net(uint_t addr, uint_t mask,
1677c478bd9Sstevel@tonic-gate 			    char *dst, uint_t dstlen);
1687c478bd9Sstevel@tonic-gate static char		*pr_netaddr(uint_t addr, uint_t mask,
1697c478bd9Sstevel@tonic-gate 			    char *dst, uint_t dstlen);
1707c478bd9Sstevel@tonic-gate static char		*fmodestr(uint_t fmode);
1717c478bd9Sstevel@tonic-gate static char		*portname(uint_t port, char *proto,
1727c478bd9Sstevel@tonic-gate 			    char *dst, uint_t dstlen);
1737c478bd9Sstevel@tonic-gate 
17445916cd2Sjpk static const char	*mitcp_state(int code,
17545916cd2Sjpk 			    const mib2_transportMLPEntry_t *attr);
17645916cd2Sjpk static const char	*miudp_state(int code,
17745916cd2Sjpk 			    const mib2_transportMLPEntry_t *attr);
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate static void		stat_report(mib_item_t *item);
1807c478bd9Sstevel@tonic-gate static void		mrt_stat_report(mib_item_t *item);
1817c478bd9Sstevel@tonic-gate static void		arp_report(mib_item_t *item);
1827c478bd9Sstevel@tonic-gate static void		ndp_report(mib_item_t *item);
1837c478bd9Sstevel@tonic-gate static void		mrt_report(mib_item_t *item);
1847c478bd9Sstevel@tonic-gate static void		if_stat_total(struct ifstat *oldstats,
1857c478bd9Sstevel@tonic-gate 			    struct ifstat *newstats, struct ifstat *sumstats);
1867c478bd9Sstevel@tonic-gate static void		if_report(mib_item_t *item, char *ifname,
1877c478bd9Sstevel@tonic-gate 			    int Iflag_only, boolean_t once_only);
1887c478bd9Sstevel@tonic-gate static void		if_report_ip4(mib2_ipAddrEntry_t *ap,
1897c478bd9Sstevel@tonic-gate 			    char ifname[], char logintname[],
1907c478bd9Sstevel@tonic-gate 			    struct ifstat *statptr, boolean_t ksp_not_null);
1917c478bd9Sstevel@tonic-gate static void		if_report_ip6(mib2_ipv6AddrEntry_t *ap6,
1927c478bd9Sstevel@tonic-gate 			    char ifname[], char logintname[],
1937c478bd9Sstevel@tonic-gate 			    struct ifstat *statptr, boolean_t ksp_not_null);
19445916cd2Sjpk static void		ire_report(const mib_item_t *item);
19545916cd2Sjpk static void		tcp_report(const mib_item_t *item);
19645916cd2Sjpk static void		udp_report(const mib_item_t *item);
1977c478bd9Sstevel@tonic-gate static void		group_report(mib_item_t *item);
1987c478bd9Sstevel@tonic-gate static void		print_ip_stats(mib2_ip_t *ip);
1997c478bd9Sstevel@tonic-gate static void		print_icmp_stats(mib2_icmp_t *icmp);
2007c478bd9Sstevel@tonic-gate static void		print_ip6_stats(mib2_ipv6IfStatsEntry_t *ip6);
2017c478bd9Sstevel@tonic-gate static void		print_icmp6_stats(mib2_ipv6IfIcmpEntry_t *icmp6);
2027c478bd9Sstevel@tonic-gate static void		print_sctp_stats(mib2_sctp_t *tcp);
2037c478bd9Sstevel@tonic-gate static void		print_tcp_stats(mib2_tcp_t *tcp);
2047c478bd9Sstevel@tonic-gate static void		print_udp_stats(mib2_udp_t *udp);
2057c478bd9Sstevel@tonic-gate static void		print_rawip_stats(mib2_rawip_t *rawip);
2067c478bd9Sstevel@tonic-gate static void		print_igmp_stats(struct igmpstat *igps);
2077c478bd9Sstevel@tonic-gate static void		print_mrt_stats(struct mrtstat *mrts);
20845916cd2Sjpk static void		sctp_report(const mib_item_t *item);
2097c478bd9Sstevel@tonic-gate static void		sum_ip6_stats(mib2_ipv6IfStatsEntry_t *ip6,
2107c478bd9Sstevel@tonic-gate 			    mib2_ipv6IfStatsEntry_t *sum6);
2117c478bd9Sstevel@tonic-gate static void		sum_icmp6_stats(mib2_ipv6IfIcmpEntry_t *icmp6,
2127c478bd9Sstevel@tonic-gate 			    mib2_ipv6IfIcmpEntry_t *sum6);
2137c478bd9Sstevel@tonic-gate static void		m_report(void);
2147c478bd9Sstevel@tonic-gate static void		dhcp_report(char *);
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	void		fail(int, char *, ...);
2177c478bd9Sstevel@tonic-gate static	uint64_t	kstat_named_value(kstat_t *, char *);
2187c478bd9Sstevel@tonic-gate static	kid_t		safe_kstat_read(kstat_ctl_t *, kstat_t *, void *);
2197c478bd9Sstevel@tonic-gate static int		isnum(char *);
2207c478bd9Sstevel@tonic-gate static char		*plural(int n);
2217c478bd9Sstevel@tonic-gate static char		*pluraly(int n);
2227c478bd9Sstevel@tonic-gate static char		*plurales(int n);
2237c478bd9Sstevel@tonic-gate static void		process_filter(char *arg);
2247c478bd9Sstevel@tonic-gate static boolean_t	family_selected(int family);
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate static void		usage(char *);
2277c478bd9Sstevel@tonic-gate static void 		fatal(int errcode, char *str1, ...);
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate #define	PLURAL(n) plural((int)n)
2307c478bd9Sstevel@tonic-gate #define	PLURALY(n) pluraly((int)n)
2317c478bd9Sstevel@tonic-gate #define	PLURALES(n) plurales((int)n)
2327c478bd9Sstevel@tonic-gate #define	IFLAGMOD(flg, val1, val2)	if (flg == val1) flg = val2
2337c478bd9Sstevel@tonic-gate #define	MDIFF(diff, elem2, elem1, member)	(diff)->member = \
2347c478bd9Sstevel@tonic-gate 	(elem2)->member - (elem1)->member
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate static	boolean_t	Aflag = B_FALSE;	/* All sockets/ifs/rtng-tbls */
2387c478bd9Sstevel@tonic-gate static	boolean_t	Dflag = B_FALSE;	/* Debug Info */
2397c478bd9Sstevel@tonic-gate static	boolean_t	Iflag = B_FALSE;	/* IP Traffic Interfaces */
2407c478bd9Sstevel@tonic-gate static	boolean_t	Mflag = B_FALSE;	/* STREAMS Memory Statistics */
2417c478bd9Sstevel@tonic-gate static	boolean_t	Nflag = B_FALSE;	/* Numeric Network Addresses */
2427c478bd9Sstevel@tonic-gate static	boolean_t	Rflag = B_FALSE;	/* Routing Tables */
24345916cd2Sjpk static	boolean_t	RSECflag = B_FALSE;	/* Security attributes */
2447c478bd9Sstevel@tonic-gate static	boolean_t	Sflag = B_FALSE;	/* Per-protocol Statistics */
2457c478bd9Sstevel@tonic-gate static	boolean_t	Vflag = B_FALSE;	/* Verbose */
2467c478bd9Sstevel@tonic-gate static	boolean_t	Pflag = B_FALSE;	/* Net to Media Tables */
2477c478bd9Sstevel@tonic-gate static	boolean_t	Gflag = B_FALSE;	/* Multicast group membership */
2487c478bd9Sstevel@tonic-gate static	boolean_t	MMflag = B_FALSE;	/* Multicast routing table */
2497c478bd9Sstevel@tonic-gate static	boolean_t	DHCPflag = B_FALSE;	/* DHCP statistics */
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate static	int	v4compat = 0;	/* Compatible printing format for status */
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate static int	proto = IPPROTO_MAX;	/* all protocols */
2547c478bd9Sstevel@tonic-gate kstat_ctl_t	*kc = NULL;
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate /*
2577c478bd9Sstevel@tonic-gate  * Sizes of data structures extracted from the base mib.
2587c478bd9Sstevel@tonic-gate  * This allows the size of the tables entries to grow while preserving
2597c478bd9Sstevel@tonic-gate  * binary compatibility.
2607c478bd9Sstevel@tonic-gate  */
2617c478bd9Sstevel@tonic-gate static int ipAddrEntrySize;
2627c478bd9Sstevel@tonic-gate static int ipRouteEntrySize;
2637c478bd9Sstevel@tonic-gate static int ipNetToMediaEntrySize;
2647c478bd9Sstevel@tonic-gate static int ipMemberEntrySize;
2657c478bd9Sstevel@tonic-gate static int ipGroupSourceEntrySize;
26645916cd2Sjpk static int ipRouteAttributeSize;
2677c478bd9Sstevel@tonic-gate static int vifctlSize;
2687c478bd9Sstevel@tonic-gate static int mfcctlSize;
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate static int ipv6IfStatsEntrySize;
2717c478bd9Sstevel@tonic-gate static int ipv6IfIcmpEntrySize;
2727c478bd9Sstevel@tonic-gate static int ipv6AddrEntrySize;
2737c478bd9Sstevel@tonic-gate static int ipv6RouteEntrySize;
2747c478bd9Sstevel@tonic-gate static int ipv6NetToMediaEntrySize;
2757c478bd9Sstevel@tonic-gate static int ipv6MemberEntrySize;
2767c478bd9Sstevel@tonic-gate static int ipv6GroupSourceEntrySize;
2777c478bd9Sstevel@tonic-gate 
27845916cd2Sjpk static int transportMLPSize;
2797c478bd9Sstevel@tonic-gate static int tcpConnEntrySize;
2807c478bd9Sstevel@tonic-gate static int tcp6ConnEntrySize;
2817c478bd9Sstevel@tonic-gate static int udpEntrySize;
2827c478bd9Sstevel@tonic-gate static int udp6EntrySize;
2837c478bd9Sstevel@tonic-gate static int sctpEntrySize;
2847c478bd9Sstevel@tonic-gate static int sctpLocalEntrySize;
2857c478bd9Sstevel@tonic-gate static int sctpRemoteEntrySize;
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate #define	protocol_selected(p)	(proto == IPPROTO_MAX || proto == (p))
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate /* Machinery used for -f (filter) option */
290*5c0b7edeSseb enum { FK_AF = 0, FK_OUTIF, FK_DST, FK_FLAGS, NFILTERKEYS };
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate static const char *filter_keys[NFILTERKEYS] = {
293*5c0b7edeSseb 	"af", "outif", "dst", "flags"
2947c478bd9Sstevel@tonic-gate };
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate /* Flags on routes */
2977c478bd9Sstevel@tonic-gate #define	FLF_A		0x00000001
2987c478bd9Sstevel@tonic-gate #define	FLF_B		0x00000002
2997c478bd9Sstevel@tonic-gate #define	FLF_D		0x00000004
3007c478bd9Sstevel@tonic-gate #define	FLF_G		0x00000008
3017c478bd9Sstevel@tonic-gate #define	FLF_H		0x00000010
3027c478bd9Sstevel@tonic-gate #define	FLF_L		0x00000020
3037c478bd9Sstevel@tonic-gate #define	FLF_U		0x00000040
3047c478bd9Sstevel@tonic-gate #define	FLF_M		0x00000080
3057c478bd9Sstevel@tonic-gate #define	FLF_S		0x00000100
3067c478bd9Sstevel@tonic-gate static const char flag_list[] = "ABDGHLUMS";
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate typedef struct filter_rule filter_t;
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate struct filter_rule {
3117c478bd9Sstevel@tonic-gate 	filter_t *f_next;
3127c478bd9Sstevel@tonic-gate 	union {
3137c478bd9Sstevel@tonic-gate 		int f_family;
3147c478bd9Sstevel@tonic-gate 		const char *f_ifname;
3157c478bd9Sstevel@tonic-gate 		struct {
3167c478bd9Sstevel@tonic-gate 			struct hostent *f_address;
3177c478bd9Sstevel@tonic-gate 			in6_addr_t f_mask;
3187c478bd9Sstevel@tonic-gate 		} a;
3197c478bd9Sstevel@tonic-gate 		struct {
3207c478bd9Sstevel@tonic-gate 			uint_t f_flagset;
3217c478bd9Sstevel@tonic-gate 			uint_t f_flagclear;
3227c478bd9Sstevel@tonic-gate 		} f;
3237c478bd9Sstevel@tonic-gate 	} u;
3247c478bd9Sstevel@tonic-gate };
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate /*
3277c478bd9Sstevel@tonic-gate  * The user-specified filters are linked into lists separated by
3287c478bd9Sstevel@tonic-gate  * keyword (type of filter).  Thus, the matching algorithm is:
3297c478bd9Sstevel@tonic-gate  *	For each non-empty filter list
3307c478bd9Sstevel@tonic-gate  *		If no filters in the list match
3317c478bd9Sstevel@tonic-gate  *			then stop here; route doesn't match
3327c478bd9Sstevel@tonic-gate  *	If loop above completes, then route does match and will be
3337c478bd9Sstevel@tonic-gate  *	displayed.
3347c478bd9Sstevel@tonic-gate  */
3357c478bd9Sstevel@tonic-gate static filter_t *filters[NFILTERKEYS];
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate int
3387c478bd9Sstevel@tonic-gate main(int argc, char **argv)
3397c478bd9Sstevel@tonic-gate {
3407c478bd9Sstevel@tonic-gate 	char		*name;
3417c478bd9Sstevel@tonic-gate 	mib_item_t	*item = NULL;
3427c478bd9Sstevel@tonic-gate 	mib_item_t	*previtem = NULL;
3437c478bd9Sstevel@tonic-gate 	int		sd = -1;
3447c478bd9Sstevel@tonic-gate 	char	*ifname = NULL;
3457c478bd9Sstevel@tonic-gate 	int	interval = 0;	/* Single time by default */
3467c478bd9Sstevel@tonic-gate 	int	count = -1;	/* Forever */
3477c478bd9Sstevel@tonic-gate 	int	c;
3487c478bd9Sstevel@tonic-gate 	int	d;
3497c478bd9Sstevel@tonic-gate 	/*
3507c478bd9Sstevel@tonic-gate 	 * Possible values of 'Iflag_only':
3517c478bd9Sstevel@tonic-gate 	 * -1, no feature-flags;
3527c478bd9Sstevel@tonic-gate 	 *  0, IFlag and other feature-flags enabled
3537c478bd9Sstevel@tonic-gate 	 *  1, IFlag is the only feature-flag enabled
3547c478bd9Sstevel@tonic-gate 	 * : trinary variable, modified using IFLAGMOD()
3557c478bd9Sstevel@tonic-gate 	 */
3567c478bd9Sstevel@tonic-gate 	int Iflag_only = -1;
3577c478bd9Sstevel@tonic-gate 	boolean_t once_only = B_FALSE; /* '-i' with count > 1 */
3587c478bd9Sstevel@tonic-gate 	extern char	*optarg;
3597c478bd9Sstevel@tonic-gate 	extern int	optind;
3607c478bd9Sstevel@tonic-gate 	char *default_ip_str = NULL;
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	name = argv[0];
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 	v4compat = get_compat_flag(&default_ip_str);
3657c478bd9Sstevel@tonic-gate 	if (v4compat == DEFAULT_PROT_BAD_VALUE)
3667c478bd9Sstevel@tonic-gate 		fatal(2, "%s: %s: Bad value for %s in %s\n", name,
3677c478bd9Sstevel@tonic-gate 		    default_ip_str, DEFAULT_IP, INET_DEFAULT_FILE);
3687c478bd9Sstevel@tonic-gate 	free(default_ip_str);
3697c478bd9Sstevel@tonic-gate 
37045916cd2Sjpk 	while ((c = getopt(argc, argv, "adimnrspMgvf:P:I:DR")) != -1) {
3717c478bd9Sstevel@tonic-gate 		switch ((char)c) {
3727c478bd9Sstevel@tonic-gate 		case 'a':		/* all connections */
3737c478bd9Sstevel@tonic-gate 			Aflag = B_TRUE;
3747c478bd9Sstevel@tonic-gate 			break;
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 		case 'd':		/* turn on debugging */
3777c478bd9Sstevel@tonic-gate 			Dflag = B_TRUE;
3787c478bd9Sstevel@tonic-gate 			break;
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 		case 'i':		/* interface (ill/ipif report) */
3817c478bd9Sstevel@tonic-gate 			Iflag = B_TRUE;
3827c478bd9Sstevel@tonic-gate 			IFLAGMOD(Iflag_only, -1, 1); /* '-i' exists */
3837c478bd9Sstevel@tonic-gate 			break;
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 		case 'm':		/* streams msg report */
3867c478bd9Sstevel@tonic-gate 			Mflag = B_TRUE;
3877c478bd9Sstevel@tonic-gate 			IFLAGMOD(Iflag_only, 1, 0); /* see macro def'n */
3887c478bd9Sstevel@tonic-gate 			break;
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 		case 'n':		/* numeric format */
3917c478bd9Sstevel@tonic-gate 			Nflag = B_TRUE;
3927c478bd9Sstevel@tonic-gate 			break;
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 		case 'r':		/* route tables */
3957c478bd9Sstevel@tonic-gate 			Rflag = B_TRUE;
3967c478bd9Sstevel@tonic-gate 			IFLAGMOD(Iflag_only, 1, 0); /* see macro def'n */
3977c478bd9Sstevel@tonic-gate 			break;
3987c478bd9Sstevel@tonic-gate 
39945916cd2Sjpk 		case 'R':		/* security attributes */
40045916cd2Sjpk 			RSECflag = B_TRUE;
40145916cd2Sjpk 			IFLAGMOD(Iflag_only, 1, 0); /* see macro def'n */
40245916cd2Sjpk 			break;
40345916cd2Sjpk 
4047c478bd9Sstevel@tonic-gate 		case 's':		/* per-protocol statistics */
4057c478bd9Sstevel@tonic-gate 			Sflag = B_TRUE;
4067c478bd9Sstevel@tonic-gate 			IFLAGMOD(Iflag_only, 1, 0); /* see macro def'n */
4077c478bd9Sstevel@tonic-gate 			break;
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 		case 'p':		/* arp/ndp table */
4107c478bd9Sstevel@tonic-gate 			Pflag = B_TRUE;
4117c478bd9Sstevel@tonic-gate 			IFLAGMOD(Iflag_only, 1, 0); /* see macro def'n */
4127c478bd9Sstevel@tonic-gate 			break;
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 		case 'M':		/* multicast routing tables */
4157c478bd9Sstevel@tonic-gate 			MMflag = B_TRUE;
4167c478bd9Sstevel@tonic-gate 			IFLAGMOD(Iflag_only, 1, 0); /* see macro def'n */
4177c478bd9Sstevel@tonic-gate 			break;
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate 		case 'g':		/* multicast group membership */
4207c478bd9Sstevel@tonic-gate 			Gflag = B_TRUE;
4217c478bd9Sstevel@tonic-gate 			IFLAGMOD(Iflag_only, 1, 0); /* see macro def'n */
4227c478bd9Sstevel@tonic-gate 			break;
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate 		case 'v':		/* verbose output format */
4257c478bd9Sstevel@tonic-gate 			Vflag = B_TRUE;
4267c478bd9Sstevel@tonic-gate 			IFLAGMOD(Iflag_only, 1, 0); /* see macro def'n */
4277c478bd9Sstevel@tonic-gate 			break;
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 		case 'f':
4307c478bd9Sstevel@tonic-gate 			process_filter(optarg);
4317c478bd9Sstevel@tonic-gate 			break;
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 		case 'P':
4347c478bd9Sstevel@tonic-gate 			if (strcmp(optarg, "ip") == 0) {
4357c478bd9Sstevel@tonic-gate 				proto = IPPROTO_IP;
4367c478bd9Sstevel@tonic-gate 			} else if (strcmp(optarg, "ipv6") == 0 ||
4377c478bd9Sstevel@tonic-gate 			    strcmp(optarg, "ip6") == 0) {
4387c478bd9Sstevel@tonic-gate 				v4compat = 0;	/* Overridden */
4397c478bd9Sstevel@tonic-gate 				proto = IPPROTO_IPV6;
4407c478bd9Sstevel@tonic-gate 			} else if (strcmp(optarg, "icmp") == 0) {
4417c478bd9Sstevel@tonic-gate 				proto = IPPROTO_ICMP;
4427c478bd9Sstevel@tonic-gate 			} else if (strcmp(optarg, "icmpv6") == 0 ||
4437c478bd9Sstevel@tonic-gate 			    strcmp(optarg, "icmp6") == 0) {
4447c478bd9Sstevel@tonic-gate 				v4compat = 0;	/* Overridden */
4457c478bd9Sstevel@tonic-gate 				proto = IPPROTO_ICMPV6;
4467c478bd9Sstevel@tonic-gate 			} else if (strcmp(optarg, "igmp") == 0) {
4477c478bd9Sstevel@tonic-gate 				proto = IPPROTO_IGMP;
4487c478bd9Sstevel@tonic-gate 			} else if (strcmp(optarg, "udp") == 0) {
4497c478bd9Sstevel@tonic-gate 				proto = IPPROTO_UDP;
4507c478bd9Sstevel@tonic-gate 			} else if (strcmp(optarg, "tcp") == 0) {
4517c478bd9Sstevel@tonic-gate 				proto = IPPROTO_TCP;
4527c478bd9Sstevel@tonic-gate 			} else if (strcmp(optarg, "sctp") == 0) {
4537c478bd9Sstevel@tonic-gate 				proto = IPPROTO_SCTP;
4547c478bd9Sstevel@tonic-gate 			} else if (strcmp(optarg, "raw") == 0 ||
4557c478bd9Sstevel@tonic-gate 			    strcmp(optarg, "rawip") == 0) {
4567c478bd9Sstevel@tonic-gate 				proto = IPPROTO_RAW;
4577c478bd9Sstevel@tonic-gate 			} else {
4587c478bd9Sstevel@tonic-gate 				fatal(1, "%s: unknown protocol.\n", optarg);
4597c478bd9Sstevel@tonic-gate 			}
4607c478bd9Sstevel@tonic-gate 			break;
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 		case 'I':
4637c478bd9Sstevel@tonic-gate 			ifname = optarg;
4647c478bd9Sstevel@tonic-gate 			Iflag = B_TRUE;
4657c478bd9Sstevel@tonic-gate 			IFLAGMOD(Iflag_only, -1, 1); /* see macro def'n */
4667c478bd9Sstevel@tonic-gate 			break;
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate 		case 'D':
4697c478bd9Sstevel@tonic-gate 			DHCPflag = B_TRUE;
4707c478bd9Sstevel@tonic-gate 			Iflag_only = 0;
4717c478bd9Sstevel@tonic-gate 			break;
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 		case '?':
4747c478bd9Sstevel@tonic-gate 		default:
4757c478bd9Sstevel@tonic-gate 			usage(name);
4767c478bd9Sstevel@tonic-gate 		}
4777c478bd9Sstevel@tonic-gate 	}
4787c478bd9Sstevel@tonic-gate 
47945916cd2Sjpk 	/*
48045916cd2Sjpk 	 * Make sure -R option is set only on a labeled system.
48145916cd2Sjpk 	 */
48245916cd2Sjpk 	if (RSECflag && !is_system_labeled()) {
48345916cd2Sjpk 		(void) fprintf(stderr, "-R set but labeling is not enabled\n");
48445916cd2Sjpk 		usage(name);
48545916cd2Sjpk 	}
48645916cd2Sjpk 
4877c478bd9Sstevel@tonic-gate 	/*
4887c478bd9Sstevel@tonic-gate 	 * Handle other arguments: find interval, count; the
4897c478bd9Sstevel@tonic-gate 	 * flags that accept 'interval' and 'count' are OR'd
4907c478bd9Sstevel@tonic-gate 	 * in the outermost 'if'; more flags may be added as
4917c478bd9Sstevel@tonic-gate 	 * required
4927c478bd9Sstevel@tonic-gate 	 */
4937c478bd9Sstevel@tonic-gate 	if (Iflag || Sflag || Mflag) {
4947c478bd9Sstevel@tonic-gate 		for (d = optind; d < argc; d++) {
4957c478bd9Sstevel@tonic-gate 			if (isnum(argv[d])) {
4967c478bd9Sstevel@tonic-gate 				interval = atoi(argv[d]);
4977c478bd9Sstevel@tonic-gate 				if (d + 1 < argc &&
4987c478bd9Sstevel@tonic-gate 				    isnum(argv[d + 1])) {
4997c478bd9Sstevel@tonic-gate 					count = atoi(argv[d + 1]);
5007c478bd9Sstevel@tonic-gate 					optind++;
5017c478bd9Sstevel@tonic-gate 				}
5027c478bd9Sstevel@tonic-gate 				optind++;
5037c478bd9Sstevel@tonic-gate 				if (interval == 0 || count == 0)
5047c478bd9Sstevel@tonic-gate 					usage(name);
5057c478bd9Sstevel@tonic-gate 				break;
5067c478bd9Sstevel@tonic-gate 			}
5077c478bd9Sstevel@tonic-gate 		}
5087c478bd9Sstevel@tonic-gate 	}
5097c478bd9Sstevel@tonic-gate 	if (optind < argc) {
5107c478bd9Sstevel@tonic-gate 		if (Iflag && isnum(argv[optind])) {
5117c478bd9Sstevel@tonic-gate 			count = atoi(argv[optind]);
5127c478bd9Sstevel@tonic-gate 			if (count == 0)
5137c478bd9Sstevel@tonic-gate 				usage(name);
5147c478bd9Sstevel@tonic-gate 			optind++;
5157c478bd9Sstevel@tonic-gate 		}
5167c478bd9Sstevel@tonic-gate 	}
5177c478bd9Sstevel@tonic-gate 	if (optind < argc) {
5187c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
5197c478bd9Sstevel@tonic-gate 		    "%s: extra arguments\n", name);
5207c478bd9Sstevel@tonic-gate 		usage(name);
5217c478bd9Sstevel@tonic-gate 	}
5227c478bd9Sstevel@tonic-gate 	if (interval)
5237c478bd9Sstevel@tonic-gate 		setbuf(stdout, NULL);
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 	if (DHCPflag) {
5267c478bd9Sstevel@tonic-gate 		dhcp_report(Iflag ? ifname : NULL);
5277c478bd9Sstevel@tonic-gate 		exit(0);
5287c478bd9Sstevel@tonic-gate 	}
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate 	/* Get data structures: priming before iteration */
5317c478bd9Sstevel@tonic-gate 	if (family_selected(AF_INET) || family_selected(AF_INET6)) {
5327c478bd9Sstevel@tonic-gate 		sd = mibopen();
5337c478bd9Sstevel@tonic-gate 		if (sd == -1)
5347c478bd9Sstevel@tonic-gate 			fatal(1, "can't open mib stream\n");
5357c478bd9Sstevel@tonic-gate 		if ((item = mibget(sd)) == NULL) {
5367c478bd9Sstevel@tonic-gate 			(void) close(sd);
5377c478bd9Sstevel@tonic-gate 			fatal(1, "mibget() failed\n");
5387c478bd9Sstevel@tonic-gate 		}
5397c478bd9Sstevel@tonic-gate 		/* Extract constant sizes - need do once only */
5407c478bd9Sstevel@tonic-gate 		mib_get_constants(item);
5417c478bd9Sstevel@tonic-gate 	}
5427c478bd9Sstevel@tonic-gate 	if ((kc = kstat_open()) == NULL) {
5437c478bd9Sstevel@tonic-gate 		mibfree(item);
5447c478bd9Sstevel@tonic-gate 		(void) close(sd);
5457c478bd9Sstevel@tonic-gate 		fail(1, "kstat_open(): can't open /dev/kstat");
5467c478bd9Sstevel@tonic-gate 	}
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 	if (interval <= 0) {
5497c478bd9Sstevel@tonic-gate 		count = 1;
5507c478bd9Sstevel@tonic-gate 		once_only = B_TRUE;
5517c478bd9Sstevel@tonic-gate 	}
5527c478bd9Sstevel@tonic-gate 	/* 'for' loop 1: */
5537c478bd9Sstevel@tonic-gate 	for (;;) {
5547c478bd9Sstevel@tonic-gate 		mib_item_t *curritem = NULL; /* only for -[M]s */
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate 		/* netstat: AF_INET[6] behaviour */
5577c478bd9Sstevel@tonic-gate 		if (family_selected(AF_INET) || family_selected(AF_INET6)) {
5587c478bd9Sstevel@tonic-gate 			if (Sflag) {
5597c478bd9Sstevel@tonic-gate 				curritem = mib_item_diff(previtem, item);
5607c478bd9Sstevel@tonic-gate 				if (curritem == NULL)
5617c478bd9Sstevel@tonic-gate 					fatal(1, "can't process mib data, "
5627c478bd9Sstevel@tonic-gate 					    "out of memory\n");
5637c478bd9Sstevel@tonic-gate 				mib_item_destroy(&previtem);
5647c478bd9Sstevel@tonic-gate 			}
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 			if (!(Iflag || Rflag || Sflag || Mflag ||
5677c478bd9Sstevel@tonic-gate 			    MMflag || Pflag || Gflag || DHCPflag)) {
5687c478bd9Sstevel@tonic-gate 				if (protocol_selected(IPPROTO_UDP))
5697c478bd9Sstevel@tonic-gate 					udp_report(item);
5707c478bd9Sstevel@tonic-gate 				if (protocol_selected(IPPROTO_TCP))
5717c478bd9Sstevel@tonic-gate 					tcp_report(item);
5727c478bd9Sstevel@tonic-gate 				if (protocol_selected(IPPROTO_SCTP))
5737c478bd9Sstevel@tonic-gate 					sctp_report(item);
5747c478bd9Sstevel@tonic-gate 			}
5757c478bd9Sstevel@tonic-gate 			if (Iflag)
5767c478bd9Sstevel@tonic-gate 				if_report(item, ifname, Iflag_only, once_only);
5777c478bd9Sstevel@tonic-gate 			if (Mflag)
5787c478bd9Sstevel@tonic-gate 				m_report();
5797c478bd9Sstevel@tonic-gate 			if (Rflag)
5807c478bd9Sstevel@tonic-gate 				ire_report(item);
5817c478bd9Sstevel@tonic-gate 			if (Sflag && MMflag) {
5827c478bd9Sstevel@tonic-gate 				mrt_stat_report(curritem);
5837c478bd9Sstevel@tonic-gate 			} else {
5847c478bd9Sstevel@tonic-gate 				if (Sflag)
5857c478bd9Sstevel@tonic-gate 					stat_report(curritem);
5867c478bd9Sstevel@tonic-gate 				if (MMflag)
5877c478bd9Sstevel@tonic-gate 					mrt_report(item);
5887c478bd9Sstevel@tonic-gate 			}
5897c478bd9Sstevel@tonic-gate 			if (Gflag)
5907c478bd9Sstevel@tonic-gate 				group_report(item);
5917c478bd9Sstevel@tonic-gate 			if (Pflag) {
5927c478bd9Sstevel@tonic-gate 				if (family_selected(AF_INET))
5937c478bd9Sstevel@tonic-gate 					arp_report(item);
5947c478bd9Sstevel@tonic-gate 				if (family_selected(AF_INET6))
5957c478bd9Sstevel@tonic-gate 					ndp_report(item);
5967c478bd9Sstevel@tonic-gate 			}
5977c478bd9Sstevel@tonic-gate 			mib_item_destroy(&curritem);
5987c478bd9Sstevel@tonic-gate 		}
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 		/* netstat: AF_UNIX behaviour */
6017c478bd9Sstevel@tonic-gate 		if (family_selected(AF_UNIX) &&
6027c478bd9Sstevel@tonic-gate 		    (!(Iflag || Rflag || Sflag || Mflag ||
6037c478bd9Sstevel@tonic-gate 		    MMflag || Pflag || Gflag)))
6047c478bd9Sstevel@tonic-gate 			unixpr(kc);
6057c478bd9Sstevel@tonic-gate 		(void) kstat_close(kc);
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 		/* iteration handling code */
6087c478bd9Sstevel@tonic-gate 		if (count > 0 && --count == 0)
6097c478bd9Sstevel@tonic-gate 			break;
6107c478bd9Sstevel@tonic-gate 		(void) sleep(interval);
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate 		/* re-populating of data structures */
6137c478bd9Sstevel@tonic-gate 		if (family_selected(AF_INET) || family_selected(AF_INET6)) {
6147c478bd9Sstevel@tonic-gate 			if (Sflag) {
6157c478bd9Sstevel@tonic-gate 				/* previtem is a cut-down list */
6167c478bd9Sstevel@tonic-gate 				previtem = mib_item_dup(item);
6177c478bd9Sstevel@tonic-gate 				if (previtem == NULL)
6187c478bd9Sstevel@tonic-gate 					fatal(1, "can't process mib data, "
6197c478bd9Sstevel@tonic-gate 					    "out of memory\n");
6207c478bd9Sstevel@tonic-gate 			}
6217c478bd9Sstevel@tonic-gate 			mibfree(item);
6227c478bd9Sstevel@tonic-gate 			(void) close(sd);
6237c478bd9Sstevel@tonic-gate 			if ((sd = mibopen()) == -1)
6247c478bd9Sstevel@tonic-gate 				fatal(1, "can't open mib stream anymore\n");
6257c478bd9Sstevel@tonic-gate 			if ((item = mibget(sd)) == NULL) {
6267c478bd9Sstevel@tonic-gate 				(void) close(sd);
6277c478bd9Sstevel@tonic-gate 				fatal(1, "mibget() failed\n");
6287c478bd9Sstevel@tonic-gate 			}
6297c478bd9Sstevel@tonic-gate 		}
6307c478bd9Sstevel@tonic-gate 		if ((kc = kstat_open()) == NULL)
6317c478bd9Sstevel@tonic-gate 			fail(1, "kstat_open(): can't open /dev/kstat");
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate 	} /* 'for' loop 1 ends */
6347c478bd9Sstevel@tonic-gate 	mibfree(item);
6357c478bd9Sstevel@tonic-gate 	(void) close(sd);
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 	return (0);
6387c478bd9Sstevel@tonic-gate }
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate static int
6427c478bd9Sstevel@tonic-gate isnum(char *p)
6437c478bd9Sstevel@tonic-gate {
6447c478bd9Sstevel@tonic-gate 	int	len;
6457c478bd9Sstevel@tonic-gate 	int	i;
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 	len = strlen(p);
6487c478bd9Sstevel@tonic-gate 	for (i = 0; i < len; i++)
6497c478bd9Sstevel@tonic-gate 		if (!isdigit(p[i]))
6507c478bd9Sstevel@tonic-gate 			return (0);
6517c478bd9Sstevel@tonic-gate 	return (1);
6527c478bd9Sstevel@tonic-gate }
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate /* --------------------------------- MIBGET -------------------------------- */
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate static mib_item_t *
6587c478bd9Sstevel@tonic-gate mibget(int sd)
6597c478bd9Sstevel@tonic-gate {
6607c478bd9Sstevel@tonic-gate 	/*
6617c478bd9Sstevel@tonic-gate 	 * buf is an automatic for this function, so the
6627c478bd9Sstevel@tonic-gate 	 * compiler has complete control over its alignment;
6637c478bd9Sstevel@tonic-gate 	 * it is assumed this alignment is satisfactory for
6647c478bd9Sstevel@tonic-gate 	 * it to be casted to certain other struct pointers
6657c478bd9Sstevel@tonic-gate 	 * here, such as struct T_optmgmt_ack * .
6667c478bd9Sstevel@tonic-gate 	 */
6677c478bd9Sstevel@tonic-gate 	uintptr_t		buf[512 / sizeof (uintptr_t)];
6687c478bd9Sstevel@tonic-gate 	int			flags;
6697c478bd9Sstevel@tonic-gate 	int			i, j, getcode;
6707c478bd9Sstevel@tonic-gate 	struct strbuf		ctlbuf, databuf;
6717c478bd9Sstevel@tonic-gate 	struct T_optmgmt_req	*tor = (struct T_optmgmt_req *)buf;
6727c478bd9Sstevel@tonic-gate 	struct T_optmgmt_ack	*toa = (struct T_optmgmt_ack *)buf;
6737c478bd9Sstevel@tonic-gate 	struct T_error_ack	*tea = (struct T_error_ack *)buf;
6747c478bd9Sstevel@tonic-gate 	struct opthdr		*req;
6757c478bd9Sstevel@tonic-gate 	mib_item_t		*first_item = NULL;
6767c478bd9Sstevel@tonic-gate 	mib_item_t		*last_item  = NULL;
6777c478bd9Sstevel@tonic-gate 	mib_item_t		*temp;
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 	tor->PRIM_type = T_SVR4_OPTMGMT_REQ;
6807c478bd9Sstevel@tonic-gate 	tor->OPT_offset = sizeof (struct T_optmgmt_req);
6817c478bd9Sstevel@tonic-gate 	tor->OPT_length = sizeof (struct opthdr);
6827c478bd9Sstevel@tonic-gate 	tor->MGMT_flags = T_CURRENT;
6837c478bd9Sstevel@tonic-gate 	req = (struct opthdr *)&tor[1];
6847c478bd9Sstevel@tonic-gate 	req->level = MIB2_IP;		/* any MIB2_xxx value ok here */
6857c478bd9Sstevel@tonic-gate 	req->name  = 0;
6867c478bd9Sstevel@tonic-gate 	req->len   = 0;
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 	ctlbuf.buf = (char *)buf;
6897c478bd9Sstevel@tonic-gate 	ctlbuf.len = tor->OPT_length + tor->OPT_offset;
6907c478bd9Sstevel@tonic-gate 	flags = 0;
6917c478bd9Sstevel@tonic-gate 	if (putmsg(sd, &ctlbuf, (struct strbuf *)0, flags) == -1) {
6927c478bd9Sstevel@tonic-gate 		perror("mibget: putmsg(ctl) failed");
6937c478bd9Sstevel@tonic-gate 		goto error_exit;
6947c478bd9Sstevel@tonic-gate 	}
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate 	/*
6977c478bd9Sstevel@tonic-gate 	 * Each reply consists of a ctl part for one fixed structure
6987c478bd9Sstevel@tonic-gate 	 * or table, as defined in mib2.h.  The format is a T_OPTMGMT_ACK,
6997c478bd9Sstevel@tonic-gate 	 * containing an opthdr structure.  level/name identify the entry,
7007c478bd9Sstevel@tonic-gate 	 * len is the size of the data part of the message.
7017c478bd9Sstevel@tonic-gate 	 */
7027c478bd9Sstevel@tonic-gate 	req = (struct opthdr *)&toa[1];
7037c478bd9Sstevel@tonic-gate 	ctlbuf.maxlen = sizeof (buf);
7047c478bd9Sstevel@tonic-gate 	j = 1;
7057c478bd9Sstevel@tonic-gate 	for (;;) {
7067c478bd9Sstevel@tonic-gate 		flags = 0;
7077c478bd9Sstevel@tonic-gate 		getcode = getmsg(sd, &ctlbuf, (struct strbuf *)0, &flags);
7087c478bd9Sstevel@tonic-gate 		if (getcode == -1) {
7097c478bd9Sstevel@tonic-gate 			perror("mibget getmsg(ctl) failed");
7107c478bd9Sstevel@tonic-gate 			if (Dflag) {
7117c478bd9Sstevel@tonic-gate 				(void) fputs("#   level   name    len\n",
7127c478bd9Sstevel@tonic-gate 				    stderr);
7137c478bd9Sstevel@tonic-gate 				i = 0;
7147c478bd9Sstevel@tonic-gate 				for (last_item = first_item; last_item;
7157c478bd9Sstevel@tonic-gate 					last_item = last_item->next_item)
7167c478bd9Sstevel@tonic-gate 					(void) printf("%d  %4d   %5d   %d\n",
7177c478bd9Sstevel@tonic-gate 					    ++i,
7187c478bd9Sstevel@tonic-gate 					    last_item->group,
7197c478bd9Sstevel@tonic-gate 					    last_item->mib_id,
7207c478bd9Sstevel@tonic-gate 					    last_item->length);
7217c478bd9Sstevel@tonic-gate 			}
7227c478bd9Sstevel@tonic-gate 			goto error_exit;
7237c478bd9Sstevel@tonic-gate 		}
7247c478bd9Sstevel@tonic-gate 		if (getcode == 0 &&
7257c478bd9Sstevel@tonic-gate 		    ctlbuf.len >= sizeof (struct T_optmgmt_ack) &&
7267c478bd9Sstevel@tonic-gate 		    toa->PRIM_type == T_OPTMGMT_ACK &&
7277c478bd9Sstevel@tonic-gate 		    toa->MGMT_flags == T_SUCCESS &&
7287c478bd9Sstevel@tonic-gate 		    req->len == 0) {
7297c478bd9Sstevel@tonic-gate 			if (Dflag)
7307c478bd9Sstevel@tonic-gate 				(void) printf("mibget getmsg() %d returned "
7317c478bd9Sstevel@tonic-gate 				    "EOD (level %ld, name %ld)\n",
7327c478bd9Sstevel@tonic-gate 				    j, req->level, req->name);
7337c478bd9Sstevel@tonic-gate 			return (first_item);		/* this is EOD msg */
7347c478bd9Sstevel@tonic-gate 		}
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate 		if (ctlbuf.len >= sizeof (struct T_error_ack) &&
7377c478bd9Sstevel@tonic-gate 		    tea->PRIM_type == T_ERROR_ACK) {
7387c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
7397c478bd9Sstevel@tonic-gate 			    "mibget %d gives T_ERROR_ACK: TLI_error = 0x%lx, "
7407c478bd9Sstevel@tonic-gate 			    "UNIX_error = 0x%lx\n",
7417c478bd9Sstevel@tonic-gate 			    j, tea->TLI_error, tea->UNIX_error);
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate 			errno = (tea->TLI_error == TSYSERR) ?
7447c478bd9Sstevel@tonic-gate 			    tea->UNIX_error : EPROTO;
7457c478bd9Sstevel@tonic-gate 			goto error_exit;
7467c478bd9Sstevel@tonic-gate 		}
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate 		if (getcode != MOREDATA ||
7497c478bd9Sstevel@tonic-gate 		    ctlbuf.len < sizeof (struct T_optmgmt_ack) ||
7507c478bd9Sstevel@tonic-gate 		    toa->PRIM_type != T_OPTMGMT_ACK ||
7517c478bd9Sstevel@tonic-gate 		    toa->MGMT_flags != T_SUCCESS) {
7527c478bd9Sstevel@tonic-gate 			(void) printf("mibget getmsg(ctl) %d returned %d, "
7537c478bd9Sstevel@tonic-gate 			    "ctlbuf.len = %d, PRIM_type = %ld\n",
7547c478bd9Sstevel@tonic-gate 			    j, getcode, ctlbuf.len, toa->PRIM_type);
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate 			if (toa->PRIM_type == T_OPTMGMT_ACK)
7577c478bd9Sstevel@tonic-gate 				(void) printf("T_OPTMGMT_ACK: "
7587c478bd9Sstevel@tonic-gate 				    "MGMT_flags = 0x%lx, req->len = %ld\n",
7597c478bd9Sstevel@tonic-gate 				    toa->MGMT_flags, req->len);
7607c478bd9Sstevel@tonic-gate 			errno = ENOMSG;
7617c478bd9Sstevel@tonic-gate 			goto error_exit;
7627c478bd9Sstevel@tonic-gate 		}
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate 		temp = (mib_item_t *)malloc(sizeof (mib_item_t));
7657c478bd9Sstevel@tonic-gate 		if (temp == NULL) {
7667c478bd9Sstevel@tonic-gate 			perror("mibget malloc failed");
7677c478bd9Sstevel@tonic-gate 			goto error_exit;
7687c478bd9Sstevel@tonic-gate 		}
7697c478bd9Sstevel@tonic-gate 		if (last_item != NULL)
7707c478bd9Sstevel@tonic-gate 			last_item->next_item = temp;
7717c478bd9Sstevel@tonic-gate 		else
7727c478bd9Sstevel@tonic-gate 			first_item = temp;
7737c478bd9Sstevel@tonic-gate 		last_item = temp;
7747c478bd9Sstevel@tonic-gate 		last_item->next_item = NULL;
7757c478bd9Sstevel@tonic-gate 		last_item->group = req->level;
7767c478bd9Sstevel@tonic-gate 		last_item->mib_id = req->name;
7777c478bd9Sstevel@tonic-gate 		last_item->length = req->len;
7787c478bd9Sstevel@tonic-gate 		last_item->valp = malloc((int)req->len);
7797c478bd9Sstevel@tonic-gate 		if (last_item->valp == NULL)
7807c478bd9Sstevel@tonic-gate 			goto error_exit;
7817c478bd9Sstevel@tonic-gate 		if (Dflag)
7827c478bd9Sstevel@tonic-gate 			(void) printf("msg %d: group = %4d   mib_id = %5d"
7837c478bd9Sstevel@tonic-gate 			    "length = %d\n",
7847c478bd9Sstevel@tonic-gate 			    j, last_item->group, last_item->mib_id,
7857c478bd9Sstevel@tonic-gate 			    last_item->length);
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate 		databuf.maxlen = last_item->length;
7887c478bd9Sstevel@tonic-gate 		databuf.buf    = (char *)last_item->valp;
7897c478bd9Sstevel@tonic-gate 		databuf.len    = 0;
7907c478bd9Sstevel@tonic-gate 		flags = 0;
7917c478bd9Sstevel@tonic-gate 		getcode = getmsg(sd, (struct strbuf *)0, &databuf, &flags);
7927c478bd9Sstevel@tonic-gate 		if (getcode == -1) {
7937c478bd9Sstevel@tonic-gate 			perror("mibget getmsg(data) failed");
7947c478bd9Sstevel@tonic-gate 			goto error_exit;
7957c478bd9Sstevel@tonic-gate 		} else if (getcode != 0) {
7967c478bd9Sstevel@tonic-gate 			(void) printf("mibget getmsg(data) returned %d, "
7977c478bd9Sstevel@tonic-gate 			    "databuf.maxlen = %d, databuf.len = %d\n",
7987c478bd9Sstevel@tonic-gate 			    getcode, databuf.maxlen, databuf.len);
7997c478bd9Sstevel@tonic-gate 			goto error_exit;
8007c478bd9Sstevel@tonic-gate 		}
8017c478bd9Sstevel@tonic-gate 		j++;
8027c478bd9Sstevel@tonic-gate 	}
8037c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate error_exit:;
8067c478bd9Sstevel@tonic-gate 	mibfree(first_item);
8077c478bd9Sstevel@tonic-gate 	return (NULL);
8087c478bd9Sstevel@tonic-gate }
8097c478bd9Sstevel@tonic-gate 
8107c478bd9Sstevel@tonic-gate /*
8117c478bd9Sstevel@tonic-gate  * mibfree: frees a linked list of type (mib_item_t *)
8127c478bd9Sstevel@tonic-gate  * returned by mibget(); this is NOT THE SAME AS
8137c478bd9Sstevel@tonic-gate  * mib_item_destroy(), so should be used for objects
8147c478bd9Sstevel@tonic-gate  * returned by mibget() only
8157c478bd9Sstevel@tonic-gate  */
8167c478bd9Sstevel@tonic-gate static void
8177c478bd9Sstevel@tonic-gate mibfree(mib_item_t *firstitem)
8187c478bd9Sstevel@tonic-gate {
8197c478bd9Sstevel@tonic-gate 	mib_item_t *lastitem;
8207c478bd9Sstevel@tonic-gate 
8217c478bd9Sstevel@tonic-gate 	while (firstitem != NULL) {
8227c478bd9Sstevel@tonic-gate 		lastitem = firstitem;
8237c478bd9Sstevel@tonic-gate 		firstitem = firstitem->next_item;
8247c478bd9Sstevel@tonic-gate 		if (lastitem->valp != NULL)
8257c478bd9Sstevel@tonic-gate 			free(lastitem->valp);
8267c478bd9Sstevel@tonic-gate 		free(lastitem);
8277c478bd9Sstevel@tonic-gate 	}
8287c478bd9Sstevel@tonic-gate }
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate static int
8317c478bd9Sstevel@tonic-gate mibopen(void)
8327c478bd9Sstevel@tonic-gate {
8337c478bd9Sstevel@tonic-gate 	int	sd;
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate 	sd = open("/dev/arp", O_RDWR);
8367c478bd9Sstevel@tonic-gate 	if (sd == -1) {
8377c478bd9Sstevel@tonic-gate 		perror("arp open");
8387c478bd9Sstevel@tonic-gate 		return (-1);
8397c478bd9Sstevel@tonic-gate 	}
8407c478bd9Sstevel@tonic-gate 	if (ioctl(sd, I_PUSH, "tcp") == -1) {
8417c478bd9Sstevel@tonic-gate 		perror("tcp I_PUSH");
8427c478bd9Sstevel@tonic-gate 		(void) close(sd);
8437c478bd9Sstevel@tonic-gate 		return (-1);
8447c478bd9Sstevel@tonic-gate 	}
8457c478bd9Sstevel@tonic-gate 	if (ioctl(sd, I_PUSH, "udp") == -1) {
8467c478bd9Sstevel@tonic-gate 		perror("udp I_PUSH");
8477c478bd9Sstevel@tonic-gate 		(void) close(sd);
8487c478bd9Sstevel@tonic-gate 		return (-1);
8497c478bd9Sstevel@tonic-gate 	}
8507c478bd9Sstevel@tonic-gate 	if (ioctl(sd, I_PUSH, "icmp") == -1) {
8517c478bd9Sstevel@tonic-gate 		perror("icmp I_PUSH");
8527c478bd9Sstevel@tonic-gate 		(void) close(sd);
8537c478bd9Sstevel@tonic-gate 		return (-1);
8547c478bd9Sstevel@tonic-gate 	}
8557c478bd9Sstevel@tonic-gate 	return (sd);
8567c478bd9Sstevel@tonic-gate }
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate /*
8597c478bd9Sstevel@tonic-gate  * mib_item_dup: returns a clean mib_item_t * linked
8607c478bd9Sstevel@tonic-gate  * list, so that for every element item->mib_id is 0;
8617c478bd9Sstevel@tonic-gate  * to deallocate this linked list, use mib_item_destroy
8627c478bd9Sstevel@tonic-gate  */
8637c478bd9Sstevel@tonic-gate static mib_item_t *
8647c478bd9Sstevel@tonic-gate mib_item_dup(mib_item_t *item)
8657c478bd9Sstevel@tonic-gate {
8667c478bd9Sstevel@tonic-gate 	int	c = 0;
8677c478bd9Sstevel@tonic-gate 	mib_item_t *localp;
8687c478bd9Sstevel@tonic-gate 	mib_item_t *tempp;
8697c478bd9Sstevel@tonic-gate 
8707c478bd9Sstevel@tonic-gate 	for (tempp = item; tempp; tempp = tempp->next_item)
8717c478bd9Sstevel@tonic-gate 		if (tempp->mib_id == 0)
8727c478bd9Sstevel@tonic-gate 			c++;
8737c478bd9Sstevel@tonic-gate 	tempp = NULL;
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate 	localp = (mib_item_t *)malloc(c * sizeof (mib_item_t));
8767c478bd9Sstevel@tonic-gate 	if (localp == NULL)
8777c478bd9Sstevel@tonic-gate 		return (NULL);
8787c478bd9Sstevel@tonic-gate 	c = 0;
8797c478bd9Sstevel@tonic-gate 	for (; item; item = item->next_item) {
8807c478bd9Sstevel@tonic-gate 		if (item->mib_id == 0) {
8817c478bd9Sstevel@tonic-gate 			/* Replicate item in localp */
8827c478bd9Sstevel@tonic-gate 			(localp[c]).next_item = NULL;
8837c478bd9Sstevel@tonic-gate 			(localp[c]).group = item->group;
8847c478bd9Sstevel@tonic-gate 			(localp[c]).mib_id = item->mib_id;
8857c478bd9Sstevel@tonic-gate 			(localp[c]).length = item->length;
8867c478bd9Sstevel@tonic-gate 			(localp[c]).valp = (uintptr_t *)malloc(
8877c478bd9Sstevel@tonic-gate 			    item->length);
8887c478bd9Sstevel@tonic-gate 			if ((localp[c]).valp == NULL) {
8897c478bd9Sstevel@tonic-gate 				mib_item_destroy(&localp);
8907c478bd9Sstevel@tonic-gate 				return (NULL);
8917c478bd9Sstevel@tonic-gate 			}
8927c478bd9Sstevel@tonic-gate 			(void *) memcpy((localp[c]).valp,
8937c478bd9Sstevel@tonic-gate 			    item->valp,
8947c478bd9Sstevel@tonic-gate 			    item->length);
8957c478bd9Sstevel@tonic-gate 			tempp = &(localp[c]);
8967c478bd9Sstevel@tonic-gate 			if (c > 0)
8977c478bd9Sstevel@tonic-gate 				(localp[c - 1]).next_item = tempp;
8987c478bd9Sstevel@tonic-gate 			c++;
8997c478bd9Sstevel@tonic-gate 		}
9007c478bd9Sstevel@tonic-gate 	}
9017c478bd9Sstevel@tonic-gate 	return (localp);
9027c478bd9Sstevel@tonic-gate }
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate /*
9057c478bd9Sstevel@tonic-gate  * mib_item_diff: takes two (mib_item_t *) linked lists
9067c478bd9Sstevel@tonic-gate  * item1 and item2 and computes the difference between
9077c478bd9Sstevel@tonic-gate  * differentiable values in item2 against item1 for every
9087c478bd9Sstevel@tonic-gate  * given member of item2; returns an mib_item_t * linked
9097c478bd9Sstevel@tonic-gate  * list of diff's, or a copy of item2 if item1 is NULL;
9107c478bd9Sstevel@tonic-gate  * will return NULL if system out of memory; works only
9117c478bd9Sstevel@tonic-gate  * for item->mib_id == 0
9127c478bd9Sstevel@tonic-gate  */
9137c478bd9Sstevel@tonic-gate static mib_item_t *
9147c478bd9Sstevel@tonic-gate mib_item_diff(mib_item_t *item1, mib_item_t *item2) {
9157c478bd9Sstevel@tonic-gate 	int	nitems	= 0; /* no. of items in item2 */
9167c478bd9Sstevel@tonic-gate 	mib_item_t *tempp2;  /* walking copy of item2 */
9177c478bd9Sstevel@tonic-gate 	mib_item_t *tempp1;  /* walking copy of item1 */
9187c478bd9Sstevel@tonic-gate 	mib_item_t *diffp;
9197c478bd9Sstevel@tonic-gate 	mib_item_t *diffptr; /* walking copy of diffp */
9207c478bd9Sstevel@tonic-gate 	mib_item_t *prevp = NULL;
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate 	if (item1 == NULL) {
9237c478bd9Sstevel@tonic-gate 		diffp = mib_item_dup(item2);
9247c478bd9Sstevel@tonic-gate 		return (diffp);
9257c478bd9Sstevel@tonic-gate 	}
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate 	for (tempp2 = item2;
9287c478bd9Sstevel@tonic-gate 	    tempp2;
9297c478bd9Sstevel@tonic-gate 	    tempp2 = tempp2->next_item) {
9307c478bd9Sstevel@tonic-gate 		if (tempp2->mib_id == 0)
9317c478bd9Sstevel@tonic-gate 			switch (tempp2->group) {
9327c478bd9Sstevel@tonic-gate 			/*
9337c478bd9Sstevel@tonic-gate 			 * upon adding a case here, the same
9347c478bd9Sstevel@tonic-gate 			 * must also be added in the next
9357c478bd9Sstevel@tonic-gate 			 * switch statement, alongwith
9367c478bd9Sstevel@tonic-gate 			 * appropriate code
9377c478bd9Sstevel@tonic-gate 			 */
9387c478bd9Sstevel@tonic-gate 			case MIB2_IP:
9397c478bd9Sstevel@tonic-gate 			case MIB2_IP6:
9407c478bd9Sstevel@tonic-gate 			case EXPER_DVMRP:
9417c478bd9Sstevel@tonic-gate 			case EXPER_IGMP:
9427c478bd9Sstevel@tonic-gate 			case MIB2_ICMP:
9437c478bd9Sstevel@tonic-gate 			case MIB2_ICMP6:
9447c478bd9Sstevel@tonic-gate 			case MIB2_TCP:
9457c478bd9Sstevel@tonic-gate 			case MIB2_UDP:
9467c478bd9Sstevel@tonic-gate 			case MIB2_SCTP:
9477c478bd9Sstevel@tonic-gate 			case EXPER_RAWIP:
9487c478bd9Sstevel@tonic-gate 				nitems++;
9497c478bd9Sstevel@tonic-gate 			}
9507c478bd9Sstevel@tonic-gate 	}
9517c478bd9Sstevel@tonic-gate 	tempp2 = NULL;
9527c478bd9Sstevel@tonic-gate 	if (nitems == 0) {
9537c478bd9Sstevel@tonic-gate 		diffp = mib_item_dup(item2);
9547c478bd9Sstevel@tonic-gate 		return (diffp);
9557c478bd9Sstevel@tonic-gate 	}
9567c478bd9Sstevel@tonic-gate 
9577c478bd9Sstevel@tonic-gate 	diffp = (mib_item_t *)calloc(nitems, sizeof (mib_item_t));
9587c478bd9Sstevel@tonic-gate 	if (diffp == NULL)
9597c478bd9Sstevel@tonic-gate 		return (NULL);
9607c478bd9Sstevel@tonic-gate 	diffptr = diffp;
9617c478bd9Sstevel@tonic-gate 	/* 'for' loop 1: */
9627c478bd9Sstevel@tonic-gate 	for (tempp2 = item2; tempp2 != NULL; tempp2 = tempp2->next_item) {
9637c478bd9Sstevel@tonic-gate 		if (tempp2->mib_id != 0)
9647c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
9657c478bd9Sstevel@tonic-gate 		/* 'for' loop 2: */
9667c478bd9Sstevel@tonic-gate 		for (tempp1 = item1; tempp1 != NULL;
9677c478bd9Sstevel@tonic-gate 		    tempp1 = tempp1->next_item) {
9687c478bd9Sstevel@tonic-gate 			if (!(tempp1->mib_id == 0 &&
9697c478bd9Sstevel@tonic-gate 			    tempp1->group == tempp2->group &&
9707c478bd9Sstevel@tonic-gate 			    tempp1->mib_id == tempp2->mib_id))
9717c478bd9Sstevel@tonic-gate 				continue; /* 'for' loop 2 */
9727c478bd9Sstevel@tonic-gate 			/* found comparable data sets */
9737c478bd9Sstevel@tonic-gate 			if (prevp != NULL)
9747c478bd9Sstevel@tonic-gate 				prevp->next_item = diffptr;
9757c478bd9Sstevel@tonic-gate 			switch (tempp2->group) {
9767c478bd9Sstevel@tonic-gate 			/*
9777c478bd9Sstevel@tonic-gate 			 * Indenting note: Because of long variable names
9787c478bd9Sstevel@tonic-gate 			 * in cases MIB2_IP6 and MIB2_ICMP6, their contents
9797c478bd9Sstevel@tonic-gate 			 * have been indented by one tab space only
9807c478bd9Sstevel@tonic-gate 			 */
9817c478bd9Sstevel@tonic-gate 			case MIB2_IP: {
9827c478bd9Sstevel@tonic-gate 				mib2_ip_t *i2 = (mib2_ip_t *)tempp2->valp;
9837c478bd9Sstevel@tonic-gate 				mib2_ip_t *i1 = (mib2_ip_t *)tempp1->valp;
9847c478bd9Sstevel@tonic-gate 				mib2_ip_t *d;
9857c478bd9Sstevel@tonic-gate 
9867c478bd9Sstevel@tonic-gate 				diffptr->group = tempp2->group;
9877c478bd9Sstevel@tonic-gate 				diffptr->mib_id = tempp2->mib_id;
9887c478bd9Sstevel@tonic-gate 				diffptr->length = tempp2->length;
9897c478bd9Sstevel@tonic-gate 				d = (mib2_ip_t *)calloc(tempp2->length, 1);
9907c478bd9Sstevel@tonic-gate 				if (d == NULL)
9917c478bd9Sstevel@tonic-gate 					goto mibdiff_out_of_memory;
9927c478bd9Sstevel@tonic-gate 				diffptr->valp = d;
9937c478bd9Sstevel@tonic-gate 				d->ipForwarding = i2->ipForwarding;
9947c478bd9Sstevel@tonic-gate 				d->ipDefaultTTL = i2->ipDefaultTTL;
9957c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipInReceives);
9967c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipInHdrErrors);
9977c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipInAddrErrors);
9987c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipInCksumErrs);
9997c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipForwDatagrams);
10007c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipForwProhibits);
10017c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipInUnknownProtos);
10027c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipInDiscards);
10037c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipInDelivers);
10047c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipOutRequests);
10057c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipOutDiscards);
10067c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipOutNoRoutes);
10077c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipReasmTimeout);
10087c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipReasmReqds);
10097c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipReasmOKs);
10107c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipReasmFails);
10117c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipReasmDuplicates);
10127c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipReasmPartDups);
10137c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipFragOKs);
10147c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipFragFails);
10157c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipFragCreates);
10167c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipRoutingDiscards);
10177c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, tcpInErrs);
10187c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, udpNoPorts);
10197c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, udpInCksumErrs);
10207c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, udpInOverflows);
10217c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, rawipInOverflows);
10227c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipsecInSucceeded);
10237c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipsecInFailed);
10247c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipInIPv6);
10257c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipOutIPv6);
10267c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, ipOutSwitchIPv6);
10277c478bd9Sstevel@tonic-gate 				prevp = diffptr++;
10287c478bd9Sstevel@tonic-gate 				break;
10297c478bd9Sstevel@tonic-gate 			}
10307c478bd9Sstevel@tonic-gate 			case MIB2_IP6: {
10317c478bd9Sstevel@tonic-gate 			mib2_ipv6IfStatsEntry_t *i2;
10327c478bd9Sstevel@tonic-gate 			mib2_ipv6IfStatsEntry_t *i1;
10337c478bd9Sstevel@tonic-gate 			mib2_ipv6IfStatsEntry_t *d;
10347c478bd9Sstevel@tonic-gate 
10357c478bd9Sstevel@tonic-gate 			i2 = (mib2_ipv6IfStatsEntry_t *)tempp2->valp;
10367c478bd9Sstevel@tonic-gate 			i1 = (mib2_ipv6IfStatsEntry_t *)tempp1->valp;
10377c478bd9Sstevel@tonic-gate 			diffptr->group = tempp2->group;
10387c478bd9Sstevel@tonic-gate 			diffptr->mib_id = tempp2->mib_id;
10397c478bd9Sstevel@tonic-gate 			diffptr->length = tempp2->length;
10407c478bd9Sstevel@tonic-gate 			d = (mib2_ipv6IfStatsEntry_t *)calloc(
10417c478bd9Sstevel@tonic-gate 			    tempp2->length, 1);
10427c478bd9Sstevel@tonic-gate 			if (d == NULL)
10437c478bd9Sstevel@tonic-gate 				goto mibdiff_out_of_memory;
10447c478bd9Sstevel@tonic-gate 			diffptr->valp = d;
10457c478bd9Sstevel@tonic-gate 			d->ipv6Forwarding = i2->ipv6Forwarding;
10467c478bd9Sstevel@tonic-gate 			d->ipv6DefaultHopLimit =
10477c478bd9Sstevel@tonic-gate 			    i2->ipv6DefaultHopLimit;
10487c478bd9Sstevel@tonic-gate 
10497c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6InReceives);
10507c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6InHdrErrors);
10517c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6InTooBigErrors);
10527c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6InNoRoutes);
10537c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6InAddrErrors);
10547c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6InUnknownProtos);
10557c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6InTruncatedPkts);
10567c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6InDiscards);
10577c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6InDelivers);
10587c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6OutForwDatagrams);
10597c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6OutRequests);
10607c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6OutDiscards);
10617c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6OutNoRoutes);
10627c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6OutFragOKs);
10637c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6OutFragFails);
10647c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6OutFragCreates);
10657c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6ReasmReqds);
10667c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6ReasmOKs);
10677c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6ReasmFails);
10687c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6InMcastPkts);
10697c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6OutMcastPkts);
10707c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6ReasmDuplicates);
10717c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6ReasmPartDups);
10727c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6ForwProhibits);
10737c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, udpInCksumErrs);
10747c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, udpInOverflows);
10757c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, rawipInOverflows);
10767c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6InIPv4);
10777c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6OutIPv4);
10787c478bd9Sstevel@tonic-gate 			MDIFF(d, i2, i1, ipv6OutSwitchIPv4);
10797c478bd9Sstevel@tonic-gate 			prevp = diffptr++;
10807c478bd9Sstevel@tonic-gate 			break;
10817c478bd9Sstevel@tonic-gate 			}
10827c478bd9Sstevel@tonic-gate 			case EXPER_DVMRP: {
10837c478bd9Sstevel@tonic-gate 				struct mrtstat *m2;
10847c478bd9Sstevel@tonic-gate 				struct mrtstat *m1;
10857c478bd9Sstevel@tonic-gate 				struct mrtstat *d;
10867c478bd9Sstevel@tonic-gate 
10877c478bd9Sstevel@tonic-gate 				m2 = (struct mrtstat *)tempp2->valp;
10887c478bd9Sstevel@tonic-gate 				m1 = (struct mrtstat *)tempp1->valp;
10897c478bd9Sstevel@tonic-gate 				diffptr->group = tempp2->group;
10907c478bd9Sstevel@tonic-gate 				diffptr->mib_id = tempp2->mib_id;
10917c478bd9Sstevel@tonic-gate 				diffptr->length = tempp2->length;
10927c478bd9Sstevel@tonic-gate 				d = (struct mrtstat *)calloc(tempp2->length, 1);
10937c478bd9Sstevel@tonic-gate 				if (d == NULL)
10947c478bd9Sstevel@tonic-gate 					goto mibdiff_out_of_memory;
10957c478bd9Sstevel@tonic-gate 				diffptr->valp = d;
10967c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_mfc_hits);
10977c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_mfc_misses);
10987c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_fwd_in);
10997c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_fwd_out);
11007c478bd9Sstevel@tonic-gate 				d->mrts_upcalls = m2->mrts_upcalls;
11017c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_fwd_drop);
11027c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_bad_tunnel);
11037c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_cant_tunnel);
11047c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_wrong_if);
11057c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_upq_ovflw);
11067c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_cache_cleanups);
11077c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_drop_sel);
11087c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_q_overflow);
11097c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_pkt2large);
11107c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_pim_badversion);
11117c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_pim_rcv_badcsum);
11127c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_pim_badregisters);
11137c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_pim_regforwards);
11147c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_pim_regsend_drops);
11157c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_pim_malformed);
11167c478bd9Sstevel@tonic-gate 				MDIFF(d, m2, m1, mrts_pim_nomemory);
11177c478bd9Sstevel@tonic-gate 				prevp = diffptr++;
11187c478bd9Sstevel@tonic-gate 				break;
11197c478bd9Sstevel@tonic-gate 			}
11207c478bd9Sstevel@tonic-gate 			case EXPER_IGMP: {
11217c478bd9Sstevel@tonic-gate 				struct igmpstat *i2;
11227c478bd9Sstevel@tonic-gate 				struct igmpstat *i1;
11237c478bd9Sstevel@tonic-gate 				struct igmpstat *d;
11247c478bd9Sstevel@tonic-gate 
11257c478bd9Sstevel@tonic-gate 				i2 = (struct igmpstat *)tempp2->valp;
11267c478bd9Sstevel@tonic-gate 				i1 = (struct igmpstat *)tempp1->valp;
11277c478bd9Sstevel@tonic-gate 				diffptr->group = tempp2->group;
11287c478bd9Sstevel@tonic-gate 				diffptr->mib_id = tempp2->mib_id;
11297c478bd9Sstevel@tonic-gate 				diffptr->length = tempp2->length;
11307c478bd9Sstevel@tonic-gate 				d = (struct igmpstat *)calloc(
11317c478bd9Sstevel@tonic-gate 				    tempp2->length, 1);
11327c478bd9Sstevel@tonic-gate 				if (d == NULL)
11337c478bd9Sstevel@tonic-gate 					goto mibdiff_out_of_memory;
11347c478bd9Sstevel@tonic-gate 				diffptr->valp = d;
11357c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, igps_rcv_total);
11367c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, igps_rcv_tooshort);
11377c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, igps_rcv_badsum);
11387c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, igps_rcv_queries);
11397c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, igps_rcv_badqueries);
11407c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, igps_rcv_reports);
11417c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, igps_rcv_badreports);
11427c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, igps_rcv_ourreports);
11437c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, igps_snd_reports);
11447c478bd9Sstevel@tonic-gate 				prevp = diffptr++;
11457c478bd9Sstevel@tonic-gate 				break;
11467c478bd9Sstevel@tonic-gate 			}
11477c478bd9Sstevel@tonic-gate 			case MIB2_ICMP: {
11487c478bd9Sstevel@tonic-gate 				mib2_icmp_t *i2;
11497c478bd9Sstevel@tonic-gate 				mib2_icmp_t *i1;
11507c478bd9Sstevel@tonic-gate 				mib2_icmp_t *d;
11517c478bd9Sstevel@tonic-gate 
11527c478bd9Sstevel@tonic-gate 				i2 = (mib2_icmp_t *)tempp2->valp;
11537c478bd9Sstevel@tonic-gate 				i1 = (mib2_icmp_t *)tempp1->valp;
11547c478bd9Sstevel@tonic-gate 				diffptr->group = tempp2->group;
11557c478bd9Sstevel@tonic-gate 				diffptr->mib_id = tempp2->mib_id;
11567c478bd9Sstevel@tonic-gate 				diffptr->length = tempp2->length;
11577c478bd9Sstevel@tonic-gate 				d = (mib2_icmp_t *)calloc(tempp2->length, 1);
11587c478bd9Sstevel@tonic-gate 				if (d == NULL)
11597c478bd9Sstevel@tonic-gate 					goto mibdiff_out_of_memory;
11607c478bd9Sstevel@tonic-gate 				diffptr->valp = d;
11617c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInMsgs);
11627c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInErrors);
11637c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInCksumErrs);
11647c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInUnknowns);
11657c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInDestUnreachs);
11667c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInTimeExcds);
11677c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInParmProbs);
11687c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInSrcQuenchs);
11697c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInRedirects);
11707c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInBadRedirects);
11717c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInEchos);
11727c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInEchoReps);
11737c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInTimestamps);
11747c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInAddrMasks);
11757c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInAddrMaskReps);
11767c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInFragNeeded);
11777c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutMsgs);
11787c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutDrops);
11797c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutErrors);
11807c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutDestUnreachs);
11817c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutTimeExcds);
11827c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutParmProbs);
11837c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutSrcQuenchs);
11847c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutRedirects);
11857c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutEchos);
11867c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutEchoReps);
11877c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutTimestamps);
11887c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutTimestampReps);
11897c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutAddrMasks);
11907c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutAddrMaskReps);
11917c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpOutFragNeeded);
11927c478bd9Sstevel@tonic-gate 				MDIFF(d, i2, i1, icmpInOverflows);
11937c478bd9Sstevel@tonic-gate 				prevp = diffptr++;
11947c478bd9Sstevel@tonic-gate 				break;
11957c478bd9Sstevel@tonic-gate 			}
11967c478bd9Sstevel@tonic-gate 			case MIB2_ICMP6: {
11977c478bd9Sstevel@tonic-gate 	mib2_ipv6IfIcmpEntry_t *i2;
11987c478bd9Sstevel@tonic-gate 	mib2_ipv6IfIcmpEntry_t *i1;
11997c478bd9Sstevel@tonic-gate 	mib2_ipv6IfIcmpEntry_t *d;
12007c478bd9Sstevel@tonic-gate 
12017c478bd9Sstevel@tonic-gate 	i2 = (mib2_ipv6IfIcmpEntry_t *)tempp2->valp;
12027c478bd9Sstevel@tonic-gate 	i1 = (mib2_ipv6IfIcmpEntry_t *)tempp1->valp;
12037c478bd9Sstevel@tonic-gate 	diffptr->group = tempp2->group;
12047c478bd9Sstevel@tonic-gate 	diffptr->mib_id = tempp2->mib_id;
12057c478bd9Sstevel@tonic-gate 	diffptr->length = tempp2->length;
12067c478bd9Sstevel@tonic-gate 	d = (mib2_ipv6IfIcmpEntry_t *)calloc(tempp2->length, 1);
12077c478bd9Sstevel@tonic-gate 	if (d == NULL)
12087c478bd9Sstevel@tonic-gate 		goto mibdiff_out_of_memory;
12097c478bd9Sstevel@tonic-gate 	diffptr->valp = d;
12107c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInMsgs);
12117c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInErrors);
12127c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInDestUnreachs);
12137c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInAdminProhibs);
12147c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInTimeExcds);
12157c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInParmProblems);
12167c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInPktTooBigs);
12177c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInEchos);
12187c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInEchoReplies);
12197c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInRouterSolicits);
12207c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInRouterAdvertisements);
12217c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInNeighborSolicits);
12227c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInNeighborAdvertisements);
12237c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInRedirects);
12247c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInBadRedirects);
12257c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInGroupMembQueries);
12267c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInGroupMembResponses);
12277c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInGroupMembReductions);
12287c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpInOverflows);
12297c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutMsgs);
12307c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutErrors);
12317c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutDestUnreachs);
12327c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutAdminProhibs);
12337c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutTimeExcds);
12347c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutParmProblems);
12357c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutPktTooBigs);
12367c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutEchos);
12377c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutEchoReplies);
12387c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutRouterSolicits);
12397c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutRouterAdvertisements);
12407c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutNeighborSolicits);
12417c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutNeighborAdvertisements);
12427c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutRedirects);
12437c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutGroupMembQueries);
12447c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutGroupMembResponses);
12457c478bd9Sstevel@tonic-gate 	MDIFF(d, i2, i1, ipv6IfIcmpOutGroupMembReductions);
12467c478bd9Sstevel@tonic-gate 	prevp = diffptr++;
12477c478bd9Sstevel@tonic-gate 	break;
12487c478bd9Sstevel@tonic-gate 			}
12497c478bd9Sstevel@tonic-gate 			case MIB2_TCP: {
12507c478bd9Sstevel@tonic-gate 				mib2_tcp_t *t2;
12517c478bd9Sstevel@tonic-gate 				mib2_tcp_t *t1;
12527c478bd9Sstevel@tonic-gate 				mib2_tcp_t *d;
12537c478bd9Sstevel@tonic-gate 
12547c478bd9Sstevel@tonic-gate 				t2 = (mib2_tcp_t *)tempp2->valp;
12557c478bd9Sstevel@tonic-gate 				t1 = (mib2_tcp_t *)tempp1->valp;
12567c478bd9Sstevel@tonic-gate 				diffptr->group = tempp2->group;
12577c478bd9Sstevel@tonic-gate 				diffptr->mib_id = tempp2->mib_id;
12587c478bd9Sstevel@tonic-gate 				diffptr->length = tempp2->length;
12597c478bd9Sstevel@tonic-gate 				d = (mib2_tcp_t *)calloc(tempp2->length, 1);
12607c478bd9Sstevel@tonic-gate 				if (d == NULL)
12617c478bd9Sstevel@tonic-gate 					goto mibdiff_out_of_memory;
12627c478bd9Sstevel@tonic-gate 				diffptr->valp = d;
12637c478bd9Sstevel@tonic-gate 				d->tcpRtoMin = t2->tcpRtoMin;
12647c478bd9Sstevel@tonic-gate 				d->tcpRtoMax = t2->tcpRtoMax;
12657c478bd9Sstevel@tonic-gate 				d->tcpMaxConn = t2->tcpMaxConn;
12667c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpActiveOpens);
12677c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpPassiveOpens);
12687c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpAttemptFails);
12697c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpEstabResets);
12707c478bd9Sstevel@tonic-gate 				d->tcpCurrEstab = t2->tcpCurrEstab;
12713173664eSapersson 				MDIFF(d, t2, t1, tcpHCOutSegs);
12727c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpOutDataSegs);
12737c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpOutDataBytes);
12747c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpRetransSegs);
12757c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpRetransBytes);
12767c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpOutAck);
12777c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpOutAckDelayed);
12787c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpOutUrg);
12797c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpOutWinUpdate);
12807c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpOutWinProbe);
12817c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpOutControl);
12827c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpOutRsts);
12837c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpOutFastRetrans);
12843173664eSapersson 				MDIFF(d, t2, t1, tcpHCInSegs);
12857c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInAckSegs);
12867c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInAckBytes);
12877c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInDupAck);
12887c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInAckUnsent);
12897c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInDataInorderSegs);
12907c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInDataInorderBytes);
12917c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInDataUnorderSegs);
12927c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInDataUnorderBytes);
12937c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInDataDupSegs);
12947c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInDataDupBytes);
12957c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInDataPartDupSegs);
12967c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInDataPartDupBytes);
12977c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInDataPastWinSegs);
12987c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInDataPastWinBytes);
12997c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInWinProbe);
13007c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInWinUpdate);
13017c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpInClosed);
13027c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpRttNoUpdate);
13037c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpRttUpdate);
13047c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpTimRetrans);
13057c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpTimRetransDrop);
13067c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpTimKeepalive);
13077c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpTimKeepaliveProbe);
13087c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpTimKeepaliveDrop);
13097c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpListenDrop);
13107c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpListenDropQ0);
13117c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpHalfOpenDrop);
13127c478bd9Sstevel@tonic-gate 				MDIFF(d, t2, t1, tcpOutSackRetransSegs);
13137c478bd9Sstevel@tonic-gate 				prevp = diffptr++;
13147c478bd9Sstevel@tonic-gate 				break;
13157c478bd9Sstevel@tonic-gate 			}
13167c478bd9Sstevel@tonic-gate 			case MIB2_UDP: {
13177c478bd9Sstevel@tonic-gate 				mib2_udp_t *u2;
13187c478bd9Sstevel@tonic-gate 				mib2_udp_t *u1;
13197c478bd9Sstevel@tonic-gate 				mib2_udp_t *d;
13207c478bd9Sstevel@tonic-gate 
13217c478bd9Sstevel@tonic-gate 				u2 = (mib2_udp_t *)tempp2->valp;
13227c478bd9Sstevel@tonic-gate 				u1 = (mib2_udp_t *)tempp1->valp;
13237c478bd9Sstevel@tonic-gate 				diffptr->group = tempp2->group;
13247c478bd9Sstevel@tonic-gate 				diffptr->mib_id = tempp2->mib_id;
13257c478bd9Sstevel@tonic-gate 				diffptr->length = tempp2->length;
13267c478bd9Sstevel@tonic-gate 				d = (mib2_udp_t *)calloc(tempp2->length, 1);
13277c478bd9Sstevel@tonic-gate 				if (d == NULL)
13287c478bd9Sstevel@tonic-gate 					goto mibdiff_out_of_memory;
13297c478bd9Sstevel@tonic-gate 				diffptr->valp = d;
13303173664eSapersson 				MDIFF(d, u2, u1, udpHCInDatagrams);
13317c478bd9Sstevel@tonic-gate 				MDIFF(d, u2, u1, udpInErrors);
13323173664eSapersson 				MDIFF(d, u2, u1, udpHCOutDatagrams);
13337c478bd9Sstevel@tonic-gate 				MDIFF(d, u2, u1, udpOutErrors);
13347c478bd9Sstevel@tonic-gate 				prevp = diffptr++;
13357c478bd9Sstevel@tonic-gate 				break;
13367c478bd9Sstevel@tonic-gate 			}
13377c478bd9Sstevel@tonic-gate 			case MIB2_SCTP: {
13387c478bd9Sstevel@tonic-gate 				mib2_sctp_t *s2;
13397c478bd9Sstevel@tonic-gate 				mib2_sctp_t *s1;
13407c478bd9Sstevel@tonic-gate 				mib2_sctp_t *d;
13417c478bd9Sstevel@tonic-gate 
13427c478bd9Sstevel@tonic-gate 				s2 = (mib2_sctp_t *)tempp2->valp;
13437c478bd9Sstevel@tonic-gate 				s1 = (mib2_sctp_t *)tempp1->valp;
13447c478bd9Sstevel@tonic-gate 				diffptr->group = tempp2->group;
13457c478bd9Sstevel@tonic-gate 				diffptr->mib_id = tempp2->mib_id;
13467c478bd9Sstevel@tonic-gate 				diffptr->length = tempp2->length;
13477c478bd9Sstevel@tonic-gate 				d = (mib2_sctp_t *)calloc(tempp2->length, 1);
13487c478bd9Sstevel@tonic-gate 				if (d == NULL)
13497c478bd9Sstevel@tonic-gate 					goto mibdiff_out_of_memory;
13507c478bd9Sstevel@tonic-gate 				diffptr->valp = d;
13517c478bd9Sstevel@tonic-gate 				d->sctpRtoAlgorithm = s2->sctpRtoAlgorithm;
13527c478bd9Sstevel@tonic-gate 				d->sctpRtoMin = s2->sctpRtoMin;
13537c478bd9Sstevel@tonic-gate 				d->sctpRtoMax = s2->sctpRtoMax;
13547c478bd9Sstevel@tonic-gate 				d->sctpRtoInitial = s2->sctpRtoInitial;
13557c478bd9Sstevel@tonic-gate 				d->sctpMaxAssocs = s2->sctpMaxAssocs;
13567c478bd9Sstevel@tonic-gate 				d->sctpValCookieLife = s2->sctpValCookieLife;
13577c478bd9Sstevel@tonic-gate 				d->sctpMaxInitRetr = s2->sctpMaxInitRetr;
13587c478bd9Sstevel@tonic-gate 				d->sctpCurrEstab = s2->sctpCurrEstab;
13597c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpActiveEstab);
13607c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpPassiveEstab);
13617c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpAborted);
13627c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpShutdowns);
13637c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpOutOfBlue);
13647c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpChecksumError);
13657c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpOutCtrlChunks);
13667c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpOutOrderChunks);
13677c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpOutUnorderChunks);
13687c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpRetransChunks);
13697c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpOutAck);
13707c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpOutAckDelayed);
13717c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpOutWinUpdate);
13727c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpOutFastRetrans);
13737c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpOutWinProbe);
13747c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpInCtrlChunks);
13757c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpInOrderChunks);
13767c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpInUnorderChunks);
13777c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpInAck);
13787c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpInDupAck);
13797c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpInAckUnsent);
13807c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpFragUsrMsgs);
13817c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpReasmUsrMsgs);
13827c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpOutSCTPPkts);
13837c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpInSCTPPkts);
13847c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpInInvalidCookie);
13857c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpTimRetrans);
13867c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpTimRetransDrop);
13877c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpTimHeartBeatProbe);
13887c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpTimHeartBeatDrop);
13897c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpListenDrop);
13907c478bd9Sstevel@tonic-gate 				MDIFF(d, s2, s1, sctpInClosed);
13917c478bd9Sstevel@tonic-gate 				prevp = diffptr++;
13927c478bd9Sstevel@tonic-gate 				break;
13937c478bd9Sstevel@tonic-gate 			}
13947c478bd9Sstevel@tonic-gate 			case EXPER_RAWIP: {
13957c478bd9Sstevel@tonic-gate 				mib2_rawip_t *r2;
13967c478bd9Sstevel@tonic-gate 				mib2_rawip_t *r1;
13977c478bd9Sstevel@tonic-gate 				mib2_rawip_t *d;
13987c478bd9Sstevel@tonic-gate 
13997c478bd9Sstevel@tonic-gate 				r2 = (mib2_rawip_t *)tempp2->valp;
14007c478bd9Sstevel@tonic-gate 				r1 = (mib2_rawip_t *)tempp1->valp;
14017c478bd9Sstevel@tonic-gate 				diffptr->group = tempp2->group;
14027c478bd9Sstevel@tonic-gate 				diffptr->mib_id = tempp2->mib_id;
14037c478bd9Sstevel@tonic-gate 				diffptr->length = tempp2->length;
14047c478bd9Sstevel@tonic-gate 				d = (mib2_rawip_t *)calloc(tempp2->length, 1);
14057c478bd9Sstevel@tonic-gate 				if (d == NULL)
14067c478bd9Sstevel@tonic-gate 					goto mibdiff_out_of_memory;
14077c478bd9Sstevel@tonic-gate 				diffptr->valp = d;
14087c478bd9Sstevel@tonic-gate 				MDIFF(d, r2, r1, rawipInDatagrams);
14097c478bd9Sstevel@tonic-gate 				MDIFF(d, r2, r1, rawipInErrors);
14107c478bd9Sstevel@tonic-gate 				MDIFF(d, r2, r1, rawipInCksumErrs);
14117c478bd9Sstevel@tonic-gate 				MDIFF(d, r2, r1, rawipOutDatagrams);
14127c478bd9Sstevel@tonic-gate 				MDIFF(d, r2, r1, rawipOutErrors);
14137c478bd9Sstevel@tonic-gate 				prevp = diffptr++;
14147c478bd9Sstevel@tonic-gate 				break;
14157c478bd9Sstevel@tonic-gate 			}
14167c478bd9Sstevel@tonic-gate 			/*
14177c478bd9Sstevel@tonic-gate 			 * there are more "group" types but they aren't
14187c478bd9Sstevel@tonic-gate 			 * required for the -s and -Ms options
14197c478bd9Sstevel@tonic-gate 			 */
14207c478bd9Sstevel@tonic-gate 			}
14217c478bd9Sstevel@tonic-gate 		} /* 'for' loop 2 ends */
14227c478bd9Sstevel@tonic-gate 		tempp1 = NULL;
14237c478bd9Sstevel@tonic-gate 	} /* 'for' loop 1 ends */
14247c478bd9Sstevel@tonic-gate 	tempp2 = NULL;
14257c478bd9Sstevel@tonic-gate 	diffptr--;
14267c478bd9Sstevel@tonic-gate 	diffptr->next_item = NULL;
14277c478bd9Sstevel@tonic-gate 	return (diffp);
14287c478bd9Sstevel@tonic-gate 
14297c478bd9Sstevel@tonic-gate mibdiff_out_of_memory:;
14307c478bd9Sstevel@tonic-gate 	mib_item_destroy(&diffp);
14317c478bd9Sstevel@tonic-gate 	return (NULL);
14327c478bd9Sstevel@tonic-gate }
14337c478bd9Sstevel@tonic-gate 
14347c478bd9Sstevel@tonic-gate /*
14357c478bd9Sstevel@tonic-gate  * mib_item_destroy: cleans up a mib_item_t *
14367c478bd9Sstevel@tonic-gate  * that was created by calling mib_item_dup or
14377c478bd9Sstevel@tonic-gate  * mib_item_diff
14387c478bd9Sstevel@tonic-gate  */
14397c478bd9Sstevel@tonic-gate static void
14407c478bd9Sstevel@tonic-gate mib_item_destroy(mib_item_t **itemp) {
14417c478bd9Sstevel@tonic-gate 	int	nitems = 0;
14427c478bd9Sstevel@tonic-gate 	int	c = 0;
14437c478bd9Sstevel@tonic-gate 	mib_item_t *tempp;
14447c478bd9Sstevel@tonic-gate 
14457c478bd9Sstevel@tonic-gate 	if (itemp == NULL || *itemp == NULL)
14467c478bd9Sstevel@tonic-gate 		return;
14477c478bd9Sstevel@tonic-gate 
14487c478bd9Sstevel@tonic-gate 	for (tempp = *itemp; tempp != NULL; tempp = tempp->next_item)
14497c478bd9Sstevel@tonic-gate 		if (tempp->mib_id == 0)
14507c478bd9Sstevel@tonic-gate 			nitems++;
14517c478bd9Sstevel@tonic-gate 		else
14527c478bd9Sstevel@tonic-gate 			return;	/* cannot destroy! */
14537c478bd9Sstevel@tonic-gate 
14547c478bd9Sstevel@tonic-gate 	if (nitems == 0)
14557c478bd9Sstevel@tonic-gate 		return;		/* cannot destroy! */
14567c478bd9Sstevel@tonic-gate 
14577c478bd9Sstevel@tonic-gate 	for (c = nitems - 1; c >= 0; c--) {
14587c478bd9Sstevel@tonic-gate 		if ((itemp[0][c]).valp != NULL)
14597c478bd9Sstevel@tonic-gate 			free((itemp[0][c]).valp);
14607c478bd9Sstevel@tonic-gate 	}
14617c478bd9Sstevel@tonic-gate 	free(*itemp);
14627c478bd9Sstevel@tonic-gate 
14637c478bd9Sstevel@tonic-gate 	*itemp = NULL;
14647c478bd9Sstevel@tonic-gate }
14657c478bd9Sstevel@tonic-gate 
14667c478bd9Sstevel@tonic-gate /* Compare two Octet_ts.  Return B_TRUE if they match, B_FALSE if not. */
14677c478bd9Sstevel@tonic-gate static boolean_t
14687c478bd9Sstevel@tonic-gate octetstrmatch(const Octet_t *a, const Octet_t *b)
14697c478bd9Sstevel@tonic-gate {
14707c478bd9Sstevel@tonic-gate 	if (a == NULL || b == NULL)
14717c478bd9Sstevel@tonic-gate 		return (B_FALSE);
14727c478bd9Sstevel@tonic-gate 
14737c478bd9Sstevel@tonic-gate 	if (a->o_length != b->o_length)
14747c478bd9Sstevel@tonic-gate 		return (B_FALSE);
14757c478bd9Sstevel@tonic-gate 
14767c478bd9Sstevel@tonic-gate 	return (memcmp(a->o_bytes, b->o_bytes, a->o_length) == 0);
14777c478bd9Sstevel@tonic-gate }
14787c478bd9Sstevel@tonic-gate 
14797c478bd9Sstevel@tonic-gate /* If octetstr() changes make an appropriate change to STR_EXPAND */
14807c478bd9Sstevel@tonic-gate static char *
148145916cd2Sjpk octetstr(const Octet_t *op, int code, char *dst, uint_t dstlen)
14827c478bd9Sstevel@tonic-gate {
14837c478bd9Sstevel@tonic-gate 	int	i;
14847c478bd9Sstevel@tonic-gate 	char	*cp;
14857c478bd9Sstevel@tonic-gate 
14867c478bd9Sstevel@tonic-gate 	cp = dst;
14877c478bd9Sstevel@tonic-gate 	if (op) {
14887c478bd9Sstevel@tonic-gate 		for (i = 0; i < op->o_length; i++) {
14897c478bd9Sstevel@tonic-gate 			switch (code) {
14907c478bd9Sstevel@tonic-gate 			case 'd':
14917c478bd9Sstevel@tonic-gate 				if (cp - dst + 4 > dstlen) {
14927c478bd9Sstevel@tonic-gate 					*cp = '\0';
14937c478bd9Sstevel@tonic-gate 					return (dst);
14947c478bd9Sstevel@tonic-gate 				}
14957c478bd9Sstevel@tonic-gate 				(void) snprintf(cp, 5, "%d.",
14967c478bd9Sstevel@tonic-gate 				    0xff & op->o_bytes[i]);
14977c478bd9Sstevel@tonic-gate 				cp = strchr(cp, '\0');
14987c478bd9Sstevel@tonic-gate 				break;
14997c478bd9Sstevel@tonic-gate 			case 'a':
15007c478bd9Sstevel@tonic-gate 				if (cp - dst + 1 > dstlen) {
15017c478bd9Sstevel@tonic-gate 					*cp = '\0';
15027c478bd9Sstevel@tonic-gate 					return (dst);
15037c478bd9Sstevel@tonic-gate 				}
15047c478bd9Sstevel@tonic-gate 				*cp++ = op->o_bytes[i];
15057c478bd9Sstevel@tonic-gate 				break;
15067c478bd9Sstevel@tonic-gate 			case 'h':
15077c478bd9Sstevel@tonic-gate 			default:
15087c478bd9Sstevel@tonic-gate 				if (cp - dst + 3 > dstlen) {
15097c478bd9Sstevel@tonic-gate 					*cp = '\0';
15107c478bd9Sstevel@tonic-gate 					return (dst);
15117c478bd9Sstevel@tonic-gate 				}
15127c478bd9Sstevel@tonic-gate 				(void) snprintf(cp, 4, "%02x:",
15137c478bd9Sstevel@tonic-gate 				    0xff & op->o_bytes[i]);
15147c478bd9Sstevel@tonic-gate 				cp += 3;
15157c478bd9Sstevel@tonic-gate 				break;
15167c478bd9Sstevel@tonic-gate 			}
15177c478bd9Sstevel@tonic-gate 		}
15187c478bd9Sstevel@tonic-gate 	}
15197c478bd9Sstevel@tonic-gate 	if (code != 'a' && cp != dst)
15207c478bd9Sstevel@tonic-gate 		cp--;
15217c478bd9Sstevel@tonic-gate 	*cp = '\0';
15227c478bd9Sstevel@tonic-gate 	return (dst);
15237c478bd9Sstevel@tonic-gate }
15247c478bd9Sstevel@tonic-gate 
152545916cd2Sjpk static const char *
152645916cd2Sjpk mitcp_state(int state, const mib2_transportMLPEntry_t *attr)
15277c478bd9Sstevel@tonic-gate {
152845916cd2Sjpk 	static char tcpsbuf[50];
152945916cd2Sjpk 	const char *cp;
15307c478bd9Sstevel@tonic-gate 
15317c478bd9Sstevel@tonic-gate 	switch (state) {
15327c478bd9Sstevel@tonic-gate 	case TCPS_CLOSED:
15337c478bd9Sstevel@tonic-gate 		cp = "CLOSED";
15347c478bd9Sstevel@tonic-gate 		break;
15357c478bd9Sstevel@tonic-gate 	case TCPS_IDLE:
15367c478bd9Sstevel@tonic-gate 		cp = "IDLE";
15377c478bd9Sstevel@tonic-gate 		break;
15387c478bd9Sstevel@tonic-gate 	case TCPS_BOUND:
15397c478bd9Sstevel@tonic-gate 		cp = "BOUND";
15407c478bd9Sstevel@tonic-gate 		break;
15417c478bd9Sstevel@tonic-gate 	case TCPS_LISTEN:
15427c478bd9Sstevel@tonic-gate 		cp = "LISTEN";
15437c478bd9Sstevel@tonic-gate 		break;
15447c478bd9Sstevel@tonic-gate 	case TCPS_SYN_SENT:
15457c478bd9Sstevel@tonic-gate 		cp = "SYN_SENT";
15467c478bd9Sstevel@tonic-gate 		break;
15477c478bd9Sstevel@tonic-gate 	case TCPS_SYN_RCVD:
15487c478bd9Sstevel@tonic-gate 		cp = "SYN_RCVD";
15497c478bd9Sstevel@tonic-gate 		break;
15507c478bd9Sstevel@tonic-gate 	case TCPS_ESTABLISHED:
15517c478bd9Sstevel@tonic-gate 		cp = "ESTABLISHED";
15527c478bd9Sstevel@tonic-gate 		break;
15537c478bd9Sstevel@tonic-gate 	case TCPS_CLOSE_WAIT:
15547c478bd9Sstevel@tonic-gate 		cp = "CLOSE_WAIT";
15557c478bd9Sstevel@tonic-gate 		break;
15567c478bd9Sstevel@tonic-gate 	case TCPS_FIN_WAIT_1:
15577c478bd9Sstevel@tonic-gate 		cp = "FIN_WAIT_1";
15587c478bd9Sstevel@tonic-gate 		break;
15597c478bd9Sstevel@tonic-gate 	case TCPS_CLOSING:
15607c478bd9Sstevel@tonic-gate 		cp = "CLOSING";
15617c478bd9Sstevel@tonic-gate 		break;
15627c478bd9Sstevel@tonic-gate 	case TCPS_LAST_ACK:
15637c478bd9Sstevel@tonic-gate 		cp = "LAST_ACK";
15647c478bd9Sstevel@tonic-gate 		break;
15657c478bd9Sstevel@tonic-gate 	case TCPS_FIN_WAIT_2:
15667c478bd9Sstevel@tonic-gate 		cp = "FIN_WAIT_2";
15677c478bd9Sstevel@tonic-gate 		break;
15687c478bd9Sstevel@tonic-gate 	case TCPS_TIME_WAIT:
15697c478bd9Sstevel@tonic-gate 		cp = "TIME_WAIT";
15707c478bd9Sstevel@tonic-gate 		break;
15717c478bd9Sstevel@tonic-gate 	default:
15727c478bd9Sstevel@tonic-gate 		(void) snprintf(tcpsbuf, sizeof (tcpsbuf),
15737c478bd9Sstevel@tonic-gate 		    "UnknownState(%d)", state);
15747c478bd9Sstevel@tonic-gate 		cp = tcpsbuf;
15757c478bd9Sstevel@tonic-gate 		break;
15767c478bd9Sstevel@tonic-gate 	}
157745916cd2Sjpk 
157845916cd2Sjpk 	if (RSECflag && attr != NULL && attr->tme_flags != 0) {
157945916cd2Sjpk 		if (cp != tcpsbuf) {
158045916cd2Sjpk 			(void) strlcpy(tcpsbuf, cp, sizeof (tcpsbuf));
158145916cd2Sjpk 			cp = tcpsbuf;
158245916cd2Sjpk 		}
158345916cd2Sjpk 		if (attr->tme_flags & MIB2_TMEF_PRIVATE)
158445916cd2Sjpk 			(void) strlcat(tcpsbuf, " P", sizeof (tcpsbuf));
158545916cd2Sjpk 		if (attr->tme_flags & MIB2_TMEF_SHARED)
158645916cd2Sjpk 			(void) strlcat(tcpsbuf, " S", sizeof (tcpsbuf));
158745916cd2Sjpk 	}
158845916cd2Sjpk 
158945916cd2Sjpk 	return (cp);
159045916cd2Sjpk }
159145916cd2Sjpk 
159245916cd2Sjpk static const char *
159345916cd2Sjpk miudp_state(int state, const mib2_transportMLPEntry_t *attr)
159445916cd2Sjpk {
159545916cd2Sjpk 	static char udpsbuf[50];
159645916cd2Sjpk 	const char *cp;
159745916cd2Sjpk 
159845916cd2Sjpk 	switch (state) {
159945916cd2Sjpk 	case MIB2_UDP_unbound:
160045916cd2Sjpk 		cp = "Unbound";
160145916cd2Sjpk 		break;
160245916cd2Sjpk 	case MIB2_UDP_idle:
160345916cd2Sjpk 		cp = "Idle";
160445916cd2Sjpk 		break;
160545916cd2Sjpk 	case MIB2_UDP_connected:
160645916cd2Sjpk 		cp = "Connected";
160745916cd2Sjpk 		break;
160845916cd2Sjpk 	default:
160945916cd2Sjpk 		(void) snprintf(udpsbuf, sizeof (udpsbuf),
161045916cd2Sjpk 		    "Unknown State(%d)", state);
161145916cd2Sjpk 		cp = udpsbuf;
161245916cd2Sjpk 		break;
161345916cd2Sjpk 	}
161445916cd2Sjpk 
161545916cd2Sjpk 	if (RSECflag && attr != NULL && attr->tme_flags != 0) {
161645916cd2Sjpk 		if (cp != udpsbuf) {
161745916cd2Sjpk 			(void) strlcpy(udpsbuf, cp, sizeof (udpsbuf));
161845916cd2Sjpk 			cp = udpsbuf;
161945916cd2Sjpk 		}
162045916cd2Sjpk 		if (attr->tme_flags & MIB2_TMEF_PRIVATE)
162145916cd2Sjpk 			(void) strlcat(udpsbuf, " P", sizeof (udpsbuf));
162245916cd2Sjpk 		if (attr->tme_flags & MIB2_TMEF_SHARED)
162345916cd2Sjpk 			(void) strlcat(udpsbuf, " S", sizeof (udpsbuf));
162445916cd2Sjpk 	}
162545916cd2Sjpk 
16267c478bd9Sstevel@tonic-gate 	return (cp);
16277c478bd9Sstevel@tonic-gate }
16287c478bd9Sstevel@tonic-gate 
16297c478bd9Sstevel@tonic-gate static int odd;
16307c478bd9Sstevel@tonic-gate 
16317c478bd9Sstevel@tonic-gate static void
16327c478bd9Sstevel@tonic-gate prval_init(void)
16337c478bd9Sstevel@tonic-gate {
16347c478bd9Sstevel@tonic-gate 	odd = 0;
16357c478bd9Sstevel@tonic-gate }
16367c478bd9Sstevel@tonic-gate 
16377c478bd9Sstevel@tonic-gate static void
16387c478bd9Sstevel@tonic-gate prval(char *str, Counter val)
16397c478bd9Sstevel@tonic-gate {
16407c478bd9Sstevel@tonic-gate 	(void) printf("\t%-20s=%6u", str, val);
16417c478bd9Sstevel@tonic-gate 	if (odd++ & 1)
16427c478bd9Sstevel@tonic-gate 		(void) putchar('\n');
16437c478bd9Sstevel@tonic-gate }
16447c478bd9Sstevel@tonic-gate 
16457c478bd9Sstevel@tonic-gate static void
16467c478bd9Sstevel@tonic-gate prval64(char *str, Counter64 val)
16477c478bd9Sstevel@tonic-gate {
16487c478bd9Sstevel@tonic-gate 	(void) printf("\t%-20s=%6llu", str, val);
16497c478bd9Sstevel@tonic-gate 	if (odd++ & 1)
16507c478bd9Sstevel@tonic-gate 		(void) putchar('\n');
16517c478bd9Sstevel@tonic-gate }
16527c478bd9Sstevel@tonic-gate 
16537c478bd9Sstevel@tonic-gate static void
16547c478bd9Sstevel@tonic-gate pr_int_val(char *str, int val)
16557c478bd9Sstevel@tonic-gate {
16567c478bd9Sstevel@tonic-gate 	(void) printf("\t%-20s=%6d", str, val);
16577c478bd9Sstevel@tonic-gate 	if (odd++ & 1)
16587c478bd9Sstevel@tonic-gate 		(void) putchar('\n');
16597c478bd9Sstevel@tonic-gate }
16607c478bd9Sstevel@tonic-gate 
16617c478bd9Sstevel@tonic-gate static void
16627c478bd9Sstevel@tonic-gate pr_sctp_rtoalgo(char *str, int val)
16637c478bd9Sstevel@tonic-gate {
16647c478bd9Sstevel@tonic-gate 	(void) printf("\t%-20s=", str);
16657c478bd9Sstevel@tonic-gate 	switch (val) {
16667c478bd9Sstevel@tonic-gate 		case MIB2_SCTP_RTOALGO_OTHER:
16677c478bd9Sstevel@tonic-gate 			(void) printf("%6.6s", "other");
16687c478bd9Sstevel@tonic-gate 			break;
16697c478bd9Sstevel@tonic-gate 
16707c478bd9Sstevel@tonic-gate 		case MIB2_SCTP_RTOALGO_VANJ:
16717c478bd9Sstevel@tonic-gate 			(void) printf("%6.6s", "vanj");
16727c478bd9Sstevel@tonic-gate 			break;
16737c478bd9Sstevel@tonic-gate 
16747c478bd9Sstevel@tonic-gate 		default:
16757c478bd9Sstevel@tonic-gate 			(void) printf("%6d", val);
16767c478bd9Sstevel@tonic-gate 			break;
16777c478bd9Sstevel@tonic-gate 	}
16787c478bd9Sstevel@tonic-gate 	if (odd++ & 1)
16797c478bd9Sstevel@tonic-gate 		(void) putchar('\n');
16807c478bd9Sstevel@tonic-gate }
16817c478bd9Sstevel@tonic-gate 
16827c478bd9Sstevel@tonic-gate static void
16837c478bd9Sstevel@tonic-gate prval_end(void)
16847c478bd9Sstevel@tonic-gate {
16857c478bd9Sstevel@tonic-gate 	if (odd++ & 1)
16867c478bd9Sstevel@tonic-gate 		(void) putchar('\n');
16877c478bd9Sstevel@tonic-gate }
16887c478bd9Sstevel@tonic-gate 
16897c478bd9Sstevel@tonic-gate /* Extract constant sizes */
16907c478bd9Sstevel@tonic-gate static void
16917c478bd9Sstevel@tonic-gate mib_get_constants(mib_item_t *item)
16927c478bd9Sstevel@tonic-gate {
16937c478bd9Sstevel@tonic-gate 	/* 'for' loop 1: */
16947c478bd9Sstevel@tonic-gate 	for (; item; item = item->next_item) {
16957c478bd9Sstevel@tonic-gate 		if (item->mib_id != 0)
16967c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
16977c478bd9Sstevel@tonic-gate 
16987c478bd9Sstevel@tonic-gate 		switch (item->group) {
16997c478bd9Sstevel@tonic-gate 		case MIB2_IP: {
17007c478bd9Sstevel@tonic-gate 			mib2_ip_t	*ip = (mib2_ip_t *)item->valp;
17017c478bd9Sstevel@tonic-gate 
17027c478bd9Sstevel@tonic-gate 			ipAddrEntrySize = ip->ipAddrEntrySize;
17037c478bd9Sstevel@tonic-gate 			ipRouteEntrySize = ip->ipRouteEntrySize;
17047c478bd9Sstevel@tonic-gate 			ipNetToMediaEntrySize = ip->ipNetToMediaEntrySize;
17057c478bd9Sstevel@tonic-gate 			ipMemberEntrySize = ip->ipMemberEntrySize;
17067c478bd9Sstevel@tonic-gate 			ipGroupSourceEntrySize = ip->ipGroupSourceEntrySize;
170745916cd2Sjpk 			ipRouteAttributeSize = ip->ipRouteAttributeSize;
170845916cd2Sjpk 			transportMLPSize = ip->transportMLPSize;
17097c478bd9Sstevel@tonic-gate 			assert(IS_P2ALIGNED(ipAddrEntrySize,
17107c478bd9Sstevel@tonic-gate 			    sizeof (mib2_ipAddrEntry_t *)) &&
17117c478bd9Sstevel@tonic-gate 			    IS_P2ALIGNED(ipRouteEntrySize,
17127c478bd9Sstevel@tonic-gate 				sizeof (mib2_ipRouteEntry_t *)) &&
17137c478bd9Sstevel@tonic-gate 			    IS_P2ALIGNED(ipNetToMediaEntrySize,
17147c478bd9Sstevel@tonic-gate 				sizeof (mib2_ipNetToMediaEntry_t *)) &&
17157c478bd9Sstevel@tonic-gate 			    IS_P2ALIGNED(ipMemberEntrySize,
17167c478bd9Sstevel@tonic-gate 				sizeof (ip_member_t *)) &&
17177c478bd9Sstevel@tonic-gate 			    IS_P2ALIGNED(ipGroupSourceEntrySize,
171845916cd2Sjpk 				sizeof (ip_grpsrc_t *)) &&
171945916cd2Sjpk 			    IS_P2ALIGNED(ipRouteAttributeSize,
172045916cd2Sjpk 				sizeof (mib2_ipAttributeEntry_t *)) &&
172145916cd2Sjpk 			    IS_P2ALIGNED(transportMLPSize,
172245916cd2Sjpk 				sizeof (mib2_transportMLPEntry_t *)));
17237c478bd9Sstevel@tonic-gate 			break;
17247c478bd9Sstevel@tonic-gate 		}
17257c478bd9Sstevel@tonic-gate 		case EXPER_DVMRP: {
17267c478bd9Sstevel@tonic-gate 			struct mrtstat	*mrts = (struct mrtstat *)item->valp;
17277c478bd9Sstevel@tonic-gate 
17287c478bd9Sstevel@tonic-gate 			vifctlSize = mrts->mrts_vifctlSize;
17297c478bd9Sstevel@tonic-gate 			mfcctlSize = mrts->mrts_mfcctlSize;
17307c478bd9Sstevel@tonic-gate 			assert(IS_P2ALIGNED(vifctlSize,
17317c478bd9Sstevel@tonic-gate 			    sizeof (struct vifclt *)) &&
17327c478bd9Sstevel@tonic-gate 			    IS_P2ALIGNED(mfcctlSize, sizeof (struct mfcctl *)));
17337c478bd9Sstevel@tonic-gate 			break;
17347c478bd9Sstevel@tonic-gate 		}
17357c478bd9Sstevel@tonic-gate 		case MIB2_IP6: {
17367c478bd9Sstevel@tonic-gate 			mib2_ipv6IfStatsEntry_t *ip6;
17377c478bd9Sstevel@tonic-gate 			/* Just use the first entry */
17387c478bd9Sstevel@tonic-gate 
17397c478bd9Sstevel@tonic-gate 			ip6 = (mib2_ipv6IfStatsEntry_t *)item->valp;
17407c478bd9Sstevel@tonic-gate 			ipv6IfStatsEntrySize = ip6->ipv6IfStatsEntrySize;
17417c478bd9Sstevel@tonic-gate 			ipv6AddrEntrySize = ip6->ipv6AddrEntrySize;
17427c478bd9Sstevel@tonic-gate 			ipv6RouteEntrySize = ip6->ipv6RouteEntrySize;
17437c478bd9Sstevel@tonic-gate 			ipv6NetToMediaEntrySize = ip6->ipv6NetToMediaEntrySize;
17447c478bd9Sstevel@tonic-gate 			ipv6MemberEntrySize = ip6->ipv6MemberEntrySize;
17457c478bd9Sstevel@tonic-gate 			ipv6GroupSourceEntrySize =
17467c478bd9Sstevel@tonic-gate 			    ip6->ipv6GroupSourceEntrySize;
17477c478bd9Sstevel@tonic-gate 			assert(IS_P2ALIGNED(ipv6IfStatsEntrySize,
17487c478bd9Sstevel@tonic-gate 			    sizeof (mib2_ipv6IfStatsEntry_t *)) &&
17497c478bd9Sstevel@tonic-gate 			    IS_P2ALIGNED(ipv6AddrEntrySize,
17507c478bd9Sstevel@tonic-gate 				sizeof (mib2_ipv6AddrEntry_t *)) &&
17517c478bd9Sstevel@tonic-gate 			    IS_P2ALIGNED(ipv6RouteEntrySize,
17527c478bd9Sstevel@tonic-gate 				sizeof (mib2_ipv6RouteEntry_t *)) &&
17537c478bd9Sstevel@tonic-gate 			    IS_P2ALIGNED(ipv6NetToMediaEntrySize,
17547c478bd9Sstevel@tonic-gate 				sizeof (mib2_ipv6NetToMediaEntry_t *)) &&
17557c478bd9Sstevel@tonic-gate 			    IS_P2ALIGNED(ipv6MemberEntrySize,
17567c478bd9Sstevel@tonic-gate 				sizeof (ipv6_member_t *)) &&
17577c478bd9Sstevel@tonic-gate 			    IS_P2ALIGNED(ipv6GroupSourceEntrySize,
17587c478bd9Sstevel@tonic-gate 				sizeof (ipv6_grpsrc_t *)));
17597c478bd9Sstevel@tonic-gate 			break;
17607c478bd9Sstevel@tonic-gate 		}
17617c478bd9Sstevel@tonic-gate 		case MIB2_ICMP6: {
17627c478bd9Sstevel@tonic-gate 			mib2_ipv6IfIcmpEntry_t *icmp6;
17637c478bd9Sstevel@tonic-gate 			/* Just use the first entry */
17647c478bd9Sstevel@tonic-gate 
17657c478bd9Sstevel@tonic-gate 			icmp6 = (mib2_ipv6IfIcmpEntry_t *)item->valp;
17667c478bd9Sstevel@tonic-gate 			ipv6IfIcmpEntrySize = icmp6->ipv6IfIcmpEntrySize;
17677c478bd9Sstevel@tonic-gate 			assert(IS_P2ALIGNED(ipv6IfIcmpEntrySize,
17687c478bd9Sstevel@tonic-gate 			    sizeof (mib2_ipv6IfIcmpEntry_t *)));
17697c478bd9Sstevel@tonic-gate 			break;
17707c478bd9Sstevel@tonic-gate 		}
17717c478bd9Sstevel@tonic-gate 		case MIB2_TCP: {
17727c478bd9Sstevel@tonic-gate 			mib2_tcp_t	*tcp = (mib2_tcp_t *)item->valp;
17737c478bd9Sstevel@tonic-gate 
17747c478bd9Sstevel@tonic-gate 			tcpConnEntrySize = tcp->tcpConnTableSize;
17757c478bd9Sstevel@tonic-gate 			tcp6ConnEntrySize = tcp->tcp6ConnTableSize;
17767c478bd9Sstevel@tonic-gate 			assert(IS_P2ALIGNED(tcpConnEntrySize,
17777c478bd9Sstevel@tonic-gate 			    sizeof (mib2_tcpConnEntry_t *)) &&
17787c478bd9Sstevel@tonic-gate 			    IS_P2ALIGNED(tcp6ConnEntrySize,
17797c478bd9Sstevel@tonic-gate 				sizeof (mib2_tcp6ConnEntry_t *)));
17807c478bd9Sstevel@tonic-gate 			break;
17817c478bd9Sstevel@tonic-gate 		}
17827c478bd9Sstevel@tonic-gate 		case MIB2_UDP: {
17837c478bd9Sstevel@tonic-gate 			mib2_udp_t	*udp = (mib2_udp_t *)item->valp;
17847c478bd9Sstevel@tonic-gate 
17857c478bd9Sstevel@tonic-gate 			udpEntrySize = udp->udpEntrySize;
17867c478bd9Sstevel@tonic-gate 			udp6EntrySize = udp->udp6EntrySize;
17877c478bd9Sstevel@tonic-gate 			assert(IS_P2ALIGNED(udpEntrySize,
17887c478bd9Sstevel@tonic-gate 			    sizeof (mib2_udpEntry_t *)) &&
17897c478bd9Sstevel@tonic-gate 			    IS_P2ALIGNED(udp6EntrySize,
17907c478bd9Sstevel@tonic-gate 				sizeof (mib2_udp6Entry_t *)));
17917c478bd9Sstevel@tonic-gate 			break;
17927c478bd9Sstevel@tonic-gate 		}
17937c478bd9Sstevel@tonic-gate 		case MIB2_SCTP: {
17947c478bd9Sstevel@tonic-gate 			mib2_sctp_t	*sctp = (mib2_sctp_t *)item->valp;
17957c478bd9Sstevel@tonic-gate 
17967c478bd9Sstevel@tonic-gate 			sctpEntrySize = sctp->sctpEntrySize;
17977c478bd9Sstevel@tonic-gate 			sctpLocalEntrySize = sctp->sctpLocalEntrySize;
17987c478bd9Sstevel@tonic-gate 			sctpRemoteEntrySize = sctp->sctpRemoteEntrySize;
17997c478bd9Sstevel@tonic-gate 			break;
18007c478bd9Sstevel@tonic-gate 		}
18017c478bd9Sstevel@tonic-gate 		}
18027c478bd9Sstevel@tonic-gate 	} /* 'for' loop 1 ends */
18037c478bd9Sstevel@tonic-gate 
18047c478bd9Sstevel@tonic-gate 	if (Dflag) {
18057c478bd9Sstevel@tonic-gate 		(void) puts("mib_get_constants:");
18067c478bd9Sstevel@tonic-gate 		(void) printf("\tipv6IfStatsEntrySize %d\n",
18077c478bd9Sstevel@tonic-gate 		    ipv6IfStatsEntrySize);
18087c478bd9Sstevel@tonic-gate 		(void) printf("\tipAddrEntrySize %d\n", ipAddrEntrySize);
18097c478bd9Sstevel@tonic-gate 		(void) printf("\tipRouteEntrySize %d\n", ipRouteEntrySize);
18107c478bd9Sstevel@tonic-gate 		(void) printf("\tipNetToMediaEntrySize %d\n",
18117c478bd9Sstevel@tonic-gate 		    ipNetToMediaEntrySize);
18127c478bd9Sstevel@tonic-gate 		(void) printf("\tipMemberEntrySize %d\n", ipMemberEntrySize);
181345916cd2Sjpk 		(void) printf("\tipRouteAttributeSize %d\n",
181445916cd2Sjpk 		    ipRouteAttributeSize);
18157c478bd9Sstevel@tonic-gate 		(void) printf("\tvifctlSize %d\n", vifctlSize);
18167c478bd9Sstevel@tonic-gate 		(void) printf("\tmfcctlSize %d\n", mfcctlSize);
18177c478bd9Sstevel@tonic-gate 
18187c478bd9Sstevel@tonic-gate 		(void) printf("\tipv6AddrEntrySize %d\n", ipv6AddrEntrySize);
18197c478bd9Sstevel@tonic-gate 		(void) printf("\tipv6RouteEntrySize %d\n", ipv6RouteEntrySize);
18207c478bd9Sstevel@tonic-gate 		(void) printf("\tipv6NetToMediaEntrySize %d\n",
18217c478bd9Sstevel@tonic-gate 		    ipv6NetToMediaEntrySize);
18227c478bd9Sstevel@tonic-gate 		(void) printf("\tipv6MemberEntrySize %d\n",
18237c478bd9Sstevel@tonic-gate 		    ipv6MemberEntrySize);
18247c478bd9Sstevel@tonic-gate 		(void) printf("\tipv6IfIcmpEntrySize %d\n",
18257c478bd9Sstevel@tonic-gate 		    ipv6IfIcmpEntrySize);
182645916cd2Sjpk 		(void) printf("\ttransportMLPSize %d\n", transportMLPSize);
18277c478bd9Sstevel@tonic-gate 		(void) printf("\ttcpConnEntrySize %d\n", tcpConnEntrySize);
18287c478bd9Sstevel@tonic-gate 		(void) printf("\ttcp6ConnEntrySize %d\n", tcp6ConnEntrySize);
18297c478bd9Sstevel@tonic-gate 		(void) printf("\tudpEntrySize %d\n", udpEntrySize);
18307c478bd9Sstevel@tonic-gate 		(void) printf("\tudp6EntrySize %d\n", udp6EntrySize);
18317c478bd9Sstevel@tonic-gate 		(void) printf("\tsctpEntrySize %d\n", sctpEntrySize);
18327c478bd9Sstevel@tonic-gate 		(void) printf("\tsctpLocalEntrySize %d\n", sctpLocalEntrySize);
18337c478bd9Sstevel@tonic-gate 		(void) printf("\tsctpRemoteEntrySize %d\n",
18347c478bd9Sstevel@tonic-gate 		    sctpRemoteEntrySize);
18357c478bd9Sstevel@tonic-gate 	}
18367c478bd9Sstevel@tonic-gate }
18377c478bd9Sstevel@tonic-gate 
18387c478bd9Sstevel@tonic-gate 
18397c478bd9Sstevel@tonic-gate /* ----------------------------- STAT_REPORT ------------------------------- */
18407c478bd9Sstevel@tonic-gate 
18417c478bd9Sstevel@tonic-gate static void
18427c478bd9Sstevel@tonic-gate stat_report(mib_item_t *item)
18437c478bd9Sstevel@tonic-gate {
18447c478bd9Sstevel@tonic-gate 	int	jtemp = 0;
18457c478bd9Sstevel@tonic-gate 	char	ifname[LIFNAMSIZ + 1];
18467c478bd9Sstevel@tonic-gate 	char	*ifnamep;
18477c478bd9Sstevel@tonic-gate 
18487c478bd9Sstevel@tonic-gate 	/* 'for' loop 1: */
18497c478bd9Sstevel@tonic-gate 	for (; item; item = item->next_item) {
18507c478bd9Sstevel@tonic-gate 		if (Dflag) {
18517c478bd9Sstevel@tonic-gate 			(void) printf("\n--- Entry %d ---\n", ++jtemp);
18527c478bd9Sstevel@tonic-gate 			(void) printf("Group = %d, mib_id = %d, "
18537c478bd9Sstevel@tonic-gate 			    "length = %d, valp = 0x%p\n",
18547c478bd9Sstevel@tonic-gate 			    item->group, item->mib_id,
18557c478bd9Sstevel@tonic-gate 			    item->length, item->valp);
18567c478bd9Sstevel@tonic-gate 		}
18577c478bd9Sstevel@tonic-gate 		if (item->mib_id != 0)
18587c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
18597c478bd9Sstevel@tonic-gate 
18607c478bd9Sstevel@tonic-gate 		switch (item->group) {
18617c478bd9Sstevel@tonic-gate 		case MIB2_IP: {
18627c478bd9Sstevel@tonic-gate 			mib2_ip_t	*ip = (mib2_ip_t *)item->valp;
18637c478bd9Sstevel@tonic-gate 
18647c478bd9Sstevel@tonic-gate 			if (protocol_selected(IPPROTO_IP) &&
18657c478bd9Sstevel@tonic-gate 			    family_selected(AF_INET)) {
18667c478bd9Sstevel@tonic-gate 				(void) fputs(v4compat ? "\nIP" : "\nIPv4",
18677c478bd9Sstevel@tonic-gate 				    stdout);
18687c478bd9Sstevel@tonic-gate 				print_ip_stats(ip);
18697c478bd9Sstevel@tonic-gate 			}
18707c478bd9Sstevel@tonic-gate 			break;
18717c478bd9Sstevel@tonic-gate 		}
18727c478bd9Sstevel@tonic-gate 		case MIB2_ICMP: {
18737c478bd9Sstevel@tonic-gate 			mib2_icmp_t	*icmp =
18747c478bd9Sstevel@tonic-gate 			    (mib2_icmp_t *)item->valp;
18757c478bd9Sstevel@tonic-gate 
18767c478bd9Sstevel@tonic-gate 			if (protocol_selected(IPPROTO_ICMP) &&
18777c478bd9Sstevel@tonic-gate 			    family_selected(AF_INET)) {
18787c478bd9Sstevel@tonic-gate 				(void) fputs(v4compat ? "\nICMP" : "\nICMPv4",
18797c478bd9Sstevel@tonic-gate 				    stdout);
18807c478bd9Sstevel@tonic-gate 				print_icmp_stats(icmp);
18817c478bd9Sstevel@tonic-gate 			}
18827c478bd9Sstevel@tonic-gate 			break;
18837c478bd9Sstevel@tonic-gate 		}
18847c478bd9Sstevel@tonic-gate 		case MIB2_IP6: {
18857c478bd9Sstevel@tonic-gate 			mib2_ipv6IfStatsEntry_t *ip6;
18867c478bd9Sstevel@tonic-gate 			mib2_ipv6IfStatsEntry_t sum6;
18877c478bd9Sstevel@tonic-gate 
18887c478bd9Sstevel@tonic-gate 			if (!(protocol_selected(IPPROTO_IPV6)) ||
18897c478bd9Sstevel@tonic-gate 			    !(family_selected(AF_INET6)))
18907c478bd9Sstevel@tonic-gate 				break;
18917c478bd9Sstevel@tonic-gate 			bzero(&sum6, sizeof (sum6));
18927c478bd9Sstevel@tonic-gate 			/* 'for' loop 2a: */
18937c478bd9Sstevel@tonic-gate 			for (ip6 = (mib2_ipv6IfStatsEntry_t *)item->valp;
18947c478bd9Sstevel@tonic-gate 			    (char *)ip6 < (char *)item->valp
18957c478bd9Sstevel@tonic-gate 			    + item->length;
18967c478bd9Sstevel@tonic-gate 			    /* LINTED: (note 1) */
18977c478bd9Sstevel@tonic-gate 			    ip6 = (mib2_ipv6IfStatsEntry_t *)((char *)ip6 +
18987c478bd9Sstevel@tonic-gate 			    ipv6IfStatsEntrySize)) {
18997c478bd9Sstevel@tonic-gate 
19007c478bd9Sstevel@tonic-gate 				if (ip6->ipv6IfIndex == 0) {
19017c478bd9Sstevel@tonic-gate 					/*
19027c478bd9Sstevel@tonic-gate 					 * The "unknown interface" ip6
19037c478bd9Sstevel@tonic-gate 					 * mib. Just add to the sum.
19047c478bd9Sstevel@tonic-gate 					 */
19057c478bd9Sstevel@tonic-gate 					sum_ip6_stats(ip6, &sum6);
19067c478bd9Sstevel@tonic-gate 					continue; /* 'for' loop 2a */
19077c478bd9Sstevel@tonic-gate 				}
19087c478bd9Sstevel@tonic-gate 				ifnamep = if_indextoname(
19097c478bd9Sstevel@tonic-gate 				    ip6->ipv6IfIndex,
19107c478bd9Sstevel@tonic-gate 				    ifname);
19117c478bd9Sstevel@tonic-gate 				if (ifnamep == NULL) {
19127c478bd9Sstevel@tonic-gate 					(void) printf(
19137c478bd9Sstevel@tonic-gate 					    "Invalid ifindex %d\n",
19147c478bd9Sstevel@tonic-gate 					    ip6->ipv6IfIndex);
19157c478bd9Sstevel@tonic-gate 					continue; /* 'for' loop 2a */
19167c478bd9Sstevel@tonic-gate 				}
19177c478bd9Sstevel@tonic-gate 
19187c478bd9Sstevel@tonic-gate 				if (Aflag) {
19197c478bd9Sstevel@tonic-gate 					(void) printf("\nIPv6 for %s\n",
19207c478bd9Sstevel@tonic-gate 					    ifnamep);
19217c478bd9Sstevel@tonic-gate 					print_ip6_stats(ip6);
19227c478bd9Sstevel@tonic-gate 				}
19237c478bd9Sstevel@tonic-gate 				sum_ip6_stats(ip6, &sum6);
19247c478bd9Sstevel@tonic-gate 			} /* 'for' loop 2a ends */
19257c478bd9Sstevel@tonic-gate 			(void) fputs("\nIPv6", stdout);
19267c478bd9Sstevel@tonic-gate 			print_ip6_stats(&sum6);
19277c478bd9Sstevel@tonic-gate 			break;
19287c478bd9Sstevel@tonic-gate 		}
19297c478bd9Sstevel@tonic-gate 		case MIB2_ICMP6: {
19307c478bd9Sstevel@tonic-gate 			mib2_ipv6IfIcmpEntry_t *icmp6;
19317c478bd9Sstevel@tonic-gate 			mib2_ipv6IfIcmpEntry_t sum6;
19327c478bd9Sstevel@tonic-gate 
19337c478bd9Sstevel@tonic-gate 			if (!(protocol_selected(IPPROTO_ICMPV6)) ||
19347c478bd9Sstevel@tonic-gate 			    !(family_selected(AF_INET6)))
19357c478bd9Sstevel@tonic-gate 				break;
19367c478bd9Sstevel@tonic-gate 			bzero(&sum6, sizeof (sum6));
19377c478bd9Sstevel@tonic-gate 			/* 'for' loop 2b: */
19387c478bd9Sstevel@tonic-gate 			for (icmp6 =
19397c478bd9Sstevel@tonic-gate 			    (mib2_ipv6IfIcmpEntry_t *)item->valp;
19407c478bd9Sstevel@tonic-gate 			    (char *)icmp6 < (char *)item->valp
19417c478bd9Sstevel@tonic-gate 				+ item->length;
19427c478bd9Sstevel@tonic-gate 			    icmp6 =
19437c478bd9Sstevel@tonic-gate 				/* LINTED: (note 1) */
19447c478bd9Sstevel@tonic-gate 				(mib2_ipv6IfIcmpEntry_t *)((char *)icmp6
19457c478bd9Sstevel@tonic-gate 			    + ipv6IfIcmpEntrySize)) {
19467c478bd9Sstevel@tonic-gate 
19477c478bd9Sstevel@tonic-gate 				if (icmp6->ipv6IfIcmpIfIndex == 0) {
19487c478bd9Sstevel@tonic-gate 					/*
19497c478bd9Sstevel@tonic-gate 					 * The "unknown interface" icmp6
19507c478bd9Sstevel@tonic-gate 					 * mib. Just add to the sum.
19517c478bd9Sstevel@tonic-gate 					 */
19527c478bd9Sstevel@tonic-gate 					sum_icmp6_stats(icmp6, &sum6);
19537c478bd9Sstevel@tonic-gate 					continue; /* 'for' loop 2b: */
19547c478bd9Sstevel@tonic-gate 				}
19557c478bd9Sstevel@tonic-gate 				ifnamep = if_indextoname(
19567c478bd9Sstevel@tonic-gate 				    icmp6->ipv6IfIcmpIfIndex, ifname);
19577c478bd9Sstevel@tonic-gate 				if (ifnamep == NULL) {
19587c478bd9Sstevel@tonic-gate 					(void) printf(
19597c478bd9Sstevel@tonic-gate 					    "Invalid ifindex %d\n",
19607c478bd9Sstevel@tonic-gate 					    icmp6->ipv6IfIcmpIfIndex);
19617c478bd9Sstevel@tonic-gate 					continue; /* 'for' loop 2b: */
19627c478bd9Sstevel@tonic-gate 				}
19637c478bd9Sstevel@tonic-gate 
19647c478bd9Sstevel@tonic-gate 				if (Aflag) {
19657c478bd9Sstevel@tonic-gate 					(void) printf(
19667c478bd9Sstevel@tonic-gate 					    "\nICMPv6 for %s\n",
19677c478bd9Sstevel@tonic-gate 					    ifnamep);
19687c478bd9Sstevel@tonic-gate 					print_icmp6_stats(icmp6);
19697c478bd9Sstevel@tonic-gate 				}
19707c478bd9Sstevel@tonic-gate 				sum_icmp6_stats(icmp6, &sum6);
19717c478bd9Sstevel@tonic-gate 			} /* 'for' loop 2b ends */
19727c478bd9Sstevel@tonic-gate 			(void) fputs("\nICMPv6", stdout);
19737c478bd9Sstevel@tonic-gate 			print_icmp6_stats(&sum6);
19747c478bd9Sstevel@tonic-gate 			break;
19757c478bd9Sstevel@tonic-gate 		}
19767c478bd9Sstevel@tonic-gate 		case MIB2_TCP: {
19777c478bd9Sstevel@tonic-gate 			mib2_tcp_t	*tcp = (mib2_tcp_t *)item->valp;
19787c478bd9Sstevel@tonic-gate 
19797c478bd9Sstevel@tonic-gate 			if (protocol_selected(IPPROTO_TCP) &&
19807c478bd9Sstevel@tonic-gate 			    (family_selected(AF_INET) ||
19817c478bd9Sstevel@tonic-gate 			    family_selected(AF_INET6))) {
19827c478bd9Sstevel@tonic-gate 				(void) fputs("\nTCP", stdout);
19837c478bd9Sstevel@tonic-gate 				print_tcp_stats(tcp);
19847c478bd9Sstevel@tonic-gate 			}
19857c478bd9Sstevel@tonic-gate 			break;
19867c478bd9Sstevel@tonic-gate 		}
19877c478bd9Sstevel@tonic-gate 		case MIB2_UDP: {
19887c478bd9Sstevel@tonic-gate 			mib2_udp_t	*udp = (mib2_udp_t *)item->valp;
19897c478bd9Sstevel@tonic-gate 
19907c478bd9Sstevel@tonic-gate 			if (protocol_selected(IPPROTO_UDP) &&
19917c478bd9Sstevel@tonic-gate 			    (family_selected(AF_INET) ||
19927c478bd9Sstevel@tonic-gate 			    family_selected(AF_INET6))) {
19937c478bd9Sstevel@tonic-gate 				(void) fputs("\nUDP", stdout);
19947c478bd9Sstevel@tonic-gate 				print_udp_stats(udp);
19957c478bd9Sstevel@tonic-gate 			}
19967c478bd9Sstevel@tonic-gate 			break;
19977c478bd9Sstevel@tonic-gate 		}
19987c478bd9Sstevel@tonic-gate 		case MIB2_SCTP: {
19997c478bd9Sstevel@tonic-gate 			mib2_sctp_t	*sctp = (mib2_sctp_t *)item->valp;
20007c478bd9Sstevel@tonic-gate 
20017c478bd9Sstevel@tonic-gate 			if (protocol_selected(IPPROTO_SCTP) &&
20027c478bd9Sstevel@tonic-gate 			    (family_selected(AF_INET) ||
20037c478bd9Sstevel@tonic-gate 			    family_selected(AF_INET6))) {
20047c478bd9Sstevel@tonic-gate 				(void) fputs("\nSCTP", stdout);
20057c478bd9Sstevel@tonic-gate 				print_sctp_stats(sctp);
20067c478bd9Sstevel@tonic-gate 			}
20077c478bd9Sstevel@tonic-gate 			break;
20087c478bd9Sstevel@tonic-gate 		}
20097c478bd9Sstevel@tonic-gate 		case EXPER_RAWIP: {
20107c478bd9Sstevel@tonic-gate 			mib2_rawip_t	*rawip =
20117c478bd9Sstevel@tonic-gate 			    (mib2_rawip_t *)item->valp;
20127c478bd9Sstevel@tonic-gate 
20137c478bd9Sstevel@tonic-gate 			if (protocol_selected(IPPROTO_RAW) &&
20147c478bd9Sstevel@tonic-gate 			    (family_selected(AF_INET) ||
20157c478bd9Sstevel@tonic-gate 			    family_selected(AF_INET6))) {
20167c478bd9Sstevel@tonic-gate 				(void) fputs("\nRAWIP", stdout);
20177c478bd9Sstevel@tonic-gate 				print_rawip_stats(rawip);
20187c478bd9Sstevel@tonic-gate 			}
20197c478bd9Sstevel@tonic-gate 			break;
20207c478bd9Sstevel@tonic-gate 		}
20217c478bd9Sstevel@tonic-gate 		case EXPER_IGMP: {
20227c478bd9Sstevel@tonic-gate 			struct igmpstat	*igps =
20237c478bd9Sstevel@tonic-gate 			    (struct igmpstat *)item->valp;
20247c478bd9Sstevel@tonic-gate 
20257c478bd9Sstevel@tonic-gate 			if (protocol_selected(IPPROTO_IGMP) &&
20267c478bd9Sstevel@tonic-gate 			    (family_selected(AF_INET))) {
20277c478bd9Sstevel@tonic-gate 				(void) fputs("\nIGMP:\n", stdout);
20287c478bd9Sstevel@tonic-gate 				print_igmp_stats(igps);
20297c478bd9Sstevel@tonic-gate 			}
20307c478bd9Sstevel@tonic-gate 			break;
20317c478bd9Sstevel@tonic-gate 		}
20327c478bd9Sstevel@tonic-gate 		}
20337c478bd9Sstevel@tonic-gate 	} /* 'for' loop 1 ends */
20347c478bd9Sstevel@tonic-gate 	(void) putchar('\n');
20357c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
20367c478bd9Sstevel@tonic-gate }
20377c478bd9Sstevel@tonic-gate 
20387c478bd9Sstevel@tonic-gate static void
20397c478bd9Sstevel@tonic-gate print_ip_stats(mib2_ip_t *ip)
20407c478bd9Sstevel@tonic-gate {
20417c478bd9Sstevel@tonic-gate 	prval_init();
20427c478bd9Sstevel@tonic-gate 	pr_int_val("ipForwarding",	ip->ipForwarding);
20437c478bd9Sstevel@tonic-gate 	pr_int_val("ipDefaultTTL",	ip->ipDefaultTTL);
20447c478bd9Sstevel@tonic-gate 	prval("ipInReceives",		ip->ipInReceives);
20457c478bd9Sstevel@tonic-gate 	prval("ipInHdrErrors",		ip->ipInHdrErrors);
20467c478bd9Sstevel@tonic-gate 	prval("ipInAddrErrors",		ip->ipInAddrErrors);
20477c478bd9Sstevel@tonic-gate 	prval("ipInCksumErrs",		ip->ipInCksumErrs);
20487c478bd9Sstevel@tonic-gate 	prval("ipForwDatagrams",	ip->ipForwDatagrams);
20497c478bd9Sstevel@tonic-gate 	prval("ipForwProhibits",	ip->ipForwProhibits);
20507c478bd9Sstevel@tonic-gate 	prval("ipInUnknownProtos",	ip->ipInUnknownProtos);
20517c478bd9Sstevel@tonic-gate 	prval("ipInDiscards",		ip->ipInDiscards);
20527c478bd9Sstevel@tonic-gate 	prval("ipInDelivers",		ip->ipInDelivers);
20537c478bd9Sstevel@tonic-gate 	prval("ipOutRequests",		ip->ipOutRequests);
20547c478bd9Sstevel@tonic-gate 	prval("ipOutDiscards",		ip->ipOutDiscards);
20557c478bd9Sstevel@tonic-gate 	prval("ipOutNoRoutes",		ip->ipOutNoRoutes);
20567c478bd9Sstevel@tonic-gate 	pr_int_val("ipReasmTimeout",	ip->ipReasmTimeout);
20577c478bd9Sstevel@tonic-gate 	prval("ipReasmReqds",		ip->ipReasmReqds);
20587c478bd9Sstevel@tonic-gate 	prval("ipReasmOKs",		ip->ipReasmOKs);
20597c478bd9Sstevel@tonic-gate 	prval("ipReasmFails",		ip->ipReasmFails);
20607c478bd9Sstevel@tonic-gate 	prval("ipReasmDuplicates",	ip->ipReasmDuplicates);
20617c478bd9Sstevel@tonic-gate 	prval("ipReasmPartDups",	ip->ipReasmPartDups);
20627c478bd9Sstevel@tonic-gate 	prval("ipFragOKs",		ip->ipFragOKs);
20637c478bd9Sstevel@tonic-gate 	prval("ipFragFails",		ip->ipFragFails);
20647c478bd9Sstevel@tonic-gate 	prval("ipFragCreates",		ip->ipFragCreates);
20657c478bd9Sstevel@tonic-gate 	prval("ipRoutingDiscards",	ip->ipRoutingDiscards);
20667c478bd9Sstevel@tonic-gate 
20677c478bd9Sstevel@tonic-gate 	prval("tcpInErrs",		ip->tcpInErrs);
20687c478bd9Sstevel@tonic-gate 	prval("udpNoPorts",		ip->udpNoPorts);
20697c478bd9Sstevel@tonic-gate 	prval("udpInCksumErrs",		ip->udpInCksumErrs);
20707c478bd9Sstevel@tonic-gate 	prval("udpInOverflows",		ip->udpInOverflows);
20717c478bd9Sstevel@tonic-gate 	prval("rawipInOverflows",	ip->rawipInOverflows);
20727c478bd9Sstevel@tonic-gate 	prval("ipsecInSucceeded",	ip->ipsecInSucceeded);
20737c478bd9Sstevel@tonic-gate 	prval("ipsecInFailed",		ip->ipsecInFailed);
20747c478bd9Sstevel@tonic-gate 	prval("ipInIPv6",		ip->ipInIPv6);
20757c478bd9Sstevel@tonic-gate 	prval("ipOutIPv6",		ip->ipOutIPv6);
20767c478bd9Sstevel@tonic-gate 	prval("ipOutSwitchIPv6",	ip->ipOutSwitchIPv6);
20777c478bd9Sstevel@tonic-gate 	prval_end();
20787c478bd9Sstevel@tonic-gate }
20797c478bd9Sstevel@tonic-gate 
20807c478bd9Sstevel@tonic-gate static void
20817c478bd9Sstevel@tonic-gate print_icmp_stats(mib2_icmp_t *icmp)
20827c478bd9Sstevel@tonic-gate {
20837c478bd9Sstevel@tonic-gate 	prval_init();
20847c478bd9Sstevel@tonic-gate 	prval("icmpInMsgs",		icmp->icmpInMsgs);
20857c478bd9Sstevel@tonic-gate 	prval("icmpInErrors",		icmp->icmpInErrors);
20867c478bd9Sstevel@tonic-gate 	prval("icmpInCksumErrs",	icmp->icmpInCksumErrs);
20877c478bd9Sstevel@tonic-gate 	prval("icmpInUnknowns",		icmp->icmpInUnknowns);
20887c478bd9Sstevel@tonic-gate 	prval("icmpInDestUnreachs",	icmp->icmpInDestUnreachs);
20897c478bd9Sstevel@tonic-gate 	prval("icmpInTimeExcds",	icmp->icmpInTimeExcds);
20907c478bd9Sstevel@tonic-gate 	prval("icmpInParmProbs",	icmp->icmpInParmProbs);
20917c478bd9Sstevel@tonic-gate 	prval("icmpInSrcQuenchs",	icmp->icmpInSrcQuenchs);
20927c478bd9Sstevel@tonic-gate 	prval("icmpInRedirects",	icmp->icmpInRedirects);
20937c478bd9Sstevel@tonic-gate 	prval("icmpInBadRedirects",	icmp->icmpInBadRedirects);
20947c478bd9Sstevel@tonic-gate 	prval("icmpInEchos",		icmp->icmpInEchos);
20957c478bd9Sstevel@tonic-gate 	prval("icmpInEchoReps",		icmp->icmpInEchoReps);
20967c478bd9Sstevel@tonic-gate 	prval("icmpInTimestamps",	icmp->icmpInTimestamps);
20977c478bd9Sstevel@tonic-gate 	prval("icmpInTimestampReps",	icmp->icmpInTimestampReps);
20987c478bd9Sstevel@tonic-gate 	prval("icmpInAddrMasks",	icmp->icmpInAddrMasks);
20997c478bd9Sstevel@tonic-gate 	prval("icmpInAddrMaskReps",	icmp->icmpInAddrMaskReps);
21007c478bd9Sstevel@tonic-gate 	prval("icmpInFragNeeded",	icmp->icmpInFragNeeded);
21017c478bd9Sstevel@tonic-gate 	prval("icmpOutMsgs",		icmp->icmpOutMsgs);
21027c478bd9Sstevel@tonic-gate 	prval("icmpOutDrops",		icmp->icmpOutDrops);
21037c478bd9Sstevel@tonic-gate 	prval("icmpOutErrors",		icmp->icmpOutErrors);
21047c478bd9Sstevel@tonic-gate 	prval("icmpOutDestUnreachs",	icmp->icmpOutDestUnreachs);
21057c478bd9Sstevel@tonic-gate 	prval("icmpOutTimeExcds",	icmp->icmpOutTimeExcds);
21067c478bd9Sstevel@tonic-gate 	prval("icmpOutParmProbs",	icmp->icmpOutParmProbs);
21077c478bd9Sstevel@tonic-gate 	prval("icmpOutSrcQuenchs",	icmp->icmpOutSrcQuenchs);
21087c478bd9Sstevel@tonic-gate 	prval("icmpOutRedirects",	icmp->icmpOutRedirects);
21097c478bd9Sstevel@tonic-gate 	prval("icmpOutEchos",		icmp->icmpOutEchos);
21107c478bd9Sstevel@tonic-gate 	prval("icmpOutEchoReps",	icmp->icmpOutEchoReps);
21117c478bd9Sstevel@tonic-gate 	prval("icmpOutTimestamps",	icmp->icmpOutTimestamps);
21127c478bd9Sstevel@tonic-gate 	prval("icmpOutTimestampReps",	icmp->icmpOutTimestampReps);
21137c478bd9Sstevel@tonic-gate 	prval("icmpOutAddrMasks",	icmp->icmpOutAddrMasks);
21147c478bd9Sstevel@tonic-gate 	prval("icmpOutAddrMaskReps",	icmp->icmpOutAddrMaskReps);
21157c478bd9Sstevel@tonic-gate 	prval("icmpOutFragNeeded",	icmp->icmpOutFragNeeded);
21167c478bd9Sstevel@tonic-gate 	prval("icmpInOverflows",	icmp->icmpInOverflows);
21177c478bd9Sstevel@tonic-gate 	prval_end();
21187c478bd9Sstevel@tonic-gate }
21197c478bd9Sstevel@tonic-gate 
21207c478bd9Sstevel@tonic-gate static void
21217c478bd9Sstevel@tonic-gate print_ip6_stats(mib2_ipv6IfStatsEntry_t *ip6)
21227c478bd9Sstevel@tonic-gate {
21237c478bd9Sstevel@tonic-gate 	prval_init();
21247c478bd9Sstevel@tonic-gate 	prval("ipv6Forwarding",		ip6->ipv6Forwarding);
21257c478bd9Sstevel@tonic-gate 	prval("ipv6DefaultHopLimit",	ip6->ipv6DefaultHopLimit);
21267c478bd9Sstevel@tonic-gate 
21277c478bd9Sstevel@tonic-gate 	prval("ipv6InReceives",		ip6->ipv6InReceives);
21287c478bd9Sstevel@tonic-gate 	prval("ipv6InHdrErrors",	ip6->ipv6InHdrErrors);
21297c478bd9Sstevel@tonic-gate 	prval("ipv6InTooBigErrors",	ip6->ipv6InTooBigErrors);
21307c478bd9Sstevel@tonic-gate 	prval("ipv6InNoRoutes",		ip6->ipv6InNoRoutes);
21317c478bd9Sstevel@tonic-gate 	prval("ipv6InAddrErrors",	ip6->ipv6InAddrErrors);
21327c478bd9Sstevel@tonic-gate 	prval("ipv6InUnknownProtos",	ip6->ipv6InUnknownProtos);
21337c478bd9Sstevel@tonic-gate 	prval("ipv6InTruncatedPkts",	ip6->ipv6InTruncatedPkts);
21347c478bd9Sstevel@tonic-gate 	prval("ipv6InDiscards",		ip6->ipv6InDiscards);
21357c478bd9Sstevel@tonic-gate 	prval("ipv6InDelivers",		ip6->ipv6InDelivers);
21367c478bd9Sstevel@tonic-gate 	prval("ipv6OutForwDatagrams",	ip6->ipv6OutForwDatagrams);
21377c478bd9Sstevel@tonic-gate 	prval("ipv6OutRequests",	ip6->ipv6OutRequests);
21387c478bd9Sstevel@tonic-gate 	prval("ipv6OutDiscards",	ip6->ipv6OutDiscards);
21397c478bd9Sstevel@tonic-gate 	prval("ipv6OutNoRoutes",	ip6->ipv6OutNoRoutes);
21407c478bd9Sstevel@tonic-gate 	prval("ipv6OutFragOKs",		ip6->ipv6OutFragOKs);
21417c478bd9Sstevel@tonic-gate 	prval("ipv6OutFragFails",	ip6->ipv6OutFragFails);
21427c478bd9Sstevel@tonic-gate 	prval("ipv6OutFragCreates",	ip6->ipv6OutFragCreates);
21437c478bd9Sstevel@tonic-gate 	prval("ipv6ReasmReqds",		ip6->ipv6ReasmReqds);
21447c478bd9Sstevel@tonic-gate 	prval("ipv6ReasmOKs",		ip6->ipv6ReasmOKs);
21457c478bd9Sstevel@tonic-gate 	prval("ipv6ReasmFails",		ip6->ipv6ReasmFails);
21467c478bd9Sstevel@tonic-gate 	prval("ipv6InMcastPkts",	ip6->ipv6InMcastPkts);
21477c478bd9Sstevel@tonic-gate 	prval("ipv6OutMcastPkts",	ip6->ipv6OutMcastPkts);
21487c478bd9Sstevel@tonic-gate 	prval("ipv6ReasmDuplicates",	ip6->ipv6ReasmDuplicates);
21497c478bd9Sstevel@tonic-gate 	prval("ipv6ReasmPartDups",	ip6->ipv6ReasmPartDups);
21507c478bd9Sstevel@tonic-gate 	prval("ipv6ForwProhibits",	ip6->ipv6ForwProhibits);
21517c478bd9Sstevel@tonic-gate 	prval("udpInCksumErrs",		ip6->udpInCksumErrs);
21527c478bd9Sstevel@tonic-gate 	prval("udpInOverflows",		ip6->udpInOverflows);
21537c478bd9Sstevel@tonic-gate 	prval("rawipInOverflows",	ip6->rawipInOverflows);
21547c478bd9Sstevel@tonic-gate 	prval("ipv6InIPv4",		ip6->ipv6InIPv4);
21557c478bd9Sstevel@tonic-gate 	prval("ipv6OutIPv4",		ip6->ipv6OutIPv4);
21567c478bd9Sstevel@tonic-gate 	prval("ipv6OutSwitchIPv4",	ip6->ipv6OutSwitchIPv4);
21577c478bd9Sstevel@tonic-gate 	prval_end();
21587c478bd9Sstevel@tonic-gate }
21597c478bd9Sstevel@tonic-gate 
21607c478bd9Sstevel@tonic-gate static void
21617c478bd9Sstevel@tonic-gate print_icmp6_stats(mib2_ipv6IfIcmpEntry_t *icmp6)
21627c478bd9Sstevel@tonic-gate {
21637c478bd9Sstevel@tonic-gate 	prval_init();
21647c478bd9Sstevel@tonic-gate 	prval("icmp6InMsgs",		icmp6->ipv6IfIcmpInMsgs);
21657c478bd9Sstevel@tonic-gate 	prval("icmp6InErrors",		icmp6->ipv6IfIcmpInErrors);
21667c478bd9Sstevel@tonic-gate 	prval("icmp6InDestUnreachs",	icmp6->ipv6IfIcmpInDestUnreachs);
21677c478bd9Sstevel@tonic-gate 	prval("icmp6InAdminProhibs",	icmp6->ipv6IfIcmpInAdminProhibs);
21687c478bd9Sstevel@tonic-gate 	prval("icmp6InTimeExcds",	icmp6->ipv6IfIcmpInTimeExcds);
21697c478bd9Sstevel@tonic-gate 	prval("icmp6InParmProblems",	icmp6->ipv6IfIcmpInParmProblems);
21707c478bd9Sstevel@tonic-gate 	prval("icmp6InPktTooBigs",	icmp6->ipv6IfIcmpInPktTooBigs);
21717c478bd9Sstevel@tonic-gate 	prval("icmp6InEchos",		icmp6->ipv6IfIcmpInEchos);
21727c478bd9Sstevel@tonic-gate 	prval("icmp6InEchoReplies",	icmp6->ipv6IfIcmpInEchoReplies);
21737c478bd9Sstevel@tonic-gate 	prval("icmp6InRouterSols",	icmp6->ipv6IfIcmpInRouterSolicits);
21747c478bd9Sstevel@tonic-gate 	prval("icmp6InRouterAds",
21757c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpInRouterAdvertisements);
21767c478bd9Sstevel@tonic-gate 	prval("icmp6InNeighborSols",	icmp6->ipv6IfIcmpInNeighborSolicits);
21777c478bd9Sstevel@tonic-gate 	prval("icmp6InNeighborAds",
21787c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpInNeighborAdvertisements);
21797c478bd9Sstevel@tonic-gate 	prval("icmp6InRedirects",	icmp6->ipv6IfIcmpInRedirects);
21807c478bd9Sstevel@tonic-gate 	prval("icmp6InBadRedirects",	icmp6->ipv6IfIcmpInBadRedirects);
21817c478bd9Sstevel@tonic-gate 	prval("icmp6InGroupQueries",	icmp6->ipv6IfIcmpInGroupMembQueries);
21827c478bd9Sstevel@tonic-gate 	prval("icmp6InGroupResps",	icmp6->ipv6IfIcmpInGroupMembResponses);
21837c478bd9Sstevel@tonic-gate 	prval("icmp6InGroupReds",	icmp6->ipv6IfIcmpInGroupMembReductions);
21847c478bd9Sstevel@tonic-gate 	prval("icmp6InOverflows",	icmp6->ipv6IfIcmpInOverflows);
21857c478bd9Sstevel@tonic-gate 	prval_end();
21867c478bd9Sstevel@tonic-gate 	prval_init();
21877c478bd9Sstevel@tonic-gate 	prval("icmp6OutMsgs",		icmp6->ipv6IfIcmpOutMsgs);
21887c478bd9Sstevel@tonic-gate 	prval("icmp6OutErrors",		icmp6->ipv6IfIcmpOutErrors);
21897c478bd9Sstevel@tonic-gate 	prval("icmp6OutDestUnreachs",	icmp6->ipv6IfIcmpOutDestUnreachs);
21907c478bd9Sstevel@tonic-gate 	prval("icmp6OutAdminProhibs",	icmp6->ipv6IfIcmpOutAdminProhibs);
21917c478bd9Sstevel@tonic-gate 	prval("icmp6OutTimeExcds",	icmp6->ipv6IfIcmpOutTimeExcds);
21927c478bd9Sstevel@tonic-gate 	prval("icmp6OutParmProblems",	icmp6->ipv6IfIcmpOutParmProblems);
21937c478bd9Sstevel@tonic-gate 	prval("icmp6OutPktTooBigs",	icmp6->ipv6IfIcmpOutPktTooBigs);
21947c478bd9Sstevel@tonic-gate 	prval("icmp6OutEchos",		icmp6->ipv6IfIcmpOutEchos);
21957c478bd9Sstevel@tonic-gate 	prval("icmp6OutEchoReplies",	icmp6->ipv6IfIcmpOutEchoReplies);
21967c478bd9Sstevel@tonic-gate 	prval("icmp6OutRouterSols",	icmp6->ipv6IfIcmpOutRouterSolicits);
21977c478bd9Sstevel@tonic-gate 	prval("icmp6OutRouterAds",
21987c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpOutRouterAdvertisements);
21997c478bd9Sstevel@tonic-gate 	prval("icmp6OutNeighborSols",	icmp6->ipv6IfIcmpOutNeighborSolicits);
22007c478bd9Sstevel@tonic-gate 	prval("icmp6OutNeighborAds",
22017c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpOutNeighborAdvertisements);
22027c478bd9Sstevel@tonic-gate 	prval("icmp6OutRedirects",	icmp6->ipv6IfIcmpOutRedirects);
22037c478bd9Sstevel@tonic-gate 	prval("icmp6OutGroupQueries",	icmp6->ipv6IfIcmpOutGroupMembQueries);
22047c478bd9Sstevel@tonic-gate 	prval("icmp6OutGroupResps",
22057c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpOutGroupMembResponses);
22067c478bd9Sstevel@tonic-gate 	prval("icmp6OutGroupReds",
22077c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpOutGroupMembReductions);
22087c478bd9Sstevel@tonic-gate 	prval_end();
22097c478bd9Sstevel@tonic-gate }
22107c478bd9Sstevel@tonic-gate 
22117c478bd9Sstevel@tonic-gate static void
22127c478bd9Sstevel@tonic-gate print_sctp_stats(mib2_sctp_t *sctp)
22137c478bd9Sstevel@tonic-gate {
22147c478bd9Sstevel@tonic-gate 	prval_init();
22157c478bd9Sstevel@tonic-gate 	pr_sctp_rtoalgo("sctpRtoAlgorithm", sctp->sctpRtoAlgorithm);
22167c478bd9Sstevel@tonic-gate 	prval("sctpRtoMin",		sctp->sctpRtoMin);
22177c478bd9Sstevel@tonic-gate 	prval("sctpRtoMax",		sctp->sctpRtoMax);
22187c478bd9Sstevel@tonic-gate 	prval("sctpRtoInitial",		sctp->sctpRtoInitial);
22197c478bd9Sstevel@tonic-gate 	pr_int_val("sctpMaxAssocs",	sctp->sctpMaxAssocs);
22207c478bd9Sstevel@tonic-gate 	prval("sctpValCookieLife",	sctp->sctpValCookieLife);
22217c478bd9Sstevel@tonic-gate 	prval("sctpMaxInitRetr",	sctp->sctpMaxInitRetr);
22227c478bd9Sstevel@tonic-gate 	prval("sctpCurrEstab",		sctp->sctpCurrEstab);
22237c478bd9Sstevel@tonic-gate 	prval("sctpActiveEstab",	sctp->sctpActiveEstab);
22247c478bd9Sstevel@tonic-gate 	prval("sctpPassiveEstab",	sctp->sctpPassiveEstab);
22257c478bd9Sstevel@tonic-gate 	prval("sctpAborted",		sctp->sctpAborted);
22267c478bd9Sstevel@tonic-gate 	prval("sctpShutdowns",		sctp->sctpShutdowns);
22277c478bd9Sstevel@tonic-gate 	prval("sctpOutOfBlue",		sctp->sctpOutOfBlue);
22287c478bd9Sstevel@tonic-gate 	prval("sctpChecksumError",	sctp->sctpChecksumError);
22297c478bd9Sstevel@tonic-gate 	prval64("sctpOutCtrlChunks",	sctp->sctpOutCtrlChunks);
22307c478bd9Sstevel@tonic-gate 	prval64("sctpOutOrderChunks",	sctp->sctpOutOrderChunks);
22317c478bd9Sstevel@tonic-gate 	prval64("sctpOutUnorderChunks",	sctp->sctpOutUnorderChunks);
22327c478bd9Sstevel@tonic-gate 	prval64("sctpRetransChunks",	sctp->sctpRetransChunks);
22337c478bd9Sstevel@tonic-gate 	prval("sctpOutAck",		sctp->sctpOutAck);
22347c478bd9Sstevel@tonic-gate 	prval("sctpOutAckDelayed",	sctp->sctpOutAckDelayed);
22357c478bd9Sstevel@tonic-gate 	prval("sctpOutWinUpdate",	sctp->sctpOutWinUpdate);
22367c478bd9Sstevel@tonic-gate 	prval("sctpOutFastRetrans",	sctp->sctpOutFastRetrans);
22377c478bd9Sstevel@tonic-gate 	prval("sctpOutWinProbe",	sctp->sctpOutWinProbe);
22387c478bd9Sstevel@tonic-gate 	prval64("sctpInCtrlChunks",	sctp->sctpInCtrlChunks);
22397c478bd9Sstevel@tonic-gate 	prval64("sctpInOrderChunks",	sctp->sctpInOrderChunks);
22407c478bd9Sstevel@tonic-gate 	prval64("sctpInUnorderChunks",	sctp->sctpInUnorderChunks);
22417c478bd9Sstevel@tonic-gate 	prval("sctpInAck",		sctp->sctpInAck);
22427c478bd9Sstevel@tonic-gate 	prval("sctpInDupAck",		sctp->sctpInDupAck);
22437c478bd9Sstevel@tonic-gate 	prval("sctpInAckUnsent",	sctp->sctpInAckUnsent);
22447c478bd9Sstevel@tonic-gate 	prval64("sctpFragUsrMsgs",	sctp->sctpFragUsrMsgs);
22457c478bd9Sstevel@tonic-gate 	prval64("sctpReasmUsrMsgs",	sctp->sctpReasmUsrMsgs);
22467c478bd9Sstevel@tonic-gate 	prval64("sctpOutSCTPPkts",	sctp->sctpOutSCTPPkts);
22477c478bd9Sstevel@tonic-gate 	prval64("sctpInSCTPPkts",	sctp->sctpInSCTPPkts);
22487c478bd9Sstevel@tonic-gate 	prval("sctpInInvalidCookie",	sctp->sctpInInvalidCookie);
22497c478bd9Sstevel@tonic-gate 	prval("sctpTimRetrans",		sctp->sctpTimRetrans);
22507c478bd9Sstevel@tonic-gate 	prval("sctpTimRetransDrop",	sctp->sctpTimRetransDrop);
22517c478bd9Sstevel@tonic-gate 	prval("sctpTimHearBeatProbe",	sctp->sctpTimHeartBeatProbe);
22527c478bd9Sstevel@tonic-gate 	prval("sctpTimHearBeatDrop",	sctp->sctpTimHeartBeatDrop);
22537c478bd9Sstevel@tonic-gate 	prval("sctpListenDrop",		sctp->sctpListenDrop);
22547c478bd9Sstevel@tonic-gate 	prval("sctpInClosed",		sctp->sctpInClosed);
22557c478bd9Sstevel@tonic-gate 	prval_end();
22567c478bd9Sstevel@tonic-gate }
22577c478bd9Sstevel@tonic-gate 
22587c478bd9Sstevel@tonic-gate static void
22597c478bd9Sstevel@tonic-gate print_tcp_stats(mib2_tcp_t *tcp)
22607c478bd9Sstevel@tonic-gate {
22617c478bd9Sstevel@tonic-gate 	prval_init();
22627c478bd9Sstevel@tonic-gate 	pr_int_val("tcpRtoAlgorithm",	tcp->tcpRtoAlgorithm);
22637c478bd9Sstevel@tonic-gate 	pr_int_val("tcpRtoMin",		tcp->tcpRtoMin);
22647c478bd9Sstevel@tonic-gate 	pr_int_val("tcpRtoMax",		tcp->tcpRtoMax);
22657c478bd9Sstevel@tonic-gate 	pr_int_val("tcpMaxConn",	tcp->tcpMaxConn);
22667c478bd9Sstevel@tonic-gate 	prval("tcpActiveOpens",		tcp->tcpActiveOpens);
22677c478bd9Sstevel@tonic-gate 	prval("tcpPassiveOpens",	tcp->tcpPassiveOpens);
22687c478bd9Sstevel@tonic-gate 	prval("tcpAttemptFails",	tcp->tcpAttemptFails);
22697c478bd9Sstevel@tonic-gate 	prval("tcpEstabResets",		tcp->tcpEstabResets);
22707c478bd9Sstevel@tonic-gate 	prval("tcpCurrEstab",		tcp->tcpCurrEstab);
22713173664eSapersson 	prval64("tcpOutSegs",		tcp->tcpHCOutSegs);
22727c478bd9Sstevel@tonic-gate 	prval("tcpOutDataSegs",		tcp->tcpOutDataSegs);
22737c478bd9Sstevel@tonic-gate 	prval("tcpOutDataBytes",	tcp->tcpOutDataBytes);
22747c478bd9Sstevel@tonic-gate 	prval("tcpRetransSegs",		tcp->tcpRetransSegs);
22757c478bd9Sstevel@tonic-gate 	prval("tcpRetransBytes",	tcp->tcpRetransBytes);
22767c478bd9Sstevel@tonic-gate 	prval("tcpOutAck",		tcp->tcpOutAck);
22777c478bd9Sstevel@tonic-gate 	prval("tcpOutAckDelayed",	tcp->tcpOutAckDelayed);
22787c478bd9Sstevel@tonic-gate 	prval("tcpOutUrg",		tcp->tcpOutUrg);
22797c478bd9Sstevel@tonic-gate 	prval("tcpOutWinUpdate",	tcp->tcpOutWinUpdate);
22807c478bd9Sstevel@tonic-gate 	prval("tcpOutWinProbe",		tcp->tcpOutWinProbe);
22817c478bd9Sstevel@tonic-gate 	prval("tcpOutControl",		tcp->tcpOutControl);
22827c478bd9Sstevel@tonic-gate 	prval("tcpOutRsts",		tcp->tcpOutRsts);
22837c478bd9Sstevel@tonic-gate 	prval("tcpOutFastRetrans",	tcp->tcpOutFastRetrans);
22843173664eSapersson 	prval64("tcpInSegs",		tcp->tcpHCInSegs);
22857c478bd9Sstevel@tonic-gate 	prval_end();
22867c478bd9Sstevel@tonic-gate 	prval("tcpInAckSegs",		tcp->tcpInAckSegs);
22877c478bd9Sstevel@tonic-gate 	prval("tcpInAckBytes",		tcp->tcpInAckBytes);
22887c478bd9Sstevel@tonic-gate 	prval("tcpInDupAck",		tcp->tcpInDupAck);
22897c478bd9Sstevel@tonic-gate 	prval("tcpInAckUnsent",		tcp->tcpInAckUnsent);
22907c478bd9Sstevel@tonic-gate 	prval("tcpInInorderSegs",	tcp->tcpInDataInorderSegs);
22917c478bd9Sstevel@tonic-gate 	prval("tcpInInorderBytes",	tcp->tcpInDataInorderBytes);
22927c478bd9Sstevel@tonic-gate 	prval("tcpInUnorderSegs",	tcp->tcpInDataUnorderSegs);
22937c478bd9Sstevel@tonic-gate 	prval("tcpInUnorderBytes",	tcp->tcpInDataUnorderBytes);
22947c478bd9Sstevel@tonic-gate 	prval("tcpInDupSegs",		tcp->tcpInDataDupSegs);
22957c478bd9Sstevel@tonic-gate 	prval("tcpInDupBytes",		tcp->tcpInDataDupBytes);
22967c478bd9Sstevel@tonic-gate 	prval("tcpInPartDupSegs",	tcp->tcpInDataPartDupSegs);
22977c478bd9Sstevel@tonic-gate 	prval("tcpInPartDupBytes",	tcp->tcpInDataPartDupBytes);
22987c478bd9Sstevel@tonic-gate 	prval("tcpInPastWinSegs",	tcp->tcpInDataPastWinSegs);
22997c478bd9Sstevel@tonic-gate 	prval("tcpInPastWinBytes",	tcp->tcpInDataPastWinBytes);
23007c478bd9Sstevel@tonic-gate 	prval("tcpInWinProbe",		tcp->tcpInWinProbe);
23017c478bd9Sstevel@tonic-gate 	prval("tcpInWinUpdate",		tcp->tcpInWinUpdate);
23027c478bd9Sstevel@tonic-gate 	prval("tcpInClosed",		tcp->tcpInClosed);
23037c478bd9Sstevel@tonic-gate 	prval("tcpRttNoUpdate",		tcp->tcpRttNoUpdate);
23047c478bd9Sstevel@tonic-gate 	prval("tcpRttUpdate",		tcp->tcpRttUpdate);
23057c478bd9Sstevel@tonic-gate 	prval("tcpTimRetrans",		tcp->tcpTimRetrans);
23067c478bd9Sstevel@tonic-gate 	prval("tcpTimRetransDrop",	tcp->tcpTimRetransDrop);
23077c478bd9Sstevel@tonic-gate 	prval("tcpTimKeepalive",	tcp->tcpTimKeepalive);
23087c478bd9Sstevel@tonic-gate 	prval("tcpTimKeepaliveProbe",	tcp->tcpTimKeepaliveProbe);
23097c478bd9Sstevel@tonic-gate 	prval("tcpTimKeepaliveDrop",	tcp->tcpTimKeepaliveDrop);
23107c478bd9Sstevel@tonic-gate 	prval("tcpListenDrop",		tcp->tcpListenDrop);
23117c478bd9Sstevel@tonic-gate 	prval("tcpListenDropQ0",	tcp->tcpListenDropQ0);
23127c478bd9Sstevel@tonic-gate 	prval("tcpHalfOpenDrop",	tcp->tcpHalfOpenDrop);
23137c478bd9Sstevel@tonic-gate 	prval("tcpOutSackRetrans",	tcp->tcpOutSackRetransSegs);
23147c478bd9Sstevel@tonic-gate 	prval_end();
23157c478bd9Sstevel@tonic-gate 
23167c478bd9Sstevel@tonic-gate }
23177c478bd9Sstevel@tonic-gate 
23187c478bd9Sstevel@tonic-gate static void
23197c478bd9Sstevel@tonic-gate print_udp_stats(mib2_udp_t *udp)
23207c478bd9Sstevel@tonic-gate {
23217c478bd9Sstevel@tonic-gate 	prval_init();
23223173664eSapersson 	prval64("udpInDatagrams",	udp->udpHCInDatagrams);
23237c478bd9Sstevel@tonic-gate 	prval("udpInErrors",		udp->udpInErrors);
23243173664eSapersson 	prval64("udpOutDatagrams",	udp->udpHCOutDatagrams);
23257c478bd9Sstevel@tonic-gate 	prval("udpOutErrors",		udp->udpOutErrors);
23267c478bd9Sstevel@tonic-gate 	prval_end();
23277c478bd9Sstevel@tonic-gate }
23287c478bd9Sstevel@tonic-gate 
23297c478bd9Sstevel@tonic-gate static void
23307c478bd9Sstevel@tonic-gate print_rawip_stats(mib2_rawip_t *rawip)
23317c478bd9Sstevel@tonic-gate {
23327c478bd9Sstevel@tonic-gate 	prval_init();
23337c478bd9Sstevel@tonic-gate 	prval("rawipInDatagrams",	rawip->rawipInDatagrams);
23347c478bd9Sstevel@tonic-gate 	prval("rawipInErrors",		rawip->rawipInErrors);
23357c478bd9Sstevel@tonic-gate 	prval("rawipInCksumErrs",	rawip->rawipInCksumErrs);
23367c478bd9Sstevel@tonic-gate 	prval("rawipOutDatagrams",	rawip->rawipOutDatagrams);
23377c478bd9Sstevel@tonic-gate 	prval("rawipOutErrors",		rawip->rawipOutErrors);
23387c478bd9Sstevel@tonic-gate 	prval_end();
23397c478bd9Sstevel@tonic-gate }
23407c478bd9Sstevel@tonic-gate 
23417c478bd9Sstevel@tonic-gate void
23427c478bd9Sstevel@tonic-gate print_igmp_stats(struct igmpstat *igps)
23437c478bd9Sstevel@tonic-gate {
23447c478bd9Sstevel@tonic-gate 	(void) printf(" %10u message%s received\n",
23457c478bd9Sstevel@tonic-gate 	    igps->igps_rcv_total, PLURAL(igps->igps_rcv_total));
23467c478bd9Sstevel@tonic-gate 	(void) printf(" %10u message%s received with too few bytes\n",
23477c478bd9Sstevel@tonic-gate 	    igps->igps_rcv_tooshort, PLURAL(igps->igps_rcv_tooshort));
23487c478bd9Sstevel@tonic-gate 	(void) printf(" %10u message%s received with bad checksum\n",
23497c478bd9Sstevel@tonic-gate 	    igps->igps_rcv_badsum, PLURAL(igps->igps_rcv_badsum));
23507c478bd9Sstevel@tonic-gate 	(void) printf(" %10u membership quer%s received\n",
23517c478bd9Sstevel@tonic-gate 	    igps->igps_rcv_queries, PLURALY(igps->igps_rcv_queries));
23527c478bd9Sstevel@tonic-gate 	(void) printf(" %10u membership quer%s received with invalid "
23537c478bd9Sstevel@tonic-gate 	    "field(s)\n",
23547c478bd9Sstevel@tonic-gate 	    igps->igps_rcv_badqueries, PLURALY(igps->igps_rcv_badqueries));
23557c478bd9Sstevel@tonic-gate 	(void) printf(" %10u membership report%s received\n",
23567c478bd9Sstevel@tonic-gate 	    igps->igps_rcv_reports, PLURAL(igps->igps_rcv_reports));
23577c478bd9Sstevel@tonic-gate 	(void) printf(" %10u membership report%s received with invalid "
23587c478bd9Sstevel@tonic-gate 	    "field(s)\n",
23597c478bd9Sstevel@tonic-gate 	    igps->igps_rcv_badreports, PLURAL(igps->igps_rcv_badreports));
23607c478bd9Sstevel@tonic-gate 	(void) printf(" %10u membership report%s received for groups to "
23617c478bd9Sstevel@tonic-gate 	    "which we belong\n",
23627c478bd9Sstevel@tonic-gate 	    igps->igps_rcv_ourreports, PLURAL(igps->igps_rcv_ourreports));
23637c478bd9Sstevel@tonic-gate 	(void) printf(" %10u membership report%s sent\n",
23647c478bd9Sstevel@tonic-gate 	    igps->igps_snd_reports, PLURAL(igps->igps_snd_reports));
23657c478bd9Sstevel@tonic-gate }
23667c478bd9Sstevel@tonic-gate 
23677c478bd9Sstevel@tonic-gate static void
23687c478bd9Sstevel@tonic-gate print_mrt_stats(struct mrtstat *mrts)
23697c478bd9Sstevel@tonic-gate {
23707c478bd9Sstevel@tonic-gate 	(void) puts("DVMRP multicast routing:");
23717c478bd9Sstevel@tonic-gate 	(void) printf(" %10u hit%s - kernel forwarding cache hits\n",
23727c478bd9Sstevel@tonic-gate 		mrts->mrts_mfc_hits, PLURAL(mrts->mrts_mfc_hits));
23737c478bd9Sstevel@tonic-gate 	(void) printf(" %10u miss%s - kernel forwarding cache misses\n",
23747c478bd9Sstevel@tonic-gate 		mrts->mrts_mfc_misses, PLURALES(mrts->mrts_mfc_misses));
23757c478bd9Sstevel@tonic-gate 	(void) printf(" %10u packet%s potentially forwarded\n",
23767c478bd9Sstevel@tonic-gate 		mrts->mrts_fwd_in, PLURAL(mrts->mrts_fwd_in));
23777c478bd9Sstevel@tonic-gate 	(void) printf(" %10u packet%s actually sent out\n",
23787c478bd9Sstevel@tonic-gate 		mrts->mrts_fwd_out, PLURAL(mrts->mrts_fwd_out));
23797c478bd9Sstevel@tonic-gate 	(void) printf(" %10u upcall%s - upcalls made to mrouted\n",
23807c478bd9Sstevel@tonic-gate 		mrts->mrts_upcalls, PLURAL(mrts->mrts_upcalls));
23817c478bd9Sstevel@tonic-gate 	(void) printf(" %10u packet%s not sent out due to lack of resources\n",
23827c478bd9Sstevel@tonic-gate 		mrts->mrts_fwd_drop, PLURAL(mrts->mrts_fwd_drop));
23837c478bd9Sstevel@tonic-gate 	(void) printf(" %10u datagram%s with malformed tunnel options\n",
23847c478bd9Sstevel@tonic-gate 		mrts->mrts_bad_tunnel, PLURAL(mrts->mrts_bad_tunnel));
23857c478bd9Sstevel@tonic-gate 	(void) printf(" %10u datagram%s with no room for tunnel options\n",
23867c478bd9Sstevel@tonic-gate 		mrts->mrts_cant_tunnel, PLURAL(mrts->mrts_cant_tunnel));
23877c478bd9Sstevel@tonic-gate 	(void) printf(" %10u datagram%s arrived on wrong interface\n",
23887c478bd9Sstevel@tonic-gate 		mrts->mrts_wrong_if, PLURAL(mrts->mrts_wrong_if));
23897c478bd9Sstevel@tonic-gate 	(void) printf(" %10u datagram%s dropped due to upcall Q overflow\n",
23907c478bd9Sstevel@tonic-gate 		mrts->mrts_upq_ovflw, PLURAL(mrts->mrts_upq_ovflw));
23917c478bd9Sstevel@tonic-gate 	(void) printf(" %10u datagram%s cleaned up by the cache\n",
23927c478bd9Sstevel@tonic-gate 		mrts->mrts_cache_cleanups, PLURAL(mrts->mrts_cache_cleanups));
23937c478bd9Sstevel@tonic-gate 	(void) printf(" %10u datagram%s dropped selectively by ratelimiter\n",
23947c478bd9Sstevel@tonic-gate 		mrts->mrts_drop_sel, PLURAL(mrts->mrts_drop_sel));
23957c478bd9Sstevel@tonic-gate 	(void) printf(" %10u datagram%s dropped - bucket Q overflow\n",
23967c478bd9Sstevel@tonic-gate 		mrts->mrts_q_overflow, PLURAL(mrts->mrts_q_overflow));
23977c478bd9Sstevel@tonic-gate 	(void) printf(" %10u datagram%s dropped - larger than bkt size\n",
23987c478bd9Sstevel@tonic-gate 		mrts->mrts_pkt2large, PLURAL(mrts->mrts_pkt2large));
23997c478bd9Sstevel@tonic-gate 	(void) printf("\nPIM multicast routing:\n");
24007c478bd9Sstevel@tonic-gate 	(void) printf(" %10u datagram%s dropped - bad version number\n",
24017c478bd9Sstevel@tonic-gate 		mrts->mrts_pim_badversion, PLURAL(mrts->mrts_pim_badversion));
24027c478bd9Sstevel@tonic-gate 	(void) printf(" %10u datagram%s dropped - bad checksum\n",
24037c478bd9Sstevel@tonic-gate 		mrts->mrts_pim_rcv_badcsum, PLURAL(mrts->mrts_pim_rcv_badcsum));
24047c478bd9Sstevel@tonic-gate 	(void) printf(" %10u datagram%s dropped - bad register packets\n",
24057c478bd9Sstevel@tonic-gate 		mrts->mrts_pim_badregisters,
24067c478bd9Sstevel@tonic-gate 		PLURAL(mrts->mrts_pim_badregisters));
24077c478bd9Sstevel@tonic-gate 	(void) printf(
24087c478bd9Sstevel@tonic-gate 		" %10u datagram%s potentially forwarded - register packets\n",
24097c478bd9Sstevel@tonic-gate 		mrts->mrts_pim_regforwards, PLURAL(mrts->mrts_pim_regforwards));
24107c478bd9Sstevel@tonic-gate 	(void) printf(" %10u datagram%s dropped - register send drops\n",
24117c478bd9Sstevel@tonic-gate 		mrts->mrts_pim_regsend_drops,
24127c478bd9Sstevel@tonic-gate 		PLURAL(mrts->mrts_pim_regsend_drops));
24137c478bd9Sstevel@tonic-gate 	(void) printf(" %10u datagram%s dropped - packet malformed\n",
24147c478bd9Sstevel@tonic-gate 		mrts->mrts_pim_malformed, PLURAL(mrts->mrts_pim_malformed));
24157c478bd9Sstevel@tonic-gate 	(void) printf(" %10u datagram%s dropped - no memory to forward\n",
24167c478bd9Sstevel@tonic-gate 		mrts->mrts_pim_nomemory, PLURAL(mrts->mrts_pim_nomemory));
24177c478bd9Sstevel@tonic-gate }
24187c478bd9Sstevel@tonic-gate 
24197c478bd9Sstevel@tonic-gate static void
24207c478bd9Sstevel@tonic-gate sum_ip6_stats(mib2_ipv6IfStatsEntry_t *ip6, mib2_ipv6IfStatsEntry_t *sum6)
24217c478bd9Sstevel@tonic-gate {
24227c478bd9Sstevel@tonic-gate 	/* First few are not additive */
24237c478bd9Sstevel@tonic-gate 	sum6->ipv6Forwarding = ip6->ipv6Forwarding;
24247c478bd9Sstevel@tonic-gate 	sum6->ipv6DefaultHopLimit = ip6->ipv6DefaultHopLimit;
24257c478bd9Sstevel@tonic-gate 
24267c478bd9Sstevel@tonic-gate 	sum6->ipv6InReceives += ip6->ipv6InReceives;
24277c478bd9Sstevel@tonic-gate 	sum6->ipv6InHdrErrors += ip6->ipv6InHdrErrors;
24287c478bd9Sstevel@tonic-gate 	sum6->ipv6InTooBigErrors += ip6->ipv6InTooBigErrors;
24297c478bd9Sstevel@tonic-gate 	sum6->ipv6InNoRoutes += ip6->ipv6InNoRoutes;
24307c478bd9Sstevel@tonic-gate 	sum6->ipv6InAddrErrors += ip6->ipv6InAddrErrors;
24317c478bd9Sstevel@tonic-gate 	sum6->ipv6InUnknownProtos += ip6->ipv6InUnknownProtos;
24327c478bd9Sstevel@tonic-gate 	sum6->ipv6InTruncatedPkts += ip6->ipv6InTruncatedPkts;
24337c478bd9Sstevel@tonic-gate 	sum6->ipv6InDiscards += ip6->ipv6InDiscards;
24347c478bd9Sstevel@tonic-gate 	sum6->ipv6InDelivers += ip6->ipv6InDelivers;
24357c478bd9Sstevel@tonic-gate 	sum6->ipv6OutForwDatagrams += ip6->ipv6OutForwDatagrams;
24367c478bd9Sstevel@tonic-gate 	sum6->ipv6OutRequests += ip6->ipv6OutRequests;
24377c478bd9Sstevel@tonic-gate 	sum6->ipv6OutDiscards += ip6->ipv6OutDiscards;
24387c478bd9Sstevel@tonic-gate 	sum6->ipv6OutFragOKs += ip6->ipv6OutFragOKs;
24397c478bd9Sstevel@tonic-gate 	sum6->ipv6OutFragFails += ip6->ipv6OutFragFails;
24407c478bd9Sstevel@tonic-gate 	sum6->ipv6OutFragCreates += ip6->ipv6OutFragCreates;
24417c478bd9Sstevel@tonic-gate 	sum6->ipv6ReasmReqds += ip6->ipv6ReasmReqds;
24427c478bd9Sstevel@tonic-gate 	sum6->ipv6ReasmOKs += ip6->ipv6ReasmOKs;
24437c478bd9Sstevel@tonic-gate 	sum6->ipv6ReasmFails += ip6->ipv6ReasmFails;
24447c478bd9Sstevel@tonic-gate 	sum6->ipv6InMcastPkts += ip6->ipv6InMcastPkts;
24457c478bd9Sstevel@tonic-gate 	sum6->ipv6OutMcastPkts += ip6->ipv6OutMcastPkts;
24467c478bd9Sstevel@tonic-gate 	sum6->ipv6OutNoRoutes += ip6->ipv6OutNoRoutes;
24477c478bd9Sstevel@tonic-gate 	sum6->ipv6ReasmDuplicates += ip6->ipv6ReasmDuplicates;
24487c478bd9Sstevel@tonic-gate 	sum6->ipv6ReasmPartDups += ip6->ipv6ReasmPartDups;
24497c478bd9Sstevel@tonic-gate 	sum6->ipv6ForwProhibits += ip6->ipv6ForwProhibits;
24507c478bd9Sstevel@tonic-gate 	sum6->udpInCksumErrs += ip6->udpInCksumErrs;
24517c478bd9Sstevel@tonic-gate 	sum6->udpInOverflows += ip6->udpInOverflows;
24527c478bd9Sstevel@tonic-gate 	sum6->rawipInOverflows += ip6->rawipInOverflows;
24537c478bd9Sstevel@tonic-gate }
24547c478bd9Sstevel@tonic-gate 
24557c478bd9Sstevel@tonic-gate static void
24567c478bd9Sstevel@tonic-gate sum_icmp6_stats(mib2_ipv6IfIcmpEntry_t *icmp6, mib2_ipv6IfIcmpEntry_t *sum6)
24577c478bd9Sstevel@tonic-gate {
24587c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInMsgs += icmp6->ipv6IfIcmpInMsgs;
24597c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInErrors += icmp6->ipv6IfIcmpInErrors;
24607c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInDestUnreachs += icmp6->ipv6IfIcmpInDestUnreachs;
24617c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInAdminProhibs += icmp6->ipv6IfIcmpInAdminProhibs;
24627c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInTimeExcds += icmp6->ipv6IfIcmpInTimeExcds;
24637c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInParmProblems += icmp6->ipv6IfIcmpInParmProblems;
24647c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInPktTooBigs += icmp6->ipv6IfIcmpInPktTooBigs;
24657c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInEchos += icmp6->ipv6IfIcmpInEchos;
24667c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInEchoReplies += icmp6->ipv6IfIcmpInEchoReplies;
24677c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInRouterSolicits += icmp6->ipv6IfIcmpInRouterSolicits;
24687c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInRouterAdvertisements +=
24697c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpInRouterAdvertisements;
24707c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInNeighborSolicits +=
24717c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpInNeighborSolicits;
24727c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInNeighborAdvertisements +=
24737c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpInNeighborAdvertisements;
24747c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInRedirects += icmp6->ipv6IfIcmpInRedirects;
24757c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInGroupMembQueries +=
24767c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpInGroupMembQueries;
24777c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInGroupMembResponses +=
24787c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpInGroupMembResponses;
24797c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInGroupMembReductions +=
24807c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpInGroupMembReductions;
24817c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutMsgs += icmp6->ipv6IfIcmpOutMsgs;
24827c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutErrors += icmp6->ipv6IfIcmpOutErrors;
24837c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutDestUnreachs += icmp6->ipv6IfIcmpOutDestUnreachs;
24847c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutAdminProhibs += icmp6->ipv6IfIcmpOutAdminProhibs;
24857c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutTimeExcds += icmp6->ipv6IfIcmpOutTimeExcds;
24867c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutParmProblems += icmp6->ipv6IfIcmpOutParmProblems;
24877c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutPktTooBigs += icmp6->ipv6IfIcmpOutPktTooBigs;
24887c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutEchos += icmp6->ipv6IfIcmpOutEchos;
24897c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutEchoReplies += icmp6->ipv6IfIcmpOutEchoReplies;
24907c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutRouterSolicits +=
24917c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpOutRouterSolicits;
24927c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutRouterAdvertisements +=
24937c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpOutRouterAdvertisements;
24947c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutNeighborSolicits +=
24957c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpOutNeighborSolicits;
24967c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutNeighborAdvertisements +=
24977c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpOutNeighborAdvertisements;
24987c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutRedirects += icmp6->ipv6IfIcmpOutRedirects;
24997c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutGroupMembQueries +=
25007c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpOutGroupMembQueries;
25017c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutGroupMembResponses +=
25027c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpOutGroupMembResponses;
25037c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpOutGroupMembReductions +=
25047c478bd9Sstevel@tonic-gate 	    icmp6->ipv6IfIcmpOutGroupMembReductions;
25057c478bd9Sstevel@tonic-gate 	sum6->ipv6IfIcmpInOverflows += icmp6->ipv6IfIcmpInOverflows;
25067c478bd9Sstevel@tonic-gate }
25077c478bd9Sstevel@tonic-gate 
25087c478bd9Sstevel@tonic-gate /* ----------------------------- MRT_STAT_REPORT --------------------------- */
25097c478bd9Sstevel@tonic-gate 
25107c478bd9Sstevel@tonic-gate static void
25117c478bd9Sstevel@tonic-gate mrt_stat_report(mib_item_t *curritem)
25127c478bd9Sstevel@tonic-gate {
25137c478bd9Sstevel@tonic-gate 	int	jtemp = 0;
25147c478bd9Sstevel@tonic-gate 	mib_item_t *tempitem;
25157c478bd9Sstevel@tonic-gate 
25167c478bd9Sstevel@tonic-gate 	if (!(family_selected(AF_INET)))
25177c478bd9Sstevel@tonic-gate 		return;
25187c478bd9Sstevel@tonic-gate 
25197c478bd9Sstevel@tonic-gate 	(void) putchar('\n');
25207c478bd9Sstevel@tonic-gate 	/* 'for' loop 1: */
25217c478bd9Sstevel@tonic-gate 	for (tempitem = curritem;
25227c478bd9Sstevel@tonic-gate 	    tempitem;
25237c478bd9Sstevel@tonic-gate 	    tempitem = tempitem->next_item) {
25247c478bd9Sstevel@tonic-gate 		if (Dflag) {
25257c478bd9Sstevel@tonic-gate 			(void) printf("\n--- Entry %d ---\n", ++jtemp);
25267c478bd9Sstevel@tonic-gate 			(void) printf("Group = %d, mib_id = %d, "
25277c478bd9Sstevel@tonic-gate 			    "length = %d, valp = 0x%p\n",
25287c478bd9Sstevel@tonic-gate 			    tempitem->group, tempitem->mib_id,
25297c478bd9Sstevel@tonic-gate 			    tempitem->length, tempitem->valp);
25307c478bd9Sstevel@tonic-gate 		}
25317c478bd9Sstevel@tonic-gate 
25327c478bd9Sstevel@tonic-gate 		if (tempitem->mib_id == 0) {
25337c478bd9Sstevel@tonic-gate 			switch (tempitem->group) {
25347c478bd9Sstevel@tonic-gate 			case EXPER_DVMRP: {
25357c478bd9Sstevel@tonic-gate 				struct mrtstat	*mrts;
25367c478bd9Sstevel@tonic-gate 				mrts = (struct mrtstat *)tempitem->valp;
25377c478bd9Sstevel@tonic-gate 
25387c478bd9Sstevel@tonic-gate 				if (!(family_selected(AF_INET)))
25397c478bd9Sstevel@tonic-gate 					continue; /* 'for' loop 1 */
25407c478bd9Sstevel@tonic-gate 
25417c478bd9Sstevel@tonic-gate 				print_mrt_stats(mrts);
25427c478bd9Sstevel@tonic-gate 				break;
25437c478bd9Sstevel@tonic-gate 			}
25447c478bd9Sstevel@tonic-gate 			}
25457c478bd9Sstevel@tonic-gate 		}
25467c478bd9Sstevel@tonic-gate 	} /* 'for' loop 1 ends */
25477c478bd9Sstevel@tonic-gate 	(void) putchar('\n');
25487c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
25497c478bd9Sstevel@tonic-gate }
25507c478bd9Sstevel@tonic-gate 
25517c478bd9Sstevel@tonic-gate /*
25527c478bd9Sstevel@tonic-gate  * if_stat_total() - Computes totals for interface statistics
25537c478bd9Sstevel@tonic-gate  *                   and returns result by updating sumstats.
25547c478bd9Sstevel@tonic-gate  */
25557c478bd9Sstevel@tonic-gate static void
25567c478bd9Sstevel@tonic-gate if_stat_total(struct ifstat *oldstats, struct ifstat *newstats,
25577c478bd9Sstevel@tonic-gate     struct ifstat *sumstats)
25587c478bd9Sstevel@tonic-gate {
25597c478bd9Sstevel@tonic-gate 	sumstats->ipackets += newstats->ipackets - oldstats->ipackets;
25607c478bd9Sstevel@tonic-gate 	sumstats->opackets += newstats->opackets - oldstats->opackets;
25617c478bd9Sstevel@tonic-gate 	sumstats->ierrors += newstats->ierrors - oldstats->ierrors;
25627c478bd9Sstevel@tonic-gate 	sumstats->oerrors += newstats->oerrors - oldstats->oerrors;
25637c478bd9Sstevel@tonic-gate 	sumstats->collisions += newstats->collisions - oldstats->collisions;
25647c478bd9Sstevel@tonic-gate }
25657c478bd9Sstevel@tonic-gate 
25667c478bd9Sstevel@tonic-gate /* --------------------- IF_REPORT (netstat -i)  -------------------------- */
25677c478bd9Sstevel@tonic-gate 
25687c478bd9Sstevel@tonic-gate static struct	ifstat	zerostat = {
25697c478bd9Sstevel@tonic-gate 	0LL, 0LL, 0LL, 0LL, 0LL
25707c478bd9Sstevel@tonic-gate };
25717c478bd9Sstevel@tonic-gate 
25727c478bd9Sstevel@tonic-gate static void
25737c478bd9Sstevel@tonic-gate if_report(mib_item_t *item, char *matchname,
25747c478bd9Sstevel@tonic-gate     int Iflag_only, boolean_t once_only)
25757c478bd9Sstevel@tonic-gate {
25767c478bd9Sstevel@tonic-gate 	static boolean_t	reentry = B_FALSE;
25777c478bd9Sstevel@tonic-gate 	boolean_t		alreadydone = B_FALSE;
25787c478bd9Sstevel@tonic-gate 	int			jtemp = 0;
25797c478bd9Sstevel@tonic-gate 	uint32_t		ifindex_v4 = 0;
25807c478bd9Sstevel@tonic-gate 	uint32_t		ifindex_v6 = 0;
25817c478bd9Sstevel@tonic-gate 
25827c478bd9Sstevel@tonic-gate 	/* 'for' loop 1: */
25837c478bd9Sstevel@tonic-gate 	for (; item; item = item->next_item) {
25847c478bd9Sstevel@tonic-gate 		if (Dflag) {
25857c478bd9Sstevel@tonic-gate 			(void) printf("\n--- Entry %d ---\n", ++jtemp);
25867c478bd9Sstevel@tonic-gate 			(void) printf("Group = %d, mib_id = %d, "
25877c478bd9Sstevel@tonic-gate 			    "length = %d, valp = 0x%p\n",
25887c478bd9Sstevel@tonic-gate 			    item->group, item->mib_id, item->length,
25897c478bd9Sstevel@tonic-gate 			    item->valp);
25907c478bd9Sstevel@tonic-gate 		}
25917c478bd9Sstevel@tonic-gate 
25927c478bd9Sstevel@tonic-gate 		switch (item->group) {
25937c478bd9Sstevel@tonic-gate 		case MIB2_IP:
25947c478bd9Sstevel@tonic-gate 		if (item->mib_id != MIB2_IP_ADDR ||
25957c478bd9Sstevel@tonic-gate 		    !family_selected(AF_INET))
25967c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
25977c478bd9Sstevel@tonic-gate 		{
25987c478bd9Sstevel@tonic-gate 			static struct ifstat	old = {0L, 0L, 0L, 0L, 0L};
25997c478bd9Sstevel@tonic-gate 			static struct ifstat	new = {0L, 0L, 0L, 0L, 0L};
26007c478bd9Sstevel@tonic-gate 			struct ifstat		sum;
26017c478bd9Sstevel@tonic-gate 			struct iflist		*newlist = NULL;
26027c478bd9Sstevel@tonic-gate 			static struct iflist	*oldlist = NULL;
26037c478bd9Sstevel@tonic-gate 			kstat_t	 *ksp;
26047c478bd9Sstevel@tonic-gate 
26057c478bd9Sstevel@tonic-gate 			if (once_only) {
26067c478bd9Sstevel@tonic-gate 				char    ifname[LIFNAMSIZ + 1];
26077c478bd9Sstevel@tonic-gate 				char    logintname[LIFNAMSIZ + 1];
26087c478bd9Sstevel@tonic-gate 				mib2_ipAddrEntry_t *ap;
26097c478bd9Sstevel@tonic-gate 				struct ifstat	stat = {0L, 0L, 0L, 0L, 0L};
26107c478bd9Sstevel@tonic-gate 				boolean_t	first = B_TRUE;
26117c478bd9Sstevel@tonic-gate 				uint32_t	new_ifindex;
26127c478bd9Sstevel@tonic-gate 
26137c478bd9Sstevel@tonic-gate 				if (Dflag)
26147c478bd9Sstevel@tonic-gate 					(void) printf("if_report: %d items\n",
26157c478bd9Sstevel@tonic-gate 					    (item->length)
26167c478bd9Sstevel@tonic-gate 					    / sizeof (mib2_ipAddrEntry_t));
26177c478bd9Sstevel@tonic-gate 
26187c478bd9Sstevel@tonic-gate 				/* 'for' loop 2a: */
26197c478bd9Sstevel@tonic-gate 				for (ap = (mib2_ipAddrEntry_t *)item->valp;
26207c478bd9Sstevel@tonic-gate 				    (char *)ap < (char *)item->valp
26217c478bd9Sstevel@tonic-gate 				    + item->length;
26227c478bd9Sstevel@tonic-gate 				    ap++) {
26237c478bd9Sstevel@tonic-gate 					(void) octetstr(&ap->ipAdEntIfIndex,
26247c478bd9Sstevel@tonic-gate 					    'a', logintname,
26257c478bd9Sstevel@tonic-gate 					    sizeof (logintname));
26267c478bd9Sstevel@tonic-gate 					(void) strcpy(ifname, logintname);
26277c478bd9Sstevel@tonic-gate 					(void) strtok(ifname, ":");
26287c478bd9Sstevel@tonic-gate 					if (matchname != NULL &&
26297c478bd9Sstevel@tonic-gate 					    strcmp(matchname, ifname) != 0 &&
26307c478bd9Sstevel@tonic-gate 					    strcmp(matchname, logintname) != 0)
26317c478bd9Sstevel@tonic-gate 						continue; /* 'for' loop 2a */
26327c478bd9Sstevel@tonic-gate 					new_ifindex =
26337c478bd9Sstevel@tonic-gate 					    if_nametoindex(logintname);
26347c478bd9Sstevel@tonic-gate 					if (new_ifindex != ifindex_v4 &&
26357c478bd9Sstevel@tonic-gate 					    (ksp = kstat_lookup(kc, NULL, -1,
26367c478bd9Sstevel@tonic-gate 					    ifname)) != NULL) {
26377c478bd9Sstevel@tonic-gate 						(void) safe_kstat_read(kc, ksp,
26387c478bd9Sstevel@tonic-gate 						    NULL);
26397c478bd9Sstevel@tonic-gate 						stat.ipackets =
26407c478bd9Sstevel@tonic-gate 						    kstat_named_value(ksp,
26417c478bd9Sstevel@tonic-gate 						    "ipackets");
26427c478bd9Sstevel@tonic-gate 						stat.ierrors =
26437c478bd9Sstevel@tonic-gate 						    kstat_named_value(ksp,
26447c478bd9Sstevel@tonic-gate 						    "ierrors");
26457c478bd9Sstevel@tonic-gate 						stat.opackets =
26467c478bd9Sstevel@tonic-gate 						    kstat_named_value(ksp,
26477c478bd9Sstevel@tonic-gate 						    "opackets");
26487c478bd9Sstevel@tonic-gate 						stat.oerrors =
26497c478bd9Sstevel@tonic-gate 						    kstat_named_value(ksp,
26507c478bd9Sstevel@tonic-gate 						    "oerrors");
26517c478bd9Sstevel@tonic-gate 						stat.collisions =
26527c478bd9Sstevel@tonic-gate 						    kstat_named_value(ksp,
26537c478bd9Sstevel@tonic-gate 						    "collisions");
26547c478bd9Sstevel@tonic-gate 						if (first) {
26557c478bd9Sstevel@tonic-gate 						(void) printf(
26567c478bd9Sstevel@tonic-gate 						    "%-5.5s %-5.5s%-13.13s "
26577c478bd9Sstevel@tonic-gate 						    "%-14.14s %-6.6s %-5.5s "
26587c478bd9Sstevel@tonic-gate 						    "%-6.6s %-5.5s %-6.6s "
26597c478bd9Sstevel@tonic-gate 						    "%-6.6s\n",
26607c478bd9Sstevel@tonic-gate 						    "Name", "Mtu", "Net/Dest",
26617c478bd9Sstevel@tonic-gate 						    "Address", "Ipkts",
26627c478bd9Sstevel@tonic-gate 						    "Ierrs", "Opkts", "Oerrs",
26637c478bd9Sstevel@tonic-gate 						    "Collis", "Queue");
26647c478bd9Sstevel@tonic-gate 						first = B_FALSE;
26657c478bd9Sstevel@tonic-gate 						}
26667c478bd9Sstevel@tonic-gate 						if_report_ip4(ap, ifname,
26677c478bd9Sstevel@tonic-gate 						    logintname, &stat, B_TRUE);
26687c478bd9Sstevel@tonic-gate 						ifindex_v4 = new_ifindex;
26697c478bd9Sstevel@tonic-gate 					} else {
26707c478bd9Sstevel@tonic-gate 						if_report_ip4(ap, ifname,
26717c478bd9Sstevel@tonic-gate 						    logintname, &stat, B_FALSE);
26727c478bd9Sstevel@tonic-gate 					}
26737c478bd9Sstevel@tonic-gate 				} /* 'for' loop 2a ends */
26747c478bd9Sstevel@tonic-gate 				if (!first)
26757c478bd9Sstevel@tonic-gate 					(void) putchar('\n');
26767c478bd9Sstevel@tonic-gate 			} else if (!alreadydone) {
26777c478bd9Sstevel@tonic-gate 				char    ifname[LIFNAMSIZ + 1];
26787c478bd9Sstevel@tonic-gate 				char    buf[LIFNAMSIZ + 1];
26797c478bd9Sstevel@tonic-gate 				mib2_ipAddrEntry_t *ap;
26807c478bd9Sstevel@tonic-gate 				struct ifstat   t;
2681aecc8c24Sja 				struct iflist	*tlp = NULL;
26827c478bd9Sstevel@tonic-gate 				struct iflist	**nextnew = &newlist;
26837c478bd9Sstevel@tonic-gate 				struct iflist	*walkold;
26847c478bd9Sstevel@tonic-gate 				struct iflist	*cleanlist;
2685aecc8c24Sja 				boolean_t	found_if = B_FALSE;
26867c478bd9Sstevel@tonic-gate 
26877c478bd9Sstevel@tonic-gate 				alreadydone = B_TRUE; /* ignore other case */
2688aecc8c24Sja 
2689aecc8c24Sja 				/*
2690aecc8c24Sja 				 * Check if there is anything to do.
2691aecc8c24Sja 				 */
2692aecc8c24Sja 				if (item->length <
2693aecc8c24Sja 				    sizeof (mib2_ipAddrEntry_t)) {
2694aecc8c24Sja 					fail(0, "No compatible interfaces");
2695aecc8c24Sja 				}
2696aecc8c24Sja 
26977c478bd9Sstevel@tonic-gate 				/*
2698aecc8c24Sja 				 * 'for' loop 2b: find the "right" entry:
2699aecc8c24Sja 				 * If an interface name to match has been
2700aecc8c24Sja 				 * supplied then try and find it, otherwise
2701aecc8c24Sja 				 * match the first non-loopback interface found.
2702aecc8c24Sja 				 * Use lo0 if all else fails.
27037c478bd9Sstevel@tonic-gate 				 */
27047c478bd9Sstevel@tonic-gate 				for (ap = (mib2_ipAddrEntry_t *)item->valp;
27057c478bd9Sstevel@tonic-gate 				    (char *)ap < (char *)item->valp
27067c478bd9Sstevel@tonic-gate 				    + item->length;
27077c478bd9Sstevel@tonic-gate 				    ap++) {
27087c478bd9Sstevel@tonic-gate 					(void) octetstr(&ap->ipAdEntIfIndex,
27097c478bd9Sstevel@tonic-gate 					'a', ifname, sizeof (ifname));
27107c478bd9Sstevel@tonic-gate 					(void) strtok(ifname, ":");
27117c478bd9Sstevel@tonic-gate 
27127c478bd9Sstevel@tonic-gate 					if (matchname) {
27137c478bd9Sstevel@tonic-gate 						if (strcmp(matchname,
2714aecc8c24Sja 						    ifname) == 0) {
27157c478bd9Sstevel@tonic-gate 							/* 'for' loop 2b */
2716aecc8c24Sja 							found_if = B_TRUE;
27177c478bd9Sstevel@tonic-gate 							break;
2718aecc8c24Sja 						}
2719aecc8c24Sja 					} else if (strcmp(ifname, "lo0") != 0)
27207c478bd9Sstevel@tonic-gate 						break; /* 'for' loop 2b */
27217c478bd9Sstevel@tonic-gate 				} /* 'for' loop 2b ends */
27227c478bd9Sstevel@tonic-gate 
2723aecc8c24Sja 				if (matchname == NULL) {
2724aecc8c24Sja 					matchname = ifname;
2725aecc8c24Sja 				} else {
2726aecc8c24Sja 					if (!found_if)
2727aecc8c24Sja 						fail(0, "-I: %s no such "
2728aecc8c24Sja 						    "interface.", matchname);
2729aecc8c24Sja 				}
2730aecc8c24Sja 
27317c478bd9Sstevel@tonic-gate 				if (Iflag_only == 0 || !reentry) {
27327c478bd9Sstevel@tonic-gate 					(void) printf("    input   %-6.6s    "
27337c478bd9Sstevel@tonic-gate 					    "output	",
27347c478bd9Sstevel@tonic-gate 					    matchname);
27357c478bd9Sstevel@tonic-gate 					(void) printf("   input  (Total)    "
27367c478bd9Sstevel@tonic-gate 					"output\n");
27377c478bd9Sstevel@tonic-gate 					(void) printf("%-7.7s %-5.5s %-7.7s "
27387c478bd9Sstevel@tonic-gate 					    "%-5.5s %-6.6s ",
27397c478bd9Sstevel@tonic-gate 					    "packets", "errs", "packets",
27407c478bd9Sstevel@tonic-gate 					    "errs", "colls");
27417c478bd9Sstevel@tonic-gate 					(void) printf("%-7.7s %-5.5s %-7.7s "
27427c478bd9Sstevel@tonic-gate 					    "%-5.5s %-6.6s\n",
27437c478bd9Sstevel@tonic-gate 					    "packets", "errs", "packets",
27447c478bd9Sstevel@tonic-gate 					    "errs", "colls");
27457c478bd9Sstevel@tonic-gate 				}
27467c478bd9Sstevel@tonic-gate 
27477c478bd9Sstevel@tonic-gate 				sum = zerostat;
27487c478bd9Sstevel@tonic-gate 
27497c478bd9Sstevel@tonic-gate 				/* 'for' loop 2c: */
27507c478bd9Sstevel@tonic-gate 				for (ap = (mib2_ipAddrEntry_t *)item->valp;
27517c478bd9Sstevel@tonic-gate 				    (char *)ap < (char *)item->valp
27527c478bd9Sstevel@tonic-gate 				    + item->length;
27537c478bd9Sstevel@tonic-gate 				    ap++) {
27547c478bd9Sstevel@tonic-gate 					(void) octetstr(&ap->ipAdEntIfIndex,
27557c478bd9Sstevel@tonic-gate 					    'a', buf, sizeof (buf));
27567c478bd9Sstevel@tonic-gate 					(void) strtok(buf, ":");
2757aecc8c24Sja 
2758aecc8c24Sja 					/*
2759aecc8c24Sja 					 * We have reduced the IP interface
2760aecc8c24Sja 					 * name, which could have been a
2761aecc8c24Sja 					 * logical, down to a name suitable
2762aecc8c24Sja 					 * for use with kstats.
2763aecc8c24Sja 					 * We treat this name as unique and
2764aecc8c24Sja 					 * only collate statistics for it once
2765aecc8c24Sja 					 * per pass. This is to avoid falsely
2766aecc8c24Sja 					 * amplifying these statistics by the
2767aecc8c24Sja 					 * the number of logical instances.
2768aecc8c24Sja 					 */
2769aecc8c24Sja 					if ((tlp != NULL) &&
2770aecc8c24Sja 					    ((strcmp(buf, tlp->ifname) == 0))) {
2771aecc8c24Sja 						continue;
2772aecc8c24Sja 					}
2773aecc8c24Sja 
27747c478bd9Sstevel@tonic-gate 					ksp = kstat_lookup(kc, NULL, -1, buf);
27757c478bd9Sstevel@tonic-gate 					if (ksp &&
27767c478bd9Sstevel@tonic-gate 					    ksp->ks_type == KSTAT_TYPE_NAMED)
27777c478bd9Sstevel@tonic-gate 						(void) safe_kstat_read(kc, ksp,
27787c478bd9Sstevel@tonic-gate 						    NULL);
27797c478bd9Sstevel@tonic-gate 
27807c478bd9Sstevel@tonic-gate 					t.ipackets = kstat_named_value(ksp,
27817c478bd9Sstevel@tonic-gate 					    "ipackets");
27827c478bd9Sstevel@tonic-gate 					t.ierrors = kstat_named_value(ksp,
27837c478bd9Sstevel@tonic-gate 					    "ierrors");
27847c478bd9Sstevel@tonic-gate 					t.opackets = kstat_named_value(ksp,
27857c478bd9Sstevel@tonic-gate 					    "opackets");
27867c478bd9Sstevel@tonic-gate 					t.oerrors = kstat_named_value(ksp,
27877c478bd9Sstevel@tonic-gate 					    "oerrors");
27887c478bd9Sstevel@tonic-gate 					t.collisions = kstat_named_value(ksp,
27897c478bd9Sstevel@tonic-gate 					    "collisions");
27907c478bd9Sstevel@tonic-gate 
27917c478bd9Sstevel@tonic-gate 					if (strcmp(buf, matchname) == 0)
27927c478bd9Sstevel@tonic-gate 						new = t;
27937c478bd9Sstevel@tonic-gate 
27947c478bd9Sstevel@tonic-gate 					/* Build the interface list */
27957c478bd9Sstevel@tonic-gate 
27967c478bd9Sstevel@tonic-gate 					tlp = malloc(sizeof (struct iflist));
27977c478bd9Sstevel@tonic-gate 					(void) strlcpy(tlp->ifname, buf,
27987c478bd9Sstevel@tonic-gate 					    sizeof (tlp->ifname));
27997c478bd9Sstevel@tonic-gate 					tlp->tot = t;
28007c478bd9Sstevel@tonic-gate 					*nextnew = tlp;
28017c478bd9Sstevel@tonic-gate 					nextnew = &tlp->next_if;
28027c478bd9Sstevel@tonic-gate 
28037c478bd9Sstevel@tonic-gate 					/*
28047c478bd9Sstevel@tonic-gate 					 * First time through.
28057c478bd9Sstevel@tonic-gate 					 * Just add up the interface stats.
28067c478bd9Sstevel@tonic-gate 					 */
28077c478bd9Sstevel@tonic-gate 
28087c478bd9Sstevel@tonic-gate 					if (oldlist == NULL) {
28097c478bd9Sstevel@tonic-gate 						if_stat_total(&zerostat,
28107c478bd9Sstevel@tonic-gate 						    &t, &sum);
28117c478bd9Sstevel@tonic-gate 						continue;
28127c478bd9Sstevel@tonic-gate 					}
28137c478bd9Sstevel@tonic-gate 
28147c478bd9Sstevel@tonic-gate 					/*
28157c478bd9Sstevel@tonic-gate 					 * Walk old list for the interface.
28167c478bd9Sstevel@tonic-gate 					 *
28177c478bd9Sstevel@tonic-gate 					 * If found, add difference to total.
28187c478bd9Sstevel@tonic-gate 					 *
28197c478bd9Sstevel@tonic-gate 					 * If not, an interface has been plumbed
28207c478bd9Sstevel@tonic-gate 					 * up.  In this case, we will simply
28217c478bd9Sstevel@tonic-gate 					 * ignore the new interface until the
28227c478bd9Sstevel@tonic-gate 					 * next interval; as there's no easy way
28237c478bd9Sstevel@tonic-gate 					 * to acquire statistics between time
28247c478bd9Sstevel@tonic-gate 					 * of the plumb and the next interval
28257c478bd9Sstevel@tonic-gate 					 * boundary.  This results in inaccurate
28267c478bd9Sstevel@tonic-gate 					 * total values for current interval.
28277c478bd9Sstevel@tonic-gate 					 *
28287c478bd9Sstevel@tonic-gate 					 * Note the case when an interface is
28297c478bd9Sstevel@tonic-gate 					 * unplumbed; as similar problems exist.
28307c478bd9Sstevel@tonic-gate 					 * The unplumbed interface is not in the
28317c478bd9Sstevel@tonic-gate 					 * current list, and there's no easy way
28327c478bd9Sstevel@tonic-gate 					 * to account for the statistics between
28337c478bd9Sstevel@tonic-gate 					 * the previous interval and time of the
28347c478bd9Sstevel@tonic-gate 					 * unplumb.  Therefore, we (in a sense)
28357c478bd9Sstevel@tonic-gate 					 * ignore the removed interface by only
28367c478bd9Sstevel@tonic-gate 					 * involving "current" interfaces when
28377c478bd9Sstevel@tonic-gate 					 * computing the total statistics.
28387c478bd9Sstevel@tonic-gate 					 * Unfortunately, this also results in
28397c478bd9Sstevel@tonic-gate 					 * inaccurate values for interval total.
28407c478bd9Sstevel@tonic-gate 					 */
28417c478bd9Sstevel@tonic-gate 
28427c478bd9Sstevel@tonic-gate 					for (walkold = oldlist;
28437c478bd9Sstevel@tonic-gate 					    walkold != NULL;
28447c478bd9Sstevel@tonic-gate 					    walkold = walkold->next_if) {
28457c478bd9Sstevel@tonic-gate 						if (strcmp(walkold->ifname,
28467c478bd9Sstevel@tonic-gate 						    buf) == 0) {
28477c478bd9Sstevel@tonic-gate 							if_stat_total(
28487c478bd9Sstevel@tonic-gate 							    &walkold->tot,
28497c478bd9Sstevel@tonic-gate 							    &t, &sum);
28507c478bd9Sstevel@tonic-gate 							break;
28517c478bd9Sstevel@tonic-gate 						}
28527c478bd9Sstevel@tonic-gate 					}
28537c478bd9Sstevel@tonic-gate 
28547c478bd9Sstevel@tonic-gate 				} /* 'for' loop 2c ends */
28557c478bd9Sstevel@tonic-gate 
28567c478bd9Sstevel@tonic-gate 				*nextnew = NULL;
28577c478bd9Sstevel@tonic-gate 
28587c478bd9Sstevel@tonic-gate 				(void) printf("%-7llu %-5llu %-7llu "
28597c478bd9Sstevel@tonic-gate 				    "%-5llu %-6llu ",
28607c478bd9Sstevel@tonic-gate 				    new.ipackets - old.ipackets,
28617c478bd9Sstevel@tonic-gate 				    new.ierrors - old.ierrors,
28627c478bd9Sstevel@tonic-gate 				    new.opackets - old.opackets,
28637c478bd9Sstevel@tonic-gate 				    new.oerrors - old.oerrors,
28647c478bd9Sstevel@tonic-gate 				    new.collisions - old.collisions);
28657c478bd9Sstevel@tonic-gate 
28667c478bd9Sstevel@tonic-gate 				(void) printf("%-7llu %-5llu %-7llu "
28677c478bd9Sstevel@tonic-gate 				    "%-5llu %-6llu\n", sum.ipackets,
28687c478bd9Sstevel@tonic-gate 				    sum.ierrors, sum.opackets,
28697c478bd9Sstevel@tonic-gate 				    sum.oerrors, sum.collisions);
28707c478bd9Sstevel@tonic-gate 
28717c478bd9Sstevel@tonic-gate 				/*
28727c478bd9Sstevel@tonic-gate 				 * Tidy things up once finished.
28737c478bd9Sstevel@tonic-gate 				 */
28747c478bd9Sstevel@tonic-gate 
28757c478bd9Sstevel@tonic-gate 				old = new;
28767c478bd9Sstevel@tonic-gate 				cleanlist = oldlist;
28777c478bd9Sstevel@tonic-gate 				oldlist = newlist;
28787c478bd9Sstevel@tonic-gate 				while (cleanlist != NULL) {
28797c478bd9Sstevel@tonic-gate 					tlp = cleanlist->next_if;
28807c478bd9Sstevel@tonic-gate 					free(cleanlist);
28817c478bd9Sstevel@tonic-gate 					cleanlist = tlp;
28827c478bd9Sstevel@tonic-gate 				}
28837c478bd9Sstevel@tonic-gate 			}
28847c478bd9Sstevel@tonic-gate 			break;
28857c478bd9Sstevel@tonic-gate 		}
28867c478bd9Sstevel@tonic-gate 		case MIB2_IP6:
28877c478bd9Sstevel@tonic-gate 		if (item->mib_id != MIB2_IP6_ADDR ||
28887c478bd9Sstevel@tonic-gate 		    !family_selected(AF_INET6))
28897c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
28907c478bd9Sstevel@tonic-gate 		{
28917c478bd9Sstevel@tonic-gate 			static struct ifstat	old6 = {0L, 0L, 0L, 0L, 0L};
28927c478bd9Sstevel@tonic-gate 			static struct ifstat	new6 = {0L, 0L, 0L, 0L, 0L};
28937c478bd9Sstevel@tonic-gate 			struct ifstat		sum6;
28947c478bd9Sstevel@tonic-gate 			struct iflist		*newlist6 = NULL;
28957c478bd9Sstevel@tonic-gate 			static struct iflist	*oldlist6 = NULL;
28967c478bd9Sstevel@tonic-gate 			kstat_t	 *ksp;
28977c478bd9Sstevel@tonic-gate 
28987c478bd9Sstevel@tonic-gate 			if (once_only) {
28997c478bd9Sstevel@tonic-gate 				char    ifname[LIFNAMSIZ + 1];
29007c478bd9Sstevel@tonic-gate 				char    logintname[LIFNAMSIZ + 1];
29017c478bd9Sstevel@tonic-gate 				mib2_ipv6AddrEntry_t *ap6;
29027c478bd9Sstevel@tonic-gate 				struct ifstat	stat = {0L, 0L, 0L, 0L, 0L};
29037c478bd9Sstevel@tonic-gate 				boolean_t	first = B_TRUE;
29047c478bd9Sstevel@tonic-gate 				uint32_t	new_ifindex;
29057c478bd9Sstevel@tonic-gate 
29067c478bd9Sstevel@tonic-gate 				if (Dflag)
29077c478bd9Sstevel@tonic-gate 					(void) printf("if_report: %d items\n",
29087c478bd9Sstevel@tonic-gate 					    (item->length)
29097c478bd9Sstevel@tonic-gate 					    / sizeof (mib2_ipv6AddrEntry_t));
29107c478bd9Sstevel@tonic-gate 				/* 'for' loop 2d: */
29117c478bd9Sstevel@tonic-gate 				for (ap6 = (mib2_ipv6AddrEntry_t *)item->valp;
29127c478bd9Sstevel@tonic-gate 				    (char *)ap6 < (char *)item->valp
29137c478bd9Sstevel@tonic-gate 				    + item->length;
29147c478bd9Sstevel@tonic-gate 				    ap6++) {
29157c478bd9Sstevel@tonic-gate 					(void) octetstr(&ap6->ipv6AddrIfIndex,
29167c478bd9Sstevel@tonic-gate 					    'a', logintname,
29177c478bd9Sstevel@tonic-gate 					    sizeof (logintname));
29187c478bd9Sstevel@tonic-gate 					(void) strcpy(ifname, logintname);
29197c478bd9Sstevel@tonic-gate 					(void) strtok(ifname, ":");
29207c478bd9Sstevel@tonic-gate 					if (matchname != NULL &&
29217c478bd9Sstevel@tonic-gate 					    strcmp(matchname, ifname) != 0 &&
29227c478bd9Sstevel@tonic-gate 					    strcmp(matchname, logintname) != 0)
29237c478bd9Sstevel@tonic-gate 						continue; /* 'for' loop 2d */
29247c478bd9Sstevel@tonic-gate 					new_ifindex =
29257c478bd9Sstevel@tonic-gate 					    if_nametoindex(logintname);
29267c478bd9Sstevel@tonic-gate 					if (new_ifindex != ifindex_v6 &&
29277c478bd9Sstevel@tonic-gate 					    (ksp = kstat_lookup(kc, NULL, -1,
29287c478bd9Sstevel@tonic-gate 					    ifname)) != NULL) {
29297c478bd9Sstevel@tonic-gate 						(void) safe_kstat_read(kc, ksp,
29307c478bd9Sstevel@tonic-gate 						    NULL);
29317c478bd9Sstevel@tonic-gate 						stat.ipackets =
29327c478bd9Sstevel@tonic-gate 						    kstat_named_value(ksp,
29337c478bd9Sstevel@tonic-gate 						    "ipackets");
29347c478bd9Sstevel@tonic-gate 						stat.ierrors =
29357c478bd9Sstevel@tonic-gate 						    kstat_named_value(ksp,
29367c478bd9Sstevel@tonic-gate 						    "ierrors");
29377c478bd9Sstevel@tonic-gate 						stat.opackets =
29387c478bd9Sstevel@tonic-gate 						    kstat_named_value(ksp,
29397c478bd9Sstevel@tonic-gate 						    "opackets");
29407c478bd9Sstevel@tonic-gate 						stat.oerrors =
29417c478bd9Sstevel@tonic-gate 						    kstat_named_value(ksp,
29427c478bd9Sstevel@tonic-gate 						    "oerrors");
29437c478bd9Sstevel@tonic-gate 						stat.collisions =
29447c478bd9Sstevel@tonic-gate 						    kstat_named_value(ksp,
29457c478bd9Sstevel@tonic-gate 						    "collisions");
29467c478bd9Sstevel@tonic-gate 						if (first) {
29477c478bd9Sstevel@tonic-gate 							(void) printf(
29487c478bd9Sstevel@tonic-gate 							    "%-5.5s %-5.5s%"
29497c478bd9Sstevel@tonic-gate 							    "-27.27s %-27.27s "
29507c478bd9Sstevel@tonic-gate 							    "%-6.6s %-5.5s "
29517c478bd9Sstevel@tonic-gate 							    "%-6.6s %-5.5s "
29527c478bd9Sstevel@tonic-gate 							    "%-6.6s\n",
29537c478bd9Sstevel@tonic-gate 							    "Name", "Mtu",
29547c478bd9Sstevel@tonic-gate 							    "Net/Dest",
29557c478bd9Sstevel@tonic-gate 							    "Address", "Ipkts",
29567c478bd9Sstevel@tonic-gate 							    "Ierrs", "Opkts",
29577c478bd9Sstevel@tonic-gate 							    "Oerrs", "Collis");
29587c478bd9Sstevel@tonic-gate 							first = B_FALSE;
29597c478bd9Sstevel@tonic-gate 						}
29607c478bd9Sstevel@tonic-gate 						if_report_ip6(ap6, ifname,
29617c478bd9Sstevel@tonic-gate 						    logintname, &stat, B_TRUE);
29627c478bd9Sstevel@tonic-gate 						ifindex_v6 = new_ifindex;
29637c478bd9Sstevel@tonic-gate 					} else {
29647c478bd9Sstevel@tonic-gate 						if_report_ip6(ap6, ifname,
29657c478bd9Sstevel@tonic-gate 						    logintname, &stat, B_FALSE);
29667c478bd9Sstevel@tonic-gate 					}
29677c478bd9Sstevel@tonic-gate 				} /* 'for' loop 2d ends */
29687c478bd9Sstevel@tonic-gate 				if (!first)
29697c478bd9Sstevel@tonic-gate 					(void) putchar('\n');
29707c478bd9Sstevel@tonic-gate 			} else if (!alreadydone) {
29717c478bd9Sstevel@tonic-gate 				char    ifname[LIFNAMSIZ + 1];
29727c478bd9Sstevel@tonic-gate 				char    buf[IFNAMSIZ + 1];
29737c478bd9Sstevel@tonic-gate 				mib2_ipv6AddrEntry_t *ap6;
29747c478bd9Sstevel@tonic-gate 				struct ifstat   t;
2975aecc8c24Sja 				struct iflist	*tlp = NULL;
29767c478bd9Sstevel@tonic-gate 				struct iflist	**nextnew = &newlist6;
29777c478bd9Sstevel@tonic-gate 				struct iflist	*walkold;
29787c478bd9Sstevel@tonic-gate 				struct iflist	*cleanlist;
2979aecc8c24Sja 				boolean_t	found_if = B_FALSE;
29807c478bd9Sstevel@tonic-gate 
29817c478bd9Sstevel@tonic-gate 				alreadydone = B_TRUE; /* ignore other case */
2982aecc8c24Sja 
29837c478bd9Sstevel@tonic-gate 				/*
2984aecc8c24Sja 				 * Check if there is anything to do.
2985aecc8c24Sja 				 */
2986aecc8c24Sja 				if (item->length <
2987aecc8c24Sja 				    sizeof (mib2_ipv6AddrEntry_t)) {
2988aecc8c24Sja 					fail(0, "No compatible interfaces");
2989aecc8c24Sja 				}
2990aecc8c24Sja 
2991aecc8c24Sja 				/*
2992aecc8c24Sja 				 * 'for' loop 2e: find the "right" entry:
2993aecc8c24Sja 				 * If an interface name to match has been
2994aecc8c24Sja 				 * supplied then try and find it, otherwise
2995aecc8c24Sja 				 * match the first non-loopback interface found.
2996aecc8c24Sja 				 * Use lo0 if all else fails.
29977c478bd9Sstevel@tonic-gate 				 */
29987c478bd9Sstevel@tonic-gate 				for (ap6 = (mib2_ipv6AddrEntry_t *)item->valp;
29997c478bd9Sstevel@tonic-gate 				    (char *)ap6 < (char *)item->valp
30007c478bd9Sstevel@tonic-gate 				    + item->length;
30017c478bd9Sstevel@tonic-gate 				    ap6++) {
30027c478bd9Sstevel@tonic-gate 					(void) octetstr(&ap6->ipv6AddrIfIndex,
30037c478bd9Sstevel@tonic-gate 					    'a', ifname, sizeof (ifname));
30047c478bd9Sstevel@tonic-gate 					(void) strtok(ifname, ":");
30057c478bd9Sstevel@tonic-gate 
30067c478bd9Sstevel@tonic-gate 					if (matchname) {
3007aecc8c24Sja 						if (strcmp(matchname,
3008aecc8c24Sja 						    ifname) == 0) {
30097c478bd9Sstevel@tonic-gate 							/* 'for' loop 2e */
3010aecc8c24Sja 							found_if = B_TRUE;
30117c478bd9Sstevel@tonic-gate 							break;
3012aecc8c24Sja 						}
3013aecc8c24Sja 					} else if (strcmp(ifname, "lo0") != 0)
30147c478bd9Sstevel@tonic-gate 						break; /* 'for' loop 2e */
30157c478bd9Sstevel@tonic-gate 				} /* 'for' loop 2e ends */
30167c478bd9Sstevel@tonic-gate 
3017aecc8c24Sja 				if (matchname == NULL) {
3018aecc8c24Sja 					matchname = ifname;
3019aecc8c24Sja 				} else {
3020aecc8c24Sja 					if (!found_if)
3021aecc8c24Sja 						fail(0, "-I: %s no such "
3022aecc8c24Sja 						    "interface.", matchname);
3023aecc8c24Sja 				}
3024aecc8c24Sja 
30257c478bd9Sstevel@tonic-gate 				if (Iflag_only == 0 || !reentry) {
30267c478bd9Sstevel@tonic-gate 					(void) printf(
30277c478bd9Sstevel@tonic-gate 					    "    input   %-6.6s"
30287c478bd9Sstevel@tonic-gate 					    "    output	",
30297c478bd9Sstevel@tonic-gate 					    matchname);
30307c478bd9Sstevel@tonic-gate 					(void) printf("   input  (Total)"
30317c478bd9Sstevel@tonic-gate 					    "    output\n");
30327c478bd9Sstevel@tonic-gate 					(void) printf("%-7.7s %-5.5s %-7.7s "
30337c478bd9Sstevel@tonic-gate 					    "%-5.5s %-6.6s ",
30347c478bd9Sstevel@tonic-gate 					    "packets", "errs", "packets",
30357c478bd9Sstevel@tonic-gate 					    "errs", "colls");
30367c478bd9Sstevel@tonic-gate 					(void) printf("%-7.7s %-5.5s %-7.7s "
30377c478bd9Sstevel@tonic-gate 					    "%-5.5s %-6.6s\n",
30387c478bd9Sstevel@tonic-gate 					    "packets", "errs", "packets",
30397c478bd9Sstevel@tonic-gate 					    "errs", "colls");
30407c478bd9Sstevel@tonic-gate 				}
30417c478bd9Sstevel@tonic-gate 
30427c478bd9Sstevel@tonic-gate 				sum6 = zerostat;
30437c478bd9Sstevel@tonic-gate 
30447c478bd9Sstevel@tonic-gate 				/* 'for' loop 2f: */
30457c478bd9Sstevel@tonic-gate 				for (ap6 = (mib2_ipv6AddrEntry_t *)item->valp;
30467c478bd9Sstevel@tonic-gate 				    (char *)ap6 < (char *)item->valp
30477c478bd9Sstevel@tonic-gate 				    + item->length;
30487c478bd9Sstevel@tonic-gate 				    ap6++) {
30497c478bd9Sstevel@tonic-gate 					(void) octetstr(&ap6->ipv6AddrIfIndex,
30507c478bd9Sstevel@tonic-gate 					    'a', buf, sizeof (buf));
30517c478bd9Sstevel@tonic-gate 					(void) strtok(buf, ":");
3052aecc8c24Sja 
3053aecc8c24Sja 					/*
3054aecc8c24Sja 					 * We have reduced the IP interface
3055aecc8c24Sja 					 * name, which could have been a
3056aecc8c24Sja 					 * logical, down to a name suitable
3057aecc8c24Sja 					 * for use with kstats.
3058aecc8c24Sja 					 * We treat this name as unique and
3059aecc8c24Sja 					 * only collate statistics for it once
3060aecc8c24Sja 					 * per pass. This is to avoid falsely
3061aecc8c24Sja 					 * amplifying these statistics by the
3062aecc8c24Sja 					 * the number of logical instances.
3063aecc8c24Sja 					 */
3064aecc8c24Sja 
3065aecc8c24Sja 					if ((tlp != NULL) &&
3066aecc8c24Sja 					    ((strcmp(buf, tlp->ifname) == 0))) {
3067aecc8c24Sja 						continue;
3068aecc8c24Sja 					}
3069aecc8c24Sja 
30707c478bd9Sstevel@tonic-gate 					ksp = kstat_lookup(kc, NULL, -1, buf);
30717c478bd9Sstevel@tonic-gate 					if (ksp && ksp->ks_type ==
30727c478bd9Sstevel@tonic-gate 					    KSTAT_TYPE_NAMED)
30737c478bd9Sstevel@tonic-gate 						(void) safe_kstat_read(kc,
30747c478bd9Sstevel@tonic-gate 						    ksp, NULL);
30757c478bd9Sstevel@tonic-gate 
30767c478bd9Sstevel@tonic-gate 					t.ipackets = kstat_named_value(ksp,
30777c478bd9Sstevel@tonic-gate 					    "ipackets");
30787c478bd9Sstevel@tonic-gate 					t.ierrors = kstat_named_value(ksp,
30797c478bd9Sstevel@tonic-gate 					    "ierrors");
30807c478bd9Sstevel@tonic-gate 					t.opackets = kstat_named_value(ksp,
30817c478bd9Sstevel@tonic-gate 					    "opackets");
30827c478bd9Sstevel@tonic-gate 					t.oerrors = kstat_named_value(ksp,
30837c478bd9Sstevel@tonic-gate 					    "oerrors");
30847c478bd9Sstevel@tonic-gate 					t.collisions = kstat_named_value(ksp,
30857c478bd9Sstevel@tonic-gate 					    "collisions");
30867c478bd9Sstevel@tonic-gate 
30877c478bd9Sstevel@tonic-gate 					if (strcmp(buf, matchname) == 0)
30887c478bd9Sstevel@tonic-gate 						new6 = t;
30897c478bd9Sstevel@tonic-gate 
30907c478bd9Sstevel@tonic-gate 					/* Build the interface list */
30917c478bd9Sstevel@tonic-gate 
30927c478bd9Sstevel@tonic-gate 					tlp = malloc(sizeof (struct iflist));
30937c478bd9Sstevel@tonic-gate 					(void) strlcpy(tlp->ifname, buf,
30947c478bd9Sstevel@tonic-gate 					    sizeof (tlp->ifname));
30957c478bd9Sstevel@tonic-gate 					tlp->tot = t;
30967c478bd9Sstevel@tonic-gate 					*nextnew = tlp;
30977c478bd9Sstevel@tonic-gate 					nextnew = &tlp->next_if;
30987c478bd9Sstevel@tonic-gate 
30997c478bd9Sstevel@tonic-gate 					/*
31007c478bd9Sstevel@tonic-gate 					 * First time through.
31017c478bd9Sstevel@tonic-gate 					 * Just add up the interface stats.
31027c478bd9Sstevel@tonic-gate 					 */
31037c478bd9Sstevel@tonic-gate 
31047c478bd9Sstevel@tonic-gate 					if (oldlist6 == NULL) {
31057c478bd9Sstevel@tonic-gate 						if_stat_total(&zerostat,
31067c478bd9Sstevel@tonic-gate 						    &t, &sum6);
31077c478bd9Sstevel@tonic-gate 						continue;
31087c478bd9Sstevel@tonic-gate 					}
31097c478bd9Sstevel@tonic-gate 
31107c478bd9Sstevel@tonic-gate 					/*
31117c478bd9Sstevel@tonic-gate 					 * Walk old list for the interface.
31127c478bd9Sstevel@tonic-gate 					 *
31137c478bd9Sstevel@tonic-gate 					 * If found, add difference to total.
31147c478bd9Sstevel@tonic-gate 					 *
31157c478bd9Sstevel@tonic-gate 					 * If not, an interface has been plumbed
31167c478bd9Sstevel@tonic-gate 					 * up.  In this case, we will simply
31177c478bd9Sstevel@tonic-gate 					 * ignore the new interface until the
31187c478bd9Sstevel@tonic-gate 					 * next interval; as there's no easy way
31197c478bd9Sstevel@tonic-gate 					 * to acquire statistics between time
31207c478bd9Sstevel@tonic-gate 					 * of the plumb and the next interval
31217c478bd9Sstevel@tonic-gate 					 * boundary.  This results in inaccurate
31227c478bd9Sstevel@tonic-gate 					 * total values for current interval.
31237c478bd9Sstevel@tonic-gate 					 *
31247c478bd9Sstevel@tonic-gate 					 * Note the case when an interface is
31257c478bd9Sstevel@tonic-gate 					 * unplumbed; as similar problems exist.
31267c478bd9Sstevel@tonic-gate 					 * The unplumbed interface is not in the
31277c478bd9Sstevel@tonic-gate 					 * current list, and there's no easy way
31287c478bd9Sstevel@tonic-gate 					 * to account for the statistics between
31297c478bd9Sstevel@tonic-gate 					 * the previous interval and time of the
31307c478bd9Sstevel@tonic-gate 					 * unplumb.  Therefore, we (in a sense)
31317c478bd9Sstevel@tonic-gate 					 * ignore the removed interface by only
31327c478bd9Sstevel@tonic-gate 					 * involving "current" interfaces when
31337c478bd9Sstevel@tonic-gate 					 * computing the total statistics.
31347c478bd9Sstevel@tonic-gate 					 * Unfortunately, this also results in
31357c478bd9Sstevel@tonic-gate 					 * inaccurate values for interval total.
31367c478bd9Sstevel@tonic-gate 					 */
31377c478bd9Sstevel@tonic-gate 
31387c478bd9Sstevel@tonic-gate 					for (walkold = oldlist6;
31397c478bd9Sstevel@tonic-gate 					    walkold != NULL;
31407c478bd9Sstevel@tonic-gate 					    walkold = walkold->next_if) {
31417c478bd9Sstevel@tonic-gate 						if (strcmp(walkold->ifname,
31427c478bd9Sstevel@tonic-gate 						    buf) == 0) {
31437c478bd9Sstevel@tonic-gate 							if_stat_total(
31447c478bd9Sstevel@tonic-gate 							    &walkold->tot,
31457c478bd9Sstevel@tonic-gate 							    &t, &sum6);
31467c478bd9Sstevel@tonic-gate 							break;
31477c478bd9Sstevel@tonic-gate 						}
31487c478bd9Sstevel@tonic-gate 					}
31497c478bd9Sstevel@tonic-gate 
31507c478bd9Sstevel@tonic-gate 				} /* 'for' loop 2f ends */
31517c478bd9Sstevel@tonic-gate 
31527c478bd9Sstevel@tonic-gate 				*nextnew = NULL;
31537c478bd9Sstevel@tonic-gate 
31547c478bd9Sstevel@tonic-gate 				(void) printf("%-7llu %-5llu %-7llu "
31557c478bd9Sstevel@tonic-gate 				    "%-5llu %-6llu ",
31567c478bd9Sstevel@tonic-gate 				    new6.ipackets - old6.ipackets,
31577c478bd9Sstevel@tonic-gate 				    new6.ierrors - old6.ierrors,
31587c478bd9Sstevel@tonic-gate 				    new6.opackets - old6.opackets,
31597c478bd9Sstevel@tonic-gate 				    new6.oerrors - old6.oerrors,
31607c478bd9Sstevel@tonic-gate 				    new6.collisions - old6.collisions);
31617c478bd9Sstevel@tonic-gate 
31627c478bd9Sstevel@tonic-gate 				(void) printf("%-7llu %-5llu %-7llu "
31637c478bd9Sstevel@tonic-gate 				    "%-5llu %-6llu\n", sum6.ipackets,
31647c478bd9Sstevel@tonic-gate 				    sum6.ierrors, sum6.opackets,
31657c478bd9Sstevel@tonic-gate 				    sum6.oerrors, sum6.collisions);
31667c478bd9Sstevel@tonic-gate 
31677c478bd9Sstevel@tonic-gate 				/*
31687c478bd9Sstevel@tonic-gate 				 * Tidy things up once finished.
31697c478bd9Sstevel@tonic-gate 				 */
31707c478bd9Sstevel@tonic-gate 
31717c478bd9Sstevel@tonic-gate 				old6 = new6;
31727c478bd9Sstevel@tonic-gate 				cleanlist = oldlist6;
31737c478bd9Sstevel@tonic-gate 				oldlist6 = newlist6;
31747c478bd9Sstevel@tonic-gate 				while (cleanlist != NULL) {
31757c478bd9Sstevel@tonic-gate 					tlp = cleanlist->next_if;
31767c478bd9Sstevel@tonic-gate 					free(cleanlist);
31777c478bd9Sstevel@tonic-gate 					cleanlist = tlp;
31787c478bd9Sstevel@tonic-gate 				}
31797c478bd9Sstevel@tonic-gate 			}
31807c478bd9Sstevel@tonic-gate 			break;
31817c478bd9Sstevel@tonic-gate 		}
31827c478bd9Sstevel@tonic-gate 		}
31837c478bd9Sstevel@tonic-gate 		if (Iflag_only == 0)
31847c478bd9Sstevel@tonic-gate 		    (void) putchar('\n');
31857c478bd9Sstevel@tonic-gate 		(void) fflush(stdout);
31867c478bd9Sstevel@tonic-gate 	} /* 'for' loop 1 ends */
31877c478bd9Sstevel@tonic-gate 	reentry = B_TRUE;
31887c478bd9Sstevel@tonic-gate }
31897c478bd9Sstevel@tonic-gate 
31907c478bd9Sstevel@tonic-gate static void
31917c478bd9Sstevel@tonic-gate if_report_ip4(mib2_ipAddrEntry_t *ap,
31927c478bd9Sstevel@tonic-gate 	char ifname[], char logintname[], struct ifstat *statptr,
31937c478bd9Sstevel@tonic-gate 	boolean_t ksp_not_null) {
31947c478bd9Sstevel@tonic-gate 
31957c478bd9Sstevel@tonic-gate 	char abuf[MAXHOSTNAMELEN + 1];
31967c478bd9Sstevel@tonic-gate 	char dstbuf[MAXHOSTNAMELEN + 1];
31977c478bd9Sstevel@tonic-gate 
31987c478bd9Sstevel@tonic-gate 	if (ksp_not_null) {
31997c478bd9Sstevel@tonic-gate 		(void) printf("%-5s %-5u",
32007c478bd9Sstevel@tonic-gate 		    ifname, ap->ipAdEntInfo.ae_mtu);
32017c478bd9Sstevel@tonic-gate 		if (ap->ipAdEntInfo.ae_flags & IFF_POINTOPOINT)
32027c478bd9Sstevel@tonic-gate 			(void) pr_addr(ap->ipAdEntInfo.ae_pp_dst_addr,
32037c478bd9Sstevel@tonic-gate 			    abuf, sizeof (abuf));
32047c478bd9Sstevel@tonic-gate 		else
32057c478bd9Sstevel@tonic-gate 			(void) pr_netaddr(ap->ipAdEntAddr,
32067c478bd9Sstevel@tonic-gate 			    ap->ipAdEntNetMask, abuf, sizeof (abuf));
32077c478bd9Sstevel@tonic-gate 		(void) printf("%-13s %-14s %-6llu %-5llu %-6llu %-5llu "
32087c478bd9Sstevel@tonic-gate 		    "%-6llu %-6llu\n",
32097c478bd9Sstevel@tonic-gate 		    abuf, pr_addr(ap->ipAdEntAddr, dstbuf, sizeof (dstbuf)),
32107c478bd9Sstevel@tonic-gate 		    statptr->ipackets, statptr->ierrors,
32117c478bd9Sstevel@tonic-gate 		    statptr->opackets, statptr->oerrors,
32127c478bd9Sstevel@tonic-gate 		    statptr->collisions, 0LL);
32137c478bd9Sstevel@tonic-gate 	}
32147c478bd9Sstevel@tonic-gate 	/*
32157c478bd9Sstevel@tonic-gate 	 * Print logical interface info if Aflag set (including logical unit 0)
32167c478bd9Sstevel@tonic-gate 	 */
32177c478bd9Sstevel@tonic-gate 	if (Aflag) {
32187c478bd9Sstevel@tonic-gate 		*statptr = zerostat;
32197c478bd9Sstevel@tonic-gate 		statptr->ipackets = ap->ipAdEntInfo.ae_ibcnt;
32207c478bd9Sstevel@tonic-gate 		statptr->opackets = ap->ipAdEntInfo.ae_obcnt;
32217c478bd9Sstevel@tonic-gate 
32227c478bd9Sstevel@tonic-gate 		(void) printf("%-5s %-5u", logintname, ap->ipAdEntInfo.ae_mtu);
32237c478bd9Sstevel@tonic-gate 		if (ap->ipAdEntInfo.ae_flags & IFF_POINTOPOINT)
32247c478bd9Sstevel@tonic-gate 			(void) pr_addr(ap->ipAdEntInfo.ae_pp_dst_addr, abuf,
32257c478bd9Sstevel@tonic-gate 			sizeof (abuf));
32267c478bd9Sstevel@tonic-gate 		else
32277c478bd9Sstevel@tonic-gate 			(void) pr_netaddr(ap->ipAdEntAddr, ap->ipAdEntNetMask,
32287c478bd9Sstevel@tonic-gate 			    abuf, sizeof (abuf));
32297c478bd9Sstevel@tonic-gate 
32307c478bd9Sstevel@tonic-gate 		(void) printf("%-13s %-14s %-6llu %-5s %-6llu "
32317c478bd9Sstevel@tonic-gate 		    "%-5s %-6s %-6llu\n", abuf,
32327c478bd9Sstevel@tonic-gate 		    pr_addr(ap->ipAdEntAddr, dstbuf, sizeof (dstbuf)),
32337c478bd9Sstevel@tonic-gate 		    statptr->ipackets, "N/A", statptr->opackets, "N/A", "N/A",
32347c478bd9Sstevel@tonic-gate 		    0LL);
32357c478bd9Sstevel@tonic-gate 	}
32367c478bd9Sstevel@tonic-gate }
32377c478bd9Sstevel@tonic-gate 
32387c478bd9Sstevel@tonic-gate static void
32397c478bd9Sstevel@tonic-gate if_report_ip6(mib2_ipv6AddrEntry_t *ap6,
32407c478bd9Sstevel@tonic-gate 	char ifname[], char logintname[], struct ifstat *statptr,
32417c478bd9Sstevel@tonic-gate 	boolean_t ksp_not_null) {
32427c478bd9Sstevel@tonic-gate 
32437c478bd9Sstevel@tonic-gate 	char abuf[MAXHOSTNAMELEN + 1];
32447c478bd9Sstevel@tonic-gate 	char dstbuf[MAXHOSTNAMELEN + 1];
32457c478bd9Sstevel@tonic-gate 
32467c478bd9Sstevel@tonic-gate 	if (ksp_not_null) {
32477c478bd9Sstevel@tonic-gate 		(void) printf("%-5s %-5u", ifname, ap6->ipv6AddrInfo.ae_mtu);
32487c478bd9Sstevel@tonic-gate 		if (ap6->ipv6AddrInfo.ae_flags &
32497c478bd9Sstevel@tonic-gate 		    IFF_POINTOPOINT) {
32507c478bd9Sstevel@tonic-gate 			(void) pr_addr6(&ap6->ipv6AddrInfo.ae_pp_dst_addr,
32517c478bd9Sstevel@tonic-gate 			    abuf, sizeof (abuf));
32527c478bd9Sstevel@tonic-gate 		} else {
32537c478bd9Sstevel@tonic-gate 			(void) pr_prefix6(&ap6->ipv6AddrAddress,
32547c478bd9Sstevel@tonic-gate 			    ap6->ipv6AddrPfxLength, abuf,
32557c478bd9Sstevel@tonic-gate 			    sizeof (abuf));
32567c478bd9Sstevel@tonic-gate 		}
32577c478bd9Sstevel@tonic-gate 		(void) printf("%-27s %-27s %-6llu %-5llu "
32587c478bd9Sstevel@tonic-gate 		    "%-6llu %-5llu %-6llu\n",
32597c478bd9Sstevel@tonic-gate 		    abuf, pr_addr6(&ap6->ipv6AddrAddress, dstbuf,
32607c478bd9Sstevel@tonic-gate 		    sizeof (dstbuf)),
32617c478bd9Sstevel@tonic-gate 		    statptr->ipackets, statptr->ierrors, statptr->opackets,
32627c478bd9Sstevel@tonic-gate 		    statptr->oerrors, statptr->collisions);
32637c478bd9Sstevel@tonic-gate 	}
32647c478bd9Sstevel@tonic-gate 	/*
32657c478bd9Sstevel@tonic-gate 	 * Print logical interface info if Aflag set (including logical unit 0)
32667c478bd9Sstevel@tonic-gate 	 */
32677c478bd9Sstevel@tonic-gate 	if (Aflag) {
32687c478bd9Sstevel@tonic-gate 		*statptr = zerostat;
32697c478bd9Sstevel@tonic-gate 		statptr->ipackets = ap6->ipv6AddrInfo.ae_ibcnt;
32707c478bd9Sstevel@tonic-gate 		statptr->opackets = ap6->ipv6AddrInfo.ae_obcnt;
32717c478bd9Sstevel@tonic-gate 
32727c478bd9Sstevel@tonic-gate 		(void) printf("%-5s %-5u", logintname,
32737c478bd9Sstevel@tonic-gate 		    ap6->ipv6AddrInfo.ae_mtu);
32747c478bd9Sstevel@tonic-gate 		if (ap6->ipv6AddrInfo.ae_flags & IFF_POINTOPOINT)
32757c478bd9Sstevel@tonic-gate 			(void) pr_addr6(&ap6->ipv6AddrInfo.ae_pp_dst_addr,
32767c478bd9Sstevel@tonic-gate 			    abuf, sizeof (abuf));
32777c478bd9Sstevel@tonic-gate 		else
32787c478bd9Sstevel@tonic-gate 			(void) pr_prefix6(&ap6->ipv6AddrAddress,
32797c478bd9Sstevel@tonic-gate 			    ap6->ipv6AddrPfxLength, abuf, sizeof (abuf));
32807c478bd9Sstevel@tonic-gate 		(void) printf("%-27s %-27s %-6llu %-5s %-6llu %-5s %-6s\n",
32817c478bd9Sstevel@tonic-gate 		    abuf, pr_addr6(&ap6->ipv6AddrAddress, dstbuf,
32827c478bd9Sstevel@tonic-gate 		    sizeof (dstbuf)),
32837c478bd9Sstevel@tonic-gate 		    statptr->ipackets, "N/A",
32847c478bd9Sstevel@tonic-gate 		    statptr->opackets, "N/A", "N/A");
32857c478bd9Sstevel@tonic-gate 	}
32867c478bd9Sstevel@tonic-gate }
32877c478bd9Sstevel@tonic-gate 
32887c478bd9Sstevel@tonic-gate /* --------------------- DHCP_REPORT  (netstat -D) ------------------------- */
32897c478bd9Sstevel@tonic-gate 
3290d04ccbb3Scarlsonj static boolean_t
3291d04ccbb3Scarlsonj dhcp_do_ipc(dhcp_ipc_type_t type, const char *ifname, boolean_t printed_one)
32927c478bd9Sstevel@tonic-gate {
32937c478bd9Sstevel@tonic-gate 	dhcp_ipc_request_t	*request;
32947c478bd9Sstevel@tonic-gate 	dhcp_ipc_reply_t	*reply;
32957c478bd9Sstevel@tonic-gate 	int			error;
32967c478bd9Sstevel@tonic-gate 
32977c478bd9Sstevel@tonic-gate 	request = dhcp_ipc_alloc_request(type, ifname, NULL, 0, DHCP_TYPE_NONE);
32987c478bd9Sstevel@tonic-gate 	if (request == NULL)
32997c478bd9Sstevel@tonic-gate 		fail(0, "dhcp_do_ipc: out of memory");
33007c478bd9Sstevel@tonic-gate 
33017c478bd9Sstevel@tonic-gate 	error = dhcp_ipc_make_request(request, &reply, DHCP_IPC_WAIT_DEFAULT);
33027c478bd9Sstevel@tonic-gate 	if (error != 0) {
33037c478bd9Sstevel@tonic-gate 		free(request);
33047c478bd9Sstevel@tonic-gate 		fail(0, "dhcp_do_ipc: %s", dhcp_ipc_strerror(error));
33057c478bd9Sstevel@tonic-gate 	}
33067c478bd9Sstevel@tonic-gate 
33077c478bd9Sstevel@tonic-gate 	free(request);
33087c478bd9Sstevel@tonic-gate 	error = reply->return_code;
3309d04ccbb3Scarlsonj 	if (error == DHCP_IPC_E_UNKIF) {
3310d04ccbb3Scarlsonj 		free(reply);
3311d04ccbb3Scarlsonj 		return (printed_one);
3312d04ccbb3Scarlsonj 	}
33137c478bd9Sstevel@tonic-gate 	if (error != 0) {
33147c478bd9Sstevel@tonic-gate 		free(reply);
33157c478bd9Sstevel@tonic-gate 		fail(0, "dhcp_do_ipc: %s", dhcp_ipc_strerror(error));
33167c478bd9Sstevel@tonic-gate 	}
33177c478bd9Sstevel@tonic-gate 
3318d04ccbb3Scarlsonj 	if (!printed_one)
3319d04ccbb3Scarlsonj 		(void) printf("%s", dhcp_status_hdr_string());
3320d04ccbb3Scarlsonj 
3321d04ccbb3Scarlsonj 	(void) printf("%s", dhcp_status_reply_to_string(reply));
3322d04ccbb3Scarlsonj 	free(reply);
3323d04ccbb3Scarlsonj 	return (B_TRUE);
33247c478bd9Sstevel@tonic-gate }
33257c478bd9Sstevel@tonic-gate 
33267c478bd9Sstevel@tonic-gate /*
3327d04ccbb3Scarlsonj  * dhcp_walk_interfaces: walk the list of interfaces that have a given set of
3328d04ccbb3Scarlsonj  * flags turned on (flags_on) and a given set turned off (flags_off) for a
3329d04ccbb3Scarlsonj  * given address family (af).  For each, print out the DHCP status using
3330d04ccbb3Scarlsonj  * dhcp_do_ipc.
33317c478bd9Sstevel@tonic-gate  */
3332d04ccbb3Scarlsonj static boolean_t
3333d04ccbb3Scarlsonj dhcp_walk_interfaces(uint_t flags_on, uint_t flags_off, int af,
3334d04ccbb3Scarlsonj     boolean_t printed_one)
33357c478bd9Sstevel@tonic-gate {
3336d04ccbb3Scarlsonj 	struct lifnum	lifn;
3337d04ccbb3Scarlsonj 	struct lifconf	lifc;
33387c478bd9Sstevel@tonic-gate 	int		n_ifs, i, sock_fd;
33397c478bd9Sstevel@tonic-gate 
3340d04ccbb3Scarlsonj 	sock_fd = socket(af, SOCK_DGRAM, 0);
33417c478bd9Sstevel@tonic-gate 	if (sock_fd == -1)
3342d04ccbb3Scarlsonj 		return (printed_one);
33437c478bd9Sstevel@tonic-gate 
3344d04ccbb3Scarlsonj 	/*
3345d04ccbb3Scarlsonj 	 * SIOCGLIFNUM is just an estimate.  If the ioctl fails, we don't care;
3346d04ccbb3Scarlsonj 	 * just drive on and use SIOCGLIFCONF with increasing buffer sizes, as
3347d04ccbb3Scarlsonj 	 * is traditional.
3348d04ccbb3Scarlsonj 	 */
3349d04ccbb3Scarlsonj 	(void) memset(&lifn, 0, sizeof (lifn));
3350d04ccbb3Scarlsonj 	lifn.lifn_family = af;
3351d04ccbb3Scarlsonj 	lifn.lifn_flags = LIFC_ALLZONES | LIFC_NOXMIT;
3352d04ccbb3Scarlsonj 	if (ioctl(sock_fd, SIOCGLIFNUM, &lifn) == -1)
3353d04ccbb3Scarlsonj 		n_ifs = LIFN_GUARD_VALUE;
3354d04ccbb3Scarlsonj 	else
3355d04ccbb3Scarlsonj 		n_ifs = lifn.lifn_count + LIFN_GUARD_VALUE;
3356d04ccbb3Scarlsonj 
3357d04ccbb3Scarlsonj 	(void) memset(&lifc, 0, sizeof (lifc));
3358d04ccbb3Scarlsonj 	lifc.lifc_family = af;
3359d04ccbb3Scarlsonj 	lifc.lifc_flags = lifn.lifn_flags;
3360d04ccbb3Scarlsonj 	lifc.lifc_len = n_ifs * sizeof (struct lifreq);
3361d04ccbb3Scarlsonj 	lifc.lifc_buf = malloc(lifc.lifc_len);
3362d04ccbb3Scarlsonj 	if (lifc.lifc_buf != NULL) {
3363d04ccbb3Scarlsonj 
3364d04ccbb3Scarlsonj 		if (ioctl(sock_fd, SIOCGLIFCONF, &lifc) == -1) {
33657c478bd9Sstevel@tonic-gate 			(void) close(sock_fd);
3366d04ccbb3Scarlsonj 			free(lifc.lifc_buf);
33677c478bd9Sstevel@tonic-gate 			return (NULL);
33687c478bd9Sstevel@tonic-gate 		}
33697c478bd9Sstevel@tonic-gate 
3370d04ccbb3Scarlsonj 		n_ifs = lifc.lifc_len / sizeof (struct lifreq);
33717c478bd9Sstevel@tonic-gate 
3372d04ccbb3Scarlsonj 		for (i = 0; i < n_ifs; i++) {
3373d04ccbb3Scarlsonj 			if (ioctl(sock_fd, SIOCGLIFFLAGS, &lifc.lifc_req[i]) ==
3374d04ccbb3Scarlsonj 			    0 && (lifc.lifc_req[i].lifr_flags & (flags_on |
3375d04ccbb3Scarlsonj 			    flags_off)) != flags_on)
3376d04ccbb3Scarlsonj 				continue;
3377d04ccbb3Scarlsonj 			printed_one = dhcp_do_ipc(DHCP_STATUS |
3378d04ccbb3Scarlsonj 			    (af == AF_INET6 ? DHCP_V6 : 0),
3379d04ccbb3Scarlsonj 			    lifc.lifc_req[i].lifr_name, printed_one);
3380d04ccbb3Scarlsonj 		}
33817c478bd9Sstevel@tonic-gate 	}
33827c478bd9Sstevel@tonic-gate 	(void) close(sock_fd);
3383d04ccbb3Scarlsonj 	free(lifc.lifc_buf);
3384d04ccbb3Scarlsonj 	return (printed_one);
33857c478bd9Sstevel@tonic-gate }
33867c478bd9Sstevel@tonic-gate 
33877c478bd9Sstevel@tonic-gate static void
33887c478bd9Sstevel@tonic-gate dhcp_report(char *ifname)
33897c478bd9Sstevel@tonic-gate {
3390d04ccbb3Scarlsonj 	boolean_t printed_one;
33917c478bd9Sstevel@tonic-gate 
3392d04ccbb3Scarlsonj 	if (!family_selected(AF_INET) && !family_selected(AF_INET6))
33937c478bd9Sstevel@tonic-gate 		return;
33947c478bd9Sstevel@tonic-gate 
3395d04ccbb3Scarlsonj 	printed_one = B_FALSE;
3396d04ccbb3Scarlsonj 	if (ifname != NULL) {
3397d04ccbb3Scarlsonj 		if (family_selected(AF_INET)) {
3398d04ccbb3Scarlsonj 			printed_one = dhcp_do_ipc(DHCP_STATUS, ifname,
3399d04ccbb3Scarlsonj 			    printed_one);
3400d04ccbb3Scarlsonj 		}
3401d04ccbb3Scarlsonj 		if (family_selected(AF_INET6)) {
3402d04ccbb3Scarlsonj 			printed_one = dhcp_do_ipc(DHCP_STATUS | DHCP_V6,
3403d04ccbb3Scarlsonj 			    ifname, printed_one);
3404d04ccbb3Scarlsonj 		}
3405d04ccbb3Scarlsonj 		if (!printed_one) {
3406d04ccbb3Scarlsonj 			fail(0, "%s: %s", ifname,
3407d04ccbb3Scarlsonj 			    dhcp_ipc_strerror(DHCP_IPC_E_UNKIF));
3408d04ccbb3Scarlsonj 		}
3409d04ccbb3Scarlsonj 	} else {
3410d04ccbb3Scarlsonj 		if (family_selected(AF_INET)) {
3411d04ccbb3Scarlsonj 			printed_one = dhcp_walk_interfaces(IFF_DHCPRUNNING,
3412d04ccbb3Scarlsonj 			    0, AF_INET, printed_one);
3413d04ccbb3Scarlsonj 		}
3414d04ccbb3Scarlsonj 		if (family_selected(AF_INET6)) {
3415d04ccbb3Scarlsonj 			(void) dhcp_walk_interfaces(IFF_DHCPRUNNING,
3416d04ccbb3Scarlsonj 			    IFF_ADDRCONF, AF_INET6, printed_one);
3417d04ccbb3Scarlsonj 		}
34187c478bd9Sstevel@tonic-gate 	}
34197c478bd9Sstevel@tonic-gate }
34207c478bd9Sstevel@tonic-gate 
34217c478bd9Sstevel@tonic-gate /* --------------------- GROUP_REPORT (netstat -g) ------------------------- */
34227c478bd9Sstevel@tonic-gate 
34237c478bd9Sstevel@tonic-gate static void
34247c478bd9Sstevel@tonic-gate group_report(mib_item_t *item)
34257c478bd9Sstevel@tonic-gate {
34267c478bd9Sstevel@tonic-gate 	mib_item_t	*v4grp = NULL, *v4src = NULL;
34277c478bd9Sstevel@tonic-gate 	mib_item_t	*v6grp = NULL, *v6src = NULL;
34287c478bd9Sstevel@tonic-gate 	int		jtemp = 0;
34297c478bd9Sstevel@tonic-gate 	char		ifname[LIFNAMSIZ + 1];
34307c478bd9Sstevel@tonic-gate 	char		abuf[MAXHOSTNAMELEN + 1];
34317c478bd9Sstevel@tonic-gate 	ip_member_t	*ipmp;
34327c478bd9Sstevel@tonic-gate 	ip_grpsrc_t	*ips;
34337c478bd9Sstevel@tonic-gate 	ipv6_member_t	*ipmp6;
34347c478bd9Sstevel@tonic-gate 	ipv6_grpsrc_t	*ips6;
34357c478bd9Sstevel@tonic-gate 	char		*ifnamep;
34367c478bd9Sstevel@tonic-gate 	boolean_t	first, first_src;
34377c478bd9Sstevel@tonic-gate 
34387c478bd9Sstevel@tonic-gate 	/* 'for' loop 1: */
34397c478bd9Sstevel@tonic-gate 	for (; item; item = item->next_item) {
34407c478bd9Sstevel@tonic-gate 		if (Dflag) {
34417c478bd9Sstevel@tonic-gate 			(void) printf("\n--- Entry %d ---\n", ++jtemp);
34427c478bd9Sstevel@tonic-gate 			(void) printf("Group = %d, mib_id = %d, "
34437c478bd9Sstevel@tonic-gate 			    "length = %d, valp = 0x%p\n",
34447c478bd9Sstevel@tonic-gate 			    item->group, item->mib_id, item->length,
34457c478bd9Sstevel@tonic-gate 			    item->valp);
34467c478bd9Sstevel@tonic-gate 		}
34477c478bd9Sstevel@tonic-gate 		if (item->group == MIB2_IP && family_selected(AF_INET)) {
34487c478bd9Sstevel@tonic-gate 			switch (item->mib_id) {
34497c478bd9Sstevel@tonic-gate 			case EXPER_IP_GROUP_MEMBERSHIP:
34507c478bd9Sstevel@tonic-gate 				v4grp = item;
34517c478bd9Sstevel@tonic-gate 				if (Dflag)
34527c478bd9Sstevel@tonic-gate 					(void) printf("item is v4grp info\n");
34537c478bd9Sstevel@tonic-gate 				break;
34547c478bd9Sstevel@tonic-gate 			case EXPER_IP_GROUP_SOURCES:
34557c478bd9Sstevel@tonic-gate 				v4src = item;
34567c478bd9Sstevel@tonic-gate 				if (Dflag)
34577c478bd9Sstevel@tonic-gate 					(void) printf("item is v4src info\n");
34587c478bd9Sstevel@tonic-gate 				break;
34597c478bd9Sstevel@tonic-gate 			default:
34607c478bd9Sstevel@tonic-gate 				continue;
34617c478bd9Sstevel@tonic-gate 			}
34627c478bd9Sstevel@tonic-gate 			continue;
34637c478bd9Sstevel@tonic-gate 		}
34647c478bd9Sstevel@tonic-gate 		if (item->group == MIB2_IP6 && family_selected(AF_INET6)) {
34657c478bd9Sstevel@tonic-gate 			switch (item->mib_id) {
34667c478bd9Sstevel@tonic-gate 			case EXPER_IP6_GROUP_MEMBERSHIP:
34677c478bd9Sstevel@tonic-gate 				v6grp = item;
34687c478bd9Sstevel@tonic-gate 				if (Dflag)
34697c478bd9Sstevel@tonic-gate 					(void) printf("item is v6grp info\n");
34707c478bd9Sstevel@tonic-gate 				break;
34717c478bd9Sstevel@tonic-gate 			case EXPER_IP6_GROUP_SOURCES:
34727c478bd9Sstevel@tonic-gate 				v6src = item;
34737c478bd9Sstevel@tonic-gate 				if (Dflag)
34747c478bd9Sstevel@tonic-gate 					(void) printf("item is v6src info\n");
34757c478bd9Sstevel@tonic-gate 				break;
34767c478bd9Sstevel@tonic-gate 			default:
34777c478bd9Sstevel@tonic-gate 				continue;
34787c478bd9Sstevel@tonic-gate 			}
34797c478bd9Sstevel@tonic-gate 		}
34807c478bd9Sstevel@tonic-gate 	}
34817c478bd9Sstevel@tonic-gate 
34827c478bd9Sstevel@tonic-gate 	if (family_selected(AF_INET) && v4grp != NULL) {
34837c478bd9Sstevel@tonic-gate 		if (Dflag)
34847c478bd9Sstevel@tonic-gate 			(void) printf("%u records for ipGroupMember:\n",
34857c478bd9Sstevel@tonic-gate 			    v4grp->length / sizeof (ip_member_t));
34867c478bd9Sstevel@tonic-gate 
34877c478bd9Sstevel@tonic-gate 		first = B_TRUE;
34887c478bd9Sstevel@tonic-gate 		for (ipmp = (ip_member_t *)v4grp->valp;
34897c478bd9Sstevel@tonic-gate 		    (char *)ipmp < (char *)v4grp->valp + v4grp->length;
34907c478bd9Sstevel@tonic-gate 		    /* LINTED: (note 1) */
34917c478bd9Sstevel@tonic-gate 		    ipmp = (ip_member_t *)((char *)ipmp + ipMemberEntrySize)) {
34927c478bd9Sstevel@tonic-gate 			if (first) {
34937c478bd9Sstevel@tonic-gate 				(void) puts(v4compat ?
34947c478bd9Sstevel@tonic-gate 				    "Group Memberships" :
34957c478bd9Sstevel@tonic-gate 				    "Group Memberships: IPv4");
34967c478bd9Sstevel@tonic-gate 				(void) puts("Interface "
34977c478bd9Sstevel@tonic-gate 				    "Group                RefCnt");
34987c478bd9Sstevel@tonic-gate 				(void) puts("--------- "
34997c478bd9Sstevel@tonic-gate 				    "-------------------- ------");
35007c478bd9Sstevel@tonic-gate 				first = B_FALSE;
35017c478bd9Sstevel@tonic-gate 			}
35027c478bd9Sstevel@tonic-gate 
35037c478bd9Sstevel@tonic-gate 			(void) printf("%-9s %-20s %6u\n",
35047c478bd9Sstevel@tonic-gate 			    octetstr(&ipmp->ipGroupMemberIfIndex, 'a',
35057c478bd9Sstevel@tonic-gate 			    ifname, sizeof (ifname)),
35067c478bd9Sstevel@tonic-gate 			    pr_addr(ipmp->ipGroupMemberAddress,
35077c478bd9Sstevel@tonic-gate 			    abuf, sizeof (abuf)),
35087c478bd9Sstevel@tonic-gate 			    ipmp->ipGroupMemberRefCnt);
35097c478bd9Sstevel@tonic-gate 
35107c478bd9Sstevel@tonic-gate 
35117c478bd9Sstevel@tonic-gate 			if (!Vflag || v4src == NULL)
35127c478bd9Sstevel@tonic-gate 				continue;
35137c478bd9Sstevel@tonic-gate 
35147c478bd9Sstevel@tonic-gate 			if (Dflag)
35157c478bd9Sstevel@tonic-gate 				(void) printf("scanning %u ipGroupSource "
35167c478bd9Sstevel@tonic-gate 				    "records...\n",
35177c478bd9Sstevel@tonic-gate 				    v4src->length/sizeof (ip_grpsrc_t));
35187c478bd9Sstevel@tonic-gate 
35197c478bd9Sstevel@tonic-gate 			first_src = B_TRUE;
35207c478bd9Sstevel@tonic-gate 			for (ips = (ip_grpsrc_t *)v4src->valp;
35217c478bd9Sstevel@tonic-gate 			    (char *)ips < (char *)v4src->valp + v4src->length;
35227c478bd9Sstevel@tonic-gate 			    /* LINTED: (note 1) */
35237c478bd9Sstevel@tonic-gate 			    ips = (ip_grpsrc_t *)((char *)ips +
35247c478bd9Sstevel@tonic-gate 			    ipGroupSourceEntrySize)) {
35257c478bd9Sstevel@tonic-gate 				/*
35267c478bd9Sstevel@tonic-gate 				 * We assume that all source addrs for a given
35277c478bd9Sstevel@tonic-gate 				 * interface/group pair are contiguous, so on
35287c478bd9Sstevel@tonic-gate 				 * the first non-match after we've found at
35297c478bd9Sstevel@tonic-gate 				 * least one, we bail.
35307c478bd9Sstevel@tonic-gate 				 */
35317c478bd9Sstevel@tonic-gate 				if ((ipmp->ipGroupMemberAddress !=
35327c478bd9Sstevel@tonic-gate 				    ips->ipGroupSourceGroup) ||
35337c478bd9Sstevel@tonic-gate 				    (!octetstrmatch(&ipmp->ipGroupMemberIfIndex,
35347c478bd9Sstevel@tonic-gate 				    &ips->ipGroupSourceIfIndex))) {
35357c478bd9Sstevel@tonic-gate 					if (first_src)
35367c478bd9Sstevel@tonic-gate 						continue;
35377c478bd9Sstevel@tonic-gate 					else
35387c478bd9Sstevel@tonic-gate 						break;
35397c478bd9Sstevel@tonic-gate 				}
35407c478bd9Sstevel@tonic-gate 				if (first_src) {
35417c478bd9Sstevel@tonic-gate 					(void) printf("\t%s:    %s\n",
35427c478bd9Sstevel@tonic-gate 					    fmodestr(
35437c478bd9Sstevel@tonic-gate 					    ipmp->ipGroupMemberFilterMode),
35447c478bd9Sstevel@tonic-gate 					    pr_addr(ips->ipGroupSourceAddress,
35457c478bd9Sstevel@tonic-gate 					    abuf, sizeof (abuf)));
35467c478bd9Sstevel@tonic-gate 					first_src = B_FALSE;
35477c478bd9Sstevel@tonic-gate 					continue;
35487c478bd9Sstevel@tonic-gate 				}
35497c478bd9Sstevel@tonic-gate 
35507c478bd9Sstevel@tonic-gate 				(void) printf("\t            %s\n",
35517c478bd9Sstevel@tonic-gate 				    pr_addr(ips->ipGroupSourceAddress, abuf,
35527c478bd9Sstevel@tonic-gate 				    sizeof (abuf)));
35537c478bd9Sstevel@tonic-gate 			}
35547c478bd9Sstevel@tonic-gate 		}
35557c478bd9Sstevel@tonic-gate 		(void) putchar('\n');
35567c478bd9Sstevel@tonic-gate 	}
35577c478bd9Sstevel@tonic-gate 
35587c478bd9Sstevel@tonic-gate 	if (family_selected(AF_INET6) && v6grp != NULL) {
35597c478bd9Sstevel@tonic-gate 		if (Dflag)
35607c478bd9Sstevel@tonic-gate 			(void) printf("%u records for ipv6GroupMember:\n",
35617c478bd9Sstevel@tonic-gate 			    v6grp->length / sizeof (ipv6_member_t));
35627c478bd9Sstevel@tonic-gate 
35637c478bd9Sstevel@tonic-gate 		first = B_TRUE;
35647c478bd9Sstevel@tonic-gate 		for (ipmp6 = (ipv6_member_t *)v6grp->valp;
35657c478bd9Sstevel@tonic-gate 		    (char *)ipmp6 < (char *)v6grp->valp + v6grp->length;
35667c478bd9Sstevel@tonic-gate 		    /* LINTED: (note 1) */
35677c478bd9Sstevel@tonic-gate 		    ipmp6 = (ipv6_member_t *)((char *)ipmp6 +
35687c478bd9Sstevel@tonic-gate 			ipv6MemberEntrySize)) {
35697c478bd9Sstevel@tonic-gate 			if (first) {
35707c478bd9Sstevel@tonic-gate 				(void) puts("Group Memberships: "
35717c478bd9Sstevel@tonic-gate 				    "IPv6");
35727c478bd9Sstevel@tonic-gate 				(void) puts(" If       "
35737c478bd9Sstevel@tonic-gate 				    "Group                   RefCnt");
35747c478bd9Sstevel@tonic-gate 				(void) puts("----- "
35757c478bd9Sstevel@tonic-gate 				    "--------------------------- ------");
35767c478bd9Sstevel@tonic-gate 				first = B_FALSE;
35777c478bd9Sstevel@tonic-gate 			}
35787c478bd9Sstevel@tonic-gate 
35797c478bd9Sstevel@tonic-gate 			ifnamep = if_indextoname(
35807c478bd9Sstevel@tonic-gate 			    ipmp6->ipv6GroupMemberIfIndex, ifname);
35817c478bd9Sstevel@tonic-gate 			if (ifnamep == NULL) {
35827c478bd9Sstevel@tonic-gate 				(void) printf("Invalid ifindex %d\n",
35837c478bd9Sstevel@tonic-gate 				    ipmp6->ipv6GroupMemberIfIndex);
35847c478bd9Sstevel@tonic-gate 				continue;
35857c478bd9Sstevel@tonic-gate 			}
35867c478bd9Sstevel@tonic-gate 			(void) printf("%-5s %-27s %5u\n",
35877c478bd9Sstevel@tonic-gate 			    ifnamep,
35887c478bd9Sstevel@tonic-gate 			    pr_addr6(&ipmp6->ipv6GroupMemberAddress,
35897c478bd9Sstevel@tonic-gate 			    abuf, sizeof (abuf)),
35907c478bd9Sstevel@tonic-gate 			    ipmp6->ipv6GroupMemberRefCnt);
35917c478bd9Sstevel@tonic-gate 
35927c478bd9Sstevel@tonic-gate 			if (!Vflag || v6src == NULL)
35937c478bd9Sstevel@tonic-gate 				continue;
35947c478bd9Sstevel@tonic-gate 
35957c478bd9Sstevel@tonic-gate 			if (Dflag)
35967c478bd9Sstevel@tonic-gate 				(void) printf("scanning %u ipv6GroupSource "
35977c478bd9Sstevel@tonic-gate 				    "records...\n",
35987c478bd9Sstevel@tonic-gate 				    v6src->length/sizeof (ipv6_grpsrc_t));
35997c478bd9Sstevel@tonic-gate 
36007c478bd9Sstevel@tonic-gate 			first_src = B_TRUE;
36017c478bd9Sstevel@tonic-gate 			for (ips6 = (ipv6_grpsrc_t *)v6src->valp;
36027c478bd9Sstevel@tonic-gate 			    (char *)ips6 < (char *)v6src->valp + v6src->length;
36037c478bd9Sstevel@tonic-gate 			    /* LINTED: (note 1) */
36047c478bd9Sstevel@tonic-gate 			    ips6 = (ipv6_grpsrc_t *)((char *)ips6 +
36057c478bd9Sstevel@tonic-gate 			    ipv6GroupSourceEntrySize)) {
36067c478bd9Sstevel@tonic-gate 				/* same assumption as in the v4 case above */
36077c478bd9Sstevel@tonic-gate 				if ((ipmp6->ipv6GroupMemberIfIndex !=
36087c478bd9Sstevel@tonic-gate 				    ips6->ipv6GroupSourceIfIndex) ||
36097c478bd9Sstevel@tonic-gate 				    (!IN6_ARE_ADDR_EQUAL(
36107c478bd9Sstevel@tonic-gate 				    &ipmp6->ipv6GroupMemberAddress,
36117c478bd9Sstevel@tonic-gate 				    &ips6->ipv6GroupSourceGroup))) {
36127c478bd9Sstevel@tonic-gate 					if (first_src)
36137c478bd9Sstevel@tonic-gate 						continue;
36147c478bd9Sstevel@tonic-gate 					else
36157c478bd9Sstevel@tonic-gate 						break;
36167c478bd9Sstevel@tonic-gate 				}
36177c478bd9Sstevel@tonic-gate 				if (first_src) {
36187c478bd9Sstevel@tonic-gate 					(void) printf("\t%s:    %s\n",
36197c478bd9Sstevel@tonic-gate 					    fmodestr(
36207c478bd9Sstevel@tonic-gate 					    ipmp6->ipv6GroupMemberFilterMode),
36217c478bd9Sstevel@tonic-gate 					    pr_addr6(
36227c478bd9Sstevel@tonic-gate 					    &ips6->ipv6GroupSourceAddress,
36237c478bd9Sstevel@tonic-gate 					    abuf, sizeof (abuf)));
36247c478bd9Sstevel@tonic-gate 					first_src = B_FALSE;
36257c478bd9Sstevel@tonic-gate 					continue;
36267c478bd9Sstevel@tonic-gate 				}
36277c478bd9Sstevel@tonic-gate 
36287c478bd9Sstevel@tonic-gate 				(void) printf("\t            %s\n",
36297c478bd9Sstevel@tonic-gate 				    pr_addr6(&ips6->ipv6GroupSourceAddress,
36307c478bd9Sstevel@tonic-gate 				    abuf, sizeof (abuf)));
36317c478bd9Sstevel@tonic-gate 			}
36327c478bd9Sstevel@tonic-gate 		}
36337c478bd9Sstevel@tonic-gate 		(void) putchar('\n');
36347c478bd9Sstevel@tonic-gate 	}
36357c478bd9Sstevel@tonic-gate 
36367c478bd9Sstevel@tonic-gate 	(void) putchar('\n');
36377c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
36387c478bd9Sstevel@tonic-gate }
36397c478bd9Sstevel@tonic-gate 
36407c478bd9Sstevel@tonic-gate /* --------------------- ARP_REPORT (netstat -p) -------------------------- */
36417c478bd9Sstevel@tonic-gate 
36427c478bd9Sstevel@tonic-gate static void
36437c478bd9Sstevel@tonic-gate arp_report(mib_item_t *item)
36447c478bd9Sstevel@tonic-gate {
36457c478bd9Sstevel@tonic-gate 	int		jtemp = 0;
36467c478bd9Sstevel@tonic-gate 	char		ifname[LIFNAMSIZ + 1];
36477c478bd9Sstevel@tonic-gate 	char		abuf[MAXHOSTNAMELEN + 1];
36487c478bd9Sstevel@tonic-gate 	char		maskbuf[STR_EXPAND * OCTET_LENGTH + 1];
36497c478bd9Sstevel@tonic-gate 	char		flbuf[32];	/* ACE_F_ flags */
36507c478bd9Sstevel@tonic-gate 	char		xbuf[STR_EXPAND * OCTET_LENGTH + 1];
36517c478bd9Sstevel@tonic-gate 	mib2_ipNetToMediaEntry_t	*np;
36527c478bd9Sstevel@tonic-gate 	int		flags;
36537c478bd9Sstevel@tonic-gate 	boolean_t	first;
36547c478bd9Sstevel@tonic-gate 
36557c478bd9Sstevel@tonic-gate 	if (!(family_selected(AF_INET)))
36567c478bd9Sstevel@tonic-gate 		return;
36577c478bd9Sstevel@tonic-gate 
36587c478bd9Sstevel@tonic-gate 	/* 'for' loop 1: */
36597c478bd9Sstevel@tonic-gate 	for (; item; item = item->next_item) {
36607c478bd9Sstevel@tonic-gate 		if (Dflag) {
36617c478bd9Sstevel@tonic-gate 			(void) printf("\n--- Entry %d ---\n", ++jtemp);
36627c478bd9Sstevel@tonic-gate 			(void) printf("Group = %d, mib_id = %d, "
36637c478bd9Sstevel@tonic-gate 			    "length = %d, valp = 0x%p\n",
36647c478bd9Sstevel@tonic-gate 			    item->group, item->mib_id, item->length,
36657c478bd9Sstevel@tonic-gate 			    item->valp);
36667c478bd9Sstevel@tonic-gate 		}
36677c478bd9Sstevel@tonic-gate 		if (!(item->group == MIB2_IP && item->mib_id == MIB2_IP_MEDIA))
36687c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
36697c478bd9Sstevel@tonic-gate 
36707c478bd9Sstevel@tonic-gate 		if (Dflag)
36717c478bd9Sstevel@tonic-gate 			(void) printf("%u records for "
36727c478bd9Sstevel@tonic-gate 			    "ipNetToMediaEntryTable:\n",
36737c478bd9Sstevel@tonic-gate 			    item->length/sizeof (mib2_ipNetToMediaEntry_t));
36747c478bd9Sstevel@tonic-gate 
36757c478bd9Sstevel@tonic-gate 		first = B_TRUE;
36767c478bd9Sstevel@tonic-gate 		/* 'for' loop 2: */
36777c478bd9Sstevel@tonic-gate 		for (np = (mib2_ipNetToMediaEntry_t *)item->valp;
36787c478bd9Sstevel@tonic-gate 		    (char *)np < (char *)item->valp + item->length;
36797c478bd9Sstevel@tonic-gate 		    /* LINTED: (note 1) */
36807c478bd9Sstevel@tonic-gate 		    np = (mib2_ipNetToMediaEntry_t *)((char *)np +
36817c478bd9Sstevel@tonic-gate 		    ipNetToMediaEntrySize)) {
36827c478bd9Sstevel@tonic-gate 			if (first) {
36837c478bd9Sstevel@tonic-gate 				(void) puts(v4compat ?
36847c478bd9Sstevel@tonic-gate 				    "Net to Media Table" :
36857c478bd9Sstevel@tonic-gate 				    "Net to Media Table: IPv4");
368669bb4bb4Scarlsonj 				(void) puts("Device "
368769bb4bb4Scarlsonj 				    "  IP Address               Mask      "
368869bb4bb4Scarlsonj 				    "Flags      Phys Addr");
368969bb4bb4Scarlsonj 				(void) puts("------ "
369069bb4bb4Scarlsonj 				    "-------------------- --------------- "
369169bb4bb4Scarlsonj 				    "-------- ---------------");
36927c478bd9Sstevel@tonic-gate 				first = B_FALSE;
36937c478bd9Sstevel@tonic-gate 			}
36947c478bd9Sstevel@tonic-gate 
36957c478bd9Sstevel@tonic-gate 			flbuf[0] = '\0';
36967c478bd9Sstevel@tonic-gate 			flags = np->ipNetToMediaInfo.ntm_flags;
369769bb4bb4Scarlsonj 			/*
369869bb4bb4Scarlsonj 			 * Note that not all flags are possible at the same
369969bb4bb4Scarlsonj 			 * time.  Patterns: SPLAy DUo
370069bb4bb4Scarlsonj 			 */
37017c478bd9Sstevel@tonic-gate 			if (flags & ACE_F_PERMANENT)
37027c478bd9Sstevel@tonic-gate 				(void) strcat(flbuf, "S");
37037c478bd9Sstevel@tonic-gate 			if (flags & ACE_F_PUBLISH)
37047c478bd9Sstevel@tonic-gate 				(void) strcat(flbuf, "P");
37057c478bd9Sstevel@tonic-gate 			if (flags & ACE_F_DYING)
37067c478bd9Sstevel@tonic-gate 				(void) strcat(flbuf, "D");
37077c478bd9Sstevel@tonic-gate 			if (!(flags & ACE_F_RESOLVED))
37087c478bd9Sstevel@tonic-gate 				(void) strcat(flbuf, "U");
37097c478bd9Sstevel@tonic-gate 			if (flags & ACE_F_MAPPING)
37107c478bd9Sstevel@tonic-gate 				(void) strcat(flbuf, "M");
371169bb4bb4Scarlsonj 			if (flags & ACE_F_MYADDR)
371269bb4bb4Scarlsonj 				(void) strcat(flbuf, "L");
371369bb4bb4Scarlsonj 			if (flags & ACE_F_UNVERIFIED)
371469bb4bb4Scarlsonj 				(void) strcat(flbuf, "d");
371569bb4bb4Scarlsonj 			if (flags & ACE_F_AUTHORITY)
371669bb4bb4Scarlsonj 				(void) strcat(flbuf, "A");
371769bb4bb4Scarlsonj 			if (flags & ACE_F_OLD)
371869bb4bb4Scarlsonj 				(void) strcat(flbuf, "o");
371969bb4bb4Scarlsonj 			if (flags & ACE_F_DELAYED)
372069bb4bb4Scarlsonj 				(void) strcat(flbuf, "y");
372169bb4bb4Scarlsonj 			(void) printf("%-6s %-20s %-15s %-8s %s\n",
37227c478bd9Sstevel@tonic-gate 			    octetstr(&np->ipNetToMediaIfIndex, 'a',
37237c478bd9Sstevel@tonic-gate 			    ifname, sizeof (ifname)),
37247c478bd9Sstevel@tonic-gate 			    pr_addr(np->ipNetToMediaNetAddress,
37257c478bd9Sstevel@tonic-gate 			    abuf, sizeof (abuf)),
37267c478bd9Sstevel@tonic-gate 			    octetstr(&np->ipNetToMediaInfo.ntm_mask, 'd',
37277c478bd9Sstevel@tonic-gate 			    maskbuf, sizeof (maskbuf)),
37287c478bd9Sstevel@tonic-gate 			    flbuf,
37297c478bd9Sstevel@tonic-gate 			    octetstr(&np->ipNetToMediaPhysAddress, 'h',
37307c478bd9Sstevel@tonic-gate 			    xbuf, sizeof (xbuf)));
37317c478bd9Sstevel@tonic-gate 		} /* 'for' loop 2 ends */
37327c478bd9Sstevel@tonic-gate 	} /* 'for' loop 1 ends */
37337c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
37347c478bd9Sstevel@tonic-gate }
37357c478bd9Sstevel@tonic-gate 
37367c478bd9Sstevel@tonic-gate /* --------------------- NDP_REPORT (netstat -p) -------------------------- */
37377c478bd9Sstevel@tonic-gate 
37387c478bd9Sstevel@tonic-gate static void
37397c478bd9Sstevel@tonic-gate ndp_report(mib_item_t *item)
37407c478bd9Sstevel@tonic-gate {
37417c478bd9Sstevel@tonic-gate 	int		jtemp = 0;
37427c478bd9Sstevel@tonic-gate 	char		abuf[MAXHOSTNAMELEN + 1];
37437c478bd9Sstevel@tonic-gate 	char		*state;
37447c478bd9Sstevel@tonic-gate 	char		*type;
37457c478bd9Sstevel@tonic-gate 	char		xbuf[STR_EXPAND * OCTET_LENGTH + 1];
37467c478bd9Sstevel@tonic-gate 	mib2_ipv6NetToMediaEntry_t	*np6;
37477c478bd9Sstevel@tonic-gate 	char		ifname[LIFNAMSIZ + 1];
37487c478bd9Sstevel@tonic-gate 	char		*ifnamep;
37497c478bd9Sstevel@tonic-gate 	boolean_t	first;
37507c478bd9Sstevel@tonic-gate 
37517c478bd9Sstevel@tonic-gate 	if (!(family_selected(AF_INET6)))
37527c478bd9Sstevel@tonic-gate 		return;
37537c478bd9Sstevel@tonic-gate 
37547c478bd9Sstevel@tonic-gate 	/* 'for' loop 1: */
37557c478bd9Sstevel@tonic-gate 	for (; item; item = item->next_item) {
37567c478bd9Sstevel@tonic-gate 		if (Dflag) {
37577c478bd9Sstevel@tonic-gate 			(void) printf("\n--- Entry %d ---\n", ++jtemp);
37587c478bd9Sstevel@tonic-gate 			(void) printf("Group = %d, mib_id = %d, "
37597c478bd9Sstevel@tonic-gate 			    "length = %d, valp = 0x%p\n",
37607c478bd9Sstevel@tonic-gate 			    item->group, item->mib_id, item->length,
37617c478bd9Sstevel@tonic-gate 			    item->valp);
37627c478bd9Sstevel@tonic-gate 		}
37637c478bd9Sstevel@tonic-gate 		if (!(item->group == MIB2_IP6 &&
37647c478bd9Sstevel@tonic-gate 		    item->mib_id == MIB2_IP6_MEDIA))
37657c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
37667c478bd9Sstevel@tonic-gate 
37677c478bd9Sstevel@tonic-gate 		first = B_TRUE;
37687c478bd9Sstevel@tonic-gate 		/* 'for' loop 2: */
37697c478bd9Sstevel@tonic-gate 		for (np6 = (mib2_ipv6NetToMediaEntry_t *)item->valp;
37707c478bd9Sstevel@tonic-gate 		    (char *)np6 < (char *)item->valp + item->length;
37717c478bd9Sstevel@tonic-gate 		    /* LINTED: (note 1) */
37727c478bd9Sstevel@tonic-gate 		    np6 = (mib2_ipv6NetToMediaEntry_t *)((char *)np6 +
37737c478bd9Sstevel@tonic-gate 		    ipv6NetToMediaEntrySize)) {
37747c478bd9Sstevel@tonic-gate 			if (first) {
37757c478bd9Sstevel@tonic-gate 				(void) puts("\nNet to Media Table: IPv6");
37767c478bd9Sstevel@tonic-gate 				(void) puts(" If   Physical Address   "
37777c478bd9Sstevel@tonic-gate 				    " Type      State      Destination/Mask");
37787c478bd9Sstevel@tonic-gate 				(void) puts("----- -----------------  "
37797c478bd9Sstevel@tonic-gate 				    "------- ------------ "
37807c478bd9Sstevel@tonic-gate 				    "---------------------------");
37817c478bd9Sstevel@tonic-gate 				first = B_FALSE;
37827c478bd9Sstevel@tonic-gate 			}
37837c478bd9Sstevel@tonic-gate 
37847c478bd9Sstevel@tonic-gate 			ifnamep = if_indextoname(np6->ipv6NetToMediaIfIndex,
37857c478bd9Sstevel@tonic-gate 			    ifname);
37867c478bd9Sstevel@tonic-gate 			if (ifnamep == NULL) {
37877c478bd9Sstevel@tonic-gate 				(void) printf("Invalid ifindex %d\n",
37887c478bd9Sstevel@tonic-gate 				    np6->ipv6NetToMediaIfIndex);
37897c478bd9Sstevel@tonic-gate 				continue; /* 'for' loop 2 */
37907c478bd9Sstevel@tonic-gate 			}
37917c478bd9Sstevel@tonic-gate 			switch (np6->ipv6NetToMediaState) {
37927c478bd9Sstevel@tonic-gate 			case ND_INCOMPLETE:
37937c478bd9Sstevel@tonic-gate 				state = "INCOMPLETE";
37947c478bd9Sstevel@tonic-gate 				break;
37957c478bd9Sstevel@tonic-gate 			case ND_REACHABLE:
37967c478bd9Sstevel@tonic-gate 				state = "REACHABLE";
37977c478bd9Sstevel@tonic-gate 				break;
37987c478bd9Sstevel@tonic-gate 			case ND_STALE:
37997c478bd9Sstevel@tonic-gate 				state = "STALE";
38007c478bd9Sstevel@tonic-gate 				break;
38017c478bd9Sstevel@tonic-gate 			case ND_DELAY:
38027c478bd9Sstevel@tonic-gate 				state = "DELAY";
38037c478bd9Sstevel@tonic-gate 				break;
38047c478bd9Sstevel@tonic-gate 			case ND_PROBE:
38057c478bd9Sstevel@tonic-gate 				state = "PROBE";
38067c478bd9Sstevel@tonic-gate 				break;
38077c478bd9Sstevel@tonic-gate 			case ND_UNREACHABLE:
38087c478bd9Sstevel@tonic-gate 				state = "UNREACHABLE";
38097c478bd9Sstevel@tonic-gate 				break;
38107c478bd9Sstevel@tonic-gate 			default:
38117c478bd9Sstevel@tonic-gate 				state = "UNKNOWN";
38127c478bd9Sstevel@tonic-gate 			}
38137c478bd9Sstevel@tonic-gate 
38147c478bd9Sstevel@tonic-gate 			switch (np6->ipv6NetToMediaType) {
38157c478bd9Sstevel@tonic-gate 			case 1:
38167c478bd9Sstevel@tonic-gate 				type = "other";
38177c478bd9Sstevel@tonic-gate 				break;
38187c478bd9Sstevel@tonic-gate 			case 2:
38197c478bd9Sstevel@tonic-gate 				type = "dynamic";
38207c478bd9Sstevel@tonic-gate 				break;
38217c478bd9Sstevel@tonic-gate 			case 3:
38227c478bd9Sstevel@tonic-gate 				type = "static";
38237c478bd9Sstevel@tonic-gate 				break;
38247c478bd9Sstevel@tonic-gate 			case 4:
38257c478bd9Sstevel@tonic-gate 				type = "local";
38267c478bd9Sstevel@tonic-gate 				break;
38277c478bd9Sstevel@tonic-gate 			}
38287c478bd9Sstevel@tonic-gate 			(void) printf("%-5s %-17s  %-7s %-12s %-27s\n",
38297c478bd9Sstevel@tonic-gate 			    ifnamep,
38307c478bd9Sstevel@tonic-gate 			    octetstr(&np6->ipv6NetToMediaPhysAddress, 'h',
38317c478bd9Sstevel@tonic-gate 			    xbuf, sizeof (xbuf)),
38327c478bd9Sstevel@tonic-gate 			    type,
38337c478bd9Sstevel@tonic-gate 			    state,
38347c478bd9Sstevel@tonic-gate 			    pr_addr6(&np6->ipv6NetToMediaNetAddress,
38357c478bd9Sstevel@tonic-gate 			    abuf, sizeof (abuf)));
38367c478bd9Sstevel@tonic-gate 		} /* 'for' loop 2 ends */
38377c478bd9Sstevel@tonic-gate 	} /* 'for' loop 1 ends */
38387c478bd9Sstevel@tonic-gate 	(void) putchar('\n');
38397c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
38407c478bd9Sstevel@tonic-gate }
38417c478bd9Sstevel@tonic-gate 
38427c478bd9Sstevel@tonic-gate /* ------------------------- ire_report (netstat -r) ------------------------ */
38437c478bd9Sstevel@tonic-gate 
384445916cd2Sjpk typedef struct sec_attr_list_s {
384545916cd2Sjpk 	struct sec_attr_list_s *sal_next;
384645916cd2Sjpk 	const mib2_ipAttributeEntry_t *sal_attr;
384745916cd2Sjpk } sec_attr_list_t;
384845916cd2Sjpk 
384945916cd2Sjpk static boolean_t ire_report_item_v4(const mib2_ipRouteEntry_t *, boolean_t,
385045916cd2Sjpk     const sec_attr_list_t *);
385145916cd2Sjpk static boolean_t ire_report_item_v6(const mib2_ipv6RouteEntry_t *, boolean_t,
385245916cd2Sjpk     const sec_attr_list_t *);
385345916cd2Sjpk static const char *pr_secattr(const sec_attr_list_t *);
38547c478bd9Sstevel@tonic-gate 
38557c478bd9Sstevel@tonic-gate static void
385645916cd2Sjpk ire_report(const mib_item_t *item)
38577c478bd9Sstevel@tonic-gate {
38587c478bd9Sstevel@tonic-gate 	int			jtemp = 0;
38597c478bd9Sstevel@tonic-gate 	boolean_t		print_hdr_once_v4 = B_TRUE;
38607c478bd9Sstevel@tonic-gate 	boolean_t		print_hdr_once_v6 = B_TRUE;
38617c478bd9Sstevel@tonic-gate 	mib2_ipRouteEntry_t	*rp;
38627c478bd9Sstevel@tonic-gate 	mib2_ipv6RouteEntry_t	*rp6;
386345916cd2Sjpk 	sec_attr_list_t		**v4_attrs, **v4a;
386445916cd2Sjpk 	sec_attr_list_t		**v6_attrs, **v6a;
386545916cd2Sjpk 	sec_attr_list_t		*all_attrs, *aptr;
386645916cd2Sjpk 	const mib_item_t	*iptr;
386745916cd2Sjpk 	int			ipv4_route_count, ipv6_route_count;
386845916cd2Sjpk 	int			route_attrs_count;
386945916cd2Sjpk 
387045916cd2Sjpk 	/*
387145916cd2Sjpk 	 * Preparation pass: the kernel returns separate entries for IP routing
387245916cd2Sjpk 	 * table entries and security attributes.  We loop through the
387345916cd2Sjpk 	 * attributes first and link them into lists.
387445916cd2Sjpk 	 */
387545916cd2Sjpk 	ipv4_route_count = ipv6_route_count = route_attrs_count = 0;
387645916cd2Sjpk 	for (iptr = item; iptr != NULL; iptr = iptr->next_item) {
387745916cd2Sjpk 		if (iptr->group == MIB2_IP6 && iptr->mib_id == MIB2_IP6_ROUTE)
387845916cd2Sjpk 			ipv6_route_count += iptr->length / ipv6RouteEntrySize;
387945916cd2Sjpk 		if (iptr->group == MIB2_IP && iptr->mib_id == MIB2_IP_ROUTE)
388045916cd2Sjpk 			ipv4_route_count += iptr->length / ipRouteEntrySize;
388145916cd2Sjpk 		if ((iptr->group == MIB2_IP || iptr->group == MIB2_IP6) &&
388245916cd2Sjpk 		    iptr->mib_id == EXPER_IP_RTATTR)
388345916cd2Sjpk 			route_attrs_count += iptr->length /
388445916cd2Sjpk 			    ipRouteAttributeSize;
388545916cd2Sjpk 	}
388645916cd2Sjpk 	v4_attrs = v6_attrs = NULL;
388745916cd2Sjpk 	all_attrs = NULL;
388845916cd2Sjpk 	if (family_selected(AF_INET) && ipv4_route_count > 0) {
388945916cd2Sjpk 		v4_attrs = calloc(ipv4_route_count, sizeof (*v4_attrs));
389045916cd2Sjpk 		if (v4_attrs == NULL) {
389145916cd2Sjpk 			perror("ire_report calloc v4_attrs failed");
389245916cd2Sjpk 			return;
389345916cd2Sjpk 		}
389445916cd2Sjpk 	}
389545916cd2Sjpk 	if (family_selected(AF_INET6) && ipv6_route_count > 0) {
389645916cd2Sjpk 		v6_attrs = calloc(ipv6_route_count, sizeof (*v6_attrs));
389745916cd2Sjpk 		if (v6_attrs == NULL) {
389845916cd2Sjpk 			perror("ire_report calloc v6_attrs failed");
389945916cd2Sjpk 			goto ire_report_done;
390045916cd2Sjpk 		}
390145916cd2Sjpk 	}
390245916cd2Sjpk 	if (route_attrs_count > 0) {
390345916cd2Sjpk 		all_attrs = malloc(route_attrs_count * sizeof (*all_attrs));
390445916cd2Sjpk 		if (all_attrs == NULL) {
390545916cd2Sjpk 			perror("ire_report malloc all_attrs failed");
390645916cd2Sjpk 			goto ire_report_done;
390745916cd2Sjpk 		}
390845916cd2Sjpk 	}
390945916cd2Sjpk 	aptr = all_attrs;
391045916cd2Sjpk 	for (iptr = item; iptr != NULL; iptr = iptr->next_item) {
391145916cd2Sjpk 		mib2_ipAttributeEntry_t *iae;
391245916cd2Sjpk 		sec_attr_list_t **alp;
391345916cd2Sjpk 
391445916cd2Sjpk 		if (v4_attrs != NULL && iptr->group == MIB2_IP &&
391545916cd2Sjpk 		    iptr->mib_id == EXPER_IP_RTATTR) {
391645916cd2Sjpk 			alp = v4_attrs;
391745916cd2Sjpk 		} else if (v6_attrs != NULL && iptr->group == MIB2_IP6 &&
391845916cd2Sjpk 		    iptr->mib_id == EXPER_IP_RTATTR) {
391945916cd2Sjpk 			alp = v6_attrs;
392045916cd2Sjpk 		} else {
392145916cd2Sjpk 			continue;
392245916cd2Sjpk 		}
392345916cd2Sjpk 		for (iae = iptr->valp;
392445916cd2Sjpk 		    (char *)iae < (char *)iptr->valp + iptr->length;
392545916cd2Sjpk 		    /* LINTED: (note 1) */
392645916cd2Sjpk 		    iae = (mib2_ipAttributeEntry_t *)((char *)iae +
392745916cd2Sjpk 		    ipRouteAttributeSize)) {
392845916cd2Sjpk 			aptr->sal_next = alp[iae->iae_routeidx];
392945916cd2Sjpk 			aptr->sal_attr = iae;
393045916cd2Sjpk 			alp[iae->iae_routeidx] = aptr++;
393145916cd2Sjpk 		}
393245916cd2Sjpk 	}
39337c478bd9Sstevel@tonic-gate 
39347c478bd9Sstevel@tonic-gate 	/* 'for' loop 1: */
393545916cd2Sjpk 	v4a = v4_attrs;
393645916cd2Sjpk 	v6a = v6_attrs;
393745916cd2Sjpk 	for (; item != NULL; item = item->next_item) {
39387c478bd9Sstevel@tonic-gate 		if (Dflag) {
39397c478bd9Sstevel@tonic-gate 			(void) printf("\n--- Entry %d ---\n", ++jtemp);
39407c478bd9Sstevel@tonic-gate 			(void) printf("Group = %d, mib_id = %d, "
39417c478bd9Sstevel@tonic-gate 			    "length = %d, valp = 0x%p\n",
39427c478bd9Sstevel@tonic-gate 			    item->group, item->mib_id,
39437c478bd9Sstevel@tonic-gate 			    item->length, item->valp);
39447c478bd9Sstevel@tonic-gate 		}
39457c478bd9Sstevel@tonic-gate 		if (!((item->group == MIB2_IP &&
39467c478bd9Sstevel@tonic-gate 		    item->mib_id == MIB2_IP_ROUTE) ||
39477c478bd9Sstevel@tonic-gate 		    (item->group == MIB2_IP6 &&
39487c478bd9Sstevel@tonic-gate 		    item->mib_id == MIB2_IP6_ROUTE)))
39497c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
39507c478bd9Sstevel@tonic-gate 
39517c478bd9Sstevel@tonic-gate 		if (item->group == MIB2_IP && !family_selected(AF_INET))
39527c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
39537c478bd9Sstevel@tonic-gate 		else if (item->group == MIB2_IP6 && !family_selected(AF_INET6))
39547c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
39557c478bd9Sstevel@tonic-gate 
39567c478bd9Sstevel@tonic-gate 		if (Dflag) {
39577c478bd9Sstevel@tonic-gate 			if (item->group == MIB2_IP) {
39587c478bd9Sstevel@tonic-gate 				(void) printf("%u records for "
39597c478bd9Sstevel@tonic-gate 				    "ipRouteEntryTable:\n",
39607c478bd9Sstevel@tonic-gate 				    item->length/sizeof (mib2_ipRouteEntry_t));
39617c478bd9Sstevel@tonic-gate 			} else {
39627c478bd9Sstevel@tonic-gate 				(void) printf("%u records for "
39637c478bd9Sstevel@tonic-gate 				    "ipv6RouteEntryTable:\n",
39647c478bd9Sstevel@tonic-gate 				    item->length/
39657c478bd9Sstevel@tonic-gate 				    sizeof (mib2_ipv6RouteEntry_t));
39667c478bd9Sstevel@tonic-gate 			}
39677c478bd9Sstevel@tonic-gate 		}
39687c478bd9Sstevel@tonic-gate 
39697c478bd9Sstevel@tonic-gate 		if (item->group == MIB2_IP) {
39707c478bd9Sstevel@tonic-gate 			for (rp = (mib2_ipRouteEntry_t *)item->valp;
39717c478bd9Sstevel@tonic-gate 			    (char *)rp < (char *)item->valp + item->length;
39727c478bd9Sstevel@tonic-gate 			    /* LINTED: (note 1) */
39737c478bd9Sstevel@tonic-gate 			    rp = (mib2_ipRouteEntry_t *)((char *)rp +
39747c478bd9Sstevel@tonic-gate 			    ipRouteEntrySize)) {
397545916cd2Sjpk 				aptr = v4a == NULL ? NULL : *v4a++;
39767c478bd9Sstevel@tonic-gate 				print_hdr_once_v4 = ire_report_item_v4(rp,
397745916cd2Sjpk 				    print_hdr_once_v4, aptr);
39787c478bd9Sstevel@tonic-gate 			}
39797c478bd9Sstevel@tonic-gate 		} else {
39807c478bd9Sstevel@tonic-gate 			for (rp6 = (mib2_ipv6RouteEntry_t *)item->valp;
39817c478bd9Sstevel@tonic-gate 			    (char *)rp6 < (char *)item->valp + item->length;
39827c478bd9Sstevel@tonic-gate 			    /* LINTED: (note 1) */
39837c478bd9Sstevel@tonic-gate 			    rp6 = (mib2_ipv6RouteEntry_t *)((char *)rp6 +
39847c478bd9Sstevel@tonic-gate 			    ipv6RouteEntrySize)) {
398545916cd2Sjpk 				aptr = v6a == NULL ? NULL : *v6a++;
39867c478bd9Sstevel@tonic-gate 				print_hdr_once_v6 = ire_report_item_v6(rp6,
398745916cd2Sjpk 				    print_hdr_once_v6, aptr);
39887c478bd9Sstevel@tonic-gate 			}
39897c478bd9Sstevel@tonic-gate 		}
39907c478bd9Sstevel@tonic-gate 	} /* 'for' loop 1 ends */
39917c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
399245916cd2Sjpk ire_report_done:
399345916cd2Sjpk 	if (v4_attrs != NULL)
399445916cd2Sjpk 		free(v4_attrs);
399545916cd2Sjpk 	if (v6_attrs != NULL)
399645916cd2Sjpk 		free(v6_attrs);
399745916cd2Sjpk 	if (all_attrs != NULL)
399845916cd2Sjpk 		free(all_attrs);
39997c478bd9Sstevel@tonic-gate }
40007c478bd9Sstevel@tonic-gate 
40017c478bd9Sstevel@tonic-gate /*
40027c478bd9Sstevel@tonic-gate  * Match a user-supplied device name.  We do this by string because
40037c478bd9Sstevel@tonic-gate  * the MIB2 interface gives us interface name strings rather than
40047c478bd9Sstevel@tonic-gate  * ifIndex numbers.  The "none" rule matches only routes with no
40057c478bd9Sstevel@tonic-gate  * interface.  The "any" rule matches routes with any non-blank
40067c478bd9Sstevel@tonic-gate  * interface.  A base name ("hme0") matches all aliases as well
40077c478bd9Sstevel@tonic-gate  * ("hme0:1").
40087c478bd9Sstevel@tonic-gate  */
40097c478bd9Sstevel@tonic-gate static boolean_t
40107c478bd9Sstevel@tonic-gate dev_name_match(const DeviceName *devnam, const char *ifname)
40117c478bd9Sstevel@tonic-gate {
40127c478bd9Sstevel@tonic-gate 	int iflen;
40137c478bd9Sstevel@tonic-gate 
40147c478bd9Sstevel@tonic-gate 	if (ifname == NULL)
40157c478bd9Sstevel@tonic-gate 		return (devnam->o_length == 0);		/* "none" */
40167c478bd9Sstevel@tonic-gate 	if (*ifname == '\0')
40177c478bd9Sstevel@tonic-gate 		return (devnam->o_length != 0);		/* "any" */
40187c478bd9Sstevel@tonic-gate 	iflen = strlen(ifname);
40197c478bd9Sstevel@tonic-gate 	/* The check for ':' here supports interface aliases. */
40207c478bd9Sstevel@tonic-gate 	if (iflen > devnam->o_length ||
40217c478bd9Sstevel@tonic-gate 	    (iflen < devnam->o_length && devnam->o_bytes[iflen] != ':'))
40227c478bd9Sstevel@tonic-gate 		return (B_FALSE);
40237c478bd9Sstevel@tonic-gate 	return (strncmp(ifname, devnam->o_bytes, iflen) == 0);
40247c478bd9Sstevel@tonic-gate }
40257c478bd9Sstevel@tonic-gate 
40267c478bd9Sstevel@tonic-gate /*
40277c478bd9Sstevel@tonic-gate  * Match a user-supplied IP address list.  The "any" rule matches any
40287c478bd9Sstevel@tonic-gate  * non-zero address.  The "none" rule matches only the zero address.
40297c478bd9Sstevel@tonic-gate  * IPv6 addresses supplied by the user are ignored.  If the user
40307c478bd9Sstevel@tonic-gate  * supplies a subnet mask, then match routes that are at least that
40317c478bd9Sstevel@tonic-gate  * specific (use the user's mask).  If the user supplies only an
40327c478bd9Sstevel@tonic-gate  * address, then select any routes that would match (use the route's
40337c478bd9Sstevel@tonic-gate  * mask).
40347c478bd9Sstevel@tonic-gate  */
40357c478bd9Sstevel@tonic-gate static boolean_t
40367c478bd9Sstevel@tonic-gate v4_addr_match(IpAddress addr, IpAddress mask, const filter_t *fp)
40377c478bd9Sstevel@tonic-gate {
40387c478bd9Sstevel@tonic-gate 	char **app;
40397c478bd9Sstevel@tonic-gate 	char *aptr;
40407c478bd9Sstevel@tonic-gate 	in_addr_t faddr, fmask;
40417c478bd9Sstevel@tonic-gate 
40427c478bd9Sstevel@tonic-gate 	if (fp->u.a.f_address == NULL) {
40437c478bd9Sstevel@tonic-gate 		if (IN6_IS_ADDR_UNSPECIFIED(&fp->u.a.f_mask))
40447c478bd9Sstevel@tonic-gate 			return (addr != INADDR_ANY);	/* "any" */
40457c478bd9Sstevel@tonic-gate 		else
40467c478bd9Sstevel@tonic-gate 			return (addr == INADDR_ANY);	/* "none" */
40477c478bd9Sstevel@tonic-gate 	}
40487c478bd9Sstevel@tonic-gate 	if (!IN6_IS_V4MASK(fp->u.a.f_mask))
40497c478bd9Sstevel@tonic-gate 		return (B_FALSE);
40507c478bd9Sstevel@tonic-gate 	IN6_V4MAPPED_TO_IPADDR(&fp->u.a.f_mask, fmask);
40517c478bd9Sstevel@tonic-gate 	if (fmask != IP_HOST_MASK) {
40527c478bd9Sstevel@tonic-gate 		if (fmask > mask)
40537c478bd9Sstevel@tonic-gate 			return (B_FALSE);
40547c478bd9Sstevel@tonic-gate 		mask = fmask;
40557c478bd9Sstevel@tonic-gate 	}
40567c478bd9Sstevel@tonic-gate 	for (app = fp->u.a.f_address->h_addr_list; (aptr = *app) != NULL; app++)
40577c478bd9Sstevel@tonic-gate 		/* LINTED: (note 1) */
40587c478bd9Sstevel@tonic-gate 		if (IN6_IS_ADDR_V4MAPPED((in6_addr_t *)aptr)) {
40597c478bd9Sstevel@tonic-gate 			/* LINTED: (note 1) */
40607c478bd9Sstevel@tonic-gate 			IN6_V4MAPPED_TO_IPADDR((in6_addr_t *)aptr, faddr);
40617c478bd9Sstevel@tonic-gate 			if (((faddr ^ addr) & mask) == 0)
40627c478bd9Sstevel@tonic-gate 				return (B_TRUE);
40637c478bd9Sstevel@tonic-gate 		}
40647c478bd9Sstevel@tonic-gate 	return (B_FALSE);
40657c478bd9Sstevel@tonic-gate }
40667c478bd9Sstevel@tonic-gate 
40677c478bd9Sstevel@tonic-gate /*
40687c478bd9Sstevel@tonic-gate  * Run through the filter list for an IPv4 MIB2 route entry.  If all
40697c478bd9Sstevel@tonic-gate  * filters of a given type fail to match, then the route is filtered
40707c478bd9Sstevel@tonic-gate  * out (not displayed).  If no filter is given or at least one filter
40717c478bd9Sstevel@tonic-gate  * of each type matches, then display the route.
40727c478bd9Sstevel@tonic-gate  */
40737c478bd9Sstevel@tonic-gate static boolean_t
407445916cd2Sjpk ire_filter_match_v4(const mib2_ipRouteEntry_t *rp, uint_t flag_b)
40757c478bd9Sstevel@tonic-gate {
40767c478bd9Sstevel@tonic-gate 	filter_t *fp;
40777c478bd9Sstevel@tonic-gate 	int idx;
40787c478bd9Sstevel@tonic-gate 
40797c478bd9Sstevel@tonic-gate 	/* 'for' loop 1: */
40807c478bd9Sstevel@tonic-gate 	for (idx = 0; idx < NFILTERKEYS; idx++)
40817c478bd9Sstevel@tonic-gate 		if ((fp = filters[idx]) != NULL) {
40827c478bd9Sstevel@tonic-gate 			/* 'for' loop 2: */
40837c478bd9Sstevel@tonic-gate 			for (; fp != NULL; fp = fp->f_next) {
40847c478bd9Sstevel@tonic-gate 				switch (idx) {
40857c478bd9Sstevel@tonic-gate 				case FK_AF:
40867c478bd9Sstevel@tonic-gate 					if (fp->u.f_family != AF_INET)
40877c478bd9Sstevel@tonic-gate 						continue; /* 'for' loop 2 */
40887c478bd9Sstevel@tonic-gate 					break;
40897c478bd9Sstevel@tonic-gate 				case FK_OUTIF:
40907c478bd9Sstevel@tonic-gate 					if (!dev_name_match(&rp->ipRouteIfIndex,
40917c478bd9Sstevel@tonic-gate 					    fp->u.f_ifname))
40927c478bd9Sstevel@tonic-gate 						continue; /* 'for' loop 2 */
40937c478bd9Sstevel@tonic-gate 					break;
40947c478bd9Sstevel@tonic-gate 				case FK_DST:
40957c478bd9Sstevel@tonic-gate 					if (!v4_addr_match(rp->ipRouteDest,
40967c478bd9Sstevel@tonic-gate 					    rp->ipRouteMask, fp))
40977c478bd9Sstevel@tonic-gate 						continue; /* 'for' loop 2 */
40987c478bd9Sstevel@tonic-gate 					break;
40997c478bd9Sstevel@tonic-gate 				case FK_FLAGS:
41007c478bd9Sstevel@tonic-gate 					if ((flag_b & fp->u.f.f_flagset) !=
41017c478bd9Sstevel@tonic-gate 					    fp->u.f.f_flagset ||
41027c478bd9Sstevel@tonic-gate 					    (flag_b & fp->u.f.f_flagclear))
41037c478bd9Sstevel@tonic-gate 						continue; /* 'for' loop 2 */
41047c478bd9Sstevel@tonic-gate 					break;
41057c478bd9Sstevel@tonic-gate 				}
41067c478bd9Sstevel@tonic-gate 				break;
41077c478bd9Sstevel@tonic-gate 			} /* 'for' loop 2 ends */
41087c478bd9Sstevel@tonic-gate 			if (fp == NULL)
41097c478bd9Sstevel@tonic-gate 				return (B_FALSE);
41107c478bd9Sstevel@tonic-gate 		}
41117c478bd9Sstevel@tonic-gate 	/* 'for' loop 1 ends */
41127c478bd9Sstevel@tonic-gate 	return (B_TRUE);
41137c478bd9Sstevel@tonic-gate }
41147c478bd9Sstevel@tonic-gate 
41157c478bd9Sstevel@tonic-gate /*
41167c478bd9Sstevel@tonic-gate  * Given an IPv4 MIB2 route entry, form the list of flags for the
41177c478bd9Sstevel@tonic-gate  * route.
41187c478bd9Sstevel@tonic-gate  */
41197c478bd9Sstevel@tonic-gate static uint_t
412045916cd2Sjpk form_v4_route_flags(const mib2_ipRouteEntry_t *rp, char *flags)
41217c478bd9Sstevel@tonic-gate {
41227c478bd9Sstevel@tonic-gate 	uint_t flag_b;
41237c478bd9Sstevel@tonic-gate 
41247c478bd9Sstevel@tonic-gate 	flag_b = FLF_U;
41257c478bd9Sstevel@tonic-gate 	(void) strcpy(flags, "U");
41267c478bd9Sstevel@tonic-gate 	if (rp->ipRouteInfo.re_ire_type == IRE_DEFAULT ||
41277c478bd9Sstevel@tonic-gate 	    rp->ipRouteInfo.re_ire_type == IRE_PREFIX ||
41287c478bd9Sstevel@tonic-gate 	    rp->ipRouteInfo.re_ire_type == IRE_HOST ||
41297c478bd9Sstevel@tonic-gate 	    rp->ipRouteInfo.re_ire_type == IRE_HOST_REDIRECT) {
41307c478bd9Sstevel@tonic-gate 		(void) strcat(flags, "G");
41317c478bd9Sstevel@tonic-gate 		flag_b |= FLF_G;
41327c478bd9Sstevel@tonic-gate 	}
41337c478bd9Sstevel@tonic-gate 	if (rp->ipRouteMask == IP_HOST_MASK) {
41347c478bd9Sstevel@tonic-gate 		(void) strcat(flags, "H");
41357c478bd9Sstevel@tonic-gate 		flag_b |= FLF_H;
41367c478bd9Sstevel@tonic-gate 	}
41377c478bd9Sstevel@tonic-gate 	if (rp->ipRouteInfo.re_ire_type == IRE_HOST_REDIRECT) {
41387c478bd9Sstevel@tonic-gate 		(void) strcat(flags, "D");
41397c478bd9Sstevel@tonic-gate 		flag_b |= FLF_D;
41407c478bd9Sstevel@tonic-gate 	}
41417c478bd9Sstevel@tonic-gate 	if (rp->ipRouteInfo.re_ire_type == IRE_CACHE) {
41427c478bd9Sstevel@tonic-gate 		/* Address resolution */
41437c478bd9Sstevel@tonic-gate 		(void) strcat(flags, "A");
41447c478bd9Sstevel@tonic-gate 		flag_b |= FLF_A;
41457c478bd9Sstevel@tonic-gate 	}
41467c478bd9Sstevel@tonic-gate 	if (rp->ipRouteInfo.re_ire_type == IRE_BROADCAST) {	/* Broadcast */
41477c478bd9Sstevel@tonic-gate 		(void) strcat(flags, "B");
41487c478bd9Sstevel@tonic-gate 		flag_b |= FLF_B;
41497c478bd9Sstevel@tonic-gate 	}
41507c478bd9Sstevel@tonic-gate 	if (rp->ipRouteInfo.re_ire_type == IRE_LOCAL) {		/* Local */
41517c478bd9Sstevel@tonic-gate 		(void) strcat(flags, "L");
41527c478bd9Sstevel@tonic-gate 		flag_b |= FLF_L;
41537c478bd9Sstevel@tonic-gate 	}
41547c478bd9Sstevel@tonic-gate 	if (rp->ipRouteInfo.re_flags & RTF_MULTIRT) {
41557c478bd9Sstevel@tonic-gate 		(void) strcat(flags, "M");			/* Multiroute */
41567c478bd9Sstevel@tonic-gate 		flag_b |= FLF_M;
41577c478bd9Sstevel@tonic-gate 	}
41587c478bd9Sstevel@tonic-gate 	if (rp->ipRouteInfo.re_flags & RTF_SETSRC) {
41597c478bd9Sstevel@tonic-gate 		(void) strcat(flags, "S");			/* Setsrc */
41607c478bd9Sstevel@tonic-gate 		flag_b |= FLF_S;
41617c478bd9Sstevel@tonic-gate 	}
41627c478bd9Sstevel@tonic-gate 	return (flag_b);
41637c478bd9Sstevel@tonic-gate }
41647c478bd9Sstevel@tonic-gate 
416545916cd2Sjpk static const char ire_hdr_v4[] =
416645916cd2Sjpk "\n%s Table: IPv4\n";
416745916cd2Sjpk static const char ire_hdr_v4_compat[] =
416845916cd2Sjpk "\n%s Table:\n";
416945916cd2Sjpk static const char ire_hdr_v4_verbose[] =
417045916cd2Sjpk "  Destination             Mask           Gateway          Device Mxfrg "
417145916cd2Sjpk "Rtt   Ref Flg  Out  In/Fwd %s\n"
417245916cd2Sjpk "-------------------- --------------- -------------------- ------ ----- "
417345916cd2Sjpk "----- --- --- ----- ------ %s\n";
417445916cd2Sjpk 
417545916cd2Sjpk static const char ire_hdr_v4_normal[] =
4176aecc8c24Sja "  Destination           Gateway           Flags  Ref     Use     Interface"
4177aecc8c24Sja " %s\n-------------------- -------------------- ----- ----- ---------- "
4178aecc8c24Sja "--------- %s\n";
417945916cd2Sjpk 
41807c478bd9Sstevel@tonic-gate static boolean_t
418145916cd2Sjpk ire_report_item_v4(const mib2_ipRouteEntry_t *rp, boolean_t first,
418245916cd2Sjpk     const sec_attr_list_t *attrs)
41837c478bd9Sstevel@tonic-gate {
41847c478bd9Sstevel@tonic-gate 	char			dstbuf[MAXHOSTNAMELEN + 1];
41857c478bd9Sstevel@tonic-gate 	char			maskbuf[MAXHOSTNAMELEN + 1];
41867c478bd9Sstevel@tonic-gate 	char			gwbuf[MAXHOSTNAMELEN + 1];
41877c478bd9Sstevel@tonic-gate 	char			ifname[LIFNAMSIZ + 1];
41887c478bd9Sstevel@tonic-gate 	char			flags[10];	/* RTF_ flags */
41897c478bd9Sstevel@tonic-gate 	uint_t			flag_b;
41907c478bd9Sstevel@tonic-gate 
4191*5c0b7edeSseb 	if (!(Aflag || (rp->ipRouteInfo.re_ire_type != IRE_CACHE &&
41927c478bd9Sstevel@tonic-gate 	    rp->ipRouteInfo.re_ire_type != IRE_BROADCAST &&
41937c478bd9Sstevel@tonic-gate 	    rp->ipRouteInfo.re_ire_type != IRE_LOCAL))) {
41947c478bd9Sstevel@tonic-gate 		return (first);
41957c478bd9Sstevel@tonic-gate 	}
41967c478bd9Sstevel@tonic-gate 
41977c478bd9Sstevel@tonic-gate 	flag_b = form_v4_route_flags(rp, flags);
41987c478bd9Sstevel@tonic-gate 
41997c478bd9Sstevel@tonic-gate 	if (!ire_filter_match_v4(rp, flag_b))
42007c478bd9Sstevel@tonic-gate 		return (first);
42017c478bd9Sstevel@tonic-gate 
42027c478bd9Sstevel@tonic-gate 	if (first) {
420345916cd2Sjpk 		(void) printf(v4compat ? ire_hdr_v4_compat : ire_hdr_v4,
420445916cd2Sjpk 		    Vflag ? "IRE" : "Routing");
420545916cd2Sjpk 		(void) printf(Vflag ? ire_hdr_v4_verbose : ire_hdr_v4_normal,
420645916cd2Sjpk 		    RSECflag ? "  Gateway security attributes  " : "",
420745916cd2Sjpk 		    RSECflag ? "-------------------------------" : "");
42087c478bd9Sstevel@tonic-gate 		first = B_FALSE;
42097c478bd9Sstevel@tonic-gate 	}
42107c478bd9Sstevel@tonic-gate 
42117c478bd9Sstevel@tonic-gate 	if (flag_b & FLF_H) {
42127c478bd9Sstevel@tonic-gate 		(void) pr_addr(rp->ipRouteDest, dstbuf, sizeof (dstbuf));
42137c478bd9Sstevel@tonic-gate 	} else {
42147c478bd9Sstevel@tonic-gate 		(void) pr_net(rp->ipRouteDest, rp->ipRouteMask,
42157c478bd9Sstevel@tonic-gate 		    dstbuf, sizeof (dstbuf));
42167c478bd9Sstevel@tonic-gate 	}
42177c478bd9Sstevel@tonic-gate 	if (Vflag) {
42187c478bd9Sstevel@tonic-gate 		(void) printf("%-20s %-15s %-20s %-6s %5u%c %4u %3u "
4219aecc8c24Sja 		    "%-4s%6u %6u %s\n",
42207c478bd9Sstevel@tonic-gate 		    dstbuf,
42217c478bd9Sstevel@tonic-gate 		    pr_mask(rp->ipRouteMask, maskbuf, sizeof (maskbuf)),
42227c478bd9Sstevel@tonic-gate 		    pr_addrnz(rp->ipRouteNextHop, gwbuf, sizeof (gwbuf)),
42237c478bd9Sstevel@tonic-gate 		    octetstr(&rp->ipRouteIfIndex, 'a', ifname, sizeof (ifname)),
42247c478bd9Sstevel@tonic-gate 		    rp->ipRouteInfo.re_max_frag,
42257c478bd9Sstevel@tonic-gate 		    rp->ipRouteInfo.re_frag_flag ? '*' : ' ',
42267c478bd9Sstevel@tonic-gate 		    rp->ipRouteInfo.re_rtt,
42277c478bd9Sstevel@tonic-gate 		    rp->ipRouteInfo.re_ref,
42287c478bd9Sstevel@tonic-gate 		    flags,
42297c478bd9Sstevel@tonic-gate 		    rp->ipRouteInfo.re_obpkt,
423045916cd2Sjpk 		    rp->ipRouteInfo.re_ibpkt,
423145916cd2Sjpk 		    pr_secattr(attrs));
42327c478bd9Sstevel@tonic-gate 	} else {
4233aecc8c24Sja 		(void) printf("%-20s %-20s %-5s  %4u %10u %-9s %s\n",
42347c478bd9Sstevel@tonic-gate 		    dstbuf,
42357c478bd9Sstevel@tonic-gate 		    pr_addrnz(rp->ipRouteNextHop, gwbuf, sizeof (gwbuf)),
42367c478bd9Sstevel@tonic-gate 		    flags,
42377c478bd9Sstevel@tonic-gate 		    rp->ipRouteInfo.re_ref,
42387c478bd9Sstevel@tonic-gate 		    rp->ipRouteInfo.re_obpkt + rp->ipRouteInfo.re_ibpkt,
42397c478bd9Sstevel@tonic-gate 		    octetstr(&rp->ipRouteIfIndex, 'a',
424045916cd2Sjpk 		    ifname, sizeof (ifname)),
424145916cd2Sjpk 		    pr_secattr(attrs));
42427c478bd9Sstevel@tonic-gate 	}
42437c478bd9Sstevel@tonic-gate 	return (first);
42447c478bd9Sstevel@tonic-gate }
42457c478bd9Sstevel@tonic-gate 
42467c478bd9Sstevel@tonic-gate /*
42477c478bd9Sstevel@tonic-gate  * Match a user-supplied IP address list against an IPv6 route entry.
42487c478bd9Sstevel@tonic-gate  * If the user specified "any," then any non-zero address matches.  If
42497c478bd9Sstevel@tonic-gate  * the user specified "none," then only the zero address matches.  If
42507c478bd9Sstevel@tonic-gate  * the user specified a subnet mask length, then use that in matching
42517c478bd9Sstevel@tonic-gate  * routes (select routes that are at least as specific).  If the user
42527c478bd9Sstevel@tonic-gate  * specified only an address, then use the route's mask (select routes
42537c478bd9Sstevel@tonic-gate  * that would match that address).  IPv4 addresses are ignored.
42547c478bd9Sstevel@tonic-gate  */
42557c478bd9Sstevel@tonic-gate static boolean_t
42567c478bd9Sstevel@tonic-gate v6_addr_match(const Ip6Address *addr, int masklen, const filter_t *fp)
42577c478bd9Sstevel@tonic-gate {
42587c478bd9Sstevel@tonic-gate 	const uint8_t *ucp;
42597c478bd9Sstevel@tonic-gate 	int fmasklen;
42607c478bd9Sstevel@tonic-gate 	int i;
42617c478bd9Sstevel@tonic-gate 	char **app;
42627c478bd9Sstevel@tonic-gate 	char *aptr;
42637c478bd9Sstevel@tonic-gate 
42647c478bd9Sstevel@tonic-gate 	if (fp->u.a.f_address == NULL) {
42657c478bd9Sstevel@tonic-gate 		if (IN6_IS_ADDR_UNSPECIFIED(&fp->u.a.f_mask))	/* any */
42667c478bd9Sstevel@tonic-gate 			return (!IN6_IS_ADDR_UNSPECIFIED(addr));
42677c478bd9Sstevel@tonic-gate 		return (IN6_IS_ADDR_UNSPECIFIED(addr));		/* "none" */
42687c478bd9Sstevel@tonic-gate 	}
42697c478bd9Sstevel@tonic-gate 	fmasklen = 0;
42707c478bd9Sstevel@tonic-gate 	/* 'for' loop 1a: */
42717c478bd9Sstevel@tonic-gate 	for (ucp = fp->u.a.f_mask.s6_addr;
42727c478bd9Sstevel@tonic-gate 	    ucp < fp->u.a.f_mask.s6_addr + sizeof (fp->u.a.f_mask.s6_addr);
42737c478bd9Sstevel@tonic-gate 	    ucp++) {
42747c478bd9Sstevel@tonic-gate 		if (*ucp != 0xff) {
42757c478bd9Sstevel@tonic-gate 			if (*ucp != 0)
42767c478bd9Sstevel@tonic-gate 				fmasklen += 9 - ffs(*ucp);
42777c478bd9Sstevel@tonic-gate 			break; /* 'for' loop 1a */
42787c478bd9Sstevel@tonic-gate 		}
42797c478bd9Sstevel@tonic-gate 		fmasklen += 8;
42807c478bd9Sstevel@tonic-gate 	} /* 'for' loop 1a ends */
42817c478bd9Sstevel@tonic-gate 	if (fmasklen != IPV6_ABITS) {
42827c478bd9Sstevel@tonic-gate 		if (fmasklen > masklen)
42837c478bd9Sstevel@tonic-gate 			return (B_FALSE);
42847c478bd9Sstevel@tonic-gate 		masklen = fmasklen;
42857c478bd9Sstevel@tonic-gate 	}
42867c478bd9Sstevel@tonic-gate 	/* 'for' loop 1b: */
42877c478bd9Sstevel@tonic-gate 	for (app = fp->u.a.f_address->h_addr_list; (aptr = *app) != NULL;
42887c478bd9Sstevel@tonic-gate 	    app++) {
42897c478bd9Sstevel@tonic-gate 		/* LINTED: (note 1) */
42907c478bd9Sstevel@tonic-gate 		if (IN6_IS_ADDR_V4MAPPED((in6_addr_t *)aptr))
42917c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1b */
42927c478bd9Sstevel@tonic-gate 		ucp = addr->s6_addr;
42937c478bd9Sstevel@tonic-gate 		for (i = masklen; i >= 8; i -= 8)
42947c478bd9Sstevel@tonic-gate 			if (*ucp++ != *aptr++)
42957c478bd9Sstevel@tonic-gate 				break; /* 'for' loop 1b */
42967c478bd9Sstevel@tonic-gate 		if (i == 0 ||
42977c478bd9Sstevel@tonic-gate 		    (i < 8 && ((*ucp ^ *aptr) & ~(0xff >> i)) == 0))
42987c478bd9Sstevel@tonic-gate 			return (B_TRUE);
42997c478bd9Sstevel@tonic-gate 	} /* 'for' loop 1b ends */
43007c478bd9Sstevel@tonic-gate 	return (B_FALSE);
43017c478bd9Sstevel@tonic-gate }
43027c478bd9Sstevel@tonic-gate 
43037c478bd9Sstevel@tonic-gate /*
43047c478bd9Sstevel@tonic-gate  * Run through the filter list for an IPv6 MIB2 IRE.  For a given
43057c478bd9Sstevel@tonic-gate  * type, if there's at least one filter and all filters of that type
43067c478bd9Sstevel@tonic-gate  * fail to match, then the route doesn't match and isn't displayed.
43077c478bd9Sstevel@tonic-gate  * If at least one matches, or none are specified, for each of the
43087c478bd9Sstevel@tonic-gate  * types, then the route is selected and displayed.
43097c478bd9Sstevel@tonic-gate  */
43107c478bd9Sstevel@tonic-gate static boolean_t
431145916cd2Sjpk ire_filter_match_v6(const mib2_ipv6RouteEntry_t *rp6, uint_t flag_b)
43127c478bd9Sstevel@tonic-gate {
43137c478bd9Sstevel@tonic-gate 	filter_t *fp;
43147c478bd9Sstevel@tonic-gate 	int idx;
43157c478bd9Sstevel@tonic-gate 
43167c478bd9Sstevel@tonic-gate 	/* 'for' loop 1: */
43177c478bd9Sstevel@tonic-gate 	for (idx = 0; idx < NFILTERKEYS; idx++)
43187c478bd9Sstevel@tonic-gate 		if ((fp = filters[idx]) != NULL) {
43197c478bd9Sstevel@tonic-gate 			/* 'for' loop 2: */
43207c478bd9Sstevel@tonic-gate 			for (; fp != NULL; fp = fp->f_next) {
43217c478bd9Sstevel@tonic-gate 				switch (idx) {
43227c478bd9Sstevel@tonic-gate 				case FK_AF:
43237c478bd9Sstevel@tonic-gate 					if (fp->u.f_family != AF_INET6)
43247c478bd9Sstevel@tonic-gate 						/* 'for' loop 2 */
43257c478bd9Sstevel@tonic-gate 						continue;
43267c478bd9Sstevel@tonic-gate 					break;
43277c478bd9Sstevel@tonic-gate 				case FK_OUTIF:
43287c478bd9Sstevel@tonic-gate 					if (!dev_name_match(&rp6->
43297c478bd9Sstevel@tonic-gate 					    ipv6RouteIfIndex, fp->u.f_ifname))
43307c478bd9Sstevel@tonic-gate 						/* 'for' loop 2 */
43317c478bd9Sstevel@tonic-gate 						continue;
43327c478bd9Sstevel@tonic-gate 					break;
43337c478bd9Sstevel@tonic-gate 				case FK_DST:
43347c478bd9Sstevel@tonic-gate 					if (!v6_addr_match(&rp6->ipv6RouteDest,
43357c478bd9Sstevel@tonic-gate 					    rp6->ipv6RoutePfxLength, fp))
43367c478bd9Sstevel@tonic-gate 						/* 'for' loop 2 */
43377c478bd9Sstevel@tonic-gate 						continue;
43387c478bd9Sstevel@tonic-gate 					break;
43397c478bd9Sstevel@tonic-gate 				case FK_FLAGS:
43407c478bd9Sstevel@tonic-gate 					if ((flag_b & fp->u.f.f_flagset) !=
43417c478bd9Sstevel@tonic-gate 					    fp->u.f.f_flagset ||
43427c478bd9Sstevel@tonic-gate 					    (flag_b & fp->u.f.f_flagclear))
43437c478bd9Sstevel@tonic-gate 						/* 'for' loop 2 */
43447c478bd9Sstevel@tonic-gate 						continue;
43457c478bd9Sstevel@tonic-gate 					break;
43467c478bd9Sstevel@tonic-gate 				}
43477c478bd9Sstevel@tonic-gate 				break;
43487c478bd9Sstevel@tonic-gate 			} /* 'for' loop 2 ends */
43497c478bd9Sstevel@tonic-gate 			if (fp == NULL)
43507c478bd9Sstevel@tonic-gate 				return (B_FALSE);
43517c478bd9Sstevel@tonic-gate 		}
43527c478bd9Sstevel@tonic-gate 	/* 'for' loop 1 ends */
43537c478bd9Sstevel@tonic-gate 	return (B_TRUE);
43547c478bd9Sstevel@tonic-gate }
43557c478bd9Sstevel@tonic-gate 
435645916cd2Sjpk static const char ire_hdr_v6[] =
435745916cd2Sjpk "\n%s Table: IPv6\n";
435845916cd2Sjpk static const char ire_hdr_v6_verbose[] =
435945916cd2Sjpk "  Destination/Mask            Gateway                    If    PMTU   Rtt  "
436045916cd2Sjpk "Ref Flags  Out   In/Fwd %s\n"
436145916cd2Sjpk "--------------------------- --------------------------- ----- ------ ----- "
436245916cd2Sjpk "--- ----- ------ ------ %s\n";
436345916cd2Sjpk static const char ire_hdr_v6_normal[] =
436445916cd2Sjpk "  Destination/Mask            Gateway                   Flags Ref   Use  "
4365aecc8c24Sja "  If   %s\n"
4366aecc8c24Sja "--------------------------- --------------------------- ----- --- ------- "
436745916cd2Sjpk "----- %s\n";
436845916cd2Sjpk 
43697c478bd9Sstevel@tonic-gate static boolean_t
437045916cd2Sjpk ire_report_item_v6(const mib2_ipv6RouteEntry_t *rp6, boolean_t first,
437145916cd2Sjpk     const sec_attr_list_t *attrs)
43727c478bd9Sstevel@tonic-gate {
43737c478bd9Sstevel@tonic-gate 	char			dstbuf[MAXHOSTNAMELEN + 1];
43747c478bd9Sstevel@tonic-gate 	char			gwbuf[MAXHOSTNAMELEN + 1];
43757c478bd9Sstevel@tonic-gate 	char			ifname[LIFNAMSIZ + 1];
43767c478bd9Sstevel@tonic-gate 	char			flags[10];	/* RTF_ flags */
43777c478bd9Sstevel@tonic-gate 	uint_t			flag_b;
43787c478bd9Sstevel@tonic-gate 
43797c478bd9Sstevel@tonic-gate 	if (!(Aflag || (rp6->ipv6RouteInfo.re_ire_type != IRE_CACHE &&
43807c478bd9Sstevel@tonic-gate 	    rp6->ipv6RouteInfo.re_ire_type != IRE_LOCAL))) {
43817c478bd9Sstevel@tonic-gate 		return (first);
43827c478bd9Sstevel@tonic-gate 	}
43837c478bd9Sstevel@tonic-gate 
43847c478bd9Sstevel@tonic-gate 	flag_b = FLF_U;
43857c478bd9Sstevel@tonic-gate 	(void) strcpy(flags, "U");
43867c478bd9Sstevel@tonic-gate 	if (rp6->ipv6RouteInfo.re_ire_type == IRE_DEFAULT ||
43877c478bd9Sstevel@tonic-gate 	    rp6->ipv6RouteInfo.re_ire_type == IRE_PREFIX ||
43887c478bd9Sstevel@tonic-gate 	    rp6->ipv6RouteInfo.re_ire_type == IRE_HOST ||
43897c478bd9Sstevel@tonic-gate 	    rp6->ipv6RouteInfo.re_ire_type == IRE_HOST_REDIRECT) {
43907c478bd9Sstevel@tonic-gate 		(void) strcat(flags, "G");
43917c478bd9Sstevel@tonic-gate 		flag_b |= FLF_G;
43927c478bd9Sstevel@tonic-gate 	}
43937c478bd9Sstevel@tonic-gate 
43947c478bd9Sstevel@tonic-gate 	if (rp6->ipv6RoutePfxLength == IPV6_ABITS) {
43957c478bd9Sstevel@tonic-gate 		(void) strcat(flags, "H");
43967c478bd9Sstevel@tonic-gate 		flag_b |= FLF_H;
43977c478bd9Sstevel@tonic-gate 	}
43987c478bd9Sstevel@tonic-gate 
43997c478bd9Sstevel@tonic-gate 	if (rp6->ipv6RouteInfo.re_ire_type == IRE_HOST_REDIRECT) {
44007c478bd9Sstevel@tonic-gate 		(void) strcat(flags, "D");
44017c478bd9Sstevel@tonic-gate 		flag_b |= FLF_D;
44027c478bd9Sstevel@tonic-gate 	}
44037c478bd9Sstevel@tonic-gate 	if (rp6->ipv6RouteInfo.re_ire_type == IRE_CACHE) {
44047c478bd9Sstevel@tonic-gate 		/* Address resolution */
44057c478bd9Sstevel@tonic-gate 		(void) strcat(flags, "A");
44067c478bd9Sstevel@tonic-gate 		flag_b |= FLF_A;
44077c478bd9Sstevel@tonic-gate 	}
44087c478bd9Sstevel@tonic-gate 	if (rp6->ipv6RouteInfo.re_ire_type == IRE_LOCAL) {	/* Local */
44097c478bd9Sstevel@tonic-gate 		(void) strcat(flags, "L");
44107c478bd9Sstevel@tonic-gate 		flag_b |= FLF_L;
44117c478bd9Sstevel@tonic-gate 	}
44127c478bd9Sstevel@tonic-gate 	if (rp6->ipv6RouteInfo.re_flags & RTF_MULTIRT) {
44137c478bd9Sstevel@tonic-gate 		(void) strcat(flags, "M");			/* Multiroute */
44147c478bd9Sstevel@tonic-gate 		flag_b |= FLF_M;
44157c478bd9Sstevel@tonic-gate 	}
44167c478bd9Sstevel@tonic-gate 	if (rp6->ipv6RouteInfo.re_flags & RTF_SETSRC) {
44177c478bd9Sstevel@tonic-gate 		(void) strcat(flags, "S");			/* Setsrc */
44187c478bd9Sstevel@tonic-gate 		flag_b |= FLF_S;
44197c478bd9Sstevel@tonic-gate 	}
44207c478bd9Sstevel@tonic-gate 
44217c478bd9Sstevel@tonic-gate 	if (!ire_filter_match_v6(rp6, flag_b))
44227c478bd9Sstevel@tonic-gate 		return (first);
44237c478bd9Sstevel@tonic-gate 
44247c478bd9Sstevel@tonic-gate 	if (first) {
442545916cd2Sjpk 		(void) printf(ire_hdr_v6, Vflag ? "IRE" : "Routing");
442645916cd2Sjpk 		(void) printf(Vflag ? ire_hdr_v6_verbose : ire_hdr_v6_normal,
442745916cd2Sjpk 		    RSECflag ? "  Gateway security attributes  " : "",
442845916cd2Sjpk 		    RSECflag ? "-------------------------------" : "");
44297c478bd9Sstevel@tonic-gate 		first = B_FALSE;
44307c478bd9Sstevel@tonic-gate 	}
44317c478bd9Sstevel@tonic-gate 
44327c478bd9Sstevel@tonic-gate 	if (Vflag) {
44337c478bd9Sstevel@tonic-gate 		(void) printf("%-27s %-27s %-5s %5u%c %5u %3u "
443445916cd2Sjpk 		    "%-5s %6u %6u %s\n",
44357c478bd9Sstevel@tonic-gate 		    pr_prefix6(&rp6->ipv6RouteDest,
44367c478bd9Sstevel@tonic-gate 			rp6->ipv6RoutePfxLength, dstbuf, sizeof (dstbuf)),
44377c478bd9Sstevel@tonic-gate 		    IN6_IS_ADDR_UNSPECIFIED(&rp6->ipv6RouteNextHop) ?
44387c478bd9Sstevel@tonic-gate 		    "    --" :
44397c478bd9Sstevel@tonic-gate 		    pr_addr6(&rp6->ipv6RouteNextHop, gwbuf, sizeof (gwbuf)),
44407c478bd9Sstevel@tonic-gate 		    octetstr(&rp6->ipv6RouteIfIndex, 'a',
44417c478bd9Sstevel@tonic-gate 		    ifname, sizeof (ifname)),
44427c478bd9Sstevel@tonic-gate 		    rp6->ipv6RouteInfo.re_max_frag,
44437c478bd9Sstevel@tonic-gate 		    rp6->ipv6RouteInfo.re_frag_flag ? '*' : ' ',
44447c478bd9Sstevel@tonic-gate 		    rp6->ipv6RouteInfo.re_rtt,
44457c478bd9Sstevel@tonic-gate 		    rp6->ipv6RouteInfo.re_ref,
44467c478bd9Sstevel@tonic-gate 		    flags,
44477c478bd9Sstevel@tonic-gate 		    rp6->ipv6RouteInfo.re_obpkt,
444845916cd2Sjpk 		    rp6->ipv6RouteInfo.re_ibpkt,
444945916cd2Sjpk 		    pr_secattr(attrs));
44507c478bd9Sstevel@tonic-gate 	} else {
4451aecc8c24Sja 		(void) printf("%-27s %-27s %-5s %3u %7u %-5s %s\n",
44527c478bd9Sstevel@tonic-gate 		    pr_prefix6(&rp6->ipv6RouteDest,
44537c478bd9Sstevel@tonic-gate 			rp6->ipv6RoutePfxLength, dstbuf, sizeof (dstbuf)),
44547c478bd9Sstevel@tonic-gate 		    IN6_IS_ADDR_UNSPECIFIED(&rp6->ipv6RouteNextHop) ?
44557c478bd9Sstevel@tonic-gate 		    "    --" :
44567c478bd9Sstevel@tonic-gate 		    pr_addr6(&rp6->ipv6RouteNextHop, gwbuf, sizeof (gwbuf)),
44577c478bd9Sstevel@tonic-gate 		    flags,
44587c478bd9Sstevel@tonic-gate 		    rp6->ipv6RouteInfo.re_ref,
44597c478bd9Sstevel@tonic-gate 		    rp6->ipv6RouteInfo.re_obpkt + rp6->ipv6RouteInfo.re_ibpkt,
44607c478bd9Sstevel@tonic-gate 		    octetstr(&rp6->ipv6RouteIfIndex, 'a',
446145916cd2Sjpk 		    ifname, sizeof (ifname)),
446245916cd2Sjpk 		    pr_secattr(attrs));
44637c478bd9Sstevel@tonic-gate 	}
44647c478bd9Sstevel@tonic-gate 	return (first);
44657c478bd9Sstevel@tonic-gate }
44667c478bd9Sstevel@tonic-gate 
446745916cd2Sjpk /*
446845916cd2Sjpk  * Common attribute-gathering routine for all transports.
446945916cd2Sjpk  */
447045916cd2Sjpk static mib2_transportMLPEntry_t **
447145916cd2Sjpk gather_attrs(const mib_item_t *item, int group, int mib_id, int esize)
447245916cd2Sjpk {
447345916cd2Sjpk 	int transport_count = 0;
447445916cd2Sjpk 	const mib_item_t *iptr;
447545916cd2Sjpk 	mib2_transportMLPEntry_t **attrs, *tme;
447645916cd2Sjpk 
447745916cd2Sjpk 	for (iptr = item; iptr != NULL; iptr = iptr->next_item) {
447845916cd2Sjpk 		if (iptr->group == group && iptr->mib_id == mib_id)
447945916cd2Sjpk 			transport_count += iptr->length / esize;
448045916cd2Sjpk 	}
448145916cd2Sjpk 	if (transport_count <= 0)
448245916cd2Sjpk 		return (NULL);
448345916cd2Sjpk 	attrs = calloc(transport_count, sizeof (*attrs));
448445916cd2Sjpk 	if (attrs == NULL) {
448545916cd2Sjpk 		perror("gather_attrs calloc failed");
448645916cd2Sjpk 		return (NULL);
448745916cd2Sjpk 	}
448845916cd2Sjpk 	for (iptr = item; iptr != NULL; iptr = iptr->next_item) {
448945916cd2Sjpk 		if (iptr->group == group && iptr->mib_id == EXPER_XPORT_MLP) {
449045916cd2Sjpk 			for (tme = iptr->valp;
449145916cd2Sjpk 			    (char *)tme < (char *)iptr->valp + iptr->length;
449245916cd2Sjpk 			    /* LINTED: (note 1) */
449345916cd2Sjpk 			    tme = (mib2_transportMLPEntry_t *)((char *)tme +
449445916cd2Sjpk 			    transportMLPSize)) {
449545916cd2Sjpk 				attrs[tme->tme_connidx] = tme;
449645916cd2Sjpk 			}
449745916cd2Sjpk 		}
449845916cd2Sjpk 	}
449945916cd2Sjpk 	return (attrs);
450045916cd2Sjpk }
450145916cd2Sjpk 
450245916cd2Sjpk static void
450345916cd2Sjpk print_transport_label(const mib2_transportMLPEntry_t *attr)
450445916cd2Sjpk {
450545916cd2Sjpk 	if (!RSECflag || attr == NULL)
450645916cd2Sjpk 		return;
450745916cd2Sjpk 
450845916cd2Sjpk 	if (bisinvalid(&attr->tme_label))
450945916cd2Sjpk 		(void) printf("   INVALID\n");
451045916cd2Sjpk 	else
451145916cd2Sjpk 		(void) printf("   %s\n", sl_to_str(&attr->tme_label));
451245916cd2Sjpk }
451345916cd2Sjpk 
45147c478bd9Sstevel@tonic-gate /* ------------------------------ TCP_REPORT------------------------------- */
45157c478bd9Sstevel@tonic-gate 
45167c478bd9Sstevel@tonic-gate static const char tcp_hdr_v4[] =
45177c478bd9Sstevel@tonic-gate "\nTCP: IPv4\n";
45187c478bd9Sstevel@tonic-gate static const char tcp_hdr_v4_compat[] =
45197c478bd9Sstevel@tonic-gate "\nTCP\n";
45207c478bd9Sstevel@tonic-gate static const char tcp_hdr_v4_verbose[] =
45217c478bd9Sstevel@tonic-gate "Local/Remote Address Swind  Snext     Suna   Rwind  Rnext     Rack   "
452245916cd2Sjpk " Rto   Mss     State\n"
45237c478bd9Sstevel@tonic-gate "-------------------- ----- -------- -------- ----- -------- -------- "
452445916cd2Sjpk "----- ----- -----------\n";
45257c478bd9Sstevel@tonic-gate static const char tcp_hdr_v4_normal[] =
452645916cd2Sjpk "   Local Address        Remote Address    Swind Send-Q Rwind Recv-Q "
452745916cd2Sjpk "   State\n"
452845916cd2Sjpk "-------------------- -------------------- ----- ------ ----- ------ "
452945916cd2Sjpk "-----------\n";
45307c478bd9Sstevel@tonic-gate 
45317c478bd9Sstevel@tonic-gate static const char tcp_hdr_v6[] =
45327c478bd9Sstevel@tonic-gate "\nTCP: IPv6\n";
45337c478bd9Sstevel@tonic-gate static const char tcp_hdr_v6_verbose[] =
45347c478bd9Sstevel@tonic-gate "Local/Remote Address              Swind  Snext     Suna   Rwind  Rnext   "
453545916cd2Sjpk "  Rack    Rto   Mss    State      If\n"
45367c478bd9Sstevel@tonic-gate "--------------------------------- ----- -------- -------- ----- -------- "
45377c478bd9Sstevel@tonic-gate "-------- ----- ----- ----------- -----\n";
45387c478bd9Sstevel@tonic-gate static const char tcp_hdr_v6_normal[] =
45397c478bd9Sstevel@tonic-gate "   Local Address                     Remote Address                 "
454045916cd2Sjpk "Swind Send-Q Rwind Recv-Q   State      If\n"
45417c478bd9Sstevel@tonic-gate "--------------------------------- --------------------------------- "
45427c478bd9Sstevel@tonic-gate "----- ------ ----- ------ ----------- -----\n";
45437c478bd9Sstevel@tonic-gate 
454445916cd2Sjpk static boolean_t tcp_report_item_v4(const mib2_tcpConnEntry_t *,
454545916cd2Sjpk     boolean_t first, const mib2_transportMLPEntry_t *);
454645916cd2Sjpk static boolean_t tcp_report_item_v6(const mib2_tcp6ConnEntry_t *,
454745916cd2Sjpk     boolean_t first, const mib2_transportMLPEntry_t *);
45487c478bd9Sstevel@tonic-gate 
45497c478bd9Sstevel@tonic-gate static void
455045916cd2Sjpk tcp_report(const mib_item_t *item)
45517c478bd9Sstevel@tonic-gate {
45527c478bd9Sstevel@tonic-gate 	int			jtemp = 0;
45537c478bd9Sstevel@tonic-gate 	boolean_t		print_hdr_once_v4 = B_TRUE;
45547c478bd9Sstevel@tonic-gate 	boolean_t		print_hdr_once_v6 = B_TRUE;
45557c478bd9Sstevel@tonic-gate 	mib2_tcpConnEntry_t	*tp;
45567c478bd9Sstevel@tonic-gate 	mib2_tcp6ConnEntry_t	*tp6;
455745916cd2Sjpk 	mib2_transportMLPEntry_t **v4_attrs, **v6_attrs;
455845916cd2Sjpk 	mib2_transportMLPEntry_t **v4a, **v6a;
455945916cd2Sjpk 	mib2_transportMLPEntry_t *aptr;
45607c478bd9Sstevel@tonic-gate 
45617c478bd9Sstevel@tonic-gate 	if (!protocol_selected(IPPROTO_TCP))
45627c478bd9Sstevel@tonic-gate 		return;
45637c478bd9Sstevel@tonic-gate 
456445916cd2Sjpk 	/*
456545916cd2Sjpk 	 * Preparation pass: the kernel returns separate entries for TCP
456645916cd2Sjpk 	 * connection table entries and Multilevel Port attributes.  We loop
456745916cd2Sjpk 	 * through the attributes first and set up an array for each address
456845916cd2Sjpk 	 * family.
456945916cd2Sjpk 	 */
457045916cd2Sjpk 	v4_attrs = family_selected(AF_INET) && RSECflag ?
457145916cd2Sjpk 	    gather_attrs(item, MIB2_TCP, MIB2_TCP_CONN, tcpConnEntrySize) :
457245916cd2Sjpk 	    NULL;
457345916cd2Sjpk 	v6_attrs = family_selected(AF_INET6) && RSECflag ?
457445916cd2Sjpk 	    gather_attrs(item, MIB2_TCP6, MIB2_TCP6_CONN, tcp6ConnEntrySize) :
457545916cd2Sjpk 	    NULL;
457645916cd2Sjpk 
45777c478bd9Sstevel@tonic-gate 	/* 'for' loop 1: */
457845916cd2Sjpk 	v4a = v4_attrs;
457945916cd2Sjpk 	v6a = v6_attrs;
458045916cd2Sjpk 	for (; item != NULL; item = item->next_item) {
45817c478bd9Sstevel@tonic-gate 		if (Dflag) {
45827c478bd9Sstevel@tonic-gate 			(void) printf("\n--- Entry %d ---\n", ++jtemp);
45837c478bd9Sstevel@tonic-gate 			(void) printf("Group = %d, mib_id = %d, "
45847c478bd9Sstevel@tonic-gate 			    "length = %d, valp = 0x%p\n",
45857c478bd9Sstevel@tonic-gate 			    item->group, item->mib_id,
45867c478bd9Sstevel@tonic-gate 			    item->length, item->valp);
45877c478bd9Sstevel@tonic-gate 		}
45887c478bd9Sstevel@tonic-gate 
45897c478bd9Sstevel@tonic-gate 		if (!((item->group == MIB2_TCP &&
45907c478bd9Sstevel@tonic-gate 		    item->mib_id == MIB2_TCP_CONN) ||
45917c478bd9Sstevel@tonic-gate 		    (item->group == MIB2_TCP6 &&
45927c478bd9Sstevel@tonic-gate 		    item->mib_id == MIB2_TCP6_CONN)))
45937c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
45947c478bd9Sstevel@tonic-gate 
45957c478bd9Sstevel@tonic-gate 		if (item->group == MIB2_TCP && !family_selected(AF_INET))
45967c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
45977c478bd9Sstevel@tonic-gate 		else if (item->group == MIB2_TCP6 && !family_selected(AF_INET6))
45987c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
45997c478bd9Sstevel@tonic-gate 
46007c478bd9Sstevel@tonic-gate 		if (item->group == MIB2_TCP) {
46017c478bd9Sstevel@tonic-gate 			for (tp = (mib2_tcpConnEntry_t *)item->valp;
46027c478bd9Sstevel@tonic-gate 			    (char *)tp < (char *)item->valp + item->length;
46037c478bd9Sstevel@tonic-gate 			    /* LINTED: (note 1) */
46047c478bd9Sstevel@tonic-gate 			    tp = (mib2_tcpConnEntry_t *)((char *)tp +
46057c478bd9Sstevel@tonic-gate 			    tcpConnEntrySize)) {
460645916cd2Sjpk 				aptr = v4a == NULL ? NULL : *v4a++;
46077c478bd9Sstevel@tonic-gate 				print_hdr_once_v4 = tcp_report_item_v4(tp,
460845916cd2Sjpk 				    print_hdr_once_v4, aptr);
46097c478bd9Sstevel@tonic-gate 			}
46107c478bd9Sstevel@tonic-gate 		} else {
46117c478bd9Sstevel@tonic-gate 			for (tp6 = (mib2_tcp6ConnEntry_t *)item->valp;
46127c478bd9Sstevel@tonic-gate 			    (char *)tp6 < (char *)item->valp + item->length;
46137c478bd9Sstevel@tonic-gate 			    /* LINTED: (note 1) */
46147c478bd9Sstevel@tonic-gate 			    tp6 = (mib2_tcp6ConnEntry_t *)((char *)tp6 +
46157c478bd9Sstevel@tonic-gate 			    tcp6ConnEntrySize)) {
461645916cd2Sjpk 				aptr = v6a == NULL ? NULL : *v6a++;
46177c478bd9Sstevel@tonic-gate 				print_hdr_once_v6 = tcp_report_item_v6(tp6,
461845916cd2Sjpk 				    print_hdr_once_v6, aptr);
46197c478bd9Sstevel@tonic-gate 			}
46207c478bd9Sstevel@tonic-gate 		}
46217c478bd9Sstevel@tonic-gate 	} /* 'for' loop 1 ends */
46227c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
462345916cd2Sjpk 
462445916cd2Sjpk 	if (v4_attrs != NULL)
462545916cd2Sjpk 		free(v4_attrs);
462645916cd2Sjpk 	if (v6_attrs != NULL)
462745916cd2Sjpk 		free(v6_attrs);
46287c478bd9Sstevel@tonic-gate }
46297c478bd9Sstevel@tonic-gate 
46307c478bd9Sstevel@tonic-gate static boolean_t
463145916cd2Sjpk tcp_report_item_v4(const mib2_tcpConnEntry_t *tp, boolean_t first,
463245916cd2Sjpk     const mib2_transportMLPEntry_t *attr)
46337c478bd9Sstevel@tonic-gate {
46347c478bd9Sstevel@tonic-gate 	/*
46357c478bd9Sstevel@tonic-gate 	 * lname and fname below are for the hostname as well as the portname
46367c478bd9Sstevel@tonic-gate 	 * There is no limit on portname length so we assume MAXHOSTNAMELEN
46377c478bd9Sstevel@tonic-gate 	 * as the limit
46387c478bd9Sstevel@tonic-gate 	 */
46397c478bd9Sstevel@tonic-gate 	char	lname[MAXHOSTNAMELEN + MAXHOSTNAMELEN + 1];
46407c478bd9Sstevel@tonic-gate 	char	fname[MAXHOSTNAMELEN + MAXHOSTNAMELEN + 1];
46417c478bd9Sstevel@tonic-gate 
46427c478bd9Sstevel@tonic-gate 	if (!(Aflag || tp->tcpConnEntryInfo.ce_state >= TCPS_ESTABLISHED))
46437c478bd9Sstevel@tonic-gate 		return (first); /* Nothing to print */
46447c478bd9Sstevel@tonic-gate 
46457c478bd9Sstevel@tonic-gate 	if (first) {
464645916cd2Sjpk 		(void) printf(v4compat ? tcp_hdr_v4_compat : tcp_hdr_v4);
464745916cd2Sjpk 		(void) printf(Vflag ? tcp_hdr_v4_verbose : tcp_hdr_v4_normal);
46487c478bd9Sstevel@tonic-gate 	}
46497c478bd9Sstevel@tonic-gate 
46507c478bd9Sstevel@tonic-gate 	if (Vflag) {
46517c478bd9Sstevel@tonic-gate 		(void) printf("%-20s\n%-20s %5u %08x %08x %5u %08x %08x "
46527c478bd9Sstevel@tonic-gate 		    "%5u %5u %s\n",
46537c478bd9Sstevel@tonic-gate 		    pr_ap(tp->tcpConnLocalAddress,
46547c478bd9Sstevel@tonic-gate 			tp->tcpConnLocalPort, "tcp", lname, sizeof (lname)),
46557c478bd9Sstevel@tonic-gate 		    pr_ap(tp->tcpConnRemAddress,
46567c478bd9Sstevel@tonic-gate 			tp->tcpConnRemPort, "tcp", fname, sizeof (fname)),
46577c478bd9Sstevel@tonic-gate 		    tp->tcpConnEntryInfo.ce_swnd,
46587c478bd9Sstevel@tonic-gate 		    tp->tcpConnEntryInfo.ce_snxt,
46597c478bd9Sstevel@tonic-gate 		    tp->tcpConnEntryInfo.ce_suna,
46607c478bd9Sstevel@tonic-gate 		    tp->tcpConnEntryInfo.ce_rwnd,
46617c478bd9Sstevel@tonic-gate 		    tp->tcpConnEntryInfo.ce_rnxt,
46627c478bd9Sstevel@tonic-gate 		    tp->tcpConnEntryInfo.ce_rack,
46637c478bd9Sstevel@tonic-gate 		    tp->tcpConnEntryInfo.ce_rto,
46647c478bd9Sstevel@tonic-gate 		    tp->tcpConnEntryInfo.ce_mss,
466545916cd2Sjpk 		    mitcp_state(tp->tcpConnEntryInfo.ce_state, attr));
46667c478bd9Sstevel@tonic-gate 	} else {
46677c478bd9Sstevel@tonic-gate 		int sq = (int)tp->tcpConnEntryInfo.ce_snxt -
46687c478bd9Sstevel@tonic-gate 		    (int)tp->tcpConnEntryInfo.ce_suna - 1;
46697c478bd9Sstevel@tonic-gate 		int rq = (int)tp->tcpConnEntryInfo.ce_rnxt -
46707c478bd9Sstevel@tonic-gate 		    (int)tp->tcpConnEntryInfo.ce_rack;
46717c478bd9Sstevel@tonic-gate 
46727c478bd9Sstevel@tonic-gate 		(void) printf("%-20s %-20s %5u %6d %5u %6d %s\n",
46737c478bd9Sstevel@tonic-gate 		    pr_ap(tp->tcpConnLocalAddress,
46747c478bd9Sstevel@tonic-gate 			tp->tcpConnLocalPort, "tcp", lname, sizeof (lname)),
46757c478bd9Sstevel@tonic-gate 		    pr_ap(tp->tcpConnRemAddress,
46767c478bd9Sstevel@tonic-gate 			tp->tcpConnRemPort, "tcp", fname, sizeof (fname)),
46777c478bd9Sstevel@tonic-gate 		    tp->tcpConnEntryInfo.ce_swnd,
46787c478bd9Sstevel@tonic-gate 		    (sq >= 0) ? sq : 0,
46797c478bd9Sstevel@tonic-gate 		    tp->tcpConnEntryInfo.ce_rwnd,
46807c478bd9Sstevel@tonic-gate 		    (rq >= 0) ? rq : 0,
468145916cd2Sjpk 		    mitcp_state(tp->tcpConnEntryInfo.ce_state, attr));
46827c478bd9Sstevel@tonic-gate 	}
468345916cd2Sjpk 
468445916cd2Sjpk 	print_transport_label(attr);
468545916cd2Sjpk 
468645916cd2Sjpk 	return (B_FALSE);
46877c478bd9Sstevel@tonic-gate }
46887c478bd9Sstevel@tonic-gate 
46897c478bd9Sstevel@tonic-gate static boolean_t
469045916cd2Sjpk tcp_report_item_v6(const mib2_tcp6ConnEntry_t *tp6, boolean_t first,
469145916cd2Sjpk     const mib2_transportMLPEntry_t *attr)
46927c478bd9Sstevel@tonic-gate {
46937c478bd9Sstevel@tonic-gate 	/*
46947c478bd9Sstevel@tonic-gate 	 * lname and fname below are for the hostname as well as the portname
46957c478bd9Sstevel@tonic-gate 	 * There is no limit on portname length so we assume MAXHOSTNAMELEN
46967c478bd9Sstevel@tonic-gate 	 * as the limit
46977c478bd9Sstevel@tonic-gate 	 */
46987c478bd9Sstevel@tonic-gate 	char	lname[MAXHOSTNAMELEN + MAXHOSTNAMELEN + 1];
46997c478bd9Sstevel@tonic-gate 	char	fname[MAXHOSTNAMELEN + MAXHOSTNAMELEN + 1];
47007c478bd9Sstevel@tonic-gate 	char	ifname[LIFNAMSIZ + 1];
47017c478bd9Sstevel@tonic-gate 	char	*ifnamep;
47027c478bd9Sstevel@tonic-gate 
47037c478bd9Sstevel@tonic-gate 	if (!(Aflag || tp6->tcp6ConnEntryInfo.ce_state >= TCPS_ESTABLISHED))
47047c478bd9Sstevel@tonic-gate 		return (first); /* Nothing to print */
47057c478bd9Sstevel@tonic-gate 
47067c478bd9Sstevel@tonic-gate 	if (first) {
470745916cd2Sjpk 		(void) printf(tcp_hdr_v6);
470845916cd2Sjpk 		(void) printf(Vflag ? tcp_hdr_v6_verbose : tcp_hdr_v6_normal);
47097c478bd9Sstevel@tonic-gate 	}
47107c478bd9Sstevel@tonic-gate 
47117c478bd9Sstevel@tonic-gate 	ifnamep = (tp6->tcp6ConnIfIndex != 0) ?
47127c478bd9Sstevel@tonic-gate 	    if_indextoname(tp6->tcp6ConnIfIndex, ifname) : NULL;
471345916cd2Sjpk 	if (ifnamep == NULL)
471445916cd2Sjpk 		ifnamep = "";
47157c478bd9Sstevel@tonic-gate 
47167c478bd9Sstevel@tonic-gate 	if (Vflag) {
47177c478bd9Sstevel@tonic-gate 		(void) printf("%-33s\n%-33s %5u %08x %08x %5u %08x %08x "
471845916cd2Sjpk 		    "%5u %5u %-11s %s\n",
47197c478bd9Sstevel@tonic-gate 		    pr_ap6(&tp6->tcp6ConnLocalAddress,
47207c478bd9Sstevel@tonic-gate 			tp6->tcp6ConnLocalPort, "tcp", lname, sizeof (lname)),
47217c478bd9Sstevel@tonic-gate 		    pr_ap6(&tp6->tcp6ConnRemAddress,
47227c478bd9Sstevel@tonic-gate 			tp6->tcp6ConnRemPort, "tcp", fname, sizeof (fname)),
47237c478bd9Sstevel@tonic-gate 		    tp6->tcp6ConnEntryInfo.ce_swnd,
47247c478bd9Sstevel@tonic-gate 		    tp6->tcp6ConnEntryInfo.ce_snxt,
47257c478bd9Sstevel@tonic-gate 		    tp6->tcp6ConnEntryInfo.ce_suna,
47267c478bd9Sstevel@tonic-gate 		    tp6->tcp6ConnEntryInfo.ce_rwnd,
47277c478bd9Sstevel@tonic-gate 		    tp6->tcp6ConnEntryInfo.ce_rnxt,
47287c478bd9Sstevel@tonic-gate 		    tp6->tcp6ConnEntryInfo.ce_rack,
47297c478bd9Sstevel@tonic-gate 		    tp6->tcp6ConnEntryInfo.ce_rto,
47307c478bd9Sstevel@tonic-gate 		    tp6->tcp6ConnEntryInfo.ce_mss,
473145916cd2Sjpk 		    mitcp_state(tp6->tcp6ConnEntryInfo.ce_state, attr),
473245916cd2Sjpk 		    ifnamep);
47337c478bd9Sstevel@tonic-gate 	} else {
47347c478bd9Sstevel@tonic-gate 		int sq = (int)tp6->tcp6ConnEntryInfo.ce_snxt -
47357c478bd9Sstevel@tonic-gate 		    (int)tp6->tcp6ConnEntryInfo.ce_suna - 1;
47367c478bd9Sstevel@tonic-gate 		int rq = (int)tp6->tcp6ConnEntryInfo.ce_rnxt -
47377c478bd9Sstevel@tonic-gate 		    (int)tp6->tcp6ConnEntryInfo.ce_rack;
47387c478bd9Sstevel@tonic-gate 
473945916cd2Sjpk 		(void) printf("%-33s %-33s %5u %6d %5u %6d %-11s %s\n",
47407c478bd9Sstevel@tonic-gate 		    pr_ap6(&tp6->tcp6ConnLocalAddress,
47417c478bd9Sstevel@tonic-gate 			tp6->tcp6ConnLocalPort, "tcp", lname, sizeof (lname)),
47427c478bd9Sstevel@tonic-gate 		    pr_ap6(&tp6->tcp6ConnRemAddress,
47437c478bd9Sstevel@tonic-gate 			tp6->tcp6ConnRemPort, "tcp", fname, sizeof (fname)),
47447c478bd9Sstevel@tonic-gate 		    tp6->tcp6ConnEntryInfo.ce_swnd,
47457c478bd9Sstevel@tonic-gate 		    (sq >= 0) ? sq : 0,
47467c478bd9Sstevel@tonic-gate 		    tp6->tcp6ConnEntryInfo.ce_rwnd,
47477c478bd9Sstevel@tonic-gate 		    (rq >= 0) ? rq : 0,
474845916cd2Sjpk 		    mitcp_state(tp6->tcp6ConnEntryInfo.ce_state, attr),
474945916cd2Sjpk 		    ifnamep);
47507c478bd9Sstevel@tonic-gate 	}
475145916cd2Sjpk 
475245916cd2Sjpk 	print_transport_label(attr);
475345916cd2Sjpk 
475445916cd2Sjpk 	return (B_FALSE);
47557c478bd9Sstevel@tonic-gate }
47567c478bd9Sstevel@tonic-gate 
47577c478bd9Sstevel@tonic-gate /* ------------------------------- UDP_REPORT------------------------------- */
47587c478bd9Sstevel@tonic-gate 
475945916cd2Sjpk static boolean_t udp_report_item_v4(const mib2_udpEntry_t *ude,
476045916cd2Sjpk     boolean_t first, const mib2_transportMLPEntry_t *attr);
476145916cd2Sjpk static boolean_t udp_report_item_v6(const mib2_udp6Entry_t *ude6,
476245916cd2Sjpk     boolean_t first, const mib2_transportMLPEntry_t *attr);
476345916cd2Sjpk 
476445916cd2Sjpk static const char udp_hdr_v4[] =
476545916cd2Sjpk "   Local Address        Remote Address      State\n"
476645916cd2Sjpk "-------------------- -------------------- ----------\n";
47677c478bd9Sstevel@tonic-gate 
476845916cd2Sjpk static const char udp_hdr_v6[] =
47697c478bd9Sstevel@tonic-gate "   Local Address                     Remote Address                 "
477045916cd2Sjpk "  State      If\n"
47717c478bd9Sstevel@tonic-gate "--------------------------------- --------------------------------- "
47727c478bd9Sstevel@tonic-gate "---------- -----\n";
47737c478bd9Sstevel@tonic-gate 
47747c478bd9Sstevel@tonic-gate static void
477545916cd2Sjpk udp_report(const mib_item_t *item)
47767c478bd9Sstevel@tonic-gate {
47777c478bd9Sstevel@tonic-gate 	int			jtemp = 0;
47787c478bd9Sstevel@tonic-gate 	boolean_t		print_hdr_once_v4 = B_TRUE;
47797c478bd9Sstevel@tonic-gate 	boolean_t		print_hdr_once_v6 = B_TRUE;
47807c478bd9Sstevel@tonic-gate 	mib2_udpEntry_t		*ude;
47817c478bd9Sstevel@tonic-gate 	mib2_udp6Entry_t	*ude6;
478245916cd2Sjpk 	mib2_transportMLPEntry_t **v4_attrs, **v6_attrs;
478345916cd2Sjpk 	mib2_transportMLPEntry_t **v4a, **v6a;
478445916cd2Sjpk 	mib2_transportMLPEntry_t *aptr;
47857c478bd9Sstevel@tonic-gate 
47867c478bd9Sstevel@tonic-gate 	if (!protocol_selected(IPPROTO_UDP))
47877c478bd9Sstevel@tonic-gate 		return;
47887c478bd9Sstevel@tonic-gate 
478945916cd2Sjpk 	/*
479045916cd2Sjpk 	 * Preparation pass: the kernel returns separate entries for UDP
479145916cd2Sjpk 	 * connection table entries and Multilevel Port attributes.  We loop
479245916cd2Sjpk 	 * through the attributes first and set up an array for each address
479345916cd2Sjpk 	 * family.
479445916cd2Sjpk 	 */
479545916cd2Sjpk 	v4_attrs = family_selected(AF_INET) && RSECflag ?
479645916cd2Sjpk 	    gather_attrs(item, MIB2_UDP, MIB2_UDP_ENTRY, udpEntrySize) : NULL;
479745916cd2Sjpk 	v6_attrs = family_selected(AF_INET6) && RSECflag ?
479845916cd2Sjpk 	    gather_attrs(item, MIB2_UDP6, MIB2_UDP6_ENTRY, udp6EntrySize) :
479945916cd2Sjpk 	    NULL;
480045916cd2Sjpk 
480145916cd2Sjpk 	v4a = v4_attrs;
480245916cd2Sjpk 	v6a = v6_attrs;
48037c478bd9Sstevel@tonic-gate 	/* 'for' loop 1: */
48047c478bd9Sstevel@tonic-gate 	for (; item; item = item->next_item) {
48057c478bd9Sstevel@tonic-gate 		if (Dflag) {
48067c478bd9Sstevel@tonic-gate 			(void) printf("\n--- Entry %d ---\n", ++jtemp);
48077c478bd9Sstevel@tonic-gate 			(void) printf("Group = %d, mib_id = %d, "
48087c478bd9Sstevel@tonic-gate 			    "length = %d, valp = 0x%p\n",
48097c478bd9Sstevel@tonic-gate 			    item->group, item->mib_id,
48107c478bd9Sstevel@tonic-gate 			    item->length, item->valp);
48117c478bd9Sstevel@tonic-gate 		}
48127c478bd9Sstevel@tonic-gate 		if (!((item->group == MIB2_UDP &&
48137c478bd9Sstevel@tonic-gate 		    item->mib_id == MIB2_UDP_ENTRY) ||
48147c478bd9Sstevel@tonic-gate 		    (item->group == MIB2_UDP6 &&
48157c478bd9Sstevel@tonic-gate 		    item->mib_id == MIB2_UDP6_ENTRY)))
48167c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
48177c478bd9Sstevel@tonic-gate 
48187c478bd9Sstevel@tonic-gate 		if (item->group == MIB2_UDP && !family_selected(AF_INET))
48197c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
48207c478bd9Sstevel@tonic-gate 		else if (item->group == MIB2_UDP6 && !family_selected(AF_INET6))
48217c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
48227c478bd9Sstevel@tonic-gate 
48237c478bd9Sstevel@tonic-gate 		/*	xxx.xxx.xxx.xxx,pppp  sss... */
48247c478bd9Sstevel@tonic-gate 		if (item->group == MIB2_UDP) {
48257c478bd9Sstevel@tonic-gate 			for (ude = (mib2_udpEntry_t *)item->valp;
48267c478bd9Sstevel@tonic-gate 			    (char *)ude < (char *)item->valp + item->length;
48277c478bd9Sstevel@tonic-gate 			    /* LINTED: (note 1) */
48287c478bd9Sstevel@tonic-gate 			    ude = (mib2_udpEntry_t *)((char *)ude +
48297c478bd9Sstevel@tonic-gate 			    udpEntrySize)) {
483045916cd2Sjpk 				aptr = v4a == NULL ? NULL : *v4a++;
48317c478bd9Sstevel@tonic-gate 				print_hdr_once_v4 = udp_report_item_v4(ude,
483245916cd2Sjpk 				    print_hdr_once_v4, aptr);
48337c478bd9Sstevel@tonic-gate 			}
48347c478bd9Sstevel@tonic-gate 		} else {
48357c478bd9Sstevel@tonic-gate 			for (ude6 = (mib2_udp6Entry_t *)item->valp;
48367c478bd9Sstevel@tonic-gate 			    (char *)ude6 < (char *)item->valp + item->length;
48377c478bd9Sstevel@tonic-gate 			    /* LINTED: (note 1) */
48387c478bd9Sstevel@tonic-gate 			    ude6 = (mib2_udp6Entry_t *)((char *)ude6 +
48397c478bd9Sstevel@tonic-gate 			    udp6EntrySize)) {
484045916cd2Sjpk 				aptr = v6a == NULL ? NULL : *v6a++;
48417c478bd9Sstevel@tonic-gate 				print_hdr_once_v6 = udp_report_item_v6(ude6,
484245916cd2Sjpk 				    print_hdr_once_v6, aptr);
48437c478bd9Sstevel@tonic-gate 			}
48447c478bd9Sstevel@tonic-gate 		}
48457c478bd9Sstevel@tonic-gate 	} /* 'for' loop 1 ends */
48467c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
484745916cd2Sjpk 
484845916cd2Sjpk 	if (v4_attrs != NULL)
484945916cd2Sjpk 		free(v4_attrs);
485045916cd2Sjpk 	if (v6_attrs != NULL)
485145916cd2Sjpk 		free(v6_attrs);
48527c478bd9Sstevel@tonic-gate }
48537c478bd9Sstevel@tonic-gate 
48547c478bd9Sstevel@tonic-gate static boolean_t
485545916cd2Sjpk udp_report_item_v4(const mib2_udpEntry_t *ude, boolean_t first,
485645916cd2Sjpk     const mib2_transportMLPEntry_t *attr)
48577c478bd9Sstevel@tonic-gate {
48587c478bd9Sstevel@tonic-gate 	char	lname[MAXHOSTNAMELEN + MAXHOSTNAMELEN + 1];
48597c478bd9Sstevel@tonic-gate 			/* hostname + portname */
48607c478bd9Sstevel@tonic-gate 
48617c478bd9Sstevel@tonic-gate 	if (!(Aflag || ude->udpEntryInfo.ue_state >= MIB2_UDP_connected))
48627c478bd9Sstevel@tonic-gate 		return (first); /* Nothing to print */
48637c478bd9Sstevel@tonic-gate 
48647c478bd9Sstevel@tonic-gate 	if (first) {
486545916cd2Sjpk 		(void) printf(v4compat ? "\nUDP\n" : "\nUDP: IPv4\n");
486645916cd2Sjpk 		(void) printf(udp_hdr_v4);
48677c478bd9Sstevel@tonic-gate 		first = B_FALSE;
48687c478bd9Sstevel@tonic-gate 	}
48697c478bd9Sstevel@tonic-gate 
48707c478bd9Sstevel@tonic-gate 	(void) printf("%-20s ",
48717c478bd9Sstevel@tonic-gate 	    pr_ap(ude->udpLocalAddress, ude->udpLocalPort, "udp",
48727c478bd9Sstevel@tonic-gate 	    lname, sizeof (lname)));
487345916cd2Sjpk 	(void) printf("%-20s %s\n",
487445916cd2Sjpk 	    ude->udpEntryInfo.ue_state == MIB2_UDP_connected ?
487545916cd2Sjpk 	    pr_ap(ude->udpEntryInfo.ue_RemoteAddress,
487645916cd2Sjpk 	    ude->udpEntryInfo.ue_RemotePort, "udp", lname, sizeof (lname)) :
487745916cd2Sjpk 	    "",
487845916cd2Sjpk 	    miudp_state(ude->udpEntryInfo.ue_state, attr));
487945916cd2Sjpk 
488045916cd2Sjpk 	/*
488145916cd2Sjpk 	 * UDP sockets don't have remote attributes, so there's no need to
488245916cd2Sjpk 	 * print them here.
488345916cd2Sjpk 	 */
488445916cd2Sjpk 
48857c478bd9Sstevel@tonic-gate 	return (first);
48867c478bd9Sstevel@tonic-gate }
48877c478bd9Sstevel@tonic-gate 
48887c478bd9Sstevel@tonic-gate static boolean_t
488945916cd2Sjpk udp_report_item_v6(const mib2_udp6Entry_t *ude6, boolean_t first,
489045916cd2Sjpk     const mib2_transportMLPEntry_t *attr)
48917c478bd9Sstevel@tonic-gate {
48927c478bd9Sstevel@tonic-gate 	char	lname[MAXHOSTNAMELEN + MAXHOSTNAMELEN + 1];
48937c478bd9Sstevel@tonic-gate 			/* hostname + portname */
48947c478bd9Sstevel@tonic-gate 	char	ifname[LIFNAMSIZ + 1];
489545916cd2Sjpk 	const char *ifnamep;
48967c478bd9Sstevel@tonic-gate 
48977c478bd9Sstevel@tonic-gate 	if (!(Aflag || ude6->udp6EntryInfo.ue_state >= MIB2_UDP_connected))
48987c478bd9Sstevel@tonic-gate 		return (first); /* Nothing to print */
48997c478bd9Sstevel@tonic-gate 
49007c478bd9Sstevel@tonic-gate 	if (first) {
490145916cd2Sjpk 		(void) printf("\nUDP: IPv6\n");
490245916cd2Sjpk 		(void) printf(udp_hdr_v6);
49037c478bd9Sstevel@tonic-gate 		first = B_FALSE;
49047c478bd9Sstevel@tonic-gate 	}
49057c478bd9Sstevel@tonic-gate 
490645916cd2Sjpk 	ifnamep = (ude6->udp6IfIndex != 0) ?
490745916cd2Sjpk 	    if_indextoname(ude6->udp6IfIndex, ifname) : NULL;
490845916cd2Sjpk 
49097c478bd9Sstevel@tonic-gate 	(void) printf("%-33s ",
49107c478bd9Sstevel@tonic-gate 	    pr_ap6(&ude6->udp6LocalAddress,
49117c478bd9Sstevel@tonic-gate 	    ude6->udp6LocalPort, "udp", lname, sizeof (lname)));
491245916cd2Sjpk 	(void) printf("%-33s %-10s %s\n",
491345916cd2Sjpk 	    ude6->udp6EntryInfo.ue_state == MIB2_UDP_connected ?
491445916cd2Sjpk 	    pr_ap6(&ude6->udp6EntryInfo.ue_RemoteAddress,
491545916cd2Sjpk 	    ude6->udp6EntryInfo.ue_RemotePort, "udp", lname, sizeof (lname)) :
491645916cd2Sjpk 	    "",
491745916cd2Sjpk 	    miudp_state(ude6->udp6EntryInfo.ue_state, attr),
491845916cd2Sjpk 	    ifnamep == NULL ? "" : ifnamep);
491945916cd2Sjpk 
492045916cd2Sjpk 	/*
492145916cd2Sjpk 	 * UDP sockets don't have remote attributes, so there's no need to
492245916cd2Sjpk 	 * print them here.
492345916cd2Sjpk 	 */
492445916cd2Sjpk 
49257c478bd9Sstevel@tonic-gate 	return (first);
49267c478bd9Sstevel@tonic-gate }
49277c478bd9Sstevel@tonic-gate 
49287c478bd9Sstevel@tonic-gate /* ------------------------------ SCTP_REPORT------------------------------- */
49297c478bd9Sstevel@tonic-gate 
49307c478bd9Sstevel@tonic-gate static const char sctp_hdr[] =
49317c478bd9Sstevel@tonic-gate "\nSCTP:";
49327c478bd9Sstevel@tonic-gate static const char sctp_hdr_normal[] =
49337c478bd9Sstevel@tonic-gate "        Local Address                   Remote Address          "
49347c478bd9Sstevel@tonic-gate "Swind  Send-Q Rwind  Recv-Q StrsI/O  State\n"
49357c478bd9Sstevel@tonic-gate "------------------------------- ------------------------------- "
49367c478bd9Sstevel@tonic-gate "------ ------ ------ ------ ------- -----------";
49377c478bd9Sstevel@tonic-gate 
49387c478bd9Sstevel@tonic-gate static const char *
493945916cd2Sjpk nssctp_state(int state, const mib2_transportMLPEntry_t *attr)
49407c478bd9Sstevel@tonic-gate {
494145916cd2Sjpk 	static char sctpsbuf[50];
494245916cd2Sjpk 	const char *cp;
494345916cd2Sjpk 
49447c478bd9Sstevel@tonic-gate 	switch (state) {
49457c478bd9Sstevel@tonic-gate 	case MIB2_SCTP_closed:
494645916cd2Sjpk 		cp = "CLOSED";
494745916cd2Sjpk 		break;
49487c478bd9Sstevel@tonic-gate 	case MIB2_SCTP_cookieWait:
494945916cd2Sjpk 		cp = "COOKIE_WAIT";
495045916cd2Sjpk 		break;
49517c478bd9Sstevel@tonic-gate 	case MIB2_SCTP_cookieEchoed:
495245916cd2Sjpk 		cp = "COOKIE_ECHOED";
495345916cd2Sjpk 		break;
49547c478bd9Sstevel@tonic-gate 	case MIB2_SCTP_established:
495545916cd2Sjpk 		cp = "ESTABLISHED";
495645916cd2Sjpk 		break;
49577c478bd9Sstevel@tonic-gate 	case MIB2_SCTP_shutdownPending:
495845916cd2Sjpk 		cp = "SHUTDOWN_PENDING";
495945916cd2Sjpk 		break;
49607c478bd9Sstevel@tonic-gate 	case MIB2_SCTP_shutdownSent:
496145916cd2Sjpk 		cp = "SHUTDOWN_SENT";
496245916cd2Sjpk 		break;
49637c478bd9Sstevel@tonic-gate 	case MIB2_SCTP_shutdownReceived:
496445916cd2Sjpk 		cp = "SHUTDOWN_RECEIVED";
496545916cd2Sjpk 		break;
49667c478bd9Sstevel@tonic-gate 	case MIB2_SCTP_shutdownAckSent:
496745916cd2Sjpk 		cp = "SHUTDOWN_ACK_SENT";
496845916cd2Sjpk 		break;
49697c478bd9Sstevel@tonic-gate 	case MIB2_SCTP_listen:
497045916cd2Sjpk 		cp = "LISTEN";
497145916cd2Sjpk 		break;
49727c478bd9Sstevel@tonic-gate 	default:
497345916cd2Sjpk 		(void) snprintf(sctpsbuf, sizeof (sctpsbuf),
497445916cd2Sjpk 		    "UNKNOWN STATE(%d)", state);
497545916cd2Sjpk 		cp = sctpsbuf;
497645916cd2Sjpk 		break;
497745916cd2Sjpk 	}
497845916cd2Sjpk 
497945916cd2Sjpk 	if (RSECflag && attr != NULL && attr->tme_flags != 0) {
498045916cd2Sjpk 		if (cp != sctpsbuf) {
498145916cd2Sjpk 			(void) strlcpy(sctpsbuf, cp, sizeof (sctpsbuf));
498245916cd2Sjpk 			cp = sctpsbuf;
498345916cd2Sjpk 		}
498445916cd2Sjpk 		if (attr->tme_flags & MIB2_TMEF_PRIVATE)
498545916cd2Sjpk 			(void) strlcat(sctpsbuf, " P", sizeof (sctpsbuf));
498645916cd2Sjpk 		if (attr->tme_flags & MIB2_TMEF_SHARED)
498745916cd2Sjpk 			(void) strlcat(sctpsbuf, " S", sizeof (sctpsbuf));
49887c478bd9Sstevel@tonic-gate 	}
498945916cd2Sjpk 
499045916cd2Sjpk 	return (cp);
49917c478bd9Sstevel@tonic-gate }
49927c478bd9Sstevel@tonic-gate 
499345916cd2Sjpk static const mib2_sctpConnRemoteEntry_t *
499445916cd2Sjpk sctp_getnext_rem(const mib_item_t **itemp,
499545916cd2Sjpk     const mib2_sctpConnRemoteEntry_t *current, uint32_t associd)
49967c478bd9Sstevel@tonic-gate {
499745916cd2Sjpk 	const mib_item_t *item = *itemp;
499845916cd2Sjpk 	const mib2_sctpConnRemoteEntry_t	*sre;
49997c478bd9Sstevel@tonic-gate 
50007c478bd9Sstevel@tonic-gate 	for (; item != NULL; item = item->next_item, current = NULL) {
50017c478bd9Sstevel@tonic-gate 		if (!(item->group == MIB2_SCTP &&
50027c478bd9Sstevel@tonic-gate 		    item->mib_id == MIB2_SCTP_CONN_REMOTE)) {
50037c478bd9Sstevel@tonic-gate 			continue;
50047c478bd9Sstevel@tonic-gate 		}
50057c478bd9Sstevel@tonic-gate 
50067c478bd9Sstevel@tonic-gate 		if (current != NULL) {
50077c478bd9Sstevel@tonic-gate 			/* LINTED: (note 1) */
500845916cd2Sjpk 			sre = (const mib2_sctpConnRemoteEntry_t *)
500945916cd2Sjpk 			    ((const char *)current + sctpRemoteEntrySize);
50107c478bd9Sstevel@tonic-gate 		} else {
50117c478bd9Sstevel@tonic-gate 			sre = item->valp;
50127c478bd9Sstevel@tonic-gate 		}
50137c478bd9Sstevel@tonic-gate 		for (; (char *)sre < (char *)item->valp + item->length;
501445916cd2Sjpk 		    /* LINTED: (note 1) */
501545916cd2Sjpk 		    sre = (const mib2_sctpConnRemoteEntry_t *)
501645916cd2Sjpk 		    ((const char *)sre + sctpRemoteEntrySize)) {
50177c478bd9Sstevel@tonic-gate 			if (sre->sctpAssocId != associd) {
50187c478bd9Sstevel@tonic-gate 				continue;
50197c478bd9Sstevel@tonic-gate 			}
50207c478bd9Sstevel@tonic-gate 			*itemp = item;
50217c478bd9Sstevel@tonic-gate 			return (sre);
50227c478bd9Sstevel@tonic-gate 		}
50237c478bd9Sstevel@tonic-gate 	}
50247c478bd9Sstevel@tonic-gate 	*itemp = NULL;
50257c478bd9Sstevel@tonic-gate 	return (NULL);
50267c478bd9Sstevel@tonic-gate }
50277c478bd9Sstevel@tonic-gate 
502845916cd2Sjpk static const mib2_sctpConnLocalEntry_t *
502945916cd2Sjpk sctp_getnext_local(const mib_item_t **itemp,
503045916cd2Sjpk     const mib2_sctpConnLocalEntry_t *current, uint32_t associd)
50317c478bd9Sstevel@tonic-gate {
503245916cd2Sjpk 	const mib_item_t *item = *itemp;
503345916cd2Sjpk 	const mib2_sctpConnLocalEntry_t	*sle;
50347c478bd9Sstevel@tonic-gate 
50357c478bd9Sstevel@tonic-gate 	for (; item != NULL; item = item->next_item, current = NULL) {
50367c478bd9Sstevel@tonic-gate 		if (!(item->group == MIB2_SCTP &&
50377c478bd9Sstevel@tonic-gate 		    item->mib_id == MIB2_SCTP_CONN_LOCAL)) {
50387c478bd9Sstevel@tonic-gate 			continue;
50397c478bd9Sstevel@tonic-gate 		}
50407c478bd9Sstevel@tonic-gate 
50417c478bd9Sstevel@tonic-gate 		if (current != NULL) {
50427c478bd9Sstevel@tonic-gate 			/* LINTED: (note 1) */
504345916cd2Sjpk 			sle = (const mib2_sctpConnLocalEntry_t *)
504445916cd2Sjpk 			    ((const char *)current + sctpLocalEntrySize);
50457c478bd9Sstevel@tonic-gate 		} else {
50467c478bd9Sstevel@tonic-gate 			sle = item->valp;
50477c478bd9Sstevel@tonic-gate 		}
50487c478bd9Sstevel@tonic-gate 		for (; (char *)sle < (char *)item->valp + item->length;
504945916cd2Sjpk 		    /* LINTED: (note 1) */
505045916cd2Sjpk 		    sle = (const mib2_sctpConnLocalEntry_t *)
505145916cd2Sjpk 		    ((const char *)sle + sctpLocalEntrySize)) {
50527c478bd9Sstevel@tonic-gate 			if (sle->sctpAssocId != associd) {
50537c478bd9Sstevel@tonic-gate 				continue;
50547c478bd9Sstevel@tonic-gate 			}
50557c478bd9Sstevel@tonic-gate 			*itemp = item;
50567c478bd9Sstevel@tonic-gate 			return (sle);
50577c478bd9Sstevel@tonic-gate 		}
50587c478bd9Sstevel@tonic-gate 	}
50597c478bd9Sstevel@tonic-gate 	*itemp = NULL;
50607c478bd9Sstevel@tonic-gate 	return (NULL);
50617c478bd9Sstevel@tonic-gate }
50627c478bd9Sstevel@tonic-gate 
50637c478bd9Sstevel@tonic-gate static void
50647c478bd9Sstevel@tonic-gate sctp_pr_addr(int type, char *name, int namelen, const in6_addr_t *addr,
50657c478bd9Sstevel@tonic-gate     int port)
50667c478bd9Sstevel@tonic-gate {
50677c478bd9Sstevel@tonic-gate 	ipaddr_t	v4addr;
50687c478bd9Sstevel@tonic-gate 	in6_addr_t	v6addr;
50697c478bd9Sstevel@tonic-gate 
50707c478bd9Sstevel@tonic-gate 	/*
50717c478bd9Sstevel@tonic-gate 	 * Address is either a v4 mapped or v6 addr. If
50727c478bd9Sstevel@tonic-gate 	 * it's a v4 mapped, convert to v4 before
50737c478bd9Sstevel@tonic-gate 	 * displaying.
50747c478bd9Sstevel@tonic-gate 	 */
50757c478bd9Sstevel@tonic-gate 	switch (type) {
50767c478bd9Sstevel@tonic-gate 	    case MIB2_SCTP_ADDR_V4:
50777c478bd9Sstevel@tonic-gate 		/* v4 */
50787c478bd9Sstevel@tonic-gate 		v6addr = *addr;
50797c478bd9Sstevel@tonic-gate 
50807c478bd9Sstevel@tonic-gate 		IN6_V4MAPPED_TO_IPADDR(&v6addr, v4addr);
50817c478bd9Sstevel@tonic-gate 		if (port > 0) {
50827c478bd9Sstevel@tonic-gate 			(void) pr_ap(v4addr, port, "sctp", name, namelen);
50837c478bd9Sstevel@tonic-gate 		} else {
50847c478bd9Sstevel@tonic-gate 			(void) pr_addr(v4addr, name, namelen);
50857c478bd9Sstevel@tonic-gate 		}
50867c478bd9Sstevel@tonic-gate 		break;
50877c478bd9Sstevel@tonic-gate 
50887c478bd9Sstevel@tonic-gate 	    case MIB2_SCTP_ADDR_V6:
50897c478bd9Sstevel@tonic-gate 		/* v6 */
50907c478bd9Sstevel@tonic-gate 		if (port > 0) {
50917c478bd9Sstevel@tonic-gate 			(void) pr_ap6(addr, port, "sctp", name, namelen);
50927c478bd9Sstevel@tonic-gate 		} else {
50937c478bd9Sstevel@tonic-gate 			(void) pr_addr6(addr, name, namelen);
50947c478bd9Sstevel@tonic-gate 		}
50957c478bd9Sstevel@tonic-gate 		break;
50967c478bd9Sstevel@tonic-gate 
50977c478bd9Sstevel@tonic-gate 	    default:
50987c478bd9Sstevel@tonic-gate 		(void) snprintf(name, namelen, "<unknown addr type>");
50997c478bd9Sstevel@tonic-gate 		break;
51007c478bd9Sstevel@tonic-gate 	}
51017c478bd9Sstevel@tonic-gate }
51027c478bd9Sstevel@tonic-gate 
51037c478bd9Sstevel@tonic-gate static void
510445916cd2Sjpk sctp_conn_report_item(const mib_item_t *head, const mib2_sctpConnEntry_t *sp,
510545916cd2Sjpk     const mib2_transportMLPEntry_t *attr)
51067c478bd9Sstevel@tonic-gate {
51077c478bd9Sstevel@tonic-gate 	char		lname[MAXHOSTNAMELEN + MAXHOSTNAMELEN + 1];
51087c478bd9Sstevel@tonic-gate 	char		fname[MAXHOSTNAMELEN + MAXHOSTNAMELEN + 1];
510945916cd2Sjpk 	const mib2_sctpConnRemoteEntry_t	*sre = NULL;
511045916cd2Sjpk 	const mib2_sctpConnLocalEntry_t	*sle = NULL;
511145916cd2Sjpk 	const mib_item_t *local = head;
511245916cd2Sjpk 	const mib_item_t *remote = head;
51137c478bd9Sstevel@tonic-gate 	uint32_t	id = sp->sctpAssocId;
51147c478bd9Sstevel@tonic-gate 	boolean_t	printfirst = B_TRUE;
51157c478bd9Sstevel@tonic-gate 
51167c478bd9Sstevel@tonic-gate 	sctp_pr_addr(sp->sctpAssocRemPrimAddrType, fname, sizeof (fname),
51177c478bd9Sstevel@tonic-gate 	    &sp->sctpAssocRemPrimAddr, sp->sctpAssocRemPort);
51187c478bd9Sstevel@tonic-gate 	sctp_pr_addr(sp->sctpAssocRemPrimAddrType, lname, sizeof (lname),
51197c478bd9Sstevel@tonic-gate 	    &sp->sctpAssocLocPrimAddr, sp->sctpAssocLocalPort);
51207c478bd9Sstevel@tonic-gate 
51217c478bd9Sstevel@tonic-gate 	(void) printf("%-31s %-31s %6u %6d %6u %6d %3d/%-3d %s\n",
51227c478bd9Sstevel@tonic-gate 	    lname, fname,
51237c478bd9Sstevel@tonic-gate 	    sp->sctpConnEntryInfo.ce_swnd,
51247c478bd9Sstevel@tonic-gate 	    sp->sctpConnEntryInfo.ce_sendq,
51257c478bd9Sstevel@tonic-gate 	    sp->sctpConnEntryInfo.ce_rwnd,
51267c478bd9Sstevel@tonic-gate 	    sp->sctpConnEntryInfo.ce_recvq,
51277c478bd9Sstevel@tonic-gate 	    sp->sctpAssocInStreams, sp->sctpAssocOutStreams,
512845916cd2Sjpk 	    nssctp_state(sp->sctpAssocState, attr));
512945916cd2Sjpk 
513045916cd2Sjpk 	print_transport_label(attr);
51317c478bd9Sstevel@tonic-gate 
51327c478bd9Sstevel@tonic-gate 	if (!Vflag) {
51337c478bd9Sstevel@tonic-gate 		return;
51347c478bd9Sstevel@tonic-gate 	}
51357c478bd9Sstevel@tonic-gate 
51367c478bd9Sstevel@tonic-gate 	/* Print remote addresses/local addresses on following lines */
51377c478bd9Sstevel@tonic-gate 	while ((sre = sctp_getnext_rem(&remote, sre, id)) != NULL) {
51387c478bd9Sstevel@tonic-gate 		if (!IN6_ARE_ADDR_EQUAL(&sre->sctpAssocRemAddr,
51397c478bd9Sstevel@tonic-gate 		    &sp->sctpAssocRemPrimAddr)) {
51407c478bd9Sstevel@tonic-gate 			if (printfirst == B_TRUE) {
51417c478bd9Sstevel@tonic-gate 				(void) fputs("\t<Remote: ", stdout);
51427c478bd9Sstevel@tonic-gate 				printfirst = B_FALSE;
51437c478bd9Sstevel@tonic-gate 			} else {
51447c478bd9Sstevel@tonic-gate 				(void) fputs(", ", stdout);
51457c478bd9Sstevel@tonic-gate 			}
51467c478bd9Sstevel@tonic-gate 			sctp_pr_addr(sre->sctpAssocRemAddrType, fname,
51477c478bd9Sstevel@tonic-gate 			    sizeof (fname), &sre->sctpAssocRemAddr, -1);
51487c478bd9Sstevel@tonic-gate 			if (sre->sctpAssocRemAddrActive == MIB2_SCTP_ACTIVE) {
51497c478bd9Sstevel@tonic-gate 				(void) fputs(fname, stdout);
51507c478bd9Sstevel@tonic-gate 			} else {
51517c478bd9Sstevel@tonic-gate 				(void) printf("(%s)", fname);
51527c478bd9Sstevel@tonic-gate 			}
51537c478bd9Sstevel@tonic-gate 		}
51547c478bd9Sstevel@tonic-gate 	}
51557c478bd9Sstevel@tonic-gate 	if (printfirst == B_FALSE) {
51567c478bd9Sstevel@tonic-gate 		(void) puts(">");
51577c478bd9Sstevel@tonic-gate 		printfirst = B_TRUE;
51587c478bd9Sstevel@tonic-gate 	}
51597c478bd9Sstevel@tonic-gate 	while ((sle = sctp_getnext_local(&local, sle, id)) != NULL) {
51607c478bd9Sstevel@tonic-gate 		if (!IN6_ARE_ADDR_EQUAL(&sle->sctpAssocLocalAddr,
51617c478bd9Sstevel@tonic-gate 		    &sp->sctpAssocLocPrimAddr)) {
51627c478bd9Sstevel@tonic-gate 			if (printfirst == B_TRUE) {
51637c478bd9Sstevel@tonic-gate 				(void) fputs("\t<Local: ", stdout);
51647c478bd9Sstevel@tonic-gate 				printfirst = B_FALSE;
51657c478bd9Sstevel@tonic-gate 			} else {
51667c478bd9Sstevel@tonic-gate 				(void) fputs(", ", stdout);
51677c478bd9Sstevel@tonic-gate 			}
51687c478bd9Sstevel@tonic-gate 			sctp_pr_addr(sle->sctpAssocLocalAddrType, lname,
51697c478bd9Sstevel@tonic-gate 			    sizeof (lname), &sle->sctpAssocLocalAddr, -1);
51707c478bd9Sstevel@tonic-gate 			(void) fputs(lname, stdout);
51717c478bd9Sstevel@tonic-gate 		}
51727c478bd9Sstevel@tonic-gate 	}
51737c478bd9Sstevel@tonic-gate 	if (printfirst == B_FALSE) {
51747c478bd9Sstevel@tonic-gate 		(void) puts(">");
51757c478bd9Sstevel@tonic-gate 	}
51767c478bd9Sstevel@tonic-gate }
51777c478bd9Sstevel@tonic-gate 
51787c478bd9Sstevel@tonic-gate static void
517945916cd2Sjpk sctp_report(const mib_item_t *item)
51807c478bd9Sstevel@tonic-gate {
518145916cd2Sjpk 	const mib_item_t		*head;
518245916cd2Sjpk 	const mib2_sctpConnEntry_t	*sp;
51837c478bd9Sstevel@tonic-gate 	boolean_t		first = B_TRUE;
518445916cd2Sjpk 	mib2_transportMLPEntry_t **attrs, **aptr;
518545916cd2Sjpk 	mib2_transportMLPEntry_t *attr;
51867c478bd9Sstevel@tonic-gate 
518745916cd2Sjpk 	/*
518845916cd2Sjpk 	 * Preparation pass: the kernel returns separate entries for SCTP
518945916cd2Sjpk 	 * connection table entries and Multilevel Port attributes.  We loop
519045916cd2Sjpk 	 * through the attributes first and set up an array for each address
519145916cd2Sjpk 	 * family.
519245916cd2Sjpk 	 */
519345916cd2Sjpk 	attrs = RSECflag ?
519445916cd2Sjpk 	    gather_attrs(item, MIB2_SCTP, MIB2_SCTP_CONN, sctpEntrySize) :
519545916cd2Sjpk 	    NULL;
519645916cd2Sjpk 
519745916cd2Sjpk 	aptr = attrs;
51987c478bd9Sstevel@tonic-gate 	head = item;
51997c478bd9Sstevel@tonic-gate 	for (; item != NULL; item = item->next_item) {
52007c478bd9Sstevel@tonic-gate 
52017c478bd9Sstevel@tonic-gate 		if (!(item->group == MIB2_SCTP &&
52027c478bd9Sstevel@tonic-gate 		    item->mib_id == MIB2_SCTP_CONN))
52037c478bd9Sstevel@tonic-gate 			continue;
52047c478bd9Sstevel@tonic-gate 
52057c478bd9Sstevel@tonic-gate 		for (sp = item->valp;
520645916cd2Sjpk 		    (char *)sp < (char *)item->valp + item->length;
520745916cd2Sjpk 		    /* LINTED: (note 1) */
520845916cd2Sjpk 		    sp = (mib2_sctpConnEntry_t *)((char *)sp + sctpEntrySize)) {
520945916cd2Sjpk 			attr = aptr == NULL ? NULL : *aptr++;
52107c478bd9Sstevel@tonic-gate 			if (Aflag ||
52117c478bd9Sstevel@tonic-gate 			    sp->sctpAssocState >= MIB2_SCTP_established) {
52127c478bd9Sstevel@tonic-gate 				if (first == B_TRUE) {
52137c478bd9Sstevel@tonic-gate 					(void) puts(sctp_hdr);
52147c478bd9Sstevel@tonic-gate 					(void) puts(sctp_hdr_normal);
52157c478bd9Sstevel@tonic-gate 					first = B_FALSE;
52167c478bd9Sstevel@tonic-gate 				}
521745916cd2Sjpk 				sctp_conn_report_item(head, sp, attr);
52187c478bd9Sstevel@tonic-gate 			}
52197c478bd9Sstevel@tonic-gate 		}
52207c478bd9Sstevel@tonic-gate 	}
522145916cd2Sjpk 	if (attrs != NULL)
522245916cd2Sjpk 		free(attrs);
52237c478bd9Sstevel@tonic-gate }
52247c478bd9Sstevel@tonic-gate 
52257c478bd9Sstevel@tonic-gate static char *
52267c478bd9Sstevel@tonic-gate plural(int n)
52277c478bd9Sstevel@tonic-gate {
52287c478bd9Sstevel@tonic-gate 	return (n != 1 ? "s" : "");
52297c478bd9Sstevel@tonic-gate }
52307c478bd9Sstevel@tonic-gate 
52317c478bd9Sstevel@tonic-gate static char *
52327c478bd9Sstevel@tonic-gate pluraly(int n)
52337c478bd9Sstevel@tonic-gate {
52347c478bd9Sstevel@tonic-gate 	return (n != 1 ? "ies" : "y");
52357c478bd9Sstevel@tonic-gate }
52367c478bd9Sstevel@tonic-gate 
52377c478bd9Sstevel@tonic-gate static char *
52387c478bd9Sstevel@tonic-gate plurales(int n)
52397c478bd9Sstevel@tonic-gate {
52407c478bd9Sstevel@tonic-gate 	return (n != 1 ? "es" : "");
52417c478bd9Sstevel@tonic-gate }
52427c478bd9Sstevel@tonic-gate 
52437c478bd9Sstevel@tonic-gate static char *
52447c478bd9Sstevel@tonic-gate pktscale(n)
52457c478bd9Sstevel@tonic-gate 	int n;
52467c478bd9Sstevel@tonic-gate {
52477c478bd9Sstevel@tonic-gate 	static char buf[6];
52487c478bd9Sstevel@tonic-gate 	char t;
52497c478bd9Sstevel@tonic-gate 
52507c478bd9Sstevel@tonic-gate 	if (n < 1024) {
52517c478bd9Sstevel@tonic-gate 		t = ' ';
52527c478bd9Sstevel@tonic-gate 	} else if (n < 1024 * 1024) {
52537c478bd9Sstevel@tonic-gate 		t = 'k';
52547c478bd9Sstevel@tonic-gate 		n /= 1024;
52557c478bd9Sstevel@tonic-gate 	} else if (n < 1024 * 1024 * 1024) {
52567c478bd9Sstevel@tonic-gate 		t = 'm';
52577c478bd9Sstevel@tonic-gate 		n /= 1024 * 1024;
52587c478bd9Sstevel@tonic-gate 	} else {
52597c478bd9Sstevel@tonic-gate 		t = 'g';
52607c478bd9Sstevel@tonic-gate 		n /= 1024 * 1024 * 1024;
52617c478bd9Sstevel@tonic-gate 	}
52627c478bd9Sstevel@tonic-gate 
52637c478bd9Sstevel@tonic-gate 	(void) snprintf(buf, sizeof (buf), "%4u%c", n, t);
52647c478bd9Sstevel@tonic-gate 	return (buf);
52657c478bd9Sstevel@tonic-gate }
52667c478bd9Sstevel@tonic-gate 
52677c478bd9Sstevel@tonic-gate /* --------------------- mrt_report (netstat -m) -------------------------- */
52687c478bd9Sstevel@tonic-gate 
52697c478bd9Sstevel@tonic-gate static void
52707c478bd9Sstevel@tonic-gate mrt_report(mib_item_t *item)
52717c478bd9Sstevel@tonic-gate {
52727c478bd9Sstevel@tonic-gate 	int		jtemp = 0;
52737c478bd9Sstevel@tonic-gate 	struct vifctl	*vip;
52747c478bd9Sstevel@tonic-gate 	vifi_t		vifi;
52757c478bd9Sstevel@tonic-gate 	struct mfcctl	*mfccp;
52767c478bd9Sstevel@tonic-gate 	int		numvifs = 0;
52777c478bd9Sstevel@tonic-gate 	int		nmfc = 0;
52787c478bd9Sstevel@tonic-gate 	char		abuf[MAXHOSTNAMELEN + 1];
52797c478bd9Sstevel@tonic-gate 
52807c478bd9Sstevel@tonic-gate 	if (!(family_selected(AF_INET)))
52817c478bd9Sstevel@tonic-gate 		return;
52827c478bd9Sstevel@tonic-gate 
52837c478bd9Sstevel@tonic-gate 	/* 'for' loop 1: */
52847c478bd9Sstevel@tonic-gate 	for (; item; item = item->next_item) {
52857c478bd9Sstevel@tonic-gate 		if (Dflag) {
52867c478bd9Sstevel@tonic-gate 			(void) printf("\n--- Entry %d ---\n", ++jtemp);
52877c478bd9Sstevel@tonic-gate 			(void) printf("Group = %d, mib_id = %d, "
52887c478bd9Sstevel@tonic-gate 			    "length = %d, valp = 0x%p\n",
52897c478bd9Sstevel@tonic-gate 			    item->group, item->mib_id, item->length,
52907c478bd9Sstevel@tonic-gate 			    item->valp);
52917c478bd9Sstevel@tonic-gate 		}
52927c478bd9Sstevel@tonic-gate 		if (item->group != EXPER_DVMRP)
52937c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
52947c478bd9Sstevel@tonic-gate 
52957c478bd9Sstevel@tonic-gate 		switch (item->mib_id) {
52967c478bd9Sstevel@tonic-gate 
52977c478bd9Sstevel@tonic-gate 		case EXPER_DVMRP_VIF:
52987c478bd9Sstevel@tonic-gate 			if (Dflag)
52997c478bd9Sstevel@tonic-gate 				(void) printf("%u records for ipVifTable:\n",
53007c478bd9Sstevel@tonic-gate 				    item->length/sizeof (struct vifctl));
53017c478bd9Sstevel@tonic-gate 			if (item->length/sizeof (struct vifctl) == 0) {
53027c478bd9Sstevel@tonic-gate 				(void) puts("\nVirtual Interface Table is "
53037c478bd9Sstevel@tonic-gate 				    "empty");
53047c478bd9Sstevel@tonic-gate 				break;
53057c478bd9Sstevel@tonic-gate 			}
53067c478bd9Sstevel@tonic-gate 
53077c478bd9Sstevel@tonic-gate 			(void) puts("\nVirtual Interface Table\n"
53087c478bd9Sstevel@tonic-gate 			    " Vif Threshold Rate_Limit Local-Address"
53097c478bd9Sstevel@tonic-gate 			    "   Remote-Address     Pkt_in   Pkt_out");
53107c478bd9Sstevel@tonic-gate 
53117c478bd9Sstevel@tonic-gate 			/* 'for' loop 2: */
53127c478bd9Sstevel@tonic-gate 			for (vip = (struct vifctl *)item->valp;
53137c478bd9Sstevel@tonic-gate 			    (char *)vip < (char *)item->valp + item->length;
53147c478bd9Sstevel@tonic-gate 			    /* LINTED: (note 1) */
53157c478bd9Sstevel@tonic-gate 			    vip = (struct vifctl *)((char *)vip +
53167c478bd9Sstevel@tonic-gate 			    vifctlSize)) {
53177c478bd9Sstevel@tonic-gate 				if (vip->vifc_lcl_addr.s_addr == 0)
53187c478bd9Sstevel@tonic-gate 					continue; /* 'for' loop 2 */
53197c478bd9Sstevel@tonic-gate 				/* numvifs = vip->vifc_vifi; */
53207c478bd9Sstevel@tonic-gate 
53217c478bd9Sstevel@tonic-gate 				numvifs++;
53227c478bd9Sstevel@tonic-gate 				(void) printf("  %2u       %3u       "
53237c478bd9Sstevel@tonic-gate 				    "%4u %-15.15s",
53247c478bd9Sstevel@tonic-gate 				    vip->vifc_vifi,
53257c478bd9Sstevel@tonic-gate 				    vip->vifc_threshold,
53267c478bd9Sstevel@tonic-gate 				    vip->vifc_rate_limit,
53277c478bd9Sstevel@tonic-gate 				    pr_addr(vip->vifc_lcl_addr.s_addr,
53287c478bd9Sstevel@tonic-gate 				    abuf, sizeof (abuf)));
53297c478bd9Sstevel@tonic-gate 				(void) printf(" %-15.15s  %8u  %8u\n",
53307c478bd9Sstevel@tonic-gate 				    (vip->vifc_flags & VIFF_TUNNEL) ?
53317c478bd9Sstevel@tonic-gate 				    pr_addr(vip->vifc_rmt_addr.s_addr,
53327c478bd9Sstevel@tonic-gate 				    abuf, sizeof (abuf)) : "",
53337c478bd9Sstevel@tonic-gate 				    vip->vifc_pkt_in,
53347c478bd9Sstevel@tonic-gate 				    vip->vifc_pkt_out);
53357c478bd9Sstevel@tonic-gate 			} /* 'for' loop 2 ends */
53367c478bd9Sstevel@tonic-gate 
53377c478bd9Sstevel@tonic-gate 			(void) printf("Numvifs: %d\n", numvifs);
53387c478bd9Sstevel@tonic-gate 			break;
53397c478bd9Sstevel@tonic-gate 
53407c478bd9Sstevel@tonic-gate 		case EXPER_DVMRP_MRT:
53417c478bd9Sstevel@tonic-gate 			if (Dflag)
53427c478bd9Sstevel@tonic-gate 				(void) printf("%u records for ipMfcTable:\n",
53437c478bd9Sstevel@tonic-gate 					item->length/sizeof (struct vifctl));
53447c478bd9Sstevel@tonic-gate 			if (item->length/sizeof (struct vifctl) == 0) {
53457c478bd9Sstevel@tonic-gate 				(void) puts("\nMulticast Forwarding Cache is "
53467c478bd9Sstevel@tonic-gate 				    "empty");
53477c478bd9Sstevel@tonic-gate 				break;
53487c478bd9Sstevel@tonic-gate 			}
53497c478bd9Sstevel@tonic-gate 
53507c478bd9Sstevel@tonic-gate 			(void) puts("\nMulticast Forwarding Cache\n"
53517c478bd9Sstevel@tonic-gate 			    "  Origin-Subnet                 Mcastgroup      "
53527c478bd9Sstevel@tonic-gate 			    "# Pkts  In-Vif  Out-vifs/Forw-ttl");
53537c478bd9Sstevel@tonic-gate 
53547c478bd9Sstevel@tonic-gate 			for (mfccp = (struct mfcctl *)item->valp;
53557c478bd9Sstevel@tonic-gate 			    (char *)mfccp < (char *)item->valp + item->length;
53567c478bd9Sstevel@tonic-gate 			    /* LINTED: (note 1) */
53577c478bd9Sstevel@tonic-gate 			    mfccp = (struct mfcctl *)((char *)mfccp +
53587c478bd9Sstevel@tonic-gate 			    mfcctlSize)) {
53597c478bd9Sstevel@tonic-gate 
53607c478bd9Sstevel@tonic-gate 				nmfc++;
53617c478bd9Sstevel@tonic-gate 				(void) printf("  %-30.15s",
53627c478bd9Sstevel@tonic-gate 				    pr_addr(mfccp->mfcc_origin.s_addr,
53637c478bd9Sstevel@tonic-gate 				    abuf, sizeof (abuf)));
53647c478bd9Sstevel@tonic-gate 				(void) printf("%-15.15s  %6s  %3u    ",
53657c478bd9Sstevel@tonic-gate 				    pr_net(mfccp->mfcc_mcastgrp.s_addr,
53667c478bd9Sstevel@tonic-gate 					mfccp->mfcc_mcastgrp.s_addr,
53677c478bd9Sstevel@tonic-gate 					abuf, sizeof (abuf)),
53687c478bd9Sstevel@tonic-gate 				    pktscale((int)mfccp->mfcc_pkt_cnt),
53697c478bd9Sstevel@tonic-gate 					mfccp->mfcc_parent);
53707c478bd9Sstevel@tonic-gate 
53717c478bd9Sstevel@tonic-gate 				for (vifi = 0; vifi < MAXVIFS; ++vifi) {
53727c478bd9Sstevel@tonic-gate 					if (mfccp->mfcc_ttls[vifi]) {
53737c478bd9Sstevel@tonic-gate 						(void) printf("      %u (%u)",
53747c478bd9Sstevel@tonic-gate 						    vifi,
53757c478bd9Sstevel@tonic-gate 						    mfccp->mfcc_ttls[vifi]);
53767c478bd9Sstevel@tonic-gate 					}
53777c478bd9Sstevel@tonic-gate 
53787c478bd9Sstevel@tonic-gate 				}
53797c478bd9Sstevel@tonic-gate 				(void) putchar('\n');
53807c478bd9Sstevel@tonic-gate 			}
53817c478bd9Sstevel@tonic-gate 			(void) printf("\nTotal no. of entries in cache: %d\n",
53827c478bd9Sstevel@tonic-gate 			    nmfc);
53837c478bd9Sstevel@tonic-gate 			break;
53847c478bd9Sstevel@tonic-gate 		}
53857c478bd9Sstevel@tonic-gate 	} /* 'for' loop 1 ends */
53867c478bd9Sstevel@tonic-gate 	(void) putchar('\n');
53877c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
53887c478bd9Sstevel@tonic-gate }
53897c478bd9Sstevel@tonic-gate 
53907c478bd9Sstevel@tonic-gate /*
53917c478bd9Sstevel@tonic-gate  * Get the stats for the cache named 'name'.  If prefix != 0, then
53927c478bd9Sstevel@tonic-gate  * interpret the name as a prefix, and sum up stats for all caches
53937c478bd9Sstevel@tonic-gate  * named 'name*'.
53947c478bd9Sstevel@tonic-gate  */
53957c478bd9Sstevel@tonic-gate static void
53967c478bd9Sstevel@tonic-gate kmem_cache_stats(char *title, char *name, int prefix, int64_t *total_bytes)
53977c478bd9Sstevel@tonic-gate {
53987c478bd9Sstevel@tonic-gate 	int len;
53997c478bd9Sstevel@tonic-gate 	int alloc;
54007c478bd9Sstevel@tonic-gate 	int64_t total_alloc = 0;
54017c478bd9Sstevel@tonic-gate 	int alloc_fail, total_alloc_fail = 0;
54027c478bd9Sstevel@tonic-gate 	int buf_size = 0;
54037c478bd9Sstevel@tonic-gate 	int buf_avail;
54047c478bd9Sstevel@tonic-gate 	int buf_total;
54057c478bd9Sstevel@tonic-gate 	int buf_max, total_buf_max = 0;
54067c478bd9Sstevel@tonic-gate 	int buf_inuse, total_buf_inuse = 0;
54077c478bd9Sstevel@tonic-gate 	kstat_t *ksp;
54087c478bd9Sstevel@tonic-gate 	char buf[256];
54097c478bd9Sstevel@tonic-gate 
54107c478bd9Sstevel@tonic-gate 	len = prefix ? strlen(name) : 256;
54117c478bd9Sstevel@tonic-gate 
54127c478bd9Sstevel@tonic-gate 	/* 'for' loop 1: */
54137c478bd9Sstevel@tonic-gate 	for (ksp = kc->kc_chain; ksp != NULL; ksp = ksp->ks_next) {
54147c478bd9Sstevel@tonic-gate 
54157c478bd9Sstevel@tonic-gate 		if (strcmp(ksp->ks_class, "kmem_cache") != 0)
54167c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
54177c478bd9Sstevel@tonic-gate 
54187c478bd9Sstevel@tonic-gate 		/*
54197c478bd9Sstevel@tonic-gate 		 * Hack alert: because of the way streams messages are
54207c478bd9Sstevel@tonic-gate 		 * allocated, every constructed free dblk has an associated
54217c478bd9Sstevel@tonic-gate 		 * mblk.  From the allocator's viewpoint those mblks are
54227c478bd9Sstevel@tonic-gate 		 * allocated (because they haven't been freed), but from
54237c478bd9Sstevel@tonic-gate 		 * our viewpoint they're actually free (because they're
54247c478bd9Sstevel@tonic-gate 		 * not currently in use).  To account for this caching
54257c478bd9Sstevel@tonic-gate 		 * effect we subtract the total constructed free dblks
54267c478bd9Sstevel@tonic-gate 		 * from the total allocated mblks to derive mblks in use.
54277c478bd9Sstevel@tonic-gate 		 */
54287c478bd9Sstevel@tonic-gate 		if (strcmp(name, "streams_mblk") == 0 &&
54297c478bd9Sstevel@tonic-gate 		    strncmp(ksp->ks_name, "streams_dblk", 12) == 0) {
54307c478bd9Sstevel@tonic-gate 			(void) safe_kstat_read(kc, ksp, NULL);
54317c478bd9Sstevel@tonic-gate 			total_buf_inuse -=
54327c478bd9Sstevel@tonic-gate 				kstat_named_value(ksp, "buf_constructed");
54337c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
54347c478bd9Sstevel@tonic-gate 		}
54357c478bd9Sstevel@tonic-gate 
54367c478bd9Sstevel@tonic-gate 		if (strncmp(ksp->ks_name, name, len) != 0)
54377c478bd9Sstevel@tonic-gate 			continue; /* 'for' loop 1 */
54387c478bd9Sstevel@tonic-gate 
54397c478bd9Sstevel@tonic-gate 		(void) safe_kstat_read(kc, ksp, NULL);
54407c478bd9Sstevel@tonic-gate 
54417c478bd9Sstevel@tonic-gate 		alloc		= kstat_named_value(ksp, "alloc");
54427c478bd9Sstevel@tonic-gate 		alloc_fail	= kstat_named_value(ksp, "alloc_fail");
54437c478bd9Sstevel@tonic-gate 		buf_size	= kstat_named_value(ksp, "buf_size");
54447c478bd9Sstevel@tonic-gate 		buf_avail	= kstat_named_value(ksp, "buf_avail");
54457c478bd9Sstevel@tonic-gate 		buf_total	= kstat_named_value(ksp, "buf_total");
54467c478bd9Sstevel@tonic-gate 		buf_max		= kstat_named_value(ksp, "buf_max");
54477c478bd9Sstevel@tonic-gate 		buf_inuse	= buf_total - buf_avail;
54487c478bd9Sstevel@tonic-gate 
54497c478bd9Sstevel@tonic-gate 		if (Vflag && prefix) {
54507c478bd9Sstevel@tonic-gate 			(void) snprintf(buf, sizeof (buf), "%s%s", title,
54517c478bd9Sstevel@tonic-gate 			    ksp->ks_name + len);
54527c478bd9Sstevel@tonic-gate 			(void) printf("    %-18s %6u %9u %11u %11u\n",
54537c478bd9Sstevel@tonic-gate 			    buf, buf_inuse, buf_max, alloc, alloc_fail);
54547c478bd9Sstevel@tonic-gate 		}
54557c478bd9Sstevel@tonic-gate 
54567c478bd9Sstevel@tonic-gate 		total_alloc		+= alloc;
54577c478bd9Sstevel@tonic-gate 		total_alloc_fail	+= alloc_fail;
54587c478bd9Sstevel@tonic-gate 		total_buf_max		+= buf_max;
54597c478bd9Sstevel@tonic-gate 		total_buf_inuse		+= buf_inuse;
54607c478bd9Sstevel@tonic-gate 		*total_bytes		+= (int64_t)buf_inuse * buf_size;
54617c478bd9Sstevel@tonic-gate 	} /* 'for' loop 1 ends */
54627c478bd9Sstevel@tonic-gate 
54637c478bd9Sstevel@tonic-gate 	if (buf_size == 0) {
54647c478bd9Sstevel@tonic-gate 		(void) printf("%-22s [couldn't find statistics for %s]\n",
54657c478bd9Sstevel@tonic-gate 			title, name);
54667c478bd9Sstevel@tonic-gate 		return;
54677c478bd9Sstevel@tonic-gate 	}
54687c478bd9Sstevel@tonic-gate 
54697c478bd9Sstevel@tonic-gate 	if (Vflag && prefix)
54707c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (buf), "%s_total", title);
54717c478bd9Sstevel@tonic-gate 	else
54727c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (buf), "%s", title);
54737c478bd9Sstevel@tonic-gate 
54747c478bd9Sstevel@tonic-gate 	(void) printf("%-22s %6d %9d %11lld %11d\n", buf,
54757c478bd9Sstevel@tonic-gate 		total_buf_inuse, total_buf_max, total_alloc, total_alloc_fail);
54767c478bd9Sstevel@tonic-gate }
54777c478bd9Sstevel@tonic-gate 
54787c478bd9Sstevel@tonic-gate static void
54797c478bd9Sstevel@tonic-gate m_report(void)
54807c478bd9Sstevel@tonic-gate {
54817c478bd9Sstevel@tonic-gate 	int64_t total_bytes = 0;
54827c478bd9Sstevel@tonic-gate 
54837c478bd9Sstevel@tonic-gate 	(void) puts("streams allocation:");
54847c478bd9Sstevel@tonic-gate 	(void) printf("%63s\n", "cumulative  allocation");
54857c478bd9Sstevel@tonic-gate 	(void) printf("%63s\n",
54867c478bd9Sstevel@tonic-gate 	    "current   maximum       total    failures");
54877c478bd9Sstevel@tonic-gate 
54887c478bd9Sstevel@tonic-gate 	kmem_cache_stats("streams",
54897c478bd9Sstevel@tonic-gate 	    "stream_head_cache", 0, &total_bytes);
54907c478bd9Sstevel@tonic-gate 	kmem_cache_stats("queues", "queue_cache", 0, &total_bytes);
54917c478bd9Sstevel@tonic-gate 	kmem_cache_stats("mblk", "streams_mblk", 0, &total_bytes);
54927c478bd9Sstevel@tonic-gate 	kmem_cache_stats("dblk", "streams_dblk", 1, &total_bytes);
54937c478bd9Sstevel@tonic-gate 	kmem_cache_stats("linkblk", "linkinfo_cache", 0, &total_bytes);
54947c478bd9Sstevel@tonic-gate 	kmem_cache_stats("syncq", "syncq_cache", 0, &total_bytes);
54957c478bd9Sstevel@tonic-gate 	kmem_cache_stats("qband", "qband_cache", 0, &total_bytes);
54967c478bd9Sstevel@tonic-gate 
54977c478bd9Sstevel@tonic-gate 	(void) printf("\n%lld Kbytes allocated for streams data\n",
54987c478bd9Sstevel@tonic-gate 		total_bytes / 1024);
54997c478bd9Sstevel@tonic-gate 
55007c478bd9Sstevel@tonic-gate 	(void) putchar('\n');
55017c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
55027c478bd9Sstevel@tonic-gate }
55037c478bd9Sstevel@tonic-gate 
55047c478bd9Sstevel@tonic-gate /* --------------------------------- */
55057c478bd9Sstevel@tonic-gate 
55067c478bd9Sstevel@tonic-gate /*
55077c478bd9Sstevel@tonic-gate  * Print an IPv4 address. Remove the matching part of the domain name
55087c478bd9Sstevel@tonic-gate  * from the returned name.
55097c478bd9Sstevel@tonic-gate  */
55107c478bd9Sstevel@tonic-gate static char *
55117c478bd9Sstevel@tonic-gate pr_addr(uint_t addr, char *dst, uint_t dstlen)
55127c478bd9Sstevel@tonic-gate {
55137c478bd9Sstevel@tonic-gate 	char			*cp;
55147c478bd9Sstevel@tonic-gate 	struct hostent		*hp = NULL;
55157c478bd9Sstevel@tonic-gate 	static char		domain[MAXHOSTNAMELEN + 1];
55167c478bd9Sstevel@tonic-gate 	static boolean_t	first = B_TRUE;
55177c478bd9Sstevel@tonic-gate 	int			error_num;
55187c478bd9Sstevel@tonic-gate 
55197c478bd9Sstevel@tonic-gate 	if (first) {
55207c478bd9Sstevel@tonic-gate 		first = B_FALSE;
55217c478bd9Sstevel@tonic-gate 		if (sysinfo(SI_HOSTNAME, domain, MAXHOSTNAMELEN) != -1 &&
55227c478bd9Sstevel@tonic-gate 		    (cp = strchr(domain, '.'))) {
55237c478bd9Sstevel@tonic-gate 			(void) strncpy(domain, cp + 1, sizeof (domain));
55247c478bd9Sstevel@tonic-gate 		} else
55257c478bd9Sstevel@tonic-gate 			domain[0] = 0;
55267c478bd9Sstevel@tonic-gate 	}
55277c478bd9Sstevel@tonic-gate 	cp = NULL;
55287c478bd9Sstevel@tonic-gate 	if (!Nflag) {
55297c478bd9Sstevel@tonic-gate 		hp = getipnodebyaddr((char *)&addr, sizeof (uint_t), AF_INET,
55307c478bd9Sstevel@tonic-gate 		    &error_num);
55317c478bd9Sstevel@tonic-gate 		if (hp) {
55327c478bd9Sstevel@tonic-gate 			if ((cp = strchr(hp->h_name, '.')) != NULL &&
55337c478bd9Sstevel@tonic-gate 			    strcasecmp(cp + 1, domain) == 0)
55347c478bd9Sstevel@tonic-gate 				*cp = 0;
55357c478bd9Sstevel@tonic-gate 			cp = hp->h_name;
55367c478bd9Sstevel@tonic-gate 		}
55377c478bd9Sstevel@tonic-gate 	}
55387c478bd9Sstevel@tonic-gate 	if (cp != NULL) {
55397c478bd9Sstevel@tonic-gate 		(void) strncpy(dst, cp, dstlen);
55407c478bd9Sstevel@tonic-gate 		dst[dstlen - 1] = 0;
55417c478bd9Sstevel@tonic-gate 	} else {
55427c478bd9Sstevel@tonic-gate 		(void) inet_ntop(AF_INET, (char *)&addr, dst, dstlen);
55437c478bd9Sstevel@tonic-gate 	}
55447c478bd9Sstevel@tonic-gate 	if (hp != NULL)
55457c478bd9Sstevel@tonic-gate 		freehostent(hp);
55467c478bd9Sstevel@tonic-gate 	return (dst);
55477c478bd9Sstevel@tonic-gate }
55487c478bd9Sstevel@tonic-gate 
55497c478bd9Sstevel@tonic-gate /*
55507c478bd9Sstevel@tonic-gate  * Print a non-zero IPv4 address.  Print "    --" if the address is zero.
55517c478bd9Sstevel@tonic-gate  */
55527c478bd9Sstevel@tonic-gate static char *
55537c478bd9Sstevel@tonic-gate pr_addrnz(ipaddr_t addr, char *dst, uint_t dstlen)
55547c478bd9Sstevel@tonic-gate {
55557c478bd9Sstevel@tonic-gate 	if (addr == INADDR_ANY) {
55567c478bd9Sstevel@tonic-gate 		(void) strlcpy(dst, "    --", dstlen);
55577c478bd9Sstevel@tonic-gate 		return (dst);
55587c478bd9Sstevel@tonic-gate 	}
55597c478bd9Sstevel@tonic-gate 	return (pr_addr(addr, dst, dstlen));
55607c478bd9Sstevel@tonic-gate }
55617c478bd9Sstevel@tonic-gate 
55627c478bd9Sstevel@tonic-gate /*
55637c478bd9Sstevel@tonic-gate  * Print an IPv6 address. Remove the matching part of the domain name
55647c478bd9Sstevel@tonic-gate  * from the returned name.
55657c478bd9Sstevel@tonic-gate  */
55667c478bd9Sstevel@tonic-gate static char *
55677c478bd9Sstevel@tonic-gate pr_addr6(const struct in6_addr *addr, char *dst, uint_t dstlen)
55687c478bd9Sstevel@tonic-gate {
55697c478bd9Sstevel@tonic-gate 	char			*cp;
55707c478bd9Sstevel@tonic-gate 	struct hostent		*hp = NULL;
55717c478bd9Sstevel@tonic-gate 	static char		domain[MAXHOSTNAMELEN + 1];
55727c478bd9Sstevel@tonic-gate 	static boolean_t	first = B_TRUE;
55737c478bd9Sstevel@tonic-gate 	int			error_num;
55747c478bd9Sstevel@tonic-gate 
55757c478bd9Sstevel@tonic-gate 	if (first) {
55767c478bd9Sstevel@tonic-gate 		first = B_FALSE;
55777c478bd9Sstevel@tonic-gate 		if (sysinfo(SI_HOSTNAME, domain, MAXHOSTNAMELEN) != -1 &&
55787c478bd9Sstevel@tonic-gate 		    (cp = strchr(domain, '.'))) {
55797c478bd9Sstevel@tonic-gate 			(void) strncpy(domain, cp + 1, sizeof (domain));
55807c478bd9Sstevel@tonic-gate 		} else
55817c478bd9Sstevel@tonic-gate 			domain[0] = 0;
55827c478bd9Sstevel@tonic-gate 	}
55837c478bd9Sstevel@tonic-gate 	cp = NULL;
55847c478bd9Sstevel@tonic-gate 	if (!Nflag) {
55857c478bd9Sstevel@tonic-gate 		hp = getipnodebyaddr((char *)addr,
55867c478bd9Sstevel@tonic-gate 		    sizeof (struct in6_addr), AF_INET6, &error_num);
55877c478bd9Sstevel@tonic-gate 		if (hp) {
55887c478bd9Sstevel@tonic-gate 			if ((cp = strchr(hp->h_name, '.')) != NULL &&
55897c478bd9Sstevel@tonic-gate 			    strcasecmp(cp + 1, domain) == 0)
55907c478bd9Sstevel@tonic-gate 				*cp = 0;
55917c478bd9Sstevel@tonic-gate 			cp = hp->h_name;
55927c478bd9Sstevel@tonic-gate 		}
55937c478bd9Sstevel@tonic-gate 	}
55947c478bd9Sstevel@tonic-gate 	if (cp != NULL) {
55957c478bd9Sstevel@tonic-gate 		(void) strncpy(dst, cp, dstlen);
55967c478bd9Sstevel@tonic-gate 		dst[dstlen - 1] = 0;
55977c478bd9Sstevel@tonic-gate 	} else {
55987c478bd9Sstevel@tonic-gate 		(void) inet_ntop(AF_INET6, (void *)addr, dst, dstlen);
55997c478bd9Sstevel@tonic-gate 	}
56007c478bd9Sstevel@tonic-gate 	if (hp != NULL)
56017c478bd9Sstevel@tonic-gate 		freehostent(hp);
56027c478bd9Sstevel@tonic-gate 	return (dst);
56037c478bd9Sstevel@tonic-gate }
56047c478bd9Sstevel@tonic-gate 
56057c478bd9Sstevel@tonic-gate /* For IPv4 masks */
56067c478bd9Sstevel@tonic-gate static char *
56077c478bd9Sstevel@tonic-gate pr_mask(uint_t addr, char *dst, uint_t dstlen)
56087c478bd9Sstevel@tonic-gate {
56097c478bd9Sstevel@tonic-gate 	uint8_t	*ip_addr = (uint8_t *)&addr;
56107c478bd9Sstevel@tonic-gate 
56117c478bd9Sstevel@tonic-gate 	(void) snprintf(dst, dstlen, "%d.%d.%d.%d",
56127c478bd9Sstevel@tonic-gate 	    ip_addr[0], ip_addr[1], ip_addr[2], ip_addr[3]);
56137c478bd9Sstevel@tonic-gate 	return (dst);
56147c478bd9Sstevel@tonic-gate }
56157c478bd9Sstevel@tonic-gate 
56167c478bd9Sstevel@tonic-gate /*
56177c478bd9Sstevel@tonic-gate  * For ipv6 masks format is : dest/mask
56187c478bd9Sstevel@tonic-gate  * Does not print /128 to save space in printout. H flag carries this notion.
56197c478bd9Sstevel@tonic-gate  */
56207c478bd9Sstevel@tonic-gate static char *
562145916cd2Sjpk pr_prefix6(const struct in6_addr *addr, uint_t prefixlen, char *dst,
562245916cd2Sjpk     uint_t dstlen)
56237c478bd9Sstevel@tonic-gate {
56247c478bd9Sstevel@tonic-gate 	char *cp;
56257c478bd9Sstevel@tonic-gate 
56267c478bd9Sstevel@tonic-gate 	if (IN6_IS_ADDR_UNSPECIFIED(addr) && prefixlen == 0) {
56277c478bd9Sstevel@tonic-gate 		(void) strncpy(dst, "default", dstlen);
56287c478bd9Sstevel@tonic-gate 		dst[dstlen - 1] = 0;
56297c478bd9Sstevel@tonic-gate 		return (dst);
56307c478bd9Sstevel@tonic-gate 	}
56317c478bd9Sstevel@tonic-gate 
56327c478bd9Sstevel@tonic-gate 	(void) pr_addr6(addr, dst, dstlen);
56337c478bd9Sstevel@tonic-gate 	if (prefixlen != IPV6_ABITS) {
56347c478bd9Sstevel@tonic-gate 		/* How much room is left? */
56357c478bd9Sstevel@tonic-gate 		cp = strchr(dst, '\0');
56367c478bd9Sstevel@tonic-gate 		if (dst + dstlen > cp) {
56377c478bd9Sstevel@tonic-gate 			dstlen -= (cp - dst);
56387c478bd9Sstevel@tonic-gate 			(void) snprintf(cp, dstlen, "/%d", prefixlen);
56397c478bd9Sstevel@tonic-gate 		}
56407c478bd9Sstevel@tonic-gate 	}
56417c478bd9Sstevel@tonic-gate 	return (dst);
56427c478bd9Sstevel@tonic-gate }
56437c478bd9Sstevel@tonic-gate 
56447c478bd9Sstevel@tonic-gate /* Print IPv4 address and port */
56457c478bd9Sstevel@tonic-gate static char *
56467c478bd9Sstevel@tonic-gate pr_ap(uint_t addr, uint_t port, char *proto,
56477c478bd9Sstevel@tonic-gate     char *dst, uint_t dstlen)
56487c478bd9Sstevel@tonic-gate {
56497c478bd9Sstevel@tonic-gate 	char *cp;
56507c478bd9Sstevel@tonic-gate 
56517c478bd9Sstevel@tonic-gate 	if (addr == INADDR_ANY) {
56527c478bd9Sstevel@tonic-gate 		(void) strncpy(dst, "      *", dstlen);
56537c478bd9Sstevel@tonic-gate 		dst[dstlen - 1] = 0;
56547c478bd9Sstevel@tonic-gate 	} else {
56557c478bd9Sstevel@tonic-gate 		(void) pr_addr(addr, dst, dstlen);
56567c478bd9Sstevel@tonic-gate 	}
56577c478bd9Sstevel@tonic-gate 	/* How much room is left? */
56587c478bd9Sstevel@tonic-gate 	cp = strchr(dst, '\0');
56597c478bd9Sstevel@tonic-gate 	if (dst + dstlen > cp + 1) {
56607c478bd9Sstevel@tonic-gate 		*cp++ = '.';
56617c478bd9Sstevel@tonic-gate 		dstlen -= (cp - dst);
56627c478bd9Sstevel@tonic-gate 		dstlen--;
56637c478bd9Sstevel@tonic-gate 		(void) portname(port, proto, cp, dstlen);
56647c478bd9Sstevel@tonic-gate 	}
56657c478bd9Sstevel@tonic-gate 	return (dst);
56667c478bd9Sstevel@tonic-gate }
56677c478bd9Sstevel@tonic-gate 
56687c478bd9Sstevel@tonic-gate /* Print IPv6 address and port */
56697c478bd9Sstevel@tonic-gate static char *
56707c478bd9Sstevel@tonic-gate pr_ap6(const in6_addr_t *addr, uint_t port, char *proto,
56717c478bd9Sstevel@tonic-gate     char *dst, uint_t dstlen)
56727c478bd9Sstevel@tonic-gate {
56737c478bd9Sstevel@tonic-gate 	char *cp;
56747c478bd9Sstevel@tonic-gate 
56757c478bd9Sstevel@tonic-gate 	if (IN6_IS_ADDR_UNSPECIFIED(addr)) {
56767c478bd9Sstevel@tonic-gate 		(void) strncpy(dst, "      *", dstlen);
56777c478bd9Sstevel@tonic-gate 		dst[dstlen - 1] = 0;
56787c478bd9Sstevel@tonic-gate 	} else {
56797c478bd9Sstevel@tonic-gate 		(void) pr_addr6(addr, dst, dstlen);
56807c478bd9Sstevel@tonic-gate 	}
56817c478bd9Sstevel@tonic-gate 	/* How much room is left? */
56827c478bd9Sstevel@tonic-gate 	cp = strchr(dst, '\0');
56837c478bd9Sstevel@tonic-gate 	if (dst + dstlen + 1 > cp) {
56847c478bd9Sstevel@tonic-gate 		*cp++ = '.';
56857c478bd9Sstevel@tonic-gate 		dstlen -= (cp - dst);
56867c478bd9Sstevel@tonic-gate 		dstlen--;
56877c478bd9Sstevel@tonic-gate 		(void) portname(port, proto, cp, dstlen);
56887c478bd9Sstevel@tonic-gate 	}
56897c478bd9Sstevel@tonic-gate 	return (dst);
56907c478bd9Sstevel@tonic-gate }
56917c478bd9Sstevel@tonic-gate 
56927c478bd9Sstevel@tonic-gate /*
56937c478bd9Sstevel@tonic-gate  * Return the name of the network whose address is given. The address is
56947c478bd9Sstevel@tonic-gate  * assumed to be that of a net or subnet, not a host.
56957c478bd9Sstevel@tonic-gate  */
56967c478bd9Sstevel@tonic-gate static char *
56977c478bd9Sstevel@tonic-gate pr_net(uint_t addr, uint_t mask, char *dst, uint_t dstlen)
56987c478bd9Sstevel@tonic-gate {
56997c478bd9Sstevel@tonic-gate 	char		*cp = NULL;
57007c478bd9Sstevel@tonic-gate 	struct netent	*np = NULL;
57017c478bd9Sstevel@tonic-gate 	struct hostent	*hp = NULL;
57027c478bd9Sstevel@tonic-gate 	uint_t		net;
57037c478bd9Sstevel@tonic-gate 	int		subnetshift;
57047c478bd9Sstevel@tonic-gate 	int		error_num;
57057c478bd9Sstevel@tonic-gate 
57067c478bd9Sstevel@tonic-gate 	if (addr == INADDR_ANY && mask == INADDR_ANY) {
57077c478bd9Sstevel@tonic-gate 		(void) strncpy(dst, "default", dstlen);
57087c478bd9Sstevel@tonic-gate 		dst[dstlen - 1] = 0;
57097c478bd9Sstevel@tonic-gate 		return (dst);
57107c478bd9Sstevel@tonic-gate 	}
57117c478bd9Sstevel@tonic-gate 
57127c478bd9Sstevel@tonic-gate 	if (!Nflag && addr) {
57137c478bd9Sstevel@tonic-gate 		if (mask == 0) {
57147c478bd9Sstevel@tonic-gate 			if (IN_CLASSA(addr)) {
57157c478bd9Sstevel@tonic-gate 				mask = (uint_t)IN_CLASSA_NET;
57167c478bd9Sstevel@tonic-gate 				subnetshift = 8;
57177c478bd9Sstevel@tonic-gate 			} else if (IN_CLASSB(addr)) {
57187c478bd9Sstevel@tonic-gate 				mask = (uint_t)IN_CLASSB_NET;
57197c478bd9Sstevel@tonic-gate 				subnetshift = 8;
57207c478bd9Sstevel@tonic-gate 			} else {
57217c478bd9Sstevel@tonic-gate 				mask = (uint_t)IN_CLASSC_NET;
57227c478bd9Sstevel@tonic-gate 				subnetshift = 4;
57237c478bd9Sstevel@tonic-gate 			}
57247c478bd9Sstevel@tonic-gate 			/*
57257c478bd9Sstevel@tonic-gate 			 * If there are more bits than the standard mask
57267c478bd9Sstevel@tonic-gate 			 * would suggest, subnets must be in use. Guess at
57277c478bd9Sstevel@tonic-gate 			 * the subnet mask, assuming reasonable width subnet
57287c478bd9Sstevel@tonic-gate 			 * fields.
57297c478bd9Sstevel@tonic-gate 			 */
57307c478bd9Sstevel@tonic-gate 			while (addr & ~mask)
57317c478bd9Sstevel@tonic-gate 				/* compiler doesn't sign extend! */
57327c478bd9Sstevel@tonic-gate 				mask = (mask | ((int)mask >> subnetshift));
57337c478bd9Sstevel@tonic-gate 		}
57347c478bd9Sstevel@tonic-gate 		net = addr & mask;
57357c478bd9Sstevel@tonic-gate 		while ((mask & 1) == 0)
57367c478bd9Sstevel@tonic-gate 			mask >>= 1, net >>= 1;
57377c478bd9Sstevel@tonic-gate 		np = getnetbyaddr(net, AF_INET);
57387c478bd9Sstevel@tonic-gate 		if (np && np->n_net == net)
57397c478bd9Sstevel@tonic-gate 			cp = np->n_name;
57407c478bd9Sstevel@tonic-gate 		else {
57417c478bd9Sstevel@tonic-gate 			/*
57427c478bd9Sstevel@tonic-gate 			 * Look for subnets in hosts map.
57437c478bd9Sstevel@tonic-gate 			 */
57447c478bd9Sstevel@tonic-gate 			hp = getipnodebyaddr((char *)&addr, sizeof (uint_t),
57457c478bd9Sstevel@tonic-gate 			    AF_INET, &error_num);
57467c478bd9Sstevel@tonic-gate 			if (hp)
57477c478bd9Sstevel@tonic-gate 				cp = hp->h_name;
57487c478bd9Sstevel@tonic-gate 		}
57497c478bd9Sstevel@tonic-gate 	}
57507c478bd9Sstevel@tonic-gate 	if (cp != NULL) {
57517c478bd9Sstevel@tonic-gate 		(void) strncpy(dst, cp, dstlen);
57527c478bd9Sstevel@tonic-gate 		dst[dstlen - 1] = 0;
57537c478bd9Sstevel@tonic-gate 	} else {
57547c478bd9Sstevel@tonic-gate 		(void) inet_ntop(AF_INET, (char *)&addr, dst, dstlen);
57557c478bd9Sstevel@tonic-gate 	}
57567c478bd9Sstevel@tonic-gate 	if (hp != NULL)
57577c478bd9Sstevel@tonic-gate 		freehostent(hp);
57587c478bd9Sstevel@tonic-gate 	return (dst);
57597c478bd9Sstevel@tonic-gate }
57607c478bd9Sstevel@tonic-gate 
57617c478bd9Sstevel@tonic-gate /*
57627c478bd9Sstevel@tonic-gate  * Return the name of the network whose address is given.
57637c478bd9Sstevel@tonic-gate  * The address is assumed to be a host address.
57647c478bd9Sstevel@tonic-gate  */
57657c478bd9Sstevel@tonic-gate static char *
57667c478bd9Sstevel@tonic-gate pr_netaddr(uint_t addr, uint_t mask, char *dst, uint_t dstlen)
57677c478bd9Sstevel@tonic-gate {
57687c478bd9Sstevel@tonic-gate 	char		*cp = NULL;
57697c478bd9Sstevel@tonic-gate 	struct netent	*np = NULL;
57707c478bd9Sstevel@tonic-gate 	struct hostent	*hp = NULL;
57717c478bd9Sstevel@tonic-gate 	uint_t		net;
57727c478bd9Sstevel@tonic-gate 	uint_t		netshifted;
57737c478bd9Sstevel@tonic-gate 	int		subnetshift;
57747c478bd9Sstevel@tonic-gate 	struct in_addr in;
57757c478bd9Sstevel@tonic-gate 	int		error_num;
57767c478bd9Sstevel@tonic-gate 	uint_t		nbo_addr = addr;	/* network byte order */
57777c478bd9Sstevel@tonic-gate 
57787c478bd9Sstevel@tonic-gate 	addr = ntohl(addr);
57797c478bd9Sstevel@tonic-gate 	mask = ntohl(mask);
57807c478bd9Sstevel@tonic-gate 	if (addr == INADDR_ANY && mask == INADDR_ANY) {
57817c478bd9Sstevel@tonic-gate 		(void) strncpy(dst, "default", dstlen);
57827c478bd9Sstevel@tonic-gate 		dst[dstlen - 1] = 0;
57837c478bd9Sstevel@tonic-gate 		return (dst);
57847c478bd9Sstevel@tonic-gate 	}
57857c478bd9Sstevel@tonic-gate 
57867c478bd9Sstevel@tonic-gate 	/* Figure out network portion of address (with host portion = 0) */
57877c478bd9Sstevel@tonic-gate 	if (addr) {
57887c478bd9Sstevel@tonic-gate 		/* Try figuring out mask if unknown (all 0s). */
57897c478bd9Sstevel@tonic-gate 		if (mask == 0) {
57907c478bd9Sstevel@tonic-gate 			if (IN_CLASSA(addr)) {
57917c478bd9Sstevel@tonic-gate 				mask = (uint_t)IN_CLASSA_NET;
57927c478bd9Sstevel@tonic-gate 				subnetshift = 8;
57937c478bd9Sstevel@tonic-gate 			} else if (IN_CLASSB(addr)) {
57947c478bd9Sstevel@tonic-gate 				mask = (uint_t)IN_CLASSB_NET;
57957c478bd9Sstevel@tonic-gate 				subnetshift = 8;
57967c478bd9Sstevel@tonic-gate 			} else {
57977c478bd9Sstevel@tonic-gate 				mask = (uint_t)IN_CLASSC_NET;
57987c478bd9Sstevel@tonic-gate 				subnetshift = 4;
57997c478bd9Sstevel@tonic-gate 			}
58007c478bd9Sstevel@tonic-gate 			/*
58017c478bd9Sstevel@tonic-gate 			 * If there are more bits than the standard mask
58027c478bd9Sstevel@tonic-gate 			 * would suggest, subnets must be in use. Guess at
58037c478bd9Sstevel@tonic-gate 			 * the subnet mask, assuming reasonable width subnet
58047c478bd9Sstevel@tonic-gate 			 * fields.
58057c478bd9Sstevel@tonic-gate 			 */
58067c478bd9Sstevel@tonic-gate 			while (addr & ~mask)
58077c478bd9Sstevel@tonic-gate 				/* compiler doesn't sign extend! */
58087c478bd9Sstevel@tonic-gate 				mask = (mask | ((int)mask >> subnetshift));
58097c478bd9Sstevel@tonic-gate 		}
58107c478bd9Sstevel@tonic-gate 		net = netshifted = addr & mask;
58117c478bd9Sstevel@tonic-gate 		while ((mask & 1) == 0)
58127c478bd9Sstevel@tonic-gate 			mask >>= 1, netshifted >>= 1;
58137c478bd9Sstevel@tonic-gate 	}
58147c478bd9Sstevel@tonic-gate 	else
58157c478bd9Sstevel@tonic-gate 		net = netshifted = 0;
58167c478bd9Sstevel@tonic-gate 
58177c478bd9Sstevel@tonic-gate 	/* Try looking up name unless -n was specified. */
58187c478bd9Sstevel@tonic-gate 	if (!Nflag) {
58197c478bd9Sstevel@tonic-gate 		np = getnetbyaddr(netshifted, AF_INET);
58207c478bd9Sstevel@tonic-gate 		if (np && np->n_net == netshifted)
58217c478bd9Sstevel@tonic-gate 			cp = np->n_name;
58227c478bd9Sstevel@tonic-gate 		else {
58237c478bd9Sstevel@tonic-gate 			/*
58247c478bd9Sstevel@tonic-gate 			 * Look for subnets in hosts map.
58257c478bd9Sstevel@tonic-gate 			 */
58267c478bd9Sstevel@tonic-gate 			hp = getipnodebyaddr((char *)&nbo_addr, sizeof (uint_t),
58277c478bd9Sstevel@tonic-gate 			    AF_INET, &error_num);
58287c478bd9Sstevel@tonic-gate 			if (hp)
58297c478bd9Sstevel@tonic-gate 				cp = hp->h_name;
58307c478bd9Sstevel@tonic-gate 		}
58317c478bd9Sstevel@tonic-gate 
58327c478bd9Sstevel@tonic-gate 		if (cp != NULL) {
58337c478bd9Sstevel@tonic-gate 			(void) strncpy(dst, cp, dstlen);
58347c478bd9Sstevel@tonic-gate 			dst[dstlen - 1] = 0;
58357c478bd9Sstevel@tonic-gate 			if (hp != NULL)
58367c478bd9Sstevel@tonic-gate 				freehostent(hp);
58377c478bd9Sstevel@tonic-gate 			return (dst);
58387c478bd9Sstevel@tonic-gate 		}
58397c478bd9Sstevel@tonic-gate 		/*
58407c478bd9Sstevel@tonic-gate 		 * No name found for net: fallthru and return in decimal
58417c478bd9Sstevel@tonic-gate 		 * dot notation.
58427c478bd9Sstevel@tonic-gate 		 */
58437c478bd9Sstevel@tonic-gate 	}
58447c478bd9Sstevel@tonic-gate 
58457c478bd9Sstevel@tonic-gate 	in.s_addr = htonl(net);
58467c478bd9Sstevel@tonic-gate 	(void) inet_ntop(AF_INET, (char *)&in, dst, dstlen);
58477c478bd9Sstevel@tonic-gate 	if (hp != NULL)
58487c478bd9Sstevel@tonic-gate 		freehostent(hp);
58497c478bd9Sstevel@tonic-gate 	return (dst);
58507c478bd9Sstevel@tonic-gate }
58517c478bd9Sstevel@tonic-gate 
58527c478bd9Sstevel@tonic-gate /*
58537c478bd9Sstevel@tonic-gate  * Return the filter mode as a string:
58547c478bd9Sstevel@tonic-gate  *	1 => "INCLUDE"
58557c478bd9Sstevel@tonic-gate  *	2 => "EXCLUDE"
58567c478bd9Sstevel@tonic-gate  *	otherwise "<unknown>"
58577c478bd9Sstevel@tonic-gate  */
58587c478bd9Sstevel@tonic-gate static char *
58597c478bd9Sstevel@tonic-gate fmodestr(uint_t fmode)
58607c478bd9Sstevel@tonic-gate {
58617c478bd9Sstevel@tonic-gate 	switch (fmode) {
58627c478bd9Sstevel@tonic-gate 	case 1:
58637c478bd9Sstevel@tonic-gate 		return ("INCLUDE");
58647c478bd9Sstevel@tonic-gate 	case 2:
58657c478bd9Sstevel@tonic-gate 		return ("EXCLUDE");
58667c478bd9Sstevel@tonic-gate 	default:
58677c478bd9Sstevel@tonic-gate 		return ("<unknown>");
58687c478bd9Sstevel@tonic-gate 	}
58697c478bd9Sstevel@tonic-gate }
58707c478bd9Sstevel@tonic-gate 
587145916cd2Sjpk #define	MAX_STRING_SIZE	256
587245916cd2Sjpk 
587345916cd2Sjpk static const char *
587445916cd2Sjpk pr_secattr(const sec_attr_list_t *attrs)
587545916cd2Sjpk {
587645916cd2Sjpk 	int i;
587745916cd2Sjpk 	char buf[MAX_STRING_SIZE + 1], *cp;
587845916cd2Sjpk 	static char *sbuf;
587945916cd2Sjpk 	static size_t sbuf_len;
588045916cd2Sjpk 	struct rtsa_s rtsa;
588145916cd2Sjpk 	const sec_attr_list_t *aptr;
588245916cd2Sjpk 
588345916cd2Sjpk 	if (!RSECflag || attrs == NULL)
588445916cd2Sjpk 		return ("");
588545916cd2Sjpk 
588645916cd2Sjpk 	for (aptr = attrs, i = 1; aptr != NULL; aptr = aptr->sal_next)
588745916cd2Sjpk 		i += MAX_STRING_SIZE;
588845916cd2Sjpk 	if (i > sbuf_len) {
588945916cd2Sjpk 		cp = realloc(sbuf, i);
589045916cd2Sjpk 		if (cp == NULL) {
589145916cd2Sjpk 			perror("realloc security attribute buffer");
589245916cd2Sjpk 			return ("");
589345916cd2Sjpk 		}
589445916cd2Sjpk 		sbuf_len = i;
589545916cd2Sjpk 		sbuf = cp;
589645916cd2Sjpk 	}
589745916cd2Sjpk 
589845916cd2Sjpk 	cp = sbuf;
589945916cd2Sjpk 	while (attrs != NULL) {
590045916cd2Sjpk 		const mib2_ipAttributeEntry_t *iae = attrs->sal_attr;
590145916cd2Sjpk 
590245916cd2Sjpk 		/* note: effectively hard-coded in rtsa_keyword */
590345916cd2Sjpk 		rtsa.rtsa_mask = RTSA_CIPSO | RTSA_SLRANGE | RTSA_DOI;
590445916cd2Sjpk 		rtsa.rtsa_slrange = iae->iae_slrange;
590545916cd2Sjpk 		rtsa.rtsa_doi = iae->iae_doi;
590645916cd2Sjpk 
590745916cd2Sjpk 		(void) snprintf(cp, MAX_STRING_SIZE,
590845916cd2Sjpk 		    "<%s>%s ", rtsa_to_str(&rtsa, buf, sizeof (buf)),
590945916cd2Sjpk 		    attrs->sal_next == NULL ? "" : ",");
591045916cd2Sjpk 		cp += strlen(cp);
591145916cd2Sjpk 		attrs = attrs->sal_next;
591245916cd2Sjpk 	}
591345916cd2Sjpk 	*cp = '\0';
591445916cd2Sjpk 
591545916cd2Sjpk 	return (sbuf);
591645916cd2Sjpk }
591745916cd2Sjpk 
59187c478bd9Sstevel@tonic-gate /*
59197c478bd9Sstevel@tonic-gate  * Pretty print a port number. If the Nflag was
59207c478bd9Sstevel@tonic-gate  * specified, use numbers instead of names.
59217c478bd9Sstevel@tonic-gate  */
59227c478bd9Sstevel@tonic-gate static char *
59237c478bd9Sstevel@tonic-gate portname(uint_t port, char *proto, char *dst, uint_t dstlen)
59247c478bd9Sstevel@tonic-gate {
59257c478bd9Sstevel@tonic-gate 	struct servent *sp = NULL;
59267c478bd9Sstevel@tonic-gate 
59277c478bd9Sstevel@tonic-gate 	if (!Nflag && port)
59287c478bd9Sstevel@tonic-gate 		sp = getservbyport(htons(port), proto);
59297c478bd9Sstevel@tonic-gate 	if (sp || port == 0)
59307c478bd9Sstevel@tonic-gate 		(void) snprintf(dst, dstlen, "%.*s", MAXHOSTNAMELEN,
59317c478bd9Sstevel@tonic-gate 				sp ? sp->s_name : "*");
59327c478bd9Sstevel@tonic-gate 	else
59337c478bd9Sstevel@tonic-gate 		(void) snprintf(dst, dstlen, "%d", port);
59347c478bd9Sstevel@tonic-gate 	dst[dstlen - 1] = 0;
59357c478bd9Sstevel@tonic-gate 	return (dst);
59367c478bd9Sstevel@tonic-gate }
59377c478bd9Sstevel@tonic-gate 
59387c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/
59397c478bd9Sstevel@tonic-gate void
59407c478bd9Sstevel@tonic-gate fail(int do_perror, char *message, ...)
59417c478bd9Sstevel@tonic-gate {
59427c478bd9Sstevel@tonic-gate 	va_list args;
59437c478bd9Sstevel@tonic-gate 
59447c478bd9Sstevel@tonic-gate 	va_start(args, message);
59457c478bd9Sstevel@tonic-gate 	(void) fputs("netstat: ", stderr);
59467c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, message, args);
59477c478bd9Sstevel@tonic-gate 	va_end(args);
59487c478bd9Sstevel@tonic-gate 	if (do_perror)
59497c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, ": %s", strerror(errno));
59507c478bd9Sstevel@tonic-gate 	(void) fputc('\n', stderr);
59517c478bd9Sstevel@tonic-gate 	exit(2);
59527c478bd9Sstevel@tonic-gate }
59537c478bd9Sstevel@tonic-gate 
59547c478bd9Sstevel@tonic-gate /*
59557c478bd9Sstevel@tonic-gate  * Return value of named statistic for given kstat_named kstat;
59567c478bd9Sstevel@tonic-gate  * return 0LL if named statistic is not in list (use "ll" as a
59577c478bd9Sstevel@tonic-gate  * type qualifier when printing 64-bit int's with printf() )
59587c478bd9Sstevel@tonic-gate  */
59597c478bd9Sstevel@tonic-gate static uint64_t
59607c478bd9Sstevel@tonic-gate kstat_named_value(kstat_t *ksp, char *name)
59617c478bd9Sstevel@tonic-gate {
59627c478bd9Sstevel@tonic-gate 	kstat_named_t *knp;
59637c478bd9Sstevel@tonic-gate 	uint64_t value;
59647c478bd9Sstevel@tonic-gate 
59657c478bd9Sstevel@tonic-gate 	if (ksp == NULL)
59667c478bd9Sstevel@tonic-gate 		return (0LL);
59677c478bd9Sstevel@tonic-gate 
59687c478bd9Sstevel@tonic-gate 	knp = kstat_data_lookup(ksp, name);
59697c478bd9Sstevel@tonic-gate 	if (knp == NULL)
59707c478bd9Sstevel@tonic-gate 		return (0LL);
59717c478bd9Sstevel@tonic-gate 
59727c478bd9Sstevel@tonic-gate 	switch (knp->data_type) {
59737c478bd9Sstevel@tonic-gate 	case KSTAT_DATA_INT32:
59747c478bd9Sstevel@tonic-gate 	case KSTAT_DATA_UINT32:
59757c478bd9Sstevel@tonic-gate 		value = (uint64_t)(knp->value.ui32);
59767c478bd9Sstevel@tonic-gate 		break;
59777c478bd9Sstevel@tonic-gate 	case KSTAT_DATA_INT64:
59787c478bd9Sstevel@tonic-gate 	case KSTAT_DATA_UINT64:
59797c478bd9Sstevel@tonic-gate 		value = knp->value.ui64;
59807c478bd9Sstevel@tonic-gate 		break;
59817c478bd9Sstevel@tonic-gate 	default:
59827c478bd9Sstevel@tonic-gate 		value = 0LL;
59837c478bd9Sstevel@tonic-gate 		break;
59847c478bd9Sstevel@tonic-gate 	}
59857c478bd9Sstevel@tonic-gate 
59867c478bd9Sstevel@tonic-gate 	return (value);
59877c478bd9Sstevel@tonic-gate }
59887c478bd9Sstevel@tonic-gate 
59897c478bd9Sstevel@tonic-gate kid_t
59907c478bd9Sstevel@tonic-gate safe_kstat_read(kstat_ctl_t *kc, kstat_t *ksp, void *data)
59917c478bd9Sstevel@tonic-gate {
59927c478bd9Sstevel@tonic-gate 	kid_t kstat_chain_id = kstat_read(kc, ksp, data);
59937c478bd9Sstevel@tonic-gate 
59947c478bd9Sstevel@tonic-gate 	if (kstat_chain_id == -1)
59957c478bd9Sstevel@tonic-gate 		fail(1, "kstat_read(%p, '%s') failed", (void *)kc,
59967c478bd9Sstevel@tonic-gate 		    ksp->ks_name);
59977c478bd9Sstevel@tonic-gate 	return (kstat_chain_id);
59987c478bd9Sstevel@tonic-gate }
59997c478bd9Sstevel@tonic-gate 
60007c478bd9Sstevel@tonic-gate /*
60017c478bd9Sstevel@tonic-gate  * Parse a list of IRE flag characters into a bit field.
60027c478bd9Sstevel@tonic-gate  */
60037c478bd9Sstevel@tonic-gate static uint_t
60047c478bd9Sstevel@tonic-gate flag_bits(const char *arg)
60057c478bd9Sstevel@tonic-gate {
60067c478bd9Sstevel@tonic-gate 	const char *cp;
60077c478bd9Sstevel@tonic-gate 	uint_t val;
60087c478bd9Sstevel@tonic-gate 
60097c478bd9Sstevel@tonic-gate 	if (*arg == '\0')
60107c478bd9Sstevel@tonic-gate 		fatal(1, "missing flag list\n");
60117c478bd9Sstevel@tonic-gate 
60127c478bd9Sstevel@tonic-gate 	val = 0;
60137c478bd9Sstevel@tonic-gate 	while (*arg != '\0') {
60147c478bd9Sstevel@tonic-gate 		if ((cp = strchr(flag_list, *arg)) == NULL)
60157c478bd9Sstevel@tonic-gate 			fatal(1, "%c: illegal flag\n", *arg);
60167c478bd9Sstevel@tonic-gate 		val |= 1 << (cp - flag_list);
60177c478bd9Sstevel@tonic-gate 		arg++;
60187c478bd9Sstevel@tonic-gate 	}
60197c478bd9Sstevel@tonic-gate 	return (val);
60207c478bd9Sstevel@tonic-gate }
60217c478bd9Sstevel@tonic-gate 
60227c478bd9Sstevel@tonic-gate /*
60237c478bd9Sstevel@tonic-gate  * Handle -f argument.  Validate input format, sort by keyword, and
60247c478bd9Sstevel@tonic-gate  * save off digested results.
60257c478bd9Sstevel@tonic-gate  */
60267c478bd9Sstevel@tonic-gate static void
60277c478bd9Sstevel@tonic-gate process_filter(char *arg)
60287c478bd9Sstevel@tonic-gate {
60297c478bd9Sstevel@tonic-gate 	int idx;
60307c478bd9Sstevel@tonic-gate 	int klen = 0;
60317c478bd9Sstevel@tonic-gate 	char *cp, *cp2;
60327c478bd9Sstevel@tonic-gate 	int val;
60337c478bd9Sstevel@tonic-gate 	filter_t *newf;
60347c478bd9Sstevel@tonic-gate 	struct hostent *hp;
60357c478bd9Sstevel@tonic-gate 	int error_num;
60367c478bd9Sstevel@tonic-gate 	uint8_t *ucp;
60377c478bd9Sstevel@tonic-gate 	int maxv;
60387c478bd9Sstevel@tonic-gate 
60397c478bd9Sstevel@tonic-gate 	/* Look up the keyword first */
60407c478bd9Sstevel@tonic-gate 	if (strchr(arg, ':') == NULL) {
60417c478bd9Sstevel@tonic-gate 		idx = FK_AF;
60427c478bd9Sstevel@tonic-gate 	} else {
60437c478bd9Sstevel@tonic-gate 		for (idx = 0; idx < NFILTERKEYS; idx++) {
60447c478bd9Sstevel@tonic-gate 			klen = strlen(filter_keys[idx]);
60457c478bd9Sstevel@tonic-gate 			if (strncmp(filter_keys[idx], arg, klen) == 0 &&
60467c478bd9Sstevel@tonic-gate 			    arg[klen] == ':')
60477c478bd9Sstevel@tonic-gate 				break;
60487c478bd9Sstevel@tonic-gate 		}
60497c478bd9Sstevel@tonic-gate 		if (idx >= NFILTERKEYS)
60507c478bd9Sstevel@tonic-gate 			fatal(1, "%s: unknown filter keyword\n", arg);
60517c478bd9Sstevel@tonic-gate 
60527c478bd9Sstevel@tonic-gate 		/* Advance past keyword and separator. */
60537c478bd9Sstevel@tonic-gate 		arg += klen + 1;
60547c478bd9Sstevel@tonic-gate 	}
60557c478bd9Sstevel@tonic-gate 
60567c478bd9Sstevel@tonic-gate 	if ((newf = malloc(sizeof (*newf))) == NULL) {
60577c478bd9Sstevel@tonic-gate 		perror("filter");
60587c478bd9Sstevel@tonic-gate 		exit(1);
60597c478bd9Sstevel@tonic-gate 	}
60607c478bd9Sstevel@tonic-gate 	switch (idx) {
60617c478bd9Sstevel@tonic-gate 	case FK_AF:
60627c478bd9Sstevel@tonic-gate 		if (strcmp(arg, "inet") == 0) {
60637c478bd9Sstevel@tonic-gate 			newf->u.f_family = AF_INET;
60647c478bd9Sstevel@tonic-gate 		} else if (strcmp(arg, "inet6") == 0) {
60657c478bd9Sstevel@tonic-gate 			newf->u.f_family = AF_INET6;
60667c478bd9Sstevel@tonic-gate 		} else if (strcmp(arg, "unix") == 0) {
60677c478bd9Sstevel@tonic-gate 			newf->u.f_family = AF_UNIX;
60687c478bd9Sstevel@tonic-gate 		} else {
60697c478bd9Sstevel@tonic-gate 			newf->u.f_family = strtol(arg, &cp, 0);
60707c478bd9Sstevel@tonic-gate 			if (arg == cp || *cp != '\0')
60717c478bd9Sstevel@tonic-gate 				fatal(1, "%s: unknown address family.\n", arg);
60727c478bd9Sstevel@tonic-gate 		}
60737c478bd9Sstevel@tonic-gate 		break;
60747c478bd9Sstevel@tonic-gate 
60757c478bd9Sstevel@tonic-gate 	case FK_OUTIF:
60767c478bd9Sstevel@tonic-gate 		if (strcmp(arg, "none") == 0) {
60777c478bd9Sstevel@tonic-gate 			newf->u.f_ifname = NULL;
60787c478bd9Sstevel@tonic-gate 			break;
60797c478bd9Sstevel@tonic-gate 		}
60807c478bd9Sstevel@tonic-gate 		if (strcmp(arg, "any") == 0) {
60817c478bd9Sstevel@tonic-gate 			newf->u.f_ifname = "";
60827c478bd9Sstevel@tonic-gate 			break;
60837c478bd9Sstevel@tonic-gate 		}
60847c478bd9Sstevel@tonic-gate 		val = strtol(arg, &cp, 0);
60857c478bd9Sstevel@tonic-gate 		if (val <= 0 || arg == cp || cp[0] != '\0') {
60867c478bd9Sstevel@tonic-gate 			if ((val = if_nametoindex(arg)) == 0) {
60877c478bd9Sstevel@tonic-gate 				perror(arg);
60887c478bd9Sstevel@tonic-gate 				exit(1);
60897c478bd9Sstevel@tonic-gate 			}
60907c478bd9Sstevel@tonic-gate 		}
60917c478bd9Sstevel@tonic-gate 		newf->u.f_ifname = arg;
60927c478bd9Sstevel@tonic-gate 		break;
60937c478bd9Sstevel@tonic-gate 
60947c478bd9Sstevel@tonic-gate 	case FK_DST:
60957c478bd9Sstevel@tonic-gate 		V4MASK_TO_V6(IP_HOST_MASK, newf->u.a.f_mask);
60967c478bd9Sstevel@tonic-gate 		if (strcmp(arg, "any") == 0) {
60977c478bd9Sstevel@tonic-gate 			/* Special semantics; any address *but* zero */
60987c478bd9Sstevel@tonic-gate 			newf->u.a.f_address = NULL;
60997c478bd9Sstevel@tonic-gate 			(void) memset(&newf->u.a.f_mask, 0,
61007c478bd9Sstevel@tonic-gate 			    sizeof (newf->u.a.f_mask));
61017c478bd9Sstevel@tonic-gate 			break;
61027c478bd9Sstevel@tonic-gate 		}
61037c478bd9Sstevel@tonic-gate 		if (strcmp(arg, "none") == 0) {
61047c478bd9Sstevel@tonic-gate 			newf->u.a.f_address = NULL;
61057c478bd9Sstevel@tonic-gate 			break;
61067c478bd9Sstevel@tonic-gate 		}
61077c478bd9Sstevel@tonic-gate 		if ((cp = strrchr(arg, '/')) != NULL)
61087c478bd9Sstevel@tonic-gate 			*cp++ = '\0';
61097c478bd9Sstevel@tonic-gate 		hp = getipnodebyname(arg, AF_INET6, AI_V4MAPPED|AI_ALL,
61107c478bd9Sstevel@tonic-gate 		    &error_num);
61117c478bd9Sstevel@tonic-gate 		if (hp == NULL)
61127c478bd9Sstevel@tonic-gate 			fatal(1, "%s: invalid or unknown host address\n", arg);
61137c478bd9Sstevel@tonic-gate 		newf->u.a.f_address = hp;
61147c478bd9Sstevel@tonic-gate 		if (cp == NULL) {
61157c478bd9Sstevel@tonic-gate 			V4MASK_TO_V6(IP_HOST_MASK, newf->u.a.f_mask);
61167c478bd9Sstevel@tonic-gate 		} else {
61177c478bd9Sstevel@tonic-gate 			val = strtol(cp, &cp2, 0);
61187c478bd9Sstevel@tonic-gate 			if (cp != cp2 && cp2[0] == '\0') {
61197c478bd9Sstevel@tonic-gate 				/*
61207c478bd9Sstevel@tonic-gate 				 * If decode as "/n" works, then translate
61217c478bd9Sstevel@tonic-gate 				 * into a mask.
61227c478bd9Sstevel@tonic-gate 				 */
61237c478bd9Sstevel@tonic-gate 				if (hp->h_addr_list[0] != NULL &&
61247c478bd9Sstevel@tonic-gate 				    /* LINTED: (note 1) */
61257c478bd9Sstevel@tonic-gate 				    IN6_IS_ADDR_V4MAPPED((in6_addr_t
61267c478bd9Sstevel@tonic-gate 					*)hp->h_addr_list[0])) {
61277c478bd9Sstevel@tonic-gate 					maxv = IP_ABITS;
61287c478bd9Sstevel@tonic-gate 				} else {
61297c478bd9Sstevel@tonic-gate 					maxv = IPV6_ABITS;
61307c478bd9Sstevel@tonic-gate 				}
61317c478bd9Sstevel@tonic-gate 				if (val < 0 || val >= maxv)
61327c478bd9Sstevel@tonic-gate 					fatal(1, "%d: not in range 0 to %d\n",
61337c478bd9Sstevel@tonic-gate 					    val, maxv - 1);
61347c478bd9Sstevel@tonic-gate 				if (maxv == IP_ABITS)
61357c478bd9Sstevel@tonic-gate 					val += IPV6_ABITS - IP_ABITS;
61367c478bd9Sstevel@tonic-gate 				ucp = newf->u.a.f_mask.s6_addr;
61377c478bd9Sstevel@tonic-gate 				while (val >= 8)
61387c478bd9Sstevel@tonic-gate 					*ucp++ = 0xff, val -= 8;
61397c478bd9Sstevel@tonic-gate 				*ucp++ = (0xff << (8 - val)) & 0xff;
61407c478bd9Sstevel@tonic-gate 				while (ucp < newf->u.a.f_mask.s6_addr +
61417c478bd9Sstevel@tonic-gate 				    sizeof (newf->u.a.f_mask.s6_addr))
61427c478bd9Sstevel@tonic-gate 					*ucp++ = 0;
61437c478bd9Sstevel@tonic-gate 				/* Otherwise, try as numeric address */
61447c478bd9Sstevel@tonic-gate 			} else if (inet_pton(AF_INET6,
61457c478bd9Sstevel@tonic-gate 			    cp, &newf->u.a.f_mask) <= 0) {
61467c478bd9Sstevel@tonic-gate 				fatal(1, "%s: illegal mask format\n", cp);
61477c478bd9Sstevel@tonic-gate 			}
61487c478bd9Sstevel@tonic-gate 		}
61497c478bd9Sstevel@tonic-gate 		break;
61507c478bd9Sstevel@tonic-gate 
61517c478bd9Sstevel@tonic-gate 	case FK_FLAGS:
61527c478bd9Sstevel@tonic-gate 		if (*arg == '+') {
61537c478bd9Sstevel@tonic-gate 			newf->u.f.f_flagset = flag_bits(arg + 1);
61547c478bd9Sstevel@tonic-gate 			newf->u.f.f_flagclear = 0;
61557c478bd9Sstevel@tonic-gate 		} else if (*arg == '-') {
61567c478bd9Sstevel@tonic-gate 			newf->u.f.f_flagset = 0;
61577c478bd9Sstevel@tonic-gate 			newf->u.f.f_flagclear = flag_bits(arg + 1);
61587c478bd9Sstevel@tonic-gate 		} else {
61597c478bd9Sstevel@tonic-gate 			newf->u.f.f_flagset = flag_bits(arg);
61607c478bd9Sstevel@tonic-gate 			newf->u.f.f_flagclear = ~newf->u.f.f_flagset;
61617c478bd9Sstevel@tonic-gate 		}
61627c478bd9Sstevel@tonic-gate 		break;
61637c478bd9Sstevel@tonic-gate 
61647c478bd9Sstevel@tonic-gate 	default:
61657c478bd9Sstevel@tonic-gate 		assert(0);
61667c478bd9Sstevel@tonic-gate 	}
61677c478bd9Sstevel@tonic-gate 	newf->f_next = filters[idx];
61687c478bd9Sstevel@tonic-gate 	filters[idx] = newf;
61697c478bd9Sstevel@tonic-gate }
61707c478bd9Sstevel@tonic-gate 
61717c478bd9Sstevel@tonic-gate /* Determine if user wants this address family printed. */
61727c478bd9Sstevel@tonic-gate static boolean_t
61737c478bd9Sstevel@tonic-gate family_selected(int family)
61747c478bd9Sstevel@tonic-gate {
61757c478bd9Sstevel@tonic-gate 	const filter_t *fp;
61767c478bd9Sstevel@tonic-gate 
61777c478bd9Sstevel@tonic-gate 	if (v4compat && family == AF_INET6)
61787c478bd9Sstevel@tonic-gate 		return (B_FALSE);
61797c478bd9Sstevel@tonic-gate 	if ((fp = filters[FK_AF]) == NULL)
61807c478bd9Sstevel@tonic-gate 		return (B_TRUE);
61817c478bd9Sstevel@tonic-gate 	while (fp != NULL) {
61827c478bd9Sstevel@tonic-gate 		if (fp->u.f_family == family)
61837c478bd9Sstevel@tonic-gate 			return (B_TRUE);
61847c478bd9Sstevel@tonic-gate 		fp = fp->f_next;
61857c478bd9Sstevel@tonic-gate 	}
61867c478bd9Sstevel@tonic-gate 	return (B_FALSE);
61877c478bd9Sstevel@tonic-gate }
61887c478bd9Sstevel@tonic-gate 
61897c478bd9Sstevel@tonic-gate /*
61907c478bd9Sstevel@tonic-gate  * print the usage line
61917c478bd9Sstevel@tonic-gate  */
61927c478bd9Sstevel@tonic-gate static void
61937c478bd9Sstevel@tonic-gate usage(char *cmdname)
61947c478bd9Sstevel@tonic-gate {
61957c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "usage: %s [-anv] [-f address_family]\n",
61967c478bd9Sstevel@tonic-gate 	    cmdname);
61977c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "       %s [-n] [-f address_family] "
61987c478bd9Sstevel@tonic-gate 	    "[-P protocol] [-g | -p | -s [interval [count]]]\n", cmdname);
61997c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "       %s -m [-v] "
62007c478bd9Sstevel@tonic-gate 	    "[interval [count]]\n", cmdname);
62017c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "       %s -i [-I interface] [-an] "
62027c478bd9Sstevel@tonic-gate 	    "[-f address_family] [interval [count]]\n", cmdname);
62037c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "       %s -r [-anv] "
62047c478bd9Sstevel@tonic-gate 	    "[-f address_family|filter]\n", cmdname);
62057c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "       %s -M [-ns] [-f address_family]\n",
62067c478bd9Sstevel@tonic-gate 	    cmdname);
62077c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "       %s -D [-I interface] "
62087c478bd9Sstevel@tonic-gate 	    "[-f address_family]\n", cmdname);
62097c478bd9Sstevel@tonic-gate 	exit(EXIT_FAILURE);
62107c478bd9Sstevel@tonic-gate }
62117c478bd9Sstevel@tonic-gate 
62127c478bd9Sstevel@tonic-gate /*
62137c478bd9Sstevel@tonic-gate  * fatal: print error message to stderr and
62147c478bd9Sstevel@tonic-gate  * call exit(errcode)
62157c478bd9Sstevel@tonic-gate  */
62167c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/
62177c478bd9Sstevel@tonic-gate static void
621845916cd2Sjpk fatal(int errcode, char *format, ...)
621945916cd2Sjpk {
62207c478bd9Sstevel@tonic-gate 	va_list argp;
62217c478bd9Sstevel@tonic-gate 
62227c478bd9Sstevel@tonic-gate 	if (format == NULL)
62237c478bd9Sstevel@tonic-gate 		return;
62247c478bd9Sstevel@tonic-gate 
62257c478bd9Sstevel@tonic-gate 	va_start(argp, format);
62267c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, format, argp);
62277c478bd9Sstevel@tonic-gate 	va_end(argp);
62287c478bd9Sstevel@tonic-gate 
62297c478bd9Sstevel@tonic-gate 	exit(errcode);
62307c478bd9Sstevel@tonic-gate }
6231