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