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/option.h>
53 #include <connman/storage.h>
54 #include <include/setting.h>
55 #include <connman/provision.h>
56 #include <connman/utsname.h>
57 #include <connman/machine.h>
58 #include <connman/tethering.h>
60 #include <gsupplicant/gsupplicant.h>
62 #define CLEANUP_TIMEOUT 8 /* in seconds */
63 #define INACTIVE_TIMEOUT 12 /* in seconds */
64 #define FAVORITE_MAXIMUM_RETRIES 2
66 #define BGSCAN_DEFAULT "simple:30:-45:300"
67 #define AUTOSCAN_EXPONENTIAL "exponential:3:300"
68 #define AUTOSCAN_SINGLE "single:3"
70 #define P2P_FIND_TIMEOUT 30
71 #define P2P_CONNECTION_TIMEOUT 100
72 #define P2P_LISTEN_PERIOD 500
73 #define P2P_LISTEN_INTERVAL 2000
75 #define ASSOC_STATUS_NO_CLIENT 17
76 #define LOAD_SHAPING_MAX_RETRIES 3
78 static struct connman_technology *wifi_technology = NULL;
79 static struct connman_technology *p2p_technology = NULL;
81 enum wifi_ap_capability{
83 WIFI_AP_SUPPORTED = 1,
84 WIFI_AP_NOT_SUPPORTED = 2,
87 enum wifi_scanning_type {
88 WIFI_SCANNING_UNKNOWN = 0,
89 WIFI_SCANNING_PASSIVE = 1,
90 WIFI_SCANNING_ACTIVE = 2,
93 struct hidden_params {
95 unsigned int ssid_len;
97 char *anonymous_identity;
99 char *altsubject_match;
100 char *domain_suffix_match;
104 GSupplicantScanParams *scan_params;
109 * Used for autoscan "emulation".
110 * Should be removed when wpa_s autoscan support will be by default.
112 struct autoscan_params {
116 unsigned int timeout;
119 struct wifi_tethering_info {
120 struct wifi_data *wifi;
121 struct connman_technology *technology;
123 GSupplicantSSID *ssid;
128 struct connman_device *device;
129 struct connman_network *network;
130 struct connman_network *pending_network;
132 GSupplicantInterface *interface;
133 GSupplicantState state;
137 enum wifi_ap_capability ap_supported;
139 bool interface_ready;
145 int load_shaping_retries;
146 struct hidden_params *hidden;
147 bool postpone_hidden;
148 struct wifi_tethering_info *tethering_param;
150 * autoscan "emulation".
152 struct autoscan_params *autoscan;
153 enum wifi_scanning_type scanning_type;
154 GSupplicantScanParams *scan_params;
155 unsigned int p2p_find_timeout;
156 unsigned int p2p_connection_timeout;
157 struct connman_peer *pending_peer;
166 static GList *iface_list = NULL;
168 static GList *pending_wifi_device = NULL;
169 static GList *p2p_iface_list = NULL;
170 static bool wfd_service_registered = false;
172 static void start_autoscan(struct connman_device *device);
173 static int tech_set_tethering(struct connman_technology *technology,
174 const char *identifier, const char *passphrase,
175 const char *bridge, bool enabled);
177 static int p2p_tech_probe(struct connman_technology *technology)
179 p2p_technology = technology;
184 static void p2p_tech_remove(struct connman_technology *technology)
186 p2p_technology = NULL;
189 static struct connman_technology_driver p2p_tech_driver = {
191 .type = CONNMAN_SERVICE_TYPE_P2P,
192 .probe = p2p_tech_probe,
193 .remove = p2p_tech_remove,
196 static bool is_p2p_connecting(void)
200 for (list = iface_list; list; list = list->next) {
201 struct wifi_data *wifi = list->data;
203 if (wifi->p2p_connecting)
210 static void add_pending_wifi_device(struct wifi_data *wifi)
212 if (g_list_find(pending_wifi_device, wifi))
215 pending_wifi_device = g_list_append(pending_wifi_device, wifi);
218 static struct wifi_data *get_pending_wifi_data(const char *ifname)
222 for (list = pending_wifi_device; list; list = list->next) {
223 struct wifi_data *wifi;
224 const char *dev_name;
227 if (!wifi || !wifi->device)
230 dev_name = connman_device_get_string(wifi->device, "Interface");
231 if (!g_strcmp0(ifname, dev_name)) {
232 pending_wifi_device = g_list_delete_link(
233 pending_wifi_device, list);
241 static void remove_pending_wifi_device(struct wifi_data *wifi)
245 link = g_list_find(pending_wifi_device, wifi);
250 pending_wifi_device = g_list_delete_link(pending_wifi_device, link);
253 static void peer_cancel_timeout(struct wifi_data *wifi)
255 if (wifi->p2p_connection_timeout > 0)
256 g_source_remove(wifi->p2p_connection_timeout);
258 wifi->p2p_connection_timeout = 0;
259 wifi->p2p_connecting = false;
261 if (wifi->pending_peer) {
262 connman_peer_unref(wifi->pending_peer);
263 wifi->pending_peer = NULL;
267 static gboolean peer_connect_timeout(gpointer data)
269 struct wifi_data *wifi = data;
273 if (wifi->p2p_connecting) {
274 enum connman_peer_state state = CONNMAN_PEER_STATE_FAILURE;
275 GSupplicantPeer *gs_peer =
276 g_supplicant_interface_peer_lookup(wifi->interface,
277 connman_peer_get_identifier(wifi->pending_peer));
279 if (g_supplicant_peer_has_requested_connection(gs_peer))
280 state = CONNMAN_PEER_STATE_IDLE;
282 connman_peer_set_state(wifi->pending_peer, state);
285 peer_cancel_timeout(wifi);
290 static void peer_connect_callback(int result, GSupplicantInterface *interface,
293 struct wifi_data *wifi = user_data;
294 struct connman_peer *peer = wifi->pending_peer;
296 DBG("peer %p - %d", peer, result);
302 peer_connect_timeout(wifi);
306 connman_peer_set_state(peer, CONNMAN_PEER_STATE_ASSOCIATION);
308 wifi->p2p_connection_timeout = g_timeout_add_seconds(
309 P2P_CONNECTION_TIMEOUT,
310 peer_connect_timeout, wifi);
313 static int peer_connect(struct connman_peer *peer,
314 enum connman_peer_wps_method wps_method,
317 struct connman_device *device = connman_peer_get_device(peer);
318 GSupplicantPeerParams *peer_params;
319 GSupplicantPeer *gs_peer;
320 struct wifi_data *wifi;
324 DBG("peer %p", peer);
329 wifi = connman_device_get_data(device);
330 if (!wifi || !wifi->interface)
333 if (wifi->p2p_connecting)
336 gs_peer = g_supplicant_interface_peer_lookup(wifi->interface,
337 connman_peer_get_identifier(peer));
341 pbc = g_supplicant_peer_is_wps_pbc(gs_peer);
342 pin = g_supplicant_peer_is_wps_pin(gs_peer);
344 switch (wps_method) {
345 case CONNMAN_PEER_WPS_UNKNOWN:
346 if ((pbc && pin) || pin)
349 case CONNMAN_PEER_WPS_PBC:
354 case CONNMAN_PEER_WPS_PIN:
355 if (!pin || !wps_pin)
360 peer_params = g_try_malloc0(sizeof(GSupplicantPeerParams));
364 peer_params->path = g_strdup(g_supplicant_peer_get_path(gs_peer));
366 peer_params->wps_pin = g_strdup(wps_pin);
368 peer_params->master = connman_peer_service_is_master();
370 ret = g_supplicant_interface_p2p_connect(wifi->interface, peer_params,
371 peer_connect_callback, wifi);
372 if (ret == -EINPROGRESS) {
373 wifi->pending_peer = connman_peer_ref(peer);
374 wifi->p2p_connecting = true;
375 } else if (ret < 0) {
376 g_free(peer_params->path);
377 g_free(peer_params->wps_pin);
384 static int peer_disconnect(struct connman_peer *peer)
386 struct connman_device *device = connman_peer_get_device(peer);
387 GSupplicantPeerParams peer_params = {};
388 GSupplicantPeer *gs_peer;
389 struct wifi_data *wifi;
392 DBG("peer %p", peer);
397 wifi = connman_device_get_data(device);
401 gs_peer = g_supplicant_interface_peer_lookup(wifi->interface,
402 connman_peer_get_identifier(peer));
406 peer_params.path = g_strdup(g_supplicant_peer_get_path(gs_peer));
408 ret = g_supplicant_interface_p2p_disconnect(wifi->interface,
410 g_free(peer_params.path);
412 if (ret == -EINPROGRESS) {
413 peer_cancel_timeout(wifi);
414 wifi->p2p_device = false;
420 struct peer_service_registration {
421 peer_service_registration_cb_t callback;
425 static bool is_service_wfd(const unsigned char *specs, int length)
427 if (length < 9 || specs[0] != 0 || specs[1] != 0 || specs[2] != 6)
433 static void apply_p2p_listen_on_iface(gpointer data, gpointer user_data)
435 struct wifi_data *wifi = data;
437 if (!wifi->interface ||
438 !g_supplicant_interface_has_p2p(wifi->interface))
441 if (!wifi->servicing) {
442 g_supplicant_interface_p2p_listen(wifi->interface,
443 P2P_LISTEN_PERIOD, P2P_LISTEN_INTERVAL);
449 static void register_wfd_service_cb(int result,
450 GSupplicantInterface *iface, void *user_data)
452 struct peer_service_registration *reg_data = user_data;
457 g_list_foreach(iface_list, apply_p2p_listen_on_iface, NULL);
459 if (reg_data && reg_data->callback) {
460 reg_data->callback(result, reg_data->user_data);
465 static GSupplicantP2PServiceParams *fill_in_peer_service_params(
466 const unsigned char *spec,
467 int spec_length, const unsigned char *query,
468 int query_length, int version)
470 GSupplicantP2PServiceParams *params;
472 params = g_try_malloc0(sizeof(GSupplicantP2PServiceParams));
477 params->version = version;
478 params->service = g_memdup(spec, spec_length);
479 } else if (query_length > 0 && spec_length > 0) {
480 params->query = g_memdup(query, query_length);
481 params->query_length = query_length;
483 params->response = g_memdup(spec, spec_length);
484 params->response_length = spec_length;
486 params->wfd_ies = g_memdup(spec, spec_length);
487 params->wfd_ies_length = spec_length;
493 static void free_peer_service_params(GSupplicantP2PServiceParams *params)
498 g_free(params->service);
499 g_free(params->query);
500 g_free(params->response);
501 g_free(params->wfd_ies);
506 static int peer_register_wfd_service(const unsigned char *specification,
507 int specification_length,
508 peer_service_registration_cb_t callback,
511 struct peer_service_registration *reg_data = NULL;
512 static GSupplicantP2PServiceParams *params;
517 if (wfd_service_registered)
520 params = fill_in_peer_service_params(specification,
521 specification_length, NULL, 0, 0);
525 reg_data = g_try_malloc0(sizeof(*reg_data));
531 reg_data->callback = callback;
532 reg_data->user_data = user_data;
534 ret = g_supplicant_set_widi_ies(params,
535 register_wfd_service_cb, reg_data);
536 if (ret < 0 && ret != -EINPROGRESS)
539 wfd_service_registered = true;
543 free_peer_service_params(params);
549 static void register_peer_service_cb(int result,
550 GSupplicantInterface *iface, void *user_data)
552 struct wifi_data *wifi = g_supplicant_interface_get_data(iface);
553 struct peer_service_registration *reg_data = user_data;
558 apply_p2p_listen_on_iface(wifi, NULL);
560 if (reg_data->callback)
561 reg_data->callback(result, reg_data->user_data);
566 static int peer_register_service(const unsigned char *specification,
567 int specification_length,
568 const unsigned char *query,
569 int query_length, int version,
570 peer_service_registration_cb_t callback,
573 struct peer_service_registration *reg_data;
574 GSupplicantP2PServiceParams *params;
581 if (specification && !version && !query &&
582 is_service_wfd(specification, specification_length)) {
583 return peer_register_wfd_service(specification,
584 specification_length, callback, user_data);
587 reg_data = g_try_malloc0(sizeof(*reg_data));
591 reg_data->callback = callback;
592 reg_data->user_data = user_data;
596 for (list = iface_list; list; list = list->next) {
597 struct wifi_data *wifi = list->data;
598 GSupplicantInterface *iface = wifi->interface;
600 if (!g_supplicant_interface_has_p2p(iface))
603 params = fill_in_peer_service_params(specification,
604 specification_length, query,
605 query_length, version);
610 ret_f = g_supplicant_interface_p2p_add_service(iface,
611 register_peer_service_cb, params, reg_data);
612 if (ret_f == 0 || ret_f == -EINPROGRESS)
616 ret = g_supplicant_interface_p2p_add_service(iface,
617 register_peer_service_cb, params, NULL);
618 if (ret != 0 && ret != -EINPROGRESS)
619 free_peer_service_params(params);
622 if (ret_f != 0 && ret_f != -EINPROGRESS)
628 static int peer_unregister_wfd_service(void)
630 GSupplicantP2PServiceParams *params;
633 if (!wfd_service_registered)
636 params = fill_in_peer_service_params(NULL, 0, NULL, 0, 0);
640 wfd_service_registered = false;
642 g_supplicant_set_widi_ies(params, NULL, NULL);
644 for (list = iface_list; list; list = list->next) {
645 struct wifi_data *wifi = list->data;
647 if (!g_supplicant_interface_has_p2p(wifi->interface))
651 if (!wifi->servicing || wifi->servicing < 0) {
652 g_supplicant_interface_p2p_listen(wifi->interface,
661 static int peer_unregister_service(const unsigned char *specification,
662 int specification_length,
663 const unsigned char *query,
664 int query_length, int version)
666 GSupplicantP2PServiceParams *params;
671 if (specification && !version && !query &&
672 is_service_wfd(specification, specification_length)) {
673 ret = peer_unregister_wfd_service();
674 if (ret != 0 && ret != -EINPROGRESS)
679 for (list = iface_list; list; list = list->next) {
680 struct wifi_data *wifi = list->data;
681 GSupplicantInterface *iface = wifi->interface;
686 if (!g_supplicant_interface_has_p2p(iface))
689 params = fill_in_peer_service_params(specification,
690 specification_length, query,
691 query_length, version);
695 ret = g_supplicant_interface_p2p_del_service(iface, params);
696 if (ret != 0 && ret != -EINPROGRESS)
697 free_peer_service_params(params);
700 if (!wifi->servicing || wifi->servicing < 0) {
701 g_supplicant_interface_p2p_listen(iface, 0, 0);
709 static struct connman_peer_driver peer_driver = {
710 .connect = peer_connect,
711 .disconnect = peer_disconnect,
712 .register_service = peer_register_service,
713 .unregister_service = peer_unregister_service,
716 static void handle_tethering(struct wifi_data *wifi)
718 if (!wifi->tethering)
727 DBG("index %d bridge %s", wifi->index, wifi->bridge);
729 if (connman_inet_add_to_bridge(wifi->index, wifi->bridge) < 0)
732 wifi->bridged = true;
735 static void wifi_newlink(unsigned flags, unsigned change, void *user_data)
737 struct connman_device *device = user_data;
738 struct wifi_data *wifi = connman_device_get_data(device);
743 DBG("index %d flags %d change %d", wifi->index, flags, change);
745 if ((wifi->flags & IFF_UP) != (flags & IFF_UP)) {
749 DBG("interface down");
752 if ((wifi->flags & IFF_LOWER_UP) != (flags & IFF_LOWER_UP)) {
753 if (flags & IFF_LOWER_UP) {
756 handle_tethering(wifi);
764 static int wifi_probe(struct connman_device *device)
766 struct wifi_data *wifi;
768 DBG("device %p", device);
770 wifi = g_try_new0(struct wifi_data, 1);
774 wifi->state = G_SUPPLICANT_STATE_INACTIVE;
775 wifi->ap_supported = WIFI_AP_UNKNOWN;
776 wifi->tethering_param = NULL;
778 connman_device_set_data(device, wifi);
779 wifi->device = connman_device_ref(device);
781 wifi->index = connman_device_get_index(device);
784 wifi->watch = connman_rtnl_add_newlink_watch(wifi->index,
785 wifi_newlink, device);
786 if (is_p2p_connecting())
787 add_pending_wifi_device(wifi);
789 iface_list = g_list_append(iface_list, wifi);
794 static void remove_networks(struct connman_device *device,
795 struct wifi_data *wifi)
799 for (list = wifi->networks; list; list = list->next) {
800 struct connman_network *network = list->data;
802 connman_device_remove_network(device, network);
803 connman_network_unref(network);
806 g_slist_free(wifi->networks);
807 wifi->networks = NULL;
810 static void remove_peers(struct wifi_data *wifi)
814 for (list = wifi->peers; list; list = list->next) {
815 struct connman_peer *peer = list->data;
817 connman_peer_unregister(peer);
818 connman_peer_unref(peer);
821 g_slist_free(wifi->peers);
825 static void reset_autoscan(struct connman_device *device)
827 struct wifi_data *wifi = connman_device_get_data(device);
828 struct autoscan_params *autoscan;
832 if (!wifi || !wifi->autoscan)
835 autoscan = wifi->autoscan;
837 autoscan->interval = 0;
839 if (autoscan->timeout == 0)
842 g_source_remove(autoscan->timeout);
843 autoscan->timeout = 0;
845 connman_device_unref(device);
848 static void stop_autoscan(struct connman_device *device)
850 const struct wifi_data *wifi = connman_device_get_data(device);
852 if (!wifi || !wifi->autoscan)
855 reset_autoscan(device);
857 connman_device_set_scanning(device, CONNMAN_SERVICE_TYPE_WIFI, false);
860 static void check_p2p_technology(void)
862 bool p2p_exists = false;
865 for (list = iface_list; list; list = list->next) {
866 struct wifi_data *w = list->data;
869 g_supplicant_interface_has_p2p(w->interface))
874 connman_technology_driver_unregister(&p2p_tech_driver);
875 connman_peer_driver_unregister(&peer_driver);
879 static void wifi_remove(struct connman_device *device)
881 struct wifi_data *wifi = connman_device_get_data(device);
883 DBG("device %p wifi %p", device, wifi);
888 stop_autoscan(device);
890 if (wifi->p2p_device)
891 p2p_iface_list = g_list_remove(p2p_iface_list, wifi);
893 iface_list = g_list_remove(iface_list, wifi);
895 check_p2p_technology();
897 remove_pending_wifi_device(wifi);
899 if (connman_device_get_scanning(device, CONNMAN_SERVICE_TYPE_P2P)) {
900 g_source_remove(wifi->p2p_find_timeout);
901 connman_device_unref(wifi->device);
904 if (wifi->p2p_connection_timeout)
905 g_source_remove(wifi->p2p_connection_timeout);
907 remove_networks(device, wifi);
910 connman_device_set_powered(device, false);
911 connman_device_set_data(device, NULL);
912 connman_device_unref(wifi->device);
913 connman_rtnl_remove_watch(wifi->watch);
915 g_supplicant_interface_set_data(wifi->interface, NULL);
917 g_supplicant_interface_cancel(wifi->interface);
919 if (wifi->scan_params)
920 g_supplicant_free_scan_params(wifi->scan_params);
922 g_free(wifi->autoscan);
923 g_free(wifi->identifier);
927 static bool is_duplicate(GSList *list, gchar *ssid, int ssid_len)
931 for (iter = list; iter; iter = g_slist_next(iter)) {
932 struct scan_ssid *scan_ssid = iter->data;
934 if (ssid_len == scan_ssid->ssid_len &&
935 memcmp(ssid, scan_ssid->ssid, ssid_len) == 0)
942 static int add_scan_param(gchar *hex_ssid, char *raw_ssid, int ssid_len,
943 int freq, GSupplicantScanParams *scan_data,
944 int driver_max_scan_ssids, char *ssid_name)
947 struct scan_ssid *scan_ssid;
949 if ((driver_max_scan_ssids == 0 ||
950 driver_max_scan_ssids > scan_data->num_ssids) &&
951 (hex_ssid || raw_ssid)) {
953 unsigned int j = 0, hex;
956 size_t hex_ssid_len = strlen(hex_ssid);
958 ssid = g_try_malloc0(hex_ssid_len / 2);
962 for (i = 0; i < hex_ssid_len; i += 2) {
963 sscanf(hex_ssid + i, "%02x", &hex);
972 * If we have already added hidden AP to the list,
973 * then do not do it again. This might happen if you have
974 * used or are using multiple wifi cards, so in that case
975 * you might have multiple service files for same AP.
977 if (is_duplicate(scan_data->ssids, ssid, j)) {
983 scan_ssid = g_try_new(struct scan_ssid, 1);
990 memcpy(scan_ssid->ssid, ssid, j);
991 scan_ssid->ssid_len = j;
992 scan_data->ssids = g_slist_prepend(scan_data->ssids,
995 scan_data->num_ssids++;
997 DBG("SSID %s added to scanned list of %d entries", ssid_name,
998 scan_data->num_ssids);
1005 scan_data->ssids = g_slist_reverse(scan_data->ssids);
1007 if (!scan_data->freqs) {
1008 scan_data->freqs = g_try_malloc0(sizeof(uint16_t));
1009 if (!scan_data->freqs) {
1010 g_slist_free_full(scan_data->ssids, g_free);
1014 scan_data->num_freqs = 1;
1015 scan_data->freqs[0] = freq;
1017 bool duplicate = false;
1019 /* Don't add duplicate entries */
1020 for (i = 0; i < scan_data->num_freqs; i++) {
1021 if (scan_data->freqs[i] == freq) {
1028 scan_data->num_freqs++;
1029 scan_data->freqs = g_try_realloc(scan_data->freqs,
1030 sizeof(uint16_t) * scan_data->num_freqs);
1031 if (!scan_data->freqs) {
1032 g_slist_free_full(scan_data->ssids, g_free);
1035 scan_data->freqs[scan_data->num_freqs - 1] = freq;
1042 static int get_hidden_connections(GSupplicantScanParams *scan_data)
1044 struct connman_config_entry **entries;
1050 int num_ssids = 0, add_param_failed = 0;
1052 services = connman_storage_get_services();
1053 for (i = 0; services && services[i]; i++) {
1054 if (strncmp(services[i], "wifi_", 5) != 0)
1057 keyfile = connman_storage_load_service(services[i]);
1061 value = g_key_file_get_boolean(keyfile,
1062 services[i], "Hidden", NULL);
1064 g_key_file_free(keyfile);
1068 value = g_key_file_get_boolean(keyfile,
1069 services[i], "Favorite", NULL);
1071 g_key_file_free(keyfile);
1075 ssid = g_key_file_get_string(keyfile,
1076 services[i], "SSID", NULL);
1078 name = g_key_file_get_string(keyfile, services[i], "Name",
1081 ret = add_scan_param(ssid, NULL, 0, 0, scan_data, 0, name);
1089 g_key_file_free(keyfile);
1093 * Check if there are any hidden AP that needs to be provisioned.
1095 entries = connman_config_get_entries("wifi");
1096 for (i = 0; entries && entries[i]; i++) {
1099 if (!entries[i]->hidden)
1102 if (!entries[i]->ssid) {
1103 ssid = entries[i]->name;
1106 ssid = entries[i]->ssid;
1107 len = entries[i]->ssid_len;
1113 ret = add_scan_param(NULL, ssid, len, 0, scan_data, 0, ssid);
1120 connman_config_free_entries(entries);
1122 if (add_param_failed > 0)
1123 DBG("Unable to scan %d out of %d SSIDs",
1124 add_param_failed, num_ssids);
1126 g_strfreev(services);
1131 static int get_hidden_connections_params(struct wifi_data *wifi,
1132 GSupplicantScanParams *scan_params)
1134 int driver_max_ssids, i;
1135 GSupplicantScanParams *orig_params;
1138 * Scan hidden networks so that we can autoconnect to them.
1139 * We will assume 1 as a default number of ssid to scan.
1141 driver_max_ssids = g_supplicant_interface_get_max_scan_ssids(
1143 if (driver_max_ssids == 0)
1144 driver_max_ssids = 1;
1146 DBG("max ssids %d", driver_max_ssids);
1148 if (!wifi->scan_params) {
1149 wifi->scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
1150 if (!wifi->scan_params)
1153 if (get_hidden_connections(wifi->scan_params) == 0) {
1154 g_supplicant_free_scan_params(wifi->scan_params);
1155 wifi->scan_params = NULL;
1161 orig_params = wifi->scan_params;
1163 /* Let's transfer driver_max_ssids params */
1164 for (i = 0; i < driver_max_ssids; i++) {
1165 struct scan_ssid *ssid;
1167 if (!wifi->scan_params->ssids)
1170 ssid = orig_params->ssids->data;
1171 orig_params->ssids = g_slist_remove(orig_params->ssids, ssid);
1172 scan_params->ssids = g_slist_prepend(scan_params->ssids, ssid);
1176 scan_params->num_ssids = i;
1177 scan_params->ssids = g_slist_reverse(scan_params->ssids);
1179 scan_params->freqs = g_memdup(orig_params->freqs,
1180 sizeof(uint16_t) * orig_params->num_freqs);
1181 if (!scan_params->freqs)
1184 scan_params->num_freqs = orig_params->num_freqs;
1189 orig_params->num_ssids -= scan_params->num_ssids;
1191 return scan_params->num_ssids;
1194 g_slist_free_full(scan_params->ssids, g_free);
1195 g_supplicant_free_scan_params(wifi->scan_params);
1196 wifi->scan_params = NULL;
1201 static int throw_wifi_scan(struct connman_device *device,
1202 GSupplicantInterfaceCallback callback)
1204 struct wifi_data *wifi = connman_device_get_data(device);
1210 DBG("device %p %p", device, wifi->interface);
1212 if (wifi->tethering)
1215 if (connman_device_get_scanning(device, CONNMAN_SERVICE_TYPE_WIFI))
1218 connman_device_ref(device);
1220 ret = g_supplicant_interface_scan(wifi->interface, NULL,
1223 connman_device_set_scanning(device,
1224 CONNMAN_SERVICE_TYPE_WIFI, true);
1226 connman_device_unref(device);
1231 static void hidden_free(struct hidden_params *hidden)
1236 if (hidden->scan_params)
1237 g_supplicant_free_scan_params(hidden->scan_params);
1238 g_free(hidden->identity);
1239 g_free(hidden->passphrase);
1240 g_free(hidden->security);
1244 static void scan_callback(int result, GSupplicantInterface *interface,
1247 struct connman_device *device = user_data;
1248 struct wifi_data *wifi = connman_device_get_data(device);
1251 DBG("result %d wifi %p", result, wifi);
1254 if (wifi->hidden && !wifi->postpone_hidden) {
1255 connman_network_clear_hidden(wifi->hidden->user_data);
1256 hidden_free(wifi->hidden);
1257 wifi->hidden = NULL;
1260 if (wifi->scan_params) {
1261 g_supplicant_free_scan_params(wifi->scan_params);
1262 wifi->scan_params = NULL;
1267 connman_device_reset_scanning(device);
1269 /* User is connecting to a hidden AP, let's wait for finished event */
1270 if (wifi && wifi->hidden && wifi->postpone_hidden) {
1271 GSupplicantScanParams *scan_params;
1274 wifi->postpone_hidden = false;
1275 scan_params = wifi->hidden->scan_params;
1276 wifi->hidden->scan_params = NULL;
1278 reset_autoscan(device);
1280 ret = g_supplicant_interface_scan(wifi->interface, scan_params,
1281 scan_callback, device);
1285 /* On error, let's recall scan_callback, which will cleanup */
1286 return scan_callback(ret, interface, user_data);
1289 scanning = connman_device_get_scanning(device, CONNMAN_SERVICE_TYPE_WIFI);
1292 connman_device_set_scanning(device,
1293 CONNMAN_SERVICE_TYPE_WIFI, false);
1296 if (result != -ENOLINK)
1297 start_autoscan(device);
1300 * If we are here then we were scanning; however, if we are
1301 * also mid-flight disabling the interface, then wifi_disable
1302 * has already cleared the device scanning state and
1303 * unreferenced the device, obviating the need to do it here.
1307 connman_device_unref(device);
1310 static void scan_callback_hidden(int result,
1311 GSupplicantInterface *interface, void *user_data)
1313 struct connman_device *device = user_data;
1314 struct wifi_data *wifi = connman_device_get_data(device);
1315 GSupplicantScanParams *scan_params;
1318 DBG("result %d wifi %p", result, wifi);
1323 /* User is trying to connect to a hidden AP */
1324 if (wifi->hidden && wifi->postpone_hidden)
1327 scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
1331 if (get_hidden_connections_params(wifi, scan_params) > 0) {
1332 ret = g_supplicant_interface_scan(wifi->interface,
1334 scan_callback_hidden,
1340 g_supplicant_free_scan_params(scan_params);
1343 scan_callback(result, interface, user_data);
1346 static gboolean autoscan_timeout(gpointer data)
1348 struct connman_device *device = data;
1349 struct wifi_data *wifi = connman_device_get_data(device);
1350 struct autoscan_params *autoscan;
1356 autoscan = wifi->autoscan;
1358 if (autoscan->interval <= 0) {
1359 interval = autoscan->base;
1362 interval = autoscan->interval * autoscan->base;
1364 if (interval > autoscan->limit)
1365 interval = autoscan->limit;
1367 throw_wifi_scan(wifi->device, scan_callback_hidden);
1370 * In case BackgroundScanning is disabled, interval will reach the
1371 * limit exactly after the very first passive scanning. It allows
1372 * to ensure at most one passive scan is performed in such cases.
1374 if (!connman_setting_get_bool("BackgroundScanning") &&
1375 interval == autoscan->limit) {
1376 g_source_remove(autoscan->timeout);
1377 autoscan->timeout = 0;
1379 connman_device_unref(device);
1385 DBG("interval %d", interval);
1387 autoscan->interval = interval;
1389 autoscan->timeout = g_timeout_add_seconds(interval,
1390 autoscan_timeout, device);
1395 static void start_autoscan(struct connman_device *device)
1397 struct wifi_data *wifi = connman_device_get_data(device);
1398 struct autoscan_params *autoscan;
1405 if (wifi->p2p_device)
1408 if (wifi->connected)
1411 autoscan = wifi->autoscan;
1415 if (autoscan->timeout > 0 || autoscan->interval > 0)
1418 connman_device_ref(device);
1420 autoscan_timeout(device);
1423 static struct autoscan_params *parse_autoscan_params(const char *params)
1425 struct autoscan_params *autoscan;
1432 list_params = g_strsplit(params, ":", 0);
1433 if (list_params == 0)
1436 if (!g_strcmp0(list_params[0], "exponential") &&
1437 g_strv_length(list_params) == 3) {
1438 base = atoi(list_params[1]);
1439 limit = atoi(list_params[2]);
1440 } else if (!g_strcmp0(list_params[0], "single") &&
1441 g_strv_length(list_params) == 2)
1442 base = limit = atoi(list_params[1]);
1444 g_strfreev(list_params);
1448 DBG("Setup %s autoscanning", list_params[0]);
1450 g_strfreev(list_params);
1452 autoscan = g_try_malloc0(sizeof(struct autoscan_params));
1454 DBG("Could not allocate memory for autoscan");
1458 DBG("base %d - limit %d", base, limit);
1459 autoscan->base = base;
1460 autoscan->limit = limit;
1465 static void setup_autoscan(struct wifi_data *wifi)
1468 * If BackgroundScanning is enabled, setup exponential
1469 * autoscanning if it has not been previously done.
1471 if (connman_setting_get_bool("BackgroundScanning")) {
1472 wifi->autoscan = parse_autoscan_params(AUTOSCAN_EXPONENTIAL);
1477 * On the contrary, if BackgroundScanning is disabled, update autoscan
1478 * parameters based on the type of scanning that is being performed.
1480 if (wifi->autoscan) {
1481 g_free(wifi->autoscan);
1482 wifi->autoscan = NULL;
1485 switch (wifi->scanning_type) {
1486 case WIFI_SCANNING_PASSIVE:
1487 /* Do not setup autoscan. */
1489 case WIFI_SCANNING_ACTIVE:
1490 /* Setup one single passive scan after active. */
1491 wifi->autoscan = parse_autoscan_params(AUTOSCAN_SINGLE);
1493 case WIFI_SCANNING_UNKNOWN:
1494 /* Setup autoscan in this case but we should never fall here. */
1495 wifi->autoscan = parse_autoscan_params(AUTOSCAN_SINGLE);
1500 static void finalize_interface_creation(struct wifi_data *wifi)
1502 DBG("interface is ready wifi %p tethering %d", wifi, wifi->tethering);
1504 if (!wifi->device) {
1505 connman_error("WiFi device not set");
1509 connman_device_set_powered(wifi->device, true);
1511 if (wifi->p2p_device)
1514 if (!wifi->autoscan)
1515 setup_autoscan(wifi);
1517 start_autoscan(wifi->device);
1520 static void interface_create_callback(int result,
1521 GSupplicantInterface *interface,
1524 struct wifi_data *wifi = user_data;
1526 DBG("result %d ifname %s, wifi %p", result,
1527 g_supplicant_interface_get_ifname(interface),
1530 if (result < 0 || !wifi)
1533 wifi->interface = interface;
1534 g_supplicant_interface_set_data(interface, wifi);
1536 if (g_supplicant_interface_get_ready(interface)) {
1537 wifi->interface_ready = true;
1538 finalize_interface_creation(wifi);
1542 static int wifi_enable(struct connman_device *device)
1544 struct wifi_data *wifi = connman_device_get_data(device);
1547 const char *driver = connman_option_get_string("wifi");
1550 DBG("device %p %p", device, wifi);
1552 index = connman_device_get_index(device);
1553 if (!wifi || index < 0)
1556 if (is_p2p_connecting())
1557 return -EINPROGRESS;
1559 interface = connman_inet_ifname(index);
1560 ret = g_supplicant_interface_create(interface, driver, NULL,
1561 interface_create_callback,
1568 return -EINPROGRESS;
1571 static int wifi_disable(struct connman_device *device)
1573 struct wifi_data *wifi = connman_device_get_data(device);
1576 DBG("device %p wifi %p", device, wifi);
1581 wifi->connected = false;
1582 wifi->disconnecting = false;
1584 if (wifi->pending_network)
1585 wifi->pending_network = NULL;
1587 stop_autoscan(device);
1589 if (connman_device_get_scanning(device, CONNMAN_SERVICE_TYPE_P2P)) {
1590 g_source_remove(wifi->p2p_find_timeout);
1591 wifi->p2p_find_timeout = 0;
1592 connman_device_set_scanning(device, CONNMAN_SERVICE_TYPE_P2P, false);
1593 connman_device_unref(wifi->device);
1596 /* In case of a user scan, device is still referenced */
1597 if (connman_device_get_scanning(device, CONNMAN_SERVICE_TYPE_WIFI)) {
1598 connman_device_set_scanning(device,
1599 CONNMAN_SERVICE_TYPE_WIFI, false);
1600 connman_device_unref(wifi->device);
1603 remove_networks(device, wifi);
1606 ret = g_supplicant_interface_remove(wifi->interface, NULL, NULL);
1610 return -EINPROGRESS;
1613 struct last_connected {
1619 static gint sort_entry(gconstpointer a, gconstpointer b, gpointer user_data)
1621 GTimeVal *aval = (GTimeVal *)a;
1622 GTimeVal *bval = (GTimeVal *)b;
1624 /* Note that the sort order is descending */
1625 if (aval->tv_sec < bval->tv_sec)
1628 if (aval->tv_sec > bval->tv_sec)
1634 static void free_entry(gpointer data)
1636 struct last_connected *entry = data;
1638 g_free(entry->ssid);
1642 static int get_latest_connections(int max_ssids,
1643 GSupplicantScanParams *scan_data)
1645 GSequenceIter *iter;
1646 GSequence *latest_list;
1647 struct last_connected *entry;
1656 latest_list = g_sequence_new(free_entry);
1660 services = connman_storage_get_services();
1661 for (i = 0; services && services[i]; i++) {
1662 if (strncmp(services[i], "wifi_", 5) != 0)
1665 keyfile = connman_storage_load_service(services[i]);
1669 str = g_key_file_get_string(keyfile,
1670 services[i], "Favorite", NULL);
1671 if (!str || g_strcmp0(str, "true")) {
1673 g_key_file_free(keyfile);
1678 str = g_key_file_get_string(keyfile,
1679 services[i], "AutoConnect", NULL);
1680 if (!str || g_strcmp0(str, "true")) {
1682 g_key_file_free(keyfile);
1687 str = g_key_file_get_string(keyfile,
1688 services[i], "Modified", NULL);
1690 g_key_file_free(keyfile);
1693 g_time_val_from_iso8601(str, &modified);
1696 ssid = g_key_file_get_string(keyfile,
1697 services[i], "SSID", NULL);
1699 freq = g_key_file_get_integer(keyfile, services[i],
1702 entry = g_try_new(struct last_connected, 1);
1704 g_sequence_free(latest_list);
1705 g_key_file_free(keyfile);
1711 entry->modified = modified;
1714 g_sequence_insert_sorted(latest_list, entry,
1720 g_key_file_free(keyfile);
1723 g_strfreev(services);
1725 num_ssids = num_ssids > max_ssids ? max_ssids : num_ssids;
1727 iter = g_sequence_get_begin_iter(latest_list);
1729 for (i = 0; i < num_ssids; i++) {
1730 entry = g_sequence_get(iter);
1732 DBG("ssid %s freq %d modified %lu", entry->ssid, entry->freq,
1733 entry->modified.tv_sec);
1735 add_scan_param(entry->ssid, NULL, 0, entry->freq, scan_data,
1736 max_ssids, entry->ssid);
1738 iter = g_sequence_iter_next(iter);
1741 g_sequence_free(latest_list);
1745 static void wifi_update_scanner_type(struct wifi_data *wifi,
1746 enum wifi_scanning_type new_type)
1750 if (!wifi || wifi->scanning_type == new_type)
1753 wifi->scanning_type = new_type;
1755 setup_autoscan(wifi);
1758 static int wifi_scan_simple(struct connman_device *device)
1760 struct wifi_data *wifi = connman_device_get_data(device);
1762 reset_autoscan(device);
1764 /* Distinguish between devices performing passive and active scanning */
1766 wifi_update_scanner_type(wifi, WIFI_SCANNING_PASSIVE);
1768 return throw_wifi_scan(device, scan_callback_hidden);
1771 static gboolean p2p_find_stop(gpointer data)
1773 struct connman_device *device = data;
1774 struct wifi_data *wifi = connman_device_get_data(device);
1779 wifi->p2p_find_timeout = 0;
1781 g_supplicant_interface_p2p_stop_find(wifi->interface);
1784 connman_device_set_scanning(device, CONNMAN_SERVICE_TYPE_P2P, false);
1786 connman_device_unref(device);
1787 start_autoscan(device);
1792 static void p2p_find_callback(int result, GSupplicantInterface *interface,
1795 struct connman_device *device = user_data;
1796 struct wifi_data *wifi = connman_device_get_data(device);
1798 DBG("result %d wifi %p", result, wifi);
1803 if (wifi->p2p_find_timeout) {
1804 g_source_remove(wifi->p2p_find_timeout);
1805 wifi->p2p_find_timeout = 0;
1811 wifi->p2p_find_timeout = g_timeout_add_seconds(P2P_FIND_TIMEOUT,
1812 p2p_find_stop, device);
1813 if (!wifi->p2p_find_timeout)
1818 p2p_find_stop(device);
1821 static int p2p_find(struct connman_device *device)
1823 struct wifi_data *wifi;
1828 if (!p2p_technology)
1831 wifi = connman_device_get_data(device);
1833 if (g_supplicant_interface_is_p2p_finding(wifi->interface))
1836 reset_autoscan(device);
1837 connman_device_ref(device);
1839 ret = g_supplicant_interface_p2p_find(wifi->interface,
1840 p2p_find_callback, device);
1842 connman_device_unref(device);
1843 start_autoscan(device);
1845 connman_device_set_scanning(device,
1846 CONNMAN_SERVICE_TYPE_P2P, true);
1853 * Note that the hidden scan is only used when connecting to this specific
1854 * hidden AP first time. It is not used when system autoconnects to hidden AP.
1856 static int wifi_scan(struct connman_device *device,
1857 struct connman_device_scan_params *params)
1859 struct wifi_data *wifi = connman_device_get_data(device);
1860 GSupplicantScanParams *scan_params = NULL;
1861 struct scan_ssid *scan_ssid;
1862 struct hidden_params *hidden;
1864 int driver_max_ssids = 0;
1871 if (wifi->p2p_device)
1874 if (wifi->tethering)
1877 if (params->type == CONNMAN_SERVICE_TYPE_P2P)
1878 return p2p_find(device);
1880 DBG("device %p wifi %p hidden ssid %s", device, wifi->interface,
1883 scanning = connman_device_get_scanning(device, CONNMAN_SERVICE_TYPE_WIFI);
1885 if (!params->ssid || params->ssid_len == 0 || params->ssid_len > 32) {
1889 driver_max_ssids = g_supplicant_interface_get_max_scan_ssids(
1891 DBG("max ssids %d", driver_max_ssids);
1892 if (driver_max_ssids == 0)
1893 return wifi_scan_simple(device);
1897 if (scanning && wifi->hidden && wifi->postpone_hidden)
1903 scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
1908 scan_ssid = g_try_new(struct scan_ssid, 1);
1910 g_free(scan_params);
1914 memcpy(scan_ssid->ssid, params->ssid, params->ssid_len);
1915 scan_ssid->ssid_len = params->ssid_len;
1916 scan_params->ssids = g_slist_prepend(scan_params->ssids,
1918 scan_params->num_ssids = 1;
1920 hidden = g_try_new0(struct hidden_params, 1);
1922 g_supplicant_free_scan_params(scan_params);
1927 hidden_free(wifi->hidden);
1928 wifi->hidden = NULL;
1931 memcpy(hidden->ssid, params->ssid, params->ssid_len);
1932 hidden->ssid_len = params->ssid_len;
1933 hidden->identity = g_strdup(params->identity);
1934 hidden->passphrase = g_strdup(params->passphrase);
1935 hidden->security = g_strdup(params->security);
1936 hidden->user_data = params->user_data;
1937 wifi->hidden = hidden;
1940 /* Let's keep this active scan for later,
1941 * when current scan will be over. */
1942 wifi->postpone_hidden = TRUE;
1943 hidden->scan_params = scan_params;
1947 } else if (wifi->connected) {
1948 g_supplicant_free_scan_params(scan_params);
1949 return wifi_scan_simple(device);
1950 } else if (!params->force_full_scan) {
1951 ret = get_latest_connections(driver_max_ssids, scan_params);
1953 g_supplicant_free_scan_params(scan_params);
1954 return wifi_scan_simple(device);
1958 /* Distinguish between devices performing passive and active scanning */
1959 wifi_update_scanner_type(wifi, WIFI_SCANNING_ACTIVE);
1961 connman_device_ref(device);
1963 reset_autoscan(device);
1965 ret = g_supplicant_interface_scan(wifi->interface, scan_params,
1966 scan_callback, device);
1968 connman_device_set_scanning(device,
1969 CONNMAN_SERVICE_TYPE_WIFI, true);
1971 g_supplicant_free_scan_params(scan_params);
1972 connman_device_unref(device);
1975 hidden_free(wifi->hidden);
1976 wifi->hidden = NULL;
1983 static void wifi_stop_scan(enum connman_service_type type,
1984 struct connman_device *device)
1986 struct wifi_data *wifi = connman_device_get_data(device);
1988 DBG("device %p wifi %p", device, wifi);
1993 if (type == CONNMAN_SERVICE_TYPE_P2P) {
1994 if (connman_device_get_scanning(device, CONNMAN_SERVICE_TYPE_P2P)) {
1995 g_source_remove(wifi->p2p_find_timeout);
1996 p2p_find_stop(device);
2001 static void wifi_regdom_callback(int result,
2005 struct connman_device *device = user_data;
2007 connman_device_regdom_notify(device, result, alpha2);
2009 connman_device_unref(device);
2012 static int wifi_set_regdom(struct connman_device *device, const char *alpha2)
2014 struct wifi_data *wifi = connman_device_get_data(device);
2020 connman_device_ref(device);
2022 ret = g_supplicant_interface_set_country(wifi->interface,
2023 wifi_regdom_callback,
2026 connman_device_unref(device);
2031 static struct connman_device_driver wifi_ng_driver = {
2033 .type = CONNMAN_DEVICE_TYPE_WIFI,
2034 .priority = CONNMAN_DEVICE_PRIORITY_LOW,
2035 .probe = wifi_probe,
2036 .remove = wifi_remove,
2037 .enable = wifi_enable,
2038 .disable = wifi_disable,
2040 .stop_scan = wifi_stop_scan,
2041 .set_regdom = wifi_set_regdom,
2044 static void system_ready(void)
2048 if (connman_device_driver_register(&wifi_ng_driver) < 0)
2049 connman_error("Failed to register WiFi driver");
2052 static void system_killed(void)
2056 connman_device_driver_unregister(&wifi_ng_driver);
2059 static int network_probe(struct connman_network *network)
2061 DBG("network %p", network);
2066 static void network_remove(struct connman_network *network)
2068 struct connman_device *device = connman_network_get_device(network);
2069 struct wifi_data *wifi;
2071 DBG("network %p", network);
2073 wifi = connman_device_get_data(device);
2077 if (wifi->network != network)
2080 wifi->network = NULL;
2083 static void connect_callback(int result, GSupplicantInterface *interface,
2086 struct connman_network *network = user_data;
2088 DBG("network %p result %d", network, result);
2090 if (result == -ENOKEY) {
2091 connman_network_set_error(network,
2092 CONNMAN_NETWORK_ERROR_INVALID_KEY);
2093 } else if (result < 0) {
2094 connman_network_set_error(network,
2095 CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
2098 connman_network_unref(network);
2101 static GSupplicantSecurity network_security(const char *security)
2103 if (g_str_equal(security, "none"))
2104 return G_SUPPLICANT_SECURITY_NONE;
2105 else if (g_str_equal(security, "wep"))
2106 return G_SUPPLICANT_SECURITY_WEP;
2107 else if (g_str_equal(security, "psk"))
2108 return G_SUPPLICANT_SECURITY_PSK;
2109 else if (g_str_equal(security, "wpa"))
2110 return G_SUPPLICANT_SECURITY_PSK;
2111 else if (g_str_equal(security, "rsn"))
2112 return G_SUPPLICANT_SECURITY_PSK;
2113 else if (g_str_equal(security, "ieee8021x"))
2114 return G_SUPPLICANT_SECURITY_IEEE8021X;
2116 return G_SUPPLICANT_SECURITY_UNKNOWN;
2119 static void ssid_init(GSupplicantSSID *ssid, struct connman_network *network)
2121 const char *security;
2123 memset(ssid, 0, sizeof(*ssid));
2124 ssid->mode = G_SUPPLICANT_MODE_INFRA;
2125 ssid->ssid = connman_network_get_blob(network, "WiFi.SSID",
2127 ssid->scan_ssid = 1;
2128 security = connman_network_get_string(network, "WiFi.Security");
2129 ssid->security = network_security(security);
2130 ssid->passphrase = connman_network_get_string(network,
2133 ssid->eap = connman_network_get_string(network, "WiFi.EAP");
2136 * If our private key password is unset,
2137 * we use the supplied passphrase. That is needed
2138 * for PEAP where 2 passphrases (identity and client
2139 * cert may have to be provided.
2141 if (!connman_network_get_string(network, "WiFi.PrivateKeyPassphrase"))
2142 connman_network_set_string(network,
2143 "WiFi.PrivateKeyPassphrase",
2145 /* We must have an identity for both PEAP and TLS */
2146 ssid->identity = connman_network_get_string(network, "WiFi.Identity");
2148 /* Use agent provided identity as a fallback */
2149 if (!ssid->identity || strlen(ssid->identity) == 0)
2150 ssid->identity = connman_network_get_string(network,
2151 "WiFi.AgentIdentity");
2153 ssid->anonymous_identity = connman_network_get_string(network,
2154 "WiFi.AnonymousIdentity");
2155 ssid->ca_cert_path = connman_network_get_string(network,
2157 ssid->subject_match = connman_network_get_string(network,
2158 "WiFi.SubjectMatch");
2159 ssid->altsubject_match = connman_network_get_string(network,
2160 "WiFi.AltSubjectMatch");
2161 ssid->domain_suffix_match = connman_network_get_string(network,
2162 "WiFi.DomainSuffixMatch");
2163 ssid->domain_match = connman_network_get_string(network,
2164 "WiFi.DomainMatch");
2165 ssid->client_cert_path = connman_network_get_string(network,
2166 "WiFi.ClientCertFile");
2167 ssid->private_key_path = connman_network_get_string(network,
2168 "WiFi.PrivateKeyFile");
2169 ssid->private_key_passphrase = connman_network_get_string(network,
2170 "WiFi.PrivateKeyPassphrase");
2171 ssid->phase2_auth = connman_network_get_string(network, "WiFi.Phase2");
2173 ssid->use_wps = connman_network_get_bool(network, "WiFi.UseWPS");
2174 ssid->pin_wps = connman_network_get_string(network, "WiFi.PinWPS");
2176 if (connman_setting_get_bool("BackgroundScanning"))
2177 ssid->bgscan = BGSCAN_DEFAULT;
2180 static int network_connect(struct connman_network *network)
2182 struct connman_device *device = connman_network_get_device(network);
2183 struct wifi_data *wifi;
2184 GSupplicantInterface *interface;
2185 GSupplicantSSID *ssid;
2187 DBG("network %p", network);
2192 wifi = connman_device_get_data(device);
2196 ssid = g_try_malloc0(sizeof(GSupplicantSSID));
2200 interface = wifi->interface;
2202 ssid_init(ssid, network);
2204 if (wifi->disconnecting) {
2205 wifi->pending_network = network;
2208 wifi->network = connman_network_ref(network);
2211 return g_supplicant_interface_connect(interface, ssid,
2212 connect_callback, network);
2215 return -EINPROGRESS;
2218 static void disconnect_callback(int result, GSupplicantInterface *interface,
2221 struct wifi_data *wifi = user_data;
2223 DBG("result %d supplicant interface %p wifi %p",
2224 result, interface, wifi);
2226 if (result == -ECONNABORTED) {
2227 DBG("wifi interface no longer available");
2231 if (wifi->network != wifi->pending_network)
2232 connman_network_set_connected(wifi->network, false);
2233 wifi->network = NULL;
2235 wifi->disconnecting = false;
2236 wifi->connected = false;
2238 if (wifi->pending_network) {
2239 network_connect(wifi->pending_network);
2240 wifi->pending_network = NULL;
2243 start_autoscan(wifi->device);
2246 static int network_disconnect(struct connman_network *network)
2248 struct connman_device *device = connman_network_get_device(network);
2249 struct wifi_data *wifi;
2252 DBG("network %p", network);
2254 wifi = connman_device_get_data(device);
2255 if (!wifi || !wifi->interface)
2258 connman_network_set_associating(network, false);
2260 if (wifi->disconnecting)
2263 wifi->disconnecting = true;
2265 err = g_supplicant_interface_disconnect(wifi->interface,
2266 disconnect_callback, wifi);
2268 wifi->disconnecting = false;
2273 static struct connman_network_driver network_driver = {
2275 .type = CONNMAN_NETWORK_TYPE_WIFI,
2276 .priority = CONNMAN_NETWORK_PRIORITY_LOW,
2277 .probe = network_probe,
2278 .remove = network_remove,
2279 .connect = network_connect,
2280 .disconnect = network_disconnect,
2283 static void interface_added(GSupplicantInterface *interface)
2285 const char *ifname = g_supplicant_interface_get_ifname(interface);
2286 const char *driver = g_supplicant_interface_get_driver(interface);
2287 struct wifi_data *wifi;
2289 wifi = g_supplicant_interface_get_data(interface);
2291 wifi = get_pending_wifi_data(ifname);
2295 wifi->interface = interface;
2296 g_supplicant_interface_set_data(interface, wifi);
2297 p2p_iface_list = g_list_append(p2p_iface_list, wifi);
2298 wifi->p2p_device = true;
2301 DBG("ifname %s driver %s wifi %p tethering %d",
2302 ifname, driver, wifi, wifi->tethering);
2304 if (!wifi->device) {
2305 connman_error("WiFi device not set");
2309 connman_device_set_powered(wifi->device, true);
2312 static bool is_idle(struct wifi_data *wifi)
2314 DBG("state %d", wifi->state);
2316 switch (wifi->state) {
2317 case G_SUPPLICANT_STATE_UNKNOWN:
2318 case G_SUPPLICANT_STATE_DISABLED:
2319 case G_SUPPLICANT_STATE_DISCONNECTED:
2320 case G_SUPPLICANT_STATE_INACTIVE:
2321 case G_SUPPLICANT_STATE_SCANNING:
2324 case G_SUPPLICANT_STATE_AUTHENTICATING:
2325 case G_SUPPLICANT_STATE_ASSOCIATING:
2326 case G_SUPPLICANT_STATE_ASSOCIATED:
2327 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
2328 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
2329 case G_SUPPLICANT_STATE_COMPLETED:
2336 static bool is_idle_wps(GSupplicantInterface *interface,
2337 struct wifi_data *wifi)
2339 /* First, let's check if WPS processing did not went wrong */
2340 if (g_supplicant_interface_get_wps_state(interface) ==
2341 G_SUPPLICANT_WPS_STATE_FAIL)
2344 /* Unlike normal connection, being associated while processing wps
2345 * actually means that we are idling. */
2346 switch (wifi->state) {
2347 case G_SUPPLICANT_STATE_UNKNOWN:
2348 case G_SUPPLICANT_STATE_DISABLED:
2349 case G_SUPPLICANT_STATE_DISCONNECTED:
2350 case G_SUPPLICANT_STATE_INACTIVE:
2351 case G_SUPPLICANT_STATE_SCANNING:
2352 case G_SUPPLICANT_STATE_ASSOCIATED:
2354 case G_SUPPLICANT_STATE_AUTHENTICATING:
2355 case G_SUPPLICANT_STATE_ASSOCIATING:
2356 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
2357 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
2358 case G_SUPPLICANT_STATE_COMPLETED:
2365 static bool handle_wps_completion(GSupplicantInterface *interface,
2366 struct connman_network *network,
2367 struct connman_device *device,
2368 struct wifi_data *wifi)
2372 wps = connman_network_get_bool(network, "WiFi.UseWPS");
2374 const unsigned char *ssid, *wps_ssid;
2375 unsigned int ssid_len, wps_ssid_len;
2376 const char *wps_key;
2378 /* Checking if we got associated with requested
2380 ssid = connman_network_get_blob(network, "WiFi.SSID",
2383 wps_ssid = g_supplicant_interface_get_wps_ssid(
2384 interface, &wps_ssid_len);
2386 if (!wps_ssid || wps_ssid_len != ssid_len ||
2387 memcmp(ssid, wps_ssid, ssid_len) != 0) {
2388 connman_network_set_associating(network, false);
2389 g_supplicant_interface_disconnect(wifi->interface,
2390 disconnect_callback, wifi);
2394 wps_key = g_supplicant_interface_get_wps_key(interface);
2395 connman_network_set_string(network, "WiFi.Passphrase",
2398 connman_network_set_string(network, "WiFi.PinWPS", NULL);
2404 static bool handle_assoc_status_code(GSupplicantInterface *interface,
2405 struct wifi_data *wifi)
2407 if (wifi->state == G_SUPPLICANT_STATE_ASSOCIATING &&
2408 wifi->assoc_code == ASSOC_STATUS_NO_CLIENT &&
2409 wifi->load_shaping_retries < LOAD_SHAPING_MAX_RETRIES) {
2410 wifi->load_shaping_retries ++;
2413 wifi->load_shaping_retries = 0;
2417 static bool handle_4way_handshake_failure(GSupplicantInterface *interface,
2418 struct connman_network *network,
2419 struct wifi_data *wifi)
2421 struct connman_service *service;
2423 if (wifi->state != G_SUPPLICANT_STATE_4WAY_HANDSHAKE)
2426 if (wifi->connected)
2429 service = connman_service_lookup_from_network(network);
2435 if (connman_service_get_favorite(service)) {
2436 if (wifi->retries < FAVORITE_MAXIMUM_RETRIES)
2441 connman_network_set_error(network, CONNMAN_NETWORK_ERROR_INVALID_KEY);
2446 static void interface_state(GSupplicantInterface *interface)
2448 struct connman_network *network;
2449 struct connman_device *device;
2450 struct wifi_data *wifi;
2451 GSupplicantState state = g_supplicant_interface_get_state(interface);
2455 wifi = g_supplicant_interface_get_data(interface);
2457 DBG("wifi %p interface state %d", wifi, state);
2462 device = wifi->device;
2466 if (state == G_SUPPLICANT_STATE_COMPLETED) {
2467 if (wifi->tethering_param) {
2468 g_free(wifi->tethering_param->ssid);
2469 g_free(wifi->tethering_param);
2470 wifi->tethering_param = NULL;
2473 if (wifi->tethering)
2474 stop_autoscan(device);
2477 if (g_supplicant_interface_get_ready(interface) &&
2478 !wifi->interface_ready) {
2479 wifi->interface_ready = true;
2480 finalize_interface_creation(wifi);
2483 network = wifi->network;
2488 case G_SUPPLICANT_STATE_SCANNING:
2489 if (wifi->connected)
2490 connman_network_set_connected(network, false);
2494 case G_SUPPLICANT_STATE_AUTHENTICATING:
2495 case G_SUPPLICANT_STATE_ASSOCIATING:
2496 stop_autoscan(device);
2498 if (!wifi->connected)
2499 connman_network_set_associating(network, true);
2503 case G_SUPPLICANT_STATE_COMPLETED:
2504 /* though it should be already stopped: */
2505 stop_autoscan(device);
2507 if (!handle_wps_completion(interface, network, device, wifi))
2510 connman_network_set_connected(network, true);
2512 wifi->disconnect_code = 0;
2513 wifi->assoc_code = 0;
2514 wifi->load_shaping_retries = 0;
2517 case G_SUPPLICANT_STATE_DISCONNECTED:
2519 * If we're in one of the idle modes, we have
2520 * not started association yet and thus setting
2521 * those ones to FALSE could cancel an association
2524 wps = connman_network_get_bool(network, "WiFi.UseWPS");
2526 if (is_idle_wps(interface, wifi))
2532 if (handle_assoc_status_code(interface, wifi))
2535 /* If previous state was 4way-handshake, then
2536 * it's either: psk was incorrect and thus we retry
2537 * or if we reach the maximum retries we declare the
2539 if (handle_4way_handshake_failure(interface,
2543 /* See table 8-36 Reason codes in IEEE Std 802.11 */
2544 switch (wifi->disconnect_code) {
2545 case 1: /* Unspecified reason */
2546 /* Let's assume it's because we got blocked */
2548 case 6: /* Class 2 frame received from nonauthenticated STA */
2549 connman_network_set_error(network,
2550 CONNMAN_NETWORK_ERROR_BLOCKED);
2557 if (network != wifi->pending_network) {
2558 connman_network_set_connected(network, false);
2559 connman_network_set_associating(network, false);
2561 wifi->disconnecting = false;
2563 start_autoscan(device);
2567 case G_SUPPLICANT_STATE_INACTIVE:
2568 connman_network_set_associating(network, false);
2569 start_autoscan(device);
2573 case G_SUPPLICANT_STATE_UNKNOWN:
2574 case G_SUPPLICANT_STATE_DISABLED:
2575 case G_SUPPLICANT_STATE_ASSOCIATED:
2576 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
2577 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
2581 old_connected = wifi->connected;
2582 wifi->state = state;
2584 /* Saving wpa_s state policy:
2585 * If connected and if the state changes are roaming related:
2586 * --> We stay connected
2588 * --> We are connected
2590 * --> We are not connected
2593 case G_SUPPLICANT_STATE_AUTHENTICATING:
2594 case G_SUPPLICANT_STATE_ASSOCIATING:
2595 case G_SUPPLICANT_STATE_ASSOCIATED:
2596 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
2597 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
2598 if (wifi->connected)
2599 connman_warn("Probably roaming right now!"
2600 " Staying connected...");
2602 case G_SUPPLICANT_STATE_SCANNING:
2603 wifi->connected = false;
2606 start_autoscan(device);
2608 case G_SUPPLICANT_STATE_COMPLETED:
2609 wifi->connected = true;
2612 wifi->connected = false;
2619 static void interface_removed(GSupplicantInterface *interface)
2621 const char *ifname = g_supplicant_interface_get_ifname(interface);
2622 struct wifi_data *wifi;
2624 DBG("ifname %s", ifname);
2626 wifi = g_supplicant_interface_get_data(interface);
2629 wifi->interface = NULL;
2631 if (wifi && wifi->tethering)
2634 if (!wifi || !wifi->device) {
2635 DBG("wifi interface already removed");
2639 connman_device_set_powered(wifi->device, false);
2641 check_p2p_technology();
2644 static void set_device_type(const char *type, char dev_type[17])
2646 const char *oui = "0050F204";
2647 const char *category = "0001";
2648 const char *sub_category = "0000";
2650 if (!g_strcmp0(type, "handset")) {
2652 sub_category = "0005";
2653 } else if (!g_strcmp0(type, "vm") || !g_strcmp0(type, "container"))
2654 sub_category = "0001";
2655 else if (!g_strcmp0(type, "server"))
2656 sub_category = "0002";
2657 else if (!g_strcmp0(type, "laptop"))
2658 sub_category = "0005";
2659 else if (!g_strcmp0(type, "desktop"))
2660 sub_category = "0006";
2661 else if (!g_strcmp0(type, "tablet"))
2662 sub_category = "0009";
2663 else if (!g_strcmp0(type, "watch"))
2666 snprintf(dev_type, 17, "%s%s%s", category, oui, sub_category);
2669 static void p2p_support(GSupplicantInterface *interface)
2671 char dev_type[17] = {};
2672 const char *hostname;
2679 if (!g_supplicant_interface_has_p2p(interface))
2682 if (connman_technology_driver_register(&p2p_tech_driver) < 0) {
2683 DBG("Could not register P2P technology driver");
2687 hostname = connman_utsname_get_hostname();
2689 hostname = "ConnMan";
2691 set_device_type(connman_machine_get_type(), dev_type);
2692 g_supplicant_interface_set_p2p_device_config(interface,
2693 hostname, dev_type);
2694 connman_peer_driver_register(&peer_driver);
2697 static void scan_started(GSupplicantInterface *interface)
2702 static void scan_finished(GSupplicantInterface *interface)
2707 static void ap_create_fail(GSupplicantInterface *interface)
2709 struct wifi_data *wifi = g_supplicant_interface_get_data(interface);
2712 if ((wifi->tethering) && (wifi->tethering_param)) {
2713 DBG("%s create AP fail \n",
2714 g_supplicant_interface_get_ifname(wifi->interface));
2716 connman_inet_remove_from_bridge(wifi->index, wifi->bridge);
2717 wifi->ap_supported = WIFI_AP_NOT_SUPPORTED;
2718 wifi->tethering = false;
2720 ret = tech_set_tethering(wifi->tethering_param->technology,
2721 wifi->tethering_param->ssid->ssid,
2722 wifi->tethering_param->ssid->passphrase,
2723 wifi->bridge, true);
2725 if ((ret == -EOPNOTSUPP) && (wifi_technology)) {
2726 connman_technology_tethering_notify(wifi_technology,false);
2729 g_free(wifi->tethering_param->ssid);
2730 g_free(wifi->tethering_param);
2731 wifi->tethering_param = NULL;
2735 static unsigned char calculate_strength(GSupplicantNetwork *supplicant_network)
2737 unsigned char strength;
2739 strength = 120 + g_supplicant_network_get_signal(supplicant_network);
2746 static void network_added(GSupplicantNetwork *supplicant_network)
2748 struct connman_network *network;
2749 GSupplicantInterface *interface;
2750 struct wifi_data *wifi;
2751 const char *name, *identifier, *security, *group, *mode;
2752 const unsigned char *ssid;
2753 unsigned int ssid_len;
2757 bool wps_advertizing;
2759 mode = g_supplicant_network_get_mode(supplicant_network);
2760 identifier = g_supplicant_network_get_identifier(supplicant_network);
2762 DBG("%s", identifier);
2764 if (!g_strcmp0(mode, "adhoc"))
2767 interface = g_supplicant_network_get_interface(supplicant_network);
2768 wifi = g_supplicant_interface_get_data(interface);
2769 name = g_supplicant_network_get_name(supplicant_network);
2770 security = g_supplicant_network_get_security(supplicant_network);
2771 group = g_supplicant_network_get_identifier(supplicant_network);
2772 wps = g_supplicant_network_get_wps(supplicant_network);
2773 wps_pbc = g_supplicant_network_is_wps_pbc(supplicant_network);
2774 wps_ready = g_supplicant_network_is_wps_active(supplicant_network);
2775 wps_advertizing = g_supplicant_network_is_wps_advertizing(
2776 supplicant_network);
2781 ssid = g_supplicant_network_get_ssid(supplicant_network, &ssid_len);
2783 network = connman_device_get_network(wifi->device, identifier);
2786 network = connman_network_create(identifier,
2787 CONNMAN_NETWORK_TYPE_WIFI);
2791 connman_network_set_index(network, wifi->index);
2793 if (connman_device_add_network(wifi->device, network) < 0) {
2794 connman_network_unref(network);
2798 wifi->networks = g_slist_prepend(wifi->networks, network);
2801 if (name && name[0] != '\0')
2802 connman_network_set_name(network, name);
2804 connman_network_set_blob(network, "WiFi.SSID",
2806 connman_network_set_string(network, "WiFi.Security", security);
2807 connman_network_set_strength(network,
2808 calculate_strength(supplicant_network));
2809 connman_network_set_bool(network, "WiFi.WPS", wps);
2810 connman_network_set_bool(network, "WiFi.WPSAdvertising",
2814 /* Is AP advertizing for WPS association?
2815 * If so, we decide to use WPS by default */
2816 if (wps_ready && wps_pbc &&
2818 connman_network_set_bool(network, "WiFi.UseWPS", true);
2821 connman_network_set_frequency(network,
2822 g_supplicant_network_get_frequency(supplicant_network));
2824 connman_network_set_available(network, true);
2825 connman_network_set_string(network, "WiFi.Mode", mode);
2828 connman_network_set_group(network, group);
2830 if (wifi->hidden && ssid) {
2831 if (!g_strcmp0(wifi->hidden->security, security) &&
2832 wifi->hidden->ssid_len == ssid_len &&
2833 !memcmp(wifi->hidden->ssid, ssid, ssid_len)) {
2834 connman_network_connect_hidden(network,
2835 wifi->hidden->identity,
2836 wifi->hidden->passphrase,
2837 wifi->hidden->user_data);
2838 wifi->hidden->user_data = NULL;
2839 hidden_free(wifi->hidden);
2840 wifi->hidden = NULL;
2845 static void network_removed(GSupplicantNetwork *network)
2847 GSupplicantInterface *interface;
2848 struct wifi_data *wifi;
2849 const char *name, *identifier;
2850 struct connman_network *connman_network;
2852 interface = g_supplicant_network_get_interface(network);
2853 wifi = g_supplicant_interface_get_data(interface);
2854 identifier = g_supplicant_network_get_identifier(network);
2855 name = g_supplicant_network_get_name(network);
2857 DBG("name %s", name);
2862 connman_network = connman_device_get_network(wifi->device, identifier);
2863 if (!connman_network)
2866 wifi->networks = g_slist_remove(wifi->networks, connman_network);
2868 connman_device_remove_network(wifi->device, connman_network);
2869 connman_network_unref(connman_network);
2872 static void network_changed(GSupplicantNetwork *network, const char *property)
2874 GSupplicantInterface *interface;
2875 struct wifi_data *wifi;
2876 const char *name, *identifier;
2877 struct connman_network *connman_network;
2880 interface = g_supplicant_network_get_interface(network);
2881 wifi = g_supplicant_interface_get_data(interface);
2882 identifier = g_supplicant_network_get_identifier(network);
2883 name = g_supplicant_network_get_name(network);
2885 DBG("name %s", name);
2890 connman_network = connman_device_get_network(wifi->device, identifier);
2891 if (!connman_network)
2894 if (g_str_equal(property, "WPSCapabilities")) {
2898 bool wps_advertizing;
2900 wps = g_supplicant_network_get_wps(network);
2901 wps_pbc = g_supplicant_network_is_wps_pbc(network);
2902 wps_ready = g_supplicant_network_is_wps_active(network);
2904 g_supplicant_network_is_wps_advertizing(network);
2906 connman_network_set_bool(connman_network, "WiFi.WPS", wps);
2907 connman_network_set_bool(connman_network,
2908 "WiFi.WPSAdvertising", wps_advertizing);
2912 * Is AP advertizing for WPS association?
2913 * If so, we decide to use WPS by default
2915 if (wps_ready && wps_pbc && wps_advertizing)
2916 connman_network_set_bool(connman_network,
2917 "WiFi.UseWPS", true);
2920 update_needed = true;
2921 } else if (g_str_equal(property, "Signal")) {
2922 connman_network_set_strength(connman_network,
2923 calculate_strength(network));
2924 update_needed = true;
2926 update_needed = false;
2929 connman_network_update(connman_network);
2932 static void network_associated(GSupplicantNetwork *network)
2934 GSupplicantInterface *interface;
2935 struct wifi_data *wifi;
2936 struct connman_network *connman_network;
2937 const char *identifier;
2941 interface = g_supplicant_network_get_interface(network);
2945 wifi = g_supplicant_interface_get_data(interface);
2949 /* P2P networks must not be treated as WiFi networks */
2950 if (wifi->p2p_connecting || wifi->p2p_device)
2953 identifier = g_supplicant_network_get_identifier(network);
2955 connman_network = connman_device_get_network(wifi->device, identifier);
2956 if (!connman_network)
2959 if (wifi->network) {
2960 if (wifi->network == connman_network)
2964 * This should never happen, we got associated with
2965 * a network different than the one we were expecting.
2967 DBG("Associated to %p while expecting %p",
2968 connman_network, wifi->network);
2970 connman_network_set_associating(wifi->network, false);
2973 DBG("Reconnecting to previous network %p from wpa_s", connman_network);
2975 wifi->network = connman_network_ref(connman_network);
2979 * Interface state changes callback (interface_state) is always
2980 * called before network_associated callback thus we need to call
2981 * interface_state again in order to process the new state now that
2982 * we have the network properly set.
2984 interface_state(interface);
2987 static void sta_authorized(GSupplicantInterface *interface,
2990 struct wifi_data *wifi = g_supplicant_interface_get_data(interface);
2992 DBG("wifi %p station %s authorized", wifi, addr);
2994 if (!wifi || !wifi->tethering)
2997 __connman_tethering_client_register(addr);
3000 static void sta_deauthorized(GSupplicantInterface *interface,
3003 struct wifi_data *wifi = g_supplicant_interface_get_data(interface);
3005 DBG("wifi %p station %s deauthorized", wifi, addr);
3007 if (!wifi || !wifi->tethering)
3010 __connman_tethering_client_unregister(addr);
3013 static void apply_peer_services(GSupplicantPeer *peer,
3014 struct connman_peer *connman_peer)
3016 const unsigned char *data;
3021 connman_peer_reset_services(connman_peer);
3023 data = g_supplicant_peer_get_widi_ies(peer, &length);
3025 connman_peer_add_service(connman_peer,
3026 CONNMAN_PEER_SERVICE_WIFI_DISPLAY, data, length);
3030 static void peer_found(GSupplicantPeer *peer)
3032 GSupplicantInterface *iface = g_supplicant_peer_get_interface(peer);
3033 struct wifi_data *wifi = g_supplicant_interface_get_data(iface);
3034 struct connman_peer *connman_peer;
3035 const char *identifier, *name;
3038 identifier = g_supplicant_peer_get_identifier(peer);
3039 name = g_supplicant_peer_get_name(peer);
3041 DBG("ident: %s", identifier);
3043 connman_peer = connman_peer_get(wifi->device, identifier);
3047 connman_peer = connman_peer_create(identifier);
3048 connman_peer_set_name(connman_peer, name);
3049 connman_peer_set_device(connman_peer, wifi->device);
3050 apply_peer_services(peer, connman_peer);
3052 ret = connman_peer_register(connman_peer);
3053 if (ret < 0 && ret != -EALREADY)
3054 connman_peer_unref(connman_peer);
3056 wifi->peers = g_slist_prepend(wifi->peers, connman_peer);
3059 static void peer_lost(GSupplicantPeer *peer)
3061 GSupplicantInterface *iface = g_supplicant_peer_get_interface(peer);
3062 struct wifi_data *wifi = g_supplicant_interface_get_data(iface);
3063 struct connman_peer *connman_peer;
3064 const char *identifier;
3069 identifier = g_supplicant_peer_get_identifier(peer);
3071 DBG("ident: %s", identifier);
3073 connman_peer = connman_peer_get(wifi->device, identifier);
3075 if (wifi->p2p_connecting &&
3076 wifi->pending_peer == connman_peer) {
3077 peer_connect_timeout(wifi);
3079 connman_peer_unregister(connman_peer);
3080 connman_peer_unref(connman_peer);
3083 wifi->peers = g_slist_remove(wifi->peers, connman_peer);
3086 static void peer_changed(GSupplicantPeer *peer, GSupplicantPeerState state)
3088 GSupplicantInterface *iface = g_supplicant_peer_get_interface(peer);
3089 struct wifi_data *wifi = g_supplicant_interface_get_data(iface);
3090 enum connman_peer_state p_state = CONNMAN_PEER_STATE_UNKNOWN;
3091 struct connman_peer *connman_peer;
3092 const char *identifier;
3094 identifier = g_supplicant_peer_get_identifier(peer);
3096 DBG("ident: %s", identifier);
3101 connman_peer = connman_peer_get(wifi->device, identifier);
3106 case G_SUPPLICANT_PEER_SERVICES_CHANGED:
3107 apply_peer_services(peer, connman_peer);
3108 connman_peer_services_changed(connman_peer);
3110 case G_SUPPLICANT_PEER_GROUP_CHANGED:
3111 if (!g_supplicant_peer_is_in_a_group(peer))
3112 p_state = CONNMAN_PEER_STATE_IDLE;
3114 p_state = CONNMAN_PEER_STATE_CONFIGURATION;
3116 case G_SUPPLICANT_PEER_GROUP_STARTED:
3118 case G_SUPPLICANT_PEER_GROUP_FINISHED:
3119 p_state = CONNMAN_PEER_STATE_IDLE;
3121 case G_SUPPLICANT_PEER_GROUP_JOINED:
3122 connman_peer_set_iface_address(connman_peer,
3123 g_supplicant_peer_get_iface_address(peer));
3125 case G_SUPPLICANT_PEER_GROUP_DISCONNECTED:
3126 p_state = CONNMAN_PEER_STATE_IDLE;
3128 case G_SUPPLICANT_PEER_GROUP_FAILED:
3129 if (g_supplicant_peer_has_requested_connection(peer))
3130 p_state = CONNMAN_PEER_STATE_IDLE;
3132 p_state = CONNMAN_PEER_STATE_FAILURE;
3136 if (p_state == CONNMAN_PEER_STATE_CONFIGURATION ||
3137 p_state == CONNMAN_PEER_STATE_FAILURE) {
3138 if (wifi->p2p_connecting
3139 && connman_peer == wifi->pending_peer)
3140 peer_cancel_timeout(wifi);
3142 p_state = CONNMAN_PEER_STATE_UNKNOWN;
3145 if (p_state == CONNMAN_PEER_STATE_UNKNOWN)
3148 if (p_state == CONNMAN_PEER_STATE_CONFIGURATION) {
3149 GSupplicantInterface *g_iface;
3150 struct wifi_data *g_wifi;
3152 g_iface = g_supplicant_peer_get_group_interface(peer);
3156 g_wifi = g_supplicant_interface_get_data(g_iface);
3160 connman_peer_set_as_master(connman_peer,
3161 !g_supplicant_peer_is_client(peer));
3162 connman_peer_set_sub_device(connman_peer, g_wifi->device);
3165 * If wpa_supplicant didn't create a dedicated p2p-group
3166 * interface then mark this interface as p2p_device to avoid
3167 * scan and auto-scan are launched on it while P2P is connected.
3169 if (!g_list_find(p2p_iface_list, g_wifi))
3170 wifi->p2p_device = true;
3173 connman_peer_set_state(connman_peer, p_state);
3176 static void peer_request(GSupplicantPeer *peer)
3178 GSupplicantInterface *iface = g_supplicant_peer_get_interface(peer);
3179 struct wifi_data *wifi = g_supplicant_interface_get_data(iface);
3180 struct connman_peer *connman_peer;
3181 const char *identifier;
3183 identifier = g_supplicant_peer_get_identifier(peer);
3185 DBG("ident: %s", identifier);
3187 connman_peer = connman_peer_get(wifi->device, identifier);
3191 connman_peer_request_connection(connman_peer);
3194 static void debug(const char *str)
3196 if (getenv("CONNMAN_SUPPLICANT_DEBUG"))
3197 connman_debug("%s", str);
3200 static void disconnect_reasoncode(GSupplicantInterface *interface,
3203 struct wifi_data *wifi = g_supplicant_interface_get_data(interface);
3206 wifi->disconnect_code = reasoncode;
3210 static void assoc_status_code(GSupplicantInterface *interface, int status_code)
3212 struct wifi_data *wifi = g_supplicant_interface_get_data(interface);
3215 wifi->assoc_code = status_code;
3219 static const GSupplicantCallbacks callbacks = {
3220 .system_ready = system_ready,
3221 .system_killed = system_killed,
3222 .interface_added = interface_added,
3223 .interface_state = interface_state,
3224 .interface_removed = interface_removed,
3225 .p2p_support = p2p_support,
3226 .scan_started = scan_started,
3227 .scan_finished = scan_finished,
3228 .ap_create_fail = ap_create_fail,
3229 .network_added = network_added,
3230 .network_removed = network_removed,
3231 .network_changed = network_changed,
3232 .network_associated = network_associated,
3233 .sta_authorized = sta_authorized,
3234 .sta_deauthorized = sta_deauthorized,
3235 .peer_found = peer_found,
3236 .peer_lost = peer_lost,
3237 .peer_changed = peer_changed,
3238 .peer_request = peer_request,
3240 .disconnect_reasoncode = disconnect_reasoncode,
3241 .assoc_status_code = assoc_status_code,
3245 static int tech_probe(struct connman_technology *technology)
3247 wifi_technology = technology;
3252 static void tech_remove(struct connman_technology *technology)
3254 wifi_technology = NULL;
3257 static GSupplicantSSID *ssid_ap_init(const char *ssid, const char *passphrase)
3259 GSupplicantSSID *ap;
3261 ap = g_try_malloc0(sizeof(GSupplicantSSID));
3265 ap->mode = G_SUPPLICANT_MODE_MASTER;
3267 ap->ssid_len = strlen(ssid);
3271 if (!passphrase || strlen(passphrase) == 0) {
3272 ap->security = G_SUPPLICANT_SECURITY_NONE;
3273 ap->passphrase = NULL;
3275 ap->security = G_SUPPLICANT_SECURITY_PSK;
3276 ap->protocol = G_SUPPLICANT_PROTO_RSN;
3277 ap->pairwise_cipher = G_SUPPLICANT_PAIRWISE_CCMP;
3278 ap->group_cipher = G_SUPPLICANT_GROUP_CCMP;
3279 ap->passphrase = passphrase;
3285 static void ap_start_callback(int result, GSupplicantInterface *interface,
3288 struct wifi_tethering_info *info = user_data;
3290 DBG("result %d index %d bridge %s",
3291 result, info->wifi->index, info->wifi->bridge);
3293 if ((result < 0) || (info->wifi->ap_supported != WIFI_AP_SUPPORTED)) {
3294 connman_inet_remove_from_bridge(info->wifi->index,
3295 info->wifi->bridge);
3297 if (info->wifi->ap_supported == WIFI_AP_SUPPORTED) {
3298 connman_technology_tethering_notify(info->technology, false);
3299 g_free(info->wifi->tethering_param->ssid);
3300 g_free(info->wifi->tethering_param);
3301 info->wifi->tethering_param = NULL;
3305 g_free(info->ifname);
3309 static void ap_create_callback(int result,
3310 GSupplicantInterface *interface,
3313 struct wifi_tethering_info *info = user_data;
3315 DBG("result %d ifname %s", result,
3316 g_supplicant_interface_get_ifname(interface));
3318 if ((result < 0) || (info->wifi->ap_supported != WIFI_AP_SUPPORTED)) {
3319 connman_inet_remove_from_bridge(info->wifi->index,
3320 info->wifi->bridge);
3322 if (info->wifi->ap_supported == WIFI_AP_SUPPORTED) {
3323 connman_technology_tethering_notify(info->technology, false);
3324 g_free(info->wifi->tethering_param->ssid);
3325 g_free(info->wifi->tethering_param);
3326 info->wifi->tethering_param = NULL;
3330 g_free(info->ifname);
3336 info->wifi->interface = interface;
3337 g_supplicant_interface_set_data(interface, info->wifi);
3339 if (g_supplicant_interface_set_apscan(interface, 2) < 0)
3340 connman_error("Failed to set interface ap_scan property");
3342 g_supplicant_interface_connect(interface, info->ssid,
3343 ap_start_callback, info);
3346 static void sta_remove_callback(int result,
3347 GSupplicantInterface *interface,
3350 struct wifi_tethering_info *info = user_data;
3351 const char *driver = connman_option_get_string("wifi");
3353 DBG("ifname %s result %d ", info->ifname, result);
3355 if ((result < 0) || (info->wifi->ap_supported != WIFI_AP_SUPPORTED)) {
3356 info->wifi->tethering = false;
3357 connman_technology_tethering_notify(info->technology, false);
3359 g_free(info->ifname);
3363 if (info->wifi->ap_supported == WIFI_AP_SUPPORTED) {
3364 g_free(info->wifi->tethering_param->ssid);
3365 g_free(info->wifi->tethering_param);
3366 info->wifi->tethering_param = NULL;
3371 info->wifi->interface = NULL;
3373 g_supplicant_interface_create(info->ifname, driver, info->wifi->bridge,
3378 static int enable_wifi_tethering(struct connman_technology *technology,
3379 const char *bridge, const char *identifier,
3380 const char *passphrase, bool available)
3383 GSupplicantInterface *interface;
3384 struct wifi_data *wifi;
3385 struct wifi_tethering_info *info;
3390 for (list = iface_list; list; list = list->next) {
3393 DBG("wifi %p network %p pending_network %p", wifi,
3394 wifi->network, wifi->pending_network);
3396 interface = wifi->interface;
3401 ifname = g_supplicant_interface_get_ifname(wifi->interface);
3405 if (wifi->ap_supported == WIFI_AP_NOT_SUPPORTED) {
3406 DBG("%s does not support AP mode (detected)", ifname);
3410 mode = g_supplicant_interface_get_mode(interface);
3411 if ((mode & G_SUPPLICANT_CAPABILITY_MODE_AP) == 0) {
3412 wifi->ap_supported = WIFI_AP_NOT_SUPPORTED;
3413 DBG("%s does not support AP mode (capability)", ifname);
3417 if (wifi->network && available)
3420 info = g_try_malloc0(sizeof(struct wifi_tethering_info));
3424 wifi->tethering_param = g_try_malloc0(sizeof(struct wifi_tethering_info));
3425 if (!wifi->tethering_param) {
3431 info->technology = technology;
3432 info->wifi->bridge = bridge;
3433 info->ssid = ssid_ap_init(identifier, passphrase);
3437 info->ifname = g_strdup(ifname);
3439 wifi->tethering_param->technology = technology;
3440 wifi->tethering_param->ssid = ssid_ap_init(identifier, passphrase);
3441 if (!wifi->tethering_param->ssid)
3444 info->wifi->tethering = true;
3445 info->wifi->ap_supported = WIFI_AP_SUPPORTED;
3447 berr = connman_technology_tethering_notify(technology, true);
3451 err = g_supplicant_interface_remove(interface,
3452 sta_remove_callback,
3455 DBG("tethering wifi %p ifname %s", wifi, ifname);
3460 g_free(info->ifname);
3463 g_free(wifi->tethering_param);
3464 wifi->tethering_param = NULL;
3467 * Remove bridge if it was correctly created but remove
3468 * operation failed. Instead, if bridge creation failed then
3469 * break out and do not try again on another interface,
3470 * bridge set-up does not depend on it.
3473 connman_technology_tethering_notify(technology, false);
3481 static int tech_set_tethering(struct connman_technology *technology,
3482 const char *identifier, const char *passphrase,
3483 const char *bridge, bool enabled)
3486 struct wifi_data *wifi;
3492 for (list = iface_list; list; list = list->next) {
3495 if (wifi->tethering) {
3496 wifi->tethering = false;
3498 connman_inet_remove_from_bridge(wifi->index,
3500 wifi->bridged = false;
3504 connman_technology_tethering_notify(technology, false);
3509 DBG("trying tethering for available devices");
3510 err = enable_wifi_tethering(technology, bridge, identifier, passphrase,
3514 DBG("trying tethering for any device");
3515 err = enable_wifi_tethering(technology, bridge, identifier,
3522 static void regdom_callback(int result, const char *alpha2, void *user_data)
3526 if (!wifi_technology)
3532 connman_technology_regdom_notify(wifi_technology, alpha2);
3535 static int tech_set_regdom(struct connman_technology *technology, const char *alpha2)
3537 return g_supplicant_set_country(alpha2, regdom_callback, NULL);
3540 static struct connman_technology_driver tech_driver = {
3542 .type = CONNMAN_SERVICE_TYPE_WIFI,
3543 .probe = tech_probe,
3544 .remove = tech_remove,
3545 .set_tethering = tech_set_tethering,
3546 .set_regdom = tech_set_regdom,
3549 static int wifi_init(void)
3553 err = connman_network_driver_register(&network_driver);
3557 err = g_supplicant_register(&callbacks);
3559 connman_network_driver_unregister(&network_driver);
3563 err = connman_technology_driver_register(&tech_driver);
3565 g_supplicant_unregister(&callbacks);
3566 connman_network_driver_unregister(&network_driver);
3573 static void wifi_exit(void)
3577 connman_technology_driver_unregister(&tech_driver);
3579 g_supplicant_unregister(&callbacks);
3581 connman_network_driver_unregister(&network_driver);
3584 CONNMAN_PLUGIN_DEFINE(wifi, "WiFi interface plugin", VERSION,
3585 CONNMAN_PLUGIN_PRIORITY_DEFAULT, wifi_init, wifi_exit)