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