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