netifd: Fix device ifindex overwrite when processing netlink event messages
[project/netifd.git] / system-linux.c
index 7955cec..6a6028c 100644 (file)
@@ -299,16 +299,14 @@ static int system_get_disable_ipv6(struct device *dev, char *buf, const size_t b
                        dev->ifname, buf, buf_sz);
 }
 
-#ifndef IFF_LOWER_UP
-#define IFF_LOWER_UP   0x10000
-#endif
-
 // Evaluate netlink messages
 static int cb_rtnl_event(struct nl_msg *msg, void *arg)
 {
        struct nlmsghdr *nh = nlmsg_hdr(msg);
        struct ifinfomsg *ifi = NLMSG_DATA(nh);
        struct nlattr *nla[__IFLA_MAX];
+       int link_state = 0;
+       char buf[10];
 
        if (nh->nlmsg_type != RTM_NEWLINK)
                goto out;
@@ -318,11 +316,13 @@ static int cb_rtnl_event(struct nl_msg *msg, void *arg)
                goto out;
 
        struct device *dev = device_get(nla_data(nla[IFLA_IFNAME]), false);
-       if (!dev)
+       if (!dev || dev->type->keep_link_status)
                goto out;
 
-       device_set_ifindex(dev, ifi->ifi_index);
-       device_set_link(dev, ifi->ifi_flags & IFF_LOWER_UP ? true : false);
+       if (!system_get_dev_sysctl("/sys/class/net/%s/carrier", dev->ifname, buf, sizeof(buf)))
+               link_state = strtoul(buf, NULL, 0);
+
+       device_set_link(dev, link_state ? true : false);
 
 out:
        return 0;
@@ -442,17 +442,18 @@ static bool system_is_bridge(const char *name, char *buf, int buflen)
 static char *system_get_bridge(const char *name, char *buf, int buflen)
 {
        char *path;
-       ssize_t len;
+       ssize_t len = -1;
        glob_t gl;
 
        snprintf(buf, buflen, "/sys/devices/virtual/net/*/brif/%s/bridge", name);
        if (glob(buf, GLOB_NOSORT, NULL, &gl) < 0)
                return NULL;
 
-       if (gl.gl_pathc == 0)
-               return NULL;
+       if (gl.gl_pathc > 0)
+               len = readlink(gl.gl_pathv[0], buf, buflen);
+
+       globfree(&gl);
 
-       len = readlink(gl.gl_pathv[0], buf, buflen);
        if (len < 0)
                return NULL;
 
@@ -492,7 +493,7 @@ int system_bridge_delif(struct device *bridge, struct device *dev)
        return system_bridge_if(bridge->ifname, dev, SIOCBRDELIF, NULL);
 }
 
-static int system_if_resolve(struct device *dev)
+int system_if_resolve(struct device *dev)
 {
        struct ifreq ifr;
        strncpy(ifr.ifr_name, dev->ifname, sizeof(ifr.ifr_name));
@@ -1001,7 +1002,6 @@ int system_if_up(struct device *dev)
 {
        system_if_get_settings(dev, &dev->orig_settings);
        system_if_apply_settings(dev, &dev->settings, dev->settings.flags);
-       device_set_ifindex(dev, system_if_resolve(dev));
        return system_if_flags(dev->ifname, IFF_UP, 0);
 }
 
@@ -1019,6 +1019,10 @@ struct if_check_data {
        int ret;
 };
 
+#ifndef IFF_LOWER_UP
+#define IFF_LOWER_UP   0x10000
+#endif
+
 static int cb_if_check_valid(struct nl_msg *msg, void *arg)
 {
        struct nlmsghdr *nh = nlmsg_hdr(msg);