summaryrefslogtreecommitdiffstats
path: root/src/vnet/lisp-gpe/interface.c
diff options
context:
space:
mode:
authorBilly McFall <bmcfall@redhat.com>2017-02-15 11:39:12 -0500
committerDave Barach <openvpp@barachs.net>2017-02-22 16:23:12 +0000
commita9a20e7f69f4a91a4d5267ab5ce14125bdc7d6c6 (patch)
tree58647f28d51d1cac3e7aa4e9ca94280192e6ec25 /src/vnet/lisp-gpe/interface.c
parent2291a36008e197423a0f0414f6dcca4afa3ac4c1 (diff)
VPP-635: CLI Memory leak with invalid parameter
In the CLI parsing, below is a common pattern: /* Get a line of input. */ if (!unformat_user (input, unformat_line_input, line_input)) return 0; while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) { if (unformat (line_input, "x")) x = 1; : else return clib_error_return (0, "unknown input `%U'", format_unformat_error, line_input); } unformat_free (line_input); The 'else' returns if an unknown string is encountered. There a memory leak because the 'unformat_free(line_input)' is not called. There is a large number of instances of this pattern. Replaced the previous pattern with: /* Get a line of input. */ if (!unformat_user (input, unformat_line_input, line_input)) return 0; while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) { if (unformat (line_input, "x")) x = 1; : else { error = clib_error_return (0, "unknown input `%U'", format_unformat_error, line_input); goto done: } } /* ...Remaining code... */ done: unformat_free (line_input); return error; } In multiple files, 'unformat_free (line_input);' was never called, so there was a memory leak whether an invalid string was entered or not. Also, there were multiple instance where: error = clib_error_return (0, "unknown input `%U'", format_unformat_error, line_input); used 'input' as the last parameter instead of 'line_input'. The result is that output did not contain the substring in error, instead just an empty string. Fixed all of those as well. There are a lot of file, and very mind numbing work, so tried to keep it to a pattern to avoid mistakes. Change-Id: I8902f0c32a47dd7fb3bb3471a89818571702f1d2 Signed-off-by: Billy McFall <bmcfall@redhat.com> Signed-off-by: Dave Barach <dave@barachs.net>
Diffstat (limited to 'src/vnet/lisp-gpe/interface.c')
-rw-r--r--src/vnet/lisp-gpe/interface.c58
1 files changed, 48 insertions, 10 deletions
diff --git a/src/vnet/lisp-gpe/interface.c b/src/vnet/lisp-gpe/interface.c
index 2142e095119..19ac22e7a56 100644
--- a/src/vnet/lisp-gpe/interface.c
+++ b/src/vnet/lisp-gpe/interface.c
@@ -794,6 +794,7 @@ lisp_gpe_add_del_iface_command_fn (vlib_main_t * vm, unformat_input_t * input,
u32 table_id, vni, bd_id;
u8 vni_is_set = 0, vrf_is_set = 0, bd_index_is_set = 0;
u8 nsh_iface = 0;
+ clib_error_t *error = NULL;
if (vnet_lisp_gpe_enable_disable_status () == 0)
{
@@ -828,8 +829,9 @@ lisp_gpe_add_del_iface_command_fn (vlib_main_t * vm, unformat_input_t * input,
}
else
{
- return clib_error_return (0, "parse error: '%U'",
- format_unformat_error, line_input);
+ error = clib_error_return (0, "parse error: '%U'",
+ format_unformat_error, line_input);
+ goto done;
}
}
@@ -839,7 +841,8 @@ lisp_gpe_add_del_iface_command_fn (vlib_main_t * vm, unformat_input_t * input,
{
if (~0 == lisp_gpe_add_nsh_iface (&lisp_gpe_main))
{
- return clib_error_return (0, "NSH interface not created");
+ error = clib_error_return (0, "NSH interface not created");
+ goto done;
}
}
else
@@ -850,21 +853,34 @@ lisp_gpe_add_del_iface_command_fn (vlib_main_t * vm, unformat_input_t * input,
}
if (vrf_is_set && bd_index_is_set)
- return clib_error_return (0,
- "Cannot set both vrf and brdige domain index!");
+ {
+ error = clib_error_return
+ (0, "Cannot set both vrf and brdige domain index!");
+ goto done;
+ }
if (!vni_is_set)
- return clib_error_return (0, "vni must be set!");
+ {
+ error = clib_error_return (0, "vni must be set!");
+ goto done;
+ }
if (!vrf_is_set && !bd_index_is_set)
- return clib_error_return (0, "vrf or bridge domain index must be set!");
+ {
+ error =
+ clib_error_return (0, "vrf or bridge domain index must be set!");
+ goto done;
+ }
if (bd_index_is_set)
{
if (is_add)
{
if (~0 == lisp_gpe_tenant_l2_iface_add_or_lock (vni, bd_id))
- return clib_error_return (0, "L2 interface not created");
+ {
+ error = clib_error_return (0, "L2 interface not created");
+ goto done;
+ }
}
else
lisp_gpe_tenant_l2_iface_unlock (vni);
@@ -874,13 +890,35 @@ lisp_gpe_add_del_iface_command_fn (vlib_main_t * vm, unformat_input_t * input,
if (is_add)
{
if (~0 == lisp_gpe_tenant_l3_iface_add_or_lock (vni, table_id))
- return clib_error_return (0, "L3 interface not created");
+ {
+ error = clib_error_return (0, "L3 interface not created");
+ goto done;
+ }
}
else
lisp_gpe_tenant_l3_iface_unlock (vni);
}
- return (NULL);
+ if (nsh_iface)
+ {
+ if (is_add)
+ {
+ if (~0 == lisp_gpe_add_nsh_iface (&lisp_gpe_main))
+ {
+ error = clib_error_return (0, "NSH interface not created");
+ goto done;
+ }
+ else
+ {
+ lisp_gpe_del_nsh_iface (&lisp_gpe_main);
+ }
+ }
+ }
+
+done:
+ unformat_free (line_input);
+
+ return error;
}
/* *INDENT-OFF* */
376' href='#n376'>376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
;;; tunnel-c-skel.el - tunnel encap cli / api

(require 'skeleton)

(define-skeleton skel-tunnel-c
"Insert a tunnel cli/api implementation"
nil
'(setq encap_stack (skeleton-read "encap_stack (e.g ip4_udp_lisp): "))
'(setq ENCAP_STACK (upcase encap_stack))
'(setq encap-stack (replace-regexp-in-string "_" "-" encap_stack))
"
#include <vnet/" encap-stack "/" encap_stack" .h>

" encap_stack "_main_t " encap_stack "_main;

static u8 * format_decap_next (u8 * s, va_list * args)
{
  u32 next_index = va_arg (*args, u32);

  switch (next_index)
    {
    case " ENCAP_STACK "_INPUT_NEXT_DROP:
      return format (s, \"drop\");
    case " ENCAP_STACK "_INPUT_NEXT_IP4_INPUT:
      return format (s, \"ip4\");
    case " ENCAP_STACK "_INPUT_NEXT_IP6_INPUT:
      return format (s, \"ip6\");
    case " ENCAP_STACK "_INPUT_NEXT_" ENCAP_STACK "_ENCAP:
      return format (s, \"" encap-stack "\");
    default:
      return format (s, \"unknown %d\", next_index);
    }
  return s;
}

u8 * format_" encap_stack "_tunnel (u8 * s, va_list * args)
{
  " encap_stack "_tunnel_t * t = va_arg (*args, " encap_stack "_tunnel_t *);
  " encap_stack "_main_t * ngm = &" encap_stack "_main;

  s = format (s, 
              \"[%d] %U (src) %U (dst) fibs: encap %d, decap %d\",
              t - ngm->tunnels,
              format_ip4_address, &t->src,
              format_ip4_address, &t->dst,
              t->encap_fib_index,
              t->decap_fib_index);

  s = format (s, \" decap next %U\\n\", format_decap_next, t->decap_next_index);
  /* FIXME: add protocol details */
  return s;
}

static u8 * format_" encap_stack "_name (u8 * s, va_list * args)
{
  u32 dev_instance = va_arg (*args, u32);
  return format (s, \"" encap_stack "_tunnel%d\", dev_instance);
}

static uword dummy_interface_tx (vlib_main_t * vm,
                                 vlib_node_runtime_t * node,
                                 vlib_frame_t * frame)
{
  clib_warning (\"you shouldn't be here, leaking buffers...\");
  return frame->n_vectors;
}

VNET_DEVICE_CLASS (" encap_stack "_device_class,static) = {
  .name = "" ENCAP_STACK "",
  .format_device_name = format_" encap_stack "_name,
  .format_tx_trace = format_" encap_stack "_encap_trace,
  .tx_function = dummy_interface_tx,
};

static uword dummy_set_rewrite (vnet_main_t * vnm,
                                u32 sw_if_index,
                                u32 l3_type,
                                void * dst_address,
                                void * rewrite,
                                uword max_rewrite_bytes)
{
  return 0;
}

u8 * format_" encap_stack "_header_with_length (u8 * s, va_list * args)
{
  " encap_stack "_header_t * h = va_arg (*args, " encap_stack "_header_t *);
  u32 max_header_bytes = va_arg (*args, u32);
  u32 header_bytes;

  header_bytes = sizeof (h[0]);
  if (max_header_bytes != 0 && header_bytes > max_header_bytes)
    return format (s, \"" encap-stack "header truncated\");

  /* FIXME: pretty-print an " encap_stack " header */

  return s;
}

VNET_HW_INTERFACE_CLASS (" encap_stack "_hw_class) = {
  .name = \"" ENCAP_STACK "\",
  .format_header = format_" encap_stack "_header_with_length,
  .set_rewrite = dummy_set_rewrite,
};

#define foreach_copy_field                      \
_(src.as_u32)                                   \
_(dst.as_u32)                                   \
_(encap_fib_index)                              \
_(decap_fib_index)                              \
_(decap_next_index)                             \
_(FIXME_ADD_ALL_COPY_FIELDS )

static int " encap_stack "_rewrite (" encap_stack "_tunnel_t * t)
{
  u8 *rw = 0;
  ip4_header_t * ip0;
  " encap_stack "_header_t * h0;
  int len;

  len = sizeof (*h0);

  vec_validate_aligned (rw, len-1, CLIB_CACHE_LINE_BYTES);

  h0 = (ip4_udp_" encap_stack "_header_t *) rw;

  /* FIXME: build the actual header here... */

  /* Fixed portion of the (outer) ip4 header */
  ip0 = &h0->ip4;
  ip0->ip_version_and_header_length = 0x45;
  ip0->ttl = 254;
  ip0->protocol = IP_PROTOCOL_UDP;

  /* we'll fix up the ip4 header length and checksum after-the-fact */
  ip0->src_address.as_u32 = t->src.as_u32;
  ip0->dst_address.as_u32 = t->dst.as_u32;
  ip0->checksum = ip4_header_checksum (ip0);

  /* UDP header, randomize src port on something, maybe? */
  h0->udp.src_port = clib_host_to_net_u16 (4341);
  h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_" encap_stack ");

  /* $$$ build a <mumble> tunnel header here */
  
  t->rewrite = rw;
  return (0);
}

int vnet_" encap_stack "_add_del_tunnel 
(vnet_" encap_stack "_add_del_tunnel_args_t *a, u32 * hw_if_indexp)
{
  " encap_stack "_main_t * ngm = &" encap_stack "_main;
  " encap_stack "_tunnel_t *t = 0;
  vnet_main_t * vnm = ngm->vnet_main;
  vnet_hw_interface_t * hi;
  uword * p;
  u32 hw_if_index = ~0;
  int rv;
  " encap_stack "_tunnel_key_t key, *key_copy;
  hash_pair_t *hp;
  
  key.FIXME = clib_host_to_net_XXX(FIXME);

  p = hash_get_mem (ngm->" encap_stack "_tunnel_by_key, &key);
  
  if (a->is_add)
    {
      /* adding a tunnel: tunnel must not already exist */
      if (p) 
        return VNET_API_ERROR_INVALID_VALUE;
      
      pool_get_aligned (ngm->tunnels, t, CLIB_CACHE_LINE_BYTES);
      memset (t, 0, sizeof (*t));
      
      /* copy from arg structure */
#define _(x) t->x = a->x;
      foreach_copy_field;
#undef _
      
      rv = " encap_stack "_rewrite (t);

      if (rv)
        {
          pool_put (ngm->tunnels, t);
          return rv;
        }

      /* $$$$ use a simple hash if you can ... */
      key_copy = clib_mem_alloc (sizeof (*key_copy));
      clib_memcpy (key_copy, &key, sizeof (*key_copy));

      hash_set_mem (ngm->" encap_stack "_tunnel_by_key, key_copy, 
                    t - ngm->tunnels);
      
      /* 
       * interface freelist / recycle shtik
       * This simple implementation rapidly reuses freed tunnel interfaces.
       * Consider whether to refcount, etc. etc.
       */ 
      if (vec_len (ngm->free_" encap_stack "_tunnel_hw_if_indices) > 0)
        {
          hw_if_index = ngm->free_" encap_stack "_tunnel_hw_if_indices
            [vec_len (ngm->free_" encap_stack "_tunnel_hw_if_indices)-1];
          _vec_len (ngm->free_" encap_stack "_tunnel_hw_if_indices) -= 1;
          
          hi = vnet_get_hw_interface (vnm, hw_if_index);
          hi->dev_instance = t - ngm->tunnels;
          hi->hw_instance = hi->dev_instance;
        }
      else 
        {
          hw_if_index = vnet_register_interface
            (vnm, " encap_stack "_device_class.index, t - ngm->tunnels,
             " encap_stack "_hw_class.index, t - ngm->tunnels);
          hi = vnet_get_hw_interface (vnm, hw_if_index);
          hi->output_node_index = " encap_stack "_encap_node.index;
        }
      
      t->hw_if_index = hw_if_index;
      
      vnet_sw_interface_set_flags (vnm, hi->sw_if_index, 
                                   VNET_SW_INTERFACE_FLAG_ADMIN_UP);
    }
  else
    {
      /* deleting a tunnel: tunnel must exist */
      if (!p) 
        return VNET_API_ERROR_NO_SUCH_ENTRY;

      t = pool_elt_at_index (ngm->tunnels, p[0]);

      vnet_sw_interface_set_flags (vnm, t->hw_if_index, 0 /* down */);
      vec_add1 (ngm->free_" encap_stack "_tunnel_hw_if_indices, t->hw_if_index);

      hp = hash_get_pair (ngm->" encap_stack "_tunnel_by_key, &key);
      key_copy = (void *)(hp->key);
      hash_unset_mem (ngm->" encap_stack "_tunnel_by_key, &key);
      clib_mem_free (key_copy);

      vec_free (t->rewrite);
      pool_put (ngm->tunnels, t);
    }

  if (hw_if_indexp)
      *hw_if_indexp = hw_if_index;

  return 0;
}

static u32 fib_index_from_fib_id (u32 fib_id)
{
  ip4_main_t * im = &ip4_main;
  uword * p;

  p = hash_get (im->fib_index_by_table_id, fib_id);
  if (!p)
    return ~0;

  return p[0];
}

static uword unformat_decap_next (unformat_input_t * input, va_list * args)
{
  u32 * result = va_arg (*args, u32 *);
  u32 tmp;
  
  if (unformat (input, \"drop\"))
    *result = " ENCAP_STACK "_INPUT_NEXT_DROP;
  else if (unformat (input, \"ip4\"))
    *result = " ENCAP_STACK "_INPUT_NEXT_IP4_INPUT;
  else if (unformat (input, \"ip6\"))
    *result = " ENCAP_STACK "_INPUT_NEXT_IP6_INPUT;
  else if (unformat (input, \"ethernet\"))
    *result = " ENCAP_STACK "_INPUT_NEXT_IP6_INPUT;
  else if (unformat (input, \"" encap-stack "\"))
    *result = " ENCAP_STACK "_INPUT_NEXT_" ENCAP_STACK "_ENCAP;
  else if (unformat (input, \"%d\", &tmp))
    *result = tmp;
  else
    return 0;
  return 1;
}

static clib_error_t *
" encap_stack "_add_del_tunnel_command_fn (vlib_main_t * vm,
                                   unformat_input_t * input,
                                   vlib_cli_command_t * cmd)
{
  unformat_input_t _line_input, * line_input = &_line_input;
  clib_error_t *error = 0;
  ip4_address_t src, dst;
  u8 is_add = 1;
  u8 src_set = 0;
  u8 dst_set = 0;
  u32 encap_fib_index = 0;
  u32 decap_fib_index = 0;
  u8 next_protocol = " ENCAP_STACK "_NEXT_PROTOCOL_IP4;
  u32 decap_next_index = " ENCAP_STACK "_INPUT_NEXT_IP4_INPUT;
  u8 flags = " ENCAP_STACK "_FLAGS_P;
  u8 ver_res = 0;
  u8 res = 0;
  u32 iid = 0;
  u8 iid_set = 0;
  u32 tmp;
  int rv;
  vnet_" encap_stack "_add_del_tunnel_args_t _a, * a = &_a;
  
  /* Get a line of input. */
  if (! unformat_user (input, unformat_line_input, line_input))
    return 0;

  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
    if (unformat (line_input, \"del\"))
      is_add = 0;
    else if (unformat (line_input, \"src %U\", 
                       unformat_ip4_address, &src))
      src_set = 1;
    else if (unformat (line_input, \"dst %U\",
                       unformat_ip4_address, &dst))
      dst_set = 1;
    else if (unformat (line_input, \"encap-vrf-id %d\", &tmp))
      {
        encap_fib_index = fib_index_from_fib_id (tmp);
        if (encap_fib_index == ~0)
          {
            unformat_free (line_input);
            return clib_error_return (0, \"nonexistent encap fib id %d\", tmp);
          }
      }
    else if (unformat (line_input, \"decap-vrf-id %d\", &tmp))
      {
        decap_fib_index = fib_index_from_fib_id (tmp);
        if (decap_fib_index == ~0)
          {
            unformat_free (line_input);
            return clib_error_return (0, \"nonexistent decap fib id %d\", tmp);
          }
      }
    else if (unformat (line_input, \"decap-next %U\", unformat_decap_next, 
                       &decap_next_index))
      ;
    else if (unformat(line_input, \"next-ip4\"))
      next_protocol = 1;
    else if (unformat(line_input, \"next-ip6\"))
      next_protocol = 2;
    else if (unformat(line_input, \"next-ethernet\"))
      next_protocol = 3;
    else if (unformat(line_input, \"next-nsh\"))
      next_protocol = 4;
    /* 
     * $$$ allow the user to specify anything they want 
     * in the " ENCAP_STACK " header
     */
    else 
      {
        error = clib_error_return (0, \"parse error: '%U'\",
                                   format_unformat_error, line_input);
        unformat_free (line_input);
        return error;
      }
  }

  unformat_free (line_input);

  if (src_set == 0)
    return clib_error_return (0, \"tunnel src address not specified\");

  if (dst_set == 0)
    return clib_error_return (0, \"tunnel dst address not specified\");

  memset (a, 0, sizeof (*a));

  a->is_add = is_add;

#define _(x) a->x = x;
  foreach_copy_field;
#undef _
  
  rv = vnet_" encap_stack "_add_del_tunnel (a, 0 /* hw_if_indexp */);

  switch(rv)
    {
    case 0:
      break;
    case VNET_API_ERROR_INVALID_VALUE:
      return clib_error_return (0, \"tunnel already exists...\");

    case VNET_API_ERROR_NO_SUCH_ENTRY:
      return clib_error_return (0, \"tunnel does not exist...\");

    default:
      return clib_error_return 
        (0, \"vnet_" encap_stack "_add_del_tunnel returned %d\", rv);
    }

  return 0;
}

VLIB_CLI_COMMAND (create_" encap_stack "_tunnel_command, static) = {
  .path = \"lisp gpe tunnel\",
  .short_help = 
  \"<mumble> tunnel src <ip4-addr> dst <ip4-addr>\\n\"
  \"    [encap-fib-id <nn>] [decap-fib-id <nn>]\\n\"
  \"    [decap-next [ip4|ip6|ethernet|nsh-encap|<nn>]][del]\\n\",
  .function = " encap_stack "_add_del_tunnel_command_fn,
};

static clib_error_t *
show_" encap_stack "_tunnel_command_fn (vlib_main_t * vm,
                                unformat_input_t * input,
                                vlib_cli_command_t * cmd)
{
  " encap_stack "_main_t * ngm = &" encap_stack "_main;
  " encap_stack "_tunnel_t * t;
  
  if (pool_elts (ngm->tunnels) == 0)
    vlib_cli_output (vm, \"No lisp-gpe tunnels configured...\");

  pool_foreach (t, ngm->tunnels,
  ({
    vlib_cli_output (vm, \"%U\", format_" encap_stack "_tunnel);
  }));
  
  return 0;
}

VLIB_CLI_COMMAND (show_" encap_stack "_tunnel_command, static) = {
    .path = \"show lisp gpe tunnel\",
    .function = show_" encap_stack "_tunnel_command_fn,
};

clib_error_t *" encap_stack "_init (vlib_main_t *vm)
{
  " encap_stack "_main_t *ngm = &" encap_stack "_main;
  
  ngm->vnet_main = vnet_get_main();
  ngm->vlib_main = vm;
  
  ngm->" encap_stack "_tunnel_by_key 
    = hash_create_mem (0, sizeof(" encap_stack "_tunnel_key_t), sizeof (uword));

  /* YMMV, register with the local netstack */
  udp_register_dst_port (vm, UDP_DST_PORT_" encap_stack ", 
                         " encap_stack "_input_node.index, 1 /* is_ip4 */);
  return 0;
}

VLIB_INIT_FUNCTION(" encap_stack "_init);

")