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