blob: 2c709e174415fe442918a3517facefdbdff74543 [file] [log] [blame]
Dmitry V. Levinddb53dd2015-07-25 23:55:51 +00001/*
2 * Copyright (c) 2015 Dmitry V. Levin <ldv@altlinux.org>
Elliott Hughesd35df492017-02-15 15:19:05 -08003 * Copyright (c) 2017 Quentin Monnet <quentin.monnet@6wind.com>
Dmitry V. Levinddb53dd2015-07-25 23:55:51 +00004 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include "defs.h"
30
31#ifdef HAVE_LINUX_BPF_H
32# include <linux/bpf.h>
33#endif
34
35#include "xlat/bpf_commands.h"
36#include "xlat/bpf_map_types.h"
37#include "xlat/bpf_prog_types.h"
38#include "xlat/bpf_map_update_elem_flags.h"
Elliott Hughesd35df492017-02-15 15:19:05 -080039#include "xlat/bpf_attach_type.h"
Elliott Hughes39bac052017-05-25 16:56:11 -070040#include "xlat/bpf_attach_flags.h"
Dmitry V. Levinddb53dd2015-07-25 23:55:51 +000041
42static int
Elliott Hughesd35df492017-02-15 15:19:05 -080043bpf_map_create(struct tcb *const tcp, const kernel_ulong_t addr,
44 unsigned int size)
Dmitry V. Levinddb53dd2015-07-25 23:55:51 +000045{
46 struct {
47 uint32_t map_type, key_size, value_size, max_entries;
48 } attr = {};
49
50 if (!size) {
51 printaddr(addr);
52 return RVAL_DECODED | RVAL_FD;
53 }
54 if (size > sizeof(attr))
55 size = sizeof(attr);
56 if (umoven_or_printaddr(tcp, addr, size, &attr))
57 return RVAL_DECODED | RVAL_FD;
58
59 tprints("{map_type=");
60 printxval(bpf_map_types, attr.map_type, "BPF_MAP_TYPE_???");
61 tprintf(", key_size=%u, value_size=%u, max_entries=%u}",
62 attr.key_size, attr.value_size, attr.max_entries);
63
64 return RVAL_DECODED | RVAL_FD;
65}
66
67static void
Elliott Hughesd35df492017-02-15 15:19:05 -080068bpf_map_update_elem(struct tcb *const tcp, const kernel_ulong_t addr,
69 unsigned int size)
Dmitry V. Levinddb53dd2015-07-25 23:55:51 +000070{
71 struct {
72 uint32_t map_fd;
73 uint64_t ATTRIBUTE_ALIGNED(8) key;
74 uint64_t ATTRIBUTE_ALIGNED(8) value;
75 uint64_t flags;
76 } attr = {};
77
78 if (!size) {
79 printaddr(addr);
80 return;
81 }
82 if (size > sizeof(attr))
83 size = sizeof(attr);
84 if (umoven_or_printaddr(tcp, addr, size, &attr))
85 return;
86
87 tprints("{map_fd=");
88 printfd(tcp, attr.map_fd);
89 tprintf(", key=%#" PRIx64 ", value=%#" PRIx64 ", flags=",
90 attr.key, attr.value);
Dmitry V. Levin18d921d2016-05-16 21:27:30 +000091 printxval64(bpf_map_update_elem_flags, attr.flags, "BPF_???");
Dmitry V. Levinddb53dd2015-07-25 23:55:51 +000092 tprints("}");
93}
94
95static void
Elliott Hughesd35df492017-02-15 15:19:05 -080096bpf_map_delete_elem(struct tcb *const tcp, const kernel_ulong_t addr,
97 unsigned int size)
Dmitry V. Levinddb53dd2015-07-25 23:55:51 +000098{
99 struct {
100 uint32_t map_fd;
101 uint64_t ATTRIBUTE_ALIGNED(8) key;
102 } attr = {};
103
104 if (!size) {
105 printaddr(addr);
106 return;
107 }
108 if (size > sizeof(attr))
109 size = sizeof(attr);
110 if (umoven_or_printaddr(tcp, addr, size, &attr))
111 return;
112
113 tprints("{map_fd=");
114 printfd(tcp, attr.map_fd);
115 tprintf(", key=%#" PRIx64 "}", attr.key);
116}
117
118static int
Elliott Hughesd35df492017-02-15 15:19:05 -0800119bpf_map_io(struct tcb *const tcp, const kernel_ulong_t addr, unsigned int size,
120 const char *const text)
Dmitry V. Levinddb53dd2015-07-25 23:55:51 +0000121{
122 struct bpf_io_elem_struct {
123 uint32_t map_fd;
124 uint64_t ATTRIBUTE_ALIGNED(8) key;
125 uint64_t ATTRIBUTE_ALIGNED(8) value;
126 } attr = {};
127
128 if (exiting(tcp)) {
129 if (!syserror(tcp) && !umove_or_printaddr(tcp, addr, &attr))
130 tprintf(", %s=%#" PRIx64, text, attr.value);
131 tprints("}");
132 return RVAL_DECODED;
133 }
134
135 if (!size) {
136 printaddr(addr);
137 return RVAL_DECODED;
138 }
139 if (size > sizeof(attr))
140 size = sizeof(attr);
141 if (umoven_or_printaddr(tcp, addr, size, &attr))
142 return RVAL_DECODED;
143
144 tprints("{map_fd=");
145 printfd(tcp, attr.map_fd);
146 tprintf(", key=%#" PRIx64, attr.key);
147
148 return 0;
149}
150
151static int
Elliott Hughesd35df492017-02-15 15:19:05 -0800152bpf_prog_load(struct tcb *const tcp, const kernel_ulong_t addr,
153 unsigned int size)
Dmitry V. Levinddb53dd2015-07-25 23:55:51 +0000154{
155 struct {
156 uint32_t prog_type, insn_cnt;
157 uint64_t ATTRIBUTE_ALIGNED(8) insns, license;
158 uint32_t log_level, log_size;
159 uint64_t ATTRIBUTE_ALIGNED(8) log_buf;
160 uint32_t kern_version;
161 } attr = {};
162
163 if (!size) {
164 printaddr(addr);
165 return RVAL_DECODED | RVAL_FD;
166 }
167 if (size > sizeof(attr))
168 size = sizeof(attr);
169 if (umoven_or_printaddr(tcp, addr, size, &attr))
170 return RVAL_DECODED | RVAL_FD;
171
172 tprints("{prog_type=");
173 printxval(bpf_prog_types, attr.prog_type, "BPF_PROG_TYPE_???");
174 tprintf(", insn_cnt=%u, insns=%#" PRIx64 ", license=",
175 attr.insn_cnt, attr.insns);
Elliott Hughesd35df492017-02-15 15:19:05 -0800176 printstr(tcp, attr.license);
Dmitry V. Levinddb53dd2015-07-25 23:55:51 +0000177 tprintf(", log_level=%u, log_size=%u, log_buf=%#" PRIx64 ", kern_version=%u}",
178 attr.log_level, attr.log_size, attr.log_buf, attr.kern_version);
179
180 return RVAL_DECODED | RVAL_FD;
181}
182
Elliott Hughesd35df492017-02-15 15:19:05 -0800183static int
184bpf_obj_manage(struct tcb *const tcp, const kernel_ulong_t addr,
185 unsigned int size)
186{
187 struct {
188 uint64_t ATTRIBUTE_ALIGNED(8) pathname;
189 uint32_t bpf_fd;
190 } attr = {};
191
192 if (!size) {
193 printaddr(addr);
194 return RVAL_DECODED | RVAL_FD;
195 }
196 if (size > sizeof(attr))
197 size = sizeof(attr);
198 if (umoven_or_printaddr(tcp, addr, size, &attr))
199 return RVAL_DECODED | RVAL_FD;
200
Elliott Hughes39bac052017-05-25 16:56:11 -0700201 tprints("{pathname=");
Elliott Hughesd35df492017-02-15 15:19:05 -0800202 printpath(tcp, attr.pathname);
203 tprints(", bpf_fd=");
204 printfd(tcp, attr.bpf_fd);
Elliott Hughes39bac052017-05-25 16:56:11 -0700205 tprints("}");
Elliott Hughesd35df492017-02-15 15:19:05 -0800206
207 return RVAL_DECODED | RVAL_FD;
208}
209
210static int
211bpf_prog_attach_detach(struct tcb *const tcp, const kernel_ulong_t addr,
Elliott Hughes39bac052017-05-25 16:56:11 -0700212 unsigned int size, bool print_attach)
Elliott Hughesd35df492017-02-15 15:19:05 -0800213{
214 struct {
Elliott Hughes39bac052017-05-25 16:56:11 -0700215 uint32_t target_fd, attach_bpf_fd, attach_type, attach_flags;
Elliott Hughesd35df492017-02-15 15:19:05 -0800216 } attr = {};
217
218 if (!size) {
219 printaddr(addr);
220 return RVAL_DECODED;
221 }
222 if (size > sizeof(attr))
223 size = sizeof(attr);
224 if (umoven_or_printaddr(tcp, addr, size, &attr))
225 return RVAL_DECODED;
226
Elliott Hughes39bac052017-05-25 16:56:11 -0700227 tprints("{target_fd=");
Elliott Hughesd35df492017-02-15 15:19:05 -0800228 printfd(tcp, attr.target_fd);
Elliott Hughes39bac052017-05-25 16:56:11 -0700229 if (print_attach) {
230 tprints(", attach_bpf_fd=");
Elliott Hughesd35df492017-02-15 15:19:05 -0800231 printfd(tcp, attr.attach_bpf_fd);
232 }
Elliott Hughes39bac052017-05-25 16:56:11 -0700233 tprints(", attach_type=");
Elliott Hughesd35df492017-02-15 15:19:05 -0800234 printxval(bpf_attach_type, attr.attach_type, "BPF_???");
Elliott Hughes39bac052017-05-25 16:56:11 -0700235 if (print_attach) {
236 tprints(", attach_flags=");
237 printflags(bpf_attach_flags, attr.attach_flags, "BPF_F_???");
238 }
239 tprints("}");
Elliott Hughesd35df492017-02-15 15:19:05 -0800240
241 return RVAL_DECODED;
242}
243
244static int
245bpf_prog_attach(struct tcb *const tcp, const kernel_ulong_t addr,
246 unsigned int size)
247{
248 return bpf_prog_attach_detach(tcp, addr, size, true);
249}
250
251static int
252bpf_prog_detach(struct tcb *const tcp, const kernel_ulong_t addr,
253 unsigned int size)
254{
255 return bpf_prog_attach_detach(tcp, addr, size, false);
256}
257
Dmitry V. Levinddb53dd2015-07-25 23:55:51 +0000258SYS_FUNC(bpf)
259{
Elliott Hughesd35df492017-02-15 15:19:05 -0800260 const unsigned int cmd = tcp->u_arg[0];
261 const kernel_ulong_t addr = tcp->u_arg[1];
Dmitry V. Levinddb53dd2015-07-25 23:55:51 +0000262 const unsigned int size = tcp->u_arg[2];
263 int rc = RVAL_DECODED;
264
265 if (entering(tcp)) {
266 printxval(bpf_commands, cmd, "BPF_???");
267 tprints(", ");
268 }
269
270 switch (cmd) {
271 case BPF_MAP_CREATE:
272 rc = bpf_map_create(tcp, addr, size);
273 break;
274 case BPF_MAP_LOOKUP_ELEM:
275 rc = bpf_map_io(tcp, addr, size, "value");
276 break;
277 case BPF_MAP_UPDATE_ELEM:
278 bpf_map_update_elem(tcp, addr, size);
279 break;
280 case BPF_MAP_DELETE_ELEM:
281 bpf_map_delete_elem(tcp, addr, size);
282 break;
283 case BPF_MAP_GET_NEXT_KEY:
284 rc = bpf_map_io(tcp, addr, size, "next_key");
285 break;
286 case BPF_PROG_LOAD:
287 rc = bpf_prog_load(tcp, addr, size);
288 break;
Elliott Hughesd35df492017-02-15 15:19:05 -0800289 case BPF_OBJ_PIN:
290 rc = bpf_obj_manage(tcp, addr, size);
291 break;
292 case BPF_OBJ_GET:
293 rc = bpf_obj_manage(tcp, addr, size);
294 break;
295 case BPF_PROG_ATTACH:
296 rc = bpf_prog_attach(tcp, addr, size);
297 break;
298 case BPF_PROG_DETACH:
299 rc = bpf_prog_detach(tcp, addr, size);
300 break;
Dmitry V. Levinddb53dd2015-07-25 23:55:51 +0000301 default:
302 printaddr(addr);
303 break;
304 }
305
306 if (rc & RVAL_DECODED)
307 tprintf(", %u", size);
308
309 return rc;
310}