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>
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.
14 /* The protocol version */
15 #define IPSET_PROTOCOL 6
17 /* The max length of strings including NUL: set and type identifiers */
18 #define IPSET_MAXNAMELEN 32
20 /* Message types and commands */
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 */
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 */
46 IPSET_CMD_COMMIT = IPSET_CMD_MAX, /* 18: Commit buffered commands */
49 /* Attributes at command level */
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 */
66 #define IPSET_ATTR_CMD_MAX (__IPSET_ATTR_CMD_MAX - 1)
68 /* CADT specific attributes */
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 */
93 IPSET_ATTR_REFERENCES,
96 __IPSET_ATTR_CREATE_MAX,
98 #define IPSET_ATTR_CREATE_MAX (__IPSET_ATTR_CREATE_MAX - 1)
100 /* ADT specific attributes */
102 IPSET_ATTR_ETHER = IPSET_ATTR_CADT_MAX + 1,
109 __IPSET_ATTR_ADT_MAX,
111 #define IPSET_ATTR_ADT_MAX (__IPSET_ATTR_ADT_MAX - 1)
113 /* IP specific attributes */
115 IPSET_ATTR_IPADDR_IPV4 = IPSET_ATTR_UNSPEC + 1,
116 IPSET_ATTR_IPADDR_IPV6,
117 __IPSET_ATTR_IPADDR_MAX,
119 #define IPSET_ATTR_IPADDR_MAX (__IPSET_ATTR_IPADDR_MAX - 1)
123 IPSET_ERR_PRIVATE = 4096,
128 IPSET_ERR_EXIST_SETNAME2,
129 IPSET_ERR_TYPE_MISMATCH,
131 IPSET_ERR_INVALID_CIDR,
132 IPSET_ERR_INVALID_NETMASK,
133 IPSET_ERR_INVALID_FAMILY,
135 IPSET_ERR_REFERENCED,
136 IPSET_ERR_IPADDR_IPV4,
137 IPSET_ERR_IPADDR_IPV6,
139 /* Type specific error codes */
140 IPSET_ERR_TYPE_SPECIFIC = 4352,
143 /* Flags at command level */
144 enum ipset_cmd_flags {
145 IPSET_FLAG_BIT_EXIST = 0,
146 IPSET_FLAG_EXIST = (1 << IPSET_FLAG_BIT_EXIST),
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),
153 /* Flags at CADT attribute level */
154 enum ipset_cadt_flags {
155 IPSET_FLAG_BIT_BEFORE = 0,
156 IPSET_FLAG_BEFORE = (1 << IPSET_FLAG_BIT_BEFORE),
157 IPSET_FLAG_BIT_PHYSDEV = 1,
158 IPSET_FLAG_PHYSDEV = (1 << IPSET_FLAG_BIT_PHYSDEV),
161 /* Commands with settype-specific attributes */
167 IPSET_CREATE = IPSET_ADT_MAX,
172 #include <linux/ip.h>
173 #include <linux/ipv6.h>
174 #include <linux/netlink.h>
175 #include <linux/netfilter.h>
176 #include <linux/netfilter/x_tables.h>
177 #include <linux/vmalloc.h>
178 #include <net/netlink.h>
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.
183 typedef u16 ip_set_id_t;
185 #define IPSET_INVALID_ID 65535
192 /* Max dimension in elements.
193 * If changed, new revision of iptables match/target is required.
198 /* Option flags for kernel operations */
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),
207 enum 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),
218 IPSET_TYPE_IFACE_FLAG = 5,
219 IPSET_TYPE_IFACE = (1 << IPSET_TYPE_IFACE_FLAG),
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),
228 typedef int (*ipset_adtfn)(struct ip_set *set, void *value,
229 u32 timeout, u32 flags);
231 /* Kernel API function options */
232 struct 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 */
240 /* Set type, variant-specific part */
241 struct 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,
247 const struct xt_action_param *par,
248 enum ipset_adt adt, const struct ip_set_adt_opt *opt);
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[],
255 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried);
257 /* Low level add/del/test functions */
258 ipset_adtfn adt[IPSET_ADT_MAX];
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);
271 int (*list)(const struct ip_set *set, struct sk_buff *skb,
272 struct netlink_callback *cb);
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);
279 /* The core set type structure */
281 struct list_head list;
284 char name[IPSET_MAXNAMELEN];
285 /* Protocol version */
287 /* Set features to control swapping */
289 /* Set type dimension */
291 /* Supported family: may be AF_UNSPEC for both AF_INET/AF_INET6 */
294 u8 revision_min, revision_max;
297 int (*create)(struct ip_set *set, struct nlattr *tb[], u32 flags);
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];
303 /* Set this to THIS_MODULE if you are a module, otherwise NULL */
307 /* register and unregister set type */
308 extern int ip_set_type_register(struct ip_set_type *set_type);
309 extern void ip_set_type_unregister(struct ip_set_type *set_type);
311 /* A generic IP set */
313 /* The name of the set */
314 char name[IPSET_MAXNAMELEN];
315 /* Lock protecting the set data */
317 /* References to the set */
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 */
325 /* The type revision */
327 /* The type specific data */
331 /* register and unregister set references */
332 extern ip_set_id_t ip_set_get_byname(const char *name, struct ip_set **set);
333 extern void ip_set_put_byindex(ip_set_id_t index);
334 extern const char *ip_set_name_byindex(ip_set_id_t index);
335 extern ip_set_id_t ip_set_nfnl_get(const char *name);
336 extern ip_set_id_t ip_set_nfnl_get_byindex(ip_set_id_t index);
337 extern void ip_set_nfnl_put(ip_set_id_t index);
339 /* API for iptables set match, and SET target */
341 extern int ip_set_add(ip_set_id_t id, const struct sk_buff *skb,
342 const struct xt_action_param *par,
343 const struct ip_set_adt_opt *opt);
344 extern int ip_set_del(ip_set_id_t id, const struct sk_buff *skb,
345 const struct xt_action_param *par,
346 const struct ip_set_adt_opt *opt);
347 extern int ip_set_test(ip_set_id_t id, const struct sk_buff *skb,
348 const struct xt_action_param *par,
349 const struct ip_set_adt_opt *opt);
351 /* Utility functions */
352 extern void *ip_set_alloc(size_t size);
353 extern void ip_set_free(void *members);
354 extern int ip_set_get_ipaddr4(struct nlattr *nla, __be32 *ipaddr);
355 extern int ip_set_get_ipaddr6(struct nlattr *nla, union nf_inet_addr *ipaddr);
358 ip_set_get_hostipaddr4(struct nlattr *nla, u32 *ipaddr)
361 int ret = ip_set_get_ipaddr4(nla, &ip);
369 /* Ignore IPSET_ERR_EXIST errors if asked to do so? */
371 ip_set_eexist(int ret, u32 flags)
373 return ret == -IPSET_ERR_EXIST && (flags & IPSET_FLAG_EXIST);
376 /* Check the NLA_F_NET_BYTEORDER flag */
378 ip_set_attr_netorder(struct nlattr *tb[], int type)
380 return tb[type] && (tb[type]->nla_type & NLA_F_NET_BYTEORDER);
384 ip_set_optattr_netorder(struct nlattr *tb[], int type)
386 return !tb[type] || (tb[type]->nla_type & NLA_F_NET_BYTEORDER);
389 /* Useful converters */
391 ip_set_get_h32(const struct nlattr *attr)
393 return ntohl(nla_get_be32(attr));
397 ip_set_get_h16(const struct nlattr *attr)
399 return ntohs(nla_get_be16(attr));
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)
405 #define NLA_PUT_IPADDR4(skb, type, ipaddr) \
407 struct nlattr *__nested = ipset_nest_start(skb, type); \
410 goto nla_put_failure; \
411 NLA_PUT_NET32(skb, IPSET_ATTR_IPADDR_IPV4, ipaddr); \
412 ipset_nest_end(skb, __nested); \
415 #define NLA_PUT_IPADDR6(skb, type, ipaddrptr) \
417 struct nlattr *__nested = ipset_nest_start(skb, type); \
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); \
426 /* Get address from skbuff */
428 ip4addr(const struct sk_buff *skb, bool src)
430 return src ? ip_hdr(skb)->saddr : ip_hdr(skb)->daddr;
434 ip4addrptr(const struct sk_buff *skb, bool src, __be32 *addr)
436 *addr = src ? ip_hdr(skb)->saddr : ip_hdr(skb)->daddr;
440 ip6addrptr(const struct sk_buff *skb, bool src, struct in6_addr *addr)
442 memcpy(addr, src ? &ipv6_hdr(skb)->saddr : &ipv6_hdr(skb)->daddr,
446 /* Calculate the bytes required to store the inclusive range of a-b */
448 bitmap_bytes(u32 a, u32 b)
450 return 4 * ((((b - a + 8) / 8) + 3) / 4);
453 /* Interface to iptables/ip6tables */
457 union ip_set_name_index {
458 char name[IPSET_MAXNAMELEN];
462 #define IP_SET_OP_GET_BYNAME 0x00000006 /* Get set index by name */
463 struct ip_set_req_get_set {
466 union ip_set_name_index set;
469 #define IP_SET_OP_GET_BYINDEX 0x00000007 /* Get set name by index */
470 /* Uses ip_set_req_get_set */
472 #define IP_SET_OP_VERSION 0x00000100 /* Ask kernel version */
473 struct ip_set_req_version {
478 #endif /* __KERNEL__ */
480 #endif /*_IP_SET_H */