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