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