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