blob: d99bbf6165c3de7fe5811e708f23c6cdb878a850 [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
12
13
14class BFDDiagCode(NumericConstant):
15 """ BFD Diagnostic Code """
16 no_diagnostic = 0
17 control_detection_time_expired = 1
18 echo_function_failed = 2
19 neighbor_signaled_session_down = 3
20 forwarding_plane_reset = 4
21 path_down = 5
22 concatenated_path_down = 6
23 administratively_down = 7
24 reverse_concatenated_path_down = 8
25
26 desc_dict = {
27 no_diagnostic: "No diagnostic",
28 control_detection_time_expired: "Control Detection Time Expired",
29 echo_function_failed: "Echo Function Failed",
30 neighbor_signaled_session_down: "Neighbor Signaled Session Down",
31 forwarding_plane_reset: "Forwarding Plane Reset",
32 path_down: "Path Down",
33 concatenated_path_down: "Concatenated Path Down",
34 administratively_down: "Administratively Down",
35 reverse_concatenated_path_down: "Reverse Concatenated Path Down",
36 }
37
Klement Sekera0e3c0de2016-09-29 14:43:44 +020038
39class BFDState(NumericConstant):
40 """ BFD State """
41 admin_down = 0
42 down = 1
43 init = 2
44 up = 3
45
46 desc_dict = {
47 admin_down: "AdminDown",
48 down: "Down",
49 init: "Init",
50 up: "Up",
51 }
52
Klement Sekera0e3c0de2016-09-29 14:43:44 +020053
Klement Sekerab17dd962017-01-09 07:43:48 +010054class BFDAuthType(NumericConstant):
55 """ BFD Authentication Type """
56 no_auth = 0
57 simple_pwd = 1
58 keyed_md5 = 2
59 meticulous_keyed_md5 = 3
60 keyed_sha1 = 4
61 meticulous_keyed_sha1 = 5
62
63 desc_dict = {
64 no_auth: "No authentication",
65 simple_pwd: "Simple Password",
66 keyed_md5: "Keyed MD5",
67 meticulous_keyed_md5: "Meticulous Keyed MD5",
68 keyed_sha1: "Keyed SHA1",
69 meticulous_keyed_sha1: "Meticulous Keyed SHA1",
70 }
71
Klement Sekerab17dd962017-01-09 07:43:48 +010072
73def bfd_is_auth_used(pkt):
Klement Sekerad3ba5152017-02-14 03:09:17 +010074 """ is packet authenticated? """
Klement Sekerab17dd962017-01-09 07:43:48 +010075 return "A" in pkt.sprintf("%BFD.flags%")
76
77
78def bfd_is_simple_pwd_used(pkt):
Klement Sekerad3ba5152017-02-14 03:09:17 +010079 """ is simple password authentication used? """
Klement Sekerab17dd962017-01-09 07:43:48 +010080 return bfd_is_auth_used(pkt) and pkt.auth_type == BFDAuthType.simple_pwd
81
82
83def bfd_is_sha1_used(pkt):
Klement Sekerad3ba5152017-02-14 03:09:17 +010084 """ is sha1 authentication used? """
Klement Sekerab17dd962017-01-09 07:43:48 +010085 return bfd_is_auth_used(pkt) and pkt.auth_type in \
86 (BFDAuthType.keyed_sha1, BFDAuthType.meticulous_keyed_sha1)
87
88
89def bfd_is_md5_used(pkt):
Klement Sekerad3ba5152017-02-14 03:09:17 +010090 """ is md5 authentication used? """
Klement Sekerab17dd962017-01-09 07:43:48 +010091 return bfd_is_auth_used(pkt) and pkt.auth_type in \
92 (BFDAuthType.keyed_md5, BFDAuthType.meticulous_keyed_md5)
93
94
95def bfd_is_md5_or_sha1_used(pkt):
Klement Sekerad3ba5152017-02-14 03:09:17 +010096 """ is md5 or sha1 used? """
Klement Sekerab17dd962017-01-09 07:43:48 +010097 return bfd_is_md5_used(pkt) or bfd_is_sha1_used(pkt)
98
99
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200100class BFD(Packet):
Klement Sekerad3ba5152017-02-14 03:09:17 +0100101 """ BFD protocol layer for scapy """
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200102
103 udp_dport = 3784 #: BFD destination port per RFC 5881
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100104 udp_dport_echo = 3785 # : BFD destination port for ECHO per RFC 5881
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200105 udp_sport_min = 49152 #: BFD source port min value per RFC 5881
106 udp_sport_max = 65535 #: BFD source port max value per RFC 5881
Klement Sekerab17dd962017-01-09 07:43:48 +0100107 bfd_pkt_len = 24 # : length of BFD pkt without authentication section
108 sha1_auth_len = 28 # : length of authentication section if SHA1 used
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200109
110 name = "BFD"
111
112 fields_desc = [
113 BitField("version", 1, 3),
114 BitEnumField("diag", 0, 5, BFDDiagCode.desc_dict),
115 BitEnumField("state", 0, 2, BFDState.desc_dict),
Klement Sekerab17dd962017-01-09 07:43:48 +0100116 FlagsField("flags", 0, 6, ['M', 'D', 'A', 'C', 'F', 'P']),
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200117 XByteField("detect_mult", 0),
Klement Sekerab17dd962017-01-09 07:43:48 +0100118 BitField("length", bfd_pkt_len, 8),
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200119 BitField("my_discriminator", 0, 32),
120 BitField("your_discriminator", 0, 32),
121 BitField("desired_min_tx_interval", 0, 32),
122 BitField("required_min_rx_interval", 0, 32),
Klement Sekerab17dd962017-01-09 07:43:48 +0100123 BitField("required_min_echo_rx_interval", 0, 32),
124 ConditionalField(
125 BitEnumField("auth_type", 0, 8, BFDAuthType.desc_dict),
126 bfd_is_auth_used),
127 ConditionalField(BitField("auth_len", 0, 8), bfd_is_auth_used),
128 ConditionalField(BitField("auth_key_id", 0, 8), bfd_is_auth_used),
129 ConditionalField(BitField("auth_reserved", 0, 8),
130 bfd_is_md5_or_sha1_used),
131 ConditionalField(
132 BitField("auth_seq_num", 0, 32), bfd_is_md5_or_sha1_used),
133 ConditionalField(StrField("auth_key_hash", "0" * 16), bfd_is_md5_used),
134 ConditionalField(
135 StrField("auth_key_hash", "0" * 20), bfd_is_sha1_used),
136 ]
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200137
138 def mysummary(self):
139 return self.sprintf("BFD(my_disc=%BFD.my_discriminator%,"
140 "your_disc=%BFD.your_discriminator%)")
141
Klement Sekeraa98346f2018-05-16 10:52:45 +0200142
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200143# glue the BFD packet class to scapy parser
144bind_layers(UDP, BFD, dport=BFD.udp_dport)
145
146
Klement Sekera239790f2017-02-16 10:53:53 +0100147class BFD_vpp_echo(Packet):
148 """ BFD echo packet as used by VPP (non-rfc, as rfc doesn't define one) """
149
150 udp_dport = 3785 #: BFD echo destination port per RFC 5881
151 name = "BFD_VPP_ECHO"
152
153 fields_desc = [
154 BitField("discriminator", 0, 32),
155 BitField("expire_time_clocks", 0, 64),
156 BitField("checksum", 0, 64)
157 ]
158
159 def mysummary(self):
160 return self.sprintf(
161 "BFD_VPP_ECHO(disc=%BFD_VPP_ECHO.discriminator%,"
162 "expire_time_clocks=%BFD_VPP_ECHO.expire_time_clocks%)")
163
Klement Sekeraa98346f2018-05-16 10:52:45 +0200164
Klement Sekera239790f2017-02-16 10:53:53 +0100165# glue the BFD echo packet class to scapy parser
166bind_layers(UDP, BFD_vpp_echo, dport=BFD_vpp_echo.udp_dport)
167
168
Klement Sekerab17dd962017-01-09 07:43:48 +0100169class VppBFDAuthKey(VppObject):
170 """ Represents BFD authentication key in VPP """
171
172 def __init__(self, test, conf_key_id, auth_type, key):
173 self._test = test
174 self._key = key
175 self._auth_type = auth_type
176 test.assertIn(auth_type, BFDAuthType.desc_dict)
177 self._conf_key_id = conf_key_id
178
179 @property
180 def test(self):
181 """ Test which created this key """
182 return self._test
183
184 @property
185 def auth_type(self):
186 """ Authentication type for this key """
187 return self._auth_type
188
189 @property
190 def key(self):
Klement Sekerad3ba5152017-02-14 03:09:17 +0100191 """ key data """
Klement Sekerab17dd962017-01-09 07:43:48 +0100192 return self._key
193
Klement Sekera73884482017-02-23 09:26:30 +0100194 @key.setter
195 def key(self, value):
196 self._key = value
197
Klement Sekerab17dd962017-01-09 07:43:48 +0100198 @property
199 def conf_key_id(self):
Klement Sekerad3ba5152017-02-14 03:09:17 +0100200 """ configuration key ID """
Klement Sekerab17dd962017-01-09 07:43:48 +0100201 return self._conf_key_id
202
203 def add_vpp_config(self):
204 self.test.vapi.bfd_auth_set_key(
205 self._conf_key_id, self._auth_type, self._key)
206 self._test.registry.register(self, self.test.logger)
207
208 def get_bfd_auth_keys_dump_entry(self):
209 """ get the entry in the auth keys dump corresponding to this key """
210 result = self.test.vapi.bfd_auth_keys_dump()
211 for k in result:
212 if k.conf_key_id == self._conf_key_id:
213 return k
214 return None
215
216 def query_vpp_config(self):
217 return self.get_bfd_auth_keys_dump_entry() is not None
218
219 def remove_vpp_config(self):
220 self.test.vapi.bfd_auth_del_key(self._conf_key_id)
221
222 def object_id(self):
223 return "bfd-auth-key-%s" % self._conf_key_id
224
225 def __str__(self):
226 return self.object_id()
227
228
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200229class VppBFDUDPSession(VppObject):
230 """ Represents BFD UDP session in VPP """
231
Klement Sekerab17dd962017-01-09 07:43:48 +0100232 def __init__(self, test, interface, peer_addr, local_addr=None, af=AF_INET,
Klement Sekera2e01dfa2017-04-03 08:10:08 +0200233 desired_min_tx=300000, required_min_rx=300000, detect_mult=3,
Klement Sekerab17dd962017-01-09 07:43:48 +0100234 sha1_key=None, bfd_key_id=None):
235 self._test = test
236 self._interface = interface
237 self._af = af
238 self._local_addr = local_addr
Klement Sekerad3ba5152017-02-14 03:09:17 +0100239 if local_addr is not None:
240 self._local_addr_n = inet_pton(af, local_addr)
241 else:
242 self._local_addr_n = None
Klement Sekerab17dd962017-01-09 07:43:48 +0100243 self._peer_addr = peer_addr
Klement Sekerad3ba5152017-02-14 03:09:17 +0100244 self._peer_addr_n = inet_pton(af, peer_addr)
Klement Sekerab17dd962017-01-09 07:43:48 +0100245 self._desired_min_tx = desired_min_tx
246 self._required_min_rx = required_min_rx
247 self._detect_mult = detect_mult
248 self._sha1_key = sha1_key
Klement Sekera73884482017-02-23 09:26:30 +0100249 if bfd_key_id is not None:
250 self._bfd_key_id = bfd_key_id
251 else:
252 self._bfd_key_id = randint(0, 255)
Klement Sekerab17dd962017-01-09 07:43:48 +0100253
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200254 @property
255 def test(self):
256 """ Test which created this session """
257 return self._test
258
259 @property
260 def interface(self):
261 """ Interface on which this session lives """
262 return self._interface
263
264 @property
265 def af(self):
266 """ Address family - AF_INET or AF_INET6 """
267 return self._af
268
269 @property
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200270 def local_addr(self):
271 """ BFD session local address (VPP address) """
272 if self._local_addr is None:
Klement Sekera46a87ad2017-01-02 08:22:23 +0100273 if self.af == AF_INET:
274 return self._interface.local_ip4
275 elif self.af == AF_INET6:
276 return self._interface.local_ip6
277 else:
Klement Sekerad3ba5152017-02-14 03:09:17 +0100278 raise Exception("Unexpected af '%s'" % self.af)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200279 return self._local_addr
280
281 @property
282 def local_addr_n(self):
283 """ BFD session local address (VPP address) - raw, suitable for API """
284 if self._local_addr is None:
Klement Sekera46a87ad2017-01-02 08:22:23 +0100285 if self.af == AF_INET:
286 return self._interface.local_ip4n
287 elif self.af == AF_INET6:
288 return self._interface.local_ip6n
289 else:
Klement Sekerad3ba5152017-02-14 03:09:17 +0100290 raise Exception("Unexpected af '%s'" % self.af)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200291 return self._local_addr_n
292
293 @property
294 def peer_addr(self):
295 """ BFD session peer address """
296 return self._peer_addr
297
298 @property
299 def peer_addr_n(self):
300 """ BFD session peer address - raw, suitable for API """
301 return self._peer_addr_n
302
Klement Sekerab17dd962017-01-09 07:43:48 +0100303 def get_bfd_udp_session_dump_entry(self):
Klement Sekerad3ba5152017-02-14 03:09:17 +0100304 """ get the namedtuple entry from bfd udp session dump """
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200305 result = self.test.vapi.bfd_udp_session_dump()
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200306 for s in result:
Klement Sekerab17dd962017-01-09 07:43:48 +0100307 self.test.logger.debug("session entry: %s" % str(s))
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200308 if s.sw_if_index == self.interface.sw_if_index:
309 if self.af == AF_INET \
310 and s.is_ipv6 == 0 \
311 and self.interface.local_ip4n == s.local_addr[:4] \
312 and self.interface.remote_ip4n == s.peer_addr[:4]:
Klement Sekerab17dd962017-01-09 07:43:48 +0100313 return s
314 if self.af == AF_INET6 \
315 and s.is_ipv6 == 1 \
316 and self.interface.local_ip6n == s.local_addr \
317 and self.interface.remote_ip6n == s.peer_addr:
318 return s
319 return None
320
321 @property
322 def state(self):
323 """ BFD session state """
324 session = self.get_bfd_udp_session_dump_entry()
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200325 if session is None:
Klement Sekerad3ba5152017-02-14 03:09:17 +0100326 raise Exception("Could not find BFD session in VPP response")
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200327 return session.state
328
329 @property
330 def desired_min_tx(self):
Klement Sekerad3ba5152017-02-14 03:09:17 +0100331 """ desired minimum tx interval """
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200332 return self._desired_min_tx
333
334 @property
335 def required_min_rx(self):
Klement Sekerad3ba5152017-02-14 03:09:17 +0100336 """ required minimum rx interval """
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200337 return self._required_min_rx
338
339 @property
340 def detect_mult(self):
Klement Sekerad3ba5152017-02-14 03:09:17 +0100341 """ detect multiplier """
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200342 return self._detect_mult
343
Klement Sekerab17dd962017-01-09 07:43:48 +0100344 @property
345 def sha1_key(self):
Klement Sekerad3ba5152017-02-14 03:09:17 +0100346 """ sha1 key """
Klement Sekerab17dd962017-01-09 07:43:48 +0100347 return self._sha1_key
348
349 @property
350 def bfd_key_id(self):
Klement Sekerad3ba5152017-02-14 03:09:17 +0100351 """ bfd key id in use """
Klement Sekerab17dd962017-01-09 07:43:48 +0100352 return self._bfd_key_id
353
354 def activate_auth(self, key, bfd_key_id=None, delayed=False):
Klement Sekerad3ba5152017-02-14 03:09:17 +0100355 """ activate authentication for this session """
Klement Sekerab17dd962017-01-09 07:43:48 +0100356 self._bfd_key_id = bfd_key_id if bfd_key_id else randint(0, 255)
357 self._sha1_key = key
358 is_ipv6 = 1 if AF_INET6 == self.af else 0
359 conf_key_id = self._sha1_key.conf_key_id
360 is_delayed = 1 if delayed else 0
361 self.test.vapi.bfd_udp_auth_activate(self._interface.sw_if_index,
362 self.local_addr_n,
363 self.peer_addr_n,
364 is_ipv6=is_ipv6,
365 bfd_key_id=self._bfd_key_id,
366 conf_key_id=conf_key_id,
367 is_delayed=is_delayed)
368
369 def deactivate_auth(self, delayed=False):
Klement Sekerad3ba5152017-02-14 03:09:17 +0100370 """ deactivate authentication """
Klement Sekerab17dd962017-01-09 07:43:48 +0100371 self._bfd_key_id = None
372 self._sha1_key = None
373 is_delayed = 1 if delayed else 0
374 is_ipv6 = 1 if AF_INET6 == self.af else 0
375 self.test.vapi.bfd_udp_auth_deactivate(self._interface.sw_if_index,
376 self.local_addr_n,
377 self.peer_addr_n,
378 is_ipv6=is_ipv6,
379 is_delayed=is_delayed)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200380
Klement Sekeraa57a9702017-02-02 06:58:07 +0100381 def modify_parameters(self,
382 detect_mult=None,
383 desired_min_tx=None,
384 required_min_rx=None):
Klement Sekerad3ba5152017-02-14 03:09:17 +0100385 """ modify session parameters """
Klement Sekeraa57a9702017-02-02 06:58:07 +0100386 if detect_mult:
387 self._detect_mult = detect_mult
388 if desired_min_tx:
389 self._desired_min_tx = desired_min_tx
390 if required_min_rx:
391 self._required_min_rx = required_min_rx
392 is_ipv6 = 1 if AF_INET6 == self.af else 0
393 self.test.vapi.bfd_udp_mod(self._interface.sw_if_index,
394 self.desired_min_tx,
395 self.required_min_rx,
396 self.detect_mult,
397 self.local_addr_n,
398 self.peer_addr_n,
399 is_ipv6=is_ipv6)
400
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200401 def add_vpp_config(self):
402 is_ipv6 = 1 if AF_INET6 == self.af else 0
Klement Sekerab17dd962017-01-09 07:43:48 +0100403 bfd_key_id = self._bfd_key_id if self._sha1_key else None
404 conf_key_id = self._sha1_key.conf_key_id if self._sha1_key else None
405 self.test.vapi.bfd_udp_add(self._interface.sw_if_index,
406 self.desired_min_tx,
407 self.required_min_rx,
408 self.detect_mult,
409 self.local_addr_n,
410 self.peer_addr_n,
411 is_ipv6=is_ipv6,
412 bfd_key_id=bfd_key_id,
413 conf_key_id=conf_key_id)
Klement Sekera10db26f2017-01-11 08:16:53 +0100414 self._test.registry.register(self, self.test.logger)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200415
416 def query_vpp_config(self):
Klement Sekerab17dd962017-01-09 07:43:48 +0100417 session = self.get_bfd_udp_session_dump_entry()
418 return session is not None
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200419
420 def remove_vpp_config(self):
Klement Sekerab17dd962017-01-09 07:43:48 +0100421 is_ipv6 = 1 if AF_INET6 == self._af else 0
422 self.test.vapi.bfd_udp_del(self._interface.sw_if_index,
423 self.local_addr_n,
424 self.peer_addr_n,
425 is_ipv6=is_ipv6)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200426
427 def object_id(self):
Klement Sekerab17dd962017-01-09 07:43:48 +0100428 return "bfd-udp-%s-%s-%s-%s" % (self._interface.sw_if_index,
429 self.local_addr,
430 self.peer_addr,
431 self.af)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200432
Klement Sekera10db26f2017-01-11 08:16:53 +0100433 def __str__(self):
434 return self.object_id()
435
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200436 def admin_up(self):
Klement Sekerad3ba5152017-02-14 03:09:17 +0100437 """ set bfd session admin-up """
Klement Sekerab17dd962017-01-09 07:43:48 +0100438 is_ipv6 = 1 if AF_INET6 == self._af else 0
439 self.test.vapi.bfd_udp_session_set_flags(1,
440 self._interface.sw_if_index,
441 self.local_addr_n,
442 self.peer_addr_n,
443 is_ipv6=is_ipv6)
Klement Sekerac48829b2017-02-14 07:55:57 +0100444
445 def admin_down(self):
446 """ set bfd session admin-down """
447 is_ipv6 = 1 if AF_INET6 == self._af else 0
448 self.test.vapi.bfd_udp_session_set_flags(0,
449 self._interface.sw_if_index,
450 self.local_addr_n,
451 self.peer_addr_n,
452 is_ipv6=is_ipv6)