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