ipt_iprange.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * iptables module to match IP address ranges
  3. *
  4. * (C) 2003 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/skbuff.h>
  12. #include <linux/ip.h>
  13. #include <linux/netfilter/x_tables.h>
  14. #include <linux/netfilter_ipv4/ipt_iprange.h>
  15. MODULE_LICENSE("GPL");
  16. MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
  17. MODULE_DESCRIPTION("iptables arbitrary IP range match module");
  18. #if 0
  19. #define DEBUGP printk
  20. #else
  21. #define DEBUGP(format, args...)
  22. #endif
  23. static int
  24. match(const struct sk_buff *skb,
  25. const struct net_device *in,
  26. const struct net_device *out,
  27. const struct xt_match *match,
  28. const void *matchinfo,
  29. int offset, unsigned int protoff, int *hotdrop)
  30. {
  31. const struct ipt_iprange_info *info = matchinfo;
  32. const struct iphdr *iph = skb->nh.iph;
  33. if (info->flags & IPRANGE_SRC) {
  34. if (((ntohl(iph->saddr) < ntohl(info->src.min_ip))
  35. || (ntohl(iph->saddr) > ntohl(info->src.max_ip)))
  36. ^ !!(info->flags & IPRANGE_SRC_INV)) {
  37. DEBUGP("src IP %u.%u.%u.%u NOT in range %s"
  38. "%u.%u.%u.%u-%u.%u.%u.%u\n",
  39. NIPQUAD(iph->saddr),
  40. info->flags & IPRANGE_SRC_INV ? "(INV) " : "",
  41. NIPQUAD(info->src.min_ip),
  42. NIPQUAD(info->src.max_ip));
  43. return 0;
  44. }
  45. }
  46. if (info->flags & IPRANGE_DST) {
  47. if (((ntohl(iph->daddr) < ntohl(info->dst.min_ip))
  48. || (ntohl(iph->daddr) > ntohl(info->dst.max_ip)))
  49. ^ !!(info->flags & IPRANGE_DST_INV)) {
  50. DEBUGP("dst IP %u.%u.%u.%u NOT in range %s"
  51. "%u.%u.%u.%u-%u.%u.%u.%u\n",
  52. NIPQUAD(iph->daddr),
  53. info->flags & IPRANGE_DST_INV ? "(INV) " : "",
  54. NIPQUAD(info->dst.min_ip),
  55. NIPQUAD(info->dst.max_ip));
  56. return 0;
  57. }
  58. }
  59. return 1;
  60. }
  61. static struct xt_match iprange_match = {
  62. .name = "iprange",
  63. .family = AF_INET,
  64. .match = match,
  65. .matchsize = sizeof(struct ipt_iprange_info),
  66. .me = THIS_MODULE
  67. };
  68. static int __init ipt_iprange_init(void)
  69. {
  70. return xt_register_match(&iprange_match);
  71. }
  72. static void __exit ipt_iprange_fini(void)
  73. {
  74. xt_unregister_match(&iprange_match);
  75. }
  76. module_init(ipt_iprange_init);
  77. module_exit(ipt_iprange_fini);