netfilter: change Ebtables function signatures to match Xtables's
[safe/jmp/linux-2.6] / net / bridge / netfilter / ebt_arp.c
1 /*
2  *  ebt_arp
3  *
4  *      Authors:
5  *      Bart De Schuymer <bdschuym@pandora.be>
6  *      Tim Gardner <timg@tpi.com>
7  *
8  *  April, 2002
9  *
10  */
11 #include <linux/if_arp.h>
12 #include <linux/if_ether.h>
13 #include <linux/module.h>
14 #include <linux/netfilter/x_tables.h>
15 #include <linux/netfilter_bridge/ebtables.h>
16 #include <linux/netfilter_bridge/ebt_arp.h>
17
18 static bool
19 ebt_arp_mt(const struct sk_buff *skb, const struct net_device *in,
20            const struct net_device *out, const struct xt_match *match,
21            const void *data, int offset, unsigned int protoff, bool *hotdrop)
22 {
23         const struct ebt_arp_info *info = data;
24         const struct arphdr *ah;
25         struct arphdr _arph;
26
27         ah = skb_header_pointer(skb, 0, sizeof(_arph), &_arph);
28         if (ah == NULL)
29                 return false;
30         if (info->bitmask & EBT_ARP_OPCODE && FWINV(info->opcode !=
31            ah->ar_op, EBT_ARP_OPCODE))
32                 return false;
33         if (info->bitmask & EBT_ARP_HTYPE && FWINV(info->htype !=
34            ah->ar_hrd, EBT_ARP_HTYPE))
35                 return false;
36         if (info->bitmask & EBT_ARP_PTYPE && FWINV(info->ptype !=
37            ah->ar_pro, EBT_ARP_PTYPE))
38                 return false;
39
40         if (info->bitmask & (EBT_ARP_SRC_IP | EBT_ARP_DST_IP | EBT_ARP_GRAT)) {
41                 const __be32 *sap, *dap;
42                 __be32 saddr, daddr;
43
44                 if (ah->ar_pln != sizeof(__be32) || ah->ar_pro != htons(ETH_P_IP))
45                         return false;
46                 sap = skb_header_pointer(skb, sizeof(struct arphdr) +
47                                         ah->ar_hln, sizeof(saddr),
48                                         &saddr);
49                 if (sap == NULL)
50                         return false;
51                 dap = skb_header_pointer(skb, sizeof(struct arphdr) +
52                                         2*ah->ar_hln+sizeof(saddr),
53                                         sizeof(daddr), &daddr);
54                 if (dap == NULL)
55                         return false;
56                 if (info->bitmask & EBT_ARP_SRC_IP &&
57                     FWINV(info->saddr != (*sap & info->smsk), EBT_ARP_SRC_IP))
58                         return false;
59                 if (info->bitmask & EBT_ARP_DST_IP &&
60                     FWINV(info->daddr != (*dap & info->dmsk), EBT_ARP_DST_IP))
61                         return false;
62                 if (info->bitmask & EBT_ARP_GRAT &&
63                     FWINV(*dap != *sap, EBT_ARP_GRAT))
64                         return false;
65         }
66
67         if (info->bitmask & (EBT_ARP_SRC_MAC | EBT_ARP_DST_MAC)) {
68                 const unsigned char *mp;
69                 unsigned char _mac[ETH_ALEN];
70                 uint8_t verdict, i;
71
72                 if (ah->ar_hln != ETH_ALEN || ah->ar_hrd != htons(ARPHRD_ETHER))
73                         return false;
74                 if (info->bitmask & EBT_ARP_SRC_MAC) {
75                         mp = skb_header_pointer(skb, sizeof(struct arphdr),
76                                                 sizeof(_mac), &_mac);
77                         if (mp == NULL)
78                                 return false;
79                         verdict = 0;
80                         for (i = 0; i < 6; i++)
81                                 verdict |= (mp[i] ^ info->smaddr[i]) &
82                                        info->smmsk[i];
83                         if (FWINV(verdict != 0, EBT_ARP_SRC_MAC))
84                                 return false;
85                 }
86
87                 if (info->bitmask & EBT_ARP_DST_MAC) {
88                         mp = skb_header_pointer(skb, sizeof(struct arphdr) +
89                                                 ah->ar_hln + ah->ar_pln,
90                                                 sizeof(_mac), &_mac);
91                         if (mp == NULL)
92                                 return false;
93                         verdict = 0;
94                         for (i = 0; i < 6; i++)
95                                 verdict |= (mp[i] ^ info->dmaddr[i]) &
96                                         info->dmmsk[i];
97                         if (FWINV(verdict != 0, EBT_ARP_DST_MAC))
98                                 return false;
99                 }
100         }
101
102         return true;
103 }
104
105 static bool
106 ebt_arp_mt_check(const char *table, const void *entry,
107                  const struct xt_match *match, void *data,
108                  unsigned int hook_mask)
109 {
110         const struct ebt_arp_info *info = data;
111         const struct ebt_entry *e = entry;
112
113         if ((e->ethproto != htons(ETH_P_ARP) &&
114            e->ethproto != htons(ETH_P_RARP)) ||
115            e->invflags & EBT_IPROTO)
116                 return false;
117         if (info->bitmask & ~EBT_ARP_MASK || info->invflags & ~EBT_ARP_MASK)
118                 return false;
119         return true;
120 }
121
122 static struct ebt_match filter_arp __read_mostly = {
123         .name           = EBT_ARP_MATCH,
124         .revision       = 0,
125         .family         = NFPROTO_BRIDGE,
126         .match          = ebt_arp_mt,
127         .checkentry     = ebt_arp_mt_check,
128         .matchsize      = XT_ALIGN(sizeof(struct ebt_arp_info)),
129         .me             = THIS_MODULE,
130 };
131
132 static int __init ebt_arp_init(void)
133 {
134         return ebt_register_match(&filter_arp);
135 }
136
137 static void __exit ebt_arp_fini(void)
138 {
139         ebt_unregister_match(&filter_arp);
140 }
141
142 module_init(ebt_arp_init);
143 module_exit(ebt_arp_fini);
144 MODULE_DESCRIPTION("Ebtables: ARP protocol packet match");
145 MODULE_LICENSE("GPL");