]>
Commit | Line | Data |
---|---|---|
8befd5cc MG |
1 | /** |
2 | * BPF Language Definitions | |
3 | * | |
4 | * Copyright (c) 2012 Red Hat <pmoore@redhat.com> | |
5 | * Author: Paul Moore <paul@paul-moore.com> | |
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 0x7fff0000U | |
60 | #define SECCOMP_RET_DATA 0x0000ffffU | |
61 | ||
62 | /* seccomp action values */ | |
63 | #define SECCOMP_RET_KILL 0x00000000U | |
64 | #define SECCOMP_RET_TRAP 0x00030000U | |
65 | #define SECCOMP_RET_ERRNO 0x00050000U | |
66 | #define SECCOMP_RET_TRACE 0x7ff00000U | |
67 | #define SECCOMP_RET_ALLOW 0x7fff0000U | |
68 | ||
69 | /* bpf command classes */ | |
70 | #define BPF_CLASS(code) ((code) & 0x07) | |
71 | #define BPF_LD 0x00 | |
72 | #define BPF_LDX 0x01 | |
73 | #define BPF_ST 0x02 | |
74 | #define BPF_STX 0x03 | |
75 | #define BPF_ALU 0x04 | |
76 | #define BPF_JMP 0x05 | |
77 | #define BPF_RET 0x06 | |
78 | #define BPF_MISC 0x07 | |
79 | ||
80 | /* BPF_LD and BPF_LDX */ | |
81 | #define BPF_SIZE(code) ((code) & 0x18) | |
82 | #define BPF_W 0x00 | |
83 | #define BPF_H 0x08 | |
84 | #define BPF_B 0x10 | |
85 | #define BPF_MODE(code) ((code) & 0xe0) | |
86 | #define BPF_IMM 0x00 | |
87 | #define BPF_ABS 0x20 | |
88 | #define BPF_IND 0x40 | |
89 | #define BPF_MEM 0x60 | |
90 | #define BPF_LEN 0x80 | |
91 | #define BPF_MSH 0xa0 | |
92 | ||
93 | #define BPF_OP(code) ((code) & 0xf0) | |
94 | /* BPF_ALU */ | |
95 | #define BPF_ADD 0x00 | |
96 | #define BPF_SUB 0x10 | |
97 | #define BPF_MUL 0x20 | |
98 | #define BPF_DIV 0x30 | |
99 | #define BPF_OR 0x40 | |
100 | #define BPF_AND 0x50 | |
101 | #define BPF_LSH 0x60 | |
102 | #define BPF_RSH 0x70 | |
103 | #define BPF_NEG 0x80 | |
104 | /* BPF_JMP */ | |
105 | #define BPF_JA 0x00 | |
106 | #define BPF_JEQ 0x10 | |
107 | #define BPF_JGT 0x20 | |
108 | #define BPF_JGE 0x30 | |
109 | #define BPF_JSET 0x40 | |
110 | ||
111 | #define BPF_SRC(code) ((code) & 0x08) | |
112 | #define BPF_K 0x00 | |
113 | #define BPF_X 0x08 | |
114 | ||
115 | /* BPF_RET (BPF_K and BPF_X also apply) */ | |
116 | #define BPF_RVAL(code) ((code) & 0x18) | |
117 | #define BPF_A 0x10 | |
118 | ||
119 | /* BPF_MISC */ | |
120 | #define BPF_MISCOP(code) ((code) & 0xf8) | |
121 | #define BPF_TAX 0x00 | |
122 | #define BPF_TXA 0x80 | |
123 | ||
124 | #endif |