ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / net / bridge / netfilter / ebt_among.c
1 /*
2  *  ebt_among
3  *
4  *      Authors:
5  *      Grzegorz Borowiak <grzes@gnu.univ.gda.pl>
6  *
7  *  August, 2003
8  *
9  */
10
11 #include <linux/netfilter_bridge/ebtables.h>
12 #include <linux/netfilter_bridge/ebt_among.h>
13 #include <linux/ip.h>
14 #include <linux/if_arp.h>
15 #include <linux/module.h>
16
17 static int ebt_mac_wormhash_contains(const struct ebt_mac_wormhash *wh,
18                                      const char *mac, uint32_t ip)
19 {
20         /* You may be puzzled as to how this code works.
21          * Some tricks were used, refer to 
22          *      include/linux/netfilter_bridge/ebt_among.h
23          * as there you can find a solution of this mystery.
24          */
25         const struct ebt_mac_wormhash_tuple *p;
26         int start, limit, i;
27         uint32_t cmp[2] = { 0, 0 };
28         int key = (const unsigned char) mac[5];
29
30         memcpy(((char *) cmp) + 2, mac, 6);
31         start = wh->table[key];
32         limit = wh->table[key + 1];
33         if (ip) {
34                 for (i = start; i < limit; i++) {
35                         p = &wh->pool[i];
36                         if (cmp[1] == p->cmp[1] && cmp[0] == p->cmp[0]) {
37                                 if (p->ip == 0 || p->ip == ip) {
38                                         return 1;
39                                 }
40                         }
41                 }
42         } else {
43                 for (i = start; i < limit; i++) {
44                         p = &wh->pool[i];
45                         if (cmp[1] == p->cmp[1] && cmp[0] == p->cmp[0]) {
46                                 if (p->ip == 0) {
47                                         return 1;
48                                 }
49                         }
50                 }
51         }
52         return 0;
53 }
54
55 static int ebt_mac_wormhash_check_integrity(const struct ebt_mac_wormhash
56                                             *wh)
57 {
58         int i;
59
60         for (i = 0; i < 256; i++) {
61                 if (wh->table[i] > wh->table[i + 1])
62                         return -0x100 - i;
63                 if (wh->table[i] < 0)
64                         return -0x200 - i;
65                 if (wh->table[i] > wh->poolsize)
66                         return -0x300 - i;
67         }
68         if (wh->table[256] > wh->poolsize)
69                 return -0xc00;
70         return 0;
71 }
72
73 static int get_ip_dst(const struct sk_buff *skb, uint32_t *addr)
74 {
75         if (skb->mac.ethernet->h_proto == __constant_htons(ETH_P_IP)) {
76                 struct iphdr iph;
77
78                 if (skb_copy_bits(skb, 0, &iph, sizeof(iph)))
79                         return -1;
80                 *addr = iph.daddr;
81         } else if (skb->mac.ethernet->h_proto == __constant_htons(ETH_P_ARP)) {
82                 struct arphdr arph;
83
84                 if (skb_copy_bits(skb, 0, &arph, sizeof(arph)) ||
85                     arph.ar_pln != sizeof(uint32_t) || arph.ar_hln != ETH_ALEN)
86                         return -1;
87                 if (skb_copy_bits(skb, sizeof(struct arphdr) +
88                     2 * ETH_ALEN + sizeof(uint32_t), addr, sizeof(uint32_t)))
89                         return -1;
90         }
91         return 0;
92 }
93
94 static int get_ip_src(const struct sk_buff *skb, uint32_t *addr)
95 {
96         if (skb->mac.ethernet->h_proto == __constant_htons(ETH_P_IP)) {
97                 struct iphdr iph;
98
99                 if (skb_copy_bits(skb, 0, &iph, sizeof(iph)))
100                         return -1;
101                 *addr = iph.saddr;
102         } else if (skb->mac.ethernet->h_proto == __constant_htons(ETH_P_ARP)) {
103                 struct arphdr arph;
104
105                 if (skb_copy_bits(skb, 0, &arph, sizeof(arph)) ||
106                     arph.ar_pln != sizeof(uint32_t) || arph.ar_hln != ETH_ALEN)
107                         return -1;
108                 if (skb_copy_bits(skb, sizeof(struct arphdr) +
109                     ETH_ALEN, addr, sizeof(uint32_t)))
110                         return -1;
111         }
112         return 0;
113 }
114
115 static int ebt_filter_among(const struct sk_buff *skb,
116                             const struct net_device *in,
117                             const struct net_device *out, const void *data,
118                             unsigned int datalen)
119 {
120         struct ebt_among_info *info = (struct ebt_among_info *) data;
121         const char *dmac, *smac;
122         const struct ebt_mac_wormhash *wh_dst, *wh_src;
123         uint32_t dip = 0, sip = 0;
124
125         wh_dst = ebt_among_wh_dst(info);
126         wh_src = ebt_among_wh_src(info);
127
128         if (wh_src) {
129                 smac = skb->mac.ethernet->h_source;
130                 if (get_ip_src(skb, &sip))
131                         return EBT_NOMATCH;
132                 if (!(info->bitmask & EBT_AMONG_SRC_NEG)) {
133                         /* we match only if it contains */
134                         if (!ebt_mac_wormhash_contains(wh_src, smac, sip))
135                                 return EBT_NOMATCH;
136                 } else {
137                         /* we match only if it DOES NOT contain */
138                         if (ebt_mac_wormhash_contains(wh_src, smac, sip))
139                                 return EBT_NOMATCH;
140                 }
141         }
142
143         if (wh_dst) {
144                 dmac = skb->mac.ethernet->h_dest;
145                 if (get_ip_dst(skb, &dip))
146                         return EBT_NOMATCH;
147                 if (!(info->bitmask & EBT_AMONG_DST_NEG)) {
148                         /* we match only if it contains */
149                         if (!ebt_mac_wormhash_contains(wh_dst, dmac, dip))
150                                 return EBT_NOMATCH;
151                 } else {
152                         /* we match only if it DOES NOT contain */
153                         if (ebt_mac_wormhash_contains(wh_dst, dmac, dip))
154                                 return EBT_NOMATCH;
155                 }
156         }
157
158         return EBT_MATCH;
159 }
160
161 static int ebt_among_check(const char *tablename, unsigned int hookmask,
162                            const struct ebt_entry *e, void *data,
163                            unsigned int datalen)
164 {
165         struct ebt_among_info *info = (struct ebt_among_info *) data;
166         int expected_length = sizeof(struct ebt_among_info);
167         const struct ebt_mac_wormhash *wh_dst, *wh_src;
168         int err;
169
170         wh_dst = ebt_among_wh_dst(info);
171         wh_src = ebt_among_wh_src(info);
172         expected_length += ebt_mac_wormhash_size(wh_dst);
173         expected_length += ebt_mac_wormhash_size(wh_src);
174
175         if (datalen != EBT_ALIGN(expected_length)) {
176                 printk(KERN_WARNING
177                        "ebtables: among: wrong size: %d"
178                        "against expected %d, rounded to %Zd\n",
179                        datalen, expected_length,
180                        EBT_ALIGN(expected_length));
181                 return -EINVAL;
182         }
183         if (wh_dst && (err = ebt_mac_wormhash_check_integrity(wh_dst))) {
184                 printk(KERN_WARNING
185                        "ebtables: among: dst integrity fail: %x\n", -err);
186                 return -EINVAL;
187         }
188         if (wh_src && (err = ebt_mac_wormhash_check_integrity(wh_src))) {
189                 printk(KERN_WARNING
190                        "ebtables: among: src integrity fail: %x\n", -err);
191                 return -EINVAL;
192         }
193         return 0;
194 }
195
196 static struct ebt_match filter_among = {
197         .name           = EBT_AMONG_MATCH, 
198         .match          = ebt_filter_among, 
199         .check          = ebt_among_check,
200         .me             = THIS_MODULE,
201 };
202
203 static int __init init(void)
204 {
205         return ebt_register_match(&filter_among);
206 }
207
208 static void __exit fini(void)
209 {
210         ebt_unregister_match(&filter_among);
211 }
212
213 module_init(init);
214 module_exit(fini);
215 MODULE_LICENSE("GPL");