5 * Copyright (C) 2007-2014 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 <net/ethernet.h>
34 #include <linux/wireless.h>
37 #define IFF_LOWER_UP 0x10000
40 #include <dbus/dbus.h>
43 #define CONNMAN_API_SUBJECT_TO_CHANGE
44 #include <connman/plugin.h>
45 #include <connman/inet.h>
46 #include <connman/device.h>
47 #include <connman/rtnl.h>
48 #include <connman/technology.h>
49 #include <connman/service.h>
50 #include <connman/peer.h>
51 #include <connman/log.h>
52 #include <connman/storage.h>
53 #include <include/setting.h>
54 #include <connman/provision.h>
55 #include <connman/utsname.h>
56 #include <connman/machine.h>
57 #include <connman/tethering.h>
59 #include <gsupplicant/gsupplicant.h>
61 #include "src/shared/util.h"
63 #define CLEANUP_TIMEOUT 8 /* in seconds */
64 #define INACTIVE_TIMEOUT 12 /* in seconds */
65 #define FAVORITE_MAXIMUM_RETRIES 2
67 #define BGSCAN_DEFAULT "simple:30:-65:300"
68 #define AUTOSCAN_EXPONENTIAL "exponential:3:300"
69 #define AUTOSCAN_SINGLE "single:3"
70 #define SCAN_MAX_DURATION 10
72 #define P2P_FIND_TIMEOUT 30
73 #define P2P_CONNECTION_TIMEOUT 100
74 #define P2P_LISTEN_PERIOD 500
75 #define P2P_LISTEN_INTERVAL 2000
77 #define ASSOC_STATUS_AUTH_TIMEOUT 16
78 #define ASSOC_STATUS_NO_CLIENT 17
79 #define LOAD_SHAPING_MAX_RETRIES 3
81 static struct connman_technology *wifi_technology = NULL;
82 static struct connman_technology *p2p_technology = NULL;
84 enum wifi_ap_capability{
86 WIFI_AP_SUPPORTED = 1,
87 WIFI_AP_NOT_SUPPORTED = 2,
90 enum wifi_scanning_type {
91 WIFI_SCANNING_UNKNOWN = 0,
92 WIFI_SCANNING_PASSIVE = 1,
93 WIFI_SCANNING_ACTIVE = 2,
96 struct hidden_params {
98 unsigned int ssid_len;
100 char *anonymous_identity;
102 char *altsubject_match;
103 char *domain_suffix_match;
107 GSupplicantScanParams *scan_params;
112 * Used for autoscan "emulation".
113 * Should be removed when wpa_s autoscan support will be by default.
115 struct autoscan_params {
119 unsigned int timeout;
122 struct wifi_tethering_info {
123 struct wifi_data *wifi;
124 struct connman_technology *technology;
126 GSupplicantSSID *ssid;
131 struct connman_device *device;
132 struct connman_network *network;
133 struct connman_network *pending_network;
135 GSupplicantInterface *interface;
136 GSupplicantState state;
140 enum wifi_ap_capability ap_supported;
142 bool interface_ready;
148 int load_shaping_retries;
149 struct hidden_params *hidden;
150 bool postpone_hidden;
151 struct wifi_tethering_info *tethering_param;
153 * autoscan "emulation".
155 struct autoscan_params *autoscan;
156 enum wifi_scanning_type scanning_type;
157 GSupplicantScanParams *scan_params;
158 unsigned int p2p_find_timeout;
159 unsigned int p2p_connection_timeout;
160 struct connman_peer *pending_peer;
169 struct disconnect_data {
170 struct wifi_data *wifi;
171 struct connman_network *network;
174 static GList *iface_list = NULL;
176 static GList *pending_wifi_device = NULL;
177 static GList *p2p_iface_list = NULL;
178 static bool wfd_service_registered = false;
180 static void start_autoscan(struct connman_device *device);
181 static int tech_set_tethering(struct connman_technology *technology,
182 const char *identifier, const char *passphrase,
183 const char *bridge, bool enabled);
185 static int p2p_tech_probe(struct connman_technology *technology)
187 p2p_technology = technology;
192 static void p2p_tech_remove(struct connman_technology *technology)
194 p2p_technology = NULL;
197 static struct connman_technology_driver p2p_tech_driver = {
199 .type = CONNMAN_SERVICE_TYPE_P2P,
200 .probe = p2p_tech_probe,
201 .remove = p2p_tech_remove,
204 static bool is_p2p_connecting(void)
208 for (list = iface_list; list; list = list->next) {
209 struct wifi_data *wifi = list->data;
211 if (wifi->p2p_connecting)
218 static void add_pending_wifi_device(struct wifi_data *wifi)
220 if (g_list_find(pending_wifi_device, wifi))
223 pending_wifi_device = g_list_append(pending_wifi_device, wifi);
226 static struct wifi_data *get_pending_wifi_data(const char *ifname)
230 for (list = pending_wifi_device; list; list = list->next) {
231 struct wifi_data *wifi;
232 const char *dev_name;
235 if (!wifi || !wifi->device)
238 dev_name = connman_device_get_string(wifi->device, "Interface");
239 if (!g_strcmp0(ifname, dev_name)) {
240 pending_wifi_device = g_list_delete_link(
241 pending_wifi_device, list);
249 static void remove_pending_wifi_device(struct wifi_data *wifi)
253 link = g_list_find(pending_wifi_device, wifi);
258 pending_wifi_device = g_list_delete_link(pending_wifi_device, link);
261 static void peer_cancel_timeout(struct wifi_data *wifi)
263 if (wifi->p2p_connection_timeout > 0)
264 g_source_remove(wifi->p2p_connection_timeout);
266 wifi->p2p_connection_timeout = 0;
267 wifi->p2p_connecting = false;
269 if (wifi->pending_peer) {
270 connman_peer_unref(wifi->pending_peer);
271 wifi->pending_peer = NULL;
275 static gboolean peer_connect_timeout(gpointer data)
277 struct wifi_data *wifi = data;
281 if (wifi->p2p_connecting) {
282 enum connman_peer_state state = CONNMAN_PEER_STATE_FAILURE;
283 GSupplicantPeer *gs_peer =
284 g_supplicant_interface_peer_lookup(wifi->interface,
285 connman_peer_get_identifier(wifi->pending_peer));
287 if (g_supplicant_peer_has_requested_connection(gs_peer))
288 state = CONNMAN_PEER_STATE_IDLE;
290 connman_peer_set_state(wifi->pending_peer, state);
293 peer_cancel_timeout(wifi);
298 static void peer_connect_callback(int result, GSupplicantInterface *interface,
301 struct wifi_data *wifi = user_data;
302 struct connman_peer *peer = wifi->pending_peer;
304 DBG("peer %p - %d", peer, result);
310 peer_connect_timeout(wifi);
314 connman_peer_set_state(peer, CONNMAN_PEER_STATE_ASSOCIATION);
316 wifi->p2p_connection_timeout = g_timeout_add_seconds(
317 P2P_CONNECTION_TIMEOUT,
318 peer_connect_timeout, wifi);
321 static int peer_connect(struct connman_peer *peer,
322 enum connman_peer_wps_method wps_method,
325 struct connman_device *device = connman_peer_get_device(peer);
326 GSupplicantPeerParams *peer_params;
327 GSupplicantPeer *gs_peer;
328 struct wifi_data *wifi;
332 DBG("peer %p", peer);
337 wifi = connman_device_get_data(device);
338 if (!wifi || !wifi->interface)
341 if (wifi->p2p_connecting)
344 gs_peer = g_supplicant_interface_peer_lookup(wifi->interface,
345 connman_peer_get_identifier(peer));
349 pbc = g_supplicant_peer_is_wps_pbc(gs_peer);
350 pin = g_supplicant_peer_is_wps_pin(gs_peer);
352 switch (wps_method) {
353 case CONNMAN_PEER_WPS_UNKNOWN:
354 if ((pbc && pin) || pin)
357 case CONNMAN_PEER_WPS_PBC:
362 case CONNMAN_PEER_WPS_PIN:
363 if (!pin || !wps_pin)
368 peer_params = g_try_malloc0(sizeof(GSupplicantPeerParams));
372 peer_params->path = g_strdup(g_supplicant_peer_get_path(gs_peer));
374 peer_params->wps_pin = g_strdup(wps_pin);
376 peer_params->master = connman_peer_service_is_master();
378 ret = g_supplicant_interface_p2p_connect(wifi->interface, peer_params,
379 peer_connect_callback, wifi);
380 if (ret == -EINPROGRESS) {
381 wifi->pending_peer = connman_peer_ref(peer);
382 wifi->p2p_connecting = true;
383 } else if (ret < 0) {
384 g_free(peer_params->path);
385 g_free(peer_params->wps_pin);
392 static int peer_disconnect(struct connman_peer *peer)
394 struct connman_device *device = connman_peer_get_device(peer);
395 GSupplicantPeerParams peer_params = {};
396 GSupplicantPeer *gs_peer;
397 struct wifi_data *wifi;
400 DBG("peer %p", peer);
405 wifi = connman_device_get_data(device);
409 gs_peer = g_supplicant_interface_peer_lookup(wifi->interface,
410 connman_peer_get_identifier(peer));
414 peer_params.path = g_strdup(g_supplicant_peer_get_path(gs_peer));
416 ret = g_supplicant_interface_p2p_disconnect(wifi->interface,
418 g_free(peer_params.path);
420 if (ret == -EINPROGRESS) {
421 peer_cancel_timeout(wifi);
422 wifi->p2p_device = false;
428 struct peer_service_registration {
429 peer_service_registration_cb_t callback;
433 static bool is_service_wfd(const unsigned char *specs, int length)
435 if (length < 9 || specs[0] != 0 || specs[1] != 0 || specs[2] != 6)
441 static void apply_p2p_listen_on_iface(gpointer data, gpointer user_data)
443 struct wifi_data *wifi = data;
445 if (!wifi->interface ||
446 !g_supplicant_interface_has_p2p(wifi->interface))
449 if (!wifi->servicing) {
450 g_supplicant_interface_p2p_listen(wifi->interface,
451 P2P_LISTEN_PERIOD, P2P_LISTEN_INTERVAL);
457 static void register_wfd_service_cb(int result,
458 GSupplicantInterface *iface, void *user_data)
460 struct peer_service_registration *reg_data = user_data;
465 g_list_foreach(iface_list, apply_p2p_listen_on_iface, NULL);
467 if (reg_data && reg_data->callback) {
468 reg_data->callback(result, reg_data->user_data);
473 static GSupplicantP2PServiceParams *fill_in_peer_service_params(
474 const unsigned char *spec,
475 int spec_length, const unsigned char *query,
476 int query_length, int version)
478 GSupplicantP2PServiceParams *params;
480 params = g_try_malloc0(sizeof(GSupplicantP2PServiceParams));
485 params->version = version;
486 if (spec_length > 0) {
487 params->service = g_malloc(spec_length);
488 memcpy(params->service, spec, spec_length);
490 } else if (query_length > 0 && spec_length > 0) {
491 params->query = g_malloc(query_length);
492 memcpy(params->query, query, query_length);
493 params->query_length = query_length;
495 params->response = g_malloc(spec_length);
496 memcpy(params->response, spec, spec_length);
497 params->response_length = spec_length;
499 if (spec_length > 0) {
500 params->wfd_ies = g_malloc(spec_length);
501 memcpy(params->wfd_ies, spec, spec_length);
503 params->wfd_ies_length = spec_length;
509 static void free_peer_service_params(GSupplicantP2PServiceParams *params)
514 g_free(params->service);
515 g_free(params->query);
516 g_free(params->response);
517 g_free(params->wfd_ies);
522 static int peer_register_wfd_service(const unsigned char *specification,
523 int specification_length,
524 peer_service_registration_cb_t callback,
527 struct peer_service_registration *reg_data = NULL;
528 static GSupplicantP2PServiceParams *params;
533 if (wfd_service_registered)
536 params = fill_in_peer_service_params(specification,
537 specification_length, NULL, 0, 0);
541 reg_data = g_try_malloc0(sizeof(*reg_data));
547 reg_data->callback = callback;
548 reg_data->user_data = user_data;
550 ret = g_supplicant_set_widi_ies(params,
551 register_wfd_service_cb, reg_data);
552 if (ret < 0 && ret != -EINPROGRESS)
555 wfd_service_registered = true;
559 free_peer_service_params(params);
565 static void register_peer_service_cb(int result,
566 GSupplicantInterface *iface, void *user_data)
568 struct wifi_data *wifi = g_supplicant_interface_get_data(iface);
569 struct peer_service_registration *reg_data = user_data;
574 apply_p2p_listen_on_iface(wifi, NULL);
576 if (reg_data->callback)
577 reg_data->callback(result, reg_data->user_data);
582 static int peer_register_service(const unsigned char *specification,
583 int specification_length,
584 const unsigned char *query,
585 int query_length, int version,
586 peer_service_registration_cb_t callback,
589 struct peer_service_registration *reg_data;
590 GSupplicantP2PServiceParams *params;
597 if (specification && !version && !query &&
598 is_service_wfd(specification, specification_length)) {
599 return peer_register_wfd_service(specification,
600 specification_length, callback, user_data);
603 reg_data = g_try_malloc0(sizeof(*reg_data));
607 reg_data->callback = callback;
608 reg_data->user_data = user_data;
612 for (list = iface_list; list; list = list->next) {
613 struct wifi_data *wifi = list->data;
614 GSupplicantInterface *iface = wifi->interface;
616 if (!g_supplicant_interface_has_p2p(iface))
619 params = fill_in_peer_service_params(specification,
620 specification_length, query,
621 query_length, version);
626 ret_f = g_supplicant_interface_p2p_add_service(iface,
627 register_peer_service_cb, params, reg_data);
628 if (ret_f == 0 || ret_f == -EINPROGRESS)
632 ret = g_supplicant_interface_p2p_add_service(iface,
633 register_peer_service_cb, params, NULL);
634 if (ret != 0 && ret != -EINPROGRESS)
635 free_peer_service_params(params);
638 if (ret_f != 0 && ret_f != -EINPROGRESS)
644 static int peer_unregister_wfd_service(void)
646 GSupplicantP2PServiceParams *params;
649 if (!wfd_service_registered)
652 params = fill_in_peer_service_params(NULL, 0, NULL, 0, 0);
656 wfd_service_registered = false;
658 g_supplicant_set_widi_ies(params, NULL, NULL);
660 for (list = iface_list; list; list = list->next) {
661 struct wifi_data *wifi = list->data;
663 if (!g_supplicant_interface_has_p2p(wifi->interface))
667 if (!wifi->servicing || wifi->servicing < 0) {
668 g_supplicant_interface_p2p_listen(wifi->interface,
677 static int peer_unregister_service(const unsigned char *specification,
678 int specification_length,
679 const unsigned char *query,
680 int query_length, int version)
682 GSupplicantP2PServiceParams *params;
687 if (specification && !version && !query &&
688 is_service_wfd(specification, specification_length)) {
689 ret = peer_unregister_wfd_service();
690 if (ret != 0 && ret != -EINPROGRESS)
695 for (list = iface_list; list; list = list->next) {
696 struct wifi_data *wifi = list->data;
697 GSupplicantInterface *iface = wifi->interface;
702 if (!g_supplicant_interface_has_p2p(iface))
705 params = fill_in_peer_service_params(specification,
706 specification_length, query,
707 query_length, version);
711 ret = g_supplicant_interface_p2p_del_service(iface, params);
712 if (ret != 0 && ret != -EINPROGRESS)
713 free_peer_service_params(params);
716 if (!wifi->servicing || wifi->servicing < 0) {
717 g_supplicant_interface_p2p_listen(iface, 0, 0);
725 static struct connman_peer_driver peer_driver = {
726 .connect = peer_connect,
727 .disconnect = peer_disconnect,
728 .register_service = peer_register_service,
729 .unregister_service = peer_unregister_service,
732 static void handle_tethering(struct wifi_data *wifi)
734 if (!wifi->tethering)
743 DBG("index %d bridge %s", wifi->index, wifi->bridge);
745 if (connman_inet_add_to_bridge(wifi->index, wifi->bridge) < 0)
748 wifi->bridged = true;
751 static void wifi_newlink(unsigned flags, unsigned change, void *user_data)
753 struct connman_device *device = user_data;
754 struct wifi_data *wifi = connman_device_get_data(device);
759 DBG("index %d flags %d change %d", wifi->index, flags, change);
761 if ((wifi->flags & IFF_UP) != (flags & IFF_UP)) {
765 DBG("interface down");
768 if ((wifi->flags & IFF_LOWER_UP) != (flags & IFF_LOWER_UP)) {
769 if (flags & IFF_LOWER_UP)
775 if (flags & IFF_LOWER_UP)
776 handle_tethering(wifi);
781 static int wifi_probe(struct connman_device *device)
783 struct wifi_data *wifi;
785 DBG("device %p", device);
787 wifi = g_try_new0(struct wifi_data, 1);
791 wifi->state = G_SUPPLICANT_STATE_INACTIVE;
792 wifi->ap_supported = WIFI_AP_UNKNOWN;
793 wifi->tethering_param = NULL;
795 connman_device_set_data(device, wifi);
796 wifi->device = connman_device_ref(device);
798 wifi->index = connman_device_get_index(device);
801 wifi->watch = connman_rtnl_add_newlink_watch(wifi->index,
802 wifi_newlink, device);
803 if (is_p2p_connecting())
804 add_pending_wifi_device(wifi);
806 iface_list = g_list_append(iface_list, wifi);
811 static void remove_networks(struct connman_device *device,
812 struct wifi_data *wifi)
816 for (list = wifi->networks; list; list = list->next) {
817 struct connman_network *network = list->data;
819 connman_device_remove_network(device, network);
820 connman_network_unref(network);
823 g_slist_free(wifi->networks);
824 wifi->networks = NULL;
827 static void remove_peers(struct wifi_data *wifi)
831 for (list = wifi->peers; list; list = list->next) {
832 struct connman_peer *peer = list->data;
834 connman_peer_unregister(peer);
835 connman_peer_unref(peer);
838 g_slist_free(wifi->peers);
842 static void reset_autoscan(struct connman_device *device)
844 struct wifi_data *wifi = connman_device_get_data(device);
845 struct autoscan_params *autoscan;
849 if (!wifi || !wifi->autoscan)
852 autoscan = wifi->autoscan;
854 autoscan->interval = 0;
856 if (autoscan->timeout == 0)
859 g_source_remove(autoscan->timeout);
860 autoscan->timeout = 0;
862 connman_device_unref(device);
865 static void stop_autoscan(struct connman_device *device)
867 const struct wifi_data *wifi = connman_device_get_data(device);
869 if (!wifi || !wifi->autoscan)
872 reset_autoscan(device);
874 connman_device_set_scanning(device, CONNMAN_SERVICE_TYPE_WIFI, false);
877 static void check_p2p_technology(void)
879 bool p2p_exists = false;
882 for (list = iface_list; list; list = list->next) {
883 struct wifi_data *w = list->data;
886 g_supplicant_interface_has_p2p(w->interface))
891 connman_technology_driver_unregister(&p2p_tech_driver);
892 connman_peer_driver_unregister(&peer_driver);
896 static void wifi_remove(struct connman_device *device)
898 struct wifi_data *wifi = connman_device_get_data(device);
900 DBG("device %p wifi %p", device, wifi);
905 stop_autoscan(device);
907 if (wifi->p2p_device)
908 p2p_iface_list = g_list_remove(p2p_iface_list, wifi);
910 iface_list = g_list_remove(iface_list, wifi);
912 check_p2p_technology();
914 remove_pending_wifi_device(wifi);
916 if (connman_device_get_scanning(device, CONNMAN_SERVICE_TYPE_P2P)) {
917 g_source_remove(wifi->p2p_find_timeout);
918 connman_device_unref(wifi->device);
921 if (wifi->p2p_connection_timeout)
922 g_source_remove(wifi->p2p_connection_timeout);
924 remove_networks(device, wifi);
927 connman_device_set_powered(device, false);
928 connman_device_set_data(device, NULL);
929 connman_device_unref(wifi->device);
930 connman_rtnl_remove_watch(wifi->watch);
932 g_supplicant_interface_set_data(wifi->interface, NULL);
934 g_supplicant_interface_cancel(wifi->interface);
936 if (wifi->scan_params)
937 g_supplicant_free_scan_params(wifi->scan_params);
939 g_free(wifi->autoscan);
940 g_free(wifi->identifier);
944 static bool is_duplicate(GSList *list, gchar *ssid, int ssid_len)
948 for (iter = list; iter; iter = g_slist_next(iter)) {
949 struct scan_ssid *scan_ssid = iter->data;
951 if (ssid_len == scan_ssid->ssid_len &&
952 memcmp(ssid, scan_ssid->ssid, ssid_len) == 0)
959 static int add_scan_param(gchar *hex_ssid, char *raw_ssid, int ssid_len,
960 int freq, GSupplicantScanParams *scan_data,
961 int driver_max_scan_ssids, char *ssid_name)
964 struct scan_ssid *scan_ssid;
966 if ((driver_max_scan_ssids == 0 ||
967 driver_max_scan_ssids > scan_data->num_ssids) &&
968 (hex_ssid || raw_ssid)) {
970 unsigned int j = 0, hex;
973 size_t hex_ssid_len = strlen(hex_ssid);
975 ssid = g_try_malloc0(hex_ssid_len / 2);
979 for (i = 0; i < hex_ssid_len; i += 2) {
980 sscanf(hex_ssid + i, "%02x", &hex);
989 * If we have already added hidden AP to the list,
990 * then do not do it again. This might happen if you have
991 * used or are using multiple wifi cards, so in that case
992 * you might have multiple service files for same AP.
994 if (is_duplicate(scan_data->ssids, ssid, j)) {
1000 scan_ssid = g_try_new(struct scan_ssid, 1);
1007 memcpy(scan_ssid->ssid, ssid, j);
1008 scan_ssid->ssid_len = j;
1009 scan_data->ssids = g_slist_prepend(scan_data->ssids,
1012 scan_data->num_ssids++;
1014 DBG("SSID %s added to scanned list of %d entries", ssid_name,
1015 scan_data->num_ssids);
1022 scan_data->ssids = g_slist_reverse(scan_data->ssids);
1024 if (!scan_data->freqs) {
1025 scan_data->freqs = g_try_malloc0(sizeof(uint16_t));
1026 if (!scan_data->freqs) {
1027 g_slist_free_full(scan_data->ssids, g_free);
1031 scan_data->num_freqs = 1;
1032 scan_data->freqs[0] = freq;
1034 bool duplicate = false;
1036 /* Don't add duplicate entries */
1037 for (i = 0; i < scan_data->num_freqs; i++) {
1038 if (scan_data->freqs[i] == freq) {
1045 scan_data->num_freqs++;
1046 scan_data->freqs = g_try_realloc(scan_data->freqs,
1047 sizeof(uint16_t) * scan_data->num_freqs);
1048 if (!scan_data->freqs) {
1049 g_slist_free_full(scan_data->ssids, g_free);
1052 scan_data->freqs[scan_data->num_freqs - 1] = freq;
1059 static int get_hidden_connections(GSupplicantScanParams *scan_data)
1061 struct connman_config_entry **entries;
1067 int num_ssids = 0, add_param_failed = 0;
1069 services = connman_storage_get_services();
1070 for (i = 0; services && services[i]; i++) {
1071 if (strncmp(services[i], "wifi_", 5) != 0)
1074 keyfile = connman_storage_load_service(services[i]);
1078 value = g_key_file_get_boolean(keyfile,
1079 services[i], "Hidden", NULL);
1081 g_key_file_free(keyfile);
1085 value = g_key_file_get_boolean(keyfile,
1086 services[i], "Favorite", NULL);
1088 g_key_file_free(keyfile);
1092 ssid = g_key_file_get_string(keyfile,
1093 services[i], "SSID", NULL);
1095 name = g_key_file_get_string(keyfile, services[i], "Name",
1098 ret = add_scan_param(ssid, NULL, 0, 0, scan_data, 0, name);
1106 g_key_file_free(keyfile);
1110 * Check if there are any hidden AP that needs to be provisioned.
1112 entries = connman_config_get_entries("wifi");
1113 for (i = 0; entries && entries[i]; i++) {
1116 if (!entries[i]->hidden)
1119 if (!entries[i]->ssid) {
1120 ssid = entries[i]->name;
1123 ssid = entries[i]->ssid;
1124 len = entries[i]->ssid_len;
1130 ret = add_scan_param(NULL, ssid, len, 0, scan_data, 0, ssid);
1137 connman_config_free_entries(entries);
1139 if (add_param_failed > 0)
1140 DBG("Unable to scan %d out of %d SSIDs",
1141 add_param_failed, num_ssids);
1143 g_strfreev(services);
1148 static int get_hidden_connections_params(struct wifi_data *wifi,
1149 GSupplicantScanParams *scan_params)
1151 int driver_max_ssids, i;
1152 GSupplicantScanParams *orig_params;
1155 * Scan hidden networks so that we can autoconnect to them.
1156 * We will assume 1 as a default number of ssid to scan.
1158 driver_max_ssids = g_supplicant_interface_get_max_scan_ssids(
1160 if (driver_max_ssids == 0)
1161 driver_max_ssids = 1;
1163 DBG("max ssids %d", driver_max_ssids);
1165 if (!wifi->scan_params) {
1166 wifi->scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
1167 if (!wifi->scan_params)
1170 if (get_hidden_connections(wifi->scan_params) == 0) {
1171 g_supplicant_free_scan_params(wifi->scan_params);
1172 wifi->scan_params = NULL;
1178 orig_params = wifi->scan_params;
1180 /* Let's transfer driver_max_ssids params */
1181 for (i = 0; i < driver_max_ssids; i++) {
1182 struct scan_ssid *ssid;
1184 if (!wifi->scan_params->ssids)
1187 ssid = orig_params->ssids->data;
1188 orig_params->ssids = g_slist_remove(orig_params->ssids, ssid);
1189 scan_params->ssids = g_slist_prepend(scan_params->ssids, ssid);
1193 scan_params->num_ssids = i;
1194 scan_params->ssids = g_slist_reverse(scan_params->ssids);
1196 if (orig_params->num_freqs <= 0)
1199 scan_params->freqs =
1200 g_malloc(sizeof(uint16_t) * orig_params->num_freqs);
1201 memcpy(scan_params->freqs, orig_params->freqs,
1202 sizeof(uint16_t) *orig_params->num_freqs);
1204 scan_params->num_freqs = orig_params->num_freqs;
1209 orig_params->num_ssids -= scan_params->num_ssids;
1211 return scan_params->num_ssids;
1214 g_slist_free_full(scan_params->ssids, g_free);
1215 g_supplicant_free_scan_params(wifi->scan_params);
1216 wifi->scan_params = NULL;
1221 static int throw_wifi_scan(struct connman_device *device,
1222 GSupplicantInterfaceCallback callback)
1224 struct wifi_data *wifi = connman_device_get_data(device);
1230 DBG("device %p %p", device, wifi->interface);
1232 if (wifi->tethering)
1235 if (connman_device_get_scanning(device, CONNMAN_SERVICE_TYPE_WIFI))
1238 connman_device_ref(device);
1240 ret = g_supplicant_interface_scan(wifi->interface, NULL,
1243 connman_device_set_scanning(device,
1244 CONNMAN_SERVICE_TYPE_WIFI, true);
1246 connman_device_unref(device);
1251 static void hidden_free(struct hidden_params *hidden)
1256 if (hidden->scan_params)
1257 g_supplicant_free_scan_params(hidden->scan_params);
1258 g_free(hidden->identity);
1259 g_free(hidden->passphrase);
1260 g_free(hidden->security);
1264 static void scan_callback(int result, GSupplicantInterface *interface,
1267 struct connman_device *device = user_data;
1268 struct wifi_data *wifi = connman_device_get_data(device);
1271 DBG("result %d wifi %p", result, wifi);
1274 if (wifi->hidden && !wifi->postpone_hidden) {
1275 connman_network_clear_hidden(wifi->hidden->user_data);
1276 hidden_free(wifi->hidden);
1277 wifi->hidden = NULL;
1280 if (wifi->scan_params) {
1281 g_supplicant_free_scan_params(wifi->scan_params);
1282 wifi->scan_params = NULL;
1287 connman_device_reset_scanning(device);
1289 /* User is connecting to a hidden AP, let's wait for finished event */
1290 if (wifi && wifi->hidden && wifi->postpone_hidden) {
1291 GSupplicantScanParams *scan_params;
1294 wifi->postpone_hidden = false;
1295 scan_params = wifi->hidden->scan_params;
1296 wifi->hidden->scan_params = NULL;
1298 reset_autoscan(device);
1300 ret = g_supplicant_interface_scan(wifi->interface, scan_params,
1301 scan_callback, device);
1305 /* On error, let's recall scan_callback, which will cleanup */
1306 return scan_callback(ret, interface, user_data);
1309 scanning = connman_device_get_scanning(device, CONNMAN_SERVICE_TYPE_WIFI);
1312 connman_device_set_scanning(device,
1313 CONNMAN_SERVICE_TYPE_WIFI, false);
1316 if (result != -ENOLINK)
1317 start_autoscan(device);
1320 * If we are here then we were scanning; however, if we are
1321 * also mid-flight disabling the interface, then wifi_disable
1322 * has already cleared the device scanning state and
1323 * unreferenced the device, obviating the need to do it here.
1327 connman_device_unref(device);
1330 static void scan_callback_hidden(int result,
1331 GSupplicantInterface *interface, void *user_data)
1333 struct connman_device *device = user_data;
1334 struct wifi_data *wifi = connman_device_get_data(device);
1335 GSupplicantScanParams *scan_params;
1338 DBG("result %d wifi %p", result, wifi);
1343 /* User is trying to connect to a hidden AP */
1344 if (wifi->hidden && wifi->postpone_hidden)
1347 scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
1351 if (get_hidden_connections_params(wifi, scan_params) > 0) {
1352 ret = g_supplicant_interface_scan(wifi->interface,
1354 scan_callback_hidden,
1360 g_supplicant_free_scan_params(scan_params);
1363 scan_callback(result, interface, user_data);
1366 static gboolean autoscan_timeout(gpointer data)
1368 struct connman_device *device = data;
1369 struct wifi_data *wifi = connman_device_get_data(device);
1370 struct autoscan_params *autoscan;
1376 autoscan = wifi->autoscan;
1378 if (autoscan->interval <= 0) {
1379 interval = autoscan->base;
1382 interval = autoscan->interval * autoscan->base;
1384 if (interval > autoscan->limit)
1385 interval = autoscan->limit;
1387 throw_wifi_scan(wifi->device, scan_callback_hidden);
1390 * In case BackgroundScanning is disabled, interval will reach the
1391 * limit exactly after the very first passive scanning. It allows
1392 * to ensure at most one passive scan is performed in such cases.
1394 if (!connman_setting_get_bool("BackgroundScanning") &&
1395 interval == autoscan->limit) {
1396 g_source_remove(autoscan->timeout);
1397 autoscan->timeout = 0;
1399 connman_device_unref(device);
1405 DBG("interval %d", interval);
1407 autoscan->interval = interval;
1409 autoscan->timeout = g_timeout_add_seconds(interval,
1410 autoscan_timeout, device);
1415 static void start_autoscan(struct connman_device *device)
1417 struct wifi_data *wifi = connman_device_get_data(device);
1418 struct autoscan_params *autoscan;
1425 if (wifi->p2p_device)
1428 if (wifi->connected)
1431 autoscan = wifi->autoscan;
1435 if (autoscan->timeout > 0 || autoscan->interval > 0)
1438 connman_device_ref(device);
1440 autoscan_timeout(device);
1443 static struct autoscan_params *parse_autoscan_params(const char *params)
1445 struct autoscan_params *autoscan;
1452 list_params = g_strsplit(params, ":", 0);
1453 if (list_params == 0)
1456 if (!g_strcmp0(list_params[0], "exponential") &&
1457 g_strv_length(list_params) == 3) {
1458 base = atoi(list_params[1]);
1459 limit = atoi(list_params[2]);
1460 } else if (!g_strcmp0(list_params[0], "single") &&
1461 g_strv_length(list_params) == 2)
1462 base = limit = atoi(list_params[1]);
1464 g_strfreev(list_params);
1468 DBG("Setup %s autoscanning", list_params[0]);
1470 g_strfreev(list_params);
1472 autoscan = g_try_malloc0(sizeof(struct autoscan_params));
1474 DBG("Could not allocate memory for autoscan");
1478 DBG("base %d - limit %d", base, limit);
1479 autoscan->base = base;
1480 autoscan->limit = limit;
1485 static void setup_autoscan(struct wifi_data *wifi)
1488 * If BackgroundScanning is enabled, setup exponential
1489 * autoscanning if it has not been previously done.
1491 if (connman_setting_get_bool("BackgroundScanning")) {
1492 wifi->autoscan = parse_autoscan_params(AUTOSCAN_EXPONENTIAL);
1497 * On the contrary, if BackgroundScanning is disabled, update autoscan
1498 * parameters based on the type of scanning that is being performed.
1500 if (wifi->autoscan) {
1501 g_free(wifi->autoscan);
1502 wifi->autoscan = NULL;
1505 switch (wifi->scanning_type) {
1506 case WIFI_SCANNING_PASSIVE:
1507 /* Do not setup autoscan. */
1509 case WIFI_SCANNING_ACTIVE:
1510 /* Setup one single passive scan after active. */
1511 wifi->autoscan = parse_autoscan_params(AUTOSCAN_SINGLE);
1513 case WIFI_SCANNING_UNKNOWN:
1514 /* Setup autoscan in this case but we should never fall here. */
1515 wifi->autoscan = parse_autoscan_params(AUTOSCAN_SINGLE);
1520 static void finalize_interface_creation(struct wifi_data *wifi)
1522 DBG("interface is ready wifi %p tethering %d", wifi, wifi->tethering);
1524 if (!wifi->device) {
1525 connman_error("WiFi device not set");
1529 connman_device_set_powered(wifi->device, true);
1531 if (wifi->p2p_device)
1534 if (!wifi->autoscan)
1535 setup_autoscan(wifi);
1537 start_autoscan(wifi->device);
1540 static void interface_create_callback(int result,
1541 GSupplicantInterface *interface,
1544 struct wifi_data *wifi = user_data;
1545 char *bgscan_range_max;
1548 DBG("result %d ifname %s, wifi %p", result,
1549 g_supplicant_interface_get_ifname(interface),
1552 if (result < 0 || !wifi)
1555 wifi->interface = interface;
1556 g_supplicant_interface_set_data(interface, wifi);
1558 if (g_supplicant_interface_get_ready(interface)) {
1559 wifi->interface_ready = true;
1560 finalize_interface_creation(wifi);
1564 * Set the BSS expiration age to match the long scanning
1565 * interval to avoid the loss of unconnected networks between
1568 bgscan_range_max = strrchr(BGSCAN_DEFAULT, ':');
1569 if (!bgscan_range_max || strlen(bgscan_range_max) < 1)
1572 value = strtol(bgscan_range_max + 1, NULL, 10);
1573 if (value <= 0 || errno == ERANGE)
1576 if (g_supplicant_interface_set_bss_expiration_age(interface,
1577 value + SCAN_MAX_DURATION) < 0) {
1578 connman_warn("Failed to set bss expiration age");
1582 static int wifi_enable(struct connman_device *device)
1584 struct wifi_data *wifi = connman_device_get_data(device);
1587 const char *driver = connman_setting_get_string("wifi");
1590 DBG("device %p %p", device, wifi);
1592 index = connman_device_get_index(device);
1593 if (!wifi || index < 0)
1596 if (is_p2p_connecting())
1597 return -EINPROGRESS;
1599 interface = connman_inet_ifname(index);
1600 ret = g_supplicant_interface_create(interface, driver, NULL,
1601 interface_create_callback,
1608 return -EINPROGRESS;
1611 static int wifi_disable(struct connman_device *device)
1613 struct wifi_data *wifi = connman_device_get_data(device);
1616 DBG("device %p wifi %p", device, wifi);
1621 wifi->connected = false;
1622 wifi->disconnecting = false;
1624 if (wifi->pending_network)
1625 wifi->pending_network = NULL;
1627 stop_autoscan(device);
1629 if (connman_device_get_scanning(device, CONNMAN_SERVICE_TYPE_P2P)) {
1630 g_source_remove(wifi->p2p_find_timeout);
1631 wifi->p2p_find_timeout = 0;
1632 connman_device_set_scanning(device, CONNMAN_SERVICE_TYPE_P2P, false);
1633 connman_device_unref(wifi->device);
1636 /* In case of a user scan, device is still referenced */
1637 if (connman_device_get_scanning(device, CONNMAN_SERVICE_TYPE_WIFI)) {
1638 connman_device_set_scanning(device,
1639 CONNMAN_SERVICE_TYPE_WIFI, false);
1640 connman_device_unref(wifi->device);
1643 remove_networks(device, wifi);
1646 ret = g_supplicant_interface_remove(wifi->interface, NULL, NULL);
1650 return -EINPROGRESS;
1653 struct last_connected {
1654 struct timeval modified;
1659 static gint sort_entry(gconstpointer a, gconstpointer b, gpointer user_data)
1661 struct timeval *aval = (struct timeval *)a;
1662 struct timeval *bval = (struct timeval *)b;
1664 /* Note that the sort order is descending */
1665 if (aval->tv_sec < bval->tv_sec)
1668 if (aval->tv_sec > bval->tv_sec)
1674 static void free_entry(gpointer data)
1676 struct last_connected *entry = data;
1678 g_free(entry->ssid);
1682 static int get_latest_connections(int max_ssids,
1683 GSupplicantScanParams *scan_data)
1685 GSequenceIter *iter;
1686 GSequence *latest_list;
1687 struct last_connected *entry;
1689 struct timeval modified;
1696 latest_list = g_sequence_new(free_entry);
1700 services = connman_storage_get_services();
1701 for (i = 0; services && services[i]; i++) {
1702 if (strncmp(services[i], "wifi_", 5) != 0)
1705 keyfile = connman_storage_load_service(services[i]);
1709 str = g_key_file_get_string(keyfile,
1710 services[i], "Favorite", NULL);
1711 if (!str || g_strcmp0(str, "true")) {
1713 g_key_file_free(keyfile);
1718 str = g_key_file_get_string(keyfile,
1719 services[i], "AutoConnect", NULL);
1720 if (!str || g_strcmp0(str, "true")) {
1722 g_key_file_free(keyfile);
1727 str = g_key_file_get_string(keyfile,
1728 services[i], "Modified", NULL);
1730 g_key_file_free(keyfile);
1733 util_iso8601_to_timeval(str, &modified);
1736 ssid = g_key_file_get_string(keyfile,
1737 services[i], "SSID", NULL);
1739 freq = g_key_file_get_integer(keyfile, services[i],
1742 entry = g_try_new(struct last_connected, 1);
1744 g_sequence_free(latest_list);
1745 g_key_file_free(keyfile);
1751 entry->modified = modified;
1754 g_sequence_insert_sorted(latest_list, entry,
1760 g_key_file_free(keyfile);
1763 g_strfreev(services);
1765 num_ssids = num_ssids > max_ssids ? max_ssids : num_ssids;
1767 iter = g_sequence_get_begin_iter(latest_list);
1769 for (i = 0; i < num_ssids; i++) {
1770 entry = g_sequence_get(iter);
1772 DBG("ssid %s freq %d modified %lu", entry->ssid, entry->freq,
1773 entry->modified.tv_sec);
1775 add_scan_param(entry->ssid, NULL, 0, entry->freq, scan_data,
1776 max_ssids, entry->ssid);
1778 iter = g_sequence_iter_next(iter);
1781 g_sequence_free(latest_list);
1785 static void wifi_update_scanner_type(struct wifi_data *wifi,
1786 enum wifi_scanning_type new_type)
1790 if (!wifi || wifi->scanning_type == new_type)
1793 wifi->scanning_type = new_type;
1795 setup_autoscan(wifi);
1798 static int wifi_scan_simple(struct connman_device *device)
1800 struct wifi_data *wifi = connman_device_get_data(device);
1802 reset_autoscan(device);
1804 /* Distinguish between devices performing passive and active scanning */
1806 wifi_update_scanner_type(wifi, WIFI_SCANNING_PASSIVE);
1808 return throw_wifi_scan(device, scan_callback_hidden);
1811 static gboolean p2p_find_stop(gpointer data)
1813 struct connman_device *device = data;
1814 struct wifi_data *wifi = connman_device_get_data(device);
1819 wifi->p2p_find_timeout = 0;
1821 g_supplicant_interface_p2p_stop_find(wifi->interface);
1824 connman_device_set_scanning(device, CONNMAN_SERVICE_TYPE_P2P, false);
1826 connman_device_unref(device);
1827 start_autoscan(device);
1832 static void p2p_find_callback(int result, GSupplicantInterface *interface,
1835 struct connman_device *device = user_data;
1836 struct wifi_data *wifi = connman_device_get_data(device);
1838 DBG("result %d wifi %p", result, wifi);
1843 if (wifi->p2p_find_timeout) {
1844 g_source_remove(wifi->p2p_find_timeout);
1845 wifi->p2p_find_timeout = 0;
1851 wifi->p2p_find_timeout = g_timeout_add_seconds(P2P_FIND_TIMEOUT,
1852 p2p_find_stop, device);
1853 if (!wifi->p2p_find_timeout)
1858 p2p_find_stop(device);
1861 static int p2p_find(struct connman_device *device)
1863 struct wifi_data *wifi;
1868 if (!p2p_technology)
1871 wifi = connman_device_get_data(device);
1873 if (g_supplicant_interface_is_p2p_finding(wifi->interface))
1876 reset_autoscan(device);
1877 connman_device_ref(device);
1879 ret = g_supplicant_interface_p2p_find(wifi->interface,
1880 p2p_find_callback, device);
1882 connman_device_unref(device);
1883 start_autoscan(device);
1885 connman_device_set_scanning(device,
1886 CONNMAN_SERVICE_TYPE_P2P, true);
1893 * Note that the hidden scan is only used when connecting to this specific
1894 * hidden AP first time. It is not used when system autoconnects to hidden AP.
1896 static int wifi_scan(struct connman_device *device,
1897 struct connman_device_scan_params *params)
1899 struct wifi_data *wifi = connman_device_get_data(device);
1900 GSupplicantScanParams *scan_params = NULL;
1901 struct scan_ssid *scan_ssid;
1902 struct hidden_params *hidden;
1904 int driver_max_ssids = 0;
1911 if (wifi->p2p_device)
1914 if (wifi->tethering)
1917 if (params->type == CONNMAN_SERVICE_TYPE_P2P)
1918 return p2p_find(device);
1920 DBG("device %p wifi %p hidden ssid %s", device, wifi->interface,
1923 scanning = connman_device_get_scanning(device, CONNMAN_SERVICE_TYPE_WIFI);
1925 if (!params->ssid || params->ssid_len == 0 || params->ssid_len > 32) {
1929 driver_max_ssids = g_supplicant_interface_get_max_scan_ssids(
1931 DBG("max ssids %d", driver_max_ssids);
1932 if (driver_max_ssids == 0)
1933 return wifi_scan_simple(device);
1937 if (scanning && wifi->hidden && wifi->postpone_hidden)
1943 scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
1948 scan_ssid = g_try_new(struct scan_ssid, 1);
1950 g_free(scan_params);
1954 memcpy(scan_ssid->ssid, params->ssid, params->ssid_len);
1955 scan_ssid->ssid_len = params->ssid_len;
1956 scan_params->ssids = g_slist_prepend(scan_params->ssids,
1958 scan_params->num_ssids = 1;
1960 hidden = g_try_new0(struct hidden_params, 1);
1962 g_supplicant_free_scan_params(scan_params);
1967 hidden_free(wifi->hidden);
1968 wifi->hidden = NULL;
1971 memcpy(hidden->ssid, params->ssid, params->ssid_len);
1972 hidden->ssid_len = params->ssid_len;
1973 hidden->identity = g_strdup(params->identity);
1974 hidden->passphrase = g_strdup(params->passphrase);
1975 hidden->security = g_strdup(params->security);
1976 hidden->user_data = params->user_data;
1977 wifi->hidden = hidden;
1980 /* Let's keep this active scan for later,
1981 * when current scan will be over. */
1982 wifi->postpone_hidden = TRUE;
1983 hidden->scan_params = scan_params;
1987 } else if (wifi->connected) {
1988 g_supplicant_free_scan_params(scan_params);
1989 return wifi_scan_simple(device);
1990 } else if (!params->force_full_scan) {
1991 ret = get_latest_connections(driver_max_ssids, scan_params);
1993 g_supplicant_free_scan_params(scan_params);
1994 return wifi_scan_simple(device);
1998 /* Distinguish between devices performing passive and active scanning */
1999 wifi_update_scanner_type(wifi, WIFI_SCANNING_ACTIVE);
2001 connman_device_ref(device);
2003 reset_autoscan(device);
2005 ret = g_supplicant_interface_scan(wifi->interface, scan_params,
2006 scan_callback, device);
2008 connman_device_set_scanning(device,
2009 CONNMAN_SERVICE_TYPE_WIFI, true);
2011 g_supplicant_free_scan_params(scan_params);
2012 connman_device_unref(device);
2015 hidden_free(wifi->hidden);
2016 wifi->hidden = NULL;
2023 static void wifi_stop_scan(enum connman_service_type type,
2024 struct connman_device *device)
2026 struct wifi_data *wifi = connman_device_get_data(device);
2028 DBG("device %p wifi %p", device, wifi);
2033 if (type == CONNMAN_SERVICE_TYPE_P2P) {
2034 if (connman_device_get_scanning(device, CONNMAN_SERVICE_TYPE_P2P)) {
2035 g_source_remove(wifi->p2p_find_timeout);
2036 p2p_find_stop(device);
2041 static void wifi_regdom_callback(int result,
2045 struct connman_device *device = user_data;
2047 connman_device_regdom_notify(device, result, alpha2);
2049 connman_device_unref(device);
2052 static int wifi_set_regdom(struct connman_device *device, const char *alpha2)
2054 struct wifi_data *wifi = connman_device_get_data(device);
2060 connman_device_ref(device);
2062 ret = g_supplicant_interface_set_country(wifi->interface,
2063 wifi_regdom_callback,
2066 connman_device_unref(device);
2071 static struct connman_device_driver wifi_ng_driver = {
2073 .type = CONNMAN_DEVICE_TYPE_WIFI,
2074 .priority = CONNMAN_DEVICE_PRIORITY_LOW,
2075 .probe = wifi_probe,
2076 .remove = wifi_remove,
2077 .enable = wifi_enable,
2078 .disable = wifi_disable,
2080 .stop_scan = wifi_stop_scan,
2081 .set_regdom = wifi_set_regdom,
2084 static void system_ready(void)
2088 if (connman_device_driver_register(&wifi_ng_driver) < 0)
2089 connman_error("Failed to register WiFi driver");
2092 static void system_killed(void)
2096 connman_device_driver_unregister(&wifi_ng_driver);
2099 static int network_probe(struct connman_network *network)
2101 DBG("network %p", network);
2106 static void network_remove(struct connman_network *network)
2108 struct connman_device *device = connman_network_get_device(network);
2109 struct wifi_data *wifi;
2111 DBG("network %p", network);
2113 wifi = connman_device_get_data(device);
2117 if (wifi->network != network)
2120 wifi->network = NULL;
2123 static void connect_callback(int result, GSupplicantInterface *interface,
2126 struct connman_network *network = user_data;
2128 DBG("network %p result %d", network, result);
2130 if (result == -ENOKEY) {
2131 connman_network_set_error(network,
2132 CONNMAN_NETWORK_ERROR_INVALID_KEY);
2133 } else if (result < 0) {
2134 connman_network_set_error(network,
2135 CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
2138 connman_network_unref(network);
2141 static GSupplicantSecurity network_security(const char *security)
2143 if (g_str_equal(security, "none"))
2144 return G_SUPPLICANT_SECURITY_NONE;
2145 else if (g_str_equal(security, "wep"))
2146 return G_SUPPLICANT_SECURITY_WEP;
2147 else if (g_str_equal(security, "psk"))
2148 return G_SUPPLICANT_SECURITY_PSK;
2149 else if (g_str_equal(security, "wpa"))
2150 return G_SUPPLICANT_SECURITY_PSK;
2151 else if (g_str_equal(security, "rsn"))
2152 return G_SUPPLICANT_SECURITY_PSK;
2153 else if (g_str_equal(security, "ieee8021x"))
2154 return G_SUPPLICANT_SECURITY_IEEE8021X;
2156 return G_SUPPLICANT_SECURITY_UNKNOWN;
2159 static void ssid_init(GSupplicantSSID *ssid, struct connman_network *network)
2161 const char *security;
2163 memset(ssid, 0, sizeof(*ssid));
2164 ssid->mode = G_SUPPLICANT_MODE_INFRA;
2165 ssid->ssid = connman_network_get_blob(network, "WiFi.SSID",
2167 ssid->scan_ssid = 1;
2168 security = connman_network_get_string(network, "WiFi.Security");
2169 ssid->security = network_security(security);
2170 ssid->passphrase = connman_network_get_string(network,
2173 ssid->eap = connman_network_get_string(network, "WiFi.EAP");
2176 * If our private key password is unset,
2177 * we use the supplied passphrase. That is needed
2178 * for PEAP where 2 passphrases (identity and client
2179 * cert may have to be provided.
2181 if (!connman_network_get_string(network, "WiFi.PrivateKeyPassphrase"))
2182 connman_network_set_string(network,
2183 "WiFi.PrivateKeyPassphrase",
2185 /* We must have an identity for both PEAP and TLS */
2186 ssid->identity = connman_network_get_string(network, "WiFi.Identity");
2188 /* Use agent provided identity as a fallback */
2189 if (!ssid->identity || strlen(ssid->identity) == 0)
2190 ssid->identity = connman_network_get_string(network,
2191 "WiFi.AgentIdentity");
2193 ssid->anonymous_identity = connman_network_get_string(network,
2194 "WiFi.AnonymousIdentity");
2195 ssid->ca_cert_path = connman_network_get_string(network,
2197 ssid->subject_match = connman_network_get_string(network,
2198 "WiFi.SubjectMatch");
2199 ssid->altsubject_match = connman_network_get_string(network,
2200 "WiFi.AltSubjectMatch");
2201 ssid->domain_suffix_match = connman_network_get_string(network,
2202 "WiFi.DomainSuffixMatch");
2203 ssid->domain_match = connman_network_get_string(network,
2204 "WiFi.DomainMatch");
2205 ssid->client_cert_path = connman_network_get_string(network,
2206 "WiFi.ClientCertFile");
2207 ssid->private_key_path = connman_network_get_string(network,
2208 "WiFi.PrivateKeyFile");
2209 ssid->private_key_passphrase = connman_network_get_string(network,
2210 "WiFi.PrivateKeyPassphrase");
2211 ssid->phase2_auth = connman_network_get_string(network, "WiFi.Phase2");
2213 ssid->use_wps = connman_network_get_bool(network, "WiFi.UseWPS");
2214 ssid->pin_wps = connman_network_get_string(network, "WiFi.PinWPS");
2216 if (connman_setting_get_bool("BackgroundScanning"))
2217 ssid->bgscan = BGSCAN_DEFAULT;
2220 static int network_connect(struct connman_network *network)
2222 struct connman_device *device = connman_network_get_device(network);
2223 struct wifi_data *wifi;
2224 GSupplicantInterface *interface;
2225 GSupplicantSSID *ssid;
2227 DBG("network %p", network);
2232 wifi = connman_device_get_data(device);
2236 ssid = g_try_malloc0(sizeof(GSupplicantSSID));
2240 interface = wifi->interface;
2242 ssid_init(ssid, network);
2244 if (wifi->disconnecting) {
2245 wifi->pending_network = network;
2248 wifi->network = connman_network_ref(network);
2251 return g_supplicant_interface_connect(interface, ssid,
2252 connect_callback, network);
2255 return -EINPROGRESS;
2258 static void disconnect_callback(int result, GSupplicantInterface *interface,
2261 struct disconnect_data *dd = user_data;
2262 struct connman_network *network = dd->network;
2263 struct wifi_data *wifi = dd->wifi;
2267 DBG("result %d supplicant interface %p wifi %p networks: current %p "
2268 "pending %p disconnected %p", result, interface, wifi,
2269 wifi->network, wifi->pending_network, network);
2271 if (result == -ECONNABORTED) {
2272 DBG("wifi interface no longer available");
2276 if (g_slist_find(wifi->networks, network))
2277 connman_network_set_connected(network, false);
2279 wifi->disconnecting = false;
2281 if (network != wifi->network) {
2282 if (network == wifi->pending_network)
2283 wifi->pending_network = NULL;
2284 DBG("current wifi network has changed since disconnection");
2288 wifi->network = NULL;
2290 wifi->connected = false;
2292 if (wifi->pending_network) {
2293 network_connect(wifi->pending_network);
2294 wifi->pending_network = NULL;
2297 start_autoscan(wifi->device);
2300 static int network_disconnect(struct connman_network *network)
2302 struct connman_device *device = connman_network_get_device(network);
2303 struct disconnect_data *dd;
2304 struct wifi_data *wifi;
2307 DBG("network %p", network);
2309 wifi = connman_device_get_data(device);
2310 if (!wifi || !wifi->interface)
2313 connman_network_set_associating(network, false);
2315 if (wifi->disconnecting)
2318 wifi->disconnecting = true;
2320 dd = g_malloc0(sizeof(*dd));
2322 dd->network = network;
2324 err = g_supplicant_interface_disconnect(wifi->interface,
2325 disconnect_callback, dd);
2327 wifi->disconnecting = false;
2334 static struct connman_network_driver network_driver = {
2336 .type = CONNMAN_NETWORK_TYPE_WIFI,
2337 .priority = CONNMAN_NETWORK_PRIORITY_LOW,
2338 .probe = network_probe,
2339 .remove = network_remove,
2340 .connect = network_connect,
2341 .disconnect = network_disconnect,
2344 static void interface_added(GSupplicantInterface *interface)
2346 const char *ifname = g_supplicant_interface_get_ifname(interface);
2347 const char *driver = g_supplicant_interface_get_driver(interface);
2348 struct wifi_data *wifi;
2350 wifi = g_supplicant_interface_get_data(interface);
2352 wifi = get_pending_wifi_data(ifname);
2356 wifi->interface = interface;
2357 g_supplicant_interface_set_data(interface, wifi);
2358 p2p_iface_list = g_list_append(p2p_iface_list, wifi);
2359 wifi->p2p_device = true;
2362 DBG("ifname %s driver %s wifi %p tethering %d",
2363 ifname, driver, wifi, wifi->tethering);
2365 if (!wifi->device) {
2366 connman_error("WiFi device not set");
2370 connman_device_set_powered(wifi->device, true);
2373 static bool is_idle(struct wifi_data *wifi)
2375 DBG("state %d", wifi->state);
2377 switch (wifi->state) {
2378 case G_SUPPLICANT_STATE_UNKNOWN:
2379 case G_SUPPLICANT_STATE_DISABLED:
2380 case G_SUPPLICANT_STATE_DISCONNECTED:
2381 case G_SUPPLICANT_STATE_INACTIVE:
2382 case G_SUPPLICANT_STATE_SCANNING:
2385 case G_SUPPLICANT_STATE_AUTHENTICATING:
2386 case G_SUPPLICANT_STATE_ASSOCIATING:
2387 case G_SUPPLICANT_STATE_ASSOCIATED:
2388 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
2389 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
2390 case G_SUPPLICANT_STATE_COMPLETED:
2397 static bool is_idle_wps(GSupplicantInterface *interface,
2398 struct wifi_data *wifi)
2400 /* First, let's check if WPS processing did not went wrong */
2401 if (g_supplicant_interface_get_wps_state(interface) ==
2402 G_SUPPLICANT_WPS_STATE_FAIL)
2405 /* Unlike normal connection, being associated while processing wps
2406 * actually means that we are idling. */
2407 switch (wifi->state) {
2408 case G_SUPPLICANT_STATE_UNKNOWN:
2409 case G_SUPPLICANT_STATE_DISABLED:
2410 case G_SUPPLICANT_STATE_DISCONNECTED:
2411 case G_SUPPLICANT_STATE_INACTIVE:
2412 case G_SUPPLICANT_STATE_SCANNING:
2413 case G_SUPPLICANT_STATE_ASSOCIATED:
2415 case G_SUPPLICANT_STATE_AUTHENTICATING:
2416 case G_SUPPLICANT_STATE_ASSOCIATING:
2417 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
2418 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
2419 case G_SUPPLICANT_STATE_COMPLETED:
2426 static bool handle_wps_completion(GSupplicantInterface *interface,
2427 struct connman_network *network,
2428 struct connman_device *device,
2429 struct wifi_data *wifi)
2433 wps = connman_network_get_bool(network, "WiFi.UseWPS");
2435 const unsigned char *ssid, *wps_ssid;
2436 unsigned int ssid_len, wps_ssid_len;
2437 struct disconnect_data *dd;
2438 const char *wps_key;
2440 /* Checking if we got associated with requested
2442 ssid = connman_network_get_blob(network, "WiFi.SSID",
2445 wps_ssid = g_supplicant_interface_get_wps_ssid(
2446 interface, &wps_ssid_len);
2448 if (!wps_ssid || wps_ssid_len != ssid_len ||
2449 memcmp(ssid, wps_ssid, ssid_len) != 0) {
2450 dd = g_malloc0(sizeof(*dd));
2452 dd->network = network;
2454 connman_network_set_associating(network, false);
2455 g_supplicant_interface_disconnect(wifi->interface,
2456 disconnect_callback, dd);
2460 wps_key = g_supplicant_interface_get_wps_key(interface);
2461 connman_network_set_string(network, "WiFi.Passphrase",
2464 connman_network_set_string(network, "WiFi.PinWPS", NULL);
2470 static bool handle_assoc_status_code(GSupplicantInterface *interface,
2471 struct wifi_data *wifi)
2473 if (wifi->state == G_SUPPLICANT_STATE_ASSOCIATING &&
2474 wifi->assoc_code == ASSOC_STATUS_NO_CLIENT &&
2475 wifi->load_shaping_retries < LOAD_SHAPING_MAX_RETRIES) {
2476 wifi->load_shaping_retries ++;
2479 wifi->load_shaping_retries = 0;
2483 static bool handle_4way_handshake_failure(GSupplicantInterface *interface,
2484 struct connman_network *network,
2485 struct wifi_data *wifi)
2487 struct connman_service *service;
2489 if ((wifi->state != G_SUPPLICANT_STATE_4WAY_HANDSHAKE) &&
2490 !((wifi->state == G_SUPPLICANT_STATE_ASSOCIATING) &&
2491 (wifi->assoc_code == ASSOC_STATUS_AUTH_TIMEOUT)))
2494 if (wifi->connected)
2497 service = connman_service_lookup_from_network(network);
2503 if (connman_service_get_favorite(service)) {
2504 if (wifi->retries < FAVORITE_MAXIMUM_RETRIES)
2509 connman_network_set_error(network, CONNMAN_NETWORK_ERROR_INVALID_KEY);
2514 static void interface_state(GSupplicantInterface *interface)
2516 struct connman_network *network;
2517 struct connman_device *device;
2518 struct wifi_data *wifi;
2519 GSupplicantState state = g_supplicant_interface_get_state(interface);
2523 wifi = g_supplicant_interface_get_data(interface);
2525 DBG("wifi %p interface state %d", wifi, state);
2530 device = wifi->device;
2534 if (state == G_SUPPLICANT_STATE_COMPLETED) {
2535 if (wifi->tethering_param) {
2536 g_free(wifi->tethering_param->ssid);
2537 g_free(wifi->tethering_param);
2538 wifi->tethering_param = NULL;
2541 if (wifi->tethering)
2542 stop_autoscan(device);
2545 if (g_supplicant_interface_get_ready(interface) &&
2546 !wifi->interface_ready) {
2547 wifi->interface_ready = true;
2548 finalize_interface_creation(wifi);
2551 network = wifi->network;
2556 case G_SUPPLICANT_STATE_SCANNING:
2557 if (wifi->connected)
2558 connman_network_set_connected(network, false);
2562 case G_SUPPLICANT_STATE_AUTHENTICATING:
2563 case G_SUPPLICANT_STATE_ASSOCIATING:
2564 stop_autoscan(device);
2566 if (!wifi->connected)
2567 connman_network_set_associating(network, true);
2571 case G_SUPPLICANT_STATE_COMPLETED:
2572 /* though it should be already stopped: */
2573 stop_autoscan(device);
2575 if (!handle_wps_completion(interface, network, device, wifi))
2578 connman_network_set_connected(network, true);
2580 wifi->disconnect_code = 0;
2581 wifi->assoc_code = 0;
2582 wifi->load_shaping_retries = 0;
2585 case G_SUPPLICANT_STATE_DISCONNECTED:
2587 * If we're in one of the idle modes, we have
2588 * not started association yet and thus setting
2589 * those ones to FALSE could cancel an association
2592 wps = connman_network_get_bool(network, "WiFi.UseWPS");
2594 if (is_idle_wps(interface, wifi))
2600 if (handle_assoc_status_code(interface, wifi))
2603 /* If previous state was 4way-handshake, then
2604 * it's either: psk was incorrect and thus we retry
2605 * or if we reach the maximum retries we declare the
2607 if (handle_4way_handshake_failure(interface,
2611 /* See table 8-36 Reason codes in IEEE Std 802.11 */
2612 switch (wifi->disconnect_code) {
2613 case 6: /* Class 2 frame received from nonauthenticated STA */
2614 connman_network_set_error(network,
2615 CONNMAN_NETWORK_ERROR_BLOCKED);
2622 if (network != wifi->pending_network) {
2623 connman_network_set_connected(network, false);
2624 connman_network_set_associating(network, false);
2626 wifi->disconnecting = false;
2628 start_autoscan(device);
2632 case G_SUPPLICANT_STATE_INACTIVE:
2633 connman_network_set_associating(network, false);
2634 start_autoscan(device);
2638 case G_SUPPLICANT_STATE_UNKNOWN:
2639 case G_SUPPLICANT_STATE_DISABLED:
2640 case G_SUPPLICANT_STATE_ASSOCIATED:
2641 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
2642 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
2646 old_connected = wifi->connected;
2647 wifi->state = state;
2649 /* Saving wpa_s state policy:
2650 * If connected and if the state changes are roaming related:
2651 * --> We stay connected
2653 * --> We are connected
2655 * --> We are not connected
2658 case G_SUPPLICANT_STATE_AUTHENTICATING:
2659 case G_SUPPLICANT_STATE_ASSOCIATING:
2660 case G_SUPPLICANT_STATE_ASSOCIATED:
2661 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
2662 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
2663 if (wifi->connected)
2664 connman_warn("Probably roaming right now!"
2665 " Staying connected...");
2667 case G_SUPPLICANT_STATE_SCANNING:
2668 wifi->connected = false;
2671 start_autoscan(device);
2673 case G_SUPPLICANT_STATE_COMPLETED:
2674 wifi->connected = true;
2677 wifi->connected = false;
2684 static void interface_removed(GSupplicantInterface *interface)
2686 const char *ifname = g_supplicant_interface_get_ifname(interface);
2687 struct wifi_data *wifi;
2689 DBG("ifname %s", ifname);
2691 wifi = g_supplicant_interface_get_data(interface);
2694 wifi->interface = NULL;
2696 if (wifi && wifi->tethering)
2699 if (!wifi || !wifi->device) {
2700 DBG("wifi interface already removed");
2704 connman_device_set_powered(wifi->device, false);
2706 check_p2p_technology();
2709 static void set_device_type(const char *type, char dev_type[17])
2711 const char *oui = "0050F204";
2712 const char *category = "0001";
2713 const char *sub_category = "0000";
2715 if (!g_strcmp0(type, "handset")) {
2717 sub_category = "0005";
2718 } else if (!g_strcmp0(type, "vm") || !g_strcmp0(type, "container"))
2719 sub_category = "0001";
2720 else if (!g_strcmp0(type, "server"))
2721 sub_category = "0002";
2722 else if (!g_strcmp0(type, "laptop"))
2723 sub_category = "0005";
2724 else if (!g_strcmp0(type, "desktop"))
2725 sub_category = "0006";
2726 else if (!g_strcmp0(type, "tablet"))
2727 sub_category = "0009";
2728 else if (!g_strcmp0(type, "watch"))
2731 snprintf(dev_type, 17, "%s%s%s", category, oui, sub_category);
2734 static void p2p_support(GSupplicantInterface *interface)
2736 char dev_type[17] = {};
2737 const char *hostname;
2744 if (!g_supplicant_interface_has_p2p(interface))
2747 if (connman_technology_driver_register(&p2p_tech_driver) < 0) {
2748 DBG("Could not register P2P technology driver");
2752 hostname = connman_utsname_get_hostname();
2754 hostname = "ConnMan";
2756 set_device_type(connman_machine_get_type(), dev_type);
2757 g_supplicant_interface_set_p2p_device_config(interface,
2758 hostname, dev_type);
2759 connman_peer_driver_register(&peer_driver);
2762 static void scan_started(GSupplicantInterface *interface)
2767 static void scan_finished(GSupplicantInterface *interface)
2772 static void ap_create_fail(GSupplicantInterface *interface)
2774 struct wifi_data *wifi = g_supplicant_interface_get_data(interface);
2777 if ((wifi->tethering) && (wifi->tethering_param)) {
2778 DBG("%s create AP fail \n",
2779 g_supplicant_interface_get_ifname(wifi->interface));
2781 connman_inet_remove_from_bridge(wifi->index, wifi->bridge);
2782 wifi->ap_supported = WIFI_AP_NOT_SUPPORTED;
2783 wifi->tethering = false;
2785 ret = tech_set_tethering(wifi->tethering_param->technology,
2786 wifi->tethering_param->ssid->ssid,
2787 wifi->tethering_param->ssid->passphrase,
2788 wifi->bridge, true);
2790 if ((ret == -EOPNOTSUPP) && (wifi_technology)) {
2791 connman_technology_tethering_notify(wifi_technology,false);
2794 g_free(wifi->tethering_param->ssid);
2795 g_free(wifi->tethering_param);
2796 wifi->tethering_param = NULL;
2800 static unsigned char calculate_strength(GSupplicantNetwork *supplicant_network)
2802 unsigned char strength;
2804 strength = 120 + g_supplicant_network_get_signal(supplicant_network);
2811 static void network_added(GSupplicantNetwork *supplicant_network)
2813 struct connman_network *network;
2814 GSupplicantInterface *interface;
2815 struct wifi_data *wifi;
2816 const char *name, *identifier, *security, *group, *mode;
2817 const unsigned char *ssid;
2818 unsigned int ssid_len;
2822 bool wps_advertizing;
2824 mode = g_supplicant_network_get_mode(supplicant_network);
2825 identifier = g_supplicant_network_get_identifier(supplicant_network);
2827 DBG("%s", identifier);
2829 if (!g_strcmp0(mode, "adhoc"))
2832 interface = g_supplicant_network_get_interface(supplicant_network);
2833 wifi = g_supplicant_interface_get_data(interface);
2834 name = g_supplicant_network_get_name(supplicant_network);
2835 security = g_supplicant_network_get_security(supplicant_network);
2836 group = g_supplicant_network_get_identifier(supplicant_network);
2837 wps = g_supplicant_network_get_wps(supplicant_network);
2838 wps_pbc = g_supplicant_network_is_wps_pbc(supplicant_network);
2839 wps_ready = g_supplicant_network_is_wps_active(supplicant_network);
2840 wps_advertizing = g_supplicant_network_is_wps_advertizing(
2841 supplicant_network);
2846 ssid = g_supplicant_network_get_ssid(supplicant_network, &ssid_len);
2848 network = connman_device_get_network(wifi->device, identifier);
2851 network = connman_network_create(identifier,
2852 CONNMAN_NETWORK_TYPE_WIFI);
2856 connman_network_set_index(network, wifi->index);
2858 if (connman_device_add_network(wifi->device, network) < 0) {
2859 connman_network_unref(network);
2863 wifi->networks = g_slist_prepend(wifi->networks, network);
2866 if (name && name[0] != '\0')
2867 connman_network_set_name(network, name);
2869 connman_network_set_blob(network, "WiFi.SSID",
2871 connman_network_set_string(network, "WiFi.Security", security);
2872 connman_network_set_strength(network,
2873 calculate_strength(supplicant_network));
2874 connman_network_set_bool(network, "WiFi.WPS", wps);
2875 connman_network_set_bool(network, "WiFi.WPSAdvertising",
2879 /* Is AP advertizing for WPS association?
2880 * If so, we decide to use WPS by default */
2881 if (wps_ready && wps_pbc &&
2883 connman_network_set_bool(network, "WiFi.UseWPS", true);
2886 connman_network_set_frequency(network,
2887 g_supplicant_network_get_frequency(supplicant_network));
2889 connman_network_set_available(network, true);
2890 connman_network_set_string(network, "WiFi.Mode", mode);
2893 connman_network_set_group(network, group);
2895 if (wifi->hidden && ssid) {
2896 if (!g_strcmp0(wifi->hidden->security, security) &&
2897 wifi->hidden->ssid_len == ssid_len &&
2898 !memcmp(wifi->hidden->ssid, ssid, ssid_len)) {
2899 connman_network_connect_hidden(network,
2900 wifi->hidden->identity,
2901 wifi->hidden->passphrase,
2902 wifi->hidden->user_data);
2903 wifi->hidden->user_data = NULL;
2904 hidden_free(wifi->hidden);
2905 wifi->hidden = NULL;
2910 static void network_removed(GSupplicantNetwork *network)
2912 GSupplicantInterface *interface;
2913 struct wifi_data *wifi;
2914 const char *name, *identifier;
2915 struct connman_network *connman_network;
2917 interface = g_supplicant_network_get_interface(network);
2918 wifi = g_supplicant_interface_get_data(interface);
2919 identifier = g_supplicant_network_get_identifier(network);
2920 name = g_supplicant_network_get_name(network);
2922 DBG("name %s", name);
2927 connman_network = connman_device_get_network(wifi->device, identifier);
2928 if (!connman_network)
2931 wifi->networks = g_slist_remove(wifi->networks, connman_network);
2933 connman_device_remove_network(wifi->device, connman_network);
2934 connman_network_unref(connman_network);
2937 static void network_changed(GSupplicantNetwork *network, const char *property)
2939 GSupplicantInterface *interface;
2940 struct wifi_data *wifi;
2941 const char *name, *identifier;
2942 struct connman_network *connman_network;
2945 interface = g_supplicant_network_get_interface(network);
2946 wifi = g_supplicant_interface_get_data(interface);
2947 identifier = g_supplicant_network_get_identifier(network);
2948 name = g_supplicant_network_get_name(network);
2950 DBG("name %s", name);
2955 connman_network = connman_device_get_network(wifi->device, identifier);
2956 if (!connman_network)
2959 if (g_str_equal(property, "WPSCapabilities")) {
2963 bool wps_advertizing;
2965 wps = g_supplicant_network_get_wps(network);
2966 wps_pbc = g_supplicant_network_is_wps_pbc(network);
2967 wps_ready = g_supplicant_network_is_wps_active(network);
2969 g_supplicant_network_is_wps_advertizing(network);
2971 connman_network_set_bool(connman_network, "WiFi.WPS", wps);
2972 connman_network_set_bool(connman_network,
2973 "WiFi.WPSAdvertising", wps_advertizing);
2977 * Is AP advertizing for WPS association?
2978 * If so, we decide to use WPS by default
2980 if (wps_ready && wps_pbc && wps_advertizing)
2981 connman_network_set_bool(connman_network,
2982 "WiFi.UseWPS", true);
2985 update_needed = true;
2986 } else if (g_str_equal(property, "Signal")) {
2987 connman_network_set_strength(connman_network,
2988 calculate_strength(network));
2989 update_needed = true;
2991 update_needed = false;
2994 connman_network_update(connman_network);
2997 static void network_associated(GSupplicantNetwork *network)
2999 GSupplicantInterface *interface;
3000 struct wifi_data *wifi;
3001 struct connman_network *connman_network;
3002 const char *identifier;
3006 interface = g_supplicant_network_get_interface(network);
3010 wifi = g_supplicant_interface_get_data(interface);
3014 /* P2P networks must not be treated as WiFi networks */
3015 if (wifi->p2p_connecting || wifi->p2p_device)
3018 identifier = g_supplicant_network_get_identifier(network);
3020 connman_network = connman_device_get_network(wifi->device, identifier);
3021 if (!connman_network)
3024 if (wifi->network) {
3025 if (wifi->network == connman_network)
3029 * This should never happen, we got associated with
3030 * a network different than the one we were expecting.
3032 DBG("Associated to %p while expecting %p",
3033 connman_network, wifi->network);
3035 connman_network_set_associating(wifi->network, false);
3038 DBG("Reconnecting to previous network %p from wpa_s", connman_network);
3040 wifi->network = connman_network_ref(connman_network);
3044 * Interface state changes callback (interface_state) is always
3045 * called before network_associated callback thus we need to call
3046 * interface_state again in order to process the new state now that
3047 * we have the network properly set.
3049 interface_state(interface);
3052 static void sta_authorized(GSupplicantInterface *interface,
3055 struct wifi_data *wifi = g_supplicant_interface_get_data(interface);
3057 DBG("wifi %p station %s authorized", wifi, addr);
3059 if (!wifi || !wifi->tethering)
3062 __connman_tethering_client_register(addr);
3065 static void sta_deauthorized(GSupplicantInterface *interface,
3068 struct wifi_data *wifi = g_supplicant_interface_get_data(interface);
3070 DBG("wifi %p station %s deauthorized", wifi, addr);
3072 if (!wifi || !wifi->tethering)
3075 __connman_tethering_client_unregister(addr);
3078 static void apply_peer_services(GSupplicantPeer *peer,
3079 struct connman_peer *connman_peer)
3081 const unsigned char *data;
3086 connman_peer_reset_services(connman_peer);
3088 data = g_supplicant_peer_get_widi_ies(peer, &length);
3090 connman_peer_add_service(connman_peer,
3091 CONNMAN_PEER_SERVICE_WIFI_DISPLAY, data, length);
3095 static void peer_found(GSupplicantPeer *peer)
3097 GSupplicantInterface *iface = g_supplicant_peer_get_interface(peer);
3098 struct wifi_data *wifi = g_supplicant_interface_get_data(iface);
3099 struct connman_peer *connman_peer;
3100 const char *identifier, *name;
3103 identifier = g_supplicant_peer_get_identifier(peer);
3104 name = g_supplicant_peer_get_name(peer);
3106 DBG("ident: %s", identifier);
3108 connman_peer = connman_peer_get(wifi->device, identifier);
3112 connman_peer = connman_peer_create(identifier);
3113 connman_peer_set_name(connman_peer, name);
3114 connman_peer_set_device(connman_peer, wifi->device);
3115 apply_peer_services(peer, connman_peer);
3117 ret = connman_peer_register(connman_peer);
3118 if (ret < 0 && ret != -EALREADY)
3119 connman_peer_unref(connman_peer);
3121 wifi->peers = g_slist_prepend(wifi->peers, connman_peer);
3124 static void peer_lost(GSupplicantPeer *peer)
3126 GSupplicantInterface *iface = g_supplicant_peer_get_interface(peer);
3127 struct wifi_data *wifi = g_supplicant_interface_get_data(iface);
3128 struct connman_peer *connman_peer;
3129 const char *identifier;
3134 identifier = g_supplicant_peer_get_identifier(peer);
3136 DBG("ident: %s", identifier);
3138 connman_peer = connman_peer_get(wifi->device, identifier);
3140 if (wifi->p2p_connecting &&
3141 wifi->pending_peer == connman_peer) {
3142 peer_connect_timeout(wifi);
3144 connman_peer_unregister(connman_peer);
3145 connman_peer_unref(connman_peer);
3148 wifi->peers = g_slist_remove(wifi->peers, connman_peer);
3151 static void peer_changed(GSupplicantPeer *peer, GSupplicantPeerState state)
3153 GSupplicantInterface *iface = g_supplicant_peer_get_interface(peer);
3154 struct wifi_data *wifi = g_supplicant_interface_get_data(iface);
3155 enum connman_peer_state p_state = CONNMAN_PEER_STATE_UNKNOWN;
3156 struct connman_peer *connman_peer;
3157 const char *identifier;
3159 identifier = g_supplicant_peer_get_identifier(peer);
3161 DBG("ident: %s", identifier);
3166 connman_peer = connman_peer_get(wifi->device, identifier);
3171 case G_SUPPLICANT_PEER_SERVICES_CHANGED:
3172 apply_peer_services(peer, connman_peer);
3173 connman_peer_services_changed(connman_peer);
3175 case G_SUPPLICANT_PEER_GROUP_CHANGED:
3176 if (!g_supplicant_peer_is_in_a_group(peer))
3177 p_state = CONNMAN_PEER_STATE_IDLE;
3179 p_state = CONNMAN_PEER_STATE_CONFIGURATION;
3181 case G_SUPPLICANT_PEER_GROUP_STARTED:
3183 case G_SUPPLICANT_PEER_GROUP_FINISHED:
3184 p_state = CONNMAN_PEER_STATE_IDLE;
3186 case G_SUPPLICANT_PEER_GROUP_JOINED:
3187 connman_peer_set_iface_address(connman_peer,
3188 g_supplicant_peer_get_iface_address(peer));
3190 case G_SUPPLICANT_PEER_GROUP_DISCONNECTED:
3191 p_state = CONNMAN_PEER_STATE_IDLE;
3193 case G_SUPPLICANT_PEER_GROUP_FAILED:
3194 if (g_supplicant_peer_has_requested_connection(peer))
3195 p_state = CONNMAN_PEER_STATE_IDLE;
3197 p_state = CONNMAN_PEER_STATE_FAILURE;
3201 if (p_state == CONNMAN_PEER_STATE_CONFIGURATION ||
3202 p_state == CONNMAN_PEER_STATE_FAILURE) {
3203 if (wifi->p2p_connecting
3204 && connman_peer == wifi->pending_peer)
3205 peer_cancel_timeout(wifi);
3207 p_state = CONNMAN_PEER_STATE_UNKNOWN;
3210 if (p_state == CONNMAN_PEER_STATE_UNKNOWN)
3213 if (p_state == CONNMAN_PEER_STATE_CONFIGURATION) {
3214 GSupplicantInterface *g_iface;
3215 struct wifi_data *g_wifi;
3217 g_iface = g_supplicant_peer_get_group_interface(peer);
3221 g_wifi = g_supplicant_interface_get_data(g_iface);
3225 connman_peer_set_as_master(connman_peer,
3226 !g_supplicant_peer_is_client(peer));
3227 connman_peer_set_sub_device(connman_peer, g_wifi->device);
3230 * If wpa_supplicant didn't create a dedicated p2p-group
3231 * interface then mark this interface as p2p_device to avoid
3232 * scan and auto-scan are launched on it while P2P is connected.
3234 if (!g_list_find(p2p_iface_list, g_wifi))
3235 wifi->p2p_device = true;
3238 connman_peer_set_state(connman_peer, p_state);
3241 static void peer_request(GSupplicantPeer *peer)
3243 GSupplicantInterface *iface = g_supplicant_peer_get_interface(peer);
3244 struct wifi_data *wifi = g_supplicant_interface_get_data(iface);
3245 struct connman_peer *connman_peer;
3246 const char *identifier;
3248 identifier = g_supplicant_peer_get_identifier(peer);
3250 DBG("ident: %s", identifier);
3252 connman_peer = connman_peer_get(wifi->device, identifier);
3256 connman_peer_request_connection(connman_peer);
3259 static void debug(const char *str)
3261 if (getenv("CONNMAN_SUPPLICANT_DEBUG"))
3262 connman_debug("%s", str);
3265 static void disconnect_reasoncode(GSupplicantInterface *interface,
3268 struct wifi_data *wifi = g_supplicant_interface_get_data(interface);
3271 wifi->disconnect_code = reasoncode;
3275 static void assoc_status_code(GSupplicantInterface *interface, int status_code)
3277 struct wifi_data *wifi = g_supplicant_interface_get_data(interface);
3280 wifi->assoc_code = status_code;
3284 static const GSupplicantCallbacks callbacks = {
3285 .system_ready = system_ready,
3286 .system_killed = system_killed,
3287 .interface_added = interface_added,
3288 .interface_state = interface_state,
3289 .interface_removed = interface_removed,
3290 .p2p_support = p2p_support,
3291 .scan_started = scan_started,
3292 .scan_finished = scan_finished,
3293 .ap_create_fail = ap_create_fail,
3294 .network_added = network_added,
3295 .network_removed = network_removed,
3296 .network_changed = network_changed,
3297 .network_associated = network_associated,
3298 .sta_authorized = sta_authorized,
3299 .sta_deauthorized = sta_deauthorized,
3300 .peer_found = peer_found,
3301 .peer_lost = peer_lost,
3302 .peer_changed = peer_changed,
3303 .peer_request = peer_request,
3305 .disconnect_reasoncode = disconnect_reasoncode,
3306 .assoc_status_code = assoc_status_code,
3310 static int tech_probe(struct connman_technology *technology)
3312 wifi_technology = technology;
3317 static void tech_remove(struct connman_technology *technology)
3319 wifi_technology = NULL;
3322 static GSupplicantSSID *ssid_ap_init(const char *ssid, const char *passphrase)
3324 GSupplicantSSID *ap;
3326 ap = g_try_malloc0(sizeof(GSupplicantSSID));
3330 ap->mode = G_SUPPLICANT_MODE_MASTER;
3332 ap->ssid_len = strlen(ssid);
3336 if (!passphrase || strlen(passphrase) == 0) {
3337 ap->security = G_SUPPLICANT_SECURITY_NONE;
3338 ap->passphrase = NULL;
3340 ap->security = G_SUPPLICANT_SECURITY_PSK;
3341 ap->protocol = G_SUPPLICANT_PROTO_RSN;
3342 ap->pairwise_cipher = G_SUPPLICANT_PAIRWISE_CCMP;
3343 ap->group_cipher = G_SUPPLICANT_GROUP_CCMP;
3344 ap->passphrase = passphrase;
3350 static void ap_start_callback(int result, GSupplicantInterface *interface,
3353 struct wifi_tethering_info *info = user_data;
3355 DBG("result %d index %d bridge %s",
3356 result, info->wifi->index, info->wifi->bridge);
3358 if ((result < 0) || (info->wifi->ap_supported != WIFI_AP_SUPPORTED)) {
3359 connman_inet_remove_from_bridge(info->wifi->index,
3360 info->wifi->bridge);
3362 if (info->wifi->ap_supported == WIFI_AP_SUPPORTED) {
3363 connman_technology_tethering_notify(info->technology, false);
3364 g_free(info->wifi->tethering_param->ssid);
3365 g_free(info->wifi->tethering_param);
3366 info->wifi->tethering_param = NULL;
3370 g_free(info->ifname);
3374 static void ap_create_callback(int result,
3375 GSupplicantInterface *interface,
3378 struct wifi_tethering_info *info = user_data;
3380 DBG("result %d ifname %s", result,
3381 g_supplicant_interface_get_ifname(interface));
3383 if ((result < 0) || (info->wifi->ap_supported != WIFI_AP_SUPPORTED)) {
3384 connman_inet_remove_from_bridge(info->wifi->index,
3385 info->wifi->bridge);
3387 if (info->wifi->ap_supported == WIFI_AP_SUPPORTED) {
3388 connman_technology_tethering_notify(info->technology, false);
3389 g_free(info->wifi->tethering_param->ssid);
3390 g_free(info->wifi->tethering_param);
3391 info->wifi->tethering_param = NULL;
3395 g_free(info->ifname);
3401 info->wifi->interface = interface;
3402 g_supplicant_interface_set_data(interface, info->wifi);
3404 if (g_supplicant_interface_set_apscan(interface, 2) < 0)
3405 connman_error("Failed to set interface ap_scan property");
3407 g_supplicant_interface_connect(interface, info->ssid,
3408 ap_start_callback, info);
3411 static void sta_remove_callback(int result,
3412 GSupplicantInterface *interface,
3415 struct wifi_tethering_info *info = user_data;
3416 const char *driver = connman_setting_get_string("wifi");
3418 DBG("ifname %s result %d ", info->ifname, result);
3420 if ((result < 0) || (info->wifi->ap_supported != WIFI_AP_SUPPORTED)) {
3421 info->wifi->tethering = false;
3422 connman_technology_tethering_notify(info->technology, false);
3424 if (info->wifi->ap_supported == WIFI_AP_SUPPORTED) {
3425 g_free(info->wifi->tethering_param->ssid);
3426 g_free(info->wifi->tethering_param);
3427 info->wifi->tethering_param = NULL;
3430 g_free(info->ifname);
3436 info->wifi->interface = NULL;
3438 g_supplicant_interface_create(info->ifname, driver, info->wifi->bridge,
3443 static int enable_wifi_tethering(struct connman_technology *technology,
3444 const char *bridge, const char *identifier,
3445 const char *passphrase, bool available)
3448 GSupplicantInterface *interface;
3449 struct wifi_data *wifi;
3450 struct wifi_tethering_info *info;
3455 for (list = iface_list; list; list = list->next) {
3458 DBG("wifi %p network %p pending_network %p", wifi,
3459 wifi->network, wifi->pending_network);
3461 interface = wifi->interface;
3466 ifname = g_supplicant_interface_get_ifname(wifi->interface);
3470 if (wifi->ap_supported == WIFI_AP_NOT_SUPPORTED) {
3471 DBG("%s does not support AP mode (detected)", ifname);
3475 mode = g_supplicant_interface_get_mode(interface);
3476 if ((mode & G_SUPPLICANT_CAPABILITY_MODE_AP) == 0) {
3477 wifi->ap_supported = WIFI_AP_NOT_SUPPORTED;
3478 DBG("%s does not support AP mode (capability)", ifname);
3482 if (wifi->network && available)
3485 info = g_try_malloc0(sizeof(struct wifi_tethering_info));
3489 wifi->tethering_param = g_try_malloc0(sizeof(struct wifi_tethering_info));
3490 if (!wifi->tethering_param) {
3496 info->technology = technology;
3497 info->wifi->bridge = bridge;
3498 info->ssid = ssid_ap_init(identifier, passphrase);
3502 info->ifname = g_strdup(ifname);
3504 wifi->tethering_param->technology = technology;
3505 wifi->tethering_param->ssid = ssid_ap_init(identifier, passphrase);
3506 if (!wifi->tethering_param->ssid)
3509 info->wifi->tethering = true;
3510 info->wifi->ap_supported = WIFI_AP_SUPPORTED;
3512 berr = connman_technology_tethering_notify(technology, true);
3516 err = g_supplicant_interface_remove(interface,
3517 sta_remove_callback,
3520 DBG("tethering wifi %p ifname %s", wifi, ifname);
3525 g_free(info->ifname);
3528 g_free(wifi->tethering_param);
3529 wifi->tethering_param = NULL;
3532 * Remove bridge if it was correctly created but remove
3533 * operation failed. Instead, if bridge creation failed then
3534 * break out and do not try again on another interface,
3535 * bridge set-up does not depend on it.
3538 connman_technology_tethering_notify(technology, false);
3546 static int tech_set_tethering(struct connman_technology *technology,
3547 const char *identifier, const char *passphrase,
3548 const char *bridge, bool enabled)
3551 struct wifi_data *wifi;
3557 for (list = iface_list; list; list = list->next) {
3560 if (wifi->tethering) {
3561 wifi->tethering = false;
3563 connman_inet_remove_from_bridge(wifi->index,
3565 wifi->bridged = false;
3569 connman_technology_tethering_notify(technology, false);
3574 DBG("trying tethering for available devices");
3575 err = enable_wifi_tethering(technology, bridge, identifier, passphrase,
3579 DBG("trying tethering for any device");
3580 err = enable_wifi_tethering(technology, bridge, identifier,
3587 static void regdom_callback(int result, const char *alpha2, void *user_data)
3591 if (!wifi_technology)
3597 connman_technology_regdom_notify(wifi_technology, alpha2);
3600 static int tech_set_regdom(struct connman_technology *technology, const char *alpha2)
3602 return g_supplicant_set_country(alpha2, regdom_callback, NULL);
3605 static struct connman_technology_driver tech_driver = {
3607 .type = CONNMAN_SERVICE_TYPE_WIFI,
3608 .probe = tech_probe,
3609 .remove = tech_remove,
3610 .set_tethering = tech_set_tethering,
3611 .set_regdom = tech_set_regdom,
3614 static int wifi_init(void)
3618 err = connman_network_driver_register(&network_driver);
3622 err = g_supplicant_register(&callbacks);
3624 connman_network_driver_unregister(&network_driver);
3628 err = connman_technology_driver_register(&tech_driver);
3630 g_supplicant_unregister(&callbacks);
3631 connman_network_driver_unregister(&network_driver);
3638 static void wifi_exit(void)
3642 connman_technology_driver_unregister(&tech_driver);
3644 g_supplicant_unregister(&callbacks);
3646 connman_network_driver_unregister(&network_driver);
3649 CONNMAN_PLUGIN_DEFINE(wifi, "WiFi interface plugin", VERSION,
3650 CONNMAN_PLUGIN_PRIORITY_DEFAULT, wifi_init, wifi_exit)