From 1a9181807c3e0517a7ef618f01079d752cdf56c1 Mon Sep 17 00:00:00 2001 From: Lee Clements Date: Fri, 6 Nov 2026 17:14:05 +0100 Subject: tunnel: T9193: match GRE tunnel uniqueness check to the Kernel The pairwise GRE collision check did not follow the Kernel's notion of a duplicate tunnel. With a key set it ignored remote and source-interface, rejecting tunnels the Kernel keeps apart. Without one it refused keyless tunnels on different source-interfaces, and collided a keyless tunnel with a keyed one, which also left the keyless tunnel failing every later commit. A zero key was judged differently depending on which side was verified. Two tunnels now collide only when local address, remote, source-interface and key all match, as ip_tunnel_find() decides. An any address counts as unset, and a zero key is treated as no key only where neither endpoint is set. Keys are compared as the value the Kernel stores, so octal spellings such as "010" are caught and invalid ones such as "08" are refused on commit. Error messages name the configured source and the shared node. Smoketests cover the new cases. --- src/conf_mode/interfaces_tunnel.py | 174 +++++++++++++++++++++++++++++-------- 1 file changed, 138 insertions(+), 36 deletions(-) (limited to 'src') diff --git a/src/conf_mode/interfaces_tunnel.py b/src/conf_mode/interfaces_tunnel.py index eaca9d2e5..8476aa7f6 100644 --- a/src/conf_mode/interfaces_tunnel.py +++ b/src/conf_mode/interfaces_tunnel.py @@ -12,6 +12,7 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . from sys import exit +from typing import NamedTuple import ipaddress from vyos.config import Config from vyos.configdict import get_interface_dict @@ -36,6 +37,58 @@ from vyos import ConfigError from vyos import airbag airbag.enable() + +class TunnelIdentity(NamedTuple): + local: str | None + remote: str | None + source_interface: str | None + key: int | None + + +def tunnel_key_value(key): + """ + iproute2 hands a GRE key to strtoul(3) with base 0, so a leading zero makes + it octal - "010" reaches the Kernel as 8, and "00" as the same zero as "0". + The numeric validator accepts those spellings, thus a key must be compared + as the value the Kernel stores and not as the string the CLI carries. + + Raises ValueError for a spelling strtoul(3) stops short of, e.g. "08", + which iproute2 refuses outright. + """ + if key is None: + return None + if len(key) > 1 and key.startswith('0'): + return int(key, 8) + return int(key, 10) + + +def tunnel_identity(conf): + """ + The Kernel identifies a GRE tunnel by the tuple of local address, remote + address, source-interface and key, see ip_tunnel_find() in + net/ipv4/ip_tunnel.c. Two tunnels collide iff these tuples match. + + An unconfigured endpoint is stored as the any address, so "0.0.0.0" and + "::" compare equal to an endpoint which is not set at all. A zero GRE key + only fails to make a tunnel unique when neither a local nor a remote + address is set - ip_tunnel_lookup() then ends in a loop which compares the + key without testing the flag saying that a key is set at all. A tunnel + which carries an address is matched before that, by ip_tunnel_key_match(), + where a zero key and an unset key differ. + """ + local = dict_search('source_address', conf) + if local in ['0.0.0.0', '::']: + local = None + remote = dict_search('remote', conf) + if remote in ['0.0.0.0', '::']: + remote = None + source_interface = dict_search('source_interface', conf) + key = tunnel_key_value(dict_search('parameters.ip.key', conf)) + if local is None and remote is None and key == 0: + key = None + return TunnelIdentity(local, remote, source_interface, key) + + def get_config(config=None): """ Retrieve CLI config as dictionary. Dictionary can never be empty, as at least @@ -103,6 +156,19 @@ def verify(tunnel): verify_tunnel(tunnel) + # The key is handed to iproute2 verbatim, which parses it with strtoul(3) + # and base 0 - a leading zero thus makes it octal, where "8" and "9" carry + # no meaning. iproute2 refuses such a value and the tunnel is never created, + # so name it here instead of failing when the interface is brought up + tmp = dict_search('parameters.ip.key', tunnel) + try: + tunnel_key_value(tmp) + except ValueError: + raise ConfigError( + f'"parameters ip key {tmp}" is invalid for {tunnel["ifname"]} - a ' + 'leading zero makes the key octal, where "8" and "9" are no digits' + ) + if tunnel['encapsulation'] in ['erspan', 'ip6erspan']: if dict_search('parameters.ip.key', tunnel) == None: raise ConfigError('ERSPAN requires ip key parameter!') @@ -120,18 +186,30 @@ def verify(tunnel): if 'direction' not in tunnel['parameters']['erspan']: raise ConfigError('ERSPAN version 2 requires direction to be set!') - # If tunnel source is any and gre key is not set + # If tunnel source is any and the gre key does not identify the tunnel interface = tunnel['ifname'] - if tunnel['encapsulation'] in ['gre'] and \ - dict_search('source_address', tunnel) == '0.0.0.0' and \ - dict_search('parameters.ip.key', tunnel) == None: - raise ConfigError(f'"parameters ip key" must be set for {interface} when '\ - 'encapsulation is GRE!') + if ( + tunnel['encapsulation'] in ['gre'] + and dict_search('source_address', tunnel) == '0.0.0.0' + ): + # The source-address being the any address, a tunnel without a remote + # carries no endpoint at all - a zero key does not tell such a tunnel + # apart from a keyless one either + identity = tunnel_identity(tunnel) + if identity.key is None: + # Only a tunnel which has no remote either is asked for a + # non-zero key, a zero one does identify the rest + tmp = 'set to a non-zero value' if identity.remote is None else 'set' + raise ConfigError( + f'"parameters ip key" must be {tmp} for {interface} when ' + 'encapsulation is GRE!' + ) gre_encapsulations = ['gre', 'gretap'] if tunnel['encapsulation'] in gre_encapsulations and 'other_tunnels' in tunnel: # Check pairs tunnel source-address/encapsulation/key with exists tunnels. # Prevent the same key for 2 tunnels with same source-address/encap. T2920 + ours = tunnel_identity(tunnel) for o_tunnel, o_tunnel_conf in tunnel['other_tunnels'].items(): # no match on encapsulation - bail out our_encapsulation = tunnel['encapsulation'] @@ -140,36 +218,60 @@ def verify(tunnel): not in gre_encapsulations: continue - our_address = dict_search('source_address', tunnel) - our_key = dict_search('parameters.ip.key', tunnel) - their_address = dict_search('source_address', o_tunnel_conf) - their_key = dict_search('parameters.ip.key', o_tunnel_conf) - if our_key != None: - if their_address == our_address and their_key == our_key: - raise ConfigError(f'Key "{our_key}" for source-address "{our_address}" ' \ - f'is already used for tunnel "{o_tunnel}"!') - else: - our_source_if = dict_search('source_interface', tunnel) - their_source_if = dict_search('source_interface', o_tunnel_conf) - our_remote = dict_search('remote', tunnel) - their_remote = dict_search('remote', o_tunnel_conf) - # If no IP GRE key is defined we cannot have more then one GRE tunnel - # bound to any one interface/IP address and the same remote. This will - # result in a OS PermissionError: add tunnel "gre0" failed: File exists - if our_remote == their_remote: - if our_address is not None and their_address == our_address: - # If set to the same values, this is always a fail - raise ConfigError(f'Missing required "ip key" parameter when '\ - 'running more then one GRE based tunnel on the '\ - 'same source-address') - - if their_source_if == our_source_if and their_address == our_address: - # Note that lack of None check on these is deliberate. - # source-if and source-ip matching while unset (all None) is a fail - # source-ifs set and matching with unset source-ips is a fail - raise ConfigError(f'Missing required "ip key" parameter when '\ - 'running more then one GRE based tunnel on the '\ - 'same source-interface') + try: + theirs = tunnel_identity(o_tunnel_conf) + except ValueError: + # iproute2 will not take their key, so the Kernel never holds + # that tunnel and it cannot collide with ours + continue + if ours != theirs: + continue + + # Remember whether a zero key was configured, the error message + # differs from the one for a tunnel carrying no key at all. Both + # keys parse, an unparsable one leaves the loop above + zero_key = 0 in [ + tunnel_key_value(dict_search('parameters.ip.key', tunnel)), + tunnel_key_value(dict_search('parameters.ip.key', o_tunnel_conf)), + ] + + if ours.key is not None: + # Prevent the same key for 2 tunnels sharing both endpoints. T2920 + # Report the source as configured and not as normalised, + # else an "any" source-address would render as "None". + # One of both is always present, see verify_tunnel() + tmp = dict_search('source_address', tunnel) or ours.source_interface + raise ConfigError( + f'Key "{ours.key}" for source "{tmp}" is already used ' + f'for tunnel "{o_tunnel}"!' + ) + + # A keyless tunnel never collides with a keyed one, as the Kernel + # only matches a tunnel carrying no key against another tunnel + # carrying no key, see ip_tunnel_key_match() in + # include/net/ip_tunnels.h. Tuple equality already requires that. + + # If no IP GRE key is defined we cannot have more than one GRE tunnel + # bound to any one interface/IP address and the same remote. This will + # result in a OS PermissionError: add tunnel "gre0" failed: File exists + + # Name what the two tunnels really have in common. An "any" + # source-address normalises to None and cannot pick the noun, + # and a tunnel carrying no source-interface always has a + # source-address, see verify_tunnel() + tmp = 'source-address' + if ours.source_interface is not None: + tmp = 'source-interface' + if zero_key: + raise ConfigError( + 'A zero "ip key" parameter cannot be told apart from ' + 'an unset one - use a non-zero key to run more than ' + f'one GRE based tunnel on the same {tmp} as "{o_tunnel}"' + ) + raise ConfigError( + 'Missing required "ip key" parameter when running more ' + f'than one GRE based tunnel on the same {tmp}' + ) # Keys are not allowed with ipip and sit tunnels if tunnel['encapsulation'] in ['ipip', 'sit']: -- cgit v1.2.3 From b3c4828f5386d3c3ee53501e7711178a1fcf8e78 Mon Sep 17 00:00:00 2001 From: Christian Breunig Date: Thu, 5 Nov 2026 23:17:40 +0100 Subject: tunnel: T9193: do not collide GRE tunnels of different encapsulation The Kernel keeps gre and gretap tunnels in tables of their own, so a gre and a gretap tunnel may share local address, remote address and GRE key. Only compare tunnels of the same encapsulation. --- smoketest/scripts/cli/test_interfaces_tunnel.py | 25 +++++++++++++++++++++++++ src/conf_mode/interfaces_tunnel.py | 11 +++++------ 2 files changed, 30 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/smoketest/scripts/cli/test_interfaces_tunnel.py b/smoketest/scripts/cli/test_interfaces_tunnel.py index e8f992793..ba30a0e73 100755 --- a/smoketest/scripts/cli/test_interfaces_tunnel.py +++ b/smoketest/scripts/cli/test_interfaces_tunnel.py @@ -619,6 +619,31 @@ class TunnelInterfaceTest(BasicInterfaceTest.TestCase): self.assertNotIn('ikey', conf['linkinfo']['info_data']) + def test_gre_and_gretap_tunnel_same_key(self): + # The Kernel keeps gre and gretap tunnels in tables of their own, so + # both may share local address, remote address and GRE key + ip_key = '60' + tunnels = { + 'tun10': 'gre', + 'tun20': 'gretap', + } + + for tunnel, encapsulation in tunnels.items(): + self.cli_set(self._base_path + [tunnel, 'encapsulation', encapsulation]) + self.cli_set(self._base_path + [tunnel, 'source-address', self.local_v4]) + self.cli_set(self._base_path + [tunnel, 'remote', remote_ip4]) + self.cli_set(self._base_path + [tunnel, 'parameters', 'ip', 'key', ip_key]) + + self.cli_commit() + + for tunnel, encapsulation in tunnels.items(): + conf = get_interface_config(tunnel) + + self.assertEqual(encapsulation, conf['linkinfo']['info_kind']) + self.assertEqual(self.local_v4, conf['linkinfo']['info_data']['local']) + self.assertEqual(remote_ip4, conf['linkinfo']['info_data']['remote']) + self.assertEqual(f'0.0.0.{ip_key}', conf['linkinfo']['info_data']['ikey']) + def test_tunnel_gre_key_leading_zero(self): # iproute2 parses the key with strtoul(3) and base 0, so a leading zero # makes it octal - "00" is the very same zero key as "0" and must be diff --git a/src/conf_mode/interfaces_tunnel.py b/src/conf_mode/interfaces_tunnel.py index 8476aa7f6..f525e9e27 100644 --- a/src/conf_mode/interfaces_tunnel.py +++ b/src/conf_mode/interfaces_tunnel.py @@ -66,7 +66,8 @@ def tunnel_identity(conf): """ The Kernel identifies a GRE tunnel by the tuple of local address, remote address, source-interface and key, see ip_tunnel_find() in - net/ipv4/ip_tunnel.c. Two tunnels collide iff these tuples match. + net/ipv4/ip_tunnel.c. Two tunnels of the same encapsulation collide if + these tuples match. An unconfigured endpoint is stored as the any address, so "0.0.0.0" and "::" compare equal to an endpoint which is not set at all. A zero GRE key @@ -211,11 +212,9 @@ def verify(tunnel): # Prevent the same key for 2 tunnels with same source-address/encap. T2920 ours = tunnel_identity(tunnel) for o_tunnel, o_tunnel_conf in tunnel['other_tunnels'].items(): - # no match on encapsulation - bail out - our_encapsulation = tunnel['encapsulation'] - their_encapsulation = o_tunnel_conf['encapsulation'] - if our_encapsulation in gre_encapsulations and their_encapsulation \ - not in gre_encapsulations: + # no match on encapsulation - bail out. The Kernel keeps gre and + # gretap tunnels in tables of their own, so they never collide + if o_tunnel_conf['encapsulation'] != tunnel['encapsulation']: continue try: -- cgit v1.2.3 From 4bdf9c3a6741f56a572a149dacc94204a513c6f2 Mon Sep 17 00:00:00 2001 From: Christian Breunig Date: Fri, 6 Nov 2026 17:21:25 +0100 Subject: tunnel: T9193: keep accepting a lone zero-keyed GRE tunnel on an any source A GRE tunnel with source-address 0.0.0.0, no remote and "parameters ip key 0" used to commit, but was refused once a zero key stopped counting as a key for such a tunnel. That breaks existing configurations on upgrade. Accept it again whenever a key is configured at all. A zero key still cannot tell two such tunnels apart, and the collision check keeps refusing the second one, so only the single-tunnel case changes back. --- smoketest/scripts/cli/test_interfaces_tunnel.py | 24 +++++++++++++++++------- src/conf_mode/interfaces_tunnel.py | 22 +++++++++------------- 2 files changed, 26 insertions(+), 20 deletions(-) (limited to 'src') diff --git a/smoketest/scripts/cli/test_interfaces_tunnel.py b/smoketest/scripts/cli/test_interfaces_tunnel.py index ba30a0e73..70097786b 100755 --- a/smoketest/scripts/cli/test_interfaces_tunnel.py +++ b/smoketest/scripts/cli/test_interfaces_tunnel.py @@ -325,13 +325,19 @@ class TunnelInterfaceTest(BasicInterfaceTest.TestCase): with self.assertRaises(ConfigSessionError): self.cli_commit() - # A zero key is no key at all for such a tunnel - with an any - # source-address and no remote it catches every packet, exactly as a - # keyless tunnel would + # A zero key on a single tunnel stays accepted for backwards + # compatibility self.cli_set(self._base_path + [interface, 'parameters', 'ip', 'key', '0']) + self.cli_commit() + + # It does not tell a second such tunnel apart, though + self.cli_set(self._base_path + ['tun1281', 'encapsulation', encapsulation]) + self.cli_set(self._base_path + ['tun1281', 'source-address', src_addr]) + self.cli_set(self._base_path + ['tun1281', 'parameters', 'ip', 'key', '0']) with self.assertRaises(ConfigSessionError): self.cli_commit() + self.cli_delete(self._base_path + ['tun1281']) self.cli_set(self._base_path + [interface, 'parameters', 'ip', 'key', key]) self.cli_commit() @@ -646,17 +652,21 @@ class TunnelInterfaceTest(BasicInterfaceTest.TestCase): def test_tunnel_gre_key_leading_zero(self): # iproute2 parses the key with strtoul(3) and base 0, so a leading zero - # makes it octal - "00" is the very same zero key as "0" and must be - # refused for a tunnel which carries no endpoint at all + # makes it octal - "00" is the very same zero key as "0" and must + # collide with it on a tunnel which carries no endpoint at all interface = 'tun1281' - self.cli_set(self._base_path + [interface, 'encapsulation', 'gre']) - self.cli_set(self._base_path + [interface, 'source-address', '0.0.0.0']) + for tunnel in ['tun1280', interface]: + self.cli_set(self._base_path + [tunnel, 'encapsulation', 'gre']) + self.cli_set(self._base_path + [tunnel, 'source-address', '0.0.0.0']) + self.cli_set(self._base_path + ['tun1280', 'parameters', 'ip', 'key', '0']) self.cli_set(self._base_path + [interface, 'parameters', 'ip', 'key', '00']) with self.assertRaises(ConfigSessionError): self.cli_commit() + self.cli_delete(self._base_path + ['tun1280']) + # "010" is octal 8 and does identify the tunnel self.cli_set(self._base_path + [interface, 'parameters', 'ip', 'key', '010']) diff --git a/src/conf_mode/interfaces_tunnel.py b/src/conf_mode/interfaces_tunnel.py index f525e9e27..9d4665a8b 100644 --- a/src/conf_mode/interfaces_tunnel.py +++ b/src/conf_mode/interfaces_tunnel.py @@ -187,24 +187,20 @@ def verify(tunnel): if 'direction' not in tunnel['parameters']['erspan']: raise ConfigError('ERSPAN version 2 requires direction to be set!') - # If tunnel source is any and the gre key does not identify the tunnel + # If tunnel source is any and gre key is not set. A zero key is accepted + # for backwards compatibility - it only fails to identify a tunnel which + # carries no remote either, and the collision check below refuses that + # as soon as a second such tunnel exists interface = tunnel['ifname'] if ( tunnel['encapsulation'] in ['gre'] and dict_search('source_address', tunnel) == '0.0.0.0' + and dict_search('parameters.ip.key', tunnel) == None ): - # The source-address being the any address, a tunnel without a remote - # carries no endpoint at all - a zero key does not tell such a tunnel - # apart from a keyless one either - identity = tunnel_identity(tunnel) - if identity.key is None: - # Only a tunnel which has no remote either is asked for a - # non-zero key, a zero one does identify the rest - tmp = 'set to a non-zero value' if identity.remote is None else 'set' - raise ConfigError( - f'"parameters ip key" must be {tmp} for {interface} when ' - 'encapsulation is GRE!' - ) + raise ConfigError( + f'"parameters ip key" must be set for {interface} when ' + 'encapsulation is GRE!' + ) gre_encapsulations = ['gre', 'gretap'] if tunnel['encapsulation'] in gre_encapsulations and 'other_tunnels' in tunnel: -- cgit v1.2.3