1 /*
2  * Copyright (C) 2017-2018 Netronome Systems, Inc.
3  *
4  * This software is dual licensed under the GNU General License Version 2,
5  * June 1991 as shown in the file COPYING in the top-level directory of this
6  * source tree or the BSD 2-Clause License provided below.  You have the
7  * option to license this software under the complete terms of either license.
8  *
9  * The BSD 2-Clause License:
10  *
11  *     Redistribution and use in source and binary forms, with or
12  *     without modification, are permitted provided that the following
13  *     conditions are met:
14  *
15  *      1. Redistributions of source code must retain the above
16  *         copyright notice, this list of conditions and the following
17  *         disclaimer.
18  *
19  *      2. Redistributions in binary form must reproduce the above
20  *         copyright notice, this list of conditions and the following
21  *         disclaimer in the documentation and/or other materials
22  *         provided with the distribution.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31  * SOFTWARE.
32  */
33 
34 #include <ctype.h>
35 #include <errno.h>
36 #include <fcntl.h>
37 #include <fts.h>
38 #include <libgen.h>
39 #include <mntent.h>
40 #include <stdbool.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45 #include <linux/limits.h>
46 #include <linux/magic.h>
47 #include <net/if.h>
48 #include <sys/mount.h>
49 #include <sys/stat.h>
50 #include <sys/types.h>
51 #include <sys/vfs.h>
52 
53 #include <bpf.h>
54 
55 #include "main.h"
56 
57 #ifndef BPF_FS_MAGIC
58 #define BPF_FS_MAGIC		0xcafe4a11
59 #endif
60 
p_err(const char * fmt,...)61 void p_err(const char *fmt, ...)
62 {
63 	va_list ap;
64 
65 	va_start(ap, fmt);
66 	if (json_output) {
67 		jsonw_start_object(json_wtr);
68 		jsonw_name(json_wtr, "error");
69 		jsonw_vprintf_enquote(json_wtr, fmt, ap);
70 		jsonw_end_object(json_wtr);
71 	} else {
72 		fprintf(stderr, "Error: ");
73 		vfprintf(stderr, fmt, ap);
74 		fprintf(stderr, "\n");
75 	}
76 	va_end(ap);
77 }
78 
p_info(const char * fmt,...)79 void p_info(const char *fmt, ...)
80 {
81 	va_list ap;
82 
83 	if (json_output)
84 		return;
85 
86 	va_start(ap, fmt);
87 	vfprintf(stderr, fmt, ap);
88 	fprintf(stderr, "\n");
89 	va_end(ap);
90 }
91 
is_bpffs(char * path)92 static bool is_bpffs(char *path)
93 {
94 	struct statfs st_fs;
95 
96 	if (statfs(path, &st_fs) < 0)
97 		return false;
98 
99 	return (unsigned long)st_fs.f_type == BPF_FS_MAGIC;
100 }
101 
mnt_bpffs(const char * target,char * buff,size_t bufflen)102 static int mnt_bpffs(const char *target, char *buff, size_t bufflen)
103 {
104 	bool bind_done = false;
105 
106 	while (mount("", target, "none", MS_PRIVATE | MS_REC, NULL)) {
107 		if (errno != EINVAL || bind_done) {
108 			snprintf(buff, bufflen,
109 				 "mount --make-private %s failed: %s",
110 				 target, strerror(errno));
111 			return -1;
112 		}
113 
114 		if (mount(target, target, "none", MS_BIND, NULL)) {
115 			snprintf(buff, bufflen,
116 				 "mount --bind %s %s failed: %s",
117 				 target, target, strerror(errno));
118 			return -1;
119 		}
120 
121 		bind_done = true;
122 	}
123 
124 	if (mount("bpf", target, "bpf", 0, "mode=0700")) {
125 		snprintf(buff, bufflen, "mount -t bpf bpf %s failed: %s",
126 			 target, strerror(errno));
127 		return -1;
128 	}
129 
130 	return 0;
131 }
132 
open_obj_pinned(char * path)133 int open_obj_pinned(char *path)
134 {
135 	int fd;
136 
137 	fd = bpf_obj_get(path);
138 	if (fd < 0) {
139 		p_err("bpf obj get (%s): %s", path,
140 		      errno == EACCES && !is_bpffs(dirname(path)) ?
141 		    "directory not in bpf file system (bpffs)" :
142 		    strerror(errno));
143 		return -1;
144 	}
145 
146 	return fd;
147 }
148 
open_obj_pinned_any(char * path,enum bpf_obj_type exp_type)149 int open_obj_pinned_any(char *path, enum bpf_obj_type exp_type)
150 {
151 	enum bpf_obj_type type;
152 	int fd;
153 
154 	fd = open_obj_pinned(path);
155 	if (fd < 0)
156 		return -1;
157 
158 	type = get_fd_type(fd);
159 	if (type < 0) {
160 		close(fd);
161 		return type;
162 	}
163 	if (type != exp_type) {
164 		p_err("incorrect object type: %s", get_fd_type_name(type));
165 		close(fd);
166 		return -1;
167 	}
168 
169 	return fd;
170 }
171 
do_pin_fd(int fd,const char * name)172 int do_pin_fd(int fd, const char *name)
173 {
174 	char err_str[ERR_MAX_LEN];
175 	char *file;
176 	char *dir;
177 	int err = 0;
178 
179 	err = bpf_obj_pin(fd, name);
180 	if (!err)
181 		goto out;
182 
183 	file = malloc(strlen(name) + 1);
184 	strcpy(file, name);
185 	dir = dirname(file);
186 
187 	if (errno != EPERM || is_bpffs(dir)) {
188 		p_err("can't pin the object (%s): %s", name, strerror(errno));
189 		goto out_free;
190 	}
191 
192 	/* Attempt to mount bpffs, then retry pinning. */
193 	err = mnt_bpffs(dir, err_str, ERR_MAX_LEN);
194 	if (!err) {
195 		err = bpf_obj_pin(fd, name);
196 		if (err)
197 			p_err("can't pin the object (%s): %s", name,
198 			      strerror(errno));
199 	} else {
200 		err_str[ERR_MAX_LEN - 1] = '\0';
201 		p_err("can't mount BPF file system to pin the object (%s): %s",
202 		      name, err_str);
203 	}
204 
205 out_free:
206 	free(file);
207 out:
208 	return err;
209 }
210 
do_pin_any(int argc,char ** argv,int (* get_fd_by_id)(__u32))211 int do_pin_any(int argc, char **argv, int (*get_fd_by_id)(__u32))
212 {
213 	unsigned int id;
214 	char *endptr;
215 	int err;
216 	int fd;
217 
218 	if (argc < 3) {
219 		p_err("too few arguments, id ID and FILE path is required");
220 		return -1;
221 	} else if (argc > 3) {
222 		p_err("too many arguments");
223 		return -1;
224 	}
225 
226 	if (!is_prefix(*argv, "id")) {
227 		p_err("expected 'id' got %s", *argv);
228 		return -1;
229 	}
230 	NEXT_ARG();
231 
232 	id = strtoul(*argv, &endptr, 0);
233 	if (*endptr) {
234 		p_err("can't parse %s as ID", *argv);
235 		return -1;
236 	}
237 	NEXT_ARG();
238 
239 	fd = get_fd_by_id(id);
240 	if (fd < 0) {
241 		p_err("can't get prog by id (%u): %s", id, strerror(errno));
242 		return -1;
243 	}
244 
245 	err = do_pin_fd(fd, *argv);
246 
247 	close(fd);
248 	return err;
249 }
250 
get_fd_type_name(enum bpf_obj_type type)251 const char *get_fd_type_name(enum bpf_obj_type type)
252 {
253 	static const char * const names[] = {
254 		[BPF_OBJ_UNKNOWN]	= "unknown",
255 		[BPF_OBJ_PROG]		= "prog",
256 		[BPF_OBJ_MAP]		= "map",
257 	};
258 
259 	if (type < 0 || type >= ARRAY_SIZE(names) || !names[type])
260 		return names[BPF_OBJ_UNKNOWN];
261 
262 	return names[type];
263 }
264 
get_fd_type(int fd)265 int get_fd_type(int fd)
266 {
267 	char path[PATH_MAX];
268 	char buf[512];
269 	ssize_t n;
270 
271 	snprintf(path, sizeof(path), "/proc/%d/fd/%d", getpid(), fd);
272 
273 	n = readlink(path, buf, sizeof(buf));
274 	if (n < 0) {
275 		p_err("can't read link type: %s", strerror(errno));
276 		return -1;
277 	}
278 	if (n == sizeof(path)) {
279 		p_err("can't read link type: path too long!");
280 		return -1;
281 	}
282 
283 	if (strstr(buf, "bpf-map"))
284 		return BPF_OBJ_MAP;
285 	else if (strstr(buf, "bpf-prog"))
286 		return BPF_OBJ_PROG;
287 
288 	return BPF_OBJ_UNKNOWN;
289 }
290 
get_fdinfo(int fd,const char * key)291 char *get_fdinfo(int fd, const char *key)
292 {
293 	char path[PATH_MAX];
294 	char *line = NULL;
295 	size_t line_n = 0;
296 	ssize_t n;
297 	FILE *fdi;
298 
299 	snprintf(path, sizeof(path), "/proc/%d/fdinfo/%d", getpid(), fd);
300 
301 	fdi = fopen(path, "r");
302 	if (!fdi) {
303 		p_err("can't open fdinfo: %s", strerror(errno));
304 		return NULL;
305 	}
306 
307 	while ((n = getline(&line, &line_n, fdi))) {
308 		char *value;
309 		int len;
310 
311 		if (!strstr(line, key))
312 			continue;
313 
314 		fclose(fdi);
315 
316 		value = strchr(line, '\t');
317 		if (!value || !value[1]) {
318 			p_err("malformed fdinfo!?");
319 			free(line);
320 			return NULL;
321 		}
322 		value++;
323 
324 		len = strlen(value);
325 		memmove(line, value, len);
326 		line[len - 1] = '\0';
327 
328 		return line;
329 	}
330 
331 	p_err("key '%s' not found in fdinfo", key);
332 	free(line);
333 	fclose(fdi);
334 	return NULL;
335 }
336 
print_data_json(uint8_t * data,size_t len)337 void print_data_json(uint8_t *data, size_t len)
338 {
339 	unsigned int i;
340 
341 	jsonw_start_array(json_wtr);
342 	for (i = 0; i < len; i++)
343 		jsonw_printf(json_wtr, "%d", data[i]);
344 	jsonw_end_array(json_wtr);
345 }
346 
print_hex_data_json(uint8_t * data,size_t len)347 void print_hex_data_json(uint8_t *data, size_t len)
348 {
349 	unsigned int i;
350 
351 	jsonw_start_array(json_wtr);
352 	for (i = 0; i < len; i++)
353 		jsonw_printf(json_wtr, "\"0x%02hhx\"", data[i]);
354 	jsonw_end_array(json_wtr);
355 }
356 
build_pinned_obj_table(struct pinned_obj_table * tab,enum bpf_obj_type type)357 int build_pinned_obj_table(struct pinned_obj_table *tab,
358 			   enum bpf_obj_type type)
359 {
360 	struct bpf_prog_info pinned_info = {};
361 	struct pinned_obj *obj_node = NULL;
362 	__u32 len = sizeof(pinned_info);
363 	struct mntent *mntent = NULL;
364 	enum bpf_obj_type objtype;
365 	FILE *mntfile = NULL;
366 	FTSENT *ftse = NULL;
367 	FTS *fts = NULL;
368 	int fd, err;
369 
370 	mntfile = setmntent("/proc/mounts", "r");
371 	if (!mntfile)
372 		return -1;
373 
374 	while ((mntent = getmntent(mntfile))) {
375 		char *path[] = { mntent->mnt_dir, NULL };
376 
377 		if (strncmp(mntent->mnt_type, "bpf", 3) != 0)
378 			continue;
379 
380 		fts = fts_open(path, 0, NULL);
381 		if (!fts)
382 			continue;
383 
384 		while ((ftse = fts_read(fts))) {
385 			if (!(ftse->fts_info & FTS_F))
386 				continue;
387 			fd = open_obj_pinned(ftse->fts_path);
388 			if (fd < 0)
389 				continue;
390 
391 			objtype = get_fd_type(fd);
392 			if (objtype != type) {
393 				close(fd);
394 				continue;
395 			}
396 			memset(&pinned_info, 0, sizeof(pinned_info));
397 			err = bpf_obj_get_info_by_fd(fd, &pinned_info, &len);
398 			if (err) {
399 				close(fd);
400 				continue;
401 			}
402 
403 			obj_node = malloc(sizeof(*obj_node));
404 			if (!obj_node) {
405 				close(fd);
406 				fts_close(fts);
407 				fclose(mntfile);
408 				return -1;
409 			}
410 
411 			memset(obj_node, 0, sizeof(*obj_node));
412 			obj_node->id = pinned_info.id;
413 			obj_node->path = strdup(ftse->fts_path);
414 			hash_add(tab->table, &obj_node->hash, obj_node->id);
415 
416 			close(fd);
417 		}
418 		fts_close(fts);
419 	}
420 	fclose(mntfile);
421 	return 0;
422 }
423 
delete_pinned_obj_table(struct pinned_obj_table * tab)424 void delete_pinned_obj_table(struct pinned_obj_table *tab)
425 {
426 	struct pinned_obj *obj;
427 	struct hlist_node *tmp;
428 	unsigned int bkt;
429 
430 	hash_for_each_safe(tab->table, bkt, tmp, obj, hash) {
431 		hash_del(&obj->hash);
432 		free(obj->path);
433 		free(obj);
434 	}
435 }
436 
get_page_size(void)437 unsigned int get_page_size(void)
438 {
439 	static int result;
440 
441 	if (!result)
442 		result = getpagesize();
443 	return result;
444 }
445 
get_possible_cpus(void)446 unsigned int get_possible_cpus(void)
447 {
448 	static unsigned int result;
449 	char buf[128];
450 	long int n;
451 	char *ptr;
452 	int fd;
453 
454 	if (result)
455 		return result;
456 
457 	fd = open("/sys/devices/system/cpu/possible", O_RDONLY);
458 	if (fd < 0) {
459 		p_err("can't open sysfs possible cpus");
460 		exit(-1);
461 	}
462 
463 	n = read(fd, buf, sizeof(buf));
464 	if (n < 2) {
465 		p_err("can't read sysfs possible cpus");
466 		exit(-1);
467 	}
468 	close(fd);
469 
470 	if (n == sizeof(buf)) {
471 		p_err("read sysfs possible cpus overflow");
472 		exit(-1);
473 	}
474 
475 	ptr = buf;
476 	n = 0;
477 	while (*ptr && *ptr != '\n') {
478 		unsigned int a, b;
479 
480 		if (sscanf(ptr, "%u-%u", &a, &b) == 2) {
481 			n += b - a + 1;
482 
483 			ptr = strchr(ptr, '-') + 1;
484 		} else if (sscanf(ptr, "%u", &a) == 1) {
485 			n++;
486 		} else {
487 			assert(0);
488 		}
489 
490 		while (isdigit(*ptr))
491 			ptr++;
492 		if (*ptr == ',')
493 			ptr++;
494 	}
495 
496 	result = n;
497 
498 	return result;
499 }
500 
501 static char *
ifindex_to_name_ns(__u32 ifindex,__u32 ns_dev,__u32 ns_ino,char * buf)502 ifindex_to_name_ns(__u32 ifindex, __u32 ns_dev, __u32 ns_ino, char *buf)
503 {
504 	struct stat st;
505 	int err;
506 
507 	err = stat("/proc/self/ns/net", &st);
508 	if (err) {
509 		p_err("Can't stat /proc/self: %s", strerror(errno));
510 		return NULL;
511 	}
512 
513 	if (st.st_dev != ns_dev || st.st_ino != ns_ino)
514 		return NULL;
515 
516 	return if_indextoname(ifindex, buf);
517 }
518 
read_sysfs_hex_int(char * path)519 static int read_sysfs_hex_int(char *path)
520 {
521 	char vendor_id_buf[8];
522 	int len;
523 	int fd;
524 
525 	fd = open(path, O_RDONLY);
526 	if (fd < 0) {
527 		p_err("Can't open %s: %s", path, strerror(errno));
528 		return -1;
529 	}
530 
531 	len = read(fd, vendor_id_buf, sizeof(vendor_id_buf));
532 	close(fd);
533 	if (len < 0) {
534 		p_err("Can't read %s: %s", path, strerror(errno));
535 		return -1;
536 	}
537 	if (len >= (int)sizeof(vendor_id_buf)) {
538 		p_err("Value in %s too long", path);
539 		return -1;
540 	}
541 
542 	vendor_id_buf[len] = 0;
543 
544 	return strtol(vendor_id_buf, NULL, 0);
545 }
546 
read_sysfs_netdev_hex_int(char * devname,const char * entry_name)547 static int read_sysfs_netdev_hex_int(char *devname, const char *entry_name)
548 {
549 	char full_path[64];
550 
551 	snprintf(full_path, sizeof(full_path), "/sys/class/net/%s/device/%s",
552 		 devname, entry_name);
553 
554 	return read_sysfs_hex_int(full_path);
555 }
556 
ifindex_to_bfd_name_ns(__u32 ifindex,__u64 ns_dev,__u64 ns_ino)557 const char *ifindex_to_bfd_name_ns(__u32 ifindex, __u64 ns_dev, __u64 ns_ino)
558 {
559 	char devname[IF_NAMESIZE];
560 	int vendor_id;
561 	int device_id;
562 
563 	if (!ifindex_to_name_ns(ifindex, ns_dev, ns_ino, devname)) {
564 		p_err("Can't get net device name for ifindex %d: %s", ifindex,
565 		      strerror(errno));
566 		return NULL;
567 	}
568 
569 	vendor_id = read_sysfs_netdev_hex_int(devname, "vendor");
570 	if (vendor_id < 0) {
571 		p_err("Can't get device vendor id for %s", devname);
572 		return NULL;
573 	}
574 
575 	switch (vendor_id) {
576 	case 0x19ee:
577 		device_id = read_sysfs_netdev_hex_int(devname, "device");
578 		if (device_id != 0x4000 &&
579 		    device_id != 0x6000 &&
580 		    device_id != 0x6003)
581 			p_info("Unknown NFP device ID, assuming it is NFP-6xxx arch");
582 		return "NFP-6xxx";
583 	default:
584 		p_err("Can't get bfd arch name for device vendor id 0x%04x",
585 		      vendor_id);
586 		return NULL;
587 	}
588 }
589 
print_dev_plain(__u32 ifindex,__u64 ns_dev,__u64 ns_inode)590 void print_dev_plain(__u32 ifindex, __u64 ns_dev, __u64 ns_inode)
591 {
592 	char name[IF_NAMESIZE];
593 
594 	if (!ifindex)
595 		return;
596 
597 	printf(" dev ");
598 	if (ifindex_to_name_ns(ifindex, ns_dev, ns_inode, name))
599 		printf("%s", name);
600 	else
601 		printf("ifindex %u ns_dev %llu ns_ino %llu",
602 		       ifindex, ns_dev, ns_inode);
603 }
604 
print_dev_json(__u32 ifindex,__u64 ns_dev,__u64 ns_inode)605 void print_dev_json(__u32 ifindex, __u64 ns_dev, __u64 ns_inode)
606 {
607 	char name[IF_NAMESIZE];
608 
609 	if (!ifindex)
610 		return;
611 
612 	jsonw_name(json_wtr, "dev");
613 	jsonw_start_object(json_wtr);
614 	jsonw_uint_field(json_wtr, "ifindex", ifindex);
615 	jsonw_uint_field(json_wtr, "ns_dev", ns_dev);
616 	jsonw_uint_field(json_wtr, "ns_inode", ns_inode);
617 	if (ifindex_to_name_ns(ifindex, ns_dev, ns_inode, name))
618 		jsonw_string_field(json_wtr, "ifname", name);
619 	jsonw_end_object(json_wtr);
620 }
621