netfilter: ebtables: use __u8 instead of uint8_t in uapi header
[linux-block.git] / include / net / netfilter / nf_tables.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
96518518
PM
2#ifndef _NET_NF_TABLES_H
3#define _NET_NF_TABLES_H
4
a1b840ad 5#include <asm/unaligned.h>
96518518
PM
6#include <linux/list.h>
7#include <linux/netfilter.h>
67a8fc27 8#include <linux/netfilter/nfnetlink.h>
0ca743a5 9#include <linux/netfilter/x_tables.h>
96518518 10#include <linux/netfilter/nf_tables.h>
ce355e20 11#include <linux/u64_stats_sync.h>
1b2470e5 12#include <linux/rhashtable.h>
3b49e2e9 13#include <net/netfilter/nf_flow_table.h>
96518518 14#include <net/netlink.h>
14bfb13f 15#include <net/flow_offload.h>
96518518 16
a4cb98f3
PG
17struct module;
18
20a69341
PM
19#define NFT_JUMP_STACK_SIZE 16
20
96518518
PM
21struct nft_pktinfo {
22 struct sk_buff *skb;
beac5afa 23 bool tprot_set;
4566bf27 24 u8 tprot;
0ca743a5
PNA
25 /* for x_tables compatibility */
26 struct xt_action_param xt;
96518518
PM
27};
28
0e5a1c7e
PNA
29static inline struct net *nft_net(const struct nft_pktinfo *pkt)
30{
31 return pkt->xt.state->net;
32}
33
34static inline unsigned int nft_hook(const struct nft_pktinfo *pkt)
35{
36 return pkt->xt.state->hook;
37}
38
39static inline u8 nft_pf(const struct nft_pktinfo *pkt)
40{
41 return pkt->xt.state->pf;
42}
43
44static inline const struct net_device *nft_in(const struct nft_pktinfo *pkt)
45{
46 return pkt->xt.state->in;
47}
48
49static inline const struct net_device *nft_out(const struct nft_pktinfo *pkt)
50{
51 return pkt->xt.state->out;
52}
53
0ca743a5 54static inline void nft_set_pktinfo(struct nft_pktinfo *pkt,
0ca743a5 55 struct sk_buff *skb,
073bfd56 56 const struct nf_hook_state *state)
0ca743a5
PNA
57{
58 pkt->skb = skb;
613dbd95 59 pkt->xt.state = state;
0ca743a5
PNA
60}
61
7a4473a3
PNA
62static inline void nft_set_pktinfo_unspec(struct nft_pktinfo *pkt,
63 struct sk_buff *skb)
beac5afa
PNA
64{
65 pkt->tprot_set = false;
66 pkt->tprot = 0;
67 pkt->xt.thoff = 0;
68 pkt->xt.fragoff = 0;
69}
70
a55e22e9
PM
71/**
72 * struct nft_verdict - nf_tables verdict
73 *
74 * @code: nf_tables/netfilter verdict code
75 * @chain: destination chain for NFT_JUMP/NFT_GOTO
76 */
77struct nft_verdict {
78 u32 code;
79 struct nft_chain *chain;
80};
81
96518518
PM
82struct nft_data {
83 union {
1ca2e170
PM
84 u32 data[4];
85 struct nft_verdict verdict;
96518518
PM
86 };
87} __attribute__((aligned(__alignof__(u64))));
88
a55e22e9
PM
89/**
90 * struct nft_regs - nf_tables register set
91 *
92 * @data: data registers
93 * @verdict: verdict register
94 *
95 * The first four data registers alias to the verdict register.
96 */
97struct nft_regs {
98 union {
49499c3e 99 u32 data[20];
a55e22e9
PM
100 struct nft_verdict verdict;
101 };
102};
103
a1b840ad 104/* Store/load an u8, u16 or u64 integer to/from the u32 data register.
10596608
LZ
105 *
106 * Note, when using concatenations, register allocation happens at 32-bit
107 * level. So for store instruction, pad the rest part with zero to avoid
108 * garbage values.
109 */
110
a1b840ad 111static inline void nft_reg_store8(u32 *dreg, u8 val)
10596608
LZ
112{
113 *dreg = 0;
a1b840ad 114 *(u8 *)dreg = val;
10596608
LZ
115}
116
a1b840ad
AJ
117static inline u8 nft_reg_load8(u32 *sreg)
118{
119 return *(u8 *)sreg;
120}
121
122static inline void nft_reg_store16(u32 *dreg, u16 val)
10596608
LZ
123{
124 *dreg = 0;
a1b840ad 125 *(u16 *)dreg = val;
10596608
LZ
126}
127
128static inline u16 nft_reg_load16(u32 *sreg)
129{
130 return *(u16 *)sreg;
131}
132
a1b840ad 133static inline void nft_reg_store64(u32 *dreg, u64 val)
10596608 134{
a1b840ad
AJ
135 put_unaligned(val, (u64 *)dreg);
136}
137
138static inline u64 nft_reg_load64(u32 *sreg)
139{
140 return get_unaligned((u64 *)sreg);
10596608
LZ
141}
142
49499c3e
PM
143static inline void nft_data_copy(u32 *dst, const struct nft_data *src,
144 unsigned int len)
96518518 145{
49499c3e 146 memcpy(dst, src, len);
96518518
PM
147}
148
149static inline void nft_data_debug(const struct nft_data *data)
150{
151 pr_debug("data[0]=%x data[1]=%x data[2]=%x data[3]=%x\n",
152 data->data[0], data->data[1],
153 data->data[2], data->data[3]);
154}
155
156/**
20a69341 157 * struct nft_ctx - nf_tables rule/set context
96518518 158 *
99633ab2 159 * @net: net namespace
96518518
PM
160 * @table: the table the chain is contained in
161 * @chain: the chain the rule is contained in
0ca743a5 162 * @nla: netlink attributes
128ad332
PNA
163 * @portid: netlink portID of the original message
164 * @seq: netlink sequence number
36596dad 165 * @family: protocol family
26b2f552 166 * @level: depth of the chains
128ad332 167 * @report: notify via unicast netlink message
96518518
PM
168 */
169struct nft_ctx {
99633ab2 170 struct net *net;
7c95f6d8
PNA
171 struct nft_table *table;
172 struct nft_chain *chain;
0ca743a5 173 const struct nlattr * const *nla;
128ad332
PNA
174 u32 portid;
175 u32 seq;
c9626a2c 176 u16 flags;
36596dad 177 u8 family;
26b2f552 178 u8 level;
128ad332 179 bool report;
96518518
PM
180};
181
96518518
PM
182struct nft_data_desc {
183 enum nft_data_types type;
184 unsigned int len;
185};
186
d0a11fc3
PM
187int nft_data_init(const struct nft_ctx *ctx,
188 struct nft_data *data, unsigned int size,
5eccdfaa 189 struct nft_data_desc *desc, const struct nlattr *nla);
bb7b40ae 190void nft_data_hold(const struct nft_data *data, enum nft_data_types type);
59105446 191void nft_data_release(const struct nft_data *data, enum nft_data_types type);
5eccdfaa
JP
192int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
193 enum nft_data_types type, unsigned int len);
96518518
PM
194
195static inline enum nft_data_types nft_dreg_to_type(enum nft_registers reg)
196{
197 return reg == NFT_REG_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE;
198}
199
20a69341
PM
200static inline enum nft_registers nft_type_to_reg(enum nft_data_types type)
201{
bf798657 202 return type == NFT_DATA_VERDICT ? NFT_REG_VERDICT : NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE;
20a69341
PM
203}
204
f1d505bb 205int nft_parse_u32_check(const struct nlattr *attr, int max, u32 *dest);
b1c96ed3
PM
206unsigned int nft_parse_register(const struct nlattr *attr);
207int nft_dump_register(struct sk_buff *skb, unsigned int attr, unsigned int reg);
208
d07db988 209int nft_validate_register_load(enum nft_registers reg, unsigned int len);
1ec10212
PM
210int nft_validate_register_store(const struct nft_ctx *ctx,
211 enum nft_registers reg,
212 const struct nft_data *data,
213 enum nft_data_types type, unsigned int len);
96518518 214
86f1ec32
PM
215/**
216 * struct nft_userdata - user defined data associated with an object
217 *
218 * @len: length of the data
219 * @data: content
220 *
221 * The presence of user data is indicated in an object specific fashion,
222 * so a length of zero can't occur and the value "len" indicates data
223 * of length len + 1.
224 */
225struct nft_userdata {
226 u8 len;
227 unsigned char data[0];
228};
229
20a69341
PM
230/**
231 * struct nft_set_elem - generic representation of set elements
232 *
20a69341 233 * @key: element key
fe2811eb 234 * @priv: element private data and extensions
20a69341
PM
235 */
236struct nft_set_elem {
7d740264
PM
237 union {
238 u32 buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)];
239 struct nft_data val;
240 } key;
fe2811eb 241 void *priv;
20a69341
PM
242};
243
244struct nft_set;
245struct nft_set_iter {
8588ac09 246 u8 genmask;
20a69341
PM
247 unsigned int count;
248 unsigned int skip;
249 int err;
250 int (*fn)(const struct nft_ctx *ctx,
de70185d 251 struct nft_set *set,
20a69341 252 const struct nft_set_iter *iter,
de70185d 253 struct nft_set_elem *elem);
20a69341
PM
254};
255
c50b960c
PM
256/**
257 * struct nft_set_desc - description of set elements
258 *
259 * @klen: key length
260 * @dlen: data length
261 * @size: number of set elements
262 */
263struct nft_set_desc {
264 unsigned int klen;
265 unsigned int dlen;
266 unsigned int size;
267};
268
269/**
270 * enum nft_set_class - performance class
271 *
272 * @NFT_LOOKUP_O_1: constant, O(1)
273 * @NFT_LOOKUP_O_LOG_N: logarithmic, O(log N)
274 * @NFT_LOOKUP_O_N: linear, O(N)
275 */
276enum nft_set_class {
277 NFT_SET_CLASS_O_1,
278 NFT_SET_CLASS_O_LOG_N,
279 NFT_SET_CLASS_O_N,
280};
281
282/**
283 * struct nft_set_estimate - estimation of memory and performance
284 * characteristics
285 *
286 * @size: required memory
55af753c 287 * @lookup: lookup performance class
0b5a7874 288 * @space: memory class
c50b960c
PM
289 */
290struct nft_set_estimate {
4ef360dd 291 u64 size;
55af753c 292 enum nft_set_class lookup;
0b5a7874 293 enum nft_set_class space;
c50b960c
PM
294};
295
b2832dd6 296struct nft_set_ext;
22fe54d5 297struct nft_expr;
b2832dd6 298
20a69341
PM
299/**
300 * struct nft_set_ops - nf_tables set operations
301 *
302 * @lookup: look up an element within the set
d0a8d877
AJ
303 * @update: update an element if exists, add it if doesn't exist
304 * @delete: delete an element
20a69341 305 * @insert: insert new element into set
cc02e457 306 * @activate: activate new element in the next generation
8411b644 307 * @deactivate: lookup for element and deactivate it in the next generation
1ba1c414 308 * @flush: deactivate element in the next generation
20a69341 309 * @remove: remove element from set
d0a8d877 310 * @walk: iterate over all set elements
ba0e4d99 311 * @get: get set elements
20a69341
PM
312 * @privsize: function to return size of set private data
313 * @init: initialize private data of new set instance
314 * @destroy: destroy private data of set instance
fe2811eb 315 * @elemsize: element private size
d0a8d877
AJ
316 *
317 * Operations lookup, update and delete have simpler interfaces, are faster
318 * and currently only used in the packet path. All the rest are slower,
319 * control plane functions.
20a69341
PM
320 */
321struct nft_set_ops {
42a55769
PNA
322 bool (*lookup)(const struct net *net,
323 const struct nft_set *set,
8cd8937a 324 const u32 *key,
b2832dd6 325 const struct nft_set_ext **ext);
22fe54d5 326 bool (*update)(struct nft_set *set,
8cd8937a 327 const u32 *key,
22fe54d5
PM
328 void *(*new)(struct nft_set *,
329 const struct nft_expr *,
a55e22e9 330 struct nft_regs *),
22fe54d5 331 const struct nft_expr *expr,
a55e22e9 332 struct nft_regs *regs,
22fe54d5 333 const struct nft_set_ext **ext);
d0a8d877
AJ
334 bool (*delete)(const struct nft_set *set,
335 const u32 *key);
22fe54d5 336
42a55769
PNA
337 int (*insert)(const struct net *net,
338 const struct nft_set *set,
c016c7e4
PNA
339 const struct nft_set_elem *elem,
340 struct nft_set_ext **ext);
42a55769
PNA
341 void (*activate)(const struct net *net,
342 const struct nft_set *set,
cc02e457 343 const struct nft_set_elem *elem);
42a55769
PNA
344 void * (*deactivate)(const struct net *net,
345 const struct nft_set *set,
cc02e457 346 const struct nft_set_elem *elem);
1ba1c414
PNA
347 bool (*flush)(const struct net *net,
348 const struct nft_set *set,
349 void *priv);
5cb82a38
PNA
350 void (*remove)(const struct net *net,
351 const struct nft_set *set,
20a69341
PM
352 const struct nft_set_elem *elem);
353 void (*walk)(const struct nft_ctx *ctx,
de70185d 354 struct nft_set *set,
20a69341 355 struct nft_set_iter *iter);
ba0e4d99
PNA
356 void * (*get)(const struct net *net,
357 const struct nft_set *set,
358 const struct nft_set_elem *elem,
359 unsigned int flags);
20a69341 360
4ef360dd 361 u64 (*privsize)(const struct nlattr * const nla[],
347b408d 362 const struct nft_set_desc *desc);
c50b960c
PM
363 bool (*estimate)(const struct nft_set_desc *desc,
364 u32 features,
365 struct nft_set_estimate *est);
20a69341 366 int (*init)(const struct nft_set *set,
c50b960c 367 const struct nft_set_desc *desc,
20a69341
PM
368 const struct nlattr * const nla[]);
369 void (*destroy)(const struct nft_set *set);
79b174ad 370 void (*gc_init)(const struct nft_set *set);
20a69341 371
fe2811eb 372 unsigned int elemsize;
71cc0873
PS
373};
374
375/**
376 * struct nft_set_type - nf_tables set type
377 *
378 * @ops: set ops for this type
379 * @list: used internally
380 * @owner: module reference
381 * @features: features supported by the implementation
382 */
383struct nft_set_type {
384 const struct nft_set_ops ops;
385 struct list_head list;
386 struct module *owner;
20a69341
PM
387 u32 features;
388};
71cc0873 389#define to_set_type(o) container_of(o, struct nft_set_type, ops)
20a69341 390
2b664957
PNA
391int nft_register_set(struct nft_set_type *type);
392void nft_unregister_set(struct nft_set_type *type);
20a69341
PM
393
394/**
395 * struct nft_set - nf_tables set instance
396 *
397 * @list: table set list node
398 * @bindings: list of set bindings
3453c927
PNA
399 * @table: table this set belongs to
400 * @net: netnamespace this set belongs to
20a69341 401 * @name: name of the set
3ecbfd65 402 * @handle: unique handle of the set
20a69341
PM
403 * @ktype: key type (numeric type defined by userspace, not used in the kernel)
404 * @dtype: data type (verdict or numeric type defined by userspace)
8aeff920 405 * @objtype: object type (see NFT_OBJECT_* definitions)
c50b960c 406 * @size: maximum set size
273fe3f1 407 * @use: number of rules references to this set
c50b960c 408 * @nelems: number of elements
3dd0673a 409 * @ndeact: number of deactivated elements queued for removal
d3e2a111 410 * @timeout: default timeout value in jiffies
761da293 411 * @gc_int: garbage collection interval in msecs
9363dc4b 412 * @policy: set parameterization (see enum nft_set_policies)
e6d8ecac
CFG
413 * @udlen: user data length
414 * @udata: user data
20a69341
PM
415 * @ops: set ops
416 * @flags: set flags
37a9cc52 417 * @genmask: generation mask
20a69341
PM
418 * @klen: key length
419 * @dlen: data length
420 * @data: private set data
421 */
422struct nft_set {
423 struct list_head list;
424 struct list_head bindings;
3453c927
PNA
425 struct nft_table *table;
426 possible_net_t net;
38745490 427 char *name;
3ecbfd65 428 u64 handle;
20a69341
PM
429 u32 ktype;
430 u32 dtype;
8aeff920 431 u32 objtype;
c50b960c 432 u32 size;
273fe3f1 433 u32 use;
3dd0673a
PM
434 atomic_t nelems;
435 u32 ndeact;
761da293
PM
436 u64 timeout;
437 u32 gc_int;
9363dc4b 438 u16 policy;
e6d8ecac
CFG
439 u16 udlen;
440 unsigned char *udata;
20a69341
PM
441 /* runtime data below here */
442 const struct nft_set_ops *ops ____cacheline_aligned;
6a0a8d10 443 u16 flags:14,
37a9cc52 444 genmask:2;
20a69341
PM
445 u8 klen;
446 u8 dlen;
447 unsigned char data[]
448 __attribute__((aligned(__alignof__(u64))));
449};
450
408070d6
PNA
451static inline bool nft_set_is_anonymous(const struct nft_set *set)
452{
453 return set->flags & NFT_SET_ANONYMOUS;
454}
455
20a69341
PM
456static inline void *nft_set_priv(const struct nft_set *set)
457{
458 return (void *)set->data;
459}
460
9d098292
PM
461static inline struct nft_set *nft_set_container_of(const void *priv)
462{
463 return (void *)priv - offsetof(struct nft_set, data);
464}
465
10659cba
PNA
466struct nft_set *nft_set_lookup_global(const struct net *net,
467 const struct nft_table *table,
468 const struct nlattr *nla_set_name,
469 const struct nlattr *nla_set_id,
470 u8 genmask);
20a69341 471
761da293
PM
472static inline unsigned long nft_set_gc_interval(const struct nft_set *set)
473{
474 return set->gc_int ? msecs_to_jiffies(set->gc_int) : HZ;
475}
476
20a69341
PM
477/**
478 * struct nft_set_binding - nf_tables set binding
479 *
480 * @list: set bindings list node
481 * @chain: chain containing the rule bound to the set
11113e19 482 * @flags: set action flags
20a69341
PM
483 *
484 * A set binding contains all information necessary for validation
485 * of new elements added to a bound set.
486 */
487struct nft_set_binding {
488 struct list_head list;
489 const struct nft_chain *chain;
11113e19 490 u32 flags;
20a69341
PM
491};
492
273fe3f1
PNA
493enum nft_trans_phase;
494void nf_tables_deactivate_set(const struct nft_ctx *ctx, struct nft_set *set,
495 struct nft_set_binding *binding,
496 enum nft_trans_phase phase);
5eccdfaa
JP
497int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
498 struct nft_set_binding *binding);
cd5125d8 499void nf_tables_destroy_set(const struct nft_ctx *ctx, struct nft_set *set);
20a69341 500
3ac4c07a
PM
501/**
502 * enum nft_set_extensions - set extension type IDs
503 *
504 * @NFT_SET_EXT_KEY: element key
505 * @NFT_SET_EXT_DATA: mapping data
506 * @NFT_SET_EXT_FLAGS: element flags
c3e1b005
PM
507 * @NFT_SET_EXT_TIMEOUT: element timeout
508 * @NFT_SET_EXT_EXPIRATION: element expiration time
68e942e8 509 * @NFT_SET_EXT_USERDATA: user data associated with the element
f25ad2e9 510 * @NFT_SET_EXT_EXPR: expression assiociated with the element
8aeff920 511 * @NFT_SET_EXT_OBJREF: stateful object reference associated with element
3ac4c07a
PM
512 * @NFT_SET_EXT_NUM: number of extension types
513 */
514enum nft_set_extensions {
515 NFT_SET_EXT_KEY,
516 NFT_SET_EXT_DATA,
517 NFT_SET_EXT_FLAGS,
c3e1b005
PM
518 NFT_SET_EXT_TIMEOUT,
519 NFT_SET_EXT_EXPIRATION,
68e942e8 520 NFT_SET_EXT_USERDATA,
f25ad2e9 521 NFT_SET_EXT_EXPR,
8aeff920 522 NFT_SET_EXT_OBJREF,
3ac4c07a
PM
523 NFT_SET_EXT_NUM
524};
525
526/**
527 * struct nft_set_ext_type - set extension type
528 *
529 * @len: fixed part length of the extension
530 * @align: alignment requirements of the extension
531 */
532struct nft_set_ext_type {
533 u8 len;
534 u8 align;
535};
536
537extern const struct nft_set_ext_type nft_set_ext_types[];
538
539/**
540 * struct nft_set_ext_tmpl - set extension template
541 *
542 * @len: length of extension area
543 * @offset: offsets of individual extension types
544 */
545struct nft_set_ext_tmpl {
546 u16 len;
547 u8 offset[NFT_SET_EXT_NUM];
548};
549
550/**
551 * struct nft_set_ext - set extensions
552 *
cc02e457 553 * @genmask: generation mask
3ac4c07a
PM
554 * @offset: offsets of individual extension types
555 * @data: beginning of extension data
556 */
557struct nft_set_ext {
cc02e457 558 u8 genmask;
3ac4c07a
PM
559 u8 offset[NFT_SET_EXT_NUM];
560 char data[0];
561};
562
563static inline void nft_set_ext_prepare(struct nft_set_ext_tmpl *tmpl)
564{
565 memset(tmpl, 0, sizeof(*tmpl));
566 tmpl->len = sizeof(struct nft_set_ext);
567}
568
569static inline void nft_set_ext_add_length(struct nft_set_ext_tmpl *tmpl, u8 id,
570 unsigned int len)
571{
572 tmpl->len = ALIGN(tmpl->len, nft_set_ext_types[id].align);
573 BUG_ON(tmpl->len > U8_MAX);
574 tmpl->offset[id] = tmpl->len;
575 tmpl->len += nft_set_ext_types[id].len + len;
576}
577
578static inline void nft_set_ext_add(struct nft_set_ext_tmpl *tmpl, u8 id)
579{
580 nft_set_ext_add_length(tmpl, id, 0);
581}
582
583static inline void nft_set_ext_init(struct nft_set_ext *ext,
584 const struct nft_set_ext_tmpl *tmpl)
585{
586 memcpy(ext->offset, tmpl->offset, sizeof(ext->offset));
587}
588
589static inline bool __nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
590{
591 return !!ext->offset[id];
592}
593
594static inline bool nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
595{
596 return ext && __nft_set_ext_exists(ext, id);
597}
598
599static inline void *nft_set_ext(const struct nft_set_ext *ext, u8 id)
600{
601 return (void *)ext + ext->offset[id];
602}
603
604static inline struct nft_data *nft_set_ext_key(const struct nft_set_ext *ext)
605{
606 return nft_set_ext(ext, NFT_SET_EXT_KEY);
607}
608
609static inline struct nft_data *nft_set_ext_data(const struct nft_set_ext *ext)
610{
611 return nft_set_ext(ext, NFT_SET_EXT_DATA);
612}
613
614static inline u8 *nft_set_ext_flags(const struct nft_set_ext *ext)
615{
616 return nft_set_ext(ext, NFT_SET_EXT_FLAGS);
617}
ef1f7df9 618
c3e1b005
PM
619static inline u64 *nft_set_ext_timeout(const struct nft_set_ext *ext)
620{
621 return nft_set_ext(ext, NFT_SET_EXT_TIMEOUT);
622}
623
8e1102d5 624static inline u64 *nft_set_ext_expiration(const struct nft_set_ext *ext)
c3e1b005
PM
625{
626 return nft_set_ext(ext, NFT_SET_EXT_EXPIRATION);
627}
628
68e942e8
PM
629static inline struct nft_userdata *nft_set_ext_userdata(const struct nft_set_ext *ext)
630{
631 return nft_set_ext(ext, NFT_SET_EXT_USERDATA);
632}
633
f25ad2e9
PM
634static inline struct nft_expr *nft_set_ext_expr(const struct nft_set_ext *ext)
635{
636 return nft_set_ext(ext, NFT_SET_EXT_EXPR);
637}
638
c3e1b005
PM
639static inline bool nft_set_elem_expired(const struct nft_set_ext *ext)
640{
641 return nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION) &&
8e1102d5 642 time_is_before_eq_jiffies64(*nft_set_ext_expiration(ext));
c3e1b005
PM
643}
644
fe2811eb
PM
645static inline struct nft_set_ext *nft_set_elem_ext(const struct nft_set *set,
646 void *elem)
647{
648 return elem + set->ops->elemsize;
649}
650
8aeff920
PNA
651static inline struct nft_object **nft_set_ext_obj(const struct nft_set_ext *ext)
652{
653 return nft_set_ext(ext, NFT_SET_EXT_OBJREF);
654}
655
22fe54d5
PM
656void *nft_set_elem_init(const struct nft_set *set,
657 const struct nft_set_ext_tmpl *tmpl,
49499c3e 658 const u32 *key, const u32 *data,
79ebb5bb 659 u64 timeout, u64 expiration, gfp_t gfp);
61f9e292
LZ
660void nft_set_elem_destroy(const struct nft_set *set, void *elem,
661 bool destroy_expr);
61edafbb 662
cfed7e1b
PM
663/**
664 * struct nft_set_gc_batch_head - nf_tables set garbage collection batch
665 *
666 * @rcu: rcu head
667 * @set: set the elements belong to
668 * @cnt: count of elements
669 */
670struct nft_set_gc_batch_head {
671 struct rcu_head rcu;
672 const struct nft_set *set;
673 unsigned int cnt;
674};
675
676#define NFT_SET_GC_BATCH_SIZE ((PAGE_SIZE - \
677 sizeof(struct nft_set_gc_batch_head)) / \
678 sizeof(void *))
679
680/**
681 * struct nft_set_gc_batch - nf_tables set garbage collection batch
682 *
683 * @head: GC batch head
684 * @elems: garbage collection elements
685 */
686struct nft_set_gc_batch {
687 struct nft_set_gc_batch_head head;
688 void *elems[NFT_SET_GC_BATCH_SIZE];
689};
690
691struct nft_set_gc_batch *nft_set_gc_batch_alloc(const struct nft_set *set,
692 gfp_t gfp);
693void nft_set_gc_batch_release(struct rcu_head *rcu);
694
695static inline void nft_set_gc_batch_complete(struct nft_set_gc_batch *gcb)
696{
697 if (gcb != NULL)
698 call_rcu(&gcb->head.rcu, nft_set_gc_batch_release);
699}
700
701static inline struct nft_set_gc_batch *
702nft_set_gc_batch_check(const struct nft_set *set, struct nft_set_gc_batch *gcb,
703 gfp_t gfp)
704{
705 if (gcb != NULL) {
706 if (gcb->head.cnt + 1 < ARRAY_SIZE(gcb->elems))
707 return gcb;
708 nft_set_gc_batch_complete(gcb);
709 }
710 return nft_set_gc_batch_alloc(set, gfp);
711}
712
713static inline void nft_set_gc_batch_add(struct nft_set_gc_batch *gcb,
714 void *elem)
715{
716 gcb->elems[gcb->head.cnt++] = elem;
717}
718
b8e20400 719struct nft_expr_ops;
96518518 720/**
ef1f7df9 721 * struct nft_expr_type - nf_tables expression type
96518518 722 *
ef1f7df9 723 * @select_ops: function to select nft_expr_ops
b8e20400 724 * @release_ops: release nft_expr_ops
ef1f7df9 725 * @ops: default ops, used when no select_ops functions is present
96518518
PM
726 * @list: used internally
727 * @name: Identifier
728 * @owner: module reference
729 * @policy: netlink attribute policy
730 * @maxattr: highest netlink attribute number
64d46806 731 * @family: address family for AF-specific types
151d799a 732 * @flags: expression type flags
ef1f7df9
PM
733 */
734struct nft_expr_type {
0ca743a5
PNA
735 const struct nft_expr_ops *(*select_ops)(const struct nft_ctx *,
736 const struct nlattr * const tb[]);
b8e20400 737 void (*release_ops)(const struct nft_expr_ops *ops);
ef1f7df9
PM
738 const struct nft_expr_ops *ops;
739 struct list_head list;
740 const char *name;
741 struct module *owner;
742 const struct nla_policy *policy;
743 unsigned int maxattr;
64d46806 744 u8 family;
151d799a 745 u8 flags;
ef1f7df9
PM
746};
747
151d799a 748#define NFT_EXPR_STATEFUL 0x1
79b174ad 749#define NFT_EXPR_GC 0x2
151d799a 750
f6ac8585
PNA
751enum nft_trans_phase {
752 NFT_TRANS_PREPARE,
753 NFT_TRANS_ABORT,
754 NFT_TRANS_COMMIT,
755 NFT_TRANS_RELEASE
756};
757
c9626a2c
PNA
758struct nft_flow_rule;
759struct nft_offload_ctx;
760
ef1f7df9
PM
761/**
762 * struct nft_expr_ops - nf_tables expression operations
763 *
764 * @eval: Expression evaluation function
96518518 765 * @size: full expression size, including private data size
ef1f7df9 766 * @init: initialization function
cd5125d8
FW
767 * @activate: activate expression in the next generation
768 * @deactivate: deactivate expression in next generation
769 * @destroy: destruction function, called after synchronize_rcu
ef1f7df9
PM
770 * @dump: function to dump parameters
771 * @type: expression type
0ca743a5
PNA
772 * @validate: validate expression, called during loop detection
773 * @data: extra data to attach to this expression operation
96518518
PM
774 */
775struct nft_expr;
776struct nft_expr_ops {
777 void (*eval)(const struct nft_expr *expr,
a55e22e9 778 struct nft_regs *regs,
96518518 779 const struct nft_pktinfo *pkt);
086f3321
PNA
780 int (*clone)(struct nft_expr *dst,
781 const struct nft_expr *src);
ef1f7df9
PM
782 unsigned int size;
783
96518518
PM
784 int (*init)(const struct nft_ctx *ctx,
785 const struct nft_expr *expr,
786 const struct nlattr * const tb[]);
bb7b40ae
PNA
787 void (*activate)(const struct nft_ctx *ctx,
788 const struct nft_expr *expr);
789 void (*deactivate)(const struct nft_ctx *ctx,
f6ac8585
PNA
790 const struct nft_expr *expr,
791 enum nft_trans_phase phase);
62472bce
PM
792 void (*destroy)(const struct nft_ctx *ctx,
793 const struct nft_expr *expr);
371ebcbb
PNA
794 void (*destroy_clone)(const struct nft_ctx *ctx,
795 const struct nft_expr *expr);
96518518
PM
796 int (*dump)(struct sk_buff *skb,
797 const struct nft_expr *expr);
0ca743a5
PNA
798 int (*validate)(const struct nft_ctx *ctx,
799 const struct nft_expr *expr,
800 const struct nft_data **data);
79b174ad
PNA
801 bool (*gc)(struct net *net,
802 const struct nft_expr *expr);
c9626a2c
PNA
803 int (*offload)(struct nft_offload_ctx *ctx,
804 struct nft_flow_rule *flow,
805 const struct nft_expr *expr);
806 u32 offload_flags;
ef1f7df9 807 const struct nft_expr_type *type;
0ca743a5 808 void *data;
96518518
PM
809};
810
ef1f7df9 811#define NFT_EXPR_MAXATTR 16
96518518
PM
812#define NFT_EXPR_SIZE(size) (sizeof(struct nft_expr) + \
813 ALIGN(size, __alignof__(struct nft_expr)))
814
815/**
816 * struct nft_expr - nf_tables expression
817 *
818 * @ops: expression ops
819 * @data: expression private data
820 */
821struct nft_expr {
822 const struct nft_expr_ops *ops;
823 unsigned char data[];
824};
825
826static inline void *nft_expr_priv(const struct nft_expr *expr)
827{
828 return (void *)expr->data;
829}
830
0b2d8a7b
PM
831struct nft_expr *nft_expr_init(const struct nft_ctx *ctx,
832 const struct nlattr *nla);
833void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr);
834int nft_expr_dump(struct sk_buff *skb, unsigned int attr,
835 const struct nft_expr *expr);
836
96518518
PM
837/**
838 * struct nft_rule - nf_tables rule
839 *
840 * @list: used internally
96518518 841 * @handle: rule handle
0628b123 842 * @genmask: generation mask
96518518 843 * @dlen: length of expression data
86f1ec32 844 * @udata: user data is appended to the rule
96518518
PM
845 * @data: expression data
846 */
847struct nft_rule {
848 struct list_head list;
0768b3b3 849 u64 handle:42,
0628b123 850 genmask:2,
0768b3b3 851 dlen:12,
86f1ec32 852 udata:1;
96518518
PM
853 unsigned char data[]
854 __attribute__((aligned(__alignof__(struct nft_expr))));
855};
856
857static inline struct nft_expr *nft_expr_first(const struct nft_rule *rule)
858{
859 return (struct nft_expr *)&rule->data[0];
860}
861
862static inline struct nft_expr *nft_expr_next(const struct nft_expr *expr)
863{
864 return ((void *)expr) + expr->ops->size;
865}
866
867static inline struct nft_expr *nft_expr_last(const struct nft_rule *rule)
868{
869 return (struct nft_expr *)&rule->data[rule->dlen];
870}
871
86f1ec32 872static inline struct nft_userdata *nft_userdata(const struct nft_rule *rule)
0768b3b3
PNA
873{
874 return (void *)&rule->data[rule->dlen];
875}
876
96518518
PM
877/*
878 * The last pointer isn't really necessary, but the compiler isn't able to
879 * determine that the result of nft_expr_last() is always the same since it
880 * can't assume that the dlen value wasn't changed within calls in the loop.
881 */
882#define nft_rule_for_each_expr(expr, last, rule) \
883 for ((expr) = nft_expr_first(rule), (last) = nft_expr_last(rule); \
884 (expr) != (last); \
885 (expr) = nft_expr_next(expr))
886
887enum nft_chain_flags {
888 NFT_BASE_CHAIN = 0x1,
c9626a2c 889 NFT_CHAIN_HW_OFFLOAD = 0x2,
96518518
PM
890};
891
ad652f38
PNA
892#define NFT_CHAIN_POLICY_UNSET U8_MAX
893
96518518
PM
894/**
895 * struct nft_chain - nf_tables chain
896 *
897 * @rules: list of rules in the chain
898 * @list: used internally
1b2470e5 899 * @rhlhead: used internally
b5bc89bf 900 * @table: table that this chain belongs to
96518518 901 * @handle: chain handle
96518518 902 * @use: number of jump references to this chain
a0a7379e 903 * @flags: bitmask of enum nft_chain_flags
96518518
PM
904 * @name: name of the chain
905 */
906struct nft_chain {
0cbc06b3
FW
907 struct nft_rule *__rcu *rules_gen_0;
908 struct nft_rule *__rcu *rules_gen_1;
96518518
PM
909 struct list_head rules;
910 struct list_head list;
1b2470e5 911 struct rhlist_head rhlhead;
b5bc89bf 912 struct nft_table *table;
96518518 913 u64 handle;
a0a7379e 914 u32 use;
664b0f8c
PNA
915 u8 flags:6,
916 genmask:2;
b7263e07 917 char *name;
0cbc06b3
FW
918
919 /* Only used during control plane commit phase: */
920 struct nft_rule **rules_next;
96518518
PM
921};
922
a654de8f
PNA
923int nft_chain_validate(const struct nft_ctx *ctx, const struct nft_chain *chain);
924
32537e91 925enum nft_chain_types {
9370761c
PNA
926 NFT_CHAIN_T_DEFAULT = 0,
927 NFT_CHAIN_T_ROUTE,
928 NFT_CHAIN_T_NAT,
929 NFT_CHAIN_T_MAX
930};
931
1a1e1a12 932/**
32537e91 933 * struct nft_chain_type - nf_tables chain type info
1a1e1a12
PM
934 *
935 * @name: name of the type
936 * @type: numeric identifier
937 * @family: address family
938 * @owner: module owner
939 * @hook_mask: mask of valid hooks
c2f9eafe 940 * @hooks: array of hook functions
4e25ceb8
FW
941 * @ops_register: base chain register function
942 * @ops_unregister: base chain unregister function
1a1e1a12 943 */
32537e91 944struct nft_chain_type {
1a1e1a12 945 const char *name;
32537e91 946 enum nft_chain_types type;
1a1e1a12
PM
947 int family;
948 struct module *owner;
949 unsigned int hook_mask;
950 nf_hookfn *hooks[NF_MAX_HOOKS];
4e25ceb8
FW
951 int (*ops_register)(struct net *net, const struct nf_hook_ops *ops);
952 void (*ops_unregister)(struct net *net, const struct nf_hook_ops *ops);
1a1e1a12
PM
953};
954
7210e4e3 955int nft_chain_validate_dependency(const struct nft_chain *chain,
32537e91 956 enum nft_chain_types type);
75e8d06d
PNA
957int nft_chain_validate_hooks(const struct nft_chain *chain,
958 unsigned int hook_flags);
7210e4e3 959
0ca743a5 960struct nft_stats {
ce355e20
ED
961 u64 bytes;
962 u64 pkts;
963 struct u64_stats_sync syncp;
0ca743a5
PNA
964};
965
96518518
PM
966/**
967 * struct nft_base_chain - nf_tables base chain
968 *
969 * @ops: netfilter hook ops
9370761c 970 * @type: chain type
0ca743a5
PNA
971 * @policy: default policy
972 * @stats: per-cpu chain stats
96518518 973 * @chain: the chain
2cbce139 974 * @dev_name: device name that this base chain is attached to (if any)
14bfb13f 975 * @flow_block: flow block (for hardware offload)
96518518
PM
976 */
977struct nft_base_chain {
c974a3a3 978 struct nf_hook_ops ops;
32537e91 979 const struct nft_chain_type *type;
0ca743a5 980 u8 policy;
835b8033 981 u8 flags;
0ca743a5 982 struct nft_stats __percpu *stats;
96518518 983 struct nft_chain chain;
2cbce139 984 char dev_name[IFNAMSIZ];
14bfb13f 985 struct flow_block flow_block;
96518518
PM
986};
987
988static inline struct nft_base_chain *nft_base_chain(const struct nft_chain *chain)
989{
990 return container_of(chain, struct nft_base_chain, chain);
991}
992
f323d954
PNA
993static inline bool nft_is_base_chain(const struct nft_chain *chain)
994{
995 return chain->flags & NFT_BASE_CHAIN;
996}
997
5ebe0b0e 998int __nft_release_basechain(struct nft_ctx *ctx);
835b8033 999
06198b34 1000unsigned int nft_do_chain(struct nft_pktinfo *pkt, void *priv);
96518518 1001
96518518
PM
1002/**
1003 * struct nft_table - nf_tables table
1004 *
1005 * @list: used internally
1b2470e5
FW
1006 * @chains_ht: chains in the table
1007 * @chains: same, for stable walks
96518518 1008 * @sets: sets in the table
e5009240 1009 * @objects: stateful objects in the table
3b49e2e9 1010 * @flowtables: flow tables in the table
96518518 1011 * @hgenerator: handle generator state
3ecbfd65 1012 * @handle: table handle
96518518
PM
1013 * @use: number of chain references to this table
1014 * @flags: table flag (see enum nft_table_flags)
f2a6d766 1015 * @genmask: generation mask
36596dad 1016 * @afinfo: address family info
96518518
PM
1017 * @name: name of the table
1018 */
1019struct nft_table {
1020 struct list_head list;
1b2470e5 1021 struct rhltable chains_ht;
96518518
PM
1022 struct list_head chains;
1023 struct list_head sets;
e5009240 1024 struct list_head objects;
3b49e2e9 1025 struct list_head flowtables;
96518518 1026 u64 hgenerator;
3ecbfd65 1027 u64 handle;
96518518 1028 u32 use;
98319cb9
PNA
1029 u16 family:6,
1030 flags:8,
f2a6d766 1031 genmask:2;
e46abbcc 1032 char *name;
ebddf1a8
PNA
1033};
1034
cc07eeb0 1035void nft_register_chain_type(const struct nft_chain_type *);
32537e91 1036void nft_unregister_chain_type(const struct nft_chain_type *);
96518518 1037
5eccdfaa
JP
1038int nft_register_expr(struct nft_expr_type *);
1039void nft_unregister_expr(struct nft_expr_type *);
96518518 1040
33d5a7b1
FW
1041int nft_verdict_dump(struct sk_buff *skb, int type,
1042 const struct nft_verdict *v);
1043
d152159b
FW
1044/**
1045 * struct nft_object_hash_key - key to lookup nft_object
1046 *
1047 * @name: name of the stateful object to look up
1048 * @table: table the object belongs to
1049 */
1050struct nft_object_hash_key {
1051 const char *name;
1052 const struct nft_table *table;
1053};
1054
e5009240
PNA
1055/**
1056 * struct nft_object - nf_tables stateful object
1057 *
1058 * @list: table stateful object list node
d152159b 1059 * @key: keys that identify this object
4d44175a 1060 * @rhlhead: nft_objname_ht node
e5009240
PNA
1061 * @genmask: generation mask
1062 * @use: number of references to this stateful object
3ecbfd65 1063 * @handle: unique object handle
dfc46034 1064 * @ops: object operations
4d44175a 1065 * @data: object data, layout depends on type
e5009240
PNA
1066 */
1067struct nft_object {
1068 struct list_head list;
4d44175a 1069 struct rhlist_head rhlhead;
d152159b 1070 struct nft_object_hash_key key;
e5009240
PNA
1071 u32 genmask:2,
1072 use:30;
3ecbfd65 1073 u64 handle;
e5009240 1074 /* runtime data below here */
dfc46034 1075 const struct nft_object_ops *ops ____cacheline_aligned;
e5009240
PNA
1076 unsigned char data[]
1077 __attribute__((aligned(__alignof__(u64))));
1078};
1079
1080static inline void *nft_obj_data(const struct nft_object *obj)
1081{
1082 return (void *)obj->data;
1083}
1084
1085#define nft_expr_obj(expr) *((struct nft_object **)nft_expr_priv(expr))
1086
4d44175a
FW
1087struct nft_object *nft_obj_lookup(const struct net *net,
1088 const struct nft_table *table,
cac20fcd
PNA
1089 const struct nlattr *nla, u32 objtype,
1090 u8 genmask);
e5009240 1091
d152159b 1092void nft_obj_notify(struct net *net, const struct nft_table *table,
25e94a99
PNA
1093 struct nft_object *obj, u32 portid, u32 seq,
1094 int event, int family, int report, gfp_t gfp);
2599e989 1095
e5009240
PNA
1096/**
1097 * struct nft_object_type - stateful object type
1098 *
dfc46034
PBG
1099 * @select_ops: function to select nft_object_ops
1100 * @ops: default ops, used when no select_ops functions is present
e5009240
PNA
1101 * @list: list node in list of object types
1102 * @type: stateful object numeric type
e5009240
PNA
1103 * @owner: module owner
1104 * @maxattr: maximum netlink attribute
1105 * @policy: netlink attribute policy
dfc46034
PBG
1106 */
1107struct nft_object_type {
1108 const struct nft_object_ops *(*select_ops)(const struct nft_ctx *,
1109 const struct nlattr * const tb[]);
1110 const struct nft_object_ops *ops;
1111 struct list_head list;
1112 u32 type;
1113 unsigned int maxattr;
1114 struct module *owner;
1115 const struct nla_policy *policy;
1116};
1117
1118/**
1119 * struct nft_object_ops - stateful object operations
1120 *
1121 * @eval: stateful object evaluation function
1122 * @size: stateful object size
e5009240
PNA
1123 * @init: initialize object from netlink attributes
1124 * @destroy: release existing stateful object
1125 * @dump: netlink dump stateful object
d62d0ba9 1126 * @update: update stateful object
e5009240 1127 */
dfc46034 1128struct nft_object_ops {
e5009240
PNA
1129 void (*eval)(struct nft_object *obj,
1130 struct nft_regs *regs,
1131 const struct nft_pktinfo *pkt);
e5009240 1132 unsigned int size;
84fba055
FW
1133 int (*init)(const struct nft_ctx *ctx,
1134 const struct nlattr *const tb[],
e5009240 1135 struct nft_object *obj);
00bfb320
PNA
1136 void (*destroy)(const struct nft_ctx *ctx,
1137 struct nft_object *obj);
e5009240 1138 int (*dump)(struct sk_buff *skb,
43da04a5
PNA
1139 struct nft_object *obj,
1140 bool reset);
d62d0ba9
FFM
1141 void (*update)(struct nft_object *obj,
1142 struct nft_object *newobj);
dfc46034 1143 const struct nft_object_type *type;
e5009240
PNA
1144};
1145
1146int nft_register_obj(struct nft_object_type *obj_type);
1147void nft_unregister_obj(struct nft_object_type *obj_type);
1148
d92191aa
PNA
1149#define NFT_FLOWTABLE_DEVICE_MAX 8
1150
3b49e2e9
PNA
1151/**
1152 * struct nft_flowtable - nf_tables flow table
1153 *
1154 * @list: flow table list node in table list
1155 * @table: the table the flow table is contained in
1156 * @name: name of this flow table
1157 * @hooknum: hook number
1158 * @priority: hook priority
1159 * @ops_len: number of hooks in array
1160 * @genmask: generation mask
1161 * @use: number of references to this flow table
3ecbfd65 1162 * @handle: unique object handle
d92191aa 1163 * @dev_name: array of device names
3b49e2e9
PNA
1164 * @data: rhashtable and garbage collector
1165 * @ops: array of hooks
1166 */
1167struct nft_flowtable {
1168 struct list_head list;
1169 struct nft_table *table;
1170 char *name;
1171 int hooknum;
1172 int priority;
1173 int ops_len;
1174 u32 genmask:2,
1175 use:30;
3ecbfd65 1176 u64 handle;
3b49e2e9
PNA
1177 /* runtime data below here */
1178 struct nf_hook_ops *ops ____cacheline_aligned;
1179 struct nf_flowtable data;
1180};
1181
cac20fcd
PNA
1182struct nft_flowtable *nft_flowtable_lookup(const struct nft_table *table,
1183 const struct nlattr *nla,
1184 u8 genmask);
3b49e2e9
PNA
1185
1186void nft_register_flowtable_type(struct nf_flowtable_type *type);
1187void nft_unregister_flowtable_type(struct nf_flowtable_type *type);
1188
33d5a7b1
FW
1189/**
1190 * struct nft_traceinfo - nft tracing information and state
1191 *
1192 * @pkt: pktinfo currently processed
1193 * @basechain: base chain currently processed
1194 * @chain: chain currently processed
1195 * @rule: rule that was evaluated
1196 * @verdict: verdict given by rule
1197 * @type: event type (enum nft_trace_types)
1198 * @packet_dumped: packet headers sent in a previous traceinfo message
1199 * @trace: other struct members are initialised
1200 */
1201struct nft_traceinfo {
1202 const struct nft_pktinfo *pkt;
1203 const struct nft_base_chain *basechain;
1204 const struct nft_chain *chain;
1205 const struct nft_rule *rule;
1206 const struct nft_verdict *verdict;
1207 enum nft_trace_types type;
1208 bool packet_dumped;
1209 bool trace;
1210};
1211
1212void nft_trace_init(struct nft_traceinfo *info, const struct nft_pktinfo *pkt,
1213 const struct nft_verdict *verdict,
1214 const struct nft_chain *basechain);
1215
1216void nft_trace_notify(struct nft_traceinfo *info);
1217
9370761c
PNA
1218#define MODULE_ALIAS_NFT_CHAIN(family, name) \
1219 MODULE_ALIAS("nft-chain-" __stringify(family) "-" name)
96518518 1220
64d46806
PM
1221#define MODULE_ALIAS_NFT_AF_EXPR(family, name) \
1222 MODULE_ALIAS("nft-expr-" __stringify(family) "-" name)
1223
96518518
PM
1224#define MODULE_ALIAS_NFT_EXPR(name) \
1225 MODULE_ALIAS("nft-expr-" name)
1226
20a69341
PM
1227#define MODULE_ALIAS_NFT_SET() \
1228 MODULE_ALIAS("nft-set")
1229
e5009240
PNA
1230#define MODULE_ALIAS_NFT_OBJ(type) \
1231 MODULE_ALIAS("nft-obj-" __stringify(type))
1232
47e640af
JS
1233#if IS_ENABLED(CONFIG_NF_TABLES)
1234
ea4bd995
PM
1235/*
1236 * The gencursor defines two generations, the currently active and the
1237 * next one. Objects contain a bitmask of 2 bits specifying the generations
1238 * they're active in. A set bit means they're inactive in the generation
1239 * represented by that bit.
1240 *
1241 * New objects start out as inactive in the current and active in the
1242 * next generation. When committing the ruleset the bitmask is cleared,
1243 * meaning they're active in all generations. When removing an object,
1244 * it is set inactive in the next generation. After committing the ruleset,
1245 * the objects are removed.
1246 */
1247static inline unsigned int nft_gencursor_next(const struct net *net)
1248{
1249 return net->nft.gencursor + 1 == 1 ? 1 : 0;
1250}
1251
1252static inline u8 nft_genmask_next(const struct net *net)
1253{
1254 return 1 << nft_gencursor_next(net);
1255}
1256
1257static inline u8 nft_genmask_cur(const struct net *net)
1258{
14cd5d4a
MR
1259 /* Use READ_ONCE() to prevent refetching the value for atomicity */
1260 return 1 << READ_ONCE(net->nft.gencursor);
ea4bd995
PM
1261}
1262
22fe54d5
PM
1263#define NFT_GENMASK_ANY ((1 << 0) | (1 << 1))
1264
889f7ee7
PNA
1265/*
1266 * Generic transaction helpers
1267 */
1268
1269/* Check if this object is currently active. */
1270#define nft_is_active(__net, __obj) \
1271 (((__obj)->genmask & nft_genmask_cur(__net)) == 0)
1272
1273/* Check if this object is active in the next generation. */
1274#define nft_is_active_next(__net, __obj) \
1275 (((__obj)->genmask & nft_genmask_next(__net)) == 0)
1276
1277/* This object becomes active in the next generation. */
1278#define nft_activate_next(__net, __obj) \
1279 (__obj)->genmask = nft_genmask_cur(__net)
1280
1281/* This object becomes inactive in the next generation. */
1282#define nft_deactivate_next(__net, __obj) \
1283 (__obj)->genmask = nft_genmask_next(__net)
1284
1285/* After committing the ruleset, clear the stale generation bit. */
1286#define nft_clear(__net, __obj) \
1287 (__obj)->genmask &= ~nft_genmask_next(__net)
f2a6d766
PNA
1288#define nft_active_genmask(__obj, __genmask) \
1289 !((__obj)->genmask & __genmask)
889f7ee7 1290
cc02e457
PM
1291/*
1292 * Set element transaction helpers
1293 */
1294
1295static inline bool nft_set_elem_active(const struct nft_set_ext *ext,
1296 u8 genmask)
1297{
1298 return !(ext->genmask & genmask);
1299}
1300
42a55769
PNA
1301static inline void nft_set_elem_change_active(const struct net *net,
1302 const struct nft_set *set,
cc02e457
PM
1303 struct nft_set_ext *ext)
1304{
42a55769 1305 ext->genmask ^= nft_genmask_next(net);
cc02e457
PM
1306}
1307
47e640af
JS
1308#endif /* IS_ENABLED(CONFIG_NF_TABLES) */
1309
69086658
PM
1310/*
1311 * We use a free bit in the genmask field to indicate the element
1312 * is busy, meaning it is currently being processed either by
1313 * the netlink API or GC.
1314 *
1315 * Even though the genmask is only a single byte wide, this works
1316 * because the extension structure if fully constant once initialized,
1317 * so there are no non-atomic write accesses unless it is already
1318 * marked busy.
1319 */
1320#define NFT_SET_ELEM_BUSY_MASK (1 << 2)
1321
1322#if defined(__LITTLE_ENDIAN_BITFIELD)
1323#define NFT_SET_ELEM_BUSY_BIT 2
1324#elif defined(__BIG_ENDIAN_BITFIELD)
1325#define NFT_SET_ELEM_BUSY_BIT (BITS_PER_LONG - BITS_PER_BYTE + 2)
1326#else
1327#error
1328#endif
1329
1330static inline int nft_set_elem_mark_busy(struct nft_set_ext *ext)
1331{
1332 unsigned long *word = (unsigned long *)ext;
1333
1334 BUILD_BUG_ON(offsetof(struct nft_set_ext, genmask) != 0);
1335 return test_and_set_bit(NFT_SET_ELEM_BUSY_BIT, word);
1336}
1337
1338static inline void nft_set_elem_clear_busy(struct nft_set_ext *ext)
1339{
1340 unsigned long *word = (unsigned long *)ext;
1341
1342 clear_bit(NFT_SET_ELEM_BUSY_BIT, word);
1343}
1344
1a1e1a12
PM
1345/**
1346 * struct nft_trans - nf_tables object update in transaction
1347 *
1348 * @list: used internally
1349 * @msg_type: message type
0935d558 1350 * @put_net: ctx->net needs to be put
1a1e1a12
PM
1351 * @ctx: transaction context
1352 * @data: internal information related to the transaction
1353 */
1354struct nft_trans {
1355 struct list_head list;
1356 int msg_type;
0935d558 1357 bool put_net;
1a1e1a12
PM
1358 struct nft_ctx ctx;
1359 char data[0];
1360};
1361
1362struct nft_trans_rule {
1363 struct nft_rule *rule;
c9626a2c 1364 struct nft_flow_rule *flow;
1a94e38d 1365 u32 rule_id;
1a1e1a12
PM
1366};
1367
1368#define nft_trans_rule(trans) \
1369 (((struct nft_trans_rule *)trans->data)->rule)
c9626a2c
PNA
1370#define nft_trans_flow_rule(trans) \
1371 (((struct nft_trans_rule *)trans->data)->flow)
1a94e38d
PNA
1372#define nft_trans_rule_id(trans) \
1373 (((struct nft_trans_rule *)trans->data)->rule_id)
1a1e1a12
PM
1374
1375struct nft_trans_set {
1376 struct nft_set *set;
1377 u32 set_id;
6a0a8d10 1378 bool bound;
1a1e1a12
PM
1379};
1380
1381#define nft_trans_set(trans) \
1382 (((struct nft_trans_set *)trans->data)->set)
1383#define nft_trans_set_id(trans) \
1384 (((struct nft_trans_set *)trans->data)->set_id)
6a0a8d10
PNA
1385#define nft_trans_set_bound(trans) \
1386 (((struct nft_trans_set *)trans->data)->bound)
1a1e1a12
PM
1387
1388struct nft_trans_chain {
1389 bool update;
b7263e07 1390 char *name;
1a1e1a12
PM
1391 struct nft_stats __percpu *stats;
1392 u8 policy;
1393};
1394
1395#define nft_trans_chain_update(trans) \
1396 (((struct nft_trans_chain *)trans->data)->update)
1397#define nft_trans_chain_name(trans) \
1398 (((struct nft_trans_chain *)trans->data)->name)
1399#define nft_trans_chain_stats(trans) \
1400 (((struct nft_trans_chain *)trans->data)->stats)
1401#define nft_trans_chain_policy(trans) \
1402 (((struct nft_trans_chain *)trans->data)->policy)
1403
1404struct nft_trans_table {
1405 bool update;
1406 bool enable;
1407};
1408
1409#define nft_trans_table_update(trans) \
1410 (((struct nft_trans_table *)trans->data)->update)
1411#define nft_trans_table_enable(trans) \
1412 (((struct nft_trans_table *)trans->data)->enable)
1413
1414struct nft_trans_elem {
1415 struct nft_set *set;
1416 struct nft_set_elem elem;
6a0a8d10 1417 bool bound;
1a1e1a12
PM
1418};
1419
1420#define nft_trans_elem_set(trans) \
1421 (((struct nft_trans_elem *)trans->data)->set)
1422#define nft_trans_elem(trans) \
1423 (((struct nft_trans_elem *)trans->data)->elem)
6a0a8d10
PNA
1424#define nft_trans_elem_set_bound(trans) \
1425 (((struct nft_trans_elem *)trans->data)->bound)
1a1e1a12 1426
e5009240
PNA
1427struct nft_trans_obj {
1428 struct nft_object *obj;
d62d0ba9
FFM
1429 struct nft_object *newobj;
1430 bool update;
e5009240
PNA
1431};
1432
1433#define nft_trans_obj(trans) \
1434 (((struct nft_trans_obj *)trans->data)->obj)
d62d0ba9
FFM
1435#define nft_trans_obj_newobj(trans) \
1436 (((struct nft_trans_obj *)trans->data)->newobj)
1437#define nft_trans_obj_update(trans) \
1438 (((struct nft_trans_obj *)trans->data)->update)
e5009240 1439
3b49e2e9
PNA
1440struct nft_trans_flowtable {
1441 struct nft_flowtable *flowtable;
1442};
1443
1444#define nft_trans_flowtable(trans) \
1445 (((struct nft_trans_flowtable *)trans->data)->flowtable)
1446
02c7b25e 1447int __init nft_chain_filter_init(void);
d209df3e 1448void nft_chain_filter_fini(void);
02c7b25e 1449
c1deb065
FW
1450void __init nft_chain_route_init(void);
1451void nft_chain_route_fini(void);
96518518 1452#endif /* _NET_NF_TABLES_H */