]> git.proxmox.com Git - mirror_iproute2.git/blobdiff - ip/link_iptnl.c
iplink_bareudp: use common include syntax
[mirror_iproute2.git] / ip / link_iptnl.c
index 2804b8f4625914b6ebe1ab43a0e8f8b03a16da2f..636cdb2cf5bb7135ff4854047ea384b6b8d354f4 100644 (file)
 #include "ip_common.h"
 #include "tunnel.h"
 
-static void print_usage(FILE *f, int sit)
+static void iptunnel_print_help(struct link_util *lu, int argc, char **argv,
+                               FILE *f)
 {
-       const char *type = sit ? "sit " : "ipip";
+       const char *mode;
 
-       fprintf(f,
-               "Usage: ... %s [ remote ADDR ]\n"
-               "                [ local ADDR ]\n"
-               "                [ ttl TTL ]\n"
-               "                [ tos TOS ]\n"
-               "                [ [no]pmtudisc ]\n"
-               "                [ dev PHYS_DEV ]\n"
-               "                [ 6rd-prefix ADDR ]\n"
-               "                [ 6rd-relay_prefix ADDR ]\n"
-               "                [ 6rd-reset ]\n"
-               "                [ noencap ]\n"
-               "                [ encap { fou | gue | none } ]\n"
-               "                [ encap-sport PORT ]\n"
-               "                [ encap-dport PORT ]\n"
-               "                [ [no]encap-csum ]\n"
-               "                [ [no]encap-csum6 ]\n"
-               "                [ [no]encap-remcsum ]\n",
-               type
-       );
-       if (sit) {
-               fprintf(f, "          [ mode { ip6ip | ipip | mplsip | any } ]\n");
-               fprintf(f, "          [ isatap ]\n");
+       if (strcmp(lu->id, "sit") == 0) {
+               mode =  "{ ip6ip | ipip | mplsip | any } ]\n"
+                       "                       [ isatap";
        } else {
-               fprintf(f, "          [ mode { ipip | mplsip | any } ]\n");
+               mode = "{ ipip | mplsip | any }";
        }
-       fprintf(f, "                [ external ]\n");
-       fprintf(f, "                [ fwmark MARK ]\n");
-       fprintf(f, "\n");
-       fprintf(f, "Where: ADDR := { IP_ADDRESS | any }\n");
-       fprintf(f, "       TOS  := { NUMBER | inherit }\n");
-       fprintf(f, "       TTL  := { 1..255 | inherit }\n");
-       fprintf(f, "       MARK := { 0x0..0xffffffff }\n");
-}
 
-static void usage(int sit) __attribute__((noreturn));
-static void usage(int sit)
-{
-       print_usage(stderr, sit);
-       exit(-1);
+       fprintf(f,
+               "Usage: ... %-6s        [ remote ADDR ]\n"
+               "                       [ local ADDR ]\n"
+               "                       [ ttl TTL ]\n"
+               "                       [ tos TOS ]\n"
+               "                       [ [no]pmtudisc ]\n"
+               "                       [ 6rd-prefix ADDR ]\n"
+               "                       [ 6rd-relay_prefix ADDR ]\n"
+               "                       [ 6rd-reset ]\n"
+               "                       [ dev PHYS_DEV ]\n"
+               "                       [ fwmark MARK ]\n"
+               "                       [ external ]\n"
+               "                       [ noencap ]\n"
+               "                       [ encap { fou | gue | none } ]\n"
+               "                       [ encap-sport PORT ]\n"
+               "                       [ encap-dport PORT ]\n"
+               "                       [ [no]encap-csum ]\n"
+               "                       [ [no]encap-csum6 ]\n"
+               "                       [ [no]encap-remcsum ]\n"
+               "                       [ mode %s ]\n"
+               "\n"
+               "Where: ADDR := { IP_ADDRESS | any }\n"
+               "       TOS  := { NUMBER | inherit }\n"
+               "       TTL  := { 1..255 | inherit }\n"
+               "       MARK := { 0x0..0xffffffff }\n",
+               lu->id, mode);
 }
 
 static int iptunnel_parse_opt(struct link_util *lu, int argc, char **argv,
                              struct nlmsghdr *n)
 {
-       struct ifinfomsg *ifi = (struct ifinfomsg *)(n + 1);
+       struct ifinfomsg *ifi = NLMSG_DATA(n);
        struct {
                struct nlmsghdr n;
                struct ifinfomsg i;
@@ -83,23 +78,18 @@ static int iptunnel_parse_opt(struct link_util *lu, int argc, char **argv,
                .i.ifi_family = preferred_family,
                .i.ifi_index = ifi->ifi_index,
        };
-       struct nlmsghdr *answer = NULL;
+       struct nlmsghdr *answer;
        struct rtattr *tb[IFLA_MAX + 1];
        struct rtattr *linkinfo[IFLA_INFO_MAX+1];
        struct rtattr *iptuninfo[IFLA_IPTUN_MAX + 1];
        int len;
-       __u32 link = 0;
-       __u32 laddr = 0;
-       __u32 raddr = 0;
-       __u8 ttl = 0;
-       __u8 tos = 0;
+       inet_prefix saddr, daddr, ip6rdprefix, ip6rdrelayprefix;
        __u8 pmtudisc = 1;
+       __u8 tos = 0;
        __u16 iflags = 0;
+       __u8 ttl = 0;
        __u8 proto = 0;
-       struct in6_addr ip6rdprefix = {};
-       __u16 ip6rdprefixlen = 0;
-       __u32 ip6rdrelayprefix = 0;
-       __u16 ip6rdrelayprefixlen = 0;
+       __u32 link = 0;
        __u16 encaptype = 0;
        __u16 encapflags = 0;
        __u16 encapsport = 0;
@@ -107,12 +97,19 @@ static int iptunnel_parse_opt(struct link_util *lu, int argc, char **argv,
        __u8 metadata = 0;
        __u32 fwmark = 0;
 
+       inet_prefix_reset(&saddr);
+       inet_prefix_reset(&daddr);
+
+       inet_prefix_reset(&ip6rdprefix);
+       inet_prefix_reset(&ip6rdrelayprefix);
+
        if (!(n->nlmsg_flags & NLM_F_CREATE)) {
+               const struct rtattr *rta;
+
                if (rtnl_talk(&rth, &req.n, &answer) < 0) {
 get_failed:
                        fprintf(stderr,
                                "Failed to get existing tunnel info.\n");
-                       free(answer);
                        return -1;
                }
 
@@ -134,22 +131,38 @@ get_failed:
                parse_rtattr_nested(iptuninfo, IFLA_IPTUN_MAX,
                                    linkinfo[IFLA_INFO_DATA]);
 
-               if (iptuninfo[IFLA_IPTUN_LOCAL])
-                       laddr = rta_getattr_u32(iptuninfo[IFLA_IPTUN_LOCAL]);
+               rta = iptuninfo[IFLA_IPTUN_LOCAL];
+               if (rta && get_addr_rta(&saddr, rta, AF_INET))
+                       goto get_failed;
 
-               if (iptuninfo[IFLA_IPTUN_REMOTE])
-                       raddr = rta_getattr_u32(iptuninfo[IFLA_IPTUN_REMOTE]);
+               rta = iptuninfo[IFLA_IPTUN_REMOTE];
+               if (rta && get_addr_rta(&daddr, rta, AF_INET))
+                       goto get_failed;
+
+               rta = iptuninfo[IFLA_IPTUN_6RD_PREFIX];
+               if (rta && get_addr_rta(&ip6rdprefix, rta, AF_INET6))
+                       goto get_failed;
+
+               rta = iptuninfo[IFLA_IPTUN_6RD_RELAY_PREFIX];
+               if (rta && get_addr_rta(&ip6rdrelayprefix, rta, AF_INET))
+                       goto get_failed;
+
+               rta = iptuninfo[IFLA_IPTUN_6RD_PREFIXLEN];
+               ip6rdprefix.bitlen = rta ? rta_getattr_u16(rta) : 0;
+
+               rta = iptuninfo[IFLA_IPTUN_6RD_RELAY_PREFIXLEN];
+               ip6rdrelayprefix.bitlen = rta ? rta_getattr_u16(rta) : 0;
 
                if (iptuninfo[IFLA_IPTUN_TTL])
                        ttl = rta_getattr_u8(iptuninfo[IFLA_IPTUN_TTL]);
 
-               if (iptuninfo[IFLA_IPTUN_TOS])
-                       tos = rta_getattr_u8(iptuninfo[IFLA_IPTUN_TOS]);
-
                if (iptuninfo[IFLA_IPTUN_PMTUDISC])
                        pmtudisc =
                                rta_getattr_u8(iptuninfo[IFLA_IPTUN_PMTUDISC]);
 
+               if (iptuninfo[IFLA_IPTUN_TOS])
+                       tos = rta_getattr_u8(iptuninfo[IFLA_IPTUN_TOS]);
+
                if (iptuninfo[IFLA_IPTUN_FLAGS])
                        iflags = rta_getattr_u16(iptuninfo[IFLA_IPTUN_FLAGS]);
 
@@ -167,22 +180,7 @@ get_failed:
                        encapsport = rta_getattr_u16(iptuninfo[IFLA_IPTUN_ENCAP_SPORT]);
                if (iptuninfo[IFLA_IPTUN_ENCAP_DPORT])
                        encapdport = rta_getattr_u16(iptuninfo[IFLA_IPTUN_ENCAP_DPORT]);
-               if (iptuninfo[IFLA_IPTUN_6RD_PREFIX])
-                       memcpy(&ip6rdprefix,
-                              RTA_DATA(iptuninfo[IFLA_IPTUN_6RD_PREFIX]),
-                              sizeof(laddr));
-
-               if (iptuninfo[IFLA_IPTUN_6RD_PREFIXLEN])
-                       ip6rdprefixlen =
-                               rta_getattr_u16(iptuninfo[IFLA_IPTUN_6RD_PREFIXLEN]);
-
-               if (iptuninfo[IFLA_IPTUN_6RD_RELAY_PREFIX])
-                       ip6rdrelayprefix =
-                               rta_getattr_u32(iptuninfo[IFLA_IPTUN_6RD_RELAY_PREFIX]);
-
-               if (iptuninfo[IFLA_IPTUN_6RD_RELAY_PREFIXLEN])
-                       ip6rdrelayprefixlen =
-                               rta_getattr_u16(iptuninfo[IFLA_IPTUN_6RD_RELAY_PREFIXLEN]);
+
                if (iptuninfo[IFLA_IPTUN_COLLECT_METADATA])
                        metadata = 1;
 
@@ -193,25 +191,38 @@ get_failed:
        }
 
        while (argc > 0) {
-               if (strcmp(*argv, "remote") == 0) {
+               if (strcmp(*argv, "mode") == 0) {
                        NEXT_ARG();
-                       if (strcmp(*argv, "any"))
-                               raddr = get_addr32(*argv);
+                       if (strcmp(lu->id, "sit") == 0 &&
+                           (strcmp(*argv, "ipv6/ipv4") == 0 ||
+                            strcmp(*argv, "ip6ip") == 0))
+                               proto = IPPROTO_IPV6;
+                       else if (strcmp(*argv, "ipv4/ipv4") == 0 ||
+                                strcmp(*argv, "ipip") == 0 ||
+                                strcmp(*argv, "ip4ip4") == 0)
+                               proto = IPPROTO_IPIP;
+                       else if (strcmp(*argv, "mpls/ipv4") == 0 ||
+                                  strcmp(*argv, "mplsip") == 0)
+                               proto = IPPROTO_MPLS;
+                       else if (strcmp(*argv, "any/ipv4") == 0 ||
+                                strcmp(*argv, "any") == 0)
+                               proto = 0;
                        else
-                               raddr = 0;
+                               invarg("Cannot guess tunnel mode.", *argv);
+               } else if (strcmp(*argv, "remote") == 0) {
+                       NEXT_ARG();
+                       get_addr(&daddr, *argv, AF_INET);
                } else if (strcmp(*argv, "local") == 0) {
                        NEXT_ARG();
-                       if (strcmp(*argv, "any"))
-                               laddr = get_addr32(*argv);
-                       else
-                               laddr = 0;
+                       get_addr(&saddr, *argv, AF_INET);
                } else if (matches(*argv, "dev") == 0) {
                        NEXT_ARG();
-                       link = if_nametoindex(*argv);
-                       if (link == 0)
-                               invarg("\"dev\" is invalid", *argv);
+                       link = ll_name_to_index(*argv);
+                       if (!link)
+                               exit(nodev(*argv));
                } else if (strcmp(*argv, "ttl") == 0 ||
-                          strcmp(*argv, "hoplimit") == 0) {
+                          strcmp(*argv, "hoplimit") == 0 ||
+                          strcmp(*argv, "hlim") == 0) {
                        NEXT_ARG();
                        if (strcmp(*argv, "inherit") != 0) {
                                if (get_u8(&ttl, *argv, 0))
@@ -220,6 +231,7 @@ get_failed:
                                ttl = 0;
                } else if (strcmp(*argv, "tos") == 0 ||
                           strcmp(*argv, "tclass") == 0 ||
+                          strcmp(*argv, "tc") == 0 ||
                           matches(*argv, "dsfield") == 0) {
                        __u32 uval;
 
@@ -237,39 +249,6 @@ get_failed:
                } else if (strcmp(lu->id, "sit") == 0 &&
                           strcmp(*argv, "isatap") == 0) {
                        iflags |= SIT_ISATAP;
-               } else if (strcmp(lu->id, "sit") == 0 &&
-                          strcmp(*argv, "mode") == 0) {
-                       NEXT_ARG();
-                       if (strcmp(*argv, "ipv6/ipv4") == 0 ||
-                           strcmp(*argv, "ip6ip") == 0)
-                               proto = IPPROTO_IPV6;
-                       else if (strcmp(*argv, "ipv4/ipv4") == 0 ||
-                                strcmp(*argv, "ipip") == 0 ||
-                                strcmp(*argv, "ip4ip4") == 0)
-                               proto = IPPROTO_IPIP;
-                       else if (strcmp(*argv, "mpls/ipv4") == 0 ||
-                                  strcmp(*argv, "mplsip") == 0)
-                               proto = IPPROTO_MPLS;
-                       else if (strcmp(*argv, "any/ipv4") == 0 ||
-                                strcmp(*argv, "any") == 0)
-                               proto = 0;
-                       else
-                               invarg("Cannot guess tunnel mode.", *argv);
-               } else if (strcmp(lu->id, "ipip") == 0 &&
-                          strcmp(*argv, "mode") == 0) {
-                       NEXT_ARG();
-                       if (strcmp(*argv, "ipv4/ipv4") == 0 ||
-                                strcmp(*argv, "ipip") == 0 ||
-                                strcmp(*argv, "ip4ip4") == 0)
-                               proto = IPPROTO_IPIP;
-                       else if (strcmp(*argv, "mpls/ipv4") == 0 ||
-                                  strcmp(*argv, "mplsip") == 0)
-                               proto = IPPROTO_MPLS;
-                       else if (strcmp(*argv, "any/ipv4") == 0 ||
-                                strcmp(*argv, "any") == 0)
-                               proto = 0;
-                       else
-                               invarg("Cannot guess tunnel mode.", *argv);
                } else if (strcmp(*argv, "noencap") == 0) {
                        encaptype = TUNNEL_ENCAP_NONE;
                } else if (strcmp(*argv, "encap") == 0) {
@@ -307,35 +286,24 @@ get_failed:
                } else if (strcmp(*argv, "external") == 0) {
                        metadata = 1;
                } else if (strcmp(*argv, "6rd-prefix") == 0) {
-                       inet_prefix prefix;
-
                        NEXT_ARG();
-                       if (get_prefix(&prefix, *argv, AF_INET6))
+                       if (get_prefix(&ip6rdprefix, *argv, AF_INET6))
                                invarg("invalid 6rd_prefix\n", *argv);
-                       memcpy(&ip6rdprefix, prefix.data, 16);
-                       ip6rdprefixlen = prefix.bitlen;
                } else if (strcmp(*argv, "6rd-relay_prefix") == 0) {
-                       inet_prefix prefix;
-
                        NEXT_ARG();
-                       if (get_prefix(&prefix, *argv, AF_INET))
+                       if (get_prefix(&ip6rdrelayprefix, *argv, AF_INET))
                                invarg("invalid 6rd-relay_prefix\n", *argv);
-                       memcpy(&ip6rdrelayprefix, prefix.data, 4);
-                       ip6rdrelayprefixlen = prefix.bitlen;
                } else if (strcmp(*argv, "6rd-reset") == 0) {
-                       inet_prefix prefix;
-
-                       get_prefix(&prefix, "2002::", AF_INET6);
-                       memcpy(&ip6rdprefix, prefix.data, 16);
-                       ip6rdprefixlen = 16;
-                       ip6rdrelayprefix = 0;
-                       ip6rdrelayprefixlen = 0;
+                       get_prefix(&ip6rdprefix, "2002::/16", AF_INET6);
+                       inet_prefix_reset(&ip6rdrelayprefix);
                } else if (strcmp(*argv, "fwmark") == 0) {
                        NEXT_ARG();
                        if (get_u32(&fwmark, *argv, 0))
                                invarg("invalid fwmark\n", *argv);
-               } else
-                       usage(strcmp(lu->id, "sit") == 0);
+               } else {
+                       iptunnel_print_help(lu, argc, argv, stderr);
+                       return -1;
+               }
                argc--, argv++;
        }
 
@@ -344,17 +312,24 @@ get_failed:
                exit(-1);
        }
 
+       addattr8(n, 1024, IFLA_IPTUN_PROTO, proto);
        if (metadata) {
                addattr_l(n, 1024, IFLA_IPTUN_COLLECT_METADATA, NULL, 0);
                return 0;
        }
 
-       addattr32(n, 1024, IFLA_IPTUN_LINK, link);
-       addattr32(n, 1024, IFLA_IPTUN_LOCAL, laddr);
-       addattr32(n, 1024, IFLA_IPTUN_REMOTE, raddr);
-       addattr8(n, 1024, IFLA_IPTUN_TTL, ttl);
-       addattr8(n, 1024, IFLA_IPTUN_TOS, tos);
+       if (is_addrtype_inet_not_unspec(&saddr)) {
+               addattr_l(n, 1024, IFLA_IPTUN_LOCAL,
+                         saddr.data, saddr.bytelen);
+       }
+       if (is_addrtype_inet_not_unspec(&daddr)) {
+               addattr_l(n, 1024, IFLA_IPTUN_REMOTE,
+                         daddr.data, daddr.bytelen);
+       }
        addattr8(n, 1024, IFLA_IPTUN_PMTUDISC, pmtudisc);
+       addattr8(n, 1024, IFLA_IPTUN_TOS, tos);
+       addattr8(n, 1024, IFLA_IPTUN_TTL, ttl);
+       addattr32(n, 1024, IFLA_IPTUN_LINK, link);
        addattr32(n, 1024, IFLA_IPTUN_FWMARK, fwmark);
 
        addattr16(n, 1024, IFLA_IPTUN_ENCAP_TYPE, encaptype);
@@ -362,20 +337,19 @@ get_failed:
        addattr16(n, 1024, IFLA_IPTUN_ENCAP_SPORT, htons(encapsport));
        addattr16(n, 1024, IFLA_IPTUN_ENCAP_DPORT, htons(encapdport));
 
-       if (strcmp(lu->id, "ipip") == 0 || strcmp(lu->id, "sit") == 0)
-               addattr8(n, 1024, IFLA_IPTUN_PROTO, proto);
-
        if (strcmp(lu->id, "sit") == 0) {
                addattr16(n, 1024, IFLA_IPTUN_FLAGS, iflags);
-               if (ip6rdprefixlen) {
+               if (is_addrtype_inet(&ip6rdprefix)) {
                        addattr_l(n, 1024, IFLA_IPTUN_6RD_PREFIX,
-                                 &ip6rdprefix, sizeof(ip6rdprefix));
+                                 ip6rdprefix.data, ip6rdprefix.bytelen);
                        addattr16(n, 1024, IFLA_IPTUN_6RD_PREFIXLEN,
-                                 ip6rdprefixlen);
+                                 ip6rdprefix.bitlen);
+               }
+               if (is_addrtype_inet(&ip6rdrelayprefix)) {
                        addattr32(n, 1024, IFLA_IPTUN_6RD_RELAY_PREFIX,
-                                 ip6rdrelayprefix);
+                                 ip6rdrelayprefix.data[0]);
                        addattr16(n, 1024, IFLA_IPTUN_6RD_RELAY_PREFIXLEN,
-                                 ip6rdrelayprefixlen);
+                                 ip6rdrelayprefix.bitlen);
                }
        }
 
@@ -384,71 +358,62 @@ get_failed:
 
 static void iptunnel_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
 {
-       char s1[1024];
        char s2[64];
-       const char *local = "any";
-       const char *remote = "any";
-       __u16 prefixlen, type;
+       __u16 prefixlen;
+       __u8 ttl = 0;
+       __u8 tos = 0;
 
        if (!tb)
                return;
 
-       if (tb[IFLA_IPTUN_COLLECT_METADATA])
+       if (tb[IFLA_IPTUN_COLLECT_METADATA]) {
                print_bool(PRINT_ANY, "external", "external ", true);
-
-       if (tb[IFLA_IPTUN_REMOTE]) {
-               unsigned int addr = rta_getattr_u32(tb[IFLA_IPTUN_REMOTE]);
-
-               if (addr)
-                       remote = format_host(AF_INET, 4, &addr);
+               return;
        }
 
-       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);
+       if (tb[IFLA_IPTUN_PROTO]) {
+               switch (rta_getattr_u8(tb[IFLA_IPTUN_PROTO])) {
+               case IPPROTO_IPIP:
+                       print_string(PRINT_ANY, "proto", "%s ", "ipip");
+                       break;
+               case IPPROTO_IPV6:
+                       print_string(PRINT_ANY, "proto", "%s ", "ip6ip");
+                       break;
+               case IPPROTO_MPLS:
+                       print_string(PRINT_ANY, "proto", "%s ", "mplsip");
+                       break;
+               case 0:
+                       print_string(PRINT_ANY, "proto", "%s ", "any");
+                       break;
+               }
        }
 
-       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] && rta_getattr_u32(tb[IFLA_IPTUN_LINK])) {
-               unsigned int link = rta_getattr_u32(tb[IFLA_IPTUN_LINK]);
-               const char *n = if_indextoname(link, s2);
+       if (tb[IFLA_IPTUN_LINK]) {
+               __u32 link = rta_getattr_u32(tb[IFLA_IPTUN_LINK]);
 
-               if (n)
-                       print_string(PRINT_ANY, "link", "dev %s ", n);
-               else
-                       print_int(PRINT_ANY, "link_index", "dev %u ", link);
+               if (link) {
+                       print_string(PRINT_ANY, "link", "dev %s ",
+                                    ll_index_to_name(link));
+               }
        }
 
-       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]);
-
-               if (tos) {
-                       if (is_json_context()) {
-                               print_0xhex(PRINT_JSON, "tos", "%#x", tos);
-                       } else {
-                               fputs("tos ", f);
-                               if (tos == 1)
-                                       fputs("inherit ", f);
-                               else
-                                       fprintf(f, "0x%x ", tos);
-                       }
-               }
+       if (tb[IFLA_IPTUN_TOS])
+               tos = rta_getattr_u8(tb[IFLA_IPTUN_TOS]);
+       if (tos) {
+               if (is_json_context() || tos != 1)
+                       print_0xhex(PRINT_ANY, "tos", "tos %#llx ", tos);
+               else
+                       print_string(PRINT_FP, NULL, "tos %s ", "inherit");
        }
 
        if (tb[IFLA_IPTUN_PMTUDISC] && rta_getattr_u8(tb[IFLA_IPTUN_PMTUDISC]))
