FIX: Crypto execution order
[csit.git] / resources / libraries / python / IPsecUtil.py
1 # Copyright (c) 2016 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13
14 """IPsec utilities library."""
15
16 import os
17 from ipaddress import ip_network, ip_address
18
19 from enum import Enum
20
21 from resources.libraries.python.VatExecutor import VatExecutor
22 from resources.libraries.python.topology import Topology
23 from resources.libraries.python.VatJsonUtil import VatJsonUtil
24
25
26 class PolicyAction(Enum):
27     """Policy actions."""
28     BYPASS = 'bypass'
29     DISCARD = 'discard'
30     PROTECT = 'protect'
31
32     def __init__(self, string):
33         self.string = string
34
35
36 class CryptoAlg(Enum):
37     """Encryption algorithms."""
38     AES_CBC_128 = ('aes-cbc-128', 'AES-CBC', 16)
39     AES_CBC_192 = ('aes-cbc-192', 'AES-CBC', 24)
40     AES_CBC_256 = ('aes-cbc-256', 'AES-CBC', 32)
41     AES_GCM_128 = ('aes-gcm-128', 'AES-GCM', 20)
42
43     def __init__(self, alg_name, scapy_name, key_len):
44         self.alg_name = alg_name
45         self.scapy_name = scapy_name
46         self.key_len = key_len
47
48
49 class IntegAlg(Enum):
50     """Integrity algorithm."""
51     SHA1_96 = ('sha1-96', 'HMAC-SHA1-96', 20)
52     SHA_256_128 = ('sha-256-128', 'SHA2-256-128', 32)
53     SHA_384_192 = ('sha-384-192', 'SHA2-384-192', 48)
54     SHA_512_256 = ('sha-512-256', 'SHA2-512-256', 64)
55     AES_GCM_128 = ('aes-gcm-128', 'AES-GCM', 20)
56
57     def __init__(self, alg_name, scapy_name, key_len):
58         self.alg_name = alg_name
59         self.scapy_name = scapy_name
60         self.key_len = key_len
61
62
63 class IPsecUtil(object):
64     """IPsec utilities."""
65
66     @staticmethod
67     def policy_action_bypass():
68         """Return policy action bypass.
69
70         :returns: PolicyAction enum BYPASS object.
71         :rtype: PolicyAction
72         """
73         return PolicyAction.BYPASS
74
75     @staticmethod
76     def policy_action_discard():
77         """Return policy action discard.
78
79         :returns: PolicyAction enum DISCARD object.
80         :rtype: PolicyAction
81         """
82         return PolicyAction.DISCARD
83
84     @staticmethod
85     def policy_action_protect():
86         """Return policy action protect.
87
88         :returns: PolicyAction enum PROTECT object.
89         :rtype: PolicyAction
90         """
91         return PolicyAction.PROTECT
92
93     @staticmethod
94     def crypto_alg_aes_cbc_128():
95         """Return encryption algorithm aes-cbc-128.
96
97         :returns: CryptoAlg enum AES_CBC_128 object.
98         :rtype: CryptoAlg
99         """
100         return CryptoAlg.AES_CBC_128
101
102     @staticmethod
103     def crypto_alg_aes_cbc_192():
104         """Return encryption algorithm aes-cbc-192.
105
106         :returns: CryptoAlg enum AES_CBC_192 objec.
107         :rtype: CryptoAlg
108         """
109         return CryptoAlg.AES_CBC_192
110
111     @staticmethod
112     def crypto_alg_aes_cbc_256():
113         """Return encryption algorithm aes-cbc-256.
114
115         :returns: CryptoAlg enum AES_CBC_256 object.
116         :rtype: CryptoAlg
117         """
118         return CryptoAlg.AES_CBC_256
119
120     @staticmethod
121     def crypto_alg_aes_gcm_128():
122         """Return encryption algorithm aes-gcm-128.
123
124         :returns: CryptoAlg enum AES_GCM_128 object.
125         :rtype: CryptoAlg
126         """
127         return CryptoAlg.AES_GCM_128
128
129     @staticmethod
130     def get_crypto_alg_key_len(crypto_alg):
131         """Return encryption algorithm key length.
132
133         :param crypto_alg: Encryption algorithm.
134         :type crypto_alg: CryptoAlg
135         :returns: Key length.
136         :rtype: int
137         """
138         return crypto_alg.key_len
139
140     @staticmethod
141     def get_crypto_alg_scapy_name(crypto_alg):
142         """Return encryption algorithm scapy name.
143
144         :param crypto_alg: Encryption algorithm.
145         :type crypto_alg: CryptoAlg
146         :returns: Algorithm scapy name.
147         :rtype: str
148         """
149         return crypto_alg.scapy_name
150
151     @staticmethod
152     def integ_alg_sha1_96():
153         """Return integrity algorithm SHA1-96.
154
155         :returns: IntegAlg enum SHA1_96 object.
156         :rtype: IntegAlg
157         """
158         return IntegAlg.SHA1_96
159
160     @staticmethod
161     def integ_alg_sha_256_128():
162         """Return integrity algorithm SHA-256-128.
163
164         :returns: IntegAlg enum SHA_256_128 object.
165         :rtype: IntegAlg
166         """
167         return IntegAlg.SHA_256_128
168
169     @staticmethod
170     def integ_alg_sha_384_192():
171         """Return integrity algorithm SHA-384-192.
172
173         :returns: IntegAlg enum SHA_384_192 object.
174         :rtype: IntegAlg
175         """
176         return IntegAlg.SHA_384_192
177
178     @staticmethod
179     def integ_alg_sha_512_256():
180         """Return integrity algorithm SHA-512-256.
181
182         :returns: IntegAlg enum SHA_512_256 object.
183         :rtype: IntegAlg
184         """
185         return IntegAlg.SHA_512_256
186
187     @staticmethod
188     def integ_alg_aes_gcm_128():
189         """Return integrity algorithm AES-GCM-128.
190
191         :returns: IntegAlg enum AES_GCM_128 object.
192         :rtype: IntegAlg
193         """
194         return IntegAlg.AES_GCM_128
195
196     @staticmethod
197     def get_integ_alg_key_len(integ_alg):
198         """Return integrity algorithm key length.
199
200         :param integ_alg: Integrity algorithm.
201         :type integ_alg: IntegAlg
202         :returns: Key length.
203         :rtype: int
204         """
205         return integ_alg.key_len
206
207     @staticmethod
208     def get_integ_alg_scapy_name(integ_alg):
209         """Return integrity algorithm scapy name.
210
211         :param integ_alg: Integrity algorithm.
212         :type integ_alg: IntegAlg
213         :returns: Algorithm scapy name.
214         :rtype: str
215         """
216         return integ_alg.scapy_name
217
218     @staticmethod
219     def vpp_ipsec_add_sad_entry(node, sad_id, spi, crypto_alg, crypto_key,
220                                 integ_alg, integ_key, tunnel_src=None,
221                                 tunnel_dst=None):
222         """Create Security Association Database entry on the VPP node.
223
224         :param node: VPP node to add SAD entry on.
225         :param sad_id: SAD entry ID.
226         :param spi: Security Parameter Index of this SAD entry.
227         :param crypto_alg: The encryption algorithm name.
228         :param crypto_key: The encryption key string.
229         :param integ_alg: The integrity algorithm name.
230         :param integ_key: The integrity key string.
231         :param tunnel_src: Tunnel header source IPv4 or IPv6 address. If not
232         specified ESP transport mode is used.
233         :param tunnel_dst: Tunnel header destination IPv4 or IPv6 address. If
234         not specified ESP transport mode is used.
235         :type node: dict
236         :type sad_id: int
237         :type spi: int
238         :type crypto_alg: CryptoAlg
239         :type crypto_key: str
240         :type integ_alg: str
241         :type integ_key: str
242         :type tunnel_src: str
243         :type tunnel_dst: str
244         """
245         ckey = crypto_key.encode('hex')
246         ikey = integ_key.encode('hex')
247         tunnel = 'tunnel_src {0} tunnel_dst {1}'.format(tunnel_src, tunnel_dst)\
248             if tunnel_src is not None and tunnel_dst is not None else ''
249
250         out = VatExecutor.cmd_from_template(node,
251                                             "ipsec/ipsec_sad_add_entry.vat",
252                                             sad_id=sad_id, spi=spi,
253                                             calg=crypto_alg.alg_name, ckey=ckey,
254                                             ialg=integ_alg.alg_name, ikey=ikey,
255                                             tunnel=tunnel)
256         VatJsonUtil.verify_vat_retval(
257             out[0],
258             err_msg='Add SAD entry failed on {0}'.format(node['host']))
259
260     @staticmethod
261     def vpp_ipsec_add_sad_entries(node, n_entries, sad_id, spi, crypto_alg,
262                                   crypto_key, integ_alg, integ_key,
263                                   tunnel_src=None, tunnel_dst=None):
264         """Create multiple Security Association Database entries on VPP node.
265
266         :param node: VPP node to add SAD entry on.
267         :param n_entries: Number of SAD entries to be created.
268         :param sad_id: First SAD entry ID. All subsequent SAD entries will have
269         id incremented by 1.
270         :param spi: Security Parameter Index of first SAD entry. All subsequent
271         SAD entries will have spi incremented by 1.
272         :param crypto_alg: The encryption algorithm name.
273         :param crypto_key: The encryption key string.
274         :param integ_alg: The integrity algorithm name.
275         :param integ_key: The integrity key string.
276         :param tunnel_src: Tunnel header source IPv4 or IPv6 address. If not
277         specified ESP transport mode is used.
278         :param tunnel_dst: Tunnel header destination IPv4 or IPv6 address. If
279         not specified ESP transport mode is used.
280         :type node: dict
281         :type n_entries: int
282         :type sad_id: int
283         :type spi: int
284         :type crypto_alg: CryptoAlg
285         :type crypto_key: str
286         :type integ_alg: IntegAlg
287         :type integ_key: str
288         :type tunnel_src: str
289         :type tunnel_dst: str
290         """
291         tmp_filename = '/tmp/ipsec_sad_{0}_add_del_entry.script'.format(sad_id)
292         ckey = crypto_key.encode('hex')
293         ikey = integ_key.encode('hex')
294         tunnel = 'tunnel_src {0} tunnel_dst {1}'.format(tunnel_src, tunnel_dst)\
295             if tunnel_src is not None and tunnel_dst is not None else ''
296
297         integ = 'integ_alg {0} integ_key {1}'.format(integ_alg.alg_name, ikey)\
298             if crypto_alg.alg_name != 'aes-gcm-128' else ''
299
300         with open(tmp_filename, 'w') as tmp_file:
301             for i in range(0, n_entries):
302                 buf_str = 'ipsec_sad_add_del_entry esp sad_id {0} spi {1} ' \
303                           'crypto_alg {2} crypto_key {3} {4} {5}\n'.format(
304                               sad_id+i, spi+i, crypto_alg.alg_name, ckey, integ,
305                               tunnel)
306                 tmp_file.write(buf_str)
307         vat = VatExecutor()
308         vat.execute_script(tmp_filename, node, timeout=300, json_out=False,
309                            copy_on_execute=True)
310         os.remove(tmp_filename)
311
312     @staticmethod
313     def vpp_ipsec_sa_set_key(node, sa_id, crypto_key, integ_key):
314         """Update Security Association (SA) keys.
315
316         :param node: VPP node to update SA keys.
317         :param sa_id: SAD entry ID.
318         :param crypto_key: The encryption key string.
319         :param integ_key: The integrity key string.
320         :type node: dict
321         :type sa_id: int
322         :type crypto_key: str
323         :type integ_key: str
324         """
325         ckey = crypto_key.encode('hex')
326         ikey = integ_key.encode('hex')
327
328         out = VatExecutor.cmd_from_template(node,
329                                             "ipsec/ipsec_sa_set_key.vat",
330                                             sa_id=sa_id,
331                                             ckey=ckey, ikey=ikey)
332         VatJsonUtil.verify_vat_retval(
333             out[0],
334             err_msg='Update SA key failed on {0}'.format(node['host']))
335
336     @staticmethod
337     def vpp_ipsec_add_spd(node, spd_id):
338         """Create Security Policy Database on the VPP node.
339
340         :param node: VPP node to add SPD on.
341         :param spd_id: SPD ID.
342         :type node: dict
343         :type spd_id: int
344         """
345         out = VatExecutor.cmd_from_template(node, "ipsec/ipsec_spd_add.vat",
346                                             spd_id=spd_id)
347         VatJsonUtil.verify_vat_retval(
348             out[0],
349             err_msg='Add SPD {0} failed on {1}'.format(spd_id, node['host']))
350
351     @staticmethod
352     def vpp_ipsec_spd_add_if(node, spd_id, interface):
353         """Add interface to the Security Policy Database.
354
355         :param node: VPP node.
356         :param spd_id: SPD ID to add interface on.
357         :param interface: Interface name or sw_if_index.
358         :type node: dict
359         :type spd_id: int
360         :type interface: str or int
361         """
362         sw_if_index = Topology.get_interface_sw_index(node, interface)\
363             if isinstance(interface, basestring) else interface
364
365         out = VatExecutor.cmd_from_template(node,
366                                             "ipsec/ipsec_interface_add_spd.vat",
367                                             spd_id=spd_id, sw_if_id=sw_if_index)
368         VatJsonUtil.verify_vat_retval(
369             out[0],
370             err_msg='Add interface {0} to SPD {1} failed on {2}'.format(
371                 interface, spd_id, node['host']))
372
373     @staticmethod
374     def vpp_ipsec_spd_add_entry(node, spd_id, priority, action, inbound=True,
375                                 sa_id=None, laddr_range=None, raddr_range=None,
376                                 proto=None, lport_range=None, rport_range=None):
377         """Create Security Policy Database entry on the VPP node.
378
379         :param node: VPP node to add SPD entry on.
380         :param spd_id: SPD ID to add entry on.
381         :param priority: SPD entry priority, higher number = higher priority.
382         :param action: Policy action.
383         :param inbound: If True policy is for inbound traffic, otherwise
384         outbound.
385         :param sa_id: SAD entry ID for protect action.
386         :param laddr_range: Policy selector local IPv4 or IPv6 address range in
387         format IP/prefix or IP/mask. If no mask is provided, it's considered
388         to be /32.
389         :param raddr_range: Policy selector remote IPv4 or IPv6 address range in
390         format IP/prefix or IP/mask. If no mask is provided, it's considered
391         to be /32.
392         :param proto: Policy selector next layer protocol number.
393         :param lport_range: Policy selector local TCP/UDP port range in format
394         <port_start>-<port_end>.
395         :param rport_range: Policy selector remote TCP/UDP port range in format
396         <port_start>-<port_end>.
397         :type node: dict
398         :type spd_id: int
399         :type priority: int
400         :type action: PolicyAction
401         :type inbound: bool
402         :type sa_id: int
403         :type laddr_range: string
404         :type raddr_range: string
405         :type proto: int
406         :type lport_range: string
407         :type rport_range: string
408         """
409         direction = 'inbound' if inbound else 'outbound'
410
411         act_str = action.value
412         if PolicyAction.PROTECT == action and sa_id is not None:
413             act_str += 'sa_id {0}'.format(sa_id)
414
415         selector = ''
416         if laddr_range is not None:
417             net = ip_network(unicode(laddr_range), strict=False)
418             selector += 'laddr_start {0} laddr_stop {1} '.format(
419                 net.network_address, net.broadcast_address)
420         if raddr_range is not None:
421             net = ip_network(unicode(raddr_range), strict=False)
422             selector += 'raddr_start {0} raddr_stop {1} '.format(
423                 net.network_address, net.broadcast_address)
424         if proto is not None:
425             selector += 'protocol {0} '.format(proto)
426         if lport_range is not None:
427             selector += 'lport_start {p[0]} lport_stop {p[1]} '.format(
428                 p=lport_range.split('-'))
429         if rport_range is not None:
430             selector += 'rport_start {p[0]} rport_stop {p[1]} '.format(
431                 p=rport_range.split('-'))
432
433         out = VatExecutor.cmd_from_template(node,
434                                             "ipsec/ipsec_spd_add_entry.vat",
435                                             spd_id=spd_id, priority=priority,
436                                             action=act_str, direction=direction,
437                                             selector=selector)
438         VatJsonUtil.verify_vat_retval(
439             out[0],
440             err_msg='Add entry to SPD {0} failed on {1}'.format(spd_id,
441                                                                 node['host']))
442
443     @staticmethod
444     def vpp_ipsec_spd_add_entries(node, n_entries, spd_id, priority, inbound,
445                                   sa_id, raddr_ip, raddr_range):
446         """Create multiple Security Policy Database entries on the VPP node.
447
448         :param node: VPP node to add SPD entries on.
449         :param n_entries: Number of SPD entries to be added.
450         :param spd_id: SPD ID to add entries on.
451         :param priority: SPD entries priority, higher number = higher priority.
452         :param inbound: If True policy is for inbound traffic, otherwise
453         outbound.
454         :param sa_id: SAD entry ID for first entry. Each subsequent entry will
455         SAD entry ID incremented by 1.
456         :param raddr_ip: Policy selector remote IPv4 start address for the first
457         entry. Remote IPv4 end address will be calculated depending on
458         raddr_range parameter. Each subsequent entry will have start address
459         next after IPv4 end address of previous entry.
460         :param raddr_range: Mask specifying range of Policy selector Remote IPv4
461         addresses. Valid values are from 1 to 32.
462         :type node: dict
463         :type n_entries: int
464         :type spd_id: int
465         :type priority: int
466         :type inbound: bool
467         :type sa_id: int
468         :type raddr_ip: string
469         :type raddr_range: int
470         """
471         tmp_filename = '/tmp/ipsec_spd_{0}_add_del_entry.script'.format(sa_id)
472
473         direction = 'inbound' if inbound else 'outbound'
474         addr_incr = 1 << (32 - raddr_range)
475         addr_ip = int(ip_address(unicode(raddr_ip)))
476         start_str = 'ipsec_spd_add_del_entry spd_id {0} priority {1} {2} ' \
477                     'action protect sa_id'.format(spd_id, priority, direction)
478         with open(tmp_filename, 'w') as tmp_file:
479             for i in range(0, n_entries):
480                 r_ip_s = ip_address(addr_ip + addr_incr * i)
481                 r_ip_e = ip_address(addr_ip + addr_incr * (i+1) - 1)
482                 buf_str = '{0} {1} raddr_start {2} raddr_stop {3}\n'.format(
483                     start_str, sa_id+i, r_ip_s, r_ip_e)
484                 tmp_file.write(buf_str)
485         vat = VatExecutor()
486         vat.execute_script(tmp_filename, node, timeout=300, json_out=False,
487                            copy_on_execute=True)
488         os.remove(tmp_filename)
489
490     @staticmethod
491     def vpp_ipsec_create_tunnel_interfaces(node1, node2, if1_ip_addr,
492                                            if2_ip_addr, if1_key, if2_key,
493                                            n_tunnels, crypto_alg, crypto_key,
494                                            integ_alg, integ_key, raddr_ip1,
495                                            raddr_ip2, raddr_range):
496         """Create multiple IPsec tunnel interfaces between two VPP nodes.
497
498         :param node1: VPP node 1 to create tunnel interfaces.
499         :param node2: VPP node 2 to create tunnel interfaces.
500         :param if1_ip_addr: VPP node 1 interface IP4 address.
501         :param if2_ip_addr: VPP node 2 interface IP4 address.
502         :param if1_key: VPP node 1 interface key from topology file.
503         :param if2_key: VPP node 2 interface key from topology file.
504         :param n_tunnels: Number of tunnell interfaces to create.
505         :param crypto_alg: The encryption algorithm name.
506         :param crypto_key: The encryption key string.
507         :param integ_alg: The integrity algorithm name.
508         :param integ_key: The integrity key string.
509         :param raddr_ip1: Policy selector remote IPv4 start address for the
510         first tunnel in direction node1->node2.
511         :param raddr_ip2: Policy selector remote IPv4 start address for the
512         first tunnel in direction node2->node1.
513         :param raddr_range: Mask specifying range of Policy selector Remote IPv4
514         addresses. Valid values are from 1 to 32.
515         :type node1: dict
516         :type node2: dict
517         :type if1_ip_addr: str
518         :type if2_ip_addr: str
519         :type if1_key: str
520         :type if2_key: str
521         :type n_tunnels: int
522         :type crypto_alg: CryptoAlg
523         :type crypto_key: str
524         :type integ_alg: IntegAlg
525         :type integ_key: str
526         :type raddr_ip1: string
527         :type raddr_ip2: string
528         :type raddr_range: int
529         """
530         spi_1 = 10000
531         spi_2 = 20000
532
533         raddr_ip1_i = int(ip_address(unicode(raddr_ip1)))
534         raddr_ip2_i = int(ip_address(unicode(raddr_ip2)))
535         addr_incr = 1 << (32 - raddr_range)
536
537         tmp_fn1 = '/tmp/ipsec_create_tunnel_dut1.config'
538         tmp_fn2 = '/tmp/ipsec_create_tunnel_dut2.config'
539
540         ckey = crypto_key.encode('hex')
541         ikey = integ_key.encode('hex')
542
543         vat = VatExecutor()
544         with open(tmp_fn1, 'w') as tmp_f1, open(tmp_fn2, 'w') as tmp_f2:
545             for i in range(0, n_tunnels):
546                 integ = ''
547                 if crypto_alg.alg_name != 'aes-gcm-128':
548                     integ = 'integ_alg {integ_alg} '\
549                             'local_integ_key {local_integ_key} '\
550                             'remote_integ_key {remote_integ_key} '\
551                             .format(integ_alg=integ_alg.alg_name,
552                                     local_integ_key=ikey,
553                                     remote_integ_key=ikey)
554                 dut1_tunnel = 'ipsec_tunnel_if_add_del '\
555                               'local_spi {local_spi} '\
556                               'remote_spi {remote_spi} '\
557                               'crypto_alg {crypto_alg} '\
558                               'local_crypto_key {local_crypto_key} '\
559                               'remote_crypto_key {remote_crypto_key} '\
560                               '{integ} '\
561                               'local_ip {local_ip} '\
562                               'remote_ip {remote_ip}\n'\
563                               .format(local_spi=spi_1+i,
564                                       remote_spi=spi_2+i,
565                                       crypto_alg=crypto_alg.alg_name,
566                                       local_crypto_key=ckey,
567                                       remote_crypto_key=ckey,
568                                       integ=integ,
569                                       local_ip=if1_ip_addr,
570                                       remote_ip=if2_ip_addr)
571                 dut2_tunnel = 'ipsec_tunnel_if_add_del '\
572                               'local_spi {local_spi} '\
573                               'remote_spi {remote_spi} '\
574                               'crypto_alg {crypto_alg} '\
575                               'local_crypto_key {local_crypto_key} '\
576                               'remote_crypto_key {remote_crypto_key} '\
577                               '{integ} '\
578                               'local_ip {local_ip} '\
579                               'remote_ip {remote_ip}\n'\
580                               .format(local_spi=spi_2+i,
581                                       remote_spi=spi_1+i,
582                                       crypto_alg=crypto_alg.alg_name,
583                                       local_crypto_key=ckey,
584                                       remote_crypto_key=ckey,
585                                       integ=integ,
586                                       local_ip=if2_ip_addr,
587                                       remote_ip=if1_ip_addr)
588                 tmp_f1.write(dut1_tunnel)
589                 tmp_f2.write(dut2_tunnel)
590         vat.execute_script(tmp_fn1, node1, timeout=300, json_out=False,
591                            copy_on_execute=True)
592         vat.execute_script(tmp_fn2, node2, timeout=300, json_out=False,
593                            copy_on_execute=True)
594         os.remove(tmp_fn1)
595         os.remove(tmp_fn2)
596
597         with open(tmp_fn1, 'w') as tmp_f1, open(tmp_fn2, 'w') as tmp_f2:
598             for i in range(0, n_tunnels):
599                 raddr_ip1 = ip_address(raddr_ip1_i + addr_incr*i)
600                 raddr_ip2 = ip_address(raddr_ip2_i + addr_incr*i)
601                 dut1_if = Topology.get_interface_name(node1, if1_key)
602                 dut1 = 'ip_add_del_route {raddr}/{mask} via {addr} ipsec{i}\n'\
603                        'exec set interface unnumbered ipsec{i} use {uifc}\n'\
604                        'sw_interface_set_flags ipsec{i} admin-up\n'\
605                        .format(raddr=raddr_ip2, mask=raddr_range,
606                                addr=if2_ip_addr, i=i, uifc=dut1_if)
607                 dut2_if = Topology.get_interface_name(node2, if2_key)
608                 dut2 = 'ip_add_del_route {raddr}/{mask} via {addr} ipsec{i}\n'\
609                        'exec set interface unnumbered ipsec{i} use {uifc}\n'\
610                        'sw_interface_set_flags ipsec{i} admin-up\n'\
611                        .format(raddr=raddr_ip1, mask=raddr_range,
612                                addr=if1_ip_addr, i=i, uifc=dut2_if)
613                 tmp_f1.write(dut1)
614                 tmp_f2.write(dut2)
615
616         vat.execute_script(tmp_fn1, node1, timeout=300, json_out=False,
617                            copy_on_execute=True)
618         vat.execute_script(tmp_fn2, node2, timeout=300, json_out=False,
619                            copy_on_execute=True)
620         os.remove(tmp_fn1)
621         os.remove(tmp_fn2)
622
623     @staticmethod
624     def vpp_ipsec_add_multiple_tunnels(node1, node2, interface1, interface2,
625                                        n_tunnels, crypto_alg, crypto_key,
626                                        integ_alg, integ_key, tunnel_ip1,
627                                        tunnel_ip2, raddr_ip1, raddr_ip2,
628                                        raddr_range):
629         """Create multiple IPsec tunnels between two VPP nodes.
630
631         :param node1: VPP node 1 to create tunnels.
632         :param node2: VPP node 2 to create tunnels.
633         :param interface1: Interface name or sw_if_index on node 1.
634         :param interface2: Interface name or sw_if_index on node 2.
635         :param n_tunnels: Number of tunnels to create.
636         :param crypto_alg: The encryption algorithm name.
637         :param crypto_key: The encryption key string.
638         :param integ_alg: The integrity algorithm name.
639         :param integ_key: The integrity key string.
640         :param tunnel_ip1: Tunnel node1 IPv4 address.
641         :param tunnel_ip2: Tunnel node2 IPv4 address.
642         :param raddr_ip1: Policy selector remote IPv4 start address for the
643         first tunnel in direction node1->node2.
644         :param raddr_ip2: Policy selector remote IPv4 start address for the
645         first tunnel in direction node2->node1.
646         :param raddr_range: Mask specifying range of Policy selector Remote IPv4
647         addresses. Valid values are from 1 to 32.
648         :type node1: dict
649         :type node2: dict
650         :type interface1: str or int
651         :type interface2: str or int
652         :type n_tunnels: int
653         :type crypto_alg: CryptoAlg
654         :type crypto_key: str
655         :type integ_alg: str
656         :type integ_key: str
657         :type tunnel_ip1: str
658         :type tunnel_ip2: str
659         :type raddr_ip1: string
660         :type raddr_ip2: string
661         :type raddr_range: int
662         """
663         spd_id = 1
664         p_hi = 100
665         p_lo = 10
666         sa_id_1 = 10000
667         sa_id_2 = 20000
668         spi_1 = 30000
669         spi_2 = 40000
670         proto = 50
671
672         IPsecUtil.vpp_ipsec_add_spd(node1, spd_id)
673         IPsecUtil.vpp_ipsec_spd_add_if(node1, spd_id, interface1)
674         IPsecUtil.vpp_ipsec_spd_add_entry(node1, spd_id, p_hi,
675                                           PolicyAction.BYPASS, inbound=False,
676                                           proto=proto)
677         IPsecUtil.vpp_ipsec_spd_add_entry(node1, spd_id, p_hi,
678                                           PolicyAction.BYPASS, inbound=True,
679                                           proto=proto)
680
681         IPsecUtil.vpp_ipsec_add_spd(node2, spd_id)
682         IPsecUtil.vpp_ipsec_spd_add_if(node2, spd_id, interface2)
683         IPsecUtil.vpp_ipsec_spd_add_entry(node2, spd_id, p_hi,
684                                           PolicyAction.BYPASS, inbound=False,
685                                           proto=proto)
686         IPsecUtil.vpp_ipsec_spd_add_entry(node2, spd_id, p_hi,
687                                           PolicyAction.BYPASS, inbound=True,
688                                           proto=proto)
689
690         IPsecUtil.vpp_ipsec_add_sad_entries(node1, n_tunnels, sa_id_1, spi_1,
691                                             crypto_alg, crypto_key, integ_alg,
692                                             integ_key, tunnel_ip1, tunnel_ip2)
693
694         IPsecUtil.vpp_ipsec_spd_add_entries(node1, n_tunnels, spd_id, p_lo,
695                                             False, sa_id_1, raddr_ip2,
696                                             raddr_range)
697
698         IPsecUtil.vpp_ipsec_add_sad_entries(node2, n_tunnels, sa_id_1, spi_1,
699                                             crypto_alg, crypto_key, integ_alg,
700                                             integ_key, tunnel_ip1, tunnel_ip2)
701
702         IPsecUtil.vpp_ipsec_spd_add_entries(node2, n_tunnels, spd_id, p_lo,
703                                             True, sa_id_1, raddr_ip2,
704                                             raddr_range)
705
706         IPsecUtil.vpp_ipsec_add_sad_entries(node2, n_tunnels, sa_id_2, spi_2,
707                                             crypto_alg, crypto_key, integ_alg,
708                                             integ_key, tunnel_ip2, tunnel_ip1)
709
710         IPsecUtil.vpp_ipsec_spd_add_entries(node2, n_tunnels, spd_id, p_lo,
711                                             False, sa_id_2, raddr_ip1,
712                                             raddr_range)
713
714         IPsecUtil.vpp_ipsec_add_sad_entries(node1, n_tunnels, sa_id_2, spi_2,
715                                             crypto_alg, crypto_key, integ_alg,
716                                             integ_key, tunnel_ip2, tunnel_ip1)
717
718         IPsecUtil.vpp_ipsec_spd_add_entries(node1, n_tunnels, spd_id, p_lo,
719                                             True, sa_id_2, raddr_ip1,
720                                             raddr_range)
721
722     @staticmethod
723     def vpp_ipsec_show(node):
724         """Run "show ipsec" debug CLI command.
725
726         :param node: Node to run command on.
727         :type node: dict
728         """
729         VatExecutor().execute_script("ipsec/ipsec_show.vat", node,
730                                      json_out=False)