Linux 6.16-rc6
[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
5f60d5f6 5#include <linux/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>
d59d2f82 16#include <net/netns/generic.h>
96518518 17
d25e2e93
PNA
18#define NFT_MAX_HOOKS (NF_INET_INGRESS + 1)
19
a4cb98f3
PG
20struct module;
21
20a69341
PM
22#define NFT_JUMP_STACK_SIZE 16
23
b5bdc6f9
PNA
24enum {
25 NFT_PKTINFO_L4PROTO = (1 << 0),
c46b38dc 26 NFT_PKTINFO_INNER = (1 << 1),
0e795b37 27 NFT_PKTINFO_INNER_FULL = (1 << 2),
b5bdc6f9
PNA
28};
29
96518518
PM
30struct nft_pktinfo {
31 struct sk_buff *skb;
897389de 32 const struct nf_hook_state *state;
b5bdc6f9 33 u8 flags;
4566bf27 34 u8 tprot;
897389de 35 u16 fragoff;
e7a1caa6
FW
36 u16 thoff;
37 u16 inneroff;
96518518
PM
38};
39
85554eb9
FW
40static inline struct sock *nft_sk(const struct nft_pktinfo *pkt)
41{
897389de 42 return pkt->state->sk;
85554eb9
FW
43}
44
2d7b4ace
FW
45static inline unsigned int nft_thoff(const struct nft_pktinfo *pkt)
46{
897389de 47 return pkt->thoff;
2d7b4ace
FW
48}
49
0e5a1c7e
PNA
50static inline struct net *nft_net(const struct nft_pktinfo *pkt)
51{
897389de 52 return pkt->state->net;
0e5a1c7e
PNA
53}
54
55static inline unsigned int nft_hook(const struct nft_pktinfo *pkt)
56{
897389de 57 return pkt->state->hook;
0e5a1c7e
PNA
58}
59
60static inline u8 nft_pf(const struct nft_pktinfo *pkt)
61{
897389de 62 return pkt->state->pf;
0e5a1c7e
PNA
63}
64
65static inline const struct net_device *nft_in(const struct nft_pktinfo *pkt)
66{
897389de 67 return pkt->state->in;
0e5a1c7e
PNA
68}
69
70static inline const struct net_device *nft_out(const struct nft_pktinfo *pkt)
71{
897389de 72 return pkt->state->out;
0e5a1c7e
PNA
73}
74
0ca743a5 75static inline void nft_set_pktinfo(struct nft_pktinfo *pkt,
0ca743a5 76 struct sk_buff *skb,
073bfd56 77 const struct nf_hook_state *state)
0ca743a5
PNA
78{
79 pkt->skb = skb;
897389de 80 pkt->state = state;
0ca743a5
PNA
81}
82
f06ad944 83static inline void nft_set_pktinfo_unspec(struct nft_pktinfo *pkt)
beac5afa 84{
b5bdc6f9 85 pkt->flags = 0;
beac5afa 86 pkt->tprot = 0;
897389de
FW
87 pkt->thoff = 0;
88 pkt->fragoff = 0;
beac5afa
PNA
89}
90
a55e22e9
PM
91/**
92 * struct nft_verdict - nf_tables verdict
93 *
94 * @code: nf_tables/netfilter verdict code
95 * @chain: destination chain for NFT_JUMP/NFT_GOTO
96 */
97struct nft_verdict {
98 u32 code;
99 struct nft_chain *chain;
100};
101
96518518
PM
102struct nft_data {
103 union {
1ca2e170
PM
104 u32 data[4];
105 struct nft_verdict verdict;
96518518
PM
106 };
107} __attribute__((aligned(__alignof__(u64))));
108
642c8eff
PNA
109#define NFT_REG32_NUM 20
110
a55e22e9
PM
111/**
112 * struct nft_regs - nf_tables register set
113 *
114 * @data: data registers
115 * @verdict: verdict register
116 *
117 * The first four data registers alias to the verdict register.
118 */
119struct nft_regs {
120 union {
642c8eff 121 u32 data[NFT_REG32_NUM];
a55e22e9
PM
122 struct nft_verdict verdict;
123 };
124};
125
12e4ecfa
PNA
126struct nft_regs_track {
127 struct {
128 const struct nft_expr *selector;
129 const struct nft_expr *bitwise;
34cc9e52 130 u8 num_reg;
12e4ecfa
PNA
131 } regs[NFT_REG32_NUM];
132
133 const struct nft_expr *cur;
134 const struct nft_expr *last;
135};
136
a1b840ad 137/* Store/load an u8, u16 or u64 integer to/from the u32 data register.
10596608
LZ
138 *
139 * Note, when using concatenations, register allocation happens at 32-bit
140 * level. So for store instruction, pad the rest part with zero to avoid
141 * garbage values.
142 */
143
a1b840ad 144static inline void nft_reg_store8(u32 *dreg, u8 val)
10596608
LZ
145{
146 *dreg = 0;
a1b840ad 147 *(u8 *)dreg = val;
10596608
LZ
148}
149
7cd9a58d 150static inline u8 nft_reg_load8(const u32 *sreg)
a1b840ad
AJ
151{
152 return *(u8 *)sreg;
153}
154
155static inline void nft_reg_store16(u32 *dreg, u16 val)
10596608
LZ
156{
157 *dreg = 0;
a1b840ad 158 *(u16 *)dreg = val;
10596608
LZ
159}
160
7278b3c1
FW
161static inline void nft_reg_store_be16(u32 *dreg, __be16 val)
162{
163 nft_reg_store16(dreg, (__force __u16)val);
164}
165
7cd9a58d 166static inline u16 nft_reg_load16(const u32 *sreg)
10596608
LZ
167{
168 return *(u16 *)sreg;
169}
170
7278b3c1
FW
171static inline __be16 nft_reg_load_be16(const u32 *sreg)
172{
173 return (__force __be16)nft_reg_load16(sreg);
174}
175
176static inline __be32 nft_reg_load_be32(const u32 *sreg)
177{
178 return *(__force __be32 *)sreg;
179}
180
c301f098 181static inline void nft_reg_store64(u64 *dreg, u64 val)
10596608 182{
c301f098 183 put_unaligned(val, dreg);
a1b840ad
AJ
184}
185
7cd9a58d 186static inline u64 nft_reg_load64(const u32 *sreg)
a1b840ad
AJ
187{
188 return get_unaligned((u64 *)sreg);
10596608
LZ
189}
190
49499c3e
PM
191static inline void nft_data_copy(u32 *dst, const struct nft_data *src,
192 unsigned int len)
96518518 193{
1e105e6a
FW
194 if (len % NFT_REG32_SIZE)
195 dst[len / NFT_REG32_SIZE] = 0;
49499c3e 196 memcpy(dst, src, len);
96518518
PM
197}
198
96518518 199/**
20a69341 200 * struct nft_ctx - nf_tables rule/set context
96518518 201 *
99633ab2 202 * @net: net namespace
96518518
PM
203 * @table: the table the chain is contained in
204 * @chain: the chain the rule is contained in
0ca743a5 205 * @nla: netlink attributes
128ad332
PNA
206 * @portid: netlink portID of the original message
207 * @seq: netlink sequence number
b253d87f 208 * @flags: modifiers to new request
36596dad 209 * @family: protocol family
26b2f552 210 * @level: depth of the chains
128ad332 211 * @report: notify via unicast netlink message
c362646b 212 * @reg_inited: bitmap of initialised registers
96518518
PM
213 */
214struct nft_ctx {
99633ab2 215 struct net *net;
7c95f6d8
PNA
216 struct nft_table *table;
217 struct nft_chain *chain;
0ca743a5 218 const struct nlattr * const *nla;
128ad332
PNA
219 u32 portid;
220 u32 seq;
c9626a2c 221 u16 flags;
36596dad 222 u8 family;
26b2f552 223 u8 level;
128ad332 224 bool report;
14fb0713 225 DECLARE_BITMAP(reg_inited, NFT_REG32_NUM);
96518518
PM
226};
227
f323ef3a
PNA
228enum nft_data_desc_flags {
229 NFT_DATA_DESC_SETELEM = (1 << 0),
230};
231
96518518
PM
232struct nft_data_desc {
233 enum nft_data_types type;
341b6941 234 unsigned int size;
96518518 235 unsigned int len;
f323ef3a 236 unsigned int flags;
96518518
PM
237};
238
341b6941 239int nft_data_init(const struct nft_ctx *ctx, struct nft_data *data,
5eccdfaa 240 struct nft_data_desc *desc, const struct nlattr *nla);
bb7b40ae 241void nft_data_hold(const struct nft_data *data, enum nft_data_types type);
59105446 242void nft_data_release(const struct nft_data *data, enum nft_data_types type);
5eccdfaa
JP
243int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
244 enum nft_data_types type, unsigned int len);
96518518
PM
245
246static inline enum nft_data_types nft_dreg_to_type(enum nft_registers reg)
247{
248 return reg == NFT_REG_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE;
249}
250
20a69341
PM
251static inline enum nft_registers nft_type_to_reg(enum nft_data_types type)
252{
bf798657 253 return type == NFT_DATA_VERDICT ? NFT_REG_VERDICT : NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE;
20a69341
PM
254}
255
f1d505bb 256int nft_parse_u32_check(const struct nlattr *attr, int max, u32 *dest);
b1c96ed3
PM
257int nft_dump_register(struct sk_buff *skb, unsigned int attr, unsigned int reg);
258
7ea0522e
FW
259int nft_parse_register_load(const struct nft_ctx *ctx,
260 const struct nlattr *attr, u8 *sreg, u32 len);
345023b0
PNA
261int nft_parse_register_store(const struct nft_ctx *ctx,
262 const struct nlattr *attr, u8 *dreg,
263 const struct nft_data *data,
264 enum nft_data_types type, unsigned int len);
96518518 265
86f1ec32
PM
266/**
267 * struct nft_userdata - user defined data associated with an object
268 *
269 * @len: length of the data
270 * @data: content
271 *
272 * The presence of user data is indicated in an object specific fashion,
273 * so a length of zero can't occur and the value "len" indicates data
274 * of length len + 1.
275 */
276struct nft_userdata {
277 u8 len;
6daf1414 278 unsigned char data[];
86f1ec32
PM
279};
280
9dad402b
PNA
281/* placeholder structure for opaque set element backend representation. */
282struct nft_elem_priv { };
283
20a69341
PM
284/**
285 * struct nft_set_elem - generic representation of set elements
286 *
20a69341 287 * @key: element key
7b225d0b 288 * @key_end: closing element key
b253d87f 289 * @data: element data
fe2811eb 290 * @priv: element private data and extensions
20a69341
PM
291 */
292struct nft_set_elem {
7d740264
PM
293 union {
294 u32 buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)];
295 struct nft_data val;
296 } key;
7b225d0b
PNA
297 union {
298 u32 buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)];
299 struct nft_data val;
300 } key_end;
fdb9c405
PNA
301 union {
302 u32 buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)];
303 struct nft_data val;
304 } data;
9dad402b 305 struct nft_elem_priv *priv;
20a69341
PM
306};
307
9dad402b
PNA
308static inline void *nft_elem_priv_cast(const struct nft_elem_priv *priv)
309{
310 return (void *)priv;
311}
312
29b359cf
PNA
313
314/**
315 * enum nft_iter_type - nftables set iterator type
316 *
c362646b 317 * @NFT_ITER_UNSPEC: unspecified, to catch errors
29b359cf
PNA
318 * @NFT_ITER_READ: read-only iteration over set elements
319 * @NFT_ITER_UPDATE: iteration under mutex to update set element state
320 */
321enum nft_iter_type {
322 NFT_ITER_UNSPEC,
323 NFT_ITER_READ,
324 NFT_ITER_UPDATE,
325};
326
20a69341
PM
327struct nft_set;
328struct nft_set_iter {
8588ac09 329 u8 genmask;
29b359cf 330 enum nft_iter_type type:8;
20a69341
PM
331 unsigned int count;
332 unsigned int skip;
333 int err;
334 int (*fn)(const struct nft_ctx *ctx,
de70185d 335 struct nft_set *set,
20a69341 336 const struct nft_set_iter *iter,
0e1ea651 337 struct nft_elem_priv *elem_priv);
20a69341
PM
338};
339
c50b960c
PM
340/**
341 * struct nft_set_desc - description of set elements
342 *
bed4a63e 343 * @ktype: key type
c50b960c 344 * @klen: key length
bed4a63e 345 * @dtype: data type
c50b960c 346 * @dlen: data length
bed4a63e 347 * @objtype: object type
c50b960c 348 * @size: number of set elements
bed4a63e
PNA
349 * @policy: set policy
350 * @gc_int: garbage collector interval
b253d87f 351 * @timeout: element timeout
f3a2181e
SB
352 * @field_len: length of each field in concatenation, bytes
353 * @field_count: number of concatenated fields in element
d56aab26 354 * @expr: set must support for expressions
c50b960c
PM
355 */
356struct nft_set_desc {
bed4a63e 357 u32 ktype;
c50b960c 358 unsigned int klen;
bed4a63e 359 u32 dtype;
c50b960c 360 unsigned int dlen;
bed4a63e 361 u32 objtype;
c50b960c 362 unsigned int size;
bed4a63e
PNA
363 u32 policy;
364 u32 gc_int;
365 u64 timeout;
f3a2181e
SB
366 u8 field_len[NFT_REG32_COUNT];
367 u8 field_count;
d56aab26 368 bool expr;
c50b960c
PM
369};
370
371/**
372 * enum nft_set_class - performance class
373 *
b253d87f
GG
374 * @NFT_SET_CLASS_O_1: constant, O(1)
375 * @NFT_SET_CLASS_O_LOG_N: logarithmic, O(log N)
376 * @NFT_SET_CLASS_O_N: linear, O(N)
c50b960c
PM
377 */
378enum nft_set_class {
379 NFT_SET_CLASS_O_1,
380 NFT_SET_CLASS_O_LOG_N,
381 NFT_SET_CLASS_O_N,
382};
383
384/**
385 * struct nft_set_estimate - estimation of memory and performance
386 * characteristics
387 *
388 * @size: required memory
55af753c 389 * @lookup: lookup performance class
0b5a7874 390 * @space: memory class
c50b960c
PM
391 */
392struct nft_set_estimate {
4ef360dd 393 u64 size;
55af753c 394 enum nft_set_class lookup;
0b5a7874 395 enum nft_set_class space;
c50b960c
PM
396};
397
92b211a2
PNA
398#define NFT_EXPR_MAXATTR 16
399#define NFT_EXPR_SIZE(size) (sizeof(struct nft_expr) + \
400 ALIGN(size, __alignof__(struct nft_expr)))
401
402/**
403 * struct nft_expr - nf_tables expression
404 *
405 * @ops: expression ops
406 * @data: expression private data
407 */
408struct nft_expr {
409 const struct nft_expr_ops *ops;
410 unsigned char data[]
411 __attribute__((aligned(__alignof__(u64))));
412};
413
414static inline void *nft_expr_priv(const struct nft_expr *expr)
415{
416 return (void *)expr->data;
417}
418
3a07327d
PNA
419struct nft_expr_info;
420
421int nft_expr_inner_parse(const struct nft_ctx *ctx, const struct nlattr *nla,
422 struct nft_expr_info *info);
fa23e0d4 423int nft_expr_clone(struct nft_expr *dst, struct nft_expr *src, gfp_t gfp);
92b211a2
PNA
424void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr);
425int nft_expr_dump(struct sk_buff *skb, unsigned int attr,
8daa8fde 426 const struct nft_expr *expr, bool reset);
be5650f8
PNA
427bool nft_expr_reduce_bitwise(struct nft_regs_track *track,
428 const struct nft_expr *expr);
92b211a2 429
b2832dd6
PM
430struct nft_set_ext;
431
20a69341
PM
432/**
433 * struct nft_set_ops - nf_tables set operations
434 *
435 * @lookup: look up an element within the set
d0a8d877
AJ
436 * @update: update an element if exists, add it if doesn't exist
437 * @delete: delete an element
20a69341 438 * @insert: insert new element into set
cc02e457 439 * @activate: activate new element in the next generation
8411b644 440 * @deactivate: lookup for element and deactivate it in the next generation
1ba1c414 441 * @flush: deactivate element in the next generation
20a69341 442 * @remove: remove element from set
d0a8d877 443 * @walk: iterate over all set elements
ba0e4d99 444 * @get: get set elements
8d738c18
PNA
445 * @ksize: kernel set size
446 * @usize: userspace set size
447 * @adjust_maxsize: delta to adjust maximum set size
b253d87f
GG
448 * @commit: commit set elements
449 * @abort: abort set elements
20a69341 450 * @privsize: function to return size of set private data
b253d87f 451 * @estimate: estimate the required memory size and the lookup complexity class
20a69341
PM
452 * @init: initialize private data of new set instance
453 * @destroy: destroy private data of set instance
b253d87f 454 * @gc_init: initialize garbage collection
fe2811eb 455 * @elemsize: element private size
d0a8d877
AJ
456 *
457 * Operations lookup, update and delete have simpler interfaces, are faster
458 * and currently only used in the packet path. All the rest are slower,
459 * control plane functions.
20a69341
PM
460 */
461struct nft_set_ops {
42a55769
PNA
462 bool (*lookup)(const struct net *net,
463 const struct nft_set *set,
8cd8937a 464 const u32 *key,
b2832dd6 465 const struct nft_set_ext **ext);
22fe54d5 466 bool (*update)(struct nft_set *set,
8cd8937a 467 const u32 *key,
9dad402b
PNA
468 struct nft_elem_priv *
469 (*new)(struct nft_set *,
22fe54d5 470 const struct nft_expr *,
a55e22e9 471 struct nft_regs *),
22fe54d5 472 const struct nft_expr *expr,
a55e22e9 473 struct nft_regs *regs,
22fe54d5 474 const struct nft_set_ext **ext);
d0a8d877
AJ
475 bool (*delete)(const struct nft_set *set,
476 const u32 *key);
22fe54d5 477
42a55769
PNA
478 int (*insert)(const struct net *net,
479 const struct nft_set *set,
c016c7e4 480 const struct nft_set_elem *elem,
078996fc 481 struct nft_elem_priv **priv);
42a55769
PNA
482 void (*activate)(const struct net *net,
483 const struct nft_set *set,
0e1ea651 484 struct nft_elem_priv *elem_priv);
9dad402b 485 struct nft_elem_priv * (*deactivate)(const struct net *net,
42a55769 486 const struct nft_set *set,
cc02e457 487 const struct nft_set_elem *elem);
6509a2e4 488 void (*flush)(const struct net *net,
1ba1c414 489 const struct nft_set *set,
9dad402b 490 struct nft_elem_priv *priv);
5cb82a38
PNA
491 void (*remove)(const struct net *net,
492 const struct nft_set *set,
0e1ea651 493 struct nft_elem_priv *elem_priv);
20a69341 494 void (*walk)(const struct nft_ctx *ctx,
de70185d 495 struct nft_set *set,
20a69341 496 struct nft_set_iter *iter);
9dad402b 497 struct nft_elem_priv * (*get)(const struct net *net,
ba0e4d99
PNA
498 const struct nft_set *set,
499 const struct nft_set_elem *elem,
500 unsigned int flags);
8d738c18
PNA
501 u32 (*ksize)(u32 size);
502 u32 (*usize)(u32 size);
503 u32 (*adjust_maxsize)(const struct nft_set *set);
25600167 504 void (*commit)(struct nft_set *set);
212ed75d 505 void (*abort)(const struct nft_set *set);
4ef360dd 506 u64 (*privsize)(const struct nlattr * const nla[],
347b408d 507 const struct nft_set_desc *desc);
c50b960c
PM
508 bool (*estimate)(const struct nft_set_desc *desc,
509 u32 features,
510 struct nft_set_estimate *est);
20a69341 511 int (*init)(const struct nft_set *set,
c50b960c 512 const struct nft_set_desc *desc,
20a69341 513 const struct nlattr * const nla[]);
628bd3e4
PNA
514 void (*destroy)(const struct nft_ctx *ctx,
515 const struct nft_set *set);
79b174ad 516 void (*gc_init)(const struct nft_set *set);
20a69341 517
fe2811eb 518 unsigned int elemsize;
71cc0873
PS
519};
520
521/**
522 * struct nft_set_type - nf_tables set type
523 *
524 * @ops: set ops for this type
71cc0873
PS
525 * @features: features supported by the implementation
526 */
527struct nft_set_type {
528 const struct nft_set_ops ops;
20a69341
PM
529 u32 features;
530};
71cc0873 531#define to_set_type(o) container_of(o, struct nft_set_type, ops)
20a69341 532
563125a7
PNA
533struct nft_set_elem_expr {
534 u8 size;
535 unsigned char data[]
536 __attribute__((aligned(__alignof__(struct nft_expr))));
537};
538
539#define nft_setelem_expr_at(__elem_expr, __offset) \
540 ((struct nft_expr *)&__elem_expr->data[__offset])
541
542#define nft_setelem_expr_foreach(__expr, __elem_expr, __size) \
543 for (__expr = nft_setelem_expr_at(__elem_expr, 0), __size = 0; \
544 __size < (__elem_expr)->size; \
545 __size += (__expr)->ops->size, __expr = ((void *)(__expr)) + (__expr)->ops->size)
546
8cfd9b0f
PNA
547#define NFT_SET_EXPR_MAX 2
548
20a69341
PM
549/**
550 * struct nft_set - nf_tables set instance
551 *
552 * @list: table set list node
553 * @bindings: list of set bindings
5f68718b 554 * @refs: internal refcounting for async set destruction
3453c927
PNA
555 * @table: table this set belongs to
556 * @net: netnamespace this set belongs to
20a69341 557 * @name: name of the set
3ecbfd65 558 * @handle: unique handle of the set
20a69341
PM
559 * @ktype: key type (numeric type defined by userspace, not used in the kernel)
560 * @dtype: data type (verdict or numeric type defined by userspace)
8aeff920 561 * @objtype: object type (see NFT_OBJECT_* definitions)
c50b960c 562 * @size: maximum set size
f3a2181e
SB
563 * @field_len: length of each field in concatenation, bytes
564 * @field_count: number of concatenated fields in element
273fe3f1 565 * @use: number of rules references to this set
c50b960c 566 * @nelems: number of elements
3dd0673a 567 * @ndeact: number of deactivated elements queued for removal
d3e2a111 568 * @timeout: default timeout value in jiffies
761da293 569 * @gc_int: garbage collection interval in msecs
9363dc4b 570 * @policy: set parameterization (see enum nft_set_policies)
e6d8ecac
CFG
571 * @udlen: user data length
572 * @udata: user data
b253d87f 573 * @pending_update: list of pending update set element
20a69341
PM
574 * @ops: set ops
575 * @flags: set flags
08713cb0 576 * @dead: set will be freed, never cleared
37a9cc52 577 * @genmask: generation mask
20a69341
PM
578 * @klen: key length
579 * @dlen: data length
b253d87f
GG
580 * @num_exprs: numbers of exprs
581 * @exprs: stateful expression
582 * @catchall_list: list of catch-all set element
20a69341
PM
583 * @data: private set data
584 */
585struct nft_set {
586 struct list_head list;
587 struct list_head bindings;
5f68718b 588 refcount_t refs;
3453c927
PNA
589 struct nft_table *table;
590 possible_net_t net;
38745490 591 char *name;
3ecbfd65 592 u64 handle;
20a69341
PM
593 u32 ktype;
594 u32 dtype;
8aeff920 595 u32 objtype;
c50b960c 596 u32 size;
f3a2181e
SB
597 u8 field_len[NFT_REG32_COUNT];
598 u8 field_count;
273fe3f1 599 u32 use;
3dd0673a
PM
600 atomic_t nelems;
601 u32 ndeact;
761da293
PM
602 u64 timeout;
603 u32 gc_int;
9363dc4b 604 u16 policy;
e6d8ecac
CFG
605 u16 udlen;
606 unsigned char *udata;
212ed75d 607 struct list_head pending_update;
20a69341
PM
608 /* runtime data below here */
609 const struct nft_set_ops *ops ____cacheline_aligned;
5f68718b
PNA
610 u16 flags:13,
611 dead:1,
37a9cc52 612 genmask:2;
20a69341
PM
613 u8 klen;
614 u8 dlen;
8cfd9b0f
PNA
615 u8 num_exprs;
616 struct nft_expr *exprs[NFT_SET_EXPR_MAX];
aaa31047 617 struct list_head catchall_list;
20a69341
PM
618 unsigned char data[]
619 __attribute__((aligned(__alignof__(u64))));
620};
621
408070d6
PNA
622static inline bool nft_set_is_anonymous(const struct nft_set *set)
623{
624 return set->flags & NFT_SET_ANONYMOUS;
625}
626
20a69341
PM
627static inline void *nft_set_priv(const struct nft_set *set)
628{
629 return (void *)set->data;
630}
631
7931d329
PNA
632static inline enum nft_data_types nft_set_datatype(const struct nft_set *set)
633{
634 return set->dtype == NFT_DATA_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE;
635}
636
8e51830e
FW
637static inline bool nft_set_gc_is_pending(const struct nft_set *s)
638{
639 return refcount_read(&s->refs) != 1;
640}
641
9d098292
PM
642static inline struct nft_set *nft_set_container_of(const void *priv)
643{
644 return (void *)priv - offsetof(struct nft_set, data);
645}
646
10659cba
PNA
647struct nft_set *nft_set_lookup_global(const struct net *net,
648 const struct nft_table *table,
649 const struct nlattr *nla_set_name,
650 const struct nlattr *nla_set_id,
651 u8 genmask);
20a69341 652
aaa31047
PNA
653struct nft_set_ext *nft_set_catchall_lookup(const struct net *net,
654 const struct nft_set *set);
aaa31047 655
761da293
PM
656static inline unsigned long nft_set_gc_interval(const struct nft_set *set)
657{
123b9961
PNA
658 u32 gc_int = READ_ONCE(set->gc_int);
659
660 return gc_int ? msecs_to_jiffies(gc_int) : HZ;
761da293
PM
661}
662
20a69341
PM
663/**
664 * struct nft_set_binding - nf_tables set binding
665 *
666 * @list: set bindings list node
667 * @chain: chain containing the rule bound to the set
11113e19 668 * @flags: set action flags
20a69341
PM
669 *
670 * A set binding contains all information necessary for validation
671 * of new elements added to a bound set.
672 */
673struct nft_set_binding {
674 struct list_head list;
675 const struct nft_chain *chain;
11113e19 676 u32 flags;
20a69341
PM
677};
678
273fe3f1 679enum nft_trans_phase;
c1592a89 680void nf_tables_activate_set(const struct nft_ctx *ctx, struct nft_set *set);
273fe3f1
PNA
681void nf_tables_deactivate_set(const struct nft_ctx *ctx, struct nft_set *set,
682 struct nft_set_binding *binding,
683 enum nft_trans_phase phase);
5eccdfaa
JP
684int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
685 struct nft_set_binding *binding);
cd5125d8 686void nf_tables_destroy_set(const struct nft_ctx *ctx, struct nft_set *set);
20a69341 687
3ac4c07a
PM
688/**
689 * enum nft_set_extensions - set extension type IDs
690 *
691 * @NFT_SET_EXT_KEY: element key
7b225d0b 692 * @NFT_SET_EXT_KEY_END: upper bound element key, for ranges
3ac4c07a
PM
693 * @NFT_SET_EXT_DATA: mapping data
694 * @NFT_SET_EXT_FLAGS: element flags
c3e1b005 695 * @NFT_SET_EXT_TIMEOUT: element timeout
68e942e8 696 * @NFT_SET_EXT_USERDATA: user data associated with the element
85dfb34b 697 * @NFT_SET_EXT_EXPRESSIONS: expressions associated with the element
8aeff920 698 * @NFT_SET_EXT_OBJREF: stateful object reference associated with element
3ac4c07a
PM
699 * @NFT_SET_EXT_NUM: number of extension types
700 */
701enum nft_set_extensions {
702 NFT_SET_EXT_KEY,
7b225d0b 703 NFT_SET_EXT_KEY_END,
3ac4c07a
PM
704 NFT_SET_EXT_DATA,
705 NFT_SET_EXT_FLAGS,
c3e1b005 706 NFT_SET_EXT_TIMEOUT,
68e942e8 707 NFT_SET_EXT_USERDATA,
563125a7 708 NFT_SET_EXT_EXPRESSIONS,
8aeff920 709 NFT_SET_EXT_OBJREF,
3ac4c07a
PM
710 NFT_SET_EXT_NUM
711};
712
713/**
714 * struct nft_set_ext_type - set extension type
715 *
716 * @len: fixed part length of the extension
717 * @align: alignment requirements of the extension
718 */
719struct nft_set_ext_type {
720 u8 len;
721 u8 align;
722};
723
724extern const struct nft_set_ext_type nft_set_ext_types[];
725
726/**
727 * struct nft_set_ext_tmpl - set extension template
728 *
729 * @len: length of extension area
730 * @offset: offsets of individual extension types
b253d87f 731 * @ext_len: length of the expected extension(used to sanity check)
3ac4c07a
PM
732 */
733struct nft_set_ext_tmpl {
734 u16 len;
735 u8 offset[NFT_SET_EXT_NUM];
34aae2c2 736 u8 ext_len[NFT_SET_EXT_NUM];
3ac4c07a
PM
737};
738
739/**
740 * struct nft_set_ext - set extensions
741 *
542ed814 742 * @genmask: generation mask, but also flags (see NFT_SET_ELEM_DEAD_BIT)
3ac4c07a
PM
743 * @offset: offsets of individual extension types
744 * @data: beginning of extension data
542ed814
PNA
745 *
746 * This structure must be aligned to word size, otherwise atomic bitops
747 * on genmask field can cause alignment failure on some archs.
3ac4c07a
PM
748 */
749struct nft_set_ext {
cc02e457 750 u8 genmask;
3ac4c07a 751 u8 offset[NFT_SET_EXT_NUM];
6daf1414 752 char data[];
542ed814 753} __aligned(BITS_PER_LONG / 8);
3ac4c07a
PM
754
755static inline void nft_set_ext_prepare(struct nft_set_ext_tmpl *tmpl)
756{
757 memset(tmpl, 0, sizeof(*tmpl));
758 tmpl->len = sizeof(struct nft_set_ext);
759}
760
c39ba4de
PNA
761static inline int nft_set_ext_add_length(struct nft_set_ext_tmpl *tmpl, u8 id,
762 unsigned int len)
3ac4c07a
PM
763{
764 tmpl->len = ALIGN(tmpl->len, nft_set_ext_types[id].align);
c39ba4de
PNA
765 if (tmpl->len > U8_MAX)
766 return -EINVAL;
767
3ac4c07a 768 tmpl->offset[id] = tmpl->len;
34aae2c2
PNA
769 tmpl->ext_len[id] = nft_set_ext_types[id].len + len;
770 tmpl->len += tmpl->ext_len[id];
c39ba4de
PNA
771
772 return 0;
3ac4c07a
PM
773}
774
c39ba4de 775static inline int nft_set_ext_add(struct nft_set_ext_tmpl *tmpl, u8 id)
3ac4c07a 776{
c39ba4de 777 return nft_set_ext_add_length(tmpl, id, 0);
3ac4c07a
PM
778}
779
780static inline void nft_set_ext_init(struct nft_set_ext *ext,
781 const struct nft_set_ext_tmpl *tmpl)
782{
783 memcpy(ext->offset, tmpl->offset, sizeof(ext->offset));
784}
785
786static inline bool __nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
787{
788 return !!ext->offset[id];
789}
790
791static inline bool nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
792{
793 return ext && __nft_set_ext_exists(ext, id);
794}
795
796static inline void *nft_set_ext(const struct nft_set_ext *ext, u8 id)
797{
798 return (void *)ext + ext->offset[id];
799}
800
801static inline struct nft_data *nft_set_ext_key(const struct nft_set_ext *ext)
802{
803 return nft_set_ext(ext, NFT_SET_EXT_KEY);
804}
805
7b225d0b
PNA
806static inline struct nft_data *nft_set_ext_key_end(const struct nft_set_ext *ext)
807{
808 return nft_set_ext(ext, NFT_SET_EXT_KEY_END);
809}
810
3ac4c07a
PM
811static inline struct nft_data *nft_set_ext_data(const struct nft_set_ext *ext)
812{
813 return nft_set_ext(ext, NFT_SET_EXT_DATA);
814}
815
816static inline u8 *nft_set_ext_flags(const struct nft_set_ext *ext)
817{
818 return nft_set_ext(ext, NFT_SET_EXT_FLAGS);
819}
ef1f7df9 820
4c5daea9
PNA
821struct nft_timeout {
822 u64 timeout;
823 u64 expiration;
824};
c3e1b005 825
4c5daea9 826static inline struct nft_timeout *nft_set_ext_timeout(const struct nft_set_ext *ext)
c3e1b005 827{
4c5daea9 828 return nft_set_ext(ext, NFT_SET_EXT_TIMEOUT);
c3e1b005
PM
829}
830
68e942e8
PM
831static inline struct nft_userdata *nft_set_ext_userdata(const struct nft_set_ext *ext)
832{
833 return nft_set_ext(ext, NFT_SET_EXT_USERDATA);
834}
835
563125a7 836static inline struct nft_set_elem_expr *nft_set_ext_expr(const struct nft_set_ext *ext)
f25ad2e9 837{
563125a7 838 return nft_set_ext(ext, NFT_SET_EXT_EXPRESSIONS);
f25ad2e9
PM
839}
840
7395dfac
PNA
841static inline bool __nft_set_elem_expired(const struct nft_set_ext *ext,
842 u64 tstamp)
c3e1b005 843{
8bfb74ae 844 if (!nft_set_ext_exists(ext, NFT_SET_EXT_TIMEOUT) ||
4201f393 845 READ_ONCE(nft_set_ext_timeout(ext)->timeout) == 0)
8bfb74ae
PNA
846 return false;
847
848 return time_after_eq64(tstamp, READ_ONCE(nft_set_ext_timeout(ext)->expiration));
7395dfac
PNA
849}
850
851static inline bool nft_set_elem_expired(const struct nft_set_ext *ext)
852{
853 return __nft_set_elem_expired(ext, get_jiffies_64());
c3e1b005
PM
854}
855
fe2811eb 856static inline struct nft_set_ext *nft_set_elem_ext(const struct nft_set *set,
9dad402b 857 const struct nft_elem_priv *elem_priv)
fe2811eb 858{
9dad402b 859 return (void *)elem_priv + set->ops->elemsize;
fe2811eb
PM
860}
861
8aeff920
PNA
862static inline struct nft_object **nft_set_ext_obj(const struct nft_set_ext *ext)
863{
864 return nft_set_ext(ext, NFT_SET_EXT_OBJREF);
865}
866
a7fc9368
PNA
867struct nft_expr *nft_set_elem_expr_alloc(const struct nft_ctx *ctx,
868 const struct nft_set *set,
869 const struct nlattr *attr);
870
9dad402b
PNA
871struct nft_elem_priv *nft_set_elem_init(const struct nft_set *set,
872 const struct nft_set_ext_tmpl *tmpl,
873 const u32 *key, const u32 *key_end,
874 const u32 *data,
875 u64 timeout, u64 expiration, gfp_t gfp);
fca05d4d
PNA
876int nft_set_elem_expr_clone(const struct nft_ctx *ctx, struct nft_set *set,
877 struct nft_expr *expr_array[]);
9dad402b
PNA
878void nft_set_elem_destroy(const struct nft_set *set,
879 const struct nft_elem_priv *elem_priv,
61f9e292 880 bool destroy_expr);
628bd3e4 881void nf_tables_set_elem_destroy(const struct nft_ctx *ctx,
9dad402b
PNA
882 const struct nft_set *set,
883 const struct nft_elem_priv *elem_priv);
61edafbb 884
b8e20400 885struct nft_expr_ops;
96518518 886/**
ef1f7df9 887 * struct nft_expr_type - nf_tables expression type
96518518 888 *
ef1f7df9 889 * @select_ops: function to select nft_expr_ops
b8e20400 890 * @release_ops: release nft_expr_ops
ef1f7df9 891 * @ops: default ops, used when no select_ops functions is present
b253d87f 892 * @inner_ops: inner ops, used for inner packet operation
96518518
PM
893 * @list: used internally
894 * @name: Identifier
895 * @owner: module reference
896 * @policy: netlink attribute policy
897 * @maxattr: highest netlink attribute number
64d46806 898 * @family: address family for AF-specific types
151d799a 899 * @flags: expression type flags
ef1f7df9
PM
900 */
901struct nft_expr_type {
0ca743a5
PNA
902 const struct nft_expr_ops *(*select_ops)(const struct nft_ctx *,
903 const struct nlattr * const tb[]);
b8e20400 904 void (*release_ops)(const struct nft_expr_ops *ops);
ef1f7df9 905 const struct nft_expr_ops *ops;
3a07327d 906 const struct nft_expr_ops *inner_ops;
ef1f7df9
PM
907 struct list_head list;
908 const char *name;
909 struct module *owner;
910 const struct nla_policy *policy;
911 unsigned int maxattr;
64d46806 912 u8 family;
151d799a 913 u8 flags;
ef1f7df9
PM
914};
915
151d799a 916#define NFT_EXPR_STATEFUL 0x1
79b174ad 917#define NFT_EXPR_GC 0x2
151d799a 918
f6ac8585
PNA
919enum nft_trans_phase {
920 NFT_TRANS_PREPARE,
26b5a571 921 NFT_TRANS_PREPARE_ERROR,
f6ac8585
PNA
922 NFT_TRANS_ABORT,
923 NFT_TRANS_COMMIT,
924 NFT_TRANS_RELEASE
925};
926
c9626a2c
PNA
927struct nft_flow_rule;
928struct nft_offload_ctx;
929
ef1f7df9
PM
930/**
931 * struct nft_expr_ops - nf_tables expression operations
932 *
933 * @eval: Expression evaluation function
b253d87f 934 * @clone: Expression clone function
96518518 935 * @size: full expression size, including private data size
ef1f7df9 936 * @init: initialization function
cd5125d8
FW
937 * @activate: activate expression in the next generation
938 * @deactivate: deactivate expression in next generation
939 * @destroy: destruction function, called after synchronize_rcu
b253d87f 940 * @destroy_clone: destruction clone function
ef1f7df9 941 * @dump: function to dump parameters
0ca743a5 942 * @validate: validate expression, called during loop detection
b253d87f
GG
943 * @reduce: reduce expression
944 * @gc: garbage collection expression
945 * @offload: hardware offload expression
946 * @offload_action: function to report true/false to allocate one slot or not in the flow
947 * offload array
948 * @offload_stats: function to synchronize hardware stats via updating the counter expression
949 * @type: expression type
0ca743a5 950 * @data: extra data to attach to this expression operation
96518518 951 */
96518518
PM
952struct nft_expr_ops {
953 void (*eval)(const struct nft_expr *expr,
a55e22e9 954 struct nft_regs *regs,
96518518 955 const struct nft_pktinfo *pkt);
086f3321 956 int (*clone)(struct nft_expr *dst,
fa23e0d4 957 const struct nft_expr *src, gfp_t gfp);
ef1f7df9
PM
958 unsigned int size;
959
96518518
PM
960 int (*init)(const struct nft_ctx *ctx,
961 const struct nft_expr *expr,
962 const struct nlattr * const tb[]);
bb7b40ae
PNA
963 void (*activate)(const struct nft_ctx *ctx,
964 const struct nft_expr *expr);
965 void (*deactivate)(const struct nft_ctx *ctx,
f6ac8585
PNA
966 const struct nft_expr *expr,
967 enum nft_trans_phase phase);
62472bce
PM
968 void (*destroy)(const struct nft_ctx *ctx,
969 const struct nft_expr *expr);
371ebcbb
PNA
970 void (*destroy_clone)(const struct nft_ctx *ctx,
971 const struct nft_expr *expr);
96518518 972 int (*dump)(struct sk_buff *skb,
7d34aa3e
PS
973 const struct nft_expr *expr,
974 bool reset);
0ca743a5 975 int (*validate)(const struct nft_ctx *ctx,
eaf9b2c8 976 const struct nft_expr *expr);
12e4ecfa
PNA
977 bool (*reduce)(struct nft_regs_track *track,
978 const struct nft_expr *expr);
79b174ad
PNA
979 bool (*gc)(struct net *net,
980 const struct nft_expr *expr);
c9626a2c
PNA
981 int (*offload)(struct nft_offload_ctx *ctx,
982 struct nft_flow_rule *flow,
983 const struct nft_expr *expr);
b1a5983f 984 bool (*offload_action)(const struct nft_expr *expr);
b72920f6
PNA
985 void (*offload_stats)(struct nft_expr *expr,
986 const struct flow_stats *stats);
ef1f7df9 987 const struct nft_expr_type *type;
0ca743a5 988 void *data;
96518518
PM
989};
990
96518518
PM
991/**
992 * struct nft_rule - nf_tables rule
993 *
994 * @list: used internally
96518518 995 * @handle: rule handle
0628b123 996 * @genmask: generation mask
96518518 997 * @dlen: length of expression data
86f1ec32 998 * @udata: user data is appended to the rule
96518518
PM
999 * @data: expression data
1000 */
1001struct nft_rule {
1002 struct list_head list;
0768b3b3 1003 u64 handle:42,
0628b123 1004 genmask:2,
0768b3b3 1005 dlen:12,
86f1ec32 1006 udata:1;
96518518
PM
1007 unsigned char data[]
1008 __attribute__((aligned(__alignof__(struct nft_expr))));
1009};
1010
1011static inline struct nft_expr *nft_expr_first(const struct nft_rule *rule)
1012{
1013 return (struct nft_expr *)&rule->data[0];
1014}
1015
1016static inline struct nft_expr *nft_expr_next(const struct nft_expr *expr)
1017{
1018 return ((void *)expr) + expr->ops->size;
1019}
1020
1021static inline struct nft_expr *nft_expr_last(const struct nft_rule *rule)
1022{
1023 return (struct nft_expr *)&rule->data[rule->dlen];
1024}
1025
31cc578a
SM
1026static inline bool nft_expr_more(const struct nft_rule *rule,
1027 const struct nft_expr *expr)
1028{
1029 return expr != nft_expr_last(rule) && expr->ops;
1030}
1031
86f1ec32 1032static inline struct nft_userdata *nft_userdata(const struct nft_rule *rule)
0768b3b3
PNA
1033{
1034 return (void *)&rule->data[rule->dlen];
1035}
1036
4bedf9ee
PNA
1037void nft_rule_expr_activate(const struct nft_ctx *ctx, struct nft_rule *rule);
1038void nft_rule_expr_deactivate(const struct nft_ctx *ctx, struct nft_rule *rule,
1039 enum nft_trans_phase phase);
1040void nf_tables_rule_destroy(const struct nft_ctx *ctx, struct nft_rule *rule);
d0e2c7de 1041
76adfafe
PNA
1042static inline void nft_set_elem_update_expr(const struct nft_set_ext *ext,
1043 struct nft_regs *regs,
1044 const struct nft_pktinfo *pkt)
1045{
563125a7 1046 struct nft_set_elem_expr *elem_expr;
76adfafe 1047 struct nft_expr *expr;
563125a7
PNA
1048 u32 size;
1049
1050 if (__nft_set_ext_exists(ext, NFT_SET_EXT_EXPRESSIONS)) {
1051 elem_expr = nft_set_ext_expr(ext);
1052 nft_setelem_expr_foreach(expr, elem_expr, size) {
1053 expr->ops->eval(expr, regs, pkt);
1054 if (regs->verdict.code == NFT_BREAK)
1055 return;
1056 }
76adfafe
PNA
1057 }
1058}
1059
96518518
PM
1060/*
1061 * The last pointer isn't really necessary, but the compiler isn't able to
1062 * determine that the result of nft_expr_last() is always the same since it
1063 * can't assume that the dlen value wasn't changed within calls in the loop.
1064 */
1065#define nft_rule_for_each_expr(expr, last, rule) \
1066 for ((expr) = nft_expr_first(rule), (last) = nft_expr_last(rule); \
1067 (expr) != (last); \
1068 (expr) = nft_expr_next(expr))
1069
ad652f38
PNA
1070#define NFT_CHAIN_POLICY_UNSET U8_MAX
1071
2c865a8a
PNA
1072struct nft_rule_dp {
1073 u64 is_last:1,
1074 dlen:12,
1075 handle:42; /* for tracing */
1076 unsigned char data[]
1077 __attribute__((aligned(__alignof__(struct nft_expr))));
1078};
1079
63e9bbbc
FW
1080struct nft_rule_dp_last {
1081 struct nft_rule_dp end; /* end of nft_rule_blob marker */
1082 struct rcu_head h; /* call_rcu head */
1083 struct nft_rule_blob *blob; /* ptr to free via call_rcu */
1084 const struct nft_chain *chain; /* for nftables tracing */
1085};
1086
1087static inline const struct nft_rule_dp *nft_rule_next(const struct nft_rule_dp *rule)
1088{
1089 return (void *)rule + sizeof(*rule) + rule->dlen;
1090}
1091
2c865a8a
PNA
1092struct nft_rule_blob {
1093 unsigned long size;
1094 unsigned char data[]
1095 __attribute__((aligned(__alignof__(struct nft_rule_dp))));
1096};
1097
96518518
PM
1098/**
1099 * struct nft_chain - nf_tables chain
1100 *
b253d87f
GG
1101 * @blob_gen_0: rule blob pointer to the current generation
1102 * @blob_gen_1: rule blob pointer to the future generation
96518518
PM
1103 * @rules: list of rules in the chain
1104 * @list: used internally
1b2470e5 1105 * @rhlhead: used internally
b5bc89bf 1106 * @table: table that this chain belongs to
96518518 1107 * @handle: chain handle
96518518 1108 * @use: number of jump references to this chain
b253d87f
GG
1109 * @flags: bitmask of enum NFTA_CHAIN_FLAGS
1110 * @bound: bind or not
1111 * @genmask: generation mask
96518518 1112 * @name: name of the chain
b253d87f
GG
1113 * @udlen: user data length
1114 * @udata: user data in the chain
1115 * @blob_next: rule blob pointer to the next in the chain
96518518
PM
1116 */
1117struct nft_chain {
2c865a8a
PNA
1118 struct nft_rule_blob __rcu *blob_gen_0;
1119 struct nft_rule_blob __rcu *blob_gen_1;
96518518
PM
1120 struct list_head rules;
1121 struct list_head list;
1b2470e5 1122 struct rhlist_head rhlhead;
b5bc89bf 1123 struct nft_table *table;
96518518 1124 u64 handle;
a0a7379e 1125 u32 use;
d0e2c7de
PNA
1126 u8 flags:5,
1127 bound:1,
664b0f8c 1128 genmask:2;
b7263e07 1129 char *name;
002f2176
JGG
1130 u16 udlen;
1131 u8 *udata;
0cbc06b3
FW
1132
1133 /* Only used during control plane commit phase: */
2c865a8a 1134 struct nft_rule_blob *blob_next;
96518518
PM
1135};
1136
a654de8f 1137int nft_chain_validate(const struct nft_ctx *ctx, const struct nft_chain *chain);
d46fc894
PNA
1138int nft_setelem_validate(const struct nft_ctx *ctx, struct nft_set *set,
1139 const struct nft_set_iter *iter,
0e1ea651 1140 struct nft_elem_priv *elem_priv);
d46fc894 1141int nft_set_catchall_validate(const struct nft_ctx *ctx, struct nft_set *set);
4bedf9ee 1142int nf_tables_bind_chain(const struct nft_ctx *ctx, struct nft_chain *chain);
26b5a571 1143void nf_tables_unbind_chain(const struct nft_ctx *ctx, struct nft_chain *chain);
a654de8f 1144
465b9ee0
PS
1145struct nft_hook;
1146void nf_tables_chain_device_notify(const struct nft_chain *chain,
1147 const struct nft_hook *hook,
1148 const struct net_device *dev, int event);
1149
32537e91 1150enum nft_chain_types {
9370761c
PNA
1151 NFT_CHAIN_T_DEFAULT = 0,
1152 NFT_CHAIN_T_ROUTE,
1153 NFT_CHAIN_T_NAT,
1154 NFT_CHAIN_T_MAX
1155};
1156
1a1e1a12 1157/**
32537e91 1158 * struct nft_chain_type - nf_tables chain type info
1a1e1a12
PM
1159 *
1160 * @name: name of the type
1161 * @type: numeric identifier
1162 * @family: address family
1163 * @owner: module owner
1164 * @hook_mask: mask of valid hooks
c2f9eafe 1165 * @hooks: array of hook functions
4e25ceb8
FW
1166 * @ops_register: base chain register function
1167 * @ops_unregister: base chain unregister function
1a1e1a12 1168 */
32537e91 1169struct nft_chain_type {
1a1e1a12 1170 const char *name;
32537e91 1171 enum nft_chain_types type;
1a1e1a12
PM
1172 int family;
1173 struct module *owner;
1174 unsigned int hook_mask;
d25e2e93 1175 nf_hookfn *hooks[NFT_MAX_HOOKS];
4e25ceb8
FW
1176 int (*ops_register)(struct net *net, const struct nf_hook_ops *ops);
1177 void (*ops_unregister)(struct net *net, const struct nf_hook_ops *ops);
1a1e1a12
PM
1178};
1179
7210e4e3 1180int nft_chain_validate_dependency(const struct nft_chain *chain,
32537e91 1181 enum nft_chain_types type);
75e8d06d
PNA
1182int nft_chain_validate_hooks(const struct nft_chain *chain,
1183 unsigned int hook_flags);
7210e4e3 1184
4bedf9ee
PNA
1185static inline bool nft_chain_binding(const struct nft_chain *chain)
1186{
1187 return chain->flags & NFT_CHAIN_BINDING;
1188}
1189
d0e2c7de
PNA
1190static inline bool nft_chain_is_bound(struct nft_chain *chain)
1191{
1192 return (chain->flags & NFT_CHAIN_BINDING) && chain->bound;
1193}
1194
4bedf9ee 1195int nft_chain_add(struct nft_table *table, struct nft_chain *chain);
d0e2c7de 1196void nft_chain_del(struct nft_chain *chain);
8965d42b 1197void nf_tables_chain_destroy(struct nft_chain *chain);
d0e2c7de 1198
0ca743a5 1199struct nft_stats {
ce355e20
ED
1200 u64 bytes;
1201 u64 pkts;
1202 struct u64_stats_sync syncp;
0ca743a5
PNA
1203};
1204
3f0465a9
PNA
1205struct nft_hook {
1206 struct list_head list;
73319a8e 1207 struct list_head ops_list;
3f0465a9 1208 struct rcu_head rcu;
b7c2d793
PS
1209 char ifname[IFNAMSIZ];
1210 u8 ifnamelen;
3f0465a9
PNA
1211};
1212
e225376d
PS
1213struct nf_hook_ops *nft_hook_find_ops(const struct nft_hook *hook,
1214 const struct net_device *dev);
1215struct nf_hook_ops *nft_hook_find_ops_rcu(const struct nft_hook *hook,
1216 const struct net_device *dev);
1217
96518518
PM
1218/**
1219 * struct nft_base_chain - nf_tables base chain
1220 *
1221 * @ops: netfilter hook ops
d54725cd 1222 * @hook_list: list of netfilter hooks (for NFPROTO_NETDEV family)
9370761c 1223 * @type: chain type
0ca743a5 1224 * @policy: default policy
b253d87f 1225 * @flags: indicate the base chain disabled or not
0ca743a5 1226 * @stats: per-cpu chain stats
96518518 1227 * @chain: the chain
14bfb13f 1228 * @flow_block: flow block (for hardware offload)
96518518
PM
1229 */
1230struct nft_base_chain {
c974a3a3 1231 struct nf_hook_ops ops;
d54725cd 1232 struct list_head hook_list;
32537e91 1233 const struct nft_chain_type *type;
0ca743a5 1234 u8 policy;
835b8033 1235 u8 flags;
0ca743a5 1236 struct nft_stats __percpu *stats;
96518518 1237 struct nft_chain chain;
14bfb13f 1238 struct flow_block flow_block;
96518518
PM
1239};
1240
1241static inline struct nft_base_chain *nft_base_chain(const struct nft_chain *chain)
1242{
1243 return container_of(chain, struct nft_base_chain, chain);
1244}
1245
f323d954
PNA
1246static inline bool nft_is_base_chain(const struct nft_chain *chain)
1247{
67c49de4 1248 return chain->flags & NFT_CHAIN_BASE;
f323d954
PNA
1249}
1250
06198b34 1251unsigned int nft_do_chain(struct nft_pktinfo *pkt, void *priv);
96518518 1252
1689f259
PNA
1253static inline bool nft_use_inc(u32 *use)
1254{
1255 if (*use == UINT_MAX)
1256 return false;
1257
1258 (*use)++;
1259
1260 return true;
1261}
1262
1263static inline void nft_use_dec(u32 *use)
1264{
1265 WARN_ON_ONCE((*use)-- == 0);
1266}
1267
1268/* For error and abort path: restore use counter to previous state. */
1269static inline void nft_use_inc_restore(u32 *use)
1270{
1271 WARN_ON_ONCE(!nft_use_inc(use));
1272}
1273
1274#define nft_use_dec_restore nft_use_dec
1275
96518518
PM
1276/**
1277 * struct nft_table - nf_tables table
1278 *
1279 * @list: used internally
1b2470e5
FW
1280 * @chains_ht: chains in the table
1281 * @chains: same, for stable walks
96518518 1282 * @sets: sets in the table
e5009240 1283 * @objects: stateful objects in the table
3b49e2e9 1284 * @flowtables: flow tables in the table
96518518 1285 * @hgenerator: handle generator state
3ecbfd65 1286 * @handle: table handle
96518518 1287 * @use: number of chain references to this table
94ecde83 1288 * @family:address family
96518518 1289 * @flags: table flag (see enum nft_table_flags)
f2a6d766 1290 * @genmask: generation mask
94ecde83 1291 * @nlpid: netlink port ID
96518518 1292 * @name: name of the table
94ecde83
GG
1293 * @udlen: length of the user data
1294 * @udata: user data
00c320f9 1295 * @validate_state: internal, set when transaction adds jumps
96518518
PM
1296 */
1297struct nft_table {
1298 struct list_head list;
1b2470e5 1299 struct rhltable chains_ht;
96518518
PM
1300 struct list_head chains;
1301 struct list_head sets;
e5009240 1302 struct list_head objects;
3b49e2e9 1303 struct list_head flowtables;
96518518 1304 u64 hgenerator;
3ecbfd65 1305 u64 handle;
96518518 1306 u32 use;
98319cb9
PNA
1307 u16 family:6,
1308 flags:8,
f2a6d766 1309 genmask:2;
6001a930 1310 u32 nlpid;
e46abbcc 1311 char *name;
7a81575b
JGG
1312 u16 udlen;
1313 u8 *udata;
00c320f9 1314 u8 validate_state;
ebddf1a8
PNA
1315};
1316
6001a930
PNA
1317static inline bool nft_table_has_owner(const struct nft_table *table)
1318{
1319 return table->flags & NFT_TABLE_F_OWNER;
1320}
1321
31bf508b
PS
1322static inline bool nft_table_is_orphan(const struct nft_table *table)
1323{
1324 return (table->flags & (NFT_TABLE_F_OWNER | NFT_TABLE_F_PERSIST)) ==
1325 NFT_TABLE_F_PERSIST;
1326}
1327
d3519cb8
PNA
1328static inline bool nft_base_chain_netdev(int family, u32 hooknum)
1329{
1330 return family == NFPROTO_NETDEV ||
1331 (family == NFPROTO_INET && hooknum == NF_INET_INGRESS);
1332}
1333
cc07eeb0 1334void nft_register_chain_type(const struct nft_chain_type *);
32537e91 1335void nft_unregister_chain_type(const struct nft_chain_type *);
96518518 1336
5eccdfaa
JP
1337int nft_register_expr(struct nft_expr_type *);
1338void nft_unregister_expr(struct nft_expr_type *);
96518518 1339
33d5a7b1
FW
1340int nft_verdict_dump(struct sk_buff *skb, int type,
1341 const struct nft_verdict *v);
1342
d152159b
FW
1343/**
1344 * struct nft_object_hash_key - key to lookup nft_object
1345 *
1346 * @name: name of the stateful object to look up
1347 * @table: table the object belongs to
1348 */
1349struct nft_object_hash_key {
1350 const char *name;
1351 const struct nft_table *table;
1352};
1353
e5009240
PNA
1354/**
1355 * struct nft_object - nf_tables stateful object
1356 *
1357 * @list: table stateful object list node
4d44175a 1358 * @rhlhead: nft_objname_ht node
b253d87f 1359 * @key: keys that identify this object
e5009240
PNA
1360 * @genmask: generation mask
1361 * @use: number of references to this stateful object
3ecbfd65 1362 * @handle: unique object handle
b253d87f
GG
1363 * @udlen: length of user data
1364 * @udata: user data
dfc46034 1365 * @ops: object operations
4d44175a 1366 * @data: object data, layout depends on type
e5009240
PNA
1367 */
1368struct nft_object {
1369 struct list_head list;
4d44175a 1370 struct rhlist_head rhlhead;
d152159b 1371 struct nft_object_hash_key key;
1689f259
PNA
1372 u32 genmask:2;
1373 u32 use;
3ecbfd65 1374 u64 handle;
b131c964
JGG
1375 u16 udlen;
1376 u8 *udata;
e5009240 1377 /* runtime data below here */
dfc46034 1378 const struct nft_object_ops *ops ____cacheline_aligned;
e5009240
PNA
1379 unsigned char data[]
1380 __attribute__((aligned(__alignof__(u64))));
1381};
1382
1383static inline void *nft_obj_data(const struct nft_object *obj)
1384{
1385 return (void *)obj->data;
1386}
1387
1388#define nft_expr_obj(expr) *((struct nft_object **)nft_expr_priv(expr))
1389
4d44175a
FW
1390struct nft_object *nft_obj_lookup(const struct net *net,
1391 const struct nft_table *table,
cac20fcd
PNA
1392 const struct nlattr *nla, u32 objtype,
1393 u8 genmask);
e5009240 1394
d152159b 1395void nft_obj_notify(struct net *net, const struct nft_table *table,
25e94a99 1396 struct nft_object *obj, u32 portid, u32 seq,
6fb721cf 1397 int event, u16 flags, int family, int report, gfp_t gfp);
2599e989 1398
e5009240
PNA
1399/**
1400 * struct nft_object_type - stateful object type
1401 *
dfc46034
PBG
1402 * @select_ops: function to select nft_object_ops
1403 * @ops: default ops, used when no select_ops functions is present
e5009240
PNA
1404 * @list: list node in list of object types
1405 * @type: stateful object numeric type
e5009240
PNA
1406 * @owner: module owner
1407 * @maxattr: maximum netlink attribute
776d4516 1408 * @family: address family for AF-specific object types
e5009240 1409 * @policy: netlink attribute policy
dfc46034
PBG
1410 */
1411struct nft_object_type {
1412 const struct nft_object_ops *(*select_ops)(const struct nft_ctx *,
1413 const struct nlattr * const tb[]);
1414 const struct nft_object_ops *ops;
1415 struct list_head list;
1416 u32 type;
1417 unsigned int maxattr;
776d4516 1418 u8 family;
dfc46034
PBG
1419 struct module *owner;
1420 const struct nla_policy *policy;
1421};
1422
1423/**
1424 * struct nft_object_ops - stateful object operations
1425 *
1426 * @eval: stateful object evaluation function
1427 * @size: stateful object size
e5009240
PNA
1428 * @init: initialize object from netlink attributes
1429 * @destroy: release existing stateful object
1430 * @dump: netlink dump stateful object
d62d0ba9 1431 * @update: update stateful object
b253d87f 1432 * @type: pointer to object type
e5009240 1433 */
dfc46034 1434struct nft_object_ops {
e5009240
PNA
1435 void (*eval)(struct nft_object *obj,
1436 struct nft_regs *regs,
1437 const struct nft_pktinfo *pkt);
e5009240 1438 unsigned int size;
84fba055
FW
1439 int (*init)(const struct nft_ctx *ctx,
1440 const struct nlattr *const tb[],
e5009240 1441 struct nft_object *obj);
00bfb320
PNA
1442 void (*destroy)(const struct nft_ctx *ctx,
1443 struct nft_object *obj);
e5009240 1444 int (*dump)(struct sk_buff *skb,
43da04a5
PNA
1445 struct nft_object *obj,
1446 bool reset);
d62d0ba9
FFM
1447 void (*update)(struct nft_object *obj,
1448 struct nft_object *newobj);
dfc46034 1449 const struct nft_object_type *type;
e5009240
PNA
1450};
1451
1452int nft_register_obj(struct nft_object_type *obj_type);
1453void nft_unregister_obj(struct nft_object_type *obj_type);
1454
cb662ac6 1455#define NFT_NETDEVICE_MAX 256
d92191aa 1456
3b49e2e9
PNA
1457/**
1458 * struct nft_flowtable - nf_tables flow table
1459 *
1460 * @list: flow table list node in table list
1461 * @table: the table the flow table is contained in
1462 * @name: name of this flow table
1463 * @hooknum: hook number
3b49e2e9
PNA
1464 * @ops_len: number of hooks in array
1465 * @genmask: generation mask
1466 * @use: number of references to this flow table
3ecbfd65 1467 * @handle: unique object handle
b253d87f 1468 * @hook_list: hook list for hooks per net_device in flowtables
3b49e2e9 1469 * @data: rhashtable and garbage collector
3b49e2e9
PNA
1470 */
1471struct nft_flowtable {
1472 struct list_head list;
1473 struct nft_table *table;
1474 char *name;
1475 int hooknum;
3b49e2e9 1476 int ops_len;
1689f259
PNA
1477 u32 genmask:2;
1478 u32 use;
3ecbfd65 1479 u64 handle;
3b49e2e9 1480 /* runtime data below here */
3f0465a9 1481 struct list_head hook_list ____cacheline_aligned;
3b49e2e9
PNA
1482 struct nf_flowtable data;
1483};
1484
b3e8f29d
FW
1485struct nft_flowtable *nft_flowtable_lookup(const struct net *net,
1486 const struct nft_table *table,
cac20fcd
PNA
1487 const struct nlattr *nla,
1488 u8 genmask);
3b49e2e9 1489
9b05b6e1
LGL
1490void nf_tables_deactivate_flowtable(const struct nft_ctx *ctx,
1491 struct nft_flowtable *flowtable,
1492 enum nft_trans_phase phase);
1493
3b49e2e9
PNA
1494void nft_register_flowtable_type(struct nf_flowtable_type *type);
1495void nft_unregister_flowtable_type(struct nf_flowtable_type *type);
1496
33d5a7b1
FW
1497/**
1498 * struct nft_traceinfo - nft tracing information and state
1499 *
e34b9ed9
FW
1500 * @trace: other struct members are initialised
1501 * @nf_trace: copy of skb->nf_trace before rule evaluation
1502 * @type: event type (enum nft_trace_types)
1503 * @skbid: hash of skb to be used as trace id
1504 * @packet_dumped: packet headers sent in a previous traceinfo message
33d5a7b1 1505 * @basechain: base chain currently processed
33d5a7b1
FW
1506 */
1507struct nft_traceinfo {
e34b9ed9
FW
1508 bool trace;
1509 bool nf_trace;
1510 bool packet_dumped;
1511 enum nft_trace_types type:8;
1512 u32 skbid;
33d5a7b1 1513 const struct nft_base_chain *basechain;
33d5a7b1
FW
1514};
1515
1516void nft_trace_init(struct nft_traceinfo *info, const struct nft_pktinfo *pkt,
33d5a7b1
FW
1517 const struct nft_chain *basechain);
1518
698bb828 1519void nft_trace_notify(const struct nft_pktinfo *pkt,
0a202145 1520 const struct nft_verdict *verdict,
46df4175 1521 const struct nft_rule_dp *rule,
698bb828 1522 struct nft_traceinfo *info);
33d5a7b1 1523
9370761c
PNA
1524#define MODULE_ALIAS_NFT_CHAIN(family, name) \
1525 MODULE_ALIAS("nft-chain-" __stringify(family) "-" name)
96518518 1526
64d46806
PM
1527#define MODULE_ALIAS_NFT_AF_EXPR(family, name) \
1528 MODULE_ALIAS("nft-expr-" __stringify(family) "-" name)
1529
96518518
PM
1530#define MODULE_ALIAS_NFT_EXPR(name) \
1531 MODULE_ALIAS("nft-expr-" name)
1532
e5009240
PNA
1533#define MODULE_ALIAS_NFT_OBJ(type) \
1534 MODULE_ALIAS("nft-obj-" __stringify(type))
1535
47e640af
JS
1536#if IS_ENABLED(CONFIG_NF_TABLES)
1537
ea4bd995
PM
1538/*
1539 * The gencursor defines two generations, the currently active and the
1540 * next one. Objects contain a bitmask of 2 bits specifying the generations
1541 * they're active in. A set bit means they're inactive in the generation
1542 * represented by that bit.
1543 *
1544 * New objects start out as inactive in the current and active in the
1545 * next generation. When committing the ruleset the bitmask is cleared,
1546 * meaning they're active in all generations. When removing an object,
1547 * it is set inactive in the next generation. After committing the ruleset,
1548 * the objects are removed.
1549 */
1550static inline unsigned int nft_gencursor_next(const struct net *net)
1551{
1552 return net->nft.gencursor + 1 == 1 ? 1 : 0;
1553}
1554
1555static inline u8 nft_genmask_next(const struct net *net)
1556{
1557 return 1 << nft_gencursor_next(net);
1558}
1559
1560static inline u8 nft_genmask_cur(const struct net *net)
1561{
14cd5d4a
MR
1562 /* Use READ_ONCE() to prevent refetching the value for atomicity */
1563 return 1 << READ_ONCE(net->nft.gencursor);
ea4bd995
PM
1564}
1565
22fe54d5
PM
1566#define NFT_GENMASK_ANY ((1 << 0) | (1 << 1))
1567
889f7ee7
PNA
1568/*
1569 * Generic transaction helpers
1570 */
1571
1572/* Check if this object is currently active. */
1573#define nft_is_active(__net, __obj) \
1574 (((__obj)->genmask & nft_genmask_cur(__net)) == 0)
1575
1576/* Check if this object is active in the next generation. */
1577#define nft_is_active_next(__net, __obj) \
1578 (((__obj)->genmask & nft_genmask_next(__net)) == 0)
1579
1580/* This object becomes active in the next generation. */
1581#define nft_activate_next(__net, __obj) \
1582 (__obj)->genmask = nft_genmask_cur(__net)
1583
1584/* This object becomes inactive in the next generation. */
1585#define nft_deactivate_next(__net, __obj) \
1586 (__obj)->genmask = nft_genmask_next(__net)
1587
1588/* After committing the ruleset, clear the stale generation bit. */
1589#define nft_clear(__net, __obj) \
1590 (__obj)->genmask &= ~nft_genmask_next(__net)
f2a6d766
PNA
1591#define nft_active_genmask(__obj, __genmask) \
1592 !((__obj)->genmask & __genmask)
889f7ee7 1593
cc02e457
PM
1594/*
1595 * Set element transaction helpers
1596 */
1597
1598static inline bool nft_set_elem_active(const struct nft_set_ext *ext,
1599 u8 genmask)
1600{
1601 return !(ext->genmask & genmask);
1602}
1603
42a55769
PNA
1604static inline void nft_set_elem_change_active(const struct net *net,
1605 const struct nft_set *set,
cc02e457
PM
1606 struct nft_set_ext *ext)
1607{
42a55769 1608 ext->genmask ^= nft_genmask_next(net);
cc02e457
PM
1609}
1610
47e640af
JS
1611#endif /* IS_ENABLED(CONFIG_NF_TABLES) */
1612
a2dd0233 1613#define NFT_SET_ELEM_DEAD_MASK (1 << 2)
5f68718b
PNA
1614
1615#if defined(__LITTLE_ENDIAN_BITFIELD)
a2dd0233 1616#define NFT_SET_ELEM_DEAD_BIT 2
5f68718b 1617#elif defined(__BIG_ENDIAN_BITFIELD)
a2dd0233 1618#define NFT_SET_ELEM_DEAD_BIT (BITS_PER_LONG - BITS_PER_BYTE + 2)
5f68718b
PNA
1619#else
1620#error
1621#endif
1622
1623static inline void nft_set_elem_dead(struct nft_set_ext *ext)
1624{
1625 unsigned long *word = (unsigned long *)ext;
1626
1627 BUILD_BUG_ON(offsetof(struct nft_set_ext, genmask) != 0);
1628 set_bit(NFT_SET_ELEM_DEAD_BIT, word);
1629}
1630
1631static inline int nft_set_elem_is_dead(const struct nft_set_ext *ext)
1632{
1633 unsigned long *word = (unsigned long *)ext;
1634
1635 BUILD_BUG_ON(offsetof(struct nft_set_ext, genmask) != 0);
1636 return test_bit(NFT_SET_ELEM_DEAD_BIT, word);
1637}
1638
1a1e1a12 1639/**
605efd54 1640 * struct nft_trans - nf_tables object update in transaction
1a1e1a12 1641 *
605efd54 1642 * @list: used internally
e169285f
FW
1643 * @net: struct net
1644 * @table: struct nft_table the object resides in
605efd54 1645 * @msg_type: message type
e169285f
FW
1646 * @seq: netlink sequence number
1647 * @flags: modifiers to new request
1648 * @report: notify via unicast netlink message
1649 * @put_net: net needs to be put
605efd54
FW
1650 *
1651 * This is the information common to all objects in the transaction,
1652 * this must always be the first member of derived sub-types.
1a1e1a12
PM
1653 */
1654struct nft_trans {
1655 struct list_head list;
e169285f
FW
1656 struct net *net;
1657 struct nft_table *table;
1a1e1a12 1658 int msg_type;
e169285f
FW
1659 u32 seq;
1660 u16 flags;
1661 u8 report:1;
1662 u8 put_net:1;
1a1e1a12
PM
1663};
1664
17d8f3ad
FW
1665/**
1666 * struct nft_trans_binding - nf_tables object with binding support in transaction
1667 * @nft_trans: base structure, MUST be first member
1668 * @binding_list: list of objects with possible bindings
1669 *
1670 * This is the base type used by objects that can be bound to a chain.
1671 */
1672struct nft_trans_binding {
1673 struct nft_trans nft_trans;
1674 struct list_head binding_list;
1a1e1a12
PM
1675};
1676
1677struct nft_trans_rule {
605efd54 1678 struct nft_trans nft_trans;
1a1e1a12 1679 struct nft_rule *rule;
13f20bc9 1680 struct nft_chain *chain;
c9626a2c 1681 struct nft_flow_rule *flow;
1a94e38d 1682 u32 rule_id;
4bedf9ee 1683 bool bound;
1a1e1a12
PM
1684};
1685
605efd54
FW
1686#define nft_trans_container_rule(trans) \
1687 container_of(trans, struct nft_trans_rule, nft_trans)
1688#define nft_trans_rule(trans) \
1689 nft_trans_container_rule(trans)->rule
1690#define nft_trans_flow_rule(trans) \
1691 nft_trans_container_rule(trans)->flow
1692#define nft_trans_rule_id(trans) \
1693 nft_trans_container_rule(trans)->rule_id
1694#define nft_trans_rule_bound(trans) \
1695 nft_trans_container_rule(trans)->bound
13f20bc9
FW
1696#define nft_trans_rule_chain(trans) \
1697 nft_trans_container_rule(trans)->chain
1a1e1a12
PM
1698
1699struct nft_trans_set {
17d8f3ad 1700 struct nft_trans_binding nft_trans_binding;
c1aa3886 1701 struct list_head list_trans_newset;
1a1e1a12
PM
1702 struct nft_set *set;
1703 u32 set_id;
123b9961
PNA
1704 u32 gc_int;
1705 u64 timeout;
1706 bool update;
6a0a8d10 1707 bool bound;
96b2ef9b 1708 u32 size;
1a1e1a12
PM
1709};
1710
17d8f3ad
FW
1711#define nft_trans_container_set(t) \
1712 container_of(t, struct nft_trans_set, nft_trans_binding.nft_trans)
605efd54
FW
1713#define nft_trans_set(trans) \
1714 nft_trans_container_set(trans)->set
1715#define nft_trans_set_id(trans) \
1716 nft_trans_container_set(trans)->set_id
1717#define nft_trans_set_bound(trans) \
1718 nft_trans_container_set(trans)->bound
1719#define nft_trans_set_update(trans) \
1720 nft_trans_container_set(trans)->update
1721#define nft_trans_set_timeout(trans) \
1722 nft_trans_container_set(trans)->timeout
1723#define nft_trans_set_gc_int(trans) \
1724 nft_trans_container_set(trans)->gc_int
1725#define nft_trans_set_size(trans) \
1726 nft_trans_container_set(trans)->size
1a1e1a12
PM
1727
1728struct nft_trans_chain {
17d8f3ad 1729 struct nft_trans_binding nft_trans_binding;
4bedf9ee 1730 struct nft_chain *chain;
b7263e07 1731 char *name;
1a1e1a12
PM
1732 struct nft_stats __percpu *stats;
1733 u8 policy;
b3f4c216 1734 bool update;
4bedf9ee 1735 bool bound;
74cccc3d 1736 u32 chain_id;
b9703ed4
PNA
1737 struct nft_base_chain *basechain;
1738 struct list_head hook_list;
1a1e1a12
PM
1739};
1740
17d8f3ad
FW
1741#define nft_trans_container_chain(t) \
1742 container_of(t, struct nft_trans_chain, nft_trans_binding.nft_trans)
605efd54
FW
1743#define nft_trans_chain(trans) \
1744 nft_trans_container_chain(trans)->chain
1745#define nft_trans_chain_update(trans) \
1746 nft_trans_container_chain(trans)->update
1747#define nft_trans_chain_name(trans) \
1748 nft_trans_container_chain(trans)->name
1749#define nft_trans_chain_stats(trans) \
1750 nft_trans_container_chain(trans)->stats
1751#define nft_trans_chain_policy(trans) \
1752 nft_trans_container_chain(trans)->policy
1753#define nft_trans_chain_bound(trans) \
1754 nft_trans_container_chain(trans)->bound
1755#define nft_trans_chain_id(trans) \
1756 nft_trans_container_chain(trans)->chain_id
1757#define nft_trans_basechain(trans) \
1758 nft_trans_container_chain(trans)->basechain
1759#define nft_trans_chain_hooks(trans) \
1760 nft_trans_container_chain(trans)->hook_list
1a1e1a12
PM
1761
1762struct nft_trans_table {
605efd54 1763 struct nft_trans nft_trans;
1a1e1a12 1764 bool update;
1a1e1a12
PM
1765};
1766
605efd54
FW
1767#define nft_trans_container_table(trans) \
1768 container_of(trans, struct nft_trans_table, nft_trans)
1769#define nft_trans_table_update(trans) \
1770 nft_trans_container_table(trans)->update
1a1e1a12 1771
4201f393
PNA
1772enum nft_trans_elem_flags {
1773 NFT_TRANS_UPD_TIMEOUT = (1 << 0),
1774 NFT_TRANS_UPD_EXPIRATION = (1 << 1),
1775};
1776
50818085 1777struct nft_elem_update {
4201f393
PNA
1778 u64 timeout;
1779 u64 expiration;
50818085
FW
1780 u8 flags;
1781};
1782
1783struct nft_trans_one_elem {
1784 struct nft_elem_priv *priv;
1785 struct nft_elem_update *update;
a8ee6b90
FW
1786};
1787
1788struct nft_trans_elem {
1789 struct nft_trans nft_trans;
1790 struct nft_set *set;
6a0a8d10 1791 bool bound;
a8ee6b90
FW
1792 unsigned int nelems;
1793 struct nft_trans_one_elem elems[] __counted_by(nelems);
1a1e1a12
PM
1794};
1795
605efd54
FW
1796#define nft_trans_container_elem(t) \
1797 container_of(t, struct nft_trans_elem, nft_trans)
1798#define nft_trans_elem_set(trans) \
1799 nft_trans_container_elem(trans)->set
605efd54
FW
1800#define nft_trans_elem_set_bound(trans) \
1801 nft_trans_container_elem(trans)->bound
1a1e1a12 1802
e5009240 1803struct nft_trans_obj {
605efd54 1804 struct nft_trans nft_trans;
e5009240 1805 struct nft_object *obj;
d62d0ba9
FFM
1806 struct nft_object *newobj;
1807 bool update;
e5009240
PNA
1808};
1809
605efd54
FW
1810#define nft_trans_container_obj(t) \
1811 container_of(t, struct nft_trans_obj, nft_trans)
1812#define nft_trans_obj(trans) \
1813 nft_trans_container_obj(trans)->obj
1814#define nft_trans_obj_newobj(trans) \
1815 nft_trans_container_obj(trans)->newobj
1816#define nft_trans_obj_update(trans) \
1817 nft_trans_container_obj(trans)->update
e5009240 1818
3b49e2e9 1819struct nft_trans_flowtable {
605efd54 1820 struct nft_trans nft_trans;
3b49e2e9 1821 struct nft_flowtable *flowtable;
78d9f48f 1822 struct list_head hook_list;
7b35582c 1823 u32 flags;
b3f4c216 1824 bool update;
3b49e2e9
PNA
1825};
1826
605efd54
FW
1827#define nft_trans_container_flowtable(t) \
1828 container_of(t, struct nft_trans_flowtable, nft_trans)
1829#define nft_trans_flowtable(trans) \
1830 nft_trans_container_flowtable(trans)->flowtable
1831#define nft_trans_flowtable_update(trans) \
1832 nft_trans_container_flowtable(trans)->update
1833#define nft_trans_flowtable_hooks(trans) \
1834 nft_trans_container_flowtable(trans)->hook_list
1835#define nft_trans_flowtable_flags(trans) \
1836 nft_trans_container_flowtable(trans)->flags
3b49e2e9 1837
5f68718b
PNA
1838#define NFT_TRANS_GC_BATCHCOUNT 256
1839
1840struct nft_trans_gc {
1841 struct list_head list;
1842 struct net *net;
1843 struct nft_set *set;
1844 u32 seq;
cf5000a7 1845 u16 count;
0e1ea651 1846 struct nft_elem_priv *priv[NFT_TRANS_GC_BATCHCOUNT];
5f68718b
PNA
1847 struct rcu_head rcu;
1848};
1849
e169285f
FW
1850static inline void nft_ctx_update(struct nft_ctx *ctx,
1851 const struct nft_trans *trans)
1852{
1853 switch (trans->msg_type) {
1854 case NFT_MSG_NEWRULE:
1855 case NFT_MSG_DELRULE:
1856 case NFT_MSG_DESTROYRULE:
1857 ctx->chain = nft_trans_rule_chain(trans);
1858 break;
1859 case NFT_MSG_NEWCHAIN:
1860 case NFT_MSG_DELCHAIN:
1861 case NFT_MSG_DESTROYCHAIN:
1862 ctx->chain = nft_trans_chain(trans);
1863 break;
1864 default:
1865 ctx->chain = NULL;
1866 break;
1867 }
1868
1869 ctx->net = trans->net;
1870 ctx->table = trans->table;
1871 ctx->family = trans->table->family;
1872 ctx->report = trans->report;
1873 ctx->flags = trans->flags;
1874 ctx->seq = trans->seq;
1875}
1876
5f68718b
PNA
1877struct nft_trans_gc *nft_trans_gc_alloc(struct nft_set *set,
1878 unsigned int gc_seq, gfp_t gfp);
1879void nft_trans_gc_destroy(struct nft_trans_gc *trans);
1880
1881struct nft_trans_gc *nft_trans_gc_queue_async(struct nft_trans_gc *gc,
1882 unsigned int gc_seq, gfp_t gfp);
1883void nft_trans_gc_queue_async_done(struct nft_trans_gc *gc);
1884
1885struct nft_trans_gc *nft_trans_gc_queue_sync(struct nft_trans_gc *gc, gfp_t gfp);
1886void nft_trans_gc_queue_sync_done(struct nft_trans_gc *trans);
1887
1888void nft_trans_gc_elem_add(struct nft_trans_gc *gc, void *priv);
1889
4a9e12ea
PNA
1890struct nft_trans_gc *nft_trans_gc_catchall_async(struct nft_trans_gc *gc,
1891 unsigned int gc_seq);
1892struct nft_trans_gc *nft_trans_gc_catchall_sync(struct nft_trans_gc *gc);
5f68718b
PNA
1893
1894void nft_setelem_data_deactivate(const struct net *net,
1895 const struct nft_set *set,
0e1ea651 1896 struct nft_elem_priv *elem_priv);
5f68718b 1897
02c7b25e 1898int __init nft_chain_filter_init(void);
d209df3e 1899void nft_chain_filter_fini(void);
02c7b25e 1900
c1deb065
FW
1901void __init nft_chain_route_init(void);
1902void nft_chain_route_fini(void);
ffe8923f 1903
fb828656 1904void nf_tables_trans_destroy_flush_work(struct net *net);
917d80d3
PNA
1905
1906int nf_msecs_to_jiffies64(const struct nlattr *nla, u64 *result);
1907__be64 nf_jiffies64_to_msecs(u64 input);
1908
cefa31a9
FW
1909#ifdef CONFIG_MODULES
1910__printf(2, 3) int nft_request_module(struct net *net, const char *fmt, ...);
1911#else
1912static inline int nft_request_module(struct net *net, const char *fmt, ...) { return -ENOENT; }
1913#endif
0854db2a
FW
1914
1915struct nftables_pernet {
1916 struct list_head tables;
1917 struct list_head commit_list;
fb828656 1918 struct list_head destroy_list;
c1aa3886 1919 struct list_head commit_set_list;
938154b9 1920 struct list_head binding_list;
0854db2a
FW
1921 struct list_head module_list;
1922 struct list_head notify_list;
1923 struct mutex commit_mutex;
ab482c6b 1924 u64 table_handle;
7395dfac 1925 u64 tstamp;
0854db2a 1926 unsigned int base_seq;
5f68718b 1927 unsigned int gc_seq;
4b80ced9 1928 u8 validate_state;
fb828656 1929 struct work_struct destroy_work;
0854db2a
FW
1930};
1931
d59d2f82
PNA
1932extern unsigned int nf_tables_net_id;
1933
1934static inline struct nftables_pernet *nft_pernet(const struct net *net)
1935{
1936 return net_generic(net, nf_tables_net_id);
1937}
1938
7395dfac
PNA
1939static inline u64 nft_net_tstamp(const struct net *net)
1940{
1941 return nft_pernet(net)->tstamp;
1942}
1943
b2d30654
PNA
1944#define __NFT_REDUCE_READONLY 1UL
1945#define NFT_REDUCE_READONLY (void *)__NFT_REDUCE_READONLY
1946
1947static inline bool nft_reduce_is_readonly(const struct nft_expr *expr)
1948{
1949 return expr->ops->reduce == NFT_REDUCE_READONLY;
1950}
1951
34cc9e52
PNA
1952void nft_reg_track_update(struct nft_regs_track *track,
1953 const struct nft_expr *expr, u8 dreg, u8 len);
1954void nft_reg_track_cancel(struct nft_regs_track *track, u8 dreg, u8 len);
1955void __nft_reg_track_cancel(struct nft_regs_track *track, u8 dreg);
1956
1957static inline bool nft_reg_track_cmp(struct nft_regs_track *track,
1958 const struct nft_expr *expr, u8 dreg)
1959{
1960 return track->regs[dreg].selector &&
1961 track->regs[dreg].selector->ops == expr->ops &&
1962 track->regs[dreg].num_reg == 0;
1963}
1964
96518518 1965#endif /* _NET_NF_TABLES_H */