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