ip: ip_address_t uses ip46_address_t

Type: improvement

type re-use.

Signed-off-by: Neale Ranns <nranns@cisco.com>
Change-Id: Ic2a2e6babf9ae66a1e53aec53a6cd157e1893dc8
diff --git a/src/vnet/api_errno.h b/src/vnet/api_errno.h
index 9e261d7..eb25f05 100644
--- a/src/vnet/api_errno.h
+++ b/src/vnet/api_errno.h
@@ -80,7 +80,7 @@
 _(NOT_RUNNING_AS_ROOT, -85, "Not running as root") \
 _(ALREADY_CONNECTED, -86, "Connection to the data plane already exists") \
 _(UNSUPPORTED_JNI_VERSION, -87, "Unsupported JNI version") \
-_(FAILED_TO_ATTACH_TO_JAVA_THREAD, -88, "Failed to attach to Java thread") \
+_(IP_PREFIX_INVALID, -88, "IP prefix invalid (masked bits set in address") \
 _(INVALID_WORKER, -89, "Invalid worker thread")                         \
 _(LISP_DISABLED, -90, "LISP is disabled")                               \
 _(CLASSIFY_TABLE_NOT_FOUND, -91, "Classify table not found")            \
diff --git a/src/vnet/ip/format.c b/src/vnet/ip/format.c
index be1c4fd..8469d07 100644
--- a/src/vnet/ip/format.c
+++ b/src/vnet/ip/format.c
@@ -43,7 +43,7 @@
 u8 *
 format_ip_protocol (u8 * s, va_list * args)
 {
-  ip_protocol_t protocol = va_arg (*args, ip_protocol_t);
+  ip_protocol_t protocol = va_arg (*args, int);	// int promo of ip_protocol_t);
   ip_main_t *im = &ip_main;
   ip_protocol_info_t *pi = ip_get_protocol_info (im, protocol);
 
diff --git a/src/vnet/ip/ip.c b/src/vnet/ip/ip.c
index 88eff4f..8959b4c 100644
--- a/src/vnet/ip/ip.c
+++ b/src/vnet/ip/ip.c
@@ -163,126 +163,10 @@
   return 0;
 }
 
