1 """ BFD protocol implementation """
3 from random import randint
4 from socket import AF_INET, AF_INET6, inet_pton
5 from scapy.all import bind_layers
6 from scapy.layers.inet import UDP
7 from scapy.packet import Packet
8 from scapy.fields import BitField, BitEnumField, XByteField, FlagsField,\
9 ConditionalField, StrField
10 from vpp_object import VppObject
11 from util import NumericConstant
14 class BFDDiagCode(NumericConstant):
15 """ BFD Diagnostic Code """
17 control_detection_time_expired = 1
18 echo_function_failed = 2
19 neighbor_signaled_session_down = 3
20 forwarding_plane_reset = 4
22 concatenated_path_down = 6
23 administratively_down = 7
24 reverse_concatenated_path_down = 8
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",
39 class BFDState(NumericConstant):
47 admin_down: "AdminDown",
54 class BFDAuthType(NumericConstant):
55 """ BFD Authentication Type """
59 meticulous_keyed_md5 = 3
61 meticulous_keyed_sha1 = 5
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",
73 def bfd_is_auth_used(pkt):
74 """ is packet authenticated? """
75 return "A" in pkt.sprintf("%BFD.flags%")
78 def bfd_is_simple_pwd_used(pkt):
79 """ is simple password authentication used? """
80 return bfd_is_auth_used(pkt) and pkt.auth_type == BFDAuthType.simple_pwd
83 def bfd_is_sha1_used(pkt):
84 """ is sha1 authentication used? """
85 return bfd_is_auth_used(pkt) and pkt.auth_type in \
86 (BFDAuthType.keyed_sha1, BFDAuthType.meticulous_keyed_sha1)
89 def bfd_is_md5_used(pkt):
90 """ is md5 authentication used? """
91 return bfd_is_auth_used(pkt) and pkt.auth_type in \
92 (BFDAuthType.keyed_md5, BFDAuthType.meticulous_keyed_md5)
95 def bfd_is_md5_or_sha1_used(pkt):
96 """ is md5 or sha1 used? """
97 return bfd_is_md5_used(pkt) or bfd_is_sha1_used(pkt)
101 """ BFD protocol layer for scapy """
103 udp_dport = 3784 #: BFD destination port per RFC 5881
104 udp_dport_echo = 3785 # : BFD destination port for ECHO per RFC 5881
105 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
107 bfd_pkt_len = 24 # : length of BFD pkt without authentication section
108 sha1_auth_len = 28 # : length of authentication section if SHA1 used
113 BitField("version", 1, 3),
114 BitEnumField("diag", 0, 5, BFDDiagCode.desc_dict),
115 BitEnumField("state", 0, 2, BFDState.desc_dict),
116 FlagsField("flags", 0, 6, ['M', 'D', 'A', 'C', 'F', 'P']),
117 XByteField("detect_mult", 0),
118 BitField("length", bfd_pkt_len, 8),
119 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),
123 BitField("required_min_echo_rx_interval", 0, 32),
125 BitEnumField("auth_type", 0, 8, BFDAuthType.desc_dict),
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),
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),
135 StrField("auth_key_hash", "0" * 20), bfd_is_sha1_used),
139 return self.sprintf("BFD(my_disc=%BFD.my_discriminator%,"
140 "your_disc=%BFD.your_discriminator%)")
143 # glue the BFD packet class to scapy parser
144 bind_layers(UDP, BFD, dport=BFD.udp_dport)
147 class BFD_vpp_echo(Packet):
148 """ BFD echo packet as used by VPP (non-rfc, as rfc doesn't define one) """
150 udp_dport = 3785 #: BFD echo destination port per RFC 5881
151 name = "BFD_VPP_ECHO"
154 BitField("discriminator", 0, 32),
155 BitField("expire_time_clocks", 0, 64),
156 BitField("checksum", 0, 64)
161 "BFD_VPP_ECHO(disc=%BFD_VPP_ECHO.discriminator%,"
162 "expire_time_clocks=%BFD_VPP_ECHO.expire_time_clocks%)")
165 # glue the BFD echo packet class to scapy parser
166 bind_layers(UDP, BFD_vpp_echo, dport=BFD_vpp_echo.udp_dport)
169 class VppBFDAuthKey(VppObject):
170 """ Represents BFD authentication key in VPP """
172 def __init__(self, test, conf_key_id, auth_type, key):
175 self._auth_type = auth_type
176 test.assertIn(auth_type, BFDAuthType.desc_dict)
177 self._conf_key_id = conf_key_id
181 """ Test which created this key """
186 """ Authentication type for this key """
187 return self._auth_type
195 def key(self, value):
199 def conf_key_id(self):
200 """ configuration key ID """
201 return self._conf_key_id
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)
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()
212 if k.conf_key_id == self._conf_key_id:
216 def query_vpp_config(self):
217 return self.get_bfd_auth_keys_dump_entry() is not None
219 def remove_vpp_config(self):
220 self.test.vapi.bfd_auth_del_key(self._conf_key_id)
223 return "bfd-auth-key-%s" % self._conf_key_id
226 class VppBFDUDPSession(VppObject):
227 """ Represents BFD UDP session in VPP """
229 def __init__(self, test, interface, peer_addr, local_addr=None, af=AF_INET,
230 desired_min_tx=300000, required_min_rx=300000, detect_mult=3,
231 sha1_key=None, bfd_key_id=None):
233 self._interface = interface
235 self._local_addr = local_addr
236 if local_addr is not None:
237 self._local_addr_n = inet_pton(af, local_addr)
239 self._local_addr_n = None
240 self._peer_addr = peer_addr
241 self._peer_addr_n = inet_pton(af, peer_addr)
242 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
246 if bfd_key_id is not None:
247 self._bfd_key_id = bfd_key_id
249 self._bfd_key_id = randint(0, 255)
253 """ Test which created this session """
258 """ Interface on which this session lives """
259 return self._interface
263 """ Address family - AF_INET or AF_INET6 """
267 def local_addr(self):
268 """ BFD session local address (VPP address) """
269 if self._local_addr is None:
270 if self.af == AF_INET:
271 return self._interface.local_ip4
272 elif self.af == AF_INET6:
273 return self._interface.local_ip6
275 raise Exception("Unexpected af '%s'" % self.af)
276 return self._local_addr
279 def local_addr_n(self):
280 """ BFD session local address (VPP address) - raw, suitable for API """
281 if self._local_addr is None:
282 if self.af == AF_INET:
283 return self._interface.local_ip4n
284 elif self.af == AF_INET6:
285 return self._interface.local_ip6n
287 raise Exception("Unexpected af '%s'" % self.af)
288 return self._local_addr_n
292 """ BFD session peer address """
293 return self._peer_addr
296 def peer_addr_n(self):
297 """ BFD session peer address - raw, suitable for API """
298 return self._peer_addr_n
300 def get_bfd_udp_session_dump_entry(self):
301 """ get the namedtuple entry from bfd udp session dump """
302 result = self.test.vapi.bfd_udp_session_dump()
304 self.test.logger.debug("session entry: %s" % str(s))
305 if s.sw_if_index == self.interface.sw_if_index:
306 if self.af == AF_INET \
308 and self.interface.local_ip4n == s.local_addr[:4] \
309 and self.interface.remote_ip4n == s.peer_addr[:4]:
311 if self.af == AF_INET6 \
313 and self.interface.local_ip6n == s.local_addr \
314 and self.interface.remote_ip6n == s.peer_addr:
320 """ BFD session state """
321 session = self.get_bfd_udp_session_dump_entry()
323 raise Exception("Could not find BFD session in VPP response")
327 def desired_min_tx(self):
328 """ desired minimum tx interval """
329 return self._desired_min_tx
332 def required_min_rx(self):
333 """ required minimum rx interval """
334 return self._required_min_rx
337 def detect_mult(self):
338 """ detect multiplier """
339 return self._detect_mult
344 return self._sha1_key
347 def bfd_key_id(self):
348 """ bfd key id in use """
349 return self._bfd_key_id
351 def activate_auth(self, key, bfd_key_id=None, delayed=False):
352 """ activate authentication for this session """
353 self._bfd_key_id = bfd_key_id if bfd_key_id else randint(0, 255)
355 is_ipv6 = 1 if AF_INET6 == self.af else 0
356 conf_key_id = self._sha1_key.conf_key_id
357 is_delayed = 1 if delayed else 0
358 self.test.vapi.bfd_udp_auth_activate(self._interface.sw_if_index,
362 bfd_key_id=self._bfd_key_id,
363 conf_key_id=conf_key_id,
364 is_delayed=is_delayed)
366 def deactivate_auth(self, delayed=False):
367 """ deactivate authentication """
368 self._bfd_key_id = None
369 self._sha1_key = None
370 is_delayed = 1 if delayed else 0
371 is_ipv6 = 1 if AF_INET6 == self.af else 0
372 self.test.vapi.bfd_udp_auth_deactivate(self._interface.sw_if_index,
376 is_delayed=is_delayed)
378 def modify_parameters(self,
381 required_min_rx=None):
382 """ modify session parameters """
384 self._detect_mult = detect_mult
386 self._desired_min_tx = desired_min_tx
388 self._required_min_rx = required_min_rx
389 is_ipv6 = 1 if AF_INET6 == self.af else 0
390 self.test.vapi.bfd_udp_mod(self._interface.sw_if_index,
392 self.required_min_rx,
398 def add_vpp_config(self):
399 is_ipv6 = 1 if AF_INET6 == self.af else 0
400 bfd_key_id = self._bfd_key_id if self._sha1_key else None
401 conf_key_id = self._sha1_key.conf_key_id if self._sha1_key else None
402 self.test.vapi.bfd_udp_add(self._interface.sw_if_index,
404 self.required_min_rx,
409 bfd_key_id=bfd_key_id,
410 conf_key_id=conf_key_id)
411 self._test.registry.register(self, self.test.logger)
413 def query_vpp_config(self):
414 session = self.get_bfd_udp_session_dump_entry()
415 return session is not None
417 def remove_vpp_config(self):
418 is_ipv6 = 1 if AF_INET6 == self._af else 0
419 self.test.vapi.bfd_udp_del(self._interface.sw_if_index,
425 return "bfd-udp-%s-%s-%s-%s" % (self._interface.sw_if_index,
431 """ set bfd session admin-up """
432 is_ipv6 = 1 if AF_INET6 == self._af else 0
433 self.test.vapi.bfd_udp_session_set_flags(1,
434 self._interface.sw_if_index,
439 def admin_down(self):
440 """ set bfd session admin-down """
441 is_ipv6 = 1 if AF_INET6 == self._af else 0
442 self.test.vapi.bfd_udp_session_set_flags(0,
443 self._interface.sw_if_index,