Skip to content

Commit 71c0efb

Browse files
q2venPaolo Abeni
authored andcommitted
ipv6: Factorise ip6_route_multipath_add().
We will get rid of RTNL from RTM_NEWROUTE and SIOCADDRT and rely on RCU to guarantee dev and nexthop lifetime. Then, the RCU section will start before ip6_route_info_create_nh() in ip6_route_multipath_add(), but ip6_route_info_create() is called in the same loop and will sleep. Let's split the loop into ip6_route_mpath_info_create() and ip6_route_mpath_info_create_nh(). Note that ip6_route_info_append() is now integrated into ip6_route_mpath_info_create_nh() because we need to call different free functions for nexthops that passed ip6_route_info_create_nh(). In case of failure, the remaining nexthops that ip6_route_info_create_nh() has not been called for will be freed by ip6_route_mpath_info_cleanup(). OTOH, if a nexthop passes ip6_route_info_create_nh(), it will be linked to a local temporary list, which will be spliced back to rt6_nh_list. In case of failure, these nexthops will be released by fib6_info_release() in ip6_route_multipath_add(). Signed-off-by: Kuniyuki Iwashima <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Paolo Abeni <[email protected]>
1 parent 5a1ccff commit 71c0efb

File tree

1 file changed

+130
-75
lines changed

1 file changed

+130
-75
lines changed

net/ipv6/route.c

Lines changed: 130 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -5316,29 +5316,131 @@ struct rt6_nh {
53165316
struct fib6_info *fib6_info;
53175317
struct fib6_config r_cfg;
53185318
struct list_head list;
5319+
int weight;
53195320
};
53205321

