""" BFD protocol implementation """ from random import randint from socket import AF_INET, AF_INET6, inet_pton from scapy.all import bind_layers from scapy.layers.inet import UDP from scapy.packet import Packet from scapy.fields import BitField, BitEnumField, XByteField, FlagsField,\ ConditionalField, StrField from vpp_object import VppObject from util import NumericConstant from vpp_ip import VppIpAddress from vpp_papi import VppEnum class BFDDiagCode(NumericConstant): """ BFD Diagnostic Code """ no_diagnostic = 0 control_detection_time_expired = 1 echo_function_failed = 2 neighbor_signaled_session_down = 3 forwarding_plane_reset = 4 path_down = 5 concatenated_path_down = 6 administratively_down = 7 reverse_concatenated_path_down = 8 desc_dict = { no_diagnostic: "No diagnostic", control_detection_time_expired: "Control Detection Time Expired", echo_function_failed: "Echo Function Failed", neighbor_signaled_session_down: "Neighbor Signaled Session Down", forwarding_plane_reset: "Forwarding Plane Reset", path_down: "Path Down", concatenated_path_down: "Concatenated Path Down", administratively_down: "Administratively Down", reverse_concatenated_path_down: "Reverse Concatenated Path Down", } class BFDState(NumericConstant): """ BFD State """ admin_down = 0 down = 1 init = 2 up = 3 desc_dict = { admin_down: "AdminDown", down: "Down", init: "Init", up: "Up", } class BFDAuthType(NumericConstant): """ BFD Authentication Type """ no_auth = 0 simple_pwd = 1 keyed_md5 = 2 meticulous_keyed_md5 = 3 keyed_sha1 = 4 meticulous_keyed_sha1 = 5 desc_dict = { no_auth: "No authentication", simple_pwd: "Simple Password", keyed_md5: "Keyed MD5", meticulous_keyed_md5: "Meticulous Keyed MD5", keyed_sha1: "Keyed SHA1", meticulous_keyed_sha1: "Meticulous Keyed SHA1", } def bfd_is_auth_used(pkt): """ is packet authenticated? """ return "A" in pkt.sprintf("%BFD.flags%") def bfd_is_simple_pwd_used(pkt): """ is simple password authentication used? """ return bfd_is_auth_used(pkt) and pkt.auth_type == BFDAuthType.simple_pwd def bfd_is_sha1_used(pkt): """ is sha1 authentication used? """ return bfd_is_auth_used(pkt) and pkt.auth_type in \ (BFDAuthType.keyed_sha1, BFDAuthType.meticulous_keyed_sha1) def bfd_is_md5_used(pkt): """ is md5 authentication used? """ return bfd_is_auth_used(pkt) and pkt.auth_type in \ (BFDAuthType.keyed_md5, BFDAuthType.meticulous_keyed_md5) def bfd_is_md5_or_sha1_used(pkt): """ is md5 or sha1 used? """ return bfd_is_md5_used(pkt) or bfd_is_sha1_used(pkt) class BFD(Packet): """ BFD protocol layer for scapy """ udp_dport = 3784 #: BFD destination port per RFC 5881 udp_dport_echo = 3785 # : BFD destination port for ECHO per RFC 5881 udp_sport_min = 49152 #: BFD source port min value per RFC 5881 udp_sport_max = 65535 #: BFD source port max value per RFC 5881 bfd_pkt_len = 24 # : length of BFD pkt without authentication section sha1_auth_len = 28 # : length of authentication section if SHA1 used name = "BFD" fields_desc = [ BitField("version", 1, 3), BitEnumField("diag", 0, 5, BFDDiagCode.desc_dict), BitEnumField("state", 0, 2, BFDState.desc_dict), FlagsField("flags", 0, 6, ['M', 'D', 'A', 'C', 'F', 'P']), XByteField("detect_mult", 0), BitField("length", bfd_pkt_len, 8), BitField("my_discriminator", 0, 32), BitField("your_discriminator", 0, 32), BitField("desired_min_tx_interval", 0, 32), BitField("required_min_rx_interval", 0, 32), BitField("required_min_echo_rx_interval", 0, 32), ConditionalField( BitEnumField("auth_type", 0, 8, BFDAuthType.desc_dict), bfd_is_auth_used), ConditionalField(BitField("auth_len", 0, 8), bfd_is_auth_used), ConditionalField(BitField("auth_key_id", 0, 8), bfd_is_auth_used), ConditionalField(BitField("auth_reserved", 0, 8), bfd_is_md5_or_sha1_used), ConditionalField( BitField("auth_seq_num", 0, 32), bfd_is_md5_or_sha1_used), ConditionalField(StrField("auth_key_hash", "0" * 16), bfd_is_md5_used), ConditionalField( StrField("auth_key_hash", "0" * 20), bfd_is_sha1_used), ] def mysummary(self): return self.sprintf("BFD(my_disc=%BFD.my_discriminator%," "your_disc=%BFD.your_discriminator%)") # glue the BFD packet class to scapy parser bind_layers(UDP, BFD, dport=BFD.udp_dport) class BFD_vpp_echo(Packet): """ BFD echo packet as used by VPP (non-rfc, as rfc doesn't define one) """ udp_dport = 3785 #: BFD echo destination port per RFC 5881 name = "BFD_VPP_ECHO" fields_desc = [ BitField("discriminator", 0, 32), BitField("expire_time_clocks", 0, 64), BitField("checksum", 0, 64) ] def mysummary(self): return self.sprintf( "BFD_VPP_ECHO(disc=%BFD_VPP_ECHO.discriminator%," "expire_time_clocks=%BFD_VPP_ECHO.expire_time_clocks%)") # glue the BFD echo packet class to scapy parser bind_layers(UDP, BFD_vpp_echo, dport=BFD_vpp_echo.udp_dport) class VppBFDAuthKey(VppObject): """ Represents BFD authentication key in VPP """ def __init__(self, test, conf_key_id, auth_type, key): self._test = test self._key = key self._auth_type = auth_type test.assertIn(auth_type, BFDAuthType.desc_dict) self._conf_key_id = conf_key_id @property def test(self): """ Test which created this key """ return self._test @property def auth_type(self): """ Authentication type for this key """ return self._auth_type @property def key(self): """ key data """ return self._key @key.setter def key(self, value): self._key = value @property def conf_key_id(self): """ configuration key ID """ return self._conf_key_id def add_vpp_config(self): self.test.vapi.bfd_auth_set_key( conf_key_id=self._conf_key_id, auth_type=self._auth_type, key=self._key, key_len=len(self._key)) self._test.registry.register(self, self.test.logger) def get_bfd_auth_keys_dump_entry(self): """ get the entry in the auth keys dump corresponding to this key """ result = self.test.vapi.bfd_auth_keys_dump() for k in result: if k.conf_key_id == self._conf_key_id: return k return None def query_vpp_config(self): return self.get_bfd_auth_keys_dump_entry() is not None def remove_vpp_config(self): self.test.vapi.bfd_auth_del_key(conf_key_id=self._conf_key_id) def object_id(self): return "bfd-auth-key-%s" % self._conf_key_id class VppBFDUDPSession(VppObject): """ Represents BFD UDP session in VPP ""