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