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