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