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