]> git.ipfire.org Git - thirdparty/iproute2.git/commitdiff
ip/tunnel: Correct and unify ttl/hoplimit printing
authorSerhey Popovych <serhe.popovych@gmail.com>
Thu, 18 Jan 2018 14:04:29 +0000 (16:04 +0200)
committerStephen Hemminger <stephen@networkplumber.org>
Fri, 19 Jan 2018 00:34:40 +0000 (16:34 -0800)
Both ttl/hoplimit is from 1 to 255. Zero has special meaning:
use encapsulated packet value. In ip-link(8) -d output this
looks like "ttl/hoplimit inherit". In JSON we have "int" type
for ttl and therefore values from 0 (inherit) to 255.

To do the best in handling ttl/hoplimit we need to accept
both cases: missing attribute in netlink dump and zero value
for "inherit"ed case. Last one is broken since JSON output
introduction for gre/iptnl versions and was never true for
gre6/ip6tnl.

For all tunnels, except ip6tnl change JSON type from "int" to
"uint" to reflect true nature of the ttl.

Signed-off-by: Serhey Popovych <serhe.popovych@gmail.com>
Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
ip/ip6tunnel.c
ip/iplink_geneve.c
ip/iplink_vxlan.c
ip/iproute_lwtunnel.c
ip/iptunnel.c
ip/link_gre.c
ip/link_gre6.c
ip/link_ip6tnl.c
ip/link_iptnl.c

index b8db49c979abc8ed9f9277a40ba87ff2589b163f..783e28a6e516dee8629bfd7a746c6f6b08ef79e5 100644 (file)
@@ -92,7 +92,10 @@ static void print_tunnel(struct ip6_tnl_parm2 *p)
        else
                printf(" encaplimit %u", p->encap_limit);
 
-       printf(" hoplimit %u", p->hop_limit);
+       if (p->hop_limit)
+               printf(" hoplimit %u", p->hop_limit);
+       else
+               printf(" hoplimit inherit");
 
        if (p->flags & IP6_TNL_F_USE_ORIG_TCLASS)
                printf(" tclass inherit");
index f0f1d1c76e3a1e25288c3da8c36d4ff2f2f3f2d7..5a0afd4ec094c00a717a9fa2a54fe1c61c958dbb 100644 (file)
@@ -227,6 +227,7 @@ static int geneve_parse_opt(struct link_util *lu, int argc, char **argv,
 static void geneve_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
 {
        __u32 vni;
+       __u8 ttl = 0;
        __u8 tos;
 
        if (!tb)
@@ -262,12 +263,12 @@ static void geneve_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
                }
        }
 
