netfilter: ipset: add xt_action_param to the variant level kadt functions, ipset...
[GitHub/mt8127/android_kernel_alcatel_ttab.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,
a7b4f989
JK
108 __IPSET_ATTR_ADT_MAX,
109};
110#define IPSET_ATTR_ADT_MAX (__IPSET_ATTR_ADT_MAX - 1)
111
112/* IP specific attributes */
113enum {
114 IPSET_ATTR_IPADDR_IPV4 = IPSET_ATTR_UNSPEC + 1,
115 IPSET_ATTR_IPADDR_IPV6,
116 __IPSET_ATTR_IPADDR_MAX,
117};
118#define IPSET_ATTR_IPADDR_MAX (__IPSET_ATTR_IPADDR_MAX - 1)
119
120/* Error codes */
121enum ipset_errno {
122 IPSET_ERR_PRIVATE = 4096,
123 IPSET_ERR_PROTOCOL,
124 IPSET_ERR_FIND_TYPE,
125 IPSET_ERR_MAX_SETS,
126 IPSET_ERR_BUSY,
127 IPSET_ERR_EXIST_SETNAME2,
128 IPSET_ERR_TYPE_MISMATCH,
129 IPSET_ERR_EXIST,
130 IPSET_ERR_INVALID_CIDR,
131 IPSET_ERR_INVALID_NETMASK,
132 IPSET_ERR_INVALID_FAMILY,
133 IPSET_ERR_TIMEOUT,
134 IPSET_ERR_REFERENCED,
135 IPSET_ERR_IPADDR_IPV4,
136 IPSET_ERR_IPADDR_IPV6,
137
138 /* Type specific error codes */
139 IPSET_ERR_TYPE_SPECIFIC = 4352,
140};
141
142/* Flags at command level */
143enum ipset_cmd_flags {
144 IPSET_FLAG_BIT_EXIST = 0,
145 IPSET_FLAG_EXIST = (1 << IPSET_FLAG_BIT_EXIST),
c1e2e043
JK
146 IPSET_FLAG_BIT_LIST_SETNAME = 1,
147 IPSET_FLAG_LIST_SETNAME = (1 << IPSET_FLAG_BIT_LIST_SETNAME),
148 IPSET_FLAG_BIT_LIST_HEADER = 2,
149 IPSET_FLAG_LIST_HEADER = (1 << IPSET_FLAG_BIT_LIST_HEADER),
a7b4f989
JK
150};
151
152/* Flags at CADT attribute level */
153enum ipset_cadt_flags {
154 IPSET_FLAG_BIT_BEFORE = 0,
155 IPSET_FLAG_BEFORE = (1 << IPSET_FLAG_BIT_BEFORE),
156};
157
158/* Commands with settype-specific attributes */
159enum ipset_adt {
160 IPSET_ADD,
161 IPSET_DEL,
162 IPSET_TEST,
163 IPSET_ADT_MAX,
164 IPSET_CREATE = IPSET_ADT_MAX,
165 IPSET_CADT_MAX,
166};
167
168#ifdef __KERNEL__
169#include <linux/ip.h>
170#include <linux/ipv6.h>
171#include <linux/netlink.h>
172#include <linux/netfilter.h>
b66554cf 173#include <linux/netfilter/x_tables.h>
a7b4f989
JK
174#include <linux/vmalloc.h>
175#include <net/netlink.h>
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 */
180typedef u16 ip_set_id_t;
181
182#define IPSET_INVALID_ID 65535
183
184enum 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 */
196enum 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/* Set features */
204enum ip_set_feature {
205 IPSET_TYPE_IP_FLAG = 0,
206 IPSET_TYPE_IP = (1 << IPSET_TYPE_IP_FLAG),
207 IPSET_TYPE_PORT_FLAG = 1,
208 IPSET_TYPE_PORT = (1 << IPSET_TYPE_PORT_FLAG),
209 IPSET_TYPE_MAC_FLAG = 2,
210 IPSET_TYPE_MAC = (1 << IPSET_TYPE_MAC_FLAG),
211 IPSET_TYPE_IP2_FLAG = 3,
212 IPSET_TYPE_IP2 = (1 << IPSET_TYPE_IP2_FLAG),
213 IPSET_TYPE_NAME_FLAG = 4,
214 IPSET_TYPE_NAME = (1 << IPSET_TYPE_NAME_FLAG),
215 /* Strictly speaking not a feature, but a flag for dumping:
216 * this settype must be dumped last */
217 IPSET_DUMP_LAST_FLAG = 7,
218 IPSET_DUMP_LAST = (1 << IPSET_DUMP_LAST_FLAG),
219};
220
221struct ip_set;
222
5416219e
JK
223typedef int (*ipset_adtfn)(struct ip_set *set, void *value,
224 u32 timeout, u32 flags);
a7b4f989 225
ac8cc925
JK
226/* Kernel API function options */
227struct ip_set_adt_opt {
228 u8 family; /* Actual protocol family */
229 u8 dim; /* Dimension of match/target */
230 u8 flags; /* Direction and negation flags */
231 u32 cmdflags; /* Command-like flags */
232 u32 timeout; /* Timeout value */
233};
234
a7b4f989
JK
235/* Set type, variant-specific part */
236struct ip_set_type_variant {
237 /* Kernelspace: test/add/del entries
238 * returns negative error code,
239 * zero for no match/success to add/delete
240 * positive for matching element */
241 int (*kadt)(struct ip_set *set, const struct sk_buff * skb,
b66554cf 242 const struct xt_action_param *par,
ac8cc925 243 enum ipset_adt adt, const struct ip_set_adt_opt *opt);
a7b4f989
JK
244
245 /* Userspace: test/add/del entries
246 * returns negative error code,
247 * zero for no match/success to add/delete
248 * positive for matching element */
249 int (*uadt)(struct ip_set *set, struct nlattr *tb[],
3d14b171 250 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried);
a7b4f989
JK
251
252 /* Low level add/del/test functions */
253 ipset_adtfn adt[IPSET_ADT_MAX];
254
255 /* When adding entries and set is full, try to resize the set */
256 int (*resize)(struct ip_set *set, bool retried);
257 /* Destroy the set */
258 void (*destroy)(struct ip_set *set);
259 /* Flush the elements */
260 void (*flush)(struct ip_set *set);
261 /* Expire entries before listing */
262 void (*expire)(struct ip_set *set);
263 /* List set header data */
264 int (*head)(struct ip_set *set, struct sk_buff *skb);
265 /* List elements */
266 int (*list)(const struct ip_set *set, struct sk_buff *skb,
267 struct netlink_callback *cb);
268
269 /* Return true if "b" set is the same as "a"
270 * according to the create set parameters */
271 bool (*same_set)(const struct ip_set *a, const struct ip_set *b);
272};
273
274/* The core set type structure */
275struct ip_set_type {
276 struct list_head list;
277
278 /* Typename */
279 char name[IPSET_MAXNAMELEN];
280 /* Protocol version */
281 u8 protocol;
282 /* Set features to control swapping */
283 u8 features;
284 /* Set type dimension */
285 u8 dimension;
286 /* Supported family: may be AF_UNSPEC for both AF_INET/AF_INET6 */
287 u8 family;
f1e00b39
JK
288 /* Type revisions */
289 u8 revision_min, revision_max;
a7b4f989
JK
290
291 /* Create set */
292 int (*create)(struct ip_set *set, struct nlattr *tb[], u32 flags);
293
294 /* Attribute policies */
295 const struct nla_policy create_policy[IPSET_ATTR_CREATE_MAX + 1];
296 const struct nla_policy adt_policy[IPSET_ATTR_ADT_MAX + 1];
297
298 /* Set this to THIS_MODULE if you are a module, otherwise NULL */
299 struct module *me;
300};
301
302/* register and unregister set type */
303extern int ip_set_type_register(struct ip_set_type *set_type);
304extern void ip_set_type_unregister(struct ip_set_type *set_type);
305
306/* A generic IP set */
307struct ip_set {
308 /* The name of the set */
309 char name[IPSET_MAXNAMELEN];
310 /* Lock protecting the set data */
311 rwlock_t lock;
312 /* References to the set */
2f9f28b2 313 u32 ref;
a7b4f989
JK
314 /* The core set type */
315 struct ip_set_type *type;
316 /* The type variant doing the real job */
317 const struct ip_set_type_variant *variant;
318 /* The actual INET family of the set */
319 u8 family;
f1e00b39
JK
320 /* The type revision */
321 u8 revision;
a7b4f989
JK
322 /* The type specific data */
323 void *data;
324};
325
326/* register and unregister set references */
327extern ip_set_id_t ip_set_get_byname(const char *name, struct ip_set **set);
328extern void ip_set_put_byindex(ip_set_id_t index);
329extern const char * ip_set_name_byindex(ip_set_id_t index);
330extern ip_set_id_t ip_set_nfnl_get(const char *name);
331extern ip_set_id_t ip_set_nfnl_get_byindex(ip_set_id_t index);
332extern void ip_set_nfnl_put(ip_set_id_t index);
333
334/* API for iptables set match, and SET target */
ac8cc925 335
a7b4f989 336extern int ip_set_add(ip_set_id_t id, const struct sk_buff *skb,
b66554cf 337 const struct xt_action_param *par,
ac8cc925 338 const struct ip_set_adt_opt *opt);
a7b4f989 339extern int ip_set_del(ip_set_id_t id, const struct sk_buff *skb,
b66554cf 340 const struct xt_action_param *par,
ac8cc925 341 const struct ip_set_adt_opt *opt);
a7b4f989 342extern int ip_set_test(ip_set_id_t id, const struct sk_buff *skb,
b66554cf 343 const struct xt_action_param *par,
ac8cc925 344 const struct ip_set_adt_opt *opt);
a7b4f989
JK
345
346/* Utility functions */
347extern void * ip_set_alloc(size_t size);
348extern void ip_set_free(void *members);
349extern int ip_set_get_ipaddr4(struct nlattr *nla, __be32 *ipaddr);
350extern int ip_set_get_ipaddr6(struct nlattr *nla, union nf_inet_addr *ipaddr);
351
352static inline int
353ip_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? */
365static inline bool
366ip_set_eexist(int ret, u32 flags)
367{
368 return ret == -IPSET_ERR_EXIST && (flags & IPSET_FLAG_EXIST);
369}
370
371/* Check the NLA_F_NET_BYTEORDER flag */
372static inline bool
373ip_set_attr_netorder(struct nlattr *tb[], int type)
374{
375 return tb[type] && (tb[type]->nla_type & NLA_F_NET_BYTEORDER);
376}
377
378static inline bool
379ip_set_optattr_netorder(struct nlattr *tb[], int type)
380{
381 return !tb[type] || (tb[type]->nla_type & NLA_F_NET_BYTEORDER);
382}
383
384/* Useful converters */
385static inline u32
386ip_set_get_h32(const struct nlattr *attr)
387{
388 return ntohl(nla_get_be32(attr));
389}
390
391static inline u16
392ip_set_get_h16(const struct nlattr *attr)
393{
394 return ntohs(nla_get_be16(attr));
395}
396
397#define ipset_nest_start(skb, attr) nla_nest_start(skb, attr | NLA_F_NESTED)
398#define ipset_nest_end(skb, start) nla_nest_end(skb, start)
399
400#define NLA_PUT_IPADDR4(skb, type, ipaddr) \
401do { \
402 struct nlattr *__nested = ipset_nest_start(skb, type); \
403 \
404 if (!__nested) \
405 goto nla_put_failure; \
406 NLA_PUT_NET32(skb, IPSET_ATTR_IPADDR_IPV4, ipaddr); \
407 ipset_nest_end(skb, __nested); \
408} while (0)
409
410#define NLA_PUT_IPADDR6(skb, type, ipaddrptr) \
411do { \
412 struct nlattr *__nested = ipset_nest_start(skb, type); \
413 \
414 if (!__nested) \
415 goto nla_put_failure; \
416 NLA_PUT(skb, IPSET_ATTR_IPADDR_IPV6, \
417 sizeof(struct in6_addr), ipaddrptr); \
418 ipset_nest_end(skb, __nested); \
419} while (0)
420
421/* Get address from skbuff */
422static inline __be32
423ip4addr(const struct sk_buff *skb, bool src)
424{
425 return src ? ip_hdr(skb)->saddr : ip_hdr(skb)->daddr;
426}
427
428static inline void
429ip4addrptr(const struct sk_buff *skb, bool src, __be32 *addr)
430{
431 *addr = src ? ip_hdr(skb)->saddr : ip_hdr(skb)->daddr;
432}
433
434static inline void
435ip6addrptr(const struct sk_buff *skb, bool src, struct in6_addr *addr)
436{
437 memcpy(addr, src ? &ipv6_hdr(skb)->saddr : &ipv6_hdr(skb)->daddr,
438 sizeof(*addr));
439}
440
441/* Calculate the bytes required to store the inclusive range of a-b */
442static inline int
443bitmap_bytes(u32 a, u32 b)
444{
445 return 4 * ((((b - a + 8) / 8) + 3) / 4);
446}
447
448/* Interface to iptables/ip6tables */
449
450#define SO_IP_SET 83
451
452union ip_set_name_index {
453 char name[IPSET_MAXNAMELEN];
454 ip_set_id_t index;
455};
456
457#define IP_SET_OP_GET_BYNAME 0x00000006 /* Get set index by name */
458struct ip_set_req_get_set {
459 unsigned op;
460 unsigned version;
461 union ip_set_name_index set;
462};
463
464#define IP_SET_OP_GET_BYINDEX 0x00000007 /* Get set name by index */
465/* Uses ip_set_req_get_set */
466
467#define IP_SET_OP_VERSION 0x00000100 /* Ask kernel version */
468struct ip_set_req_version {
469 unsigned op;
470 unsigned version;
471};
472
473#endif /* __KERNEL__ */
474
475#endif /*_IP_SET_H */