wispr: Service does not need to be reference counted
[platform/upstream/connman.git] / src / service.c
index 018f12f..3417e89 100644 (file)
@@ -44,6 +44,7 @@ static GHashTable *service_hash = NULL;
 static GSList *counter_list = NULL;
 static unsigned int autoconnect_timeout = 0;
 static struct connman_service *current_default = NULL;
+static connman_bool_t services_dirty = FALSE;
 
 struct connman_stats {
        connman_bool_t valid;
@@ -104,6 +105,7 @@ struct connman_service {
        char *private_key_passphrase;
        char *phase2;
        DBusMessage *pending;
+       DBusMessage *provider_pending;
        guint timeout;
        struct connman_stats stats;
        struct connman_stats stats_roaming;
@@ -117,8 +119,13 @@ struct connman_service {
        int online_check_count;
        connman_bool_t do_split_routing;
        connman_bool_t new_service;
+       connman_bool_t hidden_service;
+       char *config_file;
+       char *config_entry;
 };
 
+static connman_bool_t allow_property_changed(struct connman_service *service);
+
 struct find_data {
        const char *path;
        struct connman_service *service;
@@ -486,6 +493,9 @@ static int service_load(struct connman_service *service)
                service->pac = str;
        }
 
+       service->hidden_service = g_key_file_get_boolean(keyfile,
+                                       service->identifier, "Hidden", NULL);
+
 done:
        g_key_file_free(keyfile);
 
@@ -533,23 +543,23 @@ static int service_save(struct connman_service *service)
 
                        if (ssid != NULL && ssid_len > 0 && ssid[0] != '\0') {
                                char *identifier = service->identifier;
-                               GString *str;
+                               GString *ssid_str;
                                unsigned int i;
 
-                               str = g_string_sized_new(ssid_len * 2);
-                               if (str == NULL) {
+                               ssid_str = g_string_sized_new(ssid_len * 2);
+                               if (ssid_str == NULL) {
                                        err = -ENOMEM;
                                        goto done;
                                }
 
                                for (i = 0; i < ssid_len; i++)
-                                       g_string_append_printf(str,
+                                       g_string_append_printf(ssid_str,
                                                        "%02x", ssid[i]);
 
                                g_key_file_set_string(keyfile, identifier,
-                                                       "SSID", str->str);
+                                                       "SSID", ssid_str->str);
 
-                               g_string_free(str, TRUE);
+                               g_string_free(ssid_str, TRUE);
                        }
 
                        freq = connman_network_get_frequency(service->network);
@@ -668,6 +678,19 @@ static int service_save(struct connman_service *service)
                g_key_file_remove_key(keyfile, service->identifier,
                                                        "Proxy.URL", NULL);
 
+       if (service->hidden_service == TRUE)
+               g_key_file_set_boolean(keyfile, service->identifier, "Hidden",
+                                                                       TRUE);
+
+       if (service->config_file != NULL && strlen(service->config_file) > 0)
+               g_key_file_set_string(keyfile, service->identifier,
+                               "Config.file", service->config_file);
+
+       if (service->config_entry != NULL &&
+                                       strlen(service->config_entry) > 0)
+               g_key_file_set_string(keyfile, service->identifier,
+                               "Config.ident", service->config_entry);
+
 done:
        __connman_storage_save_service(keyfile, service->identifier);
 
@@ -676,6 +699,11 @@ done:
        return err;
 }
 
+void __connman_service_save(struct connman_service *service)
+{
+       service_save(service);
+}
+
 static enum connman_service_state combine_state(
                                        enum connman_service_state state_a,
                                        enum connman_service_state state_b)
@@ -832,6 +860,74 @@ static connman_bool_t is_connected(struct connman_service *service)
        return is_connected_state(service, service->state);
 }
 
+static const char *nameserver_get_ifname(struct connman_service *service)
+{
+       const char *ifname;
+
+       if (service->ipconfig_ipv4)
+               ifname = __connman_ipconfig_get_ifname(service->ipconfig_ipv4);
+       else if (service->ipconfig_ipv6)
+               ifname = __connman_ipconfig_get_ifname(service->ipconfig_ipv6);
+       else
+               ifname = NULL;
+
+       if (ifname == NULL)
+               return NULL;
+
+       switch (combine_state(service->state_ipv4, service->state_ipv6)) {
+       case CONNMAN_SERVICE_STATE_UNKNOWN:
+       case CONNMAN_SERVICE_STATE_IDLE:
+       case CONNMAN_SERVICE_STATE_ASSOCIATION:
+       case CONNMAN_SERVICE_STATE_CONFIGURATION:
+       case CONNMAN_SERVICE_STATE_FAILURE:
+       case CONNMAN_SERVICE_STATE_DISCONNECT:
+               return NULL;
+       case CONNMAN_SERVICE_STATE_READY:
+       case CONNMAN_SERVICE_STATE_ONLINE:
+               break;
+       }
+
+       return ifname;
+}
+
+static void remove_nameservers(struct connman_service *service,
+               const char* interface, char **ns)
+{
+       const char *ifname = interface;
+       int i;
+
+       if (ns == NULL)
+               return;
+
+       if (interface == NULL)
+               ifname = nameserver_get_ifname(service);
+
+       if (ifname == NULL)
+                       return;
+
+       for (i = 0; ns[i] != NULL; i++)
+               connman_resolver_remove(ifname, NULL, ns[i]);
+}
+
+static void remove_searchdomains(struct connman_service *service,
+               const char *interface, char **sd)
+{
+       const char *ifname = interface;
+       int i;
+
+       if (sd == NULL)
+               return;
+
+       if (interface == NULL)
+               ifname = nameserver_get_ifname(service);
+
+       if (ifname == NULL)
+               return;
+
+       for (i = 0; sd[i] != NULL; i++)
+               connman_resolver_remove(ifname, sd[i], NULL);
+}
+
 static void update_nameservers(struct connman_service *service)
 {
        const char *ifname;
@@ -864,25 +960,38 @@ static void update_nameservers(struct connman_service *service)
        if (service->nameservers_config != NULL) {
                int i;
 
-               for (i = 0; service->nameservers_config[i] != NULL; i++) {
+               remove_nameservers(service, ifname, service->nameservers);
+
+               i = g_strv_length(service->nameservers_config);
+               while (i != 0) {
+                       i--;
                        connman_resolver_append(ifname, NULL,
-                                               service->nameservers_config[i]);
+                                       service->nameservers_config[i]);
                }
        } else if (service->nameservers != NULL) {
                int i;
 
-               for (i = 0; service->nameservers[i] != NULL; i++) {
+               i = g_strv_length(service->nameservers);
+               while (i != 0) {
+                       i--;
                        connman_resolver_append(ifname, NULL,
-                                               service->nameservers[i]);
+                                       service->nameservers[i]);
                }
        }
 
        if (service->domains != NULL) {
+               char *searchdomains[2] = {NULL, NULL};
                int i;
 
-               for (i = 0; service->domains[i]; i++)
+               searchdomains[0] = service->domainname;
+               remove_searchdomains(service, ifname, searchdomains);
+
+               i = g_strv_length(service->domains);
+               while (i != 0) {
+                       i--;
                        connman_resolver_append(ifname, service->domains[i],
                                                NULL);
+               }
        } else if (service->domainname != NULL)
                connman_resolver_append(ifname, service->domainname, NULL);
 
@@ -1042,48 +1151,26 @@ static void add_nameserver_route(int family, int index, char *nameserver,
 static void nameserver_add_routes(int index, char **nameservers,
                                        const char *gw)
 {
-       int i, ret, family;
-       struct addrinfo hints;
-       struct addrinfo *addr;
+       int i, family;
 
        for (i = 0; nameservers[i] != NULL; i++) {
-               memset(&hints, 0, sizeof(struct addrinfo));
-               hints.ai_flags = AI_NUMERICHOST;
-               addr = NULL;
-
-               ret = getaddrinfo(nameservers[i], NULL, &hints, &addr);
-               if (ret == EAI_NONAME)
-                       family = AF_INET; /* use the IPv4 as a default */
-               else if (ret != 0)
+               family = connman_inet_check_ipaddress(nameservers[i]);
+               if (family < 0)
                        continue;
-               else
-                       family = addr->ai_family;
 
                add_nameserver_route(family, index, nameservers[i], gw);
-
-               freeaddrinfo(addr);
        }
 }
 
 static void nameserver_del_routes(int index, char **nameservers,
                                enum connman_ipconfig_type type)
 {
-       int i, ret, family;
-       struct addrinfo hints;
-       struct addrinfo *addr;
+       int i, family;
 
        for (i = 0; nameservers[i] != NULL; i++) {
-               memset(&hints, 0, sizeof(struct addrinfo));
-               hints.ai_flags = AI_NUMERICHOST;
-               addr = NULL;
-
-               ret = getaddrinfo(nameservers[i], NULL, &hints, &addr);
-               if (ret == EAI_NONAME)
-                       family = AF_INET; /* use the IPv4 as a default */
-               else if (ret != 0)
+               family = connman_inet_check_ipaddress(nameservers[i]);
+               if (family < 0)
                        continue;
-               else
-                       family = addr->ai_family;
 
                switch (family) {
                case AF_INET:
@@ -1097,8 +1184,6 @@ static void nameserver_del_routes(int index, char **nameservers,
                                                        nameservers[i]);
                        break;
                }
-
-               freeaddrinfo(addr);
        }
 }
 
@@ -1283,6 +1368,9 @@ static void state_changed(struct connman_service *service)
        if (str == NULL)
                return;
 
+       if (allow_property_changed(service) == FALSE)
+               return;
+
        connman_dbus_property_changed_basic(service->path,
                                CONNMAN_SERVICE_INTERFACE, "State",
                                                DBUS_TYPE_STRING, &str);
@@ -1293,6 +1381,9 @@ static void strength_changed(struct connman_service *service)
        if (service->strength == 0)
                return;
 
+       if (allow_property_changed(service) == FALSE)
+               return;
+
        connman_dbus_property_changed_basic(service->path,
                                CONNMAN_SERVICE_INTERFACE, "Strength",
                                        DBUS_TYPE_BYTE, &service->strength);
@@ -1303,6 +1394,9 @@ static void favorite_changed(struct connman_service *service)
        if (service->path == NULL)
                return;
 
+       if (allow_property_changed(service) == FALSE)
+               return;
+
        connman_dbus_property_changed_basic(service->path,
                                CONNMAN_SERVICE_INTERFACE, "Favorite",
                                        DBUS_TYPE_BOOLEAN, &service->favorite);
@@ -1313,6 +1407,9 @@ static void immutable_changed(struct connman_service *service)
        if (service->path == NULL)
                return;
 
+       if (allow_property_changed(service) == FALSE)
+               return;
+
        connman_dbus_property_changed_basic(service->path,
                                CONNMAN_SERVICE_INTERFACE, "Immutable",
                                        DBUS_TYPE_BOOLEAN, &service->immutable);
@@ -1323,6 +1420,9 @@ static void roaming_changed(struct connman_service *service)
        if (service->path == NULL)
                return;
 
+       if (allow_property_changed(service) == FALSE)
+               return;
+
        connman_dbus_property_changed_basic(service->path,
                                CONNMAN_SERVICE_INTERFACE, "Roaming",
                                        DBUS_TYPE_BOOLEAN, &service->roaming);
@@ -1333,6 +1433,9 @@ static void autoconnect_changed(struct connman_service *service)
        if (service->path == NULL)
                return;
 
+       if (allow_property_changed(service) == FALSE)
+               return;
+
        connman_dbus_property_changed_basic(service->path,
                                CONNMAN_SERVICE_INTERFACE, "AutoConnect",
                                DBUS_TYPE_BOOLEAN, &service->autoconnect);
@@ -1430,14 +1533,14 @@ static void append_ipv6config(DBusMessageIter *iter, void *user_data)
                                                        iter);
 }
 
-static void append_nameserver(DBusMessageIter *iter, char ***nameservers)
+static void append_nameservers(DBusMessageIter *iter, char **servers)
 {
-       char **servers;
        int i;
 
-       servers = *nameservers;
+       DBG("%p", servers);
 
        for (i = 0; servers[i] != NULL; i++) {
+               DBG("servers[%d] %s", i, servers[i]);
                dbus_message_iter_append_basic(iter,
                                        DBUS_TYPE_STRING, &servers[i]);
        }
@@ -1451,30 +1554,25 @@ static void append_dns(DBusMessageIter *iter, void *user_data)
                return;
 
        if (service->nameservers_config != NULL) {
-               append_nameserver(iter, &service->nameservers_config);
+               append_nameservers(iter, service->nameservers_config);
                return;
        } else {
                if (service->nameservers != NULL)
-                       append_nameserver(iter, &service->nameservers);
+                       append_nameservers(iter, service->nameservers);
 
                if (service->nameservers_auto != NULL)
-                       append_nameserver(iter, &service->nameservers_auto);
+                       append_nameservers(iter, service->nameservers_auto);
        }
 }
 
 static void append_dnsconfig(DBusMessageIter *iter, void *user_data)
 {
        struct connman_service *service = user_data;
-       int i;
 
        if (service->nameservers_config == NULL)
                return;
 
-       for (i = 0; service->nameservers_config[i]; i++) {
-               dbus_message_iter_append_basic(iter,
-                               DBUS_TYPE_STRING,
-                               &service->nameservers_config[i]);
-       }
+       append_nameservers(iter, service->nameservers_config);
 }
 
 static void append_ts(DBusMessageIter *iter, void *user_data)
@@ -1507,32 +1605,32 @@ static void append_tsconfig(DBusMessageIter *iter, void *user_data)
        }
 }
 