-       if (tb[IFLA_GENEVE_TTL]) {
-               __u8 ttl = rta_getattr_u8(tb[IFLA_GENEVE_TTL]);
-
-               if (ttl)
-                       print_int(PRINT_ANY, "ttl", "ttl %d ", ttl);
-       }
+       if (tb[IFLA_GENEVE_TTL])
+               ttl = rta_getattr_u8(tb[IFLA_GENEVE_TTL]);
+       if (is_json_context() || ttl)
+               print_uint(PRINT_ANY, "ttl", "ttl %u ", ttl);
+       else
+               print_string(PRINT_FP, NULL, "ttl %s ", "inherit");
 
        if (tb[IFLA_GENEVE_TOS] &&
            (tos = rta_getattr_u8(tb[IFLA_GENEVE_TOS]))) {
index ad7ef1c0d136f65349825b79931ad122474e1bd3..b9c335d3e189ece189600d8fba94a9920ca445a4 100644 (file)
@@ -394,6 +394,7 @@ static int vxlan_parse_opt(struct link_util *lu, int argc, char **argv,
 static void vxlan_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
 {
        __u32 vni;
+       __u8 ttl = 0;
        __u8 tos;
        __u32 maxaddr;
 
@@ -526,14 +527,12 @@ static void vxlan_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
                }
        }
 
-       if (tb[IFLA_VXLAN_TTL]) {
-               __u8 ttl = rta_getattr_u8(tb[IFLA_VXLAN_TTL]);
-
-               if (ttl)
-                       print_int(PRINT_ANY, "ttl", "ttl %d ", ttl);
-               else
-                       print_int(PRINT_JSON, "ttl", NULL, ttl);
-       }
+       if (tb[IFLA_VXLAN_TTL])
+               ttl = rta_getattr_u8(tb[IFLA_VXLAN_TTL]);
+       if (is_json_context() || ttl)
+               print_uint(PRINT_ANY, "ttl", "ttl %u ", ttl);
+       else
+               print_string(PRINT_FP, NULL, "ttl %s ", "inherit");
 
        if (tb[IFLA_VXLAN_LABEL]) {
                __u32 label = rta_getattr_u32(tb[IFLA_VXLAN_LABEL]);
index a8d71714b7419f0da34ec454733a6969557a1fcf..a1d36ba2409d75ef9a1ddc1ca90dfa075348f2e7 100644 (file)
@@ -271,7 +271,7 @@ static void print_encap_ip(FILE *fp, struct rtattr *encap)
                        rt_addr_n2a_rta(AF_INET, tb[LWTUNNEL_IP_DST]));
 
        if (tb[LWTUNNEL_IP_TTL])
-               fprintf(fp, "ttl %d ", rta_getattr_u8(tb[LWTUNNEL_IP_TTL]));
+               fprintf(fp, "ttl %u ", rta_getattr_u8(tb[LWTUNNEL_IP_TTL]));
 
        if (tb[LWTUNNEL_IP_TOS])
                fprintf(fp, "tos %d ", rta_getattr_u8(tb[LWTUNNEL_IP_TOS]));
@@ -326,7 +326,7 @@ static void print_encap_ip6(FILE *fp, struct rtattr *encap)
                        rt_addr_n2a_rta(AF_INET6, tb[LWTUNNEL_IP6_DST]));
 
        if (tb[LWTUNNEL_IP6_HOPLIMIT])
-               fprintf(fp, "hoplimit %d ",
+               fprintf(fp, "hoplimit %u ",
                        rta_getattr_u8(tb[LWTUNNEL_IP6_HOPLIMIT]));
 
        if (tb[LWTUNNEL_IP6_TC])
index ce610f8494dae6318c7f28fe9b870398ba04d435..0aa3b332e6cff73d806c66578b750d64f1af17b2 100644 (file)
@@ -326,7 +326,7 @@ static void print_tunnel(struct ip_tunnel_parm *p)
        }
 
        if (p->iph.ttl)
-               printf(" ttl %d", p->iph.ttl);
+               printf(" ttl %u", p->iph.ttl);
        else
                printf(" ttl inherit");
 
index 7463d7ca0866db0f6f701ab210f28d08ee2ab8d4..1cf7f4e883d387a1df564e9a61a2933d8b58c415 100644 (file)
@@ -361,6 +361,7 @@ static void gre_print_direct_opt(FILE *f, struct rtattr *tb[])
        const char *remote = "any";
        unsigned int iflags = 0;
        unsigned int oflags = 0;
+       __u8 ttl = 0;
 
        if (tb[IFLA_GRE_REMOTE]) {
                unsigned int addr = rta_getattr_u32(tb[IFLA_GRE_REMOTE]);
@@ -389,16 +390,12 @@ static void gre_print_direct_opt(FILE *f, struct rtattr *tb[])
                }
        }
 
-       if (tb[IFLA_GRE_TTL]) {
-               __u8 ttl = rta_getattr_u8(tb[IFLA_GRE_TTL]);
-
-               if (ttl)
-                       print_int(PRINT_ANY, "ttl", "ttl %d ", ttl);
-               else
-                       print_int(PRINT_JSON, "ttl", NULL, ttl);
-       } else {
+       if (tb[IFLA_GRE_TTL])
+               ttl = rta_getattr_u8(tb[IFLA_GRE_TTL]);
+       if (is_json_context() || ttl)
+               print_uint(PRINT_ANY, "ttl", "ttl %u ", ttl);
+       else
                print_string(PRINT_FP, NULL, "ttl %s ", "inherit");
-       }
 
        if (tb[IFLA_GRE_TOS] && rta_getattr_u8(tb[IFLA_GRE_TOS])) {
                int tos = rta_getattr_u8(tb[IFLA_GRE_TOS]);
index 7d38f47bb4162ff183f8c66e434157df837eba15..37bc5444daf61fc66ce22490b9ea4d7081e3756f 100644 (file)
@@ -382,6 +382,7 @@ static void gre_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
        unsigned int flags = 0;
        __u32 flowinfo = 0;
        struct in6_addr in6_addr_any = IN6ADDR_ANY_INIT;
+       __u8 ttl = 0;
 
        if (!tb)
                return;
@@ -423,14 +424,12 @@ static void gre_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
                }
        }
 
