1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2017-2018 Netronome Systems, Inc. */
3
4 #define _GNU_SOURCE
5 #include <ctype.h>
6 #include <errno.h>
7 #include <fcntl.h>
8 #include <ftw.h>
9 #include <libgen.h>
10 #include <mntent.h>
11 #include <stdbool.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <unistd.h>
16 #include <linux/limits.h>
17 #include <linux/magic.h>
18 #include <net/if.h>
19 #include <sys/mount.h>
20 #include <sys/resource.h>
21 #include <sys/stat.h>
22 #include <sys/vfs.h>
23
24 #include <bpf/bpf.h>
25 #include <bpf/libbpf.h> /* libbpf_num_possible_cpus */
26
27 #include "main.h"
28
29 #ifndef BPF_FS_MAGIC
30 #define BPF_FS_MAGIC 0xcafe4a11
31 #endif
32
33 const char * const attach_type_name[__MAX_BPF_ATTACH_TYPE] = {
34 [BPF_CGROUP_INET_INGRESS] = "ingress",
35 [BPF_CGROUP_INET_EGRESS] = "egress",
36 [BPF_CGROUP_INET_SOCK_CREATE] = "sock_create",
37 [BPF_CGROUP_INET_SOCK_RELEASE] = "sock_release",
38 [BPF_CGROUP_SOCK_OPS] = "sock_ops",
39 [BPF_CGROUP_DEVICE] = "device",
40 [BPF_CGROUP_INET4_BIND] = "bind4",
41 [BPF_CGROUP_INET6_BIND] = "bind6",
42 [BPF_CGROUP_INET4_CONNECT] = "connect4",
43 [BPF_CGROUP_INET6_CONNECT] = "connect6",
44 [BPF_CGROUP_INET4_POST_BIND] = "post_bind4",
45 [BPF_CGROUP_INET6_POST_BIND] = "post_bind6",
46 [BPF_CGROUP_INET4_GETPEERNAME] = "getpeername4",
47 [BPF_CGROUP_INET6_GETPEERNAME] = "getpeername6",
48 [BPF_CGROUP_INET4_GETSOCKNAME] = "getsockname4",
49 [BPF_CGROUP_INET6_GETSOCKNAME] = "getsockname6",
50 [BPF_CGROUP_UDP4_SENDMSG] = "sendmsg4",
51 [BPF_CGROUP_UDP6_SENDMSG] = "sendmsg6",
52 [BPF_CGROUP_SYSCTL] = "sysctl",
53 [BPF_CGROUP_UDP4_RECVMSG] = "recvmsg4",
54 [BPF_CGROUP_UDP6_RECVMSG] = "recvmsg6",
55 [BPF_CGROUP_GETSOCKOPT] = "getsockopt",
56 [BPF_CGROUP_SETSOCKOPT] = "setsockopt",
57
58 [BPF_SK_SKB_STREAM_PARSER] = "sk_skb_stream_parser",
59 [BPF_SK_SKB_STREAM_VERDICT] = "sk_skb_stream_verdict",
60 [BPF_SK_SKB_VERDICT] = "sk_skb_verdict",
61 [BPF_SK_MSG_VERDICT] = "sk_msg_verdict",
62 [BPF_LIRC_MODE2] = "lirc_mode2",
63 [BPF_FLOW_DISSECTOR] = "flow_dissector",
64 [BPF_TRACE_RAW_TP] = "raw_tp",
65 [BPF_TRACE_FENTRY] = "fentry",
66 [BPF_TRACE_FEXIT] = "fexit",
67 [BPF_MODIFY_RETURN] = "mod_ret",
68 [BPF_LSM_MAC] = "lsm_mac",
69 [BPF_SK_LOOKUP] = "sk_lookup",
70 [BPF_TRACE_ITER] = "trace_iter",
71 [BPF_XDP_DEVMAP] = "xdp_devmap",
72 [BPF_XDP_CPUMAP] = "xdp_cpumap",
73 [BPF_XDP] = "xdp",
74 [BPF_SK_REUSEPORT_SELECT] = "sk_skb_reuseport_select",
75 [BPF_SK_REUSEPORT_SELECT_OR_MIGRATE] = "sk_skb_reuseport_select_or_migrate",
76 };
77
p_err(const char * fmt,...)78 void p_err(const char *fmt, ...)
79 {
80 va_list ap;
81
82 va_start(ap, fmt);
83 if (json_output) {
84 jsonw_start_object(json_wtr);
85 jsonw_name(json_wtr, "error");
86 jsonw_vprintf_enquote(json_wtr, fmt, ap);
87 jsonw_end_object(json_wtr);
88 } else {
89 fprintf(stderr, "Error: ");
90 vfprintf(stderr, fmt, ap);
91 fprintf(stderr, "\n");
92 }
93 va_end(ap);
94 }
95
p_info(const char * fmt,...)96 void p_info(const char *fmt, ...)
97 {
98 va_list ap;
99
100 if (json_output)
101 return;
102
103 va_start(ap, fmt);
104 vfprintf(stderr, fmt, ap);
105 fprintf(stderr, "\n");
106 va_end(ap);
107 }
108
is_bpffs(char * path)109 static bool is_bpffs(char *path)
110 {
111 struct statfs st_fs;
112
113 if (statfs(path, &st_fs) < 0)
114 return false;
115
116 return (unsigned long)st_fs.f_type == BPF_FS_MAGIC;
117 }
118
set_max_rlimit(void)119 void set_max_rlimit(void)
120 {
121 struct rlimit rinf = { RLIM_INFINITY, RLIM_INFINITY };
122
123 setrlimit(RLIMIT_MEMLOCK, &rinf);
124 }
125
126 static int
mnt_fs(const char * target,const char * type,char * buff,size_t bufflen)127 mnt_fs(const char *target, const char *type, char *buff, size_t bufflen)
128 {
129 bool bind_done = false;
130
131 while (mount("", target, "none", MS_PRIVATE | MS_REC, NULL)) {
132 if (errno != EINVAL || bind_done) {
133 snprintf(buff, bufflen,
134 "mount --make-private %s failed: %s",
135 target, strerror(errno));
136 return -1;
137 }
138
139 if (mount(target, target, "none", MS_BIND, NULL)) {
140 snprintf(buff, bufflen,
141 "mount --bind %s %s failed: %s",
142 target, target, strerror(errno));
143 return -1;
144 }
145
146 bind_done = true;
147 }
148
149 if (mount(type, target, type, 0, "mode=0700")) {
150 snprintf(buff, bufflen, "mount -t %s %s %s failed: %s",
151 type, type, target, strerror(errno));
152 return -1;
153 }
154
155 return 0;
156 }
157
mount_tracefs(const char * target)158 int mount_tracefs(const char *target)
159 {
160 char err_str[ERR_MAX_LEN];
161 int err;
162
163 err = mnt_fs(target, "tracefs", err_str, ERR_MAX_LEN);
164 if (err) {
165 err_str[ERR_MAX_LEN - 1] = '\0';
166 p_err("can't mount tracefs: %s", err_str);
167 }
168
169 return err;
170 }
171
open_obj_pinned(const char * path,bool quiet)172 int open_obj_pinned(const char *path, bool quiet)
173 {
174 char *pname;
175 int fd = -1;
176
177 pname = strdup(path);
178 if (!pname) {
179 if (!quiet)
180 p_err("mem alloc failed");
181 goto out_ret;
182 }
183
184 fd = bpf_obj_get(pname);
185 if (fd < 0) {
186 if (!quiet)
187 p_err("bpf obj get (%s): %s", pname,
188 errno == EACCES && !is_bpffs(dirname(pname)) ?
189 "directory not in bpf file system (bpffs)" :
190 strerror(errno));
191 goto out_free;
192 }
193
194 out_free:
195 free(pname);
196 out_ret:
197 return fd;
198 }
199
open_obj_pinned_any(const char * path,enum bpf_obj_type exp_type)200 int open_obj_pinned_any(const char *path, enum bpf_obj_type exp_type)
201 {
202 enum bpf_obj_type type;
203 int fd;
204
205 fd = open_obj_pinned(path, false);
206 if (fd < 0)
207 return -1;
208
209 type = get_fd_type(fd);
210 if (type < 0) {
211 close(fd);
212 return type;
213 }
214 if (type != exp_type) {
215 p_err("incorrect object type: %s", get_fd_type_name(type));
216 close(fd);
217 return -1;
218 }
219
220 return fd;
221 }
222
mount_bpffs_for_pin(const char * name)223 int mount_bpffs_for_pin(const char *name)
224 {
225 char err_str[ERR_MAX_LEN];
226 char *file;
227 char *dir;
228 int err = 0;
229
230 file = malloc(strlen(name) + 1);
231 if (!file) {
232 p_err("mem alloc failed");
233 return -1;
234 }
235
236 strcpy(file, name);
237 dir = dirname(file);
238
239 if (is_bpffs(dir))
240 /* nothing to do if already mounted */
241 goto out_free;
242
243 if (block_mount) {
244 p_err("no BPF file system found, not mounting it due to --nomount option");
245 err = -1;
246 goto out_free;
247 }
248
249 err = mnt_fs(dir, "bpf", err_str, ERR_MAX_LEN);
250 if (err) {
251 err_str[ERR_MAX_LEN - 1] = '\0';
252 p_err("can't mount BPF file system to pin the object (%s): %s",
253 name, err_str);
254 }
255
256 out_free:
257 free(file);
258 return err;
259 }
260
do_pin_fd(int fd,const char * name)261 int do_pin_fd(int fd, const char *name)
262 {
263 int err;
264
265 err = mount_bpffs_for_pin(name);
266 if (err)
267 return err;
268
269 err = bpf_obj_pin(fd, name);
270 if (err)
271 p_err("can't pin the object (%s): %s", name, strerror(errno));
272
273 return err;
274 }
275
do_pin_any(int argc,char ** argv,int (* get_fd)(int *,char ***))276 int do_pin_any(int argc, char **argv, int (*get_fd)(int *, char ***))
277 {
278 int err;
279 int fd;
280
281 if (!REQ_ARGS(3))
282 return -EINVAL;
283
284 fd = get_fd(&argc, &argv);
285 if (fd < 0)
286 return fd;
287
288 err = do_pin_fd(fd, *argv);
289
290 close(fd);
291 return err;
292 }
293
get_fd_type_name(enum bpf_obj_type type)294 const char *get_fd_type_name(enum bpf_obj_type type)
295 {
296 static const char * const names[] = {
297 [BPF_OBJ_UNKNOWN] = "unknown",
298 [BPF_OBJ_PROG] = "prog",
299 [BPF_OBJ_MAP] = "map",
300 };
301
302 if (type < 0 || type >= ARRAY_SIZE(names) || !names[type])
303 return names[BPF_OBJ_UNKNOWN];
304
305 return names[type];
306 }
307
get_fd_type(int fd)308 int get_fd_type(int fd)
309 {
310 char path[PATH_MAX];
311 char buf[512];
312 ssize_t n;
313
314 snprintf(path, sizeof(path), "/proc/self/fd/%d", fd);
315
316 n = readlink(path, buf, sizeof(buf));
317 if (n < 0) {
318 p_err("can't read link type: %s", strerror(errno));
319 return -1;
320 }
321 if (n == sizeof(path)) {
322 p_err("can't read link type: path too long!");
323 return -1;
324 }
325
326 if (strstr(buf, "bpf-map"))
327 return BPF_OBJ_MAP;
328 else if (strstr(buf, "bpf-prog"))
329 return BPF_OBJ_PROG;
330 else if (strstr(buf, "bpf-link"))
331 return BPF_OBJ_LINK;
332
333 return BPF_OBJ_UNKNOWN;
334 }
335
get_fdinfo(int fd,const char * key)336 char *get_fdinfo(int fd, const char *key)
337 {
338 char path[PATH_MAX];
339 char *line = NULL;
340 size_t line_n = 0;
341 ssize_t n;
342 FILE *fdi;
343
344 snprintf(path, sizeof(path), "/proc/self/fdinfo/%d", fd);
345
346 fdi = fopen(path, "r");
347 if (!fdi)
348 return NULL;
349
350 while ((n = getline(&line, &line_n, fdi)) > 0) {
351 char *value;
352 int len;
353
354 if (!strstr(line, key))
355 continue;
356
357 fclose(fdi);
358
359 value = strchr(line, '\t');
360 if (!value || !value[1]) {
361 free(line);
362 return NULL;
363 }
364 value++;
365
366 len = strlen(value);
367 memmove(line, value, len);
368 line[len - 1] = '\0';
369
370 return line;
371 }
372
373 free(line);
374 fclose(fdi);
375 return NULL;
376 }
377
print_data_json(uint8_t * data,size_t len)378 void print_data_json(uint8_t *data, size_t len)
379 {
380 unsigned int i;
381
382 jsonw_start_array(json_wtr);
383 for (i = 0; i < len; i++)
384 jsonw_printf(json_wtr, "%d", data[i]);
385 jsonw_end_array(json_wtr);
386 }
387
print_hex_data_json(uint8_t * data,size_t len)388 void print_hex_data_json(uint8_t *data, size_t len)
389 {
390 unsigned int i;
391
392 jsonw_start_array(json_wtr);
393 for (i = 0; i < len; i++)
394 jsonw_printf(json_wtr, "\"0x%02hhx\"", data[i]);
395 jsonw_end_array(json_wtr);
396 }
397
398 /* extra params for nftw cb */
399 static struct pinned_obj_table *build_fn_table;
400 static enum bpf_obj_type build_fn_type;
401
do_build_table_cb(const char * fpath,const struct stat * sb,int typeflag,struct FTW * ftwbuf)402 static int do_build_table_cb(const char *fpath, const struct stat *sb,
403 int typeflag, struct FTW *ftwbuf)
404 {
405 struct bpf_prog_info pinned_info;
406 __u32 len = sizeof(pinned_info);
407 struct pinned_obj *obj_node;
408 enum bpf_obj_type objtype;
409 int fd, err = 0;
410
411 if (typeflag != FTW_F)
412 goto out_ret;
413
414 fd = open_obj_pinned(fpath, true);
415 if (fd < 0)
416 goto out_ret;
417
418 objtype = get_fd_type(fd);
419 if (objtype != build_fn_type)
420 goto out_close;
421
422 memset(&pinned_info, 0, sizeof(pinned_info));
423 if (bpf_obj_get_info_by_fd(fd, &pinned_info, &len))
424 goto out_close;
425
426 obj_node = calloc(1, sizeof(*obj_node));
427 if (!obj_node) {
428 err = -1;
429 goto out_close;
430 }
431
432 obj_node->id = pinned_info.id;
433 obj_node->path = strdup(fpath);
434 if (!obj_node->path) {
435 err = -1;
436 free(obj_node);
437 goto out_close;
438 }
439
440 hash_add(build_fn_table->table, &obj_node->hash, obj_node->id);
441 out_close:
442 close(fd);
443 out_ret:
444 return err;
445 }
446
build_pinned_obj_table(struct pinned_obj_table * tab,enum bpf_obj_type type)447 int build_pinned_obj_table(struct pinned_obj_table *tab,
448 enum bpf_obj_type type)
449 {
450 struct mntent *mntent = NULL;
451 FILE *mntfile = NULL;
452 int flags = FTW_PHYS;
453 int nopenfd = 16;
454 int err = 0;
455
456 mntfile = setmntent("/proc/mounts", "r");
457 if (!mntfile)
458 return -1;
459
460 build_fn_table = tab;
461 build_fn_type = type;
462
463 while ((mntent = getmntent(mntfile))) {
464 char *path = mntent->mnt_dir;
465
466 if (strncmp(mntent->mnt_type, "bpf", 3) != 0)
467 continue;
468 err = nftw(path, do_build_table_cb, nopenfd, flags);
469 if (err)
470 break;
471 }
472 fclose(mntfile);
473 return err;
474 }
475
delete_pinned_obj_table(struct pinned_obj_table * tab)476 void delete_pinned_obj_table(struct pinned_obj_table *tab)
477 {
478 struct pinned_obj *obj;
479 struct hlist_node *tmp;
480 unsigned int bkt;
481
482 hash_for_each_safe(tab->table, bkt, tmp, obj, hash) {
483 hash_del(&obj->hash);
484 free(obj->path);
485 free(obj);
486 }
487 }
488
get_page_size(void)489 unsigned int get_page_size(void)
490 {
491 static int result;
492
493 if (!result)
494 result = getpagesize();
495 return result;
496 }
497
get_possible_cpus(void)498 unsigned int get_possible_cpus(void)
499 {
500 int cpus = libbpf_num_possible_cpus();
501
502 if (cpus < 0) {
503 p_err("Can't get # of possible cpus: %s", strerror(-cpus));
504 exit(-1);
505 }
506 return cpus;
507 }
508
509 static char *
ifindex_to_name_ns(__u32 ifindex,__u32 ns_dev,__u32 ns_ino,char * buf)510 ifindex_to_name_ns(__u32 ifindex, __u32 ns_dev, __u32 ns_ino, char *buf)
511 {
512 struct stat st;
513 int err;
514
515 err = stat("/proc/self/ns/net", &st);
516 if (err) {
517 p_err("Can't stat /proc/self: %s", strerror(errno));
518 return NULL;
519 }
520
521 if (st.st_dev != ns_dev || st.st_ino != ns_ino)
522 return NULL;
523
524 return if_indextoname(ifindex, buf);
525 }
526
read_sysfs_hex_int(char * path)527 static int read_sysfs_hex_int(char *path)
528 {
529 char vendor_id_buf[8];
530 int len;
531 int fd;
532
533 fd = open(path, O_RDONLY);
534 if (fd < 0) {
535 p_err("Can't open %s: %s", path, strerror(errno));
536 return -1;
537 }
538
539 len = read(fd, vendor_id_buf, sizeof(vendor_id_buf));
540 close(fd);
541 if (len < 0) {
542 p_err("Can't read %s: %s", path, strerror(errno));
543 return -1;
544 }
545 if (len >= (int)sizeof(vendor_id_buf)) {
546 p_err("Value in %s too long", path);
547 return -1;
548 }
549
550 vendor_id_buf[len] = 0;
551
552 return strtol(vendor_id_buf, NULL, 0);
553 }
554
read_sysfs_netdev_hex_int(char * devname,const char * entry_name)555 static int read_sysfs_netdev_hex_int(char *devname, const char *entry_name)
556 {
557 char full_path[64];
558
559 snprintf(full_path, sizeof(full_path), "/sys/class/net/%s/device/%s",
560 devname, entry_name);
561
562 return read_sysfs_hex_int(full_path);
563 }
564
565 const char *
ifindex_to_bfd_params(__u32 ifindex,__u64 ns_dev,__u64 ns_ino,const char ** opt)566 ifindex_to_bfd_params(__u32 ifindex, __u64 ns_dev, __u64 ns_ino,
567 const char **opt)
568 {
569 char devname[IF_NAMESIZE];
570 int vendor_id;
571 int device_id;
572
573 if (!ifindex_to_name_ns(ifindex, ns_dev, ns_ino, devname)) {
574 p_err("Can't get net device name for ifindex %d: %s", ifindex,
575 strerror(errno));
576 return NULL;
577 }
578
579 vendor_id = read_sysfs_netdev_hex_int(devname, "vendor");
580 if (vendor_id < 0) {
581 p_err("Can't get device vendor id for %s", devname);
582 return NULL;
583 }
584
585 switch (vendor_id) {
586 case 0x19ee:
587 device_id = read_sysfs_netdev_hex_int(devname, "device");
588 if (device_id != 0x4000 &&
589 device_id != 0x6000 &&
590 device_id != 0x6003)
591 p_info("Unknown NFP device ID, assuming it is NFP-6xxx arch");
592 *opt = "ctx4";
593 return "NFP-6xxx";
594 default:
595 p_err("Can't get bfd arch name for device vendor id 0x%04x",
596 vendor_id);
597 return NULL;
598 }
599 }
600
print_dev_plain(__u32 ifindex,__u64 ns_dev,__u64 ns_inode)601 void print_dev_plain(__u32 ifindex, __u64 ns_dev, __u64 ns_inode)
602 {
603 char name[IF_NAMESIZE];
604
605 if (!ifindex)
606 return;
607
608 printf(" offloaded_to ");
609 if (ifindex_to_name_ns(ifindex, ns_dev, ns_inode, name))
610 printf("%s", name);
611 else
612 printf("ifindex %u ns_dev %llu ns_ino %llu",
613 ifindex, ns_dev, ns_inode);
614 }
615
print_dev_json(__u32 ifindex,__u64 ns_dev,__u64 ns_inode)616 void print_dev_json(__u32 ifindex, __u64 ns_dev, __u64 ns_inode)
617 {
618 char name[IF_NAMESIZE];
619
620 if (!ifindex)
621 return;
622
623 jsonw_name(json_wtr, "dev");
624 jsonw_start_object(json_wtr);
625 jsonw_uint_field(json_wtr, "ifindex", ifindex);
626 jsonw_uint_field(json_wtr, "ns_dev", ns_dev);
627 jsonw_uint_field(json_wtr, "ns_inode", ns_inode);
628 if (ifindex_to_name_ns(ifindex, ns_dev, ns_inode, name))
629 jsonw_string_field(json_wtr, "ifname", name);
630 jsonw_end_object(json_wtr);
631 }
632
parse_u32_arg(int * argc,char *** argv,__u32 * val,const char * what)633 int parse_u32_arg(int *argc, char ***argv, __u32 *val, const char *what)
634 {
635 char *endptr;
636
637 NEXT_ARGP();
638
639 if (*val) {
640 p_err("%s already specified", what);
641 return -1;
642 }
643
644 *val = strtoul(**argv, &endptr, 0);
645 if (*endptr) {
646 p_err("can't parse %s as %s", **argv, what);
647 return -1;
648 }
649 NEXT_ARGP();
650
651 return 0;
652 }
653
654 int __printf(2, 0)
print_all_levels(__maybe_unused enum libbpf_print_level level,const char * format,va_list args)655 print_all_levels(__maybe_unused enum libbpf_print_level level,
656 const char *format, va_list args)
657 {
658 return vfprintf(stderr, format, args);
659 }
660
prog_fd_by_nametag(void * nametag,int ** fds,bool tag)661 static int prog_fd_by_nametag(void *nametag, int **fds, bool tag)
662 {
663 unsigned int id = 0;
664 int fd, nb_fds = 0;
665 void *tmp;
666 int err;
667
668 while (true) {
669 struct bpf_prog_info info = {};
670 __u32 len = sizeof(info);
671
672 err = bpf_prog_get_next_id(id, &id);
673 if (err) {
674 if (errno != ENOENT) {
675 p_err("%s", strerror(errno));
676 goto err_close_fds;
677 }
678 return nb_fds;
679 }
680
681 fd = bpf_prog_get_fd_by_id(id);
682 if (fd < 0) {
683 p_err("can't get prog by id (%u): %s",
684 id, strerror(errno));
685 goto err_close_fds;
686 }
687
688 err = bpf_obj_get_info_by_fd(fd, &info, &len);
689 if (err) {
690 p_err("can't get prog info (%u): %s",
691 id, strerror(errno));
692 goto err_close_fd;
693 }
694
695 if ((tag && memcmp(nametag, info.tag, BPF_TAG_SIZE)) ||
696 (!tag && strncmp(nametag, info.name, BPF_OBJ_NAME_LEN))) {
697 close(fd);
698 continue;
699 }
700
701 if (nb_fds > 0) {
702 tmp = realloc(*fds, (nb_fds + 1) * sizeof(int));
703 if (!tmp) {
704 p_err("failed to realloc");
705 goto err_close_fd;
706 }
707 *fds = tmp;
708 }
709 (*fds)[nb_fds++] = fd;
710 }
711
712 err_close_fd:
713 close(fd);
714 err_close_fds:
715 while (--nb_fds >= 0)
716 close((*fds)[nb_fds]);
717 return -1;
718 }
719
prog_parse_fds(int * argc,char *** argv,int ** fds)720 int prog_parse_fds(int *argc, char ***argv, int **fds)
721 {
722 if (is_prefix(**argv, "id")) {
723 unsigned int id;
724 char *endptr;
725
726 NEXT_ARGP();
727
728 id = strtoul(**argv, &endptr, 0);
729 if (*endptr) {
730 p_err("can't parse %s as ID", **argv);
731 return -1;
732 }
733 NEXT_ARGP();
734
735 (*fds)[0] = bpf_prog_get_fd_by_id(id);
736 if ((*fds)[0] < 0) {
737 p_err("get by id (%u): %s", id, strerror(errno));
738 return -1;
739 }
740 return 1;
741 } else if (is_prefix(**argv, "tag")) {
742 unsigned char tag[BPF_TAG_SIZE];
743
744 NEXT_ARGP();
745
746 if (sscanf(**argv, BPF_TAG_FMT, tag, tag + 1, tag + 2,
747 tag + 3, tag + 4, tag + 5, tag + 6, tag + 7)
748 != BPF_TAG_SIZE) {
749 p_err("can't parse tag");
750 return -1;
751 }
752 NEXT_ARGP();
753
754 return prog_fd_by_nametag(tag, fds, true);
755 } else if (is_prefix(**argv, "name")) {
756 char *name;
757
758 NEXT_ARGP();
759
760 name = **argv;
761 if (strlen(name) > BPF_OBJ_NAME_LEN - 1) {
762 p_err("can't parse name");
763 return -1;
764 }
765 NEXT_ARGP();
766
767 return prog_fd_by_nametag(name, fds, false);
768 } else if (is_prefix(**argv, "pinned")) {
769 char *path;
770
771 NEXT_ARGP();
772
773 path = **argv;
774 NEXT_ARGP();
775
776 (*fds)[0] = open_obj_pinned_any(path, BPF_OBJ_PROG);
777 if ((*fds)[0] < 0)
778 return -1;
779 return 1;
780 }
781
782 p_err("expected 'id', 'tag', 'name' or 'pinned', got: '%s'?", **argv);
783 return -1;
784 }
785
prog_parse_fd(int * argc,char *** argv)786 int prog_parse_fd(int *argc, char ***argv)
787 {
788 int *fds = NULL;
789 int nb_fds, fd;
790
791 fds = malloc(sizeof(int));
792 if (!fds) {
793 p_err("mem alloc failed");
794 return -1;
795 }
796 nb_fds = prog_parse_fds(argc, argv, &fds);
797 if (nb_fds != 1) {
798 if (nb_fds > 1) {
799 p_err("several programs match this handle");
800 while (nb_fds--)
801 close(fds[nb_fds]);
802 }
803 fd = -1;
804 goto exit_free;
805 }
806
807 fd = fds[0];
808 exit_free:
809 free(fds);
810 return fd;
811 }
812
map_fd_by_name(char * name,int ** fds)813 static int map_fd_by_name(char *name, int **fds)
814 {
815 unsigned int id = 0;
816 int fd, nb_fds = 0;
817 void *tmp;
818 int err;
819
820 while (true) {
821 struct bpf_map_info info = {};
822 __u32 len = sizeof(info);
823
824 err = bpf_map_get_next_id(id, &id);
825 if (err) {
826 if (errno != ENOENT) {
827 p_err("%s", strerror(errno));
828 goto err_close_fds;
829 }
830 return nb_fds;
831 }
832
833 fd = bpf_map_get_fd_by_id(id);
834 if (fd < 0) {
835 p_err("can't get map by id (%u): %s",
836 id, strerror(errno));
837 goto err_close_fds;
838 }
839
840 err = bpf_obj_get_info_by_fd(fd, &info, &len);
841 if (err) {
842 p_err("can't get map info (%u): %s",
843 id, strerror(errno));
844 goto err_close_fd;
845 }
846
847 if (strncmp(name, info.name, BPF_OBJ_NAME_LEN)) {
848 close(fd);
849 continue;
850 }
851
852 if (nb_fds > 0) {
853 tmp = realloc(*fds, (nb_fds + 1) * sizeof(int));
854 if (!tmp) {
855 p_err("failed to realloc");
856 goto err_close_fd;
857 }
858 *fds = tmp;
859 }
860 (*fds)[nb_fds++] = fd;
861 }
862
863 err_close_fd:
864 close(fd);
865 err_close_fds:
866 while (--nb_fds >= 0)
867 close((*fds)[nb_fds]);
868 return -1;
869 }
870
map_parse_fds(int * argc,char *** argv,int ** fds)871 int map_parse_fds(int *argc, char ***argv, int **fds)
872 {
873 if (is_prefix(**argv, "id")) {
874 unsigned int id;
875 char *endptr;
876
877 NEXT_ARGP();
878
879 id = strtoul(**argv, &endptr, 0);
880 if (*endptr) {
881 p_err("can't parse %s as ID", **argv);
882 return -1;
883 }
884 NEXT_ARGP();
885
886 (*fds)[0] = bpf_map_get_fd_by_id(id);
887 if ((*fds)[0] < 0) {
888 p_err("get map by id (%u): %s", id, strerror(errno));
889 return -1;
890 }
891 return 1;
892 } else if (is_prefix(**argv, "name")) {
893 char *name;
894
895 NEXT_ARGP();
896
897 name = **argv;
898 if (strlen(name) > BPF_OBJ_NAME_LEN - 1) {
899 p_err("can't parse name");
900 return -1;
901 }
902 NEXT_ARGP();
903
904 return map_fd_by_name(name, fds);
905 } else if (is_prefix(**argv, "pinned")) {
906 char *path;
907
908 NEXT_ARGP();
909
910 path = **argv;
911 NEXT_ARGP();
912
913 (*fds)[0] = open_obj_pinned_any(path, BPF_OBJ_MAP);
914 if ((*fds)[0] < 0)
915 return -1;
916 return 1;
917 }
918
919 p_err("expected 'id', 'name' or 'pinned', got: '%s'?", **argv);
920 return -1;
921 }
922
map_parse_fd(int * argc,char *** argv)923 int map_parse_fd(int *argc, char ***argv)
924 {
925 int *fds = NULL;
926 int nb_fds, fd;
927
928 fds = malloc(sizeof(int));
929 if (!fds) {
930 p_err("mem alloc failed");
931 return -1;
932 }
933 nb_fds = map_parse_fds(argc, argv, &fds);
934 if (nb_fds != 1) {
935 if (nb_fds > 1) {
936 p_err("several maps match this handle");
937 while (nb_fds--)
938 close(fds[nb_fds]);
939 }
940 fd = -1;
941 goto exit_free;
942 }
943
944 fd = fds[0];
945 exit_free:
946 free(fds);
947 return fd;
948 }
949
map_parse_fd_and_info(int * argc,char *** argv,void * info,__u32 * info_len)950 int map_parse_fd_and_info(int *argc, char ***argv, void *info, __u32 *info_len)
951 {
952 int err;
953 int fd;
954
955 fd = map_parse_fd(argc, argv);
956 if (fd < 0)
957 return -1;
958
959 err = bpf_obj_get_info_by_fd(fd, info, info_len);
960 if (err) {
961 p_err("can't get map info: %s", strerror(errno));
962 close(fd);
963 return err;
964 }
965
966 return fd;
967 }
968