• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 // Copyright (C) 2018 Facebook
3 
4 #define _GNU_SOURCE
5 #include <errno.h>
6 #include <fcntl.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <unistd.h>
10 #include <libbpf.h>
11 #include <net/if.h>
12 #include <linux/rtnetlink.h>
13 #include <linux/tc_act/tc_bpf.h>
14 #include <sys/socket.h>
15 #include <sys/stat.h>
16 #include <sys/types.h>
17 
18 #include <bpf.h>
19 #include <nlattr.h>
20 #include "main.h"
21 #include "netlink_dumper.h"
22 
23 struct ip_devname_ifindex {
24 	char	devname[64];
25 	int	ifindex;
26 };
27 
28 struct bpf_netdev_t {
29 	struct ip_devname_ifindex *devices;
30 	int	used_len;
31 	int	array_len;
32 	int	filter_idx;
33 };
34 
35 struct tc_kind_handle {
36 	char	kind[64];
37 	int	handle;
38 };
39 
40 struct bpf_tcinfo_t {
41 	struct tc_kind_handle	*handle_array;
42 	int			used_len;
43 	int			array_len;
44 	bool			is_qdisc;
45 };
46 
47 struct bpf_filter_t {
48 	const char	*kind;
49 	const char	*devname;
50 	int		ifindex;
51 };
52 
53 struct bpf_attach_info {
54 	__u32 flow_dissector_id;
55 };
56 
57 enum net_attach_type {
58 	NET_ATTACH_TYPE_XDP,
59 	NET_ATTACH_TYPE_XDP_GENERIC,
60 	NET_ATTACH_TYPE_XDP_DRIVER,
61 	NET_ATTACH_TYPE_XDP_OFFLOAD,
62 };
63 
64 static const char * const attach_type_strings[] = {
65 	[NET_ATTACH_TYPE_XDP]		= "xdp",
66 	[NET_ATTACH_TYPE_XDP_GENERIC]	= "xdpgeneric",
67 	[NET_ATTACH_TYPE_XDP_DRIVER]	= "xdpdrv",
68 	[NET_ATTACH_TYPE_XDP_OFFLOAD]	= "xdpoffload",
69 };
70 
71 const size_t net_attach_type_size = ARRAY_SIZE(attach_type_strings);
72 
parse_attach_type(const char * str)73 static enum net_attach_type parse_attach_type(const char *str)
74 {
75 	enum net_attach_type type;
76 
77 	for (type = 0; type < net_attach_type_size; type++) {
78 		if (attach_type_strings[type] &&
79 		    is_prefix(str, attach_type_strings[type]))
80 			return type;
81 	}
82 
83 	return net_attach_type_size;
84 }
85 
dump_link_nlmsg(void * cookie,void * msg,struct nlattr ** tb)86 static int dump_link_nlmsg(void *cookie, void *msg, struct nlattr **tb)
87 {
88 	struct bpf_netdev_t *netinfo = cookie;
89 	struct ifinfomsg *ifinfo = msg;
90 
91 	if (netinfo->filter_idx > 0 && netinfo->filter_idx != ifinfo->ifi_index)
92 		return 0;
93 
94 	if (netinfo->used_len == netinfo->array_len) {
95 		netinfo->devices = realloc(netinfo->devices,
96 			(netinfo->array_len + 16) *
97 			sizeof(struct ip_devname_ifindex));
98 		if (!netinfo->devices)
99 			return -ENOMEM;
100 
101 		netinfo->array_len += 16;
102 	}
103 	netinfo->devices[netinfo->used_len].ifindex = ifinfo->ifi_index;
104 	snprintf(netinfo->devices[netinfo->used_len].devname,
105 		 sizeof(netinfo->devices[netinfo->used_len].devname),
106 		 "%s",
107 		 tb[IFLA_IFNAME]
108 			 ? libbpf_nla_getattr_str(tb[IFLA_IFNAME])
109 			 : "");
110 	netinfo->used_len++;
111 
112 	return do_xdp_dump(ifinfo, tb);
113 }
114 
dump_class_qdisc_nlmsg(void * cookie,void * msg,struct nlattr ** tb)115 static int dump_class_qdisc_nlmsg(void *cookie, void *msg, struct nlattr **tb)
116 {
117 	struct bpf_tcinfo_t *tcinfo = cookie;
118 	struct tcmsg *info = msg;
119 
120 	if (tcinfo->is_qdisc) {
121 		/* skip clsact qdisc */
122 		if (tb[TCA_KIND] &&
123 		    strcmp(libbpf_nla_data(tb[TCA_KIND]), "clsact") == 0)
124 			return 0;
125 		if (info->tcm_handle == 0)
126 			return 0;
127 	}
128 
129 	if (tcinfo->used_len == tcinfo->array_len) {
130 		tcinfo->handle_array = realloc(tcinfo->handle_array,
131 			(tcinfo->array_len + 16) * sizeof(struct tc_kind_handle));
132 		if (!tcinfo->handle_array)
133 			return -ENOMEM;
134 
135 		tcinfo->array_len += 16;
136 	}
137 	tcinfo->handle_array[tcinfo->used_len].handle = info->tcm_handle;
138 	snprintf(tcinfo->handle_array[tcinfo->used_len].kind,
139 		 sizeof(tcinfo->handle_array[tcinfo->used_len].kind),
140 		 "%s",
141 		 tb[TCA_KIND]
142 			 ? libbpf_nla_getattr_str(tb[TCA_KIND])
143 			 : "unknown");
144 	tcinfo->used_len++;
145 
146 	return 0;
147 }
148 
dump_filter_nlmsg(void * cookie,void * msg,struct nlattr ** tb)149 static int dump_filter_nlmsg(void *cookie, void *msg, struct nlattr **tb)
150 {
151 	const struct bpf_filter_t *filter_info = cookie;
152 
153 	return do_filter_dump((struct tcmsg *)msg, tb, filter_info->kind,
154 			      filter_info->devname, filter_info->ifindex);
155 }
156 
show_dev_tc_bpf(int sock,unsigned int nl_pid,struct ip_devname_ifindex * dev)157 static int show_dev_tc_bpf(int sock, unsigned int nl_pid,
158 			   struct ip_devname_ifindex *dev)
159 {
160 	struct bpf_filter_t filter_info;
161 	struct bpf_tcinfo_t tcinfo;
162 	int i, handle, ret = 0;
163 
164 	tcinfo.handle_array = NULL;
165 	tcinfo.used_len = 0;
166 	tcinfo.array_len = 0;
167 
168 	tcinfo.is_qdisc = false;
169 	ret = libbpf_nl_get_class(sock, nl_pid, dev->ifindex,
170 				  dump_class_qdisc_nlmsg, &tcinfo);
171 	if (ret)
172 		goto out;
173 
174 	tcinfo.is_qdisc = true;
175 	ret = libbpf_nl_get_qdisc(sock, nl_pid, dev->ifindex,
176 				  dump_class_qdisc_nlmsg, &tcinfo);
177 	if (ret)
178 		goto out;
179 
180 	filter_info.devname = dev->devname;
181 	filter_info.ifindex = dev->ifindex;
182 	for (i = 0; i < tcinfo.used_len; i++) {
183 		filter_info.kind = tcinfo.handle_array[i].kind;
184 		ret = libbpf_nl_get_filter(sock, nl_pid, dev->ifindex,
185 					   tcinfo.handle_array[i].handle,
186 					   dump_filter_nlmsg, &filter_info);
187 		if (ret)
188 			goto out;
189 	}
190 
191 	/* root, ingress and egress handle */
192 	handle = TC_H_ROOT;
193 	filter_info.kind = "root";
194 	ret = libbpf_nl_get_filter(sock, nl_pid, dev->ifindex, handle,
195 				   dump_filter_nlmsg, &filter_info);
196 	if (ret)
197 		goto out;
198 
199 	handle = TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_INGRESS);
200 	filter_info.kind = "clsact/ingress";
201 	ret = libbpf_nl_get_filter(sock, nl_pid, dev->ifindex, handle,
202 				   dump_filter_nlmsg, &filter_info);
203 	if (ret)
204 		goto out;
205 
206 	handle = TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_EGRESS);
207 	filter_info.kind = "clsact/egress";
208 	ret = libbpf_nl_get_filter(sock, nl_pid, dev->ifindex, handle,
209 				   dump_filter_nlmsg, &filter_info);
210 	if (ret)
211 		goto out;
212 
213 out:
214 	free(tcinfo.handle_array);
215 	return 0;
216 }
217 
query_flow_dissector(struct bpf_attach_info * attach_info)218 static int query_flow_dissector(struct bpf_attach_info *attach_info)
219 {
220 	__u32 attach_flags;
221 	__u32 prog_ids[1];
222 	__u32 prog_cnt;
223 	int err;
224 	int fd;
225 
226 	fd = open("/proc/self/ns/net", O_RDONLY);
227 	if (fd < 0) {
228 		p_err("can't open /proc/self/ns/net: %s",
229 		      strerror(errno));
230 		return -1;
231 	}
232 	prog_cnt = ARRAY_SIZE(prog_ids);
233 	err = bpf_prog_query(fd, BPF_FLOW_DISSECTOR, 0,
234 			     &attach_flags, prog_ids, &prog_cnt);
235 	close(fd);
236 	if (err) {
237 		if (errno == EINVAL) {
238 			/* Older kernel's don't support querying
239 			 * flow dissector programs.
240 			 */
241 			errno = 0;
242 			return 0;
243 		}
244 		p_err("can't query prog: %s", strerror(errno));
245 		return -1;
246 	}
247 
248 	if (prog_cnt == 1)
249 		attach_info->flow_dissector_id = prog_ids[0];
250 
251 	return 0;
252 }
253 
net_parse_dev(int * argc,char *** argv)254 static int net_parse_dev(int *argc, char ***argv)
255 {
256 	int ifindex;
257 
258 	if (is_prefix(**argv, "dev")) {
259 		NEXT_ARGP();
260 
261 		ifindex = if_nametoindex(**argv);
262 		if (!ifindex)
263 			p_err("invalid devname %s", **argv);
264 
265 		NEXT_ARGP();
266 	} else {
267 		p_err("expected 'dev', got: '%s'?", **argv);
268 		return -1;
269 	}
270 
271 	return ifindex;
272 }
273 
do_attach_detach_xdp(int progfd,enum net_attach_type attach_type,int ifindex,bool overwrite)274 static int do_attach_detach_xdp(int progfd, enum net_attach_type attach_type,
275 				int ifindex, bool overwrite)
276 {
277 	__u32 flags = 0;
278 
279 	if (!overwrite)
280 		flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
281 	if (attach_type == NET_ATTACH_TYPE_XDP_GENERIC)
282 		flags |= XDP_FLAGS_SKB_MODE;
283 	if (attach_type == NET_ATTACH_TYPE_XDP_DRIVER)
284 		flags |= XDP_FLAGS_DRV_MODE;
285 	if (attach_type == NET_ATTACH_TYPE_XDP_OFFLOAD)
286 		flags |= XDP_FLAGS_HW_MODE;
287 
288 	return bpf_set_link_xdp_fd(ifindex, progfd, flags);
289 }
290 
do_attach(int argc,char ** argv)291 static int do_attach(int argc, char **argv)
292 {
293 	enum net_attach_type attach_type;
294 	int progfd, ifindex, err = 0;
295 	bool overwrite = false;
296 
297 	/* parse attach args */
298 	if (!REQ_ARGS(5))
299 		return -EINVAL;
300 
301 	attach_type = parse_attach_type(*argv);
302 	if (attach_type == net_attach_type_size) {
303 		p_err("invalid net attach/detach type: %s", *argv);
304 		return -EINVAL;
305 	}
306 	NEXT_ARG();
307 
308 	progfd = prog_parse_fd(&argc, &argv);
309 	if (progfd < 0)
310 		return -EINVAL;
311 
312 	ifindex = net_parse_dev(&argc, &argv);
313 	if (ifindex < 1) {
314 		err = -EINVAL;
315 		goto cleanup;
316 	}
317 
318 	if (argc) {
319 		if (is_prefix(*argv, "overwrite")) {
320 			overwrite = true;
321 		} else {
322 			p_err("expected 'overwrite', got: '%s'?", *argv);
323 			err = -EINVAL;
324 			goto cleanup;
325 		}
326 	}
327 
328 	/* attach xdp prog */
329 	if (is_prefix("xdp", attach_type_strings[attach_type]))
330 		err = do_attach_detach_xdp(progfd, attach_type, ifindex,
331 					   overwrite);
332 	if (err) {
333 		p_err("interface %s attach failed: %s",
334 		      attach_type_strings[attach_type], strerror(-err));
335 		goto cleanup;
336 	}
337 
338 	if (json_output)
339 		jsonw_null(json_wtr);
340 cleanup:
341 	close(progfd);
342 	return err;
343 }
344 
do_detach(int argc,char ** argv)345 static int do_detach(int argc, char **argv)
346 {
347 	enum net_attach_type attach_type;
348 	int progfd, ifindex, err = 0;
349 
350 	/* parse detach args */
351 	if (!REQ_ARGS(3))
352 		return -EINVAL;
353 
354 	attach_type = parse_attach_type(*argv);
355 	if (attach_type == net_attach_type_size) {
356 		p_err("invalid net attach/detach type: %s", *argv);
357 		return -EINVAL;
358 	}
359 	NEXT_ARG();
360 
361 	ifindex = net_parse_dev(&argc, &argv);
362 	if (ifindex < 1)
363 		return -EINVAL;
364 
365 	/* detach xdp prog */
366 	progfd = -1;
367 	if (is_prefix("xdp", attach_type_strings[attach_type]))
368 		err = do_attach_detach_xdp(progfd, attach_type, ifindex, NULL);
369 
370 	if (err < 0) {
371 		p_err("interface %s detach failed: %s",
372 		      attach_type_strings[attach_type], strerror(-err));
373 		return err;
374 	}
375 
376 	if (json_output)
377 		jsonw_null(json_wtr);
378 
379 	return 0;
380 }
381 
do_show(int argc,char ** argv)382 static int do_show(int argc, char **argv)
383 {
384 	struct bpf_attach_info attach_info = {};
385 	int i, sock, ret, filter_idx = -1;
386 	struct bpf_netdev_t dev_array;
387 	unsigned int nl_pid;
388 	char err_buf[256];
389 
390 	if (argc == 2) {
391 		filter_idx = net_parse_dev(&argc, &argv);
392 		if (filter_idx < 1)
393 			return -1;
394 	} else if (argc != 0) {
395 		usage();
396 	}
397 
398 	ret = query_flow_dissector(&attach_info);
399 	if (ret)
400 		return -1;
401 
402 	sock = libbpf_netlink_open(&nl_pid);
403 	if (sock < 0) {
404 		fprintf(stderr, "failed to open netlink sock\n");
405 		return -1;
406 	}
407 
408 	dev_array.devices = NULL;
409 	dev_array.used_len = 0;
410 	dev_array.array_len = 0;
411 	dev_array.filter_idx = filter_idx;
412 
413 	if (json_output)
414 		jsonw_start_array(json_wtr);
415 	NET_START_OBJECT;
416 	NET_START_ARRAY("xdp", "%s:\n");
417 	ret = libbpf_nl_get_link(sock, nl_pid, dump_link_nlmsg, &dev_array);
418 	NET_END_ARRAY("\n");
419 
420 	if (!ret) {
421 		NET_START_ARRAY("tc", "%s:\n");
422 		for (i = 0; i < dev_array.used_len; i++) {
423 			ret = show_dev_tc_bpf(sock, nl_pid,
424 					      &dev_array.devices[i]);
425 			if (ret)
426 				break;
427 		}
428 		NET_END_ARRAY("\n");
429 	}
430 
431 	NET_START_ARRAY("flow_dissector", "%s:\n");
432 	if (attach_info.flow_dissector_id > 0)
433 		NET_DUMP_UINT("id", "id %u", attach_info.flow_dissector_id);
434 	NET_END_ARRAY("\n");
435 
436 	NET_END_OBJECT;
437 	if (json_output)
438 		jsonw_end_array(json_wtr);
439 
440 	if (ret) {
441 		if (json_output)
442 			jsonw_null(json_wtr);
443 		libbpf_strerror(ret, err_buf, sizeof(err_buf));
444 		fprintf(stderr, "Error: %s\n", err_buf);
445 	}
446 	free(dev_array.devices);
447 	close(sock);
448 	return ret;
449 }
450 
do_help(int argc,char ** argv)451 static int do_help(int argc, char **argv)
452 {
453 	if (json_output) {
454 		jsonw_null(json_wtr);
455 		return 0;
456 	}
457 
458 	fprintf(stderr,
459 		"Usage: %s %s { show | list } [dev <devname>]\n"
460 		"       %s %s attach ATTACH_TYPE PROG dev <devname> [ overwrite ]\n"
461 		"       %s %s detach ATTACH_TYPE dev <devname>\n"
462 		"       %s %s help\n"
463 		"\n"
464 		"       " HELP_SPEC_PROGRAM "\n"
465 		"       ATTACH_TYPE := { xdp | xdpgeneric | xdpdrv | xdpoffload }\n"
466 		"\n"
467 		"Note: Only xdp and tc attachments are supported now.\n"
468 		"      For progs attached to cgroups, use \"bpftool cgroup\"\n"
469 		"      to dump program attachments. For program types\n"
470 		"      sk_{filter,skb,msg,reuseport} and lwt/seg6, please\n"
471 		"      consult iproute2.\n",
472 		bin_name, argv[-2], bin_name, argv[-2], bin_name, argv[-2],
473 		bin_name, argv[-2]);
474 
475 	return 0;
476 }
477 
478 static const struct cmd cmds[] = {
479 	{ "show",	do_show },
480 	{ "list",	do_show },
481 	{ "attach",	do_attach },
482 	{ "detach",	do_detach },
483 	{ "help",	do_help },
484 	{ 0 }
485 };
486 
do_net(int argc,char ** argv)487 int do_net(int argc, char **argv)
488 {
489 	return cmd_select(cmds, argc, argv, do_help);
490 }
491