-       if (tb[IFLA_GRE_TTL]) {
-               __u8 ttl = rta_getattr_u8(tb[IFLA_GRE_TTL]);
-
-               if (ttl)
-                       print_int(PRINT_ANY, "ttl", "hoplimit %d ", ttl);
-               else
-                       print_int(PRINT_JSON, "ttl", NULL, ttl);
-       }
+       if (tb[IFLA_GRE_TTL])
+               ttl = rta_getattr_u8(tb[IFLA_GRE_TTL]);
+       if (is_json_context() || ttl)
+               print_uint(PRINT_ANY, "ttl", "hoplimit %u ", ttl);
+       else
+               print_string(PRINT_FP, NULL, "hoplimit %s ", "inherit");
 
        if (flags & IP6_TNL_F_IGN_ENCAP_LIMIT) {
                print_bool(PRINT_ANY,
index b22e7bc33cead1dc155d13722f35dc797792ddcb..605bc614d4b93633702d49246829b25dad5c2a31 100644 (file)
@@ -336,6 +336,7 @@ static void ip6tunnel_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb
        char s2[64];
        int flags = 0;
        __u32 flowinfo = 0;
+       __u8 ttl = 0;
 
        if (!tb)
                return;
@@ -386,12 +387,12 @@ static void ip6tunnel_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb
                }
        }
 
-       if (tb[IFLA_IPTUN_TTL]) {
-               print_uint(PRINT_ANY,
-                          "ttl",
-                          "hoplimit %u ",
-                          rta_getattr_u8(tb[IFLA_IPTUN_TTL]));
-       }
+       if (tb[IFLA_IPTUN_TTL])
+               ttl = rta_getattr_u8(tb[IFLA_IPTUN_TTL]);
+       if (is_json_context() || ttl)
+               print_uint(PRINT_ANY, "ttl", "hoplimit %u ", ttl);
+       else
+               print_string(PRINT_FP, NULL, "hoplimit %s ", "inherit");
 
        if (flags & IP6_TNL_F_IGN_ENCAP_LIMIT) {
                print_bool(PRINT_ANY,
index 35ef5c06b8246aac2ffb7b3318624707804ad7b3..93e1a1bcd43593a99ae8491422bbae357f0ecaac 100644 (file)
@@ -365,6 +365,7 @@ static void iptunnel_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[
        const char *local = "any";
        const char *remote = "any";
        __u16 prefixlen, type;
+       __u8 ttl = 0;
 
        if (!tb)
                return;
@@ -416,16 +417,12 @@ static void iptunnel_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[
                }
        }
 
-       if (tb[IFLA_IPTUN_TTL]) {
-               __u8 ttl = rta_getattr_u8(tb[IFLA_IPTUN_TTL]);
-
-               if (ttl)
-                       print_int(PRINT_ANY, "ttl", "ttl %d ", ttl);
-               else
-                       print_int(PRINT_JSON, "ttl", NULL, ttl);
-       } else {
+       if (tb[IFLA_IPTUN_TTL])
+               ttl = rta_getattr_u8(tb[IFLA_IPTUN_TTL]);
+       if (is_json_context() || ttl)
+               print_uint(PRINT_ANY, "ttl", "ttl %u ", ttl);
+       else
                print_string(PRINT_FP, NULL, "ttl %s ", "inherit");
-       }
 
        if (tb[IFLA_IPTUN_TOS]) {
                int tos = rta_getattr_u8(tb[IFLA_IPTUN_TOS]);