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 static 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 add_scan_param(gchar *hex_ssid, int freq,
256 GSupplicantScanParams *scan_data,
257 int driver_max_scan_ssids)
260 struct scan_ssid *scan_ssid;
262 if (driver_max_scan_ssids > scan_data->num_ssids && hex_ssid != NULL) {
264 unsigned int j = 0, hex;
265 size_t hex_ssid_len = strlen(hex_ssid);
267 ssid = g_try_malloc0(hex_ssid_len / 2);
271 for (i = 0; i < hex_ssid_len; i += 2) {
272 sscanf(hex_ssid + i, "%02x", &hex);
276 scan_ssid = g_try_new(struct scan_ssid, 1);
277 if (scan_ssid == NULL) {
282 memcpy(scan_ssid->ssid, ssid, j);
283 scan_ssid->ssid_len = j;
284 scan_data->ssids = g_slist_prepend(scan_data->ssids,
287 scan_data->num_ssids++;
293 scan_data->ssids = g_slist_reverse(scan_data->ssids);
295 if (scan_data->freqs == NULL) {
296 scan_data->freqs = g_try_malloc0(sizeof(uint16_t) *
297 scan_data->num_ssids);
298 if (scan_data->freqs == NULL) {
299 g_slist_free_full(scan_data->ssids, g_free);
303 scan_data->freqs = g_try_realloc(scan_data->freqs,
304 sizeof(uint16_t) * scan_data->num_ssids);
305 if (scan_data->freqs == NULL) {
306 g_slist_free_full(scan_data->ssids, g_free);
309 scan_data->freqs[scan_data->num_ssids - 1] = 0;
312 /* Don't add duplicate entries */
313 for (i = 0; i < scan_data->num_ssids; i++) {
314 if (scan_data->freqs[i] == 0) {
315 scan_data->freqs[i] = freq;
317 } else if (scan_data->freqs[i] == freq)
324 static int get_hidden_connections(int max_ssids,
325 GSupplicantScanParams *scan_data)
333 int num_ssids = 0, add_param_failed = 0;
335 services = connman_storage_get_services();
336 for (i = 0; services && services[i]; i++) {
337 if (strncmp(services[i], "wifi_", 5) != 0)
340 keyfile = connman_storage_load_service(services[i]);
342 value = g_key_file_get_boolean(keyfile,
343 services[i], "Hidden", NULL);
344 if (value == FALSE) {
345 g_key_file_free(keyfile);
349 value = g_key_file_get_boolean(keyfile,
350 services[i], "Favorite", NULL);
351 if (value == FALSE) {
352 g_key_file_free(keyfile);
356 value = g_key_file_get_boolean(keyfile,
357 services[i], "AutoConnect", NULL);
358 if (value == FALSE) {
359 g_key_file_free(keyfile);
363 ssid = g_key_file_get_string(keyfile,
364 services[i], "SSID", NULL);
366 freq = g_key_file_get_integer(keyfile, services[i],
369 if (add_scan_param(ssid, freq, scan_data, max_ssids) < 0) {
370 str = g_key_file_get_string(keyfile,
371 services[i], "Name", NULL);
372 DBG("Cannot scan %s (%s)", ssid, str);
379 g_key_file_free(keyfile);
382 if (add_param_failed > 0)
383 connman_warn("Unable to scan %d out of %d SSIDs (max is %d)",
384 add_param_failed, num_ssids, max_ssids);
386 g_strfreev(services);
388 return num_ssids > max_ssids ? max_ssids : num_ssids;
391 static int throw_wifi_scan(struct connman_device *device,
392 GSupplicantInterfaceCallback callback)
394 struct wifi_data *wifi = connman_device_get_data(device);
397 DBG("device %p %p", device, wifi->interface);
399 if (wifi->tethering == TRUE)
402 connman_device_ref(device);
404 ret = g_supplicant_interface_scan(wifi->interface, NULL,
407 connman_device_set_scanning(device, TRUE);
409 connman_device_unref(device);
414 static void hidden_scan_callback(int result,
415 GSupplicantInterface *interface, void *user_data)
417 struct connman_device *device = user_data;
419 DBG("result %d", result);
421 connman_device_set_scanning(device, FALSE);
422 connman_device_unref(device);
425 static void autoscan_scan_callback(int result,
426 GSupplicantInterface *interface, void *user_data)
428 struct connman_device *device = user_data;
429 struct wifi_data *wifi = connman_device_get_data(device);
430 int driver_max_ssids;
432 DBG("result %d", result);
435 * Scan hidden networks so that we can autoconnect to them.
437 driver_max_ssids = g_supplicant_interface_get_max_scan_ssids(
439 DBG("max ssids %d", driver_max_ssids);
441 if (driver_max_ssids > 0) {
442 GSupplicantScanParams *scan_params;
445 scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
446 if (scan_params == NULL)
449 if (get_hidden_connections(driver_max_ssids,
451 ret = g_supplicant_interface_scan(wifi->interface,
453 hidden_scan_callback,
459 g_supplicant_free_scan_params(scan_params);
463 connman_device_set_scanning(device, FALSE);
464 connman_device_unref(device);
467 static gboolean autoscan_timeout(gpointer data)
469 struct connman_device *device = data;
470 struct wifi_data *wifi = connman_device_get_data(device);
471 struct autoscan_params *autoscan;
474 autoscan = wifi->autoscan;
476 if (autoscan->interval <= 0) {
477 interval = autoscan->base;
480 interval = autoscan->interval * autoscan->base;
482 if (autoscan->interval >= autoscan->limit)
483 interval = autoscan->limit;
485 throw_wifi_scan(wifi->device, autoscan_scan_callback);
488 DBG("interval %d", interval);
490 autoscan->interval = interval;
492 autoscan->timeout = g_timeout_add_seconds(interval,
493 autoscan_timeout, device);
498 static void start_autoscan(struct connman_device *device)
500 struct wifi_data *wifi = connman_device_get_data(device);
501 struct autoscan_params *autoscan;
508 autoscan = wifi->autoscan;
509 if (autoscan == NULL)
512 if (autoscan->timeout > 0 || autoscan->interval > 0)
515 connman_device_ref(device);
517 autoscan_timeout(device);
520 static struct autoscan_params *parse_autoscan_params(const char *params)
522 struct autoscan_params *autoscan;
527 DBG("Emulating autoscan");
529 list_params = g_strsplit(params, ":", 0);
530 if (list_params == 0)
533 if (g_strv_length(list_params) < 3) {
534 g_strfreev(list_params);
538 base = atoi(list_params[1]);
539 limit = atoi(list_params[2]);
541 g_strfreev(list_params);
543 autoscan = g_try_malloc0(sizeof(struct autoscan_params));
544 if (autoscan == NULL) {
545 DBG("Could not allocate memory for autoscan");
549 DBG("base %d - limit %d", base, limit);
550 autoscan->base = base;
551 autoscan->limit = limit;
556 static void setup_autoscan(struct wifi_data *wifi)
558 if (wifi->autoscan == NULL)
559 wifi->autoscan = parse_autoscan_params(AUTOSCAN_DEFAULT);
561 start_autoscan(wifi->device);
564 static void interface_create_callback(int result,
565 GSupplicantInterface *interface,
568 struct wifi_data *wifi = user_data;
570 DBG("result %d ifname %s, wifi %p", result,
571 g_supplicant_interface_get_ifname(interface),
574 if (result < 0 || wifi == NULL)
577 wifi->interface = interface;
578 g_supplicant_interface_set_data(interface, wifi);
580 if (g_supplicant_interface_get_ready(interface) == FALSE)
583 DBG("interface is ready wifi %p tethering %d", wifi, wifi->tethering);
585 if (wifi->device == NULL) {
586 connman_error("WiFi device not set");
590 connman_device_set_powered(wifi->device, TRUE);
592 if (connman_setting_get_bool("BackgroundScanning") == FALSE)
595 /* Setting up automatic scanning */
596 setup_autoscan(wifi);
599 static int wifi_enable(struct connman_device *device)
601 struct wifi_data *wifi = connman_device_get_data(device);
602 const char *interface = connman_device_get_string(device, "Interface");
603 const char *driver = connman_option_get_string("wifi");
606 DBG("device %p %p", device, wifi);
608 ret = g_supplicant_interface_create(interface, driver, NULL,
609 interface_create_callback,
617 static int wifi_disable(struct connman_device *device)
619 struct wifi_data *wifi = connman_device_get_data(device);
622 DBG("device %p", device);
624 wifi->connected = FALSE;
625 wifi->disconnecting = FALSE;
627 if (wifi->pending_network != NULL)
628 wifi->pending_network = NULL;
630 remove_networks(device, wifi);
632 ret = g_supplicant_interface_remove(wifi->interface, NULL, NULL);
639 static void hidden_free(struct hidden_params *hidden)
644 g_free(hidden->identity);
645 g_free(hidden->passphrase);
649 static void scan_callback(int result, GSupplicantInterface *interface,
652 struct connman_device *device = user_data;
653 struct wifi_data *wifi = connman_device_get_data(device);
655 DBG("result %d", result);
657 if (wifi != NULL && wifi->hidden != NULL) {
658 hidden_free(wifi->hidden);
663 connman_device_reset_scanning(device);
665 connman_device_set_scanning(device, FALSE);
666 connman_device_unref(device);
668 start_autoscan(device);
671 struct last_connected {
677 static gint sort_entry(gconstpointer a, gconstpointer b, gpointer user_data)
679 GTimeVal *aval = (GTimeVal *)a;
680 GTimeVal *bval = (GTimeVal *)b;
682 /* Note that the sort order is descending */
683 if (aval->tv_sec < bval->tv_sec)
686 if (aval->tv_sec > bval->tv_sec)
692 static void free_entry(gpointer data)
694 struct last_connected *entry = data;
700 static int get_latest_connections(int max_ssids,
701 GSupplicantScanParams *scan_data)
704 GSequence *latest_list;
705 struct last_connected *entry;
714 latest_list = g_sequence_new(free_entry);
715 if (latest_list == NULL)
718 services = connman_storage_get_services();
719 for (i = 0; services && services[i]; i++) {
720 if (strncmp(services[i], "wifi_", 5) != 0)
723 keyfile = connman_storage_load_service(services[i]);
725 str = g_key_file_get_string(keyfile,
726 services[i], "Favorite", NULL);
727 if (str == NULL || g_strcmp0(str, "true")) {
730 g_key_file_free(keyfile);
735 str = g_key_file_get_string(keyfile,
736 services[i], "AutoConnect", NULL);
737 if (str == NULL || g_strcmp0(str, "true")) {
740 g_key_file_free(keyfile);
745 str = g_key_file_get_string(keyfile,
746 services[i], "Modified", NULL);
748 g_time_val_from_iso8601(str, &modified);
752 ssid = g_key_file_get_string(keyfile,
753 services[i], "SSID", NULL);
755 freq = g_key_file_get_integer(keyfile, services[i],
758 entry = g_try_new(struct last_connected, 1);
760 g_sequence_free(latest_list);
761 g_key_file_free(keyfile);
767 entry->modified = modified;
770 g_sequence_insert_sorted(latest_list, entry,
776 g_key_file_free(keyfile);
779 g_strfreev(services);
781 num_ssids = num_ssids > max_ssids ? max_ssids : num_ssids;
783 iter = g_sequence_get_begin_iter(latest_list);
785 for (i = 0; i < num_ssids; i++) {
786 entry = g_sequence_get(iter);
788 DBG("ssid %s freq %d modified %lu", entry->ssid, entry->freq,
789 entry->modified.tv_sec);
791 add_scan_param(entry->ssid, entry->freq, scan_data, max_ssids);
793 iter = g_sequence_iter_next(iter);
796 g_sequence_free(latest_list);
800 static int wifi_scan(struct connman_device *device)
802 stop_autoscan(device);
804 return throw_wifi_scan(device, scan_callback);
807 static int wifi_scan_fast(struct connman_device *device)
809 struct wifi_data *wifi = connman_device_get_data(device);
810 GSupplicantScanParams *scan_params = NULL;
812 int driver_max_ssids = 0;
814 DBG("device %p %p", device, wifi->interface);
816 if (wifi->tethering == TRUE)
819 driver_max_ssids = g_supplicant_interface_get_max_scan_ssids(
821 DBG("max ssids %d", driver_max_ssids);
822 if (driver_max_ssids == 0)
823 return wifi_scan(device);
825 scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
826 if (scan_params == NULL)
829 ret = get_latest_connections(driver_max_ssids, scan_params);
831 g_supplicant_free_scan_params(scan_params);
832 return wifi_scan(device);
835 stop_autoscan(device);
837 connman_device_ref(device);
838 ret = g_supplicant_interface_scan(wifi->interface, scan_params,
839 scan_callback, device);
841 connman_device_set_scanning(device, TRUE);
843 g_supplicant_free_scan_params(scan_params);
844 connman_device_unref(device);
851 * This func is only used when connecting to this specific AP first time.
852 * It is not used when system autoconnects to hidden AP.
854 static int wifi_scan_hidden(struct connman_device *device,
855 const char *ssid, unsigned int ssid_len,
856 const char *identity, const char* passphrase)
858 struct wifi_data *wifi = connman_device_get_data(device);
859 GSupplicantScanParams *scan_params = NULL;
860 struct scan_ssid *scan_ssid;
861 struct hidden_params *hidden;
864 DBG("hidden SSID %s", ssid);
866 if (wifi->tethering == TRUE || wifi->hidden != NULL)
869 if (ssid == NULL || ssid_len == 0 || ssid_len > 32)
872 scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
873 if (scan_params == NULL)
876 scan_ssid = g_try_new(struct scan_ssid, 1);
877 if (scan_ssid == NULL) {
882 memcpy(scan_ssid->ssid, ssid, ssid_len);
883 scan_ssid->ssid_len = ssid_len;
884 scan_params->ssids = g_slist_prepend(scan_params->ssids, scan_ssid);
886 scan_params->num_ssids = 1;
888 hidden = g_try_new0(struct hidden_params, 1);
889 if (hidden == NULL) {
893 memcpy(hidden->ssid, ssid, ssid_len);
894 hidden->ssid_len = ssid_len;
895 hidden->identity = g_strdup(identity);
896 hidden->passphrase = g_strdup(passphrase);
897 wifi->hidden = hidden;
899 stop_autoscan(device);
901 connman_device_ref(device);
902 ret = g_supplicant_interface_scan(wifi->interface, scan_params,
903 scan_callback, device);
905 connman_device_set_scanning(device, TRUE);
907 connman_device_unref(device);
908 g_supplicant_free_scan_params(scan_params);
909 hidden_free(wifi->hidden);
916 static struct connman_device_driver wifi_ng_driver = {
918 .type = CONNMAN_DEVICE_TYPE_WIFI,
919 .priority = CONNMAN_DEVICE_PRIORITY_LOW,
921 .remove = wifi_remove,
922 .enable = wifi_enable,
923 .disable = wifi_disable,
925 .scan_fast = wifi_scan_fast,
926 .scan_hidden = wifi_scan_hidden,
929 static void system_ready(void)
933 if (connman_device_driver_register(&wifi_ng_driver) < 0)
934 connman_error("Failed to register WiFi driver");
937 static void system_killed(void)
941 connman_device_driver_unregister(&wifi_ng_driver);
944 static int network_probe(struct connman_network *network)
946 DBG("network %p", network);
951 static void network_remove(struct connman_network *network)
953 struct connman_device *device = connman_network_get_device(network);
954 struct wifi_data *wifi;
956 DBG("network %p", network);
958 wifi = connman_device_get_data(device);
962 if (wifi->network != network)
965 wifi->network = NULL;
968 static void connect_callback(int result, GSupplicantInterface *interface,
971 struct connman_network *network = user_data;
973 DBG("network %p result %d", network, result);
975 if (result == -ENOKEY) {
976 connman_network_set_error(network,
977 CONNMAN_NETWORK_ERROR_INVALID_KEY);
978 } else if (result < 0) {
979 connman_network_set_error(network,
980 CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
984 static GSupplicantSecurity network_security(const char *security)
986 if (g_str_equal(security, "none") == TRUE)
987 return G_SUPPLICANT_SECURITY_NONE;
988 else if (g_str_equal(security, "wep") == TRUE)
989 return G_SUPPLICANT_SECURITY_WEP;
990 else if (g_str_equal(security, "psk") == TRUE)
991 return G_SUPPLICANT_SECURITY_PSK;
992 else if (g_str_equal(security, "wpa") == TRUE)
993 return G_SUPPLICANT_SECURITY_PSK;
994 else if (g_str_equal(security, "rsn") == TRUE)
995 return G_SUPPLICANT_SECURITY_PSK;
996 else if (g_str_equal(security, "ieee8021x") == TRUE)
997 return G_SUPPLICANT_SECURITY_IEEE8021X;
999 return G_SUPPLICANT_SECURITY_UNKNOWN;
1002 static void ssid_init(GSupplicantSSID *ssid, struct connman_network *network)
1004 const char *security, *passphrase, *agent_passphrase;
1006 memset(ssid, 0, sizeof(*ssid));
1007 ssid->mode = G_SUPPLICANT_MODE_INFRA;
1008 ssid->ssid = connman_network_get_blob(network, "WiFi.SSID",
1010 ssid->scan_ssid = 1;
1011 security = connman_network_get_string(network, "WiFi.Security");
1012 ssid->security = network_security(security);
1013 passphrase = connman_network_get_string(network,
1015 if (passphrase == NULL || strlen(passphrase) == 0) {
1017 /* Use agent provided passphrase as a fallback */
1018 agent_passphrase = connman_network_get_string(network,
1019 "WiFi.AgentPassphrase");
1021 if (agent_passphrase == NULL || strlen(agent_passphrase) == 0)
1022 ssid->passphrase = NULL;
1024 ssid->passphrase = agent_passphrase;
1026 ssid->passphrase = passphrase;
1028 ssid->eap = connman_network_get_string(network, "WiFi.EAP");
1031 * If our private key password is unset,
1032 * we use the supplied passphrase. That is needed
1033 * for PEAP where 2 passphrases (identity and client
1034 * cert may have to be provided.
1036 if (connman_network_get_string(network,
1037 "WiFi.PrivateKeyPassphrase") == NULL)
1038 connman_network_set_string(network,
1039 "WiFi.PrivateKeyPassphrase",
1041 /* We must have an identity for both PEAP and TLS */
1042 ssid->identity = connman_network_get_string(network, "WiFi.Identity");
1044 /* Use agent provided identity as a fallback */
1045 if (ssid->identity == NULL || strlen(ssid->identity) == 0)
1046 ssid->identity = connman_network_get_string(network,
1047 "WiFi.AgentIdentity");
1049 ssid->ca_cert_path = connman_network_get_string(network,
1051 ssid->client_cert_path = connman_network_get_string(network,
1052 "WiFi.ClientCertFile");
1053 ssid->private_key_path = connman_network_get_string(network,
1054 "WiFi.PrivateKeyFile");
1055 ssid->private_key_passphrase = connman_network_get_string(network,
1056 "WiFi.PrivateKeyPassphrase");
1057 ssid->phase2_auth = connman_network_get_string(network, "WiFi.Phase2");
1059 ssid->use_wps = connman_network_get_bool(network, "WiFi.UseWPS");
1060 ssid->pin_wps = connman_network_get_string(network, "WiFi.PinWPS");
1062 if (connman_setting_get_bool("BackgroundScanning") == TRUE)
1063 ssid->bgscan = BGSCAN_DEFAULT;
1066 static int network_connect(struct connman_network *network)
1068 struct connman_device *device = connman_network_get_device(network);
1069 struct wifi_data *wifi;
1070 GSupplicantInterface *interface;
1071 GSupplicantSSID *ssid;
1073 DBG("network %p", network);
1078 wifi = connman_device_get_data(device);
1082 ssid = g_try_malloc0(sizeof(GSupplicantSSID));
1086 interface = wifi->interface;
1088 ssid_init(ssid, network);
1090 if (wifi->disconnecting == TRUE)
1091 wifi->pending_network = network;
1093 wifi->network = network;
1096 return g_supplicant_interface_connect(interface, ssid,
1097 connect_callback, network);
1100 return -EINPROGRESS;
1103 static void disconnect_callback(int result, GSupplicantInterface *interface,
1106 struct wifi_data *wifi = user_data;
1108 if (wifi->network != NULL) {
1110 * if result < 0 supplican return an error because
1111 * the network is not current.
1112 * we wont receive G_SUPPLICANT_STATE_DISCONNECTED since it
1113 * failed, call connman_network_set_connected to report
1114 * disconnect is completed.
1117 connman_network_set_connected(wifi->network, FALSE);
1120 wifi->network = NULL;
1122 wifi->disconnecting = FALSE;
1124 if (wifi->pending_network != NULL) {
1125 network_connect(wifi->pending_network);
1126 wifi->pending_network = NULL;
1129 start_autoscan(wifi->device);
1132 static int network_disconnect(struct connman_network *network)
1134 struct connman_device *device = connman_network_get_device(network);
1135 struct wifi_data *wifi;
1138 DBG("network %p", network);
1140 wifi = connman_device_get_data(device);
1141 if (wifi == NULL || wifi->interface == NULL)
1144 connman_network_set_associating(network, FALSE);
1146 if (wifi->disconnecting == TRUE)
1149 wifi->disconnecting = TRUE;
1151 err = g_supplicant_interface_disconnect(wifi->interface,
1152 disconnect_callback, wifi);
1154 wifi->disconnecting = FALSE;
1159 static struct connman_network_driver network_driver = {
1161 .type = CONNMAN_NETWORK_TYPE_WIFI,
1162 .priority = CONNMAN_NETWORK_PRIORITY_LOW,
1163 .probe = network_probe,
1164 .remove = network_remove,
1165 .connect = network_connect,
1166 .disconnect = network_disconnect,
1169 static void interface_added(GSupplicantInterface *interface)
1171 const char *ifname = g_supplicant_interface_get_ifname(interface);
1172 const char *driver = g_supplicant_interface_get_driver(interface);
1173 struct wifi_data *wifi;
1175 wifi = g_supplicant_interface_get_data(interface);
1178 * We can get here with a NULL wifi pointer when
1179 * the interface added signal is sent before the
1180 * interface creation callback is called.
1185 DBG("ifname %s driver %s wifi %p tethering %d",
1186 ifname, driver, wifi, wifi->tethering);
1188 if (wifi->device == NULL) {
1189 connman_error("WiFi device not set");
1193 connman_device_set_powered(wifi->device, TRUE);
1195 if (wifi->tethering == TRUE)
1199 static connman_bool_t is_idle(struct wifi_data *wifi)
1201 DBG("state %d", wifi->state);
1203 switch (wifi->state) {
1204 case G_SUPPLICANT_STATE_UNKNOWN:
1205 case G_SUPPLICANT_STATE_DISCONNECTED:
1206 case G_SUPPLICANT_STATE_INACTIVE:
1207 case G_SUPPLICANT_STATE_SCANNING:
1210 case G_SUPPLICANT_STATE_AUTHENTICATING:
1211 case G_SUPPLICANT_STATE_ASSOCIATING:
1212 case G_SUPPLICANT_STATE_ASSOCIATED:
1213 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
1214 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
1215 case G_SUPPLICANT_STATE_COMPLETED:
1222 static connman_bool_t is_idle_wps(GSupplicantInterface *interface,
1223 struct wifi_data *wifi)
1225 /* First, let's check if WPS processing did not went wrong */
1226 if (g_supplicant_interface_get_wps_state(interface) ==
1227 G_SUPPLICANT_WPS_STATE_FAIL)
1230 /* Unlike normal connection, being associated while processing wps
1231 * actually means that we are idling. */
1232 switch (wifi->state) {
1233 case G_SUPPLICANT_STATE_UNKNOWN:
1234 case G_SUPPLICANT_STATE_DISCONNECTED:
1235 case G_SUPPLICANT_STATE_INACTIVE:
1236 case G_SUPPLICANT_STATE_SCANNING:
1237 case G_SUPPLICANT_STATE_ASSOCIATED:
1239 case G_SUPPLICANT_STATE_AUTHENTICATING:
1240 case G_SUPPLICANT_STATE_ASSOCIATING:
1241 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
1242 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
1243 case G_SUPPLICANT_STATE_COMPLETED:
1250 static connman_bool_t handle_wps_completion(GSupplicantInterface *interface,
1251 struct connman_network *network,
1252 struct connman_device *device,
1253 struct wifi_data *wifi)
1257 wps = connman_network_get_bool(network, "WiFi.UseWPS");
1259 const unsigned char *ssid, *wps_ssid;
1260 unsigned int ssid_len, wps_ssid_len;
1261 const char *wps_key;
1263 /* Checking if we got associated with requested
1265 ssid = connman_network_get_blob(network, "WiFi.SSID",
1268 wps_ssid = g_supplicant_interface_get_wps_ssid(
1269 interface, &wps_ssid_len);
1271 if (wps_ssid == NULL || wps_ssid_len != ssid_len ||
1272 memcmp(ssid, wps_ssid, ssid_len) != 0) {
1273 connman_network_set_associating(network, FALSE);
1274 g_supplicant_interface_disconnect(wifi->interface,
1275 disconnect_callback, wifi);
1279 wps_key = g_supplicant_interface_get_wps_key(interface);
1280 connman_network_set_string(network, "WiFi.Passphrase",
1283 connman_network_set_string(network, "WiFi.PinWPS", NULL);
1289 static connman_bool_t handle_4way_handshake_failure(GSupplicantInterface *interface,
1290 struct connman_network *network,
1291 struct wifi_data *wifi)
1293 if (wifi->state != G_SUPPLICANT_STATE_4WAY_HANDSHAKE)
1298 if (wifi->retries < MAXIMUM_RETRIES)
1301 connman_network_set_error(network, CONNMAN_NETWORK_ERROR_INVALID_KEY);
1306 static void interface_state(GSupplicantInterface *interface)
1308 struct connman_network *network;
1309 struct connman_device *device;
1310 struct wifi_data *wifi;
1311 GSupplicantState state = g_supplicant_interface_get_state(interface);
1314 wifi = g_supplicant_interface_get_data(interface);
1316 DBG("wifi %p interface state %d", wifi, state);
1321 network = wifi->network;
1322 device = wifi->device;
1324 if (device == NULL || network == NULL)
1328 case G_SUPPLICANT_STATE_SCANNING:
1331 case G_SUPPLICANT_STATE_AUTHENTICATING:
1332 case G_SUPPLICANT_STATE_ASSOCIATING:
1333 stop_autoscan(device);
1335 if (wifi->connected == FALSE)
1336 connman_network_set_associating(network, TRUE);
1340 case G_SUPPLICANT_STATE_COMPLETED:
1341 /* though it should be already stopped: */
1342 stop_autoscan(device);
1344 if (handle_wps_completion(interface, network, device, wifi) ==
1348 connman_network_set_connected(network, TRUE);
1351 case G_SUPPLICANT_STATE_DISCONNECTED:
1353 * If we're in one of the idle modes, we have
1354 * not started association yet and thus setting
1355 * those ones to FALSE could cancel an association
1358 wps = connman_network_get_bool(network, "WiFi.UseWPS");
1360 if (is_idle_wps(interface, wifi) == TRUE)
1366 /* If previous state was 4way-handshake, then
1367 * it's either: psk was incorrect and thus we retry
1368 * or if we reach the maximum retries we declare the
1370 if (handle_4way_handshake_failure(interface,
1371 network, wifi) == TRUE)
1374 /* We disable the selected network, if not then
1375 * wpa_supplicant will loop retrying */
1376 if (g_supplicant_interface_enable_selected_network(interface,
1378 DBG("Could not disables selected network");
1380 connman_network_set_connected(network, FALSE);
1381 connman_network_set_associating(network, FALSE);
1382 wifi->disconnecting = FALSE;
1384 start_autoscan(device);
1388 case G_SUPPLICANT_STATE_INACTIVE:
1389 connman_network_set_associating(network, FALSE);
1390 start_autoscan(device);
1394 case G_SUPPLICANT_STATE_UNKNOWN:
1395 case G_SUPPLICANT_STATE_ASSOCIATED:
1396 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
1397 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
1401 wifi->state = state;
1403 /* Saving wpa_s state policy:
1404 * If connected and if the state changes are roaming related:
1405 * --> We stay connected
1407 * --> We are connected
1409 * --> We are not connected
1412 case G_SUPPLICANT_STATE_AUTHENTICATING:
1413 case G_SUPPLICANT_STATE_ASSOCIATING:
1414 case G_SUPPLICANT_STATE_ASSOCIATED:
1415 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
1416 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
1417 if (wifi->connected == TRUE)
1418 connman_warn("Probably roaming right now!"
1419 " Staying connected...");
1421 wifi->connected = FALSE;
1423 case G_SUPPLICANT_STATE_COMPLETED:
1424 wifi->connected = TRUE;
1427 wifi->connected = FALSE;
1434 static void interface_removed(GSupplicantInterface *interface)
1436 const char *ifname = g_supplicant_interface_get_ifname(interface);
1437 struct wifi_data *wifi;
1439 DBG("ifname %s", ifname);
1441 wifi = g_supplicant_interface_get_data(interface);
1443 if (wifi != NULL && wifi->tethering == TRUE)
1446 if (wifi == NULL || wifi->device == NULL) {
1447 connman_error("Wrong wifi pointer");
1451 wifi->interface = NULL;
1452 connman_device_set_powered(wifi->device, FALSE);
1455 static void scan_started(GSupplicantInterface *interface)
1460 static void scan_finished(GSupplicantInterface *interface)
1465 static unsigned char calculate_strength(GSupplicantNetwork *supplicant_network)
1467 unsigned char strength;
1469 strength = 120 + g_supplicant_network_get_signal(supplicant_network);
1476 static void network_added(GSupplicantNetwork *supplicant_network)
1478 struct connman_network *network;
1479 GSupplicantInterface *interface;
1480 struct wifi_data *wifi;
1481 const char *name, *identifier, *security, *group, *mode;
1482 const unsigned char *ssid;
1483 unsigned int ssid_len;
1485 connman_bool_t wps_pbc;
1486 connman_bool_t wps_ready;
1487 connman_bool_t wps_advertizing;
1491 interface = g_supplicant_network_get_interface(supplicant_network);
1492 wifi = g_supplicant_interface_get_data(interface);
1493 name = g_supplicant_network_get_name(supplicant_network);
1494 identifier = g_supplicant_network_get_identifier(supplicant_network);
1495 security = g_supplicant_network_get_security(supplicant_network);
1496 group = g_supplicant_network_get_identifier(supplicant_network);
1497 wps = g_supplicant_network_get_wps(supplicant_network);
1498 wps_pbc = g_supplicant_network_is_wps_pbc(supplicant_network);
1499 wps_ready = g_supplicant_network_is_wps_active(supplicant_network);
1500 wps_advertizing = g_supplicant_network_is_wps_advertizing(
1501 supplicant_network);
1502 mode = g_supplicant_network_get_mode(supplicant_network);
1507 ssid = g_supplicant_network_get_ssid(supplicant_network, &ssid_len);
1509 network = connman_device_get_network(wifi->device, identifier);
1511 if (network == NULL) {
1512 network = connman_network_create(identifier,
1513 CONNMAN_NETWORK_TYPE_WIFI);
1514 if (network == NULL)
1517 connman_network_set_index(network, wifi->index);
1519 if (connman_device_add_network(wifi->device, network) < 0) {
1520 connman_network_unref(network);
1524 wifi->networks = g_slist_append(wifi->networks, network);
1527 if (name != NULL && name[0] != '\0')
1528 connman_network_set_name(network, name);
1530 connman_network_set_blob(network, "WiFi.SSID",
1532 connman_network_set_string(network, "WiFi.Security", security);
1533 connman_network_set_strength(network,
1534 calculate_strength(supplicant_network));
1535 connman_network_set_bool(network, "WiFi.WPS", wps);
1538 /* Is AP advertizing for WPS association?
1539 * If so, we decide to use WPS by default */
1540 if (wps_ready == TRUE && wps_pbc == TRUE &&
1541 wps_advertizing == TRUE)
1542 connman_network_set_bool(network, "WiFi.UseWPS", TRUE);
1545 connman_network_set_frequency(network,
1546 g_supplicant_network_get_frequency(supplicant_network));
1548 connman_network_set_available(network, TRUE);
1549 connman_network_set_string(network, "WiFi.Mode", mode);
1552 connman_network_set_group(network, group);
1554 if (wifi->hidden != NULL && ssid != NULL) {
1555 if (wifi->hidden->ssid_len == ssid_len &&
1556 memcmp(wifi->hidden->ssid, ssid,
1558 connman_network_connect_hidden(network,
1559 wifi->hidden->identity,
1560 wifi->hidden->passphrase);
1561 hidden_free(wifi->hidden);
1562 wifi->hidden = NULL;
1567 static void network_removed(GSupplicantNetwork *network)
1569 GSupplicantInterface *interface;
1570 struct wifi_data *wifi;
1571 const char *name, *identifier;
1572 struct connman_network *connman_network;
1574 interface = g_supplicant_network_get_interface(network);
1575 wifi = g_supplicant_interface_get_data(interface);
1576 identifier = g_supplicant_network_get_identifier(network);
1577 name = g_supplicant_network_get_name(network);
1579 DBG("name %s", name);
1584 connman_network = connman_device_get_network(wifi->device, identifier);
1585 if (connman_network == NULL)
1588 wifi->networks = g_slist_remove(wifi->networks, connman_network);
1590 connman_device_remove_network(wifi->device, connman_network);
1591 connman_network_unref(connman_network);
1594 static void network_changed(GSupplicantNetwork *network, const char *property)
1596 GSupplicantInterface *interface;
1597 struct wifi_data *wifi;
1598 const char *name, *identifier;
1599 struct connman_network *connman_network;
1601 interface = g_supplicant_network_get_interface(network);
1602 wifi = g_supplicant_interface_get_data(interface);
1603 identifier = g_supplicant_network_get_identifier(network);
1604 name = g_supplicant_network_get_name(network);
1606 DBG("name %s", name);
1611 connman_network = connman_device_get_network(wifi->device, identifier);
1612 if (connman_network == NULL)
1615 if (g_str_equal(property, "Signal") == TRUE) {
1616 connman_network_set_strength(connman_network,
1617 calculate_strength(network));
1618 connman_network_update(connman_network);
1622 static void debug(const char *str)
1624 if (getenv("CONNMAN_SUPPLICANT_DEBUG"))
1625 connman_debug("%s", str);
1628 static const GSupplicantCallbacks callbacks = {
1629 .system_ready = system_ready,
1630 .system_killed = system_killed,
1631 .interface_added = interface_added,
1632 .interface_state = interface_state,
1633 .interface_removed = interface_removed,
1634 .scan_started = scan_started,
1635 .scan_finished = scan_finished,
1636 .network_added = network_added,
1637 .network_removed = network_removed,
1638 .network_changed = network_changed,
1643 static int tech_probe(struct connman_technology *technology)
1645 wifi_technology = technology;
1650 static void tech_remove(struct connman_technology *technology)
1652 wifi_technology = NULL;
1655 struct wifi_tethering_info {
1656 struct wifi_data *wifi;
1657 struct connman_technology *technology;
1659 GSupplicantSSID *ssid;
1662 static GSupplicantSSID *ssid_ap_init(const char *ssid, const char *passphrase)
1664 GSupplicantSSID *ap;
1666 ap = g_try_malloc0(sizeof(GSupplicantSSID));
1670 ap->mode = G_SUPPLICANT_MODE_MASTER;
1672 ap->ssid_len = strlen(ssid);
1676 if (passphrase == NULL || strlen(passphrase) == 0) {
1677 ap->security = G_SUPPLICANT_SECURITY_NONE;
1678 ap->passphrase = NULL;
1680 ap->security = G_SUPPLICANT_SECURITY_PSK;
1681 ap->protocol = G_SUPPLICANT_PROTO_RSN;
1682 ap->pairwise_cipher = G_SUPPLICANT_PAIRWISE_CCMP;
1683 ap->group_cipher = G_SUPPLICANT_GROUP_CCMP;
1684 ap->passphrase = passphrase;
1690 static void ap_start_callback(int result, GSupplicantInterface *interface,
1693 struct wifi_tethering_info *info = user_data;
1695 DBG("result %d index %d bridge %s",
1696 result, info->wifi->index, info->wifi->bridge);
1699 connman_inet_remove_from_bridge(info->wifi->index,
1700 info->wifi->bridge);
1701 connman_technology_tethering_notify(info->technology, FALSE);
1704 g_free(info->ifname);
1708 static void ap_create_callback(int result,
1709 GSupplicantInterface *interface,
1712 struct wifi_tethering_info *info = user_data;
1714 DBG("result %d ifname %s", result,
1715 g_supplicant_interface_get_ifname(interface));
1718 connman_inet_remove_from_bridge(info->wifi->index,
1719 info->wifi->bridge);
1720 connman_technology_tethering_notify(info->technology, FALSE);
1722 g_free(info->ifname);
1727 info->wifi->interface = interface;
1728 g_supplicant_interface_set_data(interface, info->wifi);
1730 if (g_supplicant_interface_set_apscan(interface, 2) < 0)
1731 connman_error("Failed to set interface ap_scan property");
1733 g_supplicant_interface_connect(interface, info->ssid,
1734 ap_start_callback, info);
1737 static void sta_remove_callback(int result,
1738 GSupplicantInterface *interface,
1741 struct wifi_tethering_info *info = user_data;
1742 const char *driver = connman_option_get_string("wifi");
1744 DBG("ifname %s result %d ", info->ifname, result);
1747 info->wifi->tethering = TRUE;
1749 g_free(info->ifname);
1754 info->wifi->interface = NULL;
1756 connman_technology_tethering_notify(info->technology, TRUE);
1758 g_supplicant_interface_create(info->ifname, driver, info->wifi->bridge,
1763 static int tech_set_tethering(struct connman_technology *technology,
1764 const char *identifier, const char *passphrase,
1765 const char *bridge, connman_bool_t enabled)
1768 GSupplicantInterface *interface;
1769 struct wifi_data *wifi;
1770 struct wifi_tethering_info *info;
1777 if (enabled == FALSE) {
1778 for (list = iface_list; list; list = list->next) {
1781 if (wifi->tethering == TRUE) {
1782 wifi->tethering = FALSE;
1784 connman_inet_remove_from_bridge(wifi->index,
1786 wifi->bridged = FALSE;
1790 connman_technology_tethering_notify(technology, FALSE);
1795 for (list = iface_list; list; list = list->next) {
1798 interface = wifi->interface;
1800 if (interface == NULL)
1803 ifname = g_supplicant_interface_get_ifname(wifi->interface);
1805 mode = g_supplicant_interface_get_mode(interface);
1806 if ((mode & G_SUPPLICANT_CAPABILITY_MODE_AP) == 0) {
1807 DBG("%s does not support AP mode", ifname);
1811 info = g_try_malloc0(sizeof(struct wifi_tethering_info));
1816 info->technology = technology;
1817 info->wifi->bridge = bridge;
1818 info->ssid = ssid_ap_init(identifier, passphrase);
1819 if (info->ssid == NULL) {
1823 info->ifname = g_strdup(ifname);
1824 if (info->ifname == NULL) {
1829 info->wifi->tethering = TRUE;
1831 err = g_supplicant_interface_remove(interface,
1832 sta_remove_callback,
1841 static void regdom_callback(void *user_data)
1843 char *alpha2 = user_data;
1847 if (wifi_technology == NULL)
1850 connman_technology_regdom_notify(wifi_technology, alpha2);
1853 static int tech_set_regdom(struct connman_technology *technology, const char *alpha2)
1855 return g_supplicant_set_country(alpha2, regdom_callback, alpha2);
1858 static struct connman_technology_driver tech_driver = {
1860 .type = CONNMAN_SERVICE_TYPE_WIFI,
1861 .probe = tech_probe,
1862 .remove = tech_remove,
1863 .set_tethering = tech_set_tethering,
1864 .set_regdom = tech_set_regdom,
1867 static int wifi_init(void)
1871 err = connman_network_driver_register(&network_driver);
1875 err = g_supplicant_register(&callbacks);
1877 connman_network_driver_unregister(&network_driver);
1881 err = connman_technology_driver_register(&tech_driver);
1883 g_supplicant_unregister(&callbacks);
1884 connman_network_driver_unregister(&network_driver);
1891 static void wifi_exit(void)
1895 connman_technology_driver_unregister(&tech_driver);
1897 g_supplicant_unregister(&callbacks);
1899 connman_network_driver_unregister(&network_driver);
1902 CONNMAN_PLUGIN_DEFINE(wifi, "WiFi interface plugin", VERSION,
1903 CONNMAN_PLUGIN_PRIORITY_DEFAULT, wifi_init, wifi_exit)