nf_tables.h 41.0 KB
Newer Older
1
/* SPDX-License-Identifier: GPL-2.0 */
P
Patrick McHardy 已提交
2 3 4
#ifndef _NET_NF_TABLES_H
#define _NET_NF_TABLES_H

5
#include <asm/unaligned.h>
P
Patrick McHardy 已提交
6 7
#include <linux/list.h>
#include <linux/netfilter.h>
8
#include <linux/netfilter/nfnetlink.h>
9
#include <linux/netfilter/x_tables.h>
P
Patrick McHardy 已提交
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>
P
Patrick McHardy 已提交
14
#include <net/netlink.h>
15
#include <net/flow_offload.h>
P
Patrick McHardy 已提交
16

17 18
#define NFT_MAX_HOOKS	(NF_INET_INGRESS + 1)

19 20
struct module;

21 22
#define NFT_JUMP_STACK_SIZE	16

P
Patrick McHardy 已提交
23 24
struct nft_pktinfo {
	struct sk_buff			*skb;
25
	bool				tprot_set;
26
	u8				tprot;
27 28
	/* for x_tables compatibility */
	struct xt_action_param		xt;
P
Patrick McHardy 已提交
29 30
};

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
static inline struct net *nft_net(const struct nft_pktinfo *pkt)
{
	return pkt->xt.state->net;
}

static inline unsigned int nft_hook(const struct nft_pktinfo *pkt)
{
	return pkt->xt.state->hook;
}

static inline u8 nft_pf(const struct nft_pktinfo *pkt)
{
	return pkt->xt.state->pf;
}

static inline const struct net_device *nft_in(const struct nft_pktinfo *pkt)
{
	return pkt->xt.state->in;
}

static inline const struct net_device *nft_out(const struct nft_pktinfo *pkt)
{
	return pkt->xt.state->out;
}

56 57
static inline void nft_set_pktinfo(struct nft_pktinfo *pkt,
				   struct sk_buff *skb,
58
				   const struct nf_hook_state *state)
59 60
{
	pkt->skb = skb;
61
	pkt->xt.state = state;
62 63
}

64 65
static inline void nft_set_pktinfo_unspec(struct nft_pktinfo *pkt,
					  struct sk_buff *skb)
66 67 68 69 70 71 72
{
	pkt->tprot_set = false;
	pkt->tprot = 0;
	pkt->xt.thoff = 0;
	pkt->xt.fragoff = 0;
}

73 74 75 76 77 78 79 80 81 82 83
/**
 * 	struct nft_verdict - nf_tables verdict
 *
 * 	@code: nf_tables/netfilter verdict code
 * 	@chain: destination chain for NFT_JUMP/NFT_GOTO
 */
struct nft_verdict {
	u32				code;
	struct nft_chain		*chain;
};

P
Patrick McHardy 已提交
84 85
struct nft_data {
	union {
86 87
		u32			data[4];
		struct nft_verdict	verdict;
P
Patrick McHardy 已提交
88 89 90
	};
} __attribute__((aligned(__alignof__(u64))));

91 92 93 94 95 96 97 98 99 100
/**
 *	struct nft_regs - nf_tables register set
 *
 *	@data: data registers
 *	@verdict: verdict register
 *
 *	The first four data registers alias to the verdict register.
 */
struct nft_regs {
	union {
101
		u32			data[20];
102 103 104 105
		struct nft_verdict	verdict;
	};
};

106
/* Store/load an u8, u16 or u64 integer to/from the u32 data register.
107 108 109 110 111 112
 *
 * Note, when using concatenations, register allocation happens at 32-bit
 * level. So for store instruction, pad the rest part with zero to avoid
 * garbage values.
 */

113
static inline void nft_reg_store8(u32 *dreg, u8 val)
114 115
{
	*dreg = 0;
116
	*(u8 *)dreg = val;
117 118
}

119
static inline u8 nft_reg_load8(const u32 *sreg)
120 121 122 123 124
{
	return *(u8 *)sreg;
}

static inline void nft_reg_store16(u32 *dreg, u16 val)
125 126
{
	*dreg = 0;
127
	*(u16 *)dreg = val;
128 129
}

130
static inline u16 nft_reg_load16(const u32 *sreg)
131 132 133 134
{
	return *(u16 *)sreg;
}

135
static inline void nft_reg_store64(u32 *dreg, u64 val)
136
{
137 138 139
	put_unaligned(val, (u64 *)dreg);
}

140
static inline u64 nft_reg_load64(const u32 *sreg)
141 142
{
	return get_unaligned((u64 *)sreg);
143 144
}

145 146
static inline void nft_data_copy(u32 *dst, const struct nft_data *src,
				 unsigned int len)
P
Patrick McHardy 已提交
147
{
148 149
	if (len % NFT_REG32_SIZE)
		dst[len / NFT_REG32_SIZE] = 0;
150
	memcpy(dst, src, len);
P
Patrick McHardy 已提交
151 152 153
}

/**
154
 *	struct nft_ctx - nf_tables rule/set context
P
Patrick McHardy 已提交
155
 *
156
 *	@net: net namespace
P
Patrick McHardy 已提交
157 158
 * 	@table: the table the chain is contained in
 * 	@chain: the chain the rule is contained in
159
 *	@nla: netlink attributes
160 161
 *	@portid: netlink portID of the original message
 *	@seq: netlink sequence number
162
 *	@family: protocol family
163
 *	@level: depth of the chains
164
 *	@report: notify via unicast netlink message
P
Patrick McHardy 已提交
165 166
 */
struct nft_ctx {
167
	struct net			*net;
168 169
	struct nft_table		*table;
	struct nft_chain		*chain;
170
	const struct nlattr * const 	*nla;
171 172
	u32				portid;
	u32				seq;
173
	u16				flags;
174
	u8				family;
175
	u8				level;
176
	bool				report;
P
Patrick McHardy 已提交
177 178
};

179 180 181 182
enum nft_data_desc_flags {
	NFT_DATA_DESC_SETELEM	= (1 << 0),
};

P
Patrick McHardy 已提交
183 184
struct nft_data_desc {
	enum nft_data_types		type;
185
	unsigned int			size;
P
Patrick McHardy 已提交
186
	unsigned int			len;
187
	unsigned int			flags;
P
Patrick McHardy 已提交
188 189
};

190
int nft_data_init(const struct nft_ctx *ctx, struct nft_data *data,
191
		  struct nft_data_desc *desc, const struct nlattr *nla);
192
void nft_data_hold(const struct nft_data *data, enum nft_data_types type);
193
void nft_data_release(const struct nft_data *data, enum nft_data_types type);
194 195
int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
		  enum nft_data_types type, unsigned int len);
P
Patrick McHardy 已提交
196 197 198 199 200 201

static inline enum nft_data_types nft_dreg_to_type(enum nft_registers reg)
{
	return reg == NFT_REG_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE;
}

202 203
static inline enum nft_registers nft_type_to_reg(enum nft_data_types type)
{
204
	return type == NFT_DATA_VERDICT ? NFT_REG_VERDICT : NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE;
205 206
}

