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