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