ide-cd: signedness warning fix again
[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 = { .cidr = h->nets[0].cidr || HOST_MASK };
135
136         if (data.cidr == 0)
137                 return -EINVAL;
138         if (adt == IPSET_TEST)
139                 data.cidr = HOST_MASK;
140
141         ip4addrptr(skb, flags & IPSET_DIM_ONE_SRC, &data.ip);
142         data.ip &= ip_set_netmask(data.cidr);
143
144         return adtfn(set, &data, h->timeout);
145 }
146
147 static int
148 hash_net4_uadt(struct ip_set *set, struct nlattr *tb[],
149                enum ipset_adt adt, u32 *lineno, u32 flags)
150 {
151         const struct ip_set_hash *h = set->data;
152         ipset_adtfn adtfn = set->variant->adt[adt];
153         struct hash_net4_elem data = { .cidr = HOST_MASK };
154         u32 timeout = h->timeout;
155         int ret;
156
157         if (unlikely(!tb[IPSET_ATTR_IP] ||
158                      !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
159                 return -IPSET_ERR_PROTOCOL;
160
161         if (tb[IPSET_ATTR_LINENO])
162                 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
163
164         ret = ip_set_get_ipaddr4(tb[IPSET_ATTR_IP], &data.ip);
165         if (ret)
166                 return ret;
167
168         if (tb[IPSET_ATTR_CIDR])
169                 data.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
170
171         if (!data.cidr)
172                 return -IPSET_ERR_INVALID_CIDR;
173
174         data.ip &= ip_set_netmask(data.cidr);
175
176         if (tb[IPSET_ATTR_TIMEOUT]) {
177                 if (!with_timeout(h->timeout))
178                         return -IPSET_ERR_TIMEOUT;
179                 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
180         }
181
182         ret = adtfn(set, &data, timeout);
183
184         return ip_set_eexist(ret, flags) ? 0 : ret;
185 }
186
187 static bool
188 hash_net_same_set(const struct ip_set *a, const struct ip_set *b)
189 {
190         const struct ip_set_hash *x = a->data;
191         const struct ip_set_hash *y = b->data;
192
193         /* Resizing changes htable_bits, so we ignore it */
194         return x->maxelem == y->maxelem &&
195                x->timeout == y->timeout;
196 }
197
198 /* The type variant functions: IPv6 */
199
200 struct hash_net6_elem {
201         union nf_inet_addr ip;
202         u16 padding0;
203         u8 padding1;
204         u8 cidr;
205 };
206
207 struct hash_net6_telem {
208         union nf_inet_addr ip;
209         u16 padding0;
210         u8 padding1;
211         u8 cidr;
212         unsigned long timeout;
213 };
214
215 static inline bool
216 hash_net6_data_equal(const struct hash_net6_elem *ip1,
217                      const struct hash_net6_elem *ip2)
218 {
219         return ipv6_addr_cmp(&ip1->ip.in6, &ip2->ip.in6) == 0 &&
220                ip1->cidr == ip2->cidr;
221 }
222
223 static inline bool
224 hash_net6_data_isnull(const struct hash_net6_elem *elem)
225 {
226         return elem->cidr == 0;
227 }
228
229 static inline void
230 hash_net6_data_copy(struct hash_net6_elem *dst,
231                     const struct hash_net6_elem *src)
232 {
233         ipv6_addr_copy(&dst->ip.in6, &src->ip.in6);
234         dst->cidr = src->cidr;
235 }
236
237 static inline void
238 hash_net6_data_zero_out(struct hash_net6_elem *elem)
239 {
240         elem->cidr = 0;
241 }
242
243 static inline void
244 ip6_netmask(union nf_inet_addr *ip, u8 prefix)
245 {
246         ip->ip6[0] &= ip_set_netmask6(prefix)[0];
247         ip->ip6[1] &= ip_set_netmask6(prefix)[1];
248         ip->ip6[2] &= ip_set_netmask6(prefix)[2];
249         ip->ip6[3] &= ip_set_netmask6(prefix)[3];
250 }
251
252 static inline void
253 hash_net6_data_netmask(struct hash_net6_elem *elem, u8 cidr)
254 {
255         ip6_netmask(&elem->ip, cidr);
256         elem->cidr = cidr;
257 }
258
259 static bool
260 hash_net6_data_list(struct sk_buff *skb, const struct hash_net6_elem *data)
261 {
262         NLA_PUT_IPADDR6(skb, IPSET_ATTR_IP, &data->ip);
263         NLA_PUT_U8(skb, IPSET_ATTR_CIDR, data->cidr);
264         return 0;
265
266 nla_put_failure:
267         return 1;
268 }
269
270 static bool
271 hash_net6_data_tlist(struct sk_buff *skb, const struct hash_net6_elem *data)
272 {
273         const struct hash_net6_telem *e =
274                 (const struct hash_net6_telem *)data;
275
276         NLA_PUT_IPADDR6(skb, IPSET_ATTR_IP, &e->ip);
277         NLA_PUT_U8(skb, IPSET_ATTR_CIDR, e->cidr);
278         NLA_PUT_NET32(skb, IPSET_ATTR_TIMEOUT,
279                       htonl(ip_set_timeout_get(e->timeout)));
280         return 0;
281
282 nla_put_failure:
283         return 1;
284 }
285
286 #undef PF
287 #undef HOST_MASK
288
289 #define PF              6
290 #define HOST_MASK       128
291 #include <linux/netfilter/ipset/ip_set_ahash.h>
292
293 static int
294 hash_net6_kadt(struct ip_set *set, const struct sk_buff *skb,
295                enum ipset_adt adt, u8 pf, u8 dim, u8 flags)
296 {
297         const struct ip_set_hash *h = set->data;
298         ipset_adtfn adtfn = set->variant->adt[adt];
299         struct hash_net6_elem data = { .cidr = h->nets[0].cidr || HOST_MASK };
300
301         if (data.cidr == 0)
302                 return -EINVAL;
303         if (adt == IPSET_TEST)
304                 data.cidr = HOST_MASK;
305
306         ip6addrptr(skb, flags & IPSET_DIM_ONE_SRC, &data.ip.in6);
307         ip6_netmask(&data.ip, data.cidr);
308
309         return adtfn(set, &data, h->timeout);
310 }
311
312 static int
313 hash_net6_uadt(struct ip_set *set, struct nlattr *tb[],
314                enum ipset_adt adt, u32 *lineno, u32 flags)
315 {
316         const struct ip_set_hash *h = set->data;
317         ipset_adtfn adtfn = set->variant->adt[adt];
318         struct hash_net6_elem data = { .cidr = HOST_MASK };
319         u32 timeout = h->timeout;
320         int ret;
321
322         if (unlikely(!tb[IPSET_ATTR_IP] ||
323                      !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
324                 return -IPSET_ERR_PROTOCOL;
325
326         if (tb[IPSET_ATTR_LINENO])
327                 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
328
329         ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &data.ip);
330         if (ret)
331                 return ret;
332
333         if (tb[IPSET_ATTR_CIDR])
334                 data.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
335
336         if (!data.cidr)
337                 return -IPSET_ERR_INVALID_CIDR;
338
339         ip6_netmask(&data.ip, data.cidr);
340
341         if (tb[IPSET_ATTR_TIMEOUT]) {
342                 if (!with_timeout(h->timeout))
343                         return -IPSET_ERR_TIMEOUT;
344                 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
345         }
346
347         ret = adtfn(set, &data, timeout);
348
349         return ip_set_eexist(ret, flags) ? 0 : ret;
350 }
351
352 /* Create hash:ip type of sets */
353
354 static int
355 hash_net_create(struct ip_set *set, struct nlattr *tb[], u32 flags)
356 {
357         u32 hashsize = IPSET_DEFAULT_HASHSIZE, maxelem = IPSET_DEFAULT_MAXELEM;
358         struct ip_set_hash *h;
359         u8 hbits;
360
361         if (!(set->family == AF_INET || set->family == AF_INET6))
362                 return -IPSET_ERR_INVALID_FAMILY;
363
364         if (unlikely(!ip_set_optattr_netorder(tb, IPSET_ATTR_HASHSIZE) ||
365                      !ip_set_optattr_netorder(tb, IPSET_ATTR_MAXELEM) ||
366                      !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
367                 return -IPSET_ERR_PROTOCOL;
368
369         if (tb[IPSET_ATTR_HASHSIZE]) {
370                 hashsize = ip_set_get_h32(tb[IPSET_ATTR_HASHSIZE]);
371                 if (hashsize < IPSET_MIMINAL_HASHSIZE)
372                         hashsize = IPSET_MIMINAL_HASHSIZE;
373         }
374
375         if (tb[IPSET_ATTR_MAXELEM])
376                 maxelem = ip_set_get_h32(tb[IPSET_ATTR_MAXELEM]);
377
378         h = kzalloc(sizeof(*h)
379                     + sizeof(struct ip_set_hash_nets)
380                       * (set->family == AF_INET ? 32 : 128), GFP_KERNEL);
381         if (!h)
382                 return -ENOMEM;
383
384         h->maxelem = maxelem;
385         get_random_bytes(&h->initval, sizeof(h->initval));
386         h->timeout = IPSET_NO_TIMEOUT;
387
388         hbits = htable_bits(hashsize);
389         h->table = ip_set_alloc(
390                         sizeof(struct htable)
391                         + jhash_size(hbits) * sizeof(struct hbucket));
392         if (!h->table) {
393                 kfree(h);
394                 return -ENOMEM;
395         }
396         h->table->htable_bits = hbits;
397
398         set->data = h;
399
400         if (tb[IPSET_ATTR_TIMEOUT]) {
401                 h->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
402
403                 set->variant = set->family == AF_INET
404                         ? &hash_net4_tvariant : &hash_net6_tvariant;
405
406                 if (set->family == AF_INET)
407                         hash_net4_gc_init(set);
408                 else
409                         hash_net6_gc_init(set);
410         } else {
411                 set->variant = set->family == AF_INET
412                         ? &hash_net4_variant : &hash_net6_variant;
413         }
414
415         pr_debug("create %s hashsize %u (%u) maxelem %u: %p(%p)\n",
416                  set->name, jhash_size(h->table->htable_bits),
417                  h->table->htable_bits, h->maxelem, set->data, h->table);
418
419         return 0;
420 }
421
422 static struct ip_set_type hash_net_type __read_mostly = {
423         .name           = "hash:net",
424         .protocol       = IPSET_PROTOCOL,
425         .features       = IPSET_TYPE_IP,
426         .dimension      = IPSET_DIM_ONE,
427         .family         = AF_UNSPEC,
428         .revision       = 0,
429         .create         = hash_net_create,
430         .create_policy  = {
431                 [IPSET_ATTR_HASHSIZE]   = { .type = NLA_U32 },
432                 [IPSET_ATTR_MAXELEM]    = { .type = NLA_U32 },
433                 [IPSET_ATTR_PROBES]     = { .type = NLA_U8 },
434                 [IPSET_ATTR_RESIZE]     = { .type = NLA_U8  },
435                 [IPSET_ATTR_TIMEOUT]    = { .type = NLA_U32 },
436         },
437         .adt_policy     = {
438                 [IPSET_ATTR_IP]         = { .type = NLA_NESTED },
439                 [IPSET_ATTR_CIDR]       = { .type = NLA_U8 },
440                 [IPSET_ATTR_TIMEOUT]    = { .type = NLA_U32 },
441         },
442         .me             = THIS_MODULE,
443 };
444
445 static int __init
446 hash_net_init(void)
447 {
448         return ip_set_type_register(&hash_net_type);
449 }
450
451 static void __exit
452 hash_net_fini(void)
453 {
454         ip_set_type_unregister(&hash_net_type);
455 }
456
457 module_init(hash_net_init);
458 module_exit(hash_net_fini);