xt_mac.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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_ether.h>
  12. #include <linux/etherdevice.h>
  13. #include <linux/netfilter_ipv4.h>
  14. #include <linux/netfilter_ipv6.h>
  15. #include <linux/netfilter/xt_mac.h>
  16. #include <linux/netfilter/x_tables.h>
  17. MODULE_LICENSE("GPL");
  18. MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
  19. MODULE_DESCRIPTION("iptables mac matching module");
  20. MODULE_ALIAS("ipt_mac");
  21. MODULE_ALIAS("ip6t_mac");
  22. static int
  23. match(const struct sk_buff *skb,
  24. const struct net_device *in,
  25. const struct net_device *out,
  26. const struct xt_match *match,
  27. const void *matchinfo,
  28. int offset,
  29. unsigned int protoff,
  30. int *hotdrop)
  31. {
  32. const struct xt_mac_info *info = matchinfo;
  33. /* Is mac pointer valid? */
  34. return (skb->mac.raw >= skb->head
  35. && (skb->mac.raw + ETH_HLEN) <= skb->data
  36. /* If so, compare... */
  37. && ((!compare_ether_addr(eth_hdr(skb)->h_source, info->srcaddr))
  38. ^ info->invert));
  39. }
  40. static struct xt_match xt_mac_match[] = {
  41. {
  42. .name = "mac",
  43. .family = AF_INET,
  44. .match = match,
  45. .matchsize = sizeof(struct xt_mac_info),
  46. .hooks = (1 << NF_IP_PRE_ROUTING) |
  47. (1 << NF_IP_LOCAL_IN) |
  48. (1 << NF_IP_FORWARD),
  49. .me = THIS_MODULE,
  50. },
  51. {
  52. .name = "mac",
  53. .family = AF_INET6,
  54. .match = match,
  55. .matchsize = sizeof(struct xt_mac_info),
  56. .hooks = (1 << NF_IP6_PRE_ROUTING) |
  57. (1 << NF_IP6_LOCAL_IN) |
  58. (1 << NF_IP6_FORWARD),
  59. .me = THIS_MODULE,
  60. },
  61. };
  62. static int __init xt_mac_init(void)
  63. {
  64. return xt_register_matches(xt_mac_match, ARRAY_SIZE(xt_mac_match));
  65. }
  66. static void __exit xt_mac_fini(void)
  67. {
  68. xt_unregister_matches(xt_mac_match, ARRAY_SIZE(xt_mac_match));
  69. }
  70. module_init(xt_mac_init);
  71. module_exit(xt_mac_fini);