5 * Copyright (C) 2007-2012 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:3:300"
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 && autoscan->interval == 0)
220 g_source_remove(autoscan->timeout);
222 autoscan->timeout = 0;
223 autoscan->interval = 0;
225 connman_device_unref(device);
228 static void wifi_remove(struct connman_device *device)
230 struct wifi_data *wifi = connman_device_get_data(device);
232 DBG("device %p wifi %p", device, wifi);
237 stop_autoscan(device);
239 iface_list = g_list_remove(iface_list, wifi);
241 remove_networks(device, wifi);
243 connman_device_set_powered(device, FALSE);
244 connman_device_set_data(device, NULL);
245 connman_device_unref(wifi->device);
246 connman_rtnl_remove_watch(wifi->watch);
248 g_supplicant_interface_set_data(wifi->interface, NULL);
250 g_free(wifi->autoscan);
251 g_free(wifi->identifier);
255 static int throw_wifi_scan(struct connman_device *device,
256 GSupplicantInterfaceCallback callback)
258 struct wifi_data *wifi = connman_device_get_data(device);
261 DBG("device %p %p", device, wifi->interface);
263 if (wifi->tethering == TRUE)
266 connman_device_ref(device);
268 ret = g_supplicant_interface_scan(wifi->interface, NULL,
271 connman_device_set_scanning(device, TRUE);
273 connman_device_unref(device);
278 static void autoscan_scan_callback(int result,
279 GSupplicantInterface *interface, void *user_data)
281 struct connman_device *device = user_data;
285 connman_device_set_scanning(device, FALSE);
286 connman_device_unref(device);
289 static gboolean autoscan_timeout(gpointer data)
291 struct connman_device *device = data;
292 struct wifi_data *wifi = connman_device_get_data(device);
293 struct autoscan_params *autoscan;
296 autoscan = wifi->autoscan;
298 if (autoscan->interval <= 0) {
299 interval = autoscan->base;
302 interval = autoscan->interval * autoscan->base;
304 if (autoscan->interval >= autoscan->limit)
305 interval = autoscan->limit;
307 throw_wifi_scan(wifi->device, autoscan_scan_callback);
310 DBG("interval %d", interval);
312 autoscan->interval = interval;
314 autoscan->timeout = g_timeout_add_seconds(interval,
315 autoscan_timeout, device);
320 static void start_autoscan(struct connman_device *device)
322 struct wifi_data *wifi = connman_device_get_data(device);
323 struct autoscan_params *autoscan;
330 autoscan = wifi->autoscan;
331 if (autoscan == NULL)
334 if (autoscan->timeout > 0 || autoscan->interval > 0)
337 connman_device_ref(device);
339 autoscan_timeout(device);
342 static struct autoscan_params *parse_autoscan_params(const char *params)
344 struct autoscan_params *autoscan;
349 DBG("Emulating autoscan");
351 list_params = g_strsplit(params, ":", 0);
352 if (list_params == 0)
355 if (g_strv_length(list_params) < 3) {
356 g_strfreev(list_params);
360 base = atoi(list_params[1]);
361 limit = atoi(list_params[2]);
363 g_strfreev(list_params);
365 autoscan = g_try_malloc0(sizeof(struct autoscan_params));
366 if (autoscan == NULL) {
367 DBG("Could not allocate memory for autoscan");
371 DBG("base %d - limit %d", base, limit);
372 autoscan->base = base;
373 autoscan->limit = limit;
378 static void setup_autoscan(struct wifi_data *wifi)
380 if (wifi->autoscan == NULL)
381 wifi->autoscan = parse_autoscan_params(AUTOSCAN_DEFAULT);
383 start_autoscan(wifi->device);
386 static void interface_create_callback(int result,
387 GSupplicantInterface *interface,
390 struct wifi_data *wifi = user_data;
392 DBG("result %d ifname %s, wifi %p", result,
393 g_supplicant_interface_get_ifname(interface),
396 if (result < 0 || wifi == NULL)
399 wifi->interface = interface;
400 g_supplicant_interface_set_data(interface, wifi);
402 if (g_supplicant_interface_get_ready(interface) == FALSE)
405 DBG("interface is ready wifi %p tethering %d", wifi, wifi->tethering);
407 if (wifi->device == NULL) {
408 connman_error("WiFi device not set");
412 connman_device_set_powered(wifi->device, TRUE);
414 if (connman_setting_get_bool("BackgroundScanning") == FALSE)
417 /* Setting up automatic scanning */
418 setup_autoscan(wifi);
421 static int wifi_enable(struct connman_device *device)
423 struct wifi_data *wifi = connman_device_get_data(device);
424 const char *interface = connman_device_get_string(device, "Interface");
425 const char *driver = connman_option_get_string("wifi");
428 DBG("device %p %p", device, wifi);
430 ret = g_supplicant_interface_create(interface, driver, NULL,
431 interface_create_callback,
439 static int wifi_disable(struct connman_device *device)
441 struct wifi_data *wifi = connman_device_get_data(device);
444 DBG("device %p", device);
446 wifi->connected = FALSE;
447 wifi->disconnecting = FALSE;
449 if (wifi->pending_network != NULL)
450 wifi->pending_network = NULL;
452 remove_networks(device, wifi);
454 ret = g_supplicant_interface_remove(wifi->interface, NULL, NULL);
461 static void hidden_free(struct hidden_params *hidden)
466 g_free(hidden->identity);
467 g_free(hidden->passphrase);
471 static void scan_callback(int result, GSupplicantInterface *interface,
474 struct connman_device *device = user_data;
475 struct wifi_data *wifi = connman_device_get_data(device);
477 DBG("result %d", result);
479 if (wifi != NULL && wifi->hidden != NULL) {
480 hidden_free(wifi->hidden);
485 connman_device_reset_scanning(device);
487 connman_device_set_scanning(device, FALSE);
488 connman_device_unref(device);
490 start_autoscan(device);
493 static int add_scan_param(gchar *hex_ssid, int freq,
494 GSupplicantScanParams *scan_data,
495 int driver_max_scan_ssids)
499 if (driver_max_scan_ssids > scan_data->num_ssids && hex_ssid != NULL) {
501 unsigned int j = 0, hex;
502 size_t hex_ssid_len = strlen(hex_ssid);
504 ssid = g_try_malloc0(hex_ssid_len / 2);
508 for (i = 0; i < hex_ssid_len; i += 2) {
509 sscanf(hex_ssid + i, "%02x", &hex);
513 memcpy(scan_data->ssids[scan_data->num_ssids].ssid, ssid, j);
514 scan_data->ssids[scan_data->num_ssids].ssid_len = j;
515 scan_data->num_ssids++;
520 /* Don't add duplicate entries */
521 for (i = 0; i < G_SUPPLICANT_MAX_FAST_SCAN; i++) {
522 if (scan_data->freqs[i] == 0) {
523 scan_data->freqs[i] = freq;
525 } else if (scan_data->freqs[i] == freq)
532 struct last_connected {
538 static gint sort_entry(gconstpointer a, gconstpointer b, gpointer user_data)
540 GTimeVal *aval = (GTimeVal *)a;
541 GTimeVal *bval = (GTimeVal *)b;
543 /* Note that the sort order is descending */
544 if (aval->tv_sec < bval->tv_sec)
547 if (aval->tv_sec > bval->tv_sec)
553 static void free_entry(gpointer data)
555 struct last_connected *entry = data;
561 static int get_latest_connections(int max_ssids,
562 GSupplicantScanParams *scan_data)
565 GSequence *latest_list;
566 struct last_connected *entry;
575 latest_list = g_sequence_new(free_entry);
576 if (latest_list == NULL)
579 services = connman_storage_get_services();
580 for (i = 0; services && services[i]; i++) {
581 if (strncmp(services[i], "wifi_", 5) != 0)
584 keyfile = connman_storage_load_service(services[i]);
586 str = g_key_file_get_string(keyfile,
587 services[i], "Favorite", NULL);
588 if (str == NULL || g_strcmp0(str, "true")) {
591 g_key_file_free(keyfile);
596 str = g_key_file_get_string(keyfile,
597 services[i], "AutoConnect", NULL);
598 if (str == NULL || g_strcmp0(str, "true")) {
601 g_key_file_free(keyfile);
606 str = g_key_file_get_string(keyfile,
607 services[i], "Modified", NULL);
609 g_time_val_from_iso8601(str, &modified);
613 ssid = g_key_file_get_string(keyfile,
614 services[i], "SSID", NULL);
616 freq = g_key_file_get_integer(keyfile, services[i],
619 entry = g_try_new(struct last_connected, 1);
621 g_sequence_free(latest_list);
622 g_key_file_free(keyfile);
628 entry->modified = modified;
631 g_sequence_insert_sorted(latest_list, entry,
637 g_key_file_free(keyfile);
640 g_strfreev(services);
642 num_ssids = num_ssids > G_SUPPLICANT_MAX_FAST_SCAN ?
643 G_SUPPLICANT_MAX_FAST_SCAN : num_ssids;
645 iter = g_sequence_get_begin_iter(latest_list);
647 for (i = 0; i < num_ssids; i++) {
648 entry = g_sequence_get(iter);
650 DBG("ssid %s freq %d modified %lu", entry->ssid, entry->freq,
651 entry->modified.tv_sec);
653 add_scan_param(entry->ssid, entry->freq, scan_data, max_ssids);
655 iter = g_sequence_iter_next(iter);
658 g_sequence_free(latest_list);
662 static int wifi_scan(struct connman_device *device)
664 stop_autoscan(device);
666 return throw_wifi_scan(device, scan_callback);
669 static int wifi_scan_fast(struct connman_device *device)
671 struct wifi_data *wifi = connman_device_get_data(device);
672 GSupplicantScanParams *scan_params = NULL;
674 int driver_max_ssids = 0;
676 DBG("device %p %p", device, wifi->interface);
678 if (wifi->tethering == TRUE)
681 driver_max_ssids = g_supplicant_interface_get_max_scan_ssids(
683 DBG("max ssids %d", driver_max_ssids);
684 if (driver_max_ssids == 0)
685 return wifi_scan(device);
687 scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
688 if (scan_params == NULL)
691 ret = get_latest_connections(driver_max_ssids, scan_params);
694 return wifi_scan(device);
697 stop_autoscan(device);
699 connman_device_ref(device);
700 ret = g_supplicant_interface_scan(wifi->interface, scan_params,
701 scan_callback, device);
703 connman_device_set_scanning(device, TRUE);
706 connman_device_unref(device);
712 static int wifi_scan_hidden(struct connman_device *device,
713 const char *ssid, unsigned int ssid_len,
714 const char *identity, const char* passphrase)
716 struct wifi_data *wifi = connman_device_get_data(device);
717 GSupplicantScanParams *scan_params = NULL;
718 struct hidden_params *hidden;
721 DBG("hidden SSID %s", ssid);
723 if (wifi->tethering == TRUE || wifi->hidden != NULL)
726 if (ssid == NULL || ssid_len == 0 || ssid_len > 32)
729 scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
730 if (scan_params == NULL)
732 memcpy(scan_params->ssids[0].ssid, ssid, ssid_len);
733 scan_params->ssids[0].ssid_len = ssid_len;
734 scan_params->num_ssids = 1;
736 hidden = g_try_new0(struct hidden_params, 1);
737 if (hidden == NULL) {
741 memcpy(hidden->ssid, ssid, ssid_len);
742 hidden->ssid_len = ssid_len;
743 hidden->identity = g_strdup(identity);
744 hidden->passphrase = g_strdup(passphrase);
745 wifi->hidden = hidden;
747 stop_autoscan(device);
749 connman_device_ref(device);
750 ret = g_supplicant_interface_scan(wifi->interface, scan_params,
751 scan_callback, device);
753 connman_device_set_scanning(device, TRUE);
755 connman_device_unref(device);
757 hidden_free(wifi->hidden);
764 static struct connman_device_driver wifi_ng_driver = {
766 .type = CONNMAN_DEVICE_TYPE_WIFI,
767 .priority = CONNMAN_DEVICE_PRIORITY_LOW,
769 .remove = wifi_remove,
770 .enable = wifi_enable,
771 .disable = wifi_disable,
773 .scan_fast = wifi_scan_fast,
774 .scan_hidden = wifi_scan_hidden,
777 static void system_ready(void)
781 if (connman_device_driver_register(&wifi_ng_driver) < 0)
782 connman_error("Failed to register WiFi driver");
785 static void system_killed(void)
789 connman_device_driver_unregister(&wifi_ng_driver);
792 static int network_probe(struct connman_network *network)
794 DBG("network %p", network);
799 static void network_remove(struct connman_network *network)
801 struct connman_device *device = connman_network_get_device(network);
802 struct wifi_data *wifi;
804 DBG("network %p", network);
806 wifi = connman_device_get_data(device);
810 if (wifi->network != network)
813 wifi->network = NULL;
816 static void connect_callback(int result, GSupplicantInterface *interface,
819 struct connman_network *network = user_data;
821 DBG("network %p result %d", network, result);
823 if (result == -ENOKEY) {
824 connman_network_set_error(network,
825 CONNMAN_NETWORK_ERROR_INVALID_KEY);
826 } else if (result < 0) {
827 connman_network_set_error(network,
828 CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
832 static GSupplicantSecurity network_security(const char *security)
834 if (g_str_equal(security, "none") == TRUE)
835 return G_SUPPLICANT_SECURITY_NONE;
836 else if (g_str_equal(security, "wep") == TRUE)
837 return G_SUPPLICANT_SECURITY_WEP;
838 else if (g_str_equal(security, "psk") == TRUE)
839 return G_SUPPLICANT_SECURITY_PSK;
840 else if (g_str_equal(security, "wpa") == TRUE)
841 return G_SUPPLICANT_SECURITY_PSK;
842 else if (g_str_equal(security, "rsn") == TRUE)
843 return G_SUPPLICANT_SECURITY_PSK;
844 else if (g_str_equal(security, "ieee8021x") == TRUE)
845 return G_SUPPLICANT_SECURITY_IEEE8021X;
847 return G_SUPPLICANT_SECURITY_UNKNOWN;
850 static void ssid_init(GSupplicantSSID *ssid, struct connman_network *network)
852 const char *security, *passphrase, *agent_passphrase;
854 memset(ssid, 0, sizeof(*ssid));
855 ssid->mode = G_SUPPLICANT_MODE_INFRA;
856 ssid->ssid = connman_network_get_blob(network, "WiFi.SSID",
859 security = connman_network_get_string(network, "WiFi.Security");
860 ssid->security = network_security(security);
861 passphrase = connman_network_get_string(network,
863 if (passphrase == NULL || strlen(passphrase) == 0) {
865 /* Use agent provided passphrase as a fallback */
866 agent_passphrase = connman_network_get_string(network,
867 "WiFi.AgentPassphrase");
869 if (agent_passphrase == NULL || strlen(agent_passphrase) == 0)
870 ssid->passphrase = NULL;
872 ssid->passphrase = agent_passphrase;
874 ssid->passphrase = passphrase;
876 ssid->eap = connman_network_get_string(network, "WiFi.EAP");
879 * If our private key password is unset,
880 * we use the supplied passphrase. That is needed
881 * for PEAP where 2 passphrases (identity and client
882 * cert may have to be provided.
884 if (connman_network_get_string(network,
885 "WiFi.PrivateKeyPassphrase") == NULL)
886 connman_network_set_string(network,
887 "WiFi.PrivateKeyPassphrase",
889 /* We must have an identity for both PEAP and TLS */
890 ssid->identity = connman_network_get_string(network, "WiFi.Identity");
892 /* Use agent provided identity as a fallback */
893 if (ssid->identity == NULL || strlen(ssid->identity) == 0)
894 ssid->identity = connman_network_get_string(network,
895 "WiFi.AgentIdentity");
897 ssid->ca_cert_path = connman_network_get_string(network,
899 ssid->client_cert_path = connman_network_get_string(network,
900 "WiFi.ClientCertFile");
901 ssid->private_key_path = connman_network_get_string(network,
902 "WiFi.PrivateKeyFile");
903 ssid->private_key_passphrase = connman_network_get_string(network,
904 "WiFi.PrivateKeyPassphrase");
905 ssid->phase2_auth = connman_network_get_string(network, "WiFi.Phase2");
907 ssid->use_wps = connman_network_get_bool(network, "WiFi.UseWPS");
908 ssid->pin_wps = connman_network_get_string(network, "WiFi.PinWPS");
910 if (connman_setting_get_bool("BackgroundScanning") == TRUE)
911 ssid->bgscan = BGSCAN_DEFAULT;
914 static int network_connect(struct connman_network *network)
916 struct connman_device *device = connman_network_get_device(network);
917 struct wifi_data *wifi;
918 GSupplicantInterface *interface;
919 GSupplicantSSID *ssid;
921 DBG("network %p", network);
926 wifi = connman_device_get_data(device);
930 ssid = g_try_malloc0(sizeof(GSupplicantSSID));
934 interface = wifi->interface;
936 ssid_init(ssid, network);
938 if (wifi->disconnecting == TRUE)
939 wifi->pending_network = network;
941 wifi->network = network;
944 return g_supplicant_interface_connect(interface, ssid,
945 connect_callback, network);
951 static void disconnect_callback(int result, GSupplicantInterface *interface,
954 struct wifi_data *wifi = user_data;
956 if (wifi->network != NULL) {
958 * if result < 0 supplican return an error because
959 * the network is not current.
960 * we wont receive G_SUPPLICANT_STATE_DISCONNECTED since it
961 * failed, call connman_network_set_connected to report
962 * disconnect is completed.
965 connman_network_set_connected(wifi->network, FALSE);
968 wifi->network = NULL;
970 wifi->disconnecting = FALSE;
972 if (wifi->pending_network != NULL) {
973 network_connect(wifi->pending_network);
974 wifi->pending_network = NULL;
977 start_autoscan(wifi->device);
980 static int network_disconnect(struct connman_network *network)
982 struct connman_device *device = connman_network_get_device(network);
983 struct wifi_data *wifi;
986 DBG("network %p", network);
988 wifi = connman_device_get_data(device);
989 if (wifi == NULL || wifi->interface == NULL)
992 connman_network_set_associating(network, FALSE);
994 if (wifi->disconnecting == TRUE)
997 wifi->disconnecting = TRUE;
999 err = g_supplicant_interface_disconnect(wifi->interface,
1000 disconnect_callback, wifi);
1002 wifi->disconnecting = FALSE;
1007 static struct connman_network_driver network_driver = {
1009 .type = CONNMAN_NETWORK_TYPE_WIFI,
1010 .priority = CONNMAN_NETWORK_PRIORITY_LOW,
1011 .probe = network_probe,
1012 .remove = network_remove,
1013 .connect = network_connect,
1014 .disconnect = network_disconnect,
1017 static void interface_added(GSupplicantInterface *interface)
1019 const char *ifname = g_supplicant_interface_get_ifname(interface);
1020 const char *driver = g_supplicant_interface_get_driver(interface);
1021 struct wifi_data *wifi;
1023 wifi = g_supplicant_interface_get_data(interface);
1026 * We can get here with a NULL wifi pointer when
1027 * the interface added signal is sent before the
1028 * interface creation callback is called.
1033 DBG("ifname %s driver %s wifi %p tethering %d",
1034 ifname, driver, wifi, wifi->tethering);
1036 if (wifi->device == NULL) {
1037 connman_error("WiFi device not set");
1041 connman_device_set_powered(wifi->device, TRUE);
1043 if (wifi->tethering == TRUE)
1047 static connman_bool_t is_idle(struct wifi_data *wifi)
1049 DBG("state %d", wifi->state);
1051 switch (wifi->state) {
1052 case G_SUPPLICANT_STATE_UNKNOWN:
1053 case G_SUPPLICANT_STATE_DISCONNECTED:
1054 case G_SUPPLICANT_STATE_INACTIVE:
1055 case G_SUPPLICANT_STATE_SCANNING:
1058 case G_SUPPLICANT_STATE_AUTHENTICATING:
1059 case G_SUPPLICANT_STATE_ASSOCIATING:
1060 case G_SUPPLICANT_STATE_ASSOCIATED:
1061 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
1062 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
1063 case G_SUPPLICANT_STATE_COMPLETED:
1070 static connman_bool_t is_idle_wps(GSupplicantInterface *interface,
1071 struct wifi_data *wifi)
1073 /* First, let's check if WPS processing did not went wrong */
1074 if (g_supplicant_interface_get_wps_state(interface) ==
1075 G_SUPPLICANT_WPS_STATE_FAIL)
1078 /* Unlike normal connection, being associated while processing wps
1079 * actually means that we are idling. */
1080 switch (wifi->state) {
1081 case G_SUPPLICANT_STATE_UNKNOWN:
1082 case G_SUPPLICANT_STATE_DISCONNECTED:
1083 case G_SUPPLICANT_STATE_INACTIVE:
1084 case G_SUPPLICANT_STATE_SCANNING:
1085 case G_SUPPLICANT_STATE_ASSOCIATED:
1087 case G_SUPPLICANT_STATE_AUTHENTICATING:
1088 case G_SUPPLICANT_STATE_ASSOCIATING:
1089 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
1090 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
1091 case G_SUPPLICANT_STATE_COMPLETED:
1098 static connman_bool_t handle_wps_completion(GSupplicantInterface *interface,
1099 struct connman_network *network,
1100 struct connman_device *device,
1101 struct wifi_data *wifi)
1105 wps = connman_network_get_bool(network, "WiFi.UseWPS");
1107 const unsigned char *ssid, *wps_ssid;
1108 unsigned int ssid_len, wps_ssid_len;
1109 const char *wps_key;
1111 /* Checking if we got associated with requested
1113 ssid = connman_network_get_blob(network, "WiFi.SSID",
1116 wps_ssid = g_supplicant_interface_get_wps_ssid(
1117 interface, &wps_ssid_len);
1119 if (wps_ssid == NULL || wps_ssid_len != ssid_len ||
1120 memcmp(ssid, wps_ssid, ssid_len) != 0) {
1121 connman_network_set_associating(network, FALSE);
1122 g_supplicant_interface_disconnect(wifi->interface,
1123 disconnect_callback, wifi);
1127 wps_key = g_supplicant_interface_get_wps_key(interface);
1128 connman_network_set_string(network, "WiFi.Passphrase",
1131 connman_network_set_string(network, "WiFi.PinWPS", NULL);
1137 static connman_bool_t handle_4way_handshake_failure(GSupplicantInterface *interface,
1138 struct connman_network *network,
1139 struct wifi_data *wifi)
1141 if (wifi->state != G_SUPPLICANT_STATE_4WAY_HANDSHAKE)
1146 if (wifi->retries < MAXIMUM_RETRIES)
1149 connman_network_set_error(network, CONNMAN_NETWORK_ERROR_INVALID_KEY);
1154 static void interface_state(GSupplicantInterface *interface)
1156 struct connman_network *network;
1157 struct connman_device *device;
1158 struct wifi_data *wifi;
1159 GSupplicantState state = g_supplicant_interface_get_state(interface);
1162 wifi = g_supplicant_interface_get_data(interface);
1164 DBG("wifi %p interface state %d", wifi, state);
1169 network = wifi->network;
1170 device = wifi->device;
1172 if (device == NULL || network == NULL)
1176 case G_SUPPLICANT_STATE_SCANNING:
1179 case G_SUPPLICANT_STATE_AUTHENTICATING:
1180 case G_SUPPLICANT_STATE_ASSOCIATING:
1181 stop_autoscan(device);
1183 if (wifi->connected == FALSE)
1184 connman_network_set_associating(network, TRUE);
1188 case G_SUPPLICANT_STATE_COMPLETED:
1189 /* though it should be already stopped: */
1190 stop_autoscan(device);
1192 if (handle_wps_completion(interface, network, device, wifi) ==
1196 connman_network_set_connected(network, TRUE);
1199 case G_SUPPLICANT_STATE_DISCONNECTED:
1201 * If we're in one of the idle modes, we have
1202 * not started association yet and thus setting
1203 * those ones to FALSE could cancel an association
1206 wps = connman_network_get_bool(network, "WiFi.UseWPS");
1208 if (is_idle_wps(interface, wifi) == TRUE)
1214 /* If previous state was 4way-handshake, then
1215 * it's either: psk was incorrect and thus we retry
1216 * or if we reach the maximum retries we declare the
1218 if (handle_4way_handshake_failure(interface,
1219 network, wifi) == TRUE)
1222 /* We disable the selected network, if not then
1223 * wpa_supplicant will loop retrying */
1224 if (g_supplicant_interface_enable_selected_network(interface,
1226 DBG("Could not disables selected network");
1228 connman_network_set_connected(network, FALSE);
1229 connman_network_set_associating(network, FALSE);
1231 start_autoscan(device);
1235 case G_SUPPLICANT_STATE_INACTIVE:
1236 connman_network_set_associating(network, FALSE);
1237 start_autoscan(device);
1241 case G_SUPPLICANT_STATE_UNKNOWN:
1242 case G_SUPPLICANT_STATE_ASSOCIATED:
1243 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
1244 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
1248 wifi->state = state;
1250 /* Saving wpa_s state policy:
1251 * If connected and if the state changes are roaming related:
1252 * --> We stay connected
1254 * --> We are connected
1256 * --> We are not connected
1259 case G_SUPPLICANT_STATE_AUTHENTICATING:
1260 case G_SUPPLICANT_STATE_ASSOCIATING:
1261 case G_SUPPLICANT_STATE_ASSOCIATED:
1262 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
1263 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
1264 if (wifi->connected == TRUE)
1265 connman_warn("Probably roaming right now!"
1266 " Staying connected...");
1268 wifi->connected = FALSE;
1270 case G_SUPPLICANT_STATE_COMPLETED:
1271 wifi->connected = TRUE;
1274 wifi->connected = FALSE;
1281 static void interface_removed(GSupplicantInterface *interface)
1283 const char *ifname = g_supplicant_interface_get_ifname(interface);
1284 struct wifi_data *wifi;
1286 DBG("ifname %s", ifname);
1288 wifi = g_supplicant_interface_get_data(interface);
1290 if (wifi != NULL && wifi->tethering == TRUE)
1293 if (wifi == NULL || wifi->device == NULL) {
1294 connman_error("Wrong wifi pointer");
1298 wifi->interface = NULL;
1299 connman_device_set_powered(wifi->device, FALSE);
1302 static void scan_started(GSupplicantInterface *interface)
1307 static void scan_finished(GSupplicantInterface *interface)
1312 static unsigned char calculate_strength(GSupplicantNetwork *supplicant_network)
1314 unsigned char strength;
1316 strength = 120 + g_supplicant_network_get_signal(supplicant_network);
1323 static void network_added(GSupplicantNetwork *supplicant_network)
1325 struct connman_network *network;
1326 GSupplicantInterface *interface;
1327 struct wifi_data *wifi;
1328 const char *name, *identifier, *security, *group, *mode;
1329 const unsigned char *ssid;
1330 unsigned int ssid_len;
1332 connman_bool_t wps_pbc;
1333 connman_bool_t wps_ready;
1334 connman_bool_t wps_advertizing;
1338 interface = g_supplicant_network_get_interface(supplicant_network);
1339 wifi = g_supplicant_interface_get_data(interface);
1340 name = g_supplicant_network_get_name(supplicant_network);
1341 identifier = g_supplicant_network_get_identifier(supplicant_network);
1342 security = g_supplicant_network_get_security(supplicant_network);
1343 group = g_supplicant_network_get_identifier(supplicant_network);
1344 wps = g_supplicant_network_get_wps(supplicant_network);
1345 wps_pbc = g_supplicant_network_is_wps_pbc(supplicant_network);
1346 wps_ready = g_supplicant_network_is_wps_active(supplicant_network);
1347 wps_advertizing = g_supplicant_network_is_wps_advertizing(
1348 supplicant_network);
1349 mode = g_supplicant_network_get_mode(supplicant_network);
1354 ssid = g_supplicant_network_get_ssid(supplicant_network, &ssid_len);
1356 network = connman_device_get_network(wifi->device, identifier);
1358 if (network == NULL) {
1359 network = connman_network_create(identifier,
1360 CONNMAN_NETWORK_TYPE_WIFI);
1361 if (network == NULL)
1364 connman_network_set_index(network, wifi->index);
1366 if (connman_device_add_network(wifi->device, network) < 0) {
1367 connman_network_unref(network);
1371 wifi->networks = g_slist_append(wifi->networks, network);
1374 if (name != NULL && name[0] != '\0')
1375 connman_network_set_name(network, name);
1377 connman_network_set_blob(network, "WiFi.SSID",
1379 connman_network_set_string(network, "WiFi.Security", security);
1380 connman_network_set_strength(network,
1381 calculate_strength(supplicant_network));
1382 connman_network_set_bool(network, "WiFi.WPS", wps);
1385 /* Is AP advertizing for WPS association?
1386 * If so, we decide to use WPS by default */
1387 if (wps_ready == TRUE && wps_pbc == TRUE &&
1388 wps_advertizing == TRUE)
1389 connman_network_set_bool(network, "WiFi.UseWPS", TRUE);
1392 connman_network_set_frequency(network,
1393 g_supplicant_network_get_frequency(supplicant_network));
1395 connman_network_set_available(network, TRUE);
1396 connman_network_set_string(network, "WiFi.Mode", mode);
1399 connman_network_set_group(network, group);
1401 if (wifi->hidden != NULL) {
1402 if (wifi->hidden->ssid_len == ssid_len &&
1403 memcmp(wifi->hidden->ssid, ssid,
1405 connman_network_connect_hidden(network,
1406 wifi->hidden->identity,
1407 wifi->hidden->passphrase);
1408 hidden_free(wifi->hidden);
1409 wifi->hidden = NULL;
1414 static void network_removed(GSupplicantNetwork *network)
1416 GSupplicantInterface *interface;
1417 struct wifi_data *wifi;
1418 const char *name, *identifier;
1419 struct connman_network *connman_network;
1421 interface = g_supplicant_network_get_interface(network);
1422 wifi = g_supplicant_interface_get_data(interface);
1423 identifier = g_supplicant_network_get_identifier(network);
1424 name = g_supplicant_network_get_name(network);
1426 DBG("name %s", name);
1431 connman_network = connman_device_get_network(wifi->device, identifier);
1432 if (connman_network == NULL)
1435 wifi->networks = g_slist_remove(wifi->networks, connman_network);
1437 connman_device_remove_network(wifi->device, connman_network);
1438 connman_network_unref(connman_network);
1441 static void network_changed(GSupplicantNetwork *network, const char *property)
1443 GSupplicantInterface *interface;
1444 struct wifi_data *wifi;
1445 const char *name, *identifier;
1446 struct connman_network *connman_network;
1448 interface = g_supplicant_network_get_interface(network);
1449 wifi = g_supplicant_interface_get_data(interface);
1450 identifier = g_supplicant_network_get_identifier(network);
1451 name = g_supplicant_network_get_name(network);
1453 DBG("name %s", name);
1458 connman_network = connman_device_get_network(wifi->device, identifier);
1459 if (connman_network == NULL)
1462 if (g_str_equal(property, "Signal") == TRUE) {
1463 connman_network_set_strength(connman_network,
1464 calculate_strength(network));
1465 connman_network_update(connman_network);
1469 static void debug(const char *str)
1471 if (getenv("CONNMAN_SUPPLICANT_DEBUG"))
1472 connman_debug("%s", str);
1475 static const GSupplicantCallbacks callbacks = {
1476 .system_ready = system_ready,
1477 .system_killed = system_killed,
1478 .interface_added = interface_added,
1479 .interface_state = interface_state,
1480 .interface_removed = interface_removed,
1481 .scan_started = scan_started,
1482 .scan_finished = scan_finished,
1483 .network_added = network_added,
1484 .network_removed = network_removed,
1485 .network_changed = network_changed,
1490 static int tech_probe(struct connman_technology *technology)
1492 wifi_technology = technology;
1497 static void tech_remove(struct connman_technology *technology)
1499 wifi_technology = NULL;
1502 struct wifi_tethering_info {
1503 struct wifi_data *wifi;
1504 struct connman_technology *technology;
1506 GSupplicantSSID *ssid;
1509 static GSupplicantSSID *ssid_ap_init(const char *ssid, const char *passphrase)
1511 GSupplicantSSID *ap;
1513 ap = g_try_malloc0(sizeof(GSupplicantSSID));
1517 ap->mode = G_SUPPLICANT_MODE_MASTER;
1519 ap->ssid_len = strlen(ssid);
1523 if (passphrase == NULL || strlen(passphrase) == 0) {
1524 ap->security = G_SUPPLICANT_SECURITY_NONE;
1525 ap->passphrase = NULL;
1527 ap->security = G_SUPPLICANT_SECURITY_PSK;
1528 ap->protocol = G_SUPPLICANT_PROTO_RSN;
1529 ap->pairwise_cipher = G_SUPPLICANT_PAIRWISE_CCMP;
1530 ap->group_cipher = G_SUPPLICANT_GROUP_CCMP;
1531 ap->passphrase = passphrase;
1537 static void ap_start_callback(int result, GSupplicantInterface *interface,
1540 struct wifi_tethering_info *info = user_data;
1542 DBG("result %d index %d bridge %s",
1543 result, info->wifi->index, info->wifi->bridge);
1546 connman_inet_remove_from_bridge(info->wifi->index,
1547 info->wifi->bridge);
1548 connman_technology_tethering_notify(info->technology, FALSE);
1551 g_free(info->ifname);
1555 static void ap_create_callback(int result,
1556 GSupplicantInterface *interface,
1559 struct wifi_tethering_info *info = user_data;
1561 DBG("result %d ifname %s", result,
1562 g_supplicant_interface_get_ifname(interface));
1565 connman_inet_remove_from_bridge(info->wifi->index,
1566 info->wifi->bridge);
1567 connman_technology_tethering_notify(info->technology, FALSE);
1569 g_free(info->ifname);
1574 info->wifi->interface = interface;
1575 g_supplicant_interface_set_data(interface, info->wifi);
1577 if (g_supplicant_interface_set_apscan(interface, 2) < 0)
1578 connman_error("Failed to set interface ap_scan property");
1580 g_supplicant_interface_connect(interface, info->ssid,
1581 ap_start_callback, info);
1584 static void sta_remove_callback(int result,
1585 GSupplicantInterface *interface,
1588 struct wifi_tethering_info *info = user_data;
1589 const char *driver = connman_option_get_string("wifi");
1591 DBG("ifname %s result %d ", info->ifname, result);
1594 info->wifi->tethering = TRUE;
1596 g_free(info->ifname);
1601 info->wifi->interface = NULL;
1603 connman_technology_tethering_notify(info->technology, TRUE);
1605 g_supplicant_interface_create(info->ifname, driver, info->wifi->bridge,
1610 static int tech_set_tethering(struct connman_technology *technology,
1611 const char *identifier, const char *passphrase,
1612 const char *bridge, connman_bool_t enabled)
1615 GSupplicantInterface *interface;
1616 struct wifi_data *wifi;
1617 struct wifi_tethering_info *info;
1624 if (enabled == FALSE) {
1625 for (list = iface_list; list; list = list->next) {
1628 if (wifi->tethering == TRUE) {
1629 wifi->tethering = FALSE;
1631 connman_inet_remove_from_bridge(wifi->index,
1633 wifi->bridged = FALSE;
1637 connman_technology_tethering_notify(technology, FALSE);
1642 for (list = iface_list; list; list = list->next) {
1645 interface = wifi->interface;
1647 if (interface == NULL)
1650 ifname = g_supplicant_interface_get_ifname(wifi->interface);
1652 mode = g_supplicant_interface_get_mode(interface);
1653 if ((mode & G_SUPPLICANT_CAPABILITY_MODE_AP) == 0) {
1654 DBG("%s does not support AP mode", ifname);
1658 info = g_try_malloc0(sizeof(struct wifi_tethering_info));
1663 info->technology = technology;
1664 info->wifi->bridge = bridge;
1665 info->ssid = ssid_ap_init(identifier, passphrase);
1666 if (info->ssid == NULL) {
1670 info->ifname = g_strdup(ifname);
1671 if (info->ifname == NULL) {
1676 info->wifi->tethering = TRUE;
1678 err = g_supplicant_interface_remove(interface,
1679 sta_remove_callback,
1688 static void regdom_callback(void *user_data)
1690 char *alpha2 = user_data;
1694 if (wifi_technology == NULL)
1697 connman_technology_regdom_notify(wifi_technology, alpha2);
1700 static int tech_set_regdom(struct connman_technology *technology, const char *alpha2)
1702 return g_supplicant_set_country(alpha2, regdom_callback, alpha2);
1705 static struct connman_technology_driver tech_driver = {
1707 .type = CONNMAN_SERVICE_TYPE_WIFI,
1708 .probe = tech_probe,
1709 .remove = tech_remove,
1710 .set_tethering = tech_set_tethering,
1711 .set_regdom = tech_set_regdom,
1714 static int wifi_init(void)
1718 err = connman_network_driver_register(&network_driver);
1722 err = g_supplicant_register(&callbacks);
1724 connman_network_driver_unregister(&network_driver);
1728 err = connman_technology_driver_register(&tech_driver);
1730 g_supplicant_unregister(&callbacks);
1731 connman_network_driver_unregister(&network_driver);
1738 static void wifi_exit(void)
1742 connman_technology_driver_unregister(&tech_driver);
1744 g_supplicant_unregister(&callbacks);
1746 connman_network_driver_unregister(&network_driver);
1749 CONNMAN_PLUGIN_DEFINE(wifi, "WiFi interface plugin", VERSION,
1750 CONNMAN_PLUGIN_PRIORITY_DEFAULT, wifi_init, wifi_exit)