1
2
3
4
5
6
7#ifndef _IP_SET_H
8#define _IP_SET_H
9
10#include <linux/ip.h>
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>
19
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))
24
25
26enum ip_set_feature {
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
44
45 IPSET_DUMP_LAST_FLAG = 8,
46 IPSET_DUMP_LAST = (1 << IPSET_DUMP_LAST_FLAG),
47};
48
49
50enum 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
60 IPSET_EXT_BIT_DESTROY = 7,
61 IPSET_EXT_DESTROY = (1 << IPSET_EXT_BIT_DESTROY),
62};
63
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)
69
70
71enum ip_set_ext_id {
72 IPSET_EXT_ID_COUNTER = 0,
73 IPSET_EXT_ID_TIMEOUT,
74 IPSET_EXT_ID_SKBINFO,
75 IPSET_EXT_ID_COMMENT,
76 IPSET_EXT_ID_MAX,
77};
78
79struct ip_set;
80
81
82struct ip_set_ext_type {
83
84 void (*destroy)(struct ip_set *set, void *ext);
85 enum ip_set_extension type;
86 enum ipset_cadt_flags flag;
87
88 u8 len;
89 u8 align;
90};
91
92extern const struct ip_set_ext_type ip_set_extensions[];
93
94struct ip_set_counter {
95 atomic64_t bytes;
96 atomic64_t packets;
97};
98
99struct ip_set_comment_rcu {
100 struct rcu_head rcu;
101 char str[];
102};
103
104struct ip_set_comment {
105 struct ip_set_comment_rcu __rcu *c;
106};
107
108struct ip_set_skbinfo {
109 u32 skbmark;
110 u32 skbmarkmask;
111 u32 skbprio;
112 u16 skbqueue;
113 u16 __pad;
114};
115
116struct ip_set_ext {
117 struct ip_set_skbinfo skbinfo;
118 u64 packets;
119 u64 bytes;
120 char *comment;
121 u32 timeout;
122 u8 packets_op;
123 u8 bytes_op;
124 bool target;
125};
126
127struct ip_set;
128
129#define ext_timeout(e, s) \
130((unsigned long *)(((void *)(e)) + (s)->offset[IPSET_EXT_ID_TIMEOUT]))
131#define ext_counter(e, s) \
132((struct ip_set_counter *)(((void *)(e)) + (s)->offset[IPSET_EXT_ID_COUNTER]))
133#define ext_comment(e, s) \
134((struct ip_set_comment *)(((void *)(e)) + (s)->offset[IPSET_EXT_ID_COMMENT]))
135#define ext_skbinfo(e, s) \
136((struct ip_set_skbinfo *)(((void *)(e)) + (s)->offset[IPSET_EXT_ID_SKBINFO]))
137
138typedef int (*ipset_adtfn)(struct ip_set *set, void *value,
139 const struct ip_set_ext *ext,
140 struct ip_set_ext *mext, u32 cmdflags);
141
142
143struct ip_set_adt_opt {
144 u8 family;
145 u8 dim;
146 u8 flags;
147 u32 cmdflags;
148 struct ip_set_ext ext;
149};
150
151
152struct ip_set_type_variant {
153
154
155
156
157 int (*kadt)(struct ip_set *set, const struct sk_buff *skb,
158 const struct xt_action_param *par,
159 enum ipset_adt adt, struct ip_set_adt_opt *opt);
160
161
162
163
164
165 int (*uadt)(struct ip_set *set, struct nlattr *tb[],
166 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried);
167
168
169 ipset_adtfn adt[IPSET_ADT_MAX];
170
171
172 int (*resize)(struct ip_set *set, bool retried);
173
174 void (*destroy)(struct ip_set *set);
175
176 void (*flush)(struct ip_set *set);
177
178 void (*expire)(struct ip_set *set);
179
180 int (*head)(struct ip_set *set, struct sk_buff *skb);
181
182 int (*list)(const struct ip_set *set, struct sk_buff *skb,
183 struct netlink_callback *cb);
184
185 void (*uref)(struct ip_set *set, struct netlink_callback *cb,
186 bool start);
187
188
189
190 bool (*same_set)(const struct ip_set *a, const struct ip_set *b);
191
192 bool region_lock;
193};
194
195struct ip_set_region {
196 spinlock_t lock;
197 size_t ext_size;
198 u32 elements;
199};
200
201
202#define IPSET_REVISION_MAX 9
203
204
205struct ip_set_type {
206 struct list_head list;
207
208
209 char name[IPSET_MAXNAMELEN];
210
211 u8 protocol;
212
213 u8 dimension;
214
215
216
217
218 u8 family;
219
220 u8 revision_min, revision_max;
221
222 u8 create_flags[IPSET_REVISION_MAX+1];
223
224 u16 features;
225
226
227 int (*create)(struct net *net, struct ip_set *set,
228 struct nlattr *tb[], u32 flags);
229
230
231 const struct nla_policy create_policy[IPSET_ATTR_CREATE_MAX + 1];
232 const struct nla_policy adt_policy[IPSET_ATTR_ADT_MAX + 1];
233
234
235 struct module *me;
236};
237
238
239extern int ip_set_type_register(struct ip_set_type *set_type);
240extern void ip_set_type_unregister(struct ip_set_type *set_type);
241
242
243struct ip_set {
244
245 char name[IPSET_MAXNAMELEN];
246
247 spinlock_t lock;
248
249 u32 ref;
250
251
252
253 u32 ref_netlink;
254
255 struct ip_set_type *type;
256
257 const struct ip_set_type_variant *variant;
258
259 u8 family;
260
261 u8 revision;
262
263 u8 extensions;
264
265 u8 flags;
266
267 u32 timeout;
268
269 u32 elements;
270
271 size_t ext_size;
272
273 size_t dsize;
274
275 size_t offset[IPSET_EXT_ID_MAX];
276
277 void *data;
278};
279
280static inline void
281ip_set_ext_destroy(struct ip_set *set, void *data)
282{
283
284
285
286 if (SET_WITH_COMMENT(set)) {
287 struct ip_set_comment *c = ext_comment(data, set);
288
289 ip_set_extensions[IPSET_EXT_ID_COMMENT].destroy(set, c);
290 }
291}
292
293int ip_set_put_flags(struct sk_buff *skb, struct ip_set *set);
294
295
296enum {
297 IPSET_CB_NET = 0,
298 IPSET_CB_PROTO,
299 IPSET_CB_DUMP,
300 IPSET_CB_INDEX,
301 IPSET_CB_PRIVATE,
302 IPSET_CB_ARG0,
303};
304
305
306extern ip_set_id_t ip_set_get_byname(struct net *net,
307 const char *name, struct ip_set **set);
308extern void ip_set_put_byindex(struct net *net, ip_set_id_t index);
309extern void ip_set_name_byindex(struct net *net, ip_set_id_t index, char *name);
310extern ip_set_id_t ip_set_nfnl_get_byindex(struct net *net, ip_set_id_t index);
311extern void ip_set_nfnl_put(struct net *net, ip_set_id_t index);
312
313
314
315extern int ip_set_add(ip_set_id_t id, const struct sk_buff *skb,
316 const struct xt_action_param *par,
317 struct ip_set_adt_opt *opt);
318extern int ip_set_del(ip_set_id_t id, const struct sk_buff *skb,
319 const struct xt_action_param *par,
320 struct ip_set_adt_opt *opt);
321extern int ip_set_test(ip_set_id_t id, const struct sk_buff *skb,
322 const struct xt_action_param *par,
323 struct ip_set_adt_opt *opt);
324
325
326extern void *ip_set_alloc(size_t size);
327extern void ip_set_free(void *members);
328extern int ip_set_get_ipaddr4(struct nlattr *nla, __be32 *ipaddr);
329extern int ip_set_get_ipaddr6(struct nlattr *nla, union nf_inet_addr *ipaddr);
330extern size_t ip_set_elem_len(struct ip_set *set, struct nlattr *tb[],
331 size_t len, size_t align);
332extern int ip_set_get_extensions(struct ip_set *set, struct nlattr *tb[],
333 struct ip_set_ext *ext);
334extern int ip_set_put_extensions(struct sk_buff *skb, const struct ip_set *set,
335 const void *e, bool active);
336extern bool ip_set_match_extensions(struct ip_set *set,
337 const struct ip_set_ext *ext,
338 struct ip_set_ext *mext,
339 u32 flags, void *data);
340
341static inline int
342ip_set_get_hostipaddr4(struct nlattr *nla, u32 *ipaddr)
343{
344 __be32 ip;
345 int ret = ip_set_get_ipaddr4(nla, &ip);
346
347 if (ret)
348 return ret;
349 *ipaddr = ntohl(ip);
350 return 0;
351}
352
353
354static inline bool
355ip_set_eexist(int ret, u32 flags)
356{
357 return ret == -IPSET_ERR_EXIST && (flags & IPSET_FLAG_EXIST);
358}
359
360
361static inline bool
362ip_set_enomatch(int ret, u32 flags, enum ipset_adt adt, struct ip_set *set)
363{
364 return adt == IPSET_TEST &&
365 (set->type->features & IPSET_TYPE_NOMATCH) &&
366 ((flags >> 16) & IPSET_FLAG_NOMATCH) &&
367 (ret > 0 || ret == -ENOTEMPTY);
368}
369
370
371static inline bool
372ip_set_attr_netorder(struct nlattr *tb[], int type)
373{
374 return tb[type] && (tb[type]->nla_type & NLA_F_NET_BYTEORDER);
375}
376
377static inline bool
378ip_set_optattr_netorder(struct nlattr *tb[], int type)
379{
380 return !tb[type] || (tb[type]->nla_type & NLA_F_NET_BYTEORDER);
381}
382
383
384static inline u32
385ip_set_get_h32(const struct nlattr *attr)
386{
387 return ntohl(nla_get_be32(attr));
388}
389
390static inline u16
391ip_set_get_h16(const struct nlattr *attr)
392{
393 return ntohs(nla_get_be16(attr));
394}
395
396static inline int nla_put_ipaddr4(struct sk_buff *skb, int type, __be32 ipaddr)
397{
398 struct nlattr *__nested = nla_nest_start(skb, type);
399 int ret;
400
401 if (!__nested)
402 return -EMSGSIZE;
403 ret = nla_put_in_addr(skb, IPSET_ATTR_IPADDR_IPV4, ipaddr);
404 if (!ret)
405 nla_nest_end(skb, __nested);
406 return ret;
407}
408
409static inline int nla_put_ipaddr6(struct sk_buff *skb, int type,
410 const struct in6_addr *ipaddrptr)
411{
412 struct nlattr *__nested = nla_nest_start(skb, type);
413 int ret;
414
415 if (!__nested)
416 return -EMSGSIZE;
417 ret = nla_put_in6_addr(skb, IPSET_ATTR_IPADDR_IPV6, ipaddrptr);
418 if (!ret)
419 nla_nest_end(skb, __nested);
420 return ret;
421}
422
423
424static inline __be32
425ip4addr(const struct sk_buff *skb, bool src)
426{
427 return src ? ip_hdr(skb)->saddr : ip_hdr(skb)->daddr;
428}
429
430static inline void
431ip4addrptr(const struct sk_buff *skb, bool src, __be32 *addr)
432{
433 *addr = src ? ip_hdr(skb)->saddr : ip_hdr(skb)->daddr;
434}
435
436static inline void
437ip6addrptr(const struct sk_buff *skb, bool src, struct in6_addr *addr)
438{
439 memcpy(addr, src ? &ipv6_hdr(skb)->saddr : &ipv6_hdr(skb)->daddr,
440 sizeof(*addr));
441}
442
443
444#define IPSET_GC_TIME (3 * 60)
445
446
447#define IPSET_GC_PERIOD(timeout) \
448 ((timeout/3) ? min_t(u32, (timeout)/3, IPSET_GC_TIME) : 1)
449
450
451#define IPSET_ELEM_PERMANENT 0
452
453
454#define IPSET_NO_TIMEOUT UINT_MAX
455
456
457#define IPSET_MAX_TIMEOUT (UINT_MAX >> 1)/MSEC_PER_SEC
458
459#define ip_set_adt_opt_timeout(opt, set) \
460((opt)->ext.timeout != IPSET_NO_TIMEOUT ? (opt)->ext.timeout : (set)->timeout)
461
462static inline unsigned int
463ip_set_timeout_uget(struct nlattr *tb)
464{
465 unsigned int timeout = ip_set_get_h32(tb);
466
467
468 if (timeout > IPSET_MAX_TIMEOUT)
469 timeout = IPSET_MAX_TIMEOUT;
470
471 return timeout;
472}
473
474static inline bool
475ip_set_timeout_expired(const unsigned long *t)
476{
477 return *t != IPSET_ELEM_PERMANENT && time_is_before_jiffies(*t);
478}
479
480static inline void
481ip_set_timeout_set(unsigned long *timeout, u32 value)
482{
483 unsigned long t;
484
485 if (!value) {
486 *timeout = IPSET_ELEM_PERMANENT;
487 return;
488 }
489
490 t = msecs_to_jiffies(value * MSEC_PER_SEC) + jiffies;
491 if (t == IPSET_ELEM_PERMANENT)
492
493 t--;
494 *timeout = t;
495}
496
497void ip_set_init_comment(struct ip_set *set, struct ip_set_comment *comment,
498 const struct ip_set_ext *ext);
499
500static inline void
501ip_set_init_counter(struct ip_set_counter *counter,
502 const struct ip_set_ext *ext)
503{
504 if (ext->bytes != ULLONG_MAX)
505 atomic64_set(&(counter)->bytes, (long long)(ext->bytes));
506 if (ext->packets != ULLONG_MAX)
507 atomic64_set(&(counter)->packets, (long long)(ext->packets));
508}
509
510static inline void
511ip_set_init_skbinfo(struct ip_set_skbinfo *skbinfo,
512 const struct ip_set_ext *ext)
513{
514 *skbinfo = ext->skbinfo;
515}
516
517#define IP_SET_INIT_KEXT(skb, opt, set) \
518 { .bytes = (skb)->len, .packets = 1, .target = true,\
519 .timeout = ip_set_adt_opt_timeout(opt, set) }
520
521#define IP_SET_INIT_UEXT(set) \
522 { .bytes = ULLONG_MAX, .packets = ULLONG_MAX, \
523 .timeout = (set)->timeout }
524
525#define IPSET_CONCAT(a, b) a##b
526#define IPSET_TOKEN(a, b) IPSET_CONCAT(a, b)
527
528#endif
529