]> git.proxmox.com Git - mirror_iproute2.git/blobdiff - ip/iptuntap.c
ipnetns: parse nsid as a signed integer
[mirror_iproute2.git] / ip / iptuntap.c
index 4628db2832b4a0ca767819ec60297e2bb5d74ee6..528055a0bf46440ca3c3d7514ce715c37b18f70d 100644 (file)
@@ -228,24 +228,35 @@ static int do_del(int argc, char **argv)
 
 static void print_flags(long flags)
 {
+       open_json_array(PRINT_JSON, "flags");
+
        if (flags & IFF_TUN)
-               printf(" tun");
+               print_string(PRINT_ANY, NULL, " %s", "tun");
 
        if (flags & IFF_TAP)
-               printf(" tap");
+               print_string(PRINT_ANY, NULL, " %s", "tap");
 
        if (!(flags & IFF_NO_PI))
-               printf(" pi");
+               print_string(PRINT_ANY, NULL, " %s", "pi");
 
        if (flags & IFF_ONE_QUEUE)
-               printf(" one_queue");
+               print_string(PRINT_ANY, NULL, " %s", "one_queue");
 
        if (flags & IFF_VNET_HDR)
-               printf(" vnet_hdr");
+               print_string(PRINT_ANY, NULL, " %s", "vnet_hdr");
+
+       if (flags & IFF_PERSIST)
+               print_string(PRINT_ANY, NULL, " %s", "persist");
+
+       if (!(flags & IFF_NOFILTER))
+               print_string(PRINT_ANY, NULL, " %s", "filter");
 
-       flags &= ~(IFF_TUN|IFF_TAP|IFF_NO_PI|IFF_ONE_QUEUE|IFF_VNET_HDR);
+       flags &= ~(IFF_TUN | IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE |
+                  IFF_VNET_HDR | IFF_PERSIST | IFF_NOFILTER);
        if (flags)
-               printf(" UNKNOWN_FLAGS:%lx", flags);
+               print_0xhex(PRINT_ANY, NULL, "%#x", flags);
+
+       close_json_array(PRINT_JSON, NULL);
 }
 
 static char *pid_name(pid_t pid)
@@ -288,6 +299,8 @@ static void show_processes(const char *name)
        if (err)
                return;
 
+       open_json_array(PRINT_JSON, "processes");
+
        fd_path = globbuf.gl_pathv;
        while (*fd_path) {
                const char *dev_net_tun = "/dev/net/tun";
@@ -334,7 +347,11 @@ static void show_processes(const char *name)
                                   !strcmp(name, value)) {
                                char *pname = pid_name(pid);
 
-                               printf(" %s(%d)", pname ? : "<NULL>", pid);
+                               print_string(PRINT_ANY, "name",
+                                            "%s", pname ? : "<NULL>");
+
+                               print_uint(PRINT_ANY, "pid",
+                                          "(%d)", pid);
                                free(pname);
                        }
 
@@ -347,6 +364,7 @@ static void show_processes(const char *name)
 next:
                ++fd_path;
        }
+       close_json_array(PRINT_JSON, NULL);
 
        globfree(&globbuf);
 }
@@ -368,8 +386,7 @@ static int tuntap_filter_req(struct nlmsghdr *nlh, int reqlen)
        return 0;
 }
 
