1999-12-28 02:37:14 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
|
|
|
|
* All rights reserved.
|
2000-07-07 19:19:25 +00:00
|
|
|
*
|
1999-12-28 02:37:14 +00:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the project nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
2000-07-07 19:19:25 +00:00
|
|
|
*
|
1999-12-28 02:37:14 +00:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* $FreeBSD$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <kvm.h>
|
|
|
|
#include <nlist.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <limits.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
2001-06-11 12:39:29 +00:00
|
|
|
#include <sys/queue.h>
|
|
|
|
|
1999-12-28 02:37:14 +00:00
|
|
|
#include <net/if.h>
|
2000-01-07 10:02:43 +00:00
|
|
|
#include <net/if_var.h>
|
1999-12-28 02:37:14 +00:00
|
|
|
#include <net/if_types.h>
|
|
|
|
#include <net/if_dl.h>
|
|
|
|
#include <netinet/in.h>
|
2000-07-07 19:19:25 +00:00
|
|
|
#define _KERNEL
|
2000-01-07 10:02:43 +00:00
|
|
|
#include <netinet/if_ether.h>
|
2000-07-07 19:19:25 +00:00
|
|
|
#undef _KERNEL
|
1999-12-28 02:37:14 +00:00
|
|
|
#include <netinet/in_var.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
2000-07-07 19:19:25 +00:00
|
|
|
#include <netdb.h>
|
|
|
|
|
1999-12-28 02:37:14 +00:00
|
|
|
kvm_t *kvmd;
|
|
|
|
|
|
|
|
struct nlist nl[] = {
|
|
|
|
#define N_IFNET 0
|
|
|
|
{ "_ifnet" },
|
|
|
|
{ "" },
|
|
|
|
};
|
|
|
|
|
|
|
|
const char *inet6_n2a __P((struct in6_addr *));
|
|
|
|
int main __P((void));
|
|
|
|
char *ifname __P((struct ifnet *));
|
|
|
|
void kread __P((u_long, void *, int));
|
|
|
|
void if6_addrlist __P((struct ifaddr *));
|
|
|
|
void in6_multilist __P((struct in6_multi *));
|
2000-07-07 19:19:25 +00:00
|
|
|
struct in6_multi * in6_multientry __P((struct in6_multi *));
|
1999-12-28 02:37:14 +00:00
|
|
|
|
|
|
|
#define KREAD(addr, buf, type) \
|
|
|
|
kread((u_long)addr, (void *)buf, sizeof(type))
|
|
|
|
|
2000-07-07 19:19:25 +00:00
|
|
|
#ifdef N_IN6_MK
|
|
|
|
struct multi6_kludge {
|
|
|
|
LIST_ENTRY(multi6_kludge) mk_entry;
|
|
|
|
struct ifnet *mk_ifp;
|
|
|
|
struct in6_multihead mk_head;
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
1999-12-28 02:37:14 +00:00
|
|
|
const char *inet6_n2a(p)
|
|
|
|
struct in6_addr *p;
|
|
|
|
{
|
2000-07-07 19:19:25 +00:00
|
|
|
static char buf[NI_MAXHOST];
|
|
|
|
struct sockaddr_in6 sin6;
|
|
|
|
u_int32_t scopeid;
|
|
|
|
const int niflags = NI_NUMERICHOST;
|
1999-12-28 02:37:14 +00:00
|
|
|
|
2000-07-07 19:19:25 +00:00
|
|
|
memset(&sin6, 0, sizeof(sin6));
|
|
|
|
sin6.sin6_family = AF_INET6;
|
|
|
|
sin6.sin6_len = sizeof(struct sockaddr_in6);
|
|
|
|
sin6.sin6_addr = *p;
|
|
|
|
if (IN6_IS_ADDR_LINKLOCAL(p) || IN6_IS_ADDR_MC_LINKLOCAL(p)) {
|
|
|
|
scopeid = ntohs(*(u_int16_t *)&sin6.sin6_addr.s6_addr[2]);
|
|
|
|
if (scopeid) {
|
|
|
|
sin6.sin6_scope_id = scopeid;
|
|
|
|
sin6.sin6_addr.s6_addr[2] = 0;
|
|
|
|
sin6.sin6_addr.s6_addr[3] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
|
|
|
|
buf, sizeof(buf), NULL, 0, niflags) == 0)
|
|
|
|
return buf;
|
|
|
|
else
|
|
|
|
return "(invalid)";
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
char buf[_POSIX2_LINE_MAX], ifname[IFNAMSIZ];
|
|
|
|
struct ifnet *ifp, *nifp, ifnet;
|
|
|
|
|
|
|
|
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, ifname));
|
2000-07-07 19:19:25 +00:00
|
|
|
|
1999-12-28 02:37:14 +00:00
|
|
|
if6_addrlist(TAILQ_FIRST(&ifnet.if_addrhead));
|
2000-12-30 20:56:04 +00:00
|
|
|
nifp = TAILQ_NEXT(&ifnet, if_link);
|
2000-07-07 19:19:25 +00:00
|
|
|
|
|
|
|
/* not supported */
|
|
|
|
|
|
|
|
ifp = nifp;
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
exit(0);
|
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
|
|
|
|
char *ifname(ifp)
|
|
|
|
struct ifnet *ifp;
|
|
|
|
{
|
|
|
|
static char buf[BUFSIZ];
|
2000-07-07 19:19:25 +00:00
|
|
|
struct ifnet ifnet;
|
|
|
|
char ifnamebuf[IFNAMSIZ];
|
1999-12-28 02:37:14 +00:00
|
|
|
|
2000-07-07 19:19:25 +00:00
|
|
|
KREAD(ifp, &ifnet, struct ifnet);
|
2003-10-31 18:32:15 +00:00
|
|
|
strlcpy(buf, ifnet.if_xname, sizeof(buf));
|
1999-12-28 02:37:14 +00:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
void kread(addr, buf, len)
|
|
|
|
u_long addr;
|
|
|
|
void *buf;
|
|
|
|
int len;
|
|
|
|
{
|
|
|
|
if (kvm_read(kvmd, addr, buf, len) != len) {
|
|
|
|
perror("kvm_read");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
if6_addrlist(ifap)
|
|
|
|
struct ifaddr *ifap;
|
|
|
|
{
|
|
|
|
struct ifaddr ifa;
|
2000-01-07 10:02:43 +00:00
|
|
|
struct sockaddr sa;
|
2000-07-07 19:19:25 +00:00
|
|
|
struct in6_ifaddr if6a;
|
|
|
|
struct ifaddr *ifap0;
|
1999-12-28 02:37:14 +00:00
|
|
|
|
|
|
|
ifap0 = ifap;
|
2000-07-07 19:19:25 +00:00
|
|
|
while (ifap) {
|
1999-12-28 02:37:14 +00:00
|
|
|
KREAD(ifap, &ifa, struct ifaddr);
|
|
|
|
if (ifa.ifa_addr == NULL)
|
2000-07-07 19:19:25 +00:00
|
|
|
goto nextifap;
|
1999-12-28 02:37:14 +00:00
|
|
|
KREAD(ifa.ifa_addr, &sa, struct sockaddr);
|
|
|
|
if (sa.sa_family != PF_INET6)
|
2000-07-07 19:19:25 +00:00
|
|
|
goto nextifap;
|
1999-12-28 02:37:14 +00:00
|
|
|
KREAD(ifap, &if6a, struct in6_ifaddr);
|
2000-07-07 19:19:25 +00:00
|
|
|
printf("\tinet6 %s\n", inet6_n2a(&if6a.ia_addr.sin6_addr));
|
|
|
|
nextifap:
|
2000-12-30 20:56:04 +00:00
|
|
|
ifap = TAILQ_NEXT(&ifa, ifa_link);
|
2000-07-07 19:19:25 +00:00
|
|
|
}
|
|
|
|
if (ifap0) {
|
|
|
|
struct ifnet ifnet;
|
|
|
|
struct ifmultiaddr ifm, *ifmp = 0;
|
|
|
|
struct sockaddr_dl sdl;
|
|
|
|
|
|
|
|
KREAD(ifap0, &ifa, struct ifaddr);
|
|
|
|
KREAD(ifa.ifa_ifp, &ifnet, struct ifnet);
|
2001-02-06 10:12:15 +00:00
|
|
|
if (TAILQ_FIRST(&ifnet.if_multiaddrs))
|
|
|
|
ifmp = TAILQ_FIRST(&ifnet.if_multiaddrs);
|
2000-07-07 19:19:25 +00:00
|
|
|
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:
|
2001-02-06 10:12:15 +00:00
|
|
|
ifmp = TAILQ_NEXT(&ifm, ifma_link);
|
2000-07-07 19:19:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#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));
|
2001-06-11 12:39:29 +00:00
|
|
|
if (!nam) {
|
|
|
|
fprintf(stderr, "ifmcstat: not enough core\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2000-07-07 19:19:25 +00:00
|
|
|
|
2000-12-30 20:56:04 +00:00
|
|
|
LIST_FOREACH(mkp, &in6_mk, mk_entry) {
|
2000-07-07 19:19:25 +00:00
|
|
|
KREAD(mkp, &mk, struct multi6_kludge);
|
|
|
|
if (strcmp(nam, ifname(mk.mk_ifp)) == 0 &&
|
2000-12-30 20:56:04 +00:00
|
|
|
LIST_FIRST(&mk.mk_head)) {
|
2000-07-07 19:19:25 +00:00
|
|
|
printf("\t(on kludge entry for %s)\n", nam);
|
2000-12-30 20:56:04 +00:00
|
|
|
in6_multilist(LIST_FIRST(&mk.mk_head));
|
2000-07-07 19:19:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(nam);
|
|
|
|
}
|
|
|
|
#endif
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|
|
|
|
|
2000-07-07 19:19:25 +00:00
|
|
|
struct in6_multi *
|
1999-12-28 02:37:14 +00:00
|
|
|
in6_multientry(mc)
|
|
|
|
struct in6_multi *mc;
|
|
|
|
{
|
|
|
|
struct in6_multi multi;
|
|
|
|
|
|
|
|
KREAD(mc, &multi, struct in6_multi);
|
2000-07-07 19:19:25 +00:00
|
|
|
printf("\t\tgroup %s", inet6_n2a(&multi.in6m_addr));
|
|
|
|
printf(" refcnt %u\n", multi.in6m_refcount);
|
2000-12-30 20:56:04 +00:00
|
|
|
return(LIST_NEXT(&multi, in6m_entry));
|
2000-07-07 19:19:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
in6_multilist(mc)
|
|
|
|
struct in6_multi *mc;
|
|
|
|
{
|
|
|
|
while (mc)
|
|
|
|
mc = in6_multientry(mc);
|
1999-12-28 02:37:14 +00:00
|
|
|
}
|