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