1
2
3
4
5
6
7
8
9
10#ifndef _IP_SET_H
11#define _IP_SET_H
12
13#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>
18#include <linux/stringify.h>
19#include <linux/vmalloc.h>
20#include <net/netlink.h>
21#include <uapi/linux/netfilter/ipset/ip_set.h>
22
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))
27
28
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),
40 IPSET_TYPE_IFACE_FLAG = 5,
41 IPSET_TYPE_IFACE = (1 << IPSET_TYPE_IFACE_FLAG),
42 IPSET_TYPE_MARK_FLAG = 6,
43 IPSET_TYPE_MARK = (1 << IPSET_TYPE_MARK_FLAG),
44 IPSET_TYPE_NOMATCH_FLAG = 7,
45 IPSET_TYPE_NOMATCH = (1 << IPSET_TYPE_NOMATCH_FLAG),
46
47
48 IPSET_DUMP_LAST_FLAG = 8,
49 IPSET_DUMP_LAST = (1 << IPSET_DUMP_LAST_FLAG),
50};
51
52
53enum ip_set_extension {
54 IPSET_EXT_BIT_TIMEOUT = 0,
55 IPSET_EXT_TIMEOUT = (1 << IPSET_EXT_BIT_TIMEOUT),
56 IPSET_EXT_BIT_COUNTER = 1,
57 IPSET_EXT_COUNTER = (1 << IPSET_EXT_BIT_COUNTER),
58 IPSET_EXT_BIT_COMMENT = 2,
59 IPSET_EXT_COMMENT = (1 << IPSET_EXT_BIT_COMMENT),
60 IPSET_EXT_BIT_SKBINFO = 3,
61 IPSET_EXT_SKBINFO = (1 << IPSET_EXT_BIT_SKBINFO),
62
63 IPSET_EXT_BIT_DESTROY = 7,
64 IPSET_EXT_DESTROY = (1 << IPSET_EXT_BIT_DESTROY),
65};
66
67#define SET_WITH_TIMEOUT(s) ((s)->extensions & IPSET_EXT_TIMEOUT)
68#define SET_WITH_COUNTER(s) ((s)->extensions & IPSET_EXT_COUNTER)
69#define SET_WITH_COMMENT(s) ((s)->extensions & IPSET_EXT_COMMENT)
70#define SET_WITH_SKBINFO(s) ((s)->extensions & IPSET_EXT_SKBINFO)
71#define SET_WITH_FORCEADD(s) ((s)->flags & IPSET_CREATE_FLAG_FORCEADD)
72
73
74enum ip_set_ext_id {
75 IPSET_EXT_ID_COUNTER = 0,
76 IPSET_EXT_ID_TIMEOUT,
77 IPSET_EXT_ID_SKBINFO,
78 IPSET_EXT_ID_COMMENT,
79 IPSET_EXT_ID_MAX,
80};
81
82
83struct ip_set_ext_type {
84
85 void (*destroy)(void *ext);
86 enum ip_set_extension type;
87 enum ipset_cadt_flags flag;
88
89 u8 len;
90 u8 align;
91};
92
93extern const struct ip_set_ext_type ip_set_extensions[];
94
95struct ip_set_ext {
96 u64 packets;
97 u64 bytes;
98 u32 timeout;
99 u32 skbmark;
100 u32 skbmarkmask;
101 u32 skbprio;
102 u16 skbqueue;
103 char *comment;
104};
105
106struct ip_set_counter {
107 atomic64_t bytes;
108 atomic64_t packets;
109};
110
111struct ip_set_comment_rcu {
112 struct rcu_head rcu;
113 char str[0];
114};
115
116struct ip_set_comment {
117 struct ip_set_comment_rcu __rcu *c;
118};
119
120struct ip_set_skbinfo {
121 u32 skbmark;
122 u32 skbmarkmask;
123 u32 skbprio;
124 u16 skbqueue;
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
193
194struct ip_set_type {
195 struct list_head list;
196
197
198 char name[IPSET_MAXNAMELEN];
199
200 u8 protocol;
201
202 u8 dimension;
203
204
205
206
207 u8 family;
208
209 u8 revision_min, revision_max;
210
211 u16 features;
212
213
214 int (*create)(struct net *net, struct ip_set *set,
215 struct nlattr *tb[], u32 flags);
216
217
218 const struct nla_policy create_policy[IPSET_ATTR_CREATE_MAX + 1];
219 const struct nla_policy adt_policy[IPSET_ATTR_ADT_MAX + 1];
220
221
222 struct module *me;
223};
224
225
226extern int ip_set_type_register(struct ip_set_type *set_type);
227extern void ip_set_type_unregister(struct ip_set_type *set_type);
228
229
230struct ip_set {
231
232 char name[IPSET_MAXNAMELEN];
233
234 spinlock_t lock;
235
236 u32 ref;
237
238
239
240 u32 ref_netlink;
241
242 struct ip_set_type *type;
243
244 const struct ip_set_type_variant *variant;
245
246 u8 family;
247
248 u8 revision;
249
250 u8 extensions;
251
252 u8 flags;
253
254 u32 timeout;
255
256 size_t dsize;
257
258 size_t offset[IPSET_EXT_ID_MAX];
259
260 void *data;
261};
262
263static inline void
264ip_set_ext_destroy(struct ip_set *set, void *data)
265{
266
267
268
269 if (SET_WITH_COMMENT(set))
270 ip_set_extensions[IPSET_EXT_ID_COMMENT].destroy(
271 ext_comment(data, set));
272}
273
274static inline int
275ip_set_put_flags(struct sk_buff *skb, struct ip_set *set)
276{
277 u32 cadt_flags = 0;
278
279 if (SET_WITH_TIMEOUT(set))
280 if (unlikely(nla_put_net32(skb, IPSET_ATTR_TIMEOUT,
281 htonl(set->timeout))))
282 return -EMSGSIZE;
283 if (SET_WITH_COUNTER(set))
284 cadt_flags |= IPSET_FLAG_WITH_COUNTERS;
285 if (SET_WITH_COMMENT(set))
286 cadt_flags |= IPSET_FLAG_WITH_COMMENT;
287 if (SET_WITH_SKBINFO(set))
288 cadt_flags |= IPSET_FLAG_WITH_SKBINFO;
289 if (SET_WITH_FORCEADD(set))
290 cadt_flags |= IPSET_FLAG_WITH_FORCEADD;
291
292 if (!cadt_flags)
293 return 0;
294 return nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(cadt_flags));
295}
296
297static inline void
298ip_set_add_bytes(u64 bytes, struct ip_set_counter *counter)
299{
300 atomic64_add((long long)bytes, &(counter)->bytes);
301}
302
303static inline void
304ip_set_add_packets(u64 packets, struct ip_set_counter *counter)
305{
306 atomic64_add((long long)packets, &(counter)->packets);
307}
308
309static inline u64
310ip_set_get_bytes(const struct ip_set_counter *counter)
311{
312 return (u64)atomic64_read(&(counter)->bytes);
313}
314
315static inline u64
316ip_set_get_packets(const struct ip_set_counter *counter)
317{
318 return (u64)atomic64_read(&(counter)->packets);
319}
320
321static inline void
322ip_set_update_counter(struct ip_set_counter *counter,
323 const struct ip_set_ext *ext,
324 struct ip_set_ext *mext, u32 flags)
325{
326 if (ext->packets != ULLONG_MAX &&
327 !(flags & IPSET_FLAG_SKIP_COUNTER_UPDATE)) {
328 ip_set_add_bytes(ext->bytes, counter);
329 ip_set_add_packets(ext->packets, counter);
330 }
331 if (flags & IPSET_FLAG_MATCH_COUNTERS) {
332 mext->packets = ip_set_get_packets(counter);
333 mext->bytes = ip_set_get_bytes(counter);
334 }
335}
336
337static inline void
338ip_set_get_skbinfo(struct ip_set_skbinfo *skbinfo,
339 const struct ip_set_ext *ext,
340 struct ip_set_ext *mext, u32 flags)
341{
342 mext->skbmark = skbinfo->skbmark;
343 mext->skbmarkmask = skbinfo->skbmarkmask;
344 mext->skbprio = skbinfo->skbprio;
345 mext->skbqueue = skbinfo->skbqueue;
346}
347static inline bool
348ip_set_put_skbinfo(struct sk_buff *skb, struct ip_set_skbinfo *skbinfo)
349{
350
351 return ((skbinfo->skbmark || skbinfo->skbmarkmask) &&
352 nla_put_net64(skb, IPSET_ATTR_SKBMARK,
353 cpu_to_be64((u64)skbinfo->skbmark << 32 |
354 skbinfo->skbmarkmask),
355 IPSET_ATTR_PAD)) ||
356 (skbinfo->skbprio &&
357 nla_put_net32(skb, IPSET_ATTR_SKBPRIO,
358 cpu_to_be32(skbinfo->skbprio))) ||
359 (skbinfo->skbqueue &&
360 nla_put_net16(skb, IPSET_ATTR_SKBQUEUE,
361 cpu_to_be16(skbinfo->skbqueue)));
362}
363
364static inline void
365ip_set_init_skbinfo(struct ip_set_skbinfo *skbinfo,
366 const struct ip_set_ext *ext)
367{
368 skbinfo->skbmark = ext->skbmark;
369 skbinfo->skbmarkmask = ext->skbmarkmask;
370 skbinfo->skbprio = ext->skbprio;
371 skbinfo->skbqueue = ext->skbqueue;
372}
373
374static inline bool
375ip_set_put_counter(struct sk_buff *skb, struct ip_set_counter *counter)
376{
377 return nla_put_net64(skb, IPSET_ATTR_BYTES,
378 cpu_to_be64(ip_set_get_bytes(counter)),
379 IPSET_ATTR_PAD) ||
380 nla_put_net64(skb, IPSET_ATTR_PACKETS,
381 cpu_to_be64(ip_set_get_packets(counter)),
382 IPSET_ATTR_PAD);
383}
384
385static inline void
386ip_set_init_counter(struct ip_set_counter *counter,
387 const struct ip_set_ext *ext)
388{
389 if (ext->bytes != ULLONG_MAX)
390 atomic64_set(&(counter)->bytes, (long long)(ext->bytes));
391 if (ext->packets != ULLONG_MAX)
392 atomic64_set(&(counter)->packets, (long long)(ext->packets));
393}
394
395
396enum {
397 IPSET_CB_NET = 0,
398 IPSET_CB_DUMP,
399 IPSET_CB_INDEX,
400 IPSET_CB_PRIVATE,
401 IPSET_CB_ARG0,
402 IPSET_CB_ARG1,
403};
404
405
406extern ip_set_id_t ip_set_get_byname(struct net *net,
407 const char *name, struct ip_set **set);
408extern void ip_set_put_byindex(struct net *net, ip_set_id_t index);
409extern const char *ip_set_name_byindex(struct net *net, ip_set_id_t index);
410extern ip_set_id_t ip_set_nfnl_get_byindex(struct net *net, ip_set_id_t index);
411extern void ip_set_nfnl_put(struct net *net, ip_set_id_t index);
412
413
414
415extern int ip_set_add(ip_set_id_t id, const struct sk_buff *skb,
416 const struct xt_action_param *par,
417 struct ip_set_adt_opt *opt);
418extern int ip_set_del(ip_set_id_t id, const struct sk_buff *skb,
419 const struct xt_action_param *par,
420 struct ip_set_adt_opt *opt);
421extern int ip_set_test(ip_set_id_t id, const struct sk_buff *skb,
422 const struct xt_action_param *par,
423 struct ip_set_adt_opt *opt);
424
425
426extern void *ip_set_alloc(size_t size);
427extern void ip_set_free(void *members);
428extern int ip_set_get_ipaddr4(struct nlattr *nla, __be32 *ipaddr);
429extern int ip_set_get_ipaddr6(struct nlattr *nla, union nf_inet_addr *ipaddr);
430extern size_t ip_set_elem_len(struct ip_set *set, struct nlattr *tb[],
431 size_t len, size_t align);
432extern int ip_set_get_extensions(struct ip_set *set, struct nlattr *tb[],
433 struct ip_set_ext *ext);
434
435static inline int
436ip_set_get_hostipaddr4(struct nlattr *nla, u32 *ipaddr)
437{
438 __be32 ip;
439 int ret = ip_set_get_ipaddr4(nla, &ip);
440
441 if (ret)
442 return ret;
443 *ipaddr = ntohl(ip);
444 return 0;
445}
446
447
448static inline bool
449ip_set_eexist(int ret, u32 flags)
450{
451 return ret == -IPSET_ERR_EXIST && (flags & IPSET_FLAG_EXIST);
452}
453
454
455static inline bool
456ip_set_enomatch(int ret, u32 flags, enum ipset_adt adt, struct ip_set *set)
457{
458 return adt == IPSET_TEST &&
459 (set->type->features & IPSET_TYPE_NOMATCH) &&
460 ((flags >> 16) & IPSET_FLAG_NOMATCH) &&
461 (ret > 0 || ret == -ENOTEMPTY);
462}
463
464
465static inline bool
466ip_set_attr_netorder(struct nlattr *tb[], int type)
467{
468 return tb[type] && (tb[type]->nla_type & NLA_F_NET_BYTEORDER);
469}
470
471static inline bool
472ip_set_optattr_netorder(struct nlattr *tb[], int type)
473{
474 return !tb[type] || (tb[type]->nla_type & NLA_F_NET_BYTEORDER);
475}
476
477
478static inline u32
479ip_set_get_h32(const struct nlattr *attr)
480{
481 return ntohl(nla_get_be32(attr));
482}
483
484static inline u16
485ip_set_get_h16(const struct nlattr *attr)
486{
487 return ntohs(nla_get_be16(attr));
488}
489
490#define ipset_nest_start(skb, attr) nla_nest_start(skb, attr | NLA_F_NESTED)
491#define ipset_nest_end(skb, start) nla_nest_end(skb, start)
492
493static inline int nla_put_ipaddr4(struct sk_buff *skb, int type, __be32 ipaddr)
494{
495 struct nlattr *__nested = ipset_nest_start(skb, type);
496 int ret;
497
498 if (!__nested)
499 return -EMSGSIZE;
500 ret = nla_put_in_addr(skb, IPSET_ATTR_IPADDR_IPV4, ipaddr);
501 if (!ret)
502 ipset_nest_end(skb, __nested);
503 return ret;
504}
505
506static inline int nla_put_ipaddr6(struct sk_buff *skb, int type,
507 const struct in6_addr *ipaddrptr)
508{
509 struct nlattr *__nested = ipset_nest_start(skb, type);
510 int ret;
511
512 if (!__nested)
513 return -EMSGSIZE;
514 ret = nla_put_in6_addr(skb, IPSET_ATTR_IPADDR_IPV6, ipaddrptr);
515 if (!ret)
516 ipset_nest_end(skb, __nested);
517 return ret;
518}
519
520
521static inline __be32
522ip4addr(const struct sk_buff *skb, bool src)
523{
524 return src ? ip_hdr(skb)->saddr : ip_hdr(skb)->daddr;
525}
526
527static inline void
528ip4addrptr(const struct sk_buff *skb, bool src, __be32 *addr)
529{
530 *addr = src ? ip_hdr(skb)->saddr : ip_hdr(skb)->daddr;
531}
532
533static inline void
534ip6addrptr(const struct sk_buff *skb, bool src, struct in6_addr *addr)
535{
536 memcpy(addr, src ? &ipv6_hdr(skb)->saddr : &ipv6_hdr(skb)->daddr,
537 sizeof(*addr));
538}
539
540
541static inline int
542bitmap_bytes(u32 a, u32 b)
543{
544 return 4 * ((((b - a + 8) / 8) + 3) / 4);
545}
546
547#include <linux/netfilter/ipset/ip_set_timeout.h>
548#include <linux/netfilter/ipset/ip_set_comment.h>
549
550int
551ip_set_put_extensions(struct sk_buff *skb, const struct ip_set *set,
552 const void *e, bool active);
553
554#define IP_SET_INIT_KEXT(skb, opt, set) \
555 { .bytes = (skb)->len, .packets = 1, \
556 .timeout = ip_set_adt_opt_timeout(opt, set) }
557
558#define IP_SET_INIT_UEXT(set) \
559 { .bytes = ULLONG_MAX, .packets = ULLONG_MAX, \
560 .timeout = (set)->timeout }
561
562#define IPSET_CONCAT(a, b) a##b
563#define IPSET_TOKEN(a, b) IPSET_CONCAT(a, b)
564
565#endif
566