*
* Connection Manager
*
- * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
+ * Copyright (C) 2007-2012 Intel Corporation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
+#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <connman/device.h>
#include <connman/rtnl.h>
#include <connman/technology.h>
+#include <connman/service.h>
#include <connman/log.h>
#include <connman/option.h>
+#include <connman/storage.h>
+#include <include/setting.h>
+#include <connman/provision.h>
#include <gsupplicant/gsupplicant.h>
#define CLEANUP_TIMEOUT 8 /* in seconds */
#define INACTIVE_TIMEOUT 12 /* in seconds */
+#define MAXIMUM_RETRIES 2
+#define FAVORITE_MAXIMUM_RETRIES 4
-struct connman_technology *wifi_technology = NULL;
+#define BGSCAN_DEFAULT "simple:30:-45:300"
+#define AUTOSCAN_DEFAULT "exponential:3:300"
+
+static struct connman_technology *wifi_technology = NULL;
+
+struct hidden_params {
+ char ssid[32];
+ unsigned int ssid_len;
+ char *identity;
+ char *passphrase;
+ gpointer user_data;
+};
+
+/**
+ * Used for autoscan "emulation".
+ * Should be removed when wpa_s autoscan support will be by default.
+ */
+struct autoscan_params {
+ int base;
+ int limit;
+ int interval;
+ unsigned int timeout;
+};
struct wifi_data {
char *identifier;
struct connman_device *device;
struct connman_network *network;
struct connman_network *pending_network;
+ GSList *networks;
GSupplicantInterface *interface;
GSupplicantState state;
connman_bool_t connected;
connman_bool_t disconnecting;
+ connman_bool_t tethering;
+ connman_bool_t bridged;
+ const char *bridge;
int index;
unsigned flags;
unsigned int watch;
+ int retries;
+ struct hidden_params *hidden;
+ /**
+ * autoscan "emulation".
+ */
+ struct autoscan_params *autoscan;
};
-static int get_bssid(struct connman_device *device,
- unsigned char *bssid, unsigned int *bssid_len)
-{
- struct iwreq wrq;
- char *ifname;
- int ifindex;
- int fd, err;
-
- ifindex = connman_device_get_index(device);
- if (ifindex < 0)
- return -EINVAL;
+static GList *iface_list = NULL;
- ifname = connman_inet_ifname(ifindex);
- if (ifname == NULL)
- return -EINVAL;
-
- fd = socket(PF_INET, SOCK_DGRAM, 0);
- if (fd < 0) {
- g_free(ifname);
- return -EINVAL;
- }
+static void start_autoscan(struct connman_device *device);
- memset(&wrq, 0, sizeof(wrq));
- strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
+static void handle_tethering(struct wifi_data *wifi)
+{
+ if (wifi->tethering == FALSE)
+ return;
- err = ioctl(fd, SIOCGIWAP, &wrq);
+ if (wifi->bridge == NULL)
+ return;
- g_free(ifname);
- close(fd);
+ if (wifi->bridged == TRUE)
+ return;
- if (err < 0)
- return -EIO;
+ DBG("index %d bridge %s", wifi->index, wifi->bridge);
- memcpy(bssid, wrq.u.ap_addr.sa_data, ETH_ALEN);
- *bssid_len = ETH_ALEN;
+ if (connman_inet_add_to_bridge(wifi->index, wifi->bridge) < 0)
+ return;
- return 0;
+ wifi->bridged = TRUE;
}
static void wifi_newlink(unsigned flags, unsigned change, void *user_data)
struct connman_device *device = user_data;
struct wifi_data *wifi = connman_device_get_data(device);
- DBG("index %d flags %d change %d", wifi->index, flags, change);
-
- if (!change)
+ if (wifi == NULL)
return;
+ DBG("index %d flags %d change %d", wifi->index, flags, change);
+
if ((wifi->flags & IFF_UP) != (flags & IFF_UP)) {
if (flags & IFF_UP)
DBG("interface up");
}
if ((wifi->flags & IFF_LOWER_UP) != (flags & IFF_LOWER_UP)) {
- if (flags & IFF_LOWER_UP)
+ if (flags & IFF_LOWER_UP) {
DBG("carrier on");
- else
+
+ handle_tethering(wifi);
+ } else
DBG("carrier off");
}
wifi->connected = FALSE;
wifi->disconnecting = FALSE;
+ wifi->tethering = FALSE;
+ wifi->bridged = FALSE;
+ wifi->bridge = NULL;
wifi->state = G_SUPPLICANT_STATE_INACTIVE;
connman_device_set_data(device, wifi);
wifi->watch = connman_rtnl_add_newlink_watch(wifi->index,
wifi_newlink, device);
+ iface_list = g_list_append(iface_list, wifi);
+
return 0;
}
+static void remove_networks(struct connman_device *device,
+ struct wifi_data *wifi)
+{
+ GSList *list;
+
+ for (list = wifi->networks; list != NULL; list = list->next) {
+ struct connman_network *network = list->data;
+
+ connman_device_remove_network(device, network);
+ connman_network_unref(network);
+ }
+
+ g_slist_free(wifi->networks);
+ wifi->networks = NULL;
+}
+
+static void reset_autoscan(struct connman_device *device)
+{
+ struct wifi_data *wifi = connman_device_get_data(device);
+ struct autoscan_params *autoscan;
+
+ DBG("");
+
+ if (wifi == NULL || wifi->autoscan == NULL)
+ return;
+
+ autoscan = wifi->autoscan;
+
+ if (autoscan->timeout == 0 && autoscan->interval == 0)
+ return;
+
+ g_source_remove(autoscan->timeout);
+
+ autoscan->timeout = 0;
+ autoscan->interval = 0;
+
+ connman_device_unref(device);
+}
+
+static void stop_autoscan(struct connman_device *device)
+{
+ reset_autoscan(device);
+
+ connman_device_set_scanning(device, FALSE);
+}
+
static void wifi_remove(struct connman_device *device)
{
struct wifi_data *wifi = connman_device_get_data(device);
- DBG("device %p", device);
+ DBG("device %p wifi %p", device, wifi);
- if (wifi->pending_network != NULL) {
- connman_network_unref(wifi->pending_network);
- wifi->pending_network = NULL;
- }
+ if (wifi == NULL)
+ return;
+
+ iface_list = g_list_remove(iface_list, wifi);
+ remove_networks(device, wifi);
+
+ connman_device_set_powered(device, FALSE);
connman_device_set_data(device, NULL);
connman_device_unref(wifi->device);
connman_rtnl_remove_watch(wifi->watch);
g_supplicant_interface_set_data(wifi->interface, NULL);
+ g_free(wifi->autoscan);
g_free(wifi->identifier);
g_free(wifi);
}
-static void interface_create_callback(int result,
- GSupplicantInterface *interface,
- void *user_data)
+static gboolean is_duplicate(GSList *list, gchar *ssid, int ssid_len)
{
- struct wifi_data *wifi = user_data;
+ GSList *iter;
- DBG("result %d ifname %s", result,
- g_supplicant_interface_get_ifname(interface));
+ for (iter = list; iter != NULL; iter = g_slist_next(iter)) {
+ struct scan_ssid *scan_ssid = iter->data;
- if (result < 0)
- return;
+ if (ssid_len == scan_ssid->ssid_len &&
+ memcmp(ssid, scan_ssid->ssid, ssid_len) == 0)
+ return TRUE;
+ }
- wifi->interface = interface;
- g_supplicant_interface_set_data(interface, wifi);
+ return FALSE;
}
-static void interface_remove_callback(int result,
- GSupplicantInterface *interface,
- void *user_data)
+static int add_scan_param(gchar *hex_ssid, char *raw_ssid, int ssid_len,
+ int freq, GSupplicantScanParams *scan_data,
+ int driver_max_scan_ssids, char *ssid_name)
{
- struct wifi_data *wifi = user_data;
+ unsigned int i;
+ struct scan_ssid *scan_ssid;
+
+ if (driver_max_scan_ssids > scan_data->num_ssids &&
+ (hex_ssid != NULL || raw_ssid != NULL)) {
+ gchar *ssid;
+ unsigned int j = 0, hex;
+
+ if (hex_ssid != NULL) {
+ size_t hex_ssid_len = strlen(hex_ssid);
+
+ ssid = g_try_malloc0(hex_ssid_len / 2);
+ if (ssid == NULL)
+ return -ENOMEM;
+
+ for (i = 0; i < hex_ssid_len; i += 2) {
+ sscanf(hex_ssid + i, "%02x", &hex);
+ ssid[j++] = hex;
+ }
+ } else {
+ ssid = raw_ssid;
+ j = ssid_len;
+ }
- DBG("result %d", result);
+ /*
+ * If we have already added hidden AP to the list,
+ * then do not do it again. This might happen if you have
+ * used or are using multiple wifi cards, so in that case
+ * you might have multiple service files for same AP.
+ */
+ if (is_duplicate(scan_data->ssids, ssid, j) == TRUE)
+ return 0;
- if (result < 0)
- return;
+ scan_ssid = g_try_new(struct scan_ssid, 1);
+ if (scan_ssid == NULL) {
+ g_free(ssid);
+ return -ENOMEM;
+ }
- wifi->interface = NULL;
-}
+ memcpy(scan_ssid->ssid, ssid, j);
+ scan_ssid->ssid_len = j;
+ scan_data->ssids = g_slist_prepend(scan_data->ssids,
+ scan_ssid);
+ scan_data->num_ssids++;
-static int wifi_enable(struct connman_device *device)
+ DBG("SSID %s added to scanned list of %d entries", ssid_name,
+ scan_data->num_ssids);
+
+ if (hex_ssid != NULL)
+ g_free(ssid);
+ } else
+ return -EINVAL;
+
+ scan_data->ssids = g_slist_reverse(scan_data->ssids);
+
+ if (scan_data->freqs == NULL) {
+ scan_data->freqs = g_try_malloc0(sizeof(uint16_t) *
+ scan_data->num_ssids);
+ if (scan_data->freqs == NULL) {
+ g_slist_free_full(scan_data->ssids, g_free);
+ return -ENOMEM;
+ }
+ } else {
+ scan_data->freqs = g_try_realloc(scan_data->freqs,
+ sizeof(uint16_t) * scan_data->num_ssids);
+ if (scan_data->freqs == NULL) {
+ g_slist_free_full(scan_data->ssids, g_free);
+ return -ENOMEM;
+ }
+ scan_data->freqs[scan_data->num_ssids - 1] = 0;
+ }
+
+ /* Don't add duplicate entries */
+ for (i = 0; i < scan_data->num_ssids; i++) {
+ if (scan_data->freqs[i] == 0) {
+ scan_data->freqs[i] = freq;
+ break;
+ } else if (scan_data->freqs[i] == freq)
+ break;
+ }
+
+ return 1;
+}
+
+static int get_hidden_connections(int max_ssids,
+ GSupplicantScanParams *scan_data)
{
- struct wifi_data *wifi = connman_device_get_data(device);
- const char *interface = connman_device_get_string(device, "Interface");
- const char *driver = connman_option_get_string("wifi");
+ struct connman_config_entry **entries;
+ GKeyFile *keyfile;
+ gchar **services;
+ char *ssid, *name;
+ int i, freq, ret;
+ gboolean value;
+ int num_ssids = 0, add_param_failed = 0;
+
+ services = connman_storage_get_services();
+ for (i = 0; services && services[i]; i++) {
+ if (strncmp(services[i], "wifi_", 5) != 0)
+ continue;
+
+ keyfile = connman_storage_load_service(services[i]);
+ if (keyfile == NULL)
+ continue;
+
+ value = g_key_file_get_boolean(keyfile,
+ services[i], "Hidden", NULL);
+ if (value == FALSE) {
+ g_key_file_free(keyfile);
+ continue;
+ }
- DBG("device %p %p", device, wifi);
+ value = g_key_file_get_boolean(keyfile,
+ services[i], "Favorite", NULL);
+ if (value == FALSE) {
+ g_key_file_free(keyfile);
+ continue;
+ }
- return g_supplicant_interface_create(interface, driver,
- interface_create_callback,
- wifi);
+ ssid = g_key_file_get_string(keyfile,
+ services[i], "SSID", NULL);
+
+ freq = g_key_file_get_integer(keyfile, services[i],
+ "Frequency", NULL);
+
+ name = g_key_file_get_string(keyfile, services[i], "Name",
+ NULL);
+
+ ret = add_scan_param(ssid, NULL, 0, freq, scan_data,
+ max_ssids, name);
+ if (ret < 0)
+ add_param_failed++;
+ else if (ret > 0)
+ num_ssids++;
+
+ g_free(name);
+ g_key_file_free(keyfile);
+ }
+
+ /*
+ * Check if there are any hidden AP that needs to be provisioned.
+ */
+ entries = connman_config_get_entries("wifi");
+ for (i = 0; entries && entries[i]; i++) {
+ int len;
+
+ if (entries[i]->hidden == FALSE)
+ continue;
+
+ if (entries[i]->ssid == NULL) {
+ ssid = entries[i]->name;
+ len = strlen(ssid);
+ } else {
+ ssid = entries[i]->ssid;
+ len = entries[i]->ssid_len;
+ }
+
+ if (ssid == NULL)
+ continue;
+
+ ret = add_scan_param(NULL, ssid, len, 0, scan_data,
+ max_ssids, ssid);
+ if (ret < 0)
+ add_param_failed++;
+ else if (ret > 0)
+ num_ssids++;
+ }
+
+ connman_config_free_entries(entries);
+
+ if (add_param_failed > 0)
+ DBG("Unable to scan %d out of %d SSIDs (max is %d)",
+ add_param_failed, num_ssids, max_ssids);
+
+ g_strfreev(services);
+
+ return num_ssids > max_ssids ? max_ssids : num_ssids;
}
-static int wifi_disable(struct connman_device *device)
+static int throw_wifi_scan(struct connman_device *device,
+ GSupplicantInterfaceCallback callback)
{
struct wifi_data *wifi = connman_device_get_data(device);
+ int ret;
- DBG("device %p", device);
+ if (wifi == NULL)
+ return -ENODEV;
- wifi->connected = FALSE;
- wifi->disconnecting = FALSE;
+ DBG("device %p %p", device, wifi->interface);
- if (wifi->pending_network != NULL) {
- connman_network_unref(wifi->pending_network);
- wifi->pending_network = NULL;
- }
+ if (wifi->tethering == TRUE)
+ return -EBUSY;
- return g_supplicant_interface_remove(wifi->interface,
- interface_remove_callback,
- wifi);
+ if (connman_device_get_scanning(device) == TRUE)
+ return -EALREADY;
+
+ connman_device_ref(device);
+
+ ret = g_supplicant_interface_scan(wifi->interface, NULL,
+ callback, device);
+ if (ret == 0)
+ connman_device_set_scanning(device, TRUE);
+ else
+ connman_device_unref(device);
+
+ return ret;
+}
+
+static void hidden_free(struct hidden_params *hidden)
+{
+ if (hidden == NULL)
+ return;
+
+ g_free(hidden->identity);
+ g_free(hidden->passphrase);
+ g_free(hidden);
}
static void scan_callback(int result, GSupplicantInterface *interface,
void *user_data)
{
struct connman_device *device = user_data;
+ struct wifi_data *wifi = connman_device_get_data(device);
+
+ DBG("result %d wifi %p", result, wifi);
+
+ if (wifi != NULL && wifi->hidden != NULL) {
+ connman_network_clear_hidden(wifi->hidden->user_data);
+ hidden_free(wifi->hidden);
+ wifi->hidden = NULL;
+ }
- DBG("result %d", result);
+ if (result < 0)
+ connman_device_reset_scanning(device);
connman_device_set_scanning(device, FALSE);
+
+ if (result != -ENOLINK)
+ start_autoscan(device);
+
+ connman_device_unref(device);
}
-static int wifi_scan(struct connman_device *device)
+static void scan_callback_hidden(int result,
+ GSupplicantInterface *interface, void *user_data)
{
+ struct connman_device *device = user_data;
struct wifi_data *wifi = connman_device_get_data(device);
+ GSupplicantScanParams *scan_params;
+ int driver_max_ssids, ret;
- DBG("device %p %p", device, wifi->interface);
+ DBG("result %d wifi %p", result, wifi);
+
+ if (wifi == NULL)
+ goto out;
+
+ /*
+ * Scan hidden networks so that we can autoconnect to them.
+ * We will assume 1 as a default number of ssid to scan.
+ */
+ driver_max_ssids = g_supplicant_interface_get_max_scan_ssids(
+ wifi->interface);
+ if (driver_max_ssids == 0)
+ driver_max_ssids = 1;
+
+ DBG("max ssids %d", driver_max_ssids);
+
+ scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
+ if (scan_params == NULL)
+ goto out;
+
+ if (get_hidden_connections(driver_max_ssids, scan_params) > 0) {
+ ret = g_supplicant_interface_scan(wifi->interface,
+ scan_params,
+ scan_callback,
+ device);
+ if (ret == 0)
+ return;
+ }
- return g_supplicant_interface_scan(wifi->interface, scan_callback,
- device);
+ g_supplicant_free_scan_params(scan_params);
+
+out:
+ scan_callback(result, interface, user_data);
}
-static struct connman_device_driver wifi_ng_driver = {
- .name = "wifi",
- .type = CONNMAN_DEVICE_TYPE_WIFI,
- .priority = CONNMAN_DEVICE_PRIORITY_LOW,
- .probe = wifi_probe,
- .remove = wifi_remove,
- .enable = wifi_enable,
- .disable = wifi_disable,
- .scan = wifi_scan,
-};
+static gboolean autoscan_timeout(gpointer data)
+{
+ struct connman_device *device = data;
+ struct wifi_data *wifi = connman_device_get_data(device);
+ struct autoscan_params *autoscan;
+ int interval;
-static void system_ready(void)
+ if (wifi == NULL)
+ return FALSE;
+
+ autoscan = wifi->autoscan;
+
+ if (autoscan->interval <= 0) {
+ interval = autoscan->base;
+ goto set_interval;
+ } else
+ interval = autoscan->interval * autoscan->base;
+
+ if (autoscan->interval >= autoscan->limit)
+ interval = autoscan->limit;
+
+ throw_wifi_scan(wifi->device, scan_callback_hidden);
+
+set_interval:
+ DBG("interval %d", interval);
+
+ autoscan->interval = interval;
+
+ autoscan->timeout = g_timeout_add_seconds(interval,
+ autoscan_timeout, device);
+
+ return FALSE;
+}
+
+static void start_autoscan(struct connman_device *device)
{
+ struct wifi_data *wifi = connman_device_get_data(device);
+ struct autoscan_params *autoscan;
+
DBG("");
- if (connman_device_driver_register(&wifi_ng_driver) < 0)
- connman_error("Failed to register WiFi driver");
+ if (wifi == NULL)
+ return;
+
+ autoscan = wifi->autoscan;
+ if (autoscan == NULL)
+ return;
+
+ if (autoscan->timeout > 0 || autoscan->interval > 0)
+ return;
+
+ connman_device_ref(device);
+
+ autoscan_timeout(device);
}
-static void system_killed(void)
+static struct autoscan_params *parse_autoscan_params(const char *params)
{
- DBG("");
+ struct autoscan_params *autoscan;
+ char **list_params;
+ int limit;
+ int base;
- connman_device_driver_unregister(&wifi_ng_driver);
+ DBG("Emulating autoscan");
+
+ list_params = g_strsplit(params, ":", 0);
+ if (list_params == 0)
+ return NULL;
+
+ if (g_strv_length(list_params) < 3) {
+ g_strfreev(list_params);
+ return NULL;
+ }
+
+ base = atoi(list_params[1]);
+ limit = atoi(list_params[2]);
+
+ g_strfreev(list_params);
+
+ autoscan = g_try_malloc0(sizeof(struct autoscan_params));
+ if (autoscan == NULL) {
+ DBG("Could not allocate memory for autoscan");
+ return NULL;
+ }
+
+ DBG("base %d - limit %d", base, limit);
+ autoscan->base = base;
+ autoscan->limit = limit;
+
+ return autoscan;
}
-static void interface_added(GSupplicantInterface *interface)
+static void setup_autoscan(struct wifi_data *wifi)
{
- const char *ifname = g_supplicant_interface_get_ifname(interface);
- const char *driver = g_supplicant_interface_get_driver(interface);
- struct wifi_data *wifi;
+ if (wifi->autoscan == NULL)
+ wifi->autoscan = parse_autoscan_params(AUTOSCAN_DEFAULT);
- wifi = g_supplicant_interface_get_data(interface);
+ start_autoscan(wifi->device);
+}
- /*
- * We can get here with a NULL wifi pointer when
- * the interface added signal is sent before the
- * interface creation callback is called.
- */
- if (wifi == NULL)
+static void interface_autoscan_callback(int result,
+ GSupplicantInterface *interface,
+ void *user_data)
+{
+ struct wifi_data *wifi = user_data;
+
+ if (result < 0) {
+ DBG("Could not enable Autoscan, falling back...");
+ setup_autoscan(wifi);
+ }
+}
+
+static void interface_create_callback(int result,
+ GSupplicantInterface *interface,
+ void *user_data)
+{
+ struct wifi_data *wifi = user_data;
+
+ DBG("result %d ifname %s, wifi %p", result,
+ g_supplicant_interface_get_ifname(interface),
+ wifi);
+
+ if (result < 0 || wifi == NULL)
+ return;
+
+ wifi->interface = interface;
+ g_supplicant_interface_set_data(interface, wifi);
+
+ if (g_supplicant_interface_get_ready(interface) == FALSE)
return;
- DBG("ifname %s driver %s wifi %p", ifname, driver, wifi);
+ DBG("interface is ready wifi %p tethering %d", wifi, wifi->tethering);
if (wifi->device == NULL) {
connman_error("WiFi device not set");
}
connman_device_set_powered(wifi->device, TRUE);
- wifi_scan(wifi->device);
+
+ if (connman_setting_get_bool("BackgroundScanning") == FALSE)
+ return;
+
+ /* Setting up automatic scanning */
+ if (g_supplicant_interface_autoscan(interface, AUTOSCAN_DEFAULT,
+ interface_autoscan_callback, wifi) < 0) {
+ DBG("Could not enable Autoscan, falling back...");
+ setup_autoscan(wifi);
+ }
}
-static connman_bool_t is_idle(struct wifi_data *wifi)
-{
- DBG("state %d", wifi->state);
+static int wifi_enable(struct connman_device *device)
+{
+ struct wifi_data *wifi = connman_device_get_data(device);
+ const char *interface = connman_device_get_string(device, "Interface");
+ const char *driver = connman_option_get_string("wifi");
+ int ret;
+
+ DBG("device %p %p", device, wifi);
+
+ if (wifi == NULL)
+ return -ENODEV;
+
+ ret = g_supplicant_interface_create(interface, driver, NULL,
+ interface_create_callback,
+ wifi);
+ if (ret < 0)
+ return ret;
+
+ return -EINPROGRESS;
+}
+
+static int wifi_disable(struct connman_device *device)
+{
+ struct wifi_data *wifi = connman_device_get_data(device);
+ int ret;
+
+ DBG("device %p wifi %p", device, wifi);
+
+ if (wifi == NULL)
+ return -ENODEV;
+
+ wifi->connected = FALSE;
+ wifi->disconnecting = FALSE;
+
+ if (wifi->pending_network != NULL)
+ wifi->pending_network = NULL;
+
+ stop_autoscan(device);
+
+ /* In case of a user scan, device is still referenced */
+ if (connman_device_get_scanning(device) == TRUE) {
+ connman_device_set_scanning(device, FALSE);
+ connman_device_unref(wifi->device);
+ }
+
+ remove_networks(device, wifi);
+
+ ret = g_supplicant_interface_remove(wifi->interface, NULL, NULL);
+ if (ret < 0)
+ return ret;
+
+ return -EINPROGRESS;
+}
+
+struct last_connected {
+ GTimeVal modified;
+ gchar *ssid;
+ int freq;
+};
+
+static gint sort_entry(gconstpointer a, gconstpointer b, gpointer user_data)
+{
+ GTimeVal *aval = (GTimeVal *)a;
+ GTimeVal *bval = (GTimeVal *)b;
+
+ /* Note that the sort order is descending */
+ if (aval->tv_sec < bval->tv_sec)
+ return 1;
+
+ if (aval->tv_sec > bval->tv_sec)
+ return -1;
+
+ return 0;
+}
+
+static void free_entry(gpointer data)
+{
+ struct last_connected *entry = data;
+
+ g_free(entry->ssid);
+ g_free(entry);
+}
+
+static int get_latest_connections(int max_ssids,
+ GSupplicantScanParams *scan_data)
+{
+ GSequenceIter *iter;
+ GSequence *latest_list;
+ struct last_connected *entry;
+ GKeyFile *keyfile;
+ GTimeVal modified;
+ gchar **services;
+ gchar *str;
+ char *ssid;
+ int i, freq;
+ int num_ssids = 0;
+
+ latest_list = g_sequence_new(free_entry);
+ if (latest_list == NULL)
+ return -ENOMEM;
+
+ services = connman_storage_get_services();
+ for (i = 0; services && services[i]; i++) {
+ if (strncmp(services[i], "wifi_", 5) != 0)
+ continue;
+
+ keyfile = connman_storage_load_service(services[i]);
+ if (keyfile == NULL)
+ continue;
+
+ str = g_key_file_get_string(keyfile,
+ services[i], "Favorite", NULL);
+ if (str == NULL || g_strcmp0(str, "true")) {
+ if (str)
+ g_free(str);
+ g_key_file_free(keyfile);
+ continue;
+ }
+ g_free(str);
+
+ str = g_key_file_get_string(keyfile,
+ services[i], "AutoConnect", NULL);
+ if (str == NULL || g_strcmp0(str, "true")) {
+ if (str)
+ g_free(str);
+ g_key_file_free(keyfile);
+ continue;
+ }
+ g_free(str);
+
+ str = g_key_file_get_string(keyfile,
+ services[i], "Modified", NULL);
+ if (str != NULL) {
+ g_time_val_from_iso8601(str, &modified);
+ g_free(str);
+ }
+
+ ssid = g_key_file_get_string(keyfile,
+ services[i], "SSID", NULL);
+
+ freq = g_key_file_get_integer(keyfile, services[i],
+ "Frequency", NULL);
+ if (freq) {
+ entry = g_try_new(struct last_connected, 1);
+ if (entry == NULL) {
+ g_sequence_free(latest_list);
+ g_key_file_free(keyfile);
+ g_free(ssid);
+ return -ENOMEM;
+ }
+
+ entry->ssid = ssid;
+ entry->modified = modified;
+ entry->freq = freq;
+
+ g_sequence_insert_sorted(latest_list, entry,
+ sort_entry, NULL);
+ num_ssids++;
+ } else
+ g_free(ssid);
+
+ g_key_file_free(keyfile);
+ }
+
+ g_strfreev(services);
+
+ num_ssids = num_ssids > max_ssids ? max_ssids : num_ssids;
+
+ iter = g_sequence_get_begin_iter(latest_list);
+
+ for (i = 0; i < num_ssids; i++) {
+ entry = g_sequence_get(iter);
+
+ DBG("ssid %s freq %d modified %lu", entry->ssid, entry->freq,
+ entry->modified.tv_sec);
+
+ add_scan_param(entry->ssid, NULL, 0, entry->freq, scan_data,
+ max_ssids, entry->ssid);
+
+ iter = g_sequence_iter_next(iter);
+ }
+
+ g_sequence_free(latest_list);
+ return num_ssids;
+}
+
+static int wifi_scan_simple(struct connman_device *device)
+{
+ reset_autoscan(device);
+
+ return throw_wifi_scan(device, scan_callback_hidden);
+}
+
+/*
+ * 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.
+ */
+static int wifi_scan(struct connman_device *device,
+ const char *ssid, unsigned int ssid_len,
+ const char *identity, const char* passphrase,
+ gpointer user_data)
+{
+ struct wifi_data *wifi = connman_device_get_data(device);
+ GSupplicantScanParams *scan_params = NULL;
+ struct scan_ssid *scan_ssid;
+ struct hidden_params *hidden;
+ int ret;
+ int driver_max_ssids = 0;
+ connman_bool_t do_hidden;
+
+ if (wifi == NULL)
+ return -ENODEV;
+
+ DBG("device %p wifi %p hidden ssid %s", device, wifi->interface, ssid);
+
+ if (wifi->tethering == TRUE)
+ return 0;
+
+ if (connman_device_get_scanning(device) == TRUE)
+ return -EALREADY;
+
+ if (ssid == NULL || ssid_len == 0 || ssid_len > 32) {
+ do_hidden = FALSE;
+ } else {
+ if (wifi->hidden != NULL)
+ return -EBUSY;
+
+ do_hidden = TRUE;
+ }
+
+ if (do_hidden == FALSE) {
+ driver_max_ssids = g_supplicant_interface_get_max_scan_ssids(
+ wifi->interface);
+ DBG("max ssids %d", driver_max_ssids);
+ if (driver_max_ssids == 0)
+ return wifi_scan_simple(device);
+ }
+
+ scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
+ if (scan_params == NULL)
+ return -ENOMEM;
+
+ if (do_hidden == TRUE) {
+ scan_ssid = g_try_new(struct scan_ssid, 1);
+ if (scan_ssid == NULL) {
+ g_free(scan_params);
+ return -ENOMEM;
+ }
+
+ memcpy(scan_ssid->ssid, ssid, ssid_len);
+ scan_ssid->ssid_len = ssid_len;
+ scan_params->ssids = g_slist_prepend(scan_params->ssids,
+ scan_ssid);
+ scan_params->num_ssids = 1;
+
+ hidden = g_try_new0(struct hidden_params, 1);
+ if (hidden == NULL) {
+ g_free(scan_params);
+ return -ENOMEM;
+ }
+
+ memcpy(hidden->ssid, ssid, ssid_len);
+ hidden->ssid_len = ssid_len;
+ hidden->identity = g_strdup(identity);
+ hidden->passphrase = g_strdup(passphrase);
+ hidden->user_data = user_data;
+ wifi->hidden = hidden;
+
+ } else {
+ ret = get_latest_connections(driver_max_ssids, scan_params);
+ if (ret <= 0) {
+ g_supplicant_free_scan_params(scan_params);
+ return wifi_scan_simple(device);
+ }
+ }
+
+ connman_device_ref(device);
+
+ reset_autoscan(device);
+
+ ret = g_supplicant_interface_scan(wifi->interface, scan_params,
+ scan_callback, device);
+ if (ret == 0)
+ connman_device_set_scanning(device, TRUE);
+ else {
+ g_supplicant_free_scan_params(scan_params);
+ connman_device_unref(device);
+
+ if (do_hidden == TRUE) {
+ hidden_free(wifi->hidden);
+ wifi->hidden = NULL;
+ }
+ }
+
+ return ret;
+}
+
+static void wifi_regdom_callback(int result,
+ const char *alpha2,
+ void *user_data)
+{
+ struct connman_device *device = user_data;
+
+ connman_device_regdom_notify(device, result, alpha2);
+
+ connman_device_unref(device);
+}
+
+static int wifi_set_regdom(struct connman_device *device, const char *alpha2)
+{
+ struct wifi_data *wifi = connman_device_get_data(device);
+ int ret;
+
+ if (wifi == NULL)
+ return -EINVAL;
+
+ connman_device_ref(device);
+
+ ret = g_supplicant_interface_set_country(wifi->interface,
+ wifi_regdom_callback,
+ alpha2, device);
+ if (ret != 0)
+ connman_device_unref(device);
+
+ return ret;
+}
+
+static struct connman_device_driver wifi_ng_driver = {
+ .name = "wifi",
+ .type = CONNMAN_DEVICE_TYPE_WIFI,
+ .priority = CONNMAN_DEVICE_PRIORITY_LOW,
+ .probe = wifi_probe,
+ .remove = wifi_remove,
+ .enable = wifi_enable,
+ .disable = wifi_disable,
+ .scan = wifi_scan,
+ .set_regdom = wifi_set_regdom,
+};
+
+static void system_ready(void)
+{
+ DBG("");
+
+ if (connman_device_driver_register(&wifi_ng_driver) < 0)
+ connman_error("Failed to register WiFi driver");
+}
+
+static void system_killed(void)
+{
+ DBG("");
+
+ connman_device_driver_unregister(&wifi_ng_driver);
+}
+
+static int network_probe(struct connman_network *network)
+{
+ DBG("network %p", network);
+
+ return 0;
+}
+
+static void network_remove(struct connman_network *network)
+{
+ struct connman_device *device = connman_network_get_device(network);
+ struct wifi_data *wifi;
+
+ DBG("network %p", network);
+
+ wifi = connman_device_get_data(device);
+ if (wifi == NULL)
+ return;
+
+ if (wifi->network != network)
+ return;
+
+ wifi->network = NULL;
+}
+
+static void connect_callback(int result, GSupplicantInterface *interface,
+ void *user_data)
+{
+ struct connman_network *network = user_data;
+
+ DBG("network %p result %d", network, result);
+
+ if (result == -ENOKEY) {
+ connman_network_set_error(network,
+ CONNMAN_NETWORK_ERROR_INVALID_KEY);
+ } else if (result < 0) {
+ connman_network_set_error(network,
+ CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
+ }
+}
+
+static GSupplicantSecurity network_security(const char *security)
+{
+ if (g_str_equal(security, "none") == TRUE)
+ return G_SUPPLICANT_SECURITY_NONE;
+ else if (g_str_equal(security, "wep") == TRUE)
+ return G_SUPPLICANT_SECURITY_WEP;
+ else if (g_str_equal(security, "psk") == TRUE)
+ return G_SUPPLICANT_SECURITY_PSK;
+ else if (g_str_equal(security, "wpa") == TRUE)
+ return G_SUPPLICANT_SECURITY_PSK;
+ else if (g_str_equal(security, "rsn") == TRUE)
+ return G_SUPPLICANT_SECURITY_PSK;
+ else if (g_str_equal(security, "ieee8021x") == TRUE)
+ return G_SUPPLICANT_SECURITY_IEEE8021X;
+
+ return G_SUPPLICANT_SECURITY_UNKNOWN;
+}
+
+static void ssid_init(GSupplicantSSID *ssid, struct connman_network *network)
+{
+ const char *security, *passphrase, *agent_passphrase;
+
+ memset(ssid, 0, sizeof(*ssid));
+ ssid->mode = G_SUPPLICANT_MODE_INFRA;
+ ssid->ssid = connman_network_get_blob(network, "WiFi.SSID",
+ &ssid->ssid_len);
+ ssid->scan_ssid = 1;
+ security = connman_network_get_string(network, "WiFi.Security");
+ ssid->security = network_security(security);
+ passphrase = connman_network_get_string(network,
+ "WiFi.Passphrase");
+ if (passphrase == NULL || strlen(passphrase) == 0) {
+
+ /* Use agent provided passphrase as a fallback */
+ agent_passphrase = connman_network_get_string(network,
+ "WiFi.AgentPassphrase");
+
+ if (agent_passphrase == NULL || strlen(agent_passphrase) == 0)
+ ssid->passphrase = NULL;
+ else
+ ssid->passphrase = agent_passphrase;
+ } else
+ ssid->passphrase = passphrase;
+
+ ssid->eap = connman_network_get_string(network, "WiFi.EAP");
+
+ /*
+ * If our private key password is unset,
+ * we use the supplied passphrase. That is needed
+ * for PEAP where 2 passphrases (identity and client
+ * cert may have to be provided.
+ */
+ if (connman_network_get_string(network,
+ "WiFi.PrivateKeyPassphrase") == NULL)
+ connman_network_set_string(network,
+ "WiFi.PrivateKeyPassphrase",
+ ssid->passphrase);
+ /* We must have an identity for both PEAP and TLS */
+ ssid->identity = connman_network_get_string(network, "WiFi.Identity");
+
+ /* Use agent provided identity as a fallback */
+ if (ssid->identity == NULL || strlen(ssid->identity) == 0)
+ ssid->identity = connman_network_get_string(network,
+ "WiFi.AgentIdentity");
+
+ ssid->ca_cert_path = connman_network_get_string(network,
+ "WiFi.CACertFile");
+ ssid->client_cert_path = connman_network_get_string(network,
+ "WiFi.ClientCertFile");
+ ssid->private_key_path = connman_network_get_string(network,
+ "WiFi.PrivateKeyFile");
+ ssid->private_key_passphrase = connman_network_get_string(network,
+ "WiFi.PrivateKeyPassphrase");
+ ssid->phase2_auth = connman_network_get_string(network, "WiFi.Phase2");
+
+ ssid->use_wps = connman_network_get_bool(network, "WiFi.UseWPS");
+ ssid->pin_wps = connman_network_get_string(network, "WiFi.PinWPS");
+
+ if (connman_setting_get_bool("BackgroundScanning") == TRUE)
+ ssid->bgscan = BGSCAN_DEFAULT;
+}
+
+static int network_connect(struct connman_network *network)
+{
+ struct connman_device *device = connman_network_get_device(network);
+ struct wifi_data *wifi;
+ GSupplicantInterface *interface;
+ GSupplicantSSID *ssid;
+
+ DBG("network %p", network);
+
+ if (device == NULL)
+ return -ENODEV;
+
+ wifi = connman_device_get_data(device);
+ if (wifi == NULL)
+ return -ENODEV;
+
+ ssid = g_try_malloc0(sizeof(GSupplicantSSID));
+ if (ssid == NULL)
+ return -ENOMEM;
+
+ interface = wifi->interface;
+
+ ssid_init(ssid, network);
+
+ if (wifi->disconnecting == TRUE)
+ wifi->pending_network = network;
+ else {
+ wifi->network = network;
+ wifi->retries = 0;
+
+ return g_supplicant_interface_connect(interface, ssid,
+ connect_callback, network);
+ }
+
+ return -EINPROGRESS;
+}
+
+static void disconnect_callback(int result, GSupplicantInterface *interface,
+ void *user_data)
+{
+ struct wifi_data *wifi = user_data;
+
+ DBG("result %d supplicant interface %p wifi %p",
+ result, interface, wifi);
+
+ if (result == -ECONNABORTED) {
+ DBG("wifi interface no longer available");
+ return;
+ }
+
+ if (wifi->network != NULL) {
+ /*
+ * if result < 0 supplican return an error because
+ * the network is not current.
+ * we wont receive G_SUPPLICANT_STATE_DISCONNECTED since it
+ * failed, call connman_network_set_connected to report
+ * disconnect is completed.
+ */
+ if (result < 0)
+ connman_network_set_connected(wifi->network, FALSE);
+ }
+
+ wifi->network = NULL;
+
+ wifi->disconnecting = FALSE;
+
+ if (wifi->pending_network != NULL) {
+ network_connect(wifi->pending_network);
+ wifi->pending_network = NULL;
+ }
+
+ start_autoscan(wifi->device);
+}
+
+static int network_disconnect(struct connman_network *network)
+{
+ struct connman_device *device = connman_network_get_device(network);
+ struct wifi_data *wifi;
+ int err;
+
+ DBG("network %p", network);
+
+ wifi = connman_device_get_data(device);
+ if (wifi == NULL || wifi->interface == NULL)
+ return -ENODEV;
+
+ connman_network_set_associating(network, FALSE);
+
+ if (wifi->disconnecting == TRUE)
+ return -EALREADY;
+
+ wifi->disconnecting = TRUE;
+
+ err = g_supplicant_interface_disconnect(wifi->interface,
+ disconnect_callback, wifi);
+ if (err < 0)
+ wifi->disconnecting = FALSE;
+
+ return err;
+}
+
+static struct connman_network_driver network_driver = {
+ .name = "wifi",
+ .type = CONNMAN_NETWORK_TYPE_WIFI,
+ .priority = CONNMAN_NETWORK_PRIORITY_LOW,
+ .probe = network_probe,
+ .remove = network_remove,
+ .connect = network_connect,
+ .disconnect = network_disconnect,
+};
+
+static void interface_added(GSupplicantInterface *interface)
+{
+ const char *ifname = g_supplicant_interface_get_ifname(interface);
+ const char *driver = g_supplicant_interface_get_driver(interface);
+ struct wifi_data *wifi;
+
+ wifi = g_supplicant_interface_get_data(interface);
+
+ /*
+ * We can get here with a NULL wifi pointer when
+ * the interface added signal is sent before the
+ * interface creation callback is called.
+ */
+ if (wifi == NULL)
+ return;
+
+ DBG("ifname %s driver %s wifi %p tethering %d",
+ ifname, driver, wifi, wifi->tethering);
+
+ if (wifi->device == NULL) {
+ connman_error("WiFi device not set");
+ return;
+ }
+
+ connman_device_set_powered(wifi->device, TRUE);
+
+ if (wifi->tethering == TRUE)
+ return;
+}
+
+static connman_bool_t is_idle(struct wifi_data *wifi)
+{
+ DBG("state %d", wifi->state);
+
+ switch (wifi->state) {
+ case G_SUPPLICANT_STATE_UNKNOWN:
+ case G_SUPPLICANT_STATE_DISABLED:
+ case G_SUPPLICANT_STATE_DISCONNECTED:
+ case G_SUPPLICANT_STATE_INACTIVE:
+ case G_SUPPLICANT_STATE_SCANNING:
+ return TRUE;
+
+ case G_SUPPLICANT_STATE_AUTHENTICATING:
+ case G_SUPPLICANT_STATE_ASSOCIATING:
+ case G_SUPPLICANT_STATE_ASSOCIATED:
+ case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
+ case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
+ case G_SUPPLICANT_STATE_COMPLETED:
+ return FALSE;
+ }
+
+ return FALSE;
+}
+
+static connman_bool_t is_idle_wps(GSupplicantInterface *interface,
+ struct wifi_data *wifi)
+{
+ /* First, let's check if WPS processing did not went wrong */
+ if (g_supplicant_interface_get_wps_state(interface) ==
+ G_SUPPLICANT_WPS_STATE_FAIL)
+ return FALSE;
+
+ /* Unlike normal connection, being associated while processing wps
+ * actually means that we are idling. */
+ switch (wifi->state) {
+ case G_SUPPLICANT_STATE_UNKNOWN:
+ case G_SUPPLICANT_STATE_DISABLED:
+ case G_SUPPLICANT_STATE_DISCONNECTED:
+ case G_SUPPLICANT_STATE_INACTIVE:
+ case G_SUPPLICANT_STATE_SCANNING:
+ case G_SUPPLICANT_STATE_ASSOCIATED:
+ return TRUE;
+ case G_SUPPLICANT_STATE_AUTHENTICATING:
+ case G_SUPPLICANT_STATE_ASSOCIATING:
+ case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
+ case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
+ case G_SUPPLICANT_STATE_COMPLETED:
+ return FALSE;
+ }
+
+ return FALSE;
+}
+
+static connman_bool_t handle_wps_completion(GSupplicantInterface *interface,
+ struct connman_network *network,
+ struct connman_device *device,
+ struct wifi_data *wifi)
+{
+ connman_bool_t wps;
+
+ wps = connman_network_get_bool(network, "WiFi.UseWPS");
+ if (wps == TRUE) {
+ const unsigned char *ssid, *wps_ssid;
+ unsigned int ssid_len, wps_ssid_len;
+ const char *wps_key;
+
+ /* Checking if we got associated with requested
+ * network */
+ ssid = connman_network_get_blob(network, "WiFi.SSID",
+ &ssid_len);
+
+ wps_ssid = g_supplicant_interface_get_wps_ssid(
+ interface, &wps_ssid_len);
+
+ if (wps_ssid == NULL || wps_ssid_len != ssid_len ||
+ memcmp(ssid, wps_ssid, ssid_len) != 0) {
+ connman_network_set_associating(network, FALSE);
+ g_supplicant_interface_disconnect(wifi->interface,
+ disconnect_callback, wifi);
+ return FALSE;
+ }
+
+ wps_key = g_supplicant_interface_get_wps_key(interface);
+ connman_network_set_string(network, "WiFi.Passphrase",
+ wps_key);
+
+ connman_network_set_string(network, "WiFi.PinWPS", NULL);
+ }
+
+ return TRUE;
+}
+
+static connman_bool_t handle_4way_handshake_failure(GSupplicantInterface *interface,
+ struct connman_network *network,
+ struct wifi_data *wifi)
+{
+ struct connman_service *service;
+
+ if (wifi->state != G_SUPPLICANT_STATE_4WAY_HANDSHAKE)
+ return FALSE;
+
+ service = connman_service_lookup_from_network(network);
+ if (service == NULL)
+ return FALSE;
+
+ wifi->retries++;
- switch (wifi->state) {
- case G_SUPPLICANT_STATE_UNKNOWN:
- case G_SUPPLICANT_STATE_DISCONNECTED:
- case G_SUPPLICANT_STATE_INACTIVE:
- case G_SUPPLICANT_STATE_SCANNING:
+ if (connman_service_get_favorite(service) == TRUE) {
+ if (wifi->retries < FAVORITE_MAXIMUM_RETRIES)
+ return TRUE;
+ } else if (wifi->retries < MAXIMUM_RETRIES)
return TRUE;
- case G_SUPPLICANT_STATE_AUTHENTICATING:
- case G_SUPPLICANT_STATE_ASSOCIATING:
- case G_SUPPLICANT_STATE_ASSOCIATED:
- case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
- case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
- case G_SUPPLICANT_STATE_COMPLETED:
- return FALSE;
- }
+ connman_network_set_error(network, CONNMAN_NETWORK_ERROR_INVALID_KEY);
return FALSE;
}
struct connman_device *device;
struct wifi_data *wifi;
GSupplicantState state = g_supplicant_interface_get_state(interface);
- unsigned char bssid[ETH_ALEN];
- unsigned int bssid_len;
+ connman_bool_t wps;
wifi = g_supplicant_interface_get_data(interface);
switch (state) {
case G_SUPPLICANT_STATE_SCANNING:
- connman_device_set_scanning(device, TRUE);
break;
case G_SUPPLICANT_STATE_AUTHENTICATING:
case G_SUPPLICANT_STATE_ASSOCIATING:
- connman_network_set_associating(network, TRUE);
+ stop_autoscan(device);
+
+ if (wifi->connected == FALSE)
+ connman_network_set_associating(network, TRUE);
+
break;
case G_SUPPLICANT_STATE_COMPLETED:
- /* reset scan trigger and schedule background scan */
- connman_device_schedule_scan(device);
+ /* though it should be already stopped: */
+ stop_autoscan(device);
+
+ if (handle_wps_completion(interface, network, device, wifi) ==
+ FALSE)
+ break;
- if (get_bssid(device, bssid, &bssid_len) == 0)
- connman_network_set_address(network,
- bssid, bssid_len);
connman_network_set_connected(network, TRUE);
break;
* those ones to FALSE could cancel an association
* in progress.
*/
+ wps = connman_network_get_bool(network, "WiFi.UseWPS");
+ if (wps == TRUE)
+ if (is_idle_wps(interface, wifi) == TRUE)
+ break;
+
if (is_idle(wifi))
break;
- connman_network_set_associating(network, FALSE);
+
+ /* If previous state was 4way-handshake, then
+ * it's either: psk was incorrect and thus we retry
+ * or if we reach the maximum retries we declare the
+ * psk as wrong */
+ if (handle_4way_handshake_failure(interface,
+ network, wifi) == TRUE)
+ break;
+
+ /* We disable the selected network, if not then
+ * wpa_supplicant will loop retrying */
+ if (g_supplicant_interface_enable_selected_network(interface,
+ FALSE) != 0)
+ DBG("Could not disables selected network");
+
connman_network_set_connected(network, FALSE);
+ connman_network_set_associating(network, FALSE);
+ wifi->disconnecting = FALSE;
+
+ start_autoscan(device);
+
break;
case G_SUPPLICANT_STATE_INACTIVE:
connman_network_set_associating(network, FALSE);
+ start_autoscan(device);
+
break;
case G_SUPPLICANT_STATE_UNKNOWN:
+ case G_SUPPLICANT_STATE_DISABLED:
case G_SUPPLICANT_STATE_ASSOCIATED:
case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
wifi->state = state;
+ /* Saving wpa_s state policy:
+ * If connected and if the state changes are roaming related:
+ * --> We stay connected
+ * If completed
+ * --> We are connected
+ * All other case:
+ * --> We are not connected
+ * */
+ switch (state) {
+ case G_SUPPLICANT_STATE_AUTHENTICATING:
+ case G_SUPPLICANT_STATE_ASSOCIATING:
+ case G_SUPPLICANT_STATE_ASSOCIATED:
+ case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
+ case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
+ if (wifi->connected == TRUE)
+ connman_warn("Probably roaming right now!"
+ " Staying connected...");
+ else
+ wifi->connected = FALSE;
+ break;
+ case G_SUPPLICANT_STATE_COMPLETED:
+ wifi->connected = TRUE;
+ break;
+ default:
+ wifi->connected = FALSE;
+ break;
+ }
+
DBG("DONE");
}
wifi = g_supplicant_interface_get_data(interface);
+ if (wifi != NULL && wifi->tethering == TRUE)
+ return;
+
if (wifi == NULL || wifi->device == NULL) {
- connman_error("Wrong wifi pointer");
+ DBG("wifi interface already removed");
return;
}
+ wifi->interface = NULL;
connman_device_set_powered(wifi->device, FALSE);
}
static void scan_started(GSupplicantInterface *interface)
{
- struct wifi_data *wifi;
-
DBG("");
-
- wifi = g_supplicant_interface_get_data(interface);
-
- if (wifi == NULL)
- return;
-
- if (wifi->device)
- connman_device_set_scanning(wifi->device, TRUE);
}
static void scan_finished(GSupplicantInterface *interface)
{
- struct wifi_data *wifi;
-
DBG("");
-
- wifi = g_supplicant_interface_get_data(interface);
-
- if (wifi == NULL)
- return;
}
static unsigned char calculate_strength(GSupplicantNetwork *supplicant_network)
struct connman_network *network;
GSupplicantInterface *interface;
struct wifi_data *wifi;
- const char *name, *identifier, *mode, *security, *group;
+ const char *name, *identifier, *security, *group, *mode;
const unsigned char *ssid;
unsigned int ssid_len;
+ connman_bool_t wps;
+ connman_bool_t wps_pbc;
+ connman_bool_t wps_ready;
+ connman_bool_t wps_advertizing;
DBG("");
wifi = g_supplicant_interface_get_data(interface);
name = g_supplicant_network_get_name(supplicant_network);
identifier = g_supplicant_network_get_identifier(supplicant_network);
- mode = g_supplicant_network_get_mode(supplicant_network);
security = g_supplicant_network_get_security(supplicant_network);
group = g_supplicant_network_get_identifier(supplicant_network);
+ wps = g_supplicant_network_get_wps(supplicant_network);
+ wps_pbc = g_supplicant_network_is_wps_pbc(supplicant_network);
+ wps_ready = g_supplicant_network_is_wps_active(supplicant_network);
+ wps_advertizing = g_supplicant_network_is_wps_advertizing(
+ supplicant_network);
+ mode = g_supplicant_network_get_mode(supplicant_network);
if (wifi == NULL)
return;
connman_network_unref(network);
return;
}
+
+ wifi->networks = g_slist_prepend(wifi->networks, network);
}
if (name != NULL && name[0] != '\0')
connman_network_set_blob(network, "WiFi.SSID",
ssid, ssid_len);
- connman_network_set_string(network, "WiFi.Mode", mode);
connman_network_set_string(network, "WiFi.Security", security);
connman_network_set_strength(network,
calculate_strength(supplicant_network));
+ connman_network_set_bool(network, "WiFi.WPS", wps);
+
+ if (wps == TRUE) {
+ /* Is AP advertizing for WPS association?
+ * If so, we decide to use WPS by default */
+ if (wps_ready == TRUE && wps_pbc == TRUE &&
+ wps_advertizing == TRUE)
+ connman_network_set_bool(network, "WiFi.UseWPS", TRUE);
+ }
+
+ connman_network_set_frequency(network,
+ g_supplicant_network_get_frequency(supplicant_network));
connman_network_set_available(network, TRUE);
+ connman_network_set_string(network, "WiFi.Mode", mode);
if (ssid != NULL)
connman_network_set_group(network, group);
+
+ if (wifi->hidden != NULL && ssid != NULL) {
+ if (wifi->hidden->ssid_len == ssid_len &&
+ memcmp(wifi->hidden->ssid, ssid,
+ ssid_len) == 0) {
+ connman_network_connect_hidden(network,
+ wifi->hidden->identity,
+ wifi->hidden->passphrase,
+ wifi->hidden->user_data);
+ wifi->hidden->user_data = NULL;
+ hidden_free(wifi->hidden);
+ wifi->hidden = NULL;
+ }
+ }
}
static void network_removed(GSupplicantNetwork *network)
GSupplicantInterface *interface;
struct wifi_data *wifi;
const char *name, *identifier;
+ struct connman_network *connman_network;
+
+ interface = g_supplicant_network_get_interface(network);
+ wifi = g_supplicant_interface_get_data(interface);
+ identifier = g_supplicant_network_get_identifier(network);
+ name = g_supplicant_network_get_name(network);
+
+ DBG("name %s", name);
+
+ if (wifi == NULL)
+ return;
+
+ connman_network = connman_device_get_network(wifi->device, identifier);
+ if (connman_network == NULL)
+ return;
+
+ wifi->networks = g_slist_remove(wifi->networks, connman_network);
+
+ connman_device_remove_network(wifi->device, connman_network);
+ connman_network_unref(connman_network);
+}
+
+static void network_changed(GSupplicantNetwork *network, const char *property)
+{
+ GSupplicantInterface *interface;
+ struct wifi_data *wifi;
+ const char *name, *identifier;
+ struct connman_network *connman_network;
interface = g_supplicant_network_get_interface(network);
wifi = g_supplicant_interface_get_data(interface);
DBG("name %s", name);
- if (wifi != NULL)
- connman_device_remove_network(wifi->device, identifier);
+ if (wifi == NULL)
+ return;
+
+ connman_network = connman_device_get_network(wifi->device, identifier);
+ if (connman_network == NULL)
+ return;
+
+ if (g_str_equal(property, "Signal") == TRUE) {
+ connman_network_set_strength(connman_network,
+ calculate_strength(network));
+ connman_network_update(connman_network);
+ }
}
static void debug(const char *str)
.scan_finished = scan_finished,
.network_added = network_added,
.network_removed = network_removed,
+ .network_changed = network_changed,
.debug = debug,
};
-static int network_probe(struct connman_network *network)
+static int tech_probe(struct connman_technology *technology)
{
- DBG("network %p", network);
+ wifi_technology = technology;
return 0;
}
-static void network_remove(struct connman_network *network)
+static void tech_remove(struct connman_technology *technology)
{
- DBG("network %p", network);
+ wifi_technology = NULL;
}
-static void connect_callback(int result, GSupplicantInterface *interface,
- void *user_data)
-{
- connman_error("%s", __func__);
-}
+struct wifi_tethering_info {
+ struct wifi_data *wifi;
+ struct connman_technology *technology;
+ char *ifname;
+ GSupplicantSSID *ssid;
+};
-static GSupplicantSecurity network_security(const char *security)
+static GSupplicantSSID *ssid_ap_init(const char *ssid, const char *passphrase)
{
- if (g_str_equal(security, "none") == TRUE)
- return G_SUPPLICANT_SECURITY_NONE;
- else if (g_str_equal(security, "wep") == TRUE)
- return G_SUPPLICANT_SECURITY_WEP;
- else if (g_str_equal(security, "psk") == TRUE)
- return G_SUPPLICANT_SECURITY_PSK;
- else if (g_str_equal(security, "wpa") == TRUE)
- return G_SUPPLICANT_SECURITY_PSK;
- else if (g_str_equal(security, "rsn") == TRUE)
- return G_SUPPLICANT_SECURITY_PSK;
- else if (g_str_equal(security, "ieee8021x") == TRUE)
- return G_SUPPLICANT_SECURITY_IEEE8021X;
+ GSupplicantSSID *ap;
+
+ ap = g_try_malloc0(sizeof(GSupplicantSSID));
+ if (ap == NULL)
+ return NULL;
+
+ ap->mode = G_SUPPLICANT_MODE_MASTER;
+ ap->ssid = ssid;
+ ap->ssid_len = strlen(ssid);
+ ap->scan_ssid = 0;
+ ap->freq = 2412;
+
+ if (passphrase == NULL || strlen(passphrase) == 0) {
+ ap->security = G_SUPPLICANT_SECURITY_NONE;
+ ap->passphrase = NULL;
+ } else {
+ ap->security = G_SUPPLICANT_SECURITY_PSK;
+ ap->protocol = G_SUPPLICANT_PROTO_RSN;
+ ap->pairwise_cipher = G_SUPPLICANT_PAIRWISE_CCMP;
+ ap->group_cipher = G_SUPPLICANT_GROUP_CCMP;
+ ap->passphrase = passphrase;
+ }
- return G_SUPPLICANT_SECURITY_UNKNOWN;
+ return ap;
}
-static void ssid_init(GSupplicantSSID *ssid, struct connman_network *network)
+static void ap_start_callback(int result, GSupplicantInterface *interface,
+ void *user_data)
{
- const char *security, *passphrase;
-
- memset(ssid, 0, sizeof(*ssid));
- ssid->ssid = connman_network_get_blob(network, "WiFi.SSID",
- &ssid->ssid_len);
- security = connman_network_get_string(network, "WiFi.Security");
- ssid->security = network_security(security);
- passphrase = connman_network_get_string(network,
- "WiFi.Passphrase");
- if (passphrase == NULL || strlen(passphrase) == 0)
- ssid->passphrase = NULL;
- else
- ssid->passphrase = passphrase;
+ struct wifi_tethering_info *info = user_data;
- ssid->eap = connman_network_get_string(network, "WiFi.EAP");
+ DBG("result %d index %d bridge %s",
+ result, info->wifi->index, info->wifi->bridge);
- /*
- * If our private key password is unset,
- * we use the supplied passphrase. That is needed
- * for PEAP where 2 passphrases (identity and client
- * cert may have to be provided.
- */
- if (connman_network_get_string(network,
- "WiFi.PrivateKeyPassphrase") == NULL)
- connman_network_set_string(network,
- "WiFi.PrivateKeyPassphrase",
- ssid->passphrase);
- /* We must have an identity for both PEAP and TLS */
- ssid->identity = connman_network_get_string(network, "WiFi.Identity");
- ssid->ca_cert_path = connman_network_get_string(network,
- "WiFi.CACertFile");
- ssid->client_cert_path = connman_network_get_string(network,
- "WiFi.ClientCertFile");
- ssid->private_key_path = connman_network_get_string(network,
- "WiFi.PrivateKeyFile");
- ssid->private_key_passphrase = connman_network_get_string(network,
- "WiFi.PrivateKeyPassphrase");
- ssid->phase2_auth = connman_network_get_string(network, "WiFi.Phase2");
+ if (result < 0) {
+ connman_inet_remove_from_bridge(info->wifi->index,
+ info->wifi->bridge);
+ connman_technology_tethering_notify(info->technology, FALSE);
+ }
+ g_free(info->ifname);
+ g_free(info);
}
-static int network_connect(struct connman_network *network)
+static void ap_create_callback(int result,
+ GSupplicantInterface *interface,
+ void *user_data)
{
- struct connman_device *device = connman_network_get_device(network);
- struct wifi_data *wifi;
- GSupplicantInterface *interface;
- GSupplicantSSID ssid;
-
- DBG("network %p", network);
-
- if (device == NULL)
- return -ENODEV;
+ struct wifi_tethering_info *info = user_data;
- wifi = connman_device_get_data(device);
- if (wifi == NULL)
- return -ENODEV;
+ DBG("result %d ifname %s", result,
+ g_supplicant_interface_get_ifname(interface));
- interface = wifi->interface;
+ if (result < 0) {
+ connman_inet_remove_from_bridge(info->wifi->index,
+ info->wifi->bridge);
+ connman_technology_tethering_notify(info->technology, FALSE);
- ssid_init(&ssid, network);
+ g_free(info->ifname);
+ g_free(info);
+ return;
+ }
- if (wifi->disconnecting == TRUE)
- wifi->pending_network = connman_network_ref(network);
- else {
- wifi->network = connman_network_ref(network);
+ info->wifi->interface = interface;
+ g_supplicant_interface_set_data(interface, info->wifi);
- return g_supplicant_interface_connect(interface, &ssid,
- connect_callback, NULL);
- }
+ if (g_supplicant_interface_set_apscan(interface, 2) < 0)
+ connman_error("Failed to set interface ap_scan property");
- return -EINPROGRESS;
+ g_supplicant_interface_connect(interface, info->ssid,
+ ap_start_callback, info);
}
-static void disconnect_callback(int result, GSupplicantInterface *interface,
- void *user_data)
+static void sta_remove_callback(int result,
+ GSupplicantInterface *interface,
+ void *user_data)
{
- struct wifi_data *wifi = user_data;
+ struct wifi_tethering_info *info = user_data;
+ const char *driver = connman_option_get_string("wifi");
- if (wifi->network != NULL) {
- /*
- * if result < 0 supplican return an error because
- * the network is not current.
- * we wont receive G_SUPPLICANT_STATE_DISCONNECTED since it
- * failed, call connman_network_set_connected to report
- * disconnect is completed.
- */
- if (result < 0)
- connman_network_set_connected(wifi->network, FALSE);
+ DBG("ifname %s result %d ", info->ifname, result);
- connman_network_unref(wifi->network);
- }
+ if (result < 0) {
+ info->wifi->tethering = TRUE;
- wifi->network = NULL;
+ g_free(info->ifname);
+ g_free(info);
+ return;
+ }
- wifi->disconnecting = FALSE;
+ info->wifi->interface = NULL;
- if (wifi->pending_network != NULL) {
- network_connect(wifi->pending_network);
- connman_network_unref(wifi->pending_network);
- wifi->pending_network = NULL;
- }
+ connman_technology_tethering_notify(info->technology, TRUE);
+ g_supplicant_interface_create(info->ifname, driver, info->wifi->bridge,
+ ap_create_callback,
+ info);
}
-static int network_disconnect(struct connman_network *network)
+static int tech_set_tethering(struct connman_technology *technology,
+ const char *identifier, const char *passphrase,
+ const char *bridge, connman_bool_t enabled)
{
- struct connman_device *device = connman_network_get_device(network);
+ GList *list;
+ GSupplicantInterface *interface;
struct wifi_data *wifi;
+ struct wifi_tethering_info *info;
+ const char *ifname;
+ unsigned int mode;
int err;
- DBG("network %p", network);
+ DBG("");
- wifi = connman_device_get_data(device);
- if (wifi == NULL || wifi->interface == NULL)
- return -ENODEV;
+ if (enabled == FALSE) {
+ for (list = iface_list; list; list = list->next) {
+ wifi = list->data;
- connman_network_set_associating(network, FALSE);
+ if (wifi->tethering == TRUE) {
+ wifi->tethering = FALSE;
- if (wifi->disconnecting == TRUE)
- return -EALREADY;
+ connman_inet_remove_from_bridge(wifi->index,
+ bridge);
+ wifi->bridged = FALSE;
+ }
+ }
- wifi->disconnecting = TRUE;
+ connman_technology_tethering_notify(technology, FALSE);
- err = g_supplicant_interface_disconnect(wifi->interface,
- disconnect_callback, wifi);
- if (err < 0)
- wifi->disconnecting = FALSE;
+ return 0;
+ }
- return err;
-}
+ for (list = iface_list; list; list = list->next) {
+ wifi = list->data;
-static struct connman_network_driver network_driver = {
- .name = "wifi",
- .type = CONNMAN_NETWORK_TYPE_WIFI,
- .priority = CONNMAN_NETWORK_PRIORITY_LOW,
- .probe = network_probe,
- .remove = network_remove,
- .connect = network_connect,
- .disconnect = network_disconnect,
-};
+ interface = wifi->interface;
-static int tech_probe(struct connman_technology *technology)
-{
- wifi_technology = technology;
+ if (interface == NULL)
+ continue;
- return 0;
-}
+ ifname = g_supplicant_interface_get_ifname(wifi->interface);
-static void tech_remove(struct connman_technology *technology)
-{
- wifi_technology = NULL;
+ mode = g_supplicant_interface_get_mode(interface);
+ if ((mode & G_SUPPLICANT_CAPABILITY_MODE_AP) == 0) {
+ DBG("%s does not support AP mode", ifname);
+ continue;
+ }
+
+ info = g_try_malloc0(sizeof(struct wifi_tethering_info));
+ if (info == NULL)
+ return -ENOMEM;
+
+ info->wifi = wifi;
+ info->technology = technology;
+ info->wifi->bridge = bridge;
+ info->ssid = ssid_ap_init(identifier, passphrase);
+ if (info->ssid == NULL) {
+ g_free(info);
+ continue;
+ }
+ info->ifname = g_strdup(ifname);
+ if (info->ifname == NULL) {
+ g_free(info);
+ continue;
+ }
+
+ info->wifi->tethering = TRUE;
+
+ err = g_supplicant_interface_remove(interface,
+ sta_remove_callback,
+ info);
+ if (err == 0)
+ return err;
+ }
+
+ return -EOPNOTSUPP;
}
-static void regdom_callback(void *user_data)
+static void regdom_callback(int result, const char *alpha2, void *user_data)
{
- char *alpha2 = user_data;
-
DBG("");
if (wifi_technology == NULL)
return;
+ if (result != 0)
+ alpha2 = NULL;
+
connman_technology_regdom_notify(wifi_technology, alpha2);
}
static int tech_set_regdom(struct connman_technology *technology, const char *alpha2)
{
- return g_supplicant_set_country(alpha2, regdom_callback, alpha2);
+ return g_supplicant_set_country(alpha2, regdom_callback, NULL);
}
static struct connman_technology_driver tech_driver = {
.type = CONNMAN_SERVICE_TYPE_WIFI,
.probe = tech_probe,
.remove = tech_remove,
+ .set_tethering = tech_set_tethering,
.set_regdom = tech_set_regdom,
};