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 /* placeholder structure for opaque set element backend representation. */
279 struct nft_elem_priv { };
280
281 /**
282 * struct nft_set_elem - generic representation of set elements
283 *
284 * @key: element key
285 * @key_end: closing element key
286 * @data: element data
287 * @priv: element private data and extensions
288 */
289 struct nft_set_elem {
290 union {
291 u32 buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)];
292 struct nft_data val;
293 } key;
294 union {
295 u32 buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)];
296 struct nft_data val;
297 } key_end;
298 union {
299 u32 buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)];
300 struct nft_data val;
301 } data;
302 struct nft_elem_priv *priv;
303 };
304
nft_elem_priv_cast(const struct nft_elem_priv * priv)305 static inline void *nft_elem_priv_cast(const struct nft_elem_priv *priv)
306 {
307 return (void *)priv;
308 }
309
310 struct nft_set;
311 struct nft_set_iter {
312 u8 genmask;
313 unsigned int count;
314 unsigned int skip;
315 int err;
316 int (*fn)(const struct nft_ctx *ctx,
317 struct nft_set *set,
318 const struct nft_set_iter *iter,
319 struct nft_elem_priv *elem_priv);
320 };
321
322 /**
323 * struct nft_set_desc - description of set elements
324 *
325 * @ktype: key type
326 * @klen: key length
327 * @dtype: data type
328 * @dlen: data length
329 * @objtype: object type
330 * @size: number of set elements
331 * @policy: set policy
332 * @gc_int: garbage collector interval
333 * @timeout: element timeout
334 * @field_len: length of each field in concatenation, bytes
335 * @field_count: number of concatenated fields in element
336 * @expr: set must support for expressions
337 */
338 struct nft_set_desc {
339 u32 ktype;
340 unsigned int klen;
341 u32 dtype;
342 unsigned int dlen;
343 u32 objtype;
344 unsigned int size;
345 u32 policy;
346 u32 gc_int;
347 u64 timeout;
348 u8 field_len[NFT_REG32_COUNT];
349 u8 field_count;
350 bool expr;
351 };
352
353 /**
354 * enum nft_set_class - performance class
355 *
356 * @NFT_SET_CLASS_O_1: constant, O(1)
357 * @NFT_SET_CLASS_O_LOG_N: logarithmic, O(log N)
358 * @NFT_SET_CLASS_O_N: linear, O(N)
359 */
360 enum nft_set_class {
361 NFT_SET_CLASS_O_1,
362 NFT_SET_CLASS_O_LOG_N,
363 NFT_SET_CLASS_O_N,
364 };
365
366 /**
367 * struct nft_set_estimate - estimation of memory and performance
368 * characteristics
369 *
370 * @size: required memory
371 * @lookup: lookup performance class
372 * @space: memory class
373 */
374 struct nft_set_estimate {
375 u64 size;
376 enum nft_set_class lookup;
377 enum nft_set_class space;
378 };
379
380 #define NFT_EXPR_MAXATTR 16
381 #define NFT_EXPR_SIZE(size) (sizeof(struct nft_expr) + \
382 ALIGN(size, __alignof__(struct nft_expr)))
383
384 /**
385 * struct nft_expr - nf_tables expression
386 *
387 * @ops: expression ops
388 * @data: expression private data
389 */
390 struct nft_expr {
391 const struct nft_expr_ops *ops;
392 unsigned char data[]
393 __attribute__((aligned(__alignof__(u64))));
394 };
395
nft_expr_priv(const struct nft_expr * expr)396 static inline void *nft_expr_priv(const struct nft_expr *expr)
397 {
398 return (void *)expr->data;
399 }
400
401 struct nft_expr_info;
402
403 int nft_expr_inner_parse(const struct nft_ctx *ctx, const struct nlattr *nla,
404 struct nft_expr_info *info);
405 int nft_expr_clone(struct nft_expr *dst, struct nft_expr *src);
406 void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr);
407 int nft_expr_dump(struct sk_buff *skb, unsigned int attr,
408 const struct nft_expr *expr, bool reset);
409 bool nft_expr_reduce_bitwise(struct nft_regs_track *track,
410 const struct nft_expr *expr);
411
412 struct nft_set_ext;
413
414 /**
415 * struct nft_set_ops - nf_tables set operations
416 *
417 * @lookup: look up an element within the set
418 * @update: update an element if exists, add it if doesn't exist
419 * @delete: delete an element
420 * @insert: insert new element into set
421 * @activate: activate new element in the next generation
422 * @deactivate: lookup for element and deactivate it in the next generation
423 * @flush: deactivate element in the next generation
424 * @remove: remove element from set
425 * @walk: iterate over all set elements
426 * @get: get set elements
427 * @commit: commit set elements
428 * @abort: abort set elements
429 * @privsize: function to return size of set private data
430 * @estimate: estimate the required memory size and the lookup complexity class
431 * @init: initialize private data of new set instance
432 * @destroy: destroy private data of set instance
433 * @gc_init: initialize garbage collection
434 * @elemsize: element private size
435 *
436 * Operations lookup, update and delete have simpler interfaces, are faster
437 * and currently only used in the packet path. All the rest are slower,
438 * control plane functions.
439 */
440 struct nft_set_ops {
441 bool (*lookup)(const struct net *net,
442 const struct nft_set *set,
443 const u32 *key,
444 const struct nft_set_ext **ext);
445 bool (*update)(struct nft_set *set,
446 const u32 *key,
447 struct nft_elem_priv *
448 (*new)(struct nft_set *,
449 const struct nft_expr *,
450 struct nft_regs *),
451 const struct nft_expr *expr,
452 struct nft_regs *regs,
453 const struct nft_set_ext **ext);
454 bool (*delete)(const struct nft_set *set,
455 const u32 *key);
456
457 int (*insert)(const struct net *net,
458 const struct nft_set *set,
459 const struct nft_set_elem *elem,
460 struct nft_elem_priv **priv);
461 void (*activate)(const struct net *net,
462 const struct nft_set *set,
463 struct nft_elem_priv *elem_priv);
464 struct nft_elem_priv * (*deactivate)(const struct net *net,
465 const struct nft_set *set,
466 const struct nft_set_elem *elem);
467 void (*flush)(const struct net *net,
468 const struct nft_set *set,
469 struct nft_elem_priv *priv);
470 void (*remove)(const struct net *net,
471 const struct nft_set *set,
472 struct nft_elem_priv *elem_priv);
473 void (*walk)(const struct nft_ctx *ctx,
474 struct nft_set *set,
475 struct nft_set_iter *iter);
476 struct nft_elem_priv * (*get)(const struct net *net,
477 const struct nft_set *set,
478 const struct nft_set_elem *elem,
479 unsigned int flags);
480 void (*commit)(struct nft_set *set);
481 void (*abort)(const struct nft_set *set);
482 u64 (*privsize)(const struct nlattr * const nla[],
483 const struct nft_set_desc *desc);
484 bool (*estimate)(const struct nft_set_desc *desc,
485 u32 features,
486 struct nft_set_estimate *est);
487 int (*init)(const struct nft_set *set,
488 const struct nft_set_desc *desc,
489 const struct nlattr * const nla[]);
490 void (*destroy)(const struct nft_ctx *ctx,
491 const struct nft_set *set);
492 void (*gc_init)(const struct nft_set *set);
493
494 unsigned int elemsize;
495 };
496
497 /**
498 * struct nft_set_type - nf_tables set type
499 *
500 * @ops: set ops for this type
501 * @features: features supported by the implementation
502 */
503 struct nft_set_type {
504 const struct nft_set_ops ops;
505 u32 features;
506 };
507 #define to_set_type(o) container_of(o, struct nft_set_type, ops)
508
509 struct nft_set_elem_expr {
510 u8 size;
511 unsigned char data[]
512 __attribute__((aligned(__alignof__(struct nft_expr))));
513 };
514
515 #define nft_setelem_expr_at(__elem_expr, __offset) \
516 ((struct nft_expr *)&__elem_expr->data[__offset])
517
518 #define nft_setelem_expr_foreach(__expr, __elem_expr, __size) \
519 for (__expr = nft_setelem_expr_at(__elem_expr, 0), __size = 0; \
520 __size < (__elem_expr)->size; \
521 __size += (__expr)->ops->size, __expr = ((void *)(__expr)) + (__expr)->ops->size)
522
523 #define NFT_SET_EXPR_MAX 2
524
525 /**
526 * struct nft_set - nf_tables set instance
527 *
528 * @list: table set list node
529 * @bindings: list of set bindings
530 * @refs: internal refcounting for async set destruction
531 * @table: table this set belongs to
532 * @net: netnamespace this set belongs to
533 * @name: name of the set
534 * @handle: unique handle of the set
535 * @ktype: key type (numeric type defined by userspace, not used in the kernel)
536 * @dtype: data type (verdict or numeric type defined by userspace)
537 * @objtype: object type (see NFT_OBJECT_* definitions)
538 * @size: maximum set size
539 * @field_len: length of each field in concatenation, bytes
540 * @field_count: number of concatenated fields in element
541 * @use: number of rules references to this set
542 * @nelems: number of elements
543 * @ndeact: number of deactivated elements queued for removal
544 * @timeout: default timeout value in jiffies
545 * @gc_int: garbage collection interval in msecs
546 * @policy: set parameterization (see enum nft_set_policies)
547 * @udlen: user data length
548 * @udata: user data
549 * @pending_update: list of pending update set element
550 * @ops: set ops
551 * @flags: set flags
552 * @dead: set will be freed, never cleared
553 * @genmask: generation mask
554 * @klen: key length
555 * @dlen: data length
556 * @num_exprs: numbers of exprs
557 * @exprs: stateful expression
558 * @catchall_list: list of catch-all set element
559 * @data: private set data
560 */
561 struct nft_set {
562 struct list_head list;
563 struct list_head bindings;
564 refcount_t refs;
565 struct nft_table *table;
566 possible_net_t net;
567 char *name;
568 u64 handle;
569 u32 ktype;
570 u32 dtype;
571 u32 objtype;
572 u32 size;
573 u8 field_len[NFT_REG32_COUNT];
574 u8 field_count;
575 u32 use;
576 atomic_t nelems;
577 u32 ndeact;
578 u64 timeout;
579 u32 gc_int;
580 u16 policy;
581 u16 udlen;
582 unsigned char *udata;
583 struct list_head pending_update;
584 /* runtime data below here */
585 const struct nft_set_ops *ops ____cacheline_aligned;
586 u16 flags:13,
587 dead:1,
588 genmask:2;
589 u8 klen;
590 u8 dlen;
591 u8 num_exprs;
592 struct nft_expr *exprs[NFT_SET_EXPR_MAX];
593 struct list_head catchall_list;
594 unsigned char data[]
595 __attribute__((aligned(__alignof__(u64))));
596 };
597
nft_set_is_anonymous(const struct nft_set * set)598 static inline bool nft_set_is_anonymous(const struct nft_set *set)
599 {
600 return set->flags & NFT_SET_ANONYMOUS;
601 }
602
nft_set_priv(const struct nft_set * set)603 static inline void *nft_set_priv(const struct nft_set *set)
604 {
605 return (void *)set->data;
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 * @ext_len: length of the expected extension(used to sanity check)
705 */
706 struct nft_set_ext_tmpl {
707 u16 len;
708 u8 offset[NFT_SET_EXT_NUM];
709 u8 ext_len[NFT_SET_EXT_NUM];
710 };
711
712 /**
713 * struct nft_set_ext - set extensions
714 *
715 * @genmask: generation mask
716 * @offset: offsets of individual extension types
717 * @data: beginning of extension data
718 */
719 struct nft_set_ext {
720 u8 genmask;
721 u8 offset[NFT_SET_EXT_NUM];
722 char data[];
723 };
724
nft_set_ext_prepare(struct nft_set_ext_tmpl * tmpl)725 static inline void nft_set_ext_prepare(struct nft_set_ext_tmpl *tmpl)
726 {
727 memset(tmpl, 0, sizeof(*tmpl));
728 tmpl->len = sizeof(struct nft_set_ext);
729 }
730
nft_set_ext_add_length(struct nft_set_ext_tmpl * tmpl,u8 id,unsigned int len)731 static inline int nft_set_ext_add_length(struct nft_set_ext_tmpl *tmpl, u8 id,
732 unsigned int len)
733 {
734 tmpl->len = ALIGN(tmpl->len, nft_set_ext_types[id].align);
735 if (tmpl->len > U8_MAX)
736 return -EINVAL;
737
738 tmpl->offset[id] = tmpl->len;
739 tmpl->ext_len[id] = nft_set_ext_types[id].len + len;
740 tmpl->len += tmpl->ext_len[id];
741
742 return 0;
743 }
744
nft_set_ext_add(struct nft_set_ext_tmpl * tmpl,u8 id)745 static inline int nft_set_ext_add(struct nft_set_ext_tmpl *tmpl, u8 id)
746 {
747 return nft_set_ext_add_length(tmpl, id, 0);
748 }
749
nft_set_ext_init(struct nft_set_ext * ext,const struct nft_set_ext_tmpl * tmpl)750 static inline void nft_set_ext_init(struct nft_set_ext *ext,
751 const struct nft_set_ext_tmpl *tmpl)
752 {
753 memcpy(ext->offset, tmpl->offset, sizeof(ext->offset));
754 }
755
__nft_set_ext_exists(const struct nft_set_ext * ext,u8 id)756 static inline bool __nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
757 {
758 return !!ext->offset[id];
759 }
760
nft_set_ext_exists(const struct nft_set_ext * ext,u8 id)761 static inline bool nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
762 {
763 return ext && __nft_set_ext_exists(ext, id);
764 }
765
nft_set_ext(const struct nft_set_ext * ext,u8 id)766 static inline void *nft_set_ext(const struct nft_set_ext *ext, u8 id)
767 {
768 return (void *)ext + ext->offset[id];
769 }
770
nft_set_ext_key(const struct nft_set_ext * ext)771 static inline struct nft_data *nft_set_ext_key(const struct nft_set_ext *ext)
772 {
773 return nft_set_ext(ext, NFT_SET_EXT_KEY);
774 }
775
nft_set_ext_key_end(const struct nft_set_ext * ext)776 static inline struct nft_data *nft_set_ext_key_end(const struct nft_set_ext *ext)
777 {
778 return nft_set_ext(ext, NFT_SET_EXT_KEY_END);
779 }
780
nft_set_ext_data(const struct nft_set_ext * ext)781 static inline struct nft_data *nft_set_ext_data(const struct nft_set_ext *ext)
782 {
783 return nft_set_ext(ext, NFT_SET_EXT_DATA);
784 }
785
nft_set_ext_flags(const struct nft_set_ext * ext)786 static inline u8 *nft_set_ext_flags(const struct nft_set_ext *ext)
787 {
788 return nft_set_ext(ext, NFT_SET_EXT_FLAGS);
789 }
790
nft_set_ext_timeout(const struct nft_set_ext * ext)791 static inline u64 *nft_set_ext_timeout(const struct nft_set_ext *ext)
792 {
793 return nft_set_ext(ext, NFT_SET_EXT_TIMEOUT);
794 }
795
nft_set_ext_expiration(const struct nft_set_ext * ext)796 static inline u64 *nft_set_ext_expiration(const struct nft_set_ext *ext)
797 {
798 return nft_set_ext(ext, NFT_SET_EXT_EXPIRATION);
799 }
800
nft_set_ext_userdata(const struct nft_set_ext * ext)801 static inline struct nft_userdata *nft_set_ext_userdata(const struct nft_set_ext *ext)
802 {
803 return nft_set_ext(ext, NFT_SET_EXT_USERDATA);
804 }
805
nft_set_ext_expr(const struct nft_set_ext * ext)806 static inline struct nft_set_elem_expr *nft_set_ext_expr(const struct nft_set_ext *ext)
807 {
808 return nft_set_ext(ext, NFT_SET_EXT_EXPRESSIONS);
809 }
810
__nft_set_elem_expired(const struct nft_set_ext * ext,u64 tstamp)811 static inline bool __nft_set_elem_expired(const struct nft_set_ext *ext,
812 u64 tstamp)
813 {
814 return nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION) &&
815 time_after_eq64(tstamp, *nft_set_ext_expiration(ext));
816 }
817
nft_set_elem_expired(const struct nft_set_ext * ext)818 static inline bool nft_set_elem_expired(const struct nft_set_ext *ext)
819 {
820 return __nft_set_elem_expired(ext, get_jiffies_64());
821 }
822
nft_set_elem_ext(const struct nft_set * set,const struct nft_elem_priv * elem_priv)823 static inline struct nft_set_ext *nft_set_elem_ext(const struct nft_set *set,
824 const struct nft_elem_priv *elem_priv)
825 {
826 return (void *)elem_priv + set->ops->elemsize;
827 }
828
nft_set_ext_obj(const struct nft_set_ext * ext)829 static inline struct nft_object **nft_set_ext_obj(const struct nft_set_ext *ext)
830 {
831 return nft_set_ext(ext, NFT_SET_EXT_OBJREF);
832 }
833
834 struct nft_expr *nft_set_elem_expr_alloc(const struct nft_ctx *ctx,
835 const struct nft_set *set,
836 const struct nlattr *attr);
837
838 struct nft_elem_priv *nft_set_elem_init(const struct nft_set *set,
839 const struct nft_set_ext_tmpl *tmpl,
840 const u32 *key, const u32 *key_end,
841 const u32 *data,
842 u64 timeout, u64 expiration, gfp_t gfp);
843 int nft_set_elem_expr_clone(const struct nft_ctx *ctx, struct nft_set *set,
844 struct nft_expr *expr_array[]);
845 void nft_set_elem_destroy(const struct nft_set *set,
846 const struct nft_elem_priv *elem_priv,
847 bool destroy_expr);
848 void nf_tables_set_elem_destroy(const struct nft_ctx *ctx,
849 const struct nft_set *set,
850 const struct nft_elem_priv *elem_priv);
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 * @blob_next: rule blob pointer to the next in the chain
1084 */
1085 struct nft_chain {
1086 struct nft_rule_blob __rcu *blob_gen_0;
1087 struct nft_rule_blob __rcu *blob_gen_1;
1088 struct list_head rules;
1089 struct list_head list;
1090 struct rhlist_head rhlhead;
1091 struct nft_table *table;
1092 u64 handle;
1093 u32 use;
1094 u8 flags:5,
1095 bound:1,
1096 genmask:2;
1097 char *name;
1098 u16 udlen;
1099 u8 *udata;
1100
1101 /* Only used during control plane commit phase: */
1102 struct nft_rule_blob *blob_next;
1103 };
1104
1105 int nft_chain_validate(const struct nft_ctx *ctx, const struct nft_chain *chain);
1106 int nft_setelem_validate(const struct nft_ctx *ctx, struct nft_set *set,
1107 const struct nft_set_iter *iter,
1108 struct nft_elem_priv *elem_priv);
1109 int nft_set_catchall_validate(const struct nft_ctx *ctx, struct nft_set *set);
1110 int nf_tables_bind_chain(const struct nft_ctx *ctx, struct nft_chain *chain);
1111 void nf_tables_unbind_chain(const struct nft_ctx *ctx, struct nft_chain *chain);
1112
1113 enum nft_chain_types {
1114 NFT_CHAIN_T_DEFAULT = 0,
1115 NFT_CHAIN_T_ROUTE,
1116 NFT_CHAIN_T_NAT,
1117 NFT_CHAIN_T_MAX
1118 };
1119
1120 /**
1121 * struct nft_chain_type - nf_tables chain type info
1122 *
1123 * @name: name of the type
1124 * @type: numeric identifier
1125 * @family: address family
1126 * @owner: module owner
1127 * @hook_mask: mask of valid hooks
1128 * @hooks: array of hook functions
1129 * @ops_register: base chain register function
1130 * @ops_unregister: base chain unregister function
1131 */
1132 struct nft_chain_type {
1133 const char *name;
1134 enum nft_chain_types type;
1135 int family;
1136 struct module *owner;
1137 unsigned int hook_mask;
1138 nf_hookfn *hooks[NFT_MAX_HOOKS];
1139 int (*ops_register)(struct net *net, const struct nf_hook_ops *ops);
1140 void (*ops_unregister)(struct net *net, const struct nf_hook_ops *ops);
1141 };
1142
1143 int nft_chain_validate_dependency(const struct nft_chain *chain,
1144 enum nft_chain_types type);
1145 int nft_chain_validate_hooks(const struct nft_chain *chain,
1146 unsigned int hook_flags);
1147
nft_chain_binding(const struct nft_chain * chain)1148 static inline bool nft_chain_binding(const struct nft_chain *chain)
1149 {
1150 return chain->flags & NFT_CHAIN_BINDING;
1151 }
1152
nft_chain_is_bound(struct nft_chain * chain)1153 static inline bool nft_chain_is_bound(struct nft_chain *chain)
1154 {
1155 return (chain->flags & NFT_CHAIN_BINDING) && chain->bound;
1156 }
1157
1158 int nft_chain_add(struct nft_table *table, struct nft_chain *chain);
1159 void nft_chain_del(struct nft_chain *chain);
1160 void nf_tables_chain_destroy(struct nft_ctx *ctx);
1161
1162 struct nft_stats {
1163 u64 bytes;
1164 u64 pkts;
1165 struct u64_stats_sync syncp;
1166 };
1167
1168 struct nft_hook {
1169 struct list_head list;
1170 struct nf_hook_ops ops;
1171 struct rcu_head rcu;
1172 };
1173
1174 /**
1175 * struct nft_base_chain - nf_tables base chain
1176 *
1177 * @ops: netfilter hook ops
1178 * @hook_list: list of netfilter hooks (for NFPROTO_NETDEV family)
1179 * @type: chain type
1180 * @policy: default policy
1181 * @flags: indicate the base chain disabled or not
1182 * @stats: per-cpu chain stats
1183 * @chain: the chain
1184 * @flow_block: flow block (for hardware offload)
1185 */
1186 struct nft_base_chain {
1187 struct nf_hook_ops ops;
1188 struct list_head hook_list;
1189 const struct nft_chain_type *type;
1190 u8 policy;
1191 u8 flags;
1192 struct nft_stats __percpu *stats;
1193 struct nft_chain chain;
1194 struct flow_block flow_block;
1195 };
1196
nft_base_chain(const struct nft_chain * chain)1197 static inline struct nft_base_chain *nft_base_chain(const struct nft_chain *chain)
1198 {
1199 return container_of(chain, struct nft_base_chain, chain);
1200 }
1201
nft_is_base_chain(const struct nft_chain * chain)1202 static inline bool nft_is_base_chain(const struct nft_chain *chain)
1203 {
1204 return chain->flags & NFT_CHAIN_BASE;
1205 }
1206
1207 int __nft_release_basechain(struct nft_ctx *ctx);
1208
1209 unsigned int nft_do_chain(struct nft_pktinfo *pkt, void *priv);
1210
nft_use_inc(u32 * use)1211 static inline bool nft_use_inc(u32 *use)
1212 {
1213 if (*use == UINT_MAX)
1214 return false;
1215
1216 (*use)++;
1217
1218 return true;
1219 }
1220
nft_use_dec(u32 * use)1221 static inline void nft_use_dec(u32 *use)
1222 {
1223 WARN_ON_ONCE((*use)-- == 0);
1224 }
1225
1226 /* For error and abort path: restore use counter to previous state. */
nft_use_inc_restore(u32 * use)1227 static inline void nft_use_inc_restore(u32 *use)
1228 {
1229 WARN_ON_ONCE(!nft_use_inc(use));
1230 }
1231
1232 #define nft_use_dec_restore nft_use_dec
1233
1234 /**
1235 * struct nft_table - nf_tables table
1236 *
1237 * @list: used internally
1238 * @chains_ht: chains in the table
1239 * @chains: same, for stable walks
1240 * @sets: sets in the table
1241 * @objects: stateful objects in the table
1242 * @flowtables: flow tables in the table
1243 * @hgenerator: handle generator state
1244 * @handle: table handle
1245 * @use: number of chain references to this table
1246 * @family:address family
1247 * @flags: table flag (see enum nft_table_flags)
1248 * @genmask: generation mask
1249 * @nlpid: netlink port ID
1250 * @name: name of the table
1251 * @udlen: length of the user data
1252 * @udata: user data
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 u64 hgenerator;
1263 u64 handle;
1264 u32 use;
1265 u16 family:6,
1266 flags:8,
1267 genmask:2;
1268 u32 nlpid;
1269 char *name;
1270 u16 udlen;
1271 u8 *udata;
1272 u8 validate_state;
1273 };
1274
nft_table_has_owner(const struct nft_table * table)1275 static inline bool nft_table_has_owner(const struct nft_table *table)
1276 {
1277 return table->flags & NFT_TABLE_F_OWNER;
1278 }
1279
nft_base_chain_netdev(int family,u32 hooknum)1280 static inline bool nft_base_chain_netdev(int family, u32 hooknum)
1281 {
1282 return family == NFPROTO_NETDEV ||
1283 (family == NFPROTO_INET && hooknum == NF_INET_INGRESS);
1284 }
1285
1286 void nft_register_chain_type(const struct nft_chain_type *);
1287 void nft_unregister_chain_type(const struct nft_chain_type *);
1288
1289 int nft_register_expr(struct nft_expr_type *);
1290 void nft_unregister_expr(struct nft_expr_type *);
1291
1292 int nft_verdict_dump(struct sk_buff *skb, int type,
1293 const struct nft_verdict *v);
1294
1295 /**
1296 * struct nft_object_hash_key - key to lookup nft_object
1297 *
1298 * @name: name of the stateful object to look up
1299 * @table: table the object belongs to
1300 */
1301 struct nft_object_hash_key {
1302 const char *name;
1303 const struct nft_table *table;
1304 };
1305
1306 /**
1307 * struct nft_object - nf_tables stateful object
1308 *
1309 * @list: table stateful object list node
1310 * @rhlhead: nft_objname_ht node
1311 * @key: keys that identify this object
1312 * @genmask: generation mask
1313 * @use: number of references to this stateful object
1314 * @handle: unique object handle
1315 * @udlen: length of user data
1316 * @udata: user data
1317 * @ops: object operations
1318 * @data: object data, layout depends on type
1319 */
1320 struct nft_object {
1321 struct list_head list;
1322 struct rhlist_head rhlhead;
1323 struct nft_object_hash_key key;
1324 u32 genmask:2;
1325 u32 use;
1326 u64 handle;
1327 u16 udlen;
1328 u8 *udata;
1329 /* runtime data below here */
1330 const struct nft_object_ops *ops ____cacheline_aligned;
1331 unsigned char data[]
1332 __attribute__((aligned(__alignof__(u64))));
1333 };
1334
nft_obj_data(const struct nft_object * obj)1335 static inline void *nft_obj_data(const struct nft_object *obj)
1336 {
1337 return (void *)obj->data;
1338 }
1339
1340 #define nft_expr_obj(expr) *((struct nft_object **)nft_expr_priv(expr))
1341
1342 struct nft_object *nft_obj_lookup(const struct net *net,
1343 const struct nft_table *table,
1344 const struct nlattr *nla, u32 objtype,
1345 u8 genmask);
1346
1347 void nft_obj_notify(struct net *net, const struct nft_table *table,
1348 struct nft_object *obj, u32 portid, u32 seq,
1349 int event, u16 flags, int family, int report, gfp_t gfp);
1350
1351 /**
1352 * struct nft_object_type - stateful object type
1353 *
1354 * @select_ops: function to select nft_object_ops
1355 * @ops: default ops, used when no select_ops functions is present
1356 * @list: list node in list of object types
1357 * @type: stateful object numeric type
1358 * @owner: module owner
1359 * @maxattr: maximum netlink attribute
1360 * @family: address family for AF-specific object types
1361 * @policy: netlink attribute policy
1362 */
1363 struct nft_object_type {
1364 const struct nft_object_ops *(*select_ops)(const struct nft_ctx *,
1365 const struct nlattr * const tb[]);
1366 const struct nft_object_ops *ops;
1367 struct list_head list;
1368 u32 type;
1369 unsigned int maxattr;
1370 u8 family;
1371 struct module *owner;
1372 const struct nla_policy *policy;
1373 };
1374
1375 /**
1376 * struct nft_object_ops - stateful object operations
1377 *
1378 * @eval: stateful object evaluation function
1379 * @size: stateful object size
1380 * @init: initialize object from netlink attributes
1381 * @destroy: release existing stateful object
1382 * @dump: netlink dump stateful object
1383 * @update: update stateful object
1384 * @type: pointer to object type
1385 */
1386 struct nft_object_ops {
1387 void (*eval)(struct nft_object *obj,
1388 struct nft_regs *regs,
1389 const struct nft_pktinfo *pkt);
1390 unsigned int size;
1391 int (*init)(const struct nft_ctx *ctx,
1392 const struct nlattr *const tb[],
1393 struct nft_object *obj);
1394 void (*destroy)(const struct nft_ctx *ctx,
1395 struct nft_object *obj);
1396 int (*dump)(struct sk_buff *skb,
1397 struct nft_object *obj,
1398 bool reset);
1399 void (*update)(struct nft_object *obj,
1400 struct nft_object *newobj);
1401 const struct nft_object_type *type;
1402 };
1403
1404 int nft_register_obj(struct nft_object_type *obj_type);
1405 void nft_unregister_obj(struct nft_object_type *obj_type);
1406
1407 #define NFT_NETDEVICE_MAX 256
1408
1409 /**
1410 * struct nft_flowtable - nf_tables flow table
1411 *
1412 * @list: flow table list node in table list
1413 * @table: the table the flow table is contained in
1414 * @name: name of this flow table
1415 * @hooknum: hook number
1416 * @ops_len: number of hooks in array
1417 * @genmask: generation mask
1418 * @use: number of references to this flow table
1419 * @handle: unique object handle
1420 * @hook_list: hook list for hooks per net_device in flowtables
1421 * @data: rhashtable and garbage collector
1422 */
1423 struct nft_flowtable {
1424 struct list_head list;
1425 struct nft_table *table;
1426 char *name;
1427 int hooknum;
1428 int ops_len;
1429 u32 genmask:2;
1430 u32 use;
1431 u64 handle;
1432 /* runtime data below here */
1433 struct list_head hook_list ____cacheline_aligned;
1434 struct nf_flowtable data;
1435 };
1436
1437 struct nft_flowtable *nft_flowtable_lookup(const struct nft_table *table,
1438 const struct nlattr *nla,
1439 u8 genmask);
1440
1441 void nf_tables_deactivate_flowtable(const struct nft_ctx *ctx,
1442 struct nft_flowtable *flowtable,
1443 enum nft_trans_phase phase);
1444
1445 void nft_register_flowtable_type(struct nf_flowtable_type *type);
1446 void nft_unregister_flowtable_type(struct nf_flowtable_type *type);
1447
1448 /**
1449 * struct nft_traceinfo - nft tracing information and state
1450 *
1451 * @trace: other struct members are initialised
1452 * @nf_trace: copy of skb->nf_trace before rule evaluation
1453 * @type: event type (enum nft_trace_types)
1454 * @skbid: hash of skb to be used as trace id
1455 * @packet_dumped: packet headers sent in a previous traceinfo message
1456 * @basechain: base chain currently processed
1457 */
1458 struct nft_traceinfo {
1459 bool trace;
1460 bool nf_trace;
1461 bool packet_dumped;
1462 enum nft_trace_types type:8;
1463 u32 skbid;
1464 const struct nft_base_chain *basechain;
1465 };
1466
1467 void nft_trace_init(struct nft_traceinfo *info, const struct nft_pktinfo *pkt,
1468 const struct nft_chain *basechain);
1469
1470 void nft_trace_notify(const struct nft_pktinfo *pkt,
1471 const struct nft_verdict *verdict,
1472 const struct nft_rule_dp *rule,
1473 struct nft_traceinfo *info);
1474
1475 #define MODULE_ALIAS_NFT_CHAIN(family, name) \
1476 MODULE_ALIAS("nft-chain-" __stringify(family) "-" name)
1477
1478 #define MODULE_ALIAS_NFT_AF_EXPR(family, name) \
1479 MODULE_ALIAS("nft-expr-" __stringify(family) "-" name)
1480
1481 #define MODULE_ALIAS_NFT_EXPR(name) \
1482 MODULE_ALIAS("nft-expr-" name)
1483
1484 #define MODULE_ALIAS_NFT_OBJ(type) \
1485 MODULE_ALIAS("nft-obj-" __stringify(type))
1486
1487 #if IS_ENABLED(CONFIG_NF_TABLES)
1488
1489 /*
1490 * The gencursor defines two generations, the currently active and the
1491 * next one. Objects contain a bitmask of 2 bits specifying the generations
1492 * they're active in. A set bit means they're inactive in the generation
1493 * represented by that bit.
1494 *
1495 * New objects start out as inactive in the current and active in the
1496 * next generation. When committing the ruleset the bitmask is cleared,
1497 * meaning they're active in all generations. When removing an object,
1498 * it is set inactive in the next generation. After committing the ruleset,
1499 * the objects are removed.
1500 */
nft_gencursor_next(const struct net * net)1501 static inline unsigned int nft_gencursor_next(const struct net *net)
1502 {
1503 return net->nft.gencursor + 1 == 1 ? 1 : 0;
1504 }
1505
nft_genmask_next(const struct net * net)1506 static inline u8 nft_genmask_next(const struct net *net)
1507 {
1508 return 1 << nft_gencursor_next(net);
1509 }
1510
nft_genmask_cur(const struct net * net)1511 static inline u8 nft_genmask_cur(const struct net *net)
1512 {
1513 /* Use READ_ONCE() to prevent refetching the value for atomicity */
1514 return 1 << READ_ONCE(net->nft.gencursor);
1515 }
1516
1517 #define NFT_GENMASK_ANY ((1 << 0) | (1 << 1))
1518
1519 /*
1520 * Generic transaction helpers
1521 */
1522
1523 /* Check if this object is currently active. */
1524 #define nft_is_active(__net, __obj) \
1525 (((__obj)->genmask & nft_genmask_cur(__net)) == 0)
1526
1527 /* Check if this object is active in the next generation. */
1528 #define nft_is_active_next(__net, __obj) \
1529 (((__obj)->genmask & nft_genmask_next(__net)) == 0)
1530
1531 /* This object becomes active in the next generation. */
1532 #define nft_activate_next(__net, __obj) \
1533 (__obj)->genmask = nft_genmask_cur(__net)
1534
1535 /* This object becomes inactive in the next generation. */
1536 #define nft_deactivate_next(__net, __obj) \
1537 (__obj)->genmask = nft_genmask_next(__net)
1538
1539 /* After committing the ruleset, clear the stale generation bit. */
1540 #define nft_clear(__net, __obj) \
1541 (__obj)->genmask &= ~nft_genmask_next(__net)
1542 #define nft_active_genmask(__obj, __genmask) \
1543 !((__obj)->genmask & __genmask)
1544
1545 /*
1546 * Set element transaction helpers
1547 */
1548
nft_set_elem_active(const struct nft_set_ext * ext,u8 genmask)1549 static inline bool nft_set_elem_active(const struct nft_set_ext *ext,
1550 u8 genmask)
1551 {
1552 return !(ext->genmask & genmask);
1553 }
1554
nft_set_elem_change_active(const struct net * net,const struct nft_set * set,struct nft_set_ext * ext)1555 static inline void nft_set_elem_change_active(const struct net *net,
1556 const struct nft_set *set,
1557 struct nft_set_ext *ext)
1558 {
1559 ext->genmask ^= nft_genmask_next(net);
1560 }
1561
1562 #endif /* IS_ENABLED(CONFIG_NF_TABLES) */
1563
1564 #define NFT_SET_ELEM_DEAD_MASK (1 << 2)
1565
1566 #if defined(__LITTLE_ENDIAN_BITFIELD)
1567 #define NFT_SET_ELEM_DEAD_BIT 2
1568 #elif defined(__BIG_ENDIAN_BITFIELD)
1569 #define NFT_SET_ELEM_DEAD_BIT (BITS_PER_LONG - BITS_PER_BYTE + 2)
1570 #else
1571 #error
1572 #endif
1573
nft_set_elem_dead(struct nft_set_ext * ext)1574 static inline void nft_set_elem_dead(struct nft_set_ext *ext)
1575 {
1576 unsigned long *word = (unsigned long *)ext;
1577
1578 BUILD_BUG_ON(offsetof(struct nft_set_ext, genmask) != 0);
1579 set_bit(NFT_SET_ELEM_DEAD_BIT, word);
1580 }
1581
nft_set_elem_is_dead(const struct nft_set_ext * ext)1582 static inline int nft_set_elem_is_dead(const struct nft_set_ext *ext)
1583 {
1584 unsigned long *word = (unsigned long *)ext;
1585
1586 BUILD_BUG_ON(offsetof(struct nft_set_ext, genmask) != 0);
1587 return test_bit(NFT_SET_ELEM_DEAD_BIT, word);
1588 }
1589
1590 /**
1591 * struct nft_trans - nf_tables object update in transaction
1592 *
1593 * @list: used internally
1594 * @binding_list: list of objects with possible bindings
1595 * @msg_type: message type
1596 * @put_net: ctx->net needs to be put
1597 * @ctx: transaction context
1598 * @data: internal information related to the transaction
1599 */
1600 struct nft_trans {
1601 struct list_head list;
1602 struct list_head binding_list;
1603 int msg_type;
1604 bool put_net;
1605 struct nft_ctx ctx;
1606 char data[];
1607 };
1608
1609 struct nft_trans_rule {
1610 struct nft_rule *rule;
1611 struct nft_flow_rule *flow;
1612 u32 rule_id;
1613 bool bound;
1614 };
1615
1616 #define nft_trans_rule(trans) \
1617 (((struct nft_trans_rule *)trans->data)->rule)
1618 #define nft_trans_flow_rule(trans) \
1619 (((struct nft_trans_rule *)trans->data)->flow)
1620 #define nft_trans_rule_id(trans) \
1621 (((struct nft_trans_rule *)trans->data)->rule_id)
1622 #define nft_trans_rule_bound(trans) \
1623 (((struct nft_trans_rule *)trans->data)->bound)
1624
1625 struct nft_trans_set {
1626 struct nft_set *set;
1627 u32 set_id;
1628 u32 gc_int;
1629 u64 timeout;
1630 bool update;
1631 bool bound;
1632 u32 size;
1633 };
1634
1635 #define nft_trans_set(trans) \
1636 (((struct nft_trans_set *)trans->data)->set)
1637 #define nft_trans_set_id(trans) \
1638 (((struct nft_trans_set *)trans->data)->set_id)
1639 #define nft_trans_set_bound(trans) \
1640 (((struct nft_trans_set *)trans->data)->bound)
1641 #define nft_trans_set_update(trans) \
1642 (((struct nft_trans_set *)trans->data)->update)
1643 #define nft_trans_set_timeout(trans) \
1644 (((struct nft_trans_set *)trans->data)->timeout)
1645 #define nft_trans_set_gc_int(trans) \
1646 (((struct nft_trans_set *)trans->data)->gc_int)
1647 #define nft_trans_set_size(trans) \
1648 (((struct nft_trans_set *)trans->data)->size)
1649
1650 struct nft_trans_chain {
1651 struct nft_chain *chain;
1652 bool update;
1653 char *name;
1654 struct nft_stats __percpu *stats;
1655 u8 policy;
1656 bool bound;
1657 u32 chain_id;
1658 struct nft_base_chain *basechain;
1659 struct list_head hook_list;
1660 };
1661
1662 #define nft_trans_chain(trans) \
1663 (((struct nft_trans_chain *)trans->data)->chain)
1664 #define nft_trans_chain_update(trans) \
1665 (((struct nft_trans_chain *)trans->data)->update)
1666 #define nft_trans_chain_name(trans) \
1667 (((struct nft_trans_chain *)trans->data)->name)
1668 #define nft_trans_chain_stats(trans) \
1669 (((struct nft_trans_chain *)trans->data)->stats)
1670 #define nft_trans_chain_policy(trans) \
1671 (((struct nft_trans_chain *)trans->data)->policy)
1672 #define nft_trans_chain_bound(trans) \
1673 (((struct nft_trans_chain *)trans->data)->bound)
1674 #define nft_trans_chain_id(trans) \
1675 (((struct nft_trans_chain *)trans->data)->chain_id)
1676 #define nft_trans_basechain(trans) \
1677 (((struct nft_trans_chain *)trans->data)->basechain)
1678 #define nft_trans_chain_hooks(trans) \
1679 (((struct nft_trans_chain *)trans->data)->hook_list)
1680
1681 struct nft_trans_table {
1682 bool update;
1683 };
1684
1685 #define nft_trans_table_update(trans) \
1686 (((struct nft_trans_table *)trans->data)->update)
1687
1688 struct nft_trans_elem {
1689 struct nft_set *set;
1690 struct nft_elem_priv *elem_priv;
1691 bool bound;
1692 };
1693
1694 #define nft_trans_elem_set(trans) \
1695 (((struct nft_trans_elem *)trans->data)->set)
1696 #define nft_trans_elem_priv(trans) \
1697 (((struct nft_trans_elem *)trans->data)->elem_priv)
1698 #define nft_trans_elem_set_bound(trans) \
1699 (((struct nft_trans_elem *)trans->data)->bound)
1700
1701 struct nft_trans_obj {
1702 struct nft_object *obj;
1703 struct nft_object *newobj;
1704 bool update;
1705 };
1706
1707 #define nft_trans_obj(trans) \
1708 (((struct nft_trans_obj *)trans->data)->obj)
1709 #define nft_trans_obj_newobj(trans) \
1710 (((struct nft_trans_obj *)trans->data)->newobj)
1711 #define nft_trans_obj_update(trans) \
1712 (((struct nft_trans_obj *)trans->data)->update)
1713
1714 struct nft_trans_flowtable {
1715 struct nft_flowtable *flowtable;
1716 bool update;
1717 struct list_head hook_list;
1718 u32 flags;
1719 };
1720
1721 #define nft_trans_flowtable(trans) \
1722 (((struct nft_trans_flowtable *)trans->data)->flowtable)
1723 #define nft_trans_flowtable_update(trans) \
1724 (((struct nft_trans_flowtable *)trans->data)->update)
1725 #define nft_trans_flowtable_hooks(trans) \
1726 (((struct nft_trans_flowtable *)trans->data)->hook_list)
1727 #define nft_trans_flowtable_flags(trans) \
1728 (((struct nft_trans_flowtable *)trans->data)->flags)
1729
1730 #define NFT_TRANS_GC_BATCHCOUNT 256
1731
1732 struct nft_trans_gc {
1733 struct list_head list;
1734 struct net *net;
1735 struct nft_set *set;
1736 u32 seq;
1737 u16 count;
1738 struct nft_elem_priv *priv[NFT_TRANS_GC_BATCHCOUNT];
1739 struct rcu_head rcu;
1740 };
1741
1742 struct nft_trans_gc *nft_trans_gc_alloc(struct nft_set *set,
1743 unsigned int gc_seq, gfp_t gfp);
1744 void nft_trans_gc_destroy(struct nft_trans_gc *trans);
1745
1746 struct nft_trans_gc *nft_trans_gc_queue_async(struct nft_trans_gc *gc,
1747 unsigned int gc_seq, gfp_t gfp);
1748 void nft_trans_gc_queue_async_done(struct nft_trans_gc *gc);
1749
1750 struct nft_trans_gc *nft_trans_gc_queue_sync(struct nft_trans_gc *gc, gfp_t gfp);
1751 void nft_trans_gc_queue_sync_done(struct nft_trans_gc *trans);
1752
1753 void nft_trans_gc_elem_add(struct nft_trans_gc *gc, void *priv);
1754
1755 struct nft_trans_gc *nft_trans_gc_catchall_async(struct nft_trans_gc *gc,
1756 unsigned int gc_seq);
1757 struct nft_trans_gc *nft_trans_gc_catchall_sync(struct nft_trans_gc *gc);
1758
1759 void nft_setelem_data_deactivate(const struct net *net,
1760 const struct nft_set *set,
1761 struct nft_elem_priv *elem_priv);
1762
1763 int __init nft_chain_filter_init(void);
1764 void nft_chain_filter_fini(void);
1765
1766 void __init nft_chain_route_init(void);
1767 void nft_chain_route_fini(void);
1768
1769 void nf_tables_trans_destroy_flush_work(void);
1770
1771 int nf_msecs_to_jiffies64(const struct nlattr *nla, u64 *result);
1772 __be64 nf_jiffies64_to_msecs(u64 input);
1773
1774 #ifdef CONFIG_MODULES
1775 __printf(2, 3) int nft_request_module(struct net *net, const char *fmt, ...);
1776 #else
nft_request_module(struct net * net,const char * fmt,...)1777 static inline int nft_request_module(struct net *net, const char *fmt, ...) { return -ENOENT; }
1778 #endif
1779
1780 struct nftables_pernet {
1781 struct list_head tables;
1782 struct list_head commit_list;
1783 struct list_head binding_list;
1784 struct list_head module_list;
1785 struct list_head notify_list;
1786 struct mutex commit_mutex;
1787 u64 table_handle;
1788 u64 tstamp;
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
nft_net_tstamp(const struct net * net)1801 static inline u64 nft_net_tstamp(const struct net *net)
1802 {
1803 return nft_pernet(net)->tstamp;
1804 }
1805
1806 #define __NFT_REDUCE_READONLY 1UL
1807 #define NFT_REDUCE_READONLY (void *)__NFT_REDUCE_READONLY
1808
nft_reduce_is_readonly(const struct nft_expr * expr)1809 static inline bool nft_reduce_is_readonly(const struct nft_expr *expr)
1810 {
1811 return expr->ops->reduce == NFT_REDUCE_READONLY;
1812 }
1813
1814 void nft_reg_track_update(struct nft_regs_track *track,
1815 const struct nft_expr *expr, u8 dreg, u8 len);
1816 void nft_reg_track_cancel(struct nft_regs_track *track, u8 dreg, u8 len);
1817 void __nft_reg_track_cancel(struct nft_regs_track *track, u8 dreg);
1818
nft_reg_track_cmp(struct nft_regs_track * track,const struct nft_expr * expr,u8 dreg)1819 static inline bool nft_reg_track_cmp(struct nft_regs_track *track,
1820 const struct nft_expr *expr, u8 dreg)
1821 {
1822 return track->regs[dreg].selector &&
1823 track->regs[dreg].selector->ops == expr->ops &&
1824 track->regs[dreg].num_reg == 0;
1825 }
1826
1827 #endif /* _NET_NF_TABLES_H */
1828