Merge branch 'ptp-ns_to_timespec64'
[linux-block.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 *
20a69341 141 * @key: element key
fe2811eb 142 * @priv: element private data and extensions
20a69341
PM
143 */
144struct nft_set_elem {
20a69341 145 struct nft_data key;
fe2811eb 146 void *priv;
20a69341
PM
147};
148
149struct nft_set;
150struct nft_set_iter {
151 unsigned int count;
152 unsigned int skip;
153 int err;
154 int (*fn)(const struct nft_ctx *ctx,
155 const struct nft_set *set,
156 const struct nft_set_iter *iter,
157 const struct nft_set_elem *elem);
158};
159
c50b960c
PM
160/**
161 * struct nft_set_desc - description of set elements
162 *
163 * @klen: key length
164 * @dlen: data length
165 * @size: number of set elements
166 */
167struct nft_set_desc {
168 unsigned int klen;
169 unsigned int dlen;
170 unsigned int size;
171};
172
173/**
174 * enum nft_set_class - performance class
175 *
176 * @NFT_LOOKUP_O_1: constant, O(1)
177 * @NFT_LOOKUP_O_LOG_N: logarithmic, O(log N)
178 * @NFT_LOOKUP_O_N: linear, O(N)
179 */
180enum nft_set_class {
181 NFT_SET_CLASS_O_1,
182 NFT_SET_CLASS_O_LOG_N,
183 NFT_SET_CLASS_O_N,
184};
185
186/**
187 * struct nft_set_estimate - estimation of memory and performance
188 * characteristics
189 *
190 * @size: required memory
191 * @class: lookup performance class
192 */
193struct nft_set_estimate {
194 unsigned int size;
195 enum nft_set_class class;
196};
197
b2832dd6
PM
198struct nft_set_ext;
199
20a69341
PM
200/**
201 * struct nft_set_ops - nf_tables set operations
202 *
203 * @lookup: look up an element within the set
204 * @insert: insert new element into set
cc02e457
PM
205 * @activate: activate new element in the next generation
206 * @deactivate: deactivate element in the next generation
20a69341
PM
207 * @remove: remove element from set
208 * @walk: iterate over all set elemeennts
209 * @privsize: function to return size of set private data
210 * @init: initialize private data of new set instance
211 * @destroy: destroy private data of set instance
212 * @list: nf_tables_set_ops list node
213 * @owner: module reference
fe2811eb 214 * @elemsize: element private size
20a69341
PM
215 * @features: features supported by the implementation
216 */
217struct nft_set_ops {
218 bool (*lookup)(const struct nft_set *set,
219 const struct nft_data *key,
b2832dd6 220 const struct nft_set_ext **ext);
20a69341
PM
221 int (*insert)(const struct nft_set *set,
222 const struct nft_set_elem *elem);
cc02e457
PM
223 void (*activate)(const struct nft_set *set,
224 const struct nft_set_elem *elem);
225 void * (*deactivate)(const struct nft_set *set,
226 const struct nft_set_elem *elem);
20a69341
PM
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;
fe2811eb 244 unsigned int elemsize;
20a69341
PM
245 u32 features;
246};
247
5eccdfaa
JP
248int nft_register_set(struct nft_set_ops *ops);
249void nft_unregister_set(struct nft_set_ops *ops);
20a69341
PM
250
251/**
252 * struct nft_set - nf_tables set instance
253 *
254 * @list: table set list node
255 * @bindings: list of set bindings
256 * @name: name of the set
257 * @ktype: key type (numeric type defined by userspace, not used in the kernel)
258 * @dtype: data type (verdict or numeric type defined by userspace)
c50b960c
PM
259 * @size: maximum set size
260 * @nelems: number of elements
9363dc4b 261 * @policy: set parameterization (see enum nft_set_policies)
20a69341 262 * @ops: set ops
cc02e457 263 * @pnet: network namespace
20a69341
PM
264 * @flags: set flags
265 * @klen: key length
266 * @dlen: data length
267 * @data: private set data
268 */
269struct nft_set {
270 struct list_head list;
271 struct list_head bindings;
272 char name[IFNAMSIZ];
273 u32 ktype;
274 u32 dtype;
c50b960c
PM
275 u32 size;
276 u32 nelems;
9363dc4b 277 u16 policy;
20a69341
PM
278 /* runtime data below here */
279 const struct nft_set_ops *ops ____cacheline_aligned;
cc02e457 280 possible_net_t pnet;
20a69341
PM
281 u16 flags;
282 u8 klen;
283 u8 dlen;
284 unsigned char data[]
285 __attribute__((aligned(__alignof__(u64))));
286};
287
288static inline void *nft_set_priv(const struct nft_set *set)
289{
290 return (void *)set->data;
291}
292
5eccdfaa
JP
293struct nft_set *nf_tables_set_lookup(const struct nft_table *table,
294 const struct nlattr *nla);
958bee14
PNA
295struct nft_set *nf_tables_set_lookup_byid(const struct net *net,
296 const struct nlattr *nla);
20a69341
PM
297
298/**
299 * struct nft_set_binding - nf_tables set binding
300 *
301 * @list: set bindings list node
302 * @chain: chain containing the rule bound to the set
303 *
304 * A set binding contains all information necessary for validation
305 * of new elements added to a bound set.
306 */
307struct nft_set_binding {
308 struct list_head list;
309 const struct nft_chain *chain;
310};
311
5eccdfaa
JP
312int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
313 struct nft_set_binding *binding);
314void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
315 struct nft_set_binding *binding);
20a69341 316
3ac4c07a
PM
317/**
318 * enum nft_set_extensions - set extension type IDs
319 *
320 * @NFT_SET_EXT_KEY: element key
321 * @NFT_SET_EXT_DATA: mapping data
322 * @NFT_SET_EXT_FLAGS: element flags
323 * @NFT_SET_EXT_NUM: number of extension types
324 */
325enum nft_set_extensions {
326 NFT_SET_EXT_KEY,
327 NFT_SET_EXT_DATA,
328 NFT_SET_EXT_FLAGS,
329 NFT_SET_EXT_NUM
330};
331
332/**
333 * struct nft_set_ext_type - set extension type
334 *
335 * @len: fixed part length of the extension
336 * @align: alignment requirements of the extension
337 */
338struct nft_set_ext_type {
339 u8 len;
340 u8 align;
341};
342
343extern const struct nft_set_ext_type nft_set_ext_types[];
344
345/**
346 * struct nft_set_ext_tmpl - set extension template
347 *
348 * @len: length of extension area
349 * @offset: offsets of individual extension types
350 */
351struct nft_set_ext_tmpl {
352 u16 len;
353 u8 offset[NFT_SET_EXT_NUM];
354};
355
356/**
357 * struct nft_set_ext - set extensions
358 *
cc02e457 359 * @genmask: generation mask
3ac4c07a
PM
360 * @offset: offsets of individual extension types
361 * @data: beginning of extension data
362 */
363struct nft_set_ext {
cc02e457 364 u8 genmask;
3ac4c07a
PM
365 u8 offset[NFT_SET_EXT_NUM];
366 char data[0];
367};
368
369static inline void nft_set_ext_prepare(struct nft_set_ext_tmpl *tmpl)
370{
371 memset(tmpl, 0, sizeof(*tmpl));
372 tmpl->len = sizeof(struct nft_set_ext);
373}
374
375static inline void nft_set_ext_add_length(struct nft_set_ext_tmpl *tmpl, u8 id,
376 unsigned int len)
377{
378 tmpl->len = ALIGN(tmpl->len, nft_set_ext_types[id].align);
379 BUG_ON(tmpl->len > U8_MAX);
380 tmpl->offset[id] = tmpl->len;
381 tmpl->len += nft_set_ext_types[id].len + len;
382}
383
384static inline void nft_set_ext_add(struct nft_set_ext_tmpl *tmpl, u8 id)
385{
386 nft_set_ext_add_length(tmpl, id, 0);
387}
388
389static inline void nft_set_ext_init(struct nft_set_ext *ext,
390 const struct nft_set_ext_tmpl *tmpl)
391{
392 memcpy(ext->offset, tmpl->offset, sizeof(ext->offset));
393}
394
395static inline bool __nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
396{
397 return !!ext->offset[id];
398}
399
400static inline bool nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
401{
402 return ext && __nft_set_ext_exists(ext, id);
403}
404
405static inline void *nft_set_ext(const struct nft_set_ext *ext, u8 id)
406{
407 return (void *)ext + ext->offset[id];
408}
409
410static inline struct nft_data *nft_set_ext_key(const struct nft_set_ext *ext)
411{
412 return nft_set_ext(ext, NFT_SET_EXT_KEY);
413}
414
415static inline struct nft_data *nft_set_ext_data(const struct nft_set_ext *ext)
416{
417 return nft_set_ext(ext, NFT_SET_EXT_DATA);
418}
419
420static inline u8 *nft_set_ext_flags(const struct nft_set_ext *ext)
421{
422 return nft_set_ext(ext, NFT_SET_EXT_FLAGS);
423}
ef1f7df9 424
fe2811eb
PM
425static inline struct nft_set_ext *nft_set_elem_ext(const struct nft_set *set,
426 void *elem)
427{
428 return elem + set->ops->elemsize;
429}
430
61edafbb
PM
431void nft_set_elem_destroy(const struct nft_set *set, void *elem);
432
96518518 433/**
ef1f7df9 434 * struct nft_expr_type - nf_tables expression type
96518518 435 *
ef1f7df9
PM
436 * @select_ops: function to select nft_expr_ops
437 * @ops: default ops, used when no select_ops functions is present
96518518
PM
438 * @list: used internally
439 * @name: Identifier
440 * @owner: module reference
441 * @policy: netlink attribute policy
442 * @maxattr: highest netlink attribute number
64d46806 443 * @family: address family for AF-specific types
ef1f7df9
PM
444 */
445struct nft_expr_type {
0ca743a5
PNA
446 const struct nft_expr_ops *(*select_ops)(const struct nft_ctx *,
447 const struct nlattr * const tb[]);
ef1f7df9
PM
448 const struct nft_expr_ops *ops;
449 struct list_head list;
450 const char *name;
451 struct module *owner;
452 const struct nla_policy *policy;
453 unsigned int maxattr;
64d46806 454 u8 family;
ef1f7df9
PM
455};
456
457/**
458 * struct nft_expr_ops - nf_tables expression operations
459 *
460 * @eval: Expression evaluation function
96518518 461 * @size: full expression size, including private data size
ef1f7df9
PM
462 * @init: initialization function
463 * @destroy: destruction function
464 * @dump: function to dump parameters
465 * @type: expression type
0ca743a5
PNA
466 * @validate: validate expression, called during loop detection
467 * @data: extra data to attach to this expression operation
96518518
PM
468 */
469struct nft_expr;
470struct nft_expr_ops {
471 void (*eval)(const struct nft_expr *expr,
472 struct nft_data data[NFT_REG_MAX + 1],
473 const struct nft_pktinfo *pkt);
ef1f7df9
PM
474 unsigned int size;
475
96518518
PM
476 int (*init)(const struct nft_ctx *ctx,
477 const struct nft_expr *expr,
478 const struct nlattr * const tb[]);
62472bce
PM
479 void (*destroy)(const struct nft_ctx *ctx,
480 const struct nft_expr *expr);
96518518
PM
481 int (*dump)(struct sk_buff *skb,
482 const struct nft_expr *expr);
0ca743a5
PNA
483 int (*validate)(const struct nft_ctx *ctx,
484 const struct nft_expr *expr,
485 const struct nft_data **data);
ef1f7df9 486 const struct nft_expr_type *type;
0ca743a5 487 void *data;
96518518
PM
488};
489
ef1f7df9 490#define NFT_EXPR_MAXATTR 16
96518518
PM
491#define NFT_EXPR_SIZE(size) (sizeof(struct nft_expr) + \
492 ALIGN(size, __alignof__(struct nft_expr)))
493
494/**
495 * struct nft_expr - nf_tables expression
496 *
497 * @ops: expression ops
498 * @data: expression private data
499 */
500struct nft_expr {
501 const struct nft_expr_ops *ops;
502 unsigned char data[];
503};
504
505static inline void *nft_expr_priv(const struct nft_expr *expr)
506{
507 return (void *)expr->data;
508}
509
510/**
511 * struct nft_rule - nf_tables rule
512 *
513 * @list: used internally
96518518 514 * @handle: rule handle
0628b123 515 * @genmask: generation mask
96518518 516 * @dlen: length of expression data
86f1ec32 517 * @udata: user data is appended to the rule
96518518
PM
518 * @data: expression data
519 */
520struct nft_rule {
521 struct list_head list;
0768b3b3 522 u64 handle:42,
0628b123 523 genmask:2,
0768b3b3 524 dlen:12,
86f1ec32 525 udata:1;
96518518
PM
526 unsigned char data[]
527 __attribute__((aligned(__alignof__(struct nft_expr))));
528};
529
530static inline struct nft_expr *nft_expr_first(const struct nft_rule *rule)
531{
532 return (struct nft_expr *)&rule->data[0];
533}
534
535static inline struct nft_expr *nft_expr_next(const struct nft_expr *expr)
536{
537 return ((void *)expr) + expr->ops->size;
538}
539
540static inline struct nft_expr *nft_expr_last(const struct nft_rule *rule)
541{
542 return (struct nft_expr *)&rule->data[rule->dlen];
543}
544
86f1ec32 545static inline struct nft_userdata *nft_userdata(const struct nft_rule *rule)
0768b3b3
PNA
546{
547 return (void *)&rule->data[rule->dlen];
548}
549
96518518
PM
550/*
551 * The last pointer isn't really necessary, but the compiler isn't able to
552 * determine that the result of nft_expr_last() is always the same since it
553 * can't assume that the dlen value wasn't changed within calls in the loop.
554 */
555#define nft_rule_for_each_expr(expr, last, rule) \
556 for ((expr) = nft_expr_first(rule), (last) = nft_expr_last(rule); \
557 (expr) != (last); \
558 (expr) = nft_expr_next(expr))
559
560enum nft_chain_flags {
561 NFT_BASE_CHAIN = 0x1,
91c7b38d 562 NFT_CHAIN_INACTIVE = 0x2,
96518518
PM
563};
564
565/**
566 * struct nft_chain - nf_tables chain
567 *
568 * @rules: list of rules in the chain
569 * @list: used internally
b5bc89bf 570 * @table: table that this chain belongs to
96518518 571 * @handle: chain handle
96518518
PM
572 * @use: number of jump references to this chain
573 * @level: length of longest path to this chain
a0a7379e 574 * @flags: bitmask of enum nft_chain_flags
96518518
PM
575 * @name: name of the chain
576 */
577struct nft_chain {
578 struct list_head rules;
579 struct list_head list;
b5bc89bf 580 struct nft_table *table;
96518518 581 u64 handle;
a0a7379e 582 u32 use;
96518518 583 u16 level;
a0a7379e 584 u8 flags;
96518518
PM
585 char name[NFT_CHAIN_MAXNAMELEN];
586};
587
9370761c
PNA
588enum nft_chain_type {
589 NFT_CHAIN_T_DEFAULT = 0,
590 NFT_CHAIN_T_ROUTE,
591 NFT_CHAIN_T_NAT,
592 NFT_CHAIN_T_MAX
593};
594
1a1e1a12
PM
595/**
596 * struct nf_chain_type - nf_tables chain type info
597 *
598 * @name: name of the type
599 * @type: numeric identifier
600 * @family: address family
601 * @owner: module owner
602 * @hook_mask: mask of valid hooks
603 * @hooks: hookfn overrides
604 */
605struct nf_chain_type {
606 const char *name;
607 enum nft_chain_type type;
608 int family;
609 struct module *owner;
610 unsigned int hook_mask;
611 nf_hookfn *hooks[NF_MAX_HOOKS];
612};
613
7210e4e3
PNA
614int nft_chain_validate_dependency(const struct nft_chain *chain,
615 enum nft_chain_type type);
75e8d06d
PNA
616int nft_chain_validate_hooks(const struct nft_chain *chain,
617 unsigned int hook_flags);
7210e4e3 618
0ca743a5 619struct nft_stats {
ce355e20
ED
620 u64 bytes;
621 u64 pkts;
622 struct u64_stats_sync syncp;
0ca743a5
PNA
623};
624
115a60b1
PM
625#define NFT_HOOK_OPS_MAX 2
626
96518518
PM
627/**
628 * struct nft_base_chain - nf_tables base chain
629 *
630 * @ops: netfilter hook ops
5ebb335d 631 * @pnet: net namespace that this chain belongs to
9370761c 632 * @type: chain type
0ca743a5
PNA
633 * @policy: default policy
634 * @stats: per-cpu chain stats
96518518
PM
635 * @chain: the chain
636 */
637struct nft_base_chain {
115a60b1 638 struct nf_hook_ops ops[NFT_HOOK_OPS_MAX];
5ebb335d 639 possible_net_t pnet;
2a37d755 640 const struct nf_chain_type *type;
0ca743a5
PNA
641 u8 policy;
642 struct nft_stats __percpu *stats;
96518518
PM
643 struct nft_chain chain;
644};
645
646static inline struct nft_base_chain *nft_base_chain(const struct nft_chain *chain)
647{
648 return container_of(chain, struct nft_base_chain, chain);
649}
650
3876d22d
PM
651unsigned int nft_do_chain(struct nft_pktinfo *pkt,
652 const struct nf_hook_ops *ops);
96518518 653
96518518
PM
654/**
655 * struct nft_table - nf_tables table
656 *
657 * @list: used internally
658 * @chains: chains in the table
659 * @sets: sets in the table
660 * @hgenerator: handle generator state
661 * @use: number of chain references to this table
662 * @flags: table flag (see enum nft_table_flags)
663 * @name: name of the table
664 */
665struct nft_table {
666 struct list_head list;
667 struct list_head chains;
668 struct list_head sets;
669 u64 hgenerator;
670 u32 use;
671 u16 flags;
1cae565e 672 char name[NFT_TABLE_MAXNAMELEN];
96518518
PM
673};
674
675/**
676 * struct nft_af_info - nf_tables address family info
677 *
678 * @list: used internally
679 * @family: address family
680 * @nhooks: number of hooks in this family
681 * @owner: module owner
682 * @tables: used internally
115a60b1
PM
683 * @nops: number of hook ops in this family
684 * @hook_ops_init: initialization function for chain hook ops
96518518
PM
685 * @hooks: hookfn overrides for packet validation
686 */
687struct nft_af_info {
688 struct list_head list;
689 int family;
690 unsigned int nhooks;
691 struct module *owner;
692 struct list_head tables;
115a60b1
PM
693 unsigned int nops;
694 void (*hook_ops_init)(struct nf_hook_ops *,
695 unsigned int);
96518518
PM
696 nf_hookfn *hooks[NF_MAX_HOOKS];
697};
698
5eccdfaa
JP
699int nft_register_afinfo(struct net *, struct nft_af_info *);
700void nft_unregister_afinfo(struct nft_af_info *);
96518518 701
2a37d755
PM
702int nft_register_chain_type(const struct nf_chain_type *);
703void nft_unregister_chain_type(const struct nf_chain_type *);
96518518 704
5eccdfaa
JP
705int nft_register_expr(struct nft_expr_type *);
706void nft_unregister_expr(struct nft_expr_type *);
96518518 707
67a8fc27
PM
708#define nft_dereference(p) \
709 nfnl_dereference(p, NFNL_SUBSYS_NFTABLES)
710
96518518
PM
711#define MODULE_ALIAS_NFT_FAMILY(family) \
712 MODULE_ALIAS("nft-afinfo-" __stringify(family))
713
9370761c
PNA
714#define MODULE_ALIAS_NFT_CHAIN(family, name) \
715 MODULE_ALIAS("nft-chain-" __stringify(family) "-" name)
96518518 716
64d46806
PM
717#define MODULE_ALIAS_NFT_AF_EXPR(family, name) \
718 MODULE_ALIAS("nft-expr-" __stringify(family) "-" name)
719
96518518
PM
720#define MODULE_ALIAS_NFT_EXPR(name) \
721 MODULE_ALIAS("nft-expr-" name)
722
20a69341
PM
723#define MODULE_ALIAS_NFT_SET() \
724 MODULE_ALIAS("nft-set")
725
ea4bd995
PM
726/*
727 * The gencursor defines two generations, the currently active and the
728 * next one. Objects contain a bitmask of 2 bits specifying the generations
729 * they're active in. A set bit means they're inactive in the generation
730 * represented by that bit.
731 *
732 * New objects start out as inactive in the current and active in the
733 * next generation. When committing the ruleset the bitmask is cleared,
734 * meaning they're active in all generations. When removing an object,
735 * it is set inactive in the next generation. After committing the ruleset,
736 * the objects are removed.
737 */
738static inline unsigned int nft_gencursor_next(const struct net *net)
739{
740 return net->nft.gencursor + 1 == 1 ? 1 : 0;
741}
742
743static inline u8 nft_genmask_next(const struct net *net)
744{
745 return 1 << nft_gencursor_next(net);
746}
747
748static inline u8 nft_genmask_cur(const struct net *net)
749{
750 /* Use ACCESS_ONCE() to prevent refetching the value for atomicity */
751 return 1 << ACCESS_ONCE(net->nft.gencursor);
752}
753
cc02e457
PM
754/*
755 * Set element transaction helpers
756 */
757
758static inline bool nft_set_elem_active(const struct nft_set_ext *ext,
759 u8 genmask)
760{
761 return !(ext->genmask & genmask);
762}
763
764static inline void nft_set_elem_change_active(const struct nft_set *set,
765 struct nft_set_ext *ext)
766{
767 ext->genmask ^= nft_genmask_next(read_pnet(&set->pnet));
768}
769
1a1e1a12
PM
770/**
771 * struct nft_trans - nf_tables object update in transaction
772 *
773 * @list: used internally
774 * @msg_type: message type
775 * @ctx: transaction context
776 * @data: internal information related to the transaction
777 */
778struct nft_trans {
779 struct list_head list;
780 int msg_type;
781 struct nft_ctx ctx;
782 char data[0];
783};
784
785struct nft_trans_rule {
786 struct nft_rule *rule;
787};
788
789#define nft_trans_rule(trans) \
790 (((struct nft_trans_rule *)trans->data)->rule)
791
792struct nft_trans_set {
793 struct nft_set *set;
794 u32 set_id;
795};
796
797#define nft_trans_set(trans) \
798 (((struct nft_trans_set *)trans->data)->set)
799#define nft_trans_set_id(trans) \
800 (((struct nft_trans_set *)trans->data)->set_id)
801
802struct nft_trans_chain {
803 bool update;
804 char name[NFT_CHAIN_MAXNAMELEN];
805 struct nft_stats __percpu *stats;
806 u8 policy;
807};
808
809#define nft_trans_chain_update(trans) \
810 (((struct nft_trans_chain *)trans->data)->update)
811#define nft_trans_chain_name(trans) \
812 (((struct nft_trans_chain *)trans->data)->name)
813#define nft_trans_chain_stats(trans) \
814 (((struct nft_trans_chain *)trans->data)->stats)
815#define nft_trans_chain_policy(trans) \
816 (((struct nft_trans_chain *)trans->data)->policy)
817
818struct nft_trans_table {
819 bool update;
820 bool enable;
821};
822
823#define nft_trans_table_update(trans) \
824 (((struct nft_trans_table *)trans->data)->update)
825#define nft_trans_table_enable(trans) \
826 (((struct nft_trans_table *)trans->data)->enable)
827
828struct nft_trans_elem {
829 struct nft_set *set;
830 struct nft_set_elem elem;
831};
832
833#define nft_trans_elem_set(trans) \
834 (((struct nft_trans_elem *)trans->data)->set)
835#define nft_trans_elem(trans) \
836 (((struct nft_trans_elem *)trans->data)->elem)
837
96518518 838#endif /* _NET_NF_TABLES_H */