blob: 09a7681cea9380b34f04a1a7271e7c6c0840d946 [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,\
9 ConditionalField, StrField
10from 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
38 def __init__(self, value):
39 NumericConstant.__init__(self, value)
40
41
42class BFDState(NumericConstant):
43 """ BFD State """
44 admin_down = 0
45 down = 1
46 init = 2
47 up = 3
48
49 desc_dict = {
50 admin_down: "AdminDown",
51 down: "Down",
52 init: "Init",
53 up: "Up",
54 }
55
56 def __init__(self, value):
57 NumericConstant.__init__(self, value)
58
59
Klement Sekerab17dd962017-01-09 07:43:48 +010060class BFDAuthType(NumericConstant):
61 """ BFD Authentication Type """
62 no_auth = 0
63 simple_pwd = 1
64 keyed_md5 = 2
65 meticulous_keyed_md5 = 3
66 keyed_sha1 = 4
67 meticulous_keyed_sha1 = 5
68
69 desc_dict = {
70 no_auth: "No authentication",
71 simple_pwd: "Simple Password",
72 keyed_md5: "Keyed MD5",
73 meticulous_keyed_md5: "Meticulous Keyed MD5",
74 keyed_sha1: "Keyed SHA1",
75 meticulous_keyed_sha1: "Meticulous Keyed SHA1",
76 }
77
78 def __init__(self, value):
79 NumericConstant.__init__(self, value)
80
81
82def bfd_is_auth_used(pkt):
Klement Sekerad3ba5152017-02-14 03:09:17 +010083 """ is packet authenticated? """
Klement Sekerab17dd962017-01-09 07:43:48 +010084 return "A" in pkt.sprintf("%BFD.flags%")
85
86
87def bfd_is_simple_pwd_used(pkt):
Klement Sekerad3ba5152017-02-14 03:09:17 +010088 """ is simple password authentication used? """
Klement Sekerab17dd962017-01-09 07:43:48 +010089 return bfd_is_auth_used(pkt) and pkt.auth_type == BFDAuthType.simple_pwd
90
91
92def bfd_is_sha1_used(pkt):
Klement Sekerad3ba5152017-02-14 03:09:17 +010093 """ is sha1 authentication used? """
Klement Sekerab17dd962017-01-09 07:43:48 +010094 return bfd_is_auth_used(pkt) and pkt.auth_type in \
95 (BFDAuthType.keyed_sha1, BFDAuthType.meticulous_keyed_sha1)
96
97
98def bfd_is_md5_used(pkt):
Klement Sekerad3ba5152017-02-14 03:09:17 +010099 """ is md5 authentication used? """
Klement Sekerab17dd962017-01-09 07:43:48 +0100100 return bfd_is_auth_used(pkt) and pkt.auth_type in \
101 (BFDAuthType.keyed_md5, BFDAuthType.meticulous_keyed_md5)
102
103
104def bfd_is_md5_or_sha1_used(pkt):
Klement Sekerad3ba5152017-02-14 03:09:17 +0100105 """ is md5 or sha1 used? """
Klement Sekerab17dd962017-01-09 07:43:48 +0100106 return bfd_is_md5_used(pkt) or bfd_is_sha1_used(pkt)
107
108
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200109class BFD(Packet):
Klement Sekerad3ba5152017-02-14 03:09:17 +0100110 """ BFD protocol layer for scapy """
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200111
112 udp_dport = 3784 #: BFD destination port per RFC 5881
113 udp_sport_min = 49152 #: BFD source port min value per RFC 5881
114 udp_sport_max = 65535 #: BFD source port max value per RFC 5881
Klement Sekerab17dd962017-01-09 07:43:48 +0100115 bfd_pkt_len = 24 # : length of BFD pkt without authentication section
116 sha1_auth_len = 28 # : length of authentication section if SHA1 used
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200117
118 name = "BFD"
119
120 fields_desc = [
121 BitField("version", 1, 3),
122 BitEnumField("diag", 0, 5, BFDDiagCode.desc_dict),
123 BitEnumField("state", 0, 2, BFDState.desc_dict),
Klement Sekerab17dd962017-01-09 07:43:48 +0100124 FlagsField("flags", 0, 6, ['M', 'D', 'A', 'C', 'F', 'P']),
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200125 XByteField("detect_mult", 0),
Klement Sekerab17dd962017-01-09 07:43:48 +0100126 BitField("length", bfd_pkt_len, 8),
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200127 BitField("my_discriminator", 0, 32),
128 BitField("your_discriminator", 0, 32),
129 BitField("desired_min_tx_interval", 0, 32),
130 BitField("required_min_rx_interval", 0, 32),
Klement Sekerab17dd962017-01-09 07:43:48 +0100131 BitField("required_min_echo_rx_interval", 0, 32),
132 ConditionalField(
133 BitEnumField("auth_type", 0, 8, BFDAuthType.desc_dict),
134 bfd_is_auth_used),
135 ConditionalField(BitField("auth_len", 0, 8), bfd_is_auth_used),
136 ConditionalField(BitField("auth_key_id", 0, 8), bfd_is_auth_used),
137 ConditionalField(BitField("auth_reserved", 0, 8),
138 bfd_is_md5_or_sha1_used),
139 ConditionalField(
140 BitField("auth_seq_num", 0, 32), bfd_is_md5_or_sha1_used),
141 ConditionalField(StrField("auth_key_hash", "0" * 16), bfd_is_md5_used),
142 ConditionalField(
143 StrField("auth_key_hash", "0" * 20), bfd_is_sha1_used),
144 ]
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200145
146 def mysummary(self):
147 return self.sprintf("BFD(my_disc=%BFD.my_discriminator%,"
148 "your_disc=%BFD.your_discriminator%)")
149
150# glue the BFD packet class to scapy parser
151bind_layers(UDP, BFD, dport=BFD.udp_dport)
152
153
Klement Sekerab17dd962017-01-09 07:43:48 +0100154class VppBFDAuthKey(VppObject):
155 """ Represents BFD authentication key in VPP """
156
157 def __init__(self, test, conf_key_id, auth_type, key):
158 self._test = test
159 self._key = key
160 self._auth_type = auth_type
161 test.assertIn(auth_type, BFDAuthType.desc_dict)
162 self._conf_key_id = conf_key_id
163
164 @property
165 def test(self):
166 """ Test which created this key """
167 return self._test
168
169 @property
170 def auth_type(self):
171 """ Authentication type for this key """
172 return self._auth_type
173
174 @property
175 def key(self):
Klement Sekerad3ba5152017-02-14 03:09:17 +0100176 """ key data """
Klement Sekerab17dd962017-01-09 07:43:48 +0100177 return self._key
178
179 @property
180 def conf_key_id(self):
Klement Sekerad3ba5152017-02-14 03:09:17 +0100181 """ configuration key ID """
Klement Sekerab17dd962017-01-09 07:43:48 +0100182 return self._conf_key_id
183
184 def add_vpp_config(self):
185 self.test.vapi.bfd_auth_set_key(
186 self._conf_key_id, self._auth_type, self._key)
187 self._test.registry.register(self, self.test.logger)
188
189 def get_bfd_auth_keys_dump_entry(self):
190 """ get the entry in the auth keys dump corresponding to this key """
191 result = self.test.vapi.bfd_auth_keys_dump()
192 for k in result:
193 if k.conf_key_id == self._conf_key_id:
194 return k
195 return None
196
197 def query_vpp_config(self):
198 return self.get_bfd_auth_keys_dump_entry() is not None
199
200 def remove_vpp_config(self):
201 self.test.vapi.bfd_auth_del_key(self._conf_key_id)
202
203 def object_id(self):
204 return "bfd-auth-key-%s" % self._conf_key_id
205
206 def __str__(self):
207 return self.object_id()
208
209
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200210class VppBFDUDPSession(VppObject):
211 """ Represents BFD UDP session in VPP """
212
Klement Sekerab17dd962017-01-09 07:43:48 +0100213 def __init__(self, test, interface, peer_addr, local_addr=None, af=AF_INET,
214 desired_min_tx=100000, required_min_rx=100000, detect_mult=3,
215 sha1_key=None, bfd_key_id=None):
216 self._test = test
217 self._interface = interface
218 self._af = af
219 self._local_addr = local_addr
Klement Sekerad3ba5152017-02-14 03:09:17 +0100220 if local_addr is not None:
221 self._local_addr_n = inet_pton(af, local_addr)
222 else:
223 self._local_addr_n = None
Klement Sekerab17dd962017-01-09 07:43:48 +0100224 self._peer_addr = peer_addr
Klement Sekerad3ba5152017-02-14 03:09:17 +0100225 self._peer_addr_n = inet_pton(af, peer_addr)
Klement Sekerab17dd962017-01-09 07:43:48 +0100226 self._desired_min_tx = desired_min_tx
227 self._required_min_rx = required_min_rx
228 self._detect_mult = detect_mult
229 self._sha1_key = sha1_key
230 self._bfd_key_id = bfd_key_id if bfd_key_id else randint(0, 255)
231
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200232 @property
233 def test(self):
234 """ Test which created this session """
235 return self._test
236
237 @property
238 def interface(self):
239 """ Interface on which this session lives """
240 return self._interface
241
242 @property
243 def af(self):
244 """ Address family - AF_INET or AF_INET6 """
245 return self._af
246
247 @property
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200248 def local_addr(self):
249 """ BFD session local address (VPP address) """
250 if self._local_addr is None:
Klement Sekera46a87ad2017-01-02 08:22:23 +0100251 if self.af == AF_INET:
252 return self._interface.local_ip4
253 elif self.af == AF_INET6:
254 return self._interface.local_ip6
255 else:
Klement Sekerad3ba5152017-02-14 03:09:17 +0100256 raise Exception("Unexpected af '%s'" % self.af)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200257 return self._local_addr
258
259 @property
260 def local_addr_n(self):
261 """ BFD session local address (VPP address) - raw, suitable for API """
262 if self._local_addr is None:
Klement Sekera46a87ad2017-01-02 08:22:23 +0100263 if self.af == AF_INET:
264 return self._interface.local_ip4n
265 elif self.af == AF_INET6:
266 return self._interface.local_ip6n
267 else:
Klement Sekerad3ba5152017-02-14 03:09:17 +0100268 raise Exception("Unexpected af '%s'" % self.af)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200269 return self._local_addr_n
270
271 @property
272 def peer_addr(self):
273 """ BFD session peer address """
274 return self._peer_addr
275
276 @property
277 def peer_addr_n(self):
278 """ BFD session peer address - raw, suitable for API """
279 return self._peer_addr_n
280
Klement Sekerab17dd962017-01-09 07:43:48 +0100281 def get_bfd_udp_session_dump_entry(self):
Klement Sekerad3ba5152017-02-14 03:09:17 +0100282 """ get the namedtuple entry from bfd udp session dump """
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200283 result = self.test.vapi.bfd_udp_session_dump()
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200284 for s in result:
Klement Sekerab17dd962017-01-09 07:43:48 +0100285 self.test.logger.debug("session entry: %s" % str(s))
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200286 if s.sw_if_index == self.interface.sw_if_index:
287 if self.af == AF_INET \
288 and s.is_ipv6 == 0 \
289 and self.interface.local_ip4n == s.local_addr[:4] \
290 and self.interface.remote_ip4n == s.peer_addr[:4]:
Klement Sekerab17dd962017-01-09 07:43:48 +0100291 return s
292 if self.af == AF_INET6 \
293 and s.is_ipv6 == 1 \
294 and self.interface.local_ip6n == s.local_addr \
295 and self.interface.remote_ip6n == s.peer_addr:
296 return s
297 return None
298
299 @property
300 def state(self):
301 """ BFD session state """
302 session = self.get_bfd_udp_session_dump_entry()
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200303 if session is None:
Klement Sekerad3ba5152017-02-14 03:09:17 +0100304 raise Exception("Could not find BFD session in VPP response")
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200305 return session.state
306
307 @property
308 def desired_min_tx(self):
Klement Sekerad3ba5152017-02-14 03:09:17 +0100309 """ desired minimum tx interval """
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200310 return self._desired_min_tx
311
312 @property
313 def required_min_rx(self):
Klement Sekerad3ba5152017-02-14 03:09:17 +0100314 """ required minimum rx interval """
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200315 return self._required_min_rx
316
317 @property
318 def detect_mult(self):
Klement Sekerad3ba5152017-02-14 03:09:17 +0100319 """ detect multiplier """
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200320 return self._detect_mult
321
Klement Sekerab17dd962017-01-09 07:43:48 +0100322 @property
323 def sha1_key(self):
Klement Sekerad3ba5152017-02-14 03:09:17 +0100324 """ sha1 key """
Klement Sekerab17dd962017-01-09 07:43:48 +0100325 return self._sha1_key
326
327 @property
328 def bfd_key_id(self):
Klement Sekerad3ba5152017-02-14 03:09:17 +0100329 """ bfd key id in use """
Klement Sekerab17dd962017-01-09 07:43:48 +0100330 return self._bfd_key_id
331
332 def activate_auth(self, key, bfd_key_id=None, delayed=False):
Klement Sekerad3ba5152017-02-14 03:09:17 +0100333 """ activate authentication for this session """
Klement Sekerab17dd962017-01-09 07:43:48 +0100334 self._bfd_key_id = bfd_key_id if bfd_key_id else randint(0, 255)
335 self._sha1_key = key
336 is_ipv6 = 1 if AF_INET6 == self.af else 0
337 conf_key_id = self._sha1_key.conf_key_id
338 is_delayed = 1 if delayed else 0
339 self.test.vapi.bfd_udp_auth_activate(self._interface.sw_if_index,
340 self.local_addr_n,
341 self.peer_addr_n,
342 is_ipv6=is_ipv6,
343 bfd_key_id=self._bfd_key_id,
344 conf_key_id=conf_key_id,
345 is_delayed=is_delayed)
346
347 def deactivate_auth(self, delayed=False):
Klement Sekerad3ba5152017-02-14 03:09:17 +0100348 """ deactivate authentication """
Klement Sekerab17dd962017-01-09 07:43:48 +0100349 self._bfd_key_id = None
350 self._sha1_key = None
351 is_delayed = 1 if delayed else 0
352 is_ipv6 = 1 if AF_INET6 == self.af else 0
353 self.test.vapi.bfd_udp_auth_deactivate(self._interface.sw_if_index,
354 self.local_addr_n,
355 self.peer_addr_n,
356 is_ipv6=is_ipv6,
357 is_delayed=is_delayed)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200358
Klement Sekeraa57a9702017-02-02 06:58:07 +0100359 def modify_parameters(self,
360 detect_mult=None,
361 desired_min_tx=None,
362 required_min_rx=None):
Klement Sekerad3ba5152017-02-14 03:09:17 +0100363 """ modify session parameters """
Klement Sekeraa57a9702017-02-02 06:58:07 +0100364 if detect_mult:
365 self._detect_mult = detect_mult
366 if desired_min_tx:
367 self._desired_min_tx = desired_min_tx
368 if required_min_rx:
369 self._required_min_rx = required_min_rx
370 is_ipv6 = 1 if AF_INET6 == self.af else 0
371 self.test.vapi.bfd_udp_mod(self._interface.sw_if_index,
372 self.desired_min_tx,
373 self.required_min_rx,
374 self.detect_mult,
375 self.local_addr_n,
376 self.peer_addr_n,
377 is_ipv6=is_ipv6)
378
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200379 def add_vpp_config(self):
380 is_ipv6 = 1 if AF_INET6 == self.af else 0
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
383 self.test.vapi.bfd_udp_add(self._interface.sw_if_index,
384 self.desired_min_tx,
385 self.required_min_rx,
386 self.detect_mult,
387 self.local_addr_n,
388 self.peer_addr_n,
389 is_ipv6=is_ipv6,
390 bfd_key_id=bfd_key_id,
391 conf_key_id=conf_key_id)
Klement Sekera10db26f2017-01-11 08:16:53 +0100392 self._test.registry.register(self, self.test.logger)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200393
394 def query_vpp_config(self):
Klement Sekerab17dd962017-01-09 07:43:48 +0100395 session = self.get_bfd_udp_session_dump_entry()
396 return session is not None
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200397
398 def remove_vpp_config(self):
Klement Sekerab17dd962017-01-09 07:43:48 +0100399 is_ipv6 = 1 if AF_INET6 == self._af else 0
400 self.test.vapi.bfd_udp_del(self._interface.sw_if_index,
401 self.local_addr_n,
402 self.peer_addr_n,
403 is_ipv6=is_ipv6)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200404
405 def object_id(self):
Klement Sekerab17dd962017-01-09 07:43:48 +0100406 return "bfd-udp-%s-%s-%s-%s" % (self._interface.sw_if_index,
407 self.local_addr,
408 self.peer_addr,
409 self.af)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200410
Klement Sekera10db26f2017-01-11 08:16:53 +0100411 def __str__(self):
412 return self.object_id()
413
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200414 def admin_up(self):
Klement Sekerad3ba5152017-02-14 03:09:17 +0100415 """ set bfd session admin-up """
Klement Sekerab17dd962017-01-09 07:43:48 +0100416 is_ipv6 = 1 if AF_INET6 == self._af else 0
417 self.test.vapi.bfd_udp_session_set_flags(1,
418 self._interface.sw_if_index,
419 self.local_addr_n,
420 self.peer_addr_n,
421 is_ipv6=is_ipv6)