ip: fix unformat_ip_address forcing version to IP4 for some IP6 addresses 02/37602/4
authorAndrew Yourtchenko <ayourtch@gmail.com>
Wed, 9 Nov 2022 01:18:56 +0000 (01:18 +0000)
committerBeno�t Ganne <bganne@cisco.com>
Wed, 9 Nov 2022 13:07:20 +0000 (13:07 +0000)
dd2f12ba made use of ip46_address_is_ip4() in order to determine whether
the address is ipv4 or ipv6 within unformat_ip_address - however, its
logic is correct only for some addresses. e.g. a valid IPv6 address of :: (unspecified)
will result in "true" result. This is probably not an issue for most
of the cases (the unspecified address is quite rare),
however if the unformat_ip_address is used as part of the
prefix parsing, the ::/0 is a fairly often utilized construct,
which gets parsed as 0.0.0.0

Solution: return the old logic, but use a temporary
variable to avoid overwriting the target memory on failure.

Type: fix
Fixes: dd2f12ba6ab952d9d66f4d9ba89ffde6309b1ff2.
Change-Id: I272f740dfdf07036cec68516e153f0701a53233d
Signed-off-by: Andrew Yourtchenko <ayourtch@gmail.com>
src/vat/ip_types.c
src/vnet/ip/ip_types.c

index cdfcbc0..abcd2ea 100644 (file)
@@ -41,16 +41,17 @@ uword
 unformat_ip_address (unformat_input_t * input, va_list * args)
 {
   ip_address_t *a = va_arg (*args, ip_address_t *);
+  ip_address_t tmp, *p_tmp = &tmp;
 
-  if (unformat_user (input, unformat_ip46_address, &ip_addr_46 (a),
-                    IP46_TYPE_ANY))
-    {
-      ip_addr_version (a) =
-       ip46_address_is_ip4 (&ip_addr_46 (a)) ? AF_IP4 : AF_IP6;
-      return 1;
-    }
-
-  return 0;
+  clib_memset (p_tmp, 0, sizeof (*p_tmp));
+  if (unformat (input, "%U", unformat_ip4_address, &ip_addr_v4 (p_tmp)))
+    ip_addr_version (p_tmp) = AF_IP4;
+  else if (unformat_user (input, unformat_ip6_address, &ip_addr_v6 (p_tmp)))
+    ip_addr_version (p_tmp) = AF_IP6;
+  else
+    return 0;
+  *a = *p_tmp;
+  return 1;
 }
 
 u8 *
index 88b3f7b..ec80a96 100644 (file)
@@ -41,16 +41,17 @@ uword
 unformat_ip_address (unformat_input_t * input, va_list * args)
 {
   ip_address_t *a = va_arg (*args, ip_address_t *);
+  ip_address_t tmp, *p_tmp = &tmp;
 
-  if (unformat_user (input, unformat_ip46_address, &ip_addr_46 (a),
-                    IP46_TYPE_ANY))
-    {
-      ip_addr_version (a) =
-       ip46_address_is_ip4 (&ip_addr_46 (a)) ? AF_IP4 : AF_IP6;
-      return 1;
-    }
-
-  return 0;
+  clib_memset (p_tmp, 0, sizeof (*p_tmp));
+  if (unformat (input, "%U", unformat_ip4_address, &ip_addr_v4 (p_tmp)))
+    ip_addr_version (p_tmp) = AF_IP4;
+  else if (unformat_user (input, unformat_ip6_address, &ip_addr_v6 (p_tmp)))
+    ip_addr_version (p_tmp) = AF_IP6;
+  else
+    return 0;
+  *a = *p_tmp;
+  return 1;
 }
 
 u8 *