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