summaryrefslogtreecommitdiff
path: root/src/conf_mode/protocols_mpls.py
blob: 84948baf4835658517cc29706852d7014d416da9 (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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#!/usr/bin/env python3
#
# Copyright (C) 2019-2020 VyOS maintainers and contributors
#
# 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 os

from vyos.config import Config
from vyos import ConfigError
from vyos.util import call
from vyos.template import render

from vyos import airbag
airbag.enable()

config_file = r'/tmp/ldpd.frr'

def sysctl(name, value):
    call('sysctl -wq {}={}'.format(name, value))

def get_config(config=None):
    if config:
        conf = config
    else:
        conf = Config()
    mpls_conf = {
        'router_id'  : None,
        'mpls_ldp'   : False,
        'old_parameters' : {
                'no_ttl_propagation'          : False,
                'maximum_ttl'                 : None
        },
        'parameters' : {
                'no_ttl_propagation'          : False,
                'maximum_ttl'                 : None
        },
        'old_ldp'    : {
                'interfaces'                  : [],
                'neighbors'                   : {},
                'd_transp_ipv4'               : None,
                'd_transp_ipv6'               : None,
                'hello_ipv4_holdtime'         : None,
                'hello_ipv4_interval'         : None,
                'hello_ipv6_holdtime'         : None,
                'hello_ipv6_interval'         : None,
                'ses_ipv4_hold'               : None,
                'ses_ipv6_hold'               : None,
                'export_ipv4_exp'             : False,
                'export_ipv6_exp'             : False,
                'cisco_interop_tlv'           : False,
                'transport_prefer_ipv4'       : False,
                'target_ipv4_addresses'       : [],
                'target_ipv6_addresses'       : [],
                'target_ipv4_enable'          : False,
                'target_ipv6_enable'          : False,
                'target_ipv4_hello_int'       : None,
                'target_ipv6_hello_int'       : None,
                'target_ipv4_hello_hold'      : None,
                'target_ipv6_hello_hold'      : None
        },
        'ldp'        : {
                'interfaces'                  : [],
                'neighbors'                   : {},
                'd_transp_ipv4'               : None,
                'd_transp_ipv6'               : None,
                'hello_ipv4_holdtime'         : None,
                'hello_ipv4_interval'         : None,
                'hello_ipv6_holdtime'         : None,
                'hello_ipv6_interval'         : None,
                'ses_ipv4_hold'               : None,
                'ses_ipv6_hold'               : None,
                'export_ipv4_exp'             : False,
                'export_ipv6_exp'             : False,
                'cisco_interop_tlv'           : False,
                'transport_prefer_ipv4'       : False,
                'target_ipv4_addresses'       : [],
                'target_ipv6_addresses'       : [],
                'target_ipv4_enable'          : False,
                'target_ipv6_enable'          : False,
                'target_ipv4_hello_int'       : None,
                'target_ipv6_hello_int'       : None,
                'target_ipv4_hello_hold'      : None,
                'target_ipv6_hello_hold'      : None
        }
    }
    if not (conf.exists('protocols mpls') or conf.exists_effective('protocols mpls')):
        return None
    
    # If LDP is defined then enable LDP portion of code
    if conf.exists('protocols mpls ldp'):
        mpls_conf['mpls_ldp'] = True

    # Set to MPLS hierarchy configuration level
    conf.set_level('protocols mpls')
        
    # Get no_ttl_propagation
    if conf.exists_effective('parameters no-propagate-ttl'):
        mpls_conf['old_parameters']['no_ttl_propagation'] = True

    if conf.exists('parameters no-propagate-ttl'):
        mpls_conf['parameters']['no_ttl_propagation'] = True

    # Get maximum_ttl
    if conf.exists_effective('parameters maximum-ttl'):
        mpls_conf['old_parameters']['maximum_ttl'] = conf.return_effective_value('parameters maximum-ttl')

    if conf.exists('parameters maximum-ttl'):
        mpls_conf['parameters']['maximum_ttl'] = conf.return_value('parameters maximum-ttl')

    # Set to LDP hierarchy configuration level
    conf.set_level('protocols mpls ldp')

    # Get router-id
    if conf.exists_effective('router-id'):
        mpls_conf['old_router_id'] = conf.return_effective_value('router-id')
        
    if conf.exists('router-id'):
        mpls_conf['router_id'] = conf.return_value('router-id')

    # Get hello-ipv4-holdtime
    if conf.exists_effective('discovery hello-ipv4-holdtime'):
        mpls_conf['old_ldp']['hello_ipv4_holdtime'] = conf.return_effective_value('discovery hello-ipv4-holdtime')

    if conf.exists('discovery hello-ipv4-holdtime'):
        mpls_conf['ldp']['hello_ipv4_holdtime'] = conf.return_value('discovery hello-ipv4-holdtime')

    # Get hello-ipv4-interval
    if conf.exists_effective('discovery hello-ipv4-interval'):
        mpls_conf['old_ldp']['hello_ipv4_interval'] = conf.return_effective_value('discovery hello-ipv4-interval')

    if conf.exists('discovery hello-ipv4-interval'):
        mpls_conf['ldp']['hello_ipv4_interval'] = conf.return_value('discovery hello-ipv4-interval')
        
    # Get hello-ipv6-holdtime
    if conf.exists_effective('discovery hello-ipv6-holdtime'):
        mpls_conf['old_ldp']['hello_ipv6_holdtime'] = conf.return_effective_value('discovery hello-ipv6-holdtime')

    if conf.exists('discovery hello-ipv6-holdtime'):
        mpls_conf['ldp']['hello_ipv6_holdtime'] = conf.return_value('discovery hello-ipv6-holdtime')

    # Get hello-ipv6-interval
    if conf.exists_effective('discovery hello-ipv6-interval'):
        mpls_conf['old_ldp']['hello_ipv6_interval'] = conf.return_effective_value('discovery hello-ipv6-interval')

    if conf.exists('discovery hello-ipv6-interval'):
        mpls_conf['ldp']['hello_ipv6_interval'] = conf.return_value('discovery hello-ipv6-interval')

    # Get session-ipv4-holdtime
    if conf.exists_effective('discovery session-ipv4-holdtime'):
        mpls_conf['old_ldp']['ses_ipv4_hold'] = conf.return_effective_value('discovery session-ipv4-holdtime')

    if conf.exists('discovery session-ipv4-holdtime'):
        mpls_conf['ldp']['ses_ipv4_hold'] = conf.return_value('discovery session-ipv4-holdtime')

    # Get session-ipv6-holdtime
    if conf.exists_effective('discovery session-ipv6-holdtime'):
        mpls_conf['old_ldp']['ses_ipv6_hold'] = conf.return_effective_value('discovery session-ipv6-holdtime')

    if conf.exists('discovery session-ipv6-holdtime'):
        mpls_conf['ldp']['ses_ipv6_hold'] = conf.return_value('discovery session-ipv6-holdtime')

    # Get discovery transport-ipv4-address
    if conf.exists_effective('discovery transport-ipv4-address'):
        mpls_conf['old_ldp']['d_transp_ipv4'] = conf.return_effective_value('discovery transport-ipv4-address')

    if conf.exists('discovery transport-ipv4-address'):
        mpls_conf['ldp']['d_transp_ipv4'] = conf.return_value('discovery transport-ipv4-address')

    # Get discovery transport-ipv6-address
    if conf.exists_effective('discovery transport-ipv6-address'):
        mpls_conf['old_ldp']['d_transp_ipv6'] = conf.return_effective_value('discovery transport-ipv6-address')

    if conf.exists('discovery transport-ipv6-address'):
        mpls_conf['ldp']['d_transp_ipv6'] = conf.return_value('discovery transport-ipv6-address')

    # Get export ipv4 explicit-null
    if conf.exists_effective('export ipv4 explicit-null'):
        mpls_conf['old_ldp']['export_ipv4_exp'] = True

    if conf.exists('export ipv4 explicit-null'):
        mpls_conf['ldp']['export_ipv4_exp'] = True

    # Get export ipv6 explicit-null
    if conf.exists_effective('export ipv6 explicit-null'):
        mpls_conf['old_ldp']['export_ipv6_exp'] = True

    if conf.exists('export ipv6 explicit-null'):
        mpls_conf['ldp']['export_ipv6_exp'] = True

    # Get target_ipv4_addresses
    if conf.exists_effective('targeted-neighbor ipv4 address'):
        mpls_conf['old_ldp']['target_ipv4_addresses'] = conf.return_effective_values('targeted-neighbor ipv4 address')

    if conf.exists('targeted-neighbor ipv4 address'):
        mpls_conf['ldp']['target_ipv4_addresses'] = conf.return_values('targeted-neighbor ipv4 address')

    # Get target_ipv4_enable
    if conf.exists_effective('targeted-neighbor ipv4 enable'):
        mpls_conf['old_ldp']['target_ipv4_enable'] = True

    if conf.exists('targeted-neighbor ipv4 enable'):
        mpls_conf['ldp']['target_ipv4_enable'] = True

    # Get target_ipv4_hello_int
    if conf.exists_effective('targeted-neighbor ipv4 hello-interval'):
        mpls_conf['old_ldp']['target_ipv4_hello_int'] = conf.return_effective_value('targeted-neighbor ipv4 hello-interval')

    if conf.exists('targeted-neighbor ipv4 hello-interval'):
        mpls_conf['ldp']['target_ipv4_hello_int'] = conf.return_value('targeted-neighbor ipv4 hello-interval')

    # Get target_ipv4_hello_hold
    if conf.exists_effective('targeted-neighbor ipv4 hello-holdtime'):
        mpls_conf['old_ldp']['target_ipv4_hello_hold'] = conf.return_effective_value('targeted-neighbor ipv4 hello-holdtime')

    if conf.exists('targeted-neighbor ipv4 hello-holdtime'):
        mpls_conf['ldp']['target_ipv4_hello_hold'] = conf.return_value('targeted-neighbor ipv4 hello-holdtime')

    # Get target_ipv6_addresses
    if conf.exists_effective('targeted-neighbor ipv6 address'):
        mpls_conf['old_ldp']['target_ipv6_addresses'] = conf.return_effective_values('targeted-neighbor ipv6 address')

    if conf.exists('targeted-neighbor ipv6 address'):
       mpls_conf['ldp']['target_ipv6_addresses'] = conf.return_values('targeted-neighbor ipv6 address')

    # Get target_ipv6_enable
    if conf.exists_effective('targeted-neighbor ipv6 enable'):
        mpls_conf['old_ldp']['target_ipv6_enable'] = True

    if conf.exists('targeted-neighbor ipv6 enable'):
        mpls_conf['ldp']['target_ipv6_enable'] = True

    # Get target_ipv6_hello_int
    if conf.exists_effective('targeted-neighbor ipv6 hello-interval'):
        mpls_conf['old_ldp']['target_ipv6_hello_int'] = conf.return_effective_value('targeted-neighbor ipv6 hello-interval')

    if conf.exists('targeted-neighbor ipv6 hello-interval'):
        mpls_conf['ldp']['target_ipv6_hello_int'] = conf.return_value('targeted-neighbor ipv6 hello-interval')

    # Get target_ipv6_hello_hold
    if conf.exists_effective('targeted-neighbor ipv6 hello-holdtime'):
        mpls_conf['old_ldp']['target_ipv6_hello_hold'] = conf.return_effective_value('targeted-neighbor ipv6 hello-holdtime')

    if conf.exists('targeted-neighbor ipv6 hello-holdtime'):
        mpls_conf['ldp']['target_ipv6_hello_hold'] = conf.return_value('targeted-neighbor ipv6 hello-holdtime')

    # Get parameters cisco-interop-tlv
    if conf.exists_effective('parameters cisco-interop-tlv'):
        mpls_conf['old_ldp']['cisco_interop_tlv'] = True

    if conf.exists('parameters cisco-interop-tlv'):
        mpls_conf['ldp']['cisco_interop_tlv'] = True

    # Get parameters transport-prefer-ipv4
    if conf.exists_effective('parameters transport-prefer-ipv4'):
        mpls_conf['old_ldp']['transport_prefer_ipv4'] = True

    if conf.exists('parameters transport-prefer-ipv4'):
        mpls_conf['ldp']['transport_prefer_ipv4'] = True

    # Get interfaces
    if conf.exists_effective('interface'):
        mpls_conf['old_ldp']['interfaces'] = conf.return_effective_values('interface')

    if conf.exists('interface'):
        mpls_conf['ldp']['interfaces'] = conf.return_values('interface')

    # Get neighbors
    for neighbor in conf.list_effective_nodes('neighbor'):
        mpls_conf['old_ldp']['neighbors'].update({
            neighbor : {
                'password' : conf.return_effective_value('neighbor {0} password'.format(neighbor), default=''),
                'ttl_security' : conf.return_effective_value('neighbor {0} ttl-security'.format(neighbor), default=''),
                'session_holdtime' : conf.return_effective_value('neighbor {0} session-holdtime'.format(neighbor), default='')
            }
        })

    for neighbor in conf.list_nodes('neighbor'):
        mpls_conf['ldp']['neighbors'].update({
            neighbor : {
                'password' : conf.return_value('neighbor {0} password'.format(neighbor), default=''),
                'ttl_security' : conf.return_value('neighbor {0} ttl-security'.format(neighbor), default=''),
                'session_holdtime' : conf.return_value('neighbor {0} session-holdtime'.format(neighbor), default='')
            }
        })

    return mpls_conf

def operate_mpls_on_intfc(interfaces, action):
    rp_filter = 0
    if action == 1:
        rp_filter = 2
    for iface in interfaces:
        sysctl('net.mpls.conf.{0}.input'.format(iface), action)
        # Operate rp filter
        sysctl('net.ipv4.conf.{0}.rp_filter'.format(iface), rp_filter)

def verify(mpls):
    if mpls is None:
        return None

    if mpls['mpls_ldp']:
        # Require router-id
        if not mpls['router_id']:
            raise ConfigError(f"MPLS ldp router-id is mandatory!")

        # Require discovery transport-address
        if not mpls['ldp']['d_transp_ipv4'] and not mpls['ldp']['d_transp_ipv6']:
            raise ConfigError(f"MPLS ldp discovery transport address is mandatory!")

        # Require interface
        if not mpls['ldp']['interfaces']:
            raise ConfigError(f"MPLS ldp interface is mandatory!")

def generate(mpls):
    if mpls is None:
        return None

    render(config_file, 'frr/ldpd.frr.tmpl', mpls)
    return None

def apply(mpls):
    if mpls is None:
        return None

    # Set number of entries in the platform label table
    if mpls['mpls_ldp']:
        sysctl('net.mpls.platform_labels', '1048575')
    else:
        sysctl('net.mpls.platform_labels', '0')

    # Choose whether to copy IP TTL to MPLS header TTL
    if mpls['parameters']['no_ttl_propagation']:
        sysctl('net.mpls.ip_ttl_propagate', '0')
    else:
        sysctl('net.mpls.ip_ttl_propagate', '1')
        
    # Choose whether to limit maximum MPLS header TTL
    if mpls['parameters']['maximum_ttl']:
        sysctl('net.mpls.default_ttl', '%s' %(mpls['parameters']['maximum_ttl']))
    else:
        sysctl('net.mpls.default_ttl', '255')    
    
    # Allow mpls on interfaces
    operate_mpls_on_intfc(mpls['ldp']['interfaces'], 1)

    # Disable mpls on deleted interfaces
    diactive_ifaces = set(mpls['old_ldp']['interfaces']).difference(mpls['ldp']['interfaces'])
    operate_mpls_on_intfc(diactive_ifaces, 0)

    if os.path.exists(config_file):
        call(f'vtysh -d ldpd -f {config_file}')
        os.remove(config_file)

    return None

if __name__ == '__main__':
    try:
        c = get_config()
        verify(c)
        generate(c)
        apply(c)
    except ConfigError as e:
        print(e)
        exit(1)