Module Name:    src
Committed By:   joerg
Date:           Tue Jun 10 09:38:30 UTC 2014

Modified Files:
        src/sys/net: if.c if_ether.h if_ethersubr.c
        src/sys/netinet6: mld6.c
        src/usr.sbin/ifmcstat: Makefile ifmcstat.c

Log Message:
Introduce new sysctls for obtaining interface-specific addresses:
- net.sdl for the active link-layer adddress (the MAC)
- net.ether.multicast for the Ethernet multicast addresses
- net.inet6.multicast for the IPv6 multicast groups
- net.inet6.multicast_kludge for temporarily removed multicast groups

Use this sysctls for replacing the kmem grovelling in ifmcstat(8).


To generate a diff of this commit:
cvs rdiff -u -r1.279 -r1.280 src/sys/net/if.c
cvs rdiff -u -r1.62 -r1.63 src/sys/net/if_ether.h
cvs rdiff -u -r1.199 -r1.200 src/sys/net/if_ethersubr.c
cvs rdiff -u -r1.56 -r1.57 src/sys/netinet6/mld6.c
cvs rdiff -u -r1.3 -r1.4 src/usr.sbin/ifmcstat/Makefile
cvs rdiff -u -r1.16 -r1.17 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/sys/net/if.c
diff -u src/sys/net/if.c:1.279 src/sys/net/if.c:1.280
--- src/sys/net/if.c:1.279	Mon Jun  9 12:57:04 2014
+++ src/sys/net/if.c	Tue Jun 10 09:38:30 2014
@@ -1,4 +1,4 @@
-/*	$NetBSD: if.c,v 1.279 2014/06/09 12:57:04 rmind Exp $	*/
+/*	$NetBSD: if.c,v 1.280 2014/06/10 09:38:30 joerg Exp $	*/
 
 /*-
  * Copyright (c) 1999, 2000, 2001, 2008 The NetBSD Foundation, Inc.
@@ -90,7 +90,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if.c,v 1.279 2014/06/09 12:57:04 rmind Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if.c,v 1.280 2014/06/10 09:38:30 joerg Exp $");
 
 #include "opt_inet.h"
 
@@ -2457,3 +2457,44 @@ sysctl_net_pktq_setup(struct sysctllog *
 		       CTL_NET, pf, ipn, qid, IFQCTL_DROPS, CTL_EOL);
 }
 #endif /* INET || INET6 */
+
+static int
+if_sdl_sysctl(SYSCTLFN_ARGS)
+{
+	struct ifnet *ifp;
+	const struct sockaddr_dl *sdl;
+
+	if (namelen != 1)
+		return EINVAL;
+
+	ifp = if_byindex(name[0]);
+	if (ifp == NULL)
+		return ENODEV;
+
+	sdl = ifp->if_sadl;
+	if (sdl == NULL) {
+		*oldlenp = 0;
+		return 0;
+	}
+
+	if (oldp == NULL) {
+		*oldlenp = sdl->sdl_alen;
+		return 0;
+	}
+
+	if (*oldlenp >= sdl->sdl_alen)
+		*oldlenp = sdl->sdl_alen;
+	return sysctl_copyout(l, &sdl->sdl_data[sdl->sdl_nlen], oldp, *oldlenp);
+}
+
+SYSCTL_SETUP(sysctl_net_sdl_setup, "sysctl net.sdl subtree setup")
+{
+	const struct sysctlnode *rnode = NULL;
+
+	sysctl_createv(clog, 0, NULL, &rnode,
+		       CTLFLAG_PERMANENT,
+		       CTLTYPE_NODE, "sdl",
+		       SYSCTL_DESCR("Get active link-layer address"),
+		       if_sdl_sysctl, 0, NULL, 0,
+		       CTL_NET, CTL_CREATE, CTL_EOL);
+}

