xt_hl.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * IP tables module for matching the value of the TTL
  4. * (C) 2000,2001 by Harald Welte <laforge@netfilter.org>
  5. *
  6. * Hop Limit matching module
  7. * (C) 2001-2002 Maciej Soltysiak <solt@dns.toxicfilms.tv>
  8. */
  9. #include <linux/ip.h>
  10. #include <linux/ipv6.h>
  11. #include <linux/module.h>
  12. #include <linux/skbuff.h>
  13. #include <linux/netfilter/x_tables.h>
  14. #include <linux/netfilter_ipv4/ipt_ttl.h>
  15. #include <linux/netfilter_ipv6/ip6t_hl.h>
  16. MODULE_AUTHOR("Maciej Soltysiak <solt@dns.toxicfilms.tv>");
  17. MODULE_DESCRIPTION("Xtables: Hoplimit/TTL field match");
  18. MODULE_LICENSE("GPL");
  19. MODULE_ALIAS("ipt_ttl");
  20. MODULE_ALIAS("ip6t_hl");
  21. static bool ttl_mt(const struct sk_buff *skb, struct xt_action_param *par)
  22. {
  23. const struct ipt_ttl_info *info = par->matchinfo;
  24. const u8 ttl = ip_hdr(skb)->ttl;
  25. switch (info->mode) {
  26. case IPT_TTL_EQ:
  27. return ttl == info->ttl;
  28. case IPT_TTL_NE:
  29. return ttl != info->ttl;
  30. case IPT_TTL_LT:
  31. return ttl < info->ttl;
  32. case IPT_TTL_GT:
  33. return ttl > info->ttl;
  34. }
  35. return false;
  36. }
  37. static bool hl_mt6(const struct sk_buff *skb, struct xt_action_param *par)
  38. {
  39. const struct ip6t_hl_info *info = par->matchinfo;
  40. const struct ipv6hdr *ip6h = ipv6_hdr(skb);
  41. switch (info->mode) {
  42. case IP6T_HL_EQ:
  43. return ip6h->hop_limit == info->hop_limit;
  44. case IP6T_HL_NE:
  45. return ip6h->hop_limit != info->hop_limit;
  46. case IP6T_HL_LT:
  47. return ip6h->hop_limit < info->hop_limit;
  48. case IP6T_HL_GT:
  49. return ip6h->hop_limit > info->hop_limit;
  50. }
  51. return false;
  52. }
  53. static struct xt_match hl_mt_reg[] __read_mostly = {
  54. {
  55. .name = "ttl",
  56. .revision = 0,
  57. .family = NFPROTO_IPV4,
  58. .match = ttl_mt,
  59. .matchsize = sizeof(struct ipt_ttl_info),
  60. .me = THIS_MODULE,
  61. },
  62. {
  63. .name = "hl",
  64. .revision = 0,
  65. .family = NFPROTO_IPV6,
  66. .match = hl_mt6,
  67. .matchsize = sizeof(struct ip6t_hl_info),
  68. .me = THIS_MODULE,
  69. },
  70. };
  71. static int __init hl_mt_init(void)
  72. {
  73. return xt_register_matches(hl_mt_reg, ARRAY_SIZE(hl_mt_reg));
  74. }
  75. static void __exit hl_mt_exit(void)
  76. {
  77. xt_unregister_matches(hl_mt_reg, ARRAY_SIZE(hl_mt_reg));
  78. }
  79. module_init(hl_mt_init);
  80. module_exit(hl_mt_exit);