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