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