5 * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31 #include <sys/ioctl.h>
32 #include <sys/socket.h>
33 #include <linux/if_arp.h>
34 #include <linux/wireless.h>
35 #include <net/ethernet.h>
38 #define IFF_LOWER_UP 0x10000
41 #include <dbus/dbus.h>
44 #define CONNMAN_API_SUBJECT_TO_CHANGE
45 #include <connman/plugin.h>
46 #include <connman/inet.h>
47 #include <connman/device.h>
48 #include <connman/rtnl.h>
49 #include <connman/technology.h>
50 #include <connman/log.h>
51 #include <connman/option.h>
52 #include <connman/storage.h>
54 #include <gsupplicant/gsupplicant.h>
56 #define CLEANUP_TIMEOUT 8 /* in seconds */
57 #define INACTIVE_TIMEOUT 12 /* in seconds */
58 #define MAXIMUM_RETRIES 4
60 struct connman_technology *wifi_technology = NULL;
62 struct hidden_params {
64 unsigned int ssid_len;
71 struct connman_device *device;
72 struct connman_network *network;
73 struct connman_network *pending_network;
75 GSupplicantInterface *interface;
76 GSupplicantState state;
77 connman_bool_t connected;
78 connman_bool_t disconnecting;
79 connman_bool_t tethering;
80 connman_bool_t bridged;
86 struct hidden_params *hidden;
89 static GList *iface_list = NULL;
91 static void handle_tethering(struct wifi_data *wifi)
93 if (wifi->tethering == FALSE)
96 if (wifi->bridge == NULL)
99 if (wifi->bridged == TRUE)
102 DBG("index %d bridge %s", wifi->index, wifi->bridge);
104 if (connman_inet_add_to_bridge(wifi->index, wifi->bridge) < 0)
107 wifi->bridged = TRUE;
110 static void wifi_newlink(unsigned flags, unsigned change, void *user_data)
112 struct connman_device *device = user_data;
113 struct wifi_data *wifi = connman_device_get_data(device);
115 DBG("index %d flags %d change %d", wifi->index, flags, change);
120 if ((wifi->flags & IFF_UP) != (flags & IFF_UP)) {
124 DBG("interface down");
127 if ((wifi->flags & IFF_LOWER_UP) != (flags & IFF_LOWER_UP)) {
128 if (flags & IFF_LOWER_UP) {
131 handle_tethering(wifi);
139 static int wifi_probe(struct connman_device *device)
141 struct wifi_data *wifi;
143 DBG("device %p", device);
145 wifi = g_try_new0(struct wifi_data, 1);
149 wifi->connected = FALSE;
150 wifi->disconnecting = FALSE;
151 wifi->tethering = FALSE;
152 wifi->bridged = FALSE;
154 wifi->state = G_SUPPLICANT_STATE_INACTIVE;
156 connman_device_set_data(device, wifi);
157 wifi->device = connman_device_ref(device);
159 wifi->index = connman_device_get_index(device);
162 wifi->watch = connman_rtnl_add_newlink_watch(wifi->index,
163 wifi_newlink, device);
165 iface_list = g_list_append(iface_list, wifi);
170 static void remove_networks(struct connman_device *device,
171 struct wifi_data *wifi)
175 for (list = wifi->networks; list != NULL; list = list->next) {
176 struct connman_network *network = list->data;
178 connman_device_remove_network(device, network);
179 connman_network_unref(network);
182 g_slist_free(wifi->networks);
183 wifi->networks = NULL;
186 static void wifi_remove(struct connman_device *device)
188 struct wifi_data *wifi = connman_device_get_data(device);
190 DBG("device %p wifi %p", device, wifi);
195 iface_list = g_list_remove(iface_list, wifi);
197 remove_networks(device, wifi);
199 connman_device_set_powered(device, FALSE);
200 connman_device_set_data(device, NULL);
201 connman_device_unref(wifi->device);
202 connman_rtnl_remove_watch(wifi->watch);
204 g_supplicant_interface_set_data(wifi->interface, NULL);
206 g_free(wifi->identifier);
210 static void interface_create_callback(int result,
211 GSupplicantInterface *interface,
214 struct wifi_data *wifi = user_data;
216 DBG("result %d ifname %s, wifi %p", result,
217 g_supplicant_interface_get_ifname(interface),
220 if (result < 0 || wifi == NULL)
223 wifi->interface = interface;
224 g_supplicant_interface_set_data(interface, wifi);
226 if (g_supplicant_interface_get_ready(interface) == FALSE)
229 DBG("interface is ready wifi %p tethering %d", wifi, wifi->tethering);
231 if (wifi->device == NULL) {
232 connman_error("WiFi device not set");
236 connman_device_set_powered(wifi->device, TRUE);
239 static int wifi_enable(struct connman_device *device)
241 struct wifi_data *wifi = connman_device_get_data(device);
242 const char *interface = connman_device_get_string(device, "Interface");
243 const char *driver = connman_option_get_string("wifi");
246 DBG("device %p %p", device, wifi);
248 ret = g_supplicant_interface_create(interface, driver, NULL,
249 interface_create_callback,
257 static int wifi_disable(struct connman_device *device)
259 struct wifi_data *wifi = connman_device_get_data(device);
262 DBG("device %p", device);
264 wifi->connected = FALSE;
265 wifi->disconnecting = FALSE;
267 if (wifi->pending_network != NULL)
268 wifi->pending_network = NULL;
270 remove_networks(device, wifi);
272 ret = g_supplicant_interface_remove(wifi->interface, NULL, NULL);
279 static void hidden_free(struct hidden_params *hidden)
284 g_free(hidden->identity);
285 g_free(hidden->passphrase);
289 static void scan_callback(int result, GSupplicantInterface *interface,
292 struct connman_device *device = user_data;
293 struct wifi_data *wifi = connman_device_get_data(device);
295 DBG("result %d", result);
297 if (wifi != NULL && wifi->hidden != NULL) {
298 hidden_free(wifi->hidden);
303 connman_device_reset_scanning(device);
305 connman_device_set_scanning(device, FALSE);
306 connman_device_unref(device);
309 static int add_scan_param(gchar *hex_ssid, int freq,
310 GSupplicantScanParams *scan_data,
311 int driver_max_scan_ssids)
315 if (driver_max_scan_ssids > scan_data->num_ssids && hex_ssid != NULL) {
317 unsigned int j = 0, hex;
318 size_t hex_ssid_len = strlen(hex_ssid);
320 ssid = g_try_malloc0(hex_ssid_len / 2);
324 for (i = 0; i < hex_ssid_len; i += 2) {
325 sscanf(hex_ssid + i, "%02x", &hex);
329 memcpy(scan_data->ssids[scan_data->num_ssids].ssid, ssid, j);
330 scan_data->ssids[scan_data->num_ssids].ssid_len = j;
331 scan_data->num_ssids++;
336 /* Don't add duplicate entries */
337 for (i = 0; i < G_SUPPLICANT_MAX_FAST_SCAN; i++) {
338 if (scan_data->freqs[i] == 0) {
339 scan_data->freqs[i] = freq;
341 } else if (scan_data->freqs[i] == freq)
348 struct last_connected {
354 static gint sort_entry(gconstpointer a, gconstpointer b, gpointer user_data)
356 GTimeVal *aval = (GTimeVal *)a;
357 GTimeVal *bval = (GTimeVal *)b;
359 /* Note that the sort order is descending */
360 if (aval->tv_sec < bval->tv_sec)
363 if (aval->tv_sec > bval->tv_sec)
369 static void free_entry(gpointer data)
371 struct last_connected *entry = data;
377 static int get_latest_connections(int max_ssids,
378 GSupplicantScanParams *scan_data)
381 GSequence *latest_list;
382 struct last_connected *entry;
391 latest_list = g_sequence_new(free_entry);
392 if (latest_list == NULL)
395 services = connman_storage_get_services();
396 for (i = 0; services && services[i]; i++) {
397 if (strncmp(services[i], "wifi_", 5) != 0)
400 keyfile = connman_storage_load_service(services[i]);
402 str = g_key_file_get_string(keyfile,
403 services[i], "Favorite", NULL);
404 if (str == NULL || g_strcmp0(str, "true")) {
407 g_key_file_free(keyfile);
412 str = g_key_file_get_string(keyfile,
413 services[i], "AutoConnect", NULL);
414 if (str == NULL || g_strcmp0(str, "true")) {
417 g_key_file_free(keyfile);
422 str = g_key_file_get_string(keyfile,
423 services[i], "Modified", NULL);
425 g_time_val_from_iso8601(str, &modified);
429 ssid = g_key_file_get_string(keyfile,
430 services[i], "SSID", NULL);
432 freq = g_key_file_get_integer(keyfile, services[i],
435 entry = g_try_new(struct last_connected, 1);
437 g_sequence_free(latest_list);
438 g_key_file_free(keyfile);
444 entry->modified = modified;
447 g_sequence_insert_sorted(latest_list, entry,
453 g_key_file_free(keyfile);
456 g_strfreev(services);
458 num_ssids = num_ssids > G_SUPPLICANT_MAX_FAST_SCAN ?
459 G_SUPPLICANT_MAX_FAST_SCAN : num_ssids;
461 iter = g_sequence_get_begin_iter(latest_list);
463 for (i = 0; i < num_ssids; i++) {
464 entry = g_sequence_get(iter);
466 DBG("ssid %s freq %d modified %lu", entry->ssid, entry->freq,
467 entry->modified.tv_sec);
469 add_scan_param(entry->ssid, entry->freq, scan_data, max_ssids);
471 iter = g_sequence_iter_next(iter);
474 g_sequence_free(latest_list);
478 static int wifi_scan(struct connman_device *device)
480 struct wifi_data *wifi = connman_device_get_data(device);
483 DBG("device %p %p", device, wifi->interface);
485 if (wifi->tethering == TRUE)
488 connman_device_ref(device);
489 ret = g_supplicant_interface_scan(wifi->interface, NULL,
490 scan_callback, device);
492 connman_device_set_scanning(device, TRUE);
494 connman_device_unref(device);
499 static int wifi_scan_fast(struct connman_device *device)
501 struct wifi_data *wifi = connman_device_get_data(device);
502 GSupplicantScanParams *scan_params = NULL;
504 int driver_max_ssids = 0;
506 DBG("device %p %p", device, wifi->interface);
508 if (wifi->tethering == TRUE)
511 driver_max_ssids = g_supplicant_interface_get_max_scan_ssids(
513 DBG("max ssids %d", driver_max_ssids);
514 if (driver_max_ssids == 0)
515 return wifi_scan(device);
517 scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
518 if (scan_params == NULL)
521 ret = get_latest_connections(driver_max_ssids, scan_params);
524 return wifi_scan(device);
527 connman_device_ref(device);
528 ret = g_supplicant_interface_scan(wifi->interface, scan_params,
529 scan_callback, device);
531 connman_device_set_scanning(device, TRUE);
534 connman_device_unref(device);
540 static int wifi_scan_hidden(struct connman_device *device,
541 const char *ssid, unsigned int ssid_len,
542 const char *identity, const char* passphrase)
544 struct wifi_data *wifi = connman_device_get_data(device);
545 GSupplicantScanParams *scan_params = NULL;
546 struct hidden_params *hidden;
549 DBG("hidden SSID %s", ssid);
551 if (wifi->tethering == TRUE || wifi->hidden != NULL)
554 if (ssid == NULL || ssid_len == 0 || ssid_len > 32)
557 scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
558 if (scan_params == NULL)
560 memcpy(scan_params->ssids[0].ssid, ssid, ssid_len);
561 scan_params->ssids[0].ssid_len = ssid_len;
562 scan_params->num_ssids = 1;
564 hidden = g_try_new0(struct hidden_params, 1);
565 if (hidden == NULL) {
569 memcpy(hidden->ssid, ssid, ssid_len);
570 hidden->ssid_len = ssid_len;
571 hidden->identity = g_strdup(identity);
572 hidden->passphrase = g_strdup(passphrase);
573 wifi->hidden = hidden;
575 connman_device_ref(device);
576 ret = g_supplicant_interface_scan(wifi->interface, scan_params,
577 scan_callback, device);
579 connman_device_set_scanning(device, TRUE);
581 connman_device_unref(device);
583 hidden_free(wifi->hidden);
590 static struct connman_device_driver wifi_ng_driver = {
592 .type = CONNMAN_DEVICE_TYPE_WIFI,
593 .priority = CONNMAN_DEVICE_PRIORITY_LOW,
595 .remove = wifi_remove,
596 .enable = wifi_enable,
597 .disable = wifi_disable,
599 .scan_fast = wifi_scan_fast,
600 .scan_hidden = wifi_scan_hidden,
603 static void system_ready(void)
607 if (connman_device_driver_register(&wifi_ng_driver) < 0)
608 connman_error("Failed to register WiFi driver");
611 static void system_killed(void)
615 connman_device_driver_unregister(&wifi_ng_driver);
618 static int network_probe(struct connman_network *network)
620 DBG("network %p", network);
625 static void network_remove(struct connman_network *network)
627 struct connman_device *device = connman_network_get_device(network);
628 struct wifi_data *wifi;
630 DBG("network %p", network);
632 wifi = connman_device_get_data(device);
636 if (wifi->network != network)
639 wifi->network = NULL;
642 static void connect_callback(int result, GSupplicantInterface *interface,
645 struct connman_network *network = user_data;
647 DBG("network %p result %d", network, result);
649 if (result == -ENOKEY) {
650 connman_network_set_error(network,
651 CONNMAN_NETWORK_ERROR_INVALID_KEY);
652 } else if (result < 0) {
653 connman_network_set_error(network,
654 CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
658 static GSupplicantSecurity network_security(const char *security)
660 if (g_str_equal(security, "none") == TRUE)
661 return G_SUPPLICANT_SECURITY_NONE;
662 else if (g_str_equal(security, "wep") == TRUE)
663 return G_SUPPLICANT_SECURITY_WEP;
664 else if (g_str_equal(security, "psk") == TRUE)
665 return G_SUPPLICANT_SECURITY_PSK;
666 else if (g_str_equal(security, "wpa") == TRUE)
667 return G_SUPPLICANT_SECURITY_PSK;
668 else if (g_str_equal(security, "rsn") == TRUE)
669 return G_SUPPLICANT_SECURITY_PSK;
670 else if (g_str_equal(security, "ieee8021x") == TRUE)
671 return G_SUPPLICANT_SECURITY_IEEE8021X;
673 return G_SUPPLICANT_SECURITY_UNKNOWN;
676 static void ssid_init(GSupplicantSSID *ssid, struct connman_network *network)
678 const char *security, *passphrase, *agent_passphrase;
680 memset(ssid, 0, sizeof(*ssid));
681 ssid->mode = G_SUPPLICANT_MODE_INFRA;
682 ssid->ssid = connman_network_get_blob(network, "WiFi.SSID",
685 security = connman_network_get_string(network, "WiFi.Security");
686 ssid->security = network_security(security);
687 passphrase = connman_network_get_string(network,
689 if (passphrase == NULL || strlen(passphrase) == 0) {
691 /* Use agent provided passphrase as a fallback */
692 agent_passphrase = connman_network_get_string(network,
693 "WiFi.AgentPassphrase");
695 if (agent_passphrase == NULL || strlen(agent_passphrase) == 0)
696 ssid->passphrase = NULL;
698 ssid->passphrase = agent_passphrase;
700 ssid->passphrase = passphrase;
702 ssid->eap = connman_network_get_string(network, "WiFi.EAP");
705 * If our private key password is unset,
706 * we use the supplied passphrase. That is needed
707 * for PEAP where 2 passphrases (identity and client
708 * cert may have to be provided.
710 if (connman_network_get_string(network,
711 "WiFi.PrivateKeyPassphrase") == NULL)
712 connman_network_set_string(network,
713 "WiFi.PrivateKeyPassphrase",
715 /* We must have an identity for both PEAP and TLS */
716 ssid->identity = connman_network_get_string(network, "WiFi.Identity");
718 /* Use agent provided identity as a fallback */
719 if (ssid->identity == NULL || strlen(ssid->identity) == 0)
720 ssid->identity = connman_network_get_string(network,
721 "WiFi.AgentIdentity");
723 ssid->ca_cert_path = connman_network_get_string(network,
725 ssid->client_cert_path = connman_network_get_string(network,
726 "WiFi.ClientCertFile");
727 ssid->private_key_path = connman_network_get_string(network,
728 "WiFi.PrivateKeyFile");
729 ssid->private_key_passphrase = connman_network_get_string(network,
730 "WiFi.PrivateKeyPassphrase");
731 ssid->phase2_auth = connman_network_get_string(network, "WiFi.Phase2");
733 ssid->use_wps = connman_network_get_bool(network, "WiFi.UseWPS");
734 ssid->pin_wps = connman_network_get_string(network, "WiFi.PinWPS");
738 static int network_connect(struct connman_network *network)
740 struct connman_device *device = connman_network_get_device(network);
741 struct wifi_data *wifi;
742 GSupplicantInterface *interface;
743 GSupplicantSSID *ssid;
745 DBG("network %p", network);
750 wifi = connman_device_get_data(device);
754 ssid = g_try_malloc0(sizeof(GSupplicantSSID));
758 interface = wifi->interface;
760 ssid_init(ssid, network);
762 if (wifi->disconnecting == TRUE)
763 wifi->pending_network = network;
765 wifi->network = network;
768 return g_supplicant_interface_connect(interface, ssid,
769 connect_callback, network);
775 static void disconnect_callback(int result, GSupplicantInterface *interface,
778 struct wifi_data *wifi = user_data;
780 if (wifi->network != NULL) {
782 * if result < 0 supplican return an error because
783 * the network is not current.
784 * we wont receive G_SUPPLICANT_STATE_DISCONNECTED since it
785 * failed, call connman_network_set_connected to report
786 * disconnect is completed.
789 connman_network_set_connected(wifi->network, FALSE);
792 wifi->network = NULL;
794 wifi->disconnecting = FALSE;
796 if (wifi->pending_network != NULL) {
797 network_connect(wifi->pending_network);
798 wifi->pending_network = NULL;
803 static int network_disconnect(struct connman_network *network)
805 struct connman_device *device = connman_network_get_device(network);
806 struct wifi_data *wifi;
809 DBG("network %p", network);
811 wifi = connman_device_get_data(device);
812 if (wifi == NULL || wifi->interface == NULL)
815 connman_network_set_associating(network, FALSE);
817 if (wifi->disconnecting == TRUE)
820 wifi->disconnecting = TRUE;
822 err = g_supplicant_interface_disconnect(wifi->interface,
823 disconnect_callback, wifi);
825 wifi->disconnecting = FALSE;
830 static struct connman_network_driver network_driver = {
832 .type = CONNMAN_NETWORK_TYPE_WIFI,
833 .priority = CONNMAN_NETWORK_PRIORITY_LOW,
834 .probe = network_probe,
835 .remove = network_remove,
836 .connect = network_connect,
837 .disconnect = network_disconnect,
840 static void interface_added(GSupplicantInterface *interface)
842 const char *ifname = g_supplicant_interface_get_ifname(interface);
843 const char *driver = g_supplicant_interface_get_driver(interface);
844 struct wifi_data *wifi;
846 wifi = g_supplicant_interface_get_data(interface);
849 * We can get here with a NULL wifi pointer when
850 * the interface added signal is sent before the
851 * interface creation callback is called.
856 DBG("ifname %s driver %s wifi %p tethering %d",
857 ifname, driver, wifi, wifi->tethering);
859 if (wifi->device == NULL) {
860 connman_error("WiFi device not set");
864 connman_device_set_powered(wifi->device, TRUE);
866 if (wifi->tethering == TRUE)
870 static connman_bool_t is_idle(struct wifi_data *wifi)
872 DBG("state %d", wifi->state);
874 switch (wifi->state) {
875 case G_SUPPLICANT_STATE_UNKNOWN:
876 case G_SUPPLICANT_STATE_DISCONNECTED:
877 case G_SUPPLICANT_STATE_INACTIVE:
878 case G_SUPPLICANT_STATE_SCANNING:
881 case G_SUPPLICANT_STATE_AUTHENTICATING:
882 case G_SUPPLICANT_STATE_ASSOCIATING:
883 case G_SUPPLICANT_STATE_ASSOCIATED:
884 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
885 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
886 case G_SUPPLICANT_STATE_COMPLETED:
893 static connman_bool_t is_idle_wps(GSupplicantInterface *interface,
894 struct wifi_data *wifi)
896 /* First, let's check if WPS processing did not went wrong */
897 if (g_supplicant_interface_get_wps_state(interface) ==
898 G_SUPPLICANT_WPS_STATE_FAIL)
901 /* Unlike normal connection, being associated while processing wps
902 * actually means that we are idling. */
903 switch (wifi->state) {
904 case G_SUPPLICANT_STATE_UNKNOWN:
905 case G_SUPPLICANT_STATE_DISCONNECTED:
906 case G_SUPPLICANT_STATE_INACTIVE:
907 case G_SUPPLICANT_STATE_SCANNING:
908 case G_SUPPLICANT_STATE_ASSOCIATED:
910 case G_SUPPLICANT_STATE_AUTHENTICATING:
911 case G_SUPPLICANT_STATE_ASSOCIATING:
912 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
913 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
914 case G_SUPPLICANT_STATE_COMPLETED:
921 static connman_bool_t handle_wps_completion(GSupplicantInterface *interface,
922 struct connman_network *network,
923 struct connman_device *device,
924 struct wifi_data *wifi)
928 wps = connman_network_get_bool(network, "WiFi.UseWPS");
930 const unsigned char *ssid, *wps_ssid;
931 unsigned int ssid_len, wps_ssid_len;
934 /* Checking if we got associated with requested
936 ssid = connman_network_get_blob(network, "WiFi.SSID",
939 wps_ssid = g_supplicant_interface_get_wps_ssid(
940 interface, &wps_ssid_len);
942 if (wps_ssid == NULL || wps_ssid_len != ssid_len ||
943 memcmp(ssid, wps_ssid, ssid_len) != 0) {
944 connman_network_set_associating(network, FALSE);
945 g_supplicant_interface_disconnect(wifi->interface,
946 disconnect_callback, wifi);
950 wps_key = g_supplicant_interface_get_wps_key(interface);
951 connman_network_set_string(network, "WiFi.Passphrase",
954 connman_network_set_string(network, "WiFi.PinWPS", NULL);
960 static connman_bool_t handle_4way_handshake_failure(GSupplicantInterface *interface,
961 struct connman_network *network,
962 struct wifi_data *wifi)
964 if (wifi->state != G_SUPPLICANT_STATE_4WAY_HANDSHAKE)
969 if (wifi->retries < MAXIMUM_RETRIES)
972 connman_network_set_error(network, CONNMAN_NETWORK_ERROR_INVALID_KEY);
977 static void interface_state(GSupplicantInterface *interface)
979 struct connman_network *network;
980 struct connman_device *device;
981 struct wifi_data *wifi;
982 GSupplicantState state = g_supplicant_interface_get_state(interface);
985 wifi = g_supplicant_interface_get_data(interface);
987 DBG("wifi %p interface state %d", wifi, state);
992 network = wifi->network;
993 device = wifi->device;
995 if (device == NULL || network == NULL)
999 case G_SUPPLICANT_STATE_SCANNING:
1002 case G_SUPPLICANT_STATE_AUTHENTICATING:
1003 case G_SUPPLICANT_STATE_ASSOCIATING:
1004 connman_network_set_associating(network, TRUE);
1007 case G_SUPPLICANT_STATE_COMPLETED:
1008 if (handle_wps_completion(interface, network, device, wifi) ==
1012 /* reset scan trigger and schedule background scan */
1013 connman_device_schedule_scan(device);
1015 connman_network_set_connected(network, TRUE);
1018 case G_SUPPLICANT_STATE_DISCONNECTED:
1020 * If we're in one of the idle modes, we have
1021 * not started association yet and thus setting
1022 * those ones to FALSE could cancel an association
1025 wps = connman_network_get_bool(network, "WiFi.UseWPS");
1027 if (is_idle_wps(interface, wifi) == TRUE)
1033 /* If previous state was 4way-handshake, then
1034 * it's either: psk was incorrect and thus we retry
1035 * or if we reach the maximum retries we declare the
1037 if (handle_4way_handshake_failure(interface,
1038 network, wifi) == TRUE)
1041 /* We disable the selected network, if not then
1042 * wpa_supplicant will loop retrying */
1043 if (g_supplicant_interface_enable_selected_network(interface,
1045 DBG("Could not disables selected network");
1047 connman_network_set_associating(network, FALSE);
1048 connman_network_set_connected(network, FALSE);
1051 case G_SUPPLICANT_STATE_INACTIVE:
1052 connman_network_set_associating(network, FALSE);
1055 case G_SUPPLICANT_STATE_UNKNOWN:
1056 case G_SUPPLICANT_STATE_ASSOCIATED:
1057 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
1058 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
1062 wifi->state = state;
1067 static void interface_removed(GSupplicantInterface *interface)
1069 const char *ifname = g_supplicant_interface_get_ifname(interface);
1070 struct wifi_data *wifi;
1072 DBG("ifname %s", ifname);
1074 wifi = g_supplicant_interface_get_data(interface);
1076 if (wifi != NULL && wifi->tethering == TRUE)
1079 if (wifi == NULL || wifi->device == NULL) {
1080 connman_error("Wrong wifi pointer");
1084 wifi->interface = NULL;
1085 connman_device_set_powered(wifi->device, FALSE);
1088 static void scan_started(GSupplicantInterface *interface)
1093 static void scan_finished(GSupplicantInterface *interface)
1098 static unsigned char calculate_strength(GSupplicantNetwork *supplicant_network)
1100 unsigned char strength;
1102 strength = 120 + g_supplicant_network_get_signal(supplicant_network);
1109 static void network_added(GSupplicantNetwork *supplicant_network)
1111 struct connman_network *network;
1112 GSupplicantInterface *interface;
1113 struct wifi_data *wifi;
1114 const char *name, *identifier, *security, *group, *mode;
1115 const unsigned char *ssid;
1116 unsigned int ssid_len;
1121 interface = g_supplicant_network_get_interface(supplicant_network);
1122 wifi = g_supplicant_interface_get_data(interface);
1123 name = g_supplicant_network_get_name(supplicant_network);
1124 identifier = g_supplicant_network_get_identifier(supplicant_network);
1125 security = g_supplicant_network_get_security(supplicant_network);
1126 group = g_supplicant_network_get_identifier(supplicant_network);
1127 wps = g_supplicant_network_get_wps(supplicant_network);
1128 mode = g_supplicant_network_get_mode(supplicant_network);
1133 ssid = g_supplicant_network_get_ssid(supplicant_network, &ssid_len);
1135 network = connman_device_get_network(wifi->device, identifier);
1137 if (network == NULL) {
1138 network = connman_network_create(identifier,
1139 CONNMAN_NETWORK_TYPE_WIFI);
1140 if (network == NULL)
1143 connman_network_set_index(network, wifi->index);
1145 if (connman_device_add_network(wifi->device, network) < 0) {
1146 connman_network_unref(network);
1150 wifi->networks = g_slist_append(wifi->networks, network);
1153 if (name != NULL && name[0] != '\0')
1154 connman_network_set_name(network, name);
1156 connman_network_set_blob(network, "WiFi.SSID",
1158 connman_network_set_string(network, "WiFi.Security", security);
1159 connman_network_set_strength(network,
1160 calculate_strength(supplicant_network));
1161 connman_network_set_bool(network, "WiFi.WPS", wps);
1163 connman_network_set_frequency(network,
1164 g_supplicant_network_get_frequency(supplicant_network));
1166 connman_network_set_available(network, TRUE);
1167 connman_network_set_string(network, "WiFi.Mode", mode);
1170 connman_network_set_group(network, group);
1172 if (wifi->hidden != NULL) {
1173 if (wifi->hidden->ssid_len == ssid_len &&
1174 memcmp(wifi->hidden->ssid, ssid,
1176 connman_network_connect_hidden(network,
1177 wifi->hidden->identity,
1178 wifi->hidden->passphrase);
1179 hidden_free(wifi->hidden);
1180 wifi->hidden = NULL;
1185 static void network_removed(GSupplicantNetwork *network)
1187 GSupplicantInterface *interface;
1188 struct wifi_data *wifi;
1189 const char *name, *identifier;
1190 struct connman_network *connman_network;
1192 interface = g_supplicant_network_get_interface(network);
1193 wifi = g_supplicant_interface_get_data(interface);
1194 identifier = g_supplicant_network_get_identifier(network);
1195 name = g_supplicant_network_get_name(network);
1197 DBG("name %s", name);
1202 connman_network = connman_device_get_network(wifi->device, identifier);
1203 if (connman_network == NULL)
1206 wifi->networks = g_slist_remove(wifi->networks, connman_network);
1208 connman_device_remove_network(wifi->device, connman_network);
1209 connman_network_unref(connman_network);
1212 static void network_changed(GSupplicantNetwork *network, const char *property)
1214 GSupplicantInterface *interface;
1215 struct wifi_data *wifi;
1216 const char *name, *identifier;
1217 struct connman_network *connman_network;
1219 interface = g_supplicant_network_get_interface(network);
1220 wifi = g_supplicant_interface_get_data(interface);
1221 identifier = g_supplicant_network_get_identifier(network);
1222 name = g_supplicant_network_get_name(network);
1224 DBG("name %s", name);
1229 connman_network = connman_device_get_network(wifi->device, identifier);
1230 if (connman_network == NULL)
1233 if (g_str_equal(property, "Signal") == TRUE) {
1234 connman_network_set_strength(connman_network,
1235 calculate_strength(network));
1236 connman_network_update(connman_network);
1240 static void debug(const char *str)
1242 if (getenv("CONNMAN_SUPPLICANT_DEBUG"))
1243 connman_debug("%s", str);
1246 static const GSupplicantCallbacks callbacks = {
1247 .system_ready = system_ready,
1248 .system_killed = system_killed,
1249 .interface_added = interface_added,
1250 .interface_state = interface_state,
1251 .interface_removed = interface_removed,
1252 .scan_started = scan_started,
1253 .scan_finished = scan_finished,
1254 .network_added = network_added,
1255 .network_removed = network_removed,
1256 .network_changed = network_changed,
1261 static int tech_probe(struct connman_technology *technology)
1263 wifi_technology = technology;
1268 static void tech_remove(struct connman_technology *technology)
1270 wifi_technology = NULL;
1273 struct wifi_tethering_info {
1274 struct wifi_data *wifi;
1275 struct connman_technology *technology;
1277 GSupplicantSSID *ssid;
1280 static GSupplicantSSID *ssid_ap_init(const char *ssid, const char *passphrase)
1282 GSupplicantSSID *ap;
1284 ap = g_try_malloc0(sizeof(GSupplicantSSID));
1288 ap->mode = G_SUPPLICANT_MODE_MASTER;
1290 ap->ssid_len = strlen(ssid);
1294 if (passphrase == NULL || strlen(passphrase) == 0) {
1295 ap->security = G_SUPPLICANT_SECURITY_NONE;
1296 ap->passphrase = NULL;
1298 ap->security = G_SUPPLICANT_SECURITY_PSK;
1299 ap->protocol = G_SUPPLICANT_PROTO_RSN;
1300 ap->pairwise_cipher = G_SUPPLICANT_PAIRWISE_CCMP;
1301 ap->group_cipher = G_SUPPLICANT_GROUP_CCMP;
1302 ap->passphrase = passphrase;
1308 static void ap_start_callback(int result, GSupplicantInterface *interface,
1311 struct wifi_tethering_info *info = user_data;
1313 DBG("result %d index %d bridge %s",
1314 result, info->wifi->index, info->wifi->bridge);
1317 connman_inet_remove_from_bridge(info->wifi->index,
1318 info->wifi->bridge);
1319 connman_technology_tethering_notify(info->technology, FALSE);
1322 g_free(info->ifname);
1326 static void ap_create_callback(int result,
1327 GSupplicantInterface *interface,
1330 struct wifi_tethering_info *info = user_data;
1332 DBG("result %d ifname %s", result,
1333 g_supplicant_interface_get_ifname(interface));
1336 connman_inet_remove_from_bridge(info->wifi->index,
1337 info->wifi->bridge);
1338 connman_technology_tethering_notify(info->technology, FALSE);
1340 g_free(info->ifname);
1345 info->wifi->interface = interface;
1346 g_supplicant_interface_set_data(interface, info->wifi);
1348 if (g_supplicant_interface_set_apscan(interface, 2) < 0)
1349 connman_error("Failed to set interface ap_scan property");
1351 g_supplicant_interface_connect(interface, info->ssid,
1352 ap_start_callback, info);
1355 static void sta_remove_callback(int result,
1356 GSupplicantInterface *interface,
1359 struct wifi_tethering_info *info = user_data;
1360 const char *driver = connman_option_get_string("wifi");
1362 DBG("ifname %s result %d ", info->ifname, result);
1365 info->wifi->tethering = TRUE;
1367 g_free(info->ifname);
1372 info->wifi->interface = NULL;
1374 connman_technology_tethering_notify(info->technology, TRUE);
1376 g_supplicant_interface_create(info->ifname, driver, info->wifi->bridge,
1381 static int tech_set_tethering(struct connman_technology *technology,
1382 const char *identifier, const char *passphrase,
1383 const char *bridge, connman_bool_t enabled)
1386 GSupplicantInterface *interface;
1387 struct wifi_data *wifi;
1388 struct wifi_tethering_info *info;
1395 if (enabled == FALSE) {
1396 for (list = iface_list; list; list = list->next) {
1399 if (wifi->tethering == TRUE) {
1400 wifi->tethering = FALSE;
1402 connman_inet_remove_from_bridge(wifi->index,
1404 wifi->bridged = FALSE;
1408 connman_technology_tethering_notify(technology, FALSE);
1413 for (list = iface_list; list; list = list->next) {
1416 interface = wifi->interface;
1418 if (interface == NULL)
1421 ifname = g_supplicant_interface_get_ifname(wifi->interface);
1423 mode = g_supplicant_interface_get_mode(interface);
1424 if ((mode & G_SUPPLICANT_CAPABILITY_MODE_AP) == 0) {
1425 DBG("%s does not support AP mode", ifname);
1429 info = g_try_malloc0(sizeof(struct wifi_tethering_info));
1434 info->technology = technology;
1435 info->wifi->bridge = bridge;
1436 info->ssid = ssid_ap_init(identifier, passphrase);
1437 if (info->ssid == NULL) {
1441 info->ifname = g_strdup(ifname);
1442 if (info->ifname == NULL) {
1447 info->wifi->tethering = TRUE;
1449 err = g_supplicant_interface_remove(interface,
1450 sta_remove_callback,
1459 static void regdom_callback(void *user_data)
1461 char *alpha2 = user_data;
1465 if (wifi_technology == NULL)
1468 connman_technology_regdom_notify(wifi_technology, alpha2);
1471 static int tech_set_regdom(struct connman_technology *technology, const char *alpha2)
1473 return g_supplicant_set_country(alpha2, regdom_callback, alpha2);
1476 static struct connman_technology_driver tech_driver = {
1478 .type = CONNMAN_SERVICE_TYPE_WIFI,
1479 .probe = tech_probe,
1480 .remove = tech_remove,
1481 .set_tethering = tech_set_tethering,
1482 .set_regdom = tech_set_regdom,
1485 static int wifi_init(void)
1489 err = connman_network_driver_register(&network_driver);
1493 err = g_supplicant_register(&callbacks);
1495 connman_network_driver_unregister(&network_driver);
1499 err = connman_technology_driver_register(&tech_driver);
1501 g_supplicant_unregister(&callbacks);
1502 connman_network_driver_unregister(&network_driver);
1509 static void wifi_exit(void)
1513 connman_technology_driver_unregister(&tech_driver);
1515 g_supplicant_unregister(&callbacks);
1517 connman_network_driver_unregister(&network_driver);
1520 CONNMAN_PLUGIN_DEFINE(wifi, "WiFi interface plugin", VERSION,
1521 CONNMAN_PLUGIN_PRIORITY_DEFAULT, wifi_init, wifi_exit)