summaryrefslogtreecommitdiff
path: root/smoketest/scripts/cli/test_protocols_nhrp.py
blob: 3de41577b53c2b1e3c4d177b3f13289dae83140d (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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/usr/bin/env python3
#
# Copyright VyOS maintainers and contributors <maintainers@vyos.io>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 or later as
# published by the Free Software Foundation.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import unittest

from base_vyostest_shim import VyOSUnitTestSHIM
from vyos.utils.process import process_named_running

tunnel_path = ['interfaces', 'tunnel']
nhrp_path = ['protocols', 'nhrp']
vpn_path = ['vpn', 'ipsec']
PROCESS_NAME = 'nhrpd'

class TestProtocolsNHRP(VyOSUnitTestSHIM.TestCase):
    @classmethod
    def setUpClass(cls):
        super(TestProtocolsNHRP, cls).setUpClass()

        # ensure we can also run this test on a live system - so lets clean
        # out the current configuration :)
        cls.cli_delete(cls, nhrp_path)
        cls.cli_delete(cls, tunnel_path)

    def tearDown(self):
        self.cli_delete(nhrp_path)
        self.cli_delete(tunnel_path)
        self.cli_delete(vpn_path)
        self.cli_commit()
        # always forward to base class
        super().tearDown()

    def test_01_nhrp_config(self):
        tunnel_if = "tun100"
        tunnel_ip = '172.16.253.134/32'
        tunnel_source = "192.0.2.134"
        tunnel_encapsulation = "gre"
        esp_group = "ESP-HUB"
        ike_group = "IKE-HUB"
        nhrp_secret = "vyos123"
        nhrp_profile = "NHRPVPN"
        nhrp_holdtime = '300'
        nhs_tunnelip = '172.16.253.1'
        nhs_nbmaip = '192.0.2.1'
        map_tunnelip = '172.16.253.135'
        map_nbmaip = "192.0.2.135"
        nhrp_networkid = '1'
        ipsec_secret = "secret"
        multicat_log_group = '2'
        redirect_log_group = '1'
        # Tunnel
        self.cli_set(tunnel_path + [tunnel_if, "address", tunnel_ip])
        self.cli_set(tunnel_path + [tunnel_if, "encapsulation", tunnel_encapsulation])
        self.cli_set(tunnel_path + [tunnel_if, "source-address", tunnel_source])
        self.cli_set(tunnel_path + [tunnel_if, "enable-multicast"])
        self.cli_set(tunnel_path + [tunnel_if, "parameters", "ip", "key", "1"])

        # NHRP
        self.cli_set(nhrp_path + ["tunnel", tunnel_if, "authentication", nhrp_secret])
        self.cli_set(nhrp_path + ["tunnel", tunnel_if, "holdtime", nhrp_holdtime])
        self.cli_set(nhrp_path + ["tunnel", tunnel_if, "multicast", nhs_tunnelip])
        self.cli_set(nhrp_path + ["tunnel", tunnel_if, "redirect"])
        self.cli_set(nhrp_path + ["tunnel", tunnel_if, "shortcut"])
        self.cli_set(nhrp_path + ["tunnel", tunnel_if, "registration-no-unique"])
        self.cli_set(nhrp_path + ["tunnel", tunnel_if, "network-id", nhrp_networkid])
        self.cli_set(nhrp_path + ["tunnel", tunnel_if, "nhs", "tunnel-ip", nhs_tunnelip, "nbma", nhs_nbmaip])
        self.cli_set(nhrp_path + ["tunnel", tunnel_if, "map", "tunnel-ip", map_tunnelip, "nbma", map_nbmaip])

        # IKE/ESP Groups
        self.cli_set(vpn_path + ["esp-group", esp_group, "lifetime", "1800"])
        self.cli_set(vpn_path + ["esp-group", esp_group, "mode", "transport"])
        self.cli_set(vpn_path + ["esp-group", esp_group, "pfs", "dh-group2"])
        self.cli_set(vpn_path + ["esp-group", esp_group, "proposal", "1", "encryption", "aes256"])
        self.cli_set(vpn_path + ["esp-group", esp_group, "proposal", "1", "hash", "sha1"])
        self.cli_set(vpn_path + ["esp-group", esp_group, "proposal", "2", "encryption", "3des"])
        self.cli_set(vpn_path + ["esp-group", esp_group, "proposal", "2", "hash", "md5"])

        self.cli_set(vpn_path + ["ike-group", ike_group, "key-exchange", "ikev1"])
        self.cli_set(vpn_path + ["ike-group", ike_group, "lifetime", "3600"])
        self.cli_set(vpn_path + ["ike-group", ike_group, "proposal", "1", "dh-group", "2"])
        self.cli_set(vpn_path + ["ike-group", ike_group, "proposal", "1", "encryption", "aes256"])
        self.cli_set(vpn_path + ["ike-group", ike_group, "proposal", "1", "hash", "sha1"])
        self.cli_set(vpn_path + ["ike-group", ike_group, "proposal", "2", "dh-group", "2"])
        self.cli_set(vpn_path + ["ike-group", ike_group, "proposal", "2", "encryption", "aes128"])
        self.cli_set(vpn_path + ["ike-group", ike_group, "proposal", "2", "hash", "sha1"])

        # Profile - Not doing full DMVPN checks here, just want to verify the profile name in the output
        self.cli_set(vpn_path + ["interface", "eth0"])
        self.cli_set(vpn_path + ["profile", nhrp_profile, "authentication", "mode", "pre-shared-secret"])
        self.cli_set(vpn_path + ["profile", nhrp_profile, "authentication", "pre-shared-secret", ipsec_secret])
        self.cli_set(vpn_path + ["profile", nhrp_profile, "bind", "tunnel", tunnel_if])
        self.cli_set(vpn_path + ["profile", nhrp_profile, "esp-group", esp_group])
        self.cli_set(vpn_path + ["profile", nhrp_profile, "ike-group", ike_group])

        self.cli_commit()

        frrconfig = self.getFRRconfig(f'interface {tunnel_if}', stop_section='^exit')
        self.assertIn(f'interface {tunnel_if}', frrconfig)
        self.assertIn(f' ip nhrp authentication {nhrp_secret}', frrconfig)
        self.assertIn(f' ip nhrp holdtime {nhrp_holdtime}', frrconfig)
        self.assertIn(f' ip nhrp map multicast {nhs_tunnelip}', frrconfig)
        self.assertIn(f' ip nhrp redirect', frrconfig)
        self.assertIn(f' ip nhrp registration no-unique', frrconfig)
        self.assertIn(f' ip nhrp shortcut', frrconfig)
        self.assertIn(f' ip nhrp network-id {nhrp_networkid}', frrconfig)
        self.assertIn(f' ip nhrp nhs {nhs_tunnelip} nbma {nhs_nbmaip}', frrconfig)
        self.assertIn(f' ip nhrp map {map_tunnelip} {map_nbmaip}', frrconfig)
        self.assertIn(f' tunnel protection vici profile dmvpn-{nhrp_profile}-{tunnel_if}-child',
                      frrconfig)

        nftables_search_multicast = [
            ['chain VYOS_NHRP_MULTICAST_OUTPUT'],
            ['type filter hook output priority filter + 10; policy accept;'],
            [f'oifname "{tunnel_if}"', 'ip daddr 224.0.0.0/24', 'counter', f'log group {multicat_log_group}'],
            [f'oifname "{tunnel_if}"', 'ip daddr 224.0.0.0/24', 'counter', 'drop'],
            ['chain VYOS_NHRP_MULTICAST_FORWARD'],
            ['type filter hook output priority filter + 10; policy accept;'],
            [f'oifname "{tunnel_if}"', 'ip daddr 224.0.0.0/4', 'counter', f'log group {multicat_log_group}'],
            [f'oifname "{tunnel_if}"', 'ip daddr 224.0.0.0/4', 'counter', 'drop']
        ]

        nftables_search_redirect = [
            ['chain VYOS_NHRP_REDIRECT_FORWARD'],
            ['type filter hook forward priority filter + 10; policy accept;'],
            [f'iifname "{tunnel_if}" oifname "{tunnel_if}"', 'meter loglimit-0 size 65535 { ip daddr & 255.255.255.0 . ip saddr & 255.255.255.0 timeout 1m limit rate 4/minute burst 1 packets }', 'counter', f'log group {redirect_log_group}']
        ]
        self.verify_nftables(nftables_search_multicast, 'ip vyos_nhrp_multicast')
        self.verify_nftables(nftables_search_redirect, 'ip vyos_nhrp_redirect')

        self.assertTrue(process_named_running(PROCESS_NAME))

    def test_02_nhrp_multiple_tunnels(self):
        # Multiple NHRP tunnels with 'redirect' enabled must each receive a
        # uniquely named nftables meter. The meter name used to be hardcoded
        # ('loglimit-0'), so a second redirect-enabled tunnel produced a
        # duplicate set, 'nft' rejected the whole ruleset, and the commit failed
        # with "Failed to apply NHRP tunnel firewall rules".
        redirect_log_group = '1'
        multicast_log_group = '2'
        tunnels = {
            'tun100': {
                'address': '172.16.253.134/32',
                'source': '192.0.2.134',
                'key': '1',
                'network_id': '1',
                'meter': 'loglimit-0',
            },
            'tun101': {
                'address': '172.16.254.134/32',
                'source': '192.0.2.135',
                'key': '2',
                'network_id': '2',
                'meter': 'loglimit-1',
            },
        }

        for tunnel_if, tunnel_conf in tunnels.items():
            # Tunnel interface (mGRE)
            self.cli_set(tunnel_path + [tunnel_if, 'address', tunnel_conf['address']])
            self.cli_set(tunnel_path + [tunnel_if, 'encapsulation', 'gre'])
            self.cli_set(
                tunnel_path + [tunnel_if, 'source-address', tunnel_conf['source']]
            )
            self.cli_set(tunnel_path + [tunnel_if, 'enable-multicast'])
            self.cli_set(
                tunnel_path + [tunnel_if, 'parameters', 'ip', 'key', tunnel_conf['key']]
            )

            # NHRP - 'redirect' and 'multicast' are the options that render
            # nftables rules for the tunnel
            self.cli_set(
                nhrp_path
                + ['tunnel', tunnel_if, 'network-id', tunnel_conf['network_id']]
            )
            self.cli_set(nhrp_path + ['tunnel', tunnel_if, 'redirect'])
            self.cli_set(nhrp_path + ['tunnel', tunnel_if, 'multicast', 'dynamic'])

        # Must not raise "Failed to apply NHRP tunnel firewall rules"
        self.cli_commit()

        # Each redirect-enabled tunnel gets its own meter (loglimit-0,
        # loglimit-1, ...) and each multicast-enabled tunnel its own log rule.
        nftables_search_redirect = [['chain VYOS_NHRP_REDIRECT_FORWARD']]
        nftables_search_multicast = [['chain VYOS_NHRP_MULTICAST_OUTPUT']]
        for tunnel_if, tunnel_conf in tunnels.items():
            nftables_search_redirect.append(
                [
                    f'iifname "{tunnel_if}" oifname "{tunnel_if}"',
                    f'meter {tunnel_conf["meter"]} size 65535',
                    'counter',
                    f'log group {redirect_log_group}',
                ]
            )
            nftables_search_multicast.append(
                [
                    f'oifname "{tunnel_if}"',
                    'ip daddr 224.0.0.0/24',
                    'counter',
                    f'log group {multicast_log_group}',
                ]
            )

        self.verify_nftables(nftables_search_redirect, 'ip vyos_nhrp_redirect')
        self.verify_nftables(nftables_search_multicast, 'ip vyos_nhrp_multicast')

        self.assertTrue(process_named_running(PROCESS_NAME))

if __name__ == '__main__':
    unittest.main(verbosity=2, failfast=VyOSUnitTestSHIM.TestCase.debug_on())