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 */
62 /* node index of "ip4-midchain" node */
64 /* node index of "ip6-midchain" node */
67 vlib_log_class_t log_class;
68 /* number of active udp4 sessions */
69 u32 udp4_sessions_count;
70 /* number of active udp6 sessions */
71 u32 udp6_sessions_count;
74 static vlib_node_registration_t bfd_udp4_input_node;
75 static vlib_node_registration_t bfd_udp6_input_node;
76 static vlib_node_registration_t bfd_udp_echo4_input_node;
77 static vlib_node_registration_t bfd_udp_echo6_input_node;
79 bfd_udp_main_t bfd_udp_main;
82 bfd_udp_set_echo_source (u32 sw_if_index)
84 vnet_sw_interface_t *sw_if =
85 vnet_get_sw_interface_or_null (bfd_udp_main.vnet_main, sw_if_index);
88 bfd_udp_main.echo_source_sw_if_index = sw_if_index;
89 bfd_udp_main.echo_source_is_set = 1;
92 return VNET_API_ERROR_BFD_ENOENT;
96 bfd_udp_del_echo_source (u32 sw_if_index)
98 bfd_udp_main.echo_source_sw_if_index = ~0;
99 bfd_udp_main.echo_source_is_set = 0;
104 bfd_udp_is_echo_available (bfd_transport_e transport)
106 if (!bfd_udp_main.echo_source_is_set)
108 BFD_DBG ("UDP echo source not set - echo not available");
112 * for the echo to work, we need a loopback interface with at least one
113 * address with netmask length at most 31 (ip4) or 127 (ip6) so that we can
114 * pick an unused address from that subnet
116 vnet_sw_interface_t *sw_if =
117 vnet_get_sw_interface_or_null (bfd_udp_main.vnet_main,
118 bfd_udp_main.echo_source_sw_if_index);
119 if (sw_if && sw_if->flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
121 if (BFD_TRANSPORT_UDP4 == transport)
123 ip4_main_t *im = &ip4_main;
124 ip_interface_address_t *ia = NULL;
126 foreach_ip_interface_address (&im->lookup_main, ia,
127 bfd_udp_main.echo_source_sw_if_index,
128 0 /* honor unnumbered */, ({
129 if (ia->address_length <= 31)
136 else if (BFD_TRANSPORT_UDP6 == transport)
138 ip6_main_t *im = &ip6_main;
139 ip_interface_address_t *ia = NULL;
141 foreach_ip_interface_address (&im->lookup_main, ia,
142 bfd_udp_main.echo_source_sw_if_index,
143 0 /* honor unnumbered */, ({
144 if (ia->address_length <= 127)
152 BFD_DBG ("No usable IP address for UDP echo - echo not available");
157 bfd_udp_bs_idx_to_sport (u32 bs_idx)
159 /* The source port MUST be in the range 49152 through 65535. The same UDP
160 * source port number MUST be used for all BFD Control packets associated
161 * with a particular session. The source port number SHOULD be unique among
162 * all BFD sessions on the system. If more than 16384 BFD sessions are
163 * simultaneously active, UDP source port numbers MAY be reused on
164 * multiple sessions, but the number of distinct uses of the same UDP
165 * source port number SHOULD be minimized.
167 return 49152 + bs_idx % (65535 - 49152 + 1);
171 bfd_udp_get_echo_src_ip4 (ip4_address_t * addr)
173 if (!bfd_udp_main.echo_source_is_set)
175 BFD_ERR ("cannot find ip4 address, echo source not set");
178 ip_interface_address_t *ia = NULL;
179 ip4_main_t *im = &ip4_main;
182 foreach_ip_interface_address (
183 &im->lookup_main, ia, bfd_udp_main.echo_source_sw_if_index,
184 0 /* honor unnumbered */, ({
186 ip_interface_address_get_address (&im->lookup_main, ia);
187 if (ia->address_length <= 31)
189 addr->as_u32 = clib_host_to_net_u32 (x->as_u32);
191 * flip the last bit to get a different address, might be network,
195 addr->as_u32 = clib_net_to_host_u32 (addr->as_u32);
200 BFD_ERR ("cannot find ip4 address, no usable address found");
205 bfd_udp_get_echo_src_ip6 (ip6_address_t * addr)
207 if (!bfd_udp_main.echo_source_is_set)
209 BFD_ERR ("cannot find ip6 address, echo source not set");
212 ip_interface_address_t *ia = NULL;
213 ip6_main_t *im = &ip6_main;
216 foreach_ip_interface_address (
217 &im->lookup_main, ia, bfd_udp_main.echo_source_sw_if_index,
218 0 /* honor unnumbered */, ({
220 ip_interface_address_get_address (&im->lookup_main, ia);
221 if (ia->address_length <= 127)
224 addr->as_u8[15] ^= 1; /* flip the last bit of the address */
229 BFD_ERR ("cannot find ip6 address, no usable address found");
234 bfd_udp_get_echo_source (int *is_set, u32 * sw_if_index,
235 int *have_usable_ip4, ip4_address_t * ip4,
236 int *have_usable_ip6, ip6_address_t * ip6)
238 if (bfd_udp_main.echo_source_is_set)
241 *sw_if_index = bfd_udp_main.echo_source_sw_if_index;
242 *have_usable_ip4 = bfd_udp_get_echo_src_ip4 (ip4);
243 *have_usable_ip6 = bfd_udp_get_echo_src_ip6 (ip6);
252 bfd_add_udp4_transport (vlib_main_t * vm, u32 bi, const bfd_session_t * bs,
255 const bfd_udp_session_t *bus = &bs->udp;
256 const bfd_udp_key_t *key = &bus->key;
257 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
259 b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
260 vnet_buffer (b)->ip.adj_index[VLIB_RX] = bus->adj_index;
261 vnet_buffer (b)->ip.adj_index[VLIB_TX] = bus->adj_index;
262 vnet_buffer (b)->sw_if_index[VLIB_RX] = 0;
263 vnet_buffer (b)->sw_if_index[VLIB_TX] = ~0;
269 ip4_udp_headers *headers = NULL;
270 vlib_buffer_advance (b, -sizeof (*headers));
271 headers = vlib_buffer_get_current (b);
272 clib_memset (headers, 0, sizeof (*headers));
273 headers->ip4.ip_version_and_header_length = 0x45;
274 headers->ip4.ttl = 255;
275 headers->ip4.protocol = IP_PROTOCOL_UDP;
276 headers->udp.src_port =
277 clib_host_to_net_u16 (bfd_udp_bs_idx_to_sport (bs->bs_idx));
281 if (!(rv = bfd_udp_get_echo_src_ip4 (&headers->ip4.src_address)))
285 headers->ip4.dst_address.as_u32 = key->local_addr.ip4.as_u32;
286 headers->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_bfd_echo4);
290 headers->ip4.src_address.as_u32 = key->local_addr.ip4.as_u32;
291 headers->ip4.dst_address.as_u32 = key->peer_addr.ip4.as_u32;
292 headers->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_bfd4);
295 /* fix ip length, checksum and udp length */
296 const u16 ip_length = vlib_buffer_length_in_chain (vm, b);
298 headers->ip4.length = clib_host_to_net_u16 (ip_length);
299 headers->ip4.checksum = ip4_header_checksum (&headers->ip4);
301 const u16 udp_length = ip_length - (sizeof (headers->ip4));
302 headers->udp.length = clib_host_to_net_u16 (udp_length);
307 bfd_add_udp6_transport (vlib_main_t * vm, u32 bi, const bfd_session_t * bs,
310 const bfd_udp_session_t *bus = &bs->udp;
311 const bfd_udp_key_t *key = &bus->key;
312 vlib_buffer_t *b = vlib_get_buffer (vm, bi);
314 b->flags |= VNET_BUFFER_F_LOCALLY_ORIGINATED;
315 vnet_buffer (b)->ip.adj_index[VLIB_RX] = bus->adj_index;
316 vnet_buffer (b)->ip.adj_index[VLIB_TX] = bus->adj_index;
317 vnet_buffer (b)->sw_if_index[VLIB_RX] = 0;
318 vnet_buffer (b)->sw_if_index[VLIB_TX] = 0;
324 ip6_udp_headers *headers = NULL;
325 vlib_buffer_advance (b, -sizeof (*headers));
326 headers = vlib_buffer_get_current (b);
327 clib_memset (headers, 0, sizeof (*headers));
328 headers->ip6.ip_version_traffic_class_and_flow_label =
329 clib_host_to_net_u32 (0x6 << 28);
330 headers->ip6.hop_limit = 255;
331 headers->ip6.protocol = IP_PROTOCOL_UDP;
332 headers->udp.src_port =
333 clib_host_to_net_u16 (bfd_udp_bs_idx_to_sport (bs->bs_idx));
337 if (!(rv = bfd_udp_get_echo_src_ip6 (&headers->ip6.src_address)))
341 clib_memcpy_fast (&headers->ip6.dst_address, &key->local_addr.ip6,
342 sizeof (headers->ip6.dst_address));
344 headers->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_bfd_echo6);
348 clib_memcpy_fast (&headers->ip6.src_address, &key->local_addr.ip6,
349 sizeof (headers->ip6.src_address));
350 clib_memcpy_fast (&headers->ip6.dst_address, &key->peer_addr.ip6,
351 sizeof (headers->ip6.dst_address));
352 headers->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_bfd6);
355 /* fix ip payload length and udp length */
356 const u16 udp_length =
357 vlib_buffer_length_in_chain (vm, b) - (sizeof (headers->ip6));
358 headers->udp.length = clib_host_to_net_u16 (udp_length);
359 headers->ip6.payload_length = headers->udp.length;
361 /* IPv6 UDP checksum is mandatory */
363 headers->udp.checksum =
364 ip6_tcp_udp_icmp_compute_checksum (vm, b, &headers->ip6, &bogus);
366 if (headers->udp.checksum == 0)
368 headers->udp.checksum = 0xffff;
374 bfd_create_frame_to_next_node (vlib_main_t * vm, u32 bi, u32 next_node)
376 vlib_frame_t *f = vlib_get_frame_to_node (vm, next_node);
377 u32 *to_next = vlib_frame_vector_args (f);
380 vlib_put_frame_to_node (vm, next_node, f);
384 bfd_udp_calc_next_node (const struct bfd_session_s *bs, u32 * next_node)
386 vnet_main_t *vnm = vnet_get_main ();
387 const bfd_udp_session_t *bus = &bs->udp;
388 ip_adjacency_t *adj = adj_get (bus->adj_index);
390 /* don't try to send the buffer if the interface is not up */
391 if (!vnet_sw_interface_is_up (vnm, bus->key.sw_if_index))
394 switch (adj->lookup_next_index)
396 case IP_LOOKUP_NEXT_ARP:
397 switch (bs->transport)
399 case BFD_TRANSPORT_UDP4:
400 *next_node = bfd_udp_main.ip4_arp_idx;
402 case BFD_TRANSPORT_UDP6:
403 *next_node = bfd_udp_main.ip6_ndp_idx;
407 case IP_LOOKUP_NEXT_REWRITE:
408 switch (bs->transport)
410 case BFD_TRANSPORT_UDP4:
411 *next_node = bfd_udp_main.ip4_rewrite_idx;
413 case BFD_TRANSPORT_UDP6:
414 *next_node = bfd_udp_main.ip6_rewrite_idx;
418 case IP_LOOKUP_NEXT_MIDCHAIN:
419 switch (bs->transport)
421 case BFD_TRANSPORT_UDP4:
422 *next_node = bfd_udp_main.ip4_midchain_idx;
424 case BFD_TRANSPORT_UDP6:
425 *next_node = bfd_udp_main.ip6_midchain_idx;
437 bfd_transport_udp4 (vlib_main_t * vm, u32 bi, const struct bfd_session_s *bs)
440 int rv = bfd_udp_calc_next_node (bs, &next_node);
443 bfd_create_frame_to_next_node (vm, bi, next_node);
449 bfd_transport_udp6 (vlib_main_t * vm, u32 bi, const struct bfd_session_s *bs)
452 int rv = bfd_udp_calc_next_node (bs, &next_node);
455 bfd_create_frame_to_next_node (vm, bi, next_node);
460 static bfd_session_t *
461 bfd_lookup_session (bfd_udp_main_t * bum, const bfd_udp_key_t * key)
463 uword *p = mhash_get (&bum->bfd_session_idx_by_bfd_key, key);
466 return bfd_find_session_by_idx (bum->bfd_main, *p);
472 bfd_udp_key_init (bfd_udp_key_t * key, u32 sw_if_index,
473 const ip46_address_t * local_addr,
474 const ip46_address_t * peer_addr)
476 clib_memset (key, 0, sizeof (*key));
477 key->sw_if_index = sw_if_index;
478 key->local_addr.as_u64[0] = local_addr->as_u64[0];
479 key->local_addr.as_u64[1] = local_addr->as_u64[1];
480 key->peer_addr.as_u64[0] = peer_addr->as_u64[0];
481 key->peer_addr.as_u64[1] = peer_addr->as_u64[1];
484 static vnet_api_error_t
485 bfd_udp_add_session_internal (vlib_main_t * vm, bfd_udp_main_t * bum,
486 u32 sw_if_index, u32 desired_min_tx_usec,
487 u32 required_min_rx_usec, u8 detect_mult,
488 const ip46_address_t * local_addr,
489 const ip46_address_t * peer_addr,
490 bfd_session_t ** bs_out)
492 /* get a pool entry and if we end up not needing it, give it back */
493 bfd_transport_e t = BFD_TRANSPORT_UDP4;
494 if (!ip46_address_is_ip4 (local_addr))
496 t = BFD_TRANSPORT_UDP6;
498 bfd_session_t *bs = bfd_get_session (bum->bfd_main, t);
501 return VNET_API_ERROR_BFD_EAGAIN;
503 bfd_udp_session_t *bus = &bs->udp;
504 clib_memset (bus, 0, sizeof (*bus));
505 bfd_udp_key_t *key = &bus->key;
506 bfd_udp_key_init (key, sw_if_index, local_addr, peer_addr);
507 const bfd_session_t *tmp = bfd_lookup_session (bum, key);
510 vlib_log_err (bum->log_class,
511 "duplicate bfd-udp session, existing bs_idx=%d",
513 bfd_put_session (bum->bfd_main, bs);
514 return VNET_API_ERROR_BFD_EEXIST;
516 mhash_set (&bum->bfd_session_idx_by_bfd_key, key, bs->bs_idx, NULL);
517 BFD_DBG ("session created, bs_idx=%u, sw_if_index=%d, local=%U, peer=%U",
518 bs->bs_idx, key->sw_if_index, format_ip46_address,
519 &key->local_addr, IP46_TYPE_ANY, format_ip46_address,
520 &key->peer_addr, IP46_TYPE_ANY);
521 vlib_log_info (bum->log_class, "create BFD session: %U",
522 format_bfd_session, bs);
523 if (BFD_TRANSPORT_UDP4 == t)
525 bus->adj_index = adj_nbr_add_or_lock (FIB_PROTOCOL_IP4, VNET_LINK_IP4,
528 BFD_DBG ("adj_nbr_add_or_lock(FIB_PROTOCOL_IP4, VNET_LINK_IP4, %U, %d) "
529 "returns %d", format_ip46_address, &key->peer_addr,
530 IP46_TYPE_ANY, key->sw_if_index, bus->adj_index);
531 ++bum->udp4_sessions_count;
532 if (1 == bum->udp4_sessions_count)
534 udp_register_dst_port (vm, UDP_DST_PORT_bfd4,
535 bfd_udp4_input_node.index, 1);
536 udp_register_dst_port (vm, UDP_DST_PORT_bfd_echo4,
537 bfd_udp_echo4_input_node.index, 1);
542 bus->adj_index = adj_nbr_add_or_lock (FIB_PROTOCOL_IP6, VNET_LINK_IP6,
545 BFD_DBG ("adj_nbr_add_or_lock(FIB_PROTOCOL_IP6, VNET_LINK_IP6, %U, %d) "
546 "returns %d", format_ip46_address, &key->peer_addr,
547 IP46_TYPE_ANY, key->sw_if_index, bus->adj_index);
548 ++bum->udp6_sessions_count;
549 if (1 == bum->udp6_sessions_count)
551 udp_register_dst_port (vm, UDP_DST_PORT_bfd6,
552 bfd_udp6_input_node.index, 0);
553 udp_register_dst_port (vm, UDP_DST_PORT_bfd_echo6,
554 bfd_udp_echo6_input_node.index, 0);
558 return bfd_session_set_params (bum->bfd_main, bs, desired_min_tx_usec,
559 required_min_rx_usec, detect_mult);
562 static vnet_api_error_t
563 bfd_udp_validate_api_input (u32 sw_if_index,
564 const ip46_address_t * local_addr,
565 const ip46_address_t * peer_addr)
567 bfd_udp_main_t *bum = &bfd_udp_main;
568 vnet_sw_interface_t *sw_if =
569 vnet_get_sw_interface_or_null (bfd_udp_main.vnet_main, sw_if_index);
570 u8 local_ip_valid = 0;
571 ip_interface_address_t *ia = NULL;
574 vlib_log_err (bum->log_class,
575 "got NULL sw_if when getting interface by index %u",
577 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
579 if (ip46_address_is_ip4 (local_addr))
581 if (!ip46_address_is_ip4 (peer_addr))
583 vlib_log_err (bum->log_class,
584 "IP family mismatch (local is ipv4, peer is ipv6)");
585 return VNET_API_ERROR_INVALID_ARGUMENT;
587 ip4_main_t *im = &ip4_main;
590 foreach_ip_interface_address (
591 &im->lookup_main, ia, sw_if_index, 0 /* honor unnumbered */, ({
593 ip_interface_address_get_address (&im->lookup_main, ia);
594 if (x->as_u32 == local_addr->ip4.as_u32)
596 /* valid address for this interface */
605 if (ip46_address_is_ip4 (peer_addr))
607 vlib_log_err (bum->log_class,
608 "IP family mismatch (local is ipv6, peer is ipv4)");
609 return VNET_API_ERROR_INVALID_ARGUMENT;
611 ip6_main_t *im = &ip6_main;
613 foreach_ip_interface_address (
614 &im->lookup_main, ia, sw_if_index, 0 /* honor unnumbered */, ({
616 ip_interface_address_get_address (&im->lookup_main, ia);
617 if (local_addr->ip6.as_u64[0] == x->as_u64[0] &&
618 local_addr->ip6.as_u64[1] == x->as_u64[1])
620 /* valid address for this interface */
630 vlib_log_err (bum->log_class,
631 "local address %U not found on interface with index %u",
632 format_ip46_address, local_addr, IP46_TYPE_ANY,
634 return VNET_API_ERROR_ADDRESS_NOT_FOUND_FOR_INTERFACE;
640 static vnet_api_error_t
641 bfd_udp_find_session_by_api_input (u32 sw_if_index,
642 const ip46_address_t * local_addr,
643 const ip46_address_t * peer_addr,
644 bfd_session_t ** bs_out)
646 vnet_api_error_t rv =
647 bfd_udp_validate_api_input (sw_if_index, local_addr, peer_addr);
650 bfd_udp_main_t *bum = &bfd_udp_main;
652 bfd_udp_key_init (&key, sw_if_index, local_addr, peer_addr);
653 bfd_session_t *bs = bfd_lookup_session (bum, &key);
660 vlib_log_err (bum->log_class,
661 "BFD session not found, sw_if_index=%u, local=%U, peer=%U",
662 sw_if_index, format_ip46_address, local_addr,
663 IP46_TYPE_ANY, format_ip46_address, peer_addr,
665 return VNET_API_ERROR_BFD_ENOENT;
671 static vnet_api_error_t
672 bfd_api_verify_common (u32 sw_if_index, u32 desired_min_tx_usec,
673 u32 required_min_rx_usec, u8 detect_mult,
674 const ip46_address_t * local_addr,
675 const ip46_address_t * peer_addr)
677 bfd_udp_main_t *bum = &bfd_udp_main;
678 vnet_api_error_t rv =
679 bfd_udp_validate_api_input (sw_if_index, local_addr, peer_addr);
686 vlib_log_err (bum->log_class, "detect_mult < 1");
687 return VNET_API_ERROR_INVALID_ARGUMENT;
689 if (desired_min_tx_usec < 1)
691 vlib_log_err (bum->log_class, "desired_min_tx_usec < 1");
692 return VNET_API_ERROR_INVALID_ARGUMENT;
698 bfd_udp_del_session_internal (vlib_main_t * vm, bfd_session_t * bs)
700 bfd_udp_main_t *bum = &bfd_udp_main;
701 BFD_DBG ("free bfd-udp session, bs_idx=%d", bs->bs_idx);
702 mhash_unset (&bum->bfd_session_idx_by_bfd_key, &bs->udp.key, NULL);
703 adj_unlock (bs->udp.adj_index);
704 switch (bs->transport)
706 case BFD_TRANSPORT_UDP4:
707 --bum->udp4_sessions_count;
708 if (!bum->udp4_sessions_count)
710 udp_unregister_dst_port (vm, UDP_DST_PORT_bfd4, 1);
711 udp_unregister_dst_port (vm, UDP_DST_PORT_bfd_echo4, 1);
714 case BFD_TRANSPORT_UDP6:
715 --bum->udp6_sessions_count;
716 if (!bum->udp6_sessions_count)
718 udp_unregister_dst_port (vm, UDP_DST_PORT_bfd6, 0);
719 udp_unregister_dst_port (vm, UDP_DST_PORT_bfd_echo6, 0);
723 bfd_put_session (bum->bfd_main, bs);
727 bfd_udp_add_session (u32 sw_if_index, const ip46_address_t * local_addr,
728 const ip46_address_t * peer_addr,
729 u32 desired_min_tx_usec, u32 required_min_rx_usec,
730 u8 detect_mult, u8 is_authenticated, u32 conf_key_id,
733 bfd_main_t *bm = &bfd_main;
736 vnet_api_error_t rv =
737 bfd_api_verify_common (sw_if_index, desired_min_tx_usec,
738 required_min_rx_usec, detect_mult,
739 local_addr, peer_addr);
740 bfd_session_t *bs = NULL;
744 bfd_udp_add_session_internal (vlib_get_main (), &bfd_udp_main,
745 sw_if_index, desired_min_tx_usec,
746 required_min_rx_usec, detect_mult,
747 local_addr, peer_addr, &bs);
749 if (!rv && is_authenticated)
752 rv = bfd_auth_activate (bs, conf_key_id, bfd_key_id,
753 0 /* is not delayed */ );
755 vlib_log_err (bfd_udp_main.log_class,
756 "SSL missing, cannot add authenticated BFD session");
757 rv = VNET_API_ERROR_BFD_NOTSUPP;
761 bfd_udp_del_session_internal (vlib_get_main (), bs);
766 bfd_session_start (bfd_udp_main.bfd_main, bs);
774 bfd_udp_mod_session (u32 sw_if_index,
775 const ip46_address_t * local_addr,
776 const ip46_address_t * peer_addr,
777 u32 desired_min_tx_usec,
778 u32 required_min_rx_usec, u8 detect_mult)
780 bfd_session_t *bs = NULL;
781 bfd_main_t *bm = &bfd_main;
782 vnet_api_error_t error;
784 vnet_api_error_t rv =
785 bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr,
793 error = bfd_session_set_params (bfd_udp_main.bfd_main, bs,
794 desired_min_tx_usec, required_min_rx_usec,
801 bfd_udp_del_session (u32 sw_if_index,
802 const ip46_address_t * local_addr,
803 const ip46_address_t * peer_addr)
805 bfd_session_t *bs = NULL;
806 bfd_main_t *bm = &bfd_main;
808 vnet_api_error_t rv =
809 bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr,
816 bfd_udp_del_session_internal (vlib_get_main (), bs);
822 bfd_udp_session_set_flags (u32 sw_if_index,
823 const ip46_address_t * local_addr,
824 const ip46_address_t * peer_addr, u8 admin_up_down)
826 bfd_session_t *bs = NULL;
827 bfd_main_t *bm = &bfd_main;
829 vnet_api_error_t rv =
830 bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr,
837 bfd_session_set_flags (bs, admin_up_down);
843 bfd_udp_auth_activate (u32 sw_if_index,
844 const ip46_address_t * local_addr,
845 const ip46_address_t * peer_addr,
846 u32 conf_key_id, u8 key_id, u8 is_delayed)
848 bfd_main_t *bm = &bfd_main;
850 vnet_api_error_t error;
853 bfd_session_t *bs = NULL;
854 vnet_api_error_t rv =
855 bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr,
862 error = bfd_auth_activate (bs, conf_key_id, key_id, is_delayed);
866 vlib_log_err (bfd_udp_main->log_class,
867 "SSL missing, cannot activate BFD authentication");
869 return VNET_API_ERROR_BFD_NOTSUPP;
874 bfd_udp_auth_deactivate (u32 sw_if_index,
875 const ip46_address_t * local_addr,
876 const ip46_address_t * peer_addr, u8 is_delayed)
878 bfd_main_t *bm = &bfd_main;
879 vnet_api_error_t error;
881 bfd_session_t *bs = NULL;
882 vnet_api_error_t rv =
883 bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr,
890 error = bfd_auth_deactivate (bs, is_delayed);
897 BFD_UDP_INPUT_NEXT_NORMAL,
898 BFD_UDP_INPUT_NEXT_REPLY_ARP,
899 BFD_UDP_INPUT_NEXT_REPLY_REWRITE,
900 BFD_UDP_INPUT_NEXT_REPLY_MIDCHAIN,
901 BFD_UDP_INPUT_N_NEXT,
902 } bfd_udp_input_next_t;
904 /* Packet counters - BFD control frames */
905 #define foreach_bfd_udp_error(F) \
906 F (NONE, "good bfd packets (processed)") \
907 F (BAD, "invalid bfd packets")
909 #define F(sym, string) static char BFD_UDP_ERR_##sym##_STR[] = string;
910 foreach_bfd_udp_error (F);
913 static char *bfd_udp_error_strings[] = {
914 #define F(sym, string) BFD_UDP_ERR_##sym##_STR,
915 foreach_bfd_udp_error (F)
921 #define F(sym, str) BFD_UDP_ERROR_##sym,
922 foreach_bfd_udp_error (F)
929 BFD_UDP_ECHO_INPUT_NEXT_NORMAL,
930 BFD_UDP_ECHO_INPUT_NEXT_REPLY_ARP,
931 BFD_UDP_ECHO_INPUT_NEXT_REPLY_REWRITE,
932 BFD_UDP_ECHO_INPUT_N_NEXT,
933 } bfd_udp_echo_input_next_t;
935 /* Packet counters - BFD ECHO packets */
936 #define foreach_bfd_udp_echo_error(F) \
937 F (NONE, "good bfd echo packets (processed)") \
938 F (BAD, "invalid bfd echo packets")
940 #define F(sym, string) static char BFD_UDP_ECHO_ERR_##sym##_STR[] = string;
941 foreach_bfd_udp_echo_error (F);
944 static char *bfd_udp_echo_error_strings[] = {
945 #define F(sym, string) BFD_UDP_ECHO_ERR_##sym##_STR,
946 foreach_bfd_udp_echo_error (F)
952 #define F(sym, str) BFD_UDP_ECHO_ERROR_##sym,
953 foreach_bfd_udp_echo_error (F)
955 BFD_UDP_ECHO_N_ERROR,
956 } bfd_udp_echo_error_t;
959 bfd_udp4_find_headers (vlib_buffer_t * b, ip4_header_t ** ip4,
962 /* sanity check first */
963 const i32 start = vnet_buffer (b)->l3_hdr_offset;
964 if (start < 0 && start < sizeof (b->pre_data))
966 BFD_ERR ("Start of ip header is before pre_data, ignoring");
971 *ip4 = (ip4_header_t *) (b->data + start);
972 if ((u8 *) * ip4 > (u8 *) vlib_buffer_get_current (b))
974 BFD_ERR ("Start of ip header is beyond current data, ignoring");
979 *udp = (udp_header_t *) ((*ip4) + 1);
982 static bfd_udp_error_t
983 bfd_udp4_verify_transport (const ip4_header_t * ip4,
984 const udp_header_t * udp, const bfd_session_t * bs)
986 const bfd_udp_session_t *bus = &bs->udp;
987 const bfd_udp_key_t *key = &bus->key;
988 if (ip4->src_address.as_u32 != key->peer_addr.ip4.as_u32)
990 BFD_ERR ("IPv4 src addr mismatch, got %U, expected %U",
991 format_ip4_address, ip4->src_address.as_u8, format_ip4_address,
992 key->peer_addr.ip4.as_u8);
993 return BFD_UDP_ERROR_BAD;
995 if (ip4->dst_address.as_u32 != key->local_addr.ip4.as_u32)
997 BFD_ERR ("IPv4 dst addr mismatch, got %U, expected %U",
998 format_ip4_address, ip4->dst_address.as_u8, format_ip4_address,
999 key->local_addr.ip4.as_u8);
1000 return BFD_UDP_ERROR_BAD;
1002 const u8 expected_ttl = 255;
1003 if (ip4->ttl != expected_ttl)
1005 BFD_ERR ("IPv4 unexpected TTL value %u, expected %u", ip4->ttl,
1007 return BFD_UDP_ERROR_BAD;
1009 if (clib_net_to_host_u16 (udp->src_port) < 49152)
1011 BFD_ERR ("Invalid UDP src port %u, out of range <49152,65535>",
1014 return BFD_UDP_ERROR_NONE;
1024 bfd_rpc_update_session (u32 bs_idx, const bfd_pkt_t * pkt)
1026 bfd_main_t *bm = &bfd_main;
1028 bfd_consume_pkt (bm, pkt, bs_idx);
1032 static bfd_udp_error_t
1033 bfd_udp4_scan (vlib_main_t * vm, vlib_node_runtime_t * rt,
1034 vlib_buffer_t * b, bfd_session_t ** bs_out)
1036 const bfd_pkt_t *pkt = vlib_buffer_get_current (b);
1037 if (sizeof (*pkt) > b->current_length)
1040 ("Payload size %d too small to hold bfd packet of minimum size %d",
1041 b->current_length, sizeof (*pkt));
1042 return BFD_UDP_ERROR_BAD;
1046 bfd_udp4_find_headers (b, &ip4, &udp);
1049 BFD_ERR ("Couldn't find ip4 or udp header");
1050 return BFD_UDP_ERROR_BAD;
1052 const u32 udp_payload_length = udp->length - sizeof (*udp);
1053 if (pkt->head.length > udp_payload_length)
1056 ("BFD packet length is larger than udp payload length (%u > %u)",
1057 pkt->head.length, udp_payload_length);
1058 return BFD_UDP_ERROR_BAD;
1060 if (!bfd_verify_pkt_common (pkt))
1062 return BFD_UDP_ERROR_BAD;
1064 bfd_session_t *bs = NULL;
1067 BFD_DBG ("Looking up BFD session using discriminator %u",
1069 bs = bfd_find_session_by_disc (bfd_udp_main.bfd_main, pkt->your_disc);
1074 clib_memset (&key, 0, sizeof (key));
1075 key.sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
1076 key.local_addr.ip4.as_u32 = ip4->dst_address.as_u32;
1077 key.peer_addr.ip4.as_u32 = ip4->src_address.as_u32;
1078 BFD_DBG ("Looking up BFD session using key (sw_if_index=%u, local=%U, "
1080 key.sw_if_index, format_ip4_address, key.local_addr.ip4.as_u8,
1081 format_ip4_address, key.peer_addr.ip4.as_u8);
1082 bs = bfd_lookup_session (&bfd_udp_main, &key);
1086 BFD_ERR ("BFD session lookup failed - no session matches BFD pkt");
1087 return BFD_UDP_ERROR_BAD;
1089 BFD_DBG ("BFD session found, bs_idx=%u", bs->bs_idx);
1090 if (!bfd_verify_pkt_auth (pkt, b->current_length, bs))
1092 BFD_ERR ("Packet verification failed, dropping packet");
1093 return BFD_UDP_ERROR_BAD;
1095 bfd_udp_error_t err;
1096 if (BFD_UDP_ERROR_NONE != (err = bfd_udp4_verify_transport (ip4, udp, bs)))
1100 bfd_rpc_update_session (bs->bs_idx, pkt);
1102 return BFD_UDP_ERROR_NONE;
1106 bfd_udp6_find_headers (vlib_buffer_t * b, ip6_header_t ** ip6,
1107 udp_header_t ** udp)
1109 /* sanity check first */
1110 const i32 start = vnet_buffer (b)->l3_hdr_offset;
1111 if (start < 0 && start < sizeof (b->pre_data))
1113 BFD_ERR ("Start of ip header is before pre_data, ignoring");
1118 *ip6 = (ip6_header_t *) (b->data + start);
1119 if ((u8 *) * ip6 > (u8 *) vlib_buffer_get_current (b))
1121 BFD_ERR ("Start of ip header is beyond current data, ignoring");
1126 if ((*ip6)->protocol != IP_PROTOCOL_UDP)
1128 BFD_ERR ("Unexpected protocol in IPv6 header '%u', expected '%u' (== "
1129 "IP_PROTOCOL_UDP)", (*ip6)->protocol, IP_PROTOCOL_UDP);
1134 *udp = (udp_header_t *) ((*ip6) + 1);
1137 static bfd_udp_error_t
1138 bfd_udp6_verify_transport (const ip6_header_t * ip6,
1139 const udp_header_t * udp, const bfd_session_t * bs)
1141 const bfd_udp_session_t *bus = &bs->udp;
1142 const bfd_udp_key_t *key = &bus->key;
1143 if (ip6->src_address.as_u64[0] != key->peer_addr.ip6.as_u64[0] &&
1144 ip6->src_address.as_u64[1] != key->peer_addr.ip6.as_u64[1])
1146 BFD_ERR ("IP src addr mismatch, got %U, expected %U",
1147 format_ip6_address, ip6, format_ip6_address,
1148 &key->peer_addr.ip6);
1149 return BFD_UDP_ERROR_BAD;
1151 if (ip6->dst_address.as_u64[0] != key->local_addr.ip6.as_u64[0] &&
1152 ip6->dst_address.as_u64[1] != key->local_addr.ip6.as_u64[1])
1154 BFD_ERR ("IP dst addr mismatch, got %U, expected %U",
1155 format_ip6_address, ip6, format_ip6_address,
1156 &key->local_addr.ip6);
1157 return BFD_UDP_ERROR_BAD;
1159 const u8 expected_hop_limit = 255;
1160 if (ip6->hop_limit != expected_hop_limit)
1162 BFD_ERR ("IPv6 unexpected hop-limit value %u, expected %u",
1163 ip6->hop_limit, expected_hop_limit);
1164 return BFD_UDP_ERROR_BAD;
1166 if (clib_net_to_host_u16 (udp->src_port) < 49152)
1168 BFD_ERR ("Invalid UDP src port %u, out of range <49152,65535>",
1171 return BFD_UDP_ERROR_NONE;
1174 static bfd_udp_error_t
1175 bfd_udp6_scan (vlib_main_t * vm, vlib_node_runtime_t * rt,
1176 vlib_buffer_t * b, bfd_session_t ** bs_out)
1178 const bfd_pkt_t *pkt = vlib_buffer_get_current (b);
1179 if (sizeof (*pkt) > b->current_length)
1182 ("Payload size %d too small to hold bfd packet of minimum size %d",
1183 b->current_length, sizeof (*pkt));
1184 return BFD_UDP_ERROR_BAD;
1188 bfd_udp6_find_headers (b, &ip6, &udp);
1191 BFD_ERR ("Couldn't find ip6 or udp header");
1192 return BFD_UDP_ERROR_BAD;
1194 const u32 udp_payload_length = udp->length - sizeof (*udp);
1195 if (pkt->head.length > udp_payload_length)
1198 ("BFD packet length is larger than udp payload length (%u > %u)",
1199 pkt->head.length, udp_payload_length);
1200 return BFD_UDP_ERROR_BAD;
1202 if (!bfd_verify_pkt_common (pkt))
1204 return BFD_UDP_ERROR_BAD;
1206 bfd_session_t *bs = NULL;
1209 BFD_DBG ("Looking up BFD session using discriminator %u",
1211 bs = bfd_find_session_by_disc (bfd_udp_main.bfd_main, pkt->your_disc);
1216 clib_memset (&key, 0, sizeof (key));
1217 key.sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
1218 key.local_addr.ip6.as_u64[0] = ip6->dst_address.as_u64[0];
1219 key.local_addr.ip6.as_u64[1] = ip6->dst_address.as_u64[1];
1220 key.peer_addr.ip6.as_u64[0] = ip6->src_address.as_u64[0];
1221 key.peer_addr.ip6.as_u64[1] = ip6->src_address.as_u64[1];
1222 BFD_DBG ("Looking up BFD session using key (sw_if_index=%u, local=%U, "
1224 key.sw_if_index, format_ip6_address, &key.local_addr,
1225 format_ip6_address, &key.peer_addr);
1226 bs = bfd_lookup_session (&bfd_udp_main, &key);
1230 BFD_ERR ("BFD session lookup failed - no session matches BFD pkt");
1231 return BFD_UDP_ERROR_BAD;
1233 BFD_DBG ("BFD session found, bs_idx=%u", bs->bs_idx);
1234 if (!bfd_verify_pkt_auth (pkt, b->current_length, bs))
1236 BFD_ERR ("Packet verification failed, dropping packet");
1237 return BFD_UDP_ERROR_BAD;
1239 bfd_udp_error_t err;
1240 if (BFD_UDP_ERROR_NONE != (err = bfd_udp6_verify_transport (ip6, udp, bs)))
1244 bfd_rpc_update_session (bs->bs_idx, pkt);
1246 return BFD_UDP_ERROR_NONE;
1250 * Process a frame of bfd packets
1251 * Expect 1 packet / frame
1254 bfd_udp_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
1255 vlib_frame_t * f, int is_ipv6)
1257 u32 n_left_from, *from;
1258 bfd_input_trace_t *t0;
1259 bfd_main_t *bm = &bfd_main;
1261 from = vlib_frame_vector_args (f); /* array of buffer indices */
1262 n_left_from = f->n_vectors; /* number of buffer indices */
1264 while (n_left_from > 0)
1271 b0 = vlib_get_buffer (vm, bi0);
1273 bfd_session_t *bs = NULL;
1275 /* If this pkt is traced, snapshot the data */
1276 if (b0->flags & VLIB_BUFFER_IS_TRACED)
1279 t0 = vlib_add_trace (vm, rt, b0, sizeof (*t0));
1280 len = (b0->current_length < sizeof (t0->data)) ? b0->current_length
1281 : sizeof (t0->data);
1283 clib_memcpy_fast (t0->data, vlib_buffer_get_current (b0), len);
1286 /* scan this bfd pkt. error0 is the counter index to bmp */
1290 error0 = bfd_udp6_scan (vm, rt, b0, &bs);
1294 error0 = bfd_udp4_scan (vm, rt, b0, &bs);
1296 b0->error = rt->errors[error0];
1298 next0 = BFD_UDP_INPUT_NEXT_NORMAL;
1299 if (BFD_UDP_ERROR_NONE == error0)
1302 * if everything went fine, check for poll bit, if present, re-use
1303 * the buffer and based on (now updated) session parameters, send
1304 * the final packet back
1306 const bfd_pkt_t *pkt = vlib_buffer_get_current (b0);
1307 if (bfd_pkt_get_poll (pkt))
1309 b0->current_data = 0;
1310 b0->current_length = 0;
1311 bfd_init_final_control_frame (vm, b0, bfd_udp_main.bfd_main, bs,
1315 vlib_node_increment_counter (vm, bfd_udp6_input_node.index,
1320 vlib_node_increment_counter (vm, bfd_udp4_input_node.index,
1323 const bfd_udp_session_t *bus = &bs->udp;
1324 ip_adjacency_t *adj = adj_get (bus->adj_index);
1325 switch (adj->lookup_next_index)
1327 case IP_LOOKUP_NEXT_ARP:
1328 next0 = BFD_UDP_INPUT_NEXT_REPLY_ARP;
1330 case IP_LOOKUP_NEXT_REWRITE:
1331 next0 = BFD_UDP_INPUT_NEXT_REPLY_REWRITE;
1333 case IP_LOOKUP_NEXT_MIDCHAIN:
1334 next0 = BFD_UDP_INPUT_NEXT_REPLY_MIDCHAIN;
1343 vlib_set_next_frame_buffer (vm, rt, next0, bi0);
1349 return f->n_vectors;
1353 bfd_udp4_input (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
1355 return bfd_udp_input (vm, rt, f, 0);
1359 * bfd input graph node declaration
1362 VLIB_REGISTER_NODE (bfd_udp4_input_node, static) = {
1363 .function = bfd_udp4_input,
1364 .name = "bfd-udp4-input",
1365 .vector_size = sizeof (u32),
1366 .type = VLIB_NODE_TYPE_INTERNAL,
1368 .n_errors = BFD_UDP_N_ERROR,
1369 .error_strings = bfd_udp_error_strings,
1371 .format_trace = bfd_input_format_trace,
1373 .n_next_nodes = BFD_UDP_INPUT_N_NEXT,
1376 [BFD_UDP_INPUT_NEXT_NORMAL] = "error-drop",
1377 [BFD_UDP_INPUT_NEXT_REPLY_ARP] = "ip4-arp",
1378 [BFD_UDP_INPUT_NEXT_REPLY_REWRITE] = "ip4-lookup",
1379 [BFD_UDP_INPUT_NEXT_REPLY_MIDCHAIN] = "ip4-midchain",
1385 bfd_udp6_input (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
1387 return bfd_udp_input (vm, rt, f, 1);
1391 VLIB_REGISTER_NODE (bfd_udp6_input_node, static) = {
1392 .function = bfd_udp6_input,
1393 .name = "bfd-udp6-input",
1394 .vector_size = sizeof (u32),
1395 .type = VLIB_NODE_TYPE_INTERNAL,
1397 .n_errors = BFD_UDP_N_ERROR,
1398 .error_strings = bfd_udp_error_strings,
1400 .format_trace = bfd_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] = "ip6-discover-neighbor",
1407 [BFD_UDP_INPUT_NEXT_REPLY_REWRITE] = "ip6-lookup",
1408 [BFD_UDP_INPUT_NEXT_REPLY_MIDCHAIN] = "ip6-midchain",
1414 * Process a frame of bfd echo packets
1415 * Expect 1 packet / frame
1418 bfd_udp_echo_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
1419 vlib_frame_t * f, int is_ipv6)
1421 u32 n_left_from, *from;
1422 bfd_input_trace_t *t0;
1423 bfd_main_t *bm = &bfd_main;
1425 from = vlib_frame_vector_args (f); /* array of buffer indices */
1426 n_left_from = f->n_vectors; /* number of buffer indices */
1428 while (n_left_from > 0)
1435 b0 = vlib_get_buffer (vm, bi0);
1437 /* If this pkt is traced, snapshot the data */
1438 if (b0->flags & VLIB_BUFFER_IS_TRACED)
1441 t0 = vlib_add_trace (vm, rt, b0, sizeof (*t0));
1442 len = (b0->current_length < sizeof (t0->data)) ? b0->current_length
1443 : sizeof (t0->data);
1445 clib_memcpy_fast (t0->data, vlib_buffer_get_current (b0), len);
1449 if (bfd_consume_echo_pkt (bfd_udp_main.bfd_main, b0))
1451 b0->error = rt->errors[BFD_UDP_ERROR_NONE];
1452 next0 = BFD_UDP_ECHO_INPUT_NEXT_NORMAL;
1456 /* loop back the packet */
1457 b0->error = rt->errors[BFD_UDP_ERROR_NONE];
1460 vlib_node_increment_counter (vm, bfd_udp_echo6_input_node.index,
1465 vlib_node_increment_counter (vm, bfd_udp_echo4_input_node.index,
1468 next0 = BFD_UDP_ECHO_INPUT_NEXT_REPLY_REWRITE;
1472 vlib_set_next_frame_buffer (vm, rt, next0, bi0);
1478 return f->n_vectors;
1482 bfd_udp_echo4_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
1485 return bfd_udp_echo_input (vm, rt, f, 0);
1489 bfd_echo_input_format_trace (u8 * s, va_list * args)
1491 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
1492 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
1493 const bfd_udp_echo_input_trace_t *t =
1494 va_arg (*args, bfd_udp_echo_input_trace_t *);
1495 if (t->len > STRUCT_SIZE_OF (bfd_pkt_t, head))
1497 s = format (s, "BFD ECHO:\n");
1498 s = format (s, " data: %U", format_hexdump, t->data, t->len);
1505 * bfd input graph node declaration
1508 VLIB_REGISTER_NODE (bfd_udp_echo4_input_node, static) = {
1509 .function = bfd_udp_echo4_input,
1510 .name = "bfd-udp-echo4-input",
1511 .vector_size = sizeof (u32),
1512 .type = VLIB_NODE_TYPE_INTERNAL,
1514 .n_errors = BFD_UDP_ECHO_N_ERROR,
1515 .error_strings = bfd_udp_error_strings,
1517 .format_trace = bfd_echo_input_format_trace,
1519 .n_next_nodes = BFD_UDP_ECHO_INPUT_N_NEXT,
1522 [BFD_UDP_ECHO_INPUT_NEXT_NORMAL] = "error-drop",
1523 [BFD_UDP_ECHO_INPUT_NEXT_REPLY_ARP] = "ip4-arp",
1524 [BFD_UDP_ECHO_INPUT_NEXT_REPLY_REWRITE] = "ip4-lookup",
1530 bfd_udp_echo6_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
1533 return bfd_udp_echo_input (vm, rt, f, 1);
1537 VLIB_REGISTER_NODE (bfd_udp_echo6_input_node, static) = {
1538 .function = bfd_udp_echo6_input,
1539 .name = "bfd-udp-echo6-input",
1540 .vector_size = sizeof (u32),
1541 .type = VLIB_NODE_TYPE_INTERNAL,
1543 .n_errors = BFD_UDP_ECHO_N_ERROR,
1544 .error_strings = bfd_udp_echo_error_strings,
1546 .format_trace = bfd_echo_input_format_trace,
1548 .n_next_nodes = BFD_UDP_ECHO_INPUT_N_NEXT,
1551 [BFD_UDP_ECHO_INPUT_NEXT_NORMAL] = "error-drop",
1552 [BFD_UDP_ECHO_INPUT_NEXT_REPLY_ARP] = "ip6-discover-neighbor",
1553 [BFD_UDP_ECHO_INPUT_NEXT_REPLY_REWRITE] = "ip6-lookup",
1559 static clib_error_t *
1560 bfd_udp_sw_if_add_del (vnet_main_t * vnm, u32 sw_if_index, u32 is_create)
1562 bfd_session_t **to_be_freed = NULL;
1563 bfd_udp_main_t *bum = &bfd_udp_main;
1564 BFD_DBG ("sw_if_add_del called, sw_if_index=%u, is_create=%u", sw_if_index,
1569 pool_foreach (bs, bfd_udp_main.bfd_main->sessions,
1571 if (bs->transport != BFD_TRANSPORT_UDP4 &&
1572 bs->transport != BFD_TRANSPORT_UDP6)
1575 if (bs->udp.key.sw_if_index != sw_if_index)
1578 vec_add1 (to_be_freed, bs);}
1582 vec_foreach (bs, to_be_freed)
1584 vlib_log_notice (bum->log_class,
1585 "removal of sw_if_index=%u forces removal of bfd session "
1586 "with bs_idx=%u", sw_if_index, (*bs)->bs_idx);
1587 bfd_session_set_flags (*bs, 0);
1588 bfd_udp_del_session_internal (vlib_get_main (), *bs);
1593 VNET_SW_INTERFACE_ADD_DEL_FUNCTION (bfd_udp_sw_if_add_del);
1598 static clib_error_t *
1599 bfd_udp_init (vlib_main_t * vm)
1601 bfd_udp_main.udp4_sessions_count = 0;
1602 bfd_udp_main.udp6_sessions_count = 0;
1603 mhash_init (&bfd_udp_main.bfd_session_idx_by_bfd_key, sizeof (uword),
1604 sizeof (bfd_udp_key_t));
1605 bfd_udp_main.bfd_main = &bfd_main;
1606 bfd_udp_main.vnet_main = vnet_get_main ();
1607 vlib_node_t *node = vlib_get_node_by_name (vm, (u8 *) "ip4-arp");
1609 bfd_udp_main.ip4_arp_idx = node->index;
1610 node = vlib_get_node_by_name (vm, (u8 *) "ip6-discover-neighbor");
1612 bfd_udp_main.ip6_ndp_idx = node->index;
1613 node = vlib_get_node_by_name (vm, (u8 *) "ip4-rewrite");
1615 bfd_udp_main.ip4_rewrite_idx = node->index;
1616 node = vlib_get_node_by_name (vm, (u8 *) "ip6-rewrite");
1618 bfd_udp_main.ip6_rewrite_idx = node->index;
1619 node = vlib_get_node_by_name (vm, (u8 *) "ip4-midchain");
1621 bfd_udp_main.ip4_midchain_idx = node->index;
1622 node = vlib_get_node_by_name (vm, (u8 *) "ip6-midchain");
1624 bfd_udp_main.ip6_midchain_idx = node->index;
1626 bfd_udp_main.log_class = vlib_log_register_class ("bfd", "udp");
1627 vlib_log_debug (bfd_udp_main.log_class, "initialized");
1631 VLIB_INIT_FUNCTION (bfd_udp_init);
1634 * fd.io coding-style-patch-verification: ON
1637 * eval: (c-set-style "gnu")