blob: ea6db1f9d2754291982410927144bb3cceb26e3a [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
Klement Sekera0e3c0de2016-09-29 14:43:44 +020028#include <vppinfra/random.h>
29#include <vppinfra/error.h>
30#include <vppinfra/hash.h>
Klement Sekera239790f2017-02-16 10:53:53 +010031#include <vppinfra/xxhash.h>
Klement Sekera0e3c0de2016-09-29 14:43:44 +020032#include <vnet/ethernet/ethernet.h>
33#include <vnet/ethernet/packet.h>
34#include <vnet/bfd/bfd_debug.h>
35#include <vnet/bfd/bfd_protocol.h>
36#include <vnet/bfd/bfd_main.h>
Klement Sekera239790f2017-02-16 10:53:53 +010037
38static u64
39bfd_calc_echo_checksum (u32 discriminator, u64 expire_time, u32 secret)
40{
41 u64 checksum = 0;
42#if __SSE4_2__
43 checksum = _mm_crc32_u64 (0, discriminator);
44 checksum = _mm_crc32_u64 (checksum, expire_time);
45 checksum = _mm_crc32_u64 (checksum, secret);
46#else
47 checksum = clib_xxhash (discriminator ^ expire_time ^ secret);
Klement Sekerab17dd962017-01-09 07:43:48 +010048#endif
Klement Sekera239790f2017-02-16 10:53:53 +010049 return checksum;
50}
Klement Sekera0e3c0de2016-09-29 14:43:44 +020051
52static u64
Klement Sekeraa57a9702017-02-02 06:58:07 +010053bfd_usec_to_clocks (const bfd_main_t * bm, u64 us)
Klement Sekera0e3c0de2016-09-29 14:43:44 +020054{
55 return bm->cpu_cps * ((f64) us / USEC_PER_SECOND);
56}
57
Klement Sekera73884482017-02-23 09:26:30 +010058u32
Klement Sekera239790f2017-02-16 10:53:53 +010059bfd_clocks_to_usec (const bfd_main_t * bm, u64 clocks)
60{
61 return (clocks / bm->cpu_cps) * USEC_PER_SECOND;
62}
63
Klement Sekera0e3c0de2016-09-29 14:43:44 +020064static vlib_node_registration_t bfd_process_node;
65
Klement Sekera46a87ad2017-01-02 08:22:23 +010066/* set to 0 here, real values filled at startup */
67static u32 bfd_node_index_by_transport[] = {
68#define F(t, n) [BFD_TRANSPORT_##t] = 0,
Klement Sekera0e3c0de2016-09-29 14:43:44 +020069 foreach_bfd_transport (F)
70#undef F
71};
72
Klement Sekera73884482017-02-23 09:26:30 +010073u8 *
Klement Sekerab17dd962017-01-09 07:43:48 +010074format_bfd_auth_key (u8 * s, va_list * args)
75{
76 const bfd_auth_key_t *key = va_arg (*args, bfd_auth_key_t *);
77 if (key)
78 {
79 s = format (s, "{auth-type=%u:%s, conf-key-id=%u, use-count=%u}, ",
80 key->auth_type, bfd_auth_type_str (key->auth_type),
81 key->conf_key_id, key->use_count);
82 }
83 else
84 {
85 s = format (s, "{none}");
86 }
87 return s;
88}
89
Klement Sekera0e3c0de2016-09-29 14:43:44 +020090/*
91 * We actually send all bfd pkts to the "error" node after scanning
92 * them, so the graph node has only one next-index. The "error-drop"
93 * node automatically bumps our per-node packet counters for us.
94 */
95typedef enum
96{
97 BFD_INPUT_NEXT_NORMAL,
98 BFD_INPUT_N_NEXT,
99} bfd_input_next_t;
100
Klement Sekerae4504c62016-12-08 10:16:41 +0100101static void bfd_on_state_change (bfd_main_t * bm, bfd_session_t * bs, u64 now,
102 int handling_wakeup);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200103
104static void
105bfd_set_defaults (bfd_main_t * bm, bfd_session_t * bs)
106{
107 bs->local_state = BFD_STATE_down;
108 bs->local_diag = BFD_DIAG_CODE_no_diag;
109 bs->remote_state = BFD_STATE_down;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200110 bs->remote_discr = 0;
Klement Sekera239790f2017-02-16 10:53:53 +0100111 bs->config_desired_min_tx_usec = BFD_DEFAULT_DESIRED_MIN_TX_USEC;
Klement Sekeraa57a9702017-02-02 06:58:07 +0100112 bs->config_desired_min_tx_clocks = bm->default_desired_min_tx_clocks;
113 bs->effective_desired_min_tx_clocks = bm->default_desired_min_tx_clocks;
114 bs->remote_min_rx_usec = 1;
115 bs->remote_min_rx_clocks = bfd_usec_to_clocks (bm, bs->remote_min_rx_usec);
Klement Sekera239790f2017-02-16 10:53:53 +0100116 bs->remote_min_echo_rx_usec = 0;
117 bs->remote_min_echo_rx_clocks = 0;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200118 bs->remote_demand = 0;
Klement Sekerab17dd962017-01-09 07:43:48 +0100119 bs->auth.remote_seq_number = 0;
120 bs->auth.remote_seq_number_known = 0;
121 bs->auth.local_seq_number = random_u32 (&bm->random_seed);
Klement Sekera239790f2017-02-16 10:53:53 +0100122 bs->echo_secret = random_u32 (&bm->random_seed);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200123}
124
125static void
126bfd_set_diag (bfd_session_t * bs, bfd_diag_code_e code)
127{
128 if (bs->local_diag != code)
129 {
130 BFD_DBG ("set local_diag, bs_idx=%d: '%d:%s'", bs->bs_idx, code,
131 bfd_diag_code_string (code));
132 bs->local_diag = code;
133 }
134}
135
136static void
Klement Sekerae4504c62016-12-08 10:16:41 +0100137bfd_set_state (bfd_main_t * bm, bfd_session_t * bs,
138 bfd_state_e new_state, int handling_wakeup)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200139{
140 if (bs->local_state != new_state)
141 {
142 BFD_DBG ("Change state, bs_idx=%d: %s->%s", bs->bs_idx,
143 bfd_state_string (bs->local_state),
144 bfd_state_string (new_state));
145 bs->local_state = new_state;
Klement Sekerae4504c62016-12-08 10:16:41 +0100146 bfd_on_state_change (bm, bs, clib_cpu_time_now (), handling_wakeup);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200147 }
148}
149
Klement Sekera73884482017-02-23 09:26:30 +0100150const char *
Klement Sekera239790f2017-02-16 10:53:53 +0100151bfd_poll_state_string (bfd_poll_state_e state)
152{
153 switch (state)
154 {
155#define F(x) \
156 case BFD_POLL_##x: \
157 return "BFD_POLL_" #x;
158 foreach_bfd_poll_state (F)
159#undef F
160 }
161 return "UNKNOWN";
162}
163
164static void
165bfd_set_poll_state (bfd_session_t * bs, bfd_poll_state_e state)
166{
167 if (bs->poll_state != state)
168 {
169 BFD_DBG ("Setting poll state=%s, bs_idx=%u",
170 bfd_poll_state_string (state), bs->bs_idx);
171 bs->poll_state = state;
172 }
173}
174
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200175static void
176bfd_recalc_tx_interval (bfd_main_t * bm, bfd_session_t * bs)
177{
Klement Sekera239790f2017-02-16 10:53:53 +0100178 bs->transmit_interval_clocks =
179 clib_max (bs->effective_desired_min_tx_clocks, bs->remote_min_rx_clocks);
180 BFD_DBG ("Recalculated transmit interval " BFD_CLK_FMT,
181 BFD_CLK_PRN (bs->transmit_interval_clocks));
182}
183
184static void
185bfd_recalc_echo_tx_interval (bfd_main_t * bm, bfd_session_t * bs)
186{
187 bs->echo_transmit_interval_clocks =
188 clib_max (bs->effective_desired_min_tx_clocks,
189 bs->remote_min_echo_rx_clocks);
190 BFD_DBG ("Recalculated echo transmit interval " BFD_CLK_FMT,
191 BFD_CLK_PRN (bs->echo_transmit_interval_clocks));
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200192}
193
194static void
195bfd_calc_next_tx (bfd_main_t * bm, bfd_session_t * bs, u64 now)
196{
Klement Sekera239790f2017-02-16 10:53:53 +0100197 if (bs->local_detect_mult > 1)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200198 {
Klement Sekera239790f2017-02-16 10:53:53 +0100199 /* common case - 75-100% of transmit interval */
200 bs->tx_timeout_clocks = bs->last_tx_clocks +
201 (1 - .25 * (random_f64 (&bm->random_seed))) *
202 bs->transmit_interval_clocks;
203 if (bs->tx_timeout_clocks < now)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200204 {
Klement Sekera239790f2017-02-16 10:53:53 +0100205 /*
206 * the timeout is in the past, which means that either remote
207 * demand mode was set or performance/clock issues ...
208 */
209 BFD_DBG ("Missed %lu transmit events (now is %lu, calc "
210 "tx_timeout is %lu)",
211 (now - bs->tx_timeout_clocks) /
212 bs->transmit_interval_clocks, now, bs->tx_timeout_clocks);
213 bs->tx_timeout_clocks = now;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200214 }
215 }
216 else
217 {
Klement Sekera239790f2017-02-16 10:53:53 +0100218 /* special case - 75-90% of transmit interval */
219 bs->tx_timeout_clocks = bs->last_tx_clocks +
220 (.9 - .15 * (random_f64 (&bm->random_seed))) *
221 bs->transmit_interval_clocks;
222 if (bs->tx_timeout_clocks < now)
223 {
224 /*
225 * the timeout is in the past, which means that either remote
226 * demand mode was set or performance/clock issues ...
227 */
228 BFD_DBG ("Missed %lu transmit events (now is %lu, calc "
229 "tx_timeout is %lu)",
230 (now - bs->tx_timeout_clocks) /
231 bs->transmit_interval_clocks, now, bs->tx_timeout_clocks);
232 bs->tx_timeout_clocks = now;
233 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200234 }
235 if (bs->tx_timeout_clocks)
236 {
237 BFD_DBG ("Next transmit in %lu clocks/%.02fs@%lu",
238 bs->tx_timeout_clocks - now,
239 (bs->tx_timeout_clocks - now) / bm->cpu_cps,
240 bs->tx_timeout_clocks);
241 }
242}
243
244static void
Klement Sekera239790f2017-02-16 10:53:53 +0100245bfd_calc_next_echo_tx (bfd_main_t * bm, bfd_session_t * bs, u64 now)
246{
247 bs->echo_tx_timeout_clocks =
248 bs->echo_last_tx_clocks + bs->echo_transmit_interval_clocks;
249 if (bs->echo_tx_timeout_clocks < now)
250 {
251 /* huh, we've missed it already, transmit now */
252 BFD_DBG ("Missed %lu echo transmit events (now is %lu, calc tx_timeout "
253 "is %lu)",
254 (now - bs->echo_tx_timeout_clocks) /
255 bs->echo_transmit_interval_clocks,
256 now, bs->echo_tx_timeout_clocks);
257 bs->echo_tx_timeout_clocks = now;
258 }
259 BFD_DBG ("Next echo transmit in %lu clocks/%.02fs@%lu",
260 bs->echo_tx_timeout_clocks - now,
261 (bs->echo_tx_timeout_clocks - now) / bm->cpu_cps,
262 bs->echo_tx_timeout_clocks);
263}
264
265static void
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200266bfd_recalc_detection_time (bfd_main_t * bm, bfd_session_t * bs)
267{
Klement Sekera73884482017-02-23 09:26:30 +0100268 if (bs->local_state == BFD_STATE_init || bs->local_state == BFD_STATE_up)
269 {
270 bs->detection_time_clocks =
271 bs->remote_detect_mult *
272 clib_max (bs->effective_required_min_rx_clocks,
273 bs->remote_desired_min_tx_clocks);
274 BFD_DBG ("Recalculated detection time %lu clocks/%.2fs",
275 bs->detection_time_clocks,
276 bs->detection_time_clocks / bm->cpu_cps);
277 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200278}
279
280static void
Klement Sekerae4504c62016-12-08 10:16:41 +0100281bfd_set_timer (bfd_main_t * bm, bfd_session_t * bs, u64 now,
282 int handling_wakeup)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200283{
284 u64 next = 0;
285 u64 rx_timeout = 0;
Klement Sekera239790f2017-02-16 10:53:53 +0100286 u64 tx_timeout = 0;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200287 if (BFD_STATE_up == bs->local_state)
288 {
289 rx_timeout = bs->last_rx_clocks + bs->detection_time_clocks;
290 }
Klement Sekera73884482017-02-23 09:26:30 +0100291 if (BFD_STATE_up != bs->local_state ||
292 (!bs->remote_demand && bs->remote_min_rx_usec) ||
Klement Sekera239790f2017-02-16 10:53:53 +0100293 BFD_POLL_NOT_NEEDED != bs->poll_state)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200294 {
Klement Sekera239790f2017-02-16 10:53:53 +0100295 tx_timeout = bs->tx_timeout_clocks;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200296 }
Klement Sekera239790f2017-02-16 10:53:53 +0100297 if (tx_timeout && rx_timeout)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200298 {
Klement Sekera239790f2017-02-16 10:53:53 +0100299 next = clib_min (tx_timeout, rx_timeout);
300 }
301 else if (tx_timeout)
302 {
303 next = tx_timeout;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200304 }
305 else if (rx_timeout)
306 {
307 next = rx_timeout;
308 }
Klement Sekera239790f2017-02-16 10:53:53 +0100309 if (bs->echo && next > bs->echo_tx_timeout_clocks)
310 {
311 next = bs->echo_tx_timeout_clocks;
312 }
313 BFD_DBG ("bs_idx=%u, tx_timeout=%lu, echo_tx_timeout=%lu, rx_timeout=%lu, "
314 "next=%s",
315 bs->bs_idx, tx_timeout, bs->echo_tx_timeout_clocks, rx_timeout,
316 next == tx_timeout
317 ? "tx" : (next == bs->echo_tx_timeout_clocks ? "echo tx" : "rx"));
Klement Sekera637b9c42016-12-08 05:19:14 +0100318 /* sometimes the wheel expires an event a bit sooner than requested, account
319 for that here */
320 if (next && (now + bm->wheel_inaccuracy > bs->wheel_time_clocks ||
321 next < bs->wheel_time_clocks || !bs->wheel_time_clocks))
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200322 {
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200323 bs->wheel_time_clocks = next;
324 BFD_DBG ("timing_wheel_insert(%p, %lu (%ld clocks/%.2fs in the "
325 "future), %u);",
326 &bm->wheel, bs->wheel_time_clocks,
327 (i64) bs->wheel_time_clocks - clib_cpu_time_now (),
328 (i64) (bs->wheel_time_clocks - clib_cpu_time_now ()) /
329 bm->cpu_cps, bs->bs_idx);
330 timing_wheel_insert (&bm->wheel, bs->wheel_time_clocks, bs->bs_idx);
Klement Sekerae4504c62016-12-08 10:16:41 +0100331 if (!handling_wakeup)
332 {
333 vlib_process_signal_event (bm->vlib_main,
334 bm->bfd_process_node_index,
335 BFD_EVENT_RESCHEDULE, bs->bs_idx);
336 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200337 }
338}
339
340static void
Klement Sekeraa57a9702017-02-02 06:58:07 +0100341bfd_set_effective_desired_min_tx (bfd_main_t * bm,
342 bfd_session_t * bs, u64 now,
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100343 u64 desired_min_tx_clocks)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200344{
Klement Sekeraa57a9702017-02-02 06:58:07 +0100345 bs->effective_desired_min_tx_clocks = desired_min_tx_clocks;
346 BFD_DBG ("Set effective desired min tx to " BFD_CLK_FMT,
347 BFD_CLK_PRN (bs->effective_desired_min_tx_clocks));
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200348 bfd_recalc_detection_time (bm, bs);
349 bfd_recalc_tx_interval (bm, bs);
Klement Sekera239790f2017-02-16 10:53:53 +0100350 bfd_recalc_echo_tx_interval (bm, bs);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200351 bfd_calc_next_tx (bm, bs, now);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200352}
353
Klement Sekera637b9c42016-12-08 05:19:14 +0100354static void
Klement Sekeraa57a9702017-02-02 06:58:07 +0100355bfd_set_effective_required_min_rx (bfd_main_t * bm,
Klement Sekera73884482017-02-23 09:26:30 +0100356 bfd_session_t * bs,
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100357 u64 required_min_rx_clocks)
Klement Sekeraa57a9702017-02-02 06:58:07 +0100358{
359 bs->effective_required_min_rx_clocks = required_min_rx_clocks;
360 BFD_DBG ("Set effective required min rx to " BFD_CLK_FMT,
361 BFD_CLK_PRN (bs->effective_required_min_rx_clocks));
362 bfd_recalc_detection_time (bm, bs);
Klement Sekeraa57a9702017-02-02 06:58:07 +0100363}
364
365static void
Klement Sekera637b9c42016-12-08 05:19:14 +0100366bfd_set_remote_required_min_rx (bfd_main_t * bm, bfd_session_t * bs,
Klement Sekera239790f2017-02-16 10:53:53 +0100367 u64 now, u32 remote_required_min_rx_usec)
Klement Sekera637b9c42016-12-08 05:19:14 +0100368{
Klement Sekera239790f2017-02-16 10:53:53 +0100369 if (bs->remote_min_rx_usec != remote_required_min_rx_usec)
370 {
371 bs->remote_min_rx_usec = remote_required_min_rx_usec;
372 bs->remote_min_rx_clocks =
373 bfd_usec_to_clocks (bm, remote_required_min_rx_usec);
374 BFD_DBG ("Set remote min rx to " BFD_CLK_FMT,
375 BFD_CLK_PRN (bs->remote_min_rx_clocks));
376 bfd_recalc_detection_time (bm, bs);
377 bfd_recalc_tx_interval (bm, bs);
378 }
379}
380
381static void
382bfd_set_remote_required_min_echo_rx (bfd_main_t * bm, bfd_session_t * bs,
383 u64 now,
384 u32 remote_required_min_echo_rx_usec)
385{
386 if (bs->remote_min_echo_rx_usec != remote_required_min_echo_rx_usec)
387 {
388 bs->remote_min_echo_rx_usec = remote_required_min_echo_rx_usec;
389 bs->remote_min_echo_rx_clocks =
390 bfd_usec_to_clocks (bm, bs->remote_min_echo_rx_usec);
391 BFD_DBG ("Set remote min echo rx to " BFD_CLK_FMT,
392 BFD_CLK_PRN (bs->remote_min_echo_rx_clocks));
393 bfd_recalc_echo_tx_interval (bm, bs);
394 }
Klement Sekera637b9c42016-12-08 05:19:14 +0100395}
396
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200397void
398bfd_session_start (bfd_main_t * bm, bfd_session_t * bs)
399{
Klement Sekera239790f2017-02-16 10:53:53 +0100400 BFD_DBG ("\nStarting session: %U", format_bfd_session, bs);
Klement Sekera73884482017-02-23 09:26:30 +0100401 bfd_set_effective_required_min_rx (bm, bs,
402 bs->config_required_min_rx_clocks);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200403 bfd_recalc_tx_interval (bm, bs);
404 vlib_process_signal_event (bm->vlib_main, bm->bfd_process_node_index,
405 BFD_EVENT_NEW_SESSION, bs->bs_idx);
406}
407
Klement Sekerab17dd962017-01-09 07:43:48 +0100408void
409bfd_session_set_flags (bfd_session_t * bs, u8 admin_up_down)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200410{
411 bfd_main_t *bm = &bfd_main;
Klement Sekera73884482017-02-23 09:26:30 +0100412 u64 now = clib_cpu_time_now ();
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200413 if (admin_up_down)
414 {
Klement Sekerac48829b2017-02-14 07:55:57 +0100415 BFD_DBG ("Session set admin-up, bs-idx=%u", bs->bs_idx);
Klement Sekerae4504c62016-12-08 10:16:41 +0100416 bfd_set_state (bm, bs, BFD_STATE_down, 0);
Klement Sekerac48829b2017-02-14 07:55:57 +0100417 bfd_set_diag (bs, BFD_DIAG_CODE_no_diag);
Klement Sekera73884482017-02-23 09:26:30 +0100418 bfd_calc_next_tx (bm, bs, now);
419 bfd_set_timer (bm, bs, now, 0);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200420 }
421 else
422 {
Klement Sekerac48829b2017-02-14 07:55:57 +0100423 BFD_DBG ("Session set admin-down, bs-idx=%u", bs->bs_idx);
424 bfd_set_diag (bs, BFD_DIAG_CODE_admin_down);
Klement Sekerae4504c62016-12-08 10:16:41 +0100425 bfd_set_state (bm, bs, BFD_STATE_admin_down, 0);
Klement Sekera73884482017-02-23 09:26:30 +0100426 bfd_calc_next_tx (bm, bs, now);
427 bfd_set_timer (bm, bs, now, 0);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200428 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200429}
430
431u8 *
432bfd_input_format_trace (u8 * s, va_list * args)
433{
434 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
435 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
436 const bfd_input_trace_t *t = va_arg (*args, bfd_input_trace_t *);
437 const bfd_pkt_t *pkt = (bfd_pkt_t *) t->data;
438 if (t->len > STRUCT_SIZE_OF (bfd_pkt_t, head))
439 {
440 s = format (s, "BFD v%u, diag=%u(%s), state=%u(%s),\n"
Klement Sekerab17dd962017-01-09 07:43:48 +0100441 " flags=(P:%u, F:%u, C:%u, A:%u, D:%u, M:%u), "
442 "detect_mult=%u, length=%u\n",
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200443 bfd_pkt_get_version (pkt), bfd_pkt_get_diag_code (pkt),
444 bfd_diag_code_string (bfd_pkt_get_diag_code (pkt)),
445 bfd_pkt_get_state (pkt),
446 bfd_state_string (bfd_pkt_get_state (pkt)),
447 bfd_pkt_get_poll (pkt), bfd_pkt_get_final (pkt),
448 bfd_pkt_get_control_plane_independent (pkt),
449 bfd_pkt_get_auth_present (pkt), bfd_pkt_get_demand (pkt),
450 bfd_pkt_get_multipoint (pkt), pkt->head.detect_mult,
451 pkt->head.length);
Klement Sekerab17dd962017-01-09 07:43:48 +0100452 if (t->len >= sizeof (bfd_pkt_t) &&
453 pkt->head.length >= sizeof (bfd_pkt_t))
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200454 {
Klement Sekerad3ba5152017-02-14 03:09:17 +0100455 s = format (s, " my discriminator: %u\n",
456 clib_net_to_host_u32 (pkt->my_disc));
457 s = format (s, " your discriminator: %u\n",
458 clib_net_to_host_u32 (pkt->your_disc));
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200459 s = format (s, " desired min tx interval: %u\n",
460 clib_net_to_host_u32 (pkt->des_min_tx));
461 s = format (s, " required min rx interval: %u\n",
462 clib_net_to_host_u32 (pkt->req_min_rx));
Klement Sekera46a87ad2017-01-02 08:22:23 +0100463 s = format (s, " required min echo rx interval: %u",
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200464 clib_net_to_host_u32 (pkt->req_min_echo_rx));
465 }
Klement Sekerad3ba5152017-02-14 03:09:17 +0100466 if (t->len >= sizeof (bfd_pkt_with_common_auth_t) &&
467 pkt->head.length >= sizeof (bfd_pkt_with_common_auth_t) &&
468 bfd_pkt_get_auth_present (pkt))
469 {
470 const bfd_pkt_with_common_auth_t *with_auth = (void *) pkt;
471 const bfd_auth_common_t *common = &with_auth->common_auth;
472 s = format (s, "\n auth len: %u\n", common->len);
473 s = format (s, " auth type: %u:%s\n", common->type,
474 bfd_auth_type_str (common->type));
475 if (t->len >= sizeof (bfd_pkt_with_sha1_auth_t) &&
476 pkt->head.length >= sizeof (bfd_pkt_with_sha1_auth_t) &&
477 (BFD_AUTH_TYPE_keyed_sha1 == common->type ||
478 BFD_AUTH_TYPE_meticulous_keyed_sha1 == common->type))
479 {
480 const bfd_pkt_with_sha1_auth_t *with_sha1 = (void *) pkt;
481 const bfd_auth_sha1_t *sha1 = &with_sha1->sha1_auth;
482 s = format (s, " seq num: %u\n",
483 clib_net_to_host_u32 (sha1->seq_num));
484 s = format (s, " key id: %u\n", sha1->key_id);
485 s = format (s, " hash: %U", format_hex_bytes, sha1->hash,
486 sizeof (sha1->hash));
487 }
488 }
489 else
490 {
491 s = format (s, "\n");
492 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200493 }
494
495 return s;
496}
497
498static void
Klement Sekerae4504c62016-12-08 10:16:41 +0100499bfd_on_state_change (bfd_main_t * bm, bfd_session_t * bs, u64 now,
500 int handling_wakeup)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200501{
Klement Sekera239790f2017-02-16 10:53:53 +0100502 BFD_DBG ("\nState changed: %U", format_bfd_session, bs);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200503 bfd_event (bm, bs);
504 switch (bs->local_state)
505 {
506 case BFD_STATE_admin_down:
Klement Sekera239790f2017-02-16 10:53:53 +0100507 bs->echo = 0;
Klement Sekeraa57a9702017-02-02 06:58:07 +0100508 bfd_set_effective_desired_min_tx (bm, bs, now,
509 clib_max
510 (bs->config_desired_min_tx_clocks,
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100511 bm->default_desired_min_tx_clocks));
Klement Sekera73884482017-02-23 09:26:30 +0100512 bfd_set_effective_required_min_rx (bm, bs,
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100513 bs->config_required_min_rx_clocks);
514 bfd_set_timer (bm, bs, now, handling_wakeup);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200515 break;
516 case BFD_STATE_down:
Klement Sekera239790f2017-02-16 10:53:53 +0100517 bs->echo = 0;
Klement Sekeraa57a9702017-02-02 06:58:07 +0100518 bfd_set_effective_desired_min_tx (bm, bs, now,
519 clib_max
520 (bs->config_desired_min_tx_clocks,
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100521 bm->default_desired_min_tx_clocks));
Klement Sekera73884482017-02-23 09:26:30 +0100522 bfd_set_effective_required_min_rx (bm, bs,
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100523 bs->config_required_min_rx_clocks);
524 bfd_set_timer (bm, bs, now, handling_wakeup);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200525 break;
526 case BFD_STATE_init:
Klement Sekera239790f2017-02-16 10:53:53 +0100527 bs->echo = 0;
Klement Sekeraa57a9702017-02-02 06:58:07 +0100528 bfd_set_effective_desired_min_tx (bm, bs, now,
Klement Sekerac48829b2017-02-14 07:55:57 +0100529 bs->config_desired_min_tx_clocks);
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100530 bfd_set_timer (bm, bs, now, handling_wakeup);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200531 break;
532 case BFD_STATE_up:
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100533 bfd_set_effective_desired_min_tx (bm, bs, now,
534 bs->config_desired_min_tx_clocks);
Klement Sekera239790f2017-02-16 10:53:53 +0100535 if (BFD_POLL_NOT_NEEDED == bs->poll_state)
Klement Sekeraa57a9702017-02-02 06:58:07 +0100536 {
Klement Sekera73884482017-02-23 09:26:30 +0100537 bfd_set_effective_required_min_rx (bm, bs,
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100538 bs->config_required_min_rx_clocks);
Klement Sekeraa57a9702017-02-02 06:58:07 +0100539 }
Klement Sekeraaeeac3b2017-02-14 07:11:52 +0100540 bfd_set_timer (bm, bs, now, handling_wakeup);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200541 break;
542 }
543}
544
545static void
Klement Sekeraa57a9702017-02-02 06:58:07 +0100546bfd_on_config_change (vlib_main_t * vm, vlib_node_runtime_t * rt,
547 bfd_main_t * bm, bfd_session_t * bs, u64 now)
548{
Klement Sekera239790f2017-02-16 10:53:53 +0100549 /*
550 * if remote demand mode is set and we need to do a poll, set the next
551 * timeout so that the session wakes up immediately
552 */
553 if (bs->remote_demand && BFD_POLL_NEEDED == bs->poll_state &&
554 bs->poll_state_start_or_timeout_clocks < now)
Klement Sekeraa57a9702017-02-02 06:58:07 +0100555 {
Klement Sekera239790f2017-02-16 10:53:53 +0100556 bs->tx_timeout_clocks = now;
Klement Sekeraa57a9702017-02-02 06:58:07 +0100557 }
558 bfd_recalc_detection_time (bm, bs);
559 bfd_set_timer (bm, bs, now, 0);
560}
561
562static void
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200563bfd_add_transport_layer (vlib_main_t * vm, vlib_buffer_t * b,
564 bfd_session_t * bs)
565{
566 switch (bs->transport)
567 {
568 case BFD_TRANSPORT_UDP4:
Klement Sekera46a87ad2017-01-02 08:22:23 +0100569 BFD_DBG ("Transport bfd via udp4, bs_idx=%u", bs->bs_idx);
Klement Sekera239790f2017-02-16 10:53:53 +0100570 bfd_add_udp4_transport (vm, b, bs, 0 /* is_echo */ );
Klement Sekera46a87ad2017-01-02 08:22:23 +0100571 break;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200572 case BFD_TRANSPORT_UDP6:
Klement Sekera46a87ad2017-01-02 08:22:23 +0100573 BFD_DBG ("Transport bfd via udp6, bs_idx=%u", bs->bs_idx);
Klement Sekera239790f2017-02-16 10:53:53 +0100574 bfd_add_udp6_transport (vm, b, bs, 0 /* is_echo */ );
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200575 break;
576 }
577}
578
Klement Sekera239790f2017-02-16 10:53:53 +0100579static int
580bfd_echo_add_transport_layer (vlib_main_t * vm, vlib_buffer_t * b,
581 bfd_session_t * bs)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200582{
Klement Sekera239790f2017-02-16 10:53:53 +0100583 switch (bs->transport)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200584 {
Klement Sekera239790f2017-02-16 10:53:53 +0100585 case BFD_TRANSPORT_UDP4:
586 BFD_DBG ("Transport bfd echo via udp4, bs_idx=%u", bs->bs_idx);
587 return bfd_add_udp4_transport (vm, b, bs, 1 /* is_echo */ );
588 break;
589 case BFD_TRANSPORT_UDP6:
590 BFD_DBG ("Transport bfd echo via udp6, bs_idx=%u", bs->bs_idx);
591 return bfd_add_udp6_transport (vm, b, bs, 1 /* is_echo */ );
592 break;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200593 }
Klement Sekera239790f2017-02-16 10:53:53 +0100594 return 0;
595}
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200596
Klement Sekera239790f2017-02-16 10:53:53 +0100597static void
598bfd_create_frame_to_next_node (vlib_main_t * vm, bfd_session_t * bs, u32 bi)
599{
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200600
Klement Sekera46a87ad2017-01-02 08:22:23 +0100601 vlib_frame_t *f =
602 vlib_get_frame_to_node (vm, bfd_node_index_by_transport[bs->transport]);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200603
Klement Sekera46a87ad2017-01-02 08:22:23 +0100604 u32 *to_next = vlib_frame_vector_args (f);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200605 to_next[0] = bi;
Klement Sekera46a87ad2017-01-02 08:22:23 +0100606 f->n_vectors = 1;
Klement Sekera46a87ad2017-01-02 08:22:23 +0100607 vlib_put_frame_to_node (vm, bfd_node_index_by_transport[bs->transport], f);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200608}
609
Klement Sekerab17dd962017-01-09 07:43:48 +0100610#if WITH_LIBSSL > 0
611static void
612bfd_add_sha1_auth_section (vlib_buffer_t * b, bfd_session_t * bs)
613{
614 bfd_pkt_with_sha1_auth_t *pkt = vlib_buffer_get_current (b);
615 bfd_auth_sha1_t *auth = &pkt->sha1_auth;
616 b->current_length += sizeof (*auth);
617 pkt->pkt.head.length += sizeof (*auth);
618 bfd_pkt_set_auth_present (&pkt->pkt);
619 memset (auth, 0, sizeof (*auth));
620 auth->type_len.type = bs->auth.curr_key->auth_type;
621 /*
622 * only meticulous authentication types require incrementing seq number
623 * for every message, but doing so doesn't violate the RFC
624 */
625 ++bs->auth.local_seq_number;
626 auth->type_len.len = sizeof (bfd_auth_sha1_t);
627 auth->key_id = bs->auth.curr_bfd_key_id;
628 auth->seq_num = clib_host_to_net_u32 (bs->auth.local_seq_number);
629 /*
630 * first copy the password into the packet, then calculate the hash
631 * and finally replace the password with the calculated hash
632 */
633 clib_memcpy (auth->hash, bs->auth.curr_key->key,
634 sizeof (bs->auth.curr_key->key));
635 unsigned char hash[sizeof (auth->hash)];
636 SHA1 ((unsigned char *) pkt, sizeof (*pkt), hash);
637 BFD_DBG ("hashing: %U", format_hex_bytes, pkt, sizeof (*pkt));
638 clib_memcpy (auth->hash, hash, sizeof (hash));
Klement Sekerab17dd962017-01-09 07:43:48 +0100639}
Klement Sekera6f966492017-02-08 07:42:08 +0100640#endif
Klement Sekerab17dd962017-01-09 07:43:48 +0100641
642static void
643bfd_add_auth_section (vlib_buffer_t * b, bfd_session_t * bs)
644{
645 if (bs->auth.curr_key)
646 {
647 const bfd_auth_type_e auth_type = bs->auth.curr_key->auth_type;
648 switch (auth_type)
649 {
650 case BFD_AUTH_TYPE_reserved:
651 /* fallthrough */
652 case BFD_AUTH_TYPE_simple_password:
653 /* fallthrough */
654 case BFD_AUTH_TYPE_keyed_md5:
655 /* fallthrough */
656 case BFD_AUTH_TYPE_meticulous_keyed_md5:
657 clib_warning ("Internal error, unexpected BFD auth type '%d'",
658 auth_type);
659 break;
660#if WITH_LIBSSL > 0
661 case BFD_AUTH_TYPE_keyed_sha1:
662 /* fallthrough */
663 case BFD_AUTH_TYPE_meticulous_keyed_sha1:
664 bfd_add_sha1_auth_section (b, bs);
665 break;
666#else
667 case BFD_AUTH_TYPE_keyed_sha1:
668 /* fallthrough */
669 case BFD_AUTH_TYPE_meticulous_keyed_sha1:
670 clib_warning ("Internal error, unexpected BFD auth type '%d'",
671 auth_type);
672 break;
673#endif
674 }
675 }
676}
677
Klement Sekera239790f2017-02-16 10:53:53 +0100678static int
679bfd_is_echo_possible (bfd_session_t * bs)
680{
681 if (BFD_STATE_up == bs->local_state && BFD_STATE_up == bs->remote_state &&
682 bs->remote_min_echo_rx_usec > 0)
683 {
684 switch (bs->transport)
685 {
686 case BFD_TRANSPORT_UDP4:
687 return bfd_udp_is_echo_available (BFD_TRANSPORT_UDP4);
688 case BFD_TRANSPORT_UDP6:
689 return bfd_udp_is_echo_available (BFD_TRANSPORT_UDP6);
690 }
691 }
692 return 0;
693}
694
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200695static void
Klement Sekera239790f2017-02-16 10:53:53 +0100696bfd_init_control_frame (bfd_main_t * bm, bfd_session_t * bs,
697 vlib_buffer_t * b)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200698{
699 bfd_pkt_t *pkt = vlib_buffer_get_current (b);
Klement Sekerab17dd962017-01-09 07:43:48 +0100700 u32 bfd_length = 0;
701 bfd_length = sizeof (bfd_pkt_t);
702 memset (pkt, 0, sizeof (*pkt));
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200703 bfd_pkt_set_version (pkt, 1);
704 bfd_pkt_set_diag_code (pkt, bs->local_diag);
705 bfd_pkt_set_state (pkt, bs->local_state);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200706 pkt->head.detect_mult = bs->local_detect_mult;
707 pkt->head.length = clib_host_to_net_u32 (bfd_length);
708 pkt->my_disc = bs->local_discr;
709 pkt->your_disc = bs->remote_discr;
Klement Sekeraa57a9702017-02-02 06:58:07 +0100710 pkt->des_min_tx = clib_host_to_net_u32 (bs->config_desired_min_tx_usec);
Klement Sekera239790f2017-02-16 10:53:53 +0100711 if (bs->echo)
712 {
713 pkt->req_min_rx =
714 clib_host_to_net_u32 (bfd_clocks_to_usec
715 (bm, bs->effective_required_min_rx_clocks));
716 }
717 else
718 {
719 pkt->req_min_rx =
720 clib_host_to_net_u32 (bs->config_required_min_rx_usec);
721 }
Klement Sekeraa57a9702017-02-02 06:58:07 +0100722 pkt->req_min_echo_rx = clib_host_to_net_u32 (1);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200723 b->current_length = bfd_length;
724}
725
726static void
Klement Sekera239790f2017-02-16 10:53:53 +0100727bfd_send_echo (vlib_main_t * vm, vlib_node_runtime_t * rt,
728 bfd_main_t * bm, bfd_session_t * bs, u64 now,
729 int handling_wakeup)
730{
731 if (!bfd_is_echo_possible (bs))
732 {
733 BFD_DBG ("\nSwitching off echo function: %U", format_bfd_session, bs);
734 bs->echo = 0;
735 return;
736 }
737 /* sometimes the wheel expires an event a bit sooner than requested, account
738 for that here */
739 if (now + bm->wheel_inaccuracy >= bs->echo_tx_timeout_clocks)
740 {
741 BFD_DBG ("\nSending echo packet: %U", format_bfd_session, bs);
742 u32 bi;
743 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
744 {
745 clib_warning ("buffer allocation failure");
746 return;
747 }
748 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
749 ASSERT (b->current_data == 0);
750 bfd_echo_pkt_t *pkt = vlib_buffer_get_current (b);
751 memset (pkt, 0, sizeof (*pkt));
752 pkt->discriminator = bs->local_discr;
753 pkt->expire_time_clocks =
754 now + bs->echo_transmit_interval_clocks * bs->local_detect_mult;
755 pkt->checksum =
756 bfd_calc_echo_checksum (bs->local_discr, pkt->expire_time_clocks,
757 bs->echo_secret);
758 b->current_length = sizeof (*pkt);
759 if (!bfd_echo_add_transport_layer (vm, b, bs))
760 {
761 BFD_ERR ("cannot send echo packet out, turning echo off");
762 bs->echo = 0;
763 vlib_buffer_free_one (vm, bi);
764 return;
765 }
766 bs->echo_last_tx_clocks = now;
767 bfd_calc_next_echo_tx (bm, bs, now);
768 bfd_create_frame_to_next_node (vm, bs, bi);
769 }
770 else
771 {
772 BFD_DBG
773 ("No need to send echo packet now, now is %lu, tx_timeout is %lu",
774 now, bs->echo_tx_timeout_clocks);
775 }
776}
777
778static void
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200779bfd_send_periodic (vlib_main_t * vm, vlib_node_runtime_t * rt,
Klement Sekerae4504c62016-12-08 10:16:41 +0100780 bfd_main_t * bm, bfd_session_t * bs, u64 now,
781 int handling_wakeup)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200782{
Klement Sekera239790f2017-02-16 10:53:53 +0100783 if (!bs->remote_min_rx_usec && BFD_POLL_NOT_NEEDED == bs->poll_state)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200784 {
Klement Sekera239790f2017-02-16 10:53:53 +0100785 BFD_DBG ("Remote min rx interval is zero, not sending periodic control "
786 "frame");
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200787 return;
788 }
Klement Sekera239790f2017-02-16 10:53:53 +0100789 if (BFD_POLL_NOT_NEEDED == bs->poll_state && bs->remote_demand &&
Klement Sekerad3ba5152017-02-14 03:09:17 +0100790 BFD_STATE_up == bs->local_state && BFD_STATE_up == bs->remote_state)
791 {
792 /*
793 * A system MUST NOT periodically transmit BFD Control packets if Demand
794 * mode is active on the remote system (bfd.RemoteDemandMode is 1,
795 * bfd.SessionState is Up, and bfd.RemoteSessionState is Up) and a Poll
796 * Sequence is not being transmitted.
797 */
Klement Sekera239790f2017-02-16 10:53:53 +0100798 BFD_DBG ("Remote demand is set, not sending periodic control frame");
Klement Sekerad3ba5152017-02-14 03:09:17 +0100799 return;
800 }
Klement Sekera637b9c42016-12-08 05:19:14 +0100801 /* sometimes the wheel expires an event a bit sooner than requested, account
802 for that here */
803 if (now + bm->wheel_inaccuracy >= bs->tx_timeout_clocks)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200804 {
Klement Sekera239790f2017-02-16 10:53:53 +0100805 BFD_DBG ("\nSending periodic control frame: %U", format_bfd_session,
806 bs);
807 u32 bi;
808 if (vlib_buffer_alloc (vm, &bi, 1) != 1)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200809 {
Klement Sekera239790f2017-02-16 10:53:53 +0100810 clib_warning ("buffer allocation failure");
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200811 return;
812 }
Klement Sekera239790f2017-02-16 10:53:53 +0100813 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
814 ASSERT (b->current_data == 0);
815 bfd_init_control_frame (bm, bs, b);
816 switch (bs->poll_state)
Klement Sekeraa57a9702017-02-02 06:58:07 +0100817 {
Klement Sekera239790f2017-02-16 10:53:53 +0100818 case BFD_POLL_NEEDED:
819 if (now < bs->poll_state_start_or_timeout_clocks)
820 {
821 BFD_DBG ("Cannot start a poll sequence yet, need to wait "
822 "for " BFD_CLK_FMT,
823 BFD_CLK_PRN (bs->poll_state_start_or_timeout_clocks -
824 now));
825 break;
826 }
827 bs->poll_state_start_or_timeout_clocks = now;
828 bfd_set_poll_state (bs, BFD_POLL_IN_PROGRESS);
829 /* fallthrough */
830 case BFD_POLL_IN_PROGRESS:
831 case BFD_POLL_IN_PROGRESS_AND_QUEUED:
Klement Sekeraa57a9702017-02-02 06:58:07 +0100832 bfd_pkt_set_poll (vlib_buffer_get_current (b));
Klement Sekeraa57a9702017-02-02 06:58:07 +0100833 BFD_DBG ("Setting poll bit in packet, bs_idx=%u", bs->bs_idx);
Klement Sekera239790f2017-02-16 10:53:53 +0100834 break;
835 case BFD_POLL_NOT_NEEDED:
836 /* fallthrough */
837 break;
Klement Sekeraa57a9702017-02-02 06:58:07 +0100838 }
839 bfd_add_auth_section (b, bs);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200840 bfd_add_transport_layer (vm, b, bs);
Klement Sekera3e0a3562016-12-19 09:05:21 +0100841 bs->last_tx_clocks = now;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200842 bfd_calc_next_tx (bm, bs, now);
Klement Sekera239790f2017-02-16 10:53:53 +0100843 bfd_create_frame_to_next_node (vm, bs, bi);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200844 }
845 else
846 {
Klement Sekera637b9c42016-12-08 05:19:14 +0100847 BFD_DBG
848 ("No need to send control frame now, now is %lu, tx_timeout is %lu",
849 now, bs->tx_timeout_clocks);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200850 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200851}
852
853void
Klement Sekerab17dd962017-01-09 07:43:48 +0100854bfd_init_final_control_frame (vlib_main_t * vm, vlib_buffer_t * b,
Klement Sekera239790f2017-02-16 10:53:53 +0100855 bfd_main_t * bm, bfd_session_t * bs)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200856{
857 BFD_DBG ("Send final control frame for bs_idx=%lu", bs->bs_idx);
Klement Sekera239790f2017-02-16 10:53:53 +0100858 bfd_init_control_frame (bm, bs, b);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200859 bfd_pkt_set_final (vlib_buffer_get_current (b));
Klement Sekeraa57a9702017-02-02 06:58:07 +0100860 bfd_add_auth_section (b, bs);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200861 bfd_add_transport_layer (vm, b, bs);
Klement Sekera3e0a3562016-12-19 09:05:21 +0100862 bs->last_tx_clocks = clib_cpu_time_now ();
Klement Sekeraa57a9702017-02-02 06:58:07 +0100863 /*
864 * RFC allows to include changes in final frame, so if there were any
865 * pending, we already did that, thus we can clear any pending poll needs
866 */
Klement Sekera239790f2017-02-16 10:53:53 +0100867 bfd_set_poll_state (bs, BFD_POLL_NOT_NEEDED);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200868}
869
870static void
Klement Sekerae4504c62016-12-08 10:16:41 +0100871bfd_check_rx_timeout (bfd_main_t * bm, bfd_session_t * bs, u64 now,
872 int handling_wakeup)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200873{
Klement Sekera637b9c42016-12-08 05:19:14 +0100874 /* sometimes the wheel expires an event a bit sooner than requested, account
875 for that here */
876 if (bs->last_rx_clocks + bs->detection_time_clocks <=
877 now + bm->wheel_inaccuracy)
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200878 {
879 BFD_DBG ("Rx timeout, session goes down");
880 bfd_set_diag (bs, BFD_DIAG_CODE_det_time_exp);
Klement Sekerae4504c62016-12-08 10:16:41 +0100881 bfd_set_state (bm, bs, BFD_STATE_down, handling_wakeup);
Klement Sekeraa57a9702017-02-02 06:58:07 +0100882 /*
883 * If the remote system does not receive any
884 * BFD Control packets for a Detection Time, it SHOULD reset
885 * bfd.RemoteMinRxInterval to its initial value of 1 (per section 6.8.1,
886 * since it is no longer required to maintain previous session state)
887 * and then can transmit at its own rate.
888 */
Klement Sekera239790f2017-02-16 10:53:53 +0100889 bfd_set_remote_required_min_rx (bm, bs, now, 1);
890 }
891 else if (bs->echo &&
892 bs->echo_last_rx_clocks +
893 bs->echo_transmit_interval_clocks * bs->local_detect_mult <=
894 now + bm->wheel_inaccuracy)
895 {
896 BFD_DBG ("Echo rx timeout, session goes down");
897 bfd_set_diag (bs, BFD_DIAG_CODE_echo_failed);
898 bfd_set_state (bm, bs, BFD_STATE_down, handling_wakeup);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200899 }
900}
901
902void
903bfd_on_timeout (vlib_main_t * vm, vlib_node_runtime_t * rt, bfd_main_t * bm,
904 bfd_session_t * bs, u64 now)
905{
906 BFD_DBG ("Timeout for bs_idx=%lu", bs->bs_idx);
907 switch (bs->local_state)
908 {
909 case BFD_STATE_admin_down:
Klement Sekerac48829b2017-02-14 07:55:57 +0100910 bfd_send_periodic (vm, rt, bm, bs, now, 1);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200911 break;
912 case BFD_STATE_down:
Klement Sekerae4504c62016-12-08 10:16:41 +0100913 bfd_send_periodic (vm, rt, bm, bs, now, 1);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200914 break;
915 case BFD_STATE_init:
Klement Sekerae4504c62016-12-08 10:16:41 +0100916 bfd_check_rx_timeout (bm, bs, now, 1);
917 bfd_send_periodic (vm, rt, bm, bs, now, 1);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200918 break;
Klement Sekera239790f2017-02-16 10:53:53 +0100919 case BFD_STATE_up:
920 bfd_check_rx_timeout (bm, bs, now, 1);
921 if (BFD_POLL_NOT_NEEDED == bs->poll_state && !bs->echo &&
922 bfd_is_echo_possible (bs))
923 {
924 /* switch on echo function as main detection method now */
925 BFD_DBG ("Switching on echo function, bs_idx=%u", bs->bs_idx);
926 bs->echo = 1;
927 bs->echo_last_rx_clocks = now;
928 bs->echo_tx_timeout_clocks = now;
Klement Sekera73884482017-02-23 09:26:30 +0100929 bfd_set_effective_required_min_rx (bm, bs,
Klement Sekera239790f2017-02-16 10:53:53 +0100930 clib_max
931 (bm->min_required_min_rx_while_echo_clocks,
932 bs->config_required_min_rx_clocks));
933 bfd_set_poll_state (bs, BFD_POLL_NEEDED);
934 }
935 bfd_send_periodic (vm, rt, bm, bs, now, 1);
936 if (bs->echo)
937 {
938 bfd_send_echo (vm, rt, bm, bs, now, 1);
939 }
940 break;
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200941 }
942}
943
944/*
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200945 * bfd process node function
946 */
947static uword
948bfd_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
949{
950 bfd_main_t *bm = &bfd_main;
951 u32 *expired = 0;
952 uword event_type, *event_data = 0;
953
954 /* So we can send events to the bfd process */
955 bm->bfd_process_node_index = bfd_process_node.index;
956
957 while (1)
958 {
959 u64 now = clib_cpu_time_now ();
960 u64 next_expire = timing_wheel_next_expiring_elt_time (&bm->wheel);
961 BFD_DBG ("timing_wheel_next_expiring_elt_time(%p) returns %lu",
962 &bm->wheel, next_expire);
963 if ((i64) next_expire < 0)
964 {
965 BFD_DBG ("wait for event without timeout");
966 (void) vlib_process_wait_for_event (vm);
Klement Sekera0c1519b2016-12-08 05:03:32 +0100967 event_type = vlib_process_get_events (vm, &event_data);
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200968 }
969 else
970 {
971 f64 timeout = ((i64) next_expire - (i64) now) / bm->cpu_cps;
972 BFD_DBG ("wait for event with timeout %.02f", timeout);
Klement Sekera0c1519b2016-12-08 05:03:32 +0100973 if (timeout < 0)
974 {
975 BFD_DBG ("negative timeout, already expired, skipping wait");
976 event_type = ~0;
977 }
978 else
979 {
980 (void) vlib_process_wait_for_event_or_clock (vm, timeout);
981 event_type = vlib_process_get_events (vm, &event_data);
982 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200983 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200984 now = clib_cpu_time_now ();
985 switch (event_type)
986 {
987 case ~0: /* no events => timeout */
988 /* nothing to do here */
989 break;
990 case BFD_EVENT_RESCHEDULE:
991 /* nothing to do here - reschedule is done automatically after
992 * each event or timeout */
993 break;
994 case BFD_EVENT_NEW_SESSION:
Klement Sekerab17dd962017-01-09 07:43:48 +0100995 if (!pool_is_free_index (bm->sessions, *event_data))
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200996 {
997 bfd_session_t *bs =
998 pool_elt_at_index (bm->sessions, *event_data);
Klement Sekerae4504c62016-12-08 10:16:41 +0100999 bfd_send_periodic (vm, rt, bm, bs, now, 1);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001000 }
Klement Sekerab17dd962017-01-09 07:43:48 +01001001 else
1002 {
1003 BFD_DBG ("Ignoring event for non-existent session index %u",
1004 (u32) * event_data);
1005 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001006 break;
Klement Sekeraa57a9702017-02-02 06:58:07 +01001007 case BFD_EVENT_CONFIG_CHANGED:
1008 if (!pool_is_free_index (bm->sessions, *event_data))
1009 {
1010 bfd_session_t *bs =
1011 pool_elt_at_index (bm->sessions, *event_data);
1012 bfd_on_config_change (vm, rt, bm, bs, now);
1013 }
1014 else
1015 {
1016 BFD_DBG ("Ignoring event for non-existent session index %u",
1017 (u32) * event_data);
1018 }
1019 break;
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001020 default:
1021 clib_warning ("BUG: event type 0x%wx", event_type);
1022 break;
1023 }
1024 BFD_DBG ("advancing wheel, now is %lu", now);
1025 BFD_DBG ("timing_wheel_advance (%p, %lu, %p, 0);", &bm->wheel, now,
1026 expired);
1027 expired = timing_wheel_advance (&bm->wheel, now, expired, 0);
1028 BFD_DBG ("Expired %d elements", vec_len (expired));
1029 u32 *p = NULL;
1030 vec_foreach (p, expired)
1031 {
1032 const u32 bs_idx = *p;
1033 if (!pool_is_free_index (bm->sessions, bs_idx))
1034 {
1035 bfd_session_t *bs = pool_elt_at_index (bm->sessions, bs_idx);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001036 bfd_on_timeout (vm, rt, bm, bs, now);
Klement Sekera239790f2017-02-16 10:53:53 +01001037 bfd_set_timer (bm, bs, now, 1);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001038 }
1039 }
1040 if (expired)
1041 {
1042 _vec_len (expired) = 0;
1043 }
1044 if (event_data)
1045 {
1046 _vec_len (event_data) = 0;
1047 }
1048 }
1049
1050 return 0;
1051}
1052
1053/*
1054 * bfd process node declaration
1055 */
1056/* *INDENT-OFF* */
1057VLIB_REGISTER_NODE (bfd_process_node, static) = {
1058 .function = bfd_process,
1059 .type = VLIB_NODE_TYPE_PROCESS,
1060 .name = "bfd-process",
Klement Sekera46a87ad2017-01-02 08:22:23 +01001061 .n_next_nodes = 0,
1062 .next_nodes = {},
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001063};
1064/* *INDENT-ON* */
1065
1066static clib_error_t *
1067bfd_sw_interface_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
1068{
1069 // bfd_main_t *bm = &bfd_main;
1070 // vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
1071 if (!(flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
1072 {
1073 /* TODO */
1074 }
1075 return 0;
1076}
1077
1078VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (bfd_sw_interface_up_down);
1079
1080static clib_error_t *
1081bfd_hw_interface_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
1082{
1083 // bfd_main_t *bm = &bfd_main;
1084 if (flags & VNET_HW_INTERFACE_FLAG_LINK_UP)
1085 {
1086 /* TODO */
1087 }
1088 return 0;
1089}
1090
1091VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION (bfd_hw_interface_up_down);
1092
1093/*
1094 * setup function
1095 */
1096static clib_error_t *
1097bfd_main_init (vlib_main_t * vm)
1098{
Klement Sekerab17dd962017-01-09 07:43:48 +01001099#if BFD_DEBUG
1100 setbuf (stdout, NULL);
1101#endif
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001102 bfd_main_t *bm = &bfd_main;
1103 bm->random_seed = random_default_seed ();
1104 bm->vlib_main = vm;
1105 bm->vnet_main = vnet_get_main ();
1106 memset (&bm->wheel, 0, sizeof (bm->wheel));
Klement Sekerab17dd962017-01-09 07:43:48 +01001107 bm->cpu_cps = vm->clib_time.clocks_per_second;
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001108 BFD_DBG ("cps is %.2f", bm->cpu_cps);
Klement Sekeraa57a9702017-02-02 06:58:07 +01001109 bm->default_desired_min_tx_clocks =
Klement Sekera239790f2017-02-16 10:53:53 +01001110 bfd_usec_to_clocks (bm, BFD_DEFAULT_DESIRED_MIN_TX_USEC);
1111 bm->min_required_min_rx_while_echo_clocks =
1112 bfd_usec_to_clocks (bm, BFD_REQUIRED_MIN_RX_USEC_WHILE_ECHO);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001113 const u64 now = clib_cpu_time_now ();
1114 timing_wheel_init (&bm->wheel, now, bm->cpu_cps);
Klement Sekera637b9c42016-12-08 05:19:14 +01001115 bm->wheel_inaccuracy = 2 << bm->wheel.log2_clocks_per_bin;
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001116
Klement Sekera46a87ad2017-01-02 08:22:23 +01001117 vlib_node_t *node = NULL;
Klement Sekerab17dd962017-01-09 07:43:48 +01001118#define F(t, n) \
1119 node = vlib_get_node_by_name (vm, (u8 *)n); \
1120 bfd_node_index_by_transport[BFD_TRANSPORT_##t] = node->index; \
1121 BFD_DBG ("node '%s' has index %u", n, node->index);
Klement Sekera46a87ad2017-01-02 08:22:23 +01001122 foreach_bfd_transport (F);
1123#undef F
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001124 return 0;
1125}
1126
1127VLIB_INIT_FUNCTION (bfd_main_init);
1128
1129bfd_session_t *
Klement Sekera239790f2017-02-16 10:53:53 +01001130bfd_get_session (bfd_main_t * bm, bfd_transport_e t)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001131{
1132 bfd_session_t *result;
1133 pool_get (bm->sessions, result);
Klement Sekerae4504c62016-12-08 10:16:41 +01001134 memset (result, 0, sizeof (*result));
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001135 result->bs_idx = result - bm->sessions;
1136 result->transport = t;
Klement Sekera239790f2017-02-16 10:53:53 +01001137 const unsigned limit = 1000;
1138 unsigned counter = 0;
1139 do
1140 {
1141 result->local_discr = random_u32 (&bm->random_seed);
1142 if (counter > limit)
1143 {
1144 clib_warning ("Couldn't allocate unused session discriminator even "
1145 "after %u tries!", limit);
1146 pool_put (bm->sessions, result);
1147 return NULL;
1148 }
1149 ++counter;
1150 }
1151 while (hash_get (bm->session_by_disc, result->local_discr));
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001152 bfd_set_defaults (bm, result);
1153 hash_set (bm->session_by_disc, result->local_discr, result->bs_idx);
1154 return result;
1155}
1156
1157void
1158bfd_put_session (bfd_main_t * bm, bfd_session_t * bs)
1159{
Klement Sekerab17dd962017-01-09 07:43:48 +01001160 if (bs->auth.curr_key)
1161 {
1162 --bs->auth.curr_key->use_count;
1163 }
1164 if (bs->auth.next_key)
1165 {
1166 --bs->auth.next_key->use_count;
1167 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001168 hash_unset (bm->session_by_disc, bs->local_discr);
1169 pool_put (bm->sessions, bs);
1170}
1171
1172bfd_session_t *
1173bfd_find_session_by_idx (bfd_main_t * bm, uword bs_idx)
1174{
1175 if (!pool_is_free_index (bm->sessions, bs_idx))
1176 {
1177 return pool_elt_at_index (bm->sessions, bs_idx);
1178 }
1179 return NULL;
1180}
1181
1182bfd_session_t *
1183bfd_find_session_by_disc (bfd_main_t * bm, u32 disc)
1184{
1185 uword *p = hash_get (bfd_main.session_by_disc, disc);
1186 if (p)
1187 {
1188 return pool_elt_at_index (bfd_main.sessions, *p);
1189 }
1190 return NULL;
1191}
1192
1193/**
1194 * @brief verify bfd packet - common checks
1195 *
1196 * @param pkt
1197 *
1198 * @return 1 if bfd packet is valid
1199 */
1200int
1201bfd_verify_pkt_common (const bfd_pkt_t * pkt)
1202{
1203 if (1 != bfd_pkt_get_version (pkt))
1204 {
1205 BFD_ERR ("BFD verification failed - unexpected version: '%d'",
1206 bfd_pkt_get_version (pkt));
1207 return 0;
1208 }
1209 if (pkt->head.length < sizeof (bfd_pkt_t) ||
1210 (bfd_pkt_get_auth_present (pkt) &&
Klement Sekerab17dd962017-01-09 07:43:48 +01001211 pkt->head.length < sizeof (bfd_pkt_with_common_auth_t)))
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001212 {
1213 BFD_ERR ("BFD verification failed - unexpected length: '%d' (auth "
1214 "present: %d)",
1215 pkt->head.length, bfd_pkt_get_auth_present (pkt));
1216 return 0;
1217 }
1218 if (!pkt->head.detect_mult)
1219 {
1220 BFD_ERR ("BFD verification failed - unexpected detect-mult: '%d'",
1221 pkt->head.detect_mult);
1222 return 0;
1223 }
1224 if (bfd_pkt_get_multipoint (pkt))
1225 {
1226 BFD_ERR ("BFD verification failed - unexpected multipoint: '%d'",
1227 bfd_pkt_get_multipoint (pkt));
1228 return 0;
1229 }
1230 if (!pkt->my_disc)
1231 {
1232 BFD_ERR ("BFD verification failed - unexpected my-disc: '%d'",
1233 pkt->my_disc);
1234 return 0;
1235 }
1236 if (!pkt->your_disc)
1237 {
1238 const u8 pkt_state = bfd_pkt_get_state (pkt);
1239 if (pkt_state != BFD_STATE_down && pkt_state != BFD_STATE_admin_down)
1240 {
1241 BFD_ERR ("BFD verification failed - unexpected state: '%s' "
1242 "(your-disc is zero)", bfd_state_string (pkt_state));
1243 return 0;
1244 }
1245 }
1246 return 1;
1247}
1248
Klement Sekerab17dd962017-01-09 07:43:48 +01001249static void
1250bfd_session_switch_auth_to_next (bfd_session_t * bs)
1251{
1252 BFD_DBG ("Switching authentication key from %U to %U for bs_idx=%u",
1253 format_bfd_auth_key, bs->auth.curr_key, format_bfd_auth_key,
1254 bs->auth.next_key, bs->bs_idx);
1255 bs->auth.is_delayed = 0;
1256 if (bs->auth.curr_key)
1257 {
1258 --bs->auth.curr_key->use_count;
1259 }
1260 bs->auth.curr_key = bs->auth.next_key;
1261 bs->auth.next_key = NULL;
1262 bs->auth.curr_bfd_key_id = bs->auth.next_bfd_key_id;
1263}
1264
1265static int
1266bfd_auth_type_is_meticulous (bfd_auth_type_e auth_type)
1267{
1268 if (BFD_AUTH_TYPE_meticulous_keyed_md5 == auth_type ||
1269 BFD_AUTH_TYPE_meticulous_keyed_sha1 == auth_type)
1270 {
1271 return 1;
1272 }
1273 return 0;
1274}
1275
1276static int
1277bfd_verify_pkt_auth_seq_num (bfd_session_t * bs,
1278 u32 received_seq_num, int is_meticulous)
1279{
1280 /*
1281 * RFC 5880 6.8.1:
1282 *
1283 * This variable MUST be set to zero after no packets have been
1284 * received on this session for at least twice the Detection Time.
1285 */
1286 u64 now = clib_cpu_time_now ();
1287 if (now - bs->last_rx_clocks > bs->detection_time_clocks * 2)
1288 {
1289 BFD_DBG ("BFD peer unresponsive for %lu clocks, which is > 2 * "
1290 "detection_time=%u clocks, resetting remote_seq_number_known "
1291 "flag",
1292 now - bs->last_rx_clocks, bs->detection_time_clocks * 2);
1293 bs->auth.remote_seq_number_known = 0;
1294 }
1295 if (bs->auth.remote_seq_number_known)
1296 {
1297 /* remote sequence number is known, verify its validity */
1298 const u32 max_u32 = 0xffffffff;
1299 /* the calculation might wrap, account for the special case... */
1300 if (bs->auth.remote_seq_number > max_u32 - 3 * bs->local_detect_mult)
1301 {
1302 /*
1303 * special case
1304 *
1305 * x y z
1306 * |----------+----------------------------+-----------|
1307 * 0 ^ ^ 0xffffffff
1308 * | remote_seq_num------+
1309 * |
1310 * +-----(remote_seq_num + 3*detect_mult) % * 0xffffffff
1311 *
1312 * x + y + z = 0xffffffff
1313 * x + z = 3 * detect_mult
1314 */
1315 const u32 z = max_u32 - bs->auth.remote_seq_number;
1316 const u32 x = 3 * bs->local_detect_mult - z;
1317 if (received_seq_num > x &&
1318 received_seq_num < bs->auth.remote_seq_number + is_meticulous)
1319 {
1320 BFD_ERR
1321 ("Recvd sequence number=%u out of ranges <0, %u>, <%u, %u>",
1322 received_seq_num, x,
1323 bs->auth.remote_seq_number + is_meticulous, max_u32);
1324 return 0;
1325 }
1326 }
1327 else
1328 {
1329 /* regular case */
1330 const u32 min = bs->auth.remote_seq_number + is_meticulous;
1331 const u32 max =
1332 bs->auth.remote_seq_number + 3 * bs->local_detect_mult;
1333 if (received_seq_num < min || received_seq_num > max)
1334 {
1335 BFD_ERR ("Recvd sequence number=%u out of range <%u, %u>",
1336 received_seq_num, min, max);
1337 return 0;
1338 }
1339 }
1340 }
1341 return 1;
1342}
1343
1344static int
1345bfd_verify_pkt_auth_key_sha1 (const bfd_pkt_t * pkt, u32 pkt_size,
1346 bfd_session_t * bs, u8 bfd_key_id,
1347 bfd_auth_key_t * auth_key)
1348{
1349 ASSERT (auth_key->auth_type == BFD_AUTH_TYPE_keyed_sha1 ||
1350 auth_key->auth_type == BFD_AUTH_TYPE_meticulous_keyed_sha1);
1351
1352 u8 result[SHA_DIGEST_LENGTH];
1353 bfd_pkt_with_common_auth_t *with_common = (void *) pkt;
1354 if (pkt_size < sizeof (*with_common))
1355 {
1356 BFD_ERR ("Packet size too small to hold authentication common header");
1357 return 0;
1358 }
1359 if (with_common->common_auth.type != auth_key->auth_type)
1360 {
1361 BFD_ERR ("BFD auth type mismatch, packet auth=%d:%s doesn't match "
1362 "in-use auth=%d:%s",
1363 with_common->common_auth.type,
1364 bfd_auth_type_str (with_common->common_auth.type),
1365 auth_key->auth_type, bfd_auth_type_str (auth_key->auth_type));
1366 return 0;
1367 }
1368 bfd_pkt_with_sha1_auth_t *with_sha1 = (void *) pkt;
1369 if (pkt_size < sizeof (*with_sha1) ||
1370 with_sha1->sha1_auth.type_len.len < sizeof (with_sha1->sha1_auth))
1371 {
1372 BFD_ERR
1373 ("BFD size mismatch, payload size=%u, expected=%u, auth_len=%u, "
1374 "expected=%u", pkt_size, sizeof (*with_sha1),
1375 with_sha1->sha1_auth.type_len.len, sizeof (with_sha1->sha1_auth));
1376 return 0;
1377 }
1378 if (with_sha1->sha1_auth.key_id != bfd_key_id)
1379 {
1380 BFD_ERR
1381 ("BFD key ID mismatch, packet key ID=%u doesn't match key ID=%u%s",
1382 with_sha1->sha1_auth.key_id, bfd_key_id,
1383 bs->
1384 auth.is_delayed ? " (but a delayed auth change is scheduled)" : "");
1385 return 0;
1386 }
1387 SHA_CTX ctx;
1388 if (!SHA1_Init (&ctx))
1389 {
1390 BFD_ERR ("SHA1_Init failed");
1391 return 0;
1392 }
1393 /* ignore last 20 bytes - use the actual key data instead pkt data */
1394 if (!SHA1_Update (&ctx, with_sha1,
1395 sizeof (*with_sha1) - sizeof (with_sha1->sha1_auth.hash)))
1396 {
1397 BFD_ERR ("SHA1_Update failed");
1398 return 0;
1399 }
1400 if (!SHA1_Update (&ctx, auth_key->key, sizeof (auth_key->key)))
1401 {
1402 BFD_ERR ("SHA1_Update failed");
1403 return 0;
1404 }
1405 if (!SHA1_Final (result, &ctx))
1406 {
1407 BFD_ERR ("SHA1_Final failed");
1408 return 0;
1409 }
1410 if (0 == memcmp (result, with_sha1->sha1_auth.hash, SHA_DIGEST_LENGTH))
1411 {
1412 return 1;
1413 }
1414 BFD_ERR ("SHA1 hash: %U doesn't match the expected value: %U",
1415 format_hex_bytes, with_sha1->sha1_auth.hash, SHA_DIGEST_LENGTH,
1416 format_hex_bytes, result, SHA_DIGEST_LENGTH);
1417 return 0;
1418}
1419
1420static int
1421bfd_verify_pkt_auth_key (const bfd_pkt_t * pkt, u32 pkt_size,
1422 bfd_session_t * bs, u8 bfd_key_id,
1423 bfd_auth_key_t * auth_key)
1424{
1425 switch (auth_key->auth_type)
1426 {
1427 case BFD_AUTH_TYPE_reserved:
1428 clib_warning ("Internal error, unexpected auth_type=%d:%s",
1429 auth_key->auth_type,
1430 bfd_auth_type_str (auth_key->auth_type));
1431 return 0;
1432 case BFD_AUTH_TYPE_simple_password:
1433 clib_warning
1434 ("Internal error, not implemented, unexpected auth_type=%d:%s",
1435 auth_key->auth_type, bfd_auth_type_str (auth_key->auth_type));
1436 return 0;
1437 case BFD_AUTH_TYPE_keyed_md5:
1438 /* fallthrough */
1439 case BFD_AUTH_TYPE_meticulous_keyed_md5:
1440 clib_warning
1441 ("Internal error, not implemented, unexpected auth_type=%d:%s",
1442 auth_key->auth_type, bfd_auth_type_str (auth_key->auth_type));
1443 return 0;
1444 case BFD_AUTH_TYPE_keyed_sha1:
1445 /* fallthrough */
1446 case BFD_AUTH_TYPE_meticulous_keyed_sha1:
1447#if WITH_LIBSSL > 0
1448 do
1449 {
1450 const u32 seq_num = clib_net_to_host_u32 (((bfd_pkt_with_sha1_auth_t
1451 *) pkt)->
1452 sha1_auth.seq_num);
1453 return bfd_verify_pkt_auth_seq_num (bs, seq_num,
1454 bfd_auth_type_is_meticulous
1455 (auth_key->auth_type))
1456 && bfd_verify_pkt_auth_key_sha1 (pkt, pkt_size, bs, bfd_key_id,
1457 auth_key);
1458 }
1459 while (0);
1460#else
1461 clib_warning
1462 ("Internal error, attempt to use SHA1 without SSL support");
1463 return 0;
1464#endif
1465 }
1466 return 0;
1467}
1468
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001469/**
1470 * @brief verify bfd packet - authentication
1471 *
1472 * @param pkt
1473 *
1474 * @return 1 if bfd packet is valid
1475 */
1476int
Klement Sekerab17dd962017-01-09 07:43:48 +01001477bfd_verify_pkt_auth (const bfd_pkt_t * pkt, u16 pkt_size, bfd_session_t * bs)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001478{
Klement Sekerab17dd962017-01-09 07:43:48 +01001479 if (bfd_pkt_get_auth_present (pkt))
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001480 {
Klement Sekerab17dd962017-01-09 07:43:48 +01001481 /* authentication present in packet */
1482 if (!bs->auth.curr_key)
1483 {
1484 /* currently not using authentication - can we turn it on? */
1485 if (bs->auth.is_delayed && bs->auth.next_key)
1486 {
1487 /* yes, switch is scheduled - make sure the auth is valid */
1488 if (bfd_verify_pkt_auth_key (pkt, pkt_size, bs,
1489 bs->auth.next_bfd_key_id,
1490 bs->auth.next_key))
1491 {
1492 /* auth matches next key, do the switch, packet is valid */
1493 bfd_session_switch_auth_to_next (bs);
1494 return 1;
1495 }
1496 }
1497 }
1498 else
1499 {
1500 /* yes, using authentication, verify the key */
1501 if (bfd_verify_pkt_auth_key (pkt, pkt_size, bs,
1502 bs->auth.curr_bfd_key_id,
1503 bs->auth.curr_key))
1504 {
1505 /* verification passed, packet is valid */
1506 return 1;
1507 }
1508 else
1509 {
1510 /* verification failed - but maybe we need to switch key */
1511 if (bs->auth.is_delayed && bs->auth.next_key)
1512 {
1513 /* delayed switch present, verify if that key works */
1514 if (bfd_verify_pkt_auth_key (pkt, pkt_size, bs,
1515 bs->auth.next_bfd_key_id,
1516 bs->auth.next_key))
1517 {
1518 /* auth matches next key, switch key, packet is valid */
1519 bfd_session_switch_auth_to_next (bs);
1520 return 1;
1521 }
1522 }
1523 }
1524 }
1525 }
1526 else
1527 {
1528 /* authentication in packet not present */
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001529 if (pkt_size > sizeof (*pkt))
1530 {
1531 BFD_ERR ("BFD verification failed - unexpected packet size '%d' "
1532 "(auth not present)", pkt_size);
1533 return 0;
1534 }
Klement Sekerab17dd962017-01-09 07:43:48 +01001535 if (bs->auth.curr_key)
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001536 {
Klement Sekerab17dd962017-01-09 07:43:48 +01001537 /* currently authenticating - could we turn it off? */
1538 if (bs->auth.is_delayed && !bs->auth.next_key)
1539 {
1540 /* yes, delayed switch to NULL key is scheduled */
1541 bfd_session_switch_auth_to_next (bs);
1542 return 1;
1543 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001544 }
Klement Sekerab17dd962017-01-09 07:43:48 +01001545 else
1546 {
1547 /* no auth in packet, no auth in use - packet is valid */
1548 return 1;
1549 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001550 }
Klement Sekerab17dd962017-01-09 07:43:48 +01001551 return 0;
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001552}
1553
1554void
1555bfd_consume_pkt (bfd_main_t * bm, const bfd_pkt_t * pkt, u32 bs_idx)
1556{
1557 bfd_session_t *bs = bfd_find_session_by_idx (bm, bs_idx);
Klement Sekera0e2e0df2017-03-03 08:51:08 +01001558 if (!bs || (pkt->your_disc && pkt->your_disc != bs->local_discr))
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001559 {
1560 return;
1561 }
1562 BFD_DBG ("Scanning bfd packet, bs_idx=%d", bs->bs_idx);
1563 bs->remote_discr = pkt->my_disc;
1564 bs->remote_state = bfd_pkt_get_state (pkt);
1565 bs->remote_demand = bfd_pkt_get_demand (pkt);
Klement Sekera73884482017-02-23 09:26:30 +01001566 bs->remote_diag = bfd_pkt_get_diag_code (pkt);
Klement Sekera637b9c42016-12-08 05:19:14 +01001567 u64 now = clib_cpu_time_now ();
1568 bs->last_rx_clocks = now;
Klement Sekerab17dd962017-01-09 07:43:48 +01001569 if (bfd_pkt_get_auth_present (pkt))
1570 {
1571 bfd_auth_type_e auth_type =
1572 ((bfd_pkt_with_common_auth_t *) (pkt))->common_auth.type;
1573 switch (auth_type)
1574 {
1575 case BFD_AUTH_TYPE_reserved:
1576 /* fallthrough */
1577 case BFD_AUTH_TYPE_simple_password:
1578 /* fallthrough */
1579 case BFD_AUTH_TYPE_keyed_md5:
1580 /* fallthrough */
1581 case BFD_AUTH_TYPE_meticulous_keyed_md5:
1582 clib_warning ("Internal error, unexpected auth_type=%d:%s",
1583 auth_type, bfd_auth_type_str (auth_type));
1584 break;
1585 case BFD_AUTH_TYPE_keyed_sha1:
1586 /* fallthrough */
1587 case BFD_AUTH_TYPE_meticulous_keyed_sha1:
1588 do
1589 {
1590 bfd_pkt_with_sha1_auth_t *with_sha1 =
1591 (bfd_pkt_with_sha1_auth_t *) pkt;
1592 bs->auth.remote_seq_number =
1593 clib_net_to_host_u32 (with_sha1->sha1_auth.seq_num);
1594 bs->auth.remote_seq_number_known = 1;
1595 BFD_DBG ("Received sequence number %u",
1596 bs->auth.remote_seq_number);
1597 }
1598 while (0);
1599 }
1600 }
Klement Sekeraa57a9702017-02-02 06:58:07 +01001601 bs->remote_desired_min_tx_clocks =
1602 bfd_usec_to_clocks (bm, clib_net_to_host_u32 (pkt->des_min_tx));
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001603 bs->remote_detect_mult = pkt->head.detect_mult;
Klement Sekera637b9c42016-12-08 05:19:14 +01001604 bfd_set_remote_required_min_rx (bm, bs, now,
Klement Sekera239790f2017-02-16 10:53:53 +01001605 clib_net_to_host_u32 (pkt->req_min_rx));
1606 bfd_set_remote_required_min_echo_rx (bm, bs, now,
1607 clib_net_to_host_u32
1608 (pkt->req_min_echo_rx));
Klement Sekera239790f2017-02-16 10:53:53 +01001609 if (bfd_pkt_get_final (pkt))
Klement Sekeraa57a9702017-02-02 06:58:07 +01001610 {
Klement Sekera239790f2017-02-16 10:53:53 +01001611 if (BFD_POLL_IN_PROGRESS == bs->poll_state)
Klement Sekeraa57a9702017-02-02 06:58:07 +01001612 {
Klement Sekera239790f2017-02-16 10:53:53 +01001613 BFD_DBG ("Poll sequence terminated, bs_idx=%u", bs->bs_idx);
1614 bfd_set_poll_state (bs, BFD_POLL_NOT_NEEDED);
1615 if (BFD_STATE_up == bs->local_state)
1616 {
Klement Sekera73884482017-02-23 09:26:30 +01001617 bfd_set_effective_required_min_rx (bm, bs,
Klement Sekera239790f2017-02-16 10:53:53 +01001618 clib_max (bs->echo *
1619 bm->min_required_min_rx_while_echo_clocks,
1620 bs->config_required_min_rx_clocks));
1621 }
1622 }
1623 else if (BFD_POLL_IN_PROGRESS_AND_QUEUED == bs->poll_state)
1624 {
1625 /*
1626 * next poll sequence must be delayed by at least the round trip
1627 * time, so calculate that here
1628 */
1629 BFD_DBG ("Next poll sequence can commence in " BFD_CLK_FMT,
1630 BFD_CLK_PRN (now -
1631 bs->poll_state_start_or_timeout_clocks));
1632 bs->poll_state_start_or_timeout_clocks =
1633 now + (now - bs->poll_state_start_or_timeout_clocks);
1634 BFD_DBG
1635 ("Poll sequence terminated, but another is needed, bs_idx=%u",
1636 bs->bs_idx);
1637 bfd_set_poll_state (bs, BFD_POLL_NEEDED);
Klement Sekeraa57a9702017-02-02 06:58:07 +01001638 }
1639 }
Klement Sekera239790f2017-02-16 10:53:53 +01001640 bfd_calc_next_tx (bm, bs, now);
1641 bfd_set_timer (bm, bs, now, 0);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001642 if (BFD_STATE_admin_down == bs->local_state)
Klement Sekerac48829b2017-02-14 07:55:57 +01001643 {
1644 BFD_DBG ("Session is admin-down, ignoring packet, bs_idx=%u",
1645 bs->bs_idx);
1646 return;
1647 }
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001648 if (BFD_STATE_admin_down == bs->remote_state)
1649 {
1650 bfd_set_diag (bs, BFD_DIAG_CODE_neighbor_sig_down);
Klement Sekerae4504c62016-12-08 10:16:41 +01001651 bfd_set_state (bm, bs, BFD_STATE_down, 0);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001652 }
1653 else if (BFD_STATE_down == bs->local_state)
1654 {
1655 if (BFD_STATE_down == bs->remote_state)
1656 {
Klement Sekerae4504c62016-12-08 10:16:41 +01001657 bfd_set_state (bm, bs, BFD_STATE_init, 0);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001658 }
1659 else if (BFD_STATE_init == bs->remote_state)
1660 {
Klement Sekerae4504c62016-12-08 10:16:41 +01001661 bfd_set_state (bm, bs, BFD_STATE_up, 0);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001662 }
1663 }
1664 else if (BFD_STATE_init == bs->local_state)
1665 {
1666 if (BFD_STATE_up == bs->remote_state ||
1667 BFD_STATE_init == bs->remote_state)
1668 {
Klement Sekerae4504c62016-12-08 10:16:41 +01001669 bfd_set_state (bm, bs, BFD_STATE_up, 0);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001670 }
1671 }
1672 else /* BFD_STATE_up == bs->local_state */
1673 {
1674 if (BFD_STATE_down == bs->remote_state)
1675 {
1676 bfd_set_diag (bs, BFD_DIAG_CODE_neighbor_sig_down);
Klement Sekerae4504c62016-12-08 10:16:41 +01001677 bfd_set_state (bm, bs, BFD_STATE_down, 0);
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001678 }
1679 }
1680}
1681
Klement Sekera239790f2017-02-16 10:53:53 +01001682int
1683bfd_consume_echo_pkt (bfd_main_t * bm, vlib_buffer_t * b)
Klement Sekeraa57a9702017-02-02 06:58:07 +01001684{
Klement Sekera239790f2017-02-16 10:53:53 +01001685 bfd_echo_pkt_t *pkt = NULL;
1686 if (b->current_length != sizeof (*pkt))
Klement Sekeraa57a9702017-02-02 06:58:07 +01001687 {
Klement Sekera239790f2017-02-16 10:53:53 +01001688 return 0;
Klement Sekeraa57a9702017-02-02 06:58:07 +01001689 }
Klement Sekera239790f2017-02-16 10:53:53 +01001690 pkt = vlib_buffer_get_current (b);
1691 bfd_session_t *bs = bfd_find_session_by_disc (bm, pkt->discriminator);
1692 if (!bs)
1693 {
1694 return 0;
1695 }
1696 BFD_DBG ("Scanning bfd echo packet, bs_idx=%d", bs->bs_idx);
1697 u64 checksum =
1698 bfd_calc_echo_checksum (bs->local_discr, pkt->expire_time_clocks,
1699 bs->echo_secret);
1700 if (checksum != pkt->checksum)
1701 {
1702 BFD_DBG ("Invalid echo packet, checksum mismatch");
1703 return 1;
1704 }
1705 u64 now = clib_cpu_time_now ();
1706 if (pkt->expire_time_clocks < now)
1707 {
1708 BFD_DBG ("Stale packet received, expire time %lu < now %lu",
1709 pkt->expire_time_clocks, now);
1710 }
1711 else
1712 {
1713 bs->echo_last_rx_clocks = now;
1714 }
1715 return 1;
Klement Sekeraa57a9702017-02-02 06:58:07 +01001716}
1717
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001718u8 *
1719format_bfd_session (u8 * s, va_list * args)
1720{
1721 const bfd_session_t *bs = va_arg (*args, bfd_session_t *);
Klement Sekera239790f2017-02-16 10:53:53 +01001722 uword indent = format_get_indent (s);
1723 s = format (s, "bs_idx=%u local-state=%s remote-state=%s\n"
1724 "%Ulocal-discriminator=%u remote-discriminator=%u\n"
1725 "%Ulocal-diag=%s echo-active=%s\n"
1726 "%Udesired-min-tx=%u required-min-rx=%u\n"
1727 "%Urequired-min-echo-rx=%u detect-mult=%u\n"
1728 "%Uremote-min-rx=%u remote-min-echo-rx=%u\n"
1729 "%Uremote-demand=%s poll-state=%s\n"
1730 "%Uauth: local-seq-num=%u remote-seq-num=%u\n"
1731 "%U is-delayed=%s\n"
1732 "%U curr-key=%U\n"
1733 "%U next-key=%U",
Klement Sekerab17dd962017-01-09 07:43:48 +01001734 bs->bs_idx, bfd_state_string (bs->local_state),
Klement Sekera239790f2017-02-16 10:53:53 +01001735 bfd_state_string (bs->remote_state), format_white_space, indent,
1736 bs->local_discr, bs->remote_discr, format_white_space, indent,
1737 bfd_diag_code_string (bs->local_diag),
1738 (bs->echo ? "yes" : "no"), format_white_space, indent,
Klement Sekeraa57a9702017-02-02 06:58:07 +01001739 bs->config_desired_min_tx_usec, bs->config_required_min_rx_usec,
Klement Sekera239790f2017-02-16 10:53:53 +01001740 format_white_space, indent, 1, bs->local_detect_mult,
1741 format_white_space, indent, bs->remote_min_rx_usec,
1742 bs->remote_min_echo_rx_usec, format_white_space, indent,
1743 (bs->remote_demand ? "yes" : "no"),
1744 bfd_poll_state_string (bs->poll_state), format_white_space,
1745 indent, bs->auth.local_seq_number, bs->auth.remote_seq_number,
1746 format_white_space, indent,
1747 (bs->auth.is_delayed ? "yes" : "no"), format_white_space,
1748 indent, format_bfd_auth_key, bs->auth.curr_key,
1749 format_white_space, indent, format_bfd_auth_key,
1750 bs->auth.next_key);
Klement Sekerab17dd962017-01-09 07:43:48 +01001751 return s;
1752}
1753
1754unsigned
1755bfd_auth_type_supported (bfd_auth_type_e auth_type)
1756{
1757 if (auth_type == BFD_AUTH_TYPE_keyed_sha1 ||
1758 auth_type == BFD_AUTH_TYPE_meticulous_keyed_sha1)
1759 {
1760 return 1;
1761 }
1762 return 0;
1763}
1764
1765vnet_api_error_t
1766bfd_auth_activate (bfd_session_t * bs, u32 conf_key_id,
1767 u8 bfd_key_id, u8 is_delayed)
1768{
1769 bfd_main_t *bm = &bfd_main;
1770 const uword *key_idx_p =
1771 hash_get (bm->auth_key_by_conf_key_id, conf_key_id);
1772 if (!key_idx_p)
1773 {
1774 clib_warning ("Authentication key with config ID %u doesn't exist)",
1775 conf_key_id);
1776 return VNET_API_ERROR_BFD_ENOENT;
1777 }
1778 const uword key_idx = *key_idx_p;
1779 bfd_auth_key_t *key = pool_elt_at_index (bm->auth_keys, key_idx);
1780 if (is_delayed)
1781 {
1782 if (bs->auth.next_key == key)
1783 {
1784 /* already using this key, no changes required */
1785 return 0;
1786 }
1787 bs->auth.next_key = key;
1788 bs->auth.next_bfd_key_id = bfd_key_id;
1789 bs->auth.is_delayed = 1;
1790 }
1791 else
1792 {
1793 if (bs->auth.curr_key == key)
1794 {
1795 /* already using this key, no changes required */
1796 return 0;
1797 }
1798 if (bs->auth.curr_key)
1799 {
1800 --bs->auth.curr_key->use_count;
1801 }
1802 bs->auth.curr_key = key;
1803 bs->auth.curr_bfd_key_id = bfd_key_id;
1804 bs->auth.is_delayed = 0;
1805 }
1806 ++key->use_count;
Klement Sekera239790f2017-02-16 10:53:53 +01001807 BFD_DBG ("\nSession auth modified: %U", format_bfd_session, bs);
Klement Sekerab17dd962017-01-09 07:43:48 +01001808 return 0;
1809}
1810
1811vnet_api_error_t
1812bfd_auth_deactivate (bfd_session_t * bs, u8 is_delayed)
1813{
1814#if WITH_LIBSSL > 0
1815 if (!is_delayed)
1816 {
1817 /* not delayed - deactivate the current key right now */
1818 if (bs->auth.curr_key)
1819 {
1820 --bs->auth.curr_key->use_count;
1821 bs->auth.curr_key = NULL;
1822 }
1823 bs->auth.is_delayed = 0;
1824 }
1825 else
1826 {
1827 /* delayed - mark as so */
1828 bs->auth.is_delayed = 1;
1829 }
1830 /*
1831 * clear the next key unconditionally - either the auth change is not delayed
1832 * in which case the caller expects the session to not use authentication
1833 * from this point forward, or it is delayed, in which case the next_key
1834 * needs to be set to NULL to make it so in the future
1835 */
1836 if (bs->auth.next_key)
1837 {
1838 --bs->auth.next_key->use_count;
1839 bs->auth.next_key = NULL;
1840 }
Klement Sekera239790f2017-02-16 10:53:53 +01001841 BFD_DBG ("\nSession auth modified: %U", format_bfd_session, bs);
Klement Sekerab17dd962017-01-09 07:43:48 +01001842 return 0;
1843#else
1844 clib_warning ("SSL missing, cannot deactivate BFD authentication");
1845 return VNET_API_ERROR_BFD_NOTSUPP;
1846#endif
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001847}
1848
Klement Sekeraa57a9702017-02-02 06:58:07 +01001849vnet_api_error_t
1850bfd_session_set_params (bfd_main_t * bm, bfd_session_t * bs,
1851 u32 desired_min_tx_usec,
1852 u32 required_min_rx_usec, u8 detect_mult)
1853{
1854 if (bs->local_detect_mult != detect_mult ||
1855 bs->config_desired_min_tx_usec != desired_min_tx_usec ||
1856 bs->config_required_min_rx_usec != required_min_rx_usec)
1857 {
Klement Sekera239790f2017-02-16 10:53:53 +01001858 BFD_DBG ("\nChanging session params: %U", format_bfd_session, bs);
Klement Sekeraa57a9702017-02-02 06:58:07 +01001859 switch (bs->poll_state)
1860 {
Klement Sekera239790f2017-02-16 10:53:53 +01001861 case BFD_POLL_NOT_NEEDED:
Klement Sekeraa57a9702017-02-02 06:58:07 +01001862 if (BFD_STATE_up == bs->local_state ||
1863 BFD_STATE_init == bs->local_state)
1864 {
1865 /* poll sequence is not needed for detect multiplier change */
1866 if (bs->config_desired_min_tx_usec != desired_min_tx_usec ||
1867 bs->config_required_min_rx_usec != required_min_rx_usec)
1868 {
Klement Sekera239790f2017-02-16 10:53:53 +01001869 bfd_set_poll_state (bs, BFD_POLL_NEEDED);
Klement Sekeraa57a9702017-02-02 06:58:07 +01001870 }
1871 }
1872 break;
Klement Sekera239790f2017-02-16 10:53:53 +01001873 case BFD_POLL_NEEDED:
1874 case BFD_POLL_IN_PROGRESS_AND_QUEUED:
1875 /*
1876 * nothing to do - will be handled in the future poll which is
1877 * already scheduled for execution
1878 */
Klement Sekeraa57a9702017-02-02 06:58:07 +01001879 break;
Klement Sekera239790f2017-02-16 10:53:53 +01001880 case BFD_POLL_IN_PROGRESS:
1881 /* poll sequence is not needed for detect multiplier change */
1882 if (bs->config_desired_min_tx_usec != desired_min_tx_usec ||
1883 bs->config_required_min_rx_usec != required_min_rx_usec)
1884 {
1885 BFD_DBG ("Poll in progress, queueing extra poll, bs_idx=%u",
1886 bs->bs_idx);
1887 bfd_set_poll_state (bs, BFD_POLL_IN_PROGRESS_AND_QUEUED);
1888 }
Klement Sekeraa57a9702017-02-02 06:58:07 +01001889 }
1890
1891 bs->local_detect_mult = detect_mult;
1892 bs->config_desired_min_tx_usec = desired_min_tx_usec;
1893 bs->config_desired_min_tx_clocks =
1894 bfd_usec_to_clocks (bm, desired_min_tx_usec);
1895 bs->config_required_min_rx_usec = required_min_rx_usec;
1896 bs->config_required_min_rx_clocks =
1897 bfd_usec_to_clocks (bm, required_min_rx_usec);
Klement Sekera239790f2017-02-16 10:53:53 +01001898 BFD_DBG ("\nChanged session params: %U", format_bfd_session, bs);
Klement Sekeraa57a9702017-02-02 06:58:07 +01001899
1900 vlib_process_signal_event (bm->vlib_main, bm->bfd_process_node_index,
1901 BFD_EVENT_CONFIG_CHANGED, bs->bs_idx);
1902 }
1903 else
1904 {
1905 BFD_DBG ("Ignore parameter change - no change, bs_idx=%u", bs->bs_idx);
1906 }
1907 return 0;
1908}
1909
Klement Sekera73884482017-02-23 09:26:30 +01001910vnet_api_error_t
1911bfd_auth_set_key (u32 conf_key_id, u8 auth_type, u8 key_len,
1912 const u8 * key_data)
1913{
1914#if WITH_LIBSSL > 0
1915 bfd_auth_key_t *auth_key = NULL;
Klement Sekerab16bfe32017-02-28 11:56:48 +01001916 if (!key_len || key_len > bfd_max_key_len_for_auth_type (auth_type))
Klement Sekera73884482017-02-23 09:26:30 +01001917 {
1918 clib_warning ("Invalid authentication key length for auth_type=%d:%s "
1919 "(key_len=%u, must be "
1920 "non-zero, expected max=%u)",
1921 auth_type, bfd_auth_type_str (auth_type), key_len,
Klement Sekerab16bfe32017-02-28 11:56:48 +01001922 (u32) bfd_max_key_len_for_auth_type (auth_type));
Klement Sekera73884482017-02-23 09:26:30 +01001923 return VNET_API_ERROR_INVALID_VALUE;
1924 }
1925 if (!bfd_auth_type_supported (auth_type))
1926 {
1927 clib_warning ("Unsupported auth type=%d:%s", auth_type,
1928 bfd_auth_type_str (auth_type));
1929 return VNET_API_ERROR_BFD_NOTSUPP;
1930 }
1931 bfd_main_t *bm = &bfd_main;
1932 uword *key_idx_p = hash_get (bm->auth_key_by_conf_key_id, conf_key_id);
1933 if (key_idx_p)
1934 {
1935 /* modifying existing key - must not be used */
1936 const uword key_idx = *key_idx_p;
1937 auth_key = pool_elt_at_index (bm->auth_keys, key_idx);
1938 if (auth_key->use_count > 0)
1939 {
1940 clib_warning ("Authentication key with conf ID %u in use by %u BFD "
1941 "session(s) - cannot modify",
1942 conf_key_id, auth_key->use_count);
1943 return VNET_API_ERROR_BFD_EINUSE;
1944 }
1945 }
1946 else
1947 {
1948 /* adding new key */
1949 pool_get (bm->auth_keys, auth_key);
1950 auth_key->conf_key_id = conf_key_id;
1951 hash_set (bm->auth_key_by_conf_key_id, conf_key_id,
1952 auth_key - bm->auth_keys);
1953 }
1954 auth_key->auth_type = auth_type;
1955 memset (auth_key->key, 0, sizeof (auth_key->key));
1956 clib_memcpy (auth_key->key, key_data, key_len);
1957 return 0;
1958#else
1959 clib_warning ("SSL missing, cannot manipulate authentication keys");
1960 return VNET_API_ERROR_BFD_NOTSUPP;
1961#endif
1962}
1963
1964vnet_api_error_t
1965bfd_auth_del_key (u32 conf_key_id)
1966{
1967#if WITH_LIBSSL > 0
1968 bfd_auth_key_t *auth_key = NULL;
1969 bfd_main_t *bm = &bfd_main;
1970 uword *key_idx_p = hash_get (bm->auth_key_by_conf_key_id, conf_key_id);
1971 if (key_idx_p)
1972 {
1973 /* deleting existing key - must not be used */
1974 const uword key_idx = *key_idx_p;
1975 auth_key = pool_elt_at_index (bm->auth_keys, key_idx);
1976 if (auth_key->use_count > 0)
1977 {
1978 clib_warning ("Authentication key with conf ID %u in use by %u BFD "
1979 "session(s) - cannot delete",
1980 conf_key_id, auth_key->use_count);
1981 return VNET_API_ERROR_BFD_EINUSE;
1982 }
1983 hash_unset (bm->auth_key_by_conf_key_id, conf_key_id);
1984 memset (auth_key, 0, sizeof (*auth_key));
1985 pool_put (bm->auth_keys, auth_key);
1986 }
1987 else
1988 {
1989 /* no such key */
1990 clib_warning ("Authentication key with conf ID %u does not exist",
1991 conf_key_id);
1992 return VNET_API_ERROR_BFD_ENOENT;
1993 }
1994 return 0;
1995#else
1996 clib_warning ("SSL missing, cannot manipulate authentication keys");
1997 return VNET_API_ERROR_BFD_NOTSUPP;
1998#endif
1999}
2000
Klement Sekera0e3c0de2016-09-29 14:43:44 +02002001bfd_main_t bfd_main;
2002
2003/*
2004 * fd.io coding-style-patch-verification: ON
2005 *
2006 * Local Variables:
2007 * eval: (c-set-style "gnu")
2008 * End:
2009 */