Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* Kernel module to match MAC address parameters. */ |
| 2 | |
| 3 | /* (C) 1999-2001 Paul `Rusty' Russell |
| 4 | * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org> |
| 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 | |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/skbuff.h> |
| 13 | #include <linux/if_ether.h> |
Kris Katterjohn | d3f4a68 | 2006-01-09 16:01:43 -0800 | [diff] [blame^] | 14 | #include <linux/etherdevice.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | |
| 16 | #include <linux/netfilter_ipv4/ipt_mac.h> |
| 17 | #include <linux/netfilter_ipv4/ip_tables.h> |
| 18 | |
| 19 | MODULE_LICENSE("GPL"); |
| 20 | MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>"); |
| 21 | MODULE_DESCRIPTION("iptables mac matching module"); |
| 22 | |
| 23 | static int |
| 24 | match(const struct sk_buff *skb, |
| 25 | const struct net_device *in, |
| 26 | const struct net_device *out, |
| 27 | const void *matchinfo, |
| 28 | int offset, |
| 29 | int *hotdrop) |
| 30 | { |
| 31 | const struct ipt_mac_info *info = matchinfo; |
| 32 | |
| 33 | /* Is mac pointer valid? */ |
| 34 | return (skb->mac.raw >= skb->head |
| 35 | && (skb->mac.raw + ETH_HLEN) <= skb->data |
| 36 | /* If so, compare... */ |
Kris Katterjohn | d3f4a68 | 2006-01-09 16:01:43 -0800 | [diff] [blame^] | 37 | && ((!compare_ether_addr(eth_hdr(skb)->h_source, info->srcaddr)) |
| 38 | ^ info->invert)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | static int |
| 42 | ipt_mac_checkentry(const char *tablename, |
| 43 | const struct ipt_ip *ip, |
| 44 | void *matchinfo, |
| 45 | unsigned int matchsize, |
| 46 | unsigned int hook_mask) |
| 47 | { |
| 48 | /* FORWARD isn't always valid, but it's nice to be able to do --RR */ |
| 49 | if (hook_mask |
| 50 | & ~((1 << NF_IP_PRE_ROUTING) | (1 << NF_IP_LOCAL_IN) |
| 51 | | (1 << NF_IP_FORWARD))) { |
| 52 | printk("ipt_mac: only valid for PRE_ROUTING, LOCAL_IN or FORWARD.\n"); |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | if (matchsize != IPT_ALIGN(sizeof(struct ipt_mac_info))) |
| 57 | return 0; |
| 58 | |
| 59 | return 1; |
| 60 | } |
| 61 | |
| 62 | static struct ipt_match mac_match = { |
| 63 | .name = "mac", |
| 64 | .match = &match, |
| 65 | .checkentry = &ipt_mac_checkentry, |
| 66 | .me = THIS_MODULE, |
| 67 | }; |
| 68 | |
| 69 | static int __init init(void) |
| 70 | { |
| 71 | return ipt_register_match(&mac_match); |
| 72 | } |
| 73 | |
| 74 | static void __exit fini(void) |
| 75 | { |
| 76 | ipt_unregister_match(&mac_match); |
| 77 | } |
| 78 | |
| 79 | module_init(init); |
| 80 | module_exit(fini); |