Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 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 <libgen.h> |
| 38 | #include <stdbool.h> |
| 39 | #include <stdio.h> |
| 40 | #include <stdlib.h> |
| 41 | #include <string.h> |
| 42 | #include <unistd.h> |
| 43 | #include <linux/limits.h> |
| 44 | #include <linux/magic.h> |
Quentin Monnet | 3fc27b7 | 2017-10-24 20:11:28 -0700 | [diff] [blame] | 45 | #include <sys/mount.h> |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 46 | #include <sys/types.h> |
| 47 | #include <sys/vfs.h> |
| 48 | |
| 49 | #include <bpf.h> |
| 50 | |
| 51 | #include "main.h" |
| 52 | |
| 53 | static bool is_bpffs(char *path) |
| 54 | { |
| 55 | struct statfs st_fs; |
| 56 | |
| 57 | if (statfs(path, &st_fs) < 0) |
| 58 | return false; |
| 59 | |
| 60 | return (unsigned long)st_fs.f_type == BPF_FS_MAGIC; |
| 61 | } |
| 62 | |
Quentin Monnet | 3fc27b7 | 2017-10-24 20:11:28 -0700 | [diff] [blame] | 63 | static int mnt_bpffs(const char *target, char *buff, size_t bufflen) |
| 64 | { |
| 65 | bool bind_done = false; |
| 66 | |
| 67 | while (mount("", target, "none", MS_PRIVATE | MS_REC, NULL)) { |
| 68 | if (errno != EINVAL || bind_done) { |
| 69 | snprintf(buff, bufflen, |
| 70 | "mount --make-private %s failed: %s", |
| 71 | target, strerror(errno)); |
| 72 | return -1; |
| 73 | } |
| 74 | |
| 75 | if (mount(target, target, "none", MS_BIND, NULL)) { |
| 76 | snprintf(buff, bufflen, |
| 77 | "mount --bind %s %s failed: %s", |
| 78 | target, target, strerror(errno)); |
| 79 | return -1; |
| 80 | } |
| 81 | |
| 82 | bind_done = true; |
| 83 | } |
| 84 | |
| 85 | if (mount("bpf", target, "bpf", 0, "mode=0700")) { |
| 86 | snprintf(buff, bufflen, "mount -t bpf bpf %s failed: %s", |
| 87 | target, strerror(errno)); |
| 88 | return -1; |
| 89 | } |
| 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 94 | int open_obj_pinned_any(char *path, enum bpf_obj_type exp_type) |
| 95 | { |
| 96 | enum bpf_obj_type type; |
| 97 | int fd; |
| 98 | |
| 99 | fd = bpf_obj_get(path); |
| 100 | if (fd < 0) { |
Quentin Monnet | 9a5ab8b | 2017-10-23 09:24:13 -0700 | [diff] [blame] | 101 | p_err("bpf obj get (%s): %s", path, |
| 102 | errno == EACCES && !is_bpffs(dirname(path)) ? |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 103 | "directory not in bpf file system (bpffs)" : |
| 104 | strerror(errno)); |
| 105 | return -1; |
| 106 | } |
| 107 | |
| 108 | type = get_fd_type(fd); |
| 109 | if (type < 0) { |
| 110 | close(fd); |
| 111 | return type; |
| 112 | } |
| 113 | if (type != exp_type) { |
Quentin Monnet | 9a5ab8b | 2017-10-23 09:24:13 -0700 | [diff] [blame] | 114 | p_err("incorrect object type: %s", get_fd_type_name(type)); |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 115 | close(fd); |
| 116 | return -1; |
| 117 | } |
| 118 | |
| 119 | return fd; |
| 120 | } |
| 121 | |
| 122 | int do_pin_any(int argc, char **argv, int (*get_fd_by_id)(__u32)) |
| 123 | { |
Quentin Monnet | 3fc27b7 | 2017-10-24 20:11:28 -0700 | [diff] [blame] | 124 | char err_str[ERR_MAX_LEN]; |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 125 | unsigned int id; |
| 126 | char *endptr; |
Quentin Monnet | 3fc27b7 | 2017-10-24 20:11:28 -0700 | [diff] [blame] | 127 | char *file; |
| 128 | char *dir; |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 129 | int err; |
| 130 | int fd; |
| 131 | |
| 132 | if (!is_prefix(*argv, "id")) { |
Quentin Monnet | 9a5ab8b | 2017-10-23 09:24:13 -0700 | [diff] [blame] | 133 | p_err("expected 'id' got %s", *argv); |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 134 | return -1; |
| 135 | } |
| 136 | NEXT_ARG(); |
| 137 | |
| 138 | id = strtoul(*argv, &endptr, 0); |
| 139 | if (*endptr) { |
Quentin Monnet | 9a5ab8b | 2017-10-23 09:24:13 -0700 | [diff] [blame] | 140 | p_err("can't parse %s as ID", *argv); |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 141 | return -1; |
| 142 | } |
| 143 | NEXT_ARG(); |
| 144 | |
| 145 | if (argc != 1) |
| 146 | usage(); |
| 147 | |
| 148 | fd = get_fd_by_id(id); |
| 149 | if (fd < 0) { |
Quentin Monnet | 9a5ab8b | 2017-10-23 09:24:13 -0700 | [diff] [blame] | 150 | p_err("can't get prog by id (%u): %s", id, strerror(errno)); |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 151 | return -1; |
| 152 | } |
| 153 | |
| 154 | err = bpf_obj_pin(fd, *argv); |
Quentin Monnet | 3fc27b7 | 2017-10-24 20:11:28 -0700 | [diff] [blame] | 155 | if (!err) |
| 156 | goto out_close; |
| 157 | |
| 158 | file = malloc(strlen(*argv) + 1); |
| 159 | strcpy(file, *argv); |
| 160 | dir = dirname(file); |
| 161 | |
| 162 | if (errno != EPERM || is_bpffs(dir)) { |
| 163 | p_err("can't pin the object (%s): %s", *argv, strerror(errno)); |
| 164 | goto out_free; |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Quentin Monnet | 3fc27b7 | 2017-10-24 20:11:28 -0700 | [diff] [blame] | 167 | /* Attempt to mount bpffs, then retry pinning. */ |
| 168 | err = mnt_bpffs(dir, err_str, ERR_MAX_LEN); |
| 169 | if (!err) { |
| 170 | err = bpf_obj_pin(fd, *argv); |
| 171 | if (err) |
| 172 | p_err("can't pin the object (%s): %s", *argv, |
| 173 | strerror(errno)); |
| 174 | } else { |
| 175 | err_str[ERR_MAX_LEN - 1] = '\0'; |
| 176 | p_err("can't mount BPF file system to pin the object (%s): %s", |
| 177 | *argv, err_str); |
| 178 | } |
| 179 | |
| 180 | out_free: |
| 181 | free(file); |
| 182 | out_close: |
| 183 | close(fd); |
| 184 | return err; |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | const char *get_fd_type_name(enum bpf_obj_type type) |
| 188 | { |
| 189 | static const char * const names[] = { |
| 190 | [BPF_OBJ_UNKNOWN] = "unknown", |
| 191 | [BPF_OBJ_PROG] = "prog", |
| 192 | [BPF_OBJ_MAP] = "map", |
| 193 | }; |
| 194 | |
| 195 | if (type < 0 || type >= ARRAY_SIZE(names) || !names[type]) |
| 196 | return names[BPF_OBJ_UNKNOWN]; |
| 197 | |
| 198 | return names[type]; |
| 199 | } |
| 200 | |
| 201 | int get_fd_type(int fd) |
| 202 | { |
| 203 | char path[PATH_MAX]; |
| 204 | char buf[512]; |
| 205 | ssize_t n; |
| 206 | |
| 207 | snprintf(path, sizeof(path), "/proc/%d/fd/%d", getpid(), fd); |
| 208 | |
| 209 | n = readlink(path, buf, sizeof(buf)); |
| 210 | if (n < 0) { |
Quentin Monnet | 9a5ab8b | 2017-10-23 09:24:13 -0700 | [diff] [blame] | 211 | p_err("can't read link type: %s", strerror(errno)); |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 212 | return -1; |
| 213 | } |
| 214 | if (n == sizeof(path)) { |
Quentin Monnet | 9a5ab8b | 2017-10-23 09:24:13 -0700 | [diff] [blame] | 215 | p_err("can't read link type: path too long!"); |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 216 | return -1; |
| 217 | } |
| 218 | |
| 219 | if (strstr(buf, "bpf-map")) |
| 220 | return BPF_OBJ_MAP; |
| 221 | else if (strstr(buf, "bpf-prog")) |
| 222 | return BPF_OBJ_PROG; |
| 223 | |
| 224 | return BPF_OBJ_UNKNOWN; |
| 225 | } |
| 226 | |
| 227 | char *get_fdinfo(int fd, const char *key) |
| 228 | { |
| 229 | char path[PATH_MAX]; |
| 230 | char *line = NULL; |
| 231 | size_t line_n = 0; |
| 232 | ssize_t n; |
| 233 | FILE *fdi; |
| 234 | |
| 235 | snprintf(path, sizeof(path), "/proc/%d/fdinfo/%d", getpid(), fd); |
| 236 | |
| 237 | fdi = fopen(path, "r"); |
| 238 | if (!fdi) { |
Quentin Monnet | 9a5ab8b | 2017-10-23 09:24:13 -0700 | [diff] [blame] | 239 | p_err("can't open fdinfo: %s", strerror(errno)); |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 240 | return NULL; |
| 241 | } |
| 242 | |
| 243 | while ((n = getline(&line, &line_n, fdi))) { |
| 244 | char *value; |
| 245 | int len; |
| 246 | |
| 247 | if (!strstr(line, key)) |
| 248 | continue; |
| 249 | |
| 250 | fclose(fdi); |
| 251 | |
| 252 | value = strchr(line, '\t'); |
| 253 | if (!value || !value[1]) { |
Quentin Monnet | 9a5ab8b | 2017-10-23 09:24:13 -0700 | [diff] [blame] | 254 | p_err("malformed fdinfo!?"); |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 255 | free(line); |
| 256 | return NULL; |
| 257 | } |
| 258 | value++; |
| 259 | |
| 260 | len = strlen(value); |
| 261 | memmove(line, value, len); |
| 262 | line[len - 1] = '\0'; |
| 263 | |
| 264 | return line; |
| 265 | } |
| 266 | |
Quentin Monnet | 9a5ab8b | 2017-10-23 09:24:13 -0700 | [diff] [blame] | 267 | p_err("key '%s' not found in fdinfo", key); |
Jakub Kicinski | 71bb428 | 2017-10-04 20:10:04 -0700 | [diff] [blame] | 268 | free(line); |
| 269 | fclose(fdi); |
| 270 | return NULL; |
| 271 | } |
Quentin Monnet | f05e2c3 | 2017-10-23 09:24:10 -0700 | [diff] [blame] | 272 | |
| 273 | void print_hex_data_json(uint8_t *data, size_t len) |
| 274 | { |
| 275 | unsigned int i; |
| 276 | |
| 277 | jsonw_start_array(json_wtr); |
| 278 | for (i = 0; i < len; i++) |
| 279 | jsonw_printf(json_wtr, "\"0x%02hhx\"", data[i]); |
| 280 | jsonw_end_array(json_wtr); |
| 281 | } |