Debian Codegraph @

tools/bpf.h

raw
1 /**
2 * BPF Language Definitions
3 *
4 * Copyright (c) 2012 Red Hat <[email protected]>
5 * Author: Paul Moore <[email protected]>
6 */
7
8 /*
9 * This library is free software; you can redistribute it and/or modify it
10 * under the terms of version 2.1 of the GNU Lesser General Public License as
11 * published by the Free Software Foundation.
12 *
13 * This library is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16 * for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this library; if not, see <http://www.gnu.org/licenses>.
20 */
21
22 #ifndef _BPF_H
23 #define _BPF_H
24
25 #include <inttypes.h>
26 #include <stddef.h>
27
28 /* most of these structures and values are designed to match the Linux Kernel's
29 * BPF interface (see /usr/include/linux/{filter,seccomp}.h), but we define our
30 * own here so that we can function independent of the host OS */
31
32 /* XXX - need to verify these values */
33 #define BPF_SCRATCH_SIZE 6
34
35 /**
36 * Syscall record data format used by seccomp
37 */
38 #define BPF_SYS_ARG_MAX 6
39 struct seccomp_data {
40 int32_t nr;
41 uint32_t arch;
42 uint64_t instruction_pointer;
43 uint64_t args[BPF_SYS_ARG_MAX];
44 };
45 #define BPF_SYSCALL_MAX (sizeof(struct seccomp_data))
46
47 /**
48 * BPF instruction format
49 */
50 struct sock_filter {
51 uint16_t code;
52 uint8_t jt;
53 uint8_t jf;
54 uint32_t k;
55 } __attribute__ ((packed));
56 typedef struct sock_filter bpf_instr_raw;
57
58 /* seccomp return masks */
59 #define SECCOMP_RET_ACTION_FULL 0xffff0000U
60 #define SECCOMP_RET_ACTION 0x7fff0000U
61 #define SECCOMP_RET_DATA 0x0000ffffU
62
63 /* seccomp action values */
64 #define SECCOMP_RET_KILL_PROCESS 0x80000000U
65 #define SECCOMP_RET_KILL_THREAD 0x00000000U
66 #define SECCOMP_RET_KILL SECCOMP_RET_KILL_THREAD
67 #define SECCOMP_RET_TRAP 0x00030000U
68 #define SECCOMP_RET_ERRNO 0x00050000U
69 #define SECCOMP_RET_TRACE 0x7ff00000U
70 #define SECCOMP_RET_LOG 0x7ffc0000U
71 #define SECCOMP_RET_ALLOW 0x7fff0000U
72
73 /* bpf command classes */
74 #define BPF_CLASS(code) ((code) & 0x07)
75 #define BPF_LD 0x00
76 #define BPF_LDX 0x01
77 #define BPF_ST 0x02
78 #define BPF_STX 0x03
79 #define BPF_ALU 0x04
80 #define BPF_JMP 0x05
81 #define BPF_RET 0x06
82 #define BPF_MISC 0x07
83
84 /* BPF_LD and BPF_LDX */
85 #define BPF_SIZE(code) ((code) & 0x18)
86 #define BPF_W 0x00
87 #define BPF_H 0x08
88 #define BPF_B 0x10
89 #define BPF_MODE(code) ((code) & 0xe0)
90 #define BPF_IMM 0x00
91 #define BPF_ABS 0x20
92 #define BPF_IND 0x40
93 #define BPF_MEM 0x60
94 #define BPF_LEN 0x80
95 #define BPF_MSH 0xa0
96
97 #define BPF_OP(code) ((code) & 0xf0)
98 /* BPF_ALU */
99 #define BPF_ADD 0x00
100 #define BPF_SUB 0x10
101 #define BPF_MUL 0x20
102 #define BPF_DIV 0x30
103 #define BPF_OR 0x40
104 #define BPF_AND 0x50
105 #define BPF_LSH 0x60
106 #define BPF_RSH 0x70
107 #define BPF_NEG 0x80
108 #define BPF_MOD 0x90
109 #define BPF_XOR 0xa0
110
111 /* BPF_JMP */
112 #define BPF_JA 0x00
113 #define BPF_JEQ 0x10
114 #define BPF_JGT 0x20
115 #define BPF_JGE 0x30
116 #define BPF_JSET 0x40
117
118 #define BPF_SRC(code) ((code) & 0x08)
119 #define BPF_K 0x00
120 #define BPF_X 0x08
121
122 /* BPF_RET (BPF_K and BPF_X also apply) */
123 #define BPF_RVAL(code) ((code) & 0x18)
124 #define BPF_A 0x10
125
126 /* BPF_MISC */
127 #define BPF_MISCOP(code) ((code) & 0xf8)
128 #define BPF_TAX 0x00
129 #define BPF_TXA 0x80
130
131 #endif