]> git.ipfire.org Git - thirdparty/iproute2.git/commitdiff
utils: Introduce and use get_ifname_rta()
authorSerhey Popovych <serhe.popovych@gmail.com>
Thu, 15 Feb 2018 21:23:22 +0000 (23:23 +0200)
committerDavid Ahern <dsahern@gmail.com>
Fri, 16 Feb 2018 16:14:20 +0000 (08:14 -0800)
Be consistent in handling of IFLA_IFNAME attribute in all places: if
there is no attribute report bug to stderr and use ll_idx_n2a() as
last measure to get name in "if%u" format instead of "<nil>".

Use check_ifname() to validate network device name: this catches both
unexpected return from kernel and ll_idx_n2a().

Signed-off-by: Serhey Popovych <serhe.popovych@gmail.com>
Signed-off-by: David Ahern <dsahern@gmail.com>
bridge/link.c
include/utils.h
ip/ipaddress.c
lib/utils.c

index 870ebe0504777bdede5d55c4cbfb91a6a048f955..a11cbb11a356335af80678824051e4e7854ed565 100644 (file)
@@ -99,9 +99,10 @@ int print_linkinfo(const struct sockaddr_nl *who,
                   struct nlmsghdr *n, void *arg)
 {
        FILE *fp = arg;
-       int len = n->nlmsg_len;
        struct ifinfomsg *ifi = NLMSG_DATA(n);
        struct rtattr *tb[IFLA_MAX+1];
+       int len = n->nlmsg_len;
+       const char *name;
 
        len -= NLMSG_LENGTH(sizeof(*ifi));
        if (len < 0) {
@@ -117,10 +118,9 @@ int print_linkinfo(const struct sockaddr_nl *who,
 
        parse_rtattr_flags(tb, IFLA_MAX, IFLA_RTA(ifi), len, NLA_F_NESTED);
 
-       if (tb[IFLA_IFNAME] == NULL) {
-               fprintf(stderr, "BUG: nil ifname\n");
+       name = get_ifname_rta(ifi->ifi_index, tb[IFLA_IFNAME]);
+       if (!name)
                return -1;
-       }
 
        if (n->nlmsg_type == RTM_DELLINK)
                fprintf(fp, "Deleted ");
index 867e5404c95404702c2905d784c9741e8251bda8..84ca873ecbb7d19a564bc126d594c8818d99fa35 100644 (file)
@@ -183,6 +183,7 @@ void duparg(const char *, const char *) __attribute__((noreturn));
 void duparg2(const char *, const char *) __attribute__((noreturn));
 int check_ifname(const char *);
 int get_ifname(char *, const char *);
+const char *get_ifname_rta(int ifindex, const struct rtattr *rta);
 int matches(const char *arg, const char *pattern);
 int inet_addr_match(const inet_prefix *a, const inet_prefix *b, int bits);
 int inet_addr_match_rta(const inet_prefix *m, const struct rtattr *rta);
index 749178ded145a999355522e70458b90f44f8bee1..08d2576b79e25bdf470544e89421c7bedff6a20d 100644 (file)
@@ -776,12 +776,10 @@ int print_linkinfo_brief(const struct sockaddr_nl *who,
                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);
-               name = ll_idx_n2a(ifi->ifi_index);
-       } else {
-               name = rta_getattr_str(tb[IFLA_IFNAME]);
-       }
+
+       name = get_ifname_rta(ifi->ifi_index, tb[IFLA_IFNAME]);
+       if (!name)
+               return -1;
 
        if (filter.label &&
            (!filter.family || filter.family == AF_PACKET) &&
@@ -903,12 +901,10 @@ int print_linkinfo(const struct sockaddr_nl *who,
                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);
-               name = ll_idx_n2a(ifi->ifi_index);
-       } else {
-               name = rta_getattr_str(tb[IFLA_IFNAME]);
-       }
+
+       name = get_ifname_rta(ifi->ifi_index, tb[IFLA_IFNAME]);
+       if (!name)
+               return -1;
 
        if (filter.label &&
            (!filter.family || filter.family == AF_PACKET) &&
index cbe5d8de8f594f3ca60c066ec1aca847e3bcfab2..6a62c1edb7bf0598848c9203f2864317360e2b1c 100644 (file)
@@ -872,6 +872,25 @@ int get_ifname(char *buf, const char *name)
        return ret;
 }
 
+const char *get_ifname_rta(int ifindex, const struct rtattr *rta)
+{
+       const char *name;
+
+       if (rta) {
+               name = rta_getattr_str(rta);
+       } else {
+               fprintf(stderr,
+                       "BUG: device with ifindex %d has nil ifname\n",
+                       ifindex);
+               name = ll_idx_n2a(ifindex);
+       }
+
+       if (check_ifname(name))
+               return NULL;
+
+       return name;
+}
+
 int matches(const char *cmd, const char *pattern)
 {
        int len = strlen(cmd);