207
int nft_parse_u32_check(const struct nlattr *attr, int max, u32 *dest);
208
int nft_parse_register(const struct nlattr *attr, u32 *preg);
209 210
int nft_dump_register(struct sk_buff *skb, unsigned int attr, unsigned int reg);

211
int nft_parse_register_load(const struct nlattr *attr, u8 *sreg, u32 len);
212 213 214 215
int nft_parse_register_store(const struct nft_ctx *ctx,
			     const struct nlattr *attr, u8 *dreg,
			     const struct nft_data *data,
			     enum nft_data_types type, unsigned int len);
P
Patrick McHardy 已提交
216

217 218 219 220 221 222 223 224 225 226 227 228
/**
 *	struct nft_userdata - user defined data associated with an object
 *
 *	@len: length of the data
 *	@data: content
 *
 *	The presence of user data is indicated in an object specific fashion,
 *	so a length of zero can't occur and the value "len" indicates data
 *	of length len + 1.
 */
struct nft_userdata {
	u8			len;
229
	unsigned char		data[];
230 231
};

232 233 234 235
/**
 *	struct nft_set_elem - generic representation of set elements
 *
 *	@key: element key
236
 *	@key_end: closing element key
237
 *	@priv: element private data and extensions
238 239
 */
struct nft_set_elem {
240 241 242 243
	union {
		u32		buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)];
		struct nft_data	val;
	} key;
244 245 246 247
	union {
		u32		buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)];
		struct nft_data	val;
	} key_end;
248 249 250 251
	union {
		u32		buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)];
		struct nft_data val;
	} data;
252
	void			*priv;
253 254 255 256
};

struct nft_set;
struct nft_set_iter {
257
	u8		genmask;
258 259 260 261
	unsigned int	count;
	unsigned int	skip;
	int		err;
	int		(*fn)(const struct nft_ctx *ctx,
262
			      struct nft_set *set,
263
			      const struct nft_set_iter *iter,
264
			      struct nft_set_elem *elem);
265 266
};

267 268 269 270 271 272
/**
 *	struct nft_set_desc - description of set elements
 *
 *	@klen: key length
 *	@dlen: data length
 *	@size: number of set elements
273 274
 *	@field_len: length of each field in concatenation, bytes
 *	@field_count: number of concatenated fields in element
275
 *	@expr: set must support for expressions
276 277 278 279 280
 */
struct nft_set_desc {
	unsigned int		klen;
	unsigned int		dlen;
	unsigned int		size;
281 282
	u8			field_len[NFT_REG32_COUNT];
	u8			field_count;
283
	bool			expr;
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
};

/**
 *	enum nft_set_class - performance class
 *
 *	@NFT_LOOKUP_O_1: constant, O(1)
 *	@NFT_LOOKUP_O_LOG_N: logarithmic, O(log N)
 *	@NFT_LOOKUP_O_N: linear, O(N)
 */
enum nft_set_class {
	NFT_SET_CLASS_O_1,
	NFT_SET_CLASS_O_LOG_N,
	NFT_SET_CLASS_O_N,
};

/**
 *	struct nft_set_estimate - estimation of memory and performance
 *				  characteristics
 *
 *	@size: required memory
304
 *	@lookup: lookup performance class
305
 *	@space: memory class
306 307
 */
struct nft_set_estimate {
308
	u64			size;
309
	enum nft_set_class	lookup;
310
	enum nft_set_class	space;
311 312
};

313
struct nft_set_ext;
314
struct nft_expr;
315

316 317 318 319
/**
 *	struct nft_set_ops - nf_tables set operations
 *
 *	@lookup: look up an element within the set
320 321
 *	@update: update an element if exists, add it if doesn't exist
 *	@delete: delete an element
322
 *	@insert: insert new element into set
323
 *	@activate: activate new element in the next generation
324
 *	@deactivate: lookup for element and deactivate it in the next generation
325
 *	@flush: deactivate element in the next generation
326
 *	@remove: remove element from set
327
 *	@walk: iterate over all set elements
328
 *	@get: get set elements
329 330 331
 *	@privsize: function to return size of set private data
 *	@init: initialize private data of new set instance
 *	@destroy: destroy private data of set instance
332
 *	@elemsize: element private size
333 334 335 336
 *
 *	Operations lookup, update and delete have simpler interfaces, are faster
 *	and currently only used in the packet path. All the rest are slower,
 *	control plane functions.
337 338
 */
struct nft_set_ops {
339 340
	bool				(*lookup)(const struct net *net,
						  const struct nft_set *set,
341
						  const u32 *key,
342
						  const struct nft_set_ext **ext);
343
	bool				(*update)(struct nft_set *set,
344
						  const u32 *key,
345 346
						  void *(*new)(struct nft_set *,
							       const struct nft_expr *,
347
							       struct nft_regs *),
348
						  const struct nft_expr *expr,
349
						  struct nft_regs *regs,
350
						  const struct nft_set_ext **ext);
351 352
	bool				(*delete)(const struct nft_set *set,
						  const u32 *key);
353

354 355
	int				(*insert)(const struct net *net,
						  const struct nft_set *set,
356 357
						  const struct nft_set_elem *elem,
						  struct nft_set_ext **ext);
358 359
	void				(*activate)(const struct net *net,
						    const struct nft_set *set,
360
						    const struct nft_set_elem *elem);
361 362
	void *				(*deactivate)(const struct net *net,
						      const struct nft_set *set,
363
						      const struct nft_set_elem *elem);
364 365 366
	bool				(*flush)(const struct net *net,
						 const struct nft_set *set,
						 void *priv);
367 368
	void				(*remove)(const struct net *net,
						  const struct nft_set *set,
369 370
						  const struct nft_set_elem *elem);
	void				(*walk)(const struct nft_ctx *ctx,
371
						struct nft_set *set,
372
						struct nft_set_iter *iter);
373 374 375 376
	void *				(*get)(const struct net *net,
					       const struct nft_set *set,
					       const struct nft_set_elem *elem,
					       unsigned int flags);
377

378
	u64				(*privsize)(const struct nlattr * const nla[],
379
						    const struct nft_set_desc *desc);
380 381 382
	bool				(*estimate)(const struct nft_set_desc *desc,
						    u32 features,
						    struct nft_set_estimate *est);
383
	int				(*init)(const struct nft_set *set,
384
						const struct nft_set_desc *desc,
385 386
						const struct nlattr * const nla[]);
	void				(*destroy)(const struct nft_set *set);
387
	void				(*gc_init)(const struct nft_set *set);
388

389
	unsigned int			elemsize;
390 391 392 393 394 395 396 397 398 399
};

/**
 *      struct nft_set_type - nf_tables set type
 *
 *      @ops: set ops for this type
 *      @features: features supported by the implementation
 */
struct nft_set_type {
	const struct nft_set_ops	ops;
400 401
	u32				features;
};
402
#define to_set_type(o) container_of(o, struct nft_set_type, ops)
403 404 405 406 407 408

