Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Minimal BPF assembler
0004  *
0005  * Instead of libpcap high-level filter expressions, it can be quite
0006  * useful to define filters in low-level BPF assembler (that is kept
0007  * close to Steven McCanne and Van Jacobson's original BPF paper).
0008  * In particular for BPF JIT implementors, JIT security auditors, or
0009  * just for defining BPF expressions that contain extensions which are
0010  * not supported by compilers.
0011  *
0012  * How to get into it:
0013  *
0014  * 1) read Documentation/networking/filter.rst
0015  * 2) Run `bpf_asm [-c] <filter-prog file>` to translate into binary
0016  *    blob that is loadable with xt_bpf, cls_bpf et al. Note: -c will
0017  *    pretty print a C-like construct.
0018  *
0019  * Copyright 2013 Daniel Borkmann <borkmann@redhat.com>
0020  */
0021 
0022 #include <stdbool.h>
0023 #include <stdio.h>
0024 #include <string.h>
0025 
0026 extern void bpf_asm_compile(FILE *fp, bool cstyle);
0027 
0028 int main(int argc, char **argv)
0029 {
0030     FILE *fp = stdin;
0031     bool cstyle = false;
0032     int i;
0033 
0034     for (i = 1; i < argc; i++) {
0035         if (!strncmp("-c", argv[i], 2)) {
0036             cstyle = true;
0037             continue;
0038         }
0039 
0040         fp = fopen(argv[i], "r");
0041         if (!fp) {
0042             fp = stdin;
0043             continue;
0044         }
0045 
0046         break;
0047     }
0048 
0049     bpf_asm_compile(fp, cstyle);
0050 
0051     return 0;
0052 }