-static void append_domain(DBusMessageIter *iter, void *user_data)
+static void append_domainconfig(DBusMessageIter *iter, void *user_data)
 {
        struct connman_service *service = user_data;
+       int i;
 
-       if (is_connected(service) == FALSE &&
-                               is_connecting(service) == FALSE)
-               return;
-
-       if (service->domainname == NULL)
+       if (service->domains == NULL)
                return;
 
-       dbus_message_iter_append_basic(iter,
-                               DBUS_TYPE_STRING, &service->domainname);
+       for (i = 0; service->domains[i]; i++)
+               dbus_message_iter_append_basic(iter,
+                               DBUS_TYPE_STRING, &service->domains[i]);
 }
 
-static void append_domainconfig(DBusMessageIter *iter, void *user_data)
+static void append_domain(DBusMessageIter *iter, void *user_data)
 {
        struct connman_service *service = user_data;
-       int i;
 
-       if (service->domains == NULL)
+       if (is_connected(service) == FALSE &&
+                               is_connecting(service) == FALSE)
                return;
 
-       for (i = 0; service->domains[i]; i++)
+       if (service->domains != NULL)
+               append_domainconfig(iter, user_data);
+       else if (service->domainname != NULL)
                dbus_message_iter_append_basic(iter,
-                               DBUS_TYPE_STRING, &service->domains[i]);
+                               DBUS_TYPE_STRING, &service->domainname);
 }
 
 static void append_proxies(DBusMessageIter *iter, void *user_data)