-void
-ip4_address_normalize (ip4_address_t * ip4, u8 preflen)
-{
-  ASSERT (preflen <= 32);
-  if (preflen == 0)
-    ip4->data_u32 = 0;
-  else
-    ip4->data_u32 &= clib_net_to_host_u32 (0xffffffff << (32 - preflen));
-}
-
-void
-ip6_address_normalize (ip6_address_t * ip6, u8 preflen)
-{
-  ASSERT (preflen <= 128);
-  if (preflen == 0)
-    {
-      ip6->as_u64[0] = 0;
-      ip6->as_u64[1] = 0;
-    }
-  else if (preflen <= 64)
-    {
-      ip6->as_u64[0] &=
-	clib_host_to_net_u64 (0xffffffffffffffffL << (64 - preflen));
-      ip6->as_u64[1] = 0;
-    }
-  else
-    ip6->as_u64[1] &=
-      clib_host_to_net_u64 (0xffffffffffffffffL << (128 - preflen));
-}
-
-void
-ip4_preflen_to_mask (u8 pref_len, ip4_address_t * ip)
-{
-  if (pref_len == 0)
-    ip->as_u32 = 0;
-  else
-    ip->as_u32 = clib_host_to_net_u32 (~((1 << (32 - pref_len)) - 1));
-}
-
-u32
-ip4_mask_to_preflen (ip4_address_t * mask)
-{
-  if (mask->as_u32 == 0)
-    return 0;
-  return (32 - log2_first_set (clib_net_to_host_u32 (mask->as_u32)));
-}
-
-void
-ip4_prefix_max_address_host_order (ip4_address_t * ip, u8 plen,
-				   ip4_address_t * res)
-{
-  u32 not_mask;
-  not_mask = (1 << (32 - plen)) - 1;
-  res->as_u32 = clib_net_to_host_u32 (ip->as_u32) + not_mask;
-}
-
-void
-ip6_preflen_to_mask (u8 pref_len, ip6_address_t * mask)
-{
-  if (pref_len == 0)
-    {
-      mask->as_u64[0] = 0;
-      mask->as_u64[1] = 0;
-    }
-  else if (pref_len <= 64)
-    {
-      mask->as_u64[0] =
-	clib_host_to_net_u64 (0xffffffffffffffffL << (64 - pref_len));
-      mask->as_u64[1] = 0;
-    }
-  else
-    {
-      mask->as_u64[0] = 0xffffffffffffffffL;
-      mask->as_u64[1] =
-	clib_host_to_net_u64 (0xffffffffffffffffL << (128 - pref_len));
-    }
-}
-
-void
-ip6_prefix_max_address_host_order (ip6_address_t * ip, u8 plen,
-				   ip6_address_t * res)
-{
-  u64 not_mask;
-  if (plen == 0)
-    {
-      res->as_u64[0] = 0xffffffffffffffffL;
-      res->as_u64[1] = 0xffffffffffffffffL;
-    }
-  else if (plen <= 64)
-    {
-      not_mask = ((u64) 1 << (64 - plen)) - 1;
-      res->as_u64[0] = clib_net_to_host_u64 (ip->as_u64[0]) + not_mask;
-      res->as_u64[1] = 0xffffffffffffffffL;
-    }
-  else
-    {
-      not_mask = ((u64) 1 << (128 - plen)) - 1;
-      res->as_u64[1] = clib_net_to_host_u64 (ip->as_u64[1]) + not_mask;
-    }
-}
-
-u32
-ip6_mask_to_preflen (ip6_address_t * mask)
-{
-  u8 first1, first0;
-  if (mask->as_u64[0] == 0 && mask->as_u64[1] == 0)
-    return 0;
-  first1 = log2_first_set (clib_net_to_host_u64 (mask->as_u64[1]));
-  first0 = log2_first_set (clib_net_to_host_u64 (mask->as_u64[0]));
-
-  if (first1 != 0)
-    return 128 - first1;
-  else
-    return 64 - first0;
-}
-
 u8 *
 format_ip_address_family (u8 * s, va_list * args)
 {
-  ip_address_family_t af = va_arg (*args, ip_address_family_t);
+  ip_address_family_t af = va_arg (*args, int);	// int promo ip_address_family_t);
 
   switch (af)
     {
@@ -295,6 +179,26 @@
   return (format (s, "unknown"));
 }
 
+uword
+unformat_ip_address_family (unformat_input_t * input, va_list * args)
+{
+  ip_address_family_t *af = va_arg (*args, ip_address_family_t *);
+
+  if (unformat (input, "ip4") || unformat (input, "ipv4") ||
+      unformat (input, "IP4") || unformat (input, "IPv4"))
+    {
+      *af = AF_IP4;
+      return (1);
+    }
+  else if (unformat (input, "ip6") || unformat (input, "ipv6") ||
+	   unformat (input, "IP6") || unformat (input, "IPv6"))
+    {
+      *af = AF_IP6;
+      return (1);
+    }
+  return (0);
+}
+
 u8 *
 format_ip_dscp (u8 * s, va_list * va)
 {
diff --git a/src/vnet/ip/ip.h b/src/vnet/ip/ip.h
index 75750c5..a6fcd41 100644
--- a/src/vnet/ip/ip.h
+++ b/src/vnet/ip/ip.h
@@ -278,16 +278,6 @@
 void ip_copy (ip46_address_t * dst, ip46_address_t * src, u8 is_ip4);
 void ip_set (ip46_address_t * dst, void *src, u8 is_ip4);
 void *ip_interface_get_first_ip (u32 sw_if_index, u8 is_ip4);
-void ip4_address_normalize (ip4_address_t * ip4, u8 preflen);
-void ip6_address_normalize (ip6_address_t * ip6, u8 preflen);
-void ip4_preflen_to_mask (u8 pref_len, ip4_address_t * ip);
-u32 ip4_mask_to_preflen (ip4_address_t * mask);
-void ip4_prefix_max_address_host_order (ip4_address_t * ip, u8 plen,
-					ip4_address_t * res);
-void ip6_prefix_max_address_host_order (ip6_address_t * ip, u8 plen,
-					ip6_address_t * res);
-void ip6_preflen_to_mask (u8 pref_len, ip6_address_t * mask);
-u32 ip6_mask_to_preflen (ip6_address_t * mask);
 
 always_inline u32 vlib_buffer_get_ip4_fib_index (vlib_buffer_t * b);
 always_inline u32 vlib_buffer_get_ip6_fib_index (vlib_buffer_t * b);
diff --git a/src/vnet/ip/ip6_packet.h b/src/vnet/ip/ip6_packet.h
index e823214..7bbdd19 100644
--- a/src/vnet/ip/ip6_packet.h
+++ b/src/vnet/ip/ip6_packet.h
@@ -49,9 +49,12 @@
   u16 as_u16[8];
   u32 as_u32[4];
   u64 as_u64[2];
+  u64x2 as_u128;
   uword as_uword[16 / sizeof (uword)];
 }
-ip6_address_t;
+__clib_packed ip6_address_t;
+
+STATIC_ASSERT_SIZEOF (ip6_address_t, 16);
 
 typedef struct
 {
diff --git a/src/vnet/ip/ip_packet.h b/src/vnet/ip/ip_packet.h
index 9a55d5f..e67cec1 100644
--- a/src/vnet/ip/ip_packet.h
+++ b/src/vnet/ip/ip_packet.h
@@ -48,7 +48,7 @@
 #define ip_protocol(n,s) IP_PROTOCOL_##s = n,
 #include "protocols.def"
 #undef ip_protocol
-} ip_protocol_t;
+} __clib_packed ip_protocol_t;
 
 /* TCP/UDP ports. */
 typedef enum
