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