4 * This program is free software; you can distribute it and/or modify
5 * it under the terms of the GNU General Public License as published
6 * by the Free Software Foundation; either version 2 of the License,
7 * or (at your option) any later version.
9 * Syntax kept close to:
11 * Steven McCanne and Van Jacobson. 1993. The BSD packet filter: a new
12 * architecture for user-level packet capture. In Proceedings of the
13 * USENIX Winter 1993 Conference Proceedings on USENIX Winter 1993
14 * Conference Proceedings (USENIX'93). USENIX Association, Berkeley,
17 * Copyright 2013 Daniel Borkmann <borkmann@redhat.com>
18 * Licensed under the GNU General Public License, version 2.0 (GPLv2)
31 #include <linux/filter.h>
33 #include "bpf_exp.yacc.h"
35 enum jmp_type
{ JTL
, JFL
, JKL
};
38 extern
int yylex(void);
39 extern
void yyerror(const char *str
);
41 extern
void bpf_asm_compile
(FILE *fp
, bool cstyle
);
42 static void bpf_set_curr_instr
(uint16_t op
, uint8_t jt
, uint8_t jf
, uint32_t k
);
43 static void bpf_set_curr_label
(char *label
);
44 static void bpf_set_jmp_label
(char *label
, enum jmp_type type
);
53 %token OP_LDB OP_LDH OP_LD OP_LDX OP_ST OP_STX OP_JMP OP_JEQ OP_JGT OP_JGE
54 %token OP_JSET OP_ADD OP_SUB OP_MUL OP_DIV OP_AND OP_OR OP_XOR OP_LSH OP_RSH
55 %token OP_RET OP_TAX OP_TXA OP_LDXB OP_MOD OP_NEG OP_JNEQ OP_JLT OP_JLE OP_LDI
58 %token K_PKT_LEN K_PROTO K_TYPE K_NLATTR K_NLATTR_NEST K_MARK K_QUEUE K_HATYPE
59 %token K_RXHASH K_CPU K_IFIDX K_VLANT K_VLANP K_POFF
61 %token
':' ',' '[' ']' '(' ')' 'x' 'a' '+' 'M' '*' '&' '#' '%'
118 : label
':' { bpf_set_curr_label
($1); }
122 : OP_LDB
'[' 'x' '+' number
']' {
123 bpf_set_curr_instr
(BPF_LD | BPF_B | BPF_IND
, 0, 0, $5); }
124 | OP_LDB
'[' '%' 'x' '+' number
']' {
125 bpf_set_curr_instr
(BPF_LD | BPF_B | BPF_IND
, 0, 0, $6); }
126 | OP_LDB
'[' number
']' {
127 bpf_set_curr_instr
(BPF_LD | BPF_B | BPF_ABS
, 0, 0, $3); }
129 bpf_set_curr_instr
(BPF_LD | BPF_B | BPF_ABS
, 0, 0,
130 SKF_AD_OFF
+ SKF_AD_PROTOCOL
); }
132 bpf_set_curr_instr
(BPF_LD | BPF_B | BPF_ABS
, 0, 0,
133 SKF_AD_OFF
+ SKF_AD_PKTTYPE
); }
135 bpf_set_curr_instr
(BPF_LD | BPF_B | BPF_ABS
, 0, 0,
136 SKF_AD_OFF
+ SKF_AD_IFINDEX
); }
138 bpf_set_curr_instr
(BPF_LD | BPF_B | BPF_ABS
, 0, 0,
139 SKF_AD_OFF
+ SKF_AD_NLATTR
); }
140 | OP_LDB K_NLATTR_NEST
{
141 bpf_set_curr_instr
(BPF_LD | BPF_B | BPF_ABS
, 0, 0,
142 SKF_AD_OFF
+ SKF_AD_NLATTR_NEST
); }
144 bpf_set_curr_instr
(BPF_LD | BPF_B | BPF_ABS
, 0, 0,
145 SKF_AD_OFF
+ SKF_AD_MARK
); }
147 bpf_set_curr_instr
(BPF_LD | BPF_B | BPF_ABS
, 0, 0,
148 SKF_AD_OFF
+ SKF_AD_QUEUE
); }
150 bpf_set_curr_instr
(BPF_LD | BPF_B | BPF_ABS
, 0, 0,
151 SKF_AD_OFF
+ SKF_AD_HATYPE
); }
153 bpf_set_curr_instr
(BPF_LD | BPF_B | BPF_ABS
, 0, 0,
154 SKF_AD_OFF
+ SKF_AD_RXHASH
); }
156 bpf_set_curr_instr
(BPF_LD | BPF_B | BPF_ABS
, 0, 0,
157 SKF_AD_OFF
+ SKF_AD_CPU
); }
159 bpf_set_curr_instr
(BPF_LD | BPF_B | BPF_ABS
, 0, 0,
160 SKF_AD_OFF
+ SKF_AD_VLAN_TAG
); }
162 bpf_set_curr_instr
(BPF_LD | BPF_B | BPF_ABS
, 0, 0,
163 SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
); }
165 bpf_set_curr_instr
(BPF_LD | BPF_B | BPF_ABS
, 0, 0,
166 SKF_AD_OFF
+ SKF_AD_PAY_OFFSET
); }
170 : OP_LDH
'[' 'x' '+' number
']' {
171 bpf_set_curr_instr
(BPF_LD | BPF_H | BPF_IND
, 0, 0, $5); }
172 | OP_LDH
'[' '%' 'x' '+' number
']' {
173 bpf_set_curr_instr
(BPF_LD | BPF_H | BPF_IND
, 0, 0, $6); }
174 | OP_LDH
'[' number
']' {
175 bpf_set_curr_instr
(BPF_LD | BPF_H | BPF_ABS
, 0, 0, $3); }
177 bpf_set_curr_instr
(BPF_LD | BPF_H | BPF_ABS
, 0, 0,
178 SKF_AD_OFF
+ SKF_AD_PROTOCOL
); }
180 bpf_set_curr_instr
(BPF_LD | BPF_H | BPF_ABS
, 0, 0,
181 SKF_AD_OFF
+ SKF_AD_PKTTYPE
); }
183 bpf_set_curr_instr
(BPF_LD | BPF_H | BPF_ABS
, 0, 0,
184 SKF_AD_OFF
+ SKF_AD_IFINDEX
); }
186 bpf_set_curr_instr
(BPF_LD | BPF_H | BPF_ABS
, 0, 0,
187 SKF_AD_OFF
+ SKF_AD_NLATTR
); }
188 | OP_LDH K_NLATTR_NEST
{
189 bpf_set_curr_instr
(BPF_LD | BPF_H | BPF_ABS
, 0, 0,
190 SKF_AD_OFF
+ SKF_AD_NLATTR_NEST
); }
192 bpf_set_curr_instr
(BPF_LD | BPF_H | BPF_ABS
, 0, 0,
193 SKF_AD_OFF
+ SKF_AD_MARK
); }
195 bpf_set_curr_instr
(BPF_LD | BPF_H | BPF_ABS
, 0, 0,
196 SKF_AD_OFF
+ SKF_AD_QUEUE
); }
198 bpf_set_curr_instr
(BPF_LD | BPF_H | BPF_ABS
, 0, 0,
199 SKF_AD_OFF
+ SKF_AD_HATYPE
); }
201 bpf_set_curr_instr
(BPF_LD | BPF_H | BPF_ABS
, 0, 0,
202 SKF_AD_OFF
+ SKF_AD_RXHASH
); }
204 bpf_set_curr_instr
(BPF_LD | BPF_H | BPF_ABS
, 0, 0,
205 SKF_AD_OFF
+ SKF_AD_CPU
); }
207 bpf_set_curr_instr
(BPF_LD | BPF_H | BPF_ABS
, 0, 0,
208 SKF_AD_OFF
+ SKF_AD_VLAN_TAG
); }
210 bpf_set_curr_instr
(BPF_LD | BPF_H | BPF_ABS
, 0, 0,
211 SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
); }
213 bpf_set_curr_instr
(BPF_LD | BPF_H | BPF_ABS
, 0, 0,
214 SKF_AD_OFF
+ SKF_AD_PAY_OFFSET
); }
218 : OP_LDI
'#' number
{
219 bpf_set_curr_instr
(BPF_LD | BPF_IMM
, 0, 0, $3); }
221 bpf_set_curr_instr
(BPF_LD | BPF_IMM
, 0, 0, $2); }
226 bpf_set_curr_instr
(BPF_LD | BPF_IMM
, 0, 0, $3); }
228 bpf_set_curr_instr
(BPF_LD | BPF_W | BPF_LEN
, 0, 0, 0); }
230 bpf_set_curr_instr
(BPF_LD | BPF_W | BPF_ABS
, 0, 0,
231 SKF_AD_OFF
+ SKF_AD_PROTOCOL
); }
233 bpf_set_curr_instr
(BPF_LD | BPF_W | BPF_ABS
, 0, 0,
234 SKF_AD_OFF
+ SKF_AD_PKTTYPE
); }
236 bpf_set_curr_instr
(BPF_LD | BPF_W | BPF_ABS
, 0, 0,
237 SKF_AD_OFF
+ SKF_AD_IFINDEX
); }
239 bpf_set_curr_instr
(BPF_LD | BPF_W | BPF_ABS
, 0, 0,
240 SKF_AD_OFF
+ SKF_AD_NLATTR
); }
241 | OP_LD K_NLATTR_NEST
{
242 bpf_set_curr_instr
(BPF_LD | BPF_W | BPF_ABS
, 0, 0,
243 SKF_AD_OFF
+ SKF_AD_NLATTR_NEST
); }
245 bpf_set_curr_instr
(BPF_LD | BPF_W | BPF_ABS
, 0, 0,
246 SKF_AD_OFF
+ SKF_AD_MARK
); }
248 bpf_set_curr_instr
(BPF_LD | BPF_W | BPF_ABS
, 0, 0,
249 SKF_AD_OFF
+ SKF_AD_QUEUE
); }
251 bpf_set_curr_instr
(BPF_LD | BPF_W | BPF_ABS
, 0, 0,
252 SKF_AD_OFF
+ SKF_AD_HATYPE
); }
254 bpf_set_curr_instr
(BPF_LD | BPF_W | BPF_ABS
, 0, 0,
255 SKF_AD_OFF
+ SKF_AD_RXHASH
); }
257 bpf_set_curr_instr
(BPF_LD | BPF_W | BPF_ABS
, 0, 0,
258 SKF_AD_OFF
+ SKF_AD_CPU
); }
260 bpf_set_curr_instr
(BPF_LD | BPF_W | BPF_ABS
, 0, 0,
261 SKF_AD_OFF
+ SKF_AD_VLAN_TAG
); }
263 bpf_set_curr_instr
(BPF_LD | BPF_W | BPF_ABS
, 0, 0,
264 SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
); }
266 bpf_set_curr_instr
(BPF_LD | BPF_W | BPF_ABS
, 0, 0,
267 SKF_AD_OFF
+ SKF_AD_PAY_OFFSET
); }
268 | OP_LD
'M' '[' number
']' {
269 bpf_set_curr_instr
(BPF_LD | BPF_MEM
, 0, 0, $4); }
270 | OP_LD
'[' 'x' '+' number
']' {
271 bpf_set_curr_instr
(BPF_LD | BPF_W | BPF_IND
, 0, 0, $5); }
272 | OP_LD
'[' '%' 'x' '+' number
']' {
273 bpf_set_curr_instr
(BPF_LD | BPF_W | BPF_IND
, 0, 0, $6); }
274 | OP_LD
'[' number
']' {
275 bpf_set_curr_instr
(BPF_LD | BPF_W | BPF_ABS
, 0, 0, $3); }
279 : OP_LDXI
'#' number
{
280 bpf_set_curr_instr
(BPF_LDX | BPF_IMM
, 0, 0, $3); }
282 bpf_set_curr_instr
(BPF_LDX | BPF_IMM
, 0, 0, $2); }
286 : OP_LDX
'#' number
{
287 bpf_set_curr_instr
(BPF_LDX | BPF_IMM
, 0, 0, $3); }
289 bpf_set_curr_instr
(BPF_LDX | BPF_W | BPF_LEN
, 0, 0, 0); }
290 | OP_LDX
'M' '[' number
']' {
291 bpf_set_curr_instr
(BPF_LDX | BPF_MEM
, 0, 0, $4); }
292 | OP_LDXB number
'*' '(' '[' number
']' '&' number
')' {
293 if
($2 != 4 ||
$9 != 0xf) {
294 fprintf
(stderr
, "ldxb offset not supported!\n");
297 bpf_set_curr_instr
(BPF_LDX | BPF_MSH | BPF_B
, 0, 0, $6); } }
298 | OP_LDX number
'*' '(' '[' number
']' '&' number
')' {
299 if
($2 != 4 ||
$9 != 0xf) {
300 fprintf
(stderr
, "ldxb offset not supported!\n");
303 bpf_set_curr_instr
(BPF_LDX | BPF_MSH | BPF_B
, 0, 0, $6); } }
307 : OP_ST
'M' '[' number
']' {
308 bpf_set_curr_instr
(BPF_ST
, 0, 0, $4); }
312 : OP_STX
'M' '[' number
']' {
313 bpf_set_curr_instr
(BPF_STX
, 0, 0, $4); }
318 bpf_set_jmp_label
($2, JKL
);
319 bpf_set_curr_instr
(BPF_JMP | BPF_JA
, 0, 0, 0); }
323 : OP_JEQ
'#' number
',' label
',' label
{
324 bpf_set_jmp_label
($5, JTL
);
325 bpf_set_jmp_label
($7, JFL
);
326 bpf_set_curr_instr
(BPF_JMP | BPF_JEQ | BPF_K
, 0, 0, $3); }
327 | OP_JEQ
'x' ',' label
',' label
{
328 bpf_set_jmp_label
($4, JTL
);
329 bpf_set_jmp_label
($6, JFL
);
330 bpf_set_curr_instr
(BPF_JMP | BPF_JEQ | BPF_X
, 0, 0, 0); }
331 | OP_JEQ
'%' 'x' ',' label
',' label
{
332 bpf_set_jmp_label
($5, JTL
);
333 bpf_set_jmp_label
($7, JFL
);
334 bpf_set_curr_instr
(BPF_JMP | BPF_JEQ | BPF_X
, 0, 0, 0); }
335 | OP_JEQ
'#' number
',' label
{
336 bpf_set_jmp_label
($5, JTL
);
337 bpf_set_curr_instr
(BPF_JMP | BPF_JEQ | BPF_K
, 0, 0, $3); }
338 | OP_JEQ
'x' ',' label
{
339 bpf_set_jmp_label
($4, JTL
);
340 bpf_set_curr_instr
(BPF_JMP | BPF_JEQ | BPF_X
, 0, 0, 0); }
341 | OP_JEQ
'%' 'x' ',' label
{
342 bpf_set_jmp_label
($5, JTL
);
343 bpf_set_curr_instr
(BPF_JMP | BPF_JEQ | BPF_X
, 0, 0, 0); }
347 : OP_JNEQ
'#' number
',' label
{
348 bpf_set_jmp_label
($5, JFL
);
349 bpf_set_curr_instr
(BPF_JMP | BPF_JEQ | BPF_K
, 0, 0, $3); }
350 | OP_JNEQ
'x' ',' label
{
351 bpf_set_jmp_label
($4, JFL
);
352 bpf_set_curr_instr
(BPF_JMP | BPF_JEQ | BPF_X
, 0, 0, 0); }
353 | OP_JNEQ
'%' 'x' ',' label
{
354 bpf_set_jmp_label
($5, JFL
);
355 bpf_set_curr_instr
(BPF_JMP | BPF_JEQ | BPF_X
, 0, 0, 0); }
359 : OP_JLT
'#' number
',' label
{
360 bpf_set_jmp_label
($5, JFL
);
361 bpf_set_curr_instr
(BPF_JMP | BPF_JGE | BPF_K
, 0, 0, $3); }
362 | OP_JLT
'x' ',' label
{
363 bpf_set_jmp_label
($4, JFL
);
364 bpf_set_curr_instr
(BPF_JMP | BPF_JGE | BPF_X
, 0, 0, 0); }
365 | OP_JLT
'%' 'x' ',' label
{
366 bpf_set_jmp_label
($5, JFL
);
367 bpf_set_curr_instr
(BPF_JMP | BPF_JGE | BPF_X
, 0, 0, 0); }
371 : OP_JLE
'#' number
',' label
{
372 bpf_set_jmp_label
($5, JFL
);
373 bpf_set_curr_instr
(BPF_JMP | BPF_JGT | BPF_K
, 0, 0, $3); }
374 | OP_JLE
'x' ',' label
{
375 bpf_set_jmp_label
($4, JFL
);
376 bpf_set_curr_instr
(BPF_JMP | BPF_JGT | BPF_X
, 0, 0, 0); }
377 | OP_JLE
'%' 'x' ',' label
{
378 bpf_set_jmp_label
($5, JFL
);
379 bpf_set_curr_instr
(BPF_JMP | BPF_JGT | BPF_X
, 0, 0, 0); }
383 : OP_JGT
'#' number
',' label
',' label
{
384 bpf_set_jmp_label
($5, JTL
);
385 bpf_set_jmp_label
($7, JFL
);
386 bpf_set_curr_instr
(BPF_JMP | BPF_JGT | BPF_K
, 0, 0, $3); }
387 | OP_JGT
'x' ',' label
',' label
{
388 bpf_set_jmp_label
($4, JTL
);
389 bpf_set_jmp_label
($6, JFL
);
390 bpf_set_curr_instr
(BPF_JMP | BPF_JGT | BPF_X
, 0, 0, 0); }
391 | OP_JGT
'%' 'x' ',' label
',' label
{
392 bpf_set_jmp_label
($5, JTL
);
393 bpf_set_jmp_label
($7, JFL
);
394 bpf_set_curr_instr
(BPF_JMP | BPF_JGT | BPF_X
, 0, 0, 0); }
395 | OP_JGT
'#' number
',' label
{
396 bpf_set_jmp_label
($5, JTL
);
397 bpf_set_curr_instr
(BPF_JMP | BPF_JGT | BPF_K
, 0, 0, $3); }
398 | OP_JGT
'x' ',' label
{
399 bpf_set_jmp_label
($4, JTL
);
400 bpf_set_curr_instr
(BPF_JMP | BPF_JGT | BPF_X
, 0, 0, 0); }
401 | OP_JGT
'%' 'x' ',' label
{
402 bpf_set_jmp_label
($5, JTL
);
403 bpf_set_curr_instr
(BPF_JMP | BPF_JGT | BPF_X
, 0, 0, 0); }
407 : OP_JGE
'#' number
',' label
',' label
{
408 bpf_set_jmp_label
($5, JTL
);
409 bpf_set_jmp_label
($7, JFL
);
410 bpf_set_curr_instr
(BPF_JMP | BPF_JGE | BPF_K
, 0, 0, $3); }
411 | OP_JGE
'x' ',' label
',' label
{
412 bpf_set_jmp_label
($4, JTL
);
413 bpf_set_jmp_label
($6, JFL
);
414 bpf_set_curr_instr
(BPF_JMP | BPF_JGE | BPF_X
, 0, 0, 0); }
415 | OP_JGE
'%' 'x' ',' label
',' label
{
416 bpf_set_jmp_label
($5, JTL
);
417 bpf_set_jmp_label
($7, JFL
);
418 bpf_set_curr_instr
(BPF_JMP | BPF_JGE | BPF_X
, 0, 0, 0); }
419 | OP_JGE
'#' number
',' label
{
420 bpf_set_jmp_label
($5, JTL
);
421 bpf_set_curr_instr
(BPF_JMP | BPF_JGE | BPF_K
, 0, 0, $3); }
422 | OP_JGE
'x' ',' label
{
423 bpf_set_jmp_label
($4, JTL
);
424 bpf_set_curr_instr
(BPF_JMP | BPF_JGE | BPF_X
, 0, 0, 0); }
425 | OP_JGE
'%' 'x' ',' label
{
426 bpf_set_jmp_label
($5, JTL
);
427 bpf_set_curr_instr
(BPF_JMP | BPF_JGE | BPF_X
, 0, 0, 0); }
431 : OP_JSET
'#' number
',' label
',' label
{
432 bpf_set_jmp_label
($5, JTL
);
433 bpf_set_jmp_label
($7, JFL
);
434 bpf_set_curr_instr
(BPF_JMP | BPF_JSET | BPF_K
, 0, 0, $3); }
435 | OP_JSET
'x' ',' label
',' label
{
436 bpf_set_jmp_label
($4, JTL
);
437 bpf_set_jmp_label
($6, JFL
);
438 bpf_set_curr_instr
(BPF_JMP | BPF_JSET | BPF_X
, 0, 0, 0); }
439 | OP_JSET
'%' 'x' ',' label
',' label
{
440 bpf_set_jmp_label
($5, JTL
);
441 bpf_set_jmp_label
($7, JFL
);
442 bpf_set_curr_instr
(BPF_JMP | BPF_JSET | BPF_X
, 0, 0, 0); }
443 | OP_JSET
'#' number
',' label
{
444 bpf_set_jmp_label
($5, JTL
);
445 bpf_set_curr_instr
(BPF_JMP | BPF_JSET | BPF_K
, 0, 0, $3); }
446 | OP_JSET
'x' ',' label
{
447 bpf_set_jmp_label
($4, JTL
);
448 bpf_set_curr_instr
(BPF_JMP | BPF_JSET | BPF_X
, 0, 0, 0); }
449 | OP_JSET
'%' 'x' ',' label
{
450 bpf_set_jmp_label
($5, JTL
);
451 bpf_set_curr_instr
(BPF_JMP | BPF_JSET | BPF_X
, 0, 0, 0); }
455 : OP_ADD
'#' number
{
456 bpf_set_curr_instr
(BPF_ALU | BPF_ADD | BPF_K
, 0, 0, $3); }
458 bpf_set_curr_instr
(BPF_ALU | BPF_ADD | BPF_X
, 0, 0, 0); }
460 bpf_set_curr_instr
(BPF_ALU | BPF_ADD | BPF_X
, 0, 0, 0); }
464 : OP_SUB
'#' number
{
465 bpf_set_curr_instr
(BPF_ALU | BPF_SUB | BPF_K
, 0, 0, $3); }
467 bpf_set_curr_instr
(BPF_ALU | BPF_SUB | BPF_X
, 0, 0, 0); }
469 bpf_set_curr_instr
(BPF_ALU | BPF_SUB | BPF_X
, 0, 0, 0); }
473 : OP_MUL
'#' number
{
474 bpf_set_curr_instr
(BPF_ALU | BPF_MUL | BPF_K
, 0, 0, $3); }
476 bpf_set_curr_instr
(BPF_ALU | BPF_MUL | BPF_X
, 0, 0, 0); }
478 bpf_set_curr_instr
(BPF_ALU | BPF_MUL | BPF_X
, 0, 0, 0); }
482 : OP_DIV
'#' number
{
483 bpf_set_curr_instr
(BPF_ALU | BPF_DIV | BPF_K
, 0, 0, $3); }
485 bpf_set_curr_instr
(BPF_ALU | BPF_DIV | BPF_X
, 0, 0, 0); }
487 bpf_set_curr_instr
(BPF_ALU | BPF_DIV | BPF_X
, 0, 0, 0); }
491 : OP_MOD
'#' number
{
492 bpf_set_curr_instr
(BPF_ALU | BPF_MOD | BPF_K
, 0, 0, $3); }
494 bpf_set_curr_instr
(BPF_ALU | BPF_MOD | BPF_X
, 0, 0, 0); }
496 bpf_set_curr_instr
(BPF_ALU | BPF_MOD | BPF_X
, 0, 0, 0); }
501 bpf_set_curr_instr
(BPF_ALU | BPF_NEG
, 0, 0, 0); }
505 : OP_AND
'#' number
{
506 bpf_set_curr_instr
(BPF_ALU | BPF_AND | BPF_K
, 0, 0, $3); }
508 bpf_set_curr_instr
(BPF_ALU | BPF_AND | BPF_X
, 0, 0, 0); }
510 bpf_set_curr_instr
(BPF_ALU | BPF_AND | BPF_X
, 0, 0, 0); }
515 bpf_set_curr_instr
(BPF_ALU | BPF_OR | BPF_K
, 0, 0, $3); }
517 bpf_set_curr_instr
(BPF_ALU | BPF_OR | BPF_X
, 0, 0, 0); }
519 bpf_set_curr_instr
(BPF_ALU | BPF_OR | BPF_X
, 0, 0, 0); }
523 : OP_XOR
'#' number
{
524 bpf_set_curr_instr
(BPF_ALU | BPF_XOR | BPF_K
, 0, 0, $3); }
526 bpf_set_curr_instr
(BPF_ALU | BPF_XOR | BPF_X
, 0, 0, 0); }
528 bpf_set_curr_instr
(BPF_ALU | BPF_XOR | BPF_X
, 0, 0, 0); }
532 : OP_LSH
'#' number
{
533 bpf_set_curr_instr
(BPF_ALU | BPF_LSH | BPF_K
, 0, 0, $3); }
535 bpf_set_curr_instr
(BPF_ALU | BPF_LSH | BPF_X
, 0, 0, 0); }
537 bpf_set_curr_instr
(BPF_ALU | BPF_LSH | BPF_X
, 0, 0, 0); }
541 : OP_RSH
'#' number
{
542 bpf_set_curr_instr
(BPF_ALU | BPF_RSH | BPF_K
, 0, 0, $3); }
544 bpf_set_curr_instr
(BPF_ALU | BPF_RSH | BPF_X
, 0, 0, 0); }
546 bpf_set_curr_instr
(BPF_ALU | BPF_RSH | BPF_X
, 0, 0, 0); }
551 bpf_set_curr_instr
(BPF_RET | BPF_A
, 0, 0, 0); }
553 bpf_set_curr_instr
(BPF_RET | BPF_A
, 0, 0, 0); }
555 bpf_set_curr_instr
(BPF_RET | BPF_X
, 0, 0, 0); }
557 bpf_set_curr_instr
(BPF_RET | BPF_X
, 0, 0, 0); }
558 | OP_RET
'#' number
{
559 bpf_set_curr_instr
(BPF_RET | BPF_K
, 0, 0, $3); }
564 bpf_set_curr_instr
(BPF_MISC | BPF_TAX
, 0, 0, 0); }
569 bpf_set_curr_instr
(BPF_MISC | BPF_TXA
, 0, 0, 0); }
574 static int curr_instr
= 0;
575 static struct sock_filter out
[BPF_MAXINSNS
];
576 static char **labels
, **labels_jt
, **labels_jf
, **labels_k
;
578 static void bpf_assert_max
(void)
580 if
(curr_instr
>= BPF_MAXINSNS
) {
581 fprintf
(stderr
, "only max %u insns allowed!\n", BPF_MAXINSNS
);
586 static void bpf_set_curr_instr
(uint16_t code
, uint8_t jt
, uint8_t jf
,
590 out
[curr_instr
].code
= code
;
591 out
[curr_instr
].jt
= jt
;
592 out
[curr_instr
].jf
= jf
;
593 out
[curr_instr
].k
= k
;
597 static void bpf_set_curr_label
(char *label
)
600 labels
[curr_instr
] = label
;
603 static void bpf_set_jmp_label
(char *label
, enum jmp_type type
)
608 labels_jt
[curr_instr
] = label
;
611 labels_jf
[curr_instr
] = label
;
614 labels_k
[curr_instr
] = label
;
619 static int bpf_find_insns_offset
(const char *label
)
621 int i
, max
= curr_instr
, ret
= -ENOENT
;
623 for
(i
= 0; i
< max
; i
++) {
624 if
(labels
[i
] && !strcmp
(label
, labels
[i
])) {
630 if
(ret
== -ENOENT
) {
631 fprintf
(stderr
, "no such label \'%s\'!\n", label
);
638 static void bpf_stage_1_insert_insns
(void)
643 static void bpf_reduce_k_jumps
(void)
647 for
(i
= 0; i
< curr_instr
; i
++) {
649 int off
= bpf_find_insns_offset
(labels_k
[i
]);
650 out
[i
].k
= (uint32_t) (off
- i
- 1);
655 static void bpf_reduce_jt_jumps
(void)
659 for
(i
= 0; i
< curr_instr
; i
++) {
661 int off
= bpf_find_insns_offset
(labels_jt
[i
]);
662 out
[i
].jt
= (uint8_t) (off
- i
-1);
667 static void bpf_reduce_jf_jumps
(void)
671 for
(i
= 0; i
< curr_instr
; i
++) {
673 int off
= bpf_find_insns_offset
(labels_jf
[i
]);
674 out
[i
].jf
= (uint8_t) (off
- i
- 1);
679 static void bpf_stage_2_reduce_labels
(void)
681 bpf_reduce_k_jumps
();
682 bpf_reduce_jt_jumps
();
683 bpf_reduce_jf_jumps
();
686 static void bpf_pretty_print_c
(void)
690 for
(i
= 0; i
< curr_instr
; i
++)
691 printf
("{ %#04x, %2u, %2u, %#010x },\n", out
[i
].code
,
692 out
[i
].jt
, out
[i
].jf
, out
[i
].k
);
695 static void bpf_pretty_print
(void)
699 printf
("%u,", curr_instr
);
700 for
(i
= 0; i
< curr_instr
; i
++)
701 printf
("%u %u %u %u,", out
[i
].code
,
702 out
[i
].jt
, out
[i
].jf
, out
[i
].k
);
706 static void bpf_init
(void)
708 memset
(out
, 0, sizeof
(out
));
710 labels
= calloc
(BPF_MAXINSNS
, sizeof
(*labels
));
712 labels_jt
= calloc
(BPF_MAXINSNS
, sizeof
(*labels_jt
));
714 labels_jf
= calloc
(BPF_MAXINSNS
, sizeof
(*labels_jf
));
716 labels_k
= calloc
(BPF_MAXINSNS
, sizeof
(*labels_k
));
720 static void bpf_destroy_labels
(void)
724 for
(i
= 0; i
< curr_instr
; i
++) {
732 static void bpf_destroy
(void)
734 bpf_destroy_labels
();
741 void bpf_asm_compile
(FILE *fp
, bool cstyle
)
746 bpf_stage_1_insert_insns
();
747 bpf_stage_2_reduce_labels
();
751 bpf_pretty_print_c
();
759 void yyerror(const char *str
)