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 $
32 #define _KERNEL_STRUCTURES
42 #include <sys/types.h>
43 #include <sys/socket.h>
44 #include <sys/queue.h>
47 #include <net/if_var.h>
48 #include <net/if_types.h>
49 #include <net/if_dl.h>
50 #include <netinet/in.h>
51 #include <netinet/if_ether.h>
52 #include <netinet/in_var.h>
53 #include <arpa/inet.h>
61 { .n_name = "_ifnet" },
65 const char *inet6_n2a(struct in6_addr *);
66 int main(int, char **);
67 char *ifname(struct ifnet *);
68 void kread(u_long, void *, int);
69 void if6_addrlist(struct ifaddr_container *);
70 void in6_multilist(struct in6_multi *);
71 struct in6_multi * in6_multientry(struct in6_multi *);
73 #define KREAD(addr, buf, type) \
74 kread((u_long)addr, (void *)buf, sizeof(type))
77 struct multi6_kludge {
78 LIST_ENTRY(multi6_kludge) mk_entry;
80 struct in6_multihead mk_head;
85 inet6_n2a(struct in6_addr *p)
87 static char buf[NI_MAXHOST];
88 struct sockaddr_in6 sin6;
91 const int niflags = NI_NUMERICHOST | NI_WITHSCOPEID;
93 const int niflags = NI_NUMERICHOST;
96 memset(&sin6, 0, sizeof(sin6));
97 sin6.sin6_family = AF_INET6;
98 sin6.sin6_len = sizeof(struct sockaddr_in6);
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]);
103 sin6.sin6_scope_id = scopeid;
104 sin6.sin6_addr.s6_addr[2] = 0;
105 sin6.sin6_addr.s6_addr[3] = 0;
108 if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
109 buf, sizeof(buf), NULL, 0, niflags) == 0)
116 main(int argc __unused, char **argv __unused)
118 char buf[_POSIX2_LINE_MAX], ifname[IFNAMSIZ];
119 struct ifnet *ifp, *nifp, ifnet;
121 if ((kvmd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, buf)) == NULL) {
122 perror("kvm_openfiles");
125 if (kvm_nlist(kvmd, nl) < 0) {
129 if (nl[N_IFNET].n_value == 0) {
130 printf("symbol %s not found\n", nl[N_IFNET].n_name);
133 KREAD(nl[N_IFNET].n_value, &ifp, struct ifnet *);
135 struct ifaddrhead head;
136 struct ifaddr_container ifac, *ifacp = NULL;
138 KREAD(ifp, &ifnet, struct ifnet);
139 printf("%s:\n", if_indextoname(ifnet.if_index, ifname));
141 KREAD(ifnet.if_addrheads, &head, struct ifaddrhead);
142 if (TAILQ_FIRST(&head) != NULL) {
143 KREAD(TAILQ_FIRST(&head), &ifac,
144 struct ifaddr_container);
149 nifp = ifnet.if_link.tqe_next;
161 ifname(struct ifnet *ifp)
163 static char buf[BUFSIZ];
166 KREAD(ifp, &ifnet, struct ifnet);
167 strlcpy(buf, ifnet.if_xname, sizeof(buf));
172 kread(u_long addr, void *buf, int len)
174 if (kvm_read(kvmd, addr, buf, len) != len) {
181 if6_addrlist(struct ifaddr_container *ifac)
186 struct in6_ifaddr if6a;
187 struct ifaddr *ifap0;
196 KREAD(ifap, &ifa, struct ifaddr);
197 if (ifa.ifa_addr == NULL)
199 KREAD(ifa.ifa_addr, &sa, struct sockaddr);
200 if (sa.sa_family != PF_INET6)
202 KREAD(ifap, &if6a, struct in6_ifaddr);
203 printf("\tinet6 %s\n", inet6_n2a(&if6a.ia_addr.sin6_addr));
205 if (TAILQ_NEXT(ifac, ifa_link) == NULL) {
208 KREAD(TAILQ_NEXT(ifac, ifa_link), ifac,
209 struct ifaddr_container);
215 struct ifmultiaddr ifm, *ifmp = 0;
216 struct sockaddr_dl sdl;
218 KREAD(ifap0, &ifa, struct ifaddr);
219 KREAD(ifa.ifa_ifp, &ifnet, struct ifnet);
220 if (TAILQ_FIRST(&ifnet.if_multiaddrs))
221 ifmp = TAILQ_FIRST(&ifnet.if_multiaddrs);
223 KREAD(ifmp, &ifm, struct ifmultiaddr);
224 if (ifm.ifma_addr == NULL)
226 KREAD(ifm.ifma_addr, &sa, struct sockaddr);
227 if (sa.sa_family != AF_INET6)
229 in6_multientry((struct in6_multi *)ifm.ifma_protospec);
230 if (ifm.ifma_lladdr == 0)
232 KREAD(ifm.ifma_lladdr, &sdl, struct sockaddr_dl);
233 printf("\t\t\tmcast-macaddr %s multicnt %d\n",
234 ether_ntoa((struct ether_addr *)LLADDR(&sdl)),
237 ifmp = TAILQ_NEXT(&ifm, ifma_link);
241 if (nl[N_IN6_MK].n_value != 0) {
242 LIST_HEAD(in6_mktype, multi6_kludge) in6_mk;
243 struct multi6_kludge *mkp, mk;
246 KREAD(nl[N_IN6_MK].n_value, &in6_mk, struct in6_mktype);
247 KREAD(ifap0, &ifa, struct ifaddr);
249 nam = strdup(ifname(ifa.ifa_ifp));
251 fprintf(stderr, "ifmcstat: not enough core\n");
255 for (mkp = in6_mk.lh_first; mkp; mkp = mk.mk_entry.le_next) {
256 KREAD(mkp, &mk, struct multi6_kludge);
257 if (strcmp(nam, ifname(mk.mk_ifp)) == 0 &&
258 mk.mk_head.lh_first) {
259 printf("\t(on kludge entry for %s)\n", nam);
260 in6_multilist(mk.mk_head.lh_first);
270 in6_multientry(struct in6_multi *mc)
272 struct in6_multi multi;
274 KREAD(mc, &multi, struct in6_multi);
275 printf("\t\tgroup %s", inet6_n2a(&multi.in6m_addr));
276 printf(" refcnt %u\n", multi.in6m_refcount);
277 return(multi.in6m_entry.le_next);
281 in6_multilist(struct in6_multi *mc)
284 mc = in6_multientry(mc);