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