Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 1 | #include <vppinfra/types.h> |
| 2 | #include <vlibmemory/api.h> |
| 3 | #include <vlib/vlib.h> |
| 4 | #include <vlib/buffer.h> |
| 5 | #include <vnet/ip/format.h> |
| 6 | #include <vnet/ethernet/packet.h> |
| 7 | #include <vnet/ip/udp_packet.h> |
| 8 | #include <vnet/ip/lookup.h> |
| 9 | #include <vnet/ip/icmp46_packet.h> |
| 10 | #include <vnet/ip/ip4.h> |
| 11 | #include <vnet/ip/ip6.h> |
| 12 | #include <vnet/ip/udp.h> |
| 13 | #include <vnet/ip/ip6_packet.h> |
| 14 | #include <vnet/adj/adj.h> |
| 15 | #include <vnet/adj/adj_nbr.h> |
| 16 | #include <vnet/bfd/bfd_debug.h> |
| 17 | #include <vnet/bfd/bfd_udp.h> |
| 18 | #include <vnet/bfd/bfd_main.h> |
| 19 | #include <vnet/bfd/bfd_api.h> |
| 20 | |
| 21 | typedef struct |
| 22 | { |
| 23 | bfd_main_t *bfd_main; |
| 24 | /* hashmap - bfd session index by bfd key - used for CLI/API lookup, where |
| 25 | * discriminator is unknown */ |
| 26 | mhash_t bfd_session_idx_by_bfd_key; |
| 27 | } bfd_udp_main_t; |
| 28 | |
Klement Sekera | e4504c6 | 2016-12-08 10:16:41 +0100 | [diff] [blame] | 29 | static vlib_node_registration_t bfd_udp4_input_node; |
| 30 | static vlib_node_registration_t bfd_udp6_input_node; |
| 31 | |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 32 | bfd_udp_main_t bfd_udp_main; |
| 33 | |
| 34 | void bfd_udp_transport_to_buffer (vlib_main_t *vm, vlib_buffer_t *b, |
| 35 | bfd_udp_session_t *bus) |
| 36 | { |
| 37 | udp_header_t *udp; |
| 38 | u16 udp_length, ip_length; |
| 39 | bfd_udp_key_t *key = &bus->key; |
| 40 | |
| 41 | b->flags |= VNET_BUFFER_LOCALLY_ORIGINATED; |
| 42 | if (ip46_address_is_ip4 (&key->local_addr)) |
| 43 | { |
| 44 | ip4_header_t *ip4; |
| 45 | const size_t data_size = sizeof (*ip4) + sizeof (*udp); |
| 46 | vlib_buffer_advance (b, -data_size); |
| 47 | ip4 = vlib_buffer_get_current (b); |
| 48 | udp = (udp_header_t *)(ip4 + 1); |
| 49 | memset (ip4, 0, data_size); |
| 50 | ip4->ip_version_and_header_length = 0x45; |
| 51 | ip4->ttl = 255; |
| 52 | ip4->protocol = IP_PROTOCOL_UDP; |
| 53 | ip4->src_address.as_u32 = key->local_addr.ip4.as_u32; |
| 54 | ip4->dst_address.as_u32 = key->peer_addr.ip4.as_u32; |
| 55 | |
| 56 | udp->src_port = clib_host_to_net_u16 (50000); /* FIXME */ |
| 57 | udp->dst_port = clib_host_to_net_u16 (UDP_DST_PORT_bfd4); |
| 58 | |
| 59 | /* fix ip length, checksum and udp length */ |
| 60 | ip_length = vlib_buffer_length_in_chain (vm, b); |
| 61 | |
| 62 | ip4->length = clib_host_to_net_u16 (ip_length); |
| 63 | ip4->checksum = ip4_header_checksum (ip4); |
| 64 | |
| 65 | udp_length = ip_length - (sizeof (*ip4)); |
| 66 | udp->length = clib_host_to_net_u16 (udp_length); |
| 67 | } |
| 68 | else |
| 69 | { |
| 70 | BFD_ERR ("not implemented"); |
| 71 | abort (); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | void bfd_add_udp_transport (vlib_main_t *vm, vlib_buffer_t *b, |
| 76 | bfd_udp_session_t *bus) |
| 77 | { |
| 78 | vnet_buffer (b)->ip.adj_index[VLIB_RX] = bus->adj_index; |
| 79 | vnet_buffer (b)->ip.adj_index[VLIB_TX] = bus->adj_index; |
| 80 | bfd_udp_transport_to_buffer (vm, b, bus); |
| 81 | } |
| 82 | |
| 83 | static bfd_session_t *bfd_lookup_session (bfd_udp_main_t *bum, |
| 84 | const bfd_udp_key_t *key) |
| 85 | { |
| 86 | uword *p = mhash_get (&bum->bfd_session_idx_by_bfd_key, key); |
| 87 | if (p) |
| 88 | { |
| 89 | return bfd_find_session_by_idx (bum->bfd_main, *p); |
| 90 | } |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | static vnet_api_error_t |
| 95 | bfd_udp_add_session_internal (bfd_udp_main_t *bum, u32 sw_if_index, |
| 96 | u32 desired_min_tx_us, u32 required_min_rx_us, |
| 97 | u8 detect_mult, const ip46_address_t *local_addr, |
| 98 | const ip46_address_t *peer_addr) |
| 99 | { |
| 100 | vnet_sw_interface_t *sw_if = |
| 101 | vnet_get_sw_interface (vnet_get_main (), sw_if_index); |
| 102 | /* get a pool entry and if we end up not needing it, give it back */ |
| 103 | bfd_transport_t t = BFD_TRANSPORT_UDP4; |
| 104 | if (!ip46_address_is_ip4 (local_addr)) |
| 105 | { |
| 106 | t = BFD_TRANSPORT_UDP6; |
| 107 | } |
| 108 | bfd_session_t *bs = bfd_get_session (bum->bfd_main, t); |
| 109 | bfd_udp_session_t *bus = &bs->udp; |
| 110 | memset (bus, 0, sizeof (*bus)); |
| 111 | bfd_udp_key_t *key = &bus->key; |
| 112 | key->sw_if_index = sw_if->sw_if_index; |
| 113 | key->local_addr.as_u64[0] = local_addr->as_u64[0]; |
| 114 | key->local_addr.as_u64[1] = local_addr->as_u64[1]; |
| 115 | key->peer_addr.as_u64[0] = peer_addr->as_u64[0]; |
| 116 | key->peer_addr.as_u64[1] = peer_addr->as_u64[1]; |
| 117 | const bfd_session_t *tmp = bfd_lookup_session (bum, key); |
| 118 | if (tmp) |
| 119 | { |
| 120 | BFD_ERR ("duplicate bfd-udp session, existing bs_idx=%d", tmp->bs_idx); |
| 121 | bfd_put_session (bum->bfd_main, bs); |
| 122 | return VNET_API_ERROR_BFD_EEXIST; |
| 123 | } |
| 124 | key->sw_if_index = sw_if->sw_if_index; |
| 125 | mhash_set (&bum->bfd_session_idx_by_bfd_key, key, bs->bs_idx, NULL); |
| 126 | BFD_DBG ("session created, bs_idx=%u, sw_if_index=%d, local=%U, peer=%U", |
| 127 | bs->bs_idx, key->sw_if_index, format_ip46_address, &key->local_addr, |
| 128 | IP46_TYPE_ANY, format_ip46_address, &key->peer_addr, IP46_TYPE_ANY); |
| 129 | if (BFD_TRANSPORT_UDP4 == t) |
| 130 | { |
| 131 | bus->adj_index = adj_nbr_add_or_lock (FIB_PROTOCOL_IP4, VNET_LINK_IP4, |
| 132 | &key->peer_addr, key->sw_if_index); |
| 133 | BFD_DBG ("adj_nbr_add_or_lock(FIB_PROTOCOL_IP4, VNET_LINK_IP4, %U, %d) " |
| 134 | "returns %d", |
| 135 | format_ip46_address, &key->peer_addr, IP46_TYPE_ANY, |
| 136 | key->sw_if_index, bus->adj_index); |
| 137 | } |
| 138 | else |
| 139 | { |
| 140 | bus->adj_index = adj_nbr_add_or_lock (FIB_PROTOCOL_IP6, VNET_LINK_IP6, |
| 141 | &key->peer_addr, key->sw_if_index); |
| 142 | BFD_DBG ("adj_nbr_add_or_lock(FIB_PROTOCOL_IP6, VNET_LINK_IP6, %U, %d) " |
| 143 | "returns %d", |
| 144 | format_ip46_address, &key->peer_addr, IP46_TYPE_ANY, |
| 145 | key->sw_if_index, bus->adj_index); |
| 146 | } |
| 147 | bs->config_desired_min_tx_us = desired_min_tx_us; |
| 148 | bs->required_min_rx_us = required_min_rx_us; |
Klement Sekera | 3e0a356 | 2016-12-19 09:05:21 +0100 | [diff] [blame] | 149 | bs->required_min_echo_rx_us = required_min_rx_us; /* FIXME */ |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 150 | bs->local_detect_mult = detect_mult; |
| 151 | bfd_session_start (bum->bfd_main, bs); |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | static vnet_api_error_t |
| 156 | bfd_udp_validate_api_input (u32 sw_if_index, const ip46_address_t *local_addr, |
| 157 | const ip46_address_t *peer_addr) |
| 158 | { |
| 159 | vnet_sw_interface_t *sw_if = |
| 160 | vnet_get_sw_interface (vnet_get_main (), sw_if_index); |
| 161 | u8 local_ip_valid = 0; |
| 162 | ip_interface_address_t *ia = NULL; |
| 163 | if (!sw_if) |
| 164 | { |
| 165 | BFD_ERR ("got NULL sw_if"); |
| 166 | return VNET_API_ERROR_INVALID_SW_IF_INDEX; |
| 167 | } |
| 168 | if (ip46_address_is_ip4 (local_addr)) |
| 169 | { |
| 170 | if (!ip46_address_is_ip4 (peer_addr)) |
| 171 | { |
| 172 | BFD_ERR ("IP family mismatch"); |
| 173 | return VNET_API_ERROR_INVALID_ARGUMENT; |
| 174 | } |
| 175 | ip4_main_t *im = &ip4_main; |
| 176 | |
| 177 | /* *INDENT-OFF* */ |
| 178 | foreach_ip_interface_address ( |
| 179 | &im->lookup_main, ia, sw_if_index, 0 /* honor unnumbered */, ({ |
| 180 | ip4_address_t *x = |
| 181 | ip_interface_address_get_address (&im->lookup_main, ia); |
| 182 | if (x->as_u32 == local_addr->ip4.as_u32) |
| 183 | { |
| 184 | /* valid address for this interface */ |
| 185 | local_ip_valid = 1; |
| 186 | break; |
| 187 | } |
| 188 | })); |
| 189 | /* *INDENT-ON* */ |
| 190 | } |
| 191 | else |
| 192 | { |
| 193 | if (ip46_address_is_ip4 (peer_addr)) |
| 194 | { |
| 195 | BFD_ERR ("IP family mismatch"); |
| 196 | return VNET_API_ERROR_INVALID_ARGUMENT; |
| 197 | } |
| 198 | ip6_main_t *im = &ip6_main; |
| 199 | /* *INDENT-OFF* */ |
| 200 | foreach_ip_interface_address ( |
| 201 | &im->lookup_main, ia, sw_if_index, 0 /* honor unnumbered */, ({ |
| 202 | ip6_address_t *x = |
| 203 | ip_interface_address_get_address (&im->lookup_main, ia); |
| 204 | if (local_addr->ip6.as_u64[0] == x->as_u64[0] && |
| 205 | local_addr->ip6.as_u64[1] == x->as_u64[1]) |
| 206 | { |
| 207 | /* valid address for this interface */ |
| 208 | local_ip_valid = 1; |
| 209 | break; |
| 210 | } |
| 211 | })); |
| 212 | /* *INDENT-ON* */ |
| 213 | } |
| 214 | |
| 215 | if (!local_ip_valid) |
| 216 | { |
| 217 | BFD_ERR ("address not found on interface"); |
| 218 | return VNET_API_ERROR_ADDRESS_NOT_FOUND_FOR_INTERFACE; |
| 219 | } |
| 220 | |
| 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | vnet_api_error_t bfd_udp_add_session (u32 sw_if_index, u32 desired_min_tx_us, |
| 225 | u32 required_min_rx_us, u8 detect_mult, |
| 226 | const ip46_address_t *local_addr, |
| 227 | const ip46_address_t *peer_addr) |
| 228 | { |
| 229 | vnet_api_error_t rv = |
| 230 | bfd_udp_validate_api_input (sw_if_index, local_addr, peer_addr); |
| 231 | if (rv) |
| 232 | { |
| 233 | return rv; |
| 234 | } |
| 235 | if (detect_mult < 1) |
| 236 | { |
| 237 | BFD_ERR ("detect_mult < 1"); |
| 238 | return VNET_API_ERROR_INVALID_ARGUMENT; |
| 239 | } |
| 240 | if (desired_min_tx_us < 1) |
| 241 | { |
| 242 | BFD_ERR ("desired_min_tx_us < 1"); |
| 243 | return VNET_API_ERROR_INVALID_ARGUMENT; |
| 244 | } |
| 245 | return bfd_udp_add_session_internal (&bfd_udp_main, sw_if_index, |
| 246 | desired_min_tx_us, required_min_rx_us, |
| 247 | detect_mult, local_addr, peer_addr); |
| 248 | } |
| 249 | |
| 250 | vnet_api_error_t bfd_udp_del_session (u32 sw_if_index, |
| 251 | const ip46_address_t *local_addr, |
| 252 | const ip46_address_t *peer_addr) |
| 253 | { |
| 254 | vnet_api_error_t rv = |
| 255 | bfd_udp_validate_api_input (sw_if_index, local_addr, peer_addr); |
| 256 | if (rv) |
| 257 | { |
| 258 | return rv; |
| 259 | } |
| 260 | bfd_udp_main_t *bum = &bfd_udp_main; |
| 261 | vnet_sw_interface_t *sw_if = |
| 262 | vnet_get_sw_interface (vnet_get_main (), sw_if_index); |
| 263 | bfd_udp_key_t key; |
| 264 | memset (&key, 0, sizeof (key)); |
| 265 | key.sw_if_index = sw_if->sw_if_index; |
| 266 | key.local_addr.as_u64[0] = local_addr->as_u64[0]; |
| 267 | key.local_addr.as_u64[1] = local_addr->as_u64[1]; |
| 268 | key.peer_addr.as_u64[0] = peer_addr->as_u64[0]; |
| 269 | key.peer_addr.as_u64[1] = peer_addr->as_u64[1]; |
| 270 | bfd_session_t *tmp = bfd_lookup_session (bum, &key); |
| 271 | if (tmp) |
| 272 | { |
| 273 | BFD_DBG ("free bfd-udp session, bs_idx=%d", tmp->bs_idx); |
| 274 | mhash_unset (&bum->bfd_session_idx_by_bfd_key, &key, NULL); |
| 275 | adj_unlock (tmp->udp.adj_index); |
| 276 | bfd_put_session (bum->bfd_main, tmp); |
| 277 | } |
| 278 | else |
| 279 | { |
| 280 | BFD_ERR ("no such session"); |
| 281 | return VNET_API_ERROR_BFD_NOENT; |
| 282 | } |
| 283 | return 0; |
| 284 | } |
| 285 | |
| 286 | typedef enum { |
| 287 | BFD_UDP_INPUT_NEXT_NORMAL, |
| 288 | BFD_UDP_INPUT_NEXT_REPLY, |
| 289 | BFD_UDP_INPUT_N_NEXT, |
| 290 | } bfd_udp_input_next_t; |
| 291 | |
| 292 | /* Packet counters */ |
| 293 | #define foreach_bfd_udp_error(F) \ |
| 294 | F (NONE, "good bfd packets (processed)") \ |
| 295 | F (BAD, "invalid bfd packets") \ |
| 296 | F (DISABLED, "bfd packets received on disabled interfaces") |
| 297 | |
| 298 | #define F(sym, string) static char BFD_UDP_ERR_##sym##_STR[] = string; |
| 299 | foreach_bfd_udp_error (F); |
| 300 | #undef F |
| 301 | |
| 302 | static char *bfd_udp_error_strings[] = { |
| 303 | #define F(sym, string) BFD_UDP_ERR_##sym##_STR, |
| 304 | foreach_bfd_udp_error (F) |
| 305 | #undef F |
| 306 | }; |
| 307 | |
| 308 | typedef enum { |
| 309 | #define F(sym, str) BFD_UDP_ERROR_##sym, |
| 310 | foreach_bfd_udp_error (F) |
| 311 | #undef F |
| 312 | BFD_UDP_N_ERROR, |
| 313 | } bfd_udp_error_t; |
| 314 | |
| 315 | static void bfd_udp4_find_headers (vlib_buffer_t *b, const ip4_header_t **ip4, |
| 316 | const udp_header_t **udp) |
| 317 | { |
Klement Sekera | 0c1519b | 2016-12-08 05:03:32 +0100 | [diff] [blame] | 318 | /* sanity check first */ |
| 319 | const i32 start = vnet_buffer (b)->ip.start_of_ip_header; |
| 320 | if (start < 0 && start < sizeof (b->pre_data)) |
| 321 | { |
| 322 | BFD_ERR ("Start of ip header is before pre_data, ignoring"); |
| 323 | *ip4 = NULL; |
| 324 | *udp = NULL; |
| 325 | return; |
| 326 | } |
| 327 | *ip4 = (ip4_header_t *)(b->data + start); |
| 328 | if ((u8 *)*ip4 > (u8 *)vlib_buffer_get_current (b)) |
| 329 | { |
| 330 | BFD_ERR ("Start of ip header is beyond current data, ignoring"); |
| 331 | *ip4 = NULL; |
| 332 | *udp = NULL; |
| 333 | return; |
| 334 | } |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 335 | *udp = (udp_header_t *)((*ip4) + 1); |
| 336 | } |
| 337 | |
| 338 | static bfd_udp_error_t bfd_udp4_verify_transport (const ip4_header_t *ip4, |
| 339 | const udp_header_t *udp, |
| 340 | const bfd_session_t *bs) |
| 341 | { |
| 342 | const bfd_udp_session_t *bus = &bs->udp; |
| 343 | const bfd_udp_key_t *key = &bus->key; |
| 344 | if (ip4->src_address.as_u32 != key->peer_addr.ip4.as_u32) |
| 345 | { |
| 346 | BFD_ERR ("IP src addr mismatch, got %U, expected %U", format_ip4_address, |
| 347 | ip4->src_address.as_u32, format_ip4_address, |
| 348 | key->peer_addr.ip4.as_u32); |
| 349 | return BFD_UDP_ERROR_BAD; |
| 350 | } |
| 351 | if (ip4->dst_address.as_u32 != key->local_addr.ip4.as_u32) |
| 352 | { |
| 353 | BFD_ERR ("IP dst addr mismatch, got %U, expected %U", format_ip4_address, |
| 354 | ip4->dst_address.as_u32, format_ip4_address, |
| 355 | key->local_addr.ip4.as_u32); |
| 356 | return BFD_UDP_ERROR_BAD; |
| 357 | } |
| 358 | const u8 expected_ttl = 255; |
| 359 | if (ip4->ttl != expected_ttl) |
| 360 | { |
| 361 | BFD_ERR ("IP unexpected TTL value %d, expected %d", ip4->ttl, |
| 362 | expected_ttl); |
| 363 | return BFD_UDP_ERROR_BAD; |
| 364 | } |
| 365 | if (clib_net_to_host_u16 (udp->src_port) < 49152 || |
| 366 | clib_net_to_host_u16 (udp->src_port) > 65535) |
| 367 | { |
| 368 | BFD_ERR ("Invalid UDP src port %d, out of range <49152,65535>", |
| 369 | udp->src_port); |
| 370 | } |
| 371 | return BFD_UDP_ERROR_NONE; |
| 372 | } |
| 373 | |
| 374 | typedef struct |
| 375 | { |
| 376 | u32 bs_idx; |
| 377 | bfd_pkt_t pkt; |
| 378 | } bfd_rpc_update_t; |
| 379 | |
| 380 | static void bfd_rpc_update_session_cb (const bfd_rpc_update_t *a) |
| 381 | { |
| 382 | bfd_consume_pkt (bfd_udp_main.bfd_main, &a->pkt, a->bs_idx); |
| 383 | } |
| 384 | |
| 385 | static void bfd_rpc_update_session (u32 bs_idx, const bfd_pkt_t *pkt) |
| 386 | { |
| 387 | /* packet length was already verified to be correct by the caller */ |
| 388 | const u32 data_size = sizeof (bfd_rpc_update_t) - |
| 389 | STRUCT_SIZE_OF (bfd_rpc_update_t, pkt) + |
| 390 | pkt->head.length; |
| 391 | u8 data[data_size]; |
| 392 | bfd_rpc_update_t *update = (bfd_rpc_update_t *)data; |
| 393 | update->bs_idx = bs_idx; |
| 394 | clib_memcpy (&update->pkt, pkt, pkt->head.length); |
| 395 | vl_api_rpc_call_main_thread (bfd_rpc_update_session_cb, data, data_size); |
| 396 | } |
| 397 | |
| 398 | static bfd_udp_error_t bfd_udp4_scan (vlib_main_t *vm, vlib_node_runtime_t *rt, |
| 399 | vlib_buffer_t *b, bfd_session_t **bs_out) |
| 400 | { |
| 401 | const bfd_pkt_t *pkt = vlib_buffer_get_current (b); |
| 402 | if (sizeof (*pkt) > b->current_length) |
| 403 | { |
| 404 | BFD_ERR ( |
| 405 | "Payload size %d too small to hold bfd packet of minimum size %d", |
| 406 | b->current_length, sizeof (*pkt)); |
| 407 | return BFD_UDP_ERROR_BAD; |
| 408 | } |
| 409 | const ip4_header_t *ip4; |
| 410 | const udp_header_t *udp; |
| 411 | bfd_udp4_find_headers (b, &ip4, &udp); |
| 412 | if (!ip4 || !udp) |
| 413 | { |
| 414 | BFD_ERR ("Couldn't find ip4 or udp header"); |
| 415 | return BFD_UDP_ERROR_BAD; |
| 416 | } |
| 417 | if (!bfd_verify_pkt_common (pkt)) |
| 418 | { |
| 419 | return BFD_UDP_ERROR_BAD; |
| 420 | } |
| 421 | bfd_session_t *bs = NULL; |
| 422 | if (pkt->your_disc) |
| 423 | { |
| 424 | BFD_DBG ("Looking up BFD session using discriminator %u", |
| 425 | pkt->your_disc); |
| 426 | bs = bfd_find_session_by_disc (bfd_udp_main.bfd_main, pkt->your_disc); |
| 427 | } |
| 428 | else |
| 429 | { |
| 430 | bfd_udp_key_t key; |
| 431 | memset (&key, 0, sizeof (key)); |
| 432 | key.sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX]; |
| 433 | key.local_addr.ip4.as_u32 = ip4->dst_address.as_u32; |
| 434 | key.peer_addr.ip4.as_u32 = ip4->src_address.as_u32; |
| 435 | BFD_DBG ("Looking up BFD session using key (sw_if_index=%u, local=%U, " |
| 436 | "peer=%U)", |
| 437 | key.sw_if_index, format_ip4_address, key.local_addr.ip4.as_u8, |
| 438 | format_ip4_address, key.peer_addr.ip4.as_u8); |
| 439 | bs = bfd_lookup_session (&bfd_udp_main, &key); |
| 440 | } |
| 441 | if (!bs) |
| 442 | { |
| 443 | BFD_ERR ("BFD session lookup failed - no session matches BFD pkt"); |
| 444 | return BFD_UDP_ERROR_BAD; |
| 445 | } |
Klement Sekera | 637b9c4 | 2016-12-08 05:19:14 +0100 | [diff] [blame] | 446 | BFD_DBG ("BFD session found, bs_idx=%u", bs->bs_idx); |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 447 | if (!bfd_verify_pkt_session (pkt, b->current_length, bs)) |
| 448 | { |
| 449 | return BFD_UDP_ERROR_BAD; |
| 450 | } |
| 451 | bfd_udp_error_t err; |
| 452 | if (BFD_UDP_ERROR_NONE != (err = bfd_udp4_verify_transport (ip4, udp, bs))) |
| 453 | { |
| 454 | return err; |
| 455 | } |
| 456 | bfd_rpc_update_session (bs->bs_idx, pkt); |
| 457 | *bs_out = bs; |
| 458 | return BFD_UDP_ERROR_NONE; |
| 459 | } |
| 460 | |
| 461 | static bfd_udp_error_t bfd_udp6_scan (vlib_main_t *vm, vlib_buffer_t *b) |
| 462 | { |
| 463 | /* TODO */ |
| 464 | return BFD_UDP_ERROR_BAD; |
| 465 | } |
| 466 | |
| 467 | /* |
| 468 | * Process a frame of bfd packets |
| 469 | * Expect 1 packet / frame |
| 470 | */ |
| 471 | static uword bfd_udp_input (vlib_main_t *vm, vlib_node_runtime_t *rt, |
| 472 | vlib_frame_t *f, int is_ipv6) |
| 473 | { |
| 474 | u32 n_left_from, *from; |
| 475 | bfd_input_trace_t *t0; |
| 476 | |
| 477 | from = vlib_frame_vector_args (f); /* array of buffer indices */ |
| 478 | n_left_from = f->n_vectors; /* number of buffer indices */ |
| 479 | |
| 480 | while (n_left_from > 0) |
| 481 | { |
| 482 | u32 bi0; |
| 483 | vlib_buffer_t *b0; |
| 484 | u32 next0, error0; |
| 485 | |
| 486 | bi0 = from[0]; |
| 487 | b0 = vlib_get_buffer (vm, bi0); |
| 488 | |
| 489 | bfd_session_t *bs = NULL; |
| 490 | |
| 491 | /* If this pkt is traced, snapshot the data */ |
| 492 | if (b0->flags & VLIB_BUFFER_IS_TRACED) |
| 493 | { |
| 494 | int len; |
| 495 | t0 = vlib_add_trace (vm, rt, b0, sizeof (*t0)); |
| 496 | len = (b0->current_length < sizeof (t0->data)) ? b0->current_length |
| 497 | : sizeof (t0->data); |
| 498 | t0->len = len; |
| 499 | clib_memcpy (t0->data, vlib_buffer_get_current (b0), len); |
| 500 | } |
| 501 | |
| 502 | /* scan this bfd pkt. error0 is the counter index to bmp */ |
| 503 | if (is_ipv6) |
| 504 | { |
| 505 | error0 = bfd_udp6_scan (vm, b0); |
| 506 | } |
| 507 | else |
| 508 | { |
| 509 | error0 = bfd_udp4_scan (vm, rt, b0, &bs); |
| 510 | } |
| 511 | b0->error = rt->errors[error0]; |
| 512 | |
| 513 | next0 = BFD_UDP_INPUT_NEXT_NORMAL; |
| 514 | if (BFD_UDP_ERROR_NONE == error0) |
| 515 | { |
Klement Sekera | 0c1519b | 2016-12-08 05:03:32 +0100 | [diff] [blame] | 516 | /* if everything went fine, check for poll bit, if present, re-use |
Klement Sekera | e4504c6 | 2016-12-08 10:16:41 +0100 | [diff] [blame] | 517 | the buffer and based on (now updated) session parameters, send the |
Klement Sekera | 0c1519b | 2016-12-08 05:03:32 +0100 | [diff] [blame] | 518 | final packet back */ |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 519 | const bfd_pkt_t *pkt = vlib_buffer_get_current (b0); |
| 520 | if (bfd_pkt_get_poll (pkt)) |
| 521 | { |
| 522 | bfd_send_final (vm, b0, bs); |
Klement Sekera | e4504c6 | 2016-12-08 10:16:41 +0100 | [diff] [blame] | 523 | if (is_ipv6) |
| 524 | { |
| 525 | vlib_node_increment_counter (vm, bfd_udp6_input_node.index, |
| 526 | b0->error, 1); |
| 527 | } |
| 528 | else |
| 529 | { |
| 530 | vlib_node_increment_counter (vm, bfd_udp4_input_node.index, |
| 531 | b0->error, 1); |
| 532 | } |
Klement Sekera | 0c1519b | 2016-12-08 05:03:32 +0100 | [diff] [blame] | 533 | next0 = BFD_UDP_INPUT_NEXT_REPLY; |
Klement Sekera | 0e3c0de | 2016-09-29 14:43:44 +0200 | [diff] [blame] | 534 | } |
| 535 | } |
| 536 | vlib_set_next_frame_buffer (vm, rt, next0, bi0); |
| 537 | |
| 538 | from += 1; |
| 539 | n_left_from -= 1; |
| 540 | } |
| 541 | |
| 542 | return f->n_vectors; |
| 543 | } |
| 544 | |
| 545 | static uword bfd_udp4_input (vlib_main_t *vm, vlib_node_runtime_t *rt, |
| 546 | vlib_frame_t *f) |
| 547 | { |
| 548 | return bfd_udp_input (vm, rt, f, 0); |
| 549 | } |
| 550 | |
| 551 | /* |
| 552 | * bfd input graph node declaration |
| 553 | */ |
| 554 | /* *INDENT-OFF* */ |
| 555 | VLIB_REGISTER_NODE (bfd_udp4_input_node, static) = { |
| 556 | .function = bfd_udp4_input, |
| 557 | .name = "bfd-udp4-input", |
| 558 | .vector_size = sizeof (u32), |
| 559 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 560 | |
| 561 | .n_errors = BFD_UDP_N_ERROR, |
| 562 | .error_strings = bfd_udp_error_strings, |
| 563 | |
| 564 | .format_trace = bfd_input_format_trace, |
| 565 | |
| 566 | .n_next_nodes = BFD_UDP_INPUT_N_NEXT, |
| 567 | .next_nodes = |
| 568 | { |
| 569 | [BFD_UDP_INPUT_NEXT_NORMAL] = "error-drop", |
| 570 | [BFD_UDP_INPUT_NEXT_REPLY] = "ip4-lookup", |
| 571 | }, |
| 572 | }; |
| 573 | /* *INDENT-ON* */ |
| 574 | |
| 575 | static uword bfd_udp6_input (vlib_main_t *vm, vlib_node_runtime_t *rt, |
| 576 | vlib_frame_t *f) |
| 577 | { |
| 578 | return bfd_udp_input (vm, rt, f, 1); |
| 579 | } |
| 580 | |
| 581 | /* *INDENT-OFF* */ |
| 582 | VLIB_REGISTER_NODE (bfd_udp6_input_node, static) = { |
| 583 | .function = bfd_udp6_input, |
| 584 | .name = "bfd-udp6-input", |
| 585 | .vector_size = sizeof (u32), |
| 586 | .type = VLIB_NODE_TYPE_INTERNAL, |
| 587 | |
| 588 | .n_errors = BFD_UDP_N_ERROR, |
| 589 | .error_strings = bfd_udp_error_strings, |
| 590 | |
| 591 | .format_trace = bfd_input_format_trace, |
| 592 | |
| 593 | .n_next_nodes = BFD_UDP_INPUT_N_NEXT, |
| 594 | .next_nodes = |
| 595 | { |
| 596 | [BFD_UDP_INPUT_NEXT_NORMAL] = "error-drop", |
| 597 | [BFD_UDP_INPUT_NEXT_REPLY] = "ip6-lookup", |
| 598 | }, |
| 599 | }; |
| 600 | /* *INDENT-ON* */ |
| 601 | |
| 602 | static clib_error_t *bfd_sw_interface_up_down (vnet_main_t *vnm, |
| 603 | u32 sw_if_index, u32 flags) |
| 604 | { |
| 605 | // vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index); |
| 606 | if (!(flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)) |
| 607 | { |
| 608 | /* TODO */ |
| 609 | } |
| 610 | return 0; |
| 611 | } |
| 612 | |
| 613 | VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (bfd_sw_interface_up_down); |
| 614 | |
| 615 | static clib_error_t *bfd_hw_interface_up_down (vnet_main_t *vnm, |
| 616 | u32 hw_if_index, u32 flags) |
| 617 | { |
| 618 | if (flags & VNET_HW_INTERFACE_FLAG_LINK_UP) |
| 619 | { |
| 620 | /* TODO */ |
| 621 | } |
| 622 | return 0; |
| 623 | } |
| 624 | |
| 625 | VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION (bfd_hw_interface_up_down); |
| 626 | |
| 627 | /* |
| 628 | * setup function |
| 629 | */ |
| 630 | static clib_error_t *bfd_udp_init (vlib_main_t *vm) |
| 631 | { |
| 632 | mhash_init (&bfd_udp_main.bfd_session_idx_by_bfd_key, sizeof (uword), |
| 633 | sizeof (bfd_udp_key_t)); |
| 634 | bfd_udp_main.bfd_main = &bfd_main; |
| 635 | udp_register_dst_port (vm, UDP_DST_PORT_bfd4, bfd_udp4_input_node.index, 1); |
| 636 | udp_register_dst_port (vm, UDP_DST_PORT_bfd6, bfd_udp6_input_node.index, 0); |
| 637 | return 0; |
| 638 | } |
| 639 | |
| 640 | VLIB_INIT_FUNCTION (bfd_udp_init); |