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