xref: /dragonfly/usr.sbin/ifmcstat/ifmcstat.c (revision 19fe1c42)
1 /*
2  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the project nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD: src/usr.sbin/ifmcstat/ifmcstat.c,v 1.3.2.2 2001/07/03 11:02:06 ume Exp $
30  * $DragonFly: src/usr.sbin/ifmcstat/ifmcstat.c,v 1.9 2008/03/07 11:34:21 sephe Exp $
31  */
32 
33 #define _KERNEL_STRUCTURES
34 
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <fcntl.h>
38 #include <kvm.h>
39 #include <nlist.h>
40 #include <string.h>
41 #include <limits.h>
42 
43 #include <sys/types.h>
44 #include <sys/socket.h>
45 #include <sys/queue.h>
46 
47 #include <net/if.h>
48 #include <net/if_var.h>
49 #include <net/if_types.h>
50 #include <net/if_dl.h>
51 #include <netinet/in.h>
52 #include <netinet/if_ether.h>
53 #include <netinet/in_var.h>
54 #include <arpa/inet.h>
55 
56 #include <netdb.h>
57 
58 kvm_t	*kvmd;
59 
60 struct	nlist nl[] = {
61 #define	N_IFNET	0
62 	{ "_ifnet" },
63 	{ "" },
64 };
65 
66 const char *inet6_n2a(struct in6_addr *);
67 int main(int, char **);
68 char *ifname(struct ifnet *);
69 void kread(u_long, void *, int);
70 void if6_addrlist(struct ifaddr_container *);
71 void in6_multilist(struct in6_multi *);
72 struct in6_multi * in6_multientry(struct in6_multi *);
73 
74 #define	KREAD(addr, buf, type) \
75 	kread((u_long)addr, (void *)buf, sizeof(type))
76 
77 #ifdef N_IN6_MK
78 struct multi6_kludge {
79 	LIST_ENTRY(multi6_kludge) mk_entry;
80 	struct ifnet *mk_ifp;
81 	struct in6_multihead mk_head;
82 };
83 #endif
84 
85 const char *
86 inet6_n2a(struct in6_addr *p)
87 {
88 	static char buf[NI_MAXHOST];
89 	struct sockaddr_in6 sin6;
90 	u_int32_t scopeid;
91 #ifdef NI_WITHSCOPEID
92 	const int niflags = NI_NUMERICHOST | NI_WITHSCOPEID;
93 #else
94 	const int niflags = NI_NUMERICHOST;
95 #endif
96 
97 	memset(&sin6, 0, sizeof(sin6));
98 	sin6.sin6_family = AF_INET6;
99 	sin6.sin6_len = sizeof(struct sockaddr_in6);
100 	sin6.sin6_addr = *p;
101 	if (IN6_IS_ADDR_LINKLOCAL(p) || IN6_IS_ADDR_MC_LINKLOCAL(p)) {
102 		scopeid = ntohs(*(u_int16_t *)&sin6.sin6_addr.s6_addr[2]);
103 		if (scopeid) {
104 			sin6.sin6_scope_id = scopeid;
105 			sin6.sin6_addr.s6_addr[2] = 0;
106 			sin6.sin6_addr.s6_addr[3] = 0;
107 		}
108 	}
109 	if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
110 			buf, sizeof(buf), NULL, 0, niflags) == 0)
111 		return buf;
112 	else
113 		return "(invalid)";
114 }
115 
116 int
117 main(int argc __unused, char **argv __unused)
118 {
119 	char	buf[_POSIX2_LINE_MAX], ifname[IFNAMSIZ];
120 	struct	ifnet	*ifp, *nifp, ifnet;
121 	struct	arpcom	arpcom;
122 
123 	if ((kvmd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, buf)) == NULL) {
124 		perror("kvm_openfiles");
125 		exit(1);
126 	}
127 	if (kvm_nlist(kvmd, nl) < 0) {
128 		perror("kvm_nlist");
129 		exit(1);
130 	}
131 	if (nl[N_IFNET].n_value == 0) {
132 		printf("symbol %s not found\n", nl[N_IFNET].n_name);
133 		exit(1);
134 	}
135 	KREAD(nl[N_IFNET].n_value, &ifp, struct ifnet *);
136 	while (ifp) {
137 		struct ifaddrhead head;
138 		struct ifaddr_container ifac, *ifacp = NULL;
139 
140 		KREAD(ifp, &ifnet, struct ifnet);
141 		printf("%s:\n", if_indextoname(ifnet.if_index, ifname));
142 
143 		KREAD(ifnet.if_addrheads, &head, struct ifaddrhead);
144 		if (TAILQ_FIRST(&head) != NULL) {
145 			KREAD(TAILQ_FIRST(&head), &ifac,
146 			      struct ifaddr_container);
147 			ifacp = &ifac;
148 		}
149 
150 		if6_addrlist(ifacp);
151 		nifp = ifnet.if_link.tqe_next;
152 
153 		/* not supported */
154 
155 		ifp = nifp;
156 	}
157 
158 	exit(0);
159 	/*NOTREACHED*/
160 }
161 
162 char *
163 ifname(struct ifnet *ifp)
164 {
165 	static char buf[BUFSIZ];
166 	struct ifnet ifnet;
167 
168 	KREAD(ifp, &ifnet, struct ifnet);
169 	strlcpy(buf, ifnet.if_xname, sizeof(buf));
170 	return buf;
171 }
172 
173 void
174 kread(u_long addr, void *buf, int len)
175 {
176 	if (kvm_read(kvmd, addr, buf, len) != len) {
177 		perror("kvm_read");
178 		exit(1);
179 	}
180 }
181 
182 void
183 if6_addrlist(struct ifaddr_container *ifac)
184 {
185 	struct ifaddr *ifap;
186 	struct ifaddr ifa;
187 	struct sockaddr sa;
188 	struct in6_ifaddr if6a;
189 	struct in6_multi *mc = 0;
190 	struct ifaddr *ifap0;
191 
192 	if (ifac != NULL)
193 		ifap = ifac->ifa;
194 	else
195 		ifap = NULL;
196 
197 	ifap0 = ifap;
198 	while (ifap) {
199 		KREAD(ifap, &ifa, struct ifaddr);
200 		if (ifa.ifa_addr == NULL)
201 			goto nextifap;
202 		KREAD(ifa.ifa_addr, &sa, struct sockaddr);
203 		if (sa.sa_family != PF_INET6)
204 			goto nextifap;
205 		KREAD(ifap, &if6a, struct in6_ifaddr);
206 		printf("\tinet6 %s\n", inet6_n2a(&if6a.ia_addr.sin6_addr));
207 	nextifap:
208 		if (TAILQ_NEXT(ifac, ifa_link) == NULL) {
209 			ifap = NULL;
210 		} else {
211 			KREAD(TAILQ_NEXT(ifac, ifa_link), ifac,
212 			      struct ifaddr_container);
213 			ifap = ifac->ifa;
214 		}
215 	}
216 	if (ifap0) {
217 		struct ifnet ifnet;
218 		struct ifmultiaddr ifm, *ifmp = 0;
219 		struct sockaddr_in6 sin6;
220 		struct in6_multi in6m;
221 		struct sockaddr_dl sdl;
222 		int in6_multilist_done = 0;
223 
224 		KREAD(ifap0, &ifa, struct ifaddr);
225 		KREAD(ifa.ifa_ifp, &ifnet, struct ifnet);
226 		if (ifnet.if_multiaddrs.lh_first)
227 			ifmp = ifnet.if_multiaddrs.lh_first;
228 		while (ifmp) {
229 			KREAD(ifmp, &ifm, struct ifmultiaddr);
230 			if (ifm.ifma_addr == NULL)
231 				goto nextmulti;
232 			KREAD(ifm.ifma_addr, &sa, struct sockaddr);
233 			if (sa.sa_family != AF_INET6)
234 				goto nextmulti;
235 			in6_multientry((struct in6_multi *)ifm.ifma_protospec);
236 			if (ifm.ifma_lladdr == 0)
237 				goto nextmulti;
238 			KREAD(ifm.ifma_lladdr, &sdl, struct sockaddr_dl);
239 			printf("\t\t\tmcast-macaddr %s multicnt %d\n",
240 			       ether_ntoa((struct ether_addr *)LLADDR(&sdl)),
241 			       ifm.ifma_refcount);
242 		    nextmulti:
243 			ifmp = ifm.ifma_link.le_next;
244 		}
245 	}
246 #ifdef N_IN6_MK
247 	if (nl[N_IN6_MK].n_value != 0) {
248 		LIST_HEAD(in6_mktype, multi6_kludge) in6_mk;
249 		struct multi6_kludge *mkp, mk;
250 		char *nam;
251 
252 		KREAD(nl[N_IN6_MK].n_value, &in6_mk, struct in6_mktype);
253 		KREAD(ifap0, &ifa, struct ifaddr);
254 
255 		nam = strdup(ifname(ifa.ifa_ifp));
256 		if (!nam) {
257 			fprintf(stderr, "ifmcstat: not enough core\n");
258 			exit(1);
259 		}
260 
261 		for (mkp = in6_mk.lh_first; mkp; mkp = mk.mk_entry.le_next) {
262 			KREAD(mkp, &mk, struct multi6_kludge);
263 			if (strcmp(nam, ifname(mk.mk_ifp)) == 0 &&
264 			    mk.mk_head.lh_first) {
265 				printf("\t(on kludge entry for %s)\n", nam);
266 				in6_multilist(mk.mk_head.lh_first);
267 			}
268 		}
269 
270 		free(nam);
271 	}
272 #endif
273 }
274 
275 struct in6_multi *
276 in6_multientry(struct in6_multi *mc)
277 {
278 	struct in6_multi multi;
279 
280 	KREAD(mc, &multi, struct in6_multi);
281 	printf("\t\tgroup %s", inet6_n2a(&multi.in6m_addr));
282 	printf(" refcnt %u\n", multi.in6m_refcount);
283 	return(multi.in6m_entry.le_next);
284 }
285 
286 void
287 in6_multilist(struct in6_multi *mc)
288 {
289 	while (mc)
290 		mc = in6_multientry(mc);
291 }
292