]> git.proxmox.com Git - mirror_iproute2.git/blobdiff - ip/iproute.c
ip: iplink: Convert to use parse_on_off()
[mirror_iproute2.git] / ip / iproute.c
index e9c4093fa3eb7b1152a04937fd37c83b57738fe8..ebb5f160fc4437b9e0f15af3a307425b0f701483 100644 (file)
@@ -69,7 +69,8 @@ static void usage(void)
                "                            [ from ADDRESS iif STRING ]\n"
                "                            [ oif STRING ] [ tos TOS ]\n"
                "                            [ mark NUMBER ] [ vrf NAME ]\n"
-               "                            [ uid NUMBER ]\n"
+               "                            [ uid NUMBER ] [ ipproto PROTOCOL ]\n"
+               "                            [ sport NUMBER ] [ dport NUMBER ]\n"
                "       ip route { add | del | change | append | replace } ROUTE\n"
                "SELECTOR := [ root PREFIX ] [ match PREFIX ] [ exact PREFIX ]\n"
                "            [ table TABLE_ID ] [ vrf NAME ] [ proto RTPROTO ]\n"
@@ -79,10 +80,10 @@ static void usage(void)
                "             [ table TABLE_ID ] [ proto RTPROTO ]\n"
                "             [ scope SCOPE ] [ metric METRIC ]\n"
                "             [ ttl-propagate { enabled | disabled } ]\n"
-               "INFO_SPEC := NH OPTIONS FLAGS [ nexthop NH ]...\n"
+               "INFO_SPEC := { NH | nhid ID } OPTIONS FLAGS [ nexthop NH ]...\n"
                "NH := [ encap ENCAPTYPE ENCAPHDR ] [ via [ FAMILY ] ADDRESS ]\n"
                "           [ dev STRING ] [ weight NUMBER ] NHFLAGS\n"
-               "FAMILY := [ inet | inet6 | ipx | dnet | mpls | bridge | link ]\n"
+               "FAMILY := [ inet | inet6 | mpls | bridge | link ]\n"
                "OPTIONS := FLAGS [ mtu NUMBER ] [ advmss NUMBER ] [ as [ to ] ADDRESS ]\n"
                "           [ rtt TIME ] [ rttvar TIME ] [ reordering NUMBER ]\n"
                "           [ window NUMBER ] [ cwnd NUMBER ] [ initcwnd NUMBER ]\n"
@@ -100,7 +101,7 @@ static void usage(void)
                "TIME := NUMBER[s|ms]\n"
                "BOOL := [1|0]\n"
                "FEATURES := ecn\n"
-               "ENCAPTYPE := [ mpls | ip | ip6 | seg6 | seg6local ]\n"
+               "ENCAPTYPE := [ mpls | ip | ip6 | seg6 | seg6local | rpl ]\n"
                "ENCAPHDR := [ MPLSLABEL | SEG6HDR ]\n"
                "SEG6HDR := [ mode SEGMODE ] segs ADDR1,ADDRi,ADDRn [hmac HMACKEYID] [cleanup]\n"
                "SEGMODE := [ encap | inline ]\n"
@@ -339,72 +340,100 @@ static void print_rtax_features(FILE *fp, unsigned int features)
        unsigned int of = features;
 
        if (features & RTAX_FEATURE_ECN) {
-               fprintf(fp, "ecn ");
+               print_null(PRINT_ANY, "ecn", "ecn ", NULL);
                features &= ~RTAX_FEATURE_ECN;
        }
 
        if (features)
-               fprintf(fp, "0x%x ", of);
+               print_0xhex(PRINT_ANY,
+                           "features", "%#llx ", of);
 }
 
-static void print_rt_flags(FILE *fp, unsigned int flags)
+void print_rt_flags(FILE *fp, unsigned int flags)
 {
+       open_json_array(PRINT_JSON,
+                       is_json_context() ?  "flags" : "");
+
        if (flags & RTNH_F_DEAD)
-               fprintf(fp, "dead ");
+               print_string(PRINT_ANY, NULL, "%s ", "dead");
        if (flags & RTNH_F_ONLINK)
-               fprintf(fp, "onlink ");
+               print_string(PRINT_ANY, NULL, "%s ", "onlink");
        if (flags & RTNH_F_PERVASIVE)
-               fprintf(fp, "pervasive ");
+               print_string(PRINT_ANY, NULL, "%s ", "pervasive");
        if (flags & RTNH_F_OFFLOAD)
-               fprintf(fp, "offload ");
+               print_string(PRINT_ANY, NULL, "%s ", "offload");
+       if (flags & RTNH_F_TRAP)
+               print_string(PRINT_ANY, NULL, "%s ", "trap");
+       if (flags & RTM_F_NOTIFY)
+               print_string(PRINT_ANY, NULL, "%s ", "notify");
        if (flags & RTNH_F_LINKDOWN)
-               fprintf(fp, "linkdown ");
+               print_string(PRINT_ANY, NULL, "%s ", "linkdown");
        if (flags & RTNH_F_UNRESOLVED)
-               fprintf(fp, "unresolved ");
+               print_string(PRINT_ANY, NULL, "%s ", "unresolved");
+       if (flags & RTM_F_OFFLOAD)
+               print_string(PRINT_ANY, NULL, "%s ", "rt_offload");
+       if (flags & RTM_F_TRAP)
+               print_string(PRINT_ANY, NULL, "%s ", "rt_trap");
+
+       close_json_array(PRINT_JSON, NULL);
 }
 
 static void print_rt_pref(FILE *fp, unsigned int pref)
 {
-       fprintf(fp, "pref ");
 
        switch (pref) {
        case ICMPV6_ROUTER_PREF_LOW:
-               fprintf(fp, "low");
+               print_string(PRINT_ANY,
+                            "pref", "pref %s", "low");
                break;
        case ICMPV6_ROUTER_PREF_MEDIUM:
-               fprintf(fp, "medium");
+               print_string(PRINT_ANY,
+                            "pref", "pref %s", "medium");
                break;
        case ICMPV6_ROUTER_PREF_HIGH:
-               fprintf(fp, "high");
+               print_string(PRINT_ANY,
+                            "pref", "pref %s", "high");
                break;
        default:
-               fprintf(fp, "%u", pref);
+               print_uint(PRINT_ANY,
+                          "pref", "%u", pref);
        }
 }
 