/**
 * 	struct nft_set - nf_tables set instance
 *
 *	@list: table set list node
 *	@bindings: list of set bindings
409 410
 *	@table: table this set belongs to
 *	@net: netnamespace this set belongs to
411
 * 	@name: name of the set
412
 *	@handle: unique handle of the set
413 414
 * 	@ktype: key type (numeric type defined by userspace, not used in the kernel)
 * 	@dtype: data type (verdict or numeric type defined by userspace)
415
 * 	@objtype: object type (see NFT_OBJECT_* definitions)
416
 * 	@size: maximum set size
417 418
 *	@field_len: length of each field in concatenation, bytes
 *	@field_count: number of concatenated fields in element
419
 *	@use: number of rules references to this set
420
 * 	@nelems: number of elements
421
 * 	@ndeact: number of deactivated elements queued for removal
422
 *	@timeout: default timeout value in jiffies
423
 * 	@gc_int: garbage collection interval in msecs
424
 *	@policy: set parameterization (see enum nft_set_policies)
425 426
 *	@udlen: user data length
 *	@udata: user data
427
 *	@expr: stateful expression
428 429
 * 	@ops: set ops
 * 	@flags: set flags
430
 *	@genmask: generation mask
431 432 433 434 435 436 437
 * 	@klen: key length
 * 	@dlen: data length
 * 	@data: private set data
 */
struct nft_set {
	struct list_head		list;
	struct list_head		bindings;
438 439
	struct nft_table		*table;
	possible_net_t			net;
440
	char				*name;
441
	u64				handle;
442 443
	u32				ktype;
	u32				dtype;
444
	u32				objtype;
445
	u32				size;
446 447
	u8				field_len[NFT_REG32_COUNT];
	u8				field_count;
448
	u32				use;
449 450
	atomic_t			nelems;
	u32				ndeact;
451 452
	u64				timeout;
	u32				gc_int;
453
	u16				policy;
454 455
	u16				udlen;
	unsigned char			*udata;
456
	struct nft_expr			*expr;
457 458
	/* runtime data below here */
	const struct nft_set_ops	*ops ____cacheline_aligned;
459
	u16				flags:14,
460
					genmask:2;
461 462 463 464 465 466
	u8				klen;
	u8				dlen;
	unsigned char			data[]
		__attribute__((aligned(__alignof__(u64))));
};

467 468 469 470 471
static inline bool nft_set_is_anonymous(const struct nft_set *set)
{
	return set->flags & NFT_SET_ANONYMOUS;
}

472 473 474 475 476
static inline void *nft_set_priv(const struct nft_set *set)
{
	return (void *)set->data;
}

477 478 479 480 481
static inline struct nft_set *nft_set_container_of(const void *priv)
{
	return (void *)priv - offsetof(struct nft_set, data);
}

482 483 484 485 486
struct nft_set *nft_set_lookup_global(const struct net *net,
				      const struct nft_table *table,
				      const struct nlattr *nla_set_name,
				      const struct nlattr *nla_set_id,
				      u8 genmask);
487

488 489 490 491 492
static inline unsigned long nft_set_gc_interval(const struct nft_set *set)
{
	return set->gc_int ? msecs_to_jiffies(set->gc_int) : HZ;
}

493 494 495 496 497
/**
 *	struct nft_set_binding - nf_tables set binding
 *
 *	@list: set bindings list node
 *	@chain: chain containing the rule bound to the set
498
 *	@flags: set action flags
499 500 501 502 503 504 505
 *
 *	A set binding contains all information necessary for validation
 *	of new elements added to a bound set.
 */
struct nft_set_binding {
	struct list_head		list;
	const struct nft_chain		*chain;
506
	u32				flags;
507 508
};

509
enum nft_trans_phase;
510
void nf_tables_activate_set(const struct nft_ctx *ctx, struct nft_set *set);
511 512 513
void nf_tables_deactivate_set(const struct nft_ctx *ctx, struct nft_set *set,
			      struct nft_set_binding *binding,
			      enum nft_trans_phase phase);
514 515
int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
		       struct nft_set_binding *binding);
516
void nf_tables_destroy_set(const struct nft_ctx *ctx, struct nft_set *set);
517

518 519 520 521
/**
 *	enum nft_set_extensions - set extension type IDs
 *
 *	@NFT_SET_EXT_KEY: element key
522
 *	@NFT_SET_EXT_KEY_END: upper bound element key, for ranges
523 524
 *	@NFT_SET_EXT_DATA: mapping data
 *	@NFT_SET_EXT_FLAGS: element flags
525 526
 *	@NFT_SET_EXT_TIMEOUT: element timeout
 *	@NFT_SET_EXT_EXPIRATION: element expiration time
527
 *	@NFT_SET_EXT_USERDATA: user data associated with the element
528
 *	@NFT_SET_EXT_EXPR: expression assiociated with the element
529
 *	@NFT_SET_EXT_OBJREF: stateful object reference associated with element
530 531 532 533
 *	@NFT_SET_EXT_NUM: number of extension types
 */
enum nft_set_extensions {
	NFT_SET_EXT_KEY,
534
	NFT_SET_EXT_KEY_END,
535 536
	NFT_SET_EXT_DATA,
	NFT_SET_EXT_FLAGS,
537 538
	NFT_SET_EXT_TIMEOUT,
	NFT_SET_EXT_EXPIRATION,
539
	NFT_SET_EXT_USERDATA,
540
	NFT_SET_EXT_EXPR,
541
	NFT_SET_EXT_OBJREF,
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
	NFT_SET_EXT_NUM
};

/**
 *	struct nft_set_ext_type - set extension type
 *
 * 	@len: fixed part length of the extension
 * 	@align: alignment requirements of the extension
 */
struct nft_set_ext_type {
	u8	len;
	u8	align;
};

extern const struct nft_set_ext_type nft_set_ext_types[];

/**
 *	struct nft_set_ext_tmpl - set extension template
 *
 *	@len: length of extension area
 *	@offset: offsets of individual extension types
 */
struct nft_set_ext_tmpl {
	u16	len;
	u8	offset[NFT_SET_EXT_NUM];
};

/**
 *	struct nft_set_ext - set extensions
 *
572
 *	@genmask: generation mask
573 574 575 576
 *	@offset: offsets of individual extension types
 *	@data: beginning of extension data
 */
struct nft_set_ext {
577
	u8	genmask;
578
	u8	offset[NFT_SET_EXT_NUM];
579
	char	data[];
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
};

static inline void nft_set_ext_prepare(struct nft_set_ext_tmpl *tmpl)
{
	memset(tmpl, 0, sizeof(*tmpl));
	tmpl->len = sizeof(struct nft_set_ext);
}

static inline void nft_set_ext_add_length(struct nft_set_ext_tmpl *tmpl, u8 id,
					  unsigned int len)
{
	tmpl->len	 = ALIGN(tmpl->len, nft_set_ext_types[id].align);
	BUG_ON(tmpl->len > U8_MAX);
	tmpl->offset[id] = tmpl->len;
	tmpl->len	+= nft_set_ext_types[id].len + len;
}

static inline void nft_set_ext_add(struct nft_set_ext_tmpl *tmpl, u8 id)
{
	nft_set_ext_add_length(tmpl, id, 0);
}

static inline void nft_set_ext_init(struct nft_set_ext *ext,
				    const struct nft_set_ext_tmpl *tmpl)
{
	memcpy(ext->offset, tmpl->offset, sizeof(ext->offset));
}

static inline bool __nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
{
	return !!ext->offset[id];
}

