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