]> git.karo-electronics.de Git - karo-tx-linux.git/blob - include/linux/netfilter/ipset/ip_set.h
netfilter: ipset: Generalize extensions support
[karo-tx-linux.git] / include / linux / netfilter / ipset / ip_set.h
1 /* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
2  *                         Patrick Schaaf <bof@bof.de>
3  *                         Martin Josefsson <gandalf@wlug.westbo.se>
4  * Copyright (C) 2003-2013 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10 #ifndef _IP_SET_H
11 #define _IP_SET_H
12
13 #include <linux/ip.h>
14 #include <linux/ipv6.h>
15 #include <linux/netlink.h>
16 #include <linux/netfilter.h>
17 #include <linux/netfilter/x_tables.h>
18 #include <linux/stringify.h>
19 #include <linux/vmalloc.h>
20 #include <net/netlink.h>
21 #include <uapi/linux/netfilter/ipset/ip_set.h>
22
23 #define _IP_SET_MODULE_DESC(a, b, c)            \
24         MODULE_DESCRIPTION(a " type of IP sets, revisions " b "-" c)
25 #define IP_SET_MODULE_DESC(a, b, c)             \
26         _IP_SET_MODULE_DESC(a, __stringify(b), __stringify(c))
27
28 /* Set features */
29 enum ip_set_feature {
30         IPSET_TYPE_IP_FLAG = 0,
31         IPSET_TYPE_IP = (1 << IPSET_TYPE_IP_FLAG),
32         IPSET_TYPE_PORT_FLAG = 1,
33         IPSET_TYPE_PORT = (1 << IPSET_TYPE_PORT_FLAG),
34         IPSET_TYPE_MAC_FLAG = 2,
35         IPSET_TYPE_MAC = (1 << IPSET_TYPE_MAC_FLAG),
36         IPSET_TYPE_IP2_FLAG = 3,
37         IPSET_TYPE_IP2 = (1 << IPSET_TYPE_IP2_FLAG),
38         IPSET_TYPE_NAME_FLAG = 4,
39         IPSET_TYPE_NAME = (1 << IPSET_TYPE_NAME_FLAG),
40         IPSET_TYPE_IFACE_FLAG = 5,
41         IPSET_TYPE_IFACE = (1 << IPSET_TYPE_IFACE_FLAG),
42         IPSET_TYPE_NOMATCH_FLAG = 6,
43         IPSET_TYPE_NOMATCH = (1 << IPSET_TYPE_NOMATCH_FLAG),
44         /* Strictly speaking not a feature, but a flag for dumping:
45          * this settype must be dumped last */
46         IPSET_DUMP_LAST_FLAG = 7,
47         IPSET_DUMP_LAST = (1 << IPSET_DUMP_LAST_FLAG),
48 };
49
50 /* Set extensions */
51 enum ip_set_extension {
52         IPSET_EXT_NONE = 0,
53         IPSET_EXT_BIT_TIMEOUT = 1,
54         IPSET_EXT_TIMEOUT = (1 << IPSET_EXT_BIT_TIMEOUT),
55         IPSET_EXT_BIT_COUNTER = 2,
56         IPSET_EXT_COUNTER = (1 << IPSET_EXT_BIT_COUNTER),
57 };
58
59 #define SET_WITH_TIMEOUT(s)     ((s)->extensions & IPSET_EXT_TIMEOUT)
60 #define SET_WITH_COUNTER(s)     ((s)->extensions & IPSET_EXT_COUNTER)
61
62 /* Extension id, in size order */
63 enum ip_set_ext_id {
64         IPSET_EXT_ID_COUNTER = 0,
65         IPSET_EXT_ID_TIMEOUT,
66         IPSET_EXT_ID_MAX,
67 };
68
69 /* Extension type */
70 struct ip_set_ext_type {
71         enum ip_set_extension type;
72         enum ipset_cadt_flags flag;
73         /* Size and minimal alignment */
74         u8 len;
75         u8 align;
76 };
77
78 extern const struct ip_set_ext_type ip_set_extensions[];
79
80 struct ip_set_ext {
81         u64 packets;
82         u64 bytes;
83         u32 timeout;
84 };
85
86 struct ip_set_counter {
87         atomic64_t bytes;
88         atomic64_t packets;
89 };
90
91 #define ext_timeout(e, s)       \
92 (unsigned long *)(((void *)(e)) + (s)->offset[IPSET_EXT_ID_TIMEOUT])
93 #define ext_counter(e, s)       \
94 (struct ip_set_counter *)(((void *)(e)) + (s)->offset[IPSET_EXT_ID_COUNTER])
95
96 struct ip_set;
97
98 typedef int (*ipset_adtfn)(struct ip_set *set, void *value,
99                            const struct ip_set_ext *ext,
100                            struct ip_set_ext *mext, u32 cmdflags);
101
102 /* Kernel API function options */
103 struct ip_set_adt_opt {
104         u8 family;              /* Actual protocol family */
105         u8 dim;                 /* Dimension of match/target */
106         u8 flags;               /* Direction and negation flags */
107         u32 cmdflags;           /* Command-like flags */
108         struct ip_set_ext ext;  /* Extensions */
109 };
110
111 /* Set type, variant-specific part */
112 struct ip_set_type_variant {
113         /* Kernelspace: test/add/del entries
114          *              returns negative error code,
115          *                      zero for no match/success to add/delete
116          *                      positive for matching element */
117         int (*kadt)(struct ip_set *set, const struct sk_buff *skb,
118                     const struct xt_action_param *par,
119                     enum ipset_adt adt, struct ip_set_adt_opt *opt);
120
121         /* Userspace: test/add/del entries
122          *              returns negative error code,
123          *                      zero for no match/success to add/delete
124          *                      positive for matching element */
125         int (*uadt)(struct ip_set *set, struct nlattr *tb[],
126                     enum ipset_adt adt, u32 *lineno, u32 flags, bool retried);
127
128         /* Low level add/del/test functions */
129         ipset_adtfn adt[IPSET_ADT_MAX];
130
131         /* When adding entries and set is full, try to resize the set */
132         int (*resize)(struct ip_set *set, bool retried);
133         /* Destroy the set */
134         void (*destroy)(struct ip_set *set);
135         /* Flush the elements */
136         void (*flush)(struct ip_set *set);
137         /* Expire entries before listing */
138         void (*expire)(struct ip_set *set);
139         /* List set header data */
140         int (*head)(struct ip_set *set, struct sk_buff *skb);
141         /* List elements */
142         int (*list)(const struct ip_set *set, struct sk_buff *skb,
143                     struct netlink_callback *cb);
144
145         /* Return true if "b" set is the same as "a"
146          * according to the create set parameters */
147         bool (*same_set)(const struct ip_set *a, const struct ip_set *b);
148 };
149
150 /* The core set type structure */
151 struct ip_set_type {
152         struct list_head list;
153
154         /* Typename */
155         char name[IPSET_MAXNAMELEN];
156         /* Protocol version */
157         u8 protocol;
158         /* Set features to control swapping */
159         u8 features;
160         /* Set type dimension */
161         u8 dimension;
162         /*
163          * Supported family: may be NFPROTO_UNSPEC for both
164          * NFPROTO_IPV4/NFPROTO_IPV6.
165          */
166         u8 family;
167         /* Type revisions */
168         u8 revision_min, revision_max;
169
170         /* Create set */
171         int (*create)(struct ip_set *set, struct nlattr *tb[], u32 flags);
172
173         /* Attribute policies */
174         const struct nla_policy create_policy[IPSET_ATTR_CREATE_MAX + 1];
175         const struct nla_policy adt_policy[IPSET_ATTR_ADT_MAX + 1];
176
177         /* Set this to THIS_MODULE if you are a module, otherwise NULL */
178         struct module *me;
179 };
180
181 /* register and unregister set type */
182 extern int ip_set_type_register(struct ip_set_type *set_type);
183 extern void ip_set_type_unregister(struct ip_set_type *set_type);
184
185 /* A generic IP set */
186 struct ip_set {
187         /* The name of the set */
188         char name[IPSET_MAXNAMELEN];
189         /* Lock protecting the set data */
190         rwlock_t lock;
191         /* References to the set */
192         u32 ref;
193         /* The core set type */
194         struct ip_set_type *type;
195         /* The type variant doing the real job */
196         const struct ip_set_type_variant *variant;
197         /* The actual INET family of the set */
198         u8 family;
199         /* The type revision */
200         u8 revision;
201         /* Extensions */
202         u8 extensions;
203         /* Default timeout value, if enabled */
204         u32 timeout;
205         /* Element data size */
206         size_t dsize;
207         /* Offsets to extensions in elements */
208         size_t offset[IPSET_EXT_ID_MAX];
209         /* The type specific data */
210         void *data;
211 };
212
213 static inline void
214 ip_set_add_bytes(u64 bytes, struct ip_set_counter *counter)
215 {
216         atomic64_add((long long)bytes, &(counter)->bytes);
217 }
218
219 static inline void
220 ip_set_add_packets(u64 packets, struct ip_set_counter *counter)
221 {
222         atomic64_add((long long)packets, &(counter)->packets);
223 }
224
225 static inline u64
226 ip_set_get_bytes(const struct ip_set_counter *counter)
227 {
228         return (u64)atomic64_read(&(counter)->bytes);
229 }
230
231 static inline u64
232 ip_set_get_packets(const struct ip_set_counter *counter)
233 {
234         return (u64)atomic64_read(&(counter)->packets);
235 }
236
237 static inline void
238 ip_set_update_counter(struct ip_set_counter *counter,
239                       const struct ip_set_ext *ext,
240                       struct ip_set_ext *mext, u32 flags)
241 {
242         if (ext->packets != ULLONG_MAX &&
243             !(flags & IPSET_FLAG_SKIP_COUNTER_UPDATE)) {
244                 ip_set_add_bytes(ext->bytes, counter);
245                 ip_set_add_packets(ext->packets, counter);
246         }
247         if (flags & IPSET_FLAG_MATCH_COUNTERS) {
248                 mext->packets = ip_set_get_packets(counter);
249                 mext->bytes = ip_set_get_bytes(counter);
250         }
251 }
252
253 static inline bool
254 ip_set_put_counter(struct sk_buff *skb, struct ip_set_counter *counter)
255 {
256         return nla_put_net64(skb, IPSET_ATTR_BYTES,
257                              cpu_to_be64(ip_set_get_bytes(counter))) ||
258                nla_put_net64(skb, IPSET_ATTR_PACKETS,
259                              cpu_to_be64(ip_set_get_packets(counter)));
260 }
261
262 static inline void
263 ip_set_init_counter(struct ip_set_counter *counter,
264                     const struct ip_set_ext *ext)
265 {
266         if (ext->bytes != ULLONG_MAX)
267                 atomic64_set(&(counter)->bytes, (long long)(ext->bytes));
268         if (ext->packets != ULLONG_MAX)
269                 atomic64_set(&(counter)->packets, (long long)(ext->packets));
270 }
271
272 /* register and unregister set references */
273 extern ip_set_id_t ip_set_get_byname(const char *name, struct ip_set **set);
274 extern void ip_set_put_byindex(ip_set_id_t index);
275 extern const char *ip_set_name_byindex(ip_set_id_t index);
276 extern ip_set_id_t ip_set_nfnl_get(const char *name);
277 extern ip_set_id_t ip_set_nfnl_get_byindex(ip_set_id_t index);
278 extern void ip_set_nfnl_put(ip_set_id_t index);
279
280 /* API for iptables set match, and SET target */
281
282 extern int ip_set_add(ip_set_id_t id, const struct sk_buff *skb,
283                       const struct xt_action_param *par,
284                       struct ip_set_adt_opt *opt);
285 extern int ip_set_del(ip_set_id_t id, const struct sk_buff *skb,
286                       const struct xt_action_param *par,
287                       struct ip_set_adt_opt *opt);
288 extern int ip_set_test(ip_set_id_t id, const struct sk_buff *skb,
289                        const struct xt_action_param *par,
290                        struct ip_set_adt_opt *opt);
291
292 /* Utility functions */
293 extern void *ip_set_alloc(size_t size);
294 extern void ip_set_free(void *members);
295 extern int ip_set_get_ipaddr4(struct nlattr *nla,  __be32 *ipaddr);
296 extern int ip_set_get_ipaddr6(struct nlattr *nla, union nf_inet_addr *ipaddr);
297 extern size_t ip_set_elem_len(struct ip_set *set, struct nlattr *tb[],
298                               size_t len);
299 extern int ip_set_get_extensions(struct ip_set *set, struct nlattr *tb[],
300                                  struct ip_set_ext *ext);
301
302 static inline int
303 ip_set_get_hostipaddr4(struct nlattr *nla, u32 *ipaddr)
304 {
305         __be32 ip;
306         int ret = ip_set_get_ipaddr4(nla, &ip);
307
308         if (ret)
309                 return ret;
310         *ipaddr = ntohl(ip);
311         return 0;
312 }
313
314 /* Ignore IPSET_ERR_EXIST errors if asked to do so? */
315 static inline bool
316 ip_set_eexist(int ret, u32 flags)
317 {
318         return ret == -IPSET_ERR_EXIST && (flags & IPSET_FLAG_EXIST);
319 }
320
321 /* Match elements marked with nomatch */
322 static inline bool
323 ip_set_enomatch(int ret, u32 flags, enum ipset_adt adt, struct ip_set *set)
324 {
325         return adt == IPSET_TEST &&
326                (set->type->features & IPSET_TYPE_NOMATCH) &&
327                ((flags >> 16) & IPSET_FLAG_NOMATCH) &&
328                (ret > 0 || ret == -ENOTEMPTY);
329 }
330
331 /* Check the NLA_F_NET_BYTEORDER flag */
332 static inline bool
333 ip_set_attr_netorder(struct nlattr *tb[], int type)
334 {
335         return tb[type] && (tb[type]->nla_type & NLA_F_NET_BYTEORDER);
336 }
337
338 static inline bool
339 ip_set_optattr_netorder(struct nlattr *tb[], int type)
340 {
341         return !tb[type] || (tb[type]->nla_type & NLA_F_NET_BYTEORDER);
342 }
343
344 /* Useful converters */
345 static inline u32
346 ip_set_get_h32(const struct nlattr *attr)
347 {
348         return ntohl(nla_get_be32(attr));
349 }
350
351 static inline u16
352 ip_set_get_h16(const struct nlattr *attr)
353 {
354         return ntohs(nla_get_be16(attr));
355 }
356
357 #define ipset_nest_start(skb, attr) nla_nest_start(skb, attr | NLA_F_NESTED)
358 #define ipset_nest_end(skb, start)  nla_nest_end(skb, start)
359
360 static inline int nla_put_ipaddr4(struct sk_buff *skb, int type, __be32 ipaddr)
361 {
362         struct nlattr *__nested = ipset_nest_start(skb, type);
363         int ret;
364
365         if (!__nested)
366                 return -EMSGSIZE;
367         ret = nla_put_net32(skb, IPSET_ATTR_IPADDR_IPV4, ipaddr);
368         if (!ret)
369                 ipset_nest_end(skb, __nested);
370         return ret;
371 }
372
373 static inline int nla_put_ipaddr6(struct sk_buff *skb, int type,
374                                   const struct in6_addr *ipaddrptr)
375 {
376         struct nlattr *__nested = ipset_nest_start(skb, type);
377         int ret;
378
379         if (!__nested)
380                 return -EMSGSIZE;
381         ret = nla_put(skb, IPSET_ATTR_IPADDR_IPV6,
382                       sizeof(struct in6_addr), ipaddrptr);
383         if (!ret)
384                 ipset_nest_end(skb, __nested);
385         return ret;
386 }
387
388 /* Get address from skbuff */
389 static inline __be32
390 ip4addr(const struct sk_buff *skb, bool src)
391 {
392         return src ? ip_hdr(skb)->saddr : ip_hdr(skb)->daddr;
393 }
394
395 static inline void
396 ip4addrptr(const struct sk_buff *skb, bool src, __be32 *addr)
397 {
398         *addr = src ? ip_hdr(skb)->saddr : ip_hdr(skb)->daddr;
399 }
400
401 static inline void
402 ip6addrptr(const struct sk_buff *skb, bool src, struct in6_addr *addr)
403 {
404         memcpy(addr, src ? &ipv6_hdr(skb)->saddr : &ipv6_hdr(skb)->daddr,
405                sizeof(*addr));
406 }
407
408 /* Calculate the bytes required to store the inclusive range of a-b */
409 static inline int
410 bitmap_bytes(u32 a, u32 b)
411 {
412         return 4 * ((((b - a + 8) / 8) + 3) / 4);
413 }
414
415 #include <linux/netfilter/ipset/ip_set_timeout.h>
416
417 #define IP_SET_INIT_KEXT(skb, opt, set)                 \
418         { .bytes = (skb)->len, .packets = 1,            \
419           .timeout = ip_set_adt_opt_timeout(opt, set) }
420
421 #define IP_SET_INIT_UEXT(set)                           \
422         { .bytes = ULLONG_MAX, .packets = ULLONG_MAX,   \
423           .timeout = (set)->timeout }
424
425 #define IP_SET_INIT_CIDR(a, b) ((a) ? (a) : (b))
426
427 #define IPSET_CONCAT(a, b)              a##b
428 #define IPSET_TOKEN(a, b)               IPSET_CONCAT(a, b)
429
430 #endif /*_IP_SET_H */