blob: 0063c3c029e7c4b1d8f48e8599c83970e25f5579 [file] [log] [blame]
Thomas Gleixnera7d7a392019-06-03 07:44:48 +02001// SPDX-License-Identifier: GPL-2.0-only
Daniel Borkmann3f356382013-12-11 23:43:44 +01002/*
3 * Minimal BPF assembler
4 *
5 * Instead of libpcap high-level filter expressions, it can be quite
6 * useful to define filters in low-level BPF assembler (that is kept
7 * close to Steven McCanne and Van Jacobson's original BPF paper).
8 * In particular for BPF JIT implementors, JIT security auditors, or
9 * just for defining BPF expressions that contain extensions which are
10 * not supported by compilers.
11 *
12 * How to get into it:
13 *
Mauro Carvalho Chehabcb3f0d52020-04-28 00:01:36 +020014 * 1) read Documentation/networking/filter.rst
Daniel Borkmann3f356382013-12-11 23:43:44 +010015 * 2) Run `bpf_asm [-c] <filter-prog file>` to translate into binary
16 * blob that is loadable with xt_bpf, cls_bpf et al. Note: -c will
17 * pretty print a C-like construct.
18 *
19 * Copyright 2013 Daniel Borkmann <borkmann@redhat.com>
Daniel Borkmann3f356382013-12-11 23:43:44 +010020 */
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}