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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @brief BFD UDP transport layer implementation
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>
25 #include <vnet/udp/udp_packet.h>
26 #include <vnet/udp/udp.h>
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>
31 #include <vnet/ip/ip6_packet.h>
32 #include <vnet/adj/adj.h>
33 #include <vnet/adj/adj_nbr.h>
34 #include <vnet/dpo/receive_dpo.h>
35 #include <vnet/fib/fib_entry.h>
36 #include <vnet/fib/fib_table.h>
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>
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;
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;
56 static vlib_node_registration_t bfd_udp4_input_node;
57 static vlib_node_registration_t bfd_udp6_input_node;
58 static vlib_node_registration_t bfd_udp_echo4_input_node;
59 static vlib_node_registration_t bfd_udp_echo6_input_node;
61 bfd_udp_main_t bfd_udp_main;
64 bfd_udp_set_echo_source (u32 sw_if_index)
66 vnet_sw_interface_t *sw_if =
67 vnet_get_sw_interface_safe (bfd_udp_main.vnet_main, sw_if_index);
70 bfd_udp_main.echo_source_sw_if_index = sw_if_index;
71 bfd_udp_main.echo_source_is_set = 1;
74 return VNET_API_ERROR_BFD_ENOENT;
78 bfd_udp_del_echo_source (u32 sw_if_index)
80 bfd_udp_main.echo_source_sw_if_index = ~0;
81 bfd_udp_main.echo_source_is_set = 0;
86 bfd_udp_is_echo_available (bfd_transport_e transport)
88 if (!bfd_udp_main.echo_source_is_set)
90 BFD_DBG ("UDP echo source not set - echo not available");
94 * for the echo to work, we need a loopback interface with at least one
95 * address with netmask length at most 31 (ip4) or 127 (ip6) so that we can
96 * pick an unused address from that subnet
98 vnet_sw_interface_t *sw_if =
99 vnet_get_sw_interface_safe (bfd_udp_main.vnet_main,
100 bfd_udp_main.echo_source_sw_if_index);
101 if (sw_if && sw_if->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
103 if (BFD_TRANSPORT_UDP4 == transport)
105 ip4_main_t *im = &ip4_main;
106 ip_interface_address_t *ia = NULL;
108 foreach_ip_interface_address (&im->lookup_main, ia,
109 bfd_udp_main.echo_source_sw_if_index,
110 0 /* honor unnumbered */, ({
111 if (ia->address_length <= 31)
118 else if (BFD_TRANSPORT_UDP6 == transport)
120 ip6_main_t *im = &ip6_main;
121 ip_interface_address_t *ia = NULL;
123 foreach_ip_interface_address (&im->lookup_main, ia,
124 bfd_udp_main.echo_source_sw_if_index,
125 0 /* honor unnumbered */, ({
126 if (ia->address_length <= 127)
134 BFD_DBG ("No usable IP address for UDP echo - echo not available");
139 bfd_udp_bs_idx_to_sport (u32 bs_idx)
141 /* The source port MUST be in the range 49152 through 65535. The same UDP
142 * source port number MUST be used for all BFD Control packets associated
143 * with a particular session. The source port number SHOULD be unique among
144 * all BFD sessions on the system. If more than 16384 BFD sessions are
145 * simultaneously active, UDP source port numbers MAY be reused on
146 * multiple sessions, but the number of distinct uses of the same UDP
147 * source port number SHOULD be minimized.
149 return 49152 + bs_idx % (65535 - 49152 + 1);
153 bfd_udp_get_echo_src_ip4 (ip4_address_t * addr)
155 if (!bfd_udp_main.echo_source_is_set)
157 BFD_ERR ("cannot find ip4 address, echo source not set");
160 ip_interface_address_t *ia = NULL;
161 ip4_main_t *im = &ip4_main;
164 foreach_ip_interface_address (
165 &im->lookup_main, ia, bfd_udp_main.echo_source_sw_if_index,
166 0 /* honor unnumbered */, ({
168 ip_interface_address_get_address (&im->lookup_main, ia);
169 if (ia->address_length <= 31)
171 addr->as_u32 = clib_host_to_net_u32 (x->as_u32);
173 * flip the last bit to get a different address, might be network,
177 addr->as_u32 = clib_net_to_host_u32 (addr->as_u32);
182 BFD_ERR ("cannot find ip4 address, no usable address found");
187 bfd_udp_get_echo_src_ip6 (ip6_address_t * addr)
189 if (!bfd_udp_main.echo_source_is_set)
191 BFD_ERR ("cannot find ip6 address, echo source not set");
194 ip_interface_address_t *ia = NULL;
195 ip6_main_t *im = &ip6_main;
198 foreach_ip_interface_address (
199 &im->lookup_main, ia, bfd_udp_main.echo_source_sw_if_index,
200 0 /* honor unnumbered */, ({
202 ip_interface_address_get_address (&im->lookup_main, ia);
203 if (ia->address_length <= 127)
206 addr->as_u8[15] ^= 1; /* flip the last bit of the address */
211 BFD_ERR ("cannot find ip6 address, no usable address found");
216 bfd_udp_get_echo_source (int *is_set, u32 * sw_if_index, int *have_usable_ip4,
217 ip4_address_t * ip4, int *have_usable_ip6,
220 if (bfd_udp_main.echo_source_is_set)
223 *sw_if_index = bfd_udp_main.echo_source_sw_if_index;
224 *have_usable_ip4 = bfd_udp_get_echo_src_ip4 (ip4);
225 *have_usable_ip6 = bfd_udp_get_echo_src_ip6 (ip6);
234 bfd_add_udp4_transport (vlib_main_t * vm, vlib_buffer_t * b,
235 const bfd_session_t * bs, int is_echo)
237 const bfd_udp_session_t *bus = &bs->udp;
238 const bfd_udp_key_t *key = &bus->key;
240 b->flags |= VNET_BUFFER_LOCALLY_ORIGINATED;
241 vnet_buffer (b)->ip.adj_index[VLIB_RX] = bus->adj_index;
242 vnet_buffer (b)->ip.adj_index[VLIB_TX] = bus->adj_index;
248 ip4_udp_headers *headers = NULL;
249 vlib_buffer_advance (b, -sizeof (*headers));
250 headers = vlib_buffer_get_current (b);
251 memset (headers, 0, sizeof (*headers));
252 headers->ip4.ip_version_and_header_length = 0x45;
253 headers->ip4.ttl = 255;
254 headers->ip4.protocol = IP_PROTOCOL_UDP;
255 headers->udp.src_port =
256 clib_host_to_net_u16 (bfd_udp_bs_idx_to_sport (bs->bs_idx));
260 if (!(rv = bfd_udp_get_echo_src_ip4 (&headers->ip4.src_address)))
264 headers->ip4.dst_address.as_u32 = key->local_addr.ip4.as_u32;
265 headers->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_bfd_echo4);
269 headers->ip4.src_address.as_u32 = key->local_addr.ip4.as_u32;
270 headers->ip4.dst_address.as_u32 = key->peer_addr.ip4.as_u32;
271 headers->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_bfd4);
274 /* fix ip length, checksum and udp length */
275 const u16 ip_length = vlib_buffer_length_in_chain (vm, b);
277 headers->ip4.length = clib_host_to_net_u16 (ip_length);
278 headers->ip4.checksum = ip4_header_checksum (&headers->ip4);
280 const u16 udp_length = ip_length - (sizeof (headers->ip4));
281 headers->udp.length = clib_host_to_net_u16 (udp_length);
286 bfd_add_udp6_transport (vlib_main_t * vm, vlib_buffer_t * b,
287 const bfd_session_t * bs, int is_echo)
289 const bfd_udp_session_t *bus = &bs->udp;
290 const bfd_udp_key_t *key = &bus->key;
292 b->flags |= VNET_BUFFER_LOCALLY_ORIGINATED;
293 vnet_buffer (b)->ip.adj_index[VLIB_RX] = bus->adj_index;
294 vnet_buffer (b)->ip.adj_index[VLIB_TX] = bus->adj_index;
300 ip6_udp_headers *headers = NULL;
301 vlib_buffer_advance (b, -sizeof (*headers));
302 headers = vlib_buffer_get_current (b);
303 memset (headers, 0, sizeof (*headers));
304 headers->ip6.ip_version_traffic_class_and_flow_label =
305 clib_host_to_net_u32 (0x6 << 28);
306 headers->ip6.hop_limit = 255;
307 headers->ip6.protocol = IP_PROTOCOL_UDP;
308 headers->udp.src_port =
309 clib_host_to_net_u16 (bfd_udp_bs_idx_to_sport (bs->bs_idx));
313 if (!(rv = bfd_udp_get_echo_src_ip6 (&headers->ip6.src_address)))
317 clib_memcpy (&headers->ip6.dst_address, &key->local_addr.ip6,
318 sizeof (headers->ip6.dst_address));
320 headers->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_bfd_echo6);
324 clib_memcpy (&headers->ip6.src_address, &key->local_addr.ip6,
325 sizeof (headers->ip6.src_address));
326 clib_memcpy (&headers->ip6.dst_address, &key->peer_addr.ip6,
327 sizeof (headers->ip6.dst_address));
328 headers->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_bfd6);
331 /* fix ip payload length and udp length */
332 const u16 udp_length =
333 vlib_buffer_length_in_chain (vm, b) - (sizeof (headers->ip6));
334 headers->udp.length = clib_host_to_net_u16 (udp_length);
335 headers->ip6.payload_length = headers->udp.length;
337 /* IPv6 UDP checksum is mandatory */
339 headers->udp.checksum =
340 ip6_tcp_udp_icmp_compute_checksum (vm, b, &headers->ip6, &bogus);
342 if (headers->udp.checksum == 0)
344 headers->udp.checksum = 0xffff;
349 static bfd_session_t *
350 bfd_lookup_session (bfd_udp_main_t * bum, const bfd_udp_key_t * key)
352 uword *p = mhash_get (&bum->bfd_session_idx_by_bfd_key, key);
355 return bfd_find_session_by_idx (bum->bfd_main, *p);
361 bfd_udp_key_init (bfd_udp_key_t * key, u32 sw_if_index,
362 const ip46_address_t * local_addr,
363 const ip46_address_t * peer_addr)
365 memset (key, 0, sizeof (*key));
366 key->sw_if_index = sw_if_index;
367 key->local_addr.as_u64[0] = local_addr->as_u64[0];
368 key->local_addr.as_u64[1] = local_addr->as_u64[1];
369 key->peer_addr.as_u64[0] = peer_addr->as_u64[0];
370 key->peer_addr.as_u64[1] = peer_addr->as_u64[1];
373 static vnet_api_error_t
374 bfd_udp_add_session_internal (bfd_udp_main_t * bum, u32 sw_if_index,
375 u32 desired_min_tx_usec,
376 u32 required_min_rx_usec, u8 detect_mult,
377 const ip46_address_t * local_addr,
378 const ip46_address_t * peer_addr,
379 bfd_session_t ** bs_out)
381 /* get a pool entry and if we end up not needing it, give it back */
382 bfd_transport_e t = BFD_TRANSPORT_UDP4;
383 if (!ip46_address_is_ip4 (local_addr))
385 t = BFD_TRANSPORT_UDP6;
387 bfd_session_t *bs = bfd_get_session (bum->bfd_main, t);
390 bfd_put_session (bum->bfd_main, bs);
391 return VNET_API_ERROR_BFD_EAGAIN;
393 bfd_udp_session_t *bus = &bs->udp;
394 memset (bus, 0, sizeof (*bus));
395 bfd_udp_key_t *key = &bus->key;
396 bfd_udp_key_init (key, sw_if_index, local_addr, peer_addr);
397 const bfd_session_t *tmp = bfd_lookup_session (bum, key);
400 clib_warning ("duplicate bfd-udp session, existing bs_idx=%d",
402 bfd_put_session (bum->bfd_main, bs);
403 return VNET_API_ERROR_BFD_EEXIST;
405 mhash_set (&bum->bfd_session_idx_by_bfd_key, key, bs->bs_idx, NULL);
406 BFD_DBG ("session created, bs_idx=%u, sw_if_index=%d, local=%U, peer=%U",
407 bs->bs_idx, key->sw_if_index, format_ip46_address,
408 &key->local_addr, IP46_TYPE_ANY, format_ip46_address,
409 &key->peer_addr, IP46_TYPE_ANY);
410 if (BFD_TRANSPORT_UDP4 == t)
412 bus->adj_index = adj_nbr_add_or_lock (FIB_PROTOCOL_IP4, VNET_LINK_IP4,
415 BFD_DBG ("adj_nbr_add_or_lock(FIB_PROTOCOL_IP4, VNET_LINK_IP4, %U, %d) "
416 "returns %d", format_ip46_address, &key->peer_addr,
417 IP46_TYPE_ANY, key->sw_if_index, bus->adj_index);
421 bus->adj_index = adj_nbr_add_or_lock (FIB_PROTOCOL_IP6, VNET_LINK_IP6,
424 BFD_DBG ("adj_nbr_add_or_lock(FIB_PROTOCOL_IP6, VNET_LINK_IP6, %U, %d) "
425 "returns %d", format_ip46_address, &key->peer_addr,
426 IP46_TYPE_ANY, key->sw_if_index, bus->adj_index);
429 return bfd_session_set_params (bum->bfd_main, bs, desired_min_tx_usec,
430 required_min_rx_usec, detect_mult);
433 static vnet_api_error_t
434 bfd_udp_validate_api_input (u32 sw_if_index,
435 const ip46_address_t * local_addr,
436 const ip46_address_t * peer_addr)
438 vnet_sw_interface_t *sw_if =
439 vnet_get_sw_interface_safe (bfd_udp_main.vnet_main, sw_if_index);
440 u8 local_ip_valid = 0;
441 ip_interface_address_t *ia = NULL;
444 clib_warning ("got NULL sw_if");
445 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
447 if (ip46_address_is_ip4 (local_addr))
449 if (!ip46_address_is_ip4 (peer_addr))
451 clib_warning ("IP family mismatch");
452 return VNET_API_ERROR_INVALID_ARGUMENT;
454 ip4_main_t *im = &ip4_main;
457 foreach_ip_interface_address (
458 &im->lookup_main, ia, sw_if_index, 0 /* honor unnumbered */, ({
460 ip_interface_address_get_address (&im->lookup_main, ia);
461 if (x->as_u32 == local_addr->ip4.as_u32)
463 /* valid address for this interface */
472 if (ip46_address_is_ip4 (peer_addr))
474 clib_warning ("IP family mismatch");
475 return VNET_API_ERROR_INVALID_ARGUMENT;
477 ip6_main_t *im = &ip6_main;
479 foreach_ip_interface_address (
480 &im->lookup_main, ia, sw_if_index, 0 /* honor unnumbered */, ({
482 ip_interface_address_get_address (&im->lookup_main, ia);
483 if (local_addr->ip6.as_u64[0] == x->as_u64[0] &&
484 local_addr->ip6.as_u64[1] == x->as_u64[1])
486 /* valid address for this interface */
496 clib_warning ("address not found on interface");
497 return VNET_API_ERROR_ADDRESS_NOT_FOUND_FOR_INTERFACE;
503 static vnet_api_error_t
504 bfd_udp_find_session_by_api_input (u32 sw_if_index,
505 const ip46_address_t * local_addr,
506 const ip46_address_t * peer_addr,
507 bfd_session_t ** bs_out)
509 vnet_api_error_t rv =
510 bfd_udp_validate_api_input (sw_if_index, local_addr, peer_addr);
513 bfd_udp_main_t *bum = &bfd_udp_main;
515 bfd_udp_key_init (&key, sw_if_index, local_addr, peer_addr);
516 bfd_session_t *bs = bfd_lookup_session (bum, &key);
524 ("BFD session not found (sw_if_index=%u, local=%U, peer=%U",
525 sw_if_index, format_ip46_address, local_addr, IP46_TYPE_ANY,
526 format_ip46_address, peer_addr, IP46_TYPE_ANY);
527 return VNET_API_ERROR_BFD_ENOENT;
533 static vnet_api_error_t
534 bfd_api_verify_common (u32 sw_if_index, u32 desired_min_tx_usec,
535 u32 required_min_rx_usec, u8 detect_mult,
536 const ip46_address_t * local_addr,
537 const ip46_address_t * peer_addr)
539 vnet_api_error_t rv =
540 bfd_udp_validate_api_input (sw_if_index, local_addr, peer_addr);
547 clib_warning ("detect_mult < 1");
548 return VNET_API_ERROR_INVALID_ARGUMENT;
550 if (desired_min_tx_usec < 1)
552 clib_warning ("desired_min_tx_usec < 1");
553 return VNET_API_ERROR_INVALID_ARGUMENT;
559 bfd_udp_del_session_internal (bfd_session_t * bs)
561 bfd_udp_main_t *bum = &bfd_udp_main;
562 BFD_DBG ("free bfd-udp session, bs_idx=%d", bs->bs_idx);
563 mhash_unset (&bum->bfd_session_idx_by_bfd_key, &bs->udp.key, NULL);
564 adj_unlock (bs->udp.adj_index);
565 bfd_put_session (bum->bfd_main, bs);
569 bfd_udp_add_session (u32 sw_if_index, const ip46_address_t * local_addr,
570 const ip46_address_t * peer_addr,
571 u32 desired_min_tx_usec, u32 required_min_rx_usec,
572 u8 detect_mult, u8 is_authenticated, u32 conf_key_id,
575 vnet_api_error_t rv =
576 bfd_api_verify_common (sw_if_index, desired_min_tx_usec,
577 required_min_rx_usec, detect_mult,
578 local_addr, peer_addr);
579 bfd_session_t *bs = NULL;
583 bfd_udp_add_session_internal (&bfd_udp_main, sw_if_index,
585 required_min_rx_usec, detect_mult,
586 local_addr, peer_addr, &bs);
588 if (!rv && is_authenticated)
591 rv = bfd_auth_activate (bs, conf_key_id, bfd_key_id,
592 0 /* is not delayed */ );
594 clib_warning ("SSL missing, cannot add authenticated BFD session");
595 rv = VNET_API_ERROR_BFD_NOTSUPP;
599 bfd_udp_del_session_internal (bs);
604 bfd_session_start (bfd_udp_main.bfd_main, bs);
611 bfd_udp_mod_session (u32 sw_if_index,
612 const ip46_address_t * local_addr,
613 const ip46_address_t * peer_addr,
614 u32 desired_min_tx_usec,
615 u32 required_min_rx_usec, u8 detect_mult)
617 bfd_session_t *bs = NULL;
618 vnet_api_error_t rv =
619 bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr,
626 return bfd_session_set_params (bfd_udp_main.bfd_main, bs,
627 desired_min_tx_usec, required_min_rx_usec,
632 bfd_udp_del_session (u32 sw_if_index,
633 const ip46_address_t * local_addr,
634 const ip46_address_t * peer_addr)
636 bfd_session_t *bs = NULL;
637 vnet_api_error_t rv =
638 bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr,
644 bfd_udp_del_session_internal (bs);
649 bfd_udp_session_set_flags (u32 sw_if_index,
650 const ip46_address_t * local_addr,
651 const ip46_address_t * peer_addr, u8 admin_up_down)
653 bfd_session_t *bs = NULL;
654 vnet_api_error_t rv =
655 bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr,
661 bfd_session_set_flags (bs, admin_up_down);
666 bfd_udp_auth_activate (u32 sw_if_index,
667 const ip46_address_t * local_addr,
668 const ip46_address_t * peer_addr,
669 u32 conf_key_id, u8 key_id, u8 is_delayed)
672 bfd_session_t *bs = NULL;
673 vnet_api_error_t rv =
674 bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr,
680 return bfd_auth_activate (bs, conf_key_id, key_id, is_delayed);
682 clib_warning ("SSL missing, cannot activate BFD authentication");
683 return VNET_API_ERROR_BFD_NOTSUPP;
688 bfd_udp_auth_deactivate (u32 sw_if_index,
689 const ip46_address_t * local_addr,
690 const ip46_address_t * peer_addr, u8 is_delayed)
692 bfd_session_t *bs = NULL;
693 vnet_api_error_t rv =
694 bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr,
700 return bfd_auth_deactivate (bs, is_delayed);
705 BFD_UDP_INPUT_NEXT_NORMAL,
706 BFD_UDP_INPUT_NEXT_REPLY,
707 BFD_UDP_INPUT_N_NEXT,
708 } bfd_udp_input_next_t;
710 /* Packet counters - BFD control frames */
711 #define foreach_bfd_udp_error(F) \
712 F (NONE, "good bfd packets (processed)") \
713 F (BAD, "invalid bfd packets")
715 #define F(sym, string) static char BFD_UDP_ERR_##sym##_STR[] = string;
716 foreach_bfd_udp_error (F);
719 static char *bfd_udp_error_strings[] = {
720 #define F(sym, string) BFD_UDP_ERR_##sym##_STR,
721 foreach_bfd_udp_error (F)
727 #define F(sym, str) BFD_UDP_ERROR_##sym,
728 foreach_bfd_udp_error (F)
733 /* Packet counters - BFD ECHO packets */
734 #define foreach_bfd_udp_echo_error(F) \
735 F (NONE, "good bfd echo packets (processed)") \
736 F (BAD, "invalid bfd echo packets")
738 #define F(sym, string) static char BFD_UDP_ECHO_ERR_##sym##_STR[] = string;
739 foreach_bfd_udp_echo_error (F);
742 static char *bfd_udp_echo_error_strings[] = {
743 #define F(sym, string) BFD_UDP_ECHO_ERR_##sym##_STR,
744 foreach_bfd_udp_echo_error (F)
750 #define F(sym, str) BFD_UDP_ECHO_ERROR_##sym,
751 foreach_bfd_udp_echo_error (F)
753 BFD_UDP_ECHO_N_ERROR,
754 } bfd_udp_echo_error_t;
757 bfd_udp4_find_headers (vlib_buffer_t * b, ip4_header_t ** ip4,
760 /* sanity check first */
761 const i32 start = vnet_buffer (b)->ip.start_of_ip_header;
762 if (start < 0 && start < sizeof (b->pre_data))
764 BFD_ERR ("Start of ip header is before pre_data, ignoring");
769 *ip4 = (ip4_header_t *) (b->data + start);
770 if ((u8 *) * ip4 > (u8 *) vlib_buffer_get_current (b))
772 BFD_ERR ("Start of ip header is beyond current data, ignoring");
777 *udp = (udp_header_t *) ((*ip4) + 1);
780 static bfd_udp_error_t
781 bfd_udp4_verify_transport (const ip4_header_t * ip4,
782 const udp_header_t * udp, const bfd_session_t * bs)
784 const bfd_udp_session_t *bus = &bs->udp;
785 const bfd_udp_key_t *key = &bus->key;
786 if (ip4->src_address.as_u32 != key->peer_addr.ip4.as_u32)
788 BFD_ERR ("IPv4 src addr mismatch, got %U, expected %U",
789 format_ip4_address, ip4->src_address.as_u8, format_ip4_address,
790 key->peer_addr.ip4.as_u8);
791 return BFD_UDP_ERROR_BAD;
793 if (ip4->dst_address.as_u32 != key->local_addr.ip4.as_u32)
795 BFD_ERR ("IPv4 dst addr mismatch, got %U, expected %U",
796 format_ip4_address, ip4->dst_address.as_u8, format_ip4_address,
797 key->local_addr.ip4.as_u8);
798 return BFD_UDP_ERROR_BAD;
800 const u8 expected_ttl = 255;
801 if (ip4->ttl != expected_ttl)
803 BFD_ERR ("IPv4 unexpected TTL value %u, expected %u", ip4->ttl,
805 return BFD_UDP_ERROR_BAD;
807 if (clib_net_to_host_u16 (udp->src_port) < 49152)
809 BFD_ERR ("Invalid UDP src port %u, out of range <49152,65535>",
812 return BFD_UDP_ERROR_NONE;
822 bfd_rpc_update_session_cb (const bfd_rpc_update_t * a)
824 bfd_consume_pkt (bfd_udp_main.bfd_main, &a->pkt, a->bs_idx);
828 bfd_rpc_update_session (u32 bs_idx, const bfd_pkt_t * pkt)
830 /* packet length was already verified to be correct by the caller */
831 const u32 data_size = sizeof (bfd_rpc_update_t) -
832 STRUCT_SIZE_OF (bfd_rpc_update_t, pkt) + pkt->head.length;
834 bfd_rpc_update_t *update = (bfd_rpc_update_t *) data;
835 update->bs_idx = bs_idx;
836 clib_memcpy (&update->pkt, pkt, pkt->head.length);
837 vl_api_rpc_call_main_thread (bfd_rpc_update_session_cb, data, data_size);
840 static bfd_udp_error_t
841 bfd_udp4_scan (vlib_main_t * vm, vlib_node_runtime_t * rt,
842 vlib_buffer_t * b, bfd_session_t ** bs_out)
844 const bfd_pkt_t *pkt = vlib_buffer_get_current (b);
845 if (sizeof (*pkt) > b->current_length)
848 ("Payload size %d too small to hold bfd packet of minimum size %d",
849 b->current_length, sizeof (*pkt));
850 return BFD_UDP_ERROR_BAD;
854 bfd_udp4_find_headers (b, &ip4, &udp);
857 BFD_ERR ("Couldn't find ip4 or udp header");
858 return BFD_UDP_ERROR_BAD;
860 const u32 udp_payload_length = udp->length - sizeof (*udp);
861 if (pkt->head.length > udp_payload_length)
864 ("BFD packet length is larger than udp payload length (%u > %u)",
865 pkt->head.length, udp_payload_length);
866 return BFD_UDP_ERROR_BAD;
868 if (!bfd_verify_pkt_common (pkt))
870 return BFD_UDP_ERROR_BAD;
872 bfd_session_t *bs = NULL;
875 BFD_DBG ("Looking up BFD session using discriminator %u",
877 bs = bfd_find_session_by_disc (bfd_udp_main.bfd_main, pkt->your_disc);
882 memset (&key, 0, sizeof (key));
883 key.sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
884 key.local_addr.ip4.as_u32 = ip4->dst_address.as_u32;
885 key.peer_addr.ip4.as_u32 = ip4->src_address.as_u32;
886 BFD_DBG ("Looking up BFD session using key (sw_if_index=%u, local=%U, "
888 key.sw_if_index, format_ip4_address, key.local_addr.ip4.as_u8,
889 format_ip4_address, key.peer_addr.ip4.as_u8);
890 bs = bfd_lookup_session (&bfd_udp_main, &key);
894 BFD_ERR ("BFD session lookup failed - no session matches BFD pkt");
895 return BFD_UDP_ERROR_BAD;
897 BFD_DBG ("BFD session found, bs_idx=%u", bs->bs_idx);
898 if (!bfd_verify_pkt_auth (pkt, b->current_length, bs))
900 BFD_ERR ("Packet verification failed, dropping packet");
901 return BFD_UDP_ERROR_BAD;
904 if (BFD_UDP_ERROR_NONE != (err = bfd_udp4_verify_transport (ip4, udp, bs)))
908 bfd_rpc_update_session (bs->bs_idx, pkt);
910 return BFD_UDP_ERROR_NONE;
914 bfd_udp6_find_headers (vlib_buffer_t * b, ip6_header_t ** ip6,
917 /* sanity check first */
918 const i32 start = vnet_buffer (b)->ip.start_of_ip_header;
919 if (start < 0 && start < sizeof (b->pre_data))
921 BFD_ERR ("Start of ip header is before pre_data, ignoring");
926 *ip6 = (ip6_header_t *) (b->data + start);
927 if ((u8 *) * ip6 > (u8 *) vlib_buffer_get_current (b))
929 BFD_ERR ("Start of ip header is beyond current data, ignoring");
934 if ((*ip6)->protocol != IP_PROTOCOL_UDP)
936 BFD_ERR ("Unexpected protocol in IPv6 header '%u', expected '%u' (== "
937 "IP_PROTOCOL_UDP)", (*ip6)->protocol, IP_PROTOCOL_UDP);
942 *udp = (udp_header_t *) ((*ip6) + 1);
945 static bfd_udp_error_t
946 bfd_udp6_verify_transport (const ip6_header_t * ip6,
947 const udp_header_t * udp, const bfd_session_t * bs)
949 const bfd_udp_session_t *bus = &bs->udp;
950 const bfd_udp_key_t *key = &bus->key;
951 if (ip6->src_address.as_u64[0] != key->peer_addr.ip6.as_u64[0] &&
952 ip6->src_address.as_u64[1] != key->peer_addr.ip6.as_u64[1])
954 BFD_ERR ("IP src addr mismatch, got %U, expected %U",
955 format_ip6_address, ip6, format_ip6_address,
956 &key->peer_addr.ip6);
957 return BFD_UDP_ERROR_BAD;
959 if (ip6->dst_address.as_u64[0] != key->local_addr.ip6.as_u64[0] &&
960 ip6->dst_address.as_u64[1] != key->local_addr.ip6.as_u64[1])
962 BFD_ERR ("IP dst addr mismatch, got %U, expected %U",
963 format_ip6_address, ip6, format_ip6_address,
964 &key->local_addr.ip6);
965 return BFD_UDP_ERROR_BAD;
967 const u8 expected_hop_limit = 255;
968 if (ip6->hop_limit != expected_hop_limit)
970 BFD_ERR ("IPv6 unexpected hop-limit value %u, expected %u",
971 ip6->hop_limit, expected_hop_limit);
972 return BFD_UDP_ERROR_BAD;
974 if (clib_net_to_host_u16 (udp->src_port) < 49152)
976 BFD_ERR ("Invalid UDP src port %u, out of range <49152,65535>",
979 return BFD_UDP_ERROR_NONE;
982 static bfd_udp_error_t
983 bfd_udp6_scan (vlib_main_t * vm, vlib_node_runtime_t * rt,
984 vlib_buffer_t * b, bfd_session_t ** bs_out)
986 const bfd_pkt_t *pkt = vlib_buffer_get_current (b);
987 if (sizeof (*pkt) > b->current_length)
990 ("Payload size %d too small to hold bfd packet of minimum size %d",
991 b->current_length, sizeof (*pkt));
992 return BFD_UDP_ERROR_BAD;
996 bfd_udp6_find_headers (b, &ip6, &udp);
999 BFD_ERR ("Couldn't find ip6 or udp header");
1000 return BFD_UDP_ERROR_BAD;
1002 const u32 udp_payload_length = udp->length - sizeof (*udp);
1003 if (pkt->head.length > udp_payload_length)
1006 ("BFD packet length is larger than udp payload length (%u > %u)",
1007 pkt->head.length, udp_payload_length);
1008 return BFD_UDP_ERROR_BAD;
1010 if (!bfd_verify_pkt_common (pkt))
1012 return BFD_UDP_ERROR_BAD;
1014 bfd_session_t *bs = NULL;
1017 BFD_DBG ("Looking up BFD session using discriminator %u",
1019 bs = bfd_find_session_by_disc (bfd_udp_main.bfd_main, pkt->your_disc);
1024 memset (&key, 0, sizeof (key));
1025 key.sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
1026 key.local_addr.ip6.as_u64[0] = ip6->dst_address.as_u64[0];
1027 key.local_addr.ip6.as_u64[1] = ip6->dst_address.as_u64[1];
1028 key.peer_addr.ip6.as_u64[0] = ip6->src_address.as_u64[0];
1029 key.peer_addr.ip6.as_u64[1] = ip6->src_address.as_u64[1];
1030 BFD_DBG ("Looking up BFD session using key (sw_if_index=%u, local=%U, "
1032 key.sw_if_index, format_ip6_address, &key.local_addr,
1033 format_ip6_address, &key.peer_addr);
1034 bs = bfd_lookup_session (&bfd_udp_main, &key);
1038 BFD_ERR ("BFD session lookup failed - no session matches BFD pkt");
1039 return BFD_UDP_ERROR_BAD;
1041 BFD_DBG ("BFD session found, bs_idx=%u", bs->bs_idx);
1042 if (!bfd_verify_pkt_auth (pkt, b->current_length, bs))
1044 BFD_ERR ("Packet verification failed, dropping packet");
1045 return BFD_UDP_ERROR_BAD;
1047 bfd_udp_error_t err;
1048 if (BFD_UDP_ERROR_NONE != (err = bfd_udp6_verify_transport (ip6, udp, bs)))
1052 bfd_rpc_update_session (bs->bs_idx, pkt);
1054 return BFD_UDP_ERROR_NONE;
1058 * Process a frame of bfd packets
1059 * Expect 1 packet / frame
1062 bfd_udp_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
1063 vlib_frame_t * f, int is_ipv6)
1065 u32 n_left_from, *from;
1066 bfd_input_trace_t *t0;
1068 from = vlib_frame_vector_args (f); /* array of buffer indices */
1069 n_left_from = f->n_vectors; /* number of buffer indices */
1071 while (n_left_from > 0)
1078 b0 = vlib_get_buffer (vm, bi0);
1080 bfd_session_t *bs = NULL;
1082 /* If this pkt is traced, snapshot the data */
1083 if (b0->flags & VLIB_BUFFER_IS_TRACED)
1086 t0 = vlib_add_trace (vm, rt, b0, sizeof (*t0));
1087 len = (b0->current_length < sizeof (t0->data)) ? b0->current_length
1088 : sizeof (t0->data);
1090 clib_memcpy (t0->data, vlib_buffer_get_current (b0), len);
1093 /* scan this bfd pkt. error0 is the counter index to bmp */
1096 error0 = bfd_udp6_scan (vm, rt, b0, &bs);
1100 error0 = bfd_udp4_scan (vm, rt, b0, &bs);
1102 b0->error = rt->errors[error0];
1104 next0 = BFD_UDP_INPUT_NEXT_NORMAL;
1105 if (BFD_UDP_ERROR_NONE == error0)
1108 * if everything went fine, check for poll bit, if present, re-use
1109 * the buffer and based on (now updated) session parameters, send
1110 * the final packet back
1112 const bfd_pkt_t *pkt = vlib_buffer_get_current (b0);
1113 if (bfd_pkt_get_poll (pkt))
1115 bfd_init_final_control_frame (vm, b0, bfd_udp_main.bfd_main,
1119 vlib_node_increment_counter (vm, bfd_udp6_input_node.index,
1124 vlib_node_increment_counter (vm, bfd_udp4_input_node.index,
1127 next0 = BFD_UDP_INPUT_NEXT_REPLY;
1130 vlib_set_next_frame_buffer (vm, rt, next0, bi0);
1136 return f->n_vectors;
1140 bfd_udp4_input (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
1142 return bfd_udp_input (vm, rt, f, 0);
1146 * bfd input graph node declaration
1149 VLIB_REGISTER_NODE (bfd_udp4_input_node, static) = {
1150 .function = bfd_udp4_input,
1151 .name = "bfd-udp4-input",
1152 .vector_size = sizeof (u32),
1153 .type = VLIB_NODE_TYPE_INTERNAL,
1155 .n_errors = BFD_UDP_N_ERROR,
1156 .error_strings = bfd_udp_error_strings,
1158 .format_trace = bfd_input_format_trace,
1160 .n_next_nodes = BFD_UDP_INPUT_N_NEXT,
1163 [BFD_UDP_INPUT_NEXT_NORMAL] = "error-drop",
1164 [BFD_UDP_INPUT_NEXT_REPLY] = "ip4-lookup",
1170 bfd_udp6_input (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
1172 return bfd_udp_input (vm, rt, f, 1);
1176 VLIB_REGISTER_NODE (bfd_udp6_input_node, static) = {
1177 .function = bfd_udp6_input,
1178 .name = "bfd-udp6-input",
1179 .vector_size = sizeof (u32),
1180 .type = VLIB_NODE_TYPE_INTERNAL,
1182 .n_errors = BFD_UDP_N_ERROR,
1183 .error_strings = bfd_udp_error_strings,
1185 .format_trace = bfd_input_format_trace,
1187 .n_next_nodes = BFD_UDP_INPUT_N_NEXT,
1190 [BFD_UDP_INPUT_NEXT_NORMAL] = "error-drop",
1191 [BFD_UDP_INPUT_NEXT_REPLY] = "ip6-lookup",
1197 * Process a frame of bfd echo packets
1198 * Expect 1 packet / frame
1201 bfd_udp_echo_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
1202 vlib_frame_t * f, int is_ipv6)
1204 u32 n_left_from, *from;
1205 bfd_input_trace_t *t0;
1207 from = vlib_frame_vector_args (f); /* array of buffer indices */
1208 n_left_from = f->n_vectors; /* number of buffer indices */
1210 while (n_left_from > 0)
1217 b0 = vlib_get_buffer (vm, bi0);
1219 /* If this pkt is traced, snapshot the data */
1220 if (b0->flags & VLIB_BUFFER_IS_TRACED)
1223 t0 = vlib_add_trace (vm, rt, b0, sizeof (*t0));
1224 len = (b0->current_length < sizeof (t0->data)) ? b0->current_length
1225 : sizeof (t0->data);
1227 clib_memcpy (t0->data, vlib_buffer_get_current (b0), len);
1230 if (bfd_consume_echo_pkt (bfd_udp_main.bfd_main, b0))
1232 b0->error = rt->errors[BFD_UDP_ERROR_NONE];
1233 next0 = BFD_UDP_INPUT_NEXT_NORMAL;
1237 /* loop back the packet */
1238 b0->error = rt->errors[BFD_UDP_ERROR_NONE];
1241 vlib_node_increment_counter (vm, bfd_udp_echo6_input_node.index,
1246 vlib_node_increment_counter (vm, bfd_udp_echo4_input_node.index,
1249 next0 = BFD_UDP_INPUT_NEXT_REPLY;
1252 vlib_set_next_frame_buffer (vm, rt, next0, bi0);
1258 return f->n_vectors;
1262 bfd_udp_echo4_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
1265 return bfd_udp_echo_input (vm, rt, f, 0);
1269 bfd_echo_input_format_trace (u8 * s, va_list * args)
1271 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1272 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1273 const bfd_udp_echo_input_trace_t *t =
1274 va_arg (*args, bfd_udp_echo_input_trace_t *);
1275 if (t->len > STRUCT_SIZE_OF (bfd_pkt_t, head))
1277 s = format (s, "BFD ECHO:\n");
1278 s = format (s, " data: %U", format_hexdump, t->data, t->len);
1285 * bfd input graph node declaration
1288 VLIB_REGISTER_NODE (bfd_udp_echo4_input_node, static) = {
1289 .function = bfd_udp_echo4_input,
1290 .name = "bfd-udp-echo4-input",
1291 .vector_size = sizeof (u32),
1292 .type = VLIB_NODE_TYPE_INTERNAL,
1294 .n_errors = BFD_UDP_ECHO_N_ERROR,
1295 .error_strings = bfd_udp_error_strings,
1297 .format_trace = bfd_echo_input_format_trace,
1299 .n_next_nodes = BFD_UDP_INPUT_N_NEXT,
1302 [BFD_UDP_INPUT_NEXT_NORMAL] = "error-drop",
1303 [BFD_UDP_INPUT_NEXT_REPLY] = "ip4-lookup",
1309 bfd_udp_echo6_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
1312 return bfd_udp_echo_input (vm, rt, f, 1);
1316 VLIB_REGISTER_NODE (bfd_udp_echo6_input_node, static) = {
1317 .function = bfd_udp_echo6_input,
1318 .name = "bfd-udp-echo6-input",
1319 .vector_size = sizeof (u32),
1320 .type = VLIB_NODE_TYPE_INTERNAL,
1322 .n_errors = BFD_UDP_ECHO_N_ERROR,
1323 .error_strings = bfd_udp_echo_error_strings,
1325 .format_trace = bfd_echo_input_format_trace,
1327 .n_next_nodes = BFD_UDP_INPUT_N_NEXT,
1330 [BFD_UDP_INPUT_NEXT_NORMAL] = "error-drop",
1331 [BFD_UDP_INPUT_NEXT_REPLY] = "ip6-lookup",
1337 static clib_error_t *
1338 bfd_sw_interface_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
1340 // vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
1341 if (!(flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
1348 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (bfd_sw_interface_up_down);
1350 static clib_error_t *
1351 bfd_hw_interface_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
1353 if (flags & VNET_HW_INTERFACE_FLAG_LINK_UP)
1360 VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION (bfd_hw_interface_up_down);
1365 static clib_error_t *
1366 bfd_udp_init (vlib_main_t * vm)
1368 mhash_init (&bfd_udp_main.bfd_session_idx_by_bfd_key, sizeof (uword),
1369 sizeof (bfd_udp_key_t));
1370 bfd_udp_main.bfd_main = &bfd_main;
1371 bfd_udp_main.vnet_main = vnet_get_main ();
1372 udp_register_dst_port (vm, UDP_DST_PORT_bfd4, bfd_udp4_input_node.index, 1);
1373 udp_register_dst_port (vm, UDP_DST_PORT_bfd6, bfd_udp6_input_node.index, 0);
1374 udp_register_dst_port (vm, UDP_DST_PORT_bfd_echo4,
1375 bfd_udp_echo4_input_node.index, 1);
1376 udp_register_dst_port (vm, UDP_DST_PORT_bfd_echo6,
1377 bfd_udp_echo6_input_node.index, 0);
1381 VLIB_INIT_FUNCTION (bfd_udp_init);
1384 * fd.io coding-style-patch-verification: ON
1387 * eval: (c-set-style "gnu")