do not clear device state for devices created by proto-up with address-external set
[project/netifd.git] / system-linux.c
index 3d6230d..44332a2 100644 (file)
@@ -370,6 +370,9 @@ void system_if_clear_state(struct device *dev)
        char buf[256];
        char *bridge;
 
+       if (dev->external)
+               return;
+
        dev->ifindex = system_if_resolve(dev);
        if (!dev->ifindex)
                return;
@@ -470,9 +473,32 @@ int system_vlan_del(struct device *dev)
        return system_vlan(dev, -1);
 }
 
-int system_if_up(struct device *dev)
+static void
+system_if_apply_settings(struct device *dev)
 {
+       struct ifreq ifr;
+
+       memset(&ifr, 0, sizeof(ifr));
+       strncpy(ifr.ifr_name, dev->ifname, sizeof(ifr.ifr_name));
+       if (dev->flags & DEV_OPT_MTU) {
+               ifr.ifr_mtu = dev->mtu;
+               ioctl(sock_ioctl, SIOCSIFMTU, &ifr);
+       }
+       if (dev->flags & DEV_OPT_TXQUEUELEN) {
+               ifr.ifr_qlen = dev->txqueuelen;
+               ioctl(sock_ioctl, SIOCSIFTXQLEN, &ifr);
+       }
+       if (dev->flags & DEV_OPT_MACADDR) {
+               memcpy(&ifr.ifr_hwaddr, dev->macaddr, sizeof(dev->macaddr));
+               ioctl(sock_ioctl, SIOCSIFHWADDR, &ifr);
+       }
+
        dev->ifindex = system_if_resolve(dev);
+}
+
+int system_if_up(struct device *dev)
+{
+       system_if_apply_settings(dev);
        return system_if_flags(dev->ifname, IFF_UP, 0);
 }
 
@@ -539,7 +565,17 @@ static int system_addr(struct device *dev, struct device_addr *addr, int cmd)
                .ifa_index = dev->ifindex,
        };
 
-       struct nl_msg *msg = nlmsg_alloc_simple(cmd, 0);
+       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;
 
@@ -563,6 +599,7 @@ static int system_rt(struct device *dev, struct device_route *route, int cmd)
        int alen = ((route->flags & DEVADDR_FAMILY) == DEVADDR_INET4) ? 4 : 16;
        bool have_gw;
        unsigned int flags = 0;
+       int ifindex = dev->ifindex;
 
        if (alen == 4)
                have_gw = !!route->nexthop.in.s_addr;
@@ -583,11 +620,20 @@ static int system_rt(struct device *dev, struct device_route *route, int cmd)
                .rtm_scope = scope,
                .rtm_type = (cmd == RTM_DELROUTE) ? 0: RTN_UNICAST,
        };
+       struct nl_msg *msg;
 
        if (cmd == RTM_NEWROUTE)
                flags |= NLM_F_CREATE | NLM_F_REPLACE;
 
-       struct nl_msg *msg = nlmsg_alloc_simple(cmd, flags);
+       dev = route->device;
+       if (dev) {
+               if (!dev->ifindex)
+                       return -1;
+
+               ifindex = dev->ifindex;
+       }
+
+       msg = nlmsg_alloc_simple(cmd, flags);
        if (!msg)
                return -1;
 
@@ -600,7 +646,7 @@ static int system_rt(struct device *dev, struct device_route *route, int cmd)
                nla_put(msg, RTA_GATEWAY, alen, &route->nexthop);
 
        if (route->flags & DEVADDR_DEVICE)
-               nla_put_u32(msg, RTA_OIF, dev->ifindex);
+               nla_put_u32(msg, RTA_OIF, ifindex);
 
        return system_rtnl_call(msg);
 }