xt_HL.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * TTL modification target for IP tables
  3. * (C) 2000,2005 by Harald Welte <laforge@netfilter.org>
  4. *
  5. * Hop Limit modification target for ip6tables
  6. * Maciej Soltysiak <solt@dns.toxicfilms.tv>
  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 version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  13. #include <linux/module.h>
  14. #include <linux/skbuff.h>
  15. #include <linux/ip.h>
  16. #include <linux/ipv6.h>
  17. #include <net/checksum.h>
  18. #include <linux/netfilter/x_tables.h>
  19. #include <linux/netfilter_ipv4/ipt_TTL.h>
  20. #include <linux/netfilter_ipv6/ip6t_HL.h>
  21. MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
  22. MODULE_AUTHOR("Maciej Soltysiak <solt@dns.toxicfilms.tv>");
  23. MODULE_DESCRIPTION("Xtables: Hoplimit/TTL Limit field modification target");
  24. MODULE_LICENSE("GPL");
  25. static unsigned int
  26. ttl_tg(struct sk_buff *skb, const struct xt_action_param *par)
  27. {
  28. struct iphdr *iph;
  29. const struct ipt_TTL_info *info = par->targinfo;
  30. int new_ttl;
  31. if (!skb_make_writable(skb, skb->len))
  32. return NF_DROP;
  33. iph = ip_hdr(skb);
  34. switch (info->mode) {
  35. case IPT_TTL_SET:
  36. new_ttl = info->ttl;
  37. break;
  38. case IPT_TTL_INC:
  39. new_ttl = iph->ttl + info->ttl;
  40. if (new_ttl > 255)
  41. new_ttl = 255;
  42. break;
  43. case IPT_TTL_DEC:
  44. new_ttl = iph->ttl - info->ttl;
  45. if (new_ttl < 0)
  46. new_ttl = 0;
  47. break;
  48. default:
  49. new_ttl = iph->ttl;
  50. break;
  51. }
  52. if (new_ttl != iph->ttl) {
  53. csum_replace2(&iph->check, htons(iph->ttl << 8),
  54. htons(new_ttl << 8));
  55. iph->ttl = new_ttl;
  56. }
  57. return XT_CONTINUE;
  58. }
  59. static unsigned int
  60. hl_tg6(struct sk_buff *skb, const struct xt_action_param *par)
  61. {
  62. struct ipv6hdr *ip6h;
  63. const struct ip6t_HL_info *info = par->targinfo;
  64. int new_hl;
  65. if (!skb_make_writable(skb, skb->len))
  66. return NF_DROP;
  67. ip6h = ipv6_hdr(skb);
  68. switch (info->mode) {
  69. case IP6T_HL_SET:
  70. new_hl = info->hop_limit;
  71. break;
  72. case IP6T_HL_INC:
  73. new_hl = ip6h->hop_limit + info->hop_limit;
  74. if (new_hl > 255)
  75. new_hl = 255;
  76. break;
  77. case IP6T_HL_DEC:
  78. new_hl = ip6h->hop_limit - info->hop_limit;
  79. if (new_hl < 0)
  80. new_hl = 0;
  81. break;
  82. default:
  83. new_hl = ip6h->hop_limit;
  84. break;
  85. }
  86. ip6h->hop_limit = new_hl;
  87. return XT_CONTINUE;
  88. }
  89. static int ttl_tg_check(const struct xt_tgchk_param *par)
  90. {
  91. const struct ipt_TTL_info *info = par->targinfo;
  92. if (info->mode > IPT_TTL_MAXMODE) {
  93. pr_info("TTL: invalid or unknown mode %u\n", info->mode);
  94. return -EINVAL;
  95. }
  96. if (info->mode != IPT_TTL_SET && info->ttl == 0)
  97. return -EINVAL;
  98. return 0;
  99. }
  100. static int hl_tg6_check(const struct xt_tgchk_param *par)
  101. {
  102. const struct ip6t_HL_info *info = par->targinfo;
  103. if (info->mode > IP6T_HL_MAXMODE) {
  104. pr_info("invalid or unknown mode %u\n", info->mode);
  105. return -EINVAL;
  106. }
  107. if (info->mode != IP6T_HL_SET && info->hop_limit == 0) {
  108. pr_info("increment/decrement does not "
  109. "make sense with value 0\n");
  110. return -EINVAL;
  111. }
  112. return 0;
  113. }
  114. static struct xt_target hl_tg_reg[] __read_mostly = {
  115. {
  116. .name = "TTL",
  117. .revision = 0,
  118. .family = NFPROTO_IPV4,
  119. .target = ttl_tg,
  120. .targetsize = sizeof(struct ipt_TTL_info),
  121. .table = "mangle",
  122. .checkentry = ttl_tg_check,
  123. .me = THIS_MODULE,
  124. },
  125. {
  126. .name = "HL",
  127. .revision = 0,
  128. .family = NFPROTO_IPV6,
  129. .target = hl_tg6,
  130. .targetsize = sizeof(struct ip6t_HL_info),
  131. .table = "mangle",
  132. .checkentry = hl_tg6_check,
  133. .me = THIS_MODULE,
  134. },
  135. };
  136. static int __init hl_tg_init(void)
  137. {
  138. return xt_register_targets(hl_tg_reg, ARRAY_SIZE(hl_tg_reg));
  139. }
  140. static void __exit hl_tg_exit(void)
  141. {
  142. xt_unregister_targets(hl_tg_reg, ARRAY_SIZE(hl_tg_reg));
  143. }
  144. module_init(hl_tg_init);
  145. module_exit(hl_tg_exit);
  146. MODULE_ALIAS("ipt_TTL");
  147. MODULE_ALIAS("ip6t_HL");