libxt_connmark.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /* Shared library add-on to iptables to add connmark matching support.
  2. *
  3. * (C) 2002,2004 MARA Systems AB <http://www.marasystems.com>
  4. * by Henrik Nordstrom <hno@marasystems.com>
  5. *
  6. * Version 1.1
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <stdbool.h>
  23. #include <stdint.h>
  24. #include <stdio.h>
  25. #include <xtables.h>
  26. #include <linux/netfilter/xt_connmark.h>
  27. struct xt_connmark_info {
  28. unsigned long mark, mask;
  29. uint8_t invert;
  30. };
  31. enum {
  32. O_MARK = 0,
  33. };
  34. static void connmark_mt_help(void)
  35. {
  36. printf(
  37. "connmark match options:\n"
  38. "[!] --mark value[/mask] Match ctmark value with optional mask\n");
  39. }
  40. static const struct xt_option_entry connmark_mt_opts[] = {
  41. {.name = "mark", .id = O_MARK, .type = XTTYPE_MARKMASK32,
  42. .flags = XTOPT_MAND | XTOPT_INVERT},
  43. XTOPT_TABLEEND,
  44. };
  45. static void connmark_mt_parse(struct xt_option_call *cb)
  46. {
  47. struct xt_connmark_mtinfo1 *info = cb->data;
  48. xtables_option_parse(cb);
  49. if (cb->invert)
  50. info->invert = true;
  51. info->mark = cb->val.mark;
  52. info->mask = cb->val.mask;
  53. }
  54. static void connmark_parse(struct xt_option_call *cb)
  55. {
  56. struct xt_connmark_info *markinfo = cb->data;
  57. xtables_option_parse(cb);
  58. markinfo->mark = cb->val.mark;
  59. markinfo->mask = cb->val.mask;
  60. if (cb->invert)
  61. markinfo->invert = 1;
  62. }
  63. static void print_mark(unsigned int mark, unsigned int mask)
  64. {
  65. if (mask != 0xffffffffU)
  66. printf(" 0x%x/0x%x", mark, mask);
  67. else
  68. printf(" 0x%x", mark);
  69. }
  70. static void
  71. connmark_print(const void *ip, const struct xt_entry_match *match, int numeric)
  72. {
  73. const struct xt_connmark_info *info = (const void *)match->data;
  74. printf(" CONNMARK match ");
  75. if (info->invert)
  76. printf("!");
  77. print_mark(info->mark, info->mask);
  78. }
  79. static void
  80. connmark_mt_print(const void *ip, const struct xt_entry_match *match, int numeric)
  81. {
  82. const struct xt_connmark_mtinfo1 *info = (const void *)match->data;
  83. printf(" connmark match ");
  84. if (info->invert)
  85. printf("!");
  86. print_mark(info->mark, info->mask);
  87. }
  88. static void connmark_save(const void *ip, const struct xt_entry_match *match)
  89. {
  90. const struct xt_connmark_info *info = (const void *)match->data;
  91. if (info->invert)
  92. printf(" !");
  93. printf(" --mark");
  94. print_mark(info->mark, info->mask);
  95. }
  96. static void
  97. connmark_mt_save(const void *ip, const struct xt_entry_match *match)
  98. {
  99. const struct xt_connmark_mtinfo1 *info = (const void *)match->data;
  100. if (info->invert)
  101. printf(" !");
  102. printf(" --mark");
  103. print_mark(info->mark, info->mask);
  104. }
  105. static struct xtables_match connmark_mt_reg[] = {
  106. {
  107. .family = NFPROTO_UNSPEC,
  108. .name = "connmark",
  109. .revision = 0,
  110. .version = XTABLES_VERSION,
  111. .size = XT_ALIGN(sizeof(struct xt_connmark_info)),
  112. .userspacesize = XT_ALIGN(sizeof(struct xt_connmark_info)),
  113. .help = connmark_mt_help,
  114. .print = connmark_print,
  115. .save = connmark_save,
  116. .x6_parse = connmark_parse,
  117. .x6_options = connmark_mt_opts,
  118. },
  119. {
  120. .version = XTABLES_VERSION,
  121. .name = "connmark",
  122. .revision = 1,
  123. .family = NFPROTO_UNSPEC,
  124. .size = XT_ALIGN(sizeof(struct xt_connmark_mtinfo1)),
  125. .userspacesize = XT_ALIGN(sizeof(struct xt_connmark_mtinfo1)),
  126. .help = connmark_mt_help,
  127. .print = connmark_mt_print,
  128. .save = connmark_mt_save,
  129. .x6_parse = connmark_mt_parse,
  130. .x6_options = connmark_mt_opts,
  131. },
  132. };
  133. void _init(void)
  134. {
  135. xtables_register_matches(connmark_mt_reg, ARRAY_SIZE(connmark_mt_reg));
  136. }