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 | |
| 40 | static u64 |
| 41 | bfd_calc_echo_checksum (u32 discriminator, u64 expire_time, u32 secret) |
| 42 | { |
| 43 | u64 checksum = 0; |
Gabriel Ganne | 8e66b9b | 2017-12-14 16:20:37 +0100 | [diff] [blame] | 44 | #if defined(clib_crc32c_uses_intrinsics) && !defined (__i386__) |
| 45 | checksum = crc32_u64 (0, discriminator); |
| 46 | checksum = crc32_u64 (checksum, expire_time); |
| 47 | checksum = crc32_u64 (checksum, secret); |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 48 | #else |
| 49 | checksum = clib_xxhash (discriminator ^ expire_time ^ secret); |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 50 | #endif |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 51 | return checksum; |
| 52 | } |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 53 | |
| 54 | static u64 |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 55 | bfd_usec_to_nsec (u64 us) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 56 | { |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 57 | return us * NSEC_PER_USEC; |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 58 | } |
| 59 | |
Klement Sekera | 7388448 | 2017-02-23 09:26:30 +0100 | [diff] [blame] | 60 | u32 |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 61 | bfd_nsec_to_usec (u64 nsec) |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 62 | { |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 63 | return nsec / NSEC_PER_USEC; |
| 64 | } |
| 65 | |
| 66 | always_inline u64 |
| 67 | bfd_time_now_nsec (vlib_main_t * vm, f64 * vm_time) |
| 68 | { |
| 69 | f64 _vm_time = vlib_time_now (vm); |
| 70 | if (vm_time) |
| 71 | *vm_time = _vm_time; |
| 72 | return _vm_time * NSEC_PER_SEC; |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 73 | } |
| 74 | |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 75 | static vlib_node_registration_t bfd_process_node; |
| 76 | |
Klement Sekera | 7388448 | 2017-02-23 09:26:30 +0100 | [diff] [blame] | 77 | u8 * |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 78 | format_bfd_auth_key (u8 * s, va_list * args) |
| 79 | { |
| 80 | const bfd_auth_key_t *key = va_arg (*args, bfd_auth_key_t *); |
| 81 | if (key) |
| 82 | { |
| 83 | s = format (s, "{auth-type=%u:%s, conf-key-id=%u, use-count=%u}, ", |
| 84 | key->auth_type, bfd_auth_type_str (key->auth_type), |
| 85 | key->conf_key_id, key->use_count); |
| 86 | } |
| 87 | else |
| 88 | { |
| 89 | s = format (s, "{none}"); |
| 90 | } |
| 91 | return s; |
| 92 | } |
| 93 | |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 94 | /* |
| 95 | * We actually send all bfd pkts to the "error" node after scanning |
| 96 | * them, so the graph node has only one next-index. The "error-drop" |
| 97 | * node automatically bumps our per-node packet counters for us. |
| 98 | */ |
| 99 | typedef enum |
| 100 | { |
| 101 | BFD_INPUT_NEXT_NORMAL, |
| 102 | BFD_INPUT_N_NEXT, |
| 103 | } bfd_input_next_t; |
| 104 | |
Klement Sekera | e4504c6 | 2016-12-08 10:16:41 +0100 | [diff] [blame] | 105 | static void bfd_on_state_change (bfd_main_t * bm, bfd_session_t * bs, u64 now, |
| 106 | int handling_wakeup); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 107 | |
| 108 | static void |
| 109 | bfd_set_defaults (bfd_main_t * bm, bfd_session_t * bs) |
| 110 | { |
| 111 | bs->local_state = BFD_STATE_down; |
| 112 | bs->local_diag = BFD_DIAG_CODE_no_diag; |
| 113 | bs->remote_state = BFD_STATE_down; |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 114 | bs->remote_discr = 0; |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 115 | bs->hop_type = BFD_HOP_TYPE_SINGLE; |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 116 | bs->config_desired_min_tx_usec = BFD_DEFAULT_DESIRED_MIN_TX_USEC; |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 117 | bs->config_desired_min_tx_nsec = bm->default_desired_min_tx_nsec; |
| 118 | bs->effective_desired_min_tx_nsec = bm->default_desired_min_tx_nsec; |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 119 | bs->remote_min_rx_usec = 1; |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 120 | bs->remote_min_rx_nsec = bfd_usec_to_nsec (bs->remote_min_rx_usec); |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 121 | bs->remote_min_echo_rx_usec = 0; |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 122 | bs->remote_min_echo_rx_nsec = 0; |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 123 | bs->remote_demand = 0; |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 124 | bs->auth.remote_seq_number = 0; |
| 125 | bs->auth.remote_seq_number_known = 0; |
| 126 | bs->auth.local_seq_number = random_u32 (&bm->random_seed); |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 127 | bs->echo_secret = random_u32 (&bm->random_seed); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | static void |
| 131 | bfd_set_diag (bfd_session_t * bs, bfd_diag_code_e code) |
| 132 | { |
| 133 | if (bs->local_diag != code) |
| 134 | { |
| 135 | BFD_DBG ("set local_diag, bs_idx=%d: '%d:%s'", bs->bs_idx, code, |
| 136 | bfd_diag_code_string (code)); |
| 137 | bs->local_diag = code; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | static void |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 142 | bfd_set_state (vlib_main_t * vm, bfd_main_t * bm, bfd_session_t * bs, |
Klement Sekera | e4504c6 | 2016-12-08 10:16:41 +0100 | [diff] [blame] | 143 | bfd_state_e new_state, int handling_wakeup) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 144 | { |
| 145 | if (bs->local_state != new_state) |
| 146 | { |
| 147 | BFD_DBG ("Change state, bs_idx=%d: %s->%s", bs->bs_idx, |
| 148 | bfd_state_string (bs->local_state), |
| 149 | bfd_state_string (new_state)); |
| 150 | bs->local_state = new_state; |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 151 | bfd_on_state_change (bm, bs, bfd_time_now_nsec (vm, NULL), |
| 152 | handling_wakeup); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | |
Klement Sekera | 7388448 | 2017-02-23 09:26:30 +0100 | [diff] [blame] | 156 | const char * |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 157 | bfd_poll_state_string (bfd_poll_state_e state) |
| 158 | { |
| 159 | switch (state) |
| 160 | { |
| 161 | #define F(x) \ |
| 162 | case BFD_POLL_##x: \ |
| 163 | return "BFD_POLL_" #x; |
| 164 | foreach_bfd_poll_state (F) |
| 165 | #undef F |
| 166 | } |
| 167 | return "UNKNOWN"; |
| 168 | } |
| 169 | |
| 170 | static void |
| 171 | bfd_set_poll_state (bfd_session_t * bs, bfd_poll_state_e state) |
| 172 | { |
| 173 | if (bs->poll_state != state) |
| 174 | { |
| 175 | BFD_DBG ("Setting poll state=%s, bs_idx=%u", |
| 176 | bfd_poll_state_string (state), bs->bs_idx); |
| 177 | bs->poll_state = state; |
| 178 | } |
| 179 | } |
| 180 | |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 181 | static void |
| 182 | bfd_recalc_tx_interval (bfd_main_t * bm, bfd_session_t * bs) |
| 183 | { |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 184 | bs->transmit_interval_nsec = |
| 185 | clib_max (bs->effective_desired_min_tx_nsec, bs->remote_min_rx_nsec); |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 186 | BFD_DBG ("Recalculated transmit interval " BFD_CLK_FMT, |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 187 | BFD_CLK_PRN (bs->transmit_interval_nsec)); |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | static void |
| 191 | bfd_recalc_echo_tx_interval (bfd_main_t * bm, bfd_session_t * bs) |
| 192 | { |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 193 | bs->echo_transmit_interval_nsec = |
| 194 | clib_max (bs->effective_desired_min_tx_nsec, bs->remote_min_echo_rx_nsec); |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 195 | BFD_DBG ("Recalculated echo transmit interval " BFD_CLK_FMT, |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 196 | BFD_CLK_PRN (bs->echo_transmit_interval_nsec)); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | static void |
| 200 | bfd_calc_next_tx (bfd_main_t * bm, bfd_session_t * bs, u64 now) |
| 201 | { |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 202 | if (bs->local_detect_mult > 1) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 203 | { |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 204 | /* common case - 75-100% of transmit interval */ |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 205 | bs->tx_timeout_nsec = bs->last_tx_nsec + |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 206 | (1 - .25 * (random_f64 (&bm->random_seed))) * |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 207 | bs->transmit_interval_nsec; |
| 208 | if (bs->tx_timeout_nsec < now) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 209 | { |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 210 | /* |
| 211 | * the timeout is in the past, which means that either remote |
| 212 | * demand mode was set or performance/clock issues ... |
| 213 | */ |
| 214 | BFD_DBG ("Missed %lu transmit events (now is %lu, calc " |
| 215 | "tx_timeout is %lu)", |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 216 | (now - bs->tx_timeout_nsec) / |
| 217 | bs->transmit_interval_nsec, now, bs->tx_timeout_nsec); |
| 218 | bs->tx_timeout_nsec = now; |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | else |
| 222 | { |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 223 | /* special case - 75-90% of transmit interval */ |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 224 | bs->tx_timeout_nsec = bs->last_tx_nsec + |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 225 | (.9 - .15 * (random_f64 (&bm->random_seed))) * |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 226 | bs->transmit_interval_nsec; |
| 227 | if (bs->tx_timeout_nsec < now) |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 228 | { |
| 229 | /* |
| 230 | * the timeout is in the past, which means that either remote |
| 231 | * demand mode was set or performance/clock issues ... |
| 232 | */ |
| 233 | BFD_DBG ("Missed %lu transmit events (now is %lu, calc " |
| 234 | "tx_timeout is %lu)", |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 235 | (now - bs->tx_timeout_nsec) / |
| 236 | bs->transmit_interval_nsec, now, bs->tx_timeout_nsec); |
| 237 | bs->tx_timeout_nsec = now; |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 238 | } |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 239 | } |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 240 | if (bs->tx_timeout_nsec) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 241 | { |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 242 | BFD_DBG ("Next transmit in %lu nsec/%.02fs@%lu", |
| 243 | bs->tx_timeout_nsec - now, |
| 244 | (bs->tx_timeout_nsec - now) * SEC_PER_NSEC, |
| 245 | bs->tx_timeout_nsec); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | |
| 249 | static void |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 250 | bfd_calc_next_echo_tx (bfd_main_t * bm, bfd_session_t * bs, u64 now) |
| 251 | { |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 252 | bs->echo_tx_timeout_nsec = |
| 253 | bs->echo_last_tx_nsec + bs->echo_transmit_interval_nsec; |
| 254 | if (bs->echo_tx_timeout_nsec < now) |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 255 | { |
| 256 | /* huh, we've missed it already, transmit now */ |
| 257 | BFD_DBG ("Missed %lu echo transmit events (now is %lu, calc tx_timeout " |
| 258 | "is %lu)", |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 259 | (now - bs->echo_tx_timeout_nsec) / |
| 260 | bs->echo_transmit_interval_nsec, |
| 261 | now, bs->echo_tx_timeout_nsec); |
| 262 | bs->echo_tx_timeout_nsec = now; |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 263 | } |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 264 | BFD_DBG ("Next echo transmit in %lu nsec/%.02fs@%lu", |
| 265 | bs->echo_tx_timeout_nsec - now, |
| 266 | (bs->echo_tx_timeout_nsec - now) * SEC_PER_NSEC, |
| 267 | bs->echo_tx_timeout_nsec); |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | static void |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 271 | bfd_recalc_detection_time (bfd_main_t * bm, bfd_session_t * bs) |
| 272 | { |
Klement Sekera | 7388448 | 2017-02-23 09:26:30 +0100 | [diff] [blame] | 273 | if (bs->local_state == BFD_STATE_init || bs->local_state == BFD_STATE_up) |
| 274 | { |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 275 | bs->detection_time_nsec = |
Klement Sekera | 7388448 | 2017-02-23 09:26:30 +0100 | [diff] [blame] | 276 | bs->remote_detect_mult * |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 277 | clib_max (bs->effective_required_min_rx_nsec, |
| 278 | bs->remote_desired_min_tx_nsec); |
| 279 | BFD_DBG ("Recalculated detection time %lu nsec/%.3fs", |
| 280 | bs->detection_time_nsec, |
| 281 | bs->detection_time_nsec * SEC_PER_NSEC); |
Klement Sekera | 7388448 | 2017-02-23 09:26:30 +0100 | [diff] [blame] | 282 | } |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | static void |
Klement Sekera | e4504c6 | 2016-12-08 10:16:41 +0100 | [diff] [blame] | 286 | bfd_set_timer (bfd_main_t * bm, bfd_session_t * bs, u64 now, |
| 287 | int handling_wakeup) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 288 | { |
| 289 | u64 next = 0; |
| 290 | u64 rx_timeout = 0; |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 291 | u64 tx_timeout = 0; |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 292 | if (BFD_STATE_up == bs->local_state) |
| 293 | { |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 294 | rx_timeout = bs->last_rx_nsec + bs->detection_time_nsec; |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 295 | } |
Klement Sekera | 7388448 | 2017-02-23 09:26:30 +0100 | [diff] [blame] | 296 | if (BFD_STATE_up != bs->local_state || |
| 297 | (!bs->remote_demand && bs->remote_min_rx_usec) || |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 298 | BFD_POLL_NOT_NEEDED != bs->poll_state) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 299 | { |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 300 | tx_timeout = bs->tx_timeout_nsec; |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 301 | } |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 302 | if (tx_timeout && rx_timeout) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 303 | { |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 304 | next = clib_min (tx_timeout, rx_timeout); |
| 305 | } |
| 306 | else if (tx_timeout) |
| 307 | { |
| 308 | next = tx_timeout; |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 309 | } |
| 310 | else if (rx_timeout) |
| 311 | { |
| 312 | next = rx_timeout; |
| 313 | } |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 314 | if (bs->echo && next > bs->echo_tx_timeout_nsec) |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 315 | { |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 316 | next = bs->echo_tx_timeout_nsec; |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 317 | } |
| 318 | BFD_DBG ("bs_idx=%u, tx_timeout=%lu, echo_tx_timeout=%lu, rx_timeout=%lu, " |
| 319 | "next=%s", |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 320 | bs->bs_idx, tx_timeout, bs->echo_tx_timeout_nsec, rx_timeout, |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 321 | next == tx_timeout |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 322 | ? "tx" : (next == bs->echo_tx_timeout_nsec ? "echo tx" : "rx")); |
| 323 | if (next) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 324 | { |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame] | 325 | int send_signal = 0; |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 326 | bs->event_time_nsec = next; |
| 327 | /* add extra tick if it's not even */ |
| 328 | u32 wheel_time_ticks = |
| 329 | (bs->event_time_nsec - now) / bm->nsec_per_tw_tick + |
| 330 | ((bs->event_time_nsec - now) % bm->nsec_per_tw_tick != 0); |
| 331 | BFD_DBG ("event_time_nsec %lu (%lu nsec/%.3fs in future) -> " |
| 332 | "wheel_time_ticks %u", bs->event_time_nsec, |
| 333 | bs->event_time_nsec - now, |
| 334 | (bs->event_time_nsec - now) * SEC_PER_NSEC, wheel_time_ticks); |
Dave Barach | ca57852 | 2020-04-08 08:45:27 -0400 | [diff] [blame] | 335 | wheel_time_ticks = wheel_time_ticks ? wheel_time_ticks : 1; |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame] | 336 | bfd_lock (bm); |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 337 | if (bs->tw_id) |
| 338 | { |
| 339 | TW (tw_timer_update) (&bm->wheel, bs->tw_id, wheel_time_ticks); |
| 340 | BFD_DBG ("tw_timer_update(%p, %u, %lu);", &bm->wheel, bs->tw_id, |
| 341 | wheel_time_ticks); |
| 342 | } |
| 343 | else |
| 344 | { |
| 345 | bs->tw_id = |
| 346 | TW (tw_timer_start) (&bm->wheel, bs->bs_idx, 0, wheel_time_ticks); |
| 347 | BFD_DBG ("tw_timer_start(%p, %u, 0, %lu) == %u;", &bm->wheel, |
| 348 | bs->bs_idx, wheel_time_ticks); |
| 349 | } |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame] | 350 | |
Klement Sekera | e4504c6 | 2016-12-08 10:16:41 +0100 | [diff] [blame] | 351 | if (!handling_wakeup) |
| 352 | { |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame] | 353 | |
| 354 | /* Send only if it is earlier than current awaited wakeup time */ |
| 355 | send_signal = |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 356 | (bs->event_time_nsec < bm->bfd_process_next_wakeup_nsec) && |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame] | 357 | /* |
| 358 | * If the wake-up time is within 2x the delay of the event propagation delay, |
| 359 | * avoid the expense of sending the event. The 2x multiplier is to workaround the race whereby |
| 360 | * simultaneous event + expired timer create one recurring bogus wakeup/suspend instance, |
| 361 | * due to double scheduling of the node on the pending list. |
| 362 | */ |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 363 | (bm->bfd_process_next_wakeup_nsec - bs->event_time_nsec > |
| 364 | 2 * bm->bfd_process_wakeup_event_delay_nsec) && |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame] | 365 | /* Must be no events in flight to send an event */ |
| 366 | (!bm->bfd_process_wakeup_events_in_flight); |
| 367 | |
| 368 | /* If we do send the signal, note this down along with the start timestamp */ |
| 369 | if (send_signal) |
| 370 | { |
| 371 | bm->bfd_process_wakeup_events_in_flight++; |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 372 | bm->bfd_process_wakeup_event_start_nsec = now; |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame] | 373 | } |
| 374 | } |
| 375 | bfd_unlock (bm); |
| 376 | |
| 377 | /* Use the multithreaded event sending so the workers can send events too */ |
| 378 | if (send_signal) |
| 379 | { |
| 380 | vlib_process_signal_event_mt (bm->vlib_main, |
| 381 | bm->bfd_process_node_index, |
| 382 | BFD_EVENT_RESCHEDULE, ~0); |
Klement Sekera | e4504c6 | 2016-12-08 10:16:41 +0100 | [diff] [blame] | 383 | } |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 384 | } |
| 385 | } |
| 386 | |
| 387 | static void |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 388 | bfd_set_effective_desired_min_tx (bfd_main_t * bm, |
| 389 | bfd_session_t * bs, u64 now, |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 390 | u64 desired_min_tx_nsec) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 391 | { |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 392 | bs->effective_desired_min_tx_nsec = desired_min_tx_nsec; |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 393 | BFD_DBG ("Set effective desired min tx to " BFD_CLK_FMT, |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 394 | BFD_CLK_PRN (bs->effective_desired_min_tx_nsec)); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 395 | bfd_recalc_detection_time (bm, bs); |
| 396 | bfd_recalc_tx_interval (bm, bs); |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 397 | bfd_recalc_echo_tx_interval (bm, bs); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 398 | bfd_calc_next_tx (bm, bs, now); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 399 | } |
| 400 | |
Klement Sekera | 637b9c4 | 2016-12-08 05:19:14 +0100 | [diff] [blame] | 401 | static void |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 402 | bfd_set_effective_required_min_rx (bfd_main_t * bm, |
Klement Sekera | 7388448 | 2017-02-23 09:26:30 +0100 | [diff] [blame] | 403 | bfd_session_t * bs, |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 404 | u64 required_min_rx_nsec) |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 405 | { |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 406 | bs->effective_required_min_rx_nsec = required_min_rx_nsec; |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 407 | BFD_DBG ("Set effective required min rx to " BFD_CLK_FMT, |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 408 | BFD_CLK_PRN (bs->effective_required_min_rx_nsec)); |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 409 | bfd_recalc_detection_time (bm, bs); |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | static void |
Klement Sekera | 637b9c4 | 2016-12-08 05:19:14 +0100 | [diff] [blame] | 413 | 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] | 414 | u64 now, u32 remote_required_min_rx_usec) |
Klement Sekera | 637b9c4 | 2016-12-08 05:19:14 +0100 | [diff] [blame] | 415 | { |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 416 | if (bs->remote_min_rx_usec != remote_required_min_rx_usec) |
| 417 | { |
| 418 | bs->remote_min_rx_usec = remote_required_min_rx_usec; |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 419 | bs->remote_min_rx_nsec = bfd_usec_to_nsec (remote_required_min_rx_usec); |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 420 | BFD_DBG ("Set remote min rx to " BFD_CLK_FMT, |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 421 | BFD_CLK_PRN (bs->remote_min_rx_nsec)); |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 422 | bfd_recalc_detection_time (bm, bs); |
| 423 | bfd_recalc_tx_interval (bm, bs); |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | static void |
| 428 | bfd_set_remote_required_min_echo_rx (bfd_main_t * bm, bfd_session_t * bs, |
| 429 | u64 now, |
| 430 | u32 remote_required_min_echo_rx_usec) |
| 431 | { |
| 432 | if (bs->remote_min_echo_rx_usec != remote_required_min_echo_rx_usec) |
| 433 | { |
| 434 | bs->remote_min_echo_rx_usec = remote_required_min_echo_rx_usec; |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 435 | bs->remote_min_echo_rx_nsec = |
| 436 | bfd_usec_to_nsec (bs->remote_min_echo_rx_usec); |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 437 | BFD_DBG ("Set remote min echo rx to " BFD_CLK_FMT, |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 438 | BFD_CLK_PRN (bs->remote_min_echo_rx_nsec)); |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 439 | bfd_recalc_echo_tx_interval (bm, bs); |
| 440 | } |
Klement Sekera | 637b9c4 | 2016-12-08 05:19:14 +0100 | [diff] [blame] | 441 | } |
| 442 | |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 443 | static void |
| 444 | bfd_notify_listeners (bfd_main_t * bm, |
| 445 | bfd_listen_event_e event, const bfd_session_t * bs) |
| 446 | { |
| 447 | bfd_notify_fn_t *fn; |
| 448 | vec_foreach (fn, bm->listeners) |
| 449 | { |
| 450 | (*fn) (event, bs); |
| 451 | } |
| 452 | } |
| 453 | |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 454 | void |
| 455 | bfd_session_start (bfd_main_t * bm, bfd_session_t * bs) |
| 456 | { |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 457 | BFD_DBG ("\nStarting session: %U", format_bfd_session, bs); |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 458 | vlib_log_info (bm->log_class, "start BFD session: %U", |
| 459 | format_bfd_session_brief, bs); |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 460 | bfd_set_effective_required_min_rx (bm, bs, bs->config_required_min_rx_nsec); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 461 | bfd_recalc_tx_interval (bm, bs); |
| 462 | vlib_process_signal_event (bm->vlib_main, bm->bfd_process_node_index, |
| 463 | BFD_EVENT_NEW_SESSION, bs->bs_idx); |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 464 | bfd_notify_listeners (bm, BFD_LISTEN_EVENT_CREATE, bs); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 465 | } |
| 466 | |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 467 | void |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 468 | bfd_session_set_flags (vlib_main_t * vm, bfd_session_t * bs, u8 admin_up_down) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 469 | { |
| 470 | bfd_main_t *bm = &bfd_main; |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 471 | u64 now = bfd_time_now_nsec (vm, NULL); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 472 | if (admin_up_down) |
| 473 | { |
Klement Sekera | c48829b | 2017-02-14 07:55:57 +0100 | [diff] [blame] | 474 | BFD_DBG ("Session set admin-up, bs-idx=%u", bs->bs_idx); |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 475 | vlib_log_info (bm->log_class, "set session admin-up: %U", |
| 476 | format_bfd_session_brief, bs); |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 477 | bfd_set_state (vm, bm, bs, BFD_STATE_down, 0); |
Klement Sekera | c48829b | 2017-02-14 07:55:57 +0100 | [diff] [blame] | 478 | bfd_set_diag (bs, BFD_DIAG_CODE_no_diag); |
Klement Sekera | 7388448 | 2017-02-23 09:26:30 +0100 | [diff] [blame] | 479 | bfd_calc_next_tx (bm, bs, now); |
| 480 | bfd_set_timer (bm, bs, now, 0); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 481 | } |
| 482 | else |
| 483 | { |
Klement Sekera | c48829b | 2017-02-14 07:55:57 +0100 | [diff] [blame] | 484 | BFD_DBG ("Session set admin-down, bs-idx=%u", bs->bs_idx); |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 485 | vlib_log_info (bm->log_class, "set session admin-down: %U", |
| 486 | format_bfd_session_brief, bs); |
Klement Sekera | c48829b | 2017-02-14 07:55:57 +0100 | [diff] [blame] | 487 | bfd_set_diag (bs, BFD_DIAG_CODE_admin_down); |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 488 | bfd_set_state (vm, bm, bs, BFD_STATE_admin_down, 0); |
Klement Sekera | 7388448 | 2017-02-23 09:26:30 +0100 | [diff] [blame] | 489 | bfd_calc_next_tx (bm, bs, now); |
| 490 | bfd_set_timer (bm, bs, now, 0); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 491 | } |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | u8 * |
| 495 | bfd_input_format_trace (u8 * s, va_list * args) |
| 496 | { |
| 497 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 498 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
| 499 | const bfd_input_trace_t *t = va_arg (*args, bfd_input_trace_t *); |
| 500 | const bfd_pkt_t *pkt = (bfd_pkt_t *) t->data; |
| 501 | if (t->len > STRUCT_SIZE_OF (bfd_pkt_t, head)) |
| 502 | { |
| 503 | s = format (s, "BFD v%u, diag=%u(%s), state=%u(%s),\n" |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 504 | " flags=(P:%u, F:%u, C:%u, A:%u, D:%u, M:%u), " |
| 505 | "detect_mult=%u, length=%u\n", |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 506 | bfd_pkt_get_version (pkt), bfd_pkt_get_diag_code (pkt), |
| 507 | bfd_diag_code_string (bfd_pkt_get_diag_code (pkt)), |
| 508 | bfd_pkt_get_state (pkt), |
| 509 | bfd_state_string (bfd_pkt_get_state (pkt)), |
| 510 | bfd_pkt_get_poll (pkt), bfd_pkt_get_final (pkt), |
| 511 | bfd_pkt_get_control_plane_independent (pkt), |
| 512 | bfd_pkt_get_auth_present (pkt), bfd_pkt_get_demand (pkt), |
| 513 | bfd_pkt_get_multipoint (pkt), pkt->head.detect_mult, |
| 514 | pkt->head.length); |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 515 | if (t->len >= sizeof (bfd_pkt_t) && |
| 516 | pkt->head.length >= sizeof (bfd_pkt_t)) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 517 | { |
Klement Sekera | d3ba515 | 2017-02-14 03:09:17 +0100 | [diff] [blame] | 518 | s = format (s, " my discriminator: %u\n", |
| 519 | clib_net_to_host_u32 (pkt->my_disc)); |
| 520 | s = format (s, " your discriminator: %u\n", |
| 521 | clib_net_to_host_u32 (pkt->your_disc)); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 522 | s = format (s, " desired min tx interval: %u\n", |
| 523 | clib_net_to_host_u32 (pkt->des_min_tx)); |
| 524 | s = format (s, " required min rx interval: %u\n", |
| 525 | clib_net_to_host_u32 (pkt->req_min_rx)); |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame] | 526 | s = format (s, " required min echo rx interval: %u", |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 527 | clib_net_to_host_u32 (pkt->req_min_echo_rx)); |
| 528 | } |
Klement Sekera | d3ba515 | 2017-02-14 03:09:17 +0100 | [diff] [blame] | 529 | if (t->len >= sizeof (bfd_pkt_with_common_auth_t) && |
| 530 | pkt->head.length >= sizeof (bfd_pkt_with_common_auth_t) && |
| 531 | bfd_pkt_get_auth_present (pkt)) |
| 532 | { |
| 533 | const bfd_pkt_with_common_auth_t *with_auth = (void *) pkt; |
| 534 | const bfd_auth_common_t *common = &with_auth->common_auth; |
| 535 | s = format (s, "\n auth len: %u\n", common->len); |
| 536 | s = format (s, " auth type: %u:%s\n", common->type, |
| 537 | bfd_auth_type_str (common->type)); |
| 538 | if (t->len >= sizeof (bfd_pkt_with_sha1_auth_t) && |
| 539 | pkt->head.length >= sizeof (bfd_pkt_with_sha1_auth_t) && |
| 540 | (BFD_AUTH_TYPE_keyed_sha1 == common->type || |
| 541 | BFD_AUTH_TYPE_meticulous_keyed_sha1 == common->type)) |
| 542 | { |
| 543 | const bfd_pkt_with_sha1_auth_t *with_sha1 = (void *) pkt; |
| 544 | const bfd_auth_sha1_t *sha1 = &with_sha1->sha1_auth; |
| 545 | s = format (s, " seq num: %u\n", |
| 546 | clib_net_to_host_u32 (sha1->seq_num)); |
| 547 | s = format (s, " key id: %u\n", sha1->key_id); |
| 548 | s = format (s, " hash: %U", format_hex_bytes, sha1->hash, |
| 549 | sizeof (sha1->hash)); |
| 550 | } |
| 551 | } |
| 552 | else |
| 553 | { |
| 554 | s = format (s, "\n"); |
| 555 | } |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | return s; |
| 559 | } |
| 560 | |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame] | 561 | typedef struct |
| 562 | { |
| 563 | u32 bs_idx; |
| 564 | } bfd_rpc_event_t; |
| 565 | |
| 566 | static void |
| 567 | bfd_rpc_event_cb (const bfd_rpc_event_t * a) |
| 568 | { |
| 569 | bfd_main_t *bm = &bfd_main; |
| 570 | u32 bs_idx = a->bs_idx; |
| 571 | u32 valid_bs = 0; |
| 572 | bfd_session_t session_data; |
| 573 | |
| 574 | bfd_lock (bm); |
| 575 | if (!pool_is_free_index (bm->sessions, bs_idx)) |
| 576 | { |
| 577 | bfd_session_t *bs = pool_elt_at_index (bm->sessions, bs_idx); |
| 578 | clib_memcpy (&session_data, bs, sizeof (bfd_session_t)); |
| 579 | valid_bs = 1; |
| 580 | } |
| 581 | else |
| 582 | { |
| 583 | BFD_DBG ("Ignoring event RPC for non-existent session index %u", |
| 584 | bs_idx); |
| 585 | } |
| 586 | bfd_unlock (bm); |
| 587 | |
| 588 | if (valid_bs) |
| 589 | bfd_event (bm, &session_data); |
| 590 | } |
| 591 | |
| 592 | static void |
| 593 | bfd_event_rpc (u32 bs_idx) |
| 594 | { |
| 595 | const u32 data_size = sizeof (bfd_rpc_event_t); |
| 596 | u8 data[data_size]; |
| 597 | bfd_rpc_event_t *event = (bfd_rpc_event_t *) data; |
| 598 | |
| 599 | event->bs_idx = bs_idx; |
| 600 | vl_api_rpc_call_main_thread (bfd_rpc_event_cb, data, data_size); |
| 601 | } |
| 602 | |
| 603 | typedef struct |
| 604 | { |
| 605 | u32 bs_idx; |
| 606 | } bfd_rpc_notify_listeners_t; |
| 607 | |
| 608 | static void |
| 609 | bfd_rpc_notify_listeners_cb (const bfd_rpc_notify_listeners_t * a) |
| 610 | { |
| 611 | bfd_main_t *bm = &bfd_main; |
| 612 | u32 bs_idx = a->bs_idx; |
| 613 | bfd_lock (bm); |
| 614 | if (!pool_is_free_index (bm->sessions, bs_idx)) |
| 615 | { |
| 616 | bfd_session_t *bs = pool_elt_at_index (bm->sessions, bs_idx); |
| 617 | bfd_notify_listeners (bm, BFD_LISTEN_EVENT_UPDATE, bs); |
| 618 | } |
| 619 | else |
| 620 | { |
| 621 | BFD_DBG ("Ignoring notify RPC for non-existent session index %u", |
| 622 | bs_idx); |
| 623 | } |
| 624 | bfd_unlock (bm); |
| 625 | } |
| 626 | |
| 627 | static void |
| 628 | bfd_notify_listeners_rpc (u32 bs_idx) |
| 629 | { |
| 630 | const u32 data_size = sizeof (bfd_rpc_notify_listeners_t); |
| 631 | u8 data[data_size]; |
| 632 | bfd_rpc_notify_listeners_t *notify = (bfd_rpc_notify_listeners_t *) data; |
| 633 | notify->bs_idx = bs_idx; |
| 634 | vl_api_rpc_call_main_thread (bfd_rpc_notify_listeners_cb, data, data_size); |
| 635 | } |
| 636 | |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 637 | static void |
Klement Sekera | e4504c6 | 2016-12-08 10:16:41 +0100 | [diff] [blame] | 638 | bfd_on_state_change (bfd_main_t * bm, bfd_session_t * bs, u64 now, |
| 639 | int handling_wakeup) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 640 | { |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 641 | BFD_DBG ("\nState changed: %U", format_bfd_session, bs); |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame] | 642 | |
| 643 | if (vlib_get_thread_index () == 0) |
| 644 | { |
| 645 | bfd_event (bm, bs); |
| 646 | } |
| 647 | else |
| 648 | { |
| 649 | /* without RPC - a REGRESSION: BFD event are not propagated */ |
| 650 | bfd_event_rpc (bs->bs_idx); |
| 651 | } |
| 652 | |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 653 | switch (bs->local_state) |
| 654 | { |
| 655 | case BFD_STATE_admin_down: |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 656 | bs->echo = 0; |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 657 | bfd_set_effective_desired_min_tx (bm, bs, now, |
| 658 | clib_max |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 659 | (bs->config_desired_min_tx_nsec, |
| 660 | bm->default_desired_min_tx_nsec)); |
Klement Sekera | 7388448 | 2017-02-23 09:26:30 +0100 | [diff] [blame] | 661 | bfd_set_effective_required_min_rx (bm, bs, |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 662 | bs->config_required_min_rx_nsec); |
Klement Sekera | aeeac3b | 2017-02-14 07:11:52 +0100 | [diff] [blame] | 663 | bfd_set_timer (bm, bs, now, handling_wakeup); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 664 | break; |
| 665 | case BFD_STATE_down: |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 666 | bs->echo = 0; |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 667 | bfd_set_effective_desired_min_tx (bm, bs, now, |
| 668 | clib_max |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 669 | (bs->config_desired_min_tx_nsec, |
| 670 | bm->default_desired_min_tx_nsec)); |
Klement Sekera | 7388448 | 2017-02-23 09:26:30 +0100 | [diff] [blame] | 671 | bfd_set_effective_required_min_rx (bm, bs, |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 672 | bs->config_required_min_rx_nsec); |
Klement Sekera | aeeac3b | 2017-02-14 07:11:52 +0100 | [diff] [blame] | 673 | bfd_set_timer (bm, bs, now, handling_wakeup); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 674 | break; |
| 675 | case BFD_STATE_init: |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 676 | bs->echo = 0; |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 677 | bfd_set_effective_desired_min_tx (bm, bs, now, |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 678 | bs->config_desired_min_tx_nsec); |
Klement Sekera | aeeac3b | 2017-02-14 07:11:52 +0100 | [diff] [blame] | 679 | bfd_set_timer (bm, bs, now, handling_wakeup); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 680 | break; |
| 681 | case BFD_STATE_up: |
Klement Sekera | aeeac3b | 2017-02-14 07:11:52 +0100 | [diff] [blame] | 682 | bfd_set_effective_desired_min_tx (bm, bs, now, |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 683 | bs->config_desired_min_tx_nsec); |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 684 | if (BFD_POLL_NOT_NEEDED == bs->poll_state) |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 685 | { |
Klement Sekera | 7388448 | 2017-02-23 09:26:30 +0100 | [diff] [blame] | 686 | bfd_set_effective_required_min_rx (bm, bs, |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 687 | bs->config_required_min_rx_nsec); |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 688 | } |
Klement Sekera | aeeac3b | 2017-02-14 07:11:52 +0100 | [diff] [blame] | 689 | bfd_set_timer (bm, bs, now, handling_wakeup); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 690 | break; |
| 691 | } |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame] | 692 | if (vlib_get_thread_index () == 0) |
| 693 | { |
| 694 | bfd_notify_listeners (bm, BFD_LISTEN_EVENT_UPDATE, bs); |
| 695 | } |
| 696 | else |
| 697 | { |
| 698 | /* without RPC - a REGRESSION: state changes are not propagated */ |
| 699 | bfd_notify_listeners_rpc (bs->bs_idx); |
| 700 | } |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | static void |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 704 | bfd_on_config_change (vlib_main_t * vm, vlib_node_runtime_t * rt, |
| 705 | bfd_main_t * bm, bfd_session_t * bs, u64 now) |
| 706 | { |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 707 | /* |
| 708 | * if remote demand mode is set and we need to do a poll, set the next |
| 709 | * timeout so that the session wakes up immediately |
| 710 | */ |
| 711 | if (bs->remote_demand && BFD_POLL_NEEDED == bs->poll_state && |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 712 | bs->poll_state_start_or_timeout_nsec < now) |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 713 | { |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 714 | bs->tx_timeout_nsec = now; |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 715 | } |
| 716 | bfd_recalc_detection_time (bm, bs); |
| 717 | bfd_set_timer (bm, bs, now, 0); |
| 718 | } |
| 719 | |
| 720 | static void |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 721 | 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] | 722 | { |
| 723 | switch (bs->transport) |
| 724 | { |
| 725 | case BFD_TRANSPORT_UDP4: |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame] | 726 | BFD_DBG ("Transport bfd via udp4, bs_idx=%u", bs->bs_idx); |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 727 | bfd_add_udp4_transport (vm, bi, bs, 0 /* is_echo */ ); |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame] | 728 | break; |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 729 | case BFD_TRANSPORT_UDP6: |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame] | 730 | BFD_DBG ("Transport bfd via udp6, bs_idx=%u", bs->bs_idx); |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 731 | bfd_add_udp6_transport (vm, bi, bs, 0 /* is_echo */ ); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 732 | break; |
| 733 | } |
| 734 | } |
| 735 | |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 736 | static int |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 737 | 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] | 738 | { |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 739 | switch (bs->transport) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 740 | { |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 741 | case BFD_TRANSPORT_UDP4: |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 742 | BFD_DBG ("Transport bfd via udp4, bs_idx=%u", bs->bs_idx); |
| 743 | return bfd_transport_udp4 (vm, bi, bs); |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 744 | break; |
| 745 | case BFD_TRANSPORT_UDP6: |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 746 | BFD_DBG ("Transport bfd via udp6, bs_idx=%u", bs->bs_idx); |
| 747 | return bfd_transport_udp6 (vm, bi, bs); |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 748 | break; |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 749 | } |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 750 | return 0; |
| 751 | } |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 752 | |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 753 | static int |
| 754 | 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] | 755 | { |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 756 | switch (bs->transport) |
| 757 | { |
| 758 | case BFD_TRANSPORT_UDP4: |
| 759 | BFD_DBG ("Transport bfd echo via udp4, bs_idx=%u", bs->bs_idx); |
| 760 | return bfd_add_udp4_transport (vm, bi, bs, 1 /* is_echo */ ); |
| 761 | break; |
| 762 | case BFD_TRANSPORT_UDP6: |
| 763 | BFD_DBG ("Transport bfd echo via udp6, bs_idx=%u", bs->bs_idx); |
| 764 | return bfd_add_udp6_transport (vm, bi, bs, 1 /* is_echo */ ); |
| 765 | break; |
| 766 | } |
| 767 | return 0; |
| 768 | } |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 769 | |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 770 | static int |
| 771 | bfd_transport_echo (vlib_main_t * vm, u32 bi, bfd_session_t * bs) |
| 772 | { |
| 773 | switch (bs->transport) |
| 774 | { |
| 775 | case BFD_TRANSPORT_UDP4: |
| 776 | BFD_DBG ("Transport bfd echo via udp4, bs_idx=%u", bs->bs_idx); |
| 777 | return bfd_transport_udp4 (vm, bi, bs); |
| 778 | break; |
| 779 | case BFD_TRANSPORT_UDP6: |
| 780 | BFD_DBG ("Transport bfd echo via udp6, bs_idx=%u", bs->bs_idx); |
| 781 | return bfd_transport_udp6 (vm, bi, bs); |
| 782 | break; |
| 783 | } |
| 784 | return 0; |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 785 | } |
| 786 | |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 787 | #if WITH_LIBSSL > 0 |
| 788 | static void |
| 789 | bfd_add_sha1_auth_section (vlib_buffer_t * b, bfd_session_t * bs) |
| 790 | { |
| 791 | bfd_pkt_with_sha1_auth_t *pkt = vlib_buffer_get_current (b); |
| 792 | bfd_auth_sha1_t *auth = &pkt->sha1_auth; |
| 793 | b->current_length += sizeof (*auth); |
| 794 | pkt->pkt.head.length += sizeof (*auth); |
| 795 | bfd_pkt_set_auth_present (&pkt->pkt); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 796 | clib_memset (auth, 0, sizeof (*auth)); |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 797 | auth->type_len.type = bs->auth.curr_key->auth_type; |
| 798 | /* |
| 799 | * only meticulous authentication types require incrementing seq number |
| 800 | * for every message, but doing so doesn't violate the RFC |
| 801 | */ |
| 802 | ++bs->auth.local_seq_number; |
| 803 | auth->type_len.len = sizeof (bfd_auth_sha1_t); |
| 804 | auth->key_id = bs->auth.curr_bfd_key_id; |
| 805 | auth->seq_num = clib_host_to_net_u32 (bs->auth.local_seq_number); |
| 806 | /* |
| 807 | * first copy the password into the packet, then calculate the hash |
| 808 | * and finally replace the password with the calculated hash |
| 809 | */ |
| 810 | clib_memcpy (auth->hash, bs->auth.curr_key->key, |
| 811 | sizeof (bs->auth.curr_key->key)); |
| 812 | unsigned char hash[sizeof (auth->hash)]; |
| 813 | SHA1 ((unsigned char *) pkt, sizeof (*pkt), hash); |
| 814 | BFD_DBG ("hashing: %U", format_hex_bytes, pkt, sizeof (*pkt)); |
| 815 | clib_memcpy (auth->hash, hash, sizeof (hash)); |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 816 | } |
Klement Sekera | 6f96649 | 2017-02-08 07:42:08 +0100 | [diff] [blame] | 817 | #endif |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 818 | |
| 819 | static void |
| 820 | bfd_add_auth_section (vlib_buffer_t * b, bfd_session_t * bs) |
| 821 | { |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 822 | bfd_main_t *bm = &bfd_main; |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 823 | if (bs->auth.curr_key) |
| 824 | { |
| 825 | const bfd_auth_type_e auth_type = bs->auth.curr_key->auth_type; |
| 826 | switch (auth_type) |
| 827 | { |
| 828 | case BFD_AUTH_TYPE_reserved: |
| 829 | /* fallthrough */ |
| 830 | case BFD_AUTH_TYPE_simple_password: |
| 831 | /* fallthrough */ |
| 832 | case BFD_AUTH_TYPE_keyed_md5: |
| 833 | /* fallthrough */ |
| 834 | case BFD_AUTH_TYPE_meticulous_keyed_md5: |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 835 | vlib_log_crit (bm->log_class, |
| 836 | "internal error, unexpected BFD auth type '%d'", |
| 837 | auth_type); |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 838 | break; |
| 839 | #if WITH_LIBSSL > 0 |
| 840 | case BFD_AUTH_TYPE_keyed_sha1: |
| 841 | /* fallthrough */ |
| 842 | case BFD_AUTH_TYPE_meticulous_keyed_sha1: |
| 843 | bfd_add_sha1_auth_section (b, bs); |
| 844 | break; |
| 845 | #else |
| 846 | case BFD_AUTH_TYPE_keyed_sha1: |
| 847 | /* fallthrough */ |
| 848 | case BFD_AUTH_TYPE_meticulous_keyed_sha1: |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 849 | vlib_log_crit (bm->log_class, |
| 850 | "internal error, unexpected BFD auth type '%d'", |
| 851 | auth_type); |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 852 | break; |
| 853 | #endif |
| 854 | } |
| 855 | } |
| 856 | } |
| 857 | |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 858 | static int |
| 859 | bfd_is_echo_possible (bfd_session_t * bs) |
| 860 | { |
| 861 | if (BFD_STATE_up == bs->local_state && BFD_STATE_up == bs->remote_state && |
| 862 | bs->remote_min_echo_rx_usec > 0) |
| 863 | { |
| 864 | switch (bs->transport) |
| 865 | { |
| 866 | case BFD_TRANSPORT_UDP4: |
| 867 | return bfd_udp_is_echo_available (BFD_TRANSPORT_UDP4); |
| 868 | case BFD_TRANSPORT_UDP6: |
| 869 | return bfd_udp_is_echo_available (BFD_TRANSPORT_UDP6); |
| 870 | } |
| 871 | } |
| 872 | return 0; |
| 873 | } |
| 874 | |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 875 | static void |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 876 | bfd_init_control_frame (bfd_main_t * bm, bfd_session_t * bs, |
| 877 | vlib_buffer_t * b) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 878 | { |
| 879 | bfd_pkt_t *pkt = vlib_buffer_get_current (b); |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 880 | u32 bfd_length = 0; |
| 881 | bfd_length = sizeof (bfd_pkt_t); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 882 | clib_memset (pkt, 0, sizeof (*pkt)); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 883 | bfd_pkt_set_version (pkt, 1); |
| 884 | bfd_pkt_set_diag_code (pkt, bs->local_diag); |
| 885 | bfd_pkt_set_state (pkt, bs->local_state); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 886 | pkt->head.detect_mult = bs->local_detect_mult; |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 887 | pkt->head.length = bfd_length; |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 888 | pkt->my_disc = bs->local_discr; |
| 889 | pkt->your_disc = bs->remote_discr; |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 890 | 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] | 891 | if (bs->echo) |
| 892 | { |
| 893 | pkt->req_min_rx = |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 894 | clib_host_to_net_u32 (bfd_nsec_to_usec |
| 895 | (bs->effective_required_min_rx_nsec)); |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 896 | } |
| 897 | else |
| 898 | { |
| 899 | pkt->req_min_rx = |
| 900 | clib_host_to_net_u32 (bs->config_required_min_rx_usec); |
| 901 | } |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 902 | pkt->req_min_echo_rx = clib_host_to_net_u32 (1); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 903 | b->current_length = bfd_length; |
| 904 | } |
| 905 | |
| 906 | static void |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 907 | bfd_send_echo (vlib_main_t * vm, vlib_node_runtime_t * rt, |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 908 | bfd_main_t * bm, bfd_session_t * bs, u64 now) |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 909 | { |
| 910 | if (!bfd_is_echo_possible (bs)) |
| 911 | { |
| 912 | BFD_DBG ("\nSwitching off echo function: %U", format_bfd_session, bs); |
| 913 | bs->echo = 0; |
| 914 | return; |
| 915 | } |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 916 | if (now >= bs->echo_tx_timeout_nsec) |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 917 | { |
| 918 | BFD_DBG ("\nSending echo packet: %U", format_bfd_session, bs); |
| 919 | u32 bi; |
| 920 | if (vlib_buffer_alloc (vm, &bi, 1) != 1) |
| 921 | { |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 922 | vlib_log_crit (bm->log_class, "buffer allocation failure"); |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 923 | return; |
| 924 | } |
| 925 | vlib_buffer_t *b = vlib_get_buffer (vm, bi); |
| 926 | ASSERT (b->current_data == 0); |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 927 | VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b); |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 928 | bfd_echo_pkt_t *pkt = vlib_buffer_get_current (b); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 929 | clib_memset (pkt, 0, sizeof (*pkt)); |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 930 | pkt->discriminator = bs->local_discr; |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 931 | pkt->expire_time_nsec = |
| 932 | now + bs->echo_transmit_interval_nsec * bs->local_detect_mult; |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 933 | pkt->checksum = |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 934 | bfd_calc_echo_checksum (bs->local_discr, pkt->expire_time_nsec, |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 935 | bs->echo_secret); |
| 936 | b->current_length = sizeof (*pkt); |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 937 | if (!bfd_echo_add_transport_layer (vm, bi, bs)) |
| 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 | if (!bfd_transport_echo (vm, bi, bs)) |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 945 | { |
| 946 | BFD_ERR ("cannot send echo packet out, turning echo off"); |
| 947 | bs->echo = 0; |
| 948 | vlib_buffer_free_one (vm, bi); |
| 949 | return; |
| 950 | } |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 951 | bs->echo_last_tx_nsec = now; |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 952 | bfd_calc_next_echo_tx (bm, bs, now); |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 953 | } |
| 954 | else |
| 955 | { |
| 956 | BFD_DBG |
| 957 | ("No need to send echo packet now, now is %lu, tx_timeout is %lu", |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 958 | now, bs->echo_tx_timeout_nsec); |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 959 | } |
| 960 | } |
| 961 | |
| 962 | static void |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 963 | bfd_send_periodic (vlib_main_t * vm, vlib_node_runtime_t * rt, |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 964 | bfd_main_t * bm, bfd_session_t * bs, u64 now) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 965 | { |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 966 | 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] | 967 | { |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 968 | BFD_DBG ("Remote min rx interval is zero, not sending periodic control " |
| 969 | "frame"); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 970 | return; |
| 971 | } |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 972 | if (BFD_POLL_NOT_NEEDED == bs->poll_state && bs->remote_demand && |
Klement Sekera | d3ba515 | 2017-02-14 03:09:17 +0100 | [diff] [blame] | 973 | BFD_STATE_up == bs->local_state && BFD_STATE_up == bs->remote_state) |
| 974 | { |
| 975 | /* |
| 976 | * A system MUST NOT periodically transmit BFD Control packets if Demand |
| 977 | * mode is active on the remote system (bfd.RemoteDemandMode is 1, |
| 978 | * bfd.SessionState is Up, and bfd.RemoteSessionState is Up) and a Poll |
| 979 | * Sequence is not being transmitted. |
| 980 | */ |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 981 | BFD_DBG ("Remote demand is set, not sending periodic control frame"); |
Klement Sekera | d3ba515 | 2017-02-14 03:09:17 +0100 | [diff] [blame] | 982 | return; |
| 983 | } |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 984 | if (now >= bs->tx_timeout_nsec) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 985 | { |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 986 | BFD_DBG ("\nSending periodic control frame: %U", format_bfd_session, |
| 987 | bs); |
| 988 | u32 bi; |
| 989 | if (vlib_buffer_alloc (vm, &bi, 1) != 1) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 990 | { |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 991 | vlib_log_crit (bm->log_class, "buffer allocation failure"); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 992 | return; |
| 993 | } |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 994 | vlib_buffer_t *b = vlib_get_buffer (vm, bi); |
| 995 | ASSERT (b->current_data == 0); |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 996 | VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b); |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 997 | bfd_init_control_frame (bm, bs, b); |
| 998 | switch (bs->poll_state) |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 999 | { |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 1000 | case BFD_POLL_NEEDED: |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 1001 | if (now < bs->poll_state_start_or_timeout_nsec) |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 1002 | { |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 1003 | BFD_DBG ("Cannot start a poll sequence yet, need to wait for " |
| 1004 | BFD_CLK_FMT, |
| 1005 | BFD_CLK_PRN (bs->poll_state_start_or_timeout_nsec - |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 1006 | now)); |
| 1007 | break; |
| 1008 | } |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 1009 | bs->poll_state_start_or_timeout_nsec = now; |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 1010 | bfd_set_poll_state (bs, BFD_POLL_IN_PROGRESS); |
| 1011 | /* fallthrough */ |
| 1012 | case BFD_POLL_IN_PROGRESS: |
| 1013 | case BFD_POLL_IN_PROGRESS_AND_QUEUED: |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 1014 | bfd_pkt_set_poll (vlib_buffer_get_current (b)); |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 1015 | 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] | 1016 | break; |
| 1017 | case BFD_POLL_NOT_NEEDED: |
| 1018 | /* fallthrough */ |
| 1019 | break; |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 1020 | } |
| 1021 | bfd_add_auth_section (b, bs); |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 1022 | bfd_add_transport_layer (vm, bi, bs); |
| 1023 | if (!bfd_transport_control_frame (vm, bi, bs)) |
| 1024 | { |
| 1025 | vlib_buffer_free_one (vm, bi); |
| 1026 | } |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 1027 | bs->last_tx_nsec = now; |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1028 | bfd_calc_next_tx (bm, bs, now); |
| 1029 | } |
| 1030 | else |
| 1031 | { |
Klement Sekera | 637b9c4 | 2016-12-08 05:19:14 +0100 | [diff] [blame] | 1032 | BFD_DBG |
| 1033 | ("No need to send control frame now, now is %lu, tx_timeout is %lu", |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 1034 | now, bs->tx_timeout_nsec); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1035 | } |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1036 | } |
| 1037 | |
| 1038 | void |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 1039 | 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] | 1040 | bfd_main_t * bm, bfd_session_t * bs, |
| 1041 | int is_local) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1042 | { |
| 1043 | 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] | 1044 | bfd_init_control_frame (bm, bs, b); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1045 | bfd_pkt_set_final (vlib_buffer_get_current (b)); |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 1046 | bfd_add_auth_section (b, bs); |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 1047 | u32 bi = vlib_get_buffer_index (vm, b); |
| 1048 | bfd_add_transport_layer (vm, bi, bs); |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 1049 | bs->last_tx_nsec = bfd_time_now_nsec (vm, NULL); |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 1050 | /* |
| 1051 | * RFC allows to include changes in final frame, so if there were any |
| 1052 | * pending, we already did that, thus we can clear any pending poll needs |
| 1053 | */ |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 1054 | bfd_set_poll_state (bs, BFD_POLL_NOT_NEEDED); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1055 | } |
| 1056 | |
| 1057 | static void |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 1058 | bfd_check_rx_timeout (vlib_main_t * vm, bfd_main_t * bm, bfd_session_t * bs, |
| 1059 | u64 now, int handling_wakeup) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1060 | { |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 1061 | if (bs->last_rx_nsec + bs->detection_time_nsec <= now) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1062 | { |
| 1063 | BFD_DBG ("Rx timeout, session goes down"); |
Klement Sekera | 4d39f9c | 2020-01-17 10:01:52 +0000 | [diff] [blame] | 1064 | /* |
| 1065 | * RFC 5880 6.8.1. State Variables |
| 1066 | |
| 1067 | * bfd.RemoteDiscr |
| 1068 | |
| 1069 | * The remote discriminator for this BFD session. This is the |
| 1070 | * discriminator chosen by the remote system, and is totally opaque |
| 1071 | * to the local system. This MUST be initialized to zero. If a |
| 1072 | * period of a Detection Time passes without the receipt of a valid, |
| 1073 | * authenticated BFD packet from the remote system, this variable |
| 1074 | * MUST be set to zero. |
| 1075 | */ |
| 1076 | bs->remote_discr = 0; |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1077 | bfd_set_diag (bs, BFD_DIAG_CODE_det_time_exp); |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 1078 | bfd_set_state (vm, bm, bs, BFD_STATE_down, handling_wakeup); |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 1079 | /* |
| 1080 | * If the remote system does not receive any |
| 1081 | * BFD Control packets for a Detection Time, it SHOULD reset |
| 1082 | * bfd.RemoteMinRxInterval to its initial value of 1 (per section 6.8.1, |
| 1083 | * since it is no longer required to maintain previous session state) |
| 1084 | * and then can transmit at its own rate. |
| 1085 | */ |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 1086 | bfd_set_remote_required_min_rx (bm, bs, now, 1); |
| 1087 | } |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 1088 | else if (bs->echo |
| 1089 | && bs->echo_last_rx_nsec + |
| 1090 | bs->echo_transmit_interval_nsec * bs->local_detect_mult <= now) |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 1091 | { |
| 1092 | BFD_DBG ("Echo rx timeout, session goes down"); |
| 1093 | bfd_set_diag (bs, BFD_DIAG_CODE_echo_failed); |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 1094 | bfd_set_state (vm, bm, bs, BFD_STATE_down, handling_wakeup); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1095 | } |
| 1096 | } |
| 1097 | |
| 1098 | void |
| 1099 | bfd_on_timeout (vlib_main_t * vm, vlib_node_runtime_t * rt, bfd_main_t * bm, |
| 1100 | bfd_session_t * bs, u64 now) |
| 1101 | { |
| 1102 | BFD_DBG ("Timeout for bs_idx=%lu", bs->bs_idx); |
| 1103 | switch (bs->local_state) |
| 1104 | { |
| 1105 | case BFD_STATE_admin_down: |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 1106 | bfd_send_periodic (vm, rt, bm, bs, now); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1107 | break; |
| 1108 | case BFD_STATE_down: |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 1109 | bfd_send_periodic (vm, rt, bm, bs, now); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1110 | break; |
| 1111 | case BFD_STATE_init: |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 1112 | bfd_check_rx_timeout (vm, bm, bs, now, 1); |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 1113 | bfd_send_periodic (vm, rt, bm, bs, now); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1114 | break; |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 1115 | case BFD_STATE_up: |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 1116 | bfd_check_rx_timeout (vm, bm, bs, now, 1); |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 1117 | if (BFD_POLL_NOT_NEEDED == bs->poll_state && !bs->echo && |
| 1118 | bfd_is_echo_possible (bs)) |
| 1119 | { |
| 1120 | /* switch on echo function as main detection method now */ |
| 1121 | BFD_DBG ("Switching on echo function, bs_idx=%u", bs->bs_idx); |
| 1122 | bs->echo = 1; |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 1123 | bs->echo_last_rx_nsec = now; |
| 1124 | bs->echo_tx_timeout_nsec = now; |
Klement Sekera | 7388448 | 2017-02-23 09:26:30 +0100 | [diff] [blame] | 1125 | bfd_set_effective_required_min_rx (bm, bs, |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 1126 | clib_max |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 1127 | (bm->min_required_min_rx_while_echo_nsec, |
| 1128 | bs->config_required_min_rx_nsec)); |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 1129 | bfd_set_poll_state (bs, BFD_POLL_NEEDED); |
| 1130 | } |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 1131 | bfd_send_periodic (vm, rt, bm, bs, now); |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 1132 | if (bs->echo) |
| 1133 | { |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 1134 | bfd_send_echo (vm, rt, bm, bs, now); |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 1135 | } |
| 1136 | break; |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1137 | } |
| 1138 | } |
| 1139 | |
| 1140 | /* |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1141 | * bfd process node function |
| 1142 | */ |
| 1143 | static uword |
| 1144 | bfd_process (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f) |
| 1145 | { |
| 1146 | bfd_main_t *bm = &bfd_main; |
| 1147 | u32 *expired = 0; |
| 1148 | uword event_type, *event_data = 0; |
| 1149 | |
| 1150 | /* So we can send events to the bfd process */ |
| 1151 | bm->bfd_process_node_index = bfd_process_node.index; |
| 1152 | |
| 1153 | while (1) |
| 1154 | { |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 1155 | f64 vm_time; |
| 1156 | u64 now = bfd_time_now_nsec (vm, &vm_time); |
| 1157 | BFD_DBG ("wakeup, now is %llunsec, vlib_time_now() is %.9f", now, |
| 1158 | vm_time); |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame] | 1159 | bfd_lock (bm); |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 1160 | f64 timeout; |
| 1161 | if (pool_elts (bm->sessions)) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1162 | { |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 1163 | u32 first_expires_in_ticks = |
| 1164 | TW (tw_timer_first_expires_in_ticks) (&bm->wheel); |
| 1165 | if (!first_expires_in_ticks) |
Klement Sekera | 0c1519b | 2016-12-08 05:03:32 +0100 | [diff] [blame] | 1166 | { |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 1167 | BFD_DBG |
| 1168 | ("tw_timer_first_expires_in_ticks(%p) returns 0ticks", |
| 1169 | &bm->wheel); |
| 1170 | timeout = bm->wheel.next_run_time - vm_time; |
| 1171 | BFD_DBG ("wheel.next_run_time is %.9f", |
| 1172 | bm->wheel.next_run_time); |
| 1173 | u64 next_expire_nsec = now + timeout * SEC_PER_NSEC; |
| 1174 | bm->bfd_process_next_wakeup_nsec = next_expire_nsec; |
| 1175 | bfd_unlock (bm); |
Klement Sekera | 0c1519b | 2016-12-08 05:03:32 +0100 | [diff] [blame] | 1176 | } |
| 1177 | else |
| 1178 | { |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 1179 | BFD_DBG ("tw_timer_first_expires_in_ticks(%p) returns %luticks", |
| 1180 | &bm->wheel, first_expires_in_ticks); |
| 1181 | u64 next_expire_nsec = |
| 1182 | now + first_expires_in_ticks * bm->nsec_per_tw_tick; |
| 1183 | bm->bfd_process_next_wakeup_nsec = next_expire_nsec; |
| 1184 | bfd_unlock (bm); |
| 1185 | timeout = (next_expire_nsec - now) * SEC_PER_NSEC; |
Klement Sekera | 0c1519b | 2016-12-08 05:03:32 +0100 | [diff] [blame] | 1186 | } |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 1187 | BFD_DBG ("vlib_process_wait_for_event_or_clock(vm, %.09f)", |
| 1188 | timeout); |
| 1189 | (void) vlib_process_wait_for_event_or_clock (vm, timeout); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1190 | } |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 1191 | else |
| 1192 | { |
| 1193 | (void) vlib_process_wait_for_event (vm); |
| 1194 | } |
| 1195 | event_type = vlib_process_get_events (vm, &event_data); |
| 1196 | now = bfd_time_now_nsec (vm, &vm_time); |
Eyal Bari | 0db9b04 | 2018-10-11 14:09:58 +0300 | [diff] [blame] | 1197 | uword *session_index; |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1198 | switch (event_type) |
| 1199 | { |
| 1200 | case ~0: /* no events => timeout */ |
| 1201 | /* nothing to do here */ |
| 1202 | break; |
| 1203 | case BFD_EVENT_RESCHEDULE: |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 1204 | BFD_DBG ("reschedule event"); |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame] | 1205 | bfd_lock (bm); |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 1206 | bm->bfd_process_wakeup_event_delay_nsec = |
| 1207 | now - bm->bfd_process_wakeup_event_start_nsec; |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame] | 1208 | bm->bfd_process_wakeup_events_in_flight--; |
| 1209 | bfd_unlock (bm); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1210 | /* nothing to do here - reschedule is done automatically after |
| 1211 | * each event or timeout */ |
| 1212 | break; |
| 1213 | case BFD_EVENT_NEW_SESSION: |
Eyal Bari | 0db9b04 | 2018-10-11 14:09:58 +0300 | [diff] [blame] | 1214 | vec_foreach (session_index, event_data) |
| 1215 | { |
| 1216 | bfd_lock (bm); |
| 1217 | if (!pool_is_free_index (bm->sessions, *session_index)) |
| 1218 | { |
| 1219 | bfd_session_t *bs = |
| 1220 | pool_elt_at_index (bm->sessions, *session_index); |
| 1221 | bfd_send_periodic (vm, rt, bm, bs, now); |
| 1222 | bfd_set_timer (bm, bs, now, 1); |
| 1223 | } |
| 1224 | else |
| 1225 | { |
| 1226 | BFD_DBG ("Ignoring event for non-existent session index %u", |
| 1227 | (u32) * session_index); |
| 1228 | } |
| 1229 | bfd_unlock (bm); |
| 1230 | } |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1231 | break; |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 1232 | case BFD_EVENT_CONFIG_CHANGED: |
Eyal Bari | 0db9b04 | 2018-10-11 14:09:58 +0300 | [diff] [blame] | 1233 | vec_foreach (session_index, event_data) |
| 1234 | { |
| 1235 | bfd_lock (bm); |
| 1236 | if (!pool_is_free_index (bm->sessions, *session_index)) |
| 1237 | { |
| 1238 | bfd_session_t *bs = |
| 1239 | pool_elt_at_index (bm->sessions, *session_index); |
| 1240 | bfd_on_config_change (vm, rt, bm, bs, now); |
| 1241 | } |
| 1242 | else |
| 1243 | { |
| 1244 | BFD_DBG ("Ignoring event for non-existent session index %u", |
| 1245 | (u32) * session_index); |
| 1246 | } |
| 1247 | bfd_unlock (bm); |
| 1248 | } |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 1249 | break; |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1250 | default: |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 1251 | 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] | 1252 | break; |
| 1253 | } |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 1254 | BFD_DBG ("tw_timer_expire_timers_vec(%p, %.04f);", &bm->wheel, vm_time); |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame] | 1255 | bfd_lock (bm); |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 1256 | expired = |
| 1257 | TW (tw_timer_expire_timers_vec) (&bm->wheel, vm_time, expired); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1258 | BFD_DBG ("Expired %d elements", vec_len (expired)); |
| 1259 | u32 *p = NULL; |
| 1260 | vec_foreach (p, expired) |
| 1261 | { |
| 1262 | const u32 bs_idx = *p; |
| 1263 | if (!pool_is_free_index (bm->sessions, bs_idx)) |
| 1264 | { |
| 1265 | bfd_session_t *bs = pool_elt_at_index (bm->sessions, bs_idx); |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 1266 | bs->tw_id = 0; /* timer is gone because it expired */ |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1267 | bfd_on_timeout (vm, rt, bm, bs, now); |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 1268 | bfd_set_timer (bm, bs, now, 1); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1269 | } |
| 1270 | } |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame] | 1271 | bfd_unlock (bm); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1272 | if (expired) |
| 1273 | { |
| 1274 | _vec_len (expired) = 0; |
| 1275 | } |
| 1276 | if (event_data) |
| 1277 | { |
| 1278 | _vec_len (event_data) = 0; |
| 1279 | } |
| 1280 | } |
| 1281 | |
| 1282 | return 0; |
| 1283 | } |
| 1284 | |
| 1285 | /* |
| 1286 | * bfd process node declaration |
| 1287 | */ |
| 1288 | /* *INDENT-OFF* */ |
| 1289 | VLIB_REGISTER_NODE (bfd_process_node, static) = { |
| 1290 | .function = bfd_process, |
| 1291 | .type = VLIB_NODE_TYPE_PROCESS, |
| 1292 | .name = "bfd-process", |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame] | 1293 | .n_next_nodes = 0, |
| 1294 | .next_nodes = {}, |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1295 | }; |
| 1296 | /* *INDENT-ON* */ |
| 1297 | |
| 1298 | static clib_error_t * |
| 1299 | bfd_sw_interface_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags) |
| 1300 | { |
| 1301 | // bfd_main_t *bm = &bfd_main; |
| 1302 | // vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index); |
| 1303 | if (!(flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)) |
| 1304 | { |
| 1305 | /* TODO */ |
| 1306 | } |
| 1307 | return 0; |
| 1308 | } |
| 1309 | |
| 1310 | VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (bfd_sw_interface_up_down); |
| 1311 | |
| 1312 | static clib_error_t * |
| 1313 | bfd_hw_interface_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags) |
| 1314 | { |
| 1315 | // bfd_main_t *bm = &bfd_main; |
| 1316 | if (flags & VNET_HW_INTERFACE_FLAG_LINK_UP) |
| 1317 | { |
| 1318 | /* TODO */ |
| 1319 | } |
| 1320 | return 0; |
| 1321 | } |
| 1322 | |
| 1323 | VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION (bfd_hw_interface_up_down); |
| 1324 | |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 1325 | void |
| 1326 | bfd_register_listener (bfd_notify_fn_t fn) |
| 1327 | { |
| 1328 | bfd_main_t *bm = &bfd_main; |
| 1329 | |
| 1330 | vec_add1 (bm->listeners, fn); |
| 1331 | } |
| 1332 | |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1333 | /* |
| 1334 | * setup function |
| 1335 | */ |
| 1336 | static clib_error_t * |
| 1337 | bfd_main_init (vlib_main_t * vm) |
| 1338 | { |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame] | 1339 | vlib_thread_main_t *tm = &vlib_thread_main; |
| 1340 | u32 n_vlib_mains = tm->n_vlib_mains; |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 1341 | #if BFD_DEBUG |
| 1342 | setbuf (stdout, NULL); |
| 1343 | #endif |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1344 | bfd_main_t *bm = &bfd_main; |
| 1345 | bm->random_seed = random_default_seed (); |
| 1346 | bm->vlib_main = vm; |
| 1347 | bm->vnet_main = vnet_get_main (); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 1348 | clib_memset (&bm->wheel, 0, sizeof (bm->wheel)); |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 1349 | bm->nsec_per_tw_tick = (f64) NSEC_PER_SEC / BFD_TW_TPS; |
| 1350 | bm->default_desired_min_tx_nsec = |
| 1351 | bfd_usec_to_nsec (BFD_DEFAULT_DESIRED_MIN_TX_USEC); |
| 1352 | bm->min_required_min_rx_while_echo_nsec = |
| 1353 | bfd_usec_to_nsec (BFD_REQUIRED_MIN_RX_USEC_WHILE_ECHO); |
| 1354 | BFD_DBG ("tw_timer_wheel_init(%p, %p, %.04f, %u)", &bm->wheel, NULL, |
| 1355 | 1.00 / BFD_TW_TPS, ~0); |
| 1356 | TW (tw_timer_wheel_init) (&bm->wheel, NULL, 1.00 / BFD_TW_TPS, ~0); |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 1357 | bm->log_class = vlib_log_register_class ("bfd", 0); |
| 1358 | vlib_log_debug (bm->log_class, "initialized"); |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame] | 1359 | bm->owner_thread_index = ~0; |
| 1360 | if (n_vlib_mains > 1) |
| 1361 | clib_spinlock_init (&bm->lock); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1362 | return 0; |
| 1363 | } |
| 1364 | |
| 1365 | VLIB_INIT_FUNCTION (bfd_main_init); |
| 1366 | |
| 1367 | bfd_session_t * |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 1368 | bfd_get_session (bfd_main_t * bm, bfd_transport_e t) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1369 | { |
| 1370 | bfd_session_t *result; |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame] | 1371 | |
| 1372 | bfd_lock (bm); |
| 1373 | |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1374 | pool_get (bm->sessions, result); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 1375 | clib_memset (result, 0, sizeof (*result)); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1376 | result->bs_idx = result - bm->sessions; |
| 1377 | result->transport = t; |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 1378 | const unsigned limit = 1000; |
| 1379 | unsigned counter = 0; |
| 1380 | do |
| 1381 | { |
| 1382 | result->local_discr = random_u32 (&bm->random_seed); |
| 1383 | if (counter > limit) |
| 1384 | { |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 1385 | vlib_log_crit (bm->log_class, |
| 1386 | "couldn't allocate unused session discriminator even " |
| 1387 | "after %u tries!", limit); |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 1388 | pool_put (bm->sessions, result); |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame] | 1389 | bfd_unlock (bm); |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 1390 | return NULL; |
| 1391 | } |
| 1392 | ++counter; |
| 1393 | } |
| 1394 | while (hash_get (bm->session_by_disc, result->local_discr)); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1395 | bfd_set_defaults (bm, result); |
| 1396 | hash_set (bm->session_by_disc, result->local_discr, result->bs_idx); |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame] | 1397 | bfd_unlock (bm); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1398 | return result; |
| 1399 | } |
| 1400 | |
| 1401 | void |
| 1402 | bfd_put_session (bfd_main_t * bm, bfd_session_t * bs) |
| 1403 | { |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame] | 1404 | bfd_lock (bm); |
| 1405 | |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 1406 | vlib_log_info (bm->log_class, "delete session: %U", |
| 1407 | format_bfd_session_brief, bs); |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 1408 | bfd_notify_listeners (bm, BFD_LISTEN_EVENT_DELETE, bs); |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 1409 | if (bs->auth.curr_key) |
| 1410 | { |
| 1411 | --bs->auth.curr_key->use_count; |
| 1412 | } |
| 1413 | if (bs->auth.next_key) |
| 1414 | { |
| 1415 | --bs->auth.next_key->use_count; |
| 1416 | } |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1417 | hash_unset (bm->session_by_disc, bs->local_discr); |
| 1418 | pool_put (bm->sessions, bs); |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame] | 1419 | bfd_unlock (bm); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1420 | } |
| 1421 | |
| 1422 | bfd_session_t * |
| 1423 | bfd_find_session_by_idx (bfd_main_t * bm, uword bs_idx) |
| 1424 | { |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame] | 1425 | bfd_lock_check (bm); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1426 | if (!pool_is_free_index (bm->sessions, bs_idx)) |
| 1427 | { |
| 1428 | return pool_elt_at_index (bm->sessions, bs_idx); |
| 1429 | } |
| 1430 | return NULL; |
| 1431 | } |
| 1432 | |
| 1433 | bfd_session_t * |
| 1434 | bfd_find_session_by_disc (bfd_main_t * bm, u32 disc) |
| 1435 | { |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame] | 1436 | bfd_lock_check (bm); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1437 | uword *p = hash_get (bfd_main.session_by_disc, disc); |
| 1438 | if (p) |
| 1439 | { |
| 1440 | return pool_elt_at_index (bfd_main.sessions, *p); |
| 1441 | } |
| 1442 | return NULL; |
| 1443 | } |
| 1444 | |
| 1445 | /** |
| 1446 | * @brief verify bfd packet - common checks |
| 1447 | * |
| 1448 | * @param pkt |
| 1449 | * |
| 1450 | * @return 1 if bfd packet is valid |
| 1451 | */ |
| 1452 | int |
| 1453 | bfd_verify_pkt_common (const bfd_pkt_t * pkt) |
| 1454 | { |
| 1455 | if (1 != bfd_pkt_get_version (pkt)) |
| 1456 | { |
| 1457 | BFD_ERR ("BFD verification failed - unexpected version: '%d'", |
| 1458 | bfd_pkt_get_version (pkt)); |
| 1459 | return 0; |
| 1460 | } |
| 1461 | if (pkt->head.length < sizeof (bfd_pkt_t) || |
| 1462 | (bfd_pkt_get_auth_present (pkt) && |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 1463 | pkt->head.length < sizeof (bfd_pkt_with_common_auth_t))) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1464 | { |
| 1465 | BFD_ERR ("BFD verification failed - unexpected length: '%d' (auth " |
| 1466 | "present: %d)", |
| 1467 | pkt->head.length, bfd_pkt_get_auth_present (pkt)); |
| 1468 | return 0; |
| 1469 | } |
| 1470 | if (!pkt->head.detect_mult) |
| 1471 | { |
| 1472 | BFD_ERR ("BFD verification failed - unexpected detect-mult: '%d'", |
| 1473 | pkt->head.detect_mult); |
| 1474 | return 0; |
| 1475 | } |
| 1476 | if (bfd_pkt_get_multipoint (pkt)) |
| 1477 | { |
| 1478 | BFD_ERR ("BFD verification failed - unexpected multipoint: '%d'", |
| 1479 | bfd_pkt_get_multipoint (pkt)); |
| 1480 | return 0; |
| 1481 | } |
| 1482 | if (!pkt->my_disc) |
| 1483 | { |
| 1484 | BFD_ERR ("BFD verification failed - unexpected my-disc: '%d'", |
| 1485 | pkt->my_disc); |
| 1486 | return 0; |
| 1487 | } |
| 1488 | if (!pkt->your_disc) |
| 1489 | { |
| 1490 | const u8 pkt_state = bfd_pkt_get_state (pkt); |
| 1491 | if (pkt_state != BFD_STATE_down && pkt_state != BFD_STATE_admin_down) |
| 1492 | { |
| 1493 | BFD_ERR ("BFD verification failed - unexpected state: '%s' " |
| 1494 | "(your-disc is zero)", bfd_state_string (pkt_state)); |
| 1495 | return 0; |
| 1496 | } |
| 1497 | } |
| 1498 | return 1; |
| 1499 | } |
| 1500 | |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 1501 | static void |
| 1502 | bfd_session_switch_auth_to_next (bfd_session_t * bs) |
| 1503 | { |
| 1504 | BFD_DBG ("Switching authentication key from %U to %U for bs_idx=%u", |
| 1505 | format_bfd_auth_key, bs->auth.curr_key, format_bfd_auth_key, |
| 1506 | bs->auth.next_key, bs->bs_idx); |
| 1507 | bs->auth.is_delayed = 0; |
| 1508 | if (bs->auth.curr_key) |
| 1509 | { |
| 1510 | --bs->auth.curr_key->use_count; |
| 1511 | } |
| 1512 | bs->auth.curr_key = bs->auth.next_key; |
| 1513 | bs->auth.next_key = NULL; |
| 1514 | bs->auth.curr_bfd_key_id = bs->auth.next_bfd_key_id; |
| 1515 | } |
| 1516 | |
| 1517 | static int |
| 1518 | bfd_auth_type_is_meticulous (bfd_auth_type_e auth_type) |
| 1519 | { |
| 1520 | if (BFD_AUTH_TYPE_meticulous_keyed_md5 == auth_type || |
| 1521 | BFD_AUTH_TYPE_meticulous_keyed_sha1 == auth_type) |
| 1522 | { |
| 1523 | return 1; |
| 1524 | } |
| 1525 | return 0; |
| 1526 | } |
| 1527 | |
| 1528 | static int |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 1529 | bfd_verify_pkt_auth_seq_num (vlib_main_t * vm, bfd_session_t * bs, |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 1530 | u32 received_seq_num, int is_meticulous) |
| 1531 | { |
| 1532 | /* |
| 1533 | * RFC 5880 6.8.1: |
| 1534 | * |
| 1535 | * This variable MUST be set to zero after no packets have been |
| 1536 | * received on this session for at least twice the Detection Time. |
| 1537 | */ |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 1538 | u64 now = bfd_time_now_nsec (vm, NULL); |
| 1539 | if (now - bs->last_rx_nsec > bs->detection_time_nsec * 2) |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 1540 | { |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 1541 | BFD_DBG ("BFD peer unresponsive for %lu nsec, which is > 2 * " |
| 1542 | "detection_time=%u nsec, resetting remote_seq_number_known " |
| 1543 | "flag", now - bs->last_rx_nsec, bs->detection_time_nsec * 2); |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 1544 | bs->auth.remote_seq_number_known = 0; |
| 1545 | } |
| 1546 | if (bs->auth.remote_seq_number_known) |
| 1547 | { |
| 1548 | /* remote sequence number is known, verify its validity */ |
| 1549 | const u32 max_u32 = 0xffffffff; |
| 1550 | /* the calculation might wrap, account for the special case... */ |
| 1551 | if (bs->auth.remote_seq_number > max_u32 - 3 * bs->local_detect_mult) |
| 1552 | { |
| 1553 | /* |
| 1554 | * special case |
| 1555 | * |
| 1556 | * x y z |
| 1557 | * |----------+----------------------------+-----------| |
| 1558 | * 0 ^ ^ 0xffffffff |
| 1559 | * | remote_seq_num------+ |
| 1560 | * | |
| 1561 | * +-----(remote_seq_num + 3*detect_mult) % * 0xffffffff |
| 1562 | * |
| 1563 | * x + y + z = 0xffffffff |
| 1564 | * x + z = 3 * detect_mult |
| 1565 | */ |
| 1566 | const u32 z = max_u32 - bs->auth.remote_seq_number; |
| 1567 | const u32 x = 3 * bs->local_detect_mult - z; |
| 1568 | if (received_seq_num > x && |
| 1569 | received_seq_num < bs->auth.remote_seq_number + is_meticulous) |
| 1570 | { |
| 1571 | BFD_ERR |
| 1572 | ("Recvd sequence number=%u out of ranges <0, %u>, <%u, %u>", |
| 1573 | received_seq_num, x, |
| 1574 | bs->auth.remote_seq_number + is_meticulous, max_u32); |
| 1575 | return 0; |
| 1576 | } |
| 1577 | } |
| 1578 | else |
| 1579 | { |
| 1580 | /* regular case */ |
| 1581 | const u32 min = bs->auth.remote_seq_number + is_meticulous; |
| 1582 | const u32 max = |
| 1583 | bs->auth.remote_seq_number + 3 * bs->local_detect_mult; |
| 1584 | if (received_seq_num < min || received_seq_num > max) |
| 1585 | { |
| 1586 | BFD_ERR ("Recvd sequence number=%u out of range <%u, %u>", |
| 1587 | received_seq_num, min, max); |
| 1588 | return 0; |
| 1589 | } |
| 1590 | } |
| 1591 | } |
| 1592 | return 1; |
| 1593 | } |
| 1594 | |
| 1595 | static int |
| 1596 | bfd_verify_pkt_auth_key_sha1 (const bfd_pkt_t * pkt, u32 pkt_size, |
| 1597 | bfd_session_t * bs, u8 bfd_key_id, |
| 1598 | bfd_auth_key_t * auth_key) |
| 1599 | { |
| 1600 | ASSERT (auth_key->auth_type == BFD_AUTH_TYPE_keyed_sha1 || |
| 1601 | auth_key->auth_type == BFD_AUTH_TYPE_meticulous_keyed_sha1); |
| 1602 | |
| 1603 | u8 result[SHA_DIGEST_LENGTH]; |
| 1604 | bfd_pkt_with_common_auth_t *with_common = (void *) pkt; |
| 1605 | if (pkt_size < sizeof (*with_common)) |
| 1606 | { |
| 1607 | BFD_ERR ("Packet size too small to hold authentication common header"); |
| 1608 | return 0; |
| 1609 | } |
| 1610 | if (with_common->common_auth.type != auth_key->auth_type) |
| 1611 | { |
| 1612 | BFD_ERR ("BFD auth type mismatch, packet auth=%d:%s doesn't match " |
| 1613 | "in-use auth=%d:%s", |
| 1614 | with_common->common_auth.type, |
| 1615 | bfd_auth_type_str (with_common->common_auth.type), |
| 1616 | auth_key->auth_type, bfd_auth_type_str (auth_key->auth_type)); |
| 1617 | return 0; |
| 1618 | } |
| 1619 | bfd_pkt_with_sha1_auth_t *with_sha1 = (void *) pkt; |
| 1620 | if (pkt_size < sizeof (*with_sha1) || |
| 1621 | with_sha1->sha1_auth.type_len.len < sizeof (with_sha1->sha1_auth)) |
| 1622 | { |
| 1623 | BFD_ERR |
| 1624 | ("BFD size mismatch, payload size=%u, expected=%u, auth_len=%u, " |
| 1625 | "expected=%u", pkt_size, sizeof (*with_sha1), |
| 1626 | with_sha1->sha1_auth.type_len.len, sizeof (with_sha1->sha1_auth)); |
| 1627 | return 0; |
| 1628 | } |
| 1629 | if (with_sha1->sha1_auth.key_id != bfd_key_id) |
| 1630 | { |
| 1631 | BFD_ERR |
| 1632 | ("BFD key ID mismatch, packet key ID=%u doesn't match key ID=%u%s", |
| 1633 | with_sha1->sha1_auth.key_id, bfd_key_id, |
| 1634 | bs-> |
| 1635 | auth.is_delayed ? " (but a delayed auth change is scheduled)" : ""); |
| 1636 | return 0; |
| 1637 | } |
| 1638 | SHA_CTX ctx; |
| 1639 | if (!SHA1_Init (&ctx)) |
| 1640 | { |
| 1641 | BFD_ERR ("SHA1_Init failed"); |
| 1642 | return 0; |
| 1643 | } |
| 1644 | /* ignore last 20 bytes - use the actual key data instead pkt data */ |
| 1645 | if (!SHA1_Update (&ctx, with_sha1, |
| 1646 | sizeof (*with_sha1) - sizeof (with_sha1->sha1_auth.hash))) |
| 1647 | { |
| 1648 | BFD_ERR ("SHA1_Update failed"); |
| 1649 | return 0; |
| 1650 | } |
| 1651 | if (!SHA1_Update (&ctx, auth_key->key, sizeof (auth_key->key))) |
| 1652 | { |
| 1653 | BFD_ERR ("SHA1_Update failed"); |
| 1654 | return 0; |
| 1655 | } |
| 1656 | if (!SHA1_Final (result, &ctx)) |
| 1657 | { |
| 1658 | BFD_ERR ("SHA1_Final failed"); |
| 1659 | return 0; |
| 1660 | } |
| 1661 | if (0 == memcmp (result, with_sha1->sha1_auth.hash, SHA_DIGEST_LENGTH)) |
| 1662 | { |
| 1663 | return 1; |
| 1664 | } |
| 1665 | BFD_ERR ("SHA1 hash: %U doesn't match the expected value: %U", |
| 1666 | format_hex_bytes, with_sha1->sha1_auth.hash, SHA_DIGEST_LENGTH, |
| 1667 | format_hex_bytes, result, SHA_DIGEST_LENGTH); |
| 1668 | return 0; |
| 1669 | } |
| 1670 | |
| 1671 | static int |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 1672 | bfd_verify_pkt_auth_key (vlib_main_t * vm, const bfd_pkt_t * pkt, |
| 1673 | u32 pkt_size, bfd_session_t * bs, u8 bfd_key_id, |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 1674 | bfd_auth_key_t * auth_key) |
| 1675 | { |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 1676 | bfd_main_t *bm = &bfd_main; |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 1677 | switch (auth_key->auth_type) |
| 1678 | { |
| 1679 | case BFD_AUTH_TYPE_reserved: |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 1680 | vlib_log_err (bm->log_class, |
| 1681 | "internal error, unexpected auth_type=%d:%s", |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 1682 | auth_key->auth_type, |
| 1683 | bfd_auth_type_str (auth_key->auth_type)); |
| 1684 | return 0; |
| 1685 | case BFD_AUTH_TYPE_simple_password: |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 1686 | vlib_log_err (bm->log_class, |
| 1687 | "internal error, not implemented, unexpected auth_type=%d:%s", |
| 1688 | auth_key->auth_type, |
| 1689 | bfd_auth_type_str (auth_key->auth_type)); |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 1690 | return 0; |
| 1691 | case BFD_AUTH_TYPE_keyed_md5: |
| 1692 | /* fallthrough */ |
| 1693 | case BFD_AUTH_TYPE_meticulous_keyed_md5: |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 1694 | vlib_log_err |
| 1695 | (bm->log_class, |
| 1696 | "internal error, not implemented, unexpected auth_type=%d:%s", |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 1697 | auth_key->auth_type, bfd_auth_type_str (auth_key->auth_type)); |
| 1698 | return 0; |
| 1699 | case BFD_AUTH_TYPE_keyed_sha1: |
| 1700 | /* fallthrough */ |
| 1701 | case BFD_AUTH_TYPE_meticulous_keyed_sha1: |
| 1702 | #if WITH_LIBSSL > 0 |
| 1703 | do |
| 1704 | { |
| 1705 | const u32 seq_num = clib_net_to_host_u32 (((bfd_pkt_with_sha1_auth_t |
| 1706 | *) pkt)-> |
| 1707 | sha1_auth.seq_num); |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 1708 | return bfd_verify_pkt_auth_seq_num (vm, bs, seq_num, |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 1709 | bfd_auth_type_is_meticulous |
| 1710 | (auth_key->auth_type)) |
| 1711 | && bfd_verify_pkt_auth_key_sha1 (pkt, pkt_size, bs, bfd_key_id, |
| 1712 | auth_key); |
| 1713 | } |
| 1714 | while (0); |
| 1715 | #else |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 1716 | vlib_log_err |
| 1717 | (bm->log_class, |
| 1718 | "internal error, attempt to use SHA1 without SSL support"); |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 1719 | return 0; |
| 1720 | #endif |
| 1721 | } |
| 1722 | return 0; |
| 1723 | } |
| 1724 | |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1725 | /** |
| 1726 | * @brief verify bfd packet - authentication |
| 1727 | * |
| 1728 | * @param pkt |
| 1729 | * |
| 1730 | * @return 1 if bfd packet is valid |
| 1731 | */ |
| 1732 | int |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 1733 | bfd_verify_pkt_auth (vlib_main_t * vm, const bfd_pkt_t * pkt, u16 pkt_size, |
| 1734 | bfd_session_t * bs) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1735 | { |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 1736 | if (bfd_pkt_get_auth_present (pkt)) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1737 | { |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 1738 | /* authentication present in packet */ |
| 1739 | if (!bs->auth.curr_key) |
| 1740 | { |
| 1741 | /* currently not using authentication - can we turn it on? */ |
| 1742 | if (bs->auth.is_delayed && bs->auth.next_key) |
| 1743 | { |
| 1744 | /* yes, switch is scheduled - make sure the auth is valid */ |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 1745 | if (bfd_verify_pkt_auth_key (vm, pkt, pkt_size, bs, |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 1746 | bs->auth.next_bfd_key_id, |
| 1747 | bs->auth.next_key)) |
| 1748 | { |
| 1749 | /* auth matches next key, do the switch, packet is valid */ |
| 1750 | bfd_session_switch_auth_to_next (bs); |
| 1751 | return 1; |
| 1752 | } |
| 1753 | } |
| 1754 | } |
| 1755 | else |
| 1756 | { |
| 1757 | /* yes, using authentication, verify the key */ |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 1758 | if (bfd_verify_pkt_auth_key (vm, pkt, pkt_size, bs, |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 1759 | bs->auth.curr_bfd_key_id, |
| 1760 | bs->auth.curr_key)) |
| 1761 | { |
| 1762 | /* verification passed, packet is valid */ |
| 1763 | return 1; |
| 1764 | } |
| 1765 | else |
| 1766 | { |
| 1767 | /* verification failed - but maybe we need to switch key */ |
| 1768 | if (bs->auth.is_delayed && bs->auth.next_key) |
| 1769 | { |
| 1770 | /* delayed switch present, verify if that key works */ |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 1771 | if (bfd_verify_pkt_auth_key (vm, pkt, pkt_size, bs, |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 1772 | bs->auth.next_bfd_key_id, |
| 1773 | bs->auth.next_key)) |
| 1774 | { |
| 1775 | /* auth matches next key, switch key, packet is valid */ |
| 1776 | bfd_session_switch_auth_to_next (bs); |
| 1777 | return 1; |
| 1778 | } |
| 1779 | } |
| 1780 | } |
| 1781 | } |
| 1782 | } |
| 1783 | else |
| 1784 | { |
| 1785 | /* authentication in packet not present */ |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1786 | if (pkt_size > sizeof (*pkt)) |
| 1787 | { |
| 1788 | BFD_ERR ("BFD verification failed - unexpected packet size '%d' " |
| 1789 | "(auth not present)", pkt_size); |
| 1790 | return 0; |
| 1791 | } |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 1792 | if (bs->auth.curr_key) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1793 | { |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 1794 | /* currently authenticating - could we turn it off? */ |
| 1795 | if (bs->auth.is_delayed && !bs->auth.next_key) |
| 1796 | { |
| 1797 | /* yes, delayed switch to NULL key is scheduled */ |
| 1798 | bfd_session_switch_auth_to_next (bs); |
| 1799 | return 1; |
| 1800 | } |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1801 | } |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 1802 | else |
| 1803 | { |
| 1804 | /* no auth in packet, no auth in use - packet is valid */ |
| 1805 | return 1; |
| 1806 | } |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1807 | } |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 1808 | return 0; |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1809 | } |
| 1810 | |
| 1811 | void |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 1812 | bfd_consume_pkt (vlib_main_t * vm, bfd_main_t * bm, const bfd_pkt_t * pkt, |
| 1813 | u32 bs_idx) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1814 | { |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame] | 1815 | bfd_lock_check (bm); |
| 1816 | |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1817 | bfd_session_t *bs = bfd_find_session_by_idx (bm, bs_idx); |
Klement Sekera | 0e2e0df | 2017-03-03 08:51:08 +0100 | [diff] [blame] | 1818 | if (!bs || (pkt->your_disc && pkt->your_disc != bs->local_discr)) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1819 | { |
| 1820 | return; |
| 1821 | } |
| 1822 | BFD_DBG ("Scanning bfd packet, bs_idx=%d", bs->bs_idx); |
| 1823 | bs->remote_discr = pkt->my_disc; |
| 1824 | bs->remote_state = bfd_pkt_get_state (pkt); |
| 1825 | bs->remote_demand = bfd_pkt_get_demand (pkt); |
Klement Sekera | 7388448 | 2017-02-23 09:26:30 +0100 | [diff] [blame] | 1826 | bs->remote_diag = bfd_pkt_get_diag_code (pkt); |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 1827 | u64 now = bfd_time_now_nsec (vm, NULL); |
| 1828 | bs->last_rx_nsec = now; |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 1829 | if (bfd_pkt_get_auth_present (pkt)) |
| 1830 | { |
| 1831 | bfd_auth_type_e auth_type = |
| 1832 | ((bfd_pkt_with_common_auth_t *) (pkt))->common_auth.type; |
| 1833 | switch (auth_type) |
| 1834 | { |
| 1835 | case BFD_AUTH_TYPE_reserved: |
| 1836 | /* fallthrough */ |
| 1837 | case BFD_AUTH_TYPE_simple_password: |
| 1838 | /* fallthrough */ |
| 1839 | case BFD_AUTH_TYPE_keyed_md5: |
| 1840 | /* fallthrough */ |
| 1841 | case BFD_AUTH_TYPE_meticulous_keyed_md5: |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 1842 | vlib_log_crit (bm->log_class, |
| 1843 | "internal error, unexpected auth_type=%d:%s", |
| 1844 | auth_type, bfd_auth_type_str (auth_type)); |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 1845 | break; |
| 1846 | case BFD_AUTH_TYPE_keyed_sha1: |
| 1847 | /* fallthrough */ |
| 1848 | case BFD_AUTH_TYPE_meticulous_keyed_sha1: |
| 1849 | do |
| 1850 | { |
| 1851 | bfd_pkt_with_sha1_auth_t *with_sha1 = |
| 1852 | (bfd_pkt_with_sha1_auth_t *) pkt; |
| 1853 | bs->auth.remote_seq_number = |
| 1854 | clib_net_to_host_u32 (with_sha1->sha1_auth.seq_num); |
| 1855 | bs->auth.remote_seq_number_known = 1; |
| 1856 | BFD_DBG ("Received sequence number %u", |
| 1857 | bs->auth.remote_seq_number); |
| 1858 | } |
| 1859 | while (0); |
| 1860 | } |
| 1861 | } |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 1862 | bs->remote_desired_min_tx_nsec = |
| 1863 | bfd_usec_to_nsec (clib_net_to_host_u32 (pkt->des_min_tx)); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1864 | bs->remote_detect_mult = pkt->head.detect_mult; |
Klement Sekera | 637b9c4 | 2016-12-08 05:19:14 +0100 | [diff] [blame] | 1865 | bfd_set_remote_required_min_rx (bm, bs, now, |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 1866 | clib_net_to_host_u32 (pkt->req_min_rx)); |
| 1867 | bfd_set_remote_required_min_echo_rx (bm, bs, now, |
| 1868 | clib_net_to_host_u32 |
| 1869 | (pkt->req_min_echo_rx)); |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 1870 | if (bfd_pkt_get_final (pkt)) |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 1871 | { |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 1872 | if (BFD_POLL_IN_PROGRESS == bs->poll_state) |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 1873 | { |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 1874 | BFD_DBG ("Poll sequence terminated, bs_idx=%u", bs->bs_idx); |
| 1875 | bfd_set_poll_state (bs, BFD_POLL_NOT_NEEDED); |
| 1876 | if (BFD_STATE_up == bs->local_state) |
| 1877 | { |
Klement Sekera | 7388448 | 2017-02-23 09:26:30 +0100 | [diff] [blame] | 1878 | bfd_set_effective_required_min_rx (bm, bs, |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 1879 | clib_max (bs->echo * |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 1880 | bm->min_required_min_rx_while_echo_nsec, |
| 1881 | bs->config_required_min_rx_nsec)); |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 1882 | } |
| 1883 | } |
| 1884 | else if (BFD_POLL_IN_PROGRESS_AND_QUEUED == bs->poll_state) |
| 1885 | { |
| 1886 | /* |
| 1887 | * next poll sequence must be delayed by at least the round trip |
| 1888 | * time, so calculate that here |
| 1889 | */ |
| 1890 | BFD_DBG ("Next poll sequence can commence in " BFD_CLK_FMT, |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 1891 | BFD_CLK_PRN (now - bs->poll_state_start_or_timeout_nsec)); |
| 1892 | bs->poll_state_start_or_timeout_nsec = |
| 1893 | now + (now - bs->poll_state_start_or_timeout_nsec); |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 1894 | BFD_DBG |
| 1895 | ("Poll sequence terminated, but another is needed, bs_idx=%u", |
| 1896 | bs->bs_idx); |
| 1897 | bfd_set_poll_state (bs, BFD_POLL_NEEDED); |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 1898 | } |
| 1899 | } |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 1900 | bfd_calc_next_tx (bm, bs, now); |
| 1901 | bfd_set_timer (bm, bs, now, 0); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1902 | if (BFD_STATE_admin_down == bs->local_state) |
Klement Sekera | c48829b | 2017-02-14 07:55:57 +0100 | [diff] [blame] | 1903 | { |
| 1904 | BFD_DBG ("Session is admin-down, ignoring packet, bs_idx=%u", |
| 1905 | bs->bs_idx); |
| 1906 | return; |
| 1907 | } |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1908 | if (BFD_STATE_admin_down == bs->remote_state) |
| 1909 | { |
| 1910 | bfd_set_diag (bs, BFD_DIAG_CODE_neighbor_sig_down); |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 1911 | bfd_set_state (vm, bm, bs, BFD_STATE_down, 0); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1912 | } |
| 1913 | else if (BFD_STATE_down == bs->local_state) |
| 1914 | { |
| 1915 | if (BFD_STATE_down == bs->remote_state) |
| 1916 | { |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 1917 | bfd_set_diag (bs, BFD_DIAG_CODE_no_diag); |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 1918 | bfd_set_state (vm, bm, bs, BFD_STATE_init, 0); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1919 | } |
| 1920 | else if (BFD_STATE_init == bs->remote_state) |
| 1921 | { |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 1922 | bfd_set_diag (bs, BFD_DIAG_CODE_no_diag); |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 1923 | bfd_set_state (vm, bm, bs, BFD_STATE_up, 0); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1924 | } |
| 1925 | } |
| 1926 | else if (BFD_STATE_init == bs->local_state) |
| 1927 | { |
| 1928 | if (BFD_STATE_up == bs->remote_state || |
| 1929 | BFD_STATE_init == bs->remote_state) |
| 1930 | { |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 1931 | bfd_set_diag (bs, BFD_DIAG_CODE_no_diag); |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 1932 | bfd_set_state (vm, bm, bs, BFD_STATE_up, 0); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1933 | } |
| 1934 | } |
| 1935 | else /* BFD_STATE_up == bs->local_state */ |
| 1936 | { |
| 1937 | if (BFD_STATE_down == bs->remote_state) |
| 1938 | { |
| 1939 | bfd_set_diag (bs, BFD_DIAG_CODE_neighbor_sig_down); |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 1940 | bfd_set_state (vm, bm, bs, BFD_STATE_down, 0); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1941 | } |
| 1942 | } |
| 1943 | } |
| 1944 | |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 1945 | int |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 1946 | bfd_consume_echo_pkt (vlib_main_t * vm, bfd_main_t * bm, vlib_buffer_t * b) |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 1947 | { |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 1948 | bfd_echo_pkt_t *pkt = NULL; |
| 1949 | if (b->current_length != sizeof (*pkt)) |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 1950 | { |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 1951 | return 0; |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 1952 | } |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 1953 | pkt = vlib_buffer_get_current (b); |
| 1954 | bfd_session_t *bs = bfd_find_session_by_disc (bm, pkt->discriminator); |
| 1955 | if (!bs) |
| 1956 | { |
| 1957 | return 0; |
| 1958 | } |
| 1959 | BFD_DBG ("Scanning bfd echo packet, bs_idx=%d", bs->bs_idx); |
| 1960 | u64 checksum = |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 1961 | bfd_calc_echo_checksum (bs->local_discr, pkt->expire_time_nsec, |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 1962 | bs->echo_secret); |
| 1963 | if (checksum != pkt->checksum) |
| 1964 | { |
| 1965 | BFD_DBG ("Invalid echo packet, checksum mismatch"); |
| 1966 | return 1; |
| 1967 | } |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 1968 | u64 now = bfd_time_now_nsec (vm, NULL); |
| 1969 | if (pkt->expire_time_nsec < now) |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 1970 | { |
| 1971 | BFD_DBG ("Stale packet received, expire time %lu < now %lu", |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 1972 | pkt->expire_time_nsec, now); |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 1973 | } |
| 1974 | else |
| 1975 | { |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 1976 | bs->echo_last_rx_nsec = now; |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 1977 | } |
| 1978 | return 1; |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 1979 | } |
| 1980 | |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1981 | u8 * |
| 1982 | format_bfd_session (u8 * s, va_list * args) |
| 1983 | { |
| 1984 | const bfd_session_t *bs = va_arg (*args, bfd_session_t *); |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 1985 | u32 indent = format_get_indent (s) + vlib_log_get_indent (); |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 1986 | s = format (s, "bs_idx=%u local-state=%s remote-state=%s\n" |
| 1987 | "%Ulocal-discriminator=%u remote-discriminator=%u\n" |
| 1988 | "%Ulocal-diag=%s echo-active=%s\n" |
| 1989 | "%Udesired-min-tx=%u required-min-rx=%u\n" |
| 1990 | "%Urequired-min-echo-rx=%u detect-mult=%u\n" |
| 1991 | "%Uremote-min-rx=%u remote-min-echo-rx=%u\n" |
| 1992 | "%Uremote-demand=%s poll-state=%s\n" |
| 1993 | "%Uauth: local-seq-num=%u remote-seq-num=%u\n" |
| 1994 | "%U is-delayed=%s\n" |
| 1995 | "%U curr-key=%U\n" |
| 1996 | "%U next-key=%U", |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 1997 | bs->bs_idx, bfd_state_string (bs->local_state), |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 1998 | bfd_state_string (bs->remote_state), format_white_space, indent, |
| 1999 | bs->local_discr, bs->remote_discr, format_white_space, indent, |
| 2000 | bfd_diag_code_string (bs->local_diag), |
| 2001 | (bs->echo ? "yes" : "no"), format_white_space, indent, |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 2002 | bs->config_desired_min_tx_usec, bs->config_required_min_rx_usec, |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 2003 | format_white_space, indent, 1, bs->local_detect_mult, |
| 2004 | format_white_space, indent, bs->remote_min_rx_usec, |
| 2005 | bs->remote_min_echo_rx_usec, format_white_space, indent, |
| 2006 | (bs->remote_demand ? "yes" : "no"), |
| 2007 | bfd_poll_state_string (bs->poll_state), format_white_space, |
| 2008 | indent, bs->auth.local_seq_number, bs->auth.remote_seq_number, |
| 2009 | format_white_space, indent, |
| 2010 | (bs->auth.is_delayed ? "yes" : "no"), format_white_space, |
| 2011 | indent, format_bfd_auth_key, bs->auth.curr_key, |
| 2012 | format_white_space, indent, format_bfd_auth_key, |
| 2013 | bs->auth.next_key); |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 2014 | return s; |
| 2015 | } |
| 2016 | |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 2017 | u8 * |
| 2018 | format_bfd_session_brief (u8 * s, va_list * args) |
| 2019 | { |
| 2020 | const bfd_session_t *bs = va_arg (*args, bfd_session_t *); |
| 2021 | s = |
| 2022 | format (s, "bs_idx=%u local-state=%s remote-state=%s", bs->bs_idx, |
| 2023 | bfd_state_string (bs->local_state), |
| 2024 | bfd_state_string (bs->remote_state)); |
| 2025 | return s; |
| 2026 | } |
| 2027 | |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 2028 | unsigned |
| 2029 | bfd_auth_type_supported (bfd_auth_type_e auth_type) |
| 2030 | { |
| 2031 | if (auth_type == BFD_AUTH_TYPE_keyed_sha1 || |
| 2032 | auth_type == BFD_AUTH_TYPE_meticulous_keyed_sha1) |
| 2033 | { |
| 2034 | return 1; |
| 2035 | } |
| 2036 | return 0; |
| 2037 | } |
| 2038 | |
| 2039 | vnet_api_error_t |
| 2040 | bfd_auth_activate (bfd_session_t * bs, u32 conf_key_id, |
| 2041 | u8 bfd_key_id, u8 is_delayed) |
| 2042 | { |
| 2043 | bfd_main_t *bm = &bfd_main; |
| 2044 | const uword *key_idx_p = |
| 2045 | hash_get (bm->auth_key_by_conf_key_id, conf_key_id); |
| 2046 | if (!key_idx_p) |
| 2047 | { |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 2048 | vlib_log_err (bm->log_class, |
| 2049 | "authentication key with config ID %u doesn't exist)", |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 2050 | conf_key_id); |
| 2051 | return VNET_API_ERROR_BFD_ENOENT; |
| 2052 | } |
| 2053 | const uword key_idx = *key_idx_p; |
| 2054 | bfd_auth_key_t *key = pool_elt_at_index (bm->auth_keys, key_idx); |
| 2055 | if (is_delayed) |
| 2056 | { |
| 2057 | if (bs->auth.next_key == key) |
| 2058 | { |
| 2059 | /* already using this key, no changes required */ |
| 2060 | return 0; |
| 2061 | } |
| 2062 | bs->auth.next_key = key; |
| 2063 | bs->auth.next_bfd_key_id = bfd_key_id; |
| 2064 | bs->auth.is_delayed = 1; |
| 2065 | } |
| 2066 | else |
| 2067 | { |
| 2068 | if (bs->auth.curr_key == key) |
| 2069 | { |
| 2070 | /* already using this key, no changes required */ |
| 2071 | return 0; |
| 2072 | } |
| 2073 | if (bs->auth.curr_key) |
| 2074 | { |
| 2075 | --bs->auth.curr_key->use_count; |
| 2076 | } |
| 2077 | bs->auth.curr_key = key; |
| 2078 | bs->auth.curr_bfd_key_id = bfd_key_id; |
| 2079 | bs->auth.is_delayed = 0; |
| 2080 | } |
| 2081 | ++key->use_count; |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 2082 | BFD_DBG ("\nSession auth modified: %U", format_bfd_session, bs); |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 2083 | vlib_log_info (bm->log_class, "session auth modified: %U", |
| 2084 | format_bfd_session_brief, bs); |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 2085 | return 0; |
| 2086 | } |
| 2087 | |
| 2088 | vnet_api_error_t |
| 2089 | bfd_auth_deactivate (bfd_session_t * bs, u8 is_delayed) |
| 2090 | { |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 2091 | bfd_main_t *bm = &bfd_main; |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 2092 | #if WITH_LIBSSL > 0 |
| 2093 | if (!is_delayed) |
| 2094 | { |
| 2095 | /* not delayed - deactivate the current key right now */ |
| 2096 | if (bs->auth.curr_key) |
| 2097 | { |
| 2098 | --bs->auth.curr_key->use_count; |
| 2099 | bs->auth.curr_key = NULL; |
| 2100 | } |
| 2101 | bs->auth.is_delayed = 0; |
| 2102 | } |
| 2103 | else |
| 2104 | { |
| 2105 | /* delayed - mark as so */ |
| 2106 | bs->auth.is_delayed = 1; |
| 2107 | } |
| 2108 | /* |
| 2109 | * clear the next key unconditionally - either the auth change is not delayed |
| 2110 | * in which case the caller expects the session to not use authentication |
| 2111 | * from this point forward, or it is delayed, in which case the next_key |
| 2112 | * needs to be set to NULL to make it so in the future |
| 2113 | */ |
| 2114 | if (bs->auth.next_key) |
| 2115 | { |
| 2116 | --bs->auth.next_key->use_count; |
| 2117 | bs->auth.next_key = NULL; |
| 2118 | } |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 2119 | BFD_DBG ("\nSession auth modified: %U", format_bfd_session, bs); |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 2120 | vlib_log_info (bm->log_class, "session auth modified: %U", |
| 2121 | format_bfd_session_brief, bs); |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 2122 | return 0; |
| 2123 | #else |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 2124 | vlib_log_err (bm->log_class, |
| 2125 | "SSL missing, cannot deactivate BFD authentication"); |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 2126 | return VNET_API_ERROR_BFD_NOTSUPP; |
| 2127 | #endif |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 2128 | } |
| 2129 | |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 2130 | vnet_api_error_t |
| 2131 | bfd_session_set_params (bfd_main_t * bm, bfd_session_t * bs, |
| 2132 | u32 desired_min_tx_usec, |
| 2133 | u32 required_min_rx_usec, u8 detect_mult) |
| 2134 | { |
| 2135 | if (bs->local_detect_mult != detect_mult || |
| 2136 | bs->config_desired_min_tx_usec != desired_min_tx_usec || |
| 2137 | bs->config_required_min_rx_usec != required_min_rx_usec) |
| 2138 | { |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 2139 | BFD_DBG ("\nChanging session params: %U", format_bfd_session, bs); |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 2140 | switch (bs->poll_state) |
| 2141 | { |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 2142 | case BFD_POLL_NOT_NEEDED: |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 2143 | if (BFD_STATE_up == bs->local_state || |
| 2144 | BFD_STATE_init == bs->local_state) |
| 2145 | { |
| 2146 | /* poll sequence is not needed for detect multiplier change */ |
| 2147 | if (bs->config_desired_min_tx_usec != desired_min_tx_usec || |
| 2148 | bs->config_required_min_rx_usec != required_min_rx_usec) |
| 2149 | { |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 2150 | bfd_set_poll_state (bs, BFD_POLL_NEEDED); |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 2151 | } |
| 2152 | } |
| 2153 | break; |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 2154 | case BFD_POLL_NEEDED: |
| 2155 | case BFD_POLL_IN_PROGRESS_AND_QUEUED: |
| 2156 | /* |
| 2157 | * nothing to do - will be handled in the future poll which is |
| 2158 | * already scheduled for execution |
| 2159 | */ |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 2160 | break; |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 2161 | case BFD_POLL_IN_PROGRESS: |
| 2162 | /* poll sequence is not needed for detect multiplier change */ |
| 2163 | if (bs->config_desired_min_tx_usec != desired_min_tx_usec || |
| 2164 | bs->config_required_min_rx_usec != required_min_rx_usec) |
| 2165 | { |
| 2166 | BFD_DBG ("Poll in progress, queueing extra poll, bs_idx=%u", |
| 2167 | bs->bs_idx); |
| 2168 | bfd_set_poll_state (bs, BFD_POLL_IN_PROGRESS_AND_QUEUED); |
| 2169 | } |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 2170 | } |
| 2171 | |
| 2172 | bs->local_detect_mult = detect_mult; |
| 2173 | bs->config_desired_min_tx_usec = desired_min_tx_usec; |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 2174 | bs->config_desired_min_tx_nsec = bfd_usec_to_nsec (desired_min_tx_usec); |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 2175 | bs->config_required_min_rx_usec = required_min_rx_usec; |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 2176 | bs->config_required_min_rx_nsec = |
| 2177 | bfd_usec_to_nsec (required_min_rx_usec); |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 2178 | BFD_DBG ("\nChanged session params: %U", format_bfd_session, bs); |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 2179 | |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 2180 | vlib_log_info (bm->log_class, "changed session params: %U", |
| 2181 | format_bfd_session_brief, bs); |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 2182 | vlib_process_signal_event (bm->vlib_main, bm->bfd_process_node_index, |
| 2183 | BFD_EVENT_CONFIG_CHANGED, bs->bs_idx); |
| 2184 | } |
| 2185 | else |
| 2186 | { |
| 2187 | BFD_DBG ("Ignore parameter change - no change, bs_idx=%u", bs->bs_idx); |
| 2188 | } |
| 2189 | return 0; |
| 2190 | } |
| 2191 | |
Klement Sekera | 7388448 | 2017-02-23 09:26:30 +0100 | [diff] [blame] | 2192 | vnet_api_error_t |
| 2193 | bfd_auth_set_key (u32 conf_key_id, u8 auth_type, u8 key_len, |
| 2194 | const u8 * key_data) |
| 2195 | { |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 2196 | bfd_main_t *bm = &bfd_main; |
Klement Sekera | 7388448 | 2017-02-23 09:26:30 +0100 | [diff] [blame] | 2197 | #if WITH_LIBSSL > 0 |
| 2198 | bfd_auth_key_t *auth_key = NULL; |
Klement Sekera | b16bfe3 | 2017-02-28 11:56:48 +0100 | [diff] [blame] | 2199 | 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] | 2200 | { |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 2201 | vlib_log_err (bm->log_class, |
| 2202 | "invalid authentication key length for auth_type=%d:%s " |
| 2203 | "(key_len=%u, must be non-zero, expected max=%u)", |
Klement Sekera | 7388448 | 2017-02-23 09:26:30 +0100 | [diff] [blame] | 2204 | auth_type, bfd_auth_type_str (auth_type), key_len, |
Klement Sekera | b16bfe3 | 2017-02-28 11:56:48 +0100 | [diff] [blame] | 2205 | (u32) bfd_max_key_len_for_auth_type (auth_type)); |
Klement Sekera | 7388448 | 2017-02-23 09:26:30 +0100 | [diff] [blame] | 2206 | return VNET_API_ERROR_INVALID_VALUE; |
| 2207 | } |
| 2208 | if (!bfd_auth_type_supported (auth_type)) |
| 2209 | { |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 2210 | 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] | 2211 | bfd_auth_type_str (auth_type)); |
| 2212 | return VNET_API_ERROR_BFD_NOTSUPP; |
| 2213 | } |
Klement Sekera | 7388448 | 2017-02-23 09:26:30 +0100 | [diff] [blame] | 2214 | uword *key_idx_p = hash_get (bm->auth_key_by_conf_key_id, conf_key_id); |
| 2215 | if (key_idx_p) |
| 2216 | { |
| 2217 | /* modifying existing key - must not be used */ |
| 2218 | const uword key_idx = *key_idx_p; |
| 2219 | auth_key = pool_elt_at_index (bm->auth_keys, key_idx); |
| 2220 | if (auth_key->use_count > 0) |
| 2221 | { |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 2222 | vlib_log_err (bm->log_class, |
| 2223 | "authentication key with conf ID %u in use by %u BFD " |
| 2224 | "session(s) - cannot modify", conf_key_id, |
| 2225 | auth_key->use_count); |
Klement Sekera | 7388448 | 2017-02-23 09:26:30 +0100 | [diff] [blame] | 2226 | return VNET_API_ERROR_BFD_EINUSE; |
| 2227 | } |
| 2228 | } |
| 2229 | else |
| 2230 | { |
| 2231 | /* adding new key */ |
| 2232 | pool_get (bm->auth_keys, auth_key); |
| 2233 | auth_key->conf_key_id = conf_key_id; |
| 2234 | hash_set (bm->auth_key_by_conf_key_id, conf_key_id, |
| 2235 | auth_key - bm->auth_keys); |
| 2236 | } |
| 2237 | auth_key->auth_type = auth_type; |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 2238 | clib_memset (auth_key->key, 0, sizeof (auth_key->key)); |
Klement Sekera | 7388448 | 2017-02-23 09:26:30 +0100 | [diff] [blame] | 2239 | clib_memcpy (auth_key->key, key_data, key_len); |
| 2240 | return 0; |
| 2241 | #else |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 2242 | vlib_log_err (bm->log_class, |
| 2243 | "SSL missing, cannot manipulate authentication keys"); |
Klement Sekera | 7388448 | 2017-02-23 09:26:30 +0100 | [diff] [blame] | 2244 | return VNET_API_ERROR_BFD_NOTSUPP; |
| 2245 | #endif |
| 2246 | } |
| 2247 | |
| 2248 | vnet_api_error_t |
| 2249 | bfd_auth_del_key (u32 conf_key_id) |
| 2250 | { |
| 2251 | #if WITH_LIBSSL > 0 |
| 2252 | bfd_auth_key_t *auth_key = NULL; |
| 2253 | bfd_main_t *bm = &bfd_main; |
| 2254 | uword *key_idx_p = hash_get (bm->auth_key_by_conf_key_id, conf_key_id); |
| 2255 | if (key_idx_p) |
| 2256 | { |
| 2257 | /* deleting existing key - must not be used */ |
| 2258 | const uword key_idx = *key_idx_p; |
| 2259 | auth_key = pool_elt_at_index (bm->auth_keys, key_idx); |
| 2260 | if (auth_key->use_count > 0) |
| 2261 | { |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 2262 | vlib_log_err (bm->log_class, |
| 2263 | "authentication key with conf ID %u in use by %u BFD " |
| 2264 | "session(s) - cannot delete", conf_key_id, |
| 2265 | auth_key->use_count); |
Klement Sekera | 7388448 | 2017-02-23 09:26:30 +0100 | [diff] [blame] | 2266 | return VNET_API_ERROR_BFD_EINUSE; |
| 2267 | } |
| 2268 | hash_unset (bm->auth_key_by_conf_key_id, conf_key_id); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 2269 | clib_memset (auth_key, 0, sizeof (*auth_key)); |
Klement Sekera | 7388448 | 2017-02-23 09:26:30 +0100 | [diff] [blame] | 2270 | pool_put (bm->auth_keys, auth_key); |
| 2271 | } |
| 2272 | else |
| 2273 | { |
| 2274 | /* no such key */ |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 2275 | vlib_log_err (bm->log_class, |
| 2276 | "authentication key with conf ID %u does not exist", |
Klement Sekera | 7388448 | 2017-02-23 09:26:30 +0100 | [diff] [blame] | 2277 | conf_key_id); |
| 2278 | return VNET_API_ERROR_BFD_ENOENT; |
| 2279 | } |
| 2280 | return 0; |
| 2281 | #else |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 2282 | vlib_log_err (bm->log_class, |
| 2283 | "SSL missing, cannot manipulate authentication keys"); |
Klement Sekera | 7388448 | 2017-02-23 09:26:30 +0100 | [diff] [blame] | 2284 | return VNET_API_ERROR_BFD_NOTSUPP; |
| 2285 | #endif |
| 2286 | } |
| 2287 | |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 2288 | bfd_main_t bfd_main; |
| 2289 | |
| 2290 | /* |
| 2291 | * fd.io coding-style-patch-verification: ON |
| 2292 | * |
| 2293 | * Local Variables: |
| 2294 | * eval: (c-set-style "gnu") |
| 2295 | * End: |
| 2296 | */ |