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