Index: src/sys/net/if_ether.h
diff -u src/sys/net/if_ether.h:1.62 src/sys/net/if_ether.h:1.63
--- src/sys/net/if_ether.h:1.62	Wed Apr 23 23:17:22 2014
+++ src/sys/net/if_ether.h	Tue Jun 10 09:38:30 2014
@@ -1,4 +1,4 @@
-/*	$NetBSD: if_ether.h,v 1.62 2014/04/23 23:17:22 pooka Exp $	*/
+/*	$NetBSD: if_ether.h,v 1.63 2014/06/10 09:38:30 joerg Exp $	*/
 
 /*
  * Copyright (c) 1982, 1986, 1993
@@ -228,6 +228,12 @@ struct ether_multi {
 	LIST_ENTRY(ether_multi) enm_list;
 };
 
+struct ether_multi_sysctl {
+	u_int   enm_refcount;
+	uint8_t enm_addrlo[ETHER_ADDR_LEN];
+	uint8_t enm_addrhi[ETHER_ADDR_LEN];
+};
+
 /*
  * Structure used by macros below to remember position when stepping through
  * all of the ether_multi records.

Index: src/sys/net/if_ethersubr.c
diff -u src/sys/net/if_ethersubr.c:1.199 src/sys/net/if_ethersubr.c:1.200
--- src/sys/net/if_ethersubr.c:1.199	Thu Jun  5 23:48:16 2014
+++ src/sys/net/if_ethersubr.c	Tue Jun 10 09:38:30 2014
@@ -1,4 +1,4 @@
-/*	$NetBSD: if_ethersubr.c,v 1.199 2014/06/05 23:48:16 rmind Exp $	*/
+/*	$NetBSD: if_ethersubr.c,v 1.200 2014/06/10 09:38:30 joerg Exp $	*/
 
 /*
  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
@@ -61,7 +61,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if_ethersubr.c,v 1.199 2014/06/05 23:48:16 rmind Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_ethersubr.c,v 1.200 2014/06/10 09:38:30 joerg Exp $");
 
 #include "opt_inet.h"
 #include "opt_atalk.h"
@@ -78,6 +78,7 @@ __KERNEL_RCSID(0, "$NetBSD: if_ethersubr
 
 #include <sys/param.h>
 #include <sys/systm.h>
+#include <sys/sysctl.h>
 #include <sys/kernel.h>
 #include <sys/callout.h>
 #include <sys/malloc.h>
@@ -1489,3 +1490,70 @@ ether_ioctl(struct ifnet *ifp, u_long cm
 	}
 	return 0;
 }
+
+static int
+ether_multicast_sysctl(SYSCTLFN_ARGS)
+{
+	struct ether_multi *enm;
+	struct ether_multi_sysctl addr;
+	struct ifnet *ifp;
+	struct ethercom *ec;
+	int error;
+	size_t written;
+
+	if (namelen != 1)
+		return EINVAL;
+
+	ifp = if_byindex(name[0]);
+	if (ifp == NULL)
+		return ENODEV;
+	if (ifp->if_type != IFT_ETHER) {
+		*oldlenp = 0;
+		return 0;
+	}
+	ec = (struct ethercom *)ifp;
+
+	if (oldp == NULL) {
+		*oldlenp = ec->ec_multicnt * sizeof(addr);
+		return 0;
+	}
+
+	memset(&addr, 0, sizeof(addr));
+	error = 0;
+	written = 0;
+
+	LIST_FOREACH(enm, &ec->ec_multiaddrs, enm_list) {
+		if (written + sizeof(addr) > *oldlenp)
+			break;
+		addr.enm_refcount = enm->enm_refcount;
+		memcpy(addr.enm_addrlo, enm->enm_addrlo, ETHER_ADDR_LEN);
+		memcpy(addr.enm_addrhi, enm->enm_addrhi, ETHER_ADDR_LEN);
+		error = sysctl_copyout(l, &addr, oldp, sizeof(addr));
+		if (error)
+			break;
+		written += sizeof(addr);
+		oldp = (char *)oldp + sizeof(addr);
+	}
+
+	*oldlenp = written;
+	return error;
+}
+
+SYSCTL_SETUP(sysctl_net_ether_setup, "sysctl net.ether subtree setup")
+{
+	const struct sysctlnode *rnode = NULL;
+
+	sysctl_createv(clog, 0, NULL, &rnode,
+		       CTLFLAG_PERMANENT,
+		       CTLTYPE_NODE, "ether",
+		       SYSCTL_DESCR("Ethernet-specific information"),
+		       NULL, 0, NULL, 0,
+		       CTL_NET, CTL_CREATE, CTL_EOL);
+
+	sysctl_createv(clog, 0, &rnode, NULL,
+		       CTLFLAG_PERMANENT,
+		       CTLTYPE_NODE, "multicast",
+		       SYSCTL_DESCR("multicast addresses"),
+		       ether_multicast_sysctl, 0, NULL, 0,
+		       CTL_CREATE, CTL_EOL);
+}

Index: src/sys/netinet6/mld6.c
diff -u src/sys/netinet6/mld6.c:1.56 src/sys/netinet6/mld6.c:1.57
--- src/sys/netinet6/mld6.c:1.56	Mon Jun  2 11:02:20 2014
+++ src/sys/netinet6/mld6.c	Tue Jun 10 09:38:30 2014
@@ -1,4 +1,4 @@
-/*	$NetBSD: mld6.c,v 1.56 2014/06/02 11:02:20 joerg Exp $	*/
+/*	$NetBSD: mld6.c,v 1.57 2014/06/10 09:38:30 joerg Exp $	*/
 /*	$KAME: mld6.c,v 1.25 2001/01/16 14:14:18 itojun Exp $	*/
 
 /*
@@ -102,7 +102,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: mld6.c,v 1.56 2014/06/02 11:02:20 joerg Exp $");
+__KERNEL_RCSID(0, "$NetBSD: mld6.c,v 1.57 2014/06/10 09:38:30 joerg Exp $");
 
 #include "opt_inet.h"
 
@@ -897,3 +897,146 @@ in6_purgemkludge(struct ifnet *ifp)
 	LIST_REMOVE(mk, mk_entry);
 	free(mk, M_IPMADDR);
 }
+
+static int
+in6_mkludge_sysctl(SYSCTLFN_ARGS)
+{
+	struct multi6_kludge *mk;
+	struct in6_multi *in6m;
+	int error;
+	uint32_t tmp;
+	size_t written;
+
+	if (namelen != 1)
+		return EINVAL;
+
+	if (oldp == NULL) {
+		*oldlenp = 0;
+		LIST_FOREACH(mk, &in6_mk, mk_entry) {
+			if (mk->mk_ifp->if_index == name[0])
+				continue;
+			LIST_FOREACH(in6m, &mk->mk_head, in6m_entry) {
+				*oldlenp += sizeof(struct in6_addr) +
+				    sizeof(uint32_t);
+			}
+		}
+		return 0;
+	}
+
+	error = 0;
+	written = 0;
+	LIST_FOREACH(mk, &in6_mk, mk_entry) {
+		if (mk->mk_ifp->if_index == name[0])
+			continue;
+		LIST_FOREACH(in6m, &mk->mk_head, in6m_entry) {
+			if (written + sizeof(struct in6_addr) +
+			    sizeof(uint32_t) > *oldlenp)
+				goto done;
+			error = sysctl_copyout(l, &in6m->in6m_addr,
+			    oldp, sizeof(struct in6_addr));
+			if (error)
+				goto done;
+			oldp = (char *)oldp + sizeof(struct in6_addr);
+			written += sizeof(struct in6_addr);
+			tmp = in6m->in6m_refcount;
+			error = sysctl_copyout(l, &tmp, oldp, sizeof(tmp));
+			if (error)
+				goto done;
+			oldp = (char *)oldp + sizeof(tmp);
+			written += sizeof(tmp);
+		}
+	}
+
+done:
+	*oldlenp = written;
+	return error;
+}
+
+static int
+in6_multicast_sysctl(SYSCTLFN_ARGS)
+{
+	struct ifnet *ifp;
+	struct ifaddr *ifa;
+	struct in6_ifaddr *ifa6;
+	struct in6_multi *in6m;
+	uint32_t tmp;
+	int error;
+	size_t written;
+
+	if (namelen != 1)
+		return EINVAL;
+
+	ifp = if_byindex(name[0]);
+	if (ifp == NULL)
+		return ENODEV;
+
+	if (oldp == NULL) {
+		*oldlenp = 0;
+		TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
+			if (ifa->ifa_addr == NULL)
+				continue;
+			if (ifa->ifa_addr->sa_family != AF_INET6)
+				continue;
+			ifa6 = (struct in6_ifaddr *)ifa;
+			LIST_FOREACH(in6m, &ifa6->ia6_multiaddrs, in6m_entry) {
+				*oldlenp += 2 * sizeof(struct in6_addr) +
+				    sizeof(uint32_t);
+			}
+		}
+		return 0;
+	}
+
+	error = 0;
+	written = 0;
+	TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
+		if (ifa->ifa_addr == NULL)
+			continue;
+		if (ifa->ifa_addr->sa_family != AF_INET6)
+			continue;
+		ifa6 = (struct in6_ifaddr *)ifa;
+		LIST_FOREACH(in6m, &ifa6->ia6_multiaddrs, in6m_entry) {
+			if (written + 2 * sizeof(struct in6_addr) +
+			    sizeof(uint32_t) > *oldlenp)
+				goto done;
+			error = sysctl_copyout(l, &ifa6->ia_addr.sin6_addr,
+			    oldp, sizeof(struct in6_addr));
+			if (error)
+				goto done;
+			oldp = (char *)oldp + sizeof(struct in6_addr);
+			written += sizeof(struct in6_addr);
+			error = sysctl_copyout(l, &in6m->in6m_addr,
+			    oldp, sizeof(struct in6_addr));
+			if (error)
+				goto done;
+			oldp = (char *)oldp + sizeof(struct in6_addr);
+			written += sizeof(struct in6_addr);
+			tmp = in6m->in6m_refcount;
+			error = sysctl_copyout(l, &tmp, oldp, sizeof(tmp));
+			if (error)
+				goto done;
+			oldp = (char *)oldp + sizeof(tmp);
+			written += sizeof(tmp);
+		}
+	}
+done:
+	*oldlenp = written;
+	return error;
+}
+
+SYSCTL_SETUP(sysctl_in6_mklude_setup, "sysctl net.inet6.multicast_kludge subtree setup")
+{
+
+	sysctl_createv(clog, 0, NULL, NULL,
+		       CTLFLAG_PERMANENT,
+		       CTLTYPE_NODE, "multicast",
+		       SYSCTL_DESCR("Multicast information"),
+		       in6_multicast_sysctl, 0, NULL, 0,
+		       CTL_NET, PF_INET6, CTL_CREATE, CTL_EOL);
+
+	sysctl_createv(clog, 0, NULL, NULL,
+		       CTLFLAG_PERMANENT,
+		       CTLTYPE_NODE, "multicast_kludge",
+		       SYSCTL_DESCR("multicast kludge information"),
+		       in6_mkludge_sysctl, 0, NULL, 0,
+		       CTL_NET, PF_INET6, CTL_CREATE, CTL_EOL);
+}

Index: src/usr.sbin/ifmcstat/Makefile
diff -u src/usr.sbin/ifmcstat/Makefile:1.3 src/usr.sbin/ifmcstat/Makefile:1.4
--- src/usr.sbin/ifmcstat/Makefile:1.3	Fri Aug  2 03:58:10 2002
+++ src/usr.sbin/ifmcstat/Makefile	Tue Jun 10 09:38:30 2014
@@ -1,11 +1,11 @@
-# $NetBSD: Makefile,v 1.3 2002/08/02 03:58:10 simonb Exp $
+# $NetBSD: Makefile,v 1.4 2014/06/10 09:38:30 joerg 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.16 src/usr.sbin/ifmcstat/ifmcstat.c:1.17
--- src/usr.sbin/ifmcstat/ifmcstat.c:1.16	Fri May 30 22:20:48 2014
+++ src/usr.sbin/ifmcstat/ifmcstat.c	Tue Jun 10 09:38:30 2014
@@ -1,4 +1,4 @@
-/*	$NetBSD: ifmcstat.c,v 1.16 2014/05/30 22:20:48 joerg Exp $	*/
+/*	$NetBSD: ifmcstat.c,v 1.17 2014/06/10 09:38:30 joerg 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.17 2014/06/10 09:38:30 joerg Exp $");
 
+#include <err.h>
+#include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <fcntl.h>
@@ -36,9 +40,11 @@
 #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>
 #include <net/if_types.h>
 #include <net/if_dl.h>
@@ -49,34 +55,12 @@
 
 #include <netdb.h>
 
-kvm_t	*kvmd;
-
-struct	nlist nl[] = {
-#define	N_IFNET_LIST	0
-	{ "_ifnet_list", 0, 0, 0, 0 },
-#define N_IN6_MK 1
-	{ "_in6_mk", 0, 0, 0, 0 },
-	{ "", 0, 0, 0, 0 },
-};
-
-static const char *inet6_n2a(struct in6_addr *);
-static char *ifname(struct ifnet *);
-static void kread(u_long, void *, int);
-static void acmc(struct ether_multi *);
-static void if6_addrlist(struct ifaddr *);
-static void in6_multilist(struct in6_multi *);
-
-#define	KREAD(addr, buf, type) \
-	kread((u_long)addr, (void *)buf, sizeof(type))
-
-struct multi6_kludge {
-	LIST_ENTRY(multi6_kludge) mk_entry;
-	struct ifnet *mk_ifp;
-	struct in6_multihead mk_head;
-};
+static const char *inet6_n2a(void *);
+static void print_ether_mcast(u_short);
+static void print_inet6_mcast(u_short, const char *);
 
 static const char *
-inet6_n2a(struct in6_addr *p)
+inet6_n2a(void *p)
 {
 	static char buf[NI_MAXHOST];
 	struct sockaddr_in6 sin6;
@@ -85,7 +69,7 @@ inet6_n2a(struct in6_addr *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,
@@ -98,149 +82,174 @@ inet6_n2a(struct in6_addr *p)
 int
 main(void)
 {
-	char	buf[_POSIX2_LINE_MAX], ifnam[IFNAMSIZ];
-	struct	ifnet	*ifp, *nifp, ifnet;
-	struct ethercom ec;
-	union {
-		struct sockaddr_storage st;
-		struct sockaddr_dl sdl;
-	} su;
-	struct sockaddr_dl *sdlp;
-	sdlp = &su.sdl;
-
-	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_LIST].n_value == 0) {
-		printf("symbol %s not found\n", nl[N_IFNET_LIST].n_name);
-		exit(1);
-	}
-	KREAD(nl[N_IFNET_LIST].n_value, &ifp, struct ifnet *);
-	while (ifp) {
-		KREAD(ifp, &ifnet, struct ifnet);
-		printf("%s:\n", if_indextoname(ifnet.if_index, ifnam));
-
-		if6_addrlist(ifnet.if_addrlist.tqh_first);
-		nifp = ifnet.if_list.tqe_next;
-
-		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");
-		}
+	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*/
 }
 
