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

source code of linux/include/net/netfilter/nf_tables.h