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