libipt_ttl.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* Shared library add-on to iptables to add TTL matching support
  2. * (C) 2000 by Harald Welte <laforge@gnumonks.org>
  3. *
  4. * This program is released under the terms of GNU GPL */
  5. #include <stdio.h>
  6. #include <xtables.h>
  7. #include <linux/netfilter_ipv4/ipt_ttl.h>
  8. enum {
  9. O_TTL_EQ = 0,
  10. O_TTL_LT,
  11. O_TTL_GT,
  12. F_TTL_EQ = 1 << O_TTL_EQ,
  13. F_TTL_LT = 1 << O_TTL_LT,
  14. F_TTL_GT = 1 << O_TTL_GT,
  15. F_ANY = F_TTL_EQ | F_TTL_LT | F_TTL_GT,
  16. };
  17. static void ttl_help(void)
  18. {
  19. printf(
  20. "ttl match options:\n"
  21. "[!] --ttl-eq value Match time to live value\n"
  22. " --ttl-lt value Match TTL < value\n"
  23. " --ttl-gt value Match TTL > value\n");
  24. }
  25. static void ttl_parse(struct xt_option_call *cb)
  26. {
  27. struct ipt_ttl_info *info = cb->data;
  28. xtables_option_parse(cb);
  29. switch (cb->entry->id) {
  30. case O_TTL_EQ:
  31. info->mode = cb->invert ? IPT_TTL_NE : IPT_TTL_EQ;
  32. break;
  33. case O_TTL_LT:
  34. info->mode = IPT_TTL_LT;
  35. break;
  36. case O_TTL_GT:
  37. info->mode = IPT_TTL_GT;
  38. break;
  39. }
  40. }
  41. static void ttl_check(struct xt_fcheck_call *cb)
  42. {
  43. if (!(cb->xflags & F_ANY))
  44. xtables_error(PARAMETER_PROBLEM,
  45. "TTL match: You must specify one of "
  46. "`--ttl-eq', `--ttl-lt', `--ttl-gt");
  47. }
  48. static void ttl_print(const void *ip, const struct xt_entry_match *match,
  49. int numeric)
  50. {
  51. const struct ipt_ttl_info *info =
  52. (struct ipt_ttl_info *) match->data;
  53. printf(" TTL match ");
  54. switch (info->mode) {
  55. case IPT_TTL_EQ:
  56. printf("TTL ==");
  57. break;
  58. case IPT_TTL_NE:
  59. printf("TTL !=");
  60. break;
  61. case IPT_TTL_LT:
  62. printf("TTL <");
  63. break;
  64. case IPT_TTL_GT:
  65. printf("TTL >");
  66. break;
  67. }
  68. printf(" %u", info->ttl);
  69. }
  70. static void ttl_save(const void *ip, const struct xt_entry_match *match)
  71. {
  72. const struct ipt_ttl_info *info =
  73. (struct ipt_ttl_info *) match->data;
  74. switch (info->mode) {
  75. case IPT_TTL_EQ:
  76. printf(" --ttl-eq");
  77. break;
  78. case IPT_TTL_NE:
  79. printf(" ! --ttl-eq");
  80. break;
  81. case IPT_TTL_LT:
  82. printf(" --ttl-lt");
  83. break;
  84. case IPT_TTL_GT:
  85. printf(" --ttl-gt");
  86. break;
  87. default:
  88. /* error */
  89. break;
  90. }
  91. printf(" %u", info->ttl);
  92. }
  93. #define s struct ipt_ttl_info
  94. static const struct xt_option_entry ttl_opts[] = {
  95. {.name = "ttl-lt", .id = O_TTL_LT, .excl = F_ANY, .type = XTTYPE_UINT8,
  96. .flags = XTOPT_PUT, XTOPT_POINTER(s, ttl)},
  97. {.name = "ttl-gt", .id = O_TTL_GT, .excl = F_ANY, .type = XTTYPE_UINT8,
  98. .flags = XTOPT_PUT, XTOPT_POINTER(s, ttl)},
  99. {.name = "ttl-eq", .id = O_TTL_EQ, .excl = F_ANY, .type = XTTYPE_UINT8,
  100. .flags = XTOPT_INVERT | XTOPT_PUT, XTOPT_POINTER(s, ttl)},
  101. {.name = "ttl", .id = O_TTL_EQ, .excl = F_ANY, .type = XTTYPE_UINT8,
  102. .flags = XTOPT_PUT, XTOPT_POINTER(s, ttl)},
  103. XTOPT_TABLEEND,
  104. };
  105. #undef s
  106. static struct xtables_match ttl_mt_reg = {
  107. .name = "ttl",
  108. .version = XTABLES_VERSION,
  109. .family = NFPROTO_IPV4,
  110. .size = XT_ALIGN(sizeof(struct ipt_ttl_info)),
  111. .userspacesize = XT_ALIGN(sizeof(struct ipt_ttl_info)),
  112. .help = ttl_help,
  113. .print = ttl_print,
  114. .save = ttl_save,
  115. .x6_parse = ttl_parse,
  116. .x6_fcheck = ttl_check,
  117. .x6_options = ttl_opts,
  118. };
  119. void _init(void)
  120. {
  121. xtables_register_match(&ttl_mt_reg);
  122. }