xt_mac.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /* Kernel module to match MAC address parameters. */
  2. /* (C) 1999-2001 Paul `Rusty' Russell
  3. * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <linux/module.h>
  10. #include <linux/skbuff.h>
  11. #include <linux/if_arp.h>
  12. #include <linux/if_ether.h>
  13. #include <linux/etherdevice.h>
  14. #include <linux/netfilter_ipv4.h>
  15. #include <linux/netfilter_ipv6.h>
  16. #include <linux/netfilter/xt_mac.h>
  17. #include <linux/netfilter/x_tables.h>
  18. MODULE_LICENSE("GPL");
  19. MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
  20. MODULE_DESCRIPTION("Xtables: MAC address match");
  21. MODULE_ALIAS("ipt_mac");
  22. MODULE_ALIAS("ip6t_mac");
  23. static bool mac_mt(const struct sk_buff *skb, struct xt_action_param *par)
  24. {
  25. const struct xt_mac_info *info = par->matchinfo;
  26. bool ret;
  27. if (skb->dev == NULL || skb->dev->type != ARPHRD_ETHER)
  28. return false;
  29. if (skb_mac_header(skb) < skb->head)
  30. return false;
  31. if (skb_mac_header(skb) + ETH_HLEN > skb->data)
  32. return false;
  33. ret = ether_addr_equal(eth_hdr(skb)->h_source, info->srcaddr);
  34. ret ^= info->invert;
  35. return ret;
  36. }
  37. static struct xt_match mac_mt_reg __read_mostly = {
  38. .name = "mac",
  39. .revision = 0,
  40. .family = NFPROTO_UNSPEC,
  41. .match = mac_mt,
  42. .matchsize = sizeof(struct xt_mac_info),
  43. .hooks = (1 << NF_INET_PRE_ROUTING) | (1 << NF_INET_LOCAL_IN) |
  44. (1 << NF_INET_FORWARD),
  45. .me = THIS_MODULE,
  46. };
  47. static int __init mac_mt_init(void)
  48. {
  49. return xt_register_match(&mac_mt_reg);
  50. }
  51. static void __exit mac_mt_exit(void)
  52. {
  53. xt_unregister_match(&mac_mt_reg);
  54. }
  55. module_init(mac_mt_init);
  56. module_exit(mac_mt_exit);