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;
54 /* node index of "ip4-arp" node */
56 /* node index of "ip6-discover-neighbor" node */
58 /* node index of "ip4-rewrite" node */
60 /* node index of "ip6-rewrite" node */
64 static vlib_node_registration_t bfd_udp4_input_node;
65 static vlib_node_registration_t bfd_udp6_input_node;
66 static vlib_node_registration_t bfd_udp_echo4_input_node;
67 static vlib_node_registration_t bfd_udp_echo6_input_node;
69 bfd_udp_main_t bfd_udp_main;
72 bfd_udp_set_echo_source (u32 sw_if_index)
74 vnet_sw_interface_t *sw_if =
75 vnet_get_sw_interface_safe (bfd_udp_main.vnet_main, sw_if_index);
78 bfd_udp_main.echo_source_sw_if_index = sw_if_index;
79 bfd_udp_main.echo_source_is_set = 1;
82 return VNET_API_ERROR_BFD_ENOENT;
86 bfd_udp_del_echo_source (u32 sw_if_index)
88 bfd_udp_main.echo_source_sw_if_index = ~0;
89 bfd_udp_main.echo_source_is_set = 0;
94 bfd_udp_is_echo_available (bfd_transport_e transport)
96 if (!bfd_udp_main.echo_source_is_set)
98 BFD_DBG ("UDP echo source not set - echo not available");
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
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)
111 if (BFD_TRANSPORT_UDP4 == transport)
113 ip4_main_t *im = &ip4_main;
114 ip_interface_address_t *ia = NULL;
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)
126 else if (BFD_TRANSPORT_UDP6 == transport)
128 ip6_main_t *im = &ip6_main;
129 ip_interface_address_t *ia = NULL;
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)
142 BFD_DBG ("No usable IP address for UDP echo - echo not available");
147 bfd_udp_bs_idx_to_sport (u32 bs_idx)
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.
157 return 49152 + bs_idx % (65535 - 49152 + 1);
161 bfd_udp_get_echo_src_ip4 (ip4_address_t * addr)
163 if (!bfd_udp_main.echo_source_is_set)
165 BFD_ERR ("cannot find ip4 address, echo source not set");
168 ip_interface_address_t *ia = NULL;
169 ip4_main_t *im = &ip4_main;
172 foreach_ip_interface_address (
173 &im->lookup_main, ia, bfd_udp_main.echo_source_sw_if_index,
174 0 /* honor unnumbered */, ({
176 ip_interface_address_get_address (&im->lookup_main, ia);
177 if (ia->address_length <= 31)
179 addr->as_u32 = clib_host_to_net_u32 (x->as_u32);
181 * flip the last bit to get a different address, might be network,
185 addr->as_u32 = clib_net_to_host_u32 (addr->as_u32);
190 BFD_ERR ("cannot find ip4 address, no usable address found");
195 bfd_udp_get_echo_src_ip6 (ip6_address_t * addr)
197 if (!bfd_udp_main.echo_source_is_set)
199 BFD_ERR ("cannot find ip6 address, echo source not set");
202 ip_interface_address_t *ia = NULL;
203 ip6_main_t *im = &ip6_main;
206 foreach_ip_interface_address (
207 &im->lookup_main, ia, bfd_udp_main.echo_source_sw_if_index,
208 0 /* honor unnumbered */, ({
210 ip_interface_address_get_address (&im->lookup_main, ia);
211 if (ia->address_length <= 127)
214 addr->as_u8[15] ^= 1; /* flip the last bit of the address */
219 BFD_ERR ("cannot find ip6 address, no usable address found");
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)
228 if (bfd_udp_main.echo_source_is_set)
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);
242 bfd_add_udp4_transport (vlib_main_t * vm, u32 bi, const bfd_session_t * bs,
245 const bfd_udp_session_t *bus = &bs->udp;
246 const bfd_udp_key_t *key = &bus->key;
247 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
249 b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
250 vnet_buffer (b)->ip.adj_index[VLIB_RX] = bus->adj_index;
251 vnet_buffer (b)->ip.adj_index[VLIB_TX] = bus->adj_index;
252 vnet_buffer (b)->sw_if_index[VLIB_RX] = 0;
253 vnet_buffer (b)->sw_if_index[VLIB_TX] = ~0;
259 ip4_udp_headers *headers = NULL;
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;
266 headers->udp.src_port =
267 clib_host_to_net_u16 (bfd_udp_bs_idx_to_sport (bs->bs_idx));
271 if (!(rv = bfd_udp_get_echo_src_ip4 (&headers->ip4.src_address)))
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);
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);
285 /* fix ip length, checksum and udp length */
286 const u16 ip_length = vlib_buffer_length_in_chain (vm, b);
288 headers->ip4.length = clib_host_to_net_u16 (ip_length);
289 headers->ip4.checksum = ip4_header_checksum (&headers->ip4);
291 const u16 udp_length = ip_length - (sizeof (headers->ip4));
292 headers->udp.length = clib_host_to_net_u16 (udp_length);
297 bfd_add_udp6_transport (vlib_main_t * vm, u32 bi, const bfd_session_t * bs,
300 const bfd_udp_session_t *bus = &bs->udp;
301 const bfd_udp_key_t *key = &bus->key;
302 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
304 b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
305 vnet_buffer (b)->ip.adj_index[VLIB_RX] = bus->adj_index;
306 vnet_buffer (b)->ip.adj_index[VLIB_TX] = bus->adj_index;
307 vnet_buffer (b)->sw_if_index[VLIB_RX] = 0;
308 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
314 ip6_udp_headers *headers = NULL;
315 vlib_buffer_advance (b, -sizeof (*headers));
316 headers = vlib_buffer_get_current (b);
317 memset (headers, 0, sizeof (*headers));
318 headers->ip6.ip_version_traffic_class_and_flow_label =
319 clib_host_to_net_u32 (0x6 << 28);
320 headers->ip6.hop_limit = 255;
321 headers->ip6.protocol = IP_PROTOCOL_UDP;
322 headers->udp.src_port =
323 clib_host_to_net_u16 (bfd_udp_bs_idx_to_sport (bs->bs_idx));
327 if (!(rv = bfd_udp_get_echo_src_ip6 (&headers->ip6.src_address)))
331 clib_memcpy (&headers->ip6.dst_address, &key->local_addr.ip6,
332 sizeof (headers->ip6.dst_address));
334 headers->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_bfd_echo6);
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);
345 /* fix ip payload length and udp length */
346 const u16 udp_length =
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;
351 /* IPv6 UDP checksum is mandatory */
353 headers->udp.checksum =
354 ip6_tcp_udp_icmp_compute_checksum (vm, b, &headers->ip6, &bogus);
356 if (headers->udp.checksum == 0)
358 headers->udp.checksum = 0xffff;
364 bfd_create_frame_to_next_node (vlib_main_t * vm, u32 bi, u32 next_node)
366 vlib_frame_t *f = vlib_get_frame_to_node (vm, next_node);
367 u32 *to_next = vlib_frame_vector_args (f);
370 vlib_put_frame_to_node (vm, next_node, f);
374 bfd_udp_calc_next_node (const struct bfd_session_s *bs, u32 * next_node)
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)
380 case IP_LOOKUP_NEXT_ARP:
381 switch (bs->transport)
383 case BFD_TRANSPORT_UDP4:
384 *next_node = bfd_udp_main.ip4_arp_idx;
386 case BFD_TRANSPORT_UDP6:
387 *next_node = bfd_udp_main.ip6_ndp_idx;
391 case IP_LOOKUP_NEXT_REWRITE:
392 switch (bs->transport)
394 case BFD_TRANSPORT_UDP4:
395 *next_node = bfd_udp_main.ip4_rewrite_idx;
397 case BFD_TRANSPORT_UDP6:
398 *next_node = bfd_udp_main.ip6_rewrite_idx;
410 bfd_transport_udp4 (vlib_main_t * vm, u32 bi, const struct bfd_session_s *bs)
413 int rv = bfd_udp_calc_next_node (bs, &next_node);
416 bfd_create_frame_to_next_node (vm, bi, next_node);
422 bfd_transport_udp6 (vlib_main_t * vm, u32 bi, const struct bfd_session_s *bs)
425 int rv = bfd_udp_calc_next_node (bs, &next_node);
428 bfd_create_frame_to_next_node (vm, bi, next_node);
433 static bfd_session_t *
434 bfd_lookup_session (bfd_udp_main_t * bum, const bfd_udp_key_t * key)
436 uword *p = mhash_get (&bum->bfd_session_idx_by_bfd_key, key);
439 return bfd_find_session_by_idx (bum->bfd_main, *p);
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)
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];
457 static vnet_api_error_t
458 bfd_udp_add_session_internal (bfd_udp_main_t * bum, u32 sw_if_index,
459 u32 desired_min_tx_usec,
460 u32 required_min_rx_usec, u8 detect_mult,
461 const ip46_address_t * local_addr,
462 const ip46_address_t * peer_addr,
463 bfd_session_t ** bs_out)
465 /* get a pool entry and if we end up not needing it, give it back */
466 bfd_transport_e t = BFD_TRANSPORT_UDP4;
467 if (!ip46_address_is_ip4 (local_addr))
469 t = BFD_TRANSPORT_UDP6;
471 bfd_session_t *bs = bfd_get_session (bum->bfd_main, t);
474 bfd_put_session (bum->bfd_main, bs);
475 return VNET_API_ERROR_BFD_EAGAIN;
477 bfd_udp_session_t *bus = &bs->udp;
478 memset (bus, 0, sizeof (*bus));
479 bfd_udp_key_t *key = &bus->key;
480 bfd_udp_key_init (key, sw_if_index, local_addr, peer_addr);
481 const bfd_session_t *tmp = bfd_lookup_session (bum, key);
484 clib_warning ("duplicate bfd-udp session, existing bs_idx=%d",
486 bfd_put_session (bum->bfd_main, bs);
487 return VNET_API_ERROR_BFD_EEXIST;
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",
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);
494 if (BFD_TRANSPORT_UDP4 == t)
496 bus->adj_index = adj_nbr_add_or_lock (FIB_PROTOCOL_IP4, VNET_LINK_IP4,
499 BFD_DBG ("adj_nbr_add_or_lock(FIB_PROTOCOL_IP4, VNET_LINK_IP4, %U, %d) "
500 "returns %d", format_ip46_address, &key->peer_addr,
501 IP46_TYPE_ANY, key->sw_if_index, bus->adj_index);
505 bus->adj_index = adj_nbr_add_or_lock (FIB_PROTOCOL_IP6, VNET_LINK_IP6,
508 BFD_DBG ("adj_nbr_add_or_lock(FIB_PROTOCOL_IP6, VNET_LINK_IP6, %U, %d) "
509 "returns %d", format_ip46_address, &key->peer_addr,
510 IP46_TYPE_ANY, key->sw_if_index, bus->adj_index);
513 return bfd_session_set_params (bum->bfd_main, bs, desired_min_tx_usec,
514 required_min_rx_usec, detect_mult);
517 static vnet_api_error_t
518 bfd_udp_validate_api_input (u32 sw_if_index,
519 const ip46_address_t * local_addr,
520 const ip46_address_t * peer_addr)
522 vnet_sw_interface_t *sw_if =
523 vnet_get_sw_interface_safe (bfd_udp_main.vnet_main, sw_if_index);
524 u8 local_ip_valid = 0;
525 ip_interface_address_t *ia = NULL;
528 clib_warning ("got NULL sw_if");
529 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
531 if (ip46_address_is_ip4 (local_addr))
533 if (!ip46_address_is_ip4 (peer_addr))
535 clib_warning ("IP family mismatch");
536 return VNET_API_ERROR_INVALID_ARGUMENT;
538 ip4_main_t *im = &ip4_main;
541 foreach_ip_interface_address (
542 &im->lookup_main, ia, sw_if_index, 0 /* honor unnumbered */, ({
544 ip_interface_address_get_address (&im->lookup_main, ia);
545 if (x->as_u32 == local_addr->ip4.as_u32)
547 /* valid address for this interface */
556 if (ip46_address_is_ip4 (peer_addr))
558 clib_warning ("IP family mismatch");
559 return VNET_API_ERROR_INVALID_ARGUMENT;
561 ip6_main_t *im = &ip6_main;
563 foreach_ip_interface_address (
564 &im->lookup_main, ia, sw_if_index, 0 /* honor unnumbered */, ({
566 ip_interface_address_get_address (&im->lookup_main, ia);
567 if (local_addr->ip6.as_u64[0] == x->as_u64[0] &&
568 local_addr->ip6.as_u64[1] == x->as_u64[1])
570 /* valid address for this interface */
580 clib_warning ("address not found on interface");
581 return VNET_API_ERROR_ADDRESS_NOT_FOUND_FOR_INTERFACE;
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)
593 vnet_api_error_t rv =
594 bfd_udp_validate_api_input (sw_if_index, local_addr, peer_addr);
597 bfd_udp_main_t *bum = &bfd_udp_main;
599 bfd_udp_key_init (&key, sw_if_index, local_addr, peer_addr);
600 bfd_session_t *bs = bfd_lookup_session (bum, &key);
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;
617 static vnet_api_error_t
618 bfd_api_verify_common (u32 sw_if_index, u32 desired_min_tx_usec,
619 u32 required_min_rx_usec, u8 detect_mult,
620 const ip46_address_t * local_addr,
621 const ip46_address_t * peer_addr)
623 vnet_api_error_t rv =
624 bfd_udp_validate_api_input (sw_if_index, local_addr, peer_addr);
631 clib_warning ("detect_mult < 1");
632 return VNET_API_ERROR_INVALID_ARGUMENT;
634 if (desired_min_tx_usec < 1)
636 clib_warning ("desired_min_tx_usec < 1");
637 return VNET_API_ERROR_INVALID_ARGUMENT;
643 bfd_udp_del_session_internal (bfd_session_t * bs)
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);
653 bfd_udp_add_session (u32 sw_if_index, const ip46_address_t * local_addr,
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,
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);
663 bfd_session_t *bs = NULL;
667 bfd_udp_add_session_internal (&bfd_udp_main, sw_if_index,
669 required_min_rx_usec, detect_mult,
670 local_addr, peer_addr, &bs);
672 if (!rv && is_authenticated)
675 rv = bfd_auth_activate (bs, conf_key_id, bfd_key_id,
676 0 /* is not delayed */ );
678 clib_warning ("SSL missing, cannot add authenticated BFD session");
679 rv = VNET_API_ERROR_BFD_NOTSUPP;
683 bfd_udp_del_session_internal (bs);
688 bfd_session_start (bfd_udp_main.bfd_main, bs);
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)
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,
710 return bfd_session_set_params (bfd_udp_main.bfd_main, bs,
711 desired_min_tx_usec, required_min_rx_usec,
716 bfd_udp_del_session (u32 sw_if_index,
717 const ip46_address_t * local_addr,
718 const ip46_address_t * peer_addr)
720 bfd_session_t *bs = NULL;
721 vnet_api_error_t rv =
722 bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr,
728 bfd_udp_del_session_internal (bs);
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)
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,
745 bfd_session_set_flags (bs, admin_up_down);
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)
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,
764 return bfd_auth_activate (bs, conf_key_id, key_id, is_delayed);
766 clib_warning ("SSL missing, cannot activate BFD authentication");
767 return VNET_API_ERROR_BFD_NOTSUPP;
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)
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,
784 return bfd_auth_deactivate (bs, is_delayed);
789 BFD_UDP_INPUT_NEXT_NORMAL,
790 BFD_UDP_INPUT_NEXT_REPLY_ARP,
791 BFD_UDP_INPUT_NEXT_REPLY_REWRITE,
792 BFD_UDP_INPUT_N_NEXT,
793 } bfd_udp_input_next_t;
795 /* Packet counters - BFD control frames */
796 #define foreach_bfd_udp_error(F) \
797 F (NONE, "good bfd packets (processed)") \
798 F (BAD, "invalid bfd packets")
800 #define F(sym, string) static char BFD_UDP_ERR_##sym##_STR[] = string;
801 foreach_bfd_udp_error (F);
804 static char *bfd_udp_error_strings[] = {
805 #define F(sym, string) BFD_UDP_ERR_##sym##_STR,
806 foreach_bfd_udp_error (F)
812 #define F(sym, str) BFD_UDP_ERROR_##sym,
813 foreach_bfd_udp_error (F)
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")
823 #define F(sym, string) static char BFD_UDP_ECHO_ERR_##sym##_STR[] = string;
824 foreach_bfd_udp_echo_error (F);
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)
835 #define F(sym, str) BFD_UDP_ECHO_ERROR_##sym,
836 foreach_bfd_udp_echo_error (F)
838 BFD_UDP_ECHO_N_ERROR,
839 } bfd_udp_echo_error_t;
842 bfd_udp4_find_headers (vlib_buffer_t * b, ip4_header_t ** ip4,
845 /* sanity check first */
846 const i32 start = vnet_buffer (b)->l3_hdr_offset;
847 if (start < 0 && start < sizeof (b->pre_data))
849 BFD_ERR ("Start of ip header is before pre_data, ignoring");
854 *ip4 = (ip4_header_t *) (b->data + start);
855 if ((u8 *) * ip4 > (u8 *) vlib_buffer_get_current (b))
857 BFD_ERR ("Start of ip header is beyond current data, ignoring");
862 *udp = (udp_header_t *) ((*ip4) + 1);
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)
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)
873 BFD_ERR ("IPv4 src addr mismatch, got %U, expected %U",
874 format_ip4_address, ip4->src_address.as_u8, format_ip4_address,
875 key->peer_addr.ip4.as_u8);
876 return BFD_UDP_ERROR_BAD;
878 if (ip4->dst_address.as_u32 != key->local_addr.ip4.as_u32)
880 BFD_ERR ("IPv4 dst addr mismatch, got %U, expected %U",
881 format_ip4_address, ip4->dst_address.as_u8, format_ip4_address,
882 key->local_addr.ip4.as_u8);
883 return BFD_UDP_ERROR_BAD;
885 const u8 expected_ttl = 255;
886 if (ip4->ttl != expected_ttl)
888 BFD_ERR ("IPv4 unexpected TTL value %u, expected %u", ip4->ttl,
890 return BFD_UDP_ERROR_BAD;
892 if (clib_net_to_host_u16 (udp->src_port) < 49152)
894 BFD_ERR ("Invalid UDP src port %u, out of range <49152,65535>",
897 return BFD_UDP_ERROR_NONE;
907 bfd_rpc_update_session_cb (const bfd_rpc_update_t * a)
909 bfd_consume_pkt (bfd_udp_main.bfd_main, &a->pkt, a->bs_idx);
913 bfd_rpc_update_session (u32 bs_idx, const bfd_pkt_t * pkt)
915 /* packet length was already verified to be correct by the caller */
916 const u32 data_size = sizeof (bfd_rpc_update_t) -
917 STRUCT_SIZE_OF (bfd_rpc_update_t, pkt) + pkt->head.length;
919 bfd_rpc_update_t *update = (bfd_rpc_update_t *) data;
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);
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)
929 const bfd_pkt_t *pkt = vlib_buffer_get_current (b);
930 if (sizeof (*pkt) > b->current_length)
933 ("Payload size %d too small to hold bfd packet of minimum size %d",
934 b->current_length, sizeof (*pkt));
935 return BFD_UDP_ERROR_BAD;
939 bfd_udp4_find_headers (b, &ip4, &udp);
942 BFD_ERR ("Couldn't find ip4 or udp header");
943 return BFD_UDP_ERROR_BAD;
945 const u32 udp_payload_length = udp->length - sizeof (*udp);
946 if (pkt->head.length > udp_payload_length)
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;
953 if (!bfd_verify_pkt_common (pkt))
955 return BFD_UDP_ERROR_BAD;
957 bfd_session_t *bs = NULL;
960 BFD_DBG ("Looking up BFD session using discriminator %u",
962 bs = bfd_find_session_by_disc (bfd_udp_main.bfd_main, pkt->your_disc);
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, "
973 key.sw_if_index, format_ip4_address, key.local_addr.ip4.as_u8,
974 format_ip4_address, key.peer_addr.ip4.as_u8);
975 bs = bfd_lookup_session (&bfd_udp_main, &key);
979 BFD_ERR ("BFD session lookup failed - no session matches BFD pkt");
980 return BFD_UDP_ERROR_BAD;
982 BFD_DBG ("BFD session found, bs_idx=%u", bs->bs_idx);
983 if (!bfd_verify_pkt_auth (pkt, b->current_length, bs))
985 BFD_ERR ("Packet verification failed, dropping packet");
986 return BFD_UDP_ERROR_BAD;
989 if (BFD_UDP_ERROR_NONE != (err = bfd_udp4_verify_transport (ip4, udp, bs)))
993 bfd_rpc_update_session (bs->bs_idx, pkt);
995 return BFD_UDP_ERROR_NONE;
999 bfd_udp6_find_headers (vlib_buffer_t * b, ip6_header_t ** ip6,
1000 udp_header_t ** udp)
1002 /* sanity check first */
1003 const i32 start = vnet_buffer (b)->l3_hdr_offset;
1004 if (start < 0 && start < sizeof (b->pre_data))
1006 BFD_ERR ("Start of ip header is before pre_data, ignoring");
1011 *ip6 = (ip6_header_t *) (b->data + start);
1012 if ((u8 *) * ip6 > (u8 *) vlib_buffer_get_current (b))
1014 BFD_ERR ("Start of ip header is beyond current data, ignoring");
1019 if ((*ip6)->protocol != IP_PROTOCOL_UDP)
1021 BFD_ERR ("Unexpected protocol in IPv6 header '%u', expected '%u' (== "
1022 "IP_PROTOCOL_UDP)", (*ip6)->protocol, IP_PROTOCOL_UDP);
1027 *udp = (udp_header_t *) ((*ip6) + 1);
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)
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])
1039 BFD_ERR ("IP src addr mismatch, got %U, expected %U",
1040 format_ip6_address, ip6, format_ip6_address,
1041 &key->peer_addr.ip6);
1042 return BFD_UDP_ERROR_BAD;
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])
1047 BFD_ERR ("IP dst addr mismatch, got %U, expected %U",
1048 format_ip6_address, ip6, format_ip6_address,
1049 &key->local_addr.ip6);
1050 return BFD_UDP_ERROR_BAD;
1052 const u8 expected_hop_limit = 255;
1053 if (ip6->hop_limit != expected_hop_limit)
1055 BFD_ERR ("IPv6 unexpected hop-limit value %u, expected %u",
1056 ip6->hop_limit, expected_hop_limit);
1057 return BFD_UDP_ERROR_BAD;
1059 if (clib_net_to_host_u16 (udp->src_port) < 49152)
1061 BFD_ERR ("Invalid UDP src port %u, out of range <49152,65535>",
1064 return BFD_UDP_ERROR_NONE;
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)
1071 const bfd_pkt_t *pkt = vlib_buffer_get_current (b);
1072 if (sizeof (*pkt) > b->current_length)
1075 ("Payload size %d too small to hold bfd packet of minimum size %d",
1076 b->current_length, sizeof (*pkt));
1077 return BFD_UDP_ERROR_BAD;
1081 bfd_udp6_find_headers (b, &ip6, &udp);
1084 BFD_ERR ("Couldn't find ip6 or udp header");
1085 return BFD_UDP_ERROR_BAD;
1087 const u32 udp_payload_length = udp->length - sizeof (*udp);
1088 if (pkt->head.length > udp_payload_length)
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;
1095 if (!bfd_verify_pkt_common (pkt))
1097 return BFD_UDP_ERROR_BAD;
1099 bfd_session_t *bs = NULL;
1102 BFD_DBG ("Looking up BFD session using discriminator %u",
1104 bs = bfd_find_session_by_disc (bfd_udp_main.bfd_main, pkt->your_disc);
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, "
1117 key.sw_if_index, format_ip6_address, &key.local_addr,
1118 format_ip6_address, &key.peer_addr);
1119 bs = bfd_lookup_session (&bfd_udp_main, &key);
1123 BFD_ERR ("BFD session lookup failed - no session matches BFD pkt");
1124 return BFD_UDP_ERROR_BAD;
1126 BFD_DBG ("BFD session found, bs_idx=%u", bs->bs_idx);
1127 if (!bfd_verify_pkt_auth (pkt, b->current_length, bs))
1129 BFD_ERR ("Packet verification failed, dropping packet");
1130 return BFD_UDP_ERROR_BAD;
1132 bfd_udp_error_t err;
1133 if (BFD_UDP_ERROR_NONE != (err = bfd_udp6_verify_transport (ip6, udp, bs)))
1137 bfd_rpc_update_session (bs->bs_idx, pkt);
1139 return BFD_UDP_ERROR_NONE;
1143 * Process a frame of bfd packets
1144 * Expect 1 packet / frame
1147 bfd_udp_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
1148 vlib_frame_t * f, int is_ipv6)
1150 u32 n_left_from, *from;
1151 bfd_input_trace_t *t0;
1153 from = vlib_frame_vector_args (f); /* array of buffer indices */
1154 n_left_from = f->n_vectors; /* number of buffer indices */
1156 while (n_left_from > 0)
1163 b0 = vlib_get_buffer (vm, bi0);
1165 bfd_session_t *bs = NULL;
1167 /* If this pkt is traced, snapshot the data */
1168 if (b0->flags & VLIB_BUFFER_IS_TRACED)
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);
1175 clib_memcpy (t0->data, vlib_buffer_get_current (b0), len);
1178 /* scan this bfd pkt. error0 is the counter index to bmp */
1181 error0 = bfd_udp6_scan (vm, rt, b0, &bs);
1185 error0 = bfd_udp4_scan (vm, rt, b0, &bs);
1187 b0->error = rt->errors[error0];
1189 next0 = BFD_UDP_INPUT_NEXT_NORMAL;
1190 if (BFD_UDP_ERROR_NONE == error0)
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
1197 const bfd_pkt_t *pkt = vlib_buffer_get_current (b0);
1198 if (bfd_pkt_get_poll (pkt))
1200 b0->current_data = 0;
1201 b0->current_length = 0;
1202 memset (vnet_buffer (b0), 0, sizeof (*vnet_buffer (b0)));
1203 bfd_init_final_control_frame (vm, b0, bfd_udp_main.bfd_main, bs,
1207 vlib_node_increment_counter (vm, bfd_udp6_input_node.index,
1212 vlib_node_increment_counter (vm, bfd_udp4_input_node.index,
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)
1219 case IP_LOOKUP_NEXT_ARP:
1220 next0 = BFD_UDP_INPUT_NEXT_REPLY_ARP;
1222 case IP_LOOKUP_NEXT_REWRITE:
1223 next0 = BFD_UDP_INPUT_NEXT_REPLY_REWRITE;
1231 vlib_set_next_frame_buffer (vm, rt, next0, bi0);
1237 return f->n_vectors;
1241 bfd_udp4_input (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
1243 return bfd_udp_input (vm, rt, f, 0);
1247 * bfd input graph node declaration
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,
1256 .n_errors = BFD_UDP_N_ERROR,
1257 .error_strings = bfd_udp_error_strings,
1259 .format_trace = bfd_input_format_trace,
1261 .n_next_nodes = BFD_UDP_INPUT_N_NEXT,
1264 [BFD_UDP_INPUT_NEXT_NORMAL] = "error-drop",
1265 [BFD_UDP_INPUT_NEXT_REPLY_ARP] = "ip4-arp",
1266 [BFD_UDP_INPUT_NEXT_REPLY_REWRITE] = "ip4-lookup",
1272 bfd_udp6_input (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
1274 return bfd_udp_input (vm, rt, f, 1);
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,
1284 .n_errors = BFD_UDP_N_ERROR,
1285 .error_strings = bfd_udp_error_strings,
1287 .format_trace = bfd_input_format_trace,
1289 .n_next_nodes = BFD_UDP_INPUT_N_NEXT,
1292 [BFD_UDP_INPUT_NEXT_NORMAL] = "error-drop",
1293 [BFD_UDP_INPUT_NEXT_REPLY_ARP] = "ip6-discover-neighbor",
1294 [BFD_UDP_INPUT_NEXT_REPLY_REWRITE] = "ip6-lookup",
1300 * Process a frame of bfd echo packets
1301 * Expect 1 packet / frame
1304 bfd_udp_echo_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
1305 vlib_frame_t * f, int is_ipv6)
1307 u32 n_left_from, *from;
1308 bfd_input_trace_t *t0;
1310 from = vlib_frame_vector_args (f); /* array of buffer indices */
1311 n_left_from = f->n_vectors; /* number of buffer indices */
1313 while (n_left_from > 0)
1320 b0 = vlib_get_buffer (vm, bi0);
1322 /* If this pkt is traced, snapshot the data */
1323 if (b0->flags & VLIB_BUFFER_IS_TRACED)
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);
1330 clib_memcpy (t0->data, vlib_buffer_get_current (b0), len);
1333 if (bfd_consume_echo_pkt (bfd_udp_main.bfd_main, b0))
1335 b0->error = rt->errors[BFD_UDP_ERROR_NONE];
1336 next0 = BFD_UDP_INPUT_NEXT_NORMAL;
1340 /* loop back the packet */
1341 b0->error = rt->errors[BFD_UDP_ERROR_NONE];
1344 vlib_node_increment_counter (vm, bfd_udp_echo6_input_node.index,
1349 vlib_node_increment_counter (vm, bfd_udp_echo4_input_node.index,
1352 next0 = BFD_UDP_INPUT_NEXT_REPLY_REWRITE;
1355 vlib_set_next_frame_buffer (vm, rt, next0, bi0);
1361 return f->n_vectors;
1365 bfd_udp_echo4_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
1368 return bfd_udp_echo_input (vm, rt, f, 0);
1372 bfd_echo_input_format_trace (u8 * s, va_list * args)
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))
1380 s = format (s, "BFD ECHO:\n");
1381 s = format (s, " data: %U", format_hexdump, t->data, t->len);
1388 * bfd input graph node declaration
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,
1397 .n_errors = BFD_UDP_ECHO_N_ERROR,
1398 .error_strings = bfd_udp_error_strings,
1400 .format_trace = bfd_echo_input_format_trace,
1402 .n_next_nodes = BFD_UDP_INPUT_N_NEXT,
1405 [BFD_UDP_INPUT_NEXT_NORMAL] = "error-drop",
1406 [BFD_UDP_INPUT_NEXT_REPLY_ARP] = "ip4-arp",
1407 [BFD_UDP_INPUT_NEXT_REPLY_REWRITE] = "ip4-lookup",
1413 bfd_udp_echo6_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
1416 return bfd_udp_echo_input (vm, rt, f, 1);
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,
1426 .n_errors = BFD_UDP_ECHO_N_ERROR,
1427 .error_strings = bfd_udp_echo_error_strings,
1429 .format_trace = bfd_echo_input_format_trace,
1431 .n_next_nodes = BFD_UDP_INPUT_N_NEXT,
1434 [BFD_UDP_INPUT_NEXT_NORMAL] = "error-drop",
1435 [BFD_UDP_INPUT_NEXT_REPLY_ARP] = "ip6-discover-neighbor",
1436 [BFD_UDP_INPUT_NEXT_REPLY_REWRITE] = "ip6-lookup",
1442 static clib_error_t *
1443 bfd_udp_sw_if_add_del (vnet_main_t * vnm, u32 sw_if_index, u32 is_create)
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,
1451 pool_foreach (bs, bfd_udp_main.bfd_main->sessions,
1453 if (bs->transport != BFD_TRANSPORT_UDP4 &&
1454 bs->transport != BFD_TRANSPORT_UDP6)
1457 if (bs->udp.key.sw_if_index != sw_if_index)
1460 vec_add1 (to_be_freed, bs);}
1464 vec_foreach (bs, to_be_freed)
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);
1474 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (bfd_udp_sw_if_add_del);
1479 static clib_error_t *
1480 bfd_udp_init (vlib_main_t * vm)
1482 mhash_init (&bfd_udp_main.bfd_session_idx_by_bfd_key, sizeof (uword),
1483 sizeof (bfd_udp_key_t));
1484 bfd_udp_main.bfd_main = &bfd_main;
1485 bfd_udp_main.vnet_main = vnet_get_main ();
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);
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);
1492 vlib_node_t *node = vlib_get_node_by_name (vm, (u8 *) "ip4-arp");
1494 bfd_udp_main.ip4_arp_idx = node->index;
1495 node = vlib_get_node_by_name (vm, (u8 *) "ip6-discover-neighbor");
1497 bfd_udp_main.ip6_ndp_idx = node->index;
1498 node = vlib_get_node_by_name (vm, (u8 *) "ip4-rewrite");
1500 bfd_udp_main.ip4_rewrite_idx = node->index;
1501 node = vlib_get_node_by_name (vm, (u8 *) "ip6-rewrite");
1503 bfd_udp_main.ip6_rewrite_idx = node->index;
1508 VLIB_INIT_FUNCTION (bfd_udp_init);
1511 * fd.io coding-style-patch-verification: ON
1514 * eval: (c-set-style "gnu")