[PATCH net-next v4] ip6_gre: fix changelink conflict checks for L2 tunnels

From: Xia Yan

Date: Tue Nov 17 2026 - 05:16:59 EST


ip6gre_changelink_common() calls ip6gre_tunnel_locate(), which only looks
for ARPHRD_IP6GRE devices. This lets an ip6gretap or ip6erspan update
create a duplicate L2 tunnel, or rejects a valid update because an
unrelated L3 tunnel has the requested parameters. A duplicate can receive
traffic intended for the original tunnel.

When the addresses, input key or link change, check for a conflict using
dev->type, including updates to metadata devices.

A metadata device can be created with the same receive parameters as an
existing ordinary device. Allow updates that leave those parameters
unchanged, and keep the hash entry in place. Otherwise a TTL update can
either fail because another entry matches first, or move the updated
device to the head and change which device receives packets. Do this for
both GRE and ERSPAN changelink.

Check conflicts and unregistering before applying encapsulation
parameters. Leave the shared find helper, newlink, legacy ioctls and
receive lookup unchanged.

Found during code review of the newlink and changelink duplicate checks.
Reproduced under UML with an IPv6 underlay named under:

ip link add tap1 type ip6gretap local fd26:106::1 \
remote fd26:106::2 key 100 dev under
ip link add tap2 type ip6gretap local fd26:106::1 \
remote fd26:106::2 key 200 dev under
ip link set tap1 up
ip link set tap2 up
ip link set tap2 type ip6gretap key 100

Before the fix, the last command succeeds and received frames move from
tap1 to tap2. With the fix, it fails with EEXIST and tap2 is unchanged.
Custom UML tests covered conflicting and valid updates, metadata
overlaps, and receive interfaces for GRE, GRE TAP and ERSPAN. TTL,
fwmark and output-key updates kept the same receive interface. Kernel
selftests were not run.

Signed-off-by: Xia Yan <yanxia@xxxxxxxxxxxxxxxxxxxx>
---

Notes (ip6gre-v4-mail):
Changes in v4:
- Check changed receive parameters for metadata devices too.
- Skip the duplicate lookup when receive parameters are unchanged.
- Keep unchanged receive parameters in place in the hash table for both
GRE and ERSPAN, so an update does not change receive priority.
- Rebase onto current net-next; keep the shared find helper unchanged.

v3: https://lore.kernel.org/netdev/179455350258.84965.17518172977020586093.ip6gre-v3@xxxxxxxxxxxxxxxxxxxx/
Review: https://lore.kernel.org/netdev/179489968486.3136.18372818760197436344@xxxxxxxxxx/

Tested on net-next 6ff4c5942a301675eb12e5b49d53a81e604624a1 with
W=1 UML builds. The main checks passed 194/194 assertions; eight
additional cases passed 92/92. Packet checks verified the receive
interface and payload before and after updates.

net/ipv6/ip6_gre.c | 64 +++++++++++++++++++++++++++++-----------------
1 file changed, 40 insertions(+), 24 deletions(-)

diff --git a/net/ipv6/ip6_gre.c b/net/ipv6/ip6_gre.c
index 26c0e1c4de18f8b44ceb05d01673aaadfbcbd5ef..af08041b6c498bcf25885acd09328eac4ae7b668 100644
--- a/net/ipv6/ip6_gre.c
+++ b/net/ipv6/ip6_gre.c
@@ -2042,12 +2042,21 @@ static int ip6gre_newlink(struct net_device *dev,
return err;
}

