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