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