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