]> git.proxmox.com Git - mirror_iproute2.git/blobdiff - ip/ipaddress.c
Merge branch 'master' into next
[mirror_iproute2.git] / ip / ipaddress.c
index a495a391a0ecdb1bffe648cc816a10b91dedface..bc8f5ba13c33a2b26469b8dd6b374e6101dd3e56 100644 (file)
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
-#include <syslog.h>
 #include <inttypes.h>
 #include <fcntl.h>
 #include <sys/ioctl.h>
 #include <sys/socket.h>
-#include <sys/ioctl.h>
+#include <sys/param.h>
 #include <errno.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
 
 #include <linux/netdevice.h>
 #include <linux/if_arp.h>
+#include <linux/if_infiniband.h>
 #include <linux/sockios.h>
 #include <linux/net_namespace.h>
 
+#include "utils.h"
 #include "rt_names.h"
 #include "utils.h"
 #include "ll_map.h"
@@ -42,66 +43,57 @@ enum {
        IPADD_SAVE,
 };
 
-static struct
-{
-       int ifindex;
-       int family;
-       int oneline;
-       int showqueue;
-       inet_prefix pfx;
-       int scope, scopemask;
-       int flags, flagmask;
-       int up;
-       char *label;
-       int flushed;
-       char *flushb;
-       int flushp;
-       int flushe;
-       int group;
-       int master;
-       char *kind;
-} filter;
-
+static struct link_filter filter;
 static int do_link;
 
 static void usage(void) __attribute__((noreturn));
 
 static void usage(void)
 {
-       if (do_link) {
+       if (do_link)
                iplink_usage();
-       }
-       fprintf(stderr, "Usage: ip address {add|change|replace} IFADDR dev IFNAME [ LIFETIME ]\n");
-       fprintf(stderr, "                                                      [ CONFFLAG-LIST ]\n");
-       fprintf(stderr, "       ip address del IFADDR dev IFNAME [mngtmpaddr]\n");
-       fprintf(stderr, "       ip address {show|save|flush} [ dev IFNAME ] [ scope SCOPE-ID ]\n");
-       fprintf(stderr, "                            [ to PREFIX ] [ FLAG-LIST ] [ label LABEL ] [up]\n");
-       fprintf(stderr, "       ip address {showdump|restore}\n");
-       fprintf(stderr, "IFADDR := PREFIX | ADDR peer PREFIX\n");
-       fprintf(stderr, "          [ broadcast ADDR ] [ anycast ADDR ]\n");
-       fprintf(stderr, "          [ label IFNAME ] [ scope SCOPE-ID ]\n");
-       fprintf(stderr, "SCOPE-ID := [ host | link | global | NUMBER ]\n");
-       fprintf(stderr, "FLAG-LIST := [ FLAG-LIST ] FLAG\n");
-       fprintf(stderr, "FLAG  := [ permanent | dynamic | secondary | primary |\n");
-       fprintf(stderr, "           [-]tentative | [-]deprecated | [-]dadfailed | temporary |\n");
-       fprintf(stderr, "           CONFFLAG-LIST ]\n");
-       fprintf(stderr, "CONFFLAG-LIST := [ CONFFLAG-LIST ] CONFFLAG\n");
-       fprintf(stderr, "CONFFLAG  := [ home | nodad | mngtmpaddr | noprefixroute | autojoin ]\n");
-       fprintf(stderr, "LIFETIME := [ valid_lft LFT ] [ preferred_lft LFT ]\n");
-       fprintf(stderr, "LFT := forever | SECONDS\n");
+
+       fprintf(stderr,
+               "Usage: ip address {add|change|replace} IFADDR dev IFNAME [ LIFETIME ]\n"
+               "                                                      [ CONFFLAG-LIST ]\n"
+               "       ip address del IFADDR dev IFNAME [mngtmpaddr]\n"
+               "       ip address {save|flush} [ dev IFNAME ] [ scope SCOPE-ID ]\n"
+               "                            [ to PREFIX ] [ FLAG-LIST ] [ label LABEL ] [up]\n"
+               "       ip address [ show [ dev IFNAME ] [ scope SCOPE-ID ] [ master DEVICE ]\n"
+               "                         [ type TYPE ] [ to PREFIX ] [ FLAG-LIST ]\n"
+               "                         [ label LABEL ] [up] [ vrf NAME ] ]\n"
+               "       ip address {showdump|restore}\n"
+               "IFADDR := PREFIX | ADDR peer PREFIX\n"
+               "          [ broadcast ADDR ] [ anycast ADDR ]\n"
+               "          [ label IFNAME ] [ scope SCOPE-ID ] [ metric METRIC ]\n"
+               "SCOPE-ID := [ host | link | global | NUMBER ]\n"
+               "FLAG-LIST := [ FLAG-LIST ] FLAG\n"
+               "FLAG  := [ permanent | dynamic | secondary | primary |\n"
+               "           [-]tentative | [-]deprecated | [-]dadfailed | temporary |\n"
+               "           CONFFLAG-LIST ]\n"
+               "CONFFLAG-LIST := [ CONFFLAG-LIST ] CONFFLAG\n"
+               "CONFFLAG  := [ home | nodad | mngtmpaddr | noprefixroute | autojoin ]\n"
+               "LIFETIME := [ valid_lft LFT ] [ preferred_lft LFT ]\n"
+               "LFT := forever | SECONDS\n"
+               "TYPE := { vlan | veth | vcan | vxcan | dummy | ifb | macvlan | macvtap |\n"
+               "          bridge | bond | ipoib | ip6tnl | ipip | sit | vxlan | lowpan |\n"
+               "          gre | gretap | erspan | ip6gre | ip6gretap | ip6erspan | vti |\n"
+               "          nlmon | can | bond_slave | ipvlan | geneve | bridge_slave |\n"
+               "          hsr | macsec | netdevsim }\n");
 
        exit(-1);
 }
 
-static void print_link_flags(FILE *fp, unsigned flags, unsigned mdown)
+static void print_link_flags(FILE *fp, unsigned int flags, unsigned int mdown)
 {
-       fprintf(fp, "<");
+       open_json_array(PRINT_ANY, is_json_context() ? "flags" : "<");
        if (flags & IFF_UP && !(flags & IFF_RUNNING))
-               fprintf(fp, "NO-CARRIER%s", flags ? "," : "");
+               print_string(PRINT_ANY, NULL,
+                            flags ? "%s," : "%s", "NO-CARRIER");
        flags &= ~IFF_RUNNING;
-#define _PF(f) if (flags&IFF_##f) { \
-                 flags &= ~IFF_##f ; \
-                 fprintf(fp, #f "%s", flags ? "," : ""); }
+#define _PF(f) if (flags&IFF_##f) {                                    \
+               flags &= ~IFF_##f ;                                     \
+               print_string(PRINT_ANY, NULL, flags ? "%s," : "%s", #f); }
        _PF(LOOPBACK);
        _PF(BROADCAST);
        _PF(POINTOPOINT);
@@ -122,10 +114,10 @@ static void print_link_flags(FILE *fp, unsigned flags, unsigned mdown)
        _PF(ECHO);
 #undef _PF
        if (flags)
-               fprintf(fp, "%x", flags);
+               print_hex(PRINT_ANY, NULL, "%x", flags);
        if (mdown)
-               fprintf(fp, ",M-DOWN");
-       fprintf(fp, "> ");
+               print_string(PRINT_ANY, NULL, ",%s", "M-DOWN");
+       close_json_array(PRINT_ANY, "> ");
 }
 
 static const char *oper_states[] = {
@@ -135,24 +127,26 @@ static const char *oper_states[] = {
 
 static void print_operstate(FILE *f, __u8 state)
 {
-       if (state >= sizeof(oper_states)/sizeof(oper_states[0]))
-               fprintf(f, "state %#x ", state);
-       else {
-               if (brief) {
-                       if (strcmp(oper_states[state], "UP") == 0)
-                               color_fprintf(f, COLOR_OPERSTATE_UP, "%-14s ", oper_states[state]);
-                       else if (strcmp(oper_states[state], "DOWN") == 0)
-                               color_fprintf(f, COLOR_OPERSTATE_DOWN, "%-14s ", oper_states[state]);
-                       else
-                               fprintf(f, "%-14s ", oper_states[state]);
-               } else {
+       if (state >= ARRAY_SIZE(oper_states)) {
+               if (is_json_context())
+                       print_uint(PRINT_JSON, "operstate_index", NULL, state);
+               else
+                       print_0xhex(PRINT_FP, NULL, "state %#llx", state);
+       } else if (brief) {
+               print_color_string(PRINT_ANY,
+                                  oper_state_color(state),
+                                  "operstate",
+                                  "%-14s ",
+                                  oper_states[state]);
+       } else {
+               if (is_json_context())
+                       print_string(PRINT_JSON,
+                                    "operstate",
+                                    NULL, oper_states[state]);
+               else {
                        fprintf(f, "state ");
-                       if (strcmp(oper_states[state], "UP") == 0)
-                               color_fprintf(f, COLOR_OPERSTATE_UP, "%s ", oper_states[state]);
-                       else if (strcmp(oper_states[state], "DOWN") == 0)
-                               color_fprintf(f, COLOR_OPERSTATE_DOWN, "%s ", oper_states[state]);
-                       else
-                               fprintf(f, "%s ", oper_states[state]);
+                       color_fprintf(f, oper_state_color(state),
+                                     "%s ", oper_states[state]);
                }
        }
 }
@@ -161,7 +155,7 @@ int get_operstate(const char *name)
 {
        int i;
 
-       for (i = 0; i < sizeof(oper_states)/sizeof(oper_states[0]); i++)
+       for (i = 0; i < ARRAY_SIZE(oper_states); i++)
                if (strcasecmp(name, oper_states[i]) == 0)
                        return i;
        return -1;
@@ -172,18 +166,19 @@ static void print_queuelen(FILE *f, struct rtattr *tb[IFLA_MAX + 1])
        int qlen;
 
        if (tb[IFLA_TXQLEN])
-               qlen = *(int *)RTA_DATA(tb[IFLA_TXQLEN]);
+               qlen = rta_getattr_u32(tb[IFLA_TXQLEN]);
        else {
-               struct ifreq ifr;
+               struct ifreq ifr = {};
                int s = socket(AF_INET, SOCK_STREAM, 0);
 
                if (s < 0)
                        return;
 
-               memset(&ifr, 0, sizeof(ifr));
                strcpy(ifr.ifr_name, rta_getattr_str(tb[IFLA_IFNAME]));
                if (ioctl(s, SIOCGIFTXQLEN, &ifr) < 0) {
-                       fprintf(f, "ioctl(SIOCGIFTXQLEN) failed: %s\n", strerror(errno));
+                       fprintf(stderr,
+                               "ioctl(SIOCGIFTXQLEN) failed: %s\n",
+                               strerror(errno));
                        close(s);
                        return;
                }
@@ -191,7 +186,7 @@ static void print_queuelen(FILE *f, struct rtattr *tb[IFLA_MAX + 1])
                qlen = ifr.ifr_qlen;
        }
        if (qlen)
-               fprintf(f, "qlen %d", qlen);
+               print_int(PRINT_ANY, "txqlen", "qlen %d", qlen);
 }
 
 static const char *link_modes[] = {
@@ -202,39 +197,55 @@ static void print_linkmode(FILE *f, struct rtattr *tb)
 {
        unsigned int mode = rta_getattr_u8(tb);
 
-       if (mode >= sizeof(link_modes) / sizeof(link_modes[0]))
-               fprintf(f, "mode %d ", mode);
+       if (mode >= ARRAY_SIZE(link_modes))
+               print_int(PRINT_ANY,
+                         "linkmode_index",
+                         "mode %d ",
+                         mode);
        else
-               fprintf(f, "mode %s ", link_modes[mode]);
+               print_string(PRINT_ANY,
+                            "linkmode",
+                            "mode %s "
+                            , link_modes[mode]);
 }
 
-static char *parse_link_kind(struct rtattr *tb)
+static char *parse_link_kind(struct rtattr *tb, bool slave)
 {
        struct rtattr *linkinfo[IFLA_INFO_MAX+1];
+       int attr = slave ? IFLA_INFO_SLAVE_KIND : IFLA_INFO_KIND;
 
        parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb);
 
-       if (linkinfo[IFLA_INFO_KIND])
-               return RTA_DATA(linkinfo[IFLA_INFO_KIND]);
+       if (linkinfo[attr])
+               return RTA_DATA(linkinfo[attr]);
 
        return "";
 }
 
+static int match_link_kind(struct rtattr **tb, const char *kind, bool slave)
+{
+       if (!tb[IFLA_LINKINFO])
+               return -1;
+
+       return strcmp(parse_link_kind(tb[IFLA_LINKINFO], slave), kind);
+}
+
 static void print_linktype(FILE *fp, struct rtattr *tb)
 {
        struct rtattr *linkinfo[IFLA_INFO_MAX+1];
        struct link_util *lu;
        struct link_util *slave_lu;
-       char *kind;
-       char *slave_kind;
+       char slave[32];
 
        parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb);
+       open_json_object("linkinfo");
 
        if (linkinfo[IFLA_INFO_KIND]) {
-               kind = RTA_DATA(linkinfo[IFLA_INFO_KIND]);
+               const char *kind
+                       = rta_getattr_str(linkinfo[IFLA_INFO_KIND]);
 
-               fprintf(fp, "%s", _SL_);
-               fprintf(fp, "    %s ", kind);
+               print_nl();
+               print_string(PRINT_ANY, "info_kind", "    %s ", kind);
 
                lu = get_link_kind(kind);
                if (lu && lu->print_opt) {
@@ -245,21 +256,32 @@ static void print_linktype(FILE *fp, struct rtattr *tb)
                                                    linkinfo[IFLA_INFO_DATA]);
                                data = attr;
                        }
+                       open_json_object("info_data");
                        lu->print_opt(lu, fp, data);
+                       close_json_object();
 
                        if (linkinfo[IFLA_INFO_XSTATS] && show_stats &&
-                           lu->print_xstats)
+                           lu->print_xstats) {
+                               open_json_object("info_xstats");
                                lu->print_xstats(lu, fp, linkinfo[IFLA_INFO_XSTATS]);
+                               close_json_object();
+                       }
                }
        }
 
        if (linkinfo[IFLA_INFO_SLAVE_KIND]) {
-               slave_kind = RTA_DATA(linkinfo[IFLA_INFO_SLAVE_KIND]);
+               const char *slave_kind
+                       = rta_getattr_str(linkinfo[IFLA_INFO_SLAVE_KIND]);
 
-               fprintf(fp, "%s", _SL_);
-               fprintf(fp, "    %s_slave ", slave_kind);
+               print_nl();
+               print_string(PRINT_ANY,
+                            "info_slave_kind",
+                            "    %s_slave ",
+                            slave_kind);
+
+               snprintf(slave, sizeof(slave), "%s_slave", slave_kind);
 
-               slave_lu = get_link_slave_kind(slave_kind);
+               slave_lu = get_link_kind(slave);
                if (slave_lu && slave_lu->print_opt) {
                        struct rtattr *attr[slave_lu->maxattr+1], **data = NULL;
 
@@ -268,9 +290,12 @@ static void print_linktype(FILE *fp, struct rtattr *tb)
                                                    linkinfo[IFLA_INFO_SLAVE_DATA]);
                                data = attr;
                        }
+                       open_json_object("info_slave_data");
                        slave_lu->print_opt(slave_lu, fp, data);
+                       close_json_object();
                }
        }
+       close_json_object();
 }
 
 static void print_af_spec(FILE *fp, struct rtattr *af_spec_attr)
