blob: 9e575afcaca991798cbde8dc1bb4421652bef28a [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
Klement Sekera239790f2017-02-16 10:53:53 +010020#if WITH_LIBSSL > 0
21#include <openssl/sha.h>
22#endif
23
24#if __SSE4_2__
25#include <x86intrin.h>
26#endif
27
Dave Barach1e3417f2018-07-25 08:30:27 -040028#include <vlibmemory/api.h>
Klement Sekera0e3c0de2016-09-29 14:43:44 +020029#include <vppinfra/random.h>
30#include <vppinfra/error.h>
31#include <vppinfra/hash.h>
Klement Sekera239790f2017-02-16 10:53:53 +010032#include <vppinfra/xxhash.h>
Klement Sekera0e3c0de2016-09-29 14:43:44 +020033#include <vnet/ethernet/ethernet.h>
34#include <vnet/ethernet/packet.h>
35#include <vnet/bfd/bfd_debug.h>
36#include <vnet/bfd/bfd_protocol.h>
37#include <vnet/bfd/bfd_main.h>
Damjan Marion07a38572018-01-21 06:44:18 -080038#include <vlib/log.h>
Klement Sekera239790f2017-02-16 10:53:53 +010039
40static u64
41bfd_calc_echo_checksum (u32 discriminator, u64 expire_time, u32 secret)
42{
43 u64 checksum = 0;
Gabriel Ganne8e66b9b2017-12-14 16:20:37 +010044#if defined(clib_crc32c_uses_intrinsics) && !defined (__i386__)
45 checksum = crc32_u64 (0, discriminator);
46 checksum = crc32_u64 (checksum, expire_time);
47 checksum = crc32_u64 (checksum, secret);
Klement Sekera239790f2017-02-16 10:53:53 +010048#else
49 checksum = clib_xxhash (discriminator ^ expire_time ^ secret);
Klement Sekerab17dd962017-01-09 07:43:48 +010050#endif
Klement Sekera239790f2017-02-16 10:53:53 +010051 return checksum;
52}
Klement Sekera0e3c0de2016-09-29 14:43:44 +020053
54static u64
Klement Sekeraa3167442020-02-10 11:49:52 +000055bfd_usec_to_nsec (u64 us)
Klement Sekera0e3c0de2016-09-29 14:43:44 +020056{
Klement Sekeraa3167442020-02-10 11:49:52 +000057 return us * NSEC_PER_USEC;
Klement Sekera0e3c0de2016-09-29 14:43:44 +020058}
59
Klement Sekera73884482017-02-23 09:26:30 +010060u32
Klement Sekeraa3167442020-02-10 11:49:52 +000061bfd_nsec_to_usec (u64 nsec)
Klement Sekera239790f2017-02-16 10:53:53 +010062{
Klement Sekeraa3167442020-02-10 11:49:52 +000063 return nsec / NSEC_PER_USEC;
64}
65
66always_inline u64
67bfd_time_now_nsec (vlib_main_t * vm, f64 * vm_time)
68{
69 f64 _vm_time = vlib_time_now (vm);
70 if (vm_time)
71 *vm_time = _vm_time;
72 return _vm_time * NSEC_PER_SEC;
Klement Sekera239790f2017-02-16 10:53:53 +010073}
74
Klement Sekera0e3c0de2016-09-29 14:43:44 +020075static vlib_node_registration_t bfd_process_node;
76
Klement Sekera73884482017-02-23 09:26:30 +010077u8 *
Klement Sekerab17dd962017-01-09 07:43:48 +010078format_bfd_auth_key (u8 * s, va_list * args)
79{
80 const bfd_auth_key_t *key = va_arg (*args, bfd_auth_key_t *);
81 if (key)
82 {
83 s = format (s, "{auth-type=%u:%s, conf-key-id=%u, use-count=%u}, ",
84 key->auth_type, bfd_auth_type_str (key->auth_type),
85 key->conf_key_id, key->use_count);
86 }
87 else
88 {
89 s = format (s, "{none}");
90 }
91 return s;
92}
93
Klement Sekera0e3c0de2016-09-29 14:43:44 +020094/*
95 * We actually send all bfd pkts to the "error" node after scanning
96 * them, so the graph node has only one next-index. The "error-drop"
97 * node automatically bumps our per-node packet counters for us.
98 */
99typedef enum
100{
101 BFD_INPUT_NEXT_NORMAL,
102 BFD_INPUT_N_NEXT,
103} bfd_input_next_t;
104
Klement Sekerae4504c62016-12-08 10:16:41 +0100105static void bfd_on_state_change (bfd_main_t * bm, bfd_session_t * bs, u64 now,
106 int handling_wakeup);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200107
108static void
109bfd_set_defaults (bfd_main_t * bm, bfd_session_t * bs)
110{
111 bs->local_state = BFD_STATE_down;
112 bs->local_diag = BFD_DIAG_CODE_no_diag;
113 bs->remote_state = BFD_STATE_down;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200114 bs->remote_discr = 0;
Neale Ranns88fc83e2017-04-05 08:11:14 -0700115 bs->hop_type = BFD_HOP_TYPE_SINGLE;
Klement Sekera239790f2017-02-16 10:53:53 +0100116 bs->config_desired_min_tx_usec = BFD_DEFAULT_DESIRED_MIN_TX_USEC;
Klement Sekeraa3167442020-02-10 11:49:52 +0000117 bs->config_desired_min_tx_nsec = bm->default_desired_min_tx_nsec;
118 bs->effective_desired_min_tx_nsec = bm->default_desired_min_tx_nsec;
Klement Sekeraa57a9702017-02-02 06:58:07 +0100119 bs->remote_min_rx_usec = 1;
Klement Sekeraa3167442020-02-10 11:49:52 +0000120 bs->remote_min_rx_nsec = bfd_usec_to_nsec (bs->remote_min_rx_usec);
Klement Sekera239790f2017-02-16 10:53:53 +0100121 bs->remote_min_echo_rx_usec = 0;
Klement Sekeraa3167442020-02-10 11:49:52 +0000122 bs->remote_min_echo_rx_nsec = 0;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200123 bs->remote_demand = 0;
Klement Sekerab17dd962017-01-09 07:43:48 +0100124 bs->auth.remote_seq_number = 0;
125 bs->auth.remote_seq_number_known = 0;
126 bs->auth.local_seq_number = random_u32 (&bm->random_seed);
Klement Sekera239790f2017-02-16 10:53:53 +0100127 bs->echo_secret = random_u32 (&bm->random_seed);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200128}
129
130static void
131bfd_set_diag (bfd_session_t * bs, bfd_diag_code_e code)
132{
133 if (bs->local_diag != code)
134 {
135 BFD_DBG ("set local_diag, bs_idx=%d: '%d:%s'", bs->bs_idx, code,
136 bfd_diag_code_string (code));
137 bs->local_diag = code;
138 }
139}
140
141static void
Klement Sekeraa3167442020-02-10 11:49:52 +0000142bfd_set_state (vlib_main_t * vm, bfd_main_t * bm, bfd_session_t * bs,
Klement Sekerae4504c62016-12-08 10:16:41 +0100143 bfd_state_e new_state, int handling_wakeup)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200144{
145 if (bs->local_state != new_state)
146 {
147 BFD_DBG ("Change state, bs_idx=%d: %s->%s", bs->bs_idx,
148 bfd_state_string (bs->local_state),
149 bfd_state_string (new_state));
150 bs->local_state = new_state;
Klement Sekeraa3167442020-02-10 11:49:52 +0000151 bfd_on_state_change (bm, bs, bfd_time_now_nsec (vm, NULL),
152 handling_wakeup);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200153 }
154}
155
Klement Sekera73884482017-02-23 09:26:30 +0100156const char *
Klement Sekera239790f2017-02-16 10:53:53 +0100157bfd_poll_state_string (bfd_poll_state_e state)
158{
159 switch (state)
160 {
161#define F(x) \
162 case BFD_POLL_##x: \
163 return "BFD_POLL_" #x;
164 foreach_bfd_poll_state (F)
165#undef F
166 }
167 return "UNKNOWN";
168}
169
170static void
171bfd_set_poll_state (bfd_session_t * bs, bfd_poll_state_e state)
172{
173 if (bs->poll_state != state)
174 {
175 BFD_DBG ("Setting poll state=%s, bs_idx=%u",
176 bfd_poll_state_string (state), bs->bs_idx);
177 bs->poll_state = state;
178 }
179}
180
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200181static void
182bfd_recalc_tx_interval (bfd_main_t * bm, bfd_session_t * bs)
183{
Klement Sekeraa3167442020-02-10 11:49:52 +0000184 bs->transmit_interval_nsec =
185 clib_max (bs->effective_desired_min_tx_nsec, bs->remote_min_rx_nsec);
Klement Sekera239790f2017-02-16 10:53:53 +0100186 BFD_DBG ("Recalculated transmit interval " BFD_CLK_FMT,
Klement Sekeraa3167442020-02-10 11:49:52 +0000187 BFD_CLK_PRN (bs->transmit_interval_nsec));
Klement Sekera239790f2017-02-16 10:53:53 +0100188}
189
190static void
191bfd_recalc_echo_tx_interval (bfd_main_t * bm, bfd_session_t * bs)
192{
Klement Sekeraa3167442020-02-10 11:49:52 +0000193 bs->echo_transmit_interval_nsec =
194 clib_max (bs->effective_desired_min_tx_nsec, bs->remote_min_echo_rx_nsec);
Klement Sekera239790f2017-02-16 10:53:53 +0100195 BFD_DBG ("Recalculated echo transmit interval " BFD_CLK_FMT,
Klement Sekeraa3167442020-02-10 11:49:52 +0000196 BFD_CLK_PRN (bs->echo_transmit_interval_nsec));
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200197}
198
199static void
200bfd_calc_next_tx (bfd_main_t * bm, bfd_session_t * bs, u64 now)
201{
Klement Sekera239790f2017-02-16 10:53:53 +0100202 if (bs->local_detect_mult > 1)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200203 {
Klement Sekera239790f2017-02-16 10:53:53 +0100204 /* common case - 75-100% of transmit interval */
Klement Sekeraa3167442020-02-10 11:49:52 +0000205 bs->tx_timeout_nsec = bs->last_tx_nsec +
Klement Sekera239790f2017-02-16 10:53:53 +0100206 (1 - .25 * (random_f64 (&bm->random_seed))) *
Klement Sekeraa3167442020-02-10 11:49:52 +0000207 bs->transmit_interval_nsec;
208 if (bs->tx_timeout_nsec < now)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200209 {
Klement Sekera239790f2017-02-16 10:53:53 +0100210 /*
211 * the timeout is in the past, which means that either remote
212 * demand mode was set or performance/clock issues ...
213 */
214 BFD_DBG ("Missed %lu transmit events (now is %lu, calc "
215 "tx_timeout is %lu)",
Klement Sekeraa3167442020-02-10 11:49:52 +0000216 (now - bs->tx_timeout_nsec) /
217 bs->transmit_interval_nsec, now, bs->tx_timeout_nsec);
218 bs->tx_timeout_nsec = now;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200219 }
220 }
221 else
222 {
Klement Sekera239790f2017-02-16 10:53:53 +0100223 /* special case - 75-90% of transmit interval */
Klement Sekeraa3167442020-02-10 11:49:52 +0000224 bs->tx_timeout_nsec = bs->last_tx_nsec +
Klement Sekera239790f2017-02-16 10:53:53 +0100225 (.9 - .15 * (random_f64 (&bm->random_seed))) *
Klement Sekeraa3167442020-02-10 11:49:52 +0000226 bs->transmit_interval_nsec;
227 if (bs->tx_timeout_nsec < now)
Klement Sekera239790f2017-02-16 10:53:53 +0100228 {
229 /*
230 * the timeout is in the past, which means that either remote
231 * demand mode was set or performance/clock issues ...
232 */
233 BFD_DBG ("Missed %lu transmit events (now is %lu, calc "
234 "tx_timeout is %lu)",
Klement Sekeraa3167442020-02-10 11:49:52 +0000235 (now - bs->tx_timeout_nsec) /
236 bs->transmit_interval_nsec, now, bs->tx_timeout_nsec);
237 bs->tx_timeout_nsec = now;
Klement Sekera239790f2017-02-16 10:53:53 +0100238 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200239 }
Klement Sekeraa3167442020-02-10 11:49:52 +0000240 if (bs->tx_timeout_nsec)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200241 {
Klement Sekeraa3167442020-02-10 11:49:52 +0000242 BFD_DBG ("Next transmit in %lu nsec/%.02fs@%lu",
243 bs->tx_timeout_nsec - now,
244 (bs->tx_timeout_nsec - now) * SEC_PER_NSEC,
245 bs->tx_timeout_nsec);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200246 }
247}
248
249static void
Klement Sekera239790f2017-02-16 10:53:53 +0100250bfd_calc_next_echo_tx (bfd_main_t * bm, bfd_session_t * bs, u64 now)
251{
Klement Sekeraa3167442020-02-10 11:49:52 +0000252 bs->echo_tx_timeout_nsec =
253 bs->echo_last_tx_nsec + bs->echo_transmit_interval_nsec;
254 if (bs->echo_tx_timeout_nsec < now)
Klement Sekera239790f2017-02-16 10:53:53 +0100255 {
256 /* huh, we've missed it already, transmit now */
257 BFD_DBG ("Missed %lu echo transmit events (now is %lu, calc tx_timeout "
258 "is %lu)",
Klement Sekeraa3167442020-02-10 11:49:52 +0000259 (now - bs->echo_tx_timeout_nsec) /
260 bs->echo_transmit_interval_nsec,
261 now, bs->echo_tx_timeout_nsec);
262 bs->echo_tx_timeout_nsec = now;
Klement Sekera239790f2017-02-16 10:53:53 +0100263 }
Klement Sekeraa3167442020-02-10 11:49:52 +0000264 BFD_DBG ("Next echo transmit in %lu nsec/%.02fs@%lu",
265 bs->echo_tx_timeout_nsec - now,
266 (bs->echo_tx_timeout_nsec - now) * SEC_PER_NSEC,
267 bs->echo_tx_timeout_nsec);
Klement Sekera239790f2017-02-16 10:53:53 +0100268}
269
270static void
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200271bfd_recalc_detection_time (bfd_main_t * bm, bfd_session_t * bs)
272{
Klement Sekera73884482017-02-23 09:26:30 +0100273 if (bs->local_state == BFD_STATE_init || bs->local_state == BFD_STATE_up)
274 {
Klement Sekeraa3167442020-02-10 11:49:52 +0000275 bs->detection_time_nsec =
Klement Sekera73884482017-02-23 09:26:30 +0100276 bs->remote_detect_mult *
Klement Sekeraa3167442020-02-10 11:49:52 +0000277 clib_max (bs->effective_required_min_rx_nsec,
278 bs->remote_desired_min_tx_nsec);
279 BFD_DBG ("Recalculated detection time %lu nsec/%.3fs",
280 bs->detection_time_nsec,
281 bs->detection_time_nsec * SEC_PER_NSEC);
Klement Sekera73884482017-02-23 09:26:30 +0100282 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200283}
284
285static void
Klement Sekerae4504c62016-12-08 10:16:41 +0100286bfd_set_timer (bfd_main_t * bm, bfd_session_t * bs, u64 now,
287 int handling_wakeup)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200288{
289 u64 next = 0;
290 u64 rx_timeout = 0;
Klement Sekera239790f2017-02-16 10:53:53 +0100291 u64 tx_timeout = 0;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200292 if (BFD_STATE_up == bs->local_state)
293 {
Klement Sekeraa3167442020-02-10 11:49:52 +0000294 rx_timeout = bs->last_rx_nsec + bs->detection_time_nsec;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200295 }
Klement Sekera73884482017-02-23 09:26:30 +0100296 if (BFD_STATE_up != bs->local_state ||
297 (!bs->remote_demand && bs->remote_min_rx_usec) ||
Klement Sekera239790f2017-02-16 10:53:53 +0100298 BFD_POLL_NOT_NEEDED != bs->poll_state)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200299 {
Klement Sekeraa3167442020-02-10 11:49:52 +0000300 tx_timeout = bs->tx_timeout_nsec;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200301 }
Klement Sekera239790f2017-02-16 10:53:53 +0100302 if (tx_timeout && rx_timeout)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200303 {
Klement Sekera239790f2017-02-16 10:53:53 +0100304 next = clib_min (tx_timeout, rx_timeout);
305 }
306 else if (tx_timeout)
307 {
308 next = tx_timeout;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200309 }
310 else if (rx_timeout)
311 {
312 next = rx_timeout;
313 }
Klement Sekeraa3167442020-02-10 11:49:52 +0000314 if (bs->echo && next > bs->echo_tx_timeout_nsec)
Klement Sekera239790f2017-02-16 10:53:53 +0100315 {
Klement Sekeraa3167442020-02-10 11:49:52 +0000316 next = bs->echo_tx_timeout_nsec;
Klement Sekera239790f2017-02-16 10:53:53 +0100317 }
318 BFD_DBG ("bs_idx=%u, tx_timeout=%lu, echo_tx_timeout=%lu, rx_timeout=%lu, "
319 "next=%s",
Klement Sekeraa3167442020-02-10 11:49:52 +0000320 bs->bs_idx, tx_timeout, bs->echo_tx_timeout_nsec, rx_timeout,
Klement Sekera239790f2017-02-16 10:53:53 +0100321 next == tx_timeout
Klement Sekeraa3167442020-02-10 11:49:52 +0000322 ? "tx" : (next == bs->echo_tx_timeout_nsec ? "echo tx" : "rx"));
323 if (next)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200324 {
Dave Barach1e3417f2018-07-25 08:30:27 -0400325 int send_signal = 0;
Klement Sekeraa3167442020-02-10 11:49:52 +0000326 bs->event_time_nsec = next;
327 /* add extra tick if it's not even */
328 u32 wheel_time_ticks =
329 (bs->event_time_nsec - now) / bm->nsec_per_tw_tick +
330 ((bs->event_time_nsec - now) % bm->nsec_per_tw_tick != 0);
331 BFD_DBG ("event_time_nsec %lu (%lu nsec/%.3fs in future) -> "
332 "wheel_time_ticks %u", bs->event_time_nsec,
333 bs->event_time_nsec - now,
334 (bs->event_time_nsec - now) * SEC_PER_NSEC, wheel_time_ticks);
Dave Barachca578522020-04-08 08:45:27 -0400335 wheel_time_ticks = wheel_time_ticks ? wheel_time_ticks : 1;
Dave Barach1e3417f2018-07-25 08:30:27 -0400336 bfd_lock (bm);
Klement Sekeraa3167442020-02-10 11:49:52 +0000337 if (bs->tw_id)
338 {
339 TW (tw_timer_update) (&bm->wheel, bs->tw_id, wheel_time_ticks);
340 BFD_DBG ("tw_timer_update(%p, %u, %lu);", &bm->wheel, bs->tw_id,
341 wheel_time_ticks);
342 }
343 else
344 {
345 bs->tw_id =
346 TW (tw_timer_start) (&bm->wheel, bs->bs_idx, 0, wheel_time_ticks);
347 BFD_DBG ("tw_timer_start(%p, %u, 0, %lu) == %u;", &bm->wheel,
348 bs->bs_idx, wheel_time_ticks);
349 }
Dave Barach1e3417f2018-07-25 08:30:27 -0400350
Klement Sekerae4504c62016-12-08 10:16:41 +0100351 if (!handling_wakeup)
352 {
Dave Barach1e3417f2018-07-25 08:30:27 -0400353
354 /* Send only if it is earlier than current awaited wakeup time */
355 send_signal =
Klement Sekeraa3167442020-02-10 11:49:52 +0000356 (bs->event_time_nsec < bm->bfd_process_next_wakeup_nsec) &&
Dave Barach1e3417f2018-07-25 08:30:27 -0400357 /*
358 * If the wake-up time is within 2x the delay of the event propagation delay,
359 * avoid the expense of sending the event. The 2x multiplier is to workaround the race whereby
360 * simultaneous event + expired timer create one recurring bogus wakeup/suspend instance,
361 * due to double scheduling of the node on the pending list.
362 */
Klement Sekeraa3167442020-02-10 11:49:52 +0000363 (bm->bfd_process_next_wakeup_nsec - bs->event_time_nsec >
364 2 * bm->bfd_process_wakeup_event_delay_nsec) &&
Dave Barach1e3417f2018-07-25 08:30:27 -0400365 /* Must be no events in flight to send an event */
366 (!bm->bfd_process_wakeup_events_in_flight);
367
368 /* If we do send the signal, note this down along with the start timestamp */
369 if (send_signal)
370 {
371 bm->bfd_process_wakeup_events_in_flight++;
Klement Sekeraa3167442020-02-10 11:49:52 +0000372 bm->bfd_process_wakeup_event_start_nsec = now;
Dave Barach1e3417f2018-07-25 08:30:27 -0400373 }
374 }
375 bfd_unlock (bm);
376
377 /* Use the multithreaded event sending so the workers can send events too */
378 if (send_signal)
379 {
380 vlib_process_signal_event_mt (bm->vlib_main,
381 bm->bfd_process_node_index,
382 BFD_EVENT_RESCHEDULE, ~0);
Klement Sekerae4504c62016-12-08 10:16:41 +0100383 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200384 }
385}
386
387static void
Klement Sekeraa57a9702017-02-02 06:58:07 +0100388bfd_set_effective_desired_min_tx (bfd_main_t * bm,
389 bfd_session_t * bs, u64 now,
Klement Sekeraa3167442020-02-10 11:49:52 +0000390 u64 desired_min_tx_nsec)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200391{
Klement Sekeraa3167442020-02-10 11:49:52 +0000392 bs->effective_desired_min_tx_nsec = desired_min_tx_nsec;
Klement Sekeraa57a9702017-02-02 06:58:07 +0100393 BFD_DBG ("Set effective desired min tx to " BFD_CLK_FMT,
Klement Sekeraa3167442020-02-10 11:49:52 +0000394 BFD_CLK_PRN (bs->effective_desired_min_tx_nsec));
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200395 bfd_recalc_detection_time (bm, bs);
396 bfd_recalc_tx_interval (bm, bs);
Klement Sekera239790f2017-02-16 10:53:53 +0100397 bfd_recalc_echo_tx_interval (bm, bs);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200398 bfd_calc_next_tx (bm, bs, now);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200399}
400
Klement Sekera637b9c42016-12-08 05:19:14 +0100401static void
Klement Sekeraa57a9702017-02-02 06:58:07 +0100402bfd_set_effective_required_min_rx (bfd_main_t * bm,
Klement Sekera73884482017-02-23 09:26:30 +0100403 bfd_session_t * bs,
Klement Sekeraa3167442020-02-10 11:49:52 +0000404 u64 required_min_rx_nsec)
Klement Sekeraa57a9702017-02-02 06:58:07 +0100405{
Klement Sekeraa3167442020-02-10 11:49:52 +0000406 bs->effective_required_min_rx_nsec = required_min_rx_nsec;
Klement Sekeraa57a9702017-02-02 06:58:07 +0100407 BFD_DBG ("Set effective required min rx to " BFD_CLK_FMT,
Klement Sekeraa3167442020-02-10 11:49:52 +0000408 BFD_CLK_PRN (bs->effective_required_min_rx_nsec));
Klement Sekeraa57a9702017-02-02 06:58:07 +0100409 bfd_recalc_detection_time (bm, bs);
Klement Sekeraa57a9702017-02-02 06:58:07 +0100410}
411
412static void
Klement Sekera637b9c42016-12-08 05:19:14 +0100413bfd_set_remote_required_min_rx (bfd_main_t * bm, bfd_session_t * bs,
Klement Sekera239790f2017-02-16 10:53:53 +0100414 u64 now, u32 remote_required_min_rx_usec)
Klement Sekera637b9c42016-12-08 05:19:14 +0100415{
Klement Sekera239790f2017-02-16 10:53:53 +0100416 if (bs->remote_min_rx_usec != remote_required_min_rx_usec)
417 {
418 bs->remote_min_rx_usec = remote_required_min_rx_usec;
Klement Sekeraa3167442020-02-10 11:49:52 +0000419 bs->remote_min_rx_nsec = bfd_usec_to_nsec (remote_required_min_rx_usec);
Klement Sekera239790f2017-02-16 10:53:53 +0100420 BFD_DBG ("Set remote min rx to " BFD_CLK_FMT,
Klement Sekeraa3167442020-02-10 11:49:52 +0000421 BFD_CLK_PRN (bs->remote_min_rx_nsec));
Klement Sekera239790f2017-02-16 10:53:53 +0100422 bfd_recalc_detection_time (bm, bs);
423 bfd_recalc_tx_interval (bm, bs);
424 }
425}
426
427static void
428bfd_set_remote_required_min_echo_rx (bfd_main_t * bm, bfd_session_t * bs,
429 u64 now,
430 u32 remote_required_min_echo_rx_usec)
431{
432 if (bs->remote_min_echo_rx_usec != remote_required_min_echo_rx_usec)
433 {
434 bs->remote_min_echo_rx_usec = remote_required_min_echo_rx_usec;
Klement Sekeraa3167442020-02-10 11:49:52 +0000435 bs->remote_min_echo_rx_nsec =
436 bfd_usec_to_nsec (bs->remote_min_echo_rx_usec);
Klement Sekera239790f2017-02-16 10:53:53 +0100437 BFD_DBG ("Set remote min echo rx to " BFD_CLK_FMT,
Klement Sekeraa3167442020-02-10 11:49:52 +0000438 BFD_CLK_PRN (bs->remote_min_echo_rx_nsec));
Klement Sekera239790f2017-02-16 10:53:53 +0100439 bfd_recalc_echo_tx_interval (bm, bs);
440 }
Klement Sekera637b9c42016-12-08 05:19:14 +0100441}
442
Neale Ranns88fc83e2017-04-05 08:11:14 -0700443static void
444bfd_notify_listeners (bfd_main_t * bm,
445 bfd_listen_event_e event, const bfd_session_t * bs)
446{
447 bfd_notify_fn_t *fn;
448 vec_foreach (fn, bm->listeners)
449 {
450 (*fn) (event, bs);
451 }
452}
453
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200454void
455bfd_session_start (bfd_main_t * bm, bfd_session_t * bs)
456{
Klement Sekera239790f2017-02-16 10:53:53 +0100457 BFD_DBG ("\nStarting session: %U", format_bfd_session, bs);
Damjan Marion07a38572018-01-21 06:44:18 -0800458 vlib_log_info (bm->log_class, "start BFD session: %U",
459 format_bfd_session_brief, bs);
Klement Sekeraa3167442020-02-10 11:49:52 +0000460 bfd_set_effective_required_min_rx (bm, bs, bs->config_required_min_rx_nsec);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200461 bfd_recalc_tx_interval (bm, bs);
462 vlib_process_signal_event (bm->vlib_main, bm->bfd_process_node_index,
463 BFD_EVENT_NEW_SESSION, bs->bs_idx);
Neale Ranns88fc83e2017-04-05 08:11:14 -0700464 bfd_notify_listeners (bm, BFD_LISTEN_EVENT_CREATE, bs);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200465}
466
Klement Sekerab17dd962017-01-09 07:43:48 +0100467void
Klement Sekeraa3167442020-02-10 11:49:52 +0000468bfd_session_set_flags (vlib_main_t * vm, bfd_session_t * bs, u8 admin_up_down)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200469{
470 bfd_main_t *bm = &bfd_main;
Klement Sekeraa3167442020-02-10 11:49:52 +0000471 u64 now = bfd_time_now_nsec (vm, NULL);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200472 if (admin_up_down)
473 {
Klement Sekerac48829b2017-02-14 07:55:57 +0100474 BFD_DBG ("Session set admin-up, bs-idx=%u", bs->bs_idx);
Damjan Marion07a38572018-01-21 06:44:18 -0800475 vlib_log_info (bm->log_class, "set session admin-up: %U",
476 format_bfd_session_brief, bs);
Klement Sekeraa3167442020-02-10 11:49:52 +0000477 bfd_set_state (vm, bm, bs, BFD_STATE_down, 0);
Klement Sekerac48829b2017-02-14 07:55:57 +0100478 bfd_set_diag (bs, BFD_DIAG_CODE_no_diag);
Klement Sekera73884482017-02-23 09:26:30 +0100479 bfd_calc_next_tx (bm, bs, now);
480 bfd_set_timer (bm, bs, now, 0);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200481 }
482 else
483 {
Klement Sekerac48829b2017-02-14 07:55:57 +0100484 BFD_DBG ("Session set admin-down, bs-idx=%u", bs->bs_idx);
Damjan Marion07a38572018-01-21 06:44:18 -0800485 vlib_log_info (bm->log_class, "set session admin-down: %U",
486 format_bfd_session_brief, bs);
Klement Sekerac48829b2017-02-14 07:55:57 +0100487 bfd_set_diag (bs, BFD_DIAG_CODE_admin_down);
Klement Sekeraa3167442020-02-10 11:49:52 +0000488 bfd_set_state (vm, bm, bs, BFD_STATE_admin_down, 0);
Klement Sekera73884482017-02-23 09:26:30 +0100489 bfd_calc_next_tx (bm, bs, now);
490 bfd_set_timer (bm, bs, now, 0);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200491 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200492}
493
494u8 *
495bfd_input_format_trace (u8 * s, va_list * args)
496{
497 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
498 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
499 const bfd_input_trace_t *t = va_arg (*args, bfd_input_trace_t *);
500 const bfd_pkt_t *pkt = (bfd_pkt_t *) t->data;
501 if (t->len > STRUCT_SIZE_OF (bfd_pkt_t, head))
502 {
503 s = format (s, "BFD v%u, diag=%u(%s), state=%u(%s),\n"
Klement Sekerab17dd962017-01-09 07:43:48 +0100504 " flags=(P:%u, F:%u, C:%u, A:%u, D:%u, M:%u), "
505 "detect_mult=%u, length=%u\n",
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200506 bfd_pkt_get_version (pkt), bfd_pkt_get_diag_code (pkt),
507 bfd_diag_code_string (bfd_pkt_get_diag_code (pkt)),
508 bfd_pkt_get_state (pkt),
509 bfd_state_string (bfd_pkt_get_state (pkt)),
510 bfd_pkt_get_poll (pkt), bfd_pkt_get_final (pkt),
511 bfd_pkt_get_control_plane_independent (pkt),
512 bfd_pkt_get_auth_present (pkt), bfd_pkt_get_demand (pkt),
513 bfd_pkt_get_multipoint (pkt), pkt->head.detect_mult,
514 pkt->head.length);
Klement Sekerab17dd962017-01-09 07:43:48 +0100515 if (t->len >= sizeof (bfd_pkt_t) &&
516 pkt->head.length >= sizeof (bfd_pkt_t))
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200517 {
Klement Sekerad3ba5152017-02-14 03:09:17 +0100518 s = format (s, " my discriminator: %u\n",
519 clib_net_to_host_u32 (pkt->my_disc));
520 s = format (s, " your discriminator: %u\n",
521 clib_net_to_host_u32 (pkt->your_disc));
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200522 s = format (s, " desired min tx interval: %u\n",
523 clib_net_to_host_u32 (pkt->des_min_tx));
524 s = format (s, " required min rx interval: %u\n",
525 clib_net_to_host_u32 (pkt->req_min_rx));
Klement Sekera46a87ad2017-01-02 08:22:23 +0100526 s = format (s, " required min echo rx interval: %u",
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200527 clib_net_to_host_u32 (pkt->req_min_echo_rx));
528 }
Klement Sekerad3ba5152017-02-14 03:09:17 +0100529 if (t->len >= sizeof (bfd_pkt_with_common_auth_t) &&
530 pkt->head.length >= sizeof (bfd_pkt_with_common_auth_t) &&
531 bfd_pkt_get_auth_present (pkt))
532 {
533 const bfd_pkt_with_common_auth_t *with_auth = (void *) pkt;
534 const bfd_auth_common_t *common = &with_auth->common_auth;
535 s = format (s, "\n auth len: %u\n", common->len);
536 s = format (s, " auth type: %u:%s\n", common->type,
537 bfd_auth_type_str (common->type));
538 if (t->len >= sizeof (bfd_pkt_with_sha1_auth_t) &&
539 pkt->head.length >= sizeof (bfd_pkt_with_sha1_auth_t) &&
540 (BFD_AUTH_TYPE_keyed_sha1 == common->type ||
541 BFD_AUTH_TYPE_meticulous_keyed_sha1 == common->type))
542 {
543 const bfd_pkt_with_sha1_auth_t *with_sha1 = (void *) pkt;
544 const bfd_auth_sha1_t *sha1 = &with_sha1->sha1_auth;
545 s = format (s, " seq num: %u\n",
546 clib_net_to_host_u32 (sha1->seq_num));
547 s = format (s, " key id: %u\n", sha1->key_id);
548 s = format (s, " hash: %U", format_hex_bytes, sha1->hash,
549 sizeof (sha1->hash));
550 }
551 }
552 else
553 {
554 s = format (s, "\n");
555 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200556 }
557
558 return s;
559}
560
Dave Barach1e3417f2018-07-25 08:30:27 -0400561typedef struct
562{
563 u32 bs_idx;
564} bfd_rpc_event_t;
565
566static void
567bfd_rpc_event_cb (const bfd_rpc_event_t * a)
568{
569 bfd_main_t *bm = &bfd_main;
570 u32 bs_idx = a->bs_idx;
571 u32 valid_bs = 0;
572 bfd_session_t session_data;
573
574 bfd_lock (bm);
575 if (!pool_is_free_index (bm->sessions, bs_idx))
576 {
577 bfd_session_t *bs = pool_elt_at_index (bm->sessions, bs_idx);
578 clib_memcpy (&session_data, bs, sizeof (bfd_session_t));
579 valid_bs = 1;
580 }
581 else
582 {
583 BFD_DBG ("Ignoring event RPC for non-existent session index %u",
584 bs_idx);
585 }
586 bfd_unlock (bm);
587
588 if (valid_bs)
589 bfd_event (bm, &session_data);
590}
591
592static void
593bfd_event_rpc (u32 bs_idx)
594{
595 const u32 data_size = sizeof (bfd_rpc_event_t);
596 u8 data[data_size];
597 bfd_rpc_event_t *event = (bfd_rpc_event_t *) data;
598
599 event->bs_idx = bs_idx;
600 vl_api_rpc_call_main_thread (bfd_rpc_event_cb, data, data_size);
601}
602
603typedef struct
604{
605 u32 bs_idx;
606} bfd_rpc_notify_listeners_t;
607
608static void
609bfd_rpc_notify_listeners_cb (const bfd_rpc_notify_listeners_t * a)
610{
611 bfd_main_t *bm = &bfd_main;
612 u32 bs_idx = a->bs_idx;
613 bfd_lock (bm);
614 if (!pool_is_free_index (bm->sessions, bs_idx))
615 {
616 bfd_session_t *bs = pool_elt_at_index (bm->sessions, bs_idx);
617 bfd_notify_listeners (bm, BFD_LISTEN_EVENT_UPDATE, bs);
618 }
619 else
620 {
621 BFD_DBG ("Ignoring notify RPC for non-existent session index %u",
622 bs_idx);
623 }
624 bfd_unlock (bm);
625}
626
627static void
628bfd_notify_listeners_rpc (u32 bs_idx)
629{
630 const u32 data_size = sizeof (bfd_rpc_notify_listeners_t);
631 u8 data[data_size];
632 bfd_rpc_notify_listeners_t *notify = (bfd_rpc_notify_listeners_t *) data;
633 notify->bs_idx = bs_idx;
634 vl_api_rpc_call_main_thread (bfd_rpc_notify_listeners_cb, data, data_size);
635}
636
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200637static void
Klement Sekerae4504c62016-12-08 10:16:41 +0100638bfd_on_state_change (bfd_main_t * bm, bfd_session_t * bs, u64 now,
639 int handling_wakeup)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200640{
Klement Sekera239790f2017-02-16 10:53:53 +0100641 BFD_DBG ("\nState changed: %U", format_bfd_session, bs);
Dave Barach1e3417f2018-07-25 08:30:27 -0400642
643 if (vlib_get_thread_index () == 0)
644 {
645 bfd_event (bm, bs);
646 }
647 else
648 {
649 /* without RPC - a REGRESSION: BFD event are not propagated */
650 bfd_event_rpc (bs->bs_idx);
651 }
652
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200653 switch (bs->local_state)
654 {
655 case BFD_STATE_admin_down:
Klement Sekera239790f2017-02-16 10:53:53 +0100656 bs->echo = 0;
Klement Sekeraa57a9702017-02-02 06:58:07 +0100657 bfd_set_effective_desired_min_tx (bm, bs, now,
658 clib_max
Klement Sekeraa3167442020-02-10 11:49:52 +0000659 (bs->config_desired_min_tx_nsec,
660 bm->default_desired_min_tx_nsec));
Klement Sekera73884482017-02-23 09:26:30 +0100661 bfd_set_effective_required_min_rx (bm, bs,
Klement Sekeraa3167442020-02-10 11:49:52 +0000662 bs->config_required_min_rx_nsec);
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100663 bfd_set_timer (bm, bs, now, handling_wakeup);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200664 break;
665 case BFD_STATE_down:
Klement Sekera239790f2017-02-16 10:53:53 +0100666 bs->echo = 0;
Klement Sekeraa57a9702017-02-02 06:58:07 +0100667 bfd_set_effective_desired_min_tx (bm, bs, now,
668 clib_max
Klement Sekeraa3167442020-02-10 11:49:52 +0000669 (bs->config_desired_min_tx_nsec,
670 bm->default_desired_min_tx_nsec));
Klement Sekera73884482017-02-23 09:26:30 +0100671 bfd_set_effective_required_min_rx (bm, bs,
Klement Sekeraa3167442020-02-10 11:49:52 +0000672 bs->config_required_min_rx_nsec);
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100673 bfd_set_timer (bm, bs, now, handling_wakeup);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200674 break;
675 case BFD_STATE_init:
Klement Sekera239790f2017-02-16 10:53:53 +0100676 bs->echo = 0;
Klement Sekeraa57a9702017-02-02 06:58:07 +0100677 bfd_set_effective_desired_min_tx (bm, bs, now,
Klement Sekeraa3167442020-02-10 11:49:52 +0000678 bs->config_desired_min_tx_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_up:
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100682 bfd_set_effective_desired_min_tx (bm, bs, now,
Klement Sekeraa3167442020-02-10 11:49:52 +0000683 bs->config_desired_min_tx_nsec);
Klement Sekera239790f2017-02-16 10:53:53 +0100684 if (BFD_POLL_NOT_NEEDED == bs->poll_state)
Klement Sekeraa57a9702017-02-02 06:58:07 +0100685 {
Klement Sekera73884482017-02-23 09:26:30 +0100686 bfd_set_effective_required_min_rx (bm, bs,
Klement Sekeraa3167442020-02-10 11:49:52 +0000687 bs->config_required_min_rx_nsec);
Klement Sekeraa57a9702017-02-02 06:58:07 +0100688 }
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100689 bfd_set_timer (bm, bs, now, handling_wakeup);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200690 break;
691 }
Dave Barach1e3417f2018-07-25 08:30:27 -0400692 if (vlib_get_thread_index () == 0)
693 {
694 bfd_notify_listeners (bm, BFD_LISTEN_EVENT_UPDATE, bs);
695 }
696 else
697 {
698 /* without RPC - a REGRESSION: state changes are not propagated */
699 bfd_notify_listeners_rpc (bs->bs_idx);
700 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200701}
702
703static void
Klement Sekeraa57a9702017-02-02 06:58:07 +0100704bfd_on_config_change (vlib_main_t * vm, vlib_node_runtime_t * rt,
705 bfd_main_t * bm, bfd_session_t * bs, u64 now)
706{
Klement Sekera239790f2017-02-16 10:53:53 +0100707 /*
708 * if remote demand mode is set and we need to do a poll, set the next
709 * timeout so that the session wakes up immediately
710 */
711 if (bs->remote_demand && BFD_POLL_NEEDED == bs->poll_state &&
Klement Sekeraa3167442020-02-10 11:49:52 +0000712 bs->poll_state_start_or_timeout_nsec < now)
Klement Sekeraa57a9702017-02-02 06:58:07 +0100713 {
Klement Sekeraa3167442020-02-10 11:49:52 +0000714 bs->tx_timeout_nsec = now;
Klement Sekeraa57a9702017-02-02 06:58:07 +0100715 }
716 bfd_recalc_detection_time (bm, bs);
717 bfd_set_timer (bm, bs, now, 0);
718}
719
720static void
Klement Sekerae50e8562017-04-04 16:19:48 +0200721bfd_add_transport_layer (vlib_main_t * vm, u32 bi, bfd_session_t * bs)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200722{
723 switch (bs->transport)
724 {
725 case BFD_TRANSPORT_UDP4:
Klement Sekera46a87ad2017-01-02 08:22:23 +0100726 BFD_DBG ("Transport bfd via udp4, bs_idx=%u", bs->bs_idx);
Klement Sekerae50e8562017-04-04 16:19:48 +0200727 bfd_add_udp4_transport (vm, bi, bs, 0 /* is_echo */ );
Klement Sekera46a87ad2017-01-02 08:22:23 +0100728 break;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200729 case BFD_TRANSPORT_UDP6:
Klement Sekera46a87ad2017-01-02 08:22:23 +0100730 BFD_DBG ("Transport bfd via udp6, bs_idx=%u", bs->bs_idx);
Klement Sekerae50e8562017-04-04 16:19:48 +0200731 bfd_add_udp6_transport (vm, bi, bs, 0 /* is_echo */ );
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200732 break;
733 }
734}
735
Klement Sekera239790f2017-02-16 10:53:53 +0100736static int
Klement Sekerae50e8562017-04-04 16:19:48 +0200737bfd_transport_control_frame (vlib_main_t * vm, u32 bi, bfd_session_t * bs)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200738{
Klement Sekera239790f2017-02-16 10:53:53 +0100739 switch (bs->transport)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200740 {
Klement Sekera239790f2017-02-16 10:53:53 +0100741 case BFD_TRANSPORT_UDP4:
Klement Sekerae50e8562017-04-04 16:19:48 +0200742 BFD_DBG ("Transport bfd via udp4, bs_idx=%u", bs->bs_idx);
743 return bfd_transport_udp4 (vm, bi, bs);
Klement Sekera239790f2017-02-16 10:53:53 +0100744 break;
745 case BFD_TRANSPORT_UDP6:
Klement Sekerae50e8562017-04-04 16:19:48 +0200746 BFD_DBG ("Transport bfd via udp6, bs_idx=%u", bs->bs_idx);
747 return bfd_transport_udp6 (vm, bi, bs);
Klement Sekera239790f2017-02-16 10:53:53 +0100748 break;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200749 }
Klement Sekera239790f2017-02-16 10:53:53 +0100750 return 0;
751}
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200752
Klement Sekerae50e8562017-04-04 16:19:48 +0200753static int
754bfd_echo_add_transport_layer (vlib_main_t * vm, u32 bi, bfd_session_t * bs)
Klement Sekera239790f2017-02-16 10:53:53 +0100755{
Klement Sekerae50e8562017-04-04 16:19:48 +0200756 switch (bs->transport)
757 {
758 case BFD_TRANSPORT_UDP4:
759 BFD_DBG ("Transport bfd echo via udp4, bs_idx=%u", bs->bs_idx);
760 return bfd_add_udp4_transport (vm, bi, bs, 1 /* is_echo */ );
761 break;
762 case BFD_TRANSPORT_UDP6:
763 BFD_DBG ("Transport bfd echo via udp6, bs_idx=%u", bs->bs_idx);
764 return bfd_add_udp6_transport (vm, bi, bs, 1 /* is_echo */ );
765 break;
766 }
767 return 0;
768}
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200769
Klement Sekerae50e8562017-04-04 16:19:48 +0200770static int
771bfd_transport_echo (vlib_main_t * vm, u32 bi, bfd_session_t * bs)
772{
773 switch (bs->transport)
774 {
775 case BFD_TRANSPORT_UDP4:
776 BFD_DBG ("Transport bfd echo via udp4, bs_idx=%u", bs->bs_idx);
777 return bfd_transport_udp4 (vm, bi, bs);
778 break;
779 case BFD_TRANSPORT_UDP6:
780 BFD_DBG ("Transport bfd echo via udp6, bs_idx=%u", bs->bs_idx);
781 return bfd_transport_udp6 (vm, bi, bs);
782 break;
783 }
784 return 0;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200785}
786
Klement Sekerab17dd962017-01-09 07:43:48 +0100787#if WITH_LIBSSL > 0
788static void
789bfd_add_sha1_auth_section (vlib_buffer_t * b, bfd_session_t * bs)
790{
791 bfd_pkt_with_sha1_auth_t *pkt = vlib_buffer_get_current (b);
792 bfd_auth_sha1_t *auth = &pkt->sha1_auth;
793 b->current_length += sizeof (*auth);
794 pkt->pkt.head.length += sizeof (*auth);
795 bfd_pkt_set_auth_present (&pkt->pkt);
Dave Barachb7b92992018-10-17 10:38:51 -0400796 clib_memset (auth, 0, sizeof (*auth));
Klement Sekerab17dd962017-01-09 07:43:48 +0100797 auth->type_len.type = bs->auth.curr_key->auth_type;
798 /*
799 * only meticulous authentication types require incrementing seq number
800 * for every message, but doing so doesn't violate the RFC
801 */
802 ++bs->auth.local_seq_number;
803 auth->type_len.len = sizeof (bfd_auth_sha1_t);
804 auth->key_id = bs->auth.curr_bfd_key_id;
805 auth->seq_num = clib_host_to_net_u32 (bs->auth.local_seq_number);
806 /*
807 * first copy the password into the packet, then calculate the hash
808 * and finally replace the password with the calculated hash
809 */
810 clib_memcpy (auth->hash, bs->auth.curr_key->key,
811 sizeof (bs->auth.curr_key->key));
812 unsigned char hash[sizeof (auth->hash)];
813 SHA1 ((unsigned char *) pkt, sizeof (*pkt), hash);
814 BFD_DBG ("hashing: %U", format_hex_bytes, pkt, sizeof (*pkt));
815 clib_memcpy (auth->hash, hash, sizeof (hash));
Klement Sekerab17dd962017-01-09 07:43:48 +0100816}
Klement Sekera6f966492017-02-08 07:42:08 +0100817#endif
Klement Sekerab17dd962017-01-09 07:43:48 +0100818
819static void
820bfd_add_auth_section (vlib_buffer_t * b, bfd_session_t * bs)
821{
Damjan Marion07a38572018-01-21 06:44:18 -0800822 bfd_main_t *bm = &bfd_main;
Klement Sekerab17dd962017-01-09 07:43:48 +0100823 if (bs->auth.curr_key)
824 {
825 const bfd_auth_type_e auth_type = bs->auth.curr_key->auth_type;
826 switch (auth_type)
827 {
828 case BFD_AUTH_TYPE_reserved:
829 /* fallthrough */
830 case BFD_AUTH_TYPE_simple_password:
831 /* fallthrough */
832 case BFD_AUTH_TYPE_keyed_md5:
833 /* fallthrough */
834 case BFD_AUTH_TYPE_meticulous_keyed_md5:
Damjan Marion07a38572018-01-21 06:44:18 -0800835 vlib_log_crit (bm->log_class,
836 "internal error, unexpected BFD auth type '%d'",
837 auth_type);
Klement Sekerab17dd962017-01-09 07:43:48 +0100838 break;
839#if WITH_LIBSSL > 0
840 case BFD_AUTH_TYPE_keyed_sha1:
841 /* fallthrough */
842 case BFD_AUTH_TYPE_meticulous_keyed_sha1:
843 bfd_add_sha1_auth_section (b, bs);
844 break;
845#else
846 case BFD_AUTH_TYPE_keyed_sha1:
847 /* fallthrough */
848 case BFD_AUTH_TYPE_meticulous_keyed_sha1:
Damjan Marion07a38572018-01-21 06:44:18 -0800849 vlib_log_crit (bm->log_class,
850 "internal error, unexpected BFD auth type '%d'",
851 auth_type);
Klement Sekerab17dd962017-01-09 07:43:48 +0100852 break;
853#endif
854 }
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 Sekera239790f2017-02-16 10:53:53 +0100876bfd_init_control_frame (bfd_main_t * bm, bfd_session_t * bs,
877 vlib_buffer_t * b)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200878{
879 bfd_pkt_t *pkt = vlib_buffer_get_current (b);
Klement Sekerab17dd962017-01-09 07:43:48 +0100880 u32 bfd_length = 0;
881 bfd_length = sizeof (bfd_pkt_t);
Dave Barachb7b92992018-10-17 10:38:51 -0400882 clib_memset (pkt, 0, sizeof (*pkt));
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200883 bfd_pkt_set_version (pkt, 1);
884 bfd_pkt_set_diag_code (pkt, bs->local_diag);
885 bfd_pkt_set_state (pkt, bs->local_state);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200886 pkt->head.detect_mult = bs->local_detect_mult;
Klement Sekerae50e8562017-04-04 16:19:48 +0200887 pkt->head.length = bfd_length;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200888 pkt->my_disc = bs->local_discr;
889 pkt->your_disc = bs->remote_discr;
Klement Sekeraa57a9702017-02-02 06:58:07 +0100890 pkt->des_min_tx = clib_host_to_net_u32 (bs->config_desired_min_tx_usec);
Klement Sekera239790f2017-02-16 10:53:53 +0100891 if (bs->echo)
892 {
893 pkt->req_min_rx =
Klement Sekeraa3167442020-02-10 11:49:52 +0000894 clib_host_to_net_u32 (bfd_nsec_to_usec
895 (bs->effective_required_min_rx_nsec));
Klement Sekera239790f2017-02-16 10:53:53 +0100896 }
897 else
898 {
899 pkt->req_min_rx =
900 clib_host_to_net_u32 (bs->config_required_min_rx_usec);
901 }
Klement Sekeraa57a9702017-02-02 06:58:07 +0100902 pkt->req_min_echo_rx = clib_host_to_net_u32 (1);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200903 b->current_length = bfd_length;
904}
905
906static void
Klement Sekera239790f2017-02-16 10:53:53 +0100907bfd_send_echo (vlib_main_t * vm, vlib_node_runtime_t * rt,
Klement Sekerae50e8562017-04-04 16:19:48 +0200908 bfd_main_t * bm, bfd_session_t * bs, u64 now)
Klement Sekera239790f2017-02-16 10:53:53 +0100909{
910 if (!bfd_is_echo_possible (bs))
911 {
912 BFD_DBG ("\nSwitching off echo function: %U", format_bfd_session, bs);
913 bs->echo = 0;
914 return;
915 }
Klement Sekeraa3167442020-02-10 11:49:52 +0000916 if (now >= bs->echo_tx_timeout_nsec)
Klement Sekera239790f2017-02-16 10:53:53 +0100917 {
918 BFD_DBG ("\nSending echo packet: %U", format_bfd_session, bs);
919 u32 bi;
920 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
921 {
Damjan Marion07a38572018-01-21 06:44:18 -0800922 vlib_log_crit (bm->log_class, "buffer allocation failure");
Klement Sekera239790f2017-02-16 10:53:53 +0100923 return;
924 }
925 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
926 ASSERT (b->current_data == 0);
927 bfd_echo_pkt_t *pkt = vlib_buffer_get_current (b);
Dave Barachb7b92992018-10-17 10:38:51 -0400928 clib_memset (pkt, 0, sizeof (*pkt));
Klement Sekera239790f2017-02-16 10:53:53 +0100929 pkt->discriminator = bs->local_discr;
Klement Sekeraa3167442020-02-10 11:49:52 +0000930 pkt->expire_time_nsec =
931 now + bs->echo_transmit_interval_nsec * bs->local_detect_mult;
Klement Sekera239790f2017-02-16 10:53:53 +0100932 pkt->checksum =
Klement Sekeraa3167442020-02-10 11:49:52 +0000933 bfd_calc_echo_checksum (bs->local_discr, pkt->expire_time_nsec,
Klement Sekera239790f2017-02-16 10:53:53 +0100934 bs->echo_secret);
935 b->current_length = sizeof (*pkt);
Klement Sekerae50e8562017-04-04 16:19:48 +0200936 if (!bfd_echo_add_transport_layer (vm, bi, bs))
937 {
938 BFD_ERR ("cannot send echo packet out, turning echo off");
939 bs->echo = 0;
940 vlib_buffer_free_one (vm, bi);
941 return;
942 }
943 if (!bfd_transport_echo (vm, bi, bs))
Klement Sekera239790f2017-02-16 10:53:53 +0100944 {
945 BFD_ERR ("cannot send echo packet out, turning echo off");
946 bs->echo = 0;
947 vlib_buffer_free_one (vm, bi);
948 return;
949 }
Klement Sekeraa3167442020-02-10 11:49:52 +0000950 bs->echo_last_tx_nsec = now;
Klement Sekera239790f2017-02-16 10:53:53 +0100951 bfd_calc_next_echo_tx (bm, bs, now);
Klement Sekera239790f2017-02-16 10:53:53 +0100952 }
953 else
954 {
955 BFD_DBG
956 ("No need to send echo packet now, now is %lu, tx_timeout is %lu",
Klement Sekeraa3167442020-02-10 11:49:52 +0000957 now, bs->echo_tx_timeout_nsec);
Klement Sekera239790f2017-02-16 10:53:53 +0100958 }
959}
960
961static void
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200962bfd_send_periodic (vlib_main_t * vm, vlib_node_runtime_t * rt,
Klement Sekerae50e8562017-04-04 16:19:48 +0200963 bfd_main_t * bm, bfd_session_t * bs, u64 now)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200964{
Klement Sekera239790f2017-02-16 10:53:53 +0100965 if (!bs->remote_min_rx_usec && BFD_POLL_NOT_NEEDED == bs->poll_state)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200966 {
Klement Sekera239790f2017-02-16 10:53:53 +0100967 BFD_DBG ("Remote min rx interval is zero, not sending periodic control "
968 "frame");
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200969 return;
970 }
Klement Sekera239790f2017-02-16 10:53:53 +0100971 if (BFD_POLL_NOT_NEEDED == bs->poll_state && bs->remote_demand &&
Klement Sekerad3ba5152017-02-14 03:09:17 +0100972 BFD_STATE_up == bs->local_state && BFD_STATE_up == bs->remote_state)
973 {
974 /*
975 * A system MUST NOT periodically transmit BFD Control packets if Demand
976 * mode is active on the remote system (bfd.RemoteDemandMode is 1,
977 * bfd.SessionState is Up, and bfd.RemoteSessionState is Up) and a Poll
978 * Sequence is not being transmitted.
979 */
Klement Sekera239790f2017-02-16 10:53:53 +0100980 BFD_DBG ("Remote demand is set, not sending periodic control frame");
Klement Sekerad3ba5152017-02-14 03:09:17 +0100981 return;
982 }
Klement Sekeraa3167442020-02-10 11:49:52 +0000983 if (now >= bs->tx_timeout_nsec)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200984 {
Klement Sekera239790f2017-02-16 10:53:53 +0100985 BFD_DBG ("\nSending periodic control frame: %U", format_bfd_session,
986 bs);
987 u32 bi;
988 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200989 {
Damjan Marion07a38572018-01-21 06:44:18 -0800990 vlib_log_crit (bm->log_class, "buffer allocation failure");
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200991 return;
992 }
Klement Sekera239790f2017-02-16 10:53:53 +0100993 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
994 ASSERT (b->current_data == 0);
995 bfd_init_control_frame (bm, bs, b);
996 switch (bs->poll_state)
Klement Sekeraa57a9702017-02-02 06:58:07 +0100997 {
Klement Sekera239790f2017-02-16 10:53:53 +0100998 case BFD_POLL_NEEDED:
Klement Sekeraa3167442020-02-10 11:49:52 +0000999 if (now < bs->poll_state_start_or_timeout_nsec)
Klement Sekera239790f2017-02-16 10:53:53 +01001000 {
Klement Sekeraa3167442020-02-10 11:49:52 +00001001 BFD_DBG ("Cannot start a poll sequence yet, need to wait for "
1002 BFD_CLK_FMT,
1003 BFD_CLK_PRN (bs->poll_state_start_or_timeout_nsec -
Klement Sekera239790f2017-02-16 10:53:53 +01001004 now));
1005 break;
1006 }
Klement Sekeraa3167442020-02-10 11:49:52 +00001007 bs->poll_state_start_or_timeout_nsec = now;
Klement Sekera239790f2017-02-16 10:53:53 +01001008 bfd_set_poll_state (bs, BFD_POLL_IN_PROGRESS);
1009 /* fallthrough */
1010 case BFD_POLL_IN_PROGRESS:
1011 case BFD_POLL_IN_PROGRESS_AND_QUEUED:
Klement Sekeraa57a9702017-02-02 06:58:07 +01001012 bfd_pkt_set_poll (vlib_buffer_get_current (b));
Klement Sekeraa57a9702017-02-02 06:58:07 +01001013 BFD_DBG ("Setting poll bit in packet, bs_idx=%u", bs->bs_idx);
Klement Sekera239790f2017-02-16 10:53:53 +01001014 break;
1015 case BFD_POLL_NOT_NEEDED:
1016 /* fallthrough */
1017 break;
Klement Sekeraa57a9702017-02-02 06:58:07 +01001018 }
1019 bfd_add_auth_section (b, bs);
Klement Sekerae50e8562017-04-04 16:19:48 +02001020 bfd_add_transport_layer (vm, bi, bs);
1021 if (!bfd_transport_control_frame (vm, bi, bs))
1022 {
1023 vlib_buffer_free_one (vm, bi);
1024 }
Klement Sekeraa3167442020-02-10 11:49:52 +00001025 bs->last_tx_nsec = now;
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001026 bfd_calc_next_tx (bm, bs, now);
1027 }
1028 else
1029 {
Klement Sekera637b9c42016-12-08 05:19:14 +01001030 BFD_DBG
1031 ("No need to send control frame now, now is %lu, tx_timeout is %lu",
Klement Sekeraa3167442020-02-10 11:49:52 +00001032 now, bs->tx_timeout_nsec);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001033 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001034}
1035
1036void
Klement Sekerab17dd962017-01-09 07:43:48 +01001037bfd_init_final_control_frame (vlib_main_t * vm, vlib_buffer_t * b,
Klement Sekerae50e8562017-04-04 16:19:48 +02001038 bfd_main_t * bm, bfd_session_t * bs,
1039 int is_local)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001040{
1041 BFD_DBG ("Send final control frame for bs_idx=%lu", bs->bs_idx);
Klement Sekera239790f2017-02-16 10:53:53 +01001042 bfd_init_control_frame (bm, bs, b);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001043 bfd_pkt_set_final (vlib_buffer_get_current (b));
Klement Sekeraa57a9702017-02-02 06:58:07 +01001044 bfd_add_auth_section (b, bs);
Klement Sekerae50e8562017-04-04 16:19:48 +02001045 u32 bi = vlib_get_buffer_index (vm, b);
1046 bfd_add_transport_layer (vm, bi, bs);
Klement Sekeraa3167442020-02-10 11:49:52 +00001047 bs->last_tx_nsec = bfd_time_now_nsec (vm, NULL);
Klement Sekeraa57a9702017-02-02 06:58:07 +01001048 /*
1049 * RFC allows to include changes in final frame, so if there were any
1050 * pending, we already did that, thus we can clear any pending poll needs
1051 */
Klement Sekera239790f2017-02-16 10:53:53 +01001052 bfd_set_poll_state (bs, BFD_POLL_NOT_NEEDED);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001053}
1054
1055static void
Klement Sekeraa3167442020-02-10 11:49:52 +00001056bfd_check_rx_timeout (vlib_main_t * vm, bfd_main_t * bm, bfd_session_t * bs,
1057 u64 now, int handling_wakeup)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001058{
Klement Sekeraa3167442020-02-10 11:49:52 +00001059 if (bs->last_rx_nsec + bs->detection_time_nsec <= now)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001060 {
1061 BFD_DBG ("Rx timeout, session goes down");
Klement Sekera4d39f9c2020-01-17 10:01:52 +00001062 /*
1063 * RFC 5880 6.8.1. State Variables
1064
1065 * bfd.RemoteDiscr
1066
1067 * The remote discriminator for this BFD session. This is the
1068 * discriminator chosen by the remote system, and is totally opaque
1069 * to the local system. This MUST be initialized to zero. If a
1070 * period of a Detection Time passes without the receipt of a valid,
1071 * authenticated BFD packet from the remote system, this variable
1072 * MUST be set to zero.
1073 */
1074 bs->remote_discr = 0;
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001075 bfd_set_diag (bs, BFD_DIAG_CODE_det_time_exp);
Klement Sekeraa3167442020-02-10 11:49:52 +00001076 bfd_set_state (vm, bm, bs, BFD_STATE_down, handling_wakeup);
Klement Sekeraa57a9702017-02-02 06:58:07 +01001077 /*
1078 * If the remote system does not receive any
1079 * BFD Control packets for a Detection Time, it SHOULD reset
1080 * bfd.RemoteMinRxInterval to its initial value of 1 (per section 6.8.1,
1081 * since it is no longer required to maintain previous session state)
1082 * and then can transmit at its own rate.
1083 */
Klement Sekera239790f2017-02-16 10:53:53 +01001084 bfd_set_remote_required_min_rx (bm, bs, now, 1);
1085 }
Klement Sekeraa3167442020-02-10 11:49:52 +00001086 else if (bs->echo
1087 && bs->echo_last_rx_nsec +
1088 bs->echo_transmit_interval_nsec * bs->local_detect_mult <= now)
Klement Sekera239790f2017-02-16 10:53:53 +01001089 {
1090 BFD_DBG ("Echo rx timeout, session goes down");
1091 bfd_set_diag (bs, BFD_DIAG_CODE_echo_failed);
Klement Sekeraa3167442020-02-10 11:49:52 +00001092 bfd_set_state (vm, bm, bs, BFD_STATE_down, handling_wakeup);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001093 }
1094}
1095
1096void
1097bfd_on_timeout (vlib_main_t * vm, vlib_node_runtime_t * rt, bfd_main_t * bm,
1098 bfd_session_t * bs, u64 now)
1099{
1100 BFD_DBG ("Timeout for bs_idx=%lu", bs->bs_idx);
1101 switch (bs->local_state)
1102 {
1103 case BFD_STATE_admin_down:
Klement Sekerae50e8562017-04-04 16:19:48 +02001104 bfd_send_periodic (vm, rt, bm, bs, now);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001105 break;
1106 case BFD_STATE_down:
Klement Sekerae50e8562017-04-04 16:19:48 +02001107 bfd_send_periodic (vm, rt, bm, bs, now);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001108 break;
1109 case BFD_STATE_init:
Klement Sekeraa3167442020-02-10 11:49:52 +00001110 bfd_check_rx_timeout (vm, bm, bs, now, 1);
Klement Sekerae50e8562017-04-04 16:19:48 +02001111 bfd_send_periodic (vm, rt, bm, bs, now);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001112 break;
Klement Sekera239790f2017-02-16 10:53:53 +01001113 case BFD_STATE_up:
Klement Sekeraa3167442020-02-10 11:49:52 +00001114 bfd_check_rx_timeout (vm, bm, bs, now, 1);
Klement Sekera239790f2017-02-16 10:53:53 +01001115 if (BFD_POLL_NOT_NEEDED == bs->poll_state && !bs->echo &&
1116 bfd_is_echo_possible (bs))
1117 {
1118 /* switch on echo function as main detection method now */
1119 BFD_DBG ("Switching on echo function, bs_idx=%u", bs->bs_idx);
1120 bs->echo = 1;
Klement Sekeraa3167442020-02-10 11:49:52 +00001121 bs->echo_last_rx_nsec = now;
1122 bs->echo_tx_timeout_nsec = now;
Klement Sekera73884482017-02-23 09:26:30 +01001123 bfd_set_effective_required_min_rx (bm, bs,
Klement Sekera239790f2017-02-16 10:53:53 +01001124 clib_max
Klement Sekeraa3167442020-02-10 11:49:52 +00001125 (bm->min_required_min_rx_while_echo_nsec,
1126 bs->config_required_min_rx_nsec));
Klement Sekera239790f2017-02-16 10:53:53 +01001127 bfd_set_poll_state (bs, BFD_POLL_NEEDED);
1128 }
Klement Sekerae50e8562017-04-04 16:19:48 +02001129 bfd_send_periodic (vm, rt, bm, bs, now);
Klement Sekera239790f2017-02-16 10:53:53 +01001130 if (bs->echo)
1131 {
Klement Sekerae50e8562017-04-04 16:19:48 +02001132 bfd_send_echo (vm, rt, bm, bs, now);
Klement Sekera239790f2017-02-16 10:53:53 +01001133 }
1134 break;
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001135 }
1136}
1137
1138/*
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001139 * bfd process node function
1140 */
1141static uword
1142bfd_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
1143{
1144 bfd_main_t *bm = &bfd_main;
1145 u32 *expired = 0;
1146 uword event_type, *event_data = 0;
1147
1148 /* So we can send events to the bfd process */
1149 bm->bfd_process_node_index = bfd_process_node.index;
1150
1151 while (1)
1152 {
Klement Sekeraa3167442020-02-10 11:49:52 +00001153 f64 vm_time;
1154 u64 now = bfd_time_now_nsec (vm, &vm_time);
1155 BFD_DBG ("wakeup, now is %llunsec, vlib_time_now() is %.9f", now,
1156 vm_time);
Dave Barach1e3417f2018-07-25 08:30:27 -04001157 bfd_lock (bm);
Klement Sekeraa3167442020-02-10 11:49:52 +00001158 f64 timeout;
1159 if (pool_elts (bm->sessions))
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001160 {
Klement Sekeraa3167442020-02-10 11:49:52 +00001161 u32 first_expires_in_ticks =
1162 TW (tw_timer_first_expires_in_ticks) (&bm->wheel);
1163 if (!first_expires_in_ticks)
Klement Sekera0c1519b2016-12-08 05:03:32 +01001164 {
Klement Sekeraa3167442020-02-10 11:49:52 +00001165 BFD_DBG
1166 ("tw_timer_first_expires_in_ticks(%p) returns 0ticks",
1167 &bm->wheel);
1168 timeout = bm->wheel.next_run_time - vm_time;
1169 BFD_DBG ("wheel.next_run_time is %.9f",
1170 bm->wheel.next_run_time);
1171 u64 next_expire_nsec = now + timeout * SEC_PER_NSEC;
1172 bm->bfd_process_next_wakeup_nsec = next_expire_nsec;
1173 bfd_unlock (bm);
Klement Sekera0c1519b2016-12-08 05:03:32 +01001174 }
1175 else
1176 {
Klement Sekeraa3167442020-02-10 11:49:52 +00001177 BFD_DBG ("tw_timer_first_expires_in_ticks(%p) returns %luticks",
1178 &bm->wheel, first_expires_in_ticks);
1179 u64 next_expire_nsec =
1180 now + first_expires_in_ticks * bm->nsec_per_tw_tick;
1181 bm->bfd_process_next_wakeup_nsec = next_expire_nsec;
1182 bfd_unlock (bm);
1183 timeout = (next_expire_nsec - now) * SEC_PER_NSEC;
Klement Sekera0c1519b2016-12-08 05:03:32 +01001184 }
Klement Sekeraa3167442020-02-10 11:49:52 +00001185 BFD_DBG ("vlib_process_wait_for_event_or_clock(vm, %.09f)",
1186 timeout);
1187 (void) vlib_process_wait_for_event_or_clock (vm, timeout);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001188 }
Klement Sekeraa3167442020-02-10 11:49:52 +00001189 else
1190 {
Klement Sekera9d211502020-09-25 09:43:45 +00001191 bfd_unlock (bm);
Klement Sekeraa3167442020-02-10 11:49:52 +00001192 (void) vlib_process_wait_for_event (vm);
1193 }
1194 event_type = vlib_process_get_events (vm, &event_data);
1195 now = bfd_time_now_nsec (vm, &vm_time);
Eyal Bari0db9b042018-10-11 14:09:58 +03001196 uword *session_index;
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001197 switch (event_type)
1198 {
1199 case ~0: /* no events => timeout */
1200 /* nothing to do here */
1201 break;
1202 case BFD_EVENT_RESCHEDULE:
Klement Sekeraa3167442020-02-10 11:49:52 +00001203 BFD_DBG ("reschedule event");
Dave Barach1e3417f2018-07-25 08:30:27 -04001204 bfd_lock (bm);
Klement Sekeraa3167442020-02-10 11:49:52 +00001205 bm->bfd_process_wakeup_event_delay_nsec =
1206 now - bm->bfd_process_wakeup_event_start_nsec;
Dave Barach1e3417f2018-07-25 08:30:27 -04001207 bm->bfd_process_wakeup_events_in_flight--;
1208 bfd_unlock (bm);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001209 /* nothing to do here - reschedule is done automatically after
1210 * each event or timeout */
1211 break;
1212 case BFD_EVENT_NEW_SESSION:
Eyal Bari0db9b042018-10-11 14:09:58 +03001213 vec_foreach (session_index, event_data)
1214 {
1215 bfd_lock (bm);
1216 if (!pool_is_free_index (bm->sessions, *session_index))
1217 {
1218 bfd_session_t *bs =
1219 pool_elt_at_index (bm->sessions, *session_index);
1220 bfd_send_periodic (vm, rt, bm, bs, now);
1221 bfd_set_timer (bm, bs, now, 1);
1222 }
1223 else
1224 {
1225 BFD_DBG ("Ignoring event for non-existent session index %u",
1226 (u32) * session_index);
1227 }
1228 bfd_unlock (bm);
1229 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001230 break;
Klement Sekeraa57a9702017-02-02 06:58:07 +01001231 case BFD_EVENT_CONFIG_CHANGED:
Eyal Bari0db9b042018-10-11 14:09:58 +03001232 vec_foreach (session_index, event_data)
1233 {
1234 bfd_lock (bm);
1235 if (!pool_is_free_index (bm->sessions, *session_index))
1236 {
1237 bfd_session_t *bs =
1238 pool_elt_at_index (bm->sessions, *session_index);
1239 bfd_on_config_change (vm, rt, bm, bs, now);
1240 }
1241 else
1242 {
1243 BFD_DBG ("Ignoring event for non-existent session index %u",
1244 (u32) * session_index);
1245 }
1246 bfd_unlock (bm);
1247 }
Klement Sekeraa57a9702017-02-02 06:58:07 +01001248 break;
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001249 default:
Damjan Marion07a38572018-01-21 06:44:18 -08001250 vlib_log_err (bm->log_class, "BUG: event type 0x%wx", event_type);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001251 break;
1252 }
Klement Sekeraa3167442020-02-10 11:49:52 +00001253 BFD_DBG ("tw_timer_expire_timers_vec(%p, %.04f);", &bm->wheel, vm_time);
Dave Barach1e3417f2018-07-25 08:30:27 -04001254 bfd_lock (bm);
Klement Sekeraa3167442020-02-10 11:49:52 +00001255 expired =
1256 TW (tw_timer_expire_timers_vec) (&bm->wheel, vm_time, expired);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001257 BFD_DBG ("Expired %d elements", vec_len (expired));
1258 u32 *p = NULL;
1259 vec_foreach (p, expired)
1260 {
1261 const u32 bs_idx = *p;
1262 if (!pool_is_free_index (bm->sessions, bs_idx))
1263 {
1264 bfd_session_t *bs = pool_elt_at_index (bm->sessions, bs_idx);
Klement Sekeraa3167442020-02-10 11:49:52 +00001265 bs->tw_id = 0; /* timer is gone because it expired */
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001266 bfd_on_timeout (vm, rt, bm, bs, now);
Klement Sekera239790f2017-02-16 10:53:53 +01001267 bfd_set_timer (bm, bs, now, 1);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001268 }
1269 }
Dave Barach1e3417f2018-07-25 08:30:27 -04001270 bfd_unlock (bm);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001271 if (expired)
1272 {
1273 _vec_len (expired) = 0;
1274 }
1275 if (event_data)
1276 {
1277 _vec_len (event_data) = 0;
1278 }
1279 }
1280
1281 return 0;
1282}
1283
1284/*
1285 * bfd process node declaration
1286 */
1287/* *INDENT-OFF* */
1288VLIB_REGISTER_NODE (bfd_process_node, static) = {
1289 .function = bfd_process,
1290 .type = VLIB_NODE_TYPE_PROCESS,
1291 .name = "bfd-process",
Klement Sekera46a87ad2017-01-02 08:22:23 +01001292 .n_next_nodes = 0,
1293 .next_nodes = {},
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001294};
1295/* *INDENT-ON* */
1296
1297static clib_error_t *
1298bfd_sw_interface_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
1299{
1300 // bfd_main_t *bm = &bfd_main;
1301 // vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
1302 if (!(flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
1303 {
1304 /* TODO */
1305 }
1306 return 0;
1307}
1308
1309VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (bfd_sw_interface_up_down);
1310
1311static clib_error_t *
1312bfd_hw_interface_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
1313{
1314 // bfd_main_t *bm = &bfd_main;
1315 if (flags & VNET_HW_INTERFACE_FLAG_LINK_UP)
1316 {
1317 /* TODO */
1318 }
1319 return 0;
1320}
1321
1322VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION (bfd_hw_interface_up_down);
1323
Neale Ranns88fc83e2017-04-05 08:11:14 -07001324void
1325bfd_register_listener (bfd_notify_fn_t fn)
1326{
1327 bfd_main_t *bm = &bfd_main;
1328
1329 vec_add1 (bm->listeners, fn);
1330}
1331
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001332/*
1333 * setup function
1334 */
1335static clib_error_t *
1336bfd_main_init (vlib_main_t * vm)
1337{
Dave Barach1e3417f2018-07-25 08:30:27 -04001338 vlib_thread_main_t *tm = &vlib_thread_main;
1339 u32 n_vlib_mains = tm->n_vlib_mains;
Klement Sekerab17dd962017-01-09 07:43:48 +01001340#if BFD_DEBUG
1341 setbuf (stdout, NULL);
1342#endif
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001343 bfd_main_t *bm = &bfd_main;
1344 bm->random_seed = random_default_seed ();
1345 bm->vlib_main = vm;
1346 bm->vnet_main = vnet_get_main ();
Dave Barachb7b92992018-10-17 10:38:51 -04001347 clib_memset (&bm->wheel, 0, sizeof (bm->wheel));
Klement Sekeraa3167442020-02-10 11:49:52 +00001348 bm->nsec_per_tw_tick = (f64) NSEC_PER_SEC / BFD_TW_TPS;
1349 bm->default_desired_min_tx_nsec =
1350 bfd_usec_to_nsec (BFD_DEFAULT_DESIRED_MIN_TX_USEC);
1351 bm->min_required_min_rx_while_echo_nsec =
1352 bfd_usec_to_nsec (BFD_REQUIRED_MIN_RX_USEC_WHILE_ECHO);
1353 BFD_DBG ("tw_timer_wheel_init(%p, %p, %.04f, %u)", &bm->wheel, NULL,
1354 1.00 / BFD_TW_TPS, ~0);
1355 TW (tw_timer_wheel_init) (&bm->wheel, NULL, 1.00 / BFD_TW_TPS, ~0);
Damjan Marion07a38572018-01-21 06:44:18 -08001356 bm->log_class = vlib_log_register_class ("bfd", 0);
1357 vlib_log_debug (bm->log_class, "initialized");
Dave Barach1e3417f2018-07-25 08:30:27 -04001358 bm->owner_thread_index = ~0;
1359 if (n_vlib_mains > 1)
1360 clib_spinlock_init (&bm->lock);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001361 return 0;
1362}
1363
1364VLIB_INIT_FUNCTION (bfd_main_init);
1365
1366bfd_session_t *
Klement Sekera239790f2017-02-16 10:53:53 +01001367bfd_get_session (bfd_main_t * bm, bfd_transport_e t)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001368{
1369 bfd_session_t *result;
Dave Barach1e3417f2018-07-25 08:30:27 -04001370
1371 bfd_lock (bm);
1372
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001373 pool_get (bm->sessions, result);
Dave Barachb7b92992018-10-17 10:38:51 -04001374 clib_memset (result, 0, sizeof (*result));
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001375 result->bs_idx = result - bm->sessions;
1376 result->transport = t;
Klement Sekera239790f2017-02-16 10:53:53 +01001377 const unsigned limit = 1000;
1378 unsigned counter = 0;
1379 do
1380 {
1381 result->local_discr = random_u32 (&bm->random_seed);
1382 if (counter > limit)
1383 {
Damjan Marion07a38572018-01-21 06:44:18 -08001384 vlib_log_crit (bm->log_class,
1385 "couldn't allocate unused session discriminator even "
1386 "after %u tries!", limit);
Klement Sekera239790f2017-02-16 10:53:53 +01001387 pool_put (bm->sessions, result);
Dave Barach1e3417f2018-07-25 08:30:27 -04001388 bfd_unlock (bm);
Klement Sekera239790f2017-02-16 10:53:53 +01001389 return NULL;
1390 }
1391 ++counter;
1392 }
1393 while (hash_get (bm->session_by_disc, result->local_discr));
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001394 bfd_set_defaults (bm, result);
1395 hash_set (bm->session_by_disc, result->local_discr, result->bs_idx);
Dave Barach1e3417f2018-07-25 08:30:27 -04001396 bfd_unlock (bm);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001397 return result;
1398}
1399
1400void
1401bfd_put_session (bfd_main_t * bm, bfd_session_t * bs)
1402{
Dave Barach1e3417f2018-07-25 08:30:27 -04001403 bfd_lock (bm);
1404
Damjan Marion07a38572018-01-21 06:44:18 -08001405 vlib_log_info (bm->log_class, "delete session: %U",
1406 format_bfd_session_brief, bs);
Neale Ranns88fc83e2017-04-05 08:11:14 -07001407 bfd_notify_listeners (bm, BFD_LISTEN_EVENT_DELETE, bs);
Klement Sekerab17dd962017-01-09 07:43:48 +01001408 if (bs->auth.curr_key)
1409 {
1410 --bs->auth.curr_key->use_count;
1411 }
1412 if (bs->auth.next_key)
1413 {
1414 --bs->auth.next_key->use_count;
1415 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001416 hash_unset (bm->session_by_disc, bs->local_discr);
1417 pool_put (bm->sessions, bs);
Dave Barach1e3417f2018-07-25 08:30:27 -04001418 bfd_unlock (bm);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001419}
1420
1421bfd_session_t *
1422bfd_find_session_by_idx (bfd_main_t * bm, uword bs_idx)
1423{
Dave Barach1e3417f2018-07-25 08:30:27 -04001424 bfd_lock_check (bm);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001425 if (!pool_is_free_index (bm->sessions, bs_idx))
1426 {
1427 return pool_elt_at_index (bm->sessions, bs_idx);
1428 }
1429 return NULL;
1430}
1431
1432bfd_session_t *
1433bfd_find_session_by_disc (bfd_main_t * bm, u32 disc)
1434{
Dave Barach1e3417f2018-07-25 08:30:27 -04001435 bfd_lock_check (bm);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001436 uword *p = hash_get (bfd_main.session_by_disc, disc);
1437 if (p)
1438 {
1439 return pool_elt_at_index (bfd_main.sessions, *p);
1440 }
1441 return NULL;
1442}
1443
1444/**
1445 * @brief verify bfd packet - common checks
1446 *
1447 * @param pkt
1448 *
1449 * @return 1 if bfd packet is valid
1450 */
1451int
1452bfd_verify_pkt_common (const bfd_pkt_t * pkt)
1453{
1454 if (1 != bfd_pkt_get_version (pkt))
1455 {
1456 BFD_ERR ("BFD verification failed - unexpected version: '%d'",
1457 bfd_pkt_get_version (pkt));
1458 return 0;
1459 }
1460 if (pkt->head.length < sizeof (bfd_pkt_t) ||
1461 (bfd_pkt_get_auth_present (pkt) &&
Klement Sekerab17dd962017-01-09 07:43:48 +01001462 pkt->head.length < sizeof (bfd_pkt_with_common_auth_t)))
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001463 {
1464 BFD_ERR ("BFD verification failed - unexpected length: '%d' (auth "
1465 "present: %d)",
1466 pkt->head.length, bfd_pkt_get_auth_present (pkt));
1467 return 0;
1468 }
1469 if (!pkt->head.detect_mult)
1470 {
1471 BFD_ERR ("BFD verification failed - unexpected detect-mult: '%d'",
1472 pkt->head.detect_mult);
1473 return 0;
1474 }
1475 if (bfd_pkt_get_multipoint (pkt))
1476 {
1477 BFD_ERR ("BFD verification failed - unexpected multipoint: '%d'",
1478 bfd_pkt_get_multipoint (pkt));
1479 return 0;
1480 }
1481 if (!pkt->my_disc)
1482 {
1483 BFD_ERR ("BFD verification failed - unexpected my-disc: '%d'",
1484 pkt->my_disc);
1485 return 0;
1486 }
1487 if (!pkt->your_disc)
1488 {
1489 const u8 pkt_state = bfd_pkt_get_state (pkt);
1490 if (pkt_state != BFD_STATE_down && pkt_state != BFD_STATE_admin_down)
1491 {
1492 BFD_ERR ("BFD verification failed - unexpected state: '%s' "
1493 "(your-disc is zero)", bfd_state_string (pkt_state));
1494 return 0;
1495 }
1496 }
1497 return 1;
1498}
1499
Klement Sekerab17dd962017-01-09 07:43:48 +01001500static void
1501bfd_session_switch_auth_to_next (bfd_session_t * bs)
1502{
1503 BFD_DBG ("Switching authentication key from %U to %U for bs_idx=%u",
1504 format_bfd_auth_key, bs->auth.curr_key, format_bfd_auth_key,
1505 bs->auth.next_key, bs->bs_idx);
1506 bs->auth.is_delayed = 0;
1507 if (bs->auth.curr_key)
1508 {
1509 --bs->auth.curr_key->use_count;
1510 }
1511 bs->auth.curr_key = bs->auth.next_key;
1512 bs->auth.next_key = NULL;
1513 bs->auth.curr_bfd_key_id = bs->auth.next_bfd_key_id;
1514}
1515
1516static int
1517bfd_auth_type_is_meticulous (bfd_auth_type_e auth_type)
1518{
1519 if (BFD_AUTH_TYPE_meticulous_keyed_md5 == auth_type ||
1520 BFD_AUTH_TYPE_meticulous_keyed_sha1 == auth_type)
1521 {
1522 return 1;
1523 }
1524 return 0;
1525}
1526
1527static int
Klement Sekeraa3167442020-02-10 11:49:52 +00001528bfd_verify_pkt_auth_seq_num (vlib_main_t * vm, bfd_session_t * bs,
Klement Sekerab17dd962017-01-09 07:43:48 +01001529 u32 received_seq_num, int is_meticulous)
1530{
1531 /*
1532 * RFC 5880 6.8.1:
1533 *
1534 * This variable MUST be set to zero after no packets have been
1535 * received on this session for at least twice the Detection Time.
1536 */
Klement Sekeraa3167442020-02-10 11:49:52 +00001537 u64 now = bfd_time_now_nsec (vm, NULL);
1538 if (now - bs->last_rx_nsec > bs->detection_time_nsec * 2)
Klement Sekerab17dd962017-01-09 07:43:48 +01001539 {
Klement Sekeraa3167442020-02-10 11:49:52 +00001540 BFD_DBG ("BFD peer unresponsive for %lu nsec, which is > 2 * "
1541 "detection_time=%u nsec, resetting remote_seq_number_known "
1542 "flag", now - bs->last_rx_nsec, bs->detection_time_nsec * 2);
Klement Sekerab17dd962017-01-09 07:43:48 +01001543 bs->auth.remote_seq_number_known = 0;
1544 }
1545 if (bs->auth.remote_seq_number_known)
1546 {
1547 /* remote sequence number is known, verify its validity */
1548 const u32 max_u32 = 0xffffffff;
1549 /* the calculation might wrap, account for the special case... */
1550 if (bs->auth.remote_seq_number > max_u32 - 3 * bs->local_detect_mult)
1551 {
1552 /*
1553 * special case
1554 *
1555 * x y z
1556 * |----------+----------------------------+-----------|
1557 * 0 ^ ^ 0xffffffff
1558 * | remote_seq_num------+
1559 * |
1560 * +-----(remote_seq_num + 3*detect_mult) % * 0xffffffff
1561 *
1562 * x + y + z = 0xffffffff
1563 * x + z = 3 * detect_mult
1564 */
1565 const u32 z = max_u32 - bs->auth.remote_seq_number;
1566 const u32 x = 3 * bs->local_detect_mult - z;
1567 if (received_seq_num > x &&
1568 received_seq_num < bs->auth.remote_seq_number + is_meticulous)
1569 {
1570 BFD_ERR
1571 ("Recvd sequence number=%u out of ranges <0, %u>, <%u, %u>",
1572 received_seq_num, x,
1573 bs->auth.remote_seq_number + is_meticulous, max_u32);
1574 return 0;
1575 }
1576 }
1577 else
1578 {
1579 /* regular case */
1580 const u32 min = bs->auth.remote_seq_number + is_meticulous;
1581 const u32 max =
1582 bs->auth.remote_seq_number + 3 * bs->local_detect_mult;
1583 if (received_seq_num < min || received_seq_num > max)
1584 {
1585 BFD_ERR ("Recvd sequence number=%u out of range <%u, %u>",
1586 received_seq_num, min, max);
1587 return 0;
1588 }
1589 }
1590 }
1591 return 1;
1592}
1593
1594static int
1595bfd_verify_pkt_auth_key_sha1 (const bfd_pkt_t * pkt, u32 pkt_size,
1596 bfd_session_t * bs, u8 bfd_key_id,
1597 bfd_auth_key_t * auth_key)
1598{
1599 ASSERT (auth_key->auth_type == BFD_AUTH_TYPE_keyed_sha1 ||
1600 auth_key->auth_type == BFD_AUTH_TYPE_meticulous_keyed_sha1);
1601
1602 u8 result[SHA_DIGEST_LENGTH];
1603 bfd_pkt_with_common_auth_t *with_common = (void *) pkt;
1604 if (pkt_size < sizeof (*with_common))
1605 {
1606 BFD_ERR ("Packet size too small to hold authentication common header");
1607 return 0;
1608 }
1609 if (with_common->common_auth.type != auth_key->auth_type)
1610 {
1611 BFD_ERR ("BFD auth type mismatch, packet auth=%d:%s doesn't match "
1612 "in-use auth=%d:%s",
1613 with_common->common_auth.type,
1614 bfd_auth_type_str (with_common->common_auth.type),
1615 auth_key->auth_type, bfd_auth_type_str (auth_key->auth_type));
1616 return 0;
1617 }
1618 bfd_pkt_with_sha1_auth_t *with_sha1 = (void *) pkt;
1619 if (pkt_size < sizeof (*with_sha1) ||
1620 with_sha1->sha1_auth.type_len.len < sizeof (with_sha1->sha1_auth))
1621 {
1622 BFD_ERR
1623 ("BFD size mismatch, payload size=%u, expected=%u, auth_len=%u, "
1624 "expected=%u", pkt_size, sizeof (*with_sha1),
1625 with_sha1->sha1_auth.type_len.len, sizeof (with_sha1->sha1_auth));
1626 return 0;
1627 }
1628 if (with_sha1->sha1_auth.key_id != bfd_key_id)
1629 {
1630 BFD_ERR
1631 ("BFD key ID mismatch, packet key ID=%u doesn't match key ID=%u%s",
1632 with_sha1->sha1_auth.key_id, bfd_key_id,
1633 bs->
1634 auth.is_delayed ? " (but a delayed auth change is scheduled)" : "");
1635 return 0;
1636 }
1637 SHA_CTX ctx;
1638 if (!SHA1_Init (&ctx))
1639 {
1640 BFD_ERR ("SHA1_Init failed");
1641 return 0;
1642 }
1643 /* ignore last 20 bytes - use the actual key data instead pkt data */
1644 if (!SHA1_Update (&ctx, with_sha1,
1645 sizeof (*with_sha1) - sizeof (with_sha1->sha1_auth.hash)))
1646 {
1647 BFD_ERR ("SHA1_Update failed");
1648 return 0;
1649 }
1650 if (!SHA1_Update (&ctx, auth_key->key, sizeof (auth_key->key)))
1651 {
1652 BFD_ERR ("SHA1_Update failed");
1653 return 0;
1654 }
1655 if (!SHA1_Final (result, &ctx))
1656 {
1657 BFD_ERR ("SHA1_Final failed");
1658 return 0;
1659 }
1660 if (0 == memcmp (result, with_sha1->sha1_auth.hash, SHA_DIGEST_LENGTH))
1661 {
1662 return 1;
1663 }
1664 BFD_ERR ("SHA1 hash: %U doesn't match the expected value: %U",
1665 format_hex_bytes, with_sha1->sha1_auth.hash, SHA_DIGEST_LENGTH,
1666 format_hex_bytes, result, SHA_DIGEST_LENGTH);
1667 return 0;
1668}
1669
1670static int
Klement Sekeraa3167442020-02-10 11:49:52 +00001671bfd_verify_pkt_auth_key (vlib_main_t * vm, const bfd_pkt_t * pkt,
1672 u32 pkt_size, bfd_session_t * bs, u8 bfd_key_id,
Klement Sekerab17dd962017-01-09 07:43:48 +01001673 bfd_auth_key_t * auth_key)
1674{
Damjan Marion07a38572018-01-21 06:44:18 -08001675 bfd_main_t *bm = &bfd_main;
Klement Sekerab17dd962017-01-09 07:43:48 +01001676 switch (auth_key->auth_type)
1677 {
1678 case BFD_AUTH_TYPE_reserved:
Damjan Marion07a38572018-01-21 06:44:18 -08001679 vlib_log_err (bm->log_class,
1680 "internal error, unexpected auth_type=%d:%s",
Klement Sekerab17dd962017-01-09 07:43:48 +01001681 auth_key->auth_type,
1682 bfd_auth_type_str (auth_key->auth_type));
1683 return 0;
1684 case BFD_AUTH_TYPE_simple_password:
Damjan Marion07a38572018-01-21 06:44:18 -08001685 vlib_log_err (bm->log_class,
1686 "internal error, not implemented, unexpected auth_type=%d:%s",
1687 auth_key->auth_type,
1688 bfd_auth_type_str (auth_key->auth_type));
Klement Sekerab17dd962017-01-09 07:43:48 +01001689 return 0;
1690 case BFD_AUTH_TYPE_keyed_md5:
1691 /* fallthrough */
1692 case BFD_AUTH_TYPE_meticulous_keyed_md5:
Damjan Marion07a38572018-01-21 06:44:18 -08001693 vlib_log_err
1694 (bm->log_class,
1695 "internal error, not implemented, unexpected auth_type=%d:%s",
Klement Sekerab17dd962017-01-09 07:43:48 +01001696 auth_key->auth_type, bfd_auth_type_str (auth_key->auth_type));
1697 return 0;
1698 case BFD_AUTH_TYPE_keyed_sha1:
1699 /* fallthrough */
1700 case BFD_AUTH_TYPE_meticulous_keyed_sha1:
1701#if WITH_LIBSSL > 0
1702 do
1703 {
1704 const u32 seq_num = clib_net_to_host_u32 (((bfd_pkt_with_sha1_auth_t
1705 *) pkt)->
1706 sha1_auth.seq_num);
Klement Sekeraa3167442020-02-10 11:49:52 +00001707 return bfd_verify_pkt_auth_seq_num (vm, bs, seq_num,
Klement Sekerab17dd962017-01-09 07:43:48 +01001708 bfd_auth_type_is_meticulous
1709 (auth_key->auth_type))
1710 && bfd_verify_pkt_auth_key_sha1 (pkt, pkt_size, bs, bfd_key_id,
1711 auth_key);
1712 }
1713 while (0);
1714#else
Damjan Marion07a38572018-01-21 06:44:18 -08001715 vlib_log_err
1716 (bm->log_class,
1717 "internal error, attempt to use SHA1 without SSL support");
Klement Sekerab17dd962017-01-09 07:43:48 +01001718 return 0;
1719#endif
1720 }
1721 return 0;
1722}
1723
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001724/**
1725 * @brief verify bfd packet - authentication
1726 *
1727 * @param pkt
1728 *
1729 * @return 1 if bfd packet is valid
1730 */
1731int
Klement Sekeraa3167442020-02-10 11:49:52 +00001732bfd_verify_pkt_auth (vlib_main_t * vm, const bfd_pkt_t * pkt, u16 pkt_size,
1733 bfd_session_t * bs)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001734{
Klement Sekerab17dd962017-01-09 07:43:48 +01001735 if (bfd_pkt_get_auth_present (pkt))
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001736 {
Klement Sekerab17dd962017-01-09 07:43:48 +01001737 /* authentication present in packet */
1738 if (!bs->auth.curr_key)
1739 {
1740 /* currently not using authentication - can we turn it on? */
1741 if (bs->auth.is_delayed && bs->auth.next_key)
1742 {
1743 /* yes, switch is scheduled - make sure the auth is valid */
Klement Sekeraa3167442020-02-10 11:49:52 +00001744 if (bfd_verify_pkt_auth_key (vm, pkt, pkt_size, bs,
Klement Sekerab17dd962017-01-09 07:43:48 +01001745 bs->auth.next_bfd_key_id,
1746 bs->auth.next_key))
1747 {
1748 /* auth matches next key, do the switch, packet is valid */
1749 bfd_session_switch_auth_to_next (bs);
1750 return 1;
1751 }
1752 }
1753 }
1754 else
1755 {
1756 /* yes, using authentication, verify the key */
Klement Sekeraa3167442020-02-10 11:49:52 +00001757 if (bfd_verify_pkt_auth_key (vm, pkt, pkt_size, bs,
Klement Sekerab17dd962017-01-09 07:43:48 +01001758 bs->auth.curr_bfd_key_id,
1759 bs->auth.curr_key))
1760 {
1761 /* verification passed, packet is valid */
1762 return 1;
1763 }
1764 else
1765 {
1766 /* verification failed - but maybe we need to switch key */
1767 if (bs->auth.is_delayed && bs->auth.next_key)
1768 {
1769 /* delayed switch present, verify if that key works */
Klement Sekeraa3167442020-02-10 11:49:52 +00001770 if (bfd_verify_pkt_auth_key (vm, pkt, pkt_size, bs,
Klement Sekerab17dd962017-01-09 07:43:48 +01001771 bs->auth.next_bfd_key_id,
1772 bs->auth.next_key))
1773 {
1774 /* auth matches next key, switch key, packet is valid */
1775 bfd_session_switch_auth_to_next (bs);
1776 return 1;
1777 }
1778 }
1779 }
1780 }
1781 }
1782 else
1783 {
1784 /* authentication in packet not present */
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001785 if (pkt_size > sizeof (*pkt))
1786 {
1787 BFD_ERR ("BFD verification failed - unexpected packet size '%d' "
1788 "(auth not present)", pkt_size);
1789 return 0;
1790 }
Klement Sekerab17dd962017-01-09 07:43:48 +01001791 if (bs->auth.curr_key)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001792 {
Klement Sekerab17dd962017-01-09 07:43:48 +01001793 /* currently authenticating - could we turn it off? */
1794 if (bs->auth.is_delayed && !bs->auth.next_key)
1795 {
1796 /* yes, delayed switch to NULL key is scheduled */
1797 bfd_session_switch_auth_to_next (bs);
1798 return 1;
1799 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001800 }
Klement Sekerab17dd962017-01-09 07:43:48 +01001801 else
1802 {
1803 /* no auth in packet, no auth in use - packet is valid */
1804 return 1;
1805 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001806 }
Klement Sekerab17dd962017-01-09 07:43:48 +01001807 return 0;
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001808}
1809
1810void
Klement Sekeraa3167442020-02-10 11:49:52 +00001811bfd_consume_pkt (vlib_main_t * vm, bfd_main_t * bm, const bfd_pkt_t * pkt,
1812 u32 bs_idx)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001813{
Dave Barach1e3417f2018-07-25 08:30:27 -04001814 bfd_lock_check (bm);
1815
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001816 bfd_session_t *bs = bfd_find_session_by_idx (bm, bs_idx);
Klement Sekera0e2e0df2017-03-03 08:51:08 +01001817 if (!bs || (pkt->your_disc && pkt->your_disc != bs->local_discr))
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001818 {
1819 return;
1820 }
1821 BFD_DBG ("Scanning bfd packet, bs_idx=%d", bs->bs_idx);
1822 bs->remote_discr = pkt->my_disc;
1823 bs->remote_state = bfd_pkt_get_state (pkt);
1824 bs->remote_demand = bfd_pkt_get_demand (pkt);
Klement Sekera73884482017-02-23 09:26:30 +01001825 bs->remote_diag = bfd_pkt_get_diag_code (pkt);
Klement Sekeraa3167442020-02-10 11:49:52 +00001826 u64 now = bfd_time_now_nsec (vm, NULL);
1827 bs->last_rx_nsec = now;
Klement Sekerab17dd962017-01-09 07:43:48 +01001828 if (bfd_pkt_get_auth_present (pkt))
1829 {
1830 bfd_auth_type_e auth_type =
1831 ((bfd_pkt_with_common_auth_t *) (pkt))->common_auth.type;
1832 switch (auth_type)
1833 {
1834 case BFD_AUTH_TYPE_reserved:
1835 /* fallthrough */
1836 case BFD_AUTH_TYPE_simple_password:
1837 /* fallthrough */
1838 case BFD_AUTH_TYPE_keyed_md5:
1839 /* fallthrough */
1840 case BFD_AUTH_TYPE_meticulous_keyed_md5:
Damjan Marion07a38572018-01-21 06:44:18 -08001841 vlib_log_crit (bm->log_class,
1842 "internal error, unexpected auth_type=%d:%s",
1843 auth_type, bfd_auth_type_str (auth_type));
Klement Sekerab17dd962017-01-09 07:43:48 +01001844 break;
1845 case BFD_AUTH_TYPE_keyed_sha1:
1846 /* fallthrough */
1847 case BFD_AUTH_TYPE_meticulous_keyed_sha1:
1848 do
1849 {
1850 bfd_pkt_with_sha1_auth_t *with_sha1 =
1851 (bfd_pkt_with_sha1_auth_t *) pkt;
1852 bs->auth.remote_seq_number =
1853 clib_net_to_host_u32 (with_sha1->sha1_auth.seq_num);
1854 bs->auth.remote_seq_number_known = 1;
1855 BFD_DBG ("Received sequence number %u",
1856 bs->auth.remote_seq_number);
1857 }
1858 while (0);
1859 }
1860 }
Klement Sekeraa3167442020-02-10 11:49:52 +00001861 bs->remote_desired_min_tx_nsec =
1862 bfd_usec_to_nsec (clib_net_to_host_u32 (pkt->des_min_tx));
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001863 bs->remote_detect_mult = pkt->head.detect_mult;
Klement Sekera637b9c42016-12-08 05:19:14 +01001864 bfd_set_remote_required_min_rx (bm, bs, now,
Klement Sekera239790f2017-02-16 10:53:53 +01001865 clib_net_to_host_u32 (pkt->req_min_rx));
1866 bfd_set_remote_required_min_echo_rx (bm, bs, now,
1867 clib_net_to_host_u32
1868 (pkt->req_min_echo_rx));
Klement Sekera239790f2017-02-16 10:53:53 +01001869 if (bfd_pkt_get_final (pkt))
Klement Sekeraa57a9702017-02-02 06:58:07 +01001870 {
Klement Sekera239790f2017-02-16 10:53:53 +01001871 if (BFD_POLL_IN_PROGRESS == bs->poll_state)
Klement Sekeraa57a9702017-02-02 06:58:07 +01001872 {
Klement Sekera239790f2017-02-16 10:53:53 +01001873 BFD_DBG ("Poll sequence terminated, bs_idx=%u", bs->bs_idx);
1874 bfd_set_poll_state (bs, BFD_POLL_NOT_NEEDED);
1875 if (BFD_STATE_up == bs->local_state)
1876 {
Klement Sekera73884482017-02-23 09:26:30 +01001877 bfd_set_effective_required_min_rx (bm, bs,
Klement Sekera239790f2017-02-16 10:53:53 +01001878 clib_max (bs->echo *
Klement Sekeraa3167442020-02-10 11:49:52 +00001879 bm->min_required_min_rx_while_echo_nsec,
1880 bs->config_required_min_rx_nsec));
Klement Sekera239790f2017-02-16 10:53:53 +01001881 }
1882 }
1883 else if (BFD_POLL_IN_PROGRESS_AND_QUEUED == bs->poll_state)
1884 {
1885 /*
1886 * next poll sequence must be delayed by at least the round trip
1887 * time, so calculate that here
1888 */
1889 BFD_DBG ("Next poll sequence can commence in " BFD_CLK_FMT,
Klement Sekeraa3167442020-02-10 11:49:52 +00001890 BFD_CLK_PRN (now - bs->poll_state_start_or_timeout_nsec));
1891 bs->poll_state_start_or_timeout_nsec =
1892 now + (now - bs->poll_state_start_or_timeout_nsec);
Klement Sekera239790f2017-02-16 10:53:53 +01001893 BFD_DBG
1894 ("Poll sequence terminated, but another is needed, bs_idx=%u",
1895 bs->bs_idx);
1896 bfd_set_poll_state (bs, BFD_POLL_NEEDED);
Klement Sekeraa57a9702017-02-02 06:58:07 +01001897 }
1898 }
Klement Sekera239790f2017-02-16 10:53:53 +01001899 bfd_calc_next_tx (bm, bs, now);
1900 bfd_set_timer (bm, bs, now, 0);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001901 if (BFD_STATE_admin_down == bs->local_state)
Klement Sekerac48829b2017-02-14 07:55:57 +01001902 {
1903 BFD_DBG ("Session is admin-down, ignoring packet, bs_idx=%u",
1904 bs->bs_idx);
1905 return;
1906 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001907 if (BFD_STATE_admin_down == bs->remote_state)
1908 {
1909 bfd_set_diag (bs, BFD_DIAG_CODE_neighbor_sig_down);
Klement Sekeraa3167442020-02-10 11:49:52 +00001910 bfd_set_state (vm, bm, bs, BFD_STATE_down, 0);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001911 }
1912 else if (BFD_STATE_down == bs->local_state)
1913 {
1914 if (BFD_STATE_down == bs->remote_state)
1915 {
Klement Sekerae50e8562017-04-04 16:19:48 +02001916 bfd_set_diag (bs, BFD_DIAG_CODE_no_diag);
Klement Sekeraa3167442020-02-10 11:49:52 +00001917 bfd_set_state (vm, bm, bs, BFD_STATE_init, 0);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001918 }
1919 else if (BFD_STATE_init == bs->remote_state)
1920 {
Klement Sekerae50e8562017-04-04 16:19:48 +02001921 bfd_set_diag (bs, BFD_DIAG_CODE_no_diag);
Klement Sekeraa3167442020-02-10 11:49:52 +00001922 bfd_set_state (vm, bm, bs, BFD_STATE_up, 0);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001923 }
1924 }
1925 else if (BFD_STATE_init == bs->local_state)
1926 {
1927 if (BFD_STATE_up == bs->remote_state ||
1928 BFD_STATE_init == bs->remote_state)
1929 {
Klement Sekerae50e8562017-04-04 16:19:48 +02001930 bfd_set_diag (bs, BFD_DIAG_CODE_no_diag);
Klement Sekeraa3167442020-02-10 11:49:52 +00001931 bfd_set_state (vm, bm, bs, BFD_STATE_up, 0);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001932 }
1933 }
1934 else /* BFD_STATE_up == bs->local_state */
1935 {
1936 if (BFD_STATE_down == bs->remote_state)
1937 {
1938 bfd_set_diag (bs, BFD_DIAG_CODE_neighbor_sig_down);
Klement Sekeraa3167442020-02-10 11:49:52 +00001939 bfd_set_state (vm, bm, bs, BFD_STATE_down, 0);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001940 }
1941 }
1942}
1943
Klement Sekera239790f2017-02-16 10:53:53 +01001944int
Klement Sekeraa3167442020-02-10 11:49:52 +00001945bfd_consume_echo_pkt (vlib_main_t * vm, bfd_main_t * bm, vlib_buffer_t * b)
Klement Sekeraa57a9702017-02-02 06:58:07 +01001946{
Klement Sekera239790f2017-02-16 10:53:53 +01001947 bfd_echo_pkt_t *pkt = NULL;
1948 if (b->current_length != sizeof (*pkt))
Klement Sekeraa57a9702017-02-02 06:58:07 +01001949 {
Klement Sekera239790f2017-02-16 10:53:53 +01001950 return 0;
Klement Sekeraa57a9702017-02-02 06:58:07 +01001951 }
Klement Sekera239790f2017-02-16 10:53:53 +01001952 pkt = vlib_buffer_get_current (b);
1953 bfd_session_t *bs = bfd_find_session_by_disc (bm, pkt->discriminator);
1954 if (!bs)
1955 {
1956 return 0;
1957 }
1958 BFD_DBG ("Scanning bfd echo packet, bs_idx=%d", bs->bs_idx);
1959 u64 checksum =
Klement Sekeraa3167442020-02-10 11:49:52 +00001960 bfd_calc_echo_checksum (bs->local_discr, pkt->expire_time_nsec,
Klement Sekera239790f2017-02-16 10:53:53 +01001961 bs->echo_secret);
1962 if (checksum != pkt->checksum)
1963 {
1964 BFD_DBG ("Invalid echo packet, checksum mismatch");
1965 return 1;
1966 }
Klement Sekeraa3167442020-02-10 11:49:52 +00001967 u64 now = bfd_time_now_nsec (vm, NULL);
1968 if (pkt->expire_time_nsec < now)
Klement Sekera239790f2017-02-16 10:53:53 +01001969 {
1970 BFD_DBG ("Stale packet received, expire time %lu < now %lu",
Klement Sekeraa3167442020-02-10 11:49:52 +00001971 pkt->expire_time_nsec, now);
Klement Sekera239790f2017-02-16 10:53:53 +01001972 }
1973 else
1974 {
Klement Sekeraa3167442020-02-10 11:49:52 +00001975 bs->echo_last_rx_nsec = now;
Klement Sekera239790f2017-02-16 10:53:53 +01001976 }
1977 return 1;
Klement Sekeraa57a9702017-02-02 06:58:07 +01001978}
1979
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001980u8 *
1981format_bfd_session (u8 * s, va_list * args)
1982{
1983 const bfd_session_t *bs = va_arg (*args, bfd_session_t *);
Klement Sekera239790f2017-02-16 10:53:53 +01001984 s = format (s, "bs_idx=%u local-state=%s remote-state=%s\n"
Damjan Marion06d82262020-10-21 12:43:40 +02001985 "local-discriminator=%u remote-discriminator=%u\n"
1986 "local-diag=%s echo-active=%s\n"
1987 "desired-min-tx=%u required-min-rx=%u\n"
1988 "required-min-echo-rx=%u detect-mult=%u\n"
1989 "remote-min-rx=%u remote-min-echo-rx=%u\n"
1990 "remote-demand=%s poll-state=%s\n"
1991 "auth: local-seq-num=%u remote-seq-num=%u\n"
1992 " is-delayed=%s\n"
1993 " curr-key=%U\n"
1994 " next-key=%U",
Klement Sekerab17dd962017-01-09 07:43:48 +01001995 bs->bs_idx, bfd_state_string (bs->local_state),
Damjan Marion06d82262020-10-21 12:43:40 +02001996 bfd_state_string (bs->remote_state), bs->local_discr,
1997 bs->remote_discr, bfd_diag_code_string (bs->local_diag),
1998 (bs->echo ? "yes" : "no"), bs->config_desired_min_tx_usec,
1999 bs->config_required_min_rx_usec, 1, bs->local_detect_mult,
2000 bs->remote_min_rx_usec, bs->remote_min_echo_rx_usec,
Klement Sekera239790f2017-02-16 10:53:53 +01002001 (bs->remote_demand ? "yes" : "no"),
Damjan Marion06d82262020-10-21 12:43:40 +02002002 bfd_poll_state_string (bs->poll_state),
2003 bs->auth.local_seq_number, bs->auth.remote_seq_number,
2004 (bs->auth.is_delayed ? "yes" : "no"),
2005 format_bfd_auth_key, bs->auth.curr_key, format_bfd_auth_key,
Klement Sekera239790f2017-02-16 10:53:53 +01002006 bs->auth.next_key);
Klement Sekerab17dd962017-01-09 07:43:48 +01002007 return s;
2008}
2009
Damjan Marion07a38572018-01-21 06:44:18 -08002010u8 *
2011format_bfd_session_brief (u8 * s, va_list * args)
2012{
2013 const bfd_session_t *bs = va_arg (*args, bfd_session_t *);
2014 s =
2015 format (s, "bs_idx=%u local-state=%s remote-state=%s", bs->bs_idx,
2016 bfd_state_string (bs->local_state),
2017 bfd_state_string (bs->remote_state));
2018 return s;
2019}
2020
Klement Sekerab17dd962017-01-09 07:43:48 +01002021unsigned
2022bfd_auth_type_supported (bfd_auth_type_e auth_type)
2023{
2024 if (auth_type == BFD_AUTH_TYPE_keyed_sha1 ||
2025 auth_type == BFD_AUTH_TYPE_meticulous_keyed_sha1)
2026 {
2027 return 1;
2028 }
2029 return 0;
2030}
2031
2032vnet_api_error_t
2033bfd_auth_activate (bfd_session_t * bs, u32 conf_key_id,
2034 u8 bfd_key_id, u8 is_delayed)
2035{
2036 bfd_main_t *bm = &bfd_main;
2037 const uword *key_idx_p =
2038 hash_get (bm->auth_key_by_conf_key_id, conf_key_id);
2039 if (!key_idx_p)
2040 {
Damjan Marion07a38572018-01-21 06:44:18 -08002041 vlib_log_err (bm->log_class,
2042 "authentication key with config ID %u doesn't exist)",
Klement Sekerab17dd962017-01-09 07:43:48 +01002043 conf_key_id);
2044 return VNET_API_ERROR_BFD_ENOENT;
2045 }
2046 const uword key_idx = *key_idx_p;
2047 bfd_auth_key_t *key = pool_elt_at_index (bm->auth_keys, key_idx);
2048 if (is_delayed)
2049 {
2050 if (bs->auth.next_key == key)
2051 {
2052 /* already using this key, no changes required */
2053 return 0;
2054 }
2055 bs->auth.next_key = key;
2056 bs->auth.next_bfd_key_id = bfd_key_id;
2057 bs->auth.is_delayed = 1;
2058 }
2059 else
2060 {
2061 if (bs->auth.curr_key == key)
2062 {
2063 /* already using this key, no changes required */
2064 return 0;
2065 }
2066 if (bs->auth.curr_key)
2067 {
2068 --bs->auth.curr_key->use_count;
2069 }
2070 bs->auth.curr_key = key;
2071 bs->auth.curr_bfd_key_id = bfd_key_id;
2072 bs->auth.is_delayed = 0;
2073 }
2074 ++key->use_count;
Klement Sekera239790f2017-02-16 10:53:53 +01002075 BFD_DBG ("\nSession auth modified: %U", format_bfd_session, bs);
Damjan Marion07a38572018-01-21 06:44:18 -08002076 vlib_log_info (bm->log_class, "session auth modified: %U",
2077 format_bfd_session_brief, bs);
Klement Sekerab17dd962017-01-09 07:43:48 +01002078 return 0;
2079}
2080
2081vnet_api_error_t
2082bfd_auth_deactivate (bfd_session_t * bs, u8 is_delayed)
2083{
Damjan Marion07a38572018-01-21 06:44:18 -08002084 bfd_main_t *bm = &bfd_main;
Klement Sekerab17dd962017-01-09 07:43:48 +01002085#if WITH_LIBSSL > 0
2086 if (!is_delayed)
2087 {
2088 /* not delayed - deactivate the current key right now */
2089 if (bs->auth.curr_key)
2090 {
2091 --bs->auth.curr_key->use_count;
2092 bs->auth.curr_key = NULL;
2093 }
2094 bs->auth.is_delayed = 0;
2095 }
2096 else
2097 {
2098 /* delayed - mark as so */
2099 bs->auth.is_delayed = 1;
2100 }
2101 /*
2102 * clear the next key unconditionally - either the auth change is not delayed
2103 * in which case the caller expects the session to not use authentication
2104 * from this point forward, or it is delayed, in which case the next_key
2105 * needs to be set to NULL to make it so in the future
2106 */
2107 if (bs->auth.next_key)
2108 {
2109 --bs->auth.next_key->use_count;
2110 bs->auth.next_key = NULL;
2111 }
Klement Sekera239790f2017-02-16 10:53:53 +01002112 BFD_DBG ("\nSession auth modified: %U", format_bfd_session, bs);
Damjan Marion07a38572018-01-21 06:44:18 -08002113 vlib_log_info (bm->log_class, "session auth modified: %U",
2114 format_bfd_session_brief, bs);
Klement Sekerab17dd962017-01-09 07:43:48 +01002115 return 0;
2116#else
Damjan Marion07a38572018-01-21 06:44:18 -08002117 vlib_log_err (bm->log_class,
2118 "SSL missing, cannot deactivate BFD authentication");
Klement Sekerab17dd962017-01-09 07:43:48 +01002119 return VNET_API_ERROR_BFD_NOTSUPP;
2120#endif
Klement Sekera0e3c0de2016-09-29 14:43:44 +02002121}
2122
Klement Sekeraa57a9702017-02-02 06:58:07 +01002123vnet_api_error_t
2124bfd_session_set_params (bfd_main_t * bm, bfd_session_t * bs,
2125 u32 desired_min_tx_usec,
2126 u32 required_min_rx_usec, u8 detect_mult)
2127{
2128 if (bs->local_detect_mult != detect_mult ||
2129 bs->config_desired_min_tx_usec != desired_min_tx_usec ||
2130 bs->config_required_min_rx_usec != required_min_rx_usec)
2131 {
Klement Sekera239790f2017-02-16 10:53:53 +01002132 BFD_DBG ("\nChanging session params: %U", format_bfd_session, bs);
Klement Sekeraa57a9702017-02-02 06:58:07 +01002133 switch (bs->poll_state)
2134 {
Klement Sekera239790f2017-02-16 10:53:53 +01002135 case BFD_POLL_NOT_NEEDED:
Klement Sekeraa57a9702017-02-02 06:58:07 +01002136 if (BFD_STATE_up == bs->local_state ||
2137 BFD_STATE_init == bs->local_state)
2138 {
2139 /* poll sequence is not needed for detect multiplier change */
2140 if (bs->config_desired_min_tx_usec != desired_min_tx_usec ||
2141 bs->config_required_min_rx_usec != required_min_rx_usec)
2142 {
Klement Sekera239790f2017-02-16 10:53:53 +01002143 bfd_set_poll_state (bs, BFD_POLL_NEEDED);
Klement Sekeraa57a9702017-02-02 06:58:07 +01002144 }
2145 }
2146 break;
Klement Sekera239790f2017-02-16 10:53:53 +01002147 case BFD_POLL_NEEDED:
2148 case BFD_POLL_IN_PROGRESS_AND_QUEUED:
2149 /*
2150 * nothing to do - will be handled in the future poll which is
2151 * already scheduled for execution
2152 */
Klement Sekeraa57a9702017-02-02 06:58:07 +01002153 break;
Klement Sekera239790f2017-02-16 10:53:53 +01002154 case BFD_POLL_IN_PROGRESS:
2155 /* poll sequence is not needed for detect multiplier change */
2156 if (bs->config_desired_min_tx_usec != desired_min_tx_usec ||
2157 bs->config_required_min_rx_usec != required_min_rx_usec)
2158 {
2159 BFD_DBG ("Poll in progress, queueing extra poll, bs_idx=%u",
2160 bs->bs_idx);
2161 bfd_set_poll_state (bs, BFD_POLL_IN_PROGRESS_AND_QUEUED);
2162 }
Klement Sekeraa57a9702017-02-02 06:58:07 +01002163 }
2164
2165 bs->local_detect_mult = detect_mult;
2166 bs->config_desired_min_tx_usec = desired_min_tx_usec;
Klement Sekeraa3167442020-02-10 11:49:52 +00002167 bs->config_desired_min_tx_nsec = bfd_usec_to_nsec (desired_min_tx_usec);
Klement Sekeraa57a9702017-02-02 06:58:07 +01002168 bs->config_required_min_rx_usec = required_min_rx_usec;
Klement Sekeraa3167442020-02-10 11:49:52 +00002169 bs->config_required_min_rx_nsec =
2170 bfd_usec_to_nsec (required_min_rx_usec);
Klement Sekera239790f2017-02-16 10:53:53 +01002171 BFD_DBG ("\nChanged session params: %U", format_bfd_session, bs);
Klement Sekeraa57a9702017-02-02 06:58:07 +01002172
Damjan Marion07a38572018-01-21 06:44:18 -08002173 vlib_log_info (bm->log_class, "changed session params: %U",
2174 format_bfd_session_brief, bs);
Klement Sekeraa57a9702017-02-02 06:58:07 +01002175 vlib_process_signal_event (bm->vlib_main, bm->bfd_process_node_index,
2176 BFD_EVENT_CONFIG_CHANGED, bs->bs_idx);
2177 }
2178 else
2179 {
2180 BFD_DBG ("Ignore parameter change - no change, bs_idx=%u", bs->bs_idx);
2181 }
2182 return 0;
2183}
2184
Klement Sekera73884482017-02-23 09:26:30 +01002185vnet_api_error_t
2186bfd_auth_set_key (u32 conf_key_id, u8 auth_type, u8 key_len,
2187 const u8 * key_data)
2188{
Damjan Marion07a38572018-01-21 06:44:18 -08002189 bfd_main_t *bm = &bfd_main;
Klement Sekera73884482017-02-23 09:26:30 +01002190#if WITH_LIBSSL > 0
2191 bfd_auth_key_t *auth_key = NULL;
Klement Sekerab16bfe32017-02-28 11:56:48 +01002192 if (!key_len || key_len > bfd_max_key_len_for_auth_type (auth_type))
Klement Sekera73884482017-02-23 09:26:30 +01002193 {
Damjan Marion07a38572018-01-21 06:44:18 -08002194 vlib_log_err (bm->log_class,
2195 "invalid authentication key length for auth_type=%d:%s "
2196 "(key_len=%u, must be non-zero, expected max=%u)",
Klement Sekera73884482017-02-23 09:26:30 +01002197 auth_type, bfd_auth_type_str (auth_type), key_len,
Klement Sekerab16bfe32017-02-28 11:56:48 +01002198 (u32) bfd_max_key_len_for_auth_type (auth_type));
Klement Sekera73884482017-02-23 09:26:30 +01002199 return VNET_API_ERROR_INVALID_VALUE;
2200 }
2201 if (!bfd_auth_type_supported (auth_type))
2202 {
Damjan Marion07a38572018-01-21 06:44:18 -08002203 vlib_log_err (bm->log_class, "unsupported auth type=%d:%s", auth_type,
Klement Sekera73884482017-02-23 09:26:30 +01002204 bfd_auth_type_str (auth_type));
2205 return VNET_API_ERROR_BFD_NOTSUPP;
2206 }
Klement Sekera73884482017-02-23 09:26:30 +01002207 uword *key_idx_p = hash_get (bm->auth_key_by_conf_key_id, conf_key_id);
2208 if (key_idx_p)
2209 {
2210 /* modifying existing key - must not be used */
2211 const uword key_idx = *key_idx_p;
2212 auth_key = pool_elt_at_index (bm->auth_keys, key_idx);
2213 if (auth_key->use_count > 0)
2214 {
Damjan Marion07a38572018-01-21 06:44:18 -08002215 vlib_log_err (bm->log_class,
2216 "authentication key with conf ID %u in use by %u BFD "
2217 "session(s) - cannot modify", conf_key_id,
2218 auth_key->use_count);
Klement Sekera73884482017-02-23 09:26:30 +01002219 return VNET_API_ERROR_BFD_EINUSE;
2220 }
2221 }
2222 else
2223 {
2224 /* adding new key */
2225 pool_get (bm->auth_keys, auth_key);
2226 auth_key->conf_key_id = conf_key_id;
2227 hash_set (bm->auth_key_by_conf_key_id, conf_key_id,
2228 auth_key - bm->auth_keys);
2229 }
2230 auth_key->auth_type = auth_type;
Dave Barachb7b92992018-10-17 10:38:51 -04002231 clib_memset (auth_key->key, 0, sizeof (auth_key->key));
Klement Sekera73884482017-02-23 09:26:30 +01002232 clib_memcpy (auth_key->key, key_data, key_len);
2233 return 0;
2234#else
Damjan Marion07a38572018-01-21 06:44:18 -08002235 vlib_log_err (bm->log_class,
2236 "SSL missing, cannot manipulate authentication keys");
Klement Sekera73884482017-02-23 09:26:30 +01002237 return VNET_API_ERROR_BFD_NOTSUPP;
2238#endif
2239}
2240
2241vnet_api_error_t
2242bfd_auth_del_key (u32 conf_key_id)
2243{
2244#if WITH_LIBSSL > 0
2245 bfd_auth_key_t *auth_key = NULL;
2246 bfd_main_t *bm = &bfd_main;
2247 uword *key_idx_p = hash_get (bm->auth_key_by_conf_key_id, conf_key_id);
2248 if (key_idx_p)
2249 {
2250 /* deleting existing key - must not be used */
2251 const uword key_idx = *key_idx_p;
2252 auth_key = pool_elt_at_index (bm->auth_keys, key_idx);
2253 if (auth_key->use_count > 0)
2254 {
Damjan Marion07a38572018-01-21 06:44:18 -08002255 vlib_log_err (bm->log_class,
2256 "authentication key with conf ID %u in use by %u BFD "
2257 "session(s) - cannot delete", conf_key_id,
2258 auth_key->use_count);
Klement Sekera73884482017-02-23 09:26:30 +01002259 return VNET_API_ERROR_BFD_EINUSE;
2260 }
2261 hash_unset (bm->auth_key_by_conf_key_id, conf_key_id);
Dave Barachb7b92992018-10-17 10:38:51 -04002262 clib_memset (auth_key, 0, sizeof (*auth_key));
Klement Sekera73884482017-02-23 09:26:30 +01002263 pool_put (bm->auth_keys, auth_key);
2264 }
2265 else
2266 {
2267 /* no such key */
Damjan Marion07a38572018-01-21 06:44:18 -08002268 vlib_log_err (bm->log_class,
2269 "authentication key with conf ID %u does not exist",
Klement Sekera73884482017-02-23 09:26:30 +01002270 conf_key_id);
2271 return VNET_API_ERROR_BFD_ENOENT;
2272 }
2273 return 0;
2274#else
Damjan Marion07a38572018-01-21 06:44:18 -08002275 vlib_log_err (bm->log_class,
2276 "SSL missing, cannot manipulate authentication keys");
Klement Sekera73884482017-02-23 09:26:30 +01002277 return VNET_API_ERROR_BFD_NOTSUPP;
2278#endif
2279}
2280
Klement Sekera0e3c0de2016-09-29 14:43:44 +02002281bfd_main_t bfd_main;
2282
2283/*
2284 * fd.io coding-style-patch-verification: ON
2285 *
2286 * Local Variables:
2287 * eval: (c-set-style "gnu")
2288 * End:
2289 */