-static void print_rta_if(FILE *fp, const struct rtattr *rta,
-                        const char *prefix)
+void print_rta_if(FILE *fp, const struct rtattr *rta, const char *prefix)
 {
        const char *ifname = ll_index_to_name(rta_getattr_u32(rta));
 
-       fprintf(fp, "%s %s ", prefix, ifname);
+       if (is_json_context())
+               print_string(PRINT_JSON, prefix, NULL, ifname);
+       else {
+               fprintf(fp, "%s ", prefix);
+               color_fprintf(fp, COLOR_IFNAME, "%s ", ifname);
+       }
 }
 
 static void print_cache_flags(FILE *fp, __u32 flags)
 {
+       json_writer_t *jw = get_json_writer();
        flags &= ~0xFFFF;
 
-       fprintf(fp, "%s    cache ", _SL_);
-
-       if (flags == 0)
-               return;
-
-       putc('<', fp);
+       if (jw) {
+               jsonw_name(jw, "cache");
+               jsonw_start_array(jw);
+       } else {
+               fprintf(fp, "%s    cache ", _SL_);
+               if (flags == 0)
+                       return;
+               putc('<', fp);
+       }
 
 #define PRTFL(fl, flname)                                              \
        if (flags & RTCF_##fl) {                                        \
                flags &= ~RTCF_##fl;                                    \
-               fprintf(fp, "%s%s", flname, flags ? "," : "> ");        \
+               if (jw)                                                 \
+                       jsonw_string(jw, flname);                       \
+               else                                                    \
+                       fprintf(fp, "%s%s", flname, flags ? "," : "> "); \
        }
 
        PRTFL(LOCAL, "local");
@@ -424,7 +453,10 @@ static void print_cache_flags(FILE *fp, __u32 flags)
 #undef PRTFL
 
        if (flags)
-               fprintf(fp, "%#x> ", flags);
+               print_hex(PRINT_ANY, "flags", "%x>", flags);
+
+       if (jw)
+               jsonw_end_array(jw);
 }
 
 static void print_rta_cacheinfo(FILE *fp, const struct rta_cacheinfo *ci)
@@ -433,23 +465,34 @@ static void print_rta_cacheinfo(FILE *fp, const struct rta_cacheinfo *ci)
 
        if (!hz)
                hz = get_user_hz();
+
        if (ci->rta_expires != 0)
-               fprintf(fp, "expires %dsec ", ci->rta_expires/hz);
+               print_int(PRINT_ANY, "expires",
+                          "expires %dsec ", ci->rta_expires/hz);
        if (ci->rta_error != 0)
-               fprintf(fp, "error %d ", ci->rta_error);
+               print_uint(PRINT_ANY, "error",
+                          "error %u ", ci->rta_error);
+
        if (show_stats) {
                if (ci->rta_clntref)
-                       fprintf(fp, "users %d ", ci->rta_clntref);
+                       print_uint(PRINT_ANY, "users",
+                                  "users %u ", ci->rta_clntref);
                if (ci->rta_used != 0)
-                       fprintf(fp, "used %d ", ci->rta_used);
+                       print_uint(PRINT_ANY, "used",
+                                  "used %u ", ci->rta_used);
                if (ci->rta_lastuse != 0)
-                       fprintf(fp, "age %dsec ", ci->rta_lastuse/hz);
+                       print_uint(PRINT_ANY, "age",
+                                  "age %usec ", ci->rta_lastuse/hz);
        }
        if (ci->rta_id)
-               fprintf(fp, "ipid 0x%04x ", ci->rta_id);
-       if (ci->rta_ts || ci->rta_tsage)
-               fprintf(fp, "ts 0x%x tsage %dsec ",
-                       ci->rta_ts, ci->rta_tsage);
+               print_0xhex(PRINT_ANY, "ipid",
+                           "ipid 0x%04llx ", ci->rta_id);
+       if (ci->rta_ts || ci->rta_tsage) {
+               print_0xhex(PRINT_ANY, "ts",
+                           "ts 0x%llx", ci->rta_ts);
+               print_uint(PRINT_ANY, "tsage",
+                          "tsage %usec ", ci->rta_tsage);
+       }
 }
 
 static void print_rta_flow(FILE *fp, const struct rtattr *rta)
@@ -459,13 +502,24 @@ static void print_rta_flow(FILE *fp, const struct rtattr *rta)
        SPRINT_BUF(b1);
 
        to &= 0xFFFF;
