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