5 * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31 #include <sys/ioctl.h>
32 #include <sys/socket.h>
33 #include <linux/if_arp.h>
34 #include <linux/wireless.h>
35 #include <net/ethernet.h>
38 #define IFF_LOWER_UP 0x10000
41 #include <dbus/dbus.h>
44 #define CONNMAN_API_SUBJECT_TO_CHANGE
45 #include <connman/plugin.h>
46 #include <connman/inet.h>
47 #include <connman/device.h>
48 #include <connman/rtnl.h>
49 #include <connman/technology.h>
50 #include <connman/log.h>
51 #include <connman/option.h>
52 #include <connman/storage.h>
53 #include <include/setting.h>
55 #include <gsupplicant/gsupplicant.h>
57 #define CLEANUP_TIMEOUT 8 /* in seconds */
58 #define INACTIVE_TIMEOUT 12 /* in seconds */
59 #define MAXIMUM_RETRIES 4
61 #define BGSCAN_DEFAULT "simple:30:-45:300"
62 #define AUTOSCAN_DEFAULT "exponential:2:3600"
64 struct connman_technology *wifi_technology = NULL;
66 struct hidden_params {
68 unsigned int ssid_len;
74 * Used for autoscan "emulation".
75 * Should be removed when wpa_s autoscan support will be by default.
77 struct autoscan_params {
86 struct connman_device *device;
87 struct connman_network *network;
88 struct connman_network *pending_network;
90 GSupplicantInterface *interface;
91 GSupplicantState state;
92 connman_bool_t connected;
93 connman_bool_t disconnecting;
94 connman_bool_t tethering;
95 connman_bool_t bridged;
101 struct hidden_params *hidden;
103 * autoscan "emulation".
105 struct autoscan_params *autoscan;
108 static GList *iface_list = NULL;
110 static void handle_tethering(struct wifi_data *wifi)
112 if (wifi->tethering == FALSE)
115 if (wifi->bridge == NULL)
118 if (wifi->bridged == TRUE)
121 DBG("index %d bridge %s", wifi->index, wifi->bridge);
123 if (connman_inet_add_to_bridge(wifi->index, wifi->bridge) < 0)
126 wifi->bridged = TRUE;
129 static void wifi_newlink(unsigned flags, unsigned change, void *user_data)
131 struct connman_device *device = user_data;
132 struct wifi_data *wifi = connman_device_get_data(device);
134 DBG("index %d flags %d change %d", wifi->index, flags, change);
139 if ((wifi->flags & IFF_UP) != (flags & IFF_UP)) {
143 DBG("interface down");
146 if ((wifi->flags & IFF_LOWER_UP) != (flags & IFF_LOWER_UP)) {
147 if (flags & IFF_LOWER_UP) {
150 handle_tethering(wifi);
158 static int wifi_probe(struct connman_device *device)
160 struct wifi_data *wifi;
162 DBG("device %p", device);
164 wifi = g_try_new0(struct wifi_data, 1);
168 wifi->connected = FALSE;
169 wifi->disconnecting = FALSE;
170 wifi->tethering = FALSE;
171 wifi->bridged = FALSE;
173 wifi->state = G_SUPPLICANT_STATE_INACTIVE;
175 connman_device_set_data(device, wifi);
176 wifi->device = connman_device_ref(device);
178 wifi->index = connman_device_get_index(device);
181 wifi->watch = connman_rtnl_add_newlink_watch(wifi->index,
182 wifi_newlink, device);
184 iface_list = g_list_append(iface_list, wifi);
189 static void remove_networks(struct connman_device *device,
190 struct wifi_data *wifi)
194 for (list = wifi->networks; list != NULL; list = list->next) {
195 struct connman_network *network = list->data;
197 connman_device_remove_network(device, network);
198 connman_network_unref(network);
201 g_slist_free(wifi->networks);
202 wifi->networks = NULL;
205 static void stop_autoscan(struct connman_device *device)
207 struct wifi_data *wifi = connman_device_get_data(device);
208 struct autoscan_params *autoscan;
212 if (wifi == NULL || wifi->autoscan == NULL)
215 autoscan = wifi->autoscan;
217 if (autoscan->timeout > 0)
218 g_source_remove(autoscan->timeout);
220 autoscan->timeout = 0;
221 autoscan->interval = 0;
223 connman_device_unref(device);
226 static void wifi_remove(struct connman_device *device)
228 struct wifi_data *wifi = connman_device_get_data(device);
230 DBG("device %p wifi %p", device, wifi);
235 stop_autoscan(device);
237 iface_list = g_list_remove(iface_list, wifi);
239 remove_networks(device, wifi);
241 connman_device_set_powered(device, FALSE);
242 connman_device_set_data(device, NULL);
243 connman_device_unref(wifi->device);
244 connman_rtnl_remove_watch(wifi->watch);
246 g_supplicant_interface_set_data(wifi->interface, NULL);
248 g_free(wifi->autoscan);
249 g_free(wifi->identifier);
253 static int throw_wifi_scan(struct connman_device *device,
254 GSupplicantInterfaceCallback callback)
256 struct wifi_data *wifi = connman_device_get_data(device);
259 DBG("device %p %p", device, wifi->interface);
261 if (wifi->tethering == TRUE)
264 connman_device_ref(device);
266 ret = g_supplicant_interface_scan(wifi->interface, NULL,
269 connman_device_set_scanning(device, TRUE);
271 connman_device_unref(device);
276 static void autoscan_scan_callback(int result,
277 GSupplicantInterface *interface, void *user_data)
279 struct connman_device *device = user_data;
283 connman_device_set_scanning(device, FALSE);
286 static gboolean autoscan_timeout(gpointer data)
288 struct connman_device *device = data;
289 struct wifi_data *wifi = connman_device_get_data(device);
290 struct autoscan_params *autoscan;
293 autoscan = wifi->autoscan;
295 if (autoscan->interval <= 0) {
296 interval = autoscan->base;
299 interval = autoscan->interval * autoscan->base;
301 if (autoscan->interval >= autoscan->limit)
302 interval = autoscan->limit;
304 throw_wifi_scan(wifi->device, autoscan_scan_callback);
307 DBG("interval %d", interval);
309 autoscan->interval = interval;
311 autoscan->timeout = g_timeout_add_seconds(interval,
312 autoscan_timeout, device);
317 static void start_autoscan(struct connman_device *device)
319 struct wifi_data *wifi = connman_device_get_data(device);
320 struct autoscan_params *autoscan;
327 autoscan = wifi->autoscan;
328 if (autoscan == NULL)
331 if (autoscan->timeout > 0 || autoscan->interval > 0)
334 connman_device_ref(device);
336 autoscan_timeout(device);
339 static struct autoscan_params *parse_autoscan_params(const char *params)
341 struct autoscan_params *autoscan;
346 DBG("Emulating autoscan");
348 list_params = g_strsplit(params, ":", 0);
349 if (list_params == 0)
352 if (g_strv_length(list_params) < 3) {
353 g_strfreev(list_params);
357 base = atoi(list_params[1]);
358 limit = atoi(list_params[2]);
360 g_strfreev(list_params);
362 autoscan = g_try_malloc0(sizeof(struct autoscan_params));
363 if (autoscan == NULL) {
364 DBG("Could not allocate memory for autoscan");
368 DBG("base %d - limit %d", base, limit);
369 autoscan->base = base;
370 autoscan->limit = limit;
375 static void setup_autoscan(struct wifi_data *wifi)
377 if (wifi->autoscan == NULL)
378 wifi->autoscan = parse_autoscan_params(AUTOSCAN_DEFAULT);
380 start_autoscan(wifi->device);
383 static void interface_create_callback(int result,
384 GSupplicantInterface *interface,
387 struct wifi_data *wifi = user_data;
389 DBG("result %d ifname %s, wifi %p", result,
390 g_supplicant_interface_get_ifname(interface),
393 if (result < 0 || wifi == NULL)
396 wifi->interface = interface;
397 g_supplicant_interface_set_data(interface, wifi);
399 if (g_supplicant_interface_get_ready(interface) == FALSE)
402 DBG("interface is ready wifi %p tethering %d", wifi, wifi->tethering);
404 if (wifi->device == NULL) {
405 connman_error("WiFi device not set");
409 connman_device_set_powered(wifi->device, TRUE);
411 if (connman_setting_get_bool("BackgroundScanning") == FALSE)
414 /* Setting up automatic scanning */
415 setup_autoscan(wifi);
418 static int wifi_enable(struct connman_device *device)
420 struct wifi_data *wifi = connman_device_get_data(device);
421 const char *interface = connman_device_get_string(device, "Interface");
422 const char *driver = connman_option_get_string("wifi");
425 DBG("device %p %p", device, wifi);
427 ret = g_supplicant_interface_create(interface, driver, NULL,
428 interface_create_callback,
436 static int wifi_disable(struct connman_device *device)
438 struct wifi_data *wifi = connman_device_get_data(device);
441 DBG("device %p", device);
443 wifi->connected = FALSE;
444 wifi->disconnecting = FALSE;
446 if (wifi->pending_network != NULL)
447 wifi->pending_network = NULL;
449 remove_networks(device, wifi);
451 ret = g_supplicant_interface_remove(wifi->interface, NULL, NULL);
458 static void hidden_free(struct hidden_params *hidden)
463 g_free(hidden->identity);
464 g_free(hidden->passphrase);
468 static void scan_callback(int result, GSupplicantInterface *interface,
471 struct connman_device *device = user_data;
472 struct wifi_data *wifi = connman_device_get_data(device);
474 DBG("result %d", result);
476 if (wifi != NULL && wifi->hidden != NULL) {
477 hidden_free(wifi->hidden);
482 connman_device_reset_scanning(device);
484 connman_device_set_scanning(device, FALSE);
485 connman_device_unref(device);
487 start_autoscan(device);
490 static int add_scan_param(gchar *hex_ssid, int freq,
491 GSupplicantScanParams *scan_data,
492 int driver_max_scan_ssids)
496 if (driver_max_scan_ssids > scan_data->num_ssids && hex_ssid != NULL) {
498 unsigned int j = 0, hex;
499 size_t hex_ssid_len = strlen(hex_ssid);
501 ssid = g_try_malloc0(hex_ssid_len / 2);
505 for (i = 0; i < hex_ssid_len; i += 2) {
506 sscanf(hex_ssid + i, "%02x", &hex);
510 memcpy(scan_data->ssids[scan_data->num_ssids].ssid, ssid, j);
511 scan_data->ssids[scan_data->num_ssids].ssid_len = j;
512 scan_data->num_ssids++;
517 /* Don't add duplicate entries */
518 for (i = 0; i < G_SUPPLICANT_MAX_FAST_SCAN; i++) {
519 if (scan_data->freqs[i] == 0) {
520 scan_data->freqs[i] = freq;
522 } else if (scan_data->freqs[i] == freq)
529 struct last_connected {
535 static gint sort_entry(gconstpointer a, gconstpointer b, gpointer user_data)
537 GTimeVal *aval = (GTimeVal *)a;
538 GTimeVal *bval = (GTimeVal *)b;
540 /* Note that the sort order is descending */
541 if (aval->tv_sec < bval->tv_sec)
544 if (aval->tv_sec > bval->tv_sec)
550 static void free_entry(gpointer data)
552 struct last_connected *entry = data;
558 static int get_latest_connections(int max_ssids,
559 GSupplicantScanParams *scan_data)
562 GSequence *latest_list;
563 struct last_connected *entry;
572 latest_list = g_sequence_new(free_entry);
573 if (latest_list == NULL)
576 services = connman_storage_get_services();
577 for (i = 0; services && services[i]; i++) {
578 if (strncmp(services[i], "wifi_", 5) != 0)
581 keyfile = connman_storage_load_service(services[i]);
583 str = g_key_file_get_string(keyfile,
584 services[i], "Favorite", NULL);
585 if (str == NULL || g_strcmp0(str, "true")) {
588 g_key_file_free(keyfile);
593 str = g_key_file_get_string(keyfile,
594 services[i], "AutoConnect", NULL);
595 if (str == NULL || g_strcmp0(str, "true")) {
598 g_key_file_free(keyfile);
603 str = g_key_file_get_string(keyfile,
604 services[i], "Modified", NULL);
606 g_time_val_from_iso8601(str, &modified);
610 ssid = g_key_file_get_string(keyfile,
611 services[i], "SSID", NULL);
613 freq = g_key_file_get_integer(keyfile, services[i],
616 entry = g_try_new(struct last_connected, 1);
618 g_sequence_free(latest_list);
619 g_key_file_free(keyfile);
625 entry->modified = modified;
628 g_sequence_insert_sorted(latest_list, entry,
634 g_key_file_free(keyfile);
637 g_strfreev(services);
639 num_ssids = num_ssids > G_SUPPLICANT_MAX_FAST_SCAN ?
640 G_SUPPLICANT_MAX_FAST_SCAN : num_ssids;
642 iter = g_sequence_get_begin_iter(latest_list);
644 for (i = 0; i < num_ssids; i++) {
645 entry = g_sequence_get(iter);
647 DBG("ssid %s freq %d modified %lu", entry->ssid, entry->freq,
648 entry->modified.tv_sec);
650 add_scan_param(entry->ssid, entry->freq, scan_data, max_ssids);
652 iter = g_sequence_iter_next(iter);
655 g_sequence_free(latest_list);
659 static int wifi_scan(struct connman_device *device)
661 stop_autoscan(device);
663 return throw_wifi_scan(device, scan_callback);
666 static int wifi_scan_fast(struct connman_device *device)
668 struct wifi_data *wifi = connman_device_get_data(device);
669 GSupplicantScanParams *scan_params = NULL;
671 int driver_max_ssids = 0;
673 DBG("device %p %p", device, wifi->interface);
675 if (wifi->tethering == TRUE)
678 driver_max_ssids = g_supplicant_interface_get_max_scan_ssids(
680 DBG("max ssids %d", driver_max_ssids);
681 if (driver_max_ssids == 0)
682 return wifi_scan(device);
684 scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
685 if (scan_params == NULL)
688 ret = get_latest_connections(driver_max_ssids, scan_params);
691 return wifi_scan(device);
694 stop_autoscan(device);
696 connman_device_ref(device);
697 ret = g_supplicant_interface_scan(wifi->interface, scan_params,
698 scan_callback, device);
700 connman_device_set_scanning(device, TRUE);
703 connman_device_unref(device);
709 static int wifi_scan_hidden(struct connman_device *device,
710 const char *ssid, unsigned int ssid_len,
711 const char *identity, const char* passphrase)
713 struct wifi_data *wifi = connman_device_get_data(device);
714 GSupplicantScanParams *scan_params = NULL;
715 struct hidden_params *hidden;
718 DBG("hidden SSID %s", ssid);
720 if (wifi->tethering == TRUE || wifi->hidden != NULL)
723 if (ssid == NULL || ssid_len == 0 || ssid_len > 32)
726 scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
727 if (scan_params == NULL)
729 memcpy(scan_params->ssids[0].ssid, ssid, ssid_len);
730 scan_params->ssids[0].ssid_len = ssid_len;
731 scan_params->num_ssids = 1;
733 hidden = g_try_new0(struct hidden_params, 1);
734 if (hidden == NULL) {
738 memcpy(hidden->ssid, ssid, ssid_len);
739 hidden->ssid_len = ssid_len;
740 hidden->identity = g_strdup(identity);
741 hidden->passphrase = g_strdup(passphrase);
742 wifi->hidden = hidden;
744 stop_autoscan(device);
746 connman_device_ref(device);
747 ret = g_supplicant_interface_scan(wifi->interface, scan_params,
748 scan_callback, device);
750 connman_device_set_scanning(device, TRUE);
752 connman_device_unref(device);
754 hidden_free(wifi->hidden);
761 static struct connman_device_driver wifi_ng_driver = {
763 .type = CONNMAN_DEVICE_TYPE_WIFI,
764 .priority = CONNMAN_DEVICE_PRIORITY_LOW,
766 .remove = wifi_remove,
767 .enable = wifi_enable,
768 .disable = wifi_disable,
770 .scan_fast = wifi_scan_fast,
771 .scan_hidden = wifi_scan_hidden,
774 static void system_ready(void)
778 if (connman_device_driver_register(&wifi_ng_driver) < 0)
779 connman_error("Failed to register WiFi driver");
782 static void system_killed(void)
786 connman_device_driver_unregister(&wifi_ng_driver);
789 static int network_probe(struct connman_network *network)
791 DBG("network %p", network);
796 static void network_remove(struct connman_network *network)
798 struct connman_device *device = connman_network_get_device(network);
799 struct wifi_data *wifi;
801 DBG("network %p", network);
803 wifi = connman_device_get_data(device);
807 if (wifi->network != network)
810 wifi->network = NULL;
813 static void connect_callback(int result, GSupplicantInterface *interface,
816 struct connman_network *network = user_data;
818 DBG("network %p result %d", network, result);
820 if (result == -ENOKEY) {
821 connman_network_set_error(network,
822 CONNMAN_NETWORK_ERROR_INVALID_KEY);
823 } else if (result < 0) {
824 connman_network_set_error(network,
825 CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
829 static GSupplicantSecurity network_security(const char *security)
831 if (g_str_equal(security, "none") == TRUE)
832 return G_SUPPLICANT_SECURITY_NONE;
833 else if (g_str_equal(security, "wep") == TRUE)
834 return G_SUPPLICANT_SECURITY_WEP;
835 else if (g_str_equal(security, "psk") == TRUE)
836 return G_SUPPLICANT_SECURITY_PSK;
837 else if (g_str_equal(security, "wpa") == TRUE)
838 return G_SUPPLICANT_SECURITY_PSK;
839 else if (g_str_equal(security, "rsn") == TRUE)
840 return G_SUPPLICANT_SECURITY_PSK;
841 else if (g_str_equal(security, "ieee8021x") == TRUE)
842 return G_SUPPLICANT_SECURITY_IEEE8021X;
844 return G_SUPPLICANT_SECURITY_UNKNOWN;
847 static void ssid_init(GSupplicantSSID *ssid, struct connman_network *network)
849 const char *security, *passphrase, *agent_passphrase;
851 memset(ssid, 0, sizeof(*ssid));
852 ssid->mode = G_SUPPLICANT_MODE_INFRA;
853 ssid->ssid = connman_network_get_blob(network, "WiFi.SSID",
856 security = connman_network_get_string(network, "WiFi.Security");
857 ssid->security = network_security(security);
858 passphrase = connman_network_get_string(network,
860 if (passphrase == NULL || strlen(passphrase) == 0) {
862 /* Use agent provided passphrase as a fallback */
863 agent_passphrase = connman_network_get_string(network,
864 "WiFi.AgentPassphrase");
866 if (agent_passphrase == NULL || strlen(agent_passphrase) == 0)
867 ssid->passphrase = NULL;
869 ssid->passphrase = agent_passphrase;
871 ssid->passphrase = passphrase;
873 ssid->eap = connman_network_get_string(network, "WiFi.EAP");
876 * If our private key password is unset,
877 * we use the supplied passphrase. That is needed
878 * for PEAP where 2 passphrases (identity and client
879 * cert may have to be provided.
881 if (connman_network_get_string(network,
882 "WiFi.PrivateKeyPassphrase") == NULL)
883 connman_network_set_string(network,
884 "WiFi.PrivateKeyPassphrase",
886 /* We must have an identity for both PEAP and TLS */
887 ssid->identity = connman_network_get_string(network, "WiFi.Identity");
889 /* Use agent provided identity as a fallback */
890 if (ssid->identity == NULL || strlen(ssid->identity) == 0)
891 ssid->identity = connman_network_get_string(network,
892 "WiFi.AgentIdentity");
894 ssid->ca_cert_path = connman_network_get_string(network,
896 ssid->client_cert_path = connman_network_get_string(network,
897 "WiFi.ClientCertFile");
898 ssid->private_key_path = connman_network_get_string(network,
899 "WiFi.PrivateKeyFile");
900 ssid->private_key_passphrase = connman_network_get_string(network,
901 "WiFi.PrivateKeyPassphrase");
902 ssid->phase2_auth = connman_network_get_string(network, "WiFi.Phase2");
904 ssid->use_wps = connman_network_get_bool(network, "WiFi.UseWPS");
905 ssid->pin_wps = connman_network_get_string(network, "WiFi.PinWPS");
907 ssid->bgscan = BGSCAN_DEFAULT;
910 static int network_connect(struct connman_network *network)
912 struct connman_device *device = connman_network_get_device(network);
913 struct wifi_data *wifi;
914 GSupplicantInterface *interface;
915 GSupplicantSSID *ssid;
917 DBG("network %p", network);
922 wifi = connman_device_get_data(device);
926 ssid = g_try_malloc0(sizeof(GSupplicantSSID));
930 interface = wifi->interface;
932 ssid_init(ssid, network);
934 if (wifi->disconnecting == TRUE)
935 wifi->pending_network = network;
937 wifi->network = network;
940 return g_supplicant_interface_connect(interface, ssid,
941 connect_callback, network);
947 static void disconnect_callback(int result, GSupplicantInterface *interface,
950 struct wifi_data *wifi = user_data;
952 if (wifi->network != NULL) {
954 * if result < 0 supplican return an error because
955 * the network is not current.
956 * we wont receive G_SUPPLICANT_STATE_DISCONNECTED since it
957 * failed, call connman_network_set_connected to report
958 * disconnect is completed.
961 connman_network_set_connected(wifi->network, FALSE);
964 wifi->network = NULL;
966 wifi->disconnecting = FALSE;
968 if (wifi->pending_network != NULL) {
969 network_connect(wifi->pending_network);
970 wifi->pending_network = NULL;
973 start_autoscan(wifi->device);
976 static int network_disconnect(struct connman_network *network)
978 struct connman_device *device = connman_network_get_device(network);
979 struct wifi_data *wifi;
982 DBG("network %p", network);
984 wifi = connman_device_get_data(device);
985 if (wifi == NULL || wifi->interface == NULL)
988 connman_network_set_associating(network, FALSE);
990 if (wifi->disconnecting == TRUE)
993 wifi->disconnecting = TRUE;
995 err = g_supplicant_interface_disconnect(wifi->interface,
996 disconnect_callback, wifi);
998 wifi->disconnecting = FALSE;
1003 static struct connman_network_driver network_driver = {
1005 .type = CONNMAN_NETWORK_TYPE_WIFI,
1006 .priority = CONNMAN_NETWORK_PRIORITY_LOW,
1007 .probe = network_probe,
1008 .remove = network_remove,
1009 .connect = network_connect,
1010 .disconnect = network_disconnect,
1013 static void interface_added(GSupplicantInterface *interface)
1015 const char *ifname = g_supplicant_interface_get_ifname(interface);
1016 const char *driver = g_supplicant_interface_get_driver(interface);
1017 struct wifi_data *wifi;
1019 wifi = g_supplicant_interface_get_data(interface);
1022 * We can get here with a NULL wifi pointer when
1023 * the interface added signal is sent before the
1024 * interface creation callback is called.
1029 DBG("ifname %s driver %s wifi %p tethering %d",
1030 ifname, driver, wifi, wifi->tethering);
1032 if (wifi->device == NULL) {
1033 connman_error("WiFi device not set");
1037 connman_device_set_powered(wifi->device, TRUE);
1039 if (wifi->tethering == TRUE)
1043 static connman_bool_t is_idle(struct wifi_data *wifi)
1045 DBG("state %d", wifi->state);
1047 switch (wifi->state) {
1048 case G_SUPPLICANT_STATE_UNKNOWN:
1049 case G_SUPPLICANT_STATE_DISCONNECTED:
1050 case G_SUPPLICANT_STATE_INACTIVE:
1051 case G_SUPPLICANT_STATE_SCANNING:
1054 case G_SUPPLICANT_STATE_AUTHENTICATING:
1055 case G_SUPPLICANT_STATE_ASSOCIATING:
1056 case G_SUPPLICANT_STATE_ASSOCIATED:
1057 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
1058 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
1059 case G_SUPPLICANT_STATE_COMPLETED:
1066 static connman_bool_t is_idle_wps(GSupplicantInterface *interface,
1067 struct wifi_data *wifi)
1069 /* First, let's check if WPS processing did not went wrong */
1070 if (g_supplicant_interface_get_wps_state(interface) ==
1071 G_SUPPLICANT_WPS_STATE_FAIL)
1074 /* Unlike normal connection, being associated while processing wps
1075 * actually means that we are idling. */
1076 switch (wifi->state) {
1077 case G_SUPPLICANT_STATE_UNKNOWN:
1078 case G_SUPPLICANT_STATE_DISCONNECTED:
1079 case G_SUPPLICANT_STATE_INACTIVE:
1080 case G_SUPPLICANT_STATE_SCANNING:
1081 case G_SUPPLICANT_STATE_ASSOCIATED:
1083 case G_SUPPLICANT_STATE_AUTHENTICATING:
1084 case G_SUPPLICANT_STATE_ASSOCIATING:
1085 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
1086 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
1087 case G_SUPPLICANT_STATE_COMPLETED:
1094 static connman_bool_t handle_wps_completion(GSupplicantInterface *interface,
1095 struct connman_network *network,
1096 struct connman_device *device,
1097 struct wifi_data *wifi)
1101 wps = connman_network_get_bool(network, "WiFi.UseWPS");
1103 const unsigned char *ssid, *wps_ssid;
1104 unsigned int ssid_len, wps_ssid_len;
1105 const char *wps_key;
1107 /* Checking if we got associated with requested
1109 ssid = connman_network_get_blob(network, "WiFi.SSID",
1112 wps_ssid = g_supplicant_interface_get_wps_ssid(
1113 interface, &wps_ssid_len);
1115 if (wps_ssid == NULL || wps_ssid_len != ssid_len ||
1116 memcmp(ssid, wps_ssid, ssid_len) != 0) {
1117 connman_network_set_associating(network, FALSE);
1118 g_supplicant_interface_disconnect(wifi->interface,
1119 disconnect_callback, wifi);
1123 wps_key = g_supplicant_interface_get_wps_key(interface);
1124 connman_network_set_string(network, "WiFi.Passphrase",
1127 connman_network_set_string(network, "WiFi.PinWPS", NULL);
1133 static connman_bool_t handle_4way_handshake_failure(GSupplicantInterface *interface,
1134 struct connman_network *network,
1135 struct wifi_data *wifi)
1137 if (wifi->state != G_SUPPLICANT_STATE_4WAY_HANDSHAKE)
1142 if (wifi->retries < MAXIMUM_RETRIES)
1145 connman_network_set_error(network, CONNMAN_NETWORK_ERROR_INVALID_KEY);
1150 static void interface_state(GSupplicantInterface *interface)
1152 struct connman_network *network;
1153 struct connman_device *device;
1154 struct wifi_data *wifi;
1155 GSupplicantState state = g_supplicant_interface_get_state(interface);
1158 wifi = g_supplicant_interface_get_data(interface);
1160 DBG("wifi %p interface state %d", wifi, state);
1165 network = wifi->network;
1166 device = wifi->device;
1168 if (device == NULL || network == NULL)
1172 case G_SUPPLICANT_STATE_SCANNING:
1175 case G_SUPPLICANT_STATE_AUTHENTICATING:
1176 case G_SUPPLICANT_STATE_ASSOCIATING:
1177 connman_network_set_associating(network, TRUE);
1180 case G_SUPPLICANT_STATE_COMPLETED:
1181 if (handle_wps_completion(interface, network, device, wifi) ==
1185 /* reset scan trigger and schedule background scan */
1186 connman_device_schedule_scan(device);
1188 connman_network_set_connected(network, TRUE);
1191 case G_SUPPLICANT_STATE_DISCONNECTED:
1193 * If we're in one of the idle modes, we have
1194 * not started association yet and thus setting
1195 * those ones to FALSE could cancel an association
1198 wps = connman_network_get_bool(network, "WiFi.UseWPS");
1200 if (is_idle_wps(interface, wifi) == TRUE)
1206 /* If previous state was 4way-handshake, then
1207 * it's either: psk was incorrect and thus we retry
1208 * or if we reach the maximum retries we declare the
1210 if (handle_4way_handshake_failure(interface,
1211 network, wifi) == TRUE)
1214 /* We disable the selected network, if not then
1215 * wpa_supplicant will loop retrying */
1216 if (g_supplicant_interface_enable_selected_network(interface,
1218 DBG("Could not disables selected network");
1220 connman_network_set_associating(network, FALSE);
1221 connman_network_set_connected(network, FALSE);
1224 case G_SUPPLICANT_STATE_INACTIVE:
1225 connman_network_set_associating(network, FALSE);
1228 case G_SUPPLICANT_STATE_UNKNOWN:
1229 case G_SUPPLICANT_STATE_ASSOCIATED:
1230 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
1231 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
1235 wifi->state = state;
1240 static void interface_removed(GSupplicantInterface *interface)
1242 const char *ifname = g_supplicant_interface_get_ifname(interface);
1243 struct wifi_data *wifi;
1245 DBG("ifname %s", ifname);
1247 wifi = g_supplicant_interface_get_data(interface);
1249 if (wifi != NULL && wifi->tethering == TRUE)
1252 if (wifi == NULL || wifi->device == NULL) {
1253 connman_error("Wrong wifi pointer");
1257 wifi->interface = NULL;
1258 connman_device_set_powered(wifi->device, FALSE);
1261 static void scan_started(GSupplicantInterface *interface)
1266 static void scan_finished(GSupplicantInterface *interface)
1271 static unsigned char calculate_strength(GSupplicantNetwork *supplicant_network)
1273 unsigned char strength;
1275 strength = 120 + g_supplicant_network_get_signal(supplicant_network);
1282 static void network_added(GSupplicantNetwork *supplicant_network)
1284 struct connman_network *network;
1285 GSupplicantInterface *interface;
1286 struct wifi_data *wifi;
1287 const char *name, *identifier, *security, *group, *mode;
1288 const unsigned char *ssid;
1289 unsigned int ssid_len;
1291 connman_bool_t wps_pbc;
1292 connman_bool_t wps_ready;
1293 connman_bool_t wps_advertizing;
1297 interface = g_supplicant_network_get_interface(supplicant_network);
1298 wifi = g_supplicant_interface_get_data(interface);
1299 name = g_supplicant_network_get_name(supplicant_network);
1300 identifier = g_supplicant_network_get_identifier(supplicant_network);
1301 security = g_supplicant_network_get_security(supplicant_network);
1302 group = g_supplicant_network_get_identifier(supplicant_network);
1303 wps = g_supplicant_network_get_wps(supplicant_network);
1304 wps_pbc = g_supplicant_network_is_wps_pbc(supplicant_network);
1305 wps_ready = g_supplicant_network_is_wps_active(supplicant_network);
1306 wps_advertizing = g_supplicant_network_is_wps_advertizing(
1307 supplicant_network);
1308 mode = g_supplicant_network_get_mode(supplicant_network);
1313 ssid = g_supplicant_network_get_ssid(supplicant_network, &ssid_len);
1315 network = connman_device_get_network(wifi->device, identifier);
1317 if (network == NULL) {
1318 network = connman_network_create(identifier,
1319 CONNMAN_NETWORK_TYPE_WIFI);
1320 if (network == NULL)
1323 connman_network_set_index(network, wifi->index);
1325 if (connman_device_add_network(wifi->device, network) < 0) {
1326 connman_network_unref(network);
1330 wifi->networks = g_slist_append(wifi->networks, network);
1333 if (name != NULL && name[0] != '\0')
1334 connman_network_set_name(network, name);
1336 connman_network_set_blob(network, "WiFi.SSID",
1338 connman_network_set_string(network, "WiFi.Security", security);
1339 connman_network_set_strength(network,
1340 calculate_strength(supplicant_network));
1341 connman_network_set_bool(network, "WiFi.WPS", wps);
1344 /* Is AP advertizing for WPS association?
1345 * If so, we decide to use WPS by default */
1346 if (wps_ready == TRUE && wps_pbc == TRUE &&
1347 wps_advertizing == TRUE)
1348 connman_network_set_bool(network, "WiFi.UseWPS", TRUE);
1351 connman_network_set_frequency(network,
1352 g_supplicant_network_get_frequency(supplicant_network));
1354 connman_network_set_available(network, TRUE);
1355 connman_network_set_string(network, "WiFi.Mode", mode);
1358 connman_network_set_group(network, group);
1360 if (wifi->hidden != NULL) {
1361 if (wifi->hidden->ssid_len == ssid_len &&
1362 memcmp(wifi->hidden->ssid, ssid,
1364 connman_network_connect_hidden(network,
1365 wifi->hidden->identity,
1366 wifi->hidden->passphrase);
1367 hidden_free(wifi->hidden);
1368 wifi->hidden = NULL;
1373 static void network_removed(GSupplicantNetwork *network)
1375 GSupplicantInterface *interface;
1376 struct wifi_data *wifi;
1377 const char *name, *identifier;
1378 struct connman_network *connman_network;
1380 interface = g_supplicant_network_get_interface(network);
1381 wifi = g_supplicant_interface_get_data(interface);
1382 identifier = g_supplicant_network_get_identifier(network);
1383 name = g_supplicant_network_get_name(network);
1385 DBG("name %s", name);
1390 connman_network = connman_device_get_network(wifi->device, identifier);
1391 if (connman_network == NULL)
1394 wifi->networks = g_slist_remove(wifi->networks, connman_network);
1396 connman_device_remove_network(wifi->device, connman_network);
1397 connman_network_unref(connman_network);
1400 static void network_changed(GSupplicantNetwork *network, const char *property)
1402 GSupplicantInterface *interface;
1403 struct wifi_data *wifi;
1404 const char *name, *identifier;
1405 struct connman_network *connman_network;
1407 interface = g_supplicant_network_get_interface(network);
1408 wifi = g_supplicant_interface_get_data(interface);
1409 identifier = g_supplicant_network_get_identifier(network);
1410 name = g_supplicant_network_get_name(network);
1412 DBG("name %s", name);
1417 connman_network = connman_device_get_network(wifi->device, identifier);
1418 if (connman_network == NULL)
1421 if (g_str_equal(property, "Signal") == TRUE) {
1422 connman_network_set_strength(connman_network,
1423 calculate_strength(network));
1424 connman_network_update(connman_network);
1428 static void debug(const char *str)
1430 if (getenv("CONNMAN_SUPPLICANT_DEBUG"))
1431 connman_debug("%s", str);
1434 static const GSupplicantCallbacks callbacks = {
1435 .system_ready = system_ready,
1436 .system_killed = system_killed,
1437 .interface_added = interface_added,
1438 .interface_state = interface_state,
1439 .interface_removed = interface_removed,
1440 .scan_started = scan_started,
1441 .scan_finished = scan_finished,
1442 .network_added = network_added,
1443 .network_removed = network_removed,
1444 .network_changed = network_changed,
1449 static int tech_probe(struct connman_technology *technology)
1451 wifi_technology = technology;
1456 static void tech_remove(struct connman_technology *technology)
1458 wifi_technology = NULL;
1461 struct wifi_tethering_info {
1462 struct wifi_data *wifi;
1463 struct connman_technology *technology;
1465 GSupplicantSSID *ssid;
1468 static GSupplicantSSID *ssid_ap_init(const char *ssid, const char *passphrase)
1470 GSupplicantSSID *ap;
1472 ap = g_try_malloc0(sizeof(GSupplicantSSID));
1476 ap->mode = G_SUPPLICANT_MODE_MASTER;
1478 ap->ssid_len = strlen(ssid);
1482 if (passphrase == NULL || strlen(passphrase) == 0) {
1483 ap->security = G_SUPPLICANT_SECURITY_NONE;
1484 ap->passphrase = NULL;
1486 ap->security = G_SUPPLICANT_SECURITY_PSK;
1487 ap->protocol = G_SUPPLICANT_PROTO_RSN;
1488 ap->pairwise_cipher = G_SUPPLICANT_PAIRWISE_CCMP;
1489 ap->group_cipher = G_SUPPLICANT_GROUP_CCMP;
1490 ap->passphrase = passphrase;
1496 static void ap_start_callback(int result, GSupplicantInterface *interface,
1499 struct wifi_tethering_info *info = user_data;
1501 DBG("result %d index %d bridge %s",
1502 result, info->wifi->index, info->wifi->bridge);
1505 connman_inet_remove_from_bridge(info->wifi->index,
1506 info->wifi->bridge);
1507 connman_technology_tethering_notify(info->technology, FALSE);
1510 g_free(info->ifname);
1514 static void ap_create_callback(int result,
1515 GSupplicantInterface *interface,
1518 struct wifi_tethering_info *info = user_data;
1520 DBG("result %d ifname %s", result,
1521 g_supplicant_interface_get_ifname(interface));
1524 connman_inet_remove_from_bridge(info->wifi->index,
1525 info->wifi->bridge);
1526 connman_technology_tethering_notify(info->technology, FALSE);
1528 g_free(info->ifname);
1533 info->wifi->interface = interface;
1534 g_supplicant_interface_set_data(interface, info->wifi);
1536 if (g_supplicant_interface_set_apscan(interface, 2) < 0)
1537 connman_error("Failed to set interface ap_scan property");
1539 g_supplicant_interface_connect(interface, info->ssid,
1540 ap_start_callback, info);
1543 static void sta_remove_callback(int result,
1544 GSupplicantInterface *interface,
1547 struct wifi_tethering_info *info = user_data;
1548 const char *driver = connman_option_get_string("wifi");
1550 DBG("ifname %s result %d ", info->ifname, result);
1553 info->wifi->tethering = TRUE;
1555 g_free(info->ifname);
1560 info->wifi->interface = NULL;
1562 connman_technology_tethering_notify(info->technology, TRUE);
1564 g_supplicant_interface_create(info->ifname, driver, info->wifi->bridge,
1569 static int tech_set_tethering(struct connman_technology *technology,
1570 const char *identifier, const char *passphrase,
1571 const char *bridge, connman_bool_t enabled)
1574 GSupplicantInterface *interface;
1575 struct wifi_data *wifi;
1576 struct wifi_tethering_info *info;
1583 if (enabled == FALSE) {
1584 for (list = iface_list; list; list = list->next) {
1587 if (wifi->tethering == TRUE) {
1588 wifi->tethering = FALSE;
1590 connman_inet_remove_from_bridge(wifi->index,
1592 wifi->bridged = FALSE;
1596 connman_technology_tethering_notify(technology, FALSE);
1601 for (list = iface_list; list; list = list->next) {
1604 interface = wifi->interface;
1606 if (interface == NULL)
1609 ifname = g_supplicant_interface_get_ifname(wifi->interface);
1611 mode = g_supplicant_interface_get_mode(interface);
1612 if ((mode & G_SUPPLICANT_CAPABILITY_MODE_AP) == 0) {
1613 DBG("%s does not support AP mode", ifname);
1617 info = g_try_malloc0(sizeof(struct wifi_tethering_info));
1622 info->technology = technology;
1623 info->wifi->bridge = bridge;
1624 info->ssid = ssid_ap_init(identifier, passphrase);
1625 if (info->ssid == NULL) {
1629 info->ifname = g_strdup(ifname);
1630 if (info->ifname == NULL) {
1635 info->wifi->tethering = TRUE;
1637 err = g_supplicant_interface_remove(interface,
1638 sta_remove_callback,
1647 static void regdom_callback(void *user_data)
1649 char *alpha2 = user_data;
1653 if (wifi_technology == NULL)
1656 connman_technology_regdom_notify(wifi_technology, alpha2);
1659 static int tech_set_regdom(struct connman_technology *technology, const char *alpha2)
1661 return g_supplicant_set_country(alpha2, regdom_callback, alpha2);
1664 static struct connman_technology_driver tech_driver = {
1666 .type = CONNMAN_SERVICE_TYPE_WIFI,
1667 .probe = tech_probe,
1668 .remove = tech_remove,
1669 .set_tethering = tech_set_tethering,
1670 .set_regdom = tech_set_regdom,
1673 static int wifi_init(void)
1677 err = connman_network_driver_register(&network_driver);
1681 err = g_supplicant_register(&callbacks);
1683 connman_network_driver_unregister(&network_driver);
1687 err = connman_technology_driver_register(&tech_driver);
1689 g_supplicant_unregister(&callbacks);
1690 connman_network_driver_unregister(&network_driver);
1697 static void wifi_exit(void)
1701 connman_technology_driver_unregister(&tech_driver);
1703 g_supplicant_unregister(&callbacks);
1705 connman_network_driver_unregister(&network_driver);
1708 CONNMAN_PLUGIN_DEFINE(wifi, "WiFi interface plugin", VERSION,
1709 CONNMAN_PLUGIN_PRIORITY_DEFAULT, wifi_init, wifi_exit)