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