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: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 connman_network_set_associating(network, TRUE);
1186 case G_SUPPLICANT_STATE_COMPLETED:
1187 /* though it should be already stopped: */
1188 stop_autoscan(device);
1190 if (handle_wps_completion(interface, network, device, wifi) ==
1194 connman_network_set_connected(network, TRUE);
1197 case G_SUPPLICANT_STATE_DISCONNECTED:
1199 * If we're in one of the idle modes, we have
1200 * not started association yet and thus setting
1201 * those ones to FALSE could cancel an association
1204 wps = connman_network_get_bool(network, "WiFi.UseWPS");
1206 if (is_idle_wps(interface, wifi) == TRUE)
1212 /* If previous state was 4way-handshake, then
1213 * it's either: psk was incorrect and thus we retry
1214 * or if we reach the maximum retries we declare the
1216 if (handle_4way_handshake_failure(interface,
1217 network, wifi) == TRUE)
1220 /* We disable the selected network, if not then
1221 * wpa_supplicant will loop retrying */
1222 if (g_supplicant_interface_enable_selected_network(interface,
1224 DBG("Could not disables selected network");
1226 connman_network_set_associating(network, FALSE);
1227 connman_network_set_connected(network, FALSE);
1229 start_autoscan(device);
1233 case G_SUPPLICANT_STATE_INACTIVE:
1234 connman_network_set_associating(network, FALSE);
1235 start_autoscan(device);
1239 case G_SUPPLICANT_STATE_UNKNOWN:
1240 case G_SUPPLICANT_STATE_ASSOCIATED:
1241 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
1242 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
1246 wifi->state = state;
1251 static void interface_removed(GSupplicantInterface *interface)
1253 const char *ifname = g_supplicant_interface_get_ifname(interface);
1254 struct wifi_data *wifi;
1256 DBG("ifname %s", ifname);
1258 wifi = g_supplicant_interface_get_data(interface);
1260 if (wifi != NULL && wifi->tethering == TRUE)
1263 if (wifi == NULL || wifi->device == NULL) {
1264 connman_error("Wrong wifi pointer");
1268 wifi->interface = NULL;
1269 connman_device_set_powered(wifi->device, FALSE);
1272 static void scan_started(GSupplicantInterface *interface)
1277 static void scan_finished(GSupplicantInterface *interface)
1282 static unsigned char calculate_strength(GSupplicantNetwork *supplicant_network)
1284 unsigned char strength;
1286 strength = 120 + g_supplicant_network_get_signal(supplicant_network);
1293 static void network_added(GSupplicantNetwork *supplicant_network)
1295 struct connman_network *network;
1296 GSupplicantInterface *interface;
1297 struct wifi_data *wifi;
1298 const char *name, *identifier, *security, *group, *mode;
1299 const unsigned char *ssid;
1300 unsigned int ssid_len;
1302 connman_bool_t wps_pbc;
1303 connman_bool_t wps_ready;
1304 connman_bool_t wps_advertizing;
1308 interface = g_supplicant_network_get_interface(supplicant_network);
1309 wifi = g_supplicant_interface_get_data(interface);
1310 name = g_supplicant_network_get_name(supplicant_network);
1311 identifier = g_supplicant_network_get_identifier(supplicant_network);
1312 security = g_supplicant_network_get_security(supplicant_network);
1313 group = g_supplicant_network_get_identifier(supplicant_network);
1314 wps = g_supplicant_network_get_wps(supplicant_network);
1315 wps_pbc = g_supplicant_network_is_wps_pbc(supplicant_network);
1316 wps_ready = g_supplicant_network_is_wps_active(supplicant_network);
1317 wps_advertizing = g_supplicant_network_is_wps_advertizing(
1318 supplicant_network);
1319 mode = g_supplicant_network_get_mode(supplicant_network);
1324 ssid = g_supplicant_network_get_ssid(supplicant_network, &ssid_len);
1326 network = connman_device_get_network(wifi->device, identifier);
1328 if (network == NULL) {
1329 network = connman_network_create(identifier,
1330 CONNMAN_NETWORK_TYPE_WIFI);
1331 if (network == NULL)
1334 connman_network_set_index(network, wifi->index);
1336 if (connman_device_add_network(wifi->device, network) < 0) {
1337 connman_network_unref(network);
1341 wifi->networks = g_slist_append(wifi->networks, network);
1344 if (name != NULL && name[0] != '\0')
1345 connman_network_set_name(network, name);
1347 connman_network_set_blob(network, "WiFi.SSID",
1349 connman_network_set_string(network, "WiFi.Security", security);
1350 connman_network_set_strength(network,
1351 calculate_strength(supplicant_network));
1352 connman_network_set_bool(network, "WiFi.WPS", wps);
1355 /* Is AP advertizing for WPS association?
1356 * If so, we decide to use WPS by default */
1357 if (wps_ready == TRUE && wps_pbc == TRUE &&
1358 wps_advertizing == TRUE)
1359 connman_network_set_bool(network, "WiFi.UseWPS", TRUE);
1362 connman_network_set_frequency(network,
1363 g_supplicant_network_get_frequency(supplicant_network));
1365 connman_network_set_available(network, TRUE);
1366 connman_network_set_string(network, "WiFi.Mode", mode);
1369 connman_network_set_group(network, group);
1371 if (wifi->hidden != NULL) {
1372 if (wifi->hidden->ssid_len == ssid_len &&
1373 memcmp(wifi->hidden->ssid, ssid,
1375 connman_network_connect_hidden(network,
1376 wifi->hidden->identity,
1377 wifi->hidden->passphrase);
1378 hidden_free(wifi->hidden);
1379 wifi->hidden = NULL;
1384 static void network_removed(GSupplicantNetwork *network)
1386 GSupplicantInterface *interface;
1387 struct wifi_data *wifi;
1388 const char *name, *identifier;
1389 struct connman_network *connman_network;
1391 interface = g_supplicant_network_get_interface(network);
1392 wifi = g_supplicant_interface_get_data(interface);
1393 identifier = g_supplicant_network_get_identifier(network);
1394 name = g_supplicant_network_get_name(network);
1396 DBG("name %s", name);
1401 connman_network = connman_device_get_network(wifi->device, identifier);
1402 if (connman_network == NULL)
1405 wifi->networks = g_slist_remove(wifi->networks, connman_network);
1407 connman_device_remove_network(wifi->device, connman_network);
1408 connman_network_unref(connman_network);
1411 static void network_changed(GSupplicantNetwork *network, const char *property)
1413 GSupplicantInterface *interface;
1414 struct wifi_data *wifi;
1415 const char *name, *identifier;
1416 struct connman_network *connman_network;
1418 interface = g_supplicant_network_get_interface(network);
1419 wifi = g_supplicant_interface_get_data(interface);
1420 identifier = g_supplicant_network_get_identifier(network);
1421 name = g_supplicant_network_get_name(network);
1423 DBG("name %s", name);
1428 connman_network = connman_device_get_network(wifi->device, identifier);
1429 if (connman_network == NULL)
1432 if (g_str_equal(property, "Signal") == TRUE) {
1433 connman_network_set_strength(connman_network,
1434 calculate_strength(network));
1435 connman_network_update(connman_network);
1439 static void debug(const char *str)
1441 if (getenv("CONNMAN_SUPPLICANT_DEBUG"))
1442 connman_debug("%s", str);
1445 static const GSupplicantCallbacks callbacks = {
1446 .system_ready = system_ready,
1447 .system_killed = system_killed,
1448 .interface_added = interface_added,
1449 .interface_state = interface_state,
1450 .interface_removed = interface_removed,
1451 .scan_started = scan_started,
1452 .scan_finished = scan_finished,
1453 .network_added = network_added,
1454 .network_removed = network_removed,
1455 .network_changed = network_changed,
1460 static int tech_probe(struct connman_technology *technology)
1462 wifi_technology = technology;
1467 static void tech_remove(struct connman_technology *technology)
1469 wifi_technology = NULL;
1472 struct wifi_tethering_info {
1473 struct wifi_data *wifi;
1474 struct connman_technology *technology;
1476 GSupplicantSSID *ssid;
1479 static GSupplicantSSID *ssid_ap_init(const char *ssid, const char *passphrase)
1481 GSupplicantSSID *ap;
1483 ap = g_try_malloc0(sizeof(GSupplicantSSID));
1487 ap->mode = G_SUPPLICANT_MODE_MASTER;
1489 ap->ssid_len = strlen(ssid);
1493 if (passphrase == NULL || strlen(passphrase) == 0) {
1494 ap->security = G_SUPPLICANT_SECURITY_NONE;
1495 ap->passphrase = NULL;
1497 ap->security = G_SUPPLICANT_SECURITY_PSK;
1498 ap->protocol = G_SUPPLICANT_PROTO_RSN;
1499 ap->pairwise_cipher = G_SUPPLICANT_PAIRWISE_CCMP;
1500 ap->group_cipher = G_SUPPLICANT_GROUP_CCMP;
1501 ap->passphrase = passphrase;
1507 static void ap_start_callback(int result, GSupplicantInterface *interface,
1510 struct wifi_tethering_info *info = user_data;
1512 DBG("result %d index %d bridge %s",
1513 result, info->wifi->index, info->wifi->bridge);
1516 connman_inet_remove_from_bridge(info->wifi->index,
1517 info->wifi->bridge);
1518 connman_technology_tethering_notify(info->technology, FALSE);
1521 g_free(info->ifname);
1525 static void ap_create_callback(int result,
1526 GSupplicantInterface *interface,
1529 struct wifi_tethering_info *info = user_data;
1531 DBG("result %d ifname %s", result,
1532 g_supplicant_interface_get_ifname(interface));
1535 connman_inet_remove_from_bridge(info->wifi->index,
1536 info->wifi->bridge);
1537 connman_technology_tethering_notify(info->technology, FALSE);
1539 g_free(info->ifname);
1544 info->wifi->interface = interface;
1545 g_supplicant_interface_set_data(interface, info->wifi);
1547 if (g_supplicant_interface_set_apscan(interface, 2) < 0)
1548 connman_error("Failed to set interface ap_scan property");
1550 g_supplicant_interface_connect(interface, info->ssid,
1551 ap_start_callback, info);
1554 static void sta_remove_callback(int result,
1555 GSupplicantInterface *interface,
1558 struct wifi_tethering_info *info = user_data;
1559 const char *driver = connman_option_get_string("wifi");
1561 DBG("ifname %s result %d ", info->ifname, result);
1564 info->wifi->tethering = TRUE;
1566 g_free(info->ifname);
1571 info->wifi->interface = NULL;
1573 connman_technology_tethering_notify(info->technology, TRUE);
1575 g_supplicant_interface_create(info->ifname, driver, info->wifi->bridge,
1580 static int tech_set_tethering(struct connman_technology *technology,
1581 const char *identifier, const char *passphrase,
1582 const char *bridge, connman_bool_t enabled)
1585 GSupplicantInterface *interface;
1586 struct wifi_data *wifi;
1587 struct wifi_tethering_info *info;
1594 if (enabled == FALSE) {
1595 for (list = iface_list; list; list = list->next) {
1598 if (wifi->tethering == TRUE) {
1599 wifi->tethering = FALSE;
1601 connman_inet_remove_from_bridge(wifi->index,
1603 wifi->bridged = FALSE;
1607 connman_technology_tethering_notify(technology, FALSE);
1612 for (list = iface_list; list; list = list->next) {
1615 interface = wifi->interface;
1617 if (interface == NULL)
1620 ifname = g_supplicant_interface_get_ifname(wifi->interface);
1622 mode = g_supplicant_interface_get_mode(interface);
1623 if ((mode & G_SUPPLICANT_CAPABILITY_MODE_AP) == 0) {
1624 DBG("%s does not support AP mode", ifname);
1628 info = g_try_malloc0(sizeof(struct wifi_tethering_info));
1633 info->technology = technology;
1634 info->wifi->bridge = bridge;
1635 info->ssid = ssid_ap_init(identifier, passphrase);
1636 if (info->ssid == NULL) {
1640 info->ifname = g_strdup(ifname);
1641 if (info->ifname == NULL) {
1646 info->wifi->tethering = TRUE;
1648 err = g_supplicant_interface_remove(interface,
1649 sta_remove_callback,
1658 static void regdom_callback(void *user_data)
1660 char *alpha2 = user_data;
1664 if (wifi_technology == NULL)
1667 connman_technology_regdom_notify(wifi_technology, alpha2);
1670 static int tech_set_regdom(struct connman_technology *technology, const char *alpha2)
1672 return g_supplicant_set_country(alpha2, regdom_callback, alpha2);
1675 static struct connman_technology_driver tech_driver = {
1677 .type = CONNMAN_SERVICE_TYPE_WIFI,
1678 .probe = tech_probe,
1679 .remove = tech_remove,
1680 .set_tethering = tech_set_tethering,
1681 .set_regdom = tech_set_regdom,
1684 static int wifi_init(void)
1688 err = connman_network_driver_register(&network_driver);
1692 err = g_supplicant_register(&callbacks);
1694 connman_network_driver_unregister(&network_driver);
1698 err = connman_technology_driver_register(&tech_driver);
1700 g_supplicant_unregister(&callbacks);
1701 connman_network_driver_unregister(&network_driver);
1708 static void wifi_exit(void)
1712 connman_technology_driver_unregister(&tech_driver);
1714 g_supplicant_unregister(&callbacks);
1716 connman_network_driver_unregister(&network_driver);
1719 CONNMAN_PLUGIN_DEFINE(wifi, "WiFi interface plugin", VERSION,
1720 CONNMAN_PLUGIN_PRIORITY_DEFAULT, wifi_init, wifi_exit)