libxt_esp.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #include <stdio.h>
  2. #include <xtables.h>
  3. #include <linux/netfilter/xt_esp.h>
  4. enum {
  5. O_ESPSPI = 0,
  6. };
  7. static void esp_help(void)
  8. {
  9. printf(
  10. "esp match options:\n"
  11. "[!] --espspi spi[:spi]\n"
  12. " match spi (range)\n");
  13. }
  14. static const struct xt_option_entry esp_opts[] = {
  15. {.name = "espspi", .id = O_ESPSPI, .type = XTTYPE_UINT32RC,
  16. .flags = XTOPT_INVERT | XTOPT_PUT,
  17. XTOPT_POINTER(struct xt_esp, spis)},
  18. XTOPT_TABLEEND,
  19. };
  20. static void esp_parse(struct xt_option_call *cb)
  21. {
  22. struct xt_esp *espinfo = cb->data;
  23. xtables_option_parse(cb);
  24. if (cb->nvals == 1)
  25. espinfo->spis[1] = espinfo->spis[0];
  26. if (cb->invert)
  27. espinfo->invflags |= XT_ESP_INV_SPI;
  28. }
  29. static void
  30. print_spis(const char *name, uint32_t min, uint32_t max,
  31. int invert)
  32. {
  33. const char *inv = invert ? "!" : "";
  34. if (min != 0 || max != 0xFFFFFFFF || invert) {
  35. if (min == max)
  36. printf(" %s:%s%u", name, inv, min);
  37. else
  38. printf(" %ss:%s%u:%u", name, inv, min, max);
  39. }
  40. }
  41. static void
  42. esp_print(const void *ip, const struct xt_entry_match *match, int numeric)
  43. {
  44. const struct xt_esp *esp = (struct xt_esp *)match->data;
  45. printf(" esp");
  46. print_spis("spi", esp->spis[0], esp->spis[1],
  47. esp->invflags & XT_ESP_INV_SPI);
  48. if (esp->invflags & ~XT_ESP_INV_MASK)
  49. printf(" Unknown invflags: 0x%X",
  50. esp->invflags & ~XT_ESP_INV_MASK);
  51. }
  52. static void esp_save(const void *ip, const struct xt_entry_match *match)
  53. {
  54. const struct xt_esp *espinfo = (struct xt_esp *)match->data;
  55. if (!(espinfo->spis[0] == 0
  56. && espinfo->spis[1] == 0xFFFFFFFF)) {
  57. printf("%s --espspi ",
  58. (espinfo->invflags & XT_ESP_INV_SPI) ? " !" : "");
  59. if (espinfo->spis[0]
  60. != espinfo->spis[1])
  61. printf("%u:%u",
  62. espinfo->spis[0],
  63. espinfo->spis[1]);
  64. else
  65. printf("%u",
  66. espinfo->spis[0]);
  67. }
  68. }
  69. static struct xtables_match esp_match = {
  70. .family = NFPROTO_UNSPEC,
  71. .name = "esp",
  72. .version = XTABLES_VERSION,
  73. .size = XT_ALIGN(sizeof(struct xt_esp)),
  74. .userspacesize = XT_ALIGN(sizeof(struct xt_esp)),
  75. .help = esp_help,
  76. .print = esp_print,
  77. .save = esp_save,
  78. .x6_parse = esp_parse,
  79. .x6_options = esp_opts,
  80. };
  81. void
  82. _init(void)
  83. {
  84. xtables_register_match(&esp_match);
  85. }