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