Skip to content

Commit 98d3a6f

Browse files
q2venkuba-moo
authored andcommitted
net: fib_rules: Convert RTM_NEWRULE to per-netns RTNL.
fib_nl_newrule() is the doit() handler for RTM_NEWRULE but also called from vrf_newlink(). In the latter case, RTNL is already held and the 4th arg is true. Let's hold per-netns RTNL in fib_newrule() if rtnl_held is false. Note that we call fib_rule_get() before releasing per-netns RTNL to call notify_rule_change() without RTNL and prevent freeing the new rule. Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com> Reviewed-by: Eric Dumazet <edumazet@google.com> Reviewed-by: Ido Schimmel <idosch@nvidia.com> Tested-by: Ido Schimmel <idosch@nvidia.com> Link: https://patch.msgid.link/20250207072502.87775-7-kuniyu@amazon.com Signed-off-by: Jakub Kicinski <kuba@kernel.org>
1 parent a0596c2 commit 98d3a6f

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

net/core/fib_rules.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,9 @@ int fib_newrule(struct net *net, struct sk_buff *skb, struct nlmsghdr *nlh,
816816
if (err)
817817
goto errout;
818818

819+
if (!rtnl_held)
820+
rtnl_net_lock(net);
821+
819822
err = fib_nl2rule_rtnl(rule, ops, tb, extack);
820823
if (err)
821824
goto errout_free;
@@ -881,12 +884,20 @@ int fib_newrule(struct net *net, struct sk_buff *skb, struct nlmsghdr *nlh,
881884
if (rule->tun_id)
882885
ip_tunnel_need_metadata();
883886

887+
fib_rule_get(rule);
888+
889+
if (!rtnl_held)
890+
rtnl_net_unlock(net);
891+
884892
notify_rule_change(RTM_NEWRULE, rule, ops, nlh, NETLINK_CB(skb).portid);
893+
fib_rule_put(rule);
885894
flush_route_cache(ops);
886895
rules_ops_put(ops);
887896
return 0;
888897

889898
errout_free:
899+
if (!rtnl_held)
900+
rtnl_net_unlock(net);
890901
kfree(rule);
891902
errout:
892903
rules_ops_put(ops);
@@ -897,7 +908,7 @@ EXPORT_SYMBOL_GPL(fib_newrule);
897908
static int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr *nlh,
898909
struct netlink_ext_ack *extack)
899910
{
900-
return fib_newrule(sock_net(skb->sk), skb, nlh, extack, true);
911+
return fib_newrule(sock_net(skb->sk), skb, nlh, extack, false);
901912
}
902913

903914
int fib_delrule(struct net *net, struct sk_buff *skb, struct nlmsghdr *nlh,
@@ -1320,7 +1331,8 @@ static struct pernet_operations fib_rules_net_ops = {
13201331
};
13211332

13221333
static const struct rtnl_msg_handler fib_rules_rtnl_msg_handlers[] __initconst = {
1323-
{.msgtype = RTM_NEWRULE, .doit = fib_nl_newrule},
1334+
{.msgtype = RTM_NEWRULE, .doit = fib_nl_newrule,
1335+
.flags = RTNL_FLAG_DOIT_PERNET},
13241336
{.msgtype = RTM_DELRULE, .doit = fib_nl_delrule},
13251337
{.msgtype = RTM_GETRULE, .dumpit = fib_nl_dumprule,
13261338
.flags = RTNL_FLAG_DUMP_UNLOCKED},

0 commit comments

Comments
 (0)