netfilter: ipset: fix hash size checking in kernel
[linux-flexiantxendom0-3.2.10.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 nomatch;
47         u8 cidr;
48 };
49
50 /* Member elements with timeout support */
51 struct hash_net4_telem {
52         __be32 ip;
53         u16 padding0;
54         u8 nomatch;
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                      u32 *multi)
63 {
64         return ip1->ip == ip2->ip &&
65                ip1->cidr == ip2->cidr;
66 }
67
68 static inline bool
69 hash_net4_data_isnull(const struct hash_net4_elem *elem)
70 {
71         return elem->cidr == 0;
72 }
73
74 static inline void
75 hash_net4_data_copy(struct hash_net4_elem *dst,
76                     const struct hash_net4_elem *src)
77 {
78         dst->ip = src->ip;
79         dst->cidr = src->cidr;
80         dst->nomatch = src->nomatch;
81 }
82
83 static inline void
84 hash_net4_data_flags(struct hash_net4_elem *dst, u32 flags)
85 {
86         dst->nomatch = flags & IPSET_FLAG_NOMATCH;
87 }
88
89 static inline bool
90 hash_net4_data_match(const struct hash_net4_elem *elem)
91 {
92         return !elem->nomatch;
93 }
94
95 static inline void
96 hash_net4_data_netmask(struct hash_net4_elem *elem, u8 cidr)
97 {
98         elem->ip &= ip_set_netmask(cidr);
99         elem->cidr = cidr;
100 }
101
102 /* Zero CIDR values cannot be stored */
103 static inline void
104 hash_net4_data_zero_out(struct hash_net4_elem *elem)
105 {
106         elem->cidr = 0;
107 }
108
109 static bool
110 hash_net4_data_list(struct sk_buff *skb, const struct hash_net4_elem *data)
111 {
112         u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
113
114         NLA_PUT_IPADDR4(skb, IPSET_ATTR_IP, data->ip);
115         NLA_PUT_U8(skb, IPSET_ATTR_CIDR, data->cidr);
116         if (flags)
117                 NLA_PUT_NET32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags));
118         return 0;
119
120 nla_put_failure:
121         return 1;
122 }
123
124 static bool
125 hash_net4_data_tlist(struct sk_buff *skb, const struct hash_net4_elem *data)
126 {
127         const struct hash_net4_telem *tdata =
128                 (const struct hash_net4_telem *)data;
129         u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
130
131         NLA_PUT_IPADDR4(skb, IPSET_ATTR_IP, tdata->ip);
132         NLA_PUT_U8(skb, IPSET_ATTR_CIDR, tdata->cidr);
133         NLA_PUT_NET32(skb, IPSET_ATTR_TIMEOUT,
134                       htonl(ip_set_timeout_get(tdata->timeout)));
135         if (flags)
136                 NLA_PUT_NET32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags));
137
138         return 0;
139
140 nla_put_failure:
141         return 1;
142 }
143
144 #define IP_SET_HASH_WITH_NETS
145
146 #define PF              4
147 #define HOST_MASK       32
148 #include <linux/netfilter/ipset/ip_set_ahash.h>
149
150 static inline void
151 hash_net4_data_next(struct ip_set_hash *h,
152                     const struct hash_net4_elem *d)
153 {
154         h->next.ip = ntohl(d->ip);
155 }
156
157 static int
158 hash_net4_kadt(struct ip_set *set, const struct sk_buff *skb,
159                const struct xt_action_param *par,
160                enum ipset_adt adt, const struct ip_set_adt_opt *opt)
161 {
162         const struct ip_set_hash *h = set->data;
163         ipset_adtfn adtfn = set->variant->adt[adt];
164         struct hash_net4_elem data = {
165                 .cidr = h->nets[0].cidr ? h->nets[0].cidr : HOST_MASK
166         };
167
168         if (data.cidr == 0)
169                 return -EINVAL;
170         if (adt == IPSET_TEST)
171                 data.cidr = HOST_MASK;
172
173         ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &data.ip);
174         data.ip &= ip_set_netmask(data.cidr);
175
176         return adtfn(set, &data, opt_timeout(opt, h), opt->cmdflags);
177 }
178
179 static int
180 hash_net4_uadt(struct ip_set *set, struct nlattr *tb[],
181                enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
182 {
183         const struct ip_set_hash *h = set->data;
184         ipset_adtfn adtfn = set->variant->adt[adt];
185         struct hash_net4_elem data = { .cidr = HOST_MASK };
186         u32 timeout = h->timeout;
187         u32 ip = 0, ip_to, last;
188         int ret;
189
190         if (unlikely(!tb[IPSET_ATTR_IP] ||
191                      !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
192                      !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
193                 return -IPSET_ERR_PROTOCOL;
194
195         if (tb[IPSET_ATTR_LINENO])
196                 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
197
198         ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip);
199         if (ret)
200                 return ret;
201
202         if (tb[IPSET_ATTR_CIDR]) {
203                 data.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
204                 if (!data.cidr || data.cidr > HOST_MASK)
205                         return -IPSET_ERR_INVALID_CIDR;
206         }
207
208         if (tb[IPSET_ATTR_TIMEOUT]) {
209                 if (!with_timeout(h->timeout))
210                         return -IPSET_ERR_TIMEOUT;
211                 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
212         }
213
214         if (tb[IPSET_ATTR_CADT_FLAGS] && adt == IPSET_ADD) {
215                 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
216                 if (cadt_flags & IPSET_FLAG_NOMATCH)
217                         flags |= (cadt_flags << 16);
218         }
219
220         if (adt == IPSET_TEST || !tb[IPSET_ATTR_IP_TO]) {
221                 data.ip = htonl(ip & ip_set_hostmask(data.cidr));
222                 ret = adtfn(set, &data, timeout, flags);
223                 return ip_set_eexist(ret, flags) ? 0 : ret;
224         }
225
226         ip_to = ip;
227         if (tb[IPSET_ATTR_IP_TO]) {
228                 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
229                 if (ret)
230                         return ret;
231                 if (ip_to < ip)
232                         swap(ip, ip_to);
233                 if (ip + UINT_MAX == ip_to)
234                         return -IPSET_ERR_HASH_RANGE;
235         }
236         if (retried)
237                 ip = h->next.ip;
238         while (!after(ip, ip_to)) {
239                 data.ip = htonl(ip);
240                 last = ip_set_range_to_cidr(ip, ip_to, &data.cidr);
241                 ret = adtfn(set, &data, timeout, flags);
242                 if (ret && !ip_set_eexist(ret, flags))
243                         return ret;
244                 else
245                         ret = 0;
246                 ip = last + 1;
247         }
248         return ret;
249 }
250
251 static bool
252 hash_net_same_set(const struct ip_set *a, const struct ip_set *b)
253 {
254         const struct ip_set_hash *x = a->data;
255         const struct ip_set_hash *y = b->data;
256
257         /* Resizing changes htable_bits, so we ignore it */
258         return x->maxelem == y->maxelem &&
259                x->timeout == y->timeout;
260 }
261
262 /* The type variant functions: IPv6 */
263
264 struct hash_net6_elem {
265         union nf_inet_addr ip;
266         u16 padding0;
267         u8 nomatch;
268         u8 cidr;
269 };
270
271 struct hash_net6_telem {
272         union nf_inet_addr ip;
273         u16 padding0;
274         u8 nomatch;
275         u8 cidr;
276         unsigned long timeout;
277 };
278
279 static inline bool
280 hash_net6_data_equal(const struct hash_net6_elem *ip1,
281                      const struct hash_net6_elem *ip2,
282                      u32 *multi)
283 {
284         return ipv6_addr_cmp(&ip1->ip.in6, &ip2->ip.in6) == 0 &&
285                ip1->cidr == ip2->cidr;
286 }
287
288 static inline bool
289 hash_net6_data_isnull(const struct hash_net6_elem *elem)
290 {
291         return elem->cidr == 0;
292 }
293
294 static inline void
295 hash_net6_data_copy(struct hash_net6_elem *dst,
296                     const struct hash_net6_elem *src)
297 {
298         dst->ip.in6 = src->ip.in6;
299         dst->cidr = src->cidr;
300         dst->nomatch = src->nomatch;
301 }
302
303 static inline void
304 hash_net6_data_flags(struct hash_net6_elem *dst, u32 flags)
305 {
306         dst->nomatch = flags & IPSET_FLAG_NOMATCH;
307 }
308
309 static inline bool
310 hash_net6_data_match(const struct hash_net6_elem *elem)
311 {
312         return !elem->nomatch;
313 }
314
315 static inline void
316 hash_net6_data_zero_out(struct hash_net6_elem *elem)
317 {
318         elem->cidr = 0;
319 }
320
321 static inline void
322 ip6_netmask(union nf_inet_addr *ip, u8 prefix)
323 {
324         ip->ip6[0] &= ip_set_netmask6(prefix)[0];
325         ip->ip6[1] &= ip_set_netmask6(prefix)[1];
326         ip->ip6[2] &= ip_set_netmask6(prefix)[2];
327         ip->ip6[3] &= ip_set_netmask6(prefix)[3];
328 }
329
330 static inline void
331 hash_net6_data_netmask(struct hash_net6_elem *elem, u8 cidr)
332 {
333         ip6_netmask(&elem->ip, cidr);
334         elem->cidr = cidr;
335 }
336
337 static bool
338 hash_net6_data_list(struct sk_buff *skb, const struct hash_net6_elem *data)
339 {
340         u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
341
342         NLA_PUT_IPADDR6(skb, IPSET_ATTR_IP, &data->ip);
343         NLA_PUT_U8(skb, IPSET_ATTR_CIDR, data->cidr);
344         if (flags)
345                 NLA_PUT_NET32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags));
346         return 0;
347
348 nla_put_failure:
349         return 1;
350 }
351
352 static bool
353 hash_net6_data_tlist(struct sk_buff *skb, const struct hash_net6_elem *data)
354 {
355         const struct hash_net6_telem *e =
356                 (const struct hash_net6_telem *)data;
357         u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
358
359         NLA_PUT_IPADDR6(skb, IPSET_ATTR_IP, &e->ip);
360         NLA_PUT_U8(skb, IPSET_ATTR_CIDR, e->cidr);
361         NLA_PUT_NET32(skb, IPSET_ATTR_TIMEOUT,
362                       htonl(ip_set_timeout_get(e->timeout)));
363         if (flags)
364                 NLA_PUT_NET32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags));
365         return 0;
366
367 nla_put_failure:
368         return 1;
369 }
370
371 #undef PF
372 #undef HOST_MASK
373
374 #define PF              6
375 #define HOST_MASK       128
376 #include <linux/netfilter/ipset/ip_set_ahash.h>
377
378 static inline void
379 hash_net6_data_next(struct ip_set_hash *h,
380                     const struct hash_net6_elem *d)
381 {
382 }
383
384 static int
385 hash_net6_kadt(struct ip_set *set, const struct sk_buff *skb,
386                const struct xt_action_param *par,
387                enum ipset_adt adt, const struct ip_set_adt_opt *opt)
388 {
389         const struct ip_set_hash *h = set->data;
390         ipset_adtfn adtfn = set->variant->adt[adt];
391         struct hash_net6_elem data = {
392                 .cidr = h->nets[0].cidr ? h->nets[0].cidr : HOST_MASK
393         };
394
395         if (data.cidr == 0)
396                 return -EINVAL;
397         if (adt == IPSET_TEST)
398                 data.cidr = HOST_MASK;
399
400         ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &data.ip.in6);
401         ip6_netmask(&data.ip, data.cidr);
402
403         return adtfn(set, &data, opt_timeout(opt, h), opt->cmdflags);
404 }
405
406 static int
407 hash_net6_uadt(struct ip_set *set, struct nlattr *tb[],
408                enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
409 {
410         const struct ip_set_hash *h = set->data;
411         ipset_adtfn adtfn = set->variant->adt[adt];
412         struct hash_net6_elem data = { .cidr = HOST_MASK };
413         u32 timeout = h->timeout;
414         int ret;
415
416         if (unlikely(!tb[IPSET_ATTR_IP] ||
417                      !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
418                      !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
419                 return -IPSET_ERR_PROTOCOL;
420         if (unlikely(tb[IPSET_ATTR_IP_TO]))
421                 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
422
423         if (tb[IPSET_ATTR_LINENO])
424                 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
425
426         ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &data.ip);
427         if (ret)
428                 return ret;
429
430         if (tb[IPSET_ATTR_CIDR])
431                 data.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
432
433         if (!data.cidr || data.cidr > HOST_MASK)
434                 return -IPSET_ERR_INVALID_CIDR;
435
436         ip6_netmask(&data.ip, data.cidr);
437
438         if (tb[IPSET_ATTR_TIMEOUT]) {
439                 if (!with_timeout(h->timeout))
440                         return -IPSET_ERR_TIMEOUT;
441                 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
442         }
443
444         if (tb[IPSET_ATTR_CADT_FLAGS] && adt == IPSET_ADD) {
445                 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
446                 if (cadt_flags & IPSET_FLAG_NOMATCH)
447                         flags |= (cadt_flags << 16);
448         }
449
450         ret = adtfn(set, &data, timeout, flags);
451
452         return ip_set_eexist(ret, flags) ? 0 : ret;
453 }
454
455 /* Create hash:ip type of sets */
456
457 static int
458 hash_net_create(struct ip_set *set, struct nlattr *tb[], u32 flags)
459 {
460         u32 hashsize = IPSET_DEFAULT_HASHSIZE, maxelem = IPSET_DEFAULT_MAXELEM;
461         struct ip_set_hash *h;
462         u8 hbits;
463         size_t hsize;
464
465         if (!(set->family == NFPROTO_IPV4 || set->family == NFPROTO_IPV6))
466                 return -IPSET_ERR_INVALID_FAMILY;
467
468         if (unlikely(!ip_set_optattr_netorder(tb, IPSET_ATTR_HASHSIZE) ||
469                      !ip_set_optattr_netorder(tb, IPSET_ATTR_MAXELEM) ||
470                      !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
471                 return -IPSET_ERR_PROTOCOL;
472
473         if (tb[IPSET_ATTR_HASHSIZE]) {
474                 hashsize = ip_set_get_h32(tb[IPSET_ATTR_HASHSIZE]);
475                 if (hashsize < IPSET_MIMINAL_HASHSIZE)
476                         hashsize = IPSET_MIMINAL_HASHSIZE;
477         }
478
479         if (tb[IPSET_ATTR_MAXELEM])
480                 maxelem = ip_set_get_h32(tb[IPSET_ATTR_MAXELEM]);
481
482         h = kzalloc(sizeof(*h)
483                     + sizeof(struct ip_set_hash_nets)
484                       * (set->family == NFPROTO_IPV4 ? 32 : 128), GFP_KERNEL);
485         if (!h)
486                 return -ENOMEM;
487
488         h->maxelem = maxelem;
489         get_random_bytes(&h->initval, sizeof(h->initval));
490         h->timeout = IPSET_NO_TIMEOUT;
491
492         hbits = htable_bits(hashsize);
493         hsize = htable_size(hbits);
494         if (hsize == 0) {
495                 kfree(h);
496                 return -ENOMEM;
497         }
498         h->table = ip_set_alloc(hsize);
499         if (!h->table) {
500                 kfree(h);
501                 return -ENOMEM;
502         }
503         h->table->htable_bits = hbits;
504
505         set->data = h;
506
507         if (tb[IPSET_ATTR_TIMEOUT]) {
508                 h->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
509
510                 set->variant = set->family == NFPROTO_IPV4
511                         ? &hash_net4_tvariant : &hash_net6_tvariant;
512
513                 if (set->family == NFPROTO_IPV4)
514                         hash_net4_gc_init(set);
515                 else
516                         hash_net6_gc_init(set);
517         } else {
518                 set->variant = set->family == NFPROTO_IPV4
519                         ? &hash_net4_variant : &hash_net6_variant;
520         }
521
522         pr_debug("create %s hashsize %u (%u) maxelem %u: %p(%p)\n",
523                  set->name, jhash_size(h->table->htable_bits),
524                  h->table->htable_bits, h->maxelem, set->data, h->table);
525
526         return 0;
527 }
528
529 static struct ip_set_type hash_net_type __read_mostly = {
530         .name           = "hash:net",
531         .protocol       = IPSET_PROTOCOL,
532         .features       = IPSET_TYPE_IP,
533         .dimension      = IPSET_DIM_ONE,
534         .family         = NFPROTO_UNSPEC,
535         .revision_min   = 0,
536         /*              = 1        Range as input support for IPv4 added */
537         .revision_max   = 2,    /* nomatch flag support added */
538         .create         = hash_net_create,
539         .create_policy  = {
540                 [IPSET_ATTR_HASHSIZE]   = { .type = NLA_U32 },
541                 [IPSET_ATTR_MAXELEM]    = { .type = NLA_U32 },
542                 [IPSET_ATTR_PROBES]     = { .type = NLA_U8 },
543                 [IPSET_ATTR_RESIZE]     = { .type = NLA_U8  },
544                 [IPSET_ATTR_TIMEOUT]    = { .type = NLA_U32 },
545         },
546         .adt_policy     = {
547                 [IPSET_ATTR_IP]         = { .type = NLA_NESTED },
548                 [IPSET_ATTR_IP_TO]      = { .type = NLA_NESTED },
549                 [IPSET_ATTR_CIDR]       = { .type = NLA_U8 },
550                 [IPSET_ATTR_TIMEOUT]    = { .type = NLA_U32 },
551                 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
552         },
553         .me             = THIS_MODULE,
554 };
555
556 static int __init
557 hash_net_init(void)
558 {
559         return ip_set_type_register(&hash_net_type);
560 }
561
562 static void __exit
563 hash_net_fini(void)
564 {
565         ip_set_type_unregister(&hash_net_type);
566 }
567
568 module_init(hash_net_init);
569 module_exit(hash_net_fini);