doc: new fedora/rhel maintainer
[netsniff-ng.git] / src / bpf.h
blob38b636e801c460448b0886c5069dede192876de5
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2009, 2010 Daniel Borkmann.
5 * Subject to the GPL, version 2.
6 */
8 #ifndef BPF_H
9 #define BPF_H
11 #include <linux/filter.h>
12 #include <stdint.h>
13 #include <stdlib.h>
15 #include "xmalloc.h"
17 extern void bpf_dump_all(struct sock_fprog *bpf);
18 extern int bpf_validate(const struct sock_fprog *bpf);
19 extern uint32_t bpf_run_filter(const struct sock_fprog *bpf, uint8_t *packet,
20 size_t plen);
21 extern void bpf_attach_to_sock(int sock, struct sock_fprog *bpf);
22 extern void bpf_detach_from_sock(int sock);
23 extern void enable_kernel_bpf_jit_compiler(void);
24 extern void bpf_parse_rules(char *rulefile, struct sock_fprog *bpf);
26 static inline void bpf_release(struct sock_fprog *bpf)
28 free(bpf->filter);
32 * The instruction encodings.
34 /* instruction classes */
35 #define BPF_CLASS(code) ((code) & 0x07)
36 #define BPF_LD 0x00
37 #define BPF_LDX 0x01
38 #define BPF_ST 0x02
39 #define BPF_STX 0x03
40 #define BPF_ALU 0x04
41 #define BPF_JMP 0x05
42 #define BPF_RET 0x06
43 #define BPF_MISC 0x07
45 /* ld/ldx fields */
46 #define BPF_SIZE(code) ((code) & 0x18)
47 #define BPF_W 0x00
48 #define BPF_H 0x08
49 #define BPF_B 0x10
50 #define BPF_MODE(code) ((code) & 0xe0)
51 #define BPF_IMM 0x00
52 #define BPF_ABS 0x20
53 #define BPF_IND 0x40
54 #define BPF_MEM 0x60
55 #define BPF_LEN 0x80
56 #define BPF_MSH 0xa0
58 /* alu/jmp fields */
59 #define BPF_OP(code) ((code) & 0xf0)
60 #define BPF_ADD 0x00
61 #define BPF_SUB 0x10
62 #define BPF_MUL 0x20
63 #define BPF_DIV 0x30
64 #define BPF_OR 0x40
65 #define BPF_AND 0x50
66 #define BPF_LSH 0x60
67 #define BPF_RSH 0x70
68 #define BPF_NEG 0x80
69 #define BPF_JA 0x00
70 #define BPF_JEQ 0x10
71 #define BPF_JGT 0x20
72 #define BPF_JGE 0x30
73 #define BPF_JSET 0x40
74 #define BPF_SRC(code) ((code) & 0x08)
75 #define BPF_K 0x00
76 #define BPF_X 0x08
78 /* ret - BPF_K and BPF_X also apply */
79 #define BPF_RVAL(code) ((code) & 0x18)
80 #define BPF_A 0x10
82 /* misc */
83 #define BPF_MISCOP(code) ((code) & 0xf8)
84 #define BPF_TAX 0x00
85 #define BPF_TXA 0x80
87 /* Hidden Linux kernel BPF extensions */
89 * RATIONALE. Negative offsets are invalid in BPF.
90 * We use them to reference ancillary data.
91 * Unlike introduction new instructions, it does not break
92 * existing compilers/optimizers.
95 #ifndef SKF_AD_OFF
96 # define SKF_AD_OFF (-0x1000)
97 #endif
98 #ifndef SKF_AD_PROTOCOL
99 # define SKF_AD_PROTOCOL 0
100 #endif
101 #ifndef SKF_AD_PKTTYPE
102 # define SKF_AD_PKTTYPE 4
103 #endif
104 #ifndef SKF_AD_IFINDEX
105 # define SKF_AD_IFINDEX 8
106 #endif
107 #ifndef SKF_AD_NLATTR
108 # define SKF_AD_NLATTR 12
109 #endif
110 #ifndef SKF_AD_NLATTR_NEST
111 # define SKF_AD_NLATTR_NEST 16
112 #endif
113 #ifndef SKF_AD_MARK
114 # define SKF_AD_MARK 20
115 #endif
116 #ifndef SKF_AD_QUEUE
117 # define SKF_AD_QUEUE 24
118 #endif
119 #ifndef SKF_AD_HATYPE
120 # define SKF_AD_HATYPE 28
121 #endif
122 #ifndef SKF_AD_RXHASH
123 # define SKF_AD_RXHASH 32
124 #endif
125 #ifndef SKF_AD_CPU
126 # define SKF_AD_CPU 36
127 #endif
129 #endif /* BPF_H */