5321-
static int ip6_route_info_append(struct list_head *rt6_nh_list,
5322-
struct fib6_info *rt,
5323-
struct fib6_config *r_cfg)
5322+
static void ip6_route_mpath_info_cleanup(struct list_head *rt6_nh_list)
53245323
{
5325-
struct rt6_nh *nh;
5326-
int err = -EEXIST;
5324+
struct rt6_nh *nh, *nh_next;
53275325

5328-
list_for_each_entry(nh, rt6_nh_list, list) {
5329-
/* check if fib6_info already exists */
5330-
if (rt6_duplicate_nexthop(nh->fib6_info, rt))
5331-
return err;
5326+
list_for_each_entry_safe(nh, nh_next, rt6_nh_list, list) {
5327+
struct fib6_info *rt = nh->fib6_info;
5328+
5329+
if (rt) {
5330+
free_percpu(rt->fib6_nh->nh_common.nhc_pcpu_rth_output);
5331+
free_percpu(rt->fib6_nh->rt6i_pcpu);
5332+
ip_fib_metrics_put(rt->fib6_metrics);
5333+
kfree(rt);
5334+
}
5335+
5336+
list_del(&nh->list);
5337+
kfree(nh);
53325338
}
5339+
}
53335340

5334-
nh = kzalloc(sizeof(*nh), GFP_KERNEL);
5335-
if (!nh)
5336-
return -ENOMEM;
5337-
nh->fib6_info = rt;
5338-
memcpy(&nh->r_cfg, r_cfg, sizeof(*r_cfg));
5339-
list_add_tail(&nh->list, rt6_nh_list);
5341+
static int ip6_route_mpath_info_create(struct list_head *rt6_nh_list,
5342+
struct fib6_config *cfg,
5343+
struct netlink_ext_ack *extack)
5344+
{
5345+
struct rtnexthop *rtnh;
5346+
int remaining;
5347+
int err;
5348+
5349+
remaining = cfg->fc_mp_len;
5350+
rtnh = (struct rtnexthop *)cfg->fc_mp;
5351+
5352+
/* Parse a Multipath Entry and build a list (rt6_nh_list) of
5353+
* fib6_info structs per nexthop
5354+
*/
5355+
while (rtnh_ok(rtnh, remaining)) {
5356+
struct fib6_config r_cfg;
5357+
struct fib6_info *rt;
5358+
struct rt6_nh *nh;
5359+
int attrlen;
5360+
5361+
nh = kzalloc(sizeof(*nh), GFP_KERNEL);
5362+
if (!nh) {
5363+
err = -ENOMEM;
5364+
goto err;
5365+
}
5366+
5367+
list_add_tail(&nh->list, rt6_nh_list);
5368+
5369+
memcpy(&r_cfg, cfg, sizeof(*cfg));
5370+
if (rtnh->rtnh_ifindex)
5371+
r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
5372+
5373+
attrlen = rtnh_attrlen(rtnh);
5374+
if (attrlen > 0) {
5375+
struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
5376+
5377+
nla = nla_find(attrs, attrlen, RTA_GATEWAY);
5378+
if (nla) {
5379+
r_cfg.fc_gateway = nla_get_in6_addr(nla);
5380+
r_cfg.fc_flags |= RTF_GATEWAY;
5381+
}
5382+
5383+
r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
5384+
nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
5385+
if (nla)
5386+
r_cfg.fc_encap_type = nla_get_u16(nla);
5387+
}
5388+
5389+
r_cfg.fc_flags |= (rtnh->rtnh_flags & RTNH_F_ONLINK);
5390+
5391+
rt = ip6_route_info_create(&r_cfg, GFP_KERNEL, extack);
5392+
if (IS_ERR(rt)) {
5393+
err = PTR_ERR(rt);
5394+
goto err;
5395+
}
5396+
5397+
nh->fib6_info = rt;
5398+
nh->weight = rtnh->rtnh_hops + 1;
5399+
memcpy(&nh->r_cfg, &r_cfg, sizeof(r_cfg));
5400+
5401+
rtnh = rtnh_next(rtnh, &remaining);
5402+
}
53405403

53415404
return 0;
5405+
err:
5406+
ip6_route_mpath_info_cleanup(rt6_nh_list);
5407+
return err;
5408+
}
5409+
5410+
static int ip6_route_mpath_info_create_nh(struct list_head *rt6_nh_list,
5411+
struct netlink_ext_ack *extack)
5412+
{
5413+
struct rt6_nh *nh, *nh_next, *nh_tmp;
5414+
LIST_HEAD(tmp);
5415+
int err;
5416+
5417+
list_for_each_entry_safe(nh, nh_next, rt6_nh_list, list) {
5418+
struct fib6_info *rt = nh->fib6_info;
5419+
5420+
err = ip6_route_info_create_nh(rt, &nh->r_cfg, extack);
5421+
if (err) {
5422+
nh->fib6_info = NULL;
5423+
goto err;
5424+
}
5425+
5426+
rt->fib6_nh->fib_nh_weight = nh->weight;
5427+
5428+
list_move_tail(&nh->list, &tmp);
5429+
5430+
list_for_each_entry(nh_tmp, rt6_nh_list, list) {
5431+
/* check if fib6_info already exists */
5432+
if (rt6_duplicate_nexthop(nh_tmp->fib6_info, rt)) {
5433+
err = -EEXIST;
5434+
goto err;
5435+
}
5436+
}
5437+
}
5438+
out:
5439+
list_splice(&tmp, rt6_nh_list);
5440+
return err;
5441+
err:
5442+
ip6_route_mpath_info_cleanup(rt6_nh_list);
5443+
goto out;
53425444
}
53435445

53445446
static void ip6_route_mpath_notify(struct fib6_info *rt,
@@ -5397,75 +5499,28 @@ static int ip6_route_multipath_add(struct fib6_config *cfg,
53975499
{
53985500
struct fib6_info *rt_notif = NULL, *rt_last = NULL;
53995501
struct nl_info *info = &cfg->fc_nlinfo;
5400-
struct fib6_config r_cfg;
5401-
struct rtnexthop *rtnh;
5402-
struct fib6_info *rt;
5403-
struct rt6_nh *err_nh;
54045502
struct rt6_nh *nh, *nh_safe;
5503+
LIST_HEAD(rt6_nh_list);
5504+
struct rt6_nh *err_nh;
54055505
__u16 nlflags;
5406-
int remaining;
5407-
int attrlen;
5408-
int err = 1;
54095506
int nhn = 0;
5410-
int replace = (cfg->fc_nlinfo.nlh &&
5411-
(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE));
5412-
LIST_HEAD(rt6_nh_list);
5507+
int replace;
5508+
int err;
5509+
5510+
replace = (cfg->fc_nlinfo.nlh &&
5511+
(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_REPLACE));
54135512

54145513
nlflags = replace ? NLM_F_REPLACE : NLM_F_CREATE;
54155514
if (info->nlh && info->nlh->nlmsg_flags & NLM_F_APPEND)
54165515
nlflags |= NLM_F_APPEND;
54175516

5418-
remaining = cfg->fc_mp_len;
5419-
rtnh = (struct rtnexthop *)cfg->fc_mp;
5420-
5421-
/* Parse a Multipath Entry and build a list (rt6_nh_list) of
5422-
* fib6_info structs per nexthop
5423-
*/
5424-
while (rtnh_ok(rtnh, remaining)) {
5425-
memcpy(&r_cfg, cfg, sizeof(*cfg));
5426-
if (rtnh->rtnh_ifindex)
5427-
r_cfg.fc_ifindex = rtnh->rtnh_ifindex;
5428-
5429-
attrlen = rtnh_attrlen(rtnh);
5430-
if (attrlen > 0) {
5431-
struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
5432-
5433-
nla = nla_find(attrs, attrlen, RTA_GATEWAY);
5434-
if (nla) {
5435-
r_cfg.fc_gateway = nla_get_in6_addr(nla);
5436-
r_cfg.fc_flags |= RTF_GATEWAY;
5437-
}
5438-
5439-
r_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
5440-
nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
5441-
if (nla)
5442-
r_cfg.fc_encap_type = nla_get_u16(nla);
5443-
}
5444-
5445-
r_cfg.fc_flags |= (rtnh->rtnh_flags & RTNH_F_ONLINK);
5446-
rt = ip6_route_info_create(&r_cfg, GFP_KERNEL, extack);
5447-
if (IS_ERR(rt)) {
5448-
err = PTR_ERR(rt);
5449-
rt = NULL;
5450-
goto cleanup;
5451-
}
5452-
5453-
err = ip6_route_info_create_nh(rt, &r_cfg, extack);
5454-
if (err) {
5455-
rt = NULL;
5456-
goto cleanup;
5457-
}
5458-
5459-
rt->fib6_nh->fib_nh_weight = rtnh->rtnh_hops + 1;
5460-
5461-
err = ip6_route_info_append(&rt6_nh_list, rt, &r_cfg);
5462-
if (err) {
5463-
fib6_info_release(rt);
5464-
goto cleanup;
5465-
}
5517+
err = ip6_route_mpath_info_create(&rt6_nh_list, cfg, extack);
5518+
if (err)
5519+
return err;
54665520

5467-
rtnh = rtnh_next(rtnh, &remaining);
5468-
}
5521+
err = ip6_route_mpath_info_create_nh(&rt6_nh_list, extack);
5522+
if (err)
5523+
goto cleanup;
54695524

54705525
/* for add and replace send one notification with all nexthops.
54715526
* Skip the notification in fib6_add_rt2node and send one with

0 commit comments

Comments
 (0)