1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
3 * Patrick Schaaf <bof@bof.de>
4 * Martin Josefsson <gandalf@wlug.westbo.se>
5 * Copyright (C) 2003-2013 Jozsef Kadlecsik <kadlec@netfilter.org>
11 #include <linux/ipv6.h>
12 #include <linux/netlink.h>
13 #include <linux/netfilter.h>
14 #include <linux/netfilter/x_tables.h>
15 #include <linux/stringify.h>
16 #include <linux/vmalloc.h>
17 #include <net/netlink.h>
18 #include <uapi/linux/netfilter/ipset/ip_set.h>
20 #define _IP_SET_MODULE_DESC(a, b, c) \
21 MODULE_DESCRIPTION(a " type of IP sets, revisions " b "-" c)
22 #define IP_SET_MODULE_DESC(a, b, c) \
23 _IP_SET_MODULE_DESC(a, __stringify(b), __stringify(c))
27 IPSET_TYPE_IP_FLAG = 0,
28 IPSET_TYPE_IP = (1 << IPSET_TYPE_IP_FLAG),
29 IPSET_TYPE_PORT_FLAG = 1,
30 IPSET_TYPE_PORT = (1 << IPSET_TYPE_PORT_FLAG),
31 IPSET_TYPE_MAC_FLAG = 2,
32 IPSET_TYPE_MAC = (1 << IPSET_TYPE_MAC_FLAG),
33 IPSET_TYPE_IP2_FLAG = 3,
34 IPSET_TYPE_IP2 = (1 << IPSET_TYPE_IP2_FLAG),
35 IPSET_TYPE_NAME_FLAG = 4,
36 IPSET_TYPE_NAME = (1 << IPSET_TYPE_NAME_FLAG),
37 IPSET_TYPE_IFACE_FLAG = 5,
38 IPSET_TYPE_IFACE = (1 << IPSET_TYPE_IFACE_FLAG),
39 IPSET_TYPE_MARK_FLAG = 6,
40 IPSET_TYPE_MARK = (1 << IPSET_TYPE_MARK_FLAG),
41 IPSET_TYPE_NOMATCH_FLAG = 7,
42 IPSET_TYPE_NOMATCH = (1 << IPSET_TYPE_NOMATCH_FLAG),
43 /* Strictly speaking not a feature, but a flag for dumping:
44 * this settype must be dumped last */
45 IPSET_DUMP_LAST_FLAG = 8,
46 IPSET_DUMP_LAST = (1 << IPSET_DUMP_LAST_FLAG),
50 enum ip_set_extension {
51 IPSET_EXT_BIT_TIMEOUT = 0,
52 IPSET_EXT_TIMEOUT = (1 << IPSET_EXT_BIT_TIMEOUT),
53 IPSET_EXT_BIT_COUNTER = 1,
54 IPSET_EXT_COUNTER = (1 << IPSET_EXT_BIT_COUNTER),
55 IPSET_EXT_BIT_COMMENT = 2,
56 IPSET_EXT_COMMENT = (1 << IPSET_EXT_BIT_COMMENT),
57 IPSET_EXT_BIT_SKBINFO = 3,
58 IPSET_EXT_SKBINFO = (1 << IPSET_EXT_BIT_SKBINFO),
59 /* Mark set with an extension which needs to call destroy */
60 IPSET_EXT_BIT_DESTROY = 7,
61 IPSET_EXT_DESTROY = (1 << IPSET_EXT_BIT_DESTROY),
64 #define SET_WITH_TIMEOUT(s) ((s)->extensions & IPSET_EXT_TIMEOUT)
65 #define SET_WITH_COUNTER(s) ((s)->extensions & IPSET_EXT_COUNTER)
66 #define SET_WITH_COMMENT(s) ((s)->extensions & IPSET_EXT_COMMENT)
67 #define SET_WITH_SKBINFO(s) ((s)->extensions & IPSET_EXT_SKBINFO)
68 #define SET_WITH_FORCEADD(s) ((s)->flags & IPSET_CREATE_FLAG_FORCEADD)
70 /* Extension id, in size order */
72 IPSET_EXT_ID_COUNTER = 0,
82 struct ip_set_ext_type {
83 /* Destroy extension private data (can be NULL) */
84 void (*destroy)(struct ip_set *set, void *ext);
85 enum ip_set_extension type;
86 enum ipset_cadt_flags flag;
87 /* Size and minimal alignment */
92 extern const struct ip_set_ext_type ip_set_extensions[];
94 struct ip_set_counter {
99 struct ip_set_comment_rcu {
104 struct ip_set_comment {
105 struct ip_set_comment_rcu __rcu *c;
108 struct ip_set_skbinfo {
117 struct ip_set_skbinfo skbinfo;
128 #define ext_timeout(e, s) \
129 ((unsigned long *)(((void *)(e)) + (s)->offset[IPSET_EXT_ID_TIMEOUT]))
130 #define ext_counter(e, s) \
131 ((struct ip_set_counter *)(((void *)(e)) + (s)->offset[IPSET_EXT_ID_COUNTER]))
132 #define ext_comment(e, s) \
133 ((struct ip_set_comment *)(((void *)(e)) + (s)->offset[IPSET_EXT_ID_COMMENT]))
134 #define ext_skbinfo(e, s) \
135 ((struct ip_set_skbinfo *)(((void *)(e)) + (s)->offset[IPSET_EXT_ID_SKBINFO]))
137 typedef int (*ipset_adtfn)(struct ip_set *set, void *value,
138 const struct ip_set_ext *ext,
139 struct ip_set_ext *mext, u32 cmdflags);
141 /* Kernel API function options */
142 struct ip_set_adt_opt {
143 u8 family; /* Actual protocol family */
144 u8 dim; /* Dimension of match/target */
145 u8 flags; /* Direction and negation flags */
146 u32 cmdflags; /* Command-like flags */
147 struct ip_set_ext ext; /* Extensions */
150 /* Set type, variant-specific part */
151 struct ip_set_type_variant {
152 /* Kernelspace: test/add/del entries
153 * returns negative error code,
154 * zero for no match/success to add/delete
155 * positive for matching element */
156 int (*kadt)(struct ip_set *set, const struct sk_buff *skb,
157 const struct xt_action_param *par,
158 enum ipset_adt adt, struct ip_set_adt_opt *opt);
160 /* Userspace: test/add/del entries
161 * returns negative error code,
162 * zero for no match/success to add/delete
163 * positive for matching element */
164 int (*uadt)(struct ip_set *set, struct nlattr *tb[],
165 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried);
167 /* Low level add/del/test functions */
168 ipset_adtfn adt[IPSET_ADT_MAX];
170 /* When adding entries and set is full, try to resize the set */
171 int (*resize)(struct ip_set *set, bool retried);
172 /* Destroy the set */
173 void (*destroy)(struct ip_set *set);
174 /* Flush the elements */
175 void (*flush)(struct ip_set *set);
176 /* Expire entries before listing */
177 void (*expire)(struct ip_set *set);
178 /* List set header data */
179 int (*head)(struct ip_set *set, struct sk_buff *skb);
181 int (*list)(const struct ip_set *set, struct sk_buff *skb,
182 struct netlink_callback *cb);
183 /* Keep listing private when resizing runs parallel */
184 void (*uref)(struct ip_set *set, struct netlink_callback *cb,
187 /* Return true if "b" set is the same as "a"
188 * according to the create set parameters */
189 bool (*same_set)(const struct ip_set *a, const struct ip_set *b);
192 /* The core set type structure */
194 struct list_head list;
197 char name[IPSET_MAXNAMELEN];
198 /* Protocol version */
200 /* Set type dimension */
203 * Supported family: may be NFPROTO_UNSPEC for both
204 * NFPROTO_IPV4/NFPROTO_IPV6.
208 u8 revision_min, revision_max;
209 /* Set features to control swapping */
213 int (*create)(struct net *net, struct ip_set *set,
214 struct nlattr *tb[], u32 flags);
216 /* Attribute policies */
217 const struct nla_policy create_policy[IPSET_ATTR_CREATE_MAX + 1];
218 const struct nla_policy adt_policy[IPSET_ATTR_ADT_MAX + 1];
220 /* Set this to THIS_MODULE if you are a module, otherwise NULL */
224 /* register and unregister set type */
225 extern int ip_set_type_register(struct ip_set_type *set_type);
226 extern void ip_set_type_unregister(struct ip_set_type *set_type);
228 /* A generic IP set */
230 /* The name of the set */
231 char name[IPSET_MAXNAMELEN];
232 /* Lock protecting the set data */
234 /* References to the set */
236 /* References to the set for netlink events like dump,
237 * ref can be swapped out by ip_set_swap
240 /* The core set type */
241 struct ip_set_type *type;
242 /* The type variant doing the real job */
243 const struct ip_set_type_variant *variant;
244 /* The actual INET family of the set */
246 /* The type revision */
252 /* Default timeout value, if enabled */
254 /* Number of elements (vs timeout) */
256 /* Size of the dynamic extensions (vs timeout) */
258 /* Element data size */
260 /* Offsets to extensions in elements */
261 size_t offset[IPSET_EXT_ID_MAX];
262 /* The type specific data */
267 ip_set_ext_destroy(struct ip_set *set, void *data)
269 /* Check that the extension is enabled for the set and
270 * call it's destroy function for its extension part in data.
272 if (SET_WITH_COMMENT(set)) {
273 struct ip_set_comment *c = ext_comment(data, set);
275 ip_set_extensions[IPSET_EXT_ID_COMMENT].destroy(set, c);
279 int ip_set_put_flags(struct sk_buff *skb, struct ip_set *set);
281 /* Netlink CB args */
283 IPSET_CB_NET = 0, /* net namespace */
284 IPSET_CB_PROTO, /* ipset protocol */
285 IPSET_CB_DUMP, /* dump single set/all sets */
286 IPSET_CB_INDEX, /* set index */
287 IPSET_CB_PRIVATE, /* set private data */
288 IPSET_CB_ARG0, /* type specific */
291 /* register and unregister set references */
292 extern ip_set_id_t ip_set_get_byname(struct net *net,
293 const char *name, struct ip_set **set);
294 extern void ip_set_put_byindex(struct net *net, ip_set_id_t index);
295 extern void ip_set_name_byindex(struct net *net, ip_set_id_t index, char *name);
296 extern ip_set_id_t ip_set_nfnl_get_byindex(struct net *net, ip_set_id_t index);
297 extern void ip_set_nfnl_put(struct net *net, ip_set_id_t index);
299 /* API for iptables set match, and SET target */
301 extern int ip_set_add(ip_set_id_t id, const struct sk_buff *skb,
302 const struct xt_action_param *par,
303 struct ip_set_adt_opt *opt);
304 extern int ip_set_del(ip_set_id_t id, const struct sk_buff *skb,
305 const struct xt_action_param *par,
306 struct ip_set_adt_opt *opt);
307 extern int ip_set_test(ip_set_id_t id, const struct sk_buff *skb,
308 const struct xt_action_param *par,
309 struct ip_set_adt_opt *opt);
311 /* Utility functions */
312 extern void *ip_set_alloc(size_t size);
313 extern void ip_set_free(void *members);
314 extern int ip_set_get_ipaddr4(struct nlattr *nla, __be32 *ipaddr);
315 extern int ip_set_get_ipaddr6(struct nlattr *nla, union nf_inet_addr *ipaddr);
316 extern size_t ip_set_elem_len(struct ip_set *set, struct nlattr *tb[],
317 size_t len, size_t align);
318 extern int ip_set_get_extensions(struct ip_set *set, struct nlattr *tb[],
319 struct ip_set_ext *ext);
320 extern int ip_set_put_extensions(struct sk_buff *skb, const struct ip_set *set,
321 const void *e, bool active);
322 extern bool ip_set_match_extensions(struct ip_set *set,
323 const struct ip_set_ext *ext,
324 struct ip_set_ext *mext,
325 u32 flags, void *data);
328 ip_set_get_hostipaddr4(struct nlattr *nla, u32 *ipaddr)
331 int ret = ip_set_get_ipaddr4(nla, &ip);
339 /* Ignore IPSET_ERR_EXIST errors if asked to do so? */
341 ip_set_eexist(int ret, u32 flags)
343 return ret == -IPSET_ERR_EXIST && (flags & IPSET_FLAG_EXIST);
346 /* Match elements marked with nomatch */
348 ip_set_enomatch(int ret, u32 flags, enum ipset_adt adt, struct ip_set *set)
350 return adt == IPSET_TEST &&
351 (set->type->features & IPSET_TYPE_NOMATCH) &&
352 ((flags >> 16) & IPSET_FLAG_NOMATCH) &&
353 (ret > 0 || ret == -ENOTEMPTY);
356 /* Check the NLA_F_NET_BYTEORDER flag */
358 ip_set_attr_netorder(struct nlattr *tb[], int type)
360 return tb[type] && (tb[type]->nla_type & NLA_F_NET_BYTEORDER);
364 ip_set_optattr_netorder(struct nlattr *tb[], int type)
366 return !tb[type] || (tb[type]->nla_type & NLA_F_NET_BYTEORDER);
369 /* Useful converters */
371 ip_set_get_h32(const struct nlattr *attr)
373 return ntohl(nla_get_be32(attr));
377 ip_set_get_h16(const struct nlattr *attr)
379 return ntohs(nla_get_be16(attr));
382 static inline int nla_put_ipaddr4(struct sk_buff *skb, int type, __be32 ipaddr)
384 struct nlattr *__nested = nla_nest_start(skb, type);
389 ret = nla_put_in_addr(skb, IPSET_ATTR_IPADDR_IPV4, ipaddr);
391 nla_nest_end(skb, __nested);
395 static inline int nla_put_ipaddr6(struct sk_buff *skb, int type,
396 const struct in6_addr *ipaddrptr)
398 struct nlattr *__nested = nla_nest_start(skb, type);
403 ret = nla_put_in6_addr(skb, IPSET_ATTR_IPADDR_IPV6, ipaddrptr);
405 nla_nest_end(skb, __nested);
409 /* Get address from skbuff */
411 ip4addr(const struct sk_buff *skb, bool src)
413 return src ? ip_hdr(skb)->saddr : ip_hdr(skb)->daddr;
417 ip4addrptr(const struct sk_buff *skb, bool src, __be32 *addr)
419 *addr = src ? ip_hdr(skb)->saddr : ip_hdr(skb)->daddr;
423 ip6addrptr(const struct sk_buff *skb, bool src, struct in6_addr *addr)
425 memcpy(addr, src ? &ipv6_hdr(skb)->saddr : &ipv6_hdr(skb)->daddr,
429 /* How often should the gc be run by default */
430 #define IPSET_GC_TIME (3 * 60)
432 /* Timeout period depending on the timeout value of the given set */
433 #define IPSET_GC_PERIOD(timeout) \
434 ((timeout/3) ? min_t(u32, (timeout)/3, IPSET_GC_TIME) : 1)
436 /* Entry is set with no timeout value */
437 #define IPSET_ELEM_PERMANENT 0
439 /* Set is defined with timeout support: timeout value may be 0 */
440 #define IPSET_NO_TIMEOUT UINT_MAX
442 /* Max timeout value, see msecs_to_jiffies() in jiffies.h */
443 #define IPSET_MAX_TIMEOUT (UINT_MAX >> 1)/MSEC_PER_SEC
445 #define ip_set_adt_opt_timeout(opt, set) \
446 ((opt)->ext.timeout != IPSET_NO_TIMEOUT ? (opt)->ext.timeout : (set)->timeout)
448 static inline unsigned int
449 ip_set_timeout_uget(struct nlattr *tb)
451 unsigned int timeout = ip_set_get_h32(tb);
453 /* Normalize to fit into jiffies */
454 if (timeout > IPSET_MAX_TIMEOUT)
455 timeout = IPSET_MAX_TIMEOUT;
461 ip_set_timeout_expired(const unsigned long *t)
463 return *t != IPSET_ELEM_PERMANENT && time_is_before_jiffies(*t);
467 ip_set_timeout_set(unsigned long *timeout, u32 value)
472 *timeout = IPSET_ELEM_PERMANENT;
476 t = msecs_to_jiffies(value * MSEC_PER_SEC) + jiffies;
477 if (t == IPSET_ELEM_PERMANENT)
483 void ip_set_init_comment(struct ip_set *set, struct ip_set_comment *comment,
484 const struct ip_set_ext *ext);
487 ip_set_init_counter(struct ip_set_counter *counter,
488 const struct ip_set_ext *ext)
490 if (ext->bytes != ULLONG_MAX)
491 atomic64_set(&(counter)->bytes, (long long)(ext->bytes));
492 if (ext->packets != ULLONG_MAX)
493 atomic64_set(&(counter)->packets, (long long)(ext->packets));
497 ip_set_init_skbinfo(struct ip_set_skbinfo *skbinfo,
498 const struct ip_set_ext *ext)
500 *skbinfo = ext->skbinfo;
503 #define IP_SET_INIT_KEXT(skb, opt, set) \
504 { .bytes = (skb)->len, .packets = 1, \
505 .timeout = ip_set_adt_opt_timeout(opt, set) }
507 #define IP_SET_INIT_UEXT(set) \
508 { .bytes = ULLONG_MAX, .packets = ULLONG_MAX, \
509 .timeout = (set)->timeout }
511 #define IPSET_CONCAT(a, b) a##b
512 #define IPSET_TOKEN(a, b) IPSET_CONCAT(a, b)
514 #endif /*_IP_SET_H */