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 | ||
0b2d8a7b | 5 | #include <linux/module.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> |
96518518 PM |
12 | #include <net/netlink.h> |
13 | ||
20a69341 PM |
14 | #define NFT_JUMP_STACK_SIZE 16 |
15 | ||
96518518 PM |
16 | struct nft_pktinfo { |
17 | struct sk_buff *skb; | |
beac5afa | 18 | bool tprot_set; |
4566bf27 | 19 | u8 tprot; |
0ca743a5 PNA |
20 | /* for x_tables compatibility */ |
21 | struct xt_action_param xt; | |
96518518 PM |
22 | }; |
23 | ||
0e5a1c7e PNA |
24 | static inline struct net *nft_net(const struct nft_pktinfo *pkt) |
25 | { | |
26 | return pkt->xt.state->net; | |
27 | } | |
28 | ||
29 | static inline unsigned int nft_hook(const struct nft_pktinfo *pkt) | |
30 | { | |
31 | return pkt->xt.state->hook; | |
32 | } | |
33 | ||
34 | static inline u8 nft_pf(const struct nft_pktinfo *pkt) | |
35 | { | |
36 | return pkt->xt.state->pf; | |
37 | } | |
38 | ||
39 | static inline const struct net_device *nft_in(const struct nft_pktinfo *pkt) | |
40 | { | |
41 | return pkt->xt.state->in; | |
42 | } | |
43 | ||
44 | static inline const struct net_device *nft_out(const struct nft_pktinfo *pkt) | |
45 | { | |
46 | return pkt->xt.state->out; | |
47 | } | |
48 | ||
0ca743a5 | 49 | static inline void nft_set_pktinfo(struct nft_pktinfo *pkt, |
0ca743a5 | 50 | struct sk_buff *skb, |
073bfd56 | 51 | const struct nf_hook_state *state) |
0ca743a5 PNA |
52 | { |
53 | pkt->skb = skb; | |
613dbd95 | 54 | pkt->xt.state = state; |
0ca743a5 PNA |
55 | } |
56 | ||
7a4473a3 PNA |
57 | static inline void nft_set_pktinfo_unspec(struct nft_pktinfo *pkt, |
58 | struct sk_buff *skb) | |
beac5afa PNA |
59 | { |
60 | pkt->tprot_set = false; | |
61 | pkt->tprot = 0; | |
62 | pkt->xt.thoff = 0; | |
63 | pkt->xt.fragoff = 0; | |
64 | } | |
65 | ||
a55e22e9 PM |
66 | /** |
67 | * struct nft_verdict - nf_tables verdict | |
68 | * | |
69 | * @code: nf_tables/netfilter verdict code | |
70 | * @chain: destination chain for NFT_JUMP/NFT_GOTO | |
71 | */ | |
72 | struct nft_verdict { | |
73 | u32 code; | |
74 | struct nft_chain *chain; | |
75 | }; | |
76 | ||
96518518 PM |
77 | struct nft_data { |
78 | union { | |
1ca2e170 PM |
79 | u32 data[4]; |
80 | struct nft_verdict verdict; | |
96518518 PM |
81 | }; |
82 | } __attribute__((aligned(__alignof__(u64)))); | |
83 | ||
a55e22e9 PM |
84 | /** |
85 | * struct nft_regs - nf_tables register set | |
86 | * | |
87 | * @data: data registers | |
88 | * @verdict: verdict register | |
89 | * | |
90 | * The first four data registers alias to the verdict register. | |
91 | */ | |
92 | struct nft_regs { | |
93 | union { | |
49499c3e | 94 | u32 data[20]; |
a55e22e9 PM |
95 | struct nft_verdict verdict; |
96 | }; | |
97 | }; | |
98 | ||
10596608 LZ |
99 | /* Store/load an u16 or u8 integer to/from the u32 data register. |
100 | * | |
101 | * Note, when using concatenations, register allocation happens at 32-bit | |
102 | * level. So for store instruction, pad the rest part with zero to avoid | |
103 | * garbage values. | |
104 | */ | |
105 | ||
106 | static inline void nft_reg_store16(u32 *dreg, u16 val) | |
107 | { | |
108 | *dreg = 0; | |
109 | *(u16 *)dreg = val; | |
110 | } | |
111 | ||
112 | static inline void nft_reg_store8(u32 *dreg, u8 val) | |
113 | { | |
114 | *dreg = 0; | |
115 | *(u8 *)dreg = val; | |
116 | } | |
117 | ||
118 | static inline u16 nft_reg_load16(u32 *sreg) | |
119 | { | |
120 | return *(u16 *)sreg; | |
121 | } | |
122 | ||
123 | static inline u8 nft_reg_load8(u32 *sreg) | |
124 | { | |
125 | return *(u8 *)sreg; | |
126 | } | |
127 | ||
49499c3e PM |
128 | static inline void nft_data_copy(u32 *dst, const struct nft_data *src, |
129 | unsigned int len) | |
96518518 | 130 | { |
49499c3e | 131 | memcpy(dst, src, len); |
96518518 PM |
132 | } |
133 | ||
134 | static inline void nft_data_debug(const struct nft_data *data) | |
135 | { | |
136 | pr_debug("data[0]=%x data[1]=%x data[2]=%x data[3]=%x\n", | |
137 | data->data[0], data->data[1], | |
138 | data->data[2], data->data[3]); | |
139 | } | |
140 | ||
141 | /** | |
20a69341 | 142 | * struct nft_ctx - nf_tables rule/set context |
96518518 | 143 | * |
99633ab2 | 144 | * @net: net namespace |
96518518 PM |
145 | * @afi: address family info |
146 | * @table: the table the chain is contained in | |
147 | * @chain: the chain the rule is contained in | |
0ca743a5 | 148 | * @nla: netlink attributes |
128ad332 PNA |
149 | * @portid: netlink portID of the original message |
150 | * @seq: netlink sequence number | |
151 | * @report: notify via unicast netlink message | |
96518518 PM |
152 | */ |
153 | struct nft_ctx { | |
99633ab2 | 154 | struct net *net; |
7c95f6d8 PNA |
155 | struct nft_af_info *afi; |
156 | struct nft_table *table; | |
157 | struct nft_chain *chain; | |
0ca743a5 | 158 | const struct nlattr * const *nla; |
128ad332 PNA |
159 | u32 portid; |
160 | u32 seq; | |
161 | bool report; | |
96518518 PM |
162 | }; |
163 | ||
96518518 PM |
164 | struct nft_data_desc { |
165 | enum nft_data_types type; | |
166 | unsigned int len; | |
167 | }; | |
168 | ||
d0a11fc3 PM |
169 | int nft_data_init(const struct nft_ctx *ctx, |
170 | struct nft_data *data, unsigned int size, | |
5eccdfaa | 171 | struct nft_data_desc *desc, const struct nlattr *nla); |
59105446 | 172 | void nft_data_release(const struct nft_data *data, enum nft_data_types type); |
5eccdfaa JP |
173 | int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data, |
174 | enum nft_data_types type, unsigned int len); | |
96518518 PM |
175 | |
176 | static inline enum nft_data_types nft_dreg_to_type(enum nft_registers reg) | |
177 | { | |
178 | return reg == NFT_REG_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE; | |
179 | } | |
180 | ||
20a69341 PM |
181 | static inline enum nft_registers nft_type_to_reg(enum nft_data_types type) |
182 | { | |
bf798657 | 183 | return type == NFT_DATA_VERDICT ? NFT_REG_VERDICT : NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE; |
20a69341 PM |
184 | } |
185 | ||
f1d505bb | 186 | int nft_parse_u32_check(const struct nlattr *attr, int max, u32 *dest); |
b1c96ed3 PM |
187 | unsigned int nft_parse_register(const struct nlattr *attr); |
188 | int nft_dump_register(struct sk_buff *skb, unsigned int attr, unsigned int reg); | |
189 | ||
d07db988 | 190 | int nft_validate_register_load(enum nft_registers reg, unsigned int len); |
1ec10212 PM |
191 | int nft_validate_register_store(const struct nft_ctx *ctx, |
192 | enum nft_registers reg, | |
193 | const struct nft_data *data, | |
194 | enum nft_data_types type, unsigned int len); | |
96518518 | 195 | |
86f1ec32 PM |
196 | /** |
197 | * struct nft_userdata - user defined data associated with an object | |
198 | * | |
199 | * @len: length of the data | |
200 | * @data: content | |
201 | * | |
202 | * The presence of user data is indicated in an object specific fashion, | |
203 | * so a length of zero can't occur and the value "len" indicates data | |
204 | * of length len + 1. | |
205 | */ | |
206 | struct nft_userdata { | |
207 | u8 len; | |
208 | unsigned char data[0]; | |
209 | }; | |
210 | ||
20a69341 PM |
211 | /** |
212 | * struct nft_set_elem - generic representation of set elements | |
213 | * | |
20a69341 | 214 | * @key: element key |
fe2811eb | 215 | * @priv: element private data and extensions |
20a69341 PM |
216 | */ |
217 | struct nft_set_elem { | |
7d740264 PM |
218 | union { |
219 | u32 buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)]; | |
220 | struct nft_data val; | |
221 | } key; | |
fe2811eb | 222 | void *priv; |
20a69341 PM |
223 | }; |
224 | ||
225 | struct nft_set; | |
226 | struct nft_set_iter { | |
8588ac09 | 227 | u8 genmask; |
20a69341 PM |
228 | unsigned int count; |
229 | unsigned int skip; | |
230 | int err; | |
231 | int (*fn)(const struct nft_ctx *ctx, | |
de70185d | 232 | struct nft_set *set, |
20a69341 | 233 | const struct nft_set_iter *iter, |
de70185d | 234 | struct nft_set_elem *elem); |
20a69341 PM |
235 | }; |
236 | ||
c50b960c PM |
237 | /** |
238 | * struct nft_set_desc - description of set elements | |
239 | * | |
240 | * @klen: key length | |
241 | * @dlen: data length | |
242 | * @size: number of set elements | |
243 | */ | |
244 | struct nft_set_desc { | |
245 | unsigned int klen; | |
246 | unsigned int dlen; | |
247 | unsigned int size; | |
248 | }; | |
249 | ||
250 | /** | |
251 | * enum nft_set_class - performance class | |
252 | * | |
253 | * @NFT_LOOKUP_O_1: constant, O(1) | |
254 | * @NFT_LOOKUP_O_LOG_N: logarithmic, O(log N) | |
255 | * @NFT_LOOKUP_O_N: linear, O(N) | |
256 | */ | |
257 | enum nft_set_class { | |
258 | NFT_SET_CLASS_O_1, | |
259 | NFT_SET_CLASS_O_LOG_N, | |
260 | NFT_SET_CLASS_O_N, | |
261 | }; | |
262 | ||
263 | /** | |
264 | * struct nft_set_estimate - estimation of memory and performance | |
265 | * characteristics | |
266 | * | |
267 | * @size: required memory | |
55af753c | 268 | * @lookup: lookup performance class |
0b5a7874 | 269 | * @space: memory class |
c50b960c PM |
270 | */ |
271 | struct nft_set_estimate { | |
272 | unsigned int size; | |
55af753c | 273 | enum nft_set_class lookup; |
0b5a7874 | 274 | enum nft_set_class space; |
c50b960c PM |
275 | }; |
276 | ||
2b664957 PNA |
277 | /** |
278 | * struct nft_set_type - nf_tables set type | |
279 | * | |
280 | * @select_ops: function to select nft_set_ops | |
281 | * @ops: default ops, used when no select_ops functions is present | |
282 | * @list: used internally | |
283 | * @owner: module reference | |
284 | */ | |
285 | struct nft_set_type { | |
286 | const struct nft_set_ops *(*select_ops)(const struct nft_ctx *, | |
287 | const struct nft_set_desc *desc, | |
288 | u32 flags); | |
289 | const struct nft_set_ops *ops; | |
290 | struct list_head list; | |
291 | struct module *owner; | |
292 | }; | |
293 | ||
b2832dd6 | 294 | struct nft_set_ext; |
22fe54d5 | 295 | struct nft_expr; |
b2832dd6 | 296 | |
20a69341 PM |
297 | /** |
298 | * struct nft_set_ops - nf_tables set operations | |
299 | * | |
300 | * @lookup: look up an element within the set | |
301 | * @insert: insert new element into set | |
cc02e457 | 302 | * @activate: activate new element in the next generation |
8411b644 | 303 | * @deactivate: lookup for element and deactivate it in the next generation |
1ba1c414 | 304 | * @flush: deactivate element in the next generation |
20a69341 PM |
305 | * @remove: remove element from set |
306 | * @walk: iterate over all set elemeennts | |
ba0e4d99 | 307 | * @get: get set elements |
20a69341 PM |
308 | * @privsize: function to return size of set private data |
309 | * @init: initialize private data of new set instance | |
310 | * @destroy: destroy private data of set instance | |
fe2811eb | 311 | * @elemsize: element private size |
20a69341 PM |
312 | * @features: features supported by the implementation |
313 | */ | |
314 | struct nft_set_ops { | |
42a55769 PNA |
315 | bool (*lookup)(const struct net *net, |
316 | const struct nft_set *set, | |
8cd8937a | 317 | const u32 *key, |
b2832dd6 | 318 | const struct nft_set_ext **ext); |
22fe54d5 | 319 | bool (*update)(struct nft_set *set, |
8cd8937a | 320 | const u32 *key, |
22fe54d5 PM |
321 | void *(*new)(struct nft_set *, |
322 | const struct nft_expr *, | |
a55e22e9 | 323 | struct nft_regs *), |
22fe54d5 | 324 | const struct nft_expr *expr, |
a55e22e9 | 325 | struct nft_regs *regs, |
22fe54d5 PM |
326 | const struct nft_set_ext **ext); |
327 | ||
42a55769 PNA |
328 | int (*insert)(const struct net *net, |
329 | const struct nft_set *set, | |
c016c7e4 PNA |
330 | const struct nft_set_elem *elem, |
331 | struct nft_set_ext **ext); | |
42a55769 PNA |
332 | void (*activate)(const struct net *net, |
333 | const struct nft_set *set, | |
cc02e457 | 334 | const struct nft_set_elem *elem); |
42a55769 PNA |
335 | void * (*deactivate)(const struct net *net, |
336 | const struct nft_set *set, | |
cc02e457 | 337 | const struct nft_set_elem *elem); |
1ba1c414 PNA |
338 | bool (*flush)(const struct net *net, |
339 | const struct nft_set *set, | |
340 | void *priv); | |
5cb82a38 PNA |
341 | void (*remove)(const struct net *net, |
342 | const struct nft_set *set, | |
20a69341 PM |
343 | const struct nft_set_elem *elem); |
344 | void (*walk)(const struct nft_ctx *ctx, | |
de70185d | 345 | struct nft_set *set, |
20a69341 | 346 | struct nft_set_iter *iter); |
ba0e4d99 PNA |
347 | void * (*get)(const struct net *net, |
348 | const struct nft_set *set, | |
349 | const struct nft_set_elem *elem, | |
350 | unsigned int flags); | |
20a69341 | 351 | |
347b408d PNA |
352 | unsigned int (*privsize)(const struct nlattr * const nla[], |
353 | const struct nft_set_desc *desc); | |
c50b960c PM |
354 | bool (*estimate)(const struct nft_set_desc *desc, |
355 | u32 features, | |
356 | struct nft_set_estimate *est); | |
20a69341 | 357 | int (*init)(const struct nft_set *set, |
c50b960c | 358 | const struct nft_set_desc *desc, |
20a69341 PM |
359 | const struct nlattr * const nla[]); |
360 | void (*destroy)(const struct nft_set *set); | |
361 | ||
fe2811eb | 362 | unsigned int elemsize; |
20a69341 | 363 | u32 features; |
2b664957 | 364 | const struct nft_set_type *type; |
20a69341 PM |
365 | }; |
366 | ||
2b664957 PNA |
367 | int nft_register_set(struct nft_set_type *type); |
368 | void nft_unregister_set(struct nft_set_type *type); | |
20a69341 PM |
369 | |
370 | /** | |
371 | * struct nft_set - nf_tables set instance | |
372 | * | |
373 | * @list: table set list node | |
374 | * @bindings: list of set bindings | |
375 | * @name: name of the set | |
376 | * @ktype: key type (numeric type defined by userspace, not used in the kernel) | |
377 | * @dtype: data type (verdict or numeric type defined by userspace) | |
8aeff920 | 378 | * @objtype: object type (see NFT_OBJECT_* definitions) |
c50b960c PM |
379 | * @size: maximum set size |
380 | * @nelems: number of elements | |
3dd0673a | 381 | * @ndeact: number of deactivated elements queued for removal |
d3e2a111 | 382 | * @timeout: default timeout value in jiffies |
761da293 | 383 | * @gc_int: garbage collection interval in msecs |
9363dc4b | 384 | * @policy: set parameterization (see enum nft_set_policies) |
e6d8ecac CFG |
385 | * @udlen: user data length |
386 | * @udata: user data | |
20a69341 PM |
387 | * @ops: set ops |
388 | * @flags: set flags | |
37a9cc52 | 389 | * @genmask: generation mask |
20a69341 PM |
390 | * @klen: key length |
391 | * @dlen: data length | |
392 | * @data: private set data | |
393 | */ | |
394 | struct nft_set { | |
395 | struct list_head list; | |
396 | struct list_head bindings; | |
38745490 | 397 | char *name; |
20a69341 PM |
398 | u32 ktype; |
399 | u32 dtype; | |
8aeff920 | 400 | u32 objtype; |
c50b960c | 401 | u32 size; |
3dd0673a PM |
402 | atomic_t nelems; |
403 | u32 ndeact; | |
761da293 PM |
404 | u64 timeout; |
405 | u32 gc_int; | |
9363dc4b | 406 | u16 policy; |
e6d8ecac CFG |
407 | u16 udlen; |
408 | unsigned char *udata; | |
20a69341 PM |
409 | /* runtime data below here */ |
410 | const struct nft_set_ops *ops ____cacheline_aligned; | |
37a9cc52 PNA |
411 | u16 flags:14, |
412 | genmask:2; | |
20a69341 PM |
413 | u8 klen; |
414 | u8 dlen; | |
415 | unsigned char data[] | |
416 | __attribute__((aligned(__alignof__(u64)))); | |
417 | }; | |
418 | ||
408070d6 PNA |
419 | static inline bool nft_set_is_anonymous(const struct nft_set *set) |
420 | { | |
421 | return set->flags & NFT_SET_ANONYMOUS; | |
422 | } | |
423 | ||
20a69341 PM |
424 | static inline void *nft_set_priv(const struct nft_set *set) |
425 | { | |
426 | return (void *)set->data; | |
427 | } | |
428 | ||
9d098292 PM |
429 | static inline struct nft_set *nft_set_container_of(const void *priv) |
430 | { | |
431 | return (void *)priv - offsetof(struct nft_set, data); | |
432 | } | |
433 | ||
c7a72e3f PNA |
434 | struct nft_set *nft_set_lookup(const struct net *net, |
435 | const struct nft_table *table, | |
436 | const struct nlattr *nla_set_name, | |
437 | const struct nlattr *nla_set_id, | |
438 | u8 genmask); | |
20a69341 | 439 | |
761da293 PM |
440 | static inline unsigned long nft_set_gc_interval(const struct nft_set *set) |
441 | { | |
442 | return set->gc_int ? msecs_to_jiffies(set->gc_int) : HZ; | |
443 | } | |
444 | ||
20a69341 PM |
445 | /** |
446 | * struct nft_set_binding - nf_tables set binding | |
447 | * | |
448 | * @list: set bindings list node | |
449 | * @chain: chain containing the rule bound to the set | |
11113e19 | 450 | * @flags: set action flags |
20a69341 PM |
451 | * |
452 | * A set binding contains all information necessary for validation | |
453 | * of new elements added to a bound set. | |
454 | */ | |
455 | struct nft_set_binding { | |
456 | struct list_head list; | |
457 | const struct nft_chain *chain; | |
11113e19 | 458 | u32 flags; |
20a69341 PM |
459 | }; |
460 | ||
5eccdfaa JP |
461 | int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set, |
462 | struct nft_set_binding *binding); | |
463 | void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set, | |
464 | struct nft_set_binding *binding); | |
20a69341 | 465 | |
3ac4c07a PM |
466 | /** |
467 | * enum nft_set_extensions - set extension type IDs | |
468 | * | |
469 | * @NFT_SET_EXT_KEY: element key | |
470 | * @NFT_SET_EXT_DATA: mapping data | |
471 | * @NFT_SET_EXT_FLAGS: element flags | |
c3e1b005 PM |
472 | * @NFT_SET_EXT_TIMEOUT: element timeout |
473 | * @NFT_SET_EXT_EXPIRATION: element expiration time | |
68e942e8 | 474 | * @NFT_SET_EXT_USERDATA: user data associated with the element |
f25ad2e9 | 475 | * @NFT_SET_EXT_EXPR: expression assiociated with the element |
8aeff920 | 476 | * @NFT_SET_EXT_OBJREF: stateful object reference associated with element |
3ac4c07a PM |
477 | * @NFT_SET_EXT_NUM: number of extension types |
478 | */ | |
479 | enum nft_set_extensions { | |
480 | NFT_SET_EXT_KEY, | |
481 | NFT_SET_EXT_DATA, | |
482 | NFT_SET_EXT_FLAGS, | |
c3e1b005 PM |
483 | NFT_SET_EXT_TIMEOUT, |
484 | NFT_SET_EXT_EXPIRATION, | |
68e942e8 | 485 | NFT_SET_EXT_USERDATA, |
f25ad2e9 | 486 | NFT_SET_EXT_EXPR, |
8aeff920 | 487 | NFT_SET_EXT_OBJREF, |
3ac4c07a PM |
488 | NFT_SET_EXT_NUM |
489 | }; | |
490 | ||
491 | /** | |
492 | * struct nft_set_ext_type - set extension type | |
493 | * | |
494 | * @len: fixed part length of the extension | |
495 | * @align: alignment requirements of the extension | |
496 | */ | |
497 | struct nft_set_ext_type { | |
498 | u8 len; | |
499 | u8 align; | |
500 | }; | |
501 | ||
502 | extern const struct nft_set_ext_type nft_set_ext_types[]; | |
503 | ||
504 | /** | |
505 | * struct nft_set_ext_tmpl - set extension template | |
506 | * | |
507 | * @len: length of extension area | |
508 | * @offset: offsets of individual extension types | |
509 | */ | |
510 | struct nft_set_ext_tmpl { | |
511 | u16 len; | |
512 | u8 offset[NFT_SET_EXT_NUM]; | |
513 | }; | |
514 | ||
515 | /** | |
516 | * struct nft_set_ext - set extensions | |
517 | * | |
cc02e457 | 518 | * @genmask: generation mask |
3ac4c07a PM |
519 | * @offset: offsets of individual extension types |
520 | * @data: beginning of extension data | |
521 | */ | |
522 | struct nft_set_ext { | |
cc02e457 | 523 | u8 genmask; |
3ac4c07a PM |
524 | u8 offset[NFT_SET_EXT_NUM]; |
525 | char data[0]; | |
526 | }; | |
527 | ||
528 | static inline void nft_set_ext_prepare(struct nft_set_ext_tmpl *tmpl) | |
529 | { | |
530 | memset(tmpl, 0, sizeof(*tmpl)); | |
531 | tmpl->len = sizeof(struct nft_set_ext); | |
532 | } | |
533 | ||
534 | static inline void nft_set_ext_add_length(struct nft_set_ext_tmpl *tmpl, u8 id, | |
535 | unsigned int len) | |
536 | { | |
537 | tmpl->len = ALIGN(tmpl->len, nft_set_ext_types[id].align); | |
538 | BUG_ON(tmpl->len > U8_MAX); | |
539 | tmpl->offset[id] = tmpl->len; | |
540 | tmpl->len += nft_set_ext_types[id].len + len; | |
541 | } | |
542 | ||
543 | static inline void nft_set_ext_add(struct nft_set_ext_tmpl *tmpl, u8 id) | |
544 | { | |
545 | nft_set_ext_add_length(tmpl, id, 0); | |
546 | } | |
547 | ||
548 | static inline void nft_set_ext_init(struct nft_set_ext *ext, | |
549 | const struct nft_set_ext_tmpl *tmpl) | |
550 | { | |
551 | memcpy(ext->offset, tmpl->offset, sizeof(ext->offset)); | |
552 | } | |
553 | ||
554 | static inline bool __nft_set_ext_exists(const struct nft_set_ext *ext, u8 id) | |
555 | { | |
556 | return !!ext->offset[id]; | |
557 | } | |
558 | ||
559 | static inline bool nft_set_ext_exists(const struct nft_set_ext *ext, u8 id) | |
560 | { | |
561 | return ext && __nft_set_ext_exists(ext, id); | |
562 | } | |
563 | ||
564 | static inline void *nft_set_ext(const struct nft_set_ext *ext, u8 id) | |
565 | { | |
566 | return (void *)ext + ext->offset[id]; | |
567 | } | |
568 | ||
569 | static inline struct nft_data *nft_set_ext_key(const struct nft_set_ext *ext) | |
570 | { | |
571 | return nft_set_ext(ext, NFT_SET_EXT_KEY); | |
572 | } | |
573 | ||
574 | static inline struct nft_data *nft_set_ext_data(const struct nft_set_ext *ext) | |
575 | { | |
576 | return nft_set_ext(ext, NFT_SET_EXT_DATA); | |
577 | } | |
578 | ||
579 | static inline u8 *nft_set_ext_flags(const struct nft_set_ext *ext) | |
580 | { | |
581 | return nft_set_ext(ext, NFT_SET_EXT_FLAGS); | |
582 | } | |
ef1f7df9 | 583 | |
c3e1b005 PM |
584 | static inline u64 *nft_set_ext_timeout(const struct nft_set_ext *ext) |
585 | { | |
586 | return nft_set_ext(ext, NFT_SET_EXT_TIMEOUT); | |
587 | } | |
588 | ||
589 | static inline unsigned long *nft_set_ext_expiration(const struct nft_set_ext *ext) | |
590 | { | |
591 | return nft_set_ext(ext, NFT_SET_EXT_EXPIRATION); | |
592 | } | |
593 | ||
68e942e8 PM |
594 | static inline struct nft_userdata *nft_set_ext_userdata(const struct nft_set_ext *ext) |
595 | { | |
596 | return nft_set_ext(ext, NFT_SET_EXT_USERDATA); | |
597 | } | |
598 | ||
f25ad2e9 PM |
599 | static inline struct nft_expr *nft_set_ext_expr(const struct nft_set_ext *ext) |
600 | { | |
601 | return nft_set_ext(ext, NFT_SET_EXT_EXPR); | |
602 | } | |
603 | ||
c3e1b005 PM |
604 | static inline bool nft_set_elem_expired(const struct nft_set_ext *ext) |
605 | { | |
606 | return nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION) && | |
607 | time_is_before_eq_jiffies(*nft_set_ext_expiration(ext)); | |
608 | } | |
609 | ||
fe2811eb PM |
610 | static inline struct nft_set_ext *nft_set_elem_ext(const struct nft_set *set, |
611 | void *elem) | |
612 | { | |
613 | return elem + set->ops->elemsize; | |
614 | } | |
615 | ||
8aeff920 PNA |
616 | static inline struct nft_object **nft_set_ext_obj(const struct nft_set_ext *ext) |
617 | { | |
618 | return nft_set_ext(ext, NFT_SET_EXT_OBJREF); | |
619 | } | |
620 | ||
22fe54d5 PM |
621 | void *nft_set_elem_init(const struct nft_set *set, |
622 | const struct nft_set_ext_tmpl *tmpl, | |
49499c3e | 623 | const u32 *key, const u32 *data, |
22fe54d5 | 624 | u64 timeout, gfp_t gfp); |
61f9e292 LZ |
625 | void nft_set_elem_destroy(const struct nft_set *set, void *elem, |
626 | bool destroy_expr); | |
61edafbb | 627 | |
cfed7e1b PM |
628 | /** |
629 | * struct nft_set_gc_batch_head - nf_tables set garbage collection batch | |
630 | * | |
631 | * @rcu: rcu head | |
632 | * @set: set the elements belong to | |
633 | * @cnt: count of elements | |
634 | */ | |
635 | struct nft_set_gc_batch_head { | |
636 | struct rcu_head rcu; | |
637 | const struct nft_set *set; | |
638 | unsigned int cnt; | |
639 | }; | |
640 | ||
641 | #define NFT_SET_GC_BATCH_SIZE ((PAGE_SIZE - \ | |
642 | sizeof(struct nft_set_gc_batch_head)) / \ | |
643 | sizeof(void *)) | |
644 | ||
645 | /** | |
646 | * struct nft_set_gc_batch - nf_tables set garbage collection batch | |
647 | * | |
648 | * @head: GC batch head | |
649 | * @elems: garbage collection elements | |
650 | */ | |
651 | struct nft_set_gc_batch { | |
652 | struct nft_set_gc_batch_head head; | |
653 | void *elems[NFT_SET_GC_BATCH_SIZE]; | |
654 | }; | |
655 | ||
656 | struct nft_set_gc_batch *nft_set_gc_batch_alloc(const struct nft_set *set, | |
657 | gfp_t gfp); | |
658 | void nft_set_gc_batch_release(struct rcu_head *rcu); | |
659 | ||
660 | static inline void nft_set_gc_batch_complete(struct nft_set_gc_batch *gcb) | |
661 | { | |
662 | if (gcb != NULL) | |
663 | call_rcu(&gcb->head.rcu, nft_set_gc_batch_release); | |
664 | } | |
665 | ||
666 | static inline struct nft_set_gc_batch * | |
667 | nft_set_gc_batch_check(const struct nft_set *set, struct nft_set_gc_batch *gcb, | |
668 | gfp_t gfp) | |
669 | { | |
670 | if (gcb != NULL) { | |
671 | if (gcb->head.cnt + 1 < ARRAY_SIZE(gcb->elems)) | |
672 | return gcb; | |
673 | nft_set_gc_batch_complete(gcb); | |
674 | } | |
675 | return nft_set_gc_batch_alloc(set, gfp); | |
676 | } | |
677 | ||
678 | static inline void nft_set_gc_batch_add(struct nft_set_gc_batch *gcb, | |
679 | void *elem) | |
680 | { | |
681 | gcb->elems[gcb->head.cnt++] = elem; | |
682 | } | |
683 | ||
96518518 | 684 | /** |
ef1f7df9 | 685 | * struct nft_expr_type - nf_tables expression type |
96518518 | 686 | * |
ef1f7df9 PM |
687 | * @select_ops: function to select nft_expr_ops |
688 | * @ops: default ops, used when no select_ops functions is present | |
96518518 PM |
689 | * @list: used internally |
690 | * @name: Identifier | |
691 | * @owner: module reference | |
692 | * @policy: netlink attribute policy | |
693 | * @maxattr: highest netlink attribute number | |
64d46806 | 694 | * @family: address family for AF-specific types |
151d799a | 695 | * @flags: expression type flags |
ef1f7df9 PM |
696 | */ |
697 | struct nft_expr_type { | |
0ca743a5 PNA |
698 | const struct nft_expr_ops *(*select_ops)(const struct nft_ctx *, |
699 | const struct nlattr * const tb[]); | |
ef1f7df9 PM |
700 | const struct nft_expr_ops *ops; |
701 | struct list_head list; | |
702 | const char *name; | |
703 | struct module *owner; | |
704 | const struct nla_policy *policy; | |
705 | unsigned int maxattr; | |
64d46806 | 706 | u8 family; |
151d799a | 707 | u8 flags; |
ef1f7df9 PM |
708 | }; |
709 | ||
151d799a PM |
710 | #define NFT_EXPR_STATEFUL 0x1 |
711 | ||
ef1f7df9 PM |
712 | /** |
713 | * struct nft_expr_ops - nf_tables expression operations | |
714 | * | |
715 | * @eval: Expression evaluation function | |
96518518 | 716 | * @size: full expression size, including private data size |
ef1f7df9 PM |
717 | * @init: initialization function |
718 | * @destroy: destruction function | |
719 | * @dump: function to dump parameters | |
720 | * @type: expression type | |
0ca743a5 PNA |
721 | * @validate: validate expression, called during loop detection |
722 | * @data: extra data to attach to this expression operation | |
96518518 PM |
723 | */ |
724 | struct nft_expr; | |
725 | struct nft_expr_ops { | |
726 | void (*eval)(const struct nft_expr *expr, | |
a55e22e9 | 727 | struct nft_regs *regs, |
96518518 | 728 | const struct nft_pktinfo *pkt); |
086f3321 PNA |
729 | int (*clone)(struct nft_expr *dst, |
730 | const struct nft_expr *src); | |
ef1f7df9 PM |
731 | unsigned int size; |
732 | ||
96518518 PM |
733 | int (*init)(const struct nft_ctx *ctx, |
734 | const struct nft_expr *expr, | |
735 | const struct nlattr * const tb[]); | |
62472bce PM |
736 | void (*destroy)(const struct nft_ctx *ctx, |
737 | const struct nft_expr *expr); | |
96518518 PM |
738 | int (*dump)(struct sk_buff *skb, |
739 | const struct nft_expr *expr); | |
0ca743a5 PNA |
740 | int (*validate)(const struct nft_ctx *ctx, |
741 | const struct nft_expr *expr, | |
742 | const struct nft_data **data); | |
ef1f7df9 | 743 | const struct nft_expr_type *type; |
0ca743a5 | 744 | void *data; |
96518518 PM |
745 | }; |
746 | ||
ef1f7df9 | 747 | #define NFT_EXPR_MAXATTR 16 |
96518518 PM |
748 | #define NFT_EXPR_SIZE(size) (sizeof(struct nft_expr) + \ |
749 | ALIGN(size, __alignof__(struct nft_expr))) | |
750 | ||
751 | /** | |
752 | * struct nft_expr - nf_tables expression | |
753 | * | |
754 | * @ops: expression ops | |
755 | * @data: expression private data | |
756 | */ | |
757 | struct nft_expr { | |
758 | const struct nft_expr_ops *ops; | |
759 | unsigned char data[]; | |
760 | }; | |
761 | ||
762 | static inline void *nft_expr_priv(const struct nft_expr *expr) | |
763 | { | |
764 | return (void *)expr->data; | |
765 | } | |
766 | ||
0b2d8a7b PM |
767 | struct nft_expr *nft_expr_init(const struct nft_ctx *ctx, |
768 | const struct nlattr *nla); | |
769 | void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr); | |
770 | int nft_expr_dump(struct sk_buff *skb, unsigned int attr, | |
771 | const struct nft_expr *expr); | |
772 | ||
086f3321 | 773 | static inline int nft_expr_clone(struct nft_expr *dst, struct nft_expr *src) |
0b2d8a7b | 774 | { |
086f3321 PNA |
775 | int err; |
776 | ||
086f3321 PNA |
777 | if (src->ops->clone) { |
778 | dst->ops = src->ops; | |
779 | err = src->ops->clone(dst, src); | |
780 | if (err < 0) | |
781 | return err; | |
782 | } else { | |
783 | memcpy(dst, src, src->ops->size); | |
784 | } | |
61f9e292 LZ |
785 | |
786 | __module_get(src->ops->type->owner); | |
086f3321 | 787 | return 0; |
0b2d8a7b PM |
788 | } |
789 | ||
96518518 PM |
790 | /** |
791 | * struct nft_rule - nf_tables rule | |
792 | * | |
793 | * @list: used internally | |
96518518 | 794 | * @handle: rule handle |
0628b123 | 795 | * @genmask: generation mask |
96518518 | 796 | * @dlen: length of expression data |
86f1ec32 | 797 | * @udata: user data is appended to the rule |
96518518 PM |
798 | * @data: expression data |
799 | */ | |
800 | struct nft_rule { | |
801 | struct list_head list; | |
0768b3b3 | 802 | u64 handle:42, |
0628b123 | 803 | genmask:2, |
0768b3b3 | 804 | dlen:12, |
86f1ec32 | 805 | udata:1; |
96518518 PM |
806 | unsigned char data[] |
807 | __attribute__((aligned(__alignof__(struct nft_expr)))); | |
808 | }; | |
809 | ||
810 | static inline struct nft_expr *nft_expr_first(const struct nft_rule *rule) | |
811 | { | |
812 | return (struct nft_expr *)&rule->data[0]; | |
813 | } | |
814 | ||
815 | static inline struct nft_expr *nft_expr_next(const struct nft_expr *expr) | |
816 | { | |
817 | return ((void *)expr) + expr->ops->size; | |
818 | } | |
819 | ||
820 | static inline struct nft_expr *nft_expr_last(const struct nft_rule *rule) | |
821 | { | |
822 | return (struct nft_expr *)&rule->data[rule->dlen]; | |
823 | } | |
824 | ||
86f1ec32 | 825 | static inline struct nft_userdata *nft_userdata(const struct nft_rule *rule) |
0768b3b3 PNA |
826 | { |
827 | return (void *)&rule->data[rule->dlen]; | |
828 | } | |
829 | ||
96518518 PM |
830 | /* |
831 | * The last pointer isn't really necessary, but the compiler isn't able to | |
832 | * determine that the result of nft_expr_last() is always the same since it | |
833 | * can't assume that the dlen value wasn't changed within calls in the loop. | |
834 | */ | |
835 | #define nft_rule_for_each_expr(expr, last, rule) \ | |
836 | for ((expr) = nft_expr_first(rule), (last) = nft_expr_last(rule); \ | |
837 | (expr) != (last); \ | |
838 | (expr) = nft_expr_next(expr)) | |
839 | ||
840 | enum nft_chain_flags { | |
841 | NFT_BASE_CHAIN = 0x1, | |
96518518 PM |
842 | }; |
843 | ||
844 | /** | |
845 | * struct nft_chain - nf_tables chain | |
846 | * | |
847 | * @rules: list of rules in the chain | |
848 | * @list: used internally | |
b5bc89bf | 849 | * @table: table that this chain belongs to |
96518518 | 850 | * @handle: chain handle |
96518518 PM |
851 | * @use: number of jump references to this chain |
852 | * @level: length of longest path to this chain | |
a0a7379e | 853 | * @flags: bitmask of enum nft_chain_flags |
96518518 PM |
854 | * @name: name of the chain |
855 | */ | |
856 | struct nft_chain { | |
857 | struct list_head rules; | |
858 | struct list_head list; | |
b5bc89bf | 859 | struct nft_table *table; |
96518518 | 860 | u64 handle; |
a0a7379e | 861 | u32 use; |
96518518 | 862 | u16 level; |
664b0f8c PNA |
863 | u8 flags:6, |
864 | genmask:2; | |
b7263e07 | 865 | char *name; |
96518518 PM |
866 | }; |
867 | ||
9370761c PNA |
868 | enum nft_chain_type { |
869 | NFT_CHAIN_T_DEFAULT = 0, | |
870 | NFT_CHAIN_T_ROUTE, | |
871 | NFT_CHAIN_T_NAT, | |
872 | NFT_CHAIN_T_MAX | |
873 | }; | |
874 | ||
1a1e1a12 PM |
875 | /** |
876 | * struct nf_chain_type - nf_tables chain type info | |
877 | * | |
878 | * @name: name of the type | |
879 | * @type: numeric identifier | |
880 | * @family: address family | |
881 | * @owner: module owner | |
882 | * @hook_mask: mask of valid hooks | |
c2f9eafe | 883 | * @hooks: array of hook functions |
1a1e1a12 PM |
884 | */ |
885 | struct nf_chain_type { | |
886 | const char *name; | |
887 | enum nft_chain_type type; | |
888 | int family; | |
889 | struct module *owner; | |
890 | unsigned int hook_mask; | |
891 | nf_hookfn *hooks[NF_MAX_HOOKS]; | |
892 | }; | |
893 | ||
7210e4e3 PNA |
894 | int nft_chain_validate_dependency(const struct nft_chain *chain, |
895 | enum nft_chain_type type); | |
75e8d06d PNA |
896 | int nft_chain_validate_hooks(const struct nft_chain *chain, |
897 | unsigned int hook_flags); | |
7210e4e3 | 898 | |
0ca743a5 | 899 | struct nft_stats { |
ce355e20 ED |
900 | u64 bytes; |
901 | u64 pkts; | |
902 | struct u64_stats_sync syncp; | |
0ca743a5 PNA |
903 | }; |
904 | ||
96518518 PM |
905 | /** |
906 | * struct nft_base_chain - nf_tables base chain | |
907 | * | |
908 | * @ops: netfilter hook ops | |
9370761c | 909 | * @type: chain type |
0ca743a5 PNA |
910 | * @policy: default policy |
911 | * @stats: per-cpu chain stats | |
96518518 | 912 | * @chain: the chain |
2cbce139 | 913 | * @dev_name: device name that this base chain is attached to (if any) |
96518518 PM |
914 | */ |
915 | struct nft_base_chain { | |
c974a3a3 | 916 | struct nf_hook_ops ops; |
2a37d755 | 917 | const struct nf_chain_type *type; |
0ca743a5 | 918 | u8 policy; |
835b8033 | 919 | u8 flags; |
0ca743a5 | 920 | struct nft_stats __percpu *stats; |
96518518 | 921 | struct nft_chain chain; |
2cbce139 | 922 | char dev_name[IFNAMSIZ]; |
96518518 PM |
923 | }; |
924 | ||
925 | static inline struct nft_base_chain *nft_base_chain(const struct nft_chain *chain) | |
926 | { | |
927 | return container_of(chain, struct nft_base_chain, chain); | |
928 | } | |
929 | ||
f323d954 PNA |
930 | static inline bool nft_is_base_chain(const struct nft_chain *chain) |
931 | { | |
932 | return chain->flags & NFT_BASE_CHAIN; | |
933 | } | |
934 | ||
5ebe0b0e | 935 | int __nft_release_basechain(struct nft_ctx *ctx); |
835b8033 | 936 | |
06198b34 | 937 | unsigned int nft_do_chain(struct nft_pktinfo *pkt, void *priv); |
96518518 | 938 | |
96518518 PM |
939 | /** |
940 | * struct nft_table - nf_tables table | |
941 | * | |
942 | * @list: used internally | |
943 | * @chains: chains in the table | |
944 | * @sets: sets in the table | |
e5009240 | 945 | * @objects: stateful objects in the table |
96518518 PM |
946 | * @hgenerator: handle generator state |
947 | * @use: number of chain references to this table | |
948 | * @flags: table flag (see enum nft_table_flags) | |
f2a6d766 | 949 | * @genmask: generation mask |
96518518 PM |
950 | * @name: name of the table |
951 | */ | |
952 | struct nft_table { | |
953 | struct list_head list; | |
954 | struct list_head chains; | |
955 | struct list_head sets; | |
e5009240 | 956 | struct list_head objects; |
96518518 PM |
957 | u64 hgenerator; |
958 | u32 use; | |
f2a6d766 PNA |
959 | u16 flags:14, |
960 | genmask:2; | |
e46abbcc | 961 | char *name; |
ebddf1a8 PNA |
962 | }; |
963 | ||
964 | enum nft_af_flags { | |
965 | NFT_AF_NEEDS_DEV = (1 << 0), | |
96518518 PM |
966 | }; |
967 | ||
968 | /** | |
969 | * struct nft_af_info - nf_tables address family info | |
970 | * | |
971 | * @list: used internally | |
972 | * @family: address family | |
973 | * @nhooks: number of hooks in this family | |
974 | * @owner: module owner | |
975 | * @tables: used internally | |
ebddf1a8 | 976 | * @flags: family flags |
96518518 PM |
977 | */ |
978 | struct nft_af_info { | |
979 | struct list_head list; | |
980 | int family; | |
981 | unsigned int nhooks; | |
982 | struct module *owner; | |
983 | struct list_head tables; | |
ebddf1a8 | 984 | u32 flags; |
96518518 PM |
985 | }; |
986 | ||
5eccdfaa | 987 | int nft_register_afinfo(struct net *, struct nft_af_info *); |
df05ef87 | 988 | void nft_unregister_afinfo(struct net *, struct nft_af_info *); |
96518518 | 989 | |
2a37d755 PM |
990 | int nft_register_chain_type(const struct nf_chain_type *); |
991 | void nft_unregister_chain_type(const struct nf_chain_type *); | |
96518518 | 992 | |
5eccdfaa JP |
993 | int nft_register_expr(struct nft_expr_type *); |
994 | void nft_unregister_expr(struct nft_expr_type *); | |
96518518 | 995 | |
33d5a7b1 FW |
996 | int nft_verdict_dump(struct sk_buff *skb, int type, |
997 | const struct nft_verdict *v); | |
998 | ||
e5009240 PNA |
999 | /** |
1000 | * struct nft_object - nf_tables stateful object | |
1001 | * | |
1002 | * @list: table stateful object list node | |
18965317 | 1003 | * @table: table this object belongs to |
e5009240 PNA |
1004 | * @name: name of this stateful object |
1005 | * @genmask: generation mask | |
1006 | * @use: number of references to this stateful object | |
1007 | * @data: object data, layout depends on type | |
dfc46034 PBG |
1008 | * @ops: object operations |
1009 | * @data: pointer to object data | |
e5009240 PNA |
1010 | */ |
1011 | struct nft_object { | |
1012 | struct list_head list; | |
61509575 | 1013 | char *name; |
18965317 | 1014 | struct nft_table *table; |
e5009240 PNA |
1015 | u32 genmask:2, |
1016 | use:30; | |
1017 | /* runtime data below here */ | |
dfc46034 | 1018 | const struct nft_object_ops *ops ____cacheline_aligned; |
e5009240 PNA |
1019 | unsigned char data[] |
1020 | __attribute__((aligned(__alignof__(u64)))); | |
1021 | }; | |
1022 | ||
1023 | static inline void *nft_obj_data(const struct nft_object *obj) | |
1024 | { | |
1025 | return (void *)obj->data; | |
1026 | } | |
1027 | ||
1028 | #define nft_expr_obj(expr) *((struct nft_object **)nft_expr_priv(expr)) | |
1029 | ||
1030 | struct nft_object *nf_tables_obj_lookup(const struct nft_table *table, | |
1031 | const struct nlattr *nla, u32 objtype, | |
1032 | u8 genmask); | |
1033 | ||
25e94a99 PNA |
1034 | void nft_obj_notify(struct net *net, struct nft_table *table, |
1035 | struct nft_object *obj, u32 portid, u32 seq, | |
1036 | int event, int family, int report, gfp_t gfp); | |
2599e989 | 1037 | |
e5009240 PNA |
1038 | /** |
1039 | * struct nft_object_type - stateful object type | |
1040 | * | |
dfc46034 PBG |
1041 | * @select_ops: function to select nft_object_ops |
1042 | * @ops: default ops, used when no select_ops functions is present | |
e5009240 PNA |
1043 | * @list: list node in list of object types |
1044 | * @type: stateful object numeric type | |
e5009240 PNA |
1045 | * @owner: module owner |
1046 | * @maxattr: maximum netlink attribute | |
1047 | * @policy: netlink attribute policy | |
dfc46034 PBG |
1048 | */ |
1049 | struct nft_object_type { | |
1050 | const struct nft_object_ops *(*select_ops)(const struct nft_ctx *, | |
1051 | const struct nlattr * const tb[]); | |
1052 | const struct nft_object_ops *ops; | |
1053 | struct list_head list; | |
1054 | u32 type; | |
1055 | unsigned int maxattr; | |
1056 | struct module *owner; | |
1057 | const struct nla_policy *policy; | |
1058 | }; | |
1059 | ||
1060 | /** | |
1061 | * struct nft_object_ops - stateful object operations | |
1062 | * | |
1063 | * @eval: stateful object evaluation function | |
1064 | * @size: stateful object size | |
e5009240 PNA |
1065 | * @init: initialize object from netlink attributes |
1066 | * @destroy: release existing stateful object | |
1067 | * @dump: netlink dump stateful object | |
1068 | */ | |
dfc46034 | 1069 | struct nft_object_ops { |
e5009240 PNA |
1070 | void (*eval)(struct nft_object *obj, |
1071 | struct nft_regs *regs, | |
1072 | const struct nft_pktinfo *pkt); | |
e5009240 | 1073 | unsigned int size; |
84fba055 FW |
1074 | int (*init)(const struct nft_ctx *ctx, |
1075 | const struct nlattr *const tb[], | |
e5009240 PNA |
1076 | struct nft_object *obj); |
1077 | void (*destroy)(struct nft_object *obj); | |
1078 | int (*dump)(struct sk_buff *skb, | |
43da04a5 PNA |
1079 | struct nft_object *obj, |
1080 | bool reset); | |
dfc46034 | 1081 | const struct nft_object_type *type; |
e5009240 PNA |
1082 | }; |
1083 | ||
1084 | int nft_register_obj(struct nft_object_type *obj_type); | |
1085 | void nft_unregister_obj(struct nft_object_type *obj_type); | |
1086 | ||
33d5a7b1 FW |
1087 | /** |
1088 | * struct nft_traceinfo - nft tracing information and state | |
1089 | * | |
1090 | * @pkt: pktinfo currently processed | |
1091 | * @basechain: base chain currently processed | |
1092 | * @chain: chain currently processed | |
1093 | * @rule: rule that was evaluated | |
1094 | * @verdict: verdict given by rule | |
1095 | * @type: event type (enum nft_trace_types) | |
1096 | * @packet_dumped: packet headers sent in a previous traceinfo message | |
1097 | * @trace: other struct members are initialised | |
1098 | */ | |
1099 | struct nft_traceinfo { | |
1100 | const struct nft_pktinfo *pkt; | |
1101 | const struct nft_base_chain *basechain; | |
1102 | const struct nft_chain *chain; | |
1103 | const struct nft_rule *rule; | |
1104 | const struct nft_verdict *verdict; | |
1105 | enum nft_trace_types type; | |
1106 | bool packet_dumped; | |
1107 | bool trace; | |
1108 | }; | |
1109 | ||
1110 | void nft_trace_init(struct nft_traceinfo *info, const struct nft_pktinfo *pkt, | |
1111 | const struct nft_verdict *verdict, | |
1112 | const struct nft_chain *basechain); | |
1113 | ||
1114 | void nft_trace_notify(struct nft_traceinfo *info); | |
1115 | ||
96518518 PM |
1116 | #define MODULE_ALIAS_NFT_FAMILY(family) \ |
1117 | MODULE_ALIAS("nft-afinfo-" __stringify(family)) | |
1118 | ||
9370761c PNA |
1119 | #define MODULE_ALIAS_NFT_CHAIN(family, name) \ |
1120 | MODULE_ALIAS("nft-chain-" __stringify(family) "-" name) | |
96518518 | 1121 | |
64d46806 PM |
1122 | #define MODULE_ALIAS_NFT_AF_EXPR(family, name) \ |
1123 | MODULE_ALIAS("nft-expr-" __stringify(family) "-" name) | |
1124 | ||
96518518 PM |
1125 | #define MODULE_ALIAS_NFT_EXPR(name) \ |
1126 | MODULE_ALIAS("nft-expr-" name) | |
1127 | ||
20a69341 PM |
1128 | #define MODULE_ALIAS_NFT_SET() \ |
1129 | MODULE_ALIAS("nft-set") | |
1130 | ||
e5009240 PNA |
1131 | #define MODULE_ALIAS_NFT_OBJ(type) \ |
1132 | MODULE_ALIAS("nft-obj-" __stringify(type)) | |
1133 | ||
ea4bd995 PM |
1134 | /* |
1135 | * The gencursor defines two generations, the currently active and the | |
1136 | * next one. Objects contain a bitmask of 2 bits specifying the generations | |
1137 | * they're active in. A set bit means they're inactive in the generation | |
1138 | * represented by that bit. | |
1139 | * | |
1140 | * New objects start out as inactive in the current and active in the | |
1141 | * next generation. When committing the ruleset the bitmask is cleared, | |
1142 | * meaning they're active in all generations. When removing an object, | |
1143 | * it is set inactive in the next generation. After committing the ruleset, | |
1144 | * the objects are removed. | |
1145 | */ | |
1146 | static inline unsigned int nft_gencursor_next(const struct net *net) | |
1147 | { | |
1148 | return net->nft.gencursor + 1 == 1 ? 1 : 0; | |
1149 | } | |
1150 | ||
1151 | static inline u8 nft_genmask_next(const struct net *net) | |
1152 | { | |
1153 | return 1 << nft_gencursor_next(net); | |
1154 | } | |
1155 | ||
1156 | static inline u8 nft_genmask_cur(const struct net *net) | |
1157 | { | |
14cd5d4a MR |
1158 | /* Use READ_ONCE() to prevent refetching the value for atomicity */ |
1159 | return 1 << READ_ONCE(net->nft.gencursor); | |
ea4bd995 PM |
1160 | } |
1161 | ||
22fe54d5 PM |
1162 | #define NFT_GENMASK_ANY ((1 << 0) | (1 << 1)) |
1163 | ||
889f7ee7 PNA |
1164 | /* |
1165 | * Generic transaction helpers | |
1166 | */ | |
1167 | ||
1168 | /* Check if this object is currently active. */ | |
1169 | #define nft_is_active(__net, __obj) \ | |
1170 | (((__obj)->genmask & nft_genmask_cur(__net)) == 0) | |
1171 | ||
1172 | /* Check if this object is active in the next generation. */ | |
1173 | #define nft_is_active_next(__net, __obj) \ | |
1174 | (((__obj)->genmask & nft_genmask_next(__net)) == 0) | |
1175 | ||
1176 | /* This object becomes active in the next generation. */ | |
1177 | #define nft_activate_next(__net, __obj) \ | |
1178 | (__obj)->genmask = nft_genmask_cur(__net) | |
1179 | ||
1180 | /* This object becomes inactive in the next generation. */ | |
1181 | #define nft_deactivate_next(__net, __obj) \ | |
1182 | (__obj)->genmask = nft_genmask_next(__net) | |
1183 | ||
1184 | /* After committing the ruleset, clear the stale generation bit. */ | |
1185 | #define nft_clear(__net, __obj) \ | |
1186 | (__obj)->genmask &= ~nft_genmask_next(__net) | |
f2a6d766 PNA |
1187 | #define nft_active_genmask(__obj, __genmask) \ |
1188 | !((__obj)->genmask & __genmask) | |
889f7ee7 | 1189 | |
cc02e457 PM |
1190 | /* |
1191 | * Set element transaction helpers | |
1192 | */ | |
1193 | ||
1194 | static inline bool nft_set_elem_active(const struct nft_set_ext *ext, | |
1195 | u8 genmask) | |
1196 | { | |
1197 | return !(ext->genmask & genmask); | |
1198 | } | |
1199 | ||
42a55769 PNA |
1200 | static inline void nft_set_elem_change_active(const struct net *net, |
1201 | const struct nft_set *set, | |
cc02e457 PM |
1202 | struct nft_set_ext *ext) |
1203 | { | |
42a55769 | 1204 | ext->genmask ^= nft_genmask_next(net); |
cc02e457 PM |
1205 | } |
1206 | ||
69086658 PM |
1207 | /* |
1208 | * We use a free bit in the genmask field to indicate the element | |
1209 | * is busy, meaning it is currently being processed either by | |
1210 | * the netlink API or GC. | |
1211 | * | |
1212 | * Even though the genmask is only a single byte wide, this works | |
1213 | * because the extension structure if fully constant once initialized, | |
1214 | * so there are no non-atomic write accesses unless it is already | |
1215 | * marked busy. | |
1216 | */ | |
1217 | #define NFT_SET_ELEM_BUSY_MASK (1 << 2) | |
1218 | ||
1219 | #if defined(__LITTLE_ENDIAN_BITFIELD) | |
1220 | #define NFT_SET_ELEM_BUSY_BIT 2 | |
1221 | #elif defined(__BIG_ENDIAN_BITFIELD) | |
1222 | #define NFT_SET_ELEM_BUSY_BIT (BITS_PER_LONG - BITS_PER_BYTE + 2) | |
1223 | #else | |
1224 | #error | |
1225 | #endif | |
1226 | ||
1227 | static inline int nft_set_elem_mark_busy(struct nft_set_ext *ext) | |
1228 | { | |
1229 | unsigned long *word = (unsigned long *)ext; | |
1230 | ||
1231 | BUILD_BUG_ON(offsetof(struct nft_set_ext, genmask) != 0); | |
1232 | return test_and_set_bit(NFT_SET_ELEM_BUSY_BIT, word); | |
1233 | } | |
1234 | ||
1235 | static inline void nft_set_elem_clear_busy(struct nft_set_ext *ext) | |
1236 | { | |
1237 | unsigned long *word = (unsigned long *)ext; | |
1238 | ||
1239 | clear_bit(NFT_SET_ELEM_BUSY_BIT, word); | |
1240 | } | |
1241 | ||
1a1e1a12 PM |
1242 | /** |
1243 | * struct nft_trans - nf_tables object update in transaction | |
1244 | * | |
1245 | * @list: used internally | |
1246 | * @msg_type: message type | |
1247 | * @ctx: transaction context | |
1248 | * @data: internal information related to the transaction | |
1249 | */ | |
1250 | struct nft_trans { | |
1251 | struct list_head list; | |
1252 | int msg_type; | |
1253 | struct nft_ctx ctx; | |
1254 | char data[0]; | |
1255 | }; | |
1256 | ||
1257 | struct nft_trans_rule { | |
1258 | struct nft_rule *rule; | |
1a94e38d | 1259 | u32 rule_id; |
1a1e1a12 PM |
1260 | }; |
1261 | ||
1262 | #define nft_trans_rule(trans) \ | |
1263 | (((struct nft_trans_rule *)trans->data)->rule) | |
1a94e38d PNA |
1264 | #define nft_trans_rule_id(trans) \ |
1265 | (((struct nft_trans_rule *)trans->data)->rule_id) | |
1a1e1a12 PM |
1266 | |
1267 | struct nft_trans_set { | |
1268 | struct nft_set *set; | |
1269 | u32 set_id; | |
1270 | }; | |
1271 | ||
1272 | #define nft_trans_set(trans) \ | |
1273 | (((struct nft_trans_set *)trans->data)->set) | |
1274 | #define nft_trans_set_id(trans) \ | |
1275 | (((struct nft_trans_set *)trans->data)->set_id) | |
1276 | ||
1277 | struct nft_trans_chain { | |
1278 | bool update; | |
b7263e07 | 1279 | char *name; |
1a1e1a12 PM |
1280 | struct nft_stats __percpu *stats; |
1281 | u8 policy; | |
1282 | }; | |
1283 | ||
1284 | #define nft_trans_chain_update(trans) \ | |
1285 | (((struct nft_trans_chain *)trans->data)->update) | |
1286 | #define nft_trans_chain_name(trans) \ | |
1287 | (((struct nft_trans_chain *)trans->data)->name) | |
1288 | #define nft_trans_chain_stats(trans) \ | |
1289 | (((struct nft_trans_chain *)trans->data)->stats) | |
1290 | #define nft_trans_chain_policy(trans) \ | |
1291 | (((struct nft_trans_chain *)trans->data)->policy) | |
1292 | ||
1293 | struct nft_trans_table { | |
1294 | bool update; | |
1295 | bool enable; | |
1296 | }; | |
1297 | ||
1298 | #define nft_trans_table_update(trans) \ | |
1299 | (((struct nft_trans_table *)trans->data)->update) | |
1300 | #define nft_trans_table_enable(trans) \ | |
1301 | (((struct nft_trans_table *)trans->data)->enable) | |
1302 | ||
1303 | struct nft_trans_elem { | |
1304 | struct nft_set *set; | |
1305 | struct nft_set_elem elem; | |
1306 | }; | |
1307 | ||
1308 | #define nft_trans_elem_set(trans) \ | |
1309 | (((struct nft_trans_elem *)trans->data)->set) | |
1310 | #define nft_trans_elem(trans) \ | |
1311 | (((struct nft_trans_elem *)trans->data)->elem) | |
1312 | ||
e5009240 PNA |
1313 | struct nft_trans_obj { |
1314 | struct nft_object *obj; | |
1315 | }; | |
1316 | ||
1317 | #define nft_trans_obj(trans) \ | |
1318 | (((struct nft_trans_obj *)trans->data)->obj) | |
1319 | ||
96518518 | 1320 | #endif /* _NET_NF_TABLES_H */ |