Merge branch 'parisc-5.5-1' of git://git.kernel.org/pub/scm/linux/kernel/git/deller...
[linux-2.6-microblaze.git] / include / net / netfilter / nf_tables.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _NET_NF_TABLES_H
3 #define _NET_NF_TABLES_H
4
5 #include <asm/unaligned.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 #include <net/flow_offload.h>
16
17 struct module;
18
19 #define NFT_JUMP_STACK_SIZE     16
20
21 struct nft_pktinfo {
22         struct sk_buff                  *skb;
23         bool                            tprot_set;
24         u8                              tprot;
25         /* for x_tables compatibility */
26         struct xt_action_param          xt;
27 };
28
29 static inline struct net *nft_net(const struct nft_pktinfo *pkt)
30 {
31         return pkt->xt.state->net;
32 }
33
34 static inline unsigned int nft_hook(const struct nft_pktinfo *pkt)
35 {
36         return pkt->xt.state->hook;
37 }
38
39 static inline u8 nft_pf(const struct nft_pktinfo *pkt)
40 {
41         return pkt->xt.state->pf;
42 }
43
44 static inline const struct net_device *nft_in(const struct nft_pktinfo *pkt)
45 {
46         return pkt->xt.state->in;
47 }
48
49 static inline const struct net_device *nft_out(const struct nft_pktinfo *pkt)
50 {
51         return pkt->xt.state->out;
52 }
53
54 static inline void nft_set_pktinfo(struct nft_pktinfo *pkt,
55                                    struct sk_buff *skb,
56                                    const struct nf_hook_state *state)
57 {
58         pkt->skb = skb;
59         pkt->xt.state = state;
60 }
61
62 static inline void nft_set_pktinfo_unspec(struct nft_pktinfo *pkt,
63                                           struct sk_buff *skb)
64 {
65         pkt->tprot_set = false;
66         pkt->tprot = 0;
67         pkt->xt.thoff = 0;
68         pkt->xt.fragoff = 0;
69 }
70
71 /**
72  *      struct nft_verdict - nf_tables verdict
73  *
74  *      @code: nf_tables/netfilter verdict code
75  *      @chain: destination chain for NFT_JUMP/NFT_GOTO
76  */
77 struct nft_verdict {
78         u32                             code;
79         struct nft_chain                *chain;
80 };
81
82 struct nft_data {
83         union {
84                 u32                     data[4];
85                 struct nft_verdict      verdict;
86         };
87 } __attribute__((aligned(__alignof__(u64))));
88
89 /**
90  *      struct nft_regs - nf_tables register set
91  *
92  *      @data: data registers
93  *      @verdict: verdict register
94  *
95  *      The first four data registers alias to the verdict register.
96  */
97 struct nft_regs {
98         union {
99                 u32                     data[20];
100                 struct nft_verdict      verdict;
101         };
102 };
103
104 /* Store/load an u8, u16 or u64 integer to/from the u32 data register.
105  *
106  * Note, when using concatenations, register allocation happens at 32-bit
107  * level. So for store instruction, pad the rest part with zero to avoid
108  * garbage values.
109  */
110
111 static inline void nft_reg_store8(u32 *dreg, u8 val)
112 {
113         *dreg = 0;
114         *(u8 *)dreg = val;
115 }
116
117 static inline u8 nft_reg_load8(const u32 *sreg)
118 {
119         return *(u8 *)sreg;
120 }
121
122 static inline void nft_reg_store16(u32 *dreg, u16 val)
123 {
124         *dreg = 0;
125         *(u16 *)dreg = val;
126 }
127
128 static inline u16 nft_reg_load16(const u32 *sreg)
129 {
130         return *(u16 *)sreg;
131 }
132
133 static inline void nft_reg_store64(u32 *dreg, u64 val)
134 {
135         put_unaligned(val, (u64 *)dreg);
136 }
137
138 static inline u64 nft_reg_load64(const u32 *sreg)
139 {
140         return get_unaligned((u64 *)sreg);
141 }
142
143 static inline void nft_data_copy(u32 *dst, const struct nft_data *src,
144                                  unsigned int len)
145 {
146         memcpy(dst, src, len);
147 }
148
149 static inline void nft_data_debug(const struct nft_data *data)
150 {
151         pr_debug("data[0]=%x data[1]=%x data[2]=%x data[3]=%x\n",
152                  data->data[0], data->data[1],
153                  data->data[2], data->data[3]);
154 }
155
156 /**
157  *      struct nft_ctx - nf_tables rule/set context
158  *
159  *      @net: net namespace
160  *      @table: the table the chain is contained in
161  *      @chain: the chain the rule is contained in
162  *      @nla: netlink attributes
163  *      @portid: netlink portID of the original message
164  *      @seq: netlink sequence number
165  *      @family: protocol family
166  *      @level: depth of the chains
167  *      @report: notify via unicast netlink message
168  */
169 struct nft_ctx {
170         struct net                      *net;
171         struct nft_table                *table;
172         struct nft_chain                *chain;
173         const struct nlattr * const     *nla;
174         u32                             portid;
175         u32                             seq;
176         u16                             flags;
177         u8                              family;
178         u8                              level;
179         bool                            report;
180 };
181
182 struct nft_data_desc {
183         enum nft_data_types             type;
184         unsigned int                    len;
185 };
186
187 int nft_data_init(const struct nft_ctx *ctx,
188                   struct nft_data *data, unsigned int size,
189                   struct nft_data_desc *desc, const struct nlattr *nla);
190 void nft_data_hold(const struct nft_data *data, enum nft_data_types type);
191 void nft_data_release(const struct nft_data *data, enum nft_data_types type);
192 int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
193                   enum nft_data_types type, unsigned int len);
194
195 static inline enum nft_data_types nft_dreg_to_type(enum nft_registers reg)
196 {
197         return reg == NFT_REG_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE;
198 }
199
200 static inline enum nft_registers nft_type_to_reg(enum nft_data_types type)
201 {
202         return type == NFT_DATA_VERDICT ? NFT_REG_VERDICT : NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE;
203 }
204
205 int nft_parse_u32_check(const struct nlattr *attr, int max, u32 *dest);
206 unsigned int nft_parse_register(const struct nlattr *attr);
207 int nft_dump_register(struct sk_buff *skb, unsigned int attr, unsigned int reg);
208
209 int nft_validate_register_load(enum nft_registers reg, unsigned int len);
210 int nft_validate_register_store(const struct nft_ctx *ctx,
211                                 enum nft_registers reg,
212                                 const struct nft_data *data,
213                                 enum nft_data_types type, unsigned int len);
214
215 /**
216  *      struct nft_userdata - user defined data associated with an object
217  *
218  *      @len: length of the data
219  *      @data: content
220  *
221  *      The presence of user data is indicated in an object specific fashion,
222  *      so a length of zero can't occur and the value "len" indicates data
223  *      of length len + 1.
224  */
225 struct nft_userdata {
226         u8                      len;
227         unsigned char           data[0];
228 };
229
230 /**
231  *      struct nft_set_elem - generic representation of set elements
232  *
233  *      @key: element key
234  *      @priv: element private data and extensions
235  */
236 struct nft_set_elem {
237         union {
238                 u32             buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)];
239                 struct nft_data val;
240         } key;
241         void                    *priv;
242 };
243
244 struct nft_set;
245 struct nft_set_iter {
246         u8              genmask;
247         unsigned int    count;
248         unsigned int    skip;
249         int             err;
250         int             (*fn)(const struct nft_ctx *ctx,
251                               struct nft_set *set,
252                               const struct nft_set_iter *iter,
253                               struct nft_set_elem *elem);
254 };
255
256 /**
257  *      struct nft_set_desc - description of set elements
258  *
259  *      @klen: key length
260  *      @dlen: data length
261  *      @size: number of set elements
262  */
263 struct nft_set_desc {
264         unsigned int            klen;
265         unsigned int            dlen;
266         unsigned int            size;
267 };
268
269 /**
270  *      enum nft_set_class - performance class
271  *
272  *      @NFT_LOOKUP_O_1: constant, O(1)
273  *      @NFT_LOOKUP_O_LOG_N: logarithmic, O(log N)
274  *      @NFT_LOOKUP_O_N: linear, O(N)
275  */
276 enum nft_set_class {
277         NFT_SET_CLASS_O_1,
278         NFT_SET_CLASS_O_LOG_N,
279         NFT_SET_CLASS_O_N,
280 };
281
282 /**
283  *      struct nft_set_estimate - estimation of memory and performance
284  *                                characteristics
285  *
286  *      @size: required memory
287  *      @lookup: lookup performance class
288  *      @space: memory class
289  */
290 struct nft_set_estimate {
291         u64                     size;
292         enum nft_set_class      lookup;
293         enum nft_set_class      space;
294 };
295
296 struct nft_set_ext;
297 struct nft_expr;
298
299 /**
300  *      struct nft_set_ops - nf_tables set operations
301  *
302  *      @lookup: look up an element within the set
303  *      @update: update an element if exists, add it if doesn't exist
304  *      @delete: delete an element
305  *      @insert: insert new element into set
306  *      @activate: activate new element in the next generation
307  *      @deactivate: lookup for element and deactivate it in the next generation
308  *      @flush: deactivate element in the next generation
309  *      @remove: remove element from set
310  *      @walk: iterate over all set elements
311  *      @get: get set elements
312  *      @privsize: function to return size of set private data
313  *      @init: initialize private data of new set instance
314  *      @destroy: destroy private data of set instance
315  *      @elemsize: element private size
316  *
317  *      Operations lookup, update and delete have simpler interfaces, are faster
318  *      and currently only used in the packet path. All the rest are slower,
319  *      control plane functions.
320  */
321 struct nft_set_ops {
322         bool                            (*lookup)(const struct net *net,
323                                                   const struct nft_set *set,
324                                                   const u32 *key,
325                                                   const struct nft_set_ext **ext);
326         bool                            (*update)(struct nft_set *set,
327                                                   const u32 *key,
328                                                   void *(*new)(struct nft_set *,
329                                                                const struct nft_expr *,
330                                                                struct nft_regs *),
331                                                   const struct nft_expr *expr,
332                                                   struct nft_regs *regs,
333                                                   const struct nft_set_ext **ext);
334         bool                            (*delete)(const struct nft_set *set,
335                                                   const u32 *key);
336
337         int                             (*insert)(const struct net *net,
338                                                   const struct nft_set *set,
339                                                   const struct nft_set_elem *elem,
340                                                   struct nft_set_ext **ext);
341         void                            (*activate)(const struct net *net,
342                                                     const struct nft_set *set,
343                                                     const struct nft_set_elem *elem);
344         void *                          (*deactivate)(const struct net *net,
345                                                       const struct nft_set *set,
346                                                       const struct nft_set_elem *elem);
347         bool                            (*flush)(const struct net *net,
348                                                  const struct nft_set *set,
349                                                  void *priv);
350         void                            (*remove)(const struct net *net,
351                                                   const struct nft_set *set,
352                                                   const struct nft_set_elem *elem);
353         void                            (*walk)(const struct nft_ctx *ctx,
354                                                 struct nft_set *set,
355                                                 struct nft_set_iter *iter);
356         void *                          (*get)(const struct net *net,
357                                                const struct nft_set *set,
358                                                const struct nft_set_elem *elem,
359                                                unsigned int flags);
360
361         u64                             (*privsize)(const struct nlattr * const nla[],
362                                                     const struct nft_set_desc *desc);
363         bool                            (*estimate)(const struct nft_set_desc *desc,
364                                                     u32 features,
365                                                     struct nft_set_estimate *est);
366         int                             (*init)(const struct nft_set *set,
367                                                 const struct nft_set_desc *desc,
368                                                 const struct nlattr * const nla[]);
369         void                            (*destroy)(const struct nft_set *set);
370         void                            (*gc_init)(const struct nft_set *set);
371
372         unsigned int                    elemsize;
373 };
374
375 /**
376  *      struct nft_set_type - nf_tables set type
377  *
378  *      @ops: set ops for this type
379  *      @list: used internally
380  *      @owner: module reference
381  *      @features: features supported by the implementation
382  */
383 struct nft_set_type {
384         const struct nft_set_ops        ops;
385         struct list_head                list;
386         struct module                   *owner;
387         u32                             features;
388 };
389 #define to_set_type(o) container_of(o, struct nft_set_type, ops)
390
391 int nft_register_set(struct nft_set_type *type);
392 void nft_unregister_set(struct nft_set_type *type);
393
394 /**
395  *      struct nft_set - nf_tables set instance
396  *
397  *      @list: table set list node
398  *      @bindings: list of set bindings
399  *      @table: table this set belongs to
400  *      @net: netnamespace this set belongs to
401  *      @name: name of the set
402  *      @handle: unique handle of the set
403  *      @ktype: key type (numeric type defined by userspace, not used in the kernel)
404  *      @dtype: data type (verdict or numeric type defined by userspace)
405  *      @objtype: object type (see NFT_OBJECT_* definitions)
406  *      @size: maximum set size
407  *      @use: number of rules references to this set
408  *      @nelems: number of elements
409  *      @ndeact: number of deactivated elements queued for removal
410  *      @timeout: default timeout value in jiffies
411  *      @gc_int: garbage collection interval in msecs
412  *      @policy: set parameterization (see enum nft_set_policies)
413  *      @udlen: user data length
414  *      @udata: user data
415  *      @ops: set ops
416  *      @flags: set flags
417  *      @genmask: generation mask
418  *      @klen: key length
419  *      @dlen: data length
420  *      @data: private set data
421  */
422 struct nft_set {
423         struct list_head                list;
424         struct list_head                bindings;
425         struct nft_table                *table;
426         possible_net_t                  net;
427         char                            *name;
428         u64                             handle;
429         u32                             ktype;
430         u32                             dtype;
431         u32                             objtype;
432         u32                             size;
433         u32                             use;
434         atomic_t                        nelems;
435         u32                             ndeact;
436         u64                             timeout;
437         u32                             gc_int;
438         u16                             policy;
439         u16                             udlen;
440         unsigned char                   *udata;
441         /* runtime data below here */
442         const struct nft_set_ops        *ops ____cacheline_aligned;
443         u16                             flags:14,
444                                         genmask:2;
445         u8                              klen;
446         u8                              dlen;
447         unsigned char                   data[]
448                 __attribute__((aligned(__alignof__(u64))));
449 };
450
451 static inline bool nft_set_is_anonymous(const struct nft_set *set)
452 {
453         return set->flags & NFT_SET_ANONYMOUS;
454 }
455
456 static inline void *nft_set_priv(const struct nft_set *set)
457 {
458         return (void *)set->data;
459 }
460
461 static inline struct nft_set *nft_set_container_of(const void *priv)
462 {
463         return (void *)priv - offsetof(struct nft_set, data);
464 }
465
466 struct nft_set *nft_set_lookup_global(const struct net *net,
467                                       const struct nft_table *table,
468                                       const struct nlattr *nla_set_name,
469                                       const struct nlattr *nla_set_id,
470                                       u8 genmask);
471
472 static inline unsigned long nft_set_gc_interval(const struct nft_set *set)
473 {
474         return set->gc_int ? msecs_to_jiffies(set->gc_int) : HZ;
475 }
476
477 /**
478  *      struct nft_set_binding - nf_tables set binding
479  *
480  *      @list: set bindings list node
481  *      @chain: chain containing the rule bound to the set
482  *      @flags: set action flags
483  *
484  *      A set binding contains all information necessary for validation
485  *      of new elements added to a bound set.
486  */
487 struct nft_set_binding {
488         struct list_head                list;
489         const struct nft_chain          *chain;
490         u32                             flags;
491 };
492
493 enum nft_trans_phase;
494 void nf_tables_deactivate_set(const struct nft_ctx *ctx, struct nft_set *set,
495                               struct nft_set_binding *binding,
496                               enum nft_trans_phase phase);
497 int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
498                        struct nft_set_binding *binding);
499 void nf_tables_destroy_set(const struct nft_ctx *ctx, struct nft_set *set);
500
501 /**
502  *      enum nft_set_extensions - set extension type IDs
503  *
504  *      @NFT_SET_EXT_KEY: element key
505  *      @NFT_SET_EXT_DATA: mapping data
506  *      @NFT_SET_EXT_FLAGS: element flags
507  *      @NFT_SET_EXT_TIMEOUT: element timeout
508  *      @NFT_SET_EXT_EXPIRATION: element expiration time
509  *      @NFT_SET_EXT_USERDATA: user data associated with the element
510  *      @NFT_SET_EXT_EXPR: expression assiociated with the element
511  *      @NFT_SET_EXT_OBJREF: stateful object reference associated with element
512  *      @NFT_SET_EXT_NUM: number of extension types
513  */
514 enum nft_set_extensions {
515         NFT_SET_EXT_KEY,
516         NFT_SET_EXT_DATA,
517         NFT_SET_EXT_FLAGS,
518         NFT_SET_EXT_TIMEOUT,
519         NFT_SET_EXT_EXPIRATION,
520         NFT_SET_EXT_USERDATA,
521         NFT_SET_EXT_EXPR,
522         NFT_SET_EXT_OBJREF,
523         NFT_SET_EXT_NUM
524 };
525
526 /**
527  *      struct nft_set_ext_type - set extension type
528  *
529  *      @len: fixed part length of the extension
530  *      @align: alignment requirements of the extension
531  */
532 struct nft_set_ext_type {
533         u8      len;
534         u8      align;
535 };
536
537 extern const struct nft_set_ext_type nft_set_ext_types[];
538
539 /**
540  *      struct nft_set_ext_tmpl - set extension template
541  *
542  *      @len: length of extension area
543  *      @offset: offsets of individual extension types
544  */
545 struct nft_set_ext_tmpl {
546         u16     len;
547         u8      offset[NFT_SET_EXT_NUM];
548 };
549
550 /**
551  *      struct nft_set_ext - set extensions
552  *
553  *      @genmask: generation mask
554  *      @offset: offsets of individual extension types
555  *      @data: beginning of extension data
556  */
557 struct nft_set_ext {
558         u8      genmask;
559         u8      offset[NFT_SET_EXT_NUM];
560         char    data[0];
561 };
562
563 static inline void nft_set_ext_prepare(struct nft_set_ext_tmpl *tmpl)
564 {
565         memset(tmpl, 0, sizeof(*tmpl));
566         tmpl->len = sizeof(struct nft_set_ext);
567 }
568
569 static inline void nft_set_ext_add_length(struct nft_set_ext_tmpl *tmpl, u8 id,
570                                           unsigned int len)
571 {
572         tmpl->len        = ALIGN(tmpl->len, nft_set_ext_types[id].align);
573         BUG_ON(tmpl->len > U8_MAX);
574         tmpl->offset[id] = tmpl->len;
575         tmpl->len       += nft_set_ext_types[id].len + len;
576 }
577
578 static inline void nft_set_ext_add(struct nft_set_ext_tmpl *tmpl, u8 id)
579 {
580         nft_set_ext_add_length(tmpl, id, 0);
581 }
582
583 static inline void nft_set_ext_init(struct nft_set_ext *ext,
584                                     const struct nft_set_ext_tmpl *tmpl)
585 {
586         memcpy(ext->offset, tmpl->offset, sizeof(ext->offset));
587 }
588
589 static inline bool __nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
590 {
591         return !!ext->offset[id];
592 }
593
594 static inline bool nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
595 {
596         return ext && __nft_set_ext_exists(ext, id);
597 }
598
599 static inline void *nft_set_ext(const struct nft_set_ext *ext, u8 id)
600 {
601         return (void *)ext + ext->offset[id];
602 }
603
604 static inline struct nft_data *nft_set_ext_key(const struct nft_set_ext *ext)
605 {
606         return nft_set_ext(ext, NFT_SET_EXT_KEY);
607 }
608
609 static inline struct nft_data *nft_set_ext_data(const struct nft_set_ext *ext)
610 {
611         return nft_set_ext(ext, NFT_SET_EXT_DATA);
612 }
613
614 static inline u8 *nft_set_ext_flags(const struct nft_set_ext *ext)
615 {
616         return nft_set_ext(ext, NFT_SET_EXT_FLAGS);
617 }
618
619 static inline u64 *nft_set_ext_timeout(const struct nft_set_ext *ext)
620 {
621         return nft_set_ext(ext, NFT_SET_EXT_TIMEOUT);
622 }
623
624 static inline u64 *nft_set_ext_expiration(const struct nft_set_ext *ext)
625 {
626         return nft_set_ext(ext, NFT_SET_EXT_EXPIRATION);
627 }
628
629 static inline struct nft_userdata *nft_set_ext_userdata(const struct nft_set_ext *ext)
630 {
631         return nft_set_ext(ext, NFT_SET_EXT_USERDATA);
632 }
633
634 static inline struct nft_expr *nft_set_ext_expr(const struct nft_set_ext *ext)
635 {
636         return nft_set_ext(ext, NFT_SET_EXT_EXPR);
637 }
638
639 static inline bool nft_set_elem_expired(const struct nft_set_ext *ext)
640 {
641         return nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION) &&
642                time_is_before_eq_jiffies64(*nft_set_ext_expiration(ext));
643 }
644
645 static inline struct nft_set_ext *nft_set_elem_ext(const struct nft_set *set,
646                                                    void *elem)
647 {
648         return elem + set->ops->elemsize;
649 }
650
651 static inline struct nft_object **nft_set_ext_obj(const struct nft_set_ext *ext)
652 {
653         return nft_set_ext(ext, NFT_SET_EXT_OBJREF);
654 }
655
656 void *nft_set_elem_init(const struct nft_set *set,
657                         const struct nft_set_ext_tmpl *tmpl,
658                         const u32 *key, const u32 *data,
659                         u64 timeout, u64 expiration, gfp_t gfp);
660 void nft_set_elem_destroy(const struct nft_set *set, void *elem,
661                           bool destroy_expr);
662
663 /**
664  *      struct nft_set_gc_batch_head - nf_tables set garbage collection batch
665  *
666  *      @rcu: rcu head
667  *      @set: set the elements belong to
668  *      @cnt: count of elements
669  */
670 struct nft_set_gc_batch_head {
671         struct rcu_head                 rcu;
672         const struct nft_set            *set;
673         unsigned int                    cnt;
674 };
675
676 #define NFT_SET_GC_BATCH_SIZE   ((PAGE_SIZE -                             \
677                                   sizeof(struct nft_set_gc_batch_head)) / \
678                                  sizeof(void *))
679
680 /**
681  *      struct nft_set_gc_batch - nf_tables set garbage collection batch
682  *
683  *      @head: GC batch head
684  *      @elems: garbage collection elements
685  */
686 struct nft_set_gc_batch {
687         struct nft_set_gc_batch_head    head;
688         void                            *elems[NFT_SET_GC_BATCH_SIZE];
689 };
690
691 struct nft_set_gc_batch *nft_set_gc_batch_alloc(const struct nft_set *set,
692                                                 gfp_t gfp);
693 void nft_set_gc_batch_release(struct rcu_head *rcu);
694
695 static inline void nft_set_gc_batch_complete(struct nft_set_gc_batch *gcb)
696 {
697         if (gcb != NULL)
698                 call_rcu(&gcb->head.rcu, nft_set_gc_batch_release);
699 }
700
701 static inline struct nft_set_gc_batch *
702 nft_set_gc_batch_check(const struct nft_set *set, struct nft_set_gc_batch *gcb,
703                        gfp_t gfp)
704 {
705         if (gcb != NULL) {
706                 if (gcb->head.cnt + 1 < ARRAY_SIZE(gcb->elems))
707                         return gcb;
708                 nft_set_gc_batch_complete(gcb);
709         }
710         return nft_set_gc_batch_alloc(set, gfp);
711 }
712
713 static inline void nft_set_gc_batch_add(struct nft_set_gc_batch *gcb,
714                                         void *elem)
715 {
716         gcb->elems[gcb->head.cnt++] = elem;
717 }
718
719 struct nft_expr_ops;
720 /**
721  *      struct nft_expr_type - nf_tables expression type
722  *
723  *      @select_ops: function to select nft_expr_ops
724  *      @release_ops: release nft_expr_ops
725  *      @ops: default ops, used when no select_ops functions is present
726  *      @list: used internally
727  *      @name: Identifier
728  *      @owner: module reference
729  *      @policy: netlink attribute policy
730  *      @maxattr: highest netlink attribute number
731  *      @family: address family for AF-specific types
732  *      @flags: expression type flags
733  */
734 struct nft_expr_type {
735         const struct nft_expr_ops       *(*select_ops)(const struct nft_ctx *,
736                                                        const struct nlattr * const tb[]);
737         void                            (*release_ops)(const struct nft_expr_ops *ops);
738         const struct nft_expr_ops       *ops;
739         struct list_head                list;
740         const char                      *name;
741         struct module                   *owner;
742         const struct nla_policy         *policy;
743         unsigned int                    maxattr;
744         u8                              family;
745         u8                              flags;
746 };
747
748 #define NFT_EXPR_STATEFUL               0x1
749 #define NFT_EXPR_GC                     0x2
750
751 enum nft_trans_phase {
752         NFT_TRANS_PREPARE,
753         NFT_TRANS_ABORT,
754         NFT_TRANS_COMMIT,
755         NFT_TRANS_RELEASE
756 };
757
758 struct nft_flow_rule;
759 struct nft_offload_ctx;
760
761 /**
762  *      struct nft_expr_ops - nf_tables expression operations
763  *
764  *      @eval: Expression evaluation function
765  *      @size: full expression size, including private data size
766  *      @init: initialization function
767  *      @activate: activate expression in the next generation
768  *      @deactivate: deactivate expression in next generation
769  *      @destroy: destruction function, called after synchronize_rcu
770  *      @dump: function to dump parameters
771  *      @type: expression type
772  *      @validate: validate expression, called during loop detection
773  *      @data: extra data to attach to this expression operation
774  */
775 struct nft_expr;
776 struct nft_expr_ops {
777         void                            (*eval)(const struct nft_expr *expr,
778                                                 struct nft_regs *regs,
779                                                 const struct nft_pktinfo *pkt);
780         int                             (*clone)(struct nft_expr *dst,
781                                                  const struct nft_expr *src);
782         unsigned int                    size;
783
784         int                             (*init)(const struct nft_ctx *ctx,
785                                                 const struct nft_expr *expr,
786                                                 const struct nlattr * const tb[]);
787         void                            (*activate)(const struct nft_ctx *ctx,
788                                                     const struct nft_expr *expr);
789         void                            (*deactivate)(const struct nft_ctx *ctx,
790                                                       const struct nft_expr *expr,
791                                                       enum nft_trans_phase phase);
792         void                            (*destroy)(const struct nft_ctx *ctx,
793                                                    const struct nft_expr *expr);
794         void                            (*destroy_clone)(const struct nft_ctx *ctx,
795                                                          const struct nft_expr *expr);
796         int                             (*dump)(struct sk_buff *skb,
797                                                 const struct nft_expr *expr);
798         int                             (*validate)(const struct nft_ctx *ctx,
799                                                     const struct nft_expr *expr,
800                                                     const struct nft_data **data);
801         bool                            (*gc)(struct net *net,
802                                               const struct nft_expr *expr);
803         int                             (*offload)(struct nft_offload_ctx *ctx,
804                                                    struct nft_flow_rule *flow,
805                                                    const struct nft_expr *expr);
806         u32                             offload_flags;
807         const struct nft_expr_type      *type;
808         void                            *data;
809 };
810
811 #define NFT_EXPR_MAXATTR                16
812 #define NFT_EXPR_SIZE(size)             (sizeof(struct nft_expr) + \
813                                          ALIGN(size, __alignof__(struct nft_expr)))
814
815 /**
816  *      struct nft_expr - nf_tables expression
817  *
818  *      @ops: expression ops
819  *      @data: expression private data
820  */
821 struct nft_expr {
822         const struct nft_expr_ops       *ops;
823         unsigned char                   data[]
824                 __attribute__((aligned(__alignof__(u64))));
825 };
826
827 static inline void *nft_expr_priv(const struct nft_expr *expr)
828 {
829         return (void *)expr->data;
830 }
831
832 struct nft_expr *nft_expr_init(const struct nft_ctx *ctx,
833                                const struct nlattr *nla);
834 void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr);
835 int nft_expr_dump(struct sk_buff *skb, unsigned int attr,
836                   const struct nft_expr *expr);
837
838 /**
839  *      struct nft_rule - nf_tables rule
840  *
841  *      @list: used internally
842  *      @handle: rule handle
843  *      @genmask: generation mask
844  *      @dlen: length of expression data
845  *      @udata: user data is appended to the rule
846  *      @data: expression data
847  */
848 struct nft_rule {
849         struct list_head                list;
850         u64                             handle:42,
851                                         genmask:2,
852                                         dlen:12,
853                                         udata:1;
854         unsigned char                   data[]
855                 __attribute__((aligned(__alignof__(struct nft_expr))));
856 };
857
858 static inline struct nft_expr *nft_expr_first(const struct nft_rule *rule)
859 {
860         return (struct nft_expr *)&rule->data[0];
861 }
862
863 static inline struct nft_expr *nft_expr_next(const struct nft_expr *expr)
864 {
865         return ((void *)expr) + expr->ops->size;
866 }
867
868 static inline struct nft_expr *nft_expr_last(const struct nft_rule *rule)
869 {
870         return (struct nft_expr *)&rule->data[rule->dlen];
871 }
872
873 static inline struct nft_userdata *nft_userdata(const struct nft_rule *rule)
874 {
875         return (void *)&rule->data[rule->dlen];
876 }
877
878 /*
879  * The last pointer isn't really necessary, but the compiler isn't able to
880  * determine that the result of nft_expr_last() is always the same since it
881  * can't assume that the dlen value wasn't changed within calls in the loop.
882  */
883 #define nft_rule_for_each_expr(expr, last, rule) \
884         for ((expr) = nft_expr_first(rule), (last) = nft_expr_last(rule); \
885              (expr) != (last); \
886              (expr) = nft_expr_next(expr))
887
888 enum nft_chain_flags {
889         NFT_BASE_CHAIN                  = 0x1,
890         NFT_CHAIN_HW_OFFLOAD            = 0x2,
891 };
892
893 #define NFT_CHAIN_POLICY_UNSET          U8_MAX
894
895 /**
896  *      struct nft_chain - nf_tables chain
897  *
898  *      @rules: list of rules in the chain
899  *      @list: used internally
900  *      @rhlhead: used internally
901  *      @table: table that this chain belongs to
902  *      @handle: chain handle
903  *      @use: number of jump references to this chain
904  *      @flags: bitmask of enum nft_chain_flags
905  *      @name: name of the chain
906  */
907 struct nft_chain {
908         struct nft_rule                 *__rcu *rules_gen_0;
909         struct nft_rule                 *__rcu *rules_gen_1;
910         struct list_head                rules;
911         struct list_head                list;
912         struct rhlist_head              rhlhead;
913         struct nft_table                *table;
914         u64                             handle;
915         u32                             use;
916         u8                              flags:6,
917                                         genmask:2;
918         char                            *name;
919
920         /* Only used during control plane commit phase: */
921         struct nft_rule                 **rules_next;
922 };
923
924 int nft_chain_validate(const struct nft_ctx *ctx, const struct nft_chain *chain);
925
926 enum nft_chain_types {
927         NFT_CHAIN_T_DEFAULT = 0,
928         NFT_CHAIN_T_ROUTE,
929         NFT_CHAIN_T_NAT,
930         NFT_CHAIN_T_MAX
931 };
932
933 /**
934  *      struct nft_chain_type - nf_tables chain type info
935  *
936  *      @name: name of the type
937  *      @type: numeric identifier
938  *      @family: address family
939  *      @owner: module owner
940  *      @hook_mask: mask of valid hooks
941  *      @hooks: array of hook functions
942  *      @ops_register: base chain register function
943  *      @ops_unregister: base chain unregister function
944  */
945 struct nft_chain_type {
946         const char                      *name;
947         enum nft_chain_types            type;
948         int                             family;
949         struct module                   *owner;
950         unsigned int                    hook_mask;
951         nf_hookfn                       *hooks[NF_MAX_HOOKS];
952         int                             (*ops_register)(struct net *net, const struct nf_hook_ops *ops);
953         void                            (*ops_unregister)(struct net *net, const struct nf_hook_ops *ops);
954 };
955
956 int nft_chain_validate_dependency(const struct nft_chain *chain,
957                                   enum nft_chain_types type);
958 int nft_chain_validate_hooks(const struct nft_chain *chain,
959                              unsigned int hook_flags);
960
961 struct nft_stats {
962         u64                     bytes;
963         u64                     pkts;
964         struct u64_stats_sync   syncp;
965 };
966
967 struct nft_hook {
968         struct list_head        list;
969         struct nf_hook_ops      ops;
970         struct rcu_head         rcu;
971 };
972
973 /**
974  *      struct nft_base_chain - nf_tables base chain
975  *
976  *      @ops: netfilter hook ops
977  *      @hook_list: list of netfilter hooks (for NFPROTO_NETDEV family)
978  *      @type: chain type
979  *      @policy: default policy
980  *      @stats: per-cpu chain stats
981  *      @chain: the chain
982  *      @flow_block: flow block (for hardware offload)
983  */
984 struct nft_base_chain {
985         struct nf_hook_ops              ops;
986         struct list_head                hook_list;
987         const struct nft_chain_type     *type;
988         u8                              policy;
989         u8                              flags;
990         struct nft_stats __percpu       *stats;
991         struct nft_chain                chain;
992         struct flow_block               flow_block;
993 };
994
995 static inline struct nft_base_chain *nft_base_chain(const struct nft_chain *chain)
996 {
997         return container_of(chain, struct nft_base_chain, chain);
998 }
999
1000 static inline bool nft_is_base_chain(const struct nft_chain *chain)
1001 {
1002         return chain->flags & NFT_BASE_CHAIN;
1003 }
1004
1005 int __nft_release_basechain(struct nft_ctx *ctx);
1006
1007 unsigned int nft_do_chain(struct nft_pktinfo *pkt, void *priv);
1008
1009 /**
1010  *      struct nft_table - nf_tables table
1011  *
1012  *      @list: used internally
1013  *      @chains_ht: chains in the table
1014  *      @chains: same, for stable walks
1015  *      @sets: sets in the table
1016  *      @objects: stateful objects in the table
1017  *      @flowtables: flow tables in the table
1018  *      @hgenerator: handle generator state
1019  *      @handle: table handle
1020  *      @use: number of chain references to this table
1021  *      @flags: table flag (see enum nft_table_flags)
1022  *      @genmask: generation mask
1023  *      @afinfo: address family info
1024  *      @name: name of the table
1025  */
1026 struct nft_table {
1027         struct list_head                list;
1028         struct rhltable                 chains_ht;
1029         struct list_head                chains;
1030         struct list_head                sets;
1031         struct list_head                objects;
1032         struct list_head                flowtables;
1033         u64                             hgenerator;
1034         u64                             handle;
1035         u32                             use;
1036         u16                             family:6,
1037                                         flags:8,
1038                                         genmask:2;
1039         char                            *name;
1040 };
1041
1042 void nft_register_chain_type(const struct nft_chain_type *);
1043 void nft_unregister_chain_type(const struct nft_chain_type *);
1044
1045 int nft_register_expr(struct nft_expr_type *);
1046 void nft_unregister_expr(struct nft_expr_type *);
1047
1048 int nft_verdict_dump(struct sk_buff *skb, int type,
1049                      const struct nft_verdict *v);
1050
1051 /**
1052  *      struct nft_object_hash_key - key to lookup nft_object
1053  *
1054  *      @name: name of the stateful object to look up
1055  *      @table: table the object belongs to
1056  */
1057 struct nft_object_hash_key {
1058         const char                      *name;
1059         const struct nft_table          *table;
1060 };
1061
1062 /**
1063  *      struct nft_object - nf_tables stateful object
1064  *
1065  *      @list: table stateful object list node
1066  *      @key:  keys that identify this object
1067  *      @rhlhead: nft_objname_ht node
1068  *      @genmask: generation mask
1069  *      @use: number of references to this stateful object
1070  *      @handle: unique object handle
1071  *      @ops: object operations
1072  *      @data: object data, layout depends on type
1073  */
1074 struct nft_object {
1075         struct list_head                list;
1076         struct rhlist_head              rhlhead;
1077         struct nft_object_hash_key      key;
1078         u32                             genmask:2,
1079                                         use:30;
1080         u64                             handle;
1081         /* runtime data below here */
1082         const struct nft_object_ops     *ops ____cacheline_aligned;
1083         unsigned char                   data[]
1084                 __attribute__((aligned(__alignof__(u64))));
1085 };
1086
1087 static inline void *nft_obj_data(const struct nft_object *obj)
1088 {
1089         return (void *)obj->data;
1090 }
1091
1092 #define nft_expr_obj(expr)      *((struct nft_object **)nft_expr_priv(expr))
1093
1094 struct nft_object *nft_obj_lookup(const struct net *net,
1095                                   const struct nft_table *table,
1096                                   const struct nlattr *nla, u32 objtype,
1097                                   u8 genmask);
1098
1099 void nft_obj_notify(struct net *net, const struct nft_table *table,
1100                     struct nft_object *obj, u32 portid, u32 seq,
1101                     int event, int family, int report, gfp_t gfp);
1102
1103 /**
1104  *      struct nft_object_type - stateful object type
1105  *
1106  *      @select_ops: function to select nft_object_ops
1107  *      @ops: default ops, used when no select_ops functions is present
1108  *      @list: list node in list of object types
1109  *      @type: stateful object numeric type
1110  *      @owner: module owner
1111  *      @maxattr: maximum netlink attribute
1112  *      @policy: netlink attribute policy
1113  */
1114 struct nft_object_type {
1115         const struct nft_object_ops     *(*select_ops)(const struct nft_ctx *,
1116                                                        const struct nlattr * const tb[]);
1117         const struct nft_object_ops     *ops;
1118         struct list_head                list;
1119         u32                             type;
1120         unsigned int                    maxattr;
1121         struct module                   *owner;
1122         const struct nla_policy         *policy;
1123 };
1124
1125 /**
1126  *      struct nft_object_ops - stateful object operations
1127  *
1128  *      @eval: stateful object evaluation function
1129  *      @size: stateful object size
1130  *      @init: initialize object from netlink attributes
1131  *      @destroy: release existing stateful object
1132  *      @dump: netlink dump stateful object
1133  *      @update: update stateful object
1134  */
1135 struct nft_object_ops {
1136         void                            (*eval)(struct nft_object *obj,
1137                                                 struct nft_regs *regs,
1138                                                 const struct nft_pktinfo *pkt);
1139         unsigned int                    size;
1140         int                             (*init)(const struct nft_ctx *ctx,
1141                                                 const struct nlattr *const tb[],
1142                                                 struct nft_object *obj);
1143         void                            (*destroy)(const struct nft_ctx *ctx,
1144                                                    struct nft_object *obj);
1145         int                             (*dump)(struct sk_buff *skb,
1146                                                 struct nft_object *obj,
1147                                                 bool reset);
1148         void                            (*update)(struct nft_object *obj,
1149                                                   struct nft_object *newobj);
1150         const struct nft_object_type    *type;
1151 };
1152
1153 int nft_register_obj(struct nft_object_type *obj_type);
1154 void nft_unregister_obj(struct nft_object_type *obj_type);
1155
1156 #define NFT_NETDEVICE_MAX       256
1157
1158 /**
1159  *      struct nft_flowtable - nf_tables flow table
1160  *
1161  *      @list: flow table list node in table list
1162  *      @table: the table the flow table is contained in
1163  *      @name: name of this flow table
1164  *      @hooknum: hook number
1165  *      @ops_len: number of hooks in array
1166  *      @genmask: generation mask
1167  *      @use: number of references to this flow table
1168  *      @handle: unique object handle
1169  *      @dev_name: array of device names
1170  *      @data: rhashtable and garbage collector
1171  *      @ops: array of hooks
1172  */
1173 struct nft_flowtable {
1174         struct list_head                list;
1175         struct nft_table                *table;
1176         char                            *name;
1177         int                             hooknum;
1178         int                             ops_len;
1179         u32                             genmask:2,
1180                                         use:30;
1181         u64                             handle;
1182         /* runtime data below here */
1183         struct list_head                hook_list ____cacheline_aligned;
1184         struct nf_flowtable             data;
1185 };
1186
1187 struct nft_flowtable *nft_flowtable_lookup(const struct nft_table *table,
1188                                            const struct nlattr *nla,
1189                                            u8 genmask);
1190
1191 void nf_tables_deactivate_flowtable(const struct nft_ctx *ctx,
1192                                     struct nft_flowtable *flowtable,
1193                                     enum nft_trans_phase phase);
1194
1195 void nft_register_flowtable_type(struct nf_flowtable_type *type);
1196 void nft_unregister_flowtable_type(struct nf_flowtable_type *type);
1197
1198 /**
1199  *      struct nft_traceinfo - nft tracing information and state
1200  *
1201  *      @pkt: pktinfo currently processed
1202  *      @basechain: base chain currently processed
1203  *      @chain: chain currently processed
1204  *      @rule:  rule that was evaluated
1205  *      @verdict: verdict given by rule
1206  *      @type: event type (enum nft_trace_types)
1207  *      @packet_dumped: packet headers sent in a previous traceinfo message
1208  *      @trace: other struct members are initialised
1209  */
1210 struct nft_traceinfo {
1211         const struct nft_pktinfo        *pkt;
1212         const struct nft_base_chain     *basechain;
1213         const struct nft_chain          *chain;
1214         const struct nft_rule           *rule;
1215         const struct nft_verdict        *verdict;
1216         enum nft_trace_types            type;
1217         bool                            packet_dumped;
1218         bool                            trace;
1219 };
1220
1221 void nft_trace_init(struct nft_traceinfo *info, const struct nft_pktinfo *pkt,
1222                     const struct nft_verdict *verdict,
1223                     const struct nft_chain *basechain);
1224
1225 void nft_trace_notify(struct nft_traceinfo *info);
1226
1227 #define MODULE_ALIAS_NFT_CHAIN(family, name) \
1228         MODULE_ALIAS("nft-chain-" __stringify(family) "-" name)
1229
1230 #define MODULE_ALIAS_NFT_AF_EXPR(family, name) \
1231         MODULE_ALIAS("nft-expr-" __stringify(family) "-" name)
1232
1233 #define MODULE_ALIAS_NFT_EXPR(name) \
1234         MODULE_ALIAS("nft-expr-" name)
1235
1236 #define MODULE_ALIAS_NFT_SET() \
1237         MODULE_ALIAS("nft-set")
1238
1239 #define MODULE_ALIAS_NFT_OBJ(type) \
1240         MODULE_ALIAS("nft-obj-" __stringify(type))
1241
1242 #if IS_ENABLED(CONFIG_NF_TABLES)
1243
1244 /*
1245  * The gencursor defines two generations, the currently active and the
1246  * next one. Objects contain a bitmask of 2 bits specifying the generations
1247  * they're active in. A set bit means they're inactive in the generation
1248  * represented by that bit.
1249  *
1250  * New objects start out as inactive in the current and active in the
1251  * next generation. When committing the ruleset the bitmask is cleared,
1252  * meaning they're active in all generations. When removing an object,
1253  * it is set inactive in the next generation. After committing the ruleset,
1254  * the objects are removed.
1255  */
1256 static inline unsigned int nft_gencursor_next(const struct net *net)
1257 {
1258         return net->nft.gencursor + 1 == 1 ? 1 : 0;
1259 }
1260
1261 static inline u8 nft_genmask_next(const struct net *net)
1262 {
1263         return 1 << nft_gencursor_next(net);
1264 }
1265
1266 static inline u8 nft_genmask_cur(const struct net *net)
1267 {
1268         /* Use READ_ONCE() to prevent refetching the value for atomicity */
1269         return 1 << READ_ONCE(net->nft.gencursor);
1270 }
1271
1272 #define NFT_GENMASK_ANY         ((1 << 0) | (1 << 1))
1273
1274 /*
1275  * Generic transaction helpers
1276  */
1277
1278 /* Check if this object is currently active. */
1279 #define nft_is_active(__net, __obj)                             \
1280         (((__obj)->genmask & nft_genmask_cur(__net)) == 0)
1281
1282 /* Check if this object is active in the next generation. */
1283 #define nft_is_active_next(__net, __obj)                        \
1284         (((__obj)->genmask & nft_genmask_next(__net)) == 0)
1285
1286 /* This object becomes active in the next generation. */
1287 #define nft_activate_next(__net, __obj)                         \
1288         (__obj)->genmask = nft_genmask_cur(__net)
1289
1290 /* This object becomes inactive in the next generation. */
1291 #define nft_deactivate_next(__net, __obj)                       \
1292         (__obj)->genmask = nft_genmask_next(__net)
1293
1294 /* After committing the ruleset, clear the stale generation bit. */
1295 #define nft_clear(__net, __obj)                                 \
1296         (__obj)->genmask &= ~nft_genmask_next(__net)
1297 #define nft_active_genmask(__obj, __genmask)                    \
1298         !((__obj)->genmask & __genmask)
1299
1300 /*
1301  * Set element transaction helpers
1302  */
1303
1304 static inline bool nft_set_elem_active(const struct nft_set_ext *ext,
1305                                        u8 genmask)
1306 {
1307         return !(ext->genmask & genmask);
1308 }
1309
1310 static inline void nft_set_elem_change_active(const struct net *net,
1311                                               const struct nft_set *set,
1312                                               struct nft_set_ext *ext)
1313 {
1314         ext->genmask ^= nft_genmask_next(net);
1315 }
1316
1317 #endif /* IS_ENABLED(CONFIG_NF_TABLES) */
1318
1319 /*
1320  * We use a free bit in the genmask field to indicate the element
1321  * is busy, meaning it is currently being processed either by
1322  * the netlink API or GC.
1323  *
1324  * Even though the genmask is only a single byte wide, this works
1325  * because the extension structure if fully constant once initialized,
1326  * so there are no non-atomic write accesses unless it is already
1327  * marked busy.
1328  */
1329 #define NFT_SET_ELEM_BUSY_MASK  (1 << 2)
1330
1331 #if defined(__LITTLE_ENDIAN_BITFIELD)
1332 #define NFT_SET_ELEM_BUSY_BIT   2
1333 #elif defined(__BIG_ENDIAN_BITFIELD)
1334 #define NFT_SET_ELEM_BUSY_BIT   (BITS_PER_LONG - BITS_PER_BYTE + 2)
1335 #else
1336 #error
1337 #endif
1338
1339 static inline int nft_set_elem_mark_busy(struct nft_set_ext *ext)
1340 {
1341         unsigned long *word = (unsigned long *)ext;
1342
1343         BUILD_BUG_ON(offsetof(struct nft_set_ext, genmask) != 0);
1344         return test_and_set_bit(NFT_SET_ELEM_BUSY_BIT, word);
1345 }
1346
1347 static inline void nft_set_elem_clear_busy(struct nft_set_ext *ext)
1348 {
1349         unsigned long *word = (unsigned long *)ext;
1350
1351         clear_bit(NFT_SET_ELEM_BUSY_BIT, word);
1352 }
1353
1354 /**
1355  *      struct nft_trans - nf_tables object update in transaction
1356  *
1357  *      @list: used internally
1358  *      @msg_type: message type
1359  *      @put_net: ctx->net needs to be put
1360  *      @ctx: transaction context
1361  *      @data: internal information related to the transaction
1362  */
1363 struct nft_trans {
1364         struct list_head                list;
1365         int                             msg_type;
1366         bool                            put_net;
1367         struct nft_ctx                  ctx;
1368         char                            data[0];
1369 };
1370
1371 struct nft_trans_rule {
1372         struct nft_rule                 *rule;
1373         struct nft_flow_rule            *flow;
1374         u32                             rule_id;
1375 };
1376
1377 #define nft_trans_rule(trans)   \
1378         (((struct nft_trans_rule *)trans->data)->rule)
1379 #define nft_trans_flow_rule(trans)      \
1380         (((struct nft_trans_rule *)trans->data)->flow)
1381 #define nft_trans_rule_id(trans)        \
1382         (((struct nft_trans_rule *)trans->data)->rule_id)
1383
1384 struct nft_trans_set {
1385         struct nft_set                  *set;
1386         u32                             set_id;
1387         bool                            bound;
1388 };
1389
1390 #define nft_trans_set(trans)    \
1391         (((struct nft_trans_set *)trans->data)->set)
1392 #define nft_trans_set_id(trans) \
1393         (((struct nft_trans_set *)trans->data)->set_id)
1394 #define nft_trans_set_bound(trans)      \
1395         (((struct nft_trans_set *)trans->data)->bound)
1396
1397 struct nft_trans_chain {
1398         bool                            update;
1399         char                            *name;
1400         struct nft_stats __percpu       *stats;
1401         u8                              policy;
1402 };
1403
1404 #define nft_trans_chain_update(trans)   \
1405         (((struct nft_trans_chain *)trans->data)->update)
1406 #define nft_trans_chain_name(trans)     \
1407         (((struct nft_trans_chain *)trans->data)->name)
1408 #define nft_trans_chain_stats(trans)    \
1409         (((struct nft_trans_chain *)trans->data)->stats)
1410 #define nft_trans_chain_policy(trans)   \
1411         (((struct nft_trans_chain *)trans->data)->policy)
1412
1413 struct nft_trans_table {
1414         bool                            update;
1415         bool                            enable;
1416 };
1417
1418 #define nft_trans_table_update(trans)   \
1419         (((struct nft_trans_table *)trans->data)->update)
1420 #define nft_trans_table_enable(trans)   \
1421         (((struct nft_trans_table *)trans->data)->enable)
1422
1423 struct nft_trans_elem {
1424         struct nft_set                  *set;
1425         struct nft_set_elem             elem;
1426         bool                            bound;
1427 };
1428
1429 #define nft_trans_elem_set(trans)       \
1430         (((struct nft_trans_elem *)trans->data)->set)
1431 #define nft_trans_elem(trans)   \
1432         (((struct nft_trans_elem *)trans->data)->elem)
1433 #define nft_trans_elem_set_bound(trans) \
1434         (((struct nft_trans_elem *)trans->data)->bound)
1435
1436 struct nft_trans_obj {
1437         struct nft_object               *obj;
1438         struct nft_object               *newobj;
1439         bool                            update;
1440 };
1441
1442 #define nft_trans_obj(trans)    \
1443         (((struct nft_trans_obj *)trans->data)->obj)
1444 #define nft_trans_obj_newobj(trans) \
1445         (((struct nft_trans_obj *)trans->data)->newobj)
1446 #define nft_trans_obj_update(trans)     \
1447         (((struct nft_trans_obj *)trans->data)->update)
1448
1449 struct nft_trans_flowtable {
1450         struct nft_flowtable            *flowtable;
1451 };
1452
1453 #define nft_trans_flowtable(trans)      \
1454         (((struct nft_trans_flowtable *)trans->data)->flowtable)
1455
1456 int __init nft_chain_filter_init(void);
1457 void nft_chain_filter_fini(void);
1458
1459 void __init nft_chain_route_init(void);
1460 void nft_chain_route_fini(void);
1461 #endif /* _NET_NF_TABLES_H */