system-linux: fix the default broadcast address for v4
[project/netifd.git] / system-linux.c
index aa3aeda..0a989b0 100644 (file)
@@ -807,7 +807,8 @@ system_if_dump_stats(struct device *dev, struct blob_buf *b)
 
 static int system_addr(struct device *dev, struct device_addr *addr, int cmd)
 {
-       int alen = ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET4) ? 4 : 16;
+       bool v4 = ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET4);
+       int alen = v4 ? 4 : 16;
        struct ifaddrmsg ifa = {
                .ifa_family = (alen == 4) ? AF_INET : AF_INET6,
                .ifa_prefixlen = addr->mask,
@@ -816,20 +817,20 @@ static int system_addr(struct device *dev, struct device_addr *addr, int cmd)
 
        struct nl_msg *msg;
 
-       dev = addr->device;
-       if (dev) {
-               if (!dev->ifindex)
-                       return -1;
-
-               ifa.ifa_index = dev->ifindex;
-       }
-
        msg = nlmsg_alloc_simple(cmd, 0);
        if (!msg)
                return -1;
 
        nlmsg_append(msg, &ifa, sizeof(ifa), 0);
        nla_put(msg, IFA_LOCAL, alen, &addr->addr);
+       if (v4) {
+               uint32_t mask = ~0;
+               uint32_t *a = (uint32_t *) &addr->addr;
+
+               mask >>= addr->mask;
+               nla_put_u32(msg, IFA_BROADCAST, *a | mask);
+       }
+
        return system_rtnl_call(msg);
 }
 
@@ -874,14 +875,6 @@ static int system_rt(struct device *dev, struct device_route *route, int cmd)
        if (cmd == RTM_NEWROUTE)
                flags |= NLM_F_CREATE | NLM_F_REPLACE;
 
-       dev = route->device;
-       if (dev) {
-               if (!dev->ifindex)
-                       return -1;
-
-               ifindex = dev->ifindex;
-       }
-
        msg = nlmsg_alloc_simple(cmd, flags);
        if (!msg)
                return -1;
@@ -891,6 +884,9 @@ static int system_rt(struct device *dev, struct device_route *route, int cmd)
        if (route->mask)
                nla_put(msg, RTA_DST, alen, &route->addr);
 
+       if (route->metric >= 0)
+               nla_put_u32(msg, RTA_PRIORITY, route->metric);
+
        if (have_gw)
                nla_put(msg, RTA_GATEWAY, alen, &route->nexthop);