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