-
Notifications
You must be signed in to change notification settings - Fork 348
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
T5873: Re-write of ipsec updown hook to support remote access VPN
- Loading branch information
Showing
5 changed files
with
217 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
# Copyright 2024 VyOS maintainers and contributors <[email protected]> | ||
# | ||
# This library is free software; you can redistribute it and/or | ||
# modify it under the terms of the GNU Lesser General Public | ||
# License as published by the Free Software Foundation; either | ||
# version 2.1 of the License, or (at your option) any later version. | ||
# | ||
# This library is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
# Lesser General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Lesser General Public | ||
# License along with this library. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
import os | ||
|
||
from contextlib import contextmanager | ||
from syslog import syslog | ||
|
||
VTI_WANT_UP_IFLIST = '/tmp/ipsec_vti_interfaces' | ||
|
||
def vti_updown_db_exists(): | ||
return os.path.exists(VTI_WANT_UP_IFLIST) | ||
|
||
@contextmanager | ||
def open_vti_updown_db_for_create_or_update(): | ||
if vti_updown_db_exists(): | ||
f = open(VTI_WANT_UP_IFLIST, 'r+') | ||
else: | ||
f = open(VTI_WANT_UP_IFLIST, 'x+') | ||
try: | ||
db = VTIUpDownDB(f) | ||
yield db | ||
finally: | ||
f.close() | ||
|
||
@contextmanager | ||
def open_vti_updown_db_for_update(): | ||
f = open(VTI_WANT_UP_IFLIST, 'r+') | ||
try: | ||
db = VTIUpDownDB(f) | ||
yield db | ||
finally: | ||
f.close() | ||
|
||
@contextmanager | ||
def open_vti_updown_db_readonly(): | ||
f = open(VTI_WANT_UP_IFLIST, 'r') | ||
try: | ||
db = VTIUpDownDB(f) | ||
yield db | ||
finally: | ||
f.close() | ||
|
||
def remove_vti_updown_db(): | ||
# We need to process the DB first to bring down any interfaces still up | ||
with open_vti_updown_db_for_update() as db: | ||
db.syncInterfaces([]) | ||
db.commit() | ||
|
||
os.unlink(VTI_WANT_UP_IFLIST) | ||
|
||
class VTIUpDownDB: | ||
def __init__(self, f): | ||
self._fileHandle = f | ||
self._ifspecs = set([entry.strip() for entry in f.read().split(" ") if entry and not entry.isspace()]) | ||
self._ifsUp = set() | ||
self._ifsDown = set() | ||
|
||
def syncInterfaces(self, interface_list): | ||
updated_ifspecs = set([ifspec for ifspec in self._ifspecs if ifspec.split(':')[0] in interface_list]) | ||
removed_ifspecs = self._ifspecs - updated_ifspecs | ||
self._ifspecs = updated_ifspecs | ||
interfaces_to_bring_down = [ifspec.split(':')[0] for ifspec in removed_ifspecs] | ||
self._ifsDown.update(interfaces_to_bring_down) | ||
self._ifsUp.difference_update(interfaces_to_bring_down) | ||
|
||
def syncInterfacesAlwaysUp(self, interface_list): | ||
new_unconditional_interfaces = set(interface_list) | ||
current_unconditional_interfaces = set([ifspec for ifspec in self._ifspecs if ':' not in ifspec]) | ||
added_unconditional_interfaces = new_unconditional_interfaces - current_unconditional_interfaces | ||
removed_unconditional_interfaces = current_unconditional_interfaces - new_unconditional_interfaces | ||
|
||
for interface in added_unconditional_interfaces: | ||
self.add(interface) | ||
|
||
for interface in removed_unconditional_interfaces: | ||
self.remove(interface) | ||
|
||
def add(self, interface, connection = None, protocol = None): | ||
ifspec = "{}:{}:{}".format(interface, connection, protocol) if (connection is not None and protocol is not None) else interface | ||
if ifspec not in self._ifspecs: | ||
self._ifspecs.add(ifspec) | ||
self._ifsUp.add(interface) | ||
self._ifsDown.discard(interface) | ||
|
||
def remove(self, interface, connection = None, protocol = None): | ||
ifspec = "{}:{}:{}".format(interface, connection, protocol) if (connection is not None and protocol is not None) else interface | ||
if ifspec in self._ifspecs: | ||
self._ifspecs.remove(ifspec) | ||
interface_remains = False | ||
for ifspec in self._ifspecs: | ||
if ifspec.split(':')[0] == interface: | ||
interface_remains = True | ||
|
||
if not interface_remains: | ||
self._ifsDown.add(interface) | ||
self._ifsUp.discard(interface) | ||
|
||
def wantsInterfaceUp(self, interface): | ||
for ifspec in self._ifspecs: | ||
if ifspec.split(':')[0] == interface: | ||
return True | ||
|
||
return False | ||
|
||
def commit(self): | ||
from vyos.configquery import ConfigTreeQuery | ||
from vyos.configdict import get_interface_dict | ||
from vyos.ifconfig import VTIIf | ||
from vyos.utils.process import call | ||
from vyos.utils.network import get_interface_config | ||
|
||
self._fileHandle.seek(0) | ||
self._fileHandle.write(' '.join(self._ifspecs)) | ||
self._fileHandle.truncate() | ||
|
||
for interface in self._ifsDown: | ||
vti_link = get_interface_config(interface) | ||
vti_link_up = (vti_link['operstate'] != 'DOWN' if 'operstate' in vti_link else False) | ||
if vti_link_up: | ||
call(f'sudo ip link set {interface} down') | ||
syslog(f'Interface {interface} is admin down ...') | ||
|
||
self._ifsDown.clear() | ||
|
||
for interface in self._ifsUp: | ||
vti_link = get_interface_config(interface) | ||
vti_link_up = (vti_link['operstate'] != 'DOWN' if 'operstate' in vti_link else False) | ||
if not vti_link_up: | ||
conf = ConfigTreeQuery() | ||
_, vti = get_interface_dict(conf.config, ['interfaces', 'vti'], interface) | ||
if 'disable' not in vti: | ||
tmp = VTIIf(interface, bypass_vti_updown_db = True) | ||
tmp.update(vti) | ||
call(f'sudo ip link set {interface} up') | ||
|
||
self._ifsUp.clear() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters