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