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