xref: /netbsd-src/usr.sbin/ifmcstat/ifmcstat.c (revision 2c6fc41c810f5088457889d00eba558e8bc74d9e)
1 /*	$NetBSD: ifmcstat.c,v 1.16 2014/05/30 22:20:48 joerg Exp $	*/
2 
3 /*
4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the project nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <fcntl.h>
35 #include <kvm.h>
36 #include <nlist.h>
37 #include <string.h>
38 #include <limits.h>
39 
40 #include <sys/types.h>
41 #include <sys/socket.h>
42 #include <net/if.h>
43 #include <net/if_types.h>
44 #include <net/if_dl.h>
45 #include <netinet/in.h>
46 #include <net/if_ether.h>
47 #include <netinet/in_var.h>
48 #include <arpa/inet.h>
49 
50 #include <netdb.h>
51 
52 kvm_t	*kvmd;
53 
54 struct	nlist nl[] = {
55 #define	N_IFNET_LIST	0
56 	{ "_ifnet_list", 0, 0, 0, 0 },
57 #define N_IN6_MK 1
58 	{ "_in6_mk", 0, 0, 0, 0 },
59 	{ "", 0, 0, 0, 0 },
60 };
61 
62 static const char *inet6_n2a(struct in6_addr *);
63 static char *ifname(struct ifnet *);
64 static void kread(u_long, void *, int);
65 static void acmc(struct ether_multi *);
66 static void if6_addrlist(struct ifaddr *);
67 static void in6_multilist(struct in6_multi *);
68 
69 #define	KREAD(addr, buf, type) \
70 	kread((u_long)addr, (void *)buf, sizeof(type))
71 
72 struct multi6_kludge {
73 	LIST_ENTRY(multi6_kludge) mk_entry;
74 	struct ifnet *mk_ifp;
75 	struct in6_multihead mk_head;
76 };
77 
78 static const char *
79 inet6_n2a(struct in6_addr *p)
80 {
81 	static char buf[NI_MAXHOST];
82 	struct sockaddr_in6 sin6;
83 	const int niflags = NI_NUMERICHOST;
84 
85 	memset(&sin6, 0, sizeof(sin6));
86 	sin6.sin6_family = AF_INET6;
87 	sin6.sin6_len = sizeof(struct sockaddr_in6);
88 	sin6.sin6_addr = *p;
89 	inet6_getscopeid(&sin6, INET6_IS_ADDR_LINKLOCAL|
90 	    INET6_IS_ADDR_MC_LINKLOCAL);
91 	if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
92 			buf, sizeof(buf), NULL, 0, niflags) == 0)
93 		return buf;
94 	else
95 		return "(invalid)";
96 }
97 
98 int
99 main(void)
100 {
101 	char	buf[_POSIX2_LINE_MAX], ifnam[IFNAMSIZ];
102 	struct	ifnet	*ifp, *nifp, ifnet;
103 	struct ethercom ec;
104 	union {
105 		struct sockaddr_storage st;
106 		struct sockaddr_dl sdl;
107 	} su;
108 	struct sockaddr_dl *sdlp;
109 	sdlp = &su.sdl;
110 
111 	if ((kvmd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, buf)) == NULL) {
112 		perror("kvm_openfiles");
113 		exit(1);
114 	}
115 	if (kvm_nlist(kvmd, nl) < 0) {
116 		perror("kvm_nlist");
117 		exit(1);
118 	}
119 	if (nl[N_IFNET_LIST].n_value == 0) {
120 		printf("symbol %s not found\n", nl[N_IFNET_LIST].n_name);
121 		exit(1);
122 	}
123 	KREAD(nl[N_IFNET_LIST].n_value, &ifp, struct ifnet *);
124 	while (ifp) {
125 		KREAD(ifp, &ifnet, struct ifnet);
126 		printf("%s:\n", if_indextoname(ifnet.if_index, ifnam));
127 
128 		if6_addrlist(ifnet.if_addrlist.tqh_first);
129 		nifp = ifnet.if_list.tqe_next;
130 
131 		KREAD(ifnet.if_sadl, sdlp, struct sockaddr_dl);
132 		if (sdlp->sdl_type == IFT_ETHER) {
133 			/* If we didn't get all of it, try again */
134 			if (sdlp->sdl_len > sizeof(struct sockaddr_dl))
135 				kread((u_long)ifnet.if_sadl, (void *)sdlp, sdlp->sdl_len);
136 			printf("\tenaddr %s",
137 			       ether_ntoa((struct ether_addr *)LLADDR(sdlp)));
138 			KREAD(ifp, &ec, struct ethercom);
139 			printf(" multicnt %d", ec.ec_multicnt);
140 			acmc(ec.ec_multiaddrs.lh_first);
141 			printf("\n");
142 		}
143 
144 		ifp = nifp;
145 	}
146 
147 	exit(0);
148 	/*NOTREACHED*/
149 }
150 
151 static char *
152 ifname(struct ifnet *ifp)
153 {
154 	static char buf[BUFSIZ];
155 	struct ifnet ifnet;
156 
157 	KREAD(ifp, &ifnet, struct ifnet);
158 	strncpy(buf, ifnet.if_xname, BUFSIZ);
159 	return buf;
160 }
161 
162 static void
163 kread(u_long addr, void *buf, int len)
164 {
165 	if (kvm_read(kvmd, addr, buf, len) != len) {
166 		perror("kvm_read");
167 		exit(1);
168 	}
169 }
170 
171 static void
172 acmc(struct ether_multi *am)
173 {
174 	struct ether_multi em;
175 
176 	while (am) {
177 		KREAD(am, &em, struct ether_multi);
178 
179 		printf("\n\t\t");
180 		printf("%s -- ", ether_ntoa((struct ether_addr *)em.enm_addrlo));
181 		printf("%s ", ether_ntoa((struct ether_addr *)&em.enm_addrhi));
182 		printf("%d", em.enm_refcount);
183 		am = em.enm_list.le_next;
184 	}
185 }
186 
187 static void
188 if6_addrlist(struct ifaddr *ifap)
189 {
190 	struct ifaddr ifa;
191 	struct sockaddr sa;
192 	struct in6_ifaddr if6a;
193 	struct in6_multi *mc = 0;
194 	struct ifaddr *ifap0;
195 
196 	ifap0 = ifap;
197 	while (ifap) {
198 		KREAD(ifap, &ifa, struct ifaddr);
199 		if (ifa.ifa_addr == NULL)
200 			goto nextifap;
201 		KREAD(ifa.ifa_addr, &sa, struct sockaddr);
202 		if (sa.sa_family != PF_INET6)
203 			goto nextifap;
204 		KREAD(ifap, &if6a, struct in6_ifaddr);
205 		printf("\tinet6 %s\n", inet6_n2a(&if6a.ia_addr.sin6_addr));
206 		mc = mc ? mc : if6a.ia6_multiaddrs.lh_first;
207 	nextifap:
208 		ifap = ifa.ifa_list.tqe_next;
209 	}
210 	if (mc)
211 		in6_multilist(mc);
212 	if (nl[N_IN6_MK].n_value != 0) {
213 		LIST_HEAD(in6_mktype, multi6_kludge) in6_mk;
214 		struct multi6_kludge *mkp, mk;
215 		char *nam;
216 
217 		KREAD(nl[N_IN6_MK].n_value, &in6_mk, struct in6_mktype);
218 		KREAD(ifap0, &ifa, struct ifaddr);
219 
220 		nam = strdup(ifname(ifa.ifa_ifp));
221 
222 		for (mkp = in6_mk.lh_first; mkp; mkp = mk.mk_entry.le_next) {
223 			KREAD(mkp, &mk, struct multi6_kludge);
224 			if (strcmp(nam, ifname(mk.mk_ifp)) == 0 &&
225 			    mk.mk_head.lh_first) {
226 				printf("\t(on kludge entry for %s)\n", nam);
227 				in6_multilist(mk.mk_head.lh_first);
228 			}
229 		}
230 
231 		free(nam);
232 	}
233 }
234 
235 static void
236 in6_multilist(struct in6_multi *mc)
237 {
238 	struct in6_multi multi;
239 
240 	while (mc) {
241 		KREAD(mc, &multi, struct in6_multi);
242 		printf("\t\tgroup %s", inet6_n2a(&multi.in6m_addr));
243 		printf(" refcnt %u\n", multi.in6m_refcount);
244 		mc = multi.in6m_entry.le_next;
245 	}
246 }
247