-       fprintf(fp, "realm%s ", from ? "s" : "");
-       if (from) {
-               fprintf(fp, "%s/",
-                       rtnl_rtrealm_n2a(from, b1, sizeof(b1)));
+       if (is_json_context()) {
+               open_json_object("flow");
+
+               if (from)
+                       print_string(PRINT_JSON, "from", NULL,
+                                    rtnl_rtrealm_n2a(from, b1, sizeof(b1)));
+               print_string(PRINT_JSON, "to", NULL,
+                            rtnl_rtrealm_n2a(to, b1, sizeof(b1)));
+               close_json_object();
+       } else {
+               fprintf(fp, "realm%s ", from ? "s" : "");
+
+               if (from)
+                       print_string(PRINT_FP, NULL, "%s/",
+                                    rtnl_rtrealm_n2a(from, b1, sizeof(b1)));
+               print_string(PRINT_FP, NULL, "%s ",
+                            rtnl_rtrealm_n2a(to, b1, sizeof(b1)));
        }
-       fprintf(fp, "%s ",
-               rtnl_rtrealm_n2a(to, b1, sizeof(b1)));
 }
 
 static void print_rta_newdst(FILE *fp, const struct rtmsg *r,
@@ -473,25 +527,52 @@ static void print_rta_newdst(FILE *fp, const struct rtmsg *r,
 {
        const char *newdst = format_host_rta(r->rtm_family, rta);
 
-       fprintf(fp, "as to %s ", newdst);
+       if (is_json_context())
+               print_string(PRINT_JSON, "to", NULL, newdst);
+       else {
+               fprintf(fp, "as to ");
+               print_color_string(PRINT_FP,
+                                  ifa_family_color(r->rtm_family),
+                                  NULL, "%s ", newdst);
+       }
 }
 
-static void print_rta_gateway(FILE *fp, const struct rtmsg *r,
-                             const struct rtattr *rta)
+void print_rta_gateway(FILE *fp, unsigned char family, const struct rtattr *rta)
 {
-       const char *gateway = format_host_rta(r->rtm_family, rta);
-
-       fprintf(fp, "via %s ", gateway);
+       const char *gateway = format_host_rta(family, rta);
+
+       if (is_json_context())
+               print_string(PRINT_JSON, "gateway", NULL, gateway);
+       else {
+               fprintf(fp, "via ");
+               print_color_string(PRINT_FP,
+                                  ifa_family_color(family),
+                                  NULL, "%s ", gateway);
+       }
 }
 
 static void print_rta_via(FILE *fp, const struct rtattr *rta)
 {
+       size_t len = RTA_PAYLOAD(rta) - 2;
        const struct rtvia *via = RTA_DATA(rta);
-       size_t len = RTA_PAYLOAD(rta);
 
-       fprintf(fp, "via %s %s ",
-               family_name(via->rtvia_family),
-               format_host(via->rtvia_family, len, via->rtvia_addr));
+       if (is_json_context()) {
+               open_json_object("via");
+               print_string(PRINT_JSON, "family", NULL,
+                            family_name(via->rtvia_family));
+               print_string(PRINT_JSON, "host", NULL,
+                            format_host(via->rtvia_family, len,
+                                        via->rtvia_addr));
+               close_json_object();
+       } else {
+               print_string(PRINT_FP, NULL, "via %s ",
+                            family_name(via->rtvia_family));
+               print_color_string(PRINT_FP,
+                                  ifa_family_color(via->rtvia_family),
+                                  NULL, "%s ",
+                                  format_host(via->rtvia_family,
+                                              len, via->rtvia_addr));
+       }
 }
 
 static void print_rta_metrics(FILE *fp, const struct rtattr *rta)
@@ -500,6 +581,9 @@ static void print_rta_metrics(FILE *fp, const struct rtattr *rta)
        unsigned int mxlock = 0;
        int i;
 
+       open_json_array(PRINT_JSON, "metrics");
+       open_json_object(NULL);
+
        parse_rtattr(mxrta, RTAX_MAX, RTA_DATA(rta), RTA_PAYLOAD(rta));
 
        if (mxrta[RTAX_LOCK])
@@ -517,20 +601,22 @@ static void print_rta_metrics(FILE *fp, const struct rtattr *rta)
                if (i == RTAX_HOPLIMIT && (int)val == -1)
                        continue;
 
-               if (i < sizeof(mx_names)/sizeof(char *) && mx_names[i])
-                       fprintf(fp, "%s ", mx_names[i]);
-               else
-                       fprintf(fp, "metric %d ", i);
+               if (!is_json_context()) {
+                       if (i < sizeof(mx_names)/sizeof(char *) && mx_names[i])
+                               fprintf(fp, "%s ", mx_names[i]);
+                       else
+                               fprintf(fp, "metric %d ", i);
 
-               if (mxlock & (1<<i))
-                       fprintf(fp, "lock ");
+                       if (mxlock & (1<<i))
+                               fprintf(fp, "lock ");
+               }
 
                switch (i) {
                case RTAX_FEATURES:
                        print_rtax_features(fp, val);
                        break;
                default:
-                       fprintf(fp, "%u ", val);
+                       print_uint(PRINT_ANY, mx_names[i], "%u ", val);
                        break;
 
                case RTAX_RTT:
@@ -541,16 +627,25 @@ static void print_rta_metrics(FILE *fp, const struct rtattr *rta)
                        else if (i == RTAX_RTTVAR)
                                val /= 4;
 
-                       if (val >= 1000)
-                               fprintf(fp, "%gs ", val/1e3);
-                       else
-                               fprintf(fp, "%ums ", val);
+                       if (is_json_context())
+                               print_uint(PRINT_JSON, mx_names[i],
+                                          NULL, val);
+                       else {
+                               if (val >= 1000)
+                                       fprintf(fp, "%gs ", val/1e3);
+                               else
+                                       fprintf(fp, "%ums ", val);
+                       }
                        break;
                case RTAX_CC_ALGO:
-                       fprintf(fp, "%s ", rta_getattr_str(mxrta[i]));
+                       print_string(PRINT_ANY, "congestion",
+                                    "%s ", rta_getattr_str(mxrta[i]));
                        break;
                }
        }
+
+       close_json_object();
+       close_json_array(PRINT_JSON, NULL);
 }
 
 static void print_rta_multipath(FILE *fp, const struct rtmsg *r,
@@ -560,22 +655,26 @@ static void print_rta_multipath(FILE *fp, const struct rtmsg *r,
        int len = RTA_PAYLOAD(rta);
        int first = 1;
 
-       while (len > sizeof(*nh)) {
+       open_json_array(PRINT_JSON, "nexthops");
+
+       while (len >= sizeof(*nh)) {
                struct rtattr *tb[RTA_MAX + 1];
 
                if (nh->rtnh_len > len)
                        break;
 
+               open_json_object(NULL);
+
                if ((r->rtm_flags & RTM_F_CLONED) &&
                    r->rtm_type == RTN_MULTICAST) {
                        if (first) {
-                               fprintf(fp, "Oifs: ");
+                               print_string(PRINT_FP, NULL, "Oifs: ", NULL);
                                first = 0;
                        } else {
-                               fprintf(fp, " ");
+                               print_string(PRINT_FP, NULL, " ", NULL);
                        }
                } else
-                       fprintf(fp, "%s\tnexthop ", _SL_);
+                       print_string(PRINT_FP, NULL, "%s\tnexthop ", _SL_);
 
                if (nh->rtnh_len > sizeof(*nh)) {
                        parse_rtattr(tb, RTA_MAX, RTNH_DATA(nh),
@@ -588,7 +687,8 @@ static void print_rta_multipath(FILE *fp, const struct rtmsg *r,
                        if (tb[RTA_NEWDST])
                                print_rta_newdst(fp, r, tb[RTA_NEWDST]);
                        if (tb[RTA_GATEWAY])
-                               print_rta_gateway(fp, r, tb[RTA_GATEWAY]);
+                               print_rta_gateway(fp, r->rtm_family,
+                                                 tb[RTA_GATEWAY]);
                        if (tb[RTA_VIA])
                                print_rta_via(fp, tb[RTA_VIA]);
                        if (tb[RTA_FLOW])
@@ -597,32 +697,39 @@ static void print_rta_multipath(FILE *fp, const struct rtmsg *r,
 
                if ((r->rtm_flags & RTM_F_CLONED) &&
                    r->rtm_type == RTN_MULTICAST) {
-                       fprintf(fp, "%s", ll_index_to_name(nh->rtnh_ifindex));
+                       print_string(PRINT_ANY, "dev",
+                                    "%s", ll_index_to_name(nh->rtnh_ifindex));
+
                        if (nh->rtnh_hops != 1)
-                               fprintf(fp, "(ttl>%d)", nh->rtnh_hops);
-                       fprintf(fp, " ");
+                               print_int(PRINT_ANY, "ttl", "(ttl>%d)", nh->rtnh_hops);
+
+                       print_string(PRINT_FP, NULL, " ", NULL);
                } else {
-                       fprintf(fp, "dev %s ",
-                               ll_index_to_name(nh->rtnh_ifindex));
+                       print_string(PRINT_ANY, "dev",
+                                    "dev %s ", ll_index_to_name(nh->rtnh_ifindex));
+
                        if (r->rtm_family != AF_MPLS)
-                               fprintf(fp, "weight %d ",
-                                       nh->rtnh_hops+1);
+                               print_int(PRINT_ANY, "weight",
+                                         "weight %d ", nh->rtnh_hops + 1);
                }
 
                print_rt_flags(fp, nh->rtnh_flags);
 
                len -= NLMSG_ALIGN(nh->rtnh_len);
                nh = RTNH_NEXT(nh);
+
+               close_json_object();
        }
+       close_json_array(PRINT_JSON, NULL);
 }
 
-int print_route(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
+int print_route(struct nlmsghdr *n, void *arg)
 {
        FILE *fp = (FILE *)arg;
        struct rtmsg *r = NLMSG_DATA(n);
        int len = n->nlmsg_len;
        struct rtattr *tb[RTA_MAX+1];
-       int host_len, family;
+       int family, color, host_len;
        __u32 table;
        int ret;
 
@@ -653,7 +760,8 @@ int print_route(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
                struct nlmsghdr *fn;
 
                if (NLMSG_ALIGN(filter.flushp) + n->nlmsg_len > filter.flushe) {
-                       if ((ret = flush_update()) < 0)
+                       ret = flush_update();
+                       if (ret < 0)
                                return ret;
                }
                fn = (struct nlmsghdr *)(filter.flushb + NLMSG_ALIGN(filter.flushp));
@@ -667,41 +775,62 @@ int print_route(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
                        return 0;
        }
 
+       open_json_object(NULL);
        if (n->nlmsg_type == RTM_DELROUTE)
-               fprintf(fp, "Deleted ");
+               print_bool(PRINT_ANY, "deleted", "Deleted ", true);
+
        if ((r->rtm_type != RTN_UNICAST || show_details > 0) &&
            (!filter.typemask || (filter.typemask & (1 << r->rtm_type))))
-               fprintf(fp, "%s ", rtnl_rtntype_n2a(r->rtm_type, b1, sizeof(b1)));
+               print_string(PRINT_ANY, "type", "%s ",
+                            rtnl_rtntype_n2a(r->rtm_type, b1, sizeof(b1)));
 
+       color = COLOR_NONE;
        if (tb[RTA_DST]) {
                family = get_real_family(r->rtm_type, r->rtm_family);
+               color = ifa_family_color(family);
+
                if (r->rtm_dst_len != host_len) {
-                       fprintf(fp, "%s/%u ",
-                               rt_addr_n2a_rta(family, tb[RTA_DST]),
-                               r->rtm_dst_len);
+                       snprintf(b1, sizeof(b1),
+                                "%s/%u", rt_addr_n2a_rta(family, tb[RTA_DST]),
+                                r->rtm_dst_len);
                } else {
-                       fprintf(fp, "%s ",
-                               format_host_rta(family, tb[RTA_DST]));
+                       format_host_rta_r(family, tb[RTA_DST],
+                                         b1, sizeof(b1));
+
                }
        } else if (r->rtm_dst_len) {
-               fprintf(fp, "0/%d ", r->rtm_dst_len);
+               snprintf(b1, sizeof(b1), "0/%d ", r->rtm_dst_len);
        } else {
-               fprintf(fp, "default ");
+               strncpy(b1, "default", sizeof(b1));
        }
+       print_color_string(PRINT_ANY, color,
+                          "dst", "%s ", b1);
+
        if (tb[RTA_SRC]) {
                family = get_real_family(r->rtm_type, r->rtm_family);
+               color = ifa_family_color(family);
+
                if (r->rtm_src_len != host_len) {
-                       fprintf(fp, "from %s/%u ",
-                               rt_addr_n2a_rta(family, tb[RTA_SRC]),
-                               r->rtm_src_len);
+                       snprintf(b1, sizeof(b1),
+                                "%s/%u",
+                                rt_addr_n2a_rta(family, tb[RTA_SRC]),
+                                r->rtm_src_len);
                } else {
-                       fprintf(fp, "from %s ",
-                               format_host_rta(family, tb[RTA_SRC]));
+                       format_host_rta_r(family, tb[RTA_SRC],
+                                         b1, sizeof(b1));
                }
+               print_color_string(PRINT_ANY, color,
+                                  "from", "from %s ", b1);
        } else if (r->rtm_src_len) {
-               fprintf(fp, "from 0/%u ", r->rtm_src_len);
+               snprintf(b1, sizeof(b1), "0/%u", r->rtm_src_len);
+
+               print_string(PRINT_ANY, "src", "from %s ", b1);
        }
 
+       if (tb[RTA_NH_ID])
+               print_uint(PRINT_ANY, "nhid", "nhid %u ",
+                          rta_getattr_u32(tb[RTA_NH_ID]));
+
        if (tb[RTA_NEWDST])
                print_rta_newdst(fp, r, tb[RTA_NEWDST]);
 
@@ -709,36 +838,61 @@ int print_route(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
                lwt_print_encap(fp, tb[RTA_ENCAP_TYPE], tb[RTA_ENCAP]);
 
        if (r->rtm_tos && filter.tosmask != -1) {
-               SPRINT_BUF(b1);
-               fprintf(fp, "tos %s ", rtnl_dsfield_n2a(r->rtm_tos, b1, sizeof(b1)));
+               print_string(PRINT_ANY, "tos", "tos %s ",
+                            rtnl_dsfield_n2a(r->rtm_tos, b1, sizeof(b1)));
        }
 
        if (tb[RTA_GATEWAY] && filter.rvia.bitlen != host_len)
-               print_rta_gateway(fp, r, tb[RTA_GATEWAY]);
+               print_rta_gateway(fp, r->rtm_family, tb[RTA_GATEWAY]);
 
        if (tb[RTA_VIA])
                print_rta_via(fp, tb[RTA_VIA]);
 
        if (tb[RTA_OIF] && filter.oifmask != -1)
-               print_rta_if(fp,  tb[RTA_OIF], "dev");
+               print_rta_if(fp, tb[RTA_OIF], "dev");
 
        if (table && (table != RT_TABLE_MAIN || show_details > 0) && !filter.tb)
-               fprintf(fp, "table %s ", rtnl_rttable_n2a(table, b1, sizeof(b1)));
+               print_string(PRINT_ANY,
+                            "table", "table %s ",
+                            rtnl_rttable_n2a(table, b1, sizeof(b1)));
+
        if (!(r->rtm_flags & RTM_F_CLONED)) {
-               if ((r->rtm_protocol != RTPROT_BOOT || show_details > 0) && filter.protocolmask != -1)
-                       fprintf(fp, "proto %s ", rtnl_rtprot_n2a(r->rtm_protocol, b1, sizeof(b1)));
-               if ((r->rtm_scope != RT_SCOPE_UNIVERSE || show_details > 0) && filter.scopemask != -1)
-                       fprintf(fp, "scope %s ", rtnl_rtscope_n2a(r->rtm_scope, b1, sizeof(b1)));
+               if ((r->rtm_protocol != RTPROT_BOOT || show_details > 0) &&
+                   filter.protocolmask != -1)
+                       print_string(PRINT_ANY,
+                                    "protocol", "proto %s ",
+                                    rtnl_rtprot_n2a(r->rtm_protocol,
+                                                    b1, sizeof(b1)));
+
+               if ((r->rtm_scope != RT_SCOPE_UNIVERSE || show_details > 0) &&
+                   filter.scopemask != -1)
+                       print_string(PRINT_ANY,
+                                    "scope", "scope %s ",
+                                    rtnl_rtscope_n2a(r->rtm_scope,
+                                                     b1, sizeof(b1)));
        }
+
        if (tb[RTA_PREFSRC] && filter.rprefsrc.bitlen != host_len) {
+               const char *psrc
+                       = rt_addr_n2a_rta(r->rtm_family, tb[RTA_PREFSRC]);
+
                /* Do not use format_host(). It is our local addr
                   and symbolic name will not be useful.
-                */
-               fprintf(fp, "src %s ",
-                       rt_addr_n2a_rta(r->rtm_family, tb[RTA_PREFSRC]));
+               */
+               if (is_json_context())
+                       print_string(PRINT_JSON, "prefsrc", NULL, psrc);
+               else {
+                       fprintf(fp, "src ");
+                       print_color_string(PRINT_FP,
+                                          ifa_family_color(r->rtm_family),
+                                          NULL, "%s ", psrc);
+               }
+
        }
+
        if (tb[RTA_PRIORITY] && filter.metricmask != -1)
-               fprintf(fp, "metric %u ", rta_getattr_u32(tb[RTA_PRIORITY]));
+               print_uint(PRINT_ANY, "metric", "metric %u ",
+                          rta_getattr_u32(tb[RTA_PRIORITY]));
 
        print_rt_flags(fp, r->rtm_flags);
 
@@ -746,10 +900,14 @@ int print_route(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
                unsigned int mark = rta_getattr_u32(tb[RTA_MARK]);
 
                if (mark) {
-                       if (mark >= 16)
-                               fprintf(fp, "mark 0x%x ", mark);
+                       if (is_json_context())
+                               print_uint(PRINT_JSON, "mark", NULL, mark);
+                       else if (mark >= 16)
+                               print_0xhex(PRINT_FP, NULL,
+                                           "mark 0x%llx ", mark);
                        else
-                               fprintf(fp, "mark %u ", mark);
+                               print_uint(PRINT_FP, NULL,
+                                          "mark %u ", mark);
                }
        }
 
@@ -757,18 +915,16 @@ int print_route(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
                print_rta_flow(fp, tb[RTA_FLOW]);
 
        if (tb[RTA_UID])
-               fprintf(fp, "uid %u ", rta_getattr_u32(tb[RTA_UID]));
+               print_uint(PRINT_ANY, "uid", "uid %u ",
+                          rta_getattr_u32(tb[RTA_UID]));
 
-       if ((r->rtm_flags & RTM_F_CLONED) && r->rtm_family == AF_INET) {
-               print_cache_flags(fp, r->rtm_flags);
+       if (r->rtm_family == AF_INET) {
+               if (r->rtm_flags & RTM_F_CLONED)
+                       print_cache_flags(fp, r->rtm_flags);
 
                if (tb[RTA_CACHEINFO])
                        print_rta_cacheinfo(fp, RTA_DATA(tb[RTA_CACHEINFO]));
-
        } else if (r->rtm_family == AF_INET6) {
-               if (r->rtm_flags & RTM_F_CLONED)
-                       fprintf(fp, "%s    cache ", _SL_);
-
                if (tb[RTA_CACHEINFO])
                        print_rta_cacheinfo(fp, RTA_DATA(tb[RTA_CACHEINFO]));
        }
@@ -779,26 +935,37 @@ int print_route(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
        if (tb[RTA_IIF] && filter.iifmask != -1)
                print_rta_if(fp, tb[RTA_IIF], "iif");
 
-       if (tb[RTA_MULTIPATH])
-               print_rta_multipath(fp, r, tb[RTA_MULTIPATH]);
-
        if (tb[RTA_PREF])
                print_rt_pref(fp, rta_getattr_u8(tb[RTA_PREF]));
 
        if (tb[RTA_TTL_PROPAGATE]) {
-               fprintf(fp, "ttl-propagate ");
-               if (rta_getattr_u8(tb[RTA_TTL_PROPAGATE]))
-                       fprintf(fp, "enabled");
+               bool propagate = rta_getattr_u8(tb[RTA_TTL_PROPAGATE]);
+
+               if (is_json_context())
+                       print_bool(PRINT_JSON, "ttl-propogate", NULL,
+                                  propagate);
                else
-                       fprintf(fp, "disabled");
+                       print_string(PRINT_FP, NULL,
+                                    "ttl-propogate %s",
+                                    propagate ? "enabled" : "disabled");
        }
-       fprintf(fp, "\n");
+
+       if (tb[RTA_MULTIPATH])
+               print_rta_multipath(fp, r, tb[RTA_MULTIPATH]);
+
+       /* If you are adding new route RTA_XXXX then place it above
+        * the RTA_MULTIPATH else it will appear that the last nexthop
+        * in the ECMP has new attributes
+        */
+
+       print_string(PRINT_FP, NULL, "\n", NULL);
+       close_json_object();
        fflush(fp);
        return 0;
 }
 
 static int parse_one_nh(struct nlmsghdr *n, struct rtmsg *r,
-                       struct rtattr *rta, struct rtnexthop *rtnh,
+                       struct rtattr *rta, size_t len, struct rtnexthop *rtnh,
                        int *argcp, char ***argvp)
 {
        int argc = *argcp;
@@ -819,18 +986,22 @@ static int parse_one_nh(struct nlmsghdr *n, struct rtmsg *r,
                        if (r->rtm_family == AF_UNSPEC)
                                r->rtm_family = addr.family;
                        if (addr.family == r->rtm_family) {
-                               rta_addattr_l(rta, 4096, RTA_GATEWAY, &addr.data, addr.bytelen);
-                               rtnh->rtnh_len += sizeof(struct rtattr) + addr.bytelen;
+                               if (rta_addattr_l(rta, len, RTA_GATEWAY,
+                                                 &addr.data, addr.bytelen))
+                                       return -1;
+                               rtnh->rtnh_len += sizeof(struct rtattr)
+                                                 + addr.bytelen;
                        } else {
-                               rta_addattr_l(rta, 4096, RTA_VIA, &addr.family, addr.bytelen+2);
-                               rtnh->rtnh_len += RTA_SPACE(addr.bytelen+2);
+                               if (rta_addattr_l(rta, len, RTA_VIA,
+                                                 &addr.family, addr.bytelen + 2))
+                                       return -1;
+                               rtnh->rtnh_len += RTA_SPACE(addr.bytelen + 2);
                        }
                } else if (strcmp(*argv, "dev") == 0) {
                        NEXT_ARG();
-                       if ((rtnh->rtnh_ifindex = ll_name_to_index(*argv)) == 0) {
-                               fprintf(stderr, "Cannot find device \"%s\"\n", *argv);
-                               return -1;
-                       }
+                       rtnh->rtnh_ifindex = ll_name_to_index(*argv);
+                       if (!rtnh->rtnh_ifindex)
+                               return nodev(*argv);
                } else if (strcmp(*argv, "weight") == 0) {
                        unsigned int w;
 
@@ -846,13 +1017,16 @@ static int parse_one_nh(struct nlmsghdr *n, struct rtmsg *r,
                        NEXT_ARG();
                        if (get_rt_realms_or_raw(&realm, *argv))
                                invarg("\"realm\" value is invalid\n", *argv);
-                       rta_addattr32(rta, 4096, RTA_FLOW, realm);
+                       if (rta_addattr32(rta, len, RTA_FLOW, realm))
+                               return -1;
                        rtnh->rtnh_len += sizeof(struct rtattr) + 4;
                } else if (strcmp(*argv, "encap") == 0) {
-                       int len = rta->rta_len;
+                       int old_len = rta->rta_len;
 
-                       lwt_parse_encap(rta, 4096, &argc, &argv);
-                       rtnh->rtnh_len += rta->rta_len - len;
+                       if (lwt_parse_encap(rta, len, &argc, &argv,
+                                           RTA_ENCAP, RTA_ENCAP_TYPE))
+                               return -1;
+                       rtnh->rtnh_len += rta->rta_len - old_len;
                } else if (strcmp(*argv, "as") == 0) {
                        inet_prefix addr;
 
@@ -860,8 +1034,9 @@ static int parse_one_nh(struct nlmsghdr *n, struct rtmsg *r,
                        if (strcmp(*argv, "to") == 0)
                                NEXT_ARG();
                        get_addr(&addr, *argv, r->rtm_family);
-                       rta_addattr_l(rta, 4096, RTA_NEWDST, &addr.data,
-                                     addr.bytelen);
+                       if (rta_addattr_l(rta, len, RTA_NEWDST,
+                                         &addr.data, addr.bytelen))
+                               return -1;
                        rtnh->rtnh_len += sizeof(struct rtattr) + addr.bytelen;
                } else
                        break;
@@ -874,7 +1049,7 @@ static int parse_one_nh(struct nlmsghdr *n, struct rtmsg *r,
 static int parse_nexthops(struct nlmsghdr *n, struct rtmsg *r,
                          int argc, char **argv)
 {
-       char buf[1024];
+       char buf[4096];
        struct rtattr *rta = (void *)buf;
        struct rtnexthop *rtnh;
 
@@ -894,12 +1069,16 @@ static int parse_nexthops(struct nlmsghdr *n, struct rtmsg *r,
                memset(rtnh, 0, sizeof(*rtnh));
                rtnh->rtnh_len = sizeof(*rtnh);
                rta->rta_len += rtnh->rtnh_len;
-               parse_one_nh(n, r, rta, rtnh, &argc, &argv);
+               if (parse_one_nh(n, r, rta, 4096, rtnh, &argc, &argv)) {
+                       fprintf(stderr, "Error: cannot parse nexthop\n");
+                       exit(-1);
+               }
                rtnh = RTNH_NEXT(rtnh);
        }
 
        if (rta->rta_len > RTA_LENGTH(0))
-               addattr_l(n, 1024, RTA_MULTIPATH, RTA_DATA(rta), RTA_PAYLOAD(rta));
+               return addattr_l(n, 4096, RTA_MULTIPATH,
+                                RTA_DATA(rta), RTA_PAYLOAD(rta));
        return 0;
 }
 
@@ -908,7 +1087,7 @@ static int iproute_modify(int cmd, unsigned int flags, int argc, char **argv)
        struct {
                struct nlmsghdr n;
                struct rtmsg            r;
-               char                    buf[1024];
+               char                    buf[4096];
        } req = {
                .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg)),
                .n.nlmsg_flags = NLM_F_REQUEST | flags,
@@ -928,6 +1107,7 @@ static int iproute_modify(int cmd, unsigned int flags, int argc, char **argv)
        int table_ok = 0;
        int raw = 0;
        int type_ok = 0;
+       __u32 nhid = 0;
 
        if (cmd != RTM_DELROUTE) {
                req.r.rtm_protocol = RTPROT_BOOT;
@@ -1206,6 +1386,11 @@ static int iproute_modify(int cmd, unsigned int flags, int argc, char **argv)
                } else if (strcmp(*argv, "nexthop") == 0) {
                        nhs_ok = 1;
                        break;
+               } else if (!strcmp(*argv, "nhid")) {
+                       NEXT_ARG();
+                       if (get_u32(&nhid, *argv, 0))
+                               invarg("\"id\" value is invalid\n", *argv);
+                       addattr32(&req.n, sizeof(req), RTA_NH_ID, nhid);
                } else if (matches(*argv, "protocol") == 0) {
                        __u32 prot;
 
@@ -1264,7 +1449,8 @@ static int iproute_modify(int cmd, unsigned int flags, int argc, char **argv)
                        rta->rta_type = RTA_ENCAP;
                        rta->rta_len = RTA_LENGTH(0);
 
-                       lwt_parse_encap(rta, sizeof(buf), &argc, &argv);
+                       lwt_parse_encap(rta, sizeof(buf), &argc, &argv,
+                                       RTA_ENCAP, RTA_ENCAP_TYPE);
 
                        if (rta->rta_len > RTA_LENGTH(0))
                                addraw_l(&req.n, 1024
@@ -1326,12 +1512,10 @@ static int iproute_modify(int cmd, unsigned int flags, int argc, char **argv)
                usage();
 
        if (d) {
-               int idx;
+               int idx = ll_name_to_index(d);
 
-               if ((idx = ll_name_to_index(d)) == 0) {
-                       fprintf(stderr, "Cannot find device \"%s\"\n", d);
-                       return -1;
-               }
+               if (!idx)
+                       return nodev(d);
                addattr32(&req.n, sizeof(req), RTA_OIF, idx);
        }
 
@@ -1341,8 +1525,8 @@ static int iproute_modify(int cmd, unsigned int flags, int argc, char **argv)
                addattr_l(&req.n, sizeof(req), RTA_METRICS, RTA_DATA(mxrta), RTA_PAYLOAD(mxrta));
        }
 
-       if (nhs_ok)
-               parse_nexthops(&req.n, &req.r, argc, argv);
+       if (nhs_ok && parse_nexthops(&req.n, &req.r, argc, argv))
+               return -1;
 
        if (req.r.rtm_family == AF_UNSPEC)
                req.r.rtm_family = AF_INET;
@@ -1369,7 +1553,7 @@ static int iproute_modify(int cmd, unsigned int flags, int argc, char **argv)
                         req.r.rtm_type == RTN_UNSPEC) {
                        if (cmd == RTM_DELROUTE)
                                req.r.rtm_scope = RT_SCOPE_NOWHERE;
-                       else if (!gw_ok && !nhs_ok)
+                       else if (!gw_ok && !nhs_ok && !nhid)
                                req.r.rtm_scope = RT_SCOPE_LINK;
                }
        }
@@ -1383,24 +1567,6 @@ static int iproute_modify(int cmd, unsigned int flags, int argc, char **argv)
        return 0;
 }
 
-static int rtnl_rtcache_request(struct rtnl_handle *rth, int family)
-{
-       struct {
-               struct nlmsghdr nlh;
-               struct rtmsg rtm;
-       } req = {
-               .nlh.nlmsg_len = sizeof(req),
-               .nlh.nlmsg_type = RTM_GETROUTE,
-               .nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_REQUEST,
-               .nlh.nlmsg_seq = rth->dump = ++rth->seq,
-               .rtm.rtm_family = family,
-               .rtm.rtm_flags = RTM_F_CLONED,
-       };
-       struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK };
-
-       return sendto(rth->fd, (void *)&req, sizeof(req), 0, (struct sockaddr *)&nladdr, sizeof(nladdr));
-}
-
 static int iproute_flush_cache(void)
 {
 #define ROUTE_FLUSH_PATH "/proc/sys/net/ipv4/route/flush"
@@ -1428,8 +1594,7 @@ static int iproute_flush_cache(void)
 
 static __u32 route_dump_magic = 0x45311224;
 
-static int save_route(const struct sockaddr_nl *who, struct nlmsghdr *n,
-                     void *arg)
+static int save_route(struct nlmsghdr *n, void *arg)
 {
        int ret;
        int len = n->nlmsg_len;
@@ -1471,14 +1636,99 @@ static int save_route_prep(void)
        return 0;
 }
 
+static int iproute_dump_filter(struct nlmsghdr *nlh, int reqlen)
+{
+       struct rtmsg *rtm = NLMSG_DATA(nlh);
+       int err;
+
+       rtm->rtm_protocol = filter.protocol;
+       if (filter.cloned)
+               rtm->rtm_flags |= RTM_F_CLONED;
+
+       if (filter.tb) {
+               err = addattr32(nlh, reqlen, RTA_TABLE, filter.tb);
+               if (err)
+                       return err;
+       }
+
+       if (filter.oif) {
+               err = addattr32(nlh, reqlen, RTA_OIF, filter.oif);
+               if (err)
+                       return err;
+       }
+
+       return 0;
+}
+
+static int iproute_flush(int family, rtnl_filter_t filter_fn)
+{
+       time_t start = time(0);
+       char flushb[4096-512];
+       int round = 0;
+       int ret;
+
+       if (filter.cloned) {
+               if (family != AF_INET6) {
+                       iproute_flush_cache();
+                       if (show_stats)
+                               printf("*** IPv4 routing cache is flushed.\n");
+               }
+               if (family == AF_INET)
+                       return 0;
+       }
+
+       filter.flushb = flushb;
+       filter.flushp = 0;
+       filter.flushe = sizeof(flushb);
+
+       for (;;) {
+               if (rtnl_routedump_req(&rth, family, iproute_dump_filter) < 0) {
+                       perror("Cannot send dump request");
+                       return -2;
+               }
+               filter.flushed = 0;
+               if (rtnl_dump_filter(&rth, filter_fn, stdout) < 0) {
+                       fprintf(stderr, "Flush terminated\n");
+                       return -2;
+               }
+               if (filter.flushed == 0) {
+                       if (show_stats) {
+                               if (round == 0 &&
+                                   (!filter.cloned || family == AF_INET6))
+                                       printf("Nothing to flush.\n");
+                               else
+                                       printf("*** Flush is complete after %d round%s ***\n",
+                                              round, round > 1 ? "s" : "");
+                       }
+                       fflush(stdout);
+                       return 0;
+               }
+               round++;
+               ret = flush_update();
+               if (ret < 0)
+                       return ret;
+
+               if (time(0) - start > 30) {
+                       printf("\n*** Flush not completed after %ld seconds, %d entries remain ***\n",
+                              (long)(time(0) - start), filter.flushed);
+                       return -1;
+               }
+
+               if (show_stats) {
+                       printf("\n*** Round %d, deleting %d entries ***\n",
+                              round, filter.flushed);
+                       fflush(stdout);
+               }
+       }
+}
+
 static int iproute_list_flush_or_save(int argc, char **argv, int action)
 {
-       int do_ipv6 = preferred_family;
+       int dump_family = preferred_family;
        char *id = NULL;
        char *od = NULL;
        unsigned int mark = 0;
        rtnl_filter_t filter_fn;
-       int ret;
 
        if (action == IPROUTE_SAVE) {
                if (save_route_prep())
@@ -1593,13 +1843,13 @@ static int iproute_list_flush_or_save(int argc, char **argv, int action)
                        NEXT_ARG();
                        family = read_family(*argv);
                        if (family == AF_UNSPEC)
-                               family = do_ipv6;
+                               family = dump_family;
                        else
                                NEXT_ARG();
                        get_prefix(&filter.rvia, *argv, family);
                } else if (strcmp(*argv, "src") == 0) {
                        NEXT_ARG();
-                       get_prefix(&filter.rprefsrc, *argv, do_ipv6);
+                       get_prefix(&filter.rprefsrc, *argv, dump_family);
                } else if (matches(*argv, "realms") == 0) {
                        __u32 realm;
 
@@ -1619,15 +1869,15 @@ static int iproute_list_flush_or_save(int argc, char **argv, int action)
                        NEXT_ARG();
                        if (matches(*argv, "root") == 0) {
                                NEXT_ARG();
-                               get_prefix(&filter.rsrc, *argv, do_ipv6);
+                               get_prefix(&filter.rsrc, *argv, dump_family);
                        } else if (matches(*argv, "match") == 0) {
                                NEXT_ARG();
-                               get_prefix(&filter.msrc, *argv, do_ipv6);
+                               get_prefix(&filter.msrc, *argv, dump_family);
                        } else {
                                if (matches(*argv, "exact") == 0) {
                                        NEXT_ARG();
                                }
-                               get_prefix(&filter.msrc, *argv, do_ipv6);
+                               get_prefix(&filter.msrc, *argv, dump_family);
                                filter.rsrc = filter.msrc;
                        }
                } else {
@@ -1636,119 +1886,61 @@ static int iproute_list_flush_or_save(int argc, char **argv, int action)
                        }
                        if (matches(*argv, "root") == 0) {
                                NEXT_ARG();
-                               get_prefix(&filter.rdst, *argv, do_ipv6);
+                               get_prefix(&filter.rdst, *argv, dump_family);
                        } else if (matches(*argv, "match") == 0) {
                                NEXT_ARG();
-                               get_prefix(&filter.mdst, *argv, do_ipv6);
+                               get_prefix(&filter.mdst, *argv, dump_family);
                        } else {
                                if (matches(*argv, "exact") == 0) {
                                        NEXT_ARG();
                                }
-                               get_prefix(&filter.mdst, *argv, do_ipv6);
+                               get_prefix(&filter.mdst, *argv, dump_family);
                                filter.rdst = filter.mdst;
                        }
                }
                argc--; argv++;
        }
 
-       if (do_ipv6 == AF_UNSPEC && filter.tb)
-               do_ipv6 = AF_INET;
+       if (dump_family == AF_UNSPEC && filter.tb)
+               dump_family = AF_INET;
 
        if (id || od)  {
                int idx;
 
                if (id) {
-                       if ((idx = ll_name_to_index(id)) == 0) {
-                               fprintf(stderr, "Cannot find device \"%s\"\n", id);
-                               return -1;
-                       }
+                       idx = ll_name_to_index(id);
+                       if (!idx)
+                               return nodev(id);
                        filter.iif = idx;
                        filter.iifmask = -1;
                }
                if (od) {
-                       if ((idx = ll_name_to_index(od)) == 0) {
-                               fprintf(stderr, "Cannot find device \"%s\"\n", od);
-                               return -1;
-                       }
+                       idx = ll_name_to_index(od);
+                       if (!idx)
+                               return nodev(od);
                        filter.oif = idx;
                        filter.oifmask = -1;
                }
        }
        filter.mark = mark;
 
-       if (action == IPROUTE_FLUSH) {
-               int round = 0;
-               char flushb[4096-512];
-               time_t start = time(0);
-
-               if (filter.cloned) {
-                       if (do_ipv6 != AF_INET6) {
-                               iproute_flush_cache();
-                               if (show_stats)
-                                       printf("*** IPv4 routing cache is flushed.\n");
-                       }
-                       if (do_ipv6 == AF_INET)
-                               return 0;
-               }
-
-               filter.flushb = flushb;
-               filter.flushp = 0;
-               filter.flushe = sizeof(flushb);
-
-               for (;;) {
-                       if (rtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE) < 0) {
-                               perror("Cannot send dump request");
-                               return -2;
-                       }
-                       filter.flushed = 0;
-                       if (rtnl_dump_filter(&rth, filter_fn, stdout) < 0) {
-                               fprintf(stderr, "Flush terminated\n");
-                               return -2;
-                       }
-                       if (filter.flushed == 0) {
-                               if (show_stats) {
-                                       if (round == 0 && (!filter.cloned || do_ipv6 == AF_INET6))
-                                               printf("Nothing to flush.\n");
-                                       else
-                                               printf("*** Flush is complete after %d round%s ***\n", round, round > 1?"s":"");
-                               }
-                               fflush(stdout);
-                               return 0;
-                       }
-                       round++;
-                       if ((ret = flush_update()) < 0)
-                               return ret;
-
-                       if (time(0) - start > 30) {
-                               printf("\n*** Flush not completed after %ld seconds, %d entries remain ***\n",
-                                      (long)(time(0) - start), filter.flushed);
-                               return -1;
-                       }
+       if (action == IPROUTE_FLUSH)
+               return iproute_flush(dump_family, filter_fn);
 
-                       if (show_stats) {
-                               printf("\n*** Round %d, deleting %d entries ***\n", round, filter.flushed);
-                               fflush(stdout);
-                       }
-               }
+       if (rtnl_routedump_req(&rth, dump_family, iproute_dump_filter) < 0) {
+               perror("Cannot send dump request");
+               return -2;
        }
 
-       if (!filter.cloned) {
-               if (rtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE) < 0) {
-                       perror("Cannot send dump request");
-                       return -2;
-               }
-       } else {
-               if (rtnl_rtcache_request(&rth, do_ipv6) < 0) {
-                       perror("Cannot send dump request");
-                       return -2;
-               }
-       }
+       new_json_obj(json);
 
        if (rtnl_dump_filter(&rth, filter_fn, stdout) < 0) {
                fprintf(stderr, "Dump terminated\n");
                return -2;
        }
 
+       delete_json_obj();
+       fflush(stdout);
        return 0;
 }
 
@@ -1772,6 +1964,7 @@ static int iproute_get(int argc, char **argv)
        int fib_match = 0;
        int from_ok = 0;
        unsigned int mark = 0;
+       bool address_found = false;
 
        iproute_reset_filter(0);
        filter.cloned = 2;
@@ -1839,6 +2032,29 @@ static int iproute_get(int argc, char **argv)
                                req.r.rtm_family = addr.family;
                        addattr_l(&req.n, sizeof(req), RTA_NEWDST,
                                  &addr.data, addr.bytelen);
+               } else if (matches(*argv, "sport") == 0) {
+                       __be16 sport;
+
+                       NEXT_ARG();
+                       if (get_be16(&sport, *argv, 0))
+                               invarg("invalid sport\n", *argv);
+                       addattr16(&req.n, sizeof(req), RTA_SPORT, sport);
+               } else if (matches(*argv, "dport") == 0) {
+                       __be16 dport;
+
+                       NEXT_ARG();
+                       if (get_be16(&dport, *argv, 0))
+                               invarg("invalid dport\n", *argv);
+                       addattr16(&req.n, sizeof(req), RTA_DPORT, dport);
+               } else if (matches(*argv, "ipproto") == 0) {
+                       int ipproto;
+
+                       NEXT_ARG();
+                       ipproto = inet_proto_a2n(*argv);
+                       if (ipproto < 0)
+                               invarg("Invalid \"ipproto\" value\n",
+                                      *argv);
+                       addattr8(&req.n, sizeof(req), RTA_IP_PROTO, ipproto);
                } else {
                        inet_prefix addr;
 
@@ -1854,11 +2070,12 @@ static int iproute_get(int argc, char **argv)
                                addattr_l(&req.n, sizeof(req),
                                          RTA_DST, &addr.data, addr.bytelen);
                        req.r.rtm_dst_len = addr.bitlen;
+                       address_found = true;
                }
                argc--; argv++;
        }
 
-       if (req.r.rtm_dst_len == 0) {
+       if (!address_found) {
                fprintf(stderr, "need at least a destination address\n");
                return -1;
        }
@@ -1867,17 +2084,15 @@ static int iproute_get(int argc, char **argv)
                int idx;
 
                if (idev) {
-                       if ((idx = ll_name_to_index(idev)) == 0) {
-                               fprintf(stderr, "Cannot find device \"%s\"\n", idev);
-                               return -1;
-                       }
+                       idx = ll_name_to_index(idev);
+                       if (!idx)
+                               return nodev(idev);
                        addattr32(&req.n, sizeof(req), RTA_IIF, idx);
                }
                if (odev) {
-                       if ((idx = ll_name_to_index(odev)) == 0) {
-                               fprintf(stderr, "Cannot find device \"%s\"\n", odev);
-                               return -1;
-                       }
+                       idx = ll_name_to_index(odev);
+                       if (!idx)
+                               return nodev(odev);
                        addattr32(&req.n, sizeof(req), RTA_OIF, idx);
                }
        }
@@ -1887,19 +2102,23 @@ static int iproute_get(int argc, char **argv)
        if (req.r.rtm_family == AF_UNSPEC)
                req.r.rtm_family = AF_INET;
 
-       req.r.rtm_flags |= RTM_F_LOOKUP_TABLE;
+       /* Only IPv4 supports the RTM_F_LOOKUP_TABLE flag */
+       if (req.r.rtm_family == AF_INET)
+               req.r.rtm_flags |= RTM_F_LOOKUP_TABLE;
        if (fib_match)
                req.r.rtm_flags |= RTM_F_FIB_MATCH;
 
        if (rtnl_talk(&rth, &req.n, &answer) < 0)
                return -2;
 
+       new_json_obj(json);
+
        if (connected && !from_ok) {
                struct rtmsg *r = NLMSG_DATA(answer);
                int len = answer->nlmsg_len;
                struct rtattr *tb[RTA_MAX+1];
 
-               if (print_route(NULL, answer, (void *)stdout) < 0) {
+               if (print_route(answer, (void *)stdout) < 0) {
                        fprintf(stderr, "An error :-)\n");
                        free(answer);
                        return -1;
@@ -1938,17 +2157,19 @@ static int iproute_get(int argc, char **argv)
                req.n.nlmsg_flags = NLM_F_REQUEST;
                req.n.nlmsg_type = RTM_GETROUTE;
 
+               delete_json_obj();
                free(answer);
                if (rtnl_talk(&rth, &req.n, &answer) < 0)
                        return -2;
        }
 
-       if (print_route(NULL, answer, (void *)stdout) < 0) {
+       if (print_route(answer, (void *)stdout) < 0) {
                fprintf(stderr, "An error :-)\n");
                free(answer);
                return -1;
        }
 
+       delete_json_obj();
        free(answer);
        return 0;
 }
@@ -1961,8 +2182,7 @@ static int rtattr_cmp(const struct rtattr *rta1, const struct rtattr *rta2)
        return memcmp(RTA_DATA(rta1), RTA_DATA(rta2), RTA_PAYLOAD(rta1));
 }
 
-static int restore_handler(const struct sockaddr_nl *nl,
-                          struct rtnl_ctrl_data *ctrl,
+static int restore_handler(struct rtnl_ctrl_data *ctrl,
                           struct nlmsghdr *n, void *arg)
 {
        struct rtmsg *r = NLMSG_DATA(n);
@@ -2048,11 +2268,10 @@ static int iproute_restore(void)
        return 0;
 }
 
-static int show_handler(const struct sockaddr_nl *nl,
-                       struct rtnl_ctrl_data *ctrl,
+static int show_handler(struct rtnl_ctrl_data *ctrl,
                        struct nlmsghdr *n, void *arg)
 {
-       print_route(nl, n, stdout);
+       print_route(n, stdout);
        return 0;
 }