aboutsummaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorDaniel Borkmann <dborkman@redhat.com>2013-12-11 17:43:44 -0500
committerDavid S. Miller <davem@davemloft.net>2013-12-11 20:28:35 -0500
commit3f356385e8a449e1d7cfc6b6f8d634ac4f5581a0 (patch)
tree6f330ea61260191ce50d539f0b95f3c3b26d0a00 /tools
parentfd981e3c321a7e8661e06fa6077aea89e8228c3a (diff)
filter: bpf_asm: add minimal bpf asm tool
There are a couple of valid use cases for a minimal low-level bpf asm like tool, for example, using/linking to libpcap is not an option, the required BPF filters use Linux extensions that are not supported by libpcap's compiler, a filter might be more complex and not cleanly implementable with libpcap's compiler, particular filter codes should be optimized differently than libpcap's internal BPF compiler does, or for security audits of emitted BPF JIT code for prepared set of BPF instructions resp. BPF JIT compiler development in general. Then, in such cases writing such a filter in low-level syntax can be an good alternative, for example, xt_bpf and cls_bpf users might have requirements that could result in more complex filter code, or one that cannot be expressed with libpcap (e.g. different return codes in cls_bpf for flowids on various BPF code paths). Moreover, BPF JIT implementors may wish to manually write test cases in order to verify the resulting JIT image, and thus need low-level access to BPF code generation as well. Therefore, complete the available toolchain for BPF with this small bpf_asm helper tool for the tools/net/ directory. These 3 complementary minimal helper tools round up and facilitate BPF development. Signed-off-by: Daniel Borkmann <dborkman@redhat.com> Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'tools')
-rw-r--r--tools/net/Makefile18
-rw-r--r--tools/net/bpf_asm.c52
-rw-r--r--tools/net/bpf_exp.l143
-rw-r--r--tools/net/bpf_exp.y749
4 files changed, 960 insertions, 2 deletions
diff --git a/tools/net/Makefile b/tools/net/Makefile
index 0f30d923afa0..004cd74734b6 100644
--- a/tools/net/Makefile
+++ b/tools/net/Makefile
@@ -1,8 +1,16 @@
1prefix = /usr 1prefix = /usr
2 2
3CC = gcc 3CC = gcc
4LEX = flex
5YACC = bison
4 6
5all : bpf_jit_disasm bpf_dbg 7%.yacc.c: %.y
8 $(YACC) -o $@ -d $<
9
10%.lex.c: %.l
11 $(LEX) -o $@ $<
12
13all : bpf_jit_disasm bpf_dbg bpf_asm
6 14
7bpf_jit_disasm : CFLAGS = -Wall -O2 15bpf_jit_disasm : CFLAGS = -Wall -O2
8bpf_jit_disasm : LDLIBS = -lopcodes -lbfd -ldl 16bpf_jit_disasm : LDLIBS = -lopcodes -lbfd -ldl
@@ -12,9 +20,15 @@ bpf_dbg : CFLAGS = -Wall -O2
12bpf_dbg : LDLIBS = -lreadline 20bpf_dbg : LDLIBS = -lreadline
13bpf_dbg : bpf_dbg.o 21bpf_dbg : bpf_dbg.o
14 22
23bpf_asm : CFLAGS = -Wall -O2 -I.
24bpf_asm : LDLIBS =
25bpf_asm : bpf_asm.o bpf_exp.yacc.o bpf_exp.lex.o
26bpf_exp.lex.o : bpf_exp.yacc.c
27
15clean : 28clean :
16 rm -rf *.o bpf_jit_disasm bpf_dbg 29 rm -rf *.o bpf_jit_disasm bpf_dbg bpf_asm bpf_exp.yacc.* bpf_exp.lex.*
17 30
18install : 31install :
19 install bpf_jit_disasm $(prefix)/bin/bpf_jit_disasm 32 install bpf_jit_disasm $(prefix)/bin/bpf_jit_disasm
20 install bpf_dbg $(prefix)/bin/bpf_dbg 33 install bpf_dbg $(prefix)/bin/bpf_dbg
34 install bpf_asm $(prefix)/bin/bpf_asm
diff --git a/tools/net/bpf_asm.c b/tools/net/bpf_asm.c
new file mode 100644
index 000000000000..c15aef097b04
--- /dev/null
+++ b/tools/net/bpf_asm.c
@@ -0,0 +1,52 @@
1/*
2 * Minimal BPF assembler
3 *
4 * Instead of libpcap high-level filter expressions, it can be quite
5 * useful to define filters in low-level BPF assembler (that is kept
6 * close to Steven McCanne and Van Jacobson's original BPF paper).
7 * In particular for BPF JIT implementors, JIT security auditors, or
8 * just for defining BPF expressions that contain extensions which are
9 * not supported by compilers.
10 *
11 * How to get into it:
12 *
13 * 1) read Documentation/networking/filter.txt
14 * 2) Run `bpf_asm [-c] <filter-prog file>` to translate into binary
15 * blob that is loadable with xt_bpf, cls_bpf et al. Note: -c will
16 * pretty print a C-like construct.
17 *
18 * Copyright 2013 Daniel Borkmann <borkmann@redhat.com>
19 * Licensed under the GNU General Public License, version 2.0 (GPLv2)
20 */
21
22#include <stdbool.h>
23#include <stdio.h>
24#include <string.h>
25
26extern void bpf_asm_compile(FILE *fp, bool cstyle);
27
28int main(int argc, char **argv)
29{
30 FILE *fp = stdin;
31 bool cstyle = false;
32 int i;
33
34 for (i = 1; i < argc; i++) {
35 if (!strncmp("-c", argv[i], 2)) {
36 cstyle = true;
37 continue;
38 }
39
40 fp = fopen(argv[i], "r");
41 if (!fp) {
42 fp = stdin;
43 continue;
44 }
45
46 break;
47 }
48
49 bpf_asm_compile(fp, cstyle);
50
51 return 0;
52}
diff --git a/tools/net/bpf_exp.l b/tools/net/bpf_exp.l
new file mode 100644
index 000000000000..bf7be77ddd62
--- /dev/null
+++ b/tools/net/bpf_exp.l
@@ -0,0 +1,143 @@
1/*
2 * BPF asm code lexer
3 *
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.
8 *
9 * Syntax kept close to:
10 *
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,
15 * CA, USA, 2-2.
16 *
17 * Copyright 2013 Daniel Borkmann <borkmann@redhat.com>
18 * Licensed under the GNU General Public License, version 2.0 (GPLv2)
19 */
20
21%{
22
23#include <stdio.h>
24#include <stdint.h>
25#include <stdlib.h>
26
27#include "bpf_exp.yacc.h"
28
29extern void yyerror(const char *str);
30
31%}
32
33%option align
34%option ecs
35
36%option nounput
37%option noreject
38%option noinput
39%option noyywrap
40
41%option 8bit
42%option caseless
43%option yylineno
44
45%%
46
47"ldb" { return OP_LDB; }
48"ldh" { return OP_LDH; }
49"ld" { return OP_LD; }
50"ldi" { return OP_LDI; }
51"ldx" { return OP_LDX; }
52"ldxi" { return OP_LDXI; }
53"ldxb" { return OP_LDXB; }
54"st" { return OP_ST; }
55"stx" { return OP_STX; }
56"jmp" { return OP_JMP; }
57"ja" { return OP_JMP; }
58"jeq" { return OP_JEQ; }
59"jneq" { return OP_JNEQ; }
60"jne" { return OP_JNEQ; }
61"jlt" { return OP_JLT; }
62"jle" { return OP_JLE; }
63"jgt" { return OP_JGT; }
64"jge" { return OP_JGE; }
65"jset" { return OP_JSET; }
66"add" { return OP_ADD; }
67"sub" { return OP_SUB; }
68"mul" { return OP_MUL; }
69"div" { return OP_DIV; }
70"mod" { return OP_MOD; }
71"neg" { return OP_NEG; }
72"and" { return OP_AND; }
73"xor" { return OP_XOR; }
74"or" { return OP_OR; }
75"lsh" { return OP_LSH; }
76"rsh" { return OP_RSH; }
77"ret" { return OP_RET; }
78"tax" { return OP_TAX; }
79"txa" { return OP_TXA; }
80
81"#"?("len") { return K_PKT_LEN; }
82"#"?("proto") { return K_PROTO; }
83"#"?("type") { return K_TYPE; }
84"#"?("poff") { return K_POFF; }
85"#"?("ifidx") { return K_IFIDX; }
86"#"?("nla") { return K_NLATTR; }
87"#"?("nlan") { return K_NLATTR_NEST; }
88"#"?("mark") { return K_MARK; }
89"#"?("queue") { return K_QUEUE; }
90"#"?("hatype") { return K_HATYPE; }
91"#"?("rxhash") { return K_RXHASH; }
92"#"?("cpu") { return K_CPU; }
93"#"?("vlan_tci") { return K_VLANT; }
94"#"?("vlan_pr") { return K_VLANP; }
95
96":" { return ':'; }
97"," { return ','; }
98"#" { return '#'; }
99"%" { return '%'; }
100"[" { return '['; }
101"]" { return ']'; }
102"(" { return '('; }
103")" { return ')'; }
104"x" { return 'x'; }
105"a" { return 'a'; }
106"+" { return '+'; }
107"M" { return 'M'; }
108"*" { return '*'; }
109"&" { return '&'; }
110
111([0][x][a-fA-F0-9]+) {
112 yylval.number = strtoul(yytext, NULL, 16);
113 return number;
114 }
115([0][b][0-1]+) {
116 yylval.number = strtol(yytext + 2, NULL, 2);