xref: /freebsd/tools/tools/ifpifa/ifpifa.c (revision 6bfca4dcab07dad45a805879d954876b353c0810)
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 
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 static struct nlist	nl[] = {
56 #define N_IFNET         0
57         { .n_name = "_ifnet", },
58 	{ .n_name = NULL, },
59 };
60 
61 static int
kread(kvm_t * kd,u_long addr,char * buffer,int size)62 kread(kvm_t *kd, u_long addr, char *buffer, int size)
63 {
64 	if (kd == NULL || buffer == NULL)
65 		return (-1);
66 
67 	if (kvm_read(kd, addr, buffer, size) != size) {
68 		warnx("kvm_read: %s", kvm_geterr(kd));
69 		return (-1);
70 	}
71 
72 	return (0);
73 }
74 
75 int
main(void)76 main(void)
77 {
78 	kvm_t *kd;
79 	char errbuf[_POSIX2_LINE_MAX];
80 	u_long ifnetaddr, ifnetaddr_next;
81 	u_long ifaddraddr, ifaddraddr_next;
82         struct ifnet ifnet;
83         struct ifnethead ifnethead;
84         union {
85 		struct ifaddr ifa;
86 		struct in_ifaddr in;
87 		struct in6_ifaddr in6;
88         } ifaddr;
89 	union {
90 		struct sockaddr	*sa;
91 		struct sockaddr_dl *sal;
92 		struct sockaddr_in *sa4;
93 		struct sockaddr_in6 *sa6;
94 	} sa;
95 	char addr[INET6_ADDRSTRLEN];
96 
97 	kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
98 	if (kd == NULL) {
99 		warnx("kvm_openfiles: %s", errbuf);
100 		exit(0);
101 	}
102 
103 	if (kvm_nlist(kd, nl) < 0) {
104                 warnx("kvm_nlist: %s", kvm_geterr(kd));
105                 goto out;
106         }
107 
108 	if (nl[N_IFNET].n_type == 0) {
109 		warnx("kvm_nlist: no namelist");
110 		goto out;
111 	}
112 
113 	if (kread(kd, nl[N_IFNET].n_value,
114 		  (char *) &ifnethead, sizeof(ifnethead)) != 0)
115 		goto out;
116 
117 	for (ifnetaddr = (u_long) TAILQ_FIRST(&ifnethead);
118 	     ifnetaddr != 0;
119 	     ifnetaddr = ifnetaddr_next) {
120 		if (kread(kd, ifnetaddr, (char *) &ifnet, sizeof(ifnet)) != 0)
121 			goto out;
122 		ifnetaddr_next = (u_long) TAILQ_NEXT(&ifnet, if_link);
123 
124 		printf("%s\n", ifnet.if_xname);
125 
126 		for (ifaddraddr = (u_long) TAILQ_FIRST(&ifnet.if_addrhead);
127 		     ifaddraddr != 0;
128 		     ifaddraddr = ifaddraddr_next) {
129 			if (kread(kd, ifaddraddr,
130 				  (char *) &ifaddr, sizeof(ifaddr)) != 0)
131 				goto out;
132 
133 			ifaddraddr_next = (u_long)
134 				TAILQ_NEXT(&ifaddr.ifa, ifa_link);
135 
136 			sa.sa = (struct sockaddr *)(
137 				(unsigned char *) ifaddr.ifa.ifa_addr -
138 				(unsigned char *) ifaddraddr +
139 				(unsigned char *) &ifaddr);
140 
141 			switch (sa.sa->sa_family) {
142 			case AF_LINK:
143 				switch (sa.sal->sdl_type) {
144 				case IFT_ETHER:
145 				case IFT_FDDI:
146 	     				ether_ntoa_r((struct ether_addr * )
147 						LLADDR(sa.sal), addr);
148 					break;
149 
150 				case IFT_LOOP:
151 					strcpy(addr, "loopback");
152 					break;
153 
154 				default:
155 					snprintf(addr, sizeof(addr),
156 						 "<Link type %#x>",
157 						sa.sal->sdl_type);
158 					break;
159 				}
160 				break;
161 
162 			case AF_INET:
163 				inet_ntop(AF_INET, &sa.sa4->sin_addr,
164 					addr, sizeof(addr));
165 				break;
166 
167 			case AF_INET6:
168 				inet_ntop(AF_INET6, &sa.sa6->sin6_addr,
169 					addr, sizeof(addr));
170 				break;
171 
172 			default:
173 				snprintf(addr, sizeof(addr), "family=%d",
174 					sa.sa->sa_family);
175 				break;
176 			}
177 
178 			printf("\t%s ifa_refcnt=%u\n",
179 				addr, ifaddr.ifa.ifa_refcnt);
180 		}
181 	}
182 out:
183 	kvm_close(kd);
184 
185 	return (0);
186 }
187 
188