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