SNAT: fix snat_add_static_mapping_command() uninitialized variable 34/5034/3
authorMatus Fabian <[email protected]>
Mon, 6 Feb 2017 06:14:41 +0000 (22:14 -0800)
committerDamjan Marion <[email protected]>
Mon, 6 Feb 2017 10:19:43 +0000 (10:19 +0000)
Change-Id: I7775dd3b90d5a3449650c3102e24bfedd770beb1
Signed-off-by: Matus Fabian <[email protected]>
src/plugins/snat/snat.c
src/plugins/snat/snat_test.c

index 8ad3602..ba6463c 100644 (file)
@@ -1857,6 +1857,7 @@ add_static_mapping_command_fn (vlib_main_t * vm,
   vnet_main_t * vnm = vnet_get_main();
   int rv;
   snat_protocol_t proto;
+  u8 proto_set = 0;
 
   /* Get a line of input. */
   if (!unformat_user (input, unformat_line_input, line_input))
@@ -1886,7 +1887,7 @@ add_static_mapping_command_fn (vlib_main_t * vm,
       else if (unformat (line_input, "vrf %u", &vrf_id))
         ;
       else if (unformat (line_input, "%U", unformat_snat_protocol, &proto))
-        ;
+        proto_set = 1;
       else if (unformat (line_input, "del"))
         is_add = 0;
       else
@@ -1895,6 +1896,9 @@ add_static_mapping_command_fn (vlib_main_t * vm,
     }
   unformat_free (line_input);
 
+  if (!addr_only && !proto_set)
+    return clib_error_return (0, "missing protocol");
+
   rv = snat_add_static_mapping(l_addr, e_addr, (u16) l_port, (u16) e_port,
                                vrf_id, addr_only, sw_if_index, proto, is_add);
 
@@ -1926,8 +1930,8 @@ add_static_mapping_command_fn (vlib_main_t * vm,
  * Static mapping allows hosts on the external network to initiate connection
  * to to the local network host.
  * To create static mapping between local host address 10.0.0.3 port 6303 and
- * external address 4.4.4.4 port 3606 use:
- *  vpp# snat add static mapping local 10.0.0.3 6303 external 4.4.4.4 3606
+ * external address 4.4.4.4 port 3606 for TCP protocol use:
+ *  vpp# snat add static mapping local tcp 10.0.0.3 6303 external 4.4.4.4 3606
  * If not runnig "static mapping only" S-NAT plugin mode use before:
  *  vpp# snat add address 4.4.4.4
  * To create static mapping between local and external address use:
@@ -1938,7 +1942,7 @@ VLIB_CLI_COMMAND (add_static_mapping_command, static) = {
   .path = "snat add static mapping",
   .function = add_static_mapping_command_fn,
   .short_help =
-    "snat add static mapping local <addr> [<port>] external <addr> [<port>] [vrf <table-id>] [del]",
+    "snat add static mapping local tcp|udp|icmp <addr> [<port>] external <addr> [<port>] [vrf <table-id>] [del]",
 };
 
 static clib_error_t *
index 85f9d57..a8cb8cc 100644 (file)
@@ -222,6 +222,7 @@ static int api_snat_add_static_mapping(vat_main_t * vam)
   u32 sw_if_index = ~0;
   u8 sw_if_index_set = 0;
   u32 proto = ~0;
+  u8 proto_set = 0;
   int ret;
 
   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
@@ -243,7 +244,7 @@ static int api_snat_add_static_mapping(vat_main_t * vam)
       else if (unformat (i, "vrf %u", &vrf_id))
         ;
       else if (unformat (i, "protocol %u", &proto))
-        ;
+        proto_set = 1;
       else if (unformat (i, "del"))
         is_add = 0;
       else
@@ -253,6 +254,12 @@ static int api_snat_add_static_mapping(vat_main_t * vam)
         }
     }
 
+  if (!addr_only && !proto_set)
+    {
+      errmsg ("protocol required\n");
+      return -99;
+    }
+
   if (!local_addr_set)
     {
       errmsg ("local addr required\n");