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