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