1 /* Copyright (C) 2003-2011 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
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.
8 /* Kernel module implementing an IP set type: the hash:net type */
10 #include <linux/jhash.h>
11 #include <linux/module.h>
13 #include <linux/skbuff.h>
14 #include <linux/errno.h>
15 #include <linux/random.h>
18 #include <net/netlink.h>
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>
26 #define REVISION_MIN 0
27 /* 1 Range as input support for IPv4 added */
28 #define REVISION_MAX 2 /* nomatch flag support added */
30 MODULE_LICENSE("GPL");
31 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
32 IP_SET_MODULE_DESC("hash:net", REVISION_MIN, REVISION_MAX);
33 MODULE_ALIAS("ip_set_hash:net");
35 /* Type specific function prefix */
39 hash_net_same_set(const struct ip_set *a, const struct ip_set *b);
41 #define hash_net4_same_set hash_net_same_set
42 #define hash_net6_same_set hash_net_same_set
44 /* The type variant functions: IPv4 */
46 /* Member elements without timeout */
47 struct hash_net4_elem {
54 /* Member elements with timeout support */
55 struct hash_net4_telem {
60 unsigned long timeout;
64 hash_net4_data_equal(const struct hash_net4_elem *ip1,
65 const struct hash_net4_elem *ip2,
68 return ip1->ip == ip2->ip &&
69 ip1->cidr == ip2->cidr;
73 hash_net4_data_isnull(const struct hash_net4_elem *elem)
75 return elem->cidr == 0;
79 hash_net4_data_copy(struct hash_net4_elem *dst,
80 const struct hash_net4_elem *src)
83 dst->cidr = src->cidr;
84 dst->nomatch = src->nomatch;
88 hash_net4_data_flags(struct hash_net4_elem *dst, u32 flags)
90 dst->nomatch = !!(flags & IPSET_FLAG_NOMATCH);
94 hash_net4_data_reset_flags(struct hash_net4_elem *dst, u32 *flags)
97 *flags = IPSET_FLAG_NOMATCH;
103 hash_net4_data_match(const struct hash_net4_elem *elem)
105 return elem->nomatch ? -ENOTEMPTY : 1;
109 hash_net4_data_netmask(struct hash_net4_elem *elem, u8 cidr)
111 elem->ip &= ip_set_netmask(cidr);
115 /* Zero CIDR values cannot be stored */
117 hash_net4_data_zero_out(struct hash_net4_elem *elem)
123 hash_net4_data_list(struct sk_buff *skb, const struct hash_net4_elem *data)
125 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
127 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip) ||
128 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr) ||
130 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
131 goto nla_put_failure;
139 hash_net4_data_tlist(struct sk_buff *skb, const struct hash_net4_elem *data)
141 const struct hash_net4_telem *tdata =
142 (const struct hash_net4_telem *)data;
143 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
145 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, tdata->ip) ||
146 nla_put_u8(skb, IPSET_ATTR_CIDR, tdata->cidr) ||
147 nla_put_net32(skb, IPSET_ATTR_TIMEOUT,
148 htonl(ip_set_timeout_get(tdata->timeout))) ||
150 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
151 goto nla_put_failure;
158 #define IP_SET_HASH_WITH_NETS
162 #include <linux/netfilter/ipset/ip_set_ahash.h>
165 hash_net4_data_next(struct ip_set_hash *h,
166 const struct hash_net4_elem *d)
172 hash_net4_kadt(struct ip_set *set, const struct sk_buff *skb,
173 const struct xt_action_param *par,
174 enum ipset_adt adt, const struct ip_set_adt_opt *opt)
176 const struct ip_set_hash *h = set->data;
177 ipset_adtfn adtfn = set->variant->adt[adt];
178 struct hash_net4_elem data = {
179 .cidr = h->nets[0].cidr ? h->nets[0].cidr : HOST_MASK
184 if (adt == IPSET_TEST)
185 data.cidr = HOST_MASK;
187 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &data.ip);
188 data.ip &= ip_set_netmask(data.cidr);
190 return adtfn(set, &data, opt_timeout(opt, h), opt->cmdflags);
194 hash_net4_uadt(struct ip_set *set, struct nlattr *tb[],
195 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
197 const struct ip_set_hash *h = set->data;
198 ipset_adtfn adtfn = set->variant->adt[adt];
199 struct hash_net4_elem data = { .cidr = HOST_MASK };
200 u32 timeout = h->timeout;
201 u32 ip = 0, ip_to, last;
204 if (unlikely(!tb[IPSET_ATTR_IP] ||
205 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
206 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
207 return -IPSET_ERR_PROTOCOL;
209 if (tb[IPSET_ATTR_LINENO])
210 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
212 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip);
216 if (tb[IPSET_ATTR_CIDR]) {
217 data.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
218 if (!data.cidr || data.cidr > HOST_MASK)
219 return -IPSET_ERR_INVALID_CIDR;
222 if (tb[IPSET_ATTR_TIMEOUT]) {
223 if (!with_timeout(h->timeout))
224 return -IPSET_ERR_TIMEOUT;
225 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
228 if (tb[IPSET_ATTR_CADT_FLAGS] && adt == IPSET_ADD) {
229 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
230 if (cadt_flags & IPSET_FLAG_NOMATCH)
231 flags |= (cadt_flags << 16);
234 if (adt == IPSET_TEST || !tb[IPSET_ATTR_IP_TO]) {
235 data.ip = htonl(ip & ip_set_hostmask(data.cidr));
236 ret = adtfn(set, &data, timeout, flags);
237 return ip_set_eexist(ret, flags) ? 0 : ret;
241 if (tb[IPSET_ATTR_IP_TO]) {
242 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
247 if (ip + UINT_MAX == ip_to)
248 return -IPSET_ERR_HASH_RANGE;
251 ip = ntohl(h->next.ip);
252 while (!after(ip, ip_to)) {
254 last = ip_set_range_to_cidr(ip, ip_to, &data.cidr);
255 ret = adtfn(set, &data, timeout, flags);
256 if (ret && !ip_set_eexist(ret, flags))
266 hash_net_same_set(const struct ip_set *a, const struct ip_set *b)
268 const struct ip_set_hash *x = a->data;
269 const struct ip_set_hash *y = b->data;
271 /* Resizing changes htable_bits, so we ignore it */
272 return x->maxelem == y->maxelem &&
273 x->timeout == y->timeout;
276 /* The type variant functions: IPv6 */
278 struct hash_net6_elem {
279 union nf_inet_addr ip;
285 struct hash_net6_telem {
286 union nf_inet_addr ip;
290 unsigned long timeout;
294 hash_net6_data_equal(const struct hash_net6_elem *ip1,
295 const struct hash_net6_elem *ip2,
298 return ipv6_addr_equal(&ip1->ip.in6, &ip2->ip.in6) &&
299 ip1->cidr == ip2->cidr;
303 hash_net6_data_isnull(const struct hash_net6_elem *elem)
305 return elem->cidr == 0;
309 hash_net6_data_copy(struct hash_net6_elem *dst,
310 const struct hash_net6_elem *src)
312 dst->ip.in6 = src->ip.in6;
313 dst->cidr = src->cidr;
314 dst->nomatch = src->nomatch;
318 hash_net6_data_flags(struct hash_net6_elem *dst, u32 flags)
320 dst->nomatch = !!(flags & IPSET_FLAG_NOMATCH);
324 hash_net6_data_reset_flags(struct hash_net6_elem *dst, u32 *flags)
327 *flags = IPSET_FLAG_NOMATCH;
333 hash_net6_data_match(const struct hash_net6_elem *elem)
335 return elem->nomatch ? -ENOTEMPTY : 1;
339 hash_net6_data_zero_out(struct hash_net6_elem *elem)
345 ip6_netmask(union nf_inet_addr *ip, u8 prefix)
347 ip->ip6[0] &= ip_set_netmask6(prefix)[0];
348 ip->ip6[1] &= ip_set_netmask6(prefix)[1];
349 ip->ip6[2] &= ip_set_netmask6(prefix)[2];
350 ip->ip6[3] &= ip_set_netmask6(prefix)[3];
354 hash_net6_data_netmask(struct hash_net6_elem *elem, u8 cidr)
356 ip6_netmask(&elem->ip, cidr);
361 hash_net6_data_list(struct sk_buff *skb, const struct hash_net6_elem *data)
363 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
365 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip.in6) ||
366 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr) ||
368 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
369 goto nla_put_failure;
377 hash_net6_data_tlist(struct sk_buff *skb, const struct hash_net6_elem *data)
379 const struct hash_net6_telem *e =
380 (const struct hash_net6_telem *)data;
381 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
383 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &e->ip.in6) ||
384 nla_put_u8(skb, IPSET_ATTR_CIDR, e->cidr) ||
385 nla_put_net32(skb, IPSET_ATTR_TIMEOUT,
386 htonl(ip_set_timeout_get(e->timeout))) ||
388 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
389 goto nla_put_failure;
400 #define HOST_MASK 128
401 #include <linux/netfilter/ipset/ip_set_ahash.h>
404 hash_net6_data_next(struct ip_set_hash *h,
405 const struct hash_net6_elem *d)
410 hash_net6_kadt(struct ip_set *set, const struct sk_buff *skb,
411 const struct xt_action_param *par,
412 enum ipset_adt adt, const struct ip_set_adt_opt *opt)
414 const struct ip_set_hash *h = set->data;
415 ipset_adtfn adtfn = set->variant->adt[adt];
416 struct hash_net6_elem data = {
417 .cidr = h->nets[0].cidr ? h->nets[0].cidr : HOST_MASK
422 if (adt == IPSET_TEST)
423 data.cidr = HOST_MASK;
425 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &data.ip.in6);
426 ip6_netmask(&data.ip, data.cidr);
428 return adtfn(set, &data, opt_timeout(opt, h), opt->cmdflags);
432 hash_net6_uadt(struct ip_set *set, struct nlattr *tb[],
433 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
435 const struct ip_set_hash *h = set->data;
436 ipset_adtfn adtfn = set->variant->adt[adt];
437 struct hash_net6_elem data = { .cidr = HOST_MASK };
438 u32 timeout = h->timeout;
441 if (unlikely(!tb[IPSET_ATTR_IP] ||
442 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
443 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
444 return -IPSET_ERR_PROTOCOL;
445 if (unlikely(tb[IPSET_ATTR_IP_TO]))
446 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
448 if (tb[IPSET_ATTR_LINENO])
449 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
451 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &data.ip);
455 if (tb[IPSET_ATTR_CIDR])
456 data.cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
458 if (!data.cidr || data.cidr > HOST_MASK)
459 return -IPSET_ERR_INVALID_CIDR;
461 ip6_netmask(&data.ip, data.cidr);
463 if (tb[IPSET_ATTR_TIMEOUT]) {
464 if (!with_timeout(h->timeout))
465 return -IPSET_ERR_TIMEOUT;
466 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
469 if (tb[IPSET_ATTR_CADT_FLAGS] && adt == IPSET_ADD) {
470 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
471 if (cadt_flags & IPSET_FLAG_NOMATCH)
472 flags |= (cadt_flags << 16);
475 ret = adtfn(set, &data, timeout, flags);
477 return ip_set_eexist(ret, flags) ? 0 : ret;
480 /* Create hash:ip type of sets */
483 hash_net_create(struct ip_set *set, struct nlattr *tb[], u32 flags)
485 u32 hashsize = IPSET_DEFAULT_HASHSIZE, maxelem = IPSET_DEFAULT_MAXELEM;
486 struct ip_set_hash *h;
490 if (!(set->family == NFPROTO_IPV4 || set->family == NFPROTO_IPV6))
491 return -IPSET_ERR_INVALID_FAMILY;
493 if (unlikely(!ip_set_optattr_netorder(tb, IPSET_ATTR_HASHSIZE) ||
494 !ip_set_optattr_netorder(tb, IPSET_ATTR_MAXELEM) ||
495 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
496 return -IPSET_ERR_PROTOCOL;
498 if (tb[IPSET_ATTR_HASHSIZE]) {
499 hashsize = ip_set_get_h32(tb[IPSET_ATTR_HASHSIZE]);
500 if (hashsize < IPSET_MIMINAL_HASHSIZE)
501 hashsize = IPSET_MIMINAL_HASHSIZE;
504 if (tb[IPSET_ATTR_MAXELEM])
505 maxelem = ip_set_get_h32(tb[IPSET_ATTR_MAXELEM]);
507 h = kzalloc(sizeof(*h)
508 + sizeof(struct ip_set_hash_nets)
509 * (set->family == NFPROTO_IPV4 ? 32 : 128), GFP_KERNEL);
513 h->maxelem = maxelem;
514 get_random_bytes(&h->initval, sizeof(h->initval));
515 h->timeout = IPSET_NO_TIMEOUT;
517 hbits = htable_bits(hashsize);
518 hsize = htable_size(hbits);
523 h->table = ip_set_alloc(hsize);
528 h->table->htable_bits = hbits;
532 if (tb[IPSET_ATTR_TIMEOUT]) {
533 h->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
535 set->variant = set->family == NFPROTO_IPV4
536 ? &hash_net4_tvariant : &hash_net6_tvariant;
538 if (set->family == NFPROTO_IPV4)
539 hash_net4_gc_init(set);
541 hash_net6_gc_init(set);
543 set->variant = set->family == NFPROTO_IPV4
544 ? &hash_net4_variant : &hash_net6_variant;
547 pr_debug("create %s hashsize %u (%u) maxelem %u: %p(%p)\n",
548 set->name, jhash_size(h->table->htable_bits),
549 h->table->htable_bits, h->maxelem, set->data, h->table);
554 static struct ip_set_type hash_net_type __read_mostly = {
556 .protocol = IPSET_PROTOCOL,
557 .features = IPSET_TYPE_IP | IPSET_TYPE_NOMATCH,
558 .dimension = IPSET_DIM_ONE,
559 .family = NFPROTO_UNSPEC,
560 .revision_min = REVISION_MIN,
561 .revision_max = REVISION_MAX,
562 .create = hash_net_create,
564 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
565 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
566 [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
567 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
568 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
571 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
572 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
573 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
574 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
575 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
583 return ip_set_type_register(&hash_net_type);
589 ip_set_type_unregister(&hash_net_type);
592 module_init(hash_net_init);
593 module_exit(hash_net_fini);