libxt_mark.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #include <stdbool.h>
  2. #include <stdio.h>
  3. #include <xtables.h>
  4. #include <linux/netfilter/xt_mark.h>
  5. struct xt_mark_info {
  6. unsigned long mark, mask;
  7. uint8_t invert;
  8. };
  9. enum {
  10. O_MARK = 0,
  11. };
  12. static void mark_mt_help(void)
  13. {
  14. printf(
  15. "mark match options:\n"
  16. "[!] --mark value[/mask] Match nfmark value with optional mask\n");
  17. }
  18. static const struct xt_option_entry mark_mt_opts[] = {
  19. {.name = "mark", .id = O_MARK, .type = XTTYPE_MARKMASK32,
  20. .flags = XTOPT_MAND | XTOPT_INVERT},
  21. XTOPT_TABLEEND,
  22. };
  23. static void mark_mt_parse(struct xt_option_call *cb)
  24. {
  25. struct xt_mark_mtinfo1 *info = cb->data;
  26. xtables_option_parse(cb);
  27. if (cb->invert)
  28. info->invert = true;
  29. info->mark = cb->val.mark;
  30. info->mask = cb->val.mask;
  31. }
  32. static void mark_parse(struct xt_option_call *cb)
  33. {
  34. struct xt_mark_info *markinfo = cb->data;
  35. xtables_option_parse(cb);
  36. if (cb->invert)
  37. markinfo->invert = 1;
  38. markinfo->mark = cb->val.mark;
  39. markinfo->mask = cb->val.mask;
  40. }
  41. static void print_mark(unsigned int mark, unsigned int mask)
  42. {
  43. if (mask != 0xffffffffU)
  44. printf(" 0x%x/0x%x", mark, mask);
  45. else
  46. printf(" 0x%x", mark);
  47. }
  48. static void
  49. mark_mt_print(const void *ip, const struct xt_entry_match *match, int numeric)
  50. {
  51. const struct xt_mark_mtinfo1 *info = (const void *)match->data;
  52. printf(" mark match");
  53. if (info->invert)
  54. printf(" !");
  55. print_mark(info->mark, info->mask);
  56. }
  57. static void
  58. mark_print(const void *ip, const struct xt_entry_match *match, int numeric)
  59. {
  60. const struct xt_mark_info *info = (const void *)match->data;
  61. printf(" MARK match");
  62. if (info->invert)
  63. printf(" !");
  64. print_mark(info->mark, info->mask);
  65. }
  66. static void mark_mt_save(const void *ip, const struct xt_entry_match *match)
  67. {
  68. const struct xt_mark_mtinfo1 *info = (const void *)match->data;
  69. if (info->invert)
  70. printf(" !");
  71. printf(" --mark");
  72. print_mark(info->mark, info->mask);
  73. }
  74. static void
  75. mark_save(const void *ip, const struct xt_entry_match *match)
  76. {
  77. const struct xt_mark_info *info = (const void *)match->data;
  78. if (info->invert)
  79. printf(" !");
  80. printf(" --mark");
  81. print_mark(info->mark, info->mask);
  82. }
  83. static struct xtables_match mark_mt_reg[] = {
  84. {
  85. .family = NFPROTO_UNSPEC,
  86. .name = "mark",
  87. .revision = 0,
  88. .version = XTABLES_VERSION,
  89. .size = XT_ALIGN(sizeof(struct xt_mark_info)),
  90. .userspacesize = XT_ALIGN(sizeof(struct xt_mark_info)),
  91. .help = mark_mt_help,
  92. .print = mark_print,
  93. .save = mark_save,
  94. .x6_parse = mark_parse,
  95. .x6_options = mark_mt_opts,
  96. },
  97. {
  98. .version = XTABLES_VERSION,
  99. .name = "mark",
  100. .revision = 1,
  101. .family = NFPROTO_UNSPEC,
  102. .size = XT_ALIGN(sizeof(struct xt_mark_mtinfo1)),
  103. .userspacesize = XT_ALIGN(sizeof(struct xt_mark_mtinfo1)),
  104. .help = mark_mt_help,
  105. .print = mark_mt_print,
  106. .save = mark_mt_save,
  107. .x6_parse = mark_mt_parse,
  108. .x6_options = mark_mt_opts,
  109. },
  110. };
  111. void _init(void)
  112. {
  113. xtables_register_matches(mark_mt_reg, ARRAY_SIZE(mark_mt_reg));
  114. }