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