diff --git a/src/vnet/ip/ip_types.api b/src/vnet/ip/ip_types.api
index 4929458..3e6149a 100644
--- a/src/vnet/ip/ip_types.api
+++ b/src/vnet/ip/ip_types.api
@@ -70,6 +70,7 @@
   IP_API_PROTO_GRE = 47,
   IP_API_PROTO_AH = 50,
   IP_API_PROTO_ESP = 51,
+  IP_API_PROTO_ICMP6 = 58,
   IP_API_PROTO_EIGRP = 88,
   IP_API_PROTO_OSPF = 89,
   IP_API_PROTO_SCTP = 132,
diff --git a/src/vnet/ip/ip_types.c b/src/vnet/ip/ip_types.c
index 9659ca5..cd08c54 100644
--- a/src/vnet/ip/ip_types.c
+++ b/src/vnet/ip/ip_types.c
@@ -15,6 +15,7 @@
 
 #include <vnet/ip/ip_types.h>
 #include <vnet/ip/format.h>
+#include <vnet/ip/ip.h>
 
 u8 *
 format_ip_address (u8 * s, va_list * args)
@@ -115,8 +116,7 @@
   int res = 0;
   if (ip_addr_version (ip1) != ip_addr_version (ip2))
     return -1;
-  res =
-    memcmp (&ip_addr_addr (ip1), &ip_addr_addr (ip2), ip_address_size (ip1));
+  res = ip46_address_cmp (&ip_addr_46 (ip1), &ip_addr_46 (ip2));
 
   if (res < 0)
     res = 2;
@@ -133,7 +133,7 @@
     {
       /* don't copy any garbage from the union */
       clib_memset (dst, 0, sizeof (*dst));
-      dst->ip.v4 = src->ip.v4;
+      ip_addr_v4 (dst) = ip_addr_v4 (src);
       dst->version = AF_IP4;
     }
   else
@@ -142,16 +142,39 @@
     }
 }
 
+u8 *
+ip_addr_bytes (ip_address_t * ip)
+{
+  switch (ip->version)
+    {
+    case AF_IP4:
+      return (u8 *) & ip_addr_v4 (ip);
+    case AF_IP6:
+      return (u8 *) & ip_addr_v6 (ip);
+      break;
+    }
+  ASSERT (0);
+  return (NULL);
+}
+
 void
 ip_address_copy_addr (void *dst, const ip_address_t * src)
 {
-  clib_memcpy (dst, src, ip_address_size (src));
+  switch (src->version)
+    {
+    case AF_IP4:
+      clib_memcpy (dst, &ip_addr_v4 (src), ip_address_size (src));
+      break;
+    case AF_IP6:
+      clib_memcpy (dst, &ip_addr_v6 (src), ip_address_size (src));
+      break;
+    }
 }
 
 u16
