]> git.karo-electronics.de Git - linux-beck.git/blob - include/linux/netfilter/ipset/ip_set.h
netfilter: ipset: Coding style fixes
[linux-beck.git] / include / linux / netfilter / ipset / ip_set.h
1 #ifndef _IP_SET_H
2 #define _IP_SET_H
3
4 /* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
5  *                         Patrick Schaaf <bof@bof.de>
6  *                         Martin Josefsson <gandalf@wlug.westbo.se>
7  * Copyright (C) 2003-2011 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  */
13
14 #include <linux/types.h>
15
16 /* The protocol version */
17 #define IPSET_PROTOCOL          6
18
19 /* The max length of strings including NUL: set and type identifiers */
20 #define IPSET_MAXNAMELEN        32
21
22 /* Message types and commands */
23 enum ipset_cmd {
24         IPSET_CMD_NONE,
25         IPSET_CMD_PROTOCOL,     /* 1: Return protocol version */
26         IPSET_CMD_CREATE,       /* 2: Create a new (empty) set */
27         IPSET_CMD_DESTROY,      /* 3: Destroy a (empty) set */
28         IPSET_CMD_FLUSH,        /* 4: Remove all elements from a set */
29         IPSET_CMD_RENAME,       /* 5: Rename a set */
30         IPSET_CMD_SWAP,         /* 6: Swap two sets */
31         IPSET_CMD_LIST,         /* 7: List sets */
32         IPSET_CMD_SAVE,         /* 8: Save sets */
33         IPSET_CMD_ADD,          /* 9: Add an element to a set */
34         IPSET_CMD_DEL,          /* 10: Delete an element from a set */
35         IPSET_CMD_TEST,         /* 11: Test an element in a set */
36         IPSET_CMD_HEADER,       /* 12: Get set header data only */
37         IPSET_CMD_TYPE,         /* 13: Get set type */
38         IPSET_MSG_MAX,          /* Netlink message commands */
39
40         /* Commands in userspace: */
41         IPSET_CMD_RESTORE = IPSET_MSG_MAX, /* 14: Enter restore mode */
42         IPSET_CMD_HELP,         /* 15: Get help */
43         IPSET_CMD_VERSION,      /* 16: Get program version */
44         IPSET_CMD_QUIT,         /* 17: Quit from interactive mode */
45
46         IPSET_CMD_MAX,
47
48         IPSET_CMD_COMMIT = IPSET_CMD_MAX, /* 18: Commit buffered commands */
49 };
50
51 /* Attributes at command level */
52 enum {
53         IPSET_ATTR_UNSPEC,
54         IPSET_ATTR_PROTOCOL,    /* 1: Protocol version */
55         IPSET_ATTR_SETNAME,     /* 2: Name of the set */
56         IPSET_ATTR_TYPENAME,    /* 3: Typename */
57         IPSET_ATTR_SETNAME2 = IPSET_ATTR_TYPENAME, /* Setname at rename/swap */
58         IPSET_ATTR_REVISION,    /* 4: Settype revision */
59         IPSET_ATTR_FAMILY,      /* 5: Settype family */
60         IPSET_ATTR_FLAGS,       /* 6: Flags at command level */
61         IPSET_ATTR_DATA,        /* 7: Nested attributes */
62         IPSET_ATTR_ADT,         /* 8: Multiple data containers */
63         IPSET_ATTR_LINENO,      /* 9: Restore lineno */
64         IPSET_ATTR_PROTOCOL_MIN, /* 10: Minimal supported version number */
65         IPSET_ATTR_REVISION_MIN = IPSET_ATTR_PROTOCOL_MIN, /* type rev min */
66         __IPSET_ATTR_CMD_MAX,
67 };
68 #define IPSET_ATTR_CMD_MAX      (__IPSET_ATTR_CMD_MAX - 1)
69
70 /* CADT specific attributes */
71 enum {
72         IPSET_ATTR_IP = IPSET_ATTR_UNSPEC + 1,
73         IPSET_ATTR_IP_FROM = IPSET_ATTR_IP,
74         IPSET_ATTR_IP_TO,       /* 2 */
75         IPSET_ATTR_CIDR,        /* 3 */
76         IPSET_ATTR_PORT,        /* 4 */
77         IPSET_ATTR_PORT_FROM = IPSET_ATTR_PORT,
78         IPSET_ATTR_PORT_TO,     /* 5 */
79         IPSET_ATTR_TIMEOUT,     /* 6 */
80         IPSET_ATTR_PROTO,       /* 7 */
81         IPSET_ATTR_CADT_FLAGS,  /* 8 */
82         IPSET_ATTR_CADT_LINENO = IPSET_ATTR_LINENO,     /* 9 */
83         /* Reserve empty slots */
84         IPSET_ATTR_CADT_MAX = 16,
85         /* Create-only specific attributes */
86         IPSET_ATTR_GC,
87         IPSET_ATTR_HASHSIZE,
88         IPSET_ATTR_MAXELEM,
89         IPSET_ATTR_NETMASK,
90         IPSET_ATTR_PROBES,
91         IPSET_ATTR_RESIZE,
92         IPSET_ATTR_SIZE,
93         /* Kernel-only */
94         IPSET_ATTR_ELEMENTS,
95         IPSET_ATTR_REFERENCES,
96         IPSET_ATTR_MEMSIZE,
97
98         __IPSET_ATTR_CREATE_MAX,
99 };
100 #define IPSET_ATTR_CREATE_MAX   (__IPSET_ATTR_CREATE_MAX - 1)
101
102 /* ADT specific attributes */
103 enum {
104         IPSET_ATTR_ETHER = IPSET_ATTR_CADT_MAX + 1,
105         IPSET_ATTR_NAME,
106         IPSET_ATTR_NAMEREF,
107         IPSET_ATTR_IP2,
108         IPSET_ATTR_CIDR2,
109         IPSET_ATTR_IP2_TO,
110         IPSET_ATTR_IFACE,
111         __IPSET_ATTR_ADT_MAX,
112 };
113 #define IPSET_ATTR_ADT_MAX      (__IPSET_ATTR_ADT_MAX - 1)
114
115 /* IP specific attributes */
116 enum {
117         IPSET_ATTR_IPADDR_IPV4 = IPSET_ATTR_UNSPEC + 1,
118         IPSET_ATTR_IPADDR_IPV6,
119         __IPSET_ATTR_IPADDR_MAX,
120 };
121 #define IPSET_ATTR_IPADDR_MAX   (__IPSET_ATTR_IPADDR_MAX - 1)
122
123 /* Error codes */
124 enum ipset_errno {
125         IPSET_ERR_PRIVATE = 4096,
126         IPSET_ERR_PROTOCOL,
127         IPSET_ERR_FIND_TYPE,
128         IPSET_ERR_MAX_SETS,
129         IPSET_ERR_BUSY,
130         IPSET_ERR_EXIST_SETNAME2,
131         IPSET_ERR_TYPE_MISMATCH,
132         IPSET_ERR_EXIST,
133         IPSET_ERR_INVALID_CIDR,
134         IPSET_ERR_INVALID_NETMASK,
135         IPSET_ERR_INVALID_FAMILY,
136         IPSET_ERR_TIMEOUT,
137         IPSET_ERR_REFERENCED,
138         IPSET_ERR_IPADDR_IPV4,
139         IPSET_ERR_IPADDR_IPV6,
140
141         /* Type specific error codes */
142         IPSET_ERR_TYPE_SPECIFIC = 4352,
143 };
144
145 /* Flags at command level */
146 enum ipset_cmd_flags {
147         IPSET_FLAG_BIT_EXIST    = 0,
148         IPSET_FLAG_EXIST        = (1 << IPSET_FLAG_BIT_EXIST),
149         IPSET_FLAG_BIT_LIST_SETNAME = 1,
150         IPSET_FLAG_LIST_SETNAME = (1 << IPSET_FLAG_BIT_LIST_SETNAME),
151         IPSET_FLAG_BIT_LIST_HEADER = 2,
152         IPSET_FLAG_LIST_HEADER  = (1 << IPSET_FLAG_BIT_LIST_HEADER),
153         IPSET_FLAG_CMD_MAX = 15,        /* Lower half */
154 };
155
156 /* Flags at CADT attribute level */
157 enum ipset_cadt_flags {
158         IPSET_FLAG_BIT_BEFORE   = 0,
159         IPSET_FLAG_BEFORE       = (1 << IPSET_FLAG_BIT_BEFORE),
160         IPSET_FLAG_BIT_PHYSDEV  = 1,
161         IPSET_FLAG_PHYSDEV      = (1 << IPSET_FLAG_BIT_PHYSDEV),
162         IPSET_FLAG_BIT_NOMATCH  = 2,
163         IPSET_FLAG_NOMATCH      = (1 << IPSET_FLAG_BIT_NOMATCH),
164         IPSET_FLAG_CADT_MAX     = 15,   /* Upper half */
165 };
166
167 /* Commands with settype-specific attributes */
168 enum ipset_adt {
169         IPSET_ADD,
170         IPSET_DEL,
171         IPSET_TEST,
172         IPSET_ADT_MAX,
173         IPSET_CREATE = IPSET_ADT_MAX,
174         IPSET_CADT_MAX,
175 };
176
177 /* Sets are identified by an index in kernel space. Tweak with ip_set_id_t
178  * and IPSET_INVALID_ID if you want to increase the max number of sets.
179  */
180 typedef __u16 ip_set_id_t;
181
182 #define IPSET_INVALID_ID                65535
183
184 enum ip_set_dim {
185         IPSET_DIM_ZERO = 0,
186         IPSET_DIM_ONE,
187         IPSET_DIM_TWO,
188         IPSET_DIM_THREE,
189         /* Max dimension in elements.
190          * If changed, new revision of iptables match/target is required.
191          */
192         IPSET_DIM_MAX = 6,
193 };
194
195 /* Option flags for kernel operations */
196 enum ip_set_kopt {
197         IPSET_INV_MATCH = (1 << IPSET_DIM_ZERO),
198         IPSET_DIM_ONE_SRC = (1 << IPSET_DIM_ONE),
199         IPSET_DIM_TWO_SRC = (1 << IPSET_DIM_TWO),
200         IPSET_DIM_THREE_SRC = (1 << IPSET_DIM_THREE),
201 };
202
203 #ifdef __KERNEL__
204 #include <linux/ip.h>
205 #include <linux/ipv6.h>
206 #include <linux/netlink.h>
207 #include <linux/netfilter.h>
208 #include <linux/netfilter/x_tables.h>
209 #include <linux/stringify.h>
210 #include <linux/vmalloc.h>
211 #include <net/netlink.h>
212
213 #define _IP_SET_MODULE_DESC(a, b, c)            \
214         MODULE_DESCRIPTION(a " type of IP sets, revisions " b "-" c)
215 #define IP_SET_MODULE_DESC(a, b, c)             \
216         _IP_SET_MODULE_DESC(a, __stringify(b), __stringify(c))
217
218 /* Set features */
219 enum ip_set_feature {
220         IPSET_TYPE_IP_FLAG = 0,
221         IPSET_TYPE_IP = (1 << IPSET_TYPE_IP_FLAG),
222         IPSET_TYPE_PORT_FLAG = 1,
223         IPSET_TYPE_PORT = (1 << IPSET_TYPE_PORT_FLAG),
224         IPSET_TYPE_MAC_FLAG = 2,
225         IPSET_TYPE_MAC = (1 << IPSET_TYPE_MAC_FLAG),
226         IPSET_TYPE_IP2_FLAG = 3,
227         IPSET_TYPE_IP2 = (1 << IPSET_TYPE_IP2_FLAG),
228         IPSET_TYPE_NAME_FLAG = 4,
229         IPSET_TYPE_NAME = (1 << IPSET_TYPE_NAME_FLAG),
230         IPSET_TYPE_IFACE_FLAG = 5,
231         IPSET_TYPE_IFACE = (1 << IPSET_TYPE_IFACE_FLAG),
232         /* Strictly speaking not a feature, but a flag for dumping:
233          * this settype must be dumped last */
234         IPSET_DUMP_LAST_FLAG = 7,
235         IPSET_DUMP_LAST = (1 << IPSET_DUMP_LAST_FLAG),
236 };
237
238 struct ip_set;
239
240 typedef int (*ipset_adtfn)(struct ip_set *set, void *value,
241                            u32 timeout, u32 flags);
242
243 /* Kernel API function options */
244 struct ip_set_adt_opt {
245         u8 family;              /* Actual protocol family */
246         u8 dim;                 /* Dimension of match/target */
247         u8 flags;               /* Direction and negation flags */
248         u32 cmdflags;           /* Command-like flags */
249         u32 timeout;            /* Timeout value */
250 };
251
252 /* Set type, variant-specific part */
253 struct ip_set_type_variant {
254         /* Kernelspace: test/add/del entries
255          *              returns negative error code,
256          *                      zero for no match/success to add/delete
257          *                      positive for matching element */
258         int (*kadt)(struct ip_set *set, const struct sk_buff *skb,
259                     const struct xt_action_param *par,
260                     enum ipset_adt adt, const struct ip_set_adt_opt *opt);
261
262         /* Userspace: test/add/del entries
263          *              returns negative error code,
264          *                      zero for no match/success to add/delete
265          *                      positive for matching element */
266         int (*uadt)(struct ip_set *set, struct nlattr *tb[],
267                     enum ipset_adt adt, u32 *lineno, u32 flags, bool retried);
268
269         /* Low level add/del/test functions */
270         ipset_adtfn adt[IPSET_ADT_MAX];
271
272         /* When adding entries and set is full, try to resize the set */
273         int (*resize)(struct ip_set *set, bool retried);
274         /* Destroy the set */
275         void (*destroy)(struct ip_set *set);
276         /* Flush the elements */
277         void (*flush)(struct ip_set *set);
278         /* Expire entries before listing */
279         void (*expire)(struct ip_set *set);
280         /* List set header data */
281         int (*head)(struct ip_set *set, struct sk_buff *skb);
282         /* List elements */
283         int (*list)(const struct ip_set *set, struct sk_buff *skb,
284                     struct netlink_callback *cb);
285
286         /* Return true if "b" set is the same as "a"
287          * according to the create set parameters */
288         bool (*same_set)(const struct ip_set *a, const struct ip_set *b);
289 };
290
291 /* The core set type structure */
292 struct ip_set_type {
293         struct list_head list;
294
295         /* Typename */
296         char name[IPSET_MAXNAMELEN];
297         /* Protocol version */
298         u8 protocol;
299         /* Set features to control swapping */
300         u8 features;
301         /* Set type dimension */
302         u8 dimension;
303         /*
304          * Supported family: may be NFPROTO_UNSPEC for both
305          * NFPROTO_IPV4/NFPROTO_IPV6.
306          */
307         u8 family;
308         /* Type revisions */
309         u8 revision_min, revision_max;
310
311         /* Create set */
312         int (*create)(struct ip_set *set, struct nlattr *tb[], u32 flags);
313
314         /* Attribute policies */
315         const struct nla_policy create_policy[IPSET_ATTR_CREATE_MAX + 1];
316         const struct nla_policy adt_policy[IPSET_ATTR_ADT_MAX + 1];
317
318         /* Set this to THIS_MODULE if you are a module, otherwise NULL */
319         struct module *me;
320 };
321
322 /* register and unregister set type */
323 extern int ip_set_type_register(struct ip_set_type *set_type);
324 extern void ip_set_type_unregister(struct ip_set_type *set_type);
325
326 /* A generic IP set */
327 struct ip_set {
328         /* The name of the set */
329         char name[IPSET_MAXNAMELEN];
330         /* Lock protecting the set data */
331         rwlock_t lock;
332         /* References to the set */
333         u32 ref;
334         /* The core set type */
335         struct ip_set_type *type;
336         /* The type variant doing the real job */
337         const struct ip_set_type_variant *variant;
338         /* The actual INET family of the set */
339         u8 family;
340         /* The type revision */
341         u8 revision;
342         /* The type specific data */
343         void *data;
344 };
345
346 /* register and unregister set references */
347 extern ip_set_id_t ip_set_get_byname(const char *name, struct ip_set **set);
348 extern void ip_set_put_byindex(ip_set_id_t index);
349 extern const char *ip_set_name_byindex(ip_set_id_t index);
350 extern ip_set_id_t ip_set_nfnl_get(const char *name);
351 extern ip_set_id_t ip_set_nfnl_get_byindex(ip_set_id_t index);
352 extern void ip_set_nfnl_put(ip_set_id_t index);
353
354 /* API for iptables set match, and SET target */
355
356 extern int ip_set_add(ip_set_id_t id, const struct sk_buff *skb,
357                       const struct xt_action_param *par,
358                       const struct ip_set_adt_opt *opt);
359 extern int ip_set_del(ip_set_id_t id, const struct sk_buff *skb,
360                       const struct xt_action_param *par,
361                       const struct ip_set_adt_opt *opt);
362 extern int ip_set_test(ip_set_id_t id, const struct sk_buff *skb,
363                        const struct xt_action_param *par,
364                        const struct ip_set_adt_opt *opt);
365
366 /* Utility functions */
367 extern void *ip_set_alloc(size_t size);
368 extern void ip_set_free(void *members);
369 extern int ip_set_get_ipaddr4(struct nlattr *nla,  __be32 *ipaddr);
370 extern int ip_set_get_ipaddr6(struct nlattr *nla, union nf_inet_addr *ipaddr);
371
372 static inline int
373 ip_set_get_hostipaddr4(struct nlattr *nla, u32 *ipaddr)
374 {
375         __be32 ip;
376         int ret = ip_set_get_ipaddr4(nla, &ip);
377
378         if (ret)
379                 return ret;
380         *ipaddr = ntohl(ip);
381         return 0;
382 }
383
384 /* Ignore IPSET_ERR_EXIST errors if asked to do so? */
385 static inline bool
386 ip_set_eexist(int ret, u32 flags)
387 {
388         return ret == -IPSET_ERR_EXIST && (flags & IPSET_FLAG_EXIST);
389 }
390
391 /* Check the NLA_F_NET_BYTEORDER flag */
392 static inline bool
393 ip_set_attr_netorder(struct nlattr *tb[], int type)
394 {
395         return tb[type] && (tb[type]->nla_type & NLA_F_NET_BYTEORDER);
396 }
397
398 static inline bool
399 ip_set_optattr_netorder(struct nlattr *tb[], int type)
400 {
401         return !tb[type] || (tb[type]->nla_type & NLA_F_NET_BYTEORDER);
402 }
403
404 /* Useful converters */
405 static inline u32
406 ip_set_get_h32(const struct nlattr *attr)
407 {
408         return ntohl(nla_get_be32(attr));
409 }
410
411 static inline u16
412 ip_set_get_h16(const struct nlattr *attr)
413 {
414         return ntohs(nla_get_be16(attr));
415 }
416
417 #define ipset_nest_start(skb, attr) nla_nest_start(skb, attr | NLA_F_NESTED)
418 #define ipset_nest_end(skb, start)  nla_nest_end(skb, start)
419
420 static inline int nla_put_ipaddr4(struct sk_buff *skb, int type, __be32 ipaddr)
421 {
422         struct nlattr *__nested = ipset_nest_start(skb, type);
423         int ret;
424
425         if (!__nested)
426                 return -EMSGSIZE;
427         ret = nla_put_net32(skb, IPSET_ATTR_IPADDR_IPV4, ipaddr);
428         if (!ret)
429                 ipset_nest_end(skb, __nested);
430         return ret;
431 }
432
433 static inline int nla_put_ipaddr6(struct sk_buff *skb, int type,
434                                   const struct in6_addr *ipaddrptr)
435 {
436         struct nlattr *__nested = ipset_nest_start(skb, type);
437         int ret;
438
439         if (!__nested)
440                 return -EMSGSIZE;
441         ret = nla_put(skb, IPSET_ATTR_IPADDR_IPV6,
442                       sizeof(struct in6_addr), ipaddrptr);
443         if (!ret)
444                 ipset_nest_end(skb, __nested);
445         return ret;
446 }
447
448 /* Get address from skbuff */
449 static inline __be32
450 ip4addr(const struct sk_buff *skb, bool src)
451 {
452         return src ? ip_hdr(skb)->saddr : ip_hdr(skb)->daddr;
453 }
454
455 static inline void
456 ip4addrptr(const struct sk_buff *skb, bool src, __be32 *addr)
457 {
458         *addr = src ? ip_hdr(skb)->saddr : ip_hdr(skb)->daddr;
459 }
460
461 static inline void
462 ip6addrptr(const struct sk_buff *skb, bool src, struct in6_addr *addr)
463 {
464         memcpy(addr, src ? &ipv6_hdr(skb)->saddr : &ipv6_hdr(skb)->daddr,
465                sizeof(*addr));
466 }
467
468 /* Calculate the bytes required to store the inclusive range of a-b */
469 static inline int
470 bitmap_bytes(u32 a, u32 b)
471 {
472         return 4 * ((((b - a + 8) / 8) + 3) / 4);
473 }
474
475 #endif /* __KERNEL__ */
476
477 /* Interface to iptables/ip6tables */
478
479 #define SO_IP_SET               83
480
481 union ip_set_name_index {
482         char name[IPSET_MAXNAMELEN];
483         ip_set_id_t index;
484 };
485
486 #define IP_SET_OP_GET_BYNAME    0x00000006      /* Get set index by name */
487 struct ip_set_req_get_set {
488         unsigned int op;
489         unsigned int version;
490         union ip_set_name_index set;
491 };
492
493 #define IP_SET_OP_GET_BYINDEX   0x00000007      /* Get set name by index */
494 /* Uses ip_set_req_get_set */
495
496 #define IP_SET_OP_VERSION       0x00000100      /* Ask kernel version */
497 struct ip_set_req_version {
498         unsigned int op;
499         unsigned int version;
500 };
501
502 #endif /*_IP_SET_H */