xt_hl.c 2.3 KB

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