libipt_ah.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include <stdio.h>
  2. #include <xtables.h>
  3. #include <linux/netfilter_ipv4/ipt_ah.h>
  4. enum {
  5. O_AHSPI = 0,
  6. };
  7. static void ah_help(void)
  8. {
  9. printf(
  10. "ah match options:\n"
  11. "[!] --ahspi spi[:spi]\n"
  12. " match spi (range)\n");
  13. }
  14. static const struct xt_option_entry ah_opts[] = {
  15. {.name = "ahspi", .id = O_AHSPI, .type = XTTYPE_UINT32RC,
  16. .flags = XTOPT_INVERT | XTOPT_PUT,
  17. XTOPT_POINTER(struct ipt_ah, spis)},
  18. XTOPT_TABLEEND,
  19. };
  20. static void ah_parse(struct xt_option_call *cb)
  21. {
  22. struct ipt_ah *ahinfo = cb->data;
  23. xtables_option_parse(cb);
  24. if (cb->nvals == 1)
  25. ahinfo->spis[1] = ahinfo->spis[0];
  26. if (cb->invert)
  27. ahinfo->invflags |= IPT_AH_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. printf("%s", name);
  36. if (min == max) {
  37. printf(":%s", inv);
  38. printf("%u", min);
  39. } else {
  40. printf("s:%s", inv);
  41. printf("%u",min);
  42. printf(":");
  43. printf("%u",max);
  44. }
  45. }
  46. }
  47. static void ah_print(const void *ip, const struct xt_entry_match *match,
  48. int numeric)
  49. {
  50. const struct ipt_ah *ah = (struct ipt_ah *)match->data;
  51. printf(" ah ");
  52. print_spis("spi", ah->spis[0], ah->spis[1],
  53. ah->invflags & IPT_AH_INV_SPI);
  54. if (ah->invflags & ~IPT_AH_INV_MASK)
  55. printf(" Unknown invflags: 0x%X",
  56. ah->invflags & ~IPT_AH_INV_MASK);
  57. }
  58. static void ah_save(const void *ip, const struct xt_entry_match *match)
  59. {
  60. const struct ipt_ah *ahinfo = (struct ipt_ah *)match->data;
  61. if (!(ahinfo->spis[0] == 0
  62. && ahinfo->spis[1] == 0xFFFFFFFF)) {
  63. printf("%s --ahspi ",
  64. (ahinfo->invflags & IPT_AH_INV_SPI) ? " !" : "");
  65. if (ahinfo->spis[0]
  66. != ahinfo->spis[1])
  67. printf("%u:%u",
  68. ahinfo->spis[0],
  69. ahinfo->spis[1]);
  70. else
  71. printf("%u",
  72. ahinfo->spis[0]);
  73. }
  74. }
  75. static struct xtables_match ah_mt_reg = {
  76. .name = "ah",
  77. .version = XTABLES_VERSION,
  78. .family = NFPROTO_IPV4,
  79. .size = XT_ALIGN(sizeof(struct ipt_ah)),
  80. .userspacesize = XT_ALIGN(sizeof(struct ipt_ah)),
  81. .help = ah_help,
  82. .print = ah_print,
  83. .save = ah_save,
  84. .x6_parse = ah_parse,
  85. .x6_options = ah_opts,
  86. };
  87. void
  88. _init(void)
  89. {
  90. xtables_register_match(&ah_mt_reg);
  91. }