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 throw_wifi_scan(struct connman_device *device,
325 GSupplicantInterfaceCallback callback)
327 struct wifi_data *wifi = connman_device_get_data(device);
330 DBG("device %p %p", device, wifi->interface);
332 if (wifi->tethering == TRUE)
335 connman_device_ref(device);
337 ret = g_supplicant_interface_scan(wifi->interface, NULL,
340 connman_device_set_scanning(device, TRUE);
342 connman_device_unref(device);
347 static void autoscan_scan_callback(int result,
348 GSupplicantInterface *interface, void *user_data)
350 struct connman_device *device = user_data;
354 connman_device_set_scanning(device, FALSE);
355 connman_device_unref(device);
358 static gboolean autoscan_timeout(gpointer data)
360 struct connman_device *device = data;
361 struct wifi_data *wifi = connman_device_get_data(device);
362 struct autoscan_params *autoscan;
365 autoscan = wifi->autoscan;
367 if (autoscan->interval <= 0) {
368 interval = autoscan->base;
371 interval = autoscan->interval * autoscan->base;
373 if (autoscan->interval >= autoscan->limit)
374 interval = autoscan->limit;
376 throw_wifi_scan(wifi->device, autoscan_scan_callback);
379 DBG("interval %d", interval);
381 autoscan->interval = interval;
383 autoscan->timeout = g_timeout_add_seconds(interval,
384 autoscan_timeout, device);
389 static void start_autoscan(struct connman_device *device)
391 struct wifi_data *wifi = connman_device_get_data(device);
392 struct autoscan_params *autoscan;
399 autoscan = wifi->autoscan;
400 if (autoscan == NULL)
403 if (autoscan->timeout > 0 || autoscan->interval > 0)
406 connman_device_ref(device);
408 autoscan_timeout(device);
411 static struct autoscan_params *parse_autoscan_params(const char *params)
413 struct autoscan_params *autoscan;
418 DBG("Emulating autoscan");
420 list_params = g_strsplit(params, ":", 0);
421 if (list_params == 0)
424 if (g_strv_length(list_params) < 3) {
425 g_strfreev(list_params);
429 base = atoi(list_params[1]);
430 limit = atoi(list_params[2]);
432 g_strfreev(list_params);
434 autoscan = g_try_malloc0(sizeof(struct autoscan_params));
435 if (autoscan == NULL) {
436 DBG("Could not allocate memory for autoscan");
440 DBG("base %d - limit %d", base, limit);
441 autoscan->base = base;
442 autoscan->limit = limit;
447 static void setup_autoscan(struct wifi_data *wifi)
449 if (wifi->autoscan == NULL)
450 wifi->autoscan = parse_autoscan_params(AUTOSCAN_DEFAULT);
452 start_autoscan(wifi->device);
455 static void interface_create_callback(int result,
456 GSupplicantInterface *interface,
459 struct wifi_data *wifi = user_data;
461 DBG("result %d ifname %s, wifi %p", result,
462 g_supplicant_interface_get_ifname(interface),
465 if (result < 0 || wifi == NULL)
468 wifi->interface = interface;
469 g_supplicant_interface_set_data(interface, wifi);
471 if (g_supplicant_interface_get_ready(interface) == FALSE)
474 DBG("interface is ready wifi %p tethering %d", wifi, wifi->tethering);
476 if (wifi->device == NULL) {
477 connman_error("WiFi device not set");
481 connman_device_set_powered(wifi->device, TRUE);
483 if (connman_setting_get_bool("BackgroundScanning") == FALSE)
486 /* Setting up automatic scanning */
487 setup_autoscan(wifi);
490 static int wifi_enable(struct connman_device *device)
492 struct wifi_data *wifi = connman_device_get_data(device);
493 const char *interface = connman_device_get_string(device, "Interface");
494 const char *driver = connman_option_get_string("wifi");
497 DBG("device %p %p", device, wifi);
499 ret = g_supplicant_interface_create(interface, driver, NULL,
500 interface_create_callback,
508 static int wifi_disable(struct connman_device *device)
510 struct wifi_data *wifi = connman_device_get_data(device);
513 DBG("device %p", device);
515 wifi->connected = FALSE;
516 wifi->disconnecting = FALSE;
518 if (wifi->pending_network != NULL)
519 wifi->pending_network = NULL;
521 remove_networks(device, wifi);
523 ret = g_supplicant_interface_remove(wifi->interface, NULL, NULL);
530 static void hidden_free(struct hidden_params *hidden)
535 g_free(hidden->identity);
536 g_free(hidden->passphrase);
540 static void scan_callback(int result, GSupplicantInterface *interface,
543 struct connman_device *device = user_data;
544 struct wifi_data *wifi = connman_device_get_data(device);
546 DBG("result %d", result);
548 if (wifi != NULL && wifi->hidden != NULL) {
549 hidden_free(wifi->hidden);
554 connman_device_reset_scanning(device);
556 connman_device_set_scanning(device, FALSE);
557 connman_device_unref(device);
559 start_autoscan(device);
562 struct last_connected {
568 static gint sort_entry(gconstpointer a, gconstpointer b, gpointer user_data)
570 GTimeVal *aval = (GTimeVal *)a;
571 GTimeVal *bval = (GTimeVal *)b;
573 /* Note that the sort order is descending */
574 if (aval->tv_sec < bval->tv_sec)
577 if (aval->tv_sec > bval->tv_sec)
583 static void free_entry(gpointer data)
585 struct last_connected *entry = data;
591 static int get_latest_connections(int max_ssids,
592 GSupplicantScanParams *scan_data)
595 GSequence *latest_list;
596 struct last_connected *entry;
605 latest_list = g_sequence_new(free_entry);
606 if (latest_list == NULL)
609 services = connman_storage_get_services();
610 for (i = 0; services && services[i]; i++) {
611 if (strncmp(services[i], "wifi_", 5) != 0)
614 keyfile = connman_storage_load_service(services[i]);
616 str = g_key_file_get_string(keyfile,
617 services[i], "Favorite", NULL);
618 if (str == NULL || g_strcmp0(str, "true")) {
621 g_key_file_free(keyfile);
626 str = g_key_file_get_string(keyfile,
627 services[i], "AutoConnect", NULL);
628 if (str == NULL || g_strcmp0(str, "true")) {
631 g_key_file_free(keyfile);
636 str = g_key_file_get_string(keyfile,
637 services[i], "Modified", NULL);
639 g_time_val_from_iso8601(str, &modified);
643 ssid = g_key_file_get_string(keyfile,
644 services[i], "SSID", NULL);
646 freq = g_key_file_get_integer(keyfile, services[i],
649 entry = g_try_new(struct last_connected, 1);
651 g_sequence_free(latest_list);
652 g_key_file_free(keyfile);
658 entry->modified = modified;
661 g_sequence_insert_sorted(latest_list, entry,
667 g_key_file_free(keyfile);
670 g_strfreev(services);
672 num_ssids = num_ssids > max_ssids ? max_ssids : num_ssids;
674 iter = g_sequence_get_begin_iter(latest_list);
676 for (i = 0; i < num_ssids; i++) {
677 entry = g_sequence_get(iter);
679 DBG("ssid %s freq %d modified %lu", entry->ssid, entry->freq,
680 entry->modified.tv_sec);
682 add_scan_param(entry->ssid, entry->freq, scan_data, max_ssids);
684 iter = g_sequence_iter_next(iter);
687 g_sequence_free(latest_list);
691 static int wifi_scan(struct connman_device *device)
693 stop_autoscan(device);
695 return throw_wifi_scan(device, scan_callback);
698 static int wifi_scan_fast(struct connman_device *device)
700 struct wifi_data *wifi = connman_device_get_data(device);
701 GSupplicantScanParams *scan_params = NULL;
703 int driver_max_ssids = 0;
705 DBG("device %p %p", device, wifi->interface);
707 if (wifi->tethering == TRUE)
710 driver_max_ssids = g_supplicant_interface_get_max_scan_ssids(
712 DBG("max ssids %d", driver_max_ssids);
713 if (driver_max_ssids == 0)
714 return wifi_scan(device);
716 scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
717 if (scan_params == NULL)
720 ret = get_latest_connections(driver_max_ssids, scan_params);
722 g_supplicant_free_scan_params(scan_params);
723 return wifi_scan(device);
726 stop_autoscan(device);
728 connman_device_ref(device);
729 ret = g_supplicant_interface_scan(wifi->interface, scan_params,
730 scan_callback, device);
732 connman_device_set_scanning(device, TRUE);
734 g_supplicant_free_scan_params(scan_params);
735 connman_device_unref(device);
741 static int wifi_scan_hidden(struct connman_device *device,
742 const char *ssid, unsigned int ssid_len,
743 const char *identity, const char* passphrase)
745 struct wifi_data *wifi = connman_device_get_data(device);
746 GSupplicantScanParams *scan_params = NULL;
747 struct scan_ssid *scan_ssid;
748 struct hidden_params *hidden;
751 DBG("hidden SSID %s", ssid);
753 if (wifi->tethering == TRUE || wifi->hidden != NULL)
756 if (ssid == NULL || ssid_len == 0 || ssid_len > 32)
759 scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
760 if (scan_params == NULL)
763 scan_ssid = g_try_new(struct scan_ssid, 1);
764 if (scan_ssid == NULL) {
769 memcpy(scan_ssid->ssid, ssid, ssid_len);
770 scan_ssid->ssid_len = ssid_len;
771 scan_params->ssids = g_slist_prepend(scan_params->ssids, scan_ssid);
773 scan_params->num_ssids = 1;
775 hidden = g_try_new0(struct hidden_params, 1);
776 if (hidden == NULL) {
780 memcpy(hidden->ssid, ssid, ssid_len);
781 hidden->ssid_len = ssid_len;
782 hidden->identity = g_strdup(identity);
783 hidden->passphrase = g_strdup(passphrase);
784 wifi->hidden = hidden;
786 stop_autoscan(device);
788 connman_device_ref(device);
789 ret = g_supplicant_interface_scan(wifi->interface, scan_params,
790 scan_callback, device);
792 connman_device_set_scanning(device, TRUE);
794 connman_device_unref(device);
795 g_supplicant_free_scan_params(scan_params);
796 hidden_free(wifi->hidden);
803 static struct connman_device_driver wifi_ng_driver = {
805 .type = CONNMAN_DEVICE_TYPE_WIFI,
806 .priority = CONNMAN_DEVICE_PRIORITY_LOW,
808 .remove = wifi_remove,
809 .enable = wifi_enable,
810 .disable = wifi_disable,
812 .scan_fast = wifi_scan_fast,
813 .scan_hidden = wifi_scan_hidden,
816 static void system_ready(void)
820 if (connman_device_driver_register(&wifi_ng_driver) < 0)
821 connman_error("Failed to register WiFi driver");
824 static void system_killed(void)
828 connman_device_driver_unregister(&wifi_ng_driver);
831 static int network_probe(struct connman_network *network)
833 DBG("network %p", network);
838 static void network_remove(struct connman_network *network)
840 struct connman_device *device = connman_network_get_device(network);
841 struct wifi_data *wifi;
843 DBG("network %p", network);
845 wifi = connman_device_get_data(device);
849 if (wifi->network != network)
852 wifi->network = NULL;
855 static void connect_callback(int result, GSupplicantInterface *interface,
858 struct connman_network *network = user_data;
860 DBG("network %p result %d", network, result);
862 if (result == -ENOKEY) {
863 connman_network_set_error(network,
864 CONNMAN_NETWORK_ERROR_INVALID_KEY);
865 } else if (result < 0) {
866 connman_network_set_error(network,
867 CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
871 static GSupplicantSecurity network_security(const char *security)
873 if (g_str_equal(security, "none") == TRUE)
874 return G_SUPPLICANT_SECURITY_NONE;
875 else if (g_str_equal(security, "wep") == TRUE)
876 return G_SUPPLICANT_SECURITY_WEP;
877 else if (g_str_equal(security, "psk") == TRUE)
878 return G_SUPPLICANT_SECURITY_PSK;
879 else if (g_str_equal(security, "wpa") == TRUE)
880 return G_SUPPLICANT_SECURITY_PSK;
881 else if (g_str_equal(security, "rsn") == TRUE)
882 return G_SUPPLICANT_SECURITY_PSK;
883 else if (g_str_equal(security, "ieee8021x") == TRUE)
884 return G_SUPPLICANT_SECURITY_IEEE8021X;
886 return G_SUPPLICANT_SECURITY_UNKNOWN;
889 static void ssid_init(GSupplicantSSID *ssid, struct connman_network *network)
891 const char *security, *passphrase, *agent_passphrase;
893 memset(ssid, 0, sizeof(*ssid));
894 ssid->mode = G_SUPPLICANT_MODE_INFRA;
895 ssid->ssid = connman_network_get_blob(network, "WiFi.SSID",
898 security = connman_network_get_string(network, "WiFi.Security");
899 ssid->security = network_security(security);
900 passphrase = connman_network_get_string(network,
902 if (passphrase == NULL || strlen(passphrase) == 0) {
904 /* Use agent provided passphrase as a fallback */
905 agent_passphrase = connman_network_get_string(network,
906 "WiFi.AgentPassphrase");
908 if (agent_passphrase == NULL || strlen(agent_passphrase) == 0)
909 ssid->passphrase = NULL;
911 ssid->passphrase = agent_passphrase;
913 ssid->passphrase = passphrase;
915 ssid->eap = connman_network_get_string(network, "WiFi.EAP");
918 * If our private key password is unset,
919 * we use the supplied passphrase. That is needed
920 * for PEAP where 2 passphrases (identity and client
921 * cert may have to be provided.
923 if (connman_network_get_string(network,
924 "WiFi.PrivateKeyPassphrase") == NULL)
925 connman_network_set_string(network,
926 "WiFi.PrivateKeyPassphrase",
928 /* We must have an identity for both PEAP and TLS */
929 ssid->identity = connman_network_get_string(network, "WiFi.Identity");
931 /* Use agent provided identity as a fallback */
932 if (ssid->identity == NULL || strlen(ssid->identity) == 0)
933 ssid->identity = connman_network_get_string(network,
934 "WiFi.AgentIdentity");
936 ssid->ca_cert_path = connman_network_get_string(network,
938 ssid->client_cert_path = connman_network_get_string(network,
939 "WiFi.ClientCertFile");
940 ssid->private_key_path = connman_network_get_string(network,
941 "WiFi.PrivateKeyFile");
942 ssid->private_key_passphrase = connman_network_get_string(network,
943 "WiFi.PrivateKeyPassphrase");
944 ssid->phase2_auth = connman_network_get_string(network, "WiFi.Phase2");
946 ssid->use_wps = connman_network_get_bool(network, "WiFi.UseWPS");
947 ssid->pin_wps = connman_network_get_string(network, "WiFi.PinWPS");
949 if (connman_setting_get_bool("BackgroundScanning") == TRUE)
950 ssid->bgscan = BGSCAN_DEFAULT;
953 static int network_connect(struct connman_network *network)
955 struct connman_device *device = connman_network_get_device(network);
956 struct wifi_data *wifi;
957 GSupplicantInterface *interface;
958 GSupplicantSSID *ssid;
960 DBG("network %p", network);
965 wifi = connman_device_get_data(device);
969 ssid = g_try_malloc0(sizeof(GSupplicantSSID));
973 interface = wifi->interface;
975 ssid_init(ssid, network);
977 if (wifi->disconnecting == TRUE)
978 wifi->pending_network = network;
980 wifi->network = network;
983 return g_supplicant_interface_connect(interface, ssid,
984 connect_callback, network);
990 static void disconnect_callback(int result, GSupplicantInterface *interface,
993 struct wifi_data *wifi = user_data;
995 if (wifi->network != NULL) {
997 * if result < 0 supplican return an error because
998 * the network is not current.
999 * we wont receive G_SUPPLICANT_STATE_DISCONNECTED since it
1000 * failed, call connman_network_set_connected to report
1001 * disconnect is completed.
1004 connman_network_set_connected(wifi->network, FALSE);
1007 wifi->network = NULL;
1009 wifi->disconnecting = FALSE;
1011 if (wifi->pending_network != NULL) {
1012 network_connect(wifi->pending_network);
1013 wifi->pending_network = NULL;
1016 start_autoscan(wifi->device);
1019 static int network_disconnect(struct connman_network *network)
1021 struct connman_device *device = connman_network_get_device(network);
1022 struct wifi_data *wifi;
1025 DBG("network %p", network);
1027 wifi = connman_device_get_data(device);
1028 if (wifi == NULL || wifi->interface == NULL)
1031 connman_network_set_associating(network, FALSE);
1033 if (wifi->disconnecting == TRUE)
1036 wifi->disconnecting = TRUE;
1038 err = g_supplicant_interface_disconnect(wifi->interface,
1039 disconnect_callback, wifi);
1041 wifi->disconnecting = FALSE;
1046 static struct connman_network_driver network_driver = {
1048 .type = CONNMAN_NETWORK_TYPE_WIFI,
1049 .priority = CONNMAN_NETWORK_PRIORITY_LOW,
1050 .probe = network_probe,
1051 .remove = network_remove,
1052 .connect = network_connect,
1053 .disconnect = network_disconnect,
1056 static void interface_added(GSupplicantInterface *interface)
1058 const char *ifname = g_supplicant_interface_get_ifname(interface);
1059 const char *driver = g_supplicant_interface_get_driver(interface);
1060 struct wifi_data *wifi;
1062 wifi = g_supplicant_interface_get_data(interface);
1065 * We can get here with a NULL wifi pointer when
1066 * the interface added signal is sent before the
1067 * interface creation callback is called.
1072 DBG("ifname %s driver %s wifi %p tethering %d",
1073 ifname, driver, wifi, wifi->tethering);
1075 if (wifi->device == NULL) {
1076 connman_error("WiFi device not set");
1080 connman_device_set_powered(wifi->device, TRUE);
1082 if (wifi->tethering == TRUE)
1086 static connman_bool_t is_idle(struct wifi_data *wifi)
1088 DBG("state %d", wifi->state);
1090 switch (wifi->state) {
1091 case G_SUPPLICANT_STATE_UNKNOWN:
1092 case G_SUPPLICANT_STATE_DISCONNECTED:
1093 case G_SUPPLICANT_STATE_INACTIVE:
1094 case G_SUPPLICANT_STATE_SCANNING:
1097 case G_SUPPLICANT_STATE_AUTHENTICATING:
1098 case G_SUPPLICANT_STATE_ASSOCIATING:
1099 case G_SUPPLICANT_STATE_ASSOCIATED:
1100 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
1101 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
1102 case G_SUPPLICANT_STATE_COMPLETED:
1109 static connman_bool_t is_idle_wps(GSupplicantInterface *interface,
1110 struct wifi_data *wifi)
1112 /* First, let's check if WPS processing did not went wrong */
1113 if (g_supplicant_interface_get_wps_state(interface) ==
1114 G_SUPPLICANT_WPS_STATE_FAIL)
1117 /* Unlike normal connection, being associated while processing wps
1118 * actually means that we are idling. */
1119 switch (wifi->state) {
1120 case G_SUPPLICANT_STATE_UNKNOWN:
1121 case G_SUPPLICANT_STATE_DISCONNECTED:
1122 case G_SUPPLICANT_STATE_INACTIVE:
1123 case G_SUPPLICANT_STATE_SCANNING:
1124 case G_SUPPLICANT_STATE_ASSOCIATED:
1126 case G_SUPPLICANT_STATE_AUTHENTICATING:
1127 case G_SUPPLICANT_STATE_ASSOCIATING:
1128 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
1129 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
1130 case G_SUPPLICANT_STATE_COMPLETED:
1137 static connman_bool_t handle_wps_completion(GSupplicantInterface *interface,
1138 struct connman_network *network,
1139 struct connman_device *device,
1140 struct wifi_data *wifi)
1144 wps = connman_network_get_bool(network, "WiFi.UseWPS");
1146 const unsigned char *ssid, *wps_ssid;
1147 unsigned int ssid_len, wps_ssid_len;
1148 const char *wps_key;
1150 /* Checking if we got associated with requested
1152 ssid = connman_network_get_blob(network, "WiFi.SSID",
1155 wps_ssid = g_supplicant_interface_get_wps_ssid(
1156 interface, &wps_ssid_len);
1158 if (wps_ssid == NULL || wps_ssid_len != ssid_len ||
1159 memcmp(ssid, wps_ssid, ssid_len) != 0) {
1160 connman_network_set_associating(network, FALSE);
1161 g_supplicant_interface_disconnect(wifi->interface,
1162 disconnect_callback, wifi);
1166 wps_key = g_supplicant_interface_get_wps_key(interface);
1167 connman_network_set_string(network, "WiFi.Passphrase",
1170 connman_network_set_string(network, "WiFi.PinWPS", NULL);
1176 static connman_bool_t handle_4way_handshake_failure(GSupplicantInterface *interface,
1177 struct connman_network *network,
1178 struct wifi_data *wifi)
1180 if (wifi->state != G_SUPPLICANT_STATE_4WAY_HANDSHAKE)
1185 if (wifi->retries < MAXIMUM_RETRIES)
1188 connman_network_set_error(network, CONNMAN_NETWORK_ERROR_INVALID_KEY);
1193 static void interface_state(GSupplicantInterface *interface)
1195 struct connman_network *network;
1196 struct connman_device *device;
1197 struct wifi_data *wifi;
1198 GSupplicantState state = g_supplicant_interface_get_state(interface);
1201 wifi = g_supplicant_interface_get_data(interface);
1203 DBG("wifi %p interface state %d", wifi, state);
1208 network = wifi->network;
1209 device = wifi->device;
1211 if (device == NULL || network == NULL)
1215 case G_SUPPLICANT_STATE_SCANNING:
1218 case G_SUPPLICANT_STATE_AUTHENTICATING:
1219 case G_SUPPLICANT_STATE_ASSOCIATING:
1220 stop_autoscan(device);
1222 if (wifi->connected == FALSE)
1223 connman_network_set_associating(network, TRUE);
1227 case G_SUPPLICANT_STATE_COMPLETED:
1228 /* though it should be already stopped: */
1229 stop_autoscan(device);
1231 if (handle_wps_completion(interface, network, device, wifi) ==
1235 connman_network_set_connected(network, TRUE);
1238 case G_SUPPLICANT_STATE_DISCONNECTED:
1240 * If we're in one of the idle modes, we have
1241 * not started association yet and thus setting
1242 * those ones to FALSE could cancel an association
1245 wps = connman_network_get_bool(network, "WiFi.UseWPS");
1247 if (is_idle_wps(interface, wifi) == TRUE)
1253 /* If previous state was 4way-handshake, then
1254 * it's either: psk was incorrect and thus we retry
1255 * or if we reach the maximum retries we declare the
1257 if (handle_4way_handshake_failure(interface,
1258 network, wifi) == TRUE)
1261 /* We disable the selected network, if not then
1262 * wpa_supplicant will loop retrying */
1263 if (g_supplicant_interface_enable_selected_network(interface,
1265 DBG("Could not disables selected network");
1267 connman_network_set_connected(network, FALSE);
1268 connman_network_set_associating(network, FALSE);
1270 start_autoscan(device);
1274 case G_SUPPLICANT_STATE_INACTIVE:
1275 connman_network_set_associating(network, FALSE);
1276 start_autoscan(device);
1280 case G_SUPPLICANT_STATE_UNKNOWN:
1281 case G_SUPPLICANT_STATE_ASSOCIATED:
1282 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
1283 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
1287 wifi->state = state;
1289 /* Saving wpa_s state policy:
1290 * If connected and if the state changes are roaming related:
1291 * --> We stay connected
1293 * --> We are connected
1295 * --> We are not connected
1298 case G_SUPPLICANT_STATE_AUTHENTICATING:
1299 case G_SUPPLICANT_STATE_ASSOCIATING:
1300 case G_SUPPLICANT_STATE_ASSOCIATED:
1301 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
1302 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
1303 if (wifi->connected == TRUE)
1304 connman_warn("Probably roaming right now!"
1305 " Staying connected...");
1307 wifi->connected = FALSE;
1309 case G_SUPPLICANT_STATE_COMPLETED:
1310 wifi->connected = TRUE;
1313 wifi->connected = FALSE;
1320 static void interface_removed(GSupplicantInterface *interface)
1322 const char *ifname = g_supplicant_interface_get_ifname(interface);
1323 struct wifi_data *wifi;
1325 DBG("ifname %s", ifname);
1327 wifi = g_supplicant_interface_get_data(interface);
1329 if (wifi != NULL && wifi->tethering == TRUE)
1332 if (wifi == NULL || wifi->device == NULL) {
1333 connman_error("Wrong wifi pointer");
1337 wifi->interface = NULL;
1338 connman_device_set_powered(wifi->device, FALSE);
1341 static void scan_started(GSupplicantInterface *interface)
1346 static void scan_finished(GSupplicantInterface *interface)
1351 static unsigned char calculate_strength(GSupplicantNetwork *supplicant_network)
1353 unsigned char strength;
1355 strength = 120 + g_supplicant_network_get_signal(supplicant_network);
1362 static void network_added(GSupplicantNetwork *supplicant_network)
1364 struct connman_network *network;
1365 GSupplicantInterface *interface;
1366 struct wifi_data *wifi;
1367 const char *name, *identifier, *security, *group, *mode;
1368 const unsigned char *ssid;
1369 unsigned int ssid_len;
1371 connman_bool_t wps_pbc;
1372 connman_bool_t wps_ready;
1373 connman_bool_t wps_advertizing;
1377 interface = g_supplicant_network_get_interface(supplicant_network);
1378 wifi = g_supplicant_interface_get_data(interface);
1379 name = g_supplicant_network_get_name(supplicant_network);
1380 identifier = g_supplicant_network_get_identifier(supplicant_network);
1381 security = g_supplicant_network_get_security(supplicant_network);
1382 group = g_supplicant_network_get_identifier(supplicant_network);
1383 wps = g_supplicant_network_get_wps(supplicant_network);
1384 wps_pbc = g_supplicant_network_is_wps_pbc(supplicant_network);
1385 wps_ready = g_supplicant_network_is_wps_active(supplicant_network);
1386 wps_advertizing = g_supplicant_network_is_wps_advertizing(
1387 supplicant_network);
1388 mode = g_supplicant_network_get_mode(supplicant_network);
1393 ssid = g_supplicant_network_get_ssid(supplicant_network, &ssid_len);
1395 network = connman_device_get_network(wifi->device, identifier);
1397 if (network == NULL) {
1398 network = connman_network_create(identifier,
1399 CONNMAN_NETWORK_TYPE_WIFI);
1400 if (network == NULL)
1403 connman_network_set_index(network, wifi->index);
1405 if (connman_device_add_network(wifi->device, network) < 0) {
1406 connman_network_unref(network);
1410 wifi->networks = g_slist_append(wifi->networks, network);
1413 if (name != NULL && name[0] != '\0')
1414 connman_network_set_name(network, name);
1416 connman_network_set_blob(network, "WiFi.SSID",
1418 connman_network_set_string(network, "WiFi.Security", security);
1419 connman_network_set_strength(network,
1420 calculate_strength(supplicant_network));
1421 connman_network_set_bool(network, "WiFi.WPS", wps);
1424 /* Is AP advertizing for WPS association?
1425 * If so, we decide to use WPS by default */
1426 if (wps_ready == TRUE && wps_pbc == TRUE &&
1427 wps_advertizing == TRUE)
1428 connman_network_set_bool(network, "WiFi.UseWPS", TRUE);
1431 connman_network_set_frequency(network,
1432 g_supplicant_network_get_frequency(supplicant_network));
1434 connman_network_set_available(network, TRUE);
1435 connman_network_set_string(network, "WiFi.Mode", mode);
1438 connman_network_set_group(network, group);
1440 if (wifi->hidden != NULL) {
1441 if (wifi->hidden->ssid_len == ssid_len &&
1442 memcmp(wifi->hidden->ssid, ssid,
1444 connman_network_connect_hidden(network,
1445 wifi->hidden->identity,
1446 wifi->hidden->passphrase);
1447 hidden_free(wifi->hidden);
1448 wifi->hidden = NULL;
1453 static void network_removed(GSupplicantNetwork *network)
1455 GSupplicantInterface *interface;
1456 struct wifi_data *wifi;
1457 const char *name, *identifier;
1458 struct connman_network *connman_network;
1460 interface = g_supplicant_network_get_interface(network);
1461 wifi = g_supplicant_interface_get_data(interface);
1462 identifier = g_supplicant_network_get_identifier(network);
1463 name = g_supplicant_network_get_name(network);
1465 DBG("name %s", name);
1470 connman_network = connman_device_get_network(wifi->device, identifier);
1471 if (connman_network == NULL)
1474 wifi->networks = g_slist_remove(wifi->networks, connman_network);
1476 connman_device_remove_network(wifi->device, connman_network);
1477 connman_network_unref(connman_network);
1480 static void network_changed(GSupplicantNetwork *network, const char *property)
1482 GSupplicantInterface *interface;
1483 struct wifi_data *wifi;
1484 const char *name, *identifier;
1485 struct connman_network *connman_network;
1487 interface = g_supplicant_network_get_interface(network);
1488 wifi = g_supplicant_interface_get_data(interface);
1489 identifier = g_supplicant_network_get_identifier(network);
1490 name = g_supplicant_network_get_name(network);
1492 DBG("name %s", name);
1497 connman_network = connman_device_get_network(wifi->device, identifier);
1498 if (connman_network == NULL)
1501 if (g_str_equal(property, "Signal") == TRUE) {
1502 connman_network_set_strength(connman_network,
1503 calculate_strength(network));
1504 connman_network_update(connman_network);
1508 static void debug(const char *str)
1510 if (getenv("CONNMAN_SUPPLICANT_DEBUG"))
1511 connman_debug("%s", str);
1514 static const GSupplicantCallbacks callbacks = {
1515 .system_ready = system_ready,
1516 .system_killed = system_killed,
1517 .interface_added = interface_added,
1518 .interface_state = interface_state,
1519 .interface_removed = interface_removed,
1520 .scan_started = scan_started,
1521 .scan_finished = scan_finished,
1522 .network_added = network_added,
1523 .network_removed = network_removed,
1524 .network_changed = network_changed,
1529 static int tech_probe(struct connman_technology *technology)
1531 wifi_technology = technology;
1536 static void tech_remove(struct connman_technology *technology)
1538 wifi_technology = NULL;
1541 struct wifi_tethering_info {
1542 struct wifi_data *wifi;
1543 struct connman_technology *technology;
1545 GSupplicantSSID *ssid;
1548 static GSupplicantSSID *ssid_ap_init(const char *ssid, const char *passphrase)
1550 GSupplicantSSID *ap;
1552 ap = g_try_malloc0(sizeof(GSupplicantSSID));
1556 ap->mode = G_SUPPLICANT_MODE_MASTER;
1558 ap->ssid_len = strlen(ssid);
1562 if (passphrase == NULL || strlen(passphrase) == 0) {
1563 ap->security = G_SUPPLICANT_SECURITY_NONE;
1564 ap->passphrase = NULL;
1566 ap->security = G_SUPPLICANT_SECURITY_PSK;
1567 ap->protocol = G_SUPPLICANT_PROTO_RSN;
1568 ap->pairwise_cipher = G_SUPPLICANT_PAIRWISE_CCMP;
1569 ap->group_cipher = G_SUPPLICANT_GROUP_CCMP;
1570 ap->passphrase = passphrase;
1576 static void ap_start_callback(int result, GSupplicantInterface *interface,
1579 struct wifi_tethering_info *info = user_data;
1581 DBG("result %d index %d bridge %s",
1582 result, info->wifi->index, info->wifi->bridge);
1585 connman_inet_remove_from_bridge(info->wifi->index,
1586 info->wifi->bridge);
1587 connman_technology_tethering_notify(info->technology, FALSE);
1590 g_free(info->ifname);
1594 static void ap_create_callback(int result,
1595 GSupplicantInterface *interface,
1598 struct wifi_tethering_info *info = user_data;
1600 DBG("result %d ifname %s", result,
1601 g_supplicant_interface_get_ifname(interface));
1604 connman_inet_remove_from_bridge(info->wifi->index,
1605 info->wifi->bridge);
1606 connman_technology_tethering_notify(info->technology, FALSE);
1608 g_free(info->ifname);
1613 info->wifi->interface = interface;
1614 g_supplicant_interface_set_data(interface, info->wifi);
1616 if (g_supplicant_interface_set_apscan(interface, 2) < 0)
1617 connman_error("Failed to set interface ap_scan property");
1619 g_supplicant_interface_connect(interface, info->ssid,
1620 ap_start_callback, info);
1623 static void sta_remove_callback(int result,
1624 GSupplicantInterface *interface,
1627 struct wifi_tethering_info *info = user_data;
1628 const char *driver = connman_option_get_string("wifi");
1630 DBG("ifname %s result %d ", info->ifname, result);
1633 info->wifi->tethering = TRUE;
1635 g_free(info->ifname);
1640 info->wifi->interface = NULL;
1642 connman_technology_tethering_notify(info->technology, TRUE);
1644 g_supplicant_interface_create(info->ifname, driver, info->wifi->bridge,
1649 static int tech_set_tethering(struct connman_technology *technology,
1650 const char *identifier, const char *passphrase,
1651 const char *bridge, connman_bool_t enabled)
1654 GSupplicantInterface *interface;
1655 struct wifi_data *wifi;
1656 struct wifi_tethering_info *info;
1663 if (enabled == FALSE) {
1664 for (list = iface_list; list; list = list->next) {
1667 if (wifi->tethering == TRUE) {
1668 wifi->tethering = FALSE;
1670 connman_inet_remove_from_bridge(wifi->index,
1672 wifi->bridged = FALSE;
1676 connman_technology_tethering_notify(technology, FALSE);
1681 for (list = iface_list; list; list = list->next) {
1684 interface = wifi->interface;
1686 if (interface == NULL)
1689 ifname = g_supplicant_interface_get_ifname(wifi->interface);
1691 mode = g_supplicant_interface_get_mode(interface);
1692 if ((mode & G_SUPPLICANT_CAPABILITY_MODE_AP) == 0) {
1693 DBG("%s does not support AP mode", ifname);
1697 info = g_try_malloc0(sizeof(struct wifi_tethering_info));
1702 info->technology = technology;
1703 info->wifi->bridge = bridge;
1704 info->ssid = ssid_ap_init(identifier, passphrase);
1705 if (info->ssid == NULL) {
1709 info->ifname = g_strdup(ifname);
1710 if (info->ifname == NULL) {
1715 info->wifi->tethering = TRUE;
1717 err = g_supplicant_interface_remove(interface,
1718 sta_remove_callback,
1727 static void regdom_callback(void *user_data)
1729 char *alpha2 = user_data;
1733 if (wifi_technology == NULL)
1736 connman_technology_regdom_notify(wifi_technology, alpha2);
1739 static int tech_set_regdom(struct connman_technology *technology, const char *alpha2)
1741 return g_supplicant_set_country(alpha2, regdom_callback, alpha2);
1744 static struct connman_technology_driver tech_driver = {
1746 .type = CONNMAN_SERVICE_TYPE_WIFI,
1747 .probe = tech_probe,
1748 .remove = tech_remove,
1749 .set_tethering = tech_set_tethering,
1750 .set_regdom = tech_set_regdom,
1753 static int wifi_init(void)
1757 err = connman_network_driver_register(&network_driver);
1761 err = g_supplicant_register(&callbacks);
1763 connman_network_driver_unregister(&network_driver);
1767 err = connman_technology_driver_register(&tech_driver);
1769 g_supplicant_unregister(&callbacks);
1770 connman_network_driver_unregister(&network_driver);
1777 static void wifi_exit(void)
1781 connman_technology_driver_unregister(&tech_driver);
1783 g_supplicant_unregister(&callbacks);
1785 connman_network_driver_unregister(&network_driver);
1788 CONNMAN_PLUGIN_DEFINE(wifi, "WiFi interface plugin", VERSION,
1789 CONNMAN_PLUGIN_PRIORITY_DEFAULT, wifi_init, wifi_exit)