xref: /freebsd/usr.sbin/ifmcstat/ifmcstat.c (revision 3a31b7eb32ad60e1e05b2b2e184ff47e4afbb874)
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$
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 <sys/queue.h>
43 
44 #include <net/if.h>
45 #include <net/if_var.h>
46 #include <net/if_types.h>
47 #include <net/if_dl.h>
48 #include <netinet/in.h>
49 #define	_KERNEL
50 #include <netinet/if_ether.h>
51 #undef	_KERNEL
52 #include <netinet/in_var.h>
53 #include <arpa/inet.h>
54 
55 #include <netdb.h>
56 
57 kvm_t	*kvmd;
58 
59 struct	nlist nl[] = {
60 #define	N_IFNET	0
61 	{ "_ifnet" },
62 	{ "" },
63 };
64 
65 const char *inet6_n2a __P((struct in6_addr *));
66 int main __P((void));
67 char *ifname __P((struct ifnet *));
68 void kread __P((u_long, void *, int));
69 void if6_addrlist __P((struct ifaddr *));
70 void in6_multilist __P((struct in6_multi *));
71 struct in6_multi * in6_multientry __P((struct in6_multi *));
72 
73 #define	KREAD(addr, buf, type) \
74 	kread((u_long)addr, (void *)buf, sizeof(type))
75 
76 #ifdef N_IN6_MK
77 struct multi6_kludge {
78 	LIST_ENTRY(multi6_kludge) mk_entry;
79 	struct ifnet *mk_ifp;
80 	struct in6_multihead mk_head;
81 };
82 #endif
83 
84 const char *inet6_n2a(p)
85 	struct in6_addr *p;
86 {
87 	static char buf[NI_MAXHOST];
88 	struct sockaddr_in6 sin6;
89 	u_int32_t scopeid;
90 #ifdef NI_WITHSCOPEID
91 	const int niflags = NI_NUMERICHOST | NI_WITHSCOPEID;
92 #else
93 	const int niflags = NI_NUMERICHOST;
94 #endif
95 
96 	memset(&sin6, 0, sizeof(sin6));
97 	sin6.sin6_family = AF_INET6;
98 	sin6.sin6_len = sizeof(struct sockaddr_in6);
99 	sin6.sin6_addr = *p;
100 	if (IN6_IS_ADDR_LINKLOCAL(p) || IN6_IS_ADDR_MC_LINKLOCAL(p)) {
101 		scopeid = ntohs(*(u_int16_t *)&sin6.sin6_addr.s6_addr[2]);
102 		if (scopeid) {
103 			sin6.sin6_scope_id = scopeid;
104 			sin6.sin6_addr.s6_addr[2] = 0;
105 			sin6.sin6_addr.s6_addr[3] = 0;
106 		}
107 	}
108 	if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
109 			buf, sizeof(buf), NULL, 0, niflags) == 0)
110 		return buf;
111 	else
112 		return "(invalid)";
113 }
114 
115 int main()
116 {
117 	char	buf[_POSIX2_LINE_MAX], ifname[IFNAMSIZ];
118 	struct	ifnet	*ifp, *nifp, ifnet;
119 
120 	if ((kvmd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, buf)) == NULL) {
121 		perror("kvm_openfiles");
122 		exit(1);
123 	}
124 	if (kvm_nlist(kvmd, nl) < 0) {
125 		perror("kvm_nlist");
126 		exit(1);
127 	}
128 	if (nl[N_IFNET].n_value == 0) {
129 		printf("symbol %s not found\n", nl[N_IFNET].n_name);
130 		exit(1);
131 	}
132 	KREAD(nl[N_IFNET].n_value, &ifp, struct ifnet *);
133 	while (ifp) {
134 		KREAD(ifp, &ifnet, struct ifnet);
135 		printf("%s:\n", if_indextoname(ifnet.if_index, ifname));
136 
137 		if6_addrlist(TAILQ_FIRST(&ifnet.if_addrhead));
138 		nifp = TAILQ_NEXT(&ifnet, if_link);
139 
140 		/* not supported */
141 
142 		ifp = nifp;
143 	}
144 
145 	exit(0);
146 	/*NOTREACHED*/
147 }
148 
149 char *ifname(ifp)
150 	struct ifnet *ifp;
151 {
152 	static char buf[BUFSIZ];
153 	struct ifnet ifnet;
154 	char ifnamebuf[IFNAMSIZ];
155 
156 	KREAD(ifp, &ifnet, struct ifnet);
157 	KREAD(ifnet.if_name, ifnamebuf, sizeof(ifnamebuf));
158 	snprintf(buf, sizeof(buf), "%s%d", ifnamebuf,
159 		 ifnet.if_unit); /* does snprintf allow overlap copy?? */
160 	return buf;
161 }
162 
163 void kread(addr, buf, len)
164 	u_long addr;
165 	void *buf;
166 	int len;
167 {
168 	if (kvm_read(kvmd, addr, buf, len) != len) {
169 		perror("kvm_read");
170 		exit(1);
171 	}
172 }
173 
174 void
175 if6_addrlist(ifap)
176 	struct ifaddr *ifap;
177 {
178 	struct ifaddr ifa;
179 	struct sockaddr sa;
180 	struct in6_ifaddr if6a;
181 	struct ifaddr *ifap0;
182 
183 	ifap0 = ifap;
184 	while (ifap) {
185 		KREAD(ifap, &ifa, struct ifaddr);
186 		if (ifa.ifa_addr == NULL)
187 			goto nextifap;
188 		KREAD(ifa.ifa_addr, &sa, struct sockaddr);
189 		if (sa.sa_family != PF_INET6)
190 			goto nextifap;
191 		KREAD(ifap, &if6a, struct in6_ifaddr);
192 		printf("\tinet6 %s\n", inet6_n2a(&if6a.ia_addr.sin6_addr));
193 	nextifap:
194 		ifap = TAILQ_NEXT(&ifa, ifa_link);
195 	}
196 	if (ifap0) {
197 		struct ifnet ifnet;
198 		struct ifmultiaddr ifm, *ifmp = 0;
199 		struct sockaddr_dl sdl;
200 
201 		KREAD(ifap0, &ifa, struct ifaddr);
202 		KREAD(ifa.ifa_ifp, &ifnet, struct ifnet);
203 		if (TAILQ_FIRST(&ifnet.if_multiaddrs))
204 			ifmp = TAILQ_FIRST(&ifnet.if_multiaddrs);
205 		while (ifmp) {
206 			KREAD(ifmp, &ifm, struct ifmultiaddr);
207 			if (ifm.ifma_addr == NULL)
208 				goto nextmulti;
209 			KREAD(ifm.ifma_addr, &sa, struct sockaddr);
210 			if (sa.sa_family != AF_INET6)
211 				goto nextmulti;
212 			(void)in6_multientry((struct in6_multi *)
213 					     ifm.ifma_protospec);
214 			if (ifm.ifma_lladdr == 0)
215 				goto nextmulti;
216 			KREAD(ifm.ifma_lladdr, &sdl, struct sockaddr_dl);
217 			printf("\t\t\tmcast-macaddr %s multicnt %d\n",
218 			       ether_ntoa((struct ether_addr *)LLADDR(&sdl)),
219 			       ifm.ifma_refcount);
220 		    nextmulti:
221 			ifmp = TAILQ_NEXT(&ifm, ifma_link);
222 		}
223 	}
224 #ifdef N_IN6_MK
225 	if (nl[N_IN6_MK].n_value != 0) {
226 		LIST_HEAD(in6_mktype, multi6_kludge) in6_mk;
227 		struct multi6_kludge *mkp, mk;
228 		char *nam;
229 
230 		KREAD(nl[N_IN6_MK].n_value, &in6_mk, struct in6_mktype);
231 		KREAD(ifap0, &ifa, struct ifaddr);
232 
233 		nam = strdup(ifname(ifa.ifa_ifp));
234 		if (!nam) {
235 			fprintf(stderr, "ifmcstat: not enough core\n");
236 			exit(1);
237 		}
238 
239 		LIST_FOREACH(mkp, &in6_mk, mk_entry) {
240 			KREAD(mkp, &mk, struct multi6_kludge);
241 			if (strcmp(nam, ifname(mk.mk_ifp)) == 0 &&
242 			    LIST_FIRST(&mk.mk_head)) {
243 				printf("\t(on kludge entry for %s)\n", nam);
244 				in6_multilist(LIST_FIRST(&mk.mk_head));
245 			}
246 		}
247 
248 		free(nam);
249 	}
250 #endif
251 }
252 
253 struct in6_multi *
254 in6_multientry(mc)
255 	struct in6_multi *mc;
256 {
257 	struct in6_multi multi;
258 
259 	KREAD(mc, &multi, struct in6_multi);
260 	printf("\t\tgroup %s", inet6_n2a(&multi.in6m_addr));
261 	printf(" refcnt %u\n", multi.in6m_refcount);
262 	return(LIST_NEXT(&multi, in6m_entry));
263 }
264 
265 void
266 in6_multilist(mc)
267 	struct in6_multi *mc;
268 {
269 	while (mc)
270 		mc = in6_multientry(mc);
271 }
272