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