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