]> git.karo-electronics.de Git - karo-tx-linux.git/blob - include/linux/netfilter/ipset/ip_set.h
Merge branch 'perf-core-for-mingo' into perf/urgent
[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_MARK_FLAG = 6,
43         IPSET_TYPE_MARK = (1 << IPSET_TYPE_MARK_FLAG),
44         IPSET_TYPE_NOMATCH_FLAG = 7,
45         IPSET_TYPE_NOMATCH = (1 << IPSET_TYPE_NOMATCH_FLAG),
46         /* Strictly speaking not a feature, but a flag for dumping:
47          * this settype must be dumped last */
48         IPSET_DUMP_LAST_FLAG = 8,
49         IPSET_DUMP_LAST = (1 << IPSET_DUMP_LAST_FLAG),
50 };
51
52 /* Set extensions */
53 enum ip_set_extension {
54         IPSET_EXT_BIT_TIMEOUT = 0,
55         IPSET_EXT_TIMEOUT = (1 << IPSET_EXT_BIT_TIMEOUT),
56         IPSET_EXT_BIT_COUNTER = 1,
57         IPSET_EXT_COUNTER = (1 << IPSET_EXT_BIT_COUNTER),
58         IPSET_EXT_BIT_COMMENT = 2,
59         IPSET_EXT_COMMENT = (1 << IPSET_EXT_BIT_COMMENT),
60         /* Mark set with an extension which needs to call destroy */
61         IPSET_EXT_BIT_DESTROY = 7,
62         IPSET_EXT_DESTROY = (1 << IPSET_EXT_BIT_DESTROY),
63 };
64
65 #define SET_WITH_TIMEOUT(s)     ((s)->extensions & IPSET_EXT_TIMEOUT)
66 #define SET_WITH_COUNTER(s)     ((s)->extensions & IPSET_EXT_COUNTER)
67 #define SET_WITH_COMMENT(s)     ((s)->extensions & IPSET_EXT_COMMENT)
68 #define SET_WITH_FORCEADD(s)    ((s)->flags & IPSET_CREATE_FLAG_FORCEADD)
69
70 /* Extension id, in size order */
71 enum ip_set_ext_id {
72         IPSET_EXT_ID_COUNTER = 0,
73         IPSET_EXT_ID_TIMEOUT,
74         IPSET_EXT_ID_COMMENT,
75         IPSET_EXT_ID_MAX,
76 };
77
78 /* Extension type */
79 struct ip_set_ext_type {
80         /* Destroy extension private data (can be NULL) */
81         void (*destroy)(void *ext);
82         enum ip_set_extension type;
83         enum ipset_cadt_flags flag;
84         /* Size and minimal alignment */
85         u8 len;
86         u8 align;
87 };
88
89 extern const struct ip_set_ext_type ip_set_extensions[];
90
91 struct ip_set_ext {
92         u64 packets;
93         u64 bytes;
94         u32 timeout;
95         char *comment;
96 };
97
98 struct ip_set_counter {
99         atomic64_t bytes;
100         atomic64_t packets;
101 };
102
103 struct ip_set_comment {
104         char *str;
105 };
106
107 struct ip_set;
108
109 #define ext_timeout(e, s)       \
110 (unsigned long *)(((void *)(e)) + (s)->offset[IPSET_EXT_ID_TIMEOUT])
111 #define ext_counter(e, s)       \
112 (struct ip_set_counter *)(((void *)(e)) + (s)->offset[IPSET_EXT_ID_COUNTER])
113 #define ext_comment(e, s)       \
114 (struct ip_set_comment *)(((void *)(e)) + (s)->offset[IPSET_EXT_ID_COMMENT])
115
116
117 typedef int (*ipset_adtfn)(struct ip_set *set, void *value,
118                            const struct ip_set_ext *ext,
119                            struct ip_set_ext *mext, u32 cmdflags);
120
121 /* Kernel API function options */
122 struct ip_set_adt_opt {
123         u8 family;              /* Actual protocol family */
124         u8 dim;                 /* Dimension of match/target */
125         u8 flags;               /* Direction and negation flags */
126         u32 cmdflags;           /* Command-like flags */
127         struct ip_set_ext ext;  /* Extensions */
128 };
129
130 /* Set type, variant-specific part */
131 struct ip_set_type_variant {
132         /* Kernelspace: test/add/del entries
133          *              returns negative error code,
134          *                      zero for no match/success to add/delete
135          *                      positive for matching element */
136         int (*kadt)(struct ip_set *set, const struct sk_buff *skb,
137                     const struct xt_action_param *par,
138                     enum ipset_adt adt, struct ip_set_adt_opt *opt);
139
140         /* Userspace: test/add/del entries
141          *              returns negative error code,
142          *                      zero for no match/success to add/delete
143          *                      positive for matching element */
144         int (*uadt)(struct ip_set *set, struct nlattr *tb[],
145                     enum ipset_adt adt, u32 *lineno, u32 flags, bool retried);
146
147         /* Low level add/del/test functions */
148         ipset_adtfn adt[IPSET_ADT_MAX];
149
150         /* When adding entries and set is full, try to resize the set */
151         int (*resize)(struct ip_set *set, bool retried);
152         /* Destroy the set */
153         void (*destroy)(struct ip_set *set);
154         /* Flush the elements */
155         void (*flush)(struct ip_set *set);
156         /* Expire entries before listing */
157         void (*expire)(struct ip_set *set);
158         /* List set header data */
159         int (*head)(struct ip_set *set, struct sk_buff *skb);
160         /* List elements */
161         int (*list)(const struct ip_set *set, struct sk_buff *skb,
162                     struct netlink_callback *cb);
163
164         /* Return true if "b" set is the same as "a"
165          * according to the create set parameters */
166         bool (*same_set)(const struct ip_set *a, const struct ip_set *b);
167 };
168
169 /* The core set type structure */
170 struct ip_set_type {
171         struct list_head list;
172
173         /* Typename */
174         char name[IPSET_MAXNAMELEN];
175         /* Protocol version */
176         u8 protocol;
177         /* Set type dimension */
178         u8 dimension;
179         /*
180          * Supported family: may be NFPROTO_UNSPEC for both
181          * NFPROTO_IPV4/NFPROTO_IPV6.
182          */
183         u8 family;
184         /* Type revisions */
185         u8 revision_min, revision_max;
186         /* Set features to control swapping */
187         u16 features;
188
189         /* Create set */
190         int (*create)(struct net *net, struct ip_set *set,
191                       struct nlattr *tb[], u32 flags);
192
193         /* Attribute policies */
194         const struct nla_policy create_policy[IPSET_ATTR_CREATE_MAX + 1];
195         const struct nla_policy adt_policy[IPSET_ATTR_ADT_MAX + 1];
196
197         /* Set this to THIS_MODULE if you are a module, otherwise NULL */
198         struct module *me;
199 };
200
201 /* register and unregister set type */
202 extern int ip_set_type_register(struct ip_set_type *set_type);
203 extern void ip_set_type_unregister(struct ip_set_type *set_type);
204
205 /* A generic IP set */
206 struct ip_set {
207         /* The name of the set */
208         char name[IPSET_MAXNAMELEN];
209         /* Lock protecting the set data */
210         rwlock_t lock;
211         /* References to the set */
212         u32 ref;
213         /* The core set type */
214         struct ip_set_type *type;
215         /* The type variant doing the real job */
216         const struct ip_set_type_variant *variant;
217         /* The actual INET family of the set */
218         u8 family;
219         /* The type revision */
220         u8 revision;
221         /* Extensions */
222         u8 extensions;
223         /* Create flags */
224         u8 flags;
225         /* Default timeout value, if enabled */
226         u32 timeout;
227         /* Element data size */
228         size_t dsize;
229         /* Offsets to extensions in elements */
230         size_t offset[IPSET_EXT_ID_MAX];
231         /* The type specific data */
232         void *data;
233 };
234
235 static inline void
236 ip_set_ext_destroy(struct ip_set *set, void *data)
237 {
238         /* Check that the extension is enabled for the set and
239          * call it's destroy function for its extension part in data.
240          */
241         if (SET_WITH_COMMENT(set))
242                 ip_set_extensions[IPSET_EXT_ID_COMMENT].destroy(
243                         ext_comment(data, set));
244 }
245
246 static inline int
247 ip_set_put_flags(struct sk_buff *skb, struct ip_set *set)
248 {
249         u32 cadt_flags = 0;
250
251         if (SET_WITH_TIMEOUT(set))
252                 if (unlikely(nla_put_net32(skb, IPSET_ATTR_TIMEOUT,
253                                            htonl(set->timeout))))
254                         return -EMSGSIZE;
255         if (SET_WITH_COUNTER(set))
256                 cadt_flags |= IPSET_FLAG_WITH_COUNTERS;
257         if (SET_WITH_COMMENT(set))
258                 cadt_flags |= IPSET_FLAG_WITH_COMMENT;
259         if (SET_WITH_FORCEADD(set))
260                 cadt_flags |= IPSET_FLAG_WITH_FORCEADD;
261
262         if (!cadt_flags)
263                 return 0;
264         return nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(cadt_flags));
265 }
266
267 static inline void
268 ip_set_add_bytes(u64 bytes, struct ip_set_counter *counter)
269 {
270         atomic64_add((long long)bytes, &(counter)->bytes);
271 }
272
273 static inline void
274 ip_set_add_packets(u64 packets, struct ip_set_counter *counter)
275 {
276         atomic64_add((long long)packets, &(counter)->packets);
277 }
278
279 static inline u64
280 ip_set_get_bytes(const struct ip_set_counter *counter)
281 {
282         return (u64)atomic64_read(&(counter)->bytes);
283 }
284
285 static inline u64
286 ip_set_get_packets(const struct ip_set_counter *counter)
287 {
288         return (u64)atomic64_read(&(counter)->packets);
289 }
290
291 static inline void
292 ip_set_update_counter(struct ip_set_counter *counter,
293                       const struct ip_set_ext *ext,
294                       struct ip_set_ext *mext, u32 flags)
295 {
296         if (ext->packets != ULLONG_MAX &&
297             !(flags & IPSET_FLAG_SKIP_COUNTER_UPDATE)) {
298                 ip_set_add_bytes(ext->bytes, counter);
299                 ip_set_add_packets(ext->packets, counter);
300         }
301         if (flags & IPSET_FLAG_MATCH_COUNTERS) {
302                 mext->packets = ip_set_get_packets(counter);
303                 mext->bytes = ip_set_get_bytes(counter);
304         }
305 }
306
307 static inline bool
308 ip_set_put_counter(struct sk_buff *skb, struct ip_set_counter *counter)
309 {
310         return nla_put_net64(skb, IPSET_ATTR_BYTES,
311                              cpu_to_be64(ip_set_get_bytes(counter))) ||
312                nla_put_net64(skb, IPSET_ATTR_PACKETS,
313                              cpu_to_be64(ip_set_get_packets(counter)));
314 }
315
316 static inline void
317 ip_set_init_counter(struct ip_set_counter *counter,
318                     const struct ip_set_ext *ext)
319 {
320         if (ext->bytes != ULLONG_MAX)
321                 atomic64_set(&(counter)->bytes, (long long)(ext->bytes));
322         if (ext->packets != ULLONG_MAX)
323                 atomic64_set(&(counter)->packets, (long long)(ext->packets));
324 }
325
326 /* Netlink CB args */
327 enum {
328         IPSET_CB_NET = 0,
329         IPSET_CB_DUMP,
330         IPSET_CB_INDEX,
331         IPSET_CB_ARG0,
332         IPSET_CB_ARG1,
333         IPSET_CB_ARG2,
334 };
335
336 /* register and unregister set references */
337 extern ip_set_id_t ip_set_get_byname(struct net *net,
338                                      const char *name, struct ip_set **set);
339 extern void ip_set_put_byindex(struct net *net, ip_set_id_t index);
340 extern const char *ip_set_name_byindex(struct net *net, ip_set_id_t index);
341 extern ip_set_id_t ip_set_nfnl_get_byindex(struct net *net, ip_set_id_t index);
342 extern void ip_set_nfnl_put(struct net *net, ip_set_id_t index);
343
344 /* API for iptables set match, and SET target */
345
346 extern int ip_set_add(ip_set_id_t id, const struct sk_buff *skb,
347                       const struct xt_action_param *par,
348                       struct ip_set_adt_opt *opt);
349 extern int ip_set_del(ip_set_id_t id, const struct sk_buff *skb,
350                       const struct xt_action_param *par,
351                       struct ip_set_adt_opt *opt);
352 extern int ip_set_test(ip_set_id_t id, const struct sk_buff *skb,
353                        const struct xt_action_param *par,
354                        struct ip_set_adt_opt *opt);
355
356 /* Utility functions */
357 extern void *ip_set_alloc(size_t size);
358 extern void ip_set_free(void *members);
359 extern int ip_set_get_ipaddr4(struct nlattr *nla,  __be32 *ipaddr);
360 extern int ip_set_get_ipaddr6(struct nlattr *nla, union nf_inet_addr *ipaddr);
361 extern size_t ip_set_elem_len(struct ip_set *set, struct nlattr *tb[],
362                               size_t len);
363 extern int ip_set_get_extensions(struct ip_set *set, struct nlattr *tb[],
364                                  struct ip_set_ext *ext);
365
366 static inline int
367 ip_set_get_hostipaddr4(struct nlattr *nla, u32 *ipaddr)
368 {
369         __be32 ip;
370         int ret = ip_set_get_ipaddr4(nla, &ip);
371
372         if (ret)
373                 return ret;
374         *ipaddr = ntohl(ip);
375         return 0;
376 }
377
378 /* Ignore IPSET_ERR_EXIST errors if asked to do so? */
379 static inline bool
380 ip_set_eexist(int ret, u32 flags)
381 {
382         return ret == -IPSET_ERR_EXIST && (flags & IPSET_FLAG_EXIST);
383 }
384
385 /* Match elements marked with nomatch */
386 static inline bool
387 ip_set_enomatch(int ret, u32 flags, enum ipset_adt adt, struct ip_set *set)
388 {
389         return adt == IPSET_TEST &&
390                (set->type->features & IPSET_TYPE_NOMATCH) &&
391                ((flags >> 16) & IPSET_FLAG_NOMATCH) &&
392                (ret > 0 || ret == -ENOTEMPTY);
393 }
394
395 /* Check the NLA_F_NET_BYTEORDER flag */
396 static inline bool
397 ip_set_attr_netorder(struct nlattr *tb[], int type)
398 {
399         return tb[type] && (tb[type]->nla_type & NLA_F_NET_BYTEORDER);
400 }
401
402 static inline bool
403 ip_set_optattr_netorder(struct nlattr *tb[], int type)
404 {
405         return !tb[type] || (tb[type]->nla_type & NLA_F_NET_BYTEORDER);
406 }
407
408 /* Useful converters */
409 static inline u32
410 ip_set_get_h32(const struct nlattr *attr)
411 {
412         return ntohl(nla_get_be32(attr));
413 }
414
415 static inline u16
416 ip_set_get_h16(const struct nlattr *attr)
417 {
418         return ntohs(nla_get_be16(attr));
419 }
420
421 #define ipset_nest_start(skb, attr) nla_nest_start(skb, attr | NLA_F_NESTED)
422 #define ipset_nest_end(skb, start)  nla_nest_end(skb, start)
423
424 static inline int nla_put_ipaddr4(struct sk_buff *skb, int type, __be32 ipaddr)
425 {
426         struct nlattr *__nested = ipset_nest_start(skb, type);
427         int ret;
428
429         if (!__nested)
430                 return -EMSGSIZE;
431         ret = nla_put_net32(skb, IPSET_ATTR_IPADDR_IPV4, ipaddr);
432         if (!ret)
433                 ipset_nest_end(skb, __nested);
434         return ret;
435 }
436
437 static inline int nla_put_ipaddr6(struct sk_buff *skb, int type,
438                                   const struct in6_addr *ipaddrptr)
439 {
440         struct nlattr *__nested = ipset_nest_start(skb, type);
441         int ret;
442
443         if (!__nested)
444                 return -EMSGSIZE;
445         ret = nla_put(skb, IPSET_ATTR_IPADDR_IPV6,
446                       sizeof(struct in6_addr), ipaddrptr);
447         if (!ret)
448                 ipset_nest_end(skb, __nested);
449         return ret;
450 }
451
452 /* Get address from skbuff */
453 static inline __be32
454 ip4addr(const struct sk_buff *skb, bool src)
455 {
456         return src ? ip_hdr(skb)->saddr : ip_hdr(skb)->daddr;
457 }
458
459 static inline void
460 ip4addrptr(const struct sk_buff *skb, bool src, __be32 *addr)
461 {
462         *addr = src ? ip_hdr(skb)->saddr : ip_hdr(skb)->daddr;
463 }
464
465 static inline void
466 ip6addrptr(const struct sk_buff *skb, bool src, struct in6_addr *addr)
467 {
468         memcpy(addr, src ? &ipv6_hdr(skb)->saddr : &ipv6_hdr(skb)->daddr,
469                sizeof(*addr));
470 }
471
472 /* Calculate the bytes required to store the inclusive range of a-b */
473 static inline int
474 bitmap_bytes(u32 a, u32 b)
475 {
476         return 4 * ((((b - a + 8) / 8) + 3) / 4);
477 }
478
479 #include <linux/netfilter/ipset/ip_set_timeout.h>
480 #include <linux/netfilter/ipset/ip_set_comment.h>
481
482 static inline int
483 ip_set_put_extensions(struct sk_buff *skb, const struct ip_set *set,
484                       const void *e, bool active)
485 {
486         if (SET_WITH_TIMEOUT(set)) {
487                 unsigned long *timeout = ext_timeout(e, set);
488
489                 if (nla_put_net32(skb, IPSET_ATTR_TIMEOUT,
490                         htonl(active ? ip_set_timeout_get(timeout)
491                                 : *timeout)))
492                         return -EMSGSIZE;
493         }
494         if (SET_WITH_COUNTER(set) &&
495             ip_set_put_counter(skb, ext_counter(e, set)))
496                 return -EMSGSIZE;
497         if (SET_WITH_COMMENT(set) &&
498             ip_set_put_comment(skb, ext_comment(e, set)))
499                 return -EMSGSIZE;
500         return 0;
501 }
502
503 #define IP_SET_INIT_KEXT(skb, opt, set)                 \
504         { .bytes = (skb)->len, .packets = 1,            \
505           .timeout = ip_set_adt_opt_timeout(opt, set) }
506
507 #define IP_SET_INIT_UEXT(set)                           \
508         { .bytes = ULLONG_MAX, .packets = ULLONG_MAX,   \
509           .timeout = (set)->timeout }
510
511 #define IP_SET_INIT_CIDR(a, b) ((a) ? (a) : (b))
512
513 #define IPSET_CONCAT(a, b)              a##b
514 #define IPSET_TOKEN(a, b)               IPSET_CONCAT(a, b)
515
516 #endif /*_IP_SET_H */