@@ -286,18 +311,39 @@ static void print_af_spec(FILE *fp, struct rtattr *af_spec_attr)
 
        if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
                __u8 mode = rta_getattr_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
+               SPRINT_BUF(b1);
+
                switch (mode) {
                case IN6_ADDR_GEN_MODE_EUI64:
-                       fprintf(fp, "addrgenmode eui64 ");
+                       print_string(PRINT_ANY,
+                                    "inet6_addr_gen_mode",
+                                    "addrgenmode %s ",
+                                    "eui64");
                        break;
                case IN6_ADDR_GEN_MODE_NONE:
-                       fprintf(fp, "addrgenmode none ");
+                       print_string(PRINT_ANY,
+                                    "inet6_addr_gen_mode",
+                                    "addrgenmode %s ",
+                                    "none");
                        break;
                case IN6_ADDR_GEN_MODE_STABLE_PRIVACY:
-                       fprintf(fp, "addrgenmode stable_secret ");
+                       print_string(PRINT_ANY,
+                                    "inet6_addr_gen_mode",
+                                    "addrgenmode %s ",
+                                    "stable_secret");
+                       break;
+               case IN6_ADDR_GEN_MODE_RANDOM:
+                       print_string(PRINT_ANY,
+                                    "inet6_addr_gen_mode",
+                                    "addrgenmode %s ",
+                                    "random");
                        break;
                default:
-                       fprintf(fp, "addrgenmode %#.2hhx ", mode);
+                       snprintf(b1, sizeof(b1), "%#.2hhx", mode);
+                       print_string(PRINT_ANY,
+                                    "inet6_addr_gen_mode",
+                                    "addrgenmode %s ",
+                                    b1);
                        break;
                }
        }
@@ -305,15 +351,13 @@ static void print_af_spec(FILE *fp, struct rtattr *af_spec_attr)
 
 static void print_vf_stats64(FILE *fp, struct rtattr *vfstats);
 
