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