@@ -395,8 +395,6 @@ get_failed:
static void gre_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
{
char s2[64];
- const char *local = "any";
- const char *remote = "any";
unsigned int iflags = 0;
unsigned int oflags = 0;
__u8 ttl = 0;
@@ -410,23 +408,8 @@ static void gre_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
return;
}
- if (tb[IFLA_GRE_REMOTE]) {
- unsigned int addr = rta_getattr_u32(tb[IFLA_GRE_REMOTE]);
-
- if (addr)
- remote = format_host(AF_INET, 4, &addr);
- }
-
- print_string(PRINT_ANY, "remote", "remote %s ", remote);
-
- if (tb[IFLA_GRE_LOCAL]) {
- unsigned int addr = rta_getattr_u32(tb[IFLA_GRE_LOCAL]);
-
- if (addr)
- local = format_host(AF_INET, 4, &addr);
- }
-
- print_string(PRINT_ANY, "local", "local %s ", local);
+ tnl_print_endpoint("remote", tb[IFLA_GRE_REMOTE], AF_INET);
+ tnl_print_endpoint("local", tb[IFLA_GRE_LOCAL], AF_INET);
if (tb[IFLA_GRE_LINK]) {
unsigned int link = rta_getattr_u32(tb[IFLA_GRE_LINK]);
@@ -429,13 +429,10 @@ get_failed:
static void gre_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
{
char s2[64];
- const char *local = "any";
- const char *remote = "any";
unsigned int iflags = 0;
unsigned int oflags = 0;
unsigned int flags = 0;
__u32 flowinfo = 0;
- struct in6_addr in6_addr_any = IN6ADDR_ANY_INIT;
__u8 ttl = 0;
if (!tb)
@@ -452,27 +449,8 @@ static void gre_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
if (tb[IFLA_GRE_FLOWINFO])
flowinfo = rta_getattr_u32(tb[IFLA_GRE_FLOWINFO]);
- if (tb[IFLA_GRE_REMOTE]) {
- struct in6_addr addr;
-
- memcpy(&addr, RTA_DATA(tb[IFLA_GRE_REMOTE]), sizeof(addr));
-
- if (memcmp(&addr, &in6_addr_any, sizeof(addr)))
- remote = format_host(AF_INET6, sizeof(addr), &addr);
- }
-
- print_string(PRINT_ANY, "remote", "remote %s ", remote);
-
- if (tb[IFLA_GRE_LOCAL]) {
- struct in6_addr addr;
-
- memcpy(&addr, RTA_DATA(tb[IFLA_GRE_LOCAL]), sizeof(addr));
-
- if (memcmp(&addr, &in6_addr_any, sizeof(addr)))
- local = format_host(AF_INET6, sizeof(addr), &addr);
- }
-
- print_string(PRINT_ANY, "local", "local %s ", local);
+ tnl_print_endpoint("remote", tb[IFLA_GRE_REMOTE], AF_INET6);
+ tnl_print_endpoint("local", tb[IFLA_GRE_LOCAL], AF_INET6);
if (tb[IFLA_GRE_LINK]) {
unsigned int link = rta_getattr_u32(tb[IFLA_GRE_LINK]);
@@ -366,19 +366,8 @@ static void ip6tunnel_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb
}
}
- if (tb[IFLA_IPTUN_REMOTE]) {
- print_string(PRINT_ANY,
- "remote",
- "remote %s ",
- rt_addr_n2a_rta(AF_INET6, tb[IFLA_IPTUN_REMOTE]));
- }
-
- if (tb[IFLA_IPTUN_LOCAL]) {
- print_string(PRINT_ANY,
- "local",
- "local %s ",
- rt_addr_n2a_rta(AF_INET6, tb[IFLA_IPTUN_LOCAL]));
- }
+ tnl_print_endpoint("remote", tb[IFLA_IPTUN_REMOTE], AF_INET6);
+ tnl_print_endpoint("local", tb[IFLA_IPTUN_LOCAL], AF_INET6);
if (tb[IFLA_IPTUN_LINK]) {
unsigned int link = rta_getattr_u32(tb[IFLA_IPTUN_LINK]);
@@ -362,8 +362,6 @@ get_failed:
static void iptunnel_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
{
char s2[64];
- const char *local = "any";
- const char *remote = "any";
__u16 prefixlen;
__u8 ttl = 0;
__u8 tos = 0;
@@ -393,23 +391,8 @@ static void iptunnel_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[
}
}
- if (tb[IFLA_IPTUN_REMOTE]) {
- unsigned int addr = rta_getattr_u32(tb[IFLA_IPTUN_REMOTE]);
-
- if (addr)
- remote = format_host(AF_INET, 4, &addr);
- }
-
- print_string(PRINT_ANY, "remote", "remote %s ", remote);
-
- if (tb[IFLA_IPTUN_LOCAL]) {
- unsigned int addr = rta_getattr_u32(tb[IFLA_IPTUN_LOCAL]);
-
- if (addr)
- local = format_host(AF_INET, 4, &addr);
- }
-
- print_string(PRINT_ANY, "local", "local %s ", local);
+ tnl_print_endpoint("remote", tb[IFLA_IPTUN_REMOTE], AF_INET);
+ tnl_print_endpoint("local", tb[IFLA_IPTUN_LOCAL], AF_INET);
if (tb[IFLA_IPTUN_LINK]) {
unsigned int link = rta_getattr_u32(tb[IFLA_IPTUN_LINK]);
@@ -165,30 +165,13 @@ get_failed:
static void vti_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
{
- const char *local = "any";
- const char *remote = "any";
char s2[64];
if (!tb)
return;
- if (tb[IFLA_VTI_REMOTE]) {
- unsigned int addr = rta_getattr_u32(tb[IFLA_VTI_REMOTE]);
-
- if (addr)
- remote = format_host(AF_INET, 4, &addr);
- }
-
- print_string(PRINT_ANY, "remote", "remote %s ", remote);
-
- if (tb[IFLA_VTI_LOCAL]) {
- unsigned int addr = rta_getattr_u32(tb[IFLA_VTI_LOCAL]);
-
- if (addr)
- local = format_host(AF_INET, 4, &addr);
- }
-
- print_string(PRINT_ANY, "local", "local %s ", local);
+ tnl_print_endpoint("remote", tb[IFLA_VTI_REMOTE], AF_INET);
+ tnl_print_endpoint("local", tb[IFLA_VTI_LOCAL], AF_INET);
if (tb[IFLA_VTI_LINK]) {
unsigned int link = rta_getattr_u32(tb[IFLA_VTI_LINK]);
@@ -171,30 +171,13 @@ get_failed:
static void vti6_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
{
- const char *local = "any";
- const char *remote = "any";
- struct in6_addr saddr;
- struct in6_addr daddr;
char s2[64];
if (!tb)
return;
- if (tb[IFLA_VTI_REMOTE]) {
- memcpy(&daddr, RTA_DATA(tb[IFLA_VTI_REMOTE]), sizeof(daddr));
-
- remote = format_host(AF_INET6, 16, &daddr);
- }
-
- print_string(PRINT_ANY, "remote", "remote %s ", remote);
-
- if (tb[IFLA_VTI_LOCAL]) {
- memcpy(&saddr, RTA_DATA(tb[IFLA_VTI_LOCAL]), sizeof(saddr));
-
- local = format_host(AF_INET6, 16, &saddr);
- }
-
- print_string(PRINT_ANY, "local", "local %s ", local);
+ tnl_print_endpoint("remote", tb[IFLA_VTI_REMOTE], AF_INET6);
+ tnl_print_endpoint("local", tb[IFLA_VTI_LOCAL], AF_INET6);
if (tb[IFLA_VTI_LINK]) {
unsigned int link = rta_getattr_u32(tb[IFLA_VTI_LINK]);
@@ -280,6 +280,33 @@ void tnl_print_encap(struct rtattr *tb[],
}
}
+void tnl_print_endpoint(const char *name, const struct rtattr *rta, int family)
+{
+ const char *value;
+ inet_prefix dst;
+
+ if (!rta) {
+ value = "any";
+ } else if (get_addr_rta(&dst, rta, family)) {
+ value = "unknown";
+ } else if (dst.flags & ADDRTYPE_UNSPEC) {
+ value = "any";
+ } else {
+ value = format_host(family, dst.bytelen, dst.data);
+ if (!value)
+ value = "unknown";
+ }
+
+ if (is_json_context()) {
+ print_string(PRINT_JSON, name, NULL, value);
+ } else {
+ SPRINT_BUF(b1);
+
+ snprintf(b1, sizeof(b1), "%s %%s ", name);
+ print_string(PRINT_FP, NULL, b1, value);
+ }
+}
+
/* tnl_print_stats - print tunnel statistics
*
* @buf - tunnel interface's line in /proc/net/dev,
@@ -37,6 +37,8 @@ __be32 tnl_parse_key(const char *name, const char *key);
void tnl_print_encap(struct rtattr *tb[],
int encap_type, int encap_flags,
int encap_sport, int encap_dport);
+void tnl_print_endpoint(const char *name,
+ const struct rtattr *rta, int family);
void tnl_print_stats(const char *buf);
#endif
Introduce and use tnl_print_endpoint() helper to print of tunnel endpoint address. Note that for AF_INET and AF_INET6 inet_ntop(3) is used that may return NULL in case of failure and while unlikely format_host_rta() might return NULL too. Handle this case when passing local/remote to print_string(). Signed-off-by: Serhey Popovych <serhe.popovych@gmail.com> --- ip/link_gre.c | 21 ++------------------- ip/link_gre6.c | 26 ++------------------------ ip/link_ip6tnl.c | 15 ++------------- ip/link_iptnl.c | 21 ++------------------- ip/link_vti.c | 21 ++------------------- ip/link_vti6.c | 21 ++------------------- ip/tunnel.c | 27 +++++++++++++++++++++++++++ ip/tunnel.h | 2 ++ 8 files changed, 41 insertions(+), 113 deletions(-)