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