libip6t_HL.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * IPv6 Hop Limit Target module
  3. * Maciej Soltysiak <solt@dns.toxicfilms.tv>
  4. * Based on HW's ttl target
  5. * This program is distributed under the terms of GNU GPL
  6. */
  7. #include <stdio.h>
  8. #include <xtables.h>
  9. #include <linux/netfilter_ipv6/ip6t_HL.h>
  10. enum {
  11. O_HL_SET = 0,
  12. O_HL_INC,
  13. O_HL_DEC,
  14. F_HL_SET = 1 << O_HL_SET,
  15. F_HL_INC = 1 << O_HL_INC,
  16. F_HL_DEC = 1 << O_HL_DEC,
  17. F_ANY = F_HL_SET | F_HL_INC | F_HL_DEC,
  18. };
  19. #define s struct ip6t_HL_info
  20. static const struct xt_option_entry HL_opts[] = {
  21. {.name = "hl-set", .type = XTTYPE_UINT8, .id = O_HL_SET,
  22. .excl = F_ANY, .flags = XTOPT_PUT, XTOPT_POINTER(s, hop_limit)},
  23. {.name = "hl-dec", .type = XTTYPE_UINT8, .id = O_HL_DEC,
  24. .excl = F_ANY, .flags = XTOPT_PUT, XTOPT_POINTER(s, hop_limit),
  25. .min = 1},
  26. {.name = "hl-inc", .type = XTTYPE_UINT8, .id = O_HL_INC,
  27. .excl = F_ANY, .flags = XTOPT_PUT, XTOPT_POINTER(s, hop_limit),
  28. .min = 1},
  29. XTOPT_TABLEEND,
  30. };
  31. #undef s
  32. static void HL_help(void)
  33. {
  34. printf(
  35. "HL target options\n"
  36. " --hl-set value Set HL to <value 0-255>\n"
  37. " --hl-dec value Decrement HL by <value 1-255>\n"
  38. " --hl-inc value Increment HL by <value 1-255>\n");
  39. }
  40. static void HL_parse(struct xt_option_call *cb)
  41. {
  42. struct ip6t_HL_info *info = cb->data;
  43. xtables_option_parse(cb);
  44. switch (cb->entry->id) {
  45. case O_HL_SET:
  46. info->mode = IP6T_HL_SET;
  47. break;
  48. case O_HL_INC:
  49. info->mode = IP6T_HL_INC;
  50. break;
  51. case O_HL_DEC:
  52. info->mode = IP6T_HL_DEC;
  53. break;
  54. }
  55. }
  56. static void HL_check(struct xt_fcheck_call *cb)
  57. {
  58. if (!(cb->xflags & F_ANY))
  59. xtables_error(PARAMETER_PROBLEM,
  60. "HL: You must specify an action");
  61. }
  62. static void HL_save(const void *ip, const struct xt_entry_target *target)
  63. {
  64. const struct ip6t_HL_info *info =
  65. (struct ip6t_HL_info *) target->data;
  66. switch (info->mode) {
  67. case IP6T_HL_SET:
  68. printf(" --hl-set");
  69. break;
  70. case IP6T_HL_DEC:
  71. printf(" --hl-dec");
  72. break;
  73. case IP6T_HL_INC:
  74. printf(" --hl-inc");
  75. break;
  76. }
  77. printf(" %u", info->hop_limit);
  78. }
  79. static void HL_print(const void *ip, const struct xt_entry_target *target,
  80. int numeric)
  81. {
  82. const struct ip6t_HL_info *info =
  83. (struct ip6t_HL_info *) target->data;
  84. printf(" HL ");
  85. switch (info->mode) {
  86. case IP6T_HL_SET:
  87. printf("set to");
  88. break;
  89. case IP6T_HL_DEC:
  90. printf("decrement by");
  91. break;
  92. case IP6T_HL_INC:
  93. printf("increment by");
  94. break;
  95. }
  96. printf(" %u", info->hop_limit);
  97. }
  98. static struct xtables_target hl_tg6_reg = {
  99. .name = "HL",
  100. .version = XTABLES_VERSION,
  101. .family = NFPROTO_IPV6,
  102. .size = XT_ALIGN(sizeof(struct ip6t_HL_info)),
  103. .userspacesize = XT_ALIGN(sizeof(struct ip6t_HL_info)),
  104. .help = HL_help,
  105. .print = HL_print,
  106. .save = HL_save,
  107. .x6_parse = HL_parse,
  108. .x6_fcheck = HL_check,
  109. .x6_options = HL_opts,
  110. };
  111. void _init(void)
  112. {
  113. xtables_register_target(&hl_tg6_reg);
  114. }