2 * Copyright (c) 2011-2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
17 * @brief BFD nodes implementation
21 #include <openssl/sha.h>
25 #include <x86intrin.h>
28 #include <vppinfra/random.h>
29 #include <vppinfra/error.h>
30 #include <vppinfra/hash.h>
31 #include <vppinfra/xxhash.h>
32 #include <vnet/ethernet/ethernet.h>
33 #include <vnet/ethernet/packet.h>
34 #include <vnet/bfd/bfd_debug.h>
35 #include <vnet/bfd/bfd_protocol.h>
36 #include <vnet/bfd/bfd_main.h>
39 bfd_calc_echo_checksum (u32 discriminator, u64 expire_time, u32 secret)
43 checksum = _mm_crc32_u64 (0, discriminator);
44 checksum = _mm_crc32_u64 (checksum, expire_time);
45 checksum = _mm_crc32_u64 (checksum, secret);
47 checksum = clib_xxhash (discriminator ^ expire_time ^ secret);
53 bfd_usec_to_clocks (const bfd_main_t * bm, u64 us)
55 return bm->cpu_cps * ((f64) us / USEC_PER_SECOND);
59 bfd_clocks_to_usec (const bfd_main_t * bm, u64 clocks)
61 return (clocks / bm->cpu_cps) * USEC_PER_SECOND;
64 static vlib_node_registration_t bfd_process_node;
66 /* set to 0 here, real values filled at startup */
67 static u32 bfd_node_index_by_transport[] = {
68 #define F(t, n) [BFD_TRANSPORT_##t] = 0,
69 foreach_bfd_transport (F)
74 format_bfd_auth_key (u8 * s, va_list * args)
76 const bfd_auth_key_t *key = va_arg (*args, bfd_auth_key_t *);
79 s = format (s, "{auth-type=%u:%s, conf-key-id=%u, use-count=%u}, ",
80 key->auth_type, bfd_auth_type_str (key->auth_type),
81 key->conf_key_id, key->use_count);
85 s = format (s, "{none}");
91 * We actually send all bfd pkts to the "error" node after scanning
92 * them, so the graph node has only one next-index. The "error-drop"
93 * node automatically bumps our per-node packet counters for us.
97 BFD_INPUT_NEXT_NORMAL,
101 static void bfd_on_state_change (bfd_main_t * bm, bfd_session_t * bs, u64 now,
102 int handling_wakeup);
105 bfd_set_defaults (bfd_main_t * bm, bfd_session_t * bs)
107 bs->local_state = BFD_STATE_down;
108 bs->local_diag = BFD_DIAG_CODE_no_diag;
109 bs->remote_state = BFD_STATE_down;
110 bs->remote_discr = 0;
111 bs->config_desired_min_tx_usec = BFD_DEFAULT_DESIRED_MIN_TX_USEC;
112 bs->config_desired_min_tx_clocks = bm->default_desired_min_tx_clocks;
113 bs->effective_desired_min_tx_clocks = bm->default_desired_min_tx_clocks;
114 bs->remote_min_rx_usec = 1;
115 bs->remote_min_rx_clocks = bfd_usec_to_clocks (bm, bs->remote_min_rx_usec);
116 bs->remote_min_echo_rx_usec = 0;
117 bs->remote_min_echo_rx_clocks = 0;
118 bs->remote_demand = 0;
119 bs->auth.remote_seq_number = 0;
120 bs->auth.remote_seq_number_known = 0;
121 bs->auth.local_seq_number = random_u32 (&bm->random_seed);
122 bs->echo_secret = random_u32 (&bm->random_seed);
126 bfd_set_diag (bfd_session_t * bs, bfd_diag_code_e code)
128 if (bs->local_diag != code)
130 BFD_DBG ("set local_diag, bs_idx=%d: '%d:%s'", bs->bs_idx, code,
131 bfd_diag_code_string (code));
132 bs->local_diag = code;
137 bfd_set_state (bfd_main_t * bm, bfd_session_t * bs,
138 bfd_state_e new_state, int handling_wakeup)
140 if (bs->local_state != new_state)
142 BFD_DBG ("Change state, bs_idx=%d: %s->%s", bs->bs_idx,
143 bfd_state_string (bs->local_state),
144 bfd_state_string (new_state));
145 bs->local_state = new_state;
146 bfd_on_state_change (bm, bs, clib_cpu_time_now (), handling_wakeup);
151 bfd_poll_state_string (bfd_poll_state_e state)
157 return "BFD_POLL_" #x;
158 foreach_bfd_poll_state (F)
165 bfd_set_poll_state (bfd_session_t * bs, bfd_poll_state_e state)
167 if (bs->poll_state != state)
169 BFD_DBG ("Setting poll state=%s, bs_idx=%u",
170 bfd_poll_state_string (state), bs->bs_idx);
171 bs->poll_state = state;
176 bfd_recalc_tx_interval (bfd_main_t * bm, bfd_session_t * bs)
178 bs->transmit_interval_clocks =
179 clib_max (bs->effective_desired_min_tx_clocks, bs->remote_min_rx_clocks);
180 BFD_DBG ("Recalculated transmit interval " BFD_CLK_FMT,
181 BFD_CLK_PRN (bs->transmit_interval_clocks));
185 bfd_recalc_echo_tx_interval (bfd_main_t * bm, bfd_session_t * bs)
187 bs->echo_transmit_interval_clocks =
188 clib_max (bs->effective_desired_min_tx_clocks,
189 bs->remote_min_echo_rx_clocks);
190 BFD_DBG ("Recalculated echo transmit interval " BFD_CLK_FMT,
191 BFD_CLK_PRN (bs->echo_transmit_interval_clocks));
195 bfd_calc_next_tx (bfd_main_t * bm, bfd_session_t * bs, u64 now)
197 if (bs->local_detect_mult > 1)
199 /* common case - 75-100% of transmit interval */
200 bs->tx_timeout_clocks = bs->last_tx_clocks +
201 (1 - .25 * (random_f64 (&bm->random_seed))) *
202 bs->transmit_interval_clocks;
203 if (bs->tx_timeout_clocks < now)
206 * the timeout is in the past, which means that either remote
207 * demand mode was set or performance/clock issues ...
209 BFD_DBG ("Missed %lu transmit events (now is %lu, calc "
210 "tx_timeout is %lu)",
211 (now - bs->tx_timeout_clocks) /
212 bs->transmit_interval_clocks, now, bs->tx_timeout_clocks);
213 bs->tx_timeout_clocks = now;
218 /* special case - 75-90% of transmit interval */
219 bs->tx_timeout_clocks = bs->last_tx_clocks +
220 (.9 - .15 * (random_f64 (&bm->random_seed))) *
221 bs->transmit_interval_clocks;
222 if (bs->tx_timeout_clocks < now)
225 * the timeout is in the past, which means that either remote
226 * demand mode was set or performance/clock issues ...
228 BFD_DBG ("Missed %lu transmit events (now is %lu, calc "
229 "tx_timeout is %lu)",
230 (now - bs->tx_timeout_clocks) /
231 bs->transmit_interval_clocks, now, bs->tx_timeout_clocks);
232 bs->tx_timeout_clocks = now;
235 if (bs->tx_timeout_clocks)
237 BFD_DBG ("Next transmit in %lu clocks/%.02fs@%lu",
238 bs->tx_timeout_clocks - now,
239 (bs->tx_timeout_clocks - now) / bm->cpu_cps,
240 bs->tx_timeout_clocks);
245 bfd_calc_next_echo_tx (bfd_main_t * bm, bfd_session_t * bs, u64 now)
247 bs->echo_tx_timeout_clocks =
248 bs->echo_last_tx_clocks + bs->echo_transmit_interval_clocks;
249 if (bs->echo_tx_timeout_clocks < now)
251 /* huh, we've missed it already, transmit now */
252 BFD_DBG ("Missed %lu echo transmit events (now is %lu, calc tx_timeout "
254 (now - bs->echo_tx_timeout_clocks) /
255 bs->echo_transmit_interval_clocks,
256 now, bs->echo_tx_timeout_clocks);
257 bs->echo_tx_timeout_clocks = now;
259 BFD_DBG ("Next echo transmit in %lu clocks/%.02fs@%lu",
260 bs->echo_tx_timeout_clocks - now,
261 (bs->echo_tx_timeout_clocks - now) / bm->cpu_cps,
262 bs->echo_tx_timeout_clocks);
266 bfd_recalc_detection_time (bfd_main_t * bm, bfd_session_t * bs)
268 bs->detection_time_clocks =
269 bs->remote_detect_mult * clib_max (bs->effective_required_min_rx_clocks,
270 bs->remote_desired_min_tx_clocks);
271 BFD_DBG ("Recalculated detection time %lu clocks/%.2fs",
272 bs->detection_time_clocks,
273 bs->detection_time_clocks / bm->cpu_cps);
277 bfd_set_timer (bfd_main_t * bm, bfd_session_t * bs, u64 now,
283 if (BFD_STATE_up == bs->local_state)
285 rx_timeout = bs->last_rx_clocks + bs->detection_time_clocks;
287 if (BFD_STATE_up != bs->local_state || !bs->remote_demand ||
288 BFD_POLL_NOT_NEEDED != bs->poll_state)
290 tx_timeout = bs->tx_timeout_clocks;
292 if (tx_timeout && rx_timeout)
294 next = clib_min (tx_timeout, rx_timeout);
304 if (bs->echo && next > bs->echo_tx_timeout_clocks)
306 next = bs->echo_tx_timeout_clocks;
308 BFD_DBG ("bs_idx=%u, tx_timeout=%lu, echo_tx_timeout=%lu, rx_timeout=%lu, "
310 bs->bs_idx, tx_timeout, bs->echo_tx_timeout_clocks, rx_timeout,
312 ? "tx" : (next == bs->echo_tx_timeout_clocks ? "echo tx" : "rx"));
313 /* sometimes the wheel expires an event a bit sooner than requested, account
315 if (next && (now + bm->wheel_inaccuracy > bs->wheel_time_clocks ||
316 next < bs->wheel_time_clocks || !bs->wheel_time_clocks))
318 bs->wheel_time_clocks = next;
319 BFD_DBG ("timing_wheel_insert(%p, %lu (%ld clocks/%.2fs in the "
321 &bm->wheel, bs->wheel_time_clocks,
322 (i64) bs->wheel_time_clocks - clib_cpu_time_now (),
323 (i64) (bs->wheel_time_clocks - clib_cpu_time_now ()) /
324 bm->cpu_cps, bs->bs_idx);
325 timing_wheel_insert (&bm->wheel, bs->wheel_time_clocks, bs->bs_idx);
326 if (!handling_wakeup)
328 vlib_process_signal_event (bm->vlib_main,
329 bm->bfd_process_node_index,
330 BFD_EVENT_RESCHEDULE, bs->bs_idx);
336 bfd_set_effective_desired_min_tx (bfd_main_t * bm,
337 bfd_session_t * bs, u64 now,
338 u64 desired_min_tx_clocks)
340 bs->effective_desired_min_tx_clocks = desired_min_tx_clocks;
341 BFD_DBG ("Set effective desired min tx to " BFD_CLK_FMT,
342 BFD_CLK_PRN (bs->effective_desired_min_tx_clocks));
343 bfd_recalc_detection_time (bm, bs);
344 bfd_recalc_tx_interval (bm, bs);
345 bfd_recalc_echo_tx_interval (bm, bs);
346 bfd_calc_next_tx (bm, bs, now);
350 bfd_set_effective_required_min_rx (bfd_main_t * bm,
351 bfd_session_t * bs, u64 now,
352 u64 required_min_rx_clocks)
354 bs->effective_required_min_rx_clocks = required_min_rx_clocks;
355 BFD_DBG ("Set effective required min rx to " BFD_CLK_FMT,
356 BFD_CLK_PRN (bs->effective_required_min_rx_clocks));
357 bfd_recalc_detection_time (bm, bs);
361 bfd_set_remote_required_min_rx (bfd_main_t * bm, bfd_session_t * bs,
362 u64 now, u32 remote_required_min_rx_usec)
364 if (bs->remote_min_rx_usec != remote_required_min_rx_usec)
366 bs->remote_min_rx_usec = remote_required_min_rx_usec;
367 bs->remote_min_rx_clocks =
368 bfd_usec_to_clocks (bm, remote_required_min_rx_usec);
369 BFD_DBG ("Set remote min rx to " BFD_CLK_FMT,
370 BFD_CLK_PRN (bs->remote_min_rx_clocks));
371 bfd_recalc_detection_time (bm, bs);
372 bfd_recalc_tx_interval (bm, bs);
377 bfd_set_remote_required_min_echo_rx (bfd_main_t * bm, bfd_session_t * bs,
379 u32 remote_required_min_echo_rx_usec)
381 if (bs->remote_min_echo_rx_usec != remote_required_min_echo_rx_usec)
383 bs->remote_min_echo_rx_usec = remote_required_min_echo_rx_usec;
384 bs->remote_min_echo_rx_clocks =
385 bfd_usec_to_clocks (bm, bs->remote_min_echo_rx_usec);
386 BFD_DBG ("Set remote min echo rx to " BFD_CLK_FMT,
387 BFD_CLK_PRN (bs->remote_min_echo_rx_clocks));
388 bfd_recalc_echo_tx_interval (bm, bs);
393 bfd_session_start (bfd_main_t * bm, bfd_session_t * bs)
395 BFD_DBG ("\nStarting session: %U", format_bfd_session, bs);
396 bfd_recalc_tx_interval (bm, bs);
397 vlib_process_signal_event (bm->vlib_main, bm->bfd_process_node_index,
398 BFD_EVENT_NEW_SESSION, bs->bs_idx);
402 bfd_del_session (uword bs_idx)
404 const bfd_main_t *bm = &bfd_main;
405 if (!pool_is_free_index (bm->sessions, bs_idx))
407 bfd_session_t *bs = pool_elt_at_index (bm->sessions, bs_idx);
408 pool_put (bm->sessions, bs);
413 BFD_ERR ("no such session");
414 return VNET_API_ERROR_BFD_ENOENT;
420 bfd_session_set_flags (bfd_session_t * bs, u8 admin_up_down)
422 bfd_main_t *bm = &bfd_main;
425 BFD_DBG ("Session set admin-up, bs-idx=%u", bs->bs_idx);
426 bfd_set_state (bm, bs, BFD_STATE_down, 0);
427 bfd_set_diag (bs, BFD_DIAG_CODE_no_diag);
431 BFD_DBG ("Session set admin-down, bs-idx=%u", bs->bs_idx);
432 bfd_set_diag (bs, BFD_DIAG_CODE_admin_down);
433 bfd_set_state (bm, bs, BFD_STATE_admin_down, 0);
438 bfd_input_format_trace (u8 * s, va_list * args)
440 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
441 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
442 const bfd_input_trace_t *t = va_arg (*args, bfd_input_trace_t *);
443 const bfd_pkt_t *pkt = (bfd_pkt_t *) t->data;
444 if (t->len > STRUCT_SIZE_OF (bfd_pkt_t, head))
446 s = format (s, "BFD v%u, diag=%u(%s), state=%u(%s),\n"
447 " flags=(P:%u, F:%u, C:%u, A:%u, D:%u, M:%u), "
448 "detect_mult=%u, length=%u\n",
449 bfd_pkt_get_version (pkt), bfd_pkt_get_diag_code (pkt),
450 bfd_diag_code_string (bfd_pkt_get_diag_code (pkt)),
451 bfd_pkt_get_state (pkt),
452 bfd_state_string (bfd_pkt_get_state (pkt)),
453 bfd_pkt_get_poll (pkt), bfd_pkt_get_final (pkt),
454 bfd_pkt_get_control_plane_independent (pkt),
455 bfd_pkt_get_auth_present (pkt), bfd_pkt_get_demand (pkt),
456 bfd_pkt_get_multipoint (pkt), pkt->head.detect_mult,
458 if (t->len >= sizeof (bfd_pkt_t) &&
459 pkt->head.length >= sizeof (bfd_pkt_t))
461 s = format (s, " my discriminator: %u\n",
462 clib_net_to_host_u32 (pkt->my_disc));
463 s = format (s, " your discriminator: %u\n",
464 clib_net_to_host_u32 (pkt->your_disc));
465 s = format (s, " desired min tx interval: %u\n",
466 clib_net_to_host_u32 (pkt->des_min_tx));
467 s = format (s, " required min rx interval: %u\n",
468 clib_net_to_host_u32 (pkt->req_min_rx));
469 s = format (s, " required min echo rx interval: %u",
470 clib_net_to_host_u32 (pkt->req_min_echo_rx));
472 if (t->len >= sizeof (bfd_pkt_with_common_auth_t) &&
473 pkt->head.length >= sizeof (bfd_pkt_with_common_auth_t) &&
474 bfd_pkt_get_auth_present (pkt))
476 const bfd_pkt_with_common_auth_t *with_auth = (void *) pkt;
477 const bfd_auth_common_t *common = &with_auth->common_auth;
478 s = format (s, "\n auth len: %u\n", common->len);
479 s = format (s, " auth type: %u:%s\n", common->type,
480 bfd_auth_type_str (common->type));
481 if (t->len >= sizeof (bfd_pkt_with_sha1_auth_t) &&
482 pkt->head.length >= sizeof (bfd_pkt_with_sha1_auth_t) &&
483 (BFD_AUTH_TYPE_keyed_sha1 == common->type ||
484 BFD_AUTH_TYPE_meticulous_keyed_sha1 == common->type))
486 const bfd_pkt_with_sha1_auth_t *with_sha1 = (void *) pkt;
487 const bfd_auth_sha1_t *sha1 = &with_sha1->sha1_auth;
488 s = format (s, " seq num: %u\n",
489 clib_net_to_host_u32 (sha1->seq_num));
490 s = format (s, " key id: %u\n", sha1->key_id);
491 s = format (s, " hash: %U", format_hex_bytes, sha1->hash,
492 sizeof (sha1->hash));
497 s = format (s, "\n");
505 bfd_on_state_change (bfd_main_t * bm, bfd_session_t * bs, u64 now,
508 BFD_DBG ("\nState changed: %U", format_bfd_session, bs);
510 switch (bs->local_state)
512 case BFD_STATE_admin_down:
514 bfd_set_effective_desired_min_tx (bm, bs, now,
516 (bs->config_desired_min_tx_clocks,
517 bm->default_desired_min_tx_clocks));
518 bfd_set_effective_required_min_rx (bm, bs, now,
519 bs->config_required_min_rx_clocks);
520 bfd_set_timer (bm, bs, now, handling_wakeup);
524 bfd_set_effective_desired_min_tx (bm, bs, now,
526 (bs->config_desired_min_tx_clocks,
527 bm->default_desired_min_tx_clocks));
528 bfd_set_effective_required_min_rx (bm, bs, now,
529 bs->config_required_min_rx_clocks);
530 bfd_set_timer (bm, bs, now, handling_wakeup);
534 bfd_set_effective_desired_min_tx (bm, bs, now,
535 bs->config_desired_min_tx_clocks);
536 bfd_set_timer (bm, bs, now, handling_wakeup);
539 bfd_set_effective_desired_min_tx (bm, bs, now,
540 bs->config_desired_min_tx_clocks);
541 if (BFD_POLL_NOT_NEEDED == bs->poll_state)
543 bfd_set_effective_required_min_rx (bm, bs, now,
544 bs->config_required_min_rx_clocks);
546 bfd_set_timer (bm, bs, now, handling_wakeup);
552 bfd_on_config_change (vlib_main_t * vm, vlib_node_runtime_t * rt,
553 bfd_main_t * bm, bfd_session_t * bs, u64 now)
556 * if remote demand mode is set and we need to do a poll, set the next
557 * timeout so that the session wakes up immediately
559 if (bs->remote_demand && BFD_POLL_NEEDED == bs->poll_state &&
560 bs->poll_state_start_or_timeout_clocks < now)
562 bs->tx_timeout_clocks = now;
564 bfd_recalc_detection_time (bm, bs);
565 bfd_set_timer (bm, bs, now, 0);
569 bfd_add_transport_layer (vlib_main_t * vm, vlib_buffer_t * b,
572 switch (bs->transport)
574 case BFD_TRANSPORT_UDP4:
575 BFD_DBG ("Transport bfd via udp4, bs_idx=%u", bs->bs_idx);
576 bfd_add_udp4_transport (vm, b, bs, 0 /* is_echo */ );
578 case BFD_TRANSPORT_UDP6:
579 BFD_DBG ("Transport bfd via udp6, bs_idx=%u", bs->bs_idx);
580 bfd_add_udp6_transport (vm, b, bs, 0 /* is_echo */ );
586 bfd_echo_add_transport_layer (vlib_main_t * vm, vlib_buffer_t * b,
589 switch (bs->transport)
591 case BFD_TRANSPORT_UDP4:
592 BFD_DBG ("Transport bfd echo via udp4, bs_idx=%u", bs->bs_idx);
593 return bfd_add_udp4_transport (vm, b, bs, 1 /* is_echo */ );
595 case BFD_TRANSPORT_UDP6:
596 BFD_DBG ("Transport bfd echo via udp6, bs_idx=%u", bs->bs_idx);
597 return bfd_add_udp6_transport (vm, b, bs, 1 /* is_echo */ );
604 bfd_create_frame_to_next_node (vlib_main_t * vm, bfd_session_t * bs, u32 bi)
608 vlib_get_frame_to_node (vm, bfd_node_index_by_transport[bs->transport]);
610 u32 *to_next = vlib_frame_vector_args (f);
613 vlib_put_frame_to_node (vm, bfd_node_index_by_transport[bs->transport], f);
618 bfd_add_sha1_auth_section (vlib_buffer_t * b, bfd_session_t * bs)
620 bfd_pkt_with_sha1_auth_t *pkt = vlib_buffer_get_current (b);
621 bfd_auth_sha1_t *auth = &pkt->sha1_auth;
622 b->current_length += sizeof (*auth);
623 pkt->pkt.head.length += sizeof (*auth);
624 bfd_pkt_set_auth_present (&pkt->pkt);
625 memset (auth, 0, sizeof (*auth));
626 auth->type_len.type = bs->auth.curr_key->auth_type;
628 * only meticulous authentication types require incrementing seq number
629 * for every message, but doing so doesn't violate the RFC
631 ++bs->auth.local_seq_number;
632 auth->type_len.len = sizeof (bfd_auth_sha1_t);
633 auth->key_id = bs->auth.curr_bfd_key_id;
634 auth->seq_num = clib_host_to_net_u32 (bs->auth.local_seq_number);
636 * first copy the password into the packet, then calculate the hash
637 * and finally replace the password with the calculated hash
639 clib_memcpy (auth->hash, bs->auth.curr_key->key,
640 sizeof (bs->auth.curr_key->key));
641 unsigned char hash[sizeof (auth->hash)];
642 SHA1 ((unsigned char *) pkt, sizeof (*pkt), hash);
643 BFD_DBG ("hashing: %U", format_hex_bytes, pkt, sizeof (*pkt));
644 clib_memcpy (auth->hash, hash, sizeof (hash));
649 bfd_add_auth_section (vlib_buffer_t * b, bfd_session_t * bs)
651 if (bs->auth.curr_key)
653 const bfd_auth_type_e auth_type = bs->auth.curr_key->auth_type;
656 case BFD_AUTH_TYPE_reserved:
658 case BFD_AUTH_TYPE_simple_password:
660 case BFD_AUTH_TYPE_keyed_md5:
662 case BFD_AUTH_TYPE_meticulous_keyed_md5:
663 clib_warning ("Internal error, unexpected BFD auth type '%d'",
667 case BFD_AUTH_TYPE_keyed_sha1:
669 case BFD_AUTH_TYPE_meticulous_keyed_sha1:
670 bfd_add_sha1_auth_section (b, bs);
673 case BFD_AUTH_TYPE_keyed_sha1:
675 case BFD_AUTH_TYPE_meticulous_keyed_sha1:
676 clib_warning ("Internal error, unexpected BFD auth type '%d'",
685 bfd_is_echo_possible (bfd_session_t * bs)
687 if (BFD_STATE_up == bs->local_state && BFD_STATE_up == bs->remote_state &&
688 bs->remote_min_echo_rx_usec > 0)
690 switch (bs->transport)
692 case BFD_TRANSPORT_UDP4:
693 return bfd_udp_is_echo_available (BFD_TRANSPORT_UDP4);
694 case BFD_TRANSPORT_UDP6:
695 return bfd_udp_is_echo_available (BFD_TRANSPORT_UDP6);
702 bfd_init_control_frame (bfd_main_t * bm, bfd_session_t * bs,
705 bfd_pkt_t *pkt = vlib_buffer_get_current (b);
707 bfd_length = sizeof (bfd_pkt_t);
708 memset (pkt, 0, sizeof (*pkt));
709 bfd_pkt_set_version (pkt, 1);
710 bfd_pkt_set_diag_code (pkt, bs->local_diag);
711 bfd_pkt_set_state (pkt, bs->local_state);
712 pkt->head.detect_mult = bs->local_detect_mult;
713 pkt->head.length = clib_host_to_net_u32 (bfd_length);
714 pkt->my_disc = bs->local_discr;
715 pkt->your_disc = bs->remote_discr;
716 pkt->des_min_tx = clib_host_to_net_u32 (bs->config_desired_min_tx_usec);
720 clib_host_to_net_u32 (bfd_clocks_to_usec
721 (bm, bs->effective_required_min_rx_clocks));
726 clib_host_to_net_u32 (bs->config_required_min_rx_usec);
728 pkt->req_min_echo_rx = clib_host_to_net_u32 (1);
729 b->current_length = bfd_length;
733 bfd_send_echo (vlib_main_t * vm, vlib_node_runtime_t * rt,
734 bfd_main_t * bm, bfd_session_t * bs, u64 now,
737 if (!bfd_is_echo_possible (bs))
739 BFD_DBG ("\nSwitching off echo function: %U", format_bfd_session, bs);
743 /* sometimes the wheel expires an event a bit sooner than requested, account
745 if (now + bm->wheel_inaccuracy >= bs->echo_tx_timeout_clocks)
747 BFD_DBG ("\nSending echo packet: %U", format_bfd_session, bs);
749 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
751 clib_warning ("buffer allocation failure");
754 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
755 ASSERT (b->current_data == 0);
756 bfd_echo_pkt_t *pkt = vlib_buffer_get_current (b);
757 memset (pkt, 0, sizeof (*pkt));
758 pkt->discriminator = bs->local_discr;
759 pkt->expire_time_clocks =
760 now + bs->echo_transmit_interval_clocks * bs->local_detect_mult;
762 bfd_calc_echo_checksum (bs->local_discr, pkt->expire_time_clocks,
764 b->current_length = sizeof (*pkt);
765 if (!bfd_echo_add_transport_layer (vm, b, bs))
767 BFD_ERR ("cannot send echo packet out, turning echo off");
769 vlib_buffer_free_one (vm, bi);
772 bs->echo_last_tx_clocks = now;
773 bfd_calc_next_echo_tx (bm, bs, now);
774 bfd_create_frame_to_next_node (vm, bs, bi);
779 ("No need to send echo packet now, now is %lu, tx_timeout is %lu",
780 now, bs->echo_tx_timeout_clocks);
785 bfd_send_periodic (vlib_main_t * vm, vlib_node_runtime_t * rt,
786 bfd_main_t * bm, bfd_session_t * bs, u64 now,
789 if (!bs->remote_min_rx_usec && BFD_POLL_NOT_NEEDED == bs->poll_state)
791 BFD_DBG ("Remote min rx interval is zero, not sending periodic control "
795 if (BFD_POLL_NOT_NEEDED == bs->poll_state && bs->remote_demand &&
796 BFD_STATE_up == bs->local_state && BFD_STATE_up == bs->remote_state)
799 * A system MUST NOT periodically transmit BFD Control packets if Demand
800 * mode is active on the remote system (bfd.RemoteDemandMode is 1,
801 * bfd.SessionState is Up, and bfd.RemoteSessionState is Up) and a Poll
802 * Sequence is not being transmitted.
804 BFD_DBG ("Remote demand is set, not sending periodic control frame");
807 /* sometimes the wheel expires an event a bit sooner than requested, account
809 if (now + bm->wheel_inaccuracy >= bs->tx_timeout_clocks)
811 BFD_DBG ("\nSending periodic control frame: %U", format_bfd_session,
814 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
816 clib_warning ("buffer allocation failure");
819 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
820 ASSERT (b->current_data == 0);
821 bfd_init_control_frame (bm, bs, b);
822 switch (bs->poll_state)
824 case BFD_POLL_NEEDED:
825 if (now < bs->poll_state_start_or_timeout_clocks)
827 BFD_DBG ("Cannot start a poll sequence yet, need to wait "
829 BFD_CLK_PRN (bs->poll_state_start_or_timeout_clocks -
833 bs->poll_state_start_or_timeout_clocks = now;
834 bfd_set_poll_state (bs, BFD_POLL_IN_PROGRESS);
836 case BFD_POLL_IN_PROGRESS:
837 case BFD_POLL_IN_PROGRESS_AND_QUEUED:
838 bfd_pkt_set_poll (vlib_buffer_get_current (b));
839 BFD_DBG ("Setting poll bit in packet, bs_idx=%u", bs->bs_idx);
841 case BFD_POLL_NOT_NEEDED:
845 bfd_add_auth_section (b, bs);
846 bfd_add_transport_layer (vm, b, bs);
847 bs->last_tx_clocks = now;
848 bfd_calc_next_tx (bm, bs, now);
849 bfd_create_frame_to_next_node (vm, bs, bi);
854 ("No need to send control frame now, now is %lu, tx_timeout is %lu",
855 now, bs->tx_timeout_clocks);
860 bfd_init_final_control_frame (vlib_main_t * vm, vlib_buffer_t * b,
861 bfd_main_t * bm, bfd_session_t * bs)
863 BFD_DBG ("Send final control frame for bs_idx=%lu", bs->bs_idx);
864 bfd_init_control_frame (bm, bs, b);
865 bfd_pkt_set_final (vlib_buffer_get_current (b));
866 bfd_add_auth_section (b, bs);
867 bfd_add_transport_layer (vm, b, bs);
868 bs->last_tx_clocks = clib_cpu_time_now ();
870 * RFC allows to include changes in final frame, so if there were any
871 * pending, we already did that, thus we can clear any pending poll needs
873 bfd_set_poll_state (bs, BFD_POLL_NOT_NEEDED);
877 bfd_check_rx_timeout (bfd_main_t * bm, bfd_session_t * bs, u64 now,
880 /* sometimes the wheel expires an event a bit sooner than requested, account
882 if (bs->last_rx_clocks + bs->detection_time_clocks <=
883 now + bm->wheel_inaccuracy)
885 BFD_DBG ("Rx timeout, session goes down");
886 bfd_set_diag (bs, BFD_DIAG_CODE_det_time_exp);
887 bfd_set_state (bm, bs, BFD_STATE_down, handling_wakeup);
889 * If the remote system does not receive any
890 * BFD Control packets for a Detection Time, it SHOULD reset
891 * bfd.RemoteMinRxInterval to its initial value of 1 (per section 6.8.1,
892 * since it is no longer required to maintain previous session state)
893 * and then can transmit at its own rate.
895 bfd_set_remote_required_min_rx (bm, bs, now, 1);
898 bs->echo_last_rx_clocks +
899 bs->echo_transmit_interval_clocks * bs->local_detect_mult <=
900 now + bm->wheel_inaccuracy)
902 BFD_DBG ("Echo rx timeout, session goes down");
903 bfd_set_diag (bs, BFD_DIAG_CODE_echo_failed);
904 bfd_set_state (bm, bs, BFD_STATE_down, handling_wakeup);
909 bfd_on_timeout (vlib_main_t * vm, vlib_node_runtime_t * rt, bfd_main_t * bm,
910 bfd_session_t * bs, u64 now)
912 BFD_DBG ("Timeout for bs_idx=%lu", bs->bs_idx);
913 switch (bs->local_state)
915 case BFD_STATE_admin_down:
916 bfd_send_periodic (vm, rt, bm, bs, now, 1);
919 bfd_send_periodic (vm, rt, bm, bs, now, 1);
922 bfd_check_rx_timeout (bm, bs, now, 1);
923 bfd_send_periodic (vm, rt, bm, bs, now, 1);
926 bfd_check_rx_timeout (bm, bs, now, 1);
927 if (BFD_POLL_NOT_NEEDED == bs->poll_state && !bs->echo &&
928 bfd_is_echo_possible (bs))
930 /* switch on echo function as main detection method now */
931 BFD_DBG ("Switching on echo function, bs_idx=%u", bs->bs_idx);
933 bs->echo_last_rx_clocks = now;
934 bs->echo_tx_timeout_clocks = now;
935 bfd_set_effective_required_min_rx (bm, bs, now,
937 (bm->min_required_min_rx_while_echo_clocks,
938 bs->config_required_min_rx_clocks));
939 bfd_set_poll_state (bs, BFD_POLL_NEEDED);
941 bfd_send_periodic (vm, rt, bm, bs, now, 1);
944 bfd_send_echo (vm, rt, bm, bs, now, 1);
951 * bfd process node function
954 bfd_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
956 bfd_main_t *bm = &bfd_main;
958 uword event_type, *event_data = 0;
960 /* So we can send events to the bfd process */
961 bm->bfd_process_node_index = bfd_process_node.index;
965 u64 now = clib_cpu_time_now ();
966 u64 next_expire = timing_wheel_next_expiring_elt_time (&bm->wheel);
967 BFD_DBG ("timing_wheel_next_expiring_elt_time(%p) returns %lu",
968 &bm->wheel, next_expire);
969 if ((i64) next_expire < 0)
971 BFD_DBG ("wait for event without timeout");
972 (void) vlib_process_wait_for_event (vm);
973 event_type = vlib_process_get_events (vm, &event_data);
977 f64 timeout = ((i64) next_expire - (i64) now) / bm->cpu_cps;
978 BFD_DBG ("wait for event with timeout %.02f", timeout);
981 BFD_DBG ("negative timeout, already expired, skipping wait");
986 (void) vlib_process_wait_for_event_or_clock (vm, timeout);
987 event_type = vlib_process_get_events (vm, &event_data);
990 now = clib_cpu_time_now ();
993 case ~0: /* no events => timeout */
994 /* nothing to do here */
996 case BFD_EVENT_RESCHEDULE:
997 /* nothing to do here - reschedule is done automatically after
998 * each event or timeout */
1000 case BFD_EVENT_NEW_SESSION:
1001 if (!pool_is_free_index (bm->sessions, *event_data))
1004 pool_elt_at_index (bm->sessions, *event_data);
1005 bfd_send_periodic (vm, rt, bm, bs, now, 1);
1009 BFD_DBG ("Ignoring event for non-existent session index %u",
1010 (u32) * event_data);
1013 case BFD_EVENT_CONFIG_CHANGED:
1014 if (!pool_is_free_index (bm->sessions, *event_data))
1017 pool_elt_at_index (bm->sessions, *event_data);
1018 bfd_on_config_change (vm, rt, bm, bs, now);
1022 BFD_DBG ("Ignoring event for non-existent session index %u",
1023 (u32) * event_data);
1027 clib_warning ("BUG: event type 0x%wx", event_type);
1030 BFD_DBG ("advancing wheel, now is %lu", now);
1031 BFD_DBG ("timing_wheel_advance (%p, %lu, %p, 0);", &bm->wheel, now,
1033 expired = timing_wheel_advance (&bm->wheel, now, expired, 0);
1034 BFD_DBG ("Expired %d elements", vec_len (expired));
1036 vec_foreach (p, expired)
1038 const u32 bs_idx = *p;
1039 if (!pool_is_free_index (bm->sessions, bs_idx))
1041 bfd_session_t *bs = pool_elt_at_index (bm->sessions, bs_idx);
1042 bfd_on_timeout (vm, rt, bm, bs, now);
1043 bfd_set_timer (bm, bs, now, 1);
1048 _vec_len (expired) = 0;
1052 _vec_len (event_data) = 0;
1060 * bfd process node declaration
1063 VLIB_REGISTER_NODE (bfd_process_node, static) = {
1064 .function = bfd_process,
1065 .type = VLIB_NODE_TYPE_PROCESS,
1066 .name = "bfd-process",
1072 static clib_error_t *
1073 bfd_sw_interface_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
1075 // bfd_main_t *bm = &bfd_main;
1076 // vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
1077 if (!(flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
1084 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (bfd_sw_interface_up_down);
1086 static clib_error_t *
1087 bfd_hw_interface_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
1089 // bfd_main_t *bm = &bfd_main;
1090 if (flags & VNET_HW_INTERFACE_FLAG_LINK_UP)
1097 VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION (bfd_hw_interface_up_down);
1102 static clib_error_t *
1103 bfd_main_init (vlib_main_t * vm)
1106 setbuf (stdout, NULL);
1108 bfd_main_t *bm = &bfd_main;
1109 bm->random_seed = random_default_seed ();
1111 bm->vnet_main = vnet_get_main ();
1112 memset (&bm->wheel, 0, sizeof (bm->wheel));
1113 bm->cpu_cps = vm->clib_time.clocks_per_second;
1114 BFD_DBG ("cps is %.2f", bm->cpu_cps);
1115 bm->default_desired_min_tx_clocks =
1116 bfd_usec_to_clocks (bm, BFD_DEFAULT_DESIRED_MIN_TX_USEC);
1117 bm->min_required_min_rx_while_echo_clocks =
1118 bfd_usec_to_clocks (bm, BFD_REQUIRED_MIN_RX_USEC_WHILE_ECHO);
1119 const u64 now = clib_cpu_time_now ();
1120 timing_wheel_init (&bm->wheel, now, bm->cpu_cps);
1121 bm->wheel_inaccuracy = 2 << bm->wheel.log2_clocks_per_bin;
1123 vlib_node_t *node = NULL;
1125 node = vlib_get_node_by_name (vm, (u8 *)n); \
1126 bfd_node_index_by_transport[BFD_TRANSPORT_##t] = node->index; \
1127 BFD_DBG ("node '%s' has index %u", n, node->index);
1128 foreach_bfd_transport (F);
1133 VLIB_INIT_FUNCTION (bfd_main_init);
1136 bfd_get_session (bfd_main_t * bm, bfd_transport_e t)
1138 bfd_session_t *result;
1139 pool_get (bm->sessions, result);
1140 memset (result, 0, sizeof (*result));
1141 result->bs_idx = result - bm->sessions;
1142 result->transport = t;
1143 const unsigned limit = 1000;
1144 unsigned counter = 0;
1147 result->local_discr = random_u32 (&bm->random_seed);
1148 if (counter > limit)
1150 clib_warning ("Couldn't allocate unused session discriminator even "
1151 "after %u tries!", limit);
1152 pool_put (bm->sessions, result);
1157 while (hash_get (bm->session_by_disc, result->local_discr));
1158 bfd_set_defaults (bm, result);
1159 hash_set (bm->session_by_disc, result->local_discr, result->bs_idx);
1164 bfd_put_session (bfd_main_t * bm, bfd_session_t * bs)
1166 if (bs->auth.curr_key)
1168 --bs->auth.curr_key->use_count;
1170 if (bs->auth.next_key)
1172 --bs->auth.next_key->use_count;
1174 hash_unset (bm->session_by_disc, bs->local_discr);
1175 pool_put (bm->sessions, bs);
1179 bfd_find_session_by_idx (bfd_main_t * bm, uword bs_idx)
1181 if (!pool_is_free_index (bm->sessions, bs_idx))
1183 return pool_elt_at_index (bm->sessions, bs_idx);
1189 bfd_find_session_by_disc (bfd_main_t * bm, u32 disc)
1191 uword *p = hash_get (bfd_main.session_by_disc, disc);
1194 return pool_elt_at_index (bfd_main.sessions, *p);
1200 * @brief verify bfd packet - common checks
1204 * @return 1 if bfd packet is valid
1207 bfd_verify_pkt_common (const bfd_pkt_t * pkt)
1209 if (1 != bfd_pkt_get_version (pkt))
1211 BFD_ERR ("BFD verification failed - unexpected version: '%d'",
1212 bfd_pkt_get_version (pkt));
1215 if (pkt->head.length < sizeof (bfd_pkt_t) ||
1216 (bfd_pkt_get_auth_present (pkt) &&
1217 pkt->head.length < sizeof (bfd_pkt_with_common_auth_t)))
1219 BFD_ERR ("BFD verification failed - unexpected length: '%d' (auth "
1221 pkt->head.length, bfd_pkt_get_auth_present (pkt));
1224 if (!pkt->head.detect_mult)
1226 BFD_ERR ("BFD verification failed - unexpected detect-mult: '%d'",
1227 pkt->head.detect_mult);
1230 if (bfd_pkt_get_multipoint (pkt))
1232 BFD_ERR ("BFD verification failed - unexpected multipoint: '%d'",
1233 bfd_pkt_get_multipoint (pkt));
1238 BFD_ERR ("BFD verification failed - unexpected my-disc: '%d'",
1242 if (!pkt->your_disc)
1244 const u8 pkt_state = bfd_pkt_get_state (pkt);
1245 if (pkt_state != BFD_STATE_down && pkt_state != BFD_STATE_admin_down)
1247 BFD_ERR ("BFD verification failed - unexpected state: '%s' "
1248 "(your-disc is zero)", bfd_state_string (pkt_state));
1256 bfd_session_switch_auth_to_next (bfd_session_t * bs)
1258 BFD_DBG ("Switching authentication key from %U to %U for bs_idx=%u",
1259 format_bfd_auth_key, bs->auth.curr_key, format_bfd_auth_key,
1260 bs->auth.next_key, bs->bs_idx);
1261 bs->auth.is_delayed = 0;
1262 if (bs->auth.curr_key)
1264 --bs->auth.curr_key->use_count;
1266 bs->auth.curr_key = bs->auth.next_key;
1267 bs->auth.next_key = NULL;
1268 bs->auth.curr_bfd_key_id = bs->auth.next_bfd_key_id;
1272 bfd_auth_type_is_meticulous (bfd_auth_type_e auth_type)
1274 if (BFD_AUTH_TYPE_meticulous_keyed_md5 == auth_type ||
1275 BFD_AUTH_TYPE_meticulous_keyed_sha1 == auth_type)
1283 bfd_verify_pkt_auth_seq_num (bfd_session_t * bs,
1284 u32 received_seq_num, int is_meticulous)
1289 * This variable MUST be set to zero after no packets have been
1290 * received on this session for at least twice the Detection Time.
1292 u64 now = clib_cpu_time_now ();
1293 if (now - bs->last_rx_clocks > bs->detection_time_clocks * 2)
1295 BFD_DBG ("BFD peer unresponsive for %lu clocks, which is > 2 * "
1296 "detection_time=%u clocks, resetting remote_seq_number_known "
1298 now - bs->last_rx_clocks, bs->detection_time_clocks * 2);
1299 bs->auth.remote_seq_number_known = 0;
1301 if (bs->auth.remote_seq_number_known)
1303 /* remote sequence number is known, verify its validity */
1304 const u32 max_u32 = 0xffffffff;
1305 /* the calculation might wrap, account for the special case... */
1306 if (bs->auth.remote_seq_number > max_u32 - 3 * bs->local_detect_mult)
1312 * |----------+----------------------------+-----------|
1314 * | remote_seq_num------+
1316 * +-----(remote_seq_num + 3*detect_mult) % * 0xffffffff
1318 * x + y + z = 0xffffffff
1319 * x + z = 3 * detect_mult
1321 const u32 z = max_u32 - bs->auth.remote_seq_number;
1322 const u32 x = 3 * bs->local_detect_mult - z;
1323 if (received_seq_num > x &&
1324 received_seq_num < bs->auth.remote_seq_number + is_meticulous)
1327 ("Recvd sequence number=%u out of ranges <0, %u>, <%u, %u>",
1328 received_seq_num, x,
1329 bs->auth.remote_seq_number + is_meticulous, max_u32);
1336 const u32 min = bs->auth.remote_seq_number + is_meticulous;
1338 bs->auth.remote_seq_number + 3 * bs->local_detect_mult;
1339 if (received_seq_num < min || received_seq_num > max)
1341 BFD_ERR ("Recvd sequence number=%u out of range <%u, %u>",
1342 received_seq_num, min, max);
1351 bfd_verify_pkt_auth_key_sha1 (const bfd_pkt_t * pkt, u32 pkt_size,
1352 bfd_session_t * bs, u8 bfd_key_id,
1353 bfd_auth_key_t * auth_key)
1355 ASSERT (auth_key->auth_type == BFD_AUTH_TYPE_keyed_sha1 ||
1356 auth_key->auth_type == BFD_AUTH_TYPE_meticulous_keyed_sha1);
1358 u8 result[SHA_DIGEST_LENGTH];
1359 bfd_pkt_with_common_auth_t *with_common = (void *) pkt;
1360 if (pkt_size < sizeof (*with_common))
1362 BFD_ERR ("Packet size too small to hold authentication common header");
1365 if (with_common->common_auth.type != auth_key->auth_type)
1367 BFD_ERR ("BFD auth type mismatch, packet auth=%d:%s doesn't match "
1368 "in-use auth=%d:%s",
1369 with_common->common_auth.type,
1370 bfd_auth_type_str (with_common->common_auth.type),
1371 auth_key->auth_type, bfd_auth_type_str (auth_key->auth_type));
1374 bfd_pkt_with_sha1_auth_t *with_sha1 = (void *) pkt;
1375 if (pkt_size < sizeof (*with_sha1) ||
1376 with_sha1->sha1_auth.type_len.len < sizeof (with_sha1->sha1_auth))
1379 ("BFD size mismatch, payload size=%u, expected=%u, auth_len=%u, "
1380 "expected=%u", pkt_size, sizeof (*with_sha1),
1381 with_sha1->sha1_auth.type_len.len, sizeof (with_sha1->sha1_auth));
1384 if (with_sha1->sha1_auth.key_id != bfd_key_id)
1387 ("BFD key ID mismatch, packet key ID=%u doesn't match key ID=%u%s",
1388 with_sha1->sha1_auth.key_id, bfd_key_id,
1390 auth.is_delayed ? " (but a delayed auth change is scheduled)" : "");
1394 if (!SHA1_Init (&ctx))
1396 BFD_ERR ("SHA1_Init failed");
1399 /* ignore last 20 bytes - use the actual key data instead pkt data */
1400 if (!SHA1_Update (&ctx, with_sha1,
1401 sizeof (*with_sha1) - sizeof (with_sha1->sha1_auth.hash)))
1403 BFD_ERR ("SHA1_Update failed");
1406 if (!SHA1_Update (&ctx, auth_key->key, sizeof (auth_key->key)))
1408 BFD_ERR ("SHA1_Update failed");
1411 if (!SHA1_Final (result, &ctx))
1413 BFD_ERR ("SHA1_Final failed");
1416 if (0 == memcmp (result, with_sha1->sha1_auth.hash, SHA_DIGEST_LENGTH))
1420 BFD_ERR ("SHA1 hash: %U doesn't match the expected value: %U",
1421 format_hex_bytes, with_sha1->sha1_auth.hash, SHA_DIGEST_LENGTH,
1422 format_hex_bytes, result, SHA_DIGEST_LENGTH);
1427 bfd_verify_pkt_auth_key (const bfd_pkt_t * pkt, u32 pkt_size,
1428 bfd_session_t * bs, u8 bfd_key_id,
1429 bfd_auth_key_t * auth_key)
1431 switch (auth_key->auth_type)
1433 case BFD_AUTH_TYPE_reserved:
1434 clib_warning ("Internal error, unexpected auth_type=%d:%s",
1435 auth_key->auth_type,
1436 bfd_auth_type_str (auth_key->auth_type));
1438 case BFD_AUTH_TYPE_simple_password:
1440 ("Internal error, not implemented, unexpected auth_type=%d:%s",
1441 auth_key->auth_type, bfd_auth_type_str (auth_key->auth_type));
1443 case BFD_AUTH_TYPE_keyed_md5:
1445 case BFD_AUTH_TYPE_meticulous_keyed_md5:
1447 ("Internal error, not implemented, unexpected auth_type=%d:%s",
1448 auth_key->auth_type, bfd_auth_type_str (auth_key->auth_type));
1450 case BFD_AUTH_TYPE_keyed_sha1:
1452 case BFD_AUTH_TYPE_meticulous_keyed_sha1:
1456 const u32 seq_num = clib_net_to_host_u32 (((bfd_pkt_with_sha1_auth_t
1459 return bfd_verify_pkt_auth_seq_num (bs, seq_num,
1460 bfd_auth_type_is_meticulous
1461 (auth_key->auth_type))
1462 && bfd_verify_pkt_auth_key_sha1 (pkt, pkt_size, bs, bfd_key_id,
1468 ("Internal error, attempt to use SHA1 without SSL support");
1476 * @brief verify bfd packet - authentication
1480 * @return 1 if bfd packet is valid
1483 bfd_verify_pkt_auth (const bfd_pkt_t * pkt, u16 pkt_size, bfd_session_t * bs)
1485 if (bfd_pkt_get_auth_present (pkt))
1487 /* authentication present in packet */
1488 if (!bs->auth.curr_key)
1490 /* currently not using authentication - can we turn it on? */
1491 if (bs->auth.is_delayed && bs->auth.next_key)
1493 /* yes, switch is scheduled - make sure the auth is valid */
1494 if (bfd_verify_pkt_auth_key (pkt, pkt_size, bs,
1495 bs->auth.next_bfd_key_id,
1498 /* auth matches next key, do the switch, packet is valid */
1499 bfd_session_switch_auth_to_next (bs);
1506 /* yes, using authentication, verify the key */
1507 if (bfd_verify_pkt_auth_key (pkt, pkt_size, bs,
1508 bs->auth.curr_bfd_key_id,
1511 /* verification passed, packet is valid */
1516 /* verification failed - but maybe we need to switch key */
1517 if (bs->auth.is_delayed && bs->auth.next_key)
1519 /* delayed switch present, verify if that key works */
1520 if (bfd_verify_pkt_auth_key (pkt, pkt_size, bs,
1521 bs->auth.next_bfd_key_id,
1524 /* auth matches next key, switch key, packet is valid */
1525 bfd_session_switch_auth_to_next (bs);
1534 /* authentication in packet not present */
1535 if (pkt_size > sizeof (*pkt))
1537 BFD_ERR ("BFD verification failed - unexpected packet size '%d' "
1538 "(auth not present)", pkt_size);
1541 if (bs->auth.curr_key)
1543 /* currently authenticating - could we turn it off? */
1544 if (bs->auth.is_delayed && !bs->auth.next_key)
1546 /* yes, delayed switch to NULL key is scheduled */
1547 bfd_session_switch_auth_to_next (bs);
1553 /* no auth in packet, no auth in use - packet is valid */
1561 bfd_consume_pkt (bfd_main_t * bm, const bfd_pkt_t * pkt, u32 bs_idx)
1563 bfd_session_t *bs = bfd_find_session_by_idx (bm, bs_idx);
1568 BFD_DBG ("Scanning bfd packet, bs_idx=%d", bs->bs_idx);
1569 bs->remote_discr = pkt->my_disc;
1570 bs->remote_state = bfd_pkt_get_state (pkt);
1571 bs->remote_demand = bfd_pkt_get_demand (pkt);
1572 u64 now = clib_cpu_time_now ();
1573 bs->last_rx_clocks = now;
1574 if (bfd_pkt_get_auth_present (pkt))
1576 bfd_auth_type_e auth_type =
1577 ((bfd_pkt_with_common_auth_t *) (pkt))->common_auth.type;
1580 case BFD_AUTH_TYPE_reserved:
1582 case BFD_AUTH_TYPE_simple_password:
1584 case BFD_AUTH_TYPE_keyed_md5:
1586 case BFD_AUTH_TYPE_meticulous_keyed_md5:
1587 clib_warning ("Internal error, unexpected auth_type=%d:%s",
1588 auth_type, bfd_auth_type_str (auth_type));
1590 case BFD_AUTH_TYPE_keyed_sha1:
1592 case BFD_AUTH_TYPE_meticulous_keyed_sha1:
1595 bfd_pkt_with_sha1_auth_t *with_sha1 =
1596 (bfd_pkt_with_sha1_auth_t *) pkt;
1597 bs->auth.remote_seq_number =
1598 clib_net_to_host_u32 (with_sha1->sha1_auth.seq_num);
1599 bs->auth.remote_seq_number_known = 1;
1600 BFD_DBG ("Received sequence number %u",
1601 bs->auth.remote_seq_number);
1606 bs->remote_desired_min_tx_clocks =
1607 bfd_usec_to_clocks (bm, clib_net_to_host_u32 (pkt->des_min_tx));
1608 bs->remote_detect_mult = pkt->head.detect_mult;
1609 bfd_set_remote_required_min_rx (bm, bs, now,
1610 clib_net_to_host_u32 (pkt->req_min_rx));
1611 bfd_set_remote_required_min_echo_rx (bm, bs, now,
1612 clib_net_to_host_u32
1613 (pkt->req_min_echo_rx));
1616 if (bfd_pkt_get_final (pkt))
1618 if (BFD_POLL_IN_PROGRESS == bs->poll_state)
1620 BFD_DBG ("Poll sequence terminated, bs_idx=%u", bs->bs_idx);
1621 bfd_set_poll_state (bs, BFD_POLL_NOT_NEEDED);
1622 if (BFD_STATE_up == bs->local_state)
1624 bfd_set_effective_required_min_rx (bm, bs, now,
1625 clib_max (bs->echo *
1626 bm->min_required_min_rx_while_echo_clocks,
1627 bs->config_required_min_rx_clocks));
1630 else if (BFD_POLL_IN_PROGRESS_AND_QUEUED == bs->poll_state)
1633 * next poll sequence must be delayed by at least the round trip
1634 * time, so calculate that here
1636 BFD_DBG ("Next poll sequence can commence in " BFD_CLK_FMT,
1638 bs->poll_state_start_or_timeout_clocks));
1639 bs->poll_state_start_or_timeout_clocks =
1640 now + (now - bs->poll_state_start_or_timeout_clocks);
1642 ("Poll sequence terminated, but another is needed, bs_idx=%u",
1644 bfd_set_poll_state (bs, BFD_POLL_NEEDED);
1647 bfd_calc_next_tx (bm, bs, now);
1648 bfd_set_timer (bm, bs, now, 0);
1649 if (BFD_STATE_admin_down == bs->local_state)
1651 BFD_DBG ("Session is admin-down, ignoring packet, bs_idx=%u",
1655 if (BFD_STATE_admin_down == bs->remote_state)
1657 bfd_set_diag (bs, BFD_DIAG_CODE_neighbor_sig_down);
1658 bfd_set_state (bm, bs, BFD_STATE_down, 0);
1660 else if (BFD_STATE_down == bs->local_state)
1662 if (BFD_STATE_down == bs->remote_state)
1664 bfd_set_state (bm, bs, BFD_STATE_init, 0);
1666 else if (BFD_STATE_init == bs->remote_state)
1668 bfd_set_state (bm, bs, BFD_STATE_up, 0);
1671 else if (BFD_STATE_init == bs->local_state)
1673 if (BFD_STATE_up == bs->remote_state ||
1674 BFD_STATE_init == bs->remote_state)
1676 bfd_set_state (bm, bs, BFD_STATE_up, 0);
1679 else /* BFD_STATE_up == bs->local_state */
1681 if (BFD_STATE_down == bs->remote_state)
1683 bfd_set_diag (bs, BFD_DIAG_CODE_neighbor_sig_down);
1684 bfd_set_state (bm, bs, BFD_STATE_down, 0);
1690 bfd_consume_echo_pkt (bfd_main_t * bm, vlib_buffer_t * b)
1692 bfd_echo_pkt_t *pkt = NULL;
1693 if (b->current_length != sizeof (*pkt))
1697 pkt = vlib_buffer_get_current (b);
1698 bfd_session_t *bs = bfd_find_session_by_disc (bm, pkt->discriminator);
1703 BFD_DBG ("Scanning bfd echo packet, bs_idx=%d", bs->bs_idx);
1705 bfd_calc_echo_checksum (bs->local_discr, pkt->expire_time_clocks,
1707 if (checksum != pkt->checksum)
1709 BFD_DBG ("Invalid echo packet, checksum mismatch");
1712 u64 now = clib_cpu_time_now ();
1713 if (pkt->expire_time_clocks < now)
1715 BFD_DBG ("Stale packet received, expire time %lu < now %lu",
1716 pkt->expire_time_clocks, now);
1720 bs->echo_last_rx_clocks = now;
1726 format_bfd_session (u8 * s, va_list * args)
1728 const bfd_session_t *bs = va_arg (*args, bfd_session_t *);
1729 uword indent = format_get_indent (s);
1730 s = format (s, "bs_idx=%u local-state=%s remote-state=%s\n"
1731 "%Ulocal-discriminator=%u remote-discriminator=%u\n"
1732 "%Ulocal-diag=%s echo-active=%s\n"
1733 "%Udesired-min-tx=%u required-min-rx=%u\n"
1734 "%Urequired-min-echo-rx=%u detect-mult=%u\n"
1735 "%Uremote-min-rx=%u remote-min-echo-rx=%u\n"
1736 "%Uremote-demand=%s poll-state=%s\n"
1737 "%Uauth: local-seq-num=%u remote-seq-num=%u\n"
1738 "%U is-delayed=%s\n"
1741 bs->bs_idx, bfd_state_string (bs->local_state),
1742 bfd_state_string (bs->remote_state), format_white_space, indent,
1743 bs->local_discr, bs->remote_discr, format_white_space, indent,
1744 bfd_diag_code_string (bs->local_diag),
1745 (bs->echo ? "yes" : "no"), format_white_space, indent,
1746 bs->config_desired_min_tx_usec, bs->config_required_min_rx_usec,
1747 format_white_space, indent, 1, bs->local_detect_mult,
1748 format_white_space, indent, bs->remote_min_rx_usec,
1749 bs->remote_min_echo_rx_usec, format_white_space, indent,
1750 (bs->remote_demand ? "yes" : "no"),
1751 bfd_poll_state_string (bs->poll_state), format_white_space,
1752 indent, bs->auth.local_seq_number, bs->auth.remote_seq_number,
1753 format_white_space, indent,
1754 (bs->auth.is_delayed ? "yes" : "no"), format_white_space,
1755 indent, format_bfd_auth_key, bs->auth.curr_key,
1756 format_white_space, indent, format_bfd_auth_key,
1762 bfd_auth_type_supported (bfd_auth_type_e auth_type)
1764 if (auth_type == BFD_AUTH_TYPE_keyed_sha1 ||
1765 auth_type == BFD_AUTH_TYPE_meticulous_keyed_sha1)
1773 bfd_auth_activate (bfd_session_t * bs, u32 conf_key_id,
1774 u8 bfd_key_id, u8 is_delayed)
1776 bfd_main_t *bm = &bfd_main;
1777 const uword *key_idx_p =
1778 hash_get (bm->auth_key_by_conf_key_id, conf_key_id);
1781 clib_warning ("Authentication key with config ID %u doesn't exist)",
1783 return VNET_API_ERROR_BFD_ENOENT;
1785 const uword key_idx = *key_idx_p;
1786 bfd_auth_key_t *key = pool_elt_at_index (bm->auth_keys, key_idx);
1789 if (bs->auth.next_key == key)
1791 /* already using this key, no changes required */
1794 bs->auth.next_key = key;
1795 bs->auth.next_bfd_key_id = bfd_key_id;
1796 bs->auth.is_delayed = 1;
1800 if (bs->auth.curr_key == key)
1802 /* already using this key, no changes required */
1805 if (bs->auth.curr_key)
1807 --bs->auth.curr_key->use_count;
1809 bs->auth.curr_key = key;
1810 bs->auth.curr_bfd_key_id = bfd_key_id;
1811 bs->auth.is_delayed = 0;
1814 BFD_DBG ("\nSession auth modified: %U", format_bfd_session, bs);
1819 bfd_auth_deactivate (bfd_session_t * bs, u8 is_delayed)
1824 /* not delayed - deactivate the current key right now */
1825 if (bs->auth.curr_key)
1827 --bs->auth.curr_key->use_count;
1828 bs->auth.curr_key = NULL;
1830 bs->auth.is_delayed = 0;
1834 /* delayed - mark as so */
1835 bs->auth.is_delayed = 1;
1838 * clear the next key unconditionally - either the auth change is not delayed
1839 * in which case the caller expects the session to not use authentication
1840 * from this point forward, or it is delayed, in which case the next_key
1841 * needs to be set to NULL to make it so in the future
1843 if (bs->auth.next_key)
1845 --bs->auth.next_key->use_count;
1846 bs->auth.next_key = NULL;
1848 BFD_DBG ("\nSession auth modified: %U", format_bfd_session, bs);
1851 clib_warning ("SSL missing, cannot deactivate BFD authentication");
1852 return VNET_API_ERROR_BFD_NOTSUPP;
1857 bfd_session_set_params (bfd_main_t * bm, bfd_session_t * bs,
1858 u32 desired_min_tx_usec,
1859 u32 required_min_rx_usec, u8 detect_mult)
1861 if (bs->local_detect_mult != detect_mult ||
1862 bs->config_desired_min_tx_usec != desired_min_tx_usec ||
1863 bs->config_required_min_rx_usec != required_min_rx_usec)
1865 BFD_DBG ("\nChanging session params: %U", format_bfd_session, bs);
1866 switch (bs->poll_state)
1868 case BFD_POLL_NOT_NEEDED:
1869 if (BFD_STATE_up == bs->local_state ||
1870 BFD_STATE_init == bs->local_state)
1872 /* poll sequence is not needed for detect multiplier change */
1873 if (bs->config_desired_min_tx_usec != desired_min_tx_usec ||
1874 bs->config_required_min_rx_usec != required_min_rx_usec)
1876 bfd_set_poll_state (bs, BFD_POLL_NEEDED);
1880 case BFD_POLL_NEEDED:
1881 case BFD_POLL_IN_PROGRESS_AND_QUEUED:
1883 * nothing to do - will be handled in the future poll which is
1884 * already scheduled for execution
1887 case BFD_POLL_IN_PROGRESS:
1888 /* poll sequence is not needed for detect multiplier change */
1889 if (bs->config_desired_min_tx_usec != desired_min_tx_usec ||
1890 bs->config_required_min_rx_usec != required_min_rx_usec)
1892 BFD_DBG ("Poll in progress, queueing extra poll, bs_idx=%u",
1894 bfd_set_poll_state (bs, BFD_POLL_IN_PROGRESS_AND_QUEUED);
1898 bs->local_detect_mult = detect_mult;
1899 bs->config_desired_min_tx_usec = desired_min_tx_usec;
1900 bs->config_desired_min_tx_clocks =
1901 bfd_usec_to_clocks (bm, desired_min_tx_usec);
1902 bs->config_required_min_rx_usec = required_min_rx_usec;
1903 bs->config_required_min_rx_clocks =
1904 bfd_usec_to_clocks (bm, required_min_rx_usec);
1905 BFD_DBG ("\nChanged session params: %U", format_bfd_session, bs);
1907 vlib_process_signal_event (bm->vlib_main, bm->bfd_process_node_index,
1908 BFD_EVENT_CONFIG_CHANGED, bs->bs_idx);
1912 BFD_DBG ("Ignore parameter change - no change, bs_idx=%u", bs->bs_idx);
1917 bfd_main_t bfd_main;
1920 * fd.io coding-style-patch-verification: ON
1923 * eval: (c-set-style "gnu")