Klement Sekera | 2bce033 | 2017-02-09 06:03:46 +0100 | [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 | */ |
Klement Sekera | b16bfe3 | 2017-02-28 11:56:48 +0100 | [diff] [blame] | 15 | /** |
| 16 | * @file |
| 17 | * @brief BFD UDP transport layer implementation |
| 18 | */ |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 19 | #include <vppinfra/types.h> |
| 20 | #include <vlibmemory/api.h> |
| 21 | #include <vlib/vlib.h> |
| 22 | #include <vlib/buffer.h> |
| 23 | #include <vnet/ip/format.h> |
| 24 | #include <vnet/ethernet/packet.h> |
Florin Coras | b040f98 | 2020-10-20 14:59:43 -0700 | [diff] [blame] | 25 | #include <vnet/udp/udp_local.h> |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 26 | #include <vnet/udp/udp_packet.h> |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 27 | #include <vnet/ip/lookup.h> |
| 28 | #include <vnet/ip/icmp46_packet.h> |
| 29 | #include <vnet/ip/ip4.h> |
| 30 | #include <vnet/ip/ip6.h> |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 31 | #include <vnet/ip/ip6_packet.h> |
Neale Ranns | cbe25aa | 2019-09-30 10:53:31 +0000 | [diff] [blame] | 32 | #include <vnet/ip/ip6_link.h> |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 33 | #include <vnet/adj/adj.h> |
| 34 | #include <vnet/adj/adj_nbr.h> |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 35 | #include <vnet/dpo/receive_dpo.h> |
| 36 | #include <vnet/fib/fib_entry.h> |
| 37 | #include <vnet/fib/fib_table.h> |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 38 | #include <vlib/stats/stats.h> |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 39 | #include <vnet/bfd/bfd_debug.h> |
| 40 | #include <vnet/bfd/bfd_udp.h> |
| 41 | #include <vnet/bfd/bfd_main.h> |
| 42 | #include <vnet/bfd/bfd_api.h> |
Neale Ranns | 5c7e579 | 2022-08-09 00:03:17 +0000 | [diff] [blame] | 43 | #include <vnet/bfd/bfd.api_enum.h> |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 44 | |
| 45 | typedef struct |
| 46 | { |
| 47 | bfd_main_t *bfd_main; |
| 48 | /* hashmap - bfd session index by bfd key - used for CLI/API lookup, where |
| 49 | * discriminator is unknown */ |
| 50 | mhash_t bfd_session_idx_by_bfd_key; |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 51 | /* convenience variable */ |
| 52 | vnet_main_t *vnet_main; |
| 53 | /* flag indicating whether echo_source_sw_if_index holds a valid value */ |
| 54 | int echo_source_is_set; |
| 55 | /* loopback interface used to get echo source ip */ |
| 56 | u32 echo_source_sw_if_index; |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 57 | /* node index of "ip4-arp" node */ |
| 58 | u32 ip4_arp_idx; |
| 59 | /* node index of "ip6-discover-neighbor" node */ |
| 60 | u32 ip6_ndp_idx; |
| 61 | /* node index of "ip4-rewrite" node */ |
| 62 | u32 ip4_rewrite_idx; |
| 63 | /* node index of "ip6-rewrite" node */ |
| 64 | u32 ip6_rewrite_idx; |
Matthew G Smith | 2f26014 | 2019-05-22 14:49:24 -0500 | [diff] [blame] | 65 | /* node index of "ip4-midchain" node */ |
| 66 | u32 ip4_midchain_idx; |
| 67 | /* node index of "ip6-midchain" node */ |
| 68 | u32 ip6_midchain_idx; |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 69 | /* log class */ |
| 70 | vlib_log_class_t log_class; |
Klement Sekera | 88aec65 | 2019-03-11 14:04:19 +0100 | [diff] [blame] | 71 | /* number of active udp4 sessions */ |
| 72 | u32 udp4_sessions_count; |
Klement Sekera | cdaf0d8 | 2022-02-14 20:20:22 +0000 | [diff] [blame] | 73 | u32 udp4_sessions_count_stat_seg_entry; |
Klement Sekera | 88aec65 | 2019-03-11 14:04:19 +0100 | [diff] [blame] | 74 | /* number of active udp6 sessions */ |
| 75 | u32 udp6_sessions_count; |
Klement Sekera | cdaf0d8 | 2022-02-14 20:20:22 +0000 | [diff] [blame] | 76 | u32 udp6_sessions_count_stat_seg_entry; |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 77 | } bfd_udp_main_t; |
| 78 | |
Klement Sekera | e4504c6 | 2016-12-08 10:16:41 +0100 | [diff] [blame] | 79 | static vlib_node_registration_t bfd_udp4_input_node; |
| 80 | static vlib_node_registration_t bfd_udp6_input_node; |
Klement Sekera | aeeac3b | 2017-02-14 07:11:52 +0100 | [diff] [blame] | 81 | static vlib_node_registration_t bfd_udp_echo4_input_node; |
| 82 | static vlib_node_registration_t bfd_udp_echo6_input_node; |
Klement Sekera | e4504c6 | 2016-12-08 10:16:41 +0100 | [diff] [blame] | 83 | |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 84 | bfd_udp_main_t bfd_udp_main; |
| 85 | |
Klement Sekera | cdaf0d8 | 2022-02-14 20:20:22 +0000 | [diff] [blame] | 86 | void |
| 87 | bfd_udp_update_stat_segment_entry (u32 entry, u64 value) |
| 88 | { |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 89 | vlib_stats_segment_lock (); |
| 90 | vlib_stats_set_gauge (entry, value); |
| 91 | vlib_stats_segment_unlock (); |
Klement Sekera | cdaf0d8 | 2022-02-14 20:20:22 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 94 | vnet_api_error_t |
| 95 | bfd_udp_set_echo_source (u32 sw_if_index) |
| 96 | { |
| 97 | vnet_sw_interface_t *sw_if = |
Dave Barach | 3940de3 | 2019-07-23 16:28:36 -0400 | [diff] [blame] | 98 | vnet_get_sw_interface_or_null (bfd_udp_main.vnet_main, sw_if_index); |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 99 | if (sw_if) |
| 100 | { |
| 101 | bfd_udp_main.echo_source_sw_if_index = sw_if_index; |
| 102 | bfd_udp_main.echo_source_is_set = 1; |
| 103 | return 0; |
| 104 | } |
| 105 | return VNET_API_ERROR_BFD_ENOENT; |
| 106 | } |
| 107 | |
| 108 | vnet_api_error_t |
Klement Sekera | cdaf0d8 | 2022-02-14 20:20:22 +0000 | [diff] [blame] | 109 | bfd_udp_del_echo_source () |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 110 | { |
| 111 | bfd_udp_main.echo_source_sw_if_index = ~0; |
| 112 | bfd_udp_main.echo_source_is_set = 0; |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | int |
| 117 | bfd_udp_is_echo_available (bfd_transport_e transport) |
| 118 | { |
| 119 | if (!bfd_udp_main.echo_source_is_set) |
| 120 | { |
Klement Sekera | 7388448 | 2017-02-23 09:26:30 +0100 | [diff] [blame] | 121 | BFD_DBG ("UDP echo source not set - echo not available"); |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 122 | return 0; |
| 123 | } |
| 124 | /* |
| 125 | * for the echo to work, we need a loopback interface with at least one |
| 126 | * address with netmask length at most 31 (ip4) or 127 (ip6) so that we can |
| 127 | * pick an unused address from that subnet |
| 128 | */ |
| 129 | vnet_sw_interface_t *sw_if = |
Dave Barach | 3940de3 | 2019-07-23 16:28:36 -0400 | [diff] [blame] | 130 | vnet_get_sw_interface_or_null (bfd_udp_main.vnet_main, |
| 131 | bfd_udp_main.echo_source_sw_if_index); |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 132 | if (sw_if && sw_if->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) |
| 133 | { |
| 134 | if (BFD_TRANSPORT_UDP4 == transport) |
| 135 | { |
| 136 | ip4_main_t *im = &ip4_main; |
| 137 | ip_interface_address_t *ia = NULL; |
| 138 | /* *INDENT-OFF* */ |
| 139 | foreach_ip_interface_address (&im->lookup_main, ia, |
| 140 | bfd_udp_main.echo_source_sw_if_index, |
| 141 | 0 /* honor unnumbered */, ({ |
| 142 | if (ia->address_length <= 31) |
| 143 | { |
| 144 | return 1; |
| 145 | } |
| 146 | })); |
| 147 | /* *INDENT-ON* */ |
| 148 | } |
| 149 | else if (BFD_TRANSPORT_UDP6 == transport) |
| 150 | { |
| 151 | ip6_main_t *im = &ip6_main; |
| 152 | ip_interface_address_t *ia = NULL; |
| 153 | /* *INDENT-OFF* */ |
| 154 | foreach_ip_interface_address (&im->lookup_main, ia, |
| 155 | bfd_udp_main.echo_source_sw_if_index, |
| 156 | 0 /* honor unnumbered */, ({ |
| 157 | if (ia->address_length <= 127) |
| 158 | { |
| 159 | return 1; |
| 160 | } |
| 161 | })); |
| 162 | /* *INDENT-ON* */ |
| 163 | } |
| 164 | } |
Klement Sekera | 7388448 | 2017-02-23 09:26:30 +0100 | [diff] [blame] | 165 | BFD_DBG ("No usable IP address for UDP echo - echo not available"); |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 166 | return 0; |
| 167 | } |
| 168 | |
Klement Sekera | 2bce033 | 2017-02-09 06:03:46 +0100 | [diff] [blame] | 169 | static u16 |
| 170 | bfd_udp_bs_idx_to_sport (u32 bs_idx) |
| 171 | { |
| 172 | /* The source port MUST be in the range 49152 through 65535. The same UDP |
| 173 | * source port number MUST be used for all BFD Control packets associated |
| 174 | * with a particular session. The source port number SHOULD be unique among |
| 175 | * all BFD sessions on the system. If more than 16384 BFD sessions are |
| 176 | * simultaneously active, UDP source port numbers MAY be reused on |
| 177 | * multiple sessions, but the number of distinct uses of the same UDP |
| 178 | * source port number SHOULD be minimized. |
| 179 | */ |
| 180 | return 49152 + bs_idx % (65535 - 49152 + 1); |
| 181 | } |
| 182 | |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 183 | int |
| 184 | bfd_udp_get_echo_src_ip4 (ip4_address_t * addr) |
| 185 | { |
| 186 | if (!bfd_udp_main.echo_source_is_set) |
| 187 | { |
| 188 | BFD_ERR ("cannot find ip4 address, echo source not set"); |
| 189 | return 0; |
| 190 | } |
| 191 | ip_interface_address_t *ia = NULL; |
| 192 | ip4_main_t *im = &ip4_main; |
| 193 | |
| 194 | /* *INDENT-OFF* */ |
| 195 | foreach_ip_interface_address ( |
| 196 | &im->lookup_main, ia, bfd_udp_main.echo_source_sw_if_index, |
| 197 | 0 /* honor unnumbered */, ({ |
| 198 | ip4_address_t *x = |
| 199 | ip_interface_address_get_address (&im->lookup_main, ia); |
| 200 | if (ia->address_length <= 31) |
| 201 | { |
| 202 | addr->as_u32 = clib_host_to_net_u32 (x->as_u32); |
Klement Sekera | f3bcdbf | 2017-05-02 07:38:01 +0200 | [diff] [blame] | 203 | /* |
| 204 | * flip the last bit to get a different address, might be network, |
| 205 | * we don't care ... |
| 206 | */ |
| 207 | addr->as_u32 ^= 1; |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 208 | addr->as_u32 = clib_net_to_host_u32 (addr->as_u32); |
| 209 | return 1; |
| 210 | } |
| 211 | })); |
| 212 | /* *INDENT-ON* */ |
| 213 | BFD_ERR ("cannot find ip4 address, no usable address found"); |
| 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | int |
| 218 | bfd_udp_get_echo_src_ip6 (ip6_address_t * addr) |
| 219 | { |
| 220 | if (!bfd_udp_main.echo_source_is_set) |
| 221 | { |
| 222 | BFD_ERR ("cannot find ip6 address, echo source not set"); |
| 223 | return 0; |
| 224 | } |
| 225 | ip_interface_address_t *ia = NULL; |
| 226 | ip6_main_t *im = &ip6_main; |
| 227 | |
| 228 | /* *INDENT-OFF* */ |
| 229 | foreach_ip_interface_address ( |
| 230 | &im->lookup_main, ia, bfd_udp_main.echo_source_sw_if_index, |
| 231 | 0 /* honor unnumbered */, ({ |
| 232 | ip6_address_t *x = |
| 233 | ip_interface_address_get_address (&im->lookup_main, ia); |
| 234 | if (ia->address_length <= 127) |
| 235 | { |
| 236 | *addr = *x; |
| 237 | addr->as_u8[15] ^= 1; /* flip the last bit of the address */ |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 238 | return 1; |
| 239 | } |
| 240 | })); |
| 241 | /* *INDENT-ON* */ |
| 242 | BFD_ERR ("cannot find ip6 address, no usable address found"); |
| 243 | return 0; |
| 244 | } |
| 245 | |
Klement Sekera | 7388448 | 2017-02-23 09:26:30 +0100 | [diff] [blame] | 246 | void |
Klement Sekera | f3bcdbf | 2017-05-02 07:38:01 +0200 | [diff] [blame] | 247 | bfd_udp_get_echo_source (int *is_set, u32 * sw_if_index, |
| 248 | int *have_usable_ip4, ip4_address_t * ip4, |
| 249 | int *have_usable_ip6, ip6_address_t * ip6) |
Klement Sekera | 7388448 | 2017-02-23 09:26:30 +0100 | [diff] [blame] | 250 | { |
| 251 | if (bfd_udp_main.echo_source_is_set) |
| 252 | { |
| 253 | *is_set = 1; |
| 254 | *sw_if_index = bfd_udp_main.echo_source_sw_if_index; |
| 255 | *have_usable_ip4 = bfd_udp_get_echo_src_ip4 (ip4); |
| 256 | *have_usable_ip6 = bfd_udp_get_echo_src_ip6 (ip6); |
| 257 | } |
| 258 | else |
| 259 | { |
| 260 | *is_set = 0; |
| 261 | } |
| 262 | } |
| 263 | |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 264 | int |
Klement Sekera | f3bcdbf | 2017-05-02 07:38:01 +0200 | [diff] [blame] | 265 | bfd_add_udp4_transport (vlib_main_t * vm, u32 bi, const bfd_session_t * bs, |
| 266 | int is_echo) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 267 | { |
Klement Sekera | 2bce033 | 2017-02-09 06:03:46 +0100 | [diff] [blame] | 268 | const bfd_udp_session_t *bus = &bs->udp; |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame] | 269 | const bfd_udp_key_t *key = &bus->key; |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 270 | vlib_buffer_t *b = vlib_get_buffer (vm, bi); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 271 | |
Damjan Marion | 213b5aa | 2017-07-13 21:19:27 +0200 | [diff] [blame] | 272 | b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED; |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 273 | vnet_buffer (b)->ip.adj_index[VLIB_RX] = bus->adj_index; |
| 274 | vnet_buffer (b)->ip.adj_index[VLIB_TX] = bus->adj_index; |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 275 | vnet_buffer (b)->sw_if_index[VLIB_RX] = 0; |
| 276 | vnet_buffer (b)->sw_if_index[VLIB_TX] = ~0; |
Klement Sekera | 402ed31 | 2017-01-18 09:44:36 +0100 | [diff] [blame] | 277 | typedef struct |
| 278 | { |
| 279 | ip4_header_t ip4; |
| 280 | udp_header_t udp; |
| 281 | } ip4_udp_headers; |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 282 | ip4_udp_headers *headers = NULL; |
Klement Sekera | 402ed31 | 2017-01-18 09:44:36 +0100 | [diff] [blame] | 283 | vlib_buffer_advance (b, -sizeof (*headers)); |
| 284 | headers = vlib_buffer_get_current (b); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 285 | clib_memset (headers, 0, sizeof (*headers)); |
Klement Sekera | 402ed31 | 2017-01-18 09:44:36 +0100 | [diff] [blame] | 286 | headers->ip4.ip_version_and_header_length = 0x45; |
| 287 | headers->ip4.ttl = 255; |
| 288 | headers->ip4.protocol = IP_PROTOCOL_UDP; |
Klement Sekera | 2bce033 | 2017-02-09 06:03:46 +0100 | [diff] [blame] | 289 | headers->udp.src_port = |
| 290 | clib_host_to_net_u16 (bfd_udp_bs_idx_to_sport (bs->bs_idx)); |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 291 | if (is_echo) |
| 292 | { |
| 293 | int rv; |
| 294 | if (!(rv = bfd_udp_get_echo_src_ip4 (&headers->ip4.src_address))) |
| 295 | { |
| 296 | return rv; |
| 297 | } |
| 298 | headers->ip4.dst_address.as_u32 = key->local_addr.ip4.as_u32; |
| 299 | headers->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_bfd_echo4); |
| 300 | } |
| 301 | else |
| 302 | { |
| 303 | headers->ip4.src_address.as_u32 = key->local_addr.ip4.as_u32; |
| 304 | headers->ip4.dst_address.as_u32 = key->peer_addr.ip4.as_u32; |
| 305 | headers->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_bfd4); |
| 306 | } |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame] | 307 | |
| 308 | /* fix ip length, checksum and udp length */ |
| 309 | const u16 ip_length = vlib_buffer_length_in_chain (vm, b); |
| 310 | |
Klement Sekera | 402ed31 | 2017-01-18 09:44:36 +0100 | [diff] [blame] | 311 | headers->ip4.length = clib_host_to_net_u16 (ip_length); |
| 312 | headers->ip4.checksum = ip4_header_checksum (&headers->ip4); |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame] | 313 | |
Klement Sekera | 402ed31 | 2017-01-18 09:44:36 +0100 | [diff] [blame] | 314 | const u16 udp_length = ip_length - (sizeof (headers->ip4)); |
| 315 | headers->udp.length = clib_host_to_net_u16 (udp_length); |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 316 | return 1; |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame] | 317 | } |
| 318 | |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 319 | int |
Klement Sekera | f3bcdbf | 2017-05-02 07:38:01 +0200 | [diff] [blame] | 320 | bfd_add_udp6_transport (vlib_main_t * vm, u32 bi, const bfd_session_t * bs, |
| 321 | int is_echo) |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame] | 322 | { |
Klement Sekera | 2bce033 | 2017-02-09 06:03:46 +0100 | [diff] [blame] | 323 | const bfd_udp_session_t *bus = &bs->udp; |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame] | 324 | const bfd_udp_key_t *key = &bus->key; |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 325 | vlib_buffer_t *b = vlib_get_buffer (vm, bi); |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame] | 326 | |
Damjan Marion | 213b5aa | 2017-07-13 21:19:27 +0200 | [diff] [blame] | 327 | b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED; |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame] | 328 | vnet_buffer (b)->ip.adj_index[VLIB_RX] = bus->adj_index; |
| 329 | vnet_buffer (b)->ip.adj_index[VLIB_TX] = bus->adj_index; |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 330 | vnet_buffer (b)->sw_if_index[VLIB_RX] = 0; |
| 331 | vnet_buffer (b)->sw_if_index[VLIB_TX] = 0; |
Klement Sekera | 402ed31 | 2017-01-18 09:44:36 +0100 | [diff] [blame] | 332 | typedef struct |
| 333 | { |
| 334 | ip6_header_t ip6; |
| 335 | udp_header_t udp; |
| 336 | } ip6_udp_headers; |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 337 | ip6_udp_headers *headers = NULL; |
| 338 | vlib_buffer_advance (b, -sizeof (*headers)); |
| 339 | headers = vlib_buffer_get_current (b); |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 340 | clib_memset (headers, 0, sizeof (*headers)); |
Klement Sekera | 402ed31 | 2017-01-18 09:44:36 +0100 | [diff] [blame] | 341 | headers->ip6.ip_version_traffic_class_and_flow_label = |
Klement Sekera | c5fccc0 | 2017-01-18 09:56:00 +0100 | [diff] [blame] | 342 | clib_host_to_net_u32 (0x6 << 28); |
Klement Sekera | 402ed31 | 2017-01-18 09:44:36 +0100 | [diff] [blame] | 343 | headers->ip6.hop_limit = 255; |
| 344 | headers->ip6.protocol = IP_PROTOCOL_UDP; |
Klement Sekera | 2bce033 | 2017-02-09 06:03:46 +0100 | [diff] [blame] | 345 | headers->udp.src_port = |
| 346 | clib_host_to_net_u16 (bfd_udp_bs_idx_to_sport (bs->bs_idx)); |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 347 | if (is_echo) |
| 348 | { |
| 349 | int rv; |
| 350 | if (!(rv = bfd_udp_get_echo_src_ip6 (&headers->ip6.src_address))) |
| 351 | { |
| 352 | return rv; |
| 353 | } |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 354 | clib_memcpy_fast (&headers->ip6.dst_address, &key->local_addr.ip6, |
| 355 | sizeof (headers->ip6.dst_address)); |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 356 | |
| 357 | headers->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_bfd_echo6); |
| 358 | } |
| 359 | else |
| 360 | { |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 361 | clib_memcpy_fast (&headers->ip6.src_address, &key->local_addr.ip6, |
| 362 | sizeof (headers->ip6.src_address)); |
| 363 | clib_memcpy_fast (&headers->ip6.dst_address, &key->peer_addr.ip6, |
| 364 | sizeof (headers->ip6.dst_address)); |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 365 | headers->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_bfd6); |
| 366 | } |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame] | 367 | |
| 368 | /* fix ip payload length and udp length */ |
Klement Sekera | c5fccc0 | 2017-01-18 09:56:00 +0100 | [diff] [blame] | 369 | const u16 udp_length = |
Klement Sekera | 402ed31 | 2017-01-18 09:44:36 +0100 | [diff] [blame] | 370 | vlib_buffer_length_in_chain (vm, b) - (sizeof (headers->ip6)); |
| 371 | headers->udp.length = clib_host_to_net_u16 (udp_length); |
| 372 | headers->ip6.payload_length = headers->udp.length; |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame] | 373 | |
| 374 | /* IPv6 UDP checksum is mandatory */ |
| 375 | int bogus = 0; |
Klement Sekera | 402ed31 | 2017-01-18 09:44:36 +0100 | [diff] [blame] | 376 | headers->udp.checksum = |
| 377 | ip6_tcp_udp_icmp_compute_checksum (vm, b, &headers->ip6, &bogus); |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame] | 378 | ASSERT (bogus == 0); |
Klement Sekera | 402ed31 | 2017-01-18 09:44:36 +0100 | [diff] [blame] | 379 | if (headers->udp.checksum == 0) |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame] | 380 | { |
Klement Sekera | 402ed31 | 2017-01-18 09:44:36 +0100 | [diff] [blame] | 381 | headers->udp.checksum = 0xffff; |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame] | 382 | } |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 383 | return 1; |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 384 | } |
| 385 | |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 386 | static void |
Klement Sekera | cdaf0d8 | 2022-02-14 20:20:22 +0000 | [diff] [blame] | 387 | bfd_create_frame_to_next_node (vlib_main_t *vm, bfd_main_t *bm, |
| 388 | const bfd_session_t *bs, u32 bi, u32 next_node, |
| 389 | vlib_combined_counter_main_t *tx_counter) |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 390 | { |
| 391 | vlib_frame_t *f = vlib_get_frame_to_node (vm, next_node); |
| 392 | u32 *to_next = vlib_frame_vector_args (f); |
| 393 | to_next[0] = bi; |
| 394 | f->n_vectors = 1; |
| 395 | vlib_put_frame_to_node (vm, next_node, f); |
Klement Sekera | cdaf0d8 | 2022-02-14 20:20:22 +0000 | [diff] [blame] | 396 | vlib_buffer_t *b = vlib_get_buffer (vm, bi); |
| 397 | vlib_increment_combined_counter (tx_counter, vm->thread_index, bs->bs_idx, 1, |
| 398 | vlib_buffer_length_in_chain (vm, b)); |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | int |
| 402 | bfd_udp_calc_next_node (const struct bfd_session_s *bs, u32 * next_node) |
| 403 | { |
Matthew G Smith | 2f26014 | 2019-05-22 14:49:24 -0500 | [diff] [blame] | 404 | vnet_main_t *vnm = vnet_get_main (); |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 405 | const bfd_udp_session_t *bus = &bs->udp; |
| 406 | ip_adjacency_t *adj = adj_get (bus->adj_index); |
Matthew G Smith | 2f26014 | 2019-05-22 14:49:24 -0500 | [diff] [blame] | 407 | |
| 408 | /* don't try to send the buffer if the interface is not up */ |
| 409 | if (!vnet_sw_interface_is_up (vnm, bus->key.sw_if_index)) |
| 410 | return 0; |
| 411 | |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 412 | switch (adj->lookup_next_index) |
| 413 | { |
| 414 | case IP_LOOKUP_NEXT_ARP: |
| 415 | switch (bs->transport) |
| 416 | { |
| 417 | case BFD_TRANSPORT_UDP4: |
| 418 | *next_node = bfd_udp_main.ip4_arp_idx; |
| 419 | return 1; |
| 420 | case BFD_TRANSPORT_UDP6: |
| 421 | *next_node = bfd_udp_main.ip6_ndp_idx; |
| 422 | return 1; |
| 423 | } |
| 424 | break; |
| 425 | case IP_LOOKUP_NEXT_REWRITE: |
| 426 | switch (bs->transport) |
| 427 | { |
| 428 | case BFD_TRANSPORT_UDP4: |
| 429 | *next_node = bfd_udp_main.ip4_rewrite_idx; |
| 430 | return 1; |
| 431 | case BFD_TRANSPORT_UDP6: |
| 432 | *next_node = bfd_udp_main.ip6_rewrite_idx; |
| 433 | return 1; |
| 434 | } |
| 435 | break; |
Matthew G Smith | 2f26014 | 2019-05-22 14:49:24 -0500 | [diff] [blame] | 436 | case IP_LOOKUP_NEXT_MIDCHAIN: |
| 437 | switch (bs->transport) |
| 438 | { |
| 439 | case BFD_TRANSPORT_UDP4: |
| 440 | *next_node = bfd_udp_main.ip4_midchain_idx; |
| 441 | return 1; |
| 442 | case BFD_TRANSPORT_UDP6: |
| 443 | *next_node = bfd_udp_main.ip6_midchain_idx; |
| 444 | return 1; |
| 445 | } |
| 446 | break; |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 447 | default: |
| 448 | /* drop */ |
| 449 | break; |
| 450 | } |
| 451 | return 0; |
| 452 | } |
| 453 | |
| 454 | int |
Klement Sekera | cdaf0d8 | 2022-02-14 20:20:22 +0000 | [diff] [blame] | 455 | bfd_transport_udp4 (vlib_main_t *vm, u32 bi, const struct bfd_session_s *bs, |
| 456 | int is_echo) |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 457 | { |
| 458 | u32 next_node; |
| 459 | int rv = bfd_udp_calc_next_node (bs, &next_node); |
Klement Sekera | cdaf0d8 | 2022-02-14 20:20:22 +0000 | [diff] [blame] | 460 | bfd_main_t *bm = bfd_udp_main.bfd_main; |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 461 | if (rv) |
| 462 | { |
Klement Sekera | cdaf0d8 | 2022-02-14 20:20:22 +0000 | [diff] [blame] | 463 | bfd_create_frame_to_next_node (vm, bm, bs, bi, next_node, |
| 464 | is_echo ? &bm->tx_echo_counter : |
| 465 | &bm->tx_counter); |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 466 | } |
| 467 | return rv; |
| 468 | } |
| 469 | |
| 470 | int |
Klement Sekera | cdaf0d8 | 2022-02-14 20:20:22 +0000 | [diff] [blame] | 471 | bfd_transport_udp6 (vlib_main_t *vm, u32 bi, const struct bfd_session_s *bs, |
| 472 | int is_echo) |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 473 | { |
| 474 | u32 next_node; |
| 475 | int rv = bfd_udp_calc_next_node (bs, &next_node); |
Klement Sekera | cdaf0d8 | 2022-02-14 20:20:22 +0000 | [diff] [blame] | 476 | bfd_main_t *bm = bfd_udp_main.bfd_main; |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 477 | if (rv) |
| 478 | { |
Klement Sekera | cdaf0d8 | 2022-02-14 20:20:22 +0000 | [diff] [blame] | 479 | bfd_create_frame_to_next_node ( |
| 480 | vm, bfd_udp_main.bfd_main, bs, bi, next_node, |
| 481 | is_echo ? &bm->tx_echo_counter : &bm->tx_counter); |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 482 | } |
| 483 | return 1; |
| 484 | } |
| 485 | |
Klement Sekera | c5fccc0 | 2017-01-18 09:56:00 +0100 | [diff] [blame] | 486 | static bfd_session_t * |
| 487 | bfd_lookup_session (bfd_udp_main_t * bum, const bfd_udp_key_t * key) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 488 | { |
| 489 | uword *p = mhash_get (&bum->bfd_session_idx_by_bfd_key, key); |
| 490 | if (p) |
| 491 | { |
| 492 | return bfd_find_session_by_idx (bum->bfd_main, *p); |
| 493 | } |
| 494 | return 0; |
| 495 | } |
| 496 | |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 497 | static void |
| 498 | bfd_udp_key_init (bfd_udp_key_t * key, u32 sw_if_index, |
| 499 | const ip46_address_t * local_addr, |
| 500 | const ip46_address_t * peer_addr) |
| 501 | { |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 502 | clib_memset (key, 0, sizeof (*key)); |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 503 | key->sw_if_index = sw_if_index; |
| 504 | key->local_addr.as_u64[0] = local_addr->as_u64[0]; |
| 505 | key->local_addr.as_u64[1] = local_addr->as_u64[1]; |
| 506 | key->peer_addr.as_u64[0] = peer_addr->as_u64[0]; |
| 507 | key->peer_addr.as_u64[1] = peer_addr->as_u64[1]; |
| 508 | } |
| 509 | |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 510 | static vnet_api_error_t |
Klement Sekera | 88aec65 | 2019-03-11 14:04:19 +0100 | [diff] [blame] | 511 | bfd_udp_add_session_internal (vlib_main_t * vm, bfd_udp_main_t * bum, |
| 512 | u32 sw_if_index, u32 desired_min_tx_usec, |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 513 | u32 required_min_rx_usec, u8 detect_mult, |
Klement Sekera | c5fccc0 | 2017-01-18 09:56:00 +0100 | [diff] [blame] | 514 | const ip46_address_t * local_addr, |
| 515 | const ip46_address_t * peer_addr, |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 516 | bfd_session_t ** bs_out) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 517 | { |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 518 | /* get a pool entry and if we end up not needing it, give it back */ |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 519 | bfd_transport_e t = BFD_TRANSPORT_UDP4; |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 520 | if (!ip46_address_is_ip4 (local_addr)) |
| 521 | { |
| 522 | t = BFD_TRANSPORT_UDP6; |
| 523 | } |
| 524 | bfd_session_t *bs = bfd_get_session (bum->bfd_main, t); |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 525 | if (!bs) |
| 526 | { |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 527 | return VNET_API_ERROR_BFD_EAGAIN; |
| 528 | } |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 529 | bfd_udp_session_t *bus = &bs->udp; |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 530 | clib_memset (bus, 0, sizeof (*bus)); |
Neale Ranns | 32fd89b | 2022-02-15 08:28:19 +0000 | [diff] [blame] | 531 | bus->adj_index = ADJ_INDEX_INVALID; |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 532 | bfd_udp_key_t *key = &bus->key; |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 533 | bfd_udp_key_init (key, sw_if_index, local_addr, peer_addr); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 534 | const bfd_session_t *tmp = bfd_lookup_session (bum, key); |
| 535 | if (tmp) |
| 536 | { |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 537 | vlib_log_err (bum->log_class, |
| 538 | "duplicate bfd-udp session, existing bs_idx=%d", |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 539 | tmp->bs_idx); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 540 | bfd_put_session (bum->bfd_main, bs); |
| 541 | return VNET_API_ERROR_BFD_EEXIST; |
| 542 | } |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 543 | mhash_set (&bum->bfd_session_idx_by_bfd_key, key, bs->bs_idx, NULL); |
| 544 | BFD_DBG ("session created, bs_idx=%u, sw_if_index=%d, local=%U, peer=%U", |
Klement Sekera | c5fccc0 | 2017-01-18 09:56:00 +0100 | [diff] [blame] | 545 | bs->bs_idx, key->sw_if_index, format_ip46_address, |
| 546 | &key->local_addr, IP46_TYPE_ANY, format_ip46_address, |
| 547 | &key->peer_addr, IP46_TYPE_ANY); |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 548 | vlib_log_info (bum->log_class, "create BFD session: %U", |
| 549 | format_bfd_session, bs); |
Neale Ranns | 558a354 | 2022-02-15 08:24:48 +0000 | [diff] [blame] | 550 | const ip46_address_t *peer = |
| 551 | (vnet_sw_interface_is_p2p (vnet_get_main (), key->sw_if_index) ? |
| 552 | &zero_addr : |
| 553 | &key->peer_addr); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 554 | if (BFD_TRANSPORT_UDP4 == t) |
| 555 | { |
| 556 | bus->adj_index = adj_nbr_add_or_lock (FIB_PROTOCOL_IP4, VNET_LINK_IP4, |
Neale Ranns | 558a354 | 2022-02-15 08:24:48 +0000 | [diff] [blame] | 557 | peer, key->sw_if_index); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 558 | BFD_DBG ("adj_nbr_add_or_lock(FIB_PROTOCOL_IP4, VNET_LINK_IP4, %U, %d) " |
Neale Ranns | 558a354 | 2022-02-15 08:24:48 +0000 | [diff] [blame] | 559 | "returns %d", |
| 560 | format_ip46_address, peer, IP46_TYPE_ANY, key->sw_if_index, |
| 561 | bus->adj_index); |
Klement Sekera | 88aec65 | 2019-03-11 14:04:19 +0100 | [diff] [blame] | 562 | ++bum->udp4_sessions_count; |
Klement Sekera | cdaf0d8 | 2022-02-14 20:20:22 +0000 | [diff] [blame] | 563 | bfd_udp_update_stat_segment_entry ( |
| 564 | bum->udp4_sessions_count_stat_seg_entry, bum->udp4_sessions_count); |
Klement Sekera | 88aec65 | 2019-03-11 14:04:19 +0100 | [diff] [blame] | 565 | if (1 == bum->udp4_sessions_count) |
| 566 | { |
| 567 | udp_register_dst_port (vm, UDP_DST_PORT_bfd4, |
| 568 | bfd_udp4_input_node.index, 1); |
| 569 | udp_register_dst_port (vm, UDP_DST_PORT_bfd_echo4, |
| 570 | bfd_udp_echo4_input_node.index, 1); |
| 571 | } |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 572 | } |
| 573 | else |
| 574 | { |
| 575 | bus->adj_index = adj_nbr_add_or_lock (FIB_PROTOCOL_IP6, VNET_LINK_IP6, |
Neale Ranns | 558a354 | 2022-02-15 08:24:48 +0000 | [diff] [blame] | 576 | peer, key->sw_if_index); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 577 | BFD_DBG ("adj_nbr_add_or_lock(FIB_PROTOCOL_IP6, VNET_LINK_IP6, %U, %d) " |
Neale Ranns | 558a354 | 2022-02-15 08:24:48 +0000 | [diff] [blame] | 578 | "returns %d", |
| 579 | format_ip46_address, peer, IP46_TYPE_ANY, key->sw_if_index, |
| 580 | bus->adj_index); |
Klement Sekera | 88aec65 | 2019-03-11 14:04:19 +0100 | [diff] [blame] | 581 | ++bum->udp6_sessions_count; |
Klement Sekera | cdaf0d8 | 2022-02-14 20:20:22 +0000 | [diff] [blame] | 582 | bfd_udp_update_stat_segment_entry ( |
| 583 | bum->udp6_sessions_count_stat_seg_entry, bum->udp6_sessions_count); |
Klement Sekera | 88aec65 | 2019-03-11 14:04:19 +0100 | [diff] [blame] | 584 | if (1 == bum->udp6_sessions_count) |
| 585 | { |
| 586 | udp_register_dst_port (vm, UDP_DST_PORT_bfd6, |
| 587 | bfd_udp6_input_node.index, 0); |
| 588 | udp_register_dst_port (vm, UDP_DST_PORT_bfd_echo6, |
| 589 | bfd_udp_echo6_input_node.index, 0); |
| 590 | } |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 591 | } |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 592 | *bs_out = bs; |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 593 | return bfd_session_set_params (bum->bfd_main, bs, desired_min_tx_usec, |
| 594 | required_min_rx_usec, detect_mult); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | static vnet_api_error_t |
Klement Sekera | c5fccc0 | 2017-01-18 09:56:00 +0100 | [diff] [blame] | 598 | bfd_udp_validate_api_input (u32 sw_if_index, |
| 599 | const ip46_address_t * local_addr, |
| 600 | const ip46_address_t * peer_addr) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 601 | { |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 602 | bfd_udp_main_t *bum = &bfd_udp_main; |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 603 | vnet_sw_interface_t *sw_if = |
Dave Barach | 3940de3 | 2019-07-23 16:28:36 -0400 | [diff] [blame] | 604 | vnet_get_sw_interface_or_null (bfd_udp_main.vnet_main, sw_if_index); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 605 | if (!sw_if) |
| 606 | { |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 607 | vlib_log_err (bum->log_class, |
| 608 | "got NULL sw_if when getting interface by index %u", |
| 609 | sw_if_index); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 610 | return VNET_API_ERROR_INVALID_SW_IF_INDEX; |
| 611 | } |
| 612 | if (ip46_address_is_ip4 (local_addr)) |
| 613 | { |
| 614 | if (!ip46_address_is_ip4 (peer_addr)) |
Klement Sekera | c5fccc0 | 2017-01-18 09:56:00 +0100 | [diff] [blame] | 615 | { |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 616 | vlib_log_err (bum->log_class, |
| 617 | "IP family mismatch (local is ipv4, peer is ipv6)"); |
Klement Sekera | c5fccc0 | 2017-01-18 09:56:00 +0100 | [diff] [blame] | 618 | return VNET_API_ERROR_INVALID_ARGUMENT; |
| 619 | } |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 620 | } |
| 621 | else |
| 622 | { |
| 623 | if (ip46_address_is_ip4 (peer_addr)) |
Klement Sekera | c5fccc0 | 2017-01-18 09:56:00 +0100 | [diff] [blame] | 624 | { |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 625 | vlib_log_err (bum->log_class, |
| 626 | "IP family mismatch (local is ipv6, peer is ipv4)"); |
Klement Sekera | c5fccc0 | 2017-01-18 09:56:00 +0100 | [diff] [blame] | 627 | return VNET_API_ERROR_INVALID_ARGUMENT; |
| 628 | } |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | return 0; |
| 632 | } |
| 633 | |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 634 | static vnet_api_error_t |
| 635 | bfd_udp_find_session_by_api_input (u32 sw_if_index, |
| 636 | const ip46_address_t * local_addr, |
| 637 | const ip46_address_t * peer_addr, |
| 638 | bfd_session_t ** bs_out) |
| 639 | { |
| 640 | vnet_api_error_t rv = |
| 641 | bfd_udp_validate_api_input (sw_if_index, local_addr, peer_addr); |
| 642 | if (!rv) |
| 643 | { |
| 644 | bfd_udp_main_t *bum = &bfd_udp_main; |
| 645 | bfd_udp_key_t key; |
| 646 | bfd_udp_key_init (&key, sw_if_index, local_addr, peer_addr); |
| 647 | bfd_session_t *bs = bfd_lookup_session (bum, &key); |
| 648 | if (bs) |
| 649 | { |
| 650 | *bs_out = bs; |
| 651 | } |
| 652 | else |
| 653 | { |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 654 | vlib_log_err (bum->log_class, |
| 655 | "BFD session not found, sw_if_index=%u, local=%U, peer=%U", |
| 656 | sw_if_index, format_ip46_address, local_addr, |
| 657 | IP46_TYPE_ANY, format_ip46_address, peer_addr, |
| 658 | IP46_TYPE_ANY); |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 659 | return VNET_API_ERROR_BFD_ENOENT; |
| 660 | } |
| 661 | } |
| 662 | return rv; |
| 663 | } |
| 664 | |
| 665 | static vnet_api_error_t |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 666 | bfd_api_verify_common (u32 sw_if_index, u32 desired_min_tx_usec, |
Klement Sekera | b59f63b | 2022-02-14 20:19:28 +0000 | [diff] [blame] | 667 | u8 detect_mult, const ip46_address_t *local_addr, |
| 668 | const ip46_address_t *peer_addr) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 669 | { |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 670 | bfd_udp_main_t *bum = &bfd_udp_main; |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 671 | vnet_api_error_t rv = |
Klement Sekera | c5fccc0 | 2017-01-18 09:56:00 +0100 | [diff] [blame] | 672 | bfd_udp_validate_api_input (sw_if_index, local_addr, peer_addr); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 673 | if (rv) |
| 674 | { |
| 675 | return rv; |
| 676 | } |
| 677 | if (detect_mult < 1) |
| 678 | { |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 679 | vlib_log_err (bum->log_class, "detect_mult < 1"); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 680 | return VNET_API_ERROR_INVALID_ARGUMENT; |
| 681 | } |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 682 | if (desired_min_tx_usec < 1) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 683 | { |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 684 | vlib_log_err (bum->log_class, "desired_min_tx_usec < 1"); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 685 | return VNET_API_ERROR_INVALID_ARGUMENT; |
| 686 | } |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 687 | return 0; |
| 688 | } |
| 689 | |
| 690 | static void |
Klement Sekera | 88aec65 | 2019-03-11 14:04:19 +0100 | [diff] [blame] | 691 | bfd_udp_del_session_internal (vlib_main_t * vm, bfd_session_t * bs) |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 692 | { |
| 693 | bfd_udp_main_t *bum = &bfd_udp_main; |
| 694 | BFD_DBG ("free bfd-udp session, bs_idx=%d", bs->bs_idx); |
Neale Ranns | 32fd89b | 2022-02-15 08:28:19 +0000 | [diff] [blame] | 695 | bfd_session_stop (bum->bfd_main, bs); |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 696 | mhash_unset (&bum->bfd_session_idx_by_bfd_key, &bs->udp.key, NULL); |
| 697 | adj_unlock (bs->udp.adj_index); |
Klement Sekera | 88aec65 | 2019-03-11 14:04:19 +0100 | [diff] [blame] | 698 | switch (bs->transport) |
| 699 | { |
| 700 | case BFD_TRANSPORT_UDP4: |
| 701 | --bum->udp4_sessions_count; |
Klement Sekera | cdaf0d8 | 2022-02-14 20:20:22 +0000 | [diff] [blame] | 702 | bfd_udp_update_stat_segment_entry ( |
| 703 | bum->udp4_sessions_count_stat_seg_entry, bum->udp4_sessions_count); |
Klement Sekera | 88aec65 | 2019-03-11 14:04:19 +0100 | [diff] [blame] | 704 | if (!bum->udp4_sessions_count) |
| 705 | { |
| 706 | udp_unregister_dst_port (vm, UDP_DST_PORT_bfd4, 1); |
| 707 | udp_unregister_dst_port (vm, UDP_DST_PORT_bfd_echo4, 1); |
| 708 | } |
| 709 | break; |
| 710 | case BFD_TRANSPORT_UDP6: |
| 711 | --bum->udp6_sessions_count; |
Klement Sekera | cdaf0d8 | 2022-02-14 20:20:22 +0000 | [diff] [blame] | 712 | bfd_udp_update_stat_segment_entry ( |
| 713 | bum->udp6_sessions_count_stat_seg_entry, bum->udp6_sessions_count); |
Klement Sekera | 88aec65 | 2019-03-11 14:04:19 +0100 | [diff] [blame] | 714 | if (!bum->udp6_sessions_count) |
| 715 | { |
| 716 | udp_unregister_dst_port (vm, UDP_DST_PORT_bfd6, 0); |
| 717 | udp_unregister_dst_port (vm, UDP_DST_PORT_bfd_echo6, 0); |
| 718 | } |
| 719 | break; |
| 720 | } |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 721 | bfd_put_session (bum->bfd_main, bs); |
| 722 | } |
| 723 | |
Neale Ranns | 63f2c7d | 2022-02-09 13:47:29 +0000 | [diff] [blame] | 724 | static vnet_api_error_t |
| 725 | bfd_udp_add_and_start_session (u32 sw_if_index, |
| 726 | const ip46_address_t *local_addr, |
| 727 | const ip46_address_t *peer_addr, |
| 728 | u32 desired_min_tx_usec, |
| 729 | u32 required_min_rx_usec, u8 detect_mult, |
| 730 | u8 is_authenticated, u32 conf_key_id, |
| 731 | u8 bfd_key_id) |
| 732 | { |
| 733 | bfd_session_t *bs = NULL; |
| 734 | vnet_api_error_t rv; |
| 735 | |
| 736 | rv = bfd_udp_add_session_internal ( |
| 737 | vlib_get_main (), &bfd_udp_main, sw_if_index, desired_min_tx_usec, |
| 738 | required_min_rx_usec, detect_mult, local_addr, peer_addr, &bs); |
| 739 | |
| 740 | if (!rv && is_authenticated) |
| 741 | { |
| 742 | rv = bfd_auth_activate (bs, conf_key_id, bfd_key_id, |
| 743 | 0 /* is not delayed */); |
| 744 | if (rv) |
| 745 | { |
| 746 | bfd_udp_del_session_internal (vlib_get_main (), bs); |
| 747 | } |
| 748 | } |
| 749 | if (!rv) |
| 750 | { |
| 751 | bfd_session_start (bfd_udp_main.bfd_main, bs); |
| 752 | } |
| 753 | |
| 754 | return rv; |
| 755 | } |
| 756 | |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 757 | vnet_api_error_t |
| 758 | bfd_udp_add_session (u32 sw_if_index, const ip46_address_t * local_addr, |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 759 | const ip46_address_t * peer_addr, |
| 760 | u32 desired_min_tx_usec, u32 required_min_rx_usec, |
| 761 | u8 detect_mult, u8 is_authenticated, u32 conf_key_id, |
| 762 | u8 bfd_key_id) |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 763 | { |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame] | 764 | bfd_main_t *bm = &bfd_main; |
| 765 | bfd_lock (bm); |
| 766 | |
Klement Sekera | b59f63b | 2022-02-14 20:19:28 +0000 | [diff] [blame] | 767 | vnet_api_error_t rv = bfd_api_verify_common ( |
| 768 | sw_if_index, desired_min_tx_usec, detect_mult, local_addr, peer_addr); |
Neale Ranns | 63f2c7d | 2022-02-09 13:47:29 +0000 | [diff] [blame] | 769 | |
| 770 | if (!rv) |
| 771 | rv = bfd_udp_add_and_start_session ( |
| 772 | sw_if_index, local_addr, peer_addr, desired_min_tx_usec, |
| 773 | required_min_rx_usec, detect_mult, is_authenticated, conf_key_id, |
| 774 | bfd_key_id); |
| 775 | |
| 776 | bfd_unlock (bm); |
| 777 | return rv; |
| 778 | } |
| 779 | |
| 780 | vnet_api_error_t |
| 781 | bfd_udp_upd_session (u32 sw_if_index, const ip46_address_t *local_addr, |
| 782 | const ip46_address_t *peer_addr, u32 desired_min_tx_usec, |
| 783 | u32 required_min_rx_usec, u8 detect_mult, |
| 784 | u8 is_authenticated, u32 conf_key_id, u8 bfd_key_id) |
| 785 | { |
| 786 | bfd_main_t *bm = &bfd_main; |
| 787 | bfd_lock (bm); |
| 788 | |
| 789 | vnet_api_error_t rv = bfd_api_verify_common ( |
Klement Sekera | b59f63b | 2022-02-14 20:19:28 +0000 | [diff] [blame] | 790 | sw_if_index, desired_min_tx_usec, detect_mult, local_addr, peer_addr); |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 791 | if (!rv) |
| 792 | { |
Neale Ranns | 63f2c7d | 2022-02-09 13:47:29 +0000 | [diff] [blame] | 793 | bfd_session_t *bs = NULL; |
| 794 | |
| 795 | rv = bfd_udp_find_session_by_api_input (sw_if_index, local_addr, |
| 796 | peer_addr, &bs); |
| 797 | if (VNET_API_ERROR_BFD_ENOENT == rv) |
| 798 | rv = bfd_udp_add_and_start_session ( |
| 799 | sw_if_index, local_addr, peer_addr, desired_min_tx_usec, |
| 800 | required_min_rx_usec, detect_mult, is_authenticated, conf_key_id, |
| 801 | bfd_key_id); |
| 802 | else |
| 803 | rv = bfd_session_set_params (bfd_udp_main.bfd_main, bs, |
| 804 | desired_min_tx_usec, required_min_rx_usec, |
| 805 | detect_mult); |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 806 | } |
| 807 | |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame] | 808 | bfd_unlock (bm); |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 809 | return rv; |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 810 | } |
| 811 | |
Klement Sekera | c5fccc0 | 2017-01-18 09:56:00 +0100 | [diff] [blame] | 812 | vnet_api_error_t |
Neale Ranns | 63f2c7d | 2022-02-09 13:47:29 +0000 | [diff] [blame] | 813 | bfd_udp_mod_session (u32 sw_if_index, const ip46_address_t *local_addr, |
| 814 | const ip46_address_t *peer_addr, u32 desired_min_tx_usec, |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 815 | u32 required_min_rx_usec, u8 detect_mult) |
| 816 | { |
| 817 | bfd_session_t *bs = NULL; |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame] | 818 | bfd_main_t *bm = &bfd_main; |
| 819 | vnet_api_error_t error; |
| 820 | bfd_lock (bm); |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 821 | vnet_api_error_t rv = |
| 822 | bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr, |
| 823 | &bs); |
| 824 | if (rv) |
| 825 | { |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame] | 826 | bfd_unlock (bm); |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 827 | return rv; |
| 828 | } |
| 829 | |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame] | 830 | error = bfd_session_set_params (bfd_udp_main.bfd_main, bs, |
| 831 | desired_min_tx_usec, required_min_rx_usec, |
| 832 | detect_mult); |
| 833 | bfd_unlock (bm); |
| 834 | return error; |
Klement Sekera | a57a970 | 2017-02-02 06:58:07 +0100 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | vnet_api_error_t |
Klement Sekera | c5fccc0 | 2017-01-18 09:56:00 +0100 | [diff] [blame] | 838 | bfd_udp_del_session (u32 sw_if_index, |
| 839 | const ip46_address_t * local_addr, |
| 840 | const ip46_address_t * peer_addr) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 841 | { |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 842 | bfd_session_t *bs = NULL; |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame] | 843 | bfd_main_t *bm = &bfd_main; |
| 844 | bfd_lock (bm); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 845 | vnet_api_error_t rv = |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 846 | bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr, |
| 847 | &bs); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 848 | if (rv) |
| 849 | { |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame] | 850 | bfd_unlock (bm); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 851 | return rv; |
| 852 | } |
Klement Sekera | 88aec65 | 2019-03-11 14:04:19 +0100 | [diff] [blame] | 853 | bfd_udp_del_session_internal (vlib_get_main (), bs); |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame] | 854 | bfd_unlock (bm); |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 855 | return 0; |
| 856 | } |
| 857 | |
| 858 | vnet_api_error_t |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 859 | bfd_udp_session_set_flags (vlib_main_t * vm, u32 sw_if_index, |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 860 | const ip46_address_t * local_addr, |
| 861 | const ip46_address_t * peer_addr, u8 admin_up_down) |
| 862 | { |
| 863 | bfd_session_t *bs = NULL; |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame] | 864 | bfd_main_t *bm = &bfd_main; |
| 865 | bfd_lock (bm); |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 866 | vnet_api_error_t rv = |
| 867 | bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr, |
| 868 | &bs); |
| 869 | if (rv) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 870 | { |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame] | 871 | bfd_unlock (bm); |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 872 | return rv; |
| 873 | } |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 874 | bfd_session_set_flags (vm, bs, admin_up_down); |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame] | 875 | bfd_unlock (bm); |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 876 | return 0; |
| 877 | } |
| 878 | |
| 879 | vnet_api_error_t |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 880 | bfd_udp_auth_activate (u32 sw_if_index, |
| 881 | const ip46_address_t * local_addr, |
| 882 | const ip46_address_t * peer_addr, |
| 883 | u32 conf_key_id, u8 key_id, u8 is_delayed) |
| 884 | { |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame] | 885 | bfd_main_t *bm = &bfd_main; |
| 886 | bfd_lock (bm); |
| 887 | vnet_api_error_t error; |
| 888 | |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 889 | bfd_session_t *bs = NULL; |
| 890 | vnet_api_error_t rv = |
| 891 | bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr, |
| 892 | &bs); |
| 893 | if (rv) |
| 894 | { |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame] | 895 | bfd_unlock (bm); |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 896 | return rv; |
| 897 | } |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame] | 898 | error = bfd_auth_activate (bs, conf_key_id, key_id, is_delayed); |
| 899 | bfd_unlock (bm); |
| 900 | return error; |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 901 | } |
| 902 | |
| 903 | vnet_api_error_t |
| 904 | bfd_udp_auth_deactivate (u32 sw_if_index, |
| 905 | const ip46_address_t * local_addr, |
| 906 | const ip46_address_t * peer_addr, u8 is_delayed) |
| 907 | { |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame] | 908 | bfd_main_t *bm = &bfd_main; |
| 909 | vnet_api_error_t error; |
| 910 | bfd_lock (bm); |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 911 | bfd_session_t *bs = NULL; |
| 912 | vnet_api_error_t rv = |
| 913 | bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr, |
| 914 | &bs); |
| 915 | if (rv) |
| 916 | { |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame] | 917 | bfd_unlock (bm); |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 918 | return rv; |
| 919 | } |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame] | 920 | error = bfd_auth_deactivate (bs, is_delayed); |
| 921 | bfd_unlock (bm); |
| 922 | return error; |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 923 | } |
| 924 | |
Klement Sekera | c5fccc0 | 2017-01-18 09:56:00 +0100 | [diff] [blame] | 925 | typedef enum |
| 926 | { |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 927 | BFD_UDP_INPUT_NEXT_NORMAL, |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 928 | BFD_UDP_INPUT_NEXT_REPLY_ARP, |
| 929 | BFD_UDP_INPUT_NEXT_REPLY_REWRITE, |
Matthew G Smith | 2f26014 | 2019-05-22 14:49:24 -0500 | [diff] [blame] | 930 | BFD_UDP_INPUT_NEXT_REPLY_MIDCHAIN, |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 931 | BFD_UDP_INPUT_N_NEXT, |
| 932 | } bfd_udp_input_next_t; |
| 933 | |
Matthew G Smith | 2f26014 | 2019-05-22 14:49:24 -0500 | [diff] [blame] | 934 | typedef enum |
| 935 | { |
| 936 | BFD_UDP_ECHO_INPUT_NEXT_NORMAL, |
| 937 | BFD_UDP_ECHO_INPUT_NEXT_REPLY_ARP, |
| 938 | BFD_UDP_ECHO_INPUT_NEXT_REPLY_REWRITE, |
| 939 | BFD_UDP_ECHO_INPUT_N_NEXT, |
| 940 | } bfd_udp_echo_input_next_t; |
| 941 | |
Neale Ranns | 5c7e579 | 2022-08-09 00:03:17 +0000 | [diff] [blame] | 942 | static_always_inline vl_counter_bfd_udp_enum_t |
Neale Ranns | 0c50dbb | 2022-08-05 03:40:43 +0000 | [diff] [blame] | 943 | bfd_error_to_udp (bfd_error_t e) |
| 944 | { |
| 945 | /* The UDP error is a super set of the proto independent errors */ |
Neale Ranns | 5c7e579 | 2022-08-09 00:03:17 +0000 | [diff] [blame] | 946 | return ((vl_counter_bfd_udp_enum_t) e); |
Neale Ranns | 0c50dbb | 2022-08-05 03:40:43 +0000 | [diff] [blame] | 947 | } |
| 948 | |
Klement Sekera | c5fccc0 | 2017-01-18 09:56:00 +0100 | [diff] [blame] | 949 | static void |
Klement Sekera | aeeac3b | 2017-02-14 07:11:52 +0100 | [diff] [blame] | 950 | bfd_udp4_find_headers (vlib_buffer_t * b, ip4_header_t ** ip4, |
| 951 | udp_header_t ** udp) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 952 | { |
Klement Sekera | 0c1519b | 2016-12-08 05:03:32 +0100 | [diff] [blame] | 953 | /* sanity check first */ |
Damjan Marion | 072401e | 2017-07-13 18:53:27 +0200 | [diff] [blame] | 954 | const i32 start = vnet_buffer (b)->l3_hdr_offset; |
Klement Sekera | b59f63b | 2022-02-14 20:19:28 +0000 | [diff] [blame] | 955 | if (start < -(signed) sizeof (b->pre_data)) |
Klement Sekera | 0c1519b | 2016-12-08 05:03:32 +0100 | [diff] [blame] | 956 | { |
| 957 | BFD_ERR ("Start of ip header is before pre_data, ignoring"); |
| 958 | *ip4 = NULL; |
| 959 | *udp = NULL; |
| 960 | return; |
| 961 | } |
Klement Sekera | c5fccc0 | 2017-01-18 09:56:00 +0100 | [diff] [blame] | 962 | *ip4 = (ip4_header_t *) (b->data + start); |
| 963 | if ((u8 *) * ip4 > (u8 *) vlib_buffer_get_current (b)) |
Klement Sekera | 0c1519b | 2016-12-08 05:03:32 +0100 | [diff] [blame] | 964 | { |
| 965 | BFD_ERR ("Start of ip header is beyond current data, ignoring"); |
| 966 | *ip4 = NULL; |
| 967 | *udp = NULL; |
| 968 | return; |
| 969 | } |
Klement Sekera | c5fccc0 | 2017-01-18 09:56:00 +0100 | [diff] [blame] | 970 | *udp = (udp_header_t *) ((*ip4) + 1); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 971 | } |
| 972 | |
Neale Ranns | 5c7e579 | 2022-08-09 00:03:17 +0000 | [diff] [blame] | 973 | static vl_counter_bfd_udp_enum_t |
| 974 | bfd_udp4_verify_transport (const ip4_header_t *ip4, const udp_header_t *udp, |
| 975 | const bfd_session_t *bs) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 976 | { |
| 977 | const bfd_udp_session_t *bus = &bs->udp; |
| 978 | const bfd_udp_key_t *key = &bus->key; |
| 979 | if (ip4->src_address.as_u32 != key->peer_addr.ip4.as_u32) |
| 980 | { |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame] | 981 | BFD_ERR ("IPv4 src addr mismatch, got %U, expected %U", |
Klement Sekera | c5fccc0 | 2017-01-18 09:56:00 +0100 | [diff] [blame] | 982 | format_ip4_address, ip4->src_address.as_u8, format_ip4_address, |
| 983 | key->peer_addr.ip4.as_u8); |
Neale Ranns | 0c50dbb | 2022-08-05 03:40:43 +0000 | [diff] [blame] | 984 | return BFD_UDP_ERROR_SRC_MISMATCH; |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 985 | } |
| 986 | if (ip4->dst_address.as_u32 != key->local_addr.ip4.as_u32) |
| 987 | { |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame] | 988 | BFD_ERR ("IPv4 dst addr mismatch, got %U, expected %U", |
Klement Sekera | c5fccc0 | 2017-01-18 09:56:00 +0100 | [diff] [blame] | 989 | format_ip4_address, ip4->dst_address.as_u8, format_ip4_address, |
| 990 | key->local_addr.ip4.as_u8); |
Neale Ranns | 0c50dbb | 2022-08-05 03:40:43 +0000 | [diff] [blame] | 991 | return BFD_UDP_ERROR_DST_MISMATCH; |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 992 | } |
| 993 | const u8 expected_ttl = 255; |
| 994 | if (ip4->ttl != expected_ttl) |
| 995 | { |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame] | 996 | BFD_ERR ("IPv4 unexpected TTL value %u, expected %u", ip4->ttl, |
Klement Sekera | c5fccc0 | 2017-01-18 09:56:00 +0100 | [diff] [blame] | 997 | expected_ttl); |
Neale Ranns | 0c50dbb | 2022-08-05 03:40:43 +0000 | [diff] [blame] | 998 | return BFD_UDP_ERROR_TTL; |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 999 | } |
Klement Sekera | 6f96649 | 2017-02-08 07:42:08 +0100 | [diff] [blame] | 1000 | if (clib_net_to_host_u16 (udp->src_port) < 49152) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1001 | { |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame] | 1002 | BFD_ERR ("Invalid UDP src port %u, out of range <49152,65535>", |
Klement Sekera | c5fccc0 | 2017-01-18 09:56:00 +0100 | [diff] [blame] | 1003 | udp->src_port); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1004 | } |
| 1005 | return BFD_UDP_ERROR_NONE; |
| 1006 | } |
| 1007 | |
| 1008 | typedef struct |
| 1009 | { |
| 1010 | u32 bs_idx; |
| 1011 | bfd_pkt_t pkt; |
| 1012 | } bfd_rpc_update_t; |
| 1013 | |
Neale Ranns | 0c50dbb | 2022-08-05 03:40:43 +0000 | [diff] [blame] | 1014 | static bfd_error_t |
| 1015 | bfd_rpc_update_session (vlib_main_t *vm, u32 bs_idx, const bfd_pkt_t *pkt) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1016 | { |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame] | 1017 | bfd_main_t *bm = &bfd_main; |
Neale Ranns | 0c50dbb | 2022-08-05 03:40:43 +0000 | [diff] [blame] | 1018 | bfd_error_t err; |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame] | 1019 | bfd_lock (bm); |
Neale Ranns | 0c50dbb | 2022-08-05 03:40:43 +0000 | [diff] [blame] | 1020 | err = bfd_consume_pkt (vm, bm, pkt, bs_idx); |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame] | 1021 | bfd_unlock (bm); |
Neale Ranns | 0c50dbb | 2022-08-05 03:40:43 +0000 | [diff] [blame] | 1022 | |
| 1023 | return err; |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1024 | } |
| 1025 | |
Neale Ranns | 5c7e579 | 2022-08-09 00:03:17 +0000 | [diff] [blame] | 1026 | static vl_counter_bfd_udp_enum_t |
Klement Sekera | b59f63b | 2022-02-14 20:19:28 +0000 | [diff] [blame] | 1027 | bfd_udp4_scan (vlib_main_t *vm, vlib_buffer_t *b, bfd_session_t **bs_out) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1028 | { |
| 1029 | const bfd_pkt_t *pkt = vlib_buffer_get_current (b); |
| 1030 | if (sizeof (*pkt) > b->current_length) |
| 1031 | { |
Klement Sekera | c5fccc0 | 2017-01-18 09:56:00 +0100 | [diff] [blame] | 1032 | BFD_ERR |
| 1033 | ("Payload size %d too small to hold bfd packet of minimum size %d", |
| 1034 | b->current_length, sizeof (*pkt)); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1035 | return BFD_UDP_ERROR_BAD; |
| 1036 | } |
Klement Sekera | aeeac3b | 2017-02-14 07:11:52 +0100 | [diff] [blame] | 1037 | ip4_header_t *ip4; |
| 1038 | udp_header_t *udp; |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1039 | bfd_udp4_find_headers (b, &ip4, &udp); |
| 1040 | if (!ip4 || !udp) |
| 1041 | { |
| 1042 | BFD_ERR ("Couldn't find ip4 or udp header"); |
| 1043 | return BFD_UDP_ERROR_BAD; |
| 1044 | } |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 1045 | const u32 udp_payload_length = udp->length - sizeof (*udp); |
| 1046 | if (pkt->head.length > udp_payload_length) |
| 1047 | { |
| 1048 | BFD_ERR |
| 1049 | ("BFD packet length is larger than udp payload length (%u > %u)", |
| 1050 | pkt->head.length, udp_payload_length); |
Neale Ranns | 0c50dbb | 2022-08-05 03:40:43 +0000 | [diff] [blame] | 1051 | return BFD_UDP_ERROR_LENGTH; |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 1052 | } |
Neale Ranns | 5c7e579 | 2022-08-09 00:03:17 +0000 | [diff] [blame] | 1053 | vl_counter_bfd_udp_enum_t err; |
Neale Ranns | 0c50dbb | 2022-08-05 03:40:43 +0000 | [diff] [blame] | 1054 | if (BFD_UDP_ERROR_NONE != |
| 1055 | (err = bfd_error_to_udp (bfd_verify_pkt_common (pkt)))) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1056 | { |
Neale Ranns | 0c50dbb | 2022-08-05 03:40:43 +0000 | [diff] [blame] | 1057 | return err; |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1058 | } |
| 1059 | bfd_session_t *bs = NULL; |
| 1060 | if (pkt->your_disc) |
| 1061 | { |
| 1062 | BFD_DBG ("Looking up BFD session using discriminator %u", |
Klement Sekera | c5fccc0 | 2017-01-18 09:56:00 +0100 | [diff] [blame] | 1063 | pkt->your_disc); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1064 | bs = bfd_find_session_by_disc (bfd_udp_main.bfd_main, pkt->your_disc); |
| 1065 | } |
| 1066 | else |
| 1067 | { |
| 1068 | bfd_udp_key_t key; |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 1069 | clib_memset (&key, 0, sizeof (key)); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1070 | key.sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX]; |
| 1071 | key.local_addr.ip4.as_u32 = ip4->dst_address.as_u32; |
| 1072 | key.peer_addr.ip4.as_u32 = ip4->src_address.as_u32; |
| 1073 | BFD_DBG ("Looking up BFD session using key (sw_if_index=%u, local=%U, " |
Klement Sekera | c5fccc0 | 2017-01-18 09:56:00 +0100 | [diff] [blame] | 1074 | "peer=%U)", |
| 1075 | key.sw_if_index, format_ip4_address, key.local_addr.ip4.as_u8, |
| 1076 | format_ip4_address, key.peer_addr.ip4.as_u8); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1077 | bs = bfd_lookup_session (&bfd_udp_main, &key); |
| 1078 | } |
| 1079 | if (!bs) |
| 1080 | { |
| 1081 | BFD_ERR ("BFD session lookup failed - no session matches BFD pkt"); |
Neale Ranns | 0c50dbb | 2022-08-05 03:40:43 +0000 | [diff] [blame] | 1082 | return BFD_UDP_ERROR_NO_SESSION; |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1083 | } |
Klement Sekera | 637b9c4 | 2016-12-08 05:19:14 +0100 | [diff] [blame] | 1084 | BFD_DBG ("BFD session found, bs_idx=%u", bs->bs_idx); |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 1085 | if (!bfd_verify_pkt_auth (vm, pkt, b->current_length, bs)) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1086 | { |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 1087 | BFD_ERR ("Packet verification failed, dropping packet"); |
Neale Ranns | 0c50dbb | 2022-08-05 03:40:43 +0000 | [diff] [blame] | 1088 | return BFD_UDP_ERROR_FAILED_VERIFICATION; |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1089 | } |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1090 | if (BFD_UDP_ERROR_NONE != (err = bfd_udp4_verify_transport (ip4, udp, bs))) |
| 1091 | { |
| 1092 | return err; |
| 1093 | } |
Neale Ranns | 0c50dbb | 2022-08-05 03:40:43 +0000 | [diff] [blame] | 1094 | err = bfd_error_to_udp (bfd_rpc_update_session (vm, bs->bs_idx, pkt)); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1095 | *bs_out = bs; |
Neale Ranns | 0c50dbb | 2022-08-05 03:40:43 +0000 | [diff] [blame] | 1096 | return err; |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1097 | } |
| 1098 | |
Klement Sekera | c5fccc0 | 2017-01-18 09:56:00 +0100 | [diff] [blame] | 1099 | static void |
Klement Sekera | aeeac3b | 2017-02-14 07:11:52 +0100 | [diff] [blame] | 1100 | bfd_udp6_find_headers (vlib_buffer_t * b, ip6_header_t ** ip6, |
| 1101 | udp_header_t ** udp) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1102 | { |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame] | 1103 | /* sanity check first */ |
Damjan Marion | 072401e | 2017-07-13 18:53:27 +0200 | [diff] [blame] | 1104 | const i32 start = vnet_buffer (b)->l3_hdr_offset; |
Klement Sekera | b59f63b | 2022-02-14 20:19:28 +0000 | [diff] [blame] | 1105 | if (start < -(signed) sizeof (b->pre_data)) |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame] | 1106 | { |
| 1107 | BFD_ERR ("Start of ip header is before pre_data, ignoring"); |
| 1108 | *ip6 = NULL; |
| 1109 | *udp = NULL; |
| 1110 | return; |
| 1111 | } |
Klement Sekera | c5fccc0 | 2017-01-18 09:56:00 +0100 | [diff] [blame] | 1112 | *ip6 = (ip6_header_t *) (b->data + start); |
| 1113 | if ((u8 *) * ip6 > (u8 *) vlib_buffer_get_current (b)) |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame] | 1114 | { |
| 1115 | BFD_ERR ("Start of ip header is beyond current data, ignoring"); |
| 1116 | *ip6 = NULL; |
| 1117 | *udp = NULL; |
| 1118 | return; |
| 1119 | } |
Klement Sekera | 402ed31 | 2017-01-18 09:44:36 +0100 | [diff] [blame] | 1120 | if ((*ip6)->protocol != IP_PROTOCOL_UDP) |
| 1121 | { |
| 1122 | BFD_ERR ("Unexpected protocol in IPv6 header '%u', expected '%u' (== " |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 1123 | "IP_PROTOCOL_UDP)", (*ip6)->protocol, IP_PROTOCOL_UDP); |
Klement Sekera | 402ed31 | 2017-01-18 09:44:36 +0100 | [diff] [blame] | 1124 | *ip6 = NULL; |
| 1125 | *udp = NULL; |
| 1126 | return; |
| 1127 | } |
Klement Sekera | c5fccc0 | 2017-01-18 09:56:00 +0100 | [diff] [blame] | 1128 | *udp = (udp_header_t *) ((*ip6) + 1); |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame] | 1129 | } |
| 1130 | |
Neale Ranns | 5c7e579 | 2022-08-09 00:03:17 +0000 | [diff] [blame] | 1131 | static vl_counter_bfd_udp_enum_t |
| 1132 | bfd_udp6_verify_transport (const ip6_header_t *ip6, const udp_header_t *udp, |
| 1133 | const bfd_session_t *bs) |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame] | 1134 | { |
| 1135 | const bfd_udp_session_t *bus = &bs->udp; |
| 1136 | const bfd_udp_key_t *key = &bus->key; |
| 1137 | if (ip6->src_address.as_u64[0] != key->peer_addr.ip6.as_u64[0] && |
| 1138 | ip6->src_address.as_u64[1] != key->peer_addr.ip6.as_u64[1]) |
| 1139 | { |
Klement Sekera | c5fccc0 | 2017-01-18 09:56:00 +0100 | [diff] [blame] | 1140 | BFD_ERR ("IP src addr mismatch, got %U, expected %U", |
| 1141 | format_ip6_address, ip6, format_ip6_address, |
| 1142 | &key->peer_addr.ip6); |
Neale Ranns | 0c50dbb | 2022-08-05 03:40:43 +0000 | [diff] [blame] | 1143 | return BFD_UDP_ERROR_SRC_MISMATCH; |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame] | 1144 | } |
| 1145 | if (ip6->dst_address.as_u64[0] != key->local_addr.ip6.as_u64[0] && |
| 1146 | ip6->dst_address.as_u64[1] != key->local_addr.ip6.as_u64[1]) |
| 1147 | { |
Klement Sekera | c5fccc0 | 2017-01-18 09:56:00 +0100 | [diff] [blame] | 1148 | BFD_ERR ("IP dst addr mismatch, got %U, expected %U", |
| 1149 | format_ip6_address, ip6, format_ip6_address, |
| 1150 | &key->local_addr.ip6); |
Neale Ranns | 0c50dbb | 2022-08-05 03:40:43 +0000 | [diff] [blame] | 1151 | return BFD_UDP_ERROR_DST_MISMATCH; |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame] | 1152 | } |
| 1153 | const u8 expected_hop_limit = 255; |
| 1154 | if (ip6->hop_limit != expected_hop_limit) |
| 1155 | { |
| 1156 | BFD_ERR ("IPv6 unexpected hop-limit value %u, expected %u", |
Klement Sekera | c5fccc0 | 2017-01-18 09:56:00 +0100 | [diff] [blame] | 1157 | ip6->hop_limit, expected_hop_limit); |
Neale Ranns | 0c50dbb | 2022-08-05 03:40:43 +0000 | [diff] [blame] | 1158 | return BFD_UDP_ERROR_TTL; |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame] | 1159 | } |
Klement Sekera | 6f96649 | 2017-02-08 07:42:08 +0100 | [diff] [blame] | 1160 | if (clib_net_to_host_u16 (udp->src_port) < 49152) |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame] | 1161 | { |
| 1162 | BFD_ERR ("Invalid UDP src port %u, out of range <49152,65535>", |
Klement Sekera | c5fccc0 | 2017-01-18 09:56:00 +0100 | [diff] [blame] | 1163 | udp->src_port); |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame] | 1164 | } |
| 1165 | return BFD_UDP_ERROR_NONE; |
| 1166 | } |
| 1167 | |
Neale Ranns | 5c7e579 | 2022-08-09 00:03:17 +0000 | [diff] [blame] | 1168 | static vl_counter_bfd_udp_enum_t |
Klement Sekera | b59f63b | 2022-02-14 20:19:28 +0000 | [diff] [blame] | 1169 | bfd_udp6_scan (vlib_main_t *vm, vlib_buffer_t *b, bfd_session_t **bs_out) |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame] | 1170 | { |
| 1171 | const bfd_pkt_t *pkt = vlib_buffer_get_current (b); |
| 1172 | if (sizeof (*pkt) > b->current_length) |
| 1173 | { |
Klement Sekera | c5fccc0 | 2017-01-18 09:56:00 +0100 | [diff] [blame] | 1174 | BFD_ERR |
| 1175 | ("Payload size %d too small to hold bfd packet of minimum size %d", |
| 1176 | b->current_length, sizeof (*pkt)); |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame] | 1177 | return BFD_UDP_ERROR_BAD; |
| 1178 | } |
Klement Sekera | aeeac3b | 2017-02-14 07:11:52 +0100 | [diff] [blame] | 1179 | ip6_header_t *ip6; |
| 1180 | udp_header_t *udp; |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame] | 1181 | bfd_udp6_find_headers (b, &ip6, &udp); |
| 1182 | if (!ip6 || !udp) |
| 1183 | { |
| 1184 | BFD_ERR ("Couldn't find ip6 or udp header"); |
| 1185 | return BFD_UDP_ERROR_BAD; |
| 1186 | } |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 1187 | const u32 udp_payload_length = udp->length - sizeof (*udp); |
| 1188 | if (pkt->head.length > udp_payload_length) |
| 1189 | { |
| 1190 | BFD_ERR |
| 1191 | ("BFD packet length is larger than udp payload length (%u > %u)", |
| 1192 | pkt->head.length, udp_payload_length); |
| 1193 | return BFD_UDP_ERROR_BAD; |
| 1194 | } |
Neale Ranns | 5c7e579 | 2022-08-09 00:03:17 +0000 | [diff] [blame] | 1195 | vl_counter_bfd_udp_enum_t err; |
Neale Ranns | 0c50dbb | 2022-08-05 03:40:43 +0000 | [diff] [blame] | 1196 | if (BFD_UDP_ERROR_NONE != |
| 1197 | (err = bfd_error_to_udp (bfd_verify_pkt_common (pkt)))) |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame] | 1198 | { |
Neale Ranns | 0c50dbb | 2022-08-05 03:40:43 +0000 | [diff] [blame] | 1199 | return err; |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame] | 1200 | } |
| 1201 | bfd_session_t *bs = NULL; |
| 1202 | if (pkt->your_disc) |
| 1203 | { |
| 1204 | BFD_DBG ("Looking up BFD session using discriminator %u", |
Klement Sekera | c5fccc0 | 2017-01-18 09:56:00 +0100 | [diff] [blame] | 1205 | pkt->your_disc); |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame] | 1206 | bs = bfd_find_session_by_disc (bfd_udp_main.bfd_main, pkt->your_disc); |
| 1207 | } |
| 1208 | else |
| 1209 | { |
| 1210 | bfd_udp_key_t key; |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 1211 | clib_memset (&key, 0, sizeof (key)); |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame] | 1212 | key.sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX]; |
| 1213 | key.local_addr.ip6.as_u64[0] = ip6->dst_address.as_u64[0]; |
| 1214 | key.local_addr.ip6.as_u64[1] = ip6->dst_address.as_u64[1]; |
| 1215 | key.peer_addr.ip6.as_u64[0] = ip6->src_address.as_u64[0]; |
| 1216 | key.peer_addr.ip6.as_u64[1] = ip6->src_address.as_u64[1]; |
| 1217 | BFD_DBG ("Looking up BFD session using key (sw_if_index=%u, local=%U, " |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 1218 | "peer=%U)", |
| 1219 | key.sw_if_index, format_ip6_address, &key.local_addr, |
| 1220 | format_ip6_address, &key.peer_addr); |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame] | 1221 | bs = bfd_lookup_session (&bfd_udp_main, &key); |
| 1222 | } |
| 1223 | if (!bs) |
| 1224 | { |
| 1225 | BFD_ERR ("BFD session lookup failed - no session matches BFD pkt"); |
Neale Ranns | 0c50dbb | 2022-08-05 03:40:43 +0000 | [diff] [blame] | 1226 | return BFD_UDP_ERROR_NO_SESSION; |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame] | 1227 | } |
| 1228 | BFD_DBG ("BFD session found, bs_idx=%u", bs->bs_idx); |
Klement Sekera | a316744 | 2020-02-10 11:49:52 +0000 | [diff] [blame] | 1229 | if (!bfd_verify_pkt_auth (vm, pkt, b->current_length, bs)) |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame] | 1230 | { |
Klement Sekera | b17dd96 | 2017-01-09 07:43:48 +0100 | [diff] [blame] | 1231 | BFD_ERR ("Packet verification failed, dropping packet"); |
Neale Ranns | 0c50dbb | 2022-08-05 03:40:43 +0000 | [diff] [blame] | 1232 | return BFD_UDP_ERROR_FAILED_VERIFICATION; |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame] | 1233 | } |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame] | 1234 | if (BFD_UDP_ERROR_NONE != (err = bfd_udp6_verify_transport (ip6, udp, bs))) |
| 1235 | { |
| 1236 | return err; |
| 1237 | } |
Neale Ranns | 0c50dbb | 2022-08-05 03:40:43 +0000 | [diff] [blame] | 1238 | err = bfd_error_to_udp (bfd_rpc_update_session (vm, bs->bs_idx, pkt)); |
Klement Sekera | 46a87ad | 2017-01-02 08:22:23 +0100 | [diff] [blame] | 1239 | *bs_out = bs; |
Neale Ranns | 0c50dbb | 2022-08-05 03:40:43 +0000 | [diff] [blame] | 1240 | return err; |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1241 | } |
| 1242 | |
| 1243 | /* |
| 1244 | * Process a frame of bfd packets |
| 1245 | * Expect 1 packet / frame |
| 1246 | */ |
Klement Sekera | c5fccc0 | 2017-01-18 09:56:00 +0100 | [diff] [blame] | 1247 | static uword |
| 1248 | bfd_udp_input (vlib_main_t * vm, vlib_node_runtime_t * rt, |
| 1249 | vlib_frame_t * f, int is_ipv6) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1250 | { |
| 1251 | u32 n_left_from, *from; |
| 1252 | bfd_input_trace_t *t0; |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame] | 1253 | bfd_main_t *bm = &bfd_main; |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1254 | |
Klement Sekera | c5fccc0 | 2017-01-18 09:56:00 +0100 | [diff] [blame] | 1255 | from = vlib_frame_vector_args (f); /* array of buffer indices */ |
| 1256 | n_left_from = f->n_vectors; /* number of buffer indices */ |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1257 | |
| 1258 | while (n_left_from > 0) |
| 1259 | { |
| 1260 | u32 bi0; |
| 1261 | vlib_buffer_t *b0; |
| 1262 | u32 next0, error0; |
| 1263 | |
| 1264 | bi0 = from[0]; |
| 1265 | b0 = vlib_get_buffer (vm, bi0); |
| 1266 | |
| 1267 | bfd_session_t *bs = NULL; |
| 1268 | |
| 1269 | /* If this pkt is traced, snapshot the data */ |
| 1270 | if (b0->flags & VLIB_BUFFER_IS_TRACED) |
Klement Sekera | c5fccc0 | 2017-01-18 09:56:00 +0100 | [diff] [blame] | 1271 | { |
Klement Sekera | b59f63b | 2022-02-14 20:19:28 +0000 | [diff] [blame] | 1272 | u64 len; |
Klement Sekera | c5fccc0 | 2017-01-18 09:56:00 +0100 | [diff] [blame] | 1273 | t0 = vlib_add_trace (vm, rt, b0, sizeof (*t0)); |
| 1274 | len = (b0->current_length < sizeof (t0->data)) ? b0->current_length |
| 1275 | : sizeof (t0->data); |
| 1276 | t0->len = len; |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 1277 | clib_memcpy_fast (t0->data, vlib_buffer_get_current (b0), len); |
Klement Sekera | c5fccc0 | 2017-01-18 09:56:00 +0100 | [diff] [blame] | 1278 | } |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1279 | |
| 1280 | /* scan this bfd pkt. error0 is the counter index to bmp */ |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame] | 1281 | bfd_lock (bm); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1282 | if (is_ipv6) |
Klement Sekera | c5fccc0 | 2017-01-18 09:56:00 +0100 | [diff] [blame] | 1283 | { |
Klement Sekera | b59f63b | 2022-02-14 20:19:28 +0000 | [diff] [blame] | 1284 | error0 = bfd_udp6_scan (vm, b0, &bs); |
Klement Sekera | c5fccc0 | 2017-01-18 09:56:00 +0100 | [diff] [blame] | 1285 | } |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1286 | else |
Klement Sekera | c5fccc0 | 2017-01-18 09:56:00 +0100 | [diff] [blame] | 1287 | { |
Klement Sekera | b59f63b | 2022-02-14 20:19:28 +0000 | [diff] [blame] | 1288 | error0 = bfd_udp4_scan (vm, b0, &bs); |
Klement Sekera | c5fccc0 | 2017-01-18 09:56:00 +0100 | [diff] [blame] | 1289 | } |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1290 | b0->error = rt->errors[error0]; |
| 1291 | |
| 1292 | next0 = BFD_UDP_INPUT_NEXT_NORMAL; |
| 1293 | if (BFD_UDP_ERROR_NONE == error0) |
Klement Sekera | c5fccc0 | 2017-01-18 09:56:00 +0100 | [diff] [blame] | 1294 | { |
Klement Sekera | cdaf0d8 | 2022-02-14 20:20:22 +0000 | [diff] [blame] | 1295 | vlib_increment_combined_counter ( |
| 1296 | &bm->rx_counter, vm->thread_index, bs->bs_idx, 1, |
| 1297 | vlib_buffer_length_in_chain (vm, b0)); |
Klement Sekera | 402ed31 | 2017-01-18 09:44:36 +0100 | [diff] [blame] | 1298 | /* |
| 1299 | * if everything went fine, check for poll bit, if present, re-use |
| 1300 | * the buffer and based on (now updated) session parameters, send |
| 1301 | * the final packet back |
| 1302 | */ |
Klement Sekera | c5fccc0 | 2017-01-18 09:56:00 +0100 | [diff] [blame] | 1303 | const bfd_pkt_t *pkt = vlib_buffer_get_current (b0); |
| 1304 | if (bfd_pkt_get_poll (pkt)) |
| 1305 | { |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 1306 | b0->current_data = 0; |
| 1307 | b0->current_length = 0; |
Klement Sekera | b59f63b | 2022-02-14 20:19:28 +0000 | [diff] [blame] | 1308 | bfd_init_final_control_frame (vm, b0, bs); |
Klement Sekera | c5fccc0 | 2017-01-18 09:56:00 +0100 | [diff] [blame] | 1309 | if (is_ipv6) |
| 1310 | { |
| 1311 | vlib_node_increment_counter (vm, bfd_udp6_input_node.index, |
Paul Atkins | 0d03284 | 2022-03-24 11:26:16 +0000 | [diff] [blame] | 1312 | error0, 1); |
Klement Sekera | c5fccc0 | 2017-01-18 09:56:00 +0100 | [diff] [blame] | 1313 | } |
| 1314 | else |
| 1315 | { |
| 1316 | vlib_node_increment_counter (vm, bfd_udp4_input_node.index, |
Paul Atkins | 0d03284 | 2022-03-24 11:26:16 +0000 | [diff] [blame] | 1317 | error0, 1); |
Klement Sekera | c5fccc0 | 2017-01-18 09:56:00 +0100 | [diff] [blame] | 1318 | } |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 1319 | const bfd_udp_session_t *bus = &bs->udp; |
| 1320 | ip_adjacency_t *adj = adj_get (bus->adj_index); |
| 1321 | switch (adj->lookup_next_index) |
| 1322 | { |
| 1323 | case IP_LOOKUP_NEXT_ARP: |
| 1324 | next0 = BFD_UDP_INPUT_NEXT_REPLY_ARP; |
| 1325 | break; |
| 1326 | case IP_LOOKUP_NEXT_REWRITE: |
| 1327 | next0 = BFD_UDP_INPUT_NEXT_REPLY_REWRITE; |
| 1328 | break; |
Matthew G Smith | 2f26014 | 2019-05-22 14:49:24 -0500 | [diff] [blame] | 1329 | case IP_LOOKUP_NEXT_MIDCHAIN: |
| 1330 | next0 = BFD_UDP_INPUT_NEXT_REPLY_MIDCHAIN; |
| 1331 | break; |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 1332 | default: |
| 1333 | /* drop */ |
| 1334 | break; |
| 1335 | } |
Klement Sekera | c5fccc0 | 2017-01-18 09:56:00 +0100 | [diff] [blame] | 1336 | } |
| 1337 | } |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame] | 1338 | bfd_unlock (bm); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1339 | vlib_set_next_frame_buffer (vm, rt, next0, bi0); |
| 1340 | |
| 1341 | from += 1; |
| 1342 | n_left_from -= 1; |
| 1343 | } |
| 1344 | |
| 1345 | return f->n_vectors; |
| 1346 | } |
| 1347 | |
Klement Sekera | c5fccc0 | 2017-01-18 09:56:00 +0100 | [diff] [blame] | 1348 | static uword |
| 1349 | bfd_udp4_input (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1350 | { |
| 1351 | return bfd_udp_input (vm, rt, f, 0); |
| 1352 | } |
| 1353 | |
| 1354 | /* |
| 1355 | * bfd input graph node declaration |
| 1356 | */ |
| 1357 | /* *INDENT-OFF* */ |
| 1358 | VLIB_REGISTER_NODE (bfd_udp4_input_node, static) = { |
| 1359 | .function = bfd_udp4_input, |
| 1360 | .name = "bfd-udp4-input", |
| 1361 | .vector_size = sizeof (u32), |
| 1362 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 1363 | |
| 1364 | .n_errors = BFD_UDP_N_ERROR, |
Neale Ranns | 5c7e579 | 2022-08-09 00:03:17 +0000 | [diff] [blame] | 1365 | .error_counters = bfd_udp_error_counters, |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1366 | |
| 1367 | .format_trace = bfd_input_format_trace, |
| 1368 | |
| 1369 | .n_next_nodes = BFD_UDP_INPUT_N_NEXT, |
| 1370 | .next_nodes = |
| 1371 | { |
| 1372 | [BFD_UDP_INPUT_NEXT_NORMAL] = "error-drop", |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 1373 | [BFD_UDP_INPUT_NEXT_REPLY_ARP] = "ip4-arp", |
| 1374 | [BFD_UDP_INPUT_NEXT_REPLY_REWRITE] = "ip4-lookup", |
Matthew G Smith | 2f26014 | 2019-05-22 14:49:24 -0500 | [diff] [blame] | 1375 | [BFD_UDP_INPUT_NEXT_REPLY_MIDCHAIN] = "ip4-midchain", |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1376 | }, |
| 1377 | }; |
| 1378 | /* *INDENT-ON* */ |
| 1379 | |
Klement Sekera | c5fccc0 | 2017-01-18 09:56:00 +0100 | [diff] [blame] | 1380 | static uword |
| 1381 | bfd_udp6_input (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1382 | { |
| 1383 | return bfd_udp_input (vm, rt, f, 1); |
| 1384 | } |
| 1385 | |
| 1386 | /* *INDENT-OFF* */ |
| 1387 | VLIB_REGISTER_NODE (bfd_udp6_input_node, static) = { |
| 1388 | .function = bfd_udp6_input, |
| 1389 | .name = "bfd-udp6-input", |
| 1390 | .vector_size = sizeof (u32), |
| 1391 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 1392 | |
| 1393 | .n_errors = BFD_UDP_N_ERROR, |
Neale Ranns | 5c7e579 | 2022-08-09 00:03:17 +0000 | [diff] [blame] | 1394 | .error_counters = bfd_udp_error_counters, |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1395 | |
| 1396 | .format_trace = bfd_input_format_trace, |
| 1397 | |
| 1398 | .n_next_nodes = BFD_UDP_INPUT_N_NEXT, |
| 1399 | .next_nodes = |
| 1400 | { |
| 1401 | [BFD_UDP_INPUT_NEXT_NORMAL] = "error-drop", |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 1402 | [BFD_UDP_INPUT_NEXT_REPLY_ARP] = "ip6-discover-neighbor", |
| 1403 | [BFD_UDP_INPUT_NEXT_REPLY_REWRITE] = "ip6-lookup", |
Matthew G Smith | 2f26014 | 2019-05-22 14:49:24 -0500 | [diff] [blame] | 1404 | [BFD_UDP_INPUT_NEXT_REPLY_MIDCHAIN] = "ip6-midchain", |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1405 | }, |
| 1406 | }; |
| 1407 | /* *INDENT-ON* */ |
| 1408 | |
Klement Sekera | aeeac3b | 2017-02-14 07:11:52 +0100 | [diff] [blame] | 1409 | /* |
| 1410 | * Process a frame of bfd echo packets |
| 1411 | * Expect 1 packet / frame |
| 1412 | */ |
| 1413 | static uword |
| 1414 | bfd_udp_echo_input (vlib_main_t * vm, vlib_node_runtime_t * rt, |
| 1415 | vlib_frame_t * f, int is_ipv6) |
| 1416 | { |
| 1417 | u32 n_left_from, *from; |
| 1418 | bfd_input_trace_t *t0; |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame] | 1419 | bfd_main_t *bm = &bfd_main; |
Klement Sekera | aeeac3b | 2017-02-14 07:11:52 +0100 | [diff] [blame] | 1420 | |
| 1421 | from = vlib_frame_vector_args (f); /* array of buffer indices */ |
| 1422 | n_left_from = f->n_vectors; /* number of buffer indices */ |
| 1423 | |
| 1424 | while (n_left_from > 0) |
| 1425 | { |
| 1426 | u32 bi0; |
| 1427 | vlib_buffer_t *b0; |
| 1428 | u32 next0; |
| 1429 | |
| 1430 | bi0 = from[0]; |
| 1431 | b0 = vlib_get_buffer (vm, bi0); |
| 1432 | |
| 1433 | /* If this pkt is traced, snapshot the data */ |
| 1434 | if (b0->flags & VLIB_BUFFER_IS_TRACED) |
| 1435 | { |
Klement Sekera | b59f63b | 2022-02-14 20:19:28 +0000 | [diff] [blame] | 1436 | u64 len; |
Klement Sekera | aeeac3b | 2017-02-14 07:11:52 +0100 | [diff] [blame] | 1437 | t0 = vlib_add_trace (vm, rt, b0, sizeof (*t0)); |
| 1438 | len = (b0->current_length < sizeof (t0->data)) ? b0->current_length |
| 1439 | : sizeof (t0->data); |
| 1440 | t0->len = len; |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 1441 | clib_memcpy_fast (t0->data, vlib_buffer_get_current (b0), len); |
Klement Sekera | aeeac3b | 2017-02-14 07:11:52 +0100 | [diff] [blame] | 1442 | } |
| 1443 | |
Klement Sekera | cdaf0d8 | 2022-02-14 20:20:22 +0000 | [diff] [blame] | 1444 | bfd_session_t *bs = NULL; |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame] | 1445 | bfd_lock (bm); |
Klement Sekera | cdaf0d8 | 2022-02-14 20:20:22 +0000 | [diff] [blame] | 1446 | if ((bs = bfd_consume_echo_pkt (vm, bfd_udp_main.bfd_main, b0))) |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 1447 | { |
| 1448 | b0->error = rt->errors[BFD_UDP_ERROR_NONE]; |
Matthew G Smith | 2f26014 | 2019-05-22 14:49:24 -0500 | [diff] [blame] | 1449 | next0 = BFD_UDP_ECHO_INPUT_NEXT_NORMAL; |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 1450 | } |
| 1451 | else |
Klement Sekera | aeeac3b | 2017-02-14 07:11:52 +0100 | [diff] [blame] | 1452 | { |
| 1453 | /* loop back the packet */ |
| 1454 | b0->error = rt->errors[BFD_UDP_ERROR_NONE]; |
| 1455 | if (is_ipv6) |
| 1456 | { |
| 1457 | vlib_node_increment_counter (vm, bfd_udp_echo6_input_node.index, |
Paul Atkins | 0d03284 | 2022-03-24 11:26:16 +0000 | [diff] [blame] | 1458 | BFD_UDP_ERROR_NONE, 1); |
Klement Sekera | aeeac3b | 2017-02-14 07:11:52 +0100 | [diff] [blame] | 1459 | } |
| 1460 | else |
| 1461 | { |
| 1462 | vlib_node_increment_counter (vm, bfd_udp_echo4_input_node.index, |
Paul Atkins | 0d03284 | 2022-03-24 11:26:16 +0000 | [diff] [blame] | 1463 | BFD_UDP_ERROR_NONE, 1); |
Klement Sekera | aeeac3b | 2017-02-14 07:11:52 +0100 | [diff] [blame] | 1464 | } |
Matthew G Smith | 2f26014 | 2019-05-22 14:49:24 -0500 | [diff] [blame] | 1465 | next0 = BFD_UDP_ECHO_INPUT_NEXT_REPLY_REWRITE; |
Klement Sekera | aeeac3b | 2017-02-14 07:11:52 +0100 | [diff] [blame] | 1466 | } |
Klement Sekera | aeeac3b | 2017-02-14 07:11:52 +0100 | [diff] [blame] | 1467 | |
Dave Barach | 1e3417f | 2018-07-25 08:30:27 -0400 | [diff] [blame] | 1468 | bfd_unlock (bm); |
Klement Sekera | cdaf0d8 | 2022-02-14 20:20:22 +0000 | [diff] [blame] | 1469 | |
| 1470 | if (bs) |
| 1471 | { |
| 1472 | vlib_increment_combined_counter ( |
| 1473 | &bm->rx_echo_counter, vm->thread_index, bs->bs_idx, 1, |
| 1474 | vlib_buffer_length_in_chain (vm, b0)); |
| 1475 | } |
| 1476 | |
Klement Sekera | aeeac3b | 2017-02-14 07:11:52 +0100 | [diff] [blame] | 1477 | vlib_set_next_frame_buffer (vm, rt, next0, bi0); |
| 1478 | |
| 1479 | from += 1; |
| 1480 | n_left_from -= 1; |
| 1481 | } |
| 1482 | |
| 1483 | return f->n_vectors; |
| 1484 | } |
| 1485 | |
| 1486 | static uword |
| 1487 | bfd_udp_echo4_input (vlib_main_t * vm, vlib_node_runtime_t * rt, |
| 1488 | vlib_frame_t * f) |
| 1489 | { |
| 1490 | return bfd_udp_echo_input (vm, rt, f, 0); |
| 1491 | } |
| 1492 | |
| 1493 | u8 * |
| 1494 | bfd_echo_input_format_trace (u8 * s, va_list * args) |
| 1495 | { |
| 1496 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 1497 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
| 1498 | const bfd_udp_echo_input_trace_t *t = |
| 1499 | va_arg (*args, bfd_udp_echo_input_trace_t *); |
| 1500 | if (t->len > STRUCT_SIZE_OF (bfd_pkt_t, head)) |
| 1501 | { |
| 1502 | s = format (s, "BFD ECHO:\n"); |
| 1503 | s = format (s, " data: %U", format_hexdump, t->data, t->len); |
| 1504 | } |
| 1505 | |
| 1506 | return s; |
| 1507 | } |
| 1508 | |
| 1509 | /* |
| 1510 | * bfd input graph node declaration |
| 1511 | */ |
| 1512 | /* *INDENT-OFF* */ |
| 1513 | VLIB_REGISTER_NODE (bfd_udp_echo4_input_node, static) = { |
| 1514 | .function = bfd_udp_echo4_input, |
| 1515 | .name = "bfd-udp-echo4-input", |
| 1516 | .vector_size = sizeof (u32), |
| 1517 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 1518 | |
Neale Ranns | 5c7e579 | 2022-08-09 00:03:17 +0000 | [diff] [blame] | 1519 | .n_errors = BFD_UDP_N_ERROR, |
| 1520 | .error_counters = bfd_udp_error_counters, |
Klement Sekera | aeeac3b | 2017-02-14 07:11:52 +0100 | [diff] [blame] | 1521 | |
| 1522 | .format_trace = bfd_echo_input_format_trace, |
| 1523 | |
Matthew G Smith | 2f26014 | 2019-05-22 14:49:24 -0500 | [diff] [blame] | 1524 | .n_next_nodes = BFD_UDP_ECHO_INPUT_N_NEXT, |
Klement Sekera | aeeac3b | 2017-02-14 07:11:52 +0100 | [diff] [blame] | 1525 | .next_nodes = |
| 1526 | { |
Matthew G Smith | 2f26014 | 2019-05-22 14:49:24 -0500 | [diff] [blame] | 1527 | [BFD_UDP_ECHO_INPUT_NEXT_NORMAL] = "error-drop", |
| 1528 | [BFD_UDP_ECHO_INPUT_NEXT_REPLY_ARP] = "ip4-arp", |
| 1529 | [BFD_UDP_ECHO_INPUT_NEXT_REPLY_REWRITE] = "ip4-lookup", |
Klement Sekera | aeeac3b | 2017-02-14 07:11:52 +0100 | [diff] [blame] | 1530 | }, |
| 1531 | }; |
| 1532 | /* *INDENT-ON* */ |
| 1533 | |
| 1534 | static uword |
| 1535 | bfd_udp_echo6_input (vlib_main_t * vm, vlib_node_runtime_t * rt, |
| 1536 | vlib_frame_t * f) |
| 1537 | { |
| 1538 | return bfd_udp_echo_input (vm, rt, f, 1); |
| 1539 | } |
| 1540 | |
| 1541 | /* *INDENT-OFF* */ |
| 1542 | VLIB_REGISTER_NODE (bfd_udp_echo6_input_node, static) = { |
| 1543 | .function = bfd_udp_echo6_input, |
| 1544 | .name = "bfd-udp-echo6-input", |
| 1545 | .vector_size = sizeof (u32), |
| 1546 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 1547 | |
Neale Ranns | 5c7e579 | 2022-08-09 00:03:17 +0000 | [diff] [blame] | 1548 | .n_errors = BFD_UDP_N_ERROR, |
| 1549 | .error_counters = bfd_udp_error_counters, |
Klement Sekera | aeeac3b | 2017-02-14 07:11:52 +0100 | [diff] [blame] | 1550 | |
| 1551 | .format_trace = bfd_echo_input_format_trace, |
| 1552 | |
Matthew G Smith | 2f26014 | 2019-05-22 14:49:24 -0500 | [diff] [blame] | 1553 | .n_next_nodes = BFD_UDP_ECHO_INPUT_N_NEXT, |
Klement Sekera | aeeac3b | 2017-02-14 07:11:52 +0100 | [diff] [blame] | 1554 | .next_nodes = |
| 1555 | { |
Matthew G Smith | 2f26014 | 2019-05-22 14:49:24 -0500 | [diff] [blame] | 1556 | [BFD_UDP_ECHO_INPUT_NEXT_NORMAL] = "error-drop", |
| 1557 | [BFD_UDP_ECHO_INPUT_NEXT_REPLY_ARP] = "ip6-discover-neighbor", |
| 1558 | [BFD_UDP_ECHO_INPUT_NEXT_REPLY_REWRITE] = "ip6-lookup", |
Klement Sekera | aeeac3b | 2017-02-14 07:11:52 +0100 | [diff] [blame] | 1559 | }, |
| 1560 | }; |
| 1561 | |
| 1562 | /* *INDENT-ON* */ |
| 1563 | |
Klement Sekera | c5fccc0 | 2017-01-18 09:56:00 +0100 | [diff] [blame] | 1564 | static clib_error_t * |
Klement Sekera | b59f63b | 2022-02-14 20:19:28 +0000 | [diff] [blame] | 1565 | bfd_udp_sw_if_add_del (CLIB_UNUSED (vnet_main_t *vnm), u32 sw_if_index, |
| 1566 | u32 is_create) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1567 | { |
Klement Sekera | b59f63b | 2022-02-14 20:19:28 +0000 | [diff] [blame] | 1568 | u32 *to_be_freed = NULL; |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 1569 | bfd_udp_main_t *bum = &bfd_udp_main; |
Klement Sekera | f3bcdbf | 2017-05-02 07:38:01 +0200 | [diff] [blame] | 1570 | BFD_DBG ("sw_if_add_del called, sw_if_index=%u, is_create=%u", sw_if_index, |
| 1571 | is_create); |
| 1572 | if (!is_create) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1573 | { |
Klement Sekera | f3bcdbf | 2017-05-02 07:38:01 +0200 | [diff] [blame] | 1574 | bfd_session_t *bs; |
Klement Sekera | b59f63b | 2022-02-14 20:19:28 +0000 | [diff] [blame] | 1575 | pool_foreach (bs, bum->bfd_main->sessions) |
| 1576 | { |
| 1577 | if (bs->transport != BFD_TRANSPORT_UDP4 && |
| 1578 | bs->transport != BFD_TRANSPORT_UDP6) |
| 1579 | { |
| 1580 | continue; |
| 1581 | } |
| 1582 | if (bs->udp.key.sw_if_index != sw_if_index) |
| 1583 | { |
| 1584 | continue; |
| 1585 | } |
| 1586 | vec_add1 (to_be_freed, bs->bs_idx); |
| 1587 | } |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1588 | } |
Klement Sekera | b59f63b | 2022-02-14 20:19:28 +0000 | [diff] [blame] | 1589 | u32 *bs_idx; |
| 1590 | vec_foreach (bs_idx, to_be_freed) |
| 1591 | { |
| 1592 | bfd_session_t *bs = pool_elt_at_index (bum->bfd_main->sessions, *bs_idx); |
| 1593 | vlib_log_notice (bum->log_class, |
| 1594 | "removal of sw_if_index=%u forces removal of bfd " |
| 1595 | "session with bs_idx=%u", |
| 1596 | sw_if_index, bs->bs_idx); |
| 1597 | bfd_session_set_flags (vlib_get_main (), bs, 0); |
| 1598 | bfd_udp_del_session_internal (vlib_get_main (), bs); |
| 1599 | } |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1600 | return 0; |
| 1601 | } |
| 1602 | |
Klement Sekera | f3bcdbf | 2017-05-02 07:38:01 +0200 | [diff] [blame] | 1603 | VNET_SW_INTERFACE_ADD_DEL_FUNCTION (bfd_udp_sw_if_add_del); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1604 | |
Klement Sekera | cdaf0d8 | 2022-02-14 20:20:22 +0000 | [diff] [blame] | 1605 | clib_error_t * |
| 1606 | bfd_udp_stats_init (bfd_udp_main_t *bum) |
| 1607 | { |
| 1608 | const char *name4 = "/bfd/udp4/sessions"; |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 1609 | bum->udp4_sessions_count_stat_seg_entry = vlib_stats_add_gauge ("%s", name4); |
Klement Sekera | cdaf0d8 | 2022-02-14 20:20:22 +0000 | [diff] [blame] | 1610 | |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 1611 | vlib_stats_set_gauge (bum->udp4_sessions_count_stat_seg_entry, 0); |
Klement Sekera | cdaf0d8 | 2022-02-14 20:20:22 +0000 | [diff] [blame] | 1612 | if (~0 == bum->udp4_sessions_count_stat_seg_entry) |
| 1613 | { |
| 1614 | return clib_error_return ( |
| 1615 | 0, "Could not create stat segment entry for %s", name4); |
| 1616 | } |
| 1617 | const char *name6 = "/bfd/udp6/sessions"; |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 1618 | bum->udp6_sessions_count_stat_seg_entry = vlib_stats_add_gauge ("%s", name6); |
Klement Sekera | cdaf0d8 | 2022-02-14 20:20:22 +0000 | [diff] [blame] | 1619 | |
Damjan Marion | 8973b07 | 2022-03-01 15:51:18 +0100 | [diff] [blame] | 1620 | vlib_stats_set_gauge (bum->udp6_sessions_count_stat_seg_entry, 0); |
Klement Sekera | cdaf0d8 | 2022-02-14 20:20:22 +0000 | [diff] [blame] | 1621 | if (~0 == bum->udp6_sessions_count_stat_seg_entry) |
| 1622 | { |
| 1623 | return clib_error_return ( |
| 1624 | 0, "Could not create stat segment entry for %s", name6); |
| 1625 | } |
| 1626 | |
| 1627 | return 0; |
| 1628 | } |
| 1629 | |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1630 | /* |
| 1631 | * setup function |
| 1632 | */ |
Klement Sekera | c5fccc0 | 2017-01-18 09:56:00 +0100 | [diff] [blame] | 1633 | static clib_error_t * |
| 1634 | bfd_udp_init (vlib_main_t * vm) |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1635 | { |
Klement Sekera | 88aec65 | 2019-03-11 14:04:19 +0100 | [diff] [blame] | 1636 | bfd_udp_main.udp4_sessions_count = 0; |
| 1637 | bfd_udp_main.udp6_sessions_count = 0; |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1638 | mhash_init (&bfd_udp_main.bfd_session_idx_by_bfd_key, sizeof (uword), |
Klement Sekera | c5fccc0 | 2017-01-18 09:56:00 +0100 | [diff] [blame] | 1639 | sizeof (bfd_udp_key_t)); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1640 | bfd_udp_main.bfd_main = &bfd_main; |
Klement Sekera | 239790f | 2017-02-16 10:53:53 +0100 | [diff] [blame] | 1641 | bfd_udp_main.vnet_main = vnet_get_main (); |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 1642 | vlib_node_t *node = vlib_get_node_by_name (vm, (u8 *) "ip4-arp"); |
| 1643 | ASSERT (node); |
| 1644 | bfd_udp_main.ip4_arp_idx = node->index; |
| 1645 | node = vlib_get_node_by_name (vm, (u8 *) "ip6-discover-neighbor"); |
| 1646 | ASSERT (node); |
| 1647 | bfd_udp_main.ip6_ndp_idx = node->index; |
| 1648 | node = vlib_get_node_by_name (vm, (u8 *) "ip4-rewrite"); |
| 1649 | ASSERT (node); |
| 1650 | bfd_udp_main.ip4_rewrite_idx = node->index; |
| 1651 | node = vlib_get_node_by_name (vm, (u8 *) "ip6-rewrite"); |
| 1652 | ASSERT (node); |
| 1653 | bfd_udp_main.ip6_rewrite_idx = node->index; |
Matthew G Smith | 2f26014 | 2019-05-22 14:49:24 -0500 | [diff] [blame] | 1654 | node = vlib_get_node_by_name (vm, (u8 *) "ip4-midchain"); |
| 1655 | ASSERT (node); |
| 1656 | bfd_udp_main.ip4_midchain_idx = node->index; |
| 1657 | node = vlib_get_node_by_name (vm, (u8 *) "ip6-midchain"); |
| 1658 | ASSERT (node); |
| 1659 | bfd_udp_main.ip6_midchain_idx = node->index; |
Klement Sekera | e50e856 | 2017-04-04 16:19:48 +0200 | [diff] [blame] | 1660 | |
Klement Sekera | cdaf0d8 | 2022-02-14 20:20:22 +0000 | [diff] [blame] | 1661 | bfd_udp_stats_init (&bfd_udp_main); |
| 1662 | |
Damjan Marion | 07a3857 | 2018-01-21 06:44:18 -0800 | [diff] [blame] | 1663 | bfd_udp_main.log_class = vlib_log_register_class ("bfd", "udp"); |
| 1664 | vlib_log_debug (bfd_udp_main.log_class, "initialized"); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1665 | return 0; |
| 1666 | } |
| 1667 | |
| 1668 | VLIB_INIT_FUNCTION (bfd_udp_init); |
Klement Sekera | c5fccc0 | 2017-01-18 09:56:00 +0100 | [diff] [blame] | 1669 | |
| 1670 | /* |
| 1671 | * fd.io coding-style-patch-verification: ON |
| 1672 | * |
| 1673 | * Local Variables: |
| 1674 | * eval: (c-set-style "gnu") |
| 1675 | * End: |
| 1676 | */ |