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