-static void print_vfinfo(FILE *fp, struct rtattr *vfinfo)
+static void print_vfinfo(FILE *fp, struct ifinfomsg *ifi, struct rtattr *vfinfo)
 {
        struct ifla_vf_mac *vf_mac;
-       struct ifla_vf_vlan *vf_vlan;
+       struct ifla_vf_broadcast *vf_broadcast;
        struct ifla_vf_tx_rate *vf_tx_rate;
-       struct ifla_vf_spoofchk *vf_spoofchk;
-       struct ifla_vf_link_state *vf_linkstate;
        struct rtattr *vf[IFLA_VF_MAX + 1] = {};
-       struct rtattr *tmp;
+
        SPRINT_BUF(b1);
 
        if (vfinfo->rta_type != IFLA_VF_INFO) {
@@ -324,72 +368,170 @@ static void print_vfinfo(FILE *fp, struct rtattr *vfinfo)
        parse_rtattr_nested(vf, IFLA_VF_MAX, vfinfo);
 
        vf_mac = RTA_DATA(vf[IFLA_VF_MAC]);
-       vf_vlan = RTA_DATA(vf[IFLA_VF_VLAN]);
+       vf_broadcast = RTA_DATA(vf[IFLA_VF_BROADCAST]);
        vf_tx_rate = RTA_DATA(vf[IFLA_VF_TX_RATE]);
 
-       /* Check if the spoof checking vf info type is supported by
-        * this kernel.
-        */
-       tmp = (struct rtattr *)((char *)vf[IFLA_VF_TX_RATE] +
-                       vf[IFLA_VF_TX_RATE]->rta_len);
-
-       if (tmp->rta_type != IFLA_VF_SPOOFCHK)
-               vf_spoofchk = NULL;
-       else
-               vf_spoofchk = RTA_DATA(vf[IFLA_VF_SPOOFCHK]);
+       print_string(PRINT_FP, NULL, "%s    ", _SL_);
+       print_int(PRINT_ANY, "vf", "vf %d ", vf_mac->vf);
+
+       print_string(PRINT_ANY,
+                    "link_type",
+                    "    link/%s ",
+                    ll_type_n2a(ifi->ifi_type, b1, sizeof(b1)));
+
+       print_color_string(PRINT_ANY, COLOR_MAC,
+                          "address", "%s",
+                          ll_addr_n2a((unsigned char *) &vf_mac->mac,
+                                      ifi->ifi_type == ARPHRD_ETHER ?
+                                      ETH_ALEN : INFINIBAND_ALEN,
+                                      ifi->ifi_type,
+                                      b1, sizeof(b1)));
+
+       if (vf[IFLA_VF_BROADCAST]) {
+               if (ifi->ifi_flags&IFF_POINTOPOINT) {
+                       print_string(PRINT_FP, NULL, " peer ", NULL);
+                       print_bool(PRINT_JSON,
+                                  "link_pointtopoint", NULL, true);
+               } else
+                       print_string(PRINT_FP, NULL, " brd ", NULL);
+
+               print_color_string(PRINT_ANY, COLOR_MAC,
+                                  "broadcast", "%s",
+                                  ll_addr_n2a((unsigned char *) &vf_broadcast->broadcast,
+                                              ifi->ifi_type == ARPHRD_ETHER ?
+                                              ETH_ALEN : INFINIBAND_ALEN,
+                                              ifi->ifi_type,
+                                              b1, sizeof(b1)));
+       }
+
+       if (vf[IFLA_VF_VLAN_LIST]) {
+               struct rtattr *i, *vfvlanlist = vf[IFLA_VF_VLAN_LIST];
+               int rem = RTA_PAYLOAD(vfvlanlist);
+
+               open_json_array(PRINT_JSON, "vlan_list");
+               for (i = RTA_DATA(vfvlanlist);
+                    RTA_OK(i, rem); i = RTA_NEXT(i, rem)) {
+                       struct ifla_vf_vlan_info *vf_vlan_info = RTA_DATA(i);
+                       SPRINT_BUF(b2);
+
+                       open_json_object(NULL);
+                       if (vf_vlan_info->vlan)
+                               print_int(PRINT_ANY,
+                                         "vlan",
+                                         ", vlan %d",
+                                         vf_vlan_info->vlan);
+                       if (vf_vlan_info->qos)
+                               print_int(PRINT_ANY,
+                                         "qos",
+                                         ", qos %d",
+                                         vf_vlan_info->qos);
+                       if (vf_vlan_info->vlan_proto &&
+                           vf_vlan_info->vlan_proto != htons(ETH_P_8021Q))
+                               print_string(PRINT_ANY,
+                                            "protocol",
+                                            ", vlan protocol %s",
+                                            ll_proto_n2a(
+                                                    vf_vlan_info->vlan_proto,
+                                                    b2, sizeof(b2)));
+                       close_json_object();
+               }
+               close_json_array(PRINT_JSON, NULL);
+       } else {
+               struct ifla_vf_vlan *vf_vlan = RTA_DATA(vf[IFLA_VF_VLAN]);
 
-       if (vf_spoofchk) {
-               /* Check if the link state vf info type is supported by
-                * this kernel.
-                */
-               tmp = (struct rtattr *)((char *)vf[IFLA_VF_SPOOFCHK] +
-                               vf[IFLA_VF_SPOOFCHK]->rta_len);
+               if (vf_vlan->vlan)
+                       print_int(PRINT_ANY,
+                                 "vlan",
+                                 ", vlan %d",
+                                 vf_vlan->vlan);
+               if (vf_vlan->qos)
+                       print_int(PRINT_ANY, "qos", ", qos %d", vf_vlan->qos);
+       }
 
-               if (tmp->rta_type != IFLA_VF_LINK_STATE)
-                       vf_linkstate = NULL;
-               else
-                       vf_linkstate = RTA_DATA(vf[IFLA_VF_LINK_STATE]);
-       } else
-               vf_linkstate = NULL;
-
-       fprintf(fp, "%s    vf %d MAC %s", _SL_, vf_mac->vf,
-               ll_addr_n2a((unsigned char *)&vf_mac->mac,
-               ETH_ALEN, 0, b1, sizeof(b1)));
-       if (vf_vlan->vlan)
-               fprintf(fp, ", vlan %d", vf_vlan->vlan);
-       if (vf_vlan->qos)
-               fprintf(fp, ", qos %d", vf_vlan->qos);
        if (vf_tx_rate->rate)
-               fprintf(fp, ", tx rate %d (Mbps)", vf_tx_rate->rate);
+               print_uint(PRINT_ANY,
+                          "tx_rate",
+                          ", tx rate %u (Mbps)",
+                          vf_tx_rate->rate);
 
        if (vf[IFLA_VF_RATE]) {
                struct ifla_vf_rate *vf_rate = RTA_DATA(vf[IFLA_VF_RATE]);
-
-               if (vf_rate->max_tx_rate)
-                       fprintf(fp, ", max_tx_rate %dMbps", vf_rate->max_tx_rate);
-               if (vf_rate->min_tx_rate)
-                       fprintf(fp, ", min_tx_rate %dMbps", vf_rate->min_tx_rate);
+               int max_tx = vf_rate->max_tx_rate;
+               int min_tx = vf_rate->min_tx_rate;
+
+               if (is_json_context()) {
+                       open_json_object("rate");
+                       print_uint(PRINT_JSON, "max_tx", NULL, max_tx);
+                       print_uint(PRINT_ANY, "min_tx", NULL, min_tx);
+                       close_json_object();
+               } else {
+                       if (max_tx)
+                               fprintf(fp, ", max_tx_rate %uMbps", max_tx);
+                       if (min_tx)
+                               fprintf(fp, ", min_tx_rate %uMbps", min_tx);
+               }
        }
 
-       if (vf_spoofchk && vf_spoofchk->setting != -1) {
-               if (vf_spoofchk->setting)
-                       fprintf(fp, ", spoof checking on");
-               else
-                       fprintf(fp, ", spoof checking off");
+       if (vf[IFLA_VF_SPOOFCHK]) {
+               struct ifla_vf_spoofchk *vf_spoofchk =
+                       RTA_DATA(vf[IFLA_VF_SPOOFCHK]);
+
+               if (vf_spoofchk->setting != -1)
+                       print_bool(PRINT_ANY,
+                                  "spoofchk",
+                                  vf_spoofchk->setting ?
+                                  ", spoof checking on" : ", spoof checking off",
+                                  vf_spoofchk->setting);
        }
-       if (vf_linkstate) {
+
+       if (vf[IFLA_VF_LINK_STATE]) {
+               struct ifla_vf_link_state *vf_linkstate =
+                       RTA_DATA(vf[IFLA_VF_LINK_STATE]);
+
                if (vf_linkstate->link_state == IFLA_VF_LINK_STATE_AUTO)
-                       fprintf(fp, ", link-state auto");
+                       print_string(PRINT_ANY,
+                                    "link_state",
+                                    ", link-state %s",
+                                    "auto");
                else if (vf_linkstate->link_state == IFLA_VF_LINK_STATE_ENABLE)
-                       fprintf(fp, ", link-state enable");
+                       print_string(PRINT_ANY,
+                                    "link_state",
+                                    ", link-state %s",
+                                    "enable");
                else
-                       fprintf(fp, ", link-state disable");
+                       print_string(PRINT_ANY,
+                                    "link_state",
+                                    ", link-state %s",
+                                    "disable");
+       }
+
+       if (vf[IFLA_VF_TRUST]) {
+               struct ifla_vf_trust *vf_trust = RTA_DATA(vf[IFLA_VF_TRUST]);
+
+               if (vf_trust->setting != -1)
+                       print_bool(PRINT_ANY,
+                                  "trust",
+                                  vf_trust->setting ? ", trust on" : ", trust off",
+                                  vf_trust->setting);
+       }
+
+       if (vf[IFLA_VF_RSS_QUERY_EN]) {
+               struct ifla_vf_rss_query_en *rss_query =
+                       RTA_DATA(vf[IFLA_VF_RSS_QUERY_EN]);
+
+               if (rss_query->setting != -1)
+                       print_bool(PRINT_ANY,
+                                  "query_rss_en",
+                                  rss_query->setting ? ", query_rss on"
+                                  : ", query_rss off",
+                                  rss_query->setting);
        }
+
        if (vf[IFLA_VF_STATS] && show_stats)
                print_vf_stats64(fp, vf[IFLA_VF_STATS]);
 }
 
-static void print_num(FILE *fp, unsigned width, uint64_t count)
+void print_num(FILE *fp, unsigned int width, uint64_t count)
 {
        const char *prefix = "kMGTPE";
        const unsigned int base = use_iec ? 1024 : 1000;
@@ -404,8 +546,9 @@ static void print_num(FILE *fp, unsigned width, uint64_t count)
        }
 
        /* increase value by a factor of 1000/1024 and print
-        * if result is something a human can read */
-       for(;;) {
+        * if result is something a human can read
+        */
+       for (;;) {
                powi *= base;
                if (count / base < powi)
                        break;
@@ -423,298 +566,306 @@ static void print_num(FILE *fp, unsigned width, uint64_t count)
        }
 
        snprintf(buf, sizeof(buf), "%.*f%c%s", precision,
-               (double) count / powi, *prefix, use_iec ? "i" : "");
+                (double) count / powi, *prefix, use_iec ? "i" : "");
 
        fprintf(fp, "%-*s ", width, buf);
 }
 
 static void print_vf_stats64(FILE *fp, struct rtattr *vfstats)
 {
-       struct rtattr *vf[IFLA_VF_STATS_MAX + 1] = {};
+       struct rtattr *vf[IFLA_VF_STATS_MAX + 1];
 
        if (vfstats->rta_type != IFLA_VF_STATS) {
                fprintf(stderr, "BUG: rta type is %d\n", vfstats->rta_type);
                return;
        }
 
-       parse_rtattr_nested(vf, IFLA_VF_MAX, vfstats);
-
-       /* RX stats */
-       fprintf(fp, "%s", _SL_);
-       fprintf(fp, "    RX: bytes  packets  mcast   bcast %s", _SL_);
-       fprintf(fp, "    ");
-
-       print_num(fp, 10, *(__u64 *)RTA_DATA(vf[IFLA_VF_STATS_RX_BYTES]));
-       print_num(fp, 8, *(__u64 *)RTA_DATA(vf[IFLA_VF_STATS_RX_PACKETS]));
-       print_num(fp, 7, *(__u64 *)RTA_DATA(vf[IFLA_VF_STATS_MULTICAST]));
-       print_num(fp, 7, *(__u64 *)RTA_DATA(vf[IFLA_VF_STATS_BROADCAST]));
-
-       /* TX stats */
-       fprintf(fp, "%s", _SL_);
-       fprintf(fp, "    TX: bytes  packets %s", _SL_);
-       fprintf(fp, "    ");
+       parse_rtattr_nested(vf, IFLA_VF_STATS_MAX, vfstats);
+
+       if (is_json_context()) {
+               open_json_object("stats");
+
+               /* RX stats */
+               open_json_object("rx");
+               print_u64(PRINT_JSON, "bytes", NULL,
+                          rta_getattr_u64(vf[IFLA_VF_STATS_RX_BYTES]));
+               print_u64(PRINT_JSON, "packets", NULL,
+                          rta_getattr_u64(vf[IFLA_VF_STATS_RX_PACKETS]));
+               print_u64(PRINT_JSON, "multicast", NULL,
+                          rta_getattr_u64(vf[IFLA_VF_STATS_MULTICAST]));
+               print_u64(PRINT_JSON, "broadcast", NULL,
+                          rta_getattr_u64(vf[IFLA_VF_STATS_BROADCAST]));
+               if (vf[IFLA_VF_STATS_RX_DROPPED])
+                       print_u64(PRINT_JSON, "dropped", NULL,
+                                 rta_getattr_u64(vf[IFLA_VF_STATS_RX_DROPPED]));
+               close_json_object();
+
+               /* TX stats */
+               open_json_object("tx");
+               print_u64(PRINT_JSON, "tx_bytes", NULL,
+                          rta_getattr_u64(vf[IFLA_VF_STATS_TX_BYTES]));
+               print_u64(PRINT_JSON, "tx_packets", NULL,
+                          rta_getattr_u64(vf[IFLA_VF_STATS_TX_PACKETS]));
+               if (vf[IFLA_VF_STATS_TX_DROPPED])
+                       print_u64(PRINT_JSON, "dropped", NULL,
+                                 rta_getattr_u64(vf[IFLA_VF_STATS_TX_DROPPED]));
+               close_json_object();
+               close_json_object();
+       } else {
+               /* RX stats */
+               fprintf(fp, "%s", _SL_);
+               fprintf(fp, "    RX: bytes  packets  mcast   bcast ");
+               if (vf[IFLA_VF_STATS_RX_DROPPED])
+                       fprintf(fp, "  dropped ");
+               fprintf(fp, "%s", _SL_);
+               fprintf(fp, "    ");
 
-       print_num(fp, 10, *(__u64 *)RTA_DATA(vf[IFLA_VF_STATS_TX_BYTES]));
-       print_num(fp, 8, *(__u64 *)RTA_DATA(vf[IFLA_VF_STATS_TX_PACKETS]));
-}
+               print_num(fp, 10, rta_getattr_u64(vf[IFLA_VF_STATS_RX_BYTES]));
+               print_num(fp, 8, rta_getattr_u64(vf[IFLA_VF_STATS_RX_PACKETS]));
+               print_num(fp, 7, rta_getattr_u64(vf[IFLA_VF_STATS_MULTICAST]));
+               print_num(fp, 7, rta_getattr_u64(vf[IFLA_VF_STATS_BROADCAST]));
+               if (vf[IFLA_VF_STATS_RX_DROPPED])
+                       print_num(fp, 8, rta_getattr_u64(vf[IFLA_VF_STATS_RX_DROPPED]));
 
-static void print_link_stats64(FILE *fp, const struct rtnl_link_stats64 *s,
-                               const struct rtattr *carrier_changes)
-{
-       /* RX stats */
-       fprintf(fp, "    RX: bytes  packets  errors  dropped overrun mcast   %s%s",
-               s->rx_compressed ? "compressed" : "", _SL_);
-
-       fprintf(fp, "    ");
-       print_num(fp, 10, s->rx_bytes);
-       print_num(fp, 8, s->rx_packets);
-       print_num(fp, 7, s->rx_errors);
-       print_num(fp, 7, s->rx_dropped);
-       print_num(fp, 7, s->rx_over_errors);
-       print_num(fp, 7, s->multicast);
-       if (s->rx_compressed)
-               print_num(fp, 7, s->rx_compressed);
-
-       /* RX error stats */
-       if (show_stats > 1) {
-               fprintf(fp, "%s", _SL_);
-               fprintf(fp, "    RX errors: length   crc     frame   fifo    missed%s", _SL_);
-
-               fprintf(fp, "               ");
-               print_num(fp, 8, s->rx_length_errors);
-               print_num(fp, 7, s->rx_crc_errors);
-               print_num(fp, 7, s->rx_frame_errors);
-               print_num(fp, 7, s->rx_fifo_errors);
-               print_num(fp, 7, s->rx_missed_errors);
-       }
-       fprintf(fp, "%s", _SL_);
-
-       /* TX stats */
-       fprintf(fp, "    TX: bytes  packets  errors  dropped carrier collsns %s%s",
-               s->tx_compressed ? "compressed" : "", _SL_);
-
-
-       fprintf(fp, "    ");
-       print_num(fp, 10, s->tx_bytes);
-       print_num(fp, 8, s->tx_packets);
-       print_num(fp, 7, s->tx_errors);
-       print_num(fp, 7, s->tx_dropped);
-       print_num(fp, 7, s->tx_carrier_errors);
-       print_num(fp, 7, s->collisions);
-       if (s->tx_compressed)
-               print_num(fp, 7, s->tx_compressed);
-
-       /* TX error stats */
-       if (show_stats > 1) {
+               /* TX stats */
                fprintf(fp, "%s", _SL_);
-               fprintf(fp, "    TX errors: aborted  fifo   window heartbeat");
-                if (carrier_changes)
-                       fprintf(fp, " transns");
+               fprintf(fp, "    TX: bytes  packets ");
+               if (vf[IFLA_VF_STATS_TX_DROPPED])
+                       fprintf(fp, "  dropped ");
                fprintf(fp, "%s", _SL_);
+               fprintf(fp, "    ");
 
-               fprintf(fp, "               ");
-               print_num(fp, 8, s->tx_aborted_errors);
-               print_num(fp, 7, s->tx_fifo_errors);
-               print_num(fp, 7, s->tx_window_errors);
-               print_num(fp, 7, s->tx_heartbeat_errors);
-               if (carrier_changes)
-                       print_num(fp, 7, *(uint32_t*)RTA_DATA(carrier_changes));
+               print_num(fp, 10, rta_getattr_u64(vf[IFLA_VF_STATS_TX_BYTES]));
+               print_num(fp, 8, rta_getattr_u64(vf[IFLA_VF_STATS_TX_PACKETS]));
+               if (vf[IFLA_VF_STATS_TX_DROPPED])
+                       print_num(fp, 8, rta_getattr_u64(vf[IFLA_VF_STATS_TX_DROPPED]));
        }
 }
 
-static void print_link_stats32(FILE *fp, const struct rtnl_link_stats *s,
-                              const struct rtattr *carrier_changes)
+static void __print_link_stats(FILE *fp, struct rtattr *tb[])
 {
-       /* RX stats */
-       fprintf(fp, "    RX: bytes  packets  errors  dropped overrun mcast   %s%s",
-               s->rx_compressed ? "compressed" : "", _SL_);
-
-
-       fprintf(fp, "    ");
-       print_num(fp, 10, s->rx_bytes);
-       print_num(fp, 8, s->rx_packets);
-       print_num(fp, 7, s->rx_errors);
-       print_num(fp, 7, s->rx_dropped);
-       print_num(fp, 7, s->rx_over_errors);
-       print_num(fp, 7, s->multicast);
-       if (s->rx_compressed)
-               print_num(fp, 7, s->rx_compressed);
-
-       /* RX error stats */
-       if (show_stats > 1) {
-               fprintf(fp, "%s", _SL_);
-               fprintf(fp, "    RX errors: length   crc     frame   fifo    missed%s", _SL_);
-               fprintf(fp, "               ");
-               print_num(fp, 8, s->rx_length_errors);
-               print_num(fp, 7, s->rx_crc_errors);
-               print_num(fp, 7, s->rx_frame_errors);
-               print_num(fp, 7, s->rx_fifo_errors);
-               print_num(fp, 7, s->rx_missed_errors);
-       }
-       fprintf(fp, "%s", _SL_);
-
-       /* TX stats */
-       fprintf(fp, "    TX: bytes  packets  errors  dropped carrier collsns %s%s",
-               s->tx_compressed ? "compressed" : "", _SL_);
-
-       fprintf(fp, "    ");
-       print_num(fp, 10, s->tx_bytes);
-       print_num(fp, 8, s->tx_packets);
-       print_num(fp, 7, s->tx_errors);
-       print_num(fp, 7, s->tx_dropped);
-       print_num(fp, 7, s->tx_carrier_errors);
-       print_num(fp, 7, s->collisions);
-       if (s->tx_compressed)
-               print_num(fp, 7, s->tx_compressed);
-
-       /* TX error stats */
-       if (show_stats > 1) {
-               fprintf(fp, "%s", _SL_);
-               fprintf(fp, "    TX errors: aborted  fifo   window heartbeat");
-                if (carrier_changes)
-                       fprintf(fp, " transns");
+       const struct rtattr *carrier_changes = tb[IFLA_CARRIER_CHANGES];
+       struct rtnl_link_stats64 _s, *s = &_s;
+       int ret;
+
+       ret = get_rtnl_link_stats_rta(s, tb);
+       if (ret < 0)
+               return;
+
+       if (is_json_context()) {
+               open_json_object((ret == sizeof(*s)) ? "stats64" : "stats");
+
+               /* RX stats */
+               open_json_object("rx");
+               print_u64(PRINT_JSON, "bytes", NULL, s->rx_bytes);
+               print_u64(PRINT_JSON, "packets", NULL, s->rx_packets);
+               print_u64(PRINT_JSON, "errors", NULL, s->rx_errors);
+               print_u64(PRINT_JSON, "dropped", NULL, s->rx_dropped);
+               print_u64(PRINT_JSON, "over_errors", NULL, s->rx_over_errors);
+               print_u64(PRINT_JSON, "multicast", NULL, s->multicast);
+               if (s->rx_compressed)
+                       print_u64(PRINT_JSON,
+                                  "compressed", NULL, s->rx_compressed);
+
+               /* RX error stats */
+               if (show_stats > 1) {
+                       print_u64(PRINT_JSON,
+                                  "length_errors",
+                                  NULL, s->rx_length_errors);
+                       print_u64(PRINT_JSON,
+                                  "crc_errors",
+                                  NULL, s->rx_crc_errors);
+                       print_u64(PRINT_JSON,
+                                  "frame_errors",
+                                  NULL, s->rx_frame_errors);
+                       print_u64(PRINT_JSON,
+                                  "fifo_errors",
+                                  NULL, s->rx_fifo_errors);
+                       print_u64(PRINT_JSON,
+                                  "missed_errors",
+                                  NULL, s->rx_missed_errors);
+                       if (s->rx_nohandler)
+                               print_u64(PRINT_JSON,
+                                          "nohandler", NULL, s->rx_nohandler);
+               }
+               close_json_object();
+
+               /* TX stats */
+               open_json_object("tx");
+               print_u64(PRINT_JSON, "bytes", NULL, s->tx_bytes);
+               print_u64(PRINT_JSON, "packets", NULL, s->tx_packets);
+               print_u64(PRINT_JSON, "errors", NULL, s->tx_errors);
+               print_u64(PRINT_JSON, "dropped", NULL, s->tx_dropped);
+               print_u64(PRINT_JSON,
+                          "carrier_errors",
+                          NULL, s->tx_carrier_errors);
+               print_u64(PRINT_JSON, "collisions", NULL, s->collisions);
+               if (s->tx_compressed)
+                       print_u64(PRINT_JSON,
+                                  "compressed", NULL, s->tx_compressed);
+
+               /* TX error stats */
+               if (show_stats > 1) {
+                       print_u64(PRINT_JSON,
+                                  "aborted_errors",
+                                  NULL, s->tx_aborted_errors);
+                       print_u64(PRINT_JSON,
+                                  "fifo_errors",
+                                  NULL, s->tx_fifo_errors);
+                       print_u64(PRINT_JSON,
+                                  "window_errors",
+                                  NULL, s->tx_window_errors);
+                       print_u64(PRINT_JSON,
+                                  "heartbeat_errors",
+                                  NULL, s->tx_heartbeat_errors);
+                       if (carrier_changes)
+                               print_u64(PRINT_JSON, "carrier_changes", NULL,
+                                          rta_getattr_u32(carrier_changes));
+               }
+
+               close_json_object();
+               close_json_object();
+       } else {
+               /* RX stats */
+               fprintf(fp, "    RX: bytes  packets  errors  dropped overrun mcast   %s%s",
+                       s->rx_compressed ? "compressed" : "", _SL_);
+
+               fprintf(fp, "    ");
+               print_num(fp, 10, s->rx_bytes);
+               print_num(fp, 8, s->rx_packets);
+               print_num(fp, 7, s->rx_errors);
+               print_num(fp, 7, s->rx_dropped);
+               print_num(fp, 7, s->rx_over_errors);
+               print_num(fp, 7, s->multicast);
+               if (s->rx_compressed)
+                       print_num(fp, 7, s->rx_compressed);
+
+               /* RX error stats */
+               if (show_stats > 1) {
+                       fprintf(fp, "%s", _SL_);
+                       fprintf(fp, "    RX errors: length   crc     frame   fifo    missed%s%s",
+                               s->rx_nohandler ? "   nohandler" : "", _SL_);
+                       fprintf(fp, "               ");
+                       print_num(fp, 8, s->rx_length_errors);
+                       print_num(fp, 7, s->rx_crc_errors);
+                       print_num(fp, 7, s->rx_frame_errors);
+                       print_num(fp, 7, s->rx_fifo_errors);
+                       print_num(fp, 7, s->rx_missed_errors);
+                       if (s->rx_nohandler)
+                               print_num(fp, 7, s->rx_nohandler);
+               }
                fprintf(fp, "%s", _SL_);
 
-               fprintf(fp, "               ");
-               print_num(fp, 8, s->tx_aborted_errors);
-               print_num(fp, 7, s->tx_fifo_errors);
-               print_num(fp, 7, s->tx_window_errors);
-               print_num(fp, 7, s->tx_heartbeat_errors);
-               if (carrier_changes)
-                       print_num(fp, 7, *(uint32_t*)RTA_DATA(carrier_changes));
+               /* TX stats */
+               fprintf(fp, "    TX: bytes  packets  errors  dropped carrier collsns %s%s",
+                       s->tx_compressed ? "compressed" : "", _SL_);
+
+               fprintf(fp, "    ");
+               print_num(fp, 10, s->tx_bytes);
+               print_num(fp, 8, s->tx_packets);
+               print_num(fp, 7, s->tx_errors);
+               print_num(fp, 7, s->tx_dropped);
+               print_num(fp, 7, s->tx_carrier_errors);
+               print_num(fp, 7, s->collisions);
+               if (s->tx_compressed)
+                       print_num(fp, 7, s->tx_compressed);
+
+               /* TX error stats */
+               if (show_stats > 1) {
+                       fprintf(fp, "%s", _SL_);
+                       fprintf(fp, "    TX errors: aborted  fifo   window heartbeat");
+                       if (carrier_changes)
+                               fprintf(fp, " transns");
+                       fprintf(fp, "%s", _SL_);
+
+                       fprintf(fp, "               ");
+                       print_num(fp, 8, s->tx_aborted_errors);
+                       print_num(fp, 7, s->tx_fifo_errors);
+                       print_num(fp, 7, s->tx_window_errors);
+                       print_num(fp, 7, s->tx_heartbeat_errors);
+                       if (carrier_changes)
+                               print_num(fp, 7,
+                                         rta_getattr_u32(carrier_changes));
+               }
        }
 }
 
-static void __print_link_stats(FILE *fp, struct rtattr **tb)
-{
-       if (tb[IFLA_STATS64])
-               print_link_stats64(fp, RTA_DATA(tb[IFLA_STATS64]),
-                                       tb[IFLA_CARRIER_CHANGES]);
-       else if (tb[IFLA_STATS])
-               print_link_stats32(fp, RTA_DATA(tb[IFLA_STATS]),
-                                       tb[IFLA_CARRIER_CHANGES]);
-}
-
 static void print_link_stats(FILE *fp, struct nlmsghdr *n)
 {
        struct ifinfomsg *ifi = NLMSG_DATA(n);
-       struct rtattr * tb[IFLA_MAX+1];
+       struct rtattr *tb[IFLA_MAX+1];
 
        parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi),
                     n->nlmsg_len - NLMSG_LENGTH(sizeof(*ifi)));
        __print_link_stats(fp, tb);
-       fprintf(fp, "%s", _SL_);
+       print_nl();
 }
 
-int print_linkinfo_brief(const struct sockaddr_nl *who,
-                               struct nlmsghdr *n, void *arg)
+static int print_linkinfo_brief(FILE *fp, const char *name,
+                               const struct ifinfomsg *ifi,
+                               struct rtattr *tb[])
 {
-       FILE *fp = (FILE*)arg;
-       struct ifinfomsg *ifi = NLMSG_DATA(n);
-       struct rtattr * tb[IFLA_MAX+1];
-       int len = n->nlmsg_len;
-       char *name;
-       char buf[32] = { 0, };
-       unsigned m_flag = 0;
-
-       if (n->nlmsg_type != RTM_NEWLINK && n->nlmsg_type != RTM_DELLINK)
-               return -1;
+       unsigned int m_flag = 0;
 
-       len -= NLMSG_LENGTH(sizeof(*ifi));
-       if (len < 0)
-               return -1;
-
-       if (filter.ifindex && ifi->ifi_index != filter.ifindex)
-               return -1;
-       if (filter.up && !(ifi->ifi_flags&IFF_UP))
-               return -1;
-
-       parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
-       if (tb[IFLA_IFNAME] == NULL) {
-               fprintf(stderr, "BUG: device with ifindex %d has nil ifname\n", ifi->ifi_index);
-       }
-       if (filter.label &&
-           (!filter.family || filter.family == AF_PACKET) &&
-           fnmatch(filter.label, RTA_DATA(tb[IFLA_IFNAME]), 0))
-               return -1;
-
-       if (tb[IFLA_GROUP]) {
-               int group = *(int*)RTA_DATA(tb[IFLA_GROUP]);
-               if (filter.group != -1 && group != filter.group)
-                       return -1;
-       }
-
-       if (tb[IFLA_MASTER]) {
-               int master = *(int*)RTA_DATA(tb[IFLA_MASTER]);
-               if (filter.master > 0 && master != filter.master)
-                       return -1;
-       }
-       else if (filter.master > 0)
-               return -1;
-
-       if (filter.kind) {
-               if (tb[IFLA_LINKINFO]) {
-                       char *kind = parse_link_kind(tb[IFLA_LINKINFO]);
-
-                       if (strcmp(kind, filter.kind))
-                               return -1;
-               } else {
-                       return -1;
-               }
-       }
-
-       if (n->nlmsg_type == RTM_DELLINK)
-               fprintf(fp, "Deleted ");
-
-       name = (char *)(tb[IFLA_IFNAME] ? rta_getattr_str(tb[IFLA_IFNAME]) : "<nil>");
-
-       if (tb[IFLA_LINK]) {
-               SPRINT_BUF(b1);
-               int iflink = *(int*)RTA_DATA(tb[IFLA_LINK]);
-               if (iflink == 0)
-                       snprintf(buf, sizeof(buf), "%s@NONE", name);
-               else {
-                       snprintf(buf, sizeof(buf),
-                                "%s@%s", name, ll_idx_n2a(iflink, b1));
-                       m_flag = ll_index_to_flags(iflink);
-                       m_flag = !(m_flag & IFF_UP);
-               }
-       } else
-               snprintf(buf, sizeof(buf), "%s", name);
-
-       fprintf(fp, "%-16s ", buf);
+       m_flag = print_name_and_link("%-16s ", name, tb);
 
        if (tb[IFLA_OPERSTATE])
                print_operstate(fp, rta_getattr_u8(tb[IFLA_OPERSTATE]));
 
        if (filter.family == AF_PACKET) {
                SPRINT_BUF(b1);
+
                if (tb[IFLA_ADDRESS]) {
-                       color_fprintf(fp, COLOR_MAC, "%s ",
-                                       ll_addr_n2a(RTA_DATA(tb[IFLA_ADDRESS]),
-                                               RTA_PAYLOAD(tb[IFLA_ADDRESS]),
-                                               ifi->ifi_type,
-                                               b1, sizeof(b1)));
+                       print_color_string(PRINT_ANY, COLOR_MAC,
+                                          "address", "%s ",
+                                          ll_addr_n2a(
+                                                  RTA_DATA(tb[IFLA_ADDRESS]),
+                                                  RTA_PAYLOAD(tb[IFLA_ADDRESS]),
+                                                  ifi->ifi_type,
+                                                  b1, sizeof(b1)));
                }
        }
 
-       if (filter.family == AF_PACKET)
+       if (filter.family == AF_PACKET) {
                print_link_flags(fp, ifi->ifi_flags, m_flag);
+               print_string(PRINT_FP, NULL, "%s", "\n");
+       }
 
-       if (filter.family == AF_PACKET)
-               fprintf(fp, "\n");
        fflush(fp);
        return 0;
 }
 
-int print_linkinfo(const struct sockaddr_nl *who,
-                  struct nlmsghdr *n, void *arg)
+static const char *link_events[] = {
+       [IFLA_EVENT_NONE] = "NONE",
+       [IFLA_EVENT_REBOOT] = "REBOOT",
+       [IFLA_EVENT_FEATURES] = "FEATURE CHANGE",
+       [IFLA_EVENT_BONDING_FAILOVER] = "BONDING FAILOVER",
+       [IFLA_EVENT_NOTIFY_PEERS] = "NOTIFY PEERS",
+       [IFLA_EVENT_IGMP_RESEND] = "RESEND IGMP",
+       [IFLA_EVENT_BONDING_OPTIONS] = "BONDING OPTION"
+};
+
+static void print_link_event(FILE *f, __u32 event)
 {
-       FILE *fp = (FILE*)arg;
+       if (event >= ARRAY_SIZE(link_events))
+               print_int(PRINT_ANY, "event", "event %d ", event);
+       else {
+               if (event)
+                       print_string(PRINT_ANY,
+                                    "event", "event %s ",
+                                    link_events[event]);
+       }
+}
+
+int print_linkinfo(struct nlmsghdr *n, void *arg)
+{
+       FILE *fp = (FILE *)arg;
        struct ifinfomsg *ifi = NLMSG_DATA(n);
-       struct rtattr * tb[IFLA_MAX+1];
+       struct rtattr *tb[IFLA_MAX+1];
        int len = n->nlmsg_len;
-       unsigned m_flag = 0;
+       const char *name;
+       unsigned int m_flag = 0;
+       SPRINT_BUF(b1);
 
        if (n->nlmsg_type != RTM_NEWLINK && n->nlmsg_type != RTM_DELLINK)
                return 0;
@@ -724,93 +875,68 @@ int print_linkinfo(const struct sockaddr_nl *who,
                return -1;
 
        if (filter.ifindex && ifi->ifi_index != filter.ifindex)
-               return 0;
+               return -1;
        if (filter.up && !(ifi->ifi_flags&IFF_UP))
-               return 0;
+               return -1;
 
        parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
-       if (tb[IFLA_IFNAME] == NULL) {
-               fprintf(stderr, "BUG: device with ifindex %d has nil ifname\n", ifi->ifi_index);
-       }
-       if (filter.label &&
-           (!filter.family || filter.family == AF_PACKET) &&
-           fnmatch(filter.label, RTA_DATA(tb[IFLA_IFNAME]), 0))
+
+       name = get_ifname_rta(ifi->ifi_index, tb[IFLA_IFNAME]);
+       if (!name)
+               return -1;
+
+       if (filter.label)
                return 0;
 
        if (tb[IFLA_GROUP]) {
-               int group = *(int*)RTA_DATA(tb[IFLA_GROUP]);
+               int group = rta_getattr_u32(tb[IFLA_GROUP]);
+
                if (filter.group != -1 && group != filter.group)
                        return -1;
        }
 
        if (tb[IFLA_MASTER]) {
-               int master = *(int*)RTA_DATA(tb[IFLA_MASTER]);
+               int master = rta_getattr_u32(tb[IFLA_MASTER]);
+
                if (filter.master > 0 && master != filter.master)
                        return -1;
-       }
-       else if (filter.master > 0)
+       } else if (filter.master > 0)
                return -1;
 
-       if (filter.kind) {
-               if (tb[IFLA_LINKINFO]) {
-                       char *kind = parse_link_kind(tb[IFLA_LINKINFO]);
+       if (filter.kind && match_link_kind(tb, filter.kind, 0))
+               return -1;
 
-                       if (strcmp(kind, filter.kind))
-                               return -1;
-               } else {
-                       return -1;
-               }
-       }
+       if (filter.slave_kind && match_link_kind(tb, filter.slave_kind, 1))
+               return -1;
 
        if (n->nlmsg_type == RTM_DELLINK)
-               fprintf(fp, "Deleted ");
+               print_bool(PRINT_ANY, "deleted", "Deleted ", true);
 
-       fprintf(fp, "%d: ", ifi->ifi_index);
-       color_fprintf(fp, COLOR_IFNAME, "%s",
-               tb[IFLA_IFNAME] ? rta_getattr_str(tb[IFLA_IFNAME]) : "<nil>");
+       if (brief)
+               return print_linkinfo_brief(fp, name, ifi, tb);
 
-       if (tb[IFLA_LINK]) {
-               SPRINT_BUF(b1);
-               int iflink = *(int*)RTA_DATA(tb[IFLA_LINK]);
-               if (iflink == 0)
-                       fprintf(fp, "@NONE: ");
-               else {
-                       if (tb[IFLA_LINK_NETNSID])
-                               fprintf(fp, "@if%d: ", iflink);
-                       else {
-                               fprintf(fp, "@%s: ", ll_idx_n2a(iflink, b1));
-                               m_flag = ll_index_to_flags(iflink);
-                               m_flag = !(m_flag & IFF_UP);
-                       }
-               }
-       } else {
-               fprintf(fp, ": ");
-       }
+       print_int(PRINT_ANY, "ifindex", "%d: ", ifi->ifi_index);
+
+       m_flag = print_name_and_link("%s: ", name, tb);
        print_link_flags(fp, ifi->ifi_flags, m_flag);
 
        if (tb[IFLA_MTU])
-               fprintf(fp, "mtu %u ", *(int*)RTA_DATA(tb[IFLA_MTU]));
+               print_int(PRINT_ANY,
+                         "mtu", "mtu %u ",
+                         rta_getattr_u32(tb[IFLA_MTU]));
+       if (tb[IFLA_XDP])
+               xdp_dump(fp, tb[IFLA_XDP], do_link, false);
        if (tb[IFLA_QDISC])
-               fprintf(fp, "qdisc %s ", rta_getattr_str(tb[IFLA_QDISC]));
+               print_string(PRINT_ANY,
+                            "qdisc",
+                            "qdisc %s ",
+                            rta_getattr_str(tb[IFLA_QDISC]));
        if (tb[IFLA_MASTER]) {
-               SPRINT_BUF(b1);
-               fprintf(fp, "master %s ", ll_idx_n2a(*(int*)RTA_DATA(tb[IFLA_MASTER]), b1));
-       }
-
-       if (tb[IFLA_PHYS_PORT_ID]) {
-               SPRINT_BUF(b1);
-               fprintf(fp, "portid %s ",
-                       hexstring_n2a(RTA_DATA(tb[IFLA_PHYS_PORT_ID]),
-                                     RTA_PAYLOAD(tb[IFLA_PHYS_PORT_ID]),
-                                     b1, sizeof(b1)));
-       }
+               int master = rta_getattr_u32(tb[IFLA_MASTER]);
 
-       if (tb[IFLA_PHYS_SWITCH_ID]) {
-               SPRINT_BUF(b1);
-               fprintf(fp, "switchid %s ",
-                       hexstring_n2a(RTA_DATA(tb[IFLA_PHYS_SWITCH_ID]),
-                                     RTA_PAYLOAD(tb[IFLA_PHYS_SWITCH_ID]),
-                                     b1, sizeof(b1)));
+               print_string(PRINT_ANY,
+                            "master", "master %s ",
+                            ll_index_to_name(master));
        }
 
        if (tb[IFLA_OPERSTATE])
@@ -820,80 +946,200 @@ int print_linkinfo(const struct sockaddr_nl *who,
                print_linkmode(fp, tb[IFLA_LINKMODE]);
 
        if (tb[IFLA_GROUP]) {
-               SPRINT_BUF(b1);
-               int group = *(int*)RTA_DATA(tb[IFLA_GROUP]);
-               fprintf(fp, "group %s ", rtnl_group_n2a(group, b1, sizeof(b1)));
+               int group = rta_getattr_u32(tb[IFLA_GROUP]);
+
+               print_string(PRINT_ANY,
+                            "group",
+                            "group %s ",
+                            rtnl_group_n2a(group, b1, sizeof(b1)));
        }
 
        if (filter.showqueue)
                print_queuelen(fp, tb);
 
-       if (!filter.family || filter.family == AF_PACKET || show_details) {
-               SPRINT_BUF(b1);
-               fprintf(fp, "%s", _SL_);
-               fprintf(fp, "    link/%s ", ll_type_n2a(ifi->ifi_type, b1, sizeof(b1)));
+       if (tb[IFLA_EVENT])
+               print_link_event(fp, rta_getattr_u32(tb[IFLA_EVENT]));
 
+       if (!filter.family || filter.family == AF_PACKET || show_details) {
+               print_nl();
+               print_string(PRINT_ANY,
+                            "link_type",
+                            "    link/%s ",
+                            ll_type_n2a(ifi->ifi_type, b1, sizeof(b1)));
                if (tb[IFLA_ADDRESS]) {
-                       color_fprintf(fp, COLOR_MAC, "%s",
-                                       ll_addr_n2a(RTA_DATA(tb[IFLA_ADDRESS]),
-                                               RTA_PAYLOAD(tb[IFLA_ADDRESS]),
-                                               ifi->ifi_type,
-                                               b1, sizeof(b1)));
+                       print_color_string(PRINT_ANY,
+                                          COLOR_MAC,
+                                          "address",
+                                          "%s",
+                                          ll_addr_n2a(RTA_DATA(tb[IFLA_ADDRESS]),
+                                                      RTA_PAYLOAD(tb[IFLA_ADDRESS]),
+                                                      ifi->ifi_type,
+                                                      b1, sizeof(b1)));
                }
                if (tb[IFLA_BROADCAST]) {
-                       if (ifi->ifi_flags&IFF_POINTOPOINT)
-                               fprintf(fp, " peer ");
-                       else
-                               fprintf(fp, " brd ");
-                       fprintf(fp, "%s", ll_addr_n2a(RTA_DATA(tb[IFLA_BROADCAST]),
-                                                     RTA_PAYLOAD(tb[IFLA_BROADCAST]),
-                                                     ifi->ifi_type,
-                                                     b1, sizeof(b1)));
+                       if (ifi->ifi_flags&IFF_POINTOPOINT) {
+                               print_string(PRINT_FP, NULL, " peer ", NULL);
+                               print_bool(PRINT_JSON,
+                                          "link_pointtopoint", NULL, true);
+                       } else {
+                               print_string(PRINT_FP, NULL, " brd ", NULL);
+                       }
+                       print_color_string(PRINT_ANY,
+                                          COLOR_MAC,
+                                          "broadcast",
+                                          "%s",
+                                          ll_addr_n2a(RTA_DATA(tb[IFLA_BROADCAST]),
+                                                      RTA_PAYLOAD(tb[IFLA_BROADCAST]),
+                                                      ifi->ifi_type,
+                                                      b1, sizeof(b1)));
                }
        }
 
        if (tb[IFLA_LINK_NETNSID]) {
-               int id = *(int*)RTA_DATA(tb[IFLA_LINK_NETNSID]);
+               int id = rta_getattr_u32(tb[IFLA_LINK_NETNSID]);
 
-               if (id >= 0)
-                       fprintf(fp, " link-netnsid %d", id);
-               else
-                       fprintf(fp, " link-netnsid unknown");
+               if (is_json_context()) {
+                       print_int(PRINT_JSON, "link_netnsid", NULL, id);
+               } else {
+                       if (id >= 0) {
+                               char *name = get_name_from_nsid(id);
+
+                               if (name)
+                                       print_string(PRINT_FP, NULL,
+                                                    " link-netns %s", name);
+                               else
+                                       print_int(PRINT_FP, NULL,
+                                                 " link-netnsid %d", id);
+                       } else
+                               print_string(PRINT_FP, NULL,
+                                            " link-netnsid %s", "unknown");
+               }
        }
 
-       if (tb[IFLA_PROTO_DOWN]) {
-               if (rta_getattr_u8(tb[IFLA_PROTO_DOWN]))
-                       fprintf(fp, " protodown on ");
+       if (tb[IFLA_NEW_NETNSID]) {
+               int id = rta_getattr_u32(tb[IFLA_NEW_NETNSID]);
+               char *name = get_name_from_nsid(id);
+
+               if (name)
+                       print_string(PRINT_FP, NULL, " new-netns %s", name);
+               else
+                       print_int(PRINT_FP, NULL, " new-netnsid %d", id);
        }
+       if (tb[IFLA_NEW_IFINDEX]) {
+               int id = rta_getattr_u32(tb[IFLA_NEW_IFINDEX]);
 
-       if (tb[IFLA_PROMISCUITY] && show_details)
-               fprintf(fp, " promiscuity %u ",
-                       *(int*)RTA_DATA(tb[IFLA_PROMISCUITY]));
+               print_int(PRINT_FP, NULL, " new-ifindex %d", id);
+       }
 
-       if (tb[IFLA_LINKINFO] && show_details)
-               print_linktype(fp, tb[IFLA_LINKINFO]);
+       if (tb[IFLA_PROTO_DOWN]) {
+               if (rta_getattr_u8(tb[IFLA_PROTO_DOWN]))
+                       print_bool(PRINT_ANY,
+                                  "proto_down", " protodown on ", true);
+       }
+
+       if (show_details) {
+               if (tb[IFLA_PROMISCUITY])
+                       print_uint(PRINT_ANY,
+                                  "promiscuity",
+                                  " promiscuity %u ",
+                                  rta_getattr_u32(tb[IFLA_PROMISCUITY]));
+
+               if (tb[IFLA_MIN_MTU])
+                       print_uint(PRINT_ANY,
+                                  "min_mtu", "minmtu %u ",
+                                  rta_getattr_u32(tb[IFLA_MIN_MTU]));
+
+               if (tb[IFLA_MAX_MTU])
+                       print_uint(PRINT_ANY,
+                                  "max_mtu", "maxmtu %u ",
+                                  rta_getattr_u32(tb[IFLA_MAX_MTU]));
+
+               if (tb[IFLA_LINKINFO])
+                       print_linktype(fp, tb[IFLA_LINKINFO]);
+
+               if (do_link && tb[IFLA_AF_SPEC])
+                       print_af_spec(fp, tb[IFLA_AF_SPEC]);
+
+               if (tb[IFLA_NUM_TX_QUEUES])
+                       print_uint(PRINT_ANY,
+                                  "num_tx_queues",
+                                  "numtxqueues %u ",
+                                  rta_getattr_u32(tb[IFLA_NUM_TX_QUEUES]));
+
+               if (tb[IFLA_NUM_RX_QUEUES])
+                       print_uint(PRINT_ANY,
+                                  "num_rx_queues",
+                                  "numrxqueues %u ",
+                                  rta_getattr_u32(tb[IFLA_NUM_RX_QUEUES]));
+
+               if (tb[IFLA_GSO_MAX_SIZE])
+                       print_uint(PRINT_ANY,
+                                  "gso_max_size",
+                                  "gso_max_size %u ",
+                                  rta_getattr_u32(tb[IFLA_GSO_MAX_SIZE]));
+
+               if (tb[IFLA_GSO_MAX_SEGS])
+                       print_uint(PRINT_ANY,
+                                  "gso_max_segs",
+                                  "gso_max_segs %u ",
+                                  rta_getattr_u32(tb[IFLA_GSO_MAX_SEGS]));
+
+               if (tb[IFLA_PHYS_PORT_NAME])
+                       print_string(PRINT_ANY,
+                                    "phys_port_name",
+                                    "portname %s ",
+                                    rta_getattr_str(tb[IFLA_PHYS_PORT_NAME]));
+
+               if (tb[IFLA_PHYS_PORT_ID]) {
+                       print_string(PRINT_ANY,
+                                    "phys_port_id",
+                                    "portid %s ",
+                                    hexstring_n2a(
+                                            RTA_DATA(tb[IFLA_PHYS_PORT_ID]),
+                                            RTA_PAYLOAD(tb[IFLA_PHYS_PORT_ID]),
+                                            b1, sizeof(b1)));
+               }
 
-       if (do_link && tb[IFLA_AF_SPEC] && show_details)
-               print_af_spec(fp, tb[IFLA_AF_SPEC]);
+               if (tb[IFLA_PHYS_SWITCH_ID]) {
+                       print_string(PRINT_ANY,
+                                    "phys_switch_id",
+                                    "switchid %s ",
+                                    hexstring_n2a(RTA_DATA(tb[IFLA_PHYS_SWITCH_ID]),
+                                                  RTA_PAYLOAD(tb[IFLA_PHYS_SWITCH_ID]),
+                                                  b1, sizeof(b1)));
+               }
+       }
 
        if ((do_link || show_details) && tb[IFLA_IFALIAS]) {
-               fprintf(fp, "%s    alias %s", _SL_,
-                       rta_getattr_str(tb[IFLA_IFALIAS]));
+               print_string(PRINT_FP, NULL, "%s    ", _SL_);
+               print_string(PRINT_ANY,
+                            "ifalias",
+                            "alias %s",
+                            rta_getattr_str(tb[IFLA_IFALIAS]));
        }
 
+       if ((do_link || show_details) && tb[IFLA_XDP])
+               xdp_dump(fp, tb[IFLA_XDP], true, true);
+
        if (do_link && show_stats) {
-               fprintf(fp, "%s", _SL_);
+               print_nl();
                __print_link_stats(fp, tb);
        }
 
        if ((do_link || show_details) && tb[IFLA_VFINFO_LIST] && tb[IFLA_NUM_VF]) {
                struct rtattr *i, *vflist = tb[IFLA_VFINFO_LIST];
                int rem = RTA_PAYLOAD(vflist);
-               for (i = RTA_DATA(vflist); RTA_OK(i, rem); i = RTA_NEXT(i, rem))
-                       print_vfinfo(fp, i);
+
+               open_json_array(PRINT_JSON, "vfinfo_list");
+               for (i = RTA_DATA(vflist); RTA_OK(i, rem); i = RTA_NEXT(i, rem)) {
+                       open_json_object(NULL);
+                       print_vfinfo(fp, ifi, i);
+                       close_json_object();
+               }
+               close_json_array(PRINT_JSON, NULL);
        }
 
-       fprintf(fp, "\n");
+       print_string(PRINT_FP, NULL, "%s", "\n");
        fflush(fp);
        return 1;
 }
@@ -932,20 +1178,126 @@ static unsigned int get_ifa_flags(struct ifaddrmsg *ifa,
                                  struct rtattr *ifa_flags_attr)
 {
        return ifa_flags_attr ? rta_getattr_u32(ifa_flags_attr) :
-                               ifa->ifa_flags;
+               ifa->ifa_flags;
 }
 
-int print_addrinfo(const struct sockaddr_nl *who, struct nlmsghdr *n,
-                  void *arg)
+/* Mapping from argument to address flag mask */
+static const struct {
+       const char *name;
+       unsigned long value;
+} ifa_flag_names[] = {
+       { "secondary",          IFA_F_SECONDARY },
+       { "temporary",          IFA_F_SECONDARY },
+       { "nodad",              IFA_F_NODAD },
+       { "optimistic",         IFA_F_OPTIMISTIC },
+       { "dadfailed",          IFA_F_DADFAILED },
+       { "home",               IFA_F_HOMEADDRESS },
+       { "deprecated",         IFA_F_DEPRECATED },
+       { "tentative",          IFA_F_TENTATIVE },
+       { "permanent",          IFA_F_PERMANENT },
+       { "mngtmpaddr",         IFA_F_MANAGETEMPADDR },
+       { "noprefixroute",      IFA_F_NOPREFIXROUTE },
+       { "autojoin",           IFA_F_MCAUTOJOIN },
+       { "stable-privacy",     IFA_F_STABLE_PRIVACY },
+};
+
+static void print_ifa_flags(FILE *fp, const struct ifaddrmsg *ifa,
+                           unsigned int flags)
+{
+       unsigned int i;
+
+       for (i = 0; i < ARRAY_SIZE(ifa_flag_names); i++) {
+               unsigned long mask = ifa_flag_names[i].value;
+
+               if (mask == IFA_F_PERMANENT) {
+                       if (!(flags & mask))
+                               print_bool(PRINT_ANY,
+                                          "dynamic", "dynamic ", true);
+               } else if (flags & mask) {
+                       if (mask == IFA_F_SECONDARY &&
+                           ifa->ifa_family == AF_INET6) {
+                               print_bool(PRINT_ANY,
+                                          "temporary", "temporary ", true);
+                       } else {
+                               print_string(PRINT_FP, NULL,
+                                            "%s ", ifa_flag_names[i].name);
+                               print_bool(PRINT_JSON,
+                                          ifa_flag_names[i].name, NULL, true);
+                       }
+               }
+
+               flags &= ~mask;
+       }
+
+       if (flags) {
+               if (is_json_context()) {
+                       SPRINT_BUF(b1);
+
+                       snprintf(b1, sizeof(b1), "%02x", flags);
+                       print_string(PRINT_JSON, "ifa_flags", NULL, b1);
+               } else {
+                       fprintf(fp, "flags %02x ", flags);
+               }
+       }
+
+}
+
+static int get_filter(const char *arg)
+{
+       bool inv = false;
+       unsigned int i;
+
+       if (arg[0] == '-') {
+               inv = true;
+               arg++;
+       }
+
+       /* Special cases */
+       if (strcmp(arg, "dynamic") == 0) {
+               inv = !inv;
+               arg = "permanent";
+       } else if (strcmp(arg, "primary") == 0) {
+               inv = !inv;
+               arg = "secondary";
+       }
+
+       for (i = 0; i < ARRAY_SIZE(ifa_flag_names); i++) {
+               if (strcmp(arg, ifa_flag_names[i].name))
+                       continue;
+
+               if (inv)
+                       filter.flags &= ~ifa_flag_names[i].value;
+               else
+                       filter.flags |= ifa_flag_names[i].value;
+               filter.flagmask |= ifa_flag_names[i].value;
+               return 0;
+       }
+       return -1;
+}
+
+static int ifa_label_match_rta(int ifindex, const struct rtattr *rta)
+{
+       const char *label;
+
+       if (!filter.label)
+               return 0;
+
+       if (rta)
+               label = RTA_DATA(rta);
+       else
+               label = ll_index_to_name(ifindex);
+
+       return fnmatch(filter.label, label, 0);
+}
+
+int print_addrinfo(struct nlmsghdr *n, void *arg)
 {
        FILE *fp = arg;
        struct ifaddrmsg *ifa = NLMSG_DATA(n);
        int len = n->nlmsg_len;
-       int deprecated = 0;
-       /* Use local copy of ifa_flags to not interfere with filtering code */
        unsigned int ifa_flags;
-       struct rtattr * rta_tb[IFA_MAX+1];
-       char abuf[256];
+       struct rtattr *rta_tb[IFA_MAX+1];
+
        SPRINT_BUF(b1);
 
        if (n->nlmsg_type != RTM_NEWADDR && n->nlmsg_type != RTM_DELADDR)
@@ -975,209 +1327,175 @@ int print_addrinfo(const struct sockaddr_nl *who, struct nlmsghdr *n,
                return 0;
        if ((filter.flags ^ ifa_flags) & filter.flagmask)
                return 0;
-       if (filter.label) {
-               SPRINT_BUF(b1);
-               const char *label;
-               if (rta_tb[IFA_LABEL])
-                       label = RTA_DATA(rta_tb[IFA_LABEL]);
-               else
-                       label = ll_idx_n2a(ifa->ifa_index, b1);
-               if (fnmatch(filter.label, label, 0) != 0)
-                       return 0;
-       }
-       if (filter.pfx.family) {
-               if (rta_tb[IFA_LOCAL]) {
-                       inet_prefix dst;
-                       memset(&dst, 0, sizeof(dst));
-                       dst.family = ifa->ifa_family;
-                       memcpy(&dst.data, RTA_DATA(rta_tb[IFA_LOCAL]), RTA_PAYLOAD(rta_tb[IFA_LOCAL]));
-                       if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen))
-                               return 0;
-               }
-       }
 
        if (filter.family && filter.family != ifa->ifa_family)
                return 0;
 
+       if (ifa_label_match_rta(ifa->ifa_index, rta_tb[IFA_LABEL]))
+               return 0;
+
+       if (inet_addr_match_rta(&filter.pfx, rta_tb[IFA_LOCAL]))
+               return 0;
+
        if (filter.flushb) {
                struct nlmsghdr *fn;
+
                if (NLMSG_ALIGN(filter.flushp) + n->nlmsg_len > filter.flushe) {
                        if (flush_update())
                                return -1;
                }
-               fn = (struct nlmsghdr*)(filter.flushb + NLMSG_ALIGN(filter.flushp));
+               fn = (struct nlmsghdr *)(filter.flushb + NLMSG_ALIGN(filter.flushp));
                memcpy(fn, n, n->nlmsg_len);
                fn->nlmsg_type = RTM_DELADDR;
                fn->nlmsg_flags = NLM_F_REQUEST;
                fn->nlmsg_seq = ++rth.seq;
-               filter.flushp = (((char*)fn) + n->nlmsg_len) - filter.flushb;
+               filter.flushp = (((char *)fn) + n->nlmsg_len) - filter.flushb;
                filter.flushed++;
                if (show_stats < 2)
                        return 0;
        }
 
        if (n->nlmsg_type == RTM_DELADDR)
-               fprintf(fp, "Deleted ");
+               print_bool(PRINT_ANY, "deleted", "Deleted ", true);
 
        if (!brief) {
-               if (filter.oneline || filter.flushb)
-                       fprintf(fp, "%u: %s", ifa->ifa_index, ll_index_to_name(ifa->ifa_index));
-               if (ifa->ifa_family == AF_INET)
-                       fprintf(fp, "    inet ");
-               else if (ifa->ifa_family == AF_INET6)
-                       fprintf(fp, "    inet6 ");
-               else if (ifa->ifa_family == AF_DECnet)
-                       fprintf(fp, "    dnet ");
-               else if (ifa->ifa_family == AF_IPX)
-                       fprintf(fp, "     ipx ");
-               else
-                       fprintf(fp, "    family %d ", ifa->ifa_family);
+               const char *name;
+
+               if (filter.oneline || filter.flushb) {
+                       const char *dev = ll_index_to_name(ifa->ifa_index);
+
+                       if (is_json_context()) {
+                               print_int(PRINT_JSON,
+                                         "index", NULL, ifa->ifa_index);
+                               print_string(PRINT_JSON, "dev", NULL, dev);
+                       } else {
+                               fprintf(fp, "%u: %s", ifa->ifa_index, dev);
+                       }
+               }
+
+               name = family_name(ifa->ifa_family);
+               if (*name != '?') {
+                       print_string(PRINT_ANY, "family", "    %s ", name);
+               } else {
+                       print_int(PRINT_ANY, "family_index", "    family %d ",
+                                 ifa->ifa_family);
+               }
        }
 
        if (rta_tb[IFA_LOCAL]) {
-               if (ifa->ifa_family == AF_INET)
-                       color_fprintf(fp, COLOR_INET, "%s", format_host(ifa->ifa_family,
-                                               RTA_PAYLOAD(rta_tb[IFA_LOCAL]),
-                                               RTA_DATA(rta_tb[IFA_LOCAL]),
-                                               abuf, sizeof(abuf)));
-               else if (ifa->ifa_family == AF_INET6)
-                       color_fprintf(fp, COLOR_INET6, "%s", format_host(ifa->ifa_family,
-                                               RTA_PAYLOAD(rta_tb[IFA_LOCAL]),
-                                               RTA_DATA(rta_tb[IFA_LOCAL]),
-                                               abuf, sizeof(abuf)));
-               else
-                       fprintf(fp, "%s", format_host(ifa->ifa_family,
-                                               RTA_PAYLOAD(rta_tb[IFA_LOCAL]),
-                                               RTA_DATA(rta_tb[IFA_LOCAL]),
-                                               abuf, sizeof(abuf)));
-
-               if (rta_tb[IFA_ADDRESS] == NULL ||
-                   memcmp(RTA_DATA(rta_tb[IFA_ADDRESS]), RTA_DATA(rta_tb[IFA_LOCAL]),
-                          ifa->ifa_family == AF_INET ? 4 : 16) == 0) {
-                       fprintf(fp, "/%d ", ifa->ifa_prefixlen);
-               } else {
-                       fprintf(fp, " peer %s/%d ",
-                               format_host(ifa->ifa_family,
-                                           RTA_PAYLOAD(rta_tb[IFA_ADDRESS]),
-                                           RTA_DATA(rta_tb[IFA_ADDRESS]),
-                                           abuf, sizeof(abuf)),
-                               ifa->ifa_prefixlen);
+               print_color_string(PRINT_ANY,
+                                  ifa_family_color(ifa->ifa_family),
+                                  "local", "%s",
+                                  format_host_rta(ifa->ifa_family,
+                                                  rta_tb[IFA_LOCAL]));
+               if (rta_tb[IFA_ADDRESS] &&
+                   memcmp(RTA_DATA(rta_tb[IFA_ADDRESS]),
+                          RTA_DATA(rta_tb[IFA_LOCAL]),
+                          ifa->ifa_family == AF_INET ? 4 : 16)) {
+                       print_string(PRINT_FP, NULL, " %s ", "peer");
+                       print_color_string(PRINT_ANY,
+                                          ifa_family_color(ifa->ifa_family),
+                                          "address",
+                                          "%s",
+                                          format_host_rta(ifa->ifa_family,
+                                                          rta_tb[IFA_ADDRESS]));
                }
+               print_int(PRINT_ANY, "prefixlen", "/%d ", ifa->ifa_prefixlen);
+
+               if (rta_tb[IFA_RT_PRIORITY])
+                       print_uint(PRINT_ANY, "metric", "metric %u ",
+                                  rta_getattr_u32(rta_tb[IFA_RT_PRIORITY]));
        }
 
        if (brief)
                goto brief_exit;
 
        if (rta_tb[IFA_BROADCAST]) {
-               fprintf(fp, "brd %s ",
-                       format_host(ifa->ifa_family,
-                                   RTA_PAYLOAD(rta_tb[IFA_BROADCAST]),
-                                   RTA_DATA(rta_tb[IFA_BROADCAST]),
-                                   abuf, sizeof(abuf)));
+               print_string(PRINT_FP, NULL, "%s ", "brd");
+               print_color_string(PRINT_ANY,
+                                  ifa_family_color(ifa->ifa_family),
+                                  "broadcast",
+                                  "%s ",
+                                  format_host_rta(ifa->ifa_family,
+                                                  rta_tb[IFA_BROADCAST]));
        }
+
        if (rta_tb[IFA_ANYCAST]) {
-               fprintf(fp, "any %s ",
-                       format_host(ifa->ifa_family,
-                                   RTA_PAYLOAD(rta_tb[IFA_ANYCAST]),
-                                   RTA_DATA(rta_tb[IFA_ANYCAST]),
-                                   abuf, sizeof(abuf)));
-       }
-       fprintf(fp, "scope %s ", rtnl_rtscope_n2a(ifa->ifa_scope, b1, sizeof(b1)));
-       if (ifa_flags & IFA_F_SECONDARY) {
-               ifa_flags &= ~IFA_F_SECONDARY;
-               if (ifa->ifa_family == AF_INET6)
-                       fprintf(fp, "temporary ");
-               else
-                       fprintf(fp, "secondary ");
-       }
-       if (ifa_flags & IFA_F_TENTATIVE) {
-               ifa_flags &= ~IFA_F_TENTATIVE;
-               fprintf(fp, "tentative ");
-       }
-       if (ifa_flags & IFA_F_DEPRECATED) {
-               ifa_flags &= ~IFA_F_DEPRECATED;
-               deprecated = 1;
-               fprintf(fp, "deprecated ");
-       }
-       if (ifa_flags & IFA_F_HOMEADDRESS) {
-               ifa_flags &= ~IFA_F_HOMEADDRESS;
-               fprintf(fp, "home ");
-       }
-       if (ifa_flags & IFA_F_NODAD) {
-               ifa_flags &= ~IFA_F_NODAD;
-               fprintf(fp, "nodad ");
-       }
-       if (ifa_flags & IFA_F_MANAGETEMPADDR) {
-               ifa_flags &= ~IFA_F_MANAGETEMPADDR;
-               fprintf(fp, "mngtmpaddr ");
-       }
-       if (ifa_flags & IFA_F_NOPREFIXROUTE) {
-               ifa_flags &= ~IFA_F_NOPREFIXROUTE;
-               fprintf(fp, "noprefixroute ");
-       }
-       if (ifa_flags & IFA_F_MCAUTOJOIN) {
-               ifa_flags &= ~IFA_F_MCAUTOJOIN;
-               fprintf(fp, "autojoin ");
-       }
-       if (!(ifa_flags & IFA_F_PERMANENT)) {
-               fprintf(fp, "dynamic ");
-       } else
-               ifa_flags &= ~IFA_F_PERMANENT;
-       if (ifa_flags & IFA_F_DADFAILED) {
-               ifa_flags &= ~IFA_F_DADFAILED;
-               fprintf(fp, "dadfailed ");
-       }
-       if (ifa_flags)
-               fprintf(fp, "flags %02x ", ifa_flags);
+               print_string(PRINT_FP, NULL, "%s ", "any");
+               print_color_string(PRINT_ANY,
+                                  ifa_family_color(ifa->ifa_family),
+                                  "anycast",
+                                  "%s ",
+                                  format_host_rta(ifa->ifa_family,
+                                                  rta_tb[IFA_ANYCAST]));
+       }
+
+       print_string(PRINT_ANY,
+                    "scope",
+                    "scope %s ",
+                    rtnl_rtscope_n2a(ifa->ifa_scope, b1, sizeof(b1)));
+
+       print_ifa_flags(fp, ifa, ifa_flags);
+
        if (rta_tb[IFA_LABEL])
-               fprintf(fp, "%s", rta_getattr_str(rta_tb[IFA_LABEL]));
+               print_string(PRINT_ANY,
+                            "label",
+                            "%s",
+                            rta_getattr_str(rta_tb[IFA_LABEL]));
+
        if (rta_tb[IFA_CACHEINFO]) {
                struct ifa_cacheinfo *ci = RTA_DATA(rta_tb[IFA_CACHEINFO]);
-               fprintf(fp, "%s", _SL_);
-               fprintf(fp, "       valid_lft ");
-               if (ci->ifa_valid == INFINITY_LIFE_TIME)
-                       fprintf(fp, "forever");
-               else
-                       fprintf(fp, "%usec", ci->ifa_valid);
-               fprintf(fp, " preferred_lft ");
-               if (ci->ifa_prefered == INFINITY_LIFE_TIME)
-                       fprintf(fp, "forever");
-               else {
-                       if (deprecated)
-                               fprintf(fp, "%dsec", ci->ifa_prefered);
+
+               print_nl();
+               print_string(PRINT_FP, NULL, "       valid_lft ", NULL);
+
+               if (ci->ifa_valid == INFINITY_LIFE_TIME) {
+                       print_uint(PRINT_JSON,
+                                  "valid_life_time",
+                                  NULL, INFINITY_LIFE_TIME);
+                       print_string(PRINT_FP, NULL, "%s", "forever");
+               } else {
+                       print_uint(PRINT_ANY,
+                                  "valid_life_time", "%usec", ci->ifa_valid);
+               }
+
+               print_string(PRINT_FP, NULL, " preferred_lft ", NULL);
+               if (ci->ifa_prefered == INFINITY_LIFE_TIME) {
+                       print_uint(PRINT_JSON,
+                                  "preferred_life_time",
+                                  NULL, INFINITY_LIFE_TIME);
+                       print_string(PRINT_FP, NULL, "%s", "forever");
+               } else {
+                       if (ifa_flags & IFA_F_DEPRECATED)
+                               print_int(PRINT_ANY,
+                                         "preferred_life_time",
+                                         "%dsec",
+                                         ci->ifa_prefered);
                        else
-                               fprintf(fp, "%usec", ci->ifa_prefered);
+                               print_uint(PRINT_ANY,
+                                          "preferred_life_time",
+                                          "%usec",
+                                          ci->ifa_prefered);
                }
        }
-       fprintf(fp, "\n");
+       print_string(PRINT_FP, NULL, "%s", "\n");
 brief_exit:
        fflush(fp);
        return 0;
 }
 
-struct nlmsg_list
-{
-       struct nlmsg_list *next;
-       struct nlmsghdr   h;
-};
-
-struct nlmsg_chain
-{
-       struct nlmsg_list *head;
-       struct nlmsg_list *tail;
-};
-
 static int print_selected_addrinfo(struct ifinfomsg *ifi,
                                   struct nlmsg_list *ainfo, FILE *fp)
 {
-       for ( ;ainfo ;  ainfo = ainfo->next) {
+       open_json_array(PRINT_JSON, "addr_info");
+       for ( ; ainfo ;  ainfo = ainfo->next) {
                struct nlmsghdr *n = &ainfo->h;
                struct ifaddrmsg *ifa = NLMSG_DATA(n);
 
                if (n->nlmsg_type != RTM_NEWADDR)
                        continue;
 
-               if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifa)))
+               if (n->nlmsg_len < NLMSG_LENGTH(sizeof(*ifa)))
                        return -1;
 
                if (ifa->ifa_index != ifi->ifi_index ||
@@ -1187,23 +1505,26 @@ static int print_selected_addrinfo(struct ifinfomsg *ifi,
                if (filter.up && !(ifi->ifi_flags&IFF_UP))
                        continue;
 
-               print_addrinfo(NULL, n, fp);
+               open_json_object(NULL);
+               print_addrinfo(n, fp);
+               close_json_object();
        }
+       close_json_array(PRINT_JSON, NULL);
+
        if (brief) {
-               fprintf(fp, "\n");
+               print_string(PRINT_FP, NULL, "%s", "\n");
                fflush(fp);
        }
        return 0;
 }
 
 
-static int store_nlmsg(const struct sockaddr_nl *who, struct nlmsghdr *n,
-                      void *arg)
+static int store_nlmsg(struct nlmsghdr *n, void *arg)
 {
        struct nlmsg_chain *lchain = (struct nlmsg_chain *)arg;
        struct nlmsg_list *h;
 
-       h = malloc(n->nlmsg_len+sizeof(void*));
+       h = malloc(n->nlmsg_len+sizeof(void *));
        if (h == NULL)
                return -1;
 
@@ -1216,7 +1537,7 @@ static int store_nlmsg(const struct sockaddr_nl *who, struct nlmsghdr *n,
                lchain->head = h;
        lchain->tail = h;
 
-       ll_remember_index(who, n, NULL);
+       ll_remember_index(n, NULL);
        return 0;
 }
 
@@ -1259,8 +1580,7 @@ static int ipadd_dump_check_magic(void)
        return 0;
 }
 
-static int save_nlmsg(const struct sockaddr_nl *who, struct nlmsghdr *n,
-                      void *arg)
+static int save_nlmsg(struct nlmsghdr *n, void *arg)
 {
        int ret;
 
@@ -1273,27 +1593,39 @@ static int save_nlmsg(const struct sockaddr_nl *who, struct nlmsghdr *n,
        return ret == n->nlmsg_len ? 0 : ret;
 }
 
-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)
 {
        struct ifaddrmsg *ifa = NLMSG_DATA(n);
 
-       printf("if%d:\n", ifa->ifa_index);
-       print_addrinfo(NULL, n, stdout);
+       open_json_object(NULL);
+       print_int(PRINT_ANY, "index", "if%d:\n", ifa->ifa_index);
+       print_addrinfo(n, stdout);
+       close_json_object();
        return 0;
 }
 
 static int ipaddr_showdump(void)
 {
+       int err;
+
        if (ipadd_dump_check_magic())
                exit(-1);
 
-       exit(rtnl_from_file(stdin, &show_handler, NULL));
+       new_json_obj(json);
+       open_json_object(NULL);
+       open_json_array(PRINT_JSON, "addr_info");
+
+       err = rtnl_from_file(stdin, &show_handler, NULL);
+
+       close_json_array(PRINT_JSON, NULL);
+       close_json_object();
+       delete_json_obj();
+
+       exit(err);
 }
 
-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)
 {
        int ret;
@@ -1302,7 +1634,7 @@ static int restore_handler(const struct sockaddr_nl *nl,
 
        ll_init_map(&rth);
 
-       ret = rtnl_talk(&rth, n, n, sizeof(*n));
+       ret = rtnl_talk(&rth, n, NULL);
        if ((ret < 0) && (errno == EEXIST))
                ret = 0;
 
@@ -1317,7 +1649,7 @@ static int ipaddr_restore(void)
        exit(rtnl_from_file(stdin, &restore_handler, NULL));
 }
 
-static void free_nlmsg_chain(struct nlmsg_chain *info)
+void free_nlmsg_chain(struct nlmsg_chain *info)
 {
        struct nlmsg_list *l, *n;
 
@@ -1332,7 +1664,7 @@ static void ipaddr_filter(struct nlmsg_chain *linfo, struct nlmsg_chain *ainfo)
        struct nlmsg_list *l, **lp;
 
        lp = &linfo->head;
-       while ( (l = *lp) != NULL) {
+       while ((l = *lp) != NULL) {
                int ok = 0;
                int missing_net_address = 1;
                struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
@@ -1357,29 +1689,14 @@ static void ipaddr_filter(struct nlmsg_chain *linfo, struct nlmsg_chain *ainfo)
 
                        if ((filter.flags ^ ifa_flags) & filter.flagmask)
                                continue;
-                       if (filter.pfx.family || filter.label) {
-                               if (!tb[IFA_LOCAL])
-                                       tb[IFA_LOCAL] = tb[IFA_ADDRESS];
-
-                               if (filter.pfx.family && tb[IFA_LOCAL]) {
-                                       inet_prefix dst;
-                                       memset(&dst, 0, sizeof(dst));
-                                       dst.family = ifa->ifa_family;
-                                       memcpy(&dst.data, RTA_DATA(tb[IFA_LOCAL]), RTA_PAYLOAD(tb[IFA_LOCAL]));
-                                       if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen))
-                                               continue;
-                               }
-                               if (filter.label) {
-                                       SPRINT_BUF(b1);
-                                       const char *label;
-                                       if (tb[IFA_LABEL])
-                                               label = RTA_DATA(tb[IFA_LABEL]);
-                                       else
-                                               label = ll_idx_n2a(ifa->ifa_index, b1);
-                                       if (fnmatch(filter.label, label, 0) != 0)
-                                               continue;
-                               }
-                       }
+
+                       if (ifa_label_match_rta(ifa->ifa_index, tb[IFA_LABEL]))
+                               continue;
+
+                       if (!tb[IFA_LOCAL])
+                               tb[IFA_LOCAL] = tb[IFA_ADDRESS];
+                       if (inet_addr_match_rta(&filter.pfx, tb[IFA_LOCAL]))
+                               continue;
 
                        ok = 1;
                        break;
@@ -1395,6 +1712,15 @@ static void ipaddr_filter(struct nlmsg_chain *linfo, struct nlmsg_chain *ainfo)
        }
 }
 
+static int ipaddr_dump_filter(struct nlmsghdr *nlh, int reqlen)
+{
+       struct ifaddrmsg *ifa = NLMSG_DATA(nlh);
+
+       ifa->ifa_index = filter.ifindex;
+
+       return 0;
+}
+
 static int ipaddr_flush(void)
 {
        int round = 0;
@@ -1405,7 +1731,8 @@ static int ipaddr_flush(void)
        filter.flushe = sizeof(flushb);
 
        while ((max_flush_loops == 0) || (round < max_flush_loops)) {
-               if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) {
+               if (rtnl_addrdump_req(&rth, filter.family,
+                                     ipaddr_dump_filter) < 0) {
                        perror("Cannot send dump request");
                        exit(1);
                }
@@ -1421,7 +1748,7 @@ static int ipaddr_flush(void)
                                if (round == 0)
                                        printf("Nothing to flush.\n");
                                else
-                                       printf("*** Flush is complete after %d round%s ***\n", round, round>1?"s":"");
+                                       printf("*** Flush is complete after %d round%s ***\n", round, round > 1?"s":"");
                        }
                        fflush(stdout);
                        return 0;
@@ -1448,10 +1775,105 @@ static int ipaddr_flush(void)
        return 1;
 }
 
+static int iplink_filter_req(struct nlmsghdr *nlh, int reqlen)
+{
+       int err;
+
+       err = addattr32(nlh, reqlen, IFLA_EXT_MASK, RTEXT_FILTER_VF);
+       if (err)
+               return err;
+
+       if (filter.master) {
+               err = addattr32(nlh, reqlen, IFLA_MASTER, filter.master);
+               if (err)
+                       return err;
+       }
+
+       if (filter.kind) {
+               struct rtattr *linkinfo;
+
+               linkinfo = addattr_nest(nlh, reqlen, IFLA_LINKINFO);
+
+               err = addattr_l(nlh, reqlen, IFLA_INFO_KIND, filter.kind,
+                               strlen(filter.kind));
+               if (err)
+                       return err;
+
+               addattr_nest_end(nlh, linkinfo);
+       }
+
+       return 0;
+}
+
+static int ipaddr_link_get(int index, struct nlmsg_chain *linfo)
+{
+       struct iplink_req req = {
+               .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
+               .n.nlmsg_flags = NLM_F_REQUEST,
+               .n.nlmsg_type = RTM_GETLINK,
+               .i.ifi_family = filter.family,
+               .i.ifi_index = index,
+       };
+       __u32 filt_mask = RTEXT_FILTER_VF;
+       struct nlmsghdr *answer;
+
+       if (!show_stats)
+               filt_mask |= RTEXT_FILTER_SKIP_STATS;
+
+       addattr32(&req.n, sizeof(req), IFLA_EXT_MASK, filt_mask);
+
+       if (rtnl_talk(&rth, &req.n, &answer) < 0) {
+               perror("Cannot send link request");
+               return 1;
+       }
+
+       if (store_nlmsg(answer, linfo) < 0) {
+               fprintf(stderr, "Failed to process link information\n");
+               return 1;
+       }
+
+       return 0;
+}
+
+/* fills in linfo with link data and optionally ainfo with address info
+ * caller can walk lists as desired and must call free_nlmsg_chain for
+ * both when done
+ */
+int ip_link_list(req_filter_fn_t filter_fn, struct nlmsg_chain *linfo)
+{
+       if (rtnl_linkdump_req_filter_fn(&rth, preferred_family,
+                                       filter_fn) < 0) {
+               perror("Cannot send dump request");
+               return 1;
+       }
+
+       if (rtnl_dump_filter(&rth, store_nlmsg, linfo) < 0) {
+               fprintf(stderr, "Dump terminated\n");
+               return 1;
+       }
+
+       return 0;
+}
+
+static int ip_addr_list(struct nlmsg_chain *ainfo)
+{
+       if (rtnl_addrdump_req(&rth, filter.family, ipaddr_dump_filter) < 0) {
+               perror("Cannot send dump request");
+               return 1;
+       }
+
+       if (rtnl_dump_filter(&rth, store_nlmsg, ainfo) < 0) {
+               fprintf(stderr, "Dump terminated\n");
+               return 1;
+       }
+
+       return 0;
+}
+
 static int ipaddr_list_flush_or_save(int argc, char **argv, int action)
 {
        struct nlmsg_chain linfo = { NULL, NULL};
-       struct nlmsg_chain ainfo = { NULL, NULL};
+       struct nlmsg_chain _ainfo = { NULL, NULL}, *ainfo = &_ainfo;
        struct nlmsg_list *l;
        char *filter_dev = NULL;
        int no_link = 0;
@@ -1459,7 +1881,6 @@ static int ipaddr_list_flush_or_save(int argc, char **argv, int action)
        ipaddr_reset_filter(oneline, 0);
        filter.showqueue = 1;
        filter.family = preferred_family;
-       filter.group = -1;
 
        if (action == IPADD_FLUSH) {
                if (argc <= 0) {
@@ -1476,11 +1897,13 @@ static int ipaddr_list_flush_or_save(int argc, char **argv, int action)
        while (argc > 0) {
                if (strcmp(*argv, "to") == 0) {
                        NEXT_ARG();
-                       get_prefix(&filter.pfx, *argv, filter.family);
+                       if (get_prefix(&filter.pfx, *argv, filter.family))
+                               invarg("invalid \"to\"\n", *argv);
                        if (filter.family == AF_UNSPEC)
                                filter.family = filter.pfx.family;
                } else if (strcmp(*argv, "scope") == 0) {
-                       unsigned scope = 0;
+                       unsigned int scope = 0;
+
                        NEXT_ARG();
                        filter.scopemask = -1;
                        if (rtnl_rtscope_a2n(&scope, *argv)) {
@@ -1492,52 +1915,8 @@ static int ipaddr_list_flush_or_save(int argc, char **argv, int action)
                        filter.scope = scope;
                } else if (strcmp(*argv, "up") == 0) {
                        filter.up = 1;
-               } else if (strcmp(*argv, "dynamic") == 0) {
-                       filter.flags &= ~IFA_F_PERMANENT;
-                       filter.flagmask |= IFA_F_PERMANENT;
-               } else if (strcmp(*argv, "permanent") == 0) {
-                       filter.flags |= IFA_F_PERMANENT;
-                       filter.flagmask |= IFA_F_PERMANENT;
-               } else if (strcmp(*argv, "secondary") == 0 ||
-                          strcmp(*argv, "temporary") == 0) {
-                       filter.flags |= IFA_F_SECONDARY;
-                       filter.flagmask |= IFA_F_SECONDARY;
-               } else if (strcmp(*argv, "primary") == 0) {
-                       filter.flags &= ~IFA_F_SECONDARY;
-                       filter.flagmask |= IFA_F_SECONDARY;
-               } else if (strcmp(*argv, "tentative") == 0) {
-                       filter.flags |= IFA_F_TENTATIVE;
-                       filter.flagmask |= IFA_F_TENTATIVE;
-               } else if (strcmp(*argv, "-tentative") == 0) {
-                       filter.flags &= ~IFA_F_TENTATIVE;
-                       filter.flagmask |= IFA_F_TENTATIVE;
-               } else if (strcmp(*argv, "deprecated") == 0) {
-                       filter.flags |= IFA_F_DEPRECATED;
-                       filter.flagmask |= IFA_F_DEPRECATED;
-               } else if (strcmp(*argv, "-deprecated") == 0) {
-                       filter.flags &= ~IFA_F_DEPRECATED;
-                       filter.flagmask |= IFA_F_DEPRECATED;
-               } else if (strcmp(*argv, "home") == 0) {
-                       filter.flags |= IFA_F_HOMEADDRESS;
-                       filter.flagmask |= IFA_F_HOMEADDRESS;
-               } else if (strcmp(*argv, "nodad") == 0) {
-                       filter.flags |= IFA_F_NODAD;
-                       filter.flagmask |= IFA_F_NODAD;
-               } else if (strcmp(*argv, "mngtmpaddr") == 0) {
-                       filter.flags |= IFA_F_MANAGETEMPADDR;
-                       filter.flagmask |= IFA_F_MANAGETEMPADDR;
-               } else if (strcmp(*argv, "noprefixroute") == 0) {
-                       filter.flags |= IFA_F_NOPREFIXROUTE;
-                       filter.flagmask |= IFA_F_NOPREFIXROUTE;
-               } else if (strcmp(*argv, "autojoin") == 0) {
-                       filter.flags |= IFA_F_MCAUTOJOIN;
-                       filter.flagmask |= IFA_F_MCAUTOJOIN;
-               } else if (strcmp(*argv, "dadfailed") == 0) {
-                       filter.flags |= IFA_F_DADFAILED;
-                       filter.flagmask |= IFA_F_DADFAILED;
-               } else if (strcmp(*argv, "-dadfailed") == 0) {
-                       filter.flags &= ~IFA_F_DADFAILED;
-                       filter.flagmask |= IFA_F_DADFAILED;
+               } else if (get_filter(*argv) == 0) {
+
                } else if (strcmp(*argv, "label") == 0) {
                        NEXT_ARG();
                        filter.label = *argv;
@@ -1547,18 +1926,36 @@ static int ipaddr_list_flush_or_save(int argc, char **argv, int action)
                                invarg("Invalid \"group\" value\n", *argv);
                } else if (strcmp(*argv, "master") == 0) {
                        int ifindex;
+
                        NEXT_ARG();
                        ifindex = ll_name_to_index(*argv);
                        if (!ifindex)
                                invarg("Device does not exist\n", *argv);
                        filter.master = ifindex;
-               } else if (do_link && strcmp(*argv, "type") == 0) {
+               } else if (strcmp(*argv, "vrf") == 0) {
+                       int ifindex;
+
+                       NEXT_ARG();
+                       ifindex = ll_name_to_index(*argv);
+                       if (!ifindex)
+                               invarg("Not a valid VRF name\n", *argv);
+                       if (!name_is_vrf(*argv))
+                               invarg("Not a valid VRF name\n", *argv);
+                       filter.master = ifindex;
+               } else if (strcmp(*argv, "type") == 0) {
+                       int soff;
+
                        NEXT_ARG();
-                       filter.kind = *argv;
+                       soff = strlen(*argv) - strlen("_slave");
+                       if (!strcmp(*argv + soff, "_slave")) {
+                               (*argv)[soff] = '\0';
+                               filter.slave_kind = *argv;
+                       } else {
+                               filter.kind = *argv;
+                       }
                } else {
-                       if (strcmp(*argv, "dev") == 0) {
+                       if (strcmp(*argv, "dev") == 0)
                                NEXT_ARG();
-                       }
                        else if (matches(*argv, "help") == 0)
                                usage();
                        if (filter_dev)
@@ -1583,7 +1980,8 @@ static int ipaddr_list_flush_or_save(int argc, char **argv, int action)
                if (ipadd_save_prep())
                        exit(1);
 
-               if (rtnl_wilddump_request(&rth, preferred_family, RTM_GETADDR) < 0) {
+               if (rtnl_addrdump_req(&rth, preferred_family,
+                                     ipaddr_dump_filter) < 0) {
                        perror("Cannot send dump request");
                        exit(1);
                }
@@ -1596,70 +1994,65 @@ static int ipaddr_list_flush_or_save(int argc, char **argv, int action)
                exit(0);
        }
 
+       /*
+        * Initialize a json_writer and open an array object
+        * if -json was specified.
+        */
+       new_json_obj(json);
+
        /*
         * If only filter_dev present and none of the other
         * link filters are present, use RTM_GETLINK to get
         * the link device
         */
        if (filter_dev && filter.group == -1 && do_link == 1) {
-               if (iplink_get(0, filter_dev, RTEXT_FILTER_VF) < 0) {
+               if (iplink_get(filter_dev, RTEXT_FILTER_VF) < 0) {
                        perror("Cannot send link get request");
+                       delete_json_obj();
                        exit(1);
                }
-               exit(0);
+               delete_json_obj();
+               goto out;
        }
 
-       if (rtnl_wilddump_request(&rth, preferred_family, RTM_GETLINK) < 0) {
-               perror("Cannot send dump request");
-               exit(1);
-       }
-
-       if (rtnl_dump_filter(&rth, store_nlmsg, &linfo) < 0) {
-               fprintf(stderr, "Dump terminated\n");
-               exit(1);
+       if (filter.ifindex) {
+               if (ipaddr_link_get(filter.ifindex, &linfo) != 0)
+                       goto out;
+       } else {
+               if (ip_link_list(iplink_filter_req, &linfo) != 0)
+                       goto out;
        }
 
        if (filter.family != AF_PACKET) {
                if (filter.oneline)
                        no_link = 1;
 
-               if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) {
-                       perror("Cannot send dump request");
-                       exit(1);
-               }
-
-               if (rtnl_dump_filter(&rth, store_nlmsg, &ainfo) < 0) {
-                       fprintf(stderr, "Dump terminated\n");
-                       exit(1);
-               }
+               if (ip_addr_list(ainfo) != 0)
+                       goto out;
 
-               ipaddr_filter(&linfo, &ainfo);
+               ipaddr_filter(&linfo, ainfo);
        }
 
        for (l = linfo.head; l; l = l->next) {
+               struct nlmsghdr *n = &l->h;
+               struct ifinfomsg *ifi = NLMSG_DATA(n);
                int res = 0;
-               struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
 
-               if (brief) {
-                       if (print_linkinfo_brief(NULL, &l->h, stdout) == 0)
-                               if (filter.family != AF_PACKET)
-                                       print_selected_addrinfo(ifi,
-                                                               ainfo.head,
-                                                               stdout);
-               } else if (no_link ||
-                        (res = print_linkinfo(NULL, &l->h, stdout)) >= 0) {
-                       if (filter.family != AF_PACKET)
-                               print_selected_addrinfo(ifi,
-                                                       ainfo.head, stdout);
-                       if (res > 0 && !do_link && show_stats)
-                               print_link_stats(stdout, &l->h);
-               }
+               open_json_object(NULL);
+               if (brief || !no_link)
+                       res = print_linkinfo(n, stdout);
+               if (res >= 0 && filter.family != AF_PACKET)
+                       print_selected_addrinfo(ifi, ainfo->head, stdout);
+               if (res > 0 && !do_link && show_stats)
+                       print_link_stats(stdout, n);
+               close_json_object();
        }
        fflush(stdout);
 
-       free_nlmsg_chain(&ainfo);
+out:
+       free_nlmsg_chain(ainfo);
        free_nlmsg_chain(&linfo);
-
+       delete_json_obj();
        return 0;
 }
 
@@ -1675,6 +2068,12 @@ ipaddr_loop_each_vf(struct rtattr *tb[], int vfnum, int *min, int *max)
 
        for (i = RTA_DATA(vflist); RTA_OK(i, rem); i = RTA_NEXT(i, rem)) {
                parse_rtattr_nested(vf, IFLA_VF_MAX, i);
+
+               if (!vf[IFLA_VF_RATE]) {
+                       fprintf(stderr, "VF min/max rate API not supported\n");
+                       exit(1);
+               }
+
                vf_rate = RTA_DATA(vf[IFLA_VF_RATE]);
                if (vf_rate->vf == vfnum) {
                        *min = vf_rate->min_tx_rate;
@@ -1686,16 +2085,22 @@ ipaddr_loop_each_vf(struct rtattr *tb[], int vfnum, int *min, int *max)
        exit(1);
 }
 
-void ipaddr_get_vf_rate(int vfnum, int *min, int *max, int idx)
+void ipaddr_get_vf_rate(int vfnum, int *min, int *max, const char *dev)
 {
        struct nlmsg_chain linfo = { NULL, NULL};
        struct rtattr *tb[IFLA_MAX+1];
        struct ifinfomsg *ifi;
        struct nlmsg_list *l;
        struct nlmsghdr *n;
-       int len;
+       int idx, len;
 
-       if (rtnl_wilddump_request(&rth, AF_UNSPEC, RTM_GETLINK) < 0) {
+       idx = ll_name_to_index(dev);
+       if (idx == 0) {
+               fprintf(stderr, "Device %s does not exist\n", dev);
+               exit(1);
+       }
+
+       if (rtnl_linkdump_req(&rth, AF_UNSPEC) < 0) {
                perror("Cannot send dump request");
                exit(1);
        }
@@ -1732,12 +2137,13 @@ void ipaddr_reset_filter(int oneline, int ifindex)
        memset(&filter, 0, sizeof(filter));
        filter.oneline = oneline;
        filter.ifindex = ifindex;
+       filter.group = -1;
 }
 
 static int default_scope(inet_prefix *lcl)
 {
        if (lcl->family == AF_INET) {
-               if (lcl->bytelen >= 1 && *(__u8*)&lcl->data == 127)
+               if (lcl->bytelen >= 1 && *(__u8 *)&lcl->data == 127)
                        return RT_SCOPE_HOST;
        }
        return 0;
@@ -1753,19 +2159,34 @@ static bool ipaddr_is_multicast(inet_prefix *a)
                return false;
 }
 
+static bool is_valid_label(const char *dev, const char *label)
+{
+       size_t len = strlen(dev);
+
+       if (strncmp(label, dev, len) != 0)
+               return false;
+
+       return label[len] == '\0' || label[len] == ':';
+}
+
 static int ipaddr_modify(int cmd, int flags, int argc, char **argv)
 {
        struct {
                struct nlmsghdr n;
                struct ifaddrmsg        ifa;
                char                    buf[256];
-       } req;
+       } req = {
+               .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg)),
+               .n.nlmsg_flags = NLM_F_REQUEST | flags,
+               .n.nlmsg_type = cmd,
+               .ifa.ifa_family = preferred_family,
+       };
        char  *d = NULL;
        char  *l = NULL;
        char  *lcl_arg = NULL;
        char  *valid_lftp = NULL;
        char  *preferred_lftp = NULL;
-       inet_prefix lcl;
+       inet_prefix lcl = {};
        inet_prefix peer;
        int local_len = 0;
        int peer_len = 0;
@@ -1774,16 +2195,8 @@ static int ipaddr_modify(int cmd, int flags, int argc, char **argv)
        int scoped = 0;
        __u32 preferred_lft = INFINITY_LIFE_TIME;
        __u32 valid_lft = INFINITY_LIFE_TIME;
-       struct ifa_cacheinfo cinfo;
        unsigned int ifa_flags = 0;
 
-       memset(&req, 0, sizeof(req));
-
-       req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
-       req.n.nlmsg_flags = NLM_F_REQUEST | flags;
-       req.n.nlmsg_type = cmd;
-       req.ifa.ifa_family = preferred_family;
-
        while (argc > 0) {
                if (strcmp(*argv, "peer") == 0 ||
                    strcmp(*argv, "remote") == 0) {
@@ -1800,6 +2213,7 @@ static int ipaddr_modify(int cmd, int flags, int argc, char **argv)
                } else if (matches(*argv, "broadcast") == 0 ||
                           strcmp(*argv, "brd") == 0) {
                        inet_prefix addr;
+
                        NEXT_ARG();
                        if (brd_len)
                                duparg("broadcast", *argv);
@@ -1816,6 +2230,7 @@ static int ipaddr_modify(int cmd, int flags, int argc, char **argv)
                        }
                } else if (strcmp(*argv, "anycast") == 0) {
                        inet_prefix addr;
+
                        NEXT_ARG();
                        if (any_len)
                                duparg("anycast", *argv);
@@ -1825,7 +2240,8 @@ static int ipaddr_modify(int cmd, int flags, int argc, char **argv)
                        addattr_l(&req.n, sizeof(req), IFA_ANYCAST, &addr.data, addr.bytelen);
                        any_len = addr.bytelen;
                } else if (strcmp(*argv, "scope") == 0) {
-                       unsigned scope = 0;
+                       unsigned int scope = 0;
+
                        NEXT_ARG();
                        if (rtnl_rtscope_a2n(&scope, *argv))
                                invarg("invalid scope value.", *argv);
@@ -1838,6 +2254,15 @@ static int ipaddr_modify(int cmd, int flags, int argc, char **argv)
                        NEXT_ARG();
                        l = *argv;
                        addattr_l(&req.n, sizeof(req), IFA_LABEL, l, strlen(l)+1);
+               } else if (matches(*argv, "metric") == 0 ||
+                          matches(*argv, "priority") == 0 ||
+                          matches(*argv, "preference") == 0) {
+                       __u32 metric;
+
+                       NEXT_ARG();
+                       if (get_u32(&metric, *argv, 0))
+                               invarg("\"metric\" value is invalid\n", *argv);
+                       addattr32(&req.n, sizeof(req), IFA_RT_PRIORITY, metric);
                } else if (matches(*argv, "valid_lft") == 0) {
                        if (valid_lftp)
                                duparg("valid_lft", *argv);
@@ -1853,19 +2278,27 @@ static int ipaddr_modify(int cmd, int flags, int argc, char **argv)
                        if (set_lifetime(&preferred_lft, *argv))
                                invarg("preferred_lft value", *argv);
                } else if (strcmp(*argv, "home") == 0) {
-                       ifa_flags |= IFA_F_HOMEADDRESS;
+                       if (req.ifa.ifa_family == AF_INET6)
+                               ifa_flags |= IFA_F_HOMEADDRESS;
+                       else
+                               fprintf(stderr, "Warning: home option can be set only for IPv6 addresses\n");
                } else if (strcmp(*argv, "nodad") == 0) {
-                       ifa_flags |= IFA_F_NODAD;
+                       if (req.ifa.ifa_family == AF_INET6)
+                               ifa_flags |= IFA_F_NODAD;
+                       else
+                               fprintf(stderr, "Warning: nodad option can be set only for IPv6 addresses\n");
                } else if (strcmp(*argv, "mngtmpaddr") == 0) {
-                       ifa_flags |= IFA_F_MANAGETEMPADDR;
+                       if (req.ifa.ifa_family == AF_INET6)
+                               ifa_flags |= IFA_F_MANAGETEMPADDR;
+                       else
+                               fprintf(stderr, "Warning: mngtmpaddr option can be set only for IPv6 addresses\n");
                } else if (strcmp(*argv, "noprefixroute") == 0) {
                        ifa_flags |= IFA_F_NOPREFIXROUTE;
                } else if (strcmp(*argv, "autojoin") == 0) {
                        ifa_flags |= IFA_F_MCAUTOJOIN;
                } else {
-                       if (strcmp(*argv, "local") == 0) {
+                       if (strcmp(*argv, "local") == 0)
                                NEXT_ARG();
-                       }
                        if (matches(*argv, "help") == 0)
                                usage();
                        if (local_len)
@@ -1888,17 +2321,19 @@ static int ipaddr_modify(int cmd, int flags, int argc, char **argv)
                fprintf(stderr, "Not enough information: \"dev\" argument is required.\n");
                return -1;
        }
-       if (l && matches(d, l) != 0) {
-               fprintf(stderr, "\"dev\" (%s) must match \"label\" (%s).\n", d, l);
+       if (l && !is_valid_label(d, l)) {
+               fprintf(stderr,
+                       "\"label\" (%s) must match \"dev\" (%s) or be prefixed by \"dev\" with a colon.\n",
+                       l, d);
                return -1;
        }
 
        if (peer_len == 0 && local_len) {
                if (cmd == RTM_DELADDR && lcl.family == AF_INET && !(lcl.flags & PREFIXLEN_SPECIFIED)) {
                        fprintf(stderr,
-                           "Warning: Executing wildcard deletion to stay compatible with old scripts.\n" \
-                           "         Explicitly specify the prefix length (%s/%d) to avoid this warning.\n" \
-                           "         This special behaviour is likely to disappear in further releases,\n" \
+                           "Warning: Executing wildcard deletion to stay compatible with old scripts.\n"
+                           "         Explicitly specify the prefix length (%s/%d) to avoid this warning.\n"
+                           "         This special behaviour is likely to disappear in further releases,\n"
                            "         fix your scripts!\n", lcl_arg, local_len*8);
                } else {
                        peer = lcl;
@@ -1911,6 +2346,7 @@ static int ipaddr_modify(int cmd, int flags, int argc, char **argv)
        if (brd_len < 0 && cmd != RTM_DELADDR) {
                inet_prefix brd;
                int i;
+
                if (req.ifa.ifa_family != AF_INET) {
                        fprintf(stderr, "Broadcast can be set only for IPv4 addresses\n");
                        return -1;
@@ -1930,12 +2366,13 @@ static int ipaddr_modify(int cmd, int flags, int argc, char **argv)
        if (!scoped && cmd != RTM_DELADDR)
                req.ifa.ifa_scope = default_scope(&lcl);
 
-       if ((req.ifa.ifa_index = ll_name_to_index(d)) == 0) {
-               fprintf(stderr, "Cannot find device \"%s\"\n", d);
-               return -1;
-       }
+       req.ifa.ifa_index = ll_name_to_index(d);
+       if (!req.ifa.ifa_index)
+               return nodev(d);
 
        if (valid_lftp || preferred_lftp) {
+               struct ifa_cacheinfo cinfo = {};
+
                if (!valid_lft) {
                        fprintf(stderr, "valid_lft is zero\n");
                        return -1;
@@ -1945,7 +2382,6 @@ static int ipaddr_modify(int cmd, int flags, int argc, char **argv)
                        return -1;
                }
 
-               memset(&cinfo, 0, sizeof(cinfo));
                cinfo.ifa_prefered = preferred_lft;
                cinfo.ifa_valid = valid_lft;
                addattr_l(&req.n, sizeof(req), IFA_CACHEINFO, &cinfo,
@@ -1957,7 +2393,7 @@ static int ipaddr_modify(int cmd, int flags, int argc, char **argv)
                return -1;
        }
 
-       if (rtnl_talk(&rth, &req.n, NULL, 0) < 0)
+       if (rtnl_talk(&rth, &req.n, NULL) < 0)
                return -2;
 
        return 0;