-ip_version_to_size (u8 ver)
+ip_version_to_size (ip_address_family_t af)
 {
-  switch (ver)
+  switch (af)
     {
     case AF_IP4:
       return sizeof (ip4_address_t);
@@ -163,49 +186,79 @@
   return 0;
 }
 
+vnet_link_t
+ip_address_family_to_link_type (ip_address_family_t af)
+{
+  switch (af)
+    {
+    case AF_IP4:
+      return (VNET_LINK_IP4);
+    case AF_IP6:
+      return (VNET_LINK_IP6);
+    }
+  ASSERT (0);
+  return (VNET_LINK_IP4);
+}
+
+
 void
 ip_address_set (ip_address_t * dst, const void *src, u8 version)
 {
-  clib_memcpy (dst, src, ip_version_to_size (version));
   ip_addr_version (dst) = version;
+
+  switch (version)
+    {
+    case AF_IP4:
+      ip_addr_v4 (dst) = *(ip4_address_t *) src;
+      break;
+    case AF_IP6:
+      ip_addr_v6 (dst) = *(ip6_address_t *) src;
+      break;
+    }
+}
+
+fib_protocol_t
+ip_address_family_to_fib_proto (ip_address_family_t af)
+{
+  switch (af)
+    {
+    case AF_IP4:
+      return (FIB_PROTOCOL_IP4);
+    case AF_IP6:
+      return (FIB_PROTOCOL_IP6);
+    }
+  ASSERT (0);
+  return (FIB_PROTOCOL_IP4);
+}
+
+ip_address_family_t
+ip_address_family_from_fib_proto (fib_protocol_t fp)
+{
+  switch (fp)
+    {
+    case FIB_PROTOCOL_IP4:
+      return (AF_IP4);
+    case FIB_PROTOCOL_IP6:
+      return (AF_IP6);
+    case FIB_PROTOCOL_MPLS:
+      ASSERT (0);
+    }
+  return (AF_IP4);
 }
 
 fib_protocol_t
 ip_address_to_46 (const ip_address_t * addr, ip46_address_t * a)
 {
-  fib_protocol_t proto = FIB_PROTOCOL_IP4;
-
-  switch (ip_addr_version (addr))
-    {
-    case AF_IP4:
-      ip46_address_set_ip4 (a, &addr->ip.v4);
-      break;
-    case AF_IP6:
-      proto = FIB_PROTOCOL_IP6;
-      a->ip6 = addr->ip.v6;
-      break;
-    }
-
-  return (proto);
+  *a = ip_addr_46 (addr);
+  return (ip_address_family_to_fib_proto (ip_addr_version (addr)));
 }
 
 void
 ip_address_from_46 (const ip46_address_t * nh,
 		    fib_protocol_t fproto, ip_address_t * ip)
 {
-  switch (fproto)
-    {
-    case FIB_PROTOCOL_IP4:
-      clib_memset (ip, 0, sizeof (*ip));
-      ip_address_set (ip, &nh->ip4, AF_IP4);
-      break;
-    case FIB_PROTOCOL_IP6:
-      ip_address_set (ip, &nh->ip6, AF_IP6);
-      break;
-    default:
-      ASSERT (0);
-      break;
-    }
+  ip_addr_46 (ip) = *nh;
+  ip_addr_version (ip) = ip_address_family_from_fib_proto (fproto);
 }
 
 static void
@@ -311,6 +364,165 @@
   return cmp;
 }
 
