blob: 8d3c3efbbf17d5822167e814fdcd672e9d0d10a1 [file] [log] [blame]
Jozsef Kadlecsikb3837022011-02-01 15:52:54 +01001/* 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>
Jozsef Kadlecsikb3837022011-02-01 15:52:54 +010015#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
26MODULE_LICENSE("GPL");
27MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
28MODULE_DESCRIPTION("hash:net type of IP sets");
29MODULE_ALIAS("ip_set_hash:net");
30
31/* Type specific function prefix */
32#define TYPE hash_net
33
34static bool
35hash_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 */
43struct hash_net4_elem {
44 __be32 ip;
45 u16 padding0;
46 u8 padding1;
47 u8 cidr;
48};
49
50/* Member elements with timeout support */
51struct hash_net4_telem {
52 __be32 ip;
53 u16 padding0;
54 u8 padding1;
55 u8 cidr;
56 unsigned long timeout;
57};
58
59static inline bool
60hash_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
66static inline bool
67hash_net4_data_isnull(const struct hash_net4_elem *elem)
68{
69 return elem->cidr == 0;
70}
71
72static inline void
73hash_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
80static inline void
81hash_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 */
88static inline void
89hash_net4_data_zero_out(struct hash_net4_elem *elem)
90{
91 elem->cidr = 0;
92}
93
94static bool
95hash_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
101nla_put_failure:
102 return 1;
103}
104
105static bool
106hash_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
118nla_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
Jozsef Kadlecsik3d14b172011-06-16 18:49:17 +0200128static inline void
129hash_net4_data_next(struct ip_set_hash *h,
130 const struct hash_net4_elem *d)
131{
Jozsef Kadlecsikd0d9e0a2011-06-16 18:52:41 +0200132 h->next.ip = ntohl(d->ip);
Jozsef Kadlecsik3d14b172011-06-16 18:49:17 +0200133}
134
Jozsef Kadlecsikb3837022011-02-01 15:52:54 +0100135static int
136hash_net4_kadt(struct ip_set *set, const struct sk_buff *skb,
Jozsef Kadlecsikac8cc922011-06-16 18:42:40 +0200137 enum ipset_adt adt, const struct ip_set_adt_opt *opt)
Jozsef Kadlecsikb3837022011-02-01 15:52:54 +0100138{
139 const struct ip_set_hash *h = set->data;
140 ipset_adtfn adtfn = set->variant->adt[adt];
141 struct hash_net4_elem data = { .cidr = h->nets[0].cidr || HOST_MASK };
142
143 if (data.cidr == 0)
144 return -EINVAL;
145 if (adt == IPSET_TEST)
146 data.cidr = HOST_MASK;
147
Jozsef Kadlecsikac8cc922011-06-16 18:42:40 +0200148 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &data.ip);
Jozsef Kadlecsikb3837022011-02-01 15:52:54 +0100149 data.ip &= ip_set_netmask(data.cidr);
150
Jozsef Kadlecsikac8cc922011-06-16 18:42:40 +0200151 return adtfn(set, &data, opt_timeout(opt, h), opt->cmdflags);
Jozsef Kadlecsikb3837022011-02-01 15:52:54 +0100152}
153
154static int
155hash_net4_uadt(struct ip_set *set, struct nlattr *tb[],
Jozsef Kadlecsik3d14b172011-06-16 18:49:17 +0200156 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
Jozsef Kadlecsikb3837022011-02-01 15:52:54 +0100157{
158 const struct ip_set_hash *h = set->data;
159 ipset_adtfn adtfn = set->variant->adt[adt];
160 struct hash_net4_elem data = { .cidr = HOST_MASK };
161 u32 timeout = h->timeout;
Jozsef Kadlecsikd0d9e0a2011-06-16 18:52:41 +0200162 u32 ip = 0, ip_to, last;
Jozsef Kadlecsikb3837022011-02-01 15:52:54 +0100163 int ret;
164
165 if (unlikely(!tb[IPSET_ATTR_IP] ||
166 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
167 return -IPSET_ERR_PROTOCOL;
168
169 if (tb[IPSET_ATTR_LINENO])
170 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
171
Jozsef Kadlecsikd0d9e0a2011-06-16 18:52:41 +0200172 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip);
Jozsef Kadlecsikb3837022011-02-01 15:52:54 +0100173 if (ret)
174 return ret;
175
Jozsef Kadlecsikd0d9e0a2011-06-16 18:52:41 +0200176 if (tb[IPSET_ATTR_CIDR]) {
Jozsef Kadlecsikb3837022011-02-01 15:52:54 +0100177 data.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
Jozsef Kadlecsikd0d9e0a2011-06-16 18:52:41 +0200178 if (!data.cidr)
179 return -IPSET_ERR_INVALID_CIDR;
180 }
Jozsef Kadlecsikb3837022011-02-01 15:52:54 +0100181
182 if (tb[IPSET_ATTR_TIMEOUT]) {
183 if (!with_timeout(h->timeout))
184 return -IPSET_ERR_TIMEOUT;
185 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
186 }
Jozsef Kadlecsikd0d9e0a2011-06-16 18:52:41 +0200187
188 if (adt == IPSET_TEST || !tb[IPSET_ATTR_IP_TO]) {
189 data.ip = htonl(ip & ip_set_hostmask(data.cidr));
190 ret = adtfn(set, &data, timeout, flags);
191 return ip_set_eexist(ret, flags) ? 0 : ret;
192 }
Jozsef Kadlecsikb3837022011-02-01 15:52:54 +0100193
Jozsef Kadlecsikd0d9e0a2011-06-16 18:52:41 +0200194 ip_to = ip;
195 if (tb[IPSET_ATTR_IP_TO]) {
196 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
197 if (ret)
198 return ret;
199 if (ip_to < ip)
200 swap(ip, ip_to);
201 if (ip + UINT_MAX == ip_to)
202 return -IPSET_ERR_HASH_RANGE;
203 }
204 if (retried)
205 ip = h->next.ip;
206 while (!after(ip, ip_to)) {
207 data.ip = htonl(ip);
208 last = ip_set_range_to_cidr(ip, ip_to, &data.cidr);
209 ret = adtfn(set, &data, timeout, flags);
210 if (ret && !ip_set_eexist(ret, flags))
211 return ret;
212 else
213 ret = 0;
214 ip = last + 1;
215 }
216 return ret;
Jozsef Kadlecsikb3837022011-02-01 15:52:54 +0100217}
218
219static bool
220hash_net_same_set(const struct ip_set *a, const struct ip_set *b)
221{
222 const struct ip_set_hash *x = a->data;
223 const struct ip_set_hash *y = b->data;
224
225 /* Resizing changes htable_bits, so we ignore it */
226 return x->maxelem == y->maxelem &&
227 x->timeout == y->timeout;
228}
229
230/* The type variant functions: IPv6 */
231
232struct hash_net6_elem {
233 union nf_inet_addr ip;
234 u16 padding0;
235 u8 padding1;
236 u8 cidr;
237};
238
239struct hash_net6_telem {
240 union nf_inet_addr ip;
241 u16 padding0;
242 u8 padding1;
243 u8 cidr;
244 unsigned long timeout;
245};
246
247static inline bool
248hash_net6_data_equal(const struct hash_net6_elem *ip1,
249 const struct hash_net6_elem *ip2)
250{
251 return ipv6_addr_cmp(&ip1->ip.in6, &ip2->ip.in6) == 0 &&
252 ip1->cidr == ip2->cidr;
253}
254
255static inline bool
256hash_net6_data_isnull(const struct hash_net6_elem *elem)
257{
258 return elem->cidr == 0;
259}
260
261static inline void
262hash_net6_data_copy(struct hash_net6_elem *dst,
263 const struct hash_net6_elem *src)
264{
265 ipv6_addr_copy(&dst->ip.in6, &src->ip.in6);
266 dst->cidr = src->cidr;
267}
268
269static inline void
270hash_net6_data_zero_out(struct hash_net6_elem *elem)
271{
272 elem->cidr = 0;
273}
274
275static inline void
276ip6_netmask(union nf_inet_addr *ip, u8 prefix)
277{
278 ip->ip6[0] &= ip_set_netmask6(prefix)[0];
279 ip->ip6[1] &= ip_set_netmask6(prefix)[1];
280 ip->ip6[2] &= ip_set_netmask6(prefix)[2];
281 ip->ip6[3] &= ip_set_netmask6(prefix)[3];
282}
283
284static inline void
285hash_net6_data_netmask(struct hash_net6_elem *elem, u8 cidr)
286{
287 ip6_netmask(&elem->ip, cidr);
288 elem->cidr = cidr;
289}
290
291static bool
292hash_net6_data_list(struct sk_buff *skb, const struct hash_net6_elem *data)
293{
294 NLA_PUT_IPADDR6(skb, IPSET_ATTR_IP, &data->ip);
295 NLA_PUT_U8(skb, IPSET_ATTR_CIDR, data->cidr);
296 return 0;
297
298nla_put_failure:
299 return 1;
300}
301
302static bool
303hash_net6_data_tlist(struct sk_buff *skb, const struct hash_net6_elem *data)
304{
305 const struct hash_net6_telem *e =
306 (const struct hash_net6_telem *)data;
307
308 NLA_PUT_IPADDR6(skb, IPSET_ATTR_IP, &e->ip);
309 NLA_PUT_U8(skb, IPSET_ATTR_CIDR, e->cidr);
310 NLA_PUT_NET32(skb, IPSET_ATTR_TIMEOUT,
311 htonl(ip_set_timeout_get(e->timeout)));
312 return 0;
313
314nla_put_failure:
315 return 1;
316}
317
318#undef PF
319#undef HOST_MASK
320
321#define PF 6
322#define HOST_MASK 128
323#include <linux/netfilter/ipset/ip_set_ahash.h>
324
Jozsef Kadlecsik3d14b172011-06-16 18:49:17 +0200325static inline void
326hash_net6_data_next(struct ip_set_hash *h,
327 const struct hash_net6_elem *d)
328{
329}
330
Jozsef Kadlecsikb3837022011-02-01 15:52:54 +0100331static int
332hash_net6_kadt(struct ip_set *set, const struct sk_buff *skb,
Jozsef Kadlecsikac8cc922011-06-16 18:42:40 +0200333 enum ipset_adt adt, const struct ip_set_adt_opt *opt)
Jozsef Kadlecsikb3837022011-02-01 15:52:54 +0100334{
335 const struct ip_set_hash *h = set->data;
336 ipset_adtfn adtfn = set->variant->adt[adt];
337 struct hash_net6_elem data = { .cidr = h->nets[0].cidr || HOST_MASK };
338
339 if (data.cidr == 0)
340 return -EINVAL;
341 if (adt == IPSET_TEST)
342 data.cidr = HOST_MASK;
343
Jozsef Kadlecsikac8cc922011-06-16 18:42:40 +0200344 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &data.ip.in6);
Jozsef Kadlecsikb3837022011-02-01 15:52:54 +0100345 ip6_netmask(&data.ip, data.cidr);
346
Jozsef Kadlecsikac8cc922011-06-16 18:42:40 +0200347 return adtfn(set, &data, opt_timeout(opt, h), opt->cmdflags);
Jozsef Kadlecsikb3837022011-02-01 15:52:54 +0100348}
349
350static int
351hash_net6_uadt(struct ip_set *set, struct nlattr *tb[],
Jozsef Kadlecsik3d14b172011-06-16 18:49:17 +0200352 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
Jozsef Kadlecsikb3837022011-02-01 15:52:54 +0100353{
354 const struct ip_set_hash *h = set->data;
355 ipset_adtfn adtfn = set->variant->adt[adt];
356 struct hash_net6_elem data = { .cidr = HOST_MASK };
357 u32 timeout = h->timeout;
358 int ret;
359
360 if (unlikely(!tb[IPSET_ATTR_IP] ||
361 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
362 return -IPSET_ERR_PROTOCOL;
Jozsef Kadlecsikd0d9e0a2011-06-16 18:52:41 +0200363 if (unlikely(tb[IPSET_ATTR_IP_TO]))
364 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
Jozsef Kadlecsikb3837022011-02-01 15:52:54 +0100365
366 if (tb[IPSET_ATTR_LINENO])
367 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
368
369 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &data.ip);
370 if (ret)
371 return ret;
372
373 if (tb[IPSET_ATTR_CIDR])
374 data.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
375
376 if (!data.cidr)
377 return -IPSET_ERR_INVALID_CIDR;
378
379 ip6_netmask(&data.ip, data.cidr);
380
381 if (tb[IPSET_ATTR_TIMEOUT]) {
382 if (!with_timeout(h->timeout))
383 return -IPSET_ERR_TIMEOUT;
384 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
385 }
386
Jozsef Kadlecsik54162192011-06-16 18:40:55 +0200387 ret = adtfn(set, &data, timeout, flags);
Jozsef Kadlecsikb3837022011-02-01 15:52:54 +0100388
389 return ip_set_eexist(ret, flags) ? 0 : ret;
390}
391
392/* Create hash:ip type of sets */
393
394static int
395hash_net_create(struct ip_set *set, struct nlattr *tb[], u32 flags)
396{
397 u32 hashsize = IPSET_DEFAULT_HASHSIZE, maxelem = IPSET_DEFAULT_MAXELEM;
398 struct ip_set_hash *h;
399 u8 hbits;
400
401 if (!(set->family == AF_INET || set->family == AF_INET6))
402 return -IPSET_ERR_INVALID_FAMILY;
403
404 if (unlikely(!ip_set_optattr_netorder(tb, IPSET_ATTR_HASHSIZE) ||
405 !ip_set_optattr_netorder(tb, IPSET_ATTR_MAXELEM) ||
406 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
407 return -IPSET_ERR_PROTOCOL;
408
409 if (tb[IPSET_ATTR_HASHSIZE]) {
410 hashsize = ip_set_get_h32(tb[IPSET_ATTR_HASHSIZE]);
411 if (hashsize < IPSET_MIMINAL_HASHSIZE)
412 hashsize = IPSET_MIMINAL_HASHSIZE;
413 }
414
415 if (tb[IPSET_ATTR_MAXELEM])
416 maxelem = ip_set_get_h32(tb[IPSET_ATTR_MAXELEM]);
417
418 h = kzalloc(sizeof(*h)
419 + sizeof(struct ip_set_hash_nets)
420 * (set->family == AF_INET ? 32 : 128), GFP_KERNEL);
421 if (!h)
422 return -ENOMEM;
423
424 h->maxelem = maxelem;
425 get_random_bytes(&h->initval, sizeof(h->initval));
426 h->timeout = IPSET_NO_TIMEOUT;
427
428 hbits = htable_bits(hashsize);
429 h->table = ip_set_alloc(
430 sizeof(struct htable)
431 + jhash_size(hbits) * sizeof(struct hbucket));
432 if (!h->table) {
433 kfree(h);
434 return -ENOMEM;
435 }
436 h->table->htable_bits = hbits;
437
438 set->data = h;
439
440 if (tb[IPSET_ATTR_TIMEOUT]) {
441 h->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
442
443 set->variant = set->family == AF_INET
444 ? &hash_net4_tvariant : &hash_net6_tvariant;
445
446 if (set->family == AF_INET)
447 hash_net4_gc_init(set);
448 else
449 hash_net6_gc_init(set);
450 } else {
451 set->variant = set->family == AF_INET
452 ? &hash_net4_variant : &hash_net6_variant;
453 }
454
455 pr_debug("create %s hashsize %u (%u) maxelem %u: %p(%p)\n",
456 set->name, jhash_size(h->table->htable_bits),
457 h->table->htable_bits, h->maxelem, set->data, h->table);
458
459 return 0;
460}
461
462static struct ip_set_type hash_net_type __read_mostly = {
463 .name = "hash:net",
464 .protocol = IPSET_PROTOCOL,
465 .features = IPSET_TYPE_IP,
466 .dimension = IPSET_DIM_ONE,
467 .family = AF_UNSPEC,
Jozsef Kadlecsikf1e00b32011-06-16 18:51:41 +0200468 .revision_min = 0,
Jozsef Kadlecsikd0d9e0a2011-06-16 18:52:41 +0200469 .revision_max = 1, /* Range as input support for IPv4 added */
Jozsef Kadlecsikb3837022011-02-01 15:52:54 +0100470 .create = hash_net_create,
471 .create_policy = {
472 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
473 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
474 [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
475 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
476 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
477 },
478 .adt_policy = {
479 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
Jozsef Kadlecsikd0d9e0a2011-06-16 18:52:41 +0200480 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
Jozsef Kadlecsikb3837022011-02-01 15:52:54 +0100481 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
482 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
483 },
484 .me = THIS_MODULE,
485};
486
487static int __init
488hash_net_init(void)
489{
490 return ip_set_type_register(&hash_net_type);
491}
492
493static void __exit
494hash_net_fini(void)
495{
496 ip_set_type_unregister(&hash_net_type);
497}
498
499module_init(hash_net_init);
500module_exit(hash_net_fini);