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