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