static inline bool nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
{
	return ext && __nft_set_ext_exists(ext, id);
}

static inline void *nft_set_ext(const struct nft_set_ext *ext, u8 id)
{
	return (void *)ext + ext->offset[id];
}

static inline struct nft_data *nft_set_ext_key(const struct nft_set_ext *ext)
{
	return nft_set_ext(ext, NFT_SET_EXT_KEY);
}

628 629 630 631 632
static inline struct nft_data *nft_set_ext_key_end(const struct nft_set_ext *ext)
{
	return nft_set_ext(ext, NFT_SET_EXT_KEY_END);
}

633 634 635 636 637 638 639 640 641
static inline struct nft_data *nft_set_ext_data(const struct nft_set_ext *ext)
{
	return nft_set_ext(ext, NFT_SET_EXT_DATA);
}

static inline u8 *nft_set_ext_flags(const struct nft_set_ext *ext)
{
	return nft_set_ext(ext, NFT_SET_EXT_FLAGS);
}
642

643 644 645 646 647
static inline u64 *nft_set_ext_timeout(const struct nft_set_ext *ext)
{
	return nft_set_ext(ext, NFT_SET_EXT_TIMEOUT);
}

648
static inline u64 *nft_set_ext_expiration(const struct nft_set_ext *ext)
649 650 651 652
{
	return nft_set_ext(ext, NFT_SET_EXT_EXPIRATION);
}

653 654 655 656 657
static inline struct nft_userdata *nft_set_ext_userdata(const struct nft_set_ext *ext)
{
	return nft_set_ext(ext, NFT_SET_EXT_USERDATA);
}

658 659 660 661 662
static inline struct nft_expr *nft_set_ext_expr(const struct nft_set_ext *ext)
{
	return nft_set_ext(ext, NFT_SET_EXT_EXPR);
}

663 664 665
static inline bool nft_set_elem_expired(const struct nft_set_ext *ext)
{
	return nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION) &&
666
	       time_is_before_eq_jiffies64(*nft_set_ext_expiration(ext));
667 668
}

669 670 671 672 673 674
static inline struct nft_set_ext *nft_set_elem_ext(const struct nft_set *set,
						   void *elem)
{
	return elem + set->ops->elemsize;
}

675 676 677 678 679
static inline struct nft_object **nft_set_ext_obj(const struct nft_set_ext *ext)
{
	return nft_set_ext(ext, NFT_SET_EXT_OBJREF);
}

680 681 682 683
struct nft_expr *nft_set_elem_expr_alloc(const struct nft_ctx *ctx,
					 const struct nft_set *set,
					 const struct nlattr *attr);

684 685
void *nft_set_elem_init(const struct nft_set *set,
			const struct nft_set_ext_tmpl *tmpl,
686
			const u32 *key, const u32 *key_end, const u32 *data,
687
			u64 timeout, u64 expiration, gfp_t gfp);
688 689
void nft_set_elem_destroy(const struct nft_set *set, void *elem,
			  bool destroy_expr);
690

691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
/**
 *	struct nft_set_gc_batch_head - nf_tables set garbage collection batch
 *
 *	@rcu: rcu head
 *	@set: set the elements belong to
 *	@cnt: count of elements
 */
struct nft_set_gc_batch_head {
	struct rcu_head			rcu;
	const struct nft_set		*set;
	unsigned int			cnt;
};

#define NFT_SET_GC_BATCH_SIZE	((PAGE_SIZE -				  \
				  sizeof(struct nft_set_gc_batch_head)) / \
				 sizeof(void *))

/**
 *	struct nft_set_gc_batch - nf_tables set garbage collection batch
 *
 * 	@head: GC batch head
 * 	@elems: garbage collection elements
 */
struct nft_set_gc_batch {
	struct nft_set_gc_batch_head	head;
	void				*elems[NFT_SET_GC_BATCH_SIZE];
};

struct nft_set_gc_batch *nft_set_gc_batch_alloc(const struct nft_set *set,
						gfp_t gfp);
void nft_set_gc_batch_release(struct rcu_head *rcu);

static inline void nft_set_gc_batch_complete(struct nft_set_gc_batch *gcb)
{
	if (gcb != NULL)
		call_rcu(&gcb->head.rcu, nft_set_gc_batch_release);
}

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)
{
	if (gcb != NULL) {
		if (gcb->head.cnt + 1 < ARRAY_SIZE(gcb->elems))
			return gcb;
		nft_set_gc_batch_complete(gcb);
	}
	return nft_set_gc_batch_alloc(set, gfp);
}

static inline void nft_set_gc_batch_add(struct nft_set_gc_batch *gcb,
					void *elem)
{
	gcb->elems[gcb->head.cnt++] = elem;
}

747
struct nft_expr_ops;
P
Patrick McHardy 已提交
748
/**
749
 *	struct nft_expr_type - nf_tables expression type
P
Patrick McHardy 已提交
750
 *
751
 *	@select_ops: function to select nft_expr_ops
752
 *	@release_ops: release nft_expr_ops
753
 *	@ops: default ops, used when no select_ops functions is present
P
Patrick McHardy 已提交
754 755 756 757 758
 *	@list: used internally
 *	@name: Identifier
 *	@owner: module reference
 *	@policy: netlink attribute policy
 *	@maxattr: highest netlink attribute number
759
 *	@family: address family for AF-specific types
760
 *	@flags: expression type flags
761 762
 */
struct nft_expr_type {
763 764
	const struct nft_expr_ops	*(*select_ops)(const struct nft_ctx *,
						       const struct nlattr * const tb[]);
765
	void				(*release_ops)(const struct nft_expr_ops *ops);
766 767 768 769 770 771
	const struct nft_expr_ops	*ops;
	struct list_head		list;
	const char			*name;
	struct module			*owner;
	const struct nla_policy		*policy;
	unsigned int			maxattr;
772
	u8				family;
773
	u8				flags;
774 775
};

776
#define NFT_EXPR_STATEFUL		0x1
777
#define NFT_EXPR_GC			0x2
778

779 780 781 782 783 784 785
enum nft_trans_phase {
	NFT_TRANS_PREPARE,
	NFT_TRANS_ABORT,
	NFT_TRANS_COMMIT,
	NFT_TRANS_RELEASE
};

786 787 788
struct nft_flow_rule;
struct nft_offload_ctx;

789 790 791 792
/**
 *	struct nft_expr_ops - nf_tables expression operations
 *
 *	@eval: Expression evaluation function
P
Patrick McHardy 已提交
793
 *	@size: full expression size, including private data size
794
 *	@init: initialization function
795 796 797
 *	@activate: activate expression in the next generation
 *	@deactivate: deactivate expression in next generation
 *	@destroy: destruction function, called after synchronize_rcu
798 799
 *	@dump: function to dump parameters
 *	@type: expression type
800 801
 *	@validate: validate expression, called during loop detection
 *	@data: extra data to attach to this expression operation
P
Patrick McHardy 已提交
802 803 804 805
 */
