summaryrefslogtreecommitdiff
path: root/python/vyos/utils/vti_updown_db.py
blob: b491fc6f2ea4db19863c3411f60e43c583af7016 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# Copyright 2024 VyOS maintainers and contributors <maintainers@vyos.io>
#
# 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():
    """ Returns true if the database exists """
    return os.path.exists(VTI_WANT_UP_IFLIST)

@contextmanager
def open_vti_updown_db_for_create_or_update():
    """ Opens the database for reading and writing, creating the database if it does not exist """
    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():
    """ Opens the database for reading and writing, returning an error if it does not exist """
    f = open(VTI_WANT_UP_IFLIST, 'r+')
    try:
        db = VTIUpDownDB(f)
        yield db
    finally:
        f.close()

@contextmanager
def open_vti_updown_db_readonly():
    """ Opens the database for reading, returning an error if it does not exist """
    f = open(VTI_WANT_UP_IFLIST, 'r')
    try:
        db = VTIUpDownDB(f)
        yield db
    finally:
        f.close()

def remove_vti_updown_db():
    """ Brings down any interfaces referenced by the database and removes the database """
    # We need to process the DB first to bring down any interfaces still up
    with open_vti_updown_db_for_update() as db:
        db.removeAllOtherInterfaces([])
        # this usage of commit will only ever bring down interfaces,
        # do not need to provide a functional interface dict supplier
        db.commit(lambda _: None)

    os.unlink(VTI_WANT_UP_IFLIST)

class VTIUpDownDB:
    # The VTI Up-Down DB is a text-based database of space-separated "ifspecs".
    #
    # ifspecs can come in one of the two following formats:
    #
    # persistent format: <interface name>
    # indicates the named interface should always be up.
    #
    # connection format: <interface name>:<connection name>:<protocol>
    # indicates the named interface wants to be up due to an established
    # connection <connection name> using the <protocol> protocol.
    #
    # The configuration tree and ipsec daemon connection up-down hook
    # modify this file as needed and use it to determine when a
    # particular event or configuration change should lead to changing
    # the interface state.

    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 add(self, interface, connection = None, protocol = None):
        """
        Adds a new entry to the DB.

        If an interface name, connection name, and protocol are supplied,
        creates a connection entry.

        If only an interface name is specified, creates a persistent entry
        for the given interface.
        """
        ifspec = f"{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):
        """
        Removes a matching entry from the DB.

        If no matching entry can be fonud, the operation returns successfully.
        """
        ifspec = f"{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):
        """ Returns whether the DB contains at least one entry referencing the given interface """
        for ifspec in self._ifspecs:
                if ifspec.split(':')[0] == interface:
                    return True

        return False

    def removeAllOtherInterfaces(self, interface_list):
        """ Removes all interfaces not included in the given list from the DB """
        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 setPersistentInterfaces(self, interface_list):
        """ Updates the set of persistently up interfaces to match the given list """
        new_presistent_interfaces = set(interface_list)
        current_presistent_interfaces = set([ifspec for ifspec in self._ifspecs if ':' not in ifspec])
        added_presistent_interfaces = new_presistent_interfaces - current_presistent_interfaces
        removed_presistent_interfaces = current_presistent_interfaces - new_presistent_interfaces

        for interface in added_presistent_interfaces:
            self.add(interface)

        for interface in removed_presistent_interfaces:
            self.remove(interface)

    def commit(self, interface_dict_supplier):
        """
        Writes the DB to disk and brings interfaces up and down as needed.

        Only interfaces referenced by entries modified in this DB session
        are manipulated. If an interface is called to be brought up, the
        provided interface_config_supplier function is invoked and expected
        to return the config dictionary for the interface.
        """
        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:
                vti = interface_dict_supplier(interface)
                if 'disable' not in vti:
                    tmp = VTIIf(interface, bypass_vti_updown_db = True)
                    tmp.update(vti)
                    syslog(f'Interface {interface} is admin up ...')

        self._ifsUp.clear()