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@blackhole.kfki.hu>
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 <uapi/linux/netfilter/ipset/ip_set.h>
23 #define _IP_SET_MODULE_DESC(a, b, c) \
24 MODULE_DESCRIPTION(a " type of IP sets, revisions " b "-" c)
25 #define IP_SET_MODULE_DESC(a, b, c) \
26 _IP_SET_MODULE_DESC(a, __stringify(b), __stringify(c))
30 IPSET_TYPE_IP_FLAG = 0,
31 IPSET_TYPE_IP = (1 << IPSET_TYPE_IP_FLAG),
32 IPSET_TYPE_PORT_FLAG = 1,
33 IPSET_TYPE_PORT = (1 << IPSET_TYPE_PORT_FLAG),
34 IPSET_TYPE_MAC_FLAG = 2,
35 IPSET_TYPE_MAC = (1 << IPSET_TYPE_MAC_FLAG),
36 IPSET_TYPE_IP2_FLAG = 3,
37 IPSET_TYPE_IP2 = (1 << IPSET_TYPE_IP2_FLAG),
38 IPSET_TYPE_NAME_FLAG = 4,
39 IPSET_TYPE_NAME = (1 << IPSET_TYPE_NAME_FLAG),
40 IPSET_TYPE_IFACE_FLAG = 5,
41 IPSET_TYPE_IFACE = (1 << IPSET_TYPE_IFACE_FLAG),
42 IPSET_TYPE_NOMATCH_FLAG = 6,
43 IPSET_TYPE_NOMATCH = (1 << IPSET_TYPE_NOMATCH_FLAG),
44 /* Strictly speaking not a feature, but a flag for dumping:
45 * this settype must be dumped last */
46 IPSET_DUMP_LAST_FLAG = 7,
47 IPSET_DUMP_LAST = (1 << IPSET_DUMP_LAST_FLAG),
51 enum ip_set_extension {
52 IPSET_EXT_BIT_TIMEOUT = 0,
53 IPSET_EXT_TIMEOUT = (1 << IPSET_EXT_BIT_TIMEOUT),
54 IPSET_EXT_BIT_COUNTER = 1,
55 IPSET_EXT_COUNTER = (1 << IPSET_EXT_BIT_COUNTER),
56 IPSET_EXT_BIT_COMMENT = 2,
57 IPSET_EXT_COMMENT = (1 << IPSET_EXT_BIT_COMMENT),
58 /* Mark set with an extension which needs to call destroy */
59 IPSET_EXT_BIT_DESTROY = 7,
60 IPSET_EXT_DESTROY = (1 << IPSET_EXT_BIT_DESTROY),
63 #define SET_WITH_TIMEOUT(s) ((s)->extensions & IPSET_EXT_TIMEOUT)
64 #define SET_WITH_COUNTER(s) ((s)->extensions & IPSET_EXT_COUNTER)
65 #define SET_WITH_COMMENT(s) ((s)->extensions & IPSET_EXT_COMMENT)
67 /* Extension id, in size order */
69 IPSET_EXT_ID_COUNTER = 0,
76 struct ip_set_ext_type {
77 /* Destroy extension private data (can be NULL) */
78 void (*destroy)(void *ext);
79 enum ip_set_extension type;
80 enum ipset_cadt_flags flag;
81 /* Size and minimal alignment */
86 extern const struct ip_set_ext_type ip_set_extensions[];
95 struct ip_set_counter {
100 struct ip_set_comment {
106 #define ext_timeout(e, s) \
107 (unsigned long *)(((void *)(e)) + (s)->offset[IPSET_EXT_ID_TIMEOUT])
108 #define ext_counter(e, s) \
109 (struct ip_set_counter *)(((void *)(e)) + (s)->offset[IPSET_EXT_ID_COUNTER])
110 #define ext_comment(e, s) \
111 (struct ip_set_comment *)(((void *)(e)) + (s)->offset[IPSET_EXT_ID_COMMENT])
114 typedef int (*ipset_adtfn)(struct ip_set *set, void *value,
115 const struct ip_set_ext *ext,
116 struct ip_set_ext *mext, u32 cmdflags);
118 /* Kernel API function options */
119 struct ip_set_adt_opt {
120 u8 family; /* Actual protocol family */
121 u8 dim; /* Dimension of match/target */
122 u8 flags; /* Direction and negation flags */
123 u32 cmdflags; /* Command-like flags */
124 struct ip_set_ext ext; /* Extensions */
127 /* Set type, variant-specific part */
128 struct ip_set_type_variant {
129 /* Kernelspace: test/add/del entries
130 * returns negative error code,
131 * zero for no match/success to add/delete
132 * positive for matching element */
133 int (*kadt)(struct ip_set *set, const struct sk_buff *skb,
134 const struct xt_action_param *par,
135 enum ipset_adt adt, struct ip_set_adt_opt *opt);
137 /* Userspace: test/add/del entries
138 * returns negative error code,
139 * zero for no match/success to add/delete
140 * positive for matching element */
141 int (*uadt)(struct ip_set *set, struct nlattr *tb[],
142 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried);
144 /* Low level add/del/test functions */
145 ipset_adtfn adt[IPSET_ADT_MAX];
147 /* When adding entries and set is full, try to resize the set */
148 int (*resize)(struct ip_set *set, bool retried);
149 /* Destroy the set */
150 void (*destroy)(struct ip_set *set);
151 /* Flush the elements */
152 void (*flush)(struct ip_set *set);
153 /* Expire entries before listing */
154 void (*expire)(struct ip_set *set);
155 /* List set header data */
156 int (*head)(struct ip_set *set, struct sk_buff *skb);
158 int (*list)(const struct ip_set *set, struct sk_buff *skb,
159 struct netlink_callback *cb);
161 /* Return true if "b" set is the same as "a"
162 * according to the create set parameters */
163 bool (*same_set)(const struct ip_set *a, const struct ip_set *b);
166 /* The core set type structure */
168 struct list_head list;
171 char name[IPSET_MAXNAMELEN];
172 /* Protocol version */
174 /* Set features to control swapping */
176 /* Set type dimension */
179 * Supported family: may be NFPROTO_UNSPEC for both
180 * NFPROTO_IPV4/NFPROTO_IPV6.
184 u8 revision_min, revision_max;
187 int (*create)(struct net *net, struct ip_set *set,
188 struct nlattr *tb[], u32 flags);
190 /* Attribute policies */
191 const struct nla_policy create_policy[IPSET_ATTR_CREATE_MAX + 1];
192 const struct nla_policy adt_policy[IPSET_ATTR_ADT_MAX + 1];
194 /* Set this to THIS_MODULE if you are a module, otherwise NULL */
198 /* register and unregister set type */
199 extern int ip_set_type_register(struct ip_set_type *set_type);
200 extern void ip_set_type_unregister(struct ip_set_type *set_type);
202 /* A generic IP set */
204 /* The name of the set */
205 char name[IPSET_MAXNAMELEN];
206 /* Lock protecting the set data */
208 /* References to the set */
210 /* The core set type */
211 struct ip_set_type *type;
212 /* The type variant doing the real job */
213 const struct ip_set_type_variant *variant;
214 /* The actual INET family of the set */
216 /* The type revision */
220 /* Default timeout value, if enabled */
222 /* Element data size */
224 /* Offsets to extensions in elements */
225 size_t offset[IPSET_EXT_ID_MAX];
226 /* The type specific data */
231 ip_set_ext_destroy(struct ip_set *set, void *data)
233 /* Check that the extension is enabled for the set and
234 * call it's destroy function for its extension part in data.
236 if (SET_WITH_COMMENT(set))
237 ip_set_extensions[IPSET_EXT_ID_COMMENT].destroy(
238 ext_comment(data, set));
242 ip_set_put_flags(struct sk_buff *skb, struct ip_set *set)
246 if (SET_WITH_TIMEOUT(set))
247 if (unlikely(nla_put_net32(skb, IPSET_ATTR_TIMEOUT,
248 htonl(set->timeout))))
250 if (SET_WITH_COUNTER(set))
251 cadt_flags |= IPSET_FLAG_WITH_COUNTERS;
252 if (SET_WITH_COMMENT(set))
253 cadt_flags |= IPSET_FLAG_WITH_COMMENT;
257 return nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(cadt_flags));
261 ip_set_add_bytes(u64 bytes, struct ip_set_counter *counter)
263 atomic64_add((long long)bytes, &(counter)->bytes);
267 ip_set_add_packets(u64 packets, struct ip_set_counter *counter)
269 atomic64_add((long long)packets, &(counter)->packets);
273 ip_set_get_bytes(const struct ip_set_counter *counter)
275 return (u64)atomic64_read(&(counter)->bytes);
279 ip_set_get_packets(const struct ip_set_counter *counter)
281 return (u64)atomic64_read(&(counter)->packets);
285 ip_set_update_counter(struct ip_set_counter *counter,
286 const struct ip_set_ext *ext,
287 struct ip_set_ext *mext, u32 flags)
289 if (ext->packets != ULLONG_MAX &&
290 !(flags & IPSET_FLAG_SKIP_COUNTER_UPDATE)) {
291 ip_set_add_bytes(ext->bytes, counter);
292 ip_set_add_packets(ext->packets, counter);
294 if (flags & IPSET_FLAG_MATCH_COUNTERS) {
295 mext->packets = ip_set_get_packets(counter);
296 mext->bytes = ip_set_get_bytes(counter);
301 ip_set_put_counter(struct sk_buff *skb, struct ip_set_counter *counter)
303 return nla_put_net64(skb, IPSET_ATTR_BYTES,
304 cpu_to_be64(ip_set_get_bytes(counter))) ||
305 nla_put_net64(skb, IPSET_ATTR_PACKETS,
306 cpu_to_be64(ip_set_get_packets(counter)));
310 ip_set_init_counter(struct ip_set_counter *counter,
311 const struct ip_set_ext *ext)
313 if (ext->bytes != ULLONG_MAX)
314 atomic64_set(&(counter)->bytes, (long long)(ext->bytes));
315 if (ext->packets != ULLONG_MAX)
316 atomic64_set(&(counter)->packets, (long long)(ext->packets));
319 /* register and unregister set references */
320 extern ip_set_id_t ip_set_get_byname(struct net *net,
321 const char *name, struct ip_set **set);
322 extern void ip_set_put_byindex(struct net *net, ip_set_id_t index);
323 extern const char *ip_set_name_byindex(struct net *net, ip_set_id_t index);
324 extern ip_set_id_t ip_set_nfnl_get(struct net *net, const char *name);
325 extern ip_set_id_t ip_set_nfnl_get_byindex(struct net *net, ip_set_id_t index);
326 extern void ip_set_nfnl_put(struct net *net, ip_set_id_t index);
328 /* API for iptables set match, and SET target */
330 extern int ip_set_add(ip_set_id_t id, const struct sk_buff *skb,
331 const struct xt_action_param *par,
332 struct ip_set_adt_opt *opt);
333 extern int ip_set_del(ip_set_id_t id, const struct sk_buff *skb,
334 const struct xt_action_param *par,
335 struct ip_set_adt_opt *opt);
336 extern int ip_set_test(ip_set_id_t id, const struct sk_buff *skb,
337 const struct xt_action_param *par,
338 struct ip_set_adt_opt *opt);
340 /* Utility functions */
341 extern void *ip_set_alloc(size_t size);
342 extern void ip_set_free(void *members);
343 extern int ip_set_get_ipaddr4(struct nlattr *nla, __be32 *ipaddr);
344 extern int ip_set_get_ipaddr6(struct nlattr *nla, union nf_inet_addr *ipaddr);
345 extern size_t ip_set_elem_len(struct ip_set *set, struct nlattr *tb[],
347 extern int ip_set_get_extensions(struct ip_set *set, struct nlattr *tb[],
348 struct ip_set_ext *ext);
351 ip_set_get_hostipaddr4(struct nlattr *nla, u32 *ipaddr)
354 int ret = ip_set_get_ipaddr4(nla, &ip);
362 /* Ignore IPSET_ERR_EXIST errors if asked to do so? */
364 ip_set_eexist(int ret, u32 flags)
366 return ret == -IPSET_ERR_EXIST && (flags & IPSET_FLAG_EXIST);
369 /* Match elements marked with nomatch */
371 ip_set_enomatch(int ret, u32 flags, enum ipset_adt adt, struct ip_set *set)
373 return adt == IPSET_TEST &&
374 (set->type->features & IPSET_TYPE_NOMATCH) &&
375 ((flags >> 16) & IPSET_FLAG_NOMATCH) &&
376 (ret > 0 || ret == -ENOTEMPTY);
379 /* Check the NLA_F_NET_BYTEORDER flag */
381 ip_set_attr_netorder(struct nlattr *tb[], int type)
383 return tb[type] && (tb[type]->nla_type & NLA_F_NET_BYTEORDER);
387 ip_set_optattr_netorder(struct nlattr *tb[], int type)
389 return !tb[type] || (tb[type]->nla_type & NLA_F_NET_BYTEORDER);
392 /* Useful converters */
394 ip_set_get_h32(const struct nlattr *attr)
396 return ntohl(nla_get_be32(attr));
400 ip_set_get_h16(const struct nlattr *attr)
402 return ntohs(nla_get_be16(attr));
405 #define ipset_nest_start(skb, attr) nla_nest_start(skb, attr | NLA_F_NESTED)
406 #define ipset_nest_end(skb, start) nla_nest_end(skb, start)
408 static inline int nla_put_ipaddr4(struct sk_buff *skb, int type, __be32 ipaddr)
410 struct nlattr *__nested = ipset_nest_start(skb, type);
415 ret = nla_put_net32(skb, IPSET_ATTR_IPADDR_IPV4, ipaddr);
417 ipset_nest_end(skb, __nested);
421 static inline int nla_put_ipaddr6(struct sk_buff *skb, int type,
422 const struct in6_addr *ipaddrptr)
424 struct nlattr *__nested = ipset_nest_start(skb, type);
429 ret = nla_put(skb, IPSET_ATTR_IPADDR_IPV6,
430 sizeof(struct in6_addr), ipaddrptr);
432 ipset_nest_end(skb, __nested);
436 /* Get address from skbuff */
438 ip4addr(const struct sk_buff *skb, bool src)
440 return src ? ip_hdr(skb)->saddr : ip_hdr(skb)->daddr;
444 ip4addrptr(const struct sk_buff *skb, bool src, __be32 *addr)
446 *addr = src ? ip_hdr(skb)->saddr : ip_hdr(skb)->daddr;
450 ip6addrptr(const struct sk_buff *skb, bool src, struct in6_addr *addr)
452 memcpy(addr, src ? &ipv6_hdr(skb)->saddr : &ipv6_hdr(skb)->daddr,
456 /* Calculate the bytes required to store the inclusive range of a-b */
458 bitmap_bytes(u32 a, u32 b)
460 return 4 * ((((b - a + 8) / 8) + 3) / 4);
463 #include <linux/netfilter/ipset/ip_set_timeout.h>
464 #include <linux/netfilter/ipset/ip_set_comment.h>
467 ip_set_put_extensions(struct sk_buff *skb, const struct ip_set *set,
468 const void *e, bool active)
470 if (SET_WITH_TIMEOUT(set)) {
471 unsigned long *timeout = ext_timeout(e, set);
473 if (nla_put_net32(skb, IPSET_ATTR_TIMEOUT,
474 htonl(active ? ip_set_timeout_get(timeout)
478 if (SET_WITH_COUNTER(set) &&
479 ip_set_put_counter(skb, ext_counter(e, set)))
481 if (SET_WITH_COMMENT(set) &&
482 ip_set_put_comment(skb, ext_comment(e, set)))
487 #define IP_SET_INIT_KEXT(skb, opt, set) \
488 { .bytes = (skb)->len, .packets = 1, \
489 .timeout = ip_set_adt_opt_timeout(opt, set) }
491 #define IP_SET_INIT_UEXT(set) \
492 { .bytes = ULLONG_MAX, .packets = ULLONG_MAX, \
493 .timeout = (set)->timeout }
495 #define IP_SET_INIT_CIDR(a, b) ((a) ? (a) : (b))
497 #define IPSET_CONCAT(a, b) a##b
498 #define IPSET_TOKEN(a, b) IPSET_CONCAT(a, b)
500 #endif /*_IP_SET_H */