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