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,
107 __IPSET_ATTR_ADT_MAX,
109 #define IPSET_ATTR_ADT_MAX (__IPSET_ATTR_ADT_MAX - 1)
111 /* IP specific attributes */
113 IPSET_ATTR_IPADDR_IPV4 = IPSET_ATTR_UNSPEC + 1,
114 IPSET_ATTR_IPADDR_IPV6,
115 __IPSET_ATTR_IPADDR_MAX,
117 #define IPSET_ATTR_IPADDR_MAX (__IPSET_ATTR_IPADDR_MAX - 1)
121 IPSET_ERR_PRIVATE = 4096,
126 IPSET_ERR_EXIST_SETNAME2,
127 IPSET_ERR_TYPE_MISMATCH,
129 IPSET_ERR_INVALID_CIDR,
130 IPSET_ERR_INVALID_NETMASK,
131 IPSET_ERR_INVALID_FAMILY,
133 IPSET_ERR_REFERENCED,
134 IPSET_ERR_IPADDR_IPV4,
135 IPSET_ERR_IPADDR_IPV6,
137 /* Type specific error codes */
138 IPSET_ERR_TYPE_SPECIFIC = 4352,
141 /* Flags at command level */
142 enum ipset_cmd_flags {
143 IPSET_FLAG_BIT_EXIST = 0,
144 IPSET_FLAG_EXIST = (1 << IPSET_FLAG_BIT_EXIST),
147 /* Flags at CADT attribute level */
148 enum ipset_cadt_flags {
149 IPSET_FLAG_BIT_BEFORE = 0,
150 IPSET_FLAG_BEFORE = (1 << IPSET_FLAG_BIT_BEFORE),
153 /* Commands with settype-specific attributes */
159 IPSET_CREATE = IPSET_ADT_MAX,
164 #include <linux/ip.h>
165 #include <linux/ipv6.h>
166 #include <linux/netlink.h>
167 #include <linux/netfilter.h>
168 #include <linux/vmalloc.h>
169 #include <net/netlink.h>
171 /* Sets are identified by an index in kernel space. Tweak with ip_set_id_t
172 * and IPSET_INVALID_ID if you want to increase the max number of sets.
174 typedef u16 ip_set_id_t;
176 #define IPSET_INVALID_ID 65535
183 /* Max dimension in elements.
184 * If changed, new revision of iptables match/target is required.
189 /* Option flags for kernel operations */
191 IPSET_INV_MATCH = (1 << IPSET_DIM_ZERO),
192 IPSET_DIM_ONE_SRC = (1 << IPSET_DIM_ONE),
193 IPSET_DIM_TWO_SRC = (1 << IPSET_DIM_TWO),
194 IPSET_DIM_THREE_SRC = (1 << IPSET_DIM_THREE),
198 enum ip_set_feature {
199 IPSET_TYPE_IP_FLAG = 0,
200 IPSET_TYPE_IP = (1 << IPSET_TYPE_IP_FLAG),
201 IPSET_TYPE_PORT_FLAG = 1,
202 IPSET_TYPE_PORT = (1 << IPSET_TYPE_PORT_FLAG),
203 IPSET_TYPE_MAC_FLAG = 2,
204 IPSET_TYPE_MAC = (1 << IPSET_TYPE_MAC_FLAG),
205 IPSET_TYPE_IP2_FLAG = 3,
206 IPSET_TYPE_IP2 = (1 << IPSET_TYPE_IP2_FLAG),
207 IPSET_TYPE_NAME_FLAG = 4,
208 IPSET_TYPE_NAME = (1 << IPSET_TYPE_NAME_FLAG),
209 /* Strictly speaking not a feature, but a flag for dumping:
210 * this settype must be dumped last */
211 IPSET_DUMP_LAST_FLAG = 7,
212 IPSET_DUMP_LAST = (1 << IPSET_DUMP_LAST_FLAG),
217 typedef int (*ipset_adtfn)(struct ip_set *set, void *value,
218 u32 timeout, u32 flags);
220 /* Set type, variant-specific part */
221 struct ip_set_type_variant {
222 /* Kernelspace: test/add/del entries
223 * returns negative error code,
224 * zero for no match/success to add/delete
225 * positive for matching element */
226 int (*kadt)(struct ip_set *set, const struct sk_buff * skb,
227 enum ipset_adt adt, u8 pf, u8 dim, u8 flags);
229 /* Userspace: test/add/del entries
230 * returns negative error code,
231 * zero for no match/success to add/delete
232 * positive for matching element */
233 int (*uadt)(struct ip_set *set, struct nlattr *tb[],
234 enum ipset_adt adt, u32 *lineno, u32 flags);
236 /* Low level add/del/test functions */
237 ipset_adtfn adt[IPSET_ADT_MAX];
239 /* When adding entries and set is full, try to resize the set */
240 int (*resize)(struct ip_set *set, bool retried);
241 /* Destroy the set */
242 void (*destroy)(struct ip_set *set);
243 /* Flush the elements */
244 void (*flush)(struct ip_set *set);
245 /* Expire entries before listing */
246 void (*expire)(struct ip_set *set);
247 /* List set header data */
248 int (*head)(struct ip_set *set, struct sk_buff *skb);
250 int (*list)(const struct ip_set *set, struct sk_buff *skb,
251 struct netlink_callback *cb);
253 /* Return true if "b" set is the same as "a"
254 * according to the create set parameters */
255 bool (*same_set)(const struct ip_set *a, const struct ip_set *b);
258 /* The core set type structure */
260 struct list_head list;
263 char name[IPSET_MAXNAMELEN];
264 /* Protocol version */
266 /* Set features to control swapping */
268 /* Set type dimension */
270 /* Supported family: may be AF_UNSPEC for both AF_INET/AF_INET6 */
276 int (*create)(struct ip_set *set, struct nlattr *tb[], u32 flags);
278 /* Attribute policies */
279 const struct nla_policy create_policy[IPSET_ATTR_CREATE_MAX + 1];
280 const struct nla_policy adt_policy[IPSET_ATTR_ADT_MAX + 1];
282 /* Set this to THIS_MODULE if you are a module, otherwise NULL */
286 /* register and unregister set type */
287 extern int ip_set_type_register(struct ip_set_type *set_type);
288 extern void ip_set_type_unregister(struct ip_set_type *set_type);
290 /* A generic IP set */
292 /* The name of the set */
293 char name[IPSET_MAXNAMELEN];
294 /* Lock protecting the set data */
296 /* References to the set */
298 /* The core set type */
299 struct ip_set_type *type;
300 /* The type variant doing the real job */
301 const struct ip_set_type_variant *variant;
302 /* The actual INET family of the set */
304 /* The type specific data */
308 /* register and unregister set references */
309 extern ip_set_id_t ip_set_get_byname(const char *name, struct ip_set **set);
310 extern void ip_set_put_byindex(ip_set_id_t index);
311 extern const char * ip_set_name_byindex(ip_set_id_t index);
312 extern ip_set_id_t ip_set_nfnl_get(const char *name);
313 extern ip_set_id_t ip_set_nfnl_get_byindex(ip_set_id_t index);
314 extern void ip_set_nfnl_put(ip_set_id_t index);
316 /* API for iptables set match, and SET target */
317 extern int ip_set_add(ip_set_id_t id, const struct sk_buff *skb,
318 u8 family, u8 dim, u8 flags);
319 extern int ip_set_del(ip_set_id_t id, const struct sk_buff *skb,
320 u8 family, u8 dim, u8 flags);
321 extern int ip_set_test(ip_set_id_t id, const struct sk_buff *skb,
322 u8 family, u8 dim, u8 flags);
324 /* Utility functions */
325 extern void * ip_set_alloc(size_t size);
326 extern void ip_set_free(void *members);
327 extern int ip_set_get_ipaddr4(struct nlattr *nla, __be32 *ipaddr);
328 extern int ip_set_get_ipaddr6(struct nlattr *nla, union nf_inet_addr *ipaddr);
331 ip_set_get_hostipaddr4(struct nlattr *nla, u32 *ipaddr)
334 int ret = ip_set_get_ipaddr4(nla, &ip);
342 /* Ignore IPSET_ERR_EXIST errors if asked to do so? */
344 ip_set_eexist(int ret, u32 flags)
346 return ret == -IPSET_ERR_EXIST && (flags & IPSET_FLAG_EXIST);
349 /* Check the NLA_F_NET_BYTEORDER flag */
351 ip_set_attr_netorder(struct nlattr *tb[], int type)
353 return tb[type] && (tb[type]->nla_type & NLA_F_NET_BYTEORDER);
357 ip_set_optattr_netorder(struct nlattr *tb[], int type)
359 return !tb[type] || (tb[type]->nla_type & NLA_F_NET_BYTEORDER);
362 /* Useful converters */
364 ip_set_get_h32(const struct nlattr *attr)
366 return ntohl(nla_get_be32(attr));
370 ip_set_get_h16(const struct nlattr *attr)
372 return ntohs(nla_get_be16(attr));
375 #define ipset_nest_start(skb, attr) nla_nest_start(skb, attr | NLA_F_NESTED)
376 #define ipset_nest_end(skb, start) nla_nest_end(skb, start)
378 #define NLA_PUT_IPADDR4(skb, type, ipaddr) \
380 struct nlattr *__nested = ipset_nest_start(skb, type); \
383 goto nla_put_failure; \
384 NLA_PUT_NET32(skb, IPSET_ATTR_IPADDR_IPV4, ipaddr); \
385 ipset_nest_end(skb, __nested); \
388 #define NLA_PUT_IPADDR6(skb, type, ipaddrptr) \
390 struct nlattr *__nested = ipset_nest_start(skb, type); \
393 goto nla_put_failure; \
394 NLA_PUT(skb, IPSET_ATTR_IPADDR_IPV6, \
395 sizeof(struct in6_addr), ipaddrptr); \
396 ipset_nest_end(skb, __nested); \
399 /* Get address from skbuff */
401 ip4addr(const struct sk_buff *skb, bool src)
403 return src ? ip_hdr(skb)->saddr : ip_hdr(skb)->daddr;
407 ip4addrptr(const struct sk_buff *skb, bool src, __be32 *addr)
409 *addr = src ? ip_hdr(skb)->saddr : ip_hdr(skb)->daddr;
413 ip6addrptr(const struct sk_buff *skb, bool src, struct in6_addr *addr)
415 memcpy(addr, src ? &ipv6_hdr(skb)->saddr : &ipv6_hdr(skb)->daddr,
419 /* Calculate the bytes required to store the inclusive range of a-b */
421 bitmap_bytes(u32 a, u32 b)
423 return 4 * ((((b - a + 8) / 8) + 3) / 4);
426 /* Interface to iptables/ip6tables */
430 union ip_set_name_index {
431 char name[IPSET_MAXNAMELEN];
435 #define IP_SET_OP_GET_BYNAME 0x00000006 /* Get set index by name */
436 struct ip_set_req_get_set {
439 union ip_set_name_index set;
442 #define IP_SET_OP_GET_BYINDEX 0x00000007 /* Get set name by index */
443 /* Uses ip_set_req_get_set */
445 #define IP_SET_OP_VERSION 0x00000100 /* Ask kernel version */
446 struct ip_set_req_version {
451 #endif /* __KERNEL__ */
453 #endif /*_IP_SET_H */