blob: d80e2753847ce40d23e174e0b9fa623249bb4f28 [file] [log] [blame]
Jozsef Kadlecsika7b4f982011-02-01 15:28:35 +01001/* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
2 * Patrick Schaaf <bof@bof.de>
3 * Martin Josefsson <gandalf@wlug.westbo.se>
Jozsef Kadlecsik075e64c2013-04-27 14:28:55 +02004 * Copyright (C) 2003-2013 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
Jozsef Kadlecsika7b4f982011-02-01 15:28:35 +01005 *
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.
9 */
David Howellsa8201412012-10-09 09:48:55 +010010#ifndef _IP_SET_H
11#define _IP_SET_H
Jozsef Kadlecsika7b4f982011-02-01 15:28:35 +010012
Jan Engelhardtae8ded12012-01-14 16:26:20 +010013#include <linux/ip.h>
14#include <linux/ipv6.h>
15#include <linux/netlink.h>
16#include <linux/netfilter.h>
17#include <linux/netfilter/x_tables.h>
Jozsef Kadlecsik10111a62012-09-21 21:59:32 +020018#include <linux/stringify.h>
Jan Engelhardtae8ded12012-01-14 16:26:20 +010019#include <linux/vmalloc.h>
20#include <net/netlink.h>
David Howellsa8201412012-10-09 09:48:55 +010021#include <uapi/linux/netfilter/ipset/ip_set.h>
Jan Engelhardtae8ded12012-01-14 16:26:20 +010022
Jozsef Kadlecsik10111a62012-09-21 21:59:32 +020023#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))
27
Jozsef Kadlecsika7b4f982011-02-01 15:28:35 +010028/* Set features */
29enum ip_set_feature {
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),
Jozsef Kadlecsike3853572011-06-16 19:00:48 +020040 IPSET_TYPE_IFACE_FLAG = 5,
41 IPSET_TYPE_IFACE = (1 << IPSET_TYPE_IFACE_FLAG),
Jozsef Kadlecsik3e0304a2012-09-21 22:02:36 +020042 IPSET_TYPE_NOMATCH_FLAG = 6,
43 IPSET_TYPE_NOMATCH = (1 << IPSET_TYPE_NOMATCH_FLAG),
Jozsef Kadlecsika7b4f982011-02-01 15:28:35 +010044 /* 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),
48};
49
Jozsef Kadlecsik075e64c2013-04-27 14:28:55 +020050/* Set extensions */
51enum ip_set_extension {
52 IPSET_EXT_NONE = 0,
53 IPSET_EXT_BIT_TIMEOUT = 1,
54 IPSET_EXT_TIMEOUT = (1 << IPSET_EXT_BIT_TIMEOUT),
Jozsef Kadlecsik34d666d2013-04-27 14:38:56 +020055 IPSET_EXT_BIT_COUNTER = 2,
56 IPSET_EXT_COUNTER = (1 << IPSET_EXT_BIT_COUNTER),
Jozsef Kadlecsik075e64c2013-04-27 14:28:55 +020057};
58
59/* Extension offsets */
60enum ip_set_offset {
61 IPSET_OFFSET_TIMEOUT = 0,
Jozsef Kadlecsik34d666d2013-04-27 14:38:56 +020062 IPSET_OFFSET_COUNTER,
Jozsef Kadlecsik075e64c2013-04-27 14:28:55 +020063 IPSET_OFFSET_MAX,
64};
65
66#define SET_WITH_TIMEOUT(s) ((s)->extensions & IPSET_EXT_TIMEOUT)
Jozsef Kadlecsik34d666d2013-04-27 14:38:56 +020067#define SET_WITH_COUNTER(s) ((s)->extensions & IPSET_EXT_COUNTER)
Jozsef Kadlecsik075e64c2013-04-27 14:28:55 +020068
69struct ip_set_ext {
70 unsigned long timeout;
Jozsef Kadlecsik34d666d2013-04-27 14:38:56 +020071 u64 packets;
72 u64 bytes;
Jozsef Kadlecsik075e64c2013-04-27 14:28:55 +020073};
74
Jozsef Kadlecsika7b4f982011-02-01 15:28:35 +010075struct ip_set;
76
Jozsef Kadlecsik54162192011-06-16 18:40:55 +020077typedef int (*ipset_adtfn)(struct ip_set *set, void *value,
Jozsef Kadlecsik075e64c2013-04-27 14:28:55 +020078 const struct ip_set_ext *ext,
Jozsef Kadlecsik6e017812013-04-27 14:40:50 +020079 struct ip_set_ext *mext, u32 cmdflags);
Jozsef Kadlecsika7b4f982011-02-01 15:28:35 +010080
Jozsef Kadlecsikac8cc922011-06-16 18:42:40 +020081/* Kernel API function options */
82struct ip_set_adt_opt {
83 u8 family; /* Actual protocol family */
84 u8 dim; /* Dimension of match/target */
85 u8 flags; /* Direction and negation flags */
86 u32 cmdflags; /* Command-like flags */
Jozsef Kadlecsik075e64c2013-04-27 14:28:55 +020087 struct ip_set_ext ext; /* Extensions */
Jozsef Kadlecsikac8cc922011-06-16 18:42:40 +020088};
89
Jozsef Kadlecsika7b4f982011-02-01 15:28:35 +010090/* Set type, variant-specific part */
91struct ip_set_type_variant {
92 /* Kernelspace: test/add/del entries
93 * returns negative error code,
94 * zero for no match/success to add/delete
95 * positive for matching element */
Jozsef Kadlecsik3ace95c2012-09-21 22:01:45 +020096 int (*kadt)(struct ip_set *set, const struct sk_buff *skb,
Jozsef Kadlecsikb66554c2011-06-16 18:56:47 +020097 const struct xt_action_param *par,
Jozsef Kadlecsik075e64c2013-04-27 14:28:55 +020098 enum ipset_adt adt, struct ip_set_adt_opt *opt);
Jozsef Kadlecsika7b4f982011-02-01 15:28:35 +010099
100 /* Userspace: test/add/del entries
101 * returns negative error code,
102 * zero for no match/success to add/delete
103 * positive for matching element */
104 int (*uadt)(struct ip_set *set, struct nlattr *tb[],
Jozsef Kadlecsik3d14b172011-06-16 18:49:17 +0200105 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried);
Jozsef Kadlecsika7b4f982011-02-01 15:28:35 +0100106
107 /* Low level add/del/test functions */
108 ipset_adtfn adt[IPSET_ADT_MAX];
109
110 /* When adding entries and set is full, try to resize the set */
111 int (*resize)(struct ip_set *set, bool retried);
112 /* Destroy the set */
113 void (*destroy)(struct ip_set *set);
114 /* Flush the elements */
115 void (*flush)(struct ip_set *set);
116 /* Expire entries before listing */
117 void (*expire)(struct ip_set *set);
118 /* List set header data */
119 int (*head)(struct ip_set *set, struct sk_buff *skb);
120 /* List elements */
121 int (*list)(const struct ip_set *set, struct sk_buff *skb,
122 struct netlink_callback *cb);
123
124 /* Return true if "b" set is the same as "a"
125 * according to the create set parameters */
126 bool (*same_set)(const struct ip_set *a, const struct ip_set *b);
127};
128
129/* The core set type structure */
130struct ip_set_type {
131 struct list_head list;
132
133 /* Typename */
134 char name[IPSET_MAXNAMELEN];
135 /* Protocol version */
136 u8 protocol;
137 /* Set features to control swapping */
138 u8 features;
139 /* Set type dimension */
140 u8 dimension;
Jan Engelhardtc15f1c82012-02-14 00:24:10 +0100141 /*
142 * Supported family: may be NFPROTO_UNSPEC for both
143 * NFPROTO_IPV4/NFPROTO_IPV6.
144 */
Jozsef Kadlecsika7b4f982011-02-01 15:28:35 +0100145 u8 family;
Jozsef Kadlecsikf1e00b32011-06-16 18:51:41 +0200146 /* Type revisions */
147 u8 revision_min, revision_max;
Jozsef Kadlecsika7b4f982011-02-01 15:28:35 +0100148
149 /* Create set */
150 int (*create)(struct ip_set *set, struct nlattr *tb[], u32 flags);
151
152 /* Attribute policies */
153 const struct nla_policy create_policy[IPSET_ATTR_CREATE_MAX + 1];
154 const struct nla_policy adt_policy[IPSET_ATTR_ADT_MAX + 1];
155
156 /* Set this to THIS_MODULE if you are a module, otherwise NULL */
157 struct module *me;
158};
159
160/* register and unregister set type */
161extern int ip_set_type_register(struct ip_set_type *set_type);
162extern void ip_set_type_unregister(struct ip_set_type *set_type);
163
164/* A generic IP set */
165struct ip_set {
166 /* The name of the set */
167 char name[IPSET_MAXNAMELEN];
168 /* Lock protecting the set data */
169 rwlock_t lock;
170 /* References to the set */
Jozsef Kadlecsik2f9f28b2011-04-04 15:19:25 +0200171 u32 ref;
Jozsef Kadlecsika7b4f982011-02-01 15:28:35 +0100172 /* The core set type */
173 struct ip_set_type *type;
174 /* The type variant doing the real job */
175 const struct ip_set_type_variant *variant;
176 /* The actual INET family of the set */
177 u8 family;
Jozsef Kadlecsikf1e00b32011-06-16 18:51:41 +0200178 /* The type revision */
179 u8 revision;
Jozsef Kadlecsik075e64c2013-04-27 14:28:55 +0200180 /* Extensions */
181 u8 extensions;
Jozsef Kadlecsika7b4f982011-02-01 15:28:35 +0100182 /* The type specific data */
183 void *data;
184};
185
Jozsef Kadlecsik34d666d2013-04-27 14:38:56 +0200186struct ip_set_counter {
187 atomic64_t bytes;
188 atomic64_t packets;
189};
190
191static inline void
192ip_set_add_bytes(u64 bytes, struct ip_set_counter *counter)
193{
194 atomic64_add((long long)bytes, &(counter)->bytes);
195}
196
197static inline void
198ip_set_add_packets(u64 packets, struct ip_set_counter *counter)
199{
200 atomic64_add((long long)packets, &(counter)->packets);
201}
202
203static inline u64
204ip_set_get_bytes(const struct ip_set_counter *counter)
205{
206 return (u64)atomic64_read(&(counter)->bytes);
207}
208
209static inline u64
210ip_set_get_packets(const struct ip_set_counter *counter)
211{
212 return (u64)atomic64_read(&(counter)->packets);
213}
214
215static inline void
216ip_set_update_counter(struct ip_set_counter *counter,
217 const struct ip_set_ext *ext,
218 struct ip_set_ext *mext, u32 flags)
219{
Jozsef Kadlecsik6e017812013-04-27 14:40:50 +0200220 if (ext->packets != ULLONG_MAX &&
221 !(flags & IPSET_FLAG_SKIP_COUNTER_UPDATE)) {
Jozsef Kadlecsik34d666d2013-04-27 14:38:56 +0200222 ip_set_add_bytes(ext->bytes, counter);
223 ip_set_add_packets(ext->packets, counter);
224 }
Jozsef Kadlecsik6e017812013-04-27 14:40:50 +0200225 if (flags & IPSET_FLAG_MATCH_COUNTERS) {
226 mext->packets = ip_set_get_packets(counter);
227 mext->bytes = ip_set_get_bytes(counter);
228 }
Jozsef Kadlecsik34d666d2013-04-27 14:38:56 +0200229}
230
231static inline bool
232ip_set_put_counter(struct sk_buff *skb, struct ip_set_counter *counter)
233{
234 return nla_put_net64(skb, IPSET_ATTR_BYTES,
235 cpu_to_be64(ip_set_get_bytes(counter))) ||
236 nla_put_net64(skb, IPSET_ATTR_PACKETS,
237 cpu_to_be64(ip_set_get_packets(counter)));
238}
239
240static inline void
241ip_set_init_counter(struct ip_set_counter *counter,
242 const struct ip_set_ext *ext)
243{
244 if (ext->bytes != ULLONG_MAX)
245 atomic64_set(&(counter)->bytes, (long long)(ext->bytes));
246 if (ext->packets != ULLONG_MAX)
247 atomic64_set(&(counter)->packets, (long long)(ext->packets));
248}
249
Jozsef Kadlecsika7b4f982011-02-01 15:28:35 +0100250/* register and unregister set references */
251extern ip_set_id_t ip_set_get_byname(const char *name, struct ip_set **set);
252extern void ip_set_put_byindex(ip_set_id_t index);
Jozsef Kadlecsik15b4d932011-06-16 19:01:26 +0200253extern const char *ip_set_name_byindex(ip_set_id_t index);
Jozsef Kadlecsika7b4f982011-02-01 15:28:35 +0100254extern ip_set_id_t ip_set_nfnl_get(const char *name);
255extern ip_set_id_t ip_set_nfnl_get_byindex(ip_set_id_t index);
256extern void ip_set_nfnl_put(ip_set_id_t index);
257
258/* API for iptables set match, and SET target */
Jozsef Kadlecsikac8cc922011-06-16 18:42:40 +0200259
Jozsef Kadlecsika7b4f982011-02-01 15:28:35 +0100260extern int ip_set_add(ip_set_id_t id, const struct sk_buff *skb,
Jozsef Kadlecsikb66554c2011-06-16 18:56:47 +0200261 const struct xt_action_param *par,
Jozsef Kadlecsik075e64c2013-04-27 14:28:55 +0200262 struct ip_set_adt_opt *opt);
Jozsef Kadlecsika7b4f982011-02-01 15:28:35 +0100263extern int ip_set_del(ip_set_id_t id, const struct sk_buff *skb,
Jozsef Kadlecsikb66554c2011-06-16 18:56:47 +0200264 const struct xt_action_param *par,
Jozsef Kadlecsik075e64c2013-04-27 14:28:55 +0200265 struct ip_set_adt_opt *opt);
Jozsef Kadlecsika7b4f982011-02-01 15:28:35 +0100266extern int ip_set_test(ip_set_id_t id, const struct sk_buff *skb,
Jozsef Kadlecsikb66554c2011-06-16 18:56:47 +0200267 const struct xt_action_param *par,
Jozsef Kadlecsik075e64c2013-04-27 14:28:55 +0200268 struct ip_set_adt_opt *opt);
Jozsef Kadlecsika7b4f982011-02-01 15:28:35 +0100269
270/* Utility functions */
Jozsef Kadlecsik15b4d932011-06-16 19:01:26 +0200271extern void *ip_set_alloc(size_t size);
Jozsef Kadlecsika7b4f982011-02-01 15:28:35 +0100272extern void ip_set_free(void *members);
273extern int ip_set_get_ipaddr4(struct nlattr *nla, __be32 *ipaddr);
274extern int ip_set_get_ipaddr6(struct nlattr *nla, union nf_inet_addr *ipaddr);
Jozsef Kadlecsik075e64c2013-04-27 14:28:55 +0200275extern int ip_set_get_extensions(struct ip_set *set, struct nlattr *tb[],
276 struct ip_set_ext *ext);
Jozsef Kadlecsika7b4f982011-02-01 15:28:35 +0100277
278static inline int
279ip_set_get_hostipaddr4(struct nlattr *nla, u32 *ipaddr)
280{
281 __be32 ip;
282 int ret = ip_set_get_ipaddr4(nla, &ip);
Jozsef Kadlecsik15b4d932011-06-16 19:01:26 +0200283
Jozsef Kadlecsika7b4f982011-02-01 15:28:35 +0100284 if (ret)
285 return ret;
286 *ipaddr = ntohl(ip);
287 return 0;
288}
289
290/* Ignore IPSET_ERR_EXIST errors if asked to do so? */
291static inline bool
292ip_set_eexist(int ret, u32 flags)
293{
294 return ret == -IPSET_ERR_EXIST && (flags & IPSET_FLAG_EXIST);
295}
296
Jozsef Kadlecsik43c56e52013-04-08 21:51:25 +0200297/* Match elements marked with nomatch */
298static inline bool
299ip_set_enomatch(int ret, u32 flags, enum ipset_adt adt)
300{
301 return adt == IPSET_TEST &&
302 ret == -ENOTEMPTY && ((flags >> 16) & IPSET_FLAG_NOMATCH);
303}
304
Jozsef Kadlecsika7b4f982011-02-01 15:28:35 +0100305/* Check the NLA_F_NET_BYTEORDER flag */
306static inline bool
307ip_set_attr_netorder(struct nlattr *tb[], int type)
308{
309 return tb[type] && (tb[type]->nla_type & NLA_F_NET_BYTEORDER);
310}
311
312static inline bool
313ip_set_optattr_netorder(struct nlattr *tb[], int type)
314{
315 return !tb[type] || (tb[type]->nla_type & NLA_F_NET_BYTEORDER);
316}
317
318/* Useful converters */
319static inline u32
320ip_set_get_h32(const struct nlattr *attr)
321{
322 return ntohl(nla_get_be32(attr));
323}
324
325static inline u16
326ip_set_get_h16(const struct nlattr *attr)
327{
328 return ntohs(nla_get_be16(attr));
329}
330
331#define ipset_nest_start(skb, attr) nla_nest_start(skb, attr | NLA_F_NESTED)
332#define ipset_nest_end(skb, start) nla_nest_end(skb, start)
333
David S. Miller7cf78992012-04-01 19:54:46 -0400334static inline int nla_put_ipaddr4(struct sk_buff *skb, int type, __be32 ipaddr)
335{
336 struct nlattr *__nested = ipset_nest_start(skb, type);
337 int ret;
Jozsef Kadlecsika7b4f982011-02-01 15:28:35 +0100338
David S. Miller7cf78992012-04-01 19:54:46 -0400339 if (!__nested)
340 return -EMSGSIZE;
341 ret = nla_put_net32(skb, IPSET_ATTR_IPADDR_IPV4, ipaddr);
342 if (!ret)
343 ipset_nest_end(skb, __nested);
344 return ret;
345}
346
Jozsef Kadlecsik3ace95c2012-09-21 22:01:45 +0200347static inline int nla_put_ipaddr6(struct sk_buff *skb, int type,
348 const struct in6_addr *ipaddrptr)
David S. Miller7cf78992012-04-01 19:54:46 -0400349{
350 struct nlattr *__nested = ipset_nest_start(skb, type);
351 int ret;
352
353 if (!__nested)
354 return -EMSGSIZE;
355 ret = nla_put(skb, IPSET_ATTR_IPADDR_IPV6,
356 sizeof(struct in6_addr), ipaddrptr);
357 if (!ret)
358 ipset_nest_end(skb, __nested);
359 return ret;
360}
Jozsef Kadlecsika7b4f982011-02-01 15:28:35 +0100361
362/* Get address from skbuff */
363static inline __be32
364ip4addr(const struct sk_buff *skb, bool src)
365{
366 return src ? ip_hdr(skb)->saddr : ip_hdr(skb)->daddr;
367}
368
369static inline void
370ip4addrptr(const struct sk_buff *skb, bool src, __be32 *addr)
371{
372 *addr = src ? ip_hdr(skb)->saddr : ip_hdr(skb)->daddr;
373}
374
375static inline void
376ip6addrptr(const struct sk_buff *skb, bool src, struct in6_addr *addr)
377{
378 memcpy(addr, src ? &ipv6_hdr(skb)->saddr : &ipv6_hdr(skb)->daddr,
379 sizeof(*addr));
380}
381
382/* Calculate the bytes required to store the inclusive range of a-b */
383static inline int
384bitmap_bytes(u32 a, u32 b)
385{
386 return 4 * ((((b - a + 8) / 8) + 3) / 4);
387}
388
Jozsef Kadlecsik075e64c2013-04-27 14:28:55 +0200389#include <linux/netfilter/ipset/ip_set_timeout.h>
390
Jozsef Kadlecsik34d666d2013-04-27 14:38:56 +0200391#define IP_SET_INIT_KEXT(skb, opt, map) \
392 { .bytes = (skb)->len, .packets = 1, \
393 .timeout = ip_set_adt_opt_timeout(opt, map) }
Jozsef Kadlecsik075e64c2013-04-27 14:28:55 +0200394
Jozsef Kadlecsik34d666d2013-04-27 14:38:56 +0200395#define IP_SET_INIT_UEXT(map) \
396 { .bytes = ULLONG_MAX, .packets = ULLONG_MAX, \
397 .timeout = (map)->timeout }
Jozsef Kadlecsik075e64c2013-04-27 14:28:55 +0200398
Jozsef Kadlecsika7b4f982011-02-01 15:28:35 +0100399#endif /*_IP_SET_H */