+/* Match the fields used by ip6gre_tunnel_find(), except the device type. */
+static bool ip6gre_tunnel_same_rx_key(const struct __ip6_tnl_parm *a,
+ const struct __ip6_tnl_parm *b)
+{
+ return ipv6_addr_equal(&a->laddr, &b->laddr) &&
+ ipv6_addr_equal(&a->raddr, &b->raddr) &&
+ a->i_key == b->i_key && a->link == b->link;
+}
+
static struct ip6_tnl *
ip6gre_changelink_common(struct net_device *dev, struct nlattr *tb[],
struct nlattr *data[], struct __ip6_tnl_parm *p_p,
struct netlink_ext_ack *extack)
{
- struct ip6_tnl *t, *nt = netdev_priv(dev);
+ struct ip6_tnl *nt = netdev_priv(dev);
struct net *net = nt->net;
struct ip6gre_net *ign = net_generic(net, ip6gre_net_id);
struct ip_tunnel_encap ipencap;
@@ -2055,6 +2064,15 @@ ip6gre_changelink_common(struct net_device *dev, struct nlattr *tb[],
if (dev == ign->fb_tunnel_dev)
return ERR_PTR(-EINVAL);

+ if (ip6gre_tunnel_unregistering(nt))
+ return ERR_PTR(-ENODEV);
+
+ ip6gre_netlink_parms(data, p_p);
+
+ if (!ip6gre_tunnel_same_rx_key(&nt->parms, p_p) &&
+ ip6gre_tunnel_find(net, p_p, dev->type))
+ return ERR_PTR(-EEXIST);
+
if (ip6gre_netlink_encap_parms(data, &ipencap)) {
int err = ip6_tnl_encap_setup(nt, &ipencap);

@@ -2062,21 +2080,7 @@ ip6gre_changelink_common(struct net_device *dev, struct nlattr *tb[],
return ERR_PTR(err);
}

- ip6gre_netlink_parms(data, p_p);
-
- t = ip6gre_tunnel_locate(net, p_p, 0);
-
- if (t) {
- if (t->dev != dev)
- return ERR_PTR(-EEXIST);
- } else {
- t = nt;
- }
-
- if (ip6gre_tunnel_unregistering(t))
- return ERR_PTR(-ENODEV);
-
- return t;
+ return nt;
}

static int ip6gre_changelink(struct net_device *dev, struct nlattr *tb[],
@@ -2086,6 +2090,7 @@ static int ip6gre_changelink(struct net_device *dev, struct nlattr *tb[],
struct ip6_tnl *t = netdev_priv(dev);
struct __ip6_tnl_parm p;
struct ip6gre_net *ign;
+ bool rehash;
int err = 0;

ign = net_generic(t->net, ip6gre_net_id);
@@ -2101,11 +2106,16 @@ static int ip6gre_changelink(struct net_device *dev, struct nlattr *tb[],
goto unlock;
}

- ip6gre_tunnel_unlink_md(ign, t);
- ip6gre_tunnel_unlink(ign, t);
+ rehash = !ip6gre_tunnel_same_rx_key(&t->parms, &p);
+ if (rehash) {
+ ip6gre_tunnel_unlink_md(ign, t);
+ ip6gre_tunnel_unlink(ign, t);
+ }
ip6gre_tnl_change(t, &p, !tb[IFLA_MTU]);
- ip6gre_tunnel_link_md(ign, t);
- ip6gre_tunnel_link(ign, t);
+ if (rehash) {
+ ip6gre_tunnel_link_md(ign, t);
+ ip6gre_tunnel_link(ign, t);
+ }
unlock:
mutex_unlock(&ign->tunnels_lock);

@@ -2350,6 +2360,7 @@ static int ip6erspan_changelink(struct net_device *dev, struct nlattr *tb[],
struct ip6_tnl *t = netdev_priv(dev);
struct __ip6_tnl_parm p;
struct ip6gre_net *ign;
+ bool rehash;
int err = 0;

if (!rtnl_dev_link_net_capable(dev, t->net))
@@ -2366,11 +2377,16 @@ static int ip6erspan_changelink(struct net_device *dev, struct nlattr *tb[],
}

ip6erspan_set_version(data, &p);
- ip6erspan_tunnel_unlink_md(ign, t);
- ip6gre_tunnel_unlink(ign, t);
+ rehash = !ip6gre_tunnel_same_rx_key(&t->parms, &p);
+ if (rehash) {
+ ip6erspan_tunnel_unlink_md(ign, t);
+ ip6gre_tunnel_unlink(ign, t);
+ }
ip6erspan_tnl_change(t, &p, !tb[IFLA_MTU]);
- ip6erspan_tunnel_link_md(ign, t);
- ip6gre_tunnel_link(ign, t);
+ if (rehash) {
+ ip6erspan_tunnel_link_md(ign, t);
+ ip6gre_tunnel_link(ign, t);
+ }
unlock:
mutex_unlock(&ign->tunnels_lock);


base-commit: 6ff4c5942a301675eb12e5b49d53a81e604624a1
--
2.43.0