1 /* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
2 * Patrick Schaaf <bof@bof.de>
3 * Copyright (C) 2003-2013 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
10 /* Kernel module implementing an IP set type: the bitmap:ip type */
12 #include <linux/module.h>
14 #include <linux/skbuff.h>
15 #include <linux/errno.h>
16 #include <linux/bitops.h>
17 #include <linux/spinlock.h>
18 #include <linux/netlink.h>
19 #include <linux/jiffies.h>
20 #include <linux/timer.h>
21 #include <net/netlink.h>
24 #include <linux/netfilter/ipset/pfxlen.h>
25 #include <linux/netfilter/ipset/ip_set.h>
26 #include <linux/netfilter/ipset/ip_set_bitmap.h>
28 #define IPSET_TYPE_REV_MIN 0
29 /* 1 Counter support added */
30 /* 2 Comment support added */
31 #define IPSET_TYPE_REV_MAX 3 /* skbinfo support added */
33 MODULE_LICENSE("GPL");
34 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
35 IP_SET_MODULE_DESC("bitmap:ip", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
36 MODULE_ALIAS("ip_set_bitmap:ip");
38 #define MTYPE bitmap_ip
42 void *members; /* the set members */
43 void *extensions; /* data extensions */
44 u32 first_ip; /* host byte order, included in range */
45 u32 last_ip; /* host byte order, included in range */
46 u32 elements; /* number of max elements in the set */
47 u32 hosts; /* number of hosts in a subnet */
48 size_t memsize; /* members size */
49 u8 netmask; /* subnet netmask */
50 struct timer_list gc; /* garbage collection */
53 /* ADT structure for generic function args */
54 struct bitmap_ip_adt_elem {
59 ip_to_id(const struct bitmap_ip *m, u32 ip)
61 return ((ip & ip_set_hostmask(m->netmask)) - m->first_ip)/m->hosts;
64 /* Common functions */
67 bitmap_ip_do_test(const struct bitmap_ip_adt_elem *e,
68 struct bitmap_ip *map, size_t dsize)
70 return !!test_bit(e->id, map->members);
74 bitmap_ip_gc_test(u16 id, const struct bitmap_ip *map, size_t dsize)
76 return !!test_bit(id, map->members);
80 bitmap_ip_do_add(const struct bitmap_ip_adt_elem *e, struct bitmap_ip *map,
81 u32 flags, size_t dsize)
83 return !!test_and_set_bit(e->id, map->members);
87 bitmap_ip_do_del(const struct bitmap_ip_adt_elem *e, struct bitmap_ip *map)
89 return !test_and_clear_bit(e->id, map->members);
93 bitmap_ip_do_list(struct sk_buff *skb, const struct bitmap_ip *map, u32 id,
96 return nla_put_ipaddr4(skb, IPSET_ATTR_IP,
97 htonl(map->first_ip + id * map->hosts));
101 bitmap_ip_do_head(struct sk_buff *skb, const struct bitmap_ip *map)
103 return nla_put_ipaddr4(skb, IPSET_ATTR_IP, htonl(map->first_ip)) ||
104 nla_put_ipaddr4(skb, IPSET_ATTR_IP_TO, htonl(map->last_ip)) ||
105 (map->netmask != 32 &&
106 nla_put_u8(skb, IPSET_ATTR_NETMASK, map->netmask));
110 bitmap_ip_kadt(struct ip_set *set, const struct sk_buff *skb,
111 const struct xt_action_param *par,
112 enum ipset_adt adt, struct ip_set_adt_opt *opt)
114 struct bitmap_ip *map = set->data;
115 ipset_adtfn adtfn = set->variant->adt[adt];
116 struct bitmap_ip_adt_elem e = { .id = 0 };
117 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
120 ip = ntohl(ip4addr(skb, opt->flags & IPSET_DIM_ONE_SRC));
121 if (ip < map->first_ip || ip > map->last_ip)
122 return -IPSET_ERR_BITMAP_RANGE;
124 e.id = ip_to_id(map, ip);
126 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
130 bitmap_ip_uadt(struct ip_set *set, struct nlattr *tb[],
131 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
133 struct bitmap_ip *map = set->data;
134 ipset_adtfn adtfn = set->variant->adt[adt];
135 u32 ip = 0, ip_to = 0;
136 struct bitmap_ip_adt_elem e = { .id = 0 };
137 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
140 if (unlikely(!tb[IPSET_ATTR_IP] ||
141 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
142 !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) ||
143 !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES) ||
144 !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBMARK) ||
145 !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBPRIO) ||
146 !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBQUEUE)))
147 return -IPSET_ERR_PROTOCOL;
149 if (tb[IPSET_ATTR_LINENO])
150 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
152 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip) ||
153 ip_set_get_extensions(set, tb, &ext);
157 if (ip < map->first_ip || ip > map->last_ip)
158 return -IPSET_ERR_BITMAP_RANGE;
160 if (adt == IPSET_TEST) {
161 e.id = ip_to_id(map, ip);
162 return adtfn(set, &e, &ext, &ext, flags);
165 if (tb[IPSET_ATTR_IP_TO]) {
166 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
171 if (ip < map->first_ip)
172 return -IPSET_ERR_BITMAP_RANGE;
174 } else if (tb[IPSET_ATTR_CIDR]) {
175 u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
177 if (!cidr || cidr > 32)
178 return -IPSET_ERR_INVALID_CIDR;
179 ip_set_mask_from_to(ip, ip_to, cidr);
183 if (ip_to > map->last_ip)
184 return -IPSET_ERR_BITMAP_RANGE;
186 for (; !before(ip_to, ip); ip += map->hosts) {
187 e.id = ip_to_id(map, ip);
188 ret = adtfn(set, &e, &ext, &ext, flags);
190 if (ret && !ip_set_eexist(ret, flags))
199 bitmap_ip_same_set(const struct ip_set *a, const struct ip_set *b)
201 const struct bitmap_ip *x = a->data;
202 const struct bitmap_ip *y = b->data;
204 return x->first_ip == y->first_ip &&
205 x->last_ip == y->last_ip &&
206 x->netmask == y->netmask &&
207 a->timeout == b->timeout &&
208 a->extensions == b->extensions;
213 struct bitmap_ip_elem {
216 #include "ip_set_bitmap_gen.h"
218 /* Create bitmap:ip type of sets */
221 init_map_ip(struct ip_set *set, struct bitmap_ip *map,
222 u32 first_ip, u32 last_ip,
223 u32 elements, u32 hosts, u8 netmask)
225 map->members = ip_set_alloc(map->memsize);
229 map->extensions = ip_set_alloc(set->dsize * elements);
230 if (!map->extensions) {
235 map->first_ip = first_ip;
236 map->last_ip = last_ip;
237 map->elements = elements;
239 map->netmask = netmask;
240 set->timeout = IPSET_NO_TIMEOUT;
243 set->family = NFPROTO_IPV4;
249 bitmap_ip_create(struct net *net, struct ip_set *set, struct nlattr *tb[],
252 struct bitmap_ip *map;
253 u32 first_ip = 0, last_ip = 0, hosts;
258 if (unlikely(!tb[IPSET_ATTR_IP] ||
259 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
260 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
261 return -IPSET_ERR_PROTOCOL;
263 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &first_ip);
267 if (tb[IPSET_ATTR_IP_TO]) {
268 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &last_ip);
271 if (first_ip > last_ip) {
277 } else if (tb[IPSET_ATTR_CIDR]) {
278 u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
281 return -IPSET_ERR_INVALID_CIDR;
282 ip_set_mask_from_to(first_ip, last_ip, cidr);
284 return -IPSET_ERR_PROTOCOL;
286 if (tb[IPSET_ATTR_NETMASK]) {
287 netmask = nla_get_u8(tb[IPSET_ATTR_NETMASK]);
290 return -IPSET_ERR_INVALID_NETMASK;
292 first_ip &= ip_set_hostmask(netmask);
293 last_ip |= ~ip_set_hostmask(netmask);
298 elements = (u64)last_ip - first_ip + 1;
303 mask = range_to_mask(first_ip, last_ip, &mask_bits);
305 if ((!mask && (first_ip || last_ip != 0xFFFFFFFF)) ||
306 netmask <= mask_bits)
307 return -IPSET_ERR_BITMAP_RANGE;
309 pr_debug("mask_bits %u, netmask %u\n", mask_bits, netmask);
310 hosts = 2 << (32 - netmask - 1);
311 elements = 2 << (netmask - mask_bits - 1);
313 if (elements > IPSET_BITMAP_MAX_RANGE + 1)
314 return -IPSET_ERR_BITMAP_RANGE_SIZE;
316 pr_debug("hosts %u, elements %llu\n",
317 hosts, (unsigned long long)elements);
319 map = kzalloc(sizeof(*map), GFP_KERNEL);
323 map->memsize = bitmap_bytes(0, elements - 1);
324 set->variant = &bitmap_ip;
325 set->dsize = ip_set_elem_len(set, tb, 0);
326 if (!init_map_ip(set, map, first_ip, last_ip,
327 elements, hosts, netmask)) {
331 if (tb[IPSET_ATTR_TIMEOUT]) {
332 set->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
333 bitmap_ip_gc_init(set, bitmap_ip_gc);
338 static struct ip_set_type bitmap_ip_type __read_mostly = {
340 .protocol = IPSET_PROTOCOL,
341 .features = IPSET_TYPE_IP,
342 .dimension = IPSET_DIM_ONE,
343 .family = NFPROTO_IPV4,
344 .revision_min = IPSET_TYPE_REV_MIN,
345 .revision_max = IPSET_TYPE_REV_MAX,
346 .create = bitmap_ip_create,
348 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
349 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
350 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
351 [IPSET_ATTR_NETMASK] = { .type = NLA_U8 },
352 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
353 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
356 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
357 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
358 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
359 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
360 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
361 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
362 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
363 [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING },
364 [IPSET_ATTR_SKBMARK] = { .type = NLA_U64 },
365 [IPSET_ATTR_SKBPRIO] = { .type = NLA_U32 },
366 [IPSET_ATTR_SKBQUEUE] = { .type = NLA_U16 },
374 return ip_set_type_register(&bitmap_ip_type);
380 ip_set_type_unregister(&bitmap_ip_type);
383 module_init(bitmap_ip_init);
384 module_exit(bitmap_ip_fini);