blob: 6e6be9efb704700c2bed2bbbb042da3fb2bbae23 [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
Dave Barach1e3417f2018-07-25 08:30:27 -040040u32 oingoes;
41
42void
43oingo (void)
44{
45 oingoes++;
46}
47
Klement Sekera239790f2017-02-16 10:53:53 +010048static u64
49bfd_calc_echo_checksum (u32 discriminator, u64 expire_time, u32 secret)
50{
51 u64 checksum = 0;
Gabriel Ganne8e66b9b2017-12-14 16:20:37 +010052#if defined(clib_crc32c_uses_intrinsics) && !defined (__i386__)
53 checksum = crc32_u64 (0, discriminator);
54 checksum = crc32_u64 (checksum, expire_time);
55 checksum = crc32_u64 (checksum, secret);
Klement Sekera239790f2017-02-16 10:53:53 +010056#else
57 checksum = clib_xxhash (discriminator ^ expire_time ^ secret);
Klement Sekerab17dd962017-01-09 07:43:48 +010058#endif
Klement Sekera239790f2017-02-16 10:53:53 +010059 return checksum;
60}
Klement Sekera0e3c0de2016-09-29 14:43:44 +020061
62static u64
Klement Sekeraa57a9702017-02-02 06:58:07 +010063bfd_usec_to_clocks (const bfd_main_t * bm, u64 us)
Klement Sekera0e3c0de2016-09-29 14:43:44 +020064{
65 return bm->cpu_cps * ((f64) us / USEC_PER_SECOND);
66}
67
Klement Sekera73884482017-02-23 09:26:30 +010068u32
Klement Sekera239790f2017-02-16 10:53:53 +010069bfd_clocks_to_usec (const bfd_main_t * bm, u64 clocks)
70{
71 return (clocks / bm->cpu_cps) * USEC_PER_SECOND;
72}
73
Klement Sekera0e3c0de2016-09-29 14:43:44 +020074static vlib_node_registration_t bfd_process_node;
75
Klement Sekera73884482017-02-23 09:26:30 +010076u8 *
Klement Sekerab17dd962017-01-09 07:43:48 +010077format_bfd_auth_key (u8 * s, va_list * args)
78{
79 const bfd_auth_key_t *key = va_arg (*args, bfd_auth_key_t *);
80 if (key)
81 {
82 s = format (s, "{auth-type=%u:%s, conf-key-id=%u, use-count=%u}, ",
83 key->auth_type, bfd_auth_type_str (key->auth_type),
84 key->conf_key_id, key->use_count);
85 }
86 else
87 {
88 s = format (s, "{none}");
89 }
90 return s;
91}
92
Klement Sekera0e3c0de2016-09-29 14:43:44 +020093/*
94 * We actually send all bfd pkts to the "error" node after scanning
95 * them, so the graph node has only one next-index. The "error-drop"
96 * node automatically bumps our per-node packet counters for us.
97 */
98typedef enum
99{
100 BFD_INPUT_NEXT_NORMAL,
101 BFD_INPUT_N_NEXT,
102} bfd_input_next_t;
103
Klement Sekerae4504c62016-12-08 10:16:41 +0100104static void bfd_on_state_change (bfd_main_t * bm, bfd_session_t * bs, u64 now,
105 int handling_wakeup);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200106
107static void
108bfd_set_defaults (bfd_main_t * bm, bfd_session_t * bs)
109{
110 bs->local_state = BFD_STATE_down;
111 bs->local_diag = BFD_DIAG_CODE_no_diag;
112 bs->remote_state = BFD_STATE_down;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200113 bs->remote_discr = 0;
Neale Ranns88fc83e2017-04-05 08:11:14 -0700114 bs->hop_type = BFD_HOP_TYPE_SINGLE;
Klement Sekera239790f2017-02-16 10:53:53 +0100115 bs->config_desired_min_tx_usec = BFD_DEFAULT_DESIRED_MIN_TX_USEC;
Klement Sekeraa57a9702017-02-02 06:58:07 +0100116 bs->config_desired_min_tx_clocks = bm->default_desired_min_tx_clocks;
117 bs->effective_desired_min_tx_clocks = bm->default_desired_min_tx_clocks;
118 bs->remote_min_rx_usec = 1;
119 bs->remote_min_rx_clocks = bfd_usec_to_clocks (bm, bs->remote_min_rx_usec);
Klement Sekera239790f2017-02-16 10:53:53 +0100120 bs->remote_min_echo_rx_usec = 0;
121 bs->remote_min_echo_rx_clocks = 0;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200122 bs->remote_demand = 0;
Klement Sekerab17dd962017-01-09 07:43:48 +0100123 bs->auth.remote_seq_number = 0;
124 bs->auth.remote_seq_number_known = 0;
125 bs->auth.local_seq_number = random_u32 (&bm->random_seed);
Klement Sekera239790f2017-02-16 10:53:53 +0100126 bs->echo_secret = random_u32 (&bm->random_seed);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200127}
128
129static void
130bfd_set_diag (bfd_session_t * bs, bfd_diag_code_e code)
131{
132 if (bs->local_diag != code)
133 {
134 BFD_DBG ("set local_diag, bs_idx=%d: '%d:%s'", bs->bs_idx, code,
135 bfd_diag_code_string (code));
136 bs->local_diag = code;
137 }
138}
139
140static void
Klement Sekerae4504c62016-12-08 10:16:41 +0100141bfd_set_state (bfd_main_t * bm, bfd_session_t * bs,
142 bfd_state_e new_state, int handling_wakeup)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200143{
144 if (bs->local_state != new_state)
145 {
146 BFD_DBG ("Change state, bs_idx=%d: %s->%s", bs->bs_idx,
147 bfd_state_string (bs->local_state),
148 bfd_state_string (new_state));
149 bs->local_state = new_state;
Klement Sekerae4504c62016-12-08 10:16:41 +0100150 bfd_on_state_change (bm, bs, clib_cpu_time_now (), handling_wakeup);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200151 }
152}
153
Klement Sekera73884482017-02-23 09:26:30 +0100154const char *
Klement Sekera239790f2017-02-16 10:53:53 +0100155bfd_poll_state_string (bfd_poll_state_e state)
156{
157 switch (state)
158 {
159#define F(x) \
160 case BFD_POLL_##x: \
161 return "BFD_POLL_" #x;
162 foreach_bfd_poll_state (F)
163#undef F
164 }
165 return "UNKNOWN";
166}
167
168static void
169bfd_set_poll_state (bfd_session_t * bs, bfd_poll_state_e state)
170{
171 if (bs->poll_state != state)
172 {
173 BFD_DBG ("Setting poll state=%s, bs_idx=%u",
174 bfd_poll_state_string (state), bs->bs_idx);
175 bs->poll_state = state;
176 }
177}
178
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200179static void
180bfd_recalc_tx_interval (bfd_main_t * bm, bfd_session_t * bs)
181{
Klement Sekera239790f2017-02-16 10:53:53 +0100182 bs->transmit_interval_clocks =
183 clib_max (bs->effective_desired_min_tx_clocks, bs->remote_min_rx_clocks);
184 BFD_DBG ("Recalculated transmit interval " BFD_CLK_FMT,
185 BFD_CLK_PRN (bs->transmit_interval_clocks));
186}
187
188static void
189bfd_recalc_echo_tx_interval (bfd_main_t * bm, bfd_session_t * bs)
190{
191 bs->echo_transmit_interval_clocks =
192 clib_max (bs->effective_desired_min_tx_clocks,
193 bs->remote_min_echo_rx_clocks);
194 BFD_DBG ("Recalculated echo transmit interval " BFD_CLK_FMT,
195 BFD_CLK_PRN (bs->echo_transmit_interval_clocks));
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200196}
197
198static void
199bfd_calc_next_tx (bfd_main_t * bm, bfd_session_t * bs, u64 now)
200{
Klement Sekera239790f2017-02-16 10:53:53 +0100201 if (bs->local_detect_mult > 1)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200202 {
Klement Sekera239790f2017-02-16 10:53:53 +0100203 /* common case - 75-100% of transmit interval */
204 bs->tx_timeout_clocks = bs->last_tx_clocks +
205 (1 - .25 * (random_f64 (&bm->random_seed))) *
206 bs->transmit_interval_clocks;
207 if (bs->tx_timeout_clocks < now)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200208 {
Klement Sekera239790f2017-02-16 10:53:53 +0100209 /*
210 * the timeout is in the past, which means that either remote
211 * demand mode was set or performance/clock issues ...
212 */
213 BFD_DBG ("Missed %lu transmit events (now is %lu, calc "
214 "tx_timeout is %lu)",
215 (now - bs->tx_timeout_clocks) /
216 bs->transmit_interval_clocks, now, bs->tx_timeout_clocks);
217 bs->tx_timeout_clocks = now;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200218 }
219 }
220 else
221 {
Klement Sekera239790f2017-02-16 10:53:53 +0100222 /* special case - 75-90% of transmit interval */
223 bs->tx_timeout_clocks = bs->last_tx_clocks +
224 (.9 - .15 * (random_f64 (&bm->random_seed))) *
225 bs->transmit_interval_clocks;
226 if (bs->tx_timeout_clocks < now)
227 {
228 /*
229 * the timeout is in the past, which means that either remote
230 * demand mode was set or performance/clock issues ...
231 */
232 BFD_DBG ("Missed %lu transmit events (now is %lu, calc "
233 "tx_timeout is %lu)",
234 (now - bs->tx_timeout_clocks) /
235 bs->transmit_interval_clocks, now, bs->tx_timeout_clocks);
236 bs->tx_timeout_clocks = now;
237 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200238 }
239 if (bs->tx_timeout_clocks)
240 {
241 BFD_DBG ("Next transmit in %lu clocks/%.02fs@%lu",
242 bs->tx_timeout_clocks - now,
243 (bs->tx_timeout_clocks - now) / bm->cpu_cps,
244 bs->tx_timeout_clocks);
245 }
246}
247
248static void
Klement Sekera239790f2017-02-16 10:53:53 +0100249bfd_calc_next_echo_tx (bfd_main_t * bm, bfd_session_t * bs, u64 now)
250{
251 bs->echo_tx_timeout_clocks =
252 bs->echo_last_tx_clocks + bs->echo_transmit_interval_clocks;
253 if (bs->echo_tx_timeout_clocks < now)
254 {
255 /* huh, we've missed it already, transmit now */
256 BFD_DBG ("Missed %lu echo transmit events (now is %lu, calc tx_timeout "
257 "is %lu)",
258 (now - bs->echo_tx_timeout_clocks) /
259 bs->echo_transmit_interval_clocks,
260 now, bs->echo_tx_timeout_clocks);
261 bs->echo_tx_timeout_clocks = now;
262 }
263 BFD_DBG ("Next echo transmit in %lu clocks/%.02fs@%lu",
264 bs->echo_tx_timeout_clocks - now,
265 (bs->echo_tx_timeout_clocks - now) / bm->cpu_cps,
266 bs->echo_tx_timeout_clocks);
267}
268
269static void
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200270bfd_recalc_detection_time (bfd_main_t * bm, bfd_session_t * bs)
271{
Klement Sekera73884482017-02-23 09:26:30 +0100272 if (bs->local_state == BFD_STATE_init || bs->local_state == BFD_STATE_up)
273 {
274 bs->detection_time_clocks =
275 bs->remote_detect_mult *
276 clib_max (bs->effective_required_min_rx_clocks,
277 bs->remote_desired_min_tx_clocks);
278 BFD_DBG ("Recalculated detection time %lu clocks/%.2fs",
279 bs->detection_time_clocks,
280 bs->detection_time_clocks / bm->cpu_cps);
281 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200282}
283
284static void
Klement Sekerae4504c62016-12-08 10:16:41 +0100285bfd_set_timer (bfd_main_t * bm, bfd_session_t * bs, u64 now,
286 int handling_wakeup)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200287{
288 u64 next = 0;
289 u64 rx_timeout = 0;
Klement Sekera239790f2017-02-16 10:53:53 +0100290 u64 tx_timeout = 0;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200291 if (BFD_STATE_up == bs->local_state)
292 {
293 rx_timeout = bs->last_rx_clocks + bs->detection_time_clocks;
294 }
Klement Sekera73884482017-02-23 09:26:30 +0100295 if (BFD_STATE_up != bs->local_state ||
296 (!bs->remote_demand && bs->remote_min_rx_usec) ||
Klement Sekera239790f2017-02-16 10:53:53 +0100297 BFD_POLL_NOT_NEEDED != bs->poll_state)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200298 {
Klement Sekera239790f2017-02-16 10:53:53 +0100299 tx_timeout = bs->tx_timeout_clocks;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200300 }
Klement Sekera239790f2017-02-16 10:53:53 +0100301 if (tx_timeout && rx_timeout)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200302 {
Klement Sekera239790f2017-02-16 10:53:53 +0100303 next = clib_min (tx_timeout, rx_timeout);
304 }
305 else if (tx_timeout)
306 {
307 next = tx_timeout;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200308 }
309 else if (rx_timeout)
310 {
311 next = rx_timeout;
312 }
Klement Sekera239790f2017-02-16 10:53:53 +0100313 if (bs->echo && next > bs->echo_tx_timeout_clocks)
314 {
315 next = bs->echo_tx_timeout_clocks;
316 }
317 BFD_DBG ("bs_idx=%u, tx_timeout=%lu, echo_tx_timeout=%lu, rx_timeout=%lu, "
318 "next=%s",
319 bs->bs_idx, tx_timeout, bs->echo_tx_timeout_clocks, rx_timeout,
320 next == tx_timeout
321 ? "tx" : (next == bs->echo_tx_timeout_clocks ? "echo tx" : "rx"));
Klement Sekera637b9c42016-12-08 05:19:14 +0100322 /* sometimes the wheel expires an event a bit sooner than requested, account
323 for that here */
324 if (next && (now + bm->wheel_inaccuracy > bs->wheel_time_clocks ||
325 next < bs->wheel_time_clocks || !bs->wheel_time_clocks))
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200326 {
Dave Barach1e3417f2018-07-25 08:30:27 -0400327 int send_signal = 0;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200328 bs->wheel_time_clocks = next;
329 BFD_DBG ("timing_wheel_insert(%p, %lu (%ld clocks/%.2fs in the "
330 "future), %u);",
331 &bm->wheel, bs->wheel_time_clocks,
332 (i64) bs->wheel_time_clocks - clib_cpu_time_now (),
333 (i64) (bs->wheel_time_clocks - clib_cpu_time_now ()) /
334 bm->cpu_cps, bs->bs_idx);
Dave Barach1e3417f2018-07-25 08:30:27 -0400335 bfd_lock (bm);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200336 timing_wheel_insert (&bm->wheel, bs->wheel_time_clocks, bs->bs_idx);
Dave Barach1e3417f2018-07-25 08:30:27 -0400337
Klement Sekerae4504c62016-12-08 10:16:41 +0100338 if (!handling_wakeup)
339 {
Dave Barach1e3417f2018-07-25 08:30:27 -0400340
341 /* Send only if it is earlier than current awaited wakeup time */
342 send_signal =
343 (bs->wheel_time_clocks < bm->bfd_process_next_wakeup_clocks) &&
344 /*
345 * If the wake-up time is within 2x the delay of the event propagation delay,
346 * avoid the expense of sending the event. The 2x multiplier is to workaround the race whereby
347 * simultaneous event + expired timer create one recurring bogus wakeup/suspend instance,
348 * due to double scheduling of the node on the pending list.
349 */
350 (bm->bfd_process_next_wakeup_clocks - bs->wheel_time_clocks >
351 2 * bm->bfd_process_wakeup_event_delay_clocks) &&
352 /* Must be no events in flight to send an event */
353 (!bm->bfd_process_wakeup_events_in_flight);
354
355 /* If we do send the signal, note this down along with the start timestamp */
356 if (send_signal)
357 {
358 bm->bfd_process_wakeup_events_in_flight++;
359 bm->bfd_process_wakeup_event_start_clocks = now;
360 }
361 }
362 bfd_unlock (bm);
363
364 /* Use the multithreaded event sending so the workers can send events too */
365 if (send_signal)
366 {
367 vlib_process_signal_event_mt (bm->vlib_main,
368 bm->bfd_process_node_index,
369 BFD_EVENT_RESCHEDULE, ~0);
Klement Sekerae4504c62016-12-08 10:16:41 +0100370 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200371 }
372}
373
374static void
Klement Sekeraa57a9702017-02-02 06:58:07 +0100375bfd_set_effective_desired_min_tx (bfd_main_t * bm,
376 bfd_session_t * bs, u64 now,
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100377 u64 desired_min_tx_clocks)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200378{
Klement Sekeraa57a9702017-02-02 06:58:07 +0100379 bs->effective_desired_min_tx_clocks = desired_min_tx_clocks;
380 BFD_DBG ("Set effective desired min tx to " BFD_CLK_FMT,
381 BFD_CLK_PRN (bs->effective_desired_min_tx_clocks));
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200382 bfd_recalc_detection_time (bm, bs);
383 bfd_recalc_tx_interval (bm, bs);
Klement Sekera239790f2017-02-16 10:53:53 +0100384 bfd_recalc_echo_tx_interval (bm, bs);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200385 bfd_calc_next_tx (bm, bs, now);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200386}
387
Klement Sekera637b9c42016-12-08 05:19:14 +0100388static void
Klement Sekeraa57a9702017-02-02 06:58:07 +0100389bfd_set_effective_required_min_rx (bfd_main_t * bm,
Klement Sekera73884482017-02-23 09:26:30 +0100390 bfd_session_t * bs,
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100391 u64 required_min_rx_clocks)
Klement Sekeraa57a9702017-02-02 06:58:07 +0100392{
393 bs->effective_required_min_rx_clocks = required_min_rx_clocks;
394 BFD_DBG ("Set effective required min rx to " BFD_CLK_FMT,
395 BFD_CLK_PRN (bs->effective_required_min_rx_clocks));
396 bfd_recalc_detection_time (bm, bs);
Klement Sekeraa57a9702017-02-02 06:58:07 +0100397}
398
399static void
Klement Sekera637b9c42016-12-08 05:19:14 +0100400bfd_set_remote_required_min_rx (bfd_main_t * bm, bfd_session_t * bs,
Klement Sekera239790f2017-02-16 10:53:53 +0100401 u64 now, u32 remote_required_min_rx_usec)
Klement Sekera637b9c42016-12-08 05:19:14 +0100402{
Klement Sekera239790f2017-02-16 10:53:53 +0100403 if (bs->remote_min_rx_usec != remote_required_min_rx_usec)
404 {
405 bs->remote_min_rx_usec = remote_required_min_rx_usec;
406 bs->remote_min_rx_clocks =
407 bfd_usec_to_clocks (bm, remote_required_min_rx_usec);
408 BFD_DBG ("Set remote min rx to " BFD_CLK_FMT,
409 BFD_CLK_PRN (bs->remote_min_rx_clocks));
410 bfd_recalc_detection_time (bm, bs);
411 bfd_recalc_tx_interval (bm, bs);
412 }
413}
414
415static void
416bfd_set_remote_required_min_echo_rx (bfd_main_t * bm, bfd_session_t * bs,
417 u64 now,
418 u32 remote_required_min_echo_rx_usec)
419{
420 if (bs->remote_min_echo_rx_usec != remote_required_min_echo_rx_usec)
421 {
422 bs->remote_min_echo_rx_usec = remote_required_min_echo_rx_usec;
423 bs->remote_min_echo_rx_clocks =
424 bfd_usec_to_clocks (bm, bs->remote_min_echo_rx_usec);
425 BFD_DBG ("Set remote min echo rx to " BFD_CLK_FMT,
426 BFD_CLK_PRN (bs->remote_min_echo_rx_clocks));
427 bfd_recalc_echo_tx_interval (bm, bs);
428 }
Klement Sekera637b9c42016-12-08 05:19:14 +0100429}
430
Neale Ranns88fc83e2017-04-05 08:11:14 -0700431static void
432bfd_notify_listeners (bfd_main_t * bm,
433 bfd_listen_event_e event, const bfd_session_t * bs)
434{
435 bfd_notify_fn_t *fn;
436 vec_foreach (fn, bm->listeners)
437 {
438 (*fn) (event, bs);
439 }
440}
441
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200442void
443bfd_session_start (bfd_main_t * bm, bfd_session_t * bs)
444{
Klement Sekera239790f2017-02-16 10:53:53 +0100445 BFD_DBG ("\nStarting session: %U", format_bfd_session, bs);
Damjan Marion07a38572018-01-21 06:44:18 -0800446 vlib_log_info (bm->log_class, "start BFD session: %U",
447 format_bfd_session_brief, bs);
Klement Sekera73884482017-02-23 09:26:30 +0100448 bfd_set_effective_required_min_rx (bm, bs,
449 bs->config_required_min_rx_clocks);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200450 bfd_recalc_tx_interval (bm, bs);
451 vlib_process_signal_event (bm->vlib_main, bm->bfd_process_node_index,
452 BFD_EVENT_NEW_SESSION, bs->bs_idx);
Neale Ranns88fc83e2017-04-05 08:11:14 -0700453 bfd_notify_listeners (bm, BFD_LISTEN_EVENT_CREATE, bs);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200454}
455
Klement Sekerab17dd962017-01-09 07:43:48 +0100456void
457bfd_session_set_flags (bfd_session_t * bs, u8 admin_up_down)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200458{
459 bfd_main_t *bm = &bfd_main;
Klement Sekera73884482017-02-23 09:26:30 +0100460 u64 now = clib_cpu_time_now ();
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200461 if (admin_up_down)
462 {
Klement Sekerac48829b2017-02-14 07:55:57 +0100463 BFD_DBG ("Session set admin-up, bs-idx=%u", bs->bs_idx);
Damjan Marion07a38572018-01-21 06:44:18 -0800464 vlib_log_info (bm->log_class, "set session admin-up: %U",
465 format_bfd_session_brief, bs);
Klement Sekerae4504c62016-12-08 10:16:41 +0100466 bfd_set_state (bm, bs, BFD_STATE_down, 0);
Klement Sekerac48829b2017-02-14 07:55:57 +0100467 bfd_set_diag (bs, BFD_DIAG_CODE_no_diag);
Klement Sekera73884482017-02-23 09:26:30 +0100468 bfd_calc_next_tx (bm, bs, now);
469 bfd_set_timer (bm, bs, now, 0);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200470 }
471 else
472 {
Klement Sekerac48829b2017-02-14 07:55:57 +0100473 BFD_DBG ("Session set admin-down, bs-idx=%u", bs->bs_idx);
Damjan Marion07a38572018-01-21 06:44:18 -0800474 vlib_log_info (bm->log_class, "set session admin-down: %U",
475 format_bfd_session_brief, bs);
Klement Sekerac48829b2017-02-14 07:55:57 +0100476 bfd_set_diag (bs, BFD_DIAG_CODE_admin_down);
Klement Sekerae4504c62016-12-08 10:16:41 +0100477 bfd_set_state (bm, bs, BFD_STATE_admin_down, 0);
Klement Sekera73884482017-02-23 09:26:30 +0100478 bfd_calc_next_tx (bm, bs, now);
479 bfd_set_timer (bm, bs, now, 0);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200480 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200481}
482
483u8 *
484bfd_input_format_trace (u8 * s, va_list * args)
485{
486 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
487 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
488 const bfd_input_trace_t *t = va_arg (*args, bfd_input_trace_t *);
489 const bfd_pkt_t *pkt = (bfd_pkt_t *) t->data;
490 if (t->len > STRUCT_SIZE_OF (bfd_pkt_t, head))
491 {
492 s = format (s, "BFD v%u, diag=%u(%s), state=%u(%s),\n"
Klement Sekerab17dd962017-01-09 07:43:48 +0100493 " flags=(P:%u, F:%u, C:%u, A:%u, D:%u, M:%u), "
494 "detect_mult=%u, length=%u\n",
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200495 bfd_pkt_get_version (pkt), bfd_pkt_get_diag_code (pkt),
496 bfd_diag_code_string (bfd_pkt_get_diag_code (pkt)),
497 bfd_pkt_get_state (pkt),
498 bfd_state_string (bfd_pkt_get_state (pkt)),
499 bfd_pkt_get_poll (pkt), bfd_pkt_get_final (pkt),
500 bfd_pkt_get_control_plane_independent (pkt),
501 bfd_pkt_get_auth_present (pkt), bfd_pkt_get_demand (pkt),
502 bfd_pkt_get_multipoint (pkt), pkt->head.detect_mult,
503 pkt->head.length);
Klement Sekerab17dd962017-01-09 07:43:48 +0100504 if (t->len >= sizeof (bfd_pkt_t) &&
505 pkt->head.length >= sizeof (bfd_pkt_t))
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200506 {
Klement Sekerad3ba5152017-02-14 03:09:17 +0100507 s = format (s, " my discriminator: %u\n",
508 clib_net_to_host_u32 (pkt->my_disc));
509 s = format (s, " your discriminator: %u\n",
510 clib_net_to_host_u32 (pkt->your_disc));
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200511 s = format (s, " desired min tx interval: %u\n",
512 clib_net_to_host_u32 (pkt->des_min_tx));
513 s = format (s, " required min rx interval: %u\n",
514 clib_net_to_host_u32 (pkt->req_min_rx));
Klement Sekera46a87ad2017-01-02 08:22:23 +0100515 s = format (s, " required min echo rx interval: %u",
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200516 clib_net_to_host_u32 (pkt->req_min_echo_rx));
517 }
Klement Sekerad3ba5152017-02-14 03:09:17 +0100518 if (t->len >= sizeof (bfd_pkt_with_common_auth_t) &&
519 pkt->head.length >= sizeof (bfd_pkt_with_common_auth_t) &&
520 bfd_pkt_get_auth_present (pkt))
521 {
522 const bfd_pkt_with_common_auth_t *with_auth = (void *) pkt;
523 const bfd_auth_common_t *common = &with_auth->common_auth;
524 s = format (s, "\n auth len: %u\n", common->len);
525 s = format (s, " auth type: %u:%s\n", common->type,
526 bfd_auth_type_str (common->type));
527 if (t->len >= sizeof (bfd_pkt_with_sha1_auth_t) &&
528 pkt->head.length >= sizeof (bfd_pkt_with_sha1_auth_t) &&
529 (BFD_AUTH_TYPE_keyed_sha1 == common->type ||
530 BFD_AUTH_TYPE_meticulous_keyed_sha1 == common->type))
531 {
532 const bfd_pkt_with_sha1_auth_t *with_sha1 = (void *) pkt;
533 const bfd_auth_sha1_t *sha1 = &with_sha1->sha1_auth;
534 s = format (s, " seq num: %u\n",
535 clib_net_to_host_u32 (sha1->seq_num));
536 s = format (s, " key id: %u\n", sha1->key_id);
537 s = format (s, " hash: %U", format_hex_bytes, sha1->hash,
538 sizeof (sha1->hash));
539 }
540 }
541 else
542 {
543 s = format (s, "\n");
544 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200545 }
546
547 return s;
548}
549
Dave Barach1e3417f2018-07-25 08:30:27 -0400550typedef struct
551{
552 u32 bs_idx;
553} bfd_rpc_event_t;
554
555static void
556bfd_rpc_event_cb (const bfd_rpc_event_t * a)
557{
558 bfd_main_t *bm = &bfd_main;
559 u32 bs_idx = a->bs_idx;
560 u32 valid_bs = 0;
561 bfd_session_t session_data;
562
563 bfd_lock (bm);
564 if (!pool_is_free_index (bm->sessions, bs_idx))
565 {
566 bfd_session_t *bs = pool_elt_at_index (bm->sessions, bs_idx);
567 clib_memcpy (&session_data, bs, sizeof (bfd_session_t));
568 valid_bs = 1;
569 }
570 else
571 {
572 BFD_DBG ("Ignoring event RPC for non-existent session index %u",
573 bs_idx);
574 }
575 bfd_unlock (bm);
576
577 if (valid_bs)
578 bfd_event (bm, &session_data);
579}
580
581static void
582bfd_event_rpc (u32 bs_idx)
583{
584 const u32 data_size = sizeof (bfd_rpc_event_t);
585 u8 data[data_size];
586 bfd_rpc_event_t *event = (bfd_rpc_event_t *) data;
587
588 event->bs_idx = bs_idx;
589 vl_api_rpc_call_main_thread (bfd_rpc_event_cb, data, data_size);
590}
591
592typedef struct
593{
594 u32 bs_idx;
595} bfd_rpc_notify_listeners_t;
596
597static void
598bfd_rpc_notify_listeners_cb (const bfd_rpc_notify_listeners_t * a)
599{
600 bfd_main_t *bm = &bfd_main;
601 u32 bs_idx = a->bs_idx;
602 bfd_lock (bm);
603 if (!pool_is_free_index (bm->sessions, bs_idx))
604 {
605 bfd_session_t *bs = pool_elt_at_index (bm->sessions, bs_idx);
606 bfd_notify_listeners (bm, BFD_LISTEN_EVENT_UPDATE, bs);
607 }
608 else
609 {
610 BFD_DBG ("Ignoring notify RPC for non-existent session index %u",
611 bs_idx);
612 }
613 bfd_unlock (bm);
614}
615
616static void
617bfd_notify_listeners_rpc (u32 bs_idx)
618{
619 const u32 data_size = sizeof (bfd_rpc_notify_listeners_t);
620 u8 data[data_size];
621 bfd_rpc_notify_listeners_t *notify = (bfd_rpc_notify_listeners_t *) data;
622 notify->bs_idx = bs_idx;
623 vl_api_rpc_call_main_thread (bfd_rpc_notify_listeners_cb, data, data_size);
624}
625
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200626static void
Klement Sekerae4504c62016-12-08 10:16:41 +0100627bfd_on_state_change (bfd_main_t * bm, bfd_session_t * bs, u64 now,
628 int handling_wakeup)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200629{
Klement Sekera239790f2017-02-16 10:53:53 +0100630 BFD_DBG ("\nState changed: %U", format_bfd_session, bs);
Dave Barach1e3417f2018-07-25 08:30:27 -0400631
632 if (vlib_get_thread_index () == 0)
633 {
634 bfd_event (bm, bs);
635 }
636 else
637 {
638 /* without RPC - a REGRESSION: BFD event are not propagated */
639 bfd_event_rpc (bs->bs_idx);
640 }
641
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200642 switch (bs->local_state)
643 {
644 case BFD_STATE_admin_down:
Klement Sekera239790f2017-02-16 10:53:53 +0100645 bs->echo = 0;
Klement Sekeraa57a9702017-02-02 06:58:07 +0100646 bfd_set_effective_desired_min_tx (bm, bs, now,
647 clib_max
648 (bs->config_desired_min_tx_clocks,
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100649 bm->default_desired_min_tx_clocks));
Klement Sekera73884482017-02-23 09:26:30 +0100650 bfd_set_effective_required_min_rx (bm, bs,
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100651 bs->config_required_min_rx_clocks);
652 bfd_set_timer (bm, bs, now, handling_wakeup);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200653 break;
654 case BFD_STATE_down:
Klement Sekera239790f2017-02-16 10:53:53 +0100655 bs->echo = 0;
Klement Sekeraa57a9702017-02-02 06:58:07 +0100656 bfd_set_effective_desired_min_tx (bm, bs, now,
657 clib_max
658 (bs->config_desired_min_tx_clocks,
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100659 bm->default_desired_min_tx_clocks));
Klement Sekera73884482017-02-23 09:26:30 +0100660 bfd_set_effective_required_min_rx (bm, bs,
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100661 bs->config_required_min_rx_clocks);
662 bfd_set_timer (bm, bs, now, handling_wakeup);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200663 break;
664 case BFD_STATE_init:
Klement Sekera239790f2017-02-16 10:53:53 +0100665 bs->echo = 0;
Klement Sekeraa57a9702017-02-02 06:58:07 +0100666 bfd_set_effective_desired_min_tx (bm, bs, now,
Klement Sekerac48829b2017-02-14 07:55:57 +0100667 bs->config_desired_min_tx_clocks);
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100668 bfd_set_timer (bm, bs, now, handling_wakeup);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200669 break;
670 case BFD_STATE_up:
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100671 bfd_set_effective_desired_min_tx (bm, bs, now,
672 bs->config_desired_min_tx_clocks);
Klement Sekera239790f2017-02-16 10:53:53 +0100673 if (BFD_POLL_NOT_NEEDED == bs->poll_state)
Klement Sekeraa57a9702017-02-02 06:58:07 +0100674 {
Klement Sekera73884482017-02-23 09:26:30 +0100675 bfd_set_effective_required_min_rx (bm, bs,
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100676 bs->config_required_min_rx_clocks);
Klement Sekeraa57a9702017-02-02 06:58:07 +0100677 }
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100678 bfd_set_timer (bm, bs, now, handling_wakeup);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200679 break;
680 }
Dave Barach1e3417f2018-07-25 08:30:27 -0400681 if (vlib_get_thread_index () == 0)
682 {
683 bfd_notify_listeners (bm, BFD_LISTEN_EVENT_UPDATE, bs);
684 }
685 else
686 {
687 /* without RPC - a REGRESSION: state changes are not propagated */
688 bfd_notify_listeners_rpc (bs->bs_idx);
689 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200690}
691
692static void
Klement Sekeraa57a9702017-02-02 06:58:07 +0100693bfd_on_config_change (vlib_main_t * vm, vlib_node_runtime_t * rt,
694 bfd_main_t * bm, bfd_session_t * bs, u64 now)
695{
Klement Sekera239790f2017-02-16 10:53:53 +0100696 /*
697 * if remote demand mode is set and we need to do a poll, set the next
698 * timeout so that the session wakes up immediately
699 */
700 if (bs->remote_demand && BFD_POLL_NEEDED == bs->poll_state &&
701 bs->poll_state_start_or_timeout_clocks < now)
Klement Sekeraa57a9702017-02-02 06:58:07 +0100702 {
Klement Sekera239790f2017-02-16 10:53:53 +0100703 bs->tx_timeout_clocks = now;
Klement Sekeraa57a9702017-02-02 06:58:07 +0100704 }
705 bfd_recalc_detection_time (bm, bs);
706 bfd_set_timer (bm, bs, now, 0);
707}
708
709static void
Klement Sekerae50e8562017-04-04 16:19:48 +0200710bfd_add_transport_layer (vlib_main_t * vm, u32 bi, bfd_session_t * bs)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200711{
712 switch (bs->transport)
713 {
714 case BFD_TRANSPORT_UDP4:
Klement Sekera46a87ad2017-01-02 08:22:23 +0100715 BFD_DBG ("Transport bfd via udp4, bs_idx=%u", bs->bs_idx);
Klement Sekerae50e8562017-04-04 16:19:48 +0200716 bfd_add_udp4_transport (vm, bi, bs, 0 /* is_echo */ );
Klement Sekera46a87ad2017-01-02 08:22:23 +0100717 break;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200718 case BFD_TRANSPORT_UDP6:
Klement Sekera46a87ad2017-01-02 08:22:23 +0100719 BFD_DBG ("Transport bfd via udp6, bs_idx=%u", bs->bs_idx);
Klement Sekerae50e8562017-04-04 16:19:48 +0200720 bfd_add_udp6_transport (vm, bi, bs, 0 /* is_echo */ );
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200721 break;
722 }
723}
724
Klement Sekera239790f2017-02-16 10:53:53 +0100725static int
Klement Sekerae50e8562017-04-04 16:19:48 +0200726bfd_transport_control_frame (vlib_main_t * vm, u32 bi, bfd_session_t * bs)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200727{
Klement Sekera239790f2017-02-16 10:53:53 +0100728 switch (bs->transport)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200729 {
Klement Sekera239790f2017-02-16 10:53:53 +0100730 case BFD_TRANSPORT_UDP4:
Klement Sekerae50e8562017-04-04 16:19:48 +0200731 BFD_DBG ("Transport bfd via udp4, bs_idx=%u", bs->bs_idx);
732 return bfd_transport_udp4 (vm, bi, bs);
Klement Sekera239790f2017-02-16 10:53:53 +0100733 break;
734 case BFD_TRANSPORT_UDP6:
Klement Sekerae50e8562017-04-04 16:19:48 +0200735 BFD_DBG ("Transport bfd via udp6, bs_idx=%u", bs->bs_idx);
736 return bfd_transport_udp6 (vm, bi, bs);
Klement Sekera239790f2017-02-16 10:53:53 +0100737 break;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200738 }
Klement Sekera239790f2017-02-16 10:53:53 +0100739 return 0;
740}
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200741
Klement Sekerae50e8562017-04-04 16:19:48 +0200742static int
743bfd_echo_add_transport_layer (vlib_main_t * vm, u32 bi, bfd_session_t * bs)
Klement Sekera239790f2017-02-16 10:53:53 +0100744{
Klement Sekerae50e8562017-04-04 16:19:48 +0200745 switch (bs->transport)
746 {
747 case BFD_TRANSPORT_UDP4:
748 BFD_DBG ("Transport bfd echo via udp4, bs_idx=%u", bs->bs_idx);
749 return bfd_add_udp4_transport (vm, bi, bs, 1 /* is_echo */ );
750 break;
751 case BFD_TRANSPORT_UDP6:
752 BFD_DBG ("Transport bfd echo via udp6, bs_idx=%u", bs->bs_idx);
753 return bfd_add_udp6_transport (vm, bi, bs, 1 /* is_echo */ );
754 break;
755 }
756 return 0;
757}
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200758
Klement Sekerae50e8562017-04-04 16:19:48 +0200759static int
760bfd_transport_echo (vlib_main_t * vm, u32 bi, bfd_session_t * bs)
761{
762 switch (bs->transport)
763 {
764 case BFD_TRANSPORT_UDP4:
765 BFD_DBG ("Transport bfd echo via udp4, bs_idx=%u", bs->bs_idx);
766 return bfd_transport_udp4 (vm, bi, bs);
767 break;
768 case BFD_TRANSPORT_UDP6:
769 BFD_DBG ("Transport bfd echo via udp6, bs_idx=%u", bs->bs_idx);
770 return bfd_transport_udp6 (vm, bi, bs);
771 break;
772 }
773 return 0;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200774}
775
Klement Sekerab17dd962017-01-09 07:43:48 +0100776#if WITH_LIBSSL > 0
777static void
778bfd_add_sha1_auth_section (vlib_buffer_t * b, bfd_session_t * bs)
779{
780 bfd_pkt_with_sha1_auth_t *pkt = vlib_buffer_get_current (b);
781 bfd_auth_sha1_t *auth = &pkt->sha1_auth;
782 b->current_length += sizeof (*auth);
783 pkt->pkt.head.length += sizeof (*auth);
784 bfd_pkt_set_auth_present (&pkt->pkt);
785 memset (auth, 0, sizeof (*auth));
786 auth->type_len.type = bs->auth.curr_key->auth_type;
787 /*
788 * only meticulous authentication types require incrementing seq number
789 * for every message, but doing so doesn't violate the RFC
790 */
791 ++bs->auth.local_seq_number;
792 auth->type_len.len = sizeof (bfd_auth_sha1_t);
793 auth->key_id = bs->auth.curr_bfd_key_id;
794 auth->seq_num = clib_host_to_net_u32 (bs->auth.local_seq_number);
795 /*
796 * first copy the password into the packet, then calculate the hash
797 * and finally replace the password with the calculated hash
798 */
799 clib_memcpy (auth->hash, bs->auth.curr_key->key,
800 sizeof (bs->auth.curr_key->key));
801 unsigned char hash[sizeof (auth->hash)];
802 SHA1 ((unsigned char *) pkt, sizeof (*pkt), hash);
803 BFD_DBG ("hashing: %U", format_hex_bytes, pkt, sizeof (*pkt));
804 clib_memcpy (auth->hash, hash, sizeof (hash));
Klement Sekerab17dd962017-01-09 07:43:48 +0100805}
Klement Sekera6f966492017-02-08 07:42:08 +0100806#endif
Klement Sekerab17dd962017-01-09 07:43:48 +0100807
808static void
809bfd_add_auth_section (vlib_buffer_t * b, bfd_session_t * bs)
810{
Damjan Marion07a38572018-01-21 06:44:18 -0800811 bfd_main_t *bm = &bfd_main;
Klement Sekerab17dd962017-01-09 07:43:48 +0100812 if (bs->auth.curr_key)
813 {
814 const bfd_auth_type_e auth_type = bs->auth.curr_key->auth_type;
815 switch (auth_type)
816 {
817 case BFD_AUTH_TYPE_reserved:
818 /* fallthrough */
819 case BFD_AUTH_TYPE_simple_password:
820 /* fallthrough */
821 case BFD_AUTH_TYPE_keyed_md5:
822 /* fallthrough */
823 case BFD_AUTH_TYPE_meticulous_keyed_md5:
Damjan Marion07a38572018-01-21 06:44:18 -0800824 vlib_log_crit (bm->log_class,
825 "internal error, unexpected BFD auth type '%d'",
826 auth_type);
Klement Sekerab17dd962017-01-09 07:43:48 +0100827 break;
828#if WITH_LIBSSL > 0
829 case BFD_AUTH_TYPE_keyed_sha1:
830 /* fallthrough */
831 case BFD_AUTH_TYPE_meticulous_keyed_sha1:
832 bfd_add_sha1_auth_section (b, bs);
833 break;
834#else
835 case BFD_AUTH_TYPE_keyed_sha1:
836 /* fallthrough */
837 case BFD_AUTH_TYPE_meticulous_keyed_sha1:
Damjan Marion07a38572018-01-21 06:44:18 -0800838 vlib_log_crit (bm->log_class,
839 "internal error, unexpected BFD auth type '%d'",
840 auth_type);
Klement Sekerab17dd962017-01-09 07:43:48 +0100841 break;
842#endif
843 }
844 }
845}
846
Klement Sekera239790f2017-02-16 10:53:53 +0100847static int
848bfd_is_echo_possible (bfd_session_t * bs)
849{
850 if (BFD_STATE_up == bs->local_state && BFD_STATE_up == bs->remote_state &&
851 bs->remote_min_echo_rx_usec > 0)
852 {
853 switch (bs->transport)
854 {
855 case BFD_TRANSPORT_UDP4:
856 return bfd_udp_is_echo_available (BFD_TRANSPORT_UDP4);
857 case BFD_TRANSPORT_UDP6:
858 return bfd_udp_is_echo_available (BFD_TRANSPORT_UDP6);
859 }
860 }
861 return 0;
862}
863
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200864static void
Klement Sekera239790f2017-02-16 10:53:53 +0100865bfd_init_control_frame (bfd_main_t * bm, bfd_session_t * bs,
866 vlib_buffer_t * b)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200867{
868 bfd_pkt_t *pkt = vlib_buffer_get_current (b);
Klement Sekerab17dd962017-01-09 07:43:48 +0100869 u32 bfd_length = 0;
870 bfd_length = sizeof (bfd_pkt_t);
871 memset (pkt, 0, sizeof (*pkt));
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200872 bfd_pkt_set_version (pkt, 1);
873 bfd_pkt_set_diag_code (pkt, bs->local_diag);
874 bfd_pkt_set_state (pkt, bs->local_state);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200875 pkt->head.detect_mult = bs->local_detect_mult;
Klement Sekerae50e8562017-04-04 16:19:48 +0200876 pkt->head.length = bfd_length;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200877 pkt->my_disc = bs->local_discr;
878 pkt->your_disc = bs->remote_discr;
Klement Sekeraa57a9702017-02-02 06:58:07 +0100879 pkt->des_min_tx = clib_host_to_net_u32 (bs->config_desired_min_tx_usec);
Klement Sekera239790f2017-02-16 10:53:53 +0100880 if (bs->echo)
881 {
882 pkt->req_min_rx =
883 clib_host_to_net_u32 (bfd_clocks_to_usec
884 (bm, bs->effective_required_min_rx_clocks));
885 }
886 else
887 {
888 pkt->req_min_rx =
889 clib_host_to_net_u32 (bs->config_required_min_rx_usec);
890 }
Klement Sekeraa57a9702017-02-02 06:58:07 +0100891 pkt->req_min_echo_rx = clib_host_to_net_u32 (1);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200892 b->current_length = bfd_length;
893}
894
895static void
Klement Sekera239790f2017-02-16 10:53:53 +0100896bfd_send_echo (vlib_main_t * vm, vlib_node_runtime_t * rt,
Klement Sekerae50e8562017-04-04 16:19:48 +0200897 bfd_main_t * bm, bfd_session_t * bs, u64 now)
Klement Sekera239790f2017-02-16 10:53:53 +0100898{
899 if (!bfd_is_echo_possible (bs))
900 {
901 BFD_DBG ("\nSwitching off echo function: %U", format_bfd_session, bs);
902 bs->echo = 0;
903 return;
904 }
Klement Sekerae50e8562017-04-04 16:19:48 +0200905 /* sometimes the wheel expires an event a bit sooner than requested,
906 account
Klement Sekera239790f2017-02-16 10:53:53 +0100907 for that here */
908 if (now + bm->wheel_inaccuracy >= bs->echo_tx_timeout_clocks)
909 {
910 BFD_DBG ("\nSending echo packet: %U", format_bfd_session, bs);
911 u32 bi;
912 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
913 {
Damjan Marion07a38572018-01-21 06:44:18 -0800914 vlib_log_crit (bm->log_class, "buffer allocation failure");
Klement Sekera239790f2017-02-16 10:53:53 +0100915 return;
916 }
917 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
918 ASSERT (b->current_data == 0);
Klement Sekerae50e8562017-04-04 16:19:48 +0200919 memset (vnet_buffer (b), 0, sizeof (*vnet_buffer (b)));
920 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b);
Klement Sekera239790f2017-02-16 10:53:53 +0100921 bfd_echo_pkt_t *pkt = vlib_buffer_get_current (b);
922 memset (pkt, 0, sizeof (*pkt));
923 pkt->discriminator = bs->local_discr;
924 pkt->expire_time_clocks =
925 now + bs->echo_transmit_interval_clocks * bs->local_detect_mult;
926 pkt->checksum =
927 bfd_calc_echo_checksum (bs->local_discr, pkt->expire_time_clocks,
928 bs->echo_secret);
929 b->current_length = sizeof (*pkt);
Klement Sekerae50e8562017-04-04 16:19:48 +0200930 if (!bfd_echo_add_transport_layer (vm, bi, bs))
931 {
932 BFD_ERR ("cannot send echo packet out, turning echo off");
933 bs->echo = 0;
934 vlib_buffer_free_one (vm, bi);
935 return;
936 }
937 if (!bfd_transport_echo (vm, bi, bs))
Klement Sekera239790f2017-02-16 10:53:53 +0100938 {
939 BFD_ERR ("cannot send echo packet out, turning echo off");
940 bs->echo = 0;
941 vlib_buffer_free_one (vm, bi);
942 return;
943 }
944 bs->echo_last_tx_clocks = now;
945 bfd_calc_next_echo_tx (bm, bs, now);
Klement Sekera239790f2017-02-16 10:53:53 +0100946 }
947 else
948 {
949 BFD_DBG
950 ("No need to send echo packet now, now is %lu, tx_timeout is %lu",
951 now, bs->echo_tx_timeout_clocks);
952 }
953}
954
955static void
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200956bfd_send_periodic (vlib_main_t * vm, vlib_node_runtime_t * rt,
Klement Sekerae50e8562017-04-04 16:19:48 +0200957 bfd_main_t * bm, bfd_session_t * bs, u64 now)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200958{
Klement Sekera239790f2017-02-16 10:53:53 +0100959 if (!bs->remote_min_rx_usec && BFD_POLL_NOT_NEEDED == bs->poll_state)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200960 {
Klement Sekera239790f2017-02-16 10:53:53 +0100961 BFD_DBG ("Remote min rx interval is zero, not sending periodic control "
962 "frame");
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200963 return;
964 }
Klement Sekera239790f2017-02-16 10:53:53 +0100965 if (BFD_POLL_NOT_NEEDED == bs->poll_state && bs->remote_demand &&
Klement Sekerad3ba5152017-02-14 03:09:17 +0100966 BFD_STATE_up == bs->local_state && BFD_STATE_up == bs->remote_state)
967 {
968 /*
969 * A system MUST NOT periodically transmit BFD Control packets if Demand
970 * mode is active on the remote system (bfd.RemoteDemandMode is 1,
971 * bfd.SessionState is Up, and bfd.RemoteSessionState is Up) and a Poll
972 * Sequence is not being transmitted.
973 */
Klement Sekera239790f2017-02-16 10:53:53 +0100974 BFD_DBG ("Remote demand is set, not sending periodic control frame");
Klement Sekerad3ba5152017-02-14 03:09:17 +0100975 return;
976 }
Klement Sekerae50e8562017-04-04 16:19:48 +0200977 /*
978 * sometimes the wheel expires an event a bit sooner than requested, account
979 * for that here
980 */
Klement Sekera637b9c42016-12-08 05:19:14 +0100981 if (now + bm->wheel_inaccuracy >= bs->tx_timeout_clocks)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200982 {
Klement Sekera239790f2017-02-16 10:53:53 +0100983 BFD_DBG ("\nSending periodic control frame: %U", format_bfd_session,
984 bs);
985 u32 bi;
986 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200987 {
Damjan Marion07a38572018-01-21 06:44:18 -0800988 vlib_log_crit (bm->log_class, "buffer allocation failure");
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200989 return;
990 }
Klement Sekera239790f2017-02-16 10:53:53 +0100991 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
992 ASSERT (b->current_data == 0);
Klement Sekerae50e8562017-04-04 16:19:48 +0200993 memset (vnet_buffer (b), 0, sizeof (*vnet_buffer (b)));
994 VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b);
Klement Sekera239790f2017-02-16 10:53:53 +0100995 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:
999 if (now < bs->poll_state_start_or_timeout_clocks)
1000 {
1001 BFD_DBG ("Cannot start a poll sequence yet, need to wait "
1002 "for " BFD_CLK_FMT,
1003 BFD_CLK_PRN (bs->poll_state_start_or_timeout_clocks -
1004 now));
1005 break;
1006 }
1007 bs->poll_state_start_or_timeout_clocks = now;
1008 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 Sekera3e0a3562016-12-19 09:05:21 +01001025 bs->last_tx_clocks = 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",
1032 now, bs->tx_timeout_clocks);
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 Sekera3e0a3562016-12-19 09:05:21 +01001047 bs->last_tx_clocks = clib_cpu_time_now ();
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 Sekerae4504c62016-12-08 10:16:41 +01001056bfd_check_rx_timeout (bfd_main_t * bm, bfd_session_t * bs, u64 now,
1057 int handling_wakeup)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001058{
Klement Sekerae50e8562017-04-04 16:19:48 +02001059 /*
1060 * sometimes the wheel expires an event a bit sooner than requested, account
1061 * for that here
1062 */
Klement Sekera637b9c42016-12-08 05:19:14 +01001063 if (bs->last_rx_clocks + bs->detection_time_clocks <=
1064 now + bm->wheel_inaccuracy)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001065 {
1066 BFD_DBG ("Rx timeout, session goes down");
1067 bfd_set_diag (bs, BFD_DIAG_CODE_det_time_exp);
Klement Sekerae4504c62016-12-08 10:16:41 +01001068 bfd_set_state (bm, bs, BFD_STATE_down, handling_wakeup);
Klement Sekeraa57a9702017-02-02 06:58:07 +01001069 /*
1070 * If the remote system does not receive any
1071 * BFD Control packets for a Detection Time, it SHOULD reset
1072 * bfd.RemoteMinRxInterval to its initial value of 1 (per section 6.8.1,
1073 * since it is no longer required to maintain previous session state)
1074 * and then can transmit at its own rate.
1075 */
Klement Sekera239790f2017-02-16 10:53:53 +01001076 bfd_set_remote_required_min_rx (bm, bs, now, 1);
1077 }
1078 else if (bs->echo &&
1079 bs->echo_last_rx_clocks +
1080 bs->echo_transmit_interval_clocks * bs->local_detect_mult <=
1081 now + bm->wheel_inaccuracy)
1082 {
1083 BFD_DBG ("Echo rx timeout, session goes down");
1084 bfd_set_diag (bs, BFD_DIAG_CODE_echo_failed);
1085 bfd_set_state (bm, bs, BFD_STATE_down, handling_wakeup);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001086 }
1087}
1088
1089void
1090bfd_on_timeout (vlib_main_t * vm, vlib_node_runtime_t * rt, bfd_main_t * bm,
1091 bfd_session_t * bs, u64 now)
1092{
1093 BFD_DBG ("Timeout for bs_idx=%lu", bs->bs_idx);
1094 switch (bs->local_state)
1095 {
1096 case BFD_STATE_admin_down:
Klement Sekerae50e8562017-04-04 16:19:48 +02001097 bfd_send_periodic (vm, rt, bm, bs, now);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001098 break;
1099 case BFD_STATE_down:
Klement Sekerae50e8562017-04-04 16:19:48 +02001100 bfd_send_periodic (vm, rt, bm, bs, now);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001101 break;
1102 case BFD_STATE_init:
Klement Sekerae4504c62016-12-08 10:16:41 +01001103 bfd_check_rx_timeout (bm, bs, now, 1);
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;
Klement Sekera239790f2017-02-16 10:53:53 +01001106 case BFD_STATE_up:
1107 bfd_check_rx_timeout (bm, bs, now, 1);
1108 if (BFD_POLL_NOT_NEEDED == bs->poll_state && !bs->echo &&
1109 bfd_is_echo_possible (bs))
1110 {
1111 /* switch on echo function as main detection method now */
1112 BFD_DBG ("Switching on echo function, bs_idx=%u", bs->bs_idx);
1113 bs->echo = 1;
1114 bs->echo_last_rx_clocks = now;
1115 bs->echo_tx_timeout_clocks = now;
Klement Sekera73884482017-02-23 09:26:30 +01001116 bfd_set_effective_required_min_rx (bm, bs,
Klement Sekera239790f2017-02-16 10:53:53 +01001117 clib_max
1118 (bm->min_required_min_rx_while_echo_clocks,
1119 bs->config_required_min_rx_clocks));
1120 bfd_set_poll_state (bs, BFD_POLL_NEEDED);
1121 }
Klement Sekerae50e8562017-04-04 16:19:48 +02001122 bfd_send_periodic (vm, rt, bm, bs, now);
Klement Sekera239790f2017-02-16 10:53:53 +01001123 if (bs->echo)
1124 {
Klement Sekerae50e8562017-04-04 16:19:48 +02001125 bfd_send_echo (vm, rt, bm, bs, now);
Klement Sekera239790f2017-02-16 10:53:53 +01001126 }
1127 break;
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001128 }
1129}
1130
1131/*
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001132 * bfd process node function
1133 */
1134static uword
1135bfd_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
1136{
1137 bfd_main_t *bm = &bfd_main;
1138 u32 *expired = 0;
1139 uword event_type, *event_data = 0;
1140
1141 /* So we can send events to the bfd process */
1142 bm->bfd_process_node_index = bfd_process_node.index;
1143
1144 while (1)
1145 {
1146 u64 now = clib_cpu_time_now ();
Dave Barach1e3417f2018-07-25 08:30:27 -04001147 bfd_lock (bm);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001148 u64 next_expire = timing_wheel_next_expiring_elt_time (&bm->wheel);
1149 BFD_DBG ("timing_wheel_next_expiring_elt_time(%p) returns %lu",
1150 &bm->wheel, next_expire);
Dave Barach1e3417f2018-07-25 08:30:27 -04001151 bm->bfd_process_next_wakeup_clocks =
1152 (i64) next_expire >= 0 ? next_expire : ~0;
1153 bfd_unlock (bm);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001154 if ((i64) next_expire < 0)
1155 {
1156 BFD_DBG ("wait for event without timeout");
1157 (void) vlib_process_wait_for_event (vm);
Klement Sekera0c1519b2016-12-08 05:03:32 +01001158 event_type = vlib_process_get_events (vm, &event_data);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001159 }
1160 else
1161 {
1162 f64 timeout = ((i64) next_expire - (i64) now) / bm->cpu_cps;
1163 BFD_DBG ("wait for event with timeout %.02f", timeout);
Klement Sekera0c1519b2016-12-08 05:03:32 +01001164 if (timeout < 0)
1165 {
1166 BFD_DBG ("negative timeout, already expired, skipping wait");
1167 event_type = ~0;
1168 }
1169 else
1170 {
1171 (void) vlib_process_wait_for_event_or_clock (vm, timeout);
1172 event_type = vlib_process_get_events (vm, &event_data);
1173 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001174 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001175 now = clib_cpu_time_now ();
1176 switch (event_type)
1177 {
1178 case ~0: /* no events => timeout */
1179 /* nothing to do here */
1180 break;
1181 case BFD_EVENT_RESCHEDULE:
Dave Barach1e3417f2018-07-25 08:30:27 -04001182 bfd_lock (bm);
1183 bm->bfd_process_wakeup_event_delay_clocks =
1184 now - bm->bfd_process_wakeup_event_start_clocks;
1185 bm->bfd_process_wakeup_events_in_flight--;
1186 bfd_unlock (bm);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001187 /* nothing to do here - reschedule is done automatically after
1188 * each event or timeout */
1189 break;
1190 case BFD_EVENT_NEW_SESSION:
Dave Barach1e3417f2018-07-25 08:30:27 -04001191 bfd_lock (bm);
Klement Sekerab17dd962017-01-09 07:43:48 +01001192 if (!pool_is_free_index (bm->sessions, *event_data))
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001193 {
1194 bfd_session_t *bs =
1195 pool_elt_at_index (bm->sessions, *event_data);
Klement Sekerae50e8562017-04-04 16:19:48 +02001196 bfd_send_periodic (vm, rt, bm, bs, now);
1197 bfd_set_timer (bm, bs, now, 1);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001198 }
Klement Sekerab17dd962017-01-09 07:43:48 +01001199 else
1200 {
1201 BFD_DBG ("Ignoring event for non-existent session index %u",
1202 (u32) * event_data);
1203 }
Dave Barach1e3417f2018-07-25 08:30:27 -04001204 bfd_unlock (bm);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001205 break;
Klement Sekeraa57a9702017-02-02 06:58:07 +01001206 case BFD_EVENT_CONFIG_CHANGED:
Dave Barach1e3417f2018-07-25 08:30:27 -04001207 bfd_lock (bm);
Klement Sekeraa57a9702017-02-02 06:58:07 +01001208 if (!pool_is_free_index (bm->sessions, *event_data))
1209 {
1210 bfd_session_t *bs =
1211 pool_elt_at_index (bm->sessions, *event_data);
1212 bfd_on_config_change (vm, rt, bm, bs, now);
1213 }
1214 else
1215 {
1216 BFD_DBG ("Ignoring event for non-existent session index %u",
1217 (u32) * event_data);
1218 }
Dave Barach1e3417f2018-07-25 08:30:27 -04001219 bfd_unlock (bm);
Klement Sekeraa57a9702017-02-02 06:58:07 +01001220 break;
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001221 default:
Damjan Marion07a38572018-01-21 06:44:18 -08001222 vlib_log_err (bm->log_class, "BUG: event type 0x%wx", event_type);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001223 break;
1224 }
1225 BFD_DBG ("advancing wheel, now is %lu", now);
1226 BFD_DBG ("timing_wheel_advance (%p, %lu, %p, 0);", &bm->wheel, now,
1227 expired);
Dave Barach1e3417f2018-07-25 08:30:27 -04001228 bfd_lock (bm);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001229 expired = timing_wheel_advance (&bm->wheel, now, expired, 0);
1230 BFD_DBG ("Expired %d elements", vec_len (expired));
1231 u32 *p = NULL;
1232 vec_foreach (p, expired)
1233 {
1234 const u32 bs_idx = *p;
1235 if (!pool_is_free_index (bm->sessions, bs_idx))
1236 {
1237 bfd_session_t *bs = pool_elt_at_index (bm->sessions, bs_idx);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001238 bfd_on_timeout (vm, rt, bm, bs, now);
Klement Sekera239790f2017-02-16 10:53:53 +01001239 bfd_set_timer (bm, bs, now, 1);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001240 }
1241 }
Dave Barach1e3417f2018-07-25 08:30:27 -04001242 bfd_unlock (bm);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001243 if (expired)
1244 {
1245 _vec_len (expired) = 0;
1246 }
1247 if (event_data)
1248 {
1249 _vec_len (event_data) = 0;
1250 }
1251 }
1252
1253 return 0;
1254}
1255
1256/*
1257 * bfd process node declaration
1258 */
1259/* *INDENT-OFF* */
1260VLIB_REGISTER_NODE (bfd_process_node, static) = {
1261 .function = bfd_process,
1262 .type = VLIB_NODE_TYPE_PROCESS,
1263 .name = "bfd-process",
Klement Sekera46a87ad2017-01-02 08:22:23 +01001264 .n_next_nodes = 0,
1265 .next_nodes = {},
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001266};
1267/* *INDENT-ON* */
1268
1269static clib_error_t *
1270bfd_sw_interface_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
1271{
1272 // bfd_main_t *bm = &bfd_main;
1273 // vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
1274 if (!(flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
1275 {
1276 /* TODO */
1277 }
1278 return 0;
1279}
1280
1281VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (bfd_sw_interface_up_down);
1282
1283static clib_error_t *
1284bfd_hw_interface_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
1285{
1286 // bfd_main_t *bm = &bfd_main;
1287 if (flags & VNET_HW_INTERFACE_FLAG_LINK_UP)
1288 {
1289 /* TODO */
1290 }
1291 return 0;
1292}
1293
1294VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION (bfd_hw_interface_up_down);
1295
Neale Ranns88fc83e2017-04-05 08:11:14 -07001296void
1297bfd_register_listener (bfd_notify_fn_t fn)
1298{
1299 bfd_main_t *bm = &bfd_main;
1300
1301 vec_add1 (bm->listeners, fn);
1302}
1303
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001304/*
1305 * setup function
1306 */
1307static clib_error_t *
1308bfd_main_init (vlib_main_t * vm)
1309{
Dave Barach1e3417f2018-07-25 08:30:27 -04001310 vlib_thread_main_t *tm = &vlib_thread_main;
1311 u32 n_vlib_mains = tm->n_vlib_mains;
Klement Sekerab17dd962017-01-09 07:43:48 +01001312#if BFD_DEBUG
1313 setbuf (stdout, NULL);
1314#endif
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001315 bfd_main_t *bm = &bfd_main;
1316 bm->random_seed = random_default_seed ();
1317 bm->vlib_main = vm;
1318 bm->vnet_main = vnet_get_main ();
1319 memset (&bm->wheel, 0, sizeof (bm->wheel));
Klement Sekerab17dd962017-01-09 07:43:48 +01001320 bm->cpu_cps = vm->clib_time.clocks_per_second;
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001321 BFD_DBG ("cps is %.2f", bm->cpu_cps);
Klement Sekeraa57a9702017-02-02 06:58:07 +01001322 bm->default_desired_min_tx_clocks =
Klement Sekera239790f2017-02-16 10:53:53 +01001323 bfd_usec_to_clocks (bm, BFD_DEFAULT_DESIRED_MIN_TX_USEC);
1324 bm->min_required_min_rx_while_echo_clocks =
1325 bfd_usec_to_clocks (bm, BFD_REQUIRED_MIN_RX_USEC_WHILE_ECHO);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001326 const u64 now = clib_cpu_time_now ();
1327 timing_wheel_init (&bm->wheel, now, bm->cpu_cps);
Klement Sekera637b9c42016-12-08 05:19:14 +01001328 bm->wheel_inaccuracy = 2 << bm->wheel.log2_clocks_per_bin;
Damjan Marion07a38572018-01-21 06:44:18 -08001329 bm->log_class = vlib_log_register_class ("bfd", 0);
1330 vlib_log_debug (bm->log_class, "initialized");
Dave Barach1e3417f2018-07-25 08:30:27 -04001331 bm->owner_thread_index = ~0;
1332 if (n_vlib_mains > 1)
1333 clib_spinlock_init (&bm->lock);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001334 return 0;
1335}
1336
1337VLIB_INIT_FUNCTION (bfd_main_init);
1338
1339bfd_session_t *
Klement Sekera239790f2017-02-16 10:53:53 +01001340bfd_get_session (bfd_main_t * bm, bfd_transport_e t)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001341{
1342 bfd_session_t *result;
Dave Barach1e3417f2018-07-25 08:30:27 -04001343
1344 bfd_lock (bm);
1345
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001346 pool_get (bm->sessions, result);
Klement Sekerae4504c62016-12-08 10:16:41 +01001347 memset (result, 0, sizeof (*result));
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001348 result->bs_idx = result - bm->sessions;
1349 result->transport = t;
Klement Sekera239790f2017-02-16 10:53:53 +01001350 const unsigned limit = 1000;
1351 unsigned counter = 0;
1352 do
1353 {
1354 result->local_discr = random_u32 (&bm->random_seed);
1355 if (counter > limit)
1356 {
Damjan Marion07a38572018-01-21 06:44:18 -08001357 vlib_log_crit (bm->log_class,
1358 "couldn't allocate unused session discriminator even "
1359 "after %u tries!", limit);
Klement Sekera239790f2017-02-16 10:53:53 +01001360 pool_put (bm->sessions, result);
Dave Barach1e3417f2018-07-25 08:30:27 -04001361 bfd_unlock (bm);
Klement Sekera239790f2017-02-16 10:53:53 +01001362 return NULL;
1363 }
1364 ++counter;
1365 }
1366 while (hash_get (bm->session_by_disc, result->local_discr));
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001367 bfd_set_defaults (bm, result);
1368 hash_set (bm->session_by_disc, result->local_discr, result->bs_idx);
Dave Barach1e3417f2018-07-25 08:30:27 -04001369 bfd_unlock (bm);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001370 return result;
1371}
1372
1373void
1374bfd_put_session (bfd_main_t * bm, bfd_session_t * bs)
1375{
Dave Barach1e3417f2018-07-25 08:30:27 -04001376 bfd_lock (bm);
1377
Damjan Marion07a38572018-01-21 06:44:18 -08001378 vlib_log_info (bm->log_class, "delete session: %U",
1379 format_bfd_session_brief, bs);
Neale Ranns88fc83e2017-04-05 08:11:14 -07001380 bfd_notify_listeners (bm, BFD_LISTEN_EVENT_DELETE, bs);
Klement Sekerab17dd962017-01-09 07:43:48 +01001381 if (bs->auth.curr_key)
1382 {
1383 --bs->auth.curr_key->use_count;
1384 }
1385 if (bs->auth.next_key)
1386 {
1387 --bs->auth.next_key->use_count;
1388 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001389 hash_unset (bm->session_by_disc, bs->local_discr);
1390 pool_put (bm->sessions, bs);
Dave Barach1e3417f2018-07-25 08:30:27 -04001391 bfd_unlock (bm);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001392}
1393
1394bfd_session_t *
1395bfd_find_session_by_idx (bfd_main_t * bm, uword bs_idx)
1396{
Dave Barach1e3417f2018-07-25 08:30:27 -04001397 bfd_lock_check (bm);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001398 if (!pool_is_free_index (bm->sessions, bs_idx))
1399 {
1400 return pool_elt_at_index (bm->sessions, bs_idx);
1401 }
1402 return NULL;
1403}
1404
1405bfd_session_t *
1406bfd_find_session_by_disc (bfd_main_t * bm, u32 disc)
1407{
Dave Barach1e3417f2018-07-25 08:30:27 -04001408 bfd_lock_check (bm);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001409 uword *p = hash_get (bfd_main.session_by_disc, disc);
1410 if (p)
1411 {
1412 return pool_elt_at_index (bfd_main.sessions, *p);
1413 }
1414 return NULL;
1415}
1416
1417/**
1418 * @brief verify bfd packet - common checks
1419 *
1420 * @param pkt
1421 *
1422 * @return 1 if bfd packet is valid
1423 */
1424int
1425bfd_verify_pkt_common (const bfd_pkt_t * pkt)
1426{
1427 if (1 != bfd_pkt_get_version (pkt))
1428 {
1429 BFD_ERR ("BFD verification failed - unexpected version: '%d'",
1430 bfd_pkt_get_version (pkt));
1431 return 0;
1432 }
1433 if (pkt->head.length < sizeof (bfd_pkt_t) ||
1434 (bfd_pkt_get_auth_present (pkt) &&
Klement Sekerab17dd962017-01-09 07:43:48 +01001435 pkt->head.length < sizeof (bfd_pkt_with_common_auth_t)))
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001436 {
1437 BFD_ERR ("BFD verification failed - unexpected length: '%d' (auth "
1438 "present: %d)",
1439 pkt->head.length, bfd_pkt_get_auth_present (pkt));
1440 return 0;
1441 }
1442 if (!pkt->head.detect_mult)
1443 {
1444 BFD_ERR ("BFD verification failed - unexpected detect-mult: '%d'",
1445 pkt->head.detect_mult);
1446 return 0;
1447 }
1448 if (bfd_pkt_get_multipoint (pkt))
1449 {
1450 BFD_ERR ("BFD verification failed - unexpected multipoint: '%d'",
1451 bfd_pkt_get_multipoint (pkt));
1452 return 0;
1453 }
1454 if (!pkt->my_disc)
1455 {
1456 BFD_ERR ("BFD verification failed - unexpected my-disc: '%d'",
1457 pkt->my_disc);
1458 return 0;
1459 }
1460 if (!pkt->your_disc)
1461 {
1462 const u8 pkt_state = bfd_pkt_get_state (pkt);
1463 if (pkt_state != BFD_STATE_down && pkt_state != BFD_STATE_admin_down)
1464 {
1465 BFD_ERR ("BFD verification failed - unexpected state: '%s' "
1466 "(your-disc is zero)", bfd_state_string (pkt_state));
1467 return 0;
1468 }
1469 }
1470 return 1;
1471}
1472
Klement Sekerab17dd962017-01-09 07:43:48 +01001473static void
1474bfd_session_switch_auth_to_next (bfd_session_t * bs)
1475{
1476 BFD_DBG ("Switching authentication key from %U to %U for bs_idx=%u",
1477 format_bfd_auth_key, bs->auth.curr_key, format_bfd_auth_key,
1478 bs->auth.next_key, bs->bs_idx);
1479 bs->auth.is_delayed = 0;
1480 if (bs->auth.curr_key)
1481 {
1482 --bs->auth.curr_key->use_count;
1483 }
1484 bs->auth.curr_key = bs->auth.next_key;
1485 bs->auth.next_key = NULL;
1486 bs->auth.curr_bfd_key_id = bs->auth.next_bfd_key_id;
1487}
1488
1489static int
1490bfd_auth_type_is_meticulous (bfd_auth_type_e auth_type)
1491{
1492 if (BFD_AUTH_TYPE_meticulous_keyed_md5 == auth_type ||
1493 BFD_AUTH_TYPE_meticulous_keyed_sha1 == auth_type)
1494 {
1495 return 1;
1496 }
1497 return 0;
1498}
1499
1500static int
1501bfd_verify_pkt_auth_seq_num (bfd_session_t * bs,
1502 u32 received_seq_num, int is_meticulous)
1503{
1504 /*
1505 * RFC 5880 6.8.1:
1506 *
1507 * This variable MUST be set to zero after no packets have been
1508 * received on this session for at least twice the Detection Time.
1509 */
1510 u64 now = clib_cpu_time_now ();
1511 if (now - bs->last_rx_clocks > bs->detection_time_clocks * 2)
1512 {
1513 BFD_DBG ("BFD peer unresponsive for %lu clocks, which is > 2 * "
1514 "detection_time=%u clocks, resetting remote_seq_number_known "
1515 "flag",
1516 now - bs->last_rx_clocks, bs->detection_time_clocks * 2);
1517 bs->auth.remote_seq_number_known = 0;
1518 }
1519 if (bs->auth.remote_seq_number_known)
1520 {
1521 /* remote sequence number is known, verify its validity */
1522 const u32 max_u32 = 0xffffffff;
1523 /* the calculation might wrap, account for the special case... */
1524 if (bs->auth.remote_seq_number > max_u32 - 3 * bs->local_detect_mult)
1525 {
1526 /*
1527 * special case
1528 *
1529 * x y z
1530 * |----------+----------------------------+-----------|
1531 * 0 ^ ^ 0xffffffff
1532 * | remote_seq_num------+
1533 * |
1534 * +-----(remote_seq_num + 3*detect_mult) % * 0xffffffff
1535 *
1536 * x + y + z = 0xffffffff
1537 * x + z = 3 * detect_mult
1538 */
1539 const u32 z = max_u32 - bs->auth.remote_seq_number;
1540 const u32 x = 3 * bs->local_detect_mult - z;
1541 if (received_seq_num > x &&
1542 received_seq_num < bs->auth.remote_seq_number + is_meticulous)
1543 {
1544 BFD_ERR
1545 ("Recvd sequence number=%u out of ranges <0, %u>, <%u, %u>",
1546 received_seq_num, x,
1547 bs->auth.remote_seq_number + is_meticulous, max_u32);
1548 return 0;
1549 }
1550 }
1551 else
1552 {
1553 /* regular case */
1554 const u32 min = bs->auth.remote_seq_number + is_meticulous;
1555 const u32 max =
1556 bs->auth.remote_seq_number + 3 * bs->local_detect_mult;
1557 if (received_seq_num < min || received_seq_num > max)
1558 {
1559 BFD_ERR ("Recvd sequence number=%u out of range <%u, %u>",
1560 received_seq_num, min, max);
1561 return 0;
1562 }
1563 }
1564 }
1565 return 1;
1566}
1567
1568static int
1569bfd_verify_pkt_auth_key_sha1 (const bfd_pkt_t * pkt, u32 pkt_size,
1570 bfd_session_t * bs, u8 bfd_key_id,
1571 bfd_auth_key_t * auth_key)
1572{
1573 ASSERT (auth_key->auth_type == BFD_AUTH_TYPE_keyed_sha1 ||
1574 auth_key->auth_type == BFD_AUTH_TYPE_meticulous_keyed_sha1);
1575
1576 u8 result[SHA_DIGEST_LENGTH];
1577 bfd_pkt_with_common_auth_t *with_common = (void *) pkt;
1578 if (pkt_size < sizeof (*with_common))
1579 {
1580 BFD_ERR ("Packet size too small to hold authentication common header");
1581 return 0;
1582 }
1583 if (with_common->common_auth.type != auth_key->auth_type)
1584 {
1585 BFD_ERR ("BFD auth type mismatch, packet auth=%d:%s doesn't match "
1586 "in-use auth=%d:%s",
1587 with_common->common_auth.type,
1588 bfd_auth_type_str (with_common->common_auth.type),
1589 auth_key->auth_type, bfd_auth_type_str (auth_key->auth_type));
1590 return 0;
1591 }
1592 bfd_pkt_with_sha1_auth_t *with_sha1 = (void *) pkt;
1593 if (pkt_size < sizeof (*with_sha1) ||
1594 with_sha1->sha1_auth.type_len.len < sizeof (with_sha1->sha1_auth))
1595 {
1596 BFD_ERR
1597 ("BFD size mismatch, payload size=%u, expected=%u, auth_len=%u, "
1598 "expected=%u", pkt_size, sizeof (*with_sha1),
1599 with_sha1->sha1_auth.type_len.len, sizeof (with_sha1->sha1_auth));
1600 return 0;
1601 }
1602 if (with_sha1->sha1_auth.key_id != bfd_key_id)
1603 {
1604 BFD_ERR
1605 ("BFD key ID mismatch, packet key ID=%u doesn't match key ID=%u%s",
1606 with_sha1->sha1_auth.key_id, bfd_key_id,
1607 bs->
1608 auth.is_delayed ? " (but a delayed auth change is scheduled)" : "");
1609 return 0;
1610 }
1611 SHA_CTX ctx;
1612 if (!SHA1_Init (&ctx))
1613 {
1614 BFD_ERR ("SHA1_Init failed");
1615 return 0;
1616 }
1617 /* ignore last 20 bytes - use the actual key data instead pkt data */
1618 if (!SHA1_Update (&ctx, with_sha1,
1619 sizeof (*with_sha1) - sizeof (with_sha1->sha1_auth.hash)))
1620 {
1621 BFD_ERR ("SHA1_Update failed");
1622 return 0;
1623 }
1624 if (!SHA1_Update (&ctx, auth_key->key, sizeof (auth_key->key)))
1625 {
1626 BFD_ERR ("SHA1_Update failed");
1627 return 0;
1628 }
1629 if (!SHA1_Final (result, &ctx))
1630 {
1631 BFD_ERR ("SHA1_Final failed");
1632 return 0;
1633 }
1634 if (0 == memcmp (result, with_sha1->sha1_auth.hash, SHA_DIGEST_LENGTH))
1635 {
1636 return 1;
1637 }
1638 BFD_ERR ("SHA1 hash: %U doesn't match the expected value: %U",
1639 format_hex_bytes, with_sha1->sha1_auth.hash, SHA_DIGEST_LENGTH,
1640 format_hex_bytes, result, SHA_DIGEST_LENGTH);
1641 return 0;
1642}
1643
1644static int
1645bfd_verify_pkt_auth_key (const bfd_pkt_t * pkt, u32 pkt_size,
1646 bfd_session_t * bs, u8 bfd_key_id,
1647 bfd_auth_key_t * auth_key)
1648{
Damjan Marion07a38572018-01-21 06:44:18 -08001649 bfd_main_t *bm = &bfd_main;
Klement Sekerab17dd962017-01-09 07:43:48 +01001650 switch (auth_key->auth_type)
1651 {
1652 case BFD_AUTH_TYPE_reserved:
Damjan Marion07a38572018-01-21 06:44:18 -08001653 vlib_log_err (bm->log_class,
1654 "internal error, unexpected auth_type=%d:%s",
Klement Sekerab17dd962017-01-09 07:43:48 +01001655 auth_key->auth_type,
1656 bfd_auth_type_str (auth_key->auth_type));
1657 return 0;
1658 case BFD_AUTH_TYPE_simple_password:
Damjan Marion07a38572018-01-21 06:44:18 -08001659 vlib_log_err (bm->log_class,
1660 "internal error, not implemented, unexpected auth_type=%d:%s",
1661 auth_key->auth_type,
1662 bfd_auth_type_str (auth_key->auth_type));
Klement Sekerab17dd962017-01-09 07:43:48 +01001663 return 0;
1664 case BFD_AUTH_TYPE_keyed_md5:
1665 /* fallthrough */
1666 case BFD_AUTH_TYPE_meticulous_keyed_md5:
Damjan Marion07a38572018-01-21 06:44:18 -08001667 vlib_log_err
1668 (bm->log_class,
1669 "internal error, not implemented, unexpected auth_type=%d:%s",
Klement Sekerab17dd962017-01-09 07:43:48 +01001670 auth_key->auth_type, bfd_auth_type_str (auth_key->auth_type));
1671 return 0;
1672 case BFD_AUTH_TYPE_keyed_sha1:
1673 /* fallthrough */
1674 case BFD_AUTH_TYPE_meticulous_keyed_sha1:
1675#if WITH_LIBSSL > 0
1676 do
1677 {
1678 const u32 seq_num = clib_net_to_host_u32 (((bfd_pkt_with_sha1_auth_t
1679 *) pkt)->
1680 sha1_auth.seq_num);
1681 return bfd_verify_pkt_auth_seq_num (bs, seq_num,
1682 bfd_auth_type_is_meticulous
1683 (auth_key->auth_type))
1684 && bfd_verify_pkt_auth_key_sha1 (pkt, pkt_size, bs, bfd_key_id,
1685 auth_key);
1686 }
1687 while (0);
1688#else
Damjan Marion07a38572018-01-21 06:44:18 -08001689 vlib_log_err
1690 (bm->log_class,
1691 "internal error, attempt to use SHA1 without SSL support");
Klement Sekerab17dd962017-01-09 07:43:48 +01001692 return 0;
1693#endif
1694 }
1695 return 0;
1696}
1697
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001698/**
1699 * @brief verify bfd packet - authentication
1700 *
1701 * @param pkt
1702 *
1703 * @return 1 if bfd packet is valid
1704 */
1705int
Klement Sekerab17dd962017-01-09 07:43:48 +01001706bfd_verify_pkt_auth (const bfd_pkt_t * pkt, u16 pkt_size, bfd_session_t * bs)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001707{
Klement Sekerab17dd962017-01-09 07:43:48 +01001708 if (bfd_pkt_get_auth_present (pkt))
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001709 {
Klement Sekerab17dd962017-01-09 07:43:48 +01001710 /* authentication present in packet */
1711 if (!bs->auth.curr_key)
1712 {
1713 /* currently not using authentication - can we turn it on? */
1714 if (bs->auth.is_delayed && bs->auth.next_key)
1715 {
1716 /* yes, switch is scheduled - make sure the auth is valid */
1717 if (bfd_verify_pkt_auth_key (pkt, pkt_size, bs,
1718 bs->auth.next_bfd_key_id,
1719 bs->auth.next_key))
1720 {
1721 /* auth matches next key, do the switch, packet is valid */
1722 bfd_session_switch_auth_to_next (bs);
1723 return 1;
1724 }
1725 }
1726 }
1727 else
1728 {
1729 /* yes, using authentication, verify the key */
1730 if (bfd_verify_pkt_auth_key (pkt, pkt_size, bs,
1731 bs->auth.curr_bfd_key_id,
1732 bs->auth.curr_key))
1733 {
1734 /* verification passed, packet is valid */
1735 return 1;
1736 }
1737 else
1738 {
1739 /* verification failed - but maybe we need to switch key */
1740 if (bs->auth.is_delayed && bs->auth.next_key)
1741 {
1742 /* delayed switch present, verify if that key works */
1743 if (bfd_verify_pkt_auth_key (pkt, pkt_size, bs,
1744 bs->auth.next_bfd_key_id,
1745 bs->auth.next_key))
1746 {
1747 /* auth matches next key, switch key, packet is valid */
1748 bfd_session_switch_auth_to_next (bs);
1749 return 1;
1750 }
1751 }
1752 }
1753 }
1754 }
1755 else
1756 {
1757 /* authentication in packet not present */
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001758 if (pkt_size > sizeof (*pkt))
1759 {
1760 BFD_ERR ("BFD verification failed - unexpected packet size '%d' "
1761 "(auth not present)", pkt_size);
1762 return 0;
1763 }
Klement Sekerab17dd962017-01-09 07:43:48 +01001764 if (bs->auth.curr_key)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001765 {
Klement Sekerab17dd962017-01-09 07:43:48 +01001766 /* currently authenticating - could we turn it off? */
1767 if (bs->auth.is_delayed && !bs->auth.next_key)
1768 {
1769 /* yes, delayed switch to NULL key is scheduled */
1770 bfd_session_switch_auth_to_next (bs);
1771 return 1;
1772 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001773 }
Klement Sekerab17dd962017-01-09 07:43:48 +01001774 else
1775 {
1776 /* no auth in packet, no auth in use - packet is valid */
1777 return 1;
1778 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001779 }
Klement Sekerab17dd962017-01-09 07:43:48 +01001780 return 0;
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001781}
1782
1783void
1784bfd_consume_pkt (bfd_main_t * bm, const bfd_pkt_t * pkt, u32 bs_idx)
1785{
Dave Barach1e3417f2018-07-25 08:30:27 -04001786 bfd_lock_check (bm);
1787
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001788 bfd_session_t *bs = bfd_find_session_by_idx (bm, bs_idx);
Klement Sekera0e2e0df2017-03-03 08:51:08 +01001789 if (!bs || (pkt->your_disc && pkt->your_disc != bs->local_discr))
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001790 {
1791 return;
1792 }
1793 BFD_DBG ("Scanning bfd packet, bs_idx=%d", bs->bs_idx);
1794 bs->remote_discr = pkt->my_disc;
1795 bs->remote_state = bfd_pkt_get_state (pkt);
1796 bs->remote_demand = bfd_pkt_get_demand (pkt);
Klement Sekera73884482017-02-23 09:26:30 +01001797 bs->remote_diag = bfd_pkt_get_diag_code (pkt);
Klement Sekera637b9c42016-12-08 05:19:14 +01001798 u64 now = clib_cpu_time_now ();
1799 bs->last_rx_clocks = now;
Klement Sekerab17dd962017-01-09 07:43:48 +01001800 if (bfd_pkt_get_auth_present (pkt))
1801 {
1802 bfd_auth_type_e auth_type =
1803 ((bfd_pkt_with_common_auth_t *) (pkt))->common_auth.type;
1804 switch (auth_type)
1805 {
1806 case BFD_AUTH_TYPE_reserved:
1807 /* fallthrough */
1808 case BFD_AUTH_TYPE_simple_password:
1809 /* fallthrough */
1810 case BFD_AUTH_TYPE_keyed_md5:
1811 /* fallthrough */
1812 case BFD_AUTH_TYPE_meticulous_keyed_md5:
Damjan Marion07a38572018-01-21 06:44:18 -08001813 vlib_log_crit (bm->log_class,
1814 "internal error, unexpected auth_type=%d:%s",
1815 auth_type, bfd_auth_type_str (auth_type));
Klement Sekerab17dd962017-01-09 07:43:48 +01001816 break;
1817 case BFD_AUTH_TYPE_keyed_sha1:
1818 /* fallthrough */
1819 case BFD_AUTH_TYPE_meticulous_keyed_sha1:
1820 do
1821 {
1822 bfd_pkt_with_sha1_auth_t *with_sha1 =
1823 (bfd_pkt_with_sha1_auth_t *) pkt;
1824 bs->auth.remote_seq_number =
1825 clib_net_to_host_u32 (with_sha1->sha1_auth.seq_num);
1826 bs->auth.remote_seq_number_known = 1;
1827 BFD_DBG ("Received sequence number %u",
1828 bs->auth.remote_seq_number);
1829 }
1830 while (0);
1831 }
1832 }
Klement Sekeraa57a9702017-02-02 06:58:07 +01001833 bs->remote_desired_min_tx_clocks =
1834 bfd_usec_to_clocks (bm, clib_net_to_host_u32 (pkt->des_min_tx));
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001835 bs->remote_detect_mult = pkt->head.detect_mult;
Klement Sekera637b9c42016-12-08 05:19:14 +01001836 bfd_set_remote_required_min_rx (bm, bs, now,
Klement Sekera239790f2017-02-16 10:53:53 +01001837 clib_net_to_host_u32 (pkt->req_min_rx));
1838 bfd_set_remote_required_min_echo_rx (bm, bs, now,
1839 clib_net_to_host_u32
1840 (pkt->req_min_echo_rx));
Klement Sekera239790f2017-02-16 10:53:53 +01001841 if (bfd_pkt_get_final (pkt))
Klement Sekeraa57a9702017-02-02 06:58:07 +01001842 {
Klement Sekera239790f2017-02-16 10:53:53 +01001843 if (BFD_POLL_IN_PROGRESS == bs->poll_state)
Klement Sekeraa57a9702017-02-02 06:58:07 +01001844 {
Klement Sekera239790f2017-02-16 10:53:53 +01001845 BFD_DBG ("Poll sequence terminated, bs_idx=%u", bs->bs_idx);
1846 bfd_set_poll_state (bs, BFD_POLL_NOT_NEEDED);
1847 if (BFD_STATE_up == bs->local_state)
1848 {
Klement Sekera73884482017-02-23 09:26:30 +01001849 bfd_set_effective_required_min_rx (bm, bs,
Klement Sekera239790f2017-02-16 10:53:53 +01001850 clib_max (bs->echo *
1851 bm->min_required_min_rx_while_echo_clocks,
1852 bs->config_required_min_rx_clocks));
1853 }
1854 }
1855 else if (BFD_POLL_IN_PROGRESS_AND_QUEUED == bs->poll_state)
1856 {
1857 /*
1858 * next poll sequence must be delayed by at least the round trip
1859 * time, so calculate that here
1860 */
1861 BFD_DBG ("Next poll sequence can commence in " BFD_CLK_FMT,
1862 BFD_CLK_PRN (now -
1863 bs->poll_state_start_or_timeout_clocks));
1864 bs->poll_state_start_or_timeout_clocks =
1865 now + (now - bs->poll_state_start_or_timeout_clocks);
1866 BFD_DBG
1867 ("Poll sequence terminated, but another is needed, bs_idx=%u",
1868 bs->bs_idx);
1869 bfd_set_poll_state (bs, BFD_POLL_NEEDED);
Klement Sekeraa57a9702017-02-02 06:58:07 +01001870 }
1871 }
Klement Sekera239790f2017-02-16 10:53:53 +01001872 bfd_calc_next_tx (bm, bs, now);
1873 bfd_set_timer (bm, bs, now, 0);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001874 if (BFD_STATE_admin_down == bs->local_state)
Klement Sekerac48829b2017-02-14 07:55:57 +01001875 {
1876 BFD_DBG ("Session is admin-down, ignoring packet, bs_idx=%u",
1877 bs->bs_idx);
1878 return;
1879 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001880 if (BFD_STATE_admin_down == bs->remote_state)
1881 {
1882 bfd_set_diag (bs, BFD_DIAG_CODE_neighbor_sig_down);
Klement Sekerae4504c62016-12-08 10:16:41 +01001883 bfd_set_state (bm, bs, BFD_STATE_down, 0);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001884 }
1885 else if (BFD_STATE_down == bs->local_state)
1886 {
1887 if (BFD_STATE_down == bs->remote_state)
1888 {
Klement Sekerae50e8562017-04-04 16:19:48 +02001889 bfd_set_diag (bs, BFD_DIAG_CODE_no_diag);
Klement Sekerae4504c62016-12-08 10:16:41 +01001890 bfd_set_state (bm, bs, BFD_STATE_init, 0);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001891 }
1892 else if (BFD_STATE_init == bs->remote_state)
1893 {
Klement Sekerae50e8562017-04-04 16:19:48 +02001894 bfd_set_diag (bs, BFD_DIAG_CODE_no_diag);
Klement Sekerae4504c62016-12-08 10:16:41 +01001895 bfd_set_state (bm, bs, BFD_STATE_up, 0);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001896 }
1897 }
1898 else if (BFD_STATE_init == bs->local_state)
1899 {
1900 if (BFD_STATE_up == bs->remote_state ||
1901 BFD_STATE_init == bs->remote_state)
1902 {
Klement Sekerae50e8562017-04-04 16:19:48 +02001903 bfd_set_diag (bs, BFD_DIAG_CODE_no_diag);
Klement Sekerae4504c62016-12-08 10:16:41 +01001904 bfd_set_state (bm, bs, BFD_STATE_up, 0);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001905 }
1906 }
1907 else /* BFD_STATE_up == bs->local_state */
1908 {
1909 if (BFD_STATE_down == bs->remote_state)
1910 {
1911 bfd_set_diag (bs, BFD_DIAG_CODE_neighbor_sig_down);
Klement Sekerae4504c62016-12-08 10:16:41 +01001912 bfd_set_state (bm, bs, BFD_STATE_down, 0);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001913 }
1914 }
1915}
1916
Klement Sekera239790f2017-02-16 10:53:53 +01001917int
1918bfd_consume_echo_pkt (bfd_main_t * bm, vlib_buffer_t * b)
Klement Sekeraa57a9702017-02-02 06:58:07 +01001919{
Klement Sekera239790f2017-02-16 10:53:53 +01001920 bfd_echo_pkt_t *pkt = NULL;
1921 if (b->current_length != sizeof (*pkt))
Klement Sekeraa57a9702017-02-02 06:58:07 +01001922 {
Klement Sekera239790f2017-02-16 10:53:53 +01001923 return 0;
Klement Sekeraa57a9702017-02-02 06:58:07 +01001924 }
Klement Sekera239790f2017-02-16 10:53:53 +01001925 pkt = vlib_buffer_get_current (b);
1926 bfd_session_t *bs = bfd_find_session_by_disc (bm, pkt->discriminator);
1927 if (!bs)
1928 {
1929 return 0;
1930 }
1931 BFD_DBG ("Scanning bfd echo packet, bs_idx=%d", bs->bs_idx);
1932 u64 checksum =
1933 bfd_calc_echo_checksum (bs->local_discr, pkt->expire_time_clocks,
1934 bs->echo_secret);
1935 if (checksum != pkt->checksum)
1936 {
1937 BFD_DBG ("Invalid echo packet, checksum mismatch");
1938 return 1;
1939 }
1940 u64 now = clib_cpu_time_now ();
1941 if (pkt->expire_time_clocks < now)
1942 {
1943 BFD_DBG ("Stale packet received, expire time %lu < now %lu",
1944 pkt->expire_time_clocks, now);
1945 }
1946 else
1947 {
1948 bs->echo_last_rx_clocks = now;
1949 }
1950 return 1;
Klement Sekeraa57a9702017-02-02 06:58:07 +01001951}
1952
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001953u8 *
1954format_bfd_session (u8 * s, va_list * args)
1955{
1956 const bfd_session_t *bs = va_arg (*args, bfd_session_t *);
Damjan Marion07a38572018-01-21 06:44:18 -08001957 u32 indent = format_get_indent (s) + vlib_log_get_indent ();
Klement Sekera239790f2017-02-16 10:53:53 +01001958 s = format (s, "bs_idx=%u local-state=%s remote-state=%s\n"
1959 "%Ulocal-discriminator=%u remote-discriminator=%u\n"
1960 "%Ulocal-diag=%s echo-active=%s\n"
1961 "%Udesired-min-tx=%u required-min-rx=%u\n"
1962 "%Urequired-min-echo-rx=%u detect-mult=%u\n"
1963 "%Uremote-min-rx=%u remote-min-echo-rx=%u\n"
1964 "%Uremote-demand=%s poll-state=%s\n"
1965 "%Uauth: local-seq-num=%u remote-seq-num=%u\n"
1966 "%U is-delayed=%s\n"
1967 "%U curr-key=%U\n"
1968 "%U next-key=%U",
Klement Sekerab17dd962017-01-09 07:43:48 +01001969 bs->bs_idx, bfd_state_string (bs->local_state),
Klement Sekera239790f2017-02-16 10:53:53 +01001970 bfd_state_string (bs->remote_state), format_white_space, indent,
1971 bs->local_discr, bs->remote_discr, format_white_space, indent,
1972 bfd_diag_code_string (bs->local_diag),
1973 (bs->echo ? "yes" : "no"), format_white_space, indent,
Klement Sekeraa57a9702017-02-02 06:58:07 +01001974 bs->config_desired_min_tx_usec, bs->config_required_min_rx_usec,
Klement Sekera239790f2017-02-16 10:53:53 +01001975 format_white_space, indent, 1, bs->local_detect_mult,
1976 format_white_space, indent, bs->remote_min_rx_usec,
1977 bs->remote_min_echo_rx_usec, format_white_space, indent,
1978 (bs->remote_demand ? "yes" : "no"),
1979 bfd_poll_state_string (bs->poll_state), format_white_space,
1980 indent, bs->auth.local_seq_number, bs->auth.remote_seq_number,
1981 format_white_space, indent,
1982 (bs->auth.is_delayed ? "yes" : "no"), format_white_space,
1983 indent, format_bfd_auth_key, bs->auth.curr_key,
1984 format_white_space, indent, format_bfd_auth_key,
1985 bs->auth.next_key);
Klement Sekerab17dd962017-01-09 07:43:48 +01001986 return s;
1987}
1988
Damjan Marion07a38572018-01-21 06:44:18 -08001989u8 *
1990format_bfd_session_brief (u8 * s, va_list * args)
1991{
1992 const bfd_session_t *bs = va_arg (*args, bfd_session_t *);
1993 s =
1994 format (s, "bs_idx=%u local-state=%s remote-state=%s", bs->bs_idx,
1995 bfd_state_string (bs->local_state),
1996 bfd_state_string (bs->remote_state));
1997 return s;
1998}
1999
Klement Sekerab17dd962017-01-09 07:43:48 +01002000unsigned
2001bfd_auth_type_supported (bfd_auth_type_e auth_type)
2002{
2003 if (auth_type == BFD_AUTH_TYPE_keyed_sha1 ||
2004 auth_type == BFD_AUTH_TYPE_meticulous_keyed_sha1)
2005 {
2006 return 1;
2007 }
2008 return 0;
2009}
2010
2011vnet_api_error_t
2012bfd_auth_activate (bfd_session_t * bs, u32 conf_key_id,
2013 u8 bfd_key_id, u8 is_delayed)
2014{
2015 bfd_main_t *bm = &bfd_main;
2016 const uword *key_idx_p =
2017 hash_get (bm->auth_key_by_conf_key_id, conf_key_id);
2018 if (!key_idx_p)
2019 {
Damjan Marion07a38572018-01-21 06:44:18 -08002020 vlib_log_err (bm->log_class,
2021 "authentication key with config ID %u doesn't exist)",
Klement Sekerab17dd962017-01-09 07:43:48 +01002022 conf_key_id);
2023 return VNET_API_ERROR_BFD_ENOENT;
2024 }
2025 const uword key_idx = *key_idx_p;
2026 bfd_auth_key_t *key = pool_elt_at_index (bm->auth_keys, key_idx);
2027 if (is_delayed)
2028 {
2029 if (bs->auth.next_key == key)
2030 {
2031 /* already using this key, no changes required */
2032 return 0;
2033 }
2034 bs->auth.next_key = key;
2035 bs->auth.next_bfd_key_id = bfd_key_id;
2036 bs->auth.is_delayed = 1;
2037 }
2038 else
2039 {
2040 if (bs->auth.curr_key == key)
2041 {
2042 /* already using this key, no changes required */
2043 return 0;
2044 }
2045 if (bs->auth.curr_key)
2046 {
2047 --bs->auth.curr_key->use_count;
2048 }
2049 bs->auth.curr_key = key;
2050 bs->auth.curr_bfd_key_id = bfd_key_id;
2051 bs->auth.is_delayed = 0;
2052 }
2053 ++key->use_count;
Klement Sekera239790f2017-02-16 10:53:53 +01002054 BFD_DBG ("\nSession auth modified: %U", format_bfd_session, bs);
Damjan Marion07a38572018-01-21 06:44:18 -08002055 vlib_log_info (bm->log_class, "session auth modified: %U",
2056 format_bfd_session_brief, bs);
Klement Sekerab17dd962017-01-09 07:43:48 +01002057 return 0;
2058}
2059
2060vnet_api_error_t
2061bfd_auth_deactivate (bfd_session_t * bs, u8 is_delayed)
2062{
Damjan Marion07a38572018-01-21 06:44:18 -08002063 bfd_main_t *bm = &bfd_main;
Klement Sekerab17dd962017-01-09 07:43:48 +01002064#if WITH_LIBSSL > 0
2065 if (!is_delayed)
2066 {
2067 /* not delayed - deactivate the current key right now */
2068 if (bs->auth.curr_key)
2069 {
2070 --bs->auth.curr_key->use_count;
2071 bs->auth.curr_key = NULL;
2072 }
2073 bs->auth.is_delayed = 0;
2074 }
2075 else
2076 {
2077 /* delayed - mark as so */
2078 bs->auth.is_delayed = 1;
2079 }
2080 /*
2081 * clear the next key unconditionally - either the auth change is not delayed
2082 * in which case the caller expects the session to not use authentication
2083 * from this point forward, or it is delayed, in which case the next_key
2084 * needs to be set to NULL to make it so in the future
2085 */
2086 if (bs->auth.next_key)
2087 {
2088 --bs->auth.next_key->use_count;
2089 bs->auth.next_key = NULL;
2090 }
Klement Sekera239790f2017-02-16 10:53:53 +01002091 BFD_DBG ("\nSession auth modified: %U", format_bfd_session, bs);
Damjan Marion07a38572018-01-21 06:44:18 -08002092 vlib_log_info (bm->log_class, "session auth modified: %U",
2093 format_bfd_session_brief, bs);
Klement Sekerab17dd962017-01-09 07:43:48 +01002094 return 0;
2095#else
Damjan Marion07a38572018-01-21 06:44:18 -08002096 vlib_log_err (bm->log_class,
2097 "SSL missing, cannot deactivate BFD authentication");
Klement Sekerab17dd962017-01-09 07:43:48 +01002098 return VNET_API_ERROR_BFD_NOTSUPP;
2099#endif
Klement Sekera0e3c0de2016-09-29 14:43:44 +02002100}
2101
Klement Sekeraa57a9702017-02-02 06:58:07 +01002102vnet_api_error_t
2103bfd_session_set_params (bfd_main_t * bm, bfd_session_t * bs,
2104 u32 desired_min_tx_usec,
2105 u32 required_min_rx_usec, u8 detect_mult)
2106{
2107 if (bs->local_detect_mult != detect_mult ||
2108 bs->config_desired_min_tx_usec != desired_min_tx_usec ||
2109 bs->config_required_min_rx_usec != required_min_rx_usec)
2110 {
Klement Sekera239790f2017-02-16 10:53:53 +01002111 BFD_DBG ("\nChanging session params: %U", format_bfd_session, bs);
Klement Sekeraa57a9702017-02-02 06:58:07 +01002112 switch (bs->poll_state)
2113 {
Klement Sekera239790f2017-02-16 10:53:53 +01002114 case BFD_POLL_NOT_NEEDED:
Klement Sekeraa57a9702017-02-02 06:58:07 +01002115 if (BFD_STATE_up == bs->local_state ||
2116 BFD_STATE_init == bs->local_state)
2117 {
2118 /* poll sequence is not needed for detect multiplier change */
2119 if (bs->config_desired_min_tx_usec != desired_min_tx_usec ||
2120 bs->config_required_min_rx_usec != required_min_rx_usec)
2121 {
Klement Sekera239790f2017-02-16 10:53:53 +01002122 bfd_set_poll_state (bs, BFD_POLL_NEEDED);
Klement Sekeraa57a9702017-02-02 06:58:07 +01002123 }
2124 }
2125 break;
Klement Sekera239790f2017-02-16 10:53:53 +01002126 case BFD_POLL_NEEDED:
2127 case BFD_POLL_IN_PROGRESS_AND_QUEUED:
2128 /*
2129 * nothing to do - will be handled in the future poll which is
2130 * already scheduled for execution
2131 */
Klement Sekeraa57a9702017-02-02 06:58:07 +01002132 break;
Klement Sekera239790f2017-02-16 10:53:53 +01002133 case BFD_POLL_IN_PROGRESS:
2134 /* poll sequence is not needed for detect multiplier change */
2135 if (bs->config_desired_min_tx_usec != desired_min_tx_usec ||
2136 bs->config_required_min_rx_usec != required_min_rx_usec)
2137 {
2138 BFD_DBG ("Poll in progress, queueing extra poll, bs_idx=%u",
2139 bs->bs_idx);
2140 bfd_set_poll_state (bs, BFD_POLL_IN_PROGRESS_AND_QUEUED);
2141 }
Klement Sekeraa57a9702017-02-02 06:58:07 +01002142 }
2143
2144 bs->local_detect_mult = detect_mult;
2145 bs->config_desired_min_tx_usec = desired_min_tx_usec;
2146 bs->config_desired_min_tx_clocks =
2147 bfd_usec_to_clocks (bm, desired_min_tx_usec);
2148 bs->config_required_min_rx_usec = required_min_rx_usec;
2149 bs->config_required_min_rx_clocks =
2150 bfd_usec_to_clocks (bm, required_min_rx_usec);
Klement Sekera239790f2017-02-16 10:53:53 +01002151 BFD_DBG ("\nChanged session params: %U", format_bfd_session, bs);
Klement Sekeraa57a9702017-02-02 06:58:07 +01002152
Damjan Marion07a38572018-01-21 06:44:18 -08002153 vlib_log_info (bm->log_class, "changed session params: %U",
2154 format_bfd_session_brief, bs);
Klement Sekeraa57a9702017-02-02 06:58:07 +01002155 vlib_process_signal_event (bm->vlib_main, bm->bfd_process_node_index,
2156 BFD_EVENT_CONFIG_CHANGED, bs->bs_idx);
2157 }
2158 else
2159 {
2160 BFD_DBG ("Ignore parameter change - no change, bs_idx=%u", bs->bs_idx);
2161 }
2162 return 0;
2163}
2164
Klement Sekera73884482017-02-23 09:26:30 +01002165vnet_api_error_t
2166bfd_auth_set_key (u32 conf_key_id, u8 auth_type, u8 key_len,
2167 const u8 * key_data)
2168{
Damjan Marion07a38572018-01-21 06:44:18 -08002169 bfd_main_t *bm = &bfd_main;
Klement Sekera73884482017-02-23 09:26:30 +01002170#if WITH_LIBSSL > 0
2171 bfd_auth_key_t *auth_key = NULL;
Klement Sekerab16bfe32017-02-28 11:56:48 +01002172 if (!key_len || key_len > bfd_max_key_len_for_auth_type (auth_type))
Klement Sekera73884482017-02-23 09:26:30 +01002173 {
Damjan Marion07a38572018-01-21 06:44:18 -08002174 vlib_log_err (bm->log_class,
2175 "invalid authentication key length for auth_type=%d:%s "
2176 "(key_len=%u, must be non-zero, expected max=%u)",
Klement Sekera73884482017-02-23 09:26:30 +01002177 auth_type, bfd_auth_type_str (auth_type), key_len,
Klement Sekerab16bfe32017-02-28 11:56:48 +01002178 (u32) bfd_max_key_len_for_auth_type (auth_type));
Klement Sekera73884482017-02-23 09:26:30 +01002179 return VNET_API_ERROR_INVALID_VALUE;
2180 }
2181 if (!bfd_auth_type_supported (auth_type))
2182 {
Damjan Marion07a38572018-01-21 06:44:18 -08002183 vlib_log_err (bm->log_class, "unsupported auth type=%d:%s", auth_type,
Klement Sekera73884482017-02-23 09:26:30 +01002184 bfd_auth_type_str (auth_type));
2185 return VNET_API_ERROR_BFD_NOTSUPP;
2186 }
Klement Sekera73884482017-02-23 09:26:30 +01002187 uword *key_idx_p = hash_get (bm->auth_key_by_conf_key_id, conf_key_id);
2188 if (key_idx_p)
2189 {
2190 /* modifying existing key - must not be used */
2191 const uword key_idx = *key_idx_p;
2192 auth_key = pool_elt_at_index (bm->auth_keys, key_idx);
2193 if (auth_key->use_count > 0)
2194 {
Damjan Marion07a38572018-01-21 06:44:18 -08002195 vlib_log_err (bm->log_class,
2196 "authentication key with conf ID %u in use by %u BFD "
2197 "session(s) - cannot modify", conf_key_id,
2198 auth_key->use_count);
Klement Sekera73884482017-02-23 09:26:30 +01002199 return VNET_API_ERROR_BFD_EINUSE;
2200 }
2201 }
2202 else
2203 {
2204 /* adding new key */
2205 pool_get (bm->auth_keys, auth_key);
2206 auth_key->conf_key_id = conf_key_id;
2207 hash_set (bm->auth_key_by_conf_key_id, conf_key_id,
2208 auth_key - bm->auth_keys);
2209 }
2210 auth_key->auth_type = auth_type;
2211 memset (auth_key->key, 0, sizeof (auth_key->key));
2212 clib_memcpy (auth_key->key, key_data, key_len);
2213 return 0;
2214#else
Damjan Marion07a38572018-01-21 06:44:18 -08002215 vlib_log_err (bm->log_class,
2216 "SSL missing, cannot manipulate authentication keys");
Klement Sekera73884482017-02-23 09:26:30 +01002217 return VNET_API_ERROR_BFD_NOTSUPP;
2218#endif
2219}
2220
2221vnet_api_error_t
2222bfd_auth_del_key (u32 conf_key_id)
2223{
2224#if WITH_LIBSSL > 0
2225 bfd_auth_key_t *auth_key = NULL;
2226 bfd_main_t *bm = &bfd_main;
2227 uword *key_idx_p = hash_get (bm->auth_key_by_conf_key_id, conf_key_id);
2228 if (key_idx_p)
2229 {
2230 /* deleting existing key - must not be used */
2231 const uword key_idx = *key_idx_p;
2232 auth_key = pool_elt_at_index (bm->auth_keys, key_idx);
2233 if (auth_key->use_count > 0)
2234 {
Damjan Marion07a38572018-01-21 06:44:18 -08002235 vlib_log_err (bm->log_class,
2236 "authentication key with conf ID %u in use by %u BFD "
2237 "session(s) - cannot delete", conf_key_id,
2238 auth_key->use_count);
Klement Sekera73884482017-02-23 09:26:30 +01002239 return VNET_API_ERROR_BFD_EINUSE;
2240 }
2241 hash_unset (bm->auth_key_by_conf_key_id, conf_key_id);
2242 memset (auth_key, 0, sizeof (*auth_key));
2243 pool_put (bm->auth_keys, auth_key);
2244 }
2245 else
2246 {
2247 /* no such key */
Damjan Marion07a38572018-01-21 06:44:18 -08002248 vlib_log_err (bm->log_class,
2249 "authentication key with conf ID %u does not exist",
Klement Sekera73884482017-02-23 09:26:30 +01002250 conf_key_id);
2251 return VNET_API_ERROR_BFD_ENOENT;
2252 }
2253 return 0;
2254#else
Damjan Marion07a38572018-01-21 06:44:18 -08002255 vlib_log_err (bm->log_class,
2256 "SSL missing, cannot manipulate authentication keys");
Klement Sekera73884482017-02-23 09:26:30 +01002257 return VNET_API_ERROR_BFD_NOTSUPP;
2258#endif
2259}
2260
Klement Sekera0e3c0de2016-09-29 14:43:44 +02002261bfd_main_t bfd_main;
2262
2263/*
2264 * fd.io coding-style-patch-verification: ON
2265 *
2266 * Local Variables:
2267 * eval: (c-set-style "gnu")
2268 * End:
2269 */