Add the rt linux 4.1.3-rt3 as base
[kvmfornfv.git] / kernel / net / netfilter / ipset / ip_set_bitmap_port.c
1 /* Copyright (C) 2003-2013 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 bitmap:port type */
9
10 #include <linux/module.h>
11 #include <linux/ip.h>
12 #include <linux/skbuff.h>
13 #include <linux/errno.h>
14 #include <linux/netlink.h>
15 #include <linux/jiffies.h>
16 #include <linux/timer.h>
17 #include <net/netlink.h>
18
19 #include <linux/netfilter/ipset/ip_set.h>
20 #include <linux/netfilter/ipset/ip_set_bitmap.h>
21 #include <linux/netfilter/ipset/ip_set_getport.h>
22
23 #define IPSET_TYPE_REV_MIN      0
24 /*                              1          Counter support added */
25 /*                              2          Comment support added */
26 #define IPSET_TYPE_REV_MAX      3       /* skbinfo support added */
27
28 MODULE_LICENSE("GPL");
29 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
30 IP_SET_MODULE_DESC("bitmap:port", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
31 MODULE_ALIAS("ip_set_bitmap:port");
32
33 #define MTYPE           bitmap_port
34
35 /* Type structure */
36 struct bitmap_port {
37         void *members;          /* the set members */
38         void *extensions;       /* data extensions */
39         u16 first_port;         /* host byte order, included in range */
40         u16 last_port;          /* host byte order, included in range */
41         u32 elements;           /* number of max elements in the set */
42         size_t memsize;         /* members size */
43         struct timer_list gc;   /* garbage collection */
44 };
45
46 /* ADT structure for generic function args */
47 struct bitmap_port_adt_elem {
48         u16 id;
49 };
50
51 static inline u16
52 port_to_id(const struct bitmap_port *m, u16 port)
53 {
54         return port - m->first_port;
55 }
56
57 /* Common functions */
58
59 static inline int
60 bitmap_port_do_test(const struct bitmap_port_adt_elem *e,
61                     const struct bitmap_port *map, size_t dsize)
62 {
63         return !!test_bit(e->id, map->members);
64 }
65
66 static inline int
67 bitmap_port_gc_test(u16 id, const struct bitmap_port *map, size_t dsize)
68 {
69         return !!test_bit(id, map->members);
70 }
71
72 static inline int
73 bitmap_port_do_add(const struct bitmap_port_adt_elem *e,
74                    struct bitmap_port *map, u32 flags, size_t dsize)
75 {
76         return !!test_and_set_bit(e->id, map->members);
77 }
78
79 static inline int
80 bitmap_port_do_del(const struct bitmap_port_adt_elem *e,
81                    struct bitmap_port *map)
82 {
83         return !test_and_clear_bit(e->id, map->members);
84 }
85
86 static inline int
87 bitmap_port_do_list(struct sk_buff *skb, const struct bitmap_port *map, u32 id,
88                     size_t dsize)
89 {
90         return nla_put_net16(skb, IPSET_ATTR_PORT,
91                              htons(map->first_port + id));
92 }
93
94 static inline int
95 bitmap_port_do_head(struct sk_buff *skb, const struct bitmap_port *map)
96 {
97         return nla_put_net16(skb, IPSET_ATTR_PORT, htons(map->first_port)) ||
98                nla_put_net16(skb, IPSET_ATTR_PORT_TO, htons(map->last_port));
99 }
100
101 static int
102 bitmap_port_kadt(struct ip_set *set, const struct sk_buff *skb,
103                  const struct xt_action_param *par,
104                  enum ipset_adt adt, struct ip_set_adt_opt *opt)
105 {
106         struct bitmap_port *map = set->data;
107         ipset_adtfn adtfn = set->variant->adt[adt];
108         struct bitmap_port_adt_elem e = { .id = 0 };
109         struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
110         __be16 __port;
111         u16 port = 0;
112
113         if (!ip_set_get_ip_port(skb, opt->family,
114                                 opt->flags & IPSET_DIM_ONE_SRC, &__port))
115                 return -EINVAL;
116
117         port = ntohs(__port);
118
119         if (port < map->first_port || port > map->last_port)
120                 return -IPSET_ERR_BITMAP_RANGE;
121
122         e.id = port_to_id(map, port);
123
124         return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
125 }
126
127 static int
128 bitmap_port_uadt(struct ip_set *set, struct nlattr *tb[],
129                  enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
130 {
131         struct bitmap_port *map = set->data;
132         ipset_adtfn adtfn = set->variant->adt[adt];
133         struct bitmap_port_adt_elem e = { .id = 0 };
134         struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
135         u32 port;       /* wraparound */
136         u16 port_to;
137         int ret = 0;
138
139         if (unlikely(!ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
140                      !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
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;
148
149         if (tb[IPSET_ATTR_LINENO])
150                 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
151
152         port = ip_set_get_h16(tb[IPSET_ATTR_PORT]);
153         if (port < map->first_port || port > map->last_port)
154                 return -IPSET_ERR_BITMAP_RANGE;
155         ret = ip_set_get_extensions(set, tb, &ext);
156         if (ret)
157                 return ret;
158
159         if (adt == IPSET_TEST) {
160                 e.id = port_to_id(map, port);
161                 return adtfn(set, &e, &ext, &ext, flags);
162         }
163
164         if (tb[IPSET_ATTR_PORT_TO]) {
165                 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
166                 if (port > port_to) {
167                         swap(port, port_to);
168                         if (port < map->first_port)
169                                 return -IPSET_ERR_BITMAP_RANGE;
170                 }
171         } else
172                 port_to = port;
173
174         if (port_to > map->last_port)
175                 return -IPSET_ERR_BITMAP_RANGE;
176
177         for (; port <= port_to; port++) {
178                 e.id = port_to_id(map, port);
179                 ret = adtfn(set, &e, &ext, &ext, flags);
180
181                 if (ret && !ip_set_eexist(ret, flags))
182                         return ret;
183                 else
184                         ret = 0;
185         }
186         return ret;
187 }
188
189 static bool
190 bitmap_port_same_set(const struct ip_set *a, const struct ip_set *b)
191 {
192         const struct bitmap_port *x = a->data;
193         const struct bitmap_port *y = b->data;
194
195         return x->first_port == y->first_port &&
196                x->last_port == y->last_port &&
197                a->timeout == b->timeout &&
198                a->extensions == b->extensions;
199 }
200
201 /* Plain variant */
202
203 struct bitmap_port_elem {
204 };
205
206 #include "ip_set_bitmap_gen.h"
207
208 /* Create bitmap:ip type of sets */
209
210 static bool
211 init_map_port(struct ip_set *set, struct bitmap_port *map,
212               u16 first_port, u16 last_port)
213 {
214         map->members = ip_set_alloc(map->memsize);
215         if (!map->members)
216                 return false;
217         if (set->dsize) {
218                 map->extensions = ip_set_alloc(set->dsize * map->elements);
219                 if (!map->extensions) {
220                         kfree(map->members);
221                         return false;
222                 }
223         }
224         map->first_port = first_port;
225         map->last_port = last_port;
226         set->timeout = IPSET_NO_TIMEOUT;
227
228         set->data = map;
229         set->family = NFPROTO_UNSPEC;
230
231         return true;
232 }
233
234 static int
235 bitmap_port_create(struct net *net, struct ip_set *set, struct nlattr *tb[],
236                    u32 flags)
237 {
238         struct bitmap_port *map;
239         u16 first_port, last_port;
240
241         if (unlikely(!ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
242                      !ip_set_attr_netorder(tb, IPSET_ATTR_PORT_TO) ||
243                      !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
244                      !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
245                 return -IPSET_ERR_PROTOCOL;
246
247         first_port = ip_set_get_h16(tb[IPSET_ATTR_PORT]);
248         last_port = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
249         if (first_port > last_port) {
250                 u16 tmp = first_port;
251
252                 first_port = last_port;
253                 last_port = tmp;
254         }
255
256         map = kzalloc(sizeof(*map), GFP_KERNEL);
257         if (!map)
258                 return -ENOMEM;
259
260         map->elements = last_port - first_port + 1;
261         map->memsize = bitmap_bytes(0, map->elements);
262         set->variant = &bitmap_port;
263         set->dsize = ip_set_elem_len(set, tb, 0);
264         if (!init_map_port(set, map, first_port, last_port)) {
265                 kfree(map);
266                 return -ENOMEM;
267         }
268         if (tb[IPSET_ATTR_TIMEOUT]) {
269                 set->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
270                 bitmap_port_gc_init(set, bitmap_port_gc);
271         }
272         return 0;
273 }
274
275 static struct ip_set_type bitmap_port_type = {
276         .name           = "bitmap:port",
277         .protocol       = IPSET_PROTOCOL,
278         .features       = IPSET_TYPE_PORT,
279         .dimension      = IPSET_DIM_ONE,
280         .family         = NFPROTO_UNSPEC,
281         .revision_min   = IPSET_TYPE_REV_MIN,
282         .revision_max   = IPSET_TYPE_REV_MAX,
283         .create         = bitmap_port_create,
284         .create_policy  = {
285                 [IPSET_ATTR_PORT]       = { .type = NLA_U16 },
286                 [IPSET_ATTR_PORT_TO]    = { .type = NLA_U16 },
287                 [IPSET_ATTR_TIMEOUT]    = { .type = NLA_U32 },
288                 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
289         },
290         .adt_policy     = {
291                 [IPSET_ATTR_PORT]       = { .type = NLA_U16 },
292                 [IPSET_ATTR_PORT_TO]    = { .type = NLA_U16 },
293                 [IPSET_ATTR_TIMEOUT]    = { .type = NLA_U32 },
294                 [IPSET_ATTR_LINENO]     = { .type = NLA_U32 },
295                 [IPSET_ATTR_BYTES]      = { .type = NLA_U64 },
296                 [IPSET_ATTR_PACKETS]    = { .type = NLA_U64 },
297                 [IPSET_ATTR_COMMENT]    = { .type = NLA_NUL_STRING },
298                 [IPSET_ATTR_SKBMARK]    = { .type = NLA_U64 },
299                 [IPSET_ATTR_SKBPRIO]    = { .type = NLA_U32 },
300                 [IPSET_ATTR_SKBQUEUE]   = { .type = NLA_U16 },
301         },
302         .me             = THIS_MODULE,
303 };
304
305 static int __init
306 bitmap_port_init(void)
307 {
308         return ip_set_type_register(&bitmap_port_type);
309 }
310
311 static void __exit
312 bitmap_port_fini(void)
313 {
314         ip_set_type_unregister(&bitmap_port_type);
315 }
316
317 module_init(bitmap_port_init);
318 module_exit(bitmap_port_fini);