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_data(device, NULL);
200 connman_device_unref(wifi->device);
201 connman_rtnl_remove_watch(wifi->watch);
203 g_supplicant_interface_set_data(wifi->interface, NULL);
205 g_free(wifi->identifier);
209 static void interface_create_callback(int result,
210 GSupplicantInterface *interface,
213 struct wifi_data *wifi = user_data;
215 DBG("result %d ifname %s, wifi %p", result,
216 g_supplicant_interface_get_ifname(interface),
219 if (result < 0 || wifi == NULL)
222 wifi->interface = interface;
223 g_supplicant_interface_set_data(interface, wifi);
225 if (g_supplicant_interface_get_ready(interface) == FALSE)
228 DBG("interface is ready wifi %p tethering %d", wifi, wifi->tethering);
230 if (wifi->device == NULL) {
231 connman_error("WiFi device not set");
235 connman_device_set_powered(wifi->device, TRUE);
238 static void interface_remove_callback(int result,
239 GSupplicantInterface *interface,
242 struct wifi_data *wifi;
244 wifi = g_supplicant_interface_get_data(interface);
246 DBG("result %d wifi %p", result, wifi);
248 if (result < 0 || wifi == NULL)
251 wifi->interface = NULL;
255 static int wifi_enable(struct connman_device *device)
257 struct wifi_data *wifi = connman_device_get_data(device);
258 const char *interface = connman_device_get_string(device, "Interface");
259 const char *driver = connman_option_get_string("wifi");
262 DBG("device %p %p", device, wifi);
264 ret = g_supplicant_interface_create(interface, driver, NULL,
265 interface_create_callback,
273 static int wifi_disable(struct connman_device *device)
275 struct wifi_data *wifi = connman_device_get_data(device);
278 DBG("device %p", device);
280 wifi->connected = FALSE;
281 wifi->disconnecting = FALSE;
283 if (wifi->pending_network != NULL)
284 wifi->pending_network = NULL;
286 remove_networks(device, wifi);
288 ret = g_supplicant_interface_remove(wifi->interface,
289 interface_remove_callback,
297 static void hidden_free(struct hidden_params *hidden)
302 g_free(hidden->identity);
303 g_free(hidden->passphrase);
307 static void scan_callback(int result, GSupplicantInterface *interface,
310 struct connman_device *device = user_data;
311 struct wifi_data *wifi = connman_device_get_data(device);
313 DBG("result %d", result);
315 if (wifi != NULL && wifi->hidden != NULL) {
316 hidden_free(wifi->hidden);
321 connman_device_reset_scanning(device);
323 connman_device_set_scanning(device, FALSE);
324 connman_device_unref(device);
327 static int add_scan_param(gchar *hex_ssid, int freq,
328 GSupplicantScanParams *scan_data,
329 int driver_max_scan_ssids)
333 if (driver_max_scan_ssids > scan_data->num_ssids && hex_ssid != NULL) {
335 unsigned int j = 0, hex;
336 size_t hex_ssid_len = strlen(hex_ssid);
338 ssid = g_try_malloc0(hex_ssid_len / 2);
342 for (i = 0; i < hex_ssid_len; i += 2) {
343 sscanf(hex_ssid + i, "%02x", &hex);
347 memcpy(scan_data->ssids[scan_data->num_ssids].ssid, ssid, j);
348 scan_data->ssids[scan_data->num_ssids].ssid_len = j;
349 scan_data->num_ssids++;
354 /* Don't add duplicate entries */
355 for (i = 0; i < G_SUPPLICANT_MAX_FAST_SCAN; i++) {
356 if (scan_data->freqs[i] == 0) {
357 scan_data->freqs[i] = freq;
359 } else if (scan_data->freqs[i] == freq)
366 struct last_connected {
372 static gint sort_entry(gconstpointer a, gconstpointer b, gpointer user_data)
374 GTimeVal *aval = (GTimeVal *)a;
375 GTimeVal *bval = (GTimeVal *)b;
377 /* Note that the sort order is descending */
378 if (aval->tv_sec < bval->tv_sec)
381 if (aval->tv_sec > bval->tv_sec)
387 static void free_entry(gpointer data)
389 struct last_connected *entry = data;
395 static int get_latest_connections(int max_ssids,
396 GSupplicantScanParams *scan_data)
399 GSequence *latest_list;
400 struct last_connected *entry;
409 latest_list = g_sequence_new(free_entry);
410 if (latest_list == NULL)
413 services = connman_storage_get_services();
414 for (i = 0; services && services[i]; i++) {
415 if (strncmp(services[i], "wifi_", 5) != 0)
418 keyfile = connman_storage_load_service(services[i]);
420 str = g_key_file_get_string(keyfile,
421 services[i], "Favorite", NULL);
422 if (str == NULL || g_strcmp0(str, "true")) {
425 g_key_file_free(keyfile);
430 str = g_key_file_get_string(keyfile,
431 services[i], "AutoConnect", NULL);
432 if (str == NULL || g_strcmp0(str, "true")) {
435 g_key_file_free(keyfile);
440 str = g_key_file_get_string(keyfile,
441 services[i], "Modified", NULL);
443 g_time_val_from_iso8601(str, &modified);
447 ssid = g_key_file_get_string(keyfile,
448 services[i], "SSID", NULL);
450 freq = g_key_file_get_integer(keyfile, services[i],
453 entry = g_try_new(struct last_connected, 1);
455 g_sequence_free(latest_list);
456 g_key_file_free(keyfile);
462 entry->modified = modified;
465 g_sequence_insert_sorted(latest_list, entry,
471 g_key_file_free(keyfile);
474 g_strfreev(services);
476 num_ssids = num_ssids > G_SUPPLICANT_MAX_FAST_SCAN ?
477 G_SUPPLICANT_MAX_FAST_SCAN : num_ssids;
479 iter = g_sequence_get_begin_iter(latest_list);
481 for (i = 0; i < num_ssids; i++) {
482 entry = g_sequence_get(iter);
484 DBG("ssid %s freq %d modified %lu", entry->ssid, entry->freq,
485 entry->modified.tv_sec);
487 add_scan_param(entry->ssid, entry->freq, scan_data, max_ssids);
489 iter = g_sequence_iter_next(iter);
492 g_sequence_free(latest_list);
496 static int wifi_scan(struct connman_device *device)
498 struct wifi_data *wifi = connman_device_get_data(device);
501 DBG("device %p %p", device, wifi->interface);
503 if (wifi->tethering == TRUE)
506 connman_device_ref(device);
507 ret = g_supplicant_interface_scan(wifi->interface, NULL,
508 scan_callback, device);
510 connman_device_set_scanning(device, TRUE);
512 connman_device_unref(device);
517 static int wifi_scan_fast(struct connman_device *device)
519 struct wifi_data *wifi = connman_device_get_data(device);
520 GSupplicantScanParams *scan_params = NULL;
522 int driver_max_ssids = 0;
524 DBG("device %p %p", device, wifi->interface);
526 if (wifi->tethering == TRUE)
529 driver_max_ssids = g_supplicant_interface_get_max_scan_ssids(
531 DBG("max ssids %d", driver_max_ssids);
532 if (driver_max_ssids == 0)
533 return wifi_scan(device);
535 scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
536 if (scan_params == NULL)
539 ret = get_latest_connections(driver_max_ssids, scan_params);
542 return wifi_scan(device);
545 connman_device_ref(device);
546 ret = g_supplicant_interface_scan(wifi->interface, scan_params,
547 scan_callback, device);
549 connman_device_set_scanning(device, TRUE);
552 connman_device_unref(device);
558 static int wifi_scan_hidden(struct connman_device *device,
559 const char *ssid, unsigned int ssid_len,
560 const char *identity, const char* passphrase)
562 struct wifi_data *wifi = connman_device_get_data(device);
563 GSupplicantScanParams *scan_params = NULL;
564 struct hidden_params *hidden;
567 DBG("hidden SSID %s", ssid);
569 if (wifi->tethering == TRUE || wifi->hidden != NULL)
572 if (ssid == NULL || ssid_len == 0 || ssid_len > 32)
575 scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
576 if (scan_params == NULL)
578 memcpy(scan_params->ssids[0].ssid, ssid, ssid_len);
579 scan_params->ssids[0].ssid_len = ssid_len;
580 scan_params->num_ssids = 1;
582 hidden = g_try_new0(struct hidden_params, 1);
583 if (hidden == NULL) {
587 memcpy(hidden->ssid, ssid, ssid_len);
588 hidden->ssid_len = ssid_len;
589 hidden->identity = g_strdup(identity);
590 hidden->passphrase = g_strdup(passphrase);
591 wifi->hidden = hidden;
593 connman_device_ref(device);
594 ret = g_supplicant_interface_scan(wifi->interface, scan_params,
595 scan_callback, device);
597 connman_device_set_scanning(device, TRUE);
599 connman_device_unref(device);
601 hidden_free(wifi->hidden);
608 static struct connman_device_driver wifi_ng_driver = {
610 .type = CONNMAN_DEVICE_TYPE_WIFI,
611 .priority = CONNMAN_DEVICE_PRIORITY_LOW,
613 .remove = wifi_remove,
614 .enable = wifi_enable,
615 .disable = wifi_disable,
617 .scan_fast = wifi_scan_fast,
618 .scan_hidden = wifi_scan_hidden,
621 static void system_ready(void)
625 if (connman_device_driver_register(&wifi_ng_driver) < 0)
626 connman_error("Failed to register WiFi driver");
629 static void system_killed(void)
633 connman_device_driver_unregister(&wifi_ng_driver);
636 static int network_probe(struct connman_network *network)
638 DBG("network %p", network);
643 static void network_remove(struct connman_network *network)
645 struct connman_device *device = connman_network_get_device(network);
646 struct wifi_data *wifi;
648 DBG("network %p", network);
650 wifi = connman_device_get_data(device);
654 if (wifi->network != network)
657 wifi->network = NULL;
660 static void connect_callback(int result, GSupplicantInterface *interface,
663 struct connman_network *network = user_data;
665 DBG("network %p result %d", network, result);
667 if (result == -ENOKEY) {
668 connman_network_set_error(network,
669 CONNMAN_NETWORK_ERROR_INVALID_KEY);
670 } else if (result < 0) {
671 connman_network_set_error(network,
672 CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
676 static GSupplicantSecurity network_security(const char *security)
678 if (g_str_equal(security, "none") == TRUE)
679 return G_SUPPLICANT_SECURITY_NONE;
680 else if (g_str_equal(security, "wep") == TRUE)
681 return G_SUPPLICANT_SECURITY_WEP;
682 else if (g_str_equal(security, "psk") == TRUE)
683 return G_SUPPLICANT_SECURITY_PSK;
684 else if (g_str_equal(security, "wpa") == TRUE)
685 return G_SUPPLICANT_SECURITY_PSK;
686 else if (g_str_equal(security, "rsn") == TRUE)
687 return G_SUPPLICANT_SECURITY_PSK;
688 else if (g_str_equal(security, "ieee8021x") == TRUE)
689 return G_SUPPLICANT_SECURITY_IEEE8021X;
691 return G_SUPPLICANT_SECURITY_UNKNOWN;
694 static void ssid_init(GSupplicantSSID *ssid, struct connman_network *network)
696 const char *security, *passphrase, *agent_passphrase;
698 memset(ssid, 0, sizeof(*ssid));
699 ssid->mode = G_SUPPLICANT_MODE_INFRA;
700 ssid->ssid = connman_network_get_blob(network, "WiFi.SSID",
703 security = connman_network_get_string(network, "WiFi.Security");
704 ssid->security = network_security(security);
705 passphrase = connman_network_get_string(network,
707 if (passphrase == NULL || strlen(passphrase) == 0) {
709 /* Use agent provided passphrase as a fallback */
710 agent_passphrase = connman_network_get_string(network,
711 "WiFi.AgentPassphrase");
713 if (agent_passphrase == NULL || strlen(agent_passphrase) == 0)
714 ssid->passphrase = NULL;
716 ssid->passphrase = agent_passphrase;
718 ssid->passphrase = passphrase;
720 ssid->eap = connman_network_get_string(network, "WiFi.EAP");
723 * If our private key password is unset,
724 * we use the supplied passphrase. That is needed
725 * for PEAP where 2 passphrases (identity and client
726 * cert may have to be provided.
728 if (connman_network_get_string(network,
729 "WiFi.PrivateKeyPassphrase") == NULL)
730 connman_network_set_string(network,
731 "WiFi.PrivateKeyPassphrase",
733 /* We must have an identity for both PEAP and TLS */
734 ssid->identity = connman_network_get_string(network, "WiFi.Identity");
736 /* Use agent provided identity as a fallback */
737 if (ssid->identity == NULL || strlen(ssid->identity) == 0)
738 ssid->identity = connman_network_get_string(network,
739 "WiFi.AgentIdentity");
741 ssid->ca_cert_path = connman_network_get_string(network,
743 ssid->client_cert_path = connman_network_get_string(network,
744 "WiFi.ClientCertFile");
745 ssid->private_key_path = connman_network_get_string(network,
746 "WiFi.PrivateKeyFile");
747 ssid->private_key_passphrase = connman_network_get_string(network,
748 "WiFi.PrivateKeyPassphrase");
749 ssid->phase2_auth = connman_network_get_string(network, "WiFi.Phase2");
751 ssid->use_wps = connman_network_get_bool(network, "WiFi.UseWPS");
752 ssid->pin_wps = connman_network_get_string(network, "WiFi.PinWPS");
756 static int network_connect(struct connman_network *network)
758 struct connman_device *device = connman_network_get_device(network);
759 struct wifi_data *wifi;
760 GSupplicantInterface *interface;
761 GSupplicantSSID *ssid;
763 DBG("network %p", network);
768 wifi = connman_device_get_data(device);
772 ssid = g_try_malloc0(sizeof(GSupplicantSSID));
776 interface = wifi->interface;
778 ssid_init(ssid, network);
780 if (wifi->disconnecting == TRUE)
781 wifi->pending_network = network;
783 wifi->network = network;
786 return g_supplicant_interface_connect(interface, ssid,
787 connect_callback, network);
793 static void disconnect_callback(int result, GSupplicantInterface *interface,
796 struct wifi_data *wifi = user_data;
798 if (wifi->network != NULL) {
800 * if result < 0 supplican return an error because
801 * the network is not current.
802 * we wont receive G_SUPPLICANT_STATE_DISCONNECTED since it
803 * failed, call connman_network_set_connected to report
804 * disconnect is completed.
807 connman_network_set_connected(wifi->network, FALSE);
810 wifi->network = NULL;
812 wifi->disconnecting = FALSE;
814 if (wifi->pending_network != NULL) {
815 network_connect(wifi->pending_network);
816 wifi->pending_network = NULL;
821 static int network_disconnect(struct connman_network *network)
823 struct connman_device *device = connman_network_get_device(network);
824 struct wifi_data *wifi;
827 DBG("network %p", network);
829 wifi = connman_device_get_data(device);
830 if (wifi == NULL || wifi->interface == NULL)
833 connman_network_set_associating(network, FALSE);
835 if (wifi->disconnecting == TRUE)
838 wifi->disconnecting = TRUE;
840 err = g_supplicant_interface_disconnect(wifi->interface,
841 disconnect_callback, wifi);
843 wifi->disconnecting = FALSE;
848 static struct connman_network_driver network_driver = {
850 .type = CONNMAN_NETWORK_TYPE_WIFI,
851 .priority = CONNMAN_NETWORK_PRIORITY_LOW,
852 .probe = network_probe,
853 .remove = network_remove,
854 .connect = network_connect,
855 .disconnect = network_disconnect,
858 static void interface_added(GSupplicantInterface *interface)
860 const char *ifname = g_supplicant_interface_get_ifname(interface);
861 const char *driver = g_supplicant_interface_get_driver(interface);
862 struct wifi_data *wifi;
864 wifi = g_supplicant_interface_get_data(interface);
867 * We can get here with a NULL wifi pointer when
868 * the interface added signal is sent before the
869 * interface creation callback is called.
874 DBG("ifname %s driver %s wifi %p tethering %d",
875 ifname, driver, wifi, wifi->tethering);
877 if (wifi->device == NULL) {
878 connman_error("WiFi device not set");
882 connman_device_set_powered(wifi->device, TRUE);
884 if (wifi->tethering == TRUE)
888 static connman_bool_t is_idle(struct wifi_data *wifi)
890 DBG("state %d", wifi->state);
892 switch (wifi->state) {
893 case G_SUPPLICANT_STATE_UNKNOWN:
894 case G_SUPPLICANT_STATE_DISCONNECTED:
895 case G_SUPPLICANT_STATE_INACTIVE:
896 case G_SUPPLICANT_STATE_SCANNING:
899 case G_SUPPLICANT_STATE_AUTHENTICATING:
900 case G_SUPPLICANT_STATE_ASSOCIATING:
901 case G_SUPPLICANT_STATE_ASSOCIATED:
902 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
903 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
904 case G_SUPPLICANT_STATE_COMPLETED:
911 static connman_bool_t is_idle_wps(GSupplicantInterface *interface,
912 struct wifi_data *wifi)
914 /* First, let's check if WPS processing did not went wrong */
915 if (g_supplicant_interface_get_wps_state(interface) ==
916 G_SUPPLICANT_WPS_STATE_FAIL)
919 /* Unlike normal connection, being associated while processing wps
920 * actually means that we are idling. */
921 switch (wifi->state) {
922 case G_SUPPLICANT_STATE_UNKNOWN:
923 case G_SUPPLICANT_STATE_DISCONNECTED:
924 case G_SUPPLICANT_STATE_INACTIVE:
925 case G_SUPPLICANT_STATE_SCANNING:
926 case G_SUPPLICANT_STATE_ASSOCIATED:
928 case G_SUPPLICANT_STATE_AUTHENTICATING:
929 case G_SUPPLICANT_STATE_ASSOCIATING:
930 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
931 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
932 case G_SUPPLICANT_STATE_COMPLETED:
939 static connman_bool_t handle_wps_completion(GSupplicantInterface *interface,
940 struct connman_network *network,
941 struct connman_device *device,
942 struct wifi_data *wifi)
946 wps = connman_network_get_bool(network, "WiFi.UseWPS");
948 const unsigned char *ssid, *wps_ssid;
949 unsigned int ssid_len, wps_ssid_len;
952 /* Checking if we got associated with requested
954 ssid = connman_network_get_blob(network, "WiFi.SSID",
957 wps_ssid = g_supplicant_interface_get_wps_ssid(
958 interface, &wps_ssid_len);
960 if (wps_ssid == NULL || wps_ssid_len != ssid_len ||
961 memcmp(ssid, wps_ssid, ssid_len) != 0) {
962 connman_network_set_associating(network, FALSE);
963 g_supplicant_interface_disconnect(wifi->interface,
964 disconnect_callback, wifi);
968 wps_key = g_supplicant_interface_get_wps_key(interface);
969 connman_network_set_string(network, "WiFi.Passphrase",
972 connman_network_set_string(network, "WiFi.PinWPS", NULL);
978 static connman_bool_t handle_4way_handshake_failure(GSupplicantInterface *interface,
979 struct connman_network *network,
980 struct wifi_data *wifi)
982 if (wifi->state != G_SUPPLICANT_STATE_4WAY_HANDSHAKE)
987 if (wifi->retries < MAXIMUM_RETRIES)
990 connman_network_set_error(network, CONNMAN_NETWORK_ERROR_INVALID_KEY);
995 static void interface_state(GSupplicantInterface *interface)
997 struct connman_network *network;
998 struct connman_device *device;
999 struct wifi_data *wifi;
1000 GSupplicantState state = g_supplicant_interface_get_state(interface);
1003 wifi = g_supplicant_interface_get_data(interface);
1005 DBG("wifi %p interface state %d", wifi, state);
1010 network = wifi->network;
1011 device = wifi->device;
1013 if (device == NULL || network == NULL)
1017 case G_SUPPLICANT_STATE_SCANNING:
1020 case G_SUPPLICANT_STATE_AUTHENTICATING:
1021 case G_SUPPLICANT_STATE_ASSOCIATING:
1022 connman_network_set_associating(network, TRUE);
1025 case G_SUPPLICANT_STATE_COMPLETED:
1026 if (handle_wps_completion(interface, network, device, wifi) ==
1030 /* reset scan trigger and schedule background scan */
1031 connman_device_schedule_scan(device);
1033 connman_network_set_connected(network, TRUE);
1036 case G_SUPPLICANT_STATE_DISCONNECTED:
1038 * If we're in one of the idle modes, we have
1039 * not started association yet and thus setting
1040 * those ones to FALSE could cancel an association
1043 wps = connman_network_get_bool(network, "WiFi.UseWPS");
1045 if (is_idle_wps(interface, wifi) == TRUE)
1051 /* If previous state was 4way-handshake, then
1052 * it's either: psk was incorrect and thus we retry
1053 * or if we reach the maximum retries we declare the
1055 if (handle_4way_handshake_failure(interface,
1056 network, wifi) == TRUE)
1059 /* We disable the selected network, if not then
1060 * wpa_supplicant will loop retrying */
1061 if (g_supplicant_interface_enable_selected_network(interface,
1063 DBG("Could not disables selected network");
1065 connman_network_set_associating(network, FALSE);
1066 connman_network_set_connected(network, FALSE);
1069 case G_SUPPLICANT_STATE_INACTIVE:
1070 connman_network_set_associating(network, FALSE);
1073 case G_SUPPLICANT_STATE_UNKNOWN:
1074 case G_SUPPLICANT_STATE_ASSOCIATED:
1075 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
1076 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
1080 wifi->state = state;
1085 static void interface_removed(GSupplicantInterface *interface)
1087 const char *ifname = g_supplicant_interface_get_ifname(interface);
1088 struct wifi_data *wifi;
1090 DBG("ifname %s", ifname);
1092 wifi = g_supplicant_interface_get_data(interface);
1094 if (wifi != NULL && wifi->tethering == TRUE)
1097 if (wifi == NULL || wifi->device == NULL) {
1098 connman_error("Wrong wifi pointer");
1102 connman_device_set_powered(wifi->device, FALSE);
1105 static void scan_started(GSupplicantInterface *interface)
1110 static void scan_finished(GSupplicantInterface *interface)
1115 static unsigned char calculate_strength(GSupplicantNetwork *supplicant_network)
1117 unsigned char strength;
1119 strength = 120 + g_supplicant_network_get_signal(supplicant_network);
1126 static void network_added(GSupplicantNetwork *supplicant_network)
1128 struct connman_network *network;
1129 GSupplicantInterface *interface;
1130 struct wifi_data *wifi;
1131 const char *name, *identifier, *security, *group;
1132 const unsigned char *ssid;
1133 unsigned int ssid_len;
1138 interface = g_supplicant_network_get_interface(supplicant_network);
1139 wifi = g_supplicant_interface_get_data(interface);
1140 name = g_supplicant_network_get_name(supplicant_network);
1141 identifier = g_supplicant_network_get_identifier(supplicant_network);
1142 security = g_supplicant_network_get_security(supplicant_network);
1143 group = g_supplicant_network_get_identifier(supplicant_network);
1144 wps = g_supplicant_network_get_wps(supplicant_network);
1149 ssid = g_supplicant_network_get_ssid(supplicant_network, &ssid_len);
1151 network = connman_device_get_network(wifi->device, identifier);
1153 if (network == NULL) {
1154 network = connman_network_create(identifier,
1155 CONNMAN_NETWORK_TYPE_WIFI);
1156 if (network == NULL)
1159 connman_network_set_index(network, wifi->index);
1161 if (connman_device_add_network(wifi->device, network) < 0) {
1162 connman_network_unref(network);
1166 wifi->networks = g_slist_append(wifi->networks, network);
1169 if (name != NULL && name[0] != '\0')
1170 connman_network_set_name(network, name);
1172 connman_network_set_blob(network, "WiFi.SSID",
1174 connman_network_set_string(network, "WiFi.Security", security);
1175 connman_network_set_strength(network,
1176 calculate_strength(supplicant_network));
1177 connman_network_set_bool(network, "WiFi.WPS", wps);
1179 connman_network_set_frequency(network,
1180 g_supplicant_network_get_frequency(supplicant_network));
1182 connman_network_set_available(network, TRUE);
1185 connman_network_set_group(network, group);
1187 if (wifi->hidden != NULL) {
1188 if (wifi->hidden->ssid_len == ssid_len &&
1189 memcmp(wifi->hidden->ssid, ssid,
1191 connman_network_connect_hidden(network,
1192 wifi->hidden->identity,
1193 wifi->hidden->passphrase);
1194 hidden_free(wifi->hidden);
1195 wifi->hidden = NULL;
1200 static void network_removed(GSupplicantNetwork *network)
1202 GSupplicantInterface *interface;
1203 struct wifi_data *wifi;
1204 const char *name, *identifier;
1205 struct connman_network *connman_network;
1207 interface = g_supplicant_network_get_interface(network);
1208 wifi = g_supplicant_interface_get_data(interface);
1209 identifier = g_supplicant_network_get_identifier(network);
1210 name = g_supplicant_network_get_name(network);
1212 DBG("name %s", name);
1217 connman_network = connman_device_get_network(wifi->device, identifier);
1218 if (connman_network == NULL)
1221 wifi->networks = g_slist_remove(wifi->networks, connman_network);
1223 connman_device_remove_network(wifi->device, connman_network);
1224 connman_network_unref(connman_network);
1227 static void network_changed(GSupplicantNetwork *network, const char *property)
1229 GSupplicantInterface *interface;
1230 struct wifi_data *wifi;
1231 const char *name, *identifier;
1232 struct connman_network *connman_network;
1234 interface = g_supplicant_network_get_interface(network);
1235 wifi = g_supplicant_interface_get_data(interface);
1236 identifier = g_supplicant_network_get_identifier(network);
1237 name = g_supplicant_network_get_name(network);
1239 DBG("name %s", name);
1244 connman_network = connman_device_get_network(wifi->device, identifier);
1245 if (connman_network == NULL)
1248 if (g_str_equal(property, "Signal") == TRUE) {
1249 connman_network_set_strength(connman_network,
1250 calculate_strength(network));
1251 connman_network_update(connman_network);
1255 static void debug(const char *str)
1257 if (getenv("CONNMAN_SUPPLICANT_DEBUG"))
1258 connman_debug("%s", str);
1261 static const GSupplicantCallbacks callbacks = {
1262 .system_ready = system_ready,
1263 .system_killed = system_killed,
1264 .interface_added = interface_added,
1265 .interface_state = interface_state,
1266 .interface_removed = interface_removed,
1267 .scan_started = scan_started,
1268 .scan_finished = scan_finished,
1269 .network_added = network_added,
1270 .network_removed = network_removed,
1271 .network_changed = network_changed,
1276 static int tech_probe(struct connman_technology *technology)
1278 wifi_technology = technology;
1283 static void tech_remove(struct connman_technology *technology)
1285 wifi_technology = NULL;
1288 struct wifi_tethering_info {
1289 struct wifi_data *wifi;
1290 struct connman_technology *technology;
1292 GSupplicantSSID *ssid;
1295 static GSupplicantSSID *ssid_ap_init(const char *ssid, const char *passphrase)
1297 GSupplicantSSID *ap;
1299 ap = g_try_malloc0(sizeof(GSupplicantSSID));
1303 ap->mode = G_SUPPLICANT_MODE_MASTER;
1305 ap->ssid_len = strlen(ssid);
1309 if (passphrase == NULL || strlen(passphrase) == 0) {
1310 ap->security = G_SUPPLICANT_SECURITY_NONE;
1311 ap->passphrase = NULL;
1313 ap->security = G_SUPPLICANT_SECURITY_PSK;
1314 ap->protocol = G_SUPPLICANT_PROTO_RSN;
1315 ap->pairwise_cipher = G_SUPPLICANT_PAIRWISE_CCMP;
1316 ap->group_cipher = G_SUPPLICANT_GROUP_CCMP;
1317 ap->passphrase = passphrase;
1323 static void ap_start_callback(int result, GSupplicantInterface *interface,
1326 struct wifi_tethering_info *info = user_data;
1328 DBG("result %d index %d bridge %s",
1329 result, info->wifi->index, info->wifi->bridge);
1332 connman_inet_remove_from_bridge(info->wifi->index,
1333 info->wifi->bridge);
1334 connman_technology_tethering_notify(info->technology, FALSE);
1337 g_free(info->ifname);
1341 static void ap_create_callback(int result,
1342 GSupplicantInterface *interface,
1345 struct wifi_tethering_info *info = user_data;
1347 DBG("result %d ifname %s", result,
1348 g_supplicant_interface_get_ifname(interface));
1351 connman_inet_remove_from_bridge(info->wifi->index,
1352 info->wifi->bridge);
1353 connman_technology_tethering_notify(info->technology, FALSE);
1355 g_free(info->ifname);
1360 info->wifi->interface = interface;
1361 g_supplicant_interface_set_data(interface, info->wifi);
1363 if (g_supplicant_interface_set_apscan(interface, 2) < 0)
1364 connman_error("Failed to set interface ap_scan property");
1366 g_supplicant_interface_connect(interface, info->ssid,
1367 ap_start_callback, info);
1370 static void sta_remove_callback(int result,
1371 GSupplicantInterface *interface,
1374 struct wifi_tethering_info *info = user_data;
1375 const char *driver = connman_option_get_string("wifi");
1377 DBG("ifname %s result %d ", info->ifname, result);
1380 info->wifi->tethering = TRUE;
1382 g_free(info->ifname);
1387 info->wifi->interface = NULL;
1389 connman_technology_tethering_notify(info->technology, TRUE);
1391 g_supplicant_interface_create(info->ifname, driver, info->wifi->bridge,
1396 static int tech_set_tethering(struct connman_technology *technology,
1397 const char *identifier, const char *passphrase,
1398 const char *bridge, connman_bool_t enabled)
1401 GSupplicantInterface *interface;
1402 struct wifi_data *wifi;
1403 struct wifi_tethering_info *info;
1410 if (enabled == FALSE) {
1411 for (list = iface_list; list; list = list->next) {
1414 if (wifi->tethering == TRUE) {
1415 wifi->tethering = FALSE;
1417 connman_inet_remove_from_bridge(wifi->index,
1419 wifi->bridged = FALSE;
1423 connman_technology_tethering_notify(technology, FALSE);
1428 for (list = iface_list; list; list = list->next) {
1431 interface = wifi->interface;
1433 if (interface == NULL)
1436 ifname = g_supplicant_interface_get_ifname(wifi->interface);
1438 mode = g_supplicant_interface_get_mode(interface);
1439 if ((mode & G_SUPPLICANT_CAPABILITY_MODE_AP) == 0) {
1440 DBG("%s does not support AP mode", ifname);
1444 info = g_try_malloc0(sizeof(struct wifi_tethering_info));
1449 info->technology = technology;
1450 info->wifi->bridge = bridge;
1451 info->ssid = ssid_ap_init(identifier, passphrase);
1452 if (info->ssid == NULL) {
1456 info->ifname = g_strdup(ifname);
1457 if (info->ifname == NULL) {
1462 info->wifi->tethering = TRUE;
1464 err = g_supplicant_interface_remove(interface,
1465 sta_remove_callback,
1474 static void regdom_callback(void *user_data)
1476 char *alpha2 = user_data;
1480 if (wifi_technology == NULL)
1483 connman_technology_regdom_notify(wifi_technology, alpha2);
1486 static int tech_set_regdom(struct connman_technology *technology, const char *alpha2)
1488 return g_supplicant_set_country(alpha2, regdom_callback, alpha2);
1491 static struct connman_technology_driver tech_driver = {
1493 .type = CONNMAN_SERVICE_TYPE_WIFI,
1494 .probe = tech_probe,
1495 .remove = tech_remove,
1496 .set_tethering = tech_set_tethering,
1497 .set_regdom = tech_set_regdom,
1500 static int wifi_init(void)
1504 err = connman_network_driver_register(&network_driver);
1508 err = g_supplicant_register(&callbacks);
1510 connman_network_driver_unregister(&network_driver);
1514 err = connman_technology_driver_register(&tech_driver);
1516 g_supplicant_unregister(&callbacks);
1517 connman_network_driver_unregister(&network_driver);
1524 static void wifi_exit(void)
1528 connman_technology_driver_unregister(&tech_driver);
1530 g_supplicant_unregister(&callbacks);
1532 connman_network_driver_unregister(&network_driver);
1535 CONNMAN_PLUGIN_DEFINE(wifi, "WiFi interface plugin", VERSION,
1536 CONNMAN_PLUGIN_PRIORITY_DEFAULT, wifi_init, wifi_exit)