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