netfilter: nf_tables: bogus EBUSY in chain deletions
[GitHub/MotorolaMobilityLLC/kernel-slsi.git] / include / net / netfilter / nf_tables.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _NET_NF_TABLES_H
3 #define _NET_NF_TABLES_H
4
5 #include <linux/module.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 <net/netlink.h>
13
14 #define NFT_JUMP_STACK_SIZE 16
15
16 struct nft_pktinfo {
17 struct sk_buff *skb;
18 bool tprot_set;
19 u8 tprot;
20 /* for x_tables compatibility */
21 struct xt_action_param xt;
22 };
23
24 static inline struct net *nft_net(const struct nft_pktinfo *pkt)
25 {
26 return pkt->xt.state->net;
27 }
28
29 static inline unsigned int nft_hook(const struct nft_pktinfo *pkt)
30 {
31 return pkt->xt.state->hook;
32 }
33
34 static inline u8 nft_pf(const struct nft_pktinfo *pkt)
35 {
36 return pkt->xt.state->pf;
37 }
38
39 static inline const struct net_device *nft_in(const struct nft_pktinfo *pkt)
40 {
41 return pkt->xt.state->in;
42 }
43
44 static inline const struct net_device *nft_out(const struct nft_pktinfo *pkt)
45 {
46 return pkt->xt.state->out;
47 }
48
49 static inline void nft_set_pktinfo(struct nft_pktinfo *pkt,
50 struct sk_buff *skb,
51 const struct nf_hook_state *state)
52 {
53 pkt->skb = skb;
54 pkt->xt.state = state;
55 }
56
57 static inline void nft_set_pktinfo_proto_unspec(struct nft_pktinfo *pkt,
58 struct sk_buff *skb)
59 {
60 pkt->tprot_set = false;
61 pkt->tprot = 0;
62 pkt->xt.thoff = 0;
63 pkt->xt.fragoff = 0;
64 }
65
66 static inline void nft_set_pktinfo_unspec(struct nft_pktinfo *pkt,
67 struct sk_buff *skb,
68 const struct nf_hook_state *state)
69 {
70 nft_set_pktinfo(pkt, skb, state);
71 nft_set_pktinfo_proto_unspec(pkt, skb);
72 }
73
74 /**
75 * struct nft_verdict - nf_tables verdict
76 *
77 * @code: nf_tables/netfilter verdict code
78 * @chain: destination chain for NFT_JUMP/NFT_GOTO
79 */
80 struct nft_verdict {
81 u32 code;
82 struct nft_chain *chain;
83 };
84
85 struct nft_data {
86 union {
87 u32 data[4];
88 struct nft_verdict verdict;
89 };
90 } __attribute__((aligned(__alignof__(u64))));
91
92 /**
93 * struct nft_regs - nf_tables register set
94 *
95 * @data: data registers
96 * @verdict: verdict register
97 *
98 * The first four data registers alias to the verdict register.
99 */
100 struct nft_regs {
101 union {
102 u32 data[20];
103 struct nft_verdict verdict;
104 };
105 };
106
107 /* Store/load an u16 or u8 integer to/from the u32 data register.
108 *
109 * Note, when using concatenations, register allocation happens at 32-bit
110 * level. So for store instruction, pad the rest part with zero to avoid
111 * garbage values.
112 */
113
114 static inline void nft_reg_store16(u32 *dreg, u16 val)
115 {
116 *dreg = 0;
117 *(u16 *)dreg = val;
118 }
119
120 static inline void nft_reg_store8(u32 *dreg, u8 val)
121 {
122 *dreg = 0;
123 *(u8 *)dreg = val;
124 }
125
126 static inline u16 nft_reg_load16(u32 *sreg)
127 {
128 return *(u16 *)sreg;
129 }
130
131 static inline u8 nft_reg_load8(u32 *sreg)
132 {
133 return *(u8 *)sreg;
134 }
135
136 static inline void nft_data_copy(u32 *dst, const struct nft_data *src,
137 unsigned int len)
138 {
139 memcpy(dst, src, len);
140 }
141
142 static inline void nft_data_debug(const struct nft_data *data)
143 {
144 pr_debug("data[0]=%x data[1]=%x data[2]=%x data[3]=%x\n",
145 data->data[0], data->data[1],
146 data->data[2], data->data[3]);
147 }
148
149 /**
150 * struct nft_ctx - nf_tables rule/set context
151 *
152 * @net: net namespace
153 * @afi: address family info
154 * @table: the table the chain is contained in
155 * @chain: the chain the rule is contained in
156 * @nla: netlink attributes
157 * @portid: netlink portID of the original message
158 * @seq: netlink sequence number
159 * @report: notify via unicast netlink message
160 */
161 struct nft_ctx {
162 struct net *net;
163 struct nft_af_info *afi;
164 struct nft_table *table;
165 struct nft_chain *chain;
166 const struct nlattr * const *nla;
167 u32 portid;
168 u32 seq;
169 bool report;
170 };
171
172 struct nft_data_desc {
173 enum nft_data_types type;
174 unsigned int len;
175 };
176
177 int nft_data_init(const struct nft_ctx *ctx,
178 struct nft_data *data, unsigned int size,
179 struct nft_data_desc *desc, const struct nlattr *nla);
180 void nft_data_hold(const struct nft_data *data, enum nft_data_types type);
181 void nft_data_release(const struct nft_data *data, enum nft_data_types type);
182 int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
183 enum nft_data_types type, unsigned int len);
184
185 static inline enum nft_data_types nft_dreg_to_type(enum nft_registers reg)
186 {
187 return reg == NFT_REG_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE;
188 }
189
190 static inline enum nft_registers nft_type_to_reg(enum nft_data_types type)
191 {
192 return type == NFT_DATA_VERDICT ? NFT_REG_VERDICT : NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE;
193 }
194
195 int nft_parse_u32_check(const struct nlattr *attr, int max, u32 *dest);
196 unsigned int nft_parse_register(const struct nlattr *attr);
197 int nft_dump_register(struct sk_buff *skb, unsigned int attr, unsigned int reg);
198
199 int nft_validate_register_load(enum nft_registers reg, unsigned int len);
200 int nft_validate_register_store(const struct nft_ctx *ctx,
201 enum nft_registers reg,
202 const struct nft_data *data,
203 enum nft_data_types type, unsigned int len);
204
205 /**
206 * struct nft_userdata - user defined data associated with an object
207 *
208 * @len: length of the data
209 * @data: content
210 *
211 * The presence of user data is indicated in an object specific fashion,
212 * so a length of zero can't occur and the value "len" indicates data
213 * of length len + 1.
214 */
215 struct nft_userdata {
216 u8 len;
217 unsigned char data[0];
218 };
219
220 /**
221 * struct nft_set_elem - generic representation of set elements
222 *
223 * @key: element key
224 * @priv: element private data and extensions
225 */
226 struct nft_set_elem {
227 union {
228 u32 buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)];
229 struct nft_data val;
230 } key;
231 void *priv;
232 };
233
234 struct nft_set;
235 struct nft_set_iter {
236 u8 genmask;
237 unsigned int count;
238 unsigned int skip;
239 int err;
240 int (*fn)(const struct nft_ctx *ctx,
241 struct nft_set *set,
242 const struct nft_set_iter *iter,
243 struct nft_set_elem *elem);
244 };
245
246 /**
247 * struct nft_set_desc - description of set elements
248 *
249 * @klen: key length
250 * @dlen: data length
251 * @size: number of set elements
252 */
253 struct nft_set_desc {
254 unsigned int klen;
255 unsigned int dlen;
256 unsigned int size;
257 };
258
259 /**
260 * enum nft_set_class - performance class
261 *
262 * @NFT_LOOKUP_O_1: constant, O(1)
263 * @NFT_LOOKUP_O_LOG_N: logarithmic, O(log N)
264 * @NFT_LOOKUP_O_N: linear, O(N)
265 */
266 enum nft_set_class {
267 NFT_SET_CLASS_O_1,
268 NFT_SET_CLASS_O_LOG_N,
269 NFT_SET_CLASS_O_N,
270 };
271
272 /**
273 * struct nft_set_estimate - estimation of memory and performance
274 * characteristics
275 *
276 * @size: required memory
277 * @lookup: lookup performance class
278 * @space: memory class
279 */
280 struct nft_set_estimate {
281 unsigned int size;
282 enum nft_set_class lookup;
283 enum nft_set_class space;
284 };
285
286 /**
287 * struct nft_set_type - nf_tables set type
288 *
289 * @select_ops: function to select nft_set_ops
290 * @ops: default ops, used when no select_ops functions is present
291 * @list: used internally
292 * @owner: module reference
293 */
294 struct nft_set_type {
295 const struct nft_set_ops *(*select_ops)(const struct nft_ctx *,
296 const struct nft_set_desc *desc,
297 u32 flags);
298 const struct nft_set_ops *ops;
299 struct list_head list;
300 struct module *owner;
301 };
302
303 struct nft_set_ext;
304 struct nft_expr;
305
306 /**
307 * struct nft_set_ops - nf_tables set operations
308 *
309 * @lookup: look up an element within the set
310 * @insert: insert new element into set
311 * @activate: activate new element in the next generation
312 * @deactivate: lookup for element and deactivate it in the next generation
313 * @flush: deactivate element in the next generation
314 * @remove: remove element from set
315 * @walk: iterate over all set elemeennts
316 * @privsize: function to return size of set private data
317 * @init: initialize private data of new set instance
318 * @destroy: destroy private data of set instance
319 * @elemsize: element private size
320 * @features: features supported by the implementation
321 */
322 struct nft_set_ops {
323 bool (*lookup)(const struct net *net,
324 const struct nft_set *set,
325 const u32 *key,
326 const struct nft_set_ext **ext);
327 bool (*update)(struct nft_set *set,
328 const u32 *key,
329 void *(*new)(struct nft_set *,
330 const struct nft_expr *,
331 struct nft_regs *),
332 const struct nft_expr *expr,
333 struct nft_regs *regs,
334 const struct nft_set_ext **ext);
335
336 int (*insert)(const struct net *net,
337 const struct nft_set *set,
338 const struct nft_set_elem *elem,
339 struct nft_set_ext **ext);
340 void (*activate)(const struct net *net,
341 const struct nft_set *set,
342 const struct nft_set_elem *elem);
343 void * (*deactivate)(const struct net *net,
344 const struct nft_set *set,
345 const struct nft_set_elem *elem);
346 bool (*flush)(const struct net *net,
347 const struct nft_set *set,
348 void *priv);
349 void (*remove)(const struct net *net,
350 const struct nft_set *set,
351 const struct nft_set_elem *elem);
352 void (*walk)(const struct nft_ctx *ctx,
353 struct nft_set *set,
354 struct nft_set_iter *iter);
355
356 unsigned int (*privsize)(const struct nlattr * const nla[],
357 const struct nft_set_desc *desc);
358 bool (*estimate)(const struct nft_set_desc *desc,
359 u32 features,
360 struct nft_set_estimate *est);
361 int (*init)(const struct nft_set *set,
362 const struct nft_set_desc *desc,
363 const struct nlattr * const nla[]);
364 void (*destroy)(const struct nft_set *set);
365
366 unsigned int elemsize;
367 u32 features;
368 const struct nft_set_type *type;
369 };
370
371 int nft_register_set(struct nft_set_type *type);
372 void nft_unregister_set(struct nft_set_type *type);
373
374 /**
375 * struct nft_set - nf_tables set instance
376 *
377 * @list: table set list node
378 * @bindings: list of set bindings
379 * @name: name of the set
380 * @ktype: key type (numeric type defined by userspace, not used in the kernel)
381 * @dtype: data type (verdict or numeric type defined by userspace)
382 * @objtype: object type (see NFT_OBJECT_* definitions)
383 * @size: maximum set size
384 * @nelems: number of elements
385 * @ndeact: number of deactivated elements queued for removal
386 * @timeout: default timeout value in jiffies
387 * @gc_int: garbage collection interval in msecs
388 * @policy: set parameterization (see enum nft_set_policies)
389 * @udlen: user data length
390 * @udata: user data
391 * @ops: set ops
392 * @flags: set flags
393 * @genmask: generation mask
394 * @klen: key length
395 * @dlen: data length
396 * @data: private set data
397 */
398 struct nft_set {
399 struct list_head list;
400 struct list_head bindings;
401 char *name;
402 u32 ktype;
403 u32 dtype;
404 u32 objtype;
405 u32 size;
406 atomic_t nelems;
407 u32 ndeact;
408 u64 timeout;
409 u32 gc_int;
410 u16 policy;
411 u16 udlen;
412 unsigned char *udata;
413 /* runtime data below here */
414 const struct nft_set_ops *ops ____cacheline_aligned;
415 u16 flags:14,
416 genmask:2;
417 u8 klen;
418 u8 dlen;
419 unsigned char data[]
420 __attribute__((aligned(__alignof__(u64))));
421 };
422
423 static inline void *nft_set_priv(const struct nft_set *set)
424 {
425 return (void *)set->data;
426 }
427
428 static inline struct nft_set *nft_set_container_of(const void *priv)
429 {
430 return (void *)priv - offsetof(struct nft_set, data);
431 }
432
433 struct nft_set *nft_set_lookup(const struct net *net,
434 const struct nft_table *table,
435 const struct nlattr *nla_set_name,
436 const struct nlattr *nla_set_id,
437 u8 genmask);
438
439 static inline unsigned long nft_set_gc_interval(const struct nft_set *set)
440 {
441 return set->gc_int ? msecs_to_jiffies(set->gc_int) : HZ;
442 }
443
444 /**
445 * struct nft_set_binding - nf_tables set binding
446 *
447 * @list: set bindings list node
448 * @chain: chain containing the rule bound to the set
449 * @flags: set action flags
450 *
451 * A set binding contains all information necessary for validation
452 * of new elements added to a bound set.
453 */
454 struct nft_set_binding {
455 struct list_head list;
456 const struct nft_chain *chain;
457 u32 flags;
458 };
459
460 int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
461 struct nft_set_binding *binding);
462 void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
463 struct nft_set_binding *binding);
464
465 /**
466 * enum nft_set_extensions - set extension type IDs
467 *
468 * @NFT_SET_EXT_KEY: element key
469 * @NFT_SET_EXT_DATA: mapping data
470 * @NFT_SET_EXT_FLAGS: element flags
471 * @NFT_SET_EXT_TIMEOUT: element timeout
472 * @NFT_SET_EXT_EXPIRATION: element expiration time
473 * @NFT_SET_EXT_USERDATA: user data associated with the element
474 * @NFT_SET_EXT_EXPR: expression assiociated with the element
475 * @NFT_SET_EXT_OBJREF: stateful object reference associated with element
476 * @NFT_SET_EXT_NUM: number of extension types
477 */
478 enum nft_set_extensions {
479 NFT_SET_EXT_KEY,
480 NFT_SET_EXT_DATA,
481 NFT_SET_EXT_FLAGS,
482 NFT_SET_EXT_TIMEOUT,
483 NFT_SET_EXT_EXPIRATION,
484 NFT_SET_EXT_USERDATA,
485 NFT_SET_EXT_EXPR,
486 NFT_SET_EXT_OBJREF,
487 NFT_SET_EXT_NUM
488 };
489
490 /**
491 * struct nft_set_ext_type - set extension type
492 *
493 * @len: fixed part length of the extension
494 * @align: alignment requirements of the extension
495 */
496 struct nft_set_ext_type {
497 u8 len;
498 u8 align;
499 };
500
501 extern const struct nft_set_ext_type nft_set_ext_types[];
502
503 /**
504 * struct nft_set_ext_tmpl - set extension template
505 *
506 * @len: length of extension area
507 * @offset: offsets of individual extension types
508 */
509 struct nft_set_ext_tmpl {
510 u16 len;
511 u8 offset[NFT_SET_EXT_NUM];
512 };
513
514 /**
515 * struct nft_set_ext - set extensions
516 *
517 * @genmask: generation mask
518 * @offset: offsets of individual extension types
519 * @data: beginning of extension data
520 */
521 struct nft_set_ext {
522 u8 genmask;
523 u8 offset[NFT_SET_EXT_NUM];
524 char data[0];
525 };
526
527 static inline void nft_set_ext_prepare(struct nft_set_ext_tmpl *tmpl)
528 {
529 memset(tmpl, 0, sizeof(*tmpl));
530 tmpl->len = sizeof(struct nft_set_ext);
531 }
532
533 static inline void nft_set_ext_add_length(struct nft_set_ext_tmpl *tmpl, u8 id,
534 unsigned int len)
535 {
536 tmpl->len = ALIGN(tmpl->len, nft_set_ext_types[id].align);
537 BUG_ON(tmpl->len > U8_MAX);
538 tmpl->offset[id] = tmpl->len;
539 tmpl->len += nft_set_ext_types[id].len + len;
540 }
541
542 static inline void nft_set_ext_add(struct nft_set_ext_tmpl *tmpl, u8 id)
543 {
544 nft_set_ext_add_length(tmpl, id, 0);
545 }
546
547 static inline void nft_set_ext_init(struct nft_set_ext *ext,
548 const struct nft_set_ext_tmpl *tmpl)
549 {
550 memcpy(ext->offset, tmpl->offset, sizeof(ext->offset));
551 }
552
553 static inline bool __nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
554 {
555 return !!ext->offset[id];
556 }
557
558 static inline bool nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
559 {
560 return ext && __nft_set_ext_exists(ext, id);
561 }
562
563 static inline void *nft_set_ext(const struct nft_set_ext *ext, u8 id)
564 {
565 return (void *)ext + ext->offset[id];
566 }
567
568 static inline struct nft_data *nft_set_ext_key(const struct nft_set_ext *ext)
569 {
570 return nft_set_ext(ext, NFT_SET_EXT_KEY);
571 }
572
573 static inline struct nft_data *nft_set_ext_data(const struct nft_set_ext *ext)
574 {
575 return nft_set_ext(ext, NFT_SET_EXT_DATA);
576 }
577
578 static inline u8 *nft_set_ext_flags(const struct nft_set_ext *ext)
579 {
580 return nft_set_ext(ext, NFT_SET_EXT_FLAGS);
581 }
582
583 static inline u64 *nft_set_ext_timeout(const struct nft_set_ext *ext)
584 {
585 return nft_set_ext(ext, NFT_SET_EXT_TIMEOUT);
586 }
587
588 static inline unsigned long *nft_set_ext_expiration(const struct nft_set_ext *ext)
589 {
590 return nft_set_ext(ext, NFT_SET_EXT_EXPIRATION);
591 }
592
593 static inline struct nft_userdata *nft_set_ext_userdata(const struct nft_set_ext *ext)
594 {
595 return nft_set_ext(ext, NFT_SET_EXT_USERDATA);
596 }
597
598 static inline struct nft_expr *nft_set_ext_expr(const struct nft_set_ext *ext)
599 {
600 return nft_set_ext(ext, NFT_SET_EXT_EXPR);
601 }
602
603 static inline bool nft_set_elem_expired(const struct nft_set_ext *ext)
604 {
605 return nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION) &&
606 time_is_before_eq_jiffies(*nft_set_ext_expiration(ext));
607 }
608
609 static inline struct nft_set_ext *nft_set_elem_ext(const struct nft_set *set,
610 void *elem)
611 {
612 return elem + set->ops->elemsize;
613 }
614
615 static inline struct nft_object **nft_set_ext_obj(const struct nft_set_ext *ext)
616 {
617 return nft_set_ext(ext, NFT_SET_EXT_OBJREF);
618 }
619
620 void *nft_set_elem_init(const struct nft_set *set,
621 const struct nft_set_ext_tmpl *tmpl,
622 const u32 *key, const u32 *data,
623 u64 timeout, gfp_t gfp);
624 void nft_set_elem_destroy(const struct nft_set *set, void *elem,
625 bool destroy_expr);
626
627 /**
628 * struct nft_set_gc_batch_head - nf_tables set garbage collection batch
629 *
630 * @rcu: rcu head
631 * @set: set the elements belong to
632 * @cnt: count of elements
633 */
634 struct nft_set_gc_batch_head {
635 struct rcu_head rcu;
636 const struct nft_set *set;
637 unsigned int cnt;
638 };
639
640 #define NFT_SET_GC_BATCH_SIZE ((PAGE_SIZE - \
641 sizeof(struct nft_set_gc_batch_head)) / \
642 sizeof(void *))
643
644 /**
645 * struct nft_set_gc_batch - nf_tables set garbage collection batch
646 *
647 * @head: GC batch head
648 * @elems: garbage collection elements
649 */
650 struct nft_set_gc_batch {
651 struct nft_set_gc_batch_head head;
652 void *elems[NFT_SET_GC_BATCH_SIZE];
653 };
654
655 struct nft_set_gc_batch *nft_set_gc_batch_alloc(const struct nft_set *set,
656 gfp_t gfp);
657 void nft_set_gc_batch_release(struct rcu_head *rcu);
658
659 static inline void nft_set_gc_batch_complete(struct nft_set_gc_batch *gcb)
660 {
661 if (gcb != NULL)
662 call_rcu(&gcb->head.rcu, nft_set_gc_batch_release);
663 }
664
665 static inline struct nft_set_gc_batch *
666 nft_set_gc_batch_check(const struct nft_set *set, struct nft_set_gc_batch *gcb,
667 gfp_t gfp)
668 {
669 if (gcb != NULL) {
670 if (gcb->head.cnt + 1 < ARRAY_SIZE(gcb->elems))
671 return gcb;
672 nft_set_gc_batch_complete(gcb);
673 }
674 return nft_set_gc_batch_alloc(set, gfp);
675 }
676
677 static inline void nft_set_gc_batch_add(struct nft_set_gc_batch *gcb,
678 void *elem)
679 {
680 gcb->elems[gcb->head.cnt++] = elem;
681 }
682
683 /**
684 * struct nft_expr_type - nf_tables expression type
685 *
686 * @select_ops: function to select nft_expr_ops
687 * @ops: default ops, used when no select_ops functions is present
688 * @list: used internally
689 * @name: Identifier
690 * @owner: module reference
691 * @policy: netlink attribute policy
692 * @maxattr: highest netlink attribute number
693 * @family: address family for AF-specific types
694 * @flags: expression type flags
695 */
696 struct nft_expr_type {
697 const struct nft_expr_ops *(*select_ops)(const struct nft_ctx *,
698 const struct nlattr * const tb[]);
699 const struct nft_expr_ops *ops;
700 struct list_head list;
701 const char *name;
702 struct module *owner;
703 const struct nla_policy *policy;
704 unsigned int maxattr;
705 u8 family;
706 u8 flags;
707 };
708
709 #define NFT_EXPR_STATEFUL 0x1
710
711 /**
712 * struct nft_expr_ops - nf_tables expression operations
713 *
714 * @eval: Expression evaluation function
715 * @size: full expression size, including private data size
716 * @init: initialization function
717 * @destroy: destruction function
718 * @dump: function to dump parameters
719 * @type: expression type
720 * @validate: validate expression, called during loop detection
721 * @data: extra data to attach to this expression operation
722 */
723 struct nft_expr;
724 struct nft_expr_ops {
725 void (*eval)(const struct nft_expr *expr,
726 struct nft_regs *regs,
727 const struct nft_pktinfo *pkt);
728 int (*clone)(struct nft_expr *dst,
729 const struct nft_expr *src);
730 unsigned int size;
731
732 int (*init)(const struct nft_ctx *ctx,
733 const struct nft_expr *expr,
734 const struct nlattr * const tb[]);
735 void (*activate)(const struct nft_ctx *ctx,
736 const struct nft_expr *expr);
737 void (*deactivate)(const struct nft_ctx *ctx,
738 const struct nft_expr *expr);
739 void (*destroy)(const struct nft_ctx *ctx,
740 const struct nft_expr *expr);
741 int (*dump)(struct sk_buff *skb,
742 const struct nft_expr *expr);
743 int (*validate)(const struct nft_ctx *ctx,
744 const struct nft_expr *expr,
745 const struct nft_data **data);
746 const struct nft_expr_type *type;
747 void *data;
748 };
749
750 #define NFT_EXPR_MAXATTR 16
751 #define NFT_EXPR_SIZE(size) (sizeof(struct nft_expr) + \
752 ALIGN(size, __alignof__(struct nft_expr)))
753
754 /**
755 * struct nft_expr - nf_tables expression
756 *
757 * @ops: expression ops
758 * @data: expression private data
759 */
760 struct nft_expr {
761 const struct nft_expr_ops *ops;
762 unsigned char data[];
763 };
764
765 static inline void *nft_expr_priv(const struct nft_expr *expr)
766 {
767 return (void *)expr->data;
768 }
769
770 struct nft_expr *nft_expr_init(const struct nft_ctx *ctx,
771 const struct nlattr *nla);
772 void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr);
773 int nft_expr_dump(struct sk_buff *skb, unsigned int attr,
774 const struct nft_expr *expr);
775
776 static inline int nft_expr_clone(struct nft_expr *dst, struct nft_expr *src)
777 {
778 int err;
779
780 if (src->ops->clone) {
781 dst->ops = src->ops;
782 err = src->ops->clone(dst, src);
783 if (err < 0)
784 return err;
785 } else {
786 memcpy(dst, src, src->ops->size);
787 }
788
789 __module_get(src->ops->type->owner);
790 return 0;
791 }
792
793 /**
794 * struct nft_rule - nf_tables rule
795 *
796 * @list: used internally
797 * @handle: rule handle
798 * @genmask: generation mask
799 * @dlen: length of expression data
800 * @udata: user data is appended to the rule
801 * @data: expression data
802 */
803 struct nft_rule {
804 struct list_head list;
805 u64 handle:42,
806 genmask:2,
807 dlen:12,
808 udata:1;
809 unsigned char data[]
810 __attribute__((aligned(__alignof__(struct nft_expr))));
811 };
812
813 static inline struct nft_expr *nft_expr_first(const struct nft_rule *rule)
814 {
815 return (struct nft_expr *)&rule->data[0];
816 }
817
818 static inline struct nft_expr *nft_expr_next(const struct nft_expr *expr)
819 {
820 return ((void *)expr) + expr->ops->size;
821 }
822
823 static inline struct nft_expr *nft_expr_last(const struct nft_rule *rule)
824 {
825 return (struct nft_expr *)&rule->data[rule->dlen];
826 }
827
828 static inline struct nft_userdata *nft_userdata(const struct nft_rule *rule)
829 {
830 return (void *)&rule->data[rule->dlen];
831 }
832
833 /*
834 * The last pointer isn't really necessary, but the compiler isn't able to
835 * determine that the result of nft_expr_last() is always the same since it
836 * can't assume that the dlen value wasn't changed within calls in the loop.
837 */
838 #define nft_rule_for_each_expr(expr, last, rule) \
839 for ((expr) = nft_expr_first(rule), (last) = nft_expr_last(rule); \
840 (expr) != (last); \
841 (expr) = nft_expr_next(expr))
842
843 enum nft_chain_flags {
844 NFT_BASE_CHAIN = 0x1,
845 };
846
847 /**
848 * struct nft_chain - nf_tables chain
849 *
850 * @rules: list of rules in the chain
851 * @list: used internally
852 * @table: table that this chain belongs to
853 * @handle: chain handle
854 * @use: number of jump references to this chain
855 * @level: length of longest path to this chain
856 * @flags: bitmask of enum nft_chain_flags
857 * @name: name of the chain
858 */
859 struct nft_chain {
860 struct list_head rules;
861 struct list_head list;
862 struct nft_table *table;
863 u64 handle;
864 u32 use;
865 u16 level;
866 u8 flags:6,
867 genmask:2;
868 char *name;
869 };
870
871 enum nft_chain_type {
872 NFT_CHAIN_T_DEFAULT = 0,
873 NFT_CHAIN_T_ROUTE,
874 NFT_CHAIN_T_NAT,
875 NFT_CHAIN_T_MAX
876 };
877
878 /**
879 * struct nf_chain_type - nf_tables chain type info
880 *
881 * @name: name of the type
882 * @type: numeric identifier
883 * @family: address family
884 * @owner: module owner
885 * @hook_mask: mask of valid hooks
886 * @hooks: hookfn overrides
887 */
888 struct nf_chain_type {
889 const char *name;
890 enum nft_chain_type type;
891 int family;
892 struct module *owner;
893 unsigned int hook_mask;
894 nf_hookfn *hooks[NF_MAX_HOOKS];
895 };
896
897 int nft_chain_validate_dependency(const struct nft_chain *chain,
898 enum nft_chain_type type);
899 int nft_chain_validate_hooks(const struct nft_chain *chain,
900 unsigned int hook_flags);
901
902 struct nft_stats {
903 u64 bytes;
904 u64 pkts;
905 struct u64_stats_sync syncp;
906 };
907
908 #define NFT_HOOK_OPS_MAX 2
909
910 /**
911 * struct nft_base_chain - nf_tables base chain
912 *
913 * @ops: netfilter hook ops
914 * @type: chain type
915 * @policy: default policy
916 * @stats: per-cpu chain stats
917 * @chain: the chain
918 * @dev_name: device name that this base chain is attached to (if any)
919 */
920 struct nft_base_chain {
921 struct nf_hook_ops ops[NFT_HOOK_OPS_MAX];
922 const struct nf_chain_type *type;
923 u8 policy;
924 u8 flags;
925 struct nft_stats __percpu *stats;
926 struct nft_chain chain;
927 char dev_name[IFNAMSIZ];
928 };
929
930 static inline struct nft_base_chain *nft_base_chain(const struct nft_chain *chain)
931 {
932 return container_of(chain, struct nft_base_chain, chain);
933 }
934
935 static inline bool nft_is_base_chain(const struct nft_chain *chain)
936 {
937 return chain->flags & NFT_BASE_CHAIN;
938 }
939
940 int __nft_release_basechain(struct nft_ctx *ctx);
941
942 unsigned int nft_do_chain(struct nft_pktinfo *pkt, void *priv);
943
944 /**
945 * struct nft_table - nf_tables table
946 *
947 * @list: used internally
948 * @chains: chains in the table
949 * @sets: sets in the table
950 * @objects: stateful objects in the table
951 * @hgenerator: handle generator state
952 * @use: number of chain references to this table
953 * @flags: table flag (see enum nft_table_flags)
954 * @genmask: generation mask
955 * @name: name of the table
956 */
957 struct nft_table {
958 struct list_head list;
959 struct list_head chains;
960 struct list_head sets;
961 struct list_head objects;
962 u64 hgenerator;
963 u32 use;
964 u16 flags:14,
965 genmask:2;
966 char *name;
967 };
968
969 enum nft_af_flags {
970 NFT_AF_NEEDS_DEV = (1 << 0),
971 };
972
973 /**
974 * struct nft_af_info - nf_tables address family info
975 *
976 * @list: used internally
977 * @family: address family
978 * @nhooks: number of hooks in this family
979 * @owner: module owner
980 * @tables: used internally
981 * @flags: family flags
982 * @nops: number of hook ops in this family
983 * @hook_ops_init: initialization function for chain hook ops
984 * @hooks: hookfn overrides for packet validation
985 */
986 struct nft_af_info {
987 struct list_head list;
988 int family;
989 unsigned int nhooks;
990 struct module *owner;
991 struct list_head tables;
992 u32 flags;
993 unsigned int nops;
994 void (*hook_ops_init)(struct nf_hook_ops *,
995 unsigned int);
996 nf_hookfn *hooks[NF_MAX_HOOKS];
997 };
998
999 int nft_register_afinfo(struct net *, struct nft_af_info *);
1000 void nft_unregister_afinfo(struct net *, struct nft_af_info *);
1001
1002 int nft_register_chain_type(const struct nf_chain_type *);
1003 void nft_unregister_chain_type(const struct nf_chain_type *);
1004
1005 int nft_register_expr(struct nft_expr_type *);
1006 void nft_unregister_expr(struct nft_expr_type *);
1007
1008 int nft_verdict_dump(struct sk_buff *skb, int type,
1009 const struct nft_verdict *v);
1010
1011 /**
1012 * struct nft_object - nf_tables stateful object
1013 *
1014 * @list: table stateful object list node
1015 * @table: table this object belongs to
1016 * @name: name of this stateful object
1017 * @genmask: generation mask
1018 * @use: number of references to this stateful object
1019 * @data: object data, layout depends on type
1020 * @ops: object operations
1021 * @data: pointer to object data
1022 */
1023 struct nft_object {
1024 struct list_head list;
1025 char *name;
1026 struct nft_table *table;
1027 u32 genmask:2,
1028 use:30;
1029 /* runtime data below here */
1030 const struct nft_object_ops *ops ____cacheline_aligned;
1031 unsigned char data[]
1032 __attribute__((aligned(__alignof__(u64))));
1033 };
1034
1035 static inline void *nft_obj_data(const struct nft_object *obj)
1036 {
1037 return (void *)obj->data;
1038 }
1039
1040 #define nft_expr_obj(expr) *((struct nft_object **)nft_expr_priv(expr))
1041
1042 struct nft_object *nf_tables_obj_lookup(const struct nft_table *table,
1043 const struct nlattr *nla, u32 objtype,
1044 u8 genmask);
1045
1046 void nft_obj_notify(struct net *net, struct nft_table *table,
1047 struct nft_object *obj, u32 portid, u32 seq,
1048 int event, int family, int report, gfp_t gfp);
1049
1050 /**
1051 * struct nft_object_type - stateful object type
1052 *
1053 * @select_ops: function to select nft_object_ops
1054 * @ops: default ops, used when no select_ops functions is present
1055 * @list: list node in list of object types
1056 * @type: stateful object numeric type
1057 * @owner: module owner
1058 * @maxattr: maximum netlink attribute
1059 * @policy: netlink attribute policy
1060 */
1061 struct nft_object_type {
1062 const struct nft_object_ops *(*select_ops)(const struct nft_ctx *,
1063 const struct nlattr * const tb[]);
1064 const struct nft_object_ops *ops;
1065 struct list_head list;
1066 u32 type;
1067 unsigned int maxattr;
1068 struct module *owner;
1069 const struct nla_policy *policy;
1070 };
1071
1072 /**
1073 * struct nft_object_ops - stateful object operations
1074 *
1075 * @eval: stateful object evaluation function
1076 * @size: stateful object size
1077 * @init: initialize object from netlink attributes
1078 * @destroy: release existing stateful object
1079 * @dump: netlink dump stateful object
1080 */
1081 struct nft_object_ops {
1082 void (*eval)(struct nft_object *obj,
1083 struct nft_regs *regs,
1084 const struct nft_pktinfo *pkt);
1085 unsigned int size;
1086 int (*init)(const struct nft_ctx *ctx,
1087 const struct nlattr *const tb[],
1088 struct nft_object *obj);
1089 void (*destroy)(struct nft_object *obj);
1090 int (*dump)(struct sk_buff *skb,
1091 struct nft_object *obj,
1092 bool reset);
1093 const struct nft_object_type *type;
1094 };
1095
1096 int nft_register_obj(struct nft_object_type *obj_type);
1097 void nft_unregister_obj(struct nft_object_type *obj_type);
1098
1099 /**
1100 * struct nft_traceinfo - nft tracing information and state
1101 *
1102 * @pkt: pktinfo currently processed
1103 * @basechain: base chain currently processed
1104 * @chain: chain currently processed
1105 * @rule: rule that was evaluated
1106 * @verdict: verdict given by rule
1107 * @type: event type (enum nft_trace_types)
1108 * @packet_dumped: packet headers sent in a previous traceinfo message
1109 * @trace: other struct members are initialised
1110 */
1111 struct nft_traceinfo {
1112 const struct nft_pktinfo *pkt;
1113 const struct nft_base_chain *basechain;
1114 const struct nft_chain *chain;
1115 const struct nft_rule *rule;
1116 const struct nft_verdict *verdict;
1117 enum nft_trace_types type;
1118 bool packet_dumped;
1119 bool trace;
1120 };
1121
1122 void nft_trace_init(struct nft_traceinfo *info, const struct nft_pktinfo *pkt,
1123 const struct nft_verdict *verdict,
1124 const struct nft_chain *basechain);
1125
1126 void nft_trace_notify(struct nft_traceinfo *info);
1127
1128 #define nft_dereference(p) \
1129 nfnl_dereference(p, NFNL_SUBSYS_NFTABLES)
1130
1131 #define MODULE_ALIAS_NFT_FAMILY(family) \
1132 MODULE_ALIAS("nft-afinfo-" __stringify(family))
1133
1134 #define MODULE_ALIAS_NFT_CHAIN(family, name) \
1135 MODULE_ALIAS("nft-chain-" __stringify(family) "-" name)
1136
1137 #define MODULE_ALIAS_NFT_AF_EXPR(family, name) \
1138 MODULE_ALIAS("nft-expr-" __stringify(family) "-" name)
1139
1140 #define MODULE_ALIAS_NFT_EXPR(name) \
1141 MODULE_ALIAS("nft-expr-" name)
1142
1143 #define MODULE_ALIAS_NFT_SET() \
1144 MODULE_ALIAS("nft-set")
1145
1146 #define MODULE_ALIAS_NFT_OBJ(type) \
1147 MODULE_ALIAS("nft-obj-" __stringify(type))
1148
1149 /*
1150 * The gencursor defines two generations, the currently active and the
1151 * next one. Objects contain a bitmask of 2 bits specifying the generations
1152 * they're active in. A set bit means they're inactive in the generation
1153 * represented by that bit.
1154 *
1155 * New objects start out as inactive in the current and active in the
1156 * next generation. When committing the ruleset the bitmask is cleared,
1157 * meaning they're active in all generations. When removing an object,
1158 * it is set inactive in the next generation. After committing the ruleset,
1159 * the objects are removed.
1160 */
1161 static inline unsigned int nft_gencursor_next(const struct net *net)
1162 {
1163 return net->nft.gencursor + 1 == 1 ? 1 : 0;
1164 }
1165
1166 static inline u8 nft_genmask_next(const struct net *net)
1167 {
1168 return 1 << nft_gencursor_next(net);
1169 }
1170
1171 static inline u8 nft_genmask_cur(const struct net *net)
1172 {
1173 /* Use ACCESS_ONCE() to prevent refetching the value for atomicity */
1174 return 1 << ACCESS_ONCE(net->nft.gencursor);
1175 }
1176
1177 #define NFT_GENMASK_ANY ((1 << 0) | (1 << 1))
1178
1179 /*
1180 * Generic transaction helpers
1181 */
1182
1183 /* Check if this object is currently active. */
1184 #define nft_is_active(__net, __obj) \
1185 (((__obj)->genmask & nft_genmask_cur(__net)) == 0)
1186
1187 /* Check if this object is active in the next generation. */
1188 #define nft_is_active_next(__net, __obj) \
1189 (((__obj)->genmask & nft_genmask_next(__net)) == 0)
1190
1191 /* This object becomes active in the next generation. */
1192 #define nft_activate_next(__net, __obj) \
1193 (__obj)->genmask = nft_genmask_cur(__net)
1194
1195 /* This object becomes inactive in the next generation. */
1196 #define nft_deactivate_next(__net, __obj) \
1197 (__obj)->genmask = nft_genmask_next(__net)
1198
1199 /* After committing the ruleset, clear the stale generation bit. */
1200 #define nft_clear(__net, __obj) \
1201 (__obj)->genmask &= ~nft_genmask_next(__net)
1202 #define nft_active_genmask(__obj, __genmask) \
1203 !((__obj)->genmask & __genmask)
1204
1205 /*
1206 * Set element transaction helpers
1207 */
1208
1209 static inline bool nft_set_elem_active(const struct nft_set_ext *ext,
1210 u8 genmask)
1211 {
1212 return !(ext->genmask & genmask);
1213 }
1214
1215 static inline void nft_set_elem_change_active(const struct net *net,
1216 const struct nft_set *set,
1217 struct nft_set_ext *ext)
1218 {
1219 ext->genmask ^= nft_genmask_next(net);
1220 }
1221
1222 /*
1223 * We use a free bit in the genmask field to indicate the element
1224 * is busy, meaning it is currently being processed either by
1225 * the netlink API or GC.
1226 *
1227 * Even though the genmask is only a single byte wide, this works
1228 * because the extension structure if fully constant once initialized,
1229 * so there are no non-atomic write accesses unless it is already
1230 * marked busy.
1231 */
1232 #define NFT_SET_ELEM_BUSY_MASK (1 << 2)
1233
1234 #if defined(__LITTLE_ENDIAN_BITFIELD)
1235 #define NFT_SET_ELEM_BUSY_BIT 2
1236 #elif defined(__BIG_ENDIAN_BITFIELD)
1237 #define NFT_SET_ELEM_BUSY_BIT (BITS_PER_LONG - BITS_PER_BYTE + 2)
1238 #else
1239 #error
1240 #endif
1241
1242 static inline int nft_set_elem_mark_busy(struct nft_set_ext *ext)
1243 {
1244 unsigned long *word = (unsigned long *)ext;
1245
1246 BUILD_BUG_ON(offsetof(struct nft_set_ext, genmask) != 0);
1247 return test_and_set_bit(NFT_SET_ELEM_BUSY_BIT, word);
1248 }
1249
1250 static inline void nft_set_elem_clear_busy(struct nft_set_ext *ext)
1251 {
1252 unsigned long *word = (unsigned long *)ext;
1253
1254 clear_bit(NFT_SET_ELEM_BUSY_BIT, word);
1255 }
1256
1257 /**
1258 * struct nft_trans - nf_tables object update in transaction
1259 *
1260 * @list: used internally
1261 * @msg_type: message type
1262 * @ctx: transaction context
1263 * @data: internal information related to the transaction
1264 */
1265 struct nft_trans {
1266 struct list_head list;
1267 int msg_type;
1268 struct nft_ctx ctx;
1269 char data[0];
1270 };
1271
1272 struct nft_trans_rule {
1273 struct nft_rule *rule;
1274 u32 rule_id;
1275 };
1276
1277 #define nft_trans_rule(trans) \
1278 (((struct nft_trans_rule *)trans->data)->rule)
1279 #define nft_trans_rule_id(trans) \
1280 (((struct nft_trans_rule *)trans->data)->rule_id)
1281
1282 struct nft_trans_set {
1283 struct nft_set *set;
1284 u32 set_id;
1285 };
1286
1287 #define nft_trans_set(trans) \
1288 (((struct nft_trans_set *)trans->data)->set)
1289 #define nft_trans_set_id(trans) \
1290 (((struct nft_trans_set *)trans->data)->set_id)
1291
1292 struct nft_trans_chain {
1293 bool update;
1294 char *name;
1295 struct nft_stats __percpu *stats;
1296 u8 policy;
1297 };
1298
1299 #define nft_trans_chain_update(trans) \
1300 (((struct nft_trans_chain *)trans->data)->update)
1301 #define nft_trans_chain_name(trans) \
1302 (((struct nft_trans_chain *)trans->data)->name)
1303 #define nft_trans_chain_stats(trans) \
1304 (((struct nft_trans_chain *)trans->data)->stats)
1305 #define nft_trans_chain_policy(trans) \
1306 (((struct nft_trans_chain *)trans->data)->policy)
1307
1308 struct nft_trans_table {
1309 bool update;
1310 bool enable;
1311 };
1312
1313 #define nft_trans_table_update(trans) \
1314 (((struct nft_trans_table *)trans->data)->update)
1315 #define nft_trans_table_enable(trans) \
1316 (((struct nft_trans_table *)trans->data)->enable)
1317
1318 struct nft_trans_elem {
1319 struct nft_set *set;
1320 struct nft_set_elem elem;
1321 };
1322
1323 #define nft_trans_elem_set(trans) \
1324 (((struct nft_trans_elem *)trans->data)->set)
1325 #define nft_trans_elem(trans) \
1326 (((struct nft_trans_elem *)trans->data)->elem)
1327
1328 struct nft_trans_obj {
1329 struct nft_object *obj;
1330 };
1331
1332 #define nft_trans_obj(trans) \
1333 (((struct nft_trans_obj *)trans->data)->obj)
1334
1335 #endif /* _NET_NF_TABLES_H */