]> git.karo-electronics.de Git - karo-tx-linux.git/blob - include/linux/netfilter/ipset/ip_set.h
netfiler: ipset: Add net namespace for ipset
[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_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 /* register and unregister set references */
320 extern ip_set_id_t ip_set_get_byname(struct net *net,
321                                      const char *name, struct ip_set **set);
322 extern void ip_set_put_byindex(struct net *net, ip_set_id_t index);
323 extern const char *ip_set_name_byindex(struct net *net, ip_set_id_t index);
324 extern ip_set_id_t ip_set_nfnl_get(struct net *net, const char *name);
325 extern ip_set_id_t ip_set_nfnl_get_byindex(struct net *net, ip_set_id_t index);
326 extern void ip_set_nfnl_put(struct net *net, ip_set_id_t index);
327
328 /* API for iptables set match, and SET target */
329
330 extern int ip_set_add(ip_set_id_t id, const struct sk_buff *skb,
331                       const struct xt_action_param *par,
332                       struct ip_set_adt_opt *opt);
333 extern int ip_set_del(ip_set_id_t id, const struct sk_buff *skb,
334                       const struct xt_action_param *par,
335                       struct ip_set_adt_opt *opt);
336 extern int ip_set_test(ip_set_id_t id, const struct sk_buff *skb,
337                        const struct xt_action_param *par,
338                        struct ip_set_adt_opt *opt);
339
340 /* Utility functions */
341 extern void *ip_set_alloc(size_t size);
342 extern void ip_set_free(void *members);
343 extern int ip_set_get_ipaddr4(struct nlattr *nla,  __be32 *ipaddr);
344 extern int ip_set_get_ipaddr6(struct nlattr *nla, union nf_inet_addr *ipaddr);
345 extern size_t ip_set_elem_len(struct ip_set *set, struct nlattr *tb[],
346                               size_t len);
347 extern int ip_set_get_extensions(struct ip_set *set, struct nlattr *tb[],
348                                  struct ip_set_ext *ext);
349
350 static inline int
351 ip_set_get_hostipaddr4(struct nlattr *nla, u32 *ipaddr)
352 {
353         __be32 ip;
354         int ret = ip_set_get_ipaddr4(nla, &ip);
355
356         if (ret)
357                 return ret;
358         *ipaddr = ntohl(ip);
359         return 0;
360 }
361
362 /* Ignore IPSET_ERR_EXIST errors if asked to do so? */
363 static inline bool
364 ip_set_eexist(int ret, u32 flags)
365 {
366         return ret == -IPSET_ERR_EXIST && (flags & IPSET_FLAG_EXIST);
367 }
368
369 /* Match elements marked with nomatch */
370 static inline bool
371 ip_set_enomatch(int ret, u32 flags, enum ipset_adt adt, struct ip_set *set)
372 {
373         return adt == IPSET_TEST &&
374                (set->type->features & IPSET_TYPE_NOMATCH) &&
375                ((flags >> 16) & IPSET_FLAG_NOMATCH) &&
376                (ret > 0 || ret == -ENOTEMPTY);
377 }
378
379 /* Check the NLA_F_NET_BYTEORDER flag */
380 static inline bool
381 ip_set_attr_netorder(struct nlattr *tb[], int type)
382 {
383         return tb[type] && (tb[type]->nla_type & NLA_F_NET_BYTEORDER);
384 }
385
386 static inline bool
387 ip_set_optattr_netorder(struct nlattr *tb[], int type)
388 {
389         return !tb[type] || (tb[type]->nla_type & NLA_F_NET_BYTEORDER);
390 }
391
392 /* Useful converters */
393 static inline u32
394 ip_set_get_h32(const struct nlattr *attr)
395 {
396         return ntohl(nla_get_be32(attr));
397 }
398
399 static inline u16
400 ip_set_get_h16(const struct nlattr *attr)
401 {
402         return ntohs(nla_get_be16(attr));
403 }
404
405 #define ipset_nest_start(skb, attr) nla_nest_start(skb, attr | NLA_F_NESTED)
406 #define ipset_nest_end(skb, start)  nla_nest_end(skb, start)
407
408 static inline int nla_put_ipaddr4(struct sk_buff *skb, int type, __be32 ipaddr)
409 {
410         struct nlattr *__nested = ipset_nest_start(skb, type);
411         int ret;
412
413         if (!__nested)
414                 return -EMSGSIZE;
415         ret = nla_put_net32(skb, IPSET_ATTR_IPADDR_IPV4, ipaddr);
416         if (!ret)
417                 ipset_nest_end(skb, __nested);
418         return ret;
419 }
420
421 static inline int nla_put_ipaddr6(struct sk_buff *skb, int type,
422                                   const struct in6_addr *ipaddrptr)
423 {
424         struct nlattr *__nested = ipset_nest_start(skb, type);
425         int ret;
426
427         if (!__nested)
428                 return -EMSGSIZE;
429         ret = nla_put(skb, IPSET_ATTR_IPADDR_IPV6,
430                       sizeof(struct in6_addr), ipaddrptr);
431         if (!ret)
432                 ipset_nest_end(skb, __nested);
433         return ret;
434 }
435
436 /* Get address from skbuff */
437 static inline __be32
438 ip4addr(const struct sk_buff *skb, bool src)
439 {
440         return src ? ip_hdr(skb)->saddr : ip_hdr(skb)->daddr;
441 }
442
443 static inline void
444 ip4addrptr(const struct sk_buff *skb, bool src, __be32 *addr)
445 {
446         *addr = src ? ip_hdr(skb)->saddr : ip_hdr(skb)->daddr;
447 }
448
449 static inline void
450 ip6addrptr(const struct sk_buff *skb, bool src, struct in6_addr *addr)
451 {
452         memcpy(addr, src ? &ipv6_hdr(skb)->saddr : &ipv6_hdr(skb)->daddr,
453                sizeof(*addr));
454 }
455
456 /* Calculate the bytes required to store the inclusive range of a-b */
457 static inline int
458 bitmap_bytes(u32 a, u32 b)
459 {
460         return 4 * ((((b - a + 8) / 8) + 3) / 4);
461 }
462
463 #include <linux/netfilter/ipset/ip_set_timeout.h>
464 #include <linux/netfilter/ipset/ip_set_comment.h>
465
466 static inline int
467 ip_set_put_extensions(struct sk_buff *skb, const struct ip_set *set,
468                       const void *e, bool active)
469 {
470         if (SET_WITH_TIMEOUT(set)) {
471                 unsigned long *timeout = ext_timeout(e, set);
472
473                 if (nla_put_net32(skb, IPSET_ATTR_TIMEOUT,
474                         htonl(active ? ip_set_timeout_get(timeout)
475                                 : *timeout)))
476                         return -EMSGSIZE;
477         }
478         if (SET_WITH_COUNTER(set) &&
479             ip_set_put_counter(skb, ext_counter(e, set)))
480                 return -EMSGSIZE;
481         if (SET_WITH_COMMENT(set) &&
482             ip_set_put_comment(skb, ext_comment(e, set)))
483                 return -EMSGSIZE;
484         return 0;
485 }
486
487 #define IP_SET_INIT_KEXT(skb, opt, set)                 \
488         { .bytes = (skb)->len, .packets = 1,            \
489           .timeout = ip_set_adt_opt_timeout(opt, set) }
490
491 #define IP_SET_INIT_UEXT(set)                           \
492         { .bytes = ULLONG_MAX, .packets = ULLONG_MAX,   \
493           .timeout = (set)->timeout }
494
495 #define IP_SET_INIT_CIDR(a, b) ((a) ? (a) : (b))
496
497 #define IPSET_CONCAT(a, b)              a##b
498 #define IPSET_TOKEN(a, b)               IPSET_CONCAT(a, b)
499
500 #endif /*_IP_SET_H */