]> granicus.if.org Git - ipset/blob - kernel/include/linux/netfilter/ipset/ip_set.h
netfilter: ipset: move functions to ip_set_core.c.
[ipset] / kernel / 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@netfilter.org>
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 <linux/netfilter/ipset/ip_set_compat.h>
22 #include <uapi/linux/netfilter/ipset/ip_set.h>
23
24 #define _IP_SET_MODULE_DESC(a, b, c)            \
25         MODULE_DESCRIPTION(a " type of IP sets, revisions " b "-" c)
26 #define IP_SET_MODULE_DESC(a, b, c)             \
27         _IP_SET_MODULE_DESC(a, __stringify(b), __stringify(c))
28
29 /* Set features */
30 enum ip_set_feature {
31         IPSET_TYPE_IP_FLAG = 0,
32         IPSET_TYPE_IP = (1 << IPSET_TYPE_IP_FLAG),
33         IPSET_TYPE_PORT_FLAG = 1,
34         IPSET_TYPE_PORT = (1 << IPSET_TYPE_PORT_FLAG),
35         IPSET_TYPE_MAC_FLAG = 2,
36         IPSET_TYPE_MAC = (1 << IPSET_TYPE_MAC_FLAG),
37         IPSET_TYPE_IP2_FLAG = 3,
38         IPSET_TYPE_IP2 = (1 << IPSET_TYPE_IP2_FLAG),
39         IPSET_TYPE_NAME_FLAG = 4,
40         IPSET_TYPE_NAME = (1 << IPSET_TYPE_NAME_FLAG),
41         IPSET_TYPE_IFACE_FLAG = 5,
42         IPSET_TYPE_IFACE = (1 << IPSET_TYPE_IFACE_FLAG),
43         IPSET_TYPE_MARK_FLAG = 6,
44         IPSET_TYPE_MARK = (1 << IPSET_TYPE_MARK_FLAG),
45         IPSET_TYPE_NOMATCH_FLAG = 7,
46         IPSET_TYPE_NOMATCH = (1 << IPSET_TYPE_NOMATCH_FLAG),
47         /* Strictly speaking not a feature, but a flag for dumping:
48          * this settype must be dumped last */
49         IPSET_DUMP_LAST_FLAG = 8,
50         IPSET_DUMP_LAST = (1 << IPSET_DUMP_LAST_FLAG),
51 };
52
53 /* Set extensions */
54 enum ip_set_extension {
55         IPSET_EXT_BIT_TIMEOUT = 0,
56         IPSET_EXT_TIMEOUT = (1 << IPSET_EXT_BIT_TIMEOUT),
57         IPSET_EXT_BIT_COUNTER = 1,
58         IPSET_EXT_COUNTER = (1 << IPSET_EXT_BIT_COUNTER),
59         IPSET_EXT_BIT_COMMENT = 2,
60         IPSET_EXT_COMMENT = (1 << IPSET_EXT_BIT_COMMENT),
61         IPSET_EXT_BIT_SKBINFO = 3,
62         IPSET_EXT_SKBINFO = (1 << IPSET_EXT_BIT_SKBINFO),
63         /* Mark set with an extension which needs to call destroy */
64         IPSET_EXT_BIT_DESTROY = 7,
65         IPSET_EXT_DESTROY = (1 << IPSET_EXT_BIT_DESTROY),
66 };
67
68 #define SET_WITH_TIMEOUT(s)     ((s)->extensions & IPSET_EXT_TIMEOUT)
69 #define SET_WITH_COUNTER(s)     ((s)->extensions & IPSET_EXT_COUNTER)
70 #define SET_WITH_COMMENT(s)     ((s)->extensions & IPSET_EXT_COMMENT)
71 #define SET_WITH_SKBINFO(s)     ((s)->extensions & IPSET_EXT_SKBINFO)
72 #define SET_WITH_FORCEADD(s)    ((s)->flags & IPSET_CREATE_FLAG_FORCEADD)
73
74 /* Extension id, in size order */
75 enum ip_set_ext_id {
76         IPSET_EXT_ID_COUNTER = 0,
77         IPSET_EXT_ID_TIMEOUT,
78         IPSET_EXT_ID_SKBINFO,
79         IPSET_EXT_ID_COMMENT,
80         IPSET_EXT_ID_MAX,
81 };
82
83 struct ip_set;
84
85 /* Extension type */
86 struct ip_set_ext_type {
87         /* Destroy extension private data (can be NULL) */
88         void (*destroy)(struct ip_set *set, void *ext);
89         enum ip_set_extension type;
90         enum ipset_cadt_flags flag;
91         /* Size and minimal alignment */
92         u8 len;
93         u8 align;
94 };
95
96 extern const struct ip_set_ext_type ip_set_extensions[];
97
98 struct ip_set_counter {
99         atomic64_t bytes;
100         atomic64_t packets;
101 };
102
103 struct ip_set_comment_rcu {
104         struct rcu_head rcu;
105         char str[0];
106 };
107
108 struct ip_set_comment {
109         struct ip_set_comment_rcu __rcu *c;
110 };
111
112 struct ip_set_skbinfo {
113         u32 skbmark;
114         u32 skbmarkmask;
115         u32 skbprio;
116         u16 skbqueue;
117         u16 __pad;
118 };
119
120 struct ip_set_ext {
121         struct ip_set_skbinfo skbinfo;
122         u64 packets;
123         u64 bytes;
124         char *comment;
125         u32 timeout;
126         u8 packets_op;
127         u8 bytes_op;
128 };
129
130 struct ip_set;
131
132 #define ext_timeout(e, s)       \
133 ((unsigned long *)(((void *)(e)) + (s)->offset[IPSET_EXT_ID_TIMEOUT]))
134 #define ext_counter(e, s)       \
135 ((struct ip_set_counter *)(((void *)(e)) + (s)->offset[IPSET_EXT_ID_COUNTER]))
136 #define ext_comment(e, s)       \
137 ((struct ip_set_comment *)(((void *)(e)) + (s)->offset[IPSET_EXT_ID_COMMENT]))
138 #define ext_skbinfo(e, s)       \
139 ((struct ip_set_skbinfo *)(((void *)(e)) + (s)->offset[IPSET_EXT_ID_SKBINFO]))
140
141 typedef int (*ipset_adtfn)(struct ip_set *set, void *value,
142                            const struct ip_set_ext *ext,
143                            struct ip_set_ext *mext, u32 cmdflags);
144
145 /* Kernel API function options */
146 struct ip_set_adt_opt {
147         u8 family;              /* Actual protocol family */
148         u8 dim;                 /* Dimension of match/target */
149         u8 flags;               /* Direction and negation flags */
150         u32 cmdflags;           /* Command-like flags */
151         struct ip_set_ext ext;  /* Extensions */
152 };
153
154 /* Set type, variant-specific part */
155 struct ip_set_type_variant {
156         /* Kernelspace: test/add/del entries
157          *              returns negative error code,
158          *                      zero for no match/success to add/delete
159          *                      positive for matching element */
160         int (*kadt)(struct ip_set *set, const struct sk_buff *skb,
161                     const struct xt_action_param *par,
162                     enum ipset_adt adt, struct ip_set_adt_opt *opt);
163
164         /* Userspace: test/add/del entries
165          *              returns negative error code,
166          *                      zero for no match/success to add/delete
167          *                      positive for matching element */
168         int (*uadt)(struct ip_set *set, struct nlattr *tb[],
169                     enum ipset_adt adt, u32 *lineno, u32 flags, bool retried);
170
171         /* Low level add/del/test functions */
172         ipset_adtfn adt[IPSET_ADT_MAX];
173
174         /* When adding entries and set is full, try to resize the set */
175         int (*resize)(struct ip_set *set, bool retried);
176         /* Destroy the set */
177         void (*destroy)(struct ip_set *set);
178         /* Flush the elements */
179         void (*flush)(struct ip_set *set);
180         /* Expire entries before listing */
181         void (*expire)(struct ip_set *set);
182         /* List set header data */
183         int (*head)(struct ip_set *set, struct sk_buff *skb);
184         /* List elements */
185         int (*list)(const struct ip_set *set, struct sk_buff *skb,
186                     struct netlink_callback *cb);
187         /* Keep listing private when resizing runs parallel */
188         void (*uref)(struct ip_set *set, struct netlink_callback *cb,
189                      bool start);
190
191         /* Return true if "b" set is the same as "a"
192          * according to the create set parameters */
193         bool (*same_set)(const struct ip_set *a, const struct ip_set *b);
194 };
195
196 /* The core set type structure */
197 struct ip_set_type {
198         struct list_head list;
199
200         /* Typename */
201         char name[IPSET_MAXNAMELEN];
202         /* Protocol version */
203         u8 protocol;
204         /* Set type dimension */
205         u8 dimension;
206         /*
207          * Supported family: may be NFPROTO_UNSPEC for both
208          * NFPROTO_IPV4/NFPROTO_IPV6.
209          */
210         u8 family;
211         /* Type revisions */
212         u8 revision_min, revision_max;
213         /* Set features to control swapping */
214         u16 features;
215
216         /* Create set */
217         int (*create)(struct net *net, struct ip_set *set,
218                       struct nlattr *tb[], u32 flags);
219
220         /* Attribute policies */
221         const struct nla_policy create_policy[IPSET_ATTR_CREATE_MAX + 1];
222         const struct nla_policy adt_policy[IPSET_ATTR_ADT_MAX + 1];
223
224         /* Set this to THIS_MODULE if you are a module, otherwise NULL */
225         struct module *me;
226 };
227
228 /* register and unregister set type */
229 extern int ip_set_type_register(struct ip_set_type *set_type);
230 extern void ip_set_type_unregister(struct ip_set_type *set_type);
231
232 /* A generic IP set */
233 struct ip_set {
234         /* The name of the set */
235         char name[IPSET_MAXNAMELEN];
236         /* Lock protecting the set data */
237         spinlock_t lock;
238         /* References to the set */
239         u32 ref;
240         /* References to the set for netlink events like dump,
241          * ref can be swapped out by ip_set_swap
242          */
243         u32 ref_netlink;
244         /* The core set type */
245         struct ip_set_type *type;
246         /* The type variant doing the real job */
247         const struct ip_set_type_variant *variant;
248         /* The actual INET family of the set */
249         u8 family;
250         /* The type revision */
251         u8 revision;
252         /* Extensions */
253         u8 extensions;
254         /* Create flags */
255         u8 flags;
256         /* Default timeout value, if enabled */
257         u32 timeout;
258         /* Number of elements (vs timeout) */
259         u32 elements;
260         /* Size of the dynamic extensions (vs timeout) */
261         size_t ext_size;
262         /* Element data size */
263         size_t dsize;
264         /* Offsets to extensions in elements */
265         size_t offset[IPSET_EXT_ID_MAX];
266         /* The type specific data */
267         void *data;
268 };
269
270 static inline void
271 ip_set_ext_destroy(struct ip_set *set, void *data)
272 {
273         /* Check that the extension is enabled for the set and
274          * call it's destroy function for its extension part in data.
275          */
276         if (SET_WITH_COMMENT(set)) {
277                 struct ip_set_comment *c = ext_comment(data, set);
278
279                 ip_set_extensions[IPSET_EXT_ID_COMMENT].destroy(set, c);
280         }
281 }
282
283 static inline int
284 ip_set_put_flags(struct sk_buff *skb, struct ip_set *set)
285 {
286         u32 cadt_flags = 0;
287
288         if (SET_WITH_TIMEOUT(set))
289                 if (unlikely(nla_put_net32(skb, IPSET_ATTR_TIMEOUT,
290                                            htonl(set->timeout))))
291                         return -EMSGSIZE;
292         if (SET_WITH_COUNTER(set))
293                 cadt_flags |= IPSET_FLAG_WITH_COUNTERS;
294         if (SET_WITH_COMMENT(set))
295                 cadt_flags |= IPSET_FLAG_WITH_COMMENT;
296         if (SET_WITH_SKBINFO(set))
297                 cadt_flags |= IPSET_FLAG_WITH_SKBINFO;
298         if (SET_WITH_FORCEADD(set))
299                 cadt_flags |= IPSET_FLAG_WITH_FORCEADD;
300
301         if (!cadt_flags)
302                 return 0;
303         return nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(cadt_flags));
304 }
305
306 /* Netlink CB args */
307 enum {
308         IPSET_CB_NET = 0,       /* net namespace */
309         IPSET_CB_PROTO,         /* ipset protocol */
310         IPSET_CB_DUMP,          /* dump single set/all sets */
311         IPSET_CB_INDEX,         /* set index */
312         IPSET_CB_PRIVATE,       /* set private data */
313         IPSET_CB_ARG0,          /* type specific */
314 };
315
316 /* register and unregister set references */
317 extern ip_set_id_t ip_set_get_byname(struct net *net,
318                                      const char *name, struct ip_set **set);
319 extern void ip_set_put_byindex(struct net *net, ip_set_id_t index);
320 extern void ip_set_name_byindex(struct net *net, ip_set_id_t index, char *name);
321 extern ip_set_id_t ip_set_nfnl_get_byindex(struct net *net, ip_set_id_t index);
322 extern void ip_set_nfnl_put(struct net *net, ip_set_id_t index);
323
324 /* API for iptables set match, and SET target */
325
326 extern int ip_set_add(ip_set_id_t id, const struct sk_buff *skb,
327                       const struct xt_action_param *par,
328                       struct ip_set_adt_opt *opt);
329 extern int ip_set_del(ip_set_id_t id, const struct sk_buff *skb,
330                       const struct xt_action_param *par,
331                       struct ip_set_adt_opt *opt);
332 extern int ip_set_test(ip_set_id_t id, const struct sk_buff *skb,
333                        const struct xt_action_param *par,
334                        struct ip_set_adt_opt *opt);
335
336 /* Utility functions */
337 extern void *ip_set_alloc(size_t size);
338 extern void ip_set_free(void *members);
339 extern int ip_set_get_ipaddr4(struct nlattr *nla,  __be32 *ipaddr);
340 extern int ip_set_get_ipaddr6(struct nlattr *nla, union nf_inet_addr *ipaddr);
341 extern size_t ip_set_elem_len(struct ip_set *set, struct nlattr *tb[],
342                               size_t len, size_t align);
343 extern int ip_set_get_extensions(struct ip_set *set, struct nlattr *tb[],
344                                  struct ip_set_ext *ext);
345 extern int ip_set_put_extensions(struct sk_buff *skb, const struct ip_set *set,
346                                  const void *e, bool active);
347 extern bool ip_set_match_extensions(struct ip_set *set,
348                                     const struct ip_set_ext *ext,
349                                     struct ip_set_ext *mext,
350                                     u32 flags, void *data);
351
352 static inline int
353 ip_set_get_hostipaddr4(struct nlattr *nla, u32 *ipaddr)
354 {
355         __be32 ip;
356         int ret = ip_set_get_ipaddr4(nla, &ip);
357
358         if (ret)
359                 return ret;
360         *ipaddr = ntohl(ip);
361         return 0;
362 }
363
364 /* Ignore IPSET_ERR_EXIST errors if asked to do so? */
365 static inline bool
366 ip_set_eexist(int ret, u32 flags)
367 {
368         return ret == -IPSET_ERR_EXIST && (flags & IPSET_FLAG_EXIST);
369 }
370
371 /* Match elements marked with nomatch */
372 static inline bool
373 ip_set_enomatch(int ret, u32 flags, enum ipset_adt adt, struct ip_set *set)
374 {
375         return adt == IPSET_TEST &&
376                (set->type->features & IPSET_TYPE_NOMATCH) &&
377                ((flags >> 16) & IPSET_FLAG_NOMATCH) &&
378                (ret > 0 || ret == -ENOTEMPTY);
379 }
380
381 /* Check the NLA_F_NET_BYTEORDER flag */
382 static inline bool
383 ip_set_attr_netorder(struct nlattr *tb[], int type)
384 {
385         return tb[type] && (tb[type]->nla_type & NLA_F_NET_BYTEORDER);
386 }
387
388 static inline bool
389 ip_set_optattr_netorder(struct nlattr *tb[], int type)
390 {
391         return !tb[type] || (tb[type]->nla_type & NLA_F_NET_BYTEORDER);
392 }
393
394 /* Useful converters */
395 static inline u32
396 ip_set_get_h32(const struct nlattr *attr)
397 {
398         return ntohl(nla_get_be32(attr));
399 }
400
401 static inline u16
402 ip_set_get_h16(const struct nlattr *attr)
403 {
404         return ntohs(nla_get_be16(attr));
405 }
406
407 /* In order to support older kernels before patch ae0be8de9a53cda3:
408  *
409  * netlink: make nla_nest_start() add NLA_F_NESTED flag
410  *
411  * we have to keep  ipset_nest_start() ipset_nest_end()
412  * in the package source
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_in_addr(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_in6_addr(skb, IPSET_ATTR_IPADDR_IPV6, ipaddrptr);
439         if (!ret)
440                 ipset_nest_end(skb, __nested);
441         return ret;
442 }
443
444 /* Get address from skbuff */
445 static inline __be32
446 ip4addr(const struct sk_buff *skb, bool src)
447 {
448         return src ? ip_hdr(skb)->saddr : ip_hdr(skb)->daddr;
449 }
450
451 static inline void
452 ip4addrptr(const struct sk_buff *skb, bool src, __be32 *addr)
453 {
454         *addr = src ? ip_hdr(skb)->saddr : ip_hdr(skb)->daddr;
455 }
456
457 static inline void
458 ip6addrptr(const struct sk_buff *skb, bool src, struct in6_addr *addr)
459 {
460         memcpy(addr, src ? &ipv6_hdr(skb)->saddr : &ipv6_hdr(skb)->daddr,
461                sizeof(*addr));
462 }
463
464 /* Calculate the bytes required to store the inclusive range of a-b */
465 static inline int
466 bitmap_bytes(u32 a, u32 b)
467 {
468         return 4 * ((((b - a + 8) / 8) + 3) / 4);
469 }
470
471 /* How often should the gc be run by default */
472 #define IPSET_GC_TIME                   (3 * 60)
473
474 /* Timeout period depending on the timeout value of the given set */
475 #define IPSET_GC_PERIOD(timeout) \
476         ((timeout/3) ? min_t(u32, (timeout)/3, IPSET_GC_TIME) : 1)
477
478 /* Entry is set with no timeout value */
479 #define IPSET_ELEM_PERMANENT    0
480
481 /* Set is defined with timeout support: timeout value may be 0 */
482 #define IPSET_NO_TIMEOUT        UINT_MAX
483
484 /* Max timeout value, see msecs_to_jiffies() in jiffies.h */
485 #define IPSET_MAX_TIMEOUT       (UINT_MAX >> 1)/MSEC_PER_SEC
486
487 #define ip_set_adt_opt_timeout(opt, set)        \
488 ((opt)->ext.timeout != IPSET_NO_TIMEOUT ? (opt)->ext.timeout : (set)->timeout)
489
490 static inline unsigned int
491 ip_set_timeout_uget(struct nlattr *tb)
492 {
493         unsigned int timeout = ip_set_get_h32(tb);
494
495         /* Normalize to fit into jiffies */
496         if (timeout > IPSET_MAX_TIMEOUT)
497                 timeout = IPSET_MAX_TIMEOUT;
498
499         return timeout;
500 }
501
502 static inline bool
503 ip_set_timeout_expired(const unsigned long *t)
504 {
505         return *t != IPSET_ELEM_PERMANENT && time_is_before_jiffies(*t);
506 }
507
508 static inline void
509 ip_set_timeout_set(unsigned long *timeout, u32 value)
510 {
511         unsigned long t;
512
513         if (!value) {
514                 *timeout = IPSET_ELEM_PERMANENT;
515                 return;
516         }
517
518         t = msecs_to_jiffies(value * MSEC_PER_SEC) + jiffies;
519         if (t == IPSET_ELEM_PERMANENT)
520                 /* Bingo! :-) */
521                 t--;
522         *timeout = t;
523 }
524
525 void ip_set_init_comment(struct ip_set *set, struct ip_set_comment *comment,
526                          const struct ip_set_ext *ext);
527
528 static inline void
529 ip_set_init_counter(struct ip_set_counter *counter,
530                     const struct ip_set_ext *ext)
531 {
532         if (ext->bytes != ULLONG_MAX)
533                 atomic64_set(&(counter)->bytes, (long long)(ext->bytes));
534         if (ext->packets != ULLONG_MAX)
535                 atomic64_set(&(counter)->packets, (long long)(ext->packets));
536 }
537
538 static inline void
539 ip_set_init_skbinfo(struct ip_set_skbinfo *skbinfo,
540                     const struct ip_set_ext *ext)
541 {
542         *skbinfo = ext->skbinfo;
543 }
544
545 #define IP_SET_INIT_KEXT(skb, opt, set)                 \
546         { .bytes = (skb)->len, .packets = 1,            \
547           .timeout = ip_set_adt_opt_timeout(opt, set) }
548
549 #define IP_SET_INIT_UEXT(set)                           \
550         { .bytes = ULLONG_MAX, .packets = ULLONG_MAX,   \
551           .timeout = (set)->timeout }
552
553 #define IPSET_CONCAT(a, b)              a##b
554 #define IPSET_TOKEN(a, b)               IPSET_CONCAT(a, b)
555
556 #endif /*_IP_SET_H */