blob: c5ad528f046e038a3939180676d64c0a4363cd0e [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Alexei Starovoitov249b8122014-12-01 15:06:37 -08002#include <stdio.h>
3#include <sys/types.h>
4#include <sys/stat.h>
5#include <fcntl.h>
6#include <libelf.h>
7#include <gelf.h>
8#include <errno.h>
9#include <unistd.h>
10#include <string.h>
11#include <stdbool.h>
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070012#include <stdlib.h>
Alexei Starovoitov249b8122014-12-01 15:06:37 -080013#include <linux/bpf.h>
14#include <linux/filter.h>
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070015#include <linux/perf_event.h>
Martin KaFai Lau12d8bb62016-12-07 15:53:14 -080016#include <linux/netlink.h>
17#include <linux/rtnetlink.h>
Jesper Dangaard Brouer6387d012017-05-01 11:26:15 +020018#include <linux/types.h>
Martin KaFai Lau12d8bb62016-12-07 15:53:14 -080019#include <sys/socket.h>
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070020#include <sys/syscall.h>
21#include <sys/ioctl.h>
22#include <sys/mman.h>
23#include <poll.h>
Alexei Starovoitov5bacd782015-05-19 16:59:05 -070024#include <ctype.h>
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -070025#include <assert.h>
Jakub Kicinski2bf3e2e2018-05-14 22:35:02 -070026#include <bpf/bpf.h>
Alexei Starovoitov249b8122014-12-01 15:06:37 -080027#include "bpf_load.h"
Joe Stringer205c8ad2016-12-08 18:46:19 -080028#include "perf-sys.h"
Alexei Starovoitov249b8122014-12-01 15:06:37 -080029
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070030#define DEBUGFS "/sys/kernel/debug/tracing/"
31
Alexei Starovoitov249b8122014-12-01 15:06:37 -080032static char license[128];
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070033static int kern_version;
Alexei Starovoitov249b8122014-12-01 15:06:37 -080034static bool processed_sec[128];
Joe Stringerd40fc182016-12-14 14:43:38 -080035char bpf_log_buf[BPF_LOG_BUF_SIZE];
Alexei Starovoitov249b8122014-12-01 15:06:37 -080036int map_fd[MAX_MAPS];
37int prog_fd[MAX_PROGS];
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070038int event_fd[MAX_PROGS];
Alexei Starovoitov249b8122014-12-01 15:06:37 -080039int prog_cnt;
Alexei Starovoitov5bacd782015-05-19 16:59:05 -070040int prog_array_fd = -1;
41
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +020042struct bpf_map_data map_data[MAX_MAPS];
Daniel T. Lee37b54aed2019-05-23 16:24:48 +090043int map_data_count;
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +020044
Alexei Starovoitov5bacd782015-05-19 16:59:05 -070045static int populate_prog_array(const char *event, int prog_fd)
46{
47 int ind = atoi(event), err;
48
Joe Stringerd40fc182016-12-14 14:43:38 -080049 err = bpf_map_update_elem(prog_array_fd, &ind, &prog_fd, BPF_ANY);
Alexei Starovoitov5bacd782015-05-19 16:59:05 -070050 if (err < 0) {
51 printf("failed to store prog_fd in prog_array\n");
52 return -1;
53 }
54 return 0;
55}
Alexei Starovoitov249b8122014-12-01 15:06:37 -080056
Daniel T. Lee5a863812018-11-23 07:14:32 +090057static int write_kprobe_events(const char *val)
58{
59 int fd, ret, flags;
60
Daniel T. Leed59dd692018-12-03 19:39:30 +090061 if (val == NULL)
62 return -1;
63 else if (val[0] == '\0')
Daniel T. Lee5a863812018-11-23 07:14:32 +090064 flags = O_WRONLY | O_TRUNC;
65 else
66 flags = O_WRONLY | O_APPEND;
67
Daniel T. Lee37b54aed2019-05-23 16:24:48 +090068 fd = open(DEBUGFS "kprobe_events", flags);
Daniel T. Lee5a863812018-11-23 07:14:32 +090069
70 ret = write(fd, val, strlen(val));
71 close(fd);
72
73 return ret;
74}
75
Alexei Starovoitov249b8122014-12-01 15:06:37 -080076static int load_and_attach(const char *event, struct bpf_insn *prog, int size)
77{
Alexei Starovoitov249b8122014-12-01 15:06:37 -080078 bool is_socket = strncmp(event, "socket", 6) == 0;
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070079 bool is_kprobe = strncmp(event, "kprobe/", 7) == 0;
80 bool is_kretprobe = strncmp(event, "kretprobe/", 10) == 0;
Alexei Starovoitovc0766042016-04-06 18:43:29 -070081 bool is_tracepoint = strncmp(event, "tracepoint/", 11) == 0;
Alexei Starovoitov4662a4e2018-03-28 12:05:39 -070082 bool is_raw_tracepoint = strncmp(event, "raw_tracepoint/", 15) == 0;
Brenden Blanco86af8b42016-07-19 12:16:51 -070083 bool is_xdp = strncmp(event, "xdp", 3) == 0;
Alexei Starovoitov1c47910e2016-09-01 18:37:25 -070084 bool is_perf_event = strncmp(event, "perf_event", 10) == 0;
David Ahern4f2e7ae2016-12-01 08:48:07 -080085 bool is_cgroup_skb = strncmp(event, "cgroup/skb", 10) == 0;
86 bool is_cgroup_sk = strncmp(event, "cgroup/sock", 11) == 0;
Lawrence Brakmo40304b22017-06-30 20:02:40 -070087 bool is_sockops = strncmp(event, "sockops", 7) == 0;
John Fastabend69e8cc12017-08-15 22:33:32 -070088 bool is_sk_skb = strncmp(event, "sk_skb", 6) == 0;
John Fastabend4c4c3c22018-03-18 12:57:41 -070089 bool is_sk_msg = strncmp(event, "sk_msg", 6) == 0;
Joe Stringer43371c82016-12-14 14:43:39 -080090 size_t insns_cnt = size / sizeof(struct bpf_insn);
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070091 enum bpf_prog_type prog_type;
92 char buf[256];
93 int fd, efd, err, id;
94 struct perf_event_attr attr = {};
Alexei Starovoitov249b8122014-12-01 15:06:37 -080095
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -070096 attr.type = PERF_TYPE_TRACEPOINT;
97 attr.sample_type = PERF_SAMPLE_RAW;
98 attr.sample_period = 1;
99 attr.wakeup_events = 1;
100
101 if (is_socket) {
102 prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
103 } else if (is_kprobe || is_kretprobe) {
104 prog_type = BPF_PROG_TYPE_KPROBE;
Alexei Starovoitovc0766042016-04-06 18:43:29 -0700105 } else if (is_tracepoint) {
106 prog_type = BPF_PROG_TYPE_TRACEPOINT;
Alexei Starovoitov4662a4e2018-03-28 12:05:39 -0700107 } else if (is_raw_tracepoint) {
108 prog_type = BPF_PROG_TYPE_RAW_TRACEPOINT;
Brenden Blanco86af8b42016-07-19 12:16:51 -0700109 } else if (is_xdp) {
110 prog_type = BPF_PROG_TYPE_XDP;
Alexei Starovoitov1c47910e2016-09-01 18:37:25 -0700111 } else if (is_perf_event) {
112 prog_type = BPF_PROG_TYPE_PERF_EVENT;
David Ahern4f2e7ae2016-12-01 08:48:07 -0800113 } else if (is_cgroup_skb) {
114 prog_type = BPF_PROG_TYPE_CGROUP_SKB;
115 } else if (is_cgroup_sk) {
116 prog_type = BPF_PROG_TYPE_CGROUP_SOCK;
Lawrence Brakmo40304b22017-06-30 20:02:40 -0700117 } else if (is_sockops) {
118 prog_type = BPF_PROG_TYPE_SOCK_OPS;
John Fastabend69e8cc12017-08-15 22:33:32 -0700119 } else if (is_sk_skb) {
120 prog_type = BPF_PROG_TYPE_SK_SKB;
John Fastabend4c4c3c22018-03-18 12:57:41 -0700121 } else if (is_sk_msg) {
122 prog_type = BPF_PROG_TYPE_SK_MSG;
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700123 } else {
124 printf("Unknown event '%s'\n", event);
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800125 return -1;
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700126 }
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800127
Dan Carpenterb0294bc2018-07-13 18:11:39 +0300128 if (prog_cnt == MAX_PROGS)
129 return -1;
130
Joe Stringer43371c82016-12-14 14:43:39 -0800131 fd = bpf_load_program(prog_type, prog, insns_cnt, license, kern_version,
Joe Stringerd40fc182016-12-14 14:43:38 -0800132 bpf_log_buf, BPF_LOG_BUF_SIZE);
Alexei Starovoitov5bacd782015-05-19 16:59:05 -0700133 if (fd < 0) {
Joe Stringerd40fc182016-12-14 14:43:38 -0800134 printf("bpf_load_program() err=%d\n%s", errno, bpf_log_buf);
Alexei Starovoitov5bacd782015-05-19 16:59:05 -0700135 return -1;
136 }
137
138 prog_fd[prog_cnt++] = fd;
139
David Ahern4f2e7ae2016-12-01 08:48:07 -0800140 if (is_xdp || is_perf_event || is_cgroup_skb || is_cgroup_sk)
Brenden Blanco86af8b42016-07-19 12:16:51 -0700141 return 0;
142
John Fastabend4c4c3c22018-03-18 12:57:41 -0700143 if (is_socket || is_sockops || is_sk_skb || is_sk_msg) {
Lawrence Brakmo40304b22017-06-30 20:02:40 -0700144 if (is_socket)
145 event += 6;
146 else
147 event += 7;
Alexei Starovoitov5bacd782015-05-19 16:59:05 -0700148 if (*event != '/')
149 return 0;
150 event++;
151 if (!isdigit(*event)) {
152 printf("invalid prog number\n");
153 return -1;
154 }
155 return populate_prog_array(event, fd);
156 }
157
Alexei Starovoitov4662a4e2018-03-28 12:05:39 -0700158 if (is_raw_tracepoint) {
159 efd = bpf_raw_tracepoint_open(event + 15, fd);
160 if (efd < 0) {
161 printf("tracepoint %s %s\n", event + 15, strerror(errno));
162 return -1;
163 }
164 event_fd[prog_cnt - 1] = efd;
165 return 0;
166 }
167
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700168 if (is_kprobe || is_kretprobe) {
Yonghong Song34745ae2018-04-29 19:27:48 -0700169 bool need_normal_check = true;
170 const char *event_prefix = "";
171
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700172 if (is_kprobe)
173 event += 7;
174 else
175 event += 10;
176
Alexei Starovoitov5bacd782015-05-19 16:59:05 -0700177 if (*event == 0) {
178 printf("event name cannot be empty\n");
179 return -1;
180 }
181
182 if (isdigit(*event))
183 return populate_prog_array(event, fd);
184
Yonghong Song34745ae2018-04-29 19:27:48 -0700185#ifdef __x86_64__
186 if (strncmp(event, "sys_", 4) == 0) {
Daniel T. Lee5a863812018-11-23 07:14:32 +0900187 snprintf(buf, sizeof(buf), "%c:__x64_%s __x64_%s",
188 is_kprobe ? 'p' : 'r', event, event);
189 err = write_kprobe_events(buf);
Yonghong Song34745ae2018-04-29 19:27:48 -0700190 if (err >= 0) {
191 need_normal_check = false;
192 event_prefix = "__x64_";
193 }
194 }
195#endif
196 if (need_normal_check) {
Daniel T. Lee5a863812018-11-23 07:14:32 +0900197 snprintf(buf, sizeof(buf), "%c:%s %s",
198 is_kprobe ? 'p' : 'r', event, event);
199 err = write_kprobe_events(buf);
Yonghong Song34745ae2018-04-29 19:27:48 -0700200 if (err < 0) {
201 printf("failed to create kprobe '%s' error '%s'\n",
202 event, strerror(errno));
203 return -1;
204 }
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700205 }
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700206
Alexei Starovoitovc0766042016-04-06 18:43:29 -0700207 strcpy(buf, DEBUGFS);
208 strcat(buf, "events/kprobes/");
Yonghong Song34745ae2018-04-29 19:27:48 -0700209 strcat(buf, event_prefix);
Alexei Starovoitovc0766042016-04-06 18:43:29 -0700210 strcat(buf, event);
211 strcat(buf, "/id");
212 } else if (is_tracepoint) {
213 event += 11;
214
215 if (*event == 0) {
216 printf("event name cannot be empty\n");
217 return -1;
218 }
219 strcpy(buf, DEBUGFS);
220 strcat(buf, "events/");
221 strcat(buf, event);
222 strcat(buf, "/id");
223 }
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700224
225 efd = open(buf, O_RDONLY, 0);
226 if (efd < 0) {
227 printf("failed to open event %s\n", event);
228 return -1;
229 }
230
231 err = read(efd, buf, sizeof(buf));
232 if (err < 0 || err >= sizeof(buf)) {
233 printf("read from '%s' failed '%s'\n", event, strerror(errno));
234 return -1;
235 }
236
237 close(efd);
238
239 buf[err] = 0;
240 id = atoi(buf);
241 attr.config = id;
242
Joe Stringer205c8ad2016-12-08 18:46:19 -0800243 efd = sys_perf_event_open(&attr, -1/*pid*/, 0/*cpu*/, -1/*group_fd*/, 0);
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700244 if (efd < 0) {
245 printf("event %d fd %d err %s\n", id, efd, strerror(errno));
246 return -1;
247 }
248 event_fd[prog_cnt - 1] = efd;
Yonghong Song0ec95522017-11-30 13:47:55 -0800249 err = ioctl(efd, PERF_EVENT_IOC_ENABLE, 0);
250 if (err < 0) {
251 printf("ioctl PERF_EVENT_IOC_ENABLE failed err %s\n",
252 strerror(errno));
253 return -1;
254 }
255 err = ioctl(efd, PERF_EVENT_IOC_SET_BPF, fd);
256 if (err < 0) {
257 printf("ioctl PERF_EVENT_IOC_SET_BPF failed err %s\n",
258 strerror(errno));
259 return -1;
260 }
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700261
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800262 return 0;
263}
264
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200265static int load_maps(struct bpf_map_data *maps, int nr_maps,
266 fixup_map_cb fixup_map)
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800267{
Martin KaFai Lauad17d0e2017-08-18 11:28:01 -0700268 int i, numa_node;
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200269
Jesper Dangaard Brouer5010e942017-04-28 16:25:04 +0200270 for (i = 0; i < nr_maps; i++) {
Jesper Dangaard Brouer6979bcc2017-05-02 14:32:01 +0200271 if (fixup_map) {
272 fixup_map(&maps[i], i);
273 /* Allow userspace to assign map FD prior to creation */
274 if (maps[i].fd != -1) {
275 map_fd[i] = maps[i].fd;
276 continue;
277 }
278 }
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800279
Martin KaFai Lauad17d0e2017-08-18 11:28:01 -0700280 numa_node = maps[i].def.map_flags & BPF_F_NUMA_NODE ?
281 maps[i].def.numa_node : -1;
282
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200283 if (maps[i].def.type == BPF_MAP_TYPE_ARRAY_OF_MAPS ||
284 maps[i].def.type == BPF_MAP_TYPE_HASH_OF_MAPS) {
285 int inner_map_fd = map_fd[maps[i].def.inner_map_idx];
Martin KaFai Laufb30d4b2017-03-22 10:00:35 -0700286
Martin KaFai Lauad17d0e2017-08-18 11:28:01 -0700287 map_fd[i] = bpf_create_map_in_map_node(maps[i].def.type,
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700288 maps[i].name,
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200289 maps[i].def.key_size,
290 inner_map_fd,
291 maps[i].def.max_entries,
Martin KaFai Lauad17d0e2017-08-18 11:28:01 -0700292 maps[i].def.map_flags,
293 numa_node);
Martin KaFai Laufb30d4b2017-03-22 10:00:35 -0700294 } else {
Martin KaFai Lauad17d0e2017-08-18 11:28:01 -0700295 map_fd[i] = bpf_create_map_node(maps[i].def.type,
Martin KaFai Lau88cda1c2017-09-27 14:37:54 -0700296 maps[i].name,
Martin KaFai Lauad17d0e2017-08-18 11:28:01 -0700297 maps[i].def.key_size,
298 maps[i].def.value_size,
299 maps[i].def.max_entries,
300 maps[i].def.map_flags,
301 numa_node);
Martin KaFai Laufb30d4b2017-03-22 10:00:35 -0700302 }
Alexei Starovoitov618ec9a72016-03-07 21:57:18 -0800303 if (map_fd[i] < 0) {
Shannon Nelsonbce6a142018-10-29 14:14:41 -0700304 printf("failed to create map %d (%s): %d %s\n",
305 i, maps[i].name, errno, strerror(errno));
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800306 return 1;
Alexei Starovoitov618ec9a72016-03-07 21:57:18 -0800307 }
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200308 maps[i].fd = map_fd[i];
Alexei Starovoitov5bacd782015-05-19 16:59:05 -0700309
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200310 if (maps[i].def.type == BPF_MAP_TYPE_PROG_ARRAY)
Alexei Starovoitov5bacd782015-05-19 16:59:05 -0700311 prog_array_fd = map_fd[i];
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800312 }
313 return 0;
314}
315
316static int get_sec(Elf *elf, int i, GElf_Ehdr *ehdr, char **shname,
317 GElf_Shdr *shdr, Elf_Data **data)
318{
319 Elf_Scn *scn;
320
321 scn = elf_getscn(elf, i);
322 if (!scn)
323 return 1;
324
325 if (gelf_getshdr(scn, shdr) != shdr)
326 return 2;
327
328 *shname = elf_strptr(elf, ehdr->e_shstrndx, shdr->sh_name);
329 if (!*shname || !shdr->sh_size)
330 return 3;
331
332 *data = elf_getdata(scn, 0);
333 if (!*data || elf_getdata(scn, *data) != NULL)
334 return 4;
335
336 return 0;
337}
338
339static int parse_relo_and_apply(Elf_Data *data, Elf_Data *symbols,
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200340 GElf_Shdr *shdr, struct bpf_insn *insn,
341 struct bpf_map_data *maps, int nr_maps)
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800342{
343 int i, nrels;
344
345 nrels = shdr->sh_size / shdr->sh_entsize;
346
347 for (i = 0; i < nrels; i++) {
348 GElf_Sym sym;
349 GElf_Rel rel;
350 unsigned int insn_idx;
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200351 bool match = false;
352 int j, map_idx;
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800353
354 gelf_getrel(data, i, &rel);
355
356 insn_idx = rel.r_offset / sizeof(struct bpf_insn);
357
358 gelf_getsym(symbols, GELF_R_SYM(rel.r_info), &sym);
359
360 if (insn[insn_idx].code != (BPF_LD | BPF_IMM | BPF_DW)) {
361 printf("invalid relo for insn[%d].code 0x%x\n",
362 insn_idx, insn[insn_idx].code);
363 return 1;
364 }
365 insn[insn_idx].src_reg = BPF_PSEUDO_MAP_FD;
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200366
367 /* Match FD relocation against recorded map_data[] offset */
368 for (map_idx = 0; map_idx < nr_maps; map_idx++) {
369 if (maps[map_idx].elf_offset == sym.st_value) {
370 match = true;
371 break;
372 }
373 }
374 if (match) {
375 insn[insn_idx].imm = maps[map_idx].fd;
376 } else {
377 printf("invalid relo for insn[%d] no map_data match\n",
378 insn_idx);
379 return 1;
380 }
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800381 }
382
383 return 0;
384}
385
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700386static int cmp_symbols(const void *l, const void *r)
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800387{
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700388 const GElf_Sym *lsym = (const GElf_Sym *)l;
389 const GElf_Sym *rsym = (const GElf_Sym *)r;
390
391 if (lsym->st_value < rsym->st_value)
392 return -1;
393 else if (lsym->st_value > rsym->st_value)
394 return 1;
395 else
396 return 0;
397}
398
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200399static int load_elf_maps_section(struct bpf_map_data *maps, int maps_shndx,
400 Elf *elf, Elf_Data *symbols, int strtabidx)
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700401{
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200402 int map_sz_elf, map_sz_copy;
403 bool validate_zero = false;
404 Elf_Data *data_maps;
405 int i, nr_maps;
406 GElf_Sym *sym;
407 Elf_Scn *scn;
408 int copy_sz;
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700409
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200410 if (maps_shndx < 0)
411 return -EINVAL;
412 if (!symbols)
413 return -EINVAL;
414
415 /* Get data for maps section via elf index */
416 scn = elf_getscn(elf, maps_shndx);
417 if (scn)
418 data_maps = elf_getdata(scn, NULL);
419 if (!scn || !data_maps) {
420 printf("Failed to get Elf_Data from maps section %d\n",
421 maps_shndx);
422 return -EINVAL;
423 }
424
425 /* For each map get corrosponding symbol table entry */
426 sym = calloc(MAX_MAPS+1, sizeof(GElf_Sym));
427 for (i = 0, nr_maps = 0; i < symbols->d_size / sizeof(GElf_Sym); i++) {
428 assert(nr_maps < MAX_MAPS+1);
429 if (!gelf_getsym(symbols, i, &sym[nr_maps]))
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700430 continue;
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200431 if (sym[nr_maps].st_shndx != maps_shndx)
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700432 continue;
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200433 /* Only increment iif maps section */
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700434 nr_maps++;
435 }
436
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200437 /* Align to map_fd[] order, via sort on offset in sym.st_value */
438 qsort(sym, nr_maps, sizeof(GElf_Sym), cmp_symbols);
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700439
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200440 /* Keeping compatible with ELF maps section changes
441 * ------------------------------------------------
Jakub Kicinski74662ea2018-05-10 10:24:38 -0700442 * The program size of struct bpf_load_map_def is known by loader
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200443 * code, but struct stored in ELF file can be different.
444 *
445 * Unfortunately sym[i].st_size is zero. To calculate the
446 * struct size stored in the ELF file, assume all struct have
447 * the same size, and simply divide with number of map
448 * symbols.
449 */
450 map_sz_elf = data_maps->d_size / nr_maps;
Jakub Kicinski74662ea2018-05-10 10:24:38 -0700451 map_sz_copy = sizeof(struct bpf_load_map_def);
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200452 if (map_sz_elf < map_sz_copy) {
453 /*
454 * Backward compat, loading older ELF file with
455 * smaller struct, keeping remaining bytes zero.
456 */
457 map_sz_copy = map_sz_elf;
458 } else if (map_sz_elf > map_sz_copy) {
459 /*
460 * Forward compat, loading newer ELF file with larger
461 * struct with unknown features. Assume zero means
462 * feature not used. Thus, validate rest of struct
463 * data is zero.
464 */
465 validate_zero = true;
466 }
467
468 /* Memcpy relevant part of ELF maps data to loader maps */
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700469 for (i = 0; i < nr_maps; i++) {
Jakub Kicinski74662ea2018-05-10 10:24:38 -0700470 struct bpf_load_map_def *def;
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200471 unsigned char *addr, *end;
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200472 const char *map_name;
473 size_t offset;
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700474
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200475 map_name = elf_strptr(elf, strtabidx, sym[i].st_name);
476 maps[i].name = strdup(map_name);
477 if (!maps[i].name) {
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700478 printf("strdup(%s): %s(%d)\n", map_name,
479 strerror(errno), errno);
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200480 free(sym);
481 return -errno;
482 }
483
484 /* Symbol value is offset into ELF maps section data area */
485 offset = sym[i].st_value;
Jakub Kicinski74662ea2018-05-10 10:24:38 -0700486 def = (struct bpf_load_map_def *)(data_maps->d_buf + offset);
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200487 maps[i].elf_offset = offset;
Jakub Kicinski74662ea2018-05-10 10:24:38 -0700488 memset(&maps[i].def, 0, sizeof(struct bpf_load_map_def));
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200489 memcpy(&maps[i].def, def, map_sz_copy);
490
491 /* Verify no newer features were requested */
492 if (validate_zero) {
Daniel T. Lee37b54aed2019-05-23 16:24:48 +0900493 addr = (unsigned char *) def + map_sz_copy;
494 end = (unsigned char *) def + map_sz_elf;
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200495 for (; addr < end; addr++) {
496 if (*addr != 0) {
497 free(sym);
498 return -EFBIG;
499 }
500 }
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700501 }
502 }
503
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200504 free(sym);
Jesper Dangaard Brouer5010e942017-04-28 16:25:04 +0200505 return nr_maps;
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700506}
507
508static int do_load_bpf_file(const char *path, fixup_map_cb fixup_map)
509{
510 int fd, i, ret, maps_shndx = -1, strtabidx = -1;
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800511 Elf *elf;
512 GElf_Ehdr ehdr;
513 GElf_Shdr shdr, shdr_prog;
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700514 Elf_Data *data, *data_prog, *data_maps = NULL, *symbols = NULL;
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200515 char *shname, *shname_prog;
516 int nr_maps = 0;
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800517
Mickaël Salaüna734fb52017-02-08 21:27:43 +0100518 /* reset global variables */
519 kern_version = 0;
520 memset(license, 0, sizeof(license));
521 memset(processed_sec, 0, sizeof(processed_sec));
522
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800523 if (elf_version(EV_CURRENT) == EV_NONE)
524 return 1;
525
526 fd = open(path, O_RDONLY, 0);
527 if (fd < 0)
528 return 1;
529
530 elf = elf_begin(fd, ELF_C_READ, NULL);
531
532 if (!elf)
533 return 1;
534
535 if (gelf_getehdr(elf, &ehdr) != &ehdr)
536 return 1;
537
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700538 /* clear all kprobes */
Daniel T. Lee5a863812018-11-23 07:14:32 +0900539 i = write_kprobe_events("");
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700540
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800541 /* scan over all elf sections to get license and map info */
542 for (i = 1; i < ehdr.e_shnum; i++) {
543
544 if (get_sec(elf, i, &ehdr, &shname, &shdr, &data))
545 continue;
546
547 if (0) /* helpful for llvm debugging */
548 printf("section %d:%s data %p size %zd link %d flags %d\n",
549 i, shname, data->d_buf, data->d_size,
550 shdr.sh_link, (int) shdr.sh_flags);
551
552 if (strcmp(shname, "license") == 0) {
553 processed_sec[i] = true;
554 memcpy(license, data->d_buf, data->d_size);
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700555 } else if (strcmp(shname, "version") == 0) {
556 processed_sec[i] = true;
557 if (data->d_size != sizeof(int)) {
558 printf("invalid size of version section %zd\n",
559 data->d_size);
560 return 1;
561 }
562 memcpy(&kern_version, data->d_buf, sizeof(int));
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800563 } else if (strcmp(shname, "maps") == 0) {
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200564 int j;
565
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700566 maps_shndx = i;
567 data_maps = data;
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200568 for (j = 0; j < MAX_MAPS; j++)
569 map_data[j].fd = -1;
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800570 } else if (shdr.sh_type == SHT_SYMTAB) {
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700571 strtabidx = shdr.sh_link;
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800572 symbols = data;
573 }
574 }
575
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700576 ret = 1;
577
578 if (!symbols) {
579 printf("missing SHT_SYMTAB section\n");
580 goto done;
581 }
582
583 if (data_maps) {
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200584 nr_maps = load_elf_maps_section(map_data, maps_shndx,
585 elf, symbols, strtabidx);
586 if (nr_maps < 0) {
587 printf("Error: Failed loading ELF maps (errno:%d):%s\n",
588 nr_maps, strerror(-nr_maps));
Jesper Dangaard Brouer5010e942017-04-28 16:25:04 +0200589 goto done;
590 }
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200591 if (load_maps(map_data, nr_maps, fixup_map))
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700592 goto done;
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200593 map_data_count = nr_maps;
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700594
595 processed_sec[maps_shndx] = true;
596 }
597
Jesper Dangaard Brouer7bc57952017-05-30 14:37:51 +0200598 /* process all relo sections, and rewrite bpf insns for maps */
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800599 for (i = 1; i < ehdr.e_shnum; i++) {
Mickaël Salaün16ad1322017-02-08 21:27:42 +0100600 if (processed_sec[i])
601 continue;
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800602
603 if (get_sec(elf, i, &ehdr, &shname, &shdr, &data))
604 continue;
Jesper Dangaard Brouer7bc57952017-05-30 14:37:51 +0200605
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800606 if (shdr.sh_type == SHT_REL) {
607 struct bpf_insn *insns;
608
Jesper Dangaard Brouer7bc57952017-05-30 14:37:51 +0200609 /* locate prog sec that need map fixup (relocations) */
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800610 if (get_sec(elf, shdr.sh_info, &ehdr, &shname_prog,
611 &shdr_prog, &data_prog))
612 continue;
613
Alexei Starovoitovdb6a71d2016-11-22 16:52:09 -0800614 if (shdr_prog.sh_type != SHT_PROGBITS ||
615 !(shdr_prog.sh_flags & SHF_EXECINSTR))
616 continue;
617
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800618 insns = (struct bpf_insn *) data_prog->d_buf;
Jesper Dangaard Brouer7bc57952017-05-30 14:37:51 +0200619 processed_sec[i] = true; /* relo section */
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800620
Jesper Dangaard Brouer156450d2017-05-02 14:31:56 +0200621 if (parse_relo_and_apply(data, symbols, &shdr, insns,
622 map_data, nr_maps))
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800623 continue;
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800624 }
625 }
626
Jesper Dangaard Brouer7bc57952017-05-30 14:37:51 +0200627 /* load programs */
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800628 for (i = 1; i < ehdr.e_shnum; i++) {
629
630 if (processed_sec[i])
631 continue;
632
633 if (get_sec(elf, i, &ehdr, &shname, &shdr, &data))
634 continue;
635
Alexei Starovoitovb896c4f2015-03-25 12:49:23 -0700636 if (memcmp(shname, "kprobe/", 7) == 0 ||
637 memcmp(shname, "kretprobe/", 10) == 0 ||
Alexei Starovoitovc0766042016-04-06 18:43:29 -0700638 memcmp(shname, "tracepoint/", 11) == 0 ||
Alexei Starovoitov4662a4e2018-03-28 12:05:39 -0700639 memcmp(shname, "raw_tracepoint/", 15) == 0 ||
Brenden Blanco86af8b42016-07-19 12:16:51 -0700640 memcmp(shname, "xdp", 3) == 0 ||
Alexei Starovoitov1c47910e2016-09-01 18:37:25 -0700641 memcmp(shname, "perf_event", 10) == 0 ||
David Ahern4f2e7ae2016-12-01 08:48:07 -0800642 memcmp(shname, "socket", 6) == 0 ||
Lawrence Brakmo40304b22017-06-30 20:02:40 -0700643 memcmp(shname, "cgroup/", 7) == 0 ||
John Fastabend69e8cc12017-08-15 22:33:32 -0700644 memcmp(shname, "sockops", 7) == 0 ||
John Fastabend4c4c3c22018-03-18 12:57:41 -0700645 memcmp(shname, "sk_skb", 6) == 0 ||
646 memcmp(shname, "sk_msg", 6) == 0) {
Lawrence Brakmof856e462017-07-04 15:57:50 -0700647 ret = load_and_attach(shname, data->d_buf,
648 data->d_size);
649 if (ret != 0)
650 goto done;
651 }
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800652 }
653
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700654done:
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800655 close(fd);
Martin KaFai Lau9fd63d02017-04-14 10:30:28 -0700656 return ret;
657}
658
659int load_bpf_file(char *path)
660{
661 return do_load_bpf_file(path, NULL);
662}
663
664int load_bpf_file_fixup_map(const char *path, fixup_map_cb fixup_map)
665{
666 return do_load_bpf_file(path, fixup_map);
Alexei Starovoitov249b8122014-12-01 15:06:37 -0800667}