-static char *
-ifname(struct ifnet *ifp)
+static void *
+fetch_sysctl(size_t *len, int oids[], size_t oidlen, const char *msg)
 {
-	static char buf[BUFSIZ];
-	struct ifnet ifnet;
+	void *data;
 
-	KREAD(ifp, &ifnet, struct ifnet);
-	strncpy(buf, ifnet.if_xname, BUFSIZ);
-	return buf;
-}
+	*len = 0;
+	data = NULL;
 
-static void
-kread(u_long addr, void *buf, int len)
-{
-	if (kvm_read(kvmd, addr, buf, len) != len) {
-		perror("kvm_read");
-		exit(1);
+	for (;;) {
+		if (sysctl(oids, oidlen, data, len, NULL, 0) == 0) {
+			if (data != NULL || len == 0)
+				return data;
+			errno = ENOMEM;
+		}
+		free(data);
+		if (errno == ENOMEM) {
+			data = emalloc(*len);
+			continue;
+		}
+		if (errno != ENODEV)
+			warn("%s", msg);
+		return NULL;
 	}
 }
 
 static void
-acmc(struct ether_multi *am)
+print_hwaddr(const uint8_t *hwaddr, size_t len)
 {
-	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);
-		am = em.enm_list.le_next;
-	}
+	while (len)
+		printf("%02x%s", *hwaddr++, len-- == 0 ? "" : ":");
 }
 
 static void
