Skip to content

Commit a72b44a

Browse files
committed
Forbid the subnet gateway IP deletion if a router interface is attached
When a router interface is created, the corresponding subnet gateway IP is tested first [1]. If the subnet has no gateway IP, the router interface cannot be created. This IP will be assigned to this port. The Neutron API also prevents from modifying the subnet gateway IP if assigned to a router interface [2]. However the API is not preventing the subnet gateway IP deletion. This patch is adding this check. This patch is being tested in the neutron-tempest-plugin [3]. [1]https://github.com/openstack/neutron/blob/de58c1b99523104a471420ef0468147f13c9e98d/neutron/db/l3_db.py#L902-L904 [2]https://github.com/openstack/neutron/blob/de58c1b99523104a471420ef0468147f13c9e98d/neutron/db/db_base_plugin_v2.py#L715 [3]https://review.opendev.org/c/openstack/neutron-tempest-plugin/+/904710 Closes-Bug: #2036423 Change-Id: I4c7b399a3a052749abdb88fb50be628ee91b63a0 (cherry picked from commit f9e4097)
1 parent 57d2bca commit a72b44a

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

neutron/db/db_base_plugin_v2.py

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -643,32 +643,39 @@ def _validate_subnet(self, context, s, cur_subnet=None):
643643
"supported if enable_dhcp is True.")
644644
raise exc.InvalidInput(error_message=error_message)
645645

646-
if validators.is_attr_set(s.get('gateway_ip')):
647-
self._validate_ip_version(ip_ver, s['gateway_ip'], 'gateway_ip')
648-
if has_cidr:
649-
is_gateway_not_valid = (
650-
ipam.utils.check_gateway_invalid_in_subnet(
651-
s['cidr'], s['gateway_ip']))
652-
if is_gateway_not_valid:
653-
error_message = _("Gateway is not valid on subnet")
654-
raise exc.InvalidInput(error_message=error_message)
655-
# Ensure the gateway IP is not assigned to any port
656-
# skip this check in case of create (s parameter won't have id)
646+
gateway_ip = s.get('gateway_ip', constants.ATTR_NOT_SPECIFIED)
647+
if validators.is_attr_set(gateway_ip) or gateway_ip is None:
648+
# Validate the gateway IP, if defined in the request.
649+
if s['gateway_ip']:
650+
self._validate_ip_version(ip_ver, gateway_ip, 'gateway_ip')
651+
if has_cidr:
652+
is_gateway_not_valid = (
653+
ipam.utils.check_gateway_invalid_in_subnet(
654+
s['cidr'], gateway_ip))
655+
if is_gateway_not_valid:
656+
error_message = _("Gateway is not valid on subnet")
657+
raise exc.InvalidInput(error_message=error_message)
658+
659+
# Ensure the current subnet gateway IP is not assigned to any port.
660+
# The subnet gateway IP cannot be modified or removed if in use
661+
# (assigned to a router interface).
662+
# Skip this check in case of create (s parameter won't have id).
657663
# NOTE(salv-orlando): There is slight chance of a race, when
658664
# a subnet-update and a router-interface-add operation are
659665
# executed concurrently
660-
s_gateway_ip = netaddr.IPAddress(s['gateway_ip'])
666+
s_gateway_ip = (netaddr.IPAddress(gateway_ip) if gateway_ip else
667+
None)
661668
if (cur_subnet and
662669
s_gateway_ip != cur_subnet['gateway_ip'] and
663670
not ipv6_utils.is_ipv6_pd_enabled(s)):
664-
gateway_ip = str(cur_subnet['gateway_ip'])
671+
current_gateway_ip = str(cur_subnet['gateway_ip'])
665672
alloc = port_obj.IPAllocation.get_alloc_routerports(
666-
context, cur_subnet['id'], gateway_ip=gateway_ip,
673+
context, cur_subnet['id'], gateway_ip=current_gateway_ip,
667674
first=True)
668675

669676
if alloc and alloc.port_id:
670677
raise exc.GatewayIpInUse(
671-
ip_address=gateway_ip,
678+
ip_address=current_gateway_ip,
672679
port_id=alloc.port_id)
673680

674681
if validators.is_attr_set(s.get('dns_nameservers')):
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
fixes:
3+
- |
4+
[`bug 2036423 <https://bugs.launchpad.net/neutron/+bug/2036423>`_]
5+
Now it is not possible to delete a subnet gateway IP if that subnet has a
6+
router interface; the subnet gateway IP modification was already forbidden.

0 commit comments

Comments
 (0)