| @@ -4389,6 +4389,7 @@ static int nf_tables_newset(struct sk_buff *skb, const struct nfnl_info *info, · 4388 unmodified lines |
| 4389 | 4389 | } |
| 4390 | 4390 | |
| 4391 | 4391 | INIT_LIST_HEAD(&set->bindings); |
| 4392 | + INIT_LIST_HEAD(&set->catchall_list); |
| 4392 | 4393 | set->table = table; |
| 4393 | 4394 | write_pnet(&set->net, net); |
| 4394 | 4395 | set->ops = ops; |
| @@ -4434,6 +4435,24 @@ err_set_name: · 39 unmodified lines |
| 4434 | 4435 | return err; |
| 4435 | 4436 | } |
| 4436 | 4437 | |
| 4438 | +struct nft_set_elem_catchall { |
| 4439 | + struct list_head list; |
| 4440 | + struct rcu_head rcu; |
| 4441 | + void *elem; |
| 4442 | +}; |
| 4443 | + |
| 4444 | +static void nft_set_catchall_destroy(const struct nft_ctx *ctx, |
| 4445 | + struct nft_set *set) |
| 4446 | +{ |
| 4447 | + struct nft_set_elem_catchall *catchall; |
| 4448 | + |
| 4449 | + list_for_each_entry_rcu(catchall, &set->catchall_list, list) { |
| 4450 | + list_del_rcu(&catchall->list); |
| 4451 | + nft_set_elem_destroy(set, catchall->elem, true); |
| 4452 | + kfree_rcu(catchall); |
| 4453 | + } |
| 4454 | +} |
| 4455 | + |
| 4437 | 4456 | static void nft_set_destroy(const struct nft_ctx *ctx, struct nft_set *set) |
| 4438 | 4457 | { |
| 4439 | 4458 | int i; |
| @@ -4445,6 +4464,7 @@ static void nft_set_destroy(const struct nft_ctx *ctx, struct nft_set *set) · 5 unmodified lines |
| 4445 | 4464 | nft_expr_destroy(ctx, set->exprs[i]); |
| 4446 | 4465 | |
| 4447 | 4466 | set->ops->destroy(set); |
| 4467 | + nft_set_catchall_destroy(ctx, set); |
| 4448 | 4468 | kfree(set->name); |
| 4449 | 4469 | kvfree(set); |
| 4450 | 4470 | } |
| @@ -4521,6 +4541,29 @@ static int nf_tables_bind_check_setelem(const struct nft_ctx *ctx, · 70 unmodified lines |
| 4521 | 4541 | return nft_setelem_data_validate(ctx, set, elem); |
| 4522 | 4542 | } |
| 4523 | 4543 | |
| 4544 | +static int nft_set_catchall_bind_check(const struct nft_ctx *ctx, |
| 4545 | + struct nft_set *set) |
| 4546 | +{ |
| 4547 | + u8 genmask = nft_genmask_next(ctx->net); |
| 4548 | + struct nft_set_elem_catchall *catchall; |
| 4549 | + struct nft_set_elem elem; |
| 4550 | + struct nft_set_ext *ext; |
| 4551 | + int ret = 0; |
| 4552 | + |
| 4553 | + list_for_each_entry_rcu(catchall, &set->catchall_list, list) { |
| 4554 | + ext = nft_set_elem_ext(set, catchall->elem); |
| 4555 | + if (!nft_set_elem_active(ext, genmask)) |
| 4556 | + continue; |
| 4557 | + |
| 4558 | + elem.priv = catchall->elem; |
| 4559 | + ret = nft_setelem_data_validate(ctx, set, &elem); |
| 4560 | + if (ret < 0) |
| 4561 | + break; |
| 4562 | + } |
| 4563 | + |
| 4564 | + return ret; |
| 4565 | +} |
| 4566 | + |
| 4524 | 4567 | int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set, |
| 4525 | 4568 | struct nft_set_binding *binding) |
| 4526 | 4569 | { |
| @@ -4550,6 +4593,9 @@ int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set, · 23 unmodified lines |
| 4550 | 4593 | iter.fn = nf_tables_bind_check_setelem; |
| 4551 | 4594 | |
| 4552 | 4595 | set->ops->walk(ctx, set, &iter); |
| 4596 | + if (!iter.err) |
| 4597 | + iter.err = nft_set_catchall_bind_check(ctx, set); |
| 4598 | + |
| 4553 | 4599 | if (iter.err < 0) |
| 4554 | 4600 | return iter.err; |
| 4555 | 4601 | } |
| @@ -4736,7 +4782,8 @@ static int nf_tables_fill_setelem(struct sk_buff *skb, · 180 unmodified lines |
| 4736 | 4782 | if (nest == NULL) |
| 4737 | 4783 | goto nla_put_failure; |
| 4738 | 4784 | |
| 4739 | | − if (nft_data_dump(skb, NFTA_SET_ELEM_KEY, nft_set_ext_key(ext), |
| 4785 | + if (nft_set_ext_exists(ext, NFT_SET_EXT_KEY) && |
| 4786 | + nft_data_dump(skb, NFTA_SET_ELEM_KEY, nft_set_ext_key(ext), |
| 4740 | 4787 | NFT_DATA_VALUE, set->klen) < 0) |
| 4741 | 4788 | goto nla_put_failure; |
| 4742 | 4789 | |
| @@ -4825,6 +4872,29 @@ struct nft_set_dump_ctx { · 82 unmodified lines |
| 4825 | 4872 | struct nft_ctx ctx; |
| 4826 | 4873 | }; |
| 4827 | 4874 | |
| 4875 | +static int nft_set_catchall_dump(struct net *net, struct sk_buff *skb, |
| 4876 | + const struct nft_set *set) |
| 4877 | +{ |
| 4878 | + struct nft_set_elem_catchall *catchall; |
| 4879 | + u8 genmask = nft_genmask_cur(net); |
| 4880 | + struct nft_set_elem elem; |
| 4881 | + struct nft_set_ext *ext; |
| 4882 | + int ret = 0; |
| 4883 | + |
| 4884 | + list_for_each_entry_rcu(catchall, &set->catchall_list, list) { |
| 4885 | + ext = nft_set_elem_ext(set, catchall->elem); |
| 4886 | + if (!nft_set_elem_active(ext, genmask) || |
| 4887 | + nft_set_elem_expired(ext)) |
| 4888 | + continue; |
| 4889 | + |
| 4890 | + elem.priv = catchall->elem; |
| 4891 | + ret = nf_tables_fill_setelem(skb, set, &elem); |
| 4892 | + break; |
| 4893 | + } |
| 4894 | + |
| 4895 | + return ret; |
| 4896 | +} |
| 4897 | + |
| 4828 | 4898 | static int nf_tables_dump_set(struct sk_buff *skb, struct netlink_callback *cb) |
| 4829 | 4899 | { |
| 4830 | 4900 | struct nft_set_dump_ctx *dump_ctx = cb->data; |
| @@ -4889,6 +4959,9 @@ static int nf_tables_dump_set(struct sk_buff *skb, struct netlink_callback *cb) · 58 unmodified lines |
| 4889 | 4959 | args.iter.err = 0; |
| 4890 | 4960 | args.iter.fn = nf_tables_dump_setelem; |
| 4891 | 4961 | set->ops->walk(&dump_ctx->ctx, set, &args.iter); |
| 4962 | + |
| 4963 | + if (!args.iter.err && args.iter.count == cb->args[0]) |
| 4964 | + args.iter.err = nft_set_catchall_dump(net, skb, set); |
| 4892 | 4965 | rcu_read_unlock(); |
| 4893 | 4966 | |
| 4894 | 4967 | nla_nest_end(skb, nest); |
| @@ -4968,8 +5041,8 @@ static int nft_setelem_parse_flags(const struct nft_set *set, · 73 unmodified lines |
| 4968 | 5041 | return 0; |
| 4969 | 5042 | |
| 4970 | 5043 | *flags = ntohl(nla_get_be32(attr)); |
| 4971 | | − if (*flags & ~NFT_SET_ELEM_INTERVAL_END) |
| 4972 | | − return -EINVAL; |
| 5044 | + if (*flags & ~(NFT_SET_ELEM_INTERVAL_END | NFT_SET_ELEM_CATCHALL)) |
| 5045 | + return -EOPNOTSUPP; |
| 4973 | 5046 | if (!(set->flags & NFT_SET_INTERVAL) && |
| 4974 | 5047 | *flags & NFT_SET_ELEM_INTERVAL_END) |
| 4975 | 5048 | return -EINVAL; |
| @@ -5014,6 +5087,46 @@ static int nft_setelem_parse_data(struct nft_ctx *ctx, struct nft_set *set, · 38 unmodified lines |
| 5014 | 5087 | return 0; |
| 5015 | 5088 | } |
| 5016 | 5089 | |
| 5090 | +static void *nft_setelem_catchall_get(const struct net *net, |
| 5091 | + const struct nft_set *set) |
| 5092 | +{ |
| 5093 | + struct nft_set_elem_catchall *catchall; |
| 5094 | + u8 genmask = nft_genmask_cur(net); |
| 5095 | + struct nft_set_ext *ext; |
| 5096 | + void *priv = NULL; |
| 5097 | + |
| 5098 | + list_for_each_entry_rcu(catchall, &set->catchall_list, list) { |
| 5099 | + ext = nft_set_elem_ext(set, catchall->elem); |
| 5100 | + if (!nft_set_elem_active(ext, genmask) || |
| 5101 | + nft_set_elem_expired(ext)) |
| 5102 | + continue; |
| 5103 | + |
| 5104 | + priv = catchall->elem; |
| 5105 | + break; |
| 5106 | + } |
| 5107 | + |
| 5108 | + return priv; |
| 5109 | +} |
| 5110 | + |
| 5111 | +static int nft_setelem_get(struct nft_ctx *ctx, struct nft_set *set, |
| 5112 | + struct nft_set_elem *elem, u32 flags) |
| 5113 | +{ |
| 5114 | + void *priv; |
| 5115 | + |
| 5116 | + if (!(flags & NFT_SET_ELEM_CATCHALL)) { |
| 5117 | + priv = set->ops->get(ctx->net, set, elem, flags); |
| 5118 | + if (IS_ERR(priv)) |
| 5119 | + return PTR_ERR(priv); |
| 5120 | + } else { |
| 5121 | + priv = nft_setelem_catchall_get(ctx->net, set); |
| 5122 | + if (!priv) |
| 5123 | + return -ENOENT; |
| 5124 | + } |
| 5125 | + elem->priv = priv; |
| 5126 | + |
| 5127 | + return 0; |
| 5128 | +} |
| 5129 | + |
| 5017 | 5130 | static int nft_get_set_elem(struct nft_ctx *ctx, struct nft_set *set, |
| 5018 | 5131 | const struct nlattr *attr) |
| 5019 | 5132 | { |
| @@ -5021,7 +5134,6 @@ static int nft_get_set_elem(struct nft_ctx *ctx, struct nft_set *set, · 1 unmodified line |
| 5021 | 5134 | struct nft_set_elem elem; |
| 5022 | 5135 | struct sk_buff *skb; |
| 5023 | 5136 | uint32_t flags = 0; |
| 5024 | | − void *priv; |
| 5025 | 5137 | int err; |
| 5026 | 5138 | |
| 5027 | 5139 | err = nla_parse_nested_deprecated(nla, NFTA_SET_ELEM_MAX, attr, |
| @@ -5029,17 +5141,19 @@ static int nft_get_set_elem(struct nft_ctx *ctx, struct nft_set *set, · 1 unmodified line |
| 5029 | 5141 | if (err < 0) |
| 5030 | 5142 | return err; |
| 5031 | 5143 | |
| 5032 | | − if (!nla[NFTA_SET_ELEM_KEY]) |
| 5033 | | − return -EINVAL; |
| 5034 | | − |
| 5035 | 5144 | err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags); |
| 5036 | 5145 | if (err < 0) |
| 5037 | 5146 | return err; |
| 5038 | 5147 | |
| 5039 | | − err = nft_setelem_parse_key(ctx, set, &elem.key.val, |
| 5040 | | − nla[NFTA_SET_ELEM_KEY]); |
| 5041 | | − if (err < 0) |
| 5042 | | − return err; |
| 5148 | + if (!nla[NFTA_SET_ELEM_KEY] && !(flags & NFT_SET_ELEM_CATCHALL)) |
| 5149 | + return -EINVAL; |
| 5150 | + |
| 5151 | + if (nla[NFTA_SET_ELEM_KEY]) { |
| 5152 | + err = nft_setelem_parse_key(ctx, set, &elem.key.val, |
| 5153 | + nla[NFTA_SET_ELEM_KEY]); |
| 5154 | + if (err < 0) |
| 5155 | + return err; |
| 5156 | + } |
| 5043 | 5157 | |
| 5044 | 5158 | if (nla[NFTA_SET_ELEM_KEY_END]) { |
| 5045 | 5159 | err = nft_setelem_parse_key(ctx, set, &elem.key_end.val, |
| @@ -5048,11 +5162,9 @@ static int nft_get_set_elem(struct nft_ctx *ctx, struct nft_set *set, · 2 unmodified lines |
| 5048 | 5162 | return err; |
| 5049 | 5163 | } |
| 5050 | 5164 | |
| 5051 | | − priv = set->ops->get(ctx->net, set, &elem, flags); |
| 5052 | | − if (IS_ERR(priv)) |
| 5053 | | − return PTR_ERR(priv); |
| 5054 | | − |
| 5055 | | − elem.priv = priv; |
| 5165 | + err = nft_setelem_get(ctx, set, &elem, flags); |
| 5166 | + if (err < 0) |
| 5167 | + return err; |
| 5056 | 5168 | |
| 5057 | 5169 | err = -ENOMEM; |
| 5058 | 5170 | skb = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC); |
| @@ -5212,7 +5324,8 @@ void *nft_set_elem_init(const struct nft_set *set, · 153 unmodified lines |
| 5212 | 5324 | ext = nft_set_elem_ext(set, elem); |
| 5213 | 5325 | nft_set_ext_init(ext, tmpl); |
| 5214 | 5326 | |
| 5215 | | − memcpy(nft_set_ext_key(ext), key, set->klen); |
| 5327 | + if (nft_set_ext_exists(ext, NFT_SET_EXT_KEY)) |
| 5328 | + memcpy(nft_set_ext_key(ext), key, set->klen); |
| 5216 | 5329 | if (nft_set_ext_exists(ext, NFT_SET_EXT_KEY_END)) |
| 5217 | 5330 | memcpy(nft_set_ext_key_end(ext), key_end, set->klen); |
| 5218 | 5331 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA)) |
| @@ -5343,6 +5456,192 @@ err_elem_expr_setup: · 124 unmodified lines |
| 5343 | 5456 | return -ENOMEM; |
| 5344 | 5457 | } |
| 5345 | 5458 | |
| 5459 | +struct nft_set_ext *nft_set_catchall_lookup(const struct net *net, |
| 5460 | + const struct nft_set *set) |
| 5461 | +{ |
| 5462 | + struct nft_set_elem_catchall *catchall; |
| 5463 | + u8 genmask = nft_genmask_cur(net); |
| 5464 | + struct nft_set_ext *ext; |
| 5465 | + |
| 5466 | + list_for_each_entry_rcu(catchall, &set->catchall_list, list) { |
| 5467 | + ext = nft_set_elem_ext(set, catchall->elem); |
| 5468 | + if (nft_set_elem_active(ext, genmask) && |
| 5469 | + !nft_set_elem_expired(ext)) |
| 5470 | + return ext; |
| 5471 | + } |
| 5472 | + |
| 5473 | + return NULL; |
| 5474 | +} |
| 5475 | +EXPORT_SYMBOL_GPL(nft_set_catchall_lookup); |
| 5476 | + |
| 5477 | +void *nft_set_catchall_gc(const struct nft_set *set) |
| 5478 | +{ |
| 5479 | + struct nft_set_elem_catchall *catchall, *next; |
| 5480 | + struct nft_set_ext *ext; |
| 5481 | + void *elem = NULL; |
| 5482 | + |
| 5483 | + list_for_each_entry_safe(catchall, next, &set->catchall_list, list) { |
| 5484 | + ext = nft_set_elem_ext(set, catchall->elem); |
| 5485 | + |
| 5486 | + if (!nft_set_elem_expired(ext) || |
| 5487 | + nft_set_elem_mark_busy(ext)) |
| 5488 | + continue; |
| 5489 | + |
| 5490 | + elem = catchall->elem; |
| 5491 | + list_del_rcu(&catchall->list); |
| 5492 | + kfree_rcu(catchall, rcu); |
| 5493 | + break; |
| 5494 | + } |
| 5495 | + |
| 5496 | + return elem; |
| 5497 | +} |
| 5498 | +EXPORT_SYMBOL_GPL(nft_set_catchall_gc); |
| 5499 | + |
| 5500 | +static int nft_setelem_catchall_insert(const struct net *net, |
| 5501 | + struct nft_set *set, |
| 5502 | + const struct nft_set_elem *elem, |
| 5503 | + struct nft_set_ext **pext) |
| 5504 | +{ |
| 5505 | + struct nft_set_elem_catchall *catchall; |
| 5506 | + u8 genmask = nft_genmask_next(net); |
| 5507 | + struct nft_set_ext *ext; |
| 5508 | + |
| 5509 | + list_for_each_entry(catchall, &set->catchall_list, list) { |
| 5510 | + ext = nft_set_elem_ext(set, catchall->elem); |
| 5511 | + if (nft_set_elem_active(ext, genmask)) { |
| 5512 | + *pext = ext; |
| 5513 | + return -EEXIST; |
| 5514 | + } |
| 5515 | + } |
| 5516 | + |
| 5517 | + catchall = kmalloc(sizeof(*catchall), GFP_KERNEL); |
| 5518 | + if (!catchall) |
| 5519 | + return -ENOMEM; |
| 5520 | + |
| 5521 | + catchall->elem = elem->priv; |
| 5522 | + list_add_tail_rcu(&catchall->list, &set->catchall_list); |
| 5523 | + |
| 5524 | + return 0; |
| 5525 | +} |
| 5526 | + |
| 5527 | +static int nft_setelem_insert(const struct net *net, |
| 5528 | + struct nft_set *set, |
| 5529 | + const struct nft_set_elem *elem, |
| 5530 | + struct nft_set_ext **ext, unsigned int flags) |
| 5531 | +{ |
| 5532 | + int ret; |
| 5533 | + |
| 5534 | + if (flags & NFT_SET_ELEM_CATCHALL) |
| 5535 | + ret = nft_setelem_catchall_insert(net, set, elem, ext); |
| 5536 | + else |
| 5537 | + ret = set->ops->insert(net, set, elem, ext); |
| 5538 | + |
| 5539 | + return ret; |
| 5540 | +} |
| 5541 | + |
| 5542 | +static bool nft_setelem_is_catchall(const struct nft_set *set, |
| 5543 | + const struct nft_set_elem *elem) |
| 5544 | +{ |
| 5545 | + struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); |
| 5546 | + |
| 5547 | + if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) && |
| 5548 | + *nft_set_ext_flags(ext) & NFT_SET_ELEM_CATCHALL) |
| 5549 | + return true; |
| 5550 | + |
| 5551 | + return false; |
| 5552 | +} |
| 5553 | + |
| 5554 | +static void nft_setelem_activate(struct net *net, struct nft_set *set, |
| 5555 | + struct nft_set_elem *elem) |
| 5556 | +{ |
| 5557 | + struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv); |
| 5558 | + |
| 5559 | + if (nft_setelem_is_catchall(set, elem)) { |
| 5560 | + nft_set_elem_change_active(net, set, ext); |
| 5561 | + nft_set_elem_clear_busy(ext); |
| 5562 | + } else { |
| 5563 | + set->ops->activate(net, set, elem); |
| 5564 | + } |
| 5565 | +} |
| 5566 | + |
| 5567 | +static int nft_setelem_catchall_deactivate(const struct net *net, |
| 5568 | + struct nft_set *set, |
| 5569 | + struct nft_set_elem *elem) |
| 5570 | +{ |
| 5571 | + struct nft_set_elem_catchall *catchall; |
| 5572 | + struct nft_set_ext *ext; |
| 5573 | + |
| 5574 | + list_for_each_entry(catchall, &set->catchall_list, list) { |
| 5575 | + ext = nft_set_elem_ext(set, catchall->elem); |
| 5576 | + if (!nft_is_active(net, ext) || |
| 5577 | + nft_set_elem_mark_busy(ext)) |
| 5578 | + continue; |
| 5579 | + |
| 5580 | + kfree(elem->priv); |
| 5581 | + elem->priv = catchall->elem; |
| 5582 | + nft_set_elem_change_active(net, set, ext); |
| 5583 | + return 0; |
| 5584 | + } |
| 5585 | + |
| 5586 | + return -ENOENT; |
| 5587 | +} |
| 5588 | + |
| 5589 | +static int __nft_setelem_deactivate(const struct net *net, |
| 5590 | + struct nft_set *set, |
| 5591 | + struct nft_set_elem *elem) |
| 5592 | +{ |
| 5593 | + void *priv; |
| 5594 | + |
| 5595 | + priv = set->ops->deactivate(net, set, elem); |
| 5596 | + if (!priv) |
| 5597 | + return -ENOENT; |
| 5598 | + |
| 5599 | + kfree(elem->priv); |
| 5600 | + elem->priv = priv; |
| 5601 | + set->ndeact++; |
| 5602 | + |
| 5603 | + return 0; |
| 5604 | +} |
| 5605 | + |
| 5606 | +static int nft_setelem_deactivate(const struct net *net, |
| 5607 | + struct nft_set *set, |
| 5608 | + struct nft_set_elem *elem, u32 flags) |
| 5609 | +{ |
| 5610 | + int ret; |
| 5611 | + |
| 5612 | + if (flags & NFT_SET_ELEM_CATCHALL) |
| 5613 | + ret = nft_setelem_catchall_deactivate(net, set, elem); |
| 5614 | + else |
| 5615 | + ret = __nft_setelem_deactivate(net, set, elem); |
| 5616 | + |
| 5617 | + return ret; |
| 5618 | +} |
| 5619 | + |
| 5620 | +static void nft_setelem_catchall_remove(const struct net *net, |
| 5621 | + const struct nft_set *set, |
| 5622 | + const struct nft_set_elem *elem) |
| 5623 | +{ |
| 5624 | + struct nft_set_elem_catchall *catchall, *next; |
| 5625 | + |
| 5626 | + list_for_each_entry_safe(catchall, next, &set->catchall_list, list) { |
| 5627 | + if (catchall->elem == elem->priv) { |
| 5628 | + list_del_rcu(&catchall->list); |
| 5629 | + kfree_rcu(catchall); |
| 5630 | + break; |
| 5631 | + } |
| 5632 | + } |
| 5633 | +} |
| 5634 | + |
| 5635 | +static void nft_setelem_remove(const struct net *net, |
| 5636 | + const struct nft_set *set, |
| 5637 | + const struct nft_set_elem *elem) |
| 5638 | +{ |
| 5639 | + if (nft_setelem_is_catchall(set, elem)) |
| 5640 | + nft_setelem_catchall_remove(net, set, elem); |
| 5641 | + else |
| 5642 | + set->ops->remove(net, set, elem); |
| 5643 | +} |
| 5644 | + |
| 5346 | 5645 | static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set, |
| 5347 | 5646 | const struct nlattr *attr, u32 nlmsg_flags) |
| 5348 | 5647 | { |
| @@ -5369,14 +5668,15 @@ static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set, · 20 unmodified lines |
| 5369 | 5668 | if (err < 0) |
| 5370 | 5669 | return err; |
| 5371 | 5670 | |
| 5372 | | − if (nla[NFTA_SET_ELEM_KEY] == NULL) |
| 5373 | | − return -EINVAL; |
| 5374 | | − |
| 5375 | 5671 | nft_set_ext_prepare(&tmpl); |
| 5376 | 5672 | |
| 5377 | 5673 | err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags); |
| 5378 | 5674 | if (err < 0) |
| 5379 | 5675 | return err; |
| 5676 | + |
| 5677 | + if (!nla[NFTA_SET_ELEM_KEY] && !(flags & NFT_SET_ELEM_CATCHALL)) |
| 5678 | + return -EINVAL; |
| 5679 | + |
| 5380 | 5680 | if (flags != 0) |
| 5381 | 5681 | nft_set_ext_add(&tmpl, NFT_SET_EXT_FLAGS); |
| 5382 | 5682 | |
| @@ -5481,12 +5781,14 @@ static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set, · 98 unmodified lines |
| 5481 | 5781 | num_exprs = set->num_exprs; |
| 5482 | 5782 | } |
| 5483 | 5783 | |
| 5484 | | − err = nft_setelem_parse_key(ctx, set, &elem.key.val, |
| 5485 | | − nla[NFTA_SET_ELEM_KEY]); |
| 5486 | | − if (err < 0) |
| 5487 | | − goto err_set_elem_expr; |
| 5784 | + if (nla[NFTA_SET_ELEM_KEY]) { |
| 5785 | + err = nft_setelem_parse_key(ctx, set, &elem.key.val, |
| 5786 | + nla[NFTA_SET_ELEM_KEY]); |
| 5787 | + if (err < 0) |
| 5788 | + goto err_set_elem_expr; |
| 5488 | 5789 | |
| 5489 | | − nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY, set->klen); |
| 5790 | + nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY, set->klen); |
| 5791 | + } |
| 5490 | 5792 | |
| 5491 | 5793 | if (nla[NFTA_SET_ELEM_KEY_END]) { |
| 5492 | 5794 | err = nft_setelem_parse_key(ctx, set, &elem.key_end.val, |
| @@ -5603,7 +5905,8 @@ static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set, · 110 unmodified lines |
| 5603 | 5905 | } |
| 5604 | 5906 | |
| 5605 | 5907 | ext->genmask = nft_genmask_cur(ctx->net) | NFT_SET_ELEM_BUSY_MASK; |
| 5606 | | − err = set->ops->insert(ctx->net, set, &elem, &ext2); |
| 5908 | + |
| 5909 | + err = nft_setelem_insert(ctx->net, set, &elem, &ext2, flags); |
| 5607 | 5910 | if (err) { |
| 5608 | 5911 | if (err == -EEXIST) { |
| 5609 | 5912 | if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA) ^ |
| @@ -5630,7 +5933,7 @@ static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set, · 20 unmodified lines |
| 5630 | 5933 | goto err_element_clash; |
| 5631 | 5934 | } |
| 5632 | 5935 | |
| 5633 | | − if (set->size && |
| 5936 | + if (!(flags & NFT_SET_ELEM_CATCHALL) && set->size && |
| 5634 | 5937 | !atomic_add_unless(&set->nelems, 1, set->size + set->ndeact)) { |
| 5635 | 5938 | err = -ENFILE; |
| 5636 | 5939 | goto err_set_full; |
| @@ -5641,7 +5944,7 @@ static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set, · 4 unmodified lines |
| 5641 | 5944 | return 0; |
| 5642 | 5945 | |
| 5643 | 5946 | err_set_full: |
| 5644 | | − set->ops->remove(ctx->net, set, &elem); |
| 5947 | + nft_setelem_remove(ctx->net, set, &elem); |
| 5645 | 5948 | err_element_clash: |
| 5646 | 5949 | kfree(trans); |
| 5647 | 5950 | err_elem_expr: |
| @@ -5773,7 +6076,6 @@ static int nft_del_setelem(struct nft_ctx *ctx, struct nft_set *set, · 125 unmodified lines |
| 5773 | 6076 | struct nft_set_ext *ext; |
| 5774 | 6077 | struct nft_trans *trans; |
| 5775 | 6078 | u32 flags = 0; |
| 5776 | | − void *priv; |
| 5777 | 6079 | int err; |
| 5778 | 6080 | |
| 5779 | 6081 | err = nla_parse_nested_deprecated(nla, NFTA_SET_ELEM_MAX, attr, |
| @@ -5781,23 +6083,26 @@ static int nft_del_setelem(struct nft_ctx *ctx, struct nft_set *set, · 1 unmodified line |
| 5781 | 6083 | if (err < 0) |
| 5782 | 6084 | return err; |
| 5783 | 6085 | |
| 5784 | | − if (nla[NFTA_SET_ELEM_KEY] == NULL) |
| 6086 | + err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags); |
| 6087 | + if (err < 0) |
| 6088 | + return err; |
| 6089 | + |
| 6090 | + if (!nla[NFTA_SET_ELEM_KEY] && !(flags & NFT_SET_ELEM_CATCHALL)) |
| 5785 | 6091 | return -EINVAL; |
| 5786 | 6092 | |
| 5787 | 6093 | nft_set_ext_prepare(&tmpl); |
| 5788 | 6094 | |
| 5789 | | − err = nft_setelem_parse_flags(set, nla[NFTA_SET_ELEM_FLAGS], &flags); |
| 5790 | | − if (err < 0) |
| 5791 | | − return err; |
| 5792 | 6095 | if (flags != 0) |
| 5793 | 6096 | nft_set_ext_add(&tmpl, NFT_SET_EXT_FLAGS); |
| 5794 | 6097 | |
| 5795 | | − err = nft_setelem_parse_key(ctx, set, &elem.key.val, |
| 5796 | | − nla[NFTA_SET_ELEM_KEY]); |
| 5797 | | − if (err < 0) |
| 5798 | | − return err; |
| 6098 | + if (nla[NFTA_SET_ELEM_KEY]) { |
| 6099 | + err = nft_setelem_parse_key(ctx, set, &elem.key.val, |
| 6100 | + nla[NFTA_SET_ELEM_KEY]); |
| 6101 | + if (err < 0) |
| 6102 | + return err; |
| 5799 | 6103 | |
| 5800 | | − nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY, set->klen); |
| 6104 | + nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY, set->klen); |
| 6105 | + } |
| 5801 | 6106 | |
| 5802 | 6107 | if (nla[NFTA_SET_ELEM_KEY_END]) { |
| 5803 | 6108 | err = nft_setelem_parse_key(ctx, set, &elem.key_end.val, |
| @@ -5823,13 +6128,9 @@ static int nft_del_setelem(struct nft_ctx *ctx, struct nft_set *set, · 19 unmodified lines |
| 5823 | 6128 | if (trans == NULL) |
| 5824 | 6129 | goto fail_trans; |
| 5825 | 6130 | |
| 5826 | | − priv = set->ops->deactivate(ctx->net, set, &elem); |
| 5827 | | − if (priv == NULL) { |
| 5828 | | − err = -ENOENT; |
| 6131 | + err = nft_setelem_deactivate(ctx->net, set, &elem, flags); |
| 6132 | + if (err < 0) |
| 5829 | 6133 | goto fail_ops; |
| 5830 | | − } |
| 5831 | | − kfree(elem.priv); |
| 5832 | | − elem.priv = priv; |
| 5833 | 6134 | |
| 5834 | 6135 | nft_setelem_data_deactivate(ctx->net, set, &elem); |
| 5835 | 6136 | |
| @@ -5876,6 +6177,49 @@ err1: · 40 unmodified lines |
| 5876 | 6177 | return err; |
| 5877 | 6178 | } |
| 5878 | 6179 | |
| 6180 | +static int __nft_set_catchall_flush(const struct nft_ctx *ctx, |
| 6181 | + struct nft_set *set, |
| 6182 | + struct nft_set_elem *elem) |
| 6183 | +{ |
| 6184 | + struct nft_trans *trans; |
| 6185 | + |
| 6186 | + trans = nft_trans_alloc_gfp(ctx, NFT_MSG_DELSETELEM, |
| 6187 | + sizeof(struct nft_trans_elem), GFP_KERNEL); |
| 6188 | + if (!trans) |
| 6189 | + return -ENOMEM; |
| 6190 | + |
| 6191 | + nft_setelem_data_deactivate(ctx->net, set, elem); |
| 6192 | + nft_trans_elem_set(trans) = set; |
| 6193 | + nft_trans_elem(trans) = *elem; |
| 6194 | + nft_trans_commit_list_add_tail(ctx->net, trans); |
| 6195 | + |
| 6196 | + return 0; |
| 6197 | +} |
| 6198 | + |
| 6199 | +static int nft_set_catchall_flush(const struct nft_ctx *ctx, |
| 6200 | + struct nft_set *set) |
| 6201 | +{ |
| 6202 | + u8 genmask = nft_genmask_next(ctx->net); |
| 6203 | + struct nft_set_elem_catchall *catchall; |
| 6204 | + struct nft_set_elem elem; |
| 6205 | + struct nft_set_ext *ext; |
| 6206 | + int ret = 0; |
| 6207 | + |
| 6208 | + list_for_each_entry_rcu(catchall, &set->catchall_list, list) { |
| 6209 | + ext = nft_set_elem_ext(set, catchall->elem); |
| 6210 | + if (!nft_set_elem_active(ext, genmask) || |
| 6211 | + nft_set_elem_mark_busy(ext)) |
| 6212 | + continue; |
| 6213 | + |
| 6214 | + elem.priv = catchall->elem; |
| 6215 | + ret = __nft_set_catchall_flush(ctx, set, &elem); |
| 6216 | + if (ret < 0) |
| 6217 | + break; |
| 6218 | + } |
| 6219 | + |
| 6220 | + return ret; |
| 6221 | +} |
| 6222 | + |
| 5879 | 6223 | static int nft_set_flush(struct nft_ctx *ctx, struct nft_set *set, u8 genmask) |
| 5880 | 6224 | { |
| 5881 | 6225 | struct nft_set_iter iter = { |
| @@ -5884,6 +6228,8 @@ static int nft_set_flush(struct nft_ctx *ctx, struct nft_set *set, u8 genmask) · 2 unmodified lines |
| 5884 | 6228 | }; |
| 5885 | 6229 | |
| 5886 | 6230 | set->ops->walk(ctx, set, &iter); |
| 6231 | + if (!iter.err) |
| 6232 | + iter.err = nft_set_catchall_flush(ctx, set); |
| 5887 | 6233 | |
| 5888 | 6234 | return iter.err; |
| 5889 | 6235 | } |
| @@ -5918,8 +6264,6 @@ static int nf_tables_delsetelem(struct sk_buff *skb, · 28 unmodified lines |
| 5918 | 6264 | err = nft_del_setelem(&ctx, set, attr); |
| 5919 | 6265 | if (err < 0) |
| 5920 | 6266 | break; |
| 5921 | | − |
| 5922 | | − set->ndeact++; |
| 5923 | 6267 | } |
| 5924 | 6268 | return err; |
| 5925 | 6269 | } |
| @@ -8270,7 +8614,7 @@ static int nf_tables_commit(struct net *net, struct sk_buff *skb) · 2344 unmodified lines |
| 8270 | 8614 | case NFT_MSG_NEWSETELEM: |
| 8271 | 8615 | te = (struct nft_trans_elem *)trans->data; |
| 8272 | 8616 | |
| 8273 | | − te->set->ops->activate(net, te->set, &te->elem); |
| 8617 | + nft_setelem_activate(net, te->set, &te->elem); |
| 8274 | 8618 | nf_tables_setelem_notify(&trans->ctx, te->set, |
| 8275 | 8619 | &te->elem, |
| 8276 | 8620 | NFT_MSG_NEWSETELEM, 0); |
| @@ -8282,9 +8626,11 @@ static int nf_tables_commit(struct net *net, struct sk_buff *skb) · 5 unmodified lines |
| 8282 | 8626 | nf_tables_setelem_notify(&trans->ctx, te->set, |
| 8283 | 8627 | &te->elem, |
| 8284 | 8628 | NFT_MSG_DELSETELEM, 0); |
| 8285 | | − te->set->ops->remove(net, te->set, &te->elem); |
| 8286 | | − atomic_dec(&te->set->nelems); |
| 8287 | | − te->set->ndeact--; |
| 8629 | + nft_setelem_remove(net, te->set, &te->elem); |
| 8630 | + if (!nft_setelem_is_catchall(te->set, &te->elem)) { |
| 8631 | + atomic_dec(&te->set->nelems); |
| 8632 | + te->set->ndeact--; |
| 8633 | + } |
| 8288 | 8634 | break; |
| 8289 | 8635 | case NFT_MSG_NEWOBJ: |
| 8290 | 8636 | if (nft_trans_obj_update(trans)) { |
| @@ -8485,15 +8831,17 @@ static int __nf_tables_abort(struct net *net, enum nfnl_abort_action action) · 194 unmodified lines |
| 8485 | 8831 | break; |
| 8486 | 8832 | } |
| 8487 | 8833 | te = (struct nft_trans_elem *)trans->data; |
| 8488 | | − te->set->ops->remove(net, te->set, &te->elem); |
| 8489 | | − atomic_dec(&te->set->nelems); |
| 8834 | + nft_setelem_remove(net, te->set, &te->elem); |
| 8835 | + if (!nft_setelem_is_catchall(te->set, &te->elem)) |
| 8836 | + atomic_dec(&te->set->nelems); |
| 8490 | 8837 | break; |
| 8491 | 8838 | case NFT_MSG_DELSETELEM: |
| 8492 | 8839 | te = (struct nft_trans_elem *)trans->data; |
| 8493 | 8840 | |
| 8494 | 8841 | nft_setelem_data_activate(net, te->set, &te->elem); |
| 8495 | | − te->set->ops->activate(net, te->set, &te->elem); |
| 8496 | | − te->set->ndeact--; |
| 8842 | + nft_setelem_activate(net, te->set, &te->elem); |
| 8843 | + if (!nft_setelem_is_catchall(te->set, &te->elem)) |
| 8844 | + te->set->ndeact--; |
| 8497 | 8845 | |
| 8498 | 8846 | nft_trans_destroy(trans); |
| 8499 | 8847 | break; |
| @@ -8672,6 +9020,27 @@ static int nf_tables_loop_check_setelem(const struct nft_ctx *ctx, · 172 unmodified lines |
| 8672 | 9020 | return nft_check_loops(ctx, ext); |
| 8673 | 9021 | } |
| 8674 | 9022 | |
| 9023 | +static int nft_set_catchall_loops(const struct nft_ctx *ctx, |
| 9024 | + struct nft_set *set) |
| 9025 | +{ |
| 9026 | + u8 genmask = nft_genmask_next(ctx->net); |
| 9027 | + struct nft_set_elem_catchall *catchall; |
| 9028 | + struct nft_set_ext *ext; |
| 9029 | + int ret = 0; |
| 9030 | + |
| 9031 | + list_for_each_entry_rcu(catchall, &set->catchall_list, list) { |
| 9032 | + ext = nft_set_elem_ext(set, catchall->elem); |
| 9033 | + if (!nft_set_elem_active(ext, genmask)) |
| 9034 | + continue; |
| 9035 | + |
| 9036 | + ret = nft_check_loops(ctx, ext); |
| 9037 | + if (ret < 0) |
| 9038 | + return ret; |
| 9039 | + } |
| 9040 | + |
| 9041 | + return ret; |
| 9042 | +} |
| 9043 | + |
| 8675 | 9044 | static int nf_tables_check_loops(const struct nft_ctx *ctx, |
| 8676 | 9045 | const struct nft_chain *chain) |
| 8677 | 9046 | { |
| @@ -8731,6 +9100,9 @@ static int nf_tables_check_loops(const struct nft_ctx *ctx, · 53 unmodified lines |
| 8731 | 9100 | iter.fn = nf_tables_loop_check_setelem; |
| 8732 | 9101 | |
| 8733 | 9102 | set->ops->walk(ctx, set, &iter); |
| 9103 | + if (!iter.err) |
| 9104 | + iter.err = nft_set_catchall_loops(ctx, set); |
| 9105 | + |
| 8734 | 9106 | if (iter.err < 0) |
| 8735 | 9107 | return iter.err; |
| 8736 | 9108 | } |