Commit | Line | Data |
---|---|---|
b2441318 | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
96518518 PM |
2 | #ifndef _NET_NF_TABLES_H |
3 | #define _NET_NF_TABLES_H | |
4 | ||
a1b840ad | 5 | #include <asm/unaligned.h> |
96518518 PM |
6 | #include <linux/list.h> |
7 | #include <linux/netfilter.h> | |
67a8fc27 | 8 | #include <linux/netfilter/nfnetlink.h> |
0ca743a5 | 9 | #include <linux/netfilter/x_tables.h> |
96518518 | 10 | #include <linux/netfilter/nf_tables.h> |
ce355e20 | 11 | #include <linux/u64_stats_sync.h> |
1b2470e5 | 12 | #include <linux/rhashtable.h> |
3b49e2e9 | 13 | #include <net/netfilter/nf_flow_table.h> |
96518518 | 14 | #include <net/netlink.h> |
14bfb13f | 15 | #include <net/flow_offload.h> |
d59d2f82 | 16 | #include <net/netns/generic.h> |
96518518 | 17 | |
d25e2e93 PNA |
18 | #define NFT_MAX_HOOKS (NF_INET_INGRESS + 1) |
19 | ||
a4cb98f3 PG |
20 | struct module; |
21 | ||
20a69341 PM |
22 | #define NFT_JUMP_STACK_SIZE 16 |
23 | ||
b5bdc6f9 PNA |
24 | enum { |
25 | NFT_PKTINFO_L4PROTO = (1 << 0), | |
c46b38dc | 26 | NFT_PKTINFO_INNER = (1 << 1), |
0e795b37 | 27 | NFT_PKTINFO_INNER_FULL = (1 << 2), |
b5bdc6f9 PNA |
28 | }; |
29 | ||
96518518 PM |
30 | struct nft_pktinfo { |
31 | struct sk_buff *skb; | |
897389de | 32 | const struct nf_hook_state *state; |
b5bdc6f9 | 33 | u8 flags; |
4566bf27 | 34 | u8 tprot; |
897389de | 35 | u16 fragoff; |
e7a1caa6 FW |
36 | u16 thoff; |
37 | u16 inneroff; | |
96518518 PM |
38 | }; |
39 | ||
85554eb9 FW |
40 | static inline struct sock *nft_sk(const struct nft_pktinfo *pkt) |
41 | { | |
897389de | 42 | return pkt->state->sk; |
85554eb9 FW |
43 | } |
44 | ||
2d7b4ace FW |
45 | static inline unsigned int nft_thoff(const struct nft_pktinfo *pkt) |
46 | { | |
897389de | 47 | return pkt->thoff; |
2d7b4ace FW |
48 | } |
49 | ||
0e5a1c7e PNA |
50 | static inline struct net *nft_net(const struct nft_pktinfo *pkt) |
51 | { | |
897389de | 52 | return pkt->state->net; |
0e5a1c7e PNA |
53 | } |
54 | ||
55 | static inline unsigned int nft_hook(const struct nft_pktinfo *pkt) | |
56 | { | |
897389de | 57 | return pkt->state->hook; |
0e5a1c7e PNA |
58 | } |
59 | ||
60 | static inline u8 nft_pf(const struct nft_pktinfo *pkt) | |
61 | { | |
897389de | 62 | return pkt->state->pf; |
0e5a1c7e PNA |
63 | } |
64 | ||
65 | static inline const struct net_device *nft_in(const struct nft_pktinfo *pkt) | |
66 | { | |
897389de | 67 | return pkt->state->in; |
0e5a1c7e PNA |
68 | } |
69 | ||
70 | static inline const struct net_device *nft_out(const struct nft_pktinfo *pkt) | |
71 | { | |
897389de | 72 | return pkt->state->out; |
0e5a1c7e PNA |
73 | } |
74 | ||
0ca743a5 | 75 | static inline void nft_set_pktinfo(struct nft_pktinfo *pkt, |
0ca743a5 | 76 | struct sk_buff *skb, |
073bfd56 | 77 | const struct nf_hook_state *state) |
0ca743a5 PNA |
78 | { |
79 | pkt->skb = skb; | |
897389de | 80 | pkt->state = state; |
0ca743a5 PNA |
81 | } |
82 | ||
f06ad944 | 83 | static inline void nft_set_pktinfo_unspec(struct nft_pktinfo *pkt) |
beac5afa | 84 | { |
b5bdc6f9 | 85 | pkt->flags = 0; |
beac5afa | 86 | pkt->tprot = 0; |
897389de FW |
87 | pkt->thoff = 0; |
88 | pkt->fragoff = 0; | |
beac5afa PNA |
89 | } |
90 | ||
a55e22e9 PM |
91 | /** |
92 | * struct nft_verdict - nf_tables verdict | |
93 | * | |
94 | * @code: nf_tables/netfilter verdict code | |
95 | * @chain: destination chain for NFT_JUMP/NFT_GOTO | |
96 | */ | |
97 | struct nft_verdict { | |
98 | u32 code; | |
99 | struct nft_chain *chain; | |
100 | }; | |
101 | ||
96518518 PM |
102 | struct nft_data { |
103 | union { | |
1ca2e170 PM |
104 | u32 data[4]; |
105 | struct nft_verdict verdict; | |
96518518 PM |
106 | }; |
107 | } __attribute__((aligned(__alignof__(u64)))); | |
108 | ||
642c8eff PNA |
109 | #define NFT_REG32_NUM 20 |
110 | ||
a55e22e9 PM |
111 | /** |
112 | * struct nft_regs - nf_tables register set | |
113 | * | |
114 | * @data: data registers | |
115 | * @verdict: verdict register | |
116 | * | |
117 | * The first four data registers alias to the verdict register. | |
118 | */ | |
119 | struct nft_regs { | |
120 | union { | |
642c8eff | 121 | u32 data[NFT_REG32_NUM]; |
a55e22e9 PM |
122 | struct nft_verdict verdict; |
123 | }; | |
124 | }; | |
125 | ||
12e4ecfa PNA |
126 | struct nft_regs_track { |
127 | struct { | |
128 | const struct nft_expr *selector; | |
129 | const struct nft_expr *bitwise; | |
34cc9e52 | 130 | u8 num_reg; |
12e4ecfa PNA |
131 | } regs[NFT_REG32_NUM]; |
132 | ||
133 | const struct nft_expr *cur; | |
134 | const struct nft_expr *last; | |
135 | }; | |
136 | ||
a1b840ad | 137 | /* Store/load an u8, u16 or u64 integer to/from the u32 data register. |
10596608 LZ |
138 | * |
139 | * Note, when using concatenations, register allocation happens at 32-bit | |
140 | * level. So for store instruction, pad the rest part with zero to avoid | |
141 | * garbage values. | |
142 | */ | |
143 | ||
a1b840ad | 144 | static inline void nft_reg_store8(u32 *dreg, u8 val) |
10596608 LZ |
145 | { |
146 | *dreg = 0; | |
a1b840ad | 147 | *(u8 *)dreg = val; |
10596608 LZ |
148 | } |
149 | ||
7cd9a58d | 150 | static inline u8 nft_reg_load8(const u32 *sreg) |
a1b840ad AJ |
151 | { |
152 | return *(u8 *)sreg; | |
153 | } | |
154 | ||
155 | static inline void nft_reg_store16(u32 *dreg, u16 val) | |
10596608 LZ |
156 | { |
157 | *dreg = 0; | |
a1b840ad | 158 | *(u16 *)dreg = val; |
10596608 LZ |
159 | } |
160 | ||
7278b3c1 FW |
161 | static inline void nft_reg_store_be16(u32 *dreg, __be16 val) |
162 | { | |
163 | nft_reg_store16(dreg, (__force __u16)val); | |
164 | } | |
165 | ||
7cd9a58d | 166 | static inline u16 nft_reg_load16(const u32 *sreg) |
10596608 LZ |
167 | { |
168 | return *(u16 *)sreg; | |
169 | } | |
170 | ||
7278b3c1 FW |
171 | static inline __be16 nft_reg_load_be16(const u32 *sreg) |
172 | { | |
173 | return (__force __be16)nft_reg_load16(sreg); | |
174 | } | |
175 | ||
176 | static inline __be32 nft_reg_load_be32(const u32 *sreg) | |
177 | { | |
178 | return *(__force __be32 *)sreg; | |
179 | } | |
180 | ||
a1b840ad | 181 | static inline void nft_reg_store64(u32 *dreg, u64 val) |
10596608 | 182 | { |
a1b840ad AJ |
183 | put_unaligned(val, (u64 *)dreg); |
184 | } | |
185 | ||
7cd9a58d | 186 | static inline u64 nft_reg_load64(const u32 *sreg) |
a1b840ad AJ |
187 | { |
188 | return get_unaligned((u64 *)sreg); | |
10596608 LZ |
189 | } |
190 | ||
49499c3e PM |
191 | static inline void nft_data_copy(u32 *dst, const struct nft_data *src, |
192 | unsigned int len) | |
96518518 | 193 | { |
1e105e6a FW |
194 | if (len % NFT_REG32_SIZE) |
195 | dst[len / NFT_REG32_SIZE] = 0; | |
49499c3e | 196 | memcpy(dst, src, len); |
96518518 PM |
197 | } |
198 | ||
96518518 | 199 | /** |
20a69341 | 200 | * struct nft_ctx - nf_tables rule/set context |
96518518 | 201 | * |
99633ab2 | 202 | * @net: net namespace |
96518518 PM |
203 | * @table: the table the chain is contained in |
204 | * @chain: the chain the rule is contained in | |
0ca743a5 | 205 | * @nla: netlink attributes |
128ad332 PNA |
206 | * @portid: netlink portID of the original message |
207 | * @seq: netlink sequence number | |
36596dad | 208 | * @family: protocol family |
26b2f552 | 209 | * @level: depth of the chains |
128ad332 | 210 | * @report: notify via unicast netlink message |
96518518 PM |
211 | */ |
212 | struct nft_ctx { | |
99633ab2 | 213 | struct net *net; |
7c95f6d8 PNA |
214 | struct nft_table *table; |
215 | struct nft_chain *chain; | |
0ca743a5 | 216 | const struct nlattr * const *nla; |
128ad332 PNA |
217 | u32 portid; |
218 | u32 seq; | |
c9626a2c | 219 | u16 flags; |
36596dad | 220 | u8 family; |
26b2f552 | 221 | u8 level; |
128ad332 | 222 | bool report; |
96518518 PM |
223 | }; |
224 | ||
f323ef3a PNA |
225 | enum nft_data_desc_flags { |
226 | NFT_DATA_DESC_SETELEM = (1 << 0), | |
227 | }; | |
228 | ||
96518518 PM |
229 | struct nft_data_desc { |
230 | enum nft_data_types type; | |
341b6941 | 231 | unsigned int size; |
96518518 | 232 | unsigned int len; |
f323ef3a | 233 | unsigned int flags; |
96518518 PM |
234 | }; |
235 | ||
341b6941 | 236 | int nft_data_init(const struct nft_ctx *ctx, struct nft_data *data, |
5eccdfaa | 237 | struct nft_data_desc *desc, const struct nlattr *nla); |
bb7b40ae | 238 | void nft_data_hold(const struct nft_data *data, enum nft_data_types type); |
59105446 | 239 | void nft_data_release(const struct nft_data *data, enum nft_data_types type); |
5eccdfaa JP |
240 | int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data, |
241 | enum nft_data_types type, unsigned int len); | |
96518518 PM |
242 | |
243 | static inline enum nft_data_types nft_dreg_to_type(enum nft_registers reg) | |
244 | { | |
245 | return reg == NFT_REG_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE; | |
246 | } | |
247 | ||
20a69341 PM |
248 | static inline enum nft_registers nft_type_to_reg(enum nft_data_types type) |
249 | { | |
bf798657 | 250 | return type == NFT_DATA_VERDICT ? NFT_REG_VERDICT : NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE; |
20a69341 PM |
251 | } |
252 | ||
f1d505bb | 253 | int nft_parse_u32_check(const struct nlattr *attr, int max, u32 *dest); |
b1c96ed3 PM |
254 | int nft_dump_register(struct sk_buff *skb, unsigned int attr, unsigned int reg); |
255 | ||
4f16d25c | 256 | int nft_parse_register_load(const struct nlattr *attr, u8 *sreg, u32 len); |
345023b0 PNA |
257 | int nft_parse_register_store(const struct nft_ctx *ctx, |
258 | const struct nlattr *attr, u8 *dreg, | |
259 | const struct nft_data *data, | |
260 | enum nft_data_types type, unsigned int len); | |
96518518 | 261 | |
86f1ec32 PM |
262 | /** |
263 | * struct nft_userdata - user defined data associated with an object | |
264 | * | |
265 | * @len: length of the data | |
266 | * @data: content | |
267 | * | |
268 | * The presence of user data is indicated in an object specific fashion, | |
269 | * so a length of zero can't occur and the value "len" indicates data | |
270 | * of length len + 1. | |
271 | */ | |
272 | struct nft_userdata { | |
273 | u8 len; | |
6daf1414 | 274 | unsigned char data[]; |
86f1ec32 PM |
275 | }; |
276 | ||
20a69341 PM |
277 | /** |
278 | * struct nft_set_elem - generic representation of set elements | |
279 | * | |
20a69341 | 280 | * @key: element key |
7b225d0b | 281 | * @key_end: closing element key |
fe2811eb | 282 | * @priv: element private data and extensions |
20a69341 PM |
283 | */ |
284 | struct nft_set_elem { | |
7d740264 PM |
285 | union { |
286 | u32 buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)]; | |
287 | struct nft_data val; | |
288 | } key; | |
7b225d0b PNA |
289 | union { |
290 | u32 buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)]; | |
291 | struct nft_data val; | |
292 | } key_end; | |
fdb9c405 PNA |
293 | union { |
294 | u32 buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)]; | |
295 | struct nft_data val; | |
296 | } data; | |
fe2811eb | 297 | void *priv; |
20a69341 PM |
298 | }; |
299 | ||
300 | struct nft_set; | |
301 | struct nft_set_iter { | |
8588ac09 | 302 | u8 genmask; |
20a69341 PM |
303 | unsigned int count; |
304 | unsigned int skip; | |
305 | int err; | |
306 | int (*fn)(const struct nft_ctx *ctx, | |
de70185d | 307 | struct nft_set *set, |
20a69341 | 308 | const struct nft_set_iter *iter, |
de70185d | 309 | struct nft_set_elem *elem); |
20a69341 PM |
310 | }; |
311 | ||
c50b960c PM |
312 | /** |
313 | * struct nft_set_desc - description of set elements | |
314 | * | |
bed4a63e | 315 | * @ktype: key type |
c50b960c | 316 | * @klen: key length |
bed4a63e | 317 | * @dtype: data type |
c50b960c | 318 | * @dlen: data length |
bed4a63e PNA |
319 | * @objtype: object type |
320 | * @flags: flags | |
c50b960c | 321 | * @size: number of set elements |
bed4a63e PNA |
322 | * @policy: set policy |
323 | * @gc_int: garbage collector interval | |
f3a2181e SB |
324 | * @field_len: length of each field in concatenation, bytes |
325 | * @field_count: number of concatenated fields in element | |
d56aab26 | 326 | * @expr: set must support for expressions |
c50b960c PM |
327 | */ |
328 | struct nft_set_desc { | |
bed4a63e | 329 | u32 ktype; |
c50b960c | 330 | unsigned int klen; |
bed4a63e | 331 | u32 dtype; |
c50b960c | 332 | unsigned int dlen; |
bed4a63e | 333 | u32 objtype; |
c50b960c | 334 | unsigned int size; |
bed4a63e PNA |
335 | u32 policy; |
336 | u32 gc_int; | |
337 | u64 timeout; | |
f3a2181e SB |
338 | u8 field_len[NFT_REG32_COUNT]; |
339 | u8 field_count; | |
d56aab26 | 340 | bool expr; |
c50b960c PM |
341 | }; |
342 | ||
343 | /** | |
344 | * enum nft_set_class - performance class | |
345 | * | |
346 | * @NFT_LOOKUP_O_1: constant, O(1) | |
347 | * @NFT_LOOKUP_O_LOG_N: logarithmic, O(log N) | |
348 | * @NFT_LOOKUP_O_N: linear, O(N) | |
349 | */ | |
350 | enum nft_set_class { | |
351 | NFT_SET_CLASS_O_1, | |
352 | NFT_SET_CLASS_O_LOG_N, | |
353 | NFT_SET_CLASS_O_N, | |
354 | }; | |
355 | ||
356 | /** | |
357 | * struct nft_set_estimate - estimation of memory and performance | |
358 | * characteristics | |
359 | * | |
360 | * @size: required memory | |
55af753c | 361 | * @lookup: lookup performance class |
0b5a7874 | 362 | * @space: memory class |
c50b960c PM |
363 | */ |
364 | struct nft_set_estimate { | |
4ef360dd | 365 | u64 size; |
55af753c | 366 | enum nft_set_class lookup; |
0b5a7874 | 367 | enum nft_set_class space; |
c50b960c PM |
368 | }; |
369 | ||
92b211a2 PNA |
370 | #define NFT_EXPR_MAXATTR 16 |
371 | #define NFT_EXPR_SIZE(size) (sizeof(struct nft_expr) + \ | |
372 | ALIGN(size, __alignof__(struct nft_expr))) | |
373 | ||
374 | /** | |
375 | * struct nft_expr - nf_tables expression | |
376 | * | |
377 | * @ops: expression ops | |
378 | * @data: expression private data | |
379 | */ | |
380 | struct nft_expr { | |
381 | const struct nft_expr_ops *ops; | |
382 | unsigned char data[] | |
383 | __attribute__((aligned(__alignof__(u64)))); | |
384 | }; | |
385 | ||
386 | static inline void *nft_expr_priv(const struct nft_expr *expr) | |
387 | { | |
388 | return (void *)expr->data; | |
389 | } | |
390 | ||
3a07327d PNA |
391 | struct nft_expr_info; |
392 | ||
393 | int nft_expr_inner_parse(const struct nft_ctx *ctx, const struct nlattr *nla, | |
394 | struct nft_expr_info *info); | |
92b211a2 PNA |
395 | int nft_expr_clone(struct nft_expr *dst, struct nft_expr *src); |
396 | void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr); | |
397 | int nft_expr_dump(struct sk_buff *skb, unsigned int attr, | |
8daa8fde | 398 | const struct nft_expr *expr, bool reset); |
be5650f8 PNA |
399 | bool nft_expr_reduce_bitwise(struct nft_regs_track *track, |
400 | const struct nft_expr *expr); | |
92b211a2 | 401 | |
b2832dd6 PM |
402 | struct nft_set_ext; |
403 | ||
20a69341 PM |
404 | /** |
405 | * struct nft_set_ops - nf_tables set operations | |
406 | * | |
407 | * @lookup: look up an element within the set | |
d0a8d877 AJ |
408 | * @update: update an element if exists, add it if doesn't exist |
409 | * @delete: delete an element | |
20a69341 | 410 | * @insert: insert new element into set |
cc02e457 | 411 | * @activate: activate new element in the next generation |
8411b644 | 412 | * @deactivate: lookup for element and deactivate it in the next generation |
1ba1c414 | 413 | * @flush: deactivate element in the next generation |
20a69341 | 414 | * @remove: remove element from set |
d0a8d877 | 415 | * @walk: iterate over all set elements |
ba0e4d99 | 416 | * @get: get set elements |
20a69341 PM |
417 | * @privsize: function to return size of set private data |
418 | * @init: initialize private data of new set instance | |
419 | * @destroy: destroy private data of set instance | |
fe2811eb | 420 | * @elemsize: element private size |
d0a8d877 AJ |
421 | * |
422 | * Operations lookup, update and delete have simpler interfaces, are faster | |
423 | * and currently only used in the packet path. All the rest are slower, | |
424 | * control plane functions. | |
20a69341 PM |
425 | */ |
426 | struct nft_set_ops { | |
42a55769 PNA |
427 | bool (*lookup)(const struct net *net, |
428 | const struct nft_set *set, | |
8cd8937a | 429 | const u32 *key, |
b2832dd6 | 430 | const struct nft_set_ext **ext); |
22fe54d5 | 431 | bool (*update)(struct nft_set *set, |
8cd8937a | 432 | const u32 *key, |
22fe54d5 PM |
433 | void *(*new)(struct nft_set *, |
434 | const struct nft_expr *, | |
a55e22e9 | 435 | struct nft_regs *), |
22fe54d5 | 436 | const struct nft_expr *expr, |
a55e22e9 | 437 | struct nft_regs *regs, |
22fe54d5 | 438 | const struct nft_set_ext **ext); |
d0a8d877 AJ |
439 | bool (*delete)(const struct nft_set *set, |
440 | const u32 *key); | |
22fe54d5 | 441 | |
42a55769 PNA |
442 | int (*insert)(const struct net *net, |
443 | const struct nft_set *set, | |
c016c7e4 PNA |
444 | const struct nft_set_elem *elem, |
445 | struct nft_set_ext **ext); | |
42a55769 PNA |
446 | void (*activate)(const struct net *net, |
447 | const struct nft_set *set, | |
cc02e457 | 448 | const struct nft_set_elem *elem); |
42a55769 PNA |
449 | void * (*deactivate)(const struct net *net, |
450 | const struct nft_set *set, | |
cc02e457 | 451 | const struct nft_set_elem *elem); |
1ba1c414 PNA |
452 | bool (*flush)(const struct net *net, |
453 | const struct nft_set *set, | |
454 | void *priv); | |
5cb82a38 PNA |
455 | void (*remove)(const struct net *net, |
456 | const struct nft_set *set, | |
20a69341 PM |
457 | const struct nft_set_elem *elem); |
458 | void (*walk)(const struct nft_ctx *ctx, | |
de70185d | 459 | struct nft_set *set, |
20a69341 | 460 | struct nft_set_iter *iter); |
ba0e4d99 PNA |
461 | void * (*get)(const struct net *net, |
462 | const struct nft_set *set, | |
463 | const struct nft_set_elem *elem, | |
464 | unsigned int flags); | |
20a69341 | 465 | |
4ef360dd | 466 | u64 (*privsize)(const struct nlattr * const nla[], |
347b408d | 467 | const struct nft_set_desc *desc); |
c50b960c PM |
468 | bool (*estimate)(const struct nft_set_desc *desc, |
469 | u32 features, | |
470 | struct nft_set_estimate *est); | |
20a69341 | 471 | int (*init)(const struct nft_set *set, |
c50b960c | 472 | const struct nft_set_desc *desc, |
20a69341 PM |
473 | const struct nlattr * const nla[]); |
474 | void (*destroy)(const struct nft_set *set); | |
79b174ad | 475 | void (*gc_init)(const struct nft_set *set); |
20a69341 | 476 | |
fe2811eb | 477 | unsigned int elemsize; |
71cc0873 PS |
478 | }; |
479 | ||
480 | /** | |
481 | * struct nft_set_type - nf_tables set type | |
482 | * | |
483 | * @ops: set ops for this type | |
71cc0873 PS |
484 | * @features: features supported by the implementation |
485 | */ | |
486 | struct nft_set_type { | |
487 | const struct nft_set_ops ops; | |
20a69341 PM |
488 | u32 features; |
489 | }; | |
71cc0873 | 490 | #define to_set_type(o) container_of(o, struct nft_set_type, ops) |
20a69341 | 491 | |
563125a7 PNA |
492 | struct nft_set_elem_expr { |
493 | u8 size; | |
494 | unsigned char data[] | |
495 | __attribute__((aligned(__alignof__(struct nft_expr)))); | |
496 | }; | |
497 | ||
498 | #define nft_setelem_expr_at(__elem_expr, __offset) \ | |
499 | ((struct nft_expr *)&__elem_expr->data[__offset]) | |
500 | ||
501 | #define nft_setelem_expr_foreach(__expr, __elem_expr, __size) \ | |
502 | for (__expr = nft_setelem_expr_at(__elem_expr, 0), __size = 0; \ | |
503 | __size < (__elem_expr)->size; \ | |
504 | __size += (__expr)->ops->size, __expr = ((void *)(__expr)) + (__expr)->ops->size) | |
505 | ||
8cfd9b0f PNA |
506 | #define NFT_SET_EXPR_MAX 2 |
507 | ||
20a69341 PM |
508 | /** |
509 | * struct nft_set - nf_tables set instance | |
510 | * | |
511 | * @list: table set list node | |
512 | * @bindings: list of set bindings | |
3453c927 PNA |
513 | * @table: table this set belongs to |
514 | * @net: netnamespace this set belongs to | |
20a69341 | 515 | * @name: name of the set |
3ecbfd65 | 516 | * @handle: unique handle of the set |
20a69341 PM |
517 | * @ktype: key type (numeric type defined by userspace, not used in the kernel) |
518 | * @dtype: data type (verdict or numeric type defined by userspace) | |
8aeff920 | 519 | * @objtype: object type (see NFT_OBJECT_* definitions) |
c50b960c | 520 | * @size: maximum set size |
f3a2181e SB |
521 | * @field_len: length of each field in concatenation, bytes |
522 | * @field_count: number of concatenated fields in element | |
273fe3f1 | 523 | * @use: number of rules references to this set |
c50b960c | 524 | * @nelems: number of elements |
3dd0673a | 525 | * @ndeact: number of deactivated elements queued for removal |
d3e2a111 | 526 | * @timeout: default timeout value in jiffies |
761da293 | 527 | * @gc_int: garbage collection interval in msecs |
9363dc4b | 528 | * @policy: set parameterization (see enum nft_set_policies) |
e6d8ecac CFG |
529 | * @udlen: user data length |
530 | * @udata: user data | |
65038428 | 531 | * @expr: stateful expression |
20a69341 PM |
532 | * @ops: set ops |
533 | * @flags: set flags | |
37a9cc52 | 534 | * @genmask: generation mask |
20a69341 PM |
535 | * @klen: key length |
536 | * @dlen: data length | |
537 | * @data: private set data | |
538 | */ | |
539 | struct nft_set { | |
540 | struct list_head list; | |
541 | struct list_head bindings; | |
3453c927 PNA |
542 | struct nft_table *table; |
543 | possible_net_t net; | |
38745490 | 544 | char *name; |
3ecbfd65 | 545 | u64 handle; |
20a69341 PM |
546 | u32 ktype; |
547 | u32 dtype; | |
8aeff920 | 548 | u32 objtype; |
c50b960c | 549 | u32 size; |
f3a2181e SB |
550 | u8 field_len[NFT_REG32_COUNT]; |
551 | u8 field_count; | |
273fe3f1 | 552 | u32 use; |
3dd0673a PM |
553 | atomic_t nelems; |
554 | u32 ndeact; | |
761da293 PM |
555 | u64 timeout; |
556 | u32 gc_int; | |
9363dc4b | 557 | u16 policy; |
e6d8ecac CFG |
558 | u16 udlen; |
559 | unsigned char *udata; | |
20a69341 PM |
560 | /* runtime data below here */ |
561 | const struct nft_set_ops *ops ____cacheline_aligned; | |
6a0a8d10 | 562 | u16 flags:14, |
37a9cc52 | 563 | genmask:2; |
20a69341 PM |
564 | u8 klen; |
565 | u8 dlen; | |
8cfd9b0f PNA |
566 | u8 num_exprs; |
567 | struct nft_expr *exprs[NFT_SET_EXPR_MAX]; | |
aaa31047 | 568 | struct list_head catchall_list; |
20a69341 PM |
569 | unsigned char data[] |
570 | __attribute__((aligned(__alignof__(u64)))); | |
571 | }; | |
572 | ||
408070d6 PNA |
573 | static inline bool nft_set_is_anonymous(const struct nft_set *set) |
574 | { | |
575 | return set->flags & NFT_SET_ANONYMOUS; | |
576 | } | |
577 | ||
20a69341 PM |
578 | static inline void *nft_set_priv(const struct nft_set *set) |
579 | { | |
580 | return (void *)set->data; | |
581 | } | |
582 | ||
9d098292 PM |
583 | static inline struct nft_set *nft_set_container_of(const void *priv) |
584 | { | |
585 | return (void *)priv - offsetof(struct nft_set, data); | |
586 | } | |
587 | ||
10659cba PNA |
588 | struct nft_set *nft_set_lookup_global(const struct net *net, |
589 | const struct nft_table *table, | |
590 | const struct nlattr *nla_set_name, | |
591 | const struct nlattr *nla_set_id, | |
592 | u8 genmask); | |
20a69341 | 593 | |
aaa31047 PNA |
594 | struct nft_set_ext *nft_set_catchall_lookup(const struct net *net, |
595 | const struct nft_set *set); | |
596 | void *nft_set_catchall_gc(const struct nft_set *set); | |
597 | ||
761da293 PM |
598 | static inline unsigned long nft_set_gc_interval(const struct nft_set *set) |
599 | { | |
123b9961 PNA |
600 | u32 gc_int = READ_ONCE(set->gc_int); |
601 | ||
602 | return gc_int ? msecs_to_jiffies(gc_int) : HZ; | |
761da293 PM |
603 | } |
604 | ||
20a69341 PM |
605 | /** |
606 | * struct nft_set_binding - nf_tables set binding | |
607 | * | |
608 | * @list: set bindings list node | |
609 | * @chain: chain containing the rule bound to the set | |
11113e19 | 610 | * @flags: set action flags |
20a69341 PM |
611 | * |
612 | * A set binding contains all information necessary for validation | |
613 | * of new elements added to a bound set. | |
614 | */ | |
615 | struct nft_set_binding { | |
616 | struct list_head list; | |
617 | const struct nft_chain *chain; | |
11113e19 | 618 | u32 flags; |
20a69341 PM |
619 | }; |
620 | ||
273fe3f1 PNA |
621 | enum nft_trans_phase; |
622 | void nf_tables_deactivate_set(const struct nft_ctx *ctx, struct nft_set *set, | |
623 | struct nft_set_binding *binding, | |
624 | enum nft_trans_phase phase); | |
5eccdfaa JP |
625 | int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set, |
626 | struct nft_set_binding *binding); | |
cd5125d8 | 627 | void nf_tables_destroy_set(const struct nft_ctx *ctx, struct nft_set *set); |
20a69341 | 628 | |
3ac4c07a PM |
629 | /** |
630 | * enum nft_set_extensions - set extension type IDs | |
631 | * | |
632 | * @NFT_SET_EXT_KEY: element key | |
7b225d0b | 633 | * @NFT_SET_EXT_KEY_END: upper bound element key, for ranges |
3ac4c07a PM |
634 | * @NFT_SET_EXT_DATA: mapping data |
635 | * @NFT_SET_EXT_FLAGS: element flags | |
c3e1b005 PM |
636 | * @NFT_SET_EXT_TIMEOUT: element timeout |
637 | * @NFT_SET_EXT_EXPIRATION: element expiration time | |
68e942e8 | 638 | * @NFT_SET_EXT_USERDATA: user data associated with the element |
563125a7 | 639 | * @NFT_SET_EXT_EXPRESSIONS: expressions assiciated with the element |
8aeff920 | 640 | * @NFT_SET_EXT_OBJREF: stateful object reference associated with element |
3ac4c07a PM |
641 | * @NFT_SET_EXT_NUM: number of extension types |
642 | */ | |
643 | enum nft_set_extensions { | |
644 | NFT_SET_EXT_KEY, | |
7b225d0b | 645 | NFT_SET_EXT_KEY_END, |
3ac4c07a PM |
646 | NFT_SET_EXT_DATA, |
647 | NFT_SET_EXT_FLAGS, | |
c3e1b005 PM |
648 | NFT_SET_EXT_TIMEOUT, |
649 | NFT_SET_EXT_EXPIRATION, | |
68e942e8 | 650 | NFT_SET_EXT_USERDATA, |
563125a7 | 651 | NFT_SET_EXT_EXPRESSIONS, |
8aeff920 | 652 | NFT_SET_EXT_OBJREF, |
3ac4c07a PM |
653 | NFT_SET_EXT_NUM |
654 | }; | |
655 | ||
656 | /** | |
657 | * struct nft_set_ext_type - set extension type | |
658 | * | |
659 | * @len: fixed part length of the extension | |
660 | * @align: alignment requirements of the extension | |
661 | */ | |
662 | struct nft_set_ext_type { | |
663 | u8 len; | |
664 | u8 align; | |
665 | }; | |
666 | ||
667 | extern const struct nft_set_ext_type nft_set_ext_types[]; | |
668 | ||
669 | /** | |
670 | * struct nft_set_ext_tmpl - set extension template | |
671 | * | |
672 | * @len: length of extension area | |
673 | * @offset: offsets of individual extension types | |
674 | */ | |
675 | struct nft_set_ext_tmpl { | |
676 | u16 len; | |
677 | u8 offset[NFT_SET_EXT_NUM]; | |
34aae2c2 | 678 | u8 ext_len[NFT_SET_EXT_NUM]; |
3ac4c07a PM |
679 | }; |
680 | ||
681 | /** | |
682 | * struct nft_set_ext - set extensions | |
683 | * | |
cc02e457 | 684 | * @genmask: generation mask |
3ac4c07a PM |
685 | * @offset: offsets of individual extension types |
686 | * @data: beginning of extension data | |
687 | */ | |
688 | struct nft_set_ext { | |
cc02e457 | 689 | u8 genmask; |
3ac4c07a | 690 | u8 offset[NFT_SET_EXT_NUM]; |
6daf1414 | 691 | char data[]; |
3ac4c07a PM |
692 | }; |
693 | ||
694 | static inline void nft_set_ext_prepare(struct nft_set_ext_tmpl *tmpl) | |
695 | { | |
696 | memset(tmpl, 0, sizeof(*tmpl)); | |
697 | tmpl->len = sizeof(struct nft_set_ext); | |
698 | } | |
699 | ||
c39ba4de PNA |
700 | static inline int nft_set_ext_add_length(struct nft_set_ext_tmpl *tmpl, u8 id, |
701 | unsigned int len) | |
3ac4c07a PM |
702 | { |
703 | tmpl->len = ALIGN(tmpl->len, nft_set_ext_types[id].align); | |
c39ba4de PNA |
704 | if (tmpl->len > U8_MAX) |
705 | return -EINVAL; | |
706 | ||
3ac4c07a | 707 | tmpl->offset[id] = tmpl->len; |
34aae2c2 PNA |
708 | tmpl->ext_len[id] = nft_set_ext_types[id].len + len; |
709 | tmpl->len += tmpl->ext_len[id]; | |
c39ba4de PNA |
710 | |
711 | return 0; | |
3ac4c07a PM |
712 | } |
713 | ||
c39ba4de | 714 | static inline int nft_set_ext_add(struct nft_set_ext_tmpl *tmpl, u8 id) |
3ac4c07a | 715 | { |
c39ba4de | 716 | return nft_set_ext_add_length(tmpl, id, 0); |
3ac4c07a PM |
717 | } |
718 | ||
719 | static inline void nft_set_ext_init(struct nft_set_ext *ext, | |
720 | const struct nft_set_ext_tmpl *tmpl) | |
721 | { | |
722 | memcpy(ext->offset, tmpl->offset, sizeof(ext->offset)); | |
723 | } | |
724 | ||
725 | static inline bool __nft_set_ext_exists(const struct nft_set_ext *ext, u8 id) | |
726 | { | |
727 | return !!ext->offset[id]; | |
728 | } | |
729 | ||
730 | static inline bool nft_set_ext_exists(const struct nft_set_ext *ext, u8 id) | |
731 | { | |
732 | return ext && __nft_set_ext_exists(ext, id); | |
733 | } | |
734 | ||
735 | static inline void *nft_set_ext(const struct nft_set_ext *ext, u8 id) | |
736 | { | |
737 | return (void *)ext + ext->offset[id]; | |
738 | } | |
739 | ||
740 | static inline struct nft_data *nft_set_ext_key(const struct nft_set_ext *ext) | |
741 | { | |
742 | return nft_set_ext(ext, NFT_SET_EXT_KEY); | |
743 | } | |
744 | ||
7b225d0b PNA |
745 | static inline struct nft_data *nft_set_ext_key_end(const struct nft_set_ext *ext) |
746 | { | |
747 | return nft_set_ext(ext, NFT_SET_EXT_KEY_END); | |
748 | } | |
749 | ||
3ac4c07a PM |
750 | static inline struct nft_data *nft_set_ext_data(const struct nft_set_ext *ext) |
751 | { | |
752 | return nft_set_ext(ext, NFT_SET_EXT_DATA); | |
753 | } | |
754 | ||
755 | static inline u8 *nft_set_ext_flags(const struct nft_set_ext *ext) | |
756 | { | |
757 | return nft_set_ext(ext, NFT_SET_EXT_FLAGS); | |
758 | } | |
ef1f7df9 | 759 | |
c3e1b005 PM |
760 | static inline u64 *nft_set_ext_timeout(const struct nft_set_ext *ext) |
761 | { | |
762 | return nft_set_ext(ext, NFT_SET_EXT_TIMEOUT); | |
763 | } | |
764 | ||
8e1102d5 | 765 | static inline u64 *nft_set_ext_expiration(const struct nft_set_ext *ext) |
c3e1b005 PM |
766 | { |
767 | return nft_set_ext(ext, NFT_SET_EXT_EXPIRATION); | |
768 | } | |
769 | ||
68e942e8 PM |
770 | static inline struct nft_userdata *nft_set_ext_userdata(const struct nft_set_ext *ext) |
771 | { | |
772 | return nft_set_ext(ext, NFT_SET_EXT_USERDATA); | |
773 | } | |
774 | ||
563125a7 | 775 | static inline struct nft_set_elem_expr *nft_set_ext_expr(const struct nft_set_ext *ext) |
f25ad2e9 | 776 | { |
563125a7 | 777 | return nft_set_ext(ext, NFT_SET_EXT_EXPRESSIONS); |
f25ad2e9 PM |
778 | } |
779 | ||
c3e1b005 PM |
780 | static inline bool nft_set_elem_expired(const struct nft_set_ext *ext) |
781 | { | |
782 | return nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION) && | |
8e1102d5 | 783 | time_is_before_eq_jiffies64(*nft_set_ext_expiration(ext)); |
c3e1b005 PM |
784 | } |
785 | ||
fe2811eb PM |
786 | static inline struct nft_set_ext *nft_set_elem_ext(const struct nft_set *set, |
787 | void *elem) | |
788 | { | |
789 | return elem + set->ops->elemsize; | |
790 | } | |
791 | ||
8aeff920 PNA |
792 | static inline struct nft_object **nft_set_ext_obj(const struct nft_set_ext *ext) |
793 | { | |
794 | return nft_set_ext(ext, NFT_SET_EXT_OBJREF); | |
795 | } | |
796 | ||
a7fc9368 PNA |
797 | struct nft_expr *nft_set_elem_expr_alloc(const struct nft_ctx *ctx, |
798 | const struct nft_set *set, | |
799 | const struct nlattr *attr); | |
800 | ||
22fe54d5 PM |
801 | void *nft_set_elem_init(const struct nft_set *set, |
802 | const struct nft_set_ext_tmpl *tmpl, | |
7b225d0b | 803 | const u32 *key, const u32 *key_end, const u32 *data, |
79ebb5bb | 804 | u64 timeout, u64 expiration, gfp_t gfp); |
fca05d4d PNA |
805 | int nft_set_elem_expr_clone(const struct nft_ctx *ctx, struct nft_set *set, |
806 | struct nft_expr *expr_array[]); | |
61f9e292 LZ |
807 | void nft_set_elem_destroy(const struct nft_set *set, void *elem, |
808 | bool destroy_expr); | |
61edafbb | 809 | |
cfed7e1b PM |
810 | /** |
811 | * struct nft_set_gc_batch_head - nf_tables set garbage collection batch | |
812 | * | |
813 | * @rcu: rcu head | |
814 | * @set: set the elements belong to | |
815 | * @cnt: count of elements | |
816 | */ | |
817 | struct nft_set_gc_batch_head { | |
818 | struct rcu_head rcu; | |
819 | const struct nft_set *set; | |
820 | unsigned int cnt; | |
821 | }; | |
822 | ||
823 | #define NFT_SET_GC_BATCH_SIZE ((PAGE_SIZE - \ | |
824 | sizeof(struct nft_set_gc_batch_head)) / \ | |
825 | sizeof(void *)) | |
826 | ||
827 | /** | |
828 | * struct nft_set_gc_batch - nf_tables set garbage collection batch | |
829 | * | |
830 | * @head: GC batch head | |
831 | * @elems: garbage collection elements | |
832 | */ | |
833 | struct nft_set_gc_batch { | |
834 | struct nft_set_gc_batch_head head; | |
835 | void *elems[NFT_SET_GC_BATCH_SIZE]; | |
836 | }; | |
837 | ||
838 | struct nft_set_gc_batch *nft_set_gc_batch_alloc(const struct nft_set *set, | |
839 | gfp_t gfp); | |
840 | void nft_set_gc_batch_release(struct rcu_head *rcu); | |
841 | ||
842 | static inline void nft_set_gc_batch_complete(struct nft_set_gc_batch *gcb) | |
843 | { | |
844 | if (gcb != NULL) | |
845 | call_rcu(&gcb->head.rcu, nft_set_gc_batch_release); | |
846 | } | |
847 | ||
848 | static inline struct nft_set_gc_batch * | |
849 | nft_set_gc_batch_check(const struct nft_set *set, struct nft_set_gc_batch *gcb, | |
850 | gfp_t gfp) | |
851 | { | |
852 | if (gcb != NULL) { | |
853 | if (gcb->head.cnt + 1 < ARRAY_SIZE(gcb->elems)) | |
854 | return gcb; | |
855 | nft_set_gc_batch_complete(gcb); | |
856 | } | |
857 | return nft_set_gc_batch_alloc(set, gfp); | |
858 | } | |
859 | ||
860 | static inline void nft_set_gc_batch_add(struct nft_set_gc_batch *gcb, | |
861 | void *elem) | |
862 | { | |
863 | gcb->elems[gcb->head.cnt++] = elem; | |
864 | } | |
865 | ||
b8e20400 | 866 | struct nft_expr_ops; |
96518518 | 867 | /** |
ef1f7df9 | 868 | * struct nft_expr_type - nf_tables expression type |
96518518 | 869 | * |
ef1f7df9 | 870 | * @select_ops: function to select nft_expr_ops |
b8e20400 | 871 | * @release_ops: release nft_expr_ops |
ef1f7df9 | 872 | * @ops: default ops, used when no select_ops functions is present |
96518518 PM |
873 | * @list: used internally |
874 | * @name: Identifier | |
875 | * @owner: module reference | |
876 | * @policy: netlink attribute policy | |
877 | * @maxattr: highest netlink attribute number | |
64d46806 | 878 | * @family: address family for AF-specific types |
151d799a | 879 | * @flags: expression type flags |
ef1f7df9 PM |
880 | */ |
881 | struct nft_expr_type { | |
0ca743a5 PNA |
882 | const struct nft_expr_ops *(*select_ops)(const struct nft_ctx *, |
883 | const struct nlattr * const tb[]); | |
b8e20400 | 884 | void (*release_ops)(const struct nft_expr_ops *ops); |
ef1f7df9 | 885 | const struct nft_expr_ops *ops; |
3a07327d | 886 | const struct nft_expr_ops *inner_ops; |
ef1f7df9 PM |
887 | struct list_head list; |
888 | const char *name; | |
889 | struct module *owner; | |
890 | const struct nla_policy *policy; | |
891 | unsigned int maxattr; | |
64d46806 | 892 | u8 family; |
151d799a | 893 | u8 flags; |
ef1f7df9 PM |
894 | }; |
895 | ||
151d799a | 896 | #define NFT_EXPR_STATEFUL 0x1 |
79b174ad | 897 | #define NFT_EXPR_GC 0x2 |
151d799a | 898 | |
f6ac8585 PNA |
899 | enum nft_trans_phase { |
900 | NFT_TRANS_PREPARE, | |
901 | NFT_TRANS_ABORT, | |
902 | NFT_TRANS_COMMIT, | |
903 | NFT_TRANS_RELEASE | |
904 | }; | |
905 | ||
c9626a2c PNA |
906 | struct nft_flow_rule; |
907 | struct nft_offload_ctx; | |
908 | ||
ef1f7df9 PM |
909 | /** |
910 | * struct nft_expr_ops - nf_tables expression operations | |
911 | * | |
912 | * @eval: Expression evaluation function | |
96518518 | 913 | * @size: full expression size, including private data size |
ef1f7df9 | 914 | * @init: initialization function |
cd5125d8 FW |
915 | * @activate: activate expression in the next generation |
916 | * @deactivate: deactivate expression in next generation | |
917 | * @destroy: destruction function, called after synchronize_rcu | |
ef1f7df9 PM |
918 | * @dump: function to dump parameters |
919 | * @type: expression type | |
0ca743a5 PNA |
920 | * @validate: validate expression, called during loop detection |
921 | * @data: extra data to attach to this expression operation | |
96518518 | 922 | */ |
96518518 PM |
923 | struct nft_expr_ops { |
924 | void (*eval)(const struct nft_expr *expr, | |
a55e22e9 | 925 | struct nft_regs *regs, |
96518518 | 926 | const struct nft_pktinfo *pkt); |
086f3321 PNA |
927 | int (*clone)(struct nft_expr *dst, |
928 | const struct nft_expr *src); | |
ef1f7df9 PM |
929 | unsigned int size; |
930 | ||
96518518 PM |
931 | int (*init)(const struct nft_ctx *ctx, |
932 | const struct nft_expr *expr, | |
933 | const struct nlattr * const tb[]); | |
bb7b40ae PNA |
934 | void (*activate)(const struct nft_ctx *ctx, |
935 | const struct nft_expr *expr); | |
936 | void (*deactivate)(const struct nft_ctx *ctx, | |
f6ac8585 PNA |
937 | const struct nft_expr *expr, |
938 | enum nft_trans_phase phase); | |
62472bce PM |
939 | void (*destroy)(const struct nft_ctx *ctx, |
940 | const struct nft_expr *expr); | |
371ebcbb PNA |
941 | void (*destroy_clone)(const struct nft_ctx *ctx, |
942 | const struct nft_expr *expr); | |
96518518 | 943 | int (*dump)(struct sk_buff *skb, |
7d34aa3e PS |
944 | const struct nft_expr *expr, |
945 | bool reset); | |
0ca743a5 PNA |
946 | int (*validate)(const struct nft_ctx *ctx, |
947 | const struct nft_expr *expr, | |
948 | const struct nft_data **data); | |
12e4ecfa PNA |
949 | bool (*reduce)(struct nft_regs_track *track, |
950 | const struct nft_expr *expr); | |
79b174ad PNA |
951 | bool (*gc)(struct net *net, |
952 | const struct nft_expr *expr); | |
c9626a2c PNA |
953 | int (*offload)(struct nft_offload_ctx *ctx, |
954 | struct nft_flow_rule *flow, | |
955 | const struct nft_expr *expr); | |
b1a5983f | 956 | bool (*offload_action)(const struct nft_expr *expr); |
b72920f6 PNA |
957 | void (*offload_stats)(struct nft_expr *expr, |
958 | const struct flow_stats *stats); | |
ef1f7df9 | 959 | const struct nft_expr_type *type; |
0ca743a5 | 960 | void *data; |
96518518 PM |
961 | }; |
962 | ||
96518518 PM |
963 | /** |
964 | * struct nft_rule - nf_tables rule | |
965 | * | |
966 | * @list: used internally | |
96518518 | 967 | * @handle: rule handle |
0628b123 | 968 | * @genmask: generation mask |
96518518 | 969 | * @dlen: length of expression data |
86f1ec32 | 970 | * @udata: user data is appended to the rule |
96518518 PM |
971 | * @data: expression data |
972 | */ | |
973 | struct nft_rule { | |
974 | struct list_head list; | |
0768b3b3 | 975 | u64 handle:42, |
0628b123 | 976 | genmask:2, |
0768b3b3 | 977 | dlen:12, |
86f1ec32 | 978 | udata:1; |
96518518 PM |
979 | unsigned char data[] |
980 | __attribute__((aligned(__alignof__(struct nft_expr)))); | |
981 | }; | |
982 | ||
983 | static inline struct nft_expr *nft_expr_first(const struct nft_rule *rule) | |
984 | { | |
985 | return (struct nft_expr *)&rule->data[0]; | |
986 | } | |
987 | ||
988 | static inline struct nft_expr *nft_expr_next(const struct nft_expr *expr) | |
989 | { | |
990 | return ((void *)expr) + expr->ops->size; | |
991 | } | |
992 | ||
993 | static inline struct nft_expr *nft_expr_last(const struct nft_rule *rule) | |
994 | { | |
995 | return (struct nft_expr *)&rule->data[rule->dlen]; | |
996 | } | |
997 | ||
31cc578a SM |
998 | static inline bool nft_expr_more(const struct nft_rule *rule, |
999 | const struct nft_expr *expr) | |
1000 | { | |
1001 | return expr != nft_expr_last(rule) && expr->ops; | |
1002 | } | |
1003 | ||
86f1ec32 | 1004 | static inline struct nft_userdata *nft_userdata(const struct nft_rule *rule) |
0768b3b3 PNA |
1005 | { |
1006 | return (void *)&rule->data[rule->dlen]; | |
1007 | } | |
1008 | ||
d0e2c7de PNA |
1009 | void nf_tables_rule_release(const struct nft_ctx *ctx, struct nft_rule *rule); |
1010 | ||
76adfafe PNA |
1011 | static inline void nft_set_elem_update_expr(const struct nft_set_ext *ext, |
1012 | struct nft_regs *regs, | |
1013 | const struct nft_pktinfo *pkt) | |
1014 | { | |
563125a7 | 1015 | struct nft_set_elem_expr *elem_expr; |
76adfafe | 1016 | struct nft_expr *expr; |
563125a7 PNA |
1017 | u32 size; |
1018 | ||
1019 | if (__nft_set_ext_exists(ext, NFT_SET_EXT_EXPRESSIONS)) { | |
1020 | elem_expr = nft_set_ext_expr(ext); | |
1021 | nft_setelem_expr_foreach(expr, elem_expr, size) { | |
1022 | expr->ops->eval(expr, regs, pkt); | |
1023 | if (regs->verdict.code == NFT_BREAK) | |
1024 | return; | |
1025 | } | |
76adfafe PNA |
1026 | } |
1027 | } | |
1028 | ||
96518518 PM |
1029 | /* |
1030 | * The last pointer isn't really necessary, but the compiler isn't able to | |
1031 | * determine that the result of nft_expr_last() is always the same since it | |
1032 | * can't assume that the dlen value wasn't changed within calls in the loop. | |
1033 | */ | |
1034 | #define nft_rule_for_each_expr(expr, last, rule) \ | |
1035 | for ((expr) = nft_expr_first(rule), (last) = nft_expr_last(rule); \ | |
1036 | (expr) != (last); \ | |
1037 | (expr) = nft_expr_next(expr)) | |
1038 | ||
ad652f38 PNA |
1039 | #define NFT_CHAIN_POLICY_UNSET U8_MAX |
1040 | ||
2c865a8a PNA |
1041 | struct nft_rule_dp { |
1042 | u64 is_last:1, | |
1043 | dlen:12, | |
1044 | handle:42; /* for tracing */ | |
1045 | unsigned char data[] | |
1046 | __attribute__((aligned(__alignof__(struct nft_expr)))); | |
1047 | }; | |
1048 | ||
63e9bbbc FW |
1049 | struct nft_rule_dp_last { |
1050 | struct nft_rule_dp end; /* end of nft_rule_blob marker */ | |
1051 | struct rcu_head h; /* call_rcu head */ | |
1052 | struct nft_rule_blob *blob; /* ptr to free via call_rcu */ | |
1053 | const struct nft_chain *chain; /* for nftables tracing */ | |
1054 | }; | |
1055 | ||
1056 | static inline const struct nft_rule_dp *nft_rule_next(const struct nft_rule_dp *rule) | |
1057 | { | |
1058 | return (void *)rule + sizeof(*rule) + rule->dlen; | |
1059 | } | |
1060 | ||
2c865a8a PNA |
1061 | struct nft_rule_blob { |
1062 | unsigned long size; | |
1063 | unsigned char data[] | |
1064 | __attribute__((aligned(__alignof__(struct nft_rule_dp)))); | |
1065 | }; | |
1066 | ||
96518518 PM |
1067 | /** |
1068 | * struct nft_chain - nf_tables chain | |
1069 | * | |
1070 | * @rules: list of rules in the chain | |
1071 | * @list: used internally | |
1b2470e5 | 1072 | * @rhlhead: used internally |
b5bc89bf | 1073 | * @table: table that this chain belongs to |
96518518 | 1074 | * @handle: chain handle |
96518518 | 1075 | * @use: number of jump references to this chain |
a0a7379e | 1076 | * @flags: bitmask of enum nft_chain_flags |
96518518 PM |
1077 | * @name: name of the chain |
1078 | */ | |
1079 | struct nft_chain { | |
2c865a8a PNA |
1080 | struct nft_rule_blob __rcu *blob_gen_0; |
1081 | struct nft_rule_blob __rcu *blob_gen_1; | |
96518518 PM |
1082 | struct list_head rules; |
1083 | struct list_head list; | |
1b2470e5 | 1084 | struct rhlist_head rhlhead; |
b5bc89bf | 1085 | struct nft_table *table; |
96518518 | 1086 | u64 handle; |
a0a7379e | 1087 | u32 use; |
d0e2c7de PNA |
1088 | u8 flags:5, |
1089 | bound:1, | |
664b0f8c | 1090 | genmask:2; |
b7263e07 | 1091 | char *name; |
002f2176 JGG |
1092 | u16 udlen; |
1093 | u8 *udata; | |
0cbc06b3 FW |
1094 | |
1095 | /* Only used during control plane commit phase: */ | |
2c865a8a | 1096 | struct nft_rule_blob *blob_next; |
96518518 PM |
1097 | }; |
1098 | ||
a654de8f | 1099 | int nft_chain_validate(const struct nft_ctx *ctx, const struct nft_chain *chain); |
d46fc894 PNA |
1100 | int nft_setelem_validate(const struct nft_ctx *ctx, struct nft_set *set, |
1101 | const struct nft_set_iter *iter, | |
1102 | struct nft_set_elem *elem); | |
1103 | int nft_set_catchall_validate(const struct nft_ctx *ctx, struct nft_set *set); | |
a654de8f | 1104 | |
32537e91 | 1105 | enum nft_chain_types { |
9370761c PNA |
1106 | NFT_CHAIN_T_DEFAULT = 0, |
1107 | NFT_CHAIN_T_ROUTE, | |
1108 | NFT_CHAIN_T_NAT, | |
1109 | NFT_CHAIN_T_MAX | |
1110 | }; | |
1111 | ||
1a1e1a12 | 1112 | /** |
32537e91 | 1113 | * struct nft_chain_type - nf_tables chain type info |
1a1e1a12 PM |
1114 | * |
1115 | * @name: name of the type | |
1116 | * @type: numeric identifier | |
1117 | * @family: address family | |
1118 | * @owner: module owner | |
1119 | * @hook_mask: mask of valid hooks | |
c2f9eafe | 1120 | * @hooks: array of hook functions |
4e25ceb8 FW |
1121 | * @ops_register: base chain register function |
1122 | * @ops_unregister: base chain unregister function | |
1a1e1a12 | 1123 | */ |
32537e91 | 1124 | struct nft_chain_type { |
1a1e1a12 | 1125 | const char *name; |
32537e91 | 1126 | enum nft_chain_types type; |
1a1e1a12 PM |
1127 | int family; |
1128 | struct module *owner; | |
1129 | unsigned int hook_mask; | |
d25e2e93 | 1130 | nf_hookfn *hooks[NFT_MAX_HOOKS]; |
4e25ceb8 FW |
1131 | int (*ops_register)(struct net *net, const struct nf_hook_ops *ops); |
1132 | void (*ops_unregister)(struct net *net, const struct nf_hook_ops *ops); | |
1a1e1a12 PM |
1133 | }; |
1134 | ||
7210e4e3 | 1135 | int nft_chain_validate_dependency(const struct nft_chain *chain, |
32537e91 | 1136 | enum nft_chain_types type); |
75e8d06d PNA |
1137 | int nft_chain_validate_hooks(const struct nft_chain *chain, |
1138 | unsigned int hook_flags); | |
7210e4e3 | 1139 | |
d0e2c7de PNA |
1140 | static inline bool nft_chain_is_bound(struct nft_chain *chain) |
1141 | { | |
1142 | return (chain->flags & NFT_CHAIN_BINDING) && chain->bound; | |
1143 | } | |
1144 | ||
1145 | void nft_chain_del(struct nft_chain *chain); | |
1146 | void nf_tables_chain_destroy(struct nft_ctx *ctx); | |
1147 | ||
0ca743a5 | 1148 | struct nft_stats { |
ce355e20 ED |
1149 | u64 bytes; |
1150 | u64 pkts; | |
1151 | struct u64_stats_sync syncp; | |
0ca743a5 PNA |
1152 | }; |
1153 | ||
3f0465a9 PNA |
1154 | struct nft_hook { |
1155 | struct list_head list; | |
1156 | struct nf_hook_ops ops; | |
1157 | struct rcu_head rcu; | |
1158 | }; | |
1159 | ||
96518518 PM |
1160 | /** |
1161 | * struct nft_base_chain - nf_tables base chain | |
1162 | * | |
1163 | * @ops: netfilter hook ops | |
d54725cd | 1164 | * @hook_list: list of netfilter hooks (for NFPROTO_NETDEV family) |
9370761c | 1165 | * @type: chain type |
0ca743a5 PNA |
1166 | * @policy: default policy |
1167 | * @stats: per-cpu chain stats | |
96518518 | 1168 | * @chain: the chain |
14bfb13f | 1169 | * @flow_block: flow block (for hardware offload) |
96518518 PM |
1170 | */ |
1171 | struct nft_base_chain { | |
c974a3a3 | 1172 | struct nf_hook_ops ops; |
d54725cd | 1173 | struct list_head hook_list; |
32537e91 | 1174 | const struct nft_chain_type *type; |
0ca743a5 | 1175 | u8 policy; |
835b8033 | 1176 | u8 flags; |
0ca743a5 | 1177 | struct nft_stats __percpu *stats; |
96518518 | 1178 | struct nft_chain chain; |
14bfb13f | 1179 | struct flow_block flow_block; |
96518518 PM |
1180 | }; |
1181 | ||
1182 | static inline struct nft_base_chain *nft_base_chain(const struct nft_chain *chain) | |
1183 | { | |
1184 | return container_of(chain, struct nft_base_chain, chain); | |
1185 | } | |
1186 | ||
f323d954 PNA |
1187 | static inline bool nft_is_base_chain(const struct nft_chain *chain) |
1188 | { | |
67c49de4 | 1189 | return chain->flags & NFT_CHAIN_BASE; |
f323d954 PNA |
1190 | } |
1191 | ||
5ebe0b0e | 1192 | int __nft_release_basechain(struct nft_ctx *ctx); |
835b8033 | 1193 | |
06198b34 | 1194 | unsigned int nft_do_chain(struct nft_pktinfo *pkt, void *priv); |
96518518 | 1195 | |
96518518 PM |
1196 | /** |
1197 | * struct nft_table - nf_tables table | |
1198 | * | |
1199 | * @list: used internally | |
1b2470e5 FW |
1200 | * @chains_ht: chains in the table |
1201 | * @chains: same, for stable walks | |
96518518 | 1202 | * @sets: sets in the table |
e5009240 | 1203 | * @objects: stateful objects in the table |
3b49e2e9 | 1204 | * @flowtables: flow tables in the table |
96518518 | 1205 | * @hgenerator: handle generator state |
3ecbfd65 | 1206 | * @handle: table handle |
96518518 PM |
1207 | * @use: number of chain references to this table |
1208 | * @flags: table flag (see enum nft_table_flags) | |
f2a6d766 | 1209 | * @genmask: generation mask |
36596dad | 1210 | * @afinfo: address family info |
96518518 | 1211 | * @name: name of the table |
00c320f9 | 1212 | * @validate_state: internal, set when transaction adds jumps |
96518518 PM |
1213 | */ |
1214 | struct nft_table { | |
1215 | struct list_head list; | |
1b2470e5 | 1216 | struct rhltable chains_ht; |
96518518 PM |
1217 | struct list_head chains; |
1218 | struct list_head sets; | |
e5009240 | 1219 | struct list_head objects; |
3b49e2e9 | 1220 | struct list_head flowtables; |
96518518 | 1221 | u64 hgenerator; |
3ecbfd65 | 1222 | u64 handle; |
96518518 | 1223 | u32 use; |
98319cb9 PNA |
1224 | u16 family:6, |
1225 | flags:8, | |
f2a6d766 | 1226 | genmask:2; |
6001a930 | 1227 | u32 nlpid; |
e46abbcc | 1228 | char *name; |
7a81575b JGG |
1229 | u16 udlen; |
1230 | u8 *udata; | |
00c320f9 | 1231 | u8 validate_state; |
ebddf1a8 PNA |
1232 | }; |
1233 | ||
6001a930 PNA |
1234 | static inline bool nft_table_has_owner(const struct nft_table *table) |
1235 | { | |
1236 | return table->flags & NFT_TABLE_F_OWNER; | |
1237 | } | |
1238 | ||
d3519cb8 PNA |
1239 | static inline bool nft_base_chain_netdev(int family, u32 hooknum) |
1240 | { | |
1241 | return family == NFPROTO_NETDEV || | |
1242 | (family == NFPROTO_INET && hooknum == NF_INET_INGRESS); | |
1243 | } | |
1244 | ||
cc07eeb0 | 1245 | void nft_register_chain_type(const struct nft_chain_type *); |
32537e91 | 1246 | void nft_unregister_chain_type(const struct nft_chain_type *); |
96518518 | 1247 | |
5eccdfaa JP |
1248 | int nft_register_expr(struct nft_expr_type *); |
1249 | void nft_unregister_expr(struct nft_expr_type *); | |
96518518 | 1250 | |
33d5a7b1 FW |
1251 | int nft_verdict_dump(struct sk_buff *skb, int type, |
1252 | const struct nft_verdict *v); | |
1253 | ||
d152159b FW |
1254 | /** |
1255 | * struct nft_object_hash_key - key to lookup nft_object | |
1256 | * | |
1257 | * @name: name of the stateful object to look up | |
1258 | * @table: table the object belongs to | |
1259 | */ | |
1260 | struct nft_object_hash_key { | |
1261 | const char *name; | |
1262 | const struct nft_table *table; | |
1263 | }; | |
1264 | ||
e5009240 PNA |
1265 | /** |
1266 | * struct nft_object - nf_tables stateful object | |
1267 | * | |
1268 | * @list: table stateful object list node | |
d152159b | 1269 | * @key: keys that identify this object |
4d44175a | 1270 | * @rhlhead: nft_objname_ht node |
e5009240 PNA |
1271 | * @genmask: generation mask |
1272 | * @use: number of references to this stateful object | |
3ecbfd65 | 1273 | * @handle: unique object handle |
dfc46034 | 1274 | * @ops: object operations |
4d44175a | 1275 | * @data: object data, layout depends on type |
e5009240 PNA |
1276 | */ |
1277 | struct nft_object { | |
1278 | struct list_head list; | |
4d44175a | 1279 | struct rhlist_head rhlhead; |
d152159b | 1280 | struct nft_object_hash_key key; |
e5009240 PNA |
1281 | u32 genmask:2, |
1282 | use:30; | |
3ecbfd65 | 1283 | u64 handle; |
b131c964 JGG |
1284 | u16 udlen; |
1285 | u8 *udata; | |
e5009240 | 1286 | /* runtime data below here */ |
dfc46034 | 1287 | const struct nft_object_ops *ops ____cacheline_aligned; |
e5009240 PNA |
1288 | unsigned char data[] |
1289 | __attribute__((aligned(__alignof__(u64)))); | |
1290 | }; | |
1291 | ||
1292 | static inline void *nft_obj_data(const struct nft_object *obj) | |
1293 | { | |
1294 | return (void *)obj->data; | |
1295 | } | |
1296 | ||
1297 | #define nft_expr_obj(expr) *((struct nft_object **)nft_expr_priv(expr)) | |
1298 | ||
4d44175a FW |
1299 | struct nft_object *nft_obj_lookup(const struct net *net, |
1300 | const struct nft_table *table, | |
cac20fcd PNA |
1301 | const struct nlattr *nla, u32 objtype, |
1302 | u8 genmask); | |
e5009240 | 1303 | |
d152159b | 1304 | void nft_obj_notify(struct net *net, const struct nft_table *table, |
25e94a99 | 1305 | struct nft_object *obj, u32 portid, u32 seq, |
6fb721cf | 1306 | int event, u16 flags, int family, int report, gfp_t gfp); |
2599e989 | 1307 | |
e5009240 PNA |
1308 | /** |
1309 | * struct nft_object_type - stateful object type | |
1310 | * | |
dfc46034 PBG |
1311 | * @select_ops: function to select nft_object_ops |
1312 | * @ops: default ops, used when no select_ops functions is present | |
e5009240 PNA |
1313 | * @list: list node in list of object types |
1314 | * @type: stateful object numeric type | |
e5009240 PNA |
1315 | * @owner: module owner |
1316 | * @maxattr: maximum netlink attribute | |
1317 | * @policy: netlink attribute policy | |
dfc46034 PBG |
1318 | */ |
1319 | struct nft_object_type { | |
1320 | const struct nft_object_ops *(*select_ops)(const struct nft_ctx *, | |
1321 | const struct nlattr * const tb[]); | |
1322 | const struct nft_object_ops *ops; | |
1323 | struct list_head list; | |
1324 | u32 type; | |
1325 | unsigned int maxattr; | |
1326 | struct module *owner; | |
1327 | const struct nla_policy *policy; | |
1328 | }; | |
1329 | ||
1330 | /** | |
1331 | * struct nft_object_ops - stateful object operations | |
1332 | * | |
1333 | * @eval: stateful object evaluation function | |
1334 | * @size: stateful object size | |
e5009240 PNA |
1335 | * @init: initialize object from netlink attributes |
1336 | * @destroy: release existing stateful object | |
1337 | * @dump: netlink dump stateful object | |
d62d0ba9 | 1338 | * @update: update stateful object |
e5009240 | 1339 | */ |
dfc46034 | 1340 | struct nft_object_ops { |
e5009240 PNA |
1341 | void (*eval)(struct nft_object *obj, |
1342 | struct nft_regs *regs, | |
1343 | const struct nft_pktinfo *pkt); | |
e5009240 | 1344 | unsigned int size; |
84fba055 FW |
1345 | int (*init)(const struct nft_ctx *ctx, |
1346 | const struct nlattr *const tb[], | |
e5009240 | 1347 | struct nft_object *obj); |
00bfb320 PNA |
1348 | void (*destroy)(const struct nft_ctx *ctx, |
1349 | struct nft_object *obj); | |
e5009240 | 1350 | int (*dump)(struct sk_buff *skb, |
43da04a5 PNA |
1351 | struct nft_object *obj, |
1352 | bool reset); | |
d62d0ba9 FFM |
1353 | void (*update)(struct nft_object *obj, |
1354 | struct nft_object *newobj); | |
dfc46034 | 1355 | const struct nft_object_type *type; |
e5009240 PNA |
1356 | }; |
1357 | ||
1358 | int nft_register_obj(struct nft_object_type *obj_type); | |
1359 | void nft_unregister_obj(struct nft_object_type *obj_type); | |
1360 | ||
cb662ac6 | 1361 | #define NFT_NETDEVICE_MAX 256 |
d92191aa | 1362 | |
3b49e2e9 PNA |
1363 | /** |
1364 | * struct nft_flowtable - nf_tables flow table | |
1365 | * | |
1366 | * @list: flow table list node in table list | |
1367 | * @table: the table the flow table is contained in | |
1368 | * @name: name of this flow table | |
1369 | * @hooknum: hook number | |
3b49e2e9 PNA |
1370 | * @ops_len: number of hooks in array |
1371 | * @genmask: generation mask | |
1372 | * @use: number of references to this flow table | |
3ecbfd65 | 1373 | * @handle: unique object handle |
d92191aa | 1374 | * @dev_name: array of device names |
3b49e2e9 PNA |
1375 | * @data: rhashtable and garbage collector |
1376 | * @ops: array of hooks | |
1377 | */ | |
1378 | struct nft_flowtable { | |
1379 | struct list_head list; | |
1380 | struct nft_table *table; | |
1381 | char *name; | |
1382 | int hooknum; | |
3b49e2e9 PNA |
1383 | int ops_len; |
1384 | u32 genmask:2, | |
1385 | use:30; | |
3ecbfd65 | 1386 | u64 handle; |
3b49e2e9 | 1387 | /* runtime data below here */ |
3f0465a9 | 1388 | struct list_head hook_list ____cacheline_aligned; |
3b49e2e9 PNA |
1389 | struct nf_flowtable data; |
1390 | }; | |
1391 | ||
cac20fcd PNA |
1392 | struct nft_flowtable *nft_flowtable_lookup(const struct nft_table *table, |
1393 | const struct nlattr *nla, | |
1394 | u8 genmask); | |
3b49e2e9 | 1395 | |
9b05b6e1 LGL |
1396 | void nf_tables_deactivate_flowtable(const struct nft_ctx *ctx, |
1397 | struct nft_flowtable *flowtable, | |
1398 | enum nft_trans_phase phase); | |
1399 | ||
3b49e2e9 PNA |
1400 | void nft_register_flowtable_type(struct nf_flowtable_type *type); |
1401 | void nft_unregister_flowtable_type(struct nf_flowtable_type *type); | |
1402 | ||
33d5a7b1 FW |
1403 | /** |
1404 | * struct nft_traceinfo - nft tracing information and state | |
1405 | * | |
e34b9ed9 FW |
1406 | * @trace: other struct members are initialised |
1407 | * @nf_trace: copy of skb->nf_trace before rule evaluation | |
1408 | * @type: event type (enum nft_trace_types) | |
1409 | * @skbid: hash of skb to be used as trace id | |
1410 | * @packet_dumped: packet headers sent in a previous traceinfo message | |
33d5a7b1 | 1411 | * @basechain: base chain currently processed |
33d5a7b1 FW |
1412 | */ |
1413 | struct nft_traceinfo { | |
e34b9ed9 FW |
1414 | bool trace; |
1415 | bool nf_trace; | |
1416 | bool packet_dumped; | |
1417 | enum nft_trace_types type:8; | |
1418 | u32 skbid; | |
33d5a7b1 | 1419 | const struct nft_base_chain *basechain; |
33d5a7b1 FW |
1420 | }; |
1421 | ||
1422 | void nft_trace_init(struct nft_traceinfo *info, const struct nft_pktinfo *pkt, | |
33d5a7b1 FW |
1423 | const struct nft_chain *basechain); |
1424 | ||
698bb828 | 1425 | void nft_trace_notify(const struct nft_pktinfo *pkt, |
0a202145 | 1426 | const struct nft_verdict *verdict, |
46df4175 | 1427 | const struct nft_rule_dp *rule, |
698bb828 | 1428 | struct nft_traceinfo *info); |
33d5a7b1 | 1429 | |
9370761c PNA |
1430 | #define MODULE_ALIAS_NFT_CHAIN(family, name) \ |
1431 | MODULE_ALIAS("nft-chain-" __stringify(family) "-" name) | |
96518518 | 1432 | |
64d46806 PM |
1433 | #define MODULE_ALIAS_NFT_AF_EXPR(family, name) \ |
1434 | MODULE_ALIAS("nft-expr-" __stringify(family) "-" name) | |
1435 | ||
96518518 PM |
1436 | #define MODULE_ALIAS_NFT_EXPR(name) \ |
1437 | MODULE_ALIAS("nft-expr-" name) | |
1438 | ||
e5009240 PNA |
1439 | #define MODULE_ALIAS_NFT_OBJ(type) \ |
1440 | MODULE_ALIAS("nft-obj-" __stringify(type)) | |
1441 | ||
47e640af JS |
1442 | #if IS_ENABLED(CONFIG_NF_TABLES) |
1443 | ||
ea4bd995 PM |
1444 | /* |
1445 | * The gencursor defines two generations, the currently active and the | |
1446 | * next one. Objects contain a bitmask of 2 bits specifying the generations | |
1447 | * they're active in. A set bit means they're inactive in the generation | |
1448 | * represented by that bit. | |
1449 | * | |
1450 | * New objects start out as inactive in the current and active in the | |
1451 | * next generation. When committing the ruleset the bitmask is cleared, | |
1452 | * meaning they're active in all generations. When removing an object, | |
1453 | * it is set inactive in the next generation. After committing the ruleset, | |
1454 | * the objects are removed. | |
1455 | */ | |
1456 | static inline unsigned int nft_gencursor_next(const struct net *net) | |
1457 | { | |
1458 | return net->nft.gencursor + 1 == 1 ? 1 : 0; | |
1459 | } | |
1460 | ||
1461 | static inline u8 nft_genmask_next(const struct net *net) | |
1462 | { | |
1463 | return 1 << nft_gencursor_next(net); | |
1464 | } | |
1465 | ||
1466 | static inline u8 nft_genmask_cur(const struct net *net) | |
1467 | { | |
14cd5d4a MR |
1468 | /* Use READ_ONCE() to prevent refetching the value for atomicity */ |
1469 | return 1 << READ_ONCE(net->nft.gencursor); | |
ea4bd995 PM |
1470 | } |
1471 | ||
22fe54d5 PM |
1472 | #define NFT_GENMASK_ANY ((1 << 0) | (1 << 1)) |
1473 | ||
889f7ee7 PNA |
1474 | /* |
1475 | * Generic transaction helpers | |
1476 | */ | |
1477 | ||
1478 | /* Check if this object is currently active. */ | |
1479 | #define nft_is_active(__net, __obj) \ | |
1480 | (((__obj)->genmask & nft_genmask_cur(__net)) == 0) | |
1481 | ||
1482 | /* Check if this object is active in the next generation. */ | |
1483 | #define nft_is_active_next(__net, __obj) \ | |
1484 | (((__obj)->genmask & nft_genmask_next(__net)) == 0) | |
1485 | ||
1486 | /* This object becomes active in the next generation. */ | |
1487 | #define nft_activate_next(__net, __obj) \ | |
1488 | (__obj)->genmask = nft_genmask_cur(__net) | |
1489 | ||
1490 | /* This object becomes inactive in the next generation. */ | |
1491 | #define nft_deactivate_next(__net, __obj) \ | |
1492 | (__obj)->genmask = nft_genmask_next(__net) | |
1493 | ||
1494 | /* After committing the ruleset, clear the stale generation bit. */ | |
1495 | #define nft_clear(__net, __obj) \ | |
1496 | (__obj)->genmask &= ~nft_genmask_next(__net) | |
f2a6d766 PNA |
1497 | #define nft_active_genmask(__obj, __genmask) \ |
1498 | !((__obj)->genmask & __genmask) | |
889f7ee7 | 1499 | |
cc02e457 PM |
1500 | /* |
1501 | * Set element transaction helpers | |
1502 | */ | |
1503 | ||
1504 | static inline bool nft_set_elem_active(const struct nft_set_ext *ext, | |
1505 | u8 genmask) | |
1506 | { | |
1507 | return !(ext->genmask & genmask); | |
1508 | } | |
1509 | ||
42a55769 PNA |
1510 | static inline void nft_set_elem_change_active(const struct net *net, |
1511 | const struct nft_set *set, | |
cc02e457 PM |
1512 | struct nft_set_ext *ext) |
1513 | { | |
42a55769 | 1514 | ext->genmask ^= nft_genmask_next(net); |
cc02e457 PM |
1515 | } |
1516 | ||
47e640af JS |
1517 | #endif /* IS_ENABLED(CONFIG_NF_TABLES) */ |
1518 | ||
69086658 PM |
1519 | /* |
1520 | * We use a free bit in the genmask field to indicate the element | |
1521 | * is busy, meaning it is currently being processed either by | |
1522 | * the netlink API or GC. | |
1523 | * | |
1524 | * Even though the genmask is only a single byte wide, this works | |
1525 | * because the extension structure if fully constant once initialized, | |
1526 | * so there are no non-atomic write accesses unless it is already | |
1527 | * marked busy. | |
1528 | */ | |
1529 | #define NFT_SET_ELEM_BUSY_MASK (1 << 2) | |
1530 | ||
1531 | #if defined(__LITTLE_ENDIAN_BITFIELD) | |
1532 | #define NFT_SET_ELEM_BUSY_BIT 2 | |
1533 | #elif defined(__BIG_ENDIAN_BITFIELD) | |
1534 | #define NFT_SET_ELEM_BUSY_BIT (BITS_PER_LONG - BITS_PER_BYTE + 2) | |
1535 | #else | |
1536 | #error | |
1537 | #endif | |
1538 | ||
1539 | static inline int nft_set_elem_mark_busy(struct nft_set_ext *ext) | |
1540 | { | |
1541 | unsigned long *word = (unsigned long *)ext; | |
1542 | ||
1543 | BUILD_BUG_ON(offsetof(struct nft_set_ext, genmask) != 0); | |
1544 | return test_and_set_bit(NFT_SET_ELEM_BUSY_BIT, word); | |
1545 | } | |
1546 | ||
1547 | static inline void nft_set_elem_clear_busy(struct nft_set_ext *ext) | |
1548 | { | |
1549 | unsigned long *word = (unsigned long *)ext; | |
1550 | ||
1551 | clear_bit(NFT_SET_ELEM_BUSY_BIT, word); | |
1552 | } | |
1553 | ||
1a1e1a12 PM |
1554 | /** |
1555 | * struct nft_trans - nf_tables object update in transaction | |
1556 | * | |
1557 | * @list: used internally | |
1558 | * @msg_type: message type | |
0935d558 | 1559 | * @put_net: ctx->net needs to be put |
1a1e1a12 PM |
1560 | * @ctx: transaction context |
1561 | * @data: internal information related to the transaction | |
1562 | */ | |
1563 | struct nft_trans { | |
1564 | struct list_head list; | |
1565 | int msg_type; | |
0935d558 | 1566 | bool put_net; |
1a1e1a12 | 1567 | struct nft_ctx ctx; |
6daf1414 | 1568 | char data[]; |
1a1e1a12 PM |
1569 | }; |
1570 | ||
1571 | struct nft_trans_rule { | |
1572 | struct nft_rule *rule; | |
c9626a2c | 1573 | struct nft_flow_rule *flow; |
1a94e38d | 1574 | u32 rule_id; |
1a1e1a12 PM |
1575 | }; |
1576 | ||
1577 | #define nft_trans_rule(trans) \ | |
1578 | (((struct nft_trans_rule *)trans->data)->rule) | |
c9626a2c PNA |
1579 | #define nft_trans_flow_rule(trans) \ |
1580 | (((struct nft_trans_rule *)trans->data)->flow) | |
1a94e38d PNA |
1581 | #define nft_trans_rule_id(trans) \ |
1582 | (((struct nft_trans_rule *)trans->data)->rule_id) | |
1a1e1a12 PM |
1583 | |
1584 | struct nft_trans_set { | |
1585 | struct nft_set *set; | |
1586 | u32 set_id; | |
123b9961 PNA |
1587 | u32 gc_int; |
1588 | u64 timeout; | |
1589 | bool update; | |
6a0a8d10 | 1590 | bool bound; |
1a1e1a12 PM |
1591 | }; |
1592 | ||
1593 | #define nft_trans_set(trans) \ | |
1594 | (((struct nft_trans_set *)trans->data)->set) | |
1595 | #define nft_trans_set_id(trans) \ | |
1596 | (((struct nft_trans_set *)trans->data)->set_id) | |
6a0a8d10 PNA |
1597 | #define nft_trans_set_bound(trans) \ |
1598 | (((struct nft_trans_set *)trans->data)->bound) | |
123b9961 PNA |
1599 | #define nft_trans_set_update(trans) \ |
1600 | (((struct nft_trans_set *)trans->data)->update) | |
1601 | #define nft_trans_set_timeout(trans) \ | |
1602 | (((struct nft_trans_set *)trans->data)->timeout) | |
1603 | #define nft_trans_set_gc_int(trans) \ | |
1604 | (((struct nft_trans_set *)trans->data)->gc_int) | |
1a1e1a12 PM |
1605 | |
1606 | struct nft_trans_chain { | |
1607 | bool update; | |
b7263e07 | 1608 | char *name; |
1a1e1a12 PM |
1609 | struct nft_stats __percpu *stats; |
1610 | u8 policy; | |
74cccc3d | 1611 | u32 chain_id; |
b9703ed4 PNA |
1612 | struct nft_base_chain *basechain; |
1613 | struct list_head hook_list; | |
1a1e1a12 PM |
1614 | }; |
1615 | ||
1616 | #define nft_trans_chain_update(trans) \ | |
1617 | (((struct nft_trans_chain *)trans->data)->update) | |
1618 | #define nft_trans_chain_name(trans) \ | |
1619 | (((struct nft_trans_chain *)trans->data)->name) | |
1620 | #define nft_trans_chain_stats(trans) \ | |
1621 | (((struct nft_trans_chain *)trans->data)->stats) | |
1622 | #define nft_trans_chain_policy(trans) \ | |
1623 | (((struct nft_trans_chain *)trans->data)->policy) | |
74cccc3d PNA |
1624 | #define nft_trans_chain_id(trans) \ |
1625 | (((struct nft_trans_chain *)trans->data)->chain_id) | |
b9703ed4 PNA |
1626 | #define nft_trans_basechain(trans) \ |
1627 | (((struct nft_trans_chain *)trans->data)->basechain) | |
1628 | #define nft_trans_chain_hooks(trans) \ | |
1629 | (((struct nft_trans_chain *)trans->data)->hook_list) | |
1a1e1a12 PM |
1630 | |
1631 | struct nft_trans_table { | |
1632 | bool update; | |
1a1e1a12 PM |
1633 | }; |
1634 | ||
1635 | #define nft_trans_table_update(trans) \ | |
1636 | (((struct nft_trans_table *)trans->data)->update) | |
1a1e1a12 PM |
1637 | |
1638 | struct nft_trans_elem { | |
1639 | struct nft_set *set; | |
1640 | struct nft_set_elem elem; | |
6a0a8d10 | 1641 | bool bound; |
1a1e1a12 PM |
1642 | }; |
1643 | ||
1644 | #define nft_trans_elem_set(trans) \ | |
1645 | (((struct nft_trans_elem *)trans->data)->set) | |
1646 | #define nft_trans_elem(trans) \ | |
1647 | (((struct nft_trans_elem *)trans->data)->elem) | |
6a0a8d10 PNA |
1648 | #define nft_trans_elem_set_bound(trans) \ |
1649 | (((struct nft_trans_elem *)trans->data)->bound) | |
1a1e1a12 | 1650 | |
e5009240 PNA |
1651 | struct nft_trans_obj { |
1652 | struct nft_object *obj; | |
d62d0ba9 FFM |
1653 | struct nft_object *newobj; |
1654 | bool update; | |
e5009240 PNA |
1655 | }; |
1656 | ||
1657 | #define nft_trans_obj(trans) \ | |
1658 | (((struct nft_trans_obj *)trans->data)->obj) | |
d62d0ba9 FFM |
1659 | #define nft_trans_obj_newobj(trans) \ |
1660 | (((struct nft_trans_obj *)trans->data)->newobj) | |
1661 | #define nft_trans_obj_update(trans) \ | |
1662 | (((struct nft_trans_obj *)trans->data)->update) | |
e5009240 | 1663 | |
3b49e2e9 PNA |
1664 | struct nft_trans_flowtable { |
1665 | struct nft_flowtable *flowtable; | |
78d9f48f PNA |
1666 | bool update; |
1667 | struct list_head hook_list; | |
7b35582c | 1668 | u32 flags; |
3b49e2e9 PNA |
1669 | }; |
1670 | ||
1671 | #define nft_trans_flowtable(trans) \ | |
1672 | (((struct nft_trans_flowtable *)trans->data)->flowtable) | |
78d9f48f PNA |
1673 | #define nft_trans_flowtable_update(trans) \ |
1674 | (((struct nft_trans_flowtable *)trans->data)->update) | |
1675 | #define nft_trans_flowtable_hooks(trans) \ | |
1676 | (((struct nft_trans_flowtable *)trans->data)->hook_list) | |
7b35582c PNA |
1677 | #define nft_trans_flowtable_flags(trans) \ |
1678 | (((struct nft_trans_flowtable *)trans->data)->flags) | |
3b49e2e9 | 1679 | |
02c7b25e | 1680 | int __init nft_chain_filter_init(void); |
d209df3e | 1681 | void nft_chain_filter_fini(void); |
02c7b25e | 1682 | |
c1deb065 FW |
1683 | void __init nft_chain_route_init(void); |
1684 | void nft_chain_route_fini(void); | |
ffe8923f FW |
1685 | |
1686 | void nf_tables_trans_destroy_flush_work(void); | |
917d80d3 PNA |
1687 | |
1688 | int nf_msecs_to_jiffies64(const struct nlattr *nla, u64 *result); | |
1689 | __be64 nf_jiffies64_to_msecs(u64 input); | |
1690 | ||
cefa31a9 FW |
1691 | #ifdef CONFIG_MODULES |
1692 | __printf(2, 3) int nft_request_module(struct net *net, const char *fmt, ...); | |
1693 | #else | |
1694 | static inline int nft_request_module(struct net *net, const char *fmt, ...) { return -ENOENT; } | |
1695 | #endif | |
0854db2a FW |
1696 | |
1697 | struct nftables_pernet { | |
1698 | struct list_head tables; | |
1699 | struct list_head commit_list; | |
1700 | struct list_head module_list; | |
1701 | struct list_head notify_list; | |
1702 | struct mutex commit_mutex; | |
ab482c6b | 1703 | u64 table_handle; |
0854db2a | 1704 | unsigned int base_seq; |
0854db2a FW |
1705 | }; |
1706 | ||
d59d2f82 PNA |
1707 | extern unsigned int nf_tables_net_id; |
1708 | ||
1709 | static inline struct nftables_pernet *nft_pernet(const struct net *net) | |
1710 | { | |
1711 | return net_generic(net, nf_tables_net_id); | |
1712 | } | |
1713 | ||
b2d30654 PNA |
1714 | #define __NFT_REDUCE_READONLY 1UL |
1715 | #define NFT_REDUCE_READONLY (void *)__NFT_REDUCE_READONLY | |
1716 | ||
1717 | static inline bool nft_reduce_is_readonly(const struct nft_expr *expr) | |
1718 | { | |
1719 | return expr->ops->reduce == NFT_REDUCE_READONLY; | |
1720 | } | |
1721 | ||
34cc9e52 PNA |
1722 | void nft_reg_track_update(struct nft_regs_track *track, |
1723 | const struct nft_expr *expr, u8 dreg, u8 len); | |
1724 | void nft_reg_track_cancel(struct nft_regs_track *track, u8 dreg, u8 len); | |
1725 | void __nft_reg_track_cancel(struct nft_regs_track *track, u8 dreg); | |
1726 | ||
1727 | static inline bool nft_reg_track_cmp(struct nft_regs_track *track, | |
1728 | const struct nft_expr *expr, u8 dreg) | |
1729 | { | |
1730 | return track->regs[dreg].selector && | |
1731 | track->regs[dreg].selector->ops == expr->ops && | |
1732 | track->regs[dreg].num_reg == 0; | |
1733 | } | |
1734 | ||
96518518 | 1735 | #endif /* _NET_NF_TABLES_H */ |