Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/ide-2.6
[pandora-kernel.git] / net / netfilter / ipset / ip_set_hash_net.c
1 /* Copyright (C) 2003-2011 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
2  *
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License version 2 as
5  * published by the Free Software Foundation.
6  */
7
8 /* Kernel module implementing an IP set type: the hash:net type */
9
10 #include <linux/jhash.h>
11 #include <linux/module.h>
12 #include <linux/ip.h>
13 #include <linux/skbuff.h>
14 #include <linux/errno.h>
15 #include <linux/random.h>
16 #include <net/ip.h>
17 #include <net/ipv6.h>
18 #include <net/netlink.h>
19
20 #include <linux/netfilter.h>
21 #include <linux/netfilter/ipset/pfxlen.h>
22 #include <linux/netfilter/ipset/ip_set.h>
23 #include <linux/netfilter/ipset/ip_set_timeout.h>
24 #include <linux/netfilter/ipset/ip_set_hash.h>
25
26 MODULE_LICENSE("GPL");
27 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
28 MODULE_DESCRIPTION("hash:net type of IP sets");
29 MODULE_ALIAS("ip_set_hash:net");
30
31 /* Type specific function prefix */
32 #define TYPE            hash_net
33
34 static bool
35 hash_net_same_set(const struct ip_set *a, const struct ip_set *b);
36
37 #define hash_net4_same_set      hash_net_same_set
38 #define hash_net6_same_set      hash_net_same_set
39
40 /* The type variant functions: IPv4 */
41
42 /* Member elements without timeout */
43 struct hash_net4_elem {
44         __be32 ip;
45         u16 padding0;
46         u8 padding1;
47         u8 cidr;
48 };
49
50 /* Member elements with timeout support */
51 struct hash_net4_telem {
52         __be32 ip;
53         u16 padding0;
54         u8 padding1;
55         u8 cidr;
56         unsigned long timeout;
57 };
58
59 static inline bool
60 hash_net4_data_equal(const struct hash_net4_elem *ip1,
61                     const struct hash_net4_elem *ip2)
62 {
63         return ip1->ip == ip2->ip && ip1->cidr == ip2->cidr;
64 }
65
66 static inline bool
67 hash_net4_data_isnull(const struct hash_net4_elem *elem)
68 {
69         return elem->cidr == 0;
70 }
71
72 static inline void
73 hash_net4_data_copy(struct hash_net4_elem *dst,
74                     const struct hash_net4_elem *src)
75 {
76         dst->ip = src->ip;
77         dst->cidr = src->cidr;
78 }
79
80 static inline void
81 hash_net4_data_netmask(struct hash_net4_elem *elem, u8 cidr)
82 {
83         elem->ip &= ip_set_netmask(cidr);
84         elem->cidr = cidr;
85 }
86
87 /* Zero CIDR values cannot be stored */
88 static inline void
89 hash_net4_data_zero_out(struct hash_net4_elem *elem)
90 {
91         elem->cidr = 0;
92 }
93
94 static bool
95 hash_net4_data_list(struct sk_buff *skb, const struct hash_net4_elem *data)
96 {
97         NLA_PUT_IPADDR4(skb, IPSET_ATTR_IP, data->ip);
98         NLA_PUT_U8(skb, IPSET_ATTR_CIDR, data->cidr);
99         return 0;
100
101 nla_put_failure:
102         return 1;
103 }
104
105 static bool
106 hash_net4_data_tlist(struct sk_buff *skb, const struct hash_net4_elem *data)
107 {
108         const struct hash_net4_telem *tdata =
109                 (const struct hash_net4_telem *)data;
110
111         NLA_PUT_IPADDR4(skb, IPSET_ATTR_IP, tdata->ip);
112         NLA_PUT_U8(skb, IPSET_ATTR_CIDR, tdata->cidr);
113         NLA_PUT_NET32(skb, IPSET_ATTR_TIMEOUT,
114                       htonl(ip_set_timeout_get(tdata->timeout)));
115
116         return 0;
117
118 nla_put_failure:
119         return 1;
120 }
121
122 #define IP_SET_HASH_WITH_NETS
123
124 #define PF              4
125 #define HOST_MASK       32
126 #include <linux/netfilter/ipset/ip_set_ahash.h>
127
128 static int
129 hash_net4_kadt(struct ip_set *set, const struct sk_buff *skb,
130                enum ipset_adt adt, u8 pf, u8 dim, u8 flags)
131 {
132         const struct ip_set_hash *h = set->data;
133         ipset_adtfn adtfn = set->variant->adt[adt];
134         struct hash_net4_elem data = {
135                 .cidr = h->nets[0].cidr ? h->nets[0].cidr : HOST_MASK
136         };
137
138         if (data.cidr == 0)
139                 return -EINVAL;
140         if (adt == IPSET_TEST)
141                 data.cidr = HOST_MASK;
142
143         ip4addrptr(skb, flags & IPSET_DIM_ONE_SRC, &data.ip);
144         data.ip &= ip_set_netmask(data.cidr);
145
146         return adtfn(set, &data, h->timeout);
147 }
148
149 static int
150 hash_net4_uadt(struct ip_set *set, struct nlattr *tb[],
151                enum ipset_adt adt, u32 *lineno, u32 flags)
152 {
153         const struct ip_set_hash *h = set->data;
154         ipset_adtfn adtfn = set->variant->adt[adt];
155         struct hash_net4_elem data = { .cidr = HOST_MASK };
156         u32 timeout = h->timeout;
157         int ret;
158
159         if (unlikely(!tb[IPSET_ATTR_IP] ||
160                      !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
161                 return -IPSET_ERR_PROTOCOL;
162
163         if (tb[IPSET_ATTR_LINENO])
164                 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
165
166         ret = ip_set_get_ipaddr4(tb[IPSET_ATTR_IP], &data.ip);
167         if (ret)
168                 return ret;
169
170         if (tb[IPSET_ATTR_CIDR])
171                 data.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
172
173         if (!data.cidr)
174                 return -IPSET_ERR_INVALID_CIDR;
175
176         data.ip &= ip_set_netmask(data.cidr);
177
178         if (tb[IPSET_ATTR_TIMEOUT]) {
179                 if (!with_timeout(h->timeout))
180                         return -IPSET_ERR_TIMEOUT;
181                 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
182         }
183
184         ret = adtfn(set, &data, timeout);
185
186         return ip_set_eexist(ret, flags) ? 0 : ret;
187 }
188
189 static bool
190 hash_net_same_set(const struct ip_set *a, const struct ip_set *b)
191 {
192         const struct ip_set_hash *x = a->data;
193         const struct ip_set_hash *y = b->data;
194
195         /* Resizing changes htable_bits, so we ignore it */
196         return x->maxelem == y->maxelem &&
197                x->timeout == y->timeout;
198 }
199
200 /* The type variant functions: IPv6 */
201
202 struct hash_net6_elem {
203         union nf_inet_addr ip;
204         u16 padding0;
205         u8 padding1;
206         u8 cidr;
207 };
208
209 struct hash_net6_telem {
210         union nf_inet_addr ip;
211         u16 padding0;
212         u8 padding1;
213         u8 cidr;
214         unsigned long timeout;
215 };
216
217 static inline bool
218 hash_net6_data_equal(const struct hash_net6_elem *ip1,
219                      const struct hash_net6_elem *ip2)
220 {
221         return ipv6_addr_cmp(&ip1->ip.in6, &ip2->ip.in6) == 0 &&
222                ip1->cidr == ip2->cidr;
223 }
224
225 static inline bool
226 hash_net6_data_isnull(const struct hash_net6_elem *elem)
227 {
228         return elem->cidr == 0;
229 }
230
231 static inline void
232 hash_net6_data_copy(struct hash_net6_elem *dst,
233                     const struct hash_net6_elem *src)
234 {
235         ipv6_addr_copy(&dst->ip.in6, &src->ip.in6);
236         dst->cidr = src->cidr;
237 }
238
239 static inline void
240 hash_net6_data_zero_out(struct hash_net6_elem *elem)
241 {
242         elem->cidr = 0;
243 }
244
245 static inline void
246 ip6_netmask(union nf_inet_addr *ip, u8 prefix)
247 {
248         ip->ip6[0] &= ip_set_netmask6(prefix)[0];
249         ip->ip6[1] &= ip_set_netmask6(prefix)[1];
250         ip->ip6[2] &= ip_set_netmask6(prefix)[2];
251         ip->ip6[3] &= ip_set_netmask6(prefix)[3];
252 }
253
254 static inline void
255 hash_net6_data_netmask(struct hash_net6_elem *elem, u8 cidr)
256 {
257         ip6_netmask(&elem->ip, cidr);
258         elem->cidr = cidr;
259 }
260
261 static bool
262 hash_net6_data_list(struct sk_buff *skb, const struct hash_net6_elem *data)
263 {
264         NLA_PUT_IPADDR6(skb, IPSET_ATTR_IP, &data->ip);
265         NLA_PUT_U8(skb, IPSET_ATTR_CIDR, data->cidr);
266         return 0;
267
268 nla_put_failure:
269         return 1;
270 }
271
272 static bool
273 hash_net6_data_tlist(struct sk_buff *skb, const struct hash_net6_elem *data)
274 {
275         const struct hash_net6_telem *e =
276                 (const struct hash_net6_telem *)data;
277
278         NLA_PUT_IPADDR6(skb, IPSET_ATTR_IP, &e->ip);
279         NLA_PUT_U8(skb, IPSET_ATTR_CIDR, e->cidr);
280         NLA_PUT_NET32(skb, IPSET_ATTR_TIMEOUT,
281                       htonl(ip_set_timeout_get(e->timeout)));
282         return 0;
283
284 nla_put_failure:
285         return 1;
286 }
287
288 #undef PF
289 #undef HOST_MASK
290
291 #define PF              6
292 #define HOST_MASK       128
293 #include <linux/netfilter/ipset/ip_set_ahash.h>
294
295 static int
296 hash_net6_kadt(struct ip_set *set, const struct sk_buff *skb,
297                enum ipset_adt adt, u8 pf, u8 dim, u8 flags)
298 {
299         const struct ip_set_hash *h = set->data;
300         ipset_adtfn adtfn = set->variant->adt[adt];
301         struct hash_net6_elem data = {
302                 .cidr = h->nets[0].cidr ? h->nets[0].cidr : HOST_MASK
303         };
304
305         if (data.cidr == 0)
306                 return -EINVAL;
307         if (adt == IPSET_TEST)
308                 data.cidr = HOST_MASK;
309
310         ip6addrptr(skb, flags & IPSET_DIM_ONE_SRC, &data.ip.in6);
311         ip6_netmask(&data.ip, data.cidr);
312
313         return adtfn(set, &data, h->timeout);
314 }
315
316 static int
317 hash_net6_uadt(struct ip_set *set, struct nlattr *tb[],
318                enum ipset_adt adt, u32 *lineno, u32 flags)
319 {
320         const struct ip_set_hash *h = set->data;
321         ipset_adtfn adtfn = set->variant->adt[adt];
322         struct hash_net6_elem data = { .cidr = HOST_MASK };
323         u32 timeout = h->timeout;
324         int ret;
325
326         if (unlikely(!tb[IPSET_ATTR_IP] ||
327                      !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
328                 return -IPSET_ERR_PROTOCOL;
329
330         if (tb[IPSET_ATTR_LINENO])
331                 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
332
333         ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &data.ip);
334         if (ret)
335                 return ret;
336
337         if (tb[IPSET_ATTR_CIDR])
338                 data.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
339
340         if (!data.cidr)
341                 return -IPSET_ERR_INVALID_CIDR;
342
343         ip6_netmask(&data.ip, data.cidr);
344
345         if (tb[IPSET_ATTR_TIMEOUT]) {
346                 if (!with_timeout(h->timeout))
347                         return -IPSET_ERR_TIMEOUT;
348                 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
349         }
350
351         ret = adtfn(set, &data, timeout);
352
353         return ip_set_eexist(ret, flags) ? 0 : ret;
354 }
355
356 /* Create hash:ip type of sets */
357
358 static int
359 hash_net_create(struct ip_set *set, struct nlattr *tb[], u32 flags)
360 {
361         u32 hashsize = IPSET_DEFAULT_HASHSIZE, maxelem = IPSET_DEFAULT_MAXELEM;
362         struct ip_set_hash *h;
363         u8 hbits;
364
365         if (!(set->family == AF_INET || set->family == AF_INET6))
366                 return -IPSET_ERR_INVALID_FAMILY;
367
368         if (unlikely(!ip_set_optattr_netorder(tb, IPSET_ATTR_HASHSIZE) ||
369                      !ip_set_optattr_netorder(tb, IPSET_ATTR_MAXELEM) ||
370                      !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
371                 return -IPSET_ERR_PROTOCOL;
372
373         if (tb[IPSET_ATTR_HASHSIZE]) {
374                 hashsize = ip_set_get_h32(tb[IPSET_ATTR_HASHSIZE]);
375                 if (hashsize < IPSET_MIMINAL_HASHSIZE)
376                         hashsize = IPSET_MIMINAL_HASHSIZE;
377         }
378
379         if (tb[IPSET_ATTR_MAXELEM])
380                 maxelem = ip_set_get_h32(tb[IPSET_ATTR_MAXELEM]);
381
382         h = kzalloc(sizeof(*h)
383                     + sizeof(struct ip_set_hash_nets)
384                       * (set->family == AF_INET ? 32 : 128), GFP_KERNEL);
385         if (!h)
386                 return -ENOMEM;
387
388         h->maxelem = maxelem;
389         get_random_bytes(&h->initval, sizeof(h->initval));
390         h->timeout = IPSET_NO_TIMEOUT;
391
392         hbits = htable_bits(hashsize);
393         h->table = ip_set_alloc(
394                         sizeof(struct htable)
395                         + jhash_size(hbits) * sizeof(struct hbucket));
396         if (!h->table) {
397                 kfree(h);
398                 return -ENOMEM;
399         }
400         h->table->htable_bits = hbits;
401
402         set->data = h;
403
404         if (tb[IPSET_ATTR_TIMEOUT]) {
405                 h->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
406
407                 set->variant = set->family == AF_INET
408                         ? &hash_net4_tvariant : &hash_net6_tvariant;
409
410                 if (set->family == AF_INET)
411                         hash_net4_gc_init(set);
412                 else
413                         hash_net6_gc_init(set);
414         } else {
415                 set->variant = set->family == AF_INET
416                         ? &hash_net4_variant : &hash_net6_variant;
417         }
418
419         pr_debug("create %s hashsize %u (%u) maxelem %u: %p(%p)\n",
420                  set->name, jhash_size(h->table->htable_bits),
421                  h->table->htable_bits, h->maxelem, set->data, h->table);
422
423         return 0;
424 }
425
426 static struct ip_set_type hash_net_type __read_mostly = {
427         .name           = "hash:net",
428         .protocol       = IPSET_PROTOCOL,
429         .features       = IPSET_TYPE_IP,
430         .dimension      = IPSET_DIM_ONE,
431         .family         = AF_UNSPEC,
432         .revision       = 0,
433         .create         = hash_net_create,
434         .create_policy  = {
435                 [IPSET_ATTR_HASHSIZE]   = { .type = NLA_U32 },
436                 [IPSET_ATTR_MAXELEM]    = { .type = NLA_U32 },
437                 [IPSET_ATTR_PROBES]     = { .type = NLA_U8 },
438                 [IPSET_ATTR_RESIZE]     = { .type = NLA_U8  },
439                 [IPSET_ATTR_TIMEOUT]    = { .type = NLA_U32 },
440         },
441         .adt_policy     = {
442                 [IPSET_ATTR_IP]         = { .type = NLA_NESTED },
443                 [IPSET_ATTR_CIDR]       = { .type = NLA_U8 },
444                 [IPSET_ATTR_TIMEOUT]    = { .type = NLA_U32 },
445         },
446         .me             = THIS_MODULE,
447 };
448
449 static int __init
450 hash_net_init(void)
451 {
452         return ip_set_type_register(&hash_net_type);
453 }
454
455 static void __exit
456 hash_net_fini(void)
457 {
458         ip_set_type_unregister(&hash_net_type);
459 }
460
461 module_init(hash_net_init);
462 module_exit(hash_net_fini);