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.
15 #include <vppinfra/types.h>
16 #include <vlibmemory/api.h>
17 #include <vlib/vlib.h>
18 #include <vlib/buffer.h>
19 #include <vnet/ip/format.h>
20 #include <vnet/ethernet/packet.h>
21 #include <vnet/ip/udp_packet.h>
22 #include <vnet/ip/lookup.h>
23 #include <vnet/ip/icmp46_packet.h>
24 #include <vnet/ip/ip4.h>
25 #include <vnet/ip/ip6.h>
26 #include <vnet/ip/udp.h>
27 #include <vnet/ip/ip6_packet.h>
28 #include <vnet/adj/adj.h>
29 #include <vnet/adj/adj_nbr.h>
30 #include <vnet/bfd/bfd_debug.h>
31 #include <vnet/bfd/bfd_udp.h>
32 #include <vnet/bfd/bfd_main.h>
33 #include <vnet/bfd/bfd_api.h>
38 /* hashmap - bfd session index by bfd key - used for CLI/API lookup, where
39 * discriminator is unknown */
40 mhash_t bfd_session_idx_by_bfd_key;
43 static vlib_node_registration_t bfd_udp4_input_node;
44 static vlib_node_registration_t bfd_udp6_input_node;
45 static vlib_node_registration_t bfd_udp_echo4_input_node;
46 static vlib_node_registration_t bfd_udp_echo6_input_node;
48 bfd_udp_main_t bfd_udp_main;
51 bfd_udp_bs_idx_to_sport (u32 bs_idx)
53 /* The source port MUST be in the range 49152 through 65535. The same UDP
54 * source port number MUST be used for all BFD Control packets associated
55 * with a particular session. The source port number SHOULD be unique among
56 * all BFD sessions on the system. If more than 16384 BFD sessions are
57 * simultaneously active, UDP source port numbers MAY be reused on
58 * multiple sessions, but the number of distinct uses of the same UDP
59 * source port number SHOULD be minimized.
61 return 49152 + bs_idx % (65535 - 49152 + 1);
65 bfd_add_udp4_transport (vlib_main_t * vm, vlib_buffer_t * b,
66 const bfd_session_t * bs)
68 const bfd_udp_session_t *bus = &bs->udp;
69 const bfd_udp_key_t *key = &bus->key;
71 b->flags |= VNET_BUFFER_LOCALLY_ORIGINATED;
72 vnet_buffer (b)->ip.adj_index[VLIB_RX] = bus->adj_index;
73 vnet_buffer (b)->ip.adj_index[VLIB_TX] = bus->adj_index;
79 ip4_udp_headers *headers = NULL;
80 vlib_buffer_advance (b, -sizeof (*headers));
81 headers = vlib_buffer_get_current (b);
82 memset (headers, 0, sizeof (*headers));
83 headers->ip4.ip_version_and_header_length = 0x45;
84 headers->ip4.ttl = 255;
85 headers->ip4.protocol = IP_PROTOCOL_UDP;
86 headers->ip4.src_address.as_u32 = key->local_addr.ip4.as_u32;
87 headers->ip4.dst_address.as_u32 = key->peer_addr.ip4.as_u32;
89 headers->udp.src_port =
90 clib_host_to_net_u16 (bfd_udp_bs_idx_to_sport (bs->bs_idx));
91 headers->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_bfd4);
93 /* fix ip length, checksum and udp length */
94 const u16 ip_length = vlib_buffer_length_in_chain (vm, b);
96 headers->ip4.length = clib_host_to_net_u16 (ip_length);
97 headers->ip4.checksum = ip4_header_checksum (&headers->ip4);
99 const u16 udp_length = ip_length - (sizeof (headers->ip4));
100 headers->udp.length = clib_host_to_net_u16 (udp_length);
104 bfd_add_udp6_transport (vlib_main_t * vm, vlib_buffer_t * b,
105 const bfd_session_t * bs)
107 const bfd_udp_session_t *bus = &bs->udp;
108 const bfd_udp_key_t *key = &bus->key;
110 b->flags |= VNET_BUFFER_LOCALLY_ORIGINATED;
111 vnet_buffer (b)->ip.adj_index[VLIB_RX] = bus->adj_index;
112 vnet_buffer (b)->ip.adj_index[VLIB_TX] = bus->adj_index;
118 ip6_udp_headers *headers = NULL;
119 vlib_buffer_advance (b, -sizeof (*headers));
120 headers = vlib_buffer_get_current (b);
121 memset (headers, 0, sizeof (*headers));
122 headers->ip6.ip_version_traffic_class_and_flow_label =
123 clib_host_to_net_u32 (0x6 << 28);
124 headers->ip6.hop_limit = 255;
125 headers->ip6.protocol = IP_PROTOCOL_UDP;
126 clib_memcpy (&headers->ip6.src_address, &key->local_addr.ip6,
127 sizeof (headers->ip6.src_address));
128 clib_memcpy (&headers->ip6.dst_address, &key->peer_addr.ip6,
129 sizeof (headers->ip6.dst_address));
131 headers->udp.src_port =
132 clib_host_to_net_u16 (bfd_udp_bs_idx_to_sport (bs->bs_idx));
133 headers->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_bfd6);
135 /* fix ip payload length and udp length */
136 const u16 udp_length =
137 vlib_buffer_length_in_chain (vm, b) - (sizeof (headers->ip6));
138 headers->udp.length = clib_host_to_net_u16 (udp_length);
139 headers->ip6.payload_length = headers->udp.length;
141 /* IPv6 UDP checksum is mandatory */
143 headers->udp.checksum =
144 ip6_tcp_udp_icmp_compute_checksum (vm, b, &headers->ip6, &bogus);
146 if (headers->udp.checksum == 0)
148 headers->udp.checksum = 0xffff;
152 static bfd_session_t *
153 bfd_lookup_session (bfd_udp_main_t * bum, const bfd_udp_key_t * key)
155 uword *p = mhash_get (&bum->bfd_session_idx_by_bfd_key, key);
158 return bfd_find_session_by_idx (bum->bfd_main, *p);
164 bfd_udp_key_init (bfd_udp_key_t * key, u32 sw_if_index,
165 const ip46_address_t * local_addr,
166 const ip46_address_t * peer_addr)
168 memset (key, 0, sizeof (*key));
169 key->sw_if_index = sw_if_index;
170 key->local_addr.as_u64[0] = local_addr->as_u64[0];
171 key->local_addr.as_u64[1] = local_addr->as_u64[1];
172 key->peer_addr.as_u64[0] = peer_addr->as_u64[0];
173 key->peer_addr.as_u64[1] = peer_addr->as_u64[1];
176 static vnet_api_error_t
177 bfd_udp_add_session_internal (bfd_udp_main_t * bum, u32 sw_if_index,
178 u32 desired_min_tx_usec,
179 u32 required_min_rx_usec, u8 detect_mult,
180 const ip46_address_t * local_addr,
181 const ip46_address_t * peer_addr,
182 bfd_session_t ** bs_out)
184 /* get a pool entry and if we end up not needing it, give it back */
185 bfd_transport_t t = BFD_TRANSPORT_UDP4;
186 if (!ip46_address_is_ip4 (local_addr))
188 t = BFD_TRANSPORT_UDP6;
190 bfd_session_t *bs = bfd_get_session (bum->bfd_main, t);
191 bfd_udp_session_t *bus = &bs->udp;
192 memset (bus, 0, sizeof (*bus));
193 bfd_udp_key_t *key = &bus->key;
194 bfd_udp_key_init (key, sw_if_index, local_addr, peer_addr);
195 const bfd_session_t *tmp = bfd_lookup_session (bum, key);
198 clib_warning ("duplicate bfd-udp session, existing bs_idx=%d",
200 bfd_put_session (bum->bfd_main, bs);
201 return VNET_API_ERROR_BFD_EEXIST;
203 mhash_set (&bum->bfd_session_idx_by_bfd_key, key, bs->bs_idx, NULL);
204 BFD_DBG ("session created, bs_idx=%u, sw_if_index=%d, local=%U, peer=%U",
205 bs->bs_idx, key->sw_if_index, format_ip46_address,
206 &key->local_addr, IP46_TYPE_ANY, format_ip46_address,
207 &key->peer_addr, IP46_TYPE_ANY);
208 if (BFD_TRANSPORT_UDP4 == t)
210 bus->adj_index = adj_nbr_add_or_lock (FIB_PROTOCOL_IP4, VNET_LINK_IP4,
213 BFD_DBG ("adj_nbr_add_or_lock(FIB_PROTOCOL_IP4, VNET_LINK_IP4, %U, %d) "
214 "returns %d", format_ip46_address, &key->peer_addr,
215 IP46_TYPE_ANY, key->sw_if_index, bus->adj_index);
219 bus->adj_index = adj_nbr_add_or_lock (FIB_PROTOCOL_IP6, VNET_LINK_IP6,
222 BFD_DBG ("adj_nbr_add_or_lock(FIB_PROTOCOL_IP6, VNET_LINK_IP6, %U, %d) "
223 "returns %d", format_ip46_address, &key->peer_addr,
224 IP46_TYPE_ANY, key->sw_if_index, bus->adj_index);
227 return bfd_session_set_params (bum->bfd_main, bs, desired_min_tx_usec,
228 required_min_rx_usec, detect_mult);
231 static vnet_api_error_t
232 bfd_udp_validate_api_input (u32 sw_if_index,
233 const ip46_address_t * local_addr,
234 const ip46_address_t * peer_addr)
236 vnet_sw_interface_t *sw_if =
237 vnet_get_sw_interface (vnet_get_main (), sw_if_index);
238 u8 local_ip_valid = 0;
239 ip_interface_address_t *ia = NULL;
242 clib_warning ("got NULL sw_if");
243 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
245 if (ip46_address_is_ip4 (local_addr))
247 if (!ip46_address_is_ip4 (peer_addr))
249 clib_warning ("IP family mismatch");
250 return VNET_API_ERROR_INVALID_ARGUMENT;
252 ip4_main_t *im = &ip4_main;
255 foreach_ip_interface_address (
256 &im->lookup_main, ia, sw_if_index, 0 /* honor unnumbered */, ({
258 ip_interface_address_get_address (&im->lookup_main, ia);
259 if (x->as_u32 == local_addr->ip4.as_u32)
261 /* valid address for this interface */
270 if (ip46_address_is_ip4 (peer_addr))
272 clib_warning ("IP family mismatch");
273 return VNET_API_ERROR_INVALID_ARGUMENT;
275 ip6_main_t *im = &ip6_main;
277 foreach_ip_interface_address (
278 &im->lookup_main, ia, sw_if_index, 0 /* honor unnumbered */, ({
280 ip_interface_address_get_address (&im->lookup_main, ia);
281 if (local_addr->ip6.as_u64[0] == x->as_u64[0] &&
282 local_addr->ip6.as_u64[1] == x->as_u64[1])
284 /* valid address for this interface */
294 clib_warning ("address not found on interface");
295 return VNET_API_ERROR_ADDRESS_NOT_FOUND_FOR_INTERFACE;
301 static vnet_api_error_t
302 bfd_udp_find_session_by_api_input (u32 sw_if_index,
303 const ip46_address_t * local_addr,
304 const ip46_address_t * peer_addr,
305 bfd_session_t ** bs_out)
307 vnet_api_error_t rv =
308 bfd_udp_validate_api_input (sw_if_index, local_addr, peer_addr);
311 bfd_udp_main_t *bum = &bfd_udp_main;
313 bfd_udp_key_init (&key, sw_if_index, local_addr, peer_addr);
314 bfd_session_t *bs = bfd_lookup_session (bum, &key);
322 ("BFD session not found (sw_if_index=%u, local=%U, peer=%U",
323 sw_if_index, format_ip46_address, local_addr, IP46_TYPE_ANY,
324 format_ip46_address, peer_addr, IP46_TYPE_ANY);
325 return VNET_API_ERROR_BFD_ENOENT;
331 static vnet_api_error_t
332 bfd_api_verify_common (u32 sw_if_index, u32 desired_min_tx_usec,
333 u32 required_min_rx_usec, u8 detect_mult,
334 const ip46_address_t * local_addr,
335 const ip46_address_t * peer_addr)
337 vnet_api_error_t rv =
338 bfd_udp_validate_api_input (sw_if_index, local_addr, peer_addr);
345 clib_warning ("detect_mult < 1");
346 return VNET_API_ERROR_INVALID_ARGUMENT;
348 if (desired_min_tx_usec < 1)
350 clib_warning ("desired_min_tx_usec < 1");
351 return VNET_API_ERROR_INVALID_ARGUMENT;
357 bfd_udp_del_session_internal (bfd_session_t * bs)
359 bfd_udp_main_t *bum = &bfd_udp_main;
360 BFD_DBG ("free bfd-udp session, bs_idx=%d", bs->bs_idx);
361 mhash_unset (&bum->bfd_session_idx_by_bfd_key, &bs->udp.key, NULL);
362 adj_unlock (bs->udp.adj_index);
363 bfd_put_session (bum->bfd_main, bs);
367 bfd_udp_add_session (u32 sw_if_index, const ip46_address_t * local_addr,
368 const ip46_address_t * peer_addr,
369 u32 desired_min_tx_usec, u32 required_min_rx_usec,
370 u8 detect_mult, u8 is_authenticated, u32 conf_key_id,
373 vnet_api_error_t rv =
374 bfd_api_verify_common (sw_if_index, desired_min_tx_usec,
375 required_min_rx_usec, detect_mult,
376 local_addr, peer_addr);
377 bfd_session_t *bs = NULL;
381 bfd_udp_add_session_internal (&bfd_udp_main, sw_if_index,
383 required_min_rx_usec, detect_mult,
384 local_addr, peer_addr, &bs);
386 if (!rv && is_authenticated)
389 rv = bfd_auth_activate (bs, conf_key_id, bfd_key_id,
390 0 /* is not delayed */ );
392 clib_warning ("SSL missing, cannot add authenticated BFD session");
393 rv = VNET_API_ERROR_BFD_NOTSUPP;
397 bfd_udp_del_session_internal (bs);
402 bfd_session_start (bfd_udp_main.bfd_main, bs);
409 bfd_udp_mod_session (u32 sw_if_index,
410 const ip46_address_t * local_addr,
411 const ip46_address_t * peer_addr,
412 u32 desired_min_tx_usec,
413 u32 required_min_rx_usec, u8 detect_mult)
415 bfd_session_t *bs = NULL;
416 vnet_api_error_t rv =
417 bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr,
424 return bfd_session_set_params (bfd_udp_main.bfd_main, bs,
425 desired_min_tx_usec, required_min_rx_usec,
430 bfd_udp_del_session (u32 sw_if_index,
431 const ip46_address_t * local_addr,
432 const ip46_address_t * peer_addr)
434 bfd_session_t *bs = NULL;
435 vnet_api_error_t rv =
436 bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr,
442 bfd_udp_del_session_internal (bs);
447 bfd_udp_session_set_flags (u32 sw_if_index,
448 const ip46_address_t * local_addr,
449 const ip46_address_t * peer_addr, u8 admin_up_down)
451 bfd_session_t *bs = NULL;
452 vnet_api_error_t rv =
453 bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr,
459 bfd_session_set_flags (bs, admin_up_down);
464 bfd_auth_set_key (u32 conf_key_id, u8 auth_type, u8 key_len,
468 bfd_auth_key_t *auth_key = NULL;
469 if (!key_len || key_len > bfd_max_len_for_auth_type (auth_type))
471 clib_warning ("Invalid authentication key length for auth_type=%d:%s "
472 "(key_len=%u, must be "
473 "non-zero, expected max=%u)",
474 auth_type, bfd_auth_type_str (auth_type), key_len,
475 (u32) bfd_max_len_for_auth_type (auth_type));
476 return VNET_API_ERROR_INVALID_VALUE;
478 if (!bfd_auth_type_supported (auth_type))
480 clib_warning ("Unsupported auth type=%d:%s", auth_type,
481 bfd_auth_type_str (auth_type));
482 return VNET_API_ERROR_BFD_NOTSUPP;
484 bfd_main_t *bm = bfd_udp_main.bfd_main;
485 uword *key_idx_p = hash_get (bm->auth_key_by_conf_key_id, conf_key_id);
488 /* modifying existing key - must not be used */
489 const uword key_idx = *key_idx_p;
490 auth_key = pool_elt_at_index (bm->auth_keys, key_idx);
491 if (auth_key->use_count > 0)
493 clib_warning ("Authentication key with conf ID %u in use by %u BFD "
494 "sessions - cannot modify",
495 conf_key_id, auth_key->use_count);
496 return VNET_API_ERROR_BFD_EINUSE;
502 pool_get (bm->auth_keys, auth_key);
503 auth_key->conf_key_id = conf_key_id;
504 hash_set (bm->auth_key_by_conf_key_id, conf_key_id,
505 auth_key - bm->auth_keys);
507 auth_key->auth_type = auth_type;
508 memset (auth_key->key, 0, sizeof (auth_key->key));
509 clib_memcpy (auth_key->key, key_data, key_len);
512 clib_warning ("SSL missing, cannot manipulate authentication keys");
513 return VNET_API_ERROR_BFD_NOTSUPP;
518 bfd_auth_del_key (u32 conf_key_id)
521 bfd_auth_key_t *auth_key = NULL;
522 bfd_main_t *bm = bfd_udp_main.bfd_main;
523 uword *key_idx_p = hash_get (bm->auth_key_by_conf_key_id, conf_key_id);
526 /* deleting existing key - must not be used */
527 const uword key_idx = *key_idx_p;
528 auth_key = pool_elt_at_index (bm->auth_keys, key_idx);
529 if (auth_key->use_count > 0)
531 clib_warning ("Authentication key with conf ID %u in use by %u BFD "
532 "sessions - cannot delete",
533 conf_key_id, auth_key->use_count);
534 return VNET_API_ERROR_BFD_EINUSE;
536 hash_unset (bm->auth_key_by_conf_key_id, conf_key_id);
537 memset (auth_key, 0, sizeof (*auth_key));
538 pool_put (bm->auth_keys, auth_key);
543 clib_warning ("Authentication key with conf ID %u does not exist",
545 return VNET_API_ERROR_BFD_ENOENT;
549 clib_warning ("SSL missing, cannot manipulate authentication keys");
550 return VNET_API_ERROR_BFD_NOTSUPP;
555 bfd_udp_auth_activate (u32 sw_if_index,
556 const ip46_address_t * local_addr,
557 const ip46_address_t * peer_addr,
558 u32 conf_key_id, u8 key_id, u8 is_delayed)
561 bfd_session_t *bs = NULL;
562 vnet_api_error_t rv =
563 bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr,
569 return bfd_auth_activate (bs, conf_key_id, key_id, is_delayed);
571 clib_warning ("SSL missing, cannot activate BFD authentication");
572 return VNET_API_ERROR_BFD_NOTSUPP;
577 bfd_udp_auth_deactivate (u32 sw_if_index,
578 const ip46_address_t * local_addr,
579 const ip46_address_t * peer_addr, u8 is_delayed)
581 bfd_session_t *bs = NULL;
582 vnet_api_error_t rv =
583 bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr,
589 return bfd_auth_deactivate (bs, is_delayed);
594 BFD_UDP_INPUT_NEXT_NORMAL,
595 BFD_UDP_INPUT_NEXT_REPLY,
596 BFD_UDP_INPUT_N_NEXT,
597 } bfd_udp_input_next_t;
599 /* Packet counters - BFD control frames */
600 #define foreach_bfd_udp_error(F) \
601 F (NONE, "good bfd packets (processed)") \
602 F (BAD, "invalid bfd packets")
604 #define F(sym, string) static char BFD_UDP_ERR_##sym##_STR[] = string;
605 foreach_bfd_udp_error (F);
608 static char *bfd_udp_error_strings[] = {
609 #define F(sym, string) BFD_UDP_ERR_##sym##_STR,
610 foreach_bfd_udp_error (F)
616 #define F(sym, str) BFD_UDP_ERROR_##sym,
617 foreach_bfd_udp_error (F)
622 /* Packet counters - BFD ECHO packets */
623 #define foreach_bfd_udp_echo_error(F) \
624 F (NONE, "good bfd echo packets (processed)") \
625 F (BAD, "invalid bfd echo packets")
627 #define F(sym, string) static char BFD_UDP_ECHO_ERR_##sym##_STR[] = string;
628 foreach_bfd_udp_echo_error (F);
631 static char *bfd_udp_echo_error_strings[] = {
632 #define F(sym, string) BFD_UDP_ECHO_ERR_##sym##_STR,
633 foreach_bfd_udp_echo_error (F)
639 #define F(sym, str) BFD_UDP_ECHO_ERROR_##sym,
640 foreach_bfd_udp_echo_error (F)
642 BFD_UDP_ECHO_N_ERROR,
643 } bfd_udp_echo_error_t;
646 bfd_udp4_find_headers (vlib_buffer_t * b, ip4_header_t ** ip4,
649 /* sanity check first */
650 const i32 start = vnet_buffer (b)->ip.start_of_ip_header;
651 if (start < 0 && start < sizeof (b->pre_data))
653 BFD_ERR ("Start of ip header is before pre_data, ignoring");
658 *ip4 = (ip4_header_t *) (b->data + start);
659 if ((u8 *) * ip4 > (u8 *) vlib_buffer_get_current (b))
661 BFD_ERR ("Start of ip header is beyond current data, ignoring");
666 *udp = (udp_header_t *) ((*ip4) + 1);
669 static bfd_udp_error_t
670 bfd_udp4_verify_transport (const ip4_header_t * ip4,
671 const udp_header_t * udp, const bfd_session_t * bs)
673 const bfd_udp_session_t *bus = &bs->udp;
674 const bfd_udp_key_t *key = &bus->key;
675 if (ip4->src_address.as_u32 != key->peer_addr.ip4.as_u32)
677 BFD_ERR ("IPv4 src addr mismatch, got %U, expected %U",
678 format_ip4_address, ip4->src_address.as_u8, format_ip4_address,
679 key->peer_addr.ip4.as_u8);
680 return BFD_UDP_ERROR_BAD;
682 if (ip4->dst_address.as_u32 != key->local_addr.ip4.as_u32)
684 BFD_ERR ("IPv4 dst addr mismatch, got %U, expected %U",
685 format_ip4_address, ip4->dst_address.as_u8, format_ip4_address,
686 key->local_addr.ip4.as_u8);
687 return BFD_UDP_ERROR_BAD;
689 const u8 expected_ttl = 255;
690 if (ip4->ttl != expected_ttl)
692 BFD_ERR ("IPv4 unexpected TTL value %u, expected %u", ip4->ttl,
694 return BFD_UDP_ERROR_BAD;
696 if (clib_net_to_host_u16 (udp->src_port) < 49152)
698 BFD_ERR ("Invalid UDP src port %u, out of range <49152,65535>",
701 return BFD_UDP_ERROR_NONE;
711 bfd_rpc_update_session_cb (const bfd_rpc_update_t * a)
713 bfd_consume_pkt (bfd_udp_main.bfd_main, &a->pkt, a->bs_idx);
717 bfd_rpc_update_session (u32 bs_idx, const bfd_pkt_t * pkt)
719 /* packet length was already verified to be correct by the caller */
720 const u32 data_size = sizeof (bfd_rpc_update_t) -
721 STRUCT_SIZE_OF (bfd_rpc_update_t, pkt) + pkt->head.length;
723 bfd_rpc_update_t *update = (bfd_rpc_update_t *) data;
724 update->bs_idx = bs_idx;
725 clib_memcpy (&update->pkt, pkt, pkt->head.length);
726 vl_api_rpc_call_main_thread (bfd_rpc_update_session_cb, data, data_size);
729 static bfd_udp_error_t
730 bfd_udp4_scan (vlib_main_t * vm, vlib_node_runtime_t * rt,
731 vlib_buffer_t * b, bfd_session_t ** bs_out)
733 const bfd_pkt_t *pkt = vlib_buffer_get_current (b);
734 if (sizeof (*pkt) > b->current_length)
737 ("Payload size %d too small to hold bfd packet of minimum size %d",
738 b->current_length, sizeof (*pkt));
739 return BFD_UDP_ERROR_BAD;
743 bfd_udp4_find_headers (b, &ip4, &udp);
746 BFD_ERR ("Couldn't find ip4 or udp header");
747 return BFD_UDP_ERROR_BAD;
749 const u32 udp_payload_length = udp->length - sizeof (*udp);
750 if (pkt->head.length > udp_payload_length)
753 ("BFD packet length is larger than udp payload length (%u > %u)",
754 pkt->head.length, udp_payload_length);
755 return BFD_UDP_ERROR_BAD;
757 if (!bfd_verify_pkt_common (pkt))
759 return BFD_UDP_ERROR_BAD;
761 bfd_session_t *bs = NULL;
764 BFD_DBG ("Looking up BFD session using discriminator %u",
766 bs = bfd_find_session_by_disc (bfd_udp_main.bfd_main, pkt->your_disc);
771 memset (&key, 0, sizeof (key));
772 key.sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
773 key.local_addr.ip4.as_u32 = ip4->dst_address.as_u32;
774 key.peer_addr.ip4.as_u32 = ip4->src_address.as_u32;
775 BFD_DBG ("Looking up BFD session using key (sw_if_index=%u, local=%U, "
777 key.sw_if_index, format_ip4_address, key.local_addr.ip4.as_u8,
778 format_ip4_address, key.peer_addr.ip4.as_u8);
779 bs = bfd_lookup_session (&bfd_udp_main, &key);
783 BFD_ERR ("BFD session lookup failed - no session matches BFD pkt");
784 return BFD_UDP_ERROR_BAD;
786 BFD_DBG ("BFD session found, bs_idx=%u", bs->bs_idx);
787 if (!bfd_verify_pkt_auth (pkt, b->current_length, bs))
789 BFD_ERR ("Packet verification failed, dropping packet");
790 return BFD_UDP_ERROR_BAD;
793 if (BFD_UDP_ERROR_NONE != (err = bfd_udp4_verify_transport (ip4, udp, bs)))
797 bfd_rpc_update_session (bs->bs_idx, pkt);
799 return BFD_UDP_ERROR_NONE;
803 bfd_udp6_find_headers (vlib_buffer_t * b, ip6_header_t ** ip6,
806 /* sanity check first */
807 const i32 start = vnet_buffer (b)->ip.start_of_ip_header;
808 if (start < 0 && start < sizeof (b->pre_data))
810 BFD_ERR ("Start of ip header is before pre_data, ignoring");
815 *ip6 = (ip6_header_t *) (b->data + start);
816 if ((u8 *) * ip6 > (u8 *) vlib_buffer_get_current (b))
818 BFD_ERR ("Start of ip header is beyond current data, ignoring");
823 if ((*ip6)->protocol != IP_PROTOCOL_UDP)
825 BFD_ERR ("Unexpected protocol in IPv6 header '%u', expected '%u' (== "
826 "IP_PROTOCOL_UDP)", (*ip6)->protocol, IP_PROTOCOL_UDP);
831 *udp = (udp_header_t *) ((*ip6) + 1);
834 static bfd_udp_error_t
835 bfd_udp6_verify_transport (const ip6_header_t * ip6,
836 const udp_header_t * udp, const bfd_session_t * bs)
838 const bfd_udp_session_t *bus = &bs->udp;
839 const bfd_udp_key_t *key = &bus->key;
840 if (ip6->src_address.as_u64[0] != key->peer_addr.ip6.as_u64[0] &&
841 ip6->src_address.as_u64[1] != key->peer_addr.ip6.as_u64[1])
843 BFD_ERR ("IP src addr mismatch, got %U, expected %U",
844 format_ip6_address, ip6, format_ip6_address,
845 &key->peer_addr.ip6);
846 return BFD_UDP_ERROR_BAD;
848 if (ip6->dst_address.as_u64[0] != key->local_addr.ip6.as_u64[0] &&
849 ip6->dst_address.as_u64[1] != key->local_addr.ip6.as_u64[1])
851 BFD_ERR ("IP dst addr mismatch, got %U, expected %U",
852 format_ip6_address, ip6, format_ip6_address,
853 &key->local_addr.ip6);
854 return BFD_UDP_ERROR_BAD;
856 const u8 expected_hop_limit = 255;
857 if (ip6->hop_limit != expected_hop_limit)
859 BFD_ERR ("IPv6 unexpected hop-limit value %u, expected %u",
860 ip6->hop_limit, expected_hop_limit);
861 return BFD_UDP_ERROR_BAD;
863 if (clib_net_to_host_u16 (udp->src_port) < 49152)
865 BFD_ERR ("Invalid UDP src port %u, out of range <49152,65535>",
868 return BFD_UDP_ERROR_NONE;
871 static bfd_udp_error_t
872 bfd_udp6_scan (vlib_main_t * vm, vlib_node_runtime_t * rt,
873 vlib_buffer_t * b, bfd_session_t ** bs_out)
875 const bfd_pkt_t *pkt = vlib_buffer_get_current (b);
876 if (sizeof (*pkt) > b->current_length)
879 ("Payload size %d too small to hold bfd packet of minimum size %d",
880 b->current_length, sizeof (*pkt));
881 return BFD_UDP_ERROR_BAD;
885 bfd_udp6_find_headers (b, &ip6, &udp);
888 BFD_ERR ("Couldn't find ip6 or udp header");
889 return BFD_UDP_ERROR_BAD;
891 const u32 udp_payload_length = udp->length - sizeof (*udp);
892 if (pkt->head.length > udp_payload_length)
895 ("BFD packet length is larger than udp payload length (%u > %u)",
896 pkt->head.length, udp_payload_length);
897 return BFD_UDP_ERROR_BAD;
899 if (!bfd_verify_pkt_common (pkt))
901 return BFD_UDP_ERROR_BAD;
903 bfd_session_t *bs = NULL;
906 BFD_DBG ("Looking up BFD session using discriminator %u",
908 bs = bfd_find_session_by_disc (bfd_udp_main.bfd_main, pkt->your_disc);
913 memset (&key, 0, sizeof (key));
914 key.sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
915 key.local_addr.ip6.as_u64[0] = ip6->dst_address.as_u64[0];
916 key.local_addr.ip6.as_u64[1] = ip6->dst_address.as_u64[1];
917 key.peer_addr.ip6.as_u64[0] = ip6->src_address.as_u64[0];
918 key.peer_addr.ip6.as_u64[1] = ip6->src_address.as_u64[1];
919 BFD_DBG ("Looking up BFD session using key (sw_if_index=%u, local=%U, "
921 key.sw_if_index, format_ip6_address, &key.local_addr,
922 format_ip6_address, &key.peer_addr);
923 bs = bfd_lookup_session (&bfd_udp_main, &key);
927 BFD_ERR ("BFD session lookup failed - no session matches BFD pkt");
928 return BFD_UDP_ERROR_BAD;
930 BFD_DBG ("BFD session found, bs_idx=%u", bs->bs_idx);
931 if (!bfd_verify_pkt_auth (pkt, b->current_length, bs))
933 BFD_ERR ("Packet verification failed, dropping packet");
934 return BFD_UDP_ERROR_BAD;
937 if (BFD_UDP_ERROR_NONE != (err = bfd_udp6_verify_transport (ip6, udp, bs)))
941 bfd_rpc_update_session (bs->bs_idx, pkt);
943 return BFD_UDP_ERROR_NONE;
947 * Process a frame of bfd packets
948 * Expect 1 packet / frame
951 bfd_udp_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
952 vlib_frame_t * f, int is_ipv6)
954 u32 n_left_from, *from;
955 bfd_input_trace_t *t0;
957 from = vlib_frame_vector_args (f); /* array of buffer indices */
958 n_left_from = f->n_vectors; /* number of buffer indices */
960 while (n_left_from > 0)
967 b0 = vlib_get_buffer (vm, bi0);
969 bfd_session_t *bs = NULL;
971 /* If this pkt is traced, snapshot the data */
972 if (b0->flags & VLIB_BUFFER_IS_TRACED)
975 t0 = vlib_add_trace (vm, rt, b0, sizeof (*t0));
976 len = (b0->current_length < sizeof (t0->data)) ? b0->current_length
979 clib_memcpy (t0->data, vlib_buffer_get_current (b0), len);
982 /* scan this bfd pkt. error0 is the counter index to bmp */
985 error0 = bfd_udp6_scan (vm, rt, b0, &bs);
989 error0 = bfd_udp4_scan (vm, rt, b0, &bs);
991 b0->error = rt->errors[error0];
993 next0 = BFD_UDP_INPUT_NEXT_NORMAL;
994 if (BFD_UDP_ERROR_NONE == error0)
997 * if everything went fine, check for poll bit, if present, re-use
998 * the buffer and based on (now updated) session parameters, send
999 * the final packet back
1001 const bfd_pkt_t *pkt = vlib_buffer_get_current (b0);
1002 if (bfd_pkt_get_poll (pkt))
1004 bfd_init_final_control_frame (vm, b0, bs);
1007 vlib_node_increment_counter (vm, bfd_udp6_input_node.index,
1012 vlib_node_increment_counter (vm, bfd_udp4_input_node.index,
1015 next0 = BFD_UDP_INPUT_NEXT_REPLY;
1018 vlib_set_next_frame_buffer (vm, rt, next0, bi0);
1024 return f->n_vectors;
1028 bfd_udp4_input (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
1030 return bfd_udp_input (vm, rt, f, 0);
1034 * bfd input graph node declaration
1037 VLIB_REGISTER_NODE (bfd_udp4_input_node, static) = {
1038 .function = bfd_udp4_input,
1039 .name = "bfd-udp4-input",
1040 .vector_size = sizeof (u32),
1041 .type = VLIB_NODE_TYPE_INTERNAL,
1043 .n_errors = BFD_UDP_N_ERROR,
1044 .error_strings = bfd_udp_error_strings,
1046 .format_trace = bfd_input_format_trace,
1048 .n_next_nodes = BFD_UDP_INPUT_N_NEXT,
1051 [BFD_UDP_INPUT_NEXT_NORMAL] = "error-drop",
1052 [BFD_UDP_INPUT_NEXT_REPLY] = "ip4-lookup",
1058 bfd_udp6_input (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
1060 return bfd_udp_input (vm, rt, f, 1);
1064 VLIB_REGISTER_NODE (bfd_udp6_input_node, static) = {
1065 .function = bfd_udp6_input,
1066 .name = "bfd-udp6-input",
1067 .vector_size = sizeof (u32),
1068 .type = VLIB_NODE_TYPE_INTERNAL,
1070 .n_errors = BFD_UDP_N_ERROR,
1071 .error_strings = bfd_udp_error_strings,
1073 .format_trace = bfd_input_format_trace,
1075 .n_next_nodes = BFD_UDP_INPUT_N_NEXT,
1078 [BFD_UDP_INPUT_NEXT_NORMAL] = "error-drop",
1079 [BFD_UDP_INPUT_NEXT_REPLY] = "ip6-lookup",
1085 * @brief swap the source and destination IP addresses in the packet
1088 bfd_echo_address_swap (vlib_buffer_t * b, int is_ipv6)
1090 udp_header_t *dummy = NULL;
1093 ip6_header_t *ip6 = NULL;
1094 bfd_udp6_find_headers (b, &ip6, &dummy);
1099 ip6_address_t tmp = ip6->dst_address;
1100 ip6->dst_address = ip6->src_address;
1101 ip6->src_address = tmp;
1102 vlib_buffer_advance (b,
1103 (u8 *) ip6 - (u8 *) vlib_buffer_get_current (b));
1107 ip4_header_t *ip4 = NULL;
1108 bfd_udp4_find_headers (b, &ip4, &dummy);
1113 ip4_address_t tmp = ip4->dst_address;
1114 ip4->dst_address = ip4->src_address;
1115 ip4->src_address = tmp;
1116 vlib_buffer_advance (b,
1117 (u8 *) ip4 - (u8 *) vlib_buffer_get_current (b));
1123 * Process a frame of bfd echo packets
1124 * Expect 1 packet / frame
1127 bfd_udp_echo_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
1128 vlib_frame_t * f, int is_ipv6)
1130 u32 n_left_from, *from;
1131 bfd_input_trace_t *t0;
1133 from = vlib_frame_vector_args (f); /* array of buffer indices */
1134 n_left_from = f->n_vectors; /* number of buffer indices */
1136 while (n_left_from > 0)
1143 b0 = vlib_get_buffer (vm, bi0);
1145 /* If this pkt is traced, snapshot the data */
1146 if (b0->flags & VLIB_BUFFER_IS_TRACED)
1149 t0 = vlib_add_trace (vm, rt, b0, sizeof (*t0));
1150 len = (b0->current_length < sizeof (t0->data)) ? b0->current_length
1151 : sizeof (t0->data);
1153 clib_memcpy (t0->data, vlib_buffer_get_current (b0), len);
1156 if (bfd_echo_address_swap (b0, is_ipv6))
1158 /* loop back the packet */
1159 b0->error = rt->errors[BFD_UDP_ERROR_NONE];
1162 vlib_node_increment_counter (vm, bfd_udp_echo6_input_node.index,
1167 vlib_node_increment_counter (vm, bfd_udp_echo4_input_node.index,
1170 next0 = BFD_UDP_INPUT_NEXT_REPLY;
1174 b0->error = rt->errors[BFD_UDP_ERROR_BAD];
1175 next0 = BFD_UDP_INPUT_NEXT_NORMAL;
1178 vlib_set_next_frame_buffer (vm, rt, next0, bi0);
1184 return f->n_vectors;
1188 bfd_udp_echo4_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
1191 return bfd_udp_echo_input (vm, rt, f, 0);
1195 bfd_echo_input_format_trace (u8 * s, va_list * args)
1197 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1198 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1199 const bfd_udp_echo_input_trace_t *t =
1200 va_arg (*args, bfd_udp_echo_input_trace_t *);
1201 if (t->len > STRUCT_SIZE_OF (bfd_pkt_t, head))
1203 s = format (s, "BFD ECHO:\n");
1204 s = format (s, " data: %U", format_hexdump, t->data, t->len);
1211 * bfd input graph node declaration
1214 VLIB_REGISTER_NODE (bfd_udp_echo4_input_node, static) = {
1215 .function = bfd_udp_echo4_input,
1216 .name = "bfd-udp-echo4-input",
1217 .vector_size = sizeof (u32),
1218 .type = VLIB_NODE_TYPE_INTERNAL,
1220 .n_errors = BFD_UDP_ECHO_N_ERROR,
1221 .error_strings = bfd_udp_error_strings,
1223 .format_trace = bfd_echo_input_format_trace,
1225 .n_next_nodes = BFD_UDP_INPUT_N_NEXT,
1228 [BFD_UDP_INPUT_NEXT_NORMAL] = "error-drop",
1229 [BFD_UDP_INPUT_NEXT_REPLY] = "ip4-lookup",
1235 bfd_udp_echo6_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
1238 return bfd_udp_echo_input (vm, rt, f, 1);
1242 VLIB_REGISTER_NODE (bfd_udp_echo6_input_node, static) = {
1243 .function = bfd_udp_echo6_input,
1244 .name = "bfd-udp-echo6-input",
1245 .vector_size = sizeof (u32),
1246 .type = VLIB_NODE_TYPE_INTERNAL,
1248 .n_errors = BFD_UDP_ECHO_N_ERROR,
1249 .error_strings = bfd_udp_echo_error_strings,
1251 .format_trace = bfd_echo_input_format_trace,
1253 .n_next_nodes = BFD_UDP_INPUT_N_NEXT,
1256 [BFD_UDP_INPUT_NEXT_NORMAL] = "error-drop",
1257 [BFD_UDP_INPUT_NEXT_REPLY] = "ip6-lookup",
1263 static clib_error_t *
1264 bfd_sw_interface_up_down (vnet_main_t * vnm, u32 sw_if_index, u32 flags)
1266 // vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
1267 if (!(flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP))
1274 VNET_SW_INTERFACE_ADMIN_UP_DOWN_FUNCTION (bfd_sw_interface_up_down);
1276 static clib_error_t *
1277 bfd_hw_interface_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
1279 if (flags & VNET_HW_INTERFACE_FLAG_LINK_UP)
1286 VNET_HW_INTERFACE_LINK_UP_DOWN_FUNCTION (bfd_hw_interface_up_down);
1291 static clib_error_t *
1292 bfd_udp_init (vlib_main_t * vm)
1294 mhash_init (&bfd_udp_main.bfd_session_idx_by_bfd_key, sizeof (uword),
1295 sizeof (bfd_udp_key_t));
1296 bfd_udp_main.bfd_main = &bfd_main;
1297 udp_register_dst_port (vm, UDP_DST_PORT_bfd4, bfd_udp4_input_node.index, 1);
1298 udp_register_dst_port (vm, UDP_DST_PORT_bfd6, bfd_udp6_input_node.index, 0);
1299 udp_register_dst_port (vm, UDP_DST_PORT_bfd_echo4,
1300 bfd_udp_echo4_input_node.index, 1);
1301 udp_register_dst_port (vm, UDP_DST_PORT_bfd_echo6,
1302 bfd_udp_echo6_input_node.index, 0);
1306 VLIB_INIT_FUNCTION (bfd_udp_init);
1309 * fd.io coding-style-patch-verification: ON
1312 * eval: (c-set-style "gnu")