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>
54 #include <connman/provision.h>
56 #include <gsupplicant/gsupplicant.h>
58 #define CLEANUP_TIMEOUT 8 /* in seconds */
59 #define INACTIVE_TIMEOUT 12 /* in seconds */
60 #define MAXIMUM_RETRIES 4
62 #define BGSCAN_DEFAULT "simple:30:-45:300"
63 #define AUTOSCAN_DEFAULT "exponential:3:300"
65 static struct connman_technology *wifi_technology = NULL;
67 struct hidden_params {
69 unsigned int ssid_len;
76 * Used for autoscan "emulation".
77 * Should be removed when wpa_s autoscan support will be by default.
79 struct autoscan_params {
88 struct connman_device *device;
89 struct connman_network *network;
90 struct connman_network *pending_network;
92 GSupplicantInterface *interface;
93 GSupplicantState state;
94 connman_bool_t connected;
95 connman_bool_t disconnecting;
96 connman_bool_t tethering;
97 connman_bool_t bridged;
103 struct hidden_params *hidden;
105 * autoscan "emulation".
107 struct autoscan_params *autoscan;
110 static GList *iface_list = NULL;
112 static void start_autoscan(struct connman_device *device);
114 static void handle_tethering(struct wifi_data *wifi)
116 if (wifi->tethering == FALSE)
119 if (wifi->bridge == NULL)
122 if (wifi->bridged == TRUE)
125 DBG("index %d bridge %s", wifi->index, wifi->bridge);
127 if (connman_inet_add_to_bridge(wifi->index, wifi->bridge) < 0)
130 wifi->bridged = TRUE;
133 static void wifi_newlink(unsigned flags, unsigned change, void *user_data)
135 struct connman_device *device = user_data;
136 struct wifi_data *wifi = connman_device_get_data(device);
141 DBG("index %d flags %d change %d", wifi->index, flags, change);
146 if ((wifi->flags & IFF_UP) != (flags & IFF_UP)) {
150 DBG("interface down");
153 if ((wifi->flags & IFF_LOWER_UP) != (flags & IFF_LOWER_UP)) {
154 if (flags & IFF_LOWER_UP) {
157 handle_tethering(wifi);
165 static int wifi_probe(struct connman_device *device)
167 struct wifi_data *wifi;
169 DBG("device %p", device);
171 wifi = g_try_new0(struct wifi_data, 1);
175 wifi->connected = FALSE;
176 wifi->disconnecting = FALSE;
177 wifi->tethering = FALSE;
178 wifi->bridged = FALSE;
180 wifi->state = G_SUPPLICANT_STATE_INACTIVE;
182 connman_device_set_data(device, wifi);
183 wifi->device = connman_device_ref(device);
185 wifi->index = connman_device_get_index(device);
188 wifi->watch = connman_rtnl_add_newlink_watch(wifi->index,
189 wifi_newlink, device);
191 iface_list = g_list_append(iface_list, wifi);
196 static void remove_networks(struct connman_device *device,
197 struct wifi_data *wifi)
201 for (list = wifi->networks; list != NULL; list = list->next) {
202 struct connman_network *network = list->data;
204 connman_device_remove_network(device, network);
205 connman_network_unref(network);
208 g_slist_free(wifi->networks);
209 wifi->networks = NULL;
212 static void reset_autoscan(struct connman_device *device)
214 struct wifi_data *wifi = connman_device_get_data(device);
215 struct autoscan_params *autoscan;
219 if (wifi == NULL || wifi->autoscan == NULL)
222 autoscan = wifi->autoscan;
224 if (autoscan->timeout == 0 && autoscan->interval == 0)
227 g_source_remove(autoscan->timeout);
229 autoscan->timeout = 0;
230 autoscan->interval = 0;
232 connman_device_unref(device);
235 static void stop_autoscan(struct connman_device *device)
237 reset_autoscan(device);
239 connman_device_set_scanning(device, FALSE);
242 static void wifi_remove(struct connman_device *device)
244 struct wifi_data *wifi = connman_device_get_data(device);
246 DBG("device %p wifi %p", device, wifi);
251 iface_list = g_list_remove(iface_list, wifi);
253 remove_networks(device, wifi);
255 connman_device_set_powered(device, FALSE);
256 connman_device_set_data(device, NULL);
257 connman_device_unref(wifi->device);
258 connman_rtnl_remove_watch(wifi->watch);
260 g_supplicant_interface_set_data(wifi->interface, NULL);
262 g_free(wifi->autoscan);
263 g_free(wifi->identifier);
267 static int add_scan_param(gchar *hex_ssid, char *raw_ssid, int ssid_len,
268 int freq, GSupplicantScanParams *scan_data,
269 int driver_max_scan_ssids)
272 struct scan_ssid *scan_ssid;
274 if (driver_max_scan_ssids > scan_data->num_ssids &&
275 (hex_ssid != NULL || raw_ssid != NULL)) {
277 unsigned int j = 0, hex;
279 if (hex_ssid != NULL) {
280 size_t hex_ssid_len = strlen(hex_ssid);
282 ssid = g_try_malloc0(hex_ssid_len / 2);
286 for (i = 0; i < hex_ssid_len; i += 2) {
287 sscanf(hex_ssid + i, "%02x", &hex);
295 scan_ssid = g_try_new(struct scan_ssid, 1);
296 if (scan_ssid == NULL) {
301 memcpy(scan_ssid->ssid, ssid, j);
302 scan_ssid->ssid_len = j;
303 scan_data->ssids = g_slist_prepend(scan_data->ssids,
306 scan_data->num_ssids++;
308 if (hex_ssid != NULL)
313 scan_data->ssids = g_slist_reverse(scan_data->ssids);
315 if (scan_data->freqs == NULL) {
316 scan_data->freqs = g_try_malloc0(sizeof(uint16_t) *
317 scan_data->num_ssids);
318 if (scan_data->freqs == NULL) {
319 g_slist_free_full(scan_data->ssids, g_free);
323 scan_data->freqs = g_try_realloc(scan_data->freqs,
324 sizeof(uint16_t) * scan_data->num_ssids);
325 if (scan_data->freqs == NULL) {
326 g_slist_free_full(scan_data->ssids, g_free);
329 scan_data->freqs[scan_data->num_ssids - 1] = 0;
332 /* Don't add duplicate entries */
333 for (i = 0; i < scan_data->num_ssids; i++) {
334 if (scan_data->freqs[i] == 0) {
335 scan_data->freqs[i] = freq;
337 } else if (scan_data->freqs[i] == freq)
344 static int get_hidden_connections(int max_ssids,
345 GSupplicantScanParams *scan_data)
347 struct connman_config_entry **entries;
354 int num_ssids = 0, add_param_failed = 0;
356 services = connman_storage_get_services();
357 for (i = 0; services && services[i]; i++) {
358 if (strncmp(services[i], "wifi_", 5) != 0)
361 keyfile = connman_storage_load_service(services[i]);
363 value = g_key_file_get_boolean(keyfile,
364 services[i], "Hidden", NULL);
365 if (value == FALSE) {
366 g_key_file_free(keyfile);
370 value = g_key_file_get_boolean(keyfile,
371 services[i], "Favorite", NULL);
372 if (value == FALSE) {
373 g_key_file_free(keyfile);
377 value = g_key_file_get_boolean(keyfile,
378 services[i], "AutoConnect", NULL);
379 if (value == FALSE) {
380 g_key_file_free(keyfile);
384 ssid = g_key_file_get_string(keyfile,
385 services[i], "SSID", NULL);
387 freq = g_key_file_get_integer(keyfile, services[i],
390 if (add_scan_param(ssid, NULL, 0, freq, scan_data,
392 str = g_key_file_get_string(keyfile,
393 services[i], "Name", NULL);
394 DBG("Cannot scan %s (%s)", ssid, str);
401 g_key_file_free(keyfile);
405 * Check if there are any hidden AP that needs to be provisioned.
407 entries = connman_config_get_entries();
408 for (i = 0; entries && entries[i]; i++) {
411 if (entries[i]->hidden == FALSE)
414 if (entries[i]->ssid == NULL) {
415 ssid = entries[i]->name;
418 ssid = entries[i]->ssid;
419 len = entries[i]->ssid_len;
425 DBG("[%d]->ssid = %s", i, ssid);
427 if (add_scan_param(NULL, ssid, len, 0, scan_data,
429 DBG("Cannot scan %s (%s)", ssid, entries[i]->ident);
436 connman_config_free_entries(entries);
438 if (add_param_failed > 0)
439 DBG("Unable to scan %d out of %d SSIDs (max is %d)",
440 add_param_failed, num_ssids, max_ssids);
442 g_strfreev(services);
444 return num_ssids > max_ssids ? max_ssids : num_ssids;
447 static int throw_wifi_scan(struct connman_device *device,
448 GSupplicantInterfaceCallback callback)
450 struct wifi_data *wifi = connman_device_get_data(device);
456 DBG("device %p %p", device, wifi->interface);
458 if (wifi->tethering == TRUE)
461 if (connman_device_get_scanning(device) == TRUE)
464 connman_device_ref(device);
466 ret = g_supplicant_interface_scan(wifi->interface, NULL,
469 connman_device_set_scanning(device, TRUE);
471 connman_device_unref(device);
476 static void hidden_free(struct hidden_params *hidden)
481 g_free(hidden->identity);
482 g_free(hidden->passphrase);
486 static void scan_callback(int result, GSupplicantInterface *interface,
489 struct connman_device *device = user_data;
490 struct wifi_data *wifi = connman_device_get_data(device);
492 DBG("result %d wifi %p", result, wifi);
494 if (wifi != NULL && wifi->hidden != NULL) {
495 connman_network_clear_hidden(wifi->hidden->user_data);
496 hidden_free(wifi->hidden);
501 connman_device_reset_scanning(device);
503 connman_device_set_scanning(device, FALSE);
504 start_autoscan(device);
505 connman_device_unref(device);
508 static void scan_callback_hidden(int result,
509 GSupplicantInterface *interface, void *user_data)
511 struct connman_device *device = user_data;
512 struct wifi_data *wifi = connman_device_get_data(device);
513 int driver_max_ssids;
515 DBG("result %d wifi %p", result, wifi);
521 * Scan hidden networks so that we can autoconnect to them.
523 driver_max_ssids = g_supplicant_interface_get_max_scan_ssids(
525 DBG("max ssids %d", driver_max_ssids);
527 if (driver_max_ssids > 0) {
528 GSupplicantScanParams *scan_params;
531 scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
532 if (scan_params == NULL)
535 if (get_hidden_connections(driver_max_ssids,
537 ret = g_supplicant_interface_scan(wifi->interface,
545 g_supplicant_free_scan_params(scan_params);
549 scan_callback(result, interface, user_data);
552 static gboolean autoscan_timeout(gpointer data)
554 struct connman_device *device = data;
555 struct wifi_data *wifi = connman_device_get_data(device);
556 struct autoscan_params *autoscan;
562 autoscan = wifi->autoscan;
564 if (autoscan->interval <= 0) {
565 interval = autoscan->base;
568 interval = autoscan->interval * autoscan->base;
570 if (autoscan->interval >= autoscan->limit)
571 interval = autoscan->limit;
573 throw_wifi_scan(wifi->device, scan_callback_hidden);
576 DBG("interval %d", interval);
578 autoscan->interval = interval;
580 autoscan->timeout = g_timeout_add_seconds(interval,
581 autoscan_timeout, device);
586 static void start_autoscan(struct connman_device *device)
588 struct wifi_data *wifi = connman_device_get_data(device);
589 struct autoscan_params *autoscan;
596 autoscan = wifi->autoscan;
597 if (autoscan == NULL)
600 if (autoscan->timeout > 0 || autoscan->interval > 0)
603 connman_device_ref(device);
605 autoscan_timeout(device);
608 static struct autoscan_params *parse_autoscan_params(const char *params)
610 struct autoscan_params *autoscan;
615 DBG("Emulating autoscan");
617 list_params = g_strsplit(params, ":", 0);
618 if (list_params == 0)
621 if (g_strv_length(list_params) < 3) {
622 g_strfreev(list_params);
626 base = atoi(list_params[1]);
627 limit = atoi(list_params[2]);
629 g_strfreev(list_params);
631 autoscan = g_try_malloc0(sizeof(struct autoscan_params));
632 if (autoscan == NULL) {
633 DBG("Could not allocate memory for autoscan");
637 DBG("base %d - limit %d", base, limit);
638 autoscan->base = base;
639 autoscan->limit = limit;
644 static void setup_autoscan(struct wifi_data *wifi)
646 if (wifi->autoscan == NULL)
647 wifi->autoscan = parse_autoscan_params(AUTOSCAN_DEFAULT);
649 start_autoscan(wifi->device);
652 static void interface_autoscan_callback(int result,
653 GSupplicantInterface *interface,
656 struct wifi_data *wifi = user_data;
659 DBG("Could not enable Autoscan, falling back...");
660 setup_autoscan(wifi);
664 static void interface_create_callback(int result,
665 GSupplicantInterface *interface,
668 struct wifi_data *wifi = user_data;
670 DBG("result %d ifname %s, wifi %p", result,
671 g_supplicant_interface_get_ifname(interface),
674 if (result < 0 || wifi == NULL)
677 wifi->interface = interface;
678 g_supplicant_interface_set_data(interface, wifi);
680 if (g_supplicant_interface_get_ready(interface) == FALSE)
683 DBG("interface is ready wifi %p tethering %d", wifi, wifi->tethering);
685 if (wifi->device == NULL) {
686 connman_error("WiFi device not set");
690 connman_device_set_powered(wifi->device, TRUE);
692 if (connman_setting_get_bool("BackgroundScanning") == FALSE)
695 /* Setting up automatic scanning */
696 if (g_supplicant_interface_autoscan(interface, AUTOSCAN_DEFAULT,
697 interface_autoscan_callback, wifi) < 0) {
698 DBG("Could not enable Autoscan, falling back...");
699 setup_autoscan(wifi);
703 static int wifi_enable(struct connman_device *device)
705 struct wifi_data *wifi = connman_device_get_data(device);
706 const char *interface = connman_device_get_string(device, "Interface");
707 const char *driver = connman_option_get_string("wifi");
710 DBG("device %p %p", device, wifi);
715 ret = g_supplicant_interface_create(interface, driver, NULL,
716 interface_create_callback,
724 static int wifi_disable(struct connman_device *device)
726 struct wifi_data *wifi = connman_device_get_data(device);
729 DBG("device %p wifi %p", device, wifi);
734 wifi->connected = FALSE;
735 wifi->disconnecting = FALSE;
737 if (wifi->pending_network != NULL)
738 wifi->pending_network = NULL;
740 stop_autoscan(device);
742 /* In case of a user scan, device is still referenced */
743 if (connman_device_get_scanning(device) == TRUE) {
744 connman_device_set_scanning(device, FALSE);
745 connman_device_unref(wifi->device);
748 remove_networks(device, wifi);
750 ret = g_supplicant_interface_remove(wifi->interface, NULL, NULL);
757 struct last_connected {
763 static gint sort_entry(gconstpointer a, gconstpointer b, gpointer user_data)
765 GTimeVal *aval = (GTimeVal *)a;
766 GTimeVal *bval = (GTimeVal *)b;
768 /* Note that the sort order is descending */
769 if (aval->tv_sec < bval->tv_sec)
772 if (aval->tv_sec > bval->tv_sec)
778 static void free_entry(gpointer data)
780 struct last_connected *entry = data;
786 static int get_latest_connections(int max_ssids,
787 GSupplicantScanParams *scan_data)
790 GSequence *latest_list;
791 struct last_connected *entry;
800 latest_list = g_sequence_new(free_entry);
801 if (latest_list == NULL)
804 services = connman_storage_get_services();
805 for (i = 0; services && services[i]; i++) {
806 if (strncmp(services[i], "wifi_", 5) != 0)
809 keyfile = connman_storage_load_service(services[i]);
811 str = g_key_file_get_string(keyfile,
812 services[i], "Favorite", NULL);
813 if (str == NULL || g_strcmp0(str, "true")) {
816 g_key_file_free(keyfile);
821 str = g_key_file_get_string(keyfile,
822 services[i], "AutoConnect", NULL);
823 if (str == NULL || g_strcmp0(str, "true")) {
826 g_key_file_free(keyfile);
831 str = g_key_file_get_string(keyfile,
832 services[i], "Modified", NULL);
834 g_time_val_from_iso8601(str, &modified);
838 ssid = g_key_file_get_string(keyfile,
839 services[i], "SSID", NULL);
841 freq = g_key_file_get_integer(keyfile, services[i],
844 entry = g_try_new(struct last_connected, 1);
846 g_sequence_free(latest_list);
847 g_key_file_free(keyfile);
853 entry->modified = modified;
856 g_sequence_insert_sorted(latest_list, entry,
862 g_key_file_free(keyfile);
865 g_strfreev(services);
867 num_ssids = num_ssids > max_ssids ? max_ssids : num_ssids;
869 iter = g_sequence_get_begin_iter(latest_list);
871 for (i = 0; i < num_ssids; i++) {
872 entry = g_sequence_get(iter);
874 DBG("ssid %s freq %d modified %lu", entry->ssid, entry->freq,
875 entry->modified.tv_sec);
877 add_scan_param(entry->ssid, NULL, 0, entry->freq, scan_data,
880 iter = g_sequence_iter_next(iter);
883 g_sequence_free(latest_list);
887 static int wifi_scan(struct connman_device *device)
889 reset_autoscan(device);
891 return throw_wifi_scan(device, scan_callback_hidden);
894 static int wifi_scan_fast(struct connman_device *device)
896 struct wifi_data *wifi = connman_device_get_data(device);
897 GSupplicantScanParams *scan_params = NULL;
899 int driver_max_ssids = 0;
904 DBG("device %p %p", device, wifi->interface);
906 if (wifi->tethering == TRUE)
909 if (connman_device_get_scanning(device) == TRUE)
912 driver_max_ssids = g_supplicant_interface_get_max_scan_ssids(
914 DBG("max ssids %d", driver_max_ssids);
915 if (driver_max_ssids == 0)
916 return wifi_scan(device);
918 scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
919 if (scan_params == NULL)
922 ret = get_latest_connections(driver_max_ssids, scan_params);
924 g_supplicant_free_scan_params(scan_params);
925 return wifi_scan(device);
928 connman_device_ref(device);
929 reset_autoscan(device);
931 ret = g_supplicant_interface_scan(wifi->interface, scan_params,
932 scan_callback, device);
934 connman_device_set_scanning(device, TRUE);
936 g_supplicant_free_scan_params(scan_params);
937 connman_device_unref(device);
944 * This func is only used when connecting to this specific AP first time.
945 * It is not used when system autoconnects to hidden AP.
947 static int wifi_scan_hidden(struct connman_device *device,
948 const char *ssid, unsigned int ssid_len,
949 const char *identity, const char* passphrase,
952 struct wifi_data *wifi = connman_device_get_data(device);
953 GSupplicantScanParams *scan_params = NULL;
954 struct scan_ssid *scan_ssid;
955 struct hidden_params *hidden;
961 DBG("hidden SSID %s", ssid);
963 if (wifi->tethering == TRUE || wifi->hidden != NULL)
966 if (ssid == NULL || ssid_len == 0 || ssid_len > 32)
969 if (connman_device_get_scanning(device) == TRUE)
972 scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
973 if (scan_params == NULL)
976 scan_ssid = g_try_new(struct scan_ssid, 1);
977 if (scan_ssid == NULL) {
982 memcpy(scan_ssid->ssid, ssid, ssid_len);
983 scan_ssid->ssid_len = ssid_len;
984 scan_params->ssids = g_slist_prepend(scan_params->ssids, scan_ssid);
986 scan_params->num_ssids = 1;
988 hidden = g_try_new0(struct hidden_params, 1);
989 if (hidden == NULL) {
993 memcpy(hidden->ssid, ssid, ssid_len);
994 hidden->ssid_len = ssid_len;
995 hidden->identity = g_strdup(identity);
996 hidden->passphrase = g_strdup(passphrase);
997 hidden->user_data = user_data;
998 wifi->hidden = hidden;
1000 connman_device_ref(device);
1002 reset_autoscan(device);
1004 ret = g_supplicant_interface_scan(wifi->interface, scan_params,
1005 scan_callback, device);
1007 connman_device_set_scanning(device, TRUE);
1009 connman_device_unref(device);
1010 g_supplicant_free_scan_params(scan_params);
1011 hidden_free(wifi->hidden);
1012 wifi->hidden = NULL;
1018 static void wifi_regdom_callback(int result,
1022 struct connman_device *device = user_data;
1024 connman_device_regdom_notify(device, result, alpha2);
1026 connman_device_unref(device);
1029 static int wifi_set_regdom(struct connman_device *device, const char *alpha2)
1031 struct wifi_data *wifi = connman_device_get_data(device);
1037 connman_device_ref(device);
1039 ret = g_supplicant_interface_set_country(wifi->interface,
1040 wifi_regdom_callback,
1043 connman_device_unref(device);
1048 static struct connman_device_driver wifi_ng_driver = {
1050 .type = CONNMAN_DEVICE_TYPE_WIFI,
1051 .priority = CONNMAN_DEVICE_PRIORITY_LOW,
1052 .probe = wifi_probe,
1053 .remove = wifi_remove,
1054 .enable = wifi_enable,
1055 .disable = wifi_disable,
1057 .scan_fast = wifi_scan_fast,
1058 .scan_hidden = wifi_scan_hidden,
1059 .set_regdom = wifi_set_regdom,
1062 static void system_ready(void)
1066 if (connman_device_driver_register(&wifi_ng_driver) < 0)
1067 connman_error("Failed to register WiFi driver");
1070 static void system_killed(void)
1074 connman_device_driver_unregister(&wifi_ng_driver);
1077 static int network_probe(struct connman_network *network)
1079 DBG("network %p", network);
1084 static void network_remove(struct connman_network *network)
1086 struct connman_device *device = connman_network_get_device(network);
1087 struct wifi_data *wifi;
1089 DBG("network %p", network);
1091 wifi = connman_device_get_data(device);
1095 if (wifi->network != network)
1098 wifi->network = NULL;
1101 static void connect_callback(int result, GSupplicantInterface *interface,
1104 struct connman_network *network = user_data;
1106 DBG("network %p result %d", network, result);
1108 if (result == -ENOKEY) {
1109 connman_network_set_error(network,
1110 CONNMAN_NETWORK_ERROR_INVALID_KEY);
1111 } else if (result < 0) {
1112 connman_network_set_error(network,
1113 CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
1117 static GSupplicantSecurity network_security(const char *security)
1119 if (g_str_equal(security, "none") == TRUE)
1120 return G_SUPPLICANT_SECURITY_NONE;
1121 else if (g_str_equal(security, "wep") == TRUE)
1122 return G_SUPPLICANT_SECURITY_WEP;
1123 else if (g_str_equal(security, "psk") == TRUE)
1124 return G_SUPPLICANT_SECURITY_PSK;
1125 else if (g_str_equal(security, "wpa") == TRUE)
1126 return G_SUPPLICANT_SECURITY_PSK;
1127 else if (g_str_equal(security, "rsn") == TRUE)
1128 return G_SUPPLICANT_SECURITY_PSK;
1129 else if (g_str_equal(security, "ieee8021x") == TRUE)
1130 return G_SUPPLICANT_SECURITY_IEEE8021X;
1132 return G_SUPPLICANT_SECURITY_UNKNOWN;
1135 static void ssid_init(GSupplicantSSID *ssid, struct connman_network *network)
1137 const char *security, *passphrase, *agent_passphrase;
1139 memset(ssid, 0, sizeof(*ssid));
1140 ssid->mode = G_SUPPLICANT_MODE_INFRA;
1141 ssid->ssid = connman_network_get_blob(network, "WiFi.SSID",
1143 ssid->scan_ssid = 1;
1144 security = connman_network_get_string(network, "WiFi.Security");
1145 ssid->security = network_security(security);
1146 passphrase = connman_network_get_string(network,
1148 if (passphrase == NULL || strlen(passphrase) == 0) {
1150 /* Use agent provided passphrase as a fallback */
1151 agent_passphrase = connman_network_get_string(network,
1152 "WiFi.AgentPassphrase");
1154 if (agent_passphrase == NULL || strlen(agent_passphrase) == 0)
1155 ssid->passphrase = NULL;
1157 ssid->passphrase = agent_passphrase;
1159 ssid->passphrase = passphrase;
1161 ssid->eap = connman_network_get_string(network, "WiFi.EAP");
1164 * If our private key password is unset,
1165 * we use the supplied passphrase. That is needed
1166 * for PEAP where 2 passphrases (identity and client
1167 * cert may have to be provided.
1169 if (connman_network_get_string(network,
1170 "WiFi.PrivateKeyPassphrase") == NULL)
1171 connman_network_set_string(network,
1172 "WiFi.PrivateKeyPassphrase",
1174 /* We must have an identity for both PEAP and TLS */
1175 ssid->identity = connman_network_get_string(network, "WiFi.Identity");
1177 /* Use agent provided identity as a fallback */
1178 if (ssid->identity == NULL || strlen(ssid->identity) == 0)
1179 ssid->identity = connman_network_get_string(network,
1180 "WiFi.AgentIdentity");
1182 ssid->ca_cert_path = connman_network_get_string(network,
1184 ssid->client_cert_path = connman_network_get_string(network,
1185 "WiFi.ClientCertFile");
1186 ssid->private_key_path = connman_network_get_string(network,
1187 "WiFi.PrivateKeyFile");
1188 ssid->private_key_passphrase = connman_network_get_string(network,
1189 "WiFi.PrivateKeyPassphrase");
1190 ssid->phase2_auth = connman_network_get_string(network, "WiFi.Phase2");
1192 ssid->use_wps = connman_network_get_bool(network, "WiFi.UseWPS");
1193 ssid->pin_wps = connman_network_get_string(network, "WiFi.PinWPS");
1195 if (connman_setting_get_bool("BackgroundScanning") == TRUE)
1196 ssid->bgscan = BGSCAN_DEFAULT;
1199 static int network_connect(struct connman_network *network)
1201 struct connman_device *device = connman_network_get_device(network);
1202 struct wifi_data *wifi;
1203 GSupplicantInterface *interface;
1204 GSupplicantSSID *ssid;
1206 DBG("network %p", network);
1211 wifi = connman_device_get_data(device);
1215 ssid = g_try_malloc0(sizeof(GSupplicantSSID));
1219 interface = wifi->interface;
1221 ssid_init(ssid, network);
1223 if (wifi->disconnecting == TRUE)
1224 wifi->pending_network = network;
1226 wifi->network = network;
1229 return g_supplicant_interface_connect(interface, ssid,
1230 connect_callback, network);
1233 return -EINPROGRESS;
1236 static void disconnect_callback(int result, GSupplicantInterface *interface,
1239 struct wifi_data *wifi = user_data;
1241 if (wifi->network != NULL) {
1243 * if result < 0 supplican return an error because
1244 * the network is not current.
1245 * we wont receive G_SUPPLICANT_STATE_DISCONNECTED since it
1246 * failed, call connman_network_set_connected to report
1247 * disconnect is completed.
1250 connman_network_set_connected(wifi->network, FALSE);
1253 wifi->network = NULL;
1255 wifi->disconnecting = FALSE;
1257 if (wifi->pending_network != NULL) {
1258 network_connect(wifi->pending_network);
1259 wifi->pending_network = NULL;
1262 start_autoscan(wifi->device);
1265 static int network_disconnect(struct connman_network *network)
1267 struct connman_device *device = connman_network_get_device(network);
1268 struct wifi_data *wifi;
1271 DBG("network %p", network);
1273 wifi = connman_device_get_data(device);
1274 if (wifi == NULL || wifi->interface == NULL)
1277 connman_network_set_associating(network, FALSE);
1279 if (wifi->disconnecting == TRUE)
1282 wifi->disconnecting = TRUE;
1284 err = g_supplicant_interface_disconnect(wifi->interface,
1285 disconnect_callback, wifi);
1287 wifi->disconnecting = FALSE;
1292 static struct connman_network_driver network_driver = {
1294 .type = CONNMAN_NETWORK_TYPE_WIFI,
1295 .priority = CONNMAN_NETWORK_PRIORITY_LOW,
1296 .probe = network_probe,
1297 .remove = network_remove,
1298 .connect = network_connect,
1299 .disconnect = network_disconnect,
1302 static void interface_added(GSupplicantInterface *interface)
1304 const char *ifname = g_supplicant_interface_get_ifname(interface);
1305 const char *driver = g_supplicant_interface_get_driver(interface);
1306 struct wifi_data *wifi;
1308 wifi = g_supplicant_interface_get_data(interface);
1311 * We can get here with a NULL wifi pointer when
1312 * the interface added signal is sent before the
1313 * interface creation callback is called.
1318 DBG("ifname %s driver %s wifi %p tethering %d",
1319 ifname, driver, wifi, wifi->tethering);
1321 if (wifi->device == NULL) {
1322 connman_error("WiFi device not set");
1326 connman_device_set_powered(wifi->device, TRUE);
1328 if (wifi->tethering == TRUE)
1332 static connman_bool_t is_idle(struct wifi_data *wifi)
1334 DBG("state %d", wifi->state);
1336 switch (wifi->state) {
1337 case G_SUPPLICANT_STATE_UNKNOWN:
1338 case G_SUPPLICANT_STATE_DISCONNECTED:
1339 case G_SUPPLICANT_STATE_INACTIVE:
1340 case G_SUPPLICANT_STATE_SCANNING:
1343 case G_SUPPLICANT_STATE_AUTHENTICATING:
1344 case G_SUPPLICANT_STATE_ASSOCIATING:
1345 case G_SUPPLICANT_STATE_ASSOCIATED:
1346 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
1347 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
1348 case G_SUPPLICANT_STATE_COMPLETED:
1355 static connman_bool_t is_idle_wps(GSupplicantInterface *interface,
1356 struct wifi_data *wifi)
1358 /* First, let's check if WPS processing did not went wrong */
1359 if (g_supplicant_interface_get_wps_state(interface) ==
1360 G_SUPPLICANT_WPS_STATE_FAIL)
1363 /* Unlike normal connection, being associated while processing wps
1364 * actually means that we are idling. */
1365 switch (wifi->state) {
1366 case G_SUPPLICANT_STATE_UNKNOWN:
1367 case G_SUPPLICANT_STATE_DISCONNECTED:
1368 case G_SUPPLICANT_STATE_INACTIVE:
1369 case G_SUPPLICANT_STATE_SCANNING:
1370 case G_SUPPLICANT_STATE_ASSOCIATED:
1372 case G_SUPPLICANT_STATE_AUTHENTICATING:
1373 case G_SUPPLICANT_STATE_ASSOCIATING:
1374 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
1375 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
1376 case G_SUPPLICANT_STATE_COMPLETED:
1383 static connman_bool_t handle_wps_completion(GSupplicantInterface *interface,
1384 struct connman_network *network,
1385 struct connman_device *device,
1386 struct wifi_data *wifi)
1390 wps = connman_network_get_bool(network, "WiFi.UseWPS");
1392 const unsigned char *ssid, *wps_ssid;
1393 unsigned int ssid_len, wps_ssid_len;
1394 const char *wps_key;
1396 /* Checking if we got associated with requested
1398 ssid = connman_network_get_blob(network, "WiFi.SSID",
1401 wps_ssid = g_supplicant_interface_get_wps_ssid(
1402 interface, &wps_ssid_len);
1404 if (wps_ssid == NULL || wps_ssid_len != ssid_len ||
1405 memcmp(ssid, wps_ssid, ssid_len) != 0) {
1406 connman_network_set_associating(network, FALSE);
1407 g_supplicant_interface_disconnect(wifi->interface,
1408 disconnect_callback, wifi);
1412 wps_key = g_supplicant_interface_get_wps_key(interface);
1413 connman_network_set_string(network, "WiFi.Passphrase",
1416 connman_network_set_string(network, "WiFi.PinWPS", NULL);
1422 static connman_bool_t handle_4way_handshake_failure(GSupplicantInterface *interface,
1423 struct connman_network *network,
1424 struct wifi_data *wifi)
1426 if (wifi->state != G_SUPPLICANT_STATE_4WAY_HANDSHAKE)
1431 if (wifi->retries < MAXIMUM_RETRIES)
1434 connman_network_set_error(network, CONNMAN_NETWORK_ERROR_INVALID_KEY);
1439 static void interface_state(GSupplicantInterface *interface)
1441 struct connman_network *network;
1442 struct connman_device *device;
1443 struct wifi_data *wifi;
1444 GSupplicantState state = g_supplicant_interface_get_state(interface);
1447 wifi = g_supplicant_interface_get_data(interface);
1449 DBG("wifi %p interface state %d", wifi, state);
1454 network = wifi->network;
1455 device = wifi->device;
1457 if (device == NULL || network == NULL)
1461 case G_SUPPLICANT_STATE_SCANNING:
1464 case G_SUPPLICANT_STATE_AUTHENTICATING:
1465 case G_SUPPLICANT_STATE_ASSOCIATING:
1466 stop_autoscan(device);
1468 if (wifi->connected == FALSE)
1469 connman_network_set_associating(network, TRUE);
1473 case G_SUPPLICANT_STATE_COMPLETED:
1474 /* though it should be already stopped: */
1475 stop_autoscan(device);
1477 if (handle_wps_completion(interface, network, device, wifi) ==
1481 connman_network_set_connected(network, TRUE);
1484 case G_SUPPLICANT_STATE_DISCONNECTED:
1486 * If we're in one of the idle modes, we have
1487 * not started association yet and thus setting
1488 * those ones to FALSE could cancel an association
1491 wps = connman_network_get_bool(network, "WiFi.UseWPS");
1493 if (is_idle_wps(interface, wifi) == TRUE)
1499 /* If previous state was 4way-handshake, then
1500 * it's either: psk was incorrect and thus we retry
1501 * or if we reach the maximum retries we declare the
1503 if (handle_4way_handshake_failure(interface,
1504 network, wifi) == TRUE)
1507 /* We disable the selected network, if not then
1508 * wpa_supplicant will loop retrying */
1509 if (g_supplicant_interface_enable_selected_network(interface,
1511 DBG("Could not disables selected network");
1513 connman_network_set_connected(network, FALSE);
1514 connman_network_set_associating(network, FALSE);
1515 wifi->disconnecting = FALSE;
1517 start_autoscan(device);
1521 case G_SUPPLICANT_STATE_INACTIVE:
1522 connman_network_set_associating(network, FALSE);
1523 start_autoscan(device);
1527 case G_SUPPLICANT_STATE_UNKNOWN:
1528 case G_SUPPLICANT_STATE_ASSOCIATED:
1529 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
1530 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
1534 wifi->state = state;
1536 /* Saving wpa_s state policy:
1537 * If connected and if the state changes are roaming related:
1538 * --> We stay connected
1540 * --> We are connected
1542 * --> We are not connected
1545 case G_SUPPLICANT_STATE_AUTHENTICATING:
1546 case G_SUPPLICANT_STATE_ASSOCIATING:
1547 case G_SUPPLICANT_STATE_ASSOCIATED:
1548 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
1549 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
1550 if (wifi->connected == TRUE)
1551 connman_warn("Probably roaming right now!"
1552 " Staying connected...");
1554 wifi->connected = FALSE;
1556 case G_SUPPLICANT_STATE_COMPLETED:
1557 wifi->connected = TRUE;
1560 wifi->connected = FALSE;
1567 static void interface_removed(GSupplicantInterface *interface)
1569 const char *ifname = g_supplicant_interface_get_ifname(interface);
1570 struct wifi_data *wifi;
1572 DBG("ifname %s", ifname);
1574 wifi = g_supplicant_interface_get_data(interface);
1576 if (wifi != NULL && wifi->tethering == TRUE)
1579 if (wifi == NULL || wifi->device == NULL) {
1580 DBG("wifi interface already removed");
1584 wifi->interface = NULL;
1585 connman_device_set_powered(wifi->device, FALSE);
1588 static void scan_started(GSupplicantInterface *interface)
1593 static void scan_finished(GSupplicantInterface *interface)
1598 static unsigned char calculate_strength(GSupplicantNetwork *supplicant_network)
1600 unsigned char strength;
1602 strength = 120 + g_supplicant_network_get_signal(supplicant_network);
1609 static void network_added(GSupplicantNetwork *supplicant_network)
1611 struct connman_network *network;
1612 GSupplicantInterface *interface;
1613 struct wifi_data *wifi;
1614 const char *name, *identifier, *security, *group, *mode;
1615 const unsigned char *ssid;
1616 unsigned int ssid_len;
1618 connman_bool_t wps_pbc;
1619 connman_bool_t wps_ready;
1620 connman_bool_t wps_advertizing;
1624 interface = g_supplicant_network_get_interface(supplicant_network);
1625 wifi = g_supplicant_interface_get_data(interface);
1626 name = g_supplicant_network_get_name(supplicant_network);
1627 identifier = g_supplicant_network_get_identifier(supplicant_network);
1628 security = g_supplicant_network_get_security(supplicant_network);
1629 group = g_supplicant_network_get_identifier(supplicant_network);
1630 wps = g_supplicant_network_get_wps(supplicant_network);
1631 wps_pbc = g_supplicant_network_is_wps_pbc(supplicant_network);
1632 wps_ready = g_supplicant_network_is_wps_active(supplicant_network);
1633 wps_advertizing = g_supplicant_network_is_wps_advertizing(
1634 supplicant_network);
1635 mode = g_supplicant_network_get_mode(supplicant_network);
1640 ssid = g_supplicant_network_get_ssid(supplicant_network, &ssid_len);
1642 network = connman_device_get_network(wifi->device, identifier);
1644 if (network == NULL) {
1645 network = connman_network_create(identifier,
1646 CONNMAN_NETWORK_TYPE_WIFI);
1647 if (network == NULL)
1650 connman_network_set_index(network, wifi->index);
1652 if (connman_device_add_network(wifi->device, network) < 0) {
1653 connman_network_unref(network);
1657 wifi->networks = g_slist_append(wifi->networks, network);
1660 if (name != NULL && name[0] != '\0')
1661 connman_network_set_name(network, name);
1663 connman_network_set_blob(network, "WiFi.SSID",
1665 connman_network_set_string(network, "WiFi.Security", security);
1666 connman_network_set_strength(network,
1667 calculate_strength(supplicant_network));
1668 connman_network_set_bool(network, "WiFi.WPS", wps);
1671 /* Is AP advertizing for WPS association?
1672 * If so, we decide to use WPS by default */
1673 if (wps_ready == TRUE && wps_pbc == TRUE &&
1674 wps_advertizing == TRUE)
1675 connman_network_set_bool(network, "WiFi.UseWPS", TRUE);
1678 connman_network_set_frequency(network,
1679 g_supplicant_network_get_frequency(supplicant_network));
1681 connman_network_set_available(network, TRUE);
1682 connman_network_set_string(network, "WiFi.Mode", mode);
1685 connman_network_set_group(network, group);
1687 if (wifi->hidden != NULL && ssid != NULL) {
1688 if (wifi->hidden->ssid_len == ssid_len &&
1689 memcmp(wifi->hidden->ssid, ssid,
1691 connman_network_connect_hidden(network,
1692 wifi->hidden->identity,
1693 wifi->hidden->passphrase,
1694 wifi->hidden->user_data);
1695 wifi->hidden->user_data = NULL;
1696 hidden_free(wifi->hidden);
1697 wifi->hidden = NULL;
1702 static void network_removed(GSupplicantNetwork *network)
1704 GSupplicantInterface *interface;
1705 struct wifi_data *wifi;
1706 const char *name, *identifier;
1707 struct connman_network *connman_network;
1709 interface = g_supplicant_network_get_interface(network);
1710 wifi = g_supplicant_interface_get_data(interface);
1711 identifier = g_supplicant_network_get_identifier(network);
1712 name = g_supplicant_network_get_name(network);
1714 DBG("name %s", name);
1719 connman_network = connman_device_get_network(wifi->device, identifier);
1720 if (connman_network == NULL)
1723 wifi->networks = g_slist_remove(wifi->networks, connman_network);
1725 connman_device_remove_network(wifi->device, connman_network);
1726 connman_network_unref(connman_network);
1729 static void network_changed(GSupplicantNetwork *network, const char *property)
1731 GSupplicantInterface *interface;
1732 struct wifi_data *wifi;
1733 const char *name, *identifier;
1734 struct connman_network *connman_network;
1736 interface = g_supplicant_network_get_interface(network);
1737 wifi = g_supplicant_interface_get_data(interface);
1738 identifier = g_supplicant_network_get_identifier(network);
1739 name = g_supplicant_network_get_name(network);
1741 DBG("name %s", name);
1746 connman_network = connman_device_get_network(wifi->device, identifier);
1747 if (connman_network == NULL)
1750 if (g_str_equal(property, "Signal") == TRUE) {
1751 connman_network_set_strength(connman_network,
1752 calculate_strength(network));
1753 connman_network_update(connman_network);
1757 static void debug(const char *str)
1759 if (getenv("CONNMAN_SUPPLICANT_DEBUG"))
1760 connman_debug("%s", str);
1763 static const GSupplicantCallbacks callbacks = {
1764 .system_ready = system_ready,
1765 .system_killed = system_killed,
1766 .interface_added = interface_added,
1767 .interface_state = interface_state,
1768 .interface_removed = interface_removed,
1769 .scan_started = scan_started,
1770 .scan_finished = scan_finished,
1771 .network_added = network_added,
1772 .network_removed = network_removed,
1773 .network_changed = network_changed,
1778 static int tech_probe(struct connman_technology *technology)
1780 wifi_technology = technology;
1785 static void tech_remove(struct connman_technology *technology)
1787 wifi_technology = NULL;
1790 struct wifi_tethering_info {
1791 struct wifi_data *wifi;
1792 struct connman_technology *technology;
1794 GSupplicantSSID *ssid;
1797 static GSupplicantSSID *ssid_ap_init(const char *ssid, const char *passphrase)
1799 GSupplicantSSID *ap;
1801 ap = g_try_malloc0(sizeof(GSupplicantSSID));
1805 ap->mode = G_SUPPLICANT_MODE_MASTER;
1807 ap->ssid_len = strlen(ssid);
1811 if (passphrase == NULL || strlen(passphrase) == 0) {
1812 ap->security = G_SUPPLICANT_SECURITY_NONE;
1813 ap->passphrase = NULL;
1815 ap->security = G_SUPPLICANT_SECURITY_PSK;
1816 ap->protocol = G_SUPPLICANT_PROTO_RSN;
1817 ap->pairwise_cipher = G_SUPPLICANT_PAIRWISE_CCMP;
1818 ap->group_cipher = G_SUPPLICANT_GROUP_CCMP;
1819 ap->passphrase = passphrase;
1825 static void ap_start_callback(int result, GSupplicantInterface *interface,
1828 struct wifi_tethering_info *info = user_data;
1830 DBG("result %d index %d bridge %s",
1831 result, info->wifi->index, info->wifi->bridge);
1834 connman_inet_remove_from_bridge(info->wifi->index,
1835 info->wifi->bridge);
1836 connman_technology_tethering_notify(info->technology, FALSE);
1839 g_free(info->ifname);
1843 static void ap_create_callback(int result,
1844 GSupplicantInterface *interface,
1847 struct wifi_tethering_info *info = user_data;
1849 DBG("result %d ifname %s", result,
1850 g_supplicant_interface_get_ifname(interface));
1853 connman_inet_remove_from_bridge(info->wifi->index,
1854 info->wifi->bridge);
1855 connman_technology_tethering_notify(info->technology, FALSE);
1857 g_free(info->ifname);
1862 info->wifi->interface = interface;
1863 g_supplicant_interface_set_data(interface, info->wifi);
1865 if (g_supplicant_interface_set_apscan(interface, 2) < 0)
1866 connman_error("Failed to set interface ap_scan property");
1868 g_supplicant_interface_connect(interface, info->ssid,
1869 ap_start_callback, info);
1872 static void sta_remove_callback(int result,
1873 GSupplicantInterface *interface,
1876 struct wifi_tethering_info *info = user_data;
1877 const char *driver = connman_option_get_string("wifi");
1879 DBG("ifname %s result %d ", info->ifname, result);
1882 info->wifi->tethering = TRUE;
1884 g_free(info->ifname);
1889 info->wifi->interface = NULL;
1891 connman_technology_tethering_notify(info->technology, TRUE);
1893 g_supplicant_interface_create(info->ifname, driver, info->wifi->bridge,
1898 static int tech_set_tethering(struct connman_technology *technology,
1899 const char *identifier, const char *passphrase,
1900 const char *bridge, connman_bool_t enabled)
1903 GSupplicantInterface *interface;
1904 struct wifi_data *wifi;
1905 struct wifi_tethering_info *info;
1912 if (enabled == FALSE) {
1913 for (list = iface_list; list; list = list->next) {
1916 if (wifi->tethering == TRUE) {
1917 wifi->tethering = FALSE;
1919 connman_inet_remove_from_bridge(wifi->index,
1921 wifi->bridged = FALSE;
1925 connman_technology_tethering_notify(technology, FALSE);
1930 for (list = iface_list; list; list = list->next) {
1933 interface = wifi->interface;
1935 if (interface == NULL)
1938 ifname = g_supplicant_interface_get_ifname(wifi->interface);
1940 mode = g_supplicant_interface_get_mode(interface);
1941 if ((mode & G_SUPPLICANT_CAPABILITY_MODE_AP) == 0) {
1942 DBG("%s does not support AP mode", ifname);
1946 info = g_try_malloc0(sizeof(struct wifi_tethering_info));
1951 info->technology = technology;
1952 info->wifi->bridge = bridge;
1953 info->ssid = ssid_ap_init(identifier, passphrase);
1954 if (info->ssid == NULL) {
1958 info->ifname = g_strdup(ifname);
1959 if (info->ifname == NULL) {
1964 info->wifi->tethering = TRUE;
1966 err = g_supplicant_interface_remove(interface,
1967 sta_remove_callback,
1976 static void regdom_callback(int result, const char *alpha2, void *user_data)
1980 if (wifi_technology == NULL)
1986 connman_technology_regdom_notify(wifi_technology, alpha2);
1989 static int tech_set_regdom(struct connman_technology *technology, const char *alpha2)
1991 return g_supplicant_set_country(alpha2, regdom_callback, NULL);
1994 static struct connman_technology_driver tech_driver = {
1996 .type = CONNMAN_SERVICE_TYPE_WIFI,
1997 .probe = tech_probe,
1998 .remove = tech_remove,
1999 .set_tethering = tech_set_tethering,
2000 .set_regdom = tech_set_regdom,
2003 static int wifi_init(void)
2007 err = connman_network_driver_register(&network_driver);
2011 err = g_supplicant_register(&callbacks);
2013 connman_network_driver_unregister(&network_driver);
2017 err = connman_technology_driver_register(&tech_driver);
2019 g_supplicant_unregister(&callbacks);
2020 connman_network_driver_unregister(&network_driver);
2027 static void wifi_exit(void)
2031 connman_technology_driver_unregister(&tech_driver);
2033 g_supplicant_unregister(&callbacks);
2035 connman_network_driver_unregister(&network_driver);
2038 CONNMAN_PLUGIN_DEFINE(wifi, "WiFi interface plugin", VERSION,
2039 CONNMAN_PLUGIN_PRIORITY_DEFAULT, wifi_init, wifi_exit)