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