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