]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
net: fib_rules: Convert RTM_NEWRULE to per-netns RTNL.
authorKuniyuki Iwashima <kuniyu@amazon.com>
Fri, 7 Feb 2025 07:25:00 +0000 (16:25 +0900)
committerJakub Kicinski <kuba@kernel.org>
Tue, 11 Feb 2025 03:08:52 +0000 (19:08 -0800)
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>
net/core/fib_rules.c

index d68332d9cac611e06938445bf90a06eb4f3d8c03..d0995ea9d85238181f3c641a6f01598690cb18f9 100644 (file)
@@ -816,6 +816,9 @@ int fib_newrule(struct net *net, struct sk_buff *skb, struct nlmsghdr *nlh,
        if (err)
                goto errout;
 
+       if (!rtnl_held)
+               rtnl_net_lock(net);
+
        err = fib_nl2rule_rtnl(rule, ops, tb, extack);
        if (err)
                goto errout_free;
@@ -881,12 +884,20 @@ int fib_newrule(struct net *net, struct sk_buff *skb, struct nlmsghdr *nlh,
        if (rule->tun_id)
                ip_tunnel_need_metadata();
 
+       fib_rule_get(rule);
+
+       if (!rtnl_held)
+               rtnl_net_unlock(net);
+
        notify_rule_change(RTM_NEWRULE, rule, ops, nlh, NETLINK_CB(skb).portid);
+       fib_rule_put(rule);
        flush_route_cache(ops);
        rules_ops_put(ops);
        return 0;
 
 errout_free:
+       if (!rtnl_held)
+               rtnl_net_unlock(net);
        kfree(rule);
 errout:
        rules_ops_put(ops);
@@ -897,7 +908,7 @@ EXPORT_SYMBOL_GPL(fib_newrule);
 static int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr *nlh,
                          struct netlink_ext_ack *extack)
 {
-       return fib_newrule(sock_net(skb->sk), skb, nlh, extack, true);
+       return fib_newrule(sock_net(skb->sk), skb, nlh, extack, false);
 }
 
 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 = {
 };
 
 static const struct rtnl_msg_handler fib_rules_rtnl_msg_handlers[] __initconst = {
-       {.msgtype = RTM_NEWRULE, .doit = fib_nl_newrule},
+       {.msgtype = RTM_NEWRULE, .doit = fib_nl_newrule,
+        .flags = RTNL_FLAG_DOIT_PERNET},
        {.msgtype = RTM_DELRULE, .doit = fib_nl_delrule},
        {.msgtype = RTM_GETRULE, .dumpit = fib_nl_dumprule,
         .flags = RTNL_FLAG_DUMP_UNLOCKED},