@@ -472,7 +437,7 @@ static void iptunnel_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[
 
                const char *prefix = inet_ntop(AF_INET6,
                                               RTA_DATA(tb[IFLA_IPTUN_6RD_PREFIX]),
-                                              s1, sizeof(s1));
+                                              s2, sizeof(s2));
 
                if (is_json_context()) {
                        print_string(PRINT_JSON, "prefix", NULL, prefix);
@@ -499,84 +464,20 @@ static void iptunnel_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[
                }
        }
 
-       if (tb[IFLA_IPTUN_ENCAP_TYPE] &&
-           (type = rta_getattr_u16(tb[IFLA_IPTUN_ENCAP_TYPE])) != TUNNEL_ENCAP_NONE) {
-               __u16 flags = rta_getattr_u16(tb[IFLA_IPTUN_ENCAP_FLAGS]);
-               __u16 sport = rta_getattr_u16(tb[IFLA_IPTUN_ENCAP_SPORT]);
-               __u16 dport = rta_getattr_u16(tb[IFLA_IPTUN_ENCAP_DPORT]);
-
-               print_string(PRINT_FP, NULL, "encap ", NULL);
-               switch (type) {
-               case TUNNEL_ENCAP_FOU:
-                       print_string(PRINT_ANY, "type", "%s ", "fou");
-                       break;
-               case TUNNEL_ENCAP_GUE:
-                       print_string(PRINT_ANY, "type", "%s ", "gue");
-                       break;
-               default:
-                       print_null(PRINT_ANY, "type", "unknown ", NULL);
-                       break;
-               }
-
-               if (is_json_context()) {
-                       print_uint(PRINT_JSON,
-                                  "sport",
-                                  NULL,
-                                  sport ? ntohs(sport) : 0);
-                       print_uint(PRINT_JSON, "dport", NULL, ntohs(dport));
-                       print_bool(PRINT_JSON,
-                                  "csum",
-                                  NULL,
-                                  flags & TUNNEL_ENCAP_FLAG_CSUM);
-                       print_bool(PRINT_JSON,
-                                  "csum6",
-                                  NULL,
-                                  flags & TUNNEL_ENCAP_FLAG_CSUM6);
-                       print_bool(PRINT_JSON,
-                                  "remcsum",
-                                  NULL,
-                                  flags & TUNNEL_ENCAP_FLAG_REMCSUM);
-                       close_json_object();
-               } else {
-                       if (sport == 0)
-                               fputs("encap-sport auto ", f);
-                       else
-                               fprintf(f, "encap-sport %u", ntohs(sport));
-
-                       fprintf(f, "encap-dport %u ", ntohs(dport));
-
-                       if (flags & TUNNEL_ENCAP_FLAG_CSUM)
-                               fputs("encap-csum ", f);
-                       else
-                               fputs("noencap-csum ", f);
-
-                       if (flags & TUNNEL_ENCAP_FLAG_CSUM6)
-                               fputs("encap-csum6 ", f);
-                       else
-                               fputs("noencap-csum6 ", f);
-
-                       if (flags & TUNNEL_ENCAP_FLAG_REMCSUM)
-                               fputs("encap-remcsum ", f);
-                       else
-                               fputs("noencap-remcsum ", f);
-               }
-       }
-
        if (tb[IFLA_IPTUN_FWMARK]) {
                __u32 fwmark = rta_getattr_u32(tb[IFLA_IPTUN_FWMARK]);
 
                if (fwmark) {
-                       snprintf(s2, sizeof(s2), "0x%x", fwmark);
-
-                       print_string(PRINT_ANY, "fwmark", "fwmark %s ", s2);
+                       print_0xhex(PRINT_ANY,
+                                   "fwmark", "fwmark %#llx ", fwmark);
                }
        }
-}
 
-static void iptunnel_print_help(struct link_util *lu, int argc, char **argv,
-       FILE *f)
-{
-       print_usage(f, strcmp(lu->id, "sit") == 0);
+       tnl_print_encap(tb,
+                       IFLA_IPTUN_ENCAP_TYPE,
+                       IFLA_IPTUN_ENCAP_FLAGS,
+                       IFLA_IPTUN_ENCAP_SPORT,
+                       IFLA_IPTUN_ENCAP_DPORT);
 }
 
 struct link_util ipip_link_util = {