53964bc831aac0ac6c691b6bdfc3f7740b7d2125
[cascardo/linux.git] / net / netfilter / ipset / ip_set_hash_ip.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 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/uaccess.h>
16 #include <linux/bitops.h>
17 #include <linux/spinlock.h>
18 #include <linux/random.h>
19 #include <net/ip.h>
20 #include <net/ipv6.h>
21 #include <net/netlink.h>
22 #include <net/tcp.h>
23
24 #include <linux/netfilter.h>
25 #include <linux/netfilter/ipset/pfxlen.h>
26 #include <linux/netfilter/ipset/ip_set.h>
27 #include <linux/netfilter/ipset/ip_set_timeout.h>
28 #include <linux/netfilter/ipset/ip_set_hash.h>
29
30 MODULE_LICENSE("GPL");
31 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
32 MODULE_DESCRIPTION("hash:ip type of IP sets");
33 MODULE_ALIAS("ip_set_hash:ip");
34
35 /* Type specific function prefix */
36 #define TYPE            hash_ip
37
38 static bool
39 hash_ip_same_set(const struct ip_set *a, const struct ip_set *b);
40
41 #define hash_ip4_same_set       hash_ip_same_set
42 #define hash_ip6_same_set       hash_ip_same_set
43
44 /* The type variant functions: IPv4 */
45
46 /* Member elements without timeout */
47 struct hash_ip4_elem {
48         __be32 ip;
49 };
50
51 /* Member elements with timeout support */
52 struct hash_ip4_telem {
53         __be32 ip;
54         unsigned long timeout;
55 };
56
57 static inline bool
58 hash_ip4_data_equal(const struct hash_ip4_elem *ip1,
59                     const struct hash_ip4_elem *ip2)
60 {
61         return ip1->ip == ip2->ip;
62 }
63
64 static inline bool
65 hash_ip4_data_isnull(const struct hash_ip4_elem *elem)
66 {
67         return elem->ip == 0;
68 }
69
70 static inline void
71 hash_ip4_data_copy(struct hash_ip4_elem *dst, const struct hash_ip4_elem *src)
72 {
73         dst->ip = src->ip;
74 }
75
76 /* Zero valued IP addresses cannot be stored */
77 static inline void
78 hash_ip4_data_zero_out(struct hash_ip4_elem *elem)
79 {
80         elem->ip = 0;
81 }
82
83 static inline bool
84 hash_ip4_data_list(struct sk_buff *skb, const struct hash_ip4_elem *data)
85 {
86         NLA_PUT_IPADDR4(skb, IPSET_ATTR_IP, data->ip);
87         return 0;
88
89 nla_put_failure:
90         return 1;
91 }
92
93 static bool
94 hash_ip4_data_tlist(struct sk_buff *skb, const struct hash_ip4_elem *data)
95 {
96         const struct hash_ip4_telem *tdata =
97                 (const struct hash_ip4_telem *)data;
98
99         NLA_PUT_IPADDR4(skb, IPSET_ATTR_IP, tdata->ip);
100         NLA_PUT_NET32(skb, IPSET_ATTR_TIMEOUT,
101                       htonl(ip_set_timeout_get(tdata->timeout)));
102
103         return 0;
104
105 nla_put_failure:
106         return 1;
107 }
108
109 #define IP_SET_HASH_WITH_NETMASK
110 #define PF              4
111 #define HOST_MASK       32
112 #include <linux/netfilter/ipset/ip_set_ahash.h>
113
114 static int
115 hash_ip4_kadt(struct ip_set *set, const struct sk_buff *skb,
116               enum ipset_adt adt, u8 pf, u8 dim, u8 flags)
117 {
118         const struct ip_set_hash *h = set->data;
119         ipset_adtfn adtfn = set->variant->adt[adt];
120         __be32 ip;
121
122         ip4addrptr(skb, flags & IPSET_DIM_ONE_SRC, &ip);
123         ip &= ip_set_netmask(h->netmask);
124         if (ip == 0)
125                 return -EINVAL;
126
127         return adtfn(set, &ip, h->timeout);
128 }
129
130 static int
131 hash_ip4_uadt(struct ip_set *set, struct nlattr *tb[],
132               enum ipset_adt adt, u32 *lineno, u32 flags)
133 {
134         const struct ip_set_hash *h = set->data;
135         ipset_adtfn adtfn = set->variant->adt[adt];
136         u32 ip, ip_to, hosts, timeout = h->timeout;
137         __be32 nip;
138         int ret = 0;
139
140         if (unlikely(!tb[IPSET_ATTR_IP] ||
141                      !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
142                 return -IPSET_ERR_PROTOCOL;
143
144         if (tb[IPSET_ATTR_LINENO])
145                 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
146
147         ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip);
148         if (ret)
149                 return ret;
150
151         ip &= ip_set_hostmask(h->netmask);
152
153         if (tb[IPSET_ATTR_TIMEOUT]) {
154                 if (!with_timeout(h->timeout))
155                         return -IPSET_ERR_TIMEOUT;
156                 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
157         }
158
159         if (adt == IPSET_TEST) {
160                 nip = htonl(ip);
161                 if (nip == 0)
162                         return -IPSET_ERR_HASH_ELEM;
163                 return adtfn(set, &nip, timeout);
164         }
165
166         if (tb[IPSET_ATTR_IP_TO]) {
167                 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
168                 if (ret)
169                         return ret;
170                 if (ip > ip_to)
171                         swap(ip, ip_to);
172         } else if (tb[IPSET_ATTR_CIDR]) {
173                 u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
174
175                 if (cidr > 32)
176                         return -IPSET_ERR_INVALID_CIDR;
177                 ip &= ip_set_hostmask(cidr);
178                 ip_to = ip | ~ip_set_hostmask(cidr);
179         } else
180                 ip_to = ip;
181
182         hosts = h->netmask == 32 ? 1 : 2 << (32 - h->netmask - 1);
183
184         for (; !before(ip_to, ip); ip += hosts) {
185                 nip = htonl(ip);
186                 if (nip == 0)
187                         return -IPSET_ERR_HASH_ELEM;
188                 ret = adtfn(set, &nip, timeout);
189
190                 if (ret && !ip_set_eexist(ret, flags))
191                         return ret;
192                 else
193                         ret = 0;
194         }
195         return ret;
196 }
197
198 static bool
199 hash_ip_same_set(const struct ip_set *a, const struct ip_set *b)
200 {
201         const struct ip_set_hash *x = a->data;
202         const struct ip_set_hash *y = b->data;
203
204         /* Resizing changes htable_bits, so we ignore it */
205         return x->maxelem == y->maxelem &&
206                x->timeout == y->timeout &&
207                x->netmask == y->netmask;
208 }
209
210 /* The type variant functions: IPv6 */
211
212 struct hash_ip6_elem {
213         union nf_inet_addr ip;
214 };
215
216 struct hash_ip6_telem {
217         union nf_inet_addr ip;
218         unsigned long timeout;
219 };
220
221 static inline bool
222 hash_ip6_data_equal(const struct hash_ip6_elem *ip1,
223                     const struct hash_ip6_elem *ip2)
224 {
225         return ipv6_addr_cmp(&ip1->ip.in6, &ip2->ip.in6) == 0;
226 }
227
228 static inline bool
229 hash_ip6_data_isnull(const struct hash_ip6_elem *elem)
230 {
231         return ipv6_addr_any(&elem->ip.in6);
232 }
233
234 static inline void
235 hash_ip6_data_copy(struct hash_ip6_elem *dst, const struct hash_ip6_elem *src)
236 {
237         ipv6_addr_copy(&dst->ip.in6, &src->ip.in6);
238 }
239
240 static inline void
241 hash_ip6_data_zero_out(struct hash_ip6_elem *elem)
242 {
243         ipv6_addr_set(&elem->ip.in6, 0, 0, 0, 0);
244 }
245
246 static inline void
247 ip6_netmask(union nf_inet_addr *ip, u8 prefix)
248 {
249         ip->ip6[0] &= ip_set_netmask6(prefix)[0];
250         ip->ip6[1] &= ip_set_netmask6(prefix)[1];
251         ip->ip6[2] &= ip_set_netmask6(prefix)[2];
252         ip->ip6[3] &= ip_set_netmask6(prefix)[3];
253 }
254
255 static bool
256 hash_ip6_data_list(struct sk_buff *skb, const struct hash_ip6_elem *data)
257 {
258         NLA_PUT_IPADDR6(skb, IPSET_ATTR_IP, &data->ip);
259         return 0;
260
261 nla_put_failure:
262         return 1;
263 }
264
265 static bool
266 hash_ip6_data_tlist(struct sk_buff *skb, const struct hash_ip6_elem *data)
267 {
268         const struct hash_ip6_telem *e =
269                 (const struct hash_ip6_telem *)data;
270
271         NLA_PUT_IPADDR6(skb, IPSET_ATTR_IP, &e->ip);
272         NLA_PUT_NET32(skb, IPSET_ATTR_TIMEOUT,
273                       htonl(ip_set_timeout_get(e->timeout)));
274         return 0;
275
276 nla_put_failure:
277         return 1;
278 }
279
280 #undef PF
281 #undef HOST_MASK
282
283 #define PF              6
284 #define HOST_MASK       128
285 #include <linux/netfilter/ipset/ip_set_ahash.h>
286
287 static int
288 hash_ip6_kadt(struct ip_set *set, const struct sk_buff *skb,
289               enum ipset_adt adt, u8 pf, u8 dim, u8 flags)
290 {
291         const struct ip_set_hash *h = set->data;
292         ipset_adtfn adtfn = set->variant->adt[adt];
293         union nf_inet_addr ip;
294
295         ip6addrptr(skb, flags & IPSET_DIM_ONE_SRC, &ip.in6);
296         ip6_netmask(&ip, h->netmask);
297         if (ipv6_addr_any(&ip.in6))
298                 return -EINVAL;
299
300         return adtfn(set, &ip, h->timeout);
301 }
302
303 static const struct nla_policy hash_ip6_adt_policy[IPSET_ATTR_ADT_MAX + 1] = {
304         [IPSET_ATTR_IP]         = { .type = NLA_NESTED },
305         [IPSET_ATTR_TIMEOUT]    = { .type = NLA_U32 },
306         [IPSET_ATTR_LINENO]     = { .type = NLA_U32 },
307 };
308
309 static int
310 hash_ip6_uadt(struct ip_set *set, struct nlattr *tb[],
311               enum ipset_adt adt, u32 *lineno, u32 flags)
312 {
313         const struct ip_set_hash *h = set->data;
314         ipset_adtfn adtfn = set->variant->adt[adt];
315         union nf_inet_addr ip;
316         u32 timeout = h->timeout;
317         int ret;
318
319         if (unlikely(!tb[IPSET_ATTR_IP] ||
320                      !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
321                      tb[IPSET_ATTR_IP_TO] ||
322                      tb[IPSET_ATTR_CIDR]))
323                 return -IPSET_ERR_PROTOCOL;
324
325         if (tb[IPSET_ATTR_LINENO])
326                 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
327
328         ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &ip);
329         if (ret)
330                 return ret;
331
332         ip6_netmask(&ip, h->netmask);
333         if (ipv6_addr_any(&ip.in6))
334                 return -IPSET_ERR_HASH_ELEM;
335
336         if (tb[IPSET_ATTR_TIMEOUT]) {
337                 if (!with_timeout(h->timeout))
338                         return -IPSET_ERR_TIMEOUT;
339                 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
340         }
341
342         ret = adtfn(set, &ip, timeout);
343
344         return ip_set_eexist(ret, flags) ? 0 : ret;
345 }
346
347 /* Create hash:ip type of sets */
348
349 static int
350 hash_ip_create(struct ip_set *set, struct nlattr *tb[], u32 flags)
351 {
352         u32 hashsize = IPSET_DEFAULT_HASHSIZE, maxelem = IPSET_DEFAULT_MAXELEM;
353         u8 netmask, hbits;
354         struct ip_set_hash *h;
355
356         if (!(set->family == AF_INET || set->family == AF_INET6))
357                 return -IPSET_ERR_INVALID_FAMILY;
358         netmask = set->family == AF_INET ? 32 : 128;
359         pr_debug("Create set %s with family %s\n",
360                  set->name, set->family == AF_INET ? "inet" : "inet6");
361
362         if (unlikely(!ip_set_optattr_netorder(tb, IPSET_ATTR_HASHSIZE) ||
363                      !ip_set_optattr_netorder(tb, IPSET_ATTR_MAXELEM) ||
364                      !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
365                 return -IPSET_ERR_PROTOCOL;
366
367         if (tb[IPSET_ATTR_HASHSIZE]) {
368                 hashsize = ip_set_get_h32(tb[IPSET_ATTR_HASHSIZE]);
369                 if (hashsize < IPSET_MIMINAL_HASHSIZE)
370                         hashsize = IPSET_MIMINAL_HASHSIZE;
371         }
372
373         if (tb[IPSET_ATTR_MAXELEM])
374                 maxelem = ip_set_get_h32(tb[IPSET_ATTR_MAXELEM]);
375
376         if (tb[IPSET_ATTR_NETMASK]) {
377                 netmask = nla_get_u8(tb[IPSET_ATTR_NETMASK]);
378
379                 if ((set->family == AF_INET && netmask > 32) ||
380                     (set->family == AF_INET6 && netmask > 128) ||
381                     netmask == 0)
382                         return -IPSET_ERR_INVALID_NETMASK;
383         }
384
385         h = kzalloc(sizeof(*h), GFP_KERNEL);
386         if (!h)
387                 return -ENOMEM;
388
389         h->maxelem = maxelem;
390         h->netmask = netmask;
391         get_random_bytes(&h->initval, sizeof(h->initval));
392         h->timeout = IPSET_NO_TIMEOUT;
393
394         hbits = htable_bits(hashsize);
395         h->table = ip_set_alloc(
396                         sizeof(struct htable)
397                         + jhash_size(hbits) * sizeof(struct hbucket));
398         if (!h->table) {
399                 kfree(h);
400                 return -ENOMEM;
401         }
402         h->table->htable_bits = hbits;
403
404         set->data = h;
405
406         if (tb[IPSET_ATTR_TIMEOUT]) {
407                 h->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
408
409                 set->variant = set->family == AF_INET
410                         ? &hash_ip4_tvariant : &hash_ip6_tvariant;
411
412                 if (set->family == AF_INET)
413                         hash_ip4_gc_init(set);
414                 else
415                         hash_ip6_gc_init(set);
416         } else {
417                 set->variant = set->family == AF_INET
418                         ? &hash_ip4_variant : &hash_ip6_variant;
419         }
420
421         pr_debug("create %s hashsize %u (%u) maxelem %u: %p(%p)\n",
422                  set->name, jhash_size(h->table->htable_bits),
423                  h->table->htable_bits, h->maxelem, set->data, h->table);
424
425         return 0;
426 }
427
428 static struct ip_set_type hash_ip_type __read_mostly = {
429         .name           = "hash:ip",
430         .protocol       = IPSET_PROTOCOL,
431         .features       = IPSET_TYPE_IP,
432         .dimension      = IPSET_DIM_ONE,
433         .family         = AF_UNSPEC,
434         .revision       = 0,
435         .create         = hash_ip_create,
436         .create_policy  = {
437                 [IPSET_ATTR_HASHSIZE]   = { .type = NLA_U32 },
438                 [IPSET_ATTR_MAXELEM]    = { .type = NLA_U32 },
439                 [IPSET_ATTR_PROBES]     = { .type = NLA_U8 },
440                 [IPSET_ATTR_RESIZE]     = { .type = NLA_U8  },
441                 [IPSET_ATTR_TIMEOUT]    = { .type = NLA_U32 },
442                 [IPSET_ATTR_NETMASK]    = { .type = NLA_U8  },
443         },
444         .adt_policy     = {
445                 [IPSET_ATTR_IP]         = { .type = NLA_NESTED },
446                 [IPSET_ATTR_IP_TO]      = { .type = NLA_NESTED },
447                 [IPSET_ATTR_CIDR]       = { .type = NLA_U8 },
448                 [IPSET_ATTR_TIMEOUT]    = { .type = NLA_U32 },
449                 [IPSET_ATTR_LINENO]     = { .type = NLA_U32 },
450         },
451         .me             = THIS_MODULE,
452 };
453
454 static int __init
455 hash_ip_init(void)
456 {
457         return ip_set_type_register(&hash_ip_type);
458 }
459
460 static void __exit
461 hash_ip_fini(void)
462 {
463         ip_set_type_unregister(&hash_ip_type);
464 }
465
466 module_init(hash_ip_init);
467 module_exit(hash_ip_fini);