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 #include "src/shared/util.h"
64 #define CLEANUP_TIMEOUT 8 /* in seconds */
65 #define INACTIVE_TIMEOUT 12 /* in seconds */
66 #define FAVORITE_MAXIMUM_RETRIES 2
68 #define BGSCAN_DEFAULT "simple:30:-65:300"
69 #define AUTOSCAN_EXPONENTIAL "exponential:3:300"
70 #define AUTOSCAN_SINGLE "single:3"
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_NO_CLIENT 17
78 #define LOAD_SHAPING_MAX_RETRIES 3
80 static struct connman_technology *wifi_technology = NULL;
81 static struct connman_technology *p2p_technology = NULL;
83 enum wifi_ap_capability{
85 WIFI_AP_SUPPORTED = 1,
86 WIFI_AP_NOT_SUPPORTED = 2,
89 enum wifi_scanning_type {
90 WIFI_SCANNING_UNKNOWN = 0,
91 WIFI_SCANNING_PASSIVE = 1,
92 WIFI_SCANNING_ACTIVE = 2,
95 struct hidden_params {
97 unsigned int ssid_len;
99 char *anonymous_identity;
101 char *altsubject_match;
102 char *domain_suffix_match;
106 GSupplicantScanParams *scan_params;
111 * Used for autoscan "emulation".
112 * Should be removed when wpa_s autoscan support will be by default.
114 struct autoscan_params {
118 unsigned int timeout;
121 struct wifi_tethering_info {
122 struct wifi_data *wifi;
123 struct connman_technology *technology;
125 GSupplicantSSID *ssid;
130 struct connman_device *device;
131 struct connman_network *network;
132 struct connman_network *pending_network;
134 GSupplicantInterface *interface;
135 GSupplicantState state;
139 enum wifi_ap_capability ap_supported;
141 bool interface_ready;
147 int load_shaping_retries;
148 struct hidden_params *hidden;
149 bool postpone_hidden;
150 struct wifi_tethering_info *tethering_param;
152 * autoscan "emulation".
154 struct autoscan_params *autoscan;
155 enum wifi_scanning_type scanning_type;
156 GSupplicantScanParams *scan_params;
157 unsigned int p2p_find_timeout;
158 unsigned int p2p_connection_timeout;
159 struct connman_peer *pending_peer;
168 static GList *iface_list = NULL;
170 static GList *pending_wifi_device = NULL;
171 static GList *p2p_iface_list = NULL;
172 static bool wfd_service_registered = false;
174 static void start_autoscan(struct connman_device *device);
175 static int tech_set_tethering(struct connman_technology *technology,
176 const char *identifier, const char *passphrase,
177 const char *bridge, bool enabled);
179 static int p2p_tech_probe(struct connman_technology *technology)
181 p2p_technology = technology;
186 static void p2p_tech_remove(struct connman_technology *technology)
188 p2p_technology = NULL;
191 static struct connman_technology_driver p2p_tech_driver = {
193 .type = CONNMAN_SERVICE_TYPE_P2P,
194 .probe = p2p_tech_probe,
195 .remove = p2p_tech_remove,
198 static bool is_p2p_connecting(void)
202 for (list = iface_list; list; list = list->next) {
203 struct wifi_data *wifi = list->data;
205 if (wifi->p2p_connecting)
212 static void add_pending_wifi_device(struct wifi_data *wifi)
214 if (g_list_find(pending_wifi_device, wifi))
217 pending_wifi_device = g_list_append(pending_wifi_device, wifi);
220 static struct wifi_data *get_pending_wifi_data(const char *ifname)
224 for (list = pending_wifi_device; list; list = list->next) {
225 struct wifi_data *wifi;
226 const char *dev_name;
229 if (!wifi || !wifi->device)
232 dev_name = connman_device_get_string(wifi->device, "Interface");
233 if (!g_strcmp0(ifname, dev_name)) {
234 pending_wifi_device = g_list_delete_link(
235 pending_wifi_device, list);
243 static void remove_pending_wifi_device(struct wifi_data *wifi)
247 link = g_list_find(pending_wifi_device, wifi);
252 pending_wifi_device = g_list_delete_link(pending_wifi_device, link);
255 static void peer_cancel_timeout(struct wifi_data *wifi)
257 if (wifi->p2p_connection_timeout > 0)
258 g_source_remove(wifi->p2p_connection_timeout);
260 wifi->p2p_connection_timeout = 0;
261 wifi->p2p_connecting = false;
263 if (wifi->pending_peer) {
264 connman_peer_unref(wifi->pending_peer);
265 wifi->pending_peer = NULL;
269 static gboolean peer_connect_timeout(gpointer data)
271 struct wifi_data *wifi = data;
275 if (wifi->p2p_connecting) {
276 enum connman_peer_state state = CONNMAN_PEER_STATE_FAILURE;
277 GSupplicantPeer *gs_peer =
278 g_supplicant_interface_peer_lookup(wifi->interface,
279 connman_peer_get_identifier(wifi->pending_peer));
281 if (g_supplicant_peer_has_requested_connection(gs_peer))
282 state = CONNMAN_PEER_STATE_IDLE;
284 connman_peer_set_state(wifi->pending_peer, state);
287 peer_cancel_timeout(wifi);
292 static void peer_connect_callback(int result, GSupplicantInterface *interface,
295 struct wifi_data *wifi = user_data;
296 struct connman_peer *peer = wifi->pending_peer;
298 DBG("peer %p - %d", peer, result);
304 peer_connect_timeout(wifi);
308 connman_peer_set_state(peer, CONNMAN_PEER_STATE_ASSOCIATION);
310 wifi->p2p_connection_timeout = g_timeout_add_seconds(
311 P2P_CONNECTION_TIMEOUT,
312 peer_connect_timeout, wifi);
315 static int peer_connect(struct connman_peer *peer,
316 enum connman_peer_wps_method wps_method,
319 struct connman_device *device = connman_peer_get_device(peer);
320 GSupplicantPeerParams *peer_params;
321 GSupplicantPeer *gs_peer;
322 struct wifi_data *wifi;
326 DBG("peer %p", peer);
331 wifi = connman_device_get_data(device);
332 if (!wifi || !wifi->interface)
335 if (wifi->p2p_connecting)
338 gs_peer = g_supplicant_interface_peer_lookup(wifi->interface,
339 connman_peer_get_identifier(peer));
343 pbc = g_supplicant_peer_is_wps_pbc(gs_peer);
344 pin = g_supplicant_peer_is_wps_pin(gs_peer);
346 switch (wps_method) {
347 case CONNMAN_PEER_WPS_UNKNOWN:
348 if ((pbc && pin) || pin)
351 case CONNMAN_PEER_WPS_PBC:
356 case CONNMAN_PEER_WPS_PIN:
357 if (!pin || !wps_pin)
362 peer_params = g_try_malloc0(sizeof(GSupplicantPeerParams));
366 peer_params->path = g_strdup(g_supplicant_peer_get_path(gs_peer));
368 peer_params->wps_pin = g_strdup(wps_pin);
370 peer_params->master = connman_peer_service_is_master();
372 ret = g_supplicant_interface_p2p_connect(wifi->interface, peer_params,
373 peer_connect_callback, wifi);
374 if (ret == -EINPROGRESS) {
375 wifi->pending_peer = connman_peer_ref(peer);
376 wifi->p2p_connecting = true;
377 } else if (ret < 0) {
378 g_free(peer_params->path);
379 g_free(peer_params->wps_pin);
386 static int peer_disconnect(struct connman_peer *peer)
388 struct connman_device *device = connman_peer_get_device(peer);
389 GSupplicantPeerParams peer_params = {};
390 GSupplicantPeer *gs_peer;
391 struct wifi_data *wifi;
394 DBG("peer %p", peer);
399 wifi = connman_device_get_data(device);
403 gs_peer = g_supplicant_interface_peer_lookup(wifi->interface,
404 connman_peer_get_identifier(peer));
408 peer_params.path = g_strdup(g_supplicant_peer_get_path(gs_peer));
410 ret = g_supplicant_interface_p2p_disconnect(wifi->interface,
412 g_free(peer_params.path);
414 if (ret == -EINPROGRESS) {
415 peer_cancel_timeout(wifi);
416 wifi->p2p_device = false;
422 struct peer_service_registration {
423 peer_service_registration_cb_t callback;
427 static bool is_service_wfd(const unsigned char *specs, int length)
429 if (length < 9 || specs[0] != 0 || specs[1] != 0 || specs[2] != 6)
435 static void apply_p2p_listen_on_iface(gpointer data, gpointer user_data)
437 struct wifi_data *wifi = data;
439 if (!wifi->interface ||
440 !g_supplicant_interface_has_p2p(wifi->interface))
443 if (!wifi->servicing) {
444 g_supplicant_interface_p2p_listen(wifi->interface,
445 P2P_LISTEN_PERIOD, P2P_LISTEN_INTERVAL);
451 static void register_wfd_service_cb(int result,
452 GSupplicantInterface *iface, void *user_data)
454 struct peer_service_registration *reg_data = user_data;
459 g_list_foreach(iface_list, apply_p2p_listen_on_iface, NULL);
461 if (reg_data && reg_data->callback) {
462 reg_data->callback(result, reg_data->user_data);
467 static GSupplicantP2PServiceParams *fill_in_peer_service_params(
468 const unsigned char *spec,
469 int spec_length, const unsigned char *query,
470 int query_length, int version)
472 GSupplicantP2PServiceParams *params;
474 params = g_try_malloc0(sizeof(GSupplicantP2PServiceParams));
479 params->version = version;
480 params->service = g_memdup(spec, spec_length);
481 } else if (query_length > 0 && spec_length > 0) {
482 params->query = g_memdup(query, query_length);
483 params->query_length = query_length;
485 params->response = g_memdup(spec, spec_length);
486 params->response_length = spec_length;
488 params->wfd_ies = g_memdup(spec, spec_length);
489 params->wfd_ies_length = spec_length;
495 static void free_peer_service_params(GSupplicantP2PServiceParams *params)
500 g_free(params->service);
501 g_free(params->query);
502 g_free(params->response);
503 g_free(params->wfd_ies);
508 static int peer_register_wfd_service(const unsigned char *specification,
509 int specification_length,
510 peer_service_registration_cb_t callback,
513 struct peer_service_registration *reg_data = NULL;
514 static GSupplicantP2PServiceParams *params;
519 if (wfd_service_registered)
522 params = fill_in_peer_service_params(specification,
523 specification_length, NULL, 0, 0);
527 reg_data = g_try_malloc0(sizeof(*reg_data));
533 reg_data->callback = callback;
534 reg_data->user_data = user_data;
536 ret = g_supplicant_set_widi_ies(params,
537 register_wfd_service_cb, reg_data);
538 if (ret < 0 && ret != -EINPROGRESS)
541 wfd_service_registered = true;
545 free_peer_service_params(params);
551 static void register_peer_service_cb(int result,
552 GSupplicantInterface *iface, void *user_data)
554 struct wifi_data *wifi = g_supplicant_interface_get_data(iface);
555 struct peer_service_registration *reg_data = user_data;
560 apply_p2p_listen_on_iface(wifi, NULL);
562 if (reg_data->callback)
563 reg_data->callback(result, reg_data->user_data);
568 static int peer_register_service(const unsigned char *specification,
569 int specification_length,
570 const unsigned char *query,
571 int query_length, int version,
572 peer_service_registration_cb_t callback,
575 struct peer_service_registration *reg_data;
576 GSupplicantP2PServiceParams *params;
583 if (specification && !version && !query &&
584 is_service_wfd(specification, specification_length)) {
585 return peer_register_wfd_service(specification,
586 specification_length, callback, user_data);
589 reg_data = g_try_malloc0(sizeof(*reg_data));
593 reg_data->callback = callback;
594 reg_data->user_data = user_data;
598 for (list = iface_list; list; list = list->next) {
599 struct wifi_data *wifi = list->data;
600 GSupplicantInterface *iface = wifi->interface;
602 if (!g_supplicant_interface_has_p2p(iface))
605 params = fill_in_peer_service_params(specification,
606 specification_length, query,
607 query_length, version);
612 ret_f = g_supplicant_interface_p2p_add_service(iface,
613 register_peer_service_cb, params, reg_data);
614 if (ret_f == 0 || ret_f == -EINPROGRESS)
618 ret = g_supplicant_interface_p2p_add_service(iface,
619 register_peer_service_cb, params, NULL);
620 if (ret != 0 && ret != -EINPROGRESS)
621 free_peer_service_params(params);
624 if (ret_f != 0 && ret_f != -EINPROGRESS)
630 static int peer_unregister_wfd_service(void)
632 GSupplicantP2PServiceParams *params;
635 if (!wfd_service_registered)
638 params = fill_in_peer_service_params(NULL, 0, NULL, 0, 0);
642 wfd_service_registered = false;
644 g_supplicant_set_widi_ies(params, NULL, NULL);
646 for (list = iface_list; list; list = list->next) {
647 struct wifi_data *wifi = list->data;
649 if (!g_supplicant_interface_has_p2p(wifi->interface))
653 if (!wifi->servicing || wifi->servicing < 0) {
654 g_supplicant_interface_p2p_listen(wifi->interface,
663 static int peer_unregister_service(const unsigned char *specification,
664 int specification_length,
665 const unsigned char *query,
666 int query_length, int version)
668 GSupplicantP2PServiceParams *params;
673 if (specification && !version && !query &&
674 is_service_wfd(specification, specification_length)) {
675 ret = peer_unregister_wfd_service();
676 if (ret != 0 && ret != -EINPROGRESS)
681 for (list = iface_list; list; list = list->next) {
682 struct wifi_data *wifi = list->data;
683 GSupplicantInterface *iface = wifi->interface;
688 if (!g_supplicant_interface_has_p2p(iface))
691 params = fill_in_peer_service_params(specification,
692 specification_length, query,
693 query_length, version);
697 ret = g_supplicant_interface_p2p_del_service(iface, params);
698 if (ret != 0 && ret != -EINPROGRESS)
699 free_peer_service_params(params);
702 if (!wifi->servicing || wifi->servicing < 0) {
703 g_supplicant_interface_p2p_listen(iface, 0, 0);
711 static struct connman_peer_driver peer_driver = {
712 .connect = peer_connect,
713 .disconnect = peer_disconnect,
714 .register_service = peer_register_service,
715 .unregister_service = peer_unregister_service,
718 static void handle_tethering(struct wifi_data *wifi)
720 if (!wifi->tethering)
729 DBG("index %d bridge %s", wifi->index, wifi->bridge);
731 if (connman_inet_add_to_bridge(wifi->index, wifi->bridge) < 0)
734 wifi->bridged = true;
737 static void wifi_newlink(unsigned flags, unsigned change, void *user_data)
739 struct connman_device *device = user_data;
740 struct wifi_data *wifi = connman_device_get_data(device);
745 DBG("index %d flags %d change %d", wifi->index, flags, change);
747 if ((wifi->flags & IFF_UP) != (flags & IFF_UP)) {
751 DBG("interface down");
754 if ((wifi->flags & IFF_LOWER_UP) != (flags & IFF_LOWER_UP)) {
755 if (flags & IFF_LOWER_UP) {
758 handle_tethering(wifi);
766 static int wifi_probe(struct connman_device *device)
768 struct wifi_data *wifi;
770 DBG("device %p", device);
772 wifi = g_try_new0(struct wifi_data, 1);
776 wifi->state = G_SUPPLICANT_STATE_INACTIVE;
777 wifi->ap_supported = WIFI_AP_UNKNOWN;
778 wifi->tethering_param = NULL;
780 connman_device_set_data(device, wifi);
781 wifi->device = connman_device_ref(device);
783 wifi->index = connman_device_get_index(device);
786 wifi->watch = connman_rtnl_add_newlink_watch(wifi->index,
787 wifi_newlink, device);
788 if (is_p2p_connecting())
789 add_pending_wifi_device(wifi);
791 iface_list = g_list_append(iface_list, wifi);
796 static void remove_networks(struct connman_device *device,
797 struct wifi_data *wifi)
801 for (list = wifi->networks; list; list = list->next) {
802 struct connman_network *network = list->data;
804 connman_device_remove_network(device, network);
805 connman_network_unref(network);
808 g_slist_free(wifi->networks);
809 wifi->networks = NULL;
812 static void remove_peers(struct wifi_data *wifi)
816 for (list = wifi->peers; list; list = list->next) {
817 struct connman_peer *peer = list->data;
819 connman_peer_unregister(peer);
820 connman_peer_unref(peer);
823 g_slist_free(wifi->peers);
827 static void reset_autoscan(struct connman_device *device)
829 struct wifi_data *wifi = connman_device_get_data(device);
830 struct autoscan_params *autoscan;
834 if (!wifi || !wifi->autoscan)
837 autoscan = wifi->autoscan;
839 autoscan->interval = 0;
841 if (autoscan->timeout == 0)
844 g_source_remove(autoscan->timeout);
845 autoscan->timeout = 0;
847 connman_device_unref(device);
850 static void stop_autoscan(struct connman_device *device)
852 const struct wifi_data *wifi = connman_device_get_data(device);
854 if (!wifi || !wifi->autoscan)
857 reset_autoscan(device);
859 connman_device_set_scanning(device, CONNMAN_SERVICE_TYPE_WIFI, false);
862 static void check_p2p_technology(void)
864 bool p2p_exists = false;
867 for (list = iface_list; list; list = list->next) {
868 struct wifi_data *w = list->data;
871 g_supplicant_interface_has_p2p(w->interface))
876 connman_technology_driver_unregister(&p2p_tech_driver);
877 connman_peer_driver_unregister(&peer_driver);
881 static void wifi_remove(struct connman_device *device)
883 struct wifi_data *wifi = connman_device_get_data(device);
885 DBG("device %p wifi %p", device, wifi);
890 stop_autoscan(device);
892 if (wifi->p2p_device)
893 p2p_iface_list = g_list_remove(p2p_iface_list, wifi);
895 iface_list = g_list_remove(iface_list, wifi);
897 check_p2p_technology();
899 remove_pending_wifi_device(wifi);
901 if (connman_device_get_scanning(device, CONNMAN_SERVICE_TYPE_P2P)) {
902 g_source_remove(wifi->p2p_find_timeout);
903 connman_device_unref(wifi->device);
906 if (wifi->p2p_connection_timeout)
907 g_source_remove(wifi->p2p_connection_timeout);
909 remove_networks(device, wifi);
912 connman_device_set_powered(device, false);
913 connman_device_set_data(device, NULL);
914 connman_device_unref(wifi->device);
915 connman_rtnl_remove_watch(wifi->watch);
917 g_supplicant_interface_set_data(wifi->interface, NULL);
919 g_supplicant_interface_cancel(wifi->interface);
921 if (wifi->scan_params)
922 g_supplicant_free_scan_params(wifi->scan_params);
924 g_free(wifi->autoscan);
925 g_free(wifi->identifier);
929 static bool is_duplicate(GSList *list, gchar *ssid, int ssid_len)
933 for (iter = list; iter; iter = g_slist_next(iter)) {
934 struct scan_ssid *scan_ssid = iter->data;
936 if (ssid_len == scan_ssid->ssid_len &&
937 memcmp(ssid, scan_ssid->ssid, ssid_len) == 0)
944 static int add_scan_param(gchar *hex_ssid, char *raw_ssid, int ssid_len,
945 int freq, GSupplicantScanParams *scan_data,
946 int driver_max_scan_ssids, char *ssid_name)
949 struct scan_ssid *scan_ssid;
951 if ((driver_max_scan_ssids == 0 ||
952 driver_max_scan_ssids > scan_data->num_ssids) &&
953 (hex_ssid || raw_ssid)) {
955 unsigned int j = 0, hex;
958 size_t hex_ssid_len = strlen(hex_ssid);
960 ssid = g_try_malloc0(hex_ssid_len / 2);
964 for (i = 0; i < hex_ssid_len; i += 2) {
965 sscanf(hex_ssid + i, "%02x", &hex);
974 * If we have already added hidden AP to the list,
975 * then do not do it again. This might happen if you have
976 * used or are using multiple wifi cards, so in that case
977 * you might have multiple service files for same AP.
979 if (is_duplicate(scan_data->ssids, ssid, j)) {
985 scan_ssid = g_try_new(struct scan_ssid, 1);
992 memcpy(scan_ssid->ssid, ssid, j);
993 scan_ssid->ssid_len = j;
994 scan_data->ssids = g_slist_prepend(scan_data->ssids,
997 scan_data->num_ssids++;
999 DBG("SSID %s added to scanned list of %d entries", ssid_name,
1000 scan_data->num_ssids);
1007 scan_data->ssids = g_slist_reverse(scan_data->ssids);
1009 if (!scan_data->freqs) {
1010 scan_data->freqs = g_try_malloc0(sizeof(uint16_t));
1011 if (!scan_data->freqs) {
1012 g_slist_free_full(scan_data->ssids, g_free);
1016 scan_data->num_freqs = 1;
1017 scan_data->freqs[0] = freq;
1019 bool duplicate = false;
1021 /* Don't add duplicate entries */
1022 for (i = 0; i < scan_data->num_freqs; i++) {
1023 if (scan_data->freqs[i] == freq) {
1030 scan_data->num_freqs++;
1031 scan_data->freqs = g_try_realloc(scan_data->freqs,
1032 sizeof(uint16_t) * scan_data->num_freqs);
1033 if (!scan_data->freqs) {
1034 g_slist_free_full(scan_data->ssids, g_free);
1037 scan_data->freqs[scan_data->num_freqs - 1] = freq;
1044 static int get_hidden_connections(GSupplicantScanParams *scan_data)
1046 struct connman_config_entry **entries;
1052 int num_ssids = 0, add_param_failed = 0;
1054 services = connman_storage_get_services();
1055 for (i = 0; services && services[i]; i++) {
1056 if (strncmp(services[i], "wifi_", 5) != 0)
1059 keyfile = connman_storage_load_service(services[i]);
1063 value = g_key_file_get_boolean(keyfile,
1064 services[i], "Hidden", NULL);
1066 g_key_file_free(keyfile);
1070 value = g_key_file_get_boolean(keyfile,
1071 services[i], "Favorite", NULL);
1073 g_key_file_free(keyfile);
1077 ssid = g_key_file_get_string(keyfile,
1078 services[i], "SSID", NULL);
1080 name = g_key_file_get_string(keyfile, services[i], "Name",
1083 ret = add_scan_param(ssid, NULL, 0, 0, scan_data, 0, name);
1091 g_key_file_free(keyfile);
1095 * Check if there are any hidden AP that needs to be provisioned.
1097 entries = connman_config_get_entries("wifi");
1098 for (i = 0; entries && entries[i]; i++) {
1101 if (!entries[i]->hidden)
1104 if (!entries[i]->ssid) {
1105 ssid = entries[i]->name;
1108 ssid = entries[i]->ssid;
1109 len = entries[i]->ssid_len;
1115 ret = add_scan_param(NULL, ssid, len, 0, scan_data, 0, ssid);
1122 connman_config_free_entries(entries);
1124 if (add_param_failed > 0)
1125 DBG("Unable to scan %d out of %d SSIDs",
1126 add_param_failed, num_ssids);
1128 g_strfreev(services);
1133 static int get_hidden_connections_params(struct wifi_data *wifi,
1134 GSupplicantScanParams *scan_params)
1136 int driver_max_ssids, i;
1137 GSupplicantScanParams *orig_params;
1140 * Scan hidden networks so that we can autoconnect to them.
1141 * We will assume 1 as a default number of ssid to scan.
1143 driver_max_ssids = g_supplicant_interface_get_max_scan_ssids(
1145 if (driver_max_ssids == 0)
1146 driver_max_ssids = 1;
1148 DBG("max ssids %d", driver_max_ssids);
1150 if (!wifi->scan_params) {
1151 wifi->scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
1152 if (!wifi->scan_params)
1155 if (get_hidden_connections(wifi->scan_params) == 0) {
1156 g_supplicant_free_scan_params(wifi->scan_params);
1157 wifi->scan_params = NULL;
1163 orig_params = wifi->scan_params;
1165 /* Let's transfer driver_max_ssids params */
1166 for (i = 0; i < driver_max_ssids; i++) {
1167 struct scan_ssid *ssid;
1169 if (!wifi->scan_params->ssids)
1172 ssid = orig_params->ssids->data;
1173 orig_params->ssids = g_slist_remove(orig_params->ssids, ssid);
1174 scan_params->ssids = g_slist_prepend(scan_params->ssids, ssid);
1178 scan_params->num_ssids = i;
1179 scan_params->ssids = g_slist_reverse(scan_params->ssids);
1181 scan_params->freqs = g_memdup(orig_params->freqs,
1182 sizeof(uint16_t) * orig_params->num_freqs);
1183 if (!scan_params->freqs)
1186 scan_params->num_freqs = orig_params->num_freqs;
1191 orig_params->num_ssids -= scan_params->num_ssids;
1193 return scan_params->num_ssids;
1196 g_slist_free_full(scan_params->ssids, g_free);
1197 g_supplicant_free_scan_params(wifi->scan_params);
1198 wifi->scan_params = NULL;
1203 static int throw_wifi_scan(struct connman_device *device,
1204 GSupplicantInterfaceCallback callback)
1206 struct wifi_data *wifi = connman_device_get_data(device);
1212 DBG("device %p %p", device, wifi->interface);
1214 if (wifi->tethering)
1217 if (connman_device_get_scanning(device, CONNMAN_SERVICE_TYPE_WIFI))
1220 connman_device_ref(device);
1222 ret = g_supplicant_interface_scan(wifi->interface, NULL,
1225 connman_device_set_scanning(device,
1226 CONNMAN_SERVICE_TYPE_WIFI, true);
1228 connman_device_unref(device);
1233 static void hidden_free(struct hidden_params *hidden)
1238 if (hidden->scan_params)
1239 g_supplicant_free_scan_params(hidden->scan_params);
1240 g_free(hidden->identity);
1241 g_free(hidden->passphrase);
1242 g_free(hidden->security);
1246 static void scan_callback(int result, GSupplicantInterface *interface,
1249 struct connman_device *device = user_data;
1250 struct wifi_data *wifi = connman_device_get_data(device);
1253 DBG("result %d wifi %p", result, wifi);
1256 if (wifi->hidden && !wifi->postpone_hidden) {
1257 connman_network_clear_hidden(wifi->hidden->user_data);
1258 hidden_free(wifi->hidden);
1259 wifi->hidden = NULL;
1262 if (wifi->scan_params) {
1263 g_supplicant_free_scan_params(wifi->scan_params);
1264 wifi->scan_params = NULL;
1269 connman_device_reset_scanning(device);
1271 /* User is connecting to a hidden AP, let's wait for finished event */
1272 if (wifi && wifi->hidden && wifi->postpone_hidden) {
1273 GSupplicantScanParams *scan_params;
1276 wifi->postpone_hidden = false;
1277 scan_params = wifi->hidden->scan_params;
1278 wifi->hidden->scan_params = NULL;
1280 reset_autoscan(device);
1282 ret = g_supplicant_interface_scan(wifi->interface, scan_params,
1283 scan_callback, device);
1287 /* On error, let's recall scan_callback, which will cleanup */
1288 return scan_callback(ret, interface, user_data);
1291 scanning = connman_device_get_scanning(device, CONNMAN_SERVICE_TYPE_WIFI);
1294 connman_device_set_scanning(device,
1295 CONNMAN_SERVICE_TYPE_WIFI, false);
1298 if (result != -ENOLINK)
1299 start_autoscan(device);
1302 * If we are here then we were scanning; however, if we are
1303 * also mid-flight disabling the interface, then wifi_disable
1304 * has already cleared the device scanning state and
1305 * unreferenced the device, obviating the need to do it here.
1309 connman_device_unref(device);
1312 static void scan_callback_hidden(int result,
1313 GSupplicantInterface *interface, void *user_data)
1315 struct connman_device *device = user_data;
1316 struct wifi_data *wifi = connman_device_get_data(device);
1317 GSupplicantScanParams *scan_params;
1320 DBG("result %d wifi %p", result, wifi);
1325 /* User is trying to connect to a hidden AP */
1326 if (wifi->hidden && wifi->postpone_hidden)
1329 scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
1333 if (get_hidden_connections_params(wifi, scan_params) > 0) {
1334 ret = g_supplicant_interface_scan(wifi->interface,
1336 scan_callback_hidden,
1342 g_supplicant_free_scan_params(scan_params);
1345 scan_callback(result, interface, user_data);
1348 static gboolean autoscan_timeout(gpointer data)
1350 struct connman_device *device = data;
1351 struct wifi_data *wifi = connman_device_get_data(device);
1352 struct autoscan_params *autoscan;
1358 autoscan = wifi->autoscan;
1360 if (autoscan->interval <= 0) {
1361 interval = autoscan->base;
1364 interval = autoscan->interval * autoscan->base;
1366 if (interval > autoscan->limit)
1367 interval = autoscan->limit;
1369 throw_wifi_scan(wifi->device, scan_callback_hidden);
1372 * In case BackgroundScanning is disabled, interval will reach the
1373 * limit exactly after the very first passive scanning. It allows
1374 * to ensure at most one passive scan is performed in such cases.
1376 if (!connman_setting_get_bool("BackgroundScanning") &&
1377 interval == autoscan->limit) {
1378 g_source_remove(autoscan->timeout);
1379 autoscan->timeout = 0;
1381 connman_device_unref(device);
1387 DBG("interval %d", interval);
1389 autoscan->interval = interval;
1391 autoscan->timeout = g_timeout_add_seconds(interval,
1392 autoscan_timeout, device);
1397 static void start_autoscan(struct connman_device *device)
1399 struct wifi_data *wifi = connman_device_get_data(device);
1400 struct autoscan_params *autoscan;
1407 if (wifi->p2p_device)
1410 if (wifi->connected)
1413 autoscan = wifi->autoscan;
1417 if (autoscan->timeout > 0 || autoscan->interval > 0)
1420 connman_device_ref(device);
1422 autoscan_timeout(device);
1425 static struct autoscan_params *parse_autoscan_params(const char *params)
1427 struct autoscan_params *autoscan;
1434 list_params = g_strsplit(params, ":", 0);
1435 if (list_params == 0)
1438 if (!g_strcmp0(list_params[0], "exponential") &&
1439 g_strv_length(list_params) == 3) {
1440 base = atoi(list_params[1]);
1441 limit = atoi(list_params[2]);
1442 } else if (!g_strcmp0(list_params[0], "single") &&
1443 g_strv_length(list_params) == 2)
1444 base = limit = atoi(list_params[1]);
1446 g_strfreev(list_params);
1450 DBG("Setup %s autoscanning", list_params[0]);
1452 g_strfreev(list_params);
1454 autoscan = g_try_malloc0(sizeof(struct autoscan_params));
1456 DBG("Could not allocate memory for autoscan");
1460 DBG("base %d - limit %d", base, limit);
1461 autoscan->base = base;
1462 autoscan->limit = limit;
1467 static void setup_autoscan(struct wifi_data *wifi)
1470 * If BackgroundScanning is enabled, setup exponential
1471 * autoscanning if it has not been previously done.
1473 if (connman_setting_get_bool("BackgroundScanning")) {
1474 wifi->autoscan = parse_autoscan_params(AUTOSCAN_EXPONENTIAL);
1479 * On the contrary, if BackgroundScanning is disabled, update autoscan
1480 * parameters based on the type of scanning that is being performed.
1482 if (wifi->autoscan) {
1483 g_free(wifi->autoscan);
1484 wifi->autoscan = NULL;
1487 switch (wifi->scanning_type) {
1488 case WIFI_SCANNING_PASSIVE:
1489 /* Do not setup autoscan. */
1491 case WIFI_SCANNING_ACTIVE:
1492 /* Setup one single passive scan after active. */
1493 wifi->autoscan = parse_autoscan_params(AUTOSCAN_SINGLE);
1495 case WIFI_SCANNING_UNKNOWN:
1496 /* Setup autoscan in this case but we should never fall here. */
1497 wifi->autoscan = parse_autoscan_params(AUTOSCAN_SINGLE);
1502 static void finalize_interface_creation(struct wifi_data *wifi)
1504 DBG("interface is ready wifi %p tethering %d", wifi, wifi->tethering);
1506 if (!wifi->device) {
1507 connman_error("WiFi device not set");
1511 connman_device_set_powered(wifi->device, true);
1513 if (wifi->p2p_device)
1516 if (!wifi->autoscan)
1517 setup_autoscan(wifi);
1519 start_autoscan(wifi->device);
1522 static void interface_create_callback(int result,
1523 GSupplicantInterface *interface,
1526 struct wifi_data *wifi = user_data;
1528 DBG("result %d ifname %s, wifi %p", result,
1529 g_supplicant_interface_get_ifname(interface),
1532 if (result < 0 || !wifi)
1535 wifi->interface = interface;
1536 g_supplicant_interface_set_data(interface, wifi);
1538 if (g_supplicant_interface_get_ready(interface)) {
1539 wifi->interface_ready = true;
1540 finalize_interface_creation(wifi);
1544 static int wifi_enable(struct connman_device *device)
1546 struct wifi_data *wifi = connman_device_get_data(device);
1549 const char *driver = connman_option_get_string("wifi");
1552 DBG("device %p %p", device, wifi);
1554 index = connman_device_get_index(device);
1555 if (!wifi || index < 0)
1558 if (is_p2p_connecting())
1559 return -EINPROGRESS;
1561 interface = connman_inet_ifname(index);
1562 ret = g_supplicant_interface_create(interface, driver, NULL,
1563 interface_create_callback,
1570 return -EINPROGRESS;
1573 static int wifi_disable(struct connman_device *device)
1575 struct wifi_data *wifi = connman_device_get_data(device);
1578 DBG("device %p wifi %p", device, wifi);
1583 wifi->connected = false;
1584 wifi->disconnecting = false;
1586 if (wifi->pending_network)
1587 wifi->pending_network = NULL;
1589 stop_autoscan(device);
1591 if (connman_device_get_scanning(device, CONNMAN_SERVICE_TYPE_P2P)) {
1592 g_source_remove(wifi->p2p_find_timeout);
1593 wifi->p2p_find_timeout = 0;
1594 connman_device_set_scanning(device, CONNMAN_SERVICE_TYPE_P2P, false);
1595 connman_device_unref(wifi->device);
1598 /* In case of a user scan, device is still referenced */
1599 if (connman_device_get_scanning(device, CONNMAN_SERVICE_TYPE_WIFI)) {
1600 connman_device_set_scanning(device,
1601 CONNMAN_SERVICE_TYPE_WIFI, false);
1602 connman_device_unref(wifi->device);
1605 remove_networks(device, wifi);
1608 ret = g_supplicant_interface_remove(wifi->interface, NULL, NULL);
1612 return -EINPROGRESS;
1615 struct last_connected {
1616 struct timeval modified;
1621 static gint sort_entry(gconstpointer a, gconstpointer b, gpointer user_data)
1623 struct timeval *aval = (struct timeval *)a;
1624 struct timeval *bval = (struct timeval *)b;
1626 /* Note that the sort order is descending */
1627 if (aval->tv_sec < bval->tv_sec)
1630 if (aval->tv_sec > bval->tv_sec)
1636 static void free_entry(gpointer data)
1638 struct last_connected *entry = data;
1640 g_free(entry->ssid);
1644 static int get_latest_connections(int max_ssids,
1645 GSupplicantScanParams *scan_data)
1647 GSequenceIter *iter;
1648 GSequence *latest_list;
1649 struct last_connected *entry;
1651 struct timeval modified;
1658 latest_list = g_sequence_new(free_entry);
1662 services = connman_storage_get_services();
1663 for (i = 0; services && services[i]; i++) {
1664 if (strncmp(services[i], "wifi_", 5) != 0)
1667 keyfile = connman_storage_load_service(services[i]);
1671 str = g_key_file_get_string(keyfile,
1672 services[i], "Favorite", NULL);
1673 if (!str || g_strcmp0(str, "true")) {
1675 g_key_file_free(keyfile);
1680 str = g_key_file_get_string(keyfile,
1681 services[i], "AutoConnect", NULL);
1682 if (!str || g_strcmp0(str, "true")) {
1684 g_key_file_free(keyfile);
1689 str = g_key_file_get_string(keyfile,
1690 services[i], "Modified", NULL);
1692 g_key_file_free(keyfile);
1695 util_iso8601_to_timeval(str, &modified);
1698 ssid = g_key_file_get_string(keyfile,
1699 services[i], "SSID", NULL);
1701 freq = g_key_file_get_integer(keyfile, services[i],
1704 entry = g_try_new(struct last_connected, 1);
1706 g_sequence_free(latest_list);
1707 g_key_file_free(keyfile);
1713 entry->modified = modified;
1716 g_sequence_insert_sorted(latest_list, entry,
1722 g_key_file_free(keyfile);
1725 g_strfreev(services);
1727 num_ssids = num_ssids > max_ssids ? max_ssids : num_ssids;
1729 iter = g_sequence_get_begin_iter(latest_list);
1731 for (i = 0; i < num_ssids; i++) {
1732 entry = g_sequence_get(iter);
1734 DBG("ssid %s freq %d modified %lu", entry->ssid, entry->freq,
1735 entry->modified.tv_sec);
1737 add_scan_param(entry->ssid, NULL, 0, entry->freq, scan_data,
1738 max_ssids, entry->ssid);
1740 iter = g_sequence_iter_next(iter);
1743 g_sequence_free(latest_list);
1747 static void wifi_update_scanner_type(struct wifi_data *wifi,
1748 enum wifi_scanning_type new_type)
1752 if (!wifi || wifi->scanning_type == new_type)
1755 wifi->scanning_type = new_type;
1757 setup_autoscan(wifi);
1760 static int wifi_scan_simple(struct connman_device *device)
1762 struct wifi_data *wifi = connman_device_get_data(device);
1764 reset_autoscan(device);
1766 /* Distinguish between devices performing passive and active scanning */
1768 wifi_update_scanner_type(wifi, WIFI_SCANNING_PASSIVE);
1770 return throw_wifi_scan(device, scan_callback_hidden);
1773 static gboolean p2p_find_stop(gpointer data)
1775 struct connman_device *device = data;
1776 struct wifi_data *wifi = connman_device_get_data(device);
1781 wifi->p2p_find_timeout = 0;
1783 g_supplicant_interface_p2p_stop_find(wifi->interface);
1786 connman_device_set_scanning(device, CONNMAN_SERVICE_TYPE_P2P, false);
1788 connman_device_unref(device);
1789 start_autoscan(device);
1794 static void p2p_find_callback(int result, GSupplicantInterface *interface,
1797 struct connman_device *device = user_data;
1798 struct wifi_data *wifi = connman_device_get_data(device);
1800 DBG("result %d wifi %p", result, wifi);
1805 if (wifi->p2p_find_timeout) {
1806 g_source_remove(wifi->p2p_find_timeout);
1807 wifi->p2p_find_timeout = 0;
1813 wifi->p2p_find_timeout = g_timeout_add_seconds(P2P_FIND_TIMEOUT,
1814 p2p_find_stop, device);
1815 if (!wifi->p2p_find_timeout)
1820 p2p_find_stop(device);
1823 static int p2p_find(struct connman_device *device)
1825 struct wifi_data *wifi;
1830 if (!p2p_technology)
1833 wifi = connman_device_get_data(device);
1835 if (g_supplicant_interface_is_p2p_finding(wifi->interface))
1838 reset_autoscan(device);
1839 connman_device_ref(device);
1841 ret = g_supplicant_interface_p2p_find(wifi->interface,
1842 p2p_find_callback, device);
1844 connman_device_unref(device);
1845 start_autoscan(device);
1847 connman_device_set_scanning(device,
1848 CONNMAN_SERVICE_TYPE_P2P, true);
1855 * Note that the hidden scan is only used when connecting to this specific
1856 * hidden AP first time. It is not used when system autoconnects to hidden AP.
1858 static int wifi_scan(struct connman_device *device,
1859 struct connman_device_scan_params *params)
1861 struct wifi_data *wifi = connman_device_get_data(device);
1862 GSupplicantScanParams *scan_params = NULL;
1863 struct scan_ssid *scan_ssid;
1864 struct hidden_params *hidden;
1866 int driver_max_ssids = 0;
1873 if (wifi->p2p_device)
1876 if (wifi->tethering)
1879 if (params->type == CONNMAN_SERVICE_TYPE_P2P)
1880 return p2p_find(device);
1882 DBG("device %p wifi %p hidden ssid %s", device, wifi->interface,
1885 scanning = connman_device_get_scanning(device, CONNMAN_SERVICE_TYPE_WIFI);
1887 if (!params->ssid || params->ssid_len == 0 || params->ssid_len > 32) {
1891 driver_max_ssids = g_supplicant_interface_get_max_scan_ssids(
1893 DBG("max ssids %d", driver_max_ssids);
1894 if (driver_max_ssids == 0)
1895 return wifi_scan_simple(device);
1899 if (scanning && wifi->hidden && wifi->postpone_hidden)
1905 scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
1910 scan_ssid = g_try_new(struct scan_ssid, 1);
1912 g_free(scan_params);
1916 memcpy(scan_ssid->ssid, params->ssid, params->ssid_len);
1917 scan_ssid->ssid_len = params->ssid_len;
1918 scan_params->ssids = g_slist_prepend(scan_params->ssids,
1920 scan_params->num_ssids = 1;
1922 hidden = g_try_new0(struct hidden_params, 1);
1924 g_supplicant_free_scan_params(scan_params);
1929 hidden_free(wifi->hidden);
1930 wifi->hidden = NULL;
1933 memcpy(hidden->ssid, params->ssid, params->ssid_len);
1934 hidden->ssid_len = params->ssid_len;
1935 hidden->identity = g_strdup(params->identity);
1936 hidden->passphrase = g_strdup(params->passphrase);
1937 hidden->security = g_strdup(params->security);
1938 hidden->user_data = params->user_data;
1939 wifi->hidden = hidden;
1942 /* Let's keep this active scan for later,
1943 * when current scan will be over. */
1944 wifi->postpone_hidden = TRUE;
1945 hidden->scan_params = scan_params;
1949 } else if (wifi->connected) {
1950 g_supplicant_free_scan_params(scan_params);
1951 return wifi_scan_simple(device);
1952 } else if (!params->force_full_scan) {
1953 ret = get_latest_connections(driver_max_ssids, scan_params);
1955 g_supplicant_free_scan_params(scan_params);
1956 return wifi_scan_simple(device);
1960 /* Distinguish between devices performing passive and active scanning */
1961 wifi_update_scanner_type(wifi, WIFI_SCANNING_ACTIVE);
1963 connman_device_ref(device);
1965 reset_autoscan(device);
1967 ret = g_supplicant_interface_scan(wifi->interface, scan_params,
1968 scan_callback, device);
1970 connman_device_set_scanning(device,
1971 CONNMAN_SERVICE_TYPE_WIFI, true);
1973 g_supplicant_free_scan_params(scan_params);
1974 connman_device_unref(device);
1977 hidden_free(wifi->hidden);
1978 wifi->hidden = NULL;
1985 static void wifi_stop_scan(enum connman_service_type type,
1986 struct connman_device *device)
1988 struct wifi_data *wifi = connman_device_get_data(device);
1990 DBG("device %p wifi %p", device, wifi);
1995 if (type == CONNMAN_SERVICE_TYPE_P2P) {
1996 if (connman_device_get_scanning(device, CONNMAN_SERVICE_TYPE_P2P)) {
1997 g_source_remove(wifi->p2p_find_timeout);
1998 p2p_find_stop(device);
2003 static void wifi_regdom_callback(int result,
2007 struct connman_device *device = user_data;
2009 connman_device_regdom_notify(device, result, alpha2);
2011 connman_device_unref(device);
2014 static int wifi_set_regdom(struct connman_device *device, const char *alpha2)
2016 struct wifi_data *wifi = connman_device_get_data(device);
2022 connman_device_ref(device);
2024 ret = g_supplicant_interface_set_country(wifi->interface,
2025 wifi_regdom_callback,
2028 connman_device_unref(device);
2033 static struct connman_device_driver wifi_ng_driver = {
2035 .type = CONNMAN_DEVICE_TYPE_WIFI,
2036 .priority = CONNMAN_DEVICE_PRIORITY_LOW,
2037 .probe = wifi_probe,
2038 .remove = wifi_remove,
2039 .enable = wifi_enable,
2040 .disable = wifi_disable,
2042 .stop_scan = wifi_stop_scan,
2043 .set_regdom = wifi_set_regdom,
2046 static void system_ready(void)
2050 if (connman_device_driver_register(&wifi_ng_driver) < 0)
2051 connman_error("Failed to register WiFi driver");
2054 static void system_killed(void)
2058 connman_device_driver_unregister(&wifi_ng_driver);
2061 static int network_probe(struct connman_network *network)
2063 DBG("network %p", network);
2068 static void network_remove(struct connman_network *network)
2070 struct connman_device *device = connman_network_get_device(network);
2071 struct wifi_data *wifi;
2073 DBG("network %p", network);
2075 wifi = connman_device_get_data(device);
2079 if (wifi->network != network)
2082 wifi->network = NULL;
2085 static void connect_callback(int result, GSupplicantInterface *interface,
2088 struct connman_network *network = user_data;
2090 DBG("network %p result %d", network, result);
2092 if (result == -ENOKEY) {
2093 connman_network_set_error(network,
2094 CONNMAN_NETWORK_ERROR_INVALID_KEY);
2095 } else if (result < 0) {
2096 connman_network_set_error(network,
2097 CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
2100 connman_network_unref(network);
2103 static GSupplicantSecurity network_security(const char *security)
2105 if (g_str_equal(security, "none"))
2106 return G_SUPPLICANT_SECURITY_NONE;
2107 else if (g_str_equal(security, "wep"))
2108 return G_SUPPLICANT_SECURITY_WEP;
2109 else if (g_str_equal(security, "psk"))
2110 return G_SUPPLICANT_SECURITY_PSK;
2111 else if (g_str_equal(security, "wpa"))
2112 return G_SUPPLICANT_SECURITY_PSK;
2113 else if (g_str_equal(security, "rsn"))
2114 return G_SUPPLICANT_SECURITY_PSK;
2115 else if (g_str_equal(security, "ieee8021x"))
2116 return G_SUPPLICANT_SECURITY_IEEE8021X;
2118 return G_SUPPLICANT_SECURITY_UNKNOWN;
2121 static void ssid_init(GSupplicantSSID *ssid, struct connman_network *network)
2123 const char *security;
2125 memset(ssid, 0, sizeof(*ssid));
2126 ssid->mode = G_SUPPLICANT_MODE_INFRA;
2127 ssid->ssid = connman_network_get_blob(network, "WiFi.SSID",
2129 ssid->scan_ssid = 1;
2130 security = connman_network_get_string(network, "WiFi.Security");
2131 ssid->security = network_security(security);
2132 ssid->passphrase = connman_network_get_string(network,
2135 ssid->eap = connman_network_get_string(network, "WiFi.EAP");
2138 * If our private key password is unset,
2139 * we use the supplied passphrase. That is needed
2140 * for PEAP where 2 passphrases (identity and client
2141 * cert may have to be provided.
2143 if (!connman_network_get_string(network, "WiFi.PrivateKeyPassphrase"))
2144 connman_network_set_string(network,
2145 "WiFi.PrivateKeyPassphrase",
2147 /* We must have an identity for both PEAP and TLS */
2148 ssid->identity = connman_network_get_string(network, "WiFi.Identity");
2150 /* Use agent provided identity as a fallback */
2151 if (!ssid->identity || strlen(ssid->identity) == 0)
2152 ssid->identity = connman_network_get_string(network,
2153 "WiFi.AgentIdentity");
2155 ssid->anonymous_identity = connman_network_get_string(network,
2156 "WiFi.AnonymousIdentity");
2157 ssid->ca_cert_path = connman_network_get_string(network,
2159 ssid->subject_match = connman_network_get_string(network,
2160 "WiFi.SubjectMatch");
2161 ssid->altsubject_match = connman_network_get_string(network,
2162 "WiFi.AltSubjectMatch");
2163 ssid->domain_suffix_match = connman_network_get_string(network,
2164 "WiFi.DomainSuffixMatch");
2165 ssid->domain_match = connman_network_get_string(network,
2166 "WiFi.DomainMatch");
2167 ssid->client_cert_path = connman_network_get_string(network,
2168 "WiFi.ClientCertFile");
2169 ssid->private_key_path = connman_network_get_string(network,
2170 "WiFi.PrivateKeyFile");
2171 ssid->private_key_passphrase = connman_network_get_string(network,
2172 "WiFi.PrivateKeyPassphrase");
2173 ssid->phase2_auth = connman_network_get_string(network, "WiFi.Phase2");
2175 ssid->use_wps = connman_network_get_bool(network, "WiFi.UseWPS");
2176 ssid->pin_wps = connman_network_get_string(network, "WiFi.PinWPS");
2178 if (connman_setting_get_bool("BackgroundScanning"))
2179 ssid->bgscan = BGSCAN_DEFAULT;
2182 static int network_connect(struct connman_network *network)
2184 struct connman_device *device = connman_network_get_device(network);
2185 struct wifi_data *wifi;
2186 GSupplicantInterface *interface;
2187 GSupplicantSSID *ssid;
2189 DBG("network %p", network);
2194 wifi = connman_device_get_data(device);
2198 ssid = g_try_malloc0(sizeof(GSupplicantSSID));
2202 interface = wifi->interface;
2204 ssid_init(ssid, network);
2206 if (wifi->disconnecting) {
2207 wifi->pending_network = network;
2210 wifi->network = connman_network_ref(network);
2213 return g_supplicant_interface_connect(interface, ssid,
2214 connect_callback, network);
2217 return -EINPROGRESS;
2220 static void disconnect_callback(int result, GSupplicantInterface *interface,
2223 struct wifi_data *wifi = user_data;
2225 DBG("result %d supplicant interface %p wifi %p",
2226 result, interface, wifi);
2228 if (result == -ECONNABORTED) {
2229 DBG("wifi interface no longer available");
2233 if (wifi->network && wifi->network != wifi->pending_network)
2234 connman_network_set_connected(wifi->network, false);
2235 wifi->network = NULL;
2237 wifi->disconnecting = false;
2238 wifi->connected = false;
2240 if (wifi->pending_network) {
2241 network_connect(wifi->pending_network);
2242 wifi->pending_network = NULL;
2245 start_autoscan(wifi->device);
2248 static int network_disconnect(struct connman_network *network)
2250 struct connman_device *device = connman_network_get_device(network);
2251 struct wifi_data *wifi;
2254 DBG("network %p", network);
2256 wifi = connman_device_get_data(device);
2257 if (!wifi || !wifi->interface)
2260 connman_network_set_associating(network, false);
2262 if (wifi->disconnecting)
2265 wifi->disconnecting = true;
2267 err = g_supplicant_interface_disconnect(wifi->interface,
2268 disconnect_callback, wifi);
2270 wifi->disconnecting = false;
2275 static struct connman_network_driver network_driver = {
2277 .type = CONNMAN_NETWORK_TYPE_WIFI,
2278 .priority = CONNMAN_NETWORK_PRIORITY_LOW,
2279 .probe = network_probe,
2280 .remove = network_remove,
2281 .connect = network_connect,
2282 .disconnect = network_disconnect,
2285 static void interface_added(GSupplicantInterface *interface)
2287 const char *ifname = g_supplicant_interface_get_ifname(interface);
2288 const char *driver = g_supplicant_interface_get_driver(interface);
2289 struct wifi_data *wifi;
2291 wifi = g_supplicant_interface_get_data(interface);
2293 wifi = get_pending_wifi_data(ifname);
2297 wifi->interface = interface;
2298 g_supplicant_interface_set_data(interface, wifi);
2299 p2p_iface_list = g_list_append(p2p_iface_list, wifi);
2300 wifi->p2p_device = true;
2303 DBG("ifname %s driver %s wifi %p tethering %d",
2304 ifname, driver, wifi, wifi->tethering);
2306 if (!wifi->device) {
2307 connman_error("WiFi device not set");
2311 connman_device_set_powered(wifi->device, true);
2314 static bool is_idle(struct wifi_data *wifi)
2316 DBG("state %d", wifi->state);
2318 switch (wifi->state) {
2319 case G_SUPPLICANT_STATE_UNKNOWN:
2320 case G_SUPPLICANT_STATE_DISABLED:
2321 case G_SUPPLICANT_STATE_DISCONNECTED:
2322 case G_SUPPLICANT_STATE_INACTIVE:
2323 case G_SUPPLICANT_STATE_SCANNING:
2326 case G_SUPPLICANT_STATE_AUTHENTICATING:
2327 case G_SUPPLICANT_STATE_ASSOCIATING:
2328 case G_SUPPLICANT_STATE_ASSOCIATED:
2329 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
2330 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
2331 case G_SUPPLICANT_STATE_COMPLETED:
2338 static bool is_idle_wps(GSupplicantInterface *interface,
2339 struct wifi_data *wifi)
2341 /* First, let's check if WPS processing did not went wrong */
2342 if (g_supplicant_interface_get_wps_state(interface) ==
2343 G_SUPPLICANT_WPS_STATE_FAIL)
2346 /* Unlike normal connection, being associated while processing wps
2347 * actually means that we are idling. */
2348 switch (wifi->state) {
2349 case G_SUPPLICANT_STATE_UNKNOWN:
2350 case G_SUPPLICANT_STATE_DISABLED:
2351 case G_SUPPLICANT_STATE_DISCONNECTED:
2352 case G_SUPPLICANT_STATE_INACTIVE:
2353 case G_SUPPLICANT_STATE_SCANNING:
2354 case G_SUPPLICANT_STATE_ASSOCIATED:
2356 case G_SUPPLICANT_STATE_AUTHENTICATING:
2357 case G_SUPPLICANT_STATE_ASSOCIATING:
2358 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
2359 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
2360 case G_SUPPLICANT_STATE_COMPLETED:
2367 static bool handle_wps_completion(GSupplicantInterface *interface,
2368 struct connman_network *network,
2369 struct connman_device *device,
2370 struct wifi_data *wifi)
2374 wps = connman_network_get_bool(network, "WiFi.UseWPS");
2376 const unsigned char *ssid, *wps_ssid;
2377 unsigned int ssid_len, wps_ssid_len;
2378 const char *wps_key;
2380 /* Checking if we got associated with requested
2382 ssid = connman_network_get_blob(network, "WiFi.SSID",
2385 wps_ssid = g_supplicant_interface_get_wps_ssid(
2386 interface, &wps_ssid_len);
2388 if (!wps_ssid || wps_ssid_len != ssid_len ||
2389 memcmp(ssid, wps_ssid, ssid_len) != 0) {
2390 connman_network_set_associating(network, false);
2391 g_supplicant_interface_disconnect(wifi->interface,
2392 disconnect_callback, wifi);
2396 wps_key = g_supplicant_interface_get_wps_key(interface);
2397 connman_network_set_string(network, "WiFi.Passphrase",
2400 connman_network_set_string(network, "WiFi.PinWPS", NULL);
2406 static bool handle_assoc_status_code(GSupplicantInterface *interface,
2407 struct wifi_data *wifi)
2409 if (wifi->state == G_SUPPLICANT_STATE_ASSOCIATING &&
2410 wifi->assoc_code == ASSOC_STATUS_NO_CLIENT &&
2411 wifi->load_shaping_retries < LOAD_SHAPING_MAX_RETRIES) {
2412 wifi->load_shaping_retries ++;
2415 wifi->load_shaping_retries = 0;
2419 static bool handle_4way_handshake_failure(GSupplicantInterface *interface,
2420 struct connman_network *network,
2421 struct wifi_data *wifi)
2423 struct connman_service *service;
2425 if (wifi->state != G_SUPPLICANT_STATE_4WAY_HANDSHAKE)
2428 if (wifi->connected)
2431 service = connman_service_lookup_from_network(network);
2437 if (connman_service_get_favorite(service)) {
2438 if (wifi->retries < FAVORITE_MAXIMUM_RETRIES)
2443 connman_network_set_error(network, CONNMAN_NETWORK_ERROR_INVALID_KEY);
2448 static void interface_state(GSupplicantInterface *interface)
2450 struct connman_network *network;
2451 struct connman_device *device;
2452 struct wifi_data *wifi;
2453 GSupplicantState state = g_supplicant_interface_get_state(interface);
2457 wifi = g_supplicant_interface_get_data(interface);
2459 DBG("wifi %p interface state %d", wifi, state);
2464 device = wifi->device;
2468 if (state == G_SUPPLICANT_STATE_COMPLETED) {
2469 if (wifi->tethering_param) {
2470 g_free(wifi->tethering_param->ssid);
2471 g_free(wifi->tethering_param);
2472 wifi->tethering_param = NULL;
2475 if (wifi->tethering)
2476 stop_autoscan(device);
2479 if (g_supplicant_interface_get_ready(interface) &&
2480 !wifi->interface_ready) {
2481 wifi->interface_ready = true;
2482 finalize_interface_creation(wifi);
2485 network = wifi->network;
2490 case G_SUPPLICANT_STATE_SCANNING:
2491 if (wifi->connected)
2492 connman_network_set_connected(network, false);
2496 case G_SUPPLICANT_STATE_AUTHENTICATING:
2497 case G_SUPPLICANT_STATE_ASSOCIATING:
2498 stop_autoscan(device);
2500 if (!wifi->connected)
2501 connman_network_set_associating(network, true);
2505 case G_SUPPLICANT_STATE_COMPLETED:
2506 /* though it should be already stopped: */
2507 stop_autoscan(device);
2509 if (!handle_wps_completion(interface, network, device, wifi))
2512 connman_network_set_connected(network, true);
2514 wifi->disconnect_code = 0;
2515 wifi->assoc_code = 0;
2516 wifi->load_shaping_retries = 0;
2519 case G_SUPPLICANT_STATE_DISCONNECTED:
2521 * If we're in one of the idle modes, we have
2522 * not started association yet and thus setting
2523 * those ones to FALSE could cancel an association
2526 wps = connman_network_get_bool(network, "WiFi.UseWPS");
2528 if (is_idle_wps(interface, wifi))
2534 if (handle_assoc_status_code(interface, wifi))
2537 /* If previous state was 4way-handshake, then
2538 * it's either: psk was incorrect and thus we retry
2539 * or if we reach the maximum retries we declare the
2541 if (handle_4way_handshake_failure(interface,
2545 /* See table 8-36 Reason codes in IEEE Std 802.11 */
2546 switch (wifi->disconnect_code) {
2547 case 1: /* Unspecified reason */
2548 /* Let's assume it's because we got blocked */
2550 case 6: /* Class 2 frame received from nonauthenticated STA */
2551 connman_network_set_error(network,
2552 CONNMAN_NETWORK_ERROR_BLOCKED);
2559 if (network != wifi->pending_network) {
2560 connman_network_set_connected(network, false);
2561 connman_network_set_associating(network, false);
2563 wifi->disconnecting = false;
2565 start_autoscan(device);
2569 case G_SUPPLICANT_STATE_INACTIVE:
2570 connman_network_set_associating(network, false);
2571 start_autoscan(device);
2575 case G_SUPPLICANT_STATE_UNKNOWN:
2576 case G_SUPPLICANT_STATE_DISABLED:
2577 case G_SUPPLICANT_STATE_ASSOCIATED:
2578 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
2579 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
2583 old_connected = wifi->connected;
2584 wifi->state = state;
2586 /* Saving wpa_s state policy:
2587 * If connected and if the state changes are roaming related:
2588 * --> We stay connected
2590 * --> We are connected
2592 * --> We are not connected
2595 case G_SUPPLICANT_STATE_AUTHENTICATING:
2596 case G_SUPPLICANT_STATE_ASSOCIATING:
2597 case G_SUPPLICANT_STATE_ASSOCIATED:
2598 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
2599 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
2600 if (wifi->connected)
2601 connman_warn("Probably roaming right now!"
2602 " Staying connected...");
2604 case G_SUPPLICANT_STATE_SCANNING:
2605 wifi->connected = false;
2608 start_autoscan(device);
2610 case G_SUPPLICANT_STATE_COMPLETED:
2611 wifi->connected = true;
2614 wifi->connected = false;
2621 static void interface_removed(GSupplicantInterface *interface)
2623 const char *ifname = g_supplicant_interface_get_ifname(interface);
2624 struct wifi_data *wifi;
2626 DBG("ifname %s", ifname);
2628 wifi = g_supplicant_interface_get_data(interface);
2631 wifi->interface = NULL;
2633 if (wifi && wifi->tethering)
2636 if (!wifi || !wifi->device) {
2637 DBG("wifi interface already removed");
2641 connman_device_set_powered(wifi->device, false);
2643 check_p2p_technology();
2646 static void set_device_type(const char *type, char dev_type[17])
2648 const char *oui = "0050F204";
2649 const char *category = "0001";
2650 const char *sub_category = "0000";
2652 if (!g_strcmp0(type, "handset")) {
2654 sub_category = "0005";
2655 } else if (!g_strcmp0(type, "vm") || !g_strcmp0(type, "container"))
2656 sub_category = "0001";
2657 else if (!g_strcmp0(type, "server"))
2658 sub_category = "0002";
2659 else if (!g_strcmp0(type, "laptop"))
2660 sub_category = "0005";
2661 else if (!g_strcmp0(type, "desktop"))
2662 sub_category = "0006";
2663 else if (!g_strcmp0(type, "tablet"))
2664 sub_category = "0009";
2665 else if (!g_strcmp0(type, "watch"))
2668 snprintf(dev_type, 17, "%s%s%s", category, oui, sub_category);
2671 static void p2p_support(GSupplicantInterface *interface)
2673 char dev_type[17] = {};
2674 const char *hostname;
2681 if (!g_supplicant_interface_has_p2p(interface))
2684 if (connman_technology_driver_register(&p2p_tech_driver) < 0) {
2685 DBG("Could not register P2P technology driver");
2689 hostname = connman_utsname_get_hostname();
2691 hostname = "ConnMan";
2693 set_device_type(connman_machine_get_type(), dev_type);
2694 g_supplicant_interface_set_p2p_device_config(interface,
2695 hostname, dev_type);
2696 connman_peer_driver_register(&peer_driver);
2699 static void scan_started(GSupplicantInterface *interface)
2704 static void scan_finished(GSupplicantInterface *interface)
2709 static void ap_create_fail(GSupplicantInterface *interface)
2711 struct wifi_data *wifi = g_supplicant_interface_get_data(interface);
2714 if ((wifi->tethering) && (wifi->tethering_param)) {
2715 DBG("%s create AP fail \n",
2716 g_supplicant_interface_get_ifname(wifi->interface));
2718 connman_inet_remove_from_bridge(wifi->index, wifi->bridge);
2719 wifi->ap_supported = WIFI_AP_NOT_SUPPORTED;
2720 wifi->tethering = false;
2722 ret = tech_set_tethering(wifi->tethering_param->technology,
2723 wifi->tethering_param->ssid->ssid,
2724 wifi->tethering_param->ssid->passphrase,
2725 wifi->bridge, true);
2727 if ((ret == -EOPNOTSUPP) && (wifi_technology)) {
2728 connman_technology_tethering_notify(wifi_technology,false);
2731 g_free(wifi->tethering_param->ssid);
2732 g_free(wifi->tethering_param);
2733 wifi->tethering_param = NULL;
2737 static unsigned char calculate_strength(GSupplicantNetwork *supplicant_network)
2739 unsigned char strength;
2741 strength = 120 + g_supplicant_network_get_signal(supplicant_network);
2748 static void network_added(GSupplicantNetwork *supplicant_network)
2750 struct connman_network *network;
2751 GSupplicantInterface *interface;
2752 struct wifi_data *wifi;
2753 const char *name, *identifier, *security, *group, *mode;
2754 const unsigned char *ssid;
2755 unsigned int ssid_len;
2759 bool wps_advertizing;
2761 mode = g_supplicant_network_get_mode(supplicant_network);
2762 identifier = g_supplicant_network_get_identifier(supplicant_network);
2764 DBG("%s", identifier);
2766 if (!g_strcmp0(mode, "adhoc"))
2769 interface = g_supplicant_network_get_interface(supplicant_network);
2770 wifi = g_supplicant_interface_get_data(interface);
2771 name = g_supplicant_network_get_name(supplicant_network);
2772 security = g_supplicant_network_get_security(supplicant_network);
2773 group = g_supplicant_network_get_identifier(supplicant_network);
2774 wps = g_supplicant_network_get_wps(supplicant_network);
2775 wps_pbc = g_supplicant_network_is_wps_pbc(supplicant_network);
2776 wps_ready = g_supplicant_network_is_wps_active(supplicant_network);
2777 wps_advertizing = g_supplicant_network_is_wps_advertizing(
2778 supplicant_network);
2783 ssid = g_supplicant_network_get_ssid(supplicant_network, &ssid_len);
2785 network = connman_device_get_network(wifi->device, identifier);
2788 network = connman_network_create(identifier,
2789 CONNMAN_NETWORK_TYPE_WIFI);
2793 connman_network_set_index(network, wifi->index);
2795 if (connman_device_add_network(wifi->device, network) < 0) {
2796 connman_network_unref(network);
2800 wifi->networks = g_slist_prepend(wifi->networks, network);
2803 if (name && name[0] != '\0')
2804 connman_network_set_name(network, name);
2806 connman_network_set_blob(network, "WiFi.SSID",
2808 connman_network_set_string(network, "WiFi.Security", security);
2809 connman_network_set_strength(network,
2810 calculate_strength(supplicant_network));
2811 connman_network_set_bool(network, "WiFi.WPS", wps);
2812 connman_network_set_bool(network, "WiFi.WPSAdvertising",
2816 /* Is AP advertizing for WPS association?
2817 * If so, we decide to use WPS by default */
2818 if (wps_ready && wps_pbc &&
2820 connman_network_set_bool(network, "WiFi.UseWPS", true);
2823 connman_network_set_frequency(network,
2824 g_supplicant_network_get_frequency(supplicant_network));
2826 connman_network_set_available(network, true);
2827 connman_network_set_string(network, "WiFi.Mode", mode);
2830 connman_network_set_group(network, group);
2832 if (wifi->hidden && ssid) {
2833 if (!g_strcmp0(wifi->hidden->security, security) &&
2834 wifi->hidden->ssid_len == ssid_len &&
2835 !memcmp(wifi->hidden->ssid, ssid, ssid_len)) {
2836 connman_network_connect_hidden(network,
2837 wifi->hidden->identity,
2838 wifi->hidden->passphrase,
2839 wifi->hidden->user_data);
2840 wifi->hidden->user_data = NULL;
2841 hidden_free(wifi->hidden);
2842 wifi->hidden = NULL;
2847 static void network_removed(GSupplicantNetwork *network)
2849 GSupplicantInterface *interface;
2850 struct wifi_data *wifi;
2851 const char *name, *identifier;
2852 struct connman_network *connman_network;
2854 interface = g_supplicant_network_get_interface(network);
2855 wifi = g_supplicant_interface_get_data(interface);
2856 identifier = g_supplicant_network_get_identifier(network);
2857 name = g_supplicant_network_get_name(network);
2859 DBG("name %s", name);
2864 connman_network = connman_device_get_network(wifi->device, identifier);
2865 if (!connman_network)
2868 wifi->networks = g_slist_remove(wifi->networks, connman_network);
2870 connman_device_remove_network(wifi->device, connman_network);
2871 connman_network_unref(connman_network);
2874 static void network_changed(GSupplicantNetwork *network, const char *property)
2876 GSupplicantInterface *interface;
2877 struct wifi_data *wifi;
2878 const char *name, *identifier;
2879 struct connman_network *connman_network;
2882 interface = g_supplicant_network_get_interface(network);
2883 wifi = g_supplicant_interface_get_data(interface);
2884 identifier = g_supplicant_network_get_identifier(network);
2885 name = g_supplicant_network_get_name(network);
2887 DBG("name %s", name);
2892 connman_network = connman_device_get_network(wifi->device, identifier);
2893 if (!connman_network)
2896 if (g_str_equal(property, "WPSCapabilities")) {
2900 bool wps_advertizing;
2902 wps = g_supplicant_network_get_wps(network);
2903 wps_pbc = g_supplicant_network_is_wps_pbc(network);
2904 wps_ready = g_supplicant_network_is_wps_active(network);
2906 g_supplicant_network_is_wps_advertizing(network);
2908 connman_network_set_bool(connman_network, "WiFi.WPS", wps);
2909 connman_network_set_bool(connman_network,
2910 "WiFi.WPSAdvertising", wps_advertizing);
2914 * Is AP advertizing for WPS association?
2915 * If so, we decide to use WPS by default
2917 if (wps_ready && wps_pbc && wps_advertizing)
2918 connman_network_set_bool(connman_network,
2919 "WiFi.UseWPS", true);
2922 update_needed = true;
2923 } else if (g_str_equal(property, "Signal")) {
2924 connman_network_set_strength(connman_network,
2925 calculate_strength(network));
2926 update_needed = true;
2928 update_needed = false;
2931 connman_network_update(connman_network);
2934 static void network_associated(GSupplicantNetwork *network)
2936 GSupplicantInterface *interface;
2937 struct wifi_data *wifi;
2938 struct connman_network *connman_network;
2939 const char *identifier;
2943 interface = g_supplicant_network_get_interface(network);
2947 wifi = g_supplicant_interface_get_data(interface);
2951 /* P2P networks must not be treated as WiFi networks */
2952 if (wifi->p2p_connecting || wifi->p2p_device)
2955 identifier = g_supplicant_network_get_identifier(network);
2957 connman_network = connman_device_get_network(wifi->device, identifier);
2958 if (!connman_network)
2961 if (wifi->network) {
2962 if (wifi->network == connman_network)
2966 * This should never happen, we got associated with
2967 * a network different than the one we were expecting.
2969 DBG("Associated to %p while expecting %p",
2970 connman_network, wifi->network);
2972 connman_network_set_associating(wifi->network, false);
2975 DBG("Reconnecting to previous network %p from wpa_s", connman_network);
2977 wifi->network = connman_network_ref(connman_network);
2981 * Interface state changes callback (interface_state) is always
2982 * called before network_associated callback thus we need to call
2983 * interface_state again in order to process the new state now that
2984 * we have the network properly set.
2986 interface_state(interface);
2989 static void sta_authorized(GSupplicantInterface *interface,
2992 struct wifi_data *wifi = g_supplicant_interface_get_data(interface);
2994 DBG("wifi %p station %s authorized", wifi, addr);
2996 if (!wifi || !wifi->tethering)
2999 __connman_tethering_client_register(addr);
3002 static void sta_deauthorized(GSupplicantInterface *interface,
3005 struct wifi_data *wifi = g_supplicant_interface_get_data(interface);
3007 DBG("wifi %p station %s deauthorized", wifi, addr);
3009 if (!wifi || !wifi->tethering)
3012 __connman_tethering_client_unregister(addr);
3015 static void apply_peer_services(GSupplicantPeer *peer,
3016 struct connman_peer *connman_peer)
3018 const unsigned char *data;
3023 connman_peer_reset_services(connman_peer);
3025 data = g_supplicant_peer_get_widi_ies(peer, &length);
3027 connman_peer_add_service(connman_peer,
3028 CONNMAN_PEER_SERVICE_WIFI_DISPLAY, data, length);
3032 static void peer_found(GSupplicantPeer *peer)
3034 GSupplicantInterface *iface = g_supplicant_peer_get_interface(peer);
3035 struct wifi_data *wifi = g_supplicant_interface_get_data(iface);
3036 struct connman_peer *connman_peer;
3037 const char *identifier, *name;
3040 identifier = g_supplicant_peer_get_identifier(peer);
3041 name = g_supplicant_peer_get_name(peer);
3043 DBG("ident: %s", identifier);
3045 connman_peer = connman_peer_get(wifi->device, identifier);
3049 connman_peer = connman_peer_create(identifier);
3050 connman_peer_set_name(connman_peer, name);
3051 connman_peer_set_device(connman_peer, wifi->device);
3052 apply_peer_services(peer, connman_peer);
3054 ret = connman_peer_register(connman_peer);
3055 if (ret < 0 && ret != -EALREADY)
3056 connman_peer_unref(connman_peer);
3058 wifi->peers = g_slist_prepend(wifi->peers, connman_peer);
3061 static void peer_lost(GSupplicantPeer *peer)
3063 GSupplicantInterface *iface = g_supplicant_peer_get_interface(peer);
3064 struct wifi_data *wifi = g_supplicant_interface_get_data(iface);
3065 struct connman_peer *connman_peer;
3066 const char *identifier;
3071 identifier = g_supplicant_peer_get_identifier(peer);
3073 DBG("ident: %s", identifier);
3075 connman_peer = connman_peer_get(wifi->device, identifier);
3077 if (wifi->p2p_connecting &&
3078 wifi->pending_peer == connman_peer) {
3079 peer_connect_timeout(wifi);
3081 connman_peer_unregister(connman_peer);
3082 connman_peer_unref(connman_peer);
3085 wifi->peers = g_slist_remove(wifi->peers, connman_peer);
3088 static void peer_changed(GSupplicantPeer *peer, GSupplicantPeerState state)
3090 GSupplicantInterface *iface = g_supplicant_peer_get_interface(peer);
3091 struct wifi_data *wifi = g_supplicant_interface_get_data(iface);
3092 enum connman_peer_state p_state = CONNMAN_PEER_STATE_UNKNOWN;
3093 struct connman_peer *connman_peer;
3094 const char *identifier;
3096 identifier = g_supplicant_peer_get_identifier(peer);
3098 DBG("ident: %s", identifier);
3103 connman_peer = connman_peer_get(wifi->device, identifier);
3108 case G_SUPPLICANT_PEER_SERVICES_CHANGED:
3109 apply_peer_services(peer, connman_peer);
3110 connman_peer_services_changed(connman_peer);
3112 case G_SUPPLICANT_PEER_GROUP_CHANGED:
3113 if (!g_supplicant_peer_is_in_a_group(peer))
3114 p_state = CONNMAN_PEER_STATE_IDLE;
3116 p_state = CONNMAN_PEER_STATE_CONFIGURATION;
3118 case G_SUPPLICANT_PEER_GROUP_STARTED:
3120 case G_SUPPLICANT_PEER_GROUP_FINISHED:
3121 p_state = CONNMAN_PEER_STATE_IDLE;
3123 case G_SUPPLICANT_PEER_GROUP_JOINED:
3124 connman_peer_set_iface_address(connman_peer,
3125 g_supplicant_peer_get_iface_address(peer));
3127 case G_SUPPLICANT_PEER_GROUP_DISCONNECTED:
3128 p_state = CONNMAN_PEER_STATE_IDLE;
3130 case G_SUPPLICANT_PEER_GROUP_FAILED:
3131 if (g_supplicant_peer_has_requested_connection(peer))
3132 p_state = CONNMAN_PEER_STATE_IDLE;
3134 p_state = CONNMAN_PEER_STATE_FAILURE;
3138 if (p_state == CONNMAN_PEER_STATE_CONFIGURATION ||
3139 p_state == CONNMAN_PEER_STATE_FAILURE) {
3140 if (wifi->p2p_connecting
3141 && connman_peer == wifi->pending_peer)
3142 peer_cancel_timeout(wifi);
3144 p_state = CONNMAN_PEER_STATE_UNKNOWN;
3147 if (p_state == CONNMAN_PEER_STATE_UNKNOWN)
3150 if (p_state == CONNMAN_PEER_STATE_CONFIGURATION) {
3151 GSupplicantInterface *g_iface;
3152 struct wifi_data *g_wifi;
3154 g_iface = g_supplicant_peer_get_group_interface(peer);
3158 g_wifi = g_supplicant_interface_get_data(g_iface);
3162 connman_peer_set_as_master(connman_peer,
3163 !g_supplicant_peer_is_client(peer));
3164 connman_peer_set_sub_device(connman_peer, g_wifi->device);
3167 * If wpa_supplicant didn't create a dedicated p2p-group
3168 * interface then mark this interface as p2p_device to avoid
3169 * scan and auto-scan are launched on it while P2P is connected.
3171 if (!g_list_find(p2p_iface_list, g_wifi))
3172 wifi->p2p_device = true;
3175 connman_peer_set_state(connman_peer, p_state);
3178 static void peer_request(GSupplicantPeer *peer)
3180 GSupplicantInterface *iface = g_supplicant_peer_get_interface(peer);
3181 struct wifi_data *wifi = g_supplicant_interface_get_data(iface);
3182 struct connman_peer *connman_peer;
3183 const char *identifier;
3185 identifier = g_supplicant_peer_get_identifier(peer);
3187 DBG("ident: %s", identifier);
3189 connman_peer = connman_peer_get(wifi->device, identifier);
3193 connman_peer_request_connection(connman_peer);
3196 static void debug(const char *str)
3198 if (getenv("CONNMAN_SUPPLICANT_DEBUG"))
3199 connman_debug("%s", str);
3202 static void disconnect_reasoncode(GSupplicantInterface *interface,
3205 struct wifi_data *wifi = g_supplicant_interface_get_data(interface);
3208 wifi->disconnect_code = reasoncode;
3212 static void assoc_status_code(GSupplicantInterface *interface, int status_code)
3214 struct wifi_data *wifi = g_supplicant_interface_get_data(interface);
3217 wifi->assoc_code = status_code;
3221 static const GSupplicantCallbacks callbacks = {
3222 .system_ready = system_ready,
3223 .system_killed = system_killed,
3224 .interface_added = interface_added,
3225 .interface_state = interface_state,
3226 .interface_removed = interface_removed,
3227 .p2p_support = p2p_support,
3228 .scan_started = scan_started,
3229 .scan_finished = scan_finished,
3230 .ap_create_fail = ap_create_fail,
3231 .network_added = network_added,
3232 .network_removed = network_removed,
3233 .network_changed = network_changed,
3234 .network_associated = network_associated,
3235 .sta_authorized = sta_authorized,
3236 .sta_deauthorized = sta_deauthorized,
3237 .peer_found = peer_found,
3238 .peer_lost = peer_lost,
3239 .peer_changed = peer_changed,
3240 .peer_request = peer_request,
3242 .disconnect_reasoncode = disconnect_reasoncode,
3243 .assoc_status_code = assoc_status_code,
3247 static int tech_probe(struct connman_technology *technology)
3249 wifi_technology = technology;
3254 static void tech_remove(struct connman_technology *technology)
3256 wifi_technology = NULL;
3259 static GSupplicantSSID *ssid_ap_init(const char *ssid, const char *passphrase)
3261 GSupplicantSSID *ap;
3263 ap = g_try_malloc0(sizeof(GSupplicantSSID));
3267 ap->mode = G_SUPPLICANT_MODE_MASTER;
3269 ap->ssid_len = strlen(ssid);
3273 if (!passphrase || strlen(passphrase) == 0) {
3274 ap->security = G_SUPPLICANT_SECURITY_NONE;
3275 ap->passphrase = NULL;
3277 ap->security = G_SUPPLICANT_SECURITY_PSK;
3278 ap->protocol = G_SUPPLICANT_PROTO_RSN;
3279 ap->pairwise_cipher = G_SUPPLICANT_PAIRWISE_CCMP;
3280 ap->group_cipher = G_SUPPLICANT_GROUP_CCMP;
3281 ap->passphrase = passphrase;
3287 static void ap_start_callback(int result, GSupplicantInterface *interface,
3290 struct wifi_tethering_info *info = user_data;
3292 DBG("result %d index %d bridge %s",
3293 result, info->wifi->index, info->wifi->bridge);
3295 if ((result < 0) || (info->wifi->ap_supported != WIFI_AP_SUPPORTED)) {
3296 connman_inet_remove_from_bridge(info->wifi->index,
3297 info->wifi->bridge);
3299 if (info->wifi->ap_supported == WIFI_AP_SUPPORTED) {
3300 connman_technology_tethering_notify(info->technology, false);
3301 g_free(info->wifi->tethering_param->ssid);
3302 g_free(info->wifi->tethering_param);
3303 info->wifi->tethering_param = NULL;
3307 g_free(info->ifname);
3311 static void ap_create_callback(int result,
3312 GSupplicantInterface *interface,
3315 struct wifi_tethering_info *info = user_data;
3317 DBG("result %d ifname %s", result,
3318 g_supplicant_interface_get_ifname(interface));
3320 if ((result < 0) || (info->wifi->ap_supported != WIFI_AP_SUPPORTED)) {
3321 connman_inet_remove_from_bridge(info->wifi->index,
3322 info->wifi->bridge);
3324 if (info->wifi->ap_supported == WIFI_AP_SUPPORTED) {
3325 connman_technology_tethering_notify(info->technology, false);
3326 g_free(info->wifi->tethering_param->ssid);
3327 g_free(info->wifi->tethering_param);
3328 info->wifi->tethering_param = NULL;
3332 g_free(info->ifname);
3338 info->wifi->interface = interface;
3339 g_supplicant_interface_set_data(interface, info->wifi);
3341 if (g_supplicant_interface_set_apscan(interface, 2) < 0)
3342 connman_error("Failed to set interface ap_scan property");
3344 g_supplicant_interface_connect(interface, info->ssid,
3345 ap_start_callback, info);
3348 static void sta_remove_callback(int result,
3349 GSupplicantInterface *interface,
3352 struct wifi_tethering_info *info = user_data;
3353 const char *driver = connman_option_get_string("wifi");
3355 DBG("ifname %s result %d ", info->ifname, result);
3357 if ((result < 0) || (info->wifi->ap_supported != WIFI_AP_SUPPORTED)) {
3358 info->wifi->tethering = false;
3359 connman_technology_tethering_notify(info->technology, false);
3361 if (info->wifi->ap_supported == WIFI_AP_SUPPORTED) {
3362 g_free(info->wifi->tethering_param->ssid);
3363 g_free(info->wifi->tethering_param);
3364 info->wifi->tethering_param = NULL;
3367 g_free(info->ifname);
3373 info->wifi->interface = NULL;
3375 g_supplicant_interface_create(info->ifname, driver, info->wifi->bridge,
3380 static int enable_wifi_tethering(struct connman_technology *technology,
3381 const char *bridge, const char *identifier,
3382 const char *passphrase, bool available)
3385 GSupplicantInterface *interface;
3386 struct wifi_data *wifi;
3387 struct wifi_tethering_info *info;
3392 for (list = iface_list; list; list = list->next) {
3395 DBG("wifi %p network %p pending_network %p", wifi,
3396 wifi->network, wifi->pending_network);
3398 interface = wifi->interface;
3403 ifname = g_supplicant_interface_get_ifname(wifi->interface);
3407 if (wifi->ap_supported == WIFI_AP_NOT_SUPPORTED) {
3408 DBG("%s does not support AP mode (detected)", ifname);
3412 mode = g_supplicant_interface_get_mode(interface);
3413 if ((mode & G_SUPPLICANT_CAPABILITY_MODE_AP) == 0) {
3414 wifi->ap_supported = WIFI_AP_NOT_SUPPORTED;
3415 DBG("%s does not support AP mode (capability)", ifname);
3419 if (wifi->network && available)
3422 info = g_try_malloc0(sizeof(struct wifi_tethering_info));
3426 wifi->tethering_param = g_try_malloc0(sizeof(struct wifi_tethering_info));
3427 if (!wifi->tethering_param) {
3433 info->technology = technology;
3434 info->wifi->bridge = bridge;
3435 info->ssid = ssid_ap_init(identifier, passphrase);
3439 info->ifname = g_strdup(ifname);
3441 wifi->tethering_param->technology = technology;
3442 wifi->tethering_param->ssid = ssid_ap_init(identifier, passphrase);
3443 if (!wifi->tethering_param->ssid)
3446 info->wifi->tethering = true;
3447 info->wifi->ap_supported = WIFI_AP_SUPPORTED;
3449 berr = connman_technology_tethering_notify(technology, true);
3453 err = g_supplicant_interface_remove(interface,
3454 sta_remove_callback,
3457 DBG("tethering wifi %p ifname %s", wifi, ifname);
3462 g_free(info->ifname);
3465 g_free(wifi->tethering_param);
3466 wifi->tethering_param = NULL;
3469 * Remove bridge if it was correctly created but remove
3470 * operation failed. Instead, if bridge creation failed then
3471 * break out and do not try again on another interface,
3472 * bridge set-up does not depend on it.
3475 connman_technology_tethering_notify(technology, false);
3483 static int tech_set_tethering(struct connman_technology *technology,
3484 const char *identifier, const char *passphrase,
3485 const char *bridge, bool enabled)
3488 struct wifi_data *wifi;
3494 for (list = iface_list; list; list = list->next) {
3497 if (wifi->tethering) {
3498 wifi->tethering = false;
3500 connman_inet_remove_from_bridge(wifi->index,
3502 wifi->bridged = false;
3506 connman_technology_tethering_notify(technology, false);
3511 DBG("trying tethering for available devices");
3512 err = enable_wifi_tethering(technology, bridge, identifier, passphrase,
3516 DBG("trying tethering for any device");
3517 err = enable_wifi_tethering(technology, bridge, identifier,
3524 static void regdom_callback(int result, const char *alpha2, void *user_data)
3528 if (!wifi_technology)
3534 connman_technology_regdom_notify(wifi_technology, alpha2);
3537 static int tech_set_regdom(struct connman_technology *technology, const char *alpha2)
3539 return g_supplicant_set_country(alpha2, regdom_callback, NULL);
3542 static struct connman_technology_driver tech_driver = {
3544 .type = CONNMAN_SERVICE_TYPE_WIFI,
3545 .probe = tech_probe,
3546 .remove = tech_remove,
3547 .set_tethering = tech_set_tethering,
3548 .set_regdom = tech_set_regdom,
3551 static int wifi_init(void)
3555 err = connman_network_driver_register(&network_driver);
3559 err = g_supplicant_register(&callbacks);
3561 connman_network_driver_unregister(&network_driver);
3565 err = connman_technology_driver_register(&tech_driver);
3567 g_supplicant_unregister(&callbacks);
3568 connman_network_driver_unregister(&network_driver);
3575 static void wifi_exit(void)
3579 connman_technology_driver_unregister(&tech_driver);
3581 g_supplicant_unregister(&callbacks);
3583 connman_network_driver_unregister(&network_driver);
3586 CONNMAN_PLUGIN_DEFINE(wifi, "WiFi interface plugin", VERSION,
3587 CONNMAN_PLUGIN_PRIORITY_DEFAULT, wifi_init, wifi_exit)