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