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;
67 format_bfd_auth_key (u8 * s, va_list * args)
69 const bfd_auth_key_t *key = va_arg (*args, bfd_auth_key_t *);
72 s = format (s, "{auth-type=%u:%s, conf-key-id=%u, use-count=%u}, ",
73 key->auth_type, bfd_auth_type_str (key->auth_type),
74 key->conf_key_id, key->use_count);
78 s = format (s, "{none}");
84 * We actually send all bfd pkts to the "error" node after scanning
85 * them, so the graph node has only one next-index. The "error-drop"
86 * node automatically bumps our per-node packet counters for us.
90 BFD_INPUT_NEXT_NORMAL,
94 static void bfd_on_state_change (bfd_main_t * bm, bfd_session_t * bs, u64 now,
98 bfd_set_defaults (bfd_main_t * bm, bfd_session_t * bs)
100 bs->local_state = BFD_STATE_down;
101 bs->local_diag = BFD_DIAG_CODE_no_diag;
102 bs->remote_state = BFD_STATE_down;
103 bs->remote_discr = 0;
104 bs->config_desired_min_tx_usec = BFD_DEFAULT_DESIRED_MIN_TX_USEC;
105 bs->config_desired_min_tx_clocks = bm->default_desired_min_tx_clocks;
106 bs->effective_desired_min_tx_clocks = bm->default_desired_min_tx_clocks;
107 bs->remote_min_rx_usec = 1;
108 bs->remote_min_rx_clocks = bfd_usec_to_clocks (bm, bs->remote_min_rx_usec);
109 bs->remote_min_echo_rx_usec = 0;
110 bs->remote_min_echo_rx_clocks = 0;
111 bs->remote_demand = 0;
112 bs->auth.remote_seq_number = 0;
113 bs->auth.remote_seq_number_known = 0;
114 bs->auth.local_seq_number = random_u32 (&bm->random_seed);
115 bs->echo_secret = random_u32 (&bm->random_seed);
119 bfd_set_diag (bfd_session_t * bs, bfd_diag_code_e code)
121 if (bs->local_diag != code)
123 BFD_DBG ("set local_diag, bs_idx=%d: '%d:%s'", bs->bs_idx, code,
124 bfd_diag_code_string (code));
125 bs->local_diag = code;
130 bfd_set_state (bfd_main_t * bm, bfd_session_t * bs,
131 bfd_state_e new_state, int handling_wakeup)
133 if (bs->local_state != new_state)
135 BFD_DBG ("Change state, bs_idx=%d: %s->%s", bs->bs_idx,
136 bfd_state_string (bs->local_state),
137 bfd_state_string (new_state));
138 bs->local_state = new_state;
139 bfd_on_state_change (bm, bs, clib_cpu_time_now (), handling_wakeup);
144 bfd_poll_state_string (bfd_poll_state_e state)
150 return "BFD_POLL_" #x;
151 foreach_bfd_poll_state (F)
158 bfd_set_poll_state (bfd_session_t * bs, bfd_poll_state_e state)
160 if (bs->poll_state != state)
162 BFD_DBG ("Setting poll state=%s, bs_idx=%u",
163 bfd_poll_state_string (state), bs->bs_idx);
164 bs->poll_state = state;
169 bfd_recalc_tx_interval (bfd_main_t * bm, bfd_session_t * bs)
171 bs->transmit_interval_clocks =
172 clib_max (bs->effective_desired_min_tx_clocks, bs->remote_min_rx_clocks);
173 BFD_DBG ("Recalculated transmit interval " BFD_CLK_FMT,
174 BFD_CLK_PRN (bs->transmit_interval_clocks));
178 bfd_recalc_echo_tx_interval (bfd_main_t * bm, bfd_session_t * bs)
180 bs->echo_transmit_interval_clocks =
181 clib_max (bs->effective_desired_min_tx_clocks,
182 bs->remote_min_echo_rx_clocks);
183 BFD_DBG ("Recalculated echo transmit interval " BFD_CLK_FMT,
184 BFD_CLK_PRN (bs->echo_transmit_interval_clocks));
188 bfd_calc_next_tx (bfd_main_t * bm, bfd_session_t * bs, u64 now)
190 if (bs->local_detect_mult > 1)
192 /* common case - 75-100% of transmit interval */
193 bs->tx_timeout_clocks = bs->last_tx_clocks +
194 (1 - .25 * (random_f64 (&bm->random_seed))) *
195 bs->transmit_interval_clocks;
196 if (bs->tx_timeout_clocks < now)
199 * the timeout is in the past, which means that either remote
200 * demand mode was set or performance/clock issues ...
202 BFD_DBG ("Missed %lu transmit events (now is %lu, calc "
203 "tx_timeout is %lu)",
204 (now - bs->tx_timeout_clocks) /
205 bs->transmit_interval_clocks, now, bs->tx_timeout_clocks);
206 bs->tx_timeout_clocks = now;
211 /* special case - 75-90% of transmit interval */
212 bs->tx_timeout_clocks = bs->last_tx_clocks +
213 (.9 - .15 * (random_f64 (&bm->random_seed))) *
214 bs->transmit_interval_clocks;
215 if (bs->tx_timeout_clocks < now)
218 * the timeout is in the past, which means that either remote
219 * demand mode was set or performance/clock issues ...
221 BFD_DBG ("Missed %lu transmit events (now is %lu, calc "
222 "tx_timeout is %lu)",
223 (now - bs->tx_timeout_clocks) /
224 bs->transmit_interval_clocks, now, bs->tx_timeout_clocks);
225 bs->tx_timeout_clocks = now;
228 if (bs->tx_timeout_clocks)
230 BFD_DBG ("Next transmit in %lu clocks/%.02fs@%lu",
231 bs->tx_timeout_clocks - now,
232 (bs->tx_timeout_clocks - now) / bm->cpu_cps,
233 bs->tx_timeout_clocks);
238 bfd_calc_next_echo_tx (bfd_main_t * bm, bfd_session_t * bs, u64 now)
240 bs->echo_tx_timeout_clocks =
241 bs->echo_last_tx_clocks + bs->echo_transmit_interval_clocks;
242 if (bs->echo_tx_timeout_clocks < now)
244 /* huh, we've missed it already, transmit now */
245 BFD_DBG ("Missed %lu echo transmit events (now is %lu, calc tx_timeout "
247 (now - bs->echo_tx_timeout_clocks) /
248 bs->echo_transmit_interval_clocks,
249 now, bs->echo_tx_timeout_clocks);
250 bs->echo_tx_timeout_clocks = now;
252 BFD_DBG ("Next echo transmit in %lu clocks/%.02fs@%lu",
253 bs->echo_tx_timeout_clocks - now,
254 (bs->echo_tx_timeout_clocks - now) / bm->cpu_cps,
255 bs->echo_tx_timeout_clocks);
259 bfd_recalc_detection_time (bfd_main_t * bm, bfd_session_t * bs)
261 if (bs->local_state == BFD_STATE_init || bs->local_state == BFD_STATE_up)
263 bs->detection_time_clocks =
264 bs->remote_detect_mult *
265 clib_max (bs->effective_required_min_rx_clocks,
266 bs->remote_desired_min_tx_clocks);
267 BFD_DBG ("Recalculated detection time %lu clocks/%.2fs",
268 bs->detection_time_clocks,
269 bs->detection_time_clocks / bm->cpu_cps);
274 bfd_set_timer (bfd_main_t * bm, bfd_session_t * bs, u64 now,
280 if (BFD_STATE_up == bs->local_state)
282 rx_timeout = bs->last_rx_clocks + bs->detection_time_clocks;
284 if (BFD_STATE_up != bs->local_state ||
285 (!bs->remote_demand && bs->remote_min_rx_usec) ||
286 BFD_POLL_NOT_NEEDED != bs->poll_state)
288 tx_timeout = bs->tx_timeout_clocks;
290 if (tx_timeout && rx_timeout)
292 next = clib_min (tx_timeout, rx_timeout);
302 if (bs->echo && next > bs->echo_tx_timeout_clocks)
304 next = bs->echo_tx_timeout_clocks;
306 BFD_DBG ("bs_idx=%u, tx_timeout=%lu, echo_tx_timeout=%lu, rx_timeout=%lu, "
308 bs->bs_idx, tx_timeout, bs->echo_tx_timeout_clocks, rx_timeout,
310 ? "tx" : (next == bs->echo_tx_timeout_clocks ? "echo tx" : "rx"));
311 /* sometimes the wheel expires an event a bit sooner than requested, account
313 if (next && (now + bm->wheel_inaccuracy > bs->wheel_time_clocks ||
314 next < bs->wheel_time_clocks || !bs->wheel_time_clocks))
316 bs->wheel_time_clocks = next;
317 BFD_DBG ("timing_wheel_insert(%p, %lu (%ld clocks/%.2fs in the "
319 &bm->wheel, bs->wheel_time_clocks,
320 (i64) bs->wheel_time_clocks - clib_cpu_time_now (),
321 (i64) (bs->wheel_time_clocks - clib_cpu_time_now ()) /
322 bm->cpu_cps, bs->bs_idx);
323 timing_wheel_insert (&bm->wheel, bs->wheel_time_clocks, bs->bs_idx);
324 if (!handling_wakeup)
326 vlib_process_signal_event (bm->vlib_main,
327 bm->bfd_process_node_index,
328 BFD_EVENT_RESCHEDULE, bs->bs_idx);
334 bfd_set_effective_desired_min_tx (bfd_main_t * bm,
335 bfd_session_t * bs, u64 now,
336 u64 desired_min_tx_clocks)
338 bs->effective_desired_min_tx_clocks = desired_min_tx_clocks;
339 BFD_DBG ("Set effective desired min tx to " BFD_CLK_FMT,
340 BFD_CLK_PRN (bs->effective_desired_min_tx_clocks));
341 bfd_recalc_detection_time (bm, bs);
342 bfd_recalc_tx_interval (bm, bs);
343 bfd_recalc_echo_tx_interval (bm, bs);
344 bfd_calc_next_tx (bm, bs, now);
348 bfd_set_effective_required_min_rx (bfd_main_t * bm,
350 u64 required_min_rx_clocks)
352 bs->effective_required_min_rx_clocks = required_min_rx_clocks;
353 BFD_DBG ("Set effective required min rx to " BFD_CLK_FMT,
354 BFD_CLK_PRN (bs->effective_required_min_rx_clocks));
355 bfd_recalc_detection_time (bm, bs);
359 bfd_set_remote_required_min_rx (bfd_main_t * bm, bfd_session_t * bs,
360 u64 now, u32 remote_required_min_rx_usec)
362 if (bs->remote_min_rx_usec != remote_required_min_rx_usec)
364 bs->remote_min_rx_usec = remote_required_min_rx_usec;
365 bs->remote_min_rx_clocks =
366 bfd_usec_to_clocks (bm, remote_required_min_rx_usec);
367 BFD_DBG ("Set remote min rx to " BFD_CLK_FMT,
368 BFD_CLK_PRN (bs->remote_min_rx_clocks));
369 bfd_recalc_detection_time (bm, bs);
370 bfd_recalc_tx_interval (bm, bs);
375 bfd_set_remote_required_min_echo_rx (bfd_main_t * bm, bfd_session_t * bs,
377 u32 remote_required_min_echo_rx_usec)
379 if (bs->remote_min_echo_rx_usec != remote_required_min_echo_rx_usec)
381 bs->remote_min_echo_rx_usec = remote_required_min_echo_rx_usec;
382 bs->remote_min_echo_rx_clocks =
383 bfd_usec_to_clocks (bm, bs->remote_min_echo_rx_usec);
384 BFD_DBG ("Set remote min echo rx to " BFD_CLK_FMT,
385 BFD_CLK_PRN (bs->remote_min_echo_rx_clocks));
386 bfd_recalc_echo_tx_interval (bm, bs);
391 bfd_session_start (bfd_main_t * bm, bfd_session_t * bs)
393 BFD_DBG ("\nStarting session: %U", format_bfd_session, bs);
394 bfd_set_effective_required_min_rx (bm, bs,
395 bs->config_required_min_rx_clocks);
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_session_set_flags (bfd_session_t * bs, u8 admin_up_down)
404 bfd_main_t *bm = &bfd_main;
405 u64 now = clib_cpu_time_now ();
408 BFD_DBG ("Session set admin-up, bs-idx=%u", bs->bs_idx);
409 bfd_set_state (bm, bs, BFD_STATE_down, 0);
410 bfd_set_diag (bs, BFD_DIAG_CODE_no_diag);
411 bfd_calc_next_tx (bm, bs, now);
412 bfd_set_timer (bm, bs, now, 0);
416 BFD_DBG ("Session set admin-down, bs-idx=%u", bs->bs_idx);
417 bfd_set_diag (bs, BFD_DIAG_CODE_admin_down);
418 bfd_set_state (bm, bs, BFD_STATE_admin_down, 0);
419 bfd_calc_next_tx (bm, bs, now);
420 bfd_set_timer (bm, bs, now, 0);
425 bfd_input_format_trace (u8 * s, va_list * args)
427 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
428 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
429 const bfd_input_trace_t *t = va_arg (*args, bfd_input_trace_t *);
430 const bfd_pkt_t *pkt = (bfd_pkt_t *) t->data;
431 if (t->len > STRUCT_SIZE_OF (bfd_pkt_t, head))
433 s = format (s, "BFD v%u, diag=%u(%s), state=%u(%s),\n"
434 " flags=(P:%u, F:%u, C:%u, A:%u, D:%u, M:%u), "
435 "detect_mult=%u, length=%u\n",
436 bfd_pkt_get_version (pkt), bfd_pkt_get_diag_code (pkt),
437 bfd_diag_code_string (bfd_pkt_get_diag_code (pkt)),
438 bfd_pkt_get_state (pkt),
439 bfd_state_string (bfd_pkt_get_state (pkt)),
440 bfd_pkt_get_poll (pkt), bfd_pkt_get_final (pkt),
441 bfd_pkt_get_control_plane_independent (pkt),
442 bfd_pkt_get_auth_present (pkt), bfd_pkt_get_demand (pkt),
443 bfd_pkt_get_multipoint (pkt), pkt->head.detect_mult,
445 if (t->len >= sizeof (bfd_pkt_t) &&
446 pkt->head.length >= sizeof (bfd_pkt_t))
448 s = format (s, " my discriminator: %u\n",
449 clib_net_to_host_u32 (pkt->my_disc));
450 s = format (s, " your discriminator: %u\n",
451 clib_net_to_host_u32 (pkt->your_disc));
452 s = format (s, " desired min tx interval: %u\n",
453 clib_net_to_host_u32 (pkt->des_min_tx));
454 s = format (s, " required min rx interval: %u\n",
455 clib_net_to_host_u32 (pkt->req_min_rx));
456 s = format (s, " required min echo rx interval: %u",
457 clib_net_to_host_u32 (pkt->req_min_echo_rx));
459 if (t->len >= sizeof (bfd_pkt_with_common_auth_t) &&
460 pkt->head.length >= sizeof (bfd_pkt_with_common_auth_t) &&
461 bfd_pkt_get_auth_present (pkt))
463 const bfd_pkt_with_common_auth_t *with_auth = (void *) pkt;
464 const bfd_auth_common_t *common = &with_auth->common_auth;
465 s = format (s, "\n auth len: %u\n", common->len);
466 s = format (s, " auth type: %u:%s\n", common->type,
467 bfd_auth_type_str (common->type));
468 if (t->len >= sizeof (bfd_pkt_with_sha1_auth_t) &&
469 pkt->head.length >= sizeof (bfd_pkt_with_sha1_auth_t) &&
470 (BFD_AUTH_TYPE_keyed_sha1 == common->type ||
471 BFD_AUTH_TYPE_meticulous_keyed_sha1 == common->type))
473 const bfd_pkt_with_sha1_auth_t *with_sha1 = (void *) pkt;
474 const bfd_auth_sha1_t *sha1 = &with_sha1->sha1_auth;
475 s = format (s, " seq num: %u\n",
476 clib_net_to_host_u32 (sha1->seq_num));
477 s = format (s, " key id: %u\n", sha1->key_id);
478 s = format (s, " hash: %U", format_hex_bytes, sha1->hash,
479 sizeof (sha1->hash));
484 s = format (s, "\n");
492 bfd_on_state_change (bfd_main_t * bm, bfd_session_t * bs, u64 now,
495 BFD_DBG ("\nState changed: %U", format_bfd_session, bs);
497 switch (bs->local_state)
499 case BFD_STATE_admin_down:
501 bfd_set_effective_desired_min_tx (bm, bs, now,
503 (bs->config_desired_min_tx_clocks,
504 bm->default_desired_min_tx_clocks));
505 bfd_set_effective_required_min_rx (bm, bs,
506 bs->config_required_min_rx_clocks);
507 bfd_set_timer (bm, bs, now, handling_wakeup);
511 bfd_set_effective_desired_min_tx (bm, bs, now,
513 (bs->config_desired_min_tx_clocks,
514 bm->default_desired_min_tx_clocks));
515 bfd_set_effective_required_min_rx (bm, bs,
516 bs->config_required_min_rx_clocks);
517 bfd_set_timer (bm, bs, now, handling_wakeup);
521 bfd_set_effective_desired_min_tx (bm, bs, now,
522 bs->config_desired_min_tx_clocks);
523 bfd_set_timer (bm, bs, now, handling_wakeup);
526 bfd_set_effective_desired_min_tx (bm, bs, now,
527 bs->config_desired_min_tx_clocks);
528 if (BFD_POLL_NOT_NEEDED == bs->poll_state)
530 bfd_set_effective_required_min_rx (bm, bs,
531 bs->config_required_min_rx_clocks);
533 bfd_set_timer (bm, bs, now, handling_wakeup);
539 bfd_on_config_change (vlib_main_t * vm, vlib_node_runtime_t * rt,
540 bfd_main_t * bm, bfd_session_t * bs, u64 now)
543 * if remote demand mode is set and we need to do a poll, set the next
544 * timeout so that the session wakes up immediately
546 if (bs->remote_demand && BFD_POLL_NEEDED == bs->poll_state &&
547 bs->poll_state_start_or_timeout_clocks < now)
549 bs->tx_timeout_clocks = now;
551 bfd_recalc_detection_time (bm, bs);
552 bfd_set_timer (bm, bs, now, 0);
556 bfd_add_transport_layer (vlib_main_t * vm, u32 bi, bfd_session_t * bs)
558 switch (bs->transport)
560 case BFD_TRANSPORT_UDP4:
561 BFD_DBG ("Transport bfd via udp4, bs_idx=%u", bs->bs_idx);
562 bfd_add_udp4_transport (vm, bi, bs, 0 /* is_echo */ );
564 case BFD_TRANSPORT_UDP6:
565 BFD_DBG ("Transport bfd via udp6, bs_idx=%u", bs->bs_idx);
566 bfd_add_udp6_transport (vm, bi, bs, 0 /* is_echo */ );
572 bfd_transport_control_frame (vlib_main_t * vm, u32 bi, bfd_session_t * bs)
574 switch (bs->transport)
576 case BFD_TRANSPORT_UDP4:
577 BFD_DBG ("Transport bfd via udp4, bs_idx=%u", bs->bs_idx);
578 return bfd_transport_udp4 (vm, bi, bs);
580 case BFD_TRANSPORT_UDP6:
581 BFD_DBG ("Transport bfd via udp6, bs_idx=%u", bs->bs_idx);
582 return bfd_transport_udp6 (vm, bi, bs);
589 bfd_echo_add_transport_layer (vlib_main_t * vm, u32 bi, bfd_session_t * bs)
591 switch (bs->transport)
593 case BFD_TRANSPORT_UDP4:
594 BFD_DBG ("Transport bfd echo via udp4, bs_idx=%u", bs->bs_idx);
595 return bfd_add_udp4_transport (vm, bi, bs, 1 /* is_echo */ );
597 case BFD_TRANSPORT_UDP6:
598 BFD_DBG ("Transport bfd echo via udp6, bs_idx=%u", bs->bs_idx);
599 return bfd_add_udp6_transport (vm, bi, bs, 1 /* is_echo */ );
606 bfd_transport_echo (vlib_main_t * vm, u32 bi, bfd_session_t * bs)
608 switch (bs->transport)
610 case BFD_TRANSPORT_UDP4:
611 BFD_DBG ("Transport bfd echo via udp4, bs_idx=%u", bs->bs_idx);
612 return bfd_transport_udp4 (vm, bi, bs);
614 case BFD_TRANSPORT_UDP6:
615 BFD_DBG ("Transport bfd echo via udp6, bs_idx=%u", bs->bs_idx);
616 return bfd_transport_udp6 (vm, bi, bs);
624 bfd_add_sha1_auth_section (vlib_buffer_t * b, bfd_session_t * bs)
626 bfd_pkt_with_sha1_auth_t *pkt = vlib_buffer_get_current (b);
627 bfd_auth_sha1_t *auth = &pkt->sha1_auth;
628 b->current_length += sizeof (*auth);
629 pkt->pkt.head.length += sizeof (*auth);
630 bfd_pkt_set_auth_present (&pkt->pkt);
631 memset (auth, 0, sizeof (*auth));
632 auth->type_len.type = bs->auth.curr_key->auth_type;
634 * only meticulous authentication types require incrementing seq number
635 * for every message, but doing so doesn't violate the RFC
637 ++bs->auth.local_seq_number;
638 auth->type_len.len = sizeof (bfd_auth_sha1_t);
639 auth->key_id = bs->auth.curr_bfd_key_id;
640 auth->seq_num = clib_host_to_net_u32 (bs->auth.local_seq_number);
642 * first copy the password into the packet, then calculate the hash
643 * and finally replace the password with the calculated hash
645 clib_memcpy (auth->hash, bs->auth.curr_key->key,
646 sizeof (bs->auth.curr_key->key));
647 unsigned char hash[sizeof (auth->hash)];
648 SHA1 ((unsigned char *) pkt, sizeof (*pkt), hash);
649 BFD_DBG ("hashing: %U", format_hex_bytes, pkt, sizeof (*pkt));
650 clib_memcpy (auth->hash, hash, sizeof (hash));
655 bfd_add_auth_section (vlib_buffer_t * b, bfd_session_t * bs)
657 if (bs->auth.curr_key)
659 const bfd_auth_type_e auth_type = bs->auth.curr_key->auth_type;
662 case BFD_AUTH_TYPE_reserved:
664 case BFD_AUTH_TYPE_simple_password:
666 case BFD_AUTH_TYPE_keyed_md5:
668 case BFD_AUTH_TYPE_meticulous_keyed_md5:
669 clib_warning ("Internal error, unexpected BFD auth type '%d'",
673 case BFD_AUTH_TYPE_keyed_sha1:
675 case BFD_AUTH_TYPE_meticulous_keyed_sha1:
676 bfd_add_sha1_auth_section (b, bs);
679 case BFD_AUTH_TYPE_keyed_sha1:
681 case BFD_AUTH_TYPE_meticulous_keyed_sha1:
682 clib_warning ("Internal error, unexpected BFD auth type '%d'",
691 bfd_is_echo_possible (bfd_session_t * bs)
693 if (BFD_STATE_up == bs->local_state && BFD_STATE_up == bs->remote_state &&
694 bs->remote_min_echo_rx_usec > 0)
696 switch (bs->transport)
698 case BFD_TRANSPORT_UDP4:
699 return bfd_udp_is_echo_available (BFD_TRANSPORT_UDP4);
700 case BFD_TRANSPORT_UDP6:
701 return bfd_udp_is_echo_available (BFD_TRANSPORT_UDP6);
708 bfd_init_control_frame (bfd_main_t * bm, bfd_session_t * bs,
711 bfd_pkt_t *pkt = vlib_buffer_get_current (b);
713 bfd_length = sizeof (bfd_pkt_t);
714 memset (pkt, 0, sizeof (*pkt));
715 bfd_pkt_set_version (pkt, 1);
716 bfd_pkt_set_diag_code (pkt, bs->local_diag);
717 bfd_pkt_set_state (pkt, bs->local_state);
718 pkt->head.detect_mult = bs->local_detect_mult;
719 pkt->head.length = bfd_length;
720 pkt->my_disc = bs->local_discr;
721 pkt->your_disc = bs->remote_discr;
722 pkt->des_min_tx = clib_host_to_net_u32 (bs->config_desired_min_tx_usec);
726 clib_host_to_net_u32 (bfd_clocks_to_usec
727 (bm, bs->effective_required_min_rx_clocks));
732 clib_host_to_net_u32 (bs->config_required_min_rx_usec);
734 pkt->req_min_echo_rx = clib_host_to_net_u32 (1);
735 b->current_length = bfd_length;
739 bfd_send_echo (vlib_main_t * vm, vlib_node_runtime_t * rt,
740 bfd_main_t * bm, bfd_session_t * bs, u64 now)
742 if (!bfd_is_echo_possible (bs))
744 BFD_DBG ("\nSwitching off echo function: %U", format_bfd_session, bs);
748 /* sometimes the wheel expires an event a bit sooner than requested,
751 if (now + bm->wheel_inaccuracy >= bs->echo_tx_timeout_clocks)
753 BFD_DBG ("\nSending echo packet: %U", format_bfd_session, bs);
755 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
757 clib_warning ("buffer allocation failure");
760 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
761 ASSERT (b->current_data == 0);
762 memset (vnet_buffer (b), 0, sizeof (*vnet_buffer (b)));
763 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b);
764 bfd_echo_pkt_t *pkt = vlib_buffer_get_current (b);
765 memset (pkt, 0, sizeof (*pkt));
766 pkt->discriminator = bs->local_discr;
767 pkt->expire_time_clocks =
768 now + bs->echo_transmit_interval_clocks * bs->local_detect_mult;
770 bfd_calc_echo_checksum (bs->local_discr, pkt->expire_time_clocks,
772 b->current_length = sizeof (*pkt);
773 if (!bfd_echo_add_transport_layer (vm, bi, bs))
775 BFD_ERR ("cannot send echo packet out, turning echo off");
777 vlib_buffer_free_one (vm, bi);
780 if (!bfd_transport_echo (vm, bi, bs))
782 BFD_ERR ("cannot send echo packet out, turning echo off");
784 vlib_buffer_free_one (vm, bi);
787 bs->echo_last_tx_clocks = now;
788 bfd_calc_next_echo_tx (bm, bs, now);
793 ("No need to send echo packet now, now is %lu, tx_timeout is %lu",
794 now, bs->echo_tx_timeout_clocks);
799 bfd_send_periodic (vlib_main_t * vm, vlib_node_runtime_t * rt,
800 bfd_main_t * bm, bfd_session_t * bs, u64 now)
802 if (!bs->remote_min_rx_usec && BFD_POLL_NOT_NEEDED == bs->poll_state)
804 BFD_DBG ("Remote min rx interval is zero, not sending periodic control "
808 if (BFD_POLL_NOT_NEEDED == bs->poll_state && bs->remote_demand &&
809 BFD_STATE_up == bs->local_state && BFD_STATE_up == bs->remote_state)
812 * A system MUST NOT periodically transmit BFD Control packets if Demand
813 * mode is active on the remote system (bfd.RemoteDemandMode is 1,
814 * bfd.SessionState is Up, and bfd.RemoteSessionState is Up) and a Poll
815 * Sequence is not being transmitted.
817 BFD_DBG ("Remote demand is set, not sending periodic control frame");
821 * sometimes the wheel expires an event a bit sooner than requested, account
824 if (now + bm->wheel_inaccuracy >= bs->tx_timeout_clocks)
826 BFD_DBG ("\nSending periodic control frame: %U", format_bfd_session,
829 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
831 clib_warning ("buffer allocation failure");
834 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
835 ASSERT (b->current_data == 0);
836 memset (vnet_buffer (b), 0, sizeof (*vnet_buffer (b)));
837 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b);
838 bfd_init_control_frame (bm, bs, b);
839 switch (bs->poll_state)
841 case BFD_POLL_NEEDED:
842 if (now < bs->poll_state_start_or_timeout_clocks)
844 BFD_DBG ("Cannot start a poll sequence yet, need to wait "
846 BFD_CLK_PRN (bs->poll_state_start_or_timeout_clocks -
850 bs->poll_state_start_or_timeout_clocks = now;
851 bfd_set_poll_state (bs, BFD_POLL_IN_PROGRESS);
853 case BFD_POLL_IN_PROGRESS:
854 case BFD_POLL_IN_PROGRESS_AND_QUEUED:
855 bfd_pkt_set_poll (vlib_buffer_get_current (b));
856 BFD_DBG ("Setting poll bit in packet, bs_idx=%u", bs->bs_idx);
858 case BFD_POLL_NOT_NEEDED:
862 bfd_add_auth_section (b, bs);
863 bfd_add_transport_layer (vm, bi, bs);
864 if (!bfd_transport_control_frame (vm, bi, bs))
866 vlib_buffer_free_one (vm, bi);
868 bs->last_tx_clocks = now;
869 bfd_calc_next_tx (bm, bs, now);
874 ("No need to send control frame now, now is %lu, tx_timeout is %lu",
875 now, bs->tx_timeout_clocks);
880 bfd_init_final_control_frame (vlib_main_t * vm, vlib_buffer_t * b,
881 bfd_main_t * bm, bfd_session_t * bs,
884 BFD_DBG ("Send final control frame for bs_idx=%lu", bs->bs_idx);
885 bfd_init_control_frame (bm, bs, b);
886 bfd_pkt_set_final (vlib_buffer_get_current (b));
887 bfd_add_auth_section (b, bs);
888 u32 bi = vlib_get_buffer_index (vm, b);
889 bfd_add_transport_layer (vm, bi, bs);
890 bs->last_tx_clocks = clib_cpu_time_now ();
892 * RFC allows to include changes in final frame, so if there were any
893 * pending, we already did that, thus we can clear any pending poll needs
895 bfd_set_poll_state (bs, BFD_POLL_NOT_NEEDED);
899 bfd_check_rx_timeout (bfd_main_t * bm, bfd_session_t * bs, u64 now,
903 * sometimes the wheel expires an event a bit sooner than requested, account
906 if (bs->last_rx_clocks + bs->detection_time_clocks <=
907 now + bm->wheel_inaccuracy)
909 BFD_DBG ("Rx timeout, session goes down");
910 bfd_set_diag (bs, BFD_DIAG_CODE_det_time_exp);
911 bfd_set_state (bm, bs, BFD_STATE_down, handling_wakeup);
913 * If the remote system does not receive any
914 * BFD Control packets for a Detection Time, it SHOULD reset
915 * bfd.RemoteMinRxInterval to its initial value of 1 (per section 6.8.1,
916 * since it is no longer required to maintain previous session state)
917 * and then can transmit at its own rate.
919 bfd_set_remote_required_min_rx (bm, bs, now, 1);
922 bs->echo_last_rx_clocks +
923 bs->echo_transmit_interval_clocks * bs->local_detect_mult <=
924 now + bm->wheel_inaccuracy)
926 BFD_DBG ("Echo rx timeout, session goes down");
927 bfd_set_diag (bs, BFD_DIAG_CODE_echo_failed);
928 bfd_set_state (bm, bs, BFD_STATE_down, handling_wakeup);
933 bfd_on_timeout (vlib_main_t * vm, vlib_node_runtime_t * rt, bfd_main_t * bm,
934 bfd_session_t * bs, u64 now)
936 BFD_DBG ("Timeout for bs_idx=%lu", bs->bs_idx);
937 switch (bs->local_state)
939 case BFD_STATE_admin_down:
940 bfd_send_periodic (vm, rt, bm, bs, now);
943 bfd_send_periodic (vm, rt, bm, bs, now);
946 bfd_check_rx_timeout (bm, bs, now, 1);
947 bfd_send_periodic (vm, rt, bm, bs, now);
950 bfd_check_rx_timeout (bm, bs, now, 1);
951 if (BFD_POLL_NOT_NEEDED == bs->poll_state && !bs->echo &&
952 bfd_is_echo_possible (bs))
954 /* switch on echo function as main detection method now */
955 BFD_DBG ("Switching on echo function, bs_idx=%u", bs->bs_idx);
957 bs->echo_last_rx_clocks = now;
958 bs->echo_tx_timeout_clocks = now;
959 bfd_set_effective_required_min_rx (bm, bs,
961 (bm->min_required_min_rx_while_echo_clocks,
962 bs->config_required_min_rx_clocks));
963 bfd_set_poll_state (bs, BFD_POLL_NEEDED);
965 bfd_send_periodic (vm, rt, bm, bs, now);
968 bfd_send_echo (vm, rt, bm, bs, now);
975 * bfd process node function
978 bfd_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
980 bfd_main_t *bm = &bfd_main;
982 uword event_type, *event_data = 0;
984 /* So we can send events to the bfd process */
985 bm->bfd_process_node_index = bfd_process_node.index;
989 u64 now = clib_cpu_time_now ();
990 u64 next_expire = timing_wheel_next_expiring_elt_time (&bm->wheel);
991 BFD_DBG ("timing_wheel_next_expiring_elt_time(%p) returns %lu",
992 &bm->wheel, next_expire);
993 if ((i64) next_expire < 0)
995 BFD_DBG ("wait for event without timeout");
996 (void) vlib_process_wait_for_event (vm);
997 event_type = vlib_process_get_events (vm, &event_data);
1001 f64 timeout = ((i64) next_expire - (i64) now) / bm->cpu_cps;
1002 BFD_DBG ("wait for event with timeout %.02f", timeout);
1005 BFD_DBG ("negative timeout, already expired, skipping wait");
1010 (void) vlib_process_wait_for_event_or_clock (vm, timeout);
1011 event_type = vlib_process_get_events (vm, &event_data);
1014 now = clib_cpu_time_now ();
1017 case ~0: /* no events => timeout */
1018 /* nothing to do here */
1020 case BFD_EVENT_RESCHEDULE:
1021 /* nothing to do here - reschedule is done automatically after
1022 * each event or timeout */
1024 case BFD_EVENT_NEW_SESSION:
1025 if (!pool_is_free_index (bm->sessions, *event_data))
1028 pool_elt_at_index (bm->sessions, *event_data);
1029 bfd_send_periodic (vm, rt, bm, bs, now);
1030 bfd_set_timer (bm, bs, now, 1);
1034 BFD_DBG ("Ignoring event for non-existent session index %u",
1035 (u32) * event_data);
1038 case BFD_EVENT_CONFIG_CHANGED:
1039 if (!pool_is_free_index (bm->sessions, *event_data))
1042 pool_elt_at_index (bm->sessions, *event_data);
1043 bfd_on_config_change (vm, rt, bm, bs, now);
1047 BFD_DBG ("Ignoring event for non-existent session index %u",
1048 (u32) * event_data);
1052 clib_warning ("BUG: event type 0x%wx", event_type);
1055 BFD_DBG ("advancing wheel, now is %lu", now);
1056 BFD_DBG ("timing_wheel_advance (%p, %lu, %p, 0);", &bm->wheel, now,
1058 expired = timing_wheel_advance (&bm->wheel, now, expired, 0);
1059 BFD_DBG ("Expired %d elements", vec_len (expired));
1061 vec_foreach (p, expired)
1063 const u32 bs_idx = *p;
1064 if (!pool_is_free_index (bm->sessions, bs_idx))
1066 bfd_session_t *bs = pool_elt_at_index (bm->sessions, bs_idx);
1067 bfd_on_timeout (vm, rt, bm, bs, now);
1068 bfd_set_timer (bm, bs, now, 1);
1073 _vec_len (expired) = 0;
1077 _vec_len (event_data) = 0;
1085 * bfd process node declaration
1088 VLIB_REGISTER_NODE (bfd_process_node, static) = {
1089 .function = bfd_process,
1090 .type = VLIB_NODE_TYPE_PROCESS,
1091 .name = "bfd-process",
1097 static clib_error_t *
1098 bfd_sw_interface_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
1100 // bfd_main_t *bm = &bfd_main;
1101 // vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
1102 if (!(flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
1109 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (bfd_sw_interface_up_down);
1111 static clib_error_t *
1112 bfd_hw_interface_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
1114 // bfd_main_t *bm = &bfd_main;
1115 if (flags & VNET_HW_INTERFACE_FLAG_LINK_UP)
1122 VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION (bfd_hw_interface_up_down);
1127 static clib_error_t *
1128 bfd_main_init (vlib_main_t * vm)
1131 setbuf (stdout, NULL);
1133 bfd_main_t *bm = &bfd_main;
1134 bm->random_seed = random_default_seed ();
1136 bm->vnet_main = vnet_get_main ();
1137 memset (&bm->wheel, 0, sizeof (bm->wheel));
1138 bm->cpu_cps = vm->clib_time.clocks_per_second;
1139 BFD_DBG ("cps is %.2f", bm->cpu_cps);
1140 bm->default_desired_min_tx_clocks =
1141 bfd_usec_to_clocks (bm, BFD_DEFAULT_DESIRED_MIN_TX_USEC);
1142 bm->min_required_min_rx_while_echo_clocks =
1143 bfd_usec_to_clocks (bm, BFD_REQUIRED_MIN_RX_USEC_WHILE_ECHO);
1144 const u64 now = clib_cpu_time_now ();
1145 timing_wheel_init (&bm->wheel, now, bm->cpu_cps);
1146 bm->wheel_inaccuracy = 2 << bm->wheel.log2_clocks_per_bin;
1150 VLIB_INIT_FUNCTION (bfd_main_init);
1153 bfd_get_session (bfd_main_t * bm, bfd_transport_e t)
1155 bfd_session_t *result;
1156 pool_get (bm->sessions, result);
1157 memset (result, 0, sizeof (*result));
1158 result->bs_idx = result - bm->sessions;
1159 result->transport = t;
1160 const unsigned limit = 1000;
1161 unsigned counter = 0;
1164 result->local_discr = random_u32 (&bm->random_seed);
1165 if (counter > limit)
1167 clib_warning ("Couldn't allocate unused session discriminator even "
1168 "after %u tries!", limit);
1169 pool_put (bm->sessions, result);
1174 while (hash_get (bm->session_by_disc, result->local_discr));
1175 bfd_set_defaults (bm, result);
1176 hash_set (bm->session_by_disc, result->local_discr, result->bs_idx);
1181 bfd_put_session (bfd_main_t * bm, bfd_session_t * bs)
1183 if (bs->auth.curr_key)
1185 --bs->auth.curr_key->use_count;
1187 if (bs->auth.next_key)
1189 --bs->auth.next_key->use_count;
1191 hash_unset (bm->session_by_disc, bs->local_discr);
1192 pool_put (bm->sessions, bs);
1196 bfd_find_session_by_idx (bfd_main_t * bm, uword bs_idx)
1198 if (!pool_is_free_index (bm->sessions, bs_idx))
1200 return pool_elt_at_index (bm->sessions, bs_idx);
1206 bfd_find_session_by_disc (bfd_main_t * bm, u32 disc)
1208 uword *p = hash_get (bfd_main.session_by_disc, disc);
1211 return pool_elt_at_index (bfd_main.sessions, *p);
1217 * @brief verify bfd packet - common checks
1221 * @return 1 if bfd packet is valid
1224 bfd_verify_pkt_common (const bfd_pkt_t * pkt)
1226 if (1 != bfd_pkt_get_version (pkt))
1228 BFD_ERR ("BFD verification failed - unexpected version: '%d'",
1229 bfd_pkt_get_version (pkt));
1232 if (pkt->head.length < sizeof (bfd_pkt_t) ||
1233 (bfd_pkt_get_auth_present (pkt) &&
1234 pkt->head.length < sizeof (bfd_pkt_with_common_auth_t)))
1236 BFD_ERR ("BFD verification failed - unexpected length: '%d' (auth "
1238 pkt->head.length, bfd_pkt_get_auth_present (pkt));
1241 if (!pkt->head.detect_mult)
1243 BFD_ERR ("BFD verification failed - unexpected detect-mult: '%d'",
1244 pkt->head.detect_mult);
1247 if (bfd_pkt_get_multipoint (pkt))
1249 BFD_ERR ("BFD verification failed - unexpected multipoint: '%d'",
1250 bfd_pkt_get_multipoint (pkt));
1255 BFD_ERR ("BFD verification failed - unexpected my-disc: '%d'",
1259 if (!pkt->your_disc)
1261 const u8 pkt_state = bfd_pkt_get_state (pkt);
1262 if (pkt_state != BFD_STATE_down && pkt_state != BFD_STATE_admin_down)
1264 BFD_ERR ("BFD verification failed - unexpected state: '%s' "
1265 "(your-disc is zero)", bfd_state_string (pkt_state));
1273 bfd_session_switch_auth_to_next (bfd_session_t * bs)
1275 BFD_DBG ("Switching authentication key from %U to %U for bs_idx=%u",
1276 format_bfd_auth_key, bs->auth.curr_key, format_bfd_auth_key,
1277 bs->auth.next_key, bs->bs_idx);
1278 bs->auth.is_delayed = 0;
1279 if (bs->auth.curr_key)
1281 --bs->auth.curr_key->use_count;
1283 bs->auth.curr_key = bs->auth.next_key;
1284 bs->auth.next_key = NULL;
1285 bs->auth.curr_bfd_key_id = bs->auth.next_bfd_key_id;
1289 bfd_auth_type_is_meticulous (bfd_auth_type_e auth_type)
1291 if (BFD_AUTH_TYPE_meticulous_keyed_md5 == auth_type ||
1292 BFD_AUTH_TYPE_meticulous_keyed_sha1 == auth_type)
1300 bfd_verify_pkt_auth_seq_num (bfd_session_t * bs,
1301 u32 received_seq_num, int is_meticulous)
1306 * This variable MUST be set to zero after no packets have been
1307 * received on this session for at least twice the Detection Time.
1309 u64 now = clib_cpu_time_now ();
1310 if (now - bs->last_rx_clocks > bs->detection_time_clocks * 2)
1312 BFD_DBG ("BFD peer unresponsive for %lu clocks, which is > 2 * "
1313 "detection_time=%u clocks, resetting remote_seq_number_known "
1315 now - bs->last_rx_clocks, bs->detection_time_clocks * 2);
1316 bs->auth.remote_seq_number_known = 0;
1318 if (bs->auth.remote_seq_number_known)
1320 /* remote sequence number is known, verify its validity */
1321 const u32 max_u32 = 0xffffffff;
1322 /* the calculation might wrap, account for the special case... */
1323 if (bs->auth.remote_seq_number > max_u32 - 3 * bs->local_detect_mult)
1329 * |----------+----------------------------+-----------|
1331 * | remote_seq_num------+
1333 * +-----(remote_seq_num + 3*detect_mult) % * 0xffffffff
1335 * x + y + z = 0xffffffff
1336 * x + z = 3 * detect_mult
1338 const u32 z = max_u32 - bs->auth.remote_seq_number;
1339 const u32 x = 3 * bs->local_detect_mult - z;
1340 if (received_seq_num > x &&
1341 received_seq_num < bs->auth.remote_seq_number + is_meticulous)
1344 ("Recvd sequence number=%u out of ranges <0, %u>, <%u, %u>",
1345 received_seq_num, x,
1346 bs->auth.remote_seq_number + is_meticulous, max_u32);
1353 const u32 min = bs->auth.remote_seq_number + is_meticulous;
1355 bs->auth.remote_seq_number + 3 * bs->local_detect_mult;
1356 if (received_seq_num < min || received_seq_num > max)
1358 BFD_ERR ("Recvd sequence number=%u out of range <%u, %u>",
1359 received_seq_num, min, max);
1368 bfd_verify_pkt_auth_key_sha1 (const bfd_pkt_t * pkt, u32 pkt_size,
1369 bfd_session_t * bs, u8 bfd_key_id,
1370 bfd_auth_key_t * auth_key)
1372 ASSERT (auth_key->auth_type == BFD_AUTH_TYPE_keyed_sha1 ||
1373 auth_key->auth_type == BFD_AUTH_TYPE_meticulous_keyed_sha1);
1375 u8 result[SHA_DIGEST_LENGTH];
1376 bfd_pkt_with_common_auth_t *with_common = (void *) pkt;
1377 if (pkt_size < sizeof (*with_common))
1379 BFD_ERR ("Packet size too small to hold authentication common header");
1382 if (with_common->common_auth.type != auth_key->auth_type)
1384 BFD_ERR ("BFD auth type mismatch, packet auth=%d:%s doesn't match "
1385 "in-use auth=%d:%s",
1386 with_common->common_auth.type,
1387 bfd_auth_type_str (with_common->common_auth.type),
1388 auth_key->auth_type, bfd_auth_type_str (auth_key->auth_type));
1391 bfd_pkt_with_sha1_auth_t *with_sha1 = (void *) pkt;
1392 if (pkt_size < sizeof (*with_sha1) ||
1393 with_sha1->sha1_auth.type_len.len < sizeof (with_sha1->sha1_auth))
1396 ("BFD size mismatch, payload size=%u, expected=%u, auth_len=%u, "
1397 "expected=%u", pkt_size, sizeof (*with_sha1),
1398 with_sha1->sha1_auth.type_len.len, sizeof (with_sha1->sha1_auth));
1401 if (with_sha1->sha1_auth.key_id != bfd_key_id)
1404 ("BFD key ID mismatch, packet key ID=%u doesn't match key ID=%u%s",
1405 with_sha1->sha1_auth.key_id, bfd_key_id,
1407 auth.is_delayed ? " (but a delayed auth change is scheduled)" : "");
1411 if (!SHA1_Init (&ctx))
1413 BFD_ERR ("SHA1_Init failed");
1416 /* ignore last 20 bytes - use the actual key data instead pkt data */
1417 if (!SHA1_Update (&ctx, with_sha1,
1418 sizeof (*with_sha1) - sizeof (with_sha1->sha1_auth.hash)))
1420 BFD_ERR ("SHA1_Update failed");
1423 if (!SHA1_Update (&ctx, auth_key->key, sizeof (auth_key->key)))
1425 BFD_ERR ("SHA1_Update failed");
1428 if (!SHA1_Final (result, &ctx))
1430 BFD_ERR ("SHA1_Final failed");
1433 if (0 == memcmp (result, with_sha1->sha1_auth.hash, SHA_DIGEST_LENGTH))
1437 BFD_ERR ("SHA1 hash: %U doesn't match the expected value: %U",
1438 format_hex_bytes, with_sha1->sha1_auth.hash, SHA_DIGEST_LENGTH,
1439 format_hex_bytes, result, SHA_DIGEST_LENGTH);
1444 bfd_verify_pkt_auth_key (const bfd_pkt_t * pkt, u32 pkt_size,
1445 bfd_session_t * bs, u8 bfd_key_id,
1446 bfd_auth_key_t * auth_key)
1448 switch (auth_key->auth_type)
1450 case BFD_AUTH_TYPE_reserved:
1451 clib_warning ("Internal error, unexpected auth_type=%d:%s",
1452 auth_key->auth_type,
1453 bfd_auth_type_str (auth_key->auth_type));
1455 case BFD_AUTH_TYPE_simple_password:
1457 ("Internal error, not implemented, unexpected auth_type=%d:%s",
1458 auth_key->auth_type, bfd_auth_type_str (auth_key->auth_type));
1460 case BFD_AUTH_TYPE_keyed_md5:
1462 case BFD_AUTH_TYPE_meticulous_keyed_md5:
1464 ("Internal error, not implemented, unexpected auth_type=%d:%s",
1465 auth_key->auth_type, bfd_auth_type_str (auth_key->auth_type));
1467 case BFD_AUTH_TYPE_keyed_sha1:
1469 case BFD_AUTH_TYPE_meticulous_keyed_sha1:
1473 const u32 seq_num = clib_net_to_host_u32 (((bfd_pkt_with_sha1_auth_t
1476 return bfd_verify_pkt_auth_seq_num (bs, seq_num,
1477 bfd_auth_type_is_meticulous
1478 (auth_key->auth_type))
1479 && bfd_verify_pkt_auth_key_sha1 (pkt, pkt_size, bs, bfd_key_id,
1485 ("Internal error, attempt to use SHA1 without SSL support");
1493 * @brief verify bfd packet - authentication
1497 * @return 1 if bfd packet is valid
1500 bfd_verify_pkt_auth (const bfd_pkt_t * pkt, u16 pkt_size, bfd_session_t * bs)
1502 if (bfd_pkt_get_auth_present (pkt))
1504 /* authentication present in packet */
1505 if (!bs->auth.curr_key)
1507 /* currently not using authentication - can we turn it on? */
1508 if (bs->auth.is_delayed && bs->auth.next_key)
1510 /* yes, switch is scheduled - make sure the auth is valid */
1511 if (bfd_verify_pkt_auth_key (pkt, pkt_size, bs,
1512 bs->auth.next_bfd_key_id,
1515 /* auth matches next key, do the switch, packet is valid */
1516 bfd_session_switch_auth_to_next (bs);
1523 /* yes, using authentication, verify the key */
1524 if (bfd_verify_pkt_auth_key (pkt, pkt_size, bs,
1525 bs->auth.curr_bfd_key_id,
1528 /* verification passed, packet is valid */
1533 /* verification failed - but maybe we need to switch key */
1534 if (bs->auth.is_delayed && bs->auth.next_key)
1536 /* delayed switch present, verify if that key works */
1537 if (bfd_verify_pkt_auth_key (pkt, pkt_size, bs,
1538 bs->auth.next_bfd_key_id,
1541 /* auth matches next key, switch key, packet is valid */
1542 bfd_session_switch_auth_to_next (bs);
1551 /* authentication in packet not present */
1552 if (pkt_size > sizeof (*pkt))
1554 BFD_ERR ("BFD verification failed - unexpected packet size '%d' "
1555 "(auth not present)", pkt_size);
1558 if (bs->auth.curr_key)
1560 /* currently authenticating - could we turn it off? */
1561 if (bs->auth.is_delayed && !bs->auth.next_key)
1563 /* yes, delayed switch to NULL key is scheduled */
1564 bfd_session_switch_auth_to_next (bs);
1570 /* no auth in packet, no auth in use - packet is valid */
1578 bfd_consume_pkt (bfd_main_t * bm, const bfd_pkt_t * pkt, u32 bs_idx)
1580 bfd_session_t *bs = bfd_find_session_by_idx (bm, bs_idx);
1581 if (!bs || (pkt->your_disc && pkt->your_disc != bs->local_discr))
1585 BFD_DBG ("Scanning bfd packet, bs_idx=%d", bs->bs_idx);
1586 bs->remote_discr = pkt->my_disc;
1587 bs->remote_state = bfd_pkt_get_state (pkt);
1588 bs->remote_demand = bfd_pkt_get_demand (pkt);
1589 bs->remote_diag = bfd_pkt_get_diag_code (pkt);
1590 u64 now = clib_cpu_time_now ();
1591 bs->last_rx_clocks = now;
1592 if (bfd_pkt_get_auth_present (pkt))
1594 bfd_auth_type_e auth_type =
1595 ((bfd_pkt_with_common_auth_t *) (pkt))->common_auth.type;
1598 case BFD_AUTH_TYPE_reserved:
1600 case BFD_AUTH_TYPE_simple_password:
1602 case BFD_AUTH_TYPE_keyed_md5:
1604 case BFD_AUTH_TYPE_meticulous_keyed_md5:
1605 clib_warning ("Internal error, unexpected auth_type=%d:%s",
1606 auth_type, bfd_auth_type_str (auth_type));
1608 case BFD_AUTH_TYPE_keyed_sha1:
1610 case BFD_AUTH_TYPE_meticulous_keyed_sha1:
1613 bfd_pkt_with_sha1_auth_t *with_sha1 =
1614 (bfd_pkt_with_sha1_auth_t *) pkt;
1615 bs->auth.remote_seq_number =
1616 clib_net_to_host_u32 (with_sha1->sha1_auth.seq_num);
1617 bs->auth.remote_seq_number_known = 1;
1618 BFD_DBG ("Received sequence number %u",
1619 bs->auth.remote_seq_number);
1624 bs->remote_desired_min_tx_clocks =
1625 bfd_usec_to_clocks (bm, clib_net_to_host_u32 (pkt->des_min_tx));
1626 bs->remote_detect_mult = pkt->head.detect_mult;
1627 bfd_set_remote_required_min_rx (bm, bs, now,
1628 clib_net_to_host_u32 (pkt->req_min_rx));
1629 bfd_set_remote_required_min_echo_rx (bm, bs, now,
1630 clib_net_to_host_u32
1631 (pkt->req_min_echo_rx));
1632 if (bfd_pkt_get_final (pkt))
1634 if (BFD_POLL_IN_PROGRESS == bs->poll_state)
1636 BFD_DBG ("Poll sequence terminated, bs_idx=%u", bs->bs_idx);
1637 bfd_set_poll_state (bs, BFD_POLL_NOT_NEEDED);
1638 if (BFD_STATE_up == bs->local_state)
1640 bfd_set_effective_required_min_rx (bm, bs,
1641 clib_max (bs->echo *
1642 bm->min_required_min_rx_while_echo_clocks,
1643 bs->config_required_min_rx_clocks));
1646 else if (BFD_POLL_IN_PROGRESS_AND_QUEUED == bs->poll_state)
1649 * next poll sequence must be delayed by at least the round trip
1650 * time, so calculate that here
1652 BFD_DBG ("Next poll sequence can commence in " BFD_CLK_FMT,
1654 bs->poll_state_start_or_timeout_clocks));
1655 bs->poll_state_start_or_timeout_clocks =
1656 now + (now - bs->poll_state_start_or_timeout_clocks);
1658 ("Poll sequence terminated, but another is needed, bs_idx=%u",
1660 bfd_set_poll_state (bs, BFD_POLL_NEEDED);
1663 bfd_calc_next_tx (bm, bs, now);
1664 bfd_set_timer (bm, bs, now, 0);
1665 if (BFD_STATE_admin_down == bs->local_state)
1667 BFD_DBG ("Session is admin-down, ignoring packet, bs_idx=%u",
1671 if (BFD_STATE_admin_down == bs->remote_state)
1673 bfd_set_diag (bs, BFD_DIAG_CODE_neighbor_sig_down);
1674 bfd_set_state (bm, bs, BFD_STATE_down, 0);
1676 else if (BFD_STATE_down == bs->local_state)
1678 if (BFD_STATE_down == bs->remote_state)
1680 bfd_set_diag (bs, BFD_DIAG_CODE_no_diag);
1681 bfd_set_state (bm, bs, BFD_STATE_init, 0);
1683 else if (BFD_STATE_init == bs->remote_state)
1685 bfd_set_diag (bs, BFD_DIAG_CODE_no_diag);
1686 bfd_set_state (bm, bs, BFD_STATE_up, 0);
1689 else if (BFD_STATE_init == bs->local_state)
1691 if (BFD_STATE_up == bs->remote_state ||
1692 BFD_STATE_init == bs->remote_state)
1694 bfd_set_diag (bs, BFD_DIAG_CODE_no_diag);
1695 bfd_set_state (bm, bs, BFD_STATE_up, 0);
1698 else /* BFD_STATE_up == bs->local_state */
1700 if (BFD_STATE_down == bs->remote_state)
1702 bfd_set_diag (bs, BFD_DIAG_CODE_neighbor_sig_down);
1703 bfd_set_state (bm, bs, BFD_STATE_down, 0);
1709 bfd_consume_echo_pkt (bfd_main_t * bm, vlib_buffer_t * b)
1711 bfd_echo_pkt_t *pkt = NULL;
1712 if (b->current_length != sizeof (*pkt))
1716 pkt = vlib_buffer_get_current (b);
1717 bfd_session_t *bs = bfd_find_session_by_disc (bm, pkt->discriminator);
1722 BFD_DBG ("Scanning bfd echo packet, bs_idx=%d", bs->bs_idx);
1724 bfd_calc_echo_checksum (bs->local_discr, pkt->expire_time_clocks,
1726 if (checksum != pkt->checksum)
1728 BFD_DBG ("Invalid echo packet, checksum mismatch");
1731 u64 now = clib_cpu_time_now ();
1732 if (pkt->expire_time_clocks < now)
1734 BFD_DBG ("Stale packet received, expire time %lu < now %lu",
1735 pkt->expire_time_clocks, now);
1739 bs->echo_last_rx_clocks = now;
1745 format_bfd_session (u8 * s, va_list * args)
1747 const bfd_session_t *bs = va_arg (*args, bfd_session_t *);
1748 uword indent = format_get_indent (s);
1749 s = format (s, "bs_idx=%u local-state=%s remote-state=%s\n"
1750 "%Ulocal-discriminator=%u remote-discriminator=%u\n"
1751 "%Ulocal-diag=%s echo-active=%s\n"
1752 "%Udesired-min-tx=%u required-min-rx=%u\n"
1753 "%Urequired-min-echo-rx=%u detect-mult=%u\n"
1754 "%Uremote-min-rx=%u remote-min-echo-rx=%u\n"
1755 "%Uremote-demand=%s poll-state=%s\n"
1756 "%Uauth: local-seq-num=%u remote-seq-num=%u\n"
1757 "%U is-delayed=%s\n"
1760 bs->bs_idx, bfd_state_string (bs->local_state),
1761 bfd_state_string (bs->remote_state), format_white_space, indent,
1762 bs->local_discr, bs->remote_discr, format_white_space, indent,
1763 bfd_diag_code_string (bs->local_diag),
1764 (bs->echo ? "yes" : "no"), format_white_space, indent,
1765 bs->config_desired_min_tx_usec, bs->config_required_min_rx_usec,
1766 format_white_space, indent, 1, bs->local_detect_mult,
1767 format_white_space, indent, bs->remote_min_rx_usec,
1768 bs->remote_min_echo_rx_usec, format_white_space, indent,
1769 (bs->remote_demand ? "yes" : "no"),
1770 bfd_poll_state_string (bs->poll_state), format_white_space,
1771 indent, bs->auth.local_seq_number, bs->auth.remote_seq_number,
1772 format_white_space, indent,
1773 (bs->auth.is_delayed ? "yes" : "no"), format_white_space,
1774 indent, format_bfd_auth_key, bs->auth.curr_key,
1775 format_white_space, indent, format_bfd_auth_key,
1781 bfd_auth_type_supported (bfd_auth_type_e auth_type)
1783 if (auth_type == BFD_AUTH_TYPE_keyed_sha1 ||
1784 auth_type == BFD_AUTH_TYPE_meticulous_keyed_sha1)
1792 bfd_auth_activate (bfd_session_t * bs, u32 conf_key_id,
1793 u8 bfd_key_id, u8 is_delayed)
1795 bfd_main_t *bm = &bfd_main;
1796 const uword *key_idx_p =
1797 hash_get (bm->auth_key_by_conf_key_id, conf_key_id);
1800 clib_warning ("Authentication key with config ID %u doesn't exist)",
1802 return VNET_API_ERROR_BFD_ENOENT;
1804 const uword key_idx = *key_idx_p;
1805 bfd_auth_key_t *key = pool_elt_at_index (bm->auth_keys, key_idx);
1808 if (bs->auth.next_key == key)
1810 /* already using this key, no changes required */
1813 bs->auth.next_key = key;
1814 bs->auth.next_bfd_key_id = bfd_key_id;
1815 bs->auth.is_delayed = 1;
1819 if (bs->auth.curr_key == key)
1821 /* already using this key, no changes required */
1824 if (bs->auth.curr_key)
1826 --bs->auth.curr_key->use_count;
1828 bs->auth.curr_key = key;
1829 bs->auth.curr_bfd_key_id = bfd_key_id;
1830 bs->auth.is_delayed = 0;
1833 BFD_DBG ("\nSession auth modified: %U", format_bfd_session, bs);
1838 bfd_auth_deactivate (bfd_session_t * bs, u8 is_delayed)
1843 /* not delayed - deactivate the current key right now */
1844 if (bs->auth.curr_key)
1846 --bs->auth.curr_key->use_count;
1847 bs->auth.curr_key = NULL;
1849 bs->auth.is_delayed = 0;
1853 /* delayed - mark as so */
1854 bs->auth.is_delayed = 1;
1857 * clear the next key unconditionally - either the auth change is not delayed
1858 * in which case the caller expects the session to not use authentication
1859 * from this point forward, or it is delayed, in which case the next_key
1860 * needs to be set to NULL to make it so in the future
1862 if (bs->auth.next_key)
1864 --bs->auth.next_key->use_count;
1865 bs->auth.next_key = NULL;
1867 BFD_DBG ("\nSession auth modified: %U", format_bfd_session, bs);
1870 clib_warning ("SSL missing, cannot deactivate BFD authentication");
1871 return VNET_API_ERROR_BFD_NOTSUPP;
1876 bfd_session_set_params (bfd_main_t * bm, bfd_session_t * bs,
1877 u32 desired_min_tx_usec,
1878 u32 required_min_rx_usec, u8 detect_mult)
1880 if (bs->local_detect_mult != detect_mult ||
1881 bs->config_desired_min_tx_usec != desired_min_tx_usec ||
1882 bs->config_required_min_rx_usec != required_min_rx_usec)
1884 BFD_DBG ("\nChanging session params: %U", format_bfd_session, bs);
1885 switch (bs->poll_state)
1887 case BFD_POLL_NOT_NEEDED:
1888 if (BFD_STATE_up == bs->local_state ||
1889 BFD_STATE_init == bs->local_state)
1891 /* poll sequence is not needed for detect multiplier change */
1892 if (bs->config_desired_min_tx_usec != desired_min_tx_usec ||
1893 bs->config_required_min_rx_usec != required_min_rx_usec)
1895 bfd_set_poll_state (bs, BFD_POLL_NEEDED);
1899 case BFD_POLL_NEEDED:
1900 case BFD_POLL_IN_PROGRESS_AND_QUEUED:
1902 * nothing to do - will be handled in the future poll which is
1903 * already scheduled for execution
1906 case BFD_POLL_IN_PROGRESS:
1907 /* poll sequence is not needed for detect multiplier change */
1908 if (bs->config_desired_min_tx_usec != desired_min_tx_usec ||
1909 bs->config_required_min_rx_usec != required_min_rx_usec)
1911 BFD_DBG ("Poll in progress, queueing extra poll, bs_idx=%u",
1913 bfd_set_poll_state (bs, BFD_POLL_IN_PROGRESS_AND_QUEUED);
1917 bs->local_detect_mult = detect_mult;
1918 bs->config_desired_min_tx_usec = desired_min_tx_usec;
1919 bs->config_desired_min_tx_clocks =
1920 bfd_usec_to_clocks (bm, desired_min_tx_usec);
1921 bs->config_required_min_rx_usec = required_min_rx_usec;
1922 bs->config_required_min_rx_clocks =
1923 bfd_usec_to_clocks (bm, required_min_rx_usec);
1924 BFD_DBG ("\nChanged session params: %U", format_bfd_session, bs);
1926 vlib_process_signal_event (bm->vlib_main, bm->bfd_process_node_index,
1927 BFD_EVENT_CONFIG_CHANGED, bs->bs_idx);
1931 BFD_DBG ("Ignore parameter change - no change, bs_idx=%u", bs->bs_idx);
1937 bfd_auth_set_key (u32 conf_key_id, u8 auth_type, u8 key_len,
1938 const u8 * key_data)
1941 bfd_auth_key_t *auth_key = NULL;
1942 if (!key_len || key_len > bfd_max_key_len_for_auth_type (auth_type))
1944 clib_warning ("Invalid authentication key length for auth_type=%d:%s "
1945 "(key_len=%u, must be "
1946 "non-zero, expected max=%u)",
1947 auth_type, bfd_auth_type_str (auth_type), key_len,
1948 (u32) bfd_max_key_len_for_auth_type (auth_type));
1949 return VNET_API_ERROR_INVALID_VALUE;
1951 if (!bfd_auth_type_supported (auth_type))
1953 clib_warning ("Unsupported auth type=%d:%s", auth_type,
1954 bfd_auth_type_str (auth_type));
1955 return VNET_API_ERROR_BFD_NOTSUPP;
1957 bfd_main_t *bm = &bfd_main;
1958 uword *key_idx_p = hash_get (bm->auth_key_by_conf_key_id, conf_key_id);
1961 /* modifying existing key - must not be used */
1962 const uword key_idx = *key_idx_p;
1963 auth_key = pool_elt_at_index (bm->auth_keys, key_idx);
1964 if (auth_key->use_count > 0)
1966 clib_warning ("Authentication key with conf ID %u in use by %u BFD "
1967 "session(s) - cannot modify",
1968 conf_key_id, auth_key->use_count);
1969 return VNET_API_ERROR_BFD_EINUSE;
1974 /* adding new key */
1975 pool_get (bm->auth_keys, auth_key);
1976 auth_key->conf_key_id = conf_key_id;
1977 hash_set (bm->auth_key_by_conf_key_id, conf_key_id,
1978 auth_key - bm->auth_keys);
1980 auth_key->auth_type = auth_type;
1981 memset (auth_key->key, 0, sizeof (auth_key->key));
1982 clib_memcpy (auth_key->key, key_data, key_len);
1985 clib_warning ("SSL missing, cannot manipulate authentication keys");
1986 return VNET_API_ERROR_BFD_NOTSUPP;
1991 bfd_auth_del_key (u32 conf_key_id)
1994 bfd_auth_key_t *auth_key = NULL;
1995 bfd_main_t *bm = &bfd_main;
1996 uword *key_idx_p = hash_get (bm->auth_key_by_conf_key_id, conf_key_id);
1999 /* deleting existing key - must not be used */
2000 const uword key_idx = *key_idx_p;
2001 auth_key = pool_elt_at_index (bm->auth_keys, key_idx);
2002 if (auth_key->use_count > 0)
2004 clib_warning ("Authentication key with conf ID %u in use by %u BFD "
2005 "session(s) - cannot delete",
2006 conf_key_id, auth_key->use_count);
2007 return VNET_API_ERROR_BFD_EINUSE;
2009 hash_unset (bm->auth_key_by_conf_key_id, conf_key_id);
2010 memset (auth_key, 0, sizeof (*auth_key));
2011 pool_put (bm->auth_keys, auth_key);
2016 clib_warning ("Authentication key with conf ID %u does not exist",
2018 return VNET_API_ERROR_BFD_ENOENT;
2022 clib_warning ("SSL missing, cannot manipulate authentication keys");
2023 return VNET_API_ERROR_BFD_NOTSUPP;
2027 bfd_main_t bfd_main;
2030 * fd.io coding-style-patch-verification: ON
2033 * eval: (c-set-style "gnu")