Home | History | Annotate | Line # | Download | only in ifmcstat
ifmcstat.c revision 1.14
      1 /*	$NetBSD: ifmcstat.c,v 1.14 2014/05/30 02:28:07 joerg Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the project nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     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_types.h>
     44 #include <net/if_dl.h>
     45 #include <netinet/in.h>
     46 #include <net/if_ether.h>
     47 #include <netinet/in_var.h>
     48 #include <arpa/inet.h>
     49 
     50 #include <netdb.h>
     51 
     52 kvm_t	*kvmd;
     53 
     54 struct	nlist nl[] = {
     55 #define	N_IFNET_LIST	0
     56 	{ "_ifnet_list", 0, 0, 0, 0 },
     57 #define N_IN6_MK 1
     58 	{ "_in6_mk", 0, 0, 0, 0 },
     59 	{ "", 0, 0, 0, 0 },
     60 };
     61 
     62 const char *inet6_n2a __P((struct in6_addr *));
     63 int main __P((void));
     64 char *ifname __P((struct ifnet *));
     65 void kread __P((u_long, void *, int));
     66 void acmc __P((struct ether_multi *));
     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 struct multi6_kludge {
     75 	LIST_ENTRY(multi6_kludge) mk_entry;
     76 	struct ifnet *mk_ifp;
     77 	struct in6_multihead mk_head;
     78 };
     79 
     80 const char *inet6_n2a(p)
     81 	struct in6_addr *p;
     82 {
     83 	static char buf[NI_MAXHOST];
     84 	struct sockaddr_in6 sin6;
     85 	const int niflags = NI_NUMERICHOST;
     86 
     87 	memset(&sin6, 0, sizeof(sin6));
     88 	sin6.sin6_family = AF_INET6;
     89 	sin6.sin6_len = sizeof(struct sockaddr_in6);
     90 	sin6.sin6_addr = *p;
     91 	inet6_getscopeid(&sin6, INET6_IS_ADDR_LINKLOCAL|
     92 	    INET6_IS_ADDR_MC_LINKLOCAL);
     93 	if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
     94 			buf, sizeof(buf), NULL, 0, niflags) == 0)
     95 		return buf;
     96 	else
     97 		return "(invalid)";
     98 }
     99 
    100 int main()
    101 {
    102 	char	buf[_POSIX2_LINE_MAX], ifnam[IFNAMSIZ];
    103 	struct	ifnet	*ifp, *nifp, ifnet;
    104 	struct ethercom ec;
    105 	union {
    106 		struct sockaddr_storage st;
    107 		struct sockaddr_dl sdl;
    108 	} su;
    109 	struct sockaddr_dl *sdlp;
    110 	sdlp = &su.sdl;
    111 
    112 	if ((kvmd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, buf)) == NULL) {
    113 		perror("kvm_openfiles");
    114 		exit(1);
    115 	}
    116 	if (kvm_nlist(kvmd, nl) < 0) {
    117 		perror("kvm_nlist");
    118 		exit(1);
    119 	}
    120 	if (nl[N_IFNET_LIST].n_value == 0) {
    121 		printf("symbol %s not found\n", nl[N_IFNET_LIST].n_name);
    122 		exit(1);
    123 	}
    124 	KREAD(nl[N_IFNET_LIST].n_value, &ifp, struct ifnet *);
    125 	while (ifp) {
    126 		KREAD(ifp, &ifnet, struct ifnet);
    127 		printf("%s:\n", if_indextoname(ifnet.if_index, ifnam));
    128 
    129 		if6_addrlist(ifnet.if_addrlist.tqh_first);
    130 		nifp = ifnet.if_list.tqe_next;
    131 
    132 		KREAD(ifnet.if_sadl, sdlp, struct sockaddr_dl);
    133 		if (sdlp->sdl_type == IFT_ETHER) {
    134 			/* If we didn't get all of it, try again */
    135 			if (sdlp->sdl_len > sizeof(struct sockaddr_dl))
    136 				kread((u_long)ifnet.if_sadl, (void *)sdlp, sdlp->sdl_len);
    137 			printf("\tenaddr %s",
    138 			       ether_ntoa((struct ether_addr *)LLADDR(sdlp)));
    139 			KREAD(ifp, &ec, struct ethercom);
    140 			printf(" multicnt %d", ec.ec_multicnt);
    141 			acmc(ec.ec_multiaddrs.lh_first);
    142 			printf("\n");
    143 		}
    144 
    145 		ifp = nifp;
    146 	}
    147 
    148 	exit(0);
    149 	/*NOTREACHED*/
    150 }
    151 
    152 char *ifname(ifp)
    153 	struct ifnet *ifp;
    154 {
    155 	static char buf[BUFSIZ];
    156 	struct ifnet ifnet;
    157 
    158 	KREAD(ifp, &ifnet, struct ifnet);
    159 	strncpy(buf, ifnet.if_xname, BUFSIZ);
    160 	return buf;
    161 }
    162 
    163 void kread(addr, buf, len)
    164 	u_long addr;
    165 	void *buf;
    166 	int len;
    167 {
    168 	if (kvm_read(kvmd, addr, buf, len) != len) {
    169 		perror("kvm_read");
    170 		exit(1);
    171 	}
    172 }
    173 
    174 void acmc(am)
    175 	struct ether_multi *am;
    176 {
    177 	struct ether_multi em;
    178 
    179 	while (am) {
    180 		KREAD(am, &em, struct ether_multi);
    181 
    182 		printf("\n\t\t");
    183 		printf("%s -- ", ether_ntoa((struct ether_addr *)em.enm_addrlo));
    184 		printf("%s ", ether_ntoa((struct ether_addr *)&em.enm_addrhi));
    185 		printf("%d", em.enm_refcount);
    186 		am = em.enm_list.le_next;
    187 	}
    188 }
    189 
    190 void
    191 if6_addrlist(ifap)
    192 	struct ifaddr *ifap;
    193 {
    194 	struct ifaddr ifa;
    195 	struct sockaddr sa;
    196 	struct in6_ifaddr if6a;
    197 	struct in6_multi *mc = 0;
    198 	struct ifaddr *ifap0;
    199 
    200 	ifap0 = ifap;
    201 	while (ifap) {
    202 		KREAD(ifap, &ifa, struct ifaddr);
    203 		if (ifa.ifa_addr == NULL)
    204 			goto nextifap;
    205 		KREAD(ifa.ifa_addr, &sa, struct sockaddr);
    206 		if (sa.sa_family != PF_INET6)
    207 			goto nextifap;
    208 		KREAD(ifap, &if6a, struct in6_ifaddr);
    209 		printf("\tinet6 %s\n", inet6_n2a(&if6a.ia_addr.sin6_addr));
    210 		mc = mc ? mc : if6a.ia6_multiaddrs.lh_first;
    211 	nextifap:
    212 		ifap = ifa.ifa_list.tqe_next;
    213 	}
    214 	if (mc)
    215 		in6_multilist(mc);
    216 	if (nl[N_IN6_MK].n_value != 0) {
    217 		LIST_HEAD(in6_mktype, multi6_kludge) in6_mk;
    218 		struct multi6_kludge *mkp, mk;
    219 		char *nam;
    220 
    221 		KREAD(nl[N_IN6_MK].n_value, &in6_mk, struct in6_mktype);
    222 		KREAD(ifap0, &ifa, struct ifaddr);
    223 
    224 		nam = strdup(ifname(ifa.ifa_ifp));
    225 
    226 		for (mkp = in6_mk.lh_first; mkp; mkp = mk.mk_entry.le_next) {
    227 			KREAD(mkp, &mk, struct multi6_kludge);
    228 			if (strcmp(nam, ifname(mk.mk_ifp)) == 0 &&
    229 			    mk.mk_head.lh_first) {
    230 				printf("\t(on kludge entry for %s)\n", nam);
    231 				in6_multilist(mk.mk_head.lh_first);
    232 			}
    233 		}
    234 
    235 		free(nam);
    236 	}
    237 }
    238 
    239 struct in6_multi *
    240 in6_multientry(mc)
    241 	struct in6_multi *mc;
    242 {
    243 	struct in6_multi multi;
    244 
    245 	KREAD(mc, &multi, struct in6_multi);
    246 	printf("\t\tgroup %s", inet6_n2a(&multi.in6m_addr));
    247 	printf(" refcnt %u\n", multi.in6m_refcount);
    248 	return(multi.in6m_entry.le_next);
    249 }
    250 
    251 void
    252 in6_multilist(mc)
    253 	struct in6_multi *mc;
    254 {
    255 	while (mc)
    256 		mc = in6_multientry(mc);
    257 }
    258