struct nft_expr;
struct nft_expr_ops {
	void				(*eval)(const struct nft_expr *expr,
806
						struct nft_regs *regs,
P
Patrick McHardy 已提交
807
						const struct nft_pktinfo *pkt);
808 809
	int				(*clone)(struct nft_expr *dst,
						 const struct nft_expr *src);
810 811
	unsigned int			size;

P
Patrick McHardy 已提交
812 813 814
	int				(*init)(const struct nft_ctx *ctx,
						const struct nft_expr *expr,
						const struct nlattr * const tb[]);
815 816 817
	void				(*activate)(const struct nft_ctx *ctx,
						    const struct nft_expr *expr);
	void				(*deactivate)(const struct nft_ctx *ctx,
818 819
						      const struct nft_expr *expr,
						      enum nft_trans_phase phase);
820 821
	void				(*destroy)(const struct nft_ctx *ctx,
						   const struct nft_expr *expr);
822 823
	void				(*destroy_clone)(const struct nft_ctx *ctx,
							 const struct nft_expr *expr);
P
Patrick McHardy 已提交
824 825
	int				(*dump)(struct sk_buff *skb,
						const struct nft_expr *expr);
826 827 828
	int				(*validate)(const struct nft_ctx *ctx,
						    const struct nft_expr *expr,
						    const struct nft_data **data);
829 830
	bool				(*gc)(struct net *net,
					      const struct nft_expr *expr);
831 832 833
	int				(*offload)(struct nft_offload_ctx *ctx,
						   struct nft_flow_rule *flow,
						   const struct nft_expr *expr);
834
	bool				(*offload_action)(const struct nft_expr *expr);
835
	const struct nft_expr_type	*type;
836
	void				*data;
P
Patrick McHardy 已提交
837 838
};

839
#define NFT_EXPR_MAXATTR		16
P
Patrick McHardy 已提交
840 841 842 843 844 845 846 847 848 849 850
#define NFT_EXPR_SIZE(size)		(sizeof(struct nft_expr) + \
					 ALIGN(size, __alignof__(struct nft_expr)))

/**
 *	struct nft_expr - nf_tables expression
 *
 *	@ops: expression ops
 *	@data: expression private data
 */
struct nft_expr {
	const struct nft_expr_ops	*ops;
851 852
	unsigned char			data[]
		__attribute__((aligned(__alignof__(u64))));
P
Patrick McHardy 已提交
853 854 855 856 857 858 859
};

static inline void *nft_expr_priv(const struct nft_expr *expr)
{
	return (void *)expr->data;
}

860
int nft_expr_clone(struct nft_expr *dst, struct nft_expr *src);
861 862 863 864
void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr);
int nft_expr_dump(struct sk_buff *skb, unsigned int attr,
		  const struct nft_expr *expr);

P
Patrick McHardy 已提交
865 866 867 868 869
/**
 *	struct nft_rule - nf_tables rule
 *
 *	@list: used internally
 *	@handle: rule handle
870
 *	@genmask: generation mask
P
Patrick McHardy 已提交
871
 *	@dlen: length of expression data
872
 *	@udata: user data is appended to the rule
P
Patrick McHardy 已提交
873 874 875 876
 *	@data: expression data
 */
struct nft_rule {
	struct list_head		list;
877
	u64				handle:42,
878
					genmask:2,
879
					dlen:12,
880
					udata:1;
P
Patrick McHardy 已提交
881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899
	unsigned char			data[]
		__attribute__((aligned(__alignof__(struct nft_expr))));
};

static inline struct nft_expr *nft_expr_first(const struct nft_rule *rule)
{
	return (struct nft_expr *)&rule->data[0];
}

static inline struct nft_expr *nft_expr_next(const struct nft_expr *expr)
{
	return ((void *)expr) + expr->ops->size;
}

static inline struct nft_expr *nft_expr_last(const struct nft_rule *rule)
{
	return (struct nft_expr *)&rule->data[rule->dlen];
}

900 901 902 903 904 905
static inline bool nft_expr_more(const struct nft_rule *rule,
				 const struct nft_expr *expr)
{
	return expr != nft_expr_last(rule) && expr->ops;
}

906
static inline struct nft_userdata *nft_userdata(const struct nft_rule *rule)
907 908 909 910
{
	return (void *)&rule->data[rule->dlen];
}

911 912
void nf_tables_rule_release(const struct nft_ctx *ctx, struct nft_rule *rule);

913 914 915 916 917 918
static inline void nft_set_elem_update_expr(const struct nft_set_ext *ext,
					    struct nft_regs *regs,
					    const struct nft_pktinfo *pkt)
{
	struct nft_expr *expr;

919
	if (__nft_set_ext_exists(ext, NFT_SET_EXT_EXPR)) {
920 921 922 923 924
		expr = nft_set_ext_expr(ext);
		expr->ops->eval(expr, regs, pkt);
	}
}

P
Patrick McHardy 已提交
925 926 927 928 929 930 931 932 933 934
/*
 * The last pointer isn't really necessary, but the compiler isn't able to
 * determine that the result of nft_expr_last() is always the same since it
 * can't assume that the dlen value wasn't changed within calls in the loop.
 */
#define nft_rule_for_each_expr(expr, last, rule) \
	for ((expr) = nft_expr_first(rule), (last) = nft_expr_last(rule); \
	     (expr) != (last); \
	     (expr) = nft_expr_next(expr))

935 936
#define NFT_CHAIN_POLICY_UNSET		U8_MAX

P
Patrick McHardy 已提交
937 938 939 940 941
/**
 *	struct nft_chain - nf_tables chain
 *
 *	@rules: list of rules in the chain
 *	@list: used internally
942
 *	@rhlhead: used internally
943
 *	@table: table that this chain belongs to
P
Patrick McHardy 已提交
944 945
 *	@handle: chain handle
 *	@use: number of jump references to this chain
946
 *	@flags: bitmask of enum nft_chain_flags
P
Patrick McHardy 已提交
947 948 949
 *	@name: name of the chain
 */
struct nft_chain {
950 951
	struct nft_rule			*__rcu *rules_gen_0;
	struct nft_rule			*__rcu *rules_gen_1;
P
Patrick McHardy 已提交
952 953
	struct list_head		rules;
	struct list_head		list;
954
	struct rhlist_head		rhlhead;
955
	struct nft_table		*table;
P
Patrick McHardy 已提交
956
	u64				handle;
957
	u32				use;
958 959
	u8				flags:5,
					bound:1,
960
					genmask:2;
961
	char				*name;
962 963
	u16				udlen;
	u8				*udata;
964 965 966

	/* Only used during control plane commit phase: */
	struct nft_rule			**rules_next;
P
Patrick McHardy 已提交
967 968
};

969 970
int nft_chain_validate(const struct nft_ctx *ctx, const struct nft_chain *chain);

971
enum nft_chain_types {
972 973 974 975 976 977
	NFT_CHAIN_T_DEFAULT = 0,
	NFT_CHAIN_T_ROUTE,
	NFT_CHAIN_T_NAT,
	NFT_CHAIN_T_MAX
};

978
/**
979
 * 	struct nft_chain_type - nf_tables chain type info
980 981 982 983 984 985
 *
 * 	@name: name of the type
 * 	@type: numeric identifier
 * 	@family: address family
 * 	@owner: module owner
 * 	@hook_mask: mask of valid hooks
986
 * 	@hooks: array of hook functions
987 988
 *	@ops_register: base chain register function
 *	@ops_unregister: base chain unregister function
989
 */
