Module Name: src Committed By: tls Date: Sun Aug 10 06:59:31 UTC 2014
Modified Files: src/usr.sbin/ifmcstat [tls-earlyentropy]: Makefile ifmcstat.c Log Message: Rebase. To generate a diff of this commit: cvs rdiff -u -r1.3 -r1.3.72.1 src/usr.sbin/ifmcstat/Makefile cvs rdiff -u -r1.12 -r1.12.2.1 src/usr.sbin/ifmcstat/ifmcstat.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/usr.sbin/ifmcstat/Makefile diff -u src/usr.sbin/ifmcstat/Makefile:1.3 src/usr.sbin/ifmcstat/Makefile:1.3.72.1 --- src/usr.sbin/ifmcstat/Makefile:1.3 Fri Aug 2 03:58:10 2002 +++ src/usr.sbin/ifmcstat/Makefile Sun Aug 10 06:59:31 2014 @@ -1,11 +1,11 @@ -# $NetBSD: Makefile,v 1.3 2002/08/02 03:58:10 simonb Exp $ +# $NetBSD: Makefile,v 1.3.72.1 2014/08/10 06:59:31 tls Exp $ # @(#)Makefile 8.1 (Berkeley) 6/5/93 PROG= ifmcstat MAN= ifmcstat.8 -LDADD= -lkvm -DPADD= ${LIBKVM} +LDADD+= -lutil +DPADD+= ${LIBUTIL} CPPFLAGS+= -DINET6 Index: src/usr.sbin/ifmcstat/ifmcstat.c diff -u src/usr.sbin/ifmcstat/ifmcstat.c:1.12 src/usr.sbin/ifmcstat/ifmcstat.c:1.12.2.1 --- src/usr.sbin/ifmcstat/ifmcstat.c:1.12 Sat Oct 19 17:16:25 2013 +++ src/usr.sbin/ifmcstat/ifmcstat.c Sun Aug 10 06:59:31 2014 @@ -1,4 +1,4 @@ -/* $NetBSD: ifmcstat.c,v 1.12 2013/10/19 17:16:25 christos Exp $ */ +/* $NetBSD: ifmcstat.c,v 1.12.2.1 2014/08/10 06:59:31 tls Exp $ */ /* * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. @@ -28,7 +28,11 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ +#include <sys/cdefs.h> +__RCSID("$NetBSD: ifmcstat.c,v 1.12.2.1 2014/08/10 06:59:31 tls Exp $"); +#include <err.h> +#include <errno.h> #include <stdio.h> #include <stdlib.h> #include <fcntl.h> @@ -36,77 +40,27 @@ #include <nlist.h> #include <string.h> #include <limits.h> +#include <util.h> #include <sys/types.h> #include <sys/socket.h> +#include <sys/sysctl.h> #include <net/if.h> -#if defined(__FreeBSD__) && __FreeBSD__ >= 3 -# include <net/if_var.h> -#endif #include <net/if_types.h> #include <net/if_dl.h> #include <netinet/in.h> -#ifndef __NetBSD__ -# ifdef __FreeBSD__ -# define KERNEL -# endif -# include <netinet/if_ether.h> -# ifdef __FreeBSD__ -# undef KERNEL -# endif -#else -# include <net/if_ether.h> -#endif +#include <net/if_ether.h> #include <netinet/in_var.h> #include <arpa/inet.h> #include <netdb.h> -kvm_t *kvmd; +static const char *inet6_n2a(void *); +static void print_ether_mcast(u_short); +static void print_inet6_mcast(u_short, const char *); -struct nlist nl[] = { -#define N_IFNET 0 - { "_ifnet", 0, 0, 0, 0 }, -#if !(defined(__FreeBSD__) && __FreeBSD__ >= 3) -#define N_IN6_MK 1 - { "_in6_mk", 0, 0, 0, 0 }, -#endif - { "", 0, 0, 0, 0 }, -}; - -const char *inet6_n2a __P((struct in6_addr *)); -int main __P((void)); -char *ifname __P((struct ifnet *)); -void kread __P((u_long, void *, int)); -#if !(defined(__FreeBSD__) && __FreeBSD__ >= 3) -void acmc __P((struct ether_multi *)); -#endif -void if6_addrlist __P((struct ifaddr *)); -void in6_multilist __P((struct in6_multi *)); -struct in6_multi * in6_multientry __P((struct in6_multi *)); - -#if !defined(__NetBSD__) && !(defined(__FreeBSD__) && __FreeBSD__ >= 3) && !defined(__OpenBSD__) -#ifdef __bsdi__ -struct ether_addr { - u_int8_t ether_addr_octet[6]; -}; -#endif -static char *ether_ntoa __P((struct ether_addr *)); -#endif - -#define KREAD(addr, buf, type) \ - kread((u_long)addr, (void *)buf, sizeof(type)) - -#ifdef N_IN6_MK -struct multi6_kludge { - LIST_ENTRY(multi6_kludge) mk_entry; - struct ifnet *mk_ifp; - struct in6_multihead mk_head; -}; -#endif - -const char *inet6_n2a(p) - struct in6_addr *p; +static const char * +inet6_n2a(void *p) { static char buf[NI_MAXHOST]; struct sockaddr_in6 sin6; @@ -115,7 +69,7 @@ const char *inet6_n2a(p) memset(&sin6, 0, sizeof(sin6)); sin6.sin6_family = AF_INET6; sin6.sin6_len = sizeof(struct sockaddr_in6); - sin6.sin6_addr = *p; + memcpy(&sin6.sin6_addr, p, sizeof(sin6.sin6_addr)); inet6_getscopeid(&sin6, INET6_IS_ADDR_LINKLOCAL| INET6_IS_ADDR_MC_LINKLOCAL); if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len, @@ -125,263 +79,165 @@ const char *inet6_n2a(p) return "(invalid)"; } -int main() +int +main(void) { - char buf[_POSIX2_LINE_MAX], ifnam[IFNAMSIZ]; - struct ifnet *ifp, *nifp, ifnet; -#ifndef __NetBSD__ - struct arpcom arpcom; -#else - struct ethercom ec; - union { - struct sockaddr_storage st; - struct sockaddr_dl sdl; - } su; - struct sockaddr_dl *sdlp; - sdlp = &su.sdl; -#endif - - if ((kvmd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, buf)) == NULL) { - perror("kvm_openfiles"); - exit(1); - } - if (kvm_nlist(kvmd, nl) < 0) { - perror("kvm_nlist"); - exit(1); - } - if (nl[N_IFNET].n_value == 0) { - printf("symbol %s not found\n", nl[N_IFNET].n_name); - exit(1); - } - KREAD(nl[N_IFNET].n_value, &ifp, struct ifnet *); - while (ifp) { - KREAD(ifp, &ifnet, struct ifnet); - printf("%s:\n", if_indextoname(ifnet.if_index, ifnam)); - -#if defined(__NetBSD__) || defined(__OpenBSD__) - if6_addrlist(ifnet.if_addrlist.tqh_first); - nifp = ifnet.if_list.tqe_next; -#elif defined(__FreeBSD__) && __FreeBSD__ >= 3 - if6_addrlist(TAILQ_FIRST(&ifnet.if_addrhead)); - nifp = ifnet.if_link.tqe_next; -#else - if6_addrlist(ifnet.if_addrlist); - nifp = ifnet.if_next; -#endif - -#ifdef __NetBSD__ - KREAD(ifnet.if_sadl, sdlp, struct sockaddr_dl); - if (sdlp->sdl_type == IFT_ETHER) { - /* If we didn't get all of it, try again */ - if (sdlp->sdl_len > sizeof(struct sockaddr_dl)) - kread((u_long)ifnet.if_sadl, (void *)sdlp, sdlp->sdl_len); - printf("\tenaddr %s", - ether_ntoa((struct ether_addr *)LLADDR(sdlp))); - KREAD(ifp, &ec, struct ethercom); - printf(" multicnt %d", ec.ec_multicnt); - acmc(ec.ec_multiaddrs.lh_first); - printf("\n"); - } -#elif defined(__FreeBSD__) && __FreeBSD__ >= 3 - /* not supported */ -#else - if (ifnet.if_type == IFT_ETHER) { - KREAD(ifp, &arpcom, struct arpcom); - printf("\tenaddr %s", - ether_ntoa((struct ether_addr *)arpcom.ac_enaddr)); - KREAD(ifp, &arpcom, struct arpcom); - printf(" multicnt %d", arpcom.ac_multicnt); -#ifdef __OpenBSD__ - acmc(arpcom.ac_multiaddrs.lh_first); -#else - acmc(arpcom.ac_multiaddrs); -#endif - printf("\n"); - } -#endif + struct if_nameindex *ifps; + size_t i; + - ifp = nifp; + ifps = if_nameindex(); + if (ifps == NULL) + errx(1, "failed to obtain list of interfaces"); + for (i = 0; ifps[i].if_name != NULL; ++i) { + printf("%s:\n", ifps[i].if_name); + print_inet6_mcast(ifps[i].if_index, ifps[i].if_name); + print_ether_mcast(ifps[i].if_index); } + if_freenameindex(ifps); exit(0); /*NOTREACHED*/ } -char *ifname(ifp) - struct ifnet *ifp; -{ - static char buf[BUFSIZ]; -#if defined(__NetBSD__) || defined(__OpenBSD__) - struct ifnet ifnet; -#endif - -#if defined(__NetBSD__) || defined(__OpenBSD__) - KREAD(ifp, &ifnet, struct ifnet); - strncpy(buf, ifnet.if_xname, BUFSIZ); -#else - KREAD(ifp->if_name, buf, IFNAMSIZ); -#endif - return buf; -} - -void kread(addr, buf, len) - u_long addr; - void *buf; - int len; +static void +print_hwaddr(const uint8_t *hwaddr, size_t len) { - if (kvm_read(kvmd, addr, buf, len) != len) { - perror("kvm_read"); - exit(1); - } + while (len) + printf("%02x%s", *hwaddr++, len-- == 0 ? "" : ":"); } -#if !(defined(__FreeBSD__) && __FreeBSD__ >= 3) -void acmc(am) - struct ether_multi *am; -{ - struct ether_multi em; - - while (am) { - KREAD(am, &em, struct ether_multi); - - printf("\n\t\t"); - printf("%s -- ", ether_ntoa((struct ether_addr *)em.enm_addrlo)); - printf("%s ", ether_ntoa((struct ether_addr *)&em.enm_addrhi)); - printf("%d", em.enm_refcount); -#if !defined(__NetBSD__) && !defined(__OpenBSD__) - am = em.enm_next; -#else - am = em.enm_list.le_next; -#endif - } -} -#endif - -void -if6_addrlist(ifap) - struct ifaddr *ifap; -{ - struct ifaddr ifa; - struct sockaddr sa; - struct in6_ifaddr if6a; - struct in6_multi *mc = 0; - struct ifaddr *ifap0; - - ifap0 = ifap; - while (ifap) { - KREAD(ifap, &ifa, struct ifaddr); - if (ifa.ifa_addr == NULL) - goto nextifap; - KREAD(ifa.ifa_addr, &sa, struct sockaddr); - if (sa.sa_family != PF_INET6) - goto nextifap; - KREAD(ifap, &if6a, struct in6_ifaddr); - printf("\tinet6 %s\n", inet6_n2a(&if6a.ia_addr.sin6_addr)); -#if !(defined(__FreeBSD__) && __FreeBSD__ >= 3) - mc = mc ? mc : if6a.ia6_multiaddrs.lh_first; -#endif - nextifap: -#if defined(__NetBSD__) || defined(__OpenBSD__) - ifap = ifa.ifa_list.tqe_next; -#elif defined(__FreeBSD__) && __FreeBSD__ >= 3 - ifap = ifa.ifa_link.tqe_next; -#else - ifap = ifa.ifa_next; -#endif /* __FreeBSD__ >= 3 */ - } -#if defined(__FreeBSD__) && __FreeBSD__ >= 3 - if (ifap0) { - struct ifnet ifnet; - struct ifmultiaddr ifm, *ifmp = 0; - struct sockaddr_in6 sin6; - struct in6_multi in6m; - struct sockaddr_dl sdl; - int in6_multilist_done = 0; - - KREAD(ifap0, &ifa, struct ifaddr); - KREAD(ifa.ifa_ifp, &ifnet, struct ifnet); - if (ifnet.if_multiaddrs.lh_first) - ifmp = ifnet.if_multiaddrs.lh_first; - while (ifmp) { - KREAD(ifmp, &ifm, struct ifmultiaddr); - if (ifm.ifma_addr == NULL) - goto nextmulti; - KREAD(ifm.ifma_addr, &sa, struct sockaddr); - if (sa.sa_family != AF_INET6) - goto nextmulti; - (void)in6_multientry((struct in6_multi *) - ifm.ifma_protospec); - if (ifm.ifma_lladdr == 0) - goto nextmulti; - KREAD(ifm.ifma_lladdr, &sdl, struct sockaddr_dl); - printf("\t\t\tmcast-macaddr %s multicnt %d\n", - ether_ntoa((struct ether_addr *)LLADDR(&sdl)), - ifm.ifma_refcount); - nextmulti: - ifmp = ifm.ifma_link.le_next; +static void +print_ether_mcast(u_short ifindex) +{ + static int ems_oids[4], sdl_oids[3]; + size_t i, ems_len, sdl_len; + void *hwaddr; + struct ether_multi_sysctl *ems; + + if (ems_oids[0] == 0) { + size_t oidlen = __arraycount(ems_oids); + if (sysctlnametomib("net.ether.multicast", ems_oids, &oidlen) == -1) + errx(1, "net.ether.multicast not found"); + if (oidlen != 3) + errx(1, "Wrong OID path for net.ether.multicast"); + } + + if (sdl_oids[0] == 0) { + size_t oidlen = __arraycount(sdl_oids); + if (sysctlnametomib("net.sdl", sdl_oids, &oidlen) == -1) + errx(1, "net.sdl not found"); + if (oidlen != 2) + errx(1, "Wrong OID path for net.sdl"); + } + + sdl_oids[2] = ifindex; + hwaddr = asysctl(sdl_oids, 3, &sdl_len); + + if (sdl_len == 0) { + free(hwaddr); + return; + } + if (hwaddr == NULL) { + warn("failed to read net.sdl"); + } + + ems_oids[3] = ifindex; + ems = asysctl(ems_oids, 4, &ems_len); + ems_len /= sizeof(*ems); + + if (ems == NULL && ems_len != 0) { + warn("failed to read net.ether.multicast"); + return; + } + + printf("\tenaddr "); + print_hwaddr(hwaddr, sdl_len); + printf(" multicnt %zu\n", ems_len); + + for (i = 0; i < ems_len; ++i) { + printf("\t\t"); + print_hwaddr(ems[i].enm_addrlo, sizeof(ems[i].enm_addrlo)); + printf(" -- "); + print_hwaddr(ems[i].enm_addrhi, sizeof(ems[i].enm_addrhi)); + printf(" %d\n", ems[i].enm_refcount); + } + free(ems); + free(hwaddr); +} + +static void +print_inet6_mcast(u_short ifindex, const char *ifname) +{ + static int mcast_oids[4], kludge_oids[4]; + const char *addr; + uint8_t *mcast_addrs, *kludge_addrs, *p, *last_p; + uint32_t refcnt; + size_t len; + + if (mcast_oids[0] == 0) { + size_t oidlen = __arraycount(mcast_oids); + if (sysctlnametomib("net.inet6.multicast", mcast_oids, + &oidlen) == -1) + errx(1, "net.inet6.multicast not found"); + if (oidlen != 3) + errx(1, "Wrong OID path for net.inet6.multicast"); + } + + if (kludge_oids[0] == 0) { + size_t oidlen = __arraycount(kludge_oids); + if (sysctlnametomib("net.inet6.multicast_kludge", kludge_oids, + &oidlen) == -1) + errx(1, "net.inet6.multicast_kludge not found"); + if (oidlen != 3) + errx(1, "Wrong OID path for net.inet6.multicast_kludge"); + } + + mcast_oids[3] = ifindex; + kludge_oids[3] = ifindex; + + mcast_addrs = asysctl(mcast_oids, 4, &len); + if (mcast_addrs == NULL && len != 0) { + warn("failed to read net.inet6.multicast"); + return; + } + if (len) { + p = mcast_addrs; + last_p = NULL; + while (len >= 2 * sizeof(struct in6_addr) + sizeof(uint32_t)) { + if (last_p == NULL || + memcmp(p, last_p, sizeof(struct in6_addr))) + printf("\tinet6 %s\n", inet6_n2a(p)); + last_p = p; + p += sizeof(struct in6_addr); + addr = inet6_n2a(p); + p += sizeof(struct in6_addr); + memcpy(&refcnt, p, sizeof(refcnt)); + p += sizeof(refcnt); + printf("\t\tgroup %s refcount %" PRIu32 "\n", addr, + refcnt); + len -= 2 * sizeof(struct in6_addr) + sizeof(uint32_t); } } -#else - if (mc) - in6_multilist(mc); -#endif -#ifdef N_IN6_MK - if (nl[N_IN6_MK].n_value != 0) { - LIST_HEAD(in6_mktype, multi6_kludge) in6_mk; - struct multi6_kludge *mkp, mk; - char *nam; - - KREAD(nl[N_IN6_MK].n_value, &in6_mk, struct in6_mktype); - KREAD(ifap0, &ifa, struct ifaddr); - - nam = strdup(ifname(ifa.ifa_ifp)); - - for (mkp = in6_mk.lh_first; mkp; mkp = mk.mk_entry.le_next) { - KREAD(mkp, &mk, struct multi6_kludge); - if (strcmp(nam, ifname(mk.mk_ifp)) == 0 && - mk.mk_head.lh_first) { - printf("\t(on kludge entry for %s)\n", nam); - in6_multilist(mk.mk_head.lh_first); - } - } + free(mcast_addrs); - free(nam); + kludge_addrs = asysctl(kludge_oids, 4, &len); + if (kludge_addrs == NULL && len != 0) { + warn("failed to read net.inet6.multicast_kludge"); + return; + } + if (len) { + printf("\t(on kludge entry for %s)\n", ifname); + p = kludge_addrs; + while (len >= sizeof(struct in6_addr) + sizeof(uint32_t)) { + addr = inet6_n2a(p); + p += sizeof(struct in6_addr); + memcpy(&refcnt, p, sizeof(refcnt)); + p += sizeof(refcnt); + printf("\t\tgroup %s refcount %" PRIu32 "\n", addr, + refcnt); + len -= sizeof(struct in6_addr) + sizeof(uint32_t); + } } -#endif -} - -struct in6_multi * -in6_multientry(mc) - struct in6_multi *mc; -{ - struct in6_multi multi; - - KREAD(mc, &multi, struct in6_multi); - printf("\t\tgroup %s", inet6_n2a(&multi.in6m_addr)); - printf(" refcnt %u\n", multi.in6m_refcount); - return(multi.in6m_entry.le_next); -} - -void -in6_multilist(mc) - struct in6_multi *mc; -{ - while (mc) - mc = in6_multientry(mc); -} - -#if !defined(__NetBSD__) && !(defined(__FreeBSD__) && __FreeBSD__ >= 3) && !defined(__OpenBSD__) -static char * -ether_ntoa(e) - struct ether_addr *e; -{ - static char buf[20]; - u_char *p; - - p = (u_char *)e; - - snprintf(buf, sizeof(buf), "%02x:%02x:%02x:%02x:%02x:%02x", - p[0], p[1], p[2], p[3], p[4], p[5]); - return buf; + free(kludge_addrs); } -#endif