1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _NET_NF_TABLES_H
3 #define _NET_NF_TABLES_H
4
5 #include <asm/unaligned.h>
6 #include <linux/list.h>
7 #include <linux/netfilter.h>
8 #include <linux/netfilter/nfnetlink.h>
9 #include <linux/netfilter/x_tables.h>
10 #include <linux/netfilter/nf_tables.h>
11 #include <linux/u64_stats_sync.h>
12 #include <linux/rhashtable.h>
13 #include <net/netfilter/nf_flow_table.h>
14 #include <net/netlink.h>
15 #include <net/flow_offload.h>
16
17 #define NFT_MAX_HOOKS (NF_INET_INGRESS + 1)
18
19 struct module;
20
21 #define NFT_JUMP_STACK_SIZE 16
22
23 struct nft_pktinfo {
24 struct sk_buff *skb;
25 bool tprot_set;
26 u8 tprot;
27 /* for x_tables compatibility */
28 struct xt_action_param xt;
29 };
30
nft_net(const struct nft_pktinfo * pkt)31 static inline struct net *nft_net(const struct nft_pktinfo *pkt)
32 {
33 return pkt->xt.state->net;
34 }
35
nft_hook(const struct nft_pktinfo * pkt)36 static inline unsigned int nft_hook(const struct nft_pktinfo *pkt)
37 {
38 return pkt->xt.state->hook;
39 }
40
nft_pf(const struct nft_pktinfo * pkt)41 static inline u8 nft_pf(const struct nft_pktinfo *pkt)
42 {
43 return pkt->xt.state->pf;
44 }
45
nft_in(const struct nft_pktinfo * pkt)46 static inline const struct net_device *nft_in(const struct nft_pktinfo *pkt)
47 {
48 return pkt->xt.state->in;
49 }
50
nft_out(const struct nft_pktinfo * pkt)51 static inline const struct net_device *nft_out(const struct nft_pktinfo *pkt)
52 {
53 return pkt->xt.state->out;
54 }
55
nft_set_pktinfo(struct nft_pktinfo * pkt,struct sk_buff * skb,const struct nf_hook_state * state)56 static inline void nft_set_pktinfo(struct nft_pktinfo *pkt,
57 struct sk_buff *skb,
58 const struct nf_hook_state *state)
59 {
60 pkt->skb = skb;
61 pkt->xt.state = state;
62 }
63
nft_set_pktinfo_unspec(struct nft_pktinfo * pkt,struct sk_buff * skb)64 static inline void nft_set_pktinfo_unspec(struct nft_pktinfo *pkt,
65 struct sk_buff *skb)
66 {
67 pkt->tprot_set = false;
68 pkt->tprot = 0;
69 pkt->xt.thoff = 0;
70 pkt->xt.fragoff = 0;
71 }
72
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 */
79 struct nft_verdict {
80 u32 code;
81 struct nft_chain *chain;
82 };
83
84 struct nft_data {
85 union {
86 u32 data[4];
87 struct nft_verdict verdict;
88 };
89 } __attribute__((aligned(__alignof__(u64))));
90
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 */
99 struct nft_regs {
100 union {
101 u32 data[20];
102 struct nft_verdict verdict;
103 };
104 };
105
106 /* Store/load an u8, u16 or u64 integer to/from the u32 data register.
107 *
108 * Note, when using concatenations, register allocation happens at 32-bit
109 * level. So for store instruction, pad the rest part with zero to avoid
110 * garbage values.
111 */
112
nft_reg_store8(u32 * dreg,u8 val)113 static inline void nft_reg_store8(u32 *dreg, u8 val)
114 {
115 *dreg = 0;
116 *(u8 *)dreg = val;
117 }
118
nft_reg_load8(const u32 * sreg)119 static inline u8 nft_reg_load8(const u32 *sreg)
120 {
121 return *(u8 *)sreg;
122 }
123
nft_reg_store16(u32 * dreg,u16 val)124 static inline void nft_reg_store16(u32 *dreg, u16 val)
125 {
126 *dreg = 0;
127 *(u16 *)dreg = val;
128 }
129
nft_reg_load16(const u32 * sreg)130 static inline u16 nft_reg_load16(const u32 *sreg)
131 {
132 return *(u16 *)sreg;
133 }
134
nft_reg_store64(u32 * dreg,u64 val)135 static inline void nft_reg_store64(u32 *dreg, u64 val)
136 {
137 put_unaligned(val, (u64 *)dreg);
138 }
139
nft_reg_load64(const u32 * sreg)140 static inline u64 nft_reg_load64(const u32 *sreg)
141 {
142 return get_unaligned((u64 *)sreg);
143 }
144
nft_data_copy(u32 * dst,const struct nft_data * src,unsigned int len)145 static inline void nft_data_copy(u32 *dst, const struct nft_data *src,
146 unsigned int len)
147 {
148 if (len % NFT_REG32_SIZE)
149 dst[len / NFT_REG32_SIZE] = 0;
150 memcpy(dst, src, len);
151 }
152
153 /**
154 * struct nft_ctx - nf_tables rule/set context
155 *
156 * @net: net namespace
157 * @table: the table the chain is contained in
158 * @chain: the chain the rule is contained in
159 * @nla: netlink attributes
160 * @portid: netlink portID of the original message
161 * @seq: netlink sequence number
162 * @family: protocol family
163 * @level: depth of the chains
164 * @report: notify via unicast netlink message
165 */
166 struct nft_ctx {
167 struct net *net;
168 struct nft_table *table;
169 struct nft_chain *chain;
170 const struct nlattr * const *nla;
171 u32 portid;
172 u32 seq;
173 u16 flags;
174 u8 family;
175 u8 level;
176 bool report;
177 };
178
179 struct nft_data_desc {
180 enum nft_data_types type;
181 unsigned int len;
182 };
183
184 int nft_data_init(const struct nft_ctx *ctx,
185 struct nft_data *data, unsigned int size,
186 struct nft_data_desc *desc, const struct nlattr *nla);
187 void nft_data_hold(const struct nft_data *data, enum nft_data_types type);
188 void nft_data_release(const struct nft_data *data, enum nft_data_types type);
189 int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
190 enum nft_data_types type, unsigned int len);
191
nft_dreg_to_type(enum nft_registers reg)192 static inline enum nft_data_types nft_dreg_to_type(enum nft_registers reg)
193 {
194 return reg == NFT_REG_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE;
195 }
196
nft_type_to_reg(enum nft_data_types type)197 static inline enum nft_registers nft_type_to_reg(enum nft_data_types type)
198 {
199 return type == NFT_DATA_VERDICT ? NFT_REG_VERDICT : NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE;
200 }
201
202 int nft_parse_u32_check(const struct nlattr *attr, int max, u32 *dest);
203 unsigned int nft_parse_register(const struct nlattr *attr);
204 int nft_dump_register(struct sk_buff *skb, unsigned int attr, unsigned int reg);
205
206 int nft_validate_register_load(enum nft_registers reg, unsigned int len);
207 int nft_validate_register_store(const struct nft_ctx *ctx,
208 enum nft_registers reg,
209 const struct nft_data *data,
210 enum nft_data_types type, unsigned int len);
211
212 /**
213 * struct nft_userdata - user defined data associated with an object
214 *
215 * @len: length of the data
216 * @data: content
217 *
218 * The presence of user data is indicated in an object specific fashion,
219 * so a length of zero can't occur and the value "len" indicates data
220 * of length len + 1.
221 */
222 struct nft_userdata {
223 u8 len;
224 unsigned char data[];
225 };
226
227 /**
228 * struct nft_set_elem - generic representation of set elements
229 *
230 * @key: element key
231 * @key_end: closing element key
232 * @priv: element private data and extensions
233 */
234 struct nft_set_elem {
235 union {
236 u32 buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)];
237 struct nft_data val;
238 } key;
239 union {
240 u32 buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)];
241 struct nft_data val;
242 } key_end;
243 union {
244 u32 buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)];
245 struct nft_data val;
246 } data;
247 void *priv;
248 };
249
250 struct nft_set;
251 struct nft_set_iter {
252 u8 genmask;
253 unsigned int count;
254 unsigned int skip;
255 int err;
256 int (*fn)(const struct nft_ctx *ctx,
257 struct nft_set *set,
258 const struct nft_set_iter *iter,
259 struct nft_set_elem *elem);
260 };
261
262 /**
263 * struct nft_set_desc - description of set elements
264 *
265 * @klen: key length
266 * @dlen: data length
267 * @size: number of set elements
268 * @field_len: length of each field in concatenation, bytes
269 * @field_count: number of concatenated fields in element
270 * @expr: set must support for expressions
271 */
272 struct nft_set_desc {
273 unsigned int klen;
274 unsigned int dlen;
275 unsigned int size;
276 u8 field_len[NFT_REG32_COUNT];
277 u8 field_count;
278 bool expr;
279 };
280
281 /**
282 * enum nft_set_class - performance class
283 *
284 * @NFT_LOOKUP_O_1: constant, O(1)
285 * @NFT_LOOKUP_O_LOG_N: logarithmic, O(log N)
286 * @NFT_LOOKUP_O_N: linear, O(N)
287 */
288 enum nft_set_class {
289 NFT_SET_CLASS_O_1,
290 NFT_SET_CLASS_O_LOG_N,
291 NFT_SET_CLASS_O_N,
292 };
293
294 /**
295 * struct nft_set_estimate - estimation of memory and performance
296 * characteristics
297 *
298 * @size: required memory
299 * @lookup: lookup performance class
300 * @space: memory class
301 */
302 struct nft_set_estimate {
303 u64 size;
304 enum nft_set_class lookup;
305 enum nft_set_class space;
306 };
307
308 struct nft_set_ext;
309 struct nft_expr;
310
311 /**
312 * struct nft_set_ops - nf_tables set operations
313 *
314 * @lookup: look up an element within the set
315 * @update: update an element if exists, add it if doesn't exist
316 * @delete: delete an element
317 * @insert: insert new element into set
318 * @activate: activate new element in the next generation
319 * @deactivate: lookup for element and deactivate it in the next generation
320 * @flush: deactivate element in the next generation
321 * @remove: remove element from set
322 * @walk: iterate over all set elements
323 * @get: get set elements
324 * @privsize: function to return size of set private data
325 * @init: initialize private data of new set instance
326 * @destroy: destroy private data of set instance
327 * @elemsize: element private size
328 *
329 * Operations lookup, update and delete have simpler interfaces, are faster
330 * and currently only used in the packet path. All the rest are slower,
331 * control plane functions.
332 */
333 struct nft_set_ops {
334 bool (*lookup)(const struct net *net,
335 const struct nft_set *set,
336 const u32 *key,
337 const struct nft_set_ext **ext);
338 bool (*update)(struct nft_set *set,
339 const u32 *key,
340 void *(*new)(struct nft_set *,
341 const struct nft_expr *,
342 struct nft_regs *),
343 const struct nft_expr *expr,
344 struct nft_regs *regs,
345 const struct nft_set_ext **ext);
346 bool (*delete)(const struct nft_set *set,
347 const u32 *key);
348
349 int (*insert)(const struct net *net,
350 const struct nft_set *set,
351 const struct nft_set_elem *elem,
352 struct nft_set_ext **ext);
353 void (*activate)(const struct net *net,
354 const struct nft_set *set,
355 const struct nft_set_elem *elem);
356 void * (*deactivate)(const struct net *net,
357 const struct nft_set *set,
358 const struct nft_set_elem *elem);
359 bool (*flush)(const struct net *net,
360 const struct nft_set *set,
361 void *priv);
362 void (*remove)(const struct net *net,
363 const struct nft_set *set,
364 const struct nft_set_elem *elem);
365 void (*walk)(const struct nft_ctx *ctx,
366 struct nft_set *set,
367 struct nft_set_iter *iter);
368 void * (*get)(const struct net *net,
369 const struct nft_set *set,
370 const struct nft_set_elem *elem,
371 unsigned int flags);
372
373 u64 (*privsize)(const struct nlattr * const nla[],
374 const struct nft_set_desc *desc);
375 bool (*estimate)(const struct nft_set_desc *desc,
376 u32 features,
377 struct nft_set_estimate *est);
378 int (*init)(const struct nft_set *set,
379 const struct nft_set_desc *desc,
380 const struct nlattr * const nla[]);
381 void (*destroy)(const struct nft_set *set);
382 void (*gc_init)(const struct nft_set *set);
383
384 unsigned int elemsize;
385 };
386
387 /**
388 * struct nft_set_type - nf_tables set type
389 *
390 * @ops: set ops for this type
391 * @features: features supported by the implementation
392 */
393 struct nft_set_type {
394 const struct nft_set_ops ops;
395 u32 features;
396 };
397 #define to_set_type(o) container_of(o, struct nft_set_type, ops)
398
399 /**
400 * struct nft_set - nf_tables set instance
401 *
402 * @list: table set list node
403 * @bindings: list of set bindings
404 * @table: table this set belongs to
405 * @net: netnamespace this set belongs to
406 * @name: name of the set
407 * @handle: unique handle of the set
408 * @ktype: key type (numeric type defined by userspace, not used in the kernel)
409 * @dtype: data type (verdict or numeric type defined by userspace)
410 * @objtype: object type (see NFT_OBJECT_* definitions)
411 * @size: maximum set size
412 * @field_len: length of each field in concatenation, bytes
413 * @field_count: number of concatenated fields in element
414 * @use: number of rules references to this set
415 * @nelems: number of elements
416 * @ndeact: number of deactivated elements queued for removal
417 * @timeout: default timeout value in jiffies
418 * @gc_int: garbage collection interval in msecs
419 * @policy: set parameterization (see enum nft_set_policies)
420 * @udlen: user data length
421 * @udata: user data
422 * @expr: stateful expression
423 * @ops: set ops
424 * @flags: set flags
425 * @genmask: generation mask
426 * @klen: key length
427 * @dlen: data length
428 * @data: private set data
429 */
430 struct nft_set {
431 struct list_head list;
432 struct list_head bindings;
433 struct nft_table *table;
434 possible_net_t net;
435 char *name;
436 u64 handle;
437 u32 ktype;
438 u32 dtype;
439 u32 objtype;
440 u32 size;
441 u8 field_len[NFT_REG32_COUNT];
442 u8 field_count;
443 u32 use;
444 atomic_t nelems;
445 u32 ndeact;
446 u64 timeout;
447 u32 gc_int;
448 u16 policy;
449 u16 udlen;
450 unsigned char *udata;
451 struct nft_expr *expr;
452 /* runtime data below here */
453 const struct nft_set_ops *ops ____cacheline_aligned;
454 u16 flags:14,
455 genmask:2;
456 u8 klen;
457 u8 dlen;
458 unsigned char data[]
459 __attribute__((aligned(__alignof__(u64))));
460 };
461
nft_set_is_anonymous(const struct nft_set * set)462 static inline bool nft_set_is_anonymous(const struct nft_set *set)
463 {
464 return set->flags & NFT_SET_ANONYMOUS;
465 }
466
nft_set_priv(const struct nft_set * set)467 static inline void *nft_set_priv(const struct nft_set *set)
468 {
469 return (void *)set->data;
470 }
471
nft_set_container_of(const void * priv)472 static inline struct nft_set *nft_set_container_of(const void *priv)
473 {
474 return (void *)priv - offsetof(struct nft_set, data);
475 }
476
477 struct nft_set *nft_set_lookup_global(const struct net *net,
478 const struct nft_table *table,
479 const struct nlattr *nla_set_name,
480 const struct nlattr *nla_set_id,
481 u8 genmask);
482
nft_set_gc_interval(const struct nft_set * set)483 static inline unsigned long nft_set_gc_interval(const struct nft_set *set)
484 {
485 return set->gc_int ? msecs_to_jiffies(set->gc_int) : HZ;
486 }
487
488 /**
489 * struct nft_set_binding - nf_tables set binding
490 *
491 * @list: set bindings list node
492 * @chain: chain containing the rule bound to the set
493 * @flags: set action flags
494 *
495 * A set binding contains all information necessary for validation
496 * of new elements added to a bound set.
497 */
498 struct nft_set_binding {
499 struct list_head list;
500 const struct nft_chain *chain;
501 u32 flags;
502 };
503
504 enum nft_trans_phase;
505 void nf_tables_deactivate_set(const struct nft_ctx *ctx, struct nft_set *set,
506 struct nft_set_binding *binding,
507 enum nft_trans_phase phase);
508 int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
509 struct nft_set_binding *binding);
510 void nf_tables_destroy_set(const struct nft_ctx *ctx, struct nft_set *set);
511
512 /**
513 * enum nft_set_extensions - set extension type IDs
514 *
515 * @NFT_SET_EXT_KEY: element key
516 * @NFT_SET_EXT_KEY_END: upper bound element key, for ranges
517 * @NFT_SET_EXT_DATA: mapping data
518 * @NFT_SET_EXT_FLAGS: element flags
519 * @NFT_SET_EXT_TIMEOUT: element timeout
520 * @NFT_SET_EXT_EXPIRATION: element expiration time
521 * @NFT_SET_EXT_USERDATA: user data associated with the element
522 * @NFT_SET_EXT_EXPR: expression assiociated with the element
523 * @NFT_SET_EXT_OBJREF: stateful object reference associated with element
524 * @NFT_SET_EXT_NUM: number of extension types
525 */
526 enum nft_set_extensions {
527 NFT_SET_EXT_KEY,
528 NFT_SET_EXT_KEY_END,
529 NFT_SET_EXT_DATA,
530 NFT_SET_EXT_FLAGS,
531 NFT_SET_EXT_TIMEOUT,
532 NFT_SET_EXT_EXPIRATION,
533 NFT_SET_EXT_USERDATA,
534 NFT_SET_EXT_EXPR,
535 NFT_SET_EXT_OBJREF,
536 NFT_SET_EXT_NUM
537 };
538
539 /**
540 * struct nft_set_ext_type - set extension type
541 *
542 * @len: fixed part length of the extension
543 * @align: alignment requirements of the extension
544 */
545 struct nft_set_ext_type {
546 u8 len;
547 u8 align;
548 };
549
550 extern const struct nft_set_ext_type nft_set_ext_types[];
551
552 /**
553 * struct nft_set_ext_tmpl - set extension template
554 *
555 * @len: length of extension area
556 * @offset: offsets of individual extension types
557 */
558 struct nft_set_ext_tmpl {
559 u16 len;
560 u8 offset[NFT_SET_EXT_NUM];
561 };
562
563 /**
564 * struct nft_set_ext - set extensions
565 *
566 * @genmask: generation mask
567 * @offset: offsets of individual extension types
568 * @data: beginning of extension data
569 */
570 struct nft_set_ext {
571 u8 genmask;
572 u8 offset[NFT_SET_EXT_NUM];
573 char data[];
574 };
575
nft_set_ext_prepare(struct nft_set_ext_tmpl * tmpl)576 static inline void nft_set_ext_prepare(struct nft_set_ext_tmpl *tmpl)
577 {
578 memset(tmpl, 0, sizeof(*tmpl));
579 tmpl->len = sizeof(struct nft_set_ext);
580 }
581
nft_set_ext_add_length(struct nft_set_ext_tmpl * tmpl,u8 id,unsigned int len)582 static inline void nft_set_ext_add_length(struct nft_set_ext_tmpl *tmpl, u8 id,
583 unsigned int len)
584 {
585 tmpl->len = ALIGN(tmpl->len, nft_set_ext_types[id].align);
586 BUG_ON(tmpl->len > U8_MAX);
587 tmpl->offset[id] = tmpl->len;
588 tmpl->len += nft_set_ext_types[id].len + len;
589 }
590
nft_set_ext_add(struct nft_set_ext_tmpl * tmpl,u8 id)591 static inline void nft_set_ext_add(struct nft_set_ext_tmpl *tmpl, u8 id)
592 {
593 nft_set_ext_add_length(tmpl, id, 0);
594 }
595
nft_set_ext_init(struct nft_set_ext * ext,const struct nft_set_ext_tmpl * tmpl)596 static inline void nft_set_ext_init(struct nft_set_ext *ext,
597 const struct nft_set_ext_tmpl *tmpl)
598 {
599 memcpy(ext->offset, tmpl->offset, sizeof(ext->offset));
600 }
601
__nft_set_ext_exists(const struct nft_set_ext * ext,u8 id)602 static inline bool __nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
603 {
604 return !!ext->offset[id];
605 }
606
nft_set_ext_exists(const struct nft_set_ext * ext,u8 id)607 static inline bool nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
608 {
609 return ext && __nft_set_ext_exists(ext, id);
610 }
611
nft_set_ext(const struct nft_set_ext * ext,u8 id)612 static inline void *nft_set_ext(const struct nft_set_ext *ext, u8 id)
613 {
614 return (void *)ext + ext->offset[id];
615 }
616
nft_set_ext_key(const struct nft_set_ext * ext)617 static inline struct nft_data *nft_set_ext_key(const struct nft_set_ext *ext)
618 {
619 return nft_set_ext(ext, NFT_SET_EXT_KEY);
620 }
621
nft_set_ext_key_end(const struct nft_set_ext * ext)622 static inline struct nft_data *nft_set_ext_key_end(const struct nft_set_ext *ext)
623 {
624 return nft_set_ext(ext, NFT_SET_EXT_KEY_END);
625 }
626
nft_set_ext_data(const struct nft_set_ext * ext)627 static inline struct nft_data *nft_set_ext_data(const struct nft_set_ext *ext)
628 {
629 return nft_set_ext(ext, NFT_SET_EXT_DATA);
630 }
631
nft_set_ext_flags(const struct nft_set_ext * ext)632 static inline u8 *nft_set_ext_flags(const struct nft_set_ext *ext)
633 {
634 return nft_set_ext(ext, NFT_SET_EXT_FLAGS);
635 }
636
nft_set_ext_timeout(const struct nft_set_ext * ext)637 static inline u64 *nft_set_ext_timeout(const struct nft_set_ext *ext)
638 {
639 return nft_set_ext(ext, NFT_SET_EXT_TIMEOUT);
640 }
641
nft_set_ext_expiration(const struct nft_set_ext * ext)642 static inline u64 *nft_set_ext_expiration(const struct nft_set_ext *ext)
643 {
644 return nft_set_ext(ext, NFT_SET_EXT_EXPIRATION);
645 }
646
nft_set_ext_userdata(const struct nft_set_ext * ext)647 static inline struct nft_userdata *nft_set_ext_userdata(const struct nft_set_ext *ext)
648 {
649 return nft_set_ext(ext, NFT_SET_EXT_USERDATA);
650 }
651
nft_set_ext_expr(const struct nft_set_ext * ext)652 static inline struct nft_expr *nft_set_ext_expr(const struct nft_set_ext *ext)
653 {
654 return nft_set_ext(ext, NFT_SET_EXT_EXPR);
655 }
656
nft_set_elem_expired(const struct nft_set_ext * ext)657 static inline bool nft_set_elem_expired(const struct nft_set_ext *ext)
658 {
659 return nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION) &&
660 time_is_before_eq_jiffies64(*nft_set_ext_expiration(ext));
661 }
662
nft_set_elem_ext(const struct nft_set * set,void * elem)663 static inline struct nft_set_ext *nft_set_elem_ext(const struct nft_set *set,
664 void *elem)
665 {
666 return elem + set->ops->elemsize;
667 }
668
nft_set_ext_obj(const struct nft_set_ext * ext)669 static inline struct nft_object **nft_set_ext_obj(const struct nft_set_ext *ext)
670 {
671 return nft_set_ext(ext, NFT_SET_EXT_OBJREF);
672 }
673
674 struct nft_expr *nft_set_elem_expr_alloc(const struct nft_ctx *ctx,
675 const struct nft_set *set,
676 const struct nlattr *attr);
677
678 void *nft_set_elem_init(const struct nft_set *set,
679 const struct nft_set_ext_tmpl *tmpl,
680 const u32 *key, const u32 *key_end, const u32 *data,
681 u64 timeout, u64 expiration, gfp_t gfp);
682 void nft_set_elem_destroy(const struct nft_set *set, void *elem,
683 bool destroy_expr);
684
685 /**
686 * struct nft_set_gc_batch_head - nf_tables set garbage collection batch
687 *
688 * @rcu: rcu head
689 * @set: set the elements belong to
690 * @cnt: count of elements
691 */
692 struct nft_set_gc_batch_head {
693 struct rcu_head rcu;
694 const struct nft_set *set;
695 unsigned int cnt;
696 };
697
698 #define NFT_SET_GC_BATCH_SIZE ((PAGE_SIZE - \
699 sizeof(struct nft_set_gc_batch_head)) / \
700 sizeof(void *))
701
702 /**
703 * struct nft_set_gc_batch - nf_tables set garbage collection batch
704 *
705 * @head: GC batch head
706 * @elems: garbage collection elements
707 */
708 struct nft_set_gc_batch {
709 struct nft_set_gc_batch_head head;
710 void *elems[NFT_SET_GC_BATCH_SIZE];
711 };
712
713 struct nft_set_gc_batch *nft_set_gc_batch_alloc(const struct nft_set *set,
714 gfp_t gfp);
715 void nft_set_gc_batch_release(struct rcu_head *rcu);
716
nft_set_gc_batch_complete(struct nft_set_gc_batch * gcb)717 static inline void nft_set_gc_batch_complete(struct nft_set_gc_batch *gcb)
718 {
719 if (gcb != NULL)
720 call_rcu(&gcb->head.rcu, nft_set_gc_batch_release);
721 }
722
723 static inline struct nft_set_gc_batch *
nft_set_gc_batch_check(const struct nft_set * set,struct nft_set_gc_batch * gcb,gfp_t gfp)724 nft_set_gc_batch_check(const struct nft_set *set, struct nft_set_gc_batch *gcb,
725 gfp_t gfp)
726 {
727 if (gcb != NULL) {
728 if (gcb->head.cnt + 1 < ARRAY_SIZE(gcb->elems))
729 return gcb;
730 nft_set_gc_batch_complete(gcb);
731 }
732 return nft_set_gc_batch_alloc(set, gfp);
733 }
734
nft_set_gc_batch_add(struct nft_set_gc_batch * gcb,void * elem)735 static inline void nft_set_gc_batch_add(struct nft_set_gc_batch *gcb,
736 void *elem)
737 {
738 gcb->elems[gcb->head.cnt++] = elem;
739 }
740
741 struct nft_expr_ops;
742 /**
743 * struct nft_expr_type - nf_tables expression type
744 *
745 * @select_ops: function to select nft_expr_ops
746 * @release_ops: release nft_expr_ops
747 * @ops: default ops, used when no select_ops functions is present
748 * @list: used internally
749 * @name: Identifier
750 * @owner: module reference
751 * @policy: netlink attribute policy
752 * @maxattr: highest netlink attribute number
753 * @family: address family for AF-specific types
754 * @flags: expression type flags
755 */
756 struct nft_expr_type {
757 const struct nft_expr_ops *(*select_ops)(const struct nft_ctx *,
758 const struct nlattr * const tb[]);
759 void (*release_ops)(const struct nft_expr_ops *ops);
760 const struct nft_expr_ops *ops;
761 struct list_head list;
762 const char *name;
763 struct module *owner;
764 const struct nla_policy *policy;
765 unsigned int maxattr;
766 u8 family;
767 u8 flags;
768 };
769
770 #define NFT_EXPR_STATEFUL 0x1
771 #define NFT_EXPR_GC 0x2
772
773 enum nft_trans_phase {
774 NFT_TRANS_PREPARE,
775 NFT_TRANS_ABORT,
776 NFT_TRANS_COMMIT,
777 NFT_TRANS_RELEASE
778 };
779
780 struct nft_flow_rule;
781 struct nft_offload_ctx;
782
783 /**
784 * struct nft_expr_ops - nf_tables expression operations
785 *
786 * @eval: Expression evaluation function
787 * @size: full expression size, including private data size
788 * @init: initialization function
789 * @activate: activate expression in the next generation
790 * @deactivate: deactivate expression in next generation
791 * @destroy: destruction function, called after synchronize_rcu
792 * @dump: function to dump parameters
793 * @type: expression type
794 * @validate: validate expression, called during loop detection
795 * @data: extra data to attach to this expression operation
796 */
797 struct nft_expr;
798 struct nft_expr_ops {
799 void (*eval)(const struct nft_expr *expr,
800 struct nft_regs *regs,
801 const struct nft_pktinfo *pkt);
802 int (*clone)(struct nft_expr *dst,
803 const struct nft_expr *src);
804 unsigned int size;
805
806 int (*init)(const struct nft_ctx *ctx,
807 const struct nft_expr *expr,
808 const struct nlattr * const tb[]);
809 void (*activate)(const struct nft_ctx *ctx,
810 const struct nft_expr *expr);
811 void (*deactivate)(const struct nft_ctx *ctx,
812 const struct nft_expr *expr,
813 enum nft_trans_phase phase);
814 void (*destroy)(const struct nft_ctx *ctx,
815 const struct nft_expr *expr);
816 void (*destroy_clone)(const struct nft_ctx *ctx,
817 const struct nft_expr *expr);
818 int (*dump)(struct sk_buff *skb,
819 const struct nft_expr *expr);
820 int (*validate)(const struct nft_ctx *ctx,
821 const struct nft_expr *expr,
822 const struct nft_data **data);
823 bool (*gc)(struct net *net,
824 const struct nft_expr *expr);
825 int (*offload)(struct nft_offload_ctx *ctx,
826 struct nft_flow_rule *flow,
827 const struct nft_expr *expr);
828 u32 offload_flags;
829 const struct nft_expr_type *type;
830 void *data;
831 };
832
833 #define NFT_EXPR_MAXATTR 16
834 #define NFT_EXPR_SIZE(size) (sizeof(struct nft_expr) + \
835 ALIGN(size, __alignof__(struct nft_expr)))
836
837 /**
838 * struct nft_expr - nf_tables expression
839 *
840 * @ops: expression ops
841 * @data: expression private data
842 */
843 struct nft_expr {
844 const struct nft_expr_ops *ops;
845 unsigned char data[]
846 __attribute__((aligned(__alignof__(u64))));
847 };
848
nft_expr_priv(const struct nft_expr * expr)849 static inline void *nft_expr_priv(const struct nft_expr *expr)
850 {
851 return (void *)expr->data;
852 }
853
854 int nft_expr_clone(struct nft_expr *dst, struct nft_expr *src);
855 void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr);
856 int nft_expr_dump(struct sk_buff *skb, unsigned int attr,
857 const struct nft_expr *expr);
858
859 /**
860 * struct nft_rule - nf_tables rule
861 *
862 * @list: used internally
863 * @handle: rule handle
864 * @genmask: generation mask
865 * @dlen: length of expression data
866 * @udata: user data is appended to the rule
867 * @data: expression data
868 */
869 struct nft_rule {
870 struct list_head list;
871 u64 handle:42,
872 genmask:2,
873 dlen:12,
874 udata:1;
875 unsigned char data[]
876 __attribute__((aligned(__alignof__(struct nft_expr))));
877 };
878
nft_expr_first(const struct nft_rule * rule)879 static inline struct nft_expr *nft_expr_first(const struct nft_rule *rule)
880 {
881 return (struct nft_expr *)&rule->data[0];
882 }
883
nft_expr_next(const struct nft_expr * expr)884 static inline struct nft_expr *nft_expr_next(const struct nft_expr *expr)
885 {
886 return ((void *)expr) + expr->ops->size;
887 }
888
nft_expr_last(const struct nft_rule * rule)889 static inline struct nft_expr *nft_expr_last(const struct nft_rule *rule)
890 {
891 return (struct nft_expr *)&rule->data[rule->dlen];
892 }
893
nft_expr_more(const struct nft_rule * rule,const struct nft_expr * expr)894 static inline bool nft_expr_more(const struct nft_rule *rule,
895 const struct nft_expr *expr)
896 {
897 return expr != nft_expr_last(rule) && expr->ops;
898 }
899
nft_userdata(const struct nft_rule * rule)900 static inline struct nft_userdata *nft_userdata(const struct nft_rule *rule)
901 {
902 return (void *)&rule->data[rule->dlen];
903 }
904
905 void nf_tables_rule_release(const struct nft_ctx *ctx, struct nft_rule *rule);
906
nft_set_elem_update_expr(const struct nft_set_ext * ext,struct nft_regs * regs,const struct nft_pktinfo * pkt)907 static inline void nft_set_elem_update_expr(const struct nft_set_ext *ext,
908 struct nft_regs *regs,
909 const struct nft_pktinfo *pkt)
910 {
911 struct nft_expr *expr;
912
913 if (__nft_set_ext_exists(ext, NFT_SET_EXT_EXPR)) {
914 expr = nft_set_ext_expr(ext);
915 expr->ops->eval(expr, regs, pkt);
916 }
917 }
918
919 /*
920 * The last pointer isn't really necessary, but the compiler isn't able to
921 * determine that the result of nft_expr_last() is always the same since it
922 * can't assume that the dlen value wasn't changed within calls in the loop.
923 */
924 #define nft_rule_for_each_expr(expr, last, rule) \
925 for ((expr) = nft_expr_first(rule), (last) = nft_expr_last(rule); \
926 (expr) != (last); \
927 (expr) = nft_expr_next(expr))
928
929 #define NFT_CHAIN_POLICY_UNSET U8_MAX
930
931 /**
932 * struct nft_chain - nf_tables chain
933 *
934 * @rules: list of rules in the chain
935 * @list: used internally
936 * @rhlhead: used internally
937 * @table: table that this chain belongs to
938 * @handle: chain handle
939 * @use: number of jump references to this chain
940 * @flags: bitmask of enum nft_chain_flags
941 * @name: name of the chain
942 */
943 struct nft_chain {
944 struct nft_rule *__rcu *rules_gen_0;
945 struct nft_rule *__rcu *rules_gen_1;
946 struct list_head rules;
947 struct list_head list;
948 struct rhlist_head rhlhead;
949 struct nft_table *table;
950 u64 handle;
951 u32 use;
952 u8 flags:5,
953 bound:1,
954 genmask:2;
955 char *name;
956 u16 udlen;
957 u8 *udata;
958
959 /* Only used during control plane commit phase: */
960 struct nft_rule **rules_next;
961 };
962
963 int nft_chain_validate(const struct nft_ctx *ctx, const struct nft_chain *chain);
964
965 enum nft_chain_types {
966 NFT_CHAIN_T_DEFAULT = 0,
967 NFT_CHAIN_T_ROUTE,
968 NFT_CHAIN_T_NAT,
969 NFT_CHAIN_T_MAX
970 };
971
972 /**
973 * struct nft_chain_type - nf_tables chain type info
974 *
975 * @name: name of the type
976 * @type: numeric identifier
977 * @family: address family
978 * @owner: module owner
979 * @hook_mask: mask of valid hooks
980 * @hooks: array of hook functions
981 * @ops_register: base chain register function
982 * @ops_unregister: base chain unregister function
983 */
984 struct nft_chain_type {
985 const char *name;
986 enum nft_chain_types type;
987 int family;
988 struct module *owner;
989 unsigned int hook_mask;
990 nf_hookfn *hooks[NFT_MAX_HOOKS];
991 int (*ops_register)(struct net *net, const struct nf_hook_ops *ops);
992 void (*ops_unregister)(struct net *net, const struct nf_hook_ops *ops);
993 };
994
995 int nft_chain_validate_dependency(const struct nft_chain *chain,
996 enum nft_chain_types type);
997 int nft_chain_validate_hooks(const struct nft_chain *chain,
998 unsigned int hook_flags);
999
nft_chain_is_bound(struct nft_chain * chain)1000 static inline bool nft_chain_is_bound(struct nft_chain *chain)
1001 {
1002 return (chain->flags & NFT_CHAIN_BINDING) && chain->bound;
1003 }
1004
1005 void nft_chain_del(struct nft_chain *chain);
1006 void nf_tables_chain_destroy(struct nft_ctx *ctx);
1007
1008 struct nft_stats {
1009 u64 bytes;
1010 u64 pkts;
1011 struct u64_stats_sync syncp;
1012 };
1013
1014 struct nft_hook {
1015 struct list_head list;
1016 bool inactive;
1017 struct nf_hook_ops ops;
1018 struct rcu_head rcu;
1019 };
1020
1021 /**
1022 * struct nft_base_chain - nf_tables base chain
1023 *
1024 * @ops: netfilter hook ops
1025 * @hook_list: list of netfilter hooks (for NFPROTO_NETDEV family)
1026 * @type: chain type
1027 * @policy: default policy
1028 * @stats: per-cpu chain stats
1029 * @chain: the chain
1030 * @flow_block: flow block (for hardware offload)
1031 */
1032 struct nft_base_chain {
1033 struct nf_hook_ops ops;
1034 struct list_head hook_list;
1035 const struct nft_chain_type *type;
1036 u8 policy;
1037 u8 flags;
1038 struct nft_stats __percpu *stats;
1039 struct nft_chain chain;
1040 struct flow_block flow_block;
1041 };
1042
nft_base_chain(const struct nft_chain * chain)1043 static inline struct nft_base_chain *nft_base_chain(const struct nft_chain *chain)
1044 {
1045 return container_of(chain, struct nft_base_chain, chain);
1046 }
1047
nft_is_base_chain(const struct nft_chain * chain)1048 static inline bool nft_is_base_chain(const struct nft_chain *chain)
1049 {
1050 return chain->flags & NFT_CHAIN_BASE;
1051 }
1052
1053 int __nft_release_basechain(struct nft_ctx *ctx);
1054
1055 unsigned int nft_do_chain(struct nft_pktinfo *pkt, void *priv);
1056
1057 /**
1058 * struct nft_table - nf_tables table
1059 *
1060 * @list: used internally
1061 * @chains_ht: chains in the table
1062 * @chains: same, for stable walks
1063 * @sets: sets in the table
1064 * @objects: stateful objects in the table
1065 * @flowtables: flow tables in the table
1066 * @hgenerator: handle generator state
1067 * @handle: table handle
1068 * @use: number of chain references to this table
1069 * @flags: table flag (see enum nft_table_flags)
1070 * @genmask: generation mask
1071 * @afinfo: address family info
1072 * @name: name of the table
1073 */
1074 struct nft_table {
1075 struct list_head list;
1076 struct rhltable chains_ht;
1077 struct list_head chains;
1078 struct list_head sets;
1079 struct list_head objects;
1080 struct list_head flowtables;
1081 u64 hgenerator;
1082 u64 handle;
1083 u32 use;
1084 u16 family:6,
1085 flags:8,
1086 genmask:2;
1087 char *name;
1088 u16 udlen;
1089 u8 *udata;
1090 };
1091
nft_base_chain_netdev(int family,u32 hooknum)1092 static inline bool nft_base_chain_netdev(int family, u32 hooknum)
1093 {
1094 return family == NFPROTO_NETDEV ||
1095 (family == NFPROTO_INET && hooknum == NF_INET_INGRESS);
1096 }
1097
1098 void nft_register_chain_type(const struct nft_chain_type *);
1099 void nft_unregister_chain_type(const struct nft_chain_type *);
1100
1101 int nft_register_expr(struct nft_expr_type *);
1102 void nft_unregister_expr(struct nft_expr_type *);
1103
1104 int nft_verdict_dump(struct sk_buff *skb, int type,
1105 const struct nft_verdict *v);
1106
1107 /**
1108 * struct nft_object_hash_key - key to lookup nft_object
1109 *
1110 * @name: name of the stateful object to look up
1111 * @table: table the object belongs to
1112 */
1113 struct nft_object_hash_key {
1114 const char *name;
1115 const struct nft_table *table;
1116 };
1117
1118 /**
1119 * struct nft_object - nf_tables stateful object
1120 *
1121 * @list: table stateful object list node
1122 * @key: keys that identify this object
1123 * @rhlhead: nft_objname_ht node
1124 * @genmask: generation mask
1125 * @use: number of references to this stateful object
1126 * @handle: unique object handle
1127 * @ops: object operations
1128 * @data: object data, layout depends on type
1129 */
1130 struct nft_object {
1131 struct list_head list;
1132 struct rhlist_head rhlhead;
1133 struct nft_object_hash_key key;
1134 u32 genmask:2,
1135 use:30;
1136 u64 handle;
1137 u16 udlen;
1138 u8 *udata;
1139 /* runtime data below here */
1140 const struct nft_object_ops *ops ____cacheline_aligned;
1141 unsigned char data[]
1142 __attribute__((aligned(__alignof__(u64))));
1143 };
1144
nft_obj_data(const struct nft_object * obj)1145 static inline void *nft_obj_data(const struct nft_object *obj)
1146 {
1147 return (void *)obj->data;
1148 }
1149
1150 #define nft_expr_obj(expr) *((struct nft_object **)nft_expr_priv(expr))
1151
1152 struct nft_object *nft_obj_lookup(const struct net *net,
1153 const struct nft_table *table,
1154 const struct nlattr *nla, u32 objtype,
1155 u8 genmask);
1156
1157 void nft_obj_notify(struct net *net, const struct nft_table *table,
1158 struct nft_object *obj, u32 portid, u32 seq,
1159 int event, int family, int report, gfp_t gfp);
1160
1161 /**
1162 * struct nft_object_type - stateful object type
1163 *
1164 * @select_ops: function to select nft_object_ops
1165 * @ops: default ops, used when no select_ops functions is present
1166 * @list: list node in list of object types
1167 * @type: stateful object numeric type
1168 * @owner: module owner
1169 * @maxattr: maximum netlink attribute
1170 * @policy: netlink attribute policy
1171 */
1172 struct nft_object_type {
1173 const struct nft_object_ops *(*select_ops)(const struct nft_ctx *,
1174 const struct nlattr * const tb[]);
1175 const struct nft_object_ops *ops;
1176 struct list_head list;
1177 u32 type;
1178 unsigned int maxattr;
1179 struct module *owner;
1180 const struct nla_policy *policy;
1181 };
1182
1183 /**
1184 * struct nft_object_ops - stateful object operations
1185 *
1186 * @eval: stateful object evaluation function
1187 * @size: stateful object size
1188 * @init: initialize object from netlink attributes
1189 * @destroy: release existing stateful object
1190 * @dump: netlink dump stateful object
1191 * @update: update stateful object
1192 */
1193 struct nft_object_ops {
1194 void (*eval)(struct nft_object *obj,
1195 struct nft_regs *regs,
1196 const struct nft_pktinfo *pkt);
1197 unsigned int size;
1198 int (*init)(const struct nft_ctx *ctx,
1199 const struct nlattr *const tb[],
1200 struct nft_object *obj);
1201 void (*destroy)(const struct nft_ctx *ctx,
1202 struct nft_object *obj);
1203 int (*dump)(struct sk_buff *skb,
1204 struct nft_object *obj,
1205 bool reset);
1206 void (*update)(struct nft_object *obj,
1207 struct nft_object *newobj);
1208 const struct nft_object_type *type;
1209 };
1210
1211 int nft_register_obj(struct nft_object_type *obj_type);
1212 void nft_unregister_obj(struct nft_object_type *obj_type);
1213
1214 #define NFT_NETDEVICE_MAX 256
1215
1216 /**
1217 * struct nft_flowtable - nf_tables flow table
1218 *
1219 * @list: flow table list node in table list
1220 * @table: the table the flow table is contained in
1221 * @name: name of this flow table
1222 * @hooknum: hook number
1223 * @ops_len: number of hooks in array
1224 * @genmask: generation mask
1225 * @use: number of references to this flow table
1226 * @handle: unique object handle
1227 * @dev_name: array of device names
1228 * @data: rhashtable and garbage collector
1229 * @ops: array of hooks
1230 */
1231 struct nft_flowtable {
1232 struct list_head list;
1233 struct nft_table *table;
1234 char *name;
1235 int hooknum;
1236 int ops_len;
1237 u32 genmask:2,
1238 use:30;
1239 u64 handle;
1240 /* runtime data below here */
1241 struct list_head hook_list ____cacheline_aligned;
1242 struct nf_flowtable data;
1243 };
1244
1245 struct nft_flowtable *nft_flowtable_lookup(const struct nft_table *table,
1246 const struct nlattr *nla,
1247 u8 genmask);
1248
1249 void nf_tables_deactivate_flowtable(const struct nft_ctx *ctx,
1250 struct nft_flowtable *flowtable,
1251 enum nft_trans_phase phase);
1252
1253 void nft_register_flowtable_type(struct nf_flowtable_type *type);
1254 void nft_unregister_flowtable_type(struct nf_flowtable_type *type);
1255
1256 /**
1257 * struct nft_traceinfo - nft tracing information and state
1258 *
1259 * @pkt: pktinfo currently processed
1260 * @basechain: base chain currently processed
1261 * @chain: chain currently processed
1262 * @rule: rule that was evaluated
1263 * @verdict: verdict given by rule
1264 * @type: event type (enum nft_trace_types)
1265 * @packet_dumped: packet headers sent in a previous traceinfo message
1266 * @trace: other struct members are initialised
1267 */
1268 struct nft_traceinfo {
1269 const struct nft_pktinfo *pkt;
1270 const struct nft_base_chain *basechain;
1271 const struct nft_chain *chain;
1272 const struct nft_rule *rule;
1273 const struct nft_verdict *verdict;
1274 enum nft_trace_types type;
1275 bool packet_dumped;
1276 bool trace;
1277 };
1278
1279 void nft_trace_init(struct nft_traceinfo *info, const struct nft_pktinfo *pkt,
1280 const struct nft_verdict *verdict,
1281 const struct nft_chain *basechain);
1282
1283 void nft_trace_notify(struct nft_traceinfo *info);
1284
1285 #define MODULE_ALIAS_NFT_CHAIN(family, name) \
1286 MODULE_ALIAS("nft-chain-" __stringify(family) "-" name)
1287
1288 #define MODULE_ALIAS_NFT_AF_EXPR(family, name) \
1289 MODULE_ALIAS("nft-expr-" __stringify(family) "-" name)
1290
1291 #define MODULE_ALIAS_NFT_EXPR(name) \
1292 MODULE_ALIAS("nft-expr-" name)
1293
1294 #define MODULE_ALIAS_NFT_OBJ(type) \
1295 MODULE_ALIAS("nft-obj-" __stringify(type))
1296
1297 #if IS_ENABLED(CONFIG_NF_TABLES)
1298
1299 /*
1300 * The gencursor defines two generations, the currently active and the
1301 * next one. Objects contain a bitmask of 2 bits specifying the generations
1302 * they're active in. A set bit means they're inactive in the generation
1303 * represented by that bit.
1304 *
1305 * New objects start out as inactive in the current and active in the
1306 * next generation. When committing the ruleset the bitmask is cleared,
1307 * meaning they're active in all generations. When removing an object,
1308 * it is set inactive in the next generation. After committing the ruleset,
1309 * the objects are removed.
1310 */
nft_gencursor_next(const struct net * net)1311 static inline unsigned int nft_gencursor_next(const struct net *net)
1312 {
1313 return net->nft.gencursor + 1 == 1 ? 1 : 0;
1314 }
1315
nft_genmask_next(const struct net * net)1316 static inline u8 nft_genmask_next(const struct net *net)
1317 {
1318 return 1 << nft_gencursor_next(net);
1319 }
1320
nft_genmask_cur(const struct net * net)1321 static inline u8 nft_genmask_cur(const struct net *net)
1322 {
1323 /* Use READ_ONCE() to prevent refetching the value for atomicity */
1324 return 1 << READ_ONCE(net->nft.gencursor);
1325 }
1326
1327 #define NFT_GENMASK_ANY ((1 << 0) | (1 << 1))
1328
1329 /*
1330 * Generic transaction helpers
1331 */
1332
1333 /* Check if this object is currently active. */
1334 #define nft_is_active(__net, __obj) \
1335 (((__obj)->genmask & nft_genmask_cur(__net)) == 0)
1336
1337 /* Check if this object is active in the next generation. */
1338 #define nft_is_active_next(__net, __obj) \
1339 (((__obj)->genmask & nft_genmask_next(__net)) == 0)
1340
1341 /* This object becomes active in the next generation. */
1342 #define nft_activate_next(__net, __obj) \
1343 (__obj)->genmask = nft_genmask_cur(__net)
1344
1345 /* This object becomes inactive in the next generation. */
1346 #define nft_deactivate_next(__net, __obj) \
1347 (__obj)->genmask = nft_genmask_next(__net)
1348
1349 /* After committing the ruleset, clear the stale generation bit. */
1350 #define nft_clear(__net, __obj) \
1351 (__obj)->genmask &= ~nft_genmask_next(__net)
1352 #define nft_active_genmask(__obj, __genmask) \
1353 !((__obj)->genmask & __genmask)
1354
1355 /*
1356 * Set element transaction helpers
1357 */
1358
nft_set_elem_active(const struct nft_set_ext * ext,u8 genmask)1359 static inline bool nft_set_elem_active(const struct nft_set_ext *ext,
1360 u8 genmask)
1361 {
1362 return !(ext->genmask & genmask);
1363 }
1364
nft_set_elem_change_active(const struct net * net,const struct nft_set * set,struct nft_set_ext * ext)1365 static inline void nft_set_elem_change_active(const struct net *net,
1366 const struct nft_set *set,
1367 struct nft_set_ext *ext)
1368 {
1369 ext->genmask ^= nft_genmask_next(net);
1370 }
1371
1372 #endif /* IS_ENABLED(CONFIG_NF_TABLES) */
1373
1374 /*
1375 * We use a free bit in the genmask field to indicate the element
1376 * is busy, meaning it is currently being processed either by
1377 * the netlink API or GC.
1378 *
1379 * Even though the genmask is only a single byte wide, this works
1380 * because the extension structure if fully constant once initialized,
1381 * so there are no non-atomic write accesses unless it is already
1382 * marked busy.
1383 */
1384 #define NFT_SET_ELEM_BUSY_MASK (1 << 2)
1385
1386 #if defined(__LITTLE_ENDIAN_BITFIELD)
1387 #define NFT_SET_ELEM_BUSY_BIT 2
1388 #elif defined(__BIG_ENDIAN_BITFIELD)
1389 #define NFT_SET_ELEM_BUSY_BIT (BITS_PER_LONG - BITS_PER_BYTE + 2)
1390 #else
1391 #error
1392 #endif
1393
nft_set_elem_mark_busy(struct nft_set_ext * ext)1394 static inline int nft_set_elem_mark_busy(struct nft_set_ext *ext)
1395 {
1396 unsigned long *word = (unsigned long *)ext;
1397
1398 BUILD_BUG_ON(offsetof(struct nft_set_ext, genmask) != 0);
1399 return test_and_set_bit(NFT_SET_ELEM_BUSY_BIT, word);
1400 }
1401
nft_set_elem_clear_busy(struct nft_set_ext * ext)1402 static inline void nft_set_elem_clear_busy(struct nft_set_ext *ext)
1403 {
1404 unsigned long *word = (unsigned long *)ext;
1405
1406 clear_bit(NFT_SET_ELEM_BUSY_BIT, word);
1407 }
1408
1409 /**
1410 * struct nft_trans - nf_tables object update in transaction
1411 *
1412 * @list: used internally
1413 * @msg_type: message type
1414 * @put_net: ctx->net needs to be put
1415 * @ctx: transaction context
1416 * @data: internal information related to the transaction
1417 */
1418 struct nft_trans {
1419 struct list_head list;
1420 int msg_type;
1421 bool put_net;
1422 struct nft_ctx ctx;
1423 char data[];
1424 };
1425
1426 struct nft_trans_rule {
1427 struct nft_rule *rule;
1428 struct nft_flow_rule *flow;
1429 u32 rule_id;
1430 };
1431
1432 #define nft_trans_rule(trans) \
1433 (((struct nft_trans_rule *)trans->data)->rule)
1434 #define nft_trans_flow_rule(trans) \
1435 (((struct nft_trans_rule *)trans->data)->flow)
1436 #define nft_trans_rule_id(trans) \
1437 (((struct nft_trans_rule *)trans->data)->rule_id)
1438
1439 struct nft_trans_set {
1440 struct nft_set *set;
1441 u32 set_id;
1442 bool bound;
1443 };
1444
1445 #define nft_trans_set(trans) \
1446 (((struct nft_trans_set *)trans->data)->set)
1447 #define nft_trans_set_id(trans) \
1448 (((struct nft_trans_set *)trans->data)->set_id)
1449 #define nft_trans_set_bound(trans) \
1450 (((struct nft_trans_set *)trans->data)->bound)
1451
1452 struct nft_trans_chain {
1453 bool update;
1454 char *name;
1455 struct nft_stats __percpu *stats;
1456 u8 policy;
1457 u32 chain_id;
1458 };
1459
1460 #define nft_trans_chain_update(trans) \
1461 (((struct nft_trans_chain *)trans->data)->update)
1462 #define nft_trans_chain_name(trans) \
1463 (((struct nft_trans_chain *)trans->data)->name)
1464 #define nft_trans_chain_stats(trans) \
1465 (((struct nft_trans_chain *)trans->data)->stats)
1466 #define nft_trans_chain_policy(trans) \
1467 (((struct nft_trans_chain *)trans->data)->policy)
1468 #define nft_trans_chain_id(trans) \
1469 (((struct nft_trans_chain *)trans->data)->chain_id)
1470
1471 struct nft_trans_table {
1472 bool update;
1473 bool enable;
1474 };
1475
1476 #define nft_trans_table_update(trans) \
1477 (((struct nft_trans_table *)trans->data)->update)
1478 #define nft_trans_table_enable(trans) \
1479 (((struct nft_trans_table *)trans->data)->enable)
1480
1481 struct nft_trans_elem {
1482 struct nft_set *set;
1483 struct nft_set_elem elem;
1484 bool bound;
1485 };
1486
1487 #define nft_trans_elem_set(trans) \
1488 (((struct nft_trans_elem *)trans->data)->set)
1489 #define nft_trans_elem(trans) \
1490 (((struct nft_trans_elem *)trans->data)->elem)
1491 #define nft_trans_elem_set_bound(trans) \
1492 (((struct nft_trans_elem *)trans->data)->bound)
1493
1494 struct nft_trans_obj {
1495 struct nft_object *obj;
1496 struct nft_object *newobj;
1497 bool update;
1498 };
1499
1500 #define nft_trans_obj(trans) \
1501 (((struct nft_trans_obj *)trans->data)->obj)
1502 #define nft_trans_obj_newobj(trans) \
1503 (((struct nft_trans_obj *)trans->data)->newobj)
1504 #define nft_trans_obj_update(trans) \
1505 (((struct nft_trans_obj *)trans->data)->update)
1506
1507 struct nft_trans_flowtable {
1508 struct nft_flowtable *flowtable;
1509 bool update;
1510 struct list_head hook_list;
1511 };
1512
1513 #define nft_trans_flowtable(trans) \
1514 (((struct nft_trans_flowtable *)trans->data)->flowtable)
1515 #define nft_trans_flowtable_update(trans) \
1516 (((struct nft_trans_flowtable *)trans->data)->update)
1517 #define nft_trans_flowtable_hooks(trans) \
1518 (((struct nft_trans_flowtable *)trans->data)->hook_list)
1519
1520 int __init nft_chain_filter_init(void);
1521 void nft_chain_filter_fini(void);
1522
1523 void __init nft_chain_route_init(void);
1524 void nft_chain_route_fini(void);
1525
1526 void nf_tables_trans_destroy_flush_work(void);
1527
1528 int nf_msecs_to_jiffies64(const struct nlattr *nla, u64 *result);
1529 __be64 nf_jiffies64_to_msecs(u64 input);
1530
1531 #endif /* _NET_NF_TABLES_H */
1532