990
struct nft_chain_type {
991
	const char			*name;
992
	enum nft_chain_types		type;
993 994 995
	int				family;
	struct module			*owner;
	unsigned int			hook_mask;
996
	nf_hookfn			*hooks[NFT_MAX_HOOKS];
997 998
	int				(*ops_register)(struct net *net, const struct nf_hook_ops *ops);
	void				(*ops_unregister)(struct net *net, const struct nf_hook_ops *ops);
999 1000
};

1001
int nft_chain_validate_dependency(const struct nft_chain *chain,
1002
				  enum nft_chain_types type);
1003 1004
int nft_chain_validate_hooks(const struct nft_chain *chain,
                             unsigned int hook_flags);
1005

1006 1007 1008 1009 1010 1011 1012 1013
static inline bool nft_chain_is_bound(struct nft_chain *chain)
{
	return (chain->flags & NFT_CHAIN_BINDING) && chain->bound;
}

void nft_chain_del(struct nft_chain *chain);
void nf_tables_chain_destroy(struct nft_ctx *ctx);

1014
struct nft_stats {
1015 1016 1017
	u64			bytes;
	u64			pkts;
	struct u64_stats_sync	syncp;
1018 1019
};

1020 1021 1022 1023 1024 1025
struct nft_hook {
	struct list_head	list;
	struct nf_hook_ops	ops;
	struct rcu_head		rcu;
};

P
Patrick McHardy 已提交
1026 1027 1028 1029
/**
 *	struct nft_base_chain - nf_tables base chain
 *
 *	@ops: netfilter hook ops
1030
 *	@hook_list: list of netfilter hooks (for NFPROTO_NETDEV family)
1031
 *	@type: chain type
1032 1033
 *	@policy: default policy
 *	@stats: per-cpu chain stats
P
Patrick McHardy 已提交
1034
 *	@chain: the chain
1035
 *	@flow_block: flow block (for hardware offload)
P
Patrick McHardy 已提交
1036 1037
 */
struct nft_base_chain {
1038
	struct nf_hook_ops		ops;
1039
	struct list_head		hook_list;
1040
	const struct nft_chain_type	*type;
1041
	u8				policy;
1042
	u8				flags;
1043
	struct nft_stats __percpu	*stats;
P
Patrick McHardy 已提交
1044
	struct nft_chain		chain;
1045
	struct flow_block		flow_block;
P
Patrick McHardy 已提交
1046 1047 1048 1049 1050 1051 1052
};

static inline struct nft_base_chain *nft_base_chain(const struct nft_chain *chain)
{
	return container_of(chain, struct nft_base_chain, chain);
}

1053 1054
static inline bool nft_is_base_chain(const struct nft_chain *chain)
{
1055
	return chain->flags & NFT_CHAIN_BASE;
1056 1057
}

1058
int __nft_release_basechain(struct nft_ctx *ctx);
1059

1060
unsigned int nft_do_chain(struct nft_pktinfo *pkt, void *priv);
P
Patrick McHardy 已提交
1061 1062 1063 1064 1065

/**
 *	struct nft_table - nf_tables table
 *
 *	@list: used internally
1066 1067
 *	@chains_ht: chains in the table
 *	@chains: same, for stable walks
P
Patrick McHardy 已提交
1068
 *	@sets: sets in the table
1069
 *	@objects: stateful objects in the table
1070
 *	@flowtables: flow tables in the table
P
Patrick McHardy 已提交
1071
 *	@hgenerator: handle generator state
1072
 *	@handle: table handle
P
Patrick McHardy 已提交
1073 1074
 *	@use: number of chain references to this table
 *	@flags: table flag (see enum nft_table_flags)
1075
 *	@genmask: generation mask
1076
 *	@afinfo: address family info
P
Patrick McHardy 已提交
1077 1078 1079 1080
 *	@name: name of the table
 */
struct nft_table {
	struct list_head		list;
1081
	struct rhltable			chains_ht;
P
Patrick McHardy 已提交
1082 1083
	struct list_head		chains;
	struct list_head		sets;
1084
	struct list_head		objects;
1085
	struct list_head		flowtables;
P
Patrick McHardy 已提交
1086
	u64				hgenerator;
1087
	u64				handle;
P
Patrick McHardy 已提交
1088
	u32				use;
1089 1090
	u16				family:6,
					flags:8,
1091
					genmask:2;
1092
	char				*name;
1093 1094
	u16				udlen;
	u8				*udata;
1095 1096
};

1097 1098 1099 1100 1101 1102
static inline bool nft_base_chain_netdev(int family, u32 hooknum)
{
	return family == NFPROTO_NETDEV ||
	       (family == NFPROTO_INET && hooknum == NF_INET_INGRESS);
}

1103
void nft_register_chain_type(const struct nft_chain_type *);
1104
void nft_unregister_chain_type(const struct nft_chain_type *);
P
Patrick McHardy 已提交
1105

1106 1107
int nft_register_expr(struct nft_expr_type *);
void nft_unregister_expr(struct nft_expr_type *);
P
Patrick McHardy 已提交
1108

1109 1110 1111
int nft_verdict_dump(struct sk_buff *skb, int type,
		     const struct nft_verdict *v);

1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
/**
 *	struct nft_object_hash_key - key to lookup nft_object
 *
 *	@name: name of the stateful object to look up
 *	@table: table the object belongs to
 */
struct nft_object_hash_key {
	const char                      *name;
	const struct nft_table          *table;
};

1123 1124 1125 1126
/**
 *	struct nft_object - nf_tables stateful object
 *
 *	@list: table stateful object list node
1127
 *	@key:  keys that identify this object
1128
 *	@rhlhead: nft_objname_ht node
1129 1130
 *	@genmask: generation mask
 *	@use: number of references to this stateful object
1131
 *	@handle: unique object handle
1132
 *	@ops: object operations
1133
 *	@data: object data, layout depends on type
1134 1135 1136
 */
struct nft_object {
	struct list_head		list;
1137
	struct rhlist_head		rhlhead;
1138
	struct nft_object_hash_key	key;
1139 1140
	u32				genmask:2,
					use:30;
1141
	u64				handle;
1142 1143
	u16				udlen;
	u8				*udata;
1144
	/* runtime data below here */
1145
	const struct nft_object_ops	*ops ____cacheline_aligned;
1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156
	unsigned char			data[]
		__attribute__((aligned(__alignof__(u64))));
};

static inline void *nft_obj_data(const struct nft_object *obj)
{
	return (void *)obj->data;
}

#define nft_expr_obj(expr)	*((struct nft_object **)nft_expr_priv(expr))

1157 1158
struct nft_object *nft_obj_lookup(const struct net *net,
				  const struct nft_table *table,
1159 1160
				  const struct nlattr *nla, u32 objtype,
				  u8 genmask);
1161

1162
void nft_obj_notify(struct net *net, const struct nft_table *table,
1163 1164
		    struct nft_object *obj, u32 portid, u32 seq,
		    int event, int family, int report, gfp_t gfp);
1165

