blob: e6f8bc5771caf036356fef9f0319e7f2029bcd5d [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{
132}
133
Jozsef Kadlecsikb3837022011-02-01 15:52:54 +0100134static int
135hash_net4_kadt(struct ip_set *set, const struct sk_buff *skb,
Jozsef Kadlecsikac8cc922011-06-16 18:42:40 +0200136 enum ipset_adt adt, const struct ip_set_adt_opt *opt)
Jozsef Kadlecsikb3837022011-02-01 15:52:54 +0100137{
138 const struct ip_set_hash *h = set->data;
139 ipset_adtfn adtfn = set->variant->adt[adt];
140 struct hash_net4_elem data = { .cidr = h->nets[0].cidr || HOST_MASK };
141
142 if (data.cidr == 0)
143 return -EINVAL;
144 if (adt == IPSET_TEST)
145 data.cidr = HOST_MASK;
146
Jozsef Kadlecsikac8cc922011-06-16 18:42:40 +0200147 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &data.ip);
Jozsef Kadlecsikb3837022011-02-01 15:52:54 +0100148 data.ip &= ip_set_netmask(data.cidr);
149
Jozsef Kadlecsikac8cc922011-06-16 18:42:40 +0200150 return adtfn(set, &data, opt_timeout(opt, h), opt->cmdflags);
Jozsef Kadlecsikb3837022011-02-01 15:52:54 +0100151}
152
153static int
154hash_net4_uadt(struct ip_set *set, struct nlattr *tb[],
Jozsef Kadlecsik3d14b172011-06-16 18:49:17 +0200155 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
Jozsef Kadlecsikb3837022011-02-01 15:52:54 +0100156{
157 const struct ip_set_hash *h = set->data;
158 ipset_adtfn adtfn = set->variant->adt[adt];
159 struct hash_net4_elem data = { .cidr = HOST_MASK };
160 u32 timeout = h->timeout;
161 int ret;
162
163 if (unlikely(!tb[IPSET_ATTR_IP] ||
164 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
165 return -IPSET_ERR_PROTOCOL;
166
167 if (tb[IPSET_ATTR_LINENO])
168 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
169
170 ret = ip_set_get_ipaddr4(tb[IPSET_ATTR_IP], &data.ip);
171 if (ret)
172 return ret;
173
174 if (tb[IPSET_ATTR_CIDR])
175 data.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
176
177 if (!data.cidr)
178 return -IPSET_ERR_INVALID_CIDR;
179
180 data.ip &= ip_set_netmask(data.cidr);
181
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 }
187
Jozsef Kadlecsik54162192011-06-16 18:40:55 +0200188 ret = adtfn(set, &data, timeout, flags);
Jozsef Kadlecsikb3837022011-02-01 15:52:54 +0100189
190 return ip_set_eexist(ret, flags) ? 0 : ret;
191}
192
193static bool
194hash_net_same_set(const struct ip_set *a, const struct ip_set *b)
195{
196 const struct ip_set_hash *x = a->data;
197 const struct ip_set_hash *y = b->data;
198
199 /* Resizing changes htable_bits, so we ignore it */
200 return x->maxelem == y->maxelem &&
201 x->timeout == y->timeout;
202}
203
204/* The type variant functions: IPv6 */
205
206struct hash_net6_elem {
207 union nf_inet_addr ip;
208 u16 padding0;
209 u8 padding1;
210 u8 cidr;
211};
212
213struct hash_net6_telem {
214 union nf_inet_addr ip;
215 u16 padding0;
216 u8 padding1;
217 u8 cidr;
218 unsigned long timeout;
219};
220
221static inline bool
222hash_net6_data_equal(const struct hash_net6_elem *ip1,
223 const struct hash_net6_elem *ip2)
224{
225 return ipv6_addr_cmp(&ip1->ip.in6, &ip2->ip.in6) == 0 &&
226 ip1->cidr == ip2->cidr;
227}
228
229static inline bool
230hash_net6_data_isnull(const struct hash_net6_elem *elem)
231{
232 return elem->cidr == 0;
233}
234
235static inline void
236hash_net6_data_copy(struct hash_net6_elem *dst,
237 const struct hash_net6_elem *src)
238{
239 ipv6_addr_copy(&dst->ip.in6, &src->ip.in6);
240 dst->cidr = src->cidr;
241}
242
243static inline void
244hash_net6_data_zero_out(struct hash_net6_elem *elem)
245{
246 elem->cidr = 0;
247}
248
249static inline void
250ip6_netmask(union nf_inet_addr *ip, u8 prefix)
251{
252 ip->ip6[0] &= ip_set_netmask6(prefix)[0];
253 ip->ip6[1] &= ip_set_netmask6(prefix)[1];
254 ip->ip6[2] &= ip_set_netmask6(prefix)[2];
255 ip->ip6[3] &= ip_set_netmask6(prefix)[3];
256}
257
258static inline void
259hash_net6_data_netmask(struct hash_net6_elem *elem, u8 cidr)
260{
261 ip6_netmask(&elem->ip, cidr);
262 elem->cidr = cidr;
263}
264
265static bool
266hash_net6_data_list(struct sk_buff *skb, const struct hash_net6_elem *data)
267{
268 NLA_PUT_IPADDR6(skb, IPSET_ATTR_IP, &data->ip);
269 NLA_PUT_U8(skb, IPSET_ATTR_CIDR, data->cidr);
270 return 0;
271
272nla_put_failure:
273 return 1;
274}
275
276static bool
277hash_net6_data_tlist(struct sk_buff *skb, const struct hash_net6_elem *data)
278{
279 const struct hash_net6_telem *e =
280 (const struct hash_net6_telem *)data;
281
282 NLA_PUT_IPADDR6(skb, IPSET_ATTR_IP, &e->ip);
283 NLA_PUT_U8(skb, IPSET_ATTR_CIDR, e->cidr);
284 NLA_PUT_NET32(skb, IPSET_ATTR_TIMEOUT,
285 htonl(ip_set_timeout_get(e->timeout)));
286 return 0;
287
288nla_put_failure:
289 return 1;
290}
291
292#undef PF
293#undef HOST_MASK
294
295#define PF 6
296#define HOST_MASK 128
297#include <linux/netfilter/ipset/ip_set_ahash.h>
298
Jozsef Kadlecsik3d14b172011-06-16 18:49:17 +0200299static inline void
300hash_net6_data_next(struct ip_set_hash *h,
301 const struct hash_net6_elem *d)
302{
303}
304
Jozsef Kadlecsikb3837022011-02-01 15:52:54 +0100305static int
306hash_net6_kadt(struct ip_set *set, const struct sk_buff *skb,
Jozsef Kadlecsikac8cc922011-06-16 18:42:40 +0200307 enum ipset_adt adt, const struct ip_set_adt_opt *opt)
Jozsef Kadlecsikb3837022011-02-01 15:52:54 +0100308{
309 const struct ip_set_hash *h = set->data;
310 ipset_adtfn adtfn = set->variant->adt[adt];
311 struct hash_net6_elem data = { .cidr = h->nets[0].cidr || HOST_MASK };
312
313 if (data.cidr == 0)
314 return -EINVAL;
315 if (adt == IPSET_TEST)
316 data.cidr = HOST_MASK;
317
Jozsef Kadlecsikac8cc922011-06-16 18:42:40 +0200318 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &data.ip.in6);
Jozsef Kadlecsikb3837022011-02-01 15:52:54 +0100319 ip6_netmask(&data.ip, data.cidr);
320
Jozsef Kadlecsikac8cc922011-06-16 18:42:40 +0200321 return adtfn(set, &data, opt_timeout(opt, h), opt->cmdflags);
Jozsef Kadlecsikb3837022011-02-01 15:52:54 +0100322}
323
324static int
325hash_net6_uadt(struct ip_set *set, struct nlattr *tb[],
Jozsef Kadlecsik3d14b172011-06-16 18:49:17 +0200326 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
Jozsef Kadlecsikb3837022011-02-01 15:52:54 +0100327{
328 const struct ip_set_hash *h = set->data;
329 ipset_adtfn adtfn = set->variant->adt[adt];
330 struct hash_net6_elem data = { .cidr = HOST_MASK };
331 u32 timeout = h->timeout;
332 int ret;
333
334 if (unlikely(!tb[IPSET_ATTR_IP] ||
335 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
336 return -IPSET_ERR_PROTOCOL;
337
338 if (tb[IPSET_ATTR_LINENO])
339 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
340
341 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &data.ip);
342 if (ret)
343 return ret;
344
345 if (tb[IPSET_ATTR_CIDR])
346 data.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
347
348 if (!data.cidr)
349 return -IPSET_ERR_INVALID_CIDR;
350
351 ip6_netmask(&data.ip, data.cidr);
352
353 if (tb[IPSET_ATTR_TIMEOUT]) {
354 if (!with_timeout(h->timeout))
355 return -IPSET_ERR_TIMEOUT;
356 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
357 }
358
Jozsef Kadlecsik54162192011-06-16 18:40:55 +0200359 ret = adtfn(set, &data, timeout, flags);
Jozsef Kadlecsikb3837022011-02-01 15:52:54 +0100360
361 return ip_set_eexist(ret, flags) ? 0 : ret;
362}
363
364/* Create hash:ip type of sets */
365
366static int
367hash_net_create(struct ip_set *set, struct nlattr *tb[], u32 flags)
368{
369 u32 hashsize = IPSET_DEFAULT_HASHSIZE, maxelem = IPSET_DEFAULT_MAXELEM;
370 struct ip_set_hash *h;
371 u8 hbits;
372
373 if (!(set->family == AF_INET || set->family == AF_INET6))
374 return -IPSET_ERR_INVALID_FAMILY;
375
376 if (unlikely(!ip_set_optattr_netorder(tb, IPSET_ATTR_HASHSIZE) ||
377 !ip_set_optattr_netorder(tb, IPSET_ATTR_MAXELEM) ||
378 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
379 return -IPSET_ERR_PROTOCOL;
380
381 if (tb[IPSET_ATTR_HASHSIZE]) {
382 hashsize = ip_set_get_h32(tb[IPSET_ATTR_HASHSIZE]);
383 if (hashsize < IPSET_MIMINAL_HASHSIZE)
384 hashsize = IPSET_MIMINAL_HASHSIZE;
385 }
386
387 if (tb[IPSET_ATTR_MAXELEM])
388 maxelem = ip_set_get_h32(tb[IPSET_ATTR_MAXELEM]);
389
390 h = kzalloc(sizeof(*h)
391 + sizeof(struct ip_set_hash_nets)
392 * (set->family == AF_INET ? 32 : 128), GFP_KERNEL);
393 if (!h)
394 return -ENOMEM;
395
396 h->maxelem = maxelem;
397 get_random_bytes(&h->initval, sizeof(h->initval));
398 h->timeout = IPSET_NO_TIMEOUT;
399
400 hbits = htable_bits(hashsize);
401 h->table = ip_set_alloc(
402 sizeof(struct htable)
403 + jhash_size(hbits) * sizeof(struct hbucket));
404 if (!h->table) {
405 kfree(h);
406 return -ENOMEM;
407 }
408 h->table->htable_bits = hbits;
409
410 set->data = h;
411
412 if (tb[IPSET_ATTR_TIMEOUT]) {
413 h->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
414
415 set->variant = set->family == AF_INET
416 ? &hash_net4_tvariant : &hash_net6_tvariant;
417
418 if (set->family == AF_INET)
419 hash_net4_gc_init(set);
420 else
421 hash_net6_gc_init(set);
422 } else {
423 set->variant = set->family == AF_INET
424 ? &hash_net4_variant : &hash_net6_variant;
425 }
426
427 pr_debug("create %s hashsize %u (%u) maxelem %u: %p(%p)\n",
428 set->name, jhash_size(h->table->htable_bits),
429 h->table->htable_bits, h->maxelem, set->data, h->table);
430
431 return 0;
432}
433
434static struct ip_set_type hash_net_type __read_mostly = {
435 .name = "hash:net",
436 .protocol = IPSET_PROTOCOL,
437 .features = IPSET_TYPE_IP,
438 .dimension = IPSET_DIM_ONE,
439 .family = AF_UNSPEC,
440 .revision = 0,
441 .create = hash_net_create,
442 .create_policy = {
443 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
444 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
445 [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
446 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
447 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
448 },
449 .adt_policy = {
450 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
451 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
452 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
453 },
454 .me = THIS_MODULE,
455};
456
457static int __init
458hash_net_init(void)
459{
460 return ip_set_type_register(&hash_net_type);
461}
462
463static void __exit
464hash_net_fini(void)
465{
466 ip_set_type_unregister(&hash_net_type);
467}
468
469module_init(hash_net_init);
470module_exit(hash_net_fini);