Make WiFi passphrase enc/dec routine async to prevent mutual synchronous call to...
[platform/upstream/connman.git] / plugins / wifi.c
index c0079b3..995fab4 100755 (executable)
@@ -71,6 +71,7 @@
 #define P2P_LISTEN_PERIOD 500
 #define P2P_LISTEN_INTERVAL 2000
 
+
 static struct connman_technology *wifi_technology = NULL;
 static struct connman_technology *p2p_technology = NULL;
 
@@ -133,10 +134,13 @@ struct wifi_data {
        struct connman_network *scan_pending_network;
        bool allow_full_scan;
 #endif
+       int disconnect_code;
+       int assoc_code;
 };
 
 #if defined TIZEN_EXT
 #include "connman.h"
+#include "dbus.h"
 
 #define TIZEN_ASSOC_RETRY_COUNT                4
 
@@ -154,6 +158,123 @@ bool wfd_service_registered = false;
 
 static void start_autoscan(struct connman_device *device);
 
+#if defined TIZEN_EXT
+#define NETCONFIG_SERVICE "net.netconfig"
+#define NETCONFIG_WIFI_PATH "/net/netconfig/wifi"
+#define NETCONFIG_WIFI_INTERFACE NETCONFIG_SERVICE ".wifi"
+
+struct enc_method_call_data {
+       DBusConnection *connection;
+       struct connman_network *network;
+};
+
+static struct enc_method_call_data encrypt_request_data;
+
+static void encryption_request_reply(DBusPendingCall *call,
+                                               void *user_data)
+{
+       DBusMessage *reply;
+       DBusError error;
+       DBusMessageIter args;
+       char *out_data;
+       struct connman_service *service;
+       gchar* encrypted_value = NULL;
+       struct connman_network *network = encrypt_request_data.network;
+
+       DBG("");
+
+       reply = dbus_pending_call_steal_reply(call);
+
+       dbus_error_init(&error);
+       if (dbus_set_error_from_message(&error, reply)) {
+               DBG("send_encryption_request() %s %s", error.name, error.message);
+               dbus_error_free(&error);
+               goto done;
+       }
+
+       if (dbus_message_iter_init(reply, &args) == FALSE)
+               goto done;
+
+       dbus_message_iter_get_basic(&args, &out_data);
+
+       encrypted_value = g_strdup((const gchar *)out_data);
+       service = connman_service_lookup_from_network(network);
+
+       if (!service) {
+               DBG("encryption result: no service");
+               goto done;
+       }
+
+       if (connman_service_get_favorite(service)) {
+               __connman_service_set_passphrase(service, encrypted_value);
+               __connman_service_save(service);
+       } else
+               connman_network_set_string(network, "WiFi.Passphrase",
+                                                       encrypted_value);
+
+       DBG("encryption result: succeeded");
+
+done:
+       dbus_message_unref(reply);
+       dbus_pending_call_unref(call);
+       dbus_connection_unref(encrypt_request_data.connection);
+       g_free(encrypted_value);
+
+       encrypt_request_data.connection = NULL;
+       encrypt_request_data.network = NULL;
+}
+
+static int send_encryption_request(const char *passphrase,
+                               struct connman_network *network)
+{
+       DBusConnection *connection = NULL;
+       DBusMessage *msg = NULL;
+       DBusPendingCall *call;
+
+       if (!passphrase) {
+               DBG("Invalid parameter");
+               return -EINVAL;
+       }
+
+       connection = connman_dbus_get_connection();
+       if (!connection) {
+               DBG("dbus connection does not exist");
+               return -EINVAL;
+       }
+
+       msg = dbus_message_new_method_call(NETCONFIG_SERVICE, NETCONFIG_WIFI_PATH,
+                       NETCONFIG_WIFI_INTERFACE, "EncryptPassphrase");
+       if (!msg) {
+               dbus_connection_unref(connection);
+               return -EINVAL;
+       }
+
+       dbus_message_append_args(msg, DBUS_TYPE_STRING, &passphrase,
+                                                       DBUS_TYPE_INVALID);
+
+       if (!dbus_connection_send_with_reply(connection, msg,
+                               &call, DBUS_TIMEOUT_USE_DEFAULT)) {
+               dbus_message_unref(msg);
+               dbus_connection_unref(connection);
+               return -EIO;
+       }
+
+       if (!call) {
+               dbus_message_unref(msg);
+               dbus_connection_unref(connection);
+               return -EIO;
+       }
+
+       encrypt_request_data.connection = connection;
+       encrypt_request_data.network = network;
+
+       dbus_pending_call_set_notify(call, encryption_request_reply, NULL, NULL);
+       dbus_message_unref(msg);
+
+       return 0;
+}
+#endif
+
 static int p2p_tech_probe(struct connman_technology *technology)
 {
        p2p_technology = technology;
@@ -529,6 +650,11 @@ static void register_peer_service_cb(int result,
        struct wifi_data *wifi = g_supplicant_interface_get_data(iface);
        struct peer_service_registration *reg_data = user_data;
 
+#if defined TIZEN_EXT
+       if (!wifi)
+               return;
+#endif
+
        DBG("");
 
        if (result == 0)
@@ -1183,8 +1309,11 @@ static int throw_wifi_scan(struct connman_device *device,
 
        if (wifi->tethering)
                return -EBUSY;
-
+#if defined TIZEN_EXT
+       if (connman_device_get_scanning(device) && !wifi->allow_full_scan)
+#else
        if (connman_device_get_scanning(device))
+#endif
                return -EALREADY;
 
        connman_device_ref(device);
@@ -1297,12 +1426,26 @@ static void scan_callback(int result, GSupplicantInterface *interface,
                return scan_callback(ret, interface, user_data);
        }
 
-       scanning = connman_device_get_scanning(device);
+#if defined TIZEN_EXT
+       if (wifi && wifi->allow_full_scan) {
+               int ret;
+               DBG("Trigger Full Channel Scan");
+               wifi->allow_full_scan = FALSE;
 
-       if (scanning) {
+               ret = g_supplicant_interface_scan(wifi->interface, NULL,
+                                                       scan_callback, device);
+               if (ret == 0)
+                       return;
+
+               /* On error, let's recall scan_callback, which will cleanup */
+               return scan_callback(ret, interface, user_data);
+       }
+#endif
+
+       scanning = connman_device_get_scanning(device);
+       if (scanning)
                connman_device_set_scanning(device,
                                CONNMAN_SERVICE_TYPE_WIFI, false);
-       }
 
        if (result != -ENOLINK)
 #if defined TIZEN_EXT
@@ -1321,11 +1464,6 @@ static void scan_callback(int result, GSupplicantInterface *interface,
                connman_device_unref(device);
 
 #if defined TIZEN_EXT
-       if (wifi && wifi->allow_full_scan) {
-               DBG("Trigger Full Channel Scan");
-               throw_wifi_scan(device, scan_callback);
-               wifi->allow_full_scan = FALSE;
-       }
        if (wifi && wifi->scan_pending_network && result != -EIO) {
                network_connect(wifi->scan_pending_network);
                wifi->scan_pending_network = NULL;
@@ -1825,6 +1963,137 @@ static int p2p_find(struct connman_device *device)
        return ret;
 }
 
+#if defined TIZEN_EXT
+static void specific_scan_callback(int result, GSupplicantInterface *interface,
+                                               void *user_data)
+{
+       struct connman_device *device = user_data;
+       struct wifi_data *wifi = connman_device_get_data(device);
+       bool scanning;
+
+       DBG("result %d wifi %p", result, wifi);
+
+       if (wifi && wifi->scan_params) {
+               g_supplicant_free_scan_params(wifi->scan_params);
+               wifi->scan_params = NULL;
+       }
+
+       scanning = connman_device_get_scanning(device);
+       if (scanning) {
+               connman_device_set_scanning(device,
+                               CONNMAN_SERVICE_TYPE_WIFI, false);
+               connman_device_unref(device);
+       }
+}
+
+static int wifi_specific_scan(enum connman_service_type type,
+                       struct connman_device *device, int scan_type,
+                       GSList *specific_scan_list, void *user_data)
+{
+       GSList *list = NULL;
+       char *ssid = NULL;
+       struct wifi_data *wifi = connman_device_get_data(device);
+       GSupplicantScanParams *scan_params = NULL;
+       struct scan_ssid *scan_ssid = NULL;
+       bool scanning;
+       int ret;
+       int freq;
+       int count = 0;
+
+       if (!wifi)
+               return -ENODEV;
+
+       if (wifi->p2p_device)
+               return 0;
+
+       if (type == CONNMAN_SERVICE_TYPE_P2P)
+               return p2p_find(device);
+
+       if (wifi->tethering)
+               return 0;
+
+       scanning = connman_device_get_scanning(device);
+       if (scanning)
+               return -EALREADY;
+
+       DBG("scan_type: %d", scan_type);
+       if (scan_type == 1) { /* ssid based scan */
+               scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
+               if (!scan_params) {
+                       DBG("Failed to allocate memory.");
+                       return -ENOMEM;
+               }
+
+               for (list = specific_scan_list; list; list = list->next) {
+                       ssid = (char *)list->data;
+                       int ssid_len = strlen(ssid);
+
+                       scan_ssid = g_try_new0(struct scan_ssid, 1);
+                       if (!scan_ssid) {
+                               DBG("Failed to allocate memory.");
+                               g_supplicant_free_scan_params(scan_params);
+                               return -ENOMEM;
+                       }
+
+                       memcpy(scan_ssid->ssid, ssid, (ssid_len + 1));
+                       DBG("scan ssid %s len: %d", scan_ssid->ssid, ssid_len);
+                       scan_ssid->ssid_len = ssid_len;
+                       scan_params->ssids = g_slist_prepend(scan_params->ssids, scan_ssid);
+                       count++;
+               }
+               scan_params->num_ssids = count;
+
+       } else if (scan_type == 2) { /* frequency based scan */
+
+               scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
+               if (!scan_params) {
+                       DBG("Failed to allocate memory.");
+                       return -ENOMEM;
+               }
+
+               guint num_freqs = g_slist_length(specific_scan_list);
+               DBG("num_freqs: %d", num_freqs);
+
+               scan_params->freqs = g_try_new0(uint16_t, num_freqs);
+               if (!scan_params->freqs) {
+                       DBG("Failed to allocate memory.");
+                       g_free(scan_params);
+                       return -ENOMEM;
+               }
+
+               count = 0;
+               for (list = specific_scan_list; list; list = list->next) {
+                       freq = (int)list->data;
+
+                       scan_params->freqs[count] = freq;
+                       DBG("scan_params->freqs[%d]: %d", count, scan_params->freqs[count]);
+                       count++;
+               }
+               scan_params->num_freqs = count;
+
+       } else {
+               DBG("Invalid scan");
+               return -EINVAL;
+       }
+
+       reset_autoscan(device);
+       connman_device_ref(device);
+
+       ret = g_supplicant_interface_scan(wifi->interface, scan_params,
+                                               specific_scan_callback, device);
+
+       if (ret == 0) {
+               connman_device_set_scanning(device,
+                               CONNMAN_SERVICE_TYPE_WIFI, true);
+       } else {
+               g_supplicant_free_scan_params(scan_params);
+               connman_device_unref(device);
+       }
+
+       return ret;
+}
+#endif
+
 /*
  * Note that the hidden scan is only used when connecting to this specific
  * hidden AP first time. It is not used when system autoconnects to hidden AP.
@@ -1935,13 +2204,6 @@ static int wifi_scan(enum connman_service_type type,
 
        connman_device_ref(device);
 
-#if defined TIZEN_EXT
-       /*To allow the Full Scan after ssid based scan, set the flag here
-     It is required because Tizen does not use the ConnMan specific
-     backgroung Scan feature.Tizen has added the BG Scan feature in net-config
-     To sync with up ConnMan, we need to issue the Full Scan after SSID specific scan.*/
-        wifi->allow_full_scan = TRUE;
-#endif
        reset_autoscan(device);
 
        ret = g_supplicant_interface_scan(wifi->interface, scan_params,
@@ -1950,6 +2212,14 @@ static int wifi_scan(enum connman_service_type type,
        if (ret == 0) {
                connman_device_set_scanning(device,
                                CONNMAN_SERVICE_TYPE_WIFI, true);
+#if defined TIZEN_EXT
+               /*To allow the Full Scan after ssid based scan, set the flag here
+                 It is required because Tizen does not use the ConnMan specific
+                 backgroung Scan feature.Tizen has added the BG Scan feature in
+                 net-config. To sync with up ConnMan, we need to issue the Full Scan
+                 after SSID specific scan.*/
+               wifi->allow_full_scan = TRUE;
+#endif
        } else {
                g_supplicant_free_scan_params(scan_params);
                connman_device_unref(device);
@@ -2003,6 +2273,9 @@ static struct connman_device_driver wifi_ng_driver = {
        .disable        = wifi_disable,
        .scan           = wifi_scan,
        .set_regdom     = wifi_set_regdom,
+#if defined TIZEN_EXT
+       .specific_scan  = wifi_specific_scan,
+#endif
 };
 
 static void system_ready(void)
@@ -2126,7 +2399,6 @@ static void ssid_init(GSupplicantSSID *ssid, struct connman_network *network)
        ssid->security = network_security(security);
        ssid->passphrase = connman_network_get_string(network,
                                                "WiFi.Passphrase");
-
        ssid->eap = connman_network_get_string(network, "WiFi.EAP");
 
        /*
@@ -2163,6 +2435,9 @@ static void ssid_init(GSupplicantSSID *ssid, struct connman_network *network)
 #if defined TIZEN_EXT
        ssid->bssid = connman_network_get_bssid(network);
 #endif
+#if defined TIZEN_EXT
+       ssid->freq = connman_network_get_frequency(network);
+#endif
 
        if (connman_setting_get_bool("BackgroundScanning"))
                ssid->bgscan = BGSCAN_DEFAULT;
@@ -2457,10 +2732,35 @@ static bool handle_wps_completion(GSupplicantInterface *interface,
                }
 
                wps_key = g_supplicant_interface_get_wps_key(interface);
+#if defined TIZEN_EXT
+               /* Check the passphrase and encrypt it
+                */
+                int ret;
+                gchar *passphrase = g_strdup(wps_key);
+
+                connman_network_set_string(network, "WiFi.PinWPS", NULL);
+
+                if (check_passphrase_ext(network, passphrase) < 0) {
+                        DBG("[WPS] Invalid passphrase");
+                        g_free(passphrase);
+                        return true;
+                }
+
+                ret = send_encryption_request(passphrase, network);
+
+                g_free(passphrase);
+
+                if (!ret)
+                        DBG("[WPS] Encryption request succeeded");
+                else
+                        DBG("[WPS] Encryption request failed %d", ret);
+
+#else
                connman_network_set_string(network, "WiFi.Passphrase",
                                        wps_key);
 
                connman_network_set_string(network, "WiFi.PinWPS", NULL);
+#endif
        }
 
        return true;
@@ -2479,7 +2779,7 @@ static bool handle_4way_handshake_failure(GSupplicantInterface *interface,
 
        security = connman_network_get_string(network, "WiFi.Security");
 
-       if (g_str_equal(security, "ieee8021x") == true &&
+       if (security && g_str_equal(security, "ieee8021x") == true &&
                        wifi->state == G_SUPPLICANT_STATE_ASSOCIATED) {
                wifi->retries = 0;
                connman_network_set_error(network, CONNMAN_NETWORK_ERROR_INVALID_KEY);
@@ -2535,7 +2835,7 @@ static bool handle_wifi_assoc_retry(struct connman_network *network,
        }
 
        security = connman_network_get_string(network, "WiFi.Security");
-       if (g_str_equal(security, "ieee8021x") == true &&
+       if (security && g_str_equal(security, "ieee8021x") == true &&
                        wifi->state == G_SUPPLICANT_STATE_ASSOCIATED) {
                wifi->assoc_retry_count = 0;
                return false;
@@ -2611,6 +2911,14 @@ static void interface_state(GSupplicantInterface *interface)
                wifi->assoc_retry_count = 0;
 
                wifi->scan_pending_network = NULL;
+
+               /* should be cleared scanning flag */
+               bool scanning = connman_device_get_scanning(device);
+               if (scanning){
+                       connman_device_set_scanning(device,
+                               CONNMAN_SERVICE_TYPE_WIFI, false);
+                       connman_device_unref(device);
+               }
 #else
                /* though it should be already stopped: */
                stop_autoscan(device);
@@ -2620,6 +2928,8 @@ static void interface_state(GSupplicantInterface *interface)
                        break;
 
                connman_network_set_connected(network, true);
+               wifi->disconnect_code = 0;
+               wifi->assoc_code = 0;
                break;
 
        case G_SUPPLICANT_STATE_DISCONNECTED:
@@ -2645,6 +2955,21 @@ static void interface_state(GSupplicantInterface *interface)
                                                network, wifi))
                        break;
 
+               /* See table 8-36 Reason codes in IEEE Std 802.11 */
+               switch (wifi->disconnect_code) {
+               case 1: /* Unspecified reason */
+                       /* Let's assume it's because we got blocked */
+
+               case 6: /* Class 2 frame received from nonauthenticated STA */
+                       connman_network_set_error(network,
+                                               CONNMAN_NETWORK_ERROR_BLOCKED);
+                       break;
+
+               default:
+                       break;
+               }
+
+
                /* We disable the selected network, if not then
                 * wpa_supplicant will loop retrying */
                if (g_supplicant_interface_enable_selected_network(interface,
@@ -2653,13 +2978,11 @@ static void interface_state(GSupplicantInterface *interface)
 
 #if defined TIZEN_EXT
                int err;
-               int reason_code = 0;
 
                err = g_supplicant_interface_remove_network(wifi->interface);
                if (err < 0)
                        DBG("Failed to remove network(%d)", err);
 
-               reason_code = g_supplicant_interface_get_disconnect_reason(wifi->interface);
 
                /* Some of Wi-Fi networks are not comply Wi-Fi specification.
                 * Retry association until its retry count is expired */
@@ -2669,9 +2992,9 @@ static void interface_state(GSupplicantInterface *interface)
                        break;
                }
 
-               if(reason_code > 0){
-                       DBG("Set disconnect reason code(%d)", reason_code);
-                       connman_network_set_disconnect_reason(network, reason_code);
+               if(wifi->disconnect_code > 0){
+                       DBG("Set disconnect reason code(%d)", wifi->disconnect_code);
+                       connman_network_set_disconnect_reason(network, wifi->disconnect_code);
                }
 
                /* To avoid unnecessary repeated association in wpa_supplicant,
@@ -2876,9 +3199,11 @@ static unsigned char calculate_strength(GSupplicantNetwork *supplicant_network)
        unsigned char strength;
 
        strength = 120 + g_supplicant_network_get_signal(supplicant_network);
+
+#if !defined TIZEN_EXT
        if (strength > 100)
                strength = 100;
-
+#endif
        return strength;
 }
 
@@ -2895,6 +3220,11 @@ static void network_added(GSupplicantNetwork *supplicant_network)
        bool wps_ready;
        bool wps_advertizing;
 
+#if defined TIZEN_EXT
+       const char *wifi_vsie;
+       unsigned int wifi_vsie_len;
+#endif
+
        mode = g_supplicant_network_get_mode(supplicant_network);
        identifier = g_supplicant_network_get_identifier(supplicant_network);
 
@@ -2919,6 +3249,9 @@ static void network_added(GSupplicantNetwork *supplicant_network)
 
        ssid = g_supplicant_network_get_ssid(supplicant_network, &ssid_len);
 
+#if defined TIZEN_EXT
+       wifi_vsie = g_supplicant_network_get_wifi_vsie(supplicant_network, &wifi_vsie_len);
+#endif
        network = connman_device_get_network(wifi->device, identifier);
 
        if (!network) {
@@ -2942,6 +3275,11 @@ static void network_added(GSupplicantNetwork *supplicant_network)
 
        connman_network_set_blob(network, "WiFi.SSID",
                                                ssid, ssid_len);
+#if defined TIZEN_EXT
+       if(wifi_vsie_len > 0 && wifi_vsie)
+               connman_network_set_blob(network, "WiFi.Vsie",
+                                                       wifi_vsie, wifi_vsie_len);
+#endif
        connman_network_set_string(network, "WiFi.Security", security);
        connman_network_set_strength(network,
                                calculate_strength(supplicant_network));
@@ -2973,6 +3311,8 @@ static void network_added(GSupplicantNetwork *supplicant_network)
                        g_supplicant_network_get_rsn_mode(supplicant_network));
        connman_network_set_keymgmt(network,
                        g_supplicant_network_get_keymgmt(supplicant_network));
+       connman_network_set_bool(network, "WiFi.HS20AP",
+                       g_supplicant_network_is_hs20AP(supplicant_network));
 #endif
        connman_network_set_available(network, true);
        connman_network_set_string(network, "WiFi.Mode", mode);
@@ -3130,7 +3470,10 @@ static void peer_found(GSupplicantPeer *peer)
        struct connman_peer *connman_peer;
        const char *identifier, *name;
        int ret;
-
+#if defined TIZEN_EXT
+       if (!wifi)
+               return;
+#endif
        identifier = g_supplicant_peer_get_identifier(peer);
        name = g_supplicant_peer_get_name(peer);
 
@@ -3183,6 +3526,11 @@ static void peer_changed(GSupplicantPeer *peer, GSupplicantPeerState state)
        struct connman_peer *connman_peer;
        const char *identifier;
 
+#if defined TIZEN_EXT
+       if (!wifi)
+               return;
+#endif
+
        identifier = g_supplicant_peer_get_identifier(peer);
 
        DBG("ident: %s", identifier);
@@ -3261,6 +3609,11 @@ static void peer_request(GSupplicantPeer *peer)
        struct connman_peer *connman_peer;
        const char *identifier;
 
+#if defined TIZEN_EXT
+       if (!wifi)
+               return;
+#endif
+
        identifier = g_supplicant_peer_get_identifier(peer);
 
        DBG("ident: %s", identifier);
@@ -3300,7 +3653,7 @@ static void network_merged(GSupplicantNetwork *network)
        struct wifi_data *wifi;
        const char *identifier;
        struct connman_network *connman_network;
-       unsigned int ishs20AP = 0;
+       bool ishs20AP = 0;
        char *temp = NULL;
 
        interface = g_supplicant_network_get_interface(network);
@@ -3342,7 +3695,6 @@ static void network_merged(GSupplicantNetwork *network)
        }
 
        ishs20AP = g_supplicant_network_is_hs20AP(network);
-       connman_network_set_is_hs20AP(connman_network, ishs20AP);
 
        if (ishs20AP &&
                g_strcmp0(g_supplicant_network_get_security(network), "ieee8021x") == 0) {
@@ -3359,6 +3711,12 @@ static void network_merged(GSupplicantNetwork *network)
 
        wifi->network = connman_network;
 }
+
+static void assoc_failed(void *user_data)
+{
+       struct connman_network *network = user_data;
+       connman_network_set_associating(network, false);
+}
 #endif
 
 static void debug(const char *str)
@@ -3367,6 +3725,34 @@ static void debug(const char *str)
                connman_debug("%s", str);
 }
 
+static void disconnect_reasoncode(GSupplicantInterface *interface,
+                               int reasoncode)
+{
+       struct wifi_data *wifi = g_supplicant_interface_get_data(interface);
+       if (wifi != NULL) {
+               wifi->disconnect_code = reasoncode;
+       }
+}
+
+static void assoc_status_code(GSupplicantInterface *interface, int status_code)
+{
+       struct wifi_data *wifi = g_supplicant_interface_get_data(interface);
+
+#if defined TIZEN_EXT
+       struct connman_network *network;
+#endif
+
+       if (wifi != NULL) {
+               wifi->assoc_code = status_code;
+
+#if defined TIZEN_EXT
+               network = wifi->network;
+               connman_network_set_assoc_status_code(network,status_code);
+#endif
+
+       }
+}
+
 static const GSupplicantCallbacks callbacks = {
        .system_ready           = system_ready,
        .system_killed          = system_killed,
@@ -3387,8 +3773,11 @@ static const GSupplicantCallbacks callbacks = {
        .peer_request           = peer_request,
 #if defined TIZEN_EXT
        .system_power_off       = system_power_off,
-       .network_merged = network_merged,
+       .network_merged         = network_merged,
+       .assoc_failed           = assoc_failed,
 #endif
+       .disconnect_reasoncode  = disconnect_reasoncode,
+       .assoc_status_code      = assoc_status_code,
        .debug                  = debug,
 };