1166 1167 1168
/**
 *	struct nft_object_type - stateful object type
 *
1169 1170
 *	@select_ops: function to select nft_object_ops
 *	@ops: default ops, used when no select_ops functions is present
1171 1172 1173 1174 1175
 *	@list: list node in list of object types
 *	@type: stateful object numeric type
 *	@owner: module owner
 *	@maxattr: maximum netlink attribute
 *	@policy: netlink attribute policy
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192
 */
struct nft_object_type {
	const struct nft_object_ops	*(*select_ops)(const struct nft_ctx *,
						       const struct nlattr * const tb[]);
	const struct nft_object_ops	*ops;
	struct list_head		list;
	u32				type;
	unsigned int                    maxattr;
	struct module			*owner;
	const struct nla_policy		*policy;
};

/**
 *	struct nft_object_ops - stateful object operations
 *
 *	@eval: stateful object evaluation function
 *	@size: stateful object size
1193 1194 1195
 *	@init: initialize object from netlink attributes
 *	@destroy: release existing stateful object
 *	@dump: netlink dump stateful object
1196
 *	@update: update stateful object
1197
 */
1198
struct nft_object_ops {
1199 1200 1201 1202
	void				(*eval)(struct nft_object *obj,
						struct nft_regs *regs,
						const struct nft_pktinfo *pkt);
	unsigned int			size;
1203 1204
	int				(*init)(const struct nft_ctx *ctx,
						const struct nlattr *const tb[],
1205
						struct nft_object *obj);
1206 1207
	void				(*destroy)(const struct nft_ctx *ctx,
						   struct nft_object *obj);
1208
	int				(*dump)(struct sk_buff *skb,
1209 1210
						struct nft_object *obj,
						bool reset);
1211 1212
	void				(*update)(struct nft_object *obj,
						  struct nft_object *newobj);
1213
	const struct nft_object_type	*type;
1214 1215 1216 1217 1218
};

int nft_register_obj(struct nft_object_type *obj_type);
void nft_unregister_obj(struct nft_object_type *obj_type);

1219
#define NFT_NETDEVICE_MAX	256
1220

1221 1222 1223 1224 1225 1226 1227 1228 1229 1230
/**
 *	struct nft_flowtable - nf_tables flow table
 *
 *	@list: flow table list node in table list
 * 	@table: the table the flow table is contained in
 *	@name: name of this flow table
 *	@hooknum: hook number
 *	@ops_len: number of hooks in array
 *	@genmask: generation mask
 *	@use: number of references to this flow table
1231
 * 	@handle: unique object handle
1232
 *	@dev_name: array of device names
1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243
 *	@data: rhashtable and garbage collector
 * 	@ops: array of hooks
 */
struct nft_flowtable {
	struct list_head		list;
	struct nft_table		*table;
	char				*name;
	int				hooknum;
	int				ops_len;
	u32				genmask:2,
					use:30;
1244
	u64				handle;
1245
	/* runtime data below here */
1246
	struct list_head		hook_list ____cacheline_aligned;
1247 1248 1249
	struct nf_flowtable		data;
};

1250 1251 1252
struct nft_flowtable *nft_flowtable_lookup(const struct nft_table *table,
					   const struct nlattr *nla,
					   u8 genmask);
1253

1254 1255 1256 1257
void nf_tables_deactivate_flowtable(const struct nft_ctx *ctx,
				    struct nft_flowtable *flowtable,
				    enum nft_trans_phase phase);

1258 1259 1260
void nft_register_flowtable_type(struct nf_flowtable_type *type);
void nft_unregister_flowtable_type(struct nf_flowtable_type *type);

1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289
/**
 *	struct nft_traceinfo - nft tracing information and state
 *
 *	@pkt: pktinfo currently processed
 *	@basechain: base chain currently processed
 *	@chain: chain currently processed
 *	@rule:  rule that was evaluated
 *	@verdict: verdict given by rule
 *	@type: event type (enum nft_trace_types)
 *	@packet_dumped: packet headers sent in a previous traceinfo message
 *	@trace: other struct members are initialised
 */
struct nft_traceinfo {
	const struct nft_pktinfo	*pkt;
	const struct nft_base_chain	*basechain;
	const struct nft_chain		*chain;
	const struct nft_rule		*rule;
	const struct nft_verdict	*verdict;
	enum nft_trace_types		type;
	bool				packet_dumped;
	bool				trace;
};

void nft_trace_init(struct nft_traceinfo *info, const struct nft_pktinfo *pkt,
		    const struct nft_verdict *verdict,
		    const struct nft_chain *basechain);

void nft_trace_notify(struct nft_traceinfo *info);

1290 1291
#define MODULE_ALIAS_NFT_CHAIN(family, name) \
	MODULE_ALIAS("nft-chain-" __stringify(family) "-" name)
P
Patrick McHardy 已提交
1292

1293 1294 1295
#define MODULE_ALIAS_NFT_AF_EXPR(family, name) \
	MODULE_ALIAS("nft-expr-" __stringify(family) "-" name)

P
Patrick McHardy 已提交
1296 1297 1298
#define MODULE_ALIAS_NFT_EXPR(name) \
	MODULE_ALIAS("nft-expr-" name)

1299 1300 1301
#define MODULE_ALIAS_NFT_OBJ(type) \
	MODULE_ALIAS("nft-obj-" __stringify(type))

1302 1303
#if IS_ENABLED(CONFIG_NF_TABLES)

1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327
/*
 * The gencursor defines two generations, the currently active and the
 * next one. Objects contain a bitmask of 2 bits specifying the generations
 * they're active in. A set bit means they're inactive in the generation
 * represented by that bit.
 *
 * New objects start out as inactive in the current and active in the
 * next generation. When committing the ruleset the bitmask is cleared,
 * meaning they're active in all generations. When removing an object,
 * it is set inactive in the next generation. After committing the ruleset,
 * the objects are removed.
 */
static inline unsigned int nft_gencursor_next(const struct net *net)
{
	return net->nft.gencursor + 1 == 1 ? 1 : 0;
}

static inline u8 nft_genmask_next(const struct net *net)
{
	return 1 << nft_gencursor_next(net);
}

static inline u8 nft_genmask_cur(const struct net *net)
{
1328 1329
	/* Use READ_ONCE() to prevent refetching the value for atomicity */
	return 1 << READ_ONCE(net->nft.gencursor);
1330 1331
}

1332 1333
#define NFT_GENMASK_ANY		((1 << 0) | (1 << 1))

1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356
/*
 * Generic transaction helpers
 */

/* Check if this object is currently active. */
#define nft_is_active(__net, __obj)				\
	(((__obj)->genmask & nft_genmask_cur(__net)) == 0)

/* Check if this object is active in the next generation. */
#define nft_is_active_next(__net, __obj)			\
	(((__obj)->genmask & nft_genmask_next(__net)) == 0)

/* This object becomes active in the next generation. */
#define nft_activate_next(__net, __obj)				\
	(__obj)->genmask = nft_genmask_cur(__net)

/* This object becomes inactive in the next generation. */
#define nft_deactivate_next(__net, __obj)			\
        (__obj)->genmask = nft_genmask_next(__net)

/* After committing the ruleset, clear the stale generation bit. */
#define nft_clear(__net, __obj)					\
	(__obj)->genmask &= ~nft_genmask_next(__net)
