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