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