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