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