1/* 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
33#define BPF_JNE		0x50	/* jump != */
34#define BPF_JSGT	0x60	/* SGT is signed '>', GT in x86 */
35#define BPF_JSGE	0x70	/* SGE is signed '>=', GE in x86 */
36#define BPF_CALL	0x80	/* function call */
37#define BPF_EXIT	0x90	/* function return */
38
39/* Register numbers */
40enum {
41	BPF_REG_0 = 0,
42	BPF_REG_1,
43	BPF_REG_2,
44	BPF_REG_3,
45	BPF_REG_4,
46	BPF_REG_5,
47	BPF_REG_6,
48	BPF_REG_7,
49	BPF_REG_8,
50	BPF_REG_9,
51	BPF_REG_10,
52	__MAX_BPF_REG,
53};
54
55/* BPF has 10 general purpose 64-bit registers and stack frame. */
56#define MAX_BPF_REG	__MAX_BPF_REG
57
58struct bpf_insn {
59	__u8	code;		/* opcode */
60	__u8	dst_reg:4;	/* dest register */
61	__u8	src_reg:4;	/* source register */
62	__s16	off;		/* signed offset */
63	__s32	imm;		/* signed immediate constant */
64};
65
66/* BPF syscall commands, see bpf(2) man-page for details. */
67enum bpf_cmd {
68	BPF_MAP_CREATE,
69	BPF_MAP_LOOKUP_ELEM,
70	BPF_MAP_UPDATE_ELEM,
71	BPF_MAP_DELETE_ELEM,
72	BPF_MAP_GET_NEXT_KEY,
73	BPF_PROG_LOAD,
74	BPF_OBJ_PIN,
75	BPF_OBJ_GET,
76	BPF_PROG_ATTACH,
77	BPF_PROG_DETACH,
78};
79
80enum bpf_map_type {
81	BPF_MAP_TYPE_UNSPEC,
82	BPF_MAP_TYPE_HASH,
83	BPF_MAP_TYPE_ARRAY,
84	BPF_MAP_TYPE_PROG_ARRAY,
85	BPF_MAP_TYPE_PERF_EVENT_ARRAY,
86	BPF_MAP_TYPE_PERCPU_HASH,
87	BPF_MAP_TYPE_PERCPU_ARRAY,
88	BPF_MAP_TYPE_STACK_TRACE,
89	BPF_MAP_TYPE_CGROUP_ARRAY,
90	BPF_MAP_TYPE_LRU_HASH,
91	BPF_MAP_TYPE_LRU_PERCPU_HASH,
92};
93
94enum bpf_prog_type {
95	BPF_PROG_TYPE_UNSPEC,
96	BPF_PROG_TYPE_SOCKET_FILTER,
97	BPF_PROG_TYPE_KPROBE,
98	BPF_PROG_TYPE_SCHED_CLS,
99	BPF_PROG_TYPE_SCHED_ACT,
100	BPF_PROG_TYPE_TRACEPOINT,
101	BPF_PROG_TYPE_XDP,
102	BPF_PROG_TYPE_PERF_EVENT,
103	BPF_PROG_TYPE_CGROUP_SKB,
104	BPF_PROG_TYPE_CGROUP_SOCK,
105	BPF_PROG_TYPE_LWT_IN,
106	BPF_PROG_TYPE_LWT_OUT,
107	BPF_PROG_TYPE_LWT_XMIT,
108};
109
110enum bpf_attach_type {
111	BPF_CGROUP_INET_INGRESS,
112	BPF_CGROUP_INET_EGRESS,
113	BPF_CGROUP_INET_SOCK_CREATE,
114	__MAX_BPF_ATTACH_TYPE
115};
116
117#define MAX_BPF_ATTACH_TYPE __MAX_BPF_ATTACH_TYPE
118
119/* If BPF_F_ALLOW_OVERRIDE flag is used in BPF_PROG_ATTACH command
120 * to the given target_fd cgroup the descendent cgroup will be able to
121 * override effective bpf program that was inherited from this cgroup
122 */
123#define BPF_F_ALLOW_OVERRIDE	(1U << 0)
124
125#define BPF_PSEUDO_MAP_FD	1
126
127/* flags for BPF_MAP_UPDATE_ELEM command */
128#define BPF_ANY		0 /* create new element or update existing */
129#define BPF_NOEXIST	1 /* create new element if it didn't exist */
130#define BPF_EXIST	2 /* update existing element */
131
132#define BPF_F_NO_PREALLOC	(1U << 0)
133/* Instead of having one common LRU list in the
134 * BPF_MAP_TYPE_LRU_[PERCPU_]HASH map, use a percpu LRU list
135 * which can scale and perform better.
136 * Note, the LRU nodes (including free nodes) cannot be moved
137 * across different LRU lists.
138 */
139#define BPF_F_NO_COMMON_LRU	(1U << 1)
140
141union bpf_attr {
142	struct { /* anonymous struct used by BPF_MAP_CREATE command */
143		__u32	map_type;	/* one of enum bpf_map_type */
144		__u32	key_size;	/* size of key in bytes */
145		__u32	value_size;	/* size of value in bytes */
146		__u32	max_entries;	/* max number of entries in a map */
147		__u32	map_flags;	/* prealloc or not */
148	};
149
150	struct { /* anonymous struct used by BPF_MAP_*_ELEM commands */
151		__u32		map_fd;
152		__aligned_u64	key;
153		union {
154			__aligned_u64 value;
155			__aligned_u64 next_key;
156		};
157		__u64		flags;
158	};
159
160	struct { /* anonymous struct used by BPF_PROG_LOAD command */
161		__u32		prog_type;	/* one of enum bpf_prog_type */
162		__u32		insn_cnt;
163		__aligned_u64	insns;
164		__aligned_u64	license;
165		__u32		log_level;	/* verbosity level of verifier */
166		__u32		log_size;	/* size of user buffer */
167		__aligned_u64	log_buf;	/* user supplied buffer */
168		__u32		kern_version;	/* checked when prog_type=kprobe */
169	};
170
171	struct { /* anonymous struct used by BPF_OBJ_* commands */
172		__aligned_u64	pathname;
173		__u32		bpf_fd;
174	};
175
176	struct { /* anonymous struct used by BPF_PROG_ATTACH/DETACH commands */
177		__u32		target_fd;	/* container object to attach to */
178		__u32		attach_bpf_fd;	/* eBPF program to attach */
179		__u32		attach_type;
180		__u32		attach_flags;
181	};
182} __attribute__((aligned(8)));
183
184/* BPF helper function descriptions:
185 *
186 * void *bpf_map_lookup_elem(&map, &key)
187 *     Return: Map value or NULL
188 *
189 * int bpf_map_update_elem(&map, &key, &value, flags)
190 *     Return: 0 on success or negative error
191 *
192 * int bpf_map_delete_elem(&map, &key)
193 *     Return: 0 on success or negative error
194 *
195 * int bpf_probe_read(void *dst, int size, void *src)
196 *     Return: 0 on success or negative error
197 *
198 * u64 bpf_ktime_get_ns(void)
199 *     Return: current ktime
200 *
201 * int bpf_trace_printk(const char *fmt, int fmt_size, ...)
202 *     Return: length of buffer written or negative error
203 *
204 * u32 bpf_prandom_u32(void)
205 *     Return: random value
206 *
207 * u32 bpf_raw_smp_processor_id(void)
208 *     Return: SMP processor ID
209 *
210 * int bpf_skb_store_bytes(skb, offset, from, len, flags)
211 *     store bytes into packet
212 *     @skb: pointer to skb
213 *     @offset: offset within packet from skb->mac_header
214 *     @from: pointer where to copy bytes from
215 *     @len: number of bytes to store into packet
216 *     @flags: bit 0 - if true, recompute skb->csum
217 *             other bits - reserved
218 *     Return: 0 on success or negative error
219 *
220 * int bpf_l3_csum_replace(skb, offset, from, to, flags)
221 *     recompute IP checksum
222 *     @skb: pointer to skb
223 *     @offset: offset within packet where IP checksum is located
224 *     @from: old value of header field
225 *     @to: new value of header field
226 *     @flags: bits 0-3 - size of header field
227 *             other bits - reserved
228 *     Return: 0 on success or negative error
229 *
230 * int bpf_l4_csum_replace(skb, offset, from, to, flags)
231 *     recompute TCP/UDP checksum
232 *     @skb: pointer to skb
233 *     @offset: offset within packet where TCP/UDP checksum is located
234 *     @from: old value of header field
235 *     @to: new value of header field
236 *     @flags: bits 0-3 - size of header field
237 *             bit 4 - is pseudo header
238 *             other bits - reserved
239 *     Return: 0 on success or negative error
240 *
241 * int bpf_tail_call(ctx, prog_array_map, index)
242 *     jump into another BPF program
243 *     @ctx: context pointer passed to next program
244 *     @prog_array_map: pointer to map which type is BPF_MAP_TYPE_PROG_ARRAY
245 *     @index: index inside array that selects specific program to run
246 *     Return: 0 on success or negative error
247 *
248 * int bpf_clone_redirect(skb, ifindex, flags)
249 *     redirect to another netdev
250 *     @skb: pointer to skb
251 *     @ifindex: ifindex of the net device
252 *     @flags: bit 0 - if set, redirect to ingress instead of egress
253 *             other bits - reserved
254 *     Return: 0 on success or negative error
255 *
256 * u64 bpf_get_current_pid_tgid(void)
257 *     Return: current->tgid << 32 | current->pid
258 *
259 * u64 bpf_get_current_uid_gid(void)
260 *     Return: current_gid << 32 | current_uid
261 *
262 * int bpf_get_current_comm(char *buf, int size_of_buf)
263 *     stores current->comm into buf
264 *     Return: 0 on success or negative error
265 *
266 * u32 bpf_get_cgroup_classid(skb)
267 *     retrieve a proc's classid
268 *     @skb: pointer to skb
269 *     Return: classid if != 0
270 *
271 * int bpf_skb_vlan_push(skb, vlan_proto, vlan_tci)
272 *     Return: 0 on success or negative error
273 *
274 * int bpf_skb_vlan_pop(skb)
275 *     Return: 0 on success or negative error
276 *
277 * int bpf_skb_get_tunnel_key(skb, key, size, flags)
278 * int bpf_skb_set_tunnel_key(skb, key, size, flags)
279 *     retrieve or populate tunnel metadata
280 *     @skb: pointer to skb
281 *     @key: pointer to 'struct bpf_tunnel_key'
282 *     @size: size of 'struct bpf_tunnel_key'
283 *     @flags: room for future extensions
284 *     Return: 0 on success or negative error
285 *
286 * u64 bpf_perf_event_read(&map, index)
287 *     Return: Number events read or error code
288 *
289 * int bpf_redirect(ifindex, flags)
290 *     redirect to another netdev
291 *     @ifindex: ifindex of the net device
292 *     @flags: bit 0 - if set, redirect to ingress instead of egress
293 *             other bits - reserved
294 *     Return: TC_ACT_REDIRECT
295 *
296 * u32 bpf_get_route_realm(skb)
297 *     retrieve a dst's tclassid
298 *     @skb: pointer to skb
299 *     Return: realm if != 0
300 *
301 * int bpf_perf_event_output(ctx, map, index, data, size)
302 *     output perf raw sample
303 *     @ctx: struct pt_regs*
304 *     @map: pointer to perf_event_array map
305 *     @index: index of event in the map
306 *     @data: data on stack to be output as raw data
307 *     @size: size of data
308 *     Return: 0 on success or negative error
309 *
310 * int bpf_get_stackid(ctx, map, flags)
311 *     walk user or kernel stack and return id
312 *     @ctx: struct pt_regs*
313 *     @map: pointer to stack_trace map
314 *     @flags: bits 0-7 - numer of stack frames to skip
315 *             bit 8 - collect user stack instead of kernel
316 *             bit 9 - compare stacks by hash only
317 *             bit 10 - if two different stacks hash into the same stackid
318 *                      discard old
319 *             other bits - reserved
320 *     Return: >= 0 stackid on success or negative error
321 *
322 * s64 bpf_csum_diff(from, from_size, to, to_size, seed)
323 *     calculate csum diff
324 *     @from: raw from buffer
325 *     @from_size: length of from buffer
326 *     @to: raw to buffer
327 *     @to_size: length of to buffer
328 *     @seed: optional seed
329 *     Return: csum result or negative error code
330 *
331 * int bpf_skb_get_tunnel_opt(skb, opt, size)
332 *     retrieve tunnel options metadata
333 *     @skb: pointer to skb
334 *     @opt: pointer to raw tunnel option data
335 *     @size: size of @opt
336 *     Return: option size
337 *
338 * int bpf_skb_set_tunnel_opt(skb, opt, size)
339 *     populate tunnel options metadata
340 *     @skb: pointer to skb
341 *     @opt: pointer to raw tunnel option data
342 *     @size: size of @opt
343 *     Return: 0 on success or negative error
344 *
345 * int bpf_skb_change_proto(skb, proto, flags)
346 *     Change protocol of the skb. Currently supported is v4 -> v6,
347 *     v6 -> v4 transitions. The helper will also resize the skb. eBPF
348 *     program is expected to fill the new headers via skb_store_bytes
349 *     and lX_csum_replace.
350 *     @skb: pointer to skb
351 *     @proto: new skb->protocol type
352 *     @flags: reserved
353 *     Return: 0 on success or negative error
354 *
355 * int bpf_skb_change_type(skb, type)
356 *     Change packet type of skb.
357 *     @skb: pointer to skb
358 *     @type: new skb->pkt_type type
359 *     Return: 0 on success or negative error
360 *
361 * int bpf_skb_under_cgroup(skb, map, index)
362 *     Check cgroup2 membership of skb
363 *     @skb: pointer to skb
364 *     @map: pointer to bpf_map in BPF_MAP_TYPE_CGROUP_ARRAY type
365 *     @index: index of the cgroup in the bpf_map
366 *     Return:
367 *       == 0 skb failed the cgroup2 descendant test
368 *       == 1 skb succeeded the cgroup2 descendant test
369 *        < 0 error
370 *
371 * u32 bpf_get_hash_recalc(skb)
372 *     Retrieve and possibly recalculate skb->hash.
373 *     @skb: pointer to skb
374 *     Return: hash
375 *
376 * u64 bpf_get_current_task(void)
377 *     Returns current task_struct
378 *     Return: current
379 *
380 * int bpf_probe_write_user(void *dst, void *src, int len)
381 *     safely attempt to write to a location
382 *     @dst: destination address in userspace
383 *     @src: source address on stack
384 *     @len: number of bytes to copy
385 *     Return: 0 on success or negative error
386 *
387 * int bpf_current_task_under_cgroup(map, index)
388 *     Check cgroup2 membership of current task
389 *     @map: pointer to bpf_map in BPF_MAP_TYPE_CGROUP_ARRAY type
390 *     @index: index of the cgroup in the bpf_map
391 *     Return:
392 *       == 0 current failed the cgroup2 descendant test
393 *       == 1 current succeeded the cgroup2 descendant test
394 *        < 0 error
395 *
396 * int bpf_skb_change_tail(skb, len, flags)
397 *     The helper will resize the skb to the given new size, to be used f.e.
398 *     with control messages.
399 *     @skb: pointer to skb
400 *     @len: new skb length
401 *     @flags: reserved
402 *     Return: 0 on success or negative error
403 *
404 * int bpf_skb_pull_data(skb, len)
405 *     The helper will pull in non-linear data in case the skb is non-linear
406 *     and not all of len are part of the linear section. Only needed for
407 *     read/write with direct packet access.
408 *     @skb: pointer to skb
409 *     @len: len to make read/writeable
410 *     Return: 0 on success or negative error
411 *
412 * s64 bpf_csum_update(skb, csum)
413 *     Adds csum into skb->csum in case of CHECKSUM_COMPLETE.
414 *     @skb: pointer to skb
415 *     @csum: csum to add
416 *     Return: csum on success or negative error
417 *
418 * void bpf_set_hash_invalid(skb)
419 *     Invalidate current skb->hash.
420 *     @skb: pointer to skb
421 *
422 * int bpf_get_numa_node_id()
423 *     Return: Id of current NUMA node.
424 *
425 * int bpf_skb_change_head()
426 *     Grows headroom of skb and adjusts MAC header offset accordingly.
427 *     Will extends/reallocae as required automatically.
428 *     May change skb data pointer and will thus invalidate any check
429 *     performed for direct packet access.
430 *     @skb: pointer to skb
431 *     @len: length of header to be pushed in front
432 *     @flags: Flags (unused for now)
433 *     Return: 0 on success or negative error
434 *
435 * int bpf_xdp_adjust_head(xdp_md, delta)
436 *     Adjust the xdp_md.data by delta
437 *     @xdp_md: pointer to xdp_md
438 *     @delta: An positive/negative integer to be added to xdp_md.data
439 *     Return: 0 on success or negative on error
440 */
441#define __BPF_FUNC_MAPPER(FN)		\
442	FN(unspec),			\
443	FN(map_lookup_elem),		\
444	FN(map_update_elem),		\
445	FN(map_delete_elem),		\
446	FN(probe_read),			\
447	FN(ktime_get_ns),		\
448	FN(trace_printk),		\
449	FN(get_prandom_u32),		\
450	FN(get_smp_processor_id),	\
451	FN(skb_store_bytes),		\
452	FN(l3_csum_replace),		\
453	FN(l4_csum_replace),		\
454	FN(tail_call),			\
455	FN(clone_redirect),		\
456	FN(get_current_pid_tgid),	\
457	FN(get_current_uid_gid),	\
458	FN(get_current_comm),		\
459	FN(get_cgroup_classid),		\
460	FN(skb_vlan_push),		\
461	FN(skb_vlan_pop),		\
462	FN(skb_get_tunnel_key),		\
463	FN(skb_set_tunnel_key),		\
464	FN(perf_event_read),		\
465	FN(redirect),			\
466	FN(get_route_realm),		\
467	FN(perf_event_output),		\
468	FN(skb_load_bytes),		\
469	FN(get_stackid),		\
470	FN(csum_diff),			\
471	FN(skb_get_tunnel_opt),		\
472	FN(skb_set_tunnel_opt),		\
473	FN(skb_change_proto),		\
474	FN(skb_change_type),		\
475	FN(skb_under_cgroup),		\
476	FN(get_hash_recalc),		\
477	FN(get_current_task),		\
478	FN(probe_write_user),		\
479	FN(current_task_under_cgroup),	\
480	FN(skb_change_tail),		\
481	FN(skb_pull_data),		\
482	FN(csum_update),		\
483	FN(set_hash_invalid),		\
484	FN(get_numa_node_id),		\
485	FN(skb_change_head),		\
486	FN(xdp_adjust_head),
487
488/* integer value in 'imm' field of BPF_CALL instruction selects which helper
489 * function eBPF program intends to call
490 */
491#define __BPF_ENUM_FN(x) BPF_FUNC_ ## x
492enum bpf_func_id {
493	__BPF_FUNC_MAPPER(__BPF_ENUM_FN)
494	__BPF_FUNC_MAX_ID,
495};
496#undef __BPF_ENUM_FN
497
498/* All flags used by eBPF helper functions, placed here. */
499
500/* BPF_FUNC_skb_store_bytes flags. */
501#define BPF_F_RECOMPUTE_CSUM		(1ULL << 0)
502#define BPF_F_INVALIDATE_HASH		(1ULL << 1)
503
504/* BPF_FUNC_l3_csum_replace and BPF_FUNC_l4_csum_replace flags.
505 * First 4 bits are for passing the header field size.
506 */
507#define BPF_F_HDR_FIELD_MASK		0xfULL
508
509/* BPF_FUNC_l4_csum_replace flags. */
510#define BPF_F_PSEUDO_HDR		(1ULL << 4)
511#define BPF_F_MARK_MANGLED_0		(1ULL << 5)
512
513/* BPF_FUNC_clone_redirect and BPF_FUNC_redirect flags. */
514#define BPF_F_INGRESS			(1ULL << 0)
515
516/* BPF_FUNC_skb_set_tunnel_key and BPF_FUNC_skb_get_tunnel_key flags. */
517#define BPF_F_TUNINFO_IPV6		(1ULL << 0)
518
519/* BPF_FUNC_get_stackid flags. */
520#define BPF_F_SKIP_FIELD_MASK		0xffULL
521#define BPF_F_USER_STACK		(1ULL << 8)
522#define BPF_F_FAST_STACK_CMP		(1ULL << 9)
523#define BPF_F_REUSE_STACKID		(1ULL << 10)
524
525/* BPF_FUNC_skb_set_tunnel_key flags. */
526#define BPF_F_ZERO_CSUM_TX		(1ULL << 1)
527#define BPF_F_DONT_FRAGMENT		(1ULL << 2)
528
529/* BPF_FUNC_perf_event_output and BPF_FUNC_perf_event_read flags. */
530#define BPF_F_INDEX_MASK		0xffffffffULL
531#define BPF_F_CURRENT_CPU		BPF_F_INDEX_MASK
532/* BPF_FUNC_perf_event_output for sk_buff input context. */
533#define BPF_F_CTXLEN_MASK		(0xfffffULL << 32)
534
535/* user accessible mirror of in-kernel sk_buff.
536 * new fields can only be added to the end of this structure
537 */
538struct __sk_buff {
539	__u32 len;
540	__u32 pkt_type;
541	__u32 mark;
542	__u32 queue_mapping;
543	__u32 protocol;
544	__u32 vlan_present;
545	__u32 vlan_tci;
546	__u32 vlan_proto;
547	__u32 priority;
548	__u32 ingress_ifindex;
549	__u32 ifindex;
550	__u32 tc_index;
551	__u32 cb[5];
552	__u32 hash;
553	__u32 tc_classid;
554	__u32 data;
555	__u32 data_end;
556};
557
558struct bpf_tunnel_key {
559	__u32 tunnel_id;
560	union {
561		__u32 remote_ipv4;
562		__u32 remote_ipv6[4];
563	};
564	__u8 tunnel_tos;
565	__u8 tunnel_ttl;
566	__u16 tunnel_ext;
567	__u32 tunnel_label;
568};
569
570/* Generic BPF return codes which all BPF program types may support.
571 * The values are binary compatible with their TC_ACT_* counter-part to
572 * provide backwards compatibility with existing SCHED_CLS and SCHED_ACT
573 * programs.
574 *
575 * XDP is handled seprately, see XDP_*.
576 */
577enum bpf_ret_code {
578	BPF_OK = 0,
579	/* 1 reserved */
580	BPF_DROP = 2,
581	/* 3-6 reserved */
582	BPF_REDIRECT = 7,
583	/* >127 are reserved for prog type specific return codes */
584};
585
586struct bpf_sock {
587	__u32 bound_dev_if;
588	__u32 family;
589	__u32 type;
590	__u32 protocol;
591};
592
593#define XDP_PACKET_HEADROOM 256
594
595/* User return codes for XDP prog type.
596 * A valid XDP program must return one of these defined values. All other
597 * return codes are reserved for future use. Unknown return codes will result
598 * in packet drop.
599 */
600enum xdp_action {
601	XDP_ABORTED = 0,
602	XDP_DROP,
603	XDP_PASS,
604	XDP_TX,
605};
606
607/* user accessible metadata for XDP packet hook
608 * new fields must be added to the end of this structure
609 */
610struct xdp_md {
611	__u32 data;
612	__u32 data_end;
613};
614
615#endif /* _UAPI__LINUX_BPF_H__ */
616