1357 1358
#define nft_active_genmask(__obj, __genmask)			\
	!((__obj)->genmask & __genmask)
1359

1360 1361 1362 1363 1364 1365 1366 1367 1368 1369
/*
 * Set element transaction helpers
 */

static inline bool nft_set_elem_active(const struct nft_set_ext *ext,
				       u8 genmask)
{
	return !(ext->genmask & genmask);
}

1370 1371
static inline void nft_set_elem_change_active(const struct net *net,
					      const struct nft_set *set,
1372 1373
					      struct nft_set_ext *ext)
{
1374
	ext->genmask ^= nft_genmask_next(net);
1375 1376
}

1377 1378
#endif /* IS_ENABLED(CONFIG_NF_TABLES) */

1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413
/*
 * We use a free bit in the genmask field to indicate the element
 * is busy, meaning it is currently being processed either by
 * the netlink API or GC.
 *
 * Even though the genmask is only a single byte wide, this works
 * because the extension structure if fully constant once initialized,
 * so there are no non-atomic write accesses unless it is already
 * marked busy.
 */
#define NFT_SET_ELEM_BUSY_MASK	(1 << 2)

#if defined(__LITTLE_ENDIAN_BITFIELD)
#define NFT_SET_ELEM_BUSY_BIT	2
#elif defined(__BIG_ENDIAN_BITFIELD)
#define NFT_SET_ELEM_BUSY_BIT	(BITS_PER_LONG - BITS_PER_BYTE + 2)
#else
#error
#endif

static inline int nft_set_elem_mark_busy(struct nft_set_ext *ext)
{
	unsigned long *word = (unsigned long *)ext;

	BUILD_BUG_ON(offsetof(struct nft_set_ext, genmask) != 0);
	return test_and_set_bit(NFT_SET_ELEM_BUSY_BIT, word);
}

static inline void nft_set_elem_clear_busy(struct nft_set_ext *ext)
{
	unsigned long *word = (unsigned long *)ext;

	clear_bit(NFT_SET_ELEM_BUSY_BIT, word);
}

1414 1415 1416 1417 1418
/**
 *	struct nft_trans - nf_tables object update in transaction
 *
 *	@list: used internally
 *	@msg_type: message type
1419
 *	@put_net: ctx->net needs to be put
1420 1421 1422 1423 1424 1425
 *	@ctx: transaction context
 *	@data: internal information related to the transaction
 */
struct nft_trans {
	struct list_head		list;
	int				msg_type;
1426
	bool				put_net;
1427
	struct nft_ctx			ctx;
1428
	char				data[];
1429 1430 1431 1432
};

struct nft_trans_rule {
	struct nft_rule			*rule;
1433
	struct nft_flow_rule		*flow;
1434
	u32				rule_id;
1435 1436 1437 1438
};

#define nft_trans_rule(trans)	\
	(((struct nft_trans_rule *)trans->data)->rule)
1439 1440
#define nft_trans_flow_rule(trans)	\
	(((struct nft_trans_rule *)trans->data)->flow)
1441 1442
#define nft_trans_rule_id(trans)	\
	(((struct nft_trans_rule *)trans->data)->rule_id)
1443 1444 1445 1446

struct nft_trans_set {
	struct nft_set			*set;
	u32				set_id;
1447
	bool				bound;
1448 1449 1450 1451 1452 1453
};

#define nft_trans_set(trans)	\
	(((struct nft_trans_set *)trans->data)->set)
#define nft_trans_set_id(trans)	\
	(((struct nft_trans_set *)trans->data)->set_id)
1454 1455
#define nft_trans_set_bound(trans)	\
	(((struct nft_trans_set *)trans->data)->bound)
1456 1457 1458

struct nft_trans_chain {
	bool				update;
1459
	char				*name;
1460 1461
	struct nft_stats __percpu	*stats;
	u8				policy;
1462
	u32				chain_id;
1463 1464 1465 1466 1467 1468 1469 1470 1471 1472
};

#define nft_trans_chain_update(trans)	\
	(((struct nft_trans_chain *)trans->data)->update)
#define nft_trans_chain_name(trans)	\
	(((struct nft_trans_chain *)trans->data)->name)
#define nft_trans_chain_stats(trans)	\
	(((struct nft_trans_chain *)trans->data)->stats)
#define nft_trans_chain_policy(trans)	\
	(((struct nft_trans_chain *)trans->data)->policy)
1473 1474
#define nft_trans_chain_id(trans)	\
	(((struct nft_trans_chain *)trans->data)->chain_id)
1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488

struct nft_trans_table {
	bool				update;
	bool				enable;
};

#define nft_trans_table_update(trans)	\
	(((struct nft_trans_table *)trans->data)->update)
#define nft_trans_table_enable(trans)	\
	(((struct nft_trans_table *)trans->data)->enable)

struct nft_trans_elem {
	struct nft_set			*set;
	struct nft_set_elem		elem;
1489
	bool				bound;
1490 1491 1492 1493 1494 1495
};

#define nft_trans_elem_set(trans)	\
	(((struct nft_trans_elem *)trans->data)->set)
#define nft_trans_elem(trans)	\
	(((struct nft_trans_elem *)trans->data)->elem)
1496 1497
#define nft_trans_elem_set_bound(trans)	\
	(((struct nft_trans_elem *)trans->data)->bound)
1498

1499 1500
struct nft_trans_obj {
	struct nft_object		*obj;
1501 1502
	struct nft_object		*newobj;
	bool				update;
1503 1504 1505 1506
};

#define nft_trans_obj(trans)	\
	(((struct nft_trans_obj *)trans->data)->obj)
1507 1508 1509 1510
#define nft_trans_obj_newobj(trans) \
	(((struct nft_trans_obj *)trans->data)->newobj)
#define nft_trans_obj_update(trans)	\
	(((struct nft_trans_obj *)trans->data)->update)
1511

1512 1513
struct nft_trans_flowtable {
	struct nft_flowtable		*flowtable;
1514 1515
	bool				update;
	struct list_head		hook_list;
1516
	u32				flags;
1517 1518 1519 1520
};

#define nft_trans_flowtable(trans)	\
	(((struct nft_trans_flowtable *)trans->data)->flowtable)
1521 1522 1523 1524
#define nft_trans_flowtable_update(trans)	\
	(((struct nft_trans_flowtable *)trans->data)->update)
#define nft_trans_flowtable_hooks(trans)	\
	(((struct nft_trans_flowtable *)trans->data)->hook_list)
1525 1526
#define nft_trans_flowtable_flags(trans)	\
	(((struct nft_trans_flowtable *)trans->data)->flags)
1527

1528
int __init nft_chain_filter_init(void);
1529
void nft_chain_filter_fini(void);
1530

1531 1532
void __init nft_chain_route_init(void);
void nft_chain_route_fini(void);
1533 1534

void nf_tables_trans_destroy_flush_work(void);
1535 1536 1537 1538

int nf_msecs_to_jiffies64(const struct nlattr *nla, u64 *result);
__be64 nf_jiffies64_to_msecs(u64 input);

P
Patrick McHardy 已提交
1539
#endif /* _NET_NF_TABLES_H */