ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / net / ipv4 / netfilter / ipt_mac.c
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>
14
15 #include <linux/netfilter_ipv4/ipt_mac.h>
16 #include <linux/netfilter_ipv4/ip_tables.h>
17
18 MODULE_LICENSE("GPL");
19 MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
20 MODULE_DESCRIPTION("iptables mac matching module");
21
22 static int
23 match(const struct sk_buff *skb,
24       const struct net_device *in,
25       const struct net_device *out,
26       const void *matchinfo,
27       int offset,
28       int *hotdrop)
29 {
30     const struct ipt_mac_info *info = matchinfo;
31
32     /* Is mac pointer valid? */
33     return (skb->mac.raw >= skb->head
34             && (skb->mac.raw + ETH_HLEN) <= skb->data
35             /* If so, compare... */
36             && ((memcmp(skb->mac.ethernet->h_source, info->srcaddr, ETH_ALEN)
37                 == 0) ^ info->invert));
38 }
39
40 static int
41 ipt_mac_checkentry(const char *tablename,
42                    const struct ipt_ip *ip,
43                    void *matchinfo,
44                    unsigned int matchsize,
45                    unsigned int hook_mask)
46 {
47         /* FORWARD isn't always valid, but it's nice to be able to do --RR */
48         if (hook_mask
49             & ~((1 << NF_IP_PRE_ROUTING) | (1 << NF_IP_LOCAL_IN)
50                 | (1 << NF_IP_FORWARD))) {
51                 printk("ipt_mac: only valid for PRE_ROUTING, LOCAL_IN or FORWARD.\n");
52                 return 0;
53         }
54
55         if (matchsize != IPT_ALIGN(sizeof(struct ipt_mac_info)))
56                 return 0;
57
58         return 1;
59 }
60
61 static struct ipt_match mac_match = {
62         .name           = "mac",
63         .match          = &match,
64         .checkentry     = &ipt_mac_checkentry,
65         .me             = THIS_MODULE,
66 };
67
68 static int __init init(void)
69 {
70         return ipt_register_match(&mac_match);
71 }
72
73 static void __exit fini(void)
74 {
75         ipt_unregister_match(&mac_match);
76 }
77
78 module_init(init);
79 module_exit(fini);