]>
Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
96518518 | 2 | /* |
20a69341 | 3 | * Copyright (c) 2007-2009 Patrick McHardy <kaber@trash.net> |
96518518 | 4 | * |
96518518 PM |
5 | * Development of this code funded by Astaro AG (http://www.astaro.com/) |
6 | */ | |
7 | ||
8 | #include <linux/module.h> | |
9 | #include <linux/init.h> | |
10 | #include <linux/list.h> | |
11 | #include <linux/skbuff.h> | |
12 | #include <linux/netlink.h> | |
1ff75a3e | 13 | #include <linux/vmalloc.h> |
0eb71a9d | 14 | #include <linux/rhashtable.h> |
96518518 PM |
15 | #include <linux/netfilter.h> |
16 | #include <linux/netfilter/nfnetlink.h> | |
17 | #include <linux/netfilter/nf_tables.h> | |
3b49e2e9 | 18 | #include <net/netfilter/nf_flow_table.h> |
96518518 PM |
19 | #include <net/netfilter/nf_tables_core.h> |
20 | #include <net/netfilter/nf_tables.h> | |
c9626a2c | 21 | #include <net/netfilter/nf_tables_offload.h> |
99633ab2 | 22 | #include <net/net_namespace.h> |
96518518 PM |
23 | #include <net/sock.h> |
24 | ||
9332d27d FW |
25 | #define NFT_MODULE_AUTOLOAD_LIMIT (MODULE_NAME_LEN - sizeof("nft-expr-255-")) |
26 | ||
96518518 | 27 | static LIST_HEAD(nf_tables_expressions); |
e5009240 | 28 | static LIST_HEAD(nf_tables_objects); |
3b49e2e9 | 29 | static LIST_HEAD(nf_tables_flowtables); |
0935d558 FW |
30 | static LIST_HEAD(nf_tables_destroy_list); |
31 | static DEFINE_SPINLOCK(nf_tables_destroy_list_lock); | |
3ecbfd65 | 32 | static u64 table_handle; |
96518518 | 33 | |
a654de8f PNA |
34 | enum { |
35 | NFT_VALIDATE_SKIP = 0, | |
36 | NFT_VALIDATE_NEED, | |
37 | NFT_VALIDATE_DO, | |
38 | }; | |
39 | ||
4d44175a FW |
40 | static struct rhltable nft_objname_ht; |
41 | ||
1b2470e5 FW |
42 | static u32 nft_chain_hash(const void *data, u32 len, u32 seed); |
43 | static u32 nft_chain_hash_obj(const void *data, u32 len, u32 seed); | |
44 | static int nft_chain_hash_cmp(struct rhashtable_compare_arg *, const void *); | |
45 | ||
4d44175a FW |
46 | static u32 nft_objname_hash(const void *data, u32 len, u32 seed); |
47 | static u32 nft_objname_hash_obj(const void *data, u32 len, u32 seed); | |
48 | static int nft_objname_hash_cmp(struct rhashtable_compare_arg *, const void *); | |
49 | ||
1b2470e5 FW |
50 | static const struct rhashtable_params nft_chain_ht_params = { |
51 | .head_offset = offsetof(struct nft_chain, rhlhead), | |
52 | .key_offset = offsetof(struct nft_chain, name), | |
53 | .hashfn = nft_chain_hash, | |
54 | .obj_hashfn = nft_chain_hash_obj, | |
55 | .obj_cmpfn = nft_chain_hash_cmp, | |
1b2470e5 FW |
56 | .automatic_shrinking = true, |
57 | }; | |
58 | ||
4d44175a FW |
59 | static const struct rhashtable_params nft_objname_ht_params = { |
60 | .head_offset = offsetof(struct nft_object, rhlhead), | |
61 | .key_offset = offsetof(struct nft_object, key), | |
62 | .hashfn = nft_objname_hash, | |
63 | .obj_hashfn = nft_objname_hash_obj, | |
64 | .obj_cmpfn = nft_objname_hash_cmp, | |
65 | .automatic_shrinking = true, | |
66 | }; | |
67 | ||
a654de8f PNA |
68 | static void nft_validate_state_update(struct net *net, u8 new_validate_state) |
69 | { | |
70 | switch (net->nft.validate_state) { | |
71 | case NFT_VALIDATE_SKIP: | |
72 | WARN_ON_ONCE(new_validate_state == NFT_VALIDATE_DO); | |
73 | break; | |
74 | case NFT_VALIDATE_NEED: | |
75 | break; | |
76 | case NFT_VALIDATE_DO: | |
77 | if (new_validate_state == NFT_VALIDATE_NEED) | |
78 | return; | |
79 | } | |
80 | ||
81 | net->nft.validate_state = new_validate_state; | |
82 | } | |
0935d558 FW |
83 | static void nf_tables_trans_destroy_work(struct work_struct *w); |
84 | static DECLARE_WORK(trans_destroy_work, nf_tables_trans_destroy_work); | |
a654de8f | 85 | |
7c95f6d8 | 86 | static void nft_ctx_init(struct nft_ctx *ctx, |
633c9a84 | 87 | struct net *net, |
7c95f6d8 PNA |
88 | const struct sk_buff *skb, |
89 | const struct nlmsghdr *nlh, | |
36596dad | 90 | u8 family, |
7c95f6d8 PNA |
91 | struct nft_table *table, |
92 | struct nft_chain *chain, | |
93 | const struct nlattr * const *nla) | |
94 | { | |
633c9a84 | 95 | ctx->net = net; |
36596dad | 96 | ctx->family = family; |
26b2f552 | 97 | ctx->level = 0; |
128ad332 PNA |
98 | ctx->table = table; |
99 | ctx->chain = chain; | |
100 | ctx->nla = nla; | |
101 | ctx->portid = NETLINK_CB(skb).portid; | |
102 | ctx->report = nlmsg_report(nlh); | |
c9626a2c | 103 | ctx->flags = nlh->nlmsg_flags; |
128ad332 | 104 | ctx->seq = nlh->nlmsg_seq; |
7c95f6d8 PNA |
105 | } |
106 | ||
8411b644 PNA |
107 | static struct nft_trans *nft_trans_alloc_gfp(const struct nft_ctx *ctx, |
108 | int msg_type, u32 size, gfp_t gfp) | |
1081d11b PNA |
109 | { |
110 | struct nft_trans *trans; | |
111 | ||
8411b644 | 112 | trans = kzalloc(sizeof(struct nft_trans) + size, gfp); |
1081d11b PNA |
113 | if (trans == NULL) |
114 | return NULL; | |
115 | ||
b380e5c7 | 116 | trans->msg_type = msg_type; |
1081d11b PNA |
117 | trans->ctx = *ctx; |
118 | ||
119 | return trans; | |
120 | } | |
121 | ||
8411b644 PNA |
122 | static struct nft_trans *nft_trans_alloc(const struct nft_ctx *ctx, |
123 | int msg_type, u32 size) | |
124 | { | |
125 | return nft_trans_alloc_gfp(ctx, msg_type, size, GFP_KERNEL); | |
126 | } | |
127 | ||
1081d11b PNA |
128 | static void nft_trans_destroy(struct nft_trans *trans) |
129 | { | |
130 | list_del(&trans->list); | |
131 | kfree(trans); | |
132 | } | |
133 | ||
f6ac8585 PNA |
134 | static void nft_set_trans_bind(const struct nft_ctx *ctx, struct nft_set *set) |
135 | { | |
136 | struct net *net = ctx->net; | |
137 | struct nft_trans *trans; | |
138 | ||
139 | if (!nft_set_is_anonymous(set)) | |
140 | return; | |
141 | ||
142 | list_for_each_entry_reverse(trans, &net->nft.commit_list, list) { | |
6a0a8d10 PNA |
143 | switch (trans->msg_type) { |
144 | case NFT_MSG_NEWSET: | |
145 | if (nft_trans_set(trans) == set) | |
146 | nft_trans_set_bound(trans) = true; | |
147 | break; | |
148 | case NFT_MSG_NEWSETELEM: | |
149 | if (nft_trans_elem_set(trans) == set) | |
150 | nft_trans_elem_set_bound(trans) = true; | |
f6ac8585 PNA |
151 | break; |
152 | } | |
153 | } | |
154 | } | |
155 | ||
d54725cd PNA |
156 | static int nft_netdev_register_hooks(struct net *net, |
157 | struct list_head *hook_list) | |
158 | { | |
159 | struct nft_hook *hook; | |
160 | int err, j; | |
161 | ||
162 | j = 0; | |
163 | list_for_each_entry(hook, hook_list, list) { | |
164 | err = nf_register_net_hook(net, &hook->ops); | |
165 | if (err < 0) | |
166 | goto err_register; | |
167 | ||
168 | j++; | |
169 | } | |
170 | return 0; | |
171 | ||
172 | err_register: | |
173 | list_for_each_entry(hook, hook_list, list) { | |
174 | if (j-- <= 0) | |
175 | break; | |
176 | ||
177 | nf_unregister_net_hook(net, &hook->ops); | |
178 | } | |
179 | return err; | |
180 | } | |
181 | ||
182 | static void nft_netdev_unregister_hooks(struct net *net, | |
183 | struct list_head *hook_list) | |
184 | { | |
185 | struct nft_hook *hook; | |
186 | ||
187 | list_for_each_entry(hook, hook_list, list) | |
188 | nf_unregister_net_hook(net, &hook->ops); | |
189 | } | |
190 | ||
191 | static int nft_register_basechain_hooks(struct net *net, int family, | |
192 | struct nft_base_chain *basechain) | |
193 | { | |
194 | if (family == NFPROTO_NETDEV) | |
195 | return nft_netdev_register_hooks(net, &basechain->hook_list); | |
196 | ||
197 | return nf_register_net_hook(net, &basechain->ops); | |
198 | } | |
199 | ||
200 | static void nft_unregister_basechain_hooks(struct net *net, int family, | |
201 | struct nft_base_chain *basechain) | |
202 | { | |
203 | if (family == NFPROTO_NETDEV) | |
204 | nft_netdev_unregister_hooks(net, &basechain->hook_list); | |
205 | else | |
206 | nf_unregister_net_hook(net, &basechain->ops); | |
207 | } | |
208 | ||
c974a3a3 PNA |
209 | static int nf_tables_register_hook(struct net *net, |
210 | const struct nft_table *table, | |
211 | struct nft_chain *chain) | |
d8ee8f7c | 212 | { |
d54725cd | 213 | struct nft_base_chain *basechain; |
a37061a6 | 214 | const struct nf_hook_ops *ops; |
ae6153b5 | 215 | |
d8ee8f7c | 216 | if (table->flags & NFT_TABLE_F_DORMANT || |
f323d954 | 217 | !nft_is_base_chain(chain)) |
d8ee8f7c PNA |
218 | return 0; |
219 | ||
4e25ceb8 FW |
220 | basechain = nft_base_chain(chain); |
221 | ops = &basechain->ops; | |
ae6153b5 | 222 | |
4e25ceb8 FW |
223 | if (basechain->type->ops_register) |
224 | return basechain->type->ops_register(net, ops); | |
225 | ||
d54725cd | 226 | return nft_register_basechain_hooks(net, table->family, basechain); |
d8ee8f7c PNA |
227 | } |
228 | ||
c974a3a3 PNA |
229 | static void nf_tables_unregister_hook(struct net *net, |
230 | const struct nft_table *table, | |
231 | struct nft_chain *chain) | |
c5598794 | 232 | { |
d54725cd | 233 | struct nft_base_chain *basechain; |
4e25ceb8 FW |
234 | const struct nf_hook_ops *ops; |
235 | ||
d8ee8f7c | 236 | if (table->flags & NFT_TABLE_F_DORMANT || |
f323d954 | 237 | !nft_is_base_chain(chain)) |
d8ee8f7c | 238 | return; |
4e25ceb8 FW |
239 | basechain = nft_base_chain(chain); |
240 | ops = &basechain->ops; | |
d8ee8f7c | 241 | |
4e25ceb8 FW |
242 | if (basechain->type->ops_unregister) |
243 | return basechain->type->ops_unregister(net, ops); | |
244 | ||
d54725cd | 245 | nft_unregister_basechain_hooks(net, table->family, basechain); |
c5598794 AB |
246 | } |
247 | ||
ee01d542 AB |
248 | static int nft_trans_table_add(struct nft_ctx *ctx, int msg_type) |
249 | { | |
250 | struct nft_trans *trans; | |
251 | ||
252 | trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_table)); | |
253 | if (trans == NULL) | |
254 | return -ENOMEM; | |
255 | ||
256 | if (msg_type == NFT_MSG_NEWTABLE) | |
f2a6d766 | 257 | nft_activate_next(ctx->net, ctx->table); |
ee01d542 AB |
258 | |
259 | list_add_tail(&trans->list, &ctx->net->nft.commit_list); | |
260 | return 0; | |
261 | } | |
262 | ||
263 | static int nft_deltable(struct nft_ctx *ctx) | |
264 | { | |
265 | int err; | |
266 | ||
267 | err = nft_trans_table_add(ctx, NFT_MSG_DELTABLE); | |
268 | if (err < 0) | |
269 | return err; | |
270 | ||
f2a6d766 | 271 | nft_deactivate_next(ctx->net, ctx->table); |
ee01d542 AB |
272 | return err; |
273 | } | |
274 | ||
66293c46 | 275 | static struct nft_trans *nft_trans_chain_add(struct nft_ctx *ctx, int msg_type) |
ee01d542 AB |
276 | { |
277 | struct nft_trans *trans; | |
278 | ||
279 | trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_chain)); | |
280 | if (trans == NULL) | |
66293c46 | 281 | return ERR_PTR(-ENOMEM); |
ee01d542 | 282 | |
74cccc3d | 283 | if (msg_type == NFT_MSG_NEWCHAIN) { |
664b0f8c | 284 | nft_activate_next(ctx->net, ctx->chain); |
ee01d542 | 285 | |
74cccc3d PNA |
286 | if (ctx->nla[NFTA_CHAIN_ID]) { |
287 | nft_trans_chain_id(trans) = | |
288 | ntohl(nla_get_be32(ctx->nla[NFTA_CHAIN_ID])); | |
289 | } | |
290 | } | |
291 | ||
ee01d542 | 292 | list_add_tail(&trans->list, &ctx->net->nft.commit_list); |
66293c46 | 293 | return trans; |
ee01d542 AB |
294 | } |
295 | ||
296 | static int nft_delchain(struct nft_ctx *ctx) | |
297 | { | |
66293c46 | 298 | struct nft_trans *trans; |
ee01d542 | 299 | |
66293c46 FW |
300 | trans = nft_trans_chain_add(ctx, NFT_MSG_DELCHAIN); |
301 | if (IS_ERR(trans)) | |
302 | return PTR_ERR(trans); | |
ee01d542 AB |
303 | |
304 | ctx->table->use--; | |
664b0f8c | 305 | nft_deactivate_next(ctx->net, ctx->chain); |
ee01d542 | 306 | |
66293c46 | 307 | return 0; |
ee01d542 AB |
308 | } |
309 | ||
bb7b40ae PNA |
310 | static void nft_rule_expr_activate(const struct nft_ctx *ctx, |
311 | struct nft_rule *rule) | |
312 | { | |
313 | struct nft_expr *expr; | |
314 | ||
315 | expr = nft_expr_first(rule); | |
316 | while (expr != nft_expr_last(rule) && expr->ops) { | |
317 | if (expr->ops->activate) | |
318 | expr->ops->activate(ctx, expr); | |
319 | ||
320 | expr = nft_expr_next(expr); | |
321 | } | |
322 | } | |
323 | ||
324 | static void nft_rule_expr_deactivate(const struct nft_ctx *ctx, | |
f6ac8585 PNA |
325 | struct nft_rule *rule, |
326 | enum nft_trans_phase phase) | |
bb7b40ae PNA |
327 | { |
328 | struct nft_expr *expr; | |
329 | ||
330 | expr = nft_expr_first(rule); | |
331 | while (expr != nft_expr_last(rule) && expr->ops) { | |
332 | if (expr->ops->deactivate) | |
f6ac8585 | 333 | expr->ops->deactivate(ctx, expr, phase); |
bb7b40ae PNA |
334 | |
335 | expr = nft_expr_next(expr); | |
336 | } | |
337 | } | |
338 | ||
ee01d542 AB |
339 | static int |
340 | nf_tables_delrule_deactivate(struct nft_ctx *ctx, struct nft_rule *rule) | |
341 | { | |
342 | /* You cannot delete the same rule twice */ | |
889f7ee7 PNA |
343 | if (nft_is_active_next(ctx->net, rule)) { |
344 | nft_deactivate_next(ctx->net, rule); | |
ee01d542 AB |
345 | ctx->chain->use--; |
346 | return 0; | |
347 | } | |
348 | return -ENOENT; | |
349 | } | |
350 | ||
351 | static struct nft_trans *nft_trans_rule_add(struct nft_ctx *ctx, int msg_type, | |
352 | struct nft_rule *rule) | |
353 | { | |
354 | struct nft_trans *trans; | |
355 | ||
356 | trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_rule)); | |
357 | if (trans == NULL) | |
358 | return NULL; | |
359 | ||
1a94e38d PNA |
360 | if (msg_type == NFT_MSG_NEWRULE && ctx->nla[NFTA_RULE_ID] != NULL) { |
361 | nft_trans_rule_id(trans) = | |
362 | ntohl(nla_get_be32(ctx->nla[NFTA_RULE_ID])); | |
363 | } | |
ee01d542 AB |
364 | nft_trans_rule(trans) = rule; |
365 | list_add_tail(&trans->list, &ctx->net->nft.commit_list); | |
366 | ||
367 | return trans; | |
368 | } | |
369 | ||
370 | static int nft_delrule(struct nft_ctx *ctx, struct nft_rule *rule) | |
371 | { | |
63b48c73 | 372 | struct nft_flow_rule *flow; |
ee01d542 AB |
373 | struct nft_trans *trans; |
374 | int err; | |
375 | ||
376 | trans = nft_trans_rule_add(ctx, NFT_MSG_DELRULE, rule); | |
377 | if (trans == NULL) | |
378 | return -ENOMEM; | |
379 | ||
63b48c73 PNA |
380 | if (ctx->chain->flags & NFT_CHAIN_HW_OFFLOAD) { |
381 | flow = nft_flow_rule_create(ctx->net, rule); | |
382 | if (IS_ERR(flow)) { | |
383 | nft_trans_destroy(trans); | |
384 | return PTR_ERR(flow); | |
385 | } | |
386 | ||
387 | nft_trans_flow_rule(trans) = flow; | |
388 | } | |
389 | ||
ee01d542 AB |
390 | err = nf_tables_delrule_deactivate(ctx, rule); |
391 | if (err < 0) { | |
392 | nft_trans_destroy(trans); | |
393 | return err; | |
394 | } | |
f6ac8585 | 395 | nft_rule_expr_deactivate(ctx, rule, NFT_TRANS_PREPARE); |
ee01d542 AB |
396 | |
397 | return 0; | |
398 | } | |
399 | ||
400 | static int nft_delrule_by_chain(struct nft_ctx *ctx) | |
401 | { | |
402 | struct nft_rule *rule; | |
403 | int err; | |
404 | ||
405 | list_for_each_entry(rule, &ctx->chain->rules, list) { | |
23b7ca4f PNA |
406 | if (!nft_is_active_next(ctx->net, rule)) |
407 | continue; | |
408 | ||
ee01d542 AB |
409 | err = nft_delrule(ctx, rule); |
410 | if (err < 0) | |
411 | return err; | |
412 | } | |
413 | return 0; | |
414 | } | |
415 | ||
cd5125d8 | 416 | static int nft_trans_set_add(const struct nft_ctx *ctx, int msg_type, |
ee01d542 AB |
417 | struct nft_set *set) |
418 | { | |
419 | struct nft_trans *trans; | |
420 | ||
421 | trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_set)); | |
422 | if (trans == NULL) | |
423 | return -ENOMEM; | |
424 | ||
425 | if (msg_type == NFT_MSG_NEWSET && ctx->nla[NFTA_SET_ID] != NULL) { | |
426 | nft_trans_set_id(trans) = | |
427 | ntohl(nla_get_be32(ctx->nla[NFTA_SET_ID])); | |
37a9cc52 | 428 | nft_activate_next(ctx->net, set); |
ee01d542 AB |
429 | } |
430 | nft_trans_set(trans) = set; | |
431 | list_add_tail(&trans->list, &ctx->net->nft.commit_list); | |
432 | ||
433 | return 0; | |
434 | } | |
435 | ||
cd5125d8 | 436 | static int nft_delset(const struct nft_ctx *ctx, struct nft_set *set) |
ee01d542 AB |
437 | { |
438 | int err; | |
439 | ||
440 | err = nft_trans_set_add(ctx, NFT_MSG_DELSET, set); | |
441 | if (err < 0) | |
442 | return err; | |
443 | ||
37a9cc52 | 444 | nft_deactivate_next(ctx->net, set); |
ee01d542 AB |
445 | ctx->table->use--; |
446 | ||
447 | return err; | |
448 | } | |
449 | ||
e5009240 PNA |
450 | static int nft_trans_obj_add(struct nft_ctx *ctx, int msg_type, |
451 | struct nft_object *obj) | |
452 | { | |
453 | struct nft_trans *trans; | |
454 | ||
455 | trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_obj)); | |
456 | if (trans == NULL) | |
457 | return -ENOMEM; | |
458 | ||
459 | if (msg_type == NFT_MSG_NEWOBJ) | |
460 | nft_activate_next(ctx->net, obj); | |
461 | ||
462 | nft_trans_obj(trans) = obj; | |
463 | list_add_tail(&trans->list, &ctx->net->nft.commit_list); | |
464 | ||
465 | return 0; | |
466 | } | |
467 | ||
468 | static int nft_delobj(struct nft_ctx *ctx, struct nft_object *obj) | |
469 | { | |
470 | int err; | |
471 | ||
472 | err = nft_trans_obj_add(ctx, NFT_MSG_DELOBJ, obj); | |
473 | if (err < 0) | |
474 | return err; | |
475 | ||
476 | nft_deactivate_next(ctx->net, obj); | |
477 | ctx->table->use--; | |
478 | ||
479 | return err; | |
480 | } | |
481 | ||
3b49e2e9 PNA |
482 | static int nft_trans_flowtable_add(struct nft_ctx *ctx, int msg_type, |
483 | struct nft_flowtable *flowtable) | |
484 | { | |
485 | struct nft_trans *trans; | |
486 | ||
487 | trans = nft_trans_alloc(ctx, msg_type, | |
488 | sizeof(struct nft_trans_flowtable)); | |
489 | if (trans == NULL) | |
490 | return -ENOMEM; | |
491 | ||
492 | if (msg_type == NFT_MSG_NEWFLOWTABLE) | |
493 | nft_activate_next(ctx->net, flowtable); | |
494 | ||
495 | nft_trans_flowtable(trans) = flowtable; | |
496 | list_add_tail(&trans->list, &ctx->net->nft.commit_list); | |
497 | ||
498 | return 0; | |
499 | } | |
500 | ||
501 | static int nft_delflowtable(struct nft_ctx *ctx, | |
502 | struct nft_flowtable *flowtable) | |
503 | { | |
504 | int err; | |
505 | ||
506 | err = nft_trans_flowtable_add(ctx, NFT_MSG_DELFLOWTABLE, flowtable); | |
507 | if (err < 0) | |
508 | return err; | |
509 | ||
510 | nft_deactivate_next(ctx->net, flowtable); | |
511 | ctx->table->use--; | |
512 | ||
513 | return err; | |
514 | } | |
515 | ||
96518518 PM |
516 | /* |
517 | * Tables | |
518 | */ | |
519 | ||
36596dad | 520 | static struct nft_table *nft_table_lookup(const struct net *net, |
f2a6d766 | 521 | const struct nlattr *nla, |
36596dad | 522 | u8 family, u8 genmask) |
96518518 PM |
523 | { |
524 | struct nft_table *table; | |
525 | ||
cac20fcd PNA |
526 | if (nla == NULL) |
527 | return ERR_PTR(-EINVAL); | |
528 | ||
0a6a9515 QC |
529 | list_for_each_entry_rcu(table, &net->nft.tables, list, |
530 | lockdep_is_held(&net->nft.commit_mutex)) { | |
f2a6d766 | 531 | if (!nla_strcmp(nla, table->name) && |
98319cb9 | 532 | table->family == family && |
f2a6d766 | 533 | nft_active_genmask(table, genmask)) |
96518518 PM |
534 | return table; |
535 | } | |
cac20fcd PNA |
536 | |
537 | return ERR_PTR(-ENOENT); | |
96518518 PM |
538 | } |
539 | ||
3ecbfd65 HS |
540 | static struct nft_table *nft_table_lookup_byhandle(const struct net *net, |
541 | const struct nlattr *nla, | |
542 | u8 genmask) | |
543 | { | |
544 | struct nft_table *table; | |
545 | ||
546 | list_for_each_entry(table, &net->nft.tables, list) { | |
547 | if (be64_to_cpu(nla_get_be64(nla)) == table->handle && | |
548 | nft_active_genmask(table, genmask)) | |
549 | return table; | |
550 | } | |
3ecbfd65 HS |
551 | |
552 | return ERR_PTR(-ENOENT); | |
553 | } | |
554 | ||
96518518 PM |
555 | static inline u64 nf_tables_alloc_handle(struct nft_table *table) |
556 | { | |
557 | return ++table->hgenerator; | |
558 | } | |
559 | ||
32537e91 | 560 | static const struct nft_chain_type *chain_type[NFPROTO_NUMPROTO][NFT_CHAIN_T_MAX]; |
9370761c | 561 | |
82603549 PNA |
562 | static const struct nft_chain_type * |
563 | __nft_chain_type_get(u8 family, enum nft_chain_types type) | |
564 | { | |
565 | if (family >= NFPROTO_NUMPROTO || | |
566 | type >= NFT_CHAIN_T_MAX) | |
567 | return NULL; | |
568 | ||
569 | return chain_type[family][type]; | |
570 | } | |
571 | ||
32537e91 | 572 | static const struct nft_chain_type * |
1ea26cca | 573 | __nf_tables_chain_type_lookup(const struct nlattr *nla, u8 family) |
9370761c | 574 | { |
82603549 | 575 | const struct nft_chain_type *type; |
9370761c PNA |
576 | int i; |
577 | ||
baae3e62 | 578 | for (i = 0; i < NFT_CHAIN_T_MAX; i++) { |
82603549 PNA |
579 | type = __nft_chain_type_get(family, i); |
580 | if (!type) | |
581 | continue; | |
582 | if (!nla_strcmp(nla, type->name)) | |
583 | return type; | |
9370761c | 584 | } |
baae3e62 | 585 | return NULL; |
9370761c PNA |
586 | } |
587 | ||
eb014de4 PNA |
588 | struct nft_module_request { |
589 | struct list_head list; | |
590 | char module[MODULE_NAME_LEN]; | |
591 | bool done; | |
592 | }; | |
593 | ||
452238e8 | 594 | #ifdef CONFIG_MODULES |
eb014de4 | 595 | static int nft_request_module(struct net *net, const char *fmt, ...) |
452238e8 FW |
596 | { |
597 | char module_name[MODULE_NAME_LEN]; | |
eb014de4 | 598 | struct nft_module_request *req; |
452238e8 FW |
599 | va_list args; |
600 | int ret; | |
601 | ||
452238e8 FW |
602 | va_start(args, fmt); |
603 | ret = vsnprintf(module_name, MODULE_NAME_LEN, fmt, args); | |
604 | va_end(args); | |
9332d27d | 605 | if (ret >= MODULE_NAME_LEN) |
eb014de4 | 606 | return 0; |
452238e8 | 607 | |
eb014de4 PNA |
608 | list_for_each_entry(req, &net->nft.module_list, list) { |
609 | if (!strcmp(req->module, module_name)) { | |
610 | if (req->done) | |
611 | return 0; | |
ec7470b8 | 612 | |
eb014de4 PNA |
613 | /* A request to load this module already exists. */ |
614 | return -EAGAIN; | |
615 | } | |
616 | } | |
617 | ||
618 | req = kmalloc(sizeof(*req), GFP_KERNEL); | |
619 | if (!req) | |
620 | return -ENOMEM; | |
621 | ||
622 | req->done = false; | |
623 | strlcpy(req->module, module_name, MODULE_NAME_LEN); | |
624 | list_add_tail(&req->list, &net->nft.module_list); | |
625 | ||
626 | return -EAGAIN; | |
452238e8 FW |
627 | } |
628 | #endif | |
629 | ||
f102d66b FW |
630 | static void lockdep_nfnl_nft_mutex_not_held(void) |
631 | { | |
632 | #ifdef CONFIG_PROVE_LOCKING | |
633 | WARN_ON_ONCE(lockdep_nfnl_is_held(NFNL_SUBSYS_NFTABLES)); | |
634 | #endif | |
635 | } | |
636 | ||
32537e91 | 637 | static const struct nft_chain_type * |
452238e8 FW |
638 | nf_tables_chain_type_lookup(struct net *net, const struct nlattr *nla, |
639 | u8 family, bool autoload) | |
9370761c | 640 | { |
32537e91 | 641 | const struct nft_chain_type *type; |
9370761c | 642 | |
1ea26cca | 643 | type = __nf_tables_chain_type_lookup(nla, family); |
93b0806f PM |
644 | if (type != NULL) |
645 | return type; | |
f102d66b FW |
646 | |
647 | lockdep_nfnl_nft_mutex_not_held(); | |
9370761c | 648 | #ifdef CONFIG_MODULES |
93b0806f | 649 | if (autoload) { |
eb014de4 PNA |
650 | if (nft_request_module(net, "nft-chain-%u-%.*s", family, |
651 | nla_len(nla), | |
652 | (const char *)nla_data(nla)) == -EAGAIN) | |
93b0806f | 653 | return ERR_PTR(-EAGAIN); |
9370761c PNA |
654 | } |
655 | #endif | |
93b0806f | 656 | return ERR_PTR(-ENOENT); |
9370761c PNA |
657 | } |
658 | ||
96518518 | 659 | static const struct nla_policy nft_table_policy[NFTA_TABLE_MAX + 1] = { |
1cae565e PNA |
660 | [NFTA_TABLE_NAME] = { .type = NLA_STRING, |
661 | .len = NFT_TABLE_MAXNAMELEN - 1 }, | |
9ddf6323 | 662 | [NFTA_TABLE_FLAGS] = { .type = NLA_U32 }, |
3ecbfd65 | 663 | [NFTA_TABLE_HANDLE] = { .type = NLA_U64 }, |
96518518 PM |
664 | }; |
665 | ||
84d7fce6 PNA |
666 | static int nf_tables_fill_table_info(struct sk_buff *skb, struct net *net, |
667 | u32 portid, u32 seq, int event, u32 flags, | |
668 | int family, const struct nft_table *table) | |
96518518 PM |
669 | { |
670 | struct nlmsghdr *nlh; | |
671 | struct nfgenmsg *nfmsg; | |
672 | ||
dedb67c4 | 673 | event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); |
96518518 PM |
674 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags); |
675 | if (nlh == NULL) | |
676 | goto nla_put_failure; | |
677 | ||
678 | nfmsg = nlmsg_data(nlh); | |
679 | nfmsg->nfgen_family = family; | |
680 | nfmsg->version = NFNETLINK_V0; | |
84d7fce6 | 681 | nfmsg->res_id = htons(net->nft.base_seq & 0xffff); |
96518518 | 682 | |
9ddf6323 | 683 | if (nla_put_string(skb, NFTA_TABLE_NAME, table->name) || |
d8bcc768 | 684 | nla_put_be32(skb, NFTA_TABLE_FLAGS, htonl(table->flags)) || |
3ecbfd65 HS |
685 | nla_put_be32(skb, NFTA_TABLE_USE, htonl(table->use)) || |
686 | nla_put_be64(skb, NFTA_TABLE_HANDLE, cpu_to_be64(table->handle), | |
687 | NFTA_TABLE_PAD)) | |
96518518 PM |
688 | goto nla_put_failure; |
689 | ||
053c095a JB |
690 | nlmsg_end(skb, nlh); |
691 | return 0; | |
96518518 PM |
692 | |
693 | nla_put_failure: | |
694 | nlmsg_trim(skb, nlh); | |
695 | return -1; | |
696 | } | |
697 | ||
25e94a99 | 698 | static void nf_tables_table_notify(const struct nft_ctx *ctx, int event) |
96518518 PM |
699 | { |
700 | struct sk_buff *skb; | |
96518518 PM |
701 | int err; |
702 | ||
128ad332 PNA |
703 | if (!ctx->report && |
704 | !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES)) | |
25e94a99 | 705 | return; |
96518518 | 706 | |
96518518 PM |
707 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
708 | if (skb == NULL) | |
709 | goto err; | |
710 | ||
84d7fce6 | 711 | err = nf_tables_fill_table_info(skb, ctx->net, ctx->portid, ctx->seq, |
36596dad | 712 | event, 0, ctx->family, ctx->table); |
96518518 PM |
713 | if (err < 0) { |
714 | kfree_skb(skb); | |
715 | goto err; | |
716 | } | |
717 | ||
25e94a99 PNA |
718 | nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES, |
719 | ctx->report, GFP_KERNEL); | |
720 | return; | |
96518518 | 721 | err: |
25e94a99 | 722 | nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS); |
96518518 PM |
723 | } |
724 | ||
725 | static int nf_tables_dump_tables(struct sk_buff *skb, | |
726 | struct netlink_callback *cb) | |
727 | { | |
728 | const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); | |
96518518 PM |
729 | const struct nft_table *table; |
730 | unsigned int idx = 0, s_idx = cb->args[0]; | |
99633ab2 | 731 | struct net *net = sock_net(skb->sk); |
96518518 PM |
732 | int family = nfmsg->nfgen_family; |
733 | ||
e688a7f8 | 734 | rcu_read_lock(); |
38e029f1 PNA |
735 | cb->seq = net->nft.base_seq; |
736 | ||
36596dad | 737 | list_for_each_entry_rcu(table, &net->nft.tables, list) { |
98319cb9 | 738 | if (family != NFPROTO_UNSPEC && family != table->family) |
96518518 PM |
739 | continue; |
740 | ||
36596dad PNA |
741 | if (idx < s_idx) |
742 | goto cont; | |
743 | if (idx > s_idx) | |
744 | memset(&cb->args[1], 0, | |
745 | sizeof(cb->args) - sizeof(cb->args[0])); | |
746 | if (!nft_is_active(net, table)) | |
747 | continue; | |
748 | if (nf_tables_fill_table_info(skb, net, | |
749 | NETLINK_CB(cb->skb).portid, | |
750 | cb->nlh->nlmsg_seq, | |
751 | NFT_MSG_NEWTABLE, NLM_F_MULTI, | |
98319cb9 | 752 | table->family, table) < 0) |
36596dad PNA |
753 | goto done; |
754 | ||
755 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); | |
96518518 | 756 | cont: |
36596dad | 757 | idx++; |
96518518 PM |
758 | } |
759 | done: | |
e688a7f8 | 760 | rcu_read_unlock(); |
96518518 PM |
761 | cb->args[0] = idx; |
762 | return skb->len; | |
763 | } | |
764 | ||
d9adf22a FW |
765 | static int nft_netlink_dump_start_rcu(struct sock *nlsk, struct sk_buff *skb, |
766 | const struct nlmsghdr *nlh, | |
767 | struct netlink_dump_control *c) | |
768 | { | |
769 | int err; | |
770 | ||
771 | if (!try_module_get(THIS_MODULE)) | |
772 | return -EINVAL; | |
773 | ||
774 | rcu_read_unlock(); | |
775 | err = netlink_dump_start(nlsk, skb, nlh, c); | |
776 | rcu_read_lock(); | |
777 | module_put(THIS_MODULE); | |
778 | ||
779 | return err; | |
780 | } | |
781 | ||
782 | /* called with rcu_read_lock held */ | |
7b8002a1 PNA |
783 | static int nf_tables_gettable(struct net *net, struct sock *nlsk, |
784 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
04ba724b PNA |
785 | const struct nlattr * const nla[], |
786 | struct netlink_ext_ack *extack) | |
96518518 PM |
787 | { |
788 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
f2a6d766 | 789 | u8 genmask = nft_genmask_cur(net); |
96518518 PM |
790 | const struct nft_table *table; |
791 | struct sk_buff *skb2; | |
792 | int family = nfmsg->nfgen_family; | |
793 | int err; | |
794 | ||
795 | if (nlh->nlmsg_flags & NLM_F_DUMP) { | |
796 | struct netlink_dump_control c = { | |
797 | .dump = nf_tables_dump_tables, | |
d9adf22a | 798 | .module = THIS_MODULE, |
96518518 | 799 | }; |
d9adf22a FW |
800 | |
801 | return nft_netlink_dump_start_rcu(nlsk, skb, nlh, &c); | |
96518518 PM |
802 | } |
803 | ||
cac20fcd | 804 | table = nft_table_lookup(net, nla[NFTA_TABLE_NAME], family, genmask); |
36dd1bcc PNA |
805 | if (IS_ERR(table)) { |
806 | NL_SET_BAD_ATTR(extack, nla[NFTA_TABLE_NAME]); | |
96518518 | 807 | return PTR_ERR(table); |
36dd1bcc | 808 | } |
96518518 | 809 | |
d9adf22a | 810 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
96518518 PM |
811 | if (!skb2) |
812 | return -ENOMEM; | |
813 | ||
84d7fce6 | 814 | err = nf_tables_fill_table_info(skb2, net, NETLINK_CB(skb).portid, |
96518518 PM |
815 | nlh->nlmsg_seq, NFT_MSG_NEWTABLE, 0, |
816 | family, table); | |
817 | if (err < 0) | |
818 | goto err; | |
819 | ||
820 | return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid); | |
821 | ||
822 | err: | |
823 | kfree_skb(skb2); | |
824 | return err; | |
825 | } | |
826 | ||
c9c17211 | 827 | static void nft_table_disable(struct net *net, struct nft_table *table, u32 cnt) |
10435c11 F |
828 | { |
829 | struct nft_chain *chain; | |
830 | u32 i = 0; | |
831 | ||
832 | list_for_each_entry(chain, &table->chains, list) { | |
833 | if (!nft_is_active_next(net, chain)) | |
834 | continue; | |
f323d954 | 835 | if (!nft_is_base_chain(chain)) |
10435c11 F |
836 | continue; |
837 | ||
838 | if (cnt && i++ == cnt) | |
839 | break; | |
840 | ||
d54725cd PNA |
841 | nft_unregister_basechain_hooks(net, table->family, |
842 | nft_base_chain(chain)); | |
10435c11 F |
843 | } |
844 | } | |
845 | ||
c9c17211 | 846 | static int nf_tables_table_enable(struct net *net, struct nft_table *table) |
9ddf6323 PNA |
847 | { |
848 | struct nft_chain *chain; | |
849 | int err, i = 0; | |
850 | ||
851 | list_for_each_entry(chain, &table->chains, list) { | |
664b0f8c PNA |
852 | if (!nft_is_active_next(net, chain)) |
853 | continue; | |
f323d954 | 854 | if (!nft_is_base_chain(chain)) |
d2012975 PNA |
855 | continue; |
856 | ||
d54725cd PNA |
857 | err = nft_register_basechain_hooks(net, table->family, |
858 | nft_base_chain(chain)); | |
9ddf6323 | 859 | if (err < 0) |
d54725cd | 860 | goto err_register_hooks; |
9ddf6323 PNA |
861 | |
862 | i++; | |
863 | } | |
864 | return 0; | |
d54725cd PNA |
865 | |
866 | err_register_hooks: | |
10435c11 | 867 | if (i) |
c9c17211 | 868 | nft_table_disable(net, table, i); |
9ddf6323 PNA |
869 | return err; |
870 | } | |
871 | ||
c9c17211 | 872 | static void nf_tables_table_disable(struct net *net, struct nft_table *table) |
9ddf6323 | 873 | { |
c9c17211 | 874 | nft_table_disable(net, table, 0); |
9ddf6323 PNA |
875 | } |
876 | ||
e1aaca93 | 877 | static int nf_tables_updtable(struct nft_ctx *ctx) |
9ddf6323 | 878 | { |
55dd6f93 | 879 | struct nft_trans *trans; |
e1aaca93 | 880 | u32 flags; |
55dd6f93 | 881 | int ret = 0; |
9ddf6323 | 882 | |
e1aaca93 PNA |
883 | if (!ctx->nla[NFTA_TABLE_FLAGS]) |
884 | return 0; | |
9ddf6323 | 885 | |
e1aaca93 PNA |
886 | flags = ntohl(nla_get_be32(ctx->nla[NFTA_TABLE_FLAGS])); |
887 | if (flags & ~NFT_TABLE_F_DORMANT) | |
888 | return -EINVAL; | |
889 | ||
63283dd2 PNA |
890 | if (flags == ctx->table->flags) |
891 | return 0; | |
892 | ||
55dd6f93 PNA |
893 | trans = nft_trans_alloc(ctx, NFT_MSG_NEWTABLE, |
894 | sizeof(struct nft_trans_table)); | |
895 | if (trans == NULL) | |
896 | return -ENOMEM; | |
9ddf6323 | 897 | |
e1aaca93 PNA |
898 | if ((flags & NFT_TABLE_F_DORMANT) && |
899 | !(ctx->table->flags & NFT_TABLE_F_DORMANT)) { | |
55dd6f93 | 900 | nft_trans_table_enable(trans) = false; |
e1aaca93 PNA |
901 | } else if (!(flags & NFT_TABLE_F_DORMANT) && |
902 | ctx->table->flags & NFT_TABLE_F_DORMANT) { | |
c9c17211 | 903 | ret = nf_tables_table_enable(ctx->net, ctx->table); |
55dd6f93 | 904 | if (ret >= 0) { |
e1aaca93 | 905 | ctx->table->flags &= ~NFT_TABLE_F_DORMANT; |
55dd6f93 | 906 | nft_trans_table_enable(trans) = true; |
9ddf6323 | 907 | } |
9ddf6323 | 908 | } |
e1aaca93 PNA |
909 | if (ret < 0) |
910 | goto err; | |
9ddf6323 | 911 | |
55dd6f93 PNA |
912 | nft_trans_table_update(trans) = true; |
913 | list_add_tail(&trans->list, &ctx->net->nft.commit_list); | |
914 | return 0; | |
9ddf6323 | 915 | err: |
55dd6f93 | 916 | nft_trans_destroy(trans); |
9ddf6323 PNA |
917 | return ret; |
918 | } | |
919 | ||
1b2470e5 FW |
920 | static u32 nft_chain_hash(const void *data, u32 len, u32 seed) |
921 | { | |
922 | const char *name = data; | |
923 | ||
924 | return jhash(name, strlen(name), seed); | |
925 | } | |
926 | ||
927 | static u32 nft_chain_hash_obj(const void *data, u32 len, u32 seed) | |
928 | { | |
929 | const struct nft_chain *chain = data; | |
930 | ||
931 | return nft_chain_hash(chain->name, 0, seed); | |
932 | } | |
933 | ||
934 | static int nft_chain_hash_cmp(struct rhashtable_compare_arg *arg, | |
935 | const void *ptr) | |
936 | { | |
937 | const struct nft_chain *chain = ptr; | |
938 | const char *name = arg->key; | |
939 | ||
940 | return strcmp(chain->name, name); | |
941 | } | |
942 | ||
4d44175a FW |
943 | static u32 nft_objname_hash(const void *data, u32 len, u32 seed) |
944 | { | |
945 | const struct nft_object_hash_key *k = data; | |
946 | ||
947 | seed ^= hash_ptr(k->table, 32); | |
948 | ||
949 | return jhash(k->name, strlen(k->name), seed); | |
950 | } | |
951 | ||
952 | static u32 nft_objname_hash_obj(const void *data, u32 len, u32 seed) | |
953 | { | |
954 | const struct nft_object *obj = data; | |
955 | ||
956 | return nft_objname_hash(&obj->key, 0, seed); | |
957 | } | |
958 | ||
959 | static int nft_objname_hash_cmp(struct rhashtable_compare_arg *arg, | |
960 | const void *ptr) | |
961 | { | |
962 | const struct nft_object_hash_key *k = arg->key; | |
963 | const struct nft_object *obj = ptr; | |
964 | ||
965 | if (obj->key.table != k->table) | |
966 | return -1; | |
967 | ||
968 | return strcmp(obj->key.name, k->name); | |
969 | } | |
970 | ||
633c9a84 PNA |
971 | static int nf_tables_newtable(struct net *net, struct sock *nlsk, |
972 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
04ba724b PNA |
973 | const struct nlattr * const nla[], |
974 | struct netlink_ext_ack *extack) | |
96518518 PM |
975 | { |
976 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
f2a6d766 | 977 | u8 genmask = nft_genmask_next(net); |
96518518 | 978 | int family = nfmsg->nfgen_family; |
36dd1bcc PNA |
979 | const struct nlattr *attr; |
980 | struct nft_table *table; | |
c5c1f975 | 981 | u32 flags = 0; |
e1aaca93 | 982 | struct nft_ctx ctx; |
55dd6f93 | 983 | int err; |
96518518 | 984 | |
f102d66b | 985 | lockdep_assert_held(&net->nft.commit_mutex); |
36dd1bcc PNA |
986 | attr = nla[NFTA_TABLE_NAME]; |
987 | table = nft_table_lookup(net, attr, family, genmask); | |
96518518 PM |
988 | if (IS_ERR(table)) { |
989 | if (PTR_ERR(table) != -ENOENT) | |
990 | return PTR_ERR(table); | |
1a28ad74 | 991 | } else { |
36dd1bcc PNA |
992 | if (nlh->nlmsg_flags & NLM_F_EXCL) { |
993 | NL_SET_BAD_ATTR(extack, attr); | |
96518518 | 994 | return -EEXIST; |
36dd1bcc | 995 | } |
96518518 PM |
996 | if (nlh->nlmsg_flags & NLM_F_REPLACE) |
997 | return -EOPNOTSUPP; | |
e1aaca93 | 998 | |
98319cb9 | 999 | nft_ctx_init(&ctx, net, skb, nlh, family, table, NULL, nla); |
e1aaca93 | 1000 | return nf_tables_updtable(&ctx); |
96518518 PM |
1001 | } |
1002 | ||
c5c1f975 PM |
1003 | if (nla[NFTA_TABLE_FLAGS]) { |
1004 | flags = ntohl(nla_get_be32(nla[NFTA_TABLE_FLAGS])); | |
1005 | if (flags & ~NFT_TABLE_F_DORMANT) | |
1006 | return -EINVAL; | |
1007 | } | |
1008 | ||
ffdb210e | 1009 | err = -ENOMEM; |
1cae565e | 1010 | table = kzalloc(sizeof(*table), GFP_KERNEL); |
ffdb210e | 1011 | if (table == NULL) |
98319cb9 | 1012 | goto err_kzalloc; |
96518518 | 1013 | |
36dd1bcc | 1014 | table->name = nla_strdup(attr, GFP_KERNEL); |
e46abbcc | 1015 | if (table->name == NULL) |
98319cb9 | 1016 | goto err_strdup; |
e46abbcc | 1017 | |
1b2470e5 FW |
1018 | err = rhltable_init(&table->chains_ht, &nft_chain_ht_params); |
1019 | if (err) | |
1020 | goto err_chain_ht; | |
1021 | ||
96518518 | 1022 | INIT_LIST_HEAD(&table->chains); |
20a69341 | 1023 | INIT_LIST_HEAD(&table->sets); |
e5009240 | 1024 | INIT_LIST_HEAD(&table->objects); |
3b49e2e9 | 1025 | INIT_LIST_HEAD(&table->flowtables); |
98319cb9 | 1026 | table->family = family; |
c5c1f975 | 1027 | table->flags = flags; |
3ecbfd65 | 1028 | table->handle = ++table_handle; |
9ddf6323 | 1029 | |
98319cb9 | 1030 | nft_ctx_init(&ctx, net, skb, nlh, family, table, NULL, nla); |
55dd6f93 | 1031 | err = nft_trans_table_add(&ctx, NFT_MSG_NEWTABLE); |
ffdb210e | 1032 | if (err < 0) |
98319cb9 | 1033 | goto err_trans; |
ffdb210e | 1034 | |
36596dad | 1035 | list_add_tail_rcu(&table->list, &net->nft.tables); |
96518518 | 1036 | return 0; |
98319cb9 | 1037 | err_trans: |
1b2470e5 FW |
1038 | rhltable_destroy(&table->chains_ht); |
1039 | err_chain_ht: | |
e46abbcc | 1040 | kfree(table->name); |
98319cb9 | 1041 | err_strdup: |
ffdb210e | 1042 | kfree(table); |
98319cb9 | 1043 | err_kzalloc: |
ffdb210e | 1044 | return err; |
96518518 PM |
1045 | } |
1046 | ||
b9ac12ef AB |
1047 | static int nft_flush_table(struct nft_ctx *ctx) |
1048 | { | |
3b49e2e9 | 1049 | struct nft_flowtable *flowtable, *nft; |
b9ac12ef | 1050 | struct nft_chain *chain, *nc; |
e5009240 | 1051 | struct nft_object *obj, *ne; |
b9ac12ef | 1052 | struct nft_set *set, *ns; |
3b49e2e9 | 1053 | int err; |
b9ac12ef | 1054 | |
a2f18db0 | 1055 | list_for_each_entry(chain, &ctx->table->chains, list) { |
664b0f8c PNA |
1056 | if (!nft_is_active_next(ctx->net, chain)) |
1057 | continue; | |
1058 | ||
b9ac12ef AB |
1059 | ctx->chain = chain; |
1060 | ||
1061 | err = nft_delrule_by_chain(ctx); | |
1062 | if (err < 0) | |
1063 | goto out; | |
b9ac12ef AB |
1064 | } |
1065 | ||
1066 | list_for_each_entry_safe(set, ns, &ctx->table->sets, list) { | |
37a9cc52 PNA |
1067 | if (!nft_is_active_next(ctx->net, set)) |
1068 | continue; | |
1069 | ||
408070d6 | 1070 | if (nft_set_is_anonymous(set) && |
b9ac12ef AB |
1071 | !list_empty(&set->bindings)) |
1072 | continue; | |
1073 | ||
1074 | err = nft_delset(ctx, set); | |
1075 | if (err < 0) | |
1076 | goto out; | |
1077 | } | |
1078 | ||
3b49e2e9 | 1079 | list_for_each_entry_safe(flowtable, nft, &ctx->table->flowtables, list) { |
335178d5 FW |
1080 | if (!nft_is_active_next(ctx->net, flowtable)) |
1081 | continue; | |
1082 | ||
3b49e2e9 PNA |
1083 | err = nft_delflowtable(ctx, flowtable); |
1084 | if (err < 0) | |
1085 | goto out; | |
1086 | } | |
1087 | ||
e5009240 | 1088 | list_for_each_entry_safe(obj, ne, &ctx->table->objects, list) { |
335178d5 FW |
1089 | if (!nft_is_active_next(ctx->net, obj)) |
1090 | continue; | |
1091 | ||
e5009240 PNA |
1092 | err = nft_delobj(ctx, obj); |
1093 | if (err < 0) | |
1094 | goto out; | |
1095 | } | |
1096 | ||
a2f18db0 | 1097 | list_for_each_entry_safe(chain, nc, &ctx->table->chains, list) { |
664b0f8c PNA |
1098 | if (!nft_is_active_next(ctx->net, chain)) |
1099 | continue; | |
1100 | ||
a2f18db0 PNA |
1101 | ctx->chain = chain; |
1102 | ||
1103 | err = nft_delchain(ctx); | |
1104 | if (err < 0) | |
1105 | goto out; | |
1106 | } | |
1107 | ||
b9ac12ef AB |
1108 | err = nft_deltable(ctx); |
1109 | out: | |
1110 | return err; | |
1111 | } | |
1112 | ||
1113 | static int nft_flush(struct nft_ctx *ctx, int family) | |
1114 | { | |
b9ac12ef AB |
1115 | struct nft_table *table, *nt; |
1116 | const struct nlattr * const *nla = ctx->nla; | |
1117 | int err = 0; | |
1118 | ||
36596dad | 1119 | list_for_each_entry_safe(table, nt, &ctx->net->nft.tables, list) { |
98319cb9 | 1120 | if (family != AF_UNSPEC && table->family != family) |
b9ac12ef AB |
1121 | continue; |
1122 | ||
98319cb9 | 1123 | ctx->family = table->family; |
f2a6d766 | 1124 | |
36596dad PNA |
1125 | if (!nft_is_active_next(ctx->net, table)) |
1126 | continue; | |
b9ac12ef | 1127 | |
36596dad PNA |
1128 | if (nla[NFTA_TABLE_NAME] && |
1129 | nla_strcmp(nla[NFTA_TABLE_NAME], table->name) != 0) | |
1130 | continue; | |
b9ac12ef | 1131 | |
36596dad PNA |
1132 | ctx->table = table; |
1133 | ||
1134 | err = nft_flush_table(ctx); | |
1135 | if (err < 0) | |
1136 | goto out; | |
b9ac12ef AB |
1137 | } |
1138 | out: | |
1139 | return err; | |
1140 | } | |
1141 | ||
633c9a84 PNA |
1142 | static int nf_tables_deltable(struct net *net, struct sock *nlsk, |
1143 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
04ba724b PNA |
1144 | const struct nlattr * const nla[], |
1145 | struct netlink_ext_ack *extack) | |
96518518 PM |
1146 | { |
1147 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
f2a6d766 | 1148 | u8 genmask = nft_genmask_next(net); |
ee01d542 | 1149 | int family = nfmsg->nfgen_family; |
36dd1bcc PNA |
1150 | const struct nlattr *attr; |
1151 | struct nft_table *table; | |
55dd6f93 | 1152 | struct nft_ctx ctx; |
96518518 | 1153 | |
36596dad | 1154 | nft_ctx_init(&ctx, net, skb, nlh, 0, NULL, NULL, nla); |
3ecbfd65 HS |
1155 | if (family == AF_UNSPEC || |
1156 | (!nla[NFTA_TABLE_NAME] && !nla[NFTA_TABLE_HANDLE])) | |
b9ac12ef AB |
1157 | return nft_flush(&ctx, family); |
1158 | ||
36dd1bcc PNA |
1159 | if (nla[NFTA_TABLE_HANDLE]) { |
1160 | attr = nla[NFTA_TABLE_HANDLE]; | |
1161 | table = nft_table_lookup_byhandle(net, attr, genmask); | |
1162 | } else { | |
1163 | attr = nla[NFTA_TABLE_NAME]; | |
1164 | table = nft_table_lookup(net, attr, family, genmask); | |
1165 | } | |
3ecbfd65 | 1166 | |
36dd1bcc PNA |
1167 | if (IS_ERR(table)) { |
1168 | NL_SET_BAD_ATTR(extack, attr); | |
96518518 | 1169 | return PTR_ERR(table); |
36dd1bcc | 1170 | } |
96518518 | 1171 | |
a8278400 PNA |
1172 | if (nlh->nlmsg_flags & NLM_F_NONREC && |
1173 | table->use > 0) | |
1174 | return -EBUSY; | |
1175 | ||
98319cb9 | 1176 | ctx.family = family; |
b9ac12ef | 1177 | ctx.table = table; |
55dd6f93 | 1178 | |
b9ac12ef | 1179 | return nft_flush_table(&ctx); |
96518518 PM |
1180 | } |
1181 | ||
55dd6f93 PNA |
1182 | static void nf_tables_table_destroy(struct nft_ctx *ctx) |
1183 | { | |
fa5950e4 FW |
1184 | if (WARN_ON(ctx->table->use > 0)) |
1185 | return; | |
4fefee57 | 1186 | |
1b2470e5 | 1187 | rhltable_destroy(&ctx->table->chains_ht); |
e46abbcc | 1188 | kfree(ctx->table->name); |
55dd6f93 | 1189 | kfree(ctx->table); |
55dd6f93 PNA |
1190 | } |
1191 | ||
cc07eeb0 | 1192 | void nft_register_chain_type(const struct nft_chain_type *ctype) |
96518518 | 1193 | { |
96518518 | 1194 | nfnl_lock(NFNL_SUBSYS_NFTABLES); |
82603549 | 1195 | if (WARN_ON(__nft_chain_type_get(ctype->family, ctype->type))) { |
cc07eeb0 PNA |
1196 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
1197 | return; | |
96518518 | 1198 | } |
9370761c | 1199 | chain_type[ctype->family][ctype->type] = ctype; |
96518518 | 1200 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
96518518 | 1201 | } |
9370761c | 1202 | EXPORT_SYMBOL_GPL(nft_register_chain_type); |
96518518 | 1203 | |
32537e91 | 1204 | void nft_unregister_chain_type(const struct nft_chain_type *ctype) |
96518518 | 1205 | { |
96518518 | 1206 | nfnl_lock(NFNL_SUBSYS_NFTABLES); |
9370761c | 1207 | chain_type[ctype->family][ctype->type] = NULL; |
96518518 PM |
1208 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
1209 | } | |
9370761c | 1210 | EXPORT_SYMBOL_GPL(nft_unregister_chain_type); |
96518518 PM |
1211 | |
1212 | /* | |
1213 | * Chains | |
1214 | */ | |
1215 | ||
1216 | static struct nft_chain * | |
cac20fcd | 1217 | nft_chain_lookup_byhandle(const struct nft_table *table, u64 handle, u8 genmask) |
96518518 PM |
1218 | { |
1219 | struct nft_chain *chain; | |
1220 | ||
1221 | list_for_each_entry(chain, &table->chains, list) { | |
664b0f8c PNA |
1222 | if (chain->handle == handle && |
1223 | nft_active_genmask(chain, genmask)) | |
96518518 PM |
1224 | return chain; |
1225 | } | |
1226 | ||
1227 | return ERR_PTR(-ENOENT); | |
1228 | } | |
1229 | ||
4d44175a | 1230 | static bool lockdep_commit_lock_is_held(const struct net *net) |
f102d66b FW |
1231 | { |
1232 | #ifdef CONFIG_PROVE_LOCKING | |
1233 | return lockdep_is_held(&net->nft.commit_mutex); | |
1234 | #else | |
1235 | return true; | |
1236 | #endif | |
1237 | } | |
1238 | ||
1239 | static struct nft_chain *nft_chain_lookup(struct net *net, | |
1240 | struct nft_table *table, | |
cac20fcd | 1241 | const struct nlattr *nla, u8 genmask) |
96518518 | 1242 | { |
1b2470e5 FW |
1243 | char search[NFT_CHAIN_MAXNAMELEN + 1]; |
1244 | struct rhlist_head *tmp, *list; | |
96518518 PM |
1245 | struct nft_chain *chain; |
1246 | ||
1247 | if (nla == NULL) | |
1248 | return ERR_PTR(-EINVAL); | |
1249 | ||
1b2470e5 | 1250 | nla_strlcpy(search, nla, sizeof(search)); |
96518518 | 1251 | |
1b2470e5 | 1252 | WARN_ON(!rcu_read_lock_held() && |
f102d66b | 1253 | !lockdep_commit_lock_is_held(net)); |
1b2470e5 FW |
1254 | |
1255 | chain = ERR_PTR(-ENOENT); | |
1256 | rcu_read_lock(); | |
1257 | list = rhltable_lookup(&table->chains_ht, search, nft_chain_ht_params); | |
1258 | if (!list) | |
1259 | goto out_unlock; | |
1260 | ||
1261 | rhl_for_each_entry_rcu(chain, tmp, list, rhlhead) { | |
1262 | if (nft_active_genmask(chain, genmask)) | |
1263 | goto out_unlock; | |
1264 | } | |
1265 | chain = ERR_PTR(-ENOENT); | |
1266 | out_unlock: | |
1267 | rcu_read_unlock(); | |
1268 | return chain; | |
96518518 PM |
1269 | } |
1270 | ||
1271 | static const struct nla_policy nft_chain_policy[NFTA_CHAIN_MAX + 1] = { | |
b2fbd044 LZ |
1272 | [NFTA_CHAIN_TABLE] = { .type = NLA_STRING, |
1273 | .len = NFT_TABLE_MAXNAMELEN - 1 }, | |
96518518 PM |
1274 | [NFTA_CHAIN_HANDLE] = { .type = NLA_U64 }, |
1275 | [NFTA_CHAIN_NAME] = { .type = NLA_STRING, | |
1276 | .len = NFT_CHAIN_MAXNAMELEN - 1 }, | |
1277 | [NFTA_CHAIN_HOOK] = { .type = NLA_NESTED }, | |
0ca743a5 | 1278 | [NFTA_CHAIN_POLICY] = { .type = NLA_U32 }, |
9332d27d FW |
1279 | [NFTA_CHAIN_TYPE] = { .type = NLA_STRING, |
1280 | .len = NFT_MODULE_AUTOLOAD_LIMIT }, | |
0ca743a5 | 1281 | [NFTA_CHAIN_COUNTERS] = { .type = NLA_NESTED }, |
c9626a2c | 1282 | [NFTA_CHAIN_FLAGS] = { .type = NLA_U32 }, |
74cccc3d | 1283 | [NFTA_CHAIN_ID] = { .type = NLA_U32 }, |
96518518 PM |
1284 | }; |
1285 | ||
1286 | static const struct nla_policy nft_hook_policy[NFTA_HOOK_MAX + 1] = { | |
1287 | [NFTA_HOOK_HOOKNUM] = { .type = NLA_U32 }, | |
1288 | [NFTA_HOOK_PRIORITY] = { .type = NLA_U32 }, | |
2cbce139 PNA |
1289 | [NFTA_HOOK_DEV] = { .type = NLA_STRING, |
1290 | .len = IFNAMSIZ - 1 }, | |
96518518 PM |
1291 | }; |
1292 | ||
0ca743a5 PNA |
1293 | static int nft_dump_stats(struct sk_buff *skb, struct nft_stats __percpu *stats) |
1294 | { | |
1295 | struct nft_stats *cpu_stats, total; | |
1296 | struct nlattr *nest; | |
ce355e20 ED |
1297 | unsigned int seq; |
1298 | u64 pkts, bytes; | |
0ca743a5 PNA |
1299 | int cpu; |
1300 | ||
edbd82c5 FW |
1301 | if (!stats) |
1302 | return 0; | |
1303 | ||
0ca743a5 PNA |
1304 | memset(&total, 0, sizeof(total)); |
1305 | for_each_possible_cpu(cpu) { | |
1306 | cpu_stats = per_cpu_ptr(stats, cpu); | |
ce355e20 ED |
1307 | do { |
1308 | seq = u64_stats_fetch_begin_irq(&cpu_stats->syncp); | |
1309 | pkts = cpu_stats->pkts; | |
1310 | bytes = cpu_stats->bytes; | |
1311 | } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, seq)); | |
1312 | total.pkts += pkts; | |
1313 | total.bytes += bytes; | |
0ca743a5 | 1314 | } |
ae0be8de | 1315 | nest = nla_nest_start_noflag(skb, NFTA_CHAIN_COUNTERS); |
0ca743a5 PNA |
1316 | if (nest == NULL) |
1317 | goto nla_put_failure; | |
1318 | ||
b46f6ded ND |
1319 | if (nla_put_be64(skb, NFTA_COUNTER_PACKETS, cpu_to_be64(total.pkts), |
1320 | NFTA_COUNTER_PAD) || | |
1321 | nla_put_be64(skb, NFTA_COUNTER_BYTES, cpu_to_be64(total.bytes), | |
1322 | NFTA_COUNTER_PAD)) | |
0ca743a5 PNA |
1323 | goto nla_put_failure; |
1324 | ||
1325 | nla_nest_end(skb, nest); | |
1326 | return 0; | |
1327 | ||
1328 | nla_put_failure: | |
1329 | return -ENOSPC; | |
1330 | } | |
1331 | ||
d54725cd PNA |
1332 | static int nft_dump_basechain_hook(struct sk_buff *skb, int family, |
1333 | const struct nft_base_chain *basechain) | |
1334 | { | |
1335 | const struct nf_hook_ops *ops = &basechain->ops; | |
1336 | struct nft_hook *hook, *first = NULL; | |
1337 | struct nlattr *nest, *nest_devs; | |
1338 | int n = 0; | |
1339 | ||
1340 | nest = nla_nest_start_noflag(skb, NFTA_CHAIN_HOOK); | |
1341 | if (nest == NULL) | |
1342 | goto nla_put_failure; | |
1343 | if (nla_put_be32(skb, NFTA_HOOK_HOOKNUM, htonl(ops->hooknum))) | |
1344 | goto nla_put_failure; | |
1345 | if (nla_put_be32(skb, NFTA_HOOK_PRIORITY, htonl(ops->priority))) | |
1346 | goto nla_put_failure; | |
1347 | ||
1348 | if (family == NFPROTO_NETDEV) { | |
1349 | nest_devs = nla_nest_start_noflag(skb, NFTA_HOOK_DEVS); | |
1350 | list_for_each_entry(hook, &basechain->hook_list, list) { | |
1351 | if (!first) | |
1352 | first = hook; | |
1353 | ||
1354 | if (nla_put_string(skb, NFTA_DEVICE_NAME, | |
1355 | hook->ops.dev->name)) | |
1356 | goto nla_put_failure; | |
1357 | n++; | |
1358 | } | |
1359 | nla_nest_end(skb, nest_devs); | |
1360 | ||
1361 | if (n == 1 && | |
1362 | nla_put_string(skb, NFTA_HOOK_DEV, first->ops.dev->name)) | |
1363 | goto nla_put_failure; | |
1364 | } | |
1365 | nla_nest_end(skb, nest); | |
1366 | ||
1367 | return 0; | |
1368 | nla_put_failure: | |
1369 | return -1; | |
1370 | } | |
1371 | ||
84d7fce6 PNA |
1372 | static int nf_tables_fill_chain_info(struct sk_buff *skb, struct net *net, |
1373 | u32 portid, u32 seq, int event, u32 flags, | |
1374 | int family, const struct nft_table *table, | |
96518518 PM |
1375 | const struct nft_chain *chain) |
1376 | { | |
1377 | struct nlmsghdr *nlh; | |
1378 | struct nfgenmsg *nfmsg; | |
1379 | ||
dedb67c4 | 1380 | event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); |
96518518 PM |
1381 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags); |
1382 | if (nlh == NULL) | |
1383 | goto nla_put_failure; | |
1384 | ||
1385 | nfmsg = nlmsg_data(nlh); | |
1386 | nfmsg->nfgen_family = family; | |
1387 | nfmsg->version = NFNETLINK_V0; | |
84d7fce6 | 1388 | nfmsg->res_id = htons(net->nft.base_seq & 0xffff); |
96518518 PM |
1389 | |
1390 | if (nla_put_string(skb, NFTA_CHAIN_TABLE, table->name)) | |
1391 | goto nla_put_failure; | |
b46f6ded ND |
1392 | if (nla_put_be64(skb, NFTA_CHAIN_HANDLE, cpu_to_be64(chain->handle), |
1393 | NFTA_CHAIN_PAD)) | |
96518518 PM |
1394 | goto nla_put_failure; |
1395 | if (nla_put_string(skb, NFTA_CHAIN_NAME, chain->name)) | |
1396 | goto nla_put_failure; | |
1397 | ||
f323d954 | 1398 | if (nft_is_base_chain(chain)) { |
0ca743a5 | 1399 | const struct nft_base_chain *basechain = nft_base_chain(chain); |
edbd82c5 | 1400 | struct nft_stats __percpu *stats; |
0ca743a5 | 1401 | |
d54725cd | 1402 | if (nft_dump_basechain_hook(skb, family, basechain)) |
2cbce139 | 1403 | goto nla_put_failure; |
9370761c | 1404 | |
0ca743a5 PNA |
1405 | if (nla_put_be32(skb, NFTA_CHAIN_POLICY, |
1406 | htonl(basechain->policy))) | |
1407 | goto nla_put_failure; | |
1408 | ||
baae3e62 PM |
1409 | if (nla_put_string(skb, NFTA_CHAIN_TYPE, basechain->type->name)) |
1410 | goto nla_put_failure; | |
0ca743a5 | 1411 | |
edbd82c5 FW |
1412 | stats = rcu_dereference_check(basechain->stats, |
1413 | lockdep_commit_lock_is_held(net)); | |
1414 | if (nft_dump_stats(skb, stats)) | |
0ca743a5 | 1415 | goto nla_put_failure; |
d78008de PNA |
1416 | |
1417 | if ((chain->flags & NFT_CHAIN_HW_OFFLOAD) && | |
1418 | nla_put_be32(skb, NFTA_CHAIN_FLAGS, | |
1419 | htonl(NFT_CHAIN_HW_OFFLOAD))) | |
1420 | goto nla_put_failure; | |
96518518 PM |
1421 | } |
1422 | ||
0ca743a5 PNA |
1423 | if (nla_put_be32(skb, NFTA_CHAIN_USE, htonl(chain->use))) |
1424 | goto nla_put_failure; | |
1425 | ||
053c095a JB |
1426 | nlmsg_end(skb, nlh); |
1427 | return 0; | |
96518518 PM |
1428 | |
1429 | nla_put_failure: | |
1430 | nlmsg_trim(skb, nlh); | |
1431 | return -1; | |
1432 | } | |
1433 | ||
25e94a99 | 1434 | static void nf_tables_chain_notify(const struct nft_ctx *ctx, int event) |
96518518 PM |
1435 | { |
1436 | struct sk_buff *skb; | |
96518518 PM |
1437 | int err; |
1438 | ||
128ad332 PNA |
1439 | if (!ctx->report && |
1440 | !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES)) | |
25e94a99 | 1441 | return; |
96518518 | 1442 | |
96518518 PM |
1443 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
1444 | if (skb == NULL) | |
1445 | goto err; | |
1446 | ||
84d7fce6 | 1447 | err = nf_tables_fill_chain_info(skb, ctx->net, ctx->portid, ctx->seq, |
36596dad | 1448 | event, 0, ctx->family, ctx->table, |
35151d84 | 1449 | ctx->chain); |
96518518 PM |
1450 | if (err < 0) { |
1451 | kfree_skb(skb); | |
1452 | goto err; | |
1453 | } | |
1454 | ||
25e94a99 PNA |
1455 | nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES, |
1456 | ctx->report, GFP_KERNEL); | |
1457 | return; | |
96518518 | 1458 | err: |
25e94a99 | 1459 | nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS); |
96518518 PM |
1460 | } |
1461 | ||
1462 | static int nf_tables_dump_chains(struct sk_buff *skb, | |
1463 | struct netlink_callback *cb) | |
1464 | { | |
1465 | const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); | |
96518518 PM |
1466 | const struct nft_table *table; |
1467 | const struct nft_chain *chain; | |
1468 | unsigned int idx = 0, s_idx = cb->args[0]; | |
99633ab2 | 1469 | struct net *net = sock_net(skb->sk); |
96518518 PM |
1470 | int family = nfmsg->nfgen_family; |
1471 | ||
e688a7f8 | 1472 | rcu_read_lock(); |
38e029f1 PNA |
1473 | cb->seq = net->nft.base_seq; |
1474 | ||
36596dad | 1475 | list_for_each_entry_rcu(table, &net->nft.tables, list) { |
98319cb9 | 1476 | if (family != NFPROTO_UNSPEC && family != table->family) |
96518518 PM |
1477 | continue; |
1478 | ||
36596dad PNA |
1479 | list_for_each_entry_rcu(chain, &table->chains, list) { |
1480 | if (idx < s_idx) | |
1481 | goto cont; | |
1482 | if (idx > s_idx) | |
1483 | memset(&cb->args[1], 0, | |
1484 | sizeof(cb->args) - sizeof(cb->args[0])); | |
1485 | if (!nft_is_active(net, chain)) | |
1486 | continue; | |
1487 | if (nf_tables_fill_chain_info(skb, net, | |
1488 | NETLINK_CB(cb->skb).portid, | |
1489 | cb->nlh->nlmsg_seq, | |
1490 | NFT_MSG_NEWCHAIN, | |
1491 | NLM_F_MULTI, | |
98319cb9 | 1492 | table->family, table, |
36596dad PNA |
1493 | chain) < 0) |
1494 | goto done; | |
38e029f1 | 1495 | |
36596dad | 1496 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); |
96518518 | 1497 | cont: |
36596dad | 1498 | idx++; |
96518518 PM |
1499 | } |
1500 | } | |
1501 | done: | |
e688a7f8 | 1502 | rcu_read_unlock(); |
96518518 PM |
1503 | cb->args[0] = idx; |
1504 | return skb->len; | |
1505 | } | |
1506 | ||
d9adf22a | 1507 | /* called with rcu_read_lock held */ |
7b8002a1 PNA |
1508 | static int nf_tables_getchain(struct net *net, struct sock *nlsk, |
1509 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
04ba724b PNA |
1510 | const struct nlattr * const nla[], |
1511 | struct netlink_ext_ack *extack) | |
96518518 PM |
1512 | { |
1513 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
f2a6d766 | 1514 | u8 genmask = nft_genmask_cur(net); |
96518518 | 1515 | const struct nft_chain *chain; |
1b2470e5 | 1516 | struct nft_table *table; |
96518518 PM |
1517 | struct sk_buff *skb2; |
1518 | int family = nfmsg->nfgen_family; | |
1519 | int err; | |
1520 | ||
1521 | if (nlh->nlmsg_flags & NLM_F_DUMP) { | |
1522 | struct netlink_dump_control c = { | |
1523 | .dump = nf_tables_dump_chains, | |
d9adf22a | 1524 | .module = THIS_MODULE, |
96518518 | 1525 | }; |
d9adf22a FW |
1526 | |
1527 | return nft_netlink_dump_start_rcu(nlsk, skb, nlh, &c); | |
96518518 PM |
1528 | } |
1529 | ||
cac20fcd | 1530 | table = nft_table_lookup(net, nla[NFTA_CHAIN_TABLE], family, genmask); |
36dd1bcc PNA |
1531 | if (IS_ERR(table)) { |
1532 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TABLE]); | |
96518518 | 1533 | return PTR_ERR(table); |
36dd1bcc | 1534 | } |
96518518 | 1535 | |
f102d66b | 1536 | chain = nft_chain_lookup(net, table, nla[NFTA_CHAIN_NAME], genmask); |
36dd1bcc PNA |
1537 | if (IS_ERR(chain)) { |
1538 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_NAME]); | |
96518518 | 1539 | return PTR_ERR(chain); |
36dd1bcc | 1540 | } |
96518518 | 1541 | |
d9adf22a | 1542 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
96518518 PM |
1543 | if (!skb2) |
1544 | return -ENOMEM; | |
1545 | ||
84d7fce6 | 1546 | err = nf_tables_fill_chain_info(skb2, net, NETLINK_CB(skb).portid, |
96518518 PM |
1547 | nlh->nlmsg_seq, NFT_MSG_NEWCHAIN, 0, |
1548 | family, table, chain); | |
1549 | if (err < 0) | |
1550 | goto err; | |
1551 | ||
1552 | return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid); | |
1553 | ||
1554 | err: | |
1555 | kfree_skb(skb2); | |
1556 | return err; | |
1557 | } | |
1558 | ||
0ca743a5 PNA |
1559 | static const struct nla_policy nft_counter_policy[NFTA_COUNTER_MAX + 1] = { |
1560 | [NFTA_COUNTER_PACKETS] = { .type = NLA_U64 }, | |
1561 | [NFTA_COUNTER_BYTES] = { .type = NLA_U64 }, | |
1562 | }; | |
1563 | ||
ff3cd7b3 | 1564 | static struct nft_stats __percpu *nft_stats_alloc(const struct nlattr *attr) |
0ca743a5 PNA |
1565 | { |
1566 | struct nlattr *tb[NFTA_COUNTER_MAX+1]; | |
1567 | struct nft_stats __percpu *newstats; | |
1568 | struct nft_stats *stats; | |
1569 | int err; | |
1570 | ||
8cb08174 JB |
1571 | err = nla_parse_nested_deprecated(tb, NFTA_COUNTER_MAX, attr, |
1572 | nft_counter_policy, NULL); | |
0ca743a5 | 1573 | if (err < 0) |
ff3cd7b3 | 1574 | return ERR_PTR(err); |
0ca743a5 PNA |
1575 | |
1576 | if (!tb[NFTA_COUNTER_BYTES] || !tb[NFTA_COUNTER_PACKETS]) | |
ff3cd7b3 | 1577 | return ERR_PTR(-EINVAL); |
0ca743a5 | 1578 | |
ce355e20 | 1579 | newstats = netdev_alloc_pcpu_stats(struct nft_stats); |
0ca743a5 | 1580 | if (newstats == NULL) |
ff3cd7b3 | 1581 | return ERR_PTR(-ENOMEM); |
0ca743a5 PNA |
1582 | |
1583 | /* Restore old counters on this cpu, no problem. Per-cpu statistics | |
1584 | * are not exposed to userspace. | |
1585 | */ | |
e8781f70 | 1586 | preempt_disable(); |
0ca743a5 PNA |
1587 | stats = this_cpu_ptr(newstats); |
1588 | stats->bytes = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES])); | |
1589 | stats->pkts = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS])); | |
e8781f70 | 1590 | preempt_enable(); |
0ca743a5 | 1591 | |
ff3cd7b3 PNA |
1592 | return newstats; |
1593 | } | |
1594 | ||
53315ac6 | 1595 | static void nft_chain_stats_replace(struct nft_trans *trans) |
ff3cd7b3 | 1596 | { |
53315ac6 | 1597 | struct nft_base_chain *chain = nft_base_chain(trans->ctx.chain); |
0befd061 | 1598 | |
53315ac6 | 1599 | if (!nft_trans_chain_stats(trans)) |
b88825de PNA |
1600 | return; |
1601 | ||
b685b534 PM |
1602 | nft_trans_chain_stats(trans) = |
1603 | rcu_replace_pointer(chain->stats, nft_trans_chain_stats(trans), | |
1604 | lockdep_commit_lock_is_held(trans->ctx.net)); | |
53315ac6 FW |
1605 | |
1606 | if (!nft_trans_chain_stats(trans)) | |
bbb8c61f | 1607 | static_branch_inc(&nft_counters_enabled); |
0ca743a5 PNA |
1608 | } |
1609 | ||
0cbc06b3 FW |
1610 | static void nf_tables_chain_free_chain_rules(struct nft_chain *chain) |
1611 | { | |
1612 | struct nft_rule **g0 = rcu_dereference_raw(chain->rules_gen_0); | |
1613 | struct nft_rule **g1 = rcu_dereference_raw(chain->rules_gen_1); | |
1614 | ||
1615 | if (g0 != g1) | |
1616 | kvfree(g1); | |
1617 | kvfree(g0); | |
1618 | ||
1619 | /* should be NULL either via abort or via successful commit */ | |
1620 | WARN_ON_ONCE(chain->rules_next); | |
1621 | kvfree(chain->rules_next); | |
1622 | } | |
1623 | ||
43a605f2 | 1624 | static void nf_tables_chain_destroy(struct nft_ctx *ctx) |
91c7b38d | 1625 | { |
43a605f2 | 1626 | struct nft_chain *chain = ctx->chain; |
d54725cd | 1627 | struct nft_hook *hook, *next; |
43a605f2 | 1628 | |
fa5950e4 FW |
1629 | if (WARN_ON(chain->use > 0)) |
1630 | return; | |
91c7b38d | 1631 | |
0cbc06b3 FW |
1632 | /* no concurrent access possible anymore */ |
1633 | nf_tables_chain_free_chain_rules(chain); | |
1634 | ||
f323d954 | 1635 | if (nft_is_base_chain(chain)) { |
2cbce139 PNA |
1636 | struct nft_base_chain *basechain = nft_base_chain(chain); |
1637 | ||
d54725cd PNA |
1638 | if (ctx->family == NFPROTO_NETDEV) { |
1639 | list_for_each_entry_safe(hook, next, | |
1640 | &basechain->hook_list, list) { | |
1641 | list_del_rcu(&hook->list); | |
1642 | kfree_rcu(hook, rcu); | |
1643 | } | |
1644 | } | |
2cbce139 | 1645 | module_put(basechain->type->owner); |
4c05ec47 | 1646 | if (rcu_access_pointer(basechain->stats)) { |
9f08ea84 | 1647 | static_branch_dec(&nft_counters_enabled); |
4c05ec47 TY |
1648 | free_percpu(rcu_dereference_raw(basechain->stats)); |
1649 | } | |
b7263e07 | 1650 | kfree(chain->name); |
2cbce139 | 1651 | kfree(basechain); |
91c7b38d | 1652 | } else { |
b7263e07 | 1653 | kfree(chain->name); |
91c7b38d PNA |
1654 | kfree(chain); |
1655 | } | |
1656 | } | |
1657 | ||
3f0465a9 PNA |
1658 | static struct nft_hook *nft_netdev_hook_alloc(struct net *net, |
1659 | const struct nlattr *attr) | |
1660 | { | |
1661 | struct net_device *dev; | |
1662 | char ifname[IFNAMSIZ]; | |
1663 | struct nft_hook *hook; | |
1664 | int err; | |
1665 | ||
1666 | hook = kmalloc(sizeof(struct nft_hook), GFP_KERNEL); | |
1667 | if (!hook) { | |
1668 | err = -ENOMEM; | |
1669 | goto err_hook_alloc; | |
1670 | } | |
1671 | ||
1672 | nla_strlcpy(ifname, attr, IFNAMSIZ); | |
1673 | dev = __dev_get_by_name(net, ifname); | |
1674 | if (!dev) { | |
1675 | err = -ENOENT; | |
1676 | goto err_hook_dev; | |
1677 | } | |
1678 | hook->ops.dev = dev; | |
abadb2f8 | 1679 | hook->inactive = false; |
3f0465a9 PNA |
1680 | |
1681 | return hook; | |
1682 | ||
1683 | err_hook_dev: | |
1684 | kfree(hook); | |
1685 | err_hook_alloc: | |
1686 | return ERR_PTR(err); | |
1687 | } | |
1688 | ||
abadb2f8 PNA |
1689 | static struct nft_hook *nft_hook_list_find(struct list_head *hook_list, |
1690 | const struct nft_hook *this) | |
b75a3e83 PNA |
1691 | { |
1692 | struct nft_hook *hook; | |
1693 | ||
1694 | list_for_each_entry(hook, hook_list, list) { | |
1695 | if (this->ops.dev == hook->ops.dev) | |
abadb2f8 | 1696 | return hook; |
b75a3e83 PNA |
1697 | } |
1698 | ||
abadb2f8 | 1699 | return NULL; |
b75a3e83 PNA |
1700 | } |
1701 | ||
3f0465a9 PNA |
1702 | static int nf_tables_parse_netdev_hooks(struct net *net, |
1703 | const struct nlattr *attr, | |
1704 | struct list_head *hook_list) | |
1705 | { | |
1706 | struct nft_hook *hook, *next; | |
1707 | const struct nlattr *tmp; | |
1708 | int rem, n = 0, err; | |
1709 | ||
1710 | nla_for_each_nested(tmp, attr, rem) { | |
1711 | if (nla_type(tmp) != NFTA_DEVICE_NAME) { | |
1712 | err = -EINVAL; | |
1713 | goto err_hook; | |
1714 | } | |
1715 | ||
1716 | hook = nft_netdev_hook_alloc(net, tmp); | |
1717 | if (IS_ERR(hook)) { | |
1718 | err = PTR_ERR(hook); | |
1719 | goto err_hook; | |
1720 | } | |
b75a3e83 | 1721 | if (nft_hook_list_find(hook_list, hook)) { |
cd77e75b | 1722 | kfree(hook); |
b75a3e83 PNA |
1723 | err = -EEXIST; |
1724 | goto err_hook; | |
1725 | } | |
3f0465a9 PNA |
1726 | list_add_tail(&hook->list, hook_list); |
1727 | n++; | |
1728 | ||
cb662ac6 | 1729 | if (n == NFT_NETDEVICE_MAX) { |
3f0465a9 PNA |
1730 | err = -EFBIG; |
1731 | goto err_hook; | |
1732 | } | |
1733 | } | |
3f0465a9 PNA |
1734 | |
1735 | return 0; | |
1736 | ||
1737 | err_hook: | |
1738 | list_for_each_entry_safe(hook, next, hook_list, list) { | |
1739 | list_del(&hook->list); | |
1740 | kfree(hook); | |
1741 | } | |
1742 | return err; | |
1743 | } | |
1744 | ||
508f8ccd PNA |
1745 | struct nft_chain_hook { |
1746 | u32 num; | |
84ba7dd7 | 1747 | s32 priority; |
32537e91 | 1748 | const struct nft_chain_type *type; |
d54725cd | 1749 | struct list_head list; |
508f8ccd PNA |
1750 | }; |
1751 | ||
d54725cd PNA |
1752 | static int nft_chain_parse_netdev(struct net *net, |
1753 | struct nlattr *tb[], | |
1754 | struct list_head *hook_list) | |
1755 | { | |
1756 | struct nft_hook *hook; | |
1757 | int err; | |
1758 | ||
1759 | if (tb[NFTA_HOOK_DEV]) { | |
1760 | hook = nft_netdev_hook_alloc(net, tb[NFTA_HOOK_DEV]); | |
1761 | if (IS_ERR(hook)) | |
1762 | return PTR_ERR(hook); | |
1763 | ||
1764 | list_add_tail(&hook->list, hook_list); | |
1765 | } else if (tb[NFTA_HOOK_DEVS]) { | |
1766 | err = nf_tables_parse_netdev_hooks(net, tb[NFTA_HOOK_DEVS], | |
1767 | hook_list); | |
1768 | if (err < 0) | |
1769 | return err; | |
05abe445 PNA |
1770 | |
1771 | if (list_empty(hook_list)) | |
1772 | return -EINVAL; | |
d54725cd PNA |
1773 | } else { |
1774 | return -EINVAL; | |
1775 | } | |
1776 | ||
1777 | return 0; | |
1778 | } | |
1779 | ||
508f8ccd PNA |
1780 | static int nft_chain_parse_hook(struct net *net, |
1781 | const struct nlattr * const nla[], | |
36596dad | 1782 | struct nft_chain_hook *hook, u8 family, |
445509eb | 1783 | bool autoload) |
508f8ccd PNA |
1784 | { |
1785 | struct nlattr *ha[NFTA_HOOK_MAX + 1]; | |
32537e91 | 1786 | const struct nft_chain_type *type; |
508f8ccd PNA |
1787 | int err; |
1788 | ||
f102d66b FW |
1789 | lockdep_assert_held(&net->nft.commit_mutex); |
1790 | lockdep_nfnl_nft_mutex_not_held(); | |
1791 | ||
8cb08174 JB |
1792 | err = nla_parse_nested_deprecated(ha, NFTA_HOOK_MAX, |
1793 | nla[NFTA_CHAIN_HOOK], | |
1794 | nft_hook_policy, NULL); | |
508f8ccd PNA |
1795 | if (err < 0) |
1796 | return err; | |
1797 | ||
1798 | if (ha[NFTA_HOOK_HOOKNUM] == NULL || | |
1799 | ha[NFTA_HOOK_PRIORITY] == NULL) | |
1800 | return -EINVAL; | |
1801 | ||
1802 | hook->num = ntohl(nla_get_be32(ha[NFTA_HOOK_HOOKNUM])); | |
508f8ccd PNA |
1803 | hook->priority = ntohl(nla_get_be32(ha[NFTA_HOOK_PRIORITY])); |
1804 | ||
82603549 PNA |
1805 | type = __nft_chain_type_get(family, NFT_CHAIN_T_DEFAULT); |
1806 | if (!type) | |
1807 | return -EOPNOTSUPP; | |
1808 | ||
508f8ccd | 1809 | if (nla[NFTA_CHAIN_TYPE]) { |
452238e8 | 1810 | type = nf_tables_chain_type_lookup(net, nla[NFTA_CHAIN_TYPE], |
445509eb | 1811 | family, autoload); |
508f8ccd PNA |
1812 | if (IS_ERR(type)) |
1813 | return PTR_ERR(type); | |
1814 | } | |
33d1c018 | 1815 | if (hook->num > NF_MAX_HOOKS || !(type->hook_mask & (1 << hook->num))) |
508f8ccd | 1816 | return -EOPNOTSUPP; |
84ba7dd7 FW |
1817 | |
1818 | if (type->type == NFT_CHAIN_T_NAT && | |
1819 | hook->priority <= NF_IP_PRI_CONNTRACK) | |
1820 | return -EOPNOTSUPP; | |
1821 | ||
508f8ccd PNA |
1822 | if (!try_module_get(type->owner)) |
1823 | return -ENOENT; | |
1824 | ||
1825 | hook->type = type; | |
1826 | ||
d54725cd | 1827 | INIT_LIST_HEAD(&hook->list); |
36596dad | 1828 | if (family == NFPROTO_NETDEV) { |
d54725cd PNA |
1829 | err = nft_chain_parse_netdev(net, ha, &hook->list); |
1830 | if (err < 0) { | |
508f8ccd | 1831 | module_put(type->owner); |
d54725cd | 1832 | return err; |
508f8ccd | 1833 | } |
d54725cd | 1834 | } else if (ha[NFTA_HOOK_DEV] || ha[NFTA_HOOK_DEVS]) { |
508f8ccd PNA |
1835 | module_put(type->owner); |
1836 | return -EOPNOTSUPP; | |
1837 | } | |
1838 | ||
1839 | return 0; | |
1840 | } | |
1841 | ||
1842 | static void nft_chain_release_hook(struct nft_chain_hook *hook) | |
1843 | { | |
d54725cd PNA |
1844 | struct nft_hook *h, *next; |
1845 | ||
1846 | list_for_each_entry_safe(h, next, &hook->list, list) { | |
1847 | list_del(&h->list); | |
1848 | kfree(h); | |
1849 | } | |
508f8ccd | 1850 | module_put(hook->type->owner); |
508f8ccd PNA |
1851 | } |
1852 | ||
0cbc06b3 FW |
1853 | struct nft_rules_old { |
1854 | struct rcu_head h; | |
1855 | struct nft_rule **start; | |
1856 | }; | |
1857 | ||
1858 | static struct nft_rule **nf_tables_chain_alloc_rules(const struct nft_chain *chain, | |
1859 | unsigned int alloc) | |
1860 | { | |
1861 | if (alloc > INT_MAX) | |
1862 | return NULL; | |
1863 | ||
1864 | alloc += 1; /* NULL, ends rules */ | |
1865 | if (sizeof(struct nft_rule *) > INT_MAX / alloc) | |
1866 | return NULL; | |
1867 | ||
1868 | alloc *= sizeof(struct nft_rule *); | |
1869 | alloc += sizeof(struct nft_rules_old); | |
1870 | ||
1871 | return kvmalloc(alloc, GFP_KERNEL); | |
1872 | } | |
1873 | ||
d54725cd PNA |
1874 | static void nft_basechain_hook_init(struct nf_hook_ops *ops, u8 family, |
1875 | const struct nft_chain_hook *hook, | |
1876 | struct nft_chain *chain) | |
1877 | { | |
1878 | ops->pf = family; | |
1879 | ops->hooknum = hook->num; | |
1880 | ops->priority = hook->priority; | |
1881 | ops->priv = chain; | |
1882 | ops->hook = hook->type->hooks[ops->hooknum]; | |
1883 | } | |
1884 | ||
1885 | static int nft_basechain_init(struct nft_base_chain *basechain, u8 family, | |
1886 | struct nft_chain_hook *hook, u32 flags) | |
1887 | { | |
1888 | struct nft_chain *chain; | |
1889 | struct nft_hook *h; | |
1890 | ||
1891 | basechain->type = hook->type; | |
1892 | INIT_LIST_HEAD(&basechain->hook_list); | |
1893 | chain = &basechain->chain; | |
1894 | ||
1895 | if (family == NFPROTO_NETDEV) { | |
1896 | list_splice_init(&hook->list, &basechain->hook_list); | |
1897 | list_for_each_entry(h, &basechain->hook_list, list) | |
1898 | nft_basechain_hook_init(&h->ops, family, hook, chain); | |
1899 | ||
1900 | basechain->ops.hooknum = hook->num; | |
1901 | basechain->ops.priority = hook->priority; | |
1902 | } else { | |
1903 | nft_basechain_hook_init(&basechain->ops, family, hook, chain); | |
1904 | } | |
1905 | ||
1906 | chain->flags |= NFT_BASE_CHAIN | flags; | |
1907 | basechain->policy = NF_ACCEPT; | |
1908 | if (chain->flags & NFT_CHAIN_HW_OFFLOAD && | |
1909 | nft_chain_offload_priority(basechain) < 0) | |
1910 | return -EOPNOTSUPP; | |
1911 | ||
1912 | flow_block_init(&basechain->flow_block); | |
1913 | ||
1914 | return 0; | |
1915 | } | |
1916 | ||
4035285f | 1917 | static int nf_tables_addchain(struct nft_ctx *ctx, u8 family, u8 genmask, |
c9626a2c | 1918 | u8 policy, u32 flags) |
4035285f PNA |
1919 | { |
1920 | const struct nlattr * const *nla = ctx->nla; | |
1921 | struct nft_table *table = ctx->table; | |
4035285f PNA |
1922 | struct nft_base_chain *basechain; |
1923 | struct nft_stats __percpu *stats; | |
1924 | struct net *net = ctx->net; | |
66293c46 | 1925 | struct nft_trans *trans; |
4035285f | 1926 | struct nft_chain *chain; |
0cbc06b3 | 1927 | struct nft_rule **rules; |
4035285f PNA |
1928 | int err; |
1929 | ||
1930 | if (table->use == UINT_MAX) | |
1931 | return -EOVERFLOW; | |
1932 | ||
1933 | if (nla[NFTA_CHAIN_HOOK]) { | |
1934 | struct nft_chain_hook hook; | |
4035285f | 1935 | |
445509eb | 1936 | err = nft_chain_parse_hook(net, nla, &hook, family, true); |
4035285f PNA |
1937 | if (err < 0) |
1938 | return err; | |
1939 | ||
1940 | basechain = kzalloc(sizeof(*basechain), GFP_KERNEL); | |
1941 | if (basechain == NULL) { | |
1942 | nft_chain_release_hook(&hook); | |
1943 | return -ENOMEM; | |
1944 | } | |
d54725cd | 1945 | chain = &basechain->chain; |
4035285f PNA |
1946 | |
1947 | if (nla[NFTA_CHAIN_COUNTERS]) { | |
1948 | stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]); | |
1949 | if (IS_ERR(stats)) { | |
1950 | nft_chain_release_hook(&hook); | |
1951 | kfree(basechain); | |
1952 | return PTR_ERR(stats); | |
1953 | } | |
4c05ec47 | 1954 | rcu_assign_pointer(basechain->stats, stats); |
4035285f PNA |
1955 | static_branch_inc(&nft_counters_enabled); |
1956 | } | |
1957 | ||
d54725cd PNA |
1958 | err = nft_basechain_init(basechain, family, &hook, flags); |
1959 | if (err < 0) { | |
1960 | nft_chain_release_hook(&hook); | |
1961 | kfree(basechain); | |
1962 | return err; | |
1963 | } | |
4035285f PNA |
1964 | } else { |
1965 | chain = kzalloc(sizeof(*chain), GFP_KERNEL); | |
1966 | if (chain == NULL) | |
1967 | return -ENOMEM; | |
1968 | } | |
43a605f2 PNA |
1969 | ctx->chain = chain; |
1970 | ||
4035285f PNA |
1971 | INIT_LIST_HEAD(&chain->rules); |
1972 | chain->handle = nf_tables_alloc_handle(table); | |
1973 | chain->table = table; | |
1974 | chain->name = nla_strdup(nla[NFTA_CHAIN_NAME], GFP_KERNEL); | |
1975 | if (!chain->name) { | |
1976 | err = -ENOMEM; | |
1977 | goto err1; | |
1978 | } | |
1979 | ||
0cbc06b3 FW |
1980 | rules = nf_tables_chain_alloc_rules(chain, 0); |
1981 | if (!rules) { | |
1982 | err = -ENOMEM; | |
1983 | goto err1; | |
1984 | } | |
1985 | ||
1986 | *rules = NULL; | |
1987 | rcu_assign_pointer(chain->rules_gen_0, rules); | |
1988 | rcu_assign_pointer(chain->rules_gen_1, rules); | |
1989 | ||
c974a3a3 | 1990 | err = nf_tables_register_hook(net, table, chain); |
4035285f PNA |
1991 | if (err < 0) |
1992 | goto err1; | |
1993 | ||
1b2470e5 FW |
1994 | err = rhltable_insert_key(&table->chains_ht, chain->name, |
1995 | &chain->rhlhead, nft_chain_ht_params); | |
1996 | if (err) | |
1997 | goto err2; | |
1998 | ||
66293c46 FW |
1999 | trans = nft_trans_chain_add(ctx, NFT_MSG_NEWCHAIN); |
2000 | if (IS_ERR(trans)) { | |
2001 | err = PTR_ERR(trans); | |
1b2470e5 FW |
2002 | rhltable_remove(&table->chains_ht, &chain->rhlhead, |
2003 | nft_chain_ht_params); | |
4035285f | 2004 | goto err2; |
1b2470e5 | 2005 | } |
4035285f | 2006 | |
ad652f38 | 2007 | nft_trans_chain_policy(trans) = NFT_CHAIN_POLICY_UNSET; |
66293c46 FW |
2008 | if (nft_is_base_chain(chain)) |
2009 | nft_trans_chain_policy(trans) = policy; | |
2010 | ||
4035285f PNA |
2011 | table->use++; |
2012 | list_add_tail_rcu(&chain->list, &table->chains); | |
2013 | ||
2014 | return 0; | |
2015 | err2: | |
c974a3a3 | 2016 | nf_tables_unregister_hook(net, table, chain); |
4035285f | 2017 | err1: |
43a605f2 | 2018 | nf_tables_chain_destroy(ctx); |
4035285f PNA |
2019 | |
2020 | return err; | |
2021 | } | |
2022 | ||
d54725cd PNA |
2023 | static bool nft_hook_list_equal(struct list_head *hook_list1, |
2024 | struct list_head *hook_list2) | |
2025 | { | |
2026 | struct nft_hook *hook; | |
2027 | int n = 0, m = 0; | |
2028 | ||
2029 | n = 0; | |
2030 | list_for_each_entry(hook, hook_list2, list) { | |
2031 | if (!nft_hook_list_find(hook_list1, hook)) | |
2032 | return false; | |
2033 | ||
2034 | n++; | |
2035 | } | |
2036 | list_for_each_entry(hook, hook_list1, list) | |
2037 | m++; | |
2038 | ||
2039 | return n == m; | |
2040 | } | |
2041 | ||
c9626a2c PNA |
2042 | static int nf_tables_updchain(struct nft_ctx *ctx, u8 genmask, u8 policy, |
2043 | u32 flags) | |
2c4a488a PNA |
2044 | { |
2045 | const struct nlattr * const *nla = ctx->nla; | |
2046 | struct nft_table *table = ctx->table; | |
2047 | struct nft_chain *chain = ctx->chain; | |
2c4a488a PNA |
2048 | struct nft_base_chain *basechain; |
2049 | struct nft_stats *stats = NULL; | |
2050 | struct nft_chain_hook hook; | |
2c4a488a PNA |
2051 | struct nf_hook_ops *ops; |
2052 | struct nft_trans *trans; | |
c974a3a3 | 2053 | int err; |
2c4a488a | 2054 | |
c9626a2c PNA |
2055 | if (chain->flags ^ flags) |
2056 | return -EOPNOTSUPP; | |
2057 | ||
2c4a488a PNA |
2058 | if (nla[NFTA_CHAIN_HOOK]) { |
2059 | if (!nft_is_base_chain(chain)) | |
2060 | return -EBUSY; | |
2061 | ||
36596dad | 2062 | err = nft_chain_parse_hook(ctx->net, nla, &hook, ctx->family, |
445509eb | 2063 | false); |
2c4a488a PNA |
2064 | if (err < 0) |
2065 | return err; | |
2066 | ||
2067 | basechain = nft_base_chain(chain); | |
2068 | if (basechain->type != hook.type) { | |
2069 | nft_chain_release_hook(&hook); | |
2070 | return -EBUSY; | |
2071 | } | |
2072 | ||
d54725cd PNA |
2073 | if (ctx->family == NFPROTO_NETDEV) { |
2074 | if (!nft_hook_list_equal(&basechain->hook_list, | |
2075 | &hook.list)) { | |
2076 | nft_chain_release_hook(&hook); | |
2077 | return -EBUSY; | |
2078 | } | |
2079 | } else { | |
2080 | ops = &basechain->ops; | |
2081 | if (ops->hooknum != hook.num || | |
2082 | ops->priority != hook.priority) { | |
2083 | nft_chain_release_hook(&hook); | |
2084 | return -EBUSY; | |
2085 | } | |
2c4a488a PNA |
2086 | } |
2087 | nft_chain_release_hook(&hook); | |
2088 | } | |
2089 | ||
2090 | if (nla[NFTA_CHAIN_HANDLE] && | |
2091 | nla[NFTA_CHAIN_NAME]) { | |
2092 | struct nft_chain *chain2; | |
2093 | ||
f102d66b FW |
2094 | chain2 = nft_chain_lookup(ctx->net, table, |
2095 | nla[NFTA_CHAIN_NAME], genmask); | |
0d18779b JC |
2096 | if (!IS_ERR(chain2)) |
2097 | return -EEXIST; | |
2c4a488a PNA |
2098 | } |
2099 | ||
2100 | if (nla[NFTA_CHAIN_COUNTERS]) { | |
2101 | if (!nft_is_base_chain(chain)) | |
2102 | return -EOPNOTSUPP; | |
2103 | ||
2104 | stats = nft_stats_alloc(nla[NFTA_CHAIN_COUNTERS]); | |
2105 | if (IS_ERR(stats)) | |
2106 | return PTR_ERR(stats); | |
2107 | } | |
2108 | ||
c6cc94df | 2109 | err = -ENOMEM; |
2c4a488a PNA |
2110 | trans = nft_trans_alloc(ctx, NFT_MSG_NEWCHAIN, |
2111 | sizeof(struct nft_trans_chain)); | |
c6cc94df FW |
2112 | if (trans == NULL) |
2113 | goto err; | |
2c4a488a PNA |
2114 | |
2115 | nft_trans_chain_stats(trans) = stats; | |
2116 | nft_trans_chain_update(trans) = true; | |
2117 | ||
2118 | if (nla[NFTA_CHAIN_POLICY]) | |
2119 | nft_trans_chain_policy(trans) = policy; | |
2120 | else | |
2121 | nft_trans_chain_policy(trans) = -1; | |
2122 | ||
c6cc94df FW |
2123 | if (nla[NFTA_CHAIN_HANDLE] && |
2124 | nla[NFTA_CHAIN_NAME]) { | |
2125 | struct nft_trans *tmp; | |
2126 | char *name; | |
2127 | ||
2128 | err = -ENOMEM; | |
2129 | name = nla_strdup(nla[NFTA_CHAIN_NAME], GFP_KERNEL); | |
2130 | if (!name) | |
2131 | goto err; | |
2132 | ||
2133 | err = -EEXIST; | |
2134 | list_for_each_entry(tmp, &ctx->net->nft.commit_list, list) { | |
2135 | if (tmp->msg_type == NFT_MSG_NEWCHAIN && | |
2136 | tmp->ctx.table == table && | |
2137 | nft_trans_chain_update(tmp) && | |
2138 | nft_trans_chain_name(tmp) && | |
2139 | strcmp(name, nft_trans_chain_name(tmp)) == 0) { | |
2140 | kfree(name); | |
2141 | goto err; | |
2142 | } | |
2c4a488a | 2143 | } |
c6cc94df FW |
2144 | |
2145 | nft_trans_chain_name(trans) = name; | |
2c4a488a PNA |
2146 | } |
2147 | list_add_tail(&trans->list, &ctx->net->nft.commit_list); | |
2148 | ||
2149 | return 0; | |
c6cc94df FW |
2150 | err: |
2151 | free_percpu(stats); | |
2152 | kfree(trans); | |
2153 | return err; | |
2c4a488a PNA |
2154 | } |
2155 | ||
633c9a84 PNA |
2156 | static int nf_tables_newchain(struct net *net, struct sock *nlsk, |
2157 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
04ba724b PNA |
2158 | const struct nlattr * const nla[], |
2159 | struct netlink_ext_ack *extack) | |
96518518 PM |
2160 | { |
2161 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
4035285f PNA |
2162 | u8 genmask = nft_genmask_next(net); |
2163 | int family = nfmsg->nfgen_family; | |
74cccc3d | 2164 | struct nft_chain *chain = NULL; |
36dd1bcc | 2165 | const struct nlattr *attr; |
96518518 | 2166 | struct nft_table *table; |
57de2a0c | 2167 | u8 policy = NF_ACCEPT; |
4035285f | 2168 | struct nft_ctx ctx; |
96518518 | 2169 | u64 handle = 0; |
c9626a2c | 2170 | u32 flags = 0; |
96518518 | 2171 | |
f102d66b FW |
2172 | lockdep_assert_held(&net->nft.commit_mutex); |
2173 | ||
cac20fcd | 2174 | table = nft_table_lookup(net, nla[NFTA_CHAIN_TABLE], family, genmask); |
36dd1bcc PNA |
2175 | if (IS_ERR(table)) { |
2176 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TABLE]); | |
96518518 | 2177 | return PTR_ERR(table); |
36dd1bcc | 2178 | } |
96518518 | 2179 | |
96518518 | 2180 | chain = NULL; |
36dd1bcc | 2181 | attr = nla[NFTA_CHAIN_NAME]; |
96518518 PM |
2182 | |
2183 | if (nla[NFTA_CHAIN_HANDLE]) { | |
2184 | handle = be64_to_cpu(nla_get_be64(nla[NFTA_CHAIN_HANDLE])); | |
cac20fcd | 2185 | chain = nft_chain_lookup_byhandle(table, handle, genmask); |
36dd1bcc PNA |
2186 | if (IS_ERR(chain)) { |
2187 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_HANDLE]); | |
96518518 | 2188 | return PTR_ERR(chain); |
36dd1bcc PNA |
2189 | } |
2190 | attr = nla[NFTA_CHAIN_HANDLE]; | |
74cccc3d | 2191 | } else if (nla[NFTA_CHAIN_NAME]) { |
f102d66b | 2192 | chain = nft_chain_lookup(net, table, attr, genmask); |
96518518 | 2193 | if (IS_ERR(chain)) { |
36dd1bcc PNA |
2194 | if (PTR_ERR(chain) != -ENOENT) { |
2195 | NL_SET_BAD_ATTR(extack, attr); | |
96518518 | 2196 | return PTR_ERR(chain); |
36dd1bcc | 2197 | } |
96518518 PM |
2198 | chain = NULL; |
2199 | } | |
74cccc3d PNA |
2200 | } else if (!nla[NFTA_CHAIN_ID]) { |
2201 | return -EINVAL; | |
96518518 PM |
2202 | } |
2203 | ||
57de2a0c | 2204 | if (nla[NFTA_CHAIN_POLICY]) { |
f323d954 | 2205 | if (chain != NULL && |
36dd1bcc PNA |
2206 | !nft_is_base_chain(chain)) { |
2207 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_POLICY]); | |
d6b6cb1d | 2208 | return -EOPNOTSUPP; |
36dd1bcc | 2209 | } |
d6b6cb1d PNA |
2210 | |
2211 | if (chain == NULL && | |
36dd1bcc PNA |
2212 | nla[NFTA_CHAIN_HOOK] == NULL) { |
2213 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_POLICY]); | |
57de2a0c | 2214 | return -EOPNOTSUPP; |
36dd1bcc | 2215 | } |
57de2a0c | 2216 | |
8f46df18 | 2217 | policy = ntohl(nla_get_be32(nla[NFTA_CHAIN_POLICY])); |
57de2a0c PM |
2218 | switch (policy) { |
2219 | case NF_DROP: | |
2220 | case NF_ACCEPT: | |
2221 | break; | |
2222 | default: | |
2223 | return -EINVAL; | |
2224 | } | |
2225 | } | |
2226 | ||
c9626a2c PNA |
2227 | if (nla[NFTA_CHAIN_FLAGS]) |
2228 | flags = ntohl(nla_get_be32(nla[NFTA_CHAIN_FLAGS])); | |
b717273d FW |
2229 | else if (chain) |
2230 | flags = chain->flags; | |
c9626a2c | 2231 | |
98319cb9 | 2232 | nft_ctx_init(&ctx, net, skb, nlh, family, table, chain, nla); |
4035285f | 2233 | |
96518518 | 2234 | if (chain != NULL) { |
36dd1bcc PNA |
2235 | if (nlh->nlmsg_flags & NLM_F_EXCL) { |
2236 | NL_SET_BAD_ATTR(extack, attr); | |
96518518 | 2237 | return -EEXIST; |
36dd1bcc | 2238 | } |
96518518 PM |
2239 | if (nlh->nlmsg_flags & NLM_F_REPLACE) |
2240 | return -EOPNOTSUPP; | |
2241 | ||
1ed012f6 | 2242 | flags |= chain->flags & NFT_BASE_CHAIN; |
c9626a2c | 2243 | return nf_tables_updchain(&ctx, genmask, policy, flags); |
96518518 PM |
2244 | } |
2245 | ||
c9626a2c | 2246 | return nf_tables_addchain(&ctx, family, genmask, policy, flags); |
96518518 PM |
2247 | } |
2248 | ||
633c9a84 PNA |
2249 | static int nf_tables_delchain(struct net *net, struct sock *nlsk, |
2250 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
04ba724b PNA |
2251 | const struct nlattr * const nla[], |
2252 | struct netlink_ext_ack *extack) | |
96518518 PM |
2253 | { |
2254 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
f2a6d766 | 2255 | u8 genmask = nft_genmask_next(net); |
36dd1bcc PNA |
2256 | int family = nfmsg->nfgen_family; |
2257 | const struct nlattr *attr; | |
96518518 PM |
2258 | struct nft_table *table; |
2259 | struct nft_chain *chain; | |
9dee1474 | 2260 | struct nft_rule *rule; |
91c7b38d | 2261 | struct nft_ctx ctx; |
3ecbfd65 | 2262 | u64 handle; |
9dee1474 PNA |
2263 | u32 use; |
2264 | int err; | |
96518518 | 2265 | |
cac20fcd | 2266 | table = nft_table_lookup(net, nla[NFTA_CHAIN_TABLE], family, genmask); |
36dd1bcc PNA |
2267 | if (IS_ERR(table)) { |
2268 | NL_SET_BAD_ATTR(extack, nla[NFTA_CHAIN_TABLE]); | |
96518518 | 2269 | return PTR_ERR(table); |
36dd1bcc | 2270 | } |
96518518 | 2271 | |
3ecbfd65 | 2272 | if (nla[NFTA_CHAIN_HANDLE]) { |
36dd1bcc PNA |
2273 | attr = nla[NFTA_CHAIN_HANDLE]; |
2274 | handle = be64_to_cpu(nla_get_be64(attr)); | |
cac20fcd | 2275 | chain = nft_chain_lookup_byhandle(table, handle, genmask); |
3ecbfd65 | 2276 | } else { |
36dd1bcc | 2277 | attr = nla[NFTA_CHAIN_NAME]; |
f102d66b | 2278 | chain = nft_chain_lookup(net, table, attr, genmask); |
3ecbfd65 | 2279 | } |
36dd1bcc PNA |
2280 | if (IS_ERR(chain)) { |
2281 | NL_SET_BAD_ATTR(extack, attr); | |
96518518 | 2282 | return PTR_ERR(chain); |
36dd1bcc | 2283 | } |
9dee1474 PNA |
2284 | |
2285 | if (nlh->nlmsg_flags & NLM_F_NONREC && | |
2286 | chain->use > 0) | |
96518518 PM |
2287 | return -EBUSY; |
2288 | ||
98319cb9 | 2289 | nft_ctx_init(&ctx, net, skb, nlh, family, table, chain, nla); |
0165d932 | 2290 | |
9dee1474 PNA |
2291 | use = chain->use; |
2292 | list_for_each_entry(rule, &chain->rules, list) { | |
2293 | if (!nft_is_active_next(net, rule)) | |
2294 | continue; | |
2295 | use--; | |
2296 | ||
2297 | err = nft_delrule(&ctx, rule); | |
2298 | if (err < 0) | |
2299 | return err; | |
2300 | } | |
2301 | ||
2302 | /* There are rules and elements that are still holding references to us, | |
2303 | * we cannot do a recursive removal in this case. | |
2304 | */ | |
36dd1bcc PNA |
2305 | if (use > 0) { |
2306 | NL_SET_BAD_ATTR(extack, attr); | |
9dee1474 | 2307 | return -EBUSY; |
36dd1bcc | 2308 | } |
9dee1474 | 2309 | |
ee01d542 | 2310 | return nft_delchain(&ctx); |
96518518 PM |
2311 | } |
2312 | ||
96518518 PM |
2313 | /* |
2314 | * Expressions | |
2315 | */ | |
2316 | ||
2317 | /** | |
ef1f7df9 PM |
2318 | * nft_register_expr - register nf_tables expr type |
2319 | * @ops: expr type | |
96518518 | 2320 | * |
ef1f7df9 | 2321 | * Registers the expr type for use with nf_tables. Returns zero on |
96518518 PM |
2322 | * success or a negative errno code otherwise. |
2323 | */ | |
ef1f7df9 | 2324 | int nft_register_expr(struct nft_expr_type *type) |
96518518 PM |
2325 | { |
2326 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
758dbcec | 2327 | if (type->family == NFPROTO_UNSPEC) |
e688a7f8 | 2328 | list_add_tail_rcu(&type->list, &nf_tables_expressions); |
758dbcec | 2329 | else |
e688a7f8 | 2330 | list_add_rcu(&type->list, &nf_tables_expressions); |
96518518 PM |
2331 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
2332 | return 0; | |
2333 | } | |
2334 | EXPORT_SYMBOL_GPL(nft_register_expr); | |
2335 | ||
2336 | /** | |
ef1f7df9 PM |
2337 | * nft_unregister_expr - unregister nf_tables expr type |
2338 | * @ops: expr type | |
96518518 | 2339 | * |
ef1f7df9 | 2340 | * Unregisters the expr typefor use with nf_tables. |
96518518 | 2341 | */ |
ef1f7df9 | 2342 | void nft_unregister_expr(struct nft_expr_type *type) |
96518518 PM |
2343 | { |
2344 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
e688a7f8 | 2345 | list_del_rcu(&type->list); |
96518518 PM |
2346 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); |
2347 | } | |
2348 | EXPORT_SYMBOL_GPL(nft_unregister_expr); | |
2349 | ||
64d46806 PM |
2350 | static const struct nft_expr_type *__nft_expr_type_get(u8 family, |
2351 | struct nlattr *nla) | |
96518518 | 2352 | { |
9cff126f | 2353 | const struct nft_expr_type *type, *candidate = NULL; |
96518518 | 2354 | |
ef1f7df9 | 2355 | list_for_each_entry(type, &nf_tables_expressions, list) { |
9cff126f PNA |
2356 | if (!nla_strcmp(nla, type->name)) { |
2357 | if (!type->family && !candidate) | |
2358 | candidate = type; | |
2359 | else if (type->family == family) | |
2360 | candidate = type; | |
2361 | } | |
96518518 | 2362 | } |
9cff126f | 2363 | return candidate; |
96518518 PM |
2364 | } |
2365 | ||
b9c04ae7 PNA |
2366 | #ifdef CONFIG_MODULES |
2367 | static int nft_expr_type_request_module(struct net *net, u8 family, | |
2368 | struct nlattr *nla) | |
2369 | { | |
eb014de4 PNA |
2370 | if (nft_request_module(net, "nft-expr-%u-%.*s", family, |
2371 | nla_len(nla), (char *)nla_data(nla)) == -EAGAIN) | |
b9c04ae7 PNA |
2372 | return -EAGAIN; |
2373 | ||
2374 | return 0; | |
2375 | } | |
2376 | #endif | |
2377 | ||
452238e8 FW |
2378 | static const struct nft_expr_type *nft_expr_type_get(struct net *net, |
2379 | u8 family, | |
64d46806 | 2380 | struct nlattr *nla) |
96518518 | 2381 | { |
ef1f7df9 | 2382 | const struct nft_expr_type *type; |
96518518 PM |
2383 | |
2384 | if (nla == NULL) | |
2385 | return ERR_PTR(-EINVAL); | |
2386 | ||
64d46806 | 2387 | type = __nft_expr_type_get(family, nla); |
ef1f7df9 PM |
2388 | if (type != NULL && try_module_get(type->owner)) |
2389 | return type; | |
96518518 | 2390 | |
f102d66b | 2391 | lockdep_nfnl_nft_mutex_not_held(); |
96518518 | 2392 | #ifdef CONFIG_MODULES |
ef1f7df9 | 2393 | if (type == NULL) { |
b9c04ae7 | 2394 | if (nft_expr_type_request_module(net, family, nla) == -EAGAIN) |
64d46806 PM |
2395 | return ERR_PTR(-EAGAIN); |
2396 | ||
eb014de4 PNA |
2397 | if (nft_request_module(net, "nft-expr-%.*s", |
2398 | nla_len(nla), | |
2399 | (char *)nla_data(nla)) == -EAGAIN) | |
96518518 PM |
2400 | return ERR_PTR(-EAGAIN); |
2401 | } | |
2402 | #endif | |
2403 | return ERR_PTR(-ENOENT); | |
2404 | } | |
2405 | ||
2406 | static const struct nla_policy nft_expr_policy[NFTA_EXPR_MAX + 1] = { | |
9332d27d FW |
2407 | [NFTA_EXPR_NAME] = { .type = NLA_STRING, |
2408 | .len = NFT_MODULE_AUTOLOAD_LIMIT }, | |
96518518 PM |
2409 | [NFTA_EXPR_DATA] = { .type = NLA_NESTED }, |
2410 | }; | |
2411 | ||
2412 | static int nf_tables_fill_expr_info(struct sk_buff *skb, | |
2413 | const struct nft_expr *expr) | |
2414 | { | |
ef1f7df9 | 2415 | if (nla_put_string(skb, NFTA_EXPR_NAME, expr->ops->type->name)) |
96518518 PM |
2416 | goto nla_put_failure; |
2417 | ||
2418 | if (expr->ops->dump) { | |
ae0be8de MK |
2419 | struct nlattr *data = nla_nest_start_noflag(skb, |
2420 | NFTA_EXPR_DATA); | |
96518518 PM |
2421 | if (data == NULL) |
2422 | goto nla_put_failure; | |
2423 | if (expr->ops->dump(skb, expr) < 0) | |
2424 | goto nla_put_failure; | |
2425 | nla_nest_end(skb, data); | |
2426 | } | |
2427 | ||
2428 | return skb->len; | |
2429 | ||
2430 | nla_put_failure: | |
2431 | return -1; | |
2432 | }; | |
2433 | ||
0b2d8a7b PM |
2434 | int nft_expr_dump(struct sk_buff *skb, unsigned int attr, |
2435 | const struct nft_expr *expr) | |
2436 | { | |
2437 | struct nlattr *nest; | |
2438 | ||
ae0be8de | 2439 | nest = nla_nest_start_noflag(skb, attr); |
0b2d8a7b PM |
2440 | if (!nest) |
2441 | goto nla_put_failure; | |
2442 | if (nf_tables_fill_expr_info(skb, expr) < 0) | |
2443 | goto nla_put_failure; | |
2444 | nla_nest_end(skb, nest); | |
2445 | return 0; | |
2446 | ||
2447 | nla_put_failure: | |
2448 | return -1; | |
2449 | } | |
2450 | ||
96518518 PM |
2451 | struct nft_expr_info { |
2452 | const struct nft_expr_ops *ops; | |
ef1f7df9 | 2453 | struct nlattr *tb[NFT_EXPR_MAXATTR + 1]; |
96518518 PM |
2454 | }; |
2455 | ||
0ca743a5 PNA |
2456 | static int nf_tables_expr_parse(const struct nft_ctx *ctx, |
2457 | const struct nlattr *nla, | |
96518518 PM |
2458 | struct nft_expr_info *info) |
2459 | { | |
ef1f7df9 | 2460 | const struct nft_expr_type *type; |
96518518 | 2461 | const struct nft_expr_ops *ops; |
ef1f7df9 | 2462 | struct nlattr *tb[NFTA_EXPR_MAX + 1]; |
96518518 PM |
2463 | int err; |
2464 | ||
8cb08174 JB |
2465 | err = nla_parse_nested_deprecated(tb, NFTA_EXPR_MAX, nla, |
2466 | nft_expr_policy, NULL); | |
96518518 PM |
2467 | if (err < 0) |
2468 | return err; | |
2469 | ||
452238e8 | 2470 | type = nft_expr_type_get(ctx->net, ctx->family, tb[NFTA_EXPR_NAME]); |
ef1f7df9 PM |
2471 | if (IS_ERR(type)) |
2472 | return PTR_ERR(type); | |
2473 | ||
2474 | if (tb[NFTA_EXPR_DATA]) { | |
8cb08174 JB |
2475 | err = nla_parse_nested_deprecated(info->tb, type->maxattr, |
2476 | tb[NFTA_EXPR_DATA], | |
2477 | type->policy, NULL); | |
ef1f7df9 PM |
2478 | if (err < 0) |
2479 | goto err1; | |
2480 | } else | |
2481 | memset(info->tb, 0, sizeof(info->tb[0]) * (type->maxattr + 1)); | |
2482 | ||
2483 | if (type->select_ops != NULL) { | |
0ca743a5 PNA |
2484 | ops = type->select_ops(ctx, |
2485 | (const struct nlattr * const *)info->tb); | |
ef1f7df9 PM |
2486 | if (IS_ERR(ops)) { |
2487 | err = PTR_ERR(ops); | |
0ef1efd1 PNA |
2488 | #ifdef CONFIG_MODULES |
2489 | if (err == -EAGAIN) | |
eb014de4 PNA |
2490 | if (nft_expr_type_request_module(ctx->net, |
2491 | ctx->family, | |
2492 | tb[NFTA_EXPR_NAME]) != -EAGAIN) | |
2493 | err = -ENOENT; | |
0ef1efd1 | 2494 | #endif |
ef1f7df9 PM |
2495 | goto err1; |
2496 | } | |
2497 | } else | |
2498 | ops = type->ops; | |
2499 | ||
96518518 PM |
2500 | info->ops = ops; |
2501 | return 0; | |
ef1f7df9 PM |
2502 | |
2503 | err1: | |
2504 | module_put(type->owner); | |
2505 | return err; | |
96518518 PM |
2506 | } |
2507 | ||
2508 | static int nf_tables_newexpr(const struct nft_ctx *ctx, | |
ef1f7df9 | 2509 | const struct nft_expr_info *info, |
96518518 PM |
2510 | struct nft_expr *expr) |
2511 | { | |
2512 | const struct nft_expr_ops *ops = info->ops; | |
2513 | int err; | |
2514 | ||
2515 | expr->ops = ops; | |
2516 | if (ops->init) { | |
ef1f7df9 | 2517 | err = ops->init(ctx, expr, (const struct nlattr **)info->tb); |
96518518 PM |
2518 | if (err < 0) |
2519 | goto err1; | |
2520 | } | |
2521 | ||
96518518 | 2522 | return 0; |
96518518 PM |
2523 | err1: |
2524 | expr->ops = NULL; | |
2525 | return err; | |
2526 | } | |
2527 | ||
62472bce PM |
2528 | static void nf_tables_expr_destroy(const struct nft_ctx *ctx, |
2529 | struct nft_expr *expr) | |
96518518 | 2530 | { |
3f3a390d PNA |
2531 | const struct nft_expr_type *type = expr->ops->type; |
2532 | ||
96518518 | 2533 | if (expr->ops->destroy) |
62472bce | 2534 | expr->ops->destroy(ctx, expr); |
3f3a390d | 2535 | module_put(type->owner); |
96518518 PM |
2536 | } |
2537 | ||
795a6d6b PNA |
2538 | static struct nft_expr *nft_expr_init(const struct nft_ctx *ctx, |
2539 | const struct nlattr *nla) | |
0b2d8a7b PM |
2540 | { |
2541 | struct nft_expr_info info; | |
2542 | struct nft_expr *expr; | |
b8e20400 | 2543 | struct module *owner; |
0b2d8a7b PM |
2544 | int err; |
2545 | ||
2546 | err = nf_tables_expr_parse(ctx, nla, &info); | |
2547 | if (err < 0) | |
2548 | goto err1; | |
2549 | ||
2550 | err = -ENOMEM; | |
2551 | expr = kzalloc(info.ops->size, GFP_KERNEL); | |
2552 | if (expr == NULL) | |
2553 | goto err2; | |
2554 | ||
2555 | err = nf_tables_newexpr(ctx, &info, expr); | |
2556 | if (err < 0) | |
6cafaf47 | 2557 | goto err3; |
0b2d8a7b PM |
2558 | |
2559 | return expr; | |
6cafaf47 LZ |
2560 | err3: |
2561 | kfree(expr); | |
0b2d8a7b | 2562 | err2: |
b8e20400 PNA |
2563 | owner = info.ops->type->owner; |
2564 | if (info.ops->type->release_ops) | |
2565 | info.ops->type->release_ops(info.ops); | |
2566 | ||
2567 | module_put(owner); | |
0b2d8a7b PM |
2568 | err1: |
2569 | return ERR_PTR(err); | |
2570 | } | |
2571 | ||
c604cc69 PNA |
2572 | int nft_expr_clone(struct nft_expr *dst, struct nft_expr *src) |
2573 | { | |
2574 | int err; | |
2575 | ||
2576 | if (src->ops->clone) { | |
2577 | dst->ops = src->ops; | |
2578 | err = src->ops->clone(dst, src); | |
2579 | if (err < 0) | |
2580 | return err; | |
2581 | } else { | |
2582 | memcpy(dst, src, src->ops->size); | |
2583 | } | |
2584 | ||
2585 | __module_get(src->ops->type->owner); | |
2586 | ||
2587 | return 0; | |
2588 | } | |
2589 | ||
0b2d8a7b PM |
2590 | void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr) |
2591 | { | |
2592 | nf_tables_expr_destroy(ctx, expr); | |
2593 | kfree(expr); | |
2594 | } | |
2595 | ||
96518518 PM |
2596 | /* |
2597 | * Rules | |
2598 | */ | |
2599 | ||
cac20fcd PNA |
2600 | static struct nft_rule *__nft_rule_lookup(const struct nft_chain *chain, |
2601 | u64 handle) | |
96518518 PM |
2602 | { |
2603 | struct nft_rule *rule; | |
2604 | ||
2605 | // FIXME: this sucks | |
d9adf22a | 2606 | list_for_each_entry_rcu(rule, &chain->rules, list) { |
96518518 PM |
2607 | if (handle == rule->handle) |
2608 | return rule; | |
2609 | } | |
2610 | ||
2611 | return ERR_PTR(-ENOENT); | |
2612 | } | |
2613 | ||
cac20fcd PNA |
2614 | static struct nft_rule *nft_rule_lookup(const struct nft_chain *chain, |
2615 | const struct nlattr *nla) | |
96518518 PM |
2616 | { |
2617 | if (nla == NULL) | |
2618 | return ERR_PTR(-EINVAL); | |
2619 | ||
cac20fcd | 2620 | return __nft_rule_lookup(chain, be64_to_cpu(nla_get_be64(nla))); |
96518518 PM |
2621 | } |
2622 | ||
2623 | static const struct nla_policy nft_rule_policy[NFTA_RULE_MAX + 1] = { | |
b2fbd044 LZ |
2624 | [NFTA_RULE_TABLE] = { .type = NLA_STRING, |
2625 | .len = NFT_TABLE_MAXNAMELEN - 1 }, | |
96518518 PM |
2626 | [NFTA_RULE_CHAIN] = { .type = NLA_STRING, |
2627 | .len = NFT_CHAIN_MAXNAMELEN - 1 }, | |
2628 | [NFTA_RULE_HANDLE] = { .type = NLA_U64 }, | |
2629 | [NFTA_RULE_EXPRESSIONS] = { .type = NLA_NESTED }, | |
0ca743a5 | 2630 | [NFTA_RULE_COMPAT] = { .type = NLA_NESTED }, |
5e948466 | 2631 | [NFTA_RULE_POSITION] = { .type = NLA_U64 }, |
0768b3b3 PNA |
2632 | [NFTA_RULE_USERDATA] = { .type = NLA_BINARY, |
2633 | .len = NFT_USERDATA_MAXLEN }, | |
467697d2 | 2634 | [NFTA_RULE_ID] = { .type = NLA_U32 }, |
0604628b | 2635 | [NFTA_RULE_POSITION_ID] = { .type = NLA_U32 }, |
96518518 PM |
2636 | }; |
2637 | ||
84d7fce6 PNA |
2638 | static int nf_tables_fill_rule_info(struct sk_buff *skb, struct net *net, |
2639 | u32 portid, u32 seq, int event, | |
2640 | u32 flags, int family, | |
96518518 PM |
2641 | const struct nft_table *table, |
2642 | const struct nft_chain *chain, | |
2c82c7e7 FW |
2643 | const struct nft_rule *rule, |
2644 | const struct nft_rule *prule) | |
96518518 PM |
2645 | { |
2646 | struct nlmsghdr *nlh; | |
2647 | struct nfgenmsg *nfmsg; | |
2648 | const struct nft_expr *expr, *next; | |
2649 | struct nlattr *list; | |
dedb67c4 | 2650 | u16 type = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); |
96518518 | 2651 | |
dedb67c4 | 2652 | nlh = nlmsg_put(skb, portid, seq, type, sizeof(struct nfgenmsg), flags); |
96518518 PM |
2653 | if (nlh == NULL) |
2654 | goto nla_put_failure; | |
2655 | ||
2656 | nfmsg = nlmsg_data(nlh); | |
2657 | nfmsg->nfgen_family = family; | |
2658 | nfmsg->version = NFNETLINK_V0; | |
84d7fce6 | 2659 | nfmsg->res_id = htons(net->nft.base_seq & 0xffff); |
96518518 PM |
2660 | |
2661 | if (nla_put_string(skb, NFTA_RULE_TABLE, table->name)) | |
2662 | goto nla_put_failure; | |
2663 | if (nla_put_string(skb, NFTA_RULE_CHAIN, chain->name)) | |
2664 | goto nla_put_failure; | |
b46f6ded ND |
2665 | if (nla_put_be64(skb, NFTA_RULE_HANDLE, cpu_to_be64(rule->handle), |
2666 | NFTA_RULE_PAD)) | |
96518518 PM |
2667 | goto nla_put_failure; |
2668 | ||
2c82c7e7 | 2669 | if (event != NFT_MSG_DELRULE && prule) { |
5e948466 | 2670 | if (nla_put_be64(skb, NFTA_RULE_POSITION, |
b46f6ded ND |
2671 | cpu_to_be64(prule->handle), |
2672 | NFTA_RULE_PAD)) | |
5e948466 EL |
2673 | goto nla_put_failure; |
2674 | } | |
2675 | ||
ae0be8de | 2676 | list = nla_nest_start_noflag(skb, NFTA_RULE_EXPRESSIONS); |
96518518 PM |
2677 | if (list == NULL) |
2678 | goto nla_put_failure; | |
2679 | nft_rule_for_each_expr(expr, next, rule) { | |
0b2d8a7b | 2680 | if (nft_expr_dump(skb, NFTA_LIST_ELEM, expr) < 0) |
96518518 | 2681 | goto nla_put_failure; |
96518518 PM |
2682 | } |
2683 | nla_nest_end(skb, list); | |
2684 | ||
86f1ec32 PM |
2685 | if (rule->udata) { |
2686 | struct nft_userdata *udata = nft_userdata(rule); | |
2687 | if (nla_put(skb, NFTA_RULE_USERDATA, udata->len + 1, | |
2688 | udata->data) < 0) | |
2689 | goto nla_put_failure; | |
2690 | } | |
0768b3b3 | 2691 | |
053c095a JB |
2692 | nlmsg_end(skb, nlh); |
2693 | return 0; | |
96518518 PM |
2694 | |
2695 | nla_put_failure: | |
2696 | nlmsg_trim(skb, nlh); | |
2697 | return -1; | |
2698 | } | |
2699 | ||
25e94a99 PNA |
2700 | static void nf_tables_rule_notify(const struct nft_ctx *ctx, |
2701 | const struct nft_rule *rule, int event) | |
96518518 PM |
2702 | { |
2703 | struct sk_buff *skb; | |
96518518 PM |
2704 | int err; |
2705 | ||
128ad332 PNA |
2706 | if (!ctx->report && |
2707 | !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES)) | |
25e94a99 | 2708 | return; |
96518518 | 2709 | |
96518518 PM |
2710 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
2711 | if (skb == NULL) | |
2712 | goto err; | |
2713 | ||
84d7fce6 | 2714 | err = nf_tables_fill_rule_info(skb, ctx->net, ctx->portid, ctx->seq, |
36596dad | 2715 | event, 0, ctx->family, ctx->table, |
2c82c7e7 | 2716 | ctx->chain, rule, NULL); |
96518518 PM |
2717 | if (err < 0) { |
2718 | kfree_skb(skb); | |
2719 | goto err; | |
2720 | } | |
2721 | ||
25e94a99 PNA |
2722 | nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES, |
2723 | ctx->report, GFP_KERNEL); | |
2724 | return; | |
96518518 | 2725 | err: |
25e94a99 | 2726 | nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS); |
96518518 PM |
2727 | } |
2728 | ||
6e1f760e | 2729 | struct nft_rule_dump_ctx { |
e46abbcc | 2730 | char *table; |
b7263e07 | 2731 | char *chain; |
6e1f760e PNA |
2732 | }; |
2733 | ||
241faece PS |
2734 | static int __nf_tables_dump_rules(struct sk_buff *skb, |
2735 | unsigned int *idx, | |
2736 | struct netlink_callback *cb, | |
2737 | const struct nft_table *table, | |
2738 | const struct nft_chain *chain) | |
2739 | { | |
2740 | struct net *net = sock_net(skb->sk); | |
2c82c7e7 | 2741 | const struct nft_rule *rule, *prule; |
241faece | 2742 | unsigned int s_idx = cb->args[0]; |
241faece | 2743 | |
2c82c7e7 | 2744 | prule = NULL; |
241faece PS |
2745 | list_for_each_entry_rcu(rule, &chain->rules, list) { |
2746 | if (!nft_is_active(net, rule)) | |
2c82c7e7 | 2747 | goto cont_skip; |
241faece PS |
2748 | if (*idx < s_idx) |
2749 | goto cont; | |
2750 | if (*idx > s_idx) { | |
2751 | memset(&cb->args[1], 0, | |
2752 | sizeof(cb->args) - sizeof(cb->args[0])); | |
2753 | } | |
2754 | if (nf_tables_fill_rule_info(skb, net, NETLINK_CB(cb->skb).portid, | |
2755 | cb->nlh->nlmsg_seq, | |
2756 | NFT_MSG_NEWRULE, | |
2757 | NLM_F_MULTI | NLM_F_APPEND, | |
2758 | table->family, | |
2c82c7e7 | 2759 | table, chain, rule, prule) < 0) |
310529e6 | 2760 | return 1; |
241faece PS |
2761 | |
2762 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); | |
2763 | cont: | |
2c82c7e7 FW |
2764 | prule = rule; |
2765 | cont_skip: | |
241faece PS |
2766 | (*idx)++; |
2767 | } | |
310529e6 | 2768 | return 0; |
241faece PS |
2769 | } |
2770 | ||
96518518 PM |
2771 | static int nf_tables_dump_rules(struct sk_buff *skb, |
2772 | struct netlink_callback *cb) | |
2773 | { | |
2774 | const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); | |
6e1f760e | 2775 | const struct nft_rule_dump_ctx *ctx = cb->data; |
241faece | 2776 | struct nft_table *table; |
96518518 | 2777 | const struct nft_chain *chain; |
241faece | 2778 | unsigned int idx = 0; |
99633ab2 | 2779 | struct net *net = sock_net(skb->sk); |
96518518 PM |
2780 | int family = nfmsg->nfgen_family; |
2781 | ||
e688a7f8 | 2782 | rcu_read_lock(); |
38e029f1 PNA |
2783 | cb->seq = net->nft.base_seq; |
2784 | ||
36596dad | 2785 | list_for_each_entry_rcu(table, &net->nft.tables, list) { |
98319cb9 | 2786 | if (family != NFPROTO_UNSPEC && family != table->family) |
36596dad PNA |
2787 | continue; |
2788 | ||
2789 | if (ctx && ctx->table && strcmp(ctx->table, table->name) != 0) | |
96518518 PM |
2790 | continue; |
2791 | ||
715849ab | 2792 | if (ctx && ctx->table && ctx->chain) { |
241faece | 2793 | struct rhlist_head *list, *tmp; |
6e1f760e | 2794 | |
241faece PS |
2795 | list = rhltable_lookup(&table->chains_ht, ctx->chain, |
2796 | nft_chain_ht_params); | |
2797 | if (!list) | |
2798 | goto done; | |
2799 | ||
2800 | rhl_for_each_entry_rcu(chain, tmp, list, rhlhead) { | |
2801 | if (!nft_is_active(net, chain)) | |
2802 | continue; | |
2803 | __nf_tables_dump_rules(skb, &idx, | |
2804 | cb, table, chain); | |
2805 | break; | |
96518518 | 2806 | } |
241faece | 2807 | goto done; |
96518518 | 2808 | } |
241faece PS |
2809 | |
2810 | list_for_each_entry_rcu(chain, &table->chains, list) { | |
2811 | if (__nf_tables_dump_rules(skb, &idx, cb, table, chain)) | |
2812 | goto done; | |
2813 | } | |
2814 | ||
2815 | if (ctx && ctx->table) | |
2816 | break; | |
96518518 PM |
2817 | } |
2818 | done: | |
e688a7f8 | 2819 | rcu_read_unlock(); |
310529e6 PS |
2820 | |
2821 | cb->args[0] = idx; | |
96518518 PM |
2822 | return skb->len; |
2823 | } | |
2824 | ||
90fd131a FW |
2825 | static int nf_tables_dump_rules_start(struct netlink_callback *cb) |
2826 | { | |
2827 | const struct nlattr * const *nla = cb->data; | |
2828 | struct nft_rule_dump_ctx *ctx = NULL; | |
2829 | ||
2830 | if (nla[NFTA_RULE_TABLE] || nla[NFTA_RULE_CHAIN]) { | |
2831 | ctx = kzalloc(sizeof(*ctx), GFP_ATOMIC); | |
2832 | if (!ctx) | |
2833 | return -ENOMEM; | |
2834 | ||
2835 | if (nla[NFTA_RULE_TABLE]) { | |
2836 | ctx->table = nla_strdup(nla[NFTA_RULE_TABLE], | |
2837 | GFP_ATOMIC); | |
2838 | if (!ctx->table) { | |
2839 | kfree(ctx); | |
2840 | return -ENOMEM; | |
2841 | } | |
2842 | } | |
2843 | if (nla[NFTA_RULE_CHAIN]) { | |
2844 | ctx->chain = nla_strdup(nla[NFTA_RULE_CHAIN], | |
2845 | GFP_ATOMIC); | |
2846 | if (!ctx->chain) { | |
2847 | kfree(ctx->table); | |
2848 | kfree(ctx); | |
2849 | return -ENOMEM; | |
2850 | } | |
2851 | } | |
2852 | } | |
2853 | ||
2854 | cb->data = ctx; | |
2855 | return 0; | |
2856 | } | |
2857 | ||
6e1f760e PNA |
2858 | static int nf_tables_dump_rules_done(struct netlink_callback *cb) |
2859 | { | |
e46abbcc PS |
2860 | struct nft_rule_dump_ctx *ctx = cb->data; |
2861 | ||
2862 | if (ctx) { | |
2863 | kfree(ctx->table); | |
b7263e07 | 2864 | kfree(ctx->chain); |
e46abbcc PS |
2865 | kfree(ctx); |
2866 | } | |
6e1f760e PNA |
2867 | return 0; |
2868 | } | |
2869 | ||
d9adf22a | 2870 | /* called with rcu_read_lock held */ |
7b8002a1 PNA |
2871 | static int nf_tables_getrule(struct net *net, struct sock *nlsk, |
2872 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
04ba724b PNA |
2873 | const struct nlattr * const nla[], |
2874 | struct netlink_ext_ack *extack) | |
96518518 PM |
2875 | { |
2876 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
f2a6d766 | 2877 | u8 genmask = nft_genmask_cur(net); |
96518518 PM |
2878 | const struct nft_chain *chain; |
2879 | const struct nft_rule *rule; | |
1b2470e5 | 2880 | struct nft_table *table; |
96518518 PM |
2881 | struct sk_buff *skb2; |
2882 | int family = nfmsg->nfgen_family; | |
2883 | int err; | |
2884 | ||
2885 | if (nlh->nlmsg_flags & NLM_F_DUMP) { | |
2886 | struct netlink_dump_control c = { | |
90fd131a | 2887 | .start= nf_tables_dump_rules_start, |
96518518 | 2888 | .dump = nf_tables_dump_rules, |
6e1f760e | 2889 | .done = nf_tables_dump_rules_done, |
d9adf22a | 2890 | .module = THIS_MODULE, |
90fd131a | 2891 | .data = (void *)nla, |
96518518 | 2892 | }; |
6e1f760e | 2893 | |
d9adf22a | 2894 | return nft_netlink_dump_start_rcu(nlsk, skb, nlh, &c); |
96518518 PM |
2895 | } |
2896 | ||
cac20fcd | 2897 | table = nft_table_lookup(net, nla[NFTA_RULE_TABLE], family, genmask); |
36dd1bcc PNA |
2898 | if (IS_ERR(table)) { |
2899 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_TABLE]); | |
96518518 | 2900 | return PTR_ERR(table); |
36dd1bcc | 2901 | } |
96518518 | 2902 | |
f102d66b | 2903 | chain = nft_chain_lookup(net, table, nla[NFTA_RULE_CHAIN], genmask); |
36dd1bcc PNA |
2904 | if (IS_ERR(chain)) { |
2905 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_CHAIN]); | |
96518518 | 2906 | return PTR_ERR(chain); |
36dd1bcc | 2907 | } |
96518518 | 2908 | |
cac20fcd | 2909 | rule = nft_rule_lookup(chain, nla[NFTA_RULE_HANDLE]); |
36dd1bcc PNA |
2910 | if (IS_ERR(rule)) { |
2911 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]); | |
96518518 | 2912 | return PTR_ERR(rule); |
36dd1bcc | 2913 | } |
96518518 | 2914 | |
d9adf22a | 2915 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
96518518 PM |
2916 | if (!skb2) |
2917 | return -ENOMEM; | |
2918 | ||
84d7fce6 | 2919 | err = nf_tables_fill_rule_info(skb2, net, NETLINK_CB(skb).portid, |
96518518 | 2920 | nlh->nlmsg_seq, NFT_MSG_NEWRULE, 0, |
2c82c7e7 | 2921 | family, table, chain, rule, NULL); |
96518518 PM |
2922 | if (err < 0) |
2923 | goto err; | |
2924 | ||
2925 | return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid); | |
2926 | ||
2927 | err: | |
2928 | kfree_skb(skb2); | |
2929 | return err; | |
2930 | } | |
2931 | ||
62472bce PM |
2932 | static void nf_tables_rule_destroy(const struct nft_ctx *ctx, |
2933 | struct nft_rule *rule) | |
96518518 | 2934 | { |
29e38801 | 2935 | struct nft_expr *expr, *next; |
96518518 PM |
2936 | |
2937 | /* | |
2938 | * Careful: some expressions might not be initialized in case this | |
2939 | * is called on error from nf_tables_newrule(). | |
2940 | */ | |
2941 | expr = nft_expr_first(rule); | |
3e38df13 | 2942 | while (expr != nft_expr_last(rule) && expr->ops) { |
29e38801 | 2943 | next = nft_expr_next(expr); |
62472bce | 2944 | nf_tables_expr_destroy(ctx, expr); |
29e38801 | 2945 | expr = next; |
96518518 PM |
2946 | } |
2947 | kfree(rule); | |
2948 | } | |
2949 | ||
bb7b40ae PNA |
2950 | static void nf_tables_rule_release(const struct nft_ctx *ctx, |
2951 | struct nft_rule *rule) | |
2952 | { | |
f6ac8585 | 2953 | nft_rule_expr_deactivate(ctx, rule, NFT_TRANS_RELEASE); |
bb7b40ae PNA |
2954 | nf_tables_rule_destroy(ctx, rule); |
2955 | } | |
2956 | ||
a654de8f PNA |
2957 | int nft_chain_validate(const struct nft_ctx *ctx, const struct nft_chain *chain) |
2958 | { | |
2959 | struct nft_expr *expr, *last; | |
2960 | const struct nft_data *data; | |
2961 | struct nft_rule *rule; | |
2962 | int err; | |
2963 | ||
26b2f552 TY |
2964 | if (ctx->level == NFT_JUMP_STACK_SIZE) |
2965 | return -EMLINK; | |
2966 | ||
a654de8f PNA |
2967 | list_for_each_entry(rule, &chain->rules, list) { |
2968 | if (!nft_is_active_next(ctx->net, rule)) | |
2969 | continue; | |
2970 | ||
2971 | nft_rule_for_each_expr(expr, last, rule) { | |
2972 | if (!expr->ops->validate) | |
2973 | continue; | |
2974 | ||
2975 | err = expr->ops->validate(ctx, expr, &data); | |
2976 | if (err < 0) | |
2977 | return err; | |
2978 | } | |
2979 | } | |
2980 | ||
2981 | return 0; | |
2982 | } | |
2983 | EXPORT_SYMBOL_GPL(nft_chain_validate); | |
2984 | ||
2985 | static int nft_table_validate(struct net *net, const struct nft_table *table) | |
2986 | { | |
2987 | struct nft_chain *chain; | |
2988 | struct nft_ctx ctx = { | |
2989 | .net = net, | |
2990 | .family = table->family, | |
2991 | }; | |
2992 | int err; | |
2993 | ||
2994 | list_for_each_entry(chain, &table->chains, list) { | |
2995 | if (!nft_is_base_chain(chain)) | |
2996 | continue; | |
2997 | ||
2998 | ctx.chain = chain; | |
2999 | err = nft_chain_validate(&ctx, chain); | |
3000 | if (err < 0) | |
3001 | return err; | |
3002 | } | |
3003 | ||
3004 | return 0; | |
3005 | } | |
3006 | ||
75dd48e2 PS |
3007 | static struct nft_rule *nft_rule_lookup_byid(const struct net *net, |
3008 | const struct nlattr *nla); | |
3009 | ||
1081d11b PNA |
3010 | #define NFT_RULE_MAXEXPRS 128 |
3011 | ||
633c9a84 PNA |
3012 | static int nf_tables_newrule(struct net *net, struct sock *nlsk, |
3013 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
04ba724b PNA |
3014 | const struct nlattr * const nla[], |
3015 | struct netlink_ext_ack *extack) | |
96518518 PM |
3016 | { |
3017 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
f2a6d766 | 3018 | u8 genmask = nft_genmask_next(net); |
2a43ecf9 | 3019 | struct nft_expr_info *info = NULL; |
98319cb9 | 3020 | int family = nfmsg->nfgen_family; |
c9626a2c | 3021 | struct nft_flow_rule *flow; |
96518518 PM |
3022 | struct nft_table *table; |
3023 | struct nft_chain *chain; | |
3024 | struct nft_rule *rule, *old_rule = NULL; | |
86f1ec32 | 3025 | struct nft_userdata *udata; |
1081d11b | 3026 | struct nft_trans *trans = NULL; |
96518518 PM |
3027 | struct nft_expr *expr; |
3028 | struct nft_ctx ctx; | |
3029 | struct nlattr *tmp; | |
86f1ec32 | 3030 | unsigned int size, i, n, ulen = 0, usize = 0; |
96518518 | 3031 | int err, rem; |
5e948466 | 3032 | u64 handle, pos_handle; |
96518518 | 3033 | |
f102d66b FW |
3034 | lockdep_assert_held(&net->nft.commit_mutex); |
3035 | ||
cac20fcd | 3036 | table = nft_table_lookup(net, nla[NFTA_RULE_TABLE], family, genmask); |
36dd1bcc PNA |
3037 | if (IS_ERR(table)) { |
3038 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_TABLE]); | |
96518518 | 3039 | return PTR_ERR(table); |
36dd1bcc | 3040 | } |
96518518 | 3041 | |
f102d66b | 3042 | chain = nft_chain_lookup(net, table, nla[NFTA_RULE_CHAIN], genmask); |
36dd1bcc PNA |
3043 | if (IS_ERR(chain)) { |
3044 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_CHAIN]); | |
96518518 | 3045 | return PTR_ERR(chain); |
36dd1bcc | 3046 | } |
96518518 PM |
3047 | |
3048 | if (nla[NFTA_RULE_HANDLE]) { | |
3049 | handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_HANDLE])); | |
cac20fcd | 3050 | rule = __nft_rule_lookup(chain, handle); |
36dd1bcc PNA |
3051 | if (IS_ERR(rule)) { |
3052 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]); | |
96518518 | 3053 | return PTR_ERR(rule); |
36dd1bcc | 3054 | } |
96518518 | 3055 | |
36dd1bcc PNA |
3056 | if (nlh->nlmsg_flags & NLM_F_EXCL) { |
3057 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]); | |
96518518 | 3058 | return -EEXIST; |
36dd1bcc | 3059 | } |
96518518 PM |
3060 | if (nlh->nlmsg_flags & NLM_F_REPLACE) |
3061 | old_rule = rule; | |
3062 | else | |
3063 | return -EOPNOTSUPP; | |
3064 | } else { | |
445509eb PNA |
3065 | if (!(nlh->nlmsg_flags & NLM_F_CREATE) || |
3066 | nlh->nlmsg_flags & NLM_F_REPLACE) | |
96518518 PM |
3067 | return -EINVAL; |
3068 | handle = nf_tables_alloc_handle(table); | |
a0a7379e PNA |
3069 | |
3070 | if (chain->use == UINT_MAX) | |
3071 | return -EOVERFLOW; | |
5e948466 | 3072 | |
447750f2 FW |
3073 | if (nla[NFTA_RULE_POSITION]) { |
3074 | pos_handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_POSITION])); | |
3075 | old_rule = __nft_rule_lookup(chain, pos_handle); | |
3076 | if (IS_ERR(old_rule)) { | |
3077 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_POSITION]); | |
3078 | return PTR_ERR(old_rule); | |
3079 | } | |
75dd48e2 PS |
3080 | } else if (nla[NFTA_RULE_POSITION_ID]) { |
3081 | old_rule = nft_rule_lookup_byid(net, nla[NFTA_RULE_POSITION_ID]); | |
3082 | if (IS_ERR(old_rule)) { | |
3083 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_POSITION_ID]); | |
3084 | return PTR_ERR(old_rule); | |
3085 | } | |
36dd1bcc | 3086 | } |
5e948466 EL |
3087 | } |
3088 | ||
98319cb9 | 3089 | nft_ctx_init(&ctx, net, skb, nlh, family, table, chain, nla); |
0ca743a5 | 3090 | |
96518518 PM |
3091 | n = 0; |
3092 | size = 0; | |
3093 | if (nla[NFTA_RULE_EXPRESSIONS]) { | |
2a43ecf9 FW |
3094 | info = kvmalloc_array(NFT_RULE_MAXEXPRS, |
3095 | sizeof(struct nft_expr_info), | |
3096 | GFP_KERNEL); | |
3097 | if (!info) | |
3098 | return -ENOMEM; | |
3099 | ||
96518518 PM |
3100 | nla_for_each_nested(tmp, nla[NFTA_RULE_EXPRESSIONS], rem) { |
3101 | err = -EINVAL; | |
3102 | if (nla_type(tmp) != NFTA_LIST_ELEM) | |
3103 | goto err1; | |
3104 | if (n == NFT_RULE_MAXEXPRS) | |
3105 | goto err1; | |
0ca743a5 | 3106 | err = nf_tables_expr_parse(&ctx, tmp, &info[n]); |
96518518 PM |
3107 | if (err < 0) |
3108 | goto err1; | |
3109 | size += info[n].ops->size; | |
3110 | n++; | |
3111 | } | |
3112 | } | |
9889840f PM |
3113 | /* Check for overflow of dlen field */ |
3114 | err = -EFBIG; | |
3115 | if (size >= 1 << 12) | |
3116 | goto err1; | |
96518518 | 3117 | |
86f1ec32 | 3118 | if (nla[NFTA_RULE_USERDATA]) { |
0768b3b3 | 3119 | ulen = nla_len(nla[NFTA_RULE_USERDATA]); |
86f1ec32 PM |
3120 | if (ulen > 0) |
3121 | usize = sizeof(struct nft_userdata) + ulen; | |
3122 | } | |
0768b3b3 | 3123 | |
96518518 | 3124 | err = -ENOMEM; |
86f1ec32 | 3125 | rule = kzalloc(sizeof(*rule) + size + usize, GFP_KERNEL); |
96518518 PM |
3126 | if (rule == NULL) |
3127 | goto err1; | |
3128 | ||
889f7ee7 | 3129 | nft_activate_next(net, rule); |
0628b123 | 3130 | |
96518518 PM |
3131 | rule->handle = handle; |
3132 | rule->dlen = size; | |
86f1ec32 | 3133 | rule->udata = ulen ? 1 : 0; |
0768b3b3 | 3134 | |
86f1ec32 PM |
3135 | if (ulen) { |
3136 | udata = nft_userdata(rule); | |
3137 | udata->len = ulen - 1; | |
3138 | nla_memcpy(udata->data, nla[NFTA_RULE_USERDATA], ulen); | |
3139 | } | |
96518518 | 3140 | |
96518518 PM |
3141 | expr = nft_expr_first(rule); |
3142 | for (i = 0; i < n; i++) { | |
3143 | err = nf_tables_newexpr(&ctx, &info[i], expr); | |
3144 | if (err < 0) | |
3145 | goto err2; | |
a654de8f PNA |
3146 | |
3147 | if (info[i].ops->validate) | |
3148 | nft_validate_state_update(net, NFT_VALIDATE_NEED); | |
3149 | ||
ef1f7df9 | 3150 | info[i].ops = NULL; |
96518518 PM |
3151 | expr = nft_expr_next(expr); |
3152 | } | |
3153 | ||
96518518 | 3154 | if (nlh->nlmsg_flags & NLM_F_REPLACE) { |
ca089878 | 3155 | trans = nft_trans_rule_add(&ctx, NFT_MSG_NEWRULE, rule); |
569ccae6 FW |
3156 | if (trans == NULL) { |
3157 | err = -ENOMEM; | |
3158 | goto err2; | |
3159 | } | |
ca089878 TY |
3160 | err = nft_delrule(&ctx, old_rule); |
3161 | if (err < 0) { | |
3162 | nft_trans_destroy(trans); | |
569ccae6 FW |
3163 | goto err2; |
3164 | } | |
3165 | ||
3166 | list_add_tail_rcu(&rule->list, &old_rule->list); | |
3167 | } else { | |
c9626a2c PNA |
3168 | trans = nft_trans_rule_add(&ctx, NFT_MSG_NEWRULE, rule); |
3169 | if (!trans) { | |
569ccae6 FW |
3170 | err = -ENOMEM; |
3171 | goto err2; | |
3172 | } | |
3173 | ||
3174 | if (nlh->nlmsg_flags & NLM_F_APPEND) { | |
3175 | if (old_rule) | |
3176 | list_add_rcu(&rule->list, &old_rule->list); | |
3177 | else | |
3178 | list_add_tail_rcu(&rule->list, &chain->rules); | |
3179 | } else { | |
3180 | if (old_rule) | |
3181 | list_add_tail_rcu(&rule->list, &old_rule->list); | |
3182 | else | |
3183 | list_add_rcu(&rule->list, &chain->rules); | |
3184 | } | |
0628b123 | 3185 | } |
2a43ecf9 | 3186 | kvfree(info); |
4fefee57 | 3187 | chain->use++; |
96518518 | 3188 | |
a654de8f PNA |
3189 | if (net->nft.validate_state == NFT_VALIDATE_DO) |
3190 | return nft_table_validate(net, table); | |
3191 | ||
c9626a2c | 3192 | if (chain->flags & NFT_CHAIN_HW_OFFLOAD) { |
be2861dc | 3193 | flow = nft_flow_rule_create(net, rule); |
c9626a2c PNA |
3194 | if (IS_ERR(flow)) |
3195 | return PTR_ERR(flow); | |
3196 | ||
3197 | nft_trans_flow_rule(trans) = flow; | |
3198 | } | |
3199 | ||
a654de8f | 3200 | return 0; |
96518518 | 3201 | err2: |
bb7b40ae | 3202 | nf_tables_rule_release(&ctx, rule); |
96518518 PM |
3203 | err1: |
3204 | for (i = 0; i < n; i++) { | |
b25a31bf | 3205 | if (info[i].ops) { |
ef1f7df9 | 3206 | module_put(info[i].ops->type->owner); |
b25a31bf TY |
3207 | if (info[i].ops->type->release_ops) |
3208 | info[i].ops->type->release_ops(info[i].ops); | |
3209 | } | |
96518518 | 3210 | } |
2a43ecf9 | 3211 | kvfree(info); |
96518518 PM |
3212 | return err; |
3213 | } | |
3214 | ||
1a94e38d PNA |
3215 | static struct nft_rule *nft_rule_lookup_byid(const struct net *net, |
3216 | const struct nlattr *nla) | |
3217 | { | |
3218 | u32 id = ntohl(nla_get_be32(nla)); | |
3219 | struct nft_trans *trans; | |
3220 | ||
3221 | list_for_each_entry(trans, &net->nft.commit_list, list) { | |
3222 | struct nft_rule *rule = nft_trans_rule(trans); | |
3223 | ||
3224 | if (trans->msg_type == NFT_MSG_NEWRULE && | |
3225 | id == nft_trans_rule_id(trans)) | |
3226 | return rule; | |
3227 | } | |
3228 | return ERR_PTR(-ENOENT); | |
3229 | } | |
3230 | ||
633c9a84 PNA |
3231 | static int nf_tables_delrule(struct net *net, struct sock *nlsk, |
3232 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
04ba724b PNA |
3233 | const struct nlattr * const nla[], |
3234 | struct netlink_ext_ack *extack) | |
96518518 PM |
3235 | { |
3236 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
f2a6d766 | 3237 | u8 genmask = nft_genmask_next(net); |
7c95f6d8 | 3238 | struct nft_table *table; |
cf9dc09d PNA |
3239 | struct nft_chain *chain = NULL; |
3240 | struct nft_rule *rule; | |
0628b123 PNA |
3241 | int family = nfmsg->nfgen_family, err = 0; |
3242 | struct nft_ctx ctx; | |
96518518 | 3243 | |
cac20fcd | 3244 | table = nft_table_lookup(net, nla[NFTA_RULE_TABLE], family, genmask); |
36dd1bcc PNA |
3245 | if (IS_ERR(table)) { |
3246 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_TABLE]); | |
96518518 | 3247 | return PTR_ERR(table); |
36dd1bcc | 3248 | } |
96518518 | 3249 | |
cf9dc09d | 3250 | if (nla[NFTA_RULE_CHAIN]) { |
f102d66b FW |
3251 | chain = nft_chain_lookup(net, table, nla[NFTA_RULE_CHAIN], |
3252 | genmask); | |
36dd1bcc PNA |
3253 | if (IS_ERR(chain)) { |
3254 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_CHAIN]); | |
cf9dc09d | 3255 | return PTR_ERR(chain); |
36dd1bcc | 3256 | } |
cf9dc09d | 3257 | } |
96518518 | 3258 | |
98319cb9 | 3259 | nft_ctx_init(&ctx, net, skb, nlh, family, table, chain, nla); |
0628b123 | 3260 | |
cf9dc09d PNA |
3261 | if (chain) { |
3262 | if (nla[NFTA_RULE_HANDLE]) { | |
cac20fcd | 3263 | rule = nft_rule_lookup(chain, nla[NFTA_RULE_HANDLE]); |
36dd1bcc PNA |
3264 | if (IS_ERR(rule)) { |
3265 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_HANDLE]); | |
cf9dc09d | 3266 | return PTR_ERR(rule); |
36dd1bcc | 3267 | } |
96518518 | 3268 | |
1a94e38d PNA |
3269 | err = nft_delrule(&ctx, rule); |
3270 | } else if (nla[NFTA_RULE_ID]) { | |
3271 | rule = nft_rule_lookup_byid(net, nla[NFTA_RULE_ID]); | |
36dd1bcc PNA |
3272 | if (IS_ERR(rule)) { |
3273 | NL_SET_BAD_ATTR(extack, nla[NFTA_RULE_ID]); | |
1a94e38d | 3274 | return PTR_ERR(rule); |
36dd1bcc | 3275 | } |
1a94e38d | 3276 | |
5e266fe7 | 3277 | err = nft_delrule(&ctx, rule); |
cf9dc09d | 3278 | } else { |
ce24b721 | 3279 | err = nft_delrule_by_chain(&ctx); |
cf9dc09d PNA |
3280 | } |
3281 | } else { | |
3282 | list_for_each_entry(chain, &table->chains, list) { | |
664b0f8c PNA |
3283 | if (!nft_is_active_next(net, chain)) |
3284 | continue; | |
3285 | ||
cf9dc09d | 3286 | ctx.chain = chain; |
ce24b721 | 3287 | err = nft_delrule_by_chain(&ctx); |
0628b123 PNA |
3288 | if (err < 0) |
3289 | break; | |
3290 | } | |
3291 | } | |
3292 | ||
3293 | return err; | |
3294 | } | |
3295 | ||
20a69341 PM |
3296 | /* |
3297 | * Sets | |
3298 | */ | |
e32a4dc6 FW |
3299 | static const struct nft_set_type *nft_set_types[] = { |
3300 | &nft_set_hash_fast_type, | |
3301 | &nft_set_hash_type, | |
3302 | &nft_set_rhash_type, | |
3303 | &nft_set_bitmap_type, | |
3304 | &nft_set_rbtree_type, | |
e6abef61 | 3305 | #if defined(CONFIG_X86_64) && !defined(CONFIG_UML) |
7400b063 SB |
3306 | &nft_set_pipapo_avx2_type, |
3307 | #endif | |
e32a4dc6 FW |
3308 | &nft_set_pipapo_type, |
3309 | }; | |
20a69341 | 3310 | |
2b664957 | 3311 | #define NFT_SET_FEATURES (NFT_SET_INTERVAL | NFT_SET_MAP | \ |
71cc0873 PS |
3312 | NFT_SET_TIMEOUT | NFT_SET_OBJECT | \ |
3313 | NFT_SET_EVAL) | |
2b664957 | 3314 | |
71cc0873 | 3315 | static bool nft_set_ops_candidate(const struct nft_set_type *type, u32 flags) |
2b664957 | 3316 | { |
71cc0873 | 3317 | return (flags & type->features) == (flags & NFT_SET_FEATURES); |
2b664957 PNA |
3318 | } |
3319 | ||
c50b960c PM |
3320 | /* |
3321 | * Select a set implementation based on the data characteristics and the | |
3322 | * given policy. The total memory use might not be known if no size is | |
3323 | * given, in that case the amount of memory per element is used. | |
3324 | */ | |
3325 | static const struct nft_set_ops * | |
2b664957 PNA |
3326 | nft_select_set_ops(const struct nft_ctx *ctx, |
3327 | const struct nlattr * const nla[], | |
c50b960c PM |
3328 | const struct nft_set_desc *desc, |
3329 | enum nft_set_policies policy) | |
20a69341 | 3330 | { |
c50b960c PM |
3331 | const struct nft_set_ops *ops, *bops; |
3332 | struct nft_set_estimate est, best; | |
2b664957 PNA |
3333 | const struct nft_set_type *type; |
3334 | u32 flags = 0; | |
e32a4dc6 | 3335 | int i; |
20a69341 | 3336 | |
f102d66b FW |
3337 | lockdep_assert_held(&ctx->net->nft.commit_mutex); |
3338 | lockdep_nfnl_nft_mutex_not_held(); | |
e32a4dc6 | 3339 | |
2b664957 PNA |
3340 | if (nla[NFTA_SET_FLAGS] != NULL) |
3341 | flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS])); | |
20a69341 | 3342 | |
55af753c PNA |
3343 | bops = NULL; |
3344 | best.size = ~0; | |
3345 | best.lookup = ~0; | |
0b5a7874 | 3346 | best.space = ~0; |
c50b960c | 3347 | |
e32a4dc6 FW |
3348 | for (i = 0; i < ARRAY_SIZE(nft_set_types); i++) { |
3349 | type = nft_set_types[i]; | |
71cc0873 | 3350 | ops = &type->ops; |
2b664957 | 3351 | |
71cc0873 | 3352 | if (!nft_set_ops_candidate(type, flags)) |
20a69341 | 3353 | continue; |
2b664957 | 3354 | if (!ops->estimate(desc, flags, &est)) |
c50b960c PM |
3355 | continue; |
3356 | ||
3357 | switch (policy) { | |
3358 | case NFT_SET_POL_PERFORMANCE: | |
55af753c | 3359 | if (est.lookup < best.lookup) |
c50b960c | 3360 | break; |
644e334e PNA |
3361 | if (est.lookup == best.lookup && |
3362 | est.space < best.space) | |
3363 | break; | |
c50b960c PM |
3364 | continue; |
3365 | case NFT_SET_POL_MEMORY: | |
0b5a7874 PNA |
3366 | if (!desc->size) { |
3367 | if (est.space < best.space) | |
3368 | break; | |
3369 | if (est.space == best.space && | |
3370 | est.lookup < best.lookup) | |
3371 | break; | |
4f2921ca | 3372 | } else if (est.size < best.size || !bops) { |
c50b960c | 3373 | break; |
0b5a7874 | 3374 | } |
c50b960c PM |
3375 | continue; |
3376 | default: | |
3377 | break; | |
3378 | } | |
3379 | ||
c50b960c PM |
3380 | bops = ops; |
3381 | best = est; | |
20a69341 PM |
3382 | } |
3383 | ||
c50b960c PM |
3384 | if (bops != NULL) |
3385 | return bops; | |
3386 | ||
20a69341 PM |
3387 | return ERR_PTR(-EOPNOTSUPP); |
3388 | } | |
3389 | ||
3390 | static const struct nla_policy nft_set_policy[NFTA_SET_MAX + 1] = { | |
b2fbd044 LZ |
3391 | [NFTA_SET_TABLE] = { .type = NLA_STRING, |
3392 | .len = NFT_TABLE_MAXNAMELEN - 1 }, | |
a9bdd836 | 3393 | [NFTA_SET_NAME] = { .type = NLA_STRING, |
cb39ad8b | 3394 | .len = NFT_SET_MAXNAMELEN - 1 }, |
20a69341 PM |
3395 | [NFTA_SET_FLAGS] = { .type = NLA_U32 }, |
3396 | [NFTA_SET_KEY_TYPE] = { .type = NLA_U32 }, | |
3397 | [NFTA_SET_KEY_LEN] = { .type = NLA_U32 }, | |
3398 | [NFTA_SET_DATA_TYPE] = { .type = NLA_U32 }, | |
3399 | [NFTA_SET_DATA_LEN] = { .type = NLA_U32 }, | |
c50b960c PM |
3400 | [NFTA_SET_POLICY] = { .type = NLA_U32 }, |
3401 | [NFTA_SET_DESC] = { .type = NLA_NESTED }, | |
958bee14 | 3402 | [NFTA_SET_ID] = { .type = NLA_U32 }, |
761da293 PM |
3403 | [NFTA_SET_TIMEOUT] = { .type = NLA_U64 }, |
3404 | [NFTA_SET_GC_INTERVAL] = { .type = NLA_U32 }, | |
e6d8ecac CFG |
3405 | [NFTA_SET_USERDATA] = { .type = NLA_BINARY, |
3406 | .len = NFT_USERDATA_MAXLEN }, | |
8aeff920 | 3407 | [NFTA_SET_OBJ_TYPE] = { .type = NLA_U32 }, |
3ecbfd65 | 3408 | [NFTA_SET_HANDLE] = { .type = NLA_U64 }, |
65038428 | 3409 | [NFTA_SET_EXPR] = { .type = NLA_NESTED }, |
c50b960c PM |
3410 | }; |
3411 | ||
3412 | static const struct nla_policy nft_set_desc_policy[NFTA_SET_DESC_MAX + 1] = { | |
3413 | [NFTA_SET_DESC_SIZE] = { .type = NLA_U32 }, | |
f3a2181e | 3414 | [NFTA_SET_DESC_CONCAT] = { .type = NLA_NESTED }, |
20a69341 PM |
3415 | }; |
3416 | ||
633c9a84 | 3417 | static int nft_ctx_init_from_setattr(struct nft_ctx *ctx, struct net *net, |
20a69341 PM |
3418 | const struct sk_buff *skb, |
3419 | const struct nlmsghdr *nlh, | |
f2a6d766 | 3420 | const struct nlattr * const nla[], |
36dd1bcc | 3421 | struct netlink_ext_ack *extack, |
f2a6d766 | 3422 | u8 genmask) |
20a69341 PM |
3423 | { |
3424 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
98319cb9 | 3425 | int family = nfmsg->nfgen_family; |
7c95f6d8 | 3426 | struct nft_table *table = NULL; |
20a69341 | 3427 | |
20a69341 | 3428 | if (nla[NFTA_SET_TABLE] != NULL) { |
cac20fcd PNA |
3429 | table = nft_table_lookup(net, nla[NFTA_SET_TABLE], family, |
3430 | genmask); | |
36dd1bcc PNA |
3431 | if (IS_ERR(table)) { |
3432 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_TABLE]); | |
20a69341 | 3433 | return PTR_ERR(table); |
36dd1bcc | 3434 | } |
20a69341 PM |
3435 | } |
3436 | ||
98319cb9 | 3437 | nft_ctx_init(ctx, net, skb, nlh, family, table, NULL, nla); |
20a69341 PM |
3438 | return 0; |
3439 | } | |
3440 | ||
cac20fcd PNA |
3441 | static struct nft_set *nft_set_lookup(const struct nft_table *table, |
3442 | const struct nlattr *nla, u8 genmask) | |
20a69341 PM |
3443 | { |
3444 | struct nft_set *set; | |
3445 | ||
3446 | if (nla == NULL) | |
3447 | return ERR_PTR(-EINVAL); | |
3448 | ||
d9adf22a | 3449 | list_for_each_entry_rcu(set, &table->sets, list) { |
37a9cc52 PNA |
3450 | if (!nla_strcmp(nla, set->name) && |
3451 | nft_active_genmask(set, genmask)) | |
20a69341 PM |
3452 | return set; |
3453 | } | |
3454 | return ERR_PTR(-ENOENT); | |
3455 | } | |
3456 | ||
cac20fcd PNA |
3457 | static struct nft_set *nft_set_lookup_byhandle(const struct nft_table *table, |
3458 | const struct nlattr *nla, | |
3459 | u8 genmask) | |
3ecbfd65 HS |
3460 | { |
3461 | struct nft_set *set; | |
3462 | ||
3ecbfd65 HS |
3463 | list_for_each_entry(set, &table->sets, list) { |
3464 | if (be64_to_cpu(nla_get_be64(nla)) == set->handle && | |
3465 | nft_active_genmask(set, genmask)) | |
3466 | return set; | |
3467 | } | |
3468 | return ERR_PTR(-ENOENT); | |
3469 | } | |
3470 | ||
cac20fcd PNA |
3471 | static struct nft_set *nft_set_lookup_byid(const struct net *net, |
3472 | const struct nlattr *nla, u8 genmask) | |
958bee14 PNA |
3473 | { |
3474 | struct nft_trans *trans; | |
3475 | u32 id = ntohl(nla_get_be32(nla)); | |
3476 | ||
3477 | list_for_each_entry(trans, &net->nft.commit_list, list) { | |
9c7f96fd AK |
3478 | if (trans->msg_type == NFT_MSG_NEWSET) { |
3479 | struct nft_set *set = nft_trans_set(trans); | |
37a9cc52 | 3480 | |
9c7f96fd AK |
3481 | if (id == nft_trans_set_id(trans) && |
3482 | nft_active_genmask(set, genmask)) | |
3483 | return set; | |
3484 | } | |
958bee14 PNA |
3485 | } |
3486 | return ERR_PTR(-ENOENT); | |
3487 | } | |
c7a72e3f | 3488 | |
10659cba PNA |
3489 | struct nft_set *nft_set_lookup_global(const struct net *net, |
3490 | const struct nft_table *table, | |
3491 | const struct nlattr *nla_set_name, | |
3492 | const struct nlattr *nla_set_id, | |
3493 | u8 genmask) | |
c7a72e3f PNA |
3494 | { |
3495 | struct nft_set *set; | |
3496 | ||
cac20fcd | 3497 | set = nft_set_lookup(table, nla_set_name, genmask); |
c7a72e3f PNA |
3498 | if (IS_ERR(set)) { |
3499 | if (!nla_set_id) | |
3500 | return set; | |
3501 | ||
cac20fcd | 3502 | set = nft_set_lookup_byid(net, nla_set_id, genmask); |
c7a72e3f PNA |
3503 | } |
3504 | return set; | |
3505 | } | |
10659cba | 3506 | EXPORT_SYMBOL_GPL(nft_set_lookup_global); |
958bee14 | 3507 | |
20a69341 PM |
3508 | static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set, |
3509 | const char *name) | |
3510 | { | |
3511 | const struct nft_set *i; | |
3512 | const char *p; | |
3513 | unsigned long *inuse; | |
60eb1894 | 3514 | unsigned int n = 0, min = 0; |
20a69341 | 3515 | |
38745490 | 3516 | p = strchr(name, '%'); |
20a69341 PM |
3517 | if (p != NULL) { |
3518 | if (p[1] != 'd' || strchr(p + 2, '%')) | |
3519 | return -EINVAL; | |
3520 | ||
3521 | inuse = (unsigned long *)get_zeroed_page(GFP_KERNEL); | |
3522 | if (inuse == NULL) | |
3523 | return -ENOMEM; | |
60eb1894 | 3524 | cont: |
20a69341 | 3525 | list_for_each_entry(i, &ctx->table->sets, list) { |
14662917 DB |
3526 | int tmp; |
3527 | ||
37a9cc52 PNA |
3528 | if (!nft_is_active_next(ctx->net, set)) |
3529 | continue; | |
14662917 | 3530 | if (!sscanf(i->name, name, &tmp)) |
20a69341 | 3531 | continue; |
60eb1894 | 3532 | if (tmp < min || tmp >= min + BITS_PER_BYTE * PAGE_SIZE) |
20a69341 | 3533 | continue; |
14662917 | 3534 | |
60eb1894 | 3535 | set_bit(tmp - min, inuse); |
20a69341 PM |
3536 | } |
3537 | ||
53b70287 | 3538 | n = find_first_zero_bit(inuse, BITS_PER_BYTE * PAGE_SIZE); |
60eb1894 PM |
3539 | if (n >= BITS_PER_BYTE * PAGE_SIZE) { |
3540 | min += BITS_PER_BYTE * PAGE_SIZE; | |
3541 | memset(inuse, 0, PAGE_SIZE); | |
3542 | goto cont; | |
3543 | } | |
20a69341 PM |
3544 | free_page((unsigned long)inuse); |
3545 | } | |
3546 | ||
38745490 PS |
3547 | set->name = kasprintf(GFP_KERNEL, name, min + n); |
3548 | if (!set->name) | |
3549 | return -ENOMEM; | |
3550 | ||
20a69341 | 3551 | list_for_each_entry(i, &ctx->table->sets, list) { |
37a9cc52 PNA |
3552 | if (!nft_is_active_next(ctx->net, i)) |
3553 | continue; | |
e63aaaa6 AY |
3554 | if (!strcmp(set->name, i->name)) { |
3555 | kfree(set->name); | |
7fb6f78d | 3556 | set->name = NULL; |
20a69341 | 3557 | return -ENFILE; |
e63aaaa6 | 3558 | } |
20a69341 PM |
3559 | } |
3560 | return 0; | |
3561 | } | |
3562 | ||
8e1102d5 FW |
3563 | static int nf_msecs_to_jiffies64(const struct nlattr *nla, u64 *result) |
3564 | { | |
3565 | u64 ms = be64_to_cpu(nla_get_be64(nla)); | |
3566 | u64 max = (u64)(~((u64)0)); | |
3567 | ||
3568 | max = div_u64(max, NSEC_PER_MSEC); | |
3569 | if (ms >= max) | |
3570 | return -ERANGE; | |
3571 | ||
3572 | ms *= NSEC_PER_MSEC; | |
3573 | *result = nsecs_to_jiffies64(ms); | |
3574 | return 0; | |
3575 | } | |
3576 | ||
d6501de8 | 3577 | static __be64 nf_jiffies64_to_msecs(u64 input) |
8e1102d5 | 3578 | { |
3b15d09f | 3579 | return cpu_to_be64(jiffies64_to_msecs(input)); |
8e1102d5 FW |
3580 | } |
3581 | ||
f3a2181e SB |
3582 | static int nf_tables_fill_set_concat(struct sk_buff *skb, |
3583 | const struct nft_set *set) | |
3584 | { | |
3585 | struct nlattr *concat, *field; | |
3586 | int i; | |
3587 | ||
3588 | concat = nla_nest_start_noflag(skb, NFTA_SET_DESC_CONCAT); | |
3589 | if (!concat) | |
3590 | return -ENOMEM; | |
3591 | ||
3592 | for (i = 0; i < set->field_count; i++) { | |
3593 | field = nla_nest_start_noflag(skb, NFTA_LIST_ELEM); | |
3594 | if (!field) | |
3595 | return -ENOMEM; | |
3596 | ||
3597 | if (nla_put_be32(skb, NFTA_SET_FIELD_LEN, | |
3598 | htonl(set->field_len[i]))) | |
3599 | return -ENOMEM; | |
3600 | ||
3601 | nla_nest_end(skb, field); | |
3602 | } | |
3603 | ||
3604 | nla_nest_end(skb, concat); | |
3605 | ||
3606 | return 0; | |
3607 | } | |
3608 | ||
20a69341 PM |
3609 | static int nf_tables_fill_set(struct sk_buff *skb, const struct nft_ctx *ctx, |
3610 | const struct nft_set *set, u16 event, u16 flags) | |
3611 | { | |
3612 | struct nfgenmsg *nfmsg; | |
3613 | struct nlmsghdr *nlh; | |
128ad332 | 3614 | u32 portid = ctx->portid; |
65038428 | 3615 | struct nlattr *nest; |
128ad332 | 3616 | u32 seq = ctx->seq; |
20a69341 | 3617 | |
dedb67c4 | 3618 | event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); |
20a69341 PM |
3619 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), |
3620 | flags); | |
3621 | if (nlh == NULL) | |
3622 | goto nla_put_failure; | |
3623 | ||
3624 | nfmsg = nlmsg_data(nlh); | |
36596dad | 3625 | nfmsg->nfgen_family = ctx->family; |
20a69341 | 3626 | nfmsg->version = NFNETLINK_V0; |
84d7fce6 | 3627 | nfmsg->res_id = htons(ctx->net->nft.base_seq & 0xffff); |
20a69341 PM |
3628 | |
3629 | if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name)) | |
3630 | goto nla_put_failure; | |
3631 | if (nla_put_string(skb, NFTA_SET_NAME, set->name)) | |
3632 | goto nla_put_failure; | |
3ecbfd65 HS |
3633 | if (nla_put_be64(skb, NFTA_SET_HANDLE, cpu_to_be64(set->handle), |
3634 | NFTA_SET_PAD)) | |
3635 | goto nla_put_failure; | |
20a69341 PM |
3636 | if (set->flags != 0) |
3637 | if (nla_put_be32(skb, NFTA_SET_FLAGS, htonl(set->flags))) | |
3638 | goto nla_put_failure; | |
3639 | ||
3640 | if (nla_put_be32(skb, NFTA_SET_KEY_TYPE, htonl(set->ktype))) | |
3641 | goto nla_put_failure; | |
3642 | if (nla_put_be32(skb, NFTA_SET_KEY_LEN, htonl(set->klen))) | |
3643 | goto nla_put_failure; | |
3644 | if (set->flags & NFT_SET_MAP) { | |
3645 | if (nla_put_be32(skb, NFTA_SET_DATA_TYPE, htonl(set->dtype))) | |
3646 | goto nla_put_failure; | |
3647 | if (nla_put_be32(skb, NFTA_SET_DATA_LEN, htonl(set->dlen))) | |
3648 | goto nla_put_failure; | |
3649 | } | |
8aeff920 PNA |
3650 | if (set->flags & NFT_SET_OBJECT && |
3651 | nla_put_be32(skb, NFTA_SET_OBJ_TYPE, htonl(set->objtype))) | |
3652 | goto nla_put_failure; | |
20a69341 | 3653 | |
761da293 | 3654 | if (set->timeout && |
d3e2a111 | 3655 | nla_put_be64(skb, NFTA_SET_TIMEOUT, |
8e1102d5 | 3656 | nf_jiffies64_to_msecs(set->timeout), |
b46f6ded | 3657 | NFTA_SET_PAD)) |
761da293 PM |
3658 | goto nla_put_failure; |
3659 | if (set->gc_int && | |
3660 | nla_put_be32(skb, NFTA_SET_GC_INTERVAL, htonl(set->gc_int))) | |
3661 | goto nla_put_failure; | |
3662 | ||
9363dc4b AB |
3663 | if (set->policy != NFT_SET_POL_PERFORMANCE) { |
3664 | if (nla_put_be32(skb, NFTA_SET_POLICY, htonl(set->policy))) | |
3665 | goto nla_put_failure; | |
3666 | } | |
3667 | ||
e6d8ecac CFG |
3668 | if (nla_put(skb, NFTA_SET_USERDATA, set->udlen, set->udata)) |
3669 | goto nla_put_failure; | |
3670 | ||
65038428 PNA |
3671 | nest = nla_nest_start_noflag(skb, NFTA_SET_DESC); |
3672 | if (!nest) | |
c50b960c PM |
3673 | goto nla_put_failure; |
3674 | if (set->size && | |
3675 | nla_put_be32(skb, NFTA_SET_DESC_SIZE, htonl(set->size))) | |
3676 | goto nla_put_failure; | |
f3a2181e SB |
3677 | |
3678 | if (set->field_count > 1 && | |
3679 | nf_tables_fill_set_concat(skb, set)) | |
3680 | goto nla_put_failure; | |
3681 | ||
65038428 PNA |
3682 | nla_nest_end(skb, nest); |
3683 | ||
3684 | if (set->expr) { | |
3685 | nest = nla_nest_start_noflag(skb, NFTA_SET_EXPR); | |
3686 | if (nf_tables_fill_expr_info(skb, set->expr) < 0) | |
3687 | goto nla_put_failure; | |
3688 | ||
3689 | nla_nest_end(skb, nest); | |
3690 | } | |
c50b960c | 3691 | |
053c095a JB |
3692 | nlmsg_end(skb, nlh); |
3693 | return 0; | |
20a69341 PM |
3694 | |
3695 | nla_put_failure: | |
3696 | nlmsg_trim(skb, nlh); | |
3697 | return -1; | |
3698 | } | |
3699 | ||
25e94a99 PNA |
3700 | static void nf_tables_set_notify(const struct nft_ctx *ctx, |
3701 | const struct nft_set *set, int event, | |
3702 | gfp_t gfp_flags) | |
20a69341 PM |
3703 | { |
3704 | struct sk_buff *skb; | |
128ad332 | 3705 | u32 portid = ctx->portid; |
20a69341 PM |
3706 | int err; |
3707 | ||
128ad332 PNA |
3708 | if (!ctx->report && |
3709 | !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES)) | |
25e94a99 | 3710 | return; |
20a69341 | 3711 | |
31f8441c | 3712 | skb = nlmsg_new(NLMSG_GOODSIZE, gfp_flags); |
20a69341 PM |
3713 | if (skb == NULL) |
3714 | goto err; | |
3715 | ||
3716 | err = nf_tables_fill_set(skb, ctx, set, event, 0); | |
3717 | if (err < 0) { | |
3718 | kfree_skb(skb); | |
3719 | goto err; | |
3720 | } | |
3721 | ||
25e94a99 PNA |
3722 | nfnetlink_send(skb, ctx->net, portid, NFNLGRP_NFTABLES, ctx->report, |
3723 | gfp_flags); | |
3724 | return; | |
20a69341 | 3725 | err: |
25e94a99 | 3726 | nfnetlink_set_err(ctx->net, portid, NFNLGRP_NFTABLES, -ENOBUFS); |
20a69341 PM |
3727 | } |
3728 | ||
5b96af77 | 3729 | static int nf_tables_dump_sets(struct sk_buff *skb, struct netlink_callback *cb) |
c9c8e485 PNA |
3730 | { |
3731 | const struct nft_set *set; | |
3732 | unsigned int idx, s_idx = cb->args[0]; | |
c9c8e485 PNA |
3733 | struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2]; |
3734 | struct net *net = sock_net(skb->sk); | |
5b96af77 | 3735 | struct nft_ctx *ctx = cb->data, ctx_set; |
c9c8e485 PNA |
3736 | |
3737 | if (cb->args[1]) | |
3738 | return skb->len; | |
3739 | ||
e688a7f8 | 3740 | rcu_read_lock(); |
38e029f1 PNA |
3741 | cb->seq = net->nft.base_seq; |
3742 | ||
36596dad PNA |
3743 | list_for_each_entry_rcu(table, &net->nft.tables, list) { |
3744 | if (ctx->family != NFPROTO_UNSPEC && | |
98319cb9 | 3745 | ctx->family != table->family) |
36596dad PNA |
3746 | continue; |
3747 | ||
3748 | if (ctx->table && ctx->table != table) | |
5b96af77 PNA |
3749 | continue; |
3750 | ||
36596dad PNA |
3751 | if (cur_table) { |
3752 | if (cur_table != table) | |
c9c8e485 PNA |
3753 | continue; |
3754 | ||
36596dad | 3755 | cur_table = NULL; |
c9c8e485 | 3756 | } |
36596dad PNA |
3757 | idx = 0; |
3758 | list_for_each_entry_rcu(set, &table->sets, list) { | |
3759 | if (idx < s_idx) | |
3760 | goto cont; | |
3761 | if (!nft_is_active(net, set)) | |
3762 | goto cont; | |
5b96af77 | 3763 | |
36596dad PNA |
3764 | ctx_set = *ctx; |
3765 | ctx_set.table = table; | |
98319cb9 | 3766 | ctx_set.family = table->family; |
c9c8e485 | 3767 | |
36596dad PNA |
3768 | if (nf_tables_fill_set(skb, &ctx_set, set, |
3769 | NFT_MSG_NEWSET, | |
3770 | NLM_F_MULTI) < 0) { | |
3771 | cb->args[0] = idx; | |
3772 | cb->args[2] = (unsigned long) table; | |
3773 | goto done; | |
c9c8e485 | 3774 | } |
36596dad | 3775 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); |
c9c8e485 | 3776 | cont: |
36596dad | 3777 | idx++; |
c9c8e485 | 3778 | } |
36596dad PNA |
3779 | if (s_idx) |
3780 | s_idx = 0; | |
c9c8e485 PNA |
3781 | } |
3782 | cb->args[1] = 1; | |
3783 | done: | |
e688a7f8 | 3784 | rcu_read_unlock(); |
c9c8e485 PNA |
3785 | return skb->len; |
3786 | } | |
3787 | ||
90fd131a FW |
3788 | static int nf_tables_dump_sets_start(struct netlink_callback *cb) |
3789 | { | |
3790 | struct nft_ctx *ctx_dump = NULL; | |
3791 | ||
3792 | ctx_dump = kmemdup(cb->data, sizeof(*ctx_dump), GFP_ATOMIC); | |
3793 | if (ctx_dump == NULL) | |
3794 | return -ENOMEM; | |
3795 | ||
3796 | cb->data = ctx_dump; | |
3797 | return 0; | |
3798 | } | |
3799 | ||
5b96af77 | 3800 | static int nf_tables_dump_sets_done(struct netlink_callback *cb) |
20a69341 | 3801 | { |
5b96af77 PNA |
3802 | kfree(cb->data); |
3803 | return 0; | |
20a69341 PM |
3804 | } |
3805 | ||
d9adf22a | 3806 | /* called with rcu_read_lock held */ |
7b8002a1 PNA |
3807 | static int nf_tables_getset(struct net *net, struct sock *nlsk, |
3808 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
04ba724b PNA |
3809 | const struct nlattr * const nla[], |
3810 | struct netlink_ext_ack *extack) | |
20a69341 | 3811 | { |
f2a6d766 | 3812 | u8 genmask = nft_genmask_cur(net); |
20a69341 PM |
3813 | const struct nft_set *set; |
3814 | struct nft_ctx ctx; | |
3815 | struct sk_buff *skb2; | |
c9c8e485 | 3816 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
20a69341 PM |
3817 | int err; |
3818 | ||
01cfa0a4 | 3819 | /* Verify existence before starting dump */ |
36dd1bcc PNA |
3820 | err = nft_ctx_init_from_setattr(&ctx, net, skb, nlh, nla, extack, |
3821 | genmask); | |
20a69341 PM |
3822 | if (err < 0) |
3823 | return err; | |
3824 | ||
3825 | if (nlh->nlmsg_flags & NLM_F_DUMP) { | |
3826 | struct netlink_dump_control c = { | |
90fd131a | 3827 | .start = nf_tables_dump_sets_start, |
20a69341 | 3828 | .dump = nf_tables_dump_sets, |
5b96af77 | 3829 | .done = nf_tables_dump_sets_done, |
90fd131a | 3830 | .data = &ctx, |
d9adf22a | 3831 | .module = THIS_MODULE, |
20a69341 | 3832 | }; |
5b96af77 | 3833 | |
d9adf22a | 3834 | return nft_netlink_dump_start_rcu(nlsk, skb, nlh, &c); |
20a69341 PM |
3835 | } |
3836 | ||
c9c8e485 PNA |
3837 | /* Only accept unspec with dump */ |
3838 | if (nfmsg->nfgen_family == NFPROTO_UNSPEC) | |
3839 | return -EAFNOSUPPORT; | |
eaa2bcd6 PT |
3840 | if (!nla[NFTA_SET_TABLE]) |
3841 | return -EINVAL; | |
c9c8e485 | 3842 | |
cac20fcd | 3843 | set = nft_set_lookup(ctx.table, nla[NFTA_SET_NAME], genmask); |
20a69341 PM |
3844 | if (IS_ERR(set)) |
3845 | return PTR_ERR(set); | |
3846 | ||
d9adf22a | 3847 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
20a69341 PM |
3848 | if (skb2 == NULL) |
3849 | return -ENOMEM; | |
3850 | ||
3851 | err = nf_tables_fill_set(skb2, &ctx, set, NFT_MSG_NEWSET, 0); | |
3852 | if (err < 0) | |
3853 | goto err; | |
3854 | ||
3855 | return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid); | |
3856 | ||
3857 | err: | |
3858 | kfree_skb(skb2); | |
3859 | return err; | |
3860 | } | |
3861 | ||
f3a2181e SB |
3862 | static const struct nla_policy nft_concat_policy[NFTA_SET_FIELD_MAX + 1] = { |
3863 | [NFTA_SET_FIELD_LEN] = { .type = NLA_U32 }, | |
3864 | }; | |
3865 | ||
3866 | static int nft_set_desc_concat_parse(const struct nlattr *attr, | |
3867 | struct nft_set_desc *desc) | |
3868 | { | |
3869 | struct nlattr *tb[NFTA_SET_FIELD_MAX + 1]; | |
3870 | u32 len; | |
3871 | int err; | |
3872 | ||
3873 | err = nla_parse_nested_deprecated(tb, NFTA_SET_FIELD_MAX, attr, | |
3874 | nft_concat_policy, NULL); | |
3875 | if (err < 0) | |
3876 | return err; | |
3877 | ||
3878 | if (!tb[NFTA_SET_FIELD_LEN]) | |
3879 | return -EINVAL; | |
3880 | ||
3881 | len = ntohl(nla_get_be32(tb[NFTA_SET_FIELD_LEN])); | |
3882 | ||
3883 | if (len * BITS_PER_BYTE / 32 > NFT_REG32_COUNT) | |
3884 | return -E2BIG; | |
3885 | ||
3886 | desc->field_len[desc->field_count++] = len; | |
3887 | ||
3888 | return 0; | |
3889 | } | |
3890 | ||
3891 | static int nft_set_desc_concat(struct nft_set_desc *desc, | |
3892 | const struct nlattr *nla) | |
3893 | { | |
3894 | struct nlattr *attr; | |
3895 | int rem, err; | |
3896 | ||
3897 | nla_for_each_nested(attr, nla, rem) { | |
3898 | if (nla_type(attr) != NFTA_LIST_ELEM) | |
3899 | return -EINVAL; | |
3900 | ||
3901 | err = nft_set_desc_concat_parse(attr, desc); | |
3902 | if (err < 0) | |
3903 | return err; | |
3904 | } | |
3905 | ||
3906 | return 0; | |
3907 | } | |
3908 | ||
f7e840ee | 3909 | static int nf_tables_set_desc_parse(struct nft_set_desc *desc, |
c50b960c PM |
3910 | const struct nlattr *nla) |
3911 | { | |
3912 | struct nlattr *da[NFTA_SET_DESC_MAX + 1]; | |
3913 | int err; | |
3914 | ||
8cb08174 JB |
3915 | err = nla_parse_nested_deprecated(da, NFTA_SET_DESC_MAX, nla, |
3916 | nft_set_desc_policy, NULL); | |
c50b960c PM |
3917 | if (err < 0) |
3918 | return err; | |
3919 | ||
3920 | if (da[NFTA_SET_DESC_SIZE] != NULL) | |
3921 | desc->size = ntohl(nla_get_be32(da[NFTA_SET_DESC_SIZE])); | |
f3a2181e SB |
3922 | if (da[NFTA_SET_DESC_CONCAT]) |
3923 | err = nft_set_desc_concat(desc, da[NFTA_SET_DESC_CONCAT]); | |
c50b960c | 3924 | |
f3a2181e | 3925 | return err; |
c50b960c PM |
3926 | } |
3927 | ||
633c9a84 PNA |
3928 | static int nf_tables_newset(struct net *net, struct sock *nlsk, |
3929 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
04ba724b PNA |
3930 | const struct nlattr * const nla[], |
3931 | struct netlink_ext_ack *extack) | |
20a69341 PM |
3932 | { |
3933 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
f2a6d766 | 3934 | u8 genmask = nft_genmask_next(net); |
98319cb9 | 3935 | int family = nfmsg->nfgen_family; |
20a69341 | 3936 | const struct nft_set_ops *ops; |
65038428 | 3937 | struct nft_expr *expr = NULL; |
20a69341 PM |
3938 | struct nft_table *table; |
3939 | struct nft_set *set; | |
3940 | struct nft_ctx ctx; | |
38745490 | 3941 | char *name; |
4ef360dd | 3942 | u64 size; |
761da293 | 3943 | u64 timeout; |
8aeff920 | 3944 | u32 ktype, dtype, flags, policy, gc_int, objtype; |
c50b960c | 3945 | struct nft_set_desc desc; |
e6d8ecac CFG |
3946 | unsigned char *udata; |
3947 | u16 udlen; | |
20a69341 | 3948 | int err; |
f3a2181e | 3949 | int i; |
20a69341 PM |
3950 | |
3951 | if (nla[NFTA_SET_TABLE] == NULL || | |
3952 | nla[NFTA_SET_NAME] == NULL || | |
958bee14 PNA |
3953 | nla[NFTA_SET_KEY_LEN] == NULL || |
3954 | nla[NFTA_SET_ID] == NULL) | |
20a69341 PM |
3955 | return -EINVAL; |
3956 | ||
c50b960c PM |
3957 | memset(&desc, 0, sizeof(desc)); |
3958 | ||
20a69341 PM |
3959 | ktype = NFT_DATA_VALUE; |
3960 | if (nla[NFTA_SET_KEY_TYPE] != NULL) { | |
3961 | ktype = ntohl(nla_get_be32(nla[NFTA_SET_KEY_TYPE])); | |
3962 | if ((ktype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK) | |
3963 | return -EINVAL; | |
3964 | } | |
3965 | ||
c50b960c | 3966 | desc.klen = ntohl(nla_get_be32(nla[NFTA_SET_KEY_LEN])); |
7d740264 | 3967 | if (desc.klen == 0 || desc.klen > NFT_DATA_VALUE_MAXLEN) |
20a69341 PM |
3968 | return -EINVAL; |
3969 | ||
3970 | flags = 0; | |
3971 | if (nla[NFTA_SET_FLAGS] != NULL) { | |
3972 | flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS])); | |
3973 | if (flags & ~(NFT_SET_ANONYMOUS | NFT_SET_CONSTANT | | |
7c6c6e95 | 3974 | NFT_SET_INTERVAL | NFT_SET_TIMEOUT | |
8aeff920 | 3975 | NFT_SET_MAP | NFT_SET_EVAL | |
ef516e86 | 3976 | NFT_SET_OBJECT | NFT_SET_CONCAT)) |
d9583cdf | 3977 | return -EOPNOTSUPP; |
8aeff920 | 3978 | /* Only one of these operations is supported */ |
acab7131 FW |
3979 | if ((flags & (NFT_SET_MAP | NFT_SET_OBJECT)) == |
3980 | (NFT_SET_MAP | NFT_SET_OBJECT)) | |
3981 | return -EOPNOTSUPP; | |
3982 | if ((flags & (NFT_SET_EVAL | NFT_SET_OBJECT)) == | |
3983 | (NFT_SET_EVAL | NFT_SET_OBJECT)) | |
7c6c6e95 | 3984 | return -EOPNOTSUPP; |
20a69341 PM |
3985 | } |
3986 | ||
3987 | dtype = 0; | |
20a69341 PM |
3988 | if (nla[NFTA_SET_DATA_TYPE] != NULL) { |
3989 | if (!(flags & NFT_SET_MAP)) | |
3990 | return -EINVAL; | |
3991 | ||
3992 | dtype = ntohl(nla_get_be32(nla[NFTA_SET_DATA_TYPE])); | |
3993 | if ((dtype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK && | |
3994 | dtype != NFT_DATA_VERDICT) | |
3995 | return -EINVAL; | |
3996 | ||
3997 | if (dtype != NFT_DATA_VERDICT) { | |
3998 | if (nla[NFTA_SET_DATA_LEN] == NULL) | |
3999 | return -EINVAL; | |
c50b960c | 4000 | desc.dlen = ntohl(nla_get_be32(nla[NFTA_SET_DATA_LEN])); |
7d740264 | 4001 | if (desc.dlen == 0 || desc.dlen > NFT_DATA_VALUE_MAXLEN) |
20a69341 PM |
4002 | return -EINVAL; |
4003 | } else | |
7d740264 | 4004 | desc.dlen = sizeof(struct nft_verdict); |
20a69341 PM |
4005 | } else if (flags & NFT_SET_MAP) |
4006 | return -EINVAL; | |
4007 | ||
8aeff920 PNA |
4008 | if (nla[NFTA_SET_OBJ_TYPE] != NULL) { |
4009 | if (!(flags & NFT_SET_OBJECT)) | |
4010 | return -EINVAL; | |
4011 | ||
4012 | objtype = ntohl(nla_get_be32(nla[NFTA_SET_OBJ_TYPE])); | |
4013 | if (objtype == NFT_OBJECT_UNSPEC || | |
4014 | objtype > NFT_OBJECT_MAX) | |
d9583cdf | 4015 | return -EOPNOTSUPP; |
8aeff920 PNA |
4016 | } else if (flags & NFT_SET_OBJECT) |
4017 | return -EINVAL; | |
4018 | else | |
4019 | objtype = NFT_OBJECT_UNSPEC; | |
4020 | ||
761da293 PM |
4021 | timeout = 0; |
4022 | if (nla[NFTA_SET_TIMEOUT] != NULL) { | |
4023 | if (!(flags & NFT_SET_TIMEOUT)) | |
4024 | return -EINVAL; | |
8e1102d5 FW |
4025 | |
4026 | err = nf_msecs_to_jiffies64(nla[NFTA_SET_TIMEOUT], &timeout); | |
4027 | if (err) | |
4028 | return err; | |
761da293 PM |
4029 | } |
4030 | gc_int = 0; | |
4031 | if (nla[NFTA_SET_GC_INTERVAL] != NULL) { | |
4032 | if (!(flags & NFT_SET_TIMEOUT)) | |
4033 | return -EINVAL; | |
4034 | gc_int = ntohl(nla_get_be32(nla[NFTA_SET_GC_INTERVAL])); | |
4035 | } | |
4036 | ||
c50b960c PM |
4037 | policy = NFT_SET_POL_PERFORMANCE; |
4038 | if (nla[NFTA_SET_POLICY] != NULL) | |
4039 | policy = ntohl(nla_get_be32(nla[NFTA_SET_POLICY])); | |
4040 | ||
4041 | if (nla[NFTA_SET_DESC] != NULL) { | |
f7e840ee | 4042 | err = nf_tables_set_desc_parse(&desc, nla[NFTA_SET_DESC]); |
c50b960c PM |
4043 | if (err < 0) |
4044 | return err; | |
4045 | } | |
4046 | ||
d56aab26 PNA |
4047 | if (nla[NFTA_SET_EXPR]) |
4048 | desc.expr = true; | |
4049 | ||
cac20fcd | 4050 | table = nft_table_lookup(net, nla[NFTA_SET_TABLE], family, genmask); |
36dd1bcc PNA |
4051 | if (IS_ERR(table)) { |
4052 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_TABLE]); | |
20a69341 | 4053 | return PTR_ERR(table); |
36dd1bcc | 4054 | } |
20a69341 | 4055 | |
98319cb9 | 4056 | nft_ctx_init(&ctx, net, skb, nlh, family, table, NULL, nla); |
20a69341 | 4057 | |
cac20fcd | 4058 | set = nft_set_lookup(table, nla[NFTA_SET_NAME], genmask); |
20a69341 | 4059 | if (IS_ERR(set)) { |
36dd1bcc PNA |
4060 | if (PTR_ERR(set) != -ENOENT) { |
4061 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_NAME]); | |
20a69341 | 4062 | return PTR_ERR(set); |
36dd1bcc | 4063 | } |
1a28ad74 | 4064 | } else { |
36dd1bcc PNA |
4065 | if (nlh->nlmsg_flags & NLM_F_EXCL) { |
4066 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_NAME]); | |
20a69341 | 4067 | return -EEXIST; |
36dd1bcc | 4068 | } |
20a69341 PM |
4069 | if (nlh->nlmsg_flags & NLM_F_REPLACE) |
4070 | return -EOPNOTSUPP; | |
36dd1bcc | 4071 | |
20a69341 PM |
4072 | return 0; |
4073 | } | |
4074 | ||
4075 | if (!(nlh->nlmsg_flags & NLM_F_CREATE)) | |
4076 | return -ENOENT; | |
4077 | ||
2b664957 | 4078 | ops = nft_select_set_ops(&ctx, nla, &desc, policy); |
20a69341 PM |
4079 | if (IS_ERR(ops)) |
4080 | return PTR_ERR(ops); | |
4081 | ||
e6d8ecac CFG |
4082 | udlen = 0; |
4083 | if (nla[NFTA_SET_USERDATA]) | |
4084 | udlen = nla_len(nla[NFTA_SET_USERDATA]); | |
4085 | ||
20a69341 PM |
4086 | size = 0; |
4087 | if (ops->privsize != NULL) | |
347b408d | 4088 | size = ops->privsize(nla, &desc); |
20a69341 | 4089 | |
1ff75a3e | 4090 | set = kvzalloc(sizeof(*set) + size + udlen, GFP_KERNEL); |
24d19826 FW |
4091 | if (!set) |
4092 | return -ENOMEM; | |
20a69341 | 4093 | |
38745490 PS |
4094 | name = nla_strdup(nla[NFTA_SET_NAME], GFP_KERNEL); |
4095 | if (!name) { | |
4096 | err = -ENOMEM; | |
65038428 | 4097 | goto err_set_name; |
38745490 PS |
4098 | } |
4099 | ||
20a69341 | 4100 | err = nf_tables_set_alloc_name(&ctx, set, name); |
38745490 | 4101 | kfree(name); |
20a69341 | 4102 | if (err < 0) |
65038428 PNA |
4103 | goto err_set_alloc_name; |
4104 | ||
4105 | if (nla[NFTA_SET_EXPR]) { | |
4106 | expr = nft_set_elem_expr_alloc(&ctx, set, nla[NFTA_SET_EXPR]); | |
4107 | if (IS_ERR(expr)) { | |
4108 | err = PTR_ERR(expr); | |
4109 | goto err_set_alloc_name; | |
4110 | } | |
4111 | } | |
20a69341 | 4112 | |
e6d8ecac CFG |
4113 | udata = NULL; |
4114 | if (udlen) { | |
4115 | udata = set->data + size; | |
4116 | nla_memcpy(udata, nla[NFTA_SET_USERDATA], udlen); | |
4117 | } | |
4118 | ||
20a69341 | 4119 | INIT_LIST_HEAD(&set->bindings); |
3453c927 PNA |
4120 | set->table = table; |
4121 | write_pnet(&set->net, net); | |
20a69341 PM |
4122 | set->ops = ops; |
4123 | set->ktype = ktype; | |
c50b960c | 4124 | set->klen = desc.klen; |
20a69341 | 4125 | set->dtype = dtype; |
8aeff920 | 4126 | set->objtype = objtype; |
c50b960c | 4127 | set->dlen = desc.dlen; |
65038428 | 4128 | set->expr = expr; |
20a69341 | 4129 | set->flags = flags; |
c50b960c | 4130 | set->size = desc.size; |
9363dc4b | 4131 | set->policy = policy; |
e6d8ecac CFG |
4132 | set->udlen = udlen; |
4133 | set->udata = udata; | |
761da293 PM |
4134 | set->timeout = timeout; |
4135 | set->gc_int = gc_int; | |
3ecbfd65 | 4136 | set->handle = nf_tables_alloc_handle(table); |
20a69341 | 4137 | |
f3a2181e SB |
4138 | set->field_count = desc.field_count; |
4139 | for (i = 0; i < desc.field_count; i++) | |
4140 | set->field_len[i] = desc.field_len[i]; | |
4141 | ||
c50b960c | 4142 | err = ops->init(set, &desc, nla); |
20a69341 | 4143 | if (err < 0) |
65038428 | 4144 | goto err_set_init; |
20a69341 | 4145 | |
958bee14 | 4146 | err = nft_trans_set_add(&ctx, NFT_MSG_NEWSET, set); |
20a69341 | 4147 | if (err < 0) |
65038428 | 4148 | goto err_set_trans; |
20a69341 | 4149 | |
e688a7f8 | 4150 | list_add_tail_rcu(&set->list, &table->sets); |
4fefee57 | 4151 | table->use++; |
20a69341 PM |
4152 | return 0; |
4153 | ||
65038428 | 4154 | err_set_trans: |
c17c3cdf | 4155 | ops->destroy(set); |
65038428 PNA |
4156 | err_set_init: |
4157 | if (expr) | |
4158 | nft_expr_destroy(&ctx, expr); | |
4159 | err_set_alloc_name: | |
2f6adf48 | 4160 | kfree(set->name); |
65038428 | 4161 | err_set_name: |
1ff75a3e | 4162 | kvfree(set); |
20a69341 PM |
4163 | return err; |
4164 | } | |
4165 | ||
0c2a85ed | 4166 | static void nft_set_destroy(const struct nft_ctx *ctx, struct nft_set *set) |
20a69341 | 4167 | { |
273fe3f1 PNA |
4168 | if (WARN_ON(set->use > 0)) |
4169 | return; | |
4170 | ||
65038428 PNA |
4171 | if (set->expr) |
4172 | nft_expr_destroy(ctx, set->expr); | |
4173 | ||
20a69341 | 4174 | set->ops->destroy(set); |
38745490 | 4175 | kfree(set->name); |
1ff75a3e | 4176 | kvfree(set); |
20a69341 PM |
4177 | } |
4178 | ||
633c9a84 PNA |
4179 | static int nf_tables_delset(struct net *net, struct sock *nlsk, |
4180 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
04ba724b PNA |
4181 | const struct nlattr * const nla[], |
4182 | struct netlink_ext_ack *extack) | |
20a69341 | 4183 | { |
c9c8e485 | 4184 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); |
f2a6d766 | 4185 | u8 genmask = nft_genmask_next(net); |
36dd1bcc | 4186 | const struct nlattr *attr; |
20a69341 PM |
4187 | struct nft_set *set; |
4188 | struct nft_ctx ctx; | |
4189 | int err; | |
4190 | ||
ec2c9935 PM |
4191 | if (nfmsg->nfgen_family == NFPROTO_UNSPEC) |
4192 | return -EAFNOSUPPORT; | |
20a69341 PM |
4193 | if (nla[NFTA_SET_TABLE] == NULL) |
4194 | return -EINVAL; | |
4195 | ||
36dd1bcc PNA |
4196 | err = nft_ctx_init_from_setattr(&ctx, net, skb, nlh, nla, extack, |
4197 | genmask); | |
20a69341 PM |
4198 | if (err < 0) |
4199 | return err; | |
4200 | ||
36dd1bcc PNA |
4201 | if (nla[NFTA_SET_HANDLE]) { |
4202 | attr = nla[NFTA_SET_HANDLE]; | |
4203 | set = nft_set_lookup_byhandle(ctx.table, attr, genmask); | |
4204 | } else { | |
4205 | attr = nla[NFTA_SET_NAME]; | |
4206 | set = nft_set_lookup(ctx.table, attr, genmask); | |
4207 | } | |
a8278400 | 4208 | |
36dd1bcc PNA |
4209 | if (IS_ERR(set)) { |
4210 | NL_SET_BAD_ATTR(extack, attr); | |
4211 | return PTR_ERR(set); | |
4212 | } | |
273fe3f1 | 4213 | if (set->use || |
36dd1bcc PNA |
4214 | (nlh->nlmsg_flags & NLM_F_NONREC && atomic_read(&set->nelems) > 0)) { |
4215 | NL_SET_BAD_ATTR(extack, attr); | |
20a69341 | 4216 | return -EBUSY; |
36dd1bcc | 4217 | } |
20a69341 | 4218 | |
ee01d542 | 4219 | return nft_delset(&ctx, set); |
20a69341 PM |
4220 | } |
4221 | ||
4222 | static int nf_tables_bind_check_setelem(const struct nft_ctx *ctx, | |
de70185d | 4223 | struct nft_set *set, |
20a69341 | 4224 | const struct nft_set_iter *iter, |
de70185d | 4225 | struct nft_set_elem *elem) |
20a69341 | 4226 | { |
fe2811eb | 4227 | const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); |
20a69341 PM |
4228 | enum nft_registers dreg; |
4229 | ||
4230 | dreg = nft_type_to_reg(set->dtype); | |
1ec10212 PM |
4231 | return nft_validate_register_store(ctx, dreg, nft_set_ext_data(ext), |
4232 | set->dtype == NFT_DATA_VERDICT ? | |
4233 | NFT_DATA_VERDICT : NFT_DATA_VALUE, | |
4234 | set->dlen); | |
20a69341 PM |
4235 | } |
4236 | ||
4237 | int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set, | |
4238 | struct nft_set_binding *binding) | |
4239 | { | |
4240 | struct nft_set_binding *i; | |
4241 | struct nft_set_iter iter; | |
4242 | ||
273fe3f1 PNA |
4243 | if (set->use == UINT_MAX) |
4244 | return -EOVERFLOW; | |
4245 | ||
408070d6 | 4246 | if (!list_empty(&set->bindings) && nft_set_is_anonymous(set)) |
20a69341 PM |
4247 | return -EBUSY; |
4248 | ||
11113e19 | 4249 | if (binding->flags & NFT_SET_MAP) { |
20a69341 PM |
4250 | /* If the set is already bound to the same chain all |
4251 | * jumps are already validated for that chain. | |
4252 | */ | |
4253 | list_for_each_entry(i, &set->bindings, list) { | |
a4684402 | 4254 | if (i->flags & NFT_SET_MAP && |
11113e19 | 4255 | i->chain == binding->chain) |
20a69341 PM |
4256 | goto bind; |
4257 | } | |
4258 | ||
8588ac09 | 4259 | iter.genmask = nft_genmask_next(ctx->net); |
20a69341 PM |
4260 | iter.skip = 0; |
4261 | iter.count = 0; | |
4262 | iter.err = 0; | |
4263 | iter.fn = nf_tables_bind_check_setelem; | |
4264 | ||
4265 | set->ops->walk(ctx, set, &iter); | |
a02f4248 | 4266 | if (iter.err < 0) |
20a69341 | 4267 | return iter.err; |
20a69341 PM |
4268 | } |
4269 | bind: | |
4270 | binding->chain = ctx->chain; | |
e688a7f8 | 4271 | list_add_tail_rcu(&binding->list, &set->bindings); |
f6ac8585 | 4272 | nft_set_trans_bind(ctx, set); |
273fe3f1 | 4273 | set->use++; |
f6ac8585 | 4274 | |
20a69341 PM |
4275 | return 0; |
4276 | } | |
63aea290 | 4277 | EXPORT_SYMBOL_GPL(nf_tables_bind_set); |
20a69341 | 4278 | |
3b0a081d FW |
4279 | static void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set, |
4280 | struct nft_set_binding *binding, bool event) | |
20a69341 | 4281 | { |
e688a7f8 | 4282 | list_del_rcu(&binding->list); |
20a69341 | 4283 | |
f6ac8585 | 4284 | if (list_empty(&set->bindings) && nft_set_is_anonymous(set)) { |
cd5125d8 | 4285 | list_del_rcu(&set->list); |
f6ac8585 PNA |
4286 | if (event) |
4287 | nf_tables_set_notify(ctx, set, NFT_MSG_DELSET, | |
4288 | GFP_KERNEL); | |
4289 | } | |
20a69341 PM |
4290 | } |
4291 | ||
273fe3f1 PNA |
4292 | void nf_tables_deactivate_set(const struct nft_ctx *ctx, struct nft_set *set, |
4293 | struct nft_set_binding *binding, | |
4294 | enum nft_trans_phase phase) | |
4295 | { | |
4296 | switch (phase) { | |
4297 | case NFT_TRANS_PREPARE: | |
4298 | set->use--; | |
4299 | return; | |
4300 | case NFT_TRANS_ABORT: | |
4301 | case NFT_TRANS_RELEASE: | |
4302 | set->use--; | |
4303 | /* fall through */ | |
4304 | default: | |
4305 | nf_tables_unbind_set(ctx, set, binding, | |
4306 | phase == NFT_TRANS_COMMIT); | |
4307 | } | |
4308 | } | |
4309 | EXPORT_SYMBOL_GPL(nf_tables_deactivate_set); | |
4310 | ||
cd5125d8 FW |
4311 | void nf_tables_destroy_set(const struct nft_ctx *ctx, struct nft_set *set) |
4312 | { | |
f6ac8585 | 4313 | if (list_empty(&set->bindings) && nft_set_is_anonymous(set)) |
0c2a85ed | 4314 | nft_set_destroy(ctx, set); |
cd5125d8 FW |
4315 | } |
4316 | EXPORT_SYMBOL_GPL(nf_tables_destroy_set); | |
4317 | ||
3ac4c07a PM |
4318 | const struct nft_set_ext_type nft_set_ext_types[] = { |
4319 | [NFT_SET_EXT_KEY] = { | |
7d740264 | 4320 | .align = __alignof__(u32), |
3ac4c07a PM |
4321 | }, |
4322 | [NFT_SET_EXT_DATA] = { | |
7d740264 | 4323 | .align = __alignof__(u32), |
3ac4c07a | 4324 | }, |
f25ad2e9 PM |
4325 | [NFT_SET_EXT_EXPR] = { |
4326 | .align = __alignof__(struct nft_expr), | |
4327 | }, | |
8aeff920 PNA |
4328 | [NFT_SET_EXT_OBJREF] = { |
4329 | .len = sizeof(struct nft_object *), | |
4330 | .align = __alignof__(struct nft_object *), | |
4331 | }, | |
3ac4c07a PM |
4332 | [NFT_SET_EXT_FLAGS] = { |
4333 | .len = sizeof(u8), | |
4334 | .align = __alignof__(u8), | |
4335 | }, | |
c3e1b005 PM |
4336 | [NFT_SET_EXT_TIMEOUT] = { |
4337 | .len = sizeof(u64), | |
4338 | .align = __alignof__(u64), | |
4339 | }, | |
4340 | [NFT_SET_EXT_EXPIRATION] = { | |
8e1102d5 FW |
4341 | .len = sizeof(u64), |
4342 | .align = __alignof__(u64), | |
c3e1b005 | 4343 | }, |
68e942e8 PM |
4344 | [NFT_SET_EXT_USERDATA] = { |
4345 | .len = sizeof(struct nft_userdata), | |
4346 | .align = __alignof__(struct nft_userdata), | |
4347 | }, | |
7b225d0b PNA |
4348 | [NFT_SET_EXT_KEY_END] = { |
4349 | .align = __alignof__(u32), | |
4350 | }, | |
3ac4c07a | 4351 | }; |
3ac4c07a | 4352 | |
20a69341 PM |
4353 | /* |
4354 | * Set elements | |
4355 | */ | |
4356 | ||
4357 | static const struct nla_policy nft_set_elem_policy[NFTA_SET_ELEM_MAX + 1] = { | |
4358 | [NFTA_SET_ELEM_KEY] = { .type = NLA_NESTED }, | |
4359 | [NFTA_SET_ELEM_DATA] = { .type = NLA_NESTED }, | |
4360 | [NFTA_SET_ELEM_FLAGS] = { .type = NLA_U32 }, | |
c3e1b005 | 4361 | [NFTA_SET_ELEM_TIMEOUT] = { .type = NLA_U64 }, |
79ebb5bb | 4362 | [NFTA_SET_ELEM_EXPIRATION] = { .type = NLA_U64 }, |
68e942e8 PM |
4363 | [NFTA_SET_ELEM_USERDATA] = { .type = NLA_BINARY, |
4364 | .len = NFT_USERDATA_MAXLEN }, | |
467697d2 | 4365 | [NFTA_SET_ELEM_EXPR] = { .type = NLA_NESTED }, |
9332d27d FW |
4366 | [NFTA_SET_ELEM_OBJREF] = { .type = NLA_STRING, |
4367 | .len = NFT_OBJ_MAXNAMELEN - 1 }, | |
7b225d0b | 4368 | [NFTA_SET_ELEM_KEY_END] = { .type = NLA_NESTED }, |
20a69341 PM |
4369 | }; |
4370 | ||
4371 | static const struct nla_policy nft_set_elem_list_policy[NFTA_SET_ELEM_LIST_MAX + 1] = { | |
b2fbd044 LZ |
4372 | [NFTA_SET_ELEM_LIST_TABLE] = { .type = NLA_STRING, |
4373 | .len = NFT_TABLE_MAXNAMELEN - 1 }, | |
4374 | [NFTA_SET_ELEM_LIST_SET] = { .type = NLA_STRING, | |
4375 | .len = NFT_SET_MAXNAMELEN - 1 }, | |
20a69341 | 4376 | [NFTA_SET_ELEM_LIST_ELEMENTS] = { .type = NLA_NESTED }, |
958bee14 | 4377 | [NFTA_SET_ELEM_LIST_SET_ID] = { .type = NLA_U32 }, |
20a69341 PM |
4378 | }; |
4379 | ||
633c9a84 | 4380 | static int nft_ctx_init_from_elemattr(struct nft_ctx *ctx, struct net *net, |
20a69341 PM |
4381 | const struct sk_buff *skb, |
4382 | const struct nlmsghdr *nlh, | |
f2a6d766 | 4383 | const struct nlattr * const nla[], |
36dd1bcc | 4384 | struct netlink_ext_ack *extack, |
f2a6d766 | 4385 | u8 genmask) |
20a69341 PM |
4386 | { |
4387 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
98319cb9 | 4388 | int family = nfmsg->nfgen_family; |
7c95f6d8 | 4389 | struct nft_table *table; |
20a69341 | 4390 | |
cac20fcd PNA |
4391 | table = nft_table_lookup(net, nla[NFTA_SET_ELEM_LIST_TABLE], family, |
4392 | genmask); | |
36dd1bcc PNA |
4393 | if (IS_ERR(table)) { |
4394 | NL_SET_BAD_ATTR(extack, nla[NFTA_SET_ELEM_LIST_TABLE]); | |
20a69341 | 4395 | return PTR_ERR(table); |
36dd1bcc | 4396 | } |
20a69341 | 4397 | |
98319cb9 | 4398 | nft_ctx_init(ctx, net, skb, nlh, family, table, NULL, nla); |
20a69341 PM |
4399 | return 0; |
4400 | } | |
4401 | ||
4402 | static int nf_tables_fill_setelem(struct sk_buff *skb, | |
4403 | const struct nft_set *set, | |
4404 | const struct nft_set_elem *elem) | |
4405 | { | |
fe2811eb | 4406 | const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); |
20a69341 PM |
4407 | unsigned char *b = skb_tail_pointer(skb); |
4408 | struct nlattr *nest; | |
4409 | ||
ae0be8de | 4410 | nest = nla_nest_start_noflag(skb, NFTA_LIST_ELEM); |
20a69341 PM |
4411 | if (nest == NULL) |
4412 | goto nla_put_failure; | |
4413 | ||
fe2811eb PM |
4414 | if (nft_data_dump(skb, NFTA_SET_ELEM_KEY, nft_set_ext_key(ext), |
4415 | NFT_DATA_VALUE, set->klen) < 0) | |
20a69341 PM |
4416 | goto nla_put_failure; |
4417 | ||
7b225d0b PNA |
4418 | if (nft_set_ext_exists(ext, NFT_SET_EXT_KEY_END) && |
4419 | nft_data_dump(skb, NFTA_SET_ELEM_KEY_END, nft_set_ext_key_end(ext), | |
4420 | NFT_DATA_VALUE, set->klen) < 0) | |
4421 | goto nla_put_failure; | |
4422 | ||
fe2811eb PM |
4423 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA) && |
4424 | nft_data_dump(skb, NFTA_SET_ELEM_DATA, nft_set_ext_data(ext), | |
20a69341 PM |
4425 | set->dtype == NFT_DATA_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE, |
4426 | set->dlen) < 0) | |
4427 | goto nla_put_failure; | |
4428 | ||
f25ad2e9 PM |
4429 | if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPR) && |
4430 | nft_expr_dump(skb, NFTA_SET_ELEM_EXPR, nft_set_ext_expr(ext)) < 0) | |
4431 | goto nla_put_failure; | |
4432 | ||
8aeff920 PNA |
4433 | if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF) && |
4434 | nla_put_string(skb, NFTA_SET_ELEM_OBJREF, | |
d152159b | 4435 | (*nft_set_ext_obj(ext))->key.name) < 0) |
8aeff920 PNA |
4436 | goto nla_put_failure; |
4437 | ||
fe2811eb PM |
4438 | if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) && |
4439 | nla_put_be32(skb, NFTA_SET_ELEM_FLAGS, | |
4440 | htonl(*nft_set_ext_flags(ext)))) | |
4441 | goto nla_put_failure; | |
20a69341 | 4442 | |
c3e1b005 PM |
4443 | if (nft_set_ext_exists(ext, NFT_SET_EXT_TIMEOUT) && |
4444 | nla_put_be64(skb, NFTA_SET_ELEM_TIMEOUT, | |
8e1102d5 | 4445 | nf_jiffies64_to_msecs(*nft_set_ext_timeout(ext)), |
b46f6ded | 4446 | NFTA_SET_ELEM_PAD)) |
c3e1b005 PM |
4447 | goto nla_put_failure; |
4448 | ||
4449 | if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION)) { | |
8e1102d5 | 4450 | u64 expires, now = get_jiffies_64(); |
c3e1b005 PM |
4451 | |
4452 | expires = *nft_set_ext_expiration(ext); | |
8e1102d5 | 4453 | if (time_before64(now, expires)) |
c3e1b005 PM |
4454 | expires -= now; |
4455 | else | |
4456 | expires = 0; | |
4457 | ||
4458 | if (nla_put_be64(skb, NFTA_SET_ELEM_EXPIRATION, | |
8e1102d5 | 4459 | nf_jiffies64_to_msecs(expires), |
b46f6ded | 4460 | NFTA_SET_ELEM_PAD)) |
c3e1b005 PM |
4461 | goto nla_put_failure; |
4462 | } | |
4463 | ||
68e942e8 PM |
4464 | if (nft_set_ext_exists(ext, NFT_SET_EXT_USERDATA)) { |
4465 | struct nft_userdata *udata; | |
4466 | ||
4467 | udata = nft_set_ext_userdata(ext); | |
4468 | if (nla_put(skb, NFTA_SET_ELEM_USERDATA, | |
4469 | udata->len + 1, udata->data)) | |
4470 | goto nla_put_failure; | |
4471 | } | |
4472 | ||
20a69341 PM |
4473 | nla_nest_end(skb, nest); |
4474 | return 0; | |
4475 | ||
4476 | nla_put_failure: | |
4477 | nlmsg_trim(skb, b); | |
4478 | return -EMSGSIZE; | |
4479 | } | |
4480 | ||
4481 | struct nft_set_dump_args { | |
4482 | const struct netlink_callback *cb; | |
4483 | struct nft_set_iter iter; | |
4484 | struct sk_buff *skb; | |
4485 | }; | |
4486 | ||
4487 | static int nf_tables_dump_setelem(const struct nft_ctx *ctx, | |
de70185d | 4488 | struct nft_set *set, |
20a69341 | 4489 | const struct nft_set_iter *iter, |
de70185d | 4490 | struct nft_set_elem *elem) |
20a69341 PM |
4491 | { |
4492 | struct nft_set_dump_args *args; | |
4493 | ||
4494 | args = container_of(iter, struct nft_set_dump_args, iter); | |
4495 | return nf_tables_fill_setelem(args->skb, set, elem); | |
4496 | } | |
4497 | ||
fa803605 LZ |
4498 | struct nft_set_dump_ctx { |
4499 | const struct nft_set *set; | |
4500 | struct nft_ctx ctx; | |
4501 | }; | |
4502 | ||
20a69341 PM |
4503 | static int nf_tables_dump_set(struct sk_buff *skb, struct netlink_callback *cb) |
4504 | { | |
fa803605 | 4505 | struct nft_set_dump_ctx *dump_ctx = cb->data; |
633c9a84 | 4506 | struct net *net = sock_net(skb->sk); |
fa803605 | 4507 | struct nft_table *table; |
de70185d | 4508 | struct nft_set *set; |
20a69341 | 4509 | struct nft_set_dump_args args; |
fa803605 | 4510 | bool set_found = false; |
20a69341 PM |
4511 | struct nfgenmsg *nfmsg; |
4512 | struct nlmsghdr *nlh; | |
4513 | struct nlattr *nest; | |
4514 | u32 portid, seq; | |
fa803605 | 4515 | int event; |
20a69341 | 4516 | |
fa803605 | 4517 | rcu_read_lock(); |
36596dad PNA |
4518 | list_for_each_entry_rcu(table, &net->nft.tables, list) { |
4519 | if (dump_ctx->ctx.family != NFPROTO_UNSPEC && | |
98319cb9 | 4520 | dump_ctx->ctx.family != table->family) |
fa803605 | 4521 | continue; |
20a69341 | 4522 | |
36596dad PNA |
4523 | if (table != dump_ctx->ctx.table) |
4524 | continue; | |
20a69341 | 4525 | |
36596dad PNA |
4526 | list_for_each_entry_rcu(set, &table->sets, list) { |
4527 | if (set == dump_ctx->set) { | |
4528 | set_found = true; | |
4529 | break; | |
fa803605 | 4530 | } |
fa803605 LZ |
4531 | } |
4532 | break; | |
4533 | } | |
4534 | ||
4535 | if (!set_found) { | |
4536 | rcu_read_unlock(); | |
4537 | return -ENOENT; | |
4538 | } | |
20a69341 | 4539 | |
dedb67c4 | 4540 | event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, NFT_MSG_NEWSETELEM); |
20a69341 PM |
4541 | portid = NETLINK_CB(cb->skb).portid; |
4542 | seq = cb->nlh->nlmsg_seq; | |
4543 | ||
4544 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), | |
4545 | NLM_F_MULTI); | |
4546 | if (nlh == NULL) | |
4547 | goto nla_put_failure; | |
4548 | ||
4549 | nfmsg = nlmsg_data(nlh); | |
98319cb9 | 4550 | nfmsg->nfgen_family = table->family; |
20a69341 | 4551 | nfmsg->version = NFNETLINK_V0; |
fa803605 | 4552 | nfmsg->res_id = htons(net->nft.base_seq & 0xffff); |
20a69341 | 4553 | |
fa803605 | 4554 | if (nla_put_string(skb, NFTA_SET_ELEM_LIST_TABLE, table->name)) |
20a69341 PM |
4555 | goto nla_put_failure; |
4556 | if (nla_put_string(skb, NFTA_SET_ELEM_LIST_SET, set->name)) | |
4557 | goto nla_put_failure; | |
4558 | ||
ae0be8de | 4559 | nest = nla_nest_start_noflag(skb, NFTA_SET_ELEM_LIST_ELEMENTS); |
20a69341 PM |
4560 | if (nest == NULL) |
4561 | goto nla_put_failure; | |
4562 | ||
8588ac09 PNA |
4563 | args.cb = cb; |
4564 | args.skb = skb; | |
fa803605 | 4565 | args.iter.genmask = nft_genmask_cur(net); |
8588ac09 PNA |
4566 | args.iter.skip = cb->args[0]; |
4567 | args.iter.count = 0; | |
4568 | args.iter.err = 0; | |
4569 | args.iter.fn = nf_tables_dump_setelem; | |
fa803605 LZ |
4570 | set->ops->walk(&dump_ctx->ctx, set, &args.iter); |
4571 | rcu_read_unlock(); | |
20a69341 PM |
4572 | |
4573 | nla_nest_end(skb, nest); | |
4574 | nlmsg_end(skb, nlh); | |
4575 | ||
4576 | if (args.iter.err && args.iter.err != -EMSGSIZE) | |
4577 | return args.iter.err; | |
4578 | if (args.iter.count == cb->args[0]) | |
4579 | return 0; | |
4580 | ||
4581 | cb->args[0] = args.iter.count; | |
4582 | return skb->len; | |
4583 | ||
4584 | nla_put_failure: | |
fa803605 | 4585 | rcu_read_unlock(); |
20a69341 PM |
4586 | return -ENOSPC; |
4587 | } | |
4588 | ||
90fd131a FW |
4589 | static int nf_tables_dump_set_start(struct netlink_callback *cb) |
4590 | { | |
4591 | struct nft_set_dump_ctx *dump_ctx = cb->data; | |
4592 | ||
4593 | cb->data = kmemdup(dump_ctx, sizeof(*dump_ctx), GFP_ATOMIC); | |
4594 | ||
4595 | return cb->data ? 0 : -ENOMEM; | |
4596 | } | |
4597 | ||
fa803605 LZ |
4598 | static int nf_tables_dump_set_done(struct netlink_callback *cb) |
4599 | { | |
4600 | kfree(cb->data); | |
4601 | return 0; | |
4602 | } | |
4603 | ||
d60ce62f AB |
4604 | static int nf_tables_fill_setelem_info(struct sk_buff *skb, |
4605 | const struct nft_ctx *ctx, u32 seq, | |
4606 | u32 portid, int event, u16 flags, | |
4607 | const struct nft_set *set, | |
4608 | const struct nft_set_elem *elem) | |
4609 | { | |
4610 | struct nfgenmsg *nfmsg; | |
4611 | struct nlmsghdr *nlh; | |
4612 | struct nlattr *nest; | |
4613 | int err; | |
4614 | ||
dedb67c4 | 4615 | event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); |
d60ce62f AB |
4616 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), |
4617 | flags); | |
4618 | if (nlh == NULL) | |
4619 | goto nla_put_failure; | |
4620 | ||
4621 | nfmsg = nlmsg_data(nlh); | |
36596dad | 4622 | nfmsg->nfgen_family = ctx->family; |
d60ce62f | 4623 | nfmsg->version = NFNETLINK_V0; |
84d7fce6 | 4624 | nfmsg->res_id = htons(ctx->net->nft.base_seq & 0xffff); |
d60ce62f AB |
4625 | |
4626 | if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name)) | |
4627 | goto nla_put_failure; | |
4628 | if (nla_put_string(skb, NFTA_SET_NAME, set->name)) | |
4629 | goto nla_put_failure; | |
4630 | ||
ae0be8de | 4631 | nest = nla_nest_start_noflag(skb, NFTA_SET_ELEM_LIST_ELEMENTS); |
d60ce62f AB |
4632 | if (nest == NULL) |
4633 | goto nla_put_failure; | |
4634 | ||
4635 | err = nf_tables_fill_setelem(skb, set, elem); | |
4636 | if (err < 0) | |
4637 | goto nla_put_failure; | |
4638 | ||
4639 | nla_nest_end(skb, nest); | |
4640 | ||
053c095a JB |
4641 | nlmsg_end(skb, nlh); |
4642 | return 0; | |
d60ce62f AB |
4643 | |
4644 | nla_put_failure: | |
4645 | nlmsg_trim(skb, nlh); | |
4646 | return -1; | |
4647 | } | |
4648 | ||
ba0e4d99 PNA |
4649 | static int nft_setelem_parse_flags(const struct nft_set *set, |
4650 | const struct nlattr *attr, u32 *flags) | |
4651 | { | |
4652 | if (attr == NULL) | |
4653 | return 0; | |
4654 | ||
4655 | *flags = ntohl(nla_get_be32(attr)); | |
4656 | if (*flags & ~NFT_SET_ELEM_INTERVAL_END) | |
4657 | return -EINVAL; | |
4658 | if (!(set->flags & NFT_SET_INTERVAL) && | |
4659 | *flags & NFT_SET_ELEM_INTERVAL_END) | |
4660 | return -EINVAL; | |
4661 | ||
4662 | return 0; | |
4663 | } | |
4664 | ||
20a1452c PNA |
4665 | static int nft_setelem_parse_key(struct nft_ctx *ctx, struct nft_set *set, |
4666 | struct nft_data *key, struct nlattr *attr) | |
4667 | { | |
4668 | struct nft_data_desc desc; | |
4669 | int err; | |
4670 | ||
4671 | err = nft_data_init(ctx, key, NFT_DATA_VALUE_MAXLEN, &desc, attr); | |
4672 | if (err < 0) | |
4673 | return err; | |
4674 | ||
4675 | if (desc.type != NFT_DATA_VALUE || desc.len != set->klen) { | |
4676 | nft_data_release(key, desc.type); | |
4677 | return -EINVAL; | |
4678 | } | |
4679 | ||
4680 | return 0; | |
4681 | } | |
4682 | ||
fdb9c405 PNA |
4683 | static int nft_setelem_parse_data(struct nft_ctx *ctx, struct nft_set *set, |
4684 | struct nft_data_desc *desc, | |
4685 | struct nft_data *data, | |
4686 | struct nlattr *attr) | |
4687 | { | |
4688 | int err; | |
4689 | ||
4690 | err = nft_data_init(ctx, data, NFT_DATA_VALUE_MAXLEN, desc, attr); | |
4691 | if (err < 0) | |
4692 | return err; | |
4693 | ||
4694 | if (desc->type != NFT_DATA_VERDICT && desc->len != set->dlen) { | |
4695 | nft_data_release(data, desc->type); | |
4696 | return -EINVAL; | |
4697 | } | |
4698 | ||
4699 | return 0; | |
4700 | } | |
4701 | ||
ba0e4d99 PNA |
4702 | static int nft_get_set_elem(struct nft_ctx *ctx, struct nft_set *set, |
4703 | const struct nlattr *attr) | |
4704 | { | |
4705 | struct nlattr *nla[NFTA_SET_ELEM_MAX + 1]; | |
ba0e4d99 PNA |
4706 | struct nft_set_elem elem; |
4707 | struct sk_buff *skb; | |
4708 | uint32_t flags = 0; | |
4709 | void *priv; | |
4710 | int err; | |
4711 | ||
8cb08174 JB |
4712 | err = nla_parse_nested_deprecated(nla, NFTA_SET_ELEM_MAX, attr, |
4713 | nft_set_elem_policy, NULL); | |
ba0e4d99 PNA |
4714 | if (err < 0) |
4715 | return err; | |
4716 | ||
4717 | if (!nla[NFTA_SET_ELEM_KEY]) | |
4718 | return -EINVAL; | |
4719 | ||
4720 | err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags); | |
4721 | if (err < 0) | |
4722 | return err; | |
4723 | ||
20a1452c PNA |
4724 | err = nft_setelem_parse_key(ctx, set, &elem.key.val, |
4725 | nla[NFTA_SET_ELEM_KEY]); | |
ba0e4d99 PNA |
4726 | if (err < 0) |
4727 | return err; | |
4728 | ||
7b225d0b PNA |
4729 | if (nla[NFTA_SET_ELEM_KEY_END]) { |
4730 | err = nft_setelem_parse_key(ctx, set, &elem.key_end.val, | |
4731 | nla[NFTA_SET_ELEM_KEY_END]); | |
4732 | if (err < 0) | |
4733 | return err; | |
4734 | } | |
4735 | ||
ba0e4d99 PNA |
4736 | priv = set->ops->get(ctx->net, set, &elem, flags); |
4737 | if (IS_ERR(priv)) | |
4738 | return PTR_ERR(priv); | |
4739 | ||
4740 | elem.priv = priv; | |
ba0e4d99 PNA |
4741 | |
4742 | err = -ENOMEM; | |
d9adf22a | 4743 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC); |
ba0e4d99 PNA |
4744 | if (skb == NULL) |
4745 | goto err1; | |
4746 | ||
4747 | err = nf_tables_fill_setelem_info(skb, ctx, ctx->seq, ctx->portid, | |
4748 | NFT_MSG_NEWSETELEM, 0, set, &elem); | |
4749 | if (err < 0) | |
4750 | goto err2; | |
4751 | ||
4752 | err = nfnetlink_unicast(skb, ctx->net, ctx->portid, MSG_DONTWAIT); | |
4753 | /* This avoids a loop in nfnetlink. */ | |
4754 | if (err < 0) | |
4755 | goto err1; | |
4756 | ||
4757 | return 0; | |
4758 | err2: | |
4759 | kfree_skb(skb); | |
4760 | err1: | |
4761 | /* this avoids a loop in nfnetlink. */ | |
4762 | return err == -EAGAIN ? -ENOBUFS : err; | |
4763 | } | |
4764 | ||
d9adf22a | 4765 | /* called with rcu_read_lock held */ |
ba0e4d99 PNA |
4766 | static int nf_tables_getsetelem(struct net *net, struct sock *nlsk, |
4767 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
4768 | const struct nlattr * const nla[], | |
4769 | struct netlink_ext_ack *extack) | |
4770 | { | |
4771 | u8 genmask = nft_genmask_cur(net); | |
4772 | struct nft_set *set; | |
4773 | struct nlattr *attr; | |
4774 | struct nft_ctx ctx; | |
4775 | int rem, err = 0; | |
4776 | ||
36dd1bcc PNA |
4777 | err = nft_ctx_init_from_elemattr(&ctx, net, skb, nlh, nla, extack, |
4778 | genmask); | |
ba0e4d99 PNA |
4779 | if (err < 0) |
4780 | return err; | |
4781 | ||
cac20fcd | 4782 | set = nft_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET], genmask); |
ba0e4d99 PNA |
4783 | if (IS_ERR(set)) |
4784 | return PTR_ERR(set); | |
4785 | ||
4786 | if (nlh->nlmsg_flags & NLM_F_DUMP) { | |
4787 | struct netlink_dump_control c = { | |
90fd131a | 4788 | .start = nf_tables_dump_set_start, |
ba0e4d99 PNA |
4789 | .dump = nf_tables_dump_set, |
4790 | .done = nf_tables_dump_set_done, | |
d9adf22a | 4791 | .module = THIS_MODULE, |
ba0e4d99 | 4792 | }; |
90fd131a FW |
4793 | struct nft_set_dump_ctx dump_ctx = { |
4794 | .set = set, | |
4795 | .ctx = ctx, | |
4796 | }; | |
ba0e4d99 | 4797 | |
90fd131a | 4798 | c.data = &dump_ctx; |
d9adf22a | 4799 | return nft_netlink_dump_start_rcu(nlsk, skb, nlh, &c); |
ba0e4d99 PNA |
4800 | } |
4801 | ||
4802 | if (!nla[NFTA_SET_ELEM_LIST_ELEMENTS]) | |
4803 | return -EINVAL; | |
4804 | ||
4805 | nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) { | |
4806 | err = nft_get_set_elem(&ctx, set, attr); | |
4807 | if (err < 0) | |
4808 | break; | |
4809 | } | |
4810 | ||
4811 | return err; | |
4812 | } | |
4813 | ||
25e94a99 PNA |
4814 | static void nf_tables_setelem_notify(const struct nft_ctx *ctx, |
4815 | const struct nft_set *set, | |
4816 | const struct nft_set_elem *elem, | |
4817 | int event, u16 flags) | |
d60ce62f | 4818 | { |
128ad332 PNA |
4819 | struct net *net = ctx->net; |
4820 | u32 portid = ctx->portid; | |
d60ce62f AB |
4821 | struct sk_buff *skb; |
4822 | int err; | |
4823 | ||
128ad332 | 4824 | if (!ctx->report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES)) |
25e94a99 | 4825 | return; |
d60ce62f | 4826 | |
d60ce62f AB |
4827 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
4828 | if (skb == NULL) | |
4829 | goto err; | |
4830 | ||
4831 | err = nf_tables_fill_setelem_info(skb, ctx, 0, portid, event, flags, | |
4832 | set, elem); | |
4833 | if (err < 0) { | |
4834 | kfree_skb(skb); | |
4835 | goto err; | |
4836 | } | |
4837 | ||
25e94a99 PNA |
4838 | nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, ctx->report, |
4839 | GFP_KERNEL); | |
4840 | return; | |
d60ce62f | 4841 | err: |
25e94a99 | 4842 | nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, -ENOBUFS); |
d60ce62f AB |
4843 | } |
4844 | ||
60319eb1 PNA |
4845 | static struct nft_trans *nft_trans_elem_alloc(struct nft_ctx *ctx, |
4846 | int msg_type, | |
4847 | struct nft_set *set) | |
4848 | { | |
4849 | struct nft_trans *trans; | |
4850 | ||
4851 | trans = nft_trans_alloc(ctx, msg_type, sizeof(struct nft_trans_elem)); | |
4852 | if (trans == NULL) | |
4853 | return NULL; | |
4854 | ||
4855 | nft_trans_elem_set(trans) = set; | |
4856 | return trans; | |
4857 | } | |
4858 | ||
a7fc9368 PNA |
4859 | struct nft_expr *nft_set_elem_expr_alloc(const struct nft_ctx *ctx, |
4860 | const struct nft_set *set, | |
4861 | const struct nlattr *attr) | |
4862 | { | |
4863 | struct nft_expr *expr; | |
4864 | int err; | |
4865 | ||
4866 | expr = nft_expr_init(ctx, attr); | |
4867 | if (IS_ERR(expr)) | |
4868 | return expr; | |
4869 | ||
4870 | err = -EOPNOTSUPP; | |
4871 | if (!(expr->ops->type->flags & NFT_EXPR_STATEFUL)) | |
4872 | goto err_set_elem_expr; | |
4873 | ||
4874 | if (expr->ops->type->flags & NFT_EXPR_GC) { | |
4875 | if (set->flags & NFT_SET_TIMEOUT) | |
4876 | goto err_set_elem_expr; | |
4877 | if (!set->ops->gc_init) | |
4878 | goto err_set_elem_expr; | |
4879 | set->ops->gc_init(set); | |
4880 | } | |
4881 | ||
4882 | return expr; | |
4883 | ||
4884 | err_set_elem_expr: | |
4885 | nft_expr_destroy(ctx, expr); | |
4886 | return ERR_PTR(err); | |
4887 | } | |
4888 | ||
22fe54d5 PM |
4889 | void *nft_set_elem_init(const struct nft_set *set, |
4890 | const struct nft_set_ext_tmpl *tmpl, | |
7b225d0b PNA |
4891 | const u32 *key, const u32 *key_end, |
4892 | const u32 *data, u64 timeout, u64 expiration, gfp_t gfp) | |
fe2811eb PM |
4893 | { |
4894 | struct nft_set_ext *ext; | |
4895 | void *elem; | |
4896 | ||
4897 | elem = kzalloc(set->ops->elemsize + tmpl->len, gfp); | |
4898 | if (elem == NULL) | |
4899 | return NULL; | |
4900 | ||
4901 | ext = nft_set_elem_ext(set, elem); | |
4902 | nft_set_ext_init(ext, tmpl); | |
4903 | ||
4904 | memcpy(nft_set_ext_key(ext), key, set->klen); | |
7b225d0b PNA |
4905 | if (nft_set_ext_exists(ext, NFT_SET_EXT_KEY_END)) |
4906 | memcpy(nft_set_ext_key_end(ext), key_end, set->klen); | |
fe2811eb PM |
4907 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA)) |
4908 | memcpy(nft_set_ext_data(ext), data, set->dlen); | |
79ebb5bb LGL |
4909 | if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION)) { |
4910 | *nft_set_ext_expiration(ext) = get_jiffies_64() + expiration; | |
4911 | if (expiration == 0) | |
4912 | *nft_set_ext_expiration(ext) += timeout; | |
4913 | } | |
c3e1b005 PM |
4914 | if (nft_set_ext_exists(ext, NFT_SET_EXT_TIMEOUT)) |
4915 | *nft_set_ext_timeout(ext) = timeout; | |
fe2811eb PM |
4916 | |
4917 | return elem; | |
4918 | } | |
4919 | ||
475beb9c PNA |
4920 | static void nft_set_elem_expr_destroy(const struct nft_ctx *ctx, |
4921 | struct nft_expr *expr) | |
4922 | { | |
4923 | if (expr->ops->destroy_clone) { | |
4924 | expr->ops->destroy_clone(ctx, expr); | |
4925 | module_put(expr->ops->type->owner); | |
4926 | } else { | |
4927 | nf_tables_expr_destroy(ctx, expr); | |
4928 | } | |
4929 | } | |
4930 | ||
61f9e292 LZ |
4931 | void nft_set_elem_destroy(const struct nft_set *set, void *elem, |
4932 | bool destroy_expr) | |
61edafbb PM |
4933 | { |
4934 | struct nft_set_ext *ext = nft_set_elem_ext(set, elem); | |
3453c927 PNA |
4935 | struct nft_ctx ctx = { |
4936 | .net = read_pnet(&set->net), | |
4937 | .family = set->table->family, | |
4938 | }; | |
61edafbb | 4939 | |
59105446 | 4940 | nft_data_release(nft_set_ext_key(ext), NFT_DATA_VALUE); |
61edafbb | 4941 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA)) |
59105446 | 4942 | nft_data_release(nft_set_ext_data(ext), set->dtype); |
475beb9c PNA |
4943 | if (destroy_expr && nft_set_ext_exists(ext, NFT_SET_EXT_EXPR)) |
4944 | nft_set_elem_expr_destroy(&ctx, nft_set_ext_expr(ext)); | |
371ebcbb | 4945 | |
8aeff920 PNA |
4946 | if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF)) |
4947 | (*nft_set_ext_obj(ext))->use--; | |
61edafbb PM |
4948 | kfree(elem); |
4949 | } | |
4950 | EXPORT_SYMBOL_GPL(nft_set_elem_destroy); | |
4951 | ||
59105446 PNA |
4952 | /* Only called from commit path, nft_set_elem_deactivate() already deals with |
4953 | * the refcounting from the preparation phase. | |
4954 | */ | |
3453c927 PNA |
4955 | static void nf_tables_set_elem_destroy(const struct nft_ctx *ctx, |
4956 | const struct nft_set *set, void *elem) | |
59105446 PNA |
4957 | { |
4958 | struct nft_set_ext *ext = nft_set_elem_ext(set, elem); | |
4959 | ||
4960 | if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPR)) | |
475beb9c PNA |
4961 | nft_set_elem_expr_destroy(ctx, nft_set_ext_expr(ext)); |
4962 | ||
59105446 PNA |
4963 | kfree(elem); |
4964 | } | |
4965 | ||
60319eb1 | 4966 | static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set, |
c016c7e4 | 4967 | const struct nlattr *attr, u32 nlmsg_flags) |
20a69341 PM |
4968 | { |
4969 | struct nlattr *nla[NFTA_SET_ELEM_MAX + 1]; | |
8aeff920 | 4970 | u8 genmask = nft_genmask_next(ctx->net); |
fe2811eb | 4971 | struct nft_set_ext_tmpl tmpl; |
c016c7e4 | 4972 | struct nft_set_ext *ext, *ext2; |
20a69341 PM |
4973 | struct nft_set_elem elem; |
4974 | struct nft_set_binding *binding; | |
8aeff920 | 4975 | struct nft_object *obj = NULL; |
40944452 | 4976 | struct nft_expr *expr = NULL; |
68e942e8 | 4977 | struct nft_userdata *udata; |
20a1452c | 4978 | struct nft_data_desc desc; |
20a69341 | 4979 | enum nft_registers dreg; |
60319eb1 | 4980 | struct nft_trans *trans; |
0e9091d6 | 4981 | u32 flags = 0; |
c3e1b005 | 4982 | u64 timeout; |
79ebb5bb | 4983 | u64 expiration; |
68e942e8 | 4984 | u8 ulen; |
20a69341 PM |
4985 | int err; |
4986 | ||
8cb08174 JB |
4987 | err = nla_parse_nested_deprecated(nla, NFTA_SET_ELEM_MAX, attr, |
4988 | nft_set_elem_policy, NULL); | |
20a69341 PM |
4989 | if (err < 0) |
4990 | return err; | |
4991 | ||
4992 | if (nla[NFTA_SET_ELEM_KEY] == NULL) | |
4993 | return -EINVAL; | |
4994 | ||
fe2811eb PM |
4995 | nft_set_ext_prepare(&tmpl); |
4996 | ||
0e9091d6 PNA |
4997 | err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags); |
4998 | if (err < 0) | |
4999 | return err; | |
5000 | if (flags != 0) | |
5001 | nft_set_ext_add(&tmpl, NFT_SET_EXT_FLAGS); | |
20a69341 PM |
5002 | |
5003 | if (set->flags & NFT_SET_MAP) { | |
5004 | if (nla[NFTA_SET_ELEM_DATA] == NULL && | |
fe2811eb | 5005 | !(flags & NFT_SET_ELEM_INTERVAL_END)) |
20a69341 PM |
5006 | return -EINVAL; |
5007 | } else { | |
5008 | if (nla[NFTA_SET_ELEM_DATA] != NULL) | |
5009 | return -EINVAL; | |
5010 | } | |
5011 | ||
bffc124b PNA |
5012 | if ((flags & NFT_SET_ELEM_INTERVAL_END) && |
5013 | (nla[NFTA_SET_ELEM_DATA] || | |
5014 | nla[NFTA_SET_ELEM_OBJREF] || | |
5015 | nla[NFTA_SET_ELEM_TIMEOUT] || | |
5016 | nla[NFTA_SET_ELEM_EXPIRATION] || | |
5017 | nla[NFTA_SET_ELEM_USERDATA] || | |
5018 | nla[NFTA_SET_ELEM_EXPR])) | |
5019 | return -EINVAL; | |
5020 | ||
c3e1b005 PM |
5021 | timeout = 0; |
5022 | if (nla[NFTA_SET_ELEM_TIMEOUT] != NULL) { | |
5023 | if (!(set->flags & NFT_SET_TIMEOUT)) | |
5024 | return -EINVAL; | |
8e1102d5 FW |
5025 | err = nf_msecs_to_jiffies64(nla[NFTA_SET_ELEM_TIMEOUT], |
5026 | &timeout); | |
5027 | if (err) | |
5028 | return err; | |
c3e1b005 PM |
5029 | } else if (set->flags & NFT_SET_TIMEOUT) { |
5030 | timeout = set->timeout; | |
5031 | } | |
5032 | ||
79ebb5bb LGL |
5033 | expiration = 0; |
5034 | if (nla[NFTA_SET_ELEM_EXPIRATION] != NULL) { | |
5035 | if (!(set->flags & NFT_SET_TIMEOUT)) | |
5036 | return -EINVAL; | |
5037 | err = nf_msecs_to_jiffies64(nla[NFTA_SET_ELEM_EXPIRATION], | |
5038 | &expiration); | |
5039 | if (err) | |
5040 | return err; | |
5041 | } | |
5042 | ||
40944452 PNA |
5043 | if (nla[NFTA_SET_ELEM_EXPR] != NULL) { |
5044 | expr = nft_set_elem_expr_alloc(ctx, set, | |
5045 | nla[NFTA_SET_ELEM_EXPR]); | |
5046 | if (IS_ERR(expr)) | |
5047 | return PTR_ERR(expr); | |
65038428 PNA |
5048 | |
5049 | err = -EOPNOTSUPP; | |
5050 | if (set->expr && set->expr->ops != expr->ops) | |
5051 | goto err_set_elem_expr; | |
5052 | } else if (set->expr) { | |
5053 | expr = kzalloc(set->expr->ops->size, GFP_KERNEL); | |
5054 | if (!expr) | |
5055 | return -ENOMEM; | |
5056 | ||
5057 | err = nft_expr_clone(expr, set->expr); | |
5058 | if (err < 0) | |
5059 | goto err_set_elem_expr; | |
40944452 PNA |
5060 | } |
5061 | ||
20a1452c PNA |
5062 | err = nft_setelem_parse_key(ctx, set, &elem.key.val, |
5063 | nla[NFTA_SET_ELEM_KEY]); | |
20a69341 | 5064 | if (err < 0) |
40944452 | 5065 | goto err_set_elem_expr; |
20a69341 | 5066 | |
20a1452c | 5067 | nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY, set->klen); |
7b225d0b PNA |
5068 | |
5069 | if (nla[NFTA_SET_ELEM_KEY_END]) { | |
5070 | err = nft_setelem_parse_key(ctx, set, &elem.key_end.val, | |
5071 | nla[NFTA_SET_ELEM_KEY_END]); | |
5072 | if (err < 0) | |
5073 | goto err_parse_key; | |
5074 | ||
5075 | nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY_END, set->klen); | |
5076 | } | |
5077 | ||
c3e1b005 PM |
5078 | if (timeout > 0) { |
5079 | nft_set_ext_add(&tmpl, NFT_SET_EXT_EXPIRATION); | |
5080 | if (timeout != set->timeout) | |
5081 | nft_set_ext_add(&tmpl, NFT_SET_EXT_TIMEOUT); | |
5082 | } | |
fe2811eb | 5083 | |
40944452 PNA |
5084 | if (expr) |
5085 | nft_set_ext_add_length(&tmpl, NFT_SET_EXT_EXPR, | |
5086 | expr->ops->size); | |
5087 | ||
8aeff920 PNA |
5088 | if (nla[NFTA_SET_ELEM_OBJREF] != NULL) { |
5089 | if (!(set->flags & NFT_SET_OBJECT)) { | |
5090 | err = -EINVAL; | |
7b225d0b | 5091 | goto err_parse_key_end; |
8aeff920 | 5092 | } |
4d44175a FW |
5093 | obj = nft_obj_lookup(ctx->net, ctx->table, |
5094 | nla[NFTA_SET_ELEM_OBJREF], | |
cac20fcd | 5095 | set->objtype, genmask); |
8aeff920 PNA |
5096 | if (IS_ERR(obj)) { |
5097 | err = PTR_ERR(obj); | |
7b225d0b | 5098 | goto err_parse_key_end; |
8aeff920 PNA |
5099 | } |
5100 | nft_set_ext_add(&tmpl, NFT_SET_EXT_OBJREF); | |
5101 | } | |
5102 | ||
20a69341 | 5103 | if (nla[NFTA_SET_ELEM_DATA] != NULL) { |
fdb9c405 PNA |
5104 | err = nft_setelem_parse_data(ctx, set, &desc, &elem.data.val, |
5105 | nla[NFTA_SET_ELEM_DATA]); | |
20a69341 | 5106 | if (err < 0) |
7b225d0b | 5107 | goto err_parse_key_end; |
20a69341 | 5108 | |
20a69341 PM |
5109 | dreg = nft_type_to_reg(set->dtype); |
5110 | list_for_each_entry(binding, &set->bindings, list) { | |
5111 | struct nft_ctx bind_ctx = { | |
58c78e10 | 5112 | .net = ctx->net, |
36596dad | 5113 | .family = ctx->family, |
20a69341 | 5114 | .table = ctx->table, |
7c95f6d8 | 5115 | .chain = (struct nft_chain *)binding->chain, |
20a69341 PM |
5116 | }; |
5117 | ||
11113e19 PM |
5118 | if (!(binding->flags & NFT_SET_MAP)) |
5119 | continue; | |
5120 | ||
1ec10212 | 5121 | err = nft_validate_register_store(&bind_ctx, dreg, |
fdb9c405 | 5122 | &elem.data.val, |
20a1452c | 5123 | desc.type, desc.len); |
20a69341 | 5124 | if (err < 0) |
7b225d0b | 5125 | goto err_parse_data; |
a654de8f | 5126 | |
20a1452c | 5127 | if (desc.type == NFT_DATA_VERDICT && |
fdb9c405 PNA |
5128 | (elem.data.val.verdict.code == NFT_GOTO || |
5129 | elem.data.val.verdict.code == NFT_JUMP)) | |
a654de8f PNA |
5130 | nft_validate_state_update(ctx->net, |
5131 | NFT_VALIDATE_NEED); | |
20a69341 | 5132 | } |
fe2811eb | 5133 | |
20a1452c | 5134 | nft_set_ext_add_length(&tmpl, NFT_SET_EXT_DATA, desc.len); |
20a69341 PM |
5135 | } |
5136 | ||
68e942e8 PM |
5137 | /* The full maximum length of userdata can exceed the maximum |
5138 | * offset value (U8_MAX) for following extensions, therefor it | |
5139 | * must be the last extension added. | |
5140 | */ | |
5141 | ulen = 0; | |
5142 | if (nla[NFTA_SET_ELEM_USERDATA] != NULL) { | |
5143 | ulen = nla_len(nla[NFTA_SET_ELEM_USERDATA]); | |
5144 | if (ulen > 0) | |
5145 | nft_set_ext_add_length(&tmpl, NFT_SET_EXT_USERDATA, | |
5146 | ulen); | |
5147 | } | |
5148 | ||
fe2811eb | 5149 | err = -ENOMEM; |
7b225d0b | 5150 | elem.priv = nft_set_elem_init(set, &tmpl, elem.key.val.data, |
fdb9c405 | 5151 | elem.key_end.val.data, elem.data.val.data, |
79ebb5bb | 5152 | timeout, expiration, GFP_KERNEL); |
fe2811eb | 5153 | if (elem.priv == NULL) |
7b225d0b | 5154 | goto err_parse_data; |
fe2811eb PM |
5155 | |
5156 | ext = nft_set_elem_ext(set, elem.priv); | |
5157 | if (flags) | |
5158 | *nft_set_ext_flags(ext) = flags; | |
68e942e8 PM |
5159 | if (ulen > 0) { |
5160 | udata = nft_set_ext_userdata(ext); | |
5161 | udata->len = ulen - 1; | |
5162 | nla_memcpy(&udata->data, nla[NFTA_SET_ELEM_USERDATA], ulen); | |
5163 | } | |
8aeff920 PNA |
5164 | if (obj) { |
5165 | *nft_set_ext_obj(ext) = obj; | |
5166 | obj->use++; | |
5167 | } | |
40944452 PNA |
5168 | if (expr) { |
5169 | memcpy(nft_set_ext_expr(ext), expr, expr->ops->size); | |
5170 | kfree(expr); | |
772f4e82 | 5171 | expr = NULL; |
40944452 | 5172 | } |
fe2811eb | 5173 | |
60319eb1 PNA |
5174 | trans = nft_trans_elem_alloc(ctx, NFT_MSG_NEWSETELEM, set); |
5175 | if (trans == NULL) | |
7b225d0b | 5176 | goto err_trans; |
60319eb1 | 5177 | |
69086658 | 5178 | ext->genmask = nft_genmask_cur(ctx->net) | NFT_SET_ELEM_BUSY_MASK; |
c016c7e4 PNA |
5179 | err = set->ops->insert(ctx->net, set, &elem, &ext2); |
5180 | if (err) { | |
5181 | if (err == -EEXIST) { | |
9744a6fc PNA |
5182 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA) ^ |
5183 | nft_set_ext_exists(ext2, NFT_SET_EXT_DATA) || | |
5184 | nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF) ^ | |
f0dfd7a2 CIK |
5185 | nft_set_ext_exists(ext2, NFT_SET_EXT_OBJREF)) { |
5186 | err = -EBUSY; | |
7b225d0b | 5187 | goto err_element_clash; |
f0dfd7a2 | 5188 | } |
8aeff920 PNA |
5189 | if ((nft_set_ext_exists(ext, NFT_SET_EXT_DATA) && |
5190 | nft_set_ext_exists(ext2, NFT_SET_EXT_DATA) && | |
5191 | memcmp(nft_set_ext_data(ext), | |
5192 | nft_set_ext_data(ext2), set->dlen) != 0) || | |
5193 | (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF) && | |
5194 | nft_set_ext_exists(ext2, NFT_SET_EXT_OBJREF) && | |
5195 | *nft_set_ext_obj(ext) != *nft_set_ext_obj(ext2))) | |
c016c7e4 PNA |
5196 | err = -EBUSY; |
5197 | else if (!(nlmsg_flags & NLM_F_EXCL)) | |
5198 | err = 0; | |
8c2d45b2 PNA |
5199 | } else if (err == -ENOTEMPTY) { |
5200 | /* ENOTEMPTY reports overlapping between this element | |
5201 | * and an existing one. | |
5202 | */ | |
5203 | err = -EEXIST; | |
c016c7e4 | 5204 | } |
7b225d0b | 5205 | goto err_element_clash; |
c016c7e4 | 5206 | } |
20a69341 | 5207 | |
35d0ac90 PNA |
5208 | if (set->size && |
5209 | !atomic_add_unless(&set->nelems, 1, set->size + set->ndeact)) { | |
5210 | err = -ENFILE; | |
7b225d0b | 5211 | goto err_set_full; |
35d0ac90 PNA |
5212 | } |
5213 | ||
60319eb1 | 5214 | nft_trans_elem(trans) = elem; |
46bbafce | 5215 | list_add_tail(&trans->list, &ctx->net->nft.commit_list); |
20a69341 PM |
5216 | return 0; |
5217 | ||
7b225d0b | 5218 | err_set_full: |
5cb82a38 | 5219 | set->ops->remove(ctx->net, set, &elem); |
7b225d0b | 5220 | err_element_clash: |
60319eb1 | 5221 | kfree(trans); |
7b225d0b | 5222 | err_trans: |
b91d9036 TY |
5223 | if (obj) |
5224 | obj->use--; | |
475beb9c PNA |
5225 | |
5226 | nf_tables_set_elem_destroy(ctx, set, elem.priv); | |
7b225d0b | 5227 | err_parse_data: |
20a69341 | 5228 | if (nla[NFTA_SET_ELEM_DATA] != NULL) |
fdb9c405 | 5229 | nft_data_release(&elem.data.val, desc.type); |
7b225d0b PNA |
5230 | err_parse_key_end: |
5231 | nft_data_release(&elem.key_end.val, NFT_DATA_VALUE); | |
5232 | err_parse_key: | |
20a1452c | 5233 | nft_data_release(&elem.key.val, NFT_DATA_VALUE); |
40944452 PNA |
5234 | err_set_elem_expr: |
5235 | if (expr != NULL) | |
5236 | nft_expr_destroy(ctx, expr); | |
7b225d0b | 5237 | |
20a69341 PM |
5238 | return err; |
5239 | } | |
5240 | ||
633c9a84 PNA |
5241 | static int nf_tables_newsetelem(struct net *net, struct sock *nlsk, |
5242 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
04ba724b PNA |
5243 | const struct nlattr * const nla[], |
5244 | struct netlink_ext_ack *extack) | |
20a69341 | 5245 | { |
f2a6d766 | 5246 | u8 genmask = nft_genmask_next(net); |
20a69341 PM |
5247 | const struct nlattr *attr; |
5248 | struct nft_set *set; | |
5249 | struct nft_ctx ctx; | |
a654de8f | 5250 | int rem, err; |
20a69341 | 5251 | |
7d5570ca PNA |
5252 | if (nla[NFTA_SET_ELEM_LIST_ELEMENTS] == NULL) |
5253 | return -EINVAL; | |
5254 | ||
36dd1bcc PNA |
5255 | err = nft_ctx_init_from_elemattr(&ctx, net, skb, nlh, nla, extack, |
5256 | genmask); | |
20a69341 PM |
5257 | if (err < 0) |
5258 | return err; | |
5259 | ||
a3073c17 PNA |
5260 | set = nft_set_lookup_global(net, ctx.table, nla[NFTA_SET_ELEM_LIST_SET], |
5261 | nla[NFTA_SET_ELEM_LIST_SET_ID], genmask); | |
5262 | if (IS_ERR(set)) | |
5263 | return PTR_ERR(set); | |
958bee14 | 5264 | |
20a69341 PM |
5265 | if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT) |
5266 | return -EBUSY; | |
5267 | ||
5268 | nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) { | |
c016c7e4 | 5269 | err = nft_add_set_elem(&ctx, set, attr, nlh->nlmsg_flags); |
35d0ac90 | 5270 | if (err < 0) |
a654de8f | 5271 | return err; |
20a69341 | 5272 | } |
a654de8f PNA |
5273 | |
5274 | if (net->nft.validate_state == NFT_VALIDATE_DO) | |
5275 | return nft_table_validate(net, ctx.table); | |
5276 | ||
5277 | return 0; | |
20a69341 PM |
5278 | } |
5279 | ||
59105446 PNA |
5280 | /** |
5281 | * nft_data_hold - hold a nft_data item | |
5282 | * | |
5283 | * @data: struct nft_data to release | |
5284 | * @type: type of data | |
5285 | * | |
5286 | * Hold a nft_data item. NFT_DATA_VALUE types can be silently discarded, | |
5287 | * NFT_DATA_VERDICT bumps the reference to chains in case of NFT_JUMP and | |
5288 | * NFT_GOTO verdicts. This function must be called on active data objects | |
5289 | * from the second phase of the commit protocol. | |
5290 | */ | |
bb7b40ae | 5291 | void nft_data_hold(const struct nft_data *data, enum nft_data_types type) |
59105446 PNA |
5292 | { |
5293 | if (type == NFT_DATA_VERDICT) { | |
5294 | switch (data->verdict.code) { | |
5295 | case NFT_JUMP: | |
5296 | case NFT_GOTO: | |
5297 | data->verdict.chain->use++; | |
5298 | break; | |
5299 | } | |
5300 | } | |
5301 | } | |
5302 | ||
5303 | static void nft_set_elem_activate(const struct net *net, | |
5304 | const struct nft_set *set, | |
5305 | struct nft_set_elem *elem) | |
5306 | { | |
5307 | const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); | |
5308 | ||
5309 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA)) | |
5310 | nft_data_hold(nft_set_ext_data(ext), set->dtype); | |
5311 | if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF)) | |
5312 | (*nft_set_ext_obj(ext))->use++; | |
5313 | } | |
5314 | ||
5315 | static void nft_set_elem_deactivate(const struct net *net, | |
5316 | const struct nft_set *set, | |
5317 | struct nft_set_elem *elem) | |
5318 | { | |
5319 | const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); | |
5320 | ||
5321 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA)) | |
5322 | nft_data_release(nft_set_ext_data(ext), set->dtype); | |
5323 | if (nft_set_ext_exists(ext, NFT_SET_EXT_OBJREF)) | |
5324 | (*nft_set_ext_obj(ext))->use--; | |
5325 | } | |
5326 | ||
60319eb1 | 5327 | static int nft_del_setelem(struct nft_ctx *ctx, struct nft_set *set, |
20a69341 PM |
5328 | const struct nlattr *attr) |
5329 | { | |
5330 | struct nlattr *nla[NFTA_SET_ELEM_MAX + 1]; | |
3971ca14 | 5331 | struct nft_set_ext_tmpl tmpl; |
20a69341 | 5332 | struct nft_set_elem elem; |
3971ca14 | 5333 | struct nft_set_ext *ext; |
60319eb1 | 5334 | struct nft_trans *trans; |
3971ca14 PNA |
5335 | u32 flags = 0; |
5336 | void *priv; | |
20a69341 PM |
5337 | int err; |
5338 | ||
8cb08174 JB |
5339 | err = nla_parse_nested_deprecated(nla, NFTA_SET_ELEM_MAX, attr, |
5340 | nft_set_elem_policy, NULL); | |
20a69341 | 5341 | if (err < 0) |
20a1452c | 5342 | return err; |
20a69341 | 5343 | |
20a69341 | 5344 | if (nla[NFTA_SET_ELEM_KEY] == NULL) |
20a1452c | 5345 | return -EINVAL; |
20a69341 | 5346 | |
3971ca14 PNA |
5347 | nft_set_ext_prepare(&tmpl); |
5348 | ||
5349 | err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags); | |
5350 | if (err < 0) | |
5351 | return err; | |
5352 | if (flags != 0) | |
5353 | nft_set_ext_add(&tmpl, NFT_SET_EXT_FLAGS); | |
5354 | ||
20a1452c PNA |
5355 | err = nft_setelem_parse_key(ctx, set, &elem.key.val, |
5356 | nla[NFTA_SET_ELEM_KEY]); | |
20a69341 | 5357 | if (err < 0) |
20a1452c | 5358 | return err; |
20a69341 | 5359 | |
20a1452c | 5360 | nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY, set->klen); |
3971ca14 | 5361 | |
7b225d0b PNA |
5362 | if (nla[NFTA_SET_ELEM_KEY_END]) { |
5363 | err = nft_setelem_parse_key(ctx, set, &elem.key_end.val, | |
5364 | nla[NFTA_SET_ELEM_KEY_END]); | |
5365 | if (err < 0) | |
5366 | return err; | |
5367 | ||
5368 | nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY_END, set->klen); | |
5369 | } | |
5370 | ||
3971ca14 | 5371 | err = -ENOMEM; |
7b225d0b PNA |
5372 | elem.priv = nft_set_elem_init(set, &tmpl, elem.key.val.data, |
5373 | elem.key_end.val.data, NULL, 0, 0, | |
5374 | GFP_KERNEL); | |
3971ca14 | 5375 | if (elem.priv == NULL) |
20a1452c | 5376 | goto fail_elem; |
3971ca14 PNA |
5377 | |
5378 | ext = nft_set_elem_ext(set, elem.priv); | |
5379 | if (flags) | |
5380 | *nft_set_ext_flags(ext) = flags; | |
5381 | ||
60319eb1 | 5382 | trans = nft_trans_elem_alloc(ctx, NFT_MSG_DELSETELEM, set); |
20a1452c PNA |
5383 | if (trans == NULL) |
5384 | goto fail_trans; | |
20a69341 | 5385 | |
42a55769 | 5386 | priv = set->ops->deactivate(ctx->net, set, &elem); |
3971ca14 | 5387 | if (priv == NULL) { |
cc02e457 | 5388 | err = -ENOENT; |
20a1452c | 5389 | goto fail_ops; |
cc02e457 | 5390 | } |
3971ca14 PNA |
5391 | kfree(elem.priv); |
5392 | elem.priv = priv; | |
cc02e457 | 5393 | |
59105446 PNA |
5394 | nft_set_elem_deactivate(ctx->net, set, &elem); |
5395 | ||
60319eb1 | 5396 | nft_trans_elem(trans) = elem; |
46bbafce | 5397 | list_add_tail(&trans->list, &ctx->net->nft.commit_list); |
0dc13625 | 5398 | return 0; |
cc02e457 | 5399 | |
20a1452c | 5400 | fail_ops: |
cc02e457 | 5401 | kfree(trans); |
20a1452c | 5402 | fail_trans: |
3971ca14 | 5403 | kfree(elem.priv); |
20a1452c PNA |
5404 | fail_elem: |
5405 | nft_data_release(&elem.key.val, NFT_DATA_VALUE); | |
20a69341 PM |
5406 | return err; |
5407 | } | |
5408 | ||
8411b644 | 5409 | static int nft_flush_set(const struct nft_ctx *ctx, |
de70185d | 5410 | struct nft_set *set, |
8411b644 | 5411 | const struct nft_set_iter *iter, |
de70185d | 5412 | struct nft_set_elem *elem) |
8411b644 PNA |
5413 | { |
5414 | struct nft_trans *trans; | |
5415 | int err; | |
5416 | ||
5417 | trans = nft_trans_alloc_gfp(ctx, NFT_MSG_DELSETELEM, | |
5418 | sizeof(struct nft_trans_elem), GFP_ATOMIC); | |
5419 | if (!trans) | |
5420 | return -ENOMEM; | |
5421 | ||
1ba1c414 | 5422 | if (!set->ops->flush(ctx->net, set, elem->priv)) { |
8411b644 PNA |
5423 | err = -ENOENT; |
5424 | goto err1; | |
5425 | } | |
b2c11e4b | 5426 | set->ndeact++; |
8411b644 | 5427 | |
7acfda53 | 5428 | nft_set_elem_deactivate(ctx->net, set, elem); |
de70185d PNA |
5429 | nft_trans_elem_set(trans) = set; |
5430 | nft_trans_elem(trans) = *elem; | |
8411b644 PNA |
5431 | list_add_tail(&trans->list, &ctx->net->nft.commit_list); |
5432 | ||
5433 | return 0; | |
5434 | err1: | |
5435 | kfree(trans); | |
5436 | return err; | |
5437 | } | |
5438 | ||
633c9a84 PNA |
5439 | static int nf_tables_delsetelem(struct net *net, struct sock *nlsk, |
5440 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
04ba724b PNA |
5441 | const struct nlattr * const nla[], |
5442 | struct netlink_ext_ack *extack) | |
20a69341 | 5443 | { |
f2a6d766 | 5444 | u8 genmask = nft_genmask_next(net); |
20a69341 PM |
5445 | const struct nlattr *attr; |
5446 | struct nft_set *set; | |
5447 | struct nft_ctx ctx; | |
60319eb1 | 5448 | int rem, err = 0; |
20a69341 | 5449 | |
36dd1bcc PNA |
5450 | err = nft_ctx_init_from_elemattr(&ctx, net, skb, nlh, nla, extack, |
5451 | genmask); | |
20a69341 PM |
5452 | if (err < 0) |
5453 | return err; | |
5454 | ||
cac20fcd | 5455 | set = nft_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET], genmask); |
20a69341 PM |
5456 | if (IS_ERR(set)) |
5457 | return PTR_ERR(set); | |
5458 | if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT) | |
5459 | return -EBUSY; | |
5460 | ||
8411b644 | 5461 | if (nla[NFTA_SET_ELEM_LIST_ELEMENTS] == NULL) { |
baa2d42c PNA |
5462 | struct nft_set_iter iter = { |
5463 | .genmask = genmask, | |
5464 | .fn = nft_flush_set, | |
8411b644 | 5465 | }; |
baa2d42c | 5466 | set->ops->walk(&ctx, set, &iter); |
8411b644 | 5467 | |
baa2d42c | 5468 | return iter.err; |
8411b644 PNA |
5469 | } |
5470 | ||
20a69341 PM |
5471 | nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) { |
5472 | err = nft_del_setelem(&ctx, set, attr); | |
5473 | if (err < 0) | |
60319eb1 | 5474 | break; |
4fefee57 | 5475 | |
3dd0673a | 5476 | set->ndeact++; |
20a69341 | 5477 | } |
60319eb1 | 5478 | return err; |
20a69341 PM |
5479 | } |
5480 | ||
cfed7e1b PM |
5481 | void nft_set_gc_batch_release(struct rcu_head *rcu) |
5482 | { | |
5483 | struct nft_set_gc_batch *gcb; | |
5484 | unsigned int i; | |
5485 | ||
5486 | gcb = container_of(rcu, struct nft_set_gc_batch, head.rcu); | |
5487 | for (i = 0; i < gcb->head.cnt; i++) | |
61f9e292 | 5488 | nft_set_elem_destroy(gcb->head.set, gcb->elems[i], true); |
cfed7e1b PM |
5489 | kfree(gcb); |
5490 | } | |
cfed7e1b PM |
5491 | |
5492 | struct nft_set_gc_batch *nft_set_gc_batch_alloc(const struct nft_set *set, | |
5493 | gfp_t gfp) | |
5494 | { | |
5495 | struct nft_set_gc_batch *gcb; | |
5496 | ||
5497 | gcb = kzalloc(sizeof(*gcb), gfp); | |
5498 | if (gcb == NULL) | |
5499 | return gcb; | |
5500 | gcb->head.set = set; | |
5501 | return gcb; | |
5502 | } | |
cfed7e1b | 5503 | |
e5009240 PNA |
5504 | /* |
5505 | * Stateful objects | |
5506 | */ | |
5507 | ||
5508 | /** | |
5509 | * nft_register_obj- register nf_tables stateful object type | |
5510 | * @obj: object type | |
5511 | * | |
5512 | * Registers the object type for use with nf_tables. Returns zero on | |
5513 | * success or a negative errno code otherwise. | |
5514 | */ | |
5515 | int nft_register_obj(struct nft_object_type *obj_type) | |
5516 | { | |
5517 | if (obj_type->type == NFT_OBJECT_UNSPEC) | |
5518 | return -EINVAL; | |
5519 | ||
5520 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
5521 | list_add_rcu(&obj_type->list, &nf_tables_objects); | |
5522 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); | |
5523 | return 0; | |
5524 | } | |
5525 | EXPORT_SYMBOL_GPL(nft_register_obj); | |
5526 | ||
5527 | /** | |
5528 | * nft_unregister_obj - unregister nf_tables object type | |
5529 | * @obj: object type | |
5530 | * | |
5531 | * Unregisters the object type for use with nf_tables. | |
5532 | */ | |
5533 | void nft_unregister_obj(struct nft_object_type *obj_type) | |
5534 | { | |
5535 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
5536 | list_del_rcu(&obj_type->list); | |
5537 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); | |
5538 | } | |
5539 | EXPORT_SYMBOL_GPL(nft_unregister_obj); | |
5540 | ||
4d44175a FW |
5541 | struct nft_object *nft_obj_lookup(const struct net *net, |
5542 | const struct nft_table *table, | |
cac20fcd PNA |
5543 | const struct nlattr *nla, u32 objtype, |
5544 | u8 genmask) | |
e5009240 | 5545 | { |
4d44175a FW |
5546 | struct nft_object_hash_key k = { .table = table }; |
5547 | char search[NFT_OBJ_MAXNAMELEN]; | |
5548 | struct rhlist_head *tmp, *list; | |
e5009240 PNA |
5549 | struct nft_object *obj; |
5550 | ||
4d44175a FW |
5551 | nla_strlcpy(search, nla, sizeof(search)); |
5552 | k.name = search; | |
5553 | ||
5554 | WARN_ON_ONCE(!rcu_read_lock_held() && | |
5555 | !lockdep_commit_lock_is_held(net)); | |
5556 | ||
5557 | rcu_read_lock(); | |
5558 | list = rhltable_lookup(&nft_objname_ht, &k, nft_objname_ht_params); | |
5559 | if (!list) | |
5560 | goto out; | |
5561 | ||
5562 | rhl_for_each_entry_rcu(obj, tmp, list, rhlhead) { | |
5563 | if (objtype == obj->ops->type->type && | |
5564 | nft_active_genmask(obj, genmask)) { | |
5565 | rcu_read_unlock(); | |
e5009240 | 5566 | return obj; |
4d44175a | 5567 | } |
e5009240 | 5568 | } |
4d44175a FW |
5569 | out: |
5570 | rcu_read_unlock(); | |
e5009240 PNA |
5571 | return ERR_PTR(-ENOENT); |
5572 | } | |
cac20fcd | 5573 | EXPORT_SYMBOL_GPL(nft_obj_lookup); |
e5009240 | 5574 | |
cac20fcd PNA |
5575 | static struct nft_object *nft_obj_lookup_byhandle(const struct nft_table *table, |
5576 | const struct nlattr *nla, | |
5577 | u32 objtype, u8 genmask) | |
3ecbfd65 HS |
5578 | { |
5579 | struct nft_object *obj; | |
5580 | ||
5581 | list_for_each_entry(obj, &table->objects, list) { | |
5582 | if (be64_to_cpu(nla_get_be64(nla)) == obj->handle && | |
5583 | objtype == obj->ops->type->type && | |
5584 | nft_active_genmask(obj, genmask)) | |
5585 | return obj; | |
5586 | } | |
5587 | return ERR_PTR(-ENOENT); | |
5588 | } | |
5589 | ||
e5009240 | 5590 | static const struct nla_policy nft_obj_policy[NFTA_OBJ_MAX + 1] = { |
b2fbd044 LZ |
5591 | [NFTA_OBJ_TABLE] = { .type = NLA_STRING, |
5592 | .len = NFT_TABLE_MAXNAMELEN - 1 }, | |
5593 | [NFTA_OBJ_NAME] = { .type = NLA_STRING, | |
5594 | .len = NFT_OBJ_MAXNAMELEN - 1 }, | |
e5009240 PNA |
5595 | [NFTA_OBJ_TYPE] = { .type = NLA_U32 }, |
5596 | [NFTA_OBJ_DATA] = { .type = NLA_NESTED }, | |
3ecbfd65 | 5597 | [NFTA_OBJ_HANDLE] = { .type = NLA_U64}, |
e5009240 PNA |
5598 | }; |
5599 | ||
84fba055 FW |
5600 | static struct nft_object *nft_obj_init(const struct nft_ctx *ctx, |
5601 | const struct nft_object_type *type, | |
e5009240 PNA |
5602 | const struct nlattr *attr) |
5603 | { | |
5b4c6e38 | 5604 | struct nlattr **tb; |
dfc46034 | 5605 | const struct nft_object_ops *ops; |
e5009240 | 5606 | struct nft_object *obj; |
5b4c6e38 GS |
5607 | int err = -ENOMEM; |
5608 | ||
5609 | tb = kmalloc_array(type->maxattr + 1, sizeof(*tb), GFP_KERNEL); | |
5610 | if (!tb) | |
5611 | goto err1; | |
e5009240 PNA |
5612 | |
5613 | if (attr) { | |
8cb08174 JB |
5614 | err = nla_parse_nested_deprecated(tb, type->maxattr, attr, |
5615 | type->policy, NULL); | |
e5009240 | 5616 | if (err < 0) |
5b4c6e38 | 5617 | goto err2; |
e5009240 PNA |
5618 | } else { |
5619 | memset(tb, 0, sizeof(tb[0]) * (type->maxattr + 1)); | |
5620 | } | |
5621 | ||
dfc46034 PBG |
5622 | if (type->select_ops) { |
5623 | ops = type->select_ops(ctx, (const struct nlattr * const *)tb); | |
5624 | if (IS_ERR(ops)) { | |
5625 | err = PTR_ERR(ops); | |
5b4c6e38 | 5626 | goto err2; |
dfc46034 PBG |
5627 | } |
5628 | } else { | |
5629 | ops = type->ops; | |
5630 | } | |
5631 | ||
e5009240 | 5632 | err = -ENOMEM; |
dfc46034 | 5633 | obj = kzalloc(sizeof(*obj) + ops->size, GFP_KERNEL); |
5b4c6e38 GS |
5634 | if (!obj) |
5635 | goto err2; | |
e5009240 | 5636 | |
dfc46034 | 5637 | err = ops->init(ctx, (const struct nlattr * const *)tb, obj); |
e5009240 | 5638 | if (err < 0) |
5b4c6e38 | 5639 | goto err3; |
e5009240 | 5640 | |
dfc46034 PBG |
5641 | obj->ops = ops; |
5642 | ||
5b4c6e38 | 5643 | kfree(tb); |
e5009240 | 5644 | return obj; |
5b4c6e38 | 5645 | err3: |
e5009240 | 5646 | kfree(obj); |
5b4c6e38 GS |
5647 | err2: |
5648 | kfree(tb); | |
e5009240 PNA |
5649 | err1: |
5650 | return ERR_PTR(err); | |
5651 | } | |
5652 | ||
5653 | static int nft_object_dump(struct sk_buff *skb, unsigned int attr, | |
43da04a5 | 5654 | struct nft_object *obj, bool reset) |
e5009240 PNA |
5655 | { |
5656 | struct nlattr *nest; | |
5657 | ||
ae0be8de | 5658 | nest = nla_nest_start_noflag(skb, attr); |
e5009240 PNA |
5659 | if (!nest) |
5660 | goto nla_put_failure; | |
dfc46034 | 5661 | if (obj->ops->dump(skb, obj, reset) < 0) |
e5009240 PNA |
5662 | goto nla_put_failure; |
5663 | nla_nest_end(skb, nest); | |
5664 | return 0; | |
5665 | ||
5666 | nla_put_failure: | |
5667 | return -1; | |
5668 | } | |
5669 | ||
5670 | static const struct nft_object_type *__nft_obj_type_get(u32 objtype) | |
5671 | { | |
5672 | const struct nft_object_type *type; | |
5673 | ||
5674 | list_for_each_entry(type, &nf_tables_objects, list) { | |
5675 | if (objtype == type->type) | |
5676 | return type; | |
5677 | } | |
5678 | return NULL; | |
5679 | } | |
5680 | ||
452238e8 FW |
5681 | static const struct nft_object_type * |
5682 | nft_obj_type_get(struct net *net, u32 objtype) | |
e5009240 PNA |
5683 | { |
5684 | const struct nft_object_type *type; | |
5685 | ||
5686 | type = __nft_obj_type_get(objtype); | |
5687 | if (type != NULL && try_module_get(type->owner)) | |
5688 | return type; | |
5689 | ||
f102d66b | 5690 | lockdep_nfnl_nft_mutex_not_held(); |
e5009240 PNA |
5691 | #ifdef CONFIG_MODULES |
5692 | if (type == NULL) { | |
eb014de4 | 5693 | if (nft_request_module(net, "nft-obj-%u", objtype) == -EAGAIN) |
e5009240 PNA |
5694 | return ERR_PTR(-EAGAIN); |
5695 | } | |
5696 | #endif | |
5697 | return ERR_PTR(-ENOENT); | |
5698 | } | |
5699 | ||
d62d0ba9 FFM |
5700 | static int nf_tables_updobj(const struct nft_ctx *ctx, |
5701 | const struct nft_object_type *type, | |
5702 | const struct nlattr *attr, | |
5703 | struct nft_object *obj) | |
5704 | { | |
5705 | struct nft_object *newobj; | |
5706 | struct nft_trans *trans; | |
5707 | int err; | |
5708 | ||
5709 | trans = nft_trans_alloc(ctx, NFT_MSG_NEWOBJ, | |
5710 | sizeof(struct nft_trans_obj)); | |
5711 | if (!trans) | |
5712 | return -ENOMEM; | |
5713 | ||
5714 | newobj = nft_obj_init(ctx, type, attr); | |
5715 | if (IS_ERR(newobj)) { | |
5716 | err = PTR_ERR(newobj); | |
b74ae961 | 5717 | goto err_free_trans; |
d62d0ba9 FFM |
5718 | } |
5719 | ||
5720 | nft_trans_obj(trans) = obj; | |
5721 | nft_trans_obj_update(trans) = true; | |
5722 | nft_trans_obj_newobj(trans) = newobj; | |
5723 | list_add_tail(&trans->list, &ctx->net->nft.commit_list); | |
5724 | ||
5725 | return 0; | |
b74ae961 DC |
5726 | |
5727 | err_free_trans: | |
d62d0ba9 | 5728 | kfree(trans); |
d62d0ba9 FFM |
5729 | return err; |
5730 | } | |
5731 | ||
e5009240 PNA |
5732 | static int nf_tables_newobj(struct net *net, struct sock *nlsk, |
5733 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
04ba724b PNA |
5734 | const struct nlattr * const nla[], |
5735 | struct netlink_ext_ack *extack) | |
e5009240 PNA |
5736 | { |
5737 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
5738 | const struct nft_object_type *type; | |
5739 | u8 genmask = nft_genmask_next(net); | |
5740 | int family = nfmsg->nfgen_family; | |
e5009240 PNA |
5741 | struct nft_table *table; |
5742 | struct nft_object *obj; | |
5743 | struct nft_ctx ctx; | |
5744 | u32 objtype; | |
5745 | int err; | |
5746 | ||
5747 | if (!nla[NFTA_OBJ_TYPE] || | |
5748 | !nla[NFTA_OBJ_NAME] || | |
5749 | !nla[NFTA_OBJ_DATA]) | |
5750 | return -EINVAL; | |
5751 | ||
cac20fcd | 5752 | table = nft_table_lookup(net, nla[NFTA_OBJ_TABLE], family, genmask); |
36dd1bcc PNA |
5753 | if (IS_ERR(table)) { |
5754 | NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_TABLE]); | |
e5009240 | 5755 | return PTR_ERR(table); |
36dd1bcc | 5756 | } |
e5009240 PNA |
5757 | |
5758 | objtype = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE])); | |
4d44175a | 5759 | obj = nft_obj_lookup(net, table, nla[NFTA_OBJ_NAME], objtype, genmask); |
e5009240 PNA |
5760 | if (IS_ERR(obj)) { |
5761 | err = PTR_ERR(obj); | |
36dd1bcc PNA |
5762 | if (err != -ENOENT) { |
5763 | NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_NAME]); | |
e5009240 | 5764 | return err; |
36dd1bcc | 5765 | } |
1a28ad74 | 5766 | } else { |
36dd1bcc PNA |
5767 | if (nlh->nlmsg_flags & NLM_F_EXCL) { |
5768 | NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_NAME]); | |
e5009240 | 5769 | return -EEXIST; |
36dd1bcc | 5770 | } |
d62d0ba9 FFM |
5771 | if (nlh->nlmsg_flags & NLM_F_REPLACE) |
5772 | return -EOPNOTSUPP; | |
5773 | ||
fd57d0cb | 5774 | type = __nft_obj_type_get(objtype); |
d62d0ba9 FFM |
5775 | nft_ctx_init(&ctx, net, skb, nlh, family, table, NULL, nla); |
5776 | ||
5777 | return nf_tables_updobj(&ctx, type, nla[NFTA_OBJ_DATA], obj); | |
e5009240 PNA |
5778 | } |
5779 | ||
98319cb9 | 5780 | nft_ctx_init(&ctx, net, skb, nlh, family, table, NULL, nla); |
e5009240 | 5781 | |
452238e8 | 5782 | type = nft_obj_type_get(net, objtype); |
e5009240 PNA |
5783 | if (IS_ERR(type)) |
5784 | return PTR_ERR(type); | |
5785 | ||
84fba055 | 5786 | obj = nft_obj_init(&ctx, type, nla[NFTA_OBJ_DATA]); |
e5009240 PNA |
5787 | if (IS_ERR(obj)) { |
5788 | err = PTR_ERR(obj); | |
5789 | goto err1; | |
5790 | } | |
d152159b | 5791 | obj->key.table = table; |
3ecbfd65 HS |
5792 | obj->handle = nf_tables_alloc_handle(table); |
5793 | ||
d152159b FW |
5794 | obj->key.name = nla_strdup(nla[NFTA_OBJ_NAME], GFP_KERNEL); |
5795 | if (!obj->key.name) { | |
61509575 PS |
5796 | err = -ENOMEM; |
5797 | goto err2; | |
5798 | } | |
e5009240 PNA |
5799 | |
5800 | err = nft_trans_obj_add(&ctx, NFT_MSG_NEWOBJ, obj); | |
5801 | if (err < 0) | |
61509575 | 5802 | goto err3; |
e5009240 | 5803 | |
4d44175a FW |
5804 | err = rhltable_insert(&nft_objname_ht, &obj->rhlhead, |
5805 | nft_objname_ht_params); | |
5806 | if (err < 0) | |
5807 | goto err4; | |
5808 | ||
e5009240 PNA |
5809 | list_add_tail_rcu(&obj->list, &table->objects); |
5810 | table->use++; | |
5811 | return 0; | |
4d44175a FW |
5812 | err4: |
5813 | /* queued in transaction log */ | |
5814 | INIT_LIST_HEAD(&obj->list); | |
5815 | return err; | |
61509575 | 5816 | err3: |
d152159b | 5817 | kfree(obj->key.name); |
e5009240 | 5818 | err2: |
dfc46034 | 5819 | if (obj->ops->destroy) |
00bfb320 | 5820 | obj->ops->destroy(&ctx, obj); |
e5009240 PNA |
5821 | kfree(obj); |
5822 | err1: | |
5823 | module_put(type->owner); | |
5824 | return err; | |
5825 | } | |
5826 | ||
5827 | static int nf_tables_fill_obj_info(struct sk_buff *skb, struct net *net, | |
5828 | u32 portid, u32 seq, int event, u32 flags, | |
5829 | int family, const struct nft_table *table, | |
43da04a5 | 5830 | struct nft_object *obj, bool reset) |
e5009240 PNA |
5831 | { |
5832 | struct nfgenmsg *nfmsg; | |
5833 | struct nlmsghdr *nlh; | |
5834 | ||
dedb67c4 | 5835 | event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); |
e5009240 PNA |
5836 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags); |
5837 | if (nlh == NULL) | |
5838 | goto nla_put_failure; | |
5839 | ||
5840 | nfmsg = nlmsg_data(nlh); | |
5841 | nfmsg->nfgen_family = family; | |
5842 | nfmsg->version = NFNETLINK_V0; | |
5843 | nfmsg->res_id = htons(net->nft.base_seq & 0xffff); | |
5844 | ||
5845 | if (nla_put_string(skb, NFTA_OBJ_TABLE, table->name) || | |
d152159b | 5846 | nla_put_string(skb, NFTA_OBJ_NAME, obj->key.name) || |
dfc46034 | 5847 | nla_put_be32(skb, NFTA_OBJ_TYPE, htonl(obj->ops->type->type)) || |
e5009240 | 5848 | nla_put_be32(skb, NFTA_OBJ_USE, htonl(obj->use)) || |
3ecbfd65 HS |
5849 | nft_object_dump(skb, NFTA_OBJ_DATA, obj, reset) || |
5850 | nla_put_be64(skb, NFTA_OBJ_HANDLE, cpu_to_be64(obj->handle), | |
5851 | NFTA_OBJ_PAD)) | |
e5009240 PNA |
5852 | goto nla_put_failure; |
5853 | ||
5854 | nlmsg_end(skb, nlh); | |
5855 | return 0; | |
5856 | ||
5857 | nla_put_failure: | |
5858 | nlmsg_trim(skb, nlh); | |
5859 | return -1; | |
5860 | } | |
5861 | ||
a9fea2a3 | 5862 | struct nft_obj_filter { |
e46abbcc | 5863 | char *table; |
a9fea2a3 PNA |
5864 | u32 type; |
5865 | }; | |
5866 | ||
e5009240 PNA |
5867 | static int nf_tables_dump_obj(struct sk_buff *skb, struct netlink_callback *cb) |
5868 | { | |
5869 | const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); | |
e5009240 | 5870 | const struct nft_table *table; |
e5009240 | 5871 | unsigned int idx = 0, s_idx = cb->args[0]; |
a9fea2a3 | 5872 | struct nft_obj_filter *filter = cb->data; |
e5009240 PNA |
5873 | struct net *net = sock_net(skb->sk); |
5874 | int family = nfmsg->nfgen_family; | |
43da04a5 PNA |
5875 | struct nft_object *obj; |
5876 | bool reset = false; | |
5877 | ||
5878 | if (NFNL_MSG_TYPE(cb->nlh->nlmsg_type) == NFT_MSG_GETOBJ_RESET) | |
5879 | reset = true; | |
e5009240 PNA |
5880 | |
5881 | rcu_read_lock(); | |
5882 | cb->seq = net->nft.base_seq; | |
5883 | ||
36596dad | 5884 | list_for_each_entry_rcu(table, &net->nft.tables, list) { |
98319cb9 | 5885 | if (family != NFPROTO_UNSPEC && family != table->family) |
e5009240 PNA |
5886 | continue; |
5887 | ||
36596dad PNA |
5888 | list_for_each_entry_rcu(obj, &table->objects, list) { |
5889 | if (!nft_is_active(net, obj)) | |
5890 | goto cont; | |
5891 | if (idx < s_idx) | |
5892 | goto cont; | |
5893 | if (idx > s_idx) | |
5894 | memset(&cb->args[1], 0, | |
5895 | sizeof(cb->args) - sizeof(cb->args[0])); | |
360cc79d | 5896 | if (filter && filter->table && |
36596dad PNA |
5897 | strcmp(filter->table, table->name)) |
5898 | goto cont; | |
5899 | if (filter && | |
5900 | filter->type != NFT_OBJECT_UNSPEC && | |
5901 | obj->ops->type->type != filter->type) | |
5902 | goto cont; | |
a9fea2a3 | 5903 | |
36596dad PNA |
5904 | if (nf_tables_fill_obj_info(skb, net, NETLINK_CB(cb->skb).portid, |
5905 | cb->nlh->nlmsg_seq, | |
5906 | NFT_MSG_NEWOBJ, | |
5907 | NLM_F_MULTI | NLM_F_APPEND, | |
98319cb9 | 5908 | table->family, table, |
36596dad PNA |
5909 | obj, reset) < 0) |
5910 | goto done; | |
e5009240 | 5911 | |
36596dad | 5912 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); |
e5009240 | 5913 | cont: |
36596dad | 5914 | idx++; |
e5009240 PNA |
5915 | } |
5916 | } | |
5917 | done: | |
5918 | rcu_read_unlock(); | |
5919 | ||
5920 | cb->args[0] = idx; | |
5921 | return skb->len; | |
5922 | } | |
5923 | ||
90fd131a | 5924 | static int nf_tables_dump_obj_start(struct netlink_callback *cb) |
a9fea2a3 | 5925 | { |
90fd131a FW |
5926 | const struct nlattr * const *nla = cb->data; |
5927 | struct nft_obj_filter *filter = NULL; | |
e46abbcc | 5928 | |
90fd131a FW |
5929 | if (nla[NFTA_OBJ_TABLE] || nla[NFTA_OBJ_TYPE]) { |
5930 | filter = kzalloc(sizeof(*filter), GFP_ATOMIC); | |
5931 | if (!filter) | |
5932 | return -ENOMEM; | |
5933 | ||
5934 | if (nla[NFTA_OBJ_TABLE]) { | |
5935 | filter->table = nla_strdup(nla[NFTA_OBJ_TABLE], GFP_ATOMIC); | |
5936 | if (!filter->table) { | |
5937 | kfree(filter); | |
5938 | return -ENOMEM; | |
5939 | } | |
5940 | } | |
5941 | ||
5942 | if (nla[NFTA_OBJ_TYPE]) | |
5943 | filter->type = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE])); | |
8bea728d | 5944 | } |
a9fea2a3 | 5945 | |
90fd131a | 5946 | cb->data = filter; |
a9fea2a3 PNA |
5947 | return 0; |
5948 | } | |
5949 | ||
90fd131a | 5950 | static int nf_tables_dump_obj_done(struct netlink_callback *cb) |
a9fea2a3 | 5951 | { |
90fd131a | 5952 | struct nft_obj_filter *filter = cb->data; |
a9fea2a3 | 5953 | |
90fd131a FW |
5954 | if (filter) { |
5955 | kfree(filter->table); | |
5956 | kfree(filter); | |
e46abbcc | 5957 | } |
a9fea2a3 | 5958 | |
90fd131a | 5959 | return 0; |
a9fea2a3 PNA |
5960 | } |
5961 | ||
d9adf22a | 5962 | /* called with rcu_read_lock held */ |
e5009240 PNA |
5963 | static int nf_tables_getobj(struct net *net, struct sock *nlsk, |
5964 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
04ba724b PNA |
5965 | const struct nlattr * const nla[], |
5966 | struct netlink_ext_ack *extack) | |
e5009240 PNA |
5967 | { |
5968 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
5969 | u8 genmask = nft_genmask_cur(net); | |
5970 | int family = nfmsg->nfgen_family; | |
e5009240 PNA |
5971 | const struct nft_table *table; |
5972 | struct nft_object *obj; | |
5973 | struct sk_buff *skb2; | |
43da04a5 | 5974 | bool reset = false; |
e5009240 PNA |
5975 | u32 objtype; |
5976 | int err; | |
5977 | ||
5978 | if (nlh->nlmsg_flags & NLM_F_DUMP) { | |
5979 | struct netlink_dump_control c = { | |
90fd131a | 5980 | .start = nf_tables_dump_obj_start, |
e5009240 | 5981 | .dump = nf_tables_dump_obj, |
a9fea2a3 | 5982 | .done = nf_tables_dump_obj_done, |
d9adf22a | 5983 | .module = THIS_MODULE, |
90fd131a | 5984 | .data = (void *)nla, |
e5009240 | 5985 | }; |
a9fea2a3 | 5986 | |
d9adf22a | 5987 | return nft_netlink_dump_start_rcu(nlsk, skb, nlh, &c); |
e5009240 PNA |
5988 | } |
5989 | ||
5990 | if (!nla[NFTA_OBJ_NAME] || | |
5991 | !nla[NFTA_OBJ_TYPE]) | |
5992 | return -EINVAL; | |
5993 | ||
cac20fcd | 5994 | table = nft_table_lookup(net, nla[NFTA_OBJ_TABLE], family, genmask); |
36dd1bcc PNA |
5995 | if (IS_ERR(table)) { |
5996 | NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_TABLE]); | |
e5009240 | 5997 | return PTR_ERR(table); |
36dd1bcc | 5998 | } |
e5009240 PNA |
5999 | |
6000 | objtype = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE])); | |
4d44175a | 6001 | obj = nft_obj_lookup(net, table, nla[NFTA_OBJ_NAME], objtype, genmask); |
36dd1bcc PNA |
6002 | if (IS_ERR(obj)) { |
6003 | NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_NAME]); | |
e5009240 | 6004 | return PTR_ERR(obj); |
36dd1bcc | 6005 | } |
e5009240 | 6006 | |
d9adf22a | 6007 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
e5009240 PNA |
6008 | if (!skb2) |
6009 | return -ENOMEM; | |
6010 | ||
43da04a5 PNA |
6011 | if (NFNL_MSG_TYPE(nlh->nlmsg_type) == NFT_MSG_GETOBJ_RESET) |
6012 | reset = true; | |
6013 | ||
e5009240 PNA |
6014 | err = nf_tables_fill_obj_info(skb2, net, NETLINK_CB(skb).portid, |
6015 | nlh->nlmsg_seq, NFT_MSG_NEWOBJ, 0, | |
43da04a5 | 6016 | family, table, obj, reset); |
e5009240 PNA |
6017 | if (err < 0) |
6018 | goto err; | |
6019 | ||
6020 | return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid); | |
6021 | err: | |
6022 | kfree_skb(skb2); | |
6023 | return err; | |
e5009240 PNA |
6024 | } |
6025 | ||
00bfb320 | 6026 | static void nft_obj_destroy(const struct nft_ctx *ctx, struct nft_object *obj) |
e5009240 | 6027 | { |
dfc46034 | 6028 | if (obj->ops->destroy) |
00bfb320 | 6029 | obj->ops->destroy(ctx, obj); |
e5009240 | 6030 | |
dfc46034 | 6031 | module_put(obj->ops->type->owner); |
d152159b | 6032 | kfree(obj->key.name); |
e5009240 PNA |
6033 | kfree(obj); |
6034 | } | |
6035 | ||
6036 | static int nf_tables_delobj(struct net *net, struct sock *nlsk, | |
04ba724b PNA |
6037 | struct sk_buff *skb, const struct nlmsghdr *nlh, |
6038 | const struct nlattr * const nla[], | |
6039 | struct netlink_ext_ack *extack) | |
e5009240 PNA |
6040 | { |
6041 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
6042 | u8 genmask = nft_genmask_next(net); | |
6043 | int family = nfmsg->nfgen_family; | |
36dd1bcc | 6044 | const struct nlattr *attr; |
e5009240 PNA |
6045 | struct nft_table *table; |
6046 | struct nft_object *obj; | |
6047 | struct nft_ctx ctx; | |
6048 | u32 objtype; | |
6049 | ||
6050 | if (!nla[NFTA_OBJ_TYPE] || | |
3ecbfd65 | 6051 | (!nla[NFTA_OBJ_NAME] && !nla[NFTA_OBJ_HANDLE])) |
e5009240 PNA |
6052 | return -EINVAL; |
6053 | ||
cac20fcd | 6054 | table = nft_table_lookup(net, nla[NFTA_OBJ_TABLE], family, genmask); |
36dd1bcc PNA |
6055 | if (IS_ERR(table)) { |
6056 | NL_SET_BAD_ATTR(extack, nla[NFTA_OBJ_TABLE]); | |
e5009240 | 6057 | return PTR_ERR(table); |
36dd1bcc | 6058 | } |
e5009240 PNA |
6059 | |
6060 | objtype = ntohl(nla_get_be32(nla[NFTA_OBJ_TYPE])); | |
36dd1bcc PNA |
6061 | if (nla[NFTA_OBJ_HANDLE]) { |
6062 | attr = nla[NFTA_OBJ_HANDLE]; | |
6063 | obj = nft_obj_lookup_byhandle(table, attr, objtype, genmask); | |
6064 | } else { | |
6065 | attr = nla[NFTA_OBJ_NAME]; | |
4d44175a | 6066 | obj = nft_obj_lookup(net, table, attr, objtype, genmask); |
36dd1bcc PNA |
6067 | } |
6068 | ||
6069 | if (IS_ERR(obj)) { | |
6070 | NL_SET_BAD_ATTR(extack, attr); | |
e5009240 | 6071 | return PTR_ERR(obj); |
36dd1bcc PNA |
6072 | } |
6073 | if (obj->use > 0) { | |
6074 | NL_SET_BAD_ATTR(extack, attr); | |
e5009240 | 6075 | return -EBUSY; |
36dd1bcc | 6076 | } |
e5009240 | 6077 | |
98319cb9 | 6078 | nft_ctx_init(&ctx, net, skb, nlh, family, table, NULL, nla); |
e5009240 PNA |
6079 | |
6080 | return nft_delobj(&ctx, obj); | |
6081 | } | |
6082 | ||
d152159b | 6083 | void nft_obj_notify(struct net *net, const struct nft_table *table, |
25e94a99 PNA |
6084 | struct nft_object *obj, u32 portid, u32 seq, int event, |
6085 | int family, int report, gfp_t gfp) | |
e5009240 PNA |
6086 | { |
6087 | struct sk_buff *skb; | |
6088 | int err; | |
6089 | ||
2599e989 PNA |
6090 | if (!report && |
6091 | !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES)) | |
25e94a99 | 6092 | return; |
e5009240 | 6093 | |
2599e989 | 6094 | skb = nlmsg_new(NLMSG_GOODSIZE, gfp); |
e5009240 PNA |
6095 | if (skb == NULL) |
6096 | goto err; | |
6097 | ||
2599e989 PNA |
6098 | err = nf_tables_fill_obj_info(skb, net, portid, seq, event, 0, family, |
6099 | table, obj, false); | |
e5009240 PNA |
6100 | if (err < 0) { |
6101 | kfree_skb(skb); | |
6102 | goto err; | |
6103 | } | |
6104 | ||
25e94a99 PNA |
6105 | nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, report, gfp); |
6106 | return; | |
e5009240 | 6107 | err: |
25e94a99 | 6108 | nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, -ENOBUFS); |
e5009240 | 6109 | } |
2599e989 PNA |
6110 | EXPORT_SYMBOL_GPL(nft_obj_notify); |
6111 | ||
25e94a99 PNA |
6112 | static void nf_tables_obj_notify(const struct nft_ctx *ctx, |
6113 | struct nft_object *obj, int event) | |
2599e989 | 6114 | { |
25e94a99 | 6115 | nft_obj_notify(ctx->net, ctx->table, obj, ctx->portid, ctx->seq, event, |
36596dad | 6116 | ctx->family, ctx->report, GFP_KERNEL); |
2599e989 | 6117 | } |
e5009240 | 6118 | |
3b49e2e9 PNA |
6119 | /* |
6120 | * Flow tables | |
6121 | */ | |
6122 | void nft_register_flowtable_type(struct nf_flowtable_type *type) | |
6123 | { | |
6124 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
6125 | list_add_tail_rcu(&type->list, &nf_tables_flowtables); | |
6126 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); | |
6127 | } | |
6128 | EXPORT_SYMBOL_GPL(nft_register_flowtable_type); | |
6129 | ||
6130 | void nft_unregister_flowtable_type(struct nf_flowtable_type *type) | |
6131 | { | |
6132 | nfnl_lock(NFNL_SUBSYS_NFTABLES); | |
6133 | list_del_rcu(&type->list); | |
6134 | nfnl_unlock(NFNL_SUBSYS_NFTABLES); | |
6135 | } | |
6136 | EXPORT_SYMBOL_GPL(nft_unregister_flowtable_type); | |
6137 | ||
6138 | static const struct nla_policy nft_flowtable_policy[NFTA_FLOWTABLE_MAX + 1] = { | |
6139 | [NFTA_FLOWTABLE_TABLE] = { .type = NLA_STRING, | |
6140 | .len = NFT_NAME_MAXLEN - 1 }, | |
6141 | [NFTA_FLOWTABLE_NAME] = { .type = NLA_STRING, | |
6142 | .len = NFT_NAME_MAXLEN - 1 }, | |
6143 | [NFTA_FLOWTABLE_HOOK] = { .type = NLA_NESTED }, | |
3ecbfd65 | 6144 | [NFTA_FLOWTABLE_HANDLE] = { .type = NLA_U64 }, |
8bb69f3b | 6145 | [NFTA_FLOWTABLE_FLAGS] = { .type = NLA_U32 }, |
3b49e2e9 PNA |
6146 | }; |
6147 | ||
cac20fcd PNA |
6148 | struct nft_flowtable *nft_flowtable_lookup(const struct nft_table *table, |
6149 | const struct nlattr *nla, u8 genmask) | |
3b49e2e9 PNA |
6150 | { |
6151 | struct nft_flowtable *flowtable; | |
6152 | ||
d9adf22a | 6153 | list_for_each_entry_rcu(flowtable, &table->flowtables, list) { |
3b49e2e9 PNA |
6154 | if (!nla_strcmp(nla, flowtable->name) && |
6155 | nft_active_genmask(flowtable, genmask)) | |
6156 | return flowtable; | |
6157 | } | |
6158 | return ERR_PTR(-ENOENT); | |
6159 | } | |
cac20fcd | 6160 | EXPORT_SYMBOL_GPL(nft_flowtable_lookup); |
3b49e2e9 | 6161 | |
9b05b6e1 LGL |
6162 | void nf_tables_deactivate_flowtable(const struct nft_ctx *ctx, |
6163 | struct nft_flowtable *flowtable, | |
6164 | enum nft_trans_phase phase) | |
6165 | { | |
6166 | switch (phase) { | |
6167 | case NFT_TRANS_PREPARE: | |
6168 | case NFT_TRANS_ABORT: | |
6169 | case NFT_TRANS_RELEASE: | |
6170 | flowtable->use--; | |
6171 | /* fall through */ | |
6172 | default: | |
6173 | return; | |
6174 | } | |
6175 | } | |
6176 | EXPORT_SYMBOL_GPL(nf_tables_deactivate_flowtable); | |
6177 | ||
ae0662f8 | 6178 | static struct nft_flowtable * |
cac20fcd PNA |
6179 | nft_flowtable_lookup_byhandle(const struct nft_table *table, |
6180 | const struct nlattr *nla, u8 genmask) | |
3ecbfd65 HS |
6181 | { |
6182 | struct nft_flowtable *flowtable; | |
6183 | ||
6184 | list_for_each_entry(flowtable, &table->flowtables, list) { | |
6185 | if (be64_to_cpu(nla_get_be64(nla)) == flowtable->handle && | |
6186 | nft_active_genmask(flowtable, genmask)) | |
6187 | return flowtable; | |
6188 | } | |
6189 | return ERR_PTR(-ENOENT); | |
6190 | } | |
6191 | ||
d9246a53 PNA |
6192 | struct nft_flowtable_hook { |
6193 | u32 num; | |
6194 | int priority; | |
6195 | struct list_head list; | |
6196 | }; | |
6197 | ||
3b49e2e9 PNA |
6198 | static const struct nla_policy nft_flowtable_hook_policy[NFTA_FLOWTABLE_HOOK_MAX + 1] = { |
6199 | [NFTA_FLOWTABLE_HOOK_NUM] = { .type = NLA_U32 }, | |
6200 | [NFTA_FLOWTABLE_HOOK_PRIORITY] = { .type = NLA_U32 }, | |
6201 | [NFTA_FLOWTABLE_HOOK_DEVS] = { .type = NLA_NESTED }, | |
6202 | }; | |
6203 | ||
d9246a53 PNA |
6204 | static int nft_flowtable_parse_hook(const struct nft_ctx *ctx, |
6205 | const struct nlattr *attr, | |
6206 | struct nft_flowtable_hook *flowtable_hook, | |
5b6743fb | 6207 | struct nft_flowtable *flowtable, bool add) |
3b49e2e9 | 6208 | { |
3b49e2e9 | 6209 | struct nlattr *tb[NFTA_FLOWTABLE_HOOK_MAX + 1]; |
3f0465a9 | 6210 | struct nft_hook *hook; |
3b49e2e9 | 6211 | int hooknum, priority; |
3f0465a9 | 6212 | int err; |
3b49e2e9 | 6213 | |
d9246a53 PNA |
6214 | INIT_LIST_HEAD(&flowtable_hook->list); |
6215 | ||
8cb08174 JB |
6216 | err = nla_parse_nested_deprecated(tb, NFTA_FLOWTABLE_HOOK_MAX, attr, |
6217 | nft_flowtable_hook_policy, NULL); | |
3b49e2e9 PNA |
6218 | if (err < 0) |
6219 | return err; | |
6220 | ||
5b6743fb PNA |
6221 | if (add) { |
6222 | if (!tb[NFTA_FLOWTABLE_HOOK_NUM] || | |
6223 | !tb[NFTA_FLOWTABLE_HOOK_PRIORITY]) | |
6224 | return -EINVAL; | |
3b49e2e9 | 6225 | |
5b6743fb PNA |
6226 | hooknum = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_NUM])); |
6227 | if (hooknum != NF_NETDEV_INGRESS) | |
6228 | return -EOPNOTSUPP; | |
6229 | ||
6230 | priority = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_PRIORITY])); | |
6231 | ||
6232 | flowtable_hook->priority = priority; | |
6233 | flowtable_hook->num = hooknum; | |
6234 | } else { | |
6235 | if (tb[NFTA_FLOWTABLE_HOOK_NUM]) { | |
6236 | hooknum = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_NUM])); | |
6237 | if (hooknum != flowtable->hooknum) | |
6238 | return -EOPNOTSUPP; | |
6239 | } | |
6240 | ||
6241 | if (tb[NFTA_FLOWTABLE_HOOK_PRIORITY]) { | |
6242 | priority = ntohl(nla_get_be32(tb[NFTA_FLOWTABLE_HOOK_PRIORITY])); | |
6243 | if (priority != flowtable->data.priority) | |
6244 | return -EOPNOTSUPP; | |
6245 | } | |
3b49e2e9 | 6246 | |
5b6743fb PNA |
6247 | flowtable_hook->priority = flowtable->data.priority; |
6248 | flowtable_hook->num = flowtable->hooknum; | |
6249 | } | |
3b49e2e9 | 6250 | |
05abe445 PNA |
6251 | if (tb[NFTA_FLOWTABLE_HOOK_DEVS]) { |
6252 | err = nf_tables_parse_netdev_hooks(ctx->net, | |
6253 | tb[NFTA_FLOWTABLE_HOOK_DEVS], | |
6254 | &flowtable_hook->list); | |
6255 | if (err < 0) | |
6256 | return err; | |
6257 | } | |
3b49e2e9 | 6258 | |
d9246a53 | 6259 | list_for_each_entry(hook, &flowtable_hook->list, list) { |
3f0465a9 | 6260 | hook->ops.pf = NFPROTO_NETDEV; |
5b6743fb PNA |
6261 | hook->ops.hooknum = flowtable_hook->num; |
6262 | hook->ops.priority = flowtable_hook->priority; | |
6263 | hook->ops.priv = &flowtable->data; | |
6264 | hook->ops.hook = flowtable->data.type->hook; | |
3b49e2e9 PNA |
6265 | } |
6266 | ||
3b49e2e9 PNA |
6267 | return err; |
6268 | } | |
6269 | ||
98319cb9 | 6270 | static const struct nf_flowtable_type *__nft_flowtable_type_get(u8 family) |
3b49e2e9 PNA |
6271 | { |
6272 | const struct nf_flowtable_type *type; | |
6273 | ||
6274 | list_for_each_entry(type, &nf_tables_flowtables, list) { | |
98319cb9 | 6275 | if (family == type->family) |
3b49e2e9 PNA |
6276 | return type; |
6277 | } | |
6278 | return NULL; | |
6279 | } | |
6280 | ||
452238e8 FW |
6281 | static const struct nf_flowtable_type * |
6282 | nft_flowtable_type_get(struct net *net, u8 family) | |
3b49e2e9 PNA |
6283 | { |
6284 | const struct nf_flowtable_type *type; | |
6285 | ||
98319cb9 | 6286 | type = __nft_flowtable_type_get(family); |
3b49e2e9 PNA |
6287 | if (type != NULL && try_module_get(type->owner)) |
6288 | return type; | |
6289 | ||
f102d66b | 6290 | lockdep_nfnl_nft_mutex_not_held(); |
3b49e2e9 PNA |
6291 | #ifdef CONFIG_MODULES |
6292 | if (type == NULL) { | |
eb014de4 | 6293 | if (nft_request_module(net, "nf-flowtable-%u", family) == -EAGAIN) |
3b49e2e9 PNA |
6294 | return ERR_PTR(-EAGAIN); |
6295 | } | |
6296 | #endif | |
6297 | return ERR_PTR(-ENOENT); | |
6298 | } | |
6299 | ||
5acab914 | 6300 | /* Only called from error and netdev event paths. */ |
ff4bf2f4 PNA |
6301 | static void nft_unregister_flowtable_hook(struct net *net, |
6302 | struct nft_flowtable *flowtable, | |
6303 | struct nft_hook *hook) | |
6304 | { | |
6305 | nf_unregister_net_hook(net, &hook->ops); | |
6306 | flowtable->data.type->setup(&flowtable->data, hook->ops.dev, | |
6307 | FLOW_BLOCK_UNBIND); | |
6308 | } | |
6309 | ||
3b49e2e9 | 6310 | static void nft_unregister_flowtable_net_hooks(struct net *net, |
f9382669 | 6311 | struct list_head *hook_list) |
3b49e2e9 | 6312 | { |
3f0465a9 | 6313 | struct nft_hook *hook; |
3b49e2e9 | 6314 | |
f9382669 | 6315 | list_for_each_entry(hook, hook_list, list) |
5acab914 | 6316 | nf_unregister_net_hook(net, &hook->ops); |
3f0465a9 PNA |
6317 | } |
6318 | ||
6319 | static int nft_register_flowtable_net_hooks(struct net *net, | |
6320 | struct nft_table *table, | |
f9382669 | 6321 | struct list_head *hook_list, |
3f0465a9 PNA |
6322 | struct nft_flowtable *flowtable) |
6323 | { | |
6324 | struct nft_hook *hook, *hook2, *next; | |
6325 | struct nft_flowtable *ft; | |
6326 | int err, i = 0; | |
6327 | ||
f9382669 | 6328 | list_for_each_entry(hook, hook_list, list) { |
3f0465a9 PNA |
6329 | list_for_each_entry(ft, &table->flowtables, list) { |
6330 | list_for_each_entry(hook2, &ft->hook_list, list) { | |
6331 | if (hook->ops.dev == hook2->ops.dev && | |
6332 | hook->ops.pf == hook2->ops.pf) { | |
6333 | err = -EBUSY; | |
6334 | goto err_unregister_net_hooks; | |
6335 | } | |
6336 | } | |
6337 | } | |
3b49e2e9 | 6338 | |
d7c03a9f | 6339 | err = flowtable->data.type->setup(&flowtable->data, |
6340 | hook->ops.dev, | |
6341 | FLOW_BLOCK_BIND); | |
3f0465a9 PNA |
6342 | if (err < 0) |
6343 | goto err_unregister_net_hooks; | |
6344 | ||
d7c03a9f | 6345 | err = nf_register_net_hook(net, &hook->ops); |
6346 | if (err < 0) { | |
6347 | flowtable->data.type->setup(&flowtable->data, | |
6348 | hook->ops.dev, | |
6349 | FLOW_BLOCK_UNBIND); | |
6350 | goto err_unregister_net_hooks; | |
6351 | } | |
6352 | ||
3f0465a9 | 6353 | i++; |
3b49e2e9 | 6354 | } |
3f0465a9 PNA |
6355 | |
6356 | return 0; | |
6357 | ||
6358 | err_unregister_net_hooks: | |
f9382669 | 6359 | list_for_each_entry_safe(hook, next, hook_list, list) { |
3f0465a9 PNA |
6360 | if (i-- <= 0) |
6361 | break; | |
6362 | ||
ff4bf2f4 | 6363 | nft_unregister_flowtable_hook(net, flowtable, hook); |
3f0465a9 PNA |
6364 | list_del_rcu(&hook->list); |
6365 | kfree_rcu(hook, rcu); | |
6366 | } | |
6367 | ||
6368 | return err; | |
3b49e2e9 PNA |
6369 | } |
6370 | ||
389a2cbc PNA |
6371 | static void nft_flowtable_hooks_destroy(struct list_head *hook_list) |
6372 | { | |
6373 | struct nft_hook *hook, *next; | |
6374 | ||
6375 | list_for_each_entry_safe(hook, next, hook_list, list) { | |
6376 | list_del_rcu(&hook->list); | |
6377 | kfree_rcu(hook, rcu); | |
6378 | } | |
6379 | } | |
6380 | ||
78d9f48f PNA |
6381 | static int nft_flowtable_update(struct nft_ctx *ctx, const struct nlmsghdr *nlh, |
6382 | struct nft_flowtable *flowtable) | |
6383 | { | |
6384 | const struct nlattr * const *nla = ctx->nla; | |
6385 | struct nft_flowtable_hook flowtable_hook; | |
6386 | struct nft_hook *hook, *next; | |
6387 | struct nft_trans *trans; | |
6388 | bool unregister = false; | |
6389 | int err; | |
6390 | ||
6391 | err = nft_flowtable_parse_hook(ctx, nla[NFTA_FLOWTABLE_HOOK], | |
5b6743fb | 6392 | &flowtable_hook, flowtable, false); |
78d9f48f PNA |
6393 | if (err < 0) |
6394 | return err; | |
6395 | ||
6396 | list_for_each_entry_safe(hook, next, &flowtable_hook.list, list) { | |
6397 | if (nft_hook_list_find(&flowtable->hook_list, hook)) { | |
6398 | list_del(&hook->list); | |
6399 | kfree(hook); | |
6400 | } | |
6401 | } | |
6402 | ||
6403 | err = nft_register_flowtable_net_hooks(ctx->net, ctx->table, | |
6404 | &flowtable_hook.list, flowtable); | |
6405 | if (err < 0) | |
6406 | goto err_flowtable_update_hook; | |
6407 | ||
6408 | trans = nft_trans_alloc(ctx, NFT_MSG_NEWFLOWTABLE, | |
6409 | sizeof(struct nft_trans_flowtable)); | |
6410 | if (!trans) { | |
6411 | unregister = true; | |
6412 | err = -ENOMEM; | |
6413 | goto err_flowtable_update_hook; | |
6414 | } | |
6415 | ||
6416 | nft_trans_flowtable(trans) = flowtable; | |
6417 | nft_trans_flowtable_update(trans) = true; | |
6418 | INIT_LIST_HEAD(&nft_trans_flowtable_hooks(trans)); | |
6419 | list_splice(&flowtable_hook.list, &nft_trans_flowtable_hooks(trans)); | |
6420 | ||
6421 | list_add_tail(&trans->list, &ctx->net->nft.commit_list); | |
6422 | ||
6423 | return 0; | |
6424 | ||
6425 | err_flowtable_update_hook: | |
6426 | list_for_each_entry_safe(hook, next, &flowtable_hook.list, list) { | |
6427 | if (unregister) | |
6428 | nft_unregister_flowtable_hook(ctx->net, flowtable, hook); | |
6429 | list_del_rcu(&hook->list); | |
6430 | kfree_rcu(hook, rcu); | |
6431 | } | |
6432 | ||
6433 | return err; | |
6434 | ||
6435 | } | |
6436 | ||
3b49e2e9 PNA |
6437 | static int nf_tables_newflowtable(struct net *net, struct sock *nlsk, |
6438 | struct sk_buff *skb, | |
6439 | const struct nlmsghdr *nlh, | |
6440 | const struct nlattr * const nla[], | |
6441 | struct netlink_ext_ack *extack) | |
6442 | { | |
6443 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
d9246a53 | 6444 | struct nft_flowtable_hook flowtable_hook; |
3b49e2e9 PNA |
6445 | const struct nf_flowtable_type *type; |
6446 | u8 genmask = nft_genmask_next(net); | |
6447 | int family = nfmsg->nfgen_family; | |
3f0465a9 PNA |
6448 | struct nft_flowtable *flowtable; |
6449 | struct nft_hook *hook, *next; | |
3b49e2e9 PNA |
6450 | struct nft_table *table; |
6451 | struct nft_ctx ctx; | |
3f0465a9 | 6452 | int err; |
3b49e2e9 PNA |
6453 | |
6454 | if (!nla[NFTA_FLOWTABLE_TABLE] || | |
6455 | !nla[NFTA_FLOWTABLE_NAME] || | |
6456 | !nla[NFTA_FLOWTABLE_HOOK]) | |
6457 | return -EINVAL; | |
6458 | ||
cac20fcd PNA |
6459 | table = nft_table_lookup(net, nla[NFTA_FLOWTABLE_TABLE], family, |
6460 | genmask); | |
36dd1bcc PNA |
6461 | if (IS_ERR(table)) { |
6462 | NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_TABLE]); | |
3b49e2e9 | 6463 | return PTR_ERR(table); |
36dd1bcc | 6464 | } |
3b49e2e9 | 6465 | |
cac20fcd PNA |
6466 | flowtable = nft_flowtable_lookup(table, nla[NFTA_FLOWTABLE_NAME], |
6467 | genmask); | |
3b49e2e9 PNA |
6468 | if (IS_ERR(flowtable)) { |
6469 | err = PTR_ERR(flowtable); | |
36dd1bcc PNA |
6470 | if (err != -ENOENT) { |
6471 | NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_NAME]); | |
3b49e2e9 | 6472 | return err; |
36dd1bcc | 6473 | } |
3b49e2e9 | 6474 | } else { |
36dd1bcc PNA |
6475 | if (nlh->nlmsg_flags & NLM_F_EXCL) { |
6476 | NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_NAME]); | |
3b49e2e9 | 6477 | return -EEXIST; |
36dd1bcc | 6478 | } |
3b49e2e9 | 6479 | |
78d9f48f PNA |
6480 | nft_ctx_init(&ctx, net, skb, nlh, family, table, NULL, nla); |
6481 | ||
6482 | return nft_flowtable_update(&ctx, nlh, flowtable); | |
3b49e2e9 PNA |
6483 | } |
6484 | ||
98319cb9 | 6485 | nft_ctx_init(&ctx, net, skb, nlh, family, table, NULL, nla); |
3b49e2e9 PNA |
6486 | |
6487 | flowtable = kzalloc(sizeof(*flowtable), GFP_KERNEL); | |
6488 | if (!flowtable) | |
6489 | return -ENOMEM; | |
6490 | ||
6491 | flowtable->table = table; | |
3ecbfd65 | 6492 | flowtable->handle = nf_tables_alloc_handle(table); |
3f0465a9 | 6493 | INIT_LIST_HEAD(&flowtable->hook_list); |
3ecbfd65 | 6494 | |
3b49e2e9 PNA |
6495 | flowtable->name = nla_strdup(nla[NFTA_FLOWTABLE_NAME], GFP_KERNEL); |
6496 | if (!flowtable->name) { | |
6497 | err = -ENOMEM; | |
6498 | goto err1; | |
6499 | } | |
6500 | ||
452238e8 | 6501 | type = nft_flowtable_type_get(net, family); |
3b49e2e9 PNA |
6502 | if (IS_ERR(type)) { |
6503 | err = PTR_ERR(type); | |
6504 | goto err2; | |
6505 | } | |
6506 | ||
8bb69f3b PNA |
6507 | if (nla[NFTA_FLOWTABLE_FLAGS]) { |
6508 | flowtable->data.flags = | |
6509 | ntohl(nla_get_be32(nla[NFTA_FLOWTABLE_FLAGS])); | |
cfbd1125 | 6510 | if (flowtable->data.flags & ~NFT_FLOWTABLE_MASK) |
8bb69f3b PNA |
6511 | goto err3; |
6512 | } | |
6513 | ||
6514 | write_pnet(&flowtable->data.net, net); | |
3b49e2e9 | 6515 | flowtable->data.type = type; |
a268de77 | 6516 | err = type->init(&flowtable->data); |
3b49e2e9 PNA |
6517 | if (err < 0) |
6518 | goto err3; | |
6519 | ||
d9246a53 | 6520 | err = nft_flowtable_parse_hook(&ctx, nla[NFTA_FLOWTABLE_HOOK], |
5b6743fb | 6521 | &flowtable_hook, flowtable, true); |
3b49e2e9 | 6522 | if (err < 0) |
a268de77 | 6523 | goto err4; |
3b49e2e9 | 6524 | |
d9246a53 PNA |
6525 | list_splice(&flowtable_hook.list, &flowtable->hook_list); |
6526 | flowtable->data.priority = flowtable_hook.priority; | |
6527 | flowtable->hooknum = flowtable_hook.num; | |
6528 | ||
f9382669 PNA |
6529 | err = nft_register_flowtable_net_hooks(ctx.net, table, |
6530 | &flowtable->hook_list, | |
6531 | flowtable); | |
2d285f26 | 6532 | if (err < 0) { |
389a2cbc | 6533 | nft_flowtable_hooks_destroy(&flowtable->hook_list); |
3f0465a9 | 6534 | goto err4; |
2d285f26 | 6535 | } |
3b49e2e9 PNA |
6536 | |
6537 | err = nft_trans_flowtable_add(&ctx, NFT_MSG_NEWFLOWTABLE, flowtable); | |
6538 | if (err < 0) | |
3f0465a9 | 6539 | goto err5; |
3b49e2e9 PNA |
6540 | |
6541 | list_add_tail_rcu(&flowtable->list, &table->flowtables); | |
6542 | table->use++; | |
6543 | ||
6544 | return 0; | |
a268de77 | 6545 | err5: |
3f0465a9 | 6546 | list_for_each_entry_safe(hook, next, &flowtable->hook_list, list) { |
ff4bf2f4 | 6547 | nft_unregister_flowtable_hook(net, flowtable, hook); |
3f0465a9 PNA |
6548 | list_del_rcu(&hook->list); |
6549 | kfree_rcu(hook, rcu); | |
6550 | } | |
a268de77 FF |
6551 | err4: |
6552 | flowtable->data.type->free(&flowtable->data); | |
3b49e2e9 PNA |
6553 | err3: |
6554 | module_put(type->owner); | |
6555 | err2: | |
6556 | kfree(flowtable->name); | |
6557 | err1: | |
6558 | kfree(flowtable); | |
6559 | return err; | |
6560 | } | |
6561 | ||
3003055f PNA |
6562 | static void nft_flowtable_hook_release(struct nft_flowtable_hook *flowtable_hook) |
6563 | { | |
6564 | struct nft_hook *this, *next; | |
6565 | ||
6566 | list_for_each_entry_safe(this, next, &flowtable_hook->list, list) { | |
6567 | list_del(&this->list); | |
6568 | kfree(this); | |
6569 | } | |
6570 | } | |
6571 | ||
abadb2f8 PNA |
6572 | static int nft_delflowtable_hook(struct nft_ctx *ctx, |
6573 | struct nft_flowtable *flowtable) | |
6574 | { | |
6575 | const struct nlattr * const *nla = ctx->nla; | |
6576 | struct nft_flowtable_hook flowtable_hook; | |
3003055f | 6577 | struct nft_hook *this, *hook; |
abadb2f8 PNA |
6578 | struct nft_trans *trans; |
6579 | int err; | |
6580 | ||
6581 | err = nft_flowtable_parse_hook(ctx, nla[NFTA_FLOWTABLE_HOOK], | |
5b6743fb | 6582 | &flowtable_hook, flowtable, false); |
abadb2f8 PNA |
6583 | if (err < 0) |
6584 | return err; | |
6585 | ||
3003055f | 6586 | list_for_each_entry(this, &flowtable_hook.list, list) { |
abadb2f8 PNA |
6587 | hook = nft_hook_list_find(&flowtable->hook_list, this); |
6588 | if (!hook) { | |
6589 | err = -ENOENT; | |
6590 | goto err_flowtable_del_hook; | |
6591 | } | |
6592 | hook->inactive = true; | |
abadb2f8 PNA |
6593 | } |
6594 | ||
6595 | trans = nft_trans_alloc(ctx, NFT_MSG_DELFLOWTABLE, | |
6596 | sizeof(struct nft_trans_flowtable)); | |
3003055f PNA |
6597 | if (!trans) { |
6598 | err = -ENOMEM; | |
6599 | goto err_flowtable_del_hook; | |
6600 | } | |
abadb2f8 PNA |
6601 | |
6602 | nft_trans_flowtable(trans) = flowtable; | |
6603 | nft_trans_flowtable_update(trans) = true; | |
6604 | INIT_LIST_HEAD(&nft_trans_flowtable_hooks(trans)); | |
3003055f | 6605 | nft_flowtable_hook_release(&flowtable_hook); |
abadb2f8 PNA |
6606 | |
6607 | list_add_tail(&trans->list, &ctx->net->nft.commit_list); | |
6608 | ||
6609 | return 0; | |
6610 | ||
6611 | err_flowtable_del_hook: | |
3003055f PNA |
6612 | list_for_each_entry(this, &flowtable_hook.list, list) { |
6613 | hook = nft_hook_list_find(&flowtable->hook_list, this); | |
6614 | if (!hook) | |
6615 | break; | |
6616 | ||
abadb2f8 | 6617 | hook->inactive = false; |
3003055f PNA |
6618 | } |
6619 | nft_flowtable_hook_release(&flowtable_hook); | |
abadb2f8 PNA |
6620 | |
6621 | return err; | |
6622 | } | |
6623 | ||
3b49e2e9 PNA |
6624 | static int nf_tables_delflowtable(struct net *net, struct sock *nlsk, |
6625 | struct sk_buff *skb, | |
6626 | const struct nlmsghdr *nlh, | |
6627 | const struct nlattr * const nla[], | |
6628 | struct netlink_ext_ack *extack) | |
6629 | { | |
6630 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
6631 | u8 genmask = nft_genmask_next(net); | |
6632 | int family = nfmsg->nfgen_family; | |
6633 | struct nft_flowtable *flowtable; | |
36dd1bcc | 6634 | const struct nlattr *attr; |
3b49e2e9 PNA |
6635 | struct nft_table *table; |
6636 | struct nft_ctx ctx; | |
6637 | ||
e603ea4b PNA |
6638 | if (!nla[NFTA_FLOWTABLE_TABLE] || |
6639 | (!nla[NFTA_FLOWTABLE_NAME] && | |
6640 | !nla[NFTA_FLOWTABLE_HANDLE])) | |
6641 | return -EINVAL; | |
6642 | ||
cac20fcd PNA |
6643 | table = nft_table_lookup(net, nla[NFTA_FLOWTABLE_TABLE], family, |
6644 | genmask); | |
36dd1bcc PNA |
6645 | if (IS_ERR(table)) { |
6646 | NL_SET_BAD_ATTR(extack, nla[NFTA_FLOWTABLE_TABLE]); | |
3b49e2e9 | 6647 | return PTR_ERR(table); |
36dd1bcc | 6648 | } |
3b49e2e9 | 6649 | |
36dd1bcc PNA |
6650 | if (nla[NFTA_FLOWTABLE_HANDLE]) { |
6651 | attr = nla[NFTA_FLOWTABLE_HANDLE]; | |
6652 | flowtable = nft_flowtable_lookup_byhandle(table, attr, genmask); | |
6653 | } else { | |
6654 | attr = nla[NFTA_FLOWTABLE_NAME]; | |
6655 | flowtable = nft_flowtable_lookup(table, attr, genmask); | |
6656 | } | |
6657 | ||
6658 | if (IS_ERR(flowtable)) { | |
6659 | NL_SET_BAD_ATTR(extack, attr); | |
6660 | return PTR_ERR(flowtable); | |
6661 | } | |
abadb2f8 PNA |
6662 | |
6663 | nft_ctx_init(&ctx, net, skb, nlh, family, table, NULL, nla); | |
6664 | ||
6665 | if (nla[NFTA_FLOWTABLE_HOOK]) | |
6666 | return nft_delflowtable_hook(&ctx, flowtable); | |
6667 | ||
36dd1bcc PNA |
6668 | if (flowtable->use > 0) { |
6669 | NL_SET_BAD_ATTR(extack, attr); | |
3b49e2e9 | 6670 | return -EBUSY; |
36dd1bcc | 6671 | } |
3b49e2e9 | 6672 | |
3b49e2e9 PNA |
6673 | return nft_delflowtable(&ctx, flowtable); |
6674 | } | |
6675 | ||
6676 | static int nf_tables_fill_flowtable_info(struct sk_buff *skb, struct net *net, | |
6677 | u32 portid, u32 seq, int event, | |
6678 | u32 flags, int family, | |
c42d8bda PNA |
6679 | struct nft_flowtable *flowtable, |
6680 | struct list_head *hook_list) | |
3b49e2e9 PNA |
6681 | { |
6682 | struct nlattr *nest, *nest_devs; | |
6683 | struct nfgenmsg *nfmsg; | |
3f0465a9 | 6684 | struct nft_hook *hook; |
3b49e2e9 | 6685 | struct nlmsghdr *nlh; |
3b49e2e9 PNA |
6686 | |
6687 | event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, event); | |
6688 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags); | |
6689 | if (nlh == NULL) | |
6690 | goto nla_put_failure; | |
6691 | ||
6692 | nfmsg = nlmsg_data(nlh); | |
6693 | nfmsg->nfgen_family = family; | |
6694 | nfmsg->version = NFNETLINK_V0; | |
6695 | nfmsg->res_id = htons(net->nft.base_seq & 0xffff); | |
6696 | ||
6697 | if (nla_put_string(skb, NFTA_FLOWTABLE_TABLE, flowtable->table->name) || | |
6698 | nla_put_string(skb, NFTA_FLOWTABLE_NAME, flowtable->name) || | |
3ecbfd65 HS |
6699 | nla_put_be32(skb, NFTA_FLOWTABLE_USE, htonl(flowtable->use)) || |
6700 | nla_put_be64(skb, NFTA_FLOWTABLE_HANDLE, cpu_to_be64(flowtable->handle), | |
8bb69f3b PNA |
6701 | NFTA_FLOWTABLE_PAD) || |
6702 | nla_put_be32(skb, NFTA_FLOWTABLE_FLAGS, htonl(flowtable->data.flags))) | |
3b49e2e9 PNA |
6703 | goto nla_put_failure; |
6704 | ||
ae0be8de | 6705 | nest = nla_nest_start_noflag(skb, NFTA_FLOWTABLE_HOOK); |
eb895086 KL |
6706 | if (!nest) |
6707 | goto nla_put_failure; | |
3b49e2e9 | 6708 | if (nla_put_be32(skb, NFTA_FLOWTABLE_HOOK_NUM, htonl(flowtable->hooknum)) || |
71a8a63b | 6709 | nla_put_be32(skb, NFTA_FLOWTABLE_HOOK_PRIORITY, htonl(flowtable->data.priority))) |
3b49e2e9 PNA |
6710 | goto nla_put_failure; |
6711 | ||
ae0be8de | 6712 | nest_devs = nla_nest_start_noflag(skb, NFTA_FLOWTABLE_HOOK_DEVS); |
3b49e2e9 PNA |
6713 | if (!nest_devs) |
6714 | goto nla_put_failure; | |
6715 | ||
c42d8bda | 6716 | list_for_each_entry_rcu(hook, hook_list, list) { |
3f0465a9 | 6717 | if (nla_put_string(skb, NFTA_DEVICE_NAME, hook->ops.dev->name)) |
3b49e2e9 PNA |
6718 | goto nla_put_failure; |
6719 | } | |
6720 | nla_nest_end(skb, nest_devs); | |
6721 | nla_nest_end(skb, nest); | |
6722 | ||
6723 | nlmsg_end(skb, nlh); | |
6724 | return 0; | |
6725 | ||
6726 | nla_put_failure: | |
6727 | nlmsg_trim(skb, nlh); | |
6728 | return -1; | |
6729 | } | |
6730 | ||
6731 | struct nft_flowtable_filter { | |
6732 | char *table; | |
6733 | }; | |
6734 | ||
6735 | static int nf_tables_dump_flowtable(struct sk_buff *skb, | |
6736 | struct netlink_callback *cb) | |
6737 | { | |
6738 | const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh); | |
6739 | struct nft_flowtable_filter *filter = cb->data; | |
6740 | unsigned int idx = 0, s_idx = cb->args[0]; | |
6741 | struct net *net = sock_net(skb->sk); | |
6742 | int family = nfmsg->nfgen_family; | |
6743 | struct nft_flowtable *flowtable; | |
3b49e2e9 PNA |
6744 | const struct nft_table *table; |
6745 | ||
6746 | rcu_read_lock(); | |
6747 | cb->seq = net->nft.base_seq; | |
6748 | ||
36596dad | 6749 | list_for_each_entry_rcu(table, &net->nft.tables, list) { |
98319cb9 | 6750 | if (family != NFPROTO_UNSPEC && family != table->family) |
3b49e2e9 PNA |
6751 | continue; |
6752 | ||
36596dad PNA |
6753 | list_for_each_entry_rcu(flowtable, &table->flowtables, list) { |
6754 | if (!nft_is_active(net, flowtable)) | |
6755 | goto cont; | |
6756 | if (idx < s_idx) | |
6757 | goto cont; | |
6758 | if (idx > s_idx) | |
6759 | memset(&cb->args[1], 0, | |
6760 | sizeof(cb->args) - sizeof(cb->args[0])); | |
360cc79d | 6761 | if (filter && filter->table && |
36596dad PNA |
6762 | strcmp(filter->table, table->name)) |
6763 | goto cont; | |
3b49e2e9 | 6764 | |
36596dad PNA |
6765 | if (nf_tables_fill_flowtable_info(skb, net, NETLINK_CB(cb->skb).portid, |
6766 | cb->nlh->nlmsg_seq, | |
6767 | NFT_MSG_NEWFLOWTABLE, | |
6768 | NLM_F_MULTI | NLM_F_APPEND, | |
c42d8bda PNA |
6769 | table->family, |
6770 | flowtable, | |
6771 | &flowtable->hook_list) < 0) | |
36596dad | 6772 | goto done; |
3b49e2e9 | 6773 | |
36596dad | 6774 | nl_dump_check_consistent(cb, nlmsg_hdr(skb)); |
3b49e2e9 | 6775 | cont: |
36596dad | 6776 | idx++; |
3b49e2e9 PNA |
6777 | } |
6778 | } | |
6779 | done: | |
6780 | rcu_read_unlock(); | |
6781 | ||
6782 | cb->args[0] = idx; | |
6783 | return skb->len; | |
6784 | } | |
6785 | ||
90fd131a | 6786 | static int nf_tables_dump_flowtable_start(struct netlink_callback *cb) |
3b49e2e9 | 6787 | { |
90fd131a FW |
6788 | const struct nlattr * const *nla = cb->data; |
6789 | struct nft_flowtable_filter *filter = NULL; | |
3b49e2e9 | 6790 | |
90fd131a FW |
6791 | if (nla[NFTA_FLOWTABLE_TABLE]) { |
6792 | filter = kzalloc(sizeof(*filter), GFP_ATOMIC); | |
6793 | if (!filter) | |
6794 | return -ENOMEM; | |
3b49e2e9 | 6795 | |
90fd131a FW |
6796 | filter->table = nla_strdup(nla[NFTA_FLOWTABLE_TABLE], |
6797 | GFP_ATOMIC); | |
6798 | if (!filter->table) { | |
6799 | kfree(filter); | |
6800 | return -ENOMEM; | |
6801 | } | |
6802 | } | |
3b49e2e9 | 6803 | |
90fd131a | 6804 | cb->data = filter; |
3b49e2e9 PNA |
6805 | return 0; |
6806 | } | |
6807 | ||
90fd131a | 6808 | static int nf_tables_dump_flowtable_done(struct netlink_callback *cb) |
3b49e2e9 | 6809 | { |
90fd131a | 6810 | struct nft_flowtable_filter *filter = cb->data; |
3b49e2e9 | 6811 | |
3b49e2e9 | 6812 | if (!filter) |
90fd131a | 6813 | return 0; |
3b49e2e9 | 6814 | |
90fd131a FW |
6815 | kfree(filter->table); |
6816 | kfree(filter); | |
6817 | ||
6818 | return 0; | |
3b49e2e9 PNA |
6819 | } |
6820 | ||
d9adf22a | 6821 | /* called with rcu_read_lock held */ |
3b49e2e9 PNA |
6822 | static int nf_tables_getflowtable(struct net *net, struct sock *nlsk, |
6823 | struct sk_buff *skb, | |
6824 | const struct nlmsghdr *nlh, | |
6825 | const struct nlattr * const nla[], | |
6826 | struct netlink_ext_ack *extack) | |
6827 | { | |
6828 | const struct nfgenmsg *nfmsg = nlmsg_data(nlh); | |
6829 | u8 genmask = nft_genmask_cur(net); | |
6830 | int family = nfmsg->nfgen_family; | |
6831 | struct nft_flowtable *flowtable; | |
3b49e2e9 PNA |
6832 | const struct nft_table *table; |
6833 | struct sk_buff *skb2; | |
6834 | int err; | |
6835 | ||
6836 | if (nlh->nlmsg_flags & NLM_F_DUMP) { | |
6837 | struct netlink_dump_control c = { | |
90fd131a | 6838 | .start = nf_tables_dump_flowtable_start, |
3b49e2e9 PNA |
6839 | .dump = nf_tables_dump_flowtable, |
6840 | .done = nf_tables_dump_flowtable_done, | |
d9adf22a | 6841 | .module = THIS_MODULE, |
90fd131a | 6842 | .data = (void *)nla, |
3b49e2e9 PNA |
6843 | }; |
6844 | ||
d9adf22a | 6845 | return nft_netlink_dump_start_rcu(nlsk, skb, nlh, &c); |
3b49e2e9 PNA |
6846 | } |
6847 | ||
6848 | if (!nla[NFTA_FLOWTABLE_NAME]) | |
6849 | return -EINVAL; | |
6850 | ||
cac20fcd PNA |
6851 | table = nft_table_lookup(net, nla[NFTA_FLOWTABLE_TABLE], family, |
6852 | genmask); | |
3b49e2e9 PNA |
6853 | if (IS_ERR(table)) |
6854 | return PTR_ERR(table); | |
6855 | ||
cac20fcd PNA |
6856 | flowtable = nft_flowtable_lookup(table, nla[NFTA_FLOWTABLE_NAME], |
6857 | genmask); | |
03a0120f | 6858 | if (IS_ERR(flowtable)) |
3b49e2e9 PNA |
6859 | return PTR_ERR(flowtable); |
6860 | ||
d9adf22a | 6861 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
3b49e2e9 PNA |
6862 | if (!skb2) |
6863 | return -ENOMEM; | |
6864 | ||
6865 | err = nf_tables_fill_flowtable_info(skb2, net, NETLINK_CB(skb).portid, | |
6866 | nlh->nlmsg_seq, | |
6867 | NFT_MSG_NEWFLOWTABLE, 0, family, | |
c42d8bda | 6868 | flowtable, &flowtable->hook_list); |
3b49e2e9 PNA |
6869 | if (err < 0) |
6870 | goto err; | |
6871 | ||
6872 | return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid); | |
6873 | err: | |
6874 | kfree_skb(skb2); | |
6875 | return err; | |
6876 | } | |
6877 | ||
6878 | static void nf_tables_flowtable_notify(struct nft_ctx *ctx, | |
6879 | struct nft_flowtable *flowtable, | |
c42d8bda | 6880 | struct list_head *hook_list, |
3b49e2e9 PNA |
6881 | int event) |
6882 | { | |
6883 | struct sk_buff *skb; | |
6884 | int err; | |
6885 | ||
6886 | if (ctx->report && | |
6887 | !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES)) | |
6888 | return; | |
6889 | ||
6890 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); | |
6891 | if (skb == NULL) | |
6892 | goto err; | |
6893 | ||
6894 | err = nf_tables_fill_flowtable_info(skb, ctx->net, ctx->portid, | |
6895 | ctx->seq, event, 0, | |
c42d8bda | 6896 | ctx->family, flowtable, hook_list); |
3b49e2e9 PNA |
6897 | if (err < 0) { |
6898 | kfree_skb(skb); | |
6899 | goto err; | |
6900 | } | |
6901 | ||
6902 | nfnetlink_send(skb, ctx->net, ctx->portid, NFNLGRP_NFTABLES, | |
6903 | ctx->report, GFP_KERNEL); | |
6904 | return; | |
6905 | err: | |
6906 | nfnetlink_set_err(ctx->net, ctx->portid, NFNLGRP_NFTABLES, -ENOBUFS); | |
6907 | } | |
6908 | ||
3b49e2e9 PNA |
6909 | static void nf_tables_flowtable_destroy(struct nft_flowtable *flowtable) |
6910 | { | |
3f0465a9 PNA |
6911 | struct nft_hook *hook, *next; |
6912 | ||
5acab914 | 6913 | flowtable->data.type->free(&flowtable->data); |
3f0465a9 | 6914 | list_for_each_entry_safe(hook, next, &flowtable->hook_list, list) { |
5acab914 PNA |
6915 | flowtable->data.type->setup(&flowtable->data, hook->ops.dev, |
6916 | FLOW_BLOCK_UNBIND); | |
3f0465a9 PNA |
6917 | list_del_rcu(&hook->list); |
6918 | kfree(hook); | |
6919 | } | |
3b49e2e9 | 6920 | kfree(flowtable->name); |
3b49e2e9 | 6921 | module_put(flowtable->data.type->owner); |
a12486eb | 6922 | kfree(flowtable); |
3b49e2e9 PNA |
6923 | } |
6924 | ||
84d7fce6 PNA |
6925 | static int nf_tables_fill_gen_info(struct sk_buff *skb, struct net *net, |
6926 | u32 portid, u32 seq) | |
6927 | { | |
6928 | struct nlmsghdr *nlh; | |
6929 | struct nfgenmsg *nfmsg; | |
784b4e61 | 6930 | char buf[TASK_COMM_LEN]; |
dedb67c4 | 6931 | int event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, NFT_MSG_NEWGEN); |
84d7fce6 PNA |
6932 | |
6933 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), 0); | |
6934 | if (nlh == NULL) | |
6935 | goto nla_put_failure; | |
6936 | ||
6937 | nfmsg = nlmsg_data(nlh); | |
6938 | nfmsg->nfgen_family = AF_UNSPEC; | |
6939 | nfmsg->version = NFNETLINK_V0; | |
6940 | nfmsg->res_id = htons(net->nft.base_seq & 0xffff); | |
6941 | ||
784b4e61 PS |
6942 | if (nla_put_be32(skb, NFTA_GEN_ID, htonl(net->nft.base_seq)) || |
6943 | nla_put_be32(skb, NFTA_GEN_PROC_PID, htonl(task_pid_nr(current))) || | |
6944 | nla_put_string(skb, NFTA_GEN_PROC_NAME, get_task_comm(buf, current))) | |
84d7fce6 PNA |
6945 | goto nla_put_failure; |
6946 | ||
053c095a JB |
6947 | nlmsg_end(skb, nlh); |
6948 | return 0; | |
84d7fce6 PNA |
6949 | |
6950 | nla_put_failure: | |
6951 | nlmsg_trim(skb, nlh); | |
6952 | return -EMSGSIZE; | |
6953 | } | |
6954 | ||
3b49e2e9 PNA |
6955 | static void nft_flowtable_event(unsigned long event, struct net_device *dev, |
6956 | struct nft_flowtable *flowtable) | |
6957 | { | |
3f0465a9 | 6958 | struct nft_hook *hook; |
3b49e2e9 | 6959 | |
3f0465a9 PNA |
6960 | list_for_each_entry(hook, &flowtable->hook_list, list) { |
6961 | if (hook->ops.dev != dev) | |
3b49e2e9 PNA |
6962 | continue; |
6963 | ||
5acab914 | 6964 | /* flow_offload_netdev_event() cleans up entries for us. */ |
ff4bf2f4 | 6965 | nft_unregister_flowtable_hook(dev_net(dev), flowtable, hook); |
3f0465a9 PNA |
6966 | list_del_rcu(&hook->list); |
6967 | kfree_rcu(hook, rcu); | |
3b49e2e9 PNA |
6968 | break; |
6969 | } | |
6970 | } | |
6971 | ||
6972 | static int nf_tables_flowtable_event(struct notifier_block *this, | |
6973 | unsigned long event, void *ptr) | |
6974 | { | |
6975 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); | |
6976 | struct nft_flowtable *flowtable; | |
6977 | struct nft_table *table; | |
0a2cf5ee | 6978 | struct net *net; |
3b49e2e9 PNA |
6979 | |
6980 | if (event != NETDEV_UNREGISTER) | |
6981 | return 0; | |
6982 | ||
6a48de01 | 6983 | net = dev_net(dev); |
9e619d87 | 6984 | mutex_lock(&net->nft.commit_mutex); |
0a2cf5ee | 6985 | list_for_each_entry(table, &net->nft.tables, list) { |
36596dad PNA |
6986 | list_for_each_entry(flowtable, &table->flowtables, list) { |
6987 | nft_flowtable_event(event, dev, flowtable); | |
3b49e2e9 PNA |
6988 | } |
6989 | } | |
9e619d87 | 6990 | mutex_unlock(&net->nft.commit_mutex); |
6a48de01 | 6991 | |
3b49e2e9 PNA |
6992 | return NOTIFY_DONE; |
6993 | } | |
6994 | ||
6995 | static struct notifier_block nf_tables_flowtable_notifier = { | |
6996 | .notifier_call = nf_tables_flowtable_event, | |
6997 | }; | |
6998 | ||
25e94a99 PNA |
6999 | static void nf_tables_gen_notify(struct net *net, struct sk_buff *skb, |
7000 | int event) | |
84d7fce6 PNA |
7001 | { |
7002 | struct nlmsghdr *nlh = nlmsg_hdr(skb); | |
7003 | struct sk_buff *skb2; | |
7004 | int err; | |
7005 | ||
7006 | if (nlmsg_report(nlh) && | |
7007 | !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES)) | |
25e94a99 | 7008 | return; |
84d7fce6 | 7009 | |
84d7fce6 PNA |
7010 | skb2 = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
7011 | if (skb2 == NULL) | |
7012 | goto err; | |
7013 | ||
7014 | err = nf_tables_fill_gen_info(skb2, net, NETLINK_CB(skb).portid, | |
7015 | nlh->nlmsg_seq); | |
7016 | if (err < 0) { | |
7017 | kfree_skb(skb2); | |
7018 | goto err; | |
7019 | } | |
7020 | ||
25e94a99 PNA |
7021 | nfnetlink_send(skb2, net, NETLINK_CB(skb).portid, NFNLGRP_NFTABLES, |
7022 | nlmsg_report(nlh), GFP_KERNEL); | |
7023 | return; | |
84d7fce6 | 7024 | err: |
25e94a99 PNA |
7025 | nfnetlink_set_err(net, NETLINK_CB(skb).portid, NFNLGRP_NFTABLES, |
7026 | -ENOBUFS); | |
84d7fce6 PNA |
7027 | } |
7028 | ||
7b8002a1 PNA |
7029 | static int nf_tables_getgen(struct net *net, struct sock *nlsk, |
7030 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
04ba724b PNA |
7031 | const struct nlattr * const nla[], |
7032 | struct netlink_ext_ack *extack) | |
84d7fce6 | 7033 | { |
84d7fce6 PNA |
7034 | struct sk_buff *skb2; |
7035 | int err; | |
7036 | ||
d9adf22a | 7037 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); |
84d7fce6 PNA |
7038 | if (skb2 == NULL) |
7039 | return -ENOMEM; | |
7040 | ||
7041 | err = nf_tables_fill_gen_info(skb2, net, NETLINK_CB(skb).portid, | |
7042 | nlh->nlmsg_seq); | |
7043 | if (err < 0) | |
7044 | goto err; | |
7045 | ||
7046 | return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid); | |
7047 | err: | |
7048 | kfree_skb(skb2); | |
7049 | return err; | |
7050 | } | |
7051 | ||
96518518 PM |
7052 | static const struct nfnl_callback nf_tables_cb[NFT_MSG_MAX] = { |
7053 | [NFT_MSG_NEWTABLE] = { | |
55dd6f93 | 7054 | .call_batch = nf_tables_newtable, |
96518518 PM |
7055 | .attr_count = NFTA_TABLE_MAX, |
7056 | .policy = nft_table_policy, | |
7057 | }, | |
7058 | [NFT_MSG_GETTABLE] = { | |
d9adf22a | 7059 | .call_rcu = nf_tables_gettable, |
96518518 PM |
7060 | .attr_count = NFTA_TABLE_MAX, |
7061 | .policy = nft_table_policy, | |
7062 | }, | |
7063 | [NFT_MSG_DELTABLE] = { | |
55dd6f93 | 7064 | .call_batch = nf_tables_deltable, |
96518518 PM |
7065 | .attr_count = NFTA_TABLE_MAX, |
7066 | .policy = nft_table_policy, | |
7067 | }, | |
7068 | [NFT_MSG_NEWCHAIN] = { | |
91c7b38d | 7069 | .call_batch = nf_tables_newchain, |
96518518 PM |
7070 | .attr_count = NFTA_CHAIN_MAX, |
7071 | .policy = nft_chain_policy, | |
7072 | }, | |
7073 | [NFT_MSG_GETCHAIN] = { | |
d9adf22a | 7074 | .call_rcu = nf_tables_getchain, |
96518518 PM |
7075 | .attr_count = NFTA_CHAIN_MAX, |
7076 | .policy = nft_chain_policy, | |
7077 | }, | |
7078 | [NFT_MSG_DELCHAIN] = { | |
91c7b38d | 7079 | .call_batch = nf_tables_delchain, |
96518518 PM |
7080 | .attr_count = NFTA_CHAIN_MAX, |
7081 | .policy = nft_chain_policy, | |
7082 | }, | |
7083 | [NFT_MSG_NEWRULE] = { | |
0628b123 | 7084 | .call_batch = nf_tables_newrule, |
96518518 PM |
7085 | .attr_count = NFTA_RULE_MAX, |
7086 | .policy = nft_rule_policy, | |
7087 | }, | |
7088 | [NFT_MSG_GETRULE] = { | |
d9adf22a | 7089 | .call_rcu = nf_tables_getrule, |
96518518 PM |
7090 | .attr_count = NFTA_RULE_MAX, |
7091 | .policy = nft_rule_policy, | |
7092 | }, | |
7093 | [NFT_MSG_DELRULE] = { | |
0628b123 | 7094 | .call_batch = nf_tables_delrule, |
96518518 PM |
7095 | .attr_count = NFTA_RULE_MAX, |
7096 | .policy = nft_rule_policy, | |
7097 | }, | |
20a69341 | 7098 | [NFT_MSG_NEWSET] = { |
958bee14 | 7099 | .call_batch = nf_tables_newset, |
20a69341 PM |
7100 | .attr_count = NFTA_SET_MAX, |
7101 | .policy = nft_set_policy, | |
7102 | }, | |
7103 | [NFT_MSG_GETSET] = { | |
d9adf22a | 7104 | .call_rcu = nf_tables_getset, |
20a69341 PM |
7105 | .attr_count = NFTA_SET_MAX, |
7106 | .policy = nft_set_policy, | |
7107 | }, | |
7108 | [NFT_MSG_DELSET] = { | |
958bee14 | 7109 | .call_batch = nf_tables_delset, |
20a69341 PM |
7110 | .attr_count = NFTA_SET_MAX, |
7111 | .policy = nft_set_policy, | |
7112 | }, | |
7113 | [NFT_MSG_NEWSETELEM] = { | |
958bee14 | 7114 | .call_batch = nf_tables_newsetelem, |
20a69341 PM |
7115 | .attr_count = NFTA_SET_ELEM_LIST_MAX, |
7116 | .policy = nft_set_elem_list_policy, | |
7117 | }, | |
7118 | [NFT_MSG_GETSETELEM] = { | |
d9adf22a | 7119 | .call_rcu = nf_tables_getsetelem, |
20a69341 PM |
7120 | .attr_count = NFTA_SET_ELEM_LIST_MAX, |
7121 | .policy = nft_set_elem_list_policy, | |
7122 | }, | |
7123 | [NFT_MSG_DELSETELEM] = { | |
958bee14 | 7124 | .call_batch = nf_tables_delsetelem, |
20a69341 PM |
7125 | .attr_count = NFTA_SET_ELEM_LIST_MAX, |
7126 | .policy = nft_set_elem_list_policy, | |
7127 | }, | |
84d7fce6 | 7128 | [NFT_MSG_GETGEN] = { |
d9adf22a | 7129 | .call_rcu = nf_tables_getgen, |
84d7fce6 | 7130 | }, |
e5009240 PNA |
7131 | [NFT_MSG_NEWOBJ] = { |
7132 | .call_batch = nf_tables_newobj, | |
7133 | .attr_count = NFTA_OBJ_MAX, | |
7134 | .policy = nft_obj_policy, | |
7135 | }, | |
7136 | [NFT_MSG_GETOBJ] = { | |
d9adf22a | 7137 | .call_rcu = nf_tables_getobj, |
e5009240 PNA |
7138 | .attr_count = NFTA_OBJ_MAX, |
7139 | .policy = nft_obj_policy, | |
7140 | }, | |
7141 | [NFT_MSG_DELOBJ] = { | |
7142 | .call_batch = nf_tables_delobj, | |
7143 | .attr_count = NFTA_OBJ_MAX, | |
7144 | .policy = nft_obj_policy, | |
7145 | }, | |
43da04a5 | 7146 | [NFT_MSG_GETOBJ_RESET] = { |
d9adf22a | 7147 | .call_rcu = nf_tables_getobj, |
43da04a5 PNA |
7148 | .attr_count = NFTA_OBJ_MAX, |
7149 | .policy = nft_obj_policy, | |
7150 | }, | |
3b49e2e9 PNA |
7151 | [NFT_MSG_NEWFLOWTABLE] = { |
7152 | .call_batch = nf_tables_newflowtable, | |
7153 | .attr_count = NFTA_FLOWTABLE_MAX, | |
7154 | .policy = nft_flowtable_policy, | |
7155 | }, | |
7156 | [NFT_MSG_GETFLOWTABLE] = { | |
d9adf22a | 7157 | .call_rcu = nf_tables_getflowtable, |
3b49e2e9 PNA |
7158 | .attr_count = NFTA_FLOWTABLE_MAX, |
7159 | .policy = nft_flowtable_policy, | |
7160 | }, | |
7161 | [NFT_MSG_DELFLOWTABLE] = { | |
7162 | .call_batch = nf_tables_delflowtable, | |
7163 | .attr_count = NFTA_FLOWTABLE_MAX, | |
7164 | .policy = nft_flowtable_policy, | |
7165 | }, | |
96518518 PM |
7166 | }; |
7167 | ||
a654de8f PNA |
7168 | static int nf_tables_validate(struct net *net) |
7169 | { | |
7170 | struct nft_table *table; | |
7171 | ||
7172 | switch (net->nft.validate_state) { | |
7173 | case NFT_VALIDATE_SKIP: | |
7174 | break; | |
7175 | case NFT_VALIDATE_NEED: | |
7176 | nft_validate_state_update(net, NFT_VALIDATE_DO); | |
7177 | /* fall through */ | |
7178 | case NFT_VALIDATE_DO: | |
7179 | list_for_each_entry(table, &net->nft.tables, list) { | |
7180 | if (nft_table_validate(net, table) < 0) | |
7181 | return -EAGAIN; | |
7182 | } | |
7183 | break; | |
7184 | } | |
7185 | ||
7186 | return 0; | |
7187 | } | |
7188 | ||
66293c46 FW |
7189 | /* a drop policy has to be deferred until all rules have been activated, |
7190 | * otherwise a large ruleset that contains a drop-policy base chain will | |
7191 | * cause all packets to get dropped until the full transaction has been | |
7192 | * processed. | |
7193 | * | |
7194 | * We defer the drop policy until the transaction has been finalized. | |
7195 | */ | |
7196 | static void nft_chain_commit_drop_policy(struct nft_trans *trans) | |
7197 | { | |
7198 | struct nft_base_chain *basechain; | |
7199 | ||
7200 | if (nft_trans_chain_policy(trans) != NF_DROP) | |
7201 | return; | |
7202 | ||
7203 | if (!nft_is_base_chain(trans->ctx.chain)) | |
7204 | return; | |
7205 | ||
7206 | basechain = nft_base_chain(trans->ctx.chain); | |
7207 | basechain->policy = NF_DROP; | |
7208 | } | |
7209 | ||
91c7b38d PNA |
7210 | static void nft_chain_commit_update(struct nft_trans *trans) |
7211 | { | |
7212 | struct nft_base_chain *basechain; | |
7213 | ||
1b2470e5 FW |
7214 | if (nft_trans_chain_name(trans)) { |
7215 | rhltable_remove(&trans->ctx.table->chains_ht, | |
7216 | &trans->ctx.chain->rhlhead, | |
7217 | nft_chain_ht_params); | |
d71efb59 | 7218 | swap(trans->ctx.chain->name, nft_trans_chain_name(trans)); |
1b2470e5 FW |
7219 | rhltable_insert_key(&trans->ctx.table->chains_ht, |
7220 | trans->ctx.chain->name, | |
7221 | &trans->ctx.chain->rhlhead, | |
7222 | nft_chain_ht_params); | |
7223 | } | |
91c7b38d | 7224 | |
f323d954 | 7225 | if (!nft_is_base_chain(trans->ctx.chain)) |
91c7b38d PNA |
7226 | return; |
7227 | ||
53315ac6 FW |
7228 | nft_chain_stats_replace(trans); |
7229 | ||
91c7b38d | 7230 | basechain = nft_base_chain(trans->ctx.chain); |
91c7b38d PNA |
7231 | |
7232 | switch (nft_trans_chain_policy(trans)) { | |
7233 | case NF_DROP: | |
7234 | case NF_ACCEPT: | |
7235 | basechain->policy = nft_trans_chain_policy(trans); | |
7236 | break; | |
7237 | } | |
7238 | } | |
7239 | ||
d62d0ba9 FFM |
7240 | static void nft_obj_commit_update(struct nft_trans *trans) |
7241 | { | |
7242 | struct nft_object *newobj; | |
7243 | struct nft_object *obj; | |
7244 | ||
7245 | obj = nft_trans_obj(trans); | |
7246 | newobj = nft_trans_obj_newobj(trans); | |
7247 | ||
9fedd894 FFM |
7248 | if (obj->ops->update) |
7249 | obj->ops->update(obj, newobj); | |
d62d0ba9 FFM |
7250 | |
7251 | kfree(newobj); | |
7252 | } | |
7253 | ||
2f99aa31 | 7254 | static void nft_commit_release(struct nft_trans *trans) |
c7c32e72 | 7255 | { |
c7c32e72 PNA |
7256 | switch (trans->msg_type) { |
7257 | case NFT_MSG_DELTABLE: | |
7258 | nf_tables_table_destroy(&trans->ctx); | |
7259 | break; | |
9f8aac0b | 7260 | case NFT_MSG_NEWCHAIN: |
53315ac6 | 7261 | free_percpu(nft_trans_chain_stats(trans)); |
9f8aac0b FW |
7262 | kfree(nft_trans_chain_name(trans)); |
7263 | break; | |
c7c32e72 | 7264 | case NFT_MSG_DELCHAIN: |
43a605f2 | 7265 | nf_tables_chain_destroy(&trans->ctx); |
c7c32e72 PNA |
7266 | break; |
7267 | case NFT_MSG_DELRULE: | |
7268 | nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans)); | |
7269 | break; | |
7270 | case NFT_MSG_DELSET: | |
0c2a85ed | 7271 | nft_set_destroy(&trans->ctx, nft_trans_set(trans)); |
c7c32e72 | 7272 | break; |
61edafbb | 7273 | case NFT_MSG_DELSETELEM: |
3453c927 PNA |
7274 | nf_tables_set_elem_destroy(&trans->ctx, |
7275 | nft_trans_elem_set(trans), | |
59105446 | 7276 | nft_trans_elem(trans).priv); |
61edafbb | 7277 | break; |
e5009240 | 7278 | case NFT_MSG_DELOBJ: |
00bfb320 | 7279 | nft_obj_destroy(&trans->ctx, nft_trans_obj(trans)); |
e5009240 | 7280 | break; |
3b49e2e9 | 7281 | case NFT_MSG_DELFLOWTABLE: |
abadb2f8 PNA |
7282 | if (nft_trans_flowtable_update(trans)) |
7283 | nft_flowtable_hooks_destroy(&nft_trans_flowtable_hooks(trans)); | |
7284 | else | |
7285 | nf_tables_flowtable_destroy(nft_trans_flowtable(trans)); | |
3b49e2e9 | 7286 | break; |
c7c32e72 | 7287 | } |
0935d558 FW |
7288 | |
7289 | if (trans->put_net) | |
7290 | put_net(trans->ctx.net); | |
7291 | ||
c7c32e72 PNA |
7292 | kfree(trans); |
7293 | } | |
7294 | ||
0935d558 | 7295 | static void nf_tables_trans_destroy_work(struct work_struct *w) |
2f99aa31 FW |
7296 | { |
7297 | struct nft_trans *trans, *next; | |
0935d558 FW |
7298 | LIST_HEAD(head); |
7299 | ||
7300 | spin_lock(&nf_tables_destroy_list_lock); | |
7301 | list_splice_init(&nf_tables_destroy_list, &head); | |
7302 | spin_unlock(&nf_tables_destroy_list_lock); | |
2f99aa31 | 7303 | |
0935d558 | 7304 | if (list_empty(&head)) |
2f99aa31 FW |
7305 | return; |
7306 | ||
7307 | synchronize_rcu(); | |
7308 | ||
0935d558 | 7309 | list_for_each_entry_safe(trans, next, &head, list) { |
2f99aa31 FW |
7310 | list_del(&trans->list); |
7311 | nft_commit_release(trans); | |
7312 | } | |
7313 | } | |
7314 | ||
0cbc06b3 FW |
7315 | static int nf_tables_commit_chain_prepare(struct net *net, struct nft_chain *chain) |
7316 | { | |
7317 | struct nft_rule *rule; | |
7318 | unsigned int alloc = 0; | |
7319 | int i; | |
7320 | ||
7321 | /* already handled or inactive chain? */ | |
7322 | if (chain->rules_next || !nft_is_active_next(net, chain)) | |
7323 | return 0; | |
7324 | ||
7325 | rule = list_entry(&chain->rules, struct nft_rule, list); | |
7326 | i = 0; | |
7327 | ||
7328 | list_for_each_entry_continue(rule, &chain->rules, list) { | |
7329 | if (nft_is_active_next(net, rule)) | |
7330 | alloc++; | |
7331 | } | |
7332 | ||
7333 | chain->rules_next = nf_tables_chain_alloc_rules(chain, alloc); | |
7334 | if (!chain->rules_next) | |
7335 | return -ENOMEM; | |
7336 | ||
7337 | list_for_each_entry_continue(rule, &chain->rules, list) { | |
7338 | if (nft_is_active_next(net, rule)) | |
7339 | chain->rules_next[i++] = rule; | |
7340 | } | |
7341 | ||
7342 | chain->rules_next[i] = NULL; | |
7343 | return 0; | |
7344 | } | |
7345 | ||
7346 | static void nf_tables_commit_chain_prepare_cancel(struct net *net) | |
7347 | { | |
7348 | struct nft_trans *trans, *next; | |
7349 | ||
7350 | list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) { | |
7351 | struct nft_chain *chain = trans->ctx.chain; | |
7352 | ||
7353 | if (trans->msg_type == NFT_MSG_NEWRULE || | |
7354 | trans->msg_type == NFT_MSG_DELRULE) { | |
7355 | kvfree(chain->rules_next); | |
7356 | chain->rules_next = NULL; | |
7357 | } | |
7358 | } | |
7359 | } | |
7360 | ||
7361 | static void __nf_tables_commit_chain_free_rules_old(struct rcu_head *h) | |
7362 | { | |
7363 | struct nft_rules_old *o = container_of(h, struct nft_rules_old, h); | |
7364 | ||
7365 | kvfree(o->start); | |
7366 | } | |
7367 | ||
7368 | static void nf_tables_commit_chain_free_rules_old(struct nft_rule **rules) | |
7369 | { | |
7370 | struct nft_rule **r = rules; | |
7371 | struct nft_rules_old *old; | |
7372 | ||
7373 | while (*r) | |
7374 | r++; | |
7375 | ||
7376 | r++; /* rcu_head is after end marker */ | |
7377 | old = (void *) r; | |
7378 | old->start = rules; | |
7379 | ||
7380 | call_rcu(&old->h, __nf_tables_commit_chain_free_rules_old); | |
7381 | } | |
7382 | ||
0fb39bbe | 7383 | static void nf_tables_commit_chain(struct net *net, struct nft_chain *chain) |
0cbc06b3 FW |
7384 | { |
7385 | struct nft_rule **g0, **g1; | |
7386 | bool next_genbit; | |
7387 | ||
7388 | next_genbit = nft_gencursor_next(net); | |
7389 | ||
7390 | g0 = rcu_dereference_protected(chain->rules_gen_0, | |
f102d66b | 7391 | lockdep_commit_lock_is_held(net)); |
0cbc06b3 | 7392 | g1 = rcu_dereference_protected(chain->rules_gen_1, |
f102d66b | 7393 | lockdep_commit_lock_is_held(net)); |
0cbc06b3 FW |
7394 | |
7395 | /* No changes to this chain? */ | |
7396 | if (chain->rules_next == NULL) { | |
7397 | /* chain had no change in last or next generation */ | |
7398 | if (g0 == g1) | |
7399 | return; | |
7400 | /* | |
7401 | * chain had no change in this generation; make sure next | |
7402 | * one uses same rules as current generation. | |
7403 | */ | |
7404 | if (next_genbit) { | |
7405 | rcu_assign_pointer(chain->rules_gen_1, g0); | |
7406 | nf_tables_commit_chain_free_rules_old(g1); | |
7407 | } else { | |
7408 | rcu_assign_pointer(chain->rules_gen_0, g1); | |
7409 | nf_tables_commit_chain_free_rules_old(g0); | |
7410 | } | |
7411 | ||
7412 | return; | |
7413 | } | |
7414 | ||
7415 | if (next_genbit) | |
7416 | rcu_assign_pointer(chain->rules_gen_1, chain->rules_next); | |
7417 | else | |
7418 | rcu_assign_pointer(chain->rules_gen_0, chain->rules_next); | |
7419 | ||
7420 | chain->rules_next = NULL; | |
7421 | ||
7422 | if (g0 == g1) | |
7423 | return; | |
7424 | ||
7425 | if (next_genbit) | |
7426 | nf_tables_commit_chain_free_rules_old(g1); | |
7427 | else | |
7428 | nf_tables_commit_chain_free_rules_old(g0); | |
7429 | } | |
7430 | ||
d152159b FW |
7431 | static void nft_obj_del(struct nft_object *obj) |
7432 | { | |
4d44175a | 7433 | rhltable_remove(&nft_objname_ht, &obj->rhlhead, nft_objname_ht_params); |
d152159b FW |
7434 | list_del_rcu(&obj->list); |
7435 | } | |
7436 | ||
1b2470e5 FW |
7437 | static void nft_chain_del(struct nft_chain *chain) |
7438 | { | |
7439 | struct nft_table *table = chain->table; | |
7440 | ||
7441 | WARN_ON_ONCE(rhltable_remove(&table->chains_ht, &chain->rhlhead, | |
7442 | nft_chain_ht_params)); | |
7443 | list_del_rcu(&chain->list); | |
7444 | } | |
7445 | ||
abadb2f8 PNA |
7446 | static void nft_flowtable_hooks_del(struct nft_flowtable *flowtable, |
7447 | struct list_head *hook_list) | |
7448 | { | |
7449 | struct nft_hook *hook, *next; | |
7450 | ||
7451 | list_for_each_entry_safe(hook, next, &flowtable->hook_list, list) { | |
7452 | if (hook->inactive) | |
7453 | list_move(&hook->list, hook_list); | |
7454 | } | |
7455 | } | |
7456 | ||
eb014de4 PNA |
7457 | static void nf_tables_module_autoload_cleanup(struct net *net) |
7458 | { | |
7459 | struct nft_module_request *req, *next; | |
7460 | ||
7461 | WARN_ON_ONCE(!list_empty(&net->nft.commit_list)); | |
7462 | list_for_each_entry_safe(req, next, &net->nft.module_list, list) { | |
7463 | WARN_ON_ONCE(!req->done); | |
7464 | list_del(&req->list); | |
7465 | kfree(req); | |
7466 | } | |
7467 | } | |
7468 | ||
0935d558 FW |
7469 | static void nf_tables_commit_release(struct net *net) |
7470 | { | |
7471 | struct nft_trans *trans; | |
7472 | ||
7473 | /* all side effects have to be made visible. | |
7474 | * For example, if a chain named 'foo' has been deleted, a | |
7475 | * new transaction must not find it anymore. | |
7476 | * | |
7477 | * Memory reclaim happens asynchronously from work queue | |
7478 | * to prevent expensive synchronize_rcu() in commit phase. | |
7479 | */ | |
7480 | if (list_empty(&net->nft.commit_list)) { | |
eb014de4 | 7481 | nf_tables_module_autoload_cleanup(net); |
0935d558 FW |
7482 | mutex_unlock(&net->nft.commit_mutex); |
7483 | return; | |
7484 | } | |
7485 | ||
7486 | trans = list_last_entry(&net->nft.commit_list, | |
7487 | struct nft_trans, list); | |
7488 | get_net(trans->ctx.net); | |
7489 | WARN_ON_ONCE(trans->put_net); | |
7490 | ||
7491 | trans->put_net = true; | |
7492 | spin_lock(&nf_tables_destroy_list_lock); | |
7493 | list_splice_tail_init(&net->nft.commit_list, &nf_tables_destroy_list); | |
7494 | spin_unlock(&nf_tables_destroy_list_lock); | |
7495 | ||
eb014de4 | 7496 | nf_tables_module_autoload_cleanup(net); |
0935d558 FW |
7497 | mutex_unlock(&net->nft.commit_mutex); |
7498 | ||
7499 | schedule_work(&trans_destroy_work); | |
7500 | } | |
7501 | ||
5913beaf | 7502 | static int nf_tables_commit(struct net *net, struct sk_buff *skb) |
37082f93 | 7503 | { |
37082f93 | 7504 | struct nft_trans *trans, *next; |
a3716e70 | 7505 | struct nft_trans_elem *te; |
0cbc06b3 FW |
7506 | struct nft_chain *chain; |
7507 | struct nft_table *table; | |
c9626a2c | 7508 | int err; |
37082f93 | 7509 | |
b8b27498 FW |
7510 | if (list_empty(&net->nft.commit_list)) { |
7511 | mutex_unlock(&net->nft.commit_mutex); | |
7512 | return 0; | |
7513 | } | |
7514 | ||
a654de8f PNA |
7515 | /* 0. Validate ruleset, otherwise roll back for error reporting. */ |
7516 | if (nf_tables_validate(net) < 0) | |
7517 | return -EAGAIN; | |
7518 | ||
c9626a2c PNA |
7519 | err = nft_flow_rule_offload_commit(net); |
7520 | if (err < 0) | |
7521 | return err; | |
7522 | ||
0cbc06b3 FW |
7523 | /* 1. Allocate space for next generation rules_gen_X[] */ |
7524 | list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) { | |
7525 | int ret; | |
37082f93 | 7526 | |
0cbc06b3 FW |
7527 | if (trans->msg_type == NFT_MSG_NEWRULE || |
7528 | trans->msg_type == NFT_MSG_DELRULE) { | |
7529 | chain = trans->ctx.chain; | |
7530 | ||
7531 | ret = nf_tables_commit_chain_prepare(net, chain); | |
7532 | if (ret < 0) { | |
7533 | nf_tables_commit_chain_prepare_cancel(net); | |
7534 | return ret; | |
7535 | } | |
7536 | } | |
7537 | } | |
37082f93 | 7538 | |
0cbc06b3 FW |
7539 | /* step 2. Make rules_gen_X visible to packet path */ |
7540 | list_for_each_entry(table, &net->nft.tables, list) { | |
0fb39bbe FW |
7541 | list_for_each_entry(chain, &table->chains, list) |
7542 | nf_tables_commit_chain(net, chain); | |
0cbc06b3 FW |
7543 | } |
7544 | ||
7545 | /* | |
7546 | * Bump generation counter, invalidate any dump in progress. | |
7547 | * Cannot fail after this point. | |
37082f93 | 7548 | */ |
0cbc06b3 FW |
7549 | while (++net->nft.base_seq == 0); |
7550 | ||
7551 | /* step 3. Start new generation, rules_gen_X now in use. */ | |
7552 | net->nft.gencursor = nft_gencursor_next(net); | |
37082f93 PNA |
7553 | |
7554 | list_for_each_entry_safe(trans, next, &net->nft.commit_list, list) { | |
b380e5c7 | 7555 | switch (trans->msg_type) { |
55dd6f93 PNA |
7556 | case NFT_MSG_NEWTABLE: |
7557 | if (nft_trans_table_update(trans)) { | |
7558 | if (!nft_trans_table_enable(trans)) { | |
664b0f8c | 7559 | nf_tables_table_disable(net, |
55dd6f93 PNA |
7560 | trans->ctx.table); |
7561 | trans->ctx.table->flags |= NFT_TABLE_F_DORMANT; | |
7562 | } | |
7563 | } else { | |
f2a6d766 | 7564 | nft_clear(net, trans->ctx.table); |
55dd6f93 | 7565 | } |
35151d84 | 7566 | nf_tables_table_notify(&trans->ctx, NFT_MSG_NEWTABLE); |
55dd6f93 PNA |
7567 | nft_trans_destroy(trans); |
7568 | break; | |
7569 | case NFT_MSG_DELTABLE: | |
f2a6d766 | 7570 | list_del_rcu(&trans->ctx.table->list); |
35151d84 | 7571 | nf_tables_table_notify(&trans->ctx, NFT_MSG_DELTABLE); |
55dd6f93 | 7572 | break; |
91c7b38d | 7573 | case NFT_MSG_NEWCHAIN: |
9f8aac0b | 7574 | if (nft_trans_chain_update(trans)) { |
91c7b38d | 7575 | nft_chain_commit_update(trans); |
9f8aac0b FW |
7576 | nf_tables_chain_notify(&trans->ctx, NFT_MSG_NEWCHAIN); |
7577 | /* trans destroyed after rcu grace period */ | |
7578 | } else { | |
66293c46 | 7579 | nft_chain_commit_drop_policy(trans); |
664b0f8c | 7580 | nft_clear(net, trans->ctx.chain); |
9f8aac0b FW |
7581 | nf_tables_chain_notify(&trans->ctx, NFT_MSG_NEWCHAIN); |
7582 | nft_trans_destroy(trans); | |
7583 | } | |
91c7b38d PNA |
7584 | break; |
7585 | case NFT_MSG_DELCHAIN: | |
1b2470e5 | 7586 | nft_chain_del(trans->ctx.chain); |
35151d84 | 7587 | nf_tables_chain_notify(&trans->ctx, NFT_MSG_DELCHAIN); |
c974a3a3 PNA |
7588 | nf_tables_unregister_hook(trans->ctx.net, |
7589 | trans->ctx.table, | |
7590 | trans->ctx.chain); | |
91c7b38d | 7591 | break; |
b380e5c7 | 7592 | case NFT_MSG_NEWRULE: |
889f7ee7 | 7593 | nft_clear(trans->ctx.net, nft_trans_rule(trans)); |
35151d84 | 7594 | nf_tables_rule_notify(&trans->ctx, |
37082f93 | 7595 | nft_trans_rule(trans), |
35151d84 | 7596 | NFT_MSG_NEWRULE); |
37082f93 | 7597 | nft_trans_destroy(trans); |
b380e5c7 PNA |
7598 | break; |
7599 | case NFT_MSG_DELRULE: | |
7600 | list_del_rcu(&nft_trans_rule(trans)->list); | |
35151d84 PNA |
7601 | nf_tables_rule_notify(&trans->ctx, |
7602 | nft_trans_rule(trans), | |
7603 | NFT_MSG_DELRULE); | |
f6ac8585 PNA |
7604 | nft_rule_expr_deactivate(&trans->ctx, |
7605 | nft_trans_rule(trans), | |
7606 | NFT_TRANS_COMMIT); | |
b380e5c7 | 7607 | break; |
958bee14 | 7608 | case NFT_MSG_NEWSET: |
37a9cc52 | 7609 | nft_clear(net, nft_trans_set(trans)); |
4fefee57 PNA |
7610 | /* This avoids hitting -EBUSY when deleting the table |
7611 | * from the transaction. | |
7612 | */ | |
408070d6 | 7613 | if (nft_set_is_anonymous(nft_trans_set(trans)) && |
4fefee57 PNA |
7614 | !list_empty(&nft_trans_set(trans)->bindings)) |
7615 | trans->ctx.table->use--; | |
7616 | ||
958bee14 | 7617 | nf_tables_set_notify(&trans->ctx, nft_trans_set(trans), |
31f8441c | 7618 | NFT_MSG_NEWSET, GFP_KERNEL); |
958bee14 PNA |
7619 | nft_trans_destroy(trans); |
7620 | break; | |
7621 | case NFT_MSG_DELSET: | |
37a9cc52 | 7622 | list_del_rcu(&nft_trans_set(trans)->list); |
958bee14 | 7623 | nf_tables_set_notify(&trans->ctx, nft_trans_set(trans), |
31f8441c | 7624 | NFT_MSG_DELSET, GFP_KERNEL); |
958bee14 | 7625 | break; |
60319eb1 | 7626 | case NFT_MSG_NEWSETELEM: |
cc02e457 PM |
7627 | te = (struct nft_trans_elem *)trans->data; |
7628 | ||
42a55769 | 7629 | te->set->ops->activate(net, te->set, &te->elem); |
cc02e457 PM |
7630 | nf_tables_setelem_notify(&trans->ctx, te->set, |
7631 | &te->elem, | |
60319eb1 PNA |
7632 | NFT_MSG_NEWSETELEM, 0); |
7633 | nft_trans_destroy(trans); | |
7634 | break; | |
7635 | case NFT_MSG_DELSETELEM: | |
a3716e70 | 7636 | te = (struct nft_trans_elem *)trans->data; |
fe2811eb | 7637 | |
a3716e70 PNA |
7638 | nf_tables_setelem_notify(&trans->ctx, te->set, |
7639 | &te->elem, | |
60319eb1 | 7640 | NFT_MSG_DELSETELEM, 0); |
5cb82a38 | 7641 | te->set->ops->remove(net, te->set, &te->elem); |
3dd0673a PM |
7642 | atomic_dec(&te->set->nelems); |
7643 | te->set->ndeact--; | |
60319eb1 | 7644 | break; |
e5009240 | 7645 | case NFT_MSG_NEWOBJ: |
d62d0ba9 FFM |
7646 | if (nft_trans_obj_update(trans)) { |
7647 | nft_obj_commit_update(trans); | |
7648 | nf_tables_obj_notify(&trans->ctx, | |
7649 | nft_trans_obj(trans), | |
7650 | NFT_MSG_NEWOBJ); | |
7651 | } else { | |
7652 | nft_clear(net, nft_trans_obj(trans)); | |
7653 | nf_tables_obj_notify(&trans->ctx, | |
7654 | nft_trans_obj(trans), | |
7655 | NFT_MSG_NEWOBJ); | |
7656 | nft_trans_destroy(trans); | |
7657 | } | |
e5009240 PNA |
7658 | break; |
7659 | case NFT_MSG_DELOBJ: | |
d152159b | 7660 | nft_obj_del(nft_trans_obj(trans)); |
e5009240 PNA |
7661 | nf_tables_obj_notify(&trans->ctx, nft_trans_obj(trans), |
7662 | NFT_MSG_DELOBJ); | |
7663 | break; | |
3b49e2e9 | 7664 | case NFT_MSG_NEWFLOWTABLE: |
78d9f48f PNA |
7665 | if (nft_trans_flowtable_update(trans)) { |
7666 | nf_tables_flowtable_notify(&trans->ctx, | |
7667 | nft_trans_flowtable(trans), | |
7668 | &nft_trans_flowtable_hooks(trans), | |
7669 | NFT_MSG_NEWFLOWTABLE); | |
7670 | list_splice(&nft_trans_flowtable_hooks(trans), | |
7671 | &nft_trans_flowtable(trans)->hook_list); | |
7672 | } else { | |
7673 | nft_clear(net, nft_trans_flowtable(trans)); | |
7674 | nf_tables_flowtable_notify(&trans->ctx, | |
7675 | nft_trans_flowtable(trans), | |
7676 | &nft_trans_flowtable(trans)->hook_list, | |
7677 | NFT_MSG_NEWFLOWTABLE); | |
7678 | } | |
3b49e2e9 PNA |
7679 | nft_trans_destroy(trans); |
7680 | break; | |
7681 | case NFT_MSG_DELFLOWTABLE: | |
abadb2f8 PNA |
7682 | if (nft_trans_flowtable_update(trans)) { |
7683 | nft_flowtable_hooks_del(nft_trans_flowtable(trans), | |
7684 | &nft_trans_flowtable_hooks(trans)); | |
7685 | nf_tables_flowtable_notify(&trans->ctx, | |
7686 | nft_trans_flowtable(trans), | |
7687 | &nft_trans_flowtable_hooks(trans), | |
7688 | NFT_MSG_DELFLOWTABLE); | |
7689 | nft_unregister_flowtable_net_hooks(net, | |
7690 | &nft_trans_flowtable_hooks(trans)); | |
7691 | } else { | |
7692 | list_del_rcu(&nft_trans_flowtable(trans)->list); | |
7693 | nf_tables_flowtable_notify(&trans->ctx, | |
7694 | nft_trans_flowtable(trans), | |
7695 | &nft_trans_flowtable(trans)->hook_list, | |
7696 | NFT_MSG_DELFLOWTABLE); | |
7697 | nft_unregister_flowtable_net_hooks(net, | |
7698 | &nft_trans_flowtable(trans)->hook_list); | |
7699 | } | |
3b49e2e9 | 7700 | break; |
37082f93 | 7701 | } |
37082f93 PNA |
7702 | } |
7703 | ||
84d7fce6 | 7704 | nf_tables_gen_notify(net, skb, NFT_MSG_NEWGEN); |
0935d558 | 7705 | nf_tables_commit_release(net); |
37082f93 PNA |
7706 | |
7707 | return 0; | |
7708 | } | |
7709 | ||
eb014de4 PNA |
7710 | static void nf_tables_module_autoload(struct net *net) |
7711 | { | |
7712 | struct nft_module_request *req, *next; | |
7713 | LIST_HEAD(module_list); | |
7714 | ||
7715 | list_splice_init(&net->nft.module_list, &module_list); | |
7716 | mutex_unlock(&net->nft.commit_mutex); | |
7717 | list_for_each_entry_safe(req, next, &module_list, list) { | |
1d305ba4 FW |
7718 | request_module("%s", req->module); |
7719 | req->done = true; | |
eb014de4 PNA |
7720 | } |
7721 | mutex_lock(&net->nft.commit_mutex); | |
7722 | list_splice(&module_list, &net->nft.module_list); | |
7723 | } | |
7724 | ||
b326dd37 | 7725 | static void nf_tables_abort_release(struct nft_trans *trans) |
c7c32e72 | 7726 | { |
c7c32e72 PNA |
7727 | switch (trans->msg_type) { |
7728 | case NFT_MSG_NEWTABLE: | |
7729 | nf_tables_table_destroy(&trans->ctx); | |
7730 | break; | |
7731 | case NFT_MSG_NEWCHAIN: | |
43a605f2 | 7732 | nf_tables_chain_destroy(&trans->ctx); |
c7c32e72 PNA |
7733 | break; |
7734 | case NFT_MSG_NEWRULE: | |
7735 | nf_tables_rule_destroy(&trans->ctx, nft_trans_rule(trans)); | |
7736 | break; | |
7737 | case NFT_MSG_NEWSET: | |
0c2a85ed | 7738 | nft_set_destroy(&trans->ctx, nft_trans_set(trans)); |
c7c32e72 | 7739 | break; |
61edafbb PM |
7740 | case NFT_MSG_NEWSETELEM: |
7741 | nft_set_elem_destroy(nft_trans_elem_set(trans), | |
61f9e292 | 7742 | nft_trans_elem(trans).priv, true); |
61edafbb | 7743 | break; |
e5009240 | 7744 | case NFT_MSG_NEWOBJ: |
00bfb320 | 7745 | nft_obj_destroy(&trans->ctx, nft_trans_obj(trans)); |
e5009240 | 7746 | break; |
3b49e2e9 | 7747 | case NFT_MSG_NEWFLOWTABLE: |
78d9f48f PNA |
7748 | if (nft_trans_flowtable_update(trans)) |
7749 | nft_flowtable_hooks_destroy(&nft_trans_flowtable_hooks(trans)); | |
7750 | else | |
7751 | nf_tables_flowtable_destroy(nft_trans_flowtable(trans)); | |
3b49e2e9 | 7752 | break; |
c7c32e72 PNA |
7753 | } |
7754 | kfree(trans); | |
7755 | } | |
7756 | ||
eb014de4 | 7757 | static int __nf_tables_abort(struct net *net, bool autoload) |
37082f93 | 7758 | { |
37082f93 | 7759 | struct nft_trans *trans, *next; |
02263db0 | 7760 | struct nft_trans_elem *te; |
abadb2f8 | 7761 | struct nft_hook *hook; |
37082f93 | 7762 | |
a907e36d XL |
7763 | list_for_each_entry_safe_reverse(trans, next, &net->nft.commit_list, |
7764 | list) { | |
b380e5c7 | 7765 | switch (trans->msg_type) { |
55dd6f93 PNA |
7766 | case NFT_MSG_NEWTABLE: |
7767 | if (nft_trans_table_update(trans)) { | |
7768 | if (nft_trans_table_enable(trans)) { | |
664b0f8c | 7769 | nf_tables_table_disable(net, |
55dd6f93 PNA |
7770 | trans->ctx.table); |
7771 | trans->ctx.table->flags |= NFT_TABLE_F_DORMANT; | |
7772 | } | |
7773 | nft_trans_destroy(trans); | |
7774 | } else { | |
e688a7f8 | 7775 | list_del_rcu(&trans->ctx.table->list); |
55dd6f93 PNA |
7776 | } |
7777 | break; | |
7778 | case NFT_MSG_DELTABLE: | |
f2a6d766 | 7779 | nft_clear(trans->ctx.net, trans->ctx.table); |
55dd6f93 PNA |
7780 | nft_trans_destroy(trans); |
7781 | break; | |
91c7b38d PNA |
7782 | case NFT_MSG_NEWCHAIN: |
7783 | if (nft_trans_chain_update(trans)) { | |
982f4051 | 7784 | free_percpu(nft_trans_chain_stats(trans)); |
9f8aac0b | 7785 | kfree(nft_trans_chain_name(trans)); |
91c7b38d PNA |
7786 | nft_trans_destroy(trans); |
7787 | } else { | |
4fefee57 | 7788 | trans->ctx.table->use--; |
1b2470e5 | 7789 | nft_chain_del(trans->ctx.chain); |
c974a3a3 PNA |
7790 | nf_tables_unregister_hook(trans->ctx.net, |
7791 | trans->ctx.table, | |
7792 | trans->ctx.chain); | |
91c7b38d PNA |
7793 | } |
7794 | break; | |
7795 | case NFT_MSG_DELCHAIN: | |
4fefee57 | 7796 | trans->ctx.table->use++; |
664b0f8c | 7797 | nft_clear(trans->ctx.net, trans->ctx.chain); |
91c7b38d PNA |
7798 | nft_trans_destroy(trans); |
7799 | break; | |
b380e5c7 | 7800 | case NFT_MSG_NEWRULE: |
4fefee57 | 7801 | trans->ctx.chain->use--; |
b380e5c7 | 7802 | list_del_rcu(&nft_trans_rule(trans)->list); |
f6ac8585 PNA |
7803 | nft_rule_expr_deactivate(&trans->ctx, |
7804 | nft_trans_rule(trans), | |
7805 | NFT_TRANS_ABORT); | |
b380e5c7 PNA |
7806 | break; |
7807 | case NFT_MSG_DELRULE: | |
4fefee57 | 7808 | trans->ctx.chain->use++; |
889f7ee7 | 7809 | nft_clear(trans->ctx.net, nft_trans_rule(trans)); |
bb7b40ae | 7810 | nft_rule_expr_activate(&trans->ctx, nft_trans_rule(trans)); |
37082f93 | 7811 | nft_trans_destroy(trans); |
b380e5c7 | 7812 | break; |
958bee14 | 7813 | case NFT_MSG_NEWSET: |
4fefee57 | 7814 | trans->ctx.table->use--; |
6a0a8d10 | 7815 | if (nft_trans_set_bound(trans)) { |
40ba1d9b PNA |
7816 | nft_trans_destroy(trans); |
7817 | break; | |
7818 | } | |
7819 | list_del_rcu(&nft_trans_set(trans)->list); | |
958bee14 PNA |
7820 | break; |
7821 | case NFT_MSG_DELSET: | |
4fefee57 | 7822 | trans->ctx.table->use++; |
37a9cc52 | 7823 | nft_clear(trans->ctx.net, nft_trans_set(trans)); |
958bee14 PNA |
7824 | nft_trans_destroy(trans); |
7825 | break; | |
60319eb1 | 7826 | case NFT_MSG_NEWSETELEM: |
6a0a8d10 | 7827 | if (nft_trans_elem_set_bound(trans)) { |
40ba1d9b PNA |
7828 | nft_trans_destroy(trans); |
7829 | break; | |
7830 | } | |
02263db0 | 7831 | te = (struct nft_trans_elem *)trans->data; |
5cb82a38 | 7832 | te->set->ops->remove(net, te->set, &te->elem); |
3dd0673a | 7833 | atomic_dec(&te->set->nelems); |
60319eb1 PNA |
7834 | break; |
7835 | case NFT_MSG_DELSETELEM: | |
cc02e457 PM |
7836 | te = (struct nft_trans_elem *)trans->data; |
7837 | ||
59105446 | 7838 | nft_set_elem_activate(net, te->set, &te->elem); |
42a55769 | 7839 | te->set->ops->activate(net, te->set, &te->elem); |
3dd0673a | 7840 | te->set->ndeact--; |
cc02e457 | 7841 | |
e5009240 PNA |
7842 | nft_trans_destroy(trans); |
7843 | break; | |
7844 | case NFT_MSG_NEWOBJ: | |
d62d0ba9 FFM |
7845 | if (nft_trans_obj_update(trans)) { |
7846 | kfree(nft_trans_obj_newobj(trans)); | |
7847 | nft_trans_destroy(trans); | |
7848 | } else { | |
7849 | trans->ctx.table->use--; | |
7850 | nft_obj_del(nft_trans_obj(trans)); | |
7851 | } | |
e5009240 PNA |
7852 | break; |
7853 | case NFT_MSG_DELOBJ: | |
7854 | trans->ctx.table->use++; | |
7855 | nft_clear(trans->ctx.net, nft_trans_obj(trans)); | |
60319eb1 PNA |
7856 | nft_trans_destroy(trans); |
7857 | break; | |
3b49e2e9 | 7858 | case NFT_MSG_NEWFLOWTABLE: |
78d9f48f PNA |
7859 | if (nft_trans_flowtable_update(trans)) { |
7860 | nft_unregister_flowtable_net_hooks(net, | |
7861 | &nft_trans_flowtable_hooks(trans)); | |
7862 | } else { | |
7863 | trans->ctx.table->use--; | |
7864 | list_del_rcu(&nft_trans_flowtable(trans)->list); | |
7865 | nft_unregister_flowtable_net_hooks(net, | |
7866 | &nft_trans_flowtable(trans)->hook_list); | |
7867 | } | |
3b49e2e9 PNA |
7868 | break; |
7869 | case NFT_MSG_DELFLOWTABLE: | |
abadb2f8 PNA |
7870 | if (nft_trans_flowtable_update(trans)) { |
7871 | list_for_each_entry(hook, &nft_trans_flowtable(trans)->hook_list, list) | |
7872 | hook->inactive = false; | |
7873 | } else { | |
7874 | trans->ctx.table->use++; | |
7875 | nft_clear(trans->ctx.net, nft_trans_flowtable(trans)); | |
7876 | } | |
3b49e2e9 PNA |
7877 | nft_trans_destroy(trans); |
7878 | break; | |
37082f93 | 7879 | } |
37082f93 PNA |
7880 | } |
7881 | ||
b326dd37 PNA |
7882 | synchronize_rcu(); |
7883 | ||
a1cee076 PNA |
7884 | list_for_each_entry_safe_reverse(trans, next, |
7885 | &net->nft.commit_list, list) { | |
c7c32e72 | 7886 | list_del(&trans->list); |
b326dd37 | 7887 | nf_tables_abort_release(trans); |
37082f93 PNA |
7888 | } |
7889 | ||
eb014de4 PNA |
7890 | if (autoload) |
7891 | nf_tables_module_autoload(net); | |
7892 | else | |
7893 | nf_tables_module_autoload_cleanup(net); | |
7894 | ||
37082f93 PNA |
7895 | return 0; |
7896 | } | |
7897 | ||
a654de8f PNA |
7898 | static void nf_tables_cleanup(struct net *net) |
7899 | { | |
7900 | nft_validate_state_update(net, NFT_VALIDATE_SKIP); | |
7901 | } | |
7902 | ||
eb014de4 | 7903 | static int nf_tables_abort(struct net *net, struct sk_buff *skb, bool autoload) |
71ad00c5 | 7904 | { |
eb014de4 | 7905 | int ret = __nf_tables_abort(net, autoload); |
f102d66b FW |
7906 | |
7907 | mutex_unlock(&net->nft.commit_mutex); | |
7908 | ||
7909 | return ret; | |
71ad00c5 FW |
7910 | } |
7911 | ||
74e8bcd2 PNA |
7912 | static bool nf_tables_valid_genid(struct net *net, u32 genid) |
7913 | { | |
f102d66b FW |
7914 | bool genid_ok; |
7915 | ||
7916 | mutex_lock(&net->nft.commit_mutex); | |
7917 | ||
7918 | genid_ok = genid == 0 || net->nft.base_seq == genid; | |
7919 | if (!genid_ok) | |
7920 | mutex_unlock(&net->nft.commit_mutex); | |
7921 | ||
7922 | /* else, commit mutex has to be released by commit or abort function */ | |
7923 | return genid_ok; | |
74e8bcd2 PNA |
7924 | } |
7925 | ||
96518518 PM |
7926 | static const struct nfnetlink_subsystem nf_tables_subsys = { |
7927 | .name = "nf_tables", | |
7928 | .subsys_id = NFNL_SUBSYS_NFTABLES, | |
7929 | .cb_count = NFT_MSG_MAX, | |
7930 | .cb = nf_tables_cb, | |
0628b123 PNA |
7931 | .commit = nf_tables_commit, |
7932 | .abort = nf_tables_abort, | |
a654de8f | 7933 | .cleanup = nf_tables_cleanup, |
74e8bcd2 | 7934 | .valid_genid = nf_tables_valid_genid, |
be2ab5b4 | 7935 | .owner = THIS_MODULE, |
96518518 PM |
7936 | }; |
7937 | ||
7210e4e3 | 7938 | int nft_chain_validate_dependency(const struct nft_chain *chain, |
32537e91 | 7939 | enum nft_chain_types type) |
7210e4e3 PNA |
7940 | { |
7941 | const struct nft_base_chain *basechain; | |
7942 | ||
f323d954 | 7943 | if (nft_is_base_chain(chain)) { |
7210e4e3 PNA |
7944 | basechain = nft_base_chain(chain); |
7945 | if (basechain->type->type != type) | |
7946 | return -EOPNOTSUPP; | |
7947 | } | |
7948 | return 0; | |
7949 | } | |
7950 | EXPORT_SYMBOL_GPL(nft_chain_validate_dependency); | |
7951 | ||
75e8d06d PNA |
7952 | int nft_chain_validate_hooks(const struct nft_chain *chain, |
7953 | unsigned int hook_flags) | |
7954 | { | |
7955 | struct nft_base_chain *basechain; | |
7956 | ||
f323d954 | 7957 | if (nft_is_base_chain(chain)) { |
75e8d06d PNA |
7958 | basechain = nft_base_chain(chain); |
7959 | ||
c974a3a3 | 7960 | if ((1 << basechain->ops.hooknum) & hook_flags) |
75e8d06d PNA |
7961 | return 0; |
7962 | ||
7963 | return -EOPNOTSUPP; | |
7964 | } | |
7965 | ||
7966 | return 0; | |
7967 | } | |
7968 | EXPORT_SYMBOL_GPL(nft_chain_validate_hooks); | |
7969 | ||
20a69341 PM |
7970 | /* |
7971 | * Loop detection - walk through the ruleset beginning at the destination chain | |
7972 | * of a new jump until either the source chain is reached (loop) or all | |
7973 | * reachable chains have been traversed. | |
7974 | * | |
7975 | * The loop check is performed whenever a new jump verdict is added to an | |
7976 | * expression or verdict map or a verdict map is bound to a new chain. | |
7977 | */ | |
7978 | ||
7979 | static int nf_tables_check_loops(const struct nft_ctx *ctx, | |
7980 | const struct nft_chain *chain); | |
7981 | ||
7982 | static int nf_tables_loop_check_setelem(const struct nft_ctx *ctx, | |
de70185d | 7983 | struct nft_set *set, |
20a69341 | 7984 | const struct nft_set_iter *iter, |
de70185d | 7985 | struct nft_set_elem *elem) |
20a69341 | 7986 | { |
fe2811eb PM |
7987 | const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); |
7988 | const struct nft_data *data; | |
7989 | ||
7990 | if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) && | |
7991 | *nft_set_ext_flags(ext) & NFT_SET_ELEM_INTERVAL_END) | |
62f9c8b4 PNA |
7992 | return 0; |
7993 | ||
fe2811eb | 7994 | data = nft_set_ext_data(ext); |
1ca2e170 | 7995 | switch (data->verdict.code) { |
20a69341 PM |
7996 | case NFT_JUMP: |
7997 | case NFT_GOTO: | |
1ca2e170 | 7998 | return nf_tables_check_loops(ctx, data->verdict.chain); |
20a69341 PM |
7999 | default: |
8000 | return 0; | |
8001 | } | |
8002 | } | |
8003 | ||
8004 | static int nf_tables_check_loops(const struct nft_ctx *ctx, | |
8005 | const struct nft_chain *chain) | |
8006 | { | |
8007 | const struct nft_rule *rule; | |
8008 | const struct nft_expr *expr, *last; | |
de70185d | 8009 | struct nft_set *set; |
20a69341 PM |
8010 | struct nft_set_binding *binding; |
8011 | struct nft_set_iter iter; | |
20a69341 PM |
8012 | |
8013 | if (ctx->chain == chain) | |
8014 | return -ELOOP; | |
8015 | ||
8016 | list_for_each_entry(rule, &chain->rules, list) { | |
8017 | nft_rule_for_each_expr(expr, last, rule) { | |
a654de8f PNA |
8018 | struct nft_immediate_expr *priv; |
8019 | const struct nft_data *data; | |
0ca743a5 PNA |
8020 | int err; |
8021 | ||
a654de8f | 8022 | if (strcmp(expr->ops->type->name, "immediate")) |
20a69341 PM |
8023 | continue; |
8024 | ||
a654de8f PNA |
8025 | priv = nft_expr_priv(expr); |
8026 | if (priv->dreg != NFT_REG_VERDICT) | |
0ca743a5 | 8027 | continue; |
20a69341 | 8028 | |
a654de8f | 8029 | data = &priv->data; |
1ca2e170 | 8030 | switch (data->verdict.code) { |
20a69341 PM |
8031 | case NFT_JUMP: |
8032 | case NFT_GOTO: | |
1ca2e170 PM |
8033 | err = nf_tables_check_loops(ctx, |
8034 | data->verdict.chain); | |
20a69341 PM |
8035 | if (err < 0) |
8036 | return err; | |
8037 | default: | |
8038 | break; | |
8039 | } | |
8040 | } | |
8041 | } | |
8042 | ||
8043 | list_for_each_entry(set, &ctx->table->sets, list) { | |
37a9cc52 PNA |
8044 | if (!nft_is_active_next(ctx->net, set)) |
8045 | continue; | |
20a69341 PM |
8046 | if (!(set->flags & NFT_SET_MAP) || |
8047 | set->dtype != NFT_DATA_VERDICT) | |
8048 | continue; | |
8049 | ||
8050 | list_for_each_entry(binding, &set->bindings, list) { | |
11113e19 PM |
8051 | if (!(binding->flags & NFT_SET_MAP) || |
8052 | binding->chain != chain) | |
20a69341 PM |
8053 | continue; |
8054 | ||
8588ac09 | 8055 | iter.genmask = nft_genmask_next(ctx->net); |
20a69341 PM |
8056 | iter.skip = 0; |
8057 | iter.count = 0; | |
8058 | iter.err = 0; | |
8059 | iter.fn = nf_tables_loop_check_setelem; | |
8060 | ||
8061 | set->ops->walk(ctx, set, &iter); | |
8062 | if (iter.err < 0) | |
8063 | return iter.err; | |
8064 | } | |
8065 | } | |
8066 | ||
8067 | return 0; | |
8068 | } | |
8069 | ||
36b701fa LGL |
8070 | /** |
8071 | * nft_parse_u32_check - fetch u32 attribute and check for maximum value | |
8072 | * | |
8073 | * @attr: netlink attribute to fetch value from | |
8074 | * @max: maximum value to be stored in dest | |
8075 | * @dest: pointer to the variable | |
8076 | * | |
8077 | * Parse, check and store a given u32 netlink attribute into variable. | |
8078 | * This function returns -ERANGE if the value goes over maximum value. | |
8079 | * Otherwise a 0 is returned and the attribute value is stored in the | |
8080 | * destination variable. | |
8081 | */ | |
f1d505bb | 8082 | int nft_parse_u32_check(const struct nlattr *attr, int max, u32 *dest) |
36b701fa | 8083 | { |
09525a09 | 8084 | u32 val; |
36b701fa LGL |
8085 | |
8086 | val = ntohl(nla_get_be32(attr)); | |
8087 | if (val > max) | |
8088 | return -ERANGE; | |
8089 | ||
8090 | *dest = val; | |
8091 | return 0; | |
8092 | } | |
8093 | EXPORT_SYMBOL_GPL(nft_parse_u32_check); | |
8094 | ||
49499c3e PM |
8095 | /** |
8096 | * nft_parse_register - parse a register value from a netlink attribute | |
8097 | * | |
8098 | * @attr: netlink attribute | |
8099 | * | |
8100 | * Parse and translate a register value from a netlink attribute. | |
8101 | * Registers used to be 128 bit wide, these register numbers will be | |
8102 | * mapped to the corresponding 32 bit register numbers. | |
8103 | */ | |
b1c96ed3 PM |
8104 | unsigned int nft_parse_register(const struct nlattr *attr) |
8105 | { | |
49499c3e PM |
8106 | unsigned int reg; |
8107 | ||
8108 | reg = ntohl(nla_get_be32(attr)); | |
8109 | switch (reg) { | |
8110 | case NFT_REG_VERDICT...NFT_REG_4: | |
8111 | return reg * NFT_REG_SIZE / NFT_REG32_SIZE; | |
8112 | default: | |
8113 | return reg + NFT_REG_SIZE / NFT_REG32_SIZE - NFT_REG32_00; | |
8114 | } | |
b1c96ed3 PM |
8115 | } |
8116 | EXPORT_SYMBOL_GPL(nft_parse_register); | |
8117 | ||
49499c3e PM |
8118 | /** |
8119 | * nft_dump_register - dump a register value to a netlink attribute | |
8120 | * | |
8121 | * @skb: socket buffer | |
8122 | * @attr: attribute number | |
8123 | * @reg: register number | |
8124 | * | |
8125 | * Construct a netlink attribute containing the register number. For | |
8126 | * compatibility reasons, register numbers being a multiple of 4 are | |
8127 | * translated to the corresponding 128 bit register numbers. | |
8128 | */ | |
b1c96ed3 PM |
8129 | int nft_dump_register(struct sk_buff *skb, unsigned int attr, unsigned int reg) |
8130 | { | |
49499c3e PM |
8131 | if (reg % (NFT_REG_SIZE / NFT_REG32_SIZE) == 0) |
8132 | reg = reg / (NFT_REG_SIZE / NFT_REG32_SIZE); | |
8133 | else | |
8134 | reg = reg - NFT_REG_SIZE / NFT_REG32_SIZE + NFT_REG32_00; | |
8135 | ||
b1c96ed3 PM |
8136 | return nla_put_be32(skb, attr, htonl(reg)); |
8137 | } | |
8138 | EXPORT_SYMBOL_GPL(nft_dump_register); | |
8139 | ||
96518518 | 8140 | /** |
d07db988 | 8141 | * nft_validate_register_load - validate a load from a register |
96518518 PM |
8142 | * |
8143 | * @reg: the register number | |
d07db988 | 8144 | * @len: the length of the data |
96518518 PM |
8145 | * |
8146 | * Validate that the input register is one of the general purpose | |
d07db988 | 8147 | * registers and that the length of the load is within the bounds. |
96518518 | 8148 | */ |
d07db988 | 8149 | int nft_validate_register_load(enum nft_registers reg, unsigned int len) |
96518518 | 8150 | { |
49499c3e | 8151 | if (reg < NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE) |
96518518 | 8152 | return -EINVAL; |
d07db988 PM |
8153 | if (len == 0) |
8154 | return -EINVAL; | |
c593642c | 8155 | if (reg * NFT_REG32_SIZE + len > sizeof_field(struct nft_regs, data)) |
d07db988 | 8156 | return -ERANGE; |
49499c3e | 8157 | |
96518518 PM |
8158 | return 0; |
8159 | } | |
d07db988 | 8160 | EXPORT_SYMBOL_GPL(nft_validate_register_load); |
96518518 | 8161 | |
96518518 | 8162 | /** |
1ec10212 | 8163 | * nft_validate_register_store - validate an expressions' register store |
96518518 PM |
8164 | * |
8165 | * @ctx: context of the expression performing the load | |
8166 | * @reg: the destination register number | |
8167 | * @data: the data to load | |
8168 | * @type: the data type | |
45d9bcda | 8169 | * @len: the length of the data |
96518518 PM |
8170 | * |
8171 | * Validate that a data load uses the appropriate data type for | |
45d9bcda PM |
8172 | * the destination register and the length is within the bounds. |
8173 | * A value of NULL for the data means that its runtime gathered | |
58f40ab6 | 8174 | * data. |
96518518 | 8175 | */ |
1ec10212 PM |
8176 | int nft_validate_register_store(const struct nft_ctx *ctx, |
8177 | enum nft_registers reg, | |
8178 | const struct nft_data *data, | |
8179 | enum nft_data_types type, unsigned int len) | |
96518518 | 8180 | { |
20a69341 PM |
8181 | int err; |
8182 | ||
96518518 PM |
8183 | switch (reg) { |
8184 | case NFT_REG_VERDICT: | |
58f40ab6 | 8185 | if (type != NFT_DATA_VERDICT) |
96518518 | 8186 | return -EINVAL; |
20a69341 | 8187 | |
58f40ab6 | 8188 | if (data != NULL && |
1ca2e170 PM |
8189 | (data->verdict.code == NFT_GOTO || |
8190 | data->verdict.code == NFT_JUMP)) { | |
8191 | err = nf_tables_check_loops(ctx, data->verdict.chain); | |
20a69341 PM |
8192 | if (err < 0) |
8193 | return err; | |
20a69341 PM |
8194 | } |
8195 | ||
96518518 PM |
8196 | return 0; |
8197 | default: | |
49499c3e | 8198 | if (reg < NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE) |
27e6d201 | 8199 | return -EINVAL; |
45d9bcda PM |
8200 | if (len == 0) |
8201 | return -EINVAL; | |
49499c3e | 8202 | if (reg * NFT_REG32_SIZE + len > |
c593642c | 8203 | sizeof_field(struct nft_regs, data)) |
45d9bcda | 8204 | return -ERANGE; |
27e6d201 | 8205 | |
96518518 PM |
8206 | if (data != NULL && type != NFT_DATA_VALUE) |
8207 | return -EINVAL; | |
8208 | return 0; | |
8209 | } | |
8210 | } | |
1ec10212 | 8211 | EXPORT_SYMBOL_GPL(nft_validate_register_store); |
96518518 PM |
8212 | |
8213 | static const struct nla_policy nft_verdict_policy[NFTA_VERDICT_MAX + 1] = { | |
8214 | [NFTA_VERDICT_CODE] = { .type = NLA_U32 }, | |
8215 | [NFTA_VERDICT_CHAIN] = { .type = NLA_STRING, | |
8216 | .len = NFT_CHAIN_MAXNAMELEN - 1 }, | |
8217 | }; | |
8218 | ||
8219 | static int nft_verdict_init(const struct nft_ctx *ctx, struct nft_data *data, | |
8220 | struct nft_data_desc *desc, const struct nlattr *nla) | |
8221 | { | |
664b0f8c | 8222 | u8 genmask = nft_genmask_next(ctx->net); |
96518518 PM |
8223 | struct nlattr *tb[NFTA_VERDICT_MAX + 1]; |
8224 | struct nft_chain *chain; | |
8225 | int err; | |
8226 | ||
8cb08174 JB |
8227 | err = nla_parse_nested_deprecated(tb, NFTA_VERDICT_MAX, nla, |
8228 | nft_verdict_policy, NULL); | |
96518518 PM |
8229 | if (err < 0) |
8230 | return err; | |
8231 | ||
8232 | if (!tb[NFTA_VERDICT_CODE]) | |
8233 | return -EINVAL; | |
1ca2e170 | 8234 | data->verdict.code = ntohl(nla_get_be32(tb[NFTA_VERDICT_CODE])); |
96518518 | 8235 | |
1ca2e170 | 8236 | switch (data->verdict.code) { |
e0abdadc | 8237 | default: |
1ca2e170 | 8238 | switch (data->verdict.code & NF_VERDICT_MASK) { |
e0abdadc PM |
8239 | case NF_ACCEPT: |
8240 | case NF_DROP: | |
8241 | case NF_QUEUE: | |
8242 | break; | |
8243 | default: | |
8244 | return -EINVAL; | |
8245 | } | |
8246 | /* fall through */ | |
96518518 PM |
8247 | case NFT_CONTINUE: |
8248 | case NFT_BREAK: | |
8249 | case NFT_RETURN: | |
96518518 PM |
8250 | break; |
8251 | case NFT_JUMP: | |
8252 | case NFT_GOTO: | |
8253 | if (!tb[NFTA_VERDICT_CHAIN]) | |
8254 | return -EINVAL; | |
f102d66b FW |
8255 | chain = nft_chain_lookup(ctx->net, ctx->table, |
8256 | tb[NFTA_VERDICT_CHAIN], genmask); | |
96518518 PM |
8257 | if (IS_ERR(chain)) |
8258 | return PTR_ERR(chain); | |
f323d954 | 8259 | if (nft_is_base_chain(chain)) |
96518518 PM |
8260 | return -EOPNOTSUPP; |
8261 | ||
96518518 | 8262 | chain->use++; |
1ca2e170 | 8263 | data->verdict.chain = chain; |
96518518 | 8264 | break; |
96518518 PM |
8265 | } |
8266 | ||
4c4ed074 | 8267 | desc->len = sizeof(data->verdict); |
96518518 PM |
8268 | desc->type = NFT_DATA_VERDICT; |
8269 | return 0; | |
8270 | } | |
8271 | ||
8272 | static void nft_verdict_uninit(const struct nft_data *data) | |
8273 | { | |
1ca2e170 | 8274 | switch (data->verdict.code) { |
96518518 PM |
8275 | case NFT_JUMP: |
8276 | case NFT_GOTO: | |
1ca2e170 | 8277 | data->verdict.chain->use--; |
96518518 PM |
8278 | break; |
8279 | } | |
8280 | } | |
8281 | ||
33d5a7b1 | 8282 | int nft_verdict_dump(struct sk_buff *skb, int type, const struct nft_verdict *v) |
96518518 PM |
8283 | { |
8284 | struct nlattr *nest; | |
8285 | ||
ae0be8de | 8286 | nest = nla_nest_start_noflag(skb, type); |
96518518 PM |
8287 | if (!nest) |
8288 | goto nla_put_failure; | |
8289 | ||
33d5a7b1 | 8290 | if (nla_put_be32(skb, NFTA_VERDICT_CODE, htonl(v->code))) |
96518518 PM |
8291 | goto nla_put_failure; |
8292 | ||
33d5a7b1 | 8293 | switch (v->code) { |
96518518 PM |
8294 | case NFT_JUMP: |
8295 | case NFT_GOTO: | |
1ca2e170 | 8296 | if (nla_put_string(skb, NFTA_VERDICT_CHAIN, |
33d5a7b1 | 8297 | v->chain->name)) |
96518518 PM |
8298 | goto nla_put_failure; |
8299 | } | |
8300 | nla_nest_end(skb, nest); | |
8301 | return 0; | |
8302 | ||
8303 | nla_put_failure: | |
8304 | return -1; | |
8305 | } | |
8306 | ||
d0a11fc3 PM |
8307 | static int nft_value_init(const struct nft_ctx *ctx, |
8308 | struct nft_data *data, unsigned int size, | |
96518518 PM |
8309 | struct nft_data_desc *desc, const struct nlattr *nla) |
8310 | { | |
8311 | unsigned int len; | |
8312 | ||
8313 | len = nla_len(nla); | |
8314 | if (len == 0) | |
8315 | return -EINVAL; | |
d0a11fc3 | 8316 | if (len > size) |
96518518 PM |
8317 | return -EOVERFLOW; |
8318 | ||
d0a11fc3 | 8319 | nla_memcpy(data->data, nla, len); |
96518518 PM |
8320 | desc->type = NFT_DATA_VALUE; |
8321 | desc->len = len; | |
8322 | return 0; | |
8323 | } | |
8324 | ||
8325 | static int nft_value_dump(struct sk_buff *skb, const struct nft_data *data, | |
8326 | unsigned int len) | |
8327 | { | |
8328 | return nla_put(skb, NFTA_DATA_VALUE, len, data->data); | |
8329 | } | |
8330 | ||
8331 | static const struct nla_policy nft_data_policy[NFTA_DATA_MAX + 1] = { | |
d0a11fc3 | 8332 | [NFTA_DATA_VALUE] = { .type = NLA_BINARY }, |
96518518 PM |
8333 | [NFTA_DATA_VERDICT] = { .type = NLA_NESTED }, |
8334 | }; | |
8335 | ||
8336 | /** | |
8337 | * nft_data_init - parse nf_tables data netlink attributes | |
8338 | * | |
8339 | * @ctx: context of the expression using the data | |
8340 | * @data: destination struct nft_data | |
d0a11fc3 | 8341 | * @size: maximum data length |
96518518 PM |
8342 | * @desc: data description |
8343 | * @nla: netlink attribute containing data | |
8344 | * | |
8345 | * Parse the netlink data attributes and initialize a struct nft_data. | |
8346 | * The type and length of data are returned in the data description. | |
8347 | * | |
8348 | * The caller can indicate that it only wants to accept data of type | |
8349 | * NFT_DATA_VALUE by passing NULL for the ctx argument. | |
8350 | */ | |
d0a11fc3 PM |
8351 | int nft_data_init(const struct nft_ctx *ctx, |
8352 | struct nft_data *data, unsigned int size, | |
96518518 PM |
8353 | struct nft_data_desc *desc, const struct nlattr *nla) |
8354 | { | |
8355 | struct nlattr *tb[NFTA_DATA_MAX + 1]; | |
8356 | int err; | |
8357 | ||
8cb08174 JB |
8358 | err = nla_parse_nested_deprecated(tb, NFTA_DATA_MAX, nla, |
8359 | nft_data_policy, NULL); | |
96518518 PM |
8360 | if (err < 0) |
8361 | return err; | |
8362 | ||
8363 | if (tb[NFTA_DATA_VALUE]) | |
d0a11fc3 PM |
8364 | return nft_value_init(ctx, data, size, desc, |
8365 | tb[NFTA_DATA_VALUE]); | |
96518518 PM |
8366 | if (tb[NFTA_DATA_VERDICT] && ctx != NULL) |
8367 | return nft_verdict_init(ctx, data, desc, tb[NFTA_DATA_VERDICT]); | |
8368 | return -EINVAL; | |
8369 | } | |
8370 | EXPORT_SYMBOL_GPL(nft_data_init); | |
8371 | ||
8372 | /** | |
59105446 | 8373 | * nft_data_release - release a nft_data item |
96518518 PM |
8374 | * |
8375 | * @data: struct nft_data to release | |
8376 | * @type: type of data | |
8377 | * | |
8378 | * Release a nft_data item. NFT_DATA_VALUE types can be silently discarded, | |
8379 | * all others need to be released by calling this function. | |
8380 | */ | |
59105446 | 8381 | void nft_data_release(const struct nft_data *data, enum nft_data_types type) |
96518518 | 8382 | { |
960bd2c2 | 8383 | if (type < NFT_DATA_VERDICT) |
96518518 | 8384 | return; |
960bd2c2 | 8385 | switch (type) { |
96518518 PM |
8386 | case NFT_DATA_VERDICT: |
8387 | return nft_verdict_uninit(data); | |
8388 | default: | |
8389 | WARN_ON(1); | |
8390 | } | |
8391 | } | |
59105446 | 8392 | EXPORT_SYMBOL_GPL(nft_data_release); |
96518518 PM |
8393 | |
8394 | int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data, | |
8395 | enum nft_data_types type, unsigned int len) | |
8396 | { | |
8397 | struct nlattr *nest; | |
8398 | int err; | |
8399 | ||
ae0be8de | 8400 | nest = nla_nest_start_noflag(skb, attr); |
96518518 PM |
8401 | if (nest == NULL) |
8402 | return -1; | |
8403 | ||
8404 | switch (type) { | |
8405 | case NFT_DATA_VALUE: | |
8406 | err = nft_value_dump(skb, data, len); | |
8407 | break; | |
8408 | case NFT_DATA_VERDICT: | |
33d5a7b1 | 8409 | err = nft_verdict_dump(skb, NFTA_DATA_VERDICT, &data->verdict); |
96518518 PM |
8410 | break; |
8411 | default: | |
8412 | err = -EINVAL; | |
8413 | WARN_ON(1); | |
8414 | } | |
8415 | ||
8416 | nla_nest_end(skb, nest); | |
8417 | return err; | |
8418 | } | |
8419 | EXPORT_SYMBOL_GPL(nft_data_dump); | |
8420 | ||
5ebe0b0e PNA |
8421 | int __nft_release_basechain(struct nft_ctx *ctx) |
8422 | { | |
8423 | struct nft_rule *rule, *nr; | |
8424 | ||
fa5950e4 FW |
8425 | if (WARN_ON(!nft_is_base_chain(ctx->chain))) |
8426 | return 0; | |
5ebe0b0e | 8427 | |
c974a3a3 | 8428 | nf_tables_unregister_hook(ctx->net, ctx->chain->table, ctx->chain); |
5ebe0b0e PNA |
8429 | list_for_each_entry_safe(rule, nr, &ctx->chain->rules, list) { |
8430 | list_del(&rule->list); | |
8431 | ctx->chain->use--; | |
bb7b40ae | 8432 | nf_tables_rule_release(ctx, rule); |
5ebe0b0e | 8433 | } |
1b2470e5 | 8434 | nft_chain_del(ctx->chain); |
5ebe0b0e | 8435 | ctx->table->use--; |
43a605f2 | 8436 | nf_tables_chain_destroy(ctx); |
5ebe0b0e PNA |
8437 | |
8438 | return 0; | |
8439 | } | |
8440 | EXPORT_SYMBOL_GPL(__nft_release_basechain); | |
8441 | ||
98319cb9 | 8442 | static void __nft_release_tables(struct net *net) |
df05ef87 | 8443 | { |
3b49e2e9 | 8444 | struct nft_flowtable *flowtable, *nf; |
df05ef87 PNA |
8445 | struct nft_table *table, *nt; |
8446 | struct nft_chain *chain, *nc; | |
e5009240 | 8447 | struct nft_object *obj, *ne; |
df05ef87 PNA |
8448 | struct nft_rule *rule, *nr; |
8449 | struct nft_set *set, *ns; | |
8450 | struct nft_ctx ctx = { | |
8451 | .net = net, | |
43a605f2 | 8452 | .family = NFPROTO_NETDEV, |
df05ef87 PNA |
8453 | }; |
8454 | ||
36596dad | 8455 | list_for_each_entry_safe(table, nt, &net->nft.tables, list) { |
98319cb9 | 8456 | ctx.family = table->family; |
dd4cbef7 | 8457 | |
df05ef87 | 8458 | list_for_each_entry(chain, &table->chains, list) |
c974a3a3 | 8459 | nf_tables_unregister_hook(net, table, chain); |
df05ef87 PNA |
8460 | /* No packets are walking on these chains anymore. */ |
8461 | ctx.table = table; | |
8462 | list_for_each_entry(chain, &table->chains, list) { | |
8463 | ctx.chain = chain; | |
8464 | list_for_each_entry_safe(rule, nr, &chain->rules, list) { | |
8465 | list_del(&rule->list); | |
8466 | chain->use--; | |
bb7b40ae | 8467 | nf_tables_rule_release(&ctx, rule); |
df05ef87 PNA |
8468 | } |
8469 | } | |
3b49e2e9 PNA |
8470 | list_for_each_entry_safe(flowtable, nf, &table->flowtables, list) { |
8471 | list_del(&flowtable->list); | |
8472 | table->use--; | |
8473 | nf_tables_flowtable_destroy(flowtable); | |
8474 | } | |
df05ef87 PNA |
8475 | list_for_each_entry_safe(set, ns, &table->sets, list) { |
8476 | list_del(&set->list); | |
8477 | table->use--; | |
0c2a85ed | 8478 | nft_set_destroy(&ctx, set); |
df05ef87 | 8479 | } |
e5009240 | 8480 | list_for_each_entry_safe(obj, ne, &table->objects, list) { |
d152159b | 8481 | nft_obj_del(obj); |
e5009240 | 8482 | table->use--; |
00bfb320 | 8483 | nft_obj_destroy(&ctx, obj); |
e5009240 | 8484 | } |
df05ef87 | 8485 | list_for_each_entry_safe(chain, nc, &table->chains, list) { |
43a605f2 | 8486 | ctx.chain = chain; |
1b2470e5 | 8487 | nft_chain_del(chain); |
df05ef87 | 8488 | table->use--; |
43a605f2 | 8489 | nf_tables_chain_destroy(&ctx); |
df05ef87 PNA |
8490 | } |
8491 | list_del(&table->list); | |
8492 | nf_tables_table_destroy(&ctx); | |
8493 | } | |
8494 | } | |
8495 | ||
dd4cbef7 PNA |
8496 | static int __net_init nf_tables_init_net(struct net *net) |
8497 | { | |
8498 | INIT_LIST_HEAD(&net->nft.tables); | |
8499 | INIT_LIST_HEAD(&net->nft.commit_list); | |
eb014de4 | 8500 | INIT_LIST_HEAD(&net->nft.module_list); |
f102d66b | 8501 | mutex_init(&net->nft.commit_mutex); |
dd4cbef7 | 8502 | net->nft.base_seq = 1; |
a654de8f PNA |
8503 | net->nft.validate_state = NFT_VALIDATE_SKIP; |
8504 | ||
dd4cbef7 PNA |
8505 | return 0; |
8506 | } | |
8507 | ||
8508 | static void __net_exit nf_tables_exit_net(struct net *net) | |
8509 | { | |
f102d66b | 8510 | mutex_lock(&net->nft.commit_mutex); |
71ad00c5 | 8511 | if (!list_empty(&net->nft.commit_list)) |
eb014de4 | 8512 | __nf_tables_abort(net, false); |
98319cb9 | 8513 | __nft_release_tables(net); |
f102d66b | 8514 | mutex_unlock(&net->nft.commit_mutex); |
dd4cbef7 | 8515 | WARN_ON_ONCE(!list_empty(&net->nft.tables)); |
1d305ba4 | 8516 | WARN_ON_ONCE(!list_empty(&net->nft.module_list)); |
dd4cbef7 PNA |
8517 | } |
8518 | ||
99633ab2 PNA |
8519 | static struct pernet_operations nf_tables_net_ops = { |
8520 | .init = nf_tables_init_net, | |
613d0776 | 8521 | .exit = nf_tables_exit_net, |
99633ab2 PNA |
8522 | }; |
8523 | ||
96518518 PM |
8524 | static int __init nf_tables_module_init(void) |
8525 | { | |
8526 | int err; | |
8527 | ||
0935d558 | 8528 | spin_lock_init(&nf_tables_destroy_list_lock); |
d209df3e FW |
8529 | err = register_pernet_subsys(&nf_tables_net_ops); |
8530 | if (err < 0) | |
8531 | return err; | |
8532 | ||
8533 | err = nft_chain_filter_init(); | |
8534 | if (err < 0) | |
8535 | goto err1; | |
02c7b25e | 8536 | |
96518518 PM |
8537 | err = nf_tables_core_module_init(); |
8538 | if (err < 0) | |
d209df3e | 8539 | goto err2; |
96518518 | 8540 | |
d209df3e | 8541 | err = register_netdevice_notifier(&nf_tables_flowtable_notifier); |
96518518 | 8542 | if (err < 0) |
d209df3e | 8543 | goto err3; |
96518518 | 8544 | |
4d44175a FW |
8545 | err = rhltable_init(&nft_objname_ht, &nft_objname_ht_params); |
8546 | if (err < 0) | |
8547 | goto err4; | |
8548 | ||
06d392cb | 8549 | err = nft_offload_init(); |
8550 | if (err < 0) | |
8551 | goto err5; | |
8552 | ||
d209df3e FW |
8553 | /* must be last */ |
8554 | err = nfnetlink_subsys_register(&nf_tables_subsys); | |
8555 | if (err < 0) | |
06d392cb | 8556 | goto err6; |
3b49e2e9 | 8557 | |
c1deb065 | 8558 | nft_chain_route_init(); |
3474a2c6 | 8559 | |
d209df3e | 8560 | return err; |
06d392cb | 8561 | err6: |
8562 | nft_offload_exit(); | |
4d44175a FW |
8563 | err5: |
8564 | rhltable_destroy(&nft_objname_ht); | |
d209df3e FW |
8565 | err4: |
8566 | unregister_netdevice_notifier(&nf_tables_flowtable_notifier); | |
8567 | err3: | |
96518518 | 8568 | nf_tables_core_module_exit(); |
d209df3e FW |
8569 | err2: |
8570 | nft_chain_filter_fini(); | |
8571 | err1: | |
8572 | unregister_pernet_subsys(&nf_tables_net_ops); | |
96518518 PM |
8573 | return err; |
8574 | } | |
8575 | ||
8576 | static void __exit nf_tables_module_exit(void) | |
8577 | { | |
8578 | nfnetlink_subsys_unregister(&nf_tables_subsys); | |
06d392cb | 8579 | nft_offload_exit(); |
3b49e2e9 | 8580 | unregister_netdevice_notifier(&nf_tables_flowtable_notifier); |
0a2cf5ee | 8581 | nft_chain_filter_fini(); |
c1deb065 | 8582 | nft_chain_route_fini(); |
71ad00c5 | 8583 | unregister_pernet_subsys(&nf_tables_net_ops); |
0935d558 | 8584 | cancel_work_sync(&trans_destroy_work); |
1b1bc49c | 8585 | rcu_barrier(); |
4d44175a | 8586 | rhltable_destroy(&nft_objname_ht); |
96518518 | 8587 | nf_tables_core_module_exit(); |
96518518 PM |
8588 | } |
8589 | ||
8590 | module_init(nf_tables_module_init); | |
8591 | module_exit(nf_tables_module_exit); | |
8592 | ||
8593 | MODULE_LICENSE("GPL"); | |
8594 | MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); | |
8595 | MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES); |