1/*-
2 * Copyright (c) 2012 maksim yevmenkin <emax@freebsd.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted providing that the following conditions
7 * are met:
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.
13 *
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.
25 */
26
27/* gcc -Wall -ggdb ifpifa.c -lkvm -o ifpifa */
28
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>
37
38#include <net/if.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>
46
47#include <err.h>
48#include <fcntl.h>
49#include <kvm.h>
50#include <limits.h>
51#include <nlist.h>
52#include <stdio.h>
53#include <stdlib.h>
54#include <string.h>
55
56__FBSDID("$FreeBSD$");
57
58static struct nlist	nl[] = {
59#define N_IFNET         0
60        { .n_name = "_ifnet", },
61	{ .n_name = NULL, },
62};
63
64static int
65kread(kvm_t *kd, u_long addr, char *buffer, int size)
66{
67	if (kd == NULL || buffer == NULL)
68		return (-1);
69
70	if (kvm_read(kd, addr, buffer, size) != size) {
71		warnx("kvm_read: %s", kvm_geterr(kd));
72		return (-1);
73	}
74
75	return (0);
76}
77
78int
79main(void)
80{
81	kvm_t *kd;
82	char errbuf[_POSIX2_LINE_MAX];
83	u_long ifnetaddr, ifnetaddr_next;
84	u_long ifaddraddr, ifaddraddr_next;
85        struct ifnet ifnet;
86        struct ifnethead ifnethead;
87        union {
88		struct ifaddr ifa;
89		struct in_ifaddr in;
90		struct in6_ifaddr in6;
91        } ifaddr;
92	union {
93		struct sockaddr	*sa;
94		struct sockaddr_dl *sal;
95		struct sockaddr_in *sa4;
96		struct sockaddr_in6 *sa6;
97	} sa;
98	char addr[INET6_ADDRSTRLEN];
99
100	kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
101	if (kd == NULL) {
102		warnx("kvm_openfiles: %s", errbuf);
103		exit(0);
104	}
105
106	if (kvm_nlist(kd, nl) < 0) {
107                warnx("kvm_nlist: %s", kvm_geterr(kd));
108                goto out;
109        }
110
111	if (nl[N_IFNET].n_type == 0) {
112		warnx("kvm_nlist: no namelist");
113		goto out;
114	}
115
116	if (kread(kd, nl[N_IFNET].n_value,
117		  (char *) &ifnethead, sizeof(ifnethead)) != 0)
118		goto out;
119
120	for (ifnetaddr = (u_long) TAILQ_FIRST(&ifnethead);
121	     ifnetaddr != 0;
122	     ifnetaddr = ifnetaddr_next) {
123		if (kread(kd, ifnetaddr, (char *) &ifnet, sizeof(ifnet)) != 0)
124			goto out;
125		ifnetaddr_next = (u_long) TAILQ_NEXT(&ifnet, if_link);
126
127		printf("%s\n", ifnet.if_xname);
128
129		for (ifaddraddr = (u_long) TAILQ_FIRST(&ifnet.if_addrhead);
130		     ifaddraddr != 0;
131		     ifaddraddr = ifaddraddr_next) {
132			if (kread(kd, ifaddraddr,
133				  (char *) &ifaddr, sizeof(ifaddr)) != 0)
134				goto out;
135
136			ifaddraddr_next = (u_long)
137				TAILQ_NEXT(&ifaddr.ifa, ifa_link);
138
139			sa.sa = (struct sockaddr *)(
140				(unsigned char *) ifaddr.ifa.ifa_addr -
141				(unsigned char *) ifaddraddr +
142				(unsigned char *) &ifaddr);
143
144			switch (sa.sa->sa_family) {
145			case AF_LINK:
146				switch (sa.sal->sdl_type) {
147				case IFT_ETHER:
148				case IFT_FDDI:
149	     				ether_ntoa_r((struct ether_addr * )
150						LLADDR(sa.sal), addr);
151					break;
152
153				case IFT_LOOP:
154					strcpy(addr, "loopback");
155					break;
156
157				default:
158					snprintf(addr, sizeof(addr),
159						 "<Link type %#x>",
160						sa.sal->sdl_type);
161					break;
162				}
163				break;
164
165			case AF_INET:
166				inet_ntop(AF_INET, &sa.sa4->sin_addr,
167					addr, sizeof(addr));
168				break;
169
170			case AF_INET6:
171				inet_ntop(AF_INET6, &sa.sa6->sin6_addr,
172					addr, sizeof(addr));
173				break;
174
175			default:
176				snprintf(addr, sizeof(addr), "family=%d",
177					sa.sa->sa_family);
178				break;
179			}
180
181			printf("\t%s ifa_refcnt=%u\n",
182				addr, ifaddr.ifa.ifa_refcnt);
183		}
184	}
185out:
186	kvm_close(kd);
187
188	return (0);
189}
190
191