netfilter: nf_tables: use RCU-safe list insertion when replacing rules
[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
96518518
PM
75 * @afi: address family info
76 * @table: the table the chain is contained in
77 * @chain: the chain the rule is contained in
0ca743a5 78 * @nla: netlink attributes
128ad332
PNA
79 * @portid: netlink portID of the original message
80 * @seq: netlink sequence number
81 * @report: notify via unicast netlink message
96518518
PM
82 */
83struct nft_ctx {
99633ab2 84 struct net *net;
7c95f6d8
PNA
85 struct nft_af_info *afi;
86 struct nft_table *table;
87 struct nft_chain *chain;
0ca743a5 88 const struct nlattr * const *nla;
128ad332
PNA
89 u32 portid;
90 u32 seq;
91 bool report;
96518518
PM
92};
93
96518518
PM
94struct nft_data_desc {
95 enum nft_data_types type;
96 unsigned int len;
97};
98
5eccdfaa
JP
99int nft_data_init(const struct nft_ctx *ctx, struct nft_data *data,
100 struct nft_data_desc *desc, const struct nlattr *nla);
101void nft_data_uninit(const struct nft_data *data, enum nft_data_types type);
102int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
103 enum nft_data_types type, unsigned int len);
96518518
PM
104
105static 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
20a69341
PM
110static 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
5eccdfaa
JP
115int nft_validate_input_register(enum nft_registers reg);
116int nft_validate_output_register(enum nft_registers reg);
117int 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);
96518518 120
20a69341
PM
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 */
132struct nft_set_elem {
133 void *cookie;
134 struct nft_data key;
135 struct nft_data data;
136 u32 flags;
137};
138
139struct nft_set;
140struct 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
c50b960c
PM
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 */
157struct 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 */
170enum 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 */
183struct nft_set_estimate {
184 unsigned int size;
185 enum nft_set_class class;
186};
187
20a69341
PM
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 */
202struct 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[]);
c50b960c
PM
217 bool (*estimate)(const struct nft_set_desc *desc,
218 u32 features,
219 struct nft_set_estimate *est);
20a69341 220 int (*init)(const struct nft_set *set,
c50b960c 221 const struct nft_set_desc *desc,
20a69341
PM
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
5eccdfaa
JP
230int nft_register_set(struct nft_set_ops *ops);
231void nft_unregister_set(struct nft_set_ops *ops);
20a69341
PM
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)
c50b960c
PM
241 * @size: maximum set size
242 * @nelems: number of elements
20a69341
PM
243 * @ops: set ops
244 * @flags: set flags
245 * @klen: key length
246 * @dlen: data length
247 * @data: private set data
248 */
249struct nft_set {
250 struct list_head list;
251 struct list_head bindings;
252 char name[IFNAMSIZ];
253 u32 ktype;
254 u32 dtype;
c50b960c
PM
255 u32 size;
256 u32 nelems;
20a69341
PM
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
266static inline void *nft_set_priv(const struct nft_set *set)
267{
268 return (void *)set->data;
269}
270
5eccdfaa
JP
271struct nft_set *nf_tables_set_lookup(const struct nft_table *table,
272 const struct nlattr *nla);
958bee14
PNA
273struct nft_set *nf_tables_set_lookup_byid(const struct net *net,
274 const struct nlattr *nla);
20a69341
PM
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 */
285struct nft_set_binding {
286 struct list_head list;
287 const struct nft_chain *chain;
288};
289
5eccdfaa
JP
290int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
291 struct nft_set_binding *binding);
292void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
293 struct nft_set_binding *binding);
20a69341 294
ef1f7df9 295
96518518 296/**
ef1f7df9 297 * struct nft_expr_type - nf_tables expression type
96518518 298 *
ef1f7df9
PM
299 * @select_ops: function to select nft_expr_ops
300 * @ops: default ops, used when no select_ops functions is present
96518518
PM
301 * @list: used internally
302 * @name: Identifier
303 * @owner: module reference
304 * @policy: netlink attribute policy
305 * @maxattr: highest netlink attribute number
64d46806 306 * @family: address family for AF-specific types
ef1f7df9
PM
307 */
308struct nft_expr_type {
0ca743a5
PNA
309 const struct nft_expr_ops *(*select_ops)(const struct nft_ctx *,
310 const struct nlattr * const tb[]);
ef1f7df9
PM
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;
64d46806 317 u8 family;
ef1f7df9
PM
318};
319
320/**
321 * struct nft_expr_ops - nf_tables expression operations
322 *
323 * @eval: Expression evaluation function
96518518 324 * @size: full expression size, including private data size
ef1f7df9
PM
325 * @init: initialization function
326 * @destroy: destruction function
327 * @dump: function to dump parameters
328 * @type: expression type
0ca743a5
PNA
329 * @validate: validate expression, called during loop detection
330 * @data: extra data to attach to this expression operation
96518518
PM
331 */
332struct nft_expr;
333struct 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);
ef1f7df9
PM
337 unsigned int size;
338
96518518
PM
339 int (*init)(const struct nft_ctx *ctx,
340 const struct nft_expr *expr,
341 const struct nlattr * const tb[]);
62472bce
PM
342 void (*destroy)(const struct nft_ctx *ctx,
343 const struct nft_expr *expr);
96518518
PM
344 int (*dump)(struct sk_buff *skb,
345 const struct nft_expr *expr);
0ca743a5
PNA
346 int (*validate)(const struct nft_ctx *ctx,
347 const struct nft_expr *expr,
348 const struct nft_data **data);
ef1f7df9 349 const struct nft_expr_type *type;
0ca743a5 350 void *data;
96518518
PM
351};
352
ef1f7df9 353#define NFT_EXPR_MAXATTR 16
96518518
PM
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 */
363struct nft_expr {
364 const struct nft_expr_ops *ops;
365 unsigned char data[];
366};
367
368static 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
96518518 377 * @handle: rule handle
0628b123 378 * @genmask: generation mask
96518518 379 * @dlen: length of expression data
0768b3b3 380 * @ulen: length of user data (used for comments)
96518518
PM
381 * @data: expression data
382 */
383struct nft_rule {
384 struct list_head list;
0768b3b3 385 u64 handle:42,
0628b123 386 genmask:2,
0768b3b3
PNA
387 dlen:12,
388 ulen:8;
96518518
PM
389 unsigned char data[]
390 __attribute__((aligned(__alignof__(struct nft_expr))));
391};
392
0628b123 393/**
1081d11b 394 * struct nft_trans - nf_tables object update in transaction
0628b123 395 *
c7c32e72 396 * @rcu_head: rcu head to defer release of transaction data
0628b123 397 * @list: used internally
b380e5c7 398 * @msg_type: message type
1081d11b
PNA
399 * @ctx: transaction context
400 * @data: internal information related to the transaction
0628b123 401 */
1081d11b 402struct nft_trans {
c7c32e72 403 struct rcu_head rcu_head;
0628b123 404 struct list_head list;
b380e5c7 405 int msg_type;
62472bce 406 struct nft_ctx ctx;
1081d11b
PNA
407 char data[0];
408};
409
410struct nft_trans_rule {
0628b123 411 struct nft_rule *rule;
0628b123
PNA
412};
413
1081d11b
PNA
414#define nft_trans_rule(trans) \
415 (((struct nft_trans_rule *)trans->data)->rule)
416
958bee14
PNA
417struct 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
91c7b38d
PNA
427struct 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
55dd6f93
PNA
443struct 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
60319eb1
PNA
453struct 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
96518518
PM
463static inline struct nft_expr *nft_expr_first(const struct nft_rule *rule)
464{
465 return (struct nft_expr *)&rule->data[0];
466}
467
468static inline struct nft_expr *nft_expr_next(const struct nft_expr *expr)
469{
470 return ((void *)expr) + expr->ops->size;
471}
472
473static inline struct nft_expr *nft_expr_last(const struct nft_rule *rule)
474{
475 return (struct nft_expr *)&rule->data[rule->dlen];
476}
477
0768b3b3
PNA
478static inline void *nft_userdata(const struct nft_rule *rule)
479{
480 return (void *)&rule->data[rule->dlen];
481}
482
96518518
PM
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
493enum nft_chain_flags {
494 NFT_BASE_CHAIN = 0x1,
91c7b38d 495 NFT_CHAIN_INACTIVE = 0x2,
96518518
PM
496};
497
498/**
499 * struct nft_chain - nf_tables chain
500 *
501 * @rules: list of rules in the chain
502 * @list: used internally
0628b123 503 * @net: net namespace that this chain belongs to
b5bc89bf 504 * @table: table that this chain belongs to
96518518
PM
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 */
511struct nft_chain {
512 struct list_head rules;
513 struct list_head list;
0628b123 514 struct net *net;
b5bc89bf 515 struct nft_table *table;
96518518
PM
516 u64 handle;
517 u8 flags;
518 u16 use;
519 u16 level;
520 char name[NFT_CHAIN_MAXNAMELEN];
521};
522
9370761c
PNA
523enum 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
0ca743a5
PNA
530struct nft_stats {
531 u64 bytes;
532 u64 pkts;
533};
534
115a60b1
PM
535#define NFT_HOOK_OPS_MAX 2
536
96518518
PM
537/**
538 * struct nft_base_chain - nf_tables base chain
539 *
540 * @ops: netfilter hook ops
9370761c 541 * @type: chain type
0ca743a5
PNA
542 * @policy: default policy
543 * @stats: per-cpu chain stats
96518518
PM
544 * @chain: the chain
545 */
546struct nft_base_chain {
115a60b1 547 struct nf_hook_ops ops[NFT_HOOK_OPS_MAX];
2a37d755 548 const struct nf_chain_type *type;
0ca743a5
PNA
549 u8 policy;
550 struct nft_stats __percpu *stats;
96518518
PM
551 struct nft_chain chain;
552};
553
554static 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
3876d22d
PM
559unsigned int nft_do_chain(struct nft_pktinfo *pkt,
560 const struct nf_hook_ops *ops);
96518518 561
96518518
PM
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 */
573struct 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
115a60b1
PM
591 * @nops: number of hook ops in this family
592 * @hook_ops_init: initialization function for chain hook ops
96518518
PM
593 * @hooks: hookfn overrides for packet validation
594 */
595struct nft_af_info {
596 struct list_head list;
597 int family;
598 unsigned int nhooks;
599 struct module *owner;
600 struct list_head tables;
115a60b1
PM
601 unsigned int nops;
602 void (*hook_ops_init)(struct nf_hook_ops *,
603 unsigned int);
96518518
PM
604 nf_hookfn *hooks[NF_MAX_HOOKS];
605};
606
5eccdfaa
JP
607int nft_register_afinfo(struct net *, struct nft_af_info *);
608void nft_unregister_afinfo(struct nft_af_info *);
96518518 609
fa2c1de0
PM
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 */
9370761c 620struct nf_chain_type {
fa2c1de0
PM
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];
9370761c
PNA
627};
628
2a37d755
PM
629int nft_register_chain_type(const struct nf_chain_type *);
630void nft_unregister_chain_type(const struct nf_chain_type *);
96518518 631
5eccdfaa
JP
632int nft_register_expr(struct nft_expr_type *);
633void nft_unregister_expr(struct nft_expr_type *);
96518518 634
67a8fc27
PM
635#define nft_dereference(p) \
636 nfnl_dereference(p, NFNL_SUBSYS_NFTABLES)
637
96518518
PM
638#define MODULE_ALIAS_NFT_FAMILY(family) \
639 MODULE_ALIAS("nft-afinfo-" __stringify(family))
640
9370761c
PNA
641#define MODULE_ALIAS_NFT_CHAIN(family, name) \
642 MODULE_ALIAS("nft-chain-" __stringify(family) "-" name)
96518518 643
64d46806
PM
644#define MODULE_ALIAS_NFT_AF_EXPR(family, name) \
645 MODULE_ALIAS("nft-expr-" __stringify(family) "-" name)
646
96518518
PM
647#define MODULE_ALIAS_NFT_EXPR(name) \
648 MODULE_ALIAS("nft-expr-" name)
649
20a69341
PM
650#define MODULE_ALIAS_NFT_SET() \
651 MODULE_ALIAS("nft-set")
652
96518518 653#endif /* _NET_NF_TABLES_H */
This page took 0.086017 seconds and 5 git commands to generate.