ipt_addrtype.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * iptables module to match inet_addr_type() of an ip.
  3. *
  4. * Copyright (c) 2004 Patrick McHardy <kaber@trash.net>
  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/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/skbuff.h>
  13. #include <linux/netdevice.h>
  14. #include <linux/ip.h>
  15. #include <net/route.h>
  16. #include <linux/netfilter_ipv4/ipt_addrtype.h>
  17. #include <linux/netfilter/x_tables.h>
  18. MODULE_LICENSE("GPL");
  19. MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
  20. MODULE_DESCRIPTION("iptables addrtype match");
  21. static inline int match_type(__be32 addr, u_int16_t mask)
  22. {
  23. return !!(mask & (1 << inet_addr_type(addr)));
  24. }
  25. static int match(const struct sk_buff *skb,
  26. const struct net_device *in, const struct net_device *out,
  27. const struct xt_match *match, const void *matchinfo,
  28. int offset, unsigned int protoff, int *hotdrop)
  29. {
  30. const struct ipt_addrtype_info *info = matchinfo;
  31. const struct iphdr *iph = skb->nh.iph;
  32. int ret = 1;
  33. if (info->source)
  34. ret &= match_type(iph->saddr, info->source)^info->invert_source;
  35. if (info->dest)
  36. ret &= match_type(iph->daddr, info->dest)^info->invert_dest;
  37. return ret;
  38. }
  39. static struct xt_match addrtype_match = {
  40. .name = "addrtype",
  41. .family = AF_INET,
  42. .match = match,
  43. .matchsize = sizeof(struct ipt_addrtype_info),
  44. .me = THIS_MODULE
  45. };
  46. static int __init ipt_addrtype_init(void)
  47. {
  48. return xt_register_match(&addrtype_match);
  49. }
  50. static void __exit ipt_addrtype_fini(void)
  51. {
  52. xt_unregister_match(&addrtype_match);
  53. }
  54. module_init(ipt_addrtype_init);
  55. module_exit(ipt_addrtype_fini);