summaryrefslogtreecommitdiffstats
path: root/src/vnet/ip
diff options
context:
space:
mode:
authorNeale Ranns <nranns@cisco.com>2020-02-24 13:32:30 +0000
committerFlorin Coras <florin.coras@gmail.com>2020-03-17 14:04:02 +0000
commit3ec09e9243dceb78e7548725281b0da8eddf72b1 (patch)
treefb591d687ea796751b523bfd69cd96c127a9e75b /src/vnet/ip
parent68449850783ed6a2570a864b2d12c79ff35c4072 (diff)
ip: ip_address_t uses ip46_address_t
Type: improvement type re-use. Signed-off-by: Neale Ranns <nranns@cisco.com> Change-Id: Ic2a2e6babf9ae66a1e53aec53a6cd157e1893dc8
Diffstat (limited to 'src/vnet/ip')
-rw-r--r--src/vnet/ip/format.c2
-rw-r--r--src/vnet/ip/ip.c138
-rw-r--r--src/vnet/ip/ip.h10
-rw-r--r--src/vnet/ip/ip6_packet.h5
-rw-r--r--src/vnet/ip/ip_packet.h2
-rw-r--r--src/vnet/ip/ip_types.api1
-rw-r--r--src/vnet/ip/ip_types.c270
-rw-r--r--src/vnet/ip/ip_types.h41
-rw-r--r--src/vnet/ip/ip_types_api.c31
-rw-r--r--src/vnet/ip/ip_types_api.h7
10 files changed, 325 insertions, 182 deletions
diff --git a/src/vnet/ip/format.c b/src/vnet/ip/format.c
index be1c4fd32fb..8469d0716e2 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 88eff4f4d59..8959b4c09cc 100644
--- a/src/vnet/ip/ip.c
+++ b/src/vnet/ip/ip.c
@@ -163,126 +163,10 @@ ip_interface_get_first_ip (u32 sw_if_index, u8 is_ip4)
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 @@ format_ip_address_family (u8 * s, va_list * args)
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 75750c5a192..a6fcd410c0e 100644
--- a/src/vnet/ip/ip.h
+++ b/src/vnet/ip/ip.h
@@ -278,16 +278,6 @@ u8 ip_interface_has_address (u32 sw_if_index, ip46_address_t * ip, u8 is_ip4);
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 e823214dac9..7bbdd197f2f 100644
--- a/src/vnet/ip/ip6_packet.h
+++ b/src/vnet/ip/ip6_packet.h
@@ -49,9 +49,12 @@ typedef union
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 9a55d5f644e..e67cec1512f 100644
--- a/src/vnet/ip/ip_packet.h
+++ b/src/vnet/ip/ip_packet.h
@@ -48,7 +48,7 @@ typedef enum ip_protocol
#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 49294587991..3e6149a78e7 100644
--- a/src/vnet/ip/ip_types.api
+++ b/src/vnet/ip/ip_types.api
@@ -70,6 +70,7 @@ enum ip_proto {
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 9659ca503b4..cd08c5417ad 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 @@ ip_address_cmp (const ip_address_t * ip1, const ip_address_t * ip2)
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 @@ ip_address_copy (ip_address_t * dst, const ip_address_t * src)
{
/* 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 @@ ip_address_copy (ip_address_t * dst, const ip_address_t * src)
}
}
+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 @@ ip_version_to_size (u8 ver)
return 0;
}
-void
-ip_address_set (ip_address_t * dst, const void *src, u8 version)
+vnet_link_t
+ip_address_family_to_link_type (ip_address_family_t af)
{
- clib_memcpy (dst, src, ip_version_to_size (version));
- ip_addr_version (dst) = version;
+ switch (af)
+ {
+ case AF_IP4:
+ return (VNET_LINK_IP4);
+ case AF_IP6:
+ return (VNET_LINK_IP6);
+ }
+ ASSERT (0);
+ return (VNET_LINK_IP4);
}
-fib_protocol_t
-ip_address_to_46 (const ip_address_t * addr, ip46_address_t * a)
+
+void
+ip_address_set (ip_address_t * dst, const void *src, u8 version)
{
- fib_protocol_t proto = FIB_PROTOCOL_IP4;
+ ip_addr_version (dst) = version;
- switch (ip_addr_version (addr))
+ switch (version)
{
case AF_IP4:
- ip46_address_set_ip4 (a, &addr->ip.v4);
+ ip_addr_v4 (dst) = *(ip4_address_t *) src;
break;
case AF_IP6:
- proto = FIB_PROTOCOL_IP6;
- a->ip6 = addr->ip.v6;
+ ip_addr_v6 (dst) = *(ip6_address_t *) src;
break;
}
+}
- return (proto);
+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);
}
-void
-ip_address_from_46 (const ip46_address_t * nh,
- fib_protocol_t fproto, ip_address_t * ip)
+ip_address_family_t
+ip_address_family_from_fib_proto (fib_protocol_t fp)
{
- switch (fproto)
+ switch (fp)
{
case FIB_PROTOCOL_IP4:
- clib_memset (ip, 0, sizeof (*ip));
- ip_address_set (ip, &nh->ip4, AF_IP4);
- break;
+ return (AF_IP4);
case FIB_PROTOCOL_IP6:
- ip_address_set (ip, &nh->ip6, AF_IP6);
- break;
- default:
+ return (AF_IP6);
+ case FIB_PROTOCOL_MPLS:
ASSERT (0);
- break;
}
+ return (AF_IP4);
+}
+
+fib_protocol_t
+ip_address_to_46 (const ip_address_t * addr, ip46_address_t * a)
+{
+ *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)
+{
+ ip_addr_46 (ip) = *nh;
+ ip_addr_version (ip) = ip_address_family_from_fib_proto (fproto);
}
static void
@@ -311,6 +364,165 @@ ip_prefix_cmp (ip_prefix_t * p1, ip_prefix_t * p2)
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 720ef1a5edf..907d4f0afa9 100644
--- a/src/vnet/ip/ip_types.h
+++ b/src/vnet/ip/ip_types.h
@@ -22,7 +22,9 @@ typedef enum ip_address_family_t_
{
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 @@ extern u8 *format_ip_address_family (u8 * s, va_list * args);
#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 (ip_address_t * dst, const ip_address_t * src);
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 void ip_prefix_to_fib_prefix (const ip_prefix_t * ipp,
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 d201d387f02..bcf594c70b2 100644
--- a/src/vnet/ip/ip_types_api.c
+++ b/src/vnet/ip/ip_types_api.c
@@ -166,19 +166,15 @@ ip_address_decode (const vl_api_address_t * in, ip46_address_t * out)
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 @@ ip_address_encode2 (const ip_address_t * in, vl_api_address_t * out)
{
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_prefix_decode (const vl_api_prefix_t * in, fib_prefix_t * out)
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 @@ ip_prefix_encode (const fib_prefix_t * in, vl_api_prefix_t * out)
}
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 2035dbab6ac..600fe090613 100644
--- a/src/vnet/ip/ip_types_api.h
+++ b/src/vnet/ip/ip_types_api.h
@@ -42,12 +42,13 @@ extern u8 ip_dscp_encode (ip_dscp_t dscp);
*/
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 ip4_address_decode (const vl_api_ip4_address_t in,
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);