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