]>
glassweightruler.freedombox.rocks Git - Ventoy.git/blob - VBLADE/vblade-master/bpf.c
1 // bpf.c: bpf packet filter for linux/freebsd
20 struct bpf_insn
*bf_insns
;
23 /* instruction classes */
24 #define BPF_CLASS(code) ((code) & 0x07)
35 #define BPF_SIZE(code) ((code) & 0x18)
39 #define BPF_MODE(code) ((code) & 0xe0)
48 #define BPF_OP(code) ((code) & 0xf0)
63 #define BPF_SRC(code) ((code) & 0x08)
67 /* ret - BPF_K and BPF_X also apply */
68 #define BPF_RVAL(code) ((code) & 0x18)
72 #define BPF_MISCOP(code) ((code) & 0xf8)
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 }
81 create_bpf_program(int shelf
, int slot
)
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),
112 if ((bpf_program
= malloc(sizeof(struct bpf_program
))) == NULL
113 || (bpf_program
->bf_insns
= malloc(sizeof(insns
))) == NULL
) {
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
;
123 free_bpf_program(void *bpf_program
)
125 free(((struct bpf_program
*) bpf_program
)->bf_insns
);