netfilter: ipset: fix adding ranges to hash types
[pandora-kernel.git] / net / netfilter / ipset / ip_set_hash_netport.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,port 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_getport.h>
25 #include <linux/netfilter/ipset/ip_set_hash.h>
26
27 MODULE_LICENSE("GPL");
28 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
29 MODULE_DESCRIPTION("hash:net,port type of IP sets");
30 MODULE_ALIAS("ip_set_hash:net,port");
31
32 /* Type specific function prefix */
33 #define TYPE            hash_netport
34
35 static bool
36 hash_netport_same_set(const struct ip_set *a, const struct ip_set *b);
37
38 #define hash_netport4_same_set  hash_netport_same_set
39 #define hash_netport6_same_set  hash_netport_same_set
40
41 /* The type variant functions: IPv4 */
42
43 /* Member elements without timeout */
44 struct hash_netport4_elem {
45         __be32 ip;
46         __be16 port;
47         u8 proto;
48         u8 cidr;
49 };
50
51 /* Member elements with timeout support */
52 struct hash_netport4_telem {
53         __be32 ip;
54         __be16 port;
55         u8 proto;
56         u8 cidr;
57         unsigned long timeout;
58 };
59
60 static inline bool
61 hash_netport4_data_equal(const struct hash_netport4_elem *ip1,
62                          const struct hash_netport4_elem *ip2)
63 {
64         return ip1->ip == ip2->ip &&
65                ip1->port == ip2->port &&
66                ip1->proto == ip2->proto &&
67                ip1->cidr == ip2->cidr;
68 }
69
70 static inline bool
71 hash_netport4_data_isnull(const struct hash_netport4_elem *elem)
72 {
73         return elem->proto == 0;
74 }
75
76 static inline void
77 hash_netport4_data_copy(struct hash_netport4_elem *dst,
78                         const struct hash_netport4_elem *src)
79 {
80         dst->ip = src->ip;
81         dst->port = src->port;
82         dst->proto = src->proto;
83         dst->cidr = src->cidr;
84 }
85
86 static inline void
87 hash_netport4_data_netmask(struct hash_netport4_elem *elem, u8 cidr)
88 {
89         elem->ip &= ip_set_netmask(cidr);
90         elem->cidr = cidr;
91 }
92
93 static inline void
94 hash_netport4_data_zero_out(struct hash_netport4_elem *elem)
95 {
96         elem->proto = 0;
97 }
98
99 static bool
100 hash_netport4_data_list(struct sk_buff *skb,
101                         const struct hash_netport4_elem *data)
102 {
103         NLA_PUT_IPADDR4(skb, IPSET_ATTR_IP, data->ip);
104         NLA_PUT_NET16(skb, IPSET_ATTR_PORT, data->port);
105         NLA_PUT_U8(skb, IPSET_ATTR_CIDR, data->cidr);
106         NLA_PUT_U8(skb, IPSET_ATTR_PROTO, data->proto);
107         return 0;
108
109 nla_put_failure:
110         return 1;
111 }
112
113 static bool
114 hash_netport4_data_tlist(struct sk_buff *skb,
115                          const struct hash_netport4_elem *data)
116 {
117         const struct hash_netport4_telem *tdata =
118                 (const struct hash_netport4_telem *)data;
119
120         NLA_PUT_IPADDR4(skb, IPSET_ATTR_IP, tdata->ip);
121         NLA_PUT_NET16(skb, IPSET_ATTR_PORT, tdata->port);
122         NLA_PUT_U8(skb, IPSET_ATTR_CIDR, data->cidr);
123         NLA_PUT_U8(skb, IPSET_ATTR_PROTO, data->proto);
124         NLA_PUT_NET32(skb, IPSET_ATTR_TIMEOUT,
125                       htonl(ip_set_timeout_get(tdata->timeout)));
126
127         return 0;
128
129 nla_put_failure:
130         return 1;
131 }
132
133 #define IP_SET_HASH_WITH_PROTO
134 #define IP_SET_HASH_WITH_NETS
135
136 #define PF              4
137 #define HOST_MASK       32
138 #include <linux/netfilter/ipset/ip_set_ahash.h>
139
140 static inline void
141 hash_netport4_data_next(struct ip_set_hash *h,
142                         const struct hash_netport4_elem *d)
143 {
144         h->next.port = ntohs(d->port);
145 }
146
147 static int
148 hash_netport4_kadt(struct ip_set *set, const struct sk_buff *skb,
149                    enum ipset_adt adt, const struct ip_set_adt_opt *opt)
150 {
151         const struct ip_set_hash *h = set->data;
152         ipset_adtfn adtfn = set->variant->adt[adt];
153         struct hash_netport4_elem data = {
154                 .cidr = h->nets[0].cidr || HOST_MASK };
155
156         if (data.cidr == 0)
157                 return -EINVAL;
158         if (adt == IPSET_TEST)
159                 data.cidr = HOST_MASK;
160
161         if (!ip_set_get_ip4_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
162                                  &data.port, &data.proto))
163                 return -EINVAL;
164
165         ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &data.ip);
166         data.ip &= ip_set_netmask(data.cidr);
167
168         return adtfn(set, &data, opt_timeout(opt, h), opt->cmdflags);
169 }
170
171 static int
172 hash_netport4_uadt(struct ip_set *set, struct nlattr *tb[],
173                    enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
174 {
175         const struct ip_set_hash *h = set->data;
176         ipset_adtfn adtfn = set->variant->adt[adt];
177         struct hash_netport4_elem data = { .cidr = HOST_MASK };
178         u32 port, port_to;
179         u32 timeout = h->timeout;
180         bool with_ports = false;
181         int ret;
182
183         if (unlikely(!tb[IPSET_ATTR_IP] ||
184                      !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
185                      !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
186                      !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
187                 return -IPSET_ERR_PROTOCOL;
188
189         if (tb[IPSET_ATTR_LINENO])
190                 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
191
192         ret = ip_set_get_ipaddr4(tb[IPSET_ATTR_IP], &data.ip);
193         if (ret)
194                 return ret;
195
196         if (tb[IPSET_ATTR_CIDR])
197                 data.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
198         if (!data.cidr)
199                 return -IPSET_ERR_INVALID_CIDR;
200         data.ip &= ip_set_netmask(data.cidr);
201
202         if (tb[IPSET_ATTR_PORT])
203                 data.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
204         else
205                 return -IPSET_ERR_PROTOCOL;
206
207         if (tb[IPSET_ATTR_PROTO]) {
208                 data.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
209                 with_ports = ip_set_proto_with_ports(data.proto);
210
211                 if (data.proto == 0)
212                         return -IPSET_ERR_INVALID_PROTO;
213         } else
214                 return -IPSET_ERR_MISSING_PROTO;
215
216         if (!(with_ports || data.proto == IPPROTO_ICMP))
217                 data.port = 0;
218
219         if (tb[IPSET_ATTR_TIMEOUT]) {
220                 if (!with_timeout(h->timeout))
221                         return -IPSET_ERR_TIMEOUT;
222                 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
223         }
224
225         if (adt == IPSET_TEST || !with_ports || !tb[IPSET_ATTR_PORT_TO]) {
226                 ret = adtfn(set, &data, timeout, flags);
227                 return ip_set_eexist(ret, flags) ? 0 : ret;
228         }
229
230         port = ntohs(data.port);
231         port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
232         if (port > port_to)
233                 swap(port, port_to);
234
235         if (retried)
236                 port = h->next.port;
237         for (; port <= port_to; port++) {
238                 data.port = htons(port);
239                 ret = adtfn(set, &data, timeout, flags);
240
241                 if (ret && !ip_set_eexist(ret, flags))
242                         return ret;
243                 else
244                         ret = 0;
245         }
246         return ret;
247 }
248
249 static bool
250 hash_netport_same_set(const struct ip_set *a, const struct ip_set *b)
251 {
252         const struct ip_set_hash *x = a->data;
253         const struct ip_set_hash *y = b->data;
254
255         /* Resizing changes htable_bits, so we ignore it */
256         return x->maxelem == y->maxelem &&
257                x->timeout == y->timeout;
258 }
259
260 /* The type variant functions: IPv6 */
261
262 struct hash_netport6_elem {
263         union nf_inet_addr ip;
264         __be16 port;
265         u8 proto;
266         u8 cidr;
267 };
268
269 struct hash_netport6_telem {
270         union nf_inet_addr ip;
271         __be16 port;
272         u8 proto;
273         u8 cidr;
274         unsigned long timeout;
275 };
276
277 static inline bool
278 hash_netport6_data_equal(const struct hash_netport6_elem *ip1,
279                          const struct hash_netport6_elem *ip2)
280 {
281         return ipv6_addr_cmp(&ip1->ip.in6, &ip2->ip.in6) == 0 &&
282                ip1->port == ip2->port &&
283                ip1->proto == ip2->proto &&
284                ip1->cidr == ip2->cidr;
285 }
286
287 static inline bool
288 hash_netport6_data_isnull(const struct hash_netport6_elem *elem)
289 {
290         return elem->proto == 0;
291 }
292
293 static inline void
294 hash_netport6_data_copy(struct hash_netport6_elem *dst,
295                         const struct hash_netport6_elem *src)
296 {
297         memcpy(dst, src, sizeof(*dst));
298 }
299
300 static inline void
301 hash_netport6_data_zero_out(struct hash_netport6_elem *elem)
302 {
303         elem->proto = 0;
304 }
305
306 static inline void
307 ip6_netmask(union nf_inet_addr *ip, u8 prefix)
308 {
309         ip->ip6[0] &= ip_set_netmask6(prefix)[0];
310         ip->ip6[1] &= ip_set_netmask6(prefix)[1];
311         ip->ip6[2] &= ip_set_netmask6(prefix)[2];
312         ip->ip6[3] &= ip_set_netmask6(prefix)[3];
313 }
314
315 static inline void
316 hash_netport6_data_netmask(struct hash_netport6_elem *elem, u8 cidr)
317 {
318         ip6_netmask(&elem->ip, cidr);
319         elem->cidr = cidr;
320 }
321
322 static bool
323 hash_netport6_data_list(struct sk_buff *skb,
324                         const struct hash_netport6_elem *data)
325 {
326         NLA_PUT_IPADDR6(skb, IPSET_ATTR_IP, &data->ip);
327         NLA_PUT_NET16(skb, IPSET_ATTR_PORT, data->port);
328         NLA_PUT_U8(skb, IPSET_ATTR_CIDR, data->cidr);
329         NLA_PUT_U8(skb, IPSET_ATTR_PROTO, data->proto);
330         return 0;
331
332 nla_put_failure:
333         return 1;
334 }
335
336 static bool
337 hash_netport6_data_tlist(struct sk_buff *skb,
338                          const struct hash_netport6_elem *data)
339 {
340         const struct hash_netport6_telem *e =
341                 (const struct hash_netport6_telem *)data;
342
343         NLA_PUT_IPADDR6(skb, IPSET_ATTR_IP, &e->ip);
344         NLA_PUT_NET16(skb, IPSET_ATTR_PORT, data->port);
345         NLA_PUT_U8(skb, IPSET_ATTR_CIDR, data->cidr);
346         NLA_PUT_U8(skb, IPSET_ATTR_PROTO, data->proto);
347         NLA_PUT_NET32(skb, IPSET_ATTR_TIMEOUT,
348                       htonl(ip_set_timeout_get(e->timeout)));
349         return 0;
350
351 nla_put_failure:
352         return 1;
353 }
354
355 #undef PF
356 #undef HOST_MASK
357
358 #define PF              6
359 #define HOST_MASK       128
360 #include <linux/netfilter/ipset/ip_set_ahash.h>
361
362 static inline void
363 hash_netport6_data_next(struct ip_set_hash *h,
364                         const struct hash_netport6_elem *d)
365 {
366         h->next.port = ntohs(d->port);
367 }
368
369 static int
370 hash_netport6_kadt(struct ip_set *set, const struct sk_buff *skb,
371                    enum ipset_adt adt, const struct ip_set_adt_opt *opt)
372 {
373         const struct ip_set_hash *h = set->data;
374         ipset_adtfn adtfn = set->variant->adt[adt];
375         struct hash_netport6_elem data = {
376                 .cidr = h->nets[0].cidr || HOST_MASK };
377
378         if (data.cidr == 0)
379                 return -EINVAL;
380         if (adt == IPSET_TEST)
381                 data.cidr = HOST_MASK;
382
383         if (!ip_set_get_ip6_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
384                                  &data.port, &data.proto))
385                 return -EINVAL;
386
387         ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &data.ip.in6);
388         ip6_netmask(&data.ip, data.cidr);
389
390         return adtfn(set, &data, opt_timeout(opt, h), opt->cmdflags);
391 }
392
393 static int
394 hash_netport6_uadt(struct ip_set *set, struct nlattr *tb[],
395                    enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
396 {
397         const struct ip_set_hash *h = set->data;
398         ipset_adtfn adtfn = set->variant->adt[adt];
399         struct hash_netport6_elem data = { .cidr = HOST_MASK };
400         u32 port, port_to;
401         u32 timeout = h->timeout;
402         bool with_ports = false;
403         int ret;
404
405         if (unlikely(!tb[IPSET_ATTR_IP] ||
406                      !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
407                      !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
408                      !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
409                 return -IPSET_ERR_PROTOCOL;
410
411         if (tb[IPSET_ATTR_LINENO])
412                 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
413
414         ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &data.ip);
415         if (ret)
416                 return ret;
417
418         if (tb[IPSET_ATTR_CIDR])
419                 data.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
420         if (!data.cidr)
421                 return -IPSET_ERR_INVALID_CIDR;
422         ip6_netmask(&data.ip, data.cidr);
423
424         if (tb[IPSET_ATTR_PORT])
425                 data.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
426         else
427                 return -IPSET_ERR_PROTOCOL;
428
429         if (tb[IPSET_ATTR_PROTO]) {
430                 data.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
431                 with_ports = ip_set_proto_with_ports(data.proto);
432
433                 if (data.proto == 0)
434                         return -IPSET_ERR_INVALID_PROTO;
435         } else
436                 return -IPSET_ERR_MISSING_PROTO;
437
438         if (!(with_ports || data.proto == IPPROTO_ICMPV6))
439                 data.port = 0;
440
441         if (tb[IPSET_ATTR_TIMEOUT]) {
442                 if (!with_timeout(h->timeout))
443                         return -IPSET_ERR_TIMEOUT;
444                 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
445         }
446
447         if (adt == IPSET_TEST || !with_ports || !tb[IPSET_ATTR_PORT_TO]) {
448                 ret = adtfn(set, &data, timeout, flags);
449                 return ip_set_eexist(ret, flags) ? 0 : ret;
450         }
451
452         port = ntohs(data.port);
453         port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
454         if (port > port_to)
455                 swap(port, port_to);
456
457         if (retried)
458                 port = h->next.port;
459         for (; port <= port_to; port++) {
460                 data.port = htons(port);
461                 ret = adtfn(set, &data, timeout, flags);
462
463                 if (ret && !ip_set_eexist(ret, flags))
464                         return ret;
465                 else
466                         ret = 0;
467         }
468         return ret;
469 }
470
471 /* Create hash:ip type of sets */
472
473 static int
474 hash_netport_create(struct ip_set *set, struct nlattr *tb[], u32 flags)
475 {
476         struct ip_set_hash *h;
477         u32 hashsize = IPSET_DEFAULT_HASHSIZE, maxelem = IPSET_DEFAULT_MAXELEM;
478         u8 hbits;
479
480         if (!(set->family == AF_INET || set->family == AF_INET6))
481                 return -IPSET_ERR_INVALID_FAMILY;
482
483         if (unlikely(!ip_set_optattr_netorder(tb, IPSET_ATTR_HASHSIZE) ||
484                      !ip_set_optattr_netorder(tb, IPSET_ATTR_MAXELEM) ||
485                      !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
486                 return -IPSET_ERR_PROTOCOL;
487
488         if (tb[IPSET_ATTR_HASHSIZE]) {
489                 hashsize = ip_set_get_h32(tb[IPSET_ATTR_HASHSIZE]);
490                 if (hashsize < IPSET_MIMINAL_HASHSIZE)
491                         hashsize = IPSET_MIMINAL_HASHSIZE;
492         }
493
494         if (tb[IPSET_ATTR_MAXELEM])
495                 maxelem = ip_set_get_h32(tb[IPSET_ATTR_MAXELEM]);
496
497         h = kzalloc(sizeof(*h)
498                     + sizeof(struct ip_set_hash_nets)
499                       * (set->family == AF_INET ? 32 : 128), GFP_KERNEL);
500         if (!h)
501                 return -ENOMEM;
502
503         h->maxelem = maxelem;
504         get_random_bytes(&h->initval, sizeof(h->initval));
505         h->timeout = IPSET_NO_TIMEOUT;
506
507         hbits = htable_bits(hashsize);
508         h->table = ip_set_alloc(
509                         sizeof(struct htable)
510                         + jhash_size(hbits) * sizeof(struct hbucket));
511         if (!h->table) {
512                 kfree(h);
513                 return -ENOMEM;
514         }
515         h->table->htable_bits = hbits;
516
517         set->data = h;
518
519         if (tb[IPSET_ATTR_TIMEOUT]) {
520                 h->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
521
522                 set->variant = set->family == AF_INET
523                         ? &hash_netport4_tvariant : &hash_netport6_tvariant;
524
525                 if (set->family == AF_INET)
526                         hash_netport4_gc_init(set);
527                 else
528                         hash_netport6_gc_init(set);
529         } else {
530                 set->variant = set->family == AF_INET
531                         ? &hash_netport4_variant : &hash_netport6_variant;
532         }
533
534         pr_debug("create %s hashsize %u (%u) maxelem %u: %p(%p)\n",
535                  set->name, jhash_size(h->table->htable_bits),
536                  h->table->htable_bits, h->maxelem, set->data, h->table);
537
538         return 0;
539 }
540
541 static struct ip_set_type hash_netport_type __read_mostly = {
542         .name           = "hash:net,port",
543         .protocol       = IPSET_PROTOCOL,
544         .features       = IPSET_TYPE_IP | IPSET_TYPE_PORT,
545         .dimension      = IPSET_DIM_TWO,
546         .family         = AF_UNSPEC,
547         .revision       = 1,
548         .create         = hash_netport_create,
549         .create_policy  = {
550                 [IPSET_ATTR_HASHSIZE]   = { .type = NLA_U32 },
551                 [IPSET_ATTR_MAXELEM]    = { .type = NLA_U32 },
552                 [IPSET_ATTR_PROBES]     = { .type = NLA_U8 },
553                 [IPSET_ATTR_RESIZE]     = { .type = NLA_U8  },
554                 [IPSET_ATTR_PROTO]      = { .type = NLA_U8 },
555                 [IPSET_ATTR_TIMEOUT]    = { .type = NLA_U32 },
556         },
557         .adt_policy     = {
558                 [IPSET_ATTR_IP]         = { .type = NLA_NESTED },
559                 [IPSET_ATTR_PORT]       = { .type = NLA_U16 },
560                 [IPSET_ATTR_PORT_TO]    = { .type = NLA_U16 },
561                 [IPSET_ATTR_PROTO]      = { .type = NLA_U8 },
562                 [IPSET_ATTR_CIDR]       = { .type = NLA_U8 },
563                 [IPSET_ATTR_TIMEOUT]    = { .type = NLA_U32 },
564                 [IPSET_ATTR_LINENO]     = { .type = NLA_U32 },
565         },
566         .me             = THIS_MODULE,
567 };
568
569 static int __init
570 hash_netport_init(void)
571 {
572         return ip_set_type_register(&hash_netport_type);
573 }
574
575 static void __exit
576 hash_netport_fini(void)
577 {
578         ip_set_type_unregister(&hash_netport_type);
579 }
580
581 module_init(hash_netport_init);
582 module_exit(hash_netport_fini);