+static bool
+ip4_prefix_validate (const ip_prefix_t * ip)
+{
+  ip4_address_t ip4_addr, ip4_mask;
+
+  if (ip_prefix_len (ip) > 32)
+    return (false);
+
+  ip4_addr = ip_prefix_v4 (ip);
+  ip4_preflen_to_mask (ip_prefix_len (ip), &ip4_mask);
+
+  return ((ip4_addr.as_u32 & ip4_mask.as_u32) == ip4_addr.as_u32);
+}
+
+static bool
+ip6_prefix_validate (const ip_prefix_t * ip)
+{
+  ip6_address_t ip6_addr, ip6_mask;
+
+  if (ip_prefix_len (ip) > 128)
+    return (false);
+
+  ip6_addr = ip_prefix_v6 (ip);
+  ip6_preflen_to_mask (ip_prefix_len (ip), &ip6_mask);
+
+  return (((ip6_addr.as_u64[0] & ip6_mask.as_u64[0]) == ip6_addr.as_u64[0]) &&
+	  ((ip6_addr.as_u64[1] & ip6_mask.as_u64[1]) == ip6_addr.as_u64[1]));
+}
+
+bool
+ip_prefix_validate (const ip_prefix_t * ip)
+{
+  switch (ip_prefix_version (ip))
+    {
+    case AF_IP4:
+      return (ip4_prefix_validate (ip));
+    case AF_IP6:
+      return (ip6_prefix_validate (ip));
+    }
+  ASSERT (0);
+  return (false);
+}
+
+void
+ip4_address_normalize (ip4_address_t * ip4, u8 preflen)
+{
+  ASSERT (preflen <= 32);
+  if (preflen == 0)
+    ip4->data_u32 = 0;
+  else
+    ip4->data_u32 &= clib_net_to_host_u32 (0xffffffff << (32 - preflen));
+}
+
+void
+ip6_address_normalize (ip6_address_t * ip6, u8 preflen)
+{
+  ASSERT (preflen <= 128);
+  if (preflen == 0)
+    {
+      ip6->as_u64[0] = 0;
+      ip6->as_u64[1] = 0;
+    }
+  else if (preflen <= 64)
+    {
+      ip6->as_u64[0] &=
+	clib_host_to_net_u64 (0xffffffffffffffffL << (64 - preflen));
+      ip6->as_u64[1] = 0;
+    }
+  else
+    ip6->as_u64[1] &=
+      clib_host_to_net_u64 (0xffffffffffffffffL << (128 - preflen));
+}
+
+void
+ip4_preflen_to_mask (u8 pref_len, ip4_address_t * ip)
+{
+  if (pref_len == 0)
+    ip->as_u32 = 0;
+  else
+    ip->as_u32 = clib_host_to_net_u32 (~((1 << (32 - pref_len)) - 1));
+}
+
+u32
+ip4_mask_to_preflen (ip4_address_t * mask)
+{
+  if (mask->as_u32 == 0)
+    return 0;
+  return (32 - log2_first_set (clib_net_to_host_u32 (mask->as_u32)));
+}
+
+void
+ip4_prefix_max_address_host_order (ip4_address_t * ip, u8 plen,
+				   ip4_address_t * res)
+{
+  u32 not_mask;
+  not_mask = (1 << (32 - plen)) - 1;
+  res->as_u32 = clib_net_to_host_u32 (ip->as_u32) + not_mask;
+}
+
+void
+ip6_preflen_to_mask (u8 pref_len, ip6_address_t * mask)
+{
+  if (pref_len == 0)
+    {
+      mask->as_u64[0] = 0;
+      mask->as_u64[1] = 0;
+    }
+  else if (pref_len <= 64)
+    {
+      mask->as_u64[0] =
+	clib_host_to_net_u64 (0xffffffffffffffffL << (64 - pref_len));
+      mask->as_u64[1] = 0;
+    }
+  else
+    {
+      mask->as_u64[0] = 0xffffffffffffffffL;
+      mask->as_u64[1] =
+	clib_host_to_net_u64 (0xffffffffffffffffL << (128 - pref_len));
+    }
+}
+
+void
+ip6_prefix_max_address_host_order (ip6_address_t * ip, u8 plen,
+				   ip6_address_t * res)
+{
+  u64 not_mask;
+  if (plen == 0)
+    {
+      res->as_u64[0] = 0xffffffffffffffffL;
+      res->as_u64[1] = 0xffffffffffffffffL;
+    }
+  else if (plen <= 64)
+    {
+      not_mask = ((u64) 1 << (64 - plen)) - 1;
+      res->as_u64[0] = clib_net_to_host_u64 (ip->as_u64[0]) + not_mask;
+      res->as_u64[1] = 0xffffffffffffffffL;
+    }
+  else
+    {
+      not_mask = ((u64) 1 << (128 - plen)) - 1;
+      res->as_u64[1] = clib_net_to_host_u64 (ip->as_u64[1]) + not_mask;
+    }
+}
+
+u32
+ip6_mask_to_preflen (ip6_address_t * mask)
+{
+  u8 first1, first0;
+  if (mask->as_u64[0] == 0 && mask->as_u64[1] == 0)
+    return 0;
+  first1 = log2_first_set (clib_net_to_host_u64 (mask->as_u64[1]));
+  first0 = log2_first_set (clib_net_to_host_u64 (mask->as_u64[0]));
+
+  if (first1 != 0)
+    return 128 - first1;
+  else
+    return 64 - first0;
+}
+
 /*
  * fd.io coding-style-patch-verification: ON
  *
diff --git a/src/vnet/ip/ip_types.h b/src/vnet/ip/ip_types.h
index 720ef1a..907d4f0 100644
--- a/src/vnet/ip/ip_types.h
+++ b/src/vnet/ip/ip_types.h
@@ -22,7 +22,9 @@
 {
   AF_IP4,
   AF_IP6,
-} ip_address_family_t;
+} __clib_packed ip_address_family_t;
+
+#define N_AF (AF_IP6+1)
 
 #define N_AF (AF_IP6+1)
 
@@ -33,29 +35,32 @@
 #define FOR_EACH_IP_ADDRESS_FAMILY(_af) \
   for (_af = AF_IP4; _af <= AF_IP6; _af++)
 
+extern vnet_link_t ip_address_family_to_link_type (ip_address_family_t af);
+extern fib_protocol_t ip_address_family_to_fib_proto (ip_address_family_t af);
+extern ip_address_family_t ip_address_family_from_fib_proto (fib_protocol_t
+							     fp);
+
 #define u8_ptr_add(ptr, index) (((u8 *)ptr) + index)
 #define u16_net_add(u, val) clib_host_to_net_u16(clib_net_to_host_u16(u) + (val))
 
 /* *INDENT-OFF* */
 typedef struct ip_address
 {
-  union
-  {
-    ip4_address_t v4;
-    ip6_address_t v6;
-  } ip;
+  ip46_address_t ip;
   ip_address_family_t version;
 } __clib_packed ip_address_t;
 /* *INDENT-ON* */
 
