ip/tunnel: Unify local/remote endpoint address printing

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>
Signed-off-by: David Ahern <dsahern@gmail.com>
This commit is contained in:
Serhey Popovych 2018-01-24 20:56:40 +02:00 committed by David Ahern
parent 228f2e97ba
commit b761fc4113
8 changed files with 41 additions and 113 deletions

View File

@ -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]);

View File

@ -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]);

View File

@ -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]);

View File

@ -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]);

View File

@ -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]);

View File

@ -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]);

View File

@ -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,

View File

@ -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