Remove the unused code
[platform/upstream/connman.git] / src / service.c
index 87a2f2c..1eec7ca 100644 (file)
@@ -30,6 +30,8 @@
 #include <gdbus.h>
 #include <ctype.h>
 #include <stdint.h>
+#include <pwd.h>
+#include <utmpx.h>
 
 #include <connman/storage.h>
 #include <connman/setting.h>
 
 #define CONNECT_TIMEOUT                120
 
+#define USER_ROOT              0
+#define USER_NONE              (uid_t)-1
+
+#if defined TIZEN_EXT
+#define WIFI_BSSID_STR_LEN     18
+#endif
+
 static DBusConnection *connection = NULL;
 
 static GList *service_list = NULL;
@@ -63,6 +72,11 @@ struct connman_stats_counter {
        struct connman_stats stats_roaming;
 };
 
+struct connman_service_user {
+       uid_t favorite_user;
+       uid_t current_user;
+};
+
 struct connman_service {
        int refcount;
        char *identifier;
@@ -85,6 +99,8 @@ struct connman_service {
        char *name;
        char *passphrase;
        bool roaming;
+       bool request_passphrase_input;
+       struct connman_service_user user;
        struct connman_ipconfig *ipconfig_ipv4;
        struct connman_ipconfig *ipconfig_ipv6;
        struct connman_network *network;
@@ -124,6 +140,15 @@ struct connman_service {
        bool hidden_service;
        char *config_file;
        char *config_entry;
+#if defined TIZEN_EXT
+       /*
+        * Description: TIZEN implements system global connection management.
+        *              It's only for PDP (cellular) bearer. Wi-Fi is managed
+        *              by ConnMan automatically. Reference count can help to
+        *              manage open/close connection requests by each application.
+        */
+       int user_pdn_connection_refcount;
+#endif
 };
 
 static bool allow_property_changed(struct connman_service *service);
@@ -139,6 +164,50 @@ struct find_data {
        struct connman_service *service;
 };
 
+#if defined TIZEN_EXT
+/*
+ * Public APIs to use user_pdn_connection_refcount
+ */
+void connman_service_user_pdn_connection_ref(struct connman_service *service)
+{
+       __sync_fetch_and_add(&service->user_pdn_connection_refcount, 1);
+
+       DBG("User made PDN connection referenced: %d",
+                               service->user_pdn_connection_refcount);
+}
+
+gboolean connman_service_user_pdn_connection_unref_and_test(
+                                       struct connman_service *service)
+{
+       __sync_synchronize();
+
+       DBG("User made PDN connection referenced: %d, which will be decreased",
+                               service->user_pdn_connection_refcount);
+
+       if (service->user_pdn_connection_refcount < 1)
+               return TRUE;
+
+       if (__sync_sub_and_fetch(&service->user_pdn_connection_refcount, 1) == 0)
+               return TRUE;
+
+       return FALSE;
+}
+
+gboolean connman_service_is_no_ref_user_pdn_connection(
+                                       struct connman_service *cellular)
+{
+       if (cellular == NULL)
+               return TRUE;
+
+       __sync_synchronize();
+       if (cellular->type == CONNMAN_SERVICE_TYPE_CELLULAR &&
+                       cellular->user_pdn_connection_refcount == 0)
+               return TRUE;
+
+       return FALSE;
+}
+#endif
+
 static void compare_path(gpointer value, gpointer user_data)
 {
        struct connman_service *service = value;
@@ -347,6 +416,83 @@ static enum connman_service_proxy_method string2proxymethod(const char *method)
                return CONNMAN_SERVICE_PROXY_METHOD_UNKNOWN;
 }
 
+static bool
+connman_service_is_user_allowed(struct connman_service *service, uid_t uid)
+{
+       uid_t favorite_user = service->user.favorite_user;
+       uid_t current_user = uid;
+
+       DBG("Service favorite UID: %d, current UID: %d", favorite_user, current_user);
+       if (favorite_user == USER_NONE || current_user == USER_ROOT)
+               return true;
+
+       if (favorite_user != current_user || current_user == USER_NONE) {
+               DBG("Current user is not a favorite user to this service!");
+               return false;
+       }
+
+       return true;
+}
+
+static GList *connman_service_get_login_users()
+{
+       struct utmpx *utmp;
+       struct passwd *pwd;
+       GList *user_list = NULL;
+
+       setutxent();
+
+       while ((utmp = getutxent()) != NULL) {
+               DBG("User Name: %s", utmp->ut_user);
+
+               pwd = getpwnam(utmp->ut_user);
+               if (pwd) {
+                       if (!g_list_find(user_list, GUINT_TO_POINTER(pwd->pw_uid)))
+                               user_list = g_list_append(user_list,
+                                               GUINT_TO_POINTER(pwd->pw_uid));
+
+                       DBG("User Name: %s, UID: %d", utmp->ut_user, pwd->pw_uid);
+               }
+       }
+
+       endutxent();
+
+       return user_list;
+}
+
+static bool is_service_owner_user_login(struct connman_service *service)
+{
+       GList *list, *user_list;
+       bool ret = false;
+
+       /* Here we only care about wifi service */
+       if (service->type != CONNMAN_SERVICE_TYPE_WIFI)
+               return true;
+
+       DBG("service favorite user id is: %d", service->user.favorite_user);
+
+       user_list = connman_service_get_login_users();
+       if (user_list == NULL) {
+               DBG("Can not get any logged in user info.");
+               return true;
+       }
+
+       for (list = user_list; list; list = list->next) {
+               uid_t uid = GPOINTER_TO_UINT(list->data);
+
+               DBG("login user id is %d", uid);
+
+               if (service->user.favorite_user == uid) {
+                       ret = true;
+                       break;
+               }
+       }
+
+       g_list_free(user_list);
+
+       return ret;
+}
+
 int __connman_service_load_modifiable(struct connman_service *service)
 {
        GKeyFile *keyfile;
@@ -395,6 +541,23 @@ int __connman_service_load_modifiable(struct connman_service *service)
        return 0;
 }
 
+static int service_load_passphrase(struct connman_service *service)
+{
+       GKeyFile *keyfile;
+       gchar *str;
+
+       keyfile = connman_storage_load_service(service->identifier);
+       if (!keyfile)
+               return -EIO;
+
+       str = g_key_file_get_string(keyfile,
+                               service->identifier, "Passphrase", NULL);
+       if (str)
+               service->passphrase = str;
+
+       return 0;
+}
+
 static int service_load(struct connman_service *service)
 {
        GKeyFile *keyfile;
@@ -570,6 +733,9 @@ static int service_load(struct connman_service *service)
        service->hidden_service = g_key_file_get_boolean(keyfile,
                                        service->identifier, "Hidden", NULL);
 
+       if (g_key_file_has_key(keyfile, service->identifier, "UID", NULL))
+               service->user.favorite_user = g_key_file_get_integer(keyfile,
+                                       service->identifier, "UID", NULL);
 done:
        g_key_file_free(keyfile);
 
@@ -615,6 +781,13 @@ static int service_save(struct connman_service *service)
                        const unsigned char *ssid;
                        unsigned int ssid_len = 0;
 
+                       if (service->user.favorite_user == USER_NONE)
+                               g_key_file_remove_key(keyfile, service->identifier,
+                                                       "UID", NULL);
+                       else
+                               g_key_file_set_integer(keyfile, service->identifier,
+                                                       "UID", service->user.favorite_user);
+
                        ssid = connman_network_get_blob(service->network,
                                                        "WiFi.SSID", &ssid_len);
 
@@ -670,12 +843,14 @@ static int service_save(struct connman_service *service)
                g_free(str);
        }
 
-       if (service->passphrase && strlen(service->passphrase) > 0)
-               g_key_file_set_string(keyfile, service->identifier,
+       if (service->user.current_user == service->user.favorite_user) {
+               if (service->passphrase && strlen(service->passphrase) > 0)
+                       g_key_file_set_string(keyfile, service->identifier,
                                        "Passphrase", service->passphrase);
-       else
-               g_key_file_remove_key(keyfile, service->identifier,
-                                                       "Passphrase", NULL);
+               else
+                       g_key_file_remove_key(keyfile, service->identifier,
+                                       "Passphrase", NULL);
+       }
 
        if (service->ipconfig_ipv4)
                __connman_ipconfig_save(service->ipconfig_ipv4, keyfile,
@@ -1396,6 +1571,71 @@ static void reset_stats(struct connman_service *service)
        g_timer_reset(service->stats_roaming.timer);
 }
 
+#if defined TIZEN_EXT
+static gboolean __connman_service_is_internet_profile(
+               struct connman_service *cellular)
+{
+       const char internet_suffix[] = "_1";
+
+       DBG("Service path: %s", cellular->path);
+
+       if (g_str_has_suffix(cellular->path, internet_suffix) == TRUE)
+               return TRUE;
+
+       return FALSE;
+}
+
+static gboolean __connman_service_is_tethering_profile(
+               struct connman_service *cellular)
+{
+       const char tethering_suffix[] = "_5";
+
+       DBG("Service path: %s", cellular->path);
+
+       if (g_str_has_suffix(cellular->path, tethering_suffix) == TRUE)
+               return TRUE;
+
+       return FALSE;
+}
+
+struct connman_service *connman_service_get_default_connection(void)
+{
+       GList *list;
+       struct connman_service *service;
+       struct connman_service *default_service = NULL;
+
+       for (list = service_list; list; list = list->next) {
+               service = list->data;
+
+               DBG("service: %p %s %s %s", service, service->name,
+                               state2string(service->state),
+                               __connman_service_type2string(service->type));
+
+               if (service->type == CONNMAN_SERVICE_TYPE_WIFI &&
+                               is_connected(service) == TRUE) {
+                       return service;
+               } else if (service->type == CONNMAN_SERVICE_TYPE_CELLULAR &&
+                               __connman_service_is_internet_profile(service) == TRUE) {
+                       if (default_service == NULL)
+                               default_service = service;
+                       else if (is_connected(service) == TRUE &&
+                                       is_connected(default_service) == FALSE)
+                               default_service = service;
+               } else if (service->type == CONNMAN_SERVICE_TYPE_ETHERNET &&
+                               is_connected(service) == TRUE) {
+                       if (default_service == NULL)
+                               default_service = service;
+               } else if (service->type == CONNMAN_SERVICE_TYPE_BLUETOOTH &&
+                               is_connected(service) == TRUE) {
+                       if (default_service == NULL)
+                               default_service = service;
+               }
+       }
+
+       return default_service;
+}
+#endif
+
 struct connman_service *__connman_service_get_default(void)
 {
        struct connman_service *service;
@@ -2252,6 +2492,37 @@ int __connman_service_iterate_services(service_iterate_cb cb, void *user_data)
        return 0;
 }
 
+#if defined TIZEN_EXT
+static void append_wifi_ext_info(DBusMessageIter *dict,
+                                       struct connman_network *network)
+{
+       char bssid_buff[WIFI_BSSID_STR_LEN] = {0,};
+       char *bssid_str = bssid_buff;
+       unsigned char *bssid;
+       unsigned int maxrate;
+       uint16_t frequency;
+       const char *enc_mode;
+
+       bssid = connman_network_get_bssid(network);
+       maxrate = connman_network_get_maxrate(network);
+       frequency = connman_network_get_frequency(network);
+       enc_mode = connman_network_get_enc_mode(network);
+
+       snprintf(bssid_str, WIFI_BSSID_STR_LEN, "%02x:%02x:%02x:%02x:%02x:%02x",
+                               bssid[0], bssid[1], bssid[2],
+                               bssid[3], bssid[4], bssid[5]);
+
+       connman_dbus_dict_append_basic(dict, "BSSID",
+                                       DBUS_TYPE_STRING, &bssid_str);
+       connman_dbus_dict_append_basic(dict, "MaxRate",
+                                       DBUS_TYPE_UINT32, &maxrate);
+       connman_dbus_dict_append_basic(dict, "Frequency",
+                                       DBUS_TYPE_UINT16, &frequency);
+       connman_dbus_dict_append_basic(dict, "EncryptionMode",
+                                       DBUS_TYPE_STRING, &enc_mode);
+}
+#endif
+
 static void append_properties(DBusMessageIter *dict, dbus_bool_t limited,
                                        struct connman_service *service)
 {
@@ -2317,6 +2588,14 @@ static void append_properties(DBusMessageIter *dict, dbus_bool_t limited,
                                                append_ethernet, service);
                break;
        case CONNMAN_SERVICE_TYPE_WIFI:
+#if defined TIZEN_EXT
+               if (service->network != NULL)
+                       append_wifi_ext_info(dict, service->network);
+
+               connman_dbus_dict_append_dict(dict, "Ethernet",
+                                               append_ethernet, service);
+               break;
+#endif
        case CONNMAN_SERVICE_TYPE_ETHERNET:
        case CONNMAN_SERVICE_TYPE_BLUETOOTH:
        case CONNMAN_SERVICE_TYPE_GADGET:
@@ -2549,6 +2828,54 @@ char **connman_service_get_timeservers(struct connman_service *service)
        return service->timeservers;
 }
 
+#if defined TIZEN_EXT
+/*
+ * Description: Telephony plug-in requires manual PROXY setting function
+ */
+int connman_service_set_proxy(struct connman_service *service,
+                                       const char *proxy, gboolean active)
+{
+       char **proxies_array = NULL;
+
+       if (service == NULL)
+               return -EINVAL;
+
+       switch (service->type) {
+       case CONNMAN_SERVICE_TYPE_CELLULAR:
+       case CONNMAN_SERVICE_TYPE_ETHERNET:
+       case CONNMAN_SERVICE_TYPE_WIFI:
+               break;
+
+       default:
+               return -EINVAL;
+       }
+
+       g_strfreev(service->proxies);
+       service->proxies = NULL;
+
+       if (proxy != NULL)
+               proxies_array = g_strsplit(proxy, " ", 0);
+
+       service->proxies = proxies_array;
+
+       if (proxy == NULL) {
+               service->proxy_config = CONNMAN_SERVICE_PROXY_METHOD_DIRECT;
+               DBG("proxy changed (%d)", active);
+       } else {
+               service->proxy_config = CONNMAN_SERVICE_PROXY_METHOD_MANUAL;
+               DBG("proxy chagned %s (%d)", proxy, active);
+       }
+
+       if (active == TRUE) {
+               proxy_changed(service);
+
+               __connman_notifier_proxy_changed(service);
+       }
+
+       return 0;
+}
+#endif
+
 void connman_service_set_proxy_method(struct connman_service *service,
                                        enum connman_service_proxy_method method)
 {
@@ -2763,6 +3090,22 @@ void __connman_service_set_pac(struct connman_service *service,
        proxy_changed(service);
 }
 
+#if defined TIZEN_EXT
+void __connman_service_set_proxy(struct connman_service *service,
+                                       const char *proxies)
+{
+       char **proxies_array = NULL;
+
+       g_strfreev(service->proxies);
+       service->proxies = NULL;
+
+       if (proxies != NULL)
+               proxies_array = g_strsplit(proxies, " ", 0);
+
+       service->proxies = proxies_array;
+}
+#endif
+
 void __connman_service_set_identity(struct connman_service *service,
                                        const char *identity)
 {
@@ -3177,6 +3520,21 @@ static DBusMessage *set_property(DBusConnection *conn,
        if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
                return __connman_error_invalid_arguments(msg);
 
+       if (service->type == CONNMAN_SERVICE_TYPE_WIFI && is_connected(service)) {
+               uid_t uid;
+               if (connman_dbus_get_connection_unix_user_sync(conn,
+                                               dbus_message_get_sender(msg),
+                                               &uid) < 0) {
+                       DBG("Can not get unix user id!");
+                       return __connman_error_permission_denied(msg);
+               }
+
+               if (!connman_service_is_user_allowed(service, uid)) {
+                       DBG("Not allow this user to operate this wifi service now!");
+                       return __connman_error_permission_denied(msg);
+               }
+       }
+
        dbus_message_iter_get_basic(&iter, &name);
        dbus_message_iter_next(&iter);
 
@@ -3624,6 +3982,14 @@ static GList *preferred_tech_list_get(void)
                                        CONNMAN_SERVICE_CONNECT_REASON_USER) {
                                DBG("service %p name %s is user connected",
                                                service, service->name);
+#if defined TIZEN_EXT
+                               /* We can connect to a favorite service like
+                                * wifi even we have a userconnect for cellular
+                                * because we have refount for cellular service
+                                */
+                               if (service->type == CONNMAN_SERVICE_TYPE_CELLULAR)
+                                       break;
+#endif
                                return NULL;
                        }
                }
@@ -3693,6 +4059,11 @@ static bool auto_connect_service(GList *services,
                        continue;
                }
 
+               if (!is_service_owner_user_login(service)) {
+                       DBG("favorite user not login, wifi auto connect denied");
+                       continue;
+               }
+
                DBG("service %p %s %s", service, service->name,
                        (preferred) ? "preferred" : reason2string(reason));
 
@@ -3935,14 +4306,51 @@ static DBusMessage *connect_service(DBusConnection *conn,
 
        DBG("service %p", service);
 
+#if defined TIZEN_EXT
+       /*
+        * Description: TIZEN implements system global connection management.
+        */
+       if (service->type == CONNMAN_SERVICE_TYPE_CELLULAR)
+               connman_service_user_pdn_connection_ref(service);
+#endif
+
        if (service->pending)
                return __connman_error_in_progress(msg);
 
+       if (service->type == CONNMAN_SERVICE_TYPE_WIFI) {
+               uid_t uid;
+               if (connman_dbus_get_connection_unix_user_sync(conn,
+                                               dbus_message_get_sender(msg),
+                                               &uid) < 0) {
+                       DBG("Can not get unix user id!");
+                       return __connman_error_permission_denied(msg);
+               }
+
+               if (!__connman_service_is_user_allowed(CONNMAN_SERVICE_TYPE_WIFI, uid)) {
+                       DBG("Not allow this user to connect this wifi service now!");
+                       return __connman_error_permission_denied(msg);
+               }
+
+               if (uid != USER_ROOT && uid != service->user.favorite_user)
+                       service->request_passphrase_input = true;
+
+               service->user.current_user = uid;
+
+               if (!service->passphrase && uid == service->user.favorite_user) {
+                       DBG("Now load this favorite user's passphrase.");
+                       service_load_passphrase(service);
+               }
+       }
+
        index = __connman_service_get_index(service);
 
        for (list = service_list; list; list = list->next) {
                struct connman_service *temp = list->data;
 
+#if defined TIZEN_EXT
+               if (service->type == CONNMAN_SERVICE_TYPE_CELLULAR)
+                       break;
+#endif
                if (!is_connecting(temp) && !is_connected(temp))
                        break;
 
@@ -3989,6 +4397,35 @@ static DBusMessage *disconnect_service(DBusConnection *conn,
 
        DBG("service %p", service);
 
+#if defined TIZEN_EXT
+       /*
+        * Description: TIZEN implements system global connection management.
+        */
+       if (service->type == CONNMAN_SERVICE_TYPE_CELLULAR) {
+               if (connman_service_user_pdn_connection_unref_and_test(service) != TRUE)
+                       return __connman_error_failed(msg, EISCONN);
+
+               if (is_connected(service) == TRUE &&
+                               service == connman_service_get_default_connection())
+                       return __connman_error_failed(msg, EISCONN);
+       }
+#endif
+
+       if (service->type == CONNMAN_SERVICE_TYPE_WIFI) {
+               uid_t uid;
+               if (connman_dbus_get_connection_unix_user_sync(conn,
+                                               dbus_message_get_sender(msg),
+                                               &uid) < 0) {
+                       DBG("Can not get unix user id!");
+                       return __connman_error_permission_denied(msg);
+               }
+
+               if (!connman_service_is_user_allowed(service, uid)) {
+                       DBG("Not allow this user to disconnect this wifi service now!");
+                       return __connman_error_permission_denied(msg);
+               }
+       }
+
        service->ignore = true;
 
        err = __connman_service_disconnect(service);
@@ -4028,6 +4465,8 @@ bool __connman_service_remove(struct connman_service *service)
 
        service->error = CONNMAN_SERVICE_ERROR_UNKNOWN;
 
+       service->user.favorite_user = USER_NONE;
+
        __connman_service_set_favorite(service, false);
 
        __connman_ipconfig_ipv6_reset_privacy(service->ipconfig_ipv6);
@@ -4044,6 +4483,21 @@ static DBusMessage *remove_service(DBusConnection *conn,
 
        DBG("service %p", service);
 
+       if (service->type == CONNMAN_SERVICE_TYPE_WIFI) {
+               uid_t uid;
+               if (connman_dbus_get_connection_unix_user_sync(conn,
+                                               dbus_message_get_sender(msg),
+                                               &uid) < 0) {
+                       DBG("Can not get unix user id!");
+                       return __connman_error_permission_denied(msg);
+               }
+
+               if (!connman_service_is_user_allowed(service, uid)) {
+                       DBG("Not allow this user to remove this wifi service now!");
+                       return __connman_error_permission_denied(msg);
+               }
+       }
+
        if (!__connman_service_remove(service))
                return __connman_error_not_supported(msg);
 
@@ -4253,6 +4707,30 @@ static DBusMessage *reset_counters(DBusConnection *conn,
        return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
 }
 
+static DBusMessage *get_user_favorite(DBusConnection *conn,
+                                       DBusMessage *msg, void *user_data)
+{
+       DBusMessage *reply;
+       uid_t uid = USER_NONE;
+       dbus_bool_t user_favorite = false;
+       struct connman_service *service = user_data;
+
+       connman_dbus_get_connection_unix_user_sync(conn,
+                                       dbus_message_get_sender(msg),
+                                       &uid);
+       if (uid == USER_ROOT)
+               user_favorite = service->favorite;
+       else if (uid != USER_NONE && uid == service->user.favorite_user) {
+               DBG("The service is favorite to this user!");
+               user_favorite = true;
+       }
+
+       reply = g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
+       dbus_message_append_args(reply, DBUS_TYPE_BOOLEAN,
+                               &user_favorite, DBUS_TYPE_INVALID);
+       return reply;
+}
+
 static struct _services_notify {
        int id;
        GHashTable *add;
@@ -4394,6 +4872,9 @@ static const GDBusMethodTable service_methods[] = {
                        GDBUS_ARGS({ "service", "o" }), NULL,
                        move_after) },
        { GDBUS_METHOD("ResetCounters", NULL, NULL, reset_counters) },
+       { GDBUS_METHOD("GetUserFavorite",
+                       NULL, GDBUS_ARGS({ "value", "v" }),
+                       get_user_favorite) },
        { },
 };
 
@@ -4524,6 +5005,11 @@ static void service_initialize(struct connman_service *service)
 
        service->ignore = false;
 
+       service->user.favorite_user = USER_NONE;
+       service->user.current_user = USER_NONE;
+
+       service->request_passphrase_input = false;
+
        service->connect_reason = CONNMAN_SERVICE_CONNECT_REASON_NONE;
 
        service->order = 0;
@@ -4533,6 +5019,13 @@ static void service_initialize(struct connman_service *service)
        service->provider = NULL;
 
        service->wps = false;
+#if defined TIZEN_EXT
+       /*
+        * Description: TIZEN implements system global connection management.
+        */
+       service->user_pdn_connection_refcount = 0;
+       __sync_synchronize();
+#endif
 }
 
 /**
@@ -4750,6 +5243,40 @@ char *connman_service_get_interface(struct connman_service *service)
 }
 
 /**
+ * __connman_service_is_user_allowed:
+ * @type: service type
+ * @uid: user id
+ *
+ * Check a user is allowed to operate a type of service
+ */
+bool __connman_service_is_user_allowed(enum connman_service_type type,
+                                       uid_t uid)
+{
+       GList *list;
+       uid_t owner_user = USER_NONE;
+
+       for (list = service_list; list; list = list->next) {
+               struct connman_service *service = list->data;
+
+               if (service->type != type)
+                       continue;
+
+               if (is_connected(service)) {
+                       owner_user = service->user.favorite_user;
+                       break;
+               }
+       }
+
+       if (uid == USER_NONE ||
+                       (uid != USER_ROOT &&
+                       owner_user != USER_NONE &&
+                       owner_user != uid))
+               return false;
+
+       return true;
+}
+
+/**
  * connman_service_get_network:
  * @service: service structure
  *
@@ -4841,6 +5368,46 @@ void __connman_service_mark_dirty(void)
        services_dirty = true;
 }
 
+#if defined TIZEN_EXT
+/**
+  * Returns profile count if there is any connected profiles
+  * that use same interface
+  */
+int __connman_service_get_connected_count_of_iface(
+                                       struct connman_service *service)
+{
+       GList *list;
+       int count = 0;
+       int index1 = 0;
+       int index2 = 0;
+
+       DBG("");
+
+       index1 = __connman_service_get_index(service);
+
+       if (index1 <= 0)
+               return 0;
+
+       for (list = service_list; list; list = list->next) {
+               struct connman_service *service2 = list->data;
+
+               if (service == service2)
+                       continue;
+
+               index2 = __connman_service_get_index(service2);
+
+               if (is_connected(service2) && index2 > 0 && index1 == index2)
+                       count++;
+
+               index2 = 0;
+       }
+
+       DBG("Interface index %d, count %d", index1, count);
+
+       return count;
+}
+#endif
+
 /**
  * __connman_service_set_favorite_delayed:
  * @service: service structure
@@ -4853,6 +5420,10 @@ int __connman_service_set_favorite_delayed(struct connman_service *service,
                                        bool favorite,
                                        bool delay_ordering)
 {
+#if defined TIZEN_EXT
+       if (service->type == CONNMAN_SERVICE_TYPE_CELLULAR)
+               return -EIO;
+#endif
        if (service->hidden)
                return -EOPNOTSUPP;
 
@@ -4988,6 +5559,21 @@ void __connman_service_update_search_domains(struct connman_service *service,
        service->domains = g_strdupv(domains);
 }
 
+#if defined TIZEN_EXT
+void __connman_service_set_autoconnect(struct connman_service *service,
+                                               bool autoconnect)
+{
+       if (service == NULL)
+               return;
+
+       if (service->autoconnect != autoconnect) {
+               DBG("updated autoconnect flag (%d)", autoconnect);
+               service->autoconnect = autoconnect;
+               service_save(service);
+       }
+}
+#endif
+
 static void service_complete(struct connman_service *service)
 {
        reply_pending(service, EIO);
@@ -5192,6 +5778,126 @@ static int service_update_preferred_order(struct connman_service *default_servic
        return -EALREADY;
 }
 
+#if defined TIZEN_EXT
+static gboolean __connman_service_can_drop(struct connman_service *service)
+{
+       if (is_connected(service) == TRUE || is_connecting(service) == TRUE) {
+               if (service->type != CONNMAN_SERVICE_TYPE_CELLULAR)
+                       return TRUE;
+               else if (connman_service_is_no_ref_user_pdn_connection(service) == TRUE)
+                       return TRUE;
+       }
+       return FALSE;
+}
+
+static struct connman_device *default_connecting_device = NULL;
+
+static void __connman_service_disconnect_default(struct connman_service *service)
+{
+       struct connman_device *default_device = NULL;
+
+       if (default_connecting_device == NULL)
+               return;
+
+       default_device = connman_network_get_device(
+                       __connman_service_get_network(service));
+
+       DBG("Disconnecting service %p %s", service, service->path);
+       DBG("Disconnecting device %p %p %s",
+                       default_connecting_device,
+                       default_device,
+                       connman_device_get_string(default_device, "Name"));
+
+       if (default_connecting_device == default_device)
+               default_connecting_device = NULL;
+}
+
+static void __connman_service_connect_default(struct connman_service *current)
+{
+       int err;
+       GList *list;
+       bool default_internet;
+       struct connman_service *service;
+       struct connman_service *default_service = NULL;
+       struct connman_device *default_device = NULL;
+
+       if (current->type == CONNMAN_SERVICE_TYPE_CELLULAR) {
+               switch (current->state) {
+               case CONNMAN_SERVICE_STATE_UNKNOWN:
+               case CONNMAN_SERVICE_STATE_ASSOCIATION:
+               case CONNMAN_SERVICE_STATE_CONFIGURATION:
+                       return;
+               default:
+                       break;
+               }
+
+               if (default_connecting_device &&
+                               __connman_service_is_internet_profile(current) == TRUE) {
+                       if (current->network == NULL)
+                               return;
+
+                       default_device = connman_network_get_device(current->network);
+                       if (default_connecting_device == default_device) {
+                               DBG("Cellular service[%s]  %p %s",
+                                               state2string(current->state), current, current->path);
+                               DBG("Cellular device %p %p %s",
+                                               default_connecting_device, default_device,
+                                               connman_device_get_string(default_device, "Name"));
+
+                               default_connecting_device = NULL;
+                       }
+               }
+
+               return;
+       } else if (is_connected(current) == TRUE || is_connecting(current) == TRUE)
+               return;
+
+       /* Always-on: keep default cellular connection as possible */
+       for (list = service_list; list; list = list->next) {
+               service = list->data;
+
+               if (service->type != CONNMAN_SERVICE_TYPE_CELLULAR ||
+                               __connman_service_is_internet_profile(service) != TRUE ||
+                               service->network == NULL) {
+                       continue;
+               }
+
+               default_internet =
+                               connman_network_get_bool(service->network, "DefaultInternet");
+
+               DBG("service: %p %s %s %s (default: %d)", service, service->name,
+                               __connman_service_type2string(service->type),
+                               state2string(service->state), default_internet);
+
+               if (default_internet) {
+                       default_service = service;
+                       if (is_connected(default_service) == TRUE ||
+                                       is_connecting(default_service) == TRUE)
+                               return;
+
+                       default_device = connman_network_get_device(default_service->network);
+                       if (default_connecting_device == default_device) {
+                               DBG("Device is connecting (%p)", default_connecting_device);
+                               return;
+                       }
+
+                       default_connecting_device = default_device;
+                       default_service->connect_reason = CONNMAN_SERVICE_CONNECT_REASON_USER;
+
+                       err = __connman_network_connect(default_service->network);
+                       DBG("Connecting default service %p %s [%d]",
+                                       default_service, default_service->path, err);
+                       DBG("Connecting device %p %s", default_connecting_device,
+                                       connman_device_get_string(default_connecting_device, "Name"));
+                       if (err < 0 && err != -EINPROGRESS) {
+                               default_connecting_device = NULL;
+                       } else
+                               break;
+               }
+       }
+}
+#endif
+
 static void single_connected_tech(struct connman_service *allowed)
 {
        struct connman_service *service;
@@ -5203,12 +5909,16 @@ static void single_connected_tech(struct connman_service *allowed)
        for (iter = service_list; iter; iter = iter->next) {
                service = iter->data;
 
+#if defined TIZEN_EXT
+               if (service != allowed && service->type != allowed->type &&
+                               __connman_service_can_drop(service) == TRUE)
+#else
                if (!is_connected(service))
                        break;
 
                if (service == allowed)
                        continue;
-
+#endif
                services = g_slist_prepend(services, service);
        }
 
@@ -5216,6 +5926,9 @@ static void single_connected_tech(struct connman_service *allowed)
                service = list->data;
 
                DBG("disconnecting %p %s", service, service->path);
+#if defined TIZEN_EXT
+               __connman_service_disconnect_default(service);
+#endif
                __connman_service_disconnect(service);
        }
 
@@ -5381,9 +6094,21 @@ static int service_indicate_state(struct connman_service *service)
 
                __connman_wpad_stop(service);
 
+#if defined TIZEN_EXT
+               /**
+                 * Skip the functions if there is any connected profiles
+                 * that use same interface
+                 */
+               if (service->type != CONNMAN_SERVICE_TYPE_CELLULAR ||
+                       __connman_service_get_connected_count_of_iface(
+                                                       service) <= 0) {
+#endif
                dns_changed(service);
                domain_changed(service);
                proxy_changed(service);
+#if defined TIZEN_EXT
+               }
+#endif
 
                /*
                 * Previous services which are connected and which states
@@ -5414,6 +6139,10 @@ static int service_indicate_state(struct connman_service *service)
 
        service_list_sort();
 
+#if defined TIZEN_EXT
+       __connman_service_connect_default(service);
+#endif
+
        __connman_connection_update_gateway();
 
        if ((old_state == CONNMAN_SERVICE_STATE_ONLINE &&
@@ -5428,6 +6157,20 @@ static int service_indicate_state(struct connman_service *service)
                default_changed();
        }
 
+       if (service->type == CONNMAN_SERVICE_TYPE_WIFI &&
+               service->connect_reason == CONNMAN_SERVICE_CONNECT_REASON_USER &&
+               (new_state == CONNMAN_SERVICE_STATE_READY ||
+               new_state == CONNMAN_SERVICE_STATE_ONLINE)) {
+               if (service->user.favorite_user != service->user.current_user) {
+                       DBG("Now set service favorite user id from %d to %d",
+                       service->user.favorite_user, service->user.current_user);
+
+                       service->user.favorite_user = service->user.current_user;
+
+                       service_save(service);
+               }
+       }
+
        return 0;
 }
 
@@ -5683,6 +6426,18 @@ int __connman_service_ipconfig_indicate_state(struct connman_service *service,
        if (*old_state == new_state)
                return -EALREADY;
 
+#if defined TIZEN_EXT
+       __sync_synchronize();
+       if (service->user_pdn_connection_refcount > 0 &&
+                       service->type == CONNMAN_SERVICE_TYPE_CELLULAR)
+               if (new_state == CONNMAN_SERVICE_STATE_FAILURE ||
+                               new_state == CONNMAN_SERVICE_STATE_DISCONNECT ||
+                               new_state == CONNMAN_SERVICE_STATE_IDLE) {
+                       service->user_pdn_connection_refcount = 0;
+                       __sync_synchronize();
+               }
+#endif
+
        DBG("service %p (%s) old state %d (%s) new state %d (%s) type %d (%s)",
                service, service ? service->identifier : NULL,
                *old_state, state2string(*old_state),
@@ -5698,6 +6453,15 @@ int __connman_service_ipconfig_indicate_state(struct connman_service *service,
                __connman_ipconfig_enable(ipconfig);
                break;
        case CONNMAN_SERVICE_STATE_READY:
+#if defined TIZEN_EXT
+               if (service->type == CONNMAN_SERVICE_TYPE_CELLULAR &&
+                               __connman_service_is_internet_profile(service) != TRUE) {
+                       if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
+                               service_rp_filter(service, TRUE);
+
+                       break;
+               }
+#endif
                if (type == CONNMAN_IPCONFIG_TYPE_IPV4) {
                        check_proxy_setup(service);
                        service_rp_filter(service, true);
@@ -5839,7 +6603,11 @@ static int service_connect(struct connman_service *service)
                case CONNMAN_SERVICE_SECURITY_PSK:
                case CONNMAN_SERVICE_SECURITY_WPA:
                case CONNMAN_SERVICE_SECURITY_RSN:
-                       if (!service->passphrase) {
+                       if (service->request_passphrase_input) {
+                               DBG("Now try to connect other user's favorite service");
+                               service->request_passphrase_input = false;
+                               return -ENOKEY;
+                       } else if (!service->passphrase) {
                                if (!service->network)
                                        return -EOPNOTSUPP;
 
@@ -5853,13 +6621,24 @@ static int service_connect(struct connman_service *service)
                        if (!service->eap)
                                return -EINVAL;
 
+#if defined TIZEN_EXT
+                       /*
+                        * never request credentials if using EAP-TLS, EAP-SIM
+                        * or EAP-AKA (EAP-TLS, EAP-SIM and EAP-AKA networks
+                        * need to be fully provisioned)
+                        */
+                       if (g_str_equal(service->eap, "tls") ||
+                               g_str_equal(service->eap, "sim") ||
+                               g_str_equal(service->eap, "aka"))
+                               break;
+#else
                        /*
                         * never request credentials if using EAP-TLS
                         * (EAP-TLS networks need to be fully provisioned)
                         */
                        if (g_str_equal(service->eap, "tls"))
                                break;
-
+#endif
                        /*
                         * Return -ENOKEY if either identity or passphrase is
                         * missing. Agent provided credentials can be used as
@@ -6044,6 +6823,14 @@ int __connman_service_disconnect(struct connman_service *service)
                __connman_ipconfig_set_proxy_autoconfig(service->ipconfig_ipv6,
                                                        NULL);
 
+#if defined TIZEN_EXT
+       /**
+         * Skip the functions If there is any connected profiles
+         * that use same interface
+         */
+       if (service->type != CONNMAN_SERVICE_TYPE_CELLULAR ||
+               __connman_service_get_connected_count_of_iface(service) <= 0) {
+#endif
        __connman_ipconfig_address_remove(service->ipconfig_ipv4);
        settings_changed(service, service->ipconfig_ipv4);
 
@@ -6052,6 +6839,9 @@ int __connman_service_disconnect(struct connman_service *service)
 
        __connman_ipconfig_disable(service->ipconfig_ipv4);
        __connman_ipconfig_disable(service->ipconfig_ipv6);
+#if defined TIZEN_EXT
+       }
+#endif
 
        __connman_stats_service_unregister(service);
 
@@ -6518,6 +7308,28 @@ unsigned int __connman_service_get_order(struct connman_service *service)
        if (!service->favorite)
                return 0;
 
+#if defined TIZEN_EXT
+       if (service->type == CONNMAN_SERVICE_TYPE_VPN &&
+                       service->do_split_routing == FALSE)
+               order = 10;
+       else if (service->type == CONNMAN_SERVICE_TYPE_WIFI) {
+               if (service->order < 5)
+                       order = 5;
+       } else if (service->type == CONNMAN_SERVICE_TYPE_ETHERNET)
+               order = 4;
+       else if (service->type == CONNMAN_SERVICE_TYPE_BLUETOOTH)
+               order = 3;
+       else if (service->type == CONNMAN_SERVICE_TYPE_CELLULAR &&
+                       __connman_service_is_internet_profile(service) == TRUE)
+               order = 1;
+       else if (service->type == CONNMAN_SERVICE_TYPE_CELLULAR &&
+                       __connman_service_is_tethering_profile(service) == TRUE)
+               order = 0;
+       else if (service->type == CONNMAN_SERVICE_TYPE_CELLULAR)
+               order = 0;
+       else
+               order = 2;
+#else
        if (service == service_list->data)
                order = 1;
 
@@ -6526,7 +7338,7 @@ unsigned int __connman_service_get_order(struct connman_service *service)
                service->order = 10;
                order = 10;
        }
-
+#endif
        DBG("service %p name %s order %d split %d", service, service->name,
                order, service->do_split_routing);