-#define IP_ADDRESS_V4_ALL_0S {.ip.v4.as_u32 = 0, .version = AF_IP4}
-#define IP_ADDRESS_V6_ALL_0S {.ip.v4.as_u32 = 0, .version = AF_IP6}
+#define IP_ADDRESS_V4_ALL_0S {.ip.ip4.as_u32 = 0, .version = AF_IP4}
+#define IP_ADDRESS_V6_ALL_0S {.ip.ip6.as_u64 = {0, 0}, .version = AF_IP6}
 
-#define ip_addr_addr(_a) (_a)->ip
-#define ip_addr_v4(_a) (_a)->ip.v4
-#define ip_addr_v6(_a) (_a)->ip.v6
+#define ip_addr_46(_a) (_a)->ip
+#define ip_addr_v4(_a) (_a)->ip.ip4
+#define ip_addr_v6(_a) (_a)->ip.ip6
 #define ip_addr_version(_a) (_a)->version
 
+extern u8 *ip_addr_bytes (ip_address_t * ip);
+
 extern bool ip_address_is_zero (const ip_address_t * ip);
 extern int ip_address_cmp (const ip_address_t * ip1,
 			   const ip_address_t * ip2);
@@ -63,7 +68,7 @@
 extern void ip_address_copy_addr (void *dst, const ip_address_t * src);
 extern void ip_address_set (ip_address_t * dst, const void *src, u8 version);
 extern u16 ip_address_size (const ip_address_t * a);
