]> git.proxmox.com Git - mirror_iproute2.git/commitdiff
iplink: Improve index parameter handling
authorSerhey Popovych <serhe.popovych@gmail.com>
Mon, 18 Dec 2017 18:54:06 +0000 (20:54 +0200)
committerStephen Hemminger <stephen@networkplumber.org>
Tue, 19 Dec 2017 16:11:38 +0000 (08:11 -0800)
Correctly check for valid network device index supplied on
command line: indexes are always greather than zero. Check
for duplicate "index" argument.

Initialize @index to 0 to simplify handling it in iplink_modify().
Other callers (link_veth.c, iplink_vxcan.c) already did so.

No need to initialize ifi_index with 0 since it is already
initialized at the @struct req initialization time and not
modified in iplink_parse().

Signed-off-by: Serhey Popovych <serhe.popovych@gmail.com>
Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
ip/iplink.c

index 1e685cc23805060a688152fa2f137cd401935854..4f9c169a790e920441c074765510c3ec27b6bc5d 100644 (file)
@@ -586,8 +586,10 @@ int iplink_parse(int argc, char **argv, struct iplink_req *req,
                        *name = *argv;
                } else if (strcmp(*argv, "index") == 0) {
                        NEXT_ARG();
+                       if (*index)
+                               duparg("index", *argv);
                        *index = atoi(*argv);
-                       if (*index < 0)
+                       if (*index <= 0)
                                invarg("Invalid \"index\" value", *argv);
                } else if (matches(*argv, "link") == 0) {
                        NEXT_ARG();
@@ -886,7 +888,7 @@ static int iplink_modify(int cmd, unsigned int flags, int argc, char **argv)
        char *name = NULL;
        char *link = NULL;
        char *type = NULL;
-       int index = -1;
+       int index = 0;
        int group;
        struct link_util *lu = NULL;
        struct iplink_req req = {
@@ -922,7 +924,6 @@ static int iplink_modify(int cmd, unsigned int flags, int argc, char **argv)
                                return -1;
                        }
 
-                       req.i.ifi_index = 0;
                        addattr32(&req.n, sizeof(req), IFLA_GROUP, group);
                        if (rtnl_talk(&rth, &req.n, NULL) < 0)
                                return -2;
@@ -936,7 +937,7 @@ static int iplink_modify(int cmd, unsigned int flags, int argc, char **argv)
                                "Not enough information: \"dev\" argument is required.\n");
                        exit(-1);
                }
-               if (cmd == RTM_NEWLINK && index != -1) {
+               if (cmd == RTM_NEWLINK && index) {
                        fprintf(stderr,
                                "index can be used only when creating devices.\n");
                        exit(-1);
@@ -964,10 +965,7 @@ static int iplink_modify(int cmd, unsigned int flags, int argc, char **argv)
                        addattr_l(&req.n, sizeof(req), IFLA_LINK, &ifindex, 4);
                }
 
-               if (index == -1)
-                       req.i.ifi_index = 0;
-               else
-                       req.i.ifi_index = index;
+               req.i.ifi_index = index;
        }
 
        if (name) {