blob: 8deb823806fbcf2ced7e893904e011d6c22d8e3f [file] [log] [blame]
Filip Tehlar84962d12020-09-08 06:08:05 +00001from ipaddress import IPv4Address, AddressValueError
Filip Tehlar12b517b2020-04-26 18:05:05 +00002from vpp_object import VppObject
Filip Tehlar12b517b2020-04-26 18:05:05 +00003
4
5class AuthMethod:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02006 v = {"rsa-sig": 1, "shared-key": 2}
Filip Tehlar12b517b2020-04-26 18:05:05 +00007
8 @staticmethod
Klement Sekerad9b0c6f2022-04-26 19:02:15 +02009 def value(key):
10 return AuthMethod.v[key]
Filip Tehlar12b517b2020-04-26 18:05:05 +000011
12
13class IDType:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020014 v = {"ip4-addr": 1, "fqdn": 2, "ip6-addr": 5}
Filip Tehlar12b517b2020-04-26 18:05:05 +000015
16 @staticmethod
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020017 def value(key):
18 return IDType.v[key]
Filip Tehlar12b517b2020-04-26 18:05:05 +000019
20
21class Profile(VppObject):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020022 """IKEv2 profile"""
23
Filip Tehlar12b517b2020-04-26 18:05:05 +000024 def __init__(self, test, profile_name):
25 self.test = test
26 self.vapi = test.vapi
27 self.profile_name = profile_name
Filip Tehlar459d17b2020-07-06 15:40:08 +000028 self.udp_encap = False
Filip Tehlard7fc12f2020-10-30 04:47:44 +000029 self.natt = True
30
31 def disable_natt(self):
32 self.natt = False
Filip Tehlar12b517b2020-04-26 18:05:05 +000033
34 def add_auth(self, method, data, is_hex=False):
35 if isinstance(method, int):
36 m = method
37 elif isinstance(method, str):
38 m = AuthMethod.value(method)
39 else:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020040 raise Exception("unsupported type {}".format(method))
41 self.auth = {"auth_method": m, "data": data, "is_hex": is_hex}
Filip Tehlar12b517b2020-04-26 18:05:05 +000042
43 def add_local_id(self, id_type, data):
44 if isinstance(id_type, str):
45 t = IDType.value(id_type)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020046 self.local_id = {"id_type": t, "data": data, "is_local": True}
Filip Tehlar12b517b2020-04-26 18:05:05 +000047
48 def add_remote_id(self, id_type, data):
49 if isinstance(id_type, str):
50 t = IDType.value(id_type)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020051 self.remote_id = {"id_type": t, "data": data, "is_local": False}
Filip Tehlar12b517b2020-04-26 18:05:05 +000052
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020053 def add_local_ts(
54 self, start_addr, end_addr, start_port=0, end_port=0xFFFF, proto=0, is_ip4=True
55 ):
Filip Tehlar84962d12020-09-08 06:08:05 +000056 self.ts_is_ip4 = is_ip4
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020057 self.local_ts = {
58 "is_local": True,
59 "protocol_id": proto,
60 "start_port": start_port,
61 "end_port": end_port,
62 "start_addr": start_addr,
63 "end_addr": end_addr,
64 }
Filip Tehlar12b517b2020-04-26 18:05:05 +000065
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020066 def add_remote_ts(
67 self, start_addr, end_addr, start_port=0, end_port=0xFFFF, proto=0
68 ):
Filip Tehlar84962d12020-09-08 06:08:05 +000069 try:
70 IPv4Address(start_addr)
71 is_ip4 = True
72 except AddressValueError:
73 is_ip4 = False
74 self.ts_is_ip4 = is_ip4
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020075 self.remote_ts = {
76 "is_local": False,
77 "protocol_id": proto,
78 "start_port": start_port,
79 "end_port": end_port,
80 "start_addr": start_addr,
81 "end_addr": end_addr,
82 }
Filip Tehlar12b517b2020-04-26 18:05:05 +000083
Filip Tehlaraf2cc642021-02-22 16:15:51 +000084 def add_responder_hostname(self, hn):
85 self.responder_hostname = hn
86
Filip Tehlar459d17b2020-07-06 15:40:08 +000087 def add_responder(self, responder):
88 self.responder = responder
89
90 def add_ike_transforms(self, tr):
91 self.ike_transforms = tr
92
93 def add_esp_transforms(self, tr):
94 self.esp_transforms = tr
95
96 def set_udp_encap(self, udp_encap):
97 self.udp_encap = udp_encap
98
99 def set_lifetime_data(self, data):
100 self.lifetime_data = data
101
102 def set_ipsec_over_udp_port(self, port):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200103 self.ipsec_udp_port = {"is_set": 1, "port": port}
Filip Tehlar459d17b2020-07-06 15:40:08 +0000104
105 def set_tunnel_interface(self, sw_if_index):
106 self.tun_itf = sw_if_index
107
Filip Tehlar12b517b2020-04-26 18:05:05 +0000108 def object_id(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200109 return "ikev2-profile-%s" % self.profile_name
Filip Tehlar12b517b2020-04-26 18:05:05 +0000110
111 def remove_vpp_config(self):
112 self.vapi.ikev2_profile_add_del(name=self.profile_name, is_add=False)
113
114 def add_vpp_config(self):
115 self.vapi.ikev2_profile_add_del(name=self.profile_name, is_add=True)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200116 if hasattr(self, "auth"):
117 self.vapi.ikev2_profile_set_auth(
118 name=self.profile_name, data_len=len(self.auth["data"]), **self.auth
119 )
120 if hasattr(self, "local_id"):
121 self.vapi.ikev2_profile_set_id(
122 name=self.profile_name,
123 data_len=len(self.local_id["data"]),
124 **self.local_id,
125 )
126 if hasattr(self, "remote_id"):
127 self.vapi.ikev2_profile_set_id(
128 name=self.profile_name,
129 data_len=len(self.remote_id["data"]),
130 **self.remote_id,
131 )
132 if hasattr(self, "local_ts"):
133 self.vapi.ikev2_profile_set_ts(name=self.profile_name, ts=self.local_ts)
Filip Tehlar459d17b2020-07-06 15:40:08 +0000134
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200135 if hasattr(self, "remote_ts"):
136 self.vapi.ikev2_profile_set_ts(name=self.profile_name, ts=self.remote_ts)
Filip Tehlar459d17b2020-07-06 15:40:08 +0000137
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200138 if hasattr(self, "responder"):
139 self.vapi.ikev2_set_responder(
140 name=self.profile_name, responder=self.responder
141 )
Filip Tehlar459d17b2020-07-06 15:40:08 +0000142
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200143 if hasattr(self, "responder_hostname"):
Filip Tehlaraf2cc642021-02-22 16:15:51 +0000144 print(self.responder_hostname)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200145 self.vapi.ikev2_set_responder_hostname(
146 name=self.profile_name, **self.responder_hostname
147 )
Filip Tehlaraf2cc642021-02-22 16:15:51 +0000148
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200149 if hasattr(self, "ike_transforms"):
150 self.vapi.ikev2_set_ike_transforms(
151 name=self.profile_name, tr=self.ike_transforms
152 )
Filip Tehlar459d17b2020-07-06 15:40:08 +0000153
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200154 if hasattr(self, "esp_transforms"):
155 self.vapi.ikev2_set_esp_transforms(
156 name=self.profile_name, tr=self.esp_transforms
157 )
Filip Tehlar459d17b2020-07-06 15:40:08 +0000158
159 if self.udp_encap:
160 self.vapi.ikev2_profile_set_udp_encap(name=self.profile_name)
161
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200162 if hasattr(self, "lifetime_data"):
163 self.vapi.ikev2_set_sa_lifetime(
164 name=self.profile_name, **self.lifetime_data
165 )
Filip Tehlar459d17b2020-07-06 15:40:08 +0000166
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200167 if hasattr(self, "ipsec_udp_port"):
168 self.vapi.ikev2_profile_set_ipsec_udp_port(
169 name=self.profile_name, **self.ipsec_udp_port
170 )
171 if hasattr(self, "tun_itf"):
172 self.vapi.ikev2_set_tunnel_interface(
173 name=self.profile_name, sw_if_index=self.tun_itf
174 )
Filip Tehlar12b517b2020-04-26 18:05:05 +0000175
Filip Tehlard7fc12f2020-10-30 04:47:44 +0000176 if not self.natt:
177 self.vapi.ikev2_profile_disable_natt(name=self.profile_name)
178
Filip Tehlar12b517b2020-04-26 18:05:05 +0000179 def query_vpp_config(self):
Filip Tehlar459d17b2020-07-06 15:40:08 +0000180 res = self.vapi.ikev2_profile_dump()
181 for r in res:
182 if r.profile.name == self.profile_name:
183 return r.profile
184 return None