*
* Connection Manager
*
- * Copyright (C) 2007-2008 Intel Corporation. All rights reserved.
+ * Copyright (C) 2007-2010 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 <config.h>
#endif
+#include <unistd.h>
+#include <errno.h>
#include <string.h>
+#include <sys/ioctl.h>
+#include <sys/socket.h>
+#include <linux/if_arp.h>
+#include <linux/wireless.h>
+#include <net/ethernet.h>
+
+#ifndef IFF_LOWER_UP
+#define IFF_LOWER_UP 0x10000
+#endif
+
#include <dbus/dbus.h>
+#include <glib.h>
+#define CONNMAN_API_SUBJECT_TO_CHANGE
#include <connman/plugin.h>
-#include <connman/driver.h>
+#include <connman/inet.h>
+#include <connman/device.h>
+#include <connman/rtnl.h>
#include <connman/log.h>
+#include <connman/option.h>
+
+#include <gsupplicant/gsupplicant.h>
-#include "supplicant.h"
+#define CLEANUP_TIMEOUT 8 /* in seconds */
+#define INACTIVE_TIMEOUT 12 /* in seconds */
-static struct connman_element *dhcp_element = NULL;
+struct wifi_data {
+ char *identifier;
+ struct connman_device *device;
+ struct connman_network *network;
+ GSupplicantInterface *interface;
+ connman_bool_t connected;
+ int index;
+ unsigned flags;
+ unsigned int watch;
+};
-static int network_probe(struct connman_element *element)
+static int get_bssid(struct connman_device *device,
+ unsigned char *bssid, unsigned int *bssid_len)
{
- DBG("element %p name %s", element, element->name);
+ struct iwreq wrq;
+ char *ifname;
+ int ifindex;
+ int fd, err;
+
+ ifindex = connman_device_get_index(device);
+ if (ifindex < 0)
+ return -EINVAL;
+
+ 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;
+ }
+
+ memset(&wrq, 0, sizeof(wrq));
+ strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
+
+ err = ioctl(fd, SIOCGIWAP, &wrq);
+
+ g_free(ifname);
+ close(fd);
+
+ if (err < 0)
+ return -EIO;
+
+ memcpy(bssid, wrq.u.ap_addr.sa_data, ETH_ALEN);
+ *bssid_len = ETH_ALEN;
return 0;
}
-static void network_remove(struct connman_element *element)
+static void wifi_newlink(unsigned flags, unsigned change, void *user_data)
{
- DBG("element %p name %s", element, element->name);
+ 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)
+ return;
+
+ if ((wifi->flags & IFF_UP) != (flags & IFF_UP)) {
+ if (flags & IFF_UP)
+ DBG("interface up");
+ else
+ DBG("interface down");
+ }
+
+ if ((wifi->flags & IFF_LOWER_UP) != (flags & IFF_LOWER_UP)) {
+ if (flags & IFF_LOWER_UP)
+ DBG("carrier on");
+ else
+ DBG("carrier off");
+ }
+
+ wifi->flags = flags;
}
-static int network_enable(struct connman_element *element)
+static int wifi_probe(struct connman_device *device)
{
- DBG("element %p name %s", element, element->name);
+ struct wifi_data *wifi;
- if (dhcp_element != NULL) {
- connman_element_unregister(dhcp_element);
- dhcp_element = NULL;
- }
+ DBG("device %p", device);
- __supplicant_disconnect(element);
+ wifi = g_try_new0(struct wifi_data, 1);
+ if (wifi == NULL)
+ return -ENOMEM;
- element->enabled = FALSE;
+ wifi->connected = FALSE;
- connman_element_update(element);
+ connman_device_set_data(device, wifi);
+ wifi->device = connman_device_ref(device);
- g_free(element->parent->network.identifier);
- element->parent->network.identifier = element->network.identifier;
+ wifi->index = connman_device_get_index(device);
+ wifi->flags = 0;
- if (__supplicant_connect(element, element->network.identifier) < 0)
- connman_error("Failed to initiate connect");
+ wifi->watch = connman_rtnl_add_newlink_watch(wifi->index,
+ wifi_newlink, device);
return 0;
}
-static int network_disable(struct connman_element *element)
+static void wifi_remove(struct connman_device *device)
{
- DBG("element %p name %s", element, element->name);
+ struct wifi_data *wifi = connman_device_get_data(device);
+
+ DBG("device %p", device);
+
+ connman_device_set_data(device, NULL);
+ connman_device_unref(wifi->device);
+ connman_rtnl_remove_watch(wifi->watch);
+
+ g_free(wifi->identifier);
+ g_free(wifi);
+}
- if (dhcp_element != NULL) {
- connman_element_unregister(dhcp_element);
- dhcp_element = NULL;
+static void interface_create_callback(int result,
+ GSupplicantInterface *interface,
+ void *user_data)
+{
+ struct wifi_data *wifi = user_data;
+
+ DBG("result %d ifname %s", result,
+ g_supplicant_interface_get_ifname(interface));
+
+ if (result < 0)
+ return;
+
+ wifi->interface = interface;
+ g_supplicant_interface_set_data(interface, wifi);
+}
+
+static void interface_remove_callback(int result,
+ GSupplicantInterface *interface,
+ void *user_data)
+{
+ struct wifi_data *wifi = user_data;
+
+ DBG("result %d", result);
+
+ if (result < 0)
+ return;
+
+ wifi->interface = NULL;
+}
+
+
+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");
+
+ DBG("device %p %p", device, wifi);
+
+ return g_supplicant_interface_create(interface, driver,
+ interface_create_callback,
+ wifi);
+}
+
+static int wifi_disable(struct connman_device *device)
+{
+ struct wifi_data *wifi = connman_device_get_data(device);
+
+ DBG("device %p", device);
+
+ wifi->connected = FALSE;
+
+ return g_supplicant_interface_remove(wifi->interface,
+ interface_remove_callback,
+ wifi);
+}
+
+static void scan_callback(int result, GSupplicantInterface *interface,
+ void *user_data)
+{
+ struct connman_device *device = user_data;
+
+ DBG("result %d", result);
+
+ if (result < 0) {
+ connman_device_set_scanning(device, FALSE);
+ return;
}
- __supplicant_disconnect(element);
+ connman_device_set_scanning(device, TRUE);
+}
- element->enabled = FALSE;
+static int wifi_scan(struct connman_device *device)
+{
+ struct wifi_data *wifi = connman_device_get_data(device);
- connman_element_update(element);
+ DBG("device %p %p", device, wifi->interface);
- return 0;
+ return g_supplicant_interface_scan(wifi->interface, scan_callback,
+ device);
}
-static struct connman_driver network_driver = {
- .name = "wifi-network",
- .type = CONNMAN_ELEMENT_TYPE_NETWORK,
- .subtype = CONNMAN_ELEMENT_SUBTYPE_WIFI,
- .probe = network_probe,
- .remove = network_remove,
- .enable = network_enable,
- .disable = network_disable,
+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,
};
-struct wifi_data {
- GStaticMutex mutex;
- GSList *list;
-};
+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 struct connman_element *find_element(struct wifi_data *data,
- const char *identifier)
+static void interface_added(GSupplicantInterface *interface)
{
- GSList *list;
+ const char *ifname = g_supplicant_interface_get_ifname(interface);
+ const char *driver = g_supplicant_interface_get_driver(interface);
+ struct wifi_data *wifi;
- for (list = data->list; list; list = list->next) {
- struct connman_element *element = list->data;
+ wifi = g_supplicant_interface_get_data(interface);
- if (element->network.identifier == NULL)
- continue;
+ DBG("ifname %s driver %s wifi %p", ifname, driver, wifi);
- if (g_str_equal(element->network.identifier,
- identifier) == TRUE)
- return element;
+ if (wifi == NULL || wifi->device == NULL) {
+ connman_error("Wrong wifi pointer");
+ return;
}
- return NULL;
+ connman_device_set_powered(wifi->device, TRUE);
+ wifi_scan(wifi->device);
}
-static void state_change(struct connman_element *parent,
- enum supplicant_state state)
+static void interface_state(GSupplicantInterface *interface)
{
- struct wifi_data *data = connman_element_get_data(parent);
- struct connman_element *element;
+ struct connman_network *network;
+ 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;
- DBG("state %d", state);
+ wifi = g_supplicant_interface_get_data(interface);
- if (parent->network.identifier == NULL)
- return;
+ DBG("wifi %p interface state %d", wifi, state);
- element = find_element(data, parent->network.identifier);
- if (element == NULL)
+ if (wifi == NULL)
return;
- if (state == STATE_COMPLETED) {
- struct connman_element *dhcp;
+ network = wifi->network;
+ device = wifi->device;
- dhcp = connman_element_create(NULL);
+ if (device == NULL || network == NULL)
+ return;
- dhcp->type = CONNMAN_ELEMENT_TYPE_DHCP;
- dhcp->index = element->index;
+ 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);
+ break;
+
+ case G_SUPPLICANT_STATE_COMPLETED:
+ /* reset scan trigger and schedule background scan */
+ connman_device_schedule_scan(device);
+
+ if (get_bssid(device, bssid, &bssid_len) == 0)
+ connman_network_set_address(network,
+ bssid, bssid_len);
+ connman_network_set_connected(network, TRUE);
+ break;
+
+ case G_SUPPLICANT_STATE_DISCONNECTED:
+ connman_network_set_associating(network, FALSE);
+ connman_network_set_connected(network, FALSE);
+ break;
+
+ case G_SUPPLICANT_STATE_INACTIVE:
+ connman_network_set_associating(network, FALSE);
+ break;
+
+ case G_SUPPLICANT_STATE_UNKNOWN:
+ case G_SUPPLICANT_STATE_ASSOCIATED:
+ case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
+ case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
+ break;
+ }
- dhcp_element = dhcp;
+ DBG("DONE");
+}
+
+static void interface_removed(GSupplicantInterface *interface)
+{
+ const char *ifname = g_supplicant_interface_get_ifname(interface);
+ struct wifi_data *wifi;
- element->enabled = TRUE;
+ DBG("ifname %s", ifname);
- connman_element_update(element);
+ wifi = g_supplicant_interface_get_data(interface);
- connman_element_register(dhcp, element);
+ if (wifi == NULL || wifi->device == NULL) {
+ connman_error("Wrong wifi pointer");
+ return;
}
+
+ connman_device_set_powered(wifi->device, FALSE);
}
-static void scan_result(struct connman_element *parent,
- struct supplicant_network *network)
+static void scan_started(GSupplicantInterface *interface)
{
- struct wifi_data *data = connman_element_get_data(parent);
- struct connman_element *element;
- gchar *temp;
- int i;
+ struct wifi_data *wifi;
- DBG("network %p identifier %s", network, network->identifier);
+ DBG("");
- if (data == NULL)
- return;
+ wifi = g_supplicant_interface_get_data(interface);
- if (network->identifier == NULL)
+ if (wifi == NULL)
return;
- if (network->identifier[0] == '\0')
+ 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;
+}
- temp = g_strdup(network->identifier);
+static unsigned char calculate_strength(GSupplicantNetwork *supplicant_network)
+{
+ unsigned char strength;
- for (i = 0; i < strlen(temp); i++) {
- if (temp[i] == ' ' || temp[i] == '.' || temp[i] == '-')
- temp[i] = '_';
- if (temp[i] == '(' || temp[i] == ')')
- temp[i] = '_';
- if (g_ascii_isprint(temp[i]) == FALSE)
- temp[i] = '_';
- temp[i] = g_ascii_tolower(temp[i]);
- }
+ strength = 120 + g_supplicant_network_get_signal(supplicant_network);
+ if (strength > 100)
+ strength = 100;
- g_static_mutex_lock(&data->mutex);
+ return strength;
+}
- element = find_element(data, network->identifier);
- if (element == NULL) {
- element = connman_element_create(temp);
+static void network_added(GSupplicantNetwork *supplicant_network)
+{
+ struct connman_network *network;
+ GSupplicantInterface *interface;
+ struct wifi_data *wifi;
+ const char *name, *path, *identifier, *mode, *security, *group;
+ const unsigned char *ssid;
+ unsigned int ssid_len;
+
+ DBG("");
+
+ interface = g_supplicant_network_get_interface(supplicant_network);
+ wifi = g_supplicant_interface_get_data(interface);
+ name = g_supplicant_network_get_name(supplicant_network);
+ path = g_supplicant_network_get_path(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);
+
+ if (wifi == NULL)
+ return;
+
+ ssid = g_supplicant_network_get_ssid(supplicant_network, &ssid_len);
- element->type = CONNMAN_ELEMENT_TYPE_NETWORK;
- element->index = parent->index;
+ network = connman_device_get_network(wifi->device, path);
- element->network.identifier = g_strdup(network->identifier);
+ if (network == NULL) {
+ network = connman_network_create(identifier,
+ CONNMAN_NETWORK_TYPE_WIFI);
+ if (network == NULL)
+ return;
- data->list = g_slist_append(data->list, element);
+ connman_network_set_index(network, wifi->index);
- connman_element_add_static_property(element, "SSID",
- DBUS_TYPE_STRING, &network->identifier);
+ connman_network_set_protocol(network,
+ CONNMAN_NETWORK_PROTOCOL_IP);
- connman_element_register(element, parent);
+ if (connman_device_add_network(wifi->device, network) < 0) {
+ connman_network_unref(network);
+ return;
+ }
}
- g_static_mutex_unlock(&data->mutex);
+ if (name != NULL && name[0] != '\0')
+ connman_network_set_name(network, name);
+
+ 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_available(network, TRUE);
+
+ if (ssid != NULL)
+ connman_network_set_group(network, group);
+}
+
+static void network_removed(GSupplicantNetwork *network)
+{
+ const char *name = g_supplicant_network_get_name(network);
- g_free(temp);
+ DBG("* name %s", name);
}
-static struct supplicant_callback wifi_callback = {
- .state_change = state_change,
- .scan_result = scan_result,
+static void debug(const char *str)
+{
+ connman_debug("%s", str);
+}
+
+static const GSupplicantCallbacks callbacks = {
+ .system_ready = system_ready,
+ .system_killed = system_killed,
+ .interface_added = interface_added,
+ .interface_state = interface_state,
+ .interface_removed = interface_removed,
+ .scan_started = scan_started,
+ .scan_finished = scan_finished,
+ .network_added = network_added,
+ .network_removed = network_removed,
+ .debug = debug,
};
-static int wifi_probe(struct connman_element *element)
+
+static int network_probe(struct connman_network *network)
{
- struct wifi_data *data;
- int err;
+ DBG("network %p", network);
- DBG("element %p name %s", element, element->name);
+ return 0;
+}
- data = g_try_new0(struct wifi_data, 1);
- if (data == NULL)
- return -ENOMEM;
+static void network_remove(struct connman_network *network)
+{
+ DBG("network %p", network);
+}
- g_static_mutex_init(&data->mutex);
+static void connect_callback(int result, GSupplicantInterface *interface,
+ void *user_data)
+{
+ connman_error("%s", __func__);
+}
- connman_element_set_data(element, data);
+static void disconnect_callback(int result, GSupplicantInterface *interface,
+ void *user_data)
+{
+ struct wifi_data *wifi = user_data;
- err = __supplicant_start(element, &wifi_callback);
- if (err < 0)
- return err;
+ if (result < 0) {
+ connman_error("%s", __func__);
+ return;
+ }
- __supplicant_scan(element);
+ connman_network_unref(wifi->network);
- return 0;
+ wifi->network = NULL;
}
-static void wifi_remove(struct connman_element *element)
+
+static GSupplicantSecurity network_security(const char *security)
{
- struct wifi_data *data = connman_element_get_data(element);
- GSList *list;
+ 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;
+}
- DBG("element %p name %s", element, element->name);
+static void ssid_init(GSupplicantSSID *ssid, struct connman_network *network)
+{
+ const char *security;
+
+ 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);
+ ssid->passphrase = connman_network_get_string(network,
+ "WiFi.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");
+ 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");
- __supplicant_stop(element);
+}
- connman_element_set_data(element, NULL);
+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;
- if (data == NULL)
- return;
+ DBG("network %p", network);
- g_static_mutex_lock(&data->mutex);
+ if (device == NULL)
+ return -ENODEV;
- for (list = data->list; list; list = list->next) {
- struct connman_element *network = list->data;
+ wifi = connman_device_get_data(device);
+ if (wifi == NULL)
+ return -ENODEV;
- connman_element_unregister(network);
- connman_element_unref(network);
- }
+ interface = wifi->interface;
- g_slist_free(data->list);
+ ssid_init(&ssid, network);
- g_static_mutex_unlock(&data->mutex);
+ wifi->network = connman_network_ref(network);
- g_free(data);
+ return g_supplicant_interface_connect(interface, &ssid,
+ connect_callback, NULL);
}
-static int wifi_update(struct connman_element *element)
+static int network_disconnect(struct connman_network *network)
{
- DBG("element %p name %s", element, element->name);
+ struct connman_device *device = connman_network_get_device(network);
+ struct wifi_data *wifi;
- __supplicant_scan(element);
+ DBG("network %p", network);
- return 0;
+ wifi = connman_device_get_data(device);
+ if (wifi == NULL || wifi->interface == NULL)
+ return -ENODEV;
+
+ return g_supplicant_interface_disconnect(wifi->interface,
+ disconnect_callback, wifi);
}
-static struct connman_driver wifi_driver = {
- .name = "wifi-device",
- .type = CONNMAN_ELEMENT_TYPE_DEVICE,
- .subtype = CONNMAN_ELEMENT_SUBTYPE_WIFI,
- .probe = wifi_probe,
- .remove = wifi_remove,
- .update = wifi_update,
+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 int wifi_init(void)
{
int err;
- err = connman_driver_register(&network_driver);
+ err = connman_network_driver_register(&network_driver);
if (err < 0)
return err;
- err = connman_driver_register(&wifi_driver);
+ err = g_supplicant_register(&callbacks);
if (err < 0) {
- connman_driver_unregister(&network_driver);
+ connman_network_driver_unregister(&network_driver);
return err;
}
static void wifi_exit(void)
{
- connman_driver_unregister(&network_driver);
- connman_driver_unregister(&wifi_driver);
+ DBG();
+
+ g_supplicant_unregister(&callbacks);
+
+ connman_network_driver_unregister(&network_driver);
}
-CONNMAN_PLUGIN_DEFINE("wifi", "WiFi interface plugin", VERSION,
- wifi_init, wifi_exit)
+CONNMAN_PLUGIN_DEFINE(wifi, "WiFi interface plugin", VERSION,
+ CONNMAN_PLUGIN_PRIORITY_DEFAULT, wifi_init, wifi_exit)