bpf_asm.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  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. *
  14. * 1) read Documentation/networking/filter.txt
  15. * 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>
  20. */
  21. #include <stdbool.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. extern void bpf_asm_compile(FILE *fp, bool cstyle);
  25. int main(int argc, char **argv)
  26. {
  27. FILE *fp = stdin;
  28. bool cstyle = false;
  29. int i;
  30. for (i = 1; i < argc; i++) {
  31. if (!strncmp("-c", argv[i], 2)) {
  32. cstyle = true;
  33. continue;
  34. }
  35. fp = fopen(argv[i], "r");
  36. if (!fp) {
  37. fp = stdin;
  38. continue;
  39. }
  40. break;
  41. }
  42. bpf_asm_compile(fp, cstyle);
  43. return 0;
  44. }