@@ -1671,6 +1769,9 @@ static void append_provider(DBusMessageIter *iter, void *user_data)
 static void settings_changed(struct connman_service *service,
                                struct connman_ipconfig *ipconfig)
 {
+       if (allow_property_changed(service) == FALSE)
+               return;
+
        connman_dbus_property_changed_dict(service->path,
                                        CONNMAN_SERVICE_INTERFACE, "IPv4",
                                                        append_ipv4, service);
@@ -1684,6 +1785,9 @@ static void settings_changed(struct connman_service *service,
 
 static void ipv4_configuration_changed(struct connman_service *service)
 {
+       if (allow_property_changed(service) == FALSE)
+               return;
+
        connman_dbus_property_changed_dict(service->path,
                                        CONNMAN_SERVICE_INTERFACE,
                                                        "IPv4.Configuration",
@@ -1693,6 +1797,9 @@ static void ipv4_configuration_changed(struct connman_service *service)
 
 static void ipv6_configuration_changed(struct connman_service *service)
 {
+       if (allow_property_changed(service) == FALSE)
+               return;
+
        connman_dbus_property_changed_dict(service->path,
                                        CONNMAN_SERVICE_INTERFACE,
                                                        "IPv6.Configuration",
@@ -1702,6 +1809,9 @@ static void ipv6_configuration_changed(struct connman_service *service)
 
 static void dns_changed(struct connman_service *service)
 {
+       if (allow_property_changed(service) == FALSE)
+               return;
+
        connman_dbus_property_changed_array(service->path,
                                CONNMAN_SERVICE_INTERFACE, "Nameservers",
                                        DBUS_TYPE_STRING, append_dns, service);
@@ -1709,6 +1819,9 @@ static void dns_changed(struct connman_service *service)
 
 static void dns_configuration_changed(struct connman_service *service)
 {
+       if (allow_property_changed(service) == FALSE)
+               return;
+
        connman_dbus_property_changed_array(service->path,
                                CONNMAN_SERVICE_INTERFACE,
                                "Nameservers.Configuration",
@@ -1719,6 +1832,9 @@ static void dns_configuration_changed(struct connman_service *service)
 
 static void domain_changed(struct connman_service *service)
 {
+       if (allow_property_changed(service) == FALSE)
+               return;
+
        connman_dbus_property_changed_array(service->path,
                                CONNMAN_SERVICE_INTERFACE, "Domains",
                                DBUS_TYPE_STRING, append_domain, service);
@@ -1726,6 +1842,9 @@ static void domain_changed(struct connman_service *service)
 
 static void domain_configuration_changed(struct connman_service *service)
 {
+       if (allow_property_changed(service) == FALSE)
+               return;
+
        connman_dbus_property_changed_array(service->path,
                                CONNMAN_SERVICE_INTERFACE,
                                "Domains.Configuration",
@@ -1734,6 +1853,9 @@ static void domain_configuration_changed(struct connman_service *service)
 
 static void proxy_changed(struct connman_service *service)
 {
+       if (allow_property_changed(service) == FALSE)
+               return;
+
        connman_dbus_property_changed_dict(service->path,
                                        CONNMAN_SERVICE_INTERFACE, "Proxy",
                                                        append_proxy, service);
@@ -1741,6 +1863,9 @@ static void proxy_changed(struct connman_service *service)
 
 static void proxy_configuration_changed(struct connman_service *service)
 {
+       if (allow_property_changed(service) == FALSE)
+               return;
+
        connman_dbus_property_changed_dict(service->path,
                        CONNMAN_SERVICE_INTERFACE, "Proxy.Configuration",
                                                append_proxyconfig, service);
@@ -1750,6 +1875,9 @@ static void proxy_configuration_changed(struct connman_service *service)
 
 static void timeservers_configuration_changed(struct connman_service *service)
 {
+       if (allow_property_changed(service) == FALSE)
+               return;
+
        connman_dbus_property_changed_array(service->path,
                        CONNMAN_SERVICE_INTERFACE,
                        "Timeservers.Configuration",
@@ -1759,6 +1887,9 @@ static void timeservers_configuration_changed(struct connman_service *service)
 
 static void link_changed(struct connman_service *service)
 {
+       if (allow_property_changed(service) == FALSE)
+               return;
+
        connman_dbus_property_changed_dict(service->path,
                                        CONNMAN_SERVICE_INTERFACE, "Ethernet",
                                                append_ethernet, service);
@@ -1958,7 +2089,7 @@ int __connman_service_counter_register(const char *counter)
 
        DBG("counter %s", counter);
 
-       counter_list = g_slist_append(counter_list, (gpointer)counter);
+       counter_list = g_slist_prepend(counter_list, (gpointer)counter);
 
        iter = g_sequence_get_begin_iter(service_list);
 
@@ -2232,6 +2363,14 @@ int __connman_service_get_index(struct connman_service *service)
        return -1;
 }
 
+void __connman_service_set_hidden(struct connman_service *service)
+{
+       if (service == NULL || service->hidden == TRUE)
+               return;
+
+       service->hidden_service = TRUE;
+}
+
 void __connman_service_set_domainname(struct connman_service *service,
                                                const char *domainname)
 {
@@ -2488,6 +2627,9 @@ void __connman_service_timeserver_changed(struct connman_service *service,
        if (service == NULL)
                return;
 
+       if (allow_property_changed(service) == FALSE)
+               return;
+
        connman_dbus_property_changed_array(service->path,
                        CONNMAN_SERVICE_INTERFACE, "Timeservers",
                        DBUS_TYPE_STRING, append_ts, ts_list);
@@ -2533,14 +2675,23 @@ void __connman_service_set_agent_identity(struct connman_service *service,
                                        service->agent_identity);
 }
 
-static int check_passphrase(enum connman_service_security security,
+static int check_passphrase(struct connman_service *service,
+                               enum connman_service_security security,
                                const char *passphrase)
 {
        guint i;
        gsize length;
 
-       if (passphrase == NULL)
-               return 0;
+       if (passphrase == NULL) {
+               /*
+                * This will prevent __connman_service_set_passphrase() to
+                * wipe the passphrase out in case of -ENOKEY error for a
+                * favorite service. */
+               if (service->favorite == TRUE)
+                       return 1;
+               else
+                       return 0;
+       }
 
        length = strlen(passphrase);
 
@@ -2589,7 +2740,7 @@ int __connman_service_set_passphrase(struct connman_service *service,
        if (service->immutable == TRUE || service->hidden == TRUE)
                return -EINVAL;
 
-       err = check_passphrase(service->security, passphrase);
+       err = check_passphrase(service, service->security, passphrase);
 
        if (err == 0) {
                g_free(service->passphrase);
@@ -2605,6 +2756,14 @@ int __connman_service_set_passphrase(struct connman_service *service,
        return err;
 }
 
+const char *__connman_service_get_passphrase(struct connman_service *service)
+{
+       if (service == NULL)
+               return NULL;
+
+       return service->passphrase;
+}
+
 void __connman_service_set_agent_passphrase(struct connman_service *service,
                                                const char *agent_passphrase)
 {
@@ -2841,9 +3000,9 @@ static int set_ipconfig(struct connman_service *service,
        if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
                if (err == 0 && old_method == CONNMAN_IPCONFIG_METHOD_OFF &&
                                method == CONNMAN_IPCONFIG_METHOD_DHCP) {
-                       *new_state = service->state_ipv4 =
-                               CONNMAN_SERVICE_STATE_CONFIGURATION;
+                       *new_state = service->state_ipv4;
                        __connman_ipconfig_enable(ipconfig);
+                       __connman_service_auto_connect();
                }
 
        } else if (type == CONNMAN_IPCONFIG_TYPE_IPV6) {
@@ -2851,6 +3010,7 @@ static int set_ipconfig(struct connman_service *service,
                                method == CONNMAN_IPCONFIG_METHOD_AUTO) {
                        *new_state = service->state_ipv6;
                        __connman_ipconfig_enable(ipconfig);
+                       __connman_service_auto_connect();
                }
        }
 
@@ -2932,12 +3092,15 @@ static DBusMessage *set_property(DBusConnection *conn,
                        const char *val;
                        dbus_message_iter_get_basic(&entry, &val);
                        dbus_message_iter_next(&entry);
-                       if (str->len > 0)
-                               g_string_append_printf(str, " %s", val);
-                       else
-                               g_string_append(str, val);
+                       if (connman_inet_check_ipaddress(val) > 0) {
+                               if (str->len > 0)
+                                       g_string_append_printf(str, " %s", val);
+                               else
+                                       g_string_append(str, val);
+                       }
                }
 
+               remove_nameservers(service, NULL, service->nameservers_config);
                g_strfreev(service->nameservers_config);
 
                if (str->len > 0) {
@@ -3021,6 +3184,7 @@ static DBusMessage *set_property(DBusConnection *conn,
                                g_string_append(str, val);
                }
 
+               remove_searchdomains(service, NULL, service->domains);
                g_strfreev(service->domains);
 
                if (str->len > 0)
@@ -3099,11 +3263,37 @@ static DBusMessage *set_property(DBusConnection *conn,
        return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
 }
 
+static void set_error(struct connman_service *service,
+                                       enum connman_service_error error)
+{
+       const char *str;
+
+       if (service->error == error)
+               return;
+
+       service->error = error;
+
+       if (service->path == NULL)
+               return;
+
+       str = error2string(service->error);
+
+       if (str == NULL)
+               str = "";
+
+       if (allow_property_changed(service) == FALSE)
+               return;
+
+       connman_dbus_property_changed_basic(service->path,
+                               CONNMAN_SERVICE_INTERFACE, "Error",
+                               DBUS_TYPE_STRING, &str);
+}
+
 static void set_idle(struct connman_service *service)
 {
        service->state = service->state_ipv4 = service->state_ipv6 =
                                                CONNMAN_SERVICE_STATE_IDLE;
-       service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
+       set_error(service, CONNMAN_SERVICE_ERROR_UNKNOWN);
        state_changed(service);
 }
 
@@ -3129,6 +3319,16 @@ static DBusMessage *clear_property(DBusConnection *conn,
        return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
 }
 
+static connman_bool_t is_ipconfig_usable(struct connman_service *service)
+{
+       if (__connman_ipconfig_is_usable(service->ipconfig_ipv4) == FALSE &&
+                       __connman_ipconfig_is_usable(service->ipconfig_ipv6)
+                                                               == FALSE)
+               return FALSE;
+
+       return TRUE;
+}
+
 static connman_bool_t is_ignore(struct connman_service *service)
 {
        if (service->autoconnect == FALSE)
@@ -3143,6 +3343,9 @@ static connman_bool_t is_ignore(struct connman_service *service)
        if (service->state == CONNMAN_SERVICE_STATE_FAILURE)
                return TRUE;
 
+       if (is_ipconfig_usable(service) == FALSE)
+               return TRUE;
+
        return FALSE;
 }
 
@@ -3282,37 +3485,113 @@ static void remove_timeout(struct connman_service *service)
        }
 }
 
-static void reply_pending(struct connman_service *service, int error)
+void __connman_service_reply_dbus_pending(DBusMessage *pending, int error,
+                                       const char *path)
 {
-       remove_timeout(service);
-
-       if (service->pending != NULL) {
+       if (pending != NULL) {
                if (error > 0) {
                        DBusMessage *reply;
 
-                       reply = __connman_error_failed(service->pending,
-                                                               error);
+                       reply = __connman_error_failed(pending, error);
                        if (reply != NULL)
                                g_dbus_send_message(connection, reply);
                } else {
                        const char *sender;
 
-                       sender = dbus_message_get_interface(service->pending);
+                       sender = dbus_message_get_interface(pending);
+                       if (path == NULL)
+                               path = dbus_message_get_path(pending);
 
-                       DBG("sender %s", sender);
+                       DBG("sender %s path %s", sender, path);
 
                        if (g_strcmp0(sender, CONNMAN_MANAGER_INTERFACE) == 0)
-                               g_dbus_send_reply(connection, service->pending,
-                                       DBUS_TYPE_OBJECT_PATH, &service->path,
+                               g_dbus_send_reply(connection, pending,
+                                       DBUS_TYPE_OBJECT_PATH, &path,
                                                        DBUS_TYPE_INVALID);
                        else
-                               g_dbus_send_reply(connection, service->pending,
+                               g_dbus_send_reply(connection, pending,
                                                        DBUS_TYPE_INVALID);
                }
 
-               dbus_message_unref(service->pending);
+               dbus_message_unref(pending);
+       }
+}
+
+static void reply_pending(struct connman_service *service, int error)
+{
+       remove_timeout(service);
+
+       if (service->pending != NULL) {
+               __connman_service_reply_dbus_pending(service->pending, error,
+                                               NULL);
                service->pending = NULL;
        }
+
+       if (service->provider_pending != NULL) {
+               __connman_service_reply_dbus_pending(service->provider_pending,
+                                               error, service->path);
+               service->provider_pending = NULL;
+       }
+}
+
+connman_bool_t
+__connman_service_is_provider_pending(struct connman_service *service)
+{
+       if (service == NULL)
+               return FALSE;
+
+       if (service->provider_pending != NULL)
+               return TRUE;
+
+       return FALSE;
+}
+
+void __connman_service_set_provider_pending(struct connman_service *service,
+                                                       DBusMessage *msg)
+{
+       if (service->provider_pending != NULL) {
+               DBG("service %p provider pending msg %p already exists",
+                       service, service->provider_pending);
+               return;
+       }
+
+       service->provider_pending = msg;
+       return;
+}
+
+static void check_pending_msg(struct connman_service *service)
+{
+       if (service->pending == NULL)
+               return;
+
+       DBG("service %p pending msg %p already exists", service,
+                                               service->pending);
+       dbus_message_unref(service->pending);
+}
+
+void __connman_service_set_hidden_data(struct connman_service *service,
+                                                       gpointer user_data)
+{
+       DBusMessage *pending = user_data;
+
+       DBG("service %p pending %p", service, pending);
+
+       if (pending == NULL)
+               return;
+
+       check_pending_msg(service);
+
+       service->pending = pending;
+}
+
+void __connman_service_return_error(struct connman_service *service,
+                               int error, gpointer user_data)
+{
+       DBG("service %p error %d user_data %p", service, error, user_data);
+
+       __connman_service_set_hidden_data(service, user_data);
+
+       reply_pending(service, error);
 }
 
 static gboolean connect_timeout(gpointer user_data)
@@ -3326,6 +3605,8 @@ static gboolean connect_timeout(gpointer user_data)
 
        if (service->network != NULL)
                __connman_network_disconnect(service->network);
+       else if (service->provider != NULL)
+               __connman_provider_disconnect(service->provider);
 
        __connman_ipconfig_disable(service->ipconfig_ipv4);
        __connman_ipconfig_disable(service->ipconfig_ipv6);
@@ -3386,6 +3667,29 @@ static connman_bool_t get_reconnect_state(struct connman_service *service)
        return __connman_device_get_reconnect(device);
 }
 
+static connman_bool_t is_interface_available(struct connman_service *service,
+                                       struct connman_service *other_service)
+{
+       unsigned int index = 0, other_index = 0;
+
+       if (service->ipconfig_ipv4 != NULL)
+               index = __connman_ipconfig_get_index(service->ipconfig_ipv4);
+       else if (service->ipconfig_ipv6 != NULL)
+               index = __connman_ipconfig_get_index(service->ipconfig_ipv6);
+
+       if (other_service->ipconfig_ipv4 != NULL)
+               other_index = __connman_ipconfig_get_index(
+                                               other_service->ipconfig_ipv4);
+       else if (other_service->ipconfig_ipv6 != NULL)
+               other_index = __connman_ipconfig_get_index(
+                                               other_service->ipconfig_ipv6);
+
+       if (index > 0 && other_index != index)
+               return TRUE;
+
+       return FALSE;
+}
+
 static DBusMessage *connect_service(DBusConnection *conn,
                                        DBusMessage *msg, void *user_data)
 {
@@ -3403,7 +3707,15 @@ static DBusMessage *connect_service(DBusConnection *conn,
        while (g_sequence_iter_is_end(iter) == FALSE) {
                struct connman_service *temp = g_sequence_get(iter);
 
-               if (service->type == temp->type && is_connecting(temp) == TRUE)
+               /*
+                * We should allow connection if there are available
+                * interfaces for a given technology type (like having
+                * more than one wifi card).
+                */
+               if (service->type == temp->type &&
+                               is_connecting(temp) == TRUE &&
+                               is_interface_available(service,
+                                                       temp) == FALSE)
                        return __connman_error_in_progress(msg);
 
                iter = g_sequence_iter_next(iter);
@@ -3460,22 +3772,17 @@ static DBusMessage *disconnect_service(DBusConnection *conn,
        return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
 }
 
-static DBusMessage *remove_service(DBusConnection *conn,
-                                       DBusMessage *msg, void *user_data)
+gboolean __connman_service_remove(struct connman_service *service)
 {
-       struct connman_service *service = user_data;
-
-       DBG("service %p", service);
-
        if (service->type == CONNMAN_SERVICE_TYPE_ETHERNET)
-               return __connman_error_not_supported(msg);
+               return FALSE;
 
        if (service->immutable == TRUE || service->hidden == TRUE)
-               return __connman_error_not_supported(msg);
+               return FALSE;
 
        if (service->favorite == FALSE && service->state !=
                                                CONNMAN_SERVICE_STATE_FAILURE)
-               return __connman_error_not_supported(msg);
+               return FALSE;
 
        set_reconnect_state(service, FALSE);
 
@@ -3484,11 +3791,37 @@ static DBusMessage *remove_service(DBusConnection *conn,
        g_free(service->passphrase);
        service->passphrase = NULL;
 
+       g_free(service->agent_passphrase);
+       service->agent_passphrase = NULL;
+
+       g_free(service->identity);
+       service->identity = NULL;
+
+       g_free(service->agent_identity);
+       service->agent_identity = NULL;
+
+       g_free(service->eap);
+       service->eap = NULL;
+
        set_idle(service);
 
        __connman_service_set_favorite(service, FALSE);
+
        service_save(service);
 
+       return TRUE;
+}
+
+static DBusMessage *remove_service(DBusConnection *conn,
+                                       DBusMessage *msg, void *user_data)
+{
+       struct connman_service *service = user_data;
+
+       DBG("service %p", service);
+
+       if (__connman_service_remove(service) == FALSE)
+               return __connman_error_not_supported(msg);
+
        return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
 }
 
@@ -3735,6 +4068,8 @@ static gboolean service_send_changed(gpointer data)
 
        DBG("");
 
+       services_notify->id = 0;
+
        signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
                        CONNMAN_MANAGER_INTERFACE, "ServicesChanged");
        if (signal == NULL)
@@ -3757,7 +4092,6 @@ static gboolean service_send_changed(gpointer data)
        g_hash_table_remove_all(services_notify->remove);
        g_hash_table_remove_all(services_notify->add);
 
-       services_notify->id = 0;
        return FALSE;
 }
 
@@ -3795,22 +4129,45 @@ static void service_schedule_removed(struct connman_service *service)
        service_schedule_changed();
 }
 
-static GDBusMethodTable service_methods[] = {
-       { "GetProperties", "",   "a{sv}", get_properties     },
-       { "SetProperty",   "sv", "",      set_property       },
-       { "ClearProperty", "s",  "",      clear_property     },
-       { "Connect",       "",   "",      connect_service,
-                                               G_DBUS_METHOD_FLAG_ASYNC },
-       { "Disconnect",    "",   "",      disconnect_service },
-       { "Remove",        "",   "",      remove_service     },
-       { "MoveBefore",    "o",  "",      move_before        },
-       { "MoveAfter",     "o",  "",      move_after         },
-       { "ResetCounters", "",   "",      reset_counters     },
+static connman_bool_t allow_property_changed(struct connman_service *service)
+{
+       if (g_hash_table_lookup_extended(services_notify->add, service->path,
+                                       NULL, NULL) == TRUE) {
+               DBG("no property updates for service %p", service);
+               return FALSE;
+       }
+
+       return TRUE;
+}
+
+static const GDBusMethodTable service_methods[] = {
+       { GDBUS_DEPRECATED_METHOD("GetProperties",
+                       NULL, GDBUS_ARGS({ "properties", "a{sv}" }),
+                       get_properties) },
+       { GDBUS_METHOD("SetProperty",
+                       GDBUS_ARGS({ "name", "s" }, { "value", "v" }),
+                       NULL, set_property) },
+       { GDBUS_METHOD("ClearProperty",
+                       GDBUS_ARGS({ "name", "s" }), NULL,
+                       clear_property) },
+       { GDBUS_ASYNC_METHOD("Connect", NULL, NULL,
+                             connect_service) },
+       { GDBUS_METHOD("Disconnect", NULL, NULL,
+                       disconnect_service) },
+       { GDBUS_METHOD("Remove", NULL, NULL, remove_service) },
+       { GDBUS_METHOD("MoveBefore",
+                       GDBUS_ARGS({ "service", "o" }), NULL,
+                       move_before) },
+       { GDBUS_METHOD("MoveAfter",
+                       GDBUS_ARGS({ "service", "o" }), NULL,
+                       move_after) },
+       { GDBUS_METHOD("ResetCounters", NULL, NULL, reset_counters) },
        { },
 };
 
-static GDBusSignalTable service_signals[] = {
-       { "PropertyChanged", "sv" },
+static const GDBusSignalTable service_signals[] = {
+       { GDBUS_SIGNAL("PropertyChanged",
+                       GDBUS_ARGS({ "name", "s" }, { "value", "v" })) },
        { },
 };
 
@@ -3828,6 +4185,7 @@ static void service_free(gpointer user_data)
        __connman_notifier_service_remove(service);
        service_schedule_removed(service);
 
+       __connman_wispr_stop(service);
        stats_stop(service);
 
        service->path = NULL;
@@ -3888,6 +4246,8 @@ static void service_free(gpointer user_data)
        g_free(service->private_key_file);
        g_free(service->private_key_passphrase);
        g_free(service->phase2);
+       g_free(service->config_file);
+       g_free(service->config_entry);
 
        if (service->stats.timer != NULL)
                g_timer_destroy(service->stats.timer);
@@ -3917,6 +4277,8 @@ static void service_initialize(struct connman_service *service)
        service->refcount = 1;
        service->session_usage_count = 0;
 
+       service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
+
        service->type     = CONNMAN_SERVICE_TYPE_UNKNOWN;
        service->security = CONNMAN_SERVICE_SECURITY_UNKNOWN;
 
@@ -4235,15 +4597,22 @@ connman_bool_t __connman_service_wps_enabled(struct connman_service *service)
        return service->wps;
 }
 
+void __connman_service_mark_dirty()
+ {
+       services_dirty = TRUE;
+ }
+
 /**
- * __connman_service_set_favorite:
+ * __connman_service_set_favorite_delayed:
  * @service: service structure
  * @favorite: favorite value
+ * @delay_ordering: do not order service sequence
  *
  * Change the favorite setting of service
  */
-int __connman_service_set_favorite(struct connman_service *service,
-                                               connman_bool_t favorite)
+int __connman_service_set_favorite_delayed(struct connman_service *service,
+                                       connman_bool_t favorite,
+                                       gboolean delay_ordering)
 {
        GSequenceIter *iter;
 
@@ -4257,20 +4626,44 @@ int __connman_service_set_favorite(struct connman_service *service,
                return -EALREADY;
 
        service->favorite = favorite;
-       service->order = __connman_service_get_order(service);
+
+       if (delay_ordering == FALSE)
+               service->order = __connman_service_get_order(service);
 
        favorite_changed(service);
 
-       if (g_sequence_get_length(service_list) > 1) {
-               g_sequence_sort_changed(iter, service_compare, NULL);
-               service_schedule_changed();
-       }
+       if (delay_ordering == FALSE) {
 
-       __connman_connection_update_gateway();
+               if (g_sequence_get_length(service_list) > 1) {
+                       g_sequence_sort_changed(iter, service_compare, NULL);
+                       service_schedule_changed();
+               }
+
+               __connman_connection_update_gateway();
+       }
 
        return 0;
 }
 
+/**
+ * __connman_service_set_favorite:
+ * @service: service structure
+ * @favorite: favorite value
+ *
+ * Change the favorite setting of service
+ */
+int __connman_service_set_favorite(struct connman_service *service,
+                                               connman_bool_t favorite)
+{
+       return __connman_service_set_favorite_delayed(service, favorite,
+                                                       FALSE);
+}
+
+connman_bool_t connman_service_get_favorite(struct connman_service *service)
+{
+       return service->favorite;
+}
+
 int __connman_service_set_immutable(struct connman_service *service,
                                                connman_bool_t immutable)
 {
@@ -4315,6 +4708,13 @@ void __connman_service_set_string(struct connman_service *service,
        }
 }
 
+void __connman_service_set_userconnect(struct connman_service *service,
+                                               connman_bool_t userconnect)
+{
+       if (service != NULL)
+               service->userconnect = userconnect;
+}
+
 static void service_complete(struct connman_service *service)
 {
        reply_pending(service, EIO);
@@ -4332,9 +4732,12 @@ static void report_error_cb(struct connman_service *service,
        if (retry == TRUE)
                __connman_service_connect(service);
        else {
+               /* It is not relevant to stay on Failure state
+                * when failing is due to wrong user input */
+               service->state = CONNMAN_SERVICE_STATE_IDLE;
+
                service_complete(service);
                __connman_connection_update_gateway();
-               __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
        }
 }
 
@@ -4402,25 +4805,45 @@ static void request_input_cb (struct connman_service *service,
                        const char *name, int name_len,
                        const char *identity, const char *passphrase,
                        gboolean wps, const char *wpspin,
-                       void *user_data)
+                       const char *error, void *user_data)
 {
        struct connman_device *device;
        int err = 0;
 
        DBG ("RequestInput return, %p", service);
 
+       if (error != NULL) {
+               DBG("error: %s", error);
+
+               if (g_strcmp0(error,
+                               "net.connman.Agent.Error.Canceled") == 0) {
+                       err = -EINVAL;
+
+                       if (service->hidden == TRUE)
+                               __connman_service_return_error(service,
+                                                       ECANCELED, user_data);
+                       goto done;
+               } else {
+                       if (service->hidden == TRUE)
+                               __connman_service_return_error(service,
+                                                       ETIMEDOUT, user_data);
+               }
+       }
+
        if (service->hidden == TRUE && name_len > 0 && name_len <= 32) {
                device = connman_network_get_device(service->network);
-               __connman_device_request_hidden_scan(device,
+               err = __connman_device_request_hidden_scan(device,
                                                name, name_len,
-                                               identity, passphrase);
+                                               identity, passphrase,
+                                               user_data);
+               if (err < 0)
+                       __connman_service_return_error(service, -err,
+                                                       user_data);
        }
 
        if (values_received == FALSE || service->hidden == TRUE) {
-               service_complete(service);
-               __connman_connection_update_gateway();
-               __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
-               return;
+               err = -EINVAL;
+               goto done;
        }
 
        if (wps == TRUE && service->network != NULL) {
@@ -4439,6 +4862,9 @@ static void request_input_cb (struct connman_service *service,
 
  done:
        if (err >= 0) {
+               /* We forget any previous error. */
+               set_error(service, CONNMAN_SERVICE_ERROR_UNKNOWN);
+
                __connman_service_connect(service);
 
                /* Never cache agent provided credentials */
@@ -4447,9 +4873,24 @@ static void request_input_cb (struct connman_service *service,
        } else if (err == -ENOKEY) {
                __connman_service_indicate_error(service,
                                        CONNMAN_SERVICE_ERROR_INVALID_KEY);
-               __connman_agent_report_error(service,
-                                       error2string(service->error),
-                                       report_error_cb, NULL);
+       } else {
+               /* It is not relevant to stay on Failure state
+                * when failing is due to wrong user input */
+               service->state = CONNMAN_SERVICE_STATE_IDLE;
+
+               if (service->hidden == FALSE) {
+                       /*
+                        * If there was a real error when requesting
+                        * hidden scan, then that error is returned already
+                        * to the user somewhere above so do not try to
+                        * do this again.
+                        */
+                       __connman_service_return_error(service, -err,
+                                                       user_data);
+               }
+
+               service_complete(service);
+               __connman_connection_update_gateway();
        }
 }
 
@@ -4573,8 +5014,6 @@ static int service_indicate_state(struct connman_service *service)
                reconnect = get_reconnect_state(service);
                if (reconnect == TRUE)
                        __connman_service_auto_connect();
-
-               __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
        }
 
        if (new_state == CONNMAN_SERVICE_STATE_READY) {
@@ -4667,13 +5106,11 @@ static int service_indicate_state(struct connman_service *service)
                if (service->userconnect == TRUE &&
                        __connman_agent_report_error(service,
                                        error2string(service->error),
-                                       report_error_cb, NULL) == -EIO)
+                                       report_error_cb, NULL) == -EINPROGRESS)
                        return 0;
                service_complete(service);
-
-               __connman_device_request_scan(CONNMAN_DEVICE_TYPE_UNKNOWN);
        } else
-               service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
+               set_error(service, CONNMAN_SERVICE_ERROR_UNKNOWN);
 
        iter = g_hash_table_lookup(service_hash, service->identifier);
        if (iter != NULL && g_sequence_get_length(service_list) > 1) {
@@ -4699,7 +5136,7 @@ int __connman_service_indicate_error(struct connman_service *service,
        if (service == NULL)
                return -EINVAL;
 
-       service->error = error;
+       set_error(service, error);
 
        if (service->error == CONNMAN_SERVICE_ERROR_INVALID_KEY)
                __connman_service_set_passphrase(service, NULL);
@@ -4725,7 +5162,7 @@ int __connman_service_clear_error(struct connman_service *service)
 
        service->state_ipv4 = service->state_ipv6 =
                                                CONNMAN_SERVICE_STATE_UNKNOWN;
-       service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;;
+       set_error(service, CONNMAN_SERVICE_ERROR_UNKNOWN);
 
        if (service->favorite == TRUE)
                set_reconnect_state(service, TRUE);
@@ -5093,7 +5530,9 @@ static int service_connect(struct connman_service *service)
                                                        service->network,
                                                        "WiFi.UseWPS") == FALSE)
                                        return -ENOKEY;
-                       }
+                       } else if (service->error ==
+                                       CONNMAN_SERVICE_ERROR_INVALID_KEY)
+                               return -ENOKEY;
                        break;
                case CONNMAN_SERVICE_SECURITY_8021X:
                        if (service->eap == NULL)
@@ -5189,27 +5628,25 @@ int __connman_service_connect(struct connman_service *service)
        case CONNMAN_SERVICE_TYPE_GADGET:
                return -EINVAL;
        default:
+               if (is_ipconfig_usable(service) == FALSE)
+                       return -ENOLINK;
+
                err = service_connect(service);
        }
 
-       if (err >= 0)
+       if (err >= 0) {
+               set_error(service, CONNMAN_SERVICE_ERROR_UNKNOWN);
                return 0;
+       }
 
        if (err == -EINPROGRESS) {
                if (service->timeout == 0)
                        service->timeout = g_timeout_add_seconds(
                                CONNECT_TIMEOUT, connect_timeout, service);
-
+               set_error(service, CONNMAN_SERVICE_ERROR_UNKNOWN);
                return -EINPROGRESS;
        }
 
-       __connman_service_ipconfig_indicate_state(service,
-                                       CONNMAN_SERVICE_STATE_FAILURE,
-                                       CONNMAN_IPCONFIG_TYPE_IPV4);
-       __connman_service_ipconfig_indicate_state(service,
-                                       CONNMAN_SERVICE_STATE_FAILURE,
-                                       CONNMAN_IPCONFIG_TYPE_IPV6);
-
        if (service->network != NULL)
                __connman_network_disconnect(service->network);
        else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
@@ -5218,10 +5655,25 @@ int __connman_service_connect(struct connman_service *service)
 
        if (service->userconnect == TRUE) {
                if (err == -ENOKEY || err == -EPERM) {
-                       if (__connman_agent_request_passphrase_input(service,
-                                                       request_input_cb,
-                                                       NULL) == -EIO)
-                               return -EINPROGRESS;
+                       DBusMessage *pending = NULL;
+
+                       /*
+                        * We steal the reply here. The idea is that the
+                        * connecting client will see the connection status
+                        * after the real hidden network is connected or
+                        * connection failed.
+                        */
+                       if (service->hidden == TRUE) {
+                               pending = service->pending;
+                               service->pending = NULL;
+                       }
+
+                       err = __connman_agent_request_passphrase_input(service,
+                                       request_input_cb, pending);
+                       if (service->hidden == TRUE && err != -EINPROGRESS)
+                               service->pending = pending;
+
+                       return err;
                }
                reply_pending(service, -err);
        }
@@ -5235,6 +5687,8 @@ int __connman_service_disconnect(struct connman_service *service)
 
        DBG("service %p", service);
 
+       __connman_agent_cancel(service);
+
        if (service->network != NULL) {
                err = __connman_network_disconnect(service->network);
        } else if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
@@ -5316,17 +5770,62 @@ static struct connman_service *lookup_by_identifier(const char *identifier)
        return NULL;
 }
 
+struct provision_user_data {
+       const char *ident;
+       int ret;
+};
+
 static void provision_changed(gpointer value, gpointer user_data)
 {
        struct connman_service *service = value;
-       char *path = user_data;
+       struct provision_user_data *data = user_data;
+       const char *path = data->ident;
+       int ret;
 
-       __connman_config_provision_service_ident(service, path);
+       ret = __connman_config_provision_service_ident(service, path,
+                       service->config_file, service->config_entry);
+       if (ret > 0)
+               data->ret = ret;
 }
 
-void __connman_service_provision_changed(const char *ident)
+int __connman_service_provision_changed(const char *ident)
 {
-       g_sequence_foreach(service_list, provision_changed, (void *)ident);
+       struct provision_user_data data = {
+               .ident = ident,
+               .ret = 0
+       };
+
+       g_sequence_foreach(service_list, provision_changed, (void *)&data);
+
+       /*
+        * Because the provision_changed() might have set some services
+        * as favorite, we must sort the sequence now.
+        */
+       if (services_dirty == TRUE) {
+               services_dirty = FALSE;
+
+               if (g_sequence_get_length(service_list) > 1) {
+                       g_sequence_sort(service_list, service_compare, NULL);
+                       service_schedule_changed();
+               }
+
+               __connman_connection_update_gateway();
+       }
+
+       return data.ret;
+}
+
+void __connman_service_set_config(struct connman_service *service,
+                               const char *file_id, const char *entry)
+{
+       if (service == NULL)
+               return;
+
+       g_free(service->config_file);
+       service->config_file = g_strdup(file_id);
+
+       g_free(service->config_entry);
+       service->config_entry = g_strdup(entry);
 }
 
 /**
@@ -5492,6 +5991,15 @@ static void service_ip_release(struct connman_ipconfig *ipconfig)
        settings_changed(service, ipconfig);
 }
 
+static void service_route_changed(struct connman_ipconfig *ipconfig)
+{
+       struct connman_service *service = __connman_ipconfig_get_data(ipconfig);
+
+       DBG("%s route changed", __connman_ipconfig_get_ifname(ipconfig));
+
+       settings_changed(service, ipconfig);
+}
+
 static const struct connman_ipconfig_ops service_ops = {
        .up             = service_up,
        .down           = service_down,
@@ -5499,6 +6007,8 @@ static const struct connman_ipconfig_ops service_ops = {
        .lower_down     = service_lower_down,
        .ip_bound       = service_ip_bound,
        .ip_release     = service_ip_release,
+       .route_set      = service_route_changed,
+       .route_unset    = service_route_changed,
 };
 
 static void setup_ip4config(struct connman_service *service, int index,
@@ -5545,7 +6055,7 @@ void __connman_service_read_ip4config(struct connman_service *service)
        g_key_file_free(keyfile);
 }
 
-void __connman_service_create_ip4config(struct connman_service *service,
+void connman_service_create_ip4config(struct connman_service *service,
                                        int index)
 {
        DBG("ipv4 %p", service->ipconfig_ipv4);
@@ -5574,7 +6084,7 @@ void __connman_service_read_ip6config(struct connman_service *service)
        g_key_file_free(keyfile);
 }
 
-void __connman_service_create_ip6config(struct connman_service *service,
+void connman_service_create_ip6config(struct connman_service *service,
                                                                int index)
 {
        DBG("ipv6 %p", service->ipconfig_ipv6);
@@ -5588,12 +6098,12 @@ void __connman_service_create_ip6config(struct connman_service *service,
 }
 
 /**
- * __connman_service_lookup_from_network:
+ * connman_service_lookup_from_network:
  * @network: network structure
  *
  * Look up a service by network (reference count will not be increased)
  */
-struct connman_service *__connman_service_lookup_from_network(struct connman_network *network)
+struct connman_service *connman_service_lookup_from_network(struct connman_network *network)
 {
        struct connman_service *service;
        const char *ident, *group;
@@ -5644,6 +6154,11 @@ struct connman_service *__connman_service_lookup_from_index(int index)
        return NULL;
 }
 
+struct connman_service *__connman_service_lookup_from_ident(const char *identifier)
+{
+       return lookup_by_identifier(identifier);
+}
+
 const char *__connman_service_get_ident(struct connman_service *service)
 {
        return service->identifier;
@@ -5887,7 +6402,7 @@ struct connman_service * __connman_service_create_from_network(struct connman_ne
 
        if (service->favorite == TRUE) {
                device = connman_network_get_device(service->network);
-               if (device && __connman_device_scanning(device) == FALSE)
+               if (device && connman_device_get_scanning(device) == FALSE)
                        __connman_service_auto_connect();
        }
 
@@ -5909,7 +6424,7 @@ void __connman_service_update_from_network(struct connman_network *network)
 
        DBG("network %p", network);
 
-       service = __connman_service_lookup_from_network(network);
+       service = connman_service_lookup_from_network(network);
        if (service == NULL)
                return;
 
@@ -5920,9 +6435,11 @@ void __connman_service_update_from_network(struct connman_network *network)
        if (g_strcmp0(service->name, name) != 0) {
                g_free(service->name);
                service->name = g_strdup(name);
-               connman_dbus_property_changed_basic(service->path,
-                               CONNMAN_SERVICE_INTERFACE, "Name",
-                               DBUS_TYPE_STRING, &service->name);
+
+               if (allow_property_changed(service) == TRUE)
+                       connman_dbus_property_changed_basic(service->path,
+                                       CONNMAN_SERVICE_INTERFACE, "Name",
+                                       DBUS_TYPE_STRING, &service->name);
        }
 
        if (service->type == CONNMAN_SERVICE_TYPE_WIFI)
@@ -5970,7 +6487,7 @@ void __connman_service_remove_from_network(struct connman_network *network)
 
        DBG("network %p", network);
 
-       service = __connman_service_lookup_from_network(network);
+       service = connman_service_lookup_from_network(network);
        if (service == NULL)
                return;
 
@@ -6044,6 +6561,66 @@ __connman_service_create_from_provider(struct connman_provider *provider)
        return service;
 }
 
+static void remove_unprovisioned_services()
+{
+       gchar **services;
+       GKeyFile *keyfile, *configkeyfile;
+       char *file, *section;
+       int i = 0;
+
+       services = connman_storage_get_services();
+       if (services == NULL)
+               return;
+
+       for (;services[i] != NULL; i++) {
+               file = section = NULL;
+               keyfile = configkeyfile = NULL;
+
+               keyfile = connman_storage_load_service(services[i]);
+               if (keyfile == NULL)
+                       continue;
+
+               file = g_key_file_get_string(keyfile, services[i],
+                                       "Config.file", NULL);
+               if (file == NULL)
+                       goto next;
+
+               section = g_key_file_get_string(keyfile, services[i],
+                                       "Config.ident", NULL);
+               if (section == NULL)
+                       goto next;
+
+               configkeyfile = __connman_storage_load_config(file);
+               if (configkeyfile == NULL) {
+                       /*
+                        * Config file is missing, remove the provisioned
+                        * service.
+                        */
+                       __connman_storage_remove_service(services[i]);
+                       goto next;
+               }
+
+               if (g_key_file_has_group(configkeyfile, section) == FALSE)
+                       /*
+                        * Config section is missing, remove the provisioned
+                        * service.
+                        */
+                       __connman_storage_remove_service(services[i]);
+
+       next:
+               if (keyfile != NULL)
+                       g_key_file_free(keyfile);
+
+               if (configkeyfile != NULL)
+                       g_key_file_free(configkeyfile);
+
+               g_free(section);
+               g_free(file);
+       }
+
+       g_strfreev(services);
+}
+
 int __connman_service_init(void)
 {
        DBG("");
@@ -6060,6 +6637,8 @@ int __connman_service_init(void)
                        g_str_equal, g_free, NULL);
        services_notify->add = g_hash_table_new(g_str_hash, g_str_equal);
 
+       remove_unprovisioned_services();
+
        return 0;
 }