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