]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - VBLADE/vblade-master/bpf.c
DragonFly BSD support
[Ventoy.git] / VBLADE / vblade-master / bpf.c
1 // bpf.c: bpf packet filter for linux/freebsd
2
3 #include "config.h"
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <sys/types.h>
8 #include "dat.h"
9 #include "fns.h"
10
11 struct bpf_insn {
12 ushort code;
13 uchar jt;
14 uchar jf;
15 u_int32_t k;
16 };
17
18 struct bpf_program {
19 uint bf_len;
20 struct bpf_insn *bf_insns;
21 };
22
23 /* instruction classes */
24 #define BPF_CLASS(code) ((code) & 0x07)
25 #define BPF_LD 0x00
26 #define BPF_LDX 0x01
27 #define BPF_ST 0x02
28 #define BPF_STX 0x03
29 #define BPF_ALU 0x04
30 #define BPF_JMP 0x05
31 #define BPF_RET 0x06
32 #define BPF_MISC 0x07
33
34 /* ld/ldx fields */
35 #define BPF_SIZE(code) ((code) & 0x18)
36 #define BPF_W 0x00
37 #define BPF_H 0x08
38 #define BPF_B 0x10
39 #define BPF_MODE(code) ((code) & 0xe0)
40 #define BPF_IMM 0x00
41 #define BPF_ABS 0x20
42 #define BPF_IND 0x40
43 #define BPF_MEM 0x60
44 #define BPF_LEN 0x80
45 #define BPF_MSH 0xa0
46
47 /* alu/jmp fields */
48 #define BPF_OP(code) ((code) & 0xf0)
49 #define BPF_ADD 0x00
50 #define BPF_SUB 0x10
51 #define BPF_MUL 0x20
52 #define BPF_DIV 0x30
53 #define BPF_OR 0x40
54 #define BPF_AND 0x50
55 #define BPF_LSH 0x60
56 #define BPF_RSH 0x70
57 #define BPF_NEG 0x80
58 #define BPF_JA 0x00
59 #define BPF_JEQ 0x10
60 #define BPF_JGT 0x20
61 #define BPF_JGE 0x30
62 #define BPF_JSET 0x40
63 #define BPF_SRC(code) ((code) & 0x08)
64 #define BPF_K 0x00
65 #define BPF_X 0x08
66
67 /* ret - BPF_K and BPF_X also apply */
68 #define BPF_RVAL(code) ((code) & 0x18)
69 #define BPF_A 0x10
70
71 /* misc */
72 #define BPF_MISCOP(code) ((code) & 0xf8)
73 #define BPF_TAX 0x00
74 #define BPF_TXA 0x80
75
76 /* macros for insn array initializers */
77 #define BPF_STMT(code, k) { (ushort)(code), 0, 0, k }
78 #define BPF_JUMP(code, k, jt, jf) { (ushort)(code), jt, jf, k }
79
80 void *
81 create_bpf_program(int shelf, int slot)
82 {
83 struct bpf_program *bpf_program;
84 struct bpf_insn insns[] = {
85 /* CHECKTYPE: Load the type into register */
86 BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 12),
87 /* Does it match AoE Type (0x88a2)? No, goto INVALID */
88 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x88a2, 0, 10),
89 /* Load the flags into register */
90 BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 14),
91 /* Check to see if the Resp flag is set */
92 BPF_STMT(BPF_ALU+BPF_AND+BPF_K, Resp),
93 /* Yes, goto INVALID */
94 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0, 0, 7),
95 /* CHECKSHELF: Load the shelf number into register */
96 BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 16),
97 /* Does it match shelf number? Yes, goto CHECKSLOT */
98 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, shelf, 1, 0),
99 /* Does it match broadcast? No, goto INVALID */
100 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0xffff, 0, 4),
101 /* CHECKSLOT: Load the slot number into register */
102 BPF_STMT(BPF_LD+BPF_B+BPF_ABS, 18),
103 /* Does it match shelf number? Yes, goto VALID */
104 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, slot, 1, 0),
105 /* Does it match broadcast? No, goto INVALID */
106 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0xff, 0, 1),
107 /* VALID: return -1 (allow the packet to be read) */
108 BPF_STMT(BPF_RET+BPF_K, -1),
109 /* INVALID: return 0 (ignore the packet) */
110 BPF_STMT(BPF_RET+BPF_K, 0),
111 };
112 if ((bpf_program = malloc(sizeof(struct bpf_program))) == NULL
113 || (bpf_program->bf_insns = malloc(sizeof(insns))) == NULL) {
114 perror("malloc");
115 exit(1);
116 }
117 bpf_program->bf_len = sizeof(insns)/sizeof(struct bpf_insn);
118 memcpy(bpf_program->bf_insns, insns, sizeof(insns));
119 return (void *)bpf_program;
120 }
121
122 void
123 free_bpf_program(void *bpf_program)
124 {
125 free(((struct bpf_program *) bpf_program)->bf_insns);
126 free(bpf_program);
127 }