xref: /aosp_15_r20/external/ethtool/libmnl/examples/rtnl/rtnl-route-event.c (revision 1b481fc3bb1b45d4cf28d1ec12969dc1055f555d)
1 /* This example is placed in the public domain. */
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <unistd.h>
6 #include <time.h>
7 #include <arpa/inet.h>
8 
9 #include <libmnl/libmnl.h>
10 #include <linux/if.h>
11 #include <linux/if_link.h>
12 #include <linux/rtnetlink.h>
13 
data_attr_cb2(const struct nlattr * attr,void * data)14 static int data_attr_cb2(const struct nlattr *attr, void *data)
15 {
16 	const struct nlattr **tb = data;
17 
18 	/* skip unsupported attribute in user-space */
19 	if (mnl_attr_type_valid(attr, RTAX_MAX) < 0)
20 		return MNL_CB_OK;
21 
22 	if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) {
23 		perror("mnl_attr_validate");
24 		return MNL_CB_ERROR;
25 	}
26 
27 	tb[mnl_attr_get_type(attr)] = attr;
28 	return MNL_CB_OK;
29 }
30 
attributes_show_ipv4(struct nlattr * tb[])31 static void attributes_show_ipv4(struct nlattr *tb[])
32 {
33 	if (tb[RTA_TABLE]) {
34 		printf("table=%u ", mnl_attr_get_u32(tb[RTA_TABLE]));
35 	}
36 	if (tb[RTA_DST]) {
37 		struct in_addr *addr = mnl_attr_get_payload(tb[RTA_DST]);
38 		printf("dst=%s ", inet_ntoa(*addr));
39 	}
40 	if (tb[RTA_SRC]) {
41 		struct in_addr *addr = mnl_attr_get_payload(tb[RTA_SRC]);
42 		printf("src=%s ", inet_ntoa(*addr));
43 	}
44 	if (tb[RTA_OIF]) {
45 		printf("oif=%u ", mnl_attr_get_u32(tb[RTA_OIF]));
46 	}
47 	if (tb[RTA_FLOW]) {
48 		printf("flow=%u ", mnl_attr_get_u32(tb[RTA_FLOW]));
49 	}
50 	if (tb[RTA_PREFSRC]) {
51 		struct in_addr *addr = mnl_attr_get_payload(tb[RTA_PREFSRC]);
52 		printf("prefsrc=%s ", inet_ntoa(*addr));
53 	}
54 	if (tb[RTA_GATEWAY]) {
55 		struct in_addr *addr = mnl_attr_get_payload(tb[RTA_GATEWAY]);
56 		printf("gw=%s ", inet_ntoa(*addr));
57 	}
58 	if (tb[RTA_PRIORITY]) {
59 		printf("prio=%u ", mnl_attr_get_u32(tb[RTA_PRIORITY]));
60 	}
61 	if (tb[RTA_METRICS]) {
62 		int i;
63 		struct nlattr *tbx[RTAX_MAX+1] = {};
64 
65 		mnl_attr_parse_nested(tb[RTA_METRICS], data_attr_cb2, tbx);
66 
67 		for (i=0; i<RTAX_MAX; i++) {
68 			if (tbx[i]) {
69 				printf("metrics[%d]=%u ",
70 					i, mnl_attr_get_u32(tbx[i]));
71 			}
72 		}
73 	}
74 }
75 
76 /* like inet_ntoa(), not reentrant */
inet6_ntoa(struct in6_addr in6)77 static const char *inet6_ntoa(struct in6_addr in6)
78 {
79 	static char buf[INET6_ADDRSTRLEN];
80 
81 	return inet_ntop(AF_INET6, &in6.s6_addr, buf, sizeof(buf));
82 }
83 
attributes_show_ipv6(struct nlattr * tb[])84 static void attributes_show_ipv6(struct nlattr *tb[])
85 {
86 	if (tb[RTA_TABLE]) {
87 		printf("table=%u ", mnl_attr_get_u32(tb[RTA_TABLE]));
88 	}
89 	if (tb[RTA_DST]) {
90 		struct in6_addr *addr = mnl_attr_get_payload(tb[RTA_DST]);
91 		printf("dst=%s ", inet6_ntoa(*addr));
92 	}
93 	if (tb[RTA_SRC]) {
94 		struct in6_addr *addr = mnl_attr_get_payload(tb[RTA_SRC]);
95 		printf("src=%s ", inet6_ntoa(*addr));
96 	}
97 	if (tb[RTA_OIF]) {
98 		printf("oif=%u ", mnl_attr_get_u32(tb[RTA_OIF]));
99 	}
100 	if (tb[RTA_FLOW]) {
101 		printf("flow=%u ", mnl_attr_get_u32(tb[RTA_FLOW]));
102 	}
103 	if (tb[RTA_PREFSRC]) {
104 		struct in6_addr *addr = mnl_attr_get_payload(tb[RTA_PREFSRC]);
105 		printf("prefsrc=%s ", inet6_ntoa(*addr));
106 	}
107 	if (tb[RTA_GATEWAY]) {
108 		struct in6_addr *addr = mnl_attr_get_payload(tb[RTA_GATEWAY]);
109 		printf("gw=%s ", inet6_ntoa(*addr));
110 	}
111 	if (tb[RTA_PRIORITY]) {
112 		printf("prio=%u ", mnl_attr_get_u32(tb[RTA_PRIORITY]));
113 	}
114 	if (tb[RTA_METRICS]) {
115 		int i;
116 		struct nlattr *tbx[RTAX_MAX+1] = {};
117 
118 		mnl_attr_parse_nested(tb[RTA_METRICS], data_attr_cb2, tbx);
119 
120 		for (i=0; i<RTAX_MAX; i++) {
121 			if (tbx[i]) {
122 				printf("metrics[%d]=%u ",
123 					i, mnl_attr_get_u32(tbx[i]));
124 			}
125 		}
126 	}
127 }
128 
data_ipv4_attr_cb(const struct nlattr * attr,void * data)129 static int data_ipv4_attr_cb(const struct nlattr *attr, void *data)
130 {
131 	const struct nlattr **tb = data;
132 	int type = mnl_attr_get_type(attr);
133 
134 	/* skip unsupported attribute in user-space */
135 	if (mnl_attr_type_valid(attr, RTA_MAX) < 0)
136 		return MNL_CB_OK;
137 
138 	switch(type) {
139 	case RTA_TABLE:
140 	case RTA_DST:
141 	case RTA_SRC:
142 	case RTA_OIF:
143 	case RTA_FLOW:
144 	case RTA_PREFSRC:
145 	case RTA_GATEWAY:
146 	case RTA_PRIORITY:
147 		if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) {
148 			perror("mnl_attr_validate");
149 			return MNL_CB_ERROR;
150 		}
151 		break;
152 	case RTA_METRICS:
153 		if (mnl_attr_validate(attr, MNL_TYPE_NESTED) < 0) {
154 			perror("mnl_attr_validate");
155 			return MNL_CB_ERROR;
156 		}
157 		break;
158 	}
159 	tb[type] = attr;
160 	return MNL_CB_OK;
161 }
162 
data_ipv6_attr_cb(const struct nlattr * attr,void * data)163 static int data_ipv6_attr_cb(const struct nlattr *attr, void *data)
164 {
165 	const struct nlattr **tb = data;
166 	int type = mnl_attr_get_type(attr);
167 
168 	/* skip unsupported attribute in user-space */
169 	if (mnl_attr_type_valid(attr, RTA_MAX) < 0)
170 		return MNL_CB_OK;
171 
172 	switch(type) {
173 	case RTA_TABLE:
174 	case RTA_OIF:
175 	case RTA_FLOW:
176 	case RTA_PRIORITY:
177 		if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) {
178 			perror("mnl_attr_validate");
179 			return MNL_CB_ERROR;
180 		}
181 		break;
182 	case RTA_DST:
183 	case RTA_SRC:
184 	case RTA_PREFSRC:
185 	case RTA_GATEWAY:
186 		if (mnl_attr_validate2(attr, MNL_TYPE_BINARY,
187 					sizeof(struct in6_addr)) < 0) {
188 			perror("mnl_attr_validate2");
189 			return MNL_CB_ERROR;
190 		}
191 		break;
192 	case RTA_METRICS:
193 		if (mnl_attr_validate(attr, MNL_TYPE_NESTED) < 0) {
194 			perror("mnl_attr_validate");
195 			return MNL_CB_ERROR;
196 		}
197 		break;
198 	}
199 	tb[type] = attr;
200 	return MNL_CB_OK;
201 }
202 
data_cb(const struct nlmsghdr * nlh,void * data)203 static int data_cb(const struct nlmsghdr *nlh, void *data)
204 {
205 	struct nlattr *tb[RTA_MAX+1] = {};
206 	struct rtmsg *rm = mnl_nlmsg_get_payload(nlh);
207 
208 	switch(nlh->nlmsg_type) {
209 	case RTM_NEWROUTE:
210 		printf("[NEW] ");
211 		break;
212 	case RTM_DELROUTE:
213 		printf("[DEL] ");
214 		break;
215 	}
216 
217 	/* protocol family = AF_INET | AF_INET6 */
218 	printf("family=%u ", rm->rtm_family);
219 
220 	/* destination CIDR, eg. 24 or 32 for IPv4 */
221 	printf("dst_len=%u ", rm->rtm_dst_len);
222 
223 	/* source CIDR */
224 	printf("src_len=%u ", rm->rtm_src_len);
225 
226 	/* type of service (TOS), eg. 0 */
227 	printf("tos=%u ", rm->rtm_tos);
228 
229 	/* table id:
230 	 *	RT_TABLE_UNSPEC		= 0
231 	 *
232 	 * 	... user defined values ...
233 	 *
234 	 *	RT_TABLE_COMPAT		= 252
235 	 *	RT_TABLE_DEFAULT	= 253
236 	 *	RT_TABLE_MAIN		= 254
237 	 *	RT_TABLE_LOCAL		= 255
238 	 *	RT_TABLE_MAX		= 0xFFFFFFFF
239 	 *
240 	 * Synonimous attribute: RTA_TABLE.
241 	 */
242 	printf("table=%u ", rm->rtm_table);
243 
244 	/* type:
245 	 * 	RTN_UNSPEC	= 0
246 	 * 	RTN_UNICAST	= 1
247 	 * 	RTN_LOCAL	= 2
248 	 * 	RTN_BROADCAST	= 3
249 	 *	RTN_ANYCAST	= 4
250 	 *	RTN_MULTICAST	= 5
251 	 *	RTN_BLACKHOLE	= 6
252 	 *	RTN_UNREACHABLE	= 7
253 	 *	RTN_PROHIBIT	= 8
254 	 *	RTN_THROW	= 9
255 	 *	RTN_NAT		= 10
256 	 *	RTN_XRESOLVE	= 11
257 	 *	__RTN_MAX	= 12
258 	 */
259 	printf("type=%u ", rm->rtm_type);
260 
261 	/* scope:
262 	 * 	RT_SCOPE_UNIVERSE	= 0   : everywhere in the universe
263 	 *
264 	 *      ... user defined values ...
265 	 *
266 	 * 	RT_SCOPE_SITE		= 200
267 	 * 	RT_SCOPE_LINK		= 253 : destination attached to link
268 	 * 	RT_SCOPE_HOST		= 254 : local address
269 	 * 	RT_SCOPE_NOWHERE	= 255 : not existing destination
270 	 */
271 	printf("scope=%u ", rm->rtm_scope);
272 
273 	/* protocol:
274 	 * 	RTPROT_UNSPEC	= 0
275 	 * 	RTPROT_REDIRECT = 1
276 	 * 	RTPROT_KERNEL	= 2 : route installed by kernel
277 	 * 	RTPROT_BOOT	= 3 : route installed during boot
278 	 * 	RTPROT_STATIC	= 4 : route installed by administrator
279 	 *
280 	 * Values >= RTPROT_STATIC are not interpreted by kernel, they are
281 	 * just user-defined.
282 	 */
283 	printf("proto=%u ", rm->rtm_protocol);
284 
285 	/* flags:
286 	 * 	RTM_F_NOTIFY	= 0x100: notify user of route change
287 	 * 	RTM_F_CLONED	= 0x200: this route is cloned
288 	 * 	RTM_F_EQUALIZE	= 0x400: Multipath equalizer: NI
289 	 * 	RTM_F_PREFIX	= 0x800: Prefix addresses
290 	 */
291 	printf("flags=%x ", rm->rtm_flags);
292 
293 	switch(rm->rtm_family) {
294 	case AF_INET:
295 		mnl_attr_parse(nlh, sizeof(*rm), data_ipv4_attr_cb, tb);
296 		attributes_show_ipv4(tb);
297 		break;
298 	case AF_INET6:
299 		mnl_attr_parse(nlh, sizeof(*rm), data_ipv6_attr_cb, tb);
300 		attributes_show_ipv6(tb);
301 		break;
302 	}
303 
304 	printf("\n");
305 	return MNL_CB_OK;
306 }
307 
main(int argc,char * argv[])308 int main(int argc, char *argv[])
309 {
310 	struct mnl_socket *nl;
311 	char buf[MNL_SOCKET_BUFFER_SIZE];
312 	int ret;
313 
314 	nl = mnl_socket_open(NETLINK_ROUTE);
315 	if (nl == NULL) {
316 		perror("mnl_socket_open");
317 		exit(EXIT_FAILURE);
318 	}
319 
320 	if (mnl_socket_bind(nl, RTMGRP_IPV4_ROUTE | RTMGRP_IPV6_ROUTE,
321 			    MNL_SOCKET_AUTOPID) < 0) {
322 		perror("mnl_socket_bind");
323 		exit(EXIT_FAILURE);
324 	}
325 
326 	ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
327 	while (ret > 0) {
328 		ret = mnl_cb_run(buf, ret, 0, 0, data_cb, NULL);
329 		if (ret <= MNL_CB_STOP)
330 			break;
331 		ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
332 	}
333 	if (ret == -1) {
334 		perror("error");
335 		exit(EXIT_FAILURE);
336 	}
337 
338 	mnl_socket_close(nl);
339 
340 	return 0;
341 }
342