]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
nexthop: Move NHA_OIF validation to rtm_to_nh_config_rtnl().
authorKuniyuki Iwashima <kuniyu@amazon.com>
Wed, 19 Mar 2025 23:06:48 +0000 (16:06 -0700)
committerJakub Kicinski <kuba@kernel.org>
Tue, 25 Mar 2025 14:31:59 +0000 (07:31 -0700)
NHA_OIF needs to look up a device by __dev_get_by_index(),
which requires RTNL.

Let's move NHA_OIF validation to rtm_to_nh_config_rtnl().

Note that the proceeding checks made the original !cfg->nh_fdb
check redundant.

  NHA_FDB is set           -> NHA_OIF cannot be set
  NHA_FDB is set but false -> NHA_OIF must be set
  NHA_FDB is not set       -> NHA_OIF must be set

Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
Reviewed-by: Eric Dumazet <edumazet@google.com>
Reviewed-by: David Ahern <dsahern@kernel.org>
Link: https://patch.msgid.link/20250319230743.65267-4-kuniyu@amazon.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
net/ipv4/nexthop.c

index d30edc14b039b2e5c200f7692aff13d9ed2683ac..426cdf301c6fa220849367b69dc0dc1c824cef14 100644 (file)
@@ -3134,25 +3134,6 @@ static int rtm_to_nh_config(struct net *net, struct sk_buff *skb,
                goto out;
        }
 
-       if (!cfg->nh_fdb && tb[NHA_OIF]) {
-               cfg->nh_ifindex = nla_get_u32(tb[NHA_OIF]);
-               if (cfg->nh_ifindex)
-                       cfg->dev = __dev_get_by_index(net, cfg->nh_ifindex);
-
-               if (!cfg->dev) {
-                       NL_SET_ERR_MSG(extack, "Invalid device index");
-                       goto out;
-               } else if (!(cfg->dev->flags & IFF_UP)) {
-                       NL_SET_ERR_MSG(extack, "Nexthop device is not up");
-                       err = -ENETDOWN;
-                       goto out;
-               } else if (!netif_carrier_ok(cfg->dev)) {
-                       NL_SET_ERR_MSG(extack, "Carrier for nexthop device is down");
-                       err = -ENETDOWN;
-                       goto out;
-               }
-       }
-
        err = -EINVAL;
        if (tb[NHA_GATEWAY]) {
                struct nlattr *gwa = tb[NHA_GATEWAY];
@@ -3216,11 +3197,33 @@ out:
 }
 
 static int rtm_to_nh_config_rtnl(struct net *net, struct nlattr **tb,
+                                struct nh_config *cfg,
                                 struct netlink_ext_ack *extack)
 {
        if (tb[NHA_GROUP])
                return nh_check_attr_group_rtnl(net, tb, extack);
 
+       if (tb[NHA_OIF]) {
+               cfg->nh_ifindex = nla_get_u32(tb[NHA_OIF]);
+               if (cfg->nh_ifindex)
+                       cfg->dev = __dev_get_by_index(net, cfg->nh_ifindex);
+
+               if (!cfg->dev) {
+                       NL_SET_ERR_MSG(extack, "Invalid device index");
+                       return -EINVAL;
+               }
+
+               if (!(cfg->dev->flags & IFF_UP)) {
+                       NL_SET_ERR_MSG(extack, "Nexthop device is not up");
+                       return -ENETDOWN;
+               }
+
+               if (!netif_carrier_ok(cfg->dev)) {
+                       NL_SET_ERR_MSG(extack, "Carrier for nexthop device is down");
+                       return -ENETDOWN;
+               }
+       }
+
        return 0;
 }
 
@@ -3244,7 +3247,7 @@ static int rtm_new_nexthop(struct sk_buff *skb, struct nlmsghdr *nlh,
        if (err)
                goto out;
 
-       err = rtm_to_nh_config_rtnl(net, tb, extack);
+       err = rtm_to_nh_config_rtnl(net, tb, &cfg, extack);
        if (err)
                goto out;