-extern u16 ip_version_to_size (u8 ver);
+extern u16 ip_version_to_size (ip_address_family_t af);
 extern u8 *format_ip_address (u8 * s, va_list * args);
 extern uword unformat_ip_address (unformat_input_t * input, va_list * args);
 extern fib_protocol_t ip_address_to_46 (const ip_address_t * addr,
@@ -95,6 +100,18 @@
 extern u8 *format_ip_prefix (u8 * s, va_list * args);
 extern uword unformat_ip_prefix (unformat_input_t * input, va_list * args);
 
+extern bool ip_prefix_validate (const ip_prefix_t * ip);
+extern void ip4_address_normalize (ip4_address_t * ip4, u8 preflen);
+extern void ip6_address_normalize (ip6_address_t * ip6, u8 preflen);
+extern void ip4_preflen_to_mask (u8 pref_len, ip4_address_t * ip);
+extern u32 ip4_mask_to_preflen (ip4_address_t * mask);
+extern void ip4_prefix_max_address_host_order (ip4_address_t * ip, u8 plen,
+					       ip4_address_t * res);
+extern void ip6_prefix_max_address_host_order (ip6_address_t * ip, u8 plen,
+					       ip6_address_t * res);
+extern void ip6_preflen_to_mask (u8 pref_len, ip6_address_t * mask);
+extern u32 ip6_mask_to_preflen (ip6_address_t * mask);
+
 #endif /* __IP_TYPES_H__ */
 
 /*
diff --git a/src/vnet/ip/ip_types_api.c b/src/vnet/ip/ip_types_api.c
index d201d38..bcf594c 100644
--- a/src/vnet/ip/ip_types_api.c
+++ b/src/vnet/ip/ip_types_api.c
@@ -166,19 +166,15 @@
 void
 ip_address_decode2 (const vl_api_address_t * in, ip_address_t * out)
 {
-  switch (clib_net_to_host_u32 (in->af))
+  switch (ip_address_union_decode (&in->un, in->af, &out->ip))
     {
-    case ADDRESS_IP4:
-      clib_memset (out, 0, sizeof (*out));
-      clib_memcpy (&ip_addr_v4 (out), &in->un.ip4, sizeof (ip_addr_v4 (out)));
+    case IP46_TYPE_IP4:
       out->version = AF_IP4;
       break;
-    case ADDRESS_IP6:
-      clib_memcpy (&ip_addr_v6 (out), &in->un.ip6, sizeof (ip_addr_v6 (out)));
+    case IP46_TYPE_IP6:
       out->version = AF_IP6;
       break;
     default:
-      ASSERT (!"Unknown address family in API address type");
       break;
     }
 }
@@ -223,13 +219,12 @@
     {
     case AF_IP4:
       out->af = clib_net_to_host_u32 (ADDRESS_IP4);
-      ip4_address_encode (&in->ip.v4, out->un.ip4);
       break;
     case AF_IP6:
       out->af = clib_net_to_host_u32 (ADDRESS_IP6);
-      ip6_address_encode (&in->ip.v6, out->un.ip6);
       break;
     }
+  ip_address_union_encode (&in->ip, out->af, &out->un);
 }
 
 void
@@ -249,6 +244,17 @@
   ip_address_decode (&in->address, &out->fp_addr);
 }
 
+int
+ip_prefix_decode2 (const vl_api_prefix_t * in, ip_prefix_t * out)
+{
+  out->len = in->len;
+  ip_address_decode2 (&in->address, &out->addr);
+
+  if (!ip_prefix_validate (out))
+    return (VNET_API_ERROR_IP_PREFIX_INVALID);
+  return (0);
+}
+
 void
 ip_prefix_encode (const fib_prefix_t * in, vl_api_prefix_t * out)
 {
@@ -274,6 +280,13 @@
 }
 
 void
+ip_prefix_encode2 (const ip_prefix_t * in, vl_api_prefix_t * out)
+{
+  out->len = in->len;
+  ip_address_encode2 (&in->addr, &out->address);
+}
+
+void
 ip_mprefix_encode (const mfib_prefix_t * in, vl_api_mprefix_t * out)
 {
   out->af = (FIB_PROTOCOL_IP6 == in->fp_proto ? ADDRESS_IP6 : ADDRESS_IP4);
diff --git a/src/vnet/ip/ip_types_api.h b/src/vnet/ip/ip_types_api.h
index 2035dba..600fe09 100644
--- a/src/vnet/ip/ip_types_api.h
+++ b/src/vnet/ip/ip_types_api.h
@@ -42,12 +42,13 @@
  */
 extern ip46_type_t ip_address_decode (const vl_api_address_t * in,
 				      ip46_address_t * out);
-extern void ip_address_encode (const ip46_address_t * in,
-			       ip46_type_t type, vl_api_address_t * out);
 extern void ip_address_decode2 (const vl_api_address_t * in,
 				ip_address_t * out);
+extern void ip_address_encode (const ip46_address_t * in,
+			       ip46_type_t type, vl_api_address_t * out);
 extern void ip_address_encode2 (const ip_address_t * in,
 				vl_api_address_t * out);
+
 extern void ip6_address_encode (const ip6_address_t * in,
 				vl_api_ip6_address_t out);
 extern void ip6_address_decode (const vl_api_ip6_address_t in,
@@ -59,6 +60,8 @@
 
 extern void ip_prefix_decode (const vl_api_prefix_t * in, fib_prefix_t * out);
 extern void ip_prefix_encode (const fib_prefix_t * in, vl_api_prefix_t * out);
+extern int ip_prefix_decode2 (const vl_api_prefix_t * in, ip_prefix_t * out);
+extern void ip_prefix_encode2 (const ip_prefix_t * in, vl_api_prefix_t * out);
 
 extern void ip_mprefix_decode (const vl_api_mprefix_t * in,
 			       mfib_prefix_t * out);
diff --git a/src/vnet/lisp-cp/control.c b/src/vnet/lisp-cp/control.c
index e4015c7..09f5c0a 100644
--- a/src/vnet/lisp-cp/control.c
+++ b/src/vnet/lisp-cp/control.c
@@ -134,13 +134,15 @@
       prefix->fp_len = 32;
       prefix->fp_proto = FIB_PROTOCOL_IP4;
       clib_memset (&prefix->fp_addr.pad, 0, sizeof (prefix->fp_addr.pad));
-      memcpy (&prefix->fp_addr.ip4, &addr->ip, sizeof (prefix->fp_addr.ip4));
+      memcpy (&prefix->fp_addr.ip4, &addr->ip.ip4,
+	      sizeof (prefix->fp_addr.ip4));
     }
   else
     {
       prefix->fp_len = 128;
       prefix->fp_proto = FIB_PROTOCOL_IP6;
-      memcpy (&prefix->fp_addr.ip6, &addr->ip, sizeof (prefix->fp_addr.ip6));
+      memcpy (&prefix->fp_addr.ip6, &addr->ip.ip6,
+	      sizeof (prefix->fp_addr.ip6));
     }
   prefix->___fp___pad = 0;
 }
