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 | ||
9d098292 PM |
297 | static inline struct nft_set *nft_set_container_of(const void *priv) |
298 | { | |
299 | return (void *)priv - offsetof(struct nft_set, data); | |
300 | } | |
301 | ||
5eccdfaa JP |
302 | struct nft_set *nf_tables_set_lookup(const struct nft_table *table, |
303 | const struct nlattr *nla); | |
958bee14 PNA |
304 | struct nft_set *nf_tables_set_lookup_byid(const struct net *net, |
305 | const struct nlattr *nla); | |
20a69341 | 306 | |
761da293 PM |
307 | static inline unsigned long nft_set_gc_interval(const struct nft_set *set) |
308 | { | |
309 | return set->gc_int ? msecs_to_jiffies(set->gc_int) : HZ; | |
310 | } | |
311 | ||
20a69341 PM |
312 | /** |
313 | * struct nft_set_binding - nf_tables set binding | |
314 | * | |
315 | * @list: set bindings list node | |
316 | * @chain: chain containing the rule bound to the set | |
317 | * | |
318 | * A set binding contains all information necessary for validation | |
319 | * of new elements added to a bound set. | |
320 | */ | |
321 | struct nft_set_binding { | |
322 | struct list_head list; | |
323 | const struct nft_chain *chain; | |
324 | }; | |
325 | ||
5eccdfaa JP |
326 | int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set, |
327 | struct nft_set_binding *binding); | |
328 | void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set, | |
329 | struct nft_set_binding *binding); | |
20a69341 | 330 | |
3ac4c07a PM |
331 | /** |
332 | * enum nft_set_extensions - set extension type IDs | |
333 | * | |
334 | * @NFT_SET_EXT_KEY: element key | |
335 | * @NFT_SET_EXT_DATA: mapping data | |
336 | * @NFT_SET_EXT_FLAGS: element flags | |
c3e1b005 PM |
337 | * @NFT_SET_EXT_TIMEOUT: element timeout |
338 | * @NFT_SET_EXT_EXPIRATION: element expiration time | |
3ac4c07a PM |
339 | * @NFT_SET_EXT_NUM: number of extension types |
340 | */ | |
341 | enum nft_set_extensions { | |
342 | NFT_SET_EXT_KEY, | |
343 | NFT_SET_EXT_DATA, | |
344 | NFT_SET_EXT_FLAGS, | |
c3e1b005 PM |
345 | NFT_SET_EXT_TIMEOUT, |
346 | NFT_SET_EXT_EXPIRATION, | |
3ac4c07a PM |
347 | NFT_SET_EXT_NUM |
348 | }; | |
349 | ||
350 | /** | |
351 | * struct nft_set_ext_type - set extension type | |
352 | * | |
353 | * @len: fixed part length of the extension | |
354 | * @align: alignment requirements of the extension | |
355 | */ | |
356 | struct nft_set_ext_type { | |
357 | u8 len; | |
358 | u8 align; | |
359 | }; | |
360 | ||
361 | extern const struct nft_set_ext_type nft_set_ext_types[]; | |
362 | ||
363 | /** | |
364 | * struct nft_set_ext_tmpl - set extension template | |
365 | * | |
366 | * @len: length of extension area | |
367 | * @offset: offsets of individual extension types | |
368 | */ | |
369 | struct nft_set_ext_tmpl { | |
370 | u16 len; | |
371 | u8 offset[NFT_SET_EXT_NUM]; | |
372 | }; | |
373 | ||
374 | /** | |
375 | * struct nft_set_ext - set extensions | |
376 | * | |
cc02e457 | 377 | * @genmask: generation mask |
3ac4c07a PM |
378 | * @offset: offsets of individual extension types |
379 | * @data: beginning of extension data | |
380 | */ | |
381 | struct nft_set_ext { | |
cc02e457 | 382 | u8 genmask; |
3ac4c07a PM |
383 | u8 offset[NFT_SET_EXT_NUM]; |
384 | char data[0]; | |
385 | }; | |
386 | ||
387 | static inline void nft_set_ext_prepare(struct nft_set_ext_tmpl *tmpl) | |
388 | { | |
389 | memset(tmpl, 0, sizeof(*tmpl)); | |
390 | tmpl->len = sizeof(struct nft_set_ext); | |
391 | } | |
392 | ||
393 | static inline void nft_set_ext_add_length(struct nft_set_ext_tmpl *tmpl, u8 id, | |
394 | unsigned int len) | |
395 | { | |
396 | tmpl->len = ALIGN(tmpl->len, nft_set_ext_types[id].align); | |
397 | BUG_ON(tmpl->len > U8_MAX); | |
398 | tmpl->offset[id] = tmpl->len; | |
399 | tmpl->len += nft_set_ext_types[id].len + len; | |
400 | } | |
401 | ||
402 | static inline void nft_set_ext_add(struct nft_set_ext_tmpl *tmpl, u8 id) | |
403 | { | |
404 | nft_set_ext_add_length(tmpl, id, 0); | |
405 | } | |
406 | ||
407 | static inline void nft_set_ext_init(struct nft_set_ext *ext, | |
408 | const struct nft_set_ext_tmpl *tmpl) | |
409 | { | |
410 | memcpy(ext->offset, tmpl->offset, sizeof(ext->offset)); | |
411 | } | |
412 | ||
413 | static inline bool __nft_set_ext_exists(const struct nft_set_ext *ext, u8 id) | |
414 | { | |
415 | return !!ext->offset[id]; | |
416 | } | |
417 | ||
418 | static inline bool nft_set_ext_exists(const struct nft_set_ext *ext, u8 id) | |
419 | { | |
420 | return ext && __nft_set_ext_exists(ext, id); | |
421 | } | |
422 | ||
423 | static inline void *nft_set_ext(const struct nft_set_ext *ext, u8 id) | |
424 | { | |
425 | return (void *)ext + ext->offset[id]; | |
426 | } | |
427 | ||
428 | static inline struct nft_data *nft_set_ext_key(const struct nft_set_ext *ext) | |
429 | { | |
430 | return nft_set_ext(ext, NFT_SET_EXT_KEY); | |
431 | } | |
432 | ||
433 | static inline struct nft_data *nft_set_ext_data(const struct nft_set_ext *ext) | |
434 | { | |
435 | return nft_set_ext(ext, NFT_SET_EXT_DATA); | |
436 | } | |
437 | ||
438 | static inline u8 *nft_set_ext_flags(const struct nft_set_ext *ext) | |
439 | { | |
440 | return nft_set_ext(ext, NFT_SET_EXT_FLAGS); | |
441 | } | |
ef1f7df9 | 442 | |
c3e1b005 PM |
443 | static inline u64 *nft_set_ext_timeout(const struct nft_set_ext *ext) |
444 | { | |
445 | return nft_set_ext(ext, NFT_SET_EXT_TIMEOUT); | |
446 | } | |
447 | ||
448 | static inline unsigned long *nft_set_ext_expiration(const struct nft_set_ext *ext) | |
449 | { | |
450 | return nft_set_ext(ext, NFT_SET_EXT_EXPIRATION); | |
451 | } | |
452 | ||
453 | static inline bool nft_set_elem_expired(const struct nft_set_ext *ext) | |
454 | { | |
455 | return nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION) && | |
456 | time_is_before_eq_jiffies(*nft_set_ext_expiration(ext)); | |
457 | } | |
458 | ||
fe2811eb PM |
459 | static inline struct nft_set_ext *nft_set_elem_ext(const struct nft_set *set, |
460 | void *elem) | |
461 | { | |
462 | return elem + set->ops->elemsize; | |
463 | } | |
464 | ||
61edafbb PM |
465 | void nft_set_elem_destroy(const struct nft_set *set, void *elem); |
466 | ||
cfed7e1b PM |
467 | /** |
468 | * struct nft_set_gc_batch_head - nf_tables set garbage collection batch | |
469 | * | |
470 | * @rcu: rcu head | |
471 | * @set: set the elements belong to | |
472 | * @cnt: count of elements | |
473 | */ | |
474 | struct nft_set_gc_batch_head { | |
475 | struct rcu_head rcu; | |
476 | const struct nft_set *set; | |
477 | unsigned int cnt; | |
478 | }; | |
479 | ||
480 | #define NFT_SET_GC_BATCH_SIZE ((PAGE_SIZE - \ | |
481 | sizeof(struct nft_set_gc_batch_head)) / \ | |
482 | sizeof(void *)) | |
483 | ||
484 | /** | |
485 | * struct nft_set_gc_batch - nf_tables set garbage collection batch | |
486 | * | |
487 | * @head: GC batch head | |
488 | * @elems: garbage collection elements | |
489 | */ | |
490 | struct nft_set_gc_batch { | |
491 | struct nft_set_gc_batch_head head; | |
492 | void *elems[NFT_SET_GC_BATCH_SIZE]; | |
493 | }; | |
494 | ||
495 | struct nft_set_gc_batch *nft_set_gc_batch_alloc(const struct nft_set *set, | |
496 | gfp_t gfp); | |
497 | void nft_set_gc_batch_release(struct rcu_head *rcu); | |
498 | ||
499 | static inline void nft_set_gc_batch_complete(struct nft_set_gc_batch *gcb) | |
500 | { | |
501 | if (gcb != NULL) | |
502 | call_rcu(&gcb->head.rcu, nft_set_gc_batch_release); | |
503 | } | |
504 | ||
505 | static inline struct nft_set_gc_batch * | |
506 | nft_set_gc_batch_check(const struct nft_set *set, struct nft_set_gc_batch *gcb, | |
507 | gfp_t gfp) | |
508 | { | |
509 | if (gcb != NULL) { | |
510 | if (gcb->head.cnt + 1 < ARRAY_SIZE(gcb->elems)) | |
511 | return gcb; | |
512 | nft_set_gc_batch_complete(gcb); | |
513 | } | |
514 | return nft_set_gc_batch_alloc(set, gfp); | |
515 | } | |
516 | ||
517 | static inline void nft_set_gc_batch_add(struct nft_set_gc_batch *gcb, | |
518 | void *elem) | |
519 | { | |
520 | gcb->elems[gcb->head.cnt++] = elem; | |
521 | } | |
522 | ||
96518518 | 523 | /** |
ef1f7df9 | 524 | * struct nft_expr_type - nf_tables expression type |
96518518 | 525 | * |
ef1f7df9 PM |
526 | * @select_ops: function to select nft_expr_ops |
527 | * @ops: default ops, used when no select_ops functions is present | |
96518518 PM |
528 | * @list: used internally |
529 | * @name: Identifier | |
530 | * @owner: module reference | |
531 | * @policy: netlink attribute policy | |
532 | * @maxattr: highest netlink attribute number | |
64d46806 | 533 | * @family: address family for AF-specific types |
ef1f7df9 PM |
534 | */ |
535 | struct nft_expr_type { | |
0ca743a5 PNA |
536 | const struct nft_expr_ops *(*select_ops)(const struct nft_ctx *, |
537 | const struct nlattr * const tb[]); | |
ef1f7df9 PM |
538 | const struct nft_expr_ops *ops; |
539 | struct list_head list; | |
540 | const char *name; | |
541 | struct module *owner; | |
542 | const struct nla_policy *policy; | |
543 | unsigned int maxattr; | |
64d46806 | 544 | u8 family; |
ef1f7df9 PM |
545 | }; |
546 | ||
547 | /** | |
548 | * struct nft_expr_ops - nf_tables expression operations | |
549 | * | |
550 | * @eval: Expression evaluation function | |
96518518 | 551 | * @size: full expression size, including private data size |
ef1f7df9 PM |
552 | * @init: initialization function |
553 | * @destroy: destruction function | |
554 | * @dump: function to dump parameters | |
555 | * @type: expression type | |
0ca743a5 PNA |
556 | * @validate: validate expression, called during loop detection |
557 | * @data: extra data to attach to this expression operation | |
96518518 PM |
558 | */ |
559 | struct nft_expr; | |
560 | struct nft_expr_ops { | |
561 | void (*eval)(const struct nft_expr *expr, | |
562 | struct nft_data data[NFT_REG_MAX + 1], | |
563 | const struct nft_pktinfo *pkt); | |
ef1f7df9 PM |
564 | unsigned int size; |
565 | ||
96518518 PM |
566 | int (*init)(const struct nft_ctx *ctx, |
567 | const struct nft_expr *expr, | |
568 | const struct nlattr * const tb[]); | |
62472bce PM |
569 | void (*destroy)(const struct nft_ctx *ctx, |
570 | const struct nft_expr *expr); | |
96518518 PM |
571 | int (*dump)(struct sk_buff *skb, |
572 | const struct nft_expr *expr); | |
0ca743a5 PNA |
573 | int (*validate)(const struct nft_ctx *ctx, |
574 | const struct nft_expr *expr, | |
575 | const struct nft_data **data); | |
ef1f7df9 | 576 | const struct nft_expr_type *type; |
0ca743a5 | 577 | void *data; |
96518518 PM |
578 | }; |
579 | ||
ef1f7df9 | 580 | #define NFT_EXPR_MAXATTR 16 |
96518518 PM |
581 | #define NFT_EXPR_SIZE(size) (sizeof(struct nft_expr) + \ |
582 | ALIGN(size, __alignof__(struct nft_expr))) | |
583 | ||
584 | /** | |
585 | * struct nft_expr - nf_tables expression | |
586 | * | |
587 | * @ops: expression ops | |
588 | * @data: expression private data | |
589 | */ | |
590 | struct nft_expr { | |
591 | const struct nft_expr_ops *ops; | |
592 | unsigned char data[]; | |
593 | }; | |
594 | ||
595 | static inline void *nft_expr_priv(const struct nft_expr *expr) | |
596 | { | |
597 | return (void *)expr->data; | |
598 | } | |
599 | ||
600 | /** | |
601 | * struct nft_rule - nf_tables rule | |
602 | * | |
603 | * @list: used internally | |
96518518 | 604 | * @handle: rule handle |
0628b123 | 605 | * @genmask: generation mask |
96518518 | 606 | * @dlen: length of expression data |
86f1ec32 | 607 | * @udata: user data is appended to the rule |
96518518 PM |
608 | * @data: expression data |
609 | */ | |
610 | struct nft_rule { | |
611 | struct list_head list; | |
0768b3b3 | 612 | u64 handle:42, |
0628b123 | 613 | genmask:2, |
0768b3b3 | 614 | dlen:12, |
86f1ec32 | 615 | udata:1; |
96518518 PM |
616 | unsigned char data[] |
617 | __attribute__((aligned(__alignof__(struct nft_expr)))); | |
618 | }; | |
619 | ||
620 | static inline struct nft_expr *nft_expr_first(const struct nft_rule *rule) | |
621 | { | |
622 | return (struct nft_expr *)&rule->data[0]; | |
623 | } | |
624 | ||
625 | static inline struct nft_expr *nft_expr_next(const struct nft_expr *expr) | |
626 | { | |
627 | return ((void *)expr) + expr->ops->size; | |
628 | } | |
629 | ||
630 | static inline struct nft_expr *nft_expr_last(const struct nft_rule *rule) | |
631 | { | |
632 | return (struct nft_expr *)&rule->data[rule->dlen]; | |
633 | } | |
634 | ||
86f1ec32 | 635 | static inline struct nft_userdata *nft_userdata(const struct nft_rule *rule) |
0768b3b3 PNA |
636 | { |
637 | return (void *)&rule->data[rule->dlen]; | |
638 | } | |
639 | ||
96518518 PM |
640 | /* |
641 | * The last pointer isn't really necessary, but the compiler isn't able to | |
642 | * determine that the result of nft_expr_last() is always the same since it | |
643 | * can't assume that the dlen value wasn't changed within calls in the loop. | |
644 | */ | |
645 | #define nft_rule_for_each_expr(expr, last, rule) \ | |
646 | for ((expr) = nft_expr_first(rule), (last) = nft_expr_last(rule); \ | |
647 | (expr) != (last); \ | |
648 | (expr) = nft_expr_next(expr)) | |
649 | ||
650 | enum nft_chain_flags { | |
651 | NFT_BASE_CHAIN = 0x1, | |
91c7b38d | 652 | NFT_CHAIN_INACTIVE = 0x2, |
96518518 PM |
653 | }; |
654 | ||
655 | /** | |
656 | * struct nft_chain - nf_tables chain | |
657 | * | |
658 | * @rules: list of rules in the chain | |
659 | * @list: used internally | |
b5bc89bf | 660 | * @table: table that this chain belongs to |
96518518 | 661 | * @handle: chain handle |
96518518 PM |
662 | * @use: number of jump references to this chain |
663 | * @level: length of longest path to this chain | |
a0a7379e | 664 | * @flags: bitmask of enum nft_chain_flags |
96518518 PM |
665 | * @name: name of the chain |
666 | */ | |
667 | struct nft_chain { | |
668 | struct list_head rules; | |
669 | struct list_head list; | |
b5bc89bf | 670 | struct nft_table *table; |
96518518 | 671 | u64 handle; |
a0a7379e | 672 | u32 use; |
96518518 | 673 | u16 level; |
a0a7379e | 674 | u8 flags; |
96518518 PM |
675 | char name[NFT_CHAIN_MAXNAMELEN]; |
676 | }; | |
677 | ||
9370761c PNA |
678 | enum nft_chain_type { |
679 | NFT_CHAIN_T_DEFAULT = 0, | |
680 | NFT_CHAIN_T_ROUTE, | |
681 | NFT_CHAIN_T_NAT, | |
682 | NFT_CHAIN_T_MAX | |
683 | }; | |
684 | ||
1a1e1a12 PM |
685 | /** |
686 | * struct nf_chain_type - nf_tables chain type info | |
687 | * | |
688 | * @name: name of the type | |
689 | * @type: numeric identifier | |
690 | * @family: address family | |
691 | * @owner: module owner | |
692 | * @hook_mask: mask of valid hooks | |
693 | * @hooks: hookfn overrides | |
694 | */ | |
695 | struct nf_chain_type { | |
696 | const char *name; | |
697 | enum nft_chain_type type; | |
698 | int family; | |
699 | struct module *owner; | |
700 | unsigned int hook_mask; | |
701 | nf_hookfn *hooks[NF_MAX_HOOKS]; | |
702 | }; | |
703 | ||
7210e4e3 PNA |
704 | int nft_chain_validate_dependency(const struct nft_chain *chain, |
705 | enum nft_chain_type type); | |
75e8d06d PNA |
706 | int nft_chain_validate_hooks(const struct nft_chain *chain, |
707 | unsigned int hook_flags); | |
7210e4e3 | 708 | |
0ca743a5 | 709 | struct nft_stats { |
ce355e20 ED |
710 | u64 bytes; |
711 | u64 pkts; | |
712 | struct u64_stats_sync syncp; | |
0ca743a5 PNA |
713 | }; |
714 | ||
115a60b1 PM |
715 | #define NFT_HOOK_OPS_MAX 2 |
716 | ||
96518518 PM |
717 | /** |
718 | * struct nft_base_chain - nf_tables base chain | |
719 | * | |
720 | * @ops: netfilter hook ops | |
5ebb335d | 721 | * @pnet: net namespace that this chain belongs to |
9370761c | 722 | * @type: chain type |
0ca743a5 PNA |
723 | * @policy: default policy |
724 | * @stats: per-cpu chain stats | |
96518518 PM |
725 | * @chain: the chain |
726 | */ | |
727 | struct nft_base_chain { | |
115a60b1 | 728 | struct nf_hook_ops ops[NFT_HOOK_OPS_MAX]; |
5ebb335d | 729 | possible_net_t pnet; |
2a37d755 | 730 | const struct nf_chain_type *type; |
0ca743a5 PNA |
731 | u8 policy; |
732 | struct nft_stats __percpu *stats; | |
96518518 PM |
733 | struct nft_chain chain; |
734 | }; | |
735 | ||
736 | static inline struct nft_base_chain *nft_base_chain(const struct nft_chain *chain) | |
737 | { | |
738 | return container_of(chain, struct nft_base_chain, chain); | |
739 | } | |
740 | ||
3876d22d PM |
741 | unsigned int nft_do_chain(struct nft_pktinfo *pkt, |
742 | const struct nf_hook_ops *ops); | |
96518518 | 743 | |
96518518 PM |
744 | /** |
745 | * struct nft_table - nf_tables table | |
746 | * | |
747 | * @list: used internally | |
748 | * @chains: chains in the table | |
749 | * @sets: sets in the table | |
750 | * @hgenerator: handle generator state | |
751 | * @use: number of chain references to this table | |
752 | * @flags: table flag (see enum nft_table_flags) | |
753 | * @name: name of the table | |
754 | */ | |
755 | struct nft_table { | |
756 | struct list_head list; | |
757 | struct list_head chains; | |
758 | struct list_head sets; | |
759 | u64 hgenerator; | |
760 | u32 use; | |
761 | u16 flags; | |
1cae565e | 762 | char name[NFT_TABLE_MAXNAMELEN]; |
96518518 PM |
763 | }; |
764 | ||
765 | /** | |
766 | * struct nft_af_info - nf_tables address family info | |
767 | * | |
768 | * @list: used internally | |
769 | * @family: address family | |
770 | * @nhooks: number of hooks in this family | |
771 | * @owner: module owner | |
772 | * @tables: used internally | |
115a60b1 PM |
773 | * @nops: number of hook ops in this family |
774 | * @hook_ops_init: initialization function for chain hook ops | |
96518518 PM |
775 | * @hooks: hookfn overrides for packet validation |
776 | */ | |
777 | struct nft_af_info { | |
778 | struct list_head list; | |
779 | int family; | |
780 | unsigned int nhooks; | |
781 | struct module *owner; | |
782 | struct list_head tables; | |
115a60b1 PM |
783 | unsigned int nops; |
784 | void (*hook_ops_init)(struct nf_hook_ops *, | |
785 | unsigned int); | |
96518518 PM |
786 | nf_hookfn *hooks[NF_MAX_HOOKS]; |
787 | }; | |
788 | ||
5eccdfaa JP |
789 | int nft_register_afinfo(struct net *, struct nft_af_info *); |
790 | void nft_unregister_afinfo(struct nft_af_info *); | |
96518518 | 791 | |
2a37d755 PM |
792 | int nft_register_chain_type(const struct nf_chain_type *); |
793 | void nft_unregister_chain_type(const struct nf_chain_type *); | |
96518518 | 794 | |
5eccdfaa JP |
795 | int nft_register_expr(struct nft_expr_type *); |
796 | void nft_unregister_expr(struct nft_expr_type *); | |
96518518 | 797 | |
67a8fc27 PM |
798 | #define nft_dereference(p) \ |
799 | nfnl_dereference(p, NFNL_SUBSYS_NFTABLES) | |
800 | ||
96518518 PM |
801 | #define MODULE_ALIAS_NFT_FAMILY(family) \ |
802 | MODULE_ALIAS("nft-afinfo-" __stringify(family)) | |
803 | ||
9370761c PNA |
804 | #define MODULE_ALIAS_NFT_CHAIN(family, name) \ |
805 | MODULE_ALIAS("nft-chain-" __stringify(family) "-" name) | |
96518518 | 806 | |
64d46806 PM |
807 | #define MODULE_ALIAS_NFT_AF_EXPR(family, name) \ |
808 | MODULE_ALIAS("nft-expr-" __stringify(family) "-" name) | |
809 | ||
96518518 PM |
810 | #define MODULE_ALIAS_NFT_EXPR(name) \ |
811 | MODULE_ALIAS("nft-expr-" name) | |
812 | ||
20a69341 PM |
813 | #define MODULE_ALIAS_NFT_SET() \ |
814 | MODULE_ALIAS("nft-set") | |
815 | ||
ea4bd995 PM |
816 | /* |
817 | * The gencursor defines two generations, the currently active and the | |
818 | * next one. Objects contain a bitmask of 2 bits specifying the generations | |
819 | * they're active in. A set bit means they're inactive in the generation | |
820 | * represented by that bit. | |
821 | * | |
822 | * New objects start out as inactive in the current and active in the | |
823 | * next generation. When committing the ruleset the bitmask is cleared, | |
824 | * meaning they're active in all generations. When removing an object, | |
825 | * it is set inactive in the next generation. After committing the ruleset, | |
826 | * the objects are removed. | |
827 | */ | |
828 | static inline unsigned int nft_gencursor_next(const struct net *net) | |
829 | { | |
830 | return net->nft.gencursor + 1 == 1 ? 1 : 0; | |
831 | } | |
832 | ||
833 | static inline u8 nft_genmask_next(const struct net *net) | |
834 | { | |
835 | return 1 << nft_gencursor_next(net); | |
836 | } | |
837 | ||
838 | static inline u8 nft_genmask_cur(const struct net *net) | |
839 | { | |
840 | /* Use ACCESS_ONCE() to prevent refetching the value for atomicity */ | |
841 | return 1 << ACCESS_ONCE(net->nft.gencursor); | |
842 | } | |
843 | ||
cc02e457 PM |
844 | /* |
845 | * Set element transaction helpers | |
846 | */ | |
847 | ||
848 | static inline bool nft_set_elem_active(const struct nft_set_ext *ext, | |
849 | u8 genmask) | |
850 | { | |
851 | return !(ext->genmask & genmask); | |
852 | } | |
853 | ||
854 | static inline void nft_set_elem_change_active(const struct nft_set *set, | |
855 | struct nft_set_ext *ext) | |
856 | { | |
857 | ext->genmask ^= nft_genmask_next(read_pnet(&set->pnet)); | |
858 | } | |
859 | ||
69086658 PM |
860 | /* |
861 | * We use a free bit in the genmask field to indicate the element | |
862 | * is busy, meaning it is currently being processed either by | |
863 | * the netlink API or GC. | |
864 | * | |
865 | * Even though the genmask is only a single byte wide, this works | |
866 | * because the extension structure if fully constant once initialized, | |
867 | * so there are no non-atomic write accesses unless it is already | |
868 | * marked busy. | |
869 | */ | |
870 | #define NFT_SET_ELEM_BUSY_MASK (1 << 2) | |
871 | ||
872 | #if defined(__LITTLE_ENDIAN_BITFIELD) | |
873 | #define NFT_SET_ELEM_BUSY_BIT 2 | |
874 | #elif defined(__BIG_ENDIAN_BITFIELD) | |
875 | #define NFT_SET_ELEM_BUSY_BIT (BITS_PER_LONG - BITS_PER_BYTE + 2) | |
876 | #else | |
877 | #error | |
878 | #endif | |
879 | ||
880 | static inline int nft_set_elem_mark_busy(struct nft_set_ext *ext) | |
881 | { | |
882 | unsigned long *word = (unsigned long *)ext; | |
883 | ||
884 | BUILD_BUG_ON(offsetof(struct nft_set_ext, genmask) != 0); | |
885 | return test_and_set_bit(NFT_SET_ELEM_BUSY_BIT, word); | |
886 | } | |
887 | ||
888 | static inline void nft_set_elem_clear_busy(struct nft_set_ext *ext) | |
889 | { | |
890 | unsigned long *word = (unsigned long *)ext; | |
891 | ||
892 | clear_bit(NFT_SET_ELEM_BUSY_BIT, word); | |
893 | } | |
894 | ||
1a1e1a12 PM |
895 | /** |
896 | * struct nft_trans - nf_tables object update in transaction | |
897 | * | |
898 | * @list: used internally | |
899 | * @msg_type: message type | |
900 | * @ctx: transaction context | |
901 | * @data: internal information related to the transaction | |
902 | */ | |
903 | struct nft_trans { | |
904 | struct list_head list; | |
905 | int msg_type; | |
906 | struct nft_ctx ctx; | |
907 | char data[0]; | |
908 | }; | |
909 | ||
910 | struct nft_trans_rule { | |
911 | struct nft_rule *rule; | |
912 | }; | |
913 | ||
914 | #define nft_trans_rule(trans) \ | |
915 | (((struct nft_trans_rule *)trans->data)->rule) | |
916 | ||
917 | struct nft_trans_set { | |
918 | struct nft_set *set; | |
919 | u32 set_id; | |
920 | }; | |
921 | ||
922 | #define nft_trans_set(trans) \ | |
923 | (((struct nft_trans_set *)trans->data)->set) | |
924 | #define nft_trans_set_id(trans) \ | |
925 | (((struct nft_trans_set *)trans->data)->set_id) | |
926 | ||
927 | struct nft_trans_chain { | |
928 | bool update; | |
929 | char name[NFT_CHAIN_MAXNAMELEN]; | |
930 | struct nft_stats __percpu *stats; | |
931 | u8 policy; | |
932 | }; | |
933 | ||
934 | #define nft_trans_chain_update(trans) \ | |
935 | (((struct nft_trans_chain *)trans->data)->update) | |
936 | #define nft_trans_chain_name(trans) \ | |
937 | (((struct nft_trans_chain *)trans->data)->name) | |
938 | #define nft_trans_chain_stats(trans) \ | |
939 | (((struct nft_trans_chain *)trans->data)->stats) | |
940 | #define nft_trans_chain_policy(trans) \ | |
941 | (((struct nft_trans_chain *)trans->data)->policy) | |
942 | ||
943 | struct nft_trans_table { | |
944 | bool update; | |
945 | bool enable; | |
946 | }; | |
947 | ||
948 | #define nft_trans_table_update(trans) \ | |
949 | (((struct nft_trans_table *)trans->data)->update) | |
950 | #define nft_trans_table_enable(trans) \ | |
951 | (((struct nft_trans_table *)trans->data)->enable) | |
952 | ||
953 | struct nft_trans_elem { | |
954 | struct nft_set *set; | |
955 | struct nft_set_elem elem; | |
956 | }; | |
957 | ||
958 | #define nft_trans_elem_set(trans) \ | |
959 | (((struct nft_trans_elem *)trans->data)->set) | |
960 | #define nft_trans_elem(trans) \ | |
961 | (((struct nft_trans_elem *)trans->data)->elem) | |
962 | ||
96518518 | 963 | #endif /* _NET_NF_TABLES_H */ |