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