-if6_addrlist(struct ifaddr *ifap)
+print_ether_mcast(u_short ifindex)
 {
-	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));
-		mc = mc ? mc : if6a.ia6_multiaddrs.lh_first;
-	nextifap:
-		ifap = ifa.ifa_list.tqe_next;
-	}
-	if (mc)
-		in6_multilist(mc);
-	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(nam);
+	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 = fetch_sysctl(&sdl_len, sdl_oids, 3, "failed to read net.sdl");
+
+	if (sdl_len == 0) {
+		free(hwaddr);
+		return;
+	}
+
+	ems_oids[3] = ifindex;
+	ems = fetch_sysctl(&ems_len, ems_oids, 4,
+	    "failed to read net.ether.multicast");
+	ems_len /= sizeof(*ems);
+
+	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
-in6_multilist(struct in6_multi *mc)
+print_inet6_mcast(u_short ifindex, const char *ifname)
 {
-	struct in6_multi multi;
+	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 = fetch_sysctl(&len, mcast_oids, 4,
+	    "failed to read net.inet6.multicast");
+	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);
+		}
+	}
+	free(mcast_addrs);
 
-	while (mc) {
-		KREAD(mc, &multi, struct in6_multi);
-		printf("\t\tgroup %s", inet6_n2a(&multi.in6m_addr));
-		printf(" refcnt %u\n", multi.in6m_refcount);
-		mc = multi.in6m_entry.le_next;
+	kludge_addrs = fetch_sysctl(&len, kludge_oids, 4,
+	    "failed to read net.inet6.multicast_kludge");
+	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);
+		}
 	}
+	free(kludge_addrs);
 }

Reply via email to