-static int print_tuntap(const struct sockaddr_nl *who,
-                       struct nlmsghdr *n, void *arg)
+static int print_tuntap(struct nlmsghdr *n, void *arg)
 {
        struct ifinfomsg *ifi = NLMSG_DATA(n);
        struct rtattr *tb[IFLA_MAX+1];
@@ -417,35 +434,46 @@ static int print_tuntap(const struct sockaddr_nl *who,
        if (read_prop(name, "group", &group))
                return 0;
 
-       printf("%s:", name);
+       open_json_object(NULL);
+       print_color_string(PRINT_ANY, COLOR_IFNAME,
+                          "ifname", "%s:", name);
        print_flags(flags);
        if (owner != -1)
-               printf(" user %ld", owner);
+               print_u64(PRINT_ANY, "user",
+                          " user %ld", owner);
        if (group != -1)
-               printf(" group %ld", group);
-       fputc('\n', stdout);
+               print_u64(PRINT_ANY, "group",
+                          " group %ld", group);
+
        if (show_details) {
-               printf("\tAttached to processes:");
+               print_string(PRINT_FP, NULL,
+                            "%s\tAttached to processes:", _SL_);
                show_processes(name);
-               fputc('\n', stdout);
        }
+       close_json_object();
+       print_string(PRINT_FP, NULL, "%s", "\n");
 
        return 0;
 }
 
 static int do_show(int argc, char **argv)
 {
-       if (rtnl_wilddump_req_filter_fn(&rth, AF_UNSPEC, RTM_GETLINK,
+       if (rtnl_linkdump_req_filter_fn(&rth, AF_UNSPEC,
                                        tuntap_filter_req) < 0) {
                perror("Cannot send dump request\n");
                return -1;
        }
 
+       new_json_obj(json);
+
        if (rtnl_dump_filter(&rth, print_tuntap, NULL) < 0) {
                fprintf(stderr, "Dump terminated\n");
                return -1;
        }
 
+       delete_json_obj();
+       fflush(stdout);
+
        return 0;
 }
 
@@ -469,3 +497,102 @@ int do_iptuntap(int argc, char **argv)
                *argv);
        exit(-1);
 }
+
+static void print_owner(FILE *f, uid_t uid)
+{
+       struct passwd *pw = getpwuid(uid);
+
+       if (pw)
+               print_string(PRINT_ANY, "user", "user %s ", pw->pw_name);
+       else
+               print_uint(PRINT_ANY, "user", "user %u ", uid);
+}
+
+static void print_group(FILE *f, gid_t gid)
+{
+       struct group *group = getgrgid(gid);
+
+       if (group)
+               print_string(PRINT_ANY, "group", "group %s ", group->gr_name);
+       else
+               print_uint(PRINT_ANY, "group", "group %u ", gid);
+}
+
+static void print_mq(FILE *f, struct rtattr *tb[])
+{
+       if (!tb[IFLA_TUN_MULTI_QUEUE] ||
+           !rta_getattr_u8(tb[IFLA_TUN_MULTI_QUEUE])) {
+               if (is_json_context())
+                       print_bool(PRINT_JSON, "multi_queue", NULL, false);
+               return;
+       }
+
+       print_bool(PRINT_ANY, "multi_queue", "multi_queue ", true);
+
+       if (tb[IFLA_TUN_NUM_QUEUES]) {
+               print_uint(PRINT_ANY, "numqueues", "numqueues %u ",
+                          rta_getattr_u32(tb[IFLA_TUN_NUM_QUEUES]));
+       }
+
+       if (tb[IFLA_TUN_NUM_DISABLED_QUEUES]) {
+               print_uint(PRINT_ANY, "numdisabled", "numdisabled %u ",
+                          rta_getattr_u32(tb[IFLA_TUN_NUM_DISABLED_QUEUES]));
+       }
+}
+
+static void print_onoff(FILE *f, const char *flag, __u8 val)
+{
+       if (is_json_context())
+               print_bool(PRINT_JSON, flag, NULL, !!val);
+       else
+               fprintf(f, "%s %s ", flag, val ? "on" : "off");
+}
+
+static void print_type(FILE *f, __u8 type)
+{
+       SPRINT_BUF(buf);
+       const char *str = buf;
+
+       if (type == IFF_TUN)
+               str = "tun";
+       else if (type == IFF_TAP)
+               str = "tap";
+       else
+               snprintf(buf, sizeof(buf), "UNKNOWN:%hhu", type);
+
+       print_string(PRINT_ANY, "type", "type %s ", str);
+}
+
+static void tun_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
+{
+       if (!tb)
+               return;
+
+       if (tb[IFLA_TUN_TYPE])
+               print_type(f, rta_getattr_u8(tb[IFLA_TUN_TYPE]));
+
+       if (tb[IFLA_TUN_PI])
+               print_onoff(f, "pi", rta_getattr_u8(tb[IFLA_TUN_PI]));
+
+       if (tb[IFLA_TUN_VNET_HDR]) {
+               print_onoff(f, "vnet_hdr",
+                           rta_getattr_u8(tb[IFLA_TUN_VNET_HDR]));
+       }
+
+       print_mq(f, tb);
+
+       if (tb[IFLA_TUN_PERSIST])
+               print_onoff(f, "persist", rta_getattr_u8(tb[IFLA_TUN_PERSIST]));
+
+       if (tb[IFLA_TUN_OWNER])
+               print_owner(f, rta_getattr_u32(tb[IFLA_TUN_OWNER]));
+
+       if (tb[IFLA_TUN_GROUP])
+               print_group(f, rta_getattr_u32(tb[IFLA_TUN_GROUP]));
+}
+
+struct link_util tun_link_util = {
+       .id = "tun",
+       .maxattr = IFLA_TUN_MAX,
+       .print_opt = tun_print_opt,
+};