Commit | Line | Data |
---|---|---|
96518518 PM |
1 | #ifndef _NET_NF_TABLES_H |
2 | #define _NET_NF_TABLES_H | |
3 | ||
4 | #include <linux/list.h> | |
5 | #include <linux/netfilter.h> | |
67a8fc27 | 6 | #include <linux/netfilter/nfnetlink.h> |
0ca743a5 | 7 | #include <linux/netfilter/x_tables.h> |
96518518 | 8 | #include <linux/netfilter/nf_tables.h> |
ce355e20 | 9 | #include <linux/u64_stats_sync.h> |
96518518 PM |
10 | #include <net/netlink.h> |
11 | ||
20a69341 PM |
12 | #define NFT_JUMP_STACK_SIZE 16 |
13 | ||
96518518 PM |
14 | struct nft_pktinfo { |
15 | struct sk_buff *skb; | |
16 | const struct net_device *in; | |
17 | const struct net_device *out; | |
c9484874 | 18 | const struct nf_hook_ops *ops; |
96518518 PM |
19 | u8 nhoff; |
20 | u8 thoff; | |
4566bf27 | 21 | u8 tprot; |
0ca743a5 PNA |
22 | /* for x_tables compatibility */ |
23 | struct xt_action_param xt; | |
96518518 PM |
24 | }; |
25 | ||
0ca743a5 PNA |
26 | static inline void nft_set_pktinfo(struct nft_pktinfo *pkt, |
27 | const struct nf_hook_ops *ops, | |
28 | struct sk_buff *skb, | |
29 | const struct net_device *in, | |
30 | const struct net_device *out) | |
31 | { | |
32 | pkt->skb = skb; | |
33 | pkt->in = pkt->xt.in = in; | |
34 | pkt->out = pkt->xt.out = out; | |
c9484874 PM |
35 | pkt->ops = ops; |
36 | pkt->xt.hooknum = ops->hooknum; | |
0ca743a5 PNA |
37 | pkt->xt.family = ops->pf; |
38 | } | |
39 | ||
96518518 PM |
40 | struct nft_data { |
41 | union { | |
42 | u32 data[4]; | |
43 | struct { | |
44 | u32 verdict; | |
45 | struct nft_chain *chain; | |
46 | }; | |
47 | }; | |
48 | } __attribute__((aligned(__alignof__(u64)))); | |
49 | ||
50 | static inline int nft_data_cmp(const struct nft_data *d1, | |
51 | const struct nft_data *d2, | |
52 | unsigned int len) | |
53 | { | |
54 | return memcmp(d1->data, d2->data, len); | |
55 | } | |
56 | ||
57 | static inline void nft_data_copy(struct nft_data *dst, | |
58 | const struct nft_data *src) | |
59 | { | |
60 | BUILD_BUG_ON(__alignof__(*dst) != __alignof__(u64)); | |
61 | *(u64 *)&dst->data[0] = *(u64 *)&src->data[0]; | |
62 | *(u64 *)&dst->data[2] = *(u64 *)&src->data[2]; | |
63 | } | |
64 | ||
65 | static inline void nft_data_debug(const struct nft_data *data) | |
66 | { | |
67 | pr_debug("data[0]=%x data[1]=%x data[2]=%x data[3]=%x\n", | |
68 | data->data[0], data->data[1], | |
69 | data->data[2], data->data[3]); | |
70 | } | |
71 | ||
72 | /** | |
20a69341 | 73 | * struct nft_ctx - nf_tables rule/set context |
96518518 | 74 | * |
99633ab2 | 75 | * @net: net namespace |
96518518 PM |
76 | * @afi: address family info |
77 | * @table: the table the chain is contained in | |
78 | * @chain: the chain the rule is contained in | |
0ca743a5 | 79 | * @nla: netlink attributes |
128ad332 PNA |
80 | * @portid: netlink portID of the original message |
81 | * @seq: netlink sequence number | |
82 | * @report: notify via unicast netlink message | |
96518518 PM |
83 | */ |
84 | struct nft_ctx { | |
99633ab2 | 85 | struct net *net; |
7c95f6d8 PNA |
86 | struct nft_af_info *afi; |
87 | struct nft_table *table; | |
88 | struct nft_chain *chain; | |
0ca743a5 | 89 | const struct nlattr * const *nla; |
128ad332 PNA |
90 | u32 portid; |
91 | u32 seq; | |
92 | bool report; | |
96518518 PM |
93 | }; |
94 | ||
96518518 PM |
95 | struct nft_data_desc { |
96 | enum nft_data_types type; | |
97 | unsigned int len; | |
98 | }; | |
99 | ||
5eccdfaa JP |
100 | int nft_data_init(const struct nft_ctx *ctx, struct nft_data *data, |
101 | struct nft_data_desc *desc, const struct nlattr *nla); | |
102 | void nft_data_uninit(const struct nft_data *data, enum nft_data_types type); | |
103 | int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data, | |
104 | enum nft_data_types type, unsigned int len); | |
96518518 PM |
105 | |
106 | static inline enum nft_data_types nft_dreg_to_type(enum nft_registers reg) | |
107 | { | |
108 | return reg == NFT_REG_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE; | |
109 | } | |
110 | ||
20a69341 PM |
111 | static inline enum nft_registers nft_type_to_reg(enum nft_data_types type) |
112 | { | |
113 | return type == NFT_DATA_VERDICT ? NFT_REG_VERDICT : NFT_REG_1; | |
114 | } | |
115 | ||
5eccdfaa JP |
116 | int nft_validate_input_register(enum nft_registers reg); |
117 | int nft_validate_output_register(enum nft_registers reg); | |
118 | int nft_validate_data_load(const struct nft_ctx *ctx, enum nft_registers reg, | |
119 | const struct nft_data *data, | |
120 | enum nft_data_types type); | |
96518518 | 121 | |
86f1ec32 PM |
122 | |
123 | /** | |
124 | * struct nft_userdata - user defined data associated with an object | |
125 | * | |
126 | * @len: length of the data | |
127 | * @data: content | |
128 | * | |
129 | * The presence of user data is indicated in an object specific fashion, | |
130 | * so a length of zero can't occur and the value "len" indicates data | |
131 | * of length len + 1. | |
132 | */ | |
133 | struct nft_userdata { | |
134 | u8 len; | |
135 | unsigned char data[0]; | |
136 | }; | |
137 | ||
20a69341 PM |
138 | /** |
139 | * struct nft_set_elem - generic representation of set elements | |
140 | * | |
20a69341 | 141 | * @key: element key |
fe2811eb | 142 | * @priv: element private data and extensions |
20a69341 PM |
143 | */ |
144 | struct nft_set_elem { | |
20a69341 | 145 | struct nft_data key; |
fe2811eb | 146 | void *priv; |
20a69341 PM |
147 | }; |
148 | ||
149 | struct nft_set; | |
150 | struct nft_set_iter { | |
151 | unsigned int count; | |
152 | unsigned int skip; | |
153 | int err; | |
154 | int (*fn)(const struct nft_ctx *ctx, | |
155 | const struct nft_set *set, | |
156 | const struct nft_set_iter *iter, | |
157 | const struct nft_set_elem *elem); | |
158 | }; | |
159 | ||
c50b960c PM |
160 | /** |
161 | * struct nft_set_desc - description of set elements | |
162 | * | |
163 | * @klen: key length | |
164 | * @dlen: data length | |
165 | * @size: number of set elements | |
166 | */ | |
167 | struct nft_set_desc { | |
168 | unsigned int klen; | |
169 | unsigned int dlen; | |
170 | unsigned int size; | |
171 | }; | |
172 | ||
173 | /** | |
174 | * enum nft_set_class - performance class | |
175 | * | |
176 | * @NFT_LOOKUP_O_1: constant, O(1) | |
177 | * @NFT_LOOKUP_O_LOG_N: logarithmic, O(log N) | |
178 | * @NFT_LOOKUP_O_N: linear, O(N) | |
179 | */ | |
180 | enum nft_set_class { | |
181 | NFT_SET_CLASS_O_1, | |
182 | NFT_SET_CLASS_O_LOG_N, | |
183 | NFT_SET_CLASS_O_N, | |
184 | }; | |
185 | ||
186 | /** | |
187 | * struct nft_set_estimate - estimation of memory and performance | |
188 | * characteristics | |
189 | * | |
190 | * @size: required memory | |
191 | * @class: lookup performance class | |
192 | */ | |
193 | struct nft_set_estimate { | |
194 | unsigned int size; | |
195 | enum nft_set_class class; | |
196 | }; | |
197 | ||
b2832dd6 PM |
198 | struct nft_set_ext; |
199 | ||
20a69341 PM |
200 | /** |
201 | * struct nft_set_ops - nf_tables set operations | |
202 | * | |
203 | * @lookup: look up an element within the set | |
204 | * @insert: insert new element into set | |
cc02e457 PM |
205 | * @activate: activate new element in the next generation |
206 | * @deactivate: deactivate element in the next generation | |
20a69341 PM |
207 | * @remove: remove element from set |
208 | * @walk: iterate over all set elemeennts | |
209 | * @privsize: function to return size of set private data | |
210 | * @init: initialize private data of new set instance | |
211 | * @destroy: destroy private data of set instance | |
212 | * @list: nf_tables_set_ops list node | |
213 | * @owner: module reference | |
fe2811eb | 214 | * @elemsize: element private size |
20a69341 PM |
215 | * @features: features supported by the implementation |
216 | */ | |
217 | struct nft_set_ops { | |
218 | bool (*lookup)(const struct nft_set *set, | |
219 | const struct nft_data *key, | |
b2832dd6 | 220 | const struct nft_set_ext **ext); |
20a69341 PM |
221 | int (*insert)(const struct nft_set *set, |
222 | const struct nft_set_elem *elem); | |
cc02e457 PM |
223 | void (*activate)(const struct nft_set *set, |
224 | const struct nft_set_elem *elem); | |
225 | void * (*deactivate)(const struct nft_set *set, | |
226 | const struct nft_set_elem *elem); | |
20a69341 PM |
227 | void (*remove)(const struct nft_set *set, |
228 | const struct nft_set_elem *elem); | |
229 | void (*walk)(const struct nft_ctx *ctx, | |
230 | const struct nft_set *set, | |
231 | struct nft_set_iter *iter); | |
232 | ||
233 | unsigned int (*privsize)(const struct nlattr * const nla[]); | |
c50b960c PM |
234 | bool (*estimate)(const struct nft_set_desc *desc, |
235 | u32 features, | |
236 | struct nft_set_estimate *est); | |
20a69341 | 237 | int (*init)(const struct nft_set *set, |
c50b960c | 238 | const struct nft_set_desc *desc, |
20a69341 PM |
239 | const struct nlattr * const nla[]); |
240 | void (*destroy)(const struct nft_set *set); | |
241 | ||
242 | struct list_head list; | |
243 | struct module *owner; | |
fe2811eb | 244 | unsigned int elemsize; |
20a69341 PM |
245 | u32 features; |
246 | }; | |
247 | ||
5eccdfaa JP |
248 | int nft_register_set(struct nft_set_ops *ops); |
249 | void nft_unregister_set(struct nft_set_ops *ops); | |
20a69341 PM |
250 | |
251 | /** | |
252 | * struct nft_set - nf_tables set instance | |
253 | * | |
254 | * @list: table set list node | |
255 | * @bindings: list of set bindings | |
256 | * @name: name of the set | |
257 | * @ktype: key type (numeric type defined by userspace, not used in the kernel) | |
258 | * @dtype: data type (verdict or numeric type defined by userspace) | |
c50b960c PM |
259 | * @size: maximum set size |
260 | * @nelems: number of elements | |
761da293 PM |
261 | * @timeout: default timeout value in msecs |
262 | * @gc_int: garbage collection interval in msecs | |
9363dc4b | 263 | * @policy: set parameterization (see enum nft_set_policies) |
20a69341 | 264 | * @ops: set ops |
cc02e457 | 265 | * @pnet: network namespace |
20a69341 PM |
266 | * @flags: set flags |
267 | * @klen: key length | |
268 | * @dlen: data length | |
269 | * @data: private set data | |
270 | */ | |
271 | struct nft_set { | |
272 | struct list_head list; | |
273 | struct list_head bindings; | |
274 | char name[IFNAMSIZ]; | |
275 | u32 ktype; | |
276 | u32 dtype; | |
c50b960c PM |
277 | u32 size; |
278 | u32 nelems; | |
761da293 PM |
279 | u64 timeout; |
280 | u32 gc_int; | |
9363dc4b | 281 | u16 policy; |
20a69341 PM |
282 | /* runtime data below here */ |
283 | const struct nft_set_ops *ops ____cacheline_aligned; | |
cc02e457 | 284 | possible_net_t pnet; |
20a69341 PM |
285 | u16 flags; |
286 | u8 klen; | |
287 | u8 dlen; | |
288 | unsigned char data[] | |
289 | __attribute__((aligned(__alignof__(u64)))); | |
290 | }; | |
291 | ||
292 | static inline void *nft_set_priv(const struct nft_set *set) | |
293 | { | |
294 | return (void *)set->data; | |
295 | } | |
296 | ||
5eccdfaa JP |
297 | struct nft_set *nf_tables_set_lookup(const struct nft_table *table, |
298 | const struct nlattr *nla); | |
958bee14 PNA |
299 | struct nft_set *nf_tables_set_lookup_byid(const struct net *net, |
300 | const struct nlattr *nla); | |
20a69341 | 301 | |
761da293 PM |
302 | static inline unsigned long nft_set_gc_interval(const struct nft_set *set) |
303 | { | |
304 | return set->gc_int ? msecs_to_jiffies(set->gc_int) : HZ; | |
305 | } | |
306 | ||
20a69341 PM |
307 | /** |
308 | * struct nft_set_binding - nf_tables set binding | |
309 | * | |
310 | * @list: set bindings list node | |
311 | * @chain: chain containing the rule bound to the set | |
312 | * | |
313 | * A set binding contains all information necessary for validation | |
314 | * of new elements added to a bound set. | |
315 | */ | |
316 | struct nft_set_binding { | |
317 | struct list_head list; | |
318 | const struct nft_chain *chain; | |
319 | }; | |
320 | ||
5eccdfaa JP |
321 | int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set, |
322 | struct nft_set_binding *binding); | |
323 | void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set, | |
324 | struct nft_set_binding *binding); | |
20a69341 | 325 | |
3ac4c07a PM |
326 | /** |
327 | * enum nft_set_extensions - set extension type IDs | |
328 | * | |
329 | * @NFT_SET_EXT_KEY: element key | |
330 | * @NFT_SET_EXT_DATA: mapping data | |
331 | * @NFT_SET_EXT_FLAGS: element flags | |
c3e1b005 PM |
332 | * @NFT_SET_EXT_TIMEOUT: element timeout |
333 | * @NFT_SET_EXT_EXPIRATION: element expiration time | |
3ac4c07a PM |
334 | * @NFT_SET_EXT_NUM: number of extension types |
335 | */ | |
336 | enum nft_set_extensions { | |
337 | NFT_SET_EXT_KEY, | |
338 | NFT_SET_EXT_DATA, | |
339 | NFT_SET_EXT_FLAGS, | |
c3e1b005 PM |
340 | NFT_SET_EXT_TIMEOUT, |
341 | NFT_SET_EXT_EXPIRATION, | |
3ac4c07a PM |
342 | NFT_SET_EXT_NUM |
343 | }; | |
344 | ||
345 | /** | |
346 | * struct nft_set_ext_type - set extension type | |
347 | * | |
348 | * @len: fixed part length of the extension | |
349 | * @align: alignment requirements of the extension | |
350 | */ | |
351 | struct nft_set_ext_type { | |
352 | u8 len; | |
353 | u8 align; | |
354 | }; | |
355 | ||
356 | extern const struct nft_set_ext_type nft_set_ext_types[]; | |
357 | ||
358 | /** | |
359 | * struct nft_set_ext_tmpl - set extension template | |
360 | * | |
361 | * @len: length of extension area | |
362 | * @offset: offsets of individual extension types | |
363 | */ | |
364 | struct nft_set_ext_tmpl { | |
365 | u16 len; | |
366 | u8 offset[NFT_SET_EXT_NUM]; | |
367 | }; | |
368 | ||
369 | /** | |
370 | * struct nft_set_ext - set extensions | |
371 | * | |
cc02e457 | 372 | * @genmask: generation mask |
3ac4c07a PM |
373 | * @offset: offsets of individual extension types |
374 | * @data: beginning of extension data | |
375 | */ | |
376 | struct nft_set_ext { | |
cc02e457 | 377 | u8 genmask; |
3ac4c07a PM |
378 | u8 offset[NFT_SET_EXT_NUM]; |
379 | char data[0]; | |
380 | }; | |
381 | ||
382 | static inline void nft_set_ext_prepare(struct nft_set_ext_tmpl *tmpl) | |
383 | { | |
384 | memset(tmpl, 0, sizeof(*tmpl)); | |
385 | tmpl->len = sizeof(struct nft_set_ext); | |
386 | } | |
387 | ||
388 | static inline void nft_set_ext_add_length(struct nft_set_ext_tmpl *tmpl, u8 id, | |
389 | unsigned int len) | |
390 | { | |
391 | tmpl->len = ALIGN(tmpl->len, nft_set_ext_types[id].align); | |
392 | BUG_ON(tmpl->len > U8_MAX); | |
393 | tmpl->offset[id] = tmpl->len; | |
394 | tmpl->len += nft_set_ext_types[id].len + len; | |
395 | } | |
396 | ||
397 | static inline void nft_set_ext_add(struct nft_set_ext_tmpl *tmpl, u8 id) | |
398 | { | |
399 | nft_set_ext_add_length(tmpl, id, 0); | |
400 | } | |
401 | ||
402 | static inline void nft_set_ext_init(struct nft_set_ext *ext, | |
403 | const struct nft_set_ext_tmpl *tmpl) | |
404 | { | |
405 | memcpy(ext->offset, tmpl->offset, sizeof(ext->offset)); | |
406 | } | |
407 | ||
408 | static inline bool __nft_set_ext_exists(const struct nft_set_ext *ext, u8 id) | |
409 | { | |
410 | return !!ext->offset[id]; | |
411 | } | |
412 | ||
413 | static inline bool nft_set_ext_exists(const struct nft_set_ext *ext, u8 id) | |
414 | { | |
415 | return ext && __nft_set_ext_exists(ext, id); | |
416 | } | |
417 | ||
418 | static inline void *nft_set_ext(const struct nft_set_ext *ext, u8 id) | |
419 | { | |
420 | return (void *)ext + ext->offset[id]; | |
421 | } | |
422 | ||
423 | static inline struct nft_data *nft_set_ext_key(const struct nft_set_ext *ext) | |
424 | { | |
425 | return nft_set_ext(ext, NFT_SET_EXT_KEY); | |
426 | } | |
427 | ||
428 | static inline struct nft_data *nft_set_ext_data(const struct nft_set_ext *ext) | |
429 | { | |
430 | return nft_set_ext(ext, NFT_SET_EXT_DATA); | |
431 | } | |
432 | ||
433 | static inline u8 *nft_set_ext_flags(const struct nft_set_ext *ext) | |
434 | { | |
435 | return nft_set_ext(ext, NFT_SET_EXT_FLAGS); | |
436 | } | |
ef1f7df9 | 437 | |
c3e1b005 PM |
438 | static inline u64 *nft_set_ext_timeout(const struct nft_set_ext *ext) |
439 | { | |
440 | return nft_set_ext(ext, NFT_SET_EXT_TIMEOUT); | |
441 | } | |
442 | ||
443 | static inline unsigned long *nft_set_ext_expiration(const struct nft_set_ext *ext) | |
444 | { | |
445 | return nft_set_ext(ext, NFT_SET_EXT_EXPIRATION); | |
446 | } | |
447 | ||
448 | static inline bool nft_set_elem_expired(const struct nft_set_ext *ext) | |
449 | { | |
450 | return nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION) && | |
451 | time_is_before_eq_jiffies(*nft_set_ext_expiration(ext)); | |
452 | } | |
453 | ||
fe2811eb PM |
454 | static inline struct nft_set_ext *nft_set_elem_ext(const struct nft_set *set, |
455 | void *elem) | |
456 | { | |
457 | return elem + set->ops->elemsize; | |
458 | } | |
459 | ||
61edafbb PM |
460 | void nft_set_elem_destroy(const struct nft_set *set, void *elem); |
461 | ||
96518518 | 462 | /** |
ef1f7df9 | 463 | * struct nft_expr_type - nf_tables expression type |
96518518 | 464 | * |
ef1f7df9 PM |
465 | * @select_ops: function to select nft_expr_ops |
466 | * @ops: default ops, used when no select_ops functions is present | |
96518518 PM |
467 | * @list: used internally |
468 | * @name: Identifier | |
469 | * @owner: module reference | |
470 | * @policy: netlink attribute policy | |
471 | * @maxattr: highest netlink attribute number | |
64d46806 | 472 | * @family: address family for AF-specific types |
ef1f7df9 PM |
473 | */ |
474 | struct nft_expr_type { | |
0ca743a5 PNA |
475 | const struct nft_expr_ops *(*select_ops)(const struct nft_ctx *, |
476 | const struct nlattr * const tb[]); | |
ef1f7df9 PM |
477 | const struct nft_expr_ops *ops; |
478 | struct list_head list; | |
479 | const char *name; | |
480 | struct module *owner; | |
481 | const struct nla_policy *policy; | |
482 | unsigned int maxattr; | |
64d46806 | 483 | u8 family; |
ef1f7df9 PM |
484 | }; |
485 | ||
486 | /** | |
487 | * struct nft_expr_ops - nf_tables expression operations | |
488 | * | |
489 | * @eval: Expression evaluation function | |
96518518 | 490 | * @size: full expression size, including private data size |
ef1f7df9 PM |
491 | * @init: initialization function |
492 | * @destroy: destruction function | |
493 | * @dump: function to dump parameters | |
494 | * @type: expression type | |
0ca743a5 PNA |
495 | * @validate: validate expression, called during loop detection |
496 | * @data: extra data to attach to this expression operation | |
96518518 PM |
497 | */ |
498 | struct nft_expr; | |
499 | struct nft_expr_ops { | |
500 | void (*eval)(const struct nft_expr *expr, | |
501 | struct nft_data data[NFT_REG_MAX + 1], | |
502 | const struct nft_pktinfo *pkt); | |
ef1f7df9 PM |
503 | unsigned int size; |
504 | ||
96518518 PM |
505 | int (*init)(const struct nft_ctx *ctx, |
506 | const struct nft_expr *expr, | |
507 | const struct nlattr * const tb[]); | |
62472bce PM |
508 | void (*destroy)(const struct nft_ctx *ctx, |
509 | const struct nft_expr *expr); | |
96518518 PM |
510 | int (*dump)(struct sk_buff *skb, |
511 | const struct nft_expr *expr); | |
0ca743a5 PNA |
512 | int (*validate)(const struct nft_ctx *ctx, |
513 | const struct nft_expr *expr, | |
514 | const struct nft_data **data); | |
ef1f7df9 | 515 | const struct nft_expr_type *type; |
0ca743a5 | 516 | void *data; |
96518518 PM |
517 | }; |
518 | ||
ef1f7df9 | 519 | #define NFT_EXPR_MAXATTR 16 |
96518518 PM |
520 | #define NFT_EXPR_SIZE(size) (sizeof(struct nft_expr) + \ |
521 | ALIGN(size, __alignof__(struct nft_expr))) | |
522 | ||
523 | /** | |
524 | * struct nft_expr - nf_tables expression | |
525 | * | |
526 | * @ops: expression ops | |
527 | * @data: expression private data | |
528 | */ | |
529 | struct nft_expr { | |
530 | const struct nft_expr_ops *ops; | |
531 | unsigned char data[]; | |
532 | }; | |
533 | ||
534 | static inline void *nft_expr_priv(const struct nft_expr *expr) | |
535 | { | |
536 | return (void *)expr->data; | |
537 | } | |
538 | ||
539 | /** | |
540 | * struct nft_rule - nf_tables rule | |
541 | * | |
542 | * @list: used internally | |
96518518 | 543 | * @handle: rule handle |
0628b123 | 544 | * @genmask: generation mask |
96518518 | 545 | * @dlen: length of expression data |
86f1ec32 | 546 | * @udata: user data is appended to the rule |
96518518 PM |
547 | * @data: expression data |
548 | */ | |
549 | struct nft_rule { | |
550 | struct list_head list; | |
0768b3b3 | 551 | u64 handle:42, |
0628b123 | 552 | genmask:2, |
0768b3b3 | 553 | dlen:12, |
86f1ec32 | 554 | udata:1; |
96518518 PM |
555 | unsigned char data[] |
556 | __attribute__((aligned(__alignof__(struct nft_expr)))); | |
557 | }; | |
558 | ||
559 | static inline struct nft_expr *nft_expr_first(const struct nft_rule *rule) | |
560 | { | |
561 | return (struct nft_expr *)&rule->data[0]; | |
562 | } | |
563 | ||
564 | static inline struct nft_expr *nft_expr_next(const struct nft_expr *expr) | |
565 | { | |
566 | return ((void *)expr) + expr->ops->size; | |
567 | } | |
568 | ||
569 | static inline struct nft_expr *nft_expr_last(const struct nft_rule *rule) | |
570 | { | |
571 | return (struct nft_expr *)&rule->data[rule->dlen]; | |
572 | } | |
573 | ||
86f1ec32 | 574 | static inline struct nft_userdata *nft_userdata(const struct nft_rule *rule) |
0768b3b3 PNA |
575 | { |
576 | return (void *)&rule->data[rule->dlen]; | |
577 | } | |
578 | ||
96518518 PM |
579 | /* |
580 | * The last pointer isn't really necessary, but the compiler isn't able to | |
581 | * determine that the result of nft_expr_last() is always the same since it | |
582 | * can't assume that the dlen value wasn't changed within calls in the loop. | |
583 | */ | |
584 | #define nft_rule_for_each_expr(expr, last, rule) \ | |
585 | for ((expr) = nft_expr_first(rule), (last) = nft_expr_last(rule); \ | |
586 | (expr) != (last); \ | |
587 | (expr) = nft_expr_next(expr)) | |
588 | ||
589 | enum nft_chain_flags { | |
590 | NFT_BASE_CHAIN = 0x1, | |
91c7b38d | 591 | NFT_CHAIN_INACTIVE = 0x2, |
96518518 PM |
592 | }; |
593 | ||
594 | /** | |
595 | * struct nft_chain - nf_tables chain | |
596 | * | |
597 | * @rules: list of rules in the chain | |
598 | * @list: used internally | |
b5bc89bf | 599 | * @table: table that this chain belongs to |
96518518 | 600 | * @handle: chain handle |
96518518 PM |
601 | * @use: number of jump references to this chain |
602 | * @level: length of longest path to this chain | |
a0a7379e | 603 | * @flags: bitmask of enum nft_chain_flags |
96518518 PM |
604 | * @name: name of the chain |
605 | */ | |
606 | struct nft_chain { | |
607 | struct list_head rules; | |
608 | struct list_head list; | |
b5bc89bf | 609 | struct nft_table *table; |
96518518 | 610 | u64 handle; |
a0a7379e | 611 | u32 use; |
96518518 | 612 | u16 level; |
a0a7379e | 613 | u8 flags; |
96518518 PM |
614 | char name[NFT_CHAIN_MAXNAMELEN]; |
615 | }; | |
616 | ||
9370761c PNA |
617 | enum nft_chain_type { |
618 | NFT_CHAIN_T_DEFAULT = 0, | |
619 | NFT_CHAIN_T_ROUTE, | |
620 | NFT_CHAIN_T_NAT, | |
621 | NFT_CHAIN_T_MAX | |
622 | }; | |
623 | ||
1a1e1a12 PM |
624 | /** |
625 | * struct nf_chain_type - nf_tables chain type info | |
626 | * | |
627 | * @name: name of the type | |
628 | * @type: numeric identifier | |
629 | * @family: address family | |
630 | * @owner: module owner | |
631 | * @hook_mask: mask of valid hooks | |
632 | * @hooks: hookfn overrides | |
633 | */ | |
634 | struct nf_chain_type { | |
635 | const char *name; | |
636 | enum nft_chain_type type; | |
637 | int family; | |
638 | struct module *owner; | |
639 | unsigned int hook_mask; | |
640 | nf_hookfn *hooks[NF_MAX_HOOKS]; | |
641 | }; | |
642 | ||
7210e4e3 PNA |
643 | int nft_chain_validate_dependency(const struct nft_chain *chain, |
644 | enum nft_chain_type type); | |
75e8d06d PNA |
645 | int nft_chain_validate_hooks(const struct nft_chain *chain, |
646 | unsigned int hook_flags); | |
7210e4e3 | 647 | |
0ca743a5 | 648 | struct nft_stats { |
ce355e20 ED |
649 | u64 bytes; |
650 | u64 pkts; | |
651 | struct u64_stats_sync syncp; | |
0ca743a5 PNA |
652 | }; |
653 | ||
115a60b1 PM |
654 | #define NFT_HOOK_OPS_MAX 2 |
655 | ||
96518518 PM |
656 | /** |
657 | * struct nft_base_chain - nf_tables base chain | |
658 | * | |
659 | * @ops: netfilter hook ops | |
5ebb335d | 660 | * @pnet: net namespace that this chain belongs to |
9370761c | 661 | * @type: chain type |
0ca743a5 PNA |
662 | * @policy: default policy |
663 | * @stats: per-cpu chain stats | |
96518518 PM |
664 | * @chain: the chain |
665 | */ | |
666 | struct nft_base_chain { | |
115a60b1 | 667 | struct nf_hook_ops ops[NFT_HOOK_OPS_MAX]; |
5ebb335d | 668 | possible_net_t pnet; |
2a37d755 | 669 | const struct nf_chain_type *type; |
0ca743a5 PNA |
670 | u8 policy; |
671 | struct nft_stats __percpu *stats; | |
96518518 PM |
672 | struct nft_chain chain; |
673 | }; | |
674 | ||
675 | static inline struct nft_base_chain *nft_base_chain(const struct nft_chain *chain) | |
676 | { | |
677 | return container_of(chain, struct nft_base_chain, chain); | |
678 | } | |
679 | ||
3876d22d PM |
680 | unsigned int nft_do_chain(struct nft_pktinfo *pkt, |
681 | const struct nf_hook_ops *ops); | |
96518518 | 682 | |
96518518 PM |
683 | /** |
684 | * struct nft_table - nf_tables table | |
685 | * | |
686 | * @list: used internally | |
687 | * @chains: chains in the table | |
688 | * @sets: sets in the table | |
689 | * @hgenerator: handle generator state | |
690 | * @use: number of chain references to this table | |
691 | * @flags: table flag (see enum nft_table_flags) | |
692 | * @name: name of the table | |
693 | */ | |
694 | struct nft_table { | |
695 | struct list_head list; | |
696 | struct list_head chains; | |
697 | struct list_head sets; | |
698 | u64 hgenerator; | |
699 | u32 use; | |
700 | u16 flags; | |
1cae565e | 701 | char name[NFT_TABLE_MAXNAMELEN]; |
96518518 PM |
702 | }; |
703 | ||
704 | /** | |
705 | * struct nft_af_info - nf_tables address family info | |
706 | * | |
707 | * @list: used internally | |
708 | * @family: address family | |
709 | * @nhooks: number of hooks in this family | |
710 | * @owner: module owner | |
711 | * @tables: used internally | |
115a60b1 PM |
712 | * @nops: number of hook ops in this family |
713 | * @hook_ops_init: initialization function for chain hook ops | |
96518518 PM |
714 | * @hooks: hookfn overrides for packet validation |
715 | */ | |
716 | struct nft_af_info { | |
717 | struct list_head list; | |
718 | int family; | |
719 | unsigned int nhooks; | |
720 | struct module *owner; | |
721 | struct list_head tables; | |
115a60b1 PM |
722 | unsigned int nops; |
723 | void (*hook_ops_init)(struct nf_hook_ops *, | |
724 | unsigned int); | |
96518518 PM |
725 | nf_hookfn *hooks[NF_MAX_HOOKS]; |
726 | }; | |
727 | ||
5eccdfaa JP |
728 | int nft_register_afinfo(struct net *, struct nft_af_info *); |
729 | void nft_unregister_afinfo(struct nft_af_info *); | |
96518518 | 730 | |
2a37d755 PM |
731 | int nft_register_chain_type(const struct nf_chain_type *); |
732 | void nft_unregister_chain_type(const struct nf_chain_type *); | |
96518518 | 733 | |
5eccdfaa JP |
734 | int nft_register_expr(struct nft_expr_type *); |
735 | void nft_unregister_expr(struct nft_expr_type *); | |
96518518 | 736 | |
67a8fc27 PM |
737 | #define nft_dereference(p) \ |
738 | nfnl_dereference(p, NFNL_SUBSYS_NFTABLES) | |
739 | ||
96518518 PM |
740 | #define MODULE_ALIAS_NFT_FAMILY(family) \ |
741 | MODULE_ALIAS("nft-afinfo-" __stringify(family)) | |
742 | ||
9370761c PNA |
743 | #define MODULE_ALIAS_NFT_CHAIN(family, name) \ |
744 | MODULE_ALIAS("nft-chain-" __stringify(family) "-" name) | |
96518518 | 745 | |
64d46806 PM |
746 | #define MODULE_ALIAS_NFT_AF_EXPR(family, name) \ |
747 | MODULE_ALIAS("nft-expr-" __stringify(family) "-" name) | |
748 | ||
96518518 PM |
749 | #define MODULE_ALIAS_NFT_EXPR(name) \ |
750 | MODULE_ALIAS("nft-expr-" name) | |
751 | ||
20a69341 PM |
752 | #define MODULE_ALIAS_NFT_SET() \ |
753 | MODULE_ALIAS("nft-set") | |
754 | ||
ea4bd995 PM |
755 | /* |
756 | * The gencursor defines two generations, the currently active and the | |
757 | * next one. Objects contain a bitmask of 2 bits specifying the generations | |
758 | * they're active in. A set bit means they're inactive in the generation | |
759 | * represented by that bit. | |
760 | * | |
761 | * New objects start out as inactive in the current and active in the | |
762 | * next generation. When committing the ruleset the bitmask is cleared, | |
763 | * meaning they're active in all generations. When removing an object, | |
764 | * it is set inactive in the next generation. After committing the ruleset, | |
765 | * the objects are removed. | |
766 | */ | |
767 | static inline unsigned int nft_gencursor_next(const struct net *net) | |
768 | { | |
769 | return net->nft.gencursor + 1 == 1 ? 1 : 0; | |
770 | } | |
771 | ||
772 | static inline u8 nft_genmask_next(const struct net *net) | |
773 | { | |
774 | return 1 << nft_gencursor_next(net); | |
775 | } | |
776 | ||
777 | static inline u8 nft_genmask_cur(const struct net *net) | |
778 | { | |
779 | /* Use ACCESS_ONCE() to prevent refetching the value for atomicity */ | |
780 | return 1 << ACCESS_ONCE(net->nft.gencursor); | |
781 | } | |
782 | ||
cc02e457 PM |
783 | /* |
784 | * Set element transaction helpers | |
785 | */ | |
786 | ||
787 | static inline bool nft_set_elem_active(const struct nft_set_ext *ext, | |
788 | u8 genmask) | |
789 | { | |
790 | return !(ext->genmask & genmask); | |
791 | } | |
792 | ||
793 | static inline void nft_set_elem_change_active(const struct nft_set *set, | |
794 | struct nft_set_ext *ext) | |
795 | { | |
796 | ext->genmask ^= nft_genmask_next(read_pnet(&set->pnet)); | |
797 | } | |
798 | ||
1a1e1a12 PM |
799 | /** |
800 | * struct nft_trans - nf_tables object update in transaction | |
801 | * | |
802 | * @list: used internally | |
803 | * @msg_type: message type | |
804 | * @ctx: transaction context | |
805 | * @data: internal information related to the transaction | |
806 | */ | |
807 | struct nft_trans { | |
808 | struct list_head list; | |
809 | int msg_type; | |
810 | struct nft_ctx ctx; | |
811 | char data[0]; | |
812 | }; | |
813 | ||
814 | struct nft_trans_rule { | |
815 | struct nft_rule *rule; | |
816 | }; | |
817 | ||
818 | #define nft_trans_rule(trans) \ | |
819 | (((struct nft_trans_rule *)trans->data)->rule) | |
820 | ||
821 | struct nft_trans_set { | |
822 | struct nft_set *set; | |
823 | u32 set_id; | |
824 | }; | |
825 | ||
826 | #define nft_trans_set(trans) \ | |
827 | (((struct nft_trans_set *)trans->data)->set) | |
828 | #define nft_trans_set_id(trans) \ | |
829 | (((struct nft_trans_set *)trans->data)->set_id) | |
830 | ||
831 | struct nft_trans_chain { | |
832 | bool update; | |
833 | char name[NFT_CHAIN_MAXNAMELEN]; | |
834 | struct nft_stats __percpu *stats; | |
835 | u8 policy; | |
836 | }; | |
837 | ||
838 | #define nft_trans_chain_update(trans) \ | |
839 | (((struct nft_trans_chain *)trans->data)->update) | |
840 | #define nft_trans_chain_name(trans) \ | |
841 | (((struct nft_trans_chain *)trans->data)->name) | |
842 | #define nft_trans_chain_stats(trans) \ | |
843 | (((struct nft_trans_chain *)trans->data)->stats) | |
844 | #define nft_trans_chain_policy(trans) \ | |
845 | (((struct nft_trans_chain *)trans->data)->policy) | |
846 | ||
847 | struct nft_trans_table { | |
848 | bool update; | |
849 | bool enable; | |
850 | }; | |
851 | ||
852 | #define nft_trans_table_update(trans) \ | |
853 | (((struct nft_trans_table *)trans->data)->update) | |
854 | #define nft_trans_table_enable(trans) \ | |
855 | (((struct nft_trans_table *)trans->data)->enable) | |
856 | ||
857 | struct nft_trans_elem { | |
858 | struct nft_set *set; | |
859 | struct nft_set_elem elem; | |
860 | }; | |
861 | ||
862 | #define nft_trans_elem_set(trans) \ | |
863 | (((struct nft_trans_elem *)trans->data)->set) | |
864 | #define nft_trans_elem(trans) \ | |
865 | (((struct nft_trans_elem *)trans->data)->elem) | |
866 | ||
96518518 | 867 | #endif /* _NET_NF_TABLES_H */ |