xt_iprange.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * xt_iprange - Netfilter module to match IP address ranges
  3. *
  4. * (C) 2003 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
  5. * (C) CC Computer Consultants GmbH, 2008
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12. #include <linux/module.h>
  13. #include <linux/skbuff.h>
  14. #include <linux/ip.h>
  15. #include <linux/ipv6.h>
  16. #include <linux/netfilter/x_tables.h>
  17. #include <linux/netfilter/xt_iprange.h>
  18. static bool
  19. iprange_mt4(const struct sk_buff *skb, struct xt_action_param *par)
  20. {
  21. const struct xt_iprange_mtinfo *info = par->matchinfo;
  22. const struct iphdr *iph = ip_hdr(skb);
  23. bool m;
  24. if (info->flags & IPRANGE_SRC) {
  25. m = ntohl(iph->saddr) < ntohl(info->src_min.ip);
  26. m |= ntohl(iph->saddr) > ntohl(info->src_max.ip);
  27. m ^= !!(info->flags & IPRANGE_SRC_INV);
  28. if (m) {
  29. pr_debug("src IP %pI4 NOT in range %s%pI4-%pI4\n",
  30. &iph->saddr,
  31. (info->flags & IPRANGE_SRC_INV) ? "(INV) " : "",
  32. &info->src_min.ip,
  33. &info->src_max.ip);
  34. return false;
  35. }
  36. }
  37. if (info->flags & IPRANGE_DST) {
  38. m = ntohl(iph->daddr) < ntohl(info->dst_min.ip);
  39. m |= ntohl(iph->daddr) > ntohl(info->dst_max.ip);
  40. m ^= !!(info->flags & IPRANGE_DST_INV);
  41. if (m) {
  42. pr_debug("dst IP %pI4 NOT in range %s%pI4-%pI4\n",
  43. &iph->daddr,
  44. (info->flags & IPRANGE_DST_INV) ? "(INV) " : "",
  45. &info->dst_min.ip,
  46. &info->dst_max.ip);
  47. return false;
  48. }
  49. }
  50. return true;
  51. }
  52. static inline int
  53. iprange_ipv6_lt(const struct in6_addr *a, const struct in6_addr *b)
  54. {
  55. unsigned int i;
  56. for (i = 0; i < 4; ++i) {
  57. if (a->s6_addr32[i] != b->s6_addr32[i])
  58. return ntohl(a->s6_addr32[i]) < ntohl(b->s6_addr32[i]);
  59. }
  60. return 0;
  61. }
  62. static bool
  63. iprange_mt6(const struct sk_buff *skb, struct xt_action_param *par)
  64. {
  65. const struct xt_iprange_mtinfo *info = par->matchinfo;
  66. const struct ipv6hdr *iph = ipv6_hdr(skb);
  67. bool m;
  68. if (info->flags & IPRANGE_SRC) {
  69. m = iprange_ipv6_lt(&iph->saddr, &info->src_min.in6);
  70. m |= iprange_ipv6_lt(&info->src_max.in6, &iph->saddr);
  71. m ^= !!(info->flags & IPRANGE_SRC_INV);
  72. if (m) {
  73. pr_debug("src IP %pI6 NOT in range %s%pI6-%pI6\n",
  74. &iph->saddr,
  75. (info->flags & IPRANGE_SRC_INV) ? "(INV) " : "",
  76. &info->src_min.in6,
  77. &info->src_max.in6);
  78. return false;
  79. }
  80. }
  81. if (info->flags & IPRANGE_DST) {
  82. m = iprange_ipv6_lt(&iph->daddr, &info->dst_min.in6);
  83. m |= iprange_ipv6_lt(&info->dst_max.in6, &iph->daddr);
  84. m ^= !!(info->flags & IPRANGE_DST_INV);
  85. if (m) {
  86. pr_debug("dst IP %pI6 NOT in range %s%pI6-%pI6\n",
  87. &iph->daddr,
  88. (info->flags & IPRANGE_DST_INV) ? "(INV) " : "",
  89. &info->dst_min.in6,
  90. &info->dst_max.in6);
  91. return false;
  92. }
  93. }
  94. return true;
  95. }
  96. static struct xt_match iprange_mt_reg[] __read_mostly = {
  97. {
  98. .name = "iprange",
  99. .revision = 1,
  100. .family = NFPROTO_IPV4,
  101. .match = iprange_mt4,
  102. .matchsize = sizeof(struct xt_iprange_mtinfo),
  103. .me = THIS_MODULE,
  104. },
  105. {
  106. .name = "iprange",
  107. .revision = 1,
  108. .family = NFPROTO_IPV6,
  109. .match = iprange_mt6,
  110. .matchsize = sizeof(struct xt_iprange_mtinfo),
  111. .me = THIS_MODULE,
  112. },
  113. };
  114. static int __init iprange_mt_init(void)
  115. {
  116. return xt_register_matches(iprange_mt_reg, ARRAY_SIZE(iprange_mt_reg));
  117. }
  118. static void __exit iprange_mt_exit(void)
  119. {
  120. xt_unregister_matches(iprange_mt_reg, ARRAY_SIZE(iprange_mt_reg));
  121. }
  122. module_init(iprange_mt_init);
  123. module_exit(iprange_mt_exit);
  124. MODULE_LICENSE("GPL");
  125. MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
  126. MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>");
  127. MODULE_DESCRIPTION("Xtables: arbitrary IPv4 range matching");
  128. MODULE_ALIAS("ipt_iprange");
  129. MODULE_ALIAS("ip6t_iprange");