netfilter: nf_tables: cleanup nf_tables.h
[cascardo/linux.git] / include / net / netfilter / nf_tables.h
1 #ifndef _NET_NF_TABLES_H
2 #define _NET_NF_TABLES_H
3
4 #include <linux/list.h>
5 #include <linux/netfilter.h>
6 #include <linux/netfilter/nfnetlink.h>
7 #include <linux/netfilter/x_tables.h>
8 #include <linux/netfilter/nf_tables.h>
9 #include <linux/u64_stats_sync.h>
10 #include <net/netlink.h>
11
12 #define NFT_JUMP_STACK_SIZE     16
13
14 struct nft_pktinfo {
15         struct sk_buff                  *skb;
16         const struct net_device         *in;
17         const struct net_device         *out;
18         const struct nf_hook_ops        *ops;
19         u8                              nhoff;
20         u8                              thoff;
21         u8                              tprot;
22         /* for x_tables compatibility */
23         struct xt_action_param          xt;
24 };
25
26 static inline void nft_set_pktinfo(struct nft_pktinfo *pkt,
27                                    const struct nf_hook_ops *ops,
28                                    struct sk_buff *skb,
29                                    const struct net_device *in,
30                                    const struct net_device *out)
31 {
32         pkt->skb = skb;
33         pkt->in = pkt->xt.in = in;
34         pkt->out = pkt->xt.out = out;
35         pkt->ops = ops;
36         pkt->xt.hooknum = ops->hooknum;
37         pkt->xt.family = ops->pf;
38 }
39
40 struct nft_data {
41         union {
42                 u32                             data[4];
43                 struct {
44                         u32                     verdict;
45                         struct nft_chain        *chain;
46                 };
47         };
48 } __attribute__((aligned(__alignof__(u64))));
49
50 static inline int nft_data_cmp(const struct nft_data *d1,
51                                const struct nft_data *d2,
52                                unsigned int len)
53 {
54         return memcmp(d1->data, d2->data, len);
55 }
56
57 static inline void nft_data_copy(struct nft_data *dst,
58                                  const struct nft_data *src)
59 {
60         BUILD_BUG_ON(__alignof__(*dst) != __alignof__(u64));
61         *(u64 *)&dst->data[0] = *(u64 *)&src->data[0];
62         *(u64 *)&dst->data[2] = *(u64 *)&src->data[2];
63 }
64
65 static inline void nft_data_debug(const struct nft_data *data)
66 {
67         pr_debug("data[0]=%x data[1]=%x data[2]=%x data[3]=%x\n",
68                  data->data[0], data->data[1],
69                  data->data[2], data->data[3]);
70 }
71
72 /**
73  *      struct nft_ctx - nf_tables rule/set context
74  *
75  *      @net: net namespace
76  *      @afi: address family info
77  *      @table: the table the chain is contained in
78  *      @chain: the chain the rule is contained in
79  *      @nla: netlink attributes
80  *      @portid: netlink portID of the original message
81  *      @seq: netlink sequence number
82  *      @report: notify via unicast netlink message
83  */
84 struct nft_ctx {
85         struct net                      *net;
86         struct nft_af_info              *afi;
87         struct nft_table                *table;
88         struct nft_chain                *chain;
89         const struct nlattr * const     *nla;
90         u32                             portid;
91         u32                             seq;
92         bool                            report;
93 };
94
95 struct nft_data_desc {
96         enum nft_data_types             type;
97         unsigned int                    len;
98 };
99
100 int nft_data_init(const struct nft_ctx *ctx, struct nft_data *data,
101                   struct nft_data_desc *desc, const struct nlattr *nla);
102 void nft_data_uninit(const struct nft_data *data, enum nft_data_types type);
103 int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
104                   enum nft_data_types type, unsigned int len);
105
106 static inline enum nft_data_types nft_dreg_to_type(enum nft_registers reg)
107 {
108         return reg == NFT_REG_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE;
109 }
110
111 static inline enum nft_registers nft_type_to_reg(enum nft_data_types type)
112 {
113         return type == NFT_DATA_VERDICT ? NFT_REG_VERDICT : NFT_REG_1;
114 }
115
116 int nft_validate_input_register(enum nft_registers reg);
117 int nft_validate_output_register(enum nft_registers reg);
118 int nft_validate_data_load(const struct nft_ctx *ctx, enum nft_registers reg,
119                            const struct nft_data *data,
120                            enum nft_data_types type);
121
122 /**
123  *      struct nft_set_elem - generic representation of set elements
124  *
125  *      @cookie: implementation specific element cookie
126  *      @key: element key
127  *      @data: element data (maps only)
128  *      @flags: element flags (end of interval)
129  *
130  *      The cookie can be used to store a handle to the element for subsequent
131  *      removal.
132  */
133 struct nft_set_elem {
134         void                    *cookie;
135         struct nft_data         key;
136         struct nft_data         data;
137         u32                     flags;
138 };
139
140 struct nft_set;
141 struct nft_set_iter {
142         unsigned int    count;
143         unsigned int    skip;
144         int             err;
145         int             (*fn)(const struct nft_ctx *ctx,
146                               const struct nft_set *set,
147                               const struct nft_set_iter *iter,
148                               const struct nft_set_elem *elem);
149 };
150
151 /**
152  *      struct nft_set_desc - description of set elements
153  *
154  *      @klen: key length
155  *      @dlen: data length
156  *      @size: number of set elements
157  */
158 struct nft_set_desc {
159         unsigned int            klen;
160         unsigned int            dlen;
161         unsigned int            size;
162 };
163
164 /**
165  *      enum nft_set_class - performance class
166  *
167  *      @NFT_LOOKUP_O_1: constant, O(1)
168  *      @NFT_LOOKUP_O_LOG_N: logarithmic, O(log N)
169  *      @NFT_LOOKUP_O_N: linear, O(N)
170  */
171 enum nft_set_class {
172         NFT_SET_CLASS_O_1,
173         NFT_SET_CLASS_O_LOG_N,
174         NFT_SET_CLASS_O_N,
175 };
176
177 /**
178  *      struct nft_set_estimate - estimation of memory and performance
179  *                                characteristics
180  *
181  *      @size: required memory
182  *      @class: lookup performance class
183  */
184 struct nft_set_estimate {
185         unsigned int            size;
186         enum nft_set_class      class;
187 };
188
189 /**
190  *      struct nft_set_ops - nf_tables set operations
191  *
192  *      @lookup: look up an element within the set
193  *      @insert: insert new element into set
194  *      @remove: remove element from set
195  *      @walk: iterate over all set elemeennts
196  *      @privsize: function to return size of set private data
197  *      @init: initialize private data of new set instance
198  *      @destroy: destroy private data of set instance
199  *      @list: nf_tables_set_ops list node
200  *      @owner: module reference
201  *      @features: features supported by the implementation
202  */
203 struct nft_set_ops {
204         bool                            (*lookup)(const struct nft_set *set,
205                                                   const struct nft_data *key,
206                                                   struct nft_data *data);
207         int                             (*get)(const struct nft_set *set,
208                                                struct nft_set_elem *elem);
209         int                             (*insert)(const struct nft_set *set,
210                                                   const struct nft_set_elem *elem);
211         void                            (*remove)(const struct nft_set *set,
212                                                   const struct nft_set_elem *elem);
213         void                            (*walk)(const struct nft_ctx *ctx,
214                                                 const struct nft_set *set,
215                                                 struct nft_set_iter *iter);
216
217         unsigned int                    (*privsize)(const struct nlattr * const nla[]);
218         bool                            (*estimate)(const struct nft_set_desc *desc,
219                                                     u32 features,
220                                                     struct nft_set_estimate *est);
221         int                             (*init)(const struct nft_set *set,
222                                                 const struct nft_set_desc *desc,
223                                                 const struct nlattr * const nla[]);
224         void                            (*destroy)(const struct nft_set *set);
225
226         struct list_head                list;
227         struct module                   *owner;
228         u32                             features;
229 };
230
231 int nft_register_set(struct nft_set_ops *ops);
232 void nft_unregister_set(struct nft_set_ops *ops);
233
234 /**
235  *      struct nft_set - nf_tables set instance
236  *
237  *      @list: table set list node
238  *      @bindings: list of set bindings
239  *      @name: name of the set
240  *      @ktype: key type (numeric type defined by userspace, not used in the kernel)
241  *      @dtype: data type (verdict or numeric type defined by userspace)
242  *      @size: maximum set size
243  *      @nelems: number of elements
244  *      @policy: set parameterization (see enum nft_set_policies)
245  *      @ops: set ops
246  *      @flags: set flags
247  *      @klen: key length
248  *      @dlen: data length
249  *      @data: private set data
250  */
251 struct nft_set {
252         struct list_head                list;
253         struct list_head                bindings;
254         char                            name[IFNAMSIZ];
255         u32                             ktype;
256         u32                             dtype;
257         u32                             size;
258         u32                             nelems;
259         u16                             policy;
260         /* runtime data below here */
261         const struct nft_set_ops        *ops ____cacheline_aligned;
262         u16                             flags;
263         u8                              klen;
264         u8                              dlen;
265         unsigned char                   data[]
266                 __attribute__((aligned(__alignof__(u64))));
267 };
268
269 static inline void *nft_set_priv(const struct nft_set *set)
270 {
271         return (void *)set->data;
272 }
273
274 struct nft_set *nf_tables_set_lookup(const struct nft_table *table,
275                                      const struct nlattr *nla);
276 struct nft_set *nf_tables_set_lookup_byid(const struct net *net,
277                                           const struct nlattr *nla);
278
279 /**
280  *      struct nft_set_binding - nf_tables set binding
281  *
282  *      @list: set bindings list node
283  *      @chain: chain containing the rule bound to the set
284  *
285  *      A set binding contains all information necessary for validation
286  *      of new elements added to a bound set.
287  */
288 struct nft_set_binding {
289         struct list_head                list;
290         const struct nft_chain          *chain;
291 };
292
293 int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
294                        struct nft_set_binding *binding);
295 void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
296                           struct nft_set_binding *binding);
297
298
299 /**
300  *      struct nft_expr_type - nf_tables expression type
301  *
302  *      @select_ops: function to select nft_expr_ops
303  *      @ops: default ops, used when no select_ops functions is present
304  *      @list: used internally
305  *      @name: Identifier
306  *      @owner: module reference
307  *      @policy: netlink attribute policy
308  *      @maxattr: highest netlink attribute number
309  *      @family: address family for AF-specific types
310  */
311 struct nft_expr_type {
312         const struct nft_expr_ops       *(*select_ops)(const struct nft_ctx *,
313                                                        const struct nlattr * const tb[]);
314         const struct nft_expr_ops       *ops;
315         struct list_head                list;
316         const char                      *name;
317         struct module                   *owner;
318         const struct nla_policy         *policy;
319         unsigned int                    maxattr;
320         u8                              family;
321 };
322
323 /**
324  *      struct nft_expr_ops - nf_tables expression operations
325  *
326  *      @eval: Expression evaluation function
327  *      @size: full expression size, including private data size
328  *      @init: initialization function
329  *      @destroy: destruction function
330  *      @dump: function to dump parameters
331  *      @type: expression type
332  *      @validate: validate expression, called during loop detection
333  *      @data: extra data to attach to this expression operation
334  */
335 struct nft_expr;
336 struct nft_expr_ops {
337         void                            (*eval)(const struct nft_expr *expr,
338                                                 struct nft_data data[NFT_REG_MAX + 1],
339                                                 const struct nft_pktinfo *pkt);
340         unsigned int                    size;
341
342         int                             (*init)(const struct nft_ctx *ctx,
343                                                 const struct nft_expr *expr,
344                                                 const struct nlattr * const tb[]);
345         void                            (*destroy)(const struct nft_ctx *ctx,
346                                                    const struct nft_expr *expr);
347         int                             (*dump)(struct sk_buff *skb,
348                                                 const struct nft_expr *expr);
349         int                             (*validate)(const struct nft_ctx *ctx,
350                                                     const struct nft_expr *expr,
351                                                     const struct nft_data **data);
352         const struct nft_expr_type      *type;
353         void                            *data;
354 };
355
356 #define NFT_EXPR_MAXATTR                16
357 #define NFT_EXPR_SIZE(size)             (sizeof(struct nft_expr) + \
358                                          ALIGN(size, __alignof__(struct nft_expr)))
359
360 /**
361  *      struct nft_expr - nf_tables expression
362  *
363  *      @ops: expression ops
364  *      @data: expression private data
365  */
366 struct nft_expr {
367         const struct nft_expr_ops       *ops;
368         unsigned char                   data[];
369 };
370
371 static inline void *nft_expr_priv(const struct nft_expr *expr)
372 {
373         return (void *)expr->data;
374 }
375
376 /**
377  *      struct nft_rule - nf_tables rule
378  *
379  *      @list: used internally
380  *      @handle: rule handle
381  *      @genmask: generation mask
382  *      @dlen: length of expression data
383  *      @ulen: length of user data (used for comments)
384  *      @data: expression data
385  */
386 struct nft_rule {
387         struct list_head                list;
388         u64                             handle:42,
389                                         genmask:2,
390                                         dlen:12,
391                                         ulen:8;
392         unsigned char                   data[]
393                 __attribute__((aligned(__alignof__(struct nft_expr))));
394 };
395
396 static inline struct nft_expr *nft_expr_first(const struct nft_rule *rule)
397 {
398         return (struct nft_expr *)&rule->data[0];
399 }
400
401 static inline struct nft_expr *nft_expr_next(const struct nft_expr *expr)
402 {
403         return ((void *)expr) + expr->ops->size;
404 }
405
406 static inline struct nft_expr *nft_expr_last(const struct nft_rule *rule)
407 {
408         return (struct nft_expr *)&rule->data[rule->dlen];
409 }
410
411 static inline void *nft_userdata(const struct nft_rule *rule)
412 {
413         return (void *)&rule->data[rule->dlen];
414 }
415
416 /*
417  * The last pointer isn't really necessary, but the compiler isn't able to
418  * determine that the result of nft_expr_last() is always the same since it
419  * can't assume that the dlen value wasn't changed within calls in the loop.
420  */
421 #define nft_rule_for_each_expr(expr, last, rule) \
422         for ((expr) = nft_expr_first(rule), (last) = nft_expr_last(rule); \
423              (expr) != (last); \
424              (expr) = nft_expr_next(expr))
425
426 enum nft_chain_flags {
427         NFT_BASE_CHAIN                  = 0x1,
428         NFT_CHAIN_INACTIVE              = 0x2,
429 };
430
431 /**
432  *      struct nft_chain - nf_tables chain
433  *
434  *      @rules: list of rules in the chain
435  *      @list: used internally
436  *      @net: net namespace that this chain belongs to
437  *      @table: table that this chain belongs to
438  *      @handle: chain handle
439  *      @use: number of jump references to this chain
440  *      @level: length of longest path to this chain
441  *      @flags: bitmask of enum nft_chain_flags
442  *      @name: name of the chain
443  */
444 struct nft_chain {
445         struct list_head                rules;
446         struct list_head                list;
447         struct net                      *net;
448         struct nft_table                *table;
449         u64                             handle;
450         u32                             use;
451         u16                             level;
452         u8                              flags;
453         char                            name[NFT_CHAIN_MAXNAMELEN];
454 };
455
456 enum nft_chain_type {
457         NFT_CHAIN_T_DEFAULT = 0,
458         NFT_CHAIN_T_ROUTE,
459         NFT_CHAIN_T_NAT,
460         NFT_CHAIN_T_MAX
461 };
462
463 /**
464  *      struct nf_chain_type - nf_tables chain type info
465  *
466  *      @name: name of the type
467  *      @type: numeric identifier
468  *      @family: address family
469  *      @owner: module owner
470  *      @hook_mask: mask of valid hooks
471  *      @hooks: hookfn overrides
472  */
473 struct nf_chain_type {
474         const char                      *name;
475         enum nft_chain_type             type;
476         int                             family;
477         struct module                   *owner;
478         unsigned int                    hook_mask;
479         nf_hookfn                       *hooks[NF_MAX_HOOKS];
480 };
481
482 int nft_chain_validate_dependency(const struct nft_chain *chain,
483                                   enum nft_chain_type type);
484 int nft_chain_validate_hooks(const struct nft_chain *chain,
485                              unsigned int hook_flags);
486
487 struct nft_stats {
488         u64                     bytes;
489         u64                     pkts;
490         struct u64_stats_sync   syncp;
491 };
492
493 #define NFT_HOOK_OPS_MAX                2
494
495 /**
496  *      struct nft_base_chain - nf_tables base chain
497  *
498  *      @ops: netfilter hook ops
499  *      @type: chain type
500  *      @policy: default policy
501  *      @stats: per-cpu chain stats
502  *      @chain: the chain
503  */
504 struct nft_base_chain {
505         struct nf_hook_ops              ops[NFT_HOOK_OPS_MAX];
506         const struct nf_chain_type      *type;
507         u8                              policy;
508         struct nft_stats __percpu       *stats;
509         struct nft_chain                chain;
510 };
511
512 static inline struct nft_base_chain *nft_base_chain(const struct nft_chain *chain)
513 {
514         return container_of(chain, struct nft_base_chain, chain);
515 }
516
517 unsigned int nft_do_chain(struct nft_pktinfo *pkt,
518                           const struct nf_hook_ops *ops);
519
520 /**
521  *      struct nft_table - nf_tables table
522  *
523  *      @list: used internally
524  *      @chains: chains in the table
525  *      @sets: sets in the table
526  *      @hgenerator: handle generator state
527  *      @use: number of chain references to this table
528  *      @flags: table flag (see enum nft_table_flags)
529  *      @name: name of the table
530  */
531 struct nft_table {
532         struct list_head                list;
533         struct list_head                chains;
534         struct list_head                sets;
535         u64                             hgenerator;
536         u32                             use;
537         u16                             flags;
538         char                            name[];
539 };
540
541 /**
542  *      struct nft_af_info - nf_tables address family info
543  *
544  *      @list: used internally
545  *      @family: address family
546  *      @nhooks: number of hooks in this family
547  *      @owner: module owner
548  *      @tables: used internally
549  *      @nops: number of hook ops in this family
550  *      @hook_ops_init: initialization function for chain hook ops
551  *      @hooks: hookfn overrides for packet validation
552  */
553 struct nft_af_info {
554         struct list_head                list;
555         int                             family;
556         unsigned int                    nhooks;
557         struct module                   *owner;
558         struct list_head                tables;
559         unsigned int                    nops;
560         void                            (*hook_ops_init)(struct nf_hook_ops *,
561                                                          unsigned int);
562         nf_hookfn                       *hooks[NF_MAX_HOOKS];
563 };
564
565 int nft_register_afinfo(struct net *, struct nft_af_info *);
566 void nft_unregister_afinfo(struct nft_af_info *);
567
568 int nft_register_chain_type(const struct nf_chain_type *);
569 void nft_unregister_chain_type(const struct nf_chain_type *);
570
571 int nft_register_expr(struct nft_expr_type *);
572 void nft_unregister_expr(struct nft_expr_type *);
573
574 #define nft_dereference(p)                                      \
575         nfnl_dereference(p, NFNL_SUBSYS_NFTABLES)
576
577 #define MODULE_ALIAS_NFT_FAMILY(family) \
578         MODULE_ALIAS("nft-afinfo-" __stringify(family))
579
580 #define MODULE_ALIAS_NFT_CHAIN(family, name) \
581         MODULE_ALIAS("nft-chain-" __stringify(family) "-" name)
582
583 #define MODULE_ALIAS_NFT_AF_EXPR(family, name) \
584         MODULE_ALIAS("nft-expr-" __stringify(family) "-" name)
585
586 #define MODULE_ALIAS_NFT_EXPR(name) \
587         MODULE_ALIAS("nft-expr-" name)
588
589 #define MODULE_ALIAS_NFT_SET() \
590         MODULE_ALIAS("nft-set")
591
592 /**
593  *      struct nft_trans - nf_tables object update in transaction
594  *
595  *      @list: used internally
596  *      @msg_type: message type
597  *      @ctx: transaction context
598  *      @data: internal information related to the transaction
599  */
600 struct nft_trans {
601         struct list_head                list;
602         int                             msg_type;
603         struct nft_ctx                  ctx;
604         char                            data[0];
605 };
606
607 struct nft_trans_rule {
608         struct nft_rule                 *rule;
609 };
610
611 #define nft_trans_rule(trans)   \
612         (((struct nft_trans_rule *)trans->data)->rule)
613
614 struct nft_trans_set {
615         struct nft_set                  *set;
616         u32                             set_id;
617 };
618
619 #define nft_trans_set(trans)    \
620         (((struct nft_trans_set *)trans->data)->set)
621 #define nft_trans_set_id(trans) \
622         (((struct nft_trans_set *)trans->data)->set_id)
623
624 struct nft_trans_chain {
625         bool                            update;
626         char                            name[NFT_CHAIN_MAXNAMELEN];
627         struct nft_stats __percpu       *stats;
628         u8                              policy;
629 };
630
631 #define nft_trans_chain_update(trans)   \
632         (((struct nft_trans_chain *)trans->data)->update)
633 #define nft_trans_chain_name(trans)     \
634         (((struct nft_trans_chain *)trans->data)->name)
635 #define nft_trans_chain_stats(trans)    \
636         (((struct nft_trans_chain *)trans->data)->stats)
637 #define nft_trans_chain_policy(trans)   \
638         (((struct nft_trans_chain *)trans->data)->policy)
639
640 struct nft_trans_table {
641         bool                            update;
642         bool                            enable;
643 };
644
645 #define nft_trans_table_update(trans)   \
646         (((struct nft_trans_table *)trans->data)->update)
647 #define nft_trans_table_enable(trans)   \
648         (((struct nft_trans_table *)trans->data)->enable)
649
650 struct nft_trans_elem {
651         struct nft_set                  *set;
652         struct nft_set_elem             elem;
653 };
654
655 #define nft_trans_elem_set(trans)       \
656         (((struct nft_trans_elem *)trans->data)->set)
657 #define nft_trans_elem(trans)   \
658         (((struct nft_trans_elem *)trans->data)->elem)
659
660 #endif /* _NET_NF_TABLES_H */