2 * Copyright (c) 2012 maksim yevmenkin <emax@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted providing that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
22 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
23 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 * POSSIBILITY OF SUCH DAMAGE.
27 /* gcc -Wall -ggdb ifpifa.c -lkvm -o ifpifa */
29 #include <sys/types.h>
30 #include <sys/callout.h>
31 #include <sys/cdefs.h>
32 #include <sys/param.h>
33 #include <sys/protosw.h>
34 #include <sys/queue.h>
35 #include <sys/socket.h>
36 #include <sys/socketvar.h>
39 #include <net/if_var.h>
40 #include <net/if_dl.h>
41 #include <net/if_types.h>
42 #include <net/ethernet.h>
43 #include <netinet/in.h>
44 #include <netinet/in_var.h>
45 #include <arpa/inet.h>
56 __FBSDID("$FreeBSD$");
58 static struct nlist nl[] = {
60 { .n_name = "_ifnet", },
65 kread(kvm_t *kd, u_long addr, char *buffer, int size)
67 if (kd == NULL || buffer == NULL)
70 if (kvm_read(kd, addr, buffer, size) != size) {
71 warnx("kvm_read: %s", kvm_geterr(kd));
82 char errbuf[_POSIX2_LINE_MAX];
83 u_long ifnetaddr, ifnetaddr_next;
84 u_long ifaddraddr, ifaddraddr_next;
86 struct ifnethead ifnethead;
90 struct in6_ifaddr in6;
94 struct sockaddr_dl *sal;
95 struct sockaddr_in *sa4;
96 struct sockaddr_in6 *sa6;
98 char addr[INET6_ADDRSTRLEN];
100 kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
102 warnx("kvm_openfiles: %s", errbuf);
106 if (kvm_nlist(kd, nl) < 0) {
107 warnx("kvm_nlist: %s", kvm_geterr(kd));
111 if (nl[N_IFNET].n_type == 0) {
112 warnx("kvm_nlist: no namelist");
116 if (kread(kd, nl[N_IFNET].n_value,
117 (char *) &ifnethead, sizeof(ifnethead)) != 0)
120 for (ifnetaddr = (u_long) TAILQ_FIRST(&ifnethead);
122 ifnetaddr = ifnetaddr_next) {
123 if (kread(kd, ifnetaddr, (char *) &ifnet, sizeof(ifnet)) != 0)
125 ifnetaddr_next = (u_long) TAILQ_NEXT(&ifnet, if_link);
127 printf("%s\n", ifnet.if_xname);
129 for (ifaddraddr = (u_long) TAILQ_FIRST(&ifnet.if_addrhead);
131 ifaddraddr = ifaddraddr_next) {
132 if (kread(kd, ifaddraddr,
133 (char *) &ifaddr, sizeof(ifaddr)) != 0)
136 ifaddraddr_next = (u_long)
137 TAILQ_NEXT(&ifaddr.ifa, ifa_link);
139 sa.sa = (struct sockaddr *)(
140 (unsigned char *) ifaddr.ifa.ifa_addr -
141 (unsigned char *) ifaddraddr +
142 (unsigned char *) &ifaddr);
144 switch (sa.sa->sa_family) {
146 switch (sa.sal->sdl_type) {
149 ether_ntoa_r((struct ether_addr * )
150 LLADDR(sa.sal), addr);
154 strcpy(addr, "loopback");
158 snprintf(addr, sizeof(addr),
166 inet_ntop(AF_INET, &sa.sa4->sin_addr,
171 inet_ntop(AF_INET6, &sa.sa6->sin6_addr,
176 snprintf(addr, sizeof(addr), "family=%d",
181 printf("\t%s ifa_refcnt=%u\n",
182 addr, ifaddr.ifa.ifa_refcnt);