Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1 | /* |
| 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 Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 20 | #if WITH_LIBSSL > 0 |
| 21 | #include <openssl/sha.h> |
| 22 | #endif |
| 23 | |
| 24 | #if __SSE4_2__ |
| 25 | #include <x86intrin.h> |
| 26 | #endif |
| 27 | |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame^] | 28 | #include <vlibmemory/api.h> |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 29 | #include <vppinfra/random.h> |
| 30 | #include <vppinfra/error.h> |
| 31 | #include <vppinfra/hash.h> |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 32 | #include <vppinfra/xxhash.h> |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 33 | #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 Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 38 | #include <vlib/log.h> |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 39 | |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame^] | 40 | u32 oingoes; |
| 41 | |
| 42 | void |
| 43 | oingo (void) |
| 44 | { |
| 45 | oingoes++; |
| 46 | } |
| 47 | |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 48 | static u64 |
| 49 | bfd_calc_echo_checksum (u32 discriminator, u64 expire_time, u32 secret) |
| 50 | { |
| 51 | u64 checksum = 0; |
Gabriel Ganne | 8e66b9b | 2017-12-14 16:20:37 +0100 | [diff] [blame] | 52 | #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 Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 56 | #else |
| 57 | checksum = clib_xxhash (discriminator ^ expire_time ^ secret); |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 58 | #endif |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 59 | return checksum; |
| 60 | } |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 61 | |
| 62 | static u64 |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 63 | bfd_usec_to_clocks (const bfd_main_t * bm, u64 us) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 64 | { |
| 65 | return bm->cpu_cps * ((f64) us / USEC_PER_SECOND); |
| 66 | } |
| 67 | |
Klement Sekera | 7388448 | 2017-02-23 09:26:30 +0100 | [diff] [blame] | 68 | u32 |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 69 | bfd_clocks_to_usec (const bfd_main_t * bm, u64 clocks) |
| 70 | { |
| 71 | return (clocks / bm->cpu_cps) * USEC_PER_SECOND; |
| 72 | } |
| 73 | |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 74 | static vlib_node_registration_t bfd_process_node; |
| 75 | |
Klement Sekera | 7388448 | 2017-02-23 09:26:30 +0100 | [diff] [blame] | 76 | u8 * |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 77 | format_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 Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 93 | /* |
| 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 | */ |
| 98 | typedef enum |
| 99 | { |
| 100 | BFD_INPUT_NEXT_NORMAL, |
| 101 | BFD_INPUT_N_NEXT, |
| 102 | } bfd_input_next_t; |
| 103 | |
Klement Sekera | e4504c6 | 2016-12-08 10:16:41 +0100 | [diff] [blame] | 104 | static void bfd_on_state_change (bfd_main_t * bm, bfd_session_t * bs, u64 now, |
| 105 | int handling_wakeup); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 106 | |
| 107 | static void |
| 108 | bfd_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 Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 113 | bs->remote_discr = 0; |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 114 | bs->hop_type = BFD_HOP_TYPE_SINGLE; |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 115 | bs->config_desired_min_tx_usec = BFD_DEFAULT_DESIRED_MIN_TX_USEC; |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 116 | 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 Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 120 | bs->remote_min_echo_rx_usec = 0; |
| 121 | bs->remote_min_echo_rx_clocks = 0; |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 122 | bs->remote_demand = 0; |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 123 | 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 Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 126 | bs->echo_secret = random_u32 (&bm->random_seed); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | static void |
| 130 | bfd_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 | |
| 140 | static void |
Klement Sekera | e4504c6 | 2016-12-08 10:16:41 +0100 | [diff] [blame] | 141 | bfd_set_state (bfd_main_t * bm, bfd_session_t * bs, |
| 142 | bfd_state_e new_state, int handling_wakeup) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 143 | { |
| 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 Sekera | e4504c6 | 2016-12-08 10:16:41 +0100 | [diff] [blame] | 150 | bfd_on_state_change (bm, bs, clib_cpu_time_now (), handling_wakeup); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | |
Klement Sekera | 7388448 | 2017-02-23 09:26:30 +0100 | [diff] [blame] | 154 | const char * |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 155 | bfd_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 | |
| 168 | static void |
| 169 | bfd_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 Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 179 | static void |
| 180 | bfd_recalc_tx_interval (bfd_main_t * bm, bfd_session_t * bs) |
| 181 | { |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 182 | 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 | |
| 188 | static void |
| 189 | bfd_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 Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | static void |
| 199 | bfd_calc_next_tx (bfd_main_t * bm, bfd_session_t * bs, u64 now) |
| 200 | { |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 201 | if (bs->local_detect_mult > 1) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 202 | { |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 203 | /* 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 Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 208 | { |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 209 | /* |
| 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 Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | else |
| 221 | { |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 222 | /* 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 Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 238 | } |
| 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 | |
| 248 | static void |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 249 | bfd_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 | |
| 269 | static void |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 270 | bfd_recalc_detection_time (bfd_main_t * bm, bfd_session_t * bs) |
| 271 | { |
Klement Sekera | 7388448 | 2017-02-23 09:26:30 +0100 | [diff] [blame] | 272 | 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 Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | static void |
Klement Sekera | e4504c6 | 2016-12-08 10:16:41 +0100 | [diff] [blame] | 285 | bfd_set_timer (bfd_main_t * bm, bfd_session_t * bs, u64 now, |
| 286 | int handling_wakeup) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 287 | { |
| 288 | u64 next = 0; |
| 289 | u64 rx_timeout = 0; |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 290 | u64 tx_timeout = 0; |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 291 | if (BFD_STATE_up == bs->local_state) |
| 292 | { |
| 293 | rx_timeout = bs->last_rx_clocks + bs->detection_time_clocks; |
| 294 | } |
Klement Sekera | 7388448 | 2017-02-23 09:26:30 +0100 | [diff] [blame] | 295 | if (BFD_STATE_up != bs->local_state || |
| 296 | (!bs->remote_demand && bs->remote_min_rx_usec) || |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 297 | BFD_POLL_NOT_NEEDED != bs->poll_state) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 298 | { |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 299 | tx_timeout = bs->tx_timeout_clocks; |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 300 | } |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 301 | if (tx_timeout && rx_timeout) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 302 | { |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 303 | next = clib_min (tx_timeout, rx_timeout); |
| 304 | } |
| 305 | else if (tx_timeout) |
| 306 | { |
| 307 | next = tx_timeout; |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 308 | } |
| 309 | else if (rx_timeout) |
| 310 | { |
| 311 | next = rx_timeout; |
| 312 | } |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 313 | 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 Sekera | 637b9c4 | 2016-12-08 05:19:14 +0100 | [diff] [blame] | 322 | /* 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 Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 326 | { |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame^] | 327 | int send_signal = 0; |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 328 | 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 Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame^] | 335 | bfd_lock (bm); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 336 | timing_wheel_insert (&bm->wheel, bs->wheel_time_clocks, bs->bs_idx); |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame^] | 337 | |
Klement Sekera | e4504c6 | 2016-12-08 10:16:41 +0100 | [diff] [blame] | 338 | if (!handling_wakeup) |
| 339 | { |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame^] | 340 | |
| 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 Sekera | e4504c6 | 2016-12-08 10:16:41 +0100 | [diff] [blame] | 370 | } |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 371 | } |
| 372 | } |
| 373 | |
| 374 | static void |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 375 | bfd_set_effective_desired_min_tx (bfd_main_t * bm, |
| 376 | bfd_session_t * bs, u64 now, |
Klement Sekera | aeeac3b | 2017-02-14 07:11:52 +0100 | [diff] [blame] | 377 | u64 desired_min_tx_clocks) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 378 | { |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 379 | 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 Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 382 | bfd_recalc_detection_time (bm, bs); |
| 383 | bfd_recalc_tx_interval (bm, bs); |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 384 | bfd_recalc_echo_tx_interval (bm, bs); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 385 | bfd_calc_next_tx (bm, bs, now); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 386 | } |
| 387 | |
Klement Sekera | 637b9c4 | 2016-12-08 05:19:14 +0100 | [diff] [blame] | 388 | static void |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 389 | bfd_set_effective_required_min_rx (bfd_main_t * bm, |
Klement Sekera | 7388448 | 2017-02-23 09:26:30 +0100 | [diff] [blame] | 390 | bfd_session_t * bs, |
Klement Sekera | aeeac3b | 2017-02-14 07:11:52 +0100 | [diff] [blame] | 391 | u64 required_min_rx_clocks) |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 392 | { |
| 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 Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | static void |
Klement Sekera | 637b9c4 | 2016-12-08 05:19:14 +0100 | [diff] [blame] | 400 | bfd_set_remote_required_min_rx (bfd_main_t * bm, bfd_session_t * bs, |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 401 | u64 now, u32 remote_required_min_rx_usec) |
Klement Sekera | 637b9c4 | 2016-12-08 05:19:14 +0100 | [diff] [blame] | 402 | { |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 403 | 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 | |
| 415 | static void |
| 416 | bfd_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 Sekera | 637b9c4 | 2016-12-08 05:19:14 +0100 | [diff] [blame] | 429 | } |
| 430 | |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 431 | static void |
| 432 | bfd_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 Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 442 | void |
| 443 | bfd_session_start (bfd_main_t * bm, bfd_session_t * bs) |
| 444 | { |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 445 | BFD_DBG ("\nStarting session: %U", format_bfd_session, bs); |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 446 | vlib_log_info (bm->log_class, "start BFD session: %U", |
| 447 | format_bfd_session_brief, bs); |
Klement Sekera | 7388448 | 2017-02-23 09:26:30 +0100 | [diff] [blame] | 448 | bfd_set_effective_required_min_rx (bm, bs, |
| 449 | bs->config_required_min_rx_clocks); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 450 | 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 Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 453 | bfd_notify_listeners (bm, BFD_LISTEN_EVENT_CREATE, bs); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 454 | } |
| 455 | |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 456 | void |
| 457 | bfd_session_set_flags (bfd_session_t * bs, u8 admin_up_down) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 458 | { |
| 459 | bfd_main_t *bm = &bfd_main; |
Klement Sekera | 7388448 | 2017-02-23 09:26:30 +0100 | [diff] [blame] | 460 | u64 now = clib_cpu_time_now (); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 461 | if (admin_up_down) |
| 462 | { |
Klement Sekera | c48829b | 2017-02-14 07:55:57 +0100 | [diff] [blame] | 463 | BFD_DBG ("Session set admin-up, bs-idx=%u", bs->bs_idx); |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 464 | vlib_log_info (bm->log_class, "set session admin-up: %U", |
| 465 | format_bfd_session_brief, bs); |
Klement Sekera | e4504c6 | 2016-12-08 10:16:41 +0100 | [diff] [blame] | 466 | bfd_set_state (bm, bs, BFD_STATE_down, 0); |
Klement Sekera | c48829b | 2017-02-14 07:55:57 +0100 | [diff] [blame] | 467 | bfd_set_diag (bs, BFD_DIAG_CODE_no_diag); |
Klement Sekera | 7388448 | 2017-02-23 09:26:30 +0100 | [diff] [blame] | 468 | bfd_calc_next_tx (bm, bs, now); |
| 469 | bfd_set_timer (bm, bs, now, 0); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 470 | } |
| 471 | else |
| 472 | { |
Klement Sekera | c48829b | 2017-02-14 07:55:57 +0100 | [diff] [blame] | 473 | BFD_DBG ("Session set admin-down, bs-idx=%u", bs->bs_idx); |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 474 | vlib_log_info (bm->log_class, "set session admin-down: %U", |
| 475 | format_bfd_session_brief, bs); |
Klement Sekera | c48829b | 2017-02-14 07:55:57 +0100 | [diff] [blame] | 476 | bfd_set_diag (bs, BFD_DIAG_CODE_admin_down); |
Klement Sekera | e4504c6 | 2016-12-08 10:16:41 +0100 | [diff] [blame] | 477 | bfd_set_state (bm, bs, BFD_STATE_admin_down, 0); |
Klement Sekera | 7388448 | 2017-02-23 09:26:30 +0100 | [diff] [blame] | 478 | bfd_calc_next_tx (bm, bs, now); |
| 479 | bfd_set_timer (bm, bs, now, 0); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 480 | } |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | u8 * |
| 484 | bfd_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 Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 493 | " flags=(P:%u, F:%u, C:%u, A:%u, D:%u, M:%u), " |
| 494 | "detect_mult=%u, length=%u\n", |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 495 | 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 Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 504 | if (t->len >= sizeof (bfd_pkt_t) && |
| 505 | pkt->head.length >= sizeof (bfd_pkt_t)) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 506 | { |
Klement Sekera | d3ba515 | 2017-02-14 03:09:17 +0100 | [diff] [blame] | 507 | 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 Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 511 | 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 Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame] | 515 | s = format (s, " required min echo rx interval: %u", |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 516 | clib_net_to_host_u32 (pkt->req_min_echo_rx)); |
| 517 | } |
Klement Sekera | d3ba515 | 2017-02-14 03:09:17 +0100 | [diff] [blame] | 518 | 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 Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | return s; |
| 548 | } |
| 549 | |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame^] | 550 | typedef struct |
| 551 | { |
| 552 | u32 bs_idx; |
| 553 | } bfd_rpc_event_t; |
| 554 | |
| 555 | static void |
| 556 | bfd_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 | |
| 581 | static void |
| 582 | bfd_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 | |
| 592 | typedef struct |
| 593 | { |
| 594 | u32 bs_idx; |
| 595 | } bfd_rpc_notify_listeners_t; |
| 596 | |
| 597 | static void |
| 598 | bfd_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 | |
| 616 | static void |
| 617 | bfd_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 Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 626 | static void |
Klement Sekera | e4504c6 | 2016-12-08 10:16:41 +0100 | [diff] [blame] | 627 | bfd_on_state_change (bfd_main_t * bm, bfd_session_t * bs, u64 now, |
| 628 | int handling_wakeup) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 629 | { |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 630 | BFD_DBG ("\nState changed: %U", format_bfd_session, bs); |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame^] | 631 | |
| 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 Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 642 | switch (bs->local_state) |
| 643 | { |
| 644 | case BFD_STATE_admin_down: |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 645 | bs->echo = 0; |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 646 | bfd_set_effective_desired_min_tx (bm, bs, now, |
| 647 | clib_max |
| 648 | (bs->config_desired_min_tx_clocks, |
Klement Sekera | aeeac3b | 2017-02-14 07:11:52 +0100 | [diff] [blame] | 649 | bm->default_desired_min_tx_clocks)); |
Klement Sekera | 7388448 | 2017-02-23 09:26:30 +0100 | [diff] [blame] | 650 | bfd_set_effective_required_min_rx (bm, bs, |
Klement Sekera | aeeac3b | 2017-02-14 07:11:52 +0100 | [diff] [blame] | 651 | bs->config_required_min_rx_clocks); |
| 652 | bfd_set_timer (bm, bs, now, handling_wakeup); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 653 | break; |
| 654 | case BFD_STATE_down: |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 655 | bs->echo = 0; |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 656 | bfd_set_effective_desired_min_tx (bm, bs, now, |
| 657 | clib_max |
| 658 | (bs->config_desired_min_tx_clocks, |
Klement Sekera | aeeac3b | 2017-02-14 07:11:52 +0100 | [diff] [blame] | 659 | bm->default_desired_min_tx_clocks)); |
Klement Sekera | 7388448 | 2017-02-23 09:26:30 +0100 | [diff] [blame] | 660 | bfd_set_effective_required_min_rx (bm, bs, |
Klement Sekera | aeeac3b | 2017-02-14 07:11:52 +0100 | [diff] [blame] | 661 | bs->config_required_min_rx_clocks); |
| 662 | bfd_set_timer (bm, bs, now, handling_wakeup); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 663 | break; |
| 664 | case BFD_STATE_init: |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 665 | bs->echo = 0; |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 666 | bfd_set_effective_desired_min_tx (bm, bs, now, |
Klement Sekera | c48829b | 2017-02-14 07:55:57 +0100 | [diff] [blame] | 667 | bs->config_desired_min_tx_clocks); |
Klement Sekera | aeeac3b | 2017-02-14 07:11:52 +0100 | [diff] [blame] | 668 | bfd_set_timer (bm, bs, now, handling_wakeup); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 669 | break; |
| 670 | case BFD_STATE_up: |
Klement Sekera | aeeac3b | 2017-02-14 07:11:52 +0100 | [diff] [blame] | 671 | bfd_set_effective_desired_min_tx (bm, bs, now, |
| 672 | bs->config_desired_min_tx_clocks); |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 673 | if (BFD_POLL_NOT_NEEDED == bs->poll_state) |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 674 | { |
Klement Sekera | 7388448 | 2017-02-23 09:26:30 +0100 | [diff] [blame] | 675 | bfd_set_effective_required_min_rx (bm, bs, |
Klement Sekera | aeeac3b | 2017-02-14 07:11:52 +0100 | [diff] [blame] | 676 | bs->config_required_min_rx_clocks); |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 677 | } |
Klement Sekera | aeeac3b | 2017-02-14 07:11:52 +0100 | [diff] [blame] | 678 | bfd_set_timer (bm, bs, now, handling_wakeup); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 679 | break; |
| 680 | } |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame^] | 681 | 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 Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | static void |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 693 | bfd_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 Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 696 | /* |
| 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 Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 702 | { |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 703 | bs->tx_timeout_clocks = now; |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 704 | } |
| 705 | bfd_recalc_detection_time (bm, bs); |
| 706 | bfd_set_timer (bm, bs, now, 0); |
| 707 | } |
| 708 | |
| 709 | static void |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 710 | bfd_add_transport_layer (vlib_main_t * vm, u32 bi, bfd_session_t * bs) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 711 | { |
| 712 | switch (bs->transport) |
| 713 | { |
| 714 | case BFD_TRANSPORT_UDP4: |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame] | 715 | BFD_DBG ("Transport bfd via udp4, bs_idx=%u", bs->bs_idx); |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 716 | bfd_add_udp4_transport (vm, bi, bs, 0 /* is_echo */ ); |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame] | 717 | break; |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 718 | case BFD_TRANSPORT_UDP6: |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame] | 719 | BFD_DBG ("Transport bfd via udp6, bs_idx=%u", bs->bs_idx); |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 720 | bfd_add_udp6_transport (vm, bi, bs, 0 /* is_echo */ ); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 721 | break; |
| 722 | } |
| 723 | } |
| 724 | |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 725 | static int |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 726 | bfd_transport_control_frame (vlib_main_t * vm, u32 bi, bfd_session_t * bs) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 727 | { |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 728 | switch (bs->transport) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 729 | { |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 730 | case BFD_TRANSPORT_UDP4: |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 731 | BFD_DBG ("Transport bfd via udp4, bs_idx=%u", bs->bs_idx); |
| 732 | return bfd_transport_udp4 (vm, bi, bs); |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 733 | break; |
| 734 | case BFD_TRANSPORT_UDP6: |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 735 | BFD_DBG ("Transport bfd via udp6, bs_idx=%u", bs->bs_idx); |
| 736 | return bfd_transport_udp6 (vm, bi, bs); |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 737 | break; |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 738 | } |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 739 | return 0; |
| 740 | } |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 741 | |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 742 | static int |
| 743 | bfd_echo_add_transport_layer (vlib_main_t * vm, u32 bi, bfd_session_t * bs) |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 744 | { |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 745 | 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 Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 758 | |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 759 | static int |
| 760 | bfd_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 Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 774 | } |
| 775 | |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 776 | #if WITH_LIBSSL > 0 |
| 777 | static void |
| 778 | bfd_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 Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 805 | } |
Klement Sekera | 6f96649 | 2017-02-08 07:42:08 +0100 | [diff] [blame] | 806 | #endif |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 807 | |
| 808 | static void |
| 809 | bfd_add_auth_section (vlib_buffer_t * b, bfd_session_t * bs) |
| 810 | { |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 811 | bfd_main_t *bm = &bfd_main; |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 812 | 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 Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 824 | vlib_log_crit (bm->log_class, |
| 825 | "internal error, unexpected BFD auth type '%d'", |
| 826 | auth_type); |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 827 | 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 Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 838 | vlib_log_crit (bm->log_class, |
| 839 | "internal error, unexpected BFD auth type '%d'", |
| 840 | auth_type); |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 841 | break; |
| 842 | #endif |
| 843 | } |
| 844 | } |
| 845 | } |
| 846 | |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 847 | static int |
| 848 | bfd_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 Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 864 | static void |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 865 | bfd_init_control_frame (bfd_main_t * bm, bfd_session_t * bs, |
| 866 | vlib_buffer_t * b) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 867 | { |
| 868 | bfd_pkt_t *pkt = vlib_buffer_get_current (b); |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 869 | u32 bfd_length = 0; |
| 870 | bfd_length = sizeof (bfd_pkt_t); |
| 871 | memset (pkt, 0, sizeof (*pkt)); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 872 | 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 Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 875 | pkt->head.detect_mult = bs->local_detect_mult; |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 876 | pkt->head.length = bfd_length; |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 877 | pkt->my_disc = bs->local_discr; |
| 878 | pkt->your_disc = bs->remote_discr; |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 879 | pkt->des_min_tx = clib_host_to_net_u32 (bs->config_desired_min_tx_usec); |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 880 | 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 Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 891 | pkt->req_min_echo_rx = clib_host_to_net_u32 (1); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 892 | b->current_length = bfd_length; |
| 893 | } |
| 894 | |
| 895 | static void |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 896 | bfd_send_echo (vlib_main_t * vm, vlib_node_runtime_t * rt, |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 897 | bfd_main_t * bm, bfd_session_t * bs, u64 now) |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 898 | { |
| 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 Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 905 | /* sometimes the wheel expires an event a bit sooner than requested, |
| 906 | account |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 907 | 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 Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 914 | vlib_log_crit (bm->log_class, "buffer allocation failure"); |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 915 | return; |
| 916 | } |
| 917 | vlib_buffer_t *b = vlib_get_buffer (vm, bi); |
| 918 | ASSERT (b->current_data == 0); |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 919 | memset (vnet_buffer (b), 0, sizeof (*vnet_buffer (b))); |
| 920 | VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b); |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 921 | 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 Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 930 | 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 Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 938 | { |
| 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 Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 946 | } |
| 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 | |
| 955 | static void |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 956 | bfd_send_periodic (vlib_main_t * vm, vlib_node_runtime_t * rt, |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 957 | bfd_main_t * bm, bfd_session_t * bs, u64 now) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 958 | { |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 959 | if (!bs->remote_min_rx_usec && BFD_POLL_NOT_NEEDED == bs->poll_state) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 960 | { |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 961 | BFD_DBG ("Remote min rx interval is zero, not sending periodic control " |
| 962 | "frame"); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 963 | return; |
| 964 | } |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 965 | if (BFD_POLL_NOT_NEEDED == bs->poll_state && bs->remote_demand && |
Klement Sekera | d3ba515 | 2017-02-14 03:09:17 +0100 | [diff] [blame] | 966 | 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 Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 974 | BFD_DBG ("Remote demand is set, not sending periodic control frame"); |
Klement Sekera | d3ba515 | 2017-02-14 03:09:17 +0100 | [diff] [blame] | 975 | return; |
| 976 | } |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 977 | /* |
| 978 | * sometimes the wheel expires an event a bit sooner than requested, account |
| 979 | * for that here |
| 980 | */ |
Klement Sekera | 637b9c4 | 2016-12-08 05:19:14 +0100 | [diff] [blame] | 981 | if (now + bm->wheel_inaccuracy >= bs->tx_timeout_clocks) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 982 | { |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 983 | 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 Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 987 | { |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 988 | vlib_log_crit (bm->log_class, "buffer allocation failure"); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 989 | return; |
| 990 | } |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 991 | vlib_buffer_t *b = vlib_get_buffer (vm, bi); |
| 992 | ASSERT (b->current_data == 0); |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 993 | memset (vnet_buffer (b), 0, sizeof (*vnet_buffer (b))); |
| 994 | VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b); |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 995 | bfd_init_control_frame (bm, bs, b); |
| 996 | switch (bs->poll_state) |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 997 | { |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 998 | 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 Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 1012 | bfd_pkt_set_poll (vlib_buffer_get_current (b)); |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 1013 | BFD_DBG ("Setting poll bit in packet, bs_idx=%u", bs->bs_idx); |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 1014 | break; |
| 1015 | case BFD_POLL_NOT_NEEDED: |
| 1016 | /* fallthrough */ |
| 1017 | break; |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 1018 | } |
| 1019 | bfd_add_auth_section (b, bs); |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 1020 | 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 Sekera | 3e0a356 | 2016-12-19 09:05:21 +0100 | [diff] [blame] | 1025 | bs->last_tx_clocks = now; |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1026 | bfd_calc_next_tx (bm, bs, now); |
| 1027 | } |
| 1028 | else |
| 1029 | { |
Klement Sekera | 637b9c4 | 2016-12-08 05:19:14 +0100 | [diff] [blame] | 1030 | BFD_DBG |
| 1031 | ("No need to send control frame now, now is %lu, tx_timeout is %lu", |
| 1032 | now, bs->tx_timeout_clocks); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1033 | } |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1034 | } |
| 1035 | |
| 1036 | void |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 1037 | bfd_init_final_control_frame (vlib_main_t * vm, vlib_buffer_t * b, |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 1038 | bfd_main_t * bm, bfd_session_t * bs, |
| 1039 | int is_local) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1040 | { |
| 1041 | BFD_DBG ("Send final control frame for bs_idx=%lu", bs->bs_idx); |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 1042 | bfd_init_control_frame (bm, bs, b); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1043 | bfd_pkt_set_final (vlib_buffer_get_current (b)); |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 1044 | bfd_add_auth_section (b, bs); |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 1045 | u32 bi = vlib_get_buffer_index (vm, b); |
| 1046 | bfd_add_transport_layer (vm, bi, bs); |
Klement Sekera | 3e0a356 | 2016-12-19 09:05:21 +0100 | [diff] [blame] | 1047 | bs->last_tx_clocks = clib_cpu_time_now (); |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 1048 | /* |
| 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 Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 1052 | bfd_set_poll_state (bs, BFD_POLL_NOT_NEEDED); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | static void |
Klement Sekera | e4504c6 | 2016-12-08 10:16:41 +0100 | [diff] [blame] | 1056 | bfd_check_rx_timeout (bfd_main_t * bm, bfd_session_t * bs, u64 now, |
| 1057 | int handling_wakeup) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1058 | { |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 1059 | /* |
| 1060 | * sometimes the wheel expires an event a bit sooner than requested, account |
| 1061 | * for that here |
| 1062 | */ |
Klement Sekera | 637b9c4 | 2016-12-08 05:19:14 +0100 | [diff] [blame] | 1063 | if (bs->last_rx_clocks + bs->detection_time_clocks <= |
| 1064 | now + bm->wheel_inaccuracy) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1065 | { |
| 1066 | BFD_DBG ("Rx timeout, session goes down"); |
| 1067 | bfd_set_diag (bs, BFD_DIAG_CODE_det_time_exp); |
Klement Sekera | e4504c6 | 2016-12-08 10:16:41 +0100 | [diff] [blame] | 1068 | bfd_set_state (bm, bs, BFD_STATE_down, handling_wakeup); |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 1069 | /* |
| 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 Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 1076 | 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 Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1086 | } |
| 1087 | } |
| 1088 | |
| 1089 | void |
| 1090 | bfd_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 Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 1097 | bfd_send_periodic (vm, rt, bm, bs, now); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1098 | break; |
| 1099 | case BFD_STATE_down: |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 1100 | bfd_send_periodic (vm, rt, bm, bs, now); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1101 | break; |
| 1102 | case BFD_STATE_init: |
Klement Sekera | e4504c6 | 2016-12-08 10:16:41 +0100 | [diff] [blame] | 1103 | bfd_check_rx_timeout (bm, bs, now, 1); |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 1104 | bfd_send_periodic (vm, rt, bm, bs, now); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1105 | break; |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 1106 | 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 Sekera | 7388448 | 2017-02-23 09:26:30 +0100 | [diff] [blame] | 1116 | bfd_set_effective_required_min_rx (bm, bs, |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 1117 | 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 Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 1122 | bfd_send_periodic (vm, rt, bm, bs, now); |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 1123 | if (bs->echo) |
| 1124 | { |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 1125 | bfd_send_echo (vm, rt, bm, bs, now); |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 1126 | } |
| 1127 | break; |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1128 | } |
| 1129 | } |
| 1130 | |
| 1131 | /* |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1132 | * bfd process node function |
| 1133 | */ |
| 1134 | static uword |
| 1135 | bfd_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 Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame^] | 1147 | bfd_lock (bm); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1148 | 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 Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame^] | 1151 | bm->bfd_process_next_wakeup_clocks = |
| 1152 | (i64) next_expire >= 0 ? next_expire : ~0; |
| 1153 | bfd_unlock (bm); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1154 | if ((i64) next_expire < 0) |
| 1155 | { |
| 1156 | BFD_DBG ("wait for event without timeout"); |
| 1157 | (void) vlib_process_wait_for_event (vm); |
Klement Sekera | 0c1519b | 2016-12-08 05:03:32 +0100 | [diff] [blame] | 1158 | event_type = vlib_process_get_events (vm, &event_data); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1159 | } |
| 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 Sekera | 0c1519b | 2016-12-08 05:03:32 +0100 | [diff] [blame] | 1164 | 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 Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1174 | } |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1175 | 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 Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame^] | 1182 | 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 Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1187 | /* nothing to do here - reschedule is done automatically after |
| 1188 | * each event or timeout */ |
| 1189 | break; |
| 1190 | case BFD_EVENT_NEW_SESSION: |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame^] | 1191 | bfd_lock (bm); |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 1192 | if (!pool_is_free_index (bm->sessions, *event_data)) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1193 | { |
| 1194 | bfd_session_t *bs = |
| 1195 | pool_elt_at_index (bm->sessions, *event_data); |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 1196 | bfd_send_periodic (vm, rt, bm, bs, now); |
| 1197 | bfd_set_timer (bm, bs, now, 1); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1198 | } |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 1199 | else |
| 1200 | { |
| 1201 | BFD_DBG ("Ignoring event for non-existent session index %u", |
| 1202 | (u32) * event_data); |
| 1203 | } |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame^] | 1204 | bfd_unlock (bm); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1205 | break; |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 1206 | case BFD_EVENT_CONFIG_CHANGED: |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame^] | 1207 | bfd_lock (bm); |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 1208 | 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 Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame^] | 1219 | bfd_unlock (bm); |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 1220 | break; |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1221 | default: |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 1222 | vlib_log_err (bm->log_class, "BUG: event type 0x%wx", event_type); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1223 | 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 Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame^] | 1228 | bfd_lock (bm); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1229 | 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 Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1238 | bfd_on_timeout (vm, rt, bm, bs, now); |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 1239 | bfd_set_timer (bm, bs, now, 1); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1240 | } |
| 1241 | } |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame^] | 1242 | bfd_unlock (bm); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1243 | 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* */ |
| 1260 | VLIB_REGISTER_NODE (bfd_process_node, static) = { |
| 1261 | .function = bfd_process, |
| 1262 | .type = VLIB_NODE_TYPE_PROCESS, |
| 1263 | .name = "bfd-process", |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame] | 1264 | .n_next_nodes = 0, |
| 1265 | .next_nodes = {}, |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1266 | }; |
| 1267 | /* *INDENT-ON* */ |
| 1268 | |
| 1269 | static clib_error_t * |
| 1270 | bfd_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 | |
| 1281 | VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (bfd_sw_interface_up_down); |
| 1282 | |
| 1283 | static clib_error_t * |
| 1284 | bfd_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 | |
| 1294 | VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION (bfd_hw_interface_up_down); |
| 1295 | |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 1296 | void |
| 1297 | bfd_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 Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1304 | /* |
| 1305 | * setup function |
| 1306 | */ |
| 1307 | static clib_error_t * |
| 1308 | bfd_main_init (vlib_main_t * vm) |
| 1309 | { |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame^] | 1310 | vlib_thread_main_t *tm = &vlib_thread_main; |
| 1311 | u32 n_vlib_mains = tm->n_vlib_mains; |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 1312 | #if BFD_DEBUG |
| 1313 | setbuf (stdout, NULL); |
| 1314 | #endif |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1315 | 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 Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 1320 | bm->cpu_cps = vm->clib_time.clocks_per_second; |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1321 | BFD_DBG ("cps is %.2f", bm->cpu_cps); |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 1322 | bm->default_desired_min_tx_clocks = |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 1323 | 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 Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1326 | const u64 now = clib_cpu_time_now (); |
| 1327 | timing_wheel_init (&bm->wheel, now, bm->cpu_cps); |
Klement Sekera | 637b9c4 | 2016-12-08 05:19:14 +0100 | [diff] [blame] | 1328 | bm->wheel_inaccuracy = 2 << bm->wheel.log2_clocks_per_bin; |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 1329 | bm->log_class = vlib_log_register_class ("bfd", 0); |
| 1330 | vlib_log_debug (bm->log_class, "initialized"); |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame^] | 1331 | bm->owner_thread_index = ~0; |
| 1332 | if (n_vlib_mains > 1) |
| 1333 | clib_spinlock_init (&bm->lock); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1334 | return 0; |
| 1335 | } |
| 1336 | |
| 1337 | VLIB_INIT_FUNCTION (bfd_main_init); |
| 1338 | |
| 1339 | bfd_session_t * |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 1340 | bfd_get_session (bfd_main_t * bm, bfd_transport_e t) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1341 | { |
| 1342 | bfd_session_t *result; |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame^] | 1343 | |
| 1344 | bfd_lock (bm); |
| 1345 | |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1346 | pool_get (bm->sessions, result); |
Klement Sekera | e4504c6 | 2016-12-08 10:16:41 +0100 | [diff] [blame] | 1347 | memset (result, 0, sizeof (*result)); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1348 | result->bs_idx = result - bm->sessions; |
| 1349 | result->transport = t; |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 1350 | 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 Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 1357 | vlib_log_crit (bm->log_class, |
| 1358 | "couldn't allocate unused session discriminator even " |
| 1359 | "after %u tries!", limit); |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 1360 | pool_put (bm->sessions, result); |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame^] | 1361 | bfd_unlock (bm); |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 1362 | return NULL; |
| 1363 | } |
| 1364 | ++counter; |
| 1365 | } |
| 1366 | while (hash_get (bm->session_by_disc, result->local_discr)); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1367 | bfd_set_defaults (bm, result); |
| 1368 | hash_set (bm->session_by_disc, result->local_discr, result->bs_idx); |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame^] | 1369 | bfd_unlock (bm); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1370 | return result; |
| 1371 | } |
| 1372 | |
| 1373 | void |
| 1374 | bfd_put_session (bfd_main_t * bm, bfd_session_t * bs) |
| 1375 | { |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame^] | 1376 | bfd_lock (bm); |
| 1377 | |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 1378 | vlib_log_info (bm->log_class, "delete session: %U", |
| 1379 | format_bfd_session_brief, bs); |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 1380 | bfd_notify_listeners (bm, BFD_LISTEN_EVENT_DELETE, bs); |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 1381 | 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 Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1389 | hash_unset (bm->session_by_disc, bs->local_discr); |
| 1390 | pool_put (bm->sessions, bs); |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame^] | 1391 | bfd_unlock (bm); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1392 | } |
| 1393 | |
| 1394 | bfd_session_t * |
| 1395 | bfd_find_session_by_idx (bfd_main_t * bm, uword bs_idx) |
| 1396 | { |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame^] | 1397 | bfd_lock_check (bm); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1398 | 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 | |
| 1405 | bfd_session_t * |
| 1406 | bfd_find_session_by_disc (bfd_main_t * bm, u32 disc) |
| 1407 | { |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame^] | 1408 | bfd_lock_check (bm); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1409 | 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 | */ |
| 1424 | int |
| 1425 | bfd_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 Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 1435 | pkt->head.length < sizeof (bfd_pkt_with_common_auth_t))) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1436 | { |
| 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 Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 1473 | static void |
| 1474 | bfd_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 | |
| 1489 | static int |
| 1490 | bfd_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 | |
| 1500 | static int |
| 1501 | bfd_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 | |
| 1568 | static int |
| 1569 | bfd_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 | |
| 1644 | static int |
| 1645 | bfd_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 Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 1649 | bfd_main_t *bm = &bfd_main; |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 1650 | switch (auth_key->auth_type) |
| 1651 | { |
| 1652 | case BFD_AUTH_TYPE_reserved: |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 1653 | vlib_log_err (bm->log_class, |
| 1654 | "internal error, unexpected auth_type=%d:%s", |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 1655 | auth_key->auth_type, |
| 1656 | bfd_auth_type_str (auth_key->auth_type)); |
| 1657 | return 0; |
| 1658 | case BFD_AUTH_TYPE_simple_password: |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 1659 | 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 Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 1663 | return 0; |
| 1664 | case BFD_AUTH_TYPE_keyed_md5: |
| 1665 | /* fallthrough */ |
| 1666 | case BFD_AUTH_TYPE_meticulous_keyed_md5: |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 1667 | vlib_log_err |
| 1668 | (bm->log_class, |
| 1669 | "internal error, not implemented, unexpected auth_type=%d:%s", |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 1670 | 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 Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 1689 | vlib_log_err |
| 1690 | (bm->log_class, |
| 1691 | "internal error, attempt to use SHA1 without SSL support"); |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 1692 | return 0; |
| 1693 | #endif |
| 1694 | } |
| 1695 | return 0; |
| 1696 | } |
| 1697 | |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1698 | /** |
| 1699 | * @brief verify bfd packet - authentication |
| 1700 | * |
| 1701 | * @param pkt |
| 1702 | * |
| 1703 | * @return 1 if bfd packet is valid |
| 1704 | */ |
| 1705 | int |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 1706 | bfd_verify_pkt_auth (const bfd_pkt_t * pkt, u16 pkt_size, bfd_session_t * bs) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1707 | { |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 1708 | if (bfd_pkt_get_auth_present (pkt)) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1709 | { |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 1710 | /* 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 Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1758 | 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 Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 1764 | if (bs->auth.curr_key) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1765 | { |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 1766 | /* 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 Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1773 | } |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 1774 | else |
| 1775 | { |
| 1776 | /* no auth in packet, no auth in use - packet is valid */ |
| 1777 | return 1; |
| 1778 | } |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1779 | } |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 1780 | return 0; |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1781 | } |
| 1782 | |
| 1783 | void |
| 1784 | bfd_consume_pkt (bfd_main_t * bm, const bfd_pkt_t * pkt, u32 bs_idx) |
| 1785 | { |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame^] | 1786 | bfd_lock_check (bm); |
| 1787 | |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1788 | bfd_session_t *bs = bfd_find_session_by_idx (bm, bs_idx); |
Klement Sekera | 0e2e0df | 2017-03-03 08:51:08 +0100 | [diff] [blame] | 1789 | if (!bs || (pkt->your_disc && pkt->your_disc != bs->local_discr)) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1790 | { |
| 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 Sekera | 7388448 | 2017-02-23 09:26:30 +0100 | [diff] [blame] | 1797 | bs->remote_diag = bfd_pkt_get_diag_code (pkt); |
Klement Sekera | 637b9c4 | 2016-12-08 05:19:14 +0100 | [diff] [blame] | 1798 | u64 now = clib_cpu_time_now (); |
| 1799 | bs->last_rx_clocks = now; |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 1800 | 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 Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 1813 | vlib_log_crit (bm->log_class, |
| 1814 | "internal error, unexpected auth_type=%d:%s", |
| 1815 | auth_type, bfd_auth_type_str (auth_type)); |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 1816 | 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 Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 1833 | bs->remote_desired_min_tx_clocks = |
| 1834 | bfd_usec_to_clocks (bm, clib_net_to_host_u32 (pkt->des_min_tx)); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1835 | bs->remote_detect_mult = pkt->head.detect_mult; |
Klement Sekera | 637b9c4 | 2016-12-08 05:19:14 +0100 | [diff] [blame] | 1836 | bfd_set_remote_required_min_rx (bm, bs, now, |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 1837 | 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 Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 1841 | if (bfd_pkt_get_final (pkt)) |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 1842 | { |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 1843 | if (BFD_POLL_IN_PROGRESS == bs->poll_state) |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 1844 | { |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 1845 | 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 Sekera | 7388448 | 2017-02-23 09:26:30 +0100 | [diff] [blame] | 1849 | bfd_set_effective_required_min_rx (bm, bs, |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 1850 | 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 Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 1870 | } |
| 1871 | } |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 1872 | bfd_calc_next_tx (bm, bs, now); |
| 1873 | bfd_set_timer (bm, bs, now, 0); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1874 | if (BFD_STATE_admin_down == bs->local_state) |
Klement Sekera | c48829b | 2017-02-14 07:55:57 +0100 | [diff] [blame] | 1875 | { |
| 1876 | BFD_DBG ("Session is admin-down, ignoring packet, bs_idx=%u", |
| 1877 | bs->bs_idx); |
| 1878 | return; |
| 1879 | } |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1880 | if (BFD_STATE_admin_down == bs->remote_state) |
| 1881 | { |
| 1882 | bfd_set_diag (bs, BFD_DIAG_CODE_neighbor_sig_down); |
Klement Sekera | e4504c6 | 2016-12-08 10:16:41 +0100 | [diff] [blame] | 1883 | bfd_set_state (bm, bs, BFD_STATE_down, 0); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1884 | } |
| 1885 | else if (BFD_STATE_down == bs->local_state) |
| 1886 | { |
| 1887 | if (BFD_STATE_down == bs->remote_state) |
| 1888 | { |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 1889 | bfd_set_diag (bs, BFD_DIAG_CODE_no_diag); |
Klement Sekera | e4504c6 | 2016-12-08 10:16:41 +0100 | [diff] [blame] | 1890 | bfd_set_state (bm, bs, BFD_STATE_init, 0); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1891 | } |
| 1892 | else if (BFD_STATE_init == bs->remote_state) |
| 1893 | { |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 1894 | bfd_set_diag (bs, BFD_DIAG_CODE_no_diag); |
Klement Sekera | e4504c6 | 2016-12-08 10:16:41 +0100 | [diff] [blame] | 1895 | bfd_set_state (bm, bs, BFD_STATE_up, 0); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1896 | } |
| 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 Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 1903 | bfd_set_diag (bs, BFD_DIAG_CODE_no_diag); |
Klement Sekera | e4504c6 | 2016-12-08 10:16:41 +0100 | [diff] [blame] | 1904 | bfd_set_state (bm, bs, BFD_STATE_up, 0); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1905 | } |
| 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 Sekera | e4504c6 | 2016-12-08 10:16:41 +0100 | [diff] [blame] | 1912 | bfd_set_state (bm, bs, BFD_STATE_down, 0); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1913 | } |
| 1914 | } |
| 1915 | } |
| 1916 | |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 1917 | int |
| 1918 | bfd_consume_echo_pkt (bfd_main_t * bm, vlib_buffer_t * b) |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 1919 | { |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 1920 | bfd_echo_pkt_t *pkt = NULL; |
| 1921 | if (b->current_length != sizeof (*pkt)) |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 1922 | { |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 1923 | return 0; |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 1924 | } |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 1925 | 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 Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 1951 | } |
| 1952 | |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1953 | u8 * |
| 1954 | format_bfd_session (u8 * s, va_list * args) |
| 1955 | { |
| 1956 | const bfd_session_t *bs = va_arg (*args, bfd_session_t *); |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 1957 | u32 indent = format_get_indent (s) + vlib_log_get_indent (); |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 1958 | 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 Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 1969 | bs->bs_idx, bfd_state_string (bs->local_state), |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 1970 | 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 Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 1974 | bs->config_desired_min_tx_usec, bs->config_required_min_rx_usec, |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 1975 | 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 Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 1986 | return s; |
| 1987 | } |
| 1988 | |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 1989 | u8 * |
| 1990 | format_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 Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 2000 | unsigned |
| 2001 | bfd_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 | |
| 2011 | vnet_api_error_t |
| 2012 | bfd_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 Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 2020 | vlib_log_err (bm->log_class, |
| 2021 | "authentication key with config ID %u doesn't exist)", |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 2022 | 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 Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 2054 | BFD_DBG ("\nSession auth modified: %U", format_bfd_session, bs); |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 2055 | vlib_log_info (bm->log_class, "session auth modified: %U", |
| 2056 | format_bfd_session_brief, bs); |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 2057 | return 0; |
| 2058 | } |
| 2059 | |
| 2060 | vnet_api_error_t |
| 2061 | bfd_auth_deactivate (bfd_session_t * bs, u8 is_delayed) |
| 2062 | { |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 2063 | bfd_main_t *bm = &bfd_main; |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 2064 | #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 Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 2091 | BFD_DBG ("\nSession auth modified: %U", format_bfd_session, bs); |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 2092 | vlib_log_info (bm->log_class, "session auth modified: %U", |
| 2093 | format_bfd_session_brief, bs); |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 2094 | return 0; |
| 2095 | #else |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 2096 | vlib_log_err (bm->log_class, |
| 2097 | "SSL missing, cannot deactivate BFD authentication"); |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 2098 | return VNET_API_ERROR_BFD_NOTSUPP; |
| 2099 | #endif |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 2100 | } |
| 2101 | |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 2102 | vnet_api_error_t |
| 2103 | bfd_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 Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 2111 | BFD_DBG ("\nChanging session params: %U", format_bfd_session, bs); |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 2112 | switch (bs->poll_state) |
| 2113 | { |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 2114 | case BFD_POLL_NOT_NEEDED: |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 2115 | 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 Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 2122 | bfd_set_poll_state (bs, BFD_POLL_NEEDED); |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 2123 | } |
| 2124 | } |
| 2125 | break; |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 2126 | 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 Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 2132 | break; |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 2133 | 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 Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 2142 | } |
| 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 Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 2151 | BFD_DBG ("\nChanged session params: %U", format_bfd_session, bs); |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 2152 | |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 2153 | vlib_log_info (bm->log_class, "changed session params: %U", |
| 2154 | format_bfd_session_brief, bs); |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 2155 | 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 Sekera | 7388448 | 2017-02-23 09:26:30 +0100 | [diff] [blame] | 2165 | vnet_api_error_t |
| 2166 | bfd_auth_set_key (u32 conf_key_id, u8 auth_type, u8 key_len, |
| 2167 | const u8 * key_data) |
| 2168 | { |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 2169 | bfd_main_t *bm = &bfd_main; |
Klement Sekera | 7388448 | 2017-02-23 09:26:30 +0100 | [diff] [blame] | 2170 | #if WITH_LIBSSL > 0 |
| 2171 | bfd_auth_key_t *auth_key = NULL; |
Klement Sekera | b16bfe3 | 2017-02-28 11:56:48 +0100 | [diff] [blame] | 2172 | if (!key_len || key_len > bfd_max_key_len_for_auth_type (auth_type)) |
Klement Sekera | 7388448 | 2017-02-23 09:26:30 +0100 | [diff] [blame] | 2173 | { |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 2174 | 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 Sekera | 7388448 | 2017-02-23 09:26:30 +0100 | [diff] [blame] | 2177 | auth_type, bfd_auth_type_str (auth_type), key_len, |
Klement Sekera | b16bfe3 | 2017-02-28 11:56:48 +0100 | [diff] [blame] | 2178 | (u32) bfd_max_key_len_for_auth_type (auth_type)); |
Klement Sekera | 7388448 | 2017-02-23 09:26:30 +0100 | [diff] [blame] | 2179 | return VNET_API_ERROR_INVALID_VALUE; |
| 2180 | } |
| 2181 | if (!bfd_auth_type_supported (auth_type)) |
| 2182 | { |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 2183 | vlib_log_err (bm->log_class, "unsupported auth type=%d:%s", auth_type, |
Klement Sekera | 7388448 | 2017-02-23 09:26:30 +0100 | [diff] [blame] | 2184 | bfd_auth_type_str (auth_type)); |
| 2185 | return VNET_API_ERROR_BFD_NOTSUPP; |
| 2186 | } |
Klement Sekera | 7388448 | 2017-02-23 09:26:30 +0100 | [diff] [blame] | 2187 | 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 Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 2195 | 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 Sekera | 7388448 | 2017-02-23 09:26:30 +0100 | [diff] [blame] | 2199 | 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 Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 2215 | vlib_log_err (bm->log_class, |
| 2216 | "SSL missing, cannot manipulate authentication keys"); |
Klement Sekera | 7388448 | 2017-02-23 09:26:30 +0100 | [diff] [blame] | 2217 | return VNET_API_ERROR_BFD_NOTSUPP; |
| 2218 | #endif |
| 2219 | } |
| 2220 | |
| 2221 | vnet_api_error_t |
| 2222 | bfd_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 Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 2235 | 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 Sekera | 7388448 | 2017-02-23 09:26:30 +0100 | [diff] [blame] | 2239 | 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 Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 2248 | vlib_log_err (bm->log_class, |
| 2249 | "authentication key with conf ID %u does not exist", |
Klement Sekera | 7388448 | 2017-02-23 09:26:30 +0100 | [diff] [blame] | 2250 | conf_key_id); |
| 2251 | return VNET_API_ERROR_BFD_ENOENT; |
| 2252 | } |
| 2253 | return 0; |
| 2254 | #else |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 2255 | vlib_log_err (bm->log_class, |
| 2256 | "SSL missing, cannot manipulate authentication keys"); |
Klement Sekera | 7388448 | 2017-02-23 09:26:30 +0100 | [diff] [blame] | 2257 | return VNET_API_ERROR_BFD_NOTSUPP; |
| 2258 | #endif |
| 2259 | } |
| 2260 | |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 2261 | bfd_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 | */ |