5 * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31 #include <sys/ioctl.h>
32 #include <sys/socket.h>
33 #include <linux/if_arp.h>
34 #include <linux/wireless.h>
35 #include <net/ethernet.h>
38 #define IFF_LOWER_UP 0x10000
41 #include <dbus/dbus.h>
44 #define CONNMAN_API_SUBJECT_TO_CHANGE
45 #include <connman/plugin.h>
46 #include <connman/inet.h>
47 #include <connman/device.h>
48 #include <connman/rtnl.h>
49 #include <connman/technology.h>
50 #include <connman/log.h>
51 #include <connman/option.h>
52 #include <connman/storage.h>
53 #include <include/setting.h>
55 #include <gsupplicant/gsupplicant.h>
57 #define CLEANUP_TIMEOUT 8 /* in seconds */
58 #define INACTIVE_TIMEOUT 12 /* in seconds */
59 #define MAXIMUM_RETRIES 4
61 #define BGSCAN_DEFAULT "simple:30:-45:300"
62 #define AUTOSCAN_DEFAULT "exponential:3:300"
64 struct connman_technology *wifi_technology = NULL;
66 struct hidden_params {
68 unsigned int ssid_len;
74 * Used for autoscan "emulation".
75 * Should be removed when wpa_s autoscan support will be by default.
77 struct autoscan_params {
86 struct connman_device *device;
87 struct connman_network *network;
88 struct connman_network *pending_network;
90 GSupplicantInterface *interface;
91 GSupplicantState state;
92 connman_bool_t connected;
93 connman_bool_t disconnecting;
94 connman_bool_t tethering;
95 connman_bool_t bridged;
101 struct hidden_params *hidden;
103 * autoscan "emulation".
105 struct autoscan_params *autoscan;
108 static GList *iface_list = NULL;
110 static void handle_tethering(struct wifi_data *wifi)
112 if (wifi->tethering == FALSE)
115 if (wifi->bridge == NULL)
118 if (wifi->bridged == TRUE)
121 DBG("index %d bridge %s", wifi->index, wifi->bridge);
123 if (connman_inet_add_to_bridge(wifi->index, wifi->bridge) < 0)
126 wifi->bridged = TRUE;
129 static void wifi_newlink(unsigned flags, unsigned change, void *user_data)
131 struct connman_device *device = user_data;
132 struct wifi_data *wifi = connman_device_get_data(device);
134 DBG("index %d flags %d change %d", wifi->index, flags, change);
139 if ((wifi->flags & IFF_UP) != (flags & IFF_UP)) {
143 DBG("interface down");
146 if ((wifi->flags & IFF_LOWER_UP) != (flags & IFF_LOWER_UP)) {
147 if (flags & IFF_LOWER_UP) {
150 handle_tethering(wifi);
158 static int wifi_probe(struct connman_device *device)
160 struct wifi_data *wifi;
162 DBG("device %p", device);
164 wifi = g_try_new0(struct wifi_data, 1);
168 wifi->connected = FALSE;
169 wifi->disconnecting = FALSE;
170 wifi->tethering = FALSE;
171 wifi->bridged = FALSE;
173 wifi->state = G_SUPPLICANT_STATE_INACTIVE;
175 connman_device_set_data(device, wifi);
176 wifi->device = connman_device_ref(device);
178 wifi->index = connman_device_get_index(device);
181 wifi->watch = connman_rtnl_add_newlink_watch(wifi->index,
182 wifi_newlink, device);
184 iface_list = g_list_append(iface_list, wifi);
189 static void remove_networks(struct connman_device *device,
190 struct wifi_data *wifi)
194 for (list = wifi->networks; list != NULL; list = list->next) {
195 struct connman_network *network = list->data;
197 connman_device_remove_network(device, network);
198 connman_network_unref(network);
201 g_slist_free(wifi->networks);
202 wifi->networks = NULL;
205 static void stop_autoscan(struct connman_device *device)
207 struct wifi_data *wifi = connman_device_get_data(device);
208 struct autoscan_params *autoscan;
212 if (wifi == NULL || wifi->autoscan == NULL)
215 autoscan = wifi->autoscan;
217 if (autoscan->timeout > 0)
218 g_source_remove(autoscan->timeout);
220 autoscan->timeout = 0;
221 autoscan->interval = 0;
223 connman_device_unref(device);
226 static void wifi_remove(struct connman_device *device)
228 struct wifi_data *wifi = connman_device_get_data(device);
230 DBG("device %p wifi %p", device, wifi);
235 stop_autoscan(device);
237 iface_list = g_list_remove(iface_list, wifi);
239 remove_networks(device, wifi);
241 connman_device_set_powered(device, FALSE);
242 connman_device_set_data(device, NULL);
243 connman_device_unref(wifi->device);
244 connman_rtnl_remove_watch(wifi->watch);
246 g_supplicant_interface_set_data(wifi->interface, NULL);
248 g_free(wifi->autoscan);
249 g_free(wifi->identifier);
253 static int throw_wifi_scan(struct connman_device *device,
254 GSupplicantInterfaceCallback callback)
256 struct wifi_data *wifi = connman_device_get_data(device);
259 DBG("device %p %p", device, wifi->interface);
261 if (wifi->tethering == TRUE)
264 connman_device_ref(device);
266 ret = g_supplicant_interface_scan(wifi->interface, NULL,
269 connman_device_set_scanning(device, TRUE);
271 connman_device_unref(device);
276 static void autoscan_scan_callback(int result,
277 GSupplicantInterface *interface, void *user_data)
279 struct connman_device *device = user_data;
283 connman_device_set_scanning(device, FALSE);
286 static gboolean autoscan_timeout(gpointer data)
288 struct connman_device *device = data;
289 struct wifi_data *wifi = connman_device_get_data(device);
290 struct autoscan_params *autoscan;
293 autoscan = wifi->autoscan;
295 if (autoscan->interval <= 0) {
296 interval = autoscan->base;
299 interval = autoscan->interval * autoscan->base;
301 if (autoscan->interval >= autoscan->limit)
302 interval = autoscan->limit;
304 throw_wifi_scan(wifi->device, autoscan_scan_callback);
307 DBG("interval %d", interval);
309 autoscan->interval = interval;
311 autoscan->timeout = g_timeout_add_seconds(interval,
312 autoscan_timeout, device);
317 static void start_autoscan(struct connman_device *device)
319 struct wifi_data *wifi = connman_device_get_data(device);
320 struct autoscan_params *autoscan;
327 autoscan = wifi->autoscan;
328 if (autoscan == NULL)
331 if (autoscan->timeout > 0 || autoscan->interval > 0)
334 connman_device_ref(device);
336 autoscan_timeout(device);
339 static struct autoscan_params *parse_autoscan_params(const char *params)
341 struct autoscan_params *autoscan;
346 DBG("Emulating autoscan");
348 list_params = g_strsplit(params, ":", 0);
349 if (list_params == 0)
352 if (g_strv_length(list_params) < 3) {
353 g_strfreev(list_params);
357 base = atoi(list_params[1]);
358 limit = atoi(list_params[2]);
360 g_strfreev(list_params);
362 autoscan = g_try_malloc0(sizeof(struct autoscan_params));
363 if (autoscan == NULL) {
364 DBG("Could not allocate memory for autoscan");
368 DBG("base %d - limit %d", base, limit);
369 autoscan->base = base;
370 autoscan->limit = limit;
375 static void setup_autoscan(struct wifi_data *wifi)
377 if (wifi->autoscan == NULL)
378 wifi->autoscan = parse_autoscan_params(AUTOSCAN_DEFAULT);
380 start_autoscan(wifi->device);
383 static void interface_create_callback(int result,
384 GSupplicantInterface *interface,
387 struct wifi_data *wifi = user_data;
389 DBG("result %d ifname %s, wifi %p", result,
390 g_supplicant_interface_get_ifname(interface),
393 if (result < 0 || wifi == NULL)
396 wifi->interface = interface;
397 g_supplicant_interface_set_data(interface, wifi);
399 if (g_supplicant_interface_get_ready(interface) == FALSE)
402 DBG("interface is ready wifi %p tethering %d", wifi, wifi->tethering);
404 if (wifi->device == NULL) {
405 connman_error("WiFi device not set");
409 connman_device_set_powered(wifi->device, TRUE);
411 if (connman_setting_get_bool("BackgroundScanning") == FALSE)
414 /* Setting up automatic scanning */
415 setup_autoscan(wifi);
418 static int wifi_enable(struct connman_device *device)
420 struct wifi_data *wifi = connman_device_get_data(device);
421 const char *interface = connman_device_get_string(device, "Interface");
422 const char *driver = connman_option_get_string("wifi");
425 DBG("device %p %p", device, wifi);
427 ret = g_supplicant_interface_create(interface, driver, NULL,
428 interface_create_callback,
436 static int wifi_disable(struct connman_device *device)
438 struct wifi_data *wifi = connman_device_get_data(device);
441 DBG("device %p", device);
443 wifi->connected = FALSE;
444 wifi->disconnecting = FALSE;
446 if (wifi->pending_network != NULL)
447 wifi->pending_network = NULL;
449 remove_networks(device, wifi);
451 ret = g_supplicant_interface_remove(wifi->interface, NULL, NULL);
458 static void hidden_free(struct hidden_params *hidden)
463 g_free(hidden->identity);
464 g_free(hidden->passphrase);
468 static void scan_callback(int result, GSupplicantInterface *interface,
471 struct connman_device *device = user_data;
472 struct wifi_data *wifi = connman_device_get_data(device);
474 DBG("result %d", result);
476 if (wifi != NULL && wifi->hidden != NULL) {
477 hidden_free(wifi->hidden);
482 connman_device_reset_scanning(device);
484 connman_device_set_scanning(device, FALSE);
485 connman_device_unref(device);
487 start_autoscan(device);
490 static int add_scan_param(gchar *hex_ssid, int freq,
491 GSupplicantScanParams *scan_data,
492 int driver_max_scan_ssids)
496 if (driver_max_scan_ssids > scan_data->num_ssids && hex_ssid != NULL) {
498 unsigned int j = 0, hex;
499 size_t hex_ssid_len = strlen(hex_ssid);
501 ssid = g_try_malloc0(hex_ssid_len / 2);
505 for (i = 0; i < hex_ssid_len; i += 2) {
506 sscanf(hex_ssid + i, "%02x", &hex);
510 memcpy(scan_data->ssids[scan_data->num_ssids].ssid, ssid, j);
511 scan_data->ssids[scan_data->num_ssids].ssid_len = j;
512 scan_data->num_ssids++;
517 /* Don't add duplicate entries */
518 for (i = 0; i < G_SUPPLICANT_MAX_FAST_SCAN; i++) {
519 if (scan_data->freqs[i] == 0) {
520 scan_data->freqs[i] = freq;
522 } else if (scan_data->freqs[i] == freq)
529 struct last_connected {
535 static gint sort_entry(gconstpointer a, gconstpointer b, gpointer user_data)
537 GTimeVal *aval = (GTimeVal *)a;
538 GTimeVal *bval = (GTimeVal *)b;
540 /* Note that the sort order is descending */
541 if (aval->tv_sec < bval->tv_sec)
544 if (aval->tv_sec > bval->tv_sec)
550 static void free_entry(gpointer data)
552 struct last_connected *entry = data;
558 static int get_latest_connections(int max_ssids,
559 GSupplicantScanParams *scan_data)
562 GSequence *latest_list;
563 struct last_connected *entry;
572 latest_list = g_sequence_new(free_entry);
573 if (latest_list == NULL)
576 services = connman_storage_get_services();
577 for (i = 0; services && services[i]; i++) {
578 if (strncmp(services[i], "wifi_", 5) != 0)
581 keyfile = connman_storage_load_service(services[i]);
583 str = g_key_file_get_string(keyfile,
584 services[i], "Favorite", NULL);
585 if (str == NULL || g_strcmp0(str, "true")) {
588 g_key_file_free(keyfile);
593 str = g_key_file_get_string(keyfile,
594 services[i], "AutoConnect", NULL);
595 if (str == NULL || g_strcmp0(str, "true")) {
598 g_key_file_free(keyfile);
603 str = g_key_file_get_string(keyfile,
604 services[i], "Modified", NULL);
606 g_time_val_from_iso8601(str, &modified);
610 ssid = g_key_file_get_string(keyfile,
611 services[i], "SSID", NULL);
613 freq = g_key_file_get_integer(keyfile, services[i],
616 entry = g_try_new(struct last_connected, 1);
618 g_sequence_free(latest_list);
619 g_key_file_free(keyfile);
625 entry->modified = modified;
628 g_sequence_insert_sorted(latest_list, entry,
634 g_key_file_free(keyfile);
637 g_strfreev(services);
639 num_ssids = num_ssids > G_SUPPLICANT_MAX_FAST_SCAN ?
640 G_SUPPLICANT_MAX_FAST_SCAN : num_ssids;
642 iter = g_sequence_get_begin_iter(latest_list);
644 for (i = 0; i < num_ssids; i++) {
645 entry = g_sequence_get(iter);
647 DBG("ssid %s freq %d modified %lu", entry->ssid, entry->freq,
648 entry->modified.tv_sec);
650 add_scan_param(entry->ssid, entry->freq, scan_data, max_ssids);
652 iter = g_sequence_iter_next(iter);
655 g_sequence_free(latest_list);
659 static int wifi_scan(struct connman_device *device)
661 stop_autoscan(device);
663 return throw_wifi_scan(device, scan_callback);
666 static int wifi_scan_fast(struct connman_device *device)
668 struct wifi_data *wifi = connman_device_get_data(device);
669 GSupplicantScanParams *scan_params = NULL;
671 int driver_max_ssids = 0;
673 DBG("device %p %p", device, wifi->interface);
675 if (wifi->tethering == TRUE)
678 driver_max_ssids = g_supplicant_interface_get_max_scan_ssids(
680 DBG("max ssids %d", driver_max_ssids);
681 if (driver_max_ssids == 0)
682 return wifi_scan(device);
684 scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
685 if (scan_params == NULL)
688 ret = get_latest_connections(driver_max_ssids, scan_params);
691 return wifi_scan(device);
694 stop_autoscan(device);
696 connman_device_ref(device);
697 ret = g_supplicant_interface_scan(wifi->interface, scan_params,
698 scan_callback, device);
700 connman_device_set_scanning(device, TRUE);
703 connman_device_unref(device);
709 static int wifi_scan_hidden(struct connman_device *device,
710 const char *ssid, unsigned int ssid_len,
711 const char *identity, const char* passphrase)
713 struct wifi_data *wifi = connman_device_get_data(device);
714 GSupplicantScanParams *scan_params = NULL;
715 struct hidden_params *hidden;
718 DBG("hidden SSID %s", ssid);
720 if (wifi->tethering == TRUE || wifi->hidden != NULL)
723 if (ssid == NULL || ssid_len == 0 || ssid_len > 32)
726 scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
727 if (scan_params == NULL)
729 memcpy(scan_params->ssids[0].ssid, ssid, ssid_len);
730 scan_params->ssids[0].ssid_len = ssid_len;
731 scan_params->num_ssids = 1;
733 hidden = g_try_new0(struct hidden_params, 1);
734 if (hidden == NULL) {
738 memcpy(hidden->ssid, ssid, ssid_len);
739 hidden->ssid_len = ssid_len;
740 hidden->identity = g_strdup(identity);
741 hidden->passphrase = g_strdup(passphrase);
742 wifi->hidden = hidden;
744 stop_autoscan(device);
746 connman_device_ref(device);
747 ret = g_supplicant_interface_scan(wifi->interface, scan_params,
748 scan_callback, device);
750 connman_device_set_scanning(device, TRUE);
752 connman_device_unref(device);
754 hidden_free(wifi->hidden);
761 static struct connman_device_driver wifi_ng_driver = {
763 .type = CONNMAN_DEVICE_TYPE_WIFI,
764 .priority = CONNMAN_DEVICE_PRIORITY_LOW,
766 .remove = wifi_remove,
767 .enable = wifi_enable,
768 .disable = wifi_disable,
770 .scan_fast = wifi_scan_fast,
771 .scan_hidden = wifi_scan_hidden,
774 static void system_ready(void)
778 if (connman_device_driver_register(&wifi_ng_driver) < 0)
779 connman_error("Failed to register WiFi driver");
782 static void system_killed(void)
786 connman_device_driver_unregister(&wifi_ng_driver);
789 static int network_probe(struct connman_network *network)
791 DBG("network %p", network);
796 static void network_remove(struct connman_network *network)
798 struct connman_device *device = connman_network_get_device(network);
799 struct wifi_data *wifi;
801 DBG("network %p", network);
803 wifi = connman_device_get_data(device);
807 if (wifi->network != network)
810 wifi->network = NULL;
813 static void connect_callback(int result, GSupplicantInterface *interface,
816 struct connman_network *network = user_data;
818 DBG("network %p result %d", network, result);
820 if (result == -ENOKEY) {
821 connman_network_set_error(network,
822 CONNMAN_NETWORK_ERROR_INVALID_KEY);
823 } else if (result < 0) {
824 connman_network_set_error(network,
825 CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
829 static GSupplicantSecurity network_security(const char *security)
831 if (g_str_equal(security, "none") == TRUE)
832 return G_SUPPLICANT_SECURITY_NONE;
833 else if (g_str_equal(security, "wep") == TRUE)
834 return G_SUPPLICANT_SECURITY_WEP;
835 else if (g_str_equal(security, "psk") == TRUE)
836 return G_SUPPLICANT_SECURITY_PSK;
837 else if (g_str_equal(security, "wpa") == TRUE)
838 return G_SUPPLICANT_SECURITY_PSK;
839 else if (g_str_equal(security, "rsn") == TRUE)
840 return G_SUPPLICANT_SECURITY_PSK;
841 else if (g_str_equal(security, "ieee8021x") == TRUE)
842 return G_SUPPLICANT_SECURITY_IEEE8021X;
844 return G_SUPPLICANT_SECURITY_UNKNOWN;
847 static void ssid_init(GSupplicantSSID *ssid, struct connman_network *network)
849 const char *security, *passphrase, *agent_passphrase;
851 memset(ssid, 0, sizeof(*ssid));
852 ssid->mode = G_SUPPLICANT_MODE_INFRA;
853 ssid->ssid = connman_network_get_blob(network, "WiFi.SSID",
856 security = connman_network_get_string(network, "WiFi.Security");
857 ssid->security = network_security(security);
858 passphrase = connman_network_get_string(network,
860 if (passphrase == NULL || strlen(passphrase) == 0) {
862 /* Use agent provided passphrase as a fallback */
863 agent_passphrase = connman_network_get_string(network,
864 "WiFi.AgentPassphrase");
866 if (agent_passphrase == NULL || strlen(agent_passphrase) == 0)
867 ssid->passphrase = NULL;
869 ssid->passphrase = agent_passphrase;
871 ssid->passphrase = passphrase;
873 ssid->eap = connman_network_get_string(network, "WiFi.EAP");
876 * If our private key password is unset,
877 * we use the supplied passphrase. That is needed
878 * for PEAP where 2 passphrases (identity and client
879 * cert may have to be provided.
881 if (connman_network_get_string(network,
882 "WiFi.PrivateKeyPassphrase") == NULL)
883 connman_network_set_string(network,
884 "WiFi.PrivateKeyPassphrase",
886 /* We must have an identity for both PEAP and TLS */
887 ssid->identity = connman_network_get_string(network, "WiFi.Identity");
889 /* Use agent provided identity as a fallback */
890 if (ssid->identity == NULL || strlen(ssid->identity) == 0)
891 ssid->identity = connman_network_get_string(network,
892 "WiFi.AgentIdentity");
894 ssid->ca_cert_path = connman_network_get_string(network,
896 ssid->client_cert_path = connman_network_get_string(network,
897 "WiFi.ClientCertFile");
898 ssid->private_key_path = connman_network_get_string(network,
899 "WiFi.PrivateKeyFile");
900 ssid->private_key_passphrase = connman_network_get_string(network,
901 "WiFi.PrivateKeyPassphrase");
902 ssid->phase2_auth = connman_network_get_string(network, "WiFi.Phase2");
904 ssid->use_wps = connman_network_get_bool(network, "WiFi.UseWPS");
905 ssid->pin_wps = connman_network_get_string(network, "WiFi.PinWPS");
907 if (connman_setting_get_bool("BackgroundScanning") == TRUE)
908 ssid->bgscan = BGSCAN_DEFAULT;
911 static int network_connect(struct connman_network *network)
913 struct connman_device *device = connman_network_get_device(network);
914 struct wifi_data *wifi;
915 GSupplicantInterface *interface;
916 GSupplicantSSID *ssid;
918 DBG("network %p", network);
923 wifi = connman_device_get_data(device);
927 ssid = g_try_malloc0(sizeof(GSupplicantSSID));
931 interface = wifi->interface;
933 ssid_init(ssid, network);
935 if (wifi->disconnecting == TRUE)
936 wifi->pending_network = network;
938 wifi->network = network;
941 return g_supplicant_interface_connect(interface, ssid,
942 connect_callback, network);
948 static void disconnect_callback(int result, GSupplicantInterface *interface,
951 struct wifi_data *wifi = user_data;
953 if (wifi->network != NULL) {
955 * if result < 0 supplican return an error because
956 * the network is not current.
957 * we wont receive G_SUPPLICANT_STATE_DISCONNECTED since it
958 * failed, call connman_network_set_connected to report
959 * disconnect is completed.
962 connman_network_set_connected(wifi->network, FALSE);
965 wifi->network = NULL;
967 wifi->disconnecting = FALSE;
969 if (wifi->pending_network != NULL) {
970 network_connect(wifi->pending_network);
971 wifi->pending_network = NULL;
974 start_autoscan(wifi->device);
977 static int network_disconnect(struct connman_network *network)
979 struct connman_device *device = connman_network_get_device(network);
980 struct wifi_data *wifi;
983 DBG("network %p", network);
985 wifi = connman_device_get_data(device);
986 if (wifi == NULL || wifi->interface == NULL)
989 connman_network_set_associating(network, FALSE);
991 if (wifi->disconnecting == TRUE)
994 wifi->disconnecting = TRUE;
996 err = g_supplicant_interface_disconnect(wifi->interface,
997 disconnect_callback, wifi);
999 wifi->disconnecting = FALSE;
1004 static struct connman_network_driver network_driver = {
1006 .type = CONNMAN_NETWORK_TYPE_WIFI,
1007 .priority = CONNMAN_NETWORK_PRIORITY_LOW,
1008 .probe = network_probe,
1009 .remove = network_remove,
1010 .connect = network_connect,
1011 .disconnect = network_disconnect,
1014 static void interface_added(GSupplicantInterface *interface)
1016 const char *ifname = g_supplicant_interface_get_ifname(interface);
1017 const char *driver = g_supplicant_interface_get_driver(interface);
1018 struct wifi_data *wifi;
1020 wifi = g_supplicant_interface_get_data(interface);
1023 * We can get here with a NULL wifi pointer when
1024 * the interface added signal is sent before the
1025 * interface creation callback is called.
1030 DBG("ifname %s driver %s wifi %p tethering %d",
1031 ifname, driver, wifi, wifi->tethering);
1033 if (wifi->device == NULL) {
1034 connman_error("WiFi device not set");
1038 connman_device_set_powered(wifi->device, TRUE);
1040 if (wifi->tethering == TRUE)
1044 static connman_bool_t is_idle(struct wifi_data *wifi)
1046 DBG("state %d", wifi->state);
1048 switch (wifi->state) {
1049 case G_SUPPLICANT_STATE_UNKNOWN:
1050 case G_SUPPLICANT_STATE_DISCONNECTED:
1051 case G_SUPPLICANT_STATE_INACTIVE:
1052 case G_SUPPLICANT_STATE_SCANNING:
1055 case G_SUPPLICANT_STATE_AUTHENTICATING:
1056 case G_SUPPLICANT_STATE_ASSOCIATING:
1057 case G_SUPPLICANT_STATE_ASSOCIATED:
1058 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
1059 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
1060 case G_SUPPLICANT_STATE_COMPLETED:
1067 static connman_bool_t is_idle_wps(GSupplicantInterface *interface,
1068 struct wifi_data *wifi)
1070 /* First, let's check if WPS processing did not went wrong */
1071 if (g_supplicant_interface_get_wps_state(interface) ==
1072 G_SUPPLICANT_WPS_STATE_FAIL)
1075 /* Unlike normal connection, being associated while processing wps
1076 * actually means that we are idling. */
1077 switch (wifi->state) {
1078 case G_SUPPLICANT_STATE_UNKNOWN:
1079 case G_SUPPLICANT_STATE_DISCONNECTED:
1080 case G_SUPPLICANT_STATE_INACTIVE:
1081 case G_SUPPLICANT_STATE_SCANNING:
1082 case G_SUPPLICANT_STATE_ASSOCIATED:
1084 case G_SUPPLICANT_STATE_AUTHENTICATING:
1085 case G_SUPPLICANT_STATE_ASSOCIATING:
1086 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
1087 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
1088 case G_SUPPLICANT_STATE_COMPLETED:
1095 static connman_bool_t handle_wps_completion(GSupplicantInterface *interface,
1096 struct connman_network *network,
1097 struct connman_device *device,
1098 struct wifi_data *wifi)
1102 wps = connman_network_get_bool(network, "WiFi.UseWPS");
1104 const unsigned char *ssid, *wps_ssid;
1105 unsigned int ssid_len, wps_ssid_len;
1106 const char *wps_key;
1108 /* Checking if we got associated with requested
1110 ssid = connman_network_get_blob(network, "WiFi.SSID",
1113 wps_ssid = g_supplicant_interface_get_wps_ssid(
1114 interface, &wps_ssid_len);
1116 if (wps_ssid == NULL || wps_ssid_len != ssid_len ||
1117 memcmp(ssid, wps_ssid, ssid_len) != 0) {
1118 connman_network_set_associating(network, FALSE);
1119 g_supplicant_interface_disconnect(wifi->interface,
1120 disconnect_callback, wifi);
1124 wps_key = g_supplicant_interface_get_wps_key(interface);
1125 connman_network_set_string(network, "WiFi.Passphrase",
1128 connman_network_set_string(network, "WiFi.PinWPS", NULL);
1134 static connman_bool_t handle_4way_handshake_failure(GSupplicantInterface *interface,
1135 struct connman_network *network,
1136 struct wifi_data *wifi)
1138 if (wifi->state != G_SUPPLICANT_STATE_4WAY_HANDSHAKE)
1143 if (wifi->retries < MAXIMUM_RETRIES)
1146 connman_network_set_error(network, CONNMAN_NETWORK_ERROR_INVALID_KEY);
1151 static void interface_state(GSupplicantInterface *interface)
1153 struct connman_network *network;
1154 struct connman_device *device;
1155 struct wifi_data *wifi;
1156 GSupplicantState state = g_supplicant_interface_get_state(interface);
1159 wifi = g_supplicant_interface_get_data(interface);
1161 DBG("wifi %p interface state %d", wifi, state);
1166 network = wifi->network;
1167 device = wifi->device;
1169 if (device == NULL || network == NULL)
1173 case G_SUPPLICANT_STATE_SCANNING:
1176 case G_SUPPLICANT_STATE_AUTHENTICATING:
1177 case G_SUPPLICANT_STATE_ASSOCIATING:
1178 stop_autoscan(device);
1180 connman_network_set_associating(network, TRUE);
1183 case G_SUPPLICANT_STATE_COMPLETED:
1184 /* though it should be already stopped: */
1185 stop_autoscan(device);
1187 if (handle_wps_completion(interface, network, device, wifi) ==
1191 connman_network_set_connected(network, TRUE);
1194 case G_SUPPLICANT_STATE_DISCONNECTED:
1196 * If we're in one of the idle modes, we have
1197 * not started association yet and thus setting
1198 * those ones to FALSE could cancel an association
1201 wps = connman_network_get_bool(network, "WiFi.UseWPS");
1203 if (is_idle_wps(interface, wifi) == TRUE)
1209 /* If previous state was 4way-handshake, then
1210 * it's either: psk was incorrect and thus we retry
1211 * or if we reach the maximum retries we declare the
1213 if (handle_4way_handshake_failure(interface,
1214 network, wifi) == TRUE)
1217 /* We disable the selected network, if not then
1218 * wpa_supplicant will loop retrying */
1219 if (g_supplicant_interface_enable_selected_network(interface,
1221 DBG("Could not disables selected network");
1223 connman_network_set_associating(network, FALSE);
1224 connman_network_set_connected(network, FALSE);
1226 start_autoscan(device);
1230 case G_SUPPLICANT_STATE_INACTIVE:
1231 connman_network_set_associating(network, FALSE);
1232 start_autoscan(device);
1236 case G_SUPPLICANT_STATE_UNKNOWN:
1237 case G_SUPPLICANT_STATE_ASSOCIATED:
1238 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
1239 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
1243 wifi->state = state;
1248 static void interface_removed(GSupplicantInterface *interface)
1250 const char *ifname = g_supplicant_interface_get_ifname(interface);
1251 struct wifi_data *wifi;
1253 DBG("ifname %s", ifname);
1255 wifi = g_supplicant_interface_get_data(interface);
1257 if (wifi != NULL && wifi->tethering == TRUE)
1260 if (wifi == NULL || wifi->device == NULL) {
1261 connman_error("Wrong wifi pointer");
1265 wifi->interface = NULL;
1266 connman_device_set_powered(wifi->device, FALSE);
1269 static void scan_started(GSupplicantInterface *interface)
1274 static void scan_finished(GSupplicantInterface *interface)
1279 static unsigned char calculate_strength(GSupplicantNetwork *supplicant_network)
1281 unsigned char strength;
1283 strength = 120 + g_supplicant_network_get_signal(supplicant_network);
1290 static void network_added(GSupplicantNetwork *supplicant_network)
1292 struct connman_network *network;
1293 GSupplicantInterface *interface;
1294 struct wifi_data *wifi;
1295 const char *name, *identifier, *security, *group, *mode;
1296 const unsigned char *ssid;
1297 unsigned int ssid_len;
1299 connman_bool_t wps_pbc;
1300 connman_bool_t wps_ready;
1301 connman_bool_t wps_advertizing;
1305 interface = g_supplicant_network_get_interface(supplicant_network);
1306 wifi = g_supplicant_interface_get_data(interface);
1307 name = g_supplicant_network_get_name(supplicant_network);
1308 identifier = g_supplicant_network_get_identifier(supplicant_network);
1309 security = g_supplicant_network_get_security(supplicant_network);
1310 group = g_supplicant_network_get_identifier(supplicant_network);
1311 wps = g_supplicant_network_get_wps(supplicant_network);
1312 wps_pbc = g_supplicant_network_is_wps_pbc(supplicant_network);
1313 wps_ready = g_supplicant_network_is_wps_active(supplicant_network);
1314 wps_advertizing = g_supplicant_network_is_wps_advertizing(
1315 supplicant_network);
1316 mode = g_supplicant_network_get_mode(supplicant_network);
1321 ssid = g_supplicant_network_get_ssid(supplicant_network, &ssid_len);
1323 network = connman_device_get_network(wifi->device, identifier);
1325 if (network == NULL) {
1326 network = connman_network_create(identifier,
1327 CONNMAN_NETWORK_TYPE_WIFI);
1328 if (network == NULL)
1331 connman_network_set_index(network, wifi->index);
1333 if (connman_device_add_network(wifi->device, network) < 0) {
1334 connman_network_unref(network);
1338 wifi->networks = g_slist_append(wifi->networks, network);
1341 if (name != NULL && name[0] != '\0')
1342 connman_network_set_name(network, name);
1344 connman_network_set_blob(network, "WiFi.SSID",
1346 connman_network_set_string(network, "WiFi.Security", security);
1347 connman_network_set_strength(network,
1348 calculate_strength(supplicant_network));
1349 connman_network_set_bool(network, "WiFi.WPS", wps);
1352 /* Is AP advertizing for WPS association?
1353 * If so, we decide to use WPS by default */
1354 if (wps_ready == TRUE && wps_pbc == TRUE &&
1355 wps_advertizing == TRUE)
1356 connman_network_set_bool(network, "WiFi.UseWPS", TRUE);
1359 connman_network_set_frequency(network,
1360 g_supplicant_network_get_frequency(supplicant_network));
1362 connman_network_set_available(network, TRUE);
1363 connman_network_set_string(network, "WiFi.Mode", mode);
1366 connman_network_set_group(network, group);
1368 if (wifi->hidden != NULL) {
1369 if (wifi->hidden->ssid_len == ssid_len &&
1370 memcmp(wifi->hidden->ssid, ssid,
1372 connman_network_connect_hidden(network,
1373 wifi->hidden->identity,
1374 wifi->hidden->passphrase);
1375 hidden_free(wifi->hidden);
1376 wifi->hidden = NULL;
1381 static void network_removed(GSupplicantNetwork *network)
1383 GSupplicantInterface *interface;
1384 struct wifi_data *wifi;
1385 const char *name, *identifier;
1386 struct connman_network *connman_network;
1388 interface = g_supplicant_network_get_interface(network);
1389 wifi = g_supplicant_interface_get_data(interface);
1390 identifier = g_supplicant_network_get_identifier(network);
1391 name = g_supplicant_network_get_name(network);
1393 DBG("name %s", name);
1398 connman_network = connman_device_get_network(wifi->device, identifier);
1399 if (connman_network == NULL)
1402 wifi->networks = g_slist_remove(wifi->networks, connman_network);
1404 connman_device_remove_network(wifi->device, connman_network);
1405 connman_network_unref(connman_network);
1408 static void network_changed(GSupplicantNetwork *network, const char *property)
1410 GSupplicantInterface *interface;
1411 struct wifi_data *wifi;
1412 const char *name, *identifier;
1413 struct connman_network *connman_network;
1415 interface = g_supplicant_network_get_interface(network);
1416 wifi = g_supplicant_interface_get_data(interface);
1417 identifier = g_supplicant_network_get_identifier(network);
1418 name = g_supplicant_network_get_name(network);
1420 DBG("name %s", name);
1425 connman_network = connman_device_get_network(wifi->device, identifier);
1426 if (connman_network == NULL)
1429 if (g_str_equal(property, "Signal") == TRUE) {
1430 connman_network_set_strength(connman_network,
1431 calculate_strength(network));
1432 connman_network_update(connman_network);
1436 static void debug(const char *str)
1438 if (getenv("CONNMAN_SUPPLICANT_DEBUG"))
1439 connman_debug("%s", str);
1442 static const GSupplicantCallbacks callbacks = {
1443 .system_ready = system_ready,
1444 .system_killed = system_killed,
1445 .interface_added = interface_added,
1446 .interface_state = interface_state,
1447 .interface_removed = interface_removed,
1448 .scan_started = scan_started,
1449 .scan_finished = scan_finished,
1450 .network_added = network_added,
1451 .network_removed = network_removed,
1452 .network_changed = network_changed,
1457 static int tech_probe(struct connman_technology *technology)
1459 wifi_technology = technology;
1464 static void tech_remove(struct connman_technology *technology)
1466 wifi_technology = NULL;
1469 struct wifi_tethering_info {
1470 struct wifi_data *wifi;
1471 struct connman_technology *technology;
1473 GSupplicantSSID *ssid;
1476 static GSupplicantSSID *ssid_ap_init(const char *ssid, const char *passphrase)
1478 GSupplicantSSID *ap;
1480 ap = g_try_malloc0(sizeof(GSupplicantSSID));
1484 ap->mode = G_SUPPLICANT_MODE_MASTER;
1486 ap->ssid_len = strlen(ssid);
1490 if (passphrase == NULL || strlen(passphrase) == 0) {
1491 ap->security = G_SUPPLICANT_SECURITY_NONE;
1492 ap->passphrase = NULL;
1494 ap->security = G_SUPPLICANT_SECURITY_PSK;
1495 ap->protocol = G_SUPPLICANT_PROTO_RSN;
1496 ap->pairwise_cipher = G_SUPPLICANT_PAIRWISE_CCMP;
1497 ap->group_cipher = G_SUPPLICANT_GROUP_CCMP;
1498 ap->passphrase = passphrase;
1504 static void ap_start_callback(int result, GSupplicantInterface *interface,
1507 struct wifi_tethering_info *info = user_data;
1509 DBG("result %d index %d bridge %s",
1510 result, info->wifi->index, info->wifi->bridge);
1513 connman_inet_remove_from_bridge(info->wifi->index,
1514 info->wifi->bridge);
1515 connman_technology_tethering_notify(info->technology, FALSE);
1518 g_free(info->ifname);
1522 static void ap_create_callback(int result,
1523 GSupplicantInterface *interface,
1526 struct wifi_tethering_info *info = user_data;
1528 DBG("result %d ifname %s", result,
1529 g_supplicant_interface_get_ifname(interface));
1532 connman_inet_remove_from_bridge(info->wifi->index,
1533 info->wifi->bridge);
1534 connman_technology_tethering_notify(info->technology, FALSE);
1536 g_free(info->ifname);
1541 info->wifi->interface = interface;
1542 g_supplicant_interface_set_data(interface, info->wifi);
1544 if (g_supplicant_interface_set_apscan(interface, 2) < 0)
1545 connman_error("Failed to set interface ap_scan property");
1547 g_supplicant_interface_connect(interface, info->ssid,
1548 ap_start_callback, info);
1551 static void sta_remove_callback(int result,
1552 GSupplicantInterface *interface,
1555 struct wifi_tethering_info *info = user_data;
1556 const char *driver = connman_option_get_string("wifi");
1558 DBG("ifname %s result %d ", info->ifname, result);
1561 info->wifi->tethering = TRUE;
1563 g_free(info->ifname);
1568 info->wifi->interface = NULL;
1570 connman_technology_tethering_notify(info->technology, TRUE);
1572 g_supplicant_interface_create(info->ifname, driver, info->wifi->bridge,
1577 static int tech_set_tethering(struct connman_technology *technology,
1578 const char *identifier, const char *passphrase,
1579 const char *bridge, connman_bool_t enabled)
1582 GSupplicantInterface *interface;
1583 struct wifi_data *wifi;
1584 struct wifi_tethering_info *info;
1591 if (enabled == FALSE) {
1592 for (list = iface_list; list; list = list->next) {
1595 if (wifi->tethering == TRUE) {
1596 wifi->tethering = FALSE;
1598 connman_inet_remove_from_bridge(wifi->index,
1600 wifi->bridged = FALSE;
1604 connman_technology_tethering_notify(technology, FALSE);
1609 for (list = iface_list; list; list = list->next) {
1612 interface = wifi->interface;
1614 if (interface == NULL)
1617 ifname = g_supplicant_interface_get_ifname(wifi->interface);
1619 mode = g_supplicant_interface_get_mode(interface);
1620 if ((mode & G_SUPPLICANT_CAPABILITY_MODE_AP) == 0) {
1621 DBG("%s does not support AP mode", ifname);
1625 info = g_try_malloc0(sizeof(struct wifi_tethering_info));
1630 info->technology = technology;
1631 info->wifi->bridge = bridge;
1632 info->ssid = ssid_ap_init(identifier, passphrase);
1633 if (info->ssid == NULL) {
1637 info->ifname = g_strdup(ifname);
1638 if (info->ifname == NULL) {
1643 info->wifi->tethering = TRUE;
1645 err = g_supplicant_interface_remove(interface,
1646 sta_remove_callback,
1655 static void regdom_callback(void *user_data)
1657 char *alpha2 = user_data;
1661 if (wifi_technology == NULL)
1664 connman_technology_regdom_notify(wifi_technology, alpha2);
1667 static int tech_set_regdom(struct connman_technology *technology, const char *alpha2)
1669 return g_supplicant_set_country(alpha2, regdom_callback, alpha2);
1672 static struct connman_technology_driver tech_driver = {
1674 .type = CONNMAN_SERVICE_TYPE_WIFI,
1675 .probe = tech_probe,
1676 .remove = tech_remove,
1677 .set_tethering = tech_set_tethering,
1678 .set_regdom = tech_set_regdom,
1681 static int wifi_init(void)
1685 err = connman_network_driver_register(&network_driver);
1689 err = g_supplicant_register(&callbacks);
1691 connman_network_driver_unregister(&network_driver);
1695 err = connman_technology_driver_register(&tech_driver);
1697 g_supplicant_unregister(&callbacks);
1698 connman_network_driver_unregister(&network_driver);
1705 static void wifi_exit(void)
1709 connman_technology_driver_unregister(&tech_driver);
1711 g_supplicant_unregister(&callbacks);
1713 connman_network_driver_unregister(&network_driver);
1716 CONNMAN_PLUGIN_DEFINE(wifi, "WiFi interface plugin", VERSION,
1717 CONNMAN_PLUGIN_PRIORITY_DEFAULT, wifi_init, wifi_exit)