12 #include <linux/module.h>
15 #include <linux/errno.h>
16 #include <linux/bitops.h>
18 #include <linux/netlink.h>
25 #include <linux/netfilter/ipset/ip_set.h>
26 #include <linux/netfilter/ipset/ip_set_bitmap.h>
27 #define IP_SET_BITMAP_TIMEOUT
30 #define REVISION_MIN 0
31 #define REVISION_MAX 0
81 bitmap_ip_del(
struct ip_set *
set,
void *value,
u32 timeout,
u32 flags)
93 bitmap_ip_list(
const struct ip_set *
set,
97 struct nlattr *atd, *nested;
110 nla_nest_cancel(skb, atd);
113 goto nla_put_failure;
117 goto nla_put_failure;
126 nla_nest_cancel(skb, nested);
138 bitmap_ip_ttest(
struct ip_set *
set,
void *value,
u32 timeout,
u32 flags)
144 return ip_set_timeout_test(members[
id]);
148 bitmap_ip_tadd(
struct ip_set *
set,
void *value,
u32 timeout,
u32 flags)
157 members[
id] = ip_set_timeout_set(timeout);
163 bitmap_ip_tdel(
struct ip_set *
set,
void *value,
u32 timeout,
u32 flags)
166 unsigned long *members = map->
members;
170 if (ip_set_timeout_test(members[
id]))
173 members[
id] = IPSET_ELEM_UNSET;
178 bitmap_ip_tlist(
const struct ip_set *
set,
182 struct nlattr *adt, *nested;
184 const unsigned long *members = map->
members;
191 if (!ip_set_timeout_test(members[
id]))
196 nla_nest_cancel(skb, adt);
199 goto nla_put_failure;
204 htonl(ip_set_timeout_get(members[
id]))))
205 goto nla_put_failure;
216 nla_nest_cancel(skb, nested);
226 bitmap_ip_kadt(
struct ip_set *
set,
const struct sk_buff *skb,
238 ip = ip_to_id(map, ip);
240 return adtfn(
set, &ip, opt_timeout(opt, map), opt->
cmdflags);
258 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
268 if (!with_timeout(map->
timeout))
270 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
274 id = ip_to_id(map, ip);
275 return adtfn(
set, &
id, timeout, flags);
279 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
288 u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
290 if (!cidr || cidr > 32)
299 for (; !before(ip_to, ip); ip += map->
hosts) {
300 id = ip_to_id(map, ip);
301 ret = adtfn(
set, &
id, timeout, flags);
303 if (ret && !ip_set_eexist(ret, flags))
312 bitmap_ip_destroy(
struct ip_set *
set)
316 if (with_timeout(map->
timeout))
326 bitmap_ip_flush(
struct ip_set *
set)
341 goto nla_put_failure;
343 nla_put_ipaddr4(skb, IPSET_ATTR_IP_TO,
htonl(map->
last_ip)) ||
350 nla_put_net32(skb, IPSET_ATTR_TIMEOUT,
htonl(map->
timeout))))
351 goto nla_put_failure;
360 bitmap_ip_same_set(
const struct ip_set *
a,
const struct ip_set *
b)
372 .kadt = bitmap_ip_kadt,
373 .uadt = bitmap_ip_uadt,
379 .destroy = bitmap_ip_destroy,
380 .flush = bitmap_ip_flush,
381 .head = bitmap_ip_head,
382 .list = bitmap_ip_list,
383 .same_set = bitmap_ip_same_set,
387 .kadt = bitmap_ip_kadt,
388 .uadt = bitmap_ip_uadt,
394 .destroy = bitmap_ip_destroy,
395 .flush = bitmap_ip_flush,
396 .head = bitmap_ip_head,
397 .list = bitmap_ip_tlist,
398 .same_set = bitmap_ip_same_set,
402 bitmap_ip_gc(
unsigned long ul_set)
405 struct bitmap_ip *map =
set->data;
412 for (
id = 0;
id < map->
elements;
id++)
413 if (ip_set_timeout_expired(table[
id]))
414 table[
id] = IPSET_ELEM_UNSET;
422 bitmap_ip_gc_init(
struct ip_set *
set)
424 struct bitmap_ip *map =
set->data;
427 map->
gc.data = (
unsigned long)
set;
428 map->
gc.function = bitmap_ip_gc;
436 init_map_ip(
struct ip_set *
set,
struct bitmap_ip *map,
448 map->
timeout = IPSET_NO_TIMEOUT;
459 struct bitmap_ip *
map;
466 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT)))
473 if (tb[IPSET_ATTR_IP_TO]) {
474 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &last_ip);
477 if (first_ip > last_ip) {
483 }
else if (tb[IPSET_ATTR_CIDR]) {
484 u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
493 netmask = nla_get_u8(tb[IPSET_ATTR_NETMASK]);
498 first_ip &= ip_set_hostmask(netmask);
499 last_ip |= ~ip_set_hostmask(netmask);
504 elements = (
u64)last_ip - first_ip + 1;
509 mask = range_to_mask(first_ip, last_ip, &mask_bits);
511 if ((!mask && (first_ip || last_ip != 0xFFFFFFFF)) ||
512 netmask <= mask_bits)
515 pr_debug(
"mask_bits %u, netmask %u\n", mask_bits, netmask);
516 hosts = 2 << (32 - netmask - 1);
517 elements = 2 << (netmask - mask_bits - 1);
522 pr_debug(
"hosts %u, elements %llu\n",
523 hosts, (
unsigned long long)elements);
529 if (tb[IPSET_ATTR_TIMEOUT]) {
532 if (!init_map_ip(
set, map, first_ip, last_ip,
533 elements, hosts, netmask)) {
538 map->
timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
539 set->variant = &bitmap_tip;
541 bitmap_ip_gc_init(
set);
543 map->
memsize = bitmap_bytes(0, elements - 1);
545 if (!init_map_ip(
set, map, first_ip, last_ip,
546 elements, hosts, netmask)) {
551 set->variant = &bitmap_ip;
564 .create = bitmap_ip_create,