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