mlxsw: spectrum_router: Prevent incorrect replacement of local table routes
authorIdo Schimmel <idosch@mellanox.com>
Fri, 7 Feb 2020 17:26:24 +0000 (19:26 +0200)
committerDavid S. Miller <davem@davemloft.net>
Fri, 7 Feb 2020 17:47:01 +0000 (18:47 +0100)
The driver uses the same table to represent both the main and local
routing tables. Prevent routes in the main table from replacing routes
in the local table to reflect the fact that the local table is consulted
first during lookup.

Fixes: b6a1d871d37a ("mlxsw: spectrum_router: Start using new IPv4 route notifications")
Fixes: dacad7b34b59 ("mlxsw: spectrum_router: Start using new IPv6 route notifications")
Signed-off-by: Ido Schimmel <idosch@mellanox.com>
Acked-by: Jiri Pirko <jiri@mellanox.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
drivers/net/ethernet/mellanox/mlxsw/spectrum_router.c

index ce707723f8cf90afb3c255db46528897a690b9b9..f8b3869672c389efa2af28547c1c7f19a1393feb 100644 (file)
@@ -4844,6 +4844,23 @@ mlxsw_sp_fib_node_entry_unlink(struct mlxsw_sp *mlxsw_sp,
        fib_node->fib_entry = NULL;
 }
 
+static bool mlxsw_sp_fib4_allow_replace(struct mlxsw_sp_fib4_entry *fib4_entry)
+{
+       struct mlxsw_sp_fib_node *fib_node = fib4_entry->common.fib_node;
+       struct mlxsw_sp_fib4_entry *fib4_replaced;
+
+       if (!fib_node->fib_entry)
+               return true;
+
+       fib4_replaced = container_of(fib_node->fib_entry,
+                                    struct mlxsw_sp_fib4_entry, common);
+       if (fib4_entry->tb_id == RT_TABLE_MAIN &&
+           fib4_replaced->tb_id == RT_TABLE_LOCAL)
+               return false;
+
+       return true;
+}
+
 static int
 mlxsw_sp_router_fib4_replace(struct mlxsw_sp *mlxsw_sp,
                             const struct fib_entry_notifier_info *fen_info)
@@ -4872,6 +4889,12 @@ mlxsw_sp_router_fib4_replace(struct mlxsw_sp *mlxsw_sp,
                goto err_fib4_entry_create;
        }
 
+       if (!mlxsw_sp_fib4_allow_replace(fib4_entry)) {
+               mlxsw_sp_fib4_entry_destroy(mlxsw_sp, fib4_entry);
+               mlxsw_sp_fib_node_put(mlxsw_sp, fib_node);
+               return 0;
+       }
+
        replaced = fib_node->fib_entry;
        err = mlxsw_sp_fib_node_entry_link(mlxsw_sp, &fib4_entry->common);
        if (err) {
@@ -4908,7 +4931,7 @@ static void mlxsw_sp_router_fib4_del(struct mlxsw_sp *mlxsw_sp,
                return;
 
        fib4_entry = mlxsw_sp_fib4_entry_lookup(mlxsw_sp, fen_info);
-       if (WARN_ON(!fib4_entry))
+       if (!fib4_entry)
                return;
        fib_node = fib4_entry->common.fib_node;
 
@@ -5408,6 +5431,27 @@ mlxsw_sp_fib6_entry_lookup(struct mlxsw_sp *mlxsw_sp,
        return NULL;
 }
 
+static bool mlxsw_sp_fib6_allow_replace(struct mlxsw_sp_fib6_entry *fib6_entry)
+{
+       struct mlxsw_sp_fib_node *fib_node = fib6_entry->common.fib_node;
+       struct mlxsw_sp_fib6_entry *fib6_replaced;
+       struct fib6_info *rt, *rt_replaced;
+
+       if (!fib_node->fib_entry)
+               return true;
+
+       fib6_replaced = container_of(fib_node->fib_entry,
+                                    struct mlxsw_sp_fib6_entry,
+                                    common);
+       rt = mlxsw_sp_fib6_entry_rt(fib6_entry);
+       rt_replaced = mlxsw_sp_fib6_entry_rt(fib6_replaced);
+       if (rt->fib6_table->tb6_id == RT_TABLE_MAIN &&
+           rt_replaced->fib6_table->tb6_id == RT_TABLE_LOCAL)
+               return false;
+
+       return true;
+}
+
 static int mlxsw_sp_router_fib6_replace(struct mlxsw_sp *mlxsw_sp,
                                        struct fib6_info **rt_arr,
                                        unsigned int nrt6)
@@ -5442,6 +5486,12 @@ static int mlxsw_sp_router_fib6_replace(struct mlxsw_sp *mlxsw_sp,
                goto err_fib6_entry_create;
        }
 
+       if (!mlxsw_sp_fib6_allow_replace(fib6_entry)) {
+               mlxsw_sp_fib6_entry_destroy(mlxsw_sp, fib6_entry);
+               mlxsw_sp_fib_node_put(mlxsw_sp, fib_node);
+               return 0;
+       }
+
        replaced = fib_node->fib_entry;
        err = mlxsw_sp_fib_node_entry_link(mlxsw_sp, &fib6_entry->common);
        if (err)