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