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