]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - net/netfilter/ipset/ip_set_bitmap_ip.c
doc: dt: mtd: partitions: add compatible property to "partitions" node
[karo-tx-linux.git] / net / netfilter / ipset / ip_set_bitmap_ip.c
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>
4  *
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.
8  */
9
10 /* Kernel module implementing an IP set type: the bitmap:ip type */
11
12 #include <linux/module.h>
13 #include <linux/ip.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>
22 #include <net/tcp.h>
23
24 #include <linux/netfilter/ipset/pfxlen.h>
25 #include <linux/netfilter/ipset/ip_set.h>
26 #include <linux/netfilter/ipset/ip_set_bitmap.h>
27
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 */
32
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");
37
38 #define MTYPE           bitmap_ip
39 #define HOST_MASK       32
40
41 /* Type structure */
42 struct bitmap_ip {
43         void *members;          /* the set members */
44         void *extensions;       /* data extensions */
45         u32 first_ip;           /* host byte order, included in range */
46         u32 last_ip;            /* host byte order, included in range */
47         u32 elements;           /* number of max elements in the set */
48         u32 hosts;              /* number of hosts in a subnet */
49         size_t memsize;         /* members size */
50         u8 netmask;             /* subnet netmask */
51         struct timer_list gc;   /* garbage collection */
52 };
53
54 /* ADT structure for generic function args */
55 struct bitmap_ip_adt_elem {
56         u16 id;
57 };
58
59 static inline u32
60 ip_to_id(const struct bitmap_ip *m, u32 ip)
61 {
62         return ((ip & ip_set_hostmask(m->netmask)) - m->first_ip) / m->hosts;
63 }
64
65 /* Common functions */
66
67 static inline int
68 bitmap_ip_do_test(const struct bitmap_ip_adt_elem *e,
69                   struct bitmap_ip *map, size_t dsize)
70 {
71         return !!test_bit(e->id, map->members);
72 }
73
74 static inline int
75 bitmap_ip_gc_test(u16 id, const struct bitmap_ip *map, size_t dsize)
76 {
77         return !!test_bit(id, map->members);
78 }
79
80 static inline int
81 bitmap_ip_do_add(const struct bitmap_ip_adt_elem *e, struct bitmap_ip *map,
82                  u32 flags, size_t dsize)
83 {
84         return !!test_bit(e->id, map->members);
85 }
86
87 static inline int
88 bitmap_ip_do_del(const struct bitmap_ip_adt_elem *e, struct bitmap_ip *map)
89 {
90         return !test_and_clear_bit(e->id, map->members);
91 }
92
93 static inline int
94 bitmap_ip_do_list(struct sk_buff *skb, const struct bitmap_ip *map, u32 id,
95                   size_t dsize)
96 {
97         return nla_put_ipaddr4(skb, IPSET_ATTR_IP,
98                         htonl(map->first_ip + id * map->hosts));
99 }
100
101 static inline int
102 bitmap_ip_do_head(struct sk_buff *skb, const struct bitmap_ip *map)
103 {
104         return nla_put_ipaddr4(skb, IPSET_ATTR_IP, htonl(map->first_ip)) ||
105                nla_put_ipaddr4(skb, IPSET_ATTR_IP_TO, htonl(map->last_ip)) ||
106                (map->netmask != 32 &&
107                 nla_put_u8(skb, IPSET_ATTR_NETMASK, map->netmask));
108 }
109
110 static int
111 bitmap_ip_kadt(struct ip_set *set, const struct sk_buff *skb,
112                const struct xt_action_param *par,
113                enum ipset_adt adt, struct ip_set_adt_opt *opt)
114 {
115         struct bitmap_ip *map = set->data;
116         ipset_adtfn adtfn = set->variant->adt[adt];
117         struct bitmap_ip_adt_elem e = { .id = 0 };
118         struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
119         u32 ip;
120
121         ip = ntohl(ip4addr(skb, opt->flags & IPSET_DIM_ONE_SRC));
122         if (ip < map->first_ip || ip > map->last_ip)
123                 return -IPSET_ERR_BITMAP_RANGE;
124
125         e.id = ip_to_id(map, ip);
126
127         return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
128 }
129
130 static int
131 bitmap_ip_uadt(struct ip_set *set, struct nlattr *tb[],
132                enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
133 {
134         struct bitmap_ip *map = set->data;
135         ipset_adtfn adtfn = set->variant->adt[adt];
136         u32 ip = 0, ip_to = 0;
137         struct bitmap_ip_adt_elem e = { .id = 0 };
138         struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
139         int ret = 0;
140
141         if (tb[IPSET_ATTR_LINENO])
142                 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
143
144         if (unlikely(!tb[IPSET_ATTR_IP]))
145                 return -IPSET_ERR_PROTOCOL;
146
147         ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip);
148         if (ret)
149                 return ret;
150
151         ret = ip_set_get_extensions(set, tb, &ext);
152         if (ret)
153                 return ret;
154
155         if (ip < map->first_ip || ip > map->last_ip)
156                 return -IPSET_ERR_BITMAP_RANGE;
157
158         if (adt == IPSET_TEST) {
159                 e.id = ip_to_id(map, ip);
160                 return adtfn(set, &e, &ext, &ext, flags);
161         }
162
163         if (tb[IPSET_ATTR_IP_TO]) {
164                 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
165                 if (ret)
166                         return ret;
167                 if (ip > ip_to) {
168                         swap(ip, ip_to);
169                         if (ip < map->first_ip)
170                                 return -IPSET_ERR_BITMAP_RANGE;
171                 }
172         } else if (tb[IPSET_ATTR_CIDR]) {
173                 u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
174
175                 if (!cidr || cidr > HOST_MASK)
176                         return -IPSET_ERR_INVALID_CIDR;
177                 ip_set_mask_from_to(ip, ip_to, cidr);
178         } else {
179                 ip_to = ip;
180         }
181
182         if (ip_to > map->last_ip)
183                 return -IPSET_ERR_BITMAP_RANGE;
184
185         for (; !before(ip_to, ip); ip += map->hosts) {
186                 e.id = ip_to_id(map, ip);
187                 ret = adtfn(set, &e, &ext, &ext, flags);
188
189                 if (ret && !ip_set_eexist(ret, flags))
190                         return ret;
191
192                 ret = 0;
193         }
194         return ret;
195 }
196
197 static bool
198 bitmap_ip_same_set(const struct ip_set *a, const struct ip_set *b)
199 {
200         const struct bitmap_ip *x = a->data;
201         const struct bitmap_ip *y = b->data;
202
203         return x->first_ip == y->first_ip &&
204                x->last_ip == y->last_ip &&
205                x->netmask == y->netmask &&
206                a->timeout == b->timeout &&
207                a->extensions == b->extensions;
208 }
209
210 /* Plain variant */
211
212 struct bitmap_ip_elem {
213 };
214
215 #include "ip_set_bitmap_gen.h"
216
217 /* Create bitmap:ip type of sets */
218
219 static bool
220 init_map_ip(struct ip_set *set, struct bitmap_ip *map,
221             u32 first_ip, u32 last_ip,
222             u32 elements, u32 hosts, u8 netmask)
223 {
224         map->members = ip_set_alloc(map->memsize);
225         if (!map->members)
226                 return false;
227         if (set->dsize) {
228                 map->extensions = ip_set_alloc(set->dsize * elements);
229                 if (!map->extensions) {
230                         kfree(map->members);
231                         return false;
232                 }
233         }
234         map->first_ip = first_ip;
235         map->last_ip = last_ip;
236         map->elements = elements;
237         map->hosts = hosts;
238         map->netmask = netmask;
239         set->timeout = IPSET_NO_TIMEOUT;
240
241         set->data = map;
242         set->family = NFPROTO_IPV4;
243
244         return true;
245 }
246
247 static int
248 bitmap_ip_create(struct net *net, struct ip_set *set, struct nlattr *tb[],
249                  u32 flags)
250 {
251         struct bitmap_ip *map;
252         u32 first_ip = 0, last_ip = 0, hosts;
253         u64 elements;
254         u8 netmask = 32;
255         int ret;
256
257         if (unlikely(!tb[IPSET_ATTR_IP] ||
258                      !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
259                      !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
260                 return -IPSET_ERR_PROTOCOL;
261
262         ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &first_ip);
263         if (ret)
264                 return ret;
265
266         if (tb[IPSET_ATTR_IP_TO]) {
267                 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &last_ip);
268                 if (ret)
269                         return ret;
270                 if (first_ip > last_ip) {
271                         u32 tmp = first_ip;
272
273                         first_ip = last_ip;
274                         last_ip = tmp;
275                 }
276         } else if (tb[IPSET_ATTR_CIDR]) {
277                 u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
278
279                 if (cidr >= HOST_MASK)
280                         return -IPSET_ERR_INVALID_CIDR;
281                 ip_set_mask_from_to(first_ip, last_ip, cidr);
282         } else {
283                 return -IPSET_ERR_PROTOCOL;
284         }
285
286         if (tb[IPSET_ATTR_NETMASK]) {
287                 netmask = nla_get_u8(tb[IPSET_ATTR_NETMASK]);
288
289                 if (netmask > HOST_MASK)
290                         return -IPSET_ERR_INVALID_NETMASK;
291
292                 first_ip &= ip_set_hostmask(netmask);
293                 last_ip |= ~ip_set_hostmask(netmask);
294         }
295
296         if (netmask == 32) {
297                 hosts = 1;
298                 elements = (u64)last_ip - first_ip + 1;
299         } else {
300                 u8 mask_bits;
301                 u32 mask;
302
303                 mask = range_to_mask(first_ip, last_ip, &mask_bits);
304
305                 if ((!mask && (first_ip || last_ip != 0xFFFFFFFF)) ||
306                     netmask <= mask_bits)
307                         return -IPSET_ERR_BITMAP_RANGE;
308
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);
312         }
313         if (elements > IPSET_BITMAP_MAX_RANGE + 1)
314                 return -IPSET_ERR_BITMAP_RANGE_SIZE;
315
316         pr_debug("hosts %u, elements %llu\n",
317                  hosts, (unsigned long long)elements);
318
319         map = kzalloc(sizeof(*map), GFP_KERNEL);
320         if (!map)
321                 return -ENOMEM;
322
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)) {
328                 kfree(map);
329                 return -ENOMEM;
330         }
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);
334         }
335         return 0;
336 }
337
338 static struct ip_set_type bitmap_ip_type __read_mostly = {
339         .name           = "bitmap:ip",
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,
347         .create_policy  = {
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 },
354         },
355         .adt_policy     = {
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                                             .len  = IPSET_MAX_COMMENT_SIZE },
365                 [IPSET_ATTR_SKBMARK]    = { .type = NLA_U64 },
366                 [IPSET_ATTR_SKBPRIO]    = { .type = NLA_U32 },
367                 [IPSET_ATTR_SKBQUEUE]   = { .type = NLA_U16 },
368         },
369         .me             = THIS_MODULE,
370 };
371
372 static int __init
373 bitmap_ip_init(void)
374 {
375         return ip_set_type_register(&bitmap_ip_type);
376 }
377
378 static void __exit
379 bitmap_ip_fini(void)
380 {
381         rcu_barrier();
382         ip_set_type_unregister(&bitmap_ip_type);
383 }
384
385 module_init(bitmap_ip_init);
386 module_exit(bitmap_ip_fini);