1 /* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
2 * Patrick Schaaf <bof@bof.de>
3 * Martin Josefsson <gandalf@wlug.westbo.se>
4 * Copyright (C) 2003-2013 Jozsef Kadlecsik <kadlec@netfilter.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
14 #include <linux/ipv6.h>
15 #include <linux/netlink.h>
16 #include <linux/netfilter.h>
17 #include <linux/netfilter/x_tables.h>
18 #include <linux/stringify.h>
19 #include <linux/vmalloc.h>
20 #include <net/netlink.h>
21 #include <linux/netfilter/ipset/ip_set_compat.h>
22 #include <uapi/linux/netfilter/ipset/ip_set.h>
24 #define _IP_SET_MODULE_DESC(a, b, c) \
25 MODULE_DESCRIPTION(a " type of IP sets, revisions " b "-" c)
26 #define IP_SET_MODULE_DESC(a, b, c) \
27 _IP_SET_MODULE_DESC(a, __stringify(b), __stringify(c))
31 IPSET_TYPE_IP_FLAG = 0,
32 IPSET_TYPE_IP = (1 << IPSET_TYPE_IP_FLAG),
33 IPSET_TYPE_PORT_FLAG = 1,
34 IPSET_TYPE_PORT = (1 << IPSET_TYPE_PORT_FLAG),
35 IPSET_TYPE_MAC_FLAG = 2,
36 IPSET_TYPE_MAC = (1 << IPSET_TYPE_MAC_FLAG),
37 IPSET_TYPE_IP2_FLAG = 3,
38 IPSET_TYPE_IP2 = (1 << IPSET_TYPE_IP2_FLAG),
39 IPSET_TYPE_NAME_FLAG = 4,
40 IPSET_TYPE_NAME = (1 << IPSET_TYPE_NAME_FLAG),
41 IPSET_TYPE_IFACE_FLAG = 5,
42 IPSET_TYPE_IFACE = (1 << IPSET_TYPE_IFACE_FLAG),
43 IPSET_TYPE_MARK_FLAG = 6,
44 IPSET_TYPE_MARK = (1 << IPSET_TYPE_MARK_FLAG),
45 IPSET_TYPE_NOMATCH_FLAG = 7,
46 IPSET_TYPE_NOMATCH = (1 << IPSET_TYPE_NOMATCH_FLAG),
47 /* Strictly speaking not a feature, but a flag for dumping:
48 * this settype must be dumped last */
49 IPSET_DUMP_LAST_FLAG = 8,
50 IPSET_DUMP_LAST = (1 << IPSET_DUMP_LAST_FLAG),
54 enum ip_set_extension {
55 IPSET_EXT_BIT_TIMEOUT = 0,
56 IPSET_EXT_TIMEOUT = (1 << IPSET_EXT_BIT_TIMEOUT),
57 IPSET_EXT_BIT_COUNTER = 1,
58 IPSET_EXT_COUNTER = (1 << IPSET_EXT_BIT_COUNTER),
59 IPSET_EXT_BIT_COMMENT = 2,
60 IPSET_EXT_COMMENT = (1 << IPSET_EXT_BIT_COMMENT),
61 IPSET_EXT_BIT_SKBINFO = 3,
62 IPSET_EXT_SKBINFO = (1 << IPSET_EXT_BIT_SKBINFO),
63 /* Mark set with an extension which needs to call destroy */
64 IPSET_EXT_BIT_DESTROY = 7,
65 IPSET_EXT_DESTROY = (1 << IPSET_EXT_BIT_DESTROY),
68 #define SET_WITH_TIMEOUT(s) ((s)->extensions & IPSET_EXT_TIMEOUT)
69 #define SET_WITH_COUNTER(s) ((s)->extensions & IPSET_EXT_COUNTER)
70 #define SET_WITH_COMMENT(s) ((s)->extensions & IPSET_EXT_COMMENT)
71 #define SET_WITH_SKBINFO(s) ((s)->extensions & IPSET_EXT_SKBINFO)
72 #define SET_WITH_FORCEADD(s) ((s)->flags & IPSET_CREATE_FLAG_FORCEADD)
74 /* Extension id, in size order */
76 IPSET_EXT_ID_COUNTER = 0,
86 struct ip_set_ext_type {
87 /* Destroy extension private data (can be NULL) */
88 void (*destroy)(struct ip_set *set, void *ext);
89 enum ip_set_extension type;
90 enum ipset_cadt_flags flag;
91 /* Size and minimal alignment */
96 extern const struct ip_set_ext_type ip_set_extensions[];
98 struct ip_set_counter {
103 struct ip_set_comment_rcu {
108 struct ip_set_comment {
109 struct ip_set_comment_rcu __rcu *c;
112 struct ip_set_skbinfo {
121 struct ip_set_skbinfo skbinfo;
132 #define ext_timeout(e, s) \
133 ((unsigned long *)(((void *)(e)) + (s)->offset[IPSET_EXT_ID_TIMEOUT]))
134 #define ext_counter(e, s) \
135 ((struct ip_set_counter *)(((void *)(e)) + (s)->offset[IPSET_EXT_ID_COUNTER]))
136 #define ext_comment(e, s) \
137 ((struct ip_set_comment *)(((void *)(e)) + (s)->offset[IPSET_EXT_ID_COMMENT]))
138 #define ext_skbinfo(e, s) \
139 ((struct ip_set_skbinfo *)(((void *)(e)) + (s)->offset[IPSET_EXT_ID_SKBINFO]))
141 typedef int (*ipset_adtfn)(struct ip_set *set, void *value,
142 const struct ip_set_ext *ext,
143 struct ip_set_ext *mext, u32 cmdflags);
145 /* Kernel API function options */
146 struct ip_set_adt_opt {
147 u8 family; /* Actual protocol family */
148 u8 dim; /* Dimension of match/target */
149 u8 flags; /* Direction and negation flags */
150 u32 cmdflags; /* Command-like flags */
151 struct ip_set_ext ext; /* Extensions */
154 /* Set type, variant-specific part */
155 struct ip_set_type_variant {
156 /* Kernelspace: test/add/del entries
157 * returns negative error code,
158 * zero for no match/success to add/delete
159 * positive for matching element */
160 int (*kadt)(struct ip_set *set, const struct sk_buff *skb,
161 const struct xt_action_param *par,
162 enum ipset_adt adt, struct ip_set_adt_opt *opt);
164 /* Userspace: test/add/del entries
165 * returns negative error code,
166 * zero for no match/success to add/delete
167 * positive for matching element */
168 int (*uadt)(struct ip_set *set, struct nlattr *tb[],
169 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried);
171 /* Low level add/del/test functions */
172 ipset_adtfn adt[IPSET_ADT_MAX];
174 /* When adding entries and set is full, try to resize the set */
175 int (*resize)(struct ip_set *set, bool retried);
176 /* Destroy the set */
177 void (*destroy)(struct ip_set *set);
178 /* Flush the elements */
179 void (*flush)(struct ip_set *set);
180 /* Expire entries before listing */
181 void (*expire)(struct ip_set *set);
182 /* List set header data */
183 int (*head)(struct ip_set *set, struct sk_buff *skb);
185 int (*list)(const struct ip_set *set, struct sk_buff *skb,
186 struct netlink_callback *cb);
187 /* Keep listing private when resizing runs parallel */
188 void (*uref)(struct ip_set *set, struct netlink_callback *cb,
191 /* Return true if "b" set is the same as "a"
192 * according to the create set parameters */
193 bool (*same_set)(const struct ip_set *a, const struct ip_set *b);
196 /* The core set type structure */
198 struct list_head list;
201 char name[IPSET_MAXNAMELEN];
202 /* Protocol version */
204 /* Set type dimension */
207 * Supported family: may be NFPROTO_UNSPEC for both
208 * NFPROTO_IPV4/NFPROTO_IPV6.
212 u8 revision_min, revision_max;
213 /* Set features to control swapping */
217 int (*create)(struct net *net, struct ip_set *set,
218 struct nlattr *tb[], u32 flags);
220 /* Attribute policies */
221 const struct nla_policy create_policy[IPSET_ATTR_CREATE_MAX + 1];
222 const struct nla_policy adt_policy[IPSET_ATTR_ADT_MAX + 1];
224 /* Set this to THIS_MODULE if you are a module, otherwise NULL */
228 /* register and unregister set type */
229 extern int ip_set_type_register(struct ip_set_type *set_type);
230 extern void ip_set_type_unregister(struct ip_set_type *set_type);
232 /* A generic IP set */
234 /* The name of the set */
235 char name[IPSET_MAXNAMELEN];
236 /* Lock protecting the set data */
238 /* References to the set */
240 /* References to the set for netlink events like dump,
241 * ref can be swapped out by ip_set_swap
244 /* The core set type */
245 struct ip_set_type *type;
246 /* The type variant doing the real job */
247 const struct ip_set_type_variant *variant;
248 /* The actual INET family of the set */
250 /* The type revision */
256 /* Default timeout value, if enabled */
258 /* Number of elements (vs timeout) */
260 /* Size of the dynamic extensions (vs timeout) */
262 /* Element data size */
264 /* Offsets to extensions in elements */
265 size_t offset[IPSET_EXT_ID_MAX];
266 /* The type specific data */
271 ip_set_ext_destroy(struct ip_set *set, void *data)
273 /* Check that the extension is enabled for the set and
274 * call it's destroy function for its extension part in data.
276 if (SET_WITH_COMMENT(set)) {
277 struct ip_set_comment *c = ext_comment(data, set);
279 ip_set_extensions[IPSET_EXT_ID_COMMENT].destroy(set, c);
284 ip_set_put_flags(struct sk_buff *skb, struct ip_set *set)
288 if (SET_WITH_TIMEOUT(set))
289 if (unlikely(nla_put_net32(skb, IPSET_ATTR_TIMEOUT,
290 htonl(set->timeout))))
292 if (SET_WITH_COUNTER(set))
293 cadt_flags |= IPSET_FLAG_WITH_COUNTERS;
294 if (SET_WITH_COMMENT(set))
295 cadt_flags |= IPSET_FLAG_WITH_COMMENT;
296 if (SET_WITH_SKBINFO(set))
297 cadt_flags |= IPSET_FLAG_WITH_SKBINFO;
298 if (SET_WITH_FORCEADD(set))
299 cadt_flags |= IPSET_FLAG_WITH_FORCEADD;
303 return nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(cadt_flags));
306 /* Netlink CB args */
308 IPSET_CB_NET = 0, /* net namespace */
309 IPSET_CB_PROTO, /* ipset protocol */
310 IPSET_CB_DUMP, /* dump single set/all sets */
311 IPSET_CB_INDEX, /* set index */
312 IPSET_CB_PRIVATE, /* set private data */
313 IPSET_CB_ARG0, /* type specific */
316 /* register and unregister set references */
317 extern ip_set_id_t ip_set_get_byname(struct net *net,
318 const char *name, struct ip_set **set);
319 extern void ip_set_put_byindex(struct net *net, ip_set_id_t index);
320 extern void ip_set_name_byindex(struct net *net, ip_set_id_t index, char *name);
321 extern ip_set_id_t ip_set_nfnl_get_byindex(struct net *net, ip_set_id_t index);
322 extern void ip_set_nfnl_put(struct net *net, ip_set_id_t index);
324 /* API for iptables set match, and SET target */
326 extern int ip_set_add(ip_set_id_t id, const struct sk_buff *skb,
327 const struct xt_action_param *par,
328 struct ip_set_adt_opt *opt);
329 extern int ip_set_del(ip_set_id_t id, const struct sk_buff *skb,
330 const struct xt_action_param *par,
331 struct ip_set_adt_opt *opt);
332 extern int ip_set_test(ip_set_id_t id, const struct sk_buff *skb,
333 const struct xt_action_param *par,
334 struct ip_set_adt_opt *opt);
336 /* Utility functions */
337 extern void *ip_set_alloc(size_t size);
338 extern void ip_set_free(void *members);
339 extern int ip_set_get_ipaddr4(struct nlattr *nla, __be32 *ipaddr);
340 extern int ip_set_get_ipaddr6(struct nlattr *nla, union nf_inet_addr *ipaddr);
341 extern size_t ip_set_elem_len(struct ip_set *set, struct nlattr *tb[],
342 size_t len, size_t align);
343 extern int ip_set_get_extensions(struct ip_set *set, struct nlattr *tb[],
344 struct ip_set_ext *ext);
345 extern int ip_set_put_extensions(struct sk_buff *skb, const struct ip_set *set,
346 const void *e, bool active);
347 extern bool ip_set_match_extensions(struct ip_set *set,
348 const struct ip_set_ext *ext,
349 struct ip_set_ext *mext,
350 u32 flags, void *data);
353 ip_set_get_hostipaddr4(struct nlattr *nla, u32 *ipaddr)
356 int ret = ip_set_get_ipaddr4(nla, &ip);
364 /* Ignore IPSET_ERR_EXIST errors if asked to do so? */
366 ip_set_eexist(int ret, u32 flags)
368 return ret == -IPSET_ERR_EXIST && (flags & IPSET_FLAG_EXIST);
371 /* Match elements marked with nomatch */
373 ip_set_enomatch(int ret, u32 flags, enum ipset_adt adt, struct ip_set *set)
375 return adt == IPSET_TEST &&
376 (set->type->features & IPSET_TYPE_NOMATCH) &&
377 ((flags >> 16) & IPSET_FLAG_NOMATCH) &&
378 (ret > 0 || ret == -ENOTEMPTY);
381 /* Check the NLA_F_NET_BYTEORDER flag */
383 ip_set_attr_netorder(struct nlattr *tb[], int type)
385 return tb[type] && (tb[type]->nla_type & NLA_F_NET_BYTEORDER);
389 ip_set_optattr_netorder(struct nlattr *tb[], int type)
391 return !tb[type] || (tb[type]->nla_type & NLA_F_NET_BYTEORDER);
394 /* Useful converters */
396 ip_set_get_h32(const struct nlattr *attr)
398 return ntohl(nla_get_be32(attr));
402 ip_set_get_h16(const struct nlattr *attr)
404 return ntohs(nla_get_be16(attr));
407 /* In order to support older kernels before patch ae0be8de9a53cda3:
409 * netlink: make nla_nest_start() add NLA_F_NESTED flag
411 * we have to keep ipset_nest_start() ipset_nest_end()
412 * in the package source
414 #define ipset_nest_start(skb, attr) nla_nest_start(skb, attr | NLA_F_NESTED)
415 #define ipset_nest_end(skb, start) nla_nest_end(skb, start)
417 static inline int nla_put_ipaddr4(struct sk_buff *skb, int type, __be32 ipaddr)
419 struct nlattr *__nested = ipset_nest_start(skb, type);
424 ret = nla_put_in_addr(skb, IPSET_ATTR_IPADDR_IPV4, ipaddr);
426 ipset_nest_end(skb, __nested);
430 static inline int nla_put_ipaddr6(struct sk_buff *skb, int type,
431 const struct in6_addr *ipaddrptr)
433 struct nlattr *__nested = ipset_nest_start(skb, type);
438 ret = nla_put_in6_addr(skb, IPSET_ATTR_IPADDR_IPV6, ipaddrptr);
440 ipset_nest_end(skb, __nested);
444 /* Get address from skbuff */
446 ip4addr(const struct sk_buff *skb, bool src)
448 return src ? ip_hdr(skb)->saddr : ip_hdr(skb)->daddr;
452 ip4addrptr(const struct sk_buff *skb, bool src, __be32 *addr)
454 *addr = src ? ip_hdr(skb)->saddr : ip_hdr(skb)->daddr;
458 ip6addrptr(const struct sk_buff *skb, bool src, struct in6_addr *addr)
460 memcpy(addr, src ? &ipv6_hdr(skb)->saddr : &ipv6_hdr(skb)->daddr,
464 /* Calculate the bytes required to store the inclusive range of a-b */
466 bitmap_bytes(u32 a, u32 b)
468 return 4 * ((((b - a + 8) / 8) + 3) / 4);
471 /* How often should the gc be run by default */
472 #define IPSET_GC_TIME (3 * 60)
474 /* Timeout period depending on the timeout value of the given set */
475 #define IPSET_GC_PERIOD(timeout) \
476 ((timeout/3) ? min_t(u32, (timeout)/3, IPSET_GC_TIME) : 1)
478 /* Entry is set with no timeout value */
479 #define IPSET_ELEM_PERMANENT 0
481 /* Set is defined with timeout support: timeout value may be 0 */
482 #define IPSET_NO_TIMEOUT UINT_MAX
484 /* Max timeout value, see msecs_to_jiffies() in jiffies.h */
485 #define IPSET_MAX_TIMEOUT (UINT_MAX >> 1)/MSEC_PER_SEC
487 #define ip_set_adt_opt_timeout(opt, set) \
488 ((opt)->ext.timeout != IPSET_NO_TIMEOUT ? (opt)->ext.timeout : (set)->timeout)
490 static inline unsigned int
491 ip_set_timeout_uget(struct nlattr *tb)
493 unsigned int timeout = ip_set_get_h32(tb);
495 /* Normalize to fit into jiffies */
496 if (timeout > IPSET_MAX_TIMEOUT)
497 timeout = IPSET_MAX_TIMEOUT;
503 ip_set_timeout_expired(const unsigned long *t)
505 return *t != IPSET_ELEM_PERMANENT && time_is_before_jiffies(*t);
509 ip_set_timeout_set(unsigned long *timeout, u32 value)
514 *timeout = IPSET_ELEM_PERMANENT;
518 t = msecs_to_jiffies(value * MSEC_PER_SEC) + jiffies;
519 if (t == IPSET_ELEM_PERMANENT)
525 void ip_set_init_comment(struct ip_set *set, struct ip_set_comment *comment,
526 const struct ip_set_ext *ext);
529 ip_set_init_counter(struct ip_set_counter *counter,
530 const struct ip_set_ext *ext)
532 if (ext->bytes != ULLONG_MAX)
533 atomic64_set(&(counter)->bytes, (long long)(ext->bytes));
534 if (ext->packets != ULLONG_MAX)
535 atomic64_set(&(counter)->packets, (long long)(ext->packets));
539 ip_set_init_skbinfo(struct ip_set_skbinfo *skbinfo,
540 const struct ip_set_ext *ext)
542 *skbinfo = ext->skbinfo;
545 #define IP_SET_INIT_KEXT(skb, opt, set) \
546 { .bytes = (skb)->len, .packets = 1, \
547 .timeout = ip_set_adt_opt_timeout(opt, set) }
549 #define IP_SET_INIT_UEXT(set) \
550 { .bytes = ULLONG_MAX, .packets = ULLONG_MAX, \
551 .timeout = (set)->timeout }
553 #define IPSET_CONCAT(a, b) a##b
554 #define IPSET_TOKEN(a, b) IPSET_CONCAT(a, b)
556 #endif /*_IP_SET_H */