libipt_TTL.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* Shared library add-on to iptables for the TTL target
  2. * (C) 2000 by Harald Welte <laforge@gnumonks.org>
  3. *
  4. * This program is distributed under the terms of GNU GPL
  5. */
  6. #include <stdio.h>
  7. #include <xtables.h>
  8. #include <linux/netfilter_ipv4/ipt_TTL.h>
  9. enum {
  10. O_TTL_SET = 0,
  11. O_TTL_INC,
  12. O_TTL_DEC,
  13. F_TTL_SET = 1 << O_TTL_SET,
  14. F_TTL_INC = 1 << O_TTL_INC,
  15. F_TTL_DEC = 1 << O_TTL_DEC,
  16. F_ANY = F_TTL_SET | F_TTL_INC | F_TTL_DEC,
  17. };
  18. #define s struct ipt_TTL_info
  19. static const struct xt_option_entry TTL_opts[] = {
  20. {.name = "ttl-set", .type = XTTYPE_UINT8, .id = O_TTL_SET,
  21. .excl = F_ANY, .flags = XTOPT_PUT, XTOPT_POINTER(s, ttl)},
  22. {.name = "ttl-dec", .type = XTTYPE_UINT8, .id = O_TTL_DEC,
  23. .excl = F_ANY, .flags = XTOPT_PUT, XTOPT_POINTER(s, ttl),
  24. .min = 1},
  25. {.name = "ttl-inc", .type = XTTYPE_UINT8, .id = O_TTL_INC,
  26. .excl = F_ANY, .flags = XTOPT_PUT, XTOPT_POINTER(s, ttl),
  27. .min = 1},
  28. XTOPT_TABLEEND,
  29. };
  30. #undef s
  31. static void TTL_help(void)
  32. {
  33. printf(
  34. "TTL target options\n"
  35. " --ttl-set value Set TTL to <value 0-255>\n"
  36. " --ttl-dec value Decrement TTL by <value 1-255>\n"
  37. " --ttl-inc value Increment TTL by <value 1-255>\n");
  38. }
  39. static void TTL_parse(struct xt_option_call *cb)
  40. {
  41. struct ipt_TTL_info *info = cb->data;
  42. xtables_option_parse(cb);
  43. switch (cb->entry->id) {
  44. case O_TTL_SET:
  45. info->mode = IPT_TTL_SET;
  46. break;
  47. case O_TTL_DEC:
  48. info->mode = IPT_TTL_DEC;
  49. break;
  50. case O_TTL_INC:
  51. info->mode = IPT_TTL_INC;
  52. break;
  53. }
  54. }
  55. static void TTL_check(struct xt_fcheck_call *cb)
  56. {
  57. if (!(cb->xflags & F_ANY))
  58. xtables_error(PARAMETER_PROBLEM,
  59. "TTL: You must specify an action");
  60. }
  61. static void TTL_save(const void *ip, const struct xt_entry_target *target)
  62. {
  63. const struct ipt_TTL_info *info =
  64. (struct ipt_TTL_info *) target->data;
  65. switch (info->mode) {
  66. case IPT_TTL_SET:
  67. printf(" --ttl-set");
  68. break;
  69. case IPT_TTL_DEC:
  70. printf(" --ttl-dec");
  71. break;
  72. case IPT_TTL_INC:
  73. printf(" --ttl-inc");
  74. break;
  75. }
  76. printf(" %u", info->ttl);
  77. }
  78. static void TTL_print(const void *ip, const struct xt_entry_target *target,
  79. int numeric)
  80. {
  81. const struct ipt_TTL_info *info =
  82. (struct ipt_TTL_info *) target->data;
  83. printf(" TTL ");
  84. switch (info->mode) {
  85. case IPT_TTL_SET:
  86. printf("set to");
  87. break;
  88. case IPT_TTL_DEC:
  89. printf("decrement by");
  90. break;
  91. case IPT_TTL_INC:
  92. printf("increment by");
  93. break;
  94. }
  95. printf(" %u", info->ttl);
  96. }
  97. static struct xtables_target ttl_tg_reg = {
  98. .name = "TTL",
  99. .version = XTABLES_VERSION,
  100. .family = NFPROTO_IPV4,
  101. .size = XT_ALIGN(sizeof(struct ipt_TTL_info)),
  102. .userspacesize = XT_ALIGN(sizeof(struct ipt_TTL_info)),
  103. .help = TTL_help,
  104. .print = TTL_print,
  105. .save = TTL_save,
  106. .x6_parse = TTL_parse,
  107. .x6_fcheck = TTL_check,
  108. .x6_options = TTL_opts,
  109. };
  110. void _init(void)
  111. {
  112. xtables_register_target(&ttl_tg_reg);
  113. }