blob: 1ca1d7ec0ab545cb75f5f1c46e0c180f4b0d0b07 [file] [log] [blame]
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001/*
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:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
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.
14 */
15/**
16 * @file
17 * @brief BFD nodes implementation
18 */
19
Dave Barach1e3417f2018-07-25 08:30:27 -040020#include <vlibmemory/api.h>
Klement Sekera0e3c0de2016-09-29 14:43:44 +020021#include <vppinfra/random.h>
22#include <vppinfra/error.h>
23#include <vppinfra/hash.h>
Klement Sekera239790f2017-02-16 10:53:53 +010024#include <vppinfra/xxhash.h>
Klement Sekera0e3c0de2016-09-29 14:43:44 +020025#include <vnet/ethernet/ethernet.h>
26#include <vnet/ethernet/packet.h>
27#include <vnet/bfd/bfd_debug.h>
28#include <vnet/bfd/bfd_protocol.h>
29#include <vnet/bfd/bfd_main.h>
Damjan Marion07a38572018-01-21 06:44:18 -080030#include <vlib/log.h>
Klement Sekeraefd4d702021-04-20 19:21:36 +020031#include <vnet/crypto/crypto.h>
Klement Sekera239790f2017-02-16 10:53:53 +010032
Klement Sekeracdaf0d82022-02-14 20:20:22 +000033static void
34bfd_validate_counters (bfd_main_t *bm)
35{
36 vlib_validate_combined_counter (&bm->rx_counter, pool_elts (bm->sessions));
37 vlib_validate_combined_counter (&bm->rx_echo_counter,
38 pool_elts (bm->sessions));
39 vlib_validate_combined_counter (&bm->tx_counter, pool_elts (bm->sessions));
40 vlib_validate_combined_counter (&bm->tx_echo_counter,
41 pool_elts (bm->sessions));
42}
43
Klement Sekera239790f2017-02-16 10:53:53 +010044static u64
45bfd_calc_echo_checksum (u32 discriminator, u64 expire_time, u32 secret)
46{
47 u64 checksum = 0;
Gabriel Ganne8e66b9b2017-12-14 16:20:37 +010048#if defined(clib_crc32c_uses_intrinsics) && !defined (__i386__)
Damjan Marion271daab2021-11-12 16:00:24 +010049 checksum = clib_crc32c_u64 (0, discriminator);
50 checksum = clib_crc32c_u64 (checksum, expire_time);
51 checksum = clib_crc32c_u64 (checksum, secret);
Klement Sekera239790f2017-02-16 10:53:53 +010052#else
53 checksum = clib_xxhash (discriminator ^ expire_time ^ secret);
Klement Sekerab17dd962017-01-09 07:43:48 +010054#endif
Klement Sekera239790f2017-02-16 10:53:53 +010055 return checksum;
56}
Klement Sekera0e3c0de2016-09-29 14:43:44 +020057
58static u64
Klement Sekeraa3167442020-02-10 11:49:52 +000059bfd_usec_to_nsec (u64 us)
Klement Sekera0e3c0de2016-09-29 14:43:44 +020060{
Klement Sekeraa3167442020-02-10 11:49:52 +000061 return us * NSEC_PER_USEC;
Klement Sekera0e3c0de2016-09-29 14:43:44 +020062}
63
Klement Sekera73884482017-02-23 09:26:30 +010064u32
Klement Sekeraa3167442020-02-10 11:49:52 +000065bfd_nsec_to_usec (u64 nsec)
Klement Sekera239790f2017-02-16 10:53:53 +010066{
Klement Sekeraa3167442020-02-10 11:49:52 +000067 return nsec / NSEC_PER_USEC;
68}
69
70always_inline u64
71bfd_time_now_nsec (vlib_main_t * vm, f64 * vm_time)
72{
73 f64 _vm_time = vlib_time_now (vm);
74 if (vm_time)
75 *vm_time = _vm_time;
76 return _vm_time * NSEC_PER_SEC;
Klement Sekera239790f2017-02-16 10:53:53 +010077}
78
Klement Sekera0e3c0de2016-09-29 14:43:44 +020079static vlib_node_registration_t bfd_process_node;
80
Klement Sekera73884482017-02-23 09:26:30 +010081u8 *
Klement Sekerab17dd962017-01-09 07:43:48 +010082format_bfd_auth_key (u8 * s, va_list * args)
83{
84 const bfd_auth_key_t *key = va_arg (*args, bfd_auth_key_t *);
85 if (key)
86 {
87 s = format (s, "{auth-type=%u:%s, conf-key-id=%u, use-count=%u}, ",
88 key->auth_type, bfd_auth_type_str (key->auth_type),
89 key->conf_key_id, key->use_count);
90 }
91 else
92 {
93 s = format (s, "{none}");
94 }
95 return s;
96}
97
Klement Sekera0e3c0de2016-09-29 14:43:44 +020098/*
99 * We actually send all bfd pkts to the "error" node after scanning
100 * them, so the graph node has only one next-index. The "error-drop"
101 * node automatically bumps our per-node packet counters for us.
102 */
103typedef enum
104{
105 BFD_INPUT_NEXT_NORMAL,
106 BFD_INPUT_N_NEXT,
107} bfd_input_next_t;
108
Klement Sekerae4504c62016-12-08 10:16:41 +0100109static void bfd_on_state_change (bfd_main_t * bm, bfd_session_t * bs, u64 now,
110 int handling_wakeup);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200111
112static void
113bfd_set_defaults (bfd_main_t * bm, bfd_session_t * bs)
114{
115 bs->local_state = BFD_STATE_down;
116 bs->local_diag = BFD_DIAG_CODE_no_diag;
117 bs->remote_state = BFD_STATE_down;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200118 bs->remote_discr = 0;
Neale Ranns88fc83e2017-04-05 08:11:14 -0700119 bs->hop_type = BFD_HOP_TYPE_SINGLE;
Klement Sekera239790f2017-02-16 10:53:53 +0100120 bs->config_desired_min_tx_usec = BFD_DEFAULT_DESIRED_MIN_TX_USEC;
Klement Sekeraa3167442020-02-10 11:49:52 +0000121 bs->config_desired_min_tx_nsec = bm->default_desired_min_tx_nsec;
122 bs->effective_desired_min_tx_nsec = bm->default_desired_min_tx_nsec;
Klement Sekeraa57a9702017-02-02 06:58:07 +0100123 bs->remote_min_rx_usec = 1;
Klement Sekeraa3167442020-02-10 11:49:52 +0000124 bs->remote_min_rx_nsec = bfd_usec_to_nsec (bs->remote_min_rx_usec);
Klement Sekera239790f2017-02-16 10:53:53 +0100125 bs->remote_min_echo_rx_usec = 0;
Klement Sekeraa3167442020-02-10 11:49:52 +0000126 bs->remote_min_echo_rx_nsec = 0;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200127 bs->remote_demand = 0;
Klement Sekerab17dd962017-01-09 07:43:48 +0100128 bs->auth.remote_seq_number = 0;
129 bs->auth.remote_seq_number_known = 0;
130 bs->auth.local_seq_number = random_u32 (&bm->random_seed);
Klement Sekera239790f2017-02-16 10:53:53 +0100131 bs->echo_secret = random_u32 (&bm->random_seed);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200132}
133
134static void
135bfd_set_diag (bfd_session_t * bs, bfd_diag_code_e code)
136{
137 if (bs->local_diag != code)
138 {
139 BFD_DBG ("set local_diag, bs_idx=%d: '%d:%s'", bs->bs_idx, code,
140 bfd_diag_code_string (code));
141 bs->local_diag = code;
142 }
143}
144
145static void
Klement Sekeraa3167442020-02-10 11:49:52 +0000146bfd_set_state (vlib_main_t * vm, bfd_main_t * bm, bfd_session_t * bs,
Klement Sekerae4504c62016-12-08 10:16:41 +0100147 bfd_state_e new_state, int handling_wakeup)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200148{
149 if (bs->local_state != new_state)
150 {
151 BFD_DBG ("Change state, bs_idx=%d: %s->%s", bs->bs_idx,
152 bfd_state_string (bs->local_state),
153 bfd_state_string (new_state));
154 bs->local_state = new_state;
Klement Sekeraa3167442020-02-10 11:49:52 +0000155 bfd_on_state_change (bm, bs, bfd_time_now_nsec (vm, NULL),
156 handling_wakeup);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200157 }
158}
159
Klement Sekera73884482017-02-23 09:26:30 +0100160const char *
Klement Sekera239790f2017-02-16 10:53:53 +0100161bfd_poll_state_string (bfd_poll_state_e state)
162{
163 switch (state)
164 {
165#define F(x) \
166 case BFD_POLL_##x: \
167 return "BFD_POLL_" #x;
168 foreach_bfd_poll_state (F)
169#undef F
170 }
171 return "UNKNOWN";
172}
173
174static void
175bfd_set_poll_state (bfd_session_t * bs, bfd_poll_state_e state)
176{
177 if (bs->poll_state != state)
178 {
179 BFD_DBG ("Setting poll state=%s, bs_idx=%u",
180 bfd_poll_state_string (state), bs->bs_idx);
181 bs->poll_state = state;
182 }
183}
184
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200185static void
Klement Sekerab59f63b2022-02-14 20:19:28 +0000186bfd_recalc_tx_interval (bfd_session_t *bs)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200187{
Klement Sekeraa3167442020-02-10 11:49:52 +0000188 bs->transmit_interval_nsec =
189 clib_max (bs->effective_desired_min_tx_nsec, bs->remote_min_rx_nsec);
Klement Sekera239790f2017-02-16 10:53:53 +0100190 BFD_DBG ("Recalculated transmit interval " BFD_CLK_FMT,
Klement Sekeraa3167442020-02-10 11:49:52 +0000191 BFD_CLK_PRN (bs->transmit_interval_nsec));
Klement Sekera239790f2017-02-16 10:53:53 +0100192}
193
194static void
Klement Sekerab59f63b2022-02-14 20:19:28 +0000195bfd_recalc_echo_tx_interval (bfd_session_t *bs)
Klement Sekera239790f2017-02-16 10:53:53 +0100196{
Klement Sekeraa3167442020-02-10 11:49:52 +0000197 bs->echo_transmit_interval_nsec =
198 clib_max (bs->effective_desired_min_tx_nsec, bs->remote_min_echo_rx_nsec);
Klement Sekera239790f2017-02-16 10:53:53 +0100199 BFD_DBG ("Recalculated echo transmit interval " BFD_CLK_FMT,
Klement Sekeraa3167442020-02-10 11:49:52 +0000200 BFD_CLK_PRN (bs->echo_transmit_interval_nsec));
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200201}
202
203static void
204bfd_calc_next_tx (bfd_main_t * bm, bfd_session_t * bs, u64 now)
205{
Klement Sekera239790f2017-02-16 10:53:53 +0100206 if (bs->local_detect_mult > 1)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200207 {
Klement Sekera239790f2017-02-16 10:53:53 +0100208 /* common case - 75-100% of transmit interval */
Klement Sekeraa3167442020-02-10 11:49:52 +0000209 bs->tx_timeout_nsec = bs->last_tx_nsec +
Klement Sekera239790f2017-02-16 10:53:53 +0100210 (1 - .25 * (random_f64 (&bm->random_seed))) *
Klement Sekeraa3167442020-02-10 11:49:52 +0000211 bs->transmit_interval_nsec;
212 if (bs->tx_timeout_nsec < now)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200213 {
Klement Sekera239790f2017-02-16 10:53:53 +0100214 /*
215 * the timeout is in the past, which means that either remote
216 * demand mode was set or performance/clock issues ...
217 */
218 BFD_DBG ("Missed %lu transmit events (now is %lu, calc "
219 "tx_timeout is %lu)",
Klement Sekeraa3167442020-02-10 11:49:52 +0000220 (now - bs->tx_timeout_nsec) /
221 bs->transmit_interval_nsec, now, bs->tx_timeout_nsec);
222 bs->tx_timeout_nsec = now;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200223 }
224 }
225 else
226 {
Klement Sekera239790f2017-02-16 10:53:53 +0100227 /* special case - 75-90% of transmit interval */
Klement Sekeraa3167442020-02-10 11:49:52 +0000228 bs->tx_timeout_nsec = bs->last_tx_nsec +
Klement Sekera239790f2017-02-16 10:53:53 +0100229 (.9 - .15 * (random_f64 (&bm->random_seed))) *
Klement Sekeraa3167442020-02-10 11:49:52 +0000230 bs->transmit_interval_nsec;
231 if (bs->tx_timeout_nsec < now)
Klement Sekera239790f2017-02-16 10:53:53 +0100232 {
233 /*
234 * the timeout is in the past, which means that either remote
235 * demand mode was set or performance/clock issues ...
236 */
237 BFD_DBG ("Missed %lu transmit events (now is %lu, calc "
238 "tx_timeout is %lu)",
Klement Sekeraa3167442020-02-10 11:49:52 +0000239 (now - bs->tx_timeout_nsec) /
240 bs->transmit_interval_nsec, now, bs->tx_timeout_nsec);
241 bs->tx_timeout_nsec = now;
Klement Sekera239790f2017-02-16 10:53:53 +0100242 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200243 }
Klement Sekeraa3167442020-02-10 11:49:52 +0000244 if (bs->tx_timeout_nsec)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200245 {
Klement Sekeraa3167442020-02-10 11:49:52 +0000246 BFD_DBG ("Next transmit in %lu nsec/%.02fs@%lu",
247 bs->tx_timeout_nsec - now,
248 (bs->tx_timeout_nsec - now) * SEC_PER_NSEC,
249 bs->tx_timeout_nsec);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200250 }
251}
252
253static void
Klement Sekerab59f63b2022-02-14 20:19:28 +0000254bfd_calc_next_echo_tx (bfd_session_t *bs, u64 now)
Klement Sekera239790f2017-02-16 10:53:53 +0100255{
Klement Sekeraa3167442020-02-10 11:49:52 +0000256 bs->echo_tx_timeout_nsec =
257 bs->echo_last_tx_nsec + bs->echo_transmit_interval_nsec;
258 if (bs->echo_tx_timeout_nsec < now)
Klement Sekera239790f2017-02-16 10:53:53 +0100259 {
260 /* huh, we've missed it already, transmit now */
261 BFD_DBG ("Missed %lu echo transmit events (now is %lu, calc tx_timeout "
262 "is %lu)",
Klement Sekeraa3167442020-02-10 11:49:52 +0000263 (now - bs->echo_tx_timeout_nsec) /
264 bs->echo_transmit_interval_nsec,
265 now, bs->echo_tx_timeout_nsec);
266 bs->echo_tx_timeout_nsec = now;
Klement Sekera239790f2017-02-16 10:53:53 +0100267 }
Klement Sekeraa3167442020-02-10 11:49:52 +0000268 BFD_DBG ("Next echo transmit in %lu nsec/%.02fs@%lu",
269 bs->echo_tx_timeout_nsec - now,
270 (bs->echo_tx_timeout_nsec - now) * SEC_PER_NSEC,
271 bs->echo_tx_timeout_nsec);
Klement Sekera239790f2017-02-16 10:53:53 +0100272}
273
274static void
Klement Sekerab59f63b2022-02-14 20:19:28 +0000275bfd_recalc_detection_time (bfd_session_t *bs)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200276{
Klement Sekera73884482017-02-23 09:26:30 +0100277 if (bs->local_state == BFD_STATE_init || bs->local_state == BFD_STATE_up)
278 {
Klement Sekeraa3167442020-02-10 11:49:52 +0000279 bs->detection_time_nsec =
Klement Sekera73884482017-02-23 09:26:30 +0100280 bs->remote_detect_mult *
Klement Sekeraa3167442020-02-10 11:49:52 +0000281 clib_max (bs->effective_required_min_rx_nsec,
282 bs->remote_desired_min_tx_nsec);
283 BFD_DBG ("Recalculated detection time %lu nsec/%.3fs",
284 bs->detection_time_nsec,
285 bs->detection_time_nsec * SEC_PER_NSEC);
Klement Sekera73884482017-02-23 09:26:30 +0100286 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200287}
288
289static void
Klement Sekerae4504c62016-12-08 10:16:41 +0100290bfd_set_timer (bfd_main_t * bm, bfd_session_t * bs, u64 now,
291 int handling_wakeup)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200292{
293 u64 next = 0;
294 u64 rx_timeout = 0;
Klement Sekera239790f2017-02-16 10:53:53 +0100295 u64 tx_timeout = 0;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200296 if (BFD_STATE_up == bs->local_state)
297 {
Klement Sekeraa3167442020-02-10 11:49:52 +0000298 rx_timeout = bs->last_rx_nsec + bs->detection_time_nsec;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200299 }
Klement Sekera73884482017-02-23 09:26:30 +0100300 if (BFD_STATE_up != bs->local_state ||
301 (!bs->remote_demand && bs->remote_min_rx_usec) ||
Klement Sekera239790f2017-02-16 10:53:53 +0100302 BFD_POLL_NOT_NEEDED != bs->poll_state)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200303 {
Klement Sekeraa3167442020-02-10 11:49:52 +0000304 tx_timeout = bs->tx_timeout_nsec;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200305 }
Klement Sekera239790f2017-02-16 10:53:53 +0100306 if (tx_timeout && rx_timeout)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200307 {
Klement Sekera239790f2017-02-16 10:53:53 +0100308 next = clib_min (tx_timeout, rx_timeout);
309 }
310 else if (tx_timeout)
311 {
312 next = tx_timeout;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200313 }
314 else if (rx_timeout)
315 {
316 next = rx_timeout;
317 }
Klement Sekeraa3167442020-02-10 11:49:52 +0000318 if (bs->echo && next > bs->echo_tx_timeout_nsec)
Klement Sekera239790f2017-02-16 10:53:53 +0100319 {
Klement Sekeraa3167442020-02-10 11:49:52 +0000320 next = bs->echo_tx_timeout_nsec;
Klement Sekera239790f2017-02-16 10:53:53 +0100321 }
322 BFD_DBG ("bs_idx=%u, tx_timeout=%lu, echo_tx_timeout=%lu, rx_timeout=%lu, "
323 "next=%s",
Klement Sekeraa3167442020-02-10 11:49:52 +0000324 bs->bs_idx, tx_timeout, bs->echo_tx_timeout_nsec, rx_timeout,
Klement Sekera239790f2017-02-16 10:53:53 +0100325 next == tx_timeout
Klement Sekeraa3167442020-02-10 11:49:52 +0000326 ? "tx" : (next == bs->echo_tx_timeout_nsec ? "echo tx" : "rx"));
327 if (next)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200328 {
Dave Barach1e3417f2018-07-25 08:30:27 -0400329 int send_signal = 0;
Klement Sekeraa3167442020-02-10 11:49:52 +0000330 bs->event_time_nsec = next;
331 /* add extra tick if it's not even */
332 u32 wheel_time_ticks =
333 (bs->event_time_nsec - now) / bm->nsec_per_tw_tick +
334 ((bs->event_time_nsec - now) % bm->nsec_per_tw_tick != 0);
335 BFD_DBG ("event_time_nsec %lu (%lu nsec/%.3fs in future) -> "
336 "wheel_time_ticks %u", bs->event_time_nsec,
337 bs->event_time_nsec - now,
338 (bs->event_time_nsec - now) * SEC_PER_NSEC, wheel_time_ticks);
Dave Barachca578522020-04-08 08:45:27 -0400339 wheel_time_ticks = wheel_time_ticks ? wheel_time_ticks : 1;
Dave Barach1e3417f2018-07-25 08:30:27 -0400340 bfd_lock (bm);
Klement Sekeraa3167442020-02-10 11:49:52 +0000341 if (bs->tw_id)
342 {
343 TW (tw_timer_update) (&bm->wheel, bs->tw_id, wheel_time_ticks);
344 BFD_DBG ("tw_timer_update(%p, %u, %lu);", &bm->wheel, bs->tw_id,
345 wheel_time_ticks);
346 }
347 else
348 {
349 bs->tw_id =
350 TW (tw_timer_start) (&bm->wheel, bs->bs_idx, 0, wheel_time_ticks);
351 BFD_DBG ("tw_timer_start(%p, %u, 0, %lu) == %u;", &bm->wheel,
352 bs->bs_idx, wheel_time_ticks);
353 }
Dave Barach1e3417f2018-07-25 08:30:27 -0400354
Klement Sekerae4504c62016-12-08 10:16:41 +0100355 if (!handling_wakeup)
356 {
Dave Barach1e3417f2018-07-25 08:30:27 -0400357
358 /* Send only if it is earlier than current awaited wakeup time */
359 send_signal =
Klement Sekeraa3167442020-02-10 11:49:52 +0000360 (bs->event_time_nsec < bm->bfd_process_next_wakeup_nsec) &&
Dave Barach1e3417f2018-07-25 08:30:27 -0400361 /*
362 * If the wake-up time is within 2x the delay of the event propagation delay,
363 * avoid the expense of sending the event. The 2x multiplier is to workaround the race whereby
364 * simultaneous event + expired timer create one recurring bogus wakeup/suspend instance,
365 * due to double scheduling of the node on the pending list.
366 */
Klement Sekeraa3167442020-02-10 11:49:52 +0000367 (bm->bfd_process_next_wakeup_nsec - bs->event_time_nsec >
368 2 * bm->bfd_process_wakeup_event_delay_nsec) &&
Dave Barach1e3417f2018-07-25 08:30:27 -0400369 /* Must be no events in flight to send an event */
370 (!bm->bfd_process_wakeup_events_in_flight);
371
372 /* If we do send the signal, note this down along with the start timestamp */
373 if (send_signal)
374 {
375 bm->bfd_process_wakeup_events_in_flight++;
Klement Sekeraa3167442020-02-10 11:49:52 +0000376 bm->bfd_process_wakeup_event_start_nsec = now;
Dave Barach1e3417f2018-07-25 08:30:27 -0400377 }
378 }
379 bfd_unlock (bm);
380
381 /* Use the multithreaded event sending so the workers can send events too */
382 if (send_signal)
383 {
384 vlib_process_signal_event_mt (bm->vlib_main,
385 bm->bfd_process_node_index,
386 BFD_EVENT_RESCHEDULE, ~0);
Klement Sekerae4504c62016-12-08 10:16:41 +0100387 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200388 }
389}
390
391static void
Klement Sekeraa57a9702017-02-02 06:58:07 +0100392bfd_set_effective_desired_min_tx (bfd_main_t * bm,
393 bfd_session_t * bs, u64 now,
Klement Sekeraa3167442020-02-10 11:49:52 +0000394 u64 desired_min_tx_nsec)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200395{
Klement Sekeraa3167442020-02-10 11:49:52 +0000396 bs->effective_desired_min_tx_nsec = desired_min_tx_nsec;
Klement Sekeraa57a9702017-02-02 06:58:07 +0100397 BFD_DBG ("Set effective desired min tx to " BFD_CLK_FMT,
Klement Sekeraa3167442020-02-10 11:49:52 +0000398 BFD_CLK_PRN (bs->effective_desired_min_tx_nsec));
Klement Sekerab59f63b2022-02-14 20:19:28 +0000399 bfd_recalc_detection_time (bs);
400 bfd_recalc_tx_interval (bs);
401 bfd_recalc_echo_tx_interval (bs);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200402 bfd_calc_next_tx (bm, bs, now);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200403}
404
Klement Sekera637b9c42016-12-08 05:19:14 +0100405static void
Klement Sekerab59f63b2022-02-14 20:19:28 +0000406bfd_set_effective_required_min_rx (bfd_session_t *bs, u64 required_min_rx_nsec)
Klement Sekeraa57a9702017-02-02 06:58:07 +0100407{
Klement Sekeraa3167442020-02-10 11:49:52 +0000408 bs->effective_required_min_rx_nsec = required_min_rx_nsec;
Klement Sekeraa57a9702017-02-02 06:58:07 +0100409 BFD_DBG ("Set effective required min rx to " BFD_CLK_FMT,
Klement Sekeraa3167442020-02-10 11:49:52 +0000410 BFD_CLK_PRN (bs->effective_required_min_rx_nsec));
Klement Sekerab59f63b2022-02-14 20:19:28 +0000411 bfd_recalc_detection_time (bs);
Klement Sekeraa57a9702017-02-02 06:58:07 +0100412}
413
414static void
Klement Sekerab59f63b2022-02-14 20:19:28 +0000415bfd_set_remote_required_min_rx (bfd_session_t *bs,
416 u32 remote_required_min_rx_usec)
Klement Sekera637b9c42016-12-08 05:19:14 +0100417{
Klement Sekera239790f2017-02-16 10:53:53 +0100418 if (bs->remote_min_rx_usec != remote_required_min_rx_usec)
419 {
420 bs->remote_min_rx_usec = remote_required_min_rx_usec;
Klement Sekeraa3167442020-02-10 11:49:52 +0000421 bs->remote_min_rx_nsec = bfd_usec_to_nsec (remote_required_min_rx_usec);
Klement Sekera239790f2017-02-16 10:53:53 +0100422 BFD_DBG ("Set remote min rx to " BFD_CLK_FMT,
Klement Sekeraa3167442020-02-10 11:49:52 +0000423 BFD_CLK_PRN (bs->remote_min_rx_nsec));
Klement Sekerab59f63b2022-02-14 20:19:28 +0000424 bfd_recalc_detection_time (bs);
425 bfd_recalc_tx_interval (bs);
Klement Sekera239790f2017-02-16 10:53:53 +0100426 }
427}
428
429static void
Klement Sekerab59f63b2022-02-14 20:19:28 +0000430bfd_set_remote_required_min_echo_rx (bfd_session_t *bs,
Klement Sekera239790f2017-02-16 10:53:53 +0100431 u32 remote_required_min_echo_rx_usec)
432{
433 if (bs->remote_min_echo_rx_usec != remote_required_min_echo_rx_usec)
434 {
435 bs->remote_min_echo_rx_usec = remote_required_min_echo_rx_usec;
Klement Sekeraa3167442020-02-10 11:49:52 +0000436 bs->remote_min_echo_rx_nsec =
437 bfd_usec_to_nsec (bs->remote_min_echo_rx_usec);
Klement Sekera239790f2017-02-16 10:53:53 +0100438 BFD_DBG ("Set remote min echo rx to " BFD_CLK_FMT,
Klement Sekeraa3167442020-02-10 11:49:52 +0000439 BFD_CLK_PRN (bs->remote_min_echo_rx_nsec));
Klement Sekerab59f63b2022-02-14 20:19:28 +0000440 bfd_recalc_echo_tx_interval (bs);
Klement Sekera239790f2017-02-16 10:53:53 +0100441 }
Klement Sekera637b9c42016-12-08 05:19:14 +0100442}
443
Neale Ranns88fc83e2017-04-05 08:11:14 -0700444static void
445bfd_notify_listeners (bfd_main_t * bm,
446 bfd_listen_event_e event, const bfd_session_t * bs)
447{
448 bfd_notify_fn_t *fn;
449 vec_foreach (fn, bm->listeners)
450 {
451 (*fn) (event, bs);
452 }
453}
454
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200455void
456bfd_session_start (bfd_main_t * bm, bfd_session_t * bs)
457{
Klement Sekera239790f2017-02-16 10:53:53 +0100458 BFD_DBG ("\nStarting session: %U", format_bfd_session, bs);
Damjan Marion07a38572018-01-21 06:44:18 -0800459 vlib_log_info (bm->log_class, "start BFD session: %U",
460 format_bfd_session_brief, bs);
Klement Sekerab59f63b2022-02-14 20:19:28 +0000461 bfd_set_effective_required_min_rx (bs, bs->config_required_min_rx_nsec);
462 bfd_recalc_tx_interval (bs);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200463 vlib_process_signal_event (bm->vlib_main, bm->bfd_process_node_index,
464 BFD_EVENT_NEW_SESSION, bs->bs_idx);
Neale Ranns88fc83e2017-04-05 08:11:14 -0700465 bfd_notify_listeners (bm, BFD_LISTEN_EVENT_CREATE, bs);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200466}
467
Klement Sekerab17dd962017-01-09 07:43:48 +0100468void
Neale Ranns32fd89b2022-02-15 08:28:19 +0000469bfd_session_stop (bfd_main_t *bm, bfd_session_t *bs)
470{
471 BFD_DBG ("\nStopping session: %U", format_bfd_session, bs);
472 bfd_notify_listeners (bm, BFD_LISTEN_EVENT_DELETE, bs);
473}
474
475void
Klement Sekeraa3167442020-02-10 11:49:52 +0000476bfd_session_set_flags (vlib_main_t * vm, bfd_session_t * bs, u8 admin_up_down)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200477{
478 bfd_main_t *bm = &bfd_main;
Klement Sekeraa3167442020-02-10 11:49:52 +0000479 u64 now = bfd_time_now_nsec (vm, NULL);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200480 if (admin_up_down)
481 {
Klement Sekerac48829b2017-02-14 07:55:57 +0100482 BFD_DBG ("Session set admin-up, bs-idx=%u", bs->bs_idx);
Damjan Marion07a38572018-01-21 06:44:18 -0800483 vlib_log_info (bm->log_class, "set session admin-up: %U",
484 format_bfd_session_brief, bs);
Klement Sekeraa3167442020-02-10 11:49:52 +0000485 bfd_set_state (vm, bm, bs, BFD_STATE_down, 0);
Klement Sekerac48829b2017-02-14 07:55:57 +0100486 bfd_set_diag (bs, BFD_DIAG_CODE_no_diag);
Klement Sekera73884482017-02-23 09:26:30 +0100487 bfd_calc_next_tx (bm, bs, now);
488 bfd_set_timer (bm, bs, now, 0);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200489 }
490 else
491 {
Klement Sekerac48829b2017-02-14 07:55:57 +0100492 BFD_DBG ("Session set admin-down, bs-idx=%u", bs->bs_idx);
Damjan Marion07a38572018-01-21 06:44:18 -0800493 vlib_log_info (bm->log_class, "set session admin-down: %U",
494 format_bfd_session_brief, bs);
Klement Sekerac48829b2017-02-14 07:55:57 +0100495 bfd_set_diag (bs, BFD_DIAG_CODE_admin_down);
Klement Sekeraa3167442020-02-10 11:49:52 +0000496 bfd_set_state (vm, bm, bs, BFD_STATE_admin_down, 0);
Klement Sekera73884482017-02-23 09:26:30 +0100497 bfd_calc_next_tx (bm, bs, now);
498 bfd_set_timer (bm, bs, now, 0);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200499 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200500}
501
502u8 *
503bfd_input_format_trace (u8 * s, va_list * args)
504{
505 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
506 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
507 const bfd_input_trace_t *t = va_arg (*args, bfd_input_trace_t *);
508 const bfd_pkt_t *pkt = (bfd_pkt_t *) t->data;
509 if (t->len > STRUCT_SIZE_OF (bfd_pkt_t, head))
510 {
511 s = format (s, "BFD v%u, diag=%u(%s), state=%u(%s),\n"
Klement Sekerab17dd962017-01-09 07:43:48 +0100512 " flags=(P:%u, F:%u, C:%u, A:%u, D:%u, M:%u), "
513 "detect_mult=%u, length=%u\n",
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200514 bfd_pkt_get_version (pkt), bfd_pkt_get_diag_code (pkt),
515 bfd_diag_code_string (bfd_pkt_get_diag_code (pkt)),
516 bfd_pkt_get_state (pkt),
517 bfd_state_string (bfd_pkt_get_state (pkt)),
518 bfd_pkt_get_poll (pkt), bfd_pkt_get_final (pkt),
519 bfd_pkt_get_control_plane_independent (pkt),
520 bfd_pkt_get_auth_present (pkt), bfd_pkt_get_demand (pkt),
521 bfd_pkt_get_multipoint (pkt), pkt->head.detect_mult,
522 pkt->head.length);
Klement Sekerab17dd962017-01-09 07:43:48 +0100523 if (t->len >= sizeof (bfd_pkt_t) &&
524 pkt->head.length >= sizeof (bfd_pkt_t))
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200525 {
Klement Sekerad3ba5152017-02-14 03:09:17 +0100526 s = format (s, " my discriminator: %u\n",
527 clib_net_to_host_u32 (pkt->my_disc));
528 s = format (s, " your discriminator: %u\n",
529 clib_net_to_host_u32 (pkt->your_disc));
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200530 s = format (s, " desired min tx interval: %u\n",
531 clib_net_to_host_u32 (pkt->des_min_tx));
532 s = format (s, " required min rx interval: %u\n",
533 clib_net_to_host_u32 (pkt->req_min_rx));
Klement Sekera46a87ad2017-01-02 08:22:23 +0100534 s = format (s, " required min echo rx interval: %u",
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200535 clib_net_to_host_u32 (pkt->req_min_echo_rx));
536 }
Klement Sekerad3ba5152017-02-14 03:09:17 +0100537 if (t->len >= sizeof (bfd_pkt_with_common_auth_t) &&
538 pkt->head.length >= sizeof (bfd_pkt_with_common_auth_t) &&
539 bfd_pkt_get_auth_present (pkt))
540 {
541 const bfd_pkt_with_common_auth_t *with_auth = (void *) pkt;
542 const bfd_auth_common_t *common = &with_auth->common_auth;
543 s = format (s, "\n auth len: %u\n", common->len);
544 s = format (s, " auth type: %u:%s\n", common->type,
545 bfd_auth_type_str (common->type));
546 if (t->len >= sizeof (bfd_pkt_with_sha1_auth_t) &&
547 pkt->head.length >= sizeof (bfd_pkt_with_sha1_auth_t) &&
548 (BFD_AUTH_TYPE_keyed_sha1 == common->type ||
549 BFD_AUTH_TYPE_meticulous_keyed_sha1 == common->type))
550 {
551 const bfd_pkt_with_sha1_auth_t *with_sha1 = (void *) pkt;
552 const bfd_auth_sha1_t *sha1 = &with_sha1->sha1_auth;
553 s = format (s, " seq num: %u\n",
554 clib_net_to_host_u32 (sha1->seq_num));
555 s = format (s, " key id: %u\n", sha1->key_id);
556 s = format (s, " hash: %U", format_hex_bytes, sha1->hash,
557 sizeof (sha1->hash));
558 }
559 }
560 else
561 {
562 s = format (s, "\n");
563 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200564 }
565
566 return s;
567}
568
Dave Barach1e3417f2018-07-25 08:30:27 -0400569typedef struct
570{
571 u32 bs_idx;
572} bfd_rpc_event_t;
573
574static void
575bfd_rpc_event_cb (const bfd_rpc_event_t * a)
576{
577 bfd_main_t *bm = &bfd_main;
578 u32 bs_idx = a->bs_idx;
579 u32 valid_bs = 0;
580 bfd_session_t session_data;
581
582 bfd_lock (bm);
583 if (!pool_is_free_index (bm->sessions, bs_idx))
584 {
585 bfd_session_t *bs = pool_elt_at_index (bm->sessions, bs_idx);
586 clib_memcpy (&session_data, bs, sizeof (bfd_session_t));
587 valid_bs = 1;
588 }
589 else
590 {
591 BFD_DBG ("Ignoring event RPC for non-existent session index %u",
592 bs_idx);
593 }
594 bfd_unlock (bm);
595
596 if (valid_bs)
597 bfd_event (bm, &session_data);
598}
599
600static void
601bfd_event_rpc (u32 bs_idx)
602{
603 const u32 data_size = sizeof (bfd_rpc_event_t);
604 u8 data[data_size];
605 bfd_rpc_event_t *event = (bfd_rpc_event_t *) data;
606
607 event->bs_idx = bs_idx;
608 vl_api_rpc_call_main_thread (bfd_rpc_event_cb, data, data_size);
609}
610
611typedef struct
612{
613 u32 bs_idx;
614} bfd_rpc_notify_listeners_t;
615
616static void
617bfd_rpc_notify_listeners_cb (const bfd_rpc_notify_listeners_t * a)
618{
619 bfd_main_t *bm = &bfd_main;
620 u32 bs_idx = a->bs_idx;
621 bfd_lock (bm);
622 if (!pool_is_free_index (bm->sessions, bs_idx))
623 {
624 bfd_session_t *bs = pool_elt_at_index (bm->sessions, bs_idx);
625 bfd_notify_listeners (bm, BFD_LISTEN_EVENT_UPDATE, bs);
626 }
627 else
628 {
629 BFD_DBG ("Ignoring notify RPC for non-existent session index %u",
630 bs_idx);
631 }
632 bfd_unlock (bm);
633}
634
635static void
636bfd_notify_listeners_rpc (u32 bs_idx)
637{
638 const u32 data_size = sizeof (bfd_rpc_notify_listeners_t);
639 u8 data[data_size];
640 bfd_rpc_notify_listeners_t *notify = (bfd_rpc_notify_listeners_t *) data;
641 notify->bs_idx = bs_idx;
642 vl_api_rpc_call_main_thread (bfd_rpc_notify_listeners_cb, data, data_size);
643}
644
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200645static void
Klement Sekerae4504c62016-12-08 10:16:41 +0100646bfd_on_state_change (bfd_main_t * bm, bfd_session_t * bs, u64 now,
647 int handling_wakeup)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200648{
Klement Sekera239790f2017-02-16 10:53:53 +0100649 BFD_DBG ("\nState changed: %U", format_bfd_session, bs);
Dave Barach1e3417f2018-07-25 08:30:27 -0400650
651 if (vlib_get_thread_index () == 0)
652 {
653 bfd_event (bm, bs);
654 }
655 else
656 {
657 /* without RPC - a REGRESSION: BFD event are not propagated */
658 bfd_event_rpc (bs->bs_idx);
659 }
660
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200661 switch (bs->local_state)
662 {
663 case BFD_STATE_admin_down:
Klement Sekera239790f2017-02-16 10:53:53 +0100664 bs->echo = 0;
Klement Sekeraa57a9702017-02-02 06:58:07 +0100665 bfd_set_effective_desired_min_tx (bm, bs, now,
666 clib_max
Klement Sekeraa3167442020-02-10 11:49:52 +0000667 (bs->config_desired_min_tx_nsec,
668 bm->default_desired_min_tx_nsec));
Klement Sekerab59f63b2022-02-14 20:19:28 +0000669 bfd_set_effective_required_min_rx (bs, bs->config_required_min_rx_nsec);
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100670 bfd_set_timer (bm, bs, now, handling_wakeup);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200671 break;
672 case BFD_STATE_down:
Klement Sekera239790f2017-02-16 10:53:53 +0100673 bs->echo = 0;
Klement Sekeraa57a9702017-02-02 06:58:07 +0100674 bfd_set_effective_desired_min_tx (bm, bs, now,
675 clib_max
Klement Sekeraa3167442020-02-10 11:49:52 +0000676 (bs->config_desired_min_tx_nsec,
677 bm->default_desired_min_tx_nsec));
Klement Sekerab59f63b2022-02-14 20:19:28 +0000678 bfd_set_effective_required_min_rx (bs, bs->config_required_min_rx_nsec);
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100679 bfd_set_timer (bm, bs, now, handling_wakeup);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200680 break;
681 case BFD_STATE_init:
Klement Sekera239790f2017-02-16 10:53:53 +0100682 bs->echo = 0;
Klement Sekeraa57a9702017-02-02 06:58:07 +0100683 bfd_set_effective_desired_min_tx (bm, bs, now,
Klement Sekeraa3167442020-02-10 11:49:52 +0000684 bs->config_desired_min_tx_nsec);
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100685 bfd_set_timer (bm, bs, now, handling_wakeup);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200686 break;
687 case BFD_STATE_up:
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100688 bfd_set_effective_desired_min_tx (bm, bs, now,
Klement Sekeraa3167442020-02-10 11:49:52 +0000689 bs->config_desired_min_tx_nsec);
Klement Sekera239790f2017-02-16 10:53:53 +0100690 if (BFD_POLL_NOT_NEEDED == bs->poll_state)
Klement Sekeraa57a9702017-02-02 06:58:07 +0100691 {
Klement Sekerab59f63b2022-02-14 20:19:28 +0000692 bfd_set_effective_required_min_rx (bs,
Klement Sekeraa3167442020-02-10 11:49:52 +0000693 bs->config_required_min_rx_nsec);
Klement Sekeraa57a9702017-02-02 06:58:07 +0100694 }
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100695 bfd_set_timer (bm, bs, now, handling_wakeup);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200696 break;
697 }
Dave Barach1e3417f2018-07-25 08:30:27 -0400698 if (vlib_get_thread_index () == 0)
699 {
700 bfd_notify_listeners (bm, BFD_LISTEN_EVENT_UPDATE, bs);
701 }
702 else
703 {
704 /* without RPC - a REGRESSION: state changes are not propagated */
705 bfd_notify_listeners_rpc (bs->bs_idx);
706 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200707}
708
709static void
Klement Sekerab59f63b2022-02-14 20:19:28 +0000710bfd_on_config_change (bfd_main_t *bm, bfd_session_t *bs, u64 now)
Klement Sekeraa57a9702017-02-02 06:58:07 +0100711{
Klement Sekera239790f2017-02-16 10:53:53 +0100712 /*
713 * if remote demand mode is set and we need to do a poll, set the next
714 * timeout so that the session wakes up immediately
715 */
716 if (bs->remote_demand && BFD_POLL_NEEDED == bs->poll_state &&
Klement Sekeraa3167442020-02-10 11:49:52 +0000717 bs->poll_state_start_or_timeout_nsec < now)
Klement Sekeraa57a9702017-02-02 06:58:07 +0100718 {
Klement Sekeraa3167442020-02-10 11:49:52 +0000719 bs->tx_timeout_nsec = now;
Klement Sekeraa57a9702017-02-02 06:58:07 +0100720 }
Klement Sekerab59f63b2022-02-14 20:19:28 +0000721 bfd_recalc_detection_time (bs);
Klement Sekeraa57a9702017-02-02 06:58:07 +0100722 bfd_set_timer (bm, bs, now, 0);
723}
724
725static void
Klement Sekerae50e8562017-04-04 16:19:48 +0200726bfd_add_transport_layer (vlib_main_t * vm, u32 bi, bfd_session_t * bs)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200727{
728 switch (bs->transport)
729 {
730 case BFD_TRANSPORT_UDP4:
Klement Sekera46a87ad2017-01-02 08:22:23 +0100731 BFD_DBG ("Transport bfd via udp4, bs_idx=%u", bs->bs_idx);
Klement Sekerae50e8562017-04-04 16:19:48 +0200732 bfd_add_udp4_transport (vm, bi, bs, 0 /* is_echo */ );
Klement Sekera46a87ad2017-01-02 08:22:23 +0100733 break;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200734 case BFD_TRANSPORT_UDP6:
Klement Sekera46a87ad2017-01-02 08:22:23 +0100735 BFD_DBG ("Transport bfd via udp6, bs_idx=%u", bs->bs_idx);
Klement Sekerae50e8562017-04-04 16:19:48 +0200736 bfd_add_udp6_transport (vm, bi, bs, 0 /* is_echo */ );
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200737 break;
738 }
739}
740
Klement Sekera239790f2017-02-16 10:53:53 +0100741static int
Klement Sekerae50e8562017-04-04 16:19:48 +0200742bfd_transport_control_frame (vlib_main_t * vm, u32 bi, bfd_session_t * bs)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200743{
Klement Sekera239790f2017-02-16 10:53:53 +0100744 switch (bs->transport)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200745 {
Klement Sekera239790f2017-02-16 10:53:53 +0100746 case BFD_TRANSPORT_UDP4:
Klement Sekerae50e8562017-04-04 16:19:48 +0200747 BFD_DBG ("Transport bfd via udp4, bs_idx=%u", bs->bs_idx);
Klement Sekeracdaf0d82022-02-14 20:20:22 +0000748 return bfd_transport_udp4 (vm, bi, bs, 0 /* is_echo */);
Klement Sekera239790f2017-02-16 10:53:53 +0100749 break;
750 case BFD_TRANSPORT_UDP6:
Klement Sekerae50e8562017-04-04 16:19:48 +0200751 BFD_DBG ("Transport bfd via udp6, bs_idx=%u", bs->bs_idx);
Klement Sekeracdaf0d82022-02-14 20:20:22 +0000752 return bfd_transport_udp6 (vm, bi, bs, 0 /* is_echo */);
Klement Sekera239790f2017-02-16 10:53:53 +0100753 break;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200754 }
Klement Sekera239790f2017-02-16 10:53:53 +0100755 return 0;
756}
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200757
Klement Sekerae50e8562017-04-04 16:19:48 +0200758static int
759bfd_echo_add_transport_layer (vlib_main_t * vm, u32 bi, bfd_session_t * bs)
Klement Sekera239790f2017-02-16 10:53:53 +0100760{
Klement Sekerae50e8562017-04-04 16:19:48 +0200761 switch (bs->transport)
762 {
763 case BFD_TRANSPORT_UDP4:
764 BFD_DBG ("Transport bfd echo via udp4, bs_idx=%u", bs->bs_idx);
765 return bfd_add_udp4_transport (vm, bi, bs, 1 /* is_echo */ );
766 break;
767 case BFD_TRANSPORT_UDP6:
768 BFD_DBG ("Transport bfd echo via udp6, bs_idx=%u", bs->bs_idx);
769 return bfd_add_udp6_transport (vm, bi, bs, 1 /* is_echo */ );
770 break;
771 }
772 return 0;
773}
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200774
Klement Sekerae50e8562017-04-04 16:19:48 +0200775static int
776bfd_transport_echo (vlib_main_t * vm, u32 bi, bfd_session_t * bs)
777{
778 switch (bs->transport)
779 {
780 case BFD_TRANSPORT_UDP4:
781 BFD_DBG ("Transport bfd echo via udp4, bs_idx=%u", bs->bs_idx);
Klement Sekeracdaf0d82022-02-14 20:20:22 +0000782 return bfd_transport_udp4 (vm, bi, bs, 1 /* is_echo */);
Klement Sekerae50e8562017-04-04 16:19:48 +0200783 break;
784 case BFD_TRANSPORT_UDP6:
785 BFD_DBG ("Transport bfd echo via udp6, bs_idx=%u", bs->bs_idx);
Klement Sekeracdaf0d82022-02-14 20:20:22 +0000786 return bfd_transport_udp6 (vm, bi, bs, 1 /* is_echo */);
Klement Sekerae50e8562017-04-04 16:19:48 +0200787 break;
788 }
789 return 0;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200790}
791
Klement Sekerab17dd962017-01-09 07:43:48 +0100792static void
Klement Sekeraefd4d702021-04-20 19:21:36 +0200793bfd_add_sha1_auth_section (vlib_main_t *vm, vlib_buffer_t *b,
794 bfd_session_t *bs)
Klement Sekerab17dd962017-01-09 07:43:48 +0100795{
796 bfd_pkt_with_sha1_auth_t *pkt = vlib_buffer_get_current (b);
797 bfd_auth_sha1_t *auth = &pkt->sha1_auth;
798 b->current_length += sizeof (*auth);
799 pkt->pkt.head.length += sizeof (*auth);
800 bfd_pkt_set_auth_present (&pkt->pkt);
Dave Barachb7b92992018-10-17 10:38:51 -0400801 clib_memset (auth, 0, sizeof (*auth));
Klement Sekerab17dd962017-01-09 07:43:48 +0100802 auth->type_len.type = bs->auth.curr_key->auth_type;
803 /*
804 * only meticulous authentication types require incrementing seq number
805 * for every message, but doing so doesn't violate the RFC
806 */
807 ++bs->auth.local_seq_number;
808 auth->type_len.len = sizeof (bfd_auth_sha1_t);
809 auth->key_id = bs->auth.curr_bfd_key_id;
810 auth->seq_num = clib_host_to_net_u32 (bs->auth.local_seq_number);
811 /*
812 * first copy the password into the packet, then calculate the hash
813 * and finally replace the password with the calculated hash
814 */
815 clib_memcpy (auth->hash, bs->auth.curr_key->key,
816 sizeof (bs->auth.curr_key->key));
817 unsigned char hash[sizeof (auth->hash)];
Klement Sekeraefd4d702021-04-20 19:21:36 +0200818
819 vnet_crypto_op_t op;
820 vnet_crypto_op_init (&op, VNET_CRYPTO_OP_SHA1_HASH);
821 op.src = (u8 *) pkt;
822 op.len = sizeof (*pkt);
823 op.digest = hash;
824 vnet_crypto_process_ops (vm, &op, 1);
Klement Sekerab17dd962017-01-09 07:43:48 +0100825 BFD_DBG ("hashing: %U", format_hex_bytes, pkt, sizeof (*pkt));
826 clib_memcpy (auth->hash, hash, sizeof (hash));
Klement Sekerab17dd962017-01-09 07:43:48 +0100827}
828
829static void
Klement Sekeraefd4d702021-04-20 19:21:36 +0200830bfd_add_auth_section (vlib_main_t *vm, vlib_buffer_t *b, bfd_session_t *bs)
Klement Sekerab17dd962017-01-09 07:43:48 +0100831{
Damjan Marion07a38572018-01-21 06:44:18 -0800832 bfd_main_t *bm = &bfd_main;
Klement Sekerab17dd962017-01-09 07:43:48 +0100833 if (bs->auth.curr_key)
834 {
835 const bfd_auth_type_e auth_type = bs->auth.curr_key->auth_type;
836 switch (auth_type)
837 {
838 case BFD_AUTH_TYPE_reserved:
839 /* fallthrough */
840 case BFD_AUTH_TYPE_simple_password:
841 /* fallthrough */
842 case BFD_AUTH_TYPE_keyed_md5:
843 /* fallthrough */
844 case BFD_AUTH_TYPE_meticulous_keyed_md5:
Damjan Marion07a38572018-01-21 06:44:18 -0800845 vlib_log_crit (bm->log_class,
846 "internal error, unexpected BFD auth type '%d'",
847 auth_type);
Klement Sekerab17dd962017-01-09 07:43:48 +0100848 break;
Klement Sekerab17dd962017-01-09 07:43:48 +0100849 case BFD_AUTH_TYPE_keyed_sha1:
850 /* fallthrough */
851 case BFD_AUTH_TYPE_meticulous_keyed_sha1:
Klement Sekeraefd4d702021-04-20 19:21:36 +0200852 bfd_add_sha1_auth_section (vm, b, bs);
Klement Sekerab17dd962017-01-09 07:43:48 +0100853 break;
Klement Sekerab17dd962017-01-09 07:43:48 +0100854 }
855 }
856}
857
Klement Sekera239790f2017-02-16 10:53:53 +0100858static int
859bfd_is_echo_possible (bfd_session_t * bs)
860{
861 if (BFD_STATE_up == bs->local_state && BFD_STATE_up == bs->remote_state &&
862 bs->remote_min_echo_rx_usec > 0)
863 {
864 switch (bs->transport)
865 {
866 case BFD_TRANSPORT_UDP4:
867 return bfd_udp_is_echo_available (BFD_TRANSPORT_UDP4);
868 case BFD_TRANSPORT_UDP6:
869 return bfd_udp_is_echo_available (BFD_TRANSPORT_UDP6);
870 }
871 }
872 return 0;
873}
874
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200875static void
Klement Sekerab59f63b2022-02-14 20:19:28 +0000876bfd_init_control_frame (bfd_session_t *bs, vlib_buffer_t *b)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200877{
878 bfd_pkt_t *pkt = vlib_buffer_get_current (b);
Klement Sekerab17dd962017-01-09 07:43:48 +0100879 u32 bfd_length = 0;
880 bfd_length = sizeof (bfd_pkt_t);
Dave Barachb7b92992018-10-17 10:38:51 -0400881 clib_memset (pkt, 0, sizeof (*pkt));
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200882 bfd_pkt_set_version (pkt, 1);
883 bfd_pkt_set_diag_code (pkt, bs->local_diag);
884 bfd_pkt_set_state (pkt, bs->local_state);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200885 pkt->head.detect_mult = bs->local_detect_mult;
Klement Sekerae50e8562017-04-04 16:19:48 +0200886 pkt->head.length = bfd_length;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200887 pkt->my_disc = bs->local_discr;
888 pkt->your_disc = bs->remote_discr;
Klement Sekeraa57a9702017-02-02 06:58:07 +0100889 pkt->des_min_tx = clib_host_to_net_u32 (bs->config_desired_min_tx_usec);
Klement Sekera239790f2017-02-16 10:53:53 +0100890 if (bs->echo)
891 {
892 pkt->req_min_rx =
Klement Sekeraa3167442020-02-10 11:49:52 +0000893 clib_host_to_net_u32 (bfd_nsec_to_usec
894 (bs->effective_required_min_rx_nsec));
Klement Sekera239790f2017-02-16 10:53:53 +0100895 }
896 else
897 {
898 pkt->req_min_rx =
899 clib_host_to_net_u32 (bs->config_required_min_rx_usec);
900 }
Klement Sekeraa57a9702017-02-02 06:58:07 +0100901 pkt->req_min_echo_rx = clib_host_to_net_u32 (1);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200902 b->current_length = bfd_length;
903}
904
905static void
Klement Sekerab59f63b2022-02-14 20:19:28 +0000906bfd_send_echo (vlib_main_t *vm, bfd_main_t *bm, bfd_session_t *bs, u64 now)
Klement Sekera239790f2017-02-16 10:53:53 +0100907{
908 if (!bfd_is_echo_possible (bs))
909 {
910 BFD_DBG ("\nSwitching off echo function: %U", format_bfd_session, bs);
911 bs->echo = 0;
912 return;
913 }
Klement Sekeraa3167442020-02-10 11:49:52 +0000914 if (now >= bs->echo_tx_timeout_nsec)
Klement Sekera239790f2017-02-16 10:53:53 +0100915 {
916 BFD_DBG ("\nSending echo packet: %U", format_bfd_session, bs);
917 u32 bi;
918 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
919 {
Damjan Marion07a38572018-01-21 06:44:18 -0800920 vlib_log_crit (bm->log_class, "buffer allocation failure");
Klement Sekera239790f2017-02-16 10:53:53 +0100921 return;
922 }
923 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
924 ASSERT (b->current_data == 0);
925 bfd_echo_pkt_t *pkt = vlib_buffer_get_current (b);
Dave Barachb7b92992018-10-17 10:38:51 -0400926 clib_memset (pkt, 0, sizeof (*pkt));
Klement Sekera239790f2017-02-16 10:53:53 +0100927 pkt->discriminator = bs->local_discr;
Klement Sekeraa3167442020-02-10 11:49:52 +0000928 pkt->expire_time_nsec =
929 now + bs->echo_transmit_interval_nsec * bs->local_detect_mult;
Klement Sekera239790f2017-02-16 10:53:53 +0100930 pkt->checksum =
Klement Sekeraa3167442020-02-10 11:49:52 +0000931 bfd_calc_echo_checksum (bs->local_discr, pkt->expire_time_nsec,
Klement Sekera239790f2017-02-16 10:53:53 +0100932 bs->echo_secret);
933 b->current_length = sizeof (*pkt);
Klement Sekerae50e8562017-04-04 16:19:48 +0200934 if (!bfd_echo_add_transport_layer (vm, bi, bs))
935 {
936 BFD_ERR ("cannot send echo packet out, turning echo off");
937 bs->echo = 0;
938 vlib_buffer_free_one (vm, bi);
939 return;
940 }
941 if (!bfd_transport_echo (vm, bi, bs))
Klement Sekera239790f2017-02-16 10:53:53 +0100942 {
943 BFD_ERR ("cannot send echo packet out, turning echo off");
944 bs->echo = 0;
945 vlib_buffer_free_one (vm, bi);
946 return;
947 }
Klement Sekeraa3167442020-02-10 11:49:52 +0000948 bs->echo_last_tx_nsec = now;
Klement Sekerab59f63b2022-02-14 20:19:28 +0000949 bfd_calc_next_echo_tx (bs, now);
Klement Sekera239790f2017-02-16 10:53:53 +0100950 }
951 else
952 {
953 BFD_DBG
954 ("No need to send echo packet now, now is %lu, tx_timeout is %lu",
Klement Sekeraa3167442020-02-10 11:49:52 +0000955 now, bs->echo_tx_timeout_nsec);
Klement Sekera239790f2017-02-16 10:53:53 +0100956 }
957}
958
959static void
Klement Sekerab59f63b2022-02-14 20:19:28 +0000960bfd_send_periodic (vlib_main_t *vm, bfd_main_t *bm, bfd_session_t *bs, u64 now)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200961{
Klement Sekera239790f2017-02-16 10:53:53 +0100962 if (!bs->remote_min_rx_usec && BFD_POLL_NOT_NEEDED == bs->poll_state)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200963 {
Klement Sekera239790f2017-02-16 10:53:53 +0100964 BFD_DBG ("Remote min rx interval is zero, not sending periodic control "
965 "frame");
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200966 return;
967 }
Klement Sekera239790f2017-02-16 10:53:53 +0100968 if (BFD_POLL_NOT_NEEDED == bs->poll_state && bs->remote_demand &&
Klement Sekerad3ba5152017-02-14 03:09:17 +0100969 BFD_STATE_up == bs->local_state && BFD_STATE_up == bs->remote_state)
970 {
971 /*
972 * A system MUST NOT periodically transmit BFD Control packets if Demand
973 * mode is active on the remote system (bfd.RemoteDemandMode is 1,
974 * bfd.SessionState is Up, and bfd.RemoteSessionState is Up) and a Poll
975 * Sequence is not being transmitted.
976 */
Klement Sekera239790f2017-02-16 10:53:53 +0100977 BFD_DBG ("Remote demand is set, not sending periodic control frame");
Klement Sekerad3ba5152017-02-14 03:09:17 +0100978 return;
979 }
Klement Sekeraa3167442020-02-10 11:49:52 +0000980 if (now >= bs->tx_timeout_nsec)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200981 {
Klement Sekera239790f2017-02-16 10:53:53 +0100982 BFD_DBG ("\nSending periodic control frame: %U", format_bfd_session,
983 bs);
984 u32 bi;
985 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200986 {
Damjan Marion07a38572018-01-21 06:44:18 -0800987 vlib_log_crit (bm->log_class, "buffer allocation failure");
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200988 return;
989 }
Klement Sekera239790f2017-02-16 10:53:53 +0100990 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
991 ASSERT (b->current_data == 0);
Klement Sekerab59f63b2022-02-14 20:19:28 +0000992 bfd_init_control_frame (bs, b);
Klement Sekera239790f2017-02-16 10:53:53 +0100993 switch (bs->poll_state)
Klement Sekeraa57a9702017-02-02 06:58:07 +0100994 {
Klement Sekera239790f2017-02-16 10:53:53 +0100995 case BFD_POLL_NEEDED:
Klement Sekeraa3167442020-02-10 11:49:52 +0000996 if (now < bs->poll_state_start_or_timeout_nsec)
Klement Sekera239790f2017-02-16 10:53:53 +0100997 {
Klement Sekeraa3167442020-02-10 11:49:52 +0000998 BFD_DBG ("Cannot start a poll sequence yet, need to wait for "
999 BFD_CLK_FMT,
1000 BFD_CLK_PRN (bs->poll_state_start_or_timeout_nsec -
Klement Sekera239790f2017-02-16 10:53:53 +01001001 now));
1002 break;
1003 }
Klement Sekeraa3167442020-02-10 11:49:52 +00001004 bs->poll_state_start_or_timeout_nsec = now;
Klement Sekera239790f2017-02-16 10:53:53 +01001005 bfd_set_poll_state (bs, BFD_POLL_IN_PROGRESS);
1006 /* fallthrough */
1007 case BFD_POLL_IN_PROGRESS:
1008 case BFD_POLL_IN_PROGRESS_AND_QUEUED:
Klement Sekeraa57a9702017-02-02 06:58:07 +01001009 bfd_pkt_set_poll (vlib_buffer_get_current (b));
Klement Sekeraa57a9702017-02-02 06:58:07 +01001010 BFD_DBG ("Setting poll bit in packet, bs_idx=%u", bs->bs_idx);
Klement Sekera239790f2017-02-16 10:53:53 +01001011 break;
1012 case BFD_POLL_NOT_NEEDED:
1013 /* fallthrough */
1014 break;
Klement Sekeraa57a9702017-02-02 06:58:07 +01001015 }
Klement Sekeraefd4d702021-04-20 19:21:36 +02001016 bfd_add_auth_section (vm, b, bs);
Klement Sekerae50e8562017-04-04 16:19:48 +02001017 bfd_add_transport_layer (vm, bi, bs);
1018 if (!bfd_transport_control_frame (vm, bi, bs))
1019 {
1020 vlib_buffer_free_one (vm, bi);
1021 }
Klement Sekeraa3167442020-02-10 11:49:52 +00001022 bs->last_tx_nsec = now;
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001023 bfd_calc_next_tx (bm, bs, now);
1024 }
1025 else
1026 {
Klement Sekera637b9c42016-12-08 05:19:14 +01001027 BFD_DBG
1028 ("No need to send control frame now, now is %lu, tx_timeout is %lu",
Klement Sekeraa3167442020-02-10 11:49:52 +00001029 now, bs->tx_timeout_nsec);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001030 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001031}
1032
1033void
Klement Sekerab59f63b2022-02-14 20:19:28 +00001034bfd_init_final_control_frame (vlib_main_t *vm, vlib_buffer_t *b,
1035 bfd_session_t *bs)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001036{
1037 BFD_DBG ("Send final control frame for bs_idx=%lu", bs->bs_idx);
Klement Sekerab59f63b2022-02-14 20:19:28 +00001038 bfd_init_control_frame (bs, b);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001039 bfd_pkt_set_final (vlib_buffer_get_current (b));
Klement Sekeraefd4d702021-04-20 19:21:36 +02001040 bfd_add_auth_section (vm, b, bs);
Klement Sekerae50e8562017-04-04 16:19:48 +02001041 u32 bi = vlib_get_buffer_index (vm, b);
1042 bfd_add_transport_layer (vm, bi, bs);
Klement Sekeraa3167442020-02-10 11:49:52 +00001043 bs->last_tx_nsec = bfd_time_now_nsec (vm, NULL);
Klement Sekeraa57a9702017-02-02 06:58:07 +01001044 /*
1045 * RFC allows to include changes in final frame, so if there were any
1046 * pending, we already did that, thus we can clear any pending poll needs
1047 */
Klement Sekera239790f2017-02-16 10:53:53 +01001048 bfd_set_poll_state (bs, BFD_POLL_NOT_NEEDED);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001049}
1050
1051static void
Klement Sekeraa3167442020-02-10 11:49:52 +00001052bfd_check_rx_timeout (vlib_main_t * vm, bfd_main_t * bm, bfd_session_t * bs,
1053 u64 now, int handling_wakeup)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001054{
Klement Sekeraa3167442020-02-10 11:49:52 +00001055 if (bs->last_rx_nsec + bs->detection_time_nsec <= now)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001056 {
1057 BFD_DBG ("Rx timeout, session goes down");
Klement Sekera4d39f9c2020-01-17 10:01:52 +00001058 /*
1059 * RFC 5880 6.8.1. State Variables
1060
1061 * bfd.RemoteDiscr
1062
1063 * The remote discriminator for this BFD session. This is the
1064 * discriminator chosen by the remote system, and is totally opaque
1065 * to the local system. This MUST be initialized to zero. If a
1066 * period of a Detection Time passes without the receipt of a valid,
1067 * authenticated BFD packet from the remote system, this variable
1068 * MUST be set to zero.
1069 */
1070 bs->remote_discr = 0;
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001071 bfd_set_diag (bs, BFD_DIAG_CODE_det_time_exp);
Klement Sekeraa3167442020-02-10 11:49:52 +00001072 bfd_set_state (vm, bm, bs, BFD_STATE_down, handling_wakeup);
Klement Sekeraa57a9702017-02-02 06:58:07 +01001073 /*
1074 * If the remote system does not receive any
1075 * BFD Control packets for a Detection Time, it SHOULD reset
1076 * bfd.RemoteMinRxInterval to its initial value of 1 (per section 6.8.1,
1077 * since it is no longer required to maintain previous session state)
1078 * and then can transmit at its own rate.
1079 */
Klement Sekerab59f63b2022-02-14 20:19:28 +00001080 bfd_set_remote_required_min_rx (bs, 1);
Klement Sekera239790f2017-02-16 10:53:53 +01001081 }
Klement Sekeraa3167442020-02-10 11:49:52 +00001082 else if (bs->echo
1083 && bs->echo_last_rx_nsec +
1084 bs->echo_transmit_interval_nsec * bs->local_detect_mult <= now)
Klement Sekera239790f2017-02-16 10:53:53 +01001085 {
1086 BFD_DBG ("Echo rx timeout, session goes down");
1087 bfd_set_diag (bs, BFD_DIAG_CODE_echo_failed);
Klement Sekeraa3167442020-02-10 11:49:52 +00001088 bfd_set_state (vm, bm, bs, BFD_STATE_down, handling_wakeup);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001089 }
1090}
1091
1092void
Klement Sekerab59f63b2022-02-14 20:19:28 +00001093bfd_on_timeout (vlib_main_t *vm, bfd_main_t *bm, bfd_session_t *bs, u64 now)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001094{
1095 BFD_DBG ("Timeout for bs_idx=%lu", bs->bs_idx);
1096 switch (bs->local_state)
1097 {
1098 case BFD_STATE_admin_down:
Klement Sekerab59f63b2022-02-14 20:19:28 +00001099 /* fallthrough */
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001100 case BFD_STATE_down:
Klement Sekerab59f63b2022-02-14 20:19:28 +00001101 bfd_send_periodic (vm, bm, bs, now);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001102 break;
1103 case BFD_STATE_init:
Klement Sekeraa3167442020-02-10 11:49:52 +00001104 bfd_check_rx_timeout (vm, bm, bs, now, 1);
Klement Sekerab59f63b2022-02-14 20:19:28 +00001105 bfd_send_periodic (vm, bm, bs, now);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001106 break;
Klement Sekera239790f2017-02-16 10:53:53 +01001107 case BFD_STATE_up:
Klement Sekeraa3167442020-02-10 11:49:52 +00001108 bfd_check_rx_timeout (vm, bm, bs, now, 1);
Klement Sekera239790f2017-02-16 10:53:53 +01001109 if (BFD_POLL_NOT_NEEDED == bs->poll_state && !bs->echo &&
1110 bfd_is_echo_possible (bs))
1111 {
1112 /* switch on echo function as main detection method now */
1113 BFD_DBG ("Switching on echo function, bs_idx=%u", bs->bs_idx);
1114 bs->echo = 1;
Klement Sekeraa3167442020-02-10 11:49:52 +00001115 bs->echo_last_rx_nsec = now;
1116 bs->echo_tx_timeout_nsec = now;
Klement Sekerab59f63b2022-02-14 20:19:28 +00001117 bfd_set_effective_required_min_rx (
1118 bs, clib_max (bm->min_required_min_rx_while_echo_nsec,
1119 bs->config_required_min_rx_nsec));
Klement Sekera239790f2017-02-16 10:53:53 +01001120 bfd_set_poll_state (bs, BFD_POLL_NEEDED);
1121 }
Klement Sekerab59f63b2022-02-14 20:19:28 +00001122 bfd_send_periodic (vm, bm, bs, now);
Klement Sekera239790f2017-02-16 10:53:53 +01001123 if (bs->echo)
1124 {
Klement Sekerab59f63b2022-02-14 20:19:28 +00001125 bfd_send_echo (vm, bm, bs, now);
Klement Sekera239790f2017-02-16 10:53:53 +01001126 }
1127 break;
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001128 }
1129}
1130
1131/*
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001132 * bfd process node function
1133 */
1134static uword
Klement Sekerab59f63b2022-02-14 20:19:28 +00001135bfd_process (vlib_main_t *vm, CLIB_UNUSED (vlib_node_runtime_t *rt),
1136 CLIB_UNUSED (vlib_frame_t *f))
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001137{
1138 bfd_main_t *bm = &bfd_main;
1139 u32 *expired = 0;
1140 uword event_type, *event_data = 0;
1141
1142 /* So we can send events to the bfd process */
1143 bm->bfd_process_node_index = bfd_process_node.index;
1144
1145 while (1)
1146 {
Klement Sekeraa3167442020-02-10 11:49:52 +00001147 f64 vm_time;
1148 u64 now = bfd_time_now_nsec (vm, &vm_time);
1149 BFD_DBG ("wakeup, now is %llunsec, vlib_time_now() is %.9f", now,
1150 vm_time);
Dave Barach1e3417f2018-07-25 08:30:27 -04001151 bfd_lock (bm);
Klement Sekeraa3167442020-02-10 11:49:52 +00001152 f64 timeout;
1153 if (pool_elts (bm->sessions))
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001154 {
Klement Sekeraa3167442020-02-10 11:49:52 +00001155 u32 first_expires_in_ticks =
1156 TW (tw_timer_first_expires_in_ticks) (&bm->wheel);
1157 if (!first_expires_in_ticks)
Klement Sekera0c1519b2016-12-08 05:03:32 +01001158 {
Klement Sekeraa3167442020-02-10 11:49:52 +00001159 BFD_DBG
1160 ("tw_timer_first_expires_in_ticks(%p) returns 0ticks",
1161 &bm->wheel);
1162 timeout = bm->wheel.next_run_time - vm_time;
1163 BFD_DBG ("wheel.next_run_time is %.9f",
1164 bm->wheel.next_run_time);
1165 u64 next_expire_nsec = now + timeout * SEC_PER_NSEC;
1166 bm->bfd_process_next_wakeup_nsec = next_expire_nsec;
1167 bfd_unlock (bm);
Klement Sekera0c1519b2016-12-08 05:03:32 +01001168 }
1169 else
1170 {
Klement Sekeraa3167442020-02-10 11:49:52 +00001171 BFD_DBG ("tw_timer_first_expires_in_ticks(%p) returns %luticks",
1172 &bm->wheel, first_expires_in_ticks);
1173 u64 next_expire_nsec =
1174 now + first_expires_in_ticks * bm->nsec_per_tw_tick;
1175 bm->bfd_process_next_wakeup_nsec = next_expire_nsec;
1176 bfd_unlock (bm);
Klement Sekerab59f63b2022-02-14 20:19:28 +00001177 ASSERT (next_expire_nsec - now <= UINT32_MAX);
1178 // cast to u32 to avoid warning
1179 timeout = (u32) (next_expire_nsec - now) * SEC_PER_NSEC;
Klement Sekera0c1519b2016-12-08 05:03:32 +01001180 }
Klement Sekeraa3167442020-02-10 11:49:52 +00001181 BFD_DBG ("vlib_process_wait_for_event_or_clock(vm, %.09f)",
1182 timeout);
1183 (void) vlib_process_wait_for_event_or_clock (vm, timeout);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001184 }
Klement Sekeraa3167442020-02-10 11:49:52 +00001185 else
1186 {
Klement Sekera9d211502020-09-25 09:43:45 +00001187 bfd_unlock (bm);
Klement Sekeraa3167442020-02-10 11:49:52 +00001188 (void) vlib_process_wait_for_event (vm);
1189 }
1190 event_type = vlib_process_get_events (vm, &event_data);
1191 now = bfd_time_now_nsec (vm, &vm_time);
Eyal Bari0db9b042018-10-11 14:09:58 +03001192 uword *session_index;
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001193 switch (event_type)
1194 {
1195 case ~0: /* no events => timeout */
1196 /* nothing to do here */
1197 break;
1198 case BFD_EVENT_RESCHEDULE:
Klement Sekeraa3167442020-02-10 11:49:52 +00001199 BFD_DBG ("reschedule event");
Dave Barach1e3417f2018-07-25 08:30:27 -04001200 bfd_lock (bm);
Klement Sekeraa3167442020-02-10 11:49:52 +00001201 bm->bfd_process_wakeup_event_delay_nsec =
1202 now - bm->bfd_process_wakeup_event_start_nsec;
Dave Barach1e3417f2018-07-25 08:30:27 -04001203 bm->bfd_process_wakeup_events_in_flight--;
1204 bfd_unlock (bm);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001205 /* nothing to do here - reschedule is done automatically after
1206 * each event or timeout */
1207 break;
1208 case BFD_EVENT_NEW_SESSION:
Eyal Bari0db9b042018-10-11 14:09:58 +03001209 vec_foreach (session_index, event_data)
1210 {
1211 bfd_lock (bm);
1212 if (!pool_is_free_index (bm->sessions, *session_index))
1213 {
1214 bfd_session_t *bs =
1215 pool_elt_at_index (bm->sessions, *session_index);
Klement Sekerab59f63b2022-02-14 20:19:28 +00001216 bfd_send_periodic (vm, bm, bs, now);
Eyal Bari0db9b042018-10-11 14:09:58 +03001217 bfd_set_timer (bm, bs, now, 1);
1218 }
1219 else
1220 {
1221 BFD_DBG ("Ignoring event for non-existent session index %u",
1222 (u32) * session_index);
1223 }
1224 bfd_unlock (bm);
1225 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001226 break;
Klement Sekeraa57a9702017-02-02 06:58:07 +01001227 case BFD_EVENT_CONFIG_CHANGED:
Eyal Bari0db9b042018-10-11 14:09:58 +03001228 vec_foreach (session_index, event_data)
1229 {
1230 bfd_lock (bm);
1231 if (!pool_is_free_index (bm->sessions, *session_index))
1232 {
1233 bfd_session_t *bs =
1234 pool_elt_at_index (bm->sessions, *session_index);
Klement Sekerab59f63b2022-02-14 20:19:28 +00001235 bfd_on_config_change (bm, bs, now);
Eyal Bari0db9b042018-10-11 14:09:58 +03001236 }
1237 else
1238 {
1239 BFD_DBG ("Ignoring event for non-existent session index %u",
1240 (u32) * session_index);
1241 }
1242 bfd_unlock (bm);
1243 }
Klement Sekeraa57a9702017-02-02 06:58:07 +01001244 break;
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001245 default:
Damjan Marion07a38572018-01-21 06:44:18 -08001246 vlib_log_err (bm->log_class, "BUG: event type 0x%wx", event_type);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001247 break;
1248 }
Klement Sekeraa3167442020-02-10 11:49:52 +00001249 BFD_DBG ("tw_timer_expire_timers_vec(%p, %.04f);", &bm->wheel, vm_time);
Dave Barach1e3417f2018-07-25 08:30:27 -04001250 bfd_lock (bm);
Klement Sekeraa3167442020-02-10 11:49:52 +00001251 expired =
1252 TW (tw_timer_expire_timers_vec) (&bm->wheel, vm_time, expired);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001253 BFD_DBG ("Expired %d elements", vec_len (expired));
1254 u32 *p = NULL;
1255 vec_foreach (p, expired)
1256 {
1257 const u32 bs_idx = *p;
1258 if (!pool_is_free_index (bm->sessions, bs_idx))
1259 {
1260 bfd_session_t *bs = pool_elt_at_index (bm->sessions, bs_idx);
Klement Sekeraa3167442020-02-10 11:49:52 +00001261 bs->tw_id = 0; /* timer is gone because it expired */
Klement Sekerab59f63b2022-02-14 20:19:28 +00001262 bfd_on_timeout (vm, bm, bs, now);
Klement Sekera239790f2017-02-16 10:53:53 +01001263 bfd_set_timer (bm, bs, now, 1);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001264 }
1265 }
Dave Barach1e3417f2018-07-25 08:30:27 -04001266 bfd_unlock (bm);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001267 if (expired)
1268 {
Damjan Marion8bea5892022-04-04 22:40:45 +02001269 vec_set_len (expired, 0);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001270 }
1271 if (event_data)
1272 {
Damjan Marion8bea5892022-04-04 22:40:45 +02001273 vec_set_len (event_data, 0);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001274 }
1275 }
1276
1277 return 0;
1278}
1279
1280/*
1281 * bfd process node declaration
1282 */
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001283VLIB_REGISTER_NODE (bfd_process_node, static) = {
1284 .function = bfd_process,
1285 .type = VLIB_NODE_TYPE_PROCESS,
1286 .name = "bfd-process",
Klement Sekera46a87ad2017-01-02 08:22:23 +01001287 .n_next_nodes = 0,
1288 .next_nodes = {},
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001289};
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001290
1291static clib_error_t *
Klement Sekerab59f63b2022-02-14 20:19:28 +00001292bfd_sw_interface_up_down (CLIB_UNUSED (vnet_main_t *vnm),
1293 CLIB_UNUSED (u32 sw_if_index), u32 flags)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001294{
1295 // bfd_main_t *bm = &bfd_main;
1296 // vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
1297 if (!(flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
1298 {
1299 /* TODO */
1300 }
1301 return 0;
1302}
1303
1304VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (bfd_sw_interface_up_down);
1305
1306static clib_error_t *
Klement Sekerab59f63b2022-02-14 20:19:28 +00001307bfd_hw_interface_up_down (CLIB_UNUSED (vnet_main_t *vnm),
1308 CLIB_UNUSED (u32 hw_if_index), u32 flags)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001309{
1310 // bfd_main_t *bm = &bfd_main;
1311 if (flags & VNET_HW_INTERFACE_FLAG_LINK_UP)
1312 {
1313 /* TODO */
1314 }
1315 return 0;
1316}
1317
1318VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION (bfd_hw_interface_up_down);
1319
Neale Ranns88fc83e2017-04-05 08:11:14 -07001320void
1321bfd_register_listener (bfd_notify_fn_t fn)
1322{
1323 bfd_main_t *bm = &bfd_main;
1324
1325 vec_add1 (bm->listeners, fn);
1326}
1327
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001328/*
1329 * setup function
1330 */
1331static clib_error_t *
1332bfd_main_init (vlib_main_t * vm)
1333{
Dave Barach1e3417f2018-07-25 08:30:27 -04001334 vlib_thread_main_t *tm = &vlib_thread_main;
1335 u32 n_vlib_mains = tm->n_vlib_mains;
Klement Sekerab17dd962017-01-09 07:43:48 +01001336#if BFD_DEBUG
1337 setbuf (stdout, NULL);
1338#endif
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001339 bfd_main_t *bm = &bfd_main;
1340 bm->random_seed = random_default_seed ();
1341 bm->vlib_main = vm;
1342 bm->vnet_main = vnet_get_main ();
Dave Barachb7b92992018-10-17 10:38:51 -04001343 clib_memset (&bm->wheel, 0, sizeof (bm->wheel));
Klement Sekeraa3167442020-02-10 11:49:52 +00001344 bm->nsec_per_tw_tick = (f64) NSEC_PER_SEC / BFD_TW_TPS;
1345 bm->default_desired_min_tx_nsec =
1346 bfd_usec_to_nsec (BFD_DEFAULT_DESIRED_MIN_TX_USEC);
1347 bm->min_required_min_rx_while_echo_nsec =
1348 bfd_usec_to_nsec (BFD_REQUIRED_MIN_RX_USEC_WHILE_ECHO);
1349 BFD_DBG ("tw_timer_wheel_init(%p, %p, %.04f, %u)", &bm->wheel, NULL,
1350 1.00 / BFD_TW_TPS, ~0);
1351 TW (tw_timer_wheel_init) (&bm->wheel, NULL, 1.00 / BFD_TW_TPS, ~0);
Damjan Marion07a38572018-01-21 06:44:18 -08001352 bm->log_class = vlib_log_register_class ("bfd", 0);
1353 vlib_log_debug (bm->log_class, "initialized");
Dave Barach1e3417f2018-07-25 08:30:27 -04001354 bm->owner_thread_index = ~0;
1355 if (n_vlib_mains > 1)
1356 clib_spinlock_init (&bm->lock);
Klement Sekeracdaf0d82022-02-14 20:20:22 +00001357 bm->rx_counter.name = "bfd rx session counters";
1358 bm->rx_counter.stat_segment_name = "/bfd/rx-session-counters";
1359 bm->rx_echo_counter.name = "bfd rx session echo counters";
1360 bm->rx_echo_counter.stat_segment_name = "/bfd/rx-session-echo-counters";
1361 bm->tx_counter.name = "bfd tx session counters";
1362 bm->tx_counter.stat_segment_name = "/bfd/tx-session-counters";
1363 bm->tx_echo_counter.name = "bfd tx session echo counters";
1364 bm->tx_echo_counter.stat_segment_name = "/bfd/tx-session-echo-counters";
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001365 return 0;
1366}
1367
1368VLIB_INIT_FUNCTION (bfd_main_init);
1369
1370bfd_session_t *
Klement Sekera239790f2017-02-16 10:53:53 +01001371bfd_get_session (bfd_main_t * bm, bfd_transport_e t)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001372{
1373 bfd_session_t *result;
Dave Barach1e3417f2018-07-25 08:30:27 -04001374
1375 bfd_lock (bm);
1376
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001377 pool_get (bm->sessions, result);
Dave Barachb7b92992018-10-17 10:38:51 -04001378 clib_memset (result, 0, sizeof (*result));
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001379 result->bs_idx = result - bm->sessions;
1380 result->transport = t;
Klement Sekera239790f2017-02-16 10:53:53 +01001381 const unsigned limit = 1000;
1382 unsigned counter = 0;
1383 do
1384 {
1385 result->local_discr = random_u32 (&bm->random_seed);
1386 if (counter > limit)
1387 {
Damjan Marion07a38572018-01-21 06:44:18 -08001388 vlib_log_crit (bm->log_class,
1389 "couldn't allocate unused session discriminator even "
1390 "after %u tries!", limit);
Klement Sekera239790f2017-02-16 10:53:53 +01001391 pool_put (bm->sessions, result);
Dave Barach1e3417f2018-07-25 08:30:27 -04001392 bfd_unlock (bm);
Klement Sekera239790f2017-02-16 10:53:53 +01001393 return NULL;
1394 }
1395 ++counter;
1396 }
1397 while (hash_get (bm->session_by_disc, result->local_discr));
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001398 bfd_set_defaults (bm, result);
1399 hash_set (bm->session_by_disc, result->local_discr, result->bs_idx);
Klement Sekeracdaf0d82022-02-14 20:20:22 +00001400 bfd_validate_counters (bm);
1401 vlib_zero_combined_counter (&bm->rx_counter, result->bs_idx);
1402 vlib_zero_combined_counter (&bm->rx_echo_counter, result->bs_idx);
1403 vlib_zero_combined_counter (&bm->tx_counter, result->bs_idx);
1404 vlib_zero_combined_counter (&bm->tx_echo_counter, result->bs_idx);
Dave Barach1e3417f2018-07-25 08:30:27 -04001405 bfd_unlock (bm);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001406 return result;
1407}
1408
1409void
1410bfd_put_session (bfd_main_t * bm, bfd_session_t * bs)
1411{
Dave Barach1e3417f2018-07-25 08:30:27 -04001412 bfd_lock (bm);
1413
Damjan Marion07a38572018-01-21 06:44:18 -08001414 vlib_log_info (bm->log_class, "delete session: %U",
1415 format_bfd_session_brief, bs);
Klement Sekerab17dd962017-01-09 07:43:48 +01001416 if (bs->auth.curr_key)
1417 {
1418 --bs->auth.curr_key->use_count;
1419 }
1420 if (bs->auth.next_key)
1421 {
1422 --bs->auth.next_key->use_count;
1423 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001424 hash_unset (bm->session_by_disc, bs->local_discr);
Klement Sekeracdaf0d82022-02-14 20:20:22 +00001425 vlib_zero_combined_counter (&bm->rx_counter, bs->bs_idx);
1426 vlib_zero_combined_counter (&bm->rx_echo_counter, bs->bs_idx);
1427 vlib_zero_combined_counter (&bm->tx_counter, bs->bs_idx);
1428 vlib_zero_combined_counter (&bm->tx_echo_counter, bs->bs_idx);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001429 pool_put (bm->sessions, bs);
Dave Barach1e3417f2018-07-25 08:30:27 -04001430 bfd_unlock (bm);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001431}
1432
1433bfd_session_t *
1434bfd_find_session_by_idx (bfd_main_t * bm, uword bs_idx)
1435{
Dave Barach1e3417f2018-07-25 08:30:27 -04001436 bfd_lock_check (bm);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001437 if (!pool_is_free_index (bm->sessions, bs_idx))
1438 {
1439 return pool_elt_at_index (bm->sessions, bs_idx);
1440 }
1441 return NULL;
1442}
1443
1444bfd_session_t *
1445bfd_find_session_by_disc (bfd_main_t * bm, u32 disc)
1446{
Dave Barach1e3417f2018-07-25 08:30:27 -04001447 bfd_lock_check (bm);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001448 uword *p = hash_get (bfd_main.session_by_disc, disc);
1449 if (p)
1450 {
1451 return pool_elt_at_index (bfd_main.sessions, *p);
1452 }
1453 return NULL;
1454}
1455
1456/**
1457 * @brief verify bfd packet - common checks
1458 *
1459 * @param pkt
1460 *
1461 * @return 1 if bfd packet is valid
1462 */
Neale Ranns0c50dbb2022-08-05 03:40:43 +00001463bfd_error_t
1464bfd_verify_pkt_common (const bfd_pkt_t *pkt)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001465{
1466 if (1 != bfd_pkt_get_version (pkt))
1467 {
1468 BFD_ERR ("BFD verification failed - unexpected version: '%d'",
1469 bfd_pkt_get_version (pkt));
Neale Ranns0c50dbb2022-08-05 03:40:43 +00001470 return BFD_ERROR_VERSION;
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001471 }
1472 if (pkt->head.length < sizeof (bfd_pkt_t) ||
1473 (bfd_pkt_get_auth_present (pkt) &&
Klement Sekerab17dd962017-01-09 07:43:48 +01001474 pkt->head.length < sizeof (bfd_pkt_with_common_auth_t)))
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001475 {
1476 BFD_ERR ("BFD verification failed - unexpected length: '%d' (auth "
1477 "present: %d)",
1478 pkt->head.length, bfd_pkt_get_auth_present (pkt));
Neale Ranns0c50dbb2022-08-05 03:40:43 +00001479 return BFD_ERROR_LENGTH;
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001480 }
1481 if (!pkt->head.detect_mult)
1482 {
1483 BFD_ERR ("BFD verification failed - unexpected detect-mult: '%d'",
1484 pkt->head.detect_mult);
Neale Ranns0c50dbb2022-08-05 03:40:43 +00001485 return BFD_ERROR_DETECT_MULTI;
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001486 }
1487 if (bfd_pkt_get_multipoint (pkt))
1488 {
1489 BFD_ERR ("BFD verification failed - unexpected multipoint: '%d'",
1490 bfd_pkt_get_multipoint (pkt));
Neale Ranns0c50dbb2022-08-05 03:40:43 +00001491 return BFD_ERROR_MULTI_POINT;
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001492 }
1493 if (!pkt->my_disc)
1494 {
1495 BFD_ERR ("BFD verification failed - unexpected my-disc: '%d'",
1496 pkt->my_disc);
Neale Ranns0c50dbb2022-08-05 03:40:43 +00001497 return BFD_ERROR_MY_DISC;
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001498 }
1499 if (!pkt->your_disc)
1500 {
1501 const u8 pkt_state = bfd_pkt_get_state (pkt);
1502 if (pkt_state != BFD_STATE_down && pkt_state != BFD_STATE_admin_down)
1503 {
1504 BFD_ERR ("BFD verification failed - unexpected state: '%s' "
1505 "(your-disc is zero)", bfd_state_string (pkt_state));
Neale Ranns0c50dbb2022-08-05 03:40:43 +00001506 return BFD_ERROR_YOUR_DISC;
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001507 }
1508 }
Neale Ranns0c50dbb2022-08-05 03:40:43 +00001509 return BFD_ERROR_NONE;
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001510}
1511
Klement Sekerab17dd962017-01-09 07:43:48 +01001512static void
1513bfd_session_switch_auth_to_next (bfd_session_t * bs)
1514{
1515 BFD_DBG ("Switching authentication key from %U to %U for bs_idx=%u",
1516 format_bfd_auth_key, bs->auth.curr_key, format_bfd_auth_key,
1517 bs->auth.next_key, bs->bs_idx);
1518 bs->auth.is_delayed = 0;
1519 if (bs->auth.curr_key)
1520 {
1521 --bs->auth.curr_key->use_count;
1522 }
1523 bs->auth.curr_key = bs->auth.next_key;
1524 bs->auth.next_key = NULL;
1525 bs->auth.curr_bfd_key_id = bs->auth.next_bfd_key_id;
1526}
1527
1528static int
1529bfd_auth_type_is_meticulous (bfd_auth_type_e auth_type)
1530{
1531 if (BFD_AUTH_TYPE_meticulous_keyed_md5 == auth_type ||
1532 BFD_AUTH_TYPE_meticulous_keyed_sha1 == auth_type)
1533 {
1534 return 1;
1535 }
1536 return 0;
1537}
1538
1539static int
Klement Sekeraa3167442020-02-10 11:49:52 +00001540bfd_verify_pkt_auth_seq_num (vlib_main_t * vm, bfd_session_t * bs,
Klement Sekerab17dd962017-01-09 07:43:48 +01001541 u32 received_seq_num, int is_meticulous)
1542{
1543 /*
1544 * RFC 5880 6.8.1:
1545 *
1546 * This variable MUST be set to zero after no packets have been
1547 * received on this session for at least twice the Detection Time.
1548 */
Klement Sekeraa3167442020-02-10 11:49:52 +00001549 u64 now = bfd_time_now_nsec (vm, NULL);
1550 if (now - bs->last_rx_nsec > bs->detection_time_nsec * 2)
Klement Sekerab17dd962017-01-09 07:43:48 +01001551 {
Klement Sekeraa3167442020-02-10 11:49:52 +00001552 BFD_DBG ("BFD peer unresponsive for %lu nsec, which is > 2 * "
1553 "detection_time=%u nsec, resetting remote_seq_number_known "
1554 "flag", now - bs->last_rx_nsec, bs->detection_time_nsec * 2);
Klement Sekerab17dd962017-01-09 07:43:48 +01001555 bs->auth.remote_seq_number_known = 0;
1556 }
1557 if (bs->auth.remote_seq_number_known)
1558 {
1559 /* remote sequence number is known, verify its validity */
1560 const u32 max_u32 = 0xffffffff;
1561 /* the calculation might wrap, account for the special case... */
1562 if (bs->auth.remote_seq_number > max_u32 - 3 * bs->local_detect_mult)
1563 {
1564 /*
1565 * special case
1566 *
1567 * x y z
1568 * |----------+----------------------------+-----------|
1569 * 0 ^ ^ 0xffffffff
1570 * | remote_seq_num------+
1571 * |
1572 * +-----(remote_seq_num + 3*detect_mult) % * 0xffffffff
1573 *
1574 * x + y + z = 0xffffffff
1575 * x + z = 3 * detect_mult
1576 */
1577 const u32 z = max_u32 - bs->auth.remote_seq_number;
1578 const u32 x = 3 * bs->local_detect_mult - z;
1579 if (received_seq_num > x &&
1580 received_seq_num < bs->auth.remote_seq_number + is_meticulous)
1581 {
1582 BFD_ERR
1583 ("Recvd sequence number=%u out of ranges <0, %u>, <%u, %u>",
1584 received_seq_num, x,
1585 bs->auth.remote_seq_number + is_meticulous, max_u32);
1586 return 0;
1587 }
1588 }
1589 else
1590 {
1591 /* regular case */
1592 const u32 min = bs->auth.remote_seq_number + is_meticulous;
1593 const u32 max =
1594 bs->auth.remote_seq_number + 3 * bs->local_detect_mult;
1595 if (received_seq_num < min || received_seq_num > max)
1596 {
1597 BFD_ERR ("Recvd sequence number=%u out of range <%u, %u>",
1598 received_seq_num, min, max);
1599 return 0;
1600 }
1601 }
1602 }
1603 return 1;
1604}
1605
1606static int
Klement Sekeraefd4d702021-04-20 19:21:36 +02001607bfd_verify_pkt_auth_key_sha1 (vlib_main_t *vm, const bfd_pkt_t *pkt,
Klement Sekerab59f63b2022-02-14 20:19:28 +00001608 u32 pkt_size, CLIB_UNUSED (bfd_session_t *bs),
1609 u8 bfd_key_id, bfd_auth_key_t *auth_key)
Klement Sekerab17dd962017-01-09 07:43:48 +01001610{
1611 ASSERT (auth_key->auth_type == BFD_AUTH_TYPE_keyed_sha1 ||
1612 auth_key->auth_type == BFD_AUTH_TYPE_meticulous_keyed_sha1);
1613
Klement Sekerab17dd962017-01-09 07:43:48 +01001614 bfd_pkt_with_common_auth_t *with_common = (void *) pkt;
1615 if (pkt_size < sizeof (*with_common))
1616 {
1617 BFD_ERR ("Packet size too small to hold authentication common header");
1618 return 0;
1619 }
1620 if (with_common->common_auth.type != auth_key->auth_type)
1621 {
1622 BFD_ERR ("BFD auth type mismatch, packet auth=%d:%s doesn't match "
1623 "in-use auth=%d:%s",
1624 with_common->common_auth.type,
1625 bfd_auth_type_str (with_common->common_auth.type),
1626 auth_key->auth_type, bfd_auth_type_str (auth_key->auth_type));
1627 return 0;
1628 }
1629 bfd_pkt_with_sha1_auth_t *with_sha1 = (void *) pkt;
1630 if (pkt_size < sizeof (*with_sha1) ||
1631 with_sha1->sha1_auth.type_len.len < sizeof (with_sha1->sha1_auth))
1632 {
1633 BFD_ERR
1634 ("BFD size mismatch, payload size=%u, expected=%u, auth_len=%u, "
1635 "expected=%u", pkt_size, sizeof (*with_sha1),
1636 with_sha1->sha1_auth.type_len.len, sizeof (with_sha1->sha1_auth));
1637 return 0;
1638 }
1639 if (with_sha1->sha1_auth.key_id != bfd_key_id)
1640 {
1641 BFD_ERR
1642 ("BFD key ID mismatch, packet key ID=%u doesn't match key ID=%u%s",
1643 with_sha1->sha1_auth.key_id, bfd_key_id,
1644 bs->
1645 auth.is_delayed ? " (but a delayed auth change is scheduled)" : "");
1646 return 0;
1647 }
Klement Sekeraefd4d702021-04-20 19:21:36 +02001648
1649 u8 hash_from_packet[STRUCT_SIZE_OF (bfd_auth_sha1_t, hash)];
1650 u8 calculated_hash[STRUCT_SIZE_OF (bfd_auth_sha1_t, hash)];
1651 clib_memcpy (hash_from_packet, with_sha1->sha1_auth.hash,
1652 sizeof (with_sha1->sha1_auth.hash));
1653 clib_memcpy (with_sha1->sha1_auth.hash, auth_key->key,
1654 sizeof (auth_key->key));
1655 vnet_crypto_op_t op;
1656 vnet_crypto_op_init (&op, VNET_CRYPTO_OP_SHA1_HASH);
1657 op.src = (u8 *) with_sha1;
1658 op.len = sizeof (*with_sha1);
1659 op.digest = calculated_hash;
1660 vnet_crypto_process_ops (vm, &op, 1);
Andrew Yourtchenkoc556fa42022-02-02 13:32:39 +01001661
1662 /* Restore the modified data within the packet */
1663 clib_memcpy (with_sha1->sha1_auth.hash, hash_from_packet,
1664 sizeof (with_sha1->sha1_auth.hash));
1665
Klement Sekeraefd4d702021-04-20 19:21:36 +02001666 if (0 ==
1667 memcmp (calculated_hash, hash_from_packet, sizeof (calculated_hash)))
Klement Sekerab17dd962017-01-09 07:43:48 +01001668 {
Klement Sekeraefd4d702021-04-20 19:21:36 +02001669 clib_memcpy (with_sha1->sha1_auth.hash, hash_from_packet,
1670 sizeof (hash_from_packet));
Klement Sekerab17dd962017-01-09 07:43:48 +01001671 return 1;
1672 }
1673 BFD_ERR ("SHA1 hash: %U doesn't match the expected value: %U",
Klement Sekeraefd4d702021-04-20 19:21:36 +02001674 format_hex_bytes, hash_from_packet, sizeof (hash_from_packet),
1675 format_hex_bytes, calculated_hash, sizeof (calculated_hash));
Klement Sekerab17dd962017-01-09 07:43:48 +01001676 return 0;
1677}
1678
1679static int
Klement Sekeraa3167442020-02-10 11:49:52 +00001680bfd_verify_pkt_auth_key (vlib_main_t * vm, const bfd_pkt_t * pkt,
1681 u32 pkt_size, bfd_session_t * bs, u8 bfd_key_id,
Klement Sekerab17dd962017-01-09 07:43:48 +01001682 bfd_auth_key_t * auth_key)
1683{
Damjan Marion07a38572018-01-21 06:44:18 -08001684 bfd_main_t *bm = &bfd_main;
Klement Sekerab17dd962017-01-09 07:43:48 +01001685 switch (auth_key->auth_type)
1686 {
1687 case BFD_AUTH_TYPE_reserved:
Damjan Marion07a38572018-01-21 06:44:18 -08001688 vlib_log_err (bm->log_class,
1689 "internal error, unexpected auth_type=%d:%s",
Klement Sekerab17dd962017-01-09 07:43:48 +01001690 auth_key->auth_type,
1691 bfd_auth_type_str (auth_key->auth_type));
1692 return 0;
1693 case BFD_AUTH_TYPE_simple_password:
Klement Sekerab59f63b2022-02-14 20:19:28 +00001694 /* fallthrough */
Klement Sekerab17dd962017-01-09 07:43:48 +01001695 case BFD_AUTH_TYPE_keyed_md5:
1696 /* fallthrough */
1697 case BFD_AUTH_TYPE_meticulous_keyed_md5:
Klement Sekerab59f63b2022-02-14 20:19:28 +00001698 vlib_log_err (
1699 bm->log_class,
1700 "internal error, not implemented, unexpected auth_type=%d:%s",
1701 auth_key->auth_type, bfd_auth_type_str (auth_key->auth_type));
Klement Sekerab17dd962017-01-09 07:43:48 +01001702 return 0;
1703 case BFD_AUTH_TYPE_keyed_sha1:
1704 /* fallthrough */
1705 case BFD_AUTH_TYPE_meticulous_keyed_sha1:
Klement Sekerab17dd962017-01-09 07:43:48 +01001706 do
1707 {
1708 const u32 seq_num = clib_net_to_host_u32 (((bfd_pkt_with_sha1_auth_t
1709 *) pkt)->
1710 sha1_auth.seq_num);
Klement Sekeraefd4d702021-04-20 19:21:36 +02001711 return bfd_verify_pkt_auth_seq_num (
1712 vm, bs, seq_num,
1713 bfd_auth_type_is_meticulous (auth_key->auth_type)) &&
1714 bfd_verify_pkt_auth_key_sha1 (vm, pkt, pkt_size, bs,
1715 bfd_key_id, auth_key);
Klement Sekerab17dd962017-01-09 07:43:48 +01001716 }
1717 while (0);
Klement Sekerab17dd962017-01-09 07:43:48 +01001718 }
1719 return 0;
1720}
1721
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001722/**
1723 * @brief verify bfd packet - authentication
1724 *
1725 * @param pkt
1726 *
1727 * @return 1 if bfd packet is valid
1728 */
1729int
Klement Sekeraa3167442020-02-10 11:49:52 +00001730bfd_verify_pkt_auth (vlib_main_t * vm, const bfd_pkt_t * pkt, u16 pkt_size,
1731 bfd_session_t * bs)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001732{
Klement Sekerab17dd962017-01-09 07:43:48 +01001733 if (bfd_pkt_get_auth_present (pkt))
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001734 {
Klement Sekerab17dd962017-01-09 07:43:48 +01001735 /* authentication present in packet */
1736 if (!bs->auth.curr_key)
1737 {
1738 /* currently not using authentication - can we turn it on? */
1739 if (bs->auth.is_delayed && bs->auth.next_key)
1740 {
1741 /* yes, switch is scheduled - make sure the auth is valid */
Klement Sekeraa3167442020-02-10 11:49:52 +00001742 if (bfd_verify_pkt_auth_key (vm, pkt, pkt_size, bs,
Klement Sekerab17dd962017-01-09 07:43:48 +01001743 bs->auth.next_bfd_key_id,
1744 bs->auth.next_key))
1745 {
1746 /* auth matches next key, do the switch, packet is valid */
1747 bfd_session_switch_auth_to_next (bs);
1748 return 1;
1749 }
1750 }
1751 }
1752 else
1753 {
1754 /* yes, using authentication, verify the key */
Klement Sekeraa3167442020-02-10 11:49:52 +00001755 if (bfd_verify_pkt_auth_key (vm, pkt, pkt_size, bs,
Klement Sekerab17dd962017-01-09 07:43:48 +01001756 bs->auth.curr_bfd_key_id,
1757 bs->auth.curr_key))
1758 {
1759 /* verification passed, packet is valid */
1760 return 1;
1761 }
1762 else
1763 {
1764 /* verification failed - but maybe we need to switch key */
1765 if (bs->auth.is_delayed && bs->auth.next_key)
1766 {
1767 /* delayed switch present, verify if that key works */
Klement Sekeraa3167442020-02-10 11:49:52 +00001768 if (bfd_verify_pkt_auth_key (vm, pkt, pkt_size, bs,
Klement Sekerab17dd962017-01-09 07:43:48 +01001769 bs->auth.next_bfd_key_id,
1770 bs->auth.next_key))
1771 {
1772 /* auth matches next key, switch key, packet is valid */
1773 bfd_session_switch_auth_to_next (bs);
1774 return 1;
1775 }
1776 }
1777 }
1778 }
1779 }
1780 else
1781 {
1782 /* authentication in packet not present */
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001783 if (pkt_size > sizeof (*pkt))
1784 {
1785 BFD_ERR ("BFD verification failed - unexpected packet size '%d' "
1786 "(auth not present)", pkt_size);
1787 return 0;
1788 }
Klement Sekerab17dd962017-01-09 07:43:48 +01001789 if (bs->auth.curr_key)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001790 {
Klement Sekerab17dd962017-01-09 07:43:48 +01001791 /* currently authenticating - could we turn it off? */
1792 if (bs->auth.is_delayed && !bs->auth.next_key)
1793 {
1794 /* yes, delayed switch to NULL key is scheduled */
1795 bfd_session_switch_auth_to_next (bs);
1796 return 1;
1797 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001798 }
Klement Sekerab17dd962017-01-09 07:43:48 +01001799 else
1800 {
1801 /* no auth in packet, no auth in use - packet is valid */
1802 return 1;
1803 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001804 }
Klement Sekerab17dd962017-01-09 07:43:48 +01001805 return 0;
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001806}
1807
Neale Ranns0c50dbb2022-08-05 03:40:43 +00001808bfd_error_t
1809bfd_consume_pkt (vlib_main_t *vm, bfd_main_t *bm, const bfd_pkt_t *pkt,
Klement Sekeraa3167442020-02-10 11:49:52 +00001810 u32 bs_idx)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001811{
Dave Barach1e3417f2018-07-25 08:30:27 -04001812 bfd_lock_check (bm);
1813
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001814 bfd_session_t *bs = bfd_find_session_by_idx (bm, bs_idx);
Klement Sekera0e2e0df2017-03-03 08:51:08 +01001815 if (!bs || (pkt->your_disc && pkt->your_disc != bs->local_discr))
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001816 {
Neale Ranns0c50dbb2022-08-05 03:40:43 +00001817 return BFD_ERROR_YOUR_DISC;
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001818 }
1819 BFD_DBG ("Scanning bfd packet, bs_idx=%d", bs->bs_idx);
1820 bs->remote_discr = pkt->my_disc;
1821 bs->remote_state = bfd_pkt_get_state (pkt);
1822 bs->remote_demand = bfd_pkt_get_demand (pkt);
Klement Sekera73884482017-02-23 09:26:30 +01001823 bs->remote_diag = bfd_pkt_get_diag_code (pkt);
Klement Sekeraa3167442020-02-10 11:49:52 +00001824 u64 now = bfd_time_now_nsec (vm, NULL);
1825 bs->last_rx_nsec = now;
Klement Sekerab17dd962017-01-09 07:43:48 +01001826 if (bfd_pkt_get_auth_present (pkt))
1827 {
1828 bfd_auth_type_e auth_type =
1829 ((bfd_pkt_with_common_auth_t *) (pkt))->common_auth.type;
1830 switch (auth_type)
1831 {
1832 case BFD_AUTH_TYPE_reserved:
1833 /* fallthrough */
1834 case BFD_AUTH_TYPE_simple_password:
1835 /* fallthrough */
1836 case BFD_AUTH_TYPE_keyed_md5:
1837 /* fallthrough */
1838 case BFD_AUTH_TYPE_meticulous_keyed_md5:
Damjan Marion07a38572018-01-21 06:44:18 -08001839 vlib_log_crit (bm->log_class,
1840 "internal error, unexpected auth_type=%d:%s",
1841 auth_type, bfd_auth_type_str (auth_type));
Klement Sekerab17dd962017-01-09 07:43:48 +01001842 break;
1843 case BFD_AUTH_TYPE_keyed_sha1:
1844 /* fallthrough */
1845 case BFD_AUTH_TYPE_meticulous_keyed_sha1:
1846 do
1847 {
1848 bfd_pkt_with_sha1_auth_t *with_sha1 =
1849 (bfd_pkt_with_sha1_auth_t *) pkt;
1850 bs->auth.remote_seq_number =
1851 clib_net_to_host_u32 (with_sha1->sha1_auth.seq_num);
1852 bs->auth.remote_seq_number_known = 1;
1853 BFD_DBG ("Received sequence number %u",
1854 bs->auth.remote_seq_number);
1855 }
1856 while (0);
1857 }
1858 }
Klement Sekeraa3167442020-02-10 11:49:52 +00001859 bs->remote_desired_min_tx_nsec =
1860 bfd_usec_to_nsec (clib_net_to_host_u32 (pkt->des_min_tx));
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001861 bs->remote_detect_mult = pkt->head.detect_mult;
Klement Sekerab59f63b2022-02-14 20:19:28 +00001862 bfd_set_remote_required_min_rx (bs, clib_net_to_host_u32 (pkt->req_min_rx));
1863 bfd_set_remote_required_min_echo_rx (
1864 bs, clib_net_to_host_u32 (pkt->req_min_echo_rx));
Klement Sekera239790f2017-02-16 10:53:53 +01001865 if (bfd_pkt_get_final (pkt))
Klement Sekeraa57a9702017-02-02 06:58:07 +01001866 {
Klement Sekera239790f2017-02-16 10:53:53 +01001867 if (BFD_POLL_IN_PROGRESS == bs->poll_state)
Klement Sekeraa57a9702017-02-02 06:58:07 +01001868 {
Klement Sekera239790f2017-02-16 10:53:53 +01001869 BFD_DBG ("Poll sequence terminated, bs_idx=%u", bs->bs_idx);
1870 bfd_set_poll_state (bs, BFD_POLL_NOT_NEEDED);
1871 if (BFD_STATE_up == bs->local_state)
1872 {
Alexander Skorichenko0dfad212021-10-14 13:09:02 +03001873 bfd_set_effective_desired_min_tx (
1874 bm, bs, now, bs->config_desired_min_tx_nsec);
Klement Sekerab59f63b2022-02-14 20:19:28 +00001875 bfd_set_effective_required_min_rx (
1876 bs,
1877 clib_max (bs->echo * bm->min_required_min_rx_while_echo_nsec,
1878 bs->config_required_min_rx_nsec));
Klement Sekera239790f2017-02-16 10:53:53 +01001879 }
1880 }
1881 else if (BFD_POLL_IN_PROGRESS_AND_QUEUED == bs->poll_state)
1882 {
1883 /*
1884 * next poll sequence must be delayed by at least the round trip
1885 * time, so calculate that here
1886 */
1887 BFD_DBG ("Next poll sequence can commence in " BFD_CLK_FMT,
Klement Sekeraa3167442020-02-10 11:49:52 +00001888 BFD_CLK_PRN (now - bs->poll_state_start_or_timeout_nsec));
1889 bs->poll_state_start_or_timeout_nsec =
1890 now + (now - bs->poll_state_start_or_timeout_nsec);
Klement Sekera239790f2017-02-16 10:53:53 +01001891 BFD_DBG
1892 ("Poll sequence terminated, but another is needed, bs_idx=%u",
1893 bs->bs_idx);
1894 bfd_set_poll_state (bs, BFD_POLL_NEEDED);
Klement Sekeraa57a9702017-02-02 06:58:07 +01001895 }
1896 }
Klement Sekera239790f2017-02-16 10:53:53 +01001897 bfd_calc_next_tx (bm, bs, now);
1898 bfd_set_timer (bm, bs, now, 0);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001899 if (BFD_STATE_admin_down == bs->local_state)
Klement Sekerac48829b2017-02-14 07:55:57 +01001900 {
1901 BFD_DBG ("Session is admin-down, ignoring packet, bs_idx=%u",
1902 bs->bs_idx);
Neale Ranns0c50dbb2022-08-05 03:40:43 +00001903 return BFD_ERROR_ADMIN_DOWN;
Klement Sekerac48829b2017-02-14 07:55:57 +01001904 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001905 if (BFD_STATE_admin_down == bs->remote_state)
1906 {
1907 bfd_set_diag (bs, BFD_DIAG_CODE_neighbor_sig_down);
Klement Sekeraa3167442020-02-10 11:49:52 +00001908 bfd_set_state (vm, bm, bs, BFD_STATE_down, 0);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001909 }
1910 else if (BFD_STATE_down == bs->local_state)
1911 {
1912 if (BFD_STATE_down == bs->remote_state)
1913 {
Klement Sekerae50e8562017-04-04 16:19:48 +02001914 bfd_set_diag (bs, BFD_DIAG_CODE_no_diag);
Klement Sekeraa3167442020-02-10 11:49:52 +00001915 bfd_set_state (vm, bm, bs, BFD_STATE_init, 0);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001916 }
1917 else if (BFD_STATE_init == bs->remote_state)
1918 {
Klement Sekerae50e8562017-04-04 16:19:48 +02001919 bfd_set_diag (bs, BFD_DIAG_CODE_no_diag);
Klement Sekeraa3167442020-02-10 11:49:52 +00001920 bfd_set_state (vm, bm, bs, BFD_STATE_up, 0);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001921 }
1922 }
1923 else if (BFD_STATE_init == bs->local_state)
1924 {
1925 if (BFD_STATE_up == bs->remote_state ||
1926 BFD_STATE_init == bs->remote_state)
1927 {
Klement Sekerae50e8562017-04-04 16:19:48 +02001928 bfd_set_diag (bs, BFD_DIAG_CODE_no_diag);
Klement Sekeraa3167442020-02-10 11:49:52 +00001929 bfd_set_state (vm, bm, bs, BFD_STATE_up, 0);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001930 }
1931 }
1932 else /* BFD_STATE_up == bs->local_state */
1933 {
1934 if (BFD_STATE_down == bs->remote_state)
1935 {
1936 bfd_set_diag (bs, BFD_DIAG_CODE_neighbor_sig_down);
Klement Sekeraa3167442020-02-10 11:49:52 +00001937 bfd_set_state (vm, bm, bs, BFD_STATE_down, 0);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001938 }
1939 }
Neale Ranns0c50dbb2022-08-05 03:40:43 +00001940 return BFD_ERROR_NONE;
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001941}
1942
Klement Sekeracdaf0d82022-02-14 20:20:22 +00001943bfd_session_t *
1944bfd_consume_echo_pkt (vlib_main_t *vm, bfd_main_t *bm, vlib_buffer_t *b)
Klement Sekeraa57a9702017-02-02 06:58:07 +01001945{
Klement Sekera239790f2017-02-16 10:53:53 +01001946 bfd_echo_pkt_t *pkt = NULL;
1947 if (b->current_length != sizeof (*pkt))
Klement Sekeraa57a9702017-02-02 06:58:07 +01001948 {
Klement Sekera239790f2017-02-16 10:53:53 +01001949 return 0;
Klement Sekeraa57a9702017-02-02 06:58:07 +01001950 }
Klement Sekera239790f2017-02-16 10:53:53 +01001951 pkt = vlib_buffer_get_current (b);
1952 bfd_session_t *bs = bfd_find_session_by_disc (bm, pkt->discriminator);
1953 if (!bs)
1954 {
1955 return 0;
1956 }
1957 BFD_DBG ("Scanning bfd echo packet, bs_idx=%d", bs->bs_idx);
1958 u64 checksum =
Klement Sekeraa3167442020-02-10 11:49:52 +00001959 bfd_calc_echo_checksum (bs->local_discr, pkt->expire_time_nsec,
Klement Sekera239790f2017-02-16 10:53:53 +01001960 bs->echo_secret);
1961 if (checksum != pkt->checksum)
1962 {
1963 BFD_DBG ("Invalid echo packet, checksum mismatch");
Klement Sekeracdaf0d82022-02-14 20:20:22 +00001964 return 0;
Klement Sekera239790f2017-02-16 10:53:53 +01001965 }
Klement Sekeraa3167442020-02-10 11:49:52 +00001966 u64 now = bfd_time_now_nsec (vm, NULL);
1967 if (pkt->expire_time_nsec < now)
Klement Sekera239790f2017-02-16 10:53:53 +01001968 {
1969 BFD_DBG ("Stale packet received, expire time %lu < now %lu",
Klement Sekeraa3167442020-02-10 11:49:52 +00001970 pkt->expire_time_nsec, now);
Klement Sekera239790f2017-02-16 10:53:53 +01001971 }
1972 else
1973 {
Klement Sekeraa3167442020-02-10 11:49:52 +00001974 bs->echo_last_rx_nsec = now;
Klement Sekera239790f2017-02-16 10:53:53 +01001975 }
Klement Sekeracdaf0d82022-02-14 20:20:22 +00001976 return bs;
Klement Sekeraa57a9702017-02-02 06:58:07 +01001977}
1978
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001979u8 *
1980format_bfd_session (u8 * s, va_list * args)
1981{
1982 const bfd_session_t *bs = va_arg (*args, bfd_session_t *);
Klement Sekera239790f2017-02-16 10:53:53 +01001983 s = format (s, "bs_idx=%u local-state=%s remote-state=%s\n"
Damjan Marion06d82262020-10-21 12:43:40 +02001984 "local-discriminator=%u remote-discriminator=%u\n"
1985 "local-diag=%s echo-active=%s\n"
1986 "desired-min-tx=%u required-min-rx=%u\n"
1987 "required-min-echo-rx=%u detect-mult=%u\n"
1988 "remote-min-rx=%u remote-min-echo-rx=%u\n"
1989 "remote-demand=%s poll-state=%s\n"
1990 "auth: local-seq-num=%u remote-seq-num=%u\n"
1991 " is-delayed=%s\n"
1992 " curr-key=%U\n"
1993 " next-key=%U",
Klement Sekerab17dd962017-01-09 07:43:48 +01001994 bs->bs_idx, bfd_state_string (bs->local_state),
Damjan Marion06d82262020-10-21 12:43:40 +02001995 bfd_state_string (bs->remote_state), bs->local_discr,
1996 bs->remote_discr, bfd_diag_code_string (bs->local_diag),
1997 (bs->echo ? "yes" : "no"), bs->config_desired_min_tx_usec,
1998 bs->config_required_min_rx_usec, 1, bs->local_detect_mult,
1999 bs->remote_min_rx_usec, bs->remote_min_echo_rx_usec,
Klement Sekera239790f2017-02-16 10:53:53 +01002000 (bs->remote_demand ? "yes" : "no"),
Damjan Marion06d82262020-10-21 12:43:40 +02002001 bfd_poll_state_string (bs->poll_state),
2002 bs->auth.local_seq_number, bs->auth.remote_seq_number,
2003 (bs->auth.is_delayed ? "yes" : "no"),
2004 format_bfd_auth_key, bs->auth.curr_key, format_bfd_auth_key,
Klement Sekera239790f2017-02-16 10:53:53 +01002005 bs->auth.next_key);
Klement Sekerab17dd962017-01-09 07:43:48 +01002006 return s;
2007}
2008
Damjan Marion07a38572018-01-21 06:44:18 -08002009u8 *
2010format_bfd_session_brief (u8 * s, va_list * args)
2011{
2012 const bfd_session_t *bs = va_arg (*args, bfd_session_t *);
2013 s =
2014 format (s, "bs_idx=%u local-state=%s remote-state=%s", bs->bs_idx,
2015 bfd_state_string (bs->local_state),
2016 bfd_state_string (bs->remote_state));
2017 return s;
2018}
2019
Klement Sekerab17dd962017-01-09 07:43:48 +01002020unsigned
2021bfd_auth_type_supported (bfd_auth_type_e auth_type)
2022{
2023 if (auth_type == BFD_AUTH_TYPE_keyed_sha1 ||
2024 auth_type == BFD_AUTH_TYPE_meticulous_keyed_sha1)
2025 {
2026 return 1;
2027 }
2028 return 0;
2029}
2030
2031vnet_api_error_t
2032bfd_auth_activate (bfd_session_t * bs, u32 conf_key_id,
2033 u8 bfd_key_id, u8 is_delayed)
2034{
2035 bfd_main_t *bm = &bfd_main;
2036 const uword *key_idx_p =
2037 hash_get (bm->auth_key_by_conf_key_id, conf_key_id);
2038 if (!key_idx_p)
2039 {
Damjan Marion07a38572018-01-21 06:44:18 -08002040 vlib_log_err (bm->log_class,
2041 "authentication key with config ID %u doesn't exist)",
Klement Sekerab17dd962017-01-09 07:43:48 +01002042 conf_key_id);
2043 return VNET_API_ERROR_BFD_ENOENT;
2044 }
2045 const uword key_idx = *key_idx_p;
2046 bfd_auth_key_t *key = pool_elt_at_index (bm->auth_keys, key_idx);
2047 if (is_delayed)
2048 {
Alexander Skorichenko4c3c60d2021-10-15 16:04:44 +00002049 if (bs->auth.next_key == key && bs->auth.next_bfd_key_id == bfd_key_id)
Klement Sekerab17dd962017-01-09 07:43:48 +01002050 {
2051 /* already using this key, no changes required */
2052 return 0;
2053 }
Alexander Skorichenko4c3c60d2021-10-15 16:04:44 +00002054 if (bs->auth.next_key != key)
2055 {
2056 ++key->use_count;
2057 bs->auth.next_key = key;
2058 }
Klement Sekerab17dd962017-01-09 07:43:48 +01002059 bs->auth.next_bfd_key_id = bfd_key_id;
2060 bs->auth.is_delayed = 1;
2061 }
2062 else
2063 {
Alexander Skorichenko4c3c60d2021-10-15 16:04:44 +00002064 if (bs->auth.curr_key == key && bs->auth.curr_bfd_key_id == bfd_key_id)
Klement Sekerab17dd962017-01-09 07:43:48 +01002065 {
2066 /* already using this key, no changes required */
2067 return 0;
2068 }
Alexander Skorichenko4c3c60d2021-10-15 16:04:44 +00002069 ++key->use_count;
Klement Sekerab17dd962017-01-09 07:43:48 +01002070 if (bs->auth.curr_key)
2071 {
2072 --bs->auth.curr_key->use_count;
2073 }
2074 bs->auth.curr_key = key;
2075 bs->auth.curr_bfd_key_id = bfd_key_id;
2076 bs->auth.is_delayed = 0;
2077 }
Klement Sekera239790f2017-02-16 10:53:53 +01002078 BFD_DBG ("\nSession auth modified: %U", format_bfd_session, bs);
Damjan Marion07a38572018-01-21 06:44:18 -08002079 vlib_log_info (bm->log_class, "session auth modified: %U",
2080 format_bfd_session_brief, bs);
Klement Sekerab17dd962017-01-09 07:43:48 +01002081 return 0;
2082}
2083
2084vnet_api_error_t
2085bfd_auth_deactivate (bfd_session_t * bs, u8 is_delayed)
2086{
Damjan Marion07a38572018-01-21 06:44:18 -08002087 bfd_main_t *bm = &bfd_main;
Klement Sekerab17dd962017-01-09 07:43:48 +01002088 if (!is_delayed)
2089 {
2090 /* not delayed - deactivate the current key right now */
2091 if (bs->auth.curr_key)
2092 {
2093 --bs->auth.curr_key->use_count;
2094 bs->auth.curr_key = NULL;
2095 }
2096 bs->auth.is_delayed = 0;
2097 }
2098 else
2099 {
2100 /* delayed - mark as so */
2101 bs->auth.is_delayed = 1;
2102 }
2103 /*
2104 * clear the next key unconditionally - either the auth change is not delayed
2105 * in which case the caller expects the session to not use authentication
2106 * from this point forward, or it is delayed, in which case the next_key
2107 * needs to be set to NULL to make it so in the future
2108 */
2109 if (bs->auth.next_key)
2110 {
2111 --bs->auth.next_key->use_count;
2112 bs->auth.next_key = NULL;
2113 }
Klement Sekera239790f2017-02-16 10:53:53 +01002114 BFD_DBG ("\nSession auth modified: %U", format_bfd_session, bs);
Damjan Marion07a38572018-01-21 06:44:18 -08002115 vlib_log_info (bm->log_class, "session auth modified: %U",
2116 format_bfd_session_brief, bs);
Klement Sekerab17dd962017-01-09 07:43:48 +01002117 return 0;
Klement Sekera0e3c0de2016-09-29 14:43:44 +02002118}
2119
Klement Sekeraa57a9702017-02-02 06:58:07 +01002120vnet_api_error_t
2121bfd_session_set_params (bfd_main_t * bm, bfd_session_t * bs,
2122 u32 desired_min_tx_usec,
2123 u32 required_min_rx_usec, u8 detect_mult)
2124{
2125 if (bs->local_detect_mult != detect_mult ||
2126 bs->config_desired_min_tx_usec != desired_min_tx_usec ||
2127 bs->config_required_min_rx_usec != required_min_rx_usec)
2128 {
Klement Sekera239790f2017-02-16 10:53:53 +01002129 BFD_DBG ("\nChanging session params: %U", format_bfd_session, bs);
Klement Sekeraa57a9702017-02-02 06:58:07 +01002130 switch (bs->poll_state)
2131 {
Klement Sekera239790f2017-02-16 10:53:53 +01002132 case BFD_POLL_NOT_NEEDED:
Klement Sekeraa57a9702017-02-02 06:58:07 +01002133 if (BFD_STATE_up == bs->local_state ||
2134 BFD_STATE_init == bs->local_state)
2135 {
2136 /* poll sequence is not needed for detect multiplier change */
2137 if (bs->config_desired_min_tx_usec != desired_min_tx_usec ||
2138 bs->config_required_min_rx_usec != required_min_rx_usec)
2139 {
Klement Sekera239790f2017-02-16 10:53:53 +01002140 bfd_set_poll_state (bs, BFD_POLL_NEEDED);
Klement Sekeraa57a9702017-02-02 06:58:07 +01002141 }
2142 }
2143 break;
Klement Sekera239790f2017-02-16 10:53:53 +01002144 case BFD_POLL_NEEDED:
2145 case BFD_POLL_IN_PROGRESS_AND_QUEUED:
2146 /*
2147 * nothing to do - will be handled in the future poll which is
2148 * already scheduled for execution
2149 */
Klement Sekeraa57a9702017-02-02 06:58:07 +01002150 break;
Klement Sekera239790f2017-02-16 10:53:53 +01002151 case BFD_POLL_IN_PROGRESS:
2152 /* poll sequence is not needed for detect multiplier change */
2153 if (bs->config_desired_min_tx_usec != desired_min_tx_usec ||
2154 bs->config_required_min_rx_usec != required_min_rx_usec)
2155 {
2156 BFD_DBG ("Poll in progress, queueing extra poll, bs_idx=%u",
2157 bs->bs_idx);
2158 bfd_set_poll_state (bs, BFD_POLL_IN_PROGRESS_AND_QUEUED);
2159 }
Klement Sekeraa57a9702017-02-02 06:58:07 +01002160 }
2161
2162 bs->local_detect_mult = detect_mult;
2163 bs->config_desired_min_tx_usec = desired_min_tx_usec;
Klement Sekeraa3167442020-02-10 11:49:52 +00002164 bs->config_desired_min_tx_nsec = bfd_usec_to_nsec (desired_min_tx_usec);
Klement Sekeraa57a9702017-02-02 06:58:07 +01002165 bs->config_required_min_rx_usec = required_min_rx_usec;
Klement Sekeraa3167442020-02-10 11:49:52 +00002166 bs->config_required_min_rx_nsec =
2167 bfd_usec_to_nsec (required_min_rx_usec);
Klement Sekera239790f2017-02-16 10:53:53 +01002168 BFD_DBG ("\nChanged session params: %U", format_bfd_session, bs);
Klement Sekeraa57a9702017-02-02 06:58:07 +01002169
Damjan Marion07a38572018-01-21 06:44:18 -08002170 vlib_log_info (bm->log_class, "changed session params: %U",
2171 format_bfd_session_brief, bs);
Klement Sekeraa57a9702017-02-02 06:58:07 +01002172 vlib_process_signal_event (bm->vlib_main, bm->bfd_process_node_index,
2173 BFD_EVENT_CONFIG_CHANGED, bs->bs_idx);
2174 }
2175 else
2176 {
2177 BFD_DBG ("Ignore parameter change - no change, bs_idx=%u", bs->bs_idx);
2178 }
2179 return 0;
2180}
2181
Klement Sekera73884482017-02-23 09:26:30 +01002182vnet_api_error_t
2183bfd_auth_set_key (u32 conf_key_id, u8 auth_type, u8 key_len,
2184 const u8 * key_data)
2185{
Damjan Marion07a38572018-01-21 06:44:18 -08002186 bfd_main_t *bm = &bfd_main;
Klement Sekera73884482017-02-23 09:26:30 +01002187 bfd_auth_key_t *auth_key = NULL;
Klement Sekerab16bfe32017-02-28 11:56:48 +01002188 if (!key_len || key_len > bfd_max_key_len_for_auth_type (auth_type))
Klement Sekera73884482017-02-23 09:26:30 +01002189 {
Damjan Marion07a38572018-01-21 06:44:18 -08002190 vlib_log_err (bm->log_class,
2191 "invalid authentication key length for auth_type=%d:%s "
2192 "(key_len=%u, must be non-zero, expected max=%u)",
Klement Sekera73884482017-02-23 09:26:30 +01002193 auth_type, bfd_auth_type_str (auth_type), key_len,
Klement Sekerab16bfe32017-02-28 11:56:48 +01002194 (u32) bfd_max_key_len_for_auth_type (auth_type));
Klement Sekera73884482017-02-23 09:26:30 +01002195 return VNET_API_ERROR_INVALID_VALUE;
2196 }
2197 if (!bfd_auth_type_supported (auth_type))
2198 {
Damjan Marion07a38572018-01-21 06:44:18 -08002199 vlib_log_err (bm->log_class, "unsupported auth type=%d:%s", auth_type,
Klement Sekera73884482017-02-23 09:26:30 +01002200 bfd_auth_type_str (auth_type));
2201 return VNET_API_ERROR_BFD_NOTSUPP;
2202 }
Klement Sekera73884482017-02-23 09:26:30 +01002203 uword *key_idx_p = hash_get (bm->auth_key_by_conf_key_id, conf_key_id);
2204 if (key_idx_p)
2205 {
2206 /* modifying existing key - must not be used */
2207 const uword key_idx = *key_idx_p;
2208 auth_key = pool_elt_at_index (bm->auth_keys, key_idx);
2209 if (auth_key->use_count > 0)
2210 {
Damjan Marion07a38572018-01-21 06:44:18 -08002211 vlib_log_err (bm->log_class,
2212 "authentication key with conf ID %u in use by %u BFD "
2213 "session(s) - cannot modify", conf_key_id,
2214 auth_key->use_count);
Klement Sekera73884482017-02-23 09:26:30 +01002215 return VNET_API_ERROR_BFD_EINUSE;
2216 }
2217 }
2218 else
2219 {
2220 /* adding new key */
2221 pool_get (bm->auth_keys, auth_key);
2222 auth_key->conf_key_id = conf_key_id;
2223 hash_set (bm->auth_key_by_conf_key_id, conf_key_id,
2224 auth_key - bm->auth_keys);
2225 }
2226 auth_key->auth_type = auth_type;
Dave Barachb7b92992018-10-17 10:38:51 -04002227 clib_memset (auth_key->key, 0, sizeof (auth_key->key));
Klement Sekera73884482017-02-23 09:26:30 +01002228 clib_memcpy (auth_key->key, key_data, key_len);
2229 return 0;
Klement Sekera73884482017-02-23 09:26:30 +01002230}
2231
2232vnet_api_error_t
2233bfd_auth_del_key (u32 conf_key_id)
2234{
Klement Sekera73884482017-02-23 09:26:30 +01002235 bfd_auth_key_t *auth_key = NULL;
2236 bfd_main_t *bm = &bfd_main;
2237 uword *key_idx_p = hash_get (bm->auth_key_by_conf_key_id, conf_key_id);
2238 if (key_idx_p)
2239 {
2240 /* deleting existing key - must not be used */
2241 const uword key_idx = *key_idx_p;
2242 auth_key = pool_elt_at_index (bm->auth_keys, key_idx);
2243 if (auth_key->use_count > 0)
2244 {
Damjan Marion07a38572018-01-21 06:44:18 -08002245 vlib_log_err (bm->log_class,
2246 "authentication key with conf ID %u in use by %u BFD "
2247 "session(s) - cannot delete", conf_key_id,
2248 auth_key->use_count);
Klement Sekera73884482017-02-23 09:26:30 +01002249 return VNET_API_ERROR_BFD_EINUSE;
2250 }
2251 hash_unset (bm->auth_key_by_conf_key_id, conf_key_id);
Dave Barachb7b92992018-10-17 10:38:51 -04002252 clib_memset (auth_key, 0, sizeof (*auth_key));
Klement Sekera73884482017-02-23 09:26:30 +01002253 pool_put (bm->auth_keys, auth_key);
2254 }
2255 else
2256 {
2257 /* no such key */
Damjan Marion07a38572018-01-21 06:44:18 -08002258 vlib_log_err (bm->log_class,
2259 "authentication key with conf ID %u does not exist",
Klement Sekera73884482017-02-23 09:26:30 +01002260 conf_key_id);
2261 return VNET_API_ERROR_BFD_ENOENT;
2262 }
2263 return 0;
Klement Sekera73884482017-02-23 09:26:30 +01002264}
2265
Klement Sekera0e3c0de2016-09-29 14:43:44 +02002266bfd_main_t bfd_main;
2267
2268/*
2269 * fd.io coding-style-patch-verification: ON
2270 *
2271 * Local Variables:
2272 * eval: (c-set-style "gnu")
2273 * End:
2274 */