Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/tools/bpf/bpftool/net.c
26282 views
1
// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2
// Copyright (C) 2018 Facebook
3
4
#ifndef _GNU_SOURCE
5
#define _GNU_SOURCE
6
#endif
7
#include <errno.h>
8
#include <fcntl.h>
9
#include <stdlib.h>
10
#include <string.h>
11
#include <time.h>
12
#include <unistd.h>
13
#include <bpf/bpf.h>
14
#include <bpf/libbpf.h>
15
#include <net/if.h>
16
#include <linux/rtnetlink.h>
17
#include <linux/socket.h>
18
#include <linux/tc_act/tc_bpf.h>
19
#include <sys/socket.h>
20
#include <sys/stat.h>
21
#include <sys/types.h>
22
23
#include "bpf/nlattr.h"
24
#include "main.h"
25
#include "netlink_dumper.h"
26
27
#ifndef SOL_NETLINK
28
#define SOL_NETLINK 270
29
#endif
30
31
struct ip_devname_ifindex {
32
char devname[64];
33
int ifindex;
34
};
35
36
struct bpf_netdev_t {
37
struct ip_devname_ifindex *devices;
38
int used_len;
39
int array_len;
40
int filter_idx;
41
};
42
43
struct tc_kind_handle {
44
char kind[64];
45
int handle;
46
};
47
48
struct bpf_tcinfo_t {
49
struct tc_kind_handle *handle_array;
50
int used_len;
51
int array_len;
52
bool is_qdisc;
53
};
54
55
struct bpf_filter_t {
56
const char *kind;
57
const char *devname;
58
int ifindex;
59
};
60
61
struct bpf_attach_info {
62
__u32 flow_dissector_id;
63
};
64
65
enum net_attach_type {
66
NET_ATTACH_TYPE_XDP,
67
NET_ATTACH_TYPE_XDP_GENERIC,
68
NET_ATTACH_TYPE_XDP_DRIVER,
69
NET_ATTACH_TYPE_XDP_OFFLOAD,
70
NET_ATTACH_TYPE_TCX_INGRESS,
71
NET_ATTACH_TYPE_TCX_EGRESS,
72
};
73
74
static const char * const attach_type_strings[] = {
75
[NET_ATTACH_TYPE_XDP] = "xdp",
76
[NET_ATTACH_TYPE_XDP_GENERIC] = "xdpgeneric",
77
[NET_ATTACH_TYPE_XDP_DRIVER] = "xdpdrv",
78
[NET_ATTACH_TYPE_XDP_OFFLOAD] = "xdpoffload",
79
[NET_ATTACH_TYPE_TCX_INGRESS] = "tcx_ingress",
80
[NET_ATTACH_TYPE_TCX_EGRESS] = "tcx_egress",
81
};
82
83
static const char * const attach_loc_strings[] = {
84
[BPF_TCX_INGRESS] = "tcx/ingress",
85
[BPF_TCX_EGRESS] = "tcx/egress",
86
[BPF_NETKIT_PRIMARY] = "netkit/primary",
87
[BPF_NETKIT_PEER] = "netkit/peer",
88
};
89
90
const size_t net_attach_type_size = ARRAY_SIZE(attach_type_strings);
91
92
static enum net_attach_type parse_attach_type(const char *str)
93
{
94
enum net_attach_type type;
95
96
for (type = 0; type < net_attach_type_size; type++) {
97
if (attach_type_strings[type] &&
98
is_prefix(str, attach_type_strings[type]))
99
return type;
100
}
101
102
return net_attach_type_size;
103
}
104
105
typedef int (*dump_nlmsg_t)(void *cookie, void *msg, struct nlattr **tb);
106
107
typedef int (*__dump_nlmsg_t)(struct nlmsghdr *nlmsg, dump_nlmsg_t, void *cookie);
108
109
static int netlink_open(__u32 *nl_pid)
110
{
111
struct sockaddr_nl sa;
112
socklen_t addrlen;
113
int one = 1, ret;
114
int sock;
115
116
memset(&sa, 0, sizeof(sa));
117
sa.nl_family = AF_NETLINK;
118
119
sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
120
if (sock < 0)
121
return -errno;
122
123
if (setsockopt(sock, SOL_NETLINK, NETLINK_EXT_ACK,
124
&one, sizeof(one)) < 0) {
125
p_err("Netlink error reporting not supported");
126
}
127
128
if (bind(sock, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
129
ret = -errno;
130
goto cleanup;
131
}
132
133
addrlen = sizeof(sa);
134
if (getsockname(sock, (struct sockaddr *)&sa, &addrlen) < 0) {
135
ret = -errno;
136
goto cleanup;
137
}
138
139
if (addrlen != sizeof(sa)) {
140
ret = -LIBBPF_ERRNO__INTERNAL;
141
goto cleanup;
142
}
143
144
*nl_pid = sa.nl_pid;
145
return sock;
146
147
cleanup:
148
close(sock);
149
return ret;
150
}
151
152
static int netlink_recv(int sock, __u32 nl_pid, __u32 seq,
153
__dump_nlmsg_t _fn, dump_nlmsg_t fn,
154
void *cookie)
155
{
156
bool multipart = true;
157
struct nlmsgerr *err;
158
struct nlmsghdr *nh;
159
char buf[4096];
160
int len, ret;
161
162
while (multipart) {
163
multipart = false;
164
len = recv(sock, buf, sizeof(buf), 0);
165
if (len < 0) {
166
ret = -errno;
167
goto done;
168
}
169
170
if (len == 0)
171
break;
172
173
for (nh = (struct nlmsghdr *)buf; NLMSG_OK(nh, (unsigned int)len);
174
nh = NLMSG_NEXT(nh, len)) {
175
if (nh->nlmsg_pid != nl_pid) {
176
ret = -LIBBPF_ERRNO__WRNGPID;
177
goto done;
178
}
179
if (nh->nlmsg_seq != seq) {
180
ret = -LIBBPF_ERRNO__INVSEQ;
181
goto done;
182
}
183
if (nh->nlmsg_flags & NLM_F_MULTI)
184
multipart = true;
185
switch (nh->nlmsg_type) {
186
case NLMSG_ERROR:
187
err = (struct nlmsgerr *)NLMSG_DATA(nh);
188
if (!err->error)
189
continue;
190
ret = err->error;
191
libbpf_nla_dump_errormsg(nh);
192
goto done;
193
case NLMSG_DONE:
194
return 0;
195
default:
196
break;
197
}
198
if (_fn) {
199
ret = _fn(nh, fn, cookie);
200
if (ret)
201
return ret;
202
}
203
}
204
}
205
ret = 0;
206
done:
207
return ret;
208
}
209
210
static int __dump_class_nlmsg(struct nlmsghdr *nlh,
211
dump_nlmsg_t dump_class_nlmsg,
212
void *cookie)
213
{
214
struct nlattr *tb[TCA_MAX + 1], *attr;
215
struct tcmsg *t = NLMSG_DATA(nlh);
216
int len;
217
218
len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*t));
219
attr = (struct nlattr *) ((void *) t + NLMSG_ALIGN(sizeof(*t)));
220
if (libbpf_nla_parse(tb, TCA_MAX, attr, len, NULL) != 0)
221
return -LIBBPF_ERRNO__NLPARSE;
222
223
return dump_class_nlmsg(cookie, t, tb);
224
}
225
226
static int netlink_get_class(int sock, unsigned int nl_pid, int ifindex,
227
dump_nlmsg_t dump_class_nlmsg, void *cookie)
228
{
229
struct {
230
struct nlmsghdr nlh;
231
struct tcmsg t;
232
} req = {
233
.nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg)),
234
.nlh.nlmsg_type = RTM_GETTCLASS,
235
.nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
236
.t.tcm_family = AF_UNSPEC,
237
.t.tcm_ifindex = ifindex,
238
};
239
int seq = time(NULL);
240
241
req.nlh.nlmsg_seq = seq;
242
if (send(sock, &req, req.nlh.nlmsg_len, 0) < 0)
243
return -errno;
244
245
return netlink_recv(sock, nl_pid, seq, __dump_class_nlmsg,
246
dump_class_nlmsg, cookie);
247
}
248
249
static int __dump_qdisc_nlmsg(struct nlmsghdr *nlh,
250
dump_nlmsg_t dump_qdisc_nlmsg,
251
void *cookie)
252
{
253
struct nlattr *tb[TCA_MAX + 1], *attr;
254
struct tcmsg *t = NLMSG_DATA(nlh);
255
int len;
256
257
len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*t));
258
attr = (struct nlattr *) ((void *) t + NLMSG_ALIGN(sizeof(*t)));
259
if (libbpf_nla_parse(tb, TCA_MAX, attr, len, NULL) != 0)
260
return -LIBBPF_ERRNO__NLPARSE;
261
262
return dump_qdisc_nlmsg(cookie, t, tb);
263
}
264
265
static int netlink_get_qdisc(int sock, unsigned int nl_pid, int ifindex,
266
dump_nlmsg_t dump_qdisc_nlmsg, void *cookie)
267
{
268
struct {
269
struct nlmsghdr nlh;
270
struct tcmsg t;
271
} req = {
272
.nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg)),
273
.nlh.nlmsg_type = RTM_GETQDISC,
274
.nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
275
.t.tcm_family = AF_UNSPEC,
276
.t.tcm_ifindex = ifindex,
277
};
278
int seq = time(NULL);
279
280
req.nlh.nlmsg_seq = seq;
281
if (send(sock, &req, req.nlh.nlmsg_len, 0) < 0)
282
return -errno;
283
284
return netlink_recv(sock, nl_pid, seq, __dump_qdisc_nlmsg,
285
dump_qdisc_nlmsg, cookie);
286
}
287
288
static int __dump_filter_nlmsg(struct nlmsghdr *nlh,
289
dump_nlmsg_t dump_filter_nlmsg,
290
void *cookie)
291
{
292
struct nlattr *tb[TCA_MAX + 1], *attr;
293
struct tcmsg *t = NLMSG_DATA(nlh);
294
int len;
295
296
len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*t));
297
attr = (struct nlattr *) ((void *) t + NLMSG_ALIGN(sizeof(*t)));
298
if (libbpf_nla_parse(tb, TCA_MAX, attr, len, NULL) != 0)
299
return -LIBBPF_ERRNO__NLPARSE;
300
301
return dump_filter_nlmsg(cookie, t, tb);
302
}
303
304
static int netlink_get_filter(int sock, unsigned int nl_pid, int ifindex, int handle,
305
dump_nlmsg_t dump_filter_nlmsg, void *cookie)
306
{
307
struct {
308
struct nlmsghdr nlh;
309
struct tcmsg t;
310
} req = {
311
.nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg)),
312
.nlh.nlmsg_type = RTM_GETTFILTER,
313
.nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
314
.t.tcm_family = AF_UNSPEC,
315
.t.tcm_ifindex = ifindex,
316
.t.tcm_parent = handle,
317
};
318
int seq = time(NULL);
319
320
req.nlh.nlmsg_seq = seq;
321
if (send(sock, &req, req.nlh.nlmsg_len, 0) < 0)
322
return -errno;
323
324
return netlink_recv(sock, nl_pid, seq, __dump_filter_nlmsg,
325
dump_filter_nlmsg, cookie);
326
}
327
328
static int __dump_link_nlmsg(struct nlmsghdr *nlh,
329
dump_nlmsg_t dump_link_nlmsg, void *cookie)
330
{
331
struct nlattr *tb[IFLA_MAX + 1], *attr;
332
struct ifinfomsg *ifi = NLMSG_DATA(nlh);
333
int len;
334
335
len = nlh->nlmsg_len - NLMSG_LENGTH(sizeof(*ifi));
336
attr = (struct nlattr *) ((void *) ifi + NLMSG_ALIGN(sizeof(*ifi)));
337
if (libbpf_nla_parse(tb, IFLA_MAX, attr, len, NULL) != 0)
338
return -LIBBPF_ERRNO__NLPARSE;
339
340
return dump_link_nlmsg(cookie, ifi, tb);
341
}
342
343
static int netlink_get_link(int sock, unsigned int nl_pid,
344
dump_nlmsg_t dump_link_nlmsg, void *cookie)
345
{
346
struct {
347
struct nlmsghdr nlh;
348
struct ifinfomsg ifm;
349
} req = {
350
.nlh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
351
.nlh.nlmsg_type = RTM_GETLINK,
352
.nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
353
.ifm.ifi_family = AF_PACKET,
354
};
355
int seq = time(NULL);
356
357
req.nlh.nlmsg_seq = seq;
358
if (send(sock, &req, req.nlh.nlmsg_len, 0) < 0)
359
return -errno;
360
361
return netlink_recv(sock, nl_pid, seq, __dump_link_nlmsg,
362
dump_link_nlmsg, cookie);
363
}
364
365
static int dump_link_nlmsg(void *cookie, void *msg, struct nlattr **tb)
366
{
367
struct bpf_netdev_t *netinfo = cookie;
368
struct ifinfomsg *ifinfo = msg;
369
struct ip_devname_ifindex *tmp;
370
371
if (netinfo->filter_idx > 0 && netinfo->filter_idx != ifinfo->ifi_index)
372
return 0;
373
374
if (netinfo->used_len == netinfo->array_len) {
375
tmp = realloc(netinfo->devices,
376
(netinfo->array_len + 16) * sizeof(struct ip_devname_ifindex));
377
if (!tmp)
378
return -ENOMEM;
379
380
netinfo->devices = tmp;
381
netinfo->array_len += 16;
382
}
383
netinfo->devices[netinfo->used_len].ifindex = ifinfo->ifi_index;
384
snprintf(netinfo->devices[netinfo->used_len].devname,
385
sizeof(netinfo->devices[netinfo->used_len].devname),
386
"%s",
387
tb[IFLA_IFNAME]
388
? libbpf_nla_getattr_str(tb[IFLA_IFNAME])
389
: "");
390
netinfo->used_len++;
391
392
return do_xdp_dump(ifinfo, tb);
393
}
394
395
static int dump_class_qdisc_nlmsg(void *cookie, void *msg, struct nlattr **tb)
396
{
397
struct bpf_tcinfo_t *tcinfo = cookie;
398
struct tcmsg *info = msg;
399
struct tc_kind_handle *tmp;
400
401
if (tcinfo->is_qdisc) {
402
/* skip clsact qdisc */
403
if (tb[TCA_KIND] &&
404
strcmp(libbpf_nla_data(tb[TCA_KIND]), "clsact") == 0)
405
return 0;
406
if (info->tcm_handle == 0)
407
return 0;
408
}
409
410
if (tcinfo->used_len == tcinfo->array_len) {
411
tmp = realloc(tcinfo->handle_array,
412
(tcinfo->array_len + 16) * sizeof(struct tc_kind_handle));
413
if (!tmp)
414
return -ENOMEM;
415
416
tcinfo->handle_array = tmp;
417
tcinfo->array_len += 16;
418
}
419
tcinfo->handle_array[tcinfo->used_len].handle = info->tcm_handle;
420
snprintf(tcinfo->handle_array[tcinfo->used_len].kind,
421
sizeof(tcinfo->handle_array[tcinfo->used_len].kind),
422
"%s",
423
tb[TCA_KIND]
424
? libbpf_nla_getattr_str(tb[TCA_KIND])
425
: "unknown");
426
tcinfo->used_len++;
427
428
return 0;
429
}
430
431
static int dump_filter_nlmsg(void *cookie, void *msg, struct nlattr **tb)
432
{
433
const struct bpf_filter_t *filter_info = cookie;
434
435
return do_filter_dump((struct tcmsg *)msg, tb, filter_info->kind,
436
filter_info->devname, filter_info->ifindex);
437
}
438
439
static int __show_dev_tc_bpf_name(__u32 id, char *name, size_t len)
440
{
441
struct bpf_prog_info info = {};
442
__u32 ilen = sizeof(info);
443
int fd, ret;
444
445
fd = bpf_prog_get_fd_by_id(id);
446
if (fd < 0)
447
return fd;
448
ret = bpf_obj_get_info_by_fd(fd, &info, &ilen);
449
if (ret < 0)
450
goto out;
451
ret = -ENOENT;
452
if (info.name[0]) {
453
get_prog_full_name(&info, fd, name, len);
454
ret = 0;
455
}
456
out:
457
close(fd);
458
return ret;
459
}
460
461
static void __show_dev_tc_bpf(const struct ip_devname_ifindex *dev,
462
const enum bpf_attach_type loc)
463
{
464
__u32 prog_flags[64] = {}, link_flags[64] = {}, i, j;
465
__u32 prog_ids[64] = {}, link_ids[64] = {};
466
LIBBPF_OPTS(bpf_prog_query_opts, optq);
467
char prog_name[MAX_PROG_FULL_NAME];
468
int ret;
469
470
optq.prog_ids = prog_ids;
471
optq.prog_attach_flags = prog_flags;
472
optq.link_ids = link_ids;
473
optq.link_attach_flags = link_flags;
474
optq.count = ARRAY_SIZE(prog_ids);
475
476
ret = bpf_prog_query_opts(dev->ifindex, loc, &optq);
477
if (ret)
478
return;
479
for (i = 0; i < optq.count; i++) {
480
NET_START_OBJECT;
481
NET_DUMP_STR("devname", "%s", dev->devname);
482
NET_DUMP_UINT("ifindex", "(%u)", (unsigned int)dev->ifindex);
483
NET_DUMP_STR("kind", " %s", attach_loc_strings[loc]);
484
ret = __show_dev_tc_bpf_name(prog_ids[i], prog_name,
485
sizeof(prog_name));
486
if (!ret)
487
NET_DUMP_STR("name", " %s", prog_name);
488
NET_DUMP_UINT("prog_id", " prog_id %u ", prog_ids[i]);
489
if (prog_flags[i] || json_output) {
490
NET_START_ARRAY("prog_flags", "%s ");
491
for (j = 0; prog_flags[i] && j < 32; j++) {
492
if (!(prog_flags[i] & (1U << j)))
493
continue;
494
NET_DUMP_UINT_ONLY(1U << j);
495
}
496
NET_END_ARRAY("");
497
}
498
if (link_ids[i] || json_output) {
499
NET_DUMP_UINT("link_id", "link_id %u ", link_ids[i]);
500
if (link_flags[i] || json_output) {
501
NET_START_ARRAY("link_flags", "%s ");
502
for (j = 0; link_flags[i] && j < 32; j++) {
503
if (!(link_flags[i] & (1U << j)))
504
continue;
505
NET_DUMP_UINT_ONLY(1U << j);
506
}
507
NET_END_ARRAY("");
508
}
509
}
510
NET_END_OBJECT_FINAL;
511
}
512
}
513
514
static void show_dev_tc_bpf(struct ip_devname_ifindex *dev)
515
{
516
__show_dev_tc_bpf(dev, BPF_TCX_INGRESS);
517
__show_dev_tc_bpf(dev, BPF_TCX_EGRESS);
518
519
__show_dev_tc_bpf(dev, BPF_NETKIT_PRIMARY);
520
__show_dev_tc_bpf(dev, BPF_NETKIT_PEER);
521
}
522
523
static int show_dev_tc_bpf_classic(int sock, unsigned int nl_pid,
524
struct ip_devname_ifindex *dev)
525
{
526
struct bpf_filter_t filter_info;
527
struct bpf_tcinfo_t tcinfo;
528
int i, handle, ret = 0;
529
530
tcinfo.handle_array = NULL;
531
tcinfo.used_len = 0;
532
tcinfo.array_len = 0;
533
534
tcinfo.is_qdisc = false;
535
ret = netlink_get_class(sock, nl_pid, dev->ifindex,
536
dump_class_qdisc_nlmsg, &tcinfo);
537
if (ret)
538
goto out;
539
540
tcinfo.is_qdisc = true;
541
ret = netlink_get_qdisc(sock, nl_pid, dev->ifindex,
542
dump_class_qdisc_nlmsg, &tcinfo);
543
if (ret)
544
goto out;
545
546
filter_info.devname = dev->devname;
547
filter_info.ifindex = dev->ifindex;
548
for (i = 0; i < tcinfo.used_len; i++) {
549
filter_info.kind = tcinfo.handle_array[i].kind;
550
ret = netlink_get_filter(sock, nl_pid, dev->ifindex,
551
tcinfo.handle_array[i].handle,
552
dump_filter_nlmsg, &filter_info);
553
if (ret)
554
goto out;
555
}
556
557
/* root, ingress and egress handle */
558
handle = TC_H_ROOT;
559
filter_info.kind = "root";
560
ret = netlink_get_filter(sock, nl_pid, dev->ifindex, handle,
561
dump_filter_nlmsg, &filter_info);
562
if (ret)
563
goto out;
564
565
handle = TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_INGRESS);
566
filter_info.kind = "clsact/ingress";
567
ret = netlink_get_filter(sock, nl_pid, dev->ifindex, handle,
568
dump_filter_nlmsg, &filter_info);
569
if (ret)
570
goto out;
571
572
handle = TC_H_MAKE(TC_H_CLSACT, TC_H_MIN_EGRESS);
573
filter_info.kind = "clsact/egress";
574
ret = netlink_get_filter(sock, nl_pid, dev->ifindex, handle,
575
dump_filter_nlmsg, &filter_info);
576
if (ret)
577
goto out;
578
579
out:
580
free(tcinfo.handle_array);
581
return 0;
582
}
583
584
static int query_flow_dissector(struct bpf_attach_info *attach_info)
585
{
586
__u32 attach_flags;
587
__u32 prog_ids[1];
588
__u32 prog_cnt;
589
int err;
590
int fd;
591
592
fd = open("/proc/self/ns/net", O_RDONLY);
593
if (fd < 0) {
594
p_err("can't open /proc/self/ns/net: %s",
595
strerror(errno));
596
return -1;
597
}
598
prog_cnt = ARRAY_SIZE(prog_ids);
599
err = bpf_prog_query(fd, BPF_FLOW_DISSECTOR, 0,
600
&attach_flags, prog_ids, &prog_cnt);
601
close(fd);
602
if (err) {
603
if (errno == EINVAL) {
604
/* Older kernel's don't support querying
605
* flow dissector programs.
606
*/
607
errno = 0;
608
return 0;
609
}
610
p_err("can't query prog: %s", strerror(errno));
611
return -1;
612
}
613
614
if (prog_cnt == 1)
615
attach_info->flow_dissector_id = prog_ids[0];
616
617
return 0;
618
}
619
620
static int net_parse_dev(int *argc, char ***argv)
621
{
622
int ifindex;
623
624
if (is_prefix(**argv, "dev")) {
625
NEXT_ARGP();
626
627
ifindex = if_nametoindex(**argv);
628
if (!ifindex)
629
p_err("invalid devname %s", **argv);
630
631
NEXT_ARGP();
632
} else {
633
p_err("expected 'dev', got: '%s'?", **argv);
634
return -1;
635
}
636
637
return ifindex;
638
}
639
640
static int do_attach_detach_xdp(int progfd, enum net_attach_type attach_type,
641
int ifindex, bool overwrite)
642
{
643
__u32 flags = 0;
644
645
if (!overwrite)
646
flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
647
if (attach_type == NET_ATTACH_TYPE_XDP_GENERIC)
648
flags |= XDP_FLAGS_SKB_MODE;
649
if (attach_type == NET_ATTACH_TYPE_XDP_DRIVER)
650
flags |= XDP_FLAGS_DRV_MODE;
651
if (attach_type == NET_ATTACH_TYPE_XDP_OFFLOAD)
652
flags |= XDP_FLAGS_HW_MODE;
653
654
return bpf_xdp_attach(ifindex, progfd, flags, NULL);
655
}
656
657
static int get_tcx_type(enum net_attach_type attach_type)
658
{
659
switch (attach_type) {
660
case NET_ATTACH_TYPE_TCX_INGRESS:
661
return BPF_TCX_INGRESS;
662
case NET_ATTACH_TYPE_TCX_EGRESS:
663
return BPF_TCX_EGRESS;
664
default:
665
return -1;
666
}
667
}
668
669
static int do_attach_tcx(int progfd, enum net_attach_type attach_type, int ifindex)
670
{
671
int type = get_tcx_type(attach_type);
672
673
return bpf_prog_attach(progfd, ifindex, type, 0);
674
}
675
676
static int do_detach_tcx(int targetfd, enum net_attach_type attach_type)
677
{
678
int type = get_tcx_type(attach_type);
679
680
return bpf_prog_detach(targetfd, type);
681
}
682
683
static int do_attach(int argc, char **argv)
684
{
685
enum net_attach_type attach_type;
686
int progfd, ifindex, err = 0;
687
bool overwrite = false;
688
689
/* parse attach args */
690
if (!REQ_ARGS(5))
691
return -EINVAL;
692
693
attach_type = parse_attach_type(*argv);
694
if (attach_type == net_attach_type_size) {
695
p_err("invalid net attach/detach type: %s", *argv);
696
return -EINVAL;
697
}
698
NEXT_ARG();
699
700
progfd = prog_parse_fd(&argc, &argv);
701
if (progfd < 0)
702
return -EINVAL;
703
704
ifindex = net_parse_dev(&argc, &argv);
705
if (ifindex < 1) {
706
err = -EINVAL;
707
goto cleanup;
708
}
709
710
if (argc) {
711
if (is_prefix(*argv, "overwrite")) {
712
overwrite = true;
713
} else {
714
p_err("expected 'overwrite', got: '%s'?", *argv);
715
err = -EINVAL;
716
goto cleanup;
717
}
718
}
719
720
switch (attach_type) {
721
/* attach xdp prog */
722
case NET_ATTACH_TYPE_XDP:
723
case NET_ATTACH_TYPE_XDP_GENERIC:
724
case NET_ATTACH_TYPE_XDP_DRIVER:
725
case NET_ATTACH_TYPE_XDP_OFFLOAD:
726
err = do_attach_detach_xdp(progfd, attach_type, ifindex, overwrite);
727
break;
728
/* attach tcx prog */
729
case NET_ATTACH_TYPE_TCX_INGRESS:
730
case NET_ATTACH_TYPE_TCX_EGRESS:
731
err = do_attach_tcx(progfd, attach_type, ifindex);
732
break;
733
default:
734
break;
735
}
736
737
if (err) {
738
p_err("interface %s attach failed: %s",
739
attach_type_strings[attach_type], strerror(-err));
740
goto cleanup;
741
}
742
743
if (json_output)
744
jsonw_null(json_wtr);
745
cleanup:
746
close(progfd);
747
return err;
748
}
749
750
static int do_detach(int argc, char **argv)
751
{
752
enum net_attach_type attach_type;
753
int progfd, ifindex, err = 0;
754
755
/* parse detach args */
756
if (!REQ_ARGS(3))
757
return -EINVAL;
758
759
attach_type = parse_attach_type(*argv);
760
if (attach_type == net_attach_type_size) {
761
p_err("invalid net attach/detach type: %s", *argv);
762
return -EINVAL;
763
}
764
NEXT_ARG();
765
766
ifindex = net_parse_dev(&argc, &argv);
767
if (ifindex < 1)
768
return -EINVAL;
769
770
switch (attach_type) {
771
/* detach xdp prog */
772
case NET_ATTACH_TYPE_XDP:
773
case NET_ATTACH_TYPE_XDP_GENERIC:
774
case NET_ATTACH_TYPE_XDP_DRIVER:
775
case NET_ATTACH_TYPE_XDP_OFFLOAD:
776
progfd = -1;
777
err = do_attach_detach_xdp(progfd, attach_type, ifindex, NULL);
778
break;
779
/* detach tcx prog */
780
case NET_ATTACH_TYPE_TCX_INGRESS:
781
case NET_ATTACH_TYPE_TCX_EGRESS:
782
err = do_detach_tcx(ifindex, attach_type);
783
break;
784
default:
785
break;
786
}
787
788
if (err < 0) {
789
p_err("interface %s detach failed: %s",
790
attach_type_strings[attach_type], strerror(-err));
791
return err;
792
}
793
794
if (json_output)
795
jsonw_null(json_wtr);
796
797
return 0;
798
}
799
800
static int netfilter_link_compar(const void *a, const void *b)
801
{
802
const struct bpf_link_info *nfa = a;
803
const struct bpf_link_info *nfb = b;
804
int delta;
805
806
delta = nfa->netfilter.pf - nfb->netfilter.pf;
807
if (delta)
808
return delta;
809
810
delta = nfa->netfilter.hooknum - nfb->netfilter.hooknum;
811
if (delta)
812
return delta;
813
814
if (nfa->netfilter.priority < nfb->netfilter.priority)
815
return -1;
816
if (nfa->netfilter.priority > nfb->netfilter.priority)
817
return 1;
818
819
return nfa->netfilter.flags - nfb->netfilter.flags;
820
}
821
822
static void show_link_netfilter(void)
823
{
824
unsigned int nf_link_len = 0, nf_link_count = 0;
825
struct bpf_link_info *nf_link_info = NULL;
826
__u32 id = 0;
827
828
while (true) {
829
struct bpf_link_info info;
830
int fd, err;
831
__u32 len;
832
833
err = bpf_link_get_next_id(id, &id);
834
if (err) {
835
if (errno == ENOENT)
836
break;
837
p_err("can't get next link: %s (id %u)", strerror(errno), id);
838
break;
839
}
840
841
fd = bpf_link_get_fd_by_id(id);
842
if (fd < 0) {
843
p_err("can't get link by id (%u): %s", id, strerror(errno));
844
continue;
845
}
846
847
memset(&info, 0, sizeof(info));
848
len = sizeof(info);
849
850
err = bpf_link_get_info_by_fd(fd, &info, &len);
851
852
close(fd);
853
854
if (err) {
855
p_err("can't get link info for fd %d: %s", fd, strerror(errno));
856
continue;
857
}
858
859
if (info.type != BPF_LINK_TYPE_NETFILTER)
860
continue;
861
862
if (nf_link_count >= nf_link_len) {
863
static const unsigned int max_link_count = INT_MAX / sizeof(info);
864
struct bpf_link_info *expand;
865
866
if (nf_link_count > max_link_count) {
867
p_err("cannot handle more than %u links\n", max_link_count);
868
break;
869
}
870
871
nf_link_len += 16;
872
873
expand = realloc(nf_link_info, nf_link_len * sizeof(info));
874
if (!expand) {
875
p_err("realloc: %s", strerror(errno));
876
break;
877
}
878
879
nf_link_info = expand;
880
}
881
882
nf_link_info[nf_link_count] = info;
883
nf_link_count++;
884
}
885
886
if (!nf_link_info)
887
return;
888
889
qsort(nf_link_info, nf_link_count, sizeof(*nf_link_info), netfilter_link_compar);
890
891
for (id = 0; id < nf_link_count; id++) {
892
NET_START_OBJECT;
893
if (json_output)
894
netfilter_dump_json(&nf_link_info[id], json_wtr);
895
else
896
netfilter_dump_plain(&nf_link_info[id]);
897
898
NET_DUMP_UINT("id", " prog_id %u", nf_link_info[id].prog_id);
899
NET_END_OBJECT;
900
}
901
902
free(nf_link_info);
903
}
904
905
static int do_show(int argc, char **argv)
906
{
907
struct bpf_attach_info attach_info = {};
908
int i, sock, ret, filter_idx = -1;
909
struct bpf_netdev_t dev_array;
910
unsigned int nl_pid = 0;
911
char err_buf[256];
912
913
if (argc == 2) {
914
filter_idx = net_parse_dev(&argc, &argv);
915
if (filter_idx < 1)
916
return -1;
917
} else if (argc != 0) {
918
usage();
919
}
920
921
ret = query_flow_dissector(&attach_info);
922
if (ret)
923
return -1;
924
925
sock = netlink_open(&nl_pid);
926
if (sock < 0) {
927
fprintf(stderr, "failed to open netlink sock\n");
928
return -1;
929
}
930
931
dev_array.devices = NULL;
932
dev_array.used_len = 0;
933
dev_array.array_len = 0;
934
dev_array.filter_idx = filter_idx;
935
936
if (json_output)
937
jsonw_start_array(json_wtr);
938
NET_START_OBJECT;
939
NET_START_ARRAY("xdp", "%s:\n");
940
ret = netlink_get_link(sock, nl_pid, dump_link_nlmsg, &dev_array);
941
NET_END_ARRAY("\n");
942
943
if (!ret) {
944
NET_START_ARRAY("tc", "%s:\n");
945
for (i = 0; i < dev_array.used_len; i++) {
946
show_dev_tc_bpf(&dev_array.devices[i]);
947
ret = show_dev_tc_bpf_classic(sock, nl_pid,
948
&dev_array.devices[i]);
949
if (ret)
950
break;
951
}
952
NET_END_ARRAY("\n");
953
}
954
955
NET_START_ARRAY("flow_dissector", "%s:\n");
956
if (attach_info.flow_dissector_id > 0)
957
NET_DUMP_UINT("id", "id %u", attach_info.flow_dissector_id);
958
NET_END_ARRAY("\n");
959
960
NET_START_ARRAY("netfilter", "%s:\n");
961
show_link_netfilter();
962
NET_END_ARRAY("\n");
963
964
NET_END_OBJECT;
965
if (json_output)
966
jsonw_end_array(json_wtr);
967
968
if (ret) {
969
if (json_output)
970
jsonw_null(json_wtr);
971
libbpf_strerror(ret, err_buf, sizeof(err_buf));
972
fprintf(stderr, "Error: %s\n", err_buf);
973
}
974
free(dev_array.devices);
975
close(sock);
976
return ret;
977
}
978
979
static int do_help(int argc, char **argv)
980
{
981
if (json_output) {
982
jsonw_null(json_wtr);
983
return 0;
984
}
985
986
fprintf(stderr,
987
"Usage: %1$s %2$s { show | list } [dev <devname>]\n"
988
" %1$s %2$s attach ATTACH_TYPE PROG dev <devname> [ overwrite ]\n"
989
" %1$s %2$s detach ATTACH_TYPE dev <devname>\n"
990
" %1$s %2$s help\n"
991
"\n"
992
" " HELP_SPEC_PROGRAM "\n"
993
" ATTACH_TYPE := { xdp | xdpgeneric | xdpdrv | xdpoffload | tcx_ingress\n"
994
" | tcx_egress }\n"
995
" " HELP_SPEC_OPTIONS " }\n"
996
"\n"
997
"Note: Only xdp, tcx, tc, netkit, flow_dissector and netfilter attachments\n"
998
" are currently supported.\n"
999
" For progs attached to cgroups, use \"bpftool cgroup\"\n"
1000
" to dump program attachments. For program types\n"
1001
" sk_{filter,skb,msg,reuseport} and lwt/seg6, please\n"
1002
" consult iproute2.\n"
1003
"",
1004
bin_name, argv[-2]);
1005
1006
return 0;
1007
}
1008
1009
static const struct cmd cmds[] = {
1010
{ "show", do_show },
1011
{ "list", do_show },
1012
{ "attach", do_attach },
1013
{ "detach", do_detach },
1014
{ "help", do_help },
1015
{ 0 }
1016
};
1017
1018
int do_net(int argc, char **argv)
1019
{
1020
return cmd_select(cmds, argc, argv, do_help);
1021
}
1022
1023