1#ifndef _NET_NF_TABLES_H
2#define _NET_NF_TABLES_H
3
4#include <linux/module.h>
5#include <linux/list.h>
6#include <linux/netfilter.h>
7#include <linux/netfilter/nfnetlink.h>
8#include <linux/netfilter/x_tables.h>
9#include <linux/netfilter/nf_tables.h>
10#include <linux/u64_stats_sync.h>
11#include <net/netlink.h>
12
13#define NFT_JUMP_STACK_SIZE 16
14
15struct nft_pktinfo {
16 struct sk_buff *skb;
17 struct net *net;
18 const struct net_device *in;
19 const struct net_device *out;
20 u8 pf;
21 u8 hook;
22 bool tprot_set;
23 u8 tprot;
24
25 struct xt_action_param xt;
26};
27
28static inline void nft_set_pktinfo(struct nft_pktinfo *pkt,
29 struct sk_buff *skb,
30 const struct nf_hook_state *state)
31{
32 pkt->skb = skb;
33 pkt->net = pkt->xt.net = state->net;
34 pkt->in = pkt->xt.in = state->in;
35 pkt->out = pkt->xt.out = state->out;
36 pkt->hook = pkt->xt.hooknum = state->hook;
37 pkt->pf = pkt->xt.family = state->pf;
38}
39
40static inline void nft_set_pktinfo_proto_unspec(struct nft_pktinfo *pkt,
41 struct sk_buff *skb)
42{
43 pkt->tprot_set = false;
44 pkt->tprot = 0;
45 pkt->xt.thoff = 0;
46 pkt->xt.fragoff = 0;
47}
48
49static inline void nft_set_pktinfo_unspec(struct nft_pktinfo *pkt,
50 struct sk_buff *skb,
51 const struct nf_hook_state *state)
52{
53 nft_set_pktinfo(pkt, skb, state);
54 nft_set_pktinfo_proto_unspec(pkt, skb);
55}
56
57
58
59
60
61
62
63struct nft_verdict {
64 u32 code;
65 struct nft_chain *chain;
66};
67
68struct nft_data {
69 union {
70 u32 data[4];
71 struct nft_verdict verdict;
72 };
73} __attribute__((aligned(__alignof__(u64))));
74
75
76
77
78
79
80
81
82
83struct nft_regs {
84 union {
85 u32 data[20];
86 struct nft_verdict verdict;
87 };
88};
89
90static inline void nft_data_copy(u32 *dst, const struct nft_data *src,
91 unsigned int len)
92{
93 memcpy(dst, src, len);
94}
95
96static inline void nft_data_debug(const struct nft_data *data)
97{
98 pr_debug("data[0]=%x data[1]=%x data[2]=%x data[3]=%x\n",
99 data->data[0], data->data[1],
100 data->data[2], data->data[3]);
101}
102
103
104
105
106
107
108
109
110
111
112
113
114
115struct nft_ctx {
116 struct net *net;
117 struct nft_af_info *afi;
118 struct nft_table *table;
119 struct nft_chain *chain;
120 const struct nlattr * const *nla;
121 u32 portid;
122 u32 seq;
123 bool report;
124};
125
126struct nft_data_desc {
127 enum nft_data_types type;
128 unsigned int len;
129};
130
131int nft_data_init(const struct nft_ctx *ctx,
132 struct nft_data *data, unsigned int size,
133 struct nft_data_desc *desc, const struct nlattr *nla);
134void nft_data_uninit(const struct nft_data *data, enum nft_data_types type);
135int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
136 enum nft_data_types type, unsigned int len);
137
138static inline enum nft_data_types nft_dreg_to_type(enum nft_registers reg)
139{
140 return reg == NFT_REG_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE;
141}
142
143static inline enum nft_registers nft_type_to_reg(enum nft_data_types type)
144{
145 return type == NFT_DATA_VERDICT ? NFT_REG_VERDICT : NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE;
146}
147
148int nft_parse_u32_check(const struct nlattr *attr, int max, u32 *dest);
149unsigned int nft_parse_register(const struct nlattr *attr);
150int nft_dump_register(struct sk_buff *skb, unsigned int attr, unsigned int reg);
151
152int nft_validate_register_load(enum nft_registers reg, unsigned int len);
153int nft_validate_register_store(const struct nft_ctx *ctx,
154 enum nft_registers reg,
155 const struct nft_data *data,
156 enum nft_data_types type, unsigned int len);
157
158
159
160
161
162
163
164
165
166
167
168struct nft_userdata {
169 u8 len;
170 unsigned char data[0];
171};
172
173
174
175
176
177
178
179struct nft_set_elem {
180 union {
181 u32 buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)];
182 struct nft_data val;
183 } key;
184 void *priv;
185};
186
187struct nft_set;
188struct nft_set_iter {
189 u8 genmask;
190 unsigned int count;
191 unsigned int skip;
192 int err;
193 int (*fn)(const struct nft_ctx *ctx,
194 const struct nft_set *set,
195 const struct nft_set_iter *iter,
196 const struct nft_set_elem *elem);
197};
198
199
200
201
202
203
204
205
206struct nft_set_desc {
207 unsigned int klen;
208 unsigned int dlen;
209 unsigned int size;
210};
211
212
213
214
215
216
217
218
219enum nft_set_class {
220 NFT_SET_CLASS_O_1,
221 NFT_SET_CLASS_O_LOG_N,
222 NFT_SET_CLASS_O_N,
223};
224
225
226
227
228
229
230
231
232struct nft_set_estimate {
233 unsigned int size;
234 enum nft_set_class class;
235};
236
237struct nft_set_ext;
238struct nft_expr;
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257struct nft_set_ops {
258 bool (*lookup)(const struct net *net,
259 const struct nft_set *set,
260 const u32 *key,
261 const struct nft_set_ext **ext);
262 bool (*update)(struct nft_set *set,
263 const u32 *key,
264 void *(*new)(struct nft_set *,
265 const struct nft_expr *,
266 struct nft_regs *),
267 const struct nft_expr *expr,
268 struct nft_regs *regs,
269 const struct nft_set_ext **ext);
270
271 int (*insert)(const struct net *net,
272 const struct nft_set *set,
273 const struct nft_set_elem *elem,
274 struct nft_set_ext **ext);
275 void (*activate)(const struct net *net,
276 const struct nft_set *set,
277 const struct nft_set_elem *elem);
278 void * (*deactivate)(const struct net *net,
279 const struct nft_set *set,
280 const struct nft_set_elem *elem);
281 void (*remove)(const struct nft_set *set,
282 const struct nft_set_elem *elem);
283 void (*walk)(const struct nft_ctx *ctx,
284 const struct nft_set *set,
285 struct nft_set_iter *iter);
286
287 unsigned int (*privsize)(const struct nlattr * const nla[]);
288 bool (*estimate)(const struct nft_set_desc *desc,
289 u32 features,
290 struct nft_set_estimate *est);
291 int (*init)(const struct nft_set *set,
292 const struct nft_set_desc *desc,
293 const struct nlattr * const nla[]);
294 void (*destroy)(const struct nft_set *set);
295
296 struct list_head list;
297 struct module *owner;
298 unsigned int elemsize;
299 u32 features;
300};
301
302int nft_register_set(struct nft_set_ops *ops);
303void nft_unregister_set(struct nft_set_ops *ops);
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328struct nft_set {
329 struct list_head list;
330 struct list_head bindings;
331 char name[NFT_SET_MAXNAMELEN];
332 u32 ktype;
333 u32 dtype;
334 u32 size;
335 atomic_t nelems;
336 u32 ndeact;
337 u64 timeout;
338 u32 gc_int;
339 u16 policy;
340 u16 udlen;
341 unsigned char *udata;
342
343 const struct nft_set_ops *ops ____cacheline_aligned;
344 u16 flags:14,
345 genmask:2;
346 u8 klen;
347 u8 dlen;
348 unsigned char data[]
349 __attribute__((aligned(__alignof__(u64))));
350};
351
352static inline void *nft_set_priv(const struct nft_set *set)
353{
354 return (void *)set->data;
355}
356
357static inline struct nft_set *nft_set_container_of(const void *priv)
358{
359 return (void *)priv - offsetof(struct nft_set, data);
360}
361
362struct nft_set *nf_tables_set_lookup(const struct nft_table *table,
363 const struct nlattr *nla, u8 genmask);
364struct nft_set *nf_tables_set_lookup_byid(const struct net *net,
365 const struct nlattr *nla, u8 genmask);
366
367static inline unsigned long nft_set_gc_interval(const struct nft_set *set)
368{
369 return set->gc_int ? msecs_to_jiffies(set->gc_int) : HZ;
370}
371
372
373
374
375
376
377
378
379
380
381
382struct nft_set_binding {
383 struct list_head list;
384 const struct nft_chain *chain;
385 u32 flags;
386};
387
388int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
389 struct nft_set_binding *binding);
390void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
391 struct nft_set_binding *binding);
392
393
394
395
396
397
398
399
400
401
402
403
404
405enum nft_set_extensions {
406 NFT_SET_EXT_KEY,
407 NFT_SET_EXT_DATA,
408 NFT_SET_EXT_FLAGS,
409 NFT_SET_EXT_TIMEOUT,
410 NFT_SET_EXT_EXPIRATION,
411 NFT_SET_EXT_USERDATA,
412 NFT_SET_EXT_EXPR,
413 NFT_SET_EXT_NUM
414};
415
416
417
418
419
420
421
422struct nft_set_ext_type {
423 u8 len;
424 u8 align;
425};
426
427extern const struct nft_set_ext_type nft_set_ext_types[];
428
429
430
431
432
433
434
435struct nft_set_ext_tmpl {
436 u16 len;
437 u8 offset[NFT_SET_EXT_NUM];
438};
439
440
441
442
443
444
445
446
447struct nft_set_ext {
448 u8 genmask;
449 u8 offset[NFT_SET_EXT_NUM];
450 char data[0];
451};
452
453static inline void nft_set_ext_prepare(struct nft_set_ext_tmpl *tmpl)
454{
455 memset(tmpl, 0, sizeof(*tmpl));
456 tmpl->len = sizeof(struct nft_set_ext);
457}
458
459static inline void nft_set_ext_add_length(struct nft_set_ext_tmpl *tmpl, u8 id,
460 unsigned int len)
461{
462 tmpl->len = ALIGN(tmpl->len, nft_set_ext_types[id].align);
463 BUG_ON(tmpl->len > U8_MAX);
464 tmpl->offset[id] = tmpl->len;
465 tmpl->len += nft_set_ext_types[id].len + len;
466}
467
468static inline void nft_set_ext_add(struct nft_set_ext_tmpl *tmpl, u8 id)
469{
470 nft_set_ext_add_length(tmpl, id, 0);
471}
472
473static inline void nft_set_ext_init(struct nft_set_ext *ext,
474 const struct nft_set_ext_tmpl *tmpl)
475{
476 memcpy(ext->offset, tmpl->offset, sizeof(ext->offset));
477}
478
479static inline bool __nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
480{
481 return !!ext->offset[id];
482}
483
484static inline bool nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
485{
486 return ext && __nft_set_ext_exists(ext, id);
487}
488
489static inline void *nft_set_ext(const struct nft_set_ext *ext, u8 id)
490{
491 return (void *)ext + ext->offset[id];
492}
493
494static inline struct nft_data *nft_set_ext_key(const struct nft_set_ext *ext)
495{
496 return nft_set_ext(ext, NFT_SET_EXT_KEY);
497}
498
499static inline struct nft_data *nft_set_ext_data(const struct nft_set_ext *ext)
500{
501 return nft_set_ext(ext, NFT_SET_EXT_DATA);
502}
503
504static inline u8 *nft_set_ext_flags(const struct nft_set_ext *ext)
505{
506 return nft_set_ext(ext, NFT_SET_EXT_FLAGS);
507}
508
509static inline u64 *nft_set_ext_timeout(const struct nft_set_ext *ext)
510{
511 return nft_set_ext(ext, NFT_SET_EXT_TIMEOUT);
512}
513
514static inline unsigned long *nft_set_ext_expiration(const struct nft_set_ext *ext)
515{
516 return nft_set_ext(ext, NFT_SET_EXT_EXPIRATION);
517}
518
519static inline struct nft_userdata *nft_set_ext_userdata(const struct nft_set_ext *ext)
520{
521 return nft_set_ext(ext, NFT_SET_EXT_USERDATA);
522}
523
524static inline struct nft_expr *nft_set_ext_expr(const struct nft_set_ext *ext)
525{
526 return nft_set_ext(ext, NFT_SET_EXT_EXPR);
527}
528
529static inline bool nft_set_elem_expired(const struct nft_set_ext *ext)
530{
531 return nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION) &&
532 time_is_before_eq_jiffies(*nft_set_ext_expiration(ext));
533}
534
535static inline struct nft_set_ext *nft_set_elem_ext(const struct nft_set *set,
536 void *elem)
537{
538 return elem + set->ops->elemsize;
539}
540
541void *nft_set_elem_init(const struct nft_set *set,
542 const struct nft_set_ext_tmpl *tmpl,
543 const u32 *key, const u32 *data,
544 u64 timeout, gfp_t gfp);
545void nft_set_elem_destroy(const struct nft_set *set, void *elem,
546 bool destroy_expr);
547
548
549
550
551
552
553
554
555struct nft_set_gc_batch_head {
556 struct rcu_head rcu;
557 const struct nft_set *set;
558 unsigned int cnt;
559};
560
561#define NFT_SET_GC_BATCH_SIZE ((PAGE_SIZE - \
562 sizeof(struct nft_set_gc_batch_head)) / \
563 sizeof(void *))
564
565
566
567
568
569
570
571struct nft_set_gc_batch {
572 struct nft_set_gc_batch_head head;
573 void *elems[NFT_SET_GC_BATCH_SIZE];
574};
575
576struct nft_set_gc_batch *nft_set_gc_batch_alloc(const struct nft_set *set,
577 gfp_t gfp);
578void nft_set_gc_batch_release(struct rcu_head *rcu);
579
580static inline void nft_set_gc_batch_complete(struct nft_set_gc_batch *gcb)
581{
582 if (gcb != NULL)
583 call_rcu(&gcb->head.rcu, nft_set_gc_batch_release);
584}
585
586static inline struct nft_set_gc_batch *
587nft_set_gc_batch_check(const struct nft_set *set, struct nft_set_gc_batch *gcb,
588 gfp_t gfp)
589{
590 if (gcb != NULL) {
591 if (gcb->head.cnt + 1 < ARRAY_SIZE(gcb->elems))
592 return gcb;
593 nft_set_gc_batch_complete(gcb);
594 }
595 return nft_set_gc_batch_alloc(set, gfp);
596}
597
598static inline void nft_set_gc_batch_add(struct nft_set_gc_batch *gcb,
599 void *elem)
600{
601 gcb->elems[gcb->head.cnt++] = elem;
602}
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617struct nft_expr_type {
618 const struct nft_expr_ops *(*select_ops)(const struct nft_ctx *,
619 const struct nlattr * const tb[]);
620 const struct nft_expr_ops *ops;
621 struct list_head list;
622 const char *name;
623 struct module *owner;
624 const struct nla_policy *policy;
625 unsigned int maxattr;
626 u8 family;
627 u8 flags;
628};
629
630#define NFT_EXPR_STATEFUL 0x1
631
632
633
634
635
636
637
638
639
640
641
642
643
644struct nft_expr;
645struct nft_expr_ops {
646 void (*eval)(const struct nft_expr *expr,
647 struct nft_regs *regs,
648 const struct nft_pktinfo *pkt);
649 int (*clone)(struct nft_expr *dst,
650 const struct nft_expr *src);
651 unsigned int size;
652
653 int (*init)(const struct nft_ctx *ctx,
654 const struct nft_expr *expr,
655 const struct nlattr * const tb[]);
656 void (*destroy)(const struct nft_ctx *ctx,
657 const struct nft_expr *expr);
658 int (*dump)(struct sk_buff *skb,
659 const struct nft_expr *expr);
660 int (*validate)(const struct nft_ctx *ctx,
661 const struct nft_expr *expr,
662 const struct nft_data **data);
663 const struct nft_expr_type *type;
664 void *data;
665};
666
667#define NFT_EXPR_MAXATTR 16
668#define NFT_EXPR_SIZE(size) (sizeof(struct nft_expr) + \
669 ALIGN(size, __alignof__(struct nft_expr)))
670
671
672
673
674
675
676
677struct nft_expr {
678 const struct nft_expr_ops *ops;
679 unsigned char data[];
680};
681
682static inline void *nft_expr_priv(const struct nft_expr *expr)
683{
684 return (void *)expr->data;
685}
686
687struct nft_expr *nft_expr_init(const struct nft_ctx *ctx,
688 const struct nlattr *nla);
689void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr);
690int nft_expr_dump(struct sk_buff *skb, unsigned int attr,
691 const struct nft_expr *expr);
692
693static inline int nft_expr_clone(struct nft_expr *dst, struct nft_expr *src)
694{
695 int err;
696
697 if (src->ops->clone) {
698 dst->ops = src->ops;
699 err = src->ops->clone(dst, src);
700 if (err < 0)
701 return err;
702 } else {
703 memcpy(dst, src, src->ops->size);
704 }
705
706 __module_get(src->ops->type->owner);
707 return 0;
708}
709
710
711
712
713
714
715
716
717
718
719
720struct nft_rule {
721 struct list_head list;
722 u64 handle:42,
723 genmask:2,
724 dlen:12,
725 udata:1;
726 unsigned char data[]
727 __attribute__((aligned(__alignof__(struct nft_expr))));
728};
729
730static inline struct nft_expr *nft_expr_first(const struct nft_rule *rule)
731{
732 return (struct nft_expr *)&rule->data[0];
733}
734
735static inline struct nft_expr *nft_expr_next(const struct nft_expr *expr)
736{
737 return ((void *)expr) + expr->ops->size;
738}
739
740static inline struct nft_expr *nft_expr_last(const struct nft_rule *rule)
741{
742 return (struct nft_expr *)&rule->data[rule->dlen];
743}
744
745static inline struct nft_userdata *nft_userdata(const struct nft_rule *rule)
746{
747 return (void *)&rule->data[rule->dlen];
748}
749
750
751
752
753
754
755#define nft_rule_for_each_expr(expr, last, rule) \
756 for ((expr) = nft_expr_first(rule), (last) = nft_expr_last(rule); \
757 (expr) != (last); \
758 (expr) = nft_expr_next(expr))
759
760enum nft_chain_flags {
761 NFT_BASE_CHAIN = 0x1,
762};
763
764
765
766
767
768
769
770
771
772
773
774
775
776struct nft_chain {
777 struct list_head rules;
778 struct list_head list;
779 struct nft_table *table;
780 u64 handle;
781 u32 use;
782 u16 level;
783 u8 flags:6,
784 genmask:2;
785 char name[NFT_CHAIN_MAXNAMELEN];
786};
787
788enum nft_chain_type {
789 NFT_CHAIN_T_DEFAULT = 0,
790 NFT_CHAIN_T_ROUTE,
791 NFT_CHAIN_T_NAT,
792 NFT_CHAIN_T_MAX
793};
794
795
796
797
798
799
800
801
802
803
804
805struct nf_chain_type {
806 const char *name;
807 enum nft_chain_type type;
808 int family;
809 struct module *owner;
810 unsigned int hook_mask;
811 nf_hookfn *hooks[NF_MAX_HOOKS];
812};
813
814int nft_chain_validate_dependency(const struct nft_chain *chain,
815 enum nft_chain_type type);
816int nft_chain_validate_hooks(const struct nft_chain *chain,
817 unsigned int hook_flags);
818
819struct nft_stats {
820 u64 bytes;
821 u64 pkts;
822 struct u64_stats_sync syncp;
823};
824
825#define NFT_HOOK_OPS_MAX 2
826
827
828
829
830
831
832
833
834
835
836
837struct nft_base_chain {
838 struct nf_hook_ops ops[NFT_HOOK_OPS_MAX];
839 const struct nf_chain_type *type;
840 u8 policy;
841 u8 flags;
842 struct nft_stats __percpu *stats;
843 struct nft_chain chain;
844 char dev_name[IFNAMSIZ];
845};
846
847static inline struct nft_base_chain *nft_base_chain(const struct nft_chain *chain)
848{
849 return container_of(chain, struct nft_base_chain, chain);
850}
851
852int __nft_release_basechain(struct nft_ctx *ctx);
853
854unsigned int nft_do_chain(struct nft_pktinfo *pkt, void *priv);
855
856
857
858
859
860
861
862
863
864
865
866
867
868struct nft_table {
869 struct list_head list;
870 struct list_head chains;
871 struct list_head sets;
872 u64 hgenerator;
873 u32 use;
874 u16 flags:14,
875 genmask:2;
876 char name[NFT_TABLE_MAXNAMELEN];
877};
878
879enum nft_af_flags {
880 NFT_AF_NEEDS_DEV = (1 << 0),
881};
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896struct nft_af_info {
897 struct list_head list;
898 int family;
899 unsigned int nhooks;
900 struct module *owner;
901 struct list_head tables;
902 u32 flags;
903 unsigned int nops;
904 void (*hook_ops_init)(struct nf_hook_ops *,
905 unsigned int);
906 nf_hookfn *hooks[NF_MAX_HOOKS];
907};
908
909int nft_register_afinfo(struct net *, struct nft_af_info *);
910void nft_unregister_afinfo(struct net *, struct nft_af_info *);
911
912int nft_register_chain_type(const struct nf_chain_type *);
913void nft_unregister_chain_type(const struct nf_chain_type *);
914
915int nft_register_expr(struct nft_expr_type *);
916void nft_unregister_expr(struct nft_expr_type *);
917
918int nft_verdict_dump(struct sk_buff *skb, int type,
919 const struct nft_verdict *v);
920
921
922
923
924
925
926
927
928
929
930
931
932
933struct nft_traceinfo {
934 const struct nft_pktinfo *pkt;
935 const struct nft_base_chain *basechain;
936 const struct nft_chain *chain;
937 const struct nft_rule *rule;
938 const struct nft_verdict *verdict;
939 enum nft_trace_types type;
940 bool packet_dumped;
941 bool trace;
942};
943
944void nft_trace_init(struct nft_traceinfo *info, const struct nft_pktinfo *pkt,
945 const struct nft_verdict *verdict,
946 const struct nft_chain *basechain);
947
948void nft_trace_notify(struct nft_traceinfo *info);
949
950#define nft_dereference(p) \
951 nfnl_dereference(p, NFNL_SUBSYS_NFTABLES)
952
953#define MODULE_ALIAS_NFT_FAMILY(family) \
954 MODULE_ALIAS("nft-afinfo-" __stringify(family))
955
956#define MODULE_ALIAS_NFT_CHAIN(family, name) \
957 MODULE_ALIAS("nft-chain-" __stringify(family) "-" name)
958
959#define MODULE_ALIAS_NFT_AF_EXPR(family, name) \
960 MODULE_ALIAS("nft-expr-" __stringify(family) "-" name)
961
962#define MODULE_ALIAS_NFT_EXPR(name) \
963 MODULE_ALIAS("nft-expr-" name)
964
965#define MODULE_ALIAS_NFT_SET() \
966 MODULE_ALIAS("nft-set")
967
968
969
970
971
972
973
974
975
976
977
978
979
980static inline unsigned int nft_gencursor_next(const struct net *net)
981{
982 return net->nft.gencursor + 1 == 1 ? 1 : 0;
983}
984
985static inline u8 nft_genmask_next(const struct net *net)
986{
987 return 1 << nft_gencursor_next(net);
988}
989
990static inline u8 nft_genmask_cur(const struct net *net)
991{
992
993 return 1 << ACCESS_ONCE(net->nft.gencursor);
994}
995
996#define NFT_GENMASK_ANY ((1 << 0) | (1 << 1))
997
998
999
1000
1001
1002
1003#define nft_is_active(__net, __obj) \
1004 (((__obj)->genmask & nft_genmask_cur(__net)) == 0)
1005
1006
1007#define nft_is_active_next(__net, __obj) \
1008 (((__obj)->genmask & nft_genmask_next(__net)) == 0)
1009
1010
1011#define nft_activate_next(__net, __obj) \
1012 (__obj)->genmask = nft_genmask_cur(__net)
1013
1014
1015#define nft_deactivate_next(__net, __obj) \
1016 (__obj)->genmask = nft_genmask_next(__net)
1017
1018
1019#define nft_clear(__net, __obj) \
1020 (__obj)->genmask &= ~nft_genmask_next(__net)
1021#define nft_active_genmask(__obj, __genmask) \
1022 !((__obj)->genmask & __genmask)
1023
1024
1025
1026
1027
1028static inline bool nft_set_elem_active(const struct nft_set_ext *ext,
1029 u8 genmask)
1030{
1031 return !(ext->genmask & genmask);
1032}
1033
1034static inline void nft_set_elem_change_active(const struct net *net,
1035 const struct nft_set *set,
1036 struct nft_set_ext *ext)
1037{
1038 ext->genmask ^= nft_genmask_next(net);
1039}
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051#define NFT_SET_ELEM_BUSY_MASK (1 << 2)
1052
1053#if defined(__LITTLE_ENDIAN_BITFIELD)
1054#define NFT_SET_ELEM_BUSY_BIT 2
1055#elif defined(__BIG_ENDIAN_BITFIELD)
1056#define NFT_SET_ELEM_BUSY_BIT (BITS_PER_LONG - BITS_PER_BYTE + 2)
1057#else
1058#error
1059#endif
1060
1061static inline int nft_set_elem_mark_busy(struct nft_set_ext *ext)
1062{
1063 unsigned long *word = (unsigned long *)ext;
1064
1065 BUILD_BUG_ON(offsetof(struct nft_set_ext, genmask) != 0);
1066 return test_and_set_bit(NFT_SET_ELEM_BUSY_BIT, word);
1067}
1068
1069static inline void nft_set_elem_clear_busy(struct nft_set_ext *ext)
1070{
1071 unsigned long *word = (unsigned long *)ext;
1072
1073 clear_bit(NFT_SET_ELEM_BUSY_BIT, word);
1074}
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084struct nft_trans {
1085 struct list_head list;
1086 int msg_type;
1087 struct nft_ctx ctx;
1088 char data[0];
1089};
1090
1091struct nft_trans_rule {
1092 struct nft_rule *rule;
1093};
1094
1095#define nft_trans_rule(trans) \
1096 (((struct nft_trans_rule *)trans->data)->rule)
1097
1098struct nft_trans_set {
1099 struct nft_set *set;
1100 u32 set_id;
1101};
1102
1103#define nft_trans_set(trans) \
1104 (((struct nft_trans_set *)trans->data)->set)
1105#define nft_trans_set_id(trans) \
1106 (((struct nft_trans_set *)trans->data)->set_id)
1107
1108struct nft_trans_chain {
1109 bool update;
1110 char name[NFT_CHAIN_MAXNAMELEN];
1111 struct nft_stats __percpu *stats;
1112 u8 policy;
1113};
1114
1115#define nft_trans_chain_update(trans) \
1116 (((struct nft_trans_chain *)trans->data)->update)
1117#define nft_trans_chain_name(trans) \
1118 (((struct nft_trans_chain *)trans->data)->name)
1119#define nft_trans_chain_stats(trans) \
1120 (((struct nft_trans_chain *)trans->data)->stats)
1121#define nft_trans_chain_policy(trans) \
1122 (((struct nft_trans_chain *)trans->data)->policy)
1123
1124struct nft_trans_table {
1125 bool update;
1126 bool enable;
1127};
1128
1129#define nft_trans_table_update(trans) \
1130 (((struct nft_trans_table *)trans->data)->update)
1131#define nft_trans_table_enable(trans) \
1132 (((struct nft_trans_table *)trans->data)->enable)
1133
1134struct nft_trans_elem {
1135 struct nft_set *set;
1136 struct nft_set_elem elem;
1137};
1138
1139#define nft_trans_elem_set(trans) \
1140 (((struct nft_trans_elem *)trans->data)->set)
1141#define nft_trans_elem(trans) \
1142 (((struct nft_trans_elem *)trans->data)->elem)
1143
1144#endif
1145