From: Jianbo Liu mlx5e_ipsec_netevent_event() runs for every valid neighbour update on the global netevent chain. It matched the event by the SA source or destination address, which is wrong: - a foreign neighbour (veth, other netdev or netns) sharing the SA address overwrites the programmed MAC; - for a routed peer the MAC is taken from the nexthop gateway, whose address never equals the SA address, so gateway MAC changes are missed. Snapshot the resolved nexthop (table, ifindex, key) when its MAC is read in mlx5e_ipsec_init_macs(), and match events against it. The neighbour tables are per netns, so matching the table also keeps neighbours of other namespaces out. The ifindex tells apart one gateway IP on several netdevs in a netns. Save it only when the nexthop is on the SA's offload device (xso.dev): xfrm flushes the SA when that device goes down, so the saved ifindex can never be reused by another device. For any other device, e.g. a bond slave when the fallback path is used, save ifindex 0 and match the key on any device in the netns. This depends on "net/mlx5e: Copy back only replay_esn after an IPsec ESN update": without it an ESN update replaces nh with a fresh lookup while the hardware keeps the old MACs, and writes it while mlx5e_ipsec_netevent_event() reads it without a lock. Observed on a ConnectX-8 Kubernetes cluster: a pod learns its node IP with the host veth MAC, that MAC is programmed and the peer drops the tunnel traffic. Reproduced on ConnectX-7 (fw 28.48.1000): - neighbour for the peer address added on a dummy netdev: ping loss 100%, 0% with the fix; - routed peer, wrong gateway MAC corrected after the SA was added: ping loss 100%, 0% with the fix. Fixes: 4c24272b4e2b ("net/mlx5e: Listen to ARP events to update IPsec L2 headers in tunnel mode") Co-developed-by: Elliot Edmunds Signed-off-by: Elliot Edmunds Signed-off-by: Jianbo Liu Reviewed-by: Saeed Mahameed Reviewed-by: Cosmin Ratiu Signed-off-by: Tariq Toukan --- .../mellanox/mlx5/core/en_accel/ipsec.c | 48 +++++++++++++------ .../mellanox/mlx5/core/en_accel/ipsec.h | 8 ++++ 2 files changed, 41 insertions(+), 15 deletions(-) diff --git a/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.c b/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.c index 4084f8a88695..cc75fcb0aa15 100644 --- a/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.c +++ b/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.c @@ -256,6 +256,17 @@ static void mlx5e_ipsec_init_limits(struct mlx5e_ipsec_sa_entry *sa_entry, attrs->lft.numb_rounds_soft = (u64)n; } +static void mlx5e_ipsec_set_tnl_nh(struct mlx5e_ipsec_sa_entry *sa_entry, + struct mlx5_accel_esp_xfrm_attrs *attrs, + const struct neigh_table *tbl, + const struct net_device *dev, + const void *pkey) +{ + attrs->nh.tbl = tbl; + attrs->nh.ifindex = dev == sa_entry->x->xso.dev ? dev->ifindex : 0; + memcpy(attrs->nh.key, pkey, tbl->key_len); +} + static void mlx5e_ipsec_init_macs(struct mlx5e_ipsec_sa_entry *sa_entry, struct mlx5_accel_esp_xfrm_attrs *attrs) { @@ -263,6 +274,7 @@ static void mlx5e_ipsec_init_macs(struct mlx5e_ipsec_sa_entry *sa_entry, struct net_device *netdev = sa_entry->dev; struct xfrm_state *x = sa_entry->x; struct dst_entry *rt_dst_entry; + const struct neigh_table *tbl; struct flowi4 fl4 = {}; struct flowi6 fl6 = {}; struct neighbour *n; @@ -360,6 +372,7 @@ static void mlx5e_ipsec_init_macs(struct mlx5e_ipsec_sa_entry *sa_entry, if (attrs->dir == XFRM_DEV_OFFLOAD_OUT && is_zero_ether_addr(addr)) neigh_event_send(n, NULL); + mlx5e_ipsec_set_tnl_nh(sa_entry, attrs, n->tbl, n->dev, n->primary_key); dst_release(rt_dst_entry); neigh_release(n); return; @@ -375,13 +388,20 @@ static void mlx5e_ipsec_init_macs(struct mlx5e_ipsec_sa_entry *sa_entry, else n = ipv6_neigh_create(netdev, pkey); attrs->drop = true; - if (IS_ERR(n)) + if (IS_ERR(n)) { + tbl = addrs->family == AF_INET ? + arp_table(dev_net(netdev)) : + nd_table(dev_net(netdev)); + mlx5e_ipsec_set_tnl_nh(sa_entry, attrs, tbl, netdev, + pkey); return; + } neigh_event_send(n, NULL); } else { neigh_ha_snapshot(addr, n, netdev); ether_addr_copy(dst, addr); } + mlx5e_ipsec_set_tnl_nh(sa_entry, attrs, n->tbl, n->dev, n->primary_key); neigh_release(n); } @@ -973,10 +993,20 @@ static void mlx5e_xfrm_free_state(struct net_device *dev, struct xfrm_state *x) kfree(sa_entry); } +static bool +mlx5e_ipsec_neigh_matches(const struct mlx5e_ipsec_sa_entry *sa_entry, + const struct neighbour *n) +{ + const struct mlx5_accel_esp_xfrm_attrs *attrs = &sa_entry->attrs; + + return attrs->nh.tbl == n->tbl && + (!attrs->nh.ifindex || attrs->nh.ifindex == n->dev->ifindex) && + !memcmp(attrs->nh.key, n->primary_key, n->tbl->key_len); +} + static int mlx5e_ipsec_netevent_event(struct notifier_block *nb, unsigned long event, void *ptr) { - struct mlx5_accel_esp_xfrm_attrs *attrs; struct mlx5e_ipsec_netevent_data *data; struct mlx5e_ipsec_sa_entry *sa_entry; struct mlx5e_ipsec *ipsec; @@ -988,21 +1018,9 @@ static int mlx5e_ipsec_netevent_event(struct notifier_block *nb, ipsec = container_of(nb, struct mlx5e_ipsec, netevent_nb); xa_for_each_marked(&ipsec->sadb, idx, sa_entry, MLX5E_IPSEC_TUNNEL_SA) { - attrs = &sa_entry->attrs; - - if (attrs->addrs.family != n->tbl->family) + if (!mlx5e_ipsec_neigh_matches(sa_entry, n)) continue; - if (attrs->addrs.family == AF_INET) { - if (!neigh_key_eq32(n, &attrs->addrs.saddr.a4) && - !neigh_key_eq32(n, &attrs->addrs.daddr.a4)) - continue; - } else { - if (!neigh_key_eq128(n, &attrs->addrs.saddr.a4) && - !neigh_key_eq128(n, &attrs->addrs.daddr.a4)) - continue; - } - data = sa_entry->work->data; neigh_ha_snapshot(data->addr, n, sa_entry->dev); diff --git a/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.h b/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.h index 6c17bd997a0c..d60c72df0bf1 100644 --- a/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.h +++ b/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.h @@ -119,6 +119,14 @@ struct mlx5_accel_esp_xfrm_attrs { u8 dmac[ETH_ALEN]; __be16 dport; }; + struct { + /* Neighbour tables are per netns, so matching the table + * also keeps neighbours of other namespaces out. + */ + const struct neigh_table *tbl; + int ifindex; + u8 key[sizeof(struct in6_addr)]; + } nh; }; enum mlx5_ipsec_cap { -- 2.44.0