diff --git a/src/vnet/lisp-cp/gid_dictionary.c b/src/vnet/lisp-cp/gid_dictionary.c
index c1b4ada..564ee8e 100644
--- a/src/vnet/lisp-cp/gid_dictionary.c
+++ b/src/vnet/lisp-cp/gid_dictionary.c
@@ -346,13 +346,13 @@
   kv->key[0] = ((u64) bd << 32) | (u32) ip_addr_version (addr);
   if (ip_addr_version (addr) == AF_IP4)
     {
-      kv->key[1] = (u64) addr->ip.v4.as_u32;
+      kv->key[1] = (u64) ip_addr_v4 (addr).as_u32;
       kv->key[2] = (u64) 0;
     }
   else
     {
-      kv->key[1] = (u64) addr->ip.v6.as_u64[0];
-      kv->key[2] = (u64) addr->ip.v6.as_u64[1];
+      kv->key[1] = (u64) ip_addr_v6 (addr).as_u64[0];
+      kv->key[2] = (u64) ip_addr_v6 (addr).as_u64[1];
     }
 }
 
diff --git a/src/vnet/lisp-cp/lisp_types.c b/src/vnet/lisp-cp/lisp_types.c
index 9087865..971d307 100644
--- a/src/vnet/lisp-cp/lisp_types.c
+++ b/src/vnet/lisp-cp/lisp_types.c
@@ -472,7 +472,7 @@
   u32 len = ip_address_size (a);
   *(u16 *) b = clib_host_to_net_u16 (ip_address_iana_afi (a));
   u8 *p = b + sizeof (u16);
-  clib_memcpy (p, &ip_addr_addr (a), len);
+  clib_memcpy (p, ip_addr_bytes (a), len);
   return (len + sizeof (u16));
 }
 
@@ -481,7 +481,7 @@
 {
   ip_addr_version (dst) = ip_iana_afi_to_version (iana_afi);
   u8 size = ip_version_to_size (ip_addr_version (dst));
-  clib_memcpy (&ip_addr_addr (dst), offset + sizeof (u16), size);
+  clib_memcpy (ip_addr_bytes (dst), offset + sizeof (u16), size);
   return (sizeof (u16) + size);
 }
 
diff --git a/src/vnet/lisp-gpe/lisp_gpe_adjacency.c b/src/vnet/lisp-gpe/lisp_gpe_adjacency.c
index 8efb972..cd0b338 100644
--- a/src/vnet/lisp-gpe/lisp_gpe_adjacency.c
+++ b/src/vnet/lisp-gpe/lisp_gpe_adjacency.c
@@ -46,8 +46,8 @@
 
 #define LISP_ADJ_SET_KEY(_key, _itf, _nh)       \
 {						\
-  _key.key[0] = (_nh)->ip.v6.as_u64[0];		\
-  _key.key[1] = (_nh)->ip.v6.as_u64[1];		\
+  _key.key[0] = ip_addr_v6((_nh)).as_u64[0];    \
+  _key.key[1] = ip_addr_v6((_nh)).as_u64[1];    \
   _key.key[2] = (_itf);				\
 }
 
diff --git a/src/vnet/lisp-gpe/lisp_gpe_sub_interface.h b/src/vnet/lisp-gpe/lisp_gpe_sub_interface.h
index 457db93..7375bf0 100644
--- a/src/vnet/lisp-gpe/lisp_gpe_sub_interface.h
+++ b/src/vnet/lisp-gpe/lisp_gpe_sub_interface.h
@@ -110,9 +110,9 @@
   lisp_gpe_sub_interface_key_t key;
   const uword *p;
 
-  key.local_rloc.ip.v6.as_u64[0] = addr->as_u64[0];
-  key.local_rloc.ip.v6.as_u64[1] = addr->as_u64[1];
-  key.local_rloc.version = AF_IP6;
+  ip_addr_v6 (&key.local_rloc).as_u64[0] = addr->as_u64[0];
+  ip_addr_v6 (&key.local_rloc).as_u64[1] = addr->as_u64[1];
+  ip_addr_version (&key.local_rloc) = AF_IP6;
   key.vni = vni;
 
   p = hash_get_mem (&lisp_gpe_sub_interfaces_sw_if_index, &key);
@@ -134,7 +134,7 @@
   lisp_gpe_sub_interface_key_t key;
   const uword *p;
 
-  key.local_rloc.ip.v4.as_u32 = addr->as_u32;
+  ip_addr_v4 (&key.local_rloc).as_u32 = addr->as_u32;
   key.local_rloc.version = AF_IP4;
   key.vni = vni;