libip6t_hl.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * IPv6 Hop Limit matching module
  3. * Maciej Soltysiak <solt@dns.toxicfilms.tv>
  4. * Based on HW's ttl match
  5. * This program is released under the terms of GNU GPL
  6. * Cleanups by Stephane Ouellette <ouellettes@videotron.ca>
  7. */
  8. #include <stdio.h>
  9. #include <xtables.h>
  10. #include <linux/netfilter_ipv6/ip6t_hl.h>
  11. enum {
  12. O_HL_EQ = 0,
  13. O_HL_LT,
  14. O_HL_GT,
  15. F_HL_EQ = 1 << O_HL_EQ,
  16. F_HL_LT = 1 << O_HL_LT,
  17. F_HL_GT = 1 << O_HL_GT,
  18. F_ANY = F_HL_EQ | F_HL_LT | F_HL_GT,
  19. };
  20. static void hl_help(void)
  21. {
  22. printf(
  23. "hl match options:\n"
  24. "[!] --hl-eq value Match hop limit value\n"
  25. " --hl-lt value Match HL < value\n"
  26. " --hl-gt value Match HL > value\n");
  27. }
  28. static void hl_parse(struct xt_option_call *cb)
  29. {
  30. struct ip6t_hl_info *info = cb->data;
  31. xtables_option_parse(cb);
  32. switch (cb->entry->id) {
  33. case O_HL_EQ:
  34. info->mode = cb->invert ? IP6T_HL_NE : IP6T_HL_EQ;
  35. break;
  36. case O_HL_LT:
  37. info->mode = IP6T_HL_LT;
  38. break;
  39. case O_HL_GT:
  40. info->mode = IP6T_HL_GT;
  41. break;
  42. }
  43. }
  44. static void hl_check(struct xt_fcheck_call *cb)
  45. {
  46. if (!(cb->xflags & F_ANY))
  47. xtables_error(PARAMETER_PROBLEM,
  48. "HL match: You must specify one of "
  49. "`--hl-eq', `--hl-lt', `--hl-gt'");
  50. }
  51. static void hl_print(const void *ip, const struct xt_entry_match *match,
  52. int numeric)
  53. {
  54. static const char *const op[] = {
  55. [IP6T_HL_EQ] = "==",
  56. [IP6T_HL_NE] = "!=",
  57. [IP6T_HL_LT] = "<",
  58. [IP6T_HL_GT] = ">" };
  59. const struct ip6t_hl_info *info =
  60. (struct ip6t_hl_info *) match->data;
  61. printf(" HL match HL %s %u", op[info->mode], info->hop_limit);
  62. }
  63. static void hl_save(const void *ip, const struct xt_entry_match *match)
  64. {
  65. static const char *const op[] = {
  66. [IP6T_HL_EQ] = "--hl-eq",
  67. [IP6T_HL_NE] = "! --hl-eq",
  68. [IP6T_HL_LT] = "--hl-lt",
  69. [IP6T_HL_GT] = "--hl-gt" };
  70. const struct ip6t_hl_info *info =
  71. (struct ip6t_hl_info *) match->data;
  72. printf(" %s %u", op[info->mode], info->hop_limit);
  73. }
  74. #define s struct ip6t_hl_info
  75. static const struct xt_option_entry hl_opts[] = {
  76. {.name = "hl-lt", .id = O_HL_LT, .excl = F_ANY, .type = XTTYPE_UINT8,
  77. .flags = XTOPT_PUT, XTOPT_POINTER(s, hop_limit)},
  78. {.name = "hl-gt", .id = O_HL_GT, .excl = F_ANY, .type = XTTYPE_UINT8,
  79. .flags = XTOPT_PUT, XTOPT_POINTER(s, hop_limit)},
  80. {.name = "hl-eq", .id = O_HL_EQ, .excl = F_ANY, .type = XTTYPE_UINT8,
  81. .flags = XTOPT_INVERT | XTOPT_PUT, XTOPT_POINTER(s, hop_limit)},
  82. {.name = "hl", .id = O_HL_EQ, .excl = F_ANY, .type = XTTYPE_UINT8,
  83. .flags = XTOPT_PUT, XTOPT_POINTER(s, hop_limit)},
  84. XTOPT_TABLEEND,
  85. };
  86. #undef s
  87. static struct xtables_match hl_mt6_reg = {
  88. .name = "hl",
  89. .version = XTABLES_VERSION,
  90. .family = NFPROTO_IPV6,
  91. .size = XT_ALIGN(sizeof(struct ip6t_hl_info)),
  92. .userspacesize = XT_ALIGN(sizeof(struct ip6t_hl_info)),
  93. .help = hl_help,
  94. .print = hl_print,
  95. .save = hl_save,
  96. .x6_parse = hl_parse,
  97. .x6_fcheck = hl_check,
  98. .x6_options = hl_opts,
  99. };
  100. void _init(void)
  101. {
  102. xtables_register_match(&hl_mt6_reg);
  103. }