xref: /freebsd/sbin/ifconfig/ifconfig_netlink.c (revision 1d386b48a555f61cb7325543adbbb5c3f3407a66)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2022 Alexander V. Chernikov <melifaro@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <stdbool.h>
32 #include <err.h>
33 #include <errno.h>
34 #include <netdb.h>
35 
36 #include <sys/bitcount.h>
37 #include <sys/param.h>
38 #include <sys/linker.h>
39 #include <sys/module.h>
40 #include <sys/socket.h>
41 #include <sys/sysctl.h>
42 #include <sys/time.h>
43 #include <sys/types.h>
44 
45 #include <netinet/in.h>
46 #include <arpa/inet.h>
47 
48 #include <net/ethernet.h>
49 #include <net/if.h>
50 #include <net/if_dl.h>
51 #include <net/if_strings.h>
52 #include <net/if_types.h>
53 #include "ifconfig.h"
54 #include "ifconfig_netlink.h"
55 
56 static const char	*IFFBITS[] = {
57 	"UP",			/* 00:0x1 IFF_UP*/
58 	"BROADCAST",		/* 01:0x2 IFF_BROADCAST*/
59 	"DEBUG",		/* 02:0x4 IFF_DEBUG*/
60 	"LOOPBACK",		/* 03:0x8 IFF_LOOPBACK*/
61 	"POINTOPOINT",		/* 04:0x10 IFF_POINTOPOINT*/
62 	"NEEDSEPOCH",		/* 05:0x20 IFF_NEEDSEPOCH*/
63 	"RUNNING",		/* 06:0x40 IFF_DRV_RUNNING*/
64 	"NOARP",		/* 07:0x80 IFF_NOARP*/
65 	"PROMISC",		/* 08:0x100 IFF_PROMISC*/
66 	"ALLMULTI",		/* 09:0x200 IFF_ALLMULTI*/
67 	"DRV_OACTIVE",		/* 10:0x400 IFF_DRV_OACTIVE*/
68 	"SIMPLEX",		/* 11:0x800 IFF_SIMPLEX*/
69 	"LINK0",		/* 12:0x1000 IFF_LINK0*/
70 	"LINK1",		/* 13:0x2000 IFF_LINK1*/
71 	"LINK2",		/* 14:0x4000 IFF_LINK2*/
72 	"MULTICAST",		/* 15:0x8000 IFF_MULTICAST*/
73 	"CANTCONFIG",		/* 16:0x10000 IFF_CANTCONFIG*/
74 	"PPROMISC",		/* 17:0x20000 IFF_PPROMISC*/
75 	"MONITOR",		/* 18:0x40000 IFF_MONITOR*/
76 	"STATICARP",		/* 19:0x80000 IFF_STATICARP*/
77 	"STICKYARP",		/* 20:0x100000 IFF_STICKYARP*/
78 	"DYING",		/* 21:0x200000 IFF_DYING*/
79 	"RENAMING",		/* 22:0x400000 IFF_RENAMING*/
80 	"NOGROUP",		/* 23:0x800000 IFF_NOGROUP*/
81 	"LOWER_UP",		/* 24:0x1000000 IFF_NETLINK_1*/
82 };
83 
84 static void
85 print_bits(const char *btype, uint32_t *v, const int v_count,
86     const char **names, const int n_count)
87 {
88 	int num = 0;
89 
90 	for (int i = 0; i < v_count * 32; i++) {
91 		bool is_set = v[i / 32] & (1U << (i % 32));
92 		if (is_set) {
93 			if (num++ == 0)
94 				printf("<");
95 			if (num != 1)
96 				printf(",");
97 			if (i < n_count)
98 				printf("%s", names[i]);
99 			else
100 				printf("%s_%d", btype, i);
101 		}
102 	}
103 	if (num > 0)
104 		printf(">");
105 }
106 
107 static void
108 nl_init_socket(struct snl_state *ss)
109 {
110 	if (snl_init(ss, NETLINK_ROUTE))
111 		return;
112 
113 	if (modfind("netlink") == -1 && errno == ENOENT) {
114 		/* Try to load */
115 		if (kldload("netlink") == -1)
116 			err(1, "netlink is not loaded and load attempt failed");
117 		if (snl_init(ss, NETLINK_ROUTE))
118 			return;
119 	}
120 
121 	err(1, "unable to open netlink socket");
122 }
123 
124 int
125 ifconfig_nl(if_ctx *ctx, int iscreate,
126     const struct afswtch *uafp)
127 {
128 	struct snl_state ss = {};
129 
130 	nl_init_socket(&ss);
131 	ctx->io_ss = &ss;
132 
133 	int error = ifconfig_ioctl(ctx, iscreate, uafp);
134 
135 	snl_free(&ss);
136 	ctx->io_ss = NULL;
137 
138 	return (error);
139 }
140 
141 struct ifa {
142 	struct ifa		*next;
143 	uint32_t		idx;
144 	struct snl_parsed_addr	addr;
145 };
146 
147 struct iface {
148 	struct snl_parsed_link	link;
149 	struct ifa		*ifa;
150 	uint32_t		ifa_count;
151 	uint32_t		idx;
152 };
153 
154 struct ifmap {
155 	uint32_t		size;
156 	uint32_t		count;
157 	struct iface		**ifaces;
158 };
159 
160 /*
161  * Returns ifmap ifindex->snl_parsed_link.
162  * Memory is allocated using snl temporary buffers
163  */
164 static struct ifmap *
165 prepare_ifmap(struct snl_state *ss)
166 {
167 	struct snl_writer nw = {};
168 
169 	snl_init_writer(ss, &nw);
170 	struct nlmsghdr *hdr = snl_create_msg_request(&nw, RTM_GETLINK);
171 	hdr->nlmsg_flags |= NLM_F_DUMP;
172 	snl_reserve_msg_object(&nw, struct ifinfomsg);
173 
174 	if (!snl_finalize_msg(&nw) || !snl_send_message(ss, hdr))
175 		return (NULL);
176 
177 	uint32_t nlmsg_seq = hdr->nlmsg_seq;
178 	struct ifmap *ifmap = snl_allocz(ss, sizeof(*ifmap));
179 	struct snl_errmsg_data e = {};
180 
181 	while ((hdr = snl_read_reply_multi(ss, nlmsg_seq, &e)) != NULL) {
182 		struct iface *iface = snl_allocz(ss, sizeof(*iface));
183 
184 		if (!snl_parse_nlmsg(ss, hdr, &snl_rtm_link_parser, &iface->link))
185 			continue;
186 		if (iface->link.ifi_index >= ifmap->size) {
187 			size_t new_size = MAX(ifmap->size, 32);
188 
189 			while (new_size <= iface->link.ifi_index + 1)
190 				new_size *= 2;
191 
192 			struct iface **ifaces= snl_allocz(ss, new_size * sizeof(void *));
193 			memcpy(ifaces, ifmap->ifaces, ifmap->size * sizeof(void *));
194 			ifmap->ifaces = ifaces;
195 			ifmap->size = new_size;
196 		}
197 		ifmap->ifaces[iface->link.ifi_index] = iface;
198 		ifmap->count++;
199 		iface->idx = ifmap->count;
200 	}
201 	return (ifmap);
202 }
203 
204 uint32_t
205 if_nametoindex_nl(struct snl_state *ss, const char *ifname)
206 {
207 	struct snl_writer nw = {};
208 	struct snl_parsed_link_simple link = {};
209 
210 	snl_init_writer(ss, &nw);
211 	struct nlmsghdr *hdr = snl_create_msg_request(&nw, RTM_GETLINK);
212 	snl_reserve_msg_object(&nw, struct ifinfomsg);
213 	snl_add_msg_attr_string(&nw, IFLA_IFNAME, ifname);
214 
215 	if (!snl_finalize_msg(&nw) || !snl_send_message(ss, hdr))
216 		return (0);
217 
218 	hdr = snl_read_reply(ss, hdr->nlmsg_seq);
219 	if (hdr->nlmsg_type != NL_RTM_NEWLINK)
220 		return (0);
221 	if (!snl_parse_nlmsg(ss, hdr, &snl_rtm_link_parser_simple, &link))
222 		return (0);
223 
224 	return (link.ifi_index);
225 }
226 
227 static void
228 prepare_ifaddrs(struct snl_state *ss, struct ifmap *ifmap)
229 {
230 	struct snl_writer nw = {};
231 
232 	snl_init_writer(ss, &nw);
233 	struct nlmsghdr *hdr = snl_create_msg_request(&nw, RTM_GETADDR);
234 	hdr->nlmsg_flags |= NLM_F_DUMP;
235 	snl_reserve_msg_object(&nw, struct ifaddrmsg);
236 
237 	if (!snl_finalize_msg(&nw) || !snl_send_message(ss, hdr))
238 		return;
239 
240 	uint32_t nlmsg_seq = hdr->nlmsg_seq;
241 	struct snl_errmsg_data e = {};
242 	uint32_t count = 0;
243 
244 	while ((hdr = snl_read_reply_multi(ss, nlmsg_seq, &e)) != NULL) {
245 		struct ifa *ifa = snl_allocz(ss, sizeof(*ifa));
246 
247 		if (!snl_parse_nlmsg(ss, hdr, &snl_rtm_addr_parser, &ifa->addr))
248 			continue;
249 
250 		const uint32_t ifindex = ifa->addr.ifa_index;
251 		if (ifindex >= ifmap->size || ifmap->ifaces[ifindex] == NULL)
252 			continue;
253 		struct iface *iface = ifmap->ifaces[ifindex];
254 		ifa->next = iface->ifa;
255 		ifa->idx = ++count;
256 		iface->ifa = ifa;
257 		iface->ifa_count++;
258 	}
259 }
260 
261 static bool
262 match_iface(struct ifconfig_args *args, struct iface *iface)
263 {
264 	if_link_t *link = &iface->link;
265 
266 	if (args->ifname != NULL && strcmp(args->ifname, link->ifla_ifname))
267 		return (false);
268 
269 	if (!match_if_flags(args, link->ifi_flags))
270 		return (false);
271 
272 	if (!group_member(link->ifla_ifname, args->matchgroup, args->nogroup))
273 		return (false);
274 
275 	if (args->afp == NULL)
276 		return (true);
277 
278 	if (!strcmp(args->afp->af_name, "ether")) {
279 		if (link->ifla_address == NULL)
280 			return (false);
281 
282 		struct sockaddr_dl sdl = {
283 			.sdl_len = sizeof(struct sockaddr_dl),
284 			.sdl_family = AF_LINK,
285 			.sdl_type = link->ifi_type,
286 			.sdl_alen = NLA_DATA_LEN(link->ifla_address),
287 		};
288 		return (match_ether(&sdl));
289 	}
290 
291 	for (struct ifa *ifa = iface->ifa; ifa != NULL; ifa = ifa->next) {
292 		if (args->afp->af_af == ifa->addr.ifa_family)
293 			return (true);
294 	}
295 
296 	return (false);
297 }
298 
299 /* Sort according to the kernel-provided order */
300 static int
301 cmp_iface(const void *_a, const void *_b)
302 {
303 	const struct iface *a = *((const void * const *)_a);
304 	const struct iface *b = *((const void * const *)_b);
305 
306 	return ((a->idx > b->idx) * 2 - 1);
307 }
308 
309 static int
310 cmp_ifaddr(const void *_a, const void *_b)
311 {
312 	const struct ifa *a = *((const void * const *)_a);
313 	const struct ifa *b = *((const void * const *)_b);
314 
315 	if (a->addr.ifa_family != b->addr.ifa_family)
316 		return ((a->addr.ifa_family > b->addr.ifa_family) * 2 - 1);
317 	return ((a->idx > b->idx) * 2 - 1);
318 }
319 
320 static void
321 sort_iface_ifaddrs(struct snl_state *ss, struct iface *iface)
322 {
323 	if (iface->ifa_count == 0)
324 		return;
325 
326 	struct ifa **sorted_ifaddrs = snl_allocz(ss, iface->ifa_count * sizeof(void *));
327 	struct ifa *ifa = iface->ifa;
328 
329 	for (uint32_t i = 0; i < iface->ifa_count; i++) {
330 		struct ifa *ifa_next = ifa->next;
331 
332 		sorted_ifaddrs[i] = ifa;
333 		ifa->next = NULL;
334 		ifa = ifa_next;
335 	}
336 	qsort(sorted_ifaddrs, iface->ifa_count, sizeof(void *), cmp_ifaddr);
337 	ifa = sorted_ifaddrs[0];
338 	iface->ifa = ifa;
339 	for (uint32_t i = 1; i < iface->ifa_count; i++) {
340 		ifa->next = sorted_ifaddrs[i];
341 		ifa = sorted_ifaddrs[i];
342 	}
343 }
344 
345 static void
346 print_ifcaps(if_ctx *ctx, if_link_t *link)
347 {
348 	uint32_t sz_u32 = roundup2(link->iflaf_caps.nla_bitset_size, 32) / 32;
349 
350 	if (sz_u32 > 0) {
351 		uint32_t *caps = link->iflaf_caps.nla_bitset_value;
352 
353 		printf("\toptions=%x", caps[0]);
354 		print_bits("IFCAPS", caps, sz_u32, ifcap_bit_names, nitems(ifcap_bit_names));
355 		putchar('\n');
356 	}
357 
358 	if (ctx->args->supmedia && sz_u32 > 0) {
359 		uint32_t *caps = link->iflaf_caps.nla_bitset_mask;
360 
361 		printf("\tcapabilities=%x", caps[0]);
362 		print_bits("IFCAPS", caps, sz_u32, ifcap_bit_names, nitems(ifcap_bit_names));
363 		putchar('\n');
364 	}
365 }
366 
367 static void
368 status_nl(if_ctx *ctx, struct iface *iface)
369 {
370 	if_link_t *link = &iface->link;
371 	struct ifconfig_args *args = ctx->args;
372 
373 	printf("%s: ", link->ifla_ifname);
374 
375 	printf("flags=%x", link->ifi_flags);
376 	print_bits("IFF", &link->ifi_flags, 1, IFFBITS, nitems(IFFBITS));
377 
378 	print_metric(ctx);
379 	printf(" mtu %d\n", link->ifla_mtu);
380 
381 	if (link->ifla_ifalias != NULL)
382 		printf("\tdescription: %s\n", link->ifla_ifalias);
383 
384 	print_ifcaps(ctx, link);
385 	tunnel_status(ctx);
386 
387 	if (args->allfamilies | (args->afp != NULL && args->afp->af_af == AF_LINK)) {
388 		/* Start with link-level */
389 		const struct afswtch *p = af_getbyfamily(AF_LINK);
390 		if (p != NULL && link->ifla_address != NULL)
391 			p->af_status(ctx, link, NULL);
392 	}
393 
394 	sort_iface_ifaddrs(ctx->io_ss, iface);
395 
396 	for (struct ifa *ifa = iface->ifa; ifa != NULL; ifa = ifa->next) {
397 		if (args->allfamilies) {
398 			const struct afswtch *p = af_getbyfamily(ifa->addr.ifa_family);
399 
400 			if (p != NULL)
401 				p->af_status(ctx, link, &ifa->addr);
402 		} else if (args->afp->af_af == ifa->addr.ifa_family) {
403 			const struct afswtch *p = args->afp;
404 
405 			p->af_status(ctx, link, &ifa->addr);
406 		}
407 	}
408 
409 	/* TODO: convert to netlink */
410 	if (args->allfamilies)
411 		af_other_status(ctx);
412 	else if (args->afp->af_other_status != NULL)
413 		args->afp->af_other_status(ctx);
414 
415 	print_ifstatus(ctx);
416 	if (args->verbose > 0)
417 		sfp_status(ctx);
418 }
419 
420 static int
421 get_local_socket(void)
422 {
423 	int s = socket(AF_LOCAL, SOCK_DGRAM, 0);
424 
425 	if (s < 0)
426 		err(1, "socket(family %u,SOCK_DGRAM)", AF_LOCAL);
427 	return (s);
428 }
429 
430 void
431 list_interfaces_nl(struct ifconfig_args *args)
432 {
433 	struct snl_state ss = {};
434 	struct ifconfig_context _ctx = {
435 		.args = args,
436 		.io_s = get_local_socket(),
437 		.io_ss = &ss,
438 	};
439 	struct ifconfig_context *ctx = &_ctx;
440 
441 	nl_init_socket(&ss);
442 
443 	struct ifmap *ifmap = prepare_ifmap(&ss);
444 	struct iface **sorted_ifaces = snl_allocz(&ss, ifmap->count * sizeof(void *));
445 	for (uint32_t i = 0, num = 0; i < ifmap->size; i++) {
446 		if (ifmap->ifaces[i] != NULL) {
447 			sorted_ifaces[num++] = ifmap->ifaces[i];
448 			if (num == ifmap->count)
449 				break;
450 		}
451 	}
452 	qsort(sorted_ifaces, ifmap->count, sizeof(void *), cmp_iface);
453 	prepare_ifaddrs(&ss, ifmap);
454 
455 	for (uint32_t i = 0, num = 0; i < ifmap->count; i++) {
456 		struct iface *iface = sorted_ifaces[i];
457 
458 		if (!match_iface(args, iface))
459 			continue;
460 
461 		ctx->ifname = iface->link.ifla_ifname;
462 
463 		if (args->namesonly) {
464 			if (num++ != 0)
465 				printf(" ");
466 			fputs(iface->link.ifla_ifname, stdout);
467 		} else if (args->argc == 0)
468 			status_nl(ctx, iface);
469 		else
470 			ifconfig_ioctl(ctx, 0, args->afp);
471 	}
472 	if (args->namesonly)
473 		printf("\n");
474 
475 	close(ctx->io_s);
476 	snl_free(&ss);
477 }
478 
479