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