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