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