2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
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.4 2003/11/03 19:31:37 eirikn Exp $
33 #define _KERNEL_STRUCTURES
43 #include <sys/types.h>
44 #include <sys/socket.h>
45 #include <sys/queue.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>
66 const char *inet6_n2a(struct in6_addr *);
68 char *ifname(struct ifnet *);
69 void kread(u_long, void *, int);
70 void if6_addrlist(struct ifaddr *);
71 void in6_multilist(struct in6_multi *);
72 struct in6_multi * in6_multientry(struct in6_multi *);
74 #define KREAD(addr, buf, type) \
75 kread((u_long)addr, (void *)buf, sizeof(type))
78 struct multi6_kludge {
79 LIST_ENTRY(multi6_kludge) mk_entry;
81 struct in6_multihead mk_head;
85 const char *inet6_n2a(p)
88 static char buf[NI_MAXHOST];
89 struct sockaddr_in6 sin6;
92 const int niflags = NI_NUMERICHOST | NI_WITHSCOPEID;
94 const int niflags = NI_NUMERICHOST;
97 memset(&sin6, 0, sizeof(sin6));
98 sin6.sin6_family = AF_INET6;
99 sin6.sin6_len = sizeof(struct sockaddr_in6);
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]);
104 sin6.sin6_scope_id = scopeid;
105 sin6.sin6_addr.s6_addr[2] = 0;
106 sin6.sin6_addr.s6_addr[3] = 0;
109 if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
110 buf, sizeof(buf), NULL, 0, niflags) == 0)
118 char buf[_POSIX2_LINE_MAX], ifname[IFNAMSIZ];
119 struct ifnet *ifp, *nifp, ifnet;
120 struct arpcom arpcom;
122 if ((kvmd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, buf)) == NULL) {
123 perror("kvm_openfiles");
126 if (kvm_nlist(kvmd, nl) < 0) {
130 if (nl[N_IFNET].n_value == 0) {
131 printf("symbol %s not found\n", nl[N_IFNET].n_name);
134 KREAD(nl[N_IFNET].n_value, &ifp, struct ifnet *);
136 KREAD(ifp, &ifnet, struct ifnet);
137 printf("%s:\n", if_indextoname(ifnet.if_index, ifname));
139 if6_addrlist(TAILQ_FIRST(&ifnet.if_addrhead));
140 nifp = ifnet.if_link.tqe_next;
154 static char buf[BUFSIZ];
156 char ifnamebuf[IFNAMSIZ];
158 KREAD(ifp, &ifnet, struct ifnet);
159 KREAD(ifnet.if_name, ifnamebuf, sizeof(ifnamebuf));
160 snprintf(buf, sizeof(buf), "%s%d", ifnamebuf,
161 ifnet.if_unit); /* does snprintf allow overlap copy?? */
165 void kread(addr, buf, len)
170 if (kvm_read(kvmd, addr, buf, len) != len) {
182 struct in6_ifaddr if6a;
183 struct in6_multi *mc = 0;
184 struct ifaddr *ifap0;
188 KREAD(ifap, &ifa, struct ifaddr);
189 if (ifa.ifa_addr == NULL)
191 KREAD(ifa.ifa_addr, &sa, struct sockaddr);
192 if (sa.sa_family != PF_INET6)
194 KREAD(ifap, &if6a, struct in6_ifaddr);
195 printf("\tinet6 %s\n", inet6_n2a(&if6a.ia_addr.sin6_addr));
197 ifap = ifa.ifa_link.tqe_next;
201 struct ifmultiaddr ifm, *ifmp = 0;
202 struct sockaddr_in6 sin6;
203 struct in6_multi in6m;
204 struct sockaddr_dl sdl;
205 int in6_multilist_done = 0;
207 KREAD(ifap0, &ifa, struct ifaddr);
208 KREAD(ifa.ifa_ifp, &ifnet, struct ifnet);
209 if (ifnet.if_multiaddrs.lh_first)
210 ifmp = ifnet.if_multiaddrs.lh_first;
212 KREAD(ifmp, &ifm, struct ifmultiaddr);
213 if (ifm.ifma_addr == NULL)
215 KREAD(ifm.ifma_addr, &sa, struct sockaddr);
216 if (sa.sa_family != AF_INET6)
218 (void)in6_multientry((struct in6_multi *)
220 if (ifm.ifma_lladdr == 0)
222 KREAD(ifm.ifma_lladdr, &sdl, struct sockaddr_dl);
223 printf("\t\t\tmcast-macaddr %s multicnt %d\n",
224 ether_ntoa((struct ether_addr *)LLADDR(&sdl)),
227 ifmp = ifm.ifma_link.le_next;
231 if (nl[N_IN6_MK].n_value != 0) {
232 LIST_HEAD(in6_mktype, multi6_kludge) in6_mk;
233 struct multi6_kludge *mkp, mk;
236 KREAD(nl[N_IN6_MK].n_value, &in6_mk, struct in6_mktype);
237 KREAD(ifap0, &ifa, struct ifaddr);
239 nam = strdup(ifname(ifa.ifa_ifp));
241 fprintf(stderr, "ifmcstat: not enough core\n");
245 for (mkp = in6_mk.lh_first; mkp; mkp = mk.mk_entry.le_next) {
246 KREAD(mkp, &mk, struct multi6_kludge);
247 if (strcmp(nam, ifname(mk.mk_ifp)) == 0 &&
248 mk.mk_head.lh_first) {
249 printf("\t(on kludge entry for %s)\n", nam);
250 in6_multilist(mk.mk_head.lh_first);
261 struct in6_multi *mc;
263 struct in6_multi multi;
265 KREAD(mc, &multi, struct in6_multi);
266 printf("\t\tgroup %s", inet6_n2a(&multi.in6m_addr));
267 printf(" refcnt %u\n", multi.in6m_refcount);
268 return(multi.in6m_entry.le_next);
273 struct in6_multi *mc;
276 mc = in6_multientry(mc);