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