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