• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _NET_NF_TABLES_H
3 #define _NET_NF_TABLES_H
4 
5 #include <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(u64 * dreg,u64 val)180 static inline void nft_reg_store64(u64 *dreg, u64 val)
181 {
182 	put_unaligned(val, 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 	void				(*commit)(const struct nft_set *set);
461 	void				(*abort)(const struct nft_set *set);
462 	u64				(*privsize)(const struct nlattr * const nla[],
463 						    const struct nft_set_desc *desc);
464 	bool				(*estimate)(const struct nft_set_desc *desc,
465 						    u32 features,
466 						    struct nft_set_estimate *est);
467 	int				(*init)(const struct nft_set *set,
468 						const struct nft_set_desc *desc,
469 						const struct nlattr * const nla[]);
470 	void				(*destroy)(const struct nft_ctx *ctx,
471 						   const struct nft_set *set);
472 	void				(*gc_init)(const struct nft_set *set);
473 
474 	unsigned int			elemsize;
475 };
476 
477 /**
478  *      struct nft_set_type - nf_tables set type
479  *
480  *      @ops: set ops for this type
481  *      @features: features supported by the implementation
482  */
483 struct nft_set_type {
484 	const struct nft_set_ops	ops;
485 	u32				features;
486 };
487 #define to_set_type(o) container_of(o, struct nft_set_type, ops)
488 
489 struct nft_set_elem_expr {
490 	u8				size;
491 	unsigned char			data[]
492 		__attribute__((aligned(__alignof__(struct nft_expr))));
493 };
494 
495 #define nft_setelem_expr_at(__elem_expr, __offset)			\
496 	((struct nft_expr *)&__elem_expr->data[__offset])
497 
498 #define nft_setelem_expr_foreach(__expr, __elem_expr, __size)		\
499 	for (__expr = nft_setelem_expr_at(__elem_expr, 0), __size = 0;	\
500 	     __size < (__elem_expr)->size;				\
501 	     __size += (__expr)->ops->size, __expr = ((void *)(__expr)) + (__expr)->ops->size)
502 
503 #define NFT_SET_EXPR_MAX	2
504 
505 /**
506  * 	struct nft_set - nf_tables set instance
507  *
508  *	@list: table set list node
509  *	@bindings: list of set bindings
510  *	@refs: internal refcounting for async set destruction
511  *	@table: table this set belongs to
512  *	@net: netnamespace this set belongs to
513  * 	@name: name of the set
514  *	@handle: unique handle of the set
515  * 	@ktype: key type (numeric type defined by userspace, not used in the kernel)
516  * 	@dtype: data type (verdict or numeric type defined by userspace)
517  * 	@objtype: object type (see NFT_OBJECT_* definitions)
518  * 	@size: maximum set size
519  *	@field_len: length of each field in concatenation, bytes
520  *	@field_count: number of concatenated fields in element
521  *	@use: number of rules references to this set
522  * 	@nelems: number of elements
523  * 	@ndeact: number of deactivated elements queued for removal
524  *	@timeout: default timeout value in jiffies
525  * 	@gc_int: garbage collection interval in msecs
526  *	@policy: set parameterization (see enum nft_set_policies)
527  *	@udlen: user data length
528  *	@udata: user data
529  *	@expr: stateful expression
530  * 	@ops: set ops
531  * 	@flags: set flags
532  *	@dead: set will be freed, never cleared
533  *	@genmask: generation mask
534  * 	@klen: key length
535  * 	@dlen: data length
536  * 	@data: private set data
537  */
538 struct nft_set {
539 	struct list_head		list;
540 	struct list_head		bindings;
541 	refcount_t			refs;
542 	struct nft_table		*table;
543 	possible_net_t			net;
544 	char				*name;
545 	u64				handle;
546 	u32				ktype;
547 	u32				dtype;
548 	u32				objtype;
549 	u32				size;
550 	u8				field_len[NFT_REG32_COUNT];
551 	u8				field_count;
552 	u32				use;
553 	atomic_t			nelems;
554 	u32				ndeact;
555 	u64				timeout;
556 	u32				gc_int;
557 	u16				policy;
558 	u16				udlen;
559 	unsigned char			*udata;
560 	struct list_head		pending_update;
561 	/* runtime data below here */
562 	const struct nft_set_ops	*ops ____cacheline_aligned;
563 	u16				flags:13,
564 					dead:1,
565 					genmask:2;
566 	u8				klen;
567 	u8				dlen;
568 	u8				num_exprs;
569 	struct nft_expr			*exprs[NFT_SET_EXPR_MAX];
570 	struct list_head		catchall_list;
571 	unsigned char			data[]
572 		__attribute__((aligned(__alignof__(u64))));
573 };
574 
nft_set_is_anonymous(const struct nft_set * set)575 static inline bool nft_set_is_anonymous(const struct nft_set *set)
576 {
577 	return set->flags & NFT_SET_ANONYMOUS;
578 }
579 
nft_set_priv(const struct nft_set * set)580 static inline void *nft_set_priv(const struct nft_set *set)
581 {
582 	return (void *)set->data;
583 }
584 
nft_set_gc_is_pending(const struct nft_set * s)585 static inline bool nft_set_gc_is_pending(const struct nft_set *s)
586 {
587 	return refcount_read(&s->refs) != 1;
588 }
589 
nft_set_container_of(const void * priv)590 static inline struct nft_set *nft_set_container_of(const void *priv)
591 {
592 	return (void *)priv - offsetof(struct nft_set, data);
593 }
594 
595 struct nft_set *nft_set_lookup_global(const struct net *net,
596 				      const struct nft_table *table,
597 				      const struct nlattr *nla_set_name,
598 				      const struct nlattr *nla_set_id,
599 				      u8 genmask);
600 
601 struct nft_set_ext *nft_set_catchall_lookup(const struct net *net,
602 					    const struct nft_set *set);
603 
nft_set_gc_interval(const struct nft_set * set)604 static inline unsigned long nft_set_gc_interval(const struct nft_set *set)
605 {
606 	u32 gc_int = READ_ONCE(set->gc_int);
607 
608 	return gc_int ? msecs_to_jiffies(gc_int) : HZ;
609 }
610 
611 /**
612  *	struct nft_set_binding - nf_tables set binding
613  *
614  *	@list: set bindings list node
615  *	@chain: chain containing the rule bound to the set
616  *	@flags: set action flags
617  *
618  *	A set binding contains all information necessary for validation
619  *	of new elements added to a bound set.
620  */
621 struct nft_set_binding {
622 	struct list_head		list;
623 	const struct nft_chain		*chain;
624 	u32				flags;
625 };
626 
627 enum nft_trans_phase;
628 void nf_tables_activate_set(const struct nft_ctx *ctx, struct nft_set *set);
629 void nf_tables_deactivate_set(const struct nft_ctx *ctx, struct nft_set *set,
630 			      struct nft_set_binding *binding,
631 			      enum nft_trans_phase phase);
632 int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
633 		       struct nft_set_binding *binding);
634 void nf_tables_destroy_set(const struct nft_ctx *ctx, struct nft_set *set);
635 
636 /**
637  *	enum nft_set_extensions - set extension type IDs
638  *
639  *	@NFT_SET_EXT_KEY: element key
640  *	@NFT_SET_EXT_KEY_END: upper bound element key, for ranges
641  *	@NFT_SET_EXT_DATA: mapping data
642  *	@NFT_SET_EXT_FLAGS: element flags
643  *	@NFT_SET_EXT_TIMEOUT: element timeout
644  *	@NFT_SET_EXT_EXPIRATION: element expiration time
645  *	@NFT_SET_EXT_USERDATA: user data associated with the element
646  *	@NFT_SET_EXT_EXPRESSIONS: expressions assiciated with the element
647  *	@NFT_SET_EXT_OBJREF: stateful object reference associated with element
648  *	@NFT_SET_EXT_NUM: number of extension types
649  */
650 enum nft_set_extensions {
651 	NFT_SET_EXT_KEY,
652 	NFT_SET_EXT_KEY_END,
653 	NFT_SET_EXT_DATA,
654 	NFT_SET_EXT_FLAGS,
655 	NFT_SET_EXT_TIMEOUT,
656 	NFT_SET_EXT_EXPIRATION,
657 	NFT_SET_EXT_USERDATA,
658 	NFT_SET_EXT_EXPRESSIONS,
659 	NFT_SET_EXT_OBJREF,
660 	NFT_SET_EXT_NUM
661 };
662 
663 /**
664  *	struct nft_set_ext_type - set extension type
665  *
666  * 	@len: fixed part length of the extension
667  * 	@align: alignment requirements of the extension
668  */
669 struct nft_set_ext_type {
670 	u8	len;
671 	u8	align;
672 };
673 
674 extern const struct nft_set_ext_type nft_set_ext_types[];
675 
676 /**
677  *	struct nft_set_ext_tmpl - set extension template
678  *
679  *	@len: length of extension area
680  *	@offset: offsets of individual extension types
681  */
682 struct nft_set_ext_tmpl {
683 	u16	len;
684 	u8	offset[NFT_SET_EXT_NUM];
685 	u8	ext_len[NFT_SET_EXT_NUM];
686 };
687 
688 /**
689  *	struct nft_set_ext - set extensions
690  *
691  *	@genmask: generation mask
692  *	@offset: offsets of individual extension types
693  *	@data: beginning of extension data
694  */
695 struct nft_set_ext {
696 	u8	genmask;
697 	u8	offset[NFT_SET_EXT_NUM];
698 	char	data[];
699 };
700 
nft_set_ext_prepare(struct nft_set_ext_tmpl * tmpl)701 static inline void nft_set_ext_prepare(struct nft_set_ext_tmpl *tmpl)
702 {
703 	memset(tmpl, 0, sizeof(*tmpl));
704 	tmpl->len = sizeof(struct nft_set_ext);
705 }
706 
nft_set_ext_add_length(struct nft_set_ext_tmpl * tmpl,u8 id,unsigned int len)707 static inline int nft_set_ext_add_length(struct nft_set_ext_tmpl *tmpl, u8 id,
708 					 unsigned int len)
709 {
710 	tmpl->len	 = ALIGN(tmpl->len, nft_set_ext_types[id].align);
711 	if (tmpl->len > U8_MAX)
712 		return -EINVAL;
713 
714 	tmpl->offset[id] = tmpl->len;
715 	tmpl->ext_len[id] = nft_set_ext_types[id].len + len;
716 	tmpl->len	+= tmpl->ext_len[id];
717 
718 	return 0;
719 }
720 
nft_set_ext_add(struct nft_set_ext_tmpl * tmpl,u8 id)721 static inline int nft_set_ext_add(struct nft_set_ext_tmpl *tmpl, u8 id)
722 {
723 	return nft_set_ext_add_length(tmpl, id, 0);
724 }
725 
nft_set_ext_init(struct nft_set_ext * ext,const struct nft_set_ext_tmpl * tmpl)726 static inline void nft_set_ext_init(struct nft_set_ext *ext,
727 				    const struct nft_set_ext_tmpl *tmpl)
728 {
729 	memcpy(ext->offset, tmpl->offset, sizeof(ext->offset));
730 }
731 
__nft_set_ext_exists(const struct nft_set_ext * ext,u8 id)732 static inline bool __nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
733 {
734 	return !!ext->offset[id];
735 }
736 
nft_set_ext_exists(const struct nft_set_ext * ext,u8 id)737 static inline bool nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
738 {
739 	return ext && __nft_set_ext_exists(ext, id);
740 }
741 
nft_set_ext(const struct nft_set_ext * ext,u8 id)742 static inline void *nft_set_ext(const struct nft_set_ext *ext, u8 id)
743 {
744 	return (void *)ext + ext->offset[id];
745 }
746 
nft_set_ext_key(const struct nft_set_ext * ext)747 static inline struct nft_data *nft_set_ext_key(const struct nft_set_ext *ext)
748 {
749 	return nft_set_ext(ext, NFT_SET_EXT_KEY);
750 }
751 
nft_set_ext_key_end(const struct nft_set_ext * ext)752 static inline struct nft_data *nft_set_ext_key_end(const struct nft_set_ext *ext)
753 {
754 	return nft_set_ext(ext, NFT_SET_EXT_KEY_END);
755 }
756 
nft_set_ext_data(const struct nft_set_ext * ext)757 static inline struct nft_data *nft_set_ext_data(const struct nft_set_ext *ext)
758 {
759 	return nft_set_ext(ext, NFT_SET_EXT_DATA);
760 }
761 
nft_set_ext_flags(const struct nft_set_ext * ext)762 static inline u8 *nft_set_ext_flags(const struct nft_set_ext *ext)
763 {
764 	return nft_set_ext(ext, NFT_SET_EXT_FLAGS);
765 }
766 
nft_set_ext_timeout(const struct nft_set_ext * ext)767 static inline u64 *nft_set_ext_timeout(const struct nft_set_ext *ext)
768 {
769 	return nft_set_ext(ext, NFT_SET_EXT_TIMEOUT);
770 }
771 
nft_set_ext_expiration(const struct nft_set_ext * ext)772 static inline u64 *nft_set_ext_expiration(const struct nft_set_ext *ext)
773 {
774 	return nft_set_ext(ext, NFT_SET_EXT_EXPIRATION);
775 }
776 
nft_set_ext_userdata(const struct nft_set_ext * ext)777 static inline struct nft_userdata *nft_set_ext_userdata(const struct nft_set_ext *ext)
778 {
779 	return nft_set_ext(ext, NFT_SET_EXT_USERDATA);
780 }
781 
nft_set_ext_expr(const struct nft_set_ext * ext)782 static inline struct nft_set_elem_expr *nft_set_ext_expr(const struct nft_set_ext *ext)
783 {
784 	return nft_set_ext(ext, NFT_SET_EXT_EXPRESSIONS);
785 }
786 
nft_set_elem_expired(const struct nft_set_ext * ext)787 static inline bool nft_set_elem_expired(const struct nft_set_ext *ext)
788 {
789 	return nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION) &&
790 	       time_is_before_eq_jiffies64(*nft_set_ext_expiration(ext));
791 }
792 
nft_set_elem_ext(const struct nft_set * set,void * elem)793 static inline struct nft_set_ext *nft_set_elem_ext(const struct nft_set *set,
794 						   void *elem)
795 {
796 	return elem + set->ops->elemsize;
797 }
798 
nft_set_ext_obj(const struct nft_set_ext * ext)799 static inline struct nft_object **nft_set_ext_obj(const struct nft_set_ext *ext)
800 {
801 	return nft_set_ext(ext, NFT_SET_EXT_OBJREF);
802 }
803 
804 struct nft_expr *nft_set_elem_expr_alloc(const struct nft_ctx *ctx,
805 					 const struct nft_set *set,
806 					 const struct nlattr *attr);
807 
808 void *nft_set_elem_init(const struct nft_set *set,
809 			const struct nft_set_ext_tmpl *tmpl,
810 			const u32 *key, const u32 *key_end, const u32 *data,
811 			u64 timeout, u64 expiration, gfp_t gfp);
812 int nft_set_elem_expr_clone(const struct nft_ctx *ctx, struct nft_set *set,
813 			    struct nft_expr *expr_array[]);
814 void nft_set_elem_destroy(const struct nft_set *set, void *elem,
815 			  bool destroy_expr);
816 void nf_tables_set_elem_destroy(const struct nft_ctx *ctx,
817 				const struct nft_set *set, void *elem);
818 
819 struct nft_expr_ops;
820 /**
821  *	struct nft_expr_type - nf_tables expression type
822  *
823  *	@select_ops: function to select nft_expr_ops
824  *	@release_ops: release nft_expr_ops
825  *	@ops: default ops, used when no select_ops functions is present
826  *	@list: used internally
827  *	@name: Identifier
828  *	@owner: module reference
829  *	@policy: netlink attribute policy
830  *	@maxattr: highest netlink attribute number
831  *	@family: address family for AF-specific types
832  *	@flags: expression type flags
833  */
834 struct nft_expr_type {
835 	const struct nft_expr_ops	*(*select_ops)(const struct nft_ctx *,
836 						       const struct nlattr * const tb[]);
837 	void				(*release_ops)(const struct nft_expr_ops *ops);
838 	const struct nft_expr_ops	*ops;
839 	struct list_head		list;
840 	const char			*name;
841 	struct module			*owner;
842 	const struct nla_policy		*policy;
843 	unsigned int			maxattr;
844 	u8				family;
845 	u8				flags;
846 };
847 
848 #define NFT_EXPR_STATEFUL		0x1
849 #define NFT_EXPR_GC			0x2
850 
851 enum nft_trans_phase {
852 	NFT_TRANS_PREPARE,
853 	NFT_TRANS_PREPARE_ERROR,
854 	NFT_TRANS_ABORT,
855 	NFT_TRANS_COMMIT,
856 	NFT_TRANS_RELEASE
857 };
858 
859 struct nft_flow_rule;
860 struct nft_offload_ctx;
861 
862 /**
863  *	struct nft_expr_ops - nf_tables expression operations
864  *
865  *	@eval: Expression evaluation function
866  *	@size: full expression size, including private data size
867  *	@init: initialization function
868  *	@activate: activate expression in the next generation
869  *	@deactivate: deactivate expression in next generation
870  *	@destroy: destruction function, called after synchronize_rcu
871  *	@dump: function to dump parameters
872  *	@type: expression type
873  *	@validate: validate expression, called during loop detection
874  *	@data: extra data to attach to this expression operation
875  */
876 struct nft_expr_ops {
877 	void				(*eval)(const struct nft_expr *expr,
878 						struct nft_regs *regs,
879 						const struct nft_pktinfo *pkt);
880 	int				(*clone)(struct nft_expr *dst,
881 						 const struct nft_expr *src);
882 	unsigned int			size;
883 
884 	int				(*init)(const struct nft_ctx *ctx,
885 						const struct nft_expr *expr,
886 						const struct nlattr * const tb[]);
887 	void				(*activate)(const struct nft_ctx *ctx,
888 						    const struct nft_expr *expr);
889 	void				(*deactivate)(const struct nft_ctx *ctx,
890 						      const struct nft_expr *expr,
891 						      enum nft_trans_phase phase);
892 	void				(*destroy)(const struct nft_ctx *ctx,
893 						   const struct nft_expr *expr);
894 	void				(*destroy_clone)(const struct nft_ctx *ctx,
895 							 const struct nft_expr *expr);
896 	int				(*dump)(struct sk_buff *skb,
897 						const struct nft_expr *expr);
898 	int				(*validate)(const struct nft_ctx *ctx,
899 						    const struct nft_expr *expr,
900 						    const struct nft_data **data);
901 	bool				(*reduce)(struct nft_regs_track *track,
902 						  const struct nft_expr *expr);
903 	bool				(*gc)(struct net *net,
904 					      const struct nft_expr *expr);
905 	int				(*offload)(struct nft_offload_ctx *ctx,
906 						   struct nft_flow_rule *flow,
907 						   const struct nft_expr *expr);
908 	bool				(*offload_action)(const struct nft_expr *expr);
909 	void				(*offload_stats)(struct nft_expr *expr,
910 							 const struct flow_stats *stats);
911 	const struct nft_expr_type	*type;
912 	void				*data;
913 };
914 
915 /**
916  *	struct nft_rule - nf_tables rule
917  *
918  *	@list: used internally
919  *	@handle: rule handle
920  *	@genmask: generation mask
921  *	@dlen: length of expression data
922  *	@udata: user data is appended to the rule
923  *	@data: expression data
924  */
925 struct nft_rule {
926 	struct list_head		list;
927 	u64				handle:42,
928 					genmask:2,
929 					dlen:12,
930 					udata:1;
931 	unsigned char			data[]
932 		__attribute__((aligned(__alignof__(struct nft_expr))));
933 };
934 
nft_expr_first(const struct nft_rule * rule)935 static inline struct nft_expr *nft_expr_first(const struct nft_rule *rule)
936 {
937 	return (struct nft_expr *)&rule->data[0];
938 }
939 
nft_expr_next(const struct nft_expr * expr)940 static inline struct nft_expr *nft_expr_next(const struct nft_expr *expr)
941 {
942 	return ((void *)expr) + expr->ops->size;
943 }
944 
nft_expr_last(const struct nft_rule * rule)945 static inline struct nft_expr *nft_expr_last(const struct nft_rule *rule)
946 {
947 	return (struct nft_expr *)&rule->data[rule->dlen];
948 }
949 
nft_expr_more(const struct nft_rule * rule,const struct nft_expr * expr)950 static inline bool nft_expr_more(const struct nft_rule *rule,
951 				 const struct nft_expr *expr)
952 {
953 	return expr != nft_expr_last(rule) && expr->ops;
954 }
955 
nft_userdata(const struct nft_rule * rule)956 static inline struct nft_userdata *nft_userdata(const struct nft_rule *rule)
957 {
958 	return (void *)&rule->data[rule->dlen];
959 }
960 
961 void nft_rule_expr_activate(const struct nft_ctx *ctx, struct nft_rule *rule);
962 void nft_rule_expr_deactivate(const struct nft_ctx *ctx, struct nft_rule *rule,
963 			      enum nft_trans_phase phase);
964 void nf_tables_rule_destroy(const struct nft_ctx *ctx, struct nft_rule *rule);
965 
nft_set_elem_update_expr(const struct nft_set_ext * ext,struct nft_regs * regs,const struct nft_pktinfo * pkt)966 static inline void nft_set_elem_update_expr(const struct nft_set_ext *ext,
967 					    struct nft_regs *regs,
968 					    const struct nft_pktinfo *pkt)
969 {
970 	struct nft_set_elem_expr *elem_expr;
971 	struct nft_expr *expr;
972 	u32 size;
973 
974 	if (__nft_set_ext_exists(ext, NFT_SET_EXT_EXPRESSIONS)) {
975 		elem_expr = nft_set_ext_expr(ext);
976 		nft_setelem_expr_foreach(expr, elem_expr, size) {
977 			expr->ops->eval(expr, regs, pkt);
978 			if (regs->verdict.code == NFT_BREAK)
979 				return;
980 		}
981 	}
982 }
983 
984 /*
985  * The last pointer isn't really necessary, but the compiler isn't able to
986  * determine that the result of nft_expr_last() is always the same since it
987  * can't assume that the dlen value wasn't changed within calls in the loop.
988  */
989 #define nft_rule_for_each_expr(expr, last, rule) \
990 	for ((expr) = nft_expr_first(rule), (last) = nft_expr_last(rule); \
991 	     (expr) != (last); \
992 	     (expr) = nft_expr_next(expr))
993 
994 #define NFT_CHAIN_POLICY_UNSET		U8_MAX
995 
996 struct nft_rule_dp {
997 	u64				is_last:1,
998 					dlen:12,
999 					handle:42;	/* for tracing */
1000 	unsigned char			data[]
1001 		__attribute__((aligned(__alignof__(struct nft_expr))));
1002 };
1003 
1004 struct nft_rule_blob {
1005 	unsigned long			size;
1006 	unsigned char			data[]
1007 		__attribute__((aligned(__alignof__(struct nft_rule_dp))));
1008 };
1009 
1010 /**
1011  *	struct nft_chain - nf_tables chain
1012  *
1013  *	@rules: list of rules in the chain
1014  *	@list: used internally
1015  *	@rhlhead: used internally
1016  *	@table: table that this chain belongs to
1017  *	@handle: chain handle
1018  *	@use: number of jump references to this chain
1019  *	@flags: bitmask of enum nft_chain_flags
1020  *	@name: name of the chain
1021  */
1022 struct nft_chain {
1023 	struct nft_rule_blob		__rcu *blob_gen_0;
1024 	struct nft_rule_blob		__rcu *blob_gen_1;
1025 	struct list_head		rules;
1026 	struct list_head		list;
1027 	struct rhlist_head		rhlhead;
1028 	struct nft_table		*table;
1029 	u64				handle;
1030 	u32				use;
1031 	u8				flags:5,
1032 					bound:1,
1033 					genmask:2;
1034 	char				*name;
1035 	u16				udlen;
1036 	u8				*udata;
1037 
1038 	/* Only used during control plane commit phase: */
1039 	struct nft_rule_blob		*blob_next;
1040 };
1041 
1042 int nft_chain_validate(const struct nft_ctx *ctx, const struct nft_chain *chain);
1043 int nft_setelem_validate(const struct nft_ctx *ctx, struct nft_set *set,
1044 			 const struct nft_set_iter *iter,
1045 			 struct nft_set_elem *elem);
1046 int nft_set_catchall_validate(const struct nft_ctx *ctx, struct nft_set *set);
1047 int nf_tables_bind_chain(const struct nft_ctx *ctx, struct nft_chain *chain);
1048 void nf_tables_unbind_chain(const struct nft_ctx *ctx, struct nft_chain *chain);
1049 
1050 enum nft_chain_types {
1051 	NFT_CHAIN_T_DEFAULT = 0,
1052 	NFT_CHAIN_T_ROUTE,
1053 	NFT_CHAIN_T_NAT,
1054 	NFT_CHAIN_T_MAX
1055 };
1056 
1057 /**
1058  * 	struct nft_chain_type - nf_tables chain type info
1059  *
1060  * 	@name: name of the type
1061  * 	@type: numeric identifier
1062  * 	@family: address family
1063  * 	@owner: module owner
1064  * 	@hook_mask: mask of valid hooks
1065  * 	@hooks: array of hook functions
1066  *	@ops_register: base chain register function
1067  *	@ops_unregister: base chain unregister function
1068  */
1069 struct nft_chain_type {
1070 	const char			*name;
1071 	enum nft_chain_types		type;
1072 	int				family;
1073 	struct module			*owner;
1074 	unsigned int			hook_mask;
1075 	nf_hookfn			*hooks[NFT_MAX_HOOKS];
1076 	int				(*ops_register)(struct net *net, const struct nf_hook_ops *ops);
1077 	void				(*ops_unregister)(struct net *net, const struct nf_hook_ops *ops);
1078 };
1079 
1080 int nft_chain_validate_dependency(const struct nft_chain *chain,
1081 				  enum nft_chain_types type);
1082 int nft_chain_validate_hooks(const struct nft_chain *chain,
1083                              unsigned int hook_flags);
1084 
nft_chain_binding(const struct nft_chain * chain)1085 static inline bool nft_chain_binding(const struct nft_chain *chain)
1086 {
1087 	return chain->flags & NFT_CHAIN_BINDING;
1088 }
1089 
nft_chain_is_bound(struct nft_chain * chain)1090 static inline bool nft_chain_is_bound(struct nft_chain *chain)
1091 {
1092 	return (chain->flags & NFT_CHAIN_BINDING) && chain->bound;
1093 }
1094 
1095 int nft_chain_add(struct nft_table *table, struct nft_chain *chain);
1096 void nft_chain_del(struct nft_chain *chain);
1097 void nf_tables_chain_destroy(struct nft_ctx *ctx);
1098 
1099 struct nft_stats {
1100 	u64			bytes;
1101 	u64			pkts;
1102 	struct u64_stats_sync	syncp;
1103 };
1104 
1105 struct nft_hook {
1106 	struct list_head	list;
1107 	struct nf_hook_ops	ops;
1108 	struct rcu_head		rcu;
1109 };
1110 
1111 /**
1112  *	struct nft_base_chain - nf_tables base chain
1113  *
1114  *	@ops: netfilter hook ops
1115  *	@hook_list: list of netfilter hooks (for NFPROTO_NETDEV family)
1116  *	@type: chain type
1117  *	@policy: default policy
1118  *	@stats: per-cpu chain stats
1119  *	@chain: the chain
1120  *	@flow_block: flow block (for hardware offload)
1121  */
1122 struct nft_base_chain {
1123 	struct nf_hook_ops		ops;
1124 	struct list_head		hook_list;
1125 	const struct nft_chain_type	*type;
1126 	u8				policy;
1127 	u8				flags;
1128 	struct nft_stats __percpu	*stats;
1129 	struct nft_chain		chain;
1130 	struct flow_block		flow_block;
1131 };
1132 
nft_base_chain(const struct nft_chain * chain)1133 static inline struct nft_base_chain *nft_base_chain(const struct nft_chain *chain)
1134 {
1135 	return container_of(chain, struct nft_base_chain, chain);
1136 }
1137 
nft_is_base_chain(const struct nft_chain * chain)1138 static inline bool nft_is_base_chain(const struct nft_chain *chain)
1139 {
1140 	return chain->flags & NFT_CHAIN_BASE;
1141 }
1142 
1143 int __nft_release_basechain(struct nft_ctx *ctx);
1144 
1145 unsigned int nft_do_chain(struct nft_pktinfo *pkt, void *priv);
1146 
nft_use_inc(u32 * use)1147 static inline bool nft_use_inc(u32 *use)
1148 {
1149 	if (*use == UINT_MAX)
1150 		return false;
1151 
1152 	(*use)++;
1153 
1154 	return true;
1155 }
1156 
nft_use_dec(u32 * use)1157 static inline void nft_use_dec(u32 *use)
1158 {
1159 	WARN_ON_ONCE((*use)-- == 0);
1160 }
1161 
1162 /* For error and abort path: restore use counter to previous state. */
nft_use_inc_restore(u32 * use)1163 static inline void nft_use_inc_restore(u32 *use)
1164 {
1165 	WARN_ON_ONCE(!nft_use_inc(use));
1166 }
1167 
1168 #define nft_use_dec_restore	nft_use_dec
1169 
1170 /**
1171  *	struct nft_table - nf_tables table
1172  *
1173  *	@list: used internally
1174  *	@chains_ht: chains in the table
1175  *	@chains: same, for stable walks
1176  *	@sets: sets in the table
1177  *	@objects: stateful objects in the table
1178  *	@flowtables: flow tables in the table
1179  *	@hgenerator: handle generator state
1180  *	@handle: table handle
1181  *	@use: number of chain references to this table
1182  *	@flags: table flag (see enum nft_table_flags)
1183  *	@genmask: generation mask
1184  *	@afinfo: address family info
1185  *	@name: name of the table
1186  */
1187 struct nft_table {
1188 	struct list_head		list;
1189 	struct rhltable			chains_ht;
1190 	struct list_head		chains;
1191 	struct list_head		sets;
1192 	struct list_head		objects;
1193 	struct list_head		flowtables;
1194 	u64				hgenerator;
1195 	u64				handle;
1196 	u32				use;
1197 	u16				family:6,
1198 					flags:8,
1199 					genmask:2;
1200 	u32				nlpid;
1201 	char				*name;
1202 	u16				udlen;
1203 	u8				*udata;
1204 };
1205 
nft_table_has_owner(const struct nft_table * table)1206 static inline bool nft_table_has_owner(const struct nft_table *table)
1207 {
1208 	return table->flags & NFT_TABLE_F_OWNER;
1209 }
1210 
nft_base_chain_netdev(int family,u32 hooknum)1211 static inline bool nft_base_chain_netdev(int family, u32 hooknum)
1212 {
1213 	return family == NFPROTO_NETDEV ||
1214 	       (family == NFPROTO_INET && hooknum == NF_INET_INGRESS);
1215 }
1216 
1217 void nft_register_chain_type(const struct nft_chain_type *);
1218 void nft_unregister_chain_type(const struct nft_chain_type *);
1219 
1220 int nft_register_expr(struct nft_expr_type *);
1221 void nft_unregister_expr(struct nft_expr_type *);
1222 
1223 int nft_verdict_dump(struct sk_buff *skb, int type,
1224 		     const struct nft_verdict *v);
1225 
1226 /**
1227  *	struct nft_object_hash_key - key to lookup nft_object
1228  *
1229  *	@name: name of the stateful object to look up
1230  *	@table: table the object belongs to
1231  */
1232 struct nft_object_hash_key {
1233 	const char                      *name;
1234 	const struct nft_table          *table;
1235 };
1236 
1237 /**
1238  *	struct nft_object - nf_tables stateful object
1239  *
1240  *	@list: table stateful object list node
1241  *	@key:  keys that identify this object
1242  *	@rhlhead: nft_objname_ht node
1243  *	@genmask: generation mask
1244  *	@use: number of references to this stateful object
1245  *	@handle: unique object handle
1246  *	@ops: object operations
1247  *	@data: object data, layout depends on type
1248  */
1249 struct nft_object {
1250 	struct list_head		list;
1251 	struct rhlist_head		rhlhead;
1252 	struct nft_object_hash_key	key;
1253 	u32				genmask:2;
1254 	u32				use;
1255 	u64				handle;
1256 	u16				udlen;
1257 	u8				*udata;
1258 	/* runtime data below here */
1259 	const struct nft_object_ops	*ops ____cacheline_aligned;
1260 	unsigned char			data[]
1261 		__attribute__((aligned(__alignof__(u64))));
1262 };
1263 
nft_obj_data(const struct nft_object * obj)1264 static inline void *nft_obj_data(const struct nft_object *obj)
1265 {
1266 	return (void *)obj->data;
1267 }
1268 
1269 #define nft_expr_obj(expr)	*((struct nft_object **)nft_expr_priv(expr))
1270 
1271 struct nft_object *nft_obj_lookup(const struct net *net,
1272 				  const struct nft_table *table,
1273 				  const struct nlattr *nla, u32 objtype,
1274 				  u8 genmask);
1275 
1276 void nft_obj_notify(struct net *net, const struct nft_table *table,
1277 		    struct nft_object *obj, u32 portid, u32 seq,
1278 		    int event, u16 flags, int family, int report, gfp_t gfp);
1279 
1280 /**
1281  *	struct nft_object_type - stateful object type
1282  *
1283  *	@select_ops: function to select nft_object_ops
1284  *	@ops: default ops, used when no select_ops functions is present
1285  *	@list: list node in list of object types
1286  *	@type: stateful object numeric type
1287  *	@owner: module owner
1288  *	@maxattr: maximum netlink attribute
1289  *	@family: address family for AF-specific object types
1290  *	@policy: netlink attribute policy
1291  */
1292 struct nft_object_type {
1293 	const struct nft_object_ops	*(*select_ops)(const struct nft_ctx *,
1294 						       const struct nlattr * const tb[]);
1295 	const struct nft_object_ops	*ops;
1296 	struct list_head		list;
1297 	u32				type;
1298 	unsigned int                    maxattr;
1299 	u8				family;
1300 	struct module			*owner;
1301 	const struct nla_policy		*policy;
1302 };
1303 
1304 /**
1305  *	struct nft_object_ops - stateful object operations
1306  *
1307  *	@eval: stateful object evaluation function
1308  *	@size: stateful object size
1309  *	@init: initialize object from netlink attributes
1310  *	@destroy: release existing stateful object
1311  *	@dump: netlink dump stateful object
1312  *	@update: update stateful object
1313  */
1314 struct nft_object_ops {
1315 	void				(*eval)(struct nft_object *obj,
1316 						struct nft_regs *regs,
1317 						const struct nft_pktinfo *pkt);
1318 	unsigned int			size;
1319 	int				(*init)(const struct nft_ctx *ctx,
1320 						const struct nlattr *const tb[],
1321 						struct nft_object *obj);
1322 	void				(*destroy)(const struct nft_ctx *ctx,
1323 						   struct nft_object *obj);
1324 	int				(*dump)(struct sk_buff *skb,
1325 						struct nft_object *obj,
1326 						bool reset);
1327 	void				(*update)(struct nft_object *obj,
1328 						  struct nft_object *newobj);
1329 	const struct nft_object_type	*type;
1330 };
1331 
1332 int nft_register_obj(struct nft_object_type *obj_type);
1333 void nft_unregister_obj(struct nft_object_type *obj_type);
1334 
1335 #define NFT_NETDEVICE_MAX	256
1336 
1337 /**
1338  *	struct nft_flowtable - nf_tables flow table
1339  *
1340  *	@list: flow table list node in table list
1341  * 	@table: the table the flow table is contained in
1342  *	@name: name of this flow table
1343  *	@hooknum: hook number
1344  *	@ops_len: number of hooks in array
1345  *	@genmask: generation mask
1346  *	@use: number of references to this flow table
1347  * 	@handle: unique object handle
1348  *	@dev_name: array of device names
1349  *	@data: rhashtable and garbage collector
1350  * 	@ops: array of hooks
1351  */
1352 struct nft_flowtable {
1353 	struct list_head		list;
1354 	struct nft_table		*table;
1355 	char				*name;
1356 	int				hooknum;
1357 	int				ops_len;
1358 	u32				genmask:2;
1359 	u32				use;
1360 	u64				handle;
1361 	/* runtime data below here */
1362 	struct list_head		hook_list ____cacheline_aligned;
1363 	struct nf_flowtable		data;
1364 };
1365 
1366 struct nft_flowtable *nft_flowtable_lookup(const struct nft_table *table,
1367 					   const struct nlattr *nla,
1368 					   u8 genmask);
1369 
1370 void nf_tables_deactivate_flowtable(const struct nft_ctx *ctx,
1371 				    struct nft_flowtable *flowtable,
1372 				    enum nft_trans_phase phase);
1373 
1374 void nft_register_flowtable_type(struct nf_flowtable_type *type);
1375 void nft_unregister_flowtable_type(struct nf_flowtable_type *type);
1376 
1377 /**
1378  *	struct nft_traceinfo - nft tracing information and state
1379  *
1380  *	@trace: other struct members are initialised
1381  *	@nf_trace: copy of skb->nf_trace before rule evaluation
1382  *	@type: event type (enum nft_trace_types)
1383  *	@skbid: hash of skb to be used as trace id
1384  *	@packet_dumped: packet headers sent in a previous traceinfo message
1385  *	@pkt: pktinfo currently processed
1386  *	@basechain: base chain currently processed
1387  *	@chain: chain currently processed
1388  *	@rule:  rule that was evaluated
1389  *	@verdict: verdict given by rule
1390  */
1391 struct nft_traceinfo {
1392 	bool				trace;
1393 	bool				nf_trace;
1394 	bool				packet_dumped;
1395 	enum nft_trace_types		type:8;
1396 	u32				skbid;
1397 	const struct nft_pktinfo	*pkt;
1398 	const struct nft_base_chain	*basechain;
1399 	const struct nft_chain		*chain;
1400 	const struct nft_rule_dp	*rule;
1401 	const struct nft_verdict	*verdict;
1402 };
1403 
1404 void nft_trace_init(struct nft_traceinfo *info, const struct nft_pktinfo *pkt,
1405 		    const struct nft_verdict *verdict,
1406 		    const struct nft_chain *basechain);
1407 
1408 void nft_trace_notify(struct nft_traceinfo *info);
1409 
1410 #define MODULE_ALIAS_NFT_CHAIN(family, name) \
1411 	MODULE_ALIAS("nft-chain-" __stringify(family) "-" name)
1412 
1413 #define MODULE_ALIAS_NFT_AF_EXPR(family, name) \
1414 	MODULE_ALIAS("nft-expr-" __stringify(family) "-" name)
1415 
1416 #define MODULE_ALIAS_NFT_EXPR(name) \
1417 	MODULE_ALIAS("nft-expr-" name)
1418 
1419 #define MODULE_ALIAS_NFT_OBJ(type) \
1420 	MODULE_ALIAS("nft-obj-" __stringify(type))
1421 
1422 #if IS_ENABLED(CONFIG_NF_TABLES)
1423 
1424 /*
1425  * The gencursor defines two generations, the currently active and the
1426  * next one. Objects contain a bitmask of 2 bits specifying the generations
1427  * they're active in. A set bit means they're inactive in the generation
1428  * represented by that bit.
1429  *
1430  * New objects start out as inactive in the current and active in the
1431  * next generation. When committing the ruleset the bitmask is cleared,
1432  * meaning they're active in all generations. When removing an object,
1433  * it is set inactive in the next generation. After committing the ruleset,
1434  * the objects are removed.
1435  */
nft_gencursor_next(const struct net * net)1436 static inline unsigned int nft_gencursor_next(const struct net *net)
1437 {
1438 	return net->nft.gencursor + 1 == 1 ? 1 : 0;
1439 }
1440 
nft_genmask_next(const struct net * net)1441 static inline u8 nft_genmask_next(const struct net *net)
1442 {
1443 	return 1 << nft_gencursor_next(net);
1444 }
1445 
nft_genmask_cur(const struct net * net)1446 static inline u8 nft_genmask_cur(const struct net *net)
1447 {
1448 	/* Use READ_ONCE() to prevent refetching the value for atomicity */
1449 	return 1 << READ_ONCE(net->nft.gencursor);
1450 }
1451 
1452 #define NFT_GENMASK_ANY		((1 << 0) | (1 << 1))
1453 
1454 /*
1455  * Generic transaction helpers
1456  */
1457 
1458 /* Check if this object is currently active. */
1459 #define nft_is_active(__net, __obj)				\
1460 	(((__obj)->genmask & nft_genmask_cur(__net)) == 0)
1461 
1462 /* Check if this object is active in the next generation. */
1463 #define nft_is_active_next(__net, __obj)			\
1464 	(((__obj)->genmask & nft_genmask_next(__net)) == 0)
1465 
1466 /* This object becomes active in the next generation. */
1467 #define nft_activate_next(__net, __obj)				\
1468 	(__obj)->genmask = nft_genmask_cur(__net)
1469 
1470 /* This object becomes inactive in the next generation. */
1471 #define nft_deactivate_next(__net, __obj)			\
1472         (__obj)->genmask = nft_genmask_next(__net)
1473 
1474 /* After committing the ruleset, clear the stale generation bit. */
1475 #define nft_clear(__net, __obj)					\
1476 	(__obj)->genmask &= ~nft_genmask_next(__net)
1477 #define nft_active_genmask(__obj, __genmask)			\
1478 	!((__obj)->genmask & __genmask)
1479 
1480 /*
1481  * Set element transaction helpers
1482  */
1483 
nft_set_elem_active(const struct nft_set_ext * ext,u8 genmask)1484 static inline bool nft_set_elem_active(const struct nft_set_ext *ext,
1485 				       u8 genmask)
1486 {
1487 	return !(ext->genmask & genmask);
1488 }
1489 
nft_set_elem_change_active(const struct net * net,const struct nft_set * set,struct nft_set_ext * ext)1490 static inline void nft_set_elem_change_active(const struct net *net,
1491 					      const struct nft_set *set,
1492 					      struct nft_set_ext *ext)
1493 {
1494 	ext->genmask ^= nft_genmask_next(net);
1495 }
1496 
1497 #endif /* IS_ENABLED(CONFIG_NF_TABLES) */
1498 
1499 #define NFT_SET_ELEM_DEAD_MASK	(1 << 2)
1500 
1501 #if defined(__LITTLE_ENDIAN_BITFIELD)
1502 #define NFT_SET_ELEM_DEAD_BIT	2
1503 #elif defined(__BIG_ENDIAN_BITFIELD)
1504 #define NFT_SET_ELEM_DEAD_BIT	(BITS_PER_LONG - BITS_PER_BYTE + 2)
1505 #else
1506 #error
1507 #endif
1508 
nft_set_elem_dead(struct nft_set_ext * ext)1509 static inline void nft_set_elem_dead(struct nft_set_ext *ext)
1510 {
1511 	unsigned long *word = (unsigned long *)ext;
1512 
1513 	BUILD_BUG_ON(offsetof(struct nft_set_ext, genmask) != 0);
1514 	set_bit(NFT_SET_ELEM_DEAD_BIT, word);
1515 }
1516 
nft_set_elem_is_dead(const struct nft_set_ext * ext)1517 static inline int nft_set_elem_is_dead(const struct nft_set_ext *ext)
1518 {
1519 	unsigned long *word = (unsigned long *)ext;
1520 
1521 	BUILD_BUG_ON(offsetof(struct nft_set_ext, genmask) != 0);
1522 	return test_bit(NFT_SET_ELEM_DEAD_BIT, word);
1523 }
1524 
1525 /**
1526  *	struct nft_trans - nf_tables object update in transaction
1527  *
1528  *	@list: used internally
1529  *	@binding_list: list of objects with possible bindings
1530  *	@msg_type: message type
1531  *	@put_net: ctx->net needs to be put
1532  *	@ctx: transaction context
1533  *	@data: internal information related to the transaction
1534  */
1535 struct nft_trans {
1536 	struct list_head		list;
1537 	struct list_head		binding_list;
1538 	int				msg_type;
1539 	bool				put_net;
1540 	struct nft_ctx			ctx;
1541 	char				data[];
1542 };
1543 
1544 struct nft_trans_rule {
1545 	struct nft_rule			*rule;
1546 	struct nft_flow_rule		*flow;
1547 	u32				rule_id;
1548 	bool				bound;
1549 };
1550 
1551 #define nft_trans_rule(trans)	\
1552 	(((struct nft_trans_rule *)trans->data)->rule)
1553 #define nft_trans_flow_rule(trans)	\
1554 	(((struct nft_trans_rule *)trans->data)->flow)
1555 #define nft_trans_rule_id(trans)	\
1556 	(((struct nft_trans_rule *)trans->data)->rule_id)
1557 #define nft_trans_rule_bound(trans)	\
1558 	(((struct nft_trans_rule *)trans->data)->bound)
1559 
1560 struct nft_trans_set {
1561 	struct nft_set			*set;
1562 	u32				set_id;
1563 	u32				gc_int;
1564 	u64				timeout;
1565 	bool				update;
1566 	bool				bound;
1567 };
1568 
1569 #define nft_trans_set(trans)	\
1570 	(((struct nft_trans_set *)trans->data)->set)
1571 #define nft_trans_set_id(trans)	\
1572 	(((struct nft_trans_set *)trans->data)->set_id)
1573 #define nft_trans_set_bound(trans)	\
1574 	(((struct nft_trans_set *)trans->data)->bound)
1575 #define nft_trans_set_update(trans)	\
1576 	(((struct nft_trans_set *)trans->data)->update)
1577 #define nft_trans_set_timeout(trans)	\
1578 	(((struct nft_trans_set *)trans->data)->timeout)
1579 #define nft_trans_set_gc_int(trans)	\
1580 	(((struct nft_trans_set *)trans->data)->gc_int)
1581 
1582 struct nft_trans_chain {
1583 	struct nft_chain		*chain;
1584 	bool				update;
1585 	char				*name;
1586 	struct nft_stats __percpu	*stats;
1587 	u8				policy;
1588 	bool				bound;
1589 	u32				chain_id;
1590 };
1591 
1592 #define nft_trans_chain(trans)	\
1593 	(((struct nft_trans_chain *)trans->data)->chain)
1594 #define nft_trans_chain_update(trans)	\
1595 	(((struct nft_trans_chain *)trans->data)->update)
1596 #define nft_trans_chain_name(trans)	\
1597 	(((struct nft_trans_chain *)trans->data)->name)
1598 #define nft_trans_chain_stats(trans)	\
1599 	(((struct nft_trans_chain *)trans->data)->stats)
1600 #define nft_trans_chain_policy(trans)	\
1601 	(((struct nft_trans_chain *)trans->data)->policy)
1602 #define nft_trans_chain_bound(trans)	\
1603 	(((struct nft_trans_chain *)trans->data)->bound)
1604 #define nft_trans_chain_id(trans)	\
1605 	(((struct nft_trans_chain *)trans->data)->chain_id)
1606 
1607 struct nft_trans_table {
1608 	bool				update;
1609 };
1610 
1611 #define nft_trans_table_update(trans)	\
1612 	(((struct nft_trans_table *)trans->data)->update)
1613 
1614 struct nft_trans_elem {
1615 	struct nft_set			*set;
1616 	struct nft_set_elem		elem;
1617 	bool				bound;
1618 };
1619 
1620 #define nft_trans_elem_set(trans)	\
1621 	(((struct nft_trans_elem *)trans->data)->set)
1622 #define nft_trans_elem(trans)	\
1623 	(((struct nft_trans_elem *)trans->data)->elem)
1624 #define nft_trans_elem_set_bound(trans)	\
1625 	(((struct nft_trans_elem *)trans->data)->bound)
1626 
1627 struct nft_trans_obj {
1628 	struct nft_object		*obj;
1629 	struct nft_object		*newobj;
1630 	bool				update;
1631 };
1632 
1633 #define nft_trans_obj(trans)	\
1634 	(((struct nft_trans_obj *)trans->data)->obj)
1635 #define nft_trans_obj_newobj(trans) \
1636 	(((struct nft_trans_obj *)trans->data)->newobj)
1637 #define nft_trans_obj_update(trans)	\
1638 	(((struct nft_trans_obj *)trans->data)->update)
1639 
1640 struct nft_trans_flowtable {
1641 	struct nft_flowtable		*flowtable;
1642 	bool				update;
1643 	struct list_head		hook_list;
1644 	u32				flags;
1645 };
1646 
1647 #define nft_trans_flowtable(trans)	\
1648 	(((struct nft_trans_flowtable *)trans->data)->flowtable)
1649 #define nft_trans_flowtable_update(trans)	\
1650 	(((struct nft_trans_flowtable *)trans->data)->update)
1651 #define nft_trans_flowtable_hooks(trans)	\
1652 	(((struct nft_trans_flowtable *)trans->data)->hook_list)
1653 #define nft_trans_flowtable_flags(trans)	\
1654 	(((struct nft_trans_flowtable *)trans->data)->flags)
1655 
1656 #define NFT_TRANS_GC_BATCHCOUNT	256
1657 
1658 struct nft_trans_gc {
1659 	struct list_head	list;
1660 	struct net		*net;
1661 	struct nft_set		*set;
1662 	u32			seq;
1663 	u16			count;
1664 	void			*priv[NFT_TRANS_GC_BATCHCOUNT];
1665 	struct rcu_head		rcu;
1666 };
1667 
1668 struct nft_trans_gc *nft_trans_gc_alloc(struct nft_set *set,
1669 					unsigned int gc_seq, gfp_t gfp);
1670 void nft_trans_gc_destroy(struct nft_trans_gc *trans);
1671 
1672 struct nft_trans_gc *nft_trans_gc_queue_async(struct nft_trans_gc *gc,
1673 					      unsigned int gc_seq, gfp_t gfp);
1674 void nft_trans_gc_queue_async_done(struct nft_trans_gc *gc);
1675 
1676 struct nft_trans_gc *nft_trans_gc_queue_sync(struct nft_trans_gc *gc, gfp_t gfp);
1677 void nft_trans_gc_queue_sync_done(struct nft_trans_gc *trans);
1678 
1679 void nft_trans_gc_elem_add(struct nft_trans_gc *gc, void *priv);
1680 
1681 struct nft_trans_gc *nft_trans_gc_catchall_async(struct nft_trans_gc *gc,
1682 						 unsigned int gc_seq);
1683 struct nft_trans_gc *nft_trans_gc_catchall_sync(struct nft_trans_gc *gc);
1684 
1685 void nft_setelem_data_deactivate(const struct net *net,
1686 				 const struct nft_set *set,
1687 				 struct nft_set_elem *elem);
1688 
1689 int __init nft_chain_filter_init(void);
1690 void nft_chain_filter_fini(void);
1691 
1692 void __init nft_chain_route_init(void);
1693 void nft_chain_route_fini(void);
1694 
1695 void nf_tables_trans_destroy_flush_work(void);
1696 
1697 int nf_msecs_to_jiffies64(const struct nlattr *nla, u64 *result);
1698 __be64 nf_jiffies64_to_msecs(u64 input);
1699 
1700 #ifdef CONFIG_MODULES
1701 __printf(2, 3) int nft_request_module(struct net *net, const char *fmt, ...);
1702 #else
nft_request_module(struct net * net,const char * fmt,...)1703 static inline int nft_request_module(struct net *net, const char *fmt, ...) { return -ENOENT; }
1704 #endif
1705 
1706 struct nftables_pernet {
1707 	struct list_head	tables;
1708 	struct list_head	commit_list;
1709 	struct list_head	binding_list;
1710 	struct list_head	module_list;
1711 	struct list_head	notify_list;
1712 	struct mutex		commit_mutex;
1713 	u64			table_handle;
1714 	unsigned int		base_seq;
1715 	u8			validate_state;
1716 	unsigned int		gc_seq;
1717 };
1718 
1719 extern unsigned int nf_tables_net_id;
1720 
nft_pernet(const struct net * net)1721 static inline struct nftables_pernet *nft_pernet(const struct net *net)
1722 {
1723 	return net_generic(net, nf_tables_net_id);
1724 }
1725 
1726 #define __NFT_REDUCE_READONLY	1UL
1727 #define NFT_REDUCE_READONLY	(void *)__NFT_REDUCE_READONLY
1728 
nft_reduce_is_readonly(const struct nft_expr * expr)1729 static inline bool nft_reduce_is_readonly(const struct nft_expr *expr)
1730 {
1731 	return expr->ops->reduce == NFT_REDUCE_READONLY;
1732 }
1733 
1734 void nft_reg_track_update(struct nft_regs_track *track,
1735 			  const struct nft_expr *expr, u8 dreg, u8 len);
1736 void nft_reg_track_cancel(struct nft_regs_track *track, u8 dreg, u8 len);
1737 void __nft_reg_track_cancel(struct nft_regs_track *track, u8 dreg);
1738 
nft_reg_track_cmp(struct nft_regs_track * track,const struct nft_expr * expr,u8 dreg)1739 static inline bool nft_reg_track_cmp(struct nft_regs_track *track,
1740 				     const struct nft_expr *expr, u8 dreg)
1741 {
1742 	return track->regs[dreg].selector &&
1743 	       track->regs[dreg].selector->ops == expr->ops &&
1744 	       track->regs[dreg].num_reg == 0;
1745 }
1746 
1747 #endif /* _NET_NF_TABLES_H */
1748