1 /* 2 * Copyright (C) 2017 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 /* Author: Jakub Kicinski <kubakici@wp.pl> */ 35 36 #include <errno.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 void p_err(const char *fmt, ...) 58 { 59 va_list ap; 60 61 va_start(ap, fmt); 62 if (json_output) { 63 jsonw_start_object(json_wtr); 64 jsonw_name(json_wtr, "error"); 65 jsonw_vprintf_enquote(json_wtr, fmt, ap); 66 jsonw_end_object(json_wtr); 67 } else { 68 fprintf(stderr, "Error: "); 69 vfprintf(stderr, fmt, ap); 70 fprintf(stderr, "\n"); 71 } 72 va_end(ap); 73 } 74 75 void p_info(const char *fmt, ...) 76 { 77 va_list ap; 78 79 if (json_output) 80 return; 81 82 va_start(ap, fmt); 83 vfprintf(stderr, fmt, ap); 84 fprintf(stderr, "\n"); 85 va_end(ap); 86 } 87 88 static bool is_bpffs(char *path) 89 { 90 struct statfs st_fs; 91 92 if (statfs(path, &st_fs) < 0) 93 return false; 94 95 return (unsigned long)st_fs.f_type == BPF_FS_MAGIC; 96 } 97 98 static int mnt_bpffs(const char *target, char *buff, size_t bufflen) 99 { 100 bool bind_done = false; 101 102 while (mount("", target, "none", MS_PRIVATE | MS_REC, NULL)) { 103 if (errno != EINVAL || bind_done) { 104 snprintf(buff, bufflen, 105 "mount --make-private %s failed: %s", 106 target, strerror(errno)); 107 return -1; 108 } 109 110 if (mount(target, target, "none", MS_BIND, NULL)) { 111 snprintf(buff, bufflen, 112 "mount --bind %s %s failed: %s", 113 target, target, strerror(errno)); 114 return -1; 115 } 116 117 bind_done = true; 118 } 119 120 if (mount("bpf", target, "bpf", 0, "mode=0700")) { 121 snprintf(buff, bufflen, "mount -t bpf bpf %s failed: %s", 122 target, strerror(errno)); 123 return -1; 124 } 125 126 return 0; 127 } 128 129 int open_obj_pinned(char *path) 130 { 131 int fd; 132 133 fd = bpf_obj_get(path); 134 if (fd < 0) { 135 p_err("bpf obj get (%s): %s", path, 136 errno == EACCES && !is_bpffs(dirname(path)) ? 137 "directory not in bpf file system (bpffs)" : 138 strerror(errno)); 139 return -1; 140 } 141 142 return fd; 143 } 144 145 int open_obj_pinned_any(char *path, enum bpf_obj_type exp_type) 146 { 147 enum bpf_obj_type type; 148 int fd; 149 150 fd = open_obj_pinned(path); 151 if (fd < 0) 152 return -1; 153 154 type = get_fd_type(fd); 155 if (type < 0) { 156 close(fd); 157 return type; 158 } 159 if (type != exp_type) { 160 p_err("incorrect object type: %s", get_fd_type_name(type)); 161 close(fd); 162 return -1; 163 } 164 165 return fd; 166 } 167 168 int do_pin_fd(int fd, const char *name) 169 { 170 char err_str[ERR_MAX_LEN]; 171 char *file; 172 char *dir; 173 int err = 0; 174 175 err = bpf_obj_pin(fd, name); 176 if (!err) 177 goto out; 178 179 file = malloc(strlen(name) + 1); 180 strcpy(file, name); 181 dir = dirname(file); 182 183 if (errno != EPERM || is_bpffs(dir)) { 184 p_err("can't pin the object (%s): %s", name, strerror(errno)); 185 goto out_free; 186 } 187 188 /* Attempt to mount bpffs, then retry pinning. */ 189 err = mnt_bpffs(dir, err_str, ERR_MAX_LEN); 190 if (!err) { 191 err = bpf_obj_pin(fd, name); 192 if (err) 193 p_err("can't pin the object (%s): %s", name, 194 strerror(errno)); 195 } else { 196 err_str[ERR_MAX_LEN - 1] = '\0'; 197 p_err("can't mount BPF file system to pin the object (%s): %s", 198 name, err_str); 199 } 200 201 out_free: 202 free(file); 203 out: 204 return err; 205 } 206 207 int do_pin_any(int argc, char **argv, int (*get_fd_by_id)(__u32)) 208 { 209 unsigned int id; 210 char *endptr; 211 int err; 212 int fd; 213 214 if (!is_prefix(*argv, "id")) { 215 p_err("expected 'id' got %s", *argv); 216 return -1; 217 } 218 NEXT_ARG(); 219 220 id = strtoul(*argv, &endptr, 0); 221 if (*endptr) { 222 p_err("can't parse %s as ID", *argv); 223 return -1; 224 } 225 NEXT_ARG(); 226 227 if (argc != 1) 228 usage(); 229 230 fd = get_fd_by_id(id); 231 if (fd < 0) { 232 p_err("can't get prog by id (%u): %s", id, strerror(errno)); 233 return -1; 234 } 235 236 err = do_pin_fd(fd, *argv); 237 238 close(fd); 239 return err; 240 } 241 242 const char *get_fd_type_name(enum bpf_obj_type type) 243 { 244 static const char * const names[] = { 245 [BPF_OBJ_UNKNOWN] = "unknown", 246 [BPF_OBJ_PROG] = "prog", 247 [BPF_OBJ_MAP] = "map", 248 }; 249 250 if (type < 0 || type >= ARRAY_SIZE(names) || !names[type]) 251 return names[BPF_OBJ_UNKNOWN]; 252 253 return names[type]; 254 } 255 256 int get_fd_type(int fd) 257 { 258 char path[PATH_MAX]; 259 char buf[512]; 260 ssize_t n; 261 262 snprintf(path, sizeof(path), "/proc/%d/fd/%d", getpid(), fd); 263 264 n = readlink(path, buf, sizeof(buf)); 265 if (n < 0) { 266 p_err("can't read link type: %s", strerror(errno)); 267 return -1; 268 } 269 if (n == sizeof(path)) { 270 p_err("can't read link type: path too long!"); 271 return -1; 272 } 273 274 if (strstr(buf, "bpf-map")) 275 return BPF_OBJ_MAP; 276 else if (strstr(buf, "bpf-prog")) 277 return BPF_OBJ_PROG; 278 279 return BPF_OBJ_UNKNOWN; 280 } 281 282 char *get_fdinfo(int fd, const char *key) 283 { 284 char path[PATH_MAX]; 285 char *line = NULL; 286 size_t line_n = 0; 287 ssize_t n; 288 FILE *fdi; 289 290 snprintf(path, sizeof(path), "/proc/%d/fdinfo/%d", getpid(), fd); 291 292 fdi = fopen(path, "r"); 293 if (!fdi) { 294 p_err("can't open fdinfo: %s", strerror(errno)); 295 return NULL; 296 } 297 298 while ((n = getline(&line, &line_n, fdi))) { 299 char *value; 300 int len; 301 302 if (!strstr(line, key)) 303 continue; 304 305 fclose(fdi); 306 307 value = strchr(line, '\t'); 308 if (!value || !value[1]) { 309 p_err("malformed fdinfo!?"); 310 free(line); 311 return NULL; 312 } 313 value++; 314 315 len = strlen(value); 316 memmove(line, value, len); 317 line[len - 1] = '\0'; 318 319 return line; 320 } 321 322 p_err("key '%s' not found in fdinfo", key); 323 free(line); 324 fclose(fdi); 325 return NULL; 326 } 327 328 void print_hex_data_json(uint8_t *data, size_t len) 329 { 330 unsigned int i; 331 332 jsonw_start_array(json_wtr); 333 for (i = 0; i < len; i++) 334 jsonw_printf(json_wtr, "\"0x%02hhx\"", data[i]); 335 jsonw_end_array(json_wtr); 336 } 337 338 int build_pinned_obj_table(struct pinned_obj_table *tab, 339 enum bpf_obj_type type) 340 { 341 struct bpf_prog_info pinned_info = {}; 342 struct pinned_obj *obj_node = NULL; 343 __u32 len = sizeof(pinned_info); 344 struct mntent *mntent = NULL; 345 enum bpf_obj_type objtype; 346 FILE *mntfile = NULL; 347 FTSENT *ftse = NULL; 348 FTS *fts = NULL; 349 int fd, err; 350 351 mntfile = setmntent("/proc/mounts", "r"); 352 if (!mntfile) 353 return -1; 354 355 while ((mntent = getmntent(mntfile))) { 356 char *path[] = { mntent->mnt_dir, NULL }; 357 358 if (strncmp(mntent->mnt_type, "bpf", 3) != 0) 359 continue; 360 361 fts = fts_open(path, 0, NULL); 362 if (!fts) 363 continue; 364 365 while ((ftse = fts_read(fts))) { 366 if (!(ftse->fts_info & FTS_F)) 367 continue; 368 fd = open_obj_pinned(ftse->fts_path); 369 if (fd < 0) 370 continue; 371 372 objtype = get_fd_type(fd); 373 if (objtype != type) { 374 close(fd); 375 continue; 376 } 377 memset(&pinned_info, 0, sizeof(pinned_info)); 378 err = bpf_obj_get_info_by_fd(fd, &pinned_info, &len); 379 if (err) { 380 close(fd); 381 continue; 382 } 383 384 obj_node = malloc(sizeof(*obj_node)); 385 if (!obj_node) { 386 close(fd); 387 fts_close(fts); 388 fclose(mntfile); 389 return -1; 390 } 391 392 memset(obj_node, 0, sizeof(*obj_node)); 393 obj_node->id = pinned_info.id; 394 obj_node->path = strdup(ftse->fts_path); 395 hash_add(tab->table, &obj_node->hash, obj_node->id); 396 397 close(fd); 398 } 399 fts_close(fts); 400 } 401 fclose(mntfile); 402 return 0; 403 } 404 405 void delete_pinned_obj_table(struct pinned_obj_table *tab) 406 { 407 struct pinned_obj *obj; 408 struct hlist_node *tmp; 409 unsigned int bkt; 410 411 hash_for_each_safe(tab->table, bkt, tmp, obj, hash) { 412 hash_del(&obj->hash); 413 free(obj->path); 414 free(obj); 415 } 416 } 417 418 static char * 419 ifindex_to_name_ns(__u32 ifindex, __u32 ns_dev, __u32 ns_ino, char *buf) 420 { 421 struct stat st; 422 int err; 423 424 err = stat("/proc/self/ns/net", &st); 425 if (err) { 426 p_err("Can't stat /proc/self: %s", strerror(errno)); 427 return NULL; 428 } 429 430 if (st.st_dev != ns_dev || st.st_ino != ns_ino) 431 return NULL; 432 433 return if_indextoname(ifindex, buf); 434 } 435 436 void print_dev_plain(__u32 ifindex, __u64 ns_dev, __u64 ns_inode) 437 { 438 char name[IF_NAMESIZE]; 439 440 if (!ifindex) 441 return; 442 443 printf(" dev "); 444 if (ifindex_to_name_ns(ifindex, ns_dev, ns_inode, name)) 445 printf("%s", name); 446 else 447 printf("ifindex %u ns_dev %llu ns_ino %llu", 448 ifindex, ns_dev, ns_inode); 449 } 450 451 void print_dev_json(__u32 ifindex, __u64 ns_dev, __u64 ns_inode) 452 { 453 char name[IF_NAMESIZE]; 454 455 if (!ifindex) 456 return; 457 458 jsonw_name(json_wtr, "dev"); 459 jsonw_start_object(json_wtr); 460 jsonw_uint_field(json_wtr, "ifindex", ifindex); 461 jsonw_uint_field(json_wtr, "ns_dev", ns_dev); 462 jsonw_uint_field(json_wtr, "ns_inode", ns_inode); 463 if (ifindex_to_name_ns(ifindex, ns_dev, ns_inode, name)) 464 jsonw_string_field(json_wtr, "ifname", name); 465 jsonw_end_object(json_wtr); 466 } 467