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 */ 31 32 #define _KERNEL_STRUCTURES 33 34 #include <stdio.h> 35 #include <stdlib.h> 36 #include <fcntl.h> 37 #include <kvm.h> 38 #include <nlist.h> 39 #include <string.h> 40 #include <limits.h> 41 42 #include <sys/types.h> 43 #include <sys/socket.h> 44 #include <sys/queue.h> 45 46 #include <net/if.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> 54 55 #include <netdb.h> 56 57 kvm_t *kvmd; 58 59 struct nlist nl[] = { 60 #define N_IFNET 0 61 { .n_name = "_ifnet" }, 62 { .n_name = "" }, 63 }; 64 65 const char *inet6_n2a(struct in6_addr *); 66 char *ifname(struct ifnet *); 67 void kread(u_long, void *, int); 68 void if6_addrlist(struct ifaddr_container *); 69 void in6_multilist(struct in6_multi *); 70 struct in6_multi * in6_multientry(struct in6_multi *); 71 72 #define KREAD(addr, buf, type) \ 73 kread((u_long)addr, (void *)buf, sizeof(type)) 74 75 #ifdef N_IN6_MK 76 struct multi6_kludge { 77 LIST_ENTRY(multi6_kludge) mk_entry; 78 struct ifnet *mk_ifp; 79 struct in6_multihead mk_head; 80 }; 81 #endif 82 83 const char * 84 inet6_n2a(struct in6_addr *p) 85 { 86 static char buf[NI_MAXHOST]; 87 struct sockaddr_in6 sin6; 88 u_int32_t scopeid; 89 #ifdef NI_WITHSCOPEID 90 const int niflags = NI_NUMERICHOST | NI_WITHSCOPEID; 91 #else 92 const int niflags = NI_NUMERICHOST; 93 #endif 94 95 memset(&sin6, 0, sizeof(sin6)); 96 sin6.sin6_family = AF_INET6; 97 sin6.sin6_len = sizeof(struct sockaddr_in6); 98 sin6.sin6_addr = *p; 99 if (IN6_IS_ADDR_LINKLOCAL(p) || IN6_IS_ADDR_MC_LINKLOCAL(p)) { 100 scopeid = ntohs(*(u_int16_t *)&sin6.sin6_addr.s6_addr[2]); 101 if (scopeid) { 102 sin6.sin6_scope_id = scopeid; 103 sin6.sin6_addr.s6_addr[2] = 0; 104 sin6.sin6_addr.s6_addr[3] = 0; 105 } 106 } 107 if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len, 108 buf, sizeof(buf), NULL, 0, niflags) == 0) 109 return buf; 110 else 111 return "(invalid)"; 112 } 113 114 int 115 main(int argc __unused, char **argv __unused) 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 struct ifaddrhead head; 135 struct ifaddr_container ifac, *ifacp = NULL; 136 137 KREAD(ifp, &ifnet, struct ifnet); 138 printf("%s:\n", if_indextoname(ifnet.if_index, ifname)); 139 140 KREAD(ifnet.if_addrheads, &head, struct ifaddrhead); 141 if (TAILQ_FIRST(&head) != NULL) { 142 KREAD(TAILQ_FIRST(&head), &ifac, 143 struct ifaddr_container); 144 ifacp = &ifac; 145 } 146 147 if6_addrlist(ifacp); 148 nifp = ifnet.if_link.tqe_next; 149 150 /* not supported */ 151 152 ifp = nifp; 153 } 154 155 exit(0); 156 /*NOTREACHED*/ 157 } 158 159 char * 160 ifname(struct ifnet *ifp) 161 { 162 static char buf[BUFSIZ]; 163 struct ifnet ifnet; 164 165 KREAD(ifp, &ifnet, struct ifnet); 166 strlcpy(buf, ifnet.if_xname, sizeof(buf)); 167 return buf; 168 } 169 170 void 171 kread(u_long addr, void *buf, int len) 172 { 173 if (kvm_read(kvmd, addr, buf, len) != len) { 174 perror("kvm_read"); 175 exit(1); 176 } 177 } 178 179 void 180 if6_addrlist(struct ifaddr_container *ifac) 181 { 182 struct ifaddr *ifap; 183 struct ifaddr ifa; 184 struct sockaddr sa; 185 struct in6_ifaddr if6a; 186 struct ifaddr *ifap0; 187 188 if (ifac != NULL) 189 ifap = ifac->ifa; 190 else 191 ifap = NULL; 192 193 ifap0 = ifap; 194 while (ifap) { 195 KREAD(ifap, &ifa, struct ifaddr); 196 if (ifa.ifa_addr == NULL) 197 goto nextifap; 198 KREAD(ifa.ifa_addr, &sa, struct sockaddr); 199 if (sa.sa_family != PF_INET6) 200 goto nextifap; 201 KREAD(ifap, &if6a, struct in6_ifaddr); 202 printf("\tinet6 %s\n", inet6_n2a(&if6a.ia_addr.sin6_addr)); 203 nextifap: 204 if (TAILQ_NEXT(ifac, ifa_link) == NULL) { 205 ifap = NULL; 206 } else { 207 KREAD(TAILQ_NEXT(ifac, ifa_link), ifac, 208 struct ifaddr_container); 209 ifap = ifac->ifa; 210 } 211 } 212 if (ifap0) { 213 struct ifnet ifnet; 214 struct ifmultiaddr ifm, *ifmp = 0; 215 struct sockaddr_dl sdl; 216 217 KREAD(ifap0, &ifa, struct ifaddr); 218 KREAD(ifa.ifa_ifp, &ifnet, struct ifnet); 219 if (TAILQ_FIRST(&ifnet.if_multiaddrs)) 220 ifmp = TAILQ_FIRST(&ifnet.if_multiaddrs); 221 while (ifmp) { 222 KREAD(ifmp, &ifm, struct ifmultiaddr); 223 if (ifm.ifma_addr == NULL) 224 goto nextmulti; 225 KREAD(ifm.ifma_addr, &sa, struct sockaddr); 226 if (sa.sa_family != AF_INET6) 227 goto nextmulti; 228 in6_multientry((struct in6_multi *)ifm.ifma_protospec); 229 if (ifm.ifma_lladdr == 0) 230 goto nextmulti; 231 KREAD(ifm.ifma_lladdr, &sdl, struct sockaddr_dl); 232 printf("\t\t\tmcast-macaddr %s multicnt %d\n", 233 ether_ntoa((struct ether_addr *)LLADDR(&sdl)), 234 ifm.ifma_refcount); 235 nextmulti: 236 ifmp = TAILQ_NEXT(&ifm, ifma_link); 237 } 238 } 239 #ifdef N_IN6_MK 240 if (nl[N_IN6_MK].n_value != 0) { 241 LIST_HEAD(in6_mktype, multi6_kludge) in6_mk; 242 struct multi6_kludge *mkp, mk; 243 char *nam; 244 245 KREAD(nl[N_IN6_MK].n_value, &in6_mk, struct in6_mktype); 246 KREAD(ifap0, &ifa, struct ifaddr); 247 248 nam = strdup(ifname(ifa.ifa_ifp)); 249 if (!nam) { 250 fprintf(stderr, "ifmcstat: not enough core\n"); 251 exit(1); 252 } 253 254 for (mkp = in6_mk.lh_first; mkp; mkp = mk.mk_entry.le_next) { 255 KREAD(mkp, &mk, struct multi6_kludge); 256 if (strcmp(nam, ifname(mk.mk_ifp)) == 0 && 257 mk.mk_head.lh_first) { 258 printf("\t(on kludge entry for %s)\n", nam); 259 in6_multilist(mk.mk_head.lh_first); 260 } 261 } 262 263 free(nam); 264 } 265 #endif 266 } 267 268 struct in6_multi * 269 in6_multientry(struct in6_multi *mc) 270 { 271 struct in6_multi multi; 272 273 KREAD(mc, &multi, struct in6_multi); 274 printf("\t\tgroup %s", inet6_n2a(&multi.in6m_addr)); 275 printf(" refcnt %u\n", multi.in6m_refcount); 276 return(multi.in6m_entry.le_next); 277 } 278 279 void 280 in6_multilist(struct in6_multi *mc) 281 { 282 while (mc) 283 mc = in6_multientry(mc); 284 } 285