1a7d7a392SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2a92bb546SJakub Kicinski /* 3a92bb546SJakub Kicinski * Minimal BPF assembler 4a92bb546SJakub Kicinski * 5a92bb546SJakub Kicinski * Instead of libpcap high-level filter expressions, it can be quite 6a92bb546SJakub Kicinski * useful to define filters in low-level BPF assembler (that is kept 7a92bb546SJakub Kicinski * close to Steven McCanne and Van Jacobson's original BPF paper). 8a92bb546SJakub Kicinski * In particular for BPF JIT implementors, JIT security auditors, or 9a92bb546SJakub Kicinski * just for defining BPF expressions that contain extensions which are 10a92bb546SJakub Kicinski * not supported by compilers. 11a92bb546SJakub Kicinski * 12a92bb546SJakub Kicinski * How to get into it: 13a92bb546SJakub Kicinski * 14a92bb546SJakub Kicinski * 1) read Documentation/networking/filter.txt 15a92bb546SJakub Kicinski * 2) Run `bpf_asm [-c] <filter-prog file>` to translate into binary 16a92bb546SJakub Kicinski * blob that is loadable with xt_bpf, cls_bpf et al. Note: -c will 17a92bb546SJakub Kicinski * pretty print a C-like construct. 18a92bb546SJakub Kicinski * 19a92bb546SJakub Kicinski * Copyright 2013 Daniel Borkmann <borkmann@redhat.com> 20a92bb546SJakub Kicinski */ 21a92bb546SJakub Kicinski 22a92bb546SJakub Kicinski #include <stdbool.h> 23a92bb546SJakub Kicinski #include <stdio.h> 24a92bb546SJakub Kicinski #include <string.h> 25a92bb546SJakub Kicinski 26a92bb546SJakub Kicinski extern void bpf_asm_compile(FILE *fp, bool cstyle); 27a92bb546SJakub Kicinski 28a92bb546SJakub Kicinski int main(int argc, char **argv) 29a92bb546SJakub Kicinski { 30a92bb546SJakub Kicinski FILE *fp = stdin; 31a92bb546SJakub Kicinski bool cstyle = false; 32a92bb546SJakub Kicinski int i; 33a92bb546SJakub Kicinski 34a92bb546SJakub Kicinski for (i = 1; i < argc; i++) { 35a92bb546SJakub Kicinski if (!strncmp("-c", argv[i], 2)) { 36a92bb546SJakub Kicinski cstyle = true; 37a92bb546SJakub Kicinski continue; 38a92bb546SJakub Kicinski } 39a92bb546SJakub Kicinski 40a92bb546SJakub Kicinski fp = fopen(argv[i], "r"); 41a92bb546SJakub Kicinski if (!fp) { 42a92bb546SJakub Kicinski fp = stdin; 43a92bb546SJakub Kicinski continue; 44a92bb546SJakub Kicinski } 45a92bb546SJakub Kicinski 46a92bb546SJakub Kicinski break; 47a92bb546SJakub Kicinski } 48a92bb546SJakub Kicinski 49a92bb546SJakub Kicinski bpf_asm_compile(fp, cstyle); 50a92bb546SJakub Kicinski 51a92bb546SJakub Kicinski return 0; 52a92bb546SJakub Kicinski } 53