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