blob: 2d97dd27c8f64f5e6162472aae9b70eea58f1dda [file] [log] [blame]
Arnaldo Carvalho de Melo971e8272016-07-11 16:38:05 -03001/* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
2 *
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of version 2 of the GNU General Public
5 * License as published by the Free Software Foundation.
6 */
7#ifndef _UAPI__LINUX_BPF_H__
8#define _UAPI__LINUX_BPF_H__
9
10#include <linux/types.h>
11#include <linux/bpf_common.h>
12
13/* Extended instruction set based on top of classic BPF */
14
15/* instruction classes */
16#define BPF_ALU64 0x07 /* alu mode in double word width */
17
18/* ld/ldx fields */
19#define BPF_DW 0x18 /* double word */
20#define BPF_XADD 0xc0 /* exclusive add */
21
22/* alu/jmp fields */
23#define BPF_MOV 0xb0 /* mov reg to reg */
24#define BPF_ARSH 0xc0 /* sign extending arithmetic shift right */
25
26/* change endianness of a register */
27#define BPF_END 0xd0 /* flags for endianness conversion: */
28#define BPF_TO_LE 0x00 /* convert to little-endian */
29#define BPF_TO_BE 0x08 /* convert to big-endian */
30#define BPF_FROM_LE BPF_TO_LE
31#define BPF_FROM_BE BPF_TO_BE
32
Daniel Borkmann92b31a92017-08-10 01:39:55 +020033/* jmp encodings */
Arnaldo Carvalho de Melo971e8272016-07-11 16:38:05 -030034#define BPF_JNE 0x50 /* jump != */
Daniel Borkmann92b31a92017-08-10 01:39:55 +020035#define BPF_JLT 0xa0 /* LT is unsigned, '<' */
36#define BPF_JLE 0xb0 /* LE is unsigned, '<=' */
Arnaldo Carvalho de Melo971e8272016-07-11 16:38:05 -030037#define BPF_JSGT 0x60 /* SGT is signed '>', GT in x86 */
38#define BPF_JSGE 0x70 /* SGE is signed '>=', GE in x86 */
Daniel Borkmann92b31a92017-08-10 01:39:55 +020039#define BPF_JSLT 0xc0 /* SLT is signed, '<' */
40#define BPF_JSLE 0xd0 /* SLE is signed, '<=' */
Arnaldo Carvalho de Melo971e8272016-07-11 16:38:05 -030041#define BPF_CALL 0x80 /* function call */
42#define BPF_EXIT 0x90 /* function return */
43
44/* Register numbers */
45enum {
46 BPF_REG_0 = 0,
47 BPF_REG_1,
48 BPF_REG_2,
49 BPF_REG_3,
50 BPF_REG_4,
51 BPF_REG_5,
52 BPF_REG_6,
53 BPF_REG_7,
54 BPF_REG_8,
55 BPF_REG_9,
56 BPF_REG_10,
57 __MAX_BPF_REG,
58};
59
60/* BPF has 10 general purpose 64-bit registers and stack frame. */
61#define MAX_BPF_REG __MAX_BPF_REG
62
63struct bpf_insn {
64 __u8 code; /* opcode */
65 __u8 dst_reg:4; /* dest register */
66 __u8 src_reg:4; /* source register */
67 __s16 off; /* signed offset */
68 __s32 imm; /* signed immediate constant */
69};
70
Mickaël Salaün9a738262017-02-10 00:21:35 +010071/* Key of an a BPF_MAP_TYPE_LPM_TRIE entry */
72struct bpf_lpm_trie_key {
73 __u32 prefixlen; /* up to 32 for AF_INET, 128 for AF_INET6 */
74 __u8 data[0]; /* Arbitrary size */
75};
76
Arnaldo Carvalho de Melo971e8272016-07-11 16:38:05 -030077/* BPF syscall commands, see bpf(2) man-page for details. */
78enum bpf_cmd {
79 BPF_MAP_CREATE,
80 BPF_MAP_LOOKUP_ELEM,
81 BPF_MAP_UPDATE_ELEM,
82 BPF_MAP_DELETE_ELEM,
83 BPF_MAP_GET_NEXT_KEY,
84 BPF_PROG_LOAD,
85 BPF_OBJ_PIN,
86 BPF_OBJ_GET,
Joe Stringer0cb34dc2016-12-08 18:46:14 -080087 BPF_PROG_ATTACH,
88 BPF_PROG_DETACH,
Alexei Starovoitov30848872017-03-30 21:45:39 -070089 BPF_PROG_TEST_RUN,
Martin KaFai Lau95b9afd2017-06-05 12:15:53 -070090 BPF_PROG_GET_NEXT_ID,
91 BPF_MAP_GET_NEXT_ID,
92 BPF_PROG_GET_FD_BY_ID,
93 BPF_MAP_GET_FD_BY_ID,
94 BPF_OBJ_GET_INFO_BY_FD,
Arnaldo Carvalho de Melo971e8272016-07-11 16:38:05 -030095};
96
97enum bpf_map_type {
98 BPF_MAP_TYPE_UNSPEC,
99 BPF_MAP_TYPE_HASH,
100 BPF_MAP_TYPE_ARRAY,
101 BPF_MAP_TYPE_PROG_ARRAY,
102 BPF_MAP_TYPE_PERF_EVENT_ARRAY,
103 BPF_MAP_TYPE_PERCPU_HASH,
104 BPF_MAP_TYPE_PERCPU_ARRAY,
105 BPF_MAP_TYPE_STACK_TRACE,
Arnaldo Carvalho de Melo791cceb2016-08-09 11:48:07 -0300106 BPF_MAP_TYPE_CGROUP_ARRAY,
Joe Stringer0cb34dc2016-12-08 18:46:14 -0800107 BPF_MAP_TYPE_LRU_HASH,
108 BPF_MAP_TYPE_LRU_PERCPU_HASH,
Mickaël Salaün9a738262017-02-10 00:21:35 +0100109 BPF_MAP_TYPE_LPM_TRIE,
Martin KaFai Laufb30d4b2017-03-22 10:00:35 -0700110 BPF_MAP_TYPE_ARRAY_OF_MAPS,
111 BPF_MAP_TYPE_HASH_OF_MAPS,
John Fastabend81f6bf82017-07-26 17:32:07 -0700112 BPF_MAP_TYPE_DEVMAP,
John Fastabend69e8cc12017-08-15 22:33:32 -0700113 BPF_MAP_TYPE_SOCKMAP,
Arnaldo Carvalho de Melo971e8272016-07-11 16:38:05 -0300114};
115
116enum bpf_prog_type {
117 BPF_PROG_TYPE_UNSPEC,
118 BPF_PROG_TYPE_SOCKET_FILTER,
119 BPF_PROG_TYPE_KPROBE,
120 BPF_PROG_TYPE_SCHED_CLS,
121 BPF_PROG_TYPE_SCHED_ACT,
122 BPF_PROG_TYPE_TRACEPOINT,
Arnaldo Carvalho de Melo791cceb2016-08-09 11:48:07 -0300123 BPF_PROG_TYPE_XDP,
Joe Stringer0cb34dc2016-12-08 18:46:14 -0800124 BPF_PROG_TYPE_PERF_EVENT,
125 BPF_PROG_TYPE_CGROUP_SKB,
126 BPF_PROG_TYPE_CGROUP_SOCK,
127 BPF_PROG_TYPE_LWT_IN,
128 BPF_PROG_TYPE_LWT_OUT,
129 BPF_PROG_TYPE_LWT_XMIT,
Lawrence Brakmo04df41e2017-06-30 20:02:55 -0700130 BPF_PROG_TYPE_SOCK_OPS,
John Fastabend69e8cc12017-08-15 22:33:32 -0700131 BPF_PROG_TYPE_SK_SKB,
Arnaldo Carvalho de Melo971e8272016-07-11 16:38:05 -0300132};
133
Joe Stringer0cb34dc2016-12-08 18:46:14 -0800134enum bpf_attach_type {
135 BPF_CGROUP_INET_INGRESS,
136 BPF_CGROUP_INET_EGRESS,
137 BPF_CGROUP_INET_SOCK_CREATE,
Lawrence Brakmo04df41e2017-06-30 20:02:55 -0700138 BPF_CGROUP_SOCK_OPS,
John Fastabend69e8cc12017-08-15 22:33:32 -0700139 BPF_CGROUP_SMAP_INGRESS,
Joe Stringer0cb34dc2016-12-08 18:46:14 -0800140 __MAX_BPF_ATTACH_TYPE
141};
142
143#define MAX_BPF_ATTACH_TYPE __MAX_BPF_ATTACH_TYPE
144
John Fastabend69e8cc12017-08-15 22:33:32 -0700145enum bpf_sockmap_flags {
146 BPF_SOCKMAP_UNSPEC,
147 BPF_SOCKMAP_STRPARSER,
148 __MAX_BPF_SOCKMAP_FLAG
149};
150
Stephen Rothwell5463b3d2017-02-14 08:22:20 +1100151/* If BPF_F_ALLOW_OVERRIDE flag is used in BPF_PROG_ATTACH command
152 * to the given target_fd cgroup the descendent cgroup will be able to
153 * override effective bpf program that was inherited from this cgroup
154 */
155#define BPF_F_ALLOW_OVERRIDE (1U << 0)
156
David S. Millere07b98d2017-05-10 11:38:07 -0700157/* If BPF_F_STRICT_ALIGNMENT is used in BPF_PROG_LOAD command, the
158 * verifier will perform strict alignment checking as if the kernel
159 * has been built with CONFIG_EFFICIENT_UNALIGNED_ACCESS not set,
160 * and NET_IP_ALIGN defined to 2.
161 */
162#define BPF_F_STRICT_ALIGNMENT (1U << 0)
163
Arnaldo Carvalho de Melo971e8272016-07-11 16:38:05 -0300164#define BPF_PSEUDO_MAP_FD 1
165
166/* flags for BPF_MAP_UPDATE_ELEM command */
167#define BPF_ANY 0 /* create new element or update existing */
168#define BPF_NOEXIST 1 /* create new element if it didn't exist */
169#define BPF_EXIST 2 /* update existing element */
170
171#define BPF_F_NO_PREALLOC (1U << 0)
Joe Stringer0cb34dc2016-12-08 18:46:14 -0800172/* Instead of having one common LRU list in the
173 * BPF_MAP_TYPE_LRU_[PERCPU_]HASH map, use a percpu LRU list
174 * which can scale and perform better.
175 * Note, the LRU nodes (including free nodes) cannot be moved
176 * across different LRU lists.
177 */
178#define BPF_F_NO_COMMON_LRU (1U << 1)
Arnaldo Carvalho de Melo971e8272016-07-11 16:38:05 -0300179
180union bpf_attr {
181 struct { /* anonymous struct used by BPF_MAP_CREATE command */
182 __u32 map_type; /* one of enum bpf_map_type */
183 __u32 key_size; /* size of key in bytes */
184 __u32 value_size; /* size of value in bytes */
185 __u32 max_entries; /* max number of entries in a map */
186 __u32 map_flags; /* prealloc or not */
Martin KaFai Laufb30d4b2017-03-22 10:00:35 -0700187 __u32 inner_map_fd; /* fd pointing to the inner map */
Arnaldo Carvalho de Melo971e8272016-07-11 16:38:05 -0300188 };
189
190 struct { /* anonymous struct used by BPF_MAP_*_ELEM commands */
191 __u32 map_fd;
192 __aligned_u64 key;
193 union {
194 __aligned_u64 value;
195 __aligned_u64 next_key;
196 };
197 __u64 flags;
198 };
199
200 struct { /* anonymous struct used by BPF_PROG_LOAD command */
201 __u32 prog_type; /* one of enum bpf_prog_type */
202 __u32 insn_cnt;
203 __aligned_u64 insns;
204 __aligned_u64 license;
205 __u32 log_level; /* verbosity level of verifier */
206 __u32 log_size; /* size of user buffer */
207 __aligned_u64 log_buf; /* user supplied buffer */
208 __u32 kern_version; /* checked when prog_type=kprobe */
David S. Millere07b98d2017-05-10 11:38:07 -0700209 __u32 prog_flags;
Arnaldo Carvalho de Melo971e8272016-07-11 16:38:05 -0300210 };
211
212 struct { /* anonymous struct used by BPF_OBJ_* commands */
213 __aligned_u64 pathname;
214 __u32 bpf_fd;
215 };
Joe Stringer0cb34dc2016-12-08 18:46:14 -0800216
217 struct { /* anonymous struct used by BPF_PROG_ATTACH/DETACH commands */
218 __u32 target_fd; /* container object to attach to */
219 __u32 attach_bpf_fd; /* eBPF program to attach */
220 __u32 attach_type;
Stephen Rothwell5463b3d2017-02-14 08:22:20 +1100221 __u32 attach_flags;
John Fastabend69e8cc12017-08-15 22:33:32 -0700222 __u32 attach_bpf_fd2;
Joe Stringer0cb34dc2016-12-08 18:46:14 -0800223 };
Alexei Starovoitov30848872017-03-30 21:45:39 -0700224
225 struct { /* anonymous struct used by BPF_PROG_TEST_RUN command */
226 __u32 prog_fd;
227 __u32 retval;
228 __u32 data_size_in;
229 __u32 data_size_out;
230 __aligned_u64 data_in;
231 __aligned_u64 data_out;
232 __u32 repeat;
233 __u32 duration;
234 } test;
Martin KaFai Lau95b9afd2017-06-05 12:15:53 -0700235
236 struct { /* anonymous struct used by BPF_*_GET_*_ID */
237 union {
238 __u32 start_id;
239 __u32 prog_id;
240 __u32 map_id;
241 };
242 __u32 next_id;
243 };
244
245 struct { /* anonymous struct used by BPF_OBJ_GET_INFO_BY_FD */
246 __u32 bpf_fd;
247 __u32 info_len;
248 __aligned_u64 info;
249 } info;
Arnaldo Carvalho de Melo971e8272016-07-11 16:38:05 -0300250} __attribute__((aligned(8)));
251
Joe Stringer0cb34dc2016-12-08 18:46:14 -0800252/* BPF helper function descriptions:
253 *
254 * void *bpf_map_lookup_elem(&map, &key)
255 * Return: Map value or NULL
256 *
257 * int bpf_map_update_elem(&map, &key, &value, flags)
258 * Return: 0 on success or negative error
259 *
260 * int bpf_map_delete_elem(&map, &key)
261 * Return: 0 on success or negative error
262 *
263 * int bpf_probe_read(void *dst, int size, void *src)
264 * Return: 0 on success or negative error
265 *
266 * u64 bpf_ktime_get_ns(void)
267 * Return: current ktime
268 *
269 * int bpf_trace_printk(const char *fmt, int fmt_size, ...)
270 * Return: length of buffer written or negative error
271 *
272 * u32 bpf_prandom_u32(void)
273 * Return: random value
274 *
275 * u32 bpf_raw_smp_processor_id(void)
276 * Return: SMP processor ID
277 *
278 * int bpf_skb_store_bytes(skb, offset, from, len, flags)
279 * store bytes into packet
280 * @skb: pointer to skb
281 * @offset: offset within packet from skb->mac_header
282 * @from: pointer where to copy bytes from
283 * @len: number of bytes to store into packet
284 * @flags: bit 0 - if true, recompute skb->csum
285 * other bits - reserved
286 * Return: 0 on success or negative error
287 *
288 * int bpf_l3_csum_replace(skb, offset, from, to, flags)
289 * recompute IP checksum
290 * @skb: pointer to skb
291 * @offset: offset within packet where IP checksum is located
292 * @from: old value of header field
293 * @to: new value of header field
294 * @flags: bits 0-3 - size of header field
295 * other bits - reserved
296 * Return: 0 on success or negative error
297 *
298 * int bpf_l4_csum_replace(skb, offset, from, to, flags)
299 * recompute TCP/UDP checksum
300 * @skb: pointer to skb
301 * @offset: offset within packet where TCP/UDP checksum is located
302 * @from: old value of header field
303 * @to: new value of header field
304 * @flags: bits 0-3 - size of header field
305 * bit 4 - is pseudo header
306 * other bits - reserved
307 * Return: 0 on success or negative error
308 *
309 * int bpf_tail_call(ctx, prog_array_map, index)
310 * jump into another BPF program
311 * @ctx: context pointer passed to next program
312 * @prog_array_map: pointer to map which type is BPF_MAP_TYPE_PROG_ARRAY
313 * @index: index inside array that selects specific program to run
314 * Return: 0 on success or negative error
315 *
316 * int bpf_clone_redirect(skb, ifindex, flags)
317 * redirect to another netdev
318 * @skb: pointer to skb
319 * @ifindex: ifindex of the net device
320 * @flags: bit 0 - if set, redirect to ingress instead of egress
321 * other bits - reserved
322 * Return: 0 on success or negative error
323 *
324 * u64 bpf_get_current_pid_tgid(void)
325 * Return: current->tgid << 32 | current->pid
326 *
327 * u64 bpf_get_current_uid_gid(void)
328 * Return: current_gid << 32 | current_uid
329 *
330 * int bpf_get_current_comm(char *buf, int size_of_buf)
331 * stores current->comm into buf
332 * Return: 0 on success or negative error
333 *
334 * u32 bpf_get_cgroup_classid(skb)
335 * retrieve a proc's classid
336 * @skb: pointer to skb
337 * Return: classid if != 0
338 *
339 * int bpf_skb_vlan_push(skb, vlan_proto, vlan_tci)
340 * Return: 0 on success or negative error
341 *
342 * int bpf_skb_vlan_pop(skb)
343 * Return: 0 on success or negative error
344 *
345 * int bpf_skb_get_tunnel_key(skb, key, size, flags)
346 * int bpf_skb_set_tunnel_key(skb, key, size, flags)
347 * retrieve or populate tunnel metadata
348 * @skb: pointer to skb
349 * @key: pointer to 'struct bpf_tunnel_key'
350 * @size: size of 'struct bpf_tunnel_key'
351 * @flags: room for future extensions
352 * Return: 0 on success or negative error
353 *
Teng Qinb7d3ed52017-06-02 21:03:54 -0700354 * u64 bpf_perf_event_read(map, flags)
355 * read perf event counter value
356 * @map: pointer to perf_event_array map
357 * @flags: index of event in the map or bitmask flags
358 * Return: value of perf event counter read or error code
Joe Stringer0cb34dc2016-12-08 18:46:14 -0800359 *
360 * int bpf_redirect(ifindex, flags)
361 * redirect to another netdev
362 * @ifindex: ifindex of the net device
363 * @flags: bit 0 - if set, redirect to ingress instead of egress
364 * other bits - reserved
365 * Return: TC_ACT_REDIRECT
366 *
367 * u32 bpf_get_route_realm(skb)
368 * retrieve a dst's tclassid
369 * @skb: pointer to skb
370 * Return: realm if != 0
371 *
Teng Qinb7d3ed52017-06-02 21:03:54 -0700372 * int bpf_perf_event_output(ctx, map, flags, data, size)
Joe Stringer0cb34dc2016-12-08 18:46:14 -0800373 * output perf raw sample
374 * @ctx: struct pt_regs*
375 * @map: pointer to perf_event_array map
Teng Qinb7d3ed52017-06-02 21:03:54 -0700376 * @flags: index of event in the map or bitmask flags
Joe Stringer0cb34dc2016-12-08 18:46:14 -0800377 * @data: data on stack to be output as raw data
378 * @size: size of data
379 * Return: 0 on success or negative error
380 *
381 * int bpf_get_stackid(ctx, map, flags)
382 * walk user or kernel stack and return id
383 * @ctx: struct pt_regs*
384 * @map: pointer to stack_trace map
385 * @flags: bits 0-7 - numer of stack frames to skip
386 * bit 8 - collect user stack instead of kernel
387 * bit 9 - compare stacks by hash only
388 * bit 10 - if two different stacks hash into the same stackid
389 * discard old
390 * other bits - reserved
391 * Return: >= 0 stackid on success or negative error
392 *
393 * s64 bpf_csum_diff(from, from_size, to, to_size, seed)
394 * calculate csum diff
395 * @from: raw from buffer
396 * @from_size: length of from buffer
397 * @to: raw to buffer
398 * @to_size: length of to buffer
399 * @seed: optional seed
400 * Return: csum result or negative error code
401 *
402 * int bpf_skb_get_tunnel_opt(skb, opt, size)
403 * retrieve tunnel options metadata
404 * @skb: pointer to skb
405 * @opt: pointer to raw tunnel option data
406 * @size: size of @opt
407 * Return: option size
408 *
409 * int bpf_skb_set_tunnel_opt(skb, opt, size)
410 * populate tunnel options metadata
411 * @skb: pointer to skb
412 * @opt: pointer to raw tunnel option data
413 * @size: size of @opt
414 * Return: 0 on success or negative error
415 *
416 * int bpf_skb_change_proto(skb, proto, flags)
417 * Change protocol of the skb. Currently supported is v4 -> v6,
418 * v6 -> v4 transitions. The helper will also resize the skb. eBPF
419 * program is expected to fill the new headers via skb_store_bytes
420 * and lX_csum_replace.
421 * @skb: pointer to skb
422 * @proto: new skb->protocol type
423 * @flags: reserved
424 * Return: 0 on success or negative error
425 *
426 * int bpf_skb_change_type(skb, type)
427 * Change packet type of skb.
428 * @skb: pointer to skb
429 * @type: new skb->pkt_type type
430 * Return: 0 on success or negative error
431 *
432 * int bpf_skb_under_cgroup(skb, map, index)
433 * Check cgroup2 membership of skb
434 * @skb: pointer to skb
435 * @map: pointer to bpf_map in BPF_MAP_TYPE_CGROUP_ARRAY type
436 * @index: index of the cgroup in the bpf_map
437 * Return:
438 * == 0 skb failed the cgroup2 descendant test
439 * == 1 skb succeeded the cgroup2 descendant test
440 * < 0 error
441 *
442 * u32 bpf_get_hash_recalc(skb)
443 * Retrieve and possibly recalculate skb->hash.
444 * @skb: pointer to skb
445 * Return: hash
446 *
447 * u64 bpf_get_current_task(void)
448 * Returns current task_struct
449 * Return: current
450 *
451 * int bpf_probe_write_user(void *dst, void *src, int len)
452 * safely attempt to write to a location
453 * @dst: destination address in userspace
454 * @src: source address on stack
455 * @len: number of bytes to copy
456 * Return: 0 on success or negative error
457 *
458 * int bpf_current_task_under_cgroup(map, index)
459 * Check cgroup2 membership of current task
460 * @map: pointer to bpf_map in BPF_MAP_TYPE_CGROUP_ARRAY type
461 * @index: index of the cgroup in the bpf_map
462 * Return:
463 * == 0 current failed the cgroup2 descendant test
464 * == 1 current succeeded the cgroup2 descendant test
465 * < 0 error
466 *
467 * int bpf_skb_change_tail(skb, len, flags)
468 * The helper will resize the skb to the given new size, to be used f.e.
469 * with control messages.
470 * @skb: pointer to skb
471 * @len: new skb length
472 * @flags: reserved
473 * Return: 0 on success or negative error
474 *
475 * int bpf_skb_pull_data(skb, len)
476 * The helper will pull in non-linear data in case the skb is non-linear
477 * and not all of len are part of the linear section. Only needed for
478 * read/write with direct packet access.
479 * @skb: pointer to skb
480 * @len: len to make read/writeable
481 * Return: 0 on success or negative error
482 *
483 * s64 bpf_csum_update(skb, csum)
484 * Adds csum into skb->csum in case of CHECKSUM_COMPLETE.
485 * @skb: pointer to skb
486 * @csum: csum to add
487 * Return: csum on success or negative error
488 *
489 * void bpf_set_hash_invalid(skb)
490 * Invalidate current skb->hash.
491 * @skb: pointer to skb
492 *
493 * int bpf_get_numa_node_id()
494 * Return: Id of current NUMA node.
495 *
496 * int bpf_skb_change_head()
497 * Grows headroom of skb and adjusts MAC header offset accordingly.
498 * Will extends/reallocae as required automatically.
499 * May change skb data pointer and will thus invalidate any check
500 * performed for direct packet access.
501 * @skb: pointer to skb
502 * @len: length of header to be pushed in front
503 * @flags: Flags (unused for now)
504 * Return: 0 on success or negative error
505 *
506 * int bpf_xdp_adjust_head(xdp_md, delta)
507 * Adjust the xdp_md.data by delta
508 * @xdp_md: pointer to xdp_md
509 * @delta: An positive/negative integer to be added to xdp_md.data
510 * Return: 0 on success or negative on error
Mickaël Salaün9a738262017-02-10 00:21:35 +0100511 *
512 * int bpf_probe_read_str(void *dst, int size, const void *unsafe_ptr)
513 * Copy a NUL terminated string from unsafe address. In case the string
514 * length is smaller than size, the target is not padded with further NUL
515 * bytes. In case the string length is larger than size, just count-1
516 * bytes are copied and the last byte is set to NUL.
517 * @dst: destination address
518 * @size: maximum number of bytes to copy, including the trailing NUL
519 * @unsafe_ptr: unsafe address
520 * Return:
521 * > 0 length of the string including the trailing NUL on success
522 * < 0 error
Alexei Starovoitov30848872017-03-30 21:45:39 -0700523 *
Alexander Alemayhu3c60a532017-04-08 22:08:10 +0200524 * u64 bpf_get_socket_cookie(skb)
Alexei Starovoitov30848872017-03-30 21:45:39 -0700525 * Get the cookie for the socket stored inside sk_buff.
526 * @skb: pointer to skb
527 * Return: 8 Bytes non-decreasing number on success or 0 if the socket
528 * field is missing inside sk_buff
529 *
530 * u32 bpf_get_socket_uid(skb)
531 * Get the owner uid of the socket stored inside sk_buff.
532 * @skb: pointer to skb
David S. Millere07b98d2017-05-10 11:38:07 -0700533 * Return: uid of the socket owner on success or overflowuid if failed.
Daniel Borkmannded092c2017-06-11 00:50:47 +0200534 *
535 * u32 bpf_set_hash(skb, hash)
536 * Set full skb->hash.
537 * @skb: pointer to skb
538 * @hash: hash to set
Lawrence Brakmo04df41e2017-06-30 20:02:55 -0700539 *
540 * int bpf_setsockopt(bpf_socket, level, optname, optval, optlen)
541 * Calls setsockopt. Not all opts are available, only those with
542 * integer optvals plus TCP_CONGESTION.
543 * Supported levels: SOL_SOCKET and IPROTO_TCP
544 * @bpf_socket: pointer to bpf_socket
545 * @level: SOL_SOCKET or IPROTO_TCP
546 * @optname: option name
547 * @optval: pointer to option value
548 * @optlen: length of optval in byes
549 * Return: 0 or negative error
Daniel Borkmann2be7e212017-07-02 02:13:26 +0200550 *
551 * int bpf_skb_adjust_room(skb, len_diff, mode, flags)
552 * Grow or shrink room in sk_buff.
553 * @skb: pointer to skb
554 * @len_diff: (signed) amount of room to grow/shrink
555 * @mode: operation mode (enum bpf_adj_room_mode)
556 * @flags: reserved for future use
557 * Return: 0 on success or negative error code
John Fastabend69e8cc12017-08-15 22:33:32 -0700558 *
559 * int bpf_sk_redirect_map(map, key, flags)
560 * Redirect skb to a sock in map using key as a lookup key for the
561 * sock in map.
562 * @map: pointer to sockmap
563 * @key: key to lookup sock in map
564 * @flags: reserved for future use
565 * Return: SK_REDIRECT
566 *
567 * int bpf_sock_map_update(skops, map, key, flags, map_flags)
568 * @skops: pointer to bpf_sock_ops
569 * @map: pointer to sockmap to update
570 * @key: key to insert/update sock in map
571 * @flags: same flags as map update elem
572 * @map_flags: sock map specific flags
573 * bit 1: Enable strparser
574 * other bits: reserved
Joe Stringer0cb34dc2016-12-08 18:46:14 -0800575 */
576#define __BPF_FUNC_MAPPER(FN) \
577 FN(unspec), \
578 FN(map_lookup_elem), \
579 FN(map_update_elem), \
580 FN(map_delete_elem), \
581 FN(probe_read), \
582 FN(ktime_get_ns), \
583 FN(trace_printk), \
584 FN(get_prandom_u32), \
585 FN(get_smp_processor_id), \
586 FN(skb_store_bytes), \
587 FN(l3_csum_replace), \
588 FN(l4_csum_replace), \
589 FN(tail_call), \
590 FN(clone_redirect), \
591 FN(get_current_pid_tgid), \
592 FN(get_current_uid_gid), \
593 FN(get_current_comm), \
594 FN(get_cgroup_classid), \
595 FN(skb_vlan_push), \
596 FN(skb_vlan_pop), \
597 FN(skb_get_tunnel_key), \
598 FN(skb_set_tunnel_key), \
599 FN(perf_event_read), \
600 FN(redirect), \
601 FN(get_route_realm), \
602 FN(perf_event_output), \
603 FN(skb_load_bytes), \
604 FN(get_stackid), \
605 FN(csum_diff), \
606 FN(skb_get_tunnel_opt), \
607 FN(skb_set_tunnel_opt), \
608 FN(skb_change_proto), \
609 FN(skb_change_type), \
610 FN(skb_under_cgroup), \
611 FN(get_hash_recalc), \
612 FN(get_current_task), \
613 FN(probe_write_user), \
614 FN(current_task_under_cgroup), \
615 FN(skb_change_tail), \
616 FN(skb_pull_data), \
617 FN(csum_update), \
618 FN(set_hash_invalid), \
619 FN(get_numa_node_id), \
620 FN(skb_change_head), \
Mickaël Salaün9a738262017-02-10 00:21:35 +0100621 FN(xdp_adjust_head), \
Chenbo Feng91b82702017-03-22 17:27:34 -0700622 FN(probe_read_str), \
Chenbo Feng6acc5c22017-03-22 17:27:35 -0700623 FN(get_socket_cookie), \
Daniel Borkmannded092c2017-06-11 00:50:47 +0200624 FN(get_socket_uid), \
Lawrence Brakmo04df41e2017-06-30 20:02:55 -0700625 FN(set_hash), \
Daniel Borkmann2be7e212017-07-02 02:13:26 +0200626 FN(setsockopt), \
William Tu996139e2017-08-07 13:14:42 -0700627 FN(skb_adjust_room), \
John Fastabend69e8cc12017-08-15 22:33:32 -0700628 FN(redirect_map), \
629 FN(sk_redirect_map), \
630 FN(sock_map_update),
Joe Stringer0cb34dc2016-12-08 18:46:14 -0800631
Arnaldo Carvalho de Melo971e8272016-07-11 16:38:05 -0300632/* integer value in 'imm' field of BPF_CALL instruction selects which helper
633 * function eBPF program intends to call
634 */
Joe Stringer0cb34dc2016-12-08 18:46:14 -0800635#define __BPF_ENUM_FN(x) BPF_FUNC_ ## x
Arnaldo Carvalho de Melo971e8272016-07-11 16:38:05 -0300636enum bpf_func_id {
Joe Stringer0cb34dc2016-12-08 18:46:14 -0800637 __BPF_FUNC_MAPPER(__BPF_ENUM_FN)
Arnaldo Carvalho de Melo971e8272016-07-11 16:38:05 -0300638 __BPF_FUNC_MAX_ID,
639};
Joe Stringer0cb34dc2016-12-08 18:46:14 -0800640#undef __BPF_ENUM_FN
Arnaldo Carvalho de Melo971e8272016-07-11 16:38:05 -0300641
642/* All flags used by eBPF helper functions, placed here. */
643
644/* BPF_FUNC_skb_store_bytes flags. */
645#define BPF_F_RECOMPUTE_CSUM (1ULL << 0)
646#define BPF_F_INVALIDATE_HASH (1ULL << 1)
647
648/* BPF_FUNC_l3_csum_replace and BPF_FUNC_l4_csum_replace flags.
649 * First 4 bits are for passing the header field size.
650 */
651#define BPF_F_HDR_FIELD_MASK 0xfULL
652
653/* BPF_FUNC_l4_csum_replace flags. */
654#define BPF_F_PSEUDO_HDR (1ULL << 4)
655#define BPF_F_MARK_MANGLED_0 (1ULL << 5)
Mickaël Salaün9a738262017-02-10 00:21:35 +0100656#define BPF_F_MARK_ENFORCE (1ULL << 6)
Arnaldo Carvalho de Melo971e8272016-07-11 16:38:05 -0300657
658/* BPF_FUNC_clone_redirect and BPF_FUNC_redirect flags. */
659#define BPF_F_INGRESS (1ULL << 0)
660
661/* BPF_FUNC_skb_set_tunnel_key and BPF_FUNC_skb_get_tunnel_key flags. */
662#define BPF_F_TUNINFO_IPV6 (1ULL << 0)
663
664/* BPF_FUNC_get_stackid flags. */
665#define BPF_F_SKIP_FIELD_MASK 0xffULL
666#define BPF_F_USER_STACK (1ULL << 8)
667#define BPF_F_FAST_STACK_CMP (1ULL << 9)
668#define BPF_F_REUSE_STACKID (1ULL << 10)
669
670/* BPF_FUNC_skb_set_tunnel_key flags. */
671#define BPF_F_ZERO_CSUM_TX (1ULL << 1)
672#define BPF_F_DONT_FRAGMENT (1ULL << 2)
673
Arnaldo Carvalho de Melo791cceb2016-08-09 11:48:07 -0300674/* BPF_FUNC_perf_event_output and BPF_FUNC_perf_event_read flags. */
Arnaldo Carvalho de Melo971e8272016-07-11 16:38:05 -0300675#define BPF_F_INDEX_MASK 0xffffffffULL
676#define BPF_F_CURRENT_CPU BPF_F_INDEX_MASK
Arnaldo Carvalho de Melo791cceb2016-08-09 11:48:07 -0300677/* BPF_FUNC_perf_event_output for sk_buff input context. */
678#define BPF_F_CTXLEN_MASK (0xfffffULL << 32)
Arnaldo Carvalho de Melo971e8272016-07-11 16:38:05 -0300679
Daniel Borkmann2be7e212017-07-02 02:13:26 +0200680/* Mode for BPF_FUNC_skb_adjust_room helper. */
681enum bpf_adj_room_mode {
682 BPF_ADJ_ROOM_NET_OPTS,
683};
684
Arnaldo Carvalho de Melo971e8272016-07-11 16:38:05 -0300685/* user accessible mirror of in-kernel sk_buff.
686 * new fields can only be added to the end of this structure
687 */
688struct __sk_buff {
689 __u32 len;
690 __u32 pkt_type;
691 __u32 mark;
692 __u32 queue_mapping;
693 __u32 protocol;
694 __u32 vlan_present;
695 __u32 vlan_tci;
696 __u32 vlan_proto;
697 __u32 priority;
698 __u32 ingress_ifindex;
699 __u32 ifindex;
700 __u32 tc_index;
701 __u32 cb[5];
702 __u32 hash;
703 __u32 tc_classid;
704 __u32 data;
705 __u32 data_end;
Daniel Borkmannb1d9fc42017-04-19 23:01:17 +0200706 __u32 napi_id;
John Fastabend69e8cc12017-08-15 22:33:32 -0700707
708 /* accessed by BPF_PROG_TYPE_sk_skb types */
709 __u32 family;
710 __u32 remote_ip4; /* Stored in network byte order */
711 __u32 local_ip4; /* Stored in network byte order */
712 __u32 remote_ip6[4]; /* Stored in network byte order */
713 __u32 local_ip6[4]; /* Stored in network byte order */
714 __u32 remote_port; /* Stored in network byte order */
715 __u32 local_port; /* stored in host byte order */
Arnaldo Carvalho de Melo971e8272016-07-11 16:38:05 -0300716};
717
718struct bpf_tunnel_key {
719 __u32 tunnel_id;
720 union {
721 __u32 remote_ipv4;
722 __u32 remote_ipv6[4];
723 };
724 __u8 tunnel_tos;
725 __u8 tunnel_ttl;
726 __u16 tunnel_ext;
727 __u32 tunnel_label;
728};
729
Joe Stringer0cb34dc2016-12-08 18:46:14 -0800730/* Generic BPF return codes which all BPF program types may support.
731 * The values are binary compatible with their TC_ACT_* counter-part to
732 * provide backwards compatibility with existing SCHED_CLS and SCHED_ACT
733 * programs.
734 *
735 * XDP is handled seprately, see XDP_*.
736 */
737enum bpf_ret_code {
738 BPF_OK = 0,
739 /* 1 reserved */
740 BPF_DROP = 2,
741 /* 3-6 reserved */
742 BPF_REDIRECT = 7,
743 /* >127 are reserved for prog type specific return codes */
744};
745
746struct bpf_sock {
747 __u32 bound_dev_if;
748 __u32 family;
749 __u32 type;
750 __u32 protocol;
751};
752
753#define XDP_PACKET_HEADROOM 256
754
Arnaldo Carvalho de Melo791cceb2016-08-09 11:48:07 -0300755/* User return codes for XDP prog type.
756 * A valid XDP program must return one of these defined values. All other
757 * return codes are reserved for future use. Unknown return codes will result
758 * in packet drop.
759 */
760enum xdp_action {
761 XDP_ABORTED = 0,
762 XDP_DROP,
763 XDP_PASS,
764 XDP_TX,
765};
766
767/* user accessible metadata for XDP packet hook
768 * new fields must be added to the end of this structure
769 */
770struct xdp_md {
771 __u32 data;
772 __u32 data_end;
773};
774
John Fastabend69e8cc12017-08-15 22:33:32 -0700775enum sk_action {
776 SK_ABORTED = 0,
777 SK_DROP,
778 SK_REDIRECT,
779};
780
Martin KaFai Lau95b9afd2017-06-05 12:15:53 -0700781#define BPF_TAG_SIZE 8
782
783struct bpf_prog_info {
784 __u32 type;
785 __u32 id;
786 __u8 tag[BPF_TAG_SIZE];
787 __u32 jited_prog_len;
788 __u32 xlated_prog_len;
789 __aligned_u64 jited_prog_insns;
790 __aligned_u64 xlated_prog_insns;
791} __attribute__((aligned(8)));
792
793struct bpf_map_info {
794 __u32 type;
795 __u32 id;
796 __u32 key_size;
797 __u32 value_size;
798 __u32 max_entries;
799 __u32 map_flags;
800} __attribute__((aligned(8)));
801
Lawrence Brakmo04df41e2017-06-30 20:02:55 -0700802/* User bpf_sock_ops struct to access socket values and specify request ops
803 * and their replies.
804 * New fields can only be added at the end of this structure
805 */
806struct bpf_sock_ops {
807 __u32 op;
808 union {
809 __u32 reply;
810 __u32 replylong[4];
811 };
812 __u32 family;
813 __u32 remote_ip4;
814 __u32 local_ip4;
815 __u32 remote_ip6[4];
816 __u32 local_ip6[4];
817 __u32 remote_port;
818 __u32 local_port;
819};
820
821/* List of known BPF sock_ops operators.
822 * New entries can only be added at the end
823 */
824enum {
825 BPF_SOCK_OPS_VOID,
826 BPF_SOCK_OPS_TIMEOUT_INIT, /* Should return SYN-RTO value to use or
827 * -1 if default value should be used
828 */
829 BPF_SOCK_OPS_RWND_INIT, /* Should return initial advertized
830 * window (in packets) or -1 if default
831 * value should be used
832 */
833 BPF_SOCK_OPS_TCP_CONNECT_CB, /* Calls BPF program right before an
834 * active connection is initialized
835 */
836 BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB, /* Calls BPF program when an
837 * active connection is
838 * established
839 */
840 BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB, /* Calls BPF program when a
841 * passive connection is
842 * established
843 */
844 BPF_SOCK_OPS_NEEDS_ECN, /* If connection's congestion control
845 * needs ECN
846 */
847};
848
849#define TCP_BPF_IW 1001 /* Set TCP initial congestion window */
850#define TCP_BPF_SNDCWND_CLAMP 1002 /* Set sndcwnd_clamp */
851
Arnaldo Carvalho de Melo971e8272016-07-11 16:38:05 -0300852#endif /* _UAPI__LINUX_BPF_H__ */