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