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