blob: 9d44425ec9f43a674635bb73e6df4e72a45e0936 [file] [log] [blame]
Klement Sekerad3ba5152017-02-14 03:09:17 +01001""" BFD protocol implementation """
2
Klement Sekerab17dd962017-01-09 07:43:48 +01003from random import randint
Klement Sekerad3ba5152017-02-14 03:09:17 +01004from socket import AF_INET, AF_INET6, inet_pton
5from scapy.all import bind_layers
6from scapy.layers.inet import UDP
7from scapy.packet import Packet
8from scapy.fields import BitField, BitEnumField, XByteField, FlagsField,\
Klement Sekeraaeeac3b2017-02-14 07:11:52 +01009 ConditionalField, StrField
Klement Sekerad3ba5152017-02-14 03:09:17 +010010from vpp_object import VppObject
Klement Sekera0e3c0de2016-09-29 14:43:44 +020011from util import NumericConstant
Jakub Grajciar4682feb2019-09-02 13:28:52 +020012from vpp_papi import VppEnum
Klement Sekera0e3c0de2016-09-29 14:43:44 +020013
14
15class BFDDiagCode(NumericConstant):
16 """ BFD Diagnostic Code """
17 no_diagnostic = 0
18 control_detection_time_expired = 1
19 echo_function_failed = 2
20 neighbor_signaled_session_down = 3
21 forwarding_plane_reset = 4
22 path_down = 5
23 concatenated_path_down = 6
24 administratively_down = 7
25 reverse_concatenated_path_down = 8
26
27 desc_dict = {
28 no_diagnostic: "No diagnostic",
29 control_detection_time_expired: "Control Detection Time Expired",
30 echo_function_failed: "Echo Function Failed",
31 neighbor_signaled_session_down: "Neighbor Signaled Session Down",
32 forwarding_plane_reset: "Forwarding Plane Reset",
33 path_down: "Path Down",
34 concatenated_path_down: "Concatenated Path Down",
35 administratively_down: "Administratively Down",
36 reverse_concatenated_path_down: "Reverse Concatenated Path Down",
37 }
38
Klement Sekera0e3c0de2016-09-29 14:43:44 +020039
40class BFDState(NumericConstant):
41 """ BFD State """
42 admin_down = 0
43 down = 1
44 init = 2
45 up = 3
46
47 desc_dict = {
48 admin_down: "AdminDown",
49 down: "Down",
50 init: "Init",
51 up: "Up",
52 }
53
Klement Sekera0e3c0de2016-09-29 14:43:44 +020054
Klement Sekerab17dd962017-01-09 07:43:48 +010055class BFDAuthType(NumericConstant):
56 """ BFD Authentication Type """
57 no_auth = 0
58 simple_pwd = 1
59 keyed_md5 = 2
60 meticulous_keyed_md5 = 3
61 keyed_sha1 = 4
62 meticulous_keyed_sha1 = 5
63
64 desc_dict = {
65 no_auth: "No authentication",
66 simple_pwd: "Simple Password",
67 keyed_md5: "Keyed MD5",
68 meticulous_keyed_md5: "Meticulous Keyed MD5",
69 keyed_sha1: "Keyed SHA1",
70 meticulous_keyed_sha1: "Meticulous Keyed SHA1",
71 }
72
Klement Sekerab17dd962017-01-09 07:43:48 +010073
74def bfd_is_auth_used(pkt):
Klement Sekerad3ba5152017-02-14 03:09:17 +010075 """ is packet authenticated? """
Klement Sekerab17dd962017-01-09 07:43:48 +010076 return "A" in pkt.sprintf("%BFD.flags%")
77
78
79def bfd_is_simple_pwd_used(pkt):
Klement Sekerad3ba5152017-02-14 03:09:17 +010080 """ is simple password authentication used? """
Klement Sekerab17dd962017-01-09 07:43:48 +010081 return bfd_is_auth_used(pkt) and pkt.auth_type == BFDAuthType.simple_pwd
82
83
84def bfd_is_sha1_used(pkt):
Klement Sekerad3ba5152017-02-14 03:09:17 +010085 """ is sha1 authentication used? """
Klement Sekerab17dd962017-01-09 07:43:48 +010086 return bfd_is_auth_used(pkt) and pkt.auth_type in \
87 (BFDAuthType.keyed_sha1, BFDAuthType.meticulous_keyed_sha1)
88
89
90def bfd_is_md5_used(pkt):
Klement Sekerad3ba5152017-02-14 03:09:17 +010091 """ is md5 authentication used? """
Klement Sekerab17dd962017-01-09 07:43:48 +010092 return bfd_is_auth_used(pkt) and pkt.auth_type in \
93 (BFDAuthType.keyed_md5, BFDAuthType.meticulous_keyed_md5)
94
95
96def bfd_is_md5_or_sha1_used(pkt):
Klement Sekerad3ba5152017-02-14 03:09:17 +010097 """ is md5 or sha1 used? """
Klement Sekerab17dd962017-01-09 07:43:48 +010098 return bfd_is_md5_used(pkt) or bfd_is_sha1_used(pkt)
99
100
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200101class BFD(Packet):
Klement Sekerad3ba5152017-02-14 03:09:17 +0100102 """ BFD protocol layer for scapy """
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200103
104 udp_dport = 3784 #: BFD destination port per RFC 5881
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100105 udp_dport_echo = 3785 # : BFD destination port for ECHO per RFC 5881
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200106 udp_sport_min = 49152 #: BFD source port min value per RFC 5881
107 udp_sport_max = 65535 #: BFD source port max value per RFC 5881
Klement Sekerab17dd962017-01-09 07:43:48 +0100108 bfd_pkt_len = 24 # : length of BFD pkt without authentication section
109 sha1_auth_len = 28 # : length of authentication section if SHA1 used
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200110
111 name = "BFD"
112
113 fields_desc = [
114 BitField("version", 1, 3),
115 BitEnumField("diag", 0, 5, BFDDiagCode.desc_dict),
116 BitEnumField("state", 0, 2, BFDState.desc_dict),
Klement Sekerab17dd962017-01-09 07:43:48 +0100117 FlagsField("flags", 0, 6, ['M', 'D', 'A', 'C', 'F', 'P']),
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200118 XByteField("detect_mult", 0),
Klement Sekerab17dd962017-01-09 07:43:48 +0100119 BitField("length", bfd_pkt_len, 8),
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200120 BitField("my_discriminator", 0, 32),
121 BitField("your_discriminator", 0, 32),
122 BitField("desired_min_tx_interval", 0, 32),
123 BitField("required_min_rx_interval", 0, 32),
Klement Sekerab17dd962017-01-09 07:43:48 +0100124 BitField("required_min_echo_rx_interval", 0, 32),
125 ConditionalField(
126 BitEnumField("auth_type", 0, 8, BFDAuthType.desc_dict),
127 bfd_is_auth_used),
128 ConditionalField(BitField("auth_len", 0, 8), bfd_is_auth_used),
129 ConditionalField(BitField("auth_key_id", 0, 8), bfd_is_auth_used),
130 ConditionalField(BitField("auth_reserved", 0, 8),
131 bfd_is_md5_or_sha1_used),
132 ConditionalField(
133 BitField("auth_seq_num", 0, 32), bfd_is_md5_or_sha1_used),
134 ConditionalField(StrField("auth_key_hash", "0" * 16), bfd_is_md5_used),
135 ConditionalField(
136 StrField("auth_key_hash", "0" * 20), bfd_is_sha1_used),
137 ]
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200138
139 def mysummary(self):
140 return self.sprintf("BFD(my_disc=%BFD.my_discriminator%,"
141 "your_disc=%BFD.your_discriminator%)")
142
Klement Sekera31da2e32018-06-24 22:49:55 +0200143
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200144# glue the BFD packet class to scapy parser
145bind_layers(UDP, BFD, dport=BFD.udp_dport)
146
147
Klement Sekera239790f2017-02-16 10:53:53 +0100148class BFD_vpp_echo(Packet):
149 """ BFD echo packet as used by VPP (non-rfc, as rfc doesn't define one) """
150
151 udp_dport = 3785 #: BFD echo destination port per RFC 5881
152 name = "BFD_VPP_ECHO"
153
154 fields_desc = [
155 BitField("discriminator", 0, 32),
156 BitField("expire_time_clocks", 0, 64),
157 BitField("checksum", 0, 64)
158 ]
159
160 def mysummary(self):
161 return self.sprintf(
162 "BFD_VPP_ECHO(disc=%BFD_VPP_ECHO.discriminator%,"
163 "expire_time_clocks=%BFD_VPP_ECHO.expire_time_clocks%)")
164
Klement Sekera31da2e32018-06-24 22:49:55 +0200165
Klement Sekera239790f2017-02-16 10:53:53 +0100166# glue the BFD echo packet class to scapy parser
167bind_layers(UDP, BFD_vpp_echo, dport=BFD_vpp_echo.udp_dport)
168
169
Klement Sekerab17dd962017-01-09 07:43:48 +0100170class VppBFDAuthKey(VppObject):
171 """ Represents BFD authentication key in VPP """
172
173 def __init__(self, test, conf_key_id, auth_type, key):
174 self._test = test
175 self._key = key
176 self._auth_type = auth_type
177 test.assertIn(auth_type, BFDAuthType.desc_dict)
178 self._conf_key_id = conf_key_id
179
180 @property
181 def test(self):
182 """ Test which created this key """
183 return self._test
184
185 @property
186 def auth_type(self):
187 """ Authentication type for this key """
188 return self._auth_type
189
190 @property
191 def key(self):
Klement Sekerad3ba5152017-02-14 03:09:17 +0100192 """ key data """
Klement Sekerab17dd962017-01-09 07:43:48 +0100193 return self._key
194
Klement Sekera73884482017-02-23 09:26:30 +0100195 @key.setter
196 def key(self, value):
197 self._key = value
198
Klement Sekerab17dd962017-01-09 07:43:48 +0100199 @property
200 def conf_key_id(self):
Klement Sekerad3ba5152017-02-14 03:09:17 +0100201 """ configuration key ID """
Klement Sekerab17dd962017-01-09 07:43:48 +0100202 return self._conf_key_id
203
204 def add_vpp_config(self):
205 self.test.vapi.bfd_auth_set_key(
Jakub Grajciar4682feb2019-09-02 13:28:52 +0200206 conf_key_id=self._conf_key_id, auth_type=self._auth_type,
207 key=self._key, key_len=len(self._key))
Klement Sekerab17dd962017-01-09 07:43:48 +0100208 self._test.registry.register(self, self.test.logger)
209
210 def get_bfd_auth_keys_dump_entry(self):
211 """ get the entry in the auth keys dump corresponding to this key """
212 result = self.test.vapi.bfd_auth_keys_dump()
213 for k in result:
214 if k.conf_key_id == self._conf_key_id:
215 return k
216 return None
217
218 def query_vpp_config(self):
219 return self.get_bfd_auth_keys_dump_entry() is not None
220
221 def remove_vpp_config(self):
Jakub Grajciar4682feb2019-09-02 13:28:52 +0200222 self.test.vapi.bfd_auth_del_key(conf_key_id=self._conf_key_id)
Klement Sekerab17dd962017-01-09 07:43:48 +0100223
224 def object_id(self):
225 return "bfd-auth-key-%s" % self._conf_key_id
226
Klement Sekerab17dd962017-01-09 07:43:48 +0100227
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200228class VppBFDUDPSession(VppObject):
229 """ Represents BFD UDP session in VPP """
230
Klement Sekerab17dd962017-01-09 07:43:48 +0100231 def __init__(self, test, interface, peer_addr, local_addr=None, af=AF_INET,
Klement Sekera2e01dfa2017-04-03 08:10:08 +0200232 desired_min_tx=300000, required_min_rx=300000, detect_mult=3,
Neale Ranns52cd4962019-06-05 10:28:17 +0000233 sha1_key=None, bfd_key_id=None, is_tunnel=False):
Klement Sekerab17dd962017-01-09 07:43:48 +0100234 self._test = test
235 self._interface = interface
236 self._af = af
Jakub Grajciar4682feb2019-09-02 13:28:52 +0200237 if local_addr:
Neale Rannsefd7bc22019-11-11 08:32:34 +0000238 self._local_addr = local_addr
Klement Sekerad3ba5152017-02-14 03:09:17 +0100239 else:
Jakub Grajciar4682feb2019-09-02 13:28:52 +0200240 self._local_addr = None
Neale Rannsefd7bc22019-11-11 08:32:34 +0000241 self._peer_addr = peer_addr
Klement Sekerab17dd962017-01-09 07:43:48 +0100242 self._desired_min_tx = desired_min_tx
243 self._required_min_rx = required_min_rx
244 self._detect_mult = detect_mult
245 self._sha1_key = sha1_key
Klement Sekera73884482017-02-23 09:26:30 +0100246 if bfd_key_id is not None:
247 self._bfd_key_id = bfd_key_id
248 else:
249 self._bfd_key_id = randint(0, 255)
Neale Ranns52cd4962019-06-05 10:28:17 +0000250 self._is_tunnel = is_tunnel
Klement Sekerab17dd962017-01-09 07:43:48 +0100251
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200252 @property
253 def test(self):
254 """ Test which created this session """
255 return self._test
256
257 @property
258 def interface(self):
259 """ Interface on which this session lives """
260 return self._interface
261
262 @property
263 def af(self):
264 """ Address family - AF_INET or AF_INET6 """
265 return self._af
266
267 @property
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200268 def local_addr(self):
269 """ BFD session local address (VPP address) """
270 if self._local_addr is None:
Klement Sekera46a87ad2017-01-02 08:22:23 +0100271 if self.af == AF_INET:
272 return self._interface.local_ip4
273 elif self.af == AF_INET6:
274 return self._interface.local_ip6
275 else:
Klement Sekerad3ba5152017-02-14 03:09:17 +0100276 raise Exception("Unexpected af '%s'" % self.af)
Neale Rannsefd7bc22019-11-11 08:32:34 +0000277 return self._local_addr
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200278
279 @property
280 def peer_addr(self):
281 """ BFD session peer address """
Neale Rannsefd7bc22019-11-11 08:32:34 +0000282 return self._peer_addr
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200283
Klement Sekerab17dd962017-01-09 07:43:48 +0100284 def get_bfd_udp_session_dump_entry(self):
Klement Sekerad3ba5152017-02-14 03:09:17 +0100285 """ get the namedtuple entry from bfd udp session dump """
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200286 result = self.test.vapi.bfd_udp_session_dump()
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200287 for s in result:
Klement Sekerab17dd962017-01-09 07:43:48 +0100288 self.test.logger.debug("session entry: %s" % str(s))
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200289 if s.sw_if_index == self.interface.sw_if_index:
290 if self.af == AF_INET \
Jakub Grajciar4682feb2019-09-02 13:28:52 +0200291 and self.interface.local_ip4 == str(s.local_addr) \
292 and self.interface.remote_ip4 == str(s.peer_addr):
Klement Sekerab17dd962017-01-09 07:43:48 +0100293 return s
294 if self.af == AF_INET6 \
Jakub Grajciar4682feb2019-09-02 13:28:52 +0200295 and self.interface.local_ip6 == str(s.local_addr) \
296 and self.interface.remote_ip6 == str(s.peer_addr):
Klement Sekerab17dd962017-01-09 07:43:48 +0100297 return s
298 return None
299
300 @property
301 def state(self):
302 """ BFD session state """
303 session = self.get_bfd_udp_session_dump_entry()
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200304 if session is None:
Klement Sekerad3ba5152017-02-14 03:09:17 +0100305 raise Exception("Could not find BFD session in VPP response")
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200306 return session.state
307
308 @property
309 def desired_min_tx(self):
Klement Sekerad3ba5152017-02-14 03:09:17 +0100310 """ desired minimum tx interval """
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200311 return self._desired_min_tx
312
313 @property
314 def required_min_rx(self):
Klement Sekerad3ba5152017-02-14 03:09:17 +0100315 """ required minimum rx interval """
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200316 return self._required_min_rx
317
318 @property
319 def detect_mult(self):
Klement Sekerad3ba5152017-02-14 03:09:17 +0100320 """ detect multiplier """
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200321 return self._detect_mult
322
Klement Sekerab17dd962017-01-09 07:43:48 +0100323 @property
324 def sha1_key(self):
Klement Sekerad3ba5152017-02-14 03:09:17 +0100325 """ sha1 key """
Klement Sekerab17dd962017-01-09 07:43:48 +0100326 return self._sha1_key
327
328 @property
329 def bfd_key_id(self):
Klement Sekerad3ba5152017-02-14 03:09:17 +0100330 """ bfd key id in use """
Klement Sekerab17dd962017-01-09 07:43:48 +0100331 return self._bfd_key_id
332
Neale Ranns52cd4962019-06-05 10:28:17 +0000333 @property
334 def is_tunnel(self):
335 return self._is_tunnel
336
Klement Sekerab17dd962017-01-09 07:43:48 +0100337 def activate_auth(self, key, bfd_key_id=None, delayed=False):
Klement Sekerad3ba5152017-02-14 03:09:17 +0100338 """ activate authentication for this session """
Klement Sekerab17dd962017-01-09 07:43:48 +0100339 self._bfd_key_id = bfd_key_id if bfd_key_id else randint(0, 255)
340 self._sha1_key = key
Klement Sekerab17dd962017-01-09 07:43:48 +0100341 conf_key_id = self._sha1_key.conf_key_id
342 is_delayed = 1 if delayed else 0
Jakub Grajciar4682feb2019-09-02 13:28:52 +0200343 self.test.vapi.bfd_udp_auth_activate(
344 sw_if_index=self._interface.sw_if_index,
Neale Rannsefd7bc22019-11-11 08:32:34 +0000345 local_addr=self.local_addr,
346 peer_addr=self.peer_addr,
Jakub Grajciar4682feb2019-09-02 13:28:52 +0200347 bfd_key_id=self._bfd_key_id,
348 conf_key_id=conf_key_id,
349 is_delayed=is_delayed)
Klement Sekerab17dd962017-01-09 07:43:48 +0100350
351 def deactivate_auth(self, delayed=False):
Klement Sekerad3ba5152017-02-14 03:09:17 +0100352 """ deactivate authentication """
Klement Sekerab17dd962017-01-09 07:43:48 +0100353 self._bfd_key_id = None
354 self._sha1_key = None
355 is_delayed = 1 if delayed else 0
Jakub Grajciar4682feb2019-09-02 13:28:52 +0200356 self.test.vapi.bfd_udp_auth_deactivate(
357 sw_if_index=self._interface.sw_if_index,
Neale Rannsefd7bc22019-11-11 08:32:34 +0000358 local_addr=self.local_addr,
359 peer_addr=self.peer_addr,
Jakub Grajciar4682feb2019-09-02 13:28:52 +0200360 is_delayed=is_delayed)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200361
Klement Sekeraa57a9702017-02-02 06:58:07 +0100362 def modify_parameters(self,
363 detect_mult=None,
364 desired_min_tx=None,
365 required_min_rx=None):
Klement Sekerad3ba5152017-02-14 03:09:17 +0100366 """ modify session parameters """
Klement Sekeraa57a9702017-02-02 06:58:07 +0100367 if detect_mult:
368 self._detect_mult = detect_mult
369 if desired_min_tx:
370 self._desired_min_tx = desired_min_tx
371 if required_min_rx:
372 self._required_min_rx = required_min_rx
Jakub Grajciar4682feb2019-09-02 13:28:52 +0200373 self.test.vapi.bfd_udp_mod(sw_if_index=self._interface.sw_if_index,
374 desired_min_tx=self.desired_min_tx,
375 required_min_rx=self.required_min_rx,
376 detect_mult=self.detect_mult,
Neale Rannsefd7bc22019-11-11 08:32:34 +0000377 local_addr=self.local_addr,
378 peer_addr=self.peer_addr)
Klement Sekeraa57a9702017-02-02 06:58:07 +0100379
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200380 def add_vpp_config(self):
Klement Sekerab17dd962017-01-09 07:43:48 +0100381 bfd_key_id = self._bfd_key_id if self._sha1_key else None
382 conf_key_id = self._sha1_key.conf_key_id if self._sha1_key else None
Jakub Grajciar4682feb2019-09-02 13:28:52 +0200383 is_authenticated = True if self._sha1_key else False
384 self.test.vapi.bfd_udp_add(sw_if_index=self._interface.sw_if_index,
385 desired_min_tx=self.desired_min_tx,
386 required_min_rx=self.required_min_rx,
387 detect_mult=self.detect_mult,
Neale Rannsefd7bc22019-11-11 08:32:34 +0000388 local_addr=self.local_addr,
389 peer_addr=self.peer_addr,
Klement Sekerab17dd962017-01-09 07:43:48 +0100390 bfd_key_id=bfd_key_id,
Jakub Grajciar4682feb2019-09-02 13:28:52 +0200391 conf_key_id=conf_key_id,
392 is_authenticated=is_authenticated)
Klement Sekera10db26f2017-01-11 08:16:53 +0100393 self._test.registry.register(self, self.test.logger)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200394
395 def query_vpp_config(self):
Klement Sekerab17dd962017-01-09 07:43:48 +0100396 session = self.get_bfd_udp_session_dump_entry()
397 return session is not None
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200398
399 def remove_vpp_config(self):
Klement Sekerab17dd962017-01-09 07:43:48 +0100400 self.test.vapi.bfd_udp_del(self._interface.sw_if_index,
Neale Rannsefd7bc22019-11-11 08:32:34 +0000401 local_addr=self.local_addr,
402 peer_addr=self.peer_addr)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200403
404 def object_id(self):
Klement Sekerab17dd962017-01-09 07:43:48 +0100405 return "bfd-udp-%s-%s-%s-%s" % (self._interface.sw_if_index,
406 self.local_addr,
407 self.peer_addr,
408 self.af)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200409
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200410 def admin_up(self):
Klement Sekerad3ba5152017-02-14 03:09:17 +0100411 """ set bfd session admin-up """
Jakub Grajciar4682feb2019-09-02 13:28:52 +0200412 self.test.vapi.bfd_udp_session_set_flags(
413 flags=VppEnum.vl_api_if_status_flags_t.IF_STATUS_API_FLAG_ADMIN_UP,
414 sw_if_index=self._interface.sw_if_index,
Neale Rannsefd7bc22019-11-11 08:32:34 +0000415 local_addr=self.local_addr,
416 peer_addr=self.peer_addr)
Klement Sekerac48829b2017-02-14 07:55:57 +0100417
418 def admin_down(self):
419 """ set bfd session admin-down """
Jakub Grajciar4682feb2019-09-02 13:28:52 +0200420 self.test.vapi.bfd_udp_session_set_flags(
421 flags=0, sw_if_index=self._interface.sw_if_index,
Neale Rannsefd7bc22019-11-11 08:32:34 +0000422 local_addr=self.local_addr,
423 peer_addr=self.peer_addr)