bfd: refactor code to fix misc warnings
[vpp.git] / src / vnet / bfd / bfd_udp.c
index cc3b40c..d8fd4a1 100644 (file)
 #include <vlib/buffer.h>
 #include <vnet/ip/format.h>
 #include <vnet/ethernet/packet.h>
+#include <vnet/udp/udp_local.h>
 #include <vnet/udp/udp_packet.h>
-#include <vnet/udp/udp.h>
 #include <vnet/ip/lookup.h>
 #include <vnet/ip/icmp46_packet.h>
 #include <vnet/ip/ip4.h>
 #include <vnet/ip/ip6.h>
 #include <vnet/ip/ip6_packet.h>
+#include <vnet/ip/ip6_link.h>
 #include <vnet/adj/adj.h>
 #include <vnet/adj/adj_nbr.h>
 #include <vnet/dpo/receive_dpo.h>
@@ -608,21 +609,35 @@ bfd_udp_validate_api_input (u32 sw_if_index,
                        "IP family mismatch (local is ipv6, peer is ipv4)");
          return VNET_API_ERROR_INVALID_ARGUMENT;
        }
-      ip6_main_t *im = &ip6_main;
-      /* *INDENT-OFF* */
-      foreach_ip_interface_address (
-          &im->lookup_main, ia, sw_if_index, 0 /* honor unnumbered */, ({
-            ip6_address_t *x =
-                ip_interface_address_get_address (&im->lookup_main, ia);
-            if (local_addr->ip6.as_u64[0] == x->as_u64[0] &&
-                local_addr->ip6.as_u64[1] == x->as_u64[1])
-              {
-                /* valid address for this interface */
-                local_ip_valid = 1;
-                break;
-              }
-          }));
-      /* *INDENT-ON* */
+
+      if (ip6_address_is_link_local_unicast (&local_addr->ip6))
+       {
+         const ip6_address_t *ll_addr;
+         ll_addr = ip6_get_link_local_address (sw_if_index);
+         if (ll_addr && ip6_address_is_equal (ll_addr, &local_addr->ip6))
+           {
+             /* valid address for this interface */
+             local_ip_valid = 1;
+           }
+       }
+      else
+       {
+         ip6_main_t *im = &ip6_main;
+         /* *INDENT-OFF* */
+         foreach_ip_interface_address (
+             &im->lookup_main, ia, sw_if_index, 0 /* honor unnumbered */, ({
+               ip6_address_t *x =
+                   ip_interface_address_get_address (&im->lookup_main, ia);
+               if (local_addr->ip6.as_u64[0] == x->as_u64[0] &&
+                   local_addr->ip6.as_u64[1] == x->as_u64[1])
+                 {
+                   /* valid address for this interface */
+                   local_ip_valid = 1;
+                   break;
+                 }
+             }));
+         /* *INDENT-ON* */
+       }
     }
 
   if (!local_ip_valid)
@@ -670,9 +685,8 @@ bfd_udp_find_session_by_api_input (u32 sw_if_index,
 
 static vnet_api_error_t
 bfd_api_verify_common (u32 sw_if_index, u32 desired_min_tx_usec,
-                      u32 required_min_rx_usec, u8 detect_mult,
-                      const ip46_address_t * local_addr,
-                      const ip46_address_t * peer_addr)
+                      u8 detect_mult, const ip46_address_t *local_addr,
+                      const ip46_address_t *peer_addr)
 {
   bfd_udp_main_t *bum = &bfd_udp_main;
   vnet_api_error_t rv =
@@ -723,39 +737,26 @@ bfd_udp_del_session_internal (vlib_main_t * vm, bfd_session_t * bs)
   bfd_put_session (bum->bfd_main, bs);
 }
 
-vnet_api_error_t
-bfd_udp_add_session (u32 sw_if_index, const ip46_address_t * local_addr,
-                    const ip46_address_t * peer_addr,
-                    u32 desired_min_tx_usec, u32 required_min_rx_usec,
-                    u8 detect_mult, u8 is_authenticated, u32 conf_key_id,
-                    u8 bfd_key_id)
+static vnet_api_error_t
+bfd_udp_add_and_start_session (u32 sw_if_index,
+                              const ip46_address_t *local_addr,
+                              const ip46_address_t *peer_addr,
+                              u32 desired_min_tx_usec,
+                              u32 required_min_rx_usec, u8 detect_mult,
+                              u8 is_authenticated, u32 conf_key_id,
+                              u8 bfd_key_id)
 {
-  bfd_main_t *bm = &bfd_main;
-  bfd_lock (bm);
-
-  vnet_api_error_t rv =
-    bfd_api_verify_common (sw_if_index, desired_min_tx_usec,
-                          required_min_rx_usec, detect_mult,
-                          local_addr, peer_addr);
   bfd_session_t *bs = NULL;
-  if (!rv)
-    {
-      rv =
-       bfd_udp_add_session_internal (vlib_get_main (), &bfd_udp_main,
-                                     sw_if_index, desired_min_tx_usec,
-                                     required_min_rx_usec, detect_mult,
-                                     local_addr, peer_addr, &bs);
-    }
+  vnet_api_error_t rv;
+
+  rv = bfd_udp_add_session_internal (
+    vlib_get_main (), &bfd_udp_main, sw_if_index, desired_min_tx_usec,
+    required_min_rx_usec, detect_mult, local_addr, peer_addr, &bs);
+
   if (!rv && is_authenticated)
     {
-#if WITH_LIBSSL > 0
       rv = bfd_auth_activate (bs, conf_key_id, bfd_key_id,
-                             0 /* is not delayed */ );
-#else
-      vlib_log_err (bfd_udp_main.log_class,
-                   "SSL missing, cannot add authenticated BFD session");
-      rv = VNET_API_ERROR_BFD_NOTSUPP;
-#endif
+                             0 /* is not delayed */);
       if (rv)
        {
          bfd_udp_del_session_internal (vlib_get_main (), bs);
@@ -766,15 +767,67 @@ bfd_udp_add_session (u32 sw_if_index, const ip46_address_t * local_addr,
       bfd_session_start (bfd_udp_main.bfd_main, bs);
     }
 
-  bfd_unlock (bm);
   return rv;
 }
 
 vnet_api_error_t
-bfd_udp_mod_session (u32 sw_if_index,
-                    const ip46_address_t * local_addr,
+bfd_udp_add_session (u32 sw_if_index, const ip46_address_t * local_addr,
                     const ip46_address_t * peer_addr,
-                    u32 desired_min_tx_usec,
+                    u32 desired_min_tx_usec, u32 required_min_rx_usec,
+                    u8 detect_mult, u8 is_authenticated, u32 conf_key_id,
+                    u8 bfd_key_id)
+{
+  bfd_main_t *bm = &bfd_main;
+  bfd_lock (bm);
+
+  vnet_api_error_t rv = bfd_api_verify_common (
+    sw_if_index, desired_min_tx_usec, detect_mult, local_addr, peer_addr);
+
+  if (!rv)
+    rv = bfd_udp_add_and_start_session (
+      sw_if_index, local_addr, peer_addr, desired_min_tx_usec,
+      required_min_rx_usec, detect_mult, is_authenticated, conf_key_id,
+      bfd_key_id);
+
+  bfd_unlock (bm);
+  return rv;
+}
+
+vnet_api_error_t
+bfd_udp_upd_session (u32 sw_if_index, const ip46_address_t *local_addr,
+                    const ip46_address_t *peer_addr, u32 desired_min_tx_usec,
+                    u32 required_min_rx_usec, u8 detect_mult,
+                    u8 is_authenticated, u32 conf_key_id, u8 bfd_key_id)
+{
+  bfd_main_t *bm = &bfd_main;
+  bfd_lock (bm);
+
+  vnet_api_error_t rv = bfd_api_verify_common (
+    sw_if_index, desired_min_tx_usec, detect_mult, local_addr, peer_addr);
+  if (!rv)
+    {
+      bfd_session_t *bs = NULL;
+
+      rv = bfd_udp_find_session_by_api_input (sw_if_index, local_addr,
+                                             peer_addr, &bs);
+      if (VNET_API_ERROR_BFD_ENOENT == rv)
+       rv = bfd_udp_add_and_start_session (
+         sw_if_index, local_addr, peer_addr, desired_min_tx_usec,
+         required_min_rx_usec, detect_mult, is_authenticated, conf_key_id,
+         bfd_key_id);
+      else
+       rv = bfd_session_set_params (bfd_udp_main.bfd_main, bs,
+                                    desired_min_tx_usec, required_min_rx_usec,
+                                    detect_mult);
+    }
+
+  bfd_unlock (bm);
+  return rv;
+}
+
+vnet_api_error_t
+bfd_udp_mod_session (u32 sw_if_index, const ip46_address_t *local_addr,
+                    const ip46_address_t *peer_addr, u32 desired_min_tx_usec,
                     u32 required_min_rx_usec, u8 detect_mult)
 {
   bfd_session_t *bs = NULL;
@@ -819,7 +872,7 @@ bfd_udp_del_session (u32 sw_if_index,
 }
 
 vnet_api_error_t
-bfd_udp_session_set_flags (u32 sw_if_index,
+bfd_udp_session_set_flags (vlib_main_t * vm, u32 sw_if_index,
                           const ip46_address_t * local_addr,
                           const ip46_address_t * peer_addr, u8 admin_up_down)
 {
@@ -834,7 +887,7 @@ bfd_udp_session_set_flags (u32 sw_if_index,
       bfd_unlock (bm);
       return rv;
     }
-  bfd_session_set_flags (bs, admin_up_down);
+  bfd_session_set_flags (vm, bs, admin_up_down);
   bfd_unlock (bm);
   return 0;
 }
@@ -849,7 +902,6 @@ bfd_udp_auth_activate (u32 sw_if_index,
   bfd_lock (bm);
   vnet_api_error_t error;
 
-#if WITH_LIBSSL > 0
   bfd_session_t *bs = NULL;
   vnet_api_error_t rv =
     bfd_udp_find_session_by_api_input (sw_if_index, local_addr, peer_addr,
@@ -862,12 +914,6 @@ bfd_udp_auth_activate (u32 sw_if_index,
   error = bfd_auth_activate (bs, conf_key_id, key_id, is_delayed);
   bfd_unlock (bm);
   return error;
-#else
-  vlib_log_err (bfd_udp_main->log_class,
-               "SSL missing, cannot activate BFD authentication");
-  bfd_unlock (bm);
-  return VNET_API_ERROR_BFD_NOTSUPP;
-#endif
 }
 
 vnet_api_error_t
@@ -961,7 +1007,7 @@ bfd_udp4_find_headers (vlib_buffer_t * b, ip4_header_t ** ip4,
 {
   /* sanity check first */
   const i32 start = vnet_buffer (b)->l3_hdr_offset;
-  if (start < 0 && start < sizeof (b->pre_data))
+  if (start < -(signed) sizeof (b->pre_data))
     {
       BFD_ERR ("Start of ip header is before pre_data, ignoring");
       *ip4 = NULL;
@@ -1021,17 +1067,16 @@ typedef struct
 } bfd_rpc_update_t;
 
 static void
-bfd_rpc_update_session (u32 bs_idx, const bfd_pkt_t * pkt)
+bfd_rpc_update_session (vlib_main_t * vm, u32 bs_idx, const bfd_pkt_t * pkt)
 {
   bfd_main_t *bm = &bfd_main;
   bfd_lock (bm);
-  bfd_consume_pkt (bm, pkt, bs_idx);
+  bfd_consume_pkt (vm, bm, pkt, bs_idx);
   bfd_unlock (bm);
 }
 
 static bfd_udp_error_t
-bfd_udp4_scan (vlib_main_t * vm, vlib_node_runtime_t * rt,
-              vlib_buffer_t * b, bfd_session_t ** bs_out)
+bfd_udp4_scan (vlib_main_t *vm, vlib_buffer_t *b, bfd_session_t **bs_out)
 {
   const bfd_pkt_t *pkt = vlib_buffer_get_current (b);
   if (sizeof (*pkt) > b->current_length)
@@ -1087,7 +1132,7 @@ bfd_udp4_scan (vlib_main_t * vm, vlib_node_runtime_t * rt,
       return BFD_UDP_ERROR_BAD;
     }
   BFD_DBG ("BFD session found, bs_idx=%u", bs->bs_idx);
-  if (!bfd_verify_pkt_auth (pkt, b->current_length, bs))
+  if (!bfd_verify_pkt_auth (vm, pkt, b->current_length, bs))
     {
       BFD_ERR ("Packet verification failed, dropping packet");
       return BFD_UDP_ERROR_BAD;
@@ -1097,7 +1142,7 @@ bfd_udp4_scan (vlib_main_t * vm, vlib_node_runtime_t * rt,
     {
       return err;
     }
-  bfd_rpc_update_session (bs->bs_idx, pkt);
+  bfd_rpc_update_session (vm, bs->bs_idx, pkt);
   *bs_out = bs;
   return BFD_UDP_ERROR_NONE;
 }
@@ -1108,7 +1153,7 @@ bfd_udp6_find_headers (vlib_buffer_t * b, ip6_header_t ** ip6,
 {
   /* sanity check first */
   const i32 start = vnet_buffer (b)->l3_hdr_offset;
-  if (start < 0 && start < sizeof (b->pre_data))
+  if (start < -(signed) sizeof (b->pre_data))
     {
       BFD_ERR ("Start of ip header is before pre_data, ignoring");
       *ip6 = NULL;
@@ -1172,8 +1217,7 @@ bfd_udp6_verify_transport (const ip6_header_t * ip6,
 }
 
 static bfd_udp_error_t
-bfd_udp6_scan (vlib_main_t * vm, vlib_node_runtime_t * rt,
-              vlib_buffer_t * b, bfd_session_t ** bs_out)
+bfd_udp6_scan (vlib_main_t *vm, vlib_buffer_t *b, bfd_session_t **bs_out)
 {
   const bfd_pkt_t *pkt = vlib_buffer_get_current (b);
   if (sizeof (*pkt) > b->current_length)
@@ -1231,7 +1275,7 @@ bfd_udp6_scan (vlib_main_t * vm, vlib_node_runtime_t * rt,
       return BFD_UDP_ERROR_BAD;
     }
   BFD_DBG ("BFD session found, bs_idx=%u", bs->bs_idx);
-  if (!bfd_verify_pkt_auth (pkt, b->current_length, bs))
+  if (!bfd_verify_pkt_auth (vm, pkt, b->current_length, bs))
     {
       BFD_ERR ("Packet verification failed, dropping packet");
       return BFD_UDP_ERROR_BAD;
@@ -1241,7 +1285,7 @@ bfd_udp6_scan (vlib_main_t * vm, vlib_node_runtime_t * rt,
     {
       return err;
     }
-  bfd_rpc_update_session (bs->bs_idx, pkt);
+  bfd_rpc_update_session (vm, bs->bs_idx, pkt);
   *bs_out = bs;
   return BFD_UDP_ERROR_NONE;
 }
@@ -1275,7 +1319,7 @@ bfd_udp_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
       /* If this pkt is traced, snapshot the data */
       if (b0->flags & VLIB_BUFFER_IS_TRACED)
        {
-         int len;
+         u64 len;
          t0 = vlib_add_trace (vm, rt, b0, sizeof (*t0));
          len = (b0->current_length < sizeof (t0->data)) ? b0->current_length
            : sizeof (t0->data);
@@ -1287,11 +1331,11 @@ bfd_udp_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
       bfd_lock (bm);
       if (is_ipv6)
        {
-         error0 = bfd_udp6_scan (vm, rt, b0, &bs);
+         error0 = bfd_udp6_scan (vm, b0, &bs);
        }
       else
        {
-         error0 = bfd_udp4_scan (vm, rt, b0, &bs);
+         error0 = bfd_udp4_scan (vm, b0, &bs);
        }
       b0->error = rt->errors[error0];
 
@@ -1308,8 +1352,7 @@ bfd_udp_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
            {
              b0->current_data = 0;
              b0->current_length = 0;
-             bfd_init_final_control_frame (vm, b0, bfd_udp_main.bfd_main, bs,
-                                           0);
+             bfd_init_final_control_frame (vm, b0, bs);
              if (is_ipv6)
                {
                  vlib_node_increment_counter (vm, bfd_udp6_input_node.index,
@@ -1437,7 +1480,7 @@ bfd_udp_echo_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
       /* If this pkt is traced, snapshot the data */
       if (b0->flags & VLIB_BUFFER_IS_TRACED)
        {
-         int len;
+         u64 len;
          t0 = vlib_add_trace (vm, rt, b0, sizeof (*t0));
          len = (b0->current_length < sizeof (t0->data)) ? b0->current_length
            : sizeof (t0->data);
@@ -1446,7 +1489,7 @@ bfd_udp_echo_input (vlib_main_t * vm, vlib_node_runtime_t * rt,
        }
 
       bfd_lock (bm);
-      if (bfd_consume_echo_pkt (bfd_udp_main.bfd_main, b0))
+      if (bfd_consume_echo_pkt (vm, bfd_udp_main.bfd_main, b0))
        {
          b0->error = rt->errors[BFD_UDP_ERROR_NONE];
          next0 = BFD_UDP_ECHO_INPUT_NEXT_NORMAL;
@@ -1557,36 +1600,41 @@ VLIB_REGISTER_NODE (bfd_udp_echo6_input_node, static) = {
 /* *INDENT-ON* */
 
 static clib_error_t *
-bfd_udp_sw_if_add_del (vnet_main_t * vnm, u32 sw_if_index, u32 is_create)
+bfd_udp_sw_if_add_del (CLIB_UNUSED (vnet_main_t *vnm), u32 sw_if_index,
+                      u32 is_create)
 {
-  bfd_session_t **to_be_freed = NULL;
+  u32 *to_be_freed = NULL;
   bfd_udp_main_t *bum = &bfd_udp_main;
   BFD_DBG ("sw_if_add_del called, sw_if_index=%u, is_create=%u", sw_if_index,
           is_create);
   if (!is_create)
     {
       bfd_session_t *bs;
-      pool_foreach (bs, bfd_udp_main.bfd_main->sessions,
-                   {
-                   if (bs->transport != BFD_TRANSPORT_UDP4 &&
-                       bs->transport != BFD_TRANSPORT_UDP6)
-                   {
-                   continue;}
-                   if (bs->udp.key.sw_if_index != sw_if_index)
-                   {
-                   continue;}
-                   vec_add1 (to_be_freed, bs);}
-      );
-    }
-  bfd_session_t **bs;
-  vec_foreach (bs, to_be_freed)
-  {
-    vlib_log_notice (bum->log_class,
-                    "removal of sw_if_index=%u forces removal of bfd session "
-                    "with bs_idx=%u", sw_if_index, (*bs)->bs_idx);
-    bfd_session_set_flags (*bs, 0);
-    bfd_udp_del_session_internal (vlib_get_main (), *bs);
-  }
+      pool_foreach (bs, bum->bfd_main->sessions)
+       {
+         if (bs->transport != BFD_TRANSPORT_UDP4 &&
+             bs->transport != BFD_TRANSPORT_UDP6)
+           {
+             continue;
+           }
+         if (bs->udp.key.sw_if_index != sw_if_index)
+           {
+             continue;
+           }
+         vec_add1 (to_be_freed, bs->bs_idx);
+       }
+    }
+  u32 *bs_idx;
+  vec_foreach (bs_idx, to_be_freed)
+    {
+      bfd_session_t *bs = pool_elt_at_index (bum->bfd_main->sessions, *bs_idx);
+      vlib_log_notice (bum->log_class,
+                      "removal of sw_if_index=%u forces removal of bfd "
+                      "session with bs_idx=%u",
+                      sw_if_index, bs->bs_idx);
+      bfd_session_set_flags (vlib_get_main (), bs, 0);
+      bfd_udp_del_session_internal (vlib_get_main (), bs);
+    }
   return 0;
 }