diff options
| author | Christian Breunig <christian@breunig.cc> | 2026-11-05 22:17:15 +0100 |
|---|---|---|
| committer | Christian Breunig <christian@breunig.cc> | 2026-11-05 22:17:15 +0100 |
| commit | 1b5bdef24632bbc497768847719eba8c33d93ef5 (patch) | |
| tree | eb636b77f49652d35ed164abb792a76153899b23 /python | |
| parent | 69494046c5fc9c3c8e0f310e14a7c31d326af7ca (diff) | |
| download | vyos-1x-1b5bdef24632bbc497768847719eba8c33d93ef5.tar.gz vyos-1x-1b5bdef24632bbc497768847719eba8c33d93ef5.zip | |
https: T9559: fix bogus port conflict with "vrf default"
Committing "set service https vrf default" failed with "TCP port 443 in vrf
default is already used by another service" although nothing listened on the
port. The port check bound its test socket to a VRF device named "default",
which does not exist, and reported that failure as a port conflict.
The default VRF is now treated like no VRF at all. A VRF device that does not
exist no longer counts as a busy port, as nothing can listen in it; any other
error is raised instead of being reported as a conflict. The same check is
used by the Prometheus VPP exporter.
Diffstat (limited to 'python')
| -rw-r--r-- | python/vyos/utils/network.py | 27 |
1 files changed, 19 insertions, 8 deletions
diff --git a/python/vyos/utils/network.py b/python/vyos/utils/network.py index 4a4414f56..d478f4d2c 100644 --- a/python/vyos/utils/network.py +++ b/python/vyos/utils/network.py @@ -394,6 +394,7 @@ def check_port_availability(address: str = None, port: int = 0, False if a port is busy or IP address does not exists True if a port is free and IP address exists """ + import errno import socket from ipaddress import ip_address @@ -417,19 +418,29 @@ def check_port_availability(address: str = None, port: int = 0, print(f'Invalid address: {address}') return False + # the default VRF has no master device - an unbound socket already uses it + if vrf == 'default': + vrf = None + for family, socktype, proto, canonname, sockaddr in addr_info: - try: - with socket.socket(family, socktype, proto) as s: - s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) - # When a VRF is specified, bind the socket to the VRF master - # device so the address is resolved in that VRF's L3 domain. - if vrf: + with socket.socket(family, socktype, proto) as s: + s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + # When a VRF is specified, bind the socket to the VRF master + # device so the address is resolved in that VRF's L3 domain. + if vrf: + try: s.setsockopt(socket.SOL_SOCKET, socket.SO_BINDTODEVICE, (vrf + '\0').encode()) + except OSError as e: + # nothing can listen in a VRF that does not exist (yet) + if e.errno == errno.ENODEV: + return True + raise + try: s.bind(sockaddr) return True # port is free to use - except OSError: - return False # port is already in use + except OSError: + return False # port is already in use # if we reach this point, no socket was tested and we assume the port is # already in use - better safe then sorry |
