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