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 <linux/if_arp.h>
34 #include <linux/wireless.h>
35 #include <net/ethernet.h>
38 #define IFF_LOWER_UP 0x10000
41 #include <dbus/dbus.h>
44 #define CONNMAN_API_SUBJECT_TO_CHANGE
45 #include <connman/plugin.h>
46 #include <connman/inet.h>
47 #include <connman/device.h>
48 #include <connman/rtnl.h>
49 #include <connman/technology.h>
50 #include <connman/service.h>
51 #include <connman/peer.h>
52 #include <connman/log.h>
53 #include <connman/option.h>
54 #include <connman/storage.h>
55 #include <include/setting.h>
56 #include <connman/provision.h>
57 #include <connman/utsname.h>
58 #include <connman/machine.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_DEFAULT "exponential:3:300"
69 #define P2P_FIND_TIMEOUT 30
70 #define P2P_CONNECTION_TIMEOUT 100
71 #define P2P_LISTEN_PERIOD 500
72 #define P2P_LISTEN_INTERVAL 2000
74 #define ASSOC_STATUS_NO_CLIENT 17
75 #define LOAD_SHAPING_MAX_RETRIES 3
77 static struct connman_technology *wifi_technology = NULL;
78 static struct connman_technology *p2p_technology = NULL;
80 enum wifi_ap_capability{
82 WIFI_AP_SUPPORTED = 1,
83 WIFI_AP_NOT_SUPPORTED = 2,
86 struct hidden_params {
88 unsigned int ssid_len;
90 char *anonymous_identity;
92 char *altsubject_match;
93 char *domain_suffix_match;
97 GSupplicantScanParams *scan_params;
102 * Used for autoscan "emulation".
103 * Should be removed when wpa_s autoscan support will be by default.
105 struct autoscan_params {
109 unsigned int timeout;
112 struct wifi_tethering_info {
113 struct wifi_data *wifi;
114 struct connman_technology *technology;
116 GSupplicantSSID *ssid;
121 struct connman_device *device;
122 struct connman_network *network;
123 struct connman_network *pending_network;
125 GSupplicantInterface *interface;
126 GSupplicantState state;
130 enum wifi_ap_capability ap_supported;
132 bool interface_ready;
138 int load_shaping_retries;
139 struct hidden_params *hidden;
140 bool postpone_hidden;
141 struct wifi_tethering_info *tethering_param;
143 * autoscan "emulation".
145 struct autoscan_params *autoscan;
147 GSupplicantScanParams *scan_params;
148 unsigned int p2p_find_timeout;
149 unsigned int p2p_connection_timeout;
150 struct connman_peer *pending_peer;
159 static GList *iface_list = NULL;
161 static GList *pending_wifi_device = NULL;
162 static GList *p2p_iface_list = NULL;
163 bool wfd_service_registered = false;
165 static void start_autoscan(struct connman_device *device);
166 static int tech_set_tethering(struct connman_technology *technology,
167 const char *identifier, const char *passphrase,
168 const char *bridge, bool enabled);
170 static int p2p_tech_probe(struct connman_technology *technology)
172 p2p_technology = technology;
177 static void p2p_tech_remove(struct connman_technology *technology)
179 p2p_technology = NULL;
182 static struct connman_technology_driver p2p_tech_driver = {
184 .type = CONNMAN_SERVICE_TYPE_P2P,
185 .probe = p2p_tech_probe,
186 .remove = p2p_tech_remove,
189 static bool is_p2p_connecting(void)
193 for (list = iface_list; list; list = list->next) {
194 struct wifi_data *wifi = list->data;
196 if (wifi->p2p_connecting)
203 static void add_pending_wifi_device(struct wifi_data *wifi)
205 if (g_list_find(pending_wifi_device, wifi))
208 pending_wifi_device = g_list_append(pending_wifi_device, wifi);
211 static struct wifi_data *get_pending_wifi_data(const char *ifname)
215 for (list = pending_wifi_device; list; list = list->next) {
216 struct wifi_data *wifi;
217 const char *dev_name;
220 if (!wifi || !wifi->device)
223 dev_name = connman_device_get_string(wifi->device, "Interface");
224 if (!g_strcmp0(ifname, dev_name)) {
225 pending_wifi_device = g_list_delete_link(
226 pending_wifi_device, list);
234 static void remove_pending_wifi_device(struct wifi_data *wifi)
238 link = g_list_find(pending_wifi_device, wifi);
243 pending_wifi_device = g_list_delete_link(pending_wifi_device, link);
246 static void peer_cancel_timeout(struct wifi_data *wifi)
248 if (wifi->p2p_connection_timeout > 0)
249 g_source_remove(wifi->p2p_connection_timeout);
251 wifi->p2p_connection_timeout = 0;
252 wifi->p2p_connecting = false;
254 if (wifi->pending_peer) {
255 connman_peer_unref(wifi->pending_peer);
256 wifi->pending_peer = NULL;
260 static gboolean peer_connect_timeout(gpointer data)
262 struct wifi_data *wifi = data;
266 if (wifi->p2p_connecting) {
267 enum connman_peer_state state = CONNMAN_PEER_STATE_FAILURE;
268 GSupplicantPeer *gs_peer =
269 g_supplicant_interface_peer_lookup(wifi->interface,
270 connman_peer_get_identifier(wifi->pending_peer));
272 if (g_supplicant_peer_has_requested_connection(gs_peer))
273 state = CONNMAN_PEER_STATE_IDLE;
275 connman_peer_set_state(wifi->pending_peer, state);
278 peer_cancel_timeout(wifi);
283 static void peer_connect_callback(int result, GSupplicantInterface *interface,
286 struct wifi_data *wifi = user_data;
287 struct connman_peer *peer = wifi->pending_peer;
289 DBG("peer %p - %d", peer, result);
295 peer_connect_timeout(wifi);
299 connman_peer_set_state(peer, CONNMAN_PEER_STATE_ASSOCIATION);
301 wifi->p2p_connection_timeout = g_timeout_add_seconds(
302 P2P_CONNECTION_TIMEOUT,
303 peer_connect_timeout, wifi);
306 static int peer_connect(struct connman_peer *peer,
307 enum connman_peer_wps_method wps_method,
310 struct connman_device *device = connman_peer_get_device(peer);
311 GSupplicantPeerParams *peer_params;
312 GSupplicantPeer *gs_peer;
313 struct wifi_data *wifi;
317 DBG("peer %p", peer);
322 wifi = connman_device_get_data(device);
323 if (!wifi || !wifi->interface)
326 if (wifi->p2p_connecting)
329 gs_peer = g_supplicant_interface_peer_lookup(wifi->interface,
330 connman_peer_get_identifier(peer));
334 pbc = g_supplicant_peer_is_wps_pbc(gs_peer);
335 pin = g_supplicant_peer_is_wps_pin(gs_peer);
337 switch (wps_method) {
338 case CONNMAN_PEER_WPS_UNKNOWN:
339 if ((pbc && pin) || pin)
342 case CONNMAN_PEER_WPS_PBC:
347 case CONNMAN_PEER_WPS_PIN:
348 if (!pin || !wps_pin)
353 peer_params = g_try_malloc0(sizeof(GSupplicantPeerParams));
357 peer_params->path = g_strdup(g_supplicant_peer_get_path(gs_peer));
359 peer_params->wps_pin = g_strdup(wps_pin);
361 peer_params->master = connman_peer_service_is_master();
363 ret = g_supplicant_interface_p2p_connect(wifi->interface, peer_params,
364 peer_connect_callback, wifi);
365 if (ret == -EINPROGRESS) {
366 wifi->pending_peer = connman_peer_ref(peer);
367 wifi->p2p_connecting = true;
368 } else if (ret < 0) {
369 g_free(peer_params->path);
370 g_free(peer_params->wps_pin);
377 static int peer_disconnect(struct connman_peer *peer)
379 struct connman_device *device = connman_peer_get_device(peer);
380 GSupplicantPeerParams peer_params = {};
381 GSupplicantPeer *gs_peer;
382 struct wifi_data *wifi;
385 DBG("peer %p", peer);
390 wifi = connman_device_get_data(device);
394 gs_peer = g_supplicant_interface_peer_lookup(wifi->interface,
395 connman_peer_get_identifier(peer));
399 peer_params.path = g_strdup(g_supplicant_peer_get_path(gs_peer));
401 ret = g_supplicant_interface_p2p_disconnect(wifi->interface,
403 g_free(peer_params.path);
405 if (ret == -EINPROGRESS) {
406 peer_cancel_timeout(wifi);
407 wifi->p2p_device = false;
413 struct peer_service_registration {
414 peer_service_registration_cb_t callback;
418 static bool is_service_wfd(const unsigned char *specs, int length)
420 if (length < 9 || specs[0] != 0 || specs[1] != 0 || specs[2] != 6)
426 static void apply_p2p_listen_on_iface(gpointer data, gpointer user_data)
428 struct wifi_data *wifi = data;
430 if (!wifi->interface ||
431 !g_supplicant_interface_has_p2p(wifi->interface))
434 if (!wifi->servicing) {
435 g_supplicant_interface_p2p_listen(wifi->interface,
436 P2P_LISTEN_PERIOD, P2P_LISTEN_INTERVAL);
442 static void register_wfd_service_cb(int result,
443 GSupplicantInterface *iface, void *user_data)
445 struct peer_service_registration *reg_data = user_data;
450 g_list_foreach(iface_list, apply_p2p_listen_on_iface, NULL);
452 if (reg_data && reg_data->callback) {
453 reg_data->callback(result, reg_data->user_data);
458 static GSupplicantP2PServiceParams *fill_in_peer_service_params(
459 const unsigned char *spec,
460 int spec_length, const unsigned char *query,
461 int query_length, int version)
463 GSupplicantP2PServiceParams *params;
465 params = g_try_malloc0(sizeof(GSupplicantP2PServiceParams));
470 params->version = version;
471 params->service = g_memdup(spec, spec_length);
472 } else if (query_length > 0 && spec_length > 0) {
473 params->query = g_memdup(query, query_length);
474 params->query_length = query_length;
476 params->response = g_memdup(spec, spec_length);
477 params->response_length = spec_length;
479 params->wfd_ies = g_memdup(spec, spec_length);
480 params->wfd_ies_length = spec_length;
486 static void free_peer_service_params(GSupplicantP2PServiceParams *params)
491 g_free(params->service);
492 g_free(params->query);
493 g_free(params->response);
494 g_free(params->wfd_ies);
499 static int peer_register_wfd_service(const unsigned char *specification,
500 int specification_length,
501 peer_service_registration_cb_t callback,
504 struct peer_service_registration *reg_data = NULL;
505 static GSupplicantP2PServiceParams *params;
510 if (wfd_service_registered)
513 params = fill_in_peer_service_params(specification,
514 specification_length, NULL, 0, 0);
518 reg_data = g_try_malloc0(sizeof(*reg_data));
524 reg_data->callback = callback;
525 reg_data->user_data = user_data;
527 ret = g_supplicant_set_widi_ies(params,
528 register_wfd_service_cb, reg_data);
529 if (ret < 0 && ret != -EINPROGRESS)
532 wfd_service_registered = true;
536 free_peer_service_params(params);
542 static void register_peer_service_cb(int result,
543 GSupplicantInterface *iface, void *user_data)
545 struct wifi_data *wifi = g_supplicant_interface_get_data(iface);
546 struct peer_service_registration *reg_data = user_data;
551 apply_p2p_listen_on_iface(wifi, NULL);
553 if (reg_data->callback)
554 reg_data->callback(result, reg_data->user_data);
559 static int peer_register_service(const unsigned char *specification,
560 int specification_length,
561 const unsigned char *query,
562 int query_length, int version,
563 peer_service_registration_cb_t callback,
566 struct peer_service_registration *reg_data;
567 GSupplicantP2PServiceParams *params;
574 if (specification && !version && !query &&
575 is_service_wfd(specification, specification_length)) {
576 return peer_register_wfd_service(specification,
577 specification_length, callback, user_data);
580 reg_data = g_try_malloc0(sizeof(*reg_data));
584 reg_data->callback = callback;
585 reg_data->user_data = user_data;
589 for (list = iface_list; list; list = list->next) {
590 struct wifi_data *wifi = list->data;
591 GSupplicantInterface *iface = wifi->interface;
593 if (!g_supplicant_interface_has_p2p(iface))
596 params = fill_in_peer_service_params(specification,
597 specification_length, query,
598 query_length, version);
605 ret_f = g_supplicant_interface_p2p_add_service(iface,
606 register_peer_service_cb, params, reg_data);
607 if (ret_f == 0 || ret_f == -EINPROGRESS)
611 ret = g_supplicant_interface_p2p_add_service(iface,
612 register_peer_service_cb, params, NULL);
613 if (ret != 0 && ret != -EINPROGRESS)
614 free_peer_service_params(params);
617 if (ret_f != 0 && ret_f != -EINPROGRESS)
623 static int peer_unregister_wfd_service(void)
625 GSupplicantP2PServiceParams *params;
628 if (!wfd_service_registered)
631 params = fill_in_peer_service_params(NULL, 0, NULL, 0, 0);
635 wfd_service_registered = false;
637 g_supplicant_set_widi_ies(params, NULL, NULL);
639 for (list = iface_list; list; list = list->next) {
640 struct wifi_data *wifi = list->data;
642 if (!g_supplicant_interface_has_p2p(wifi->interface))
646 if (!wifi->servicing || wifi->servicing < 0) {
647 g_supplicant_interface_p2p_listen(wifi->interface,
656 static int peer_unregister_service(const unsigned char *specification,
657 int specification_length,
658 const unsigned char *query,
659 int query_length, int version)
661 GSupplicantP2PServiceParams *params;
666 if (specification && !version && !query &&
667 is_service_wfd(specification, specification_length)) {
668 ret = peer_unregister_wfd_service();
669 if (ret != 0 && ret != -EINPROGRESS)
674 for (list = iface_list; list; list = list->next) {
675 struct wifi_data *wifi = list->data;
676 GSupplicantInterface *iface = wifi->interface;
681 if (!g_supplicant_interface_has_p2p(iface))
684 params = fill_in_peer_service_params(specification,
685 specification_length, query,
686 query_length, version);
692 ret = g_supplicant_interface_p2p_del_service(iface, params);
693 if (ret != 0 && ret != -EINPROGRESS)
694 free_peer_service_params(params);
697 if (!wifi->servicing || wifi->servicing < 0) {
698 g_supplicant_interface_p2p_listen(iface, 0, 0);
706 static struct connman_peer_driver peer_driver = {
707 .connect = peer_connect,
708 .disconnect = peer_disconnect,
709 .register_service = peer_register_service,
710 .unregister_service = peer_unregister_service,
713 static void handle_tethering(struct wifi_data *wifi)
715 if (!wifi->tethering)
724 DBG("index %d bridge %s", wifi->index, wifi->bridge);
726 if (connman_inet_add_to_bridge(wifi->index, wifi->bridge) < 0)
729 wifi->bridged = true;
732 static void wifi_newlink(unsigned flags, unsigned change, void *user_data)
734 struct connman_device *device = user_data;
735 struct wifi_data *wifi = connman_device_get_data(device);
740 DBG("index %d flags %d change %d", wifi->index, flags, change);
742 if ((wifi->flags & IFF_UP) != (flags & IFF_UP)) {
746 DBG("interface down");
749 if ((wifi->flags & IFF_LOWER_UP) != (flags & IFF_LOWER_UP)) {
750 if (flags & IFF_LOWER_UP) {
753 handle_tethering(wifi);
761 static int wifi_probe(struct connman_device *device)
763 struct wifi_data *wifi;
765 DBG("device %p", device);
767 wifi = g_try_new0(struct wifi_data, 1);
771 wifi->state = G_SUPPLICANT_STATE_INACTIVE;
772 wifi->ap_supported = WIFI_AP_UNKNOWN;
773 wifi->tethering_param = NULL;
775 connman_device_set_data(device, wifi);
776 wifi->device = connman_device_ref(device);
778 wifi->index = connman_device_get_index(device);
781 wifi->watch = connman_rtnl_add_newlink_watch(wifi->index,
782 wifi_newlink, device);
783 if (is_p2p_connecting())
784 add_pending_wifi_device(wifi);
786 iface_list = g_list_append(iface_list, wifi);
791 static void remove_networks(struct connman_device *device,
792 struct wifi_data *wifi)
796 for (list = wifi->networks; list; list = list->next) {
797 struct connman_network *network = list->data;
799 connman_device_remove_network(device, network);
800 connman_network_unref(network);
803 g_slist_free(wifi->networks);
804 wifi->networks = NULL;
807 static void remove_peers(struct wifi_data *wifi)
811 for (list = wifi->peers; list; list = list->next) {
812 struct connman_peer *peer = list->data;
814 connman_peer_unregister(peer);
815 connman_peer_unref(peer);
818 g_slist_free(wifi->peers);
822 static void reset_autoscan(struct connman_device *device)
824 struct wifi_data *wifi = connman_device_get_data(device);
825 struct autoscan_params *autoscan;
829 if (!wifi || !wifi->autoscan)
832 autoscan = wifi->autoscan;
834 if (autoscan->timeout == 0 && autoscan->interval == 0)
837 g_source_remove(autoscan->timeout);
839 autoscan->timeout = 0;
840 autoscan->interval = 0;
842 connman_device_unref(device);
845 static void stop_autoscan(struct connman_device *device)
847 const struct wifi_data *wifi = connman_device_get_data(device);
849 if (!wifi || !wifi->autoscan)
852 reset_autoscan(device);
854 connman_device_set_scanning(device, CONNMAN_SERVICE_TYPE_WIFI, false);
857 static void check_p2p_technology(void)
859 bool p2p_exists = false;
862 for (list = iface_list; list; list = list->next) {
863 struct wifi_data *w = list->data;
866 g_supplicant_interface_has_p2p(w->interface))
871 connman_technology_driver_unregister(&p2p_tech_driver);
872 connman_peer_driver_unregister(&peer_driver);
876 static void wifi_remove(struct connman_device *device)
878 struct wifi_data *wifi = connman_device_get_data(device);
880 DBG("device %p wifi %p", device, wifi);
885 stop_autoscan(device);
887 if (wifi->p2p_device)
888 p2p_iface_list = g_list_remove(p2p_iface_list, wifi);
890 iface_list = g_list_remove(iface_list, wifi);
892 check_p2p_technology();
894 remove_pending_wifi_device(wifi);
896 if (wifi->p2p_find_timeout) {
897 g_source_remove(wifi->p2p_find_timeout);
898 connman_device_unref(wifi->device);
901 if (wifi->p2p_connection_timeout)
902 g_source_remove(wifi->p2p_connection_timeout);
904 remove_networks(device, wifi);
907 connman_device_set_powered(device, false);
908 connman_device_set_data(device, NULL);
909 connman_device_unref(wifi->device);
910 connman_rtnl_remove_watch(wifi->watch);
912 g_supplicant_interface_set_data(wifi->interface, NULL);
914 g_supplicant_interface_cancel(wifi->interface);
916 if (wifi->scan_params)
917 g_supplicant_free_scan_params(wifi->scan_params);
919 g_free(wifi->autoscan);
920 g_free(wifi->identifier);
924 static bool is_duplicate(GSList *list, gchar *ssid, int ssid_len)
928 for (iter = list; iter; iter = g_slist_next(iter)) {
929 struct scan_ssid *scan_ssid = iter->data;
931 if (ssid_len == scan_ssid->ssid_len &&
932 memcmp(ssid, scan_ssid->ssid, ssid_len) == 0)
939 static int add_scan_param(gchar *hex_ssid, char *raw_ssid, int ssid_len,
940 int freq, GSupplicantScanParams *scan_data,
941 int driver_max_scan_ssids, char *ssid_name)
944 struct scan_ssid *scan_ssid;
946 if ((driver_max_scan_ssids == 0 ||
947 driver_max_scan_ssids > scan_data->num_ssids) &&
948 (hex_ssid || raw_ssid)) {
950 unsigned int j = 0, hex;
953 size_t hex_ssid_len = strlen(hex_ssid);
955 ssid = g_try_malloc0(hex_ssid_len / 2);
959 for (i = 0; i < hex_ssid_len; i += 2) {
960 sscanf(hex_ssid + i, "%02x", &hex);
969 * If we have already added hidden AP to the list,
970 * then do not do it again. This might happen if you have
971 * used or are using multiple wifi cards, so in that case
972 * you might have multiple service files for same AP.
974 if (is_duplicate(scan_data->ssids, ssid, j)) {
980 scan_ssid = g_try_new(struct scan_ssid, 1);
987 memcpy(scan_ssid->ssid, ssid, j);
988 scan_ssid->ssid_len = j;
989 scan_data->ssids = g_slist_prepend(scan_data->ssids,
992 scan_data->num_ssids++;
994 DBG("SSID %s added to scanned list of %d entries", ssid_name,
995 scan_data->num_ssids);
1002 scan_data->ssids = g_slist_reverse(scan_data->ssids);
1004 if (!scan_data->freqs) {
1005 scan_data->freqs = g_try_malloc0(sizeof(uint16_t));
1006 if (!scan_data->freqs) {
1007 g_slist_free_full(scan_data->ssids, g_free);
1011 scan_data->num_freqs = 1;
1012 scan_data->freqs[0] = freq;
1014 bool duplicate = false;
1016 /* Don't add duplicate entries */
1017 for (i = 0; i < scan_data->num_freqs; i++) {
1018 if (scan_data->freqs[i] == freq) {
1025 scan_data->num_freqs++;
1026 scan_data->freqs = g_try_realloc(scan_data->freqs,
1027 sizeof(uint16_t) * scan_data->num_freqs);
1028 if (!scan_data->freqs) {
1029 g_slist_free_full(scan_data->ssids, g_free);
1032 scan_data->freqs[scan_data->num_freqs - 1] = freq;
1039 static int get_hidden_connections(GSupplicantScanParams *scan_data)
1041 struct connman_config_entry **entries;
1047 int num_ssids = 0, add_param_failed = 0;
1049 services = connman_storage_get_services();
1050 for (i = 0; services && services[i]; i++) {
1051 if (strncmp(services[i], "wifi_", 5) != 0)
1054 keyfile = connman_storage_load_service(services[i]);
1058 value = g_key_file_get_boolean(keyfile,
1059 services[i], "Hidden", NULL);
1061 g_key_file_free(keyfile);
1065 value = g_key_file_get_boolean(keyfile,
1066 services[i], "Favorite", NULL);
1068 g_key_file_free(keyfile);
1072 ssid = g_key_file_get_string(keyfile,
1073 services[i], "SSID", NULL);
1075 name = g_key_file_get_string(keyfile, services[i], "Name",
1078 ret = add_scan_param(ssid, NULL, 0, 0, scan_data, 0, name);
1086 g_key_file_free(keyfile);
1090 * Check if there are any hidden AP that needs to be provisioned.
1092 entries = connman_config_get_entries("wifi");
1093 for (i = 0; entries && entries[i]; i++) {
1096 if (!entries[i]->hidden)
1099 if (!entries[i]->ssid) {
1100 ssid = entries[i]->name;
1103 ssid = entries[i]->ssid;
1104 len = entries[i]->ssid_len;
1110 ret = add_scan_param(NULL, ssid, len, 0, scan_data, 0, ssid);
1117 connman_config_free_entries(entries);
1119 if (add_param_failed > 0)
1120 DBG("Unable to scan %d out of %d SSIDs",
1121 add_param_failed, num_ssids);
1123 g_strfreev(services);
1128 static int get_hidden_connections_params(struct wifi_data *wifi,
1129 GSupplicantScanParams *scan_params)
1131 int driver_max_ssids, i;
1132 GSupplicantScanParams *orig_params;
1135 * Scan hidden networks so that we can autoconnect to them.
1136 * We will assume 1 as a default number of ssid to scan.
1138 driver_max_ssids = g_supplicant_interface_get_max_scan_ssids(
1140 if (driver_max_ssids == 0)
1141 driver_max_ssids = 1;
1143 DBG("max ssids %d", driver_max_ssids);
1145 if (!wifi->scan_params) {
1146 wifi->scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
1147 if (!wifi->scan_params)
1150 if (get_hidden_connections(wifi->scan_params) == 0) {
1151 g_supplicant_free_scan_params(wifi->scan_params);
1152 wifi->scan_params = NULL;
1158 orig_params = wifi->scan_params;
1160 /* Let's transfer driver_max_ssids params */
1161 for (i = 0; i < driver_max_ssids; i++) {
1162 struct scan_ssid *ssid;
1164 if (!wifi->scan_params->ssids)
1167 ssid = orig_params->ssids->data;
1168 orig_params->ssids = g_slist_remove(orig_params->ssids, ssid);
1169 scan_params->ssids = g_slist_prepend(scan_params->ssids, ssid);
1173 scan_params->num_ssids = i;
1174 scan_params->ssids = g_slist_reverse(scan_params->ssids);
1176 scan_params->freqs = g_memdup(orig_params->freqs,
1177 sizeof(uint16_t) * orig_params->num_freqs);
1178 if (!scan_params->freqs)
1181 scan_params->num_freqs = orig_params->num_freqs;
1186 orig_params->num_ssids -= scan_params->num_ssids;
1188 return scan_params->num_ssids;
1191 g_slist_free_full(scan_params->ssids, g_free);
1192 g_supplicant_free_scan_params(wifi->scan_params);
1193 wifi->scan_params = NULL;
1198 static int throw_wifi_scan(struct connman_device *device,
1199 GSupplicantInterfaceCallback callback)
1201 struct wifi_data *wifi = connman_device_get_data(device);
1207 DBG("device %p %p", device, wifi->interface);
1209 if (wifi->tethering)
1212 if (connman_device_get_scanning(device))
1215 connman_device_ref(device);
1217 ret = g_supplicant_interface_scan(wifi->interface, NULL,
1220 connman_device_set_scanning(device,
1221 CONNMAN_SERVICE_TYPE_WIFI, true);
1223 connman_device_unref(device);
1228 static void hidden_free(struct hidden_params *hidden)
1233 if (hidden->scan_params)
1234 g_supplicant_free_scan_params(hidden->scan_params);
1235 g_free(hidden->identity);
1236 g_free(hidden->passphrase);
1237 g_free(hidden->security);
1241 static void scan_callback(int result, GSupplicantInterface *interface,
1244 struct connman_device *device = user_data;
1245 struct wifi_data *wifi = connman_device_get_data(device);
1248 DBG("result %d wifi %p", result, wifi);
1251 if (wifi->hidden && !wifi->postpone_hidden) {
1252 connman_network_clear_hidden(wifi->hidden->user_data);
1253 hidden_free(wifi->hidden);
1254 wifi->hidden = NULL;
1257 if (wifi->scan_params) {
1258 g_supplicant_free_scan_params(wifi->scan_params);
1259 wifi->scan_params = NULL;
1264 connman_device_reset_scanning(device);
1266 /* User is connecting to a hidden AP, let's wait for finished event */
1267 if (wifi && wifi->hidden && wifi->postpone_hidden) {
1268 GSupplicantScanParams *scan_params;
1271 wifi->postpone_hidden = false;
1272 scan_params = wifi->hidden->scan_params;
1273 wifi->hidden->scan_params = NULL;
1275 reset_autoscan(device);
1277 ret = g_supplicant_interface_scan(wifi->interface, scan_params,
1278 scan_callback, device);
1282 /* On error, let's recall scan_callback, which will cleanup */
1283 return scan_callback(ret, interface, user_data);
1286 scanning = connman_device_get_scanning(device);
1289 connman_device_set_scanning(device,
1290 CONNMAN_SERVICE_TYPE_WIFI, false);
1293 if (result != -ENOLINK)
1294 start_autoscan(device);
1297 * If we are here then we were scanning; however, if we are
1298 * also mid-flight disabling the interface, then wifi_disable
1299 * has already cleared the device scanning state and
1300 * unreferenced the device, obviating the need to do it here.
1304 connman_device_unref(device);
1307 static void scan_callback_hidden(int result,
1308 GSupplicantInterface *interface, void *user_data)
1310 struct connman_device *device = user_data;
1311 struct wifi_data *wifi = connman_device_get_data(device);
1312 GSupplicantScanParams *scan_params;
1315 DBG("result %d wifi %p", result, wifi);
1320 /* User is trying to connect to a hidden AP */
1321 if (wifi->hidden && wifi->postpone_hidden)
1324 scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
1328 if (get_hidden_connections_params(wifi, scan_params) > 0) {
1329 ret = g_supplicant_interface_scan(wifi->interface,
1331 scan_callback_hidden,
1337 g_supplicant_free_scan_params(scan_params);
1340 scan_callback(result, interface, user_data);
1343 static gboolean autoscan_timeout(gpointer data)
1345 struct connman_device *device = data;
1346 struct wifi_data *wifi = connman_device_get_data(device);
1347 struct autoscan_params *autoscan;
1353 autoscan = wifi->autoscan;
1355 if (autoscan->interval <= 0) {
1356 interval = autoscan->base;
1359 interval = autoscan->interval * autoscan->base;
1361 if (interval > autoscan->limit)
1362 interval = autoscan->limit;
1364 throw_wifi_scan(wifi->device, scan_callback_hidden);
1367 DBG("interval %d", interval);
1369 autoscan->interval = interval;
1371 autoscan->timeout = g_timeout_add_seconds(interval,
1372 autoscan_timeout, device);
1377 static void start_autoscan(struct connman_device *device)
1379 struct wifi_data *wifi = connman_device_get_data(device);
1380 struct autoscan_params *autoscan;
1387 if (wifi->p2p_device)
1390 if (wifi->connected)
1393 autoscan = wifi->autoscan;
1397 if (autoscan->timeout > 0 || autoscan->interval > 0)
1400 connman_device_ref(device);
1402 autoscan_timeout(device);
1405 static struct autoscan_params *parse_autoscan_params(const char *params)
1407 struct autoscan_params *autoscan;
1412 DBG("Emulating autoscan");
1414 list_params = g_strsplit(params, ":", 0);
1415 if (list_params == 0)
1418 if (g_strv_length(list_params) < 3) {
1419 g_strfreev(list_params);
1423 base = atoi(list_params[1]);
1424 limit = atoi(list_params[2]);
1426 g_strfreev(list_params);
1428 autoscan = g_try_malloc0(sizeof(struct autoscan_params));
1430 DBG("Could not allocate memory for autoscan");
1434 DBG("base %d - limit %d", base, limit);
1435 autoscan->base = base;
1436 autoscan->limit = limit;
1441 static void setup_autoscan(struct wifi_data *wifi)
1443 if (!wifi->autoscan)
1444 wifi->autoscan = parse_autoscan_params(AUTOSCAN_DEFAULT);
1446 start_autoscan(wifi->device);
1449 static void finalize_interface_creation(struct wifi_data *wifi)
1451 DBG("interface is ready wifi %p tethering %d", wifi, wifi->tethering);
1453 if (!wifi->device) {
1454 connman_error("WiFi device not set");
1458 connman_device_set_powered(wifi->device, true);
1460 if (!connman_setting_get_bool("BackgroundScanning"))
1463 if (wifi->p2p_device)
1466 setup_autoscan(wifi);
1469 static void interface_create_callback(int result,
1470 GSupplicantInterface *interface,
1473 struct wifi_data *wifi = user_data;
1475 DBG("result %d ifname %s, wifi %p", result,
1476 g_supplicant_interface_get_ifname(interface),
1479 if (result < 0 || !wifi)
1482 wifi->interface = interface;
1483 g_supplicant_interface_set_data(interface, wifi);
1485 if (g_supplicant_interface_get_ready(interface)) {
1486 wifi->interface_ready = true;
1487 finalize_interface_creation(wifi);
1491 static int wifi_enable(struct connman_device *device)
1493 struct wifi_data *wifi = connman_device_get_data(device);
1496 const char *driver = connman_option_get_string("wifi");
1499 DBG("device %p %p", device, wifi);
1501 index = connman_device_get_index(device);
1502 if (!wifi || index < 0)
1505 if (is_p2p_connecting())
1506 return -EINPROGRESS;
1508 interface = connman_inet_ifname(index);
1509 ret = g_supplicant_interface_create(interface, driver, NULL,
1510 interface_create_callback,
1517 return -EINPROGRESS;
1520 static int wifi_disable(struct connman_device *device)
1522 struct wifi_data *wifi = connman_device_get_data(device);
1525 DBG("device %p wifi %p", device, wifi);
1530 wifi->connected = false;
1531 wifi->disconnecting = false;
1533 if (wifi->pending_network)
1534 wifi->pending_network = NULL;
1536 stop_autoscan(device);
1538 if (wifi->p2p_find_timeout) {
1539 g_source_remove(wifi->p2p_find_timeout);
1540 wifi->p2p_find_timeout = 0;
1541 connman_device_set_scanning(device, CONNMAN_SERVICE_TYPE_P2P, false);
1542 connman_device_unref(wifi->device);
1545 /* In case of a user scan, device is still referenced */
1546 if (connman_device_get_scanning(device)) {
1547 connman_device_set_scanning(device,
1548 CONNMAN_SERVICE_TYPE_WIFI, false);
1549 connman_device_unref(wifi->device);
1552 remove_networks(device, wifi);
1555 ret = g_supplicant_interface_remove(wifi->interface, NULL, NULL);
1559 return -EINPROGRESS;
1562 struct last_connected {
1568 static gint sort_entry(gconstpointer a, gconstpointer b, gpointer user_data)
1570 GTimeVal *aval = (GTimeVal *)a;
1571 GTimeVal *bval = (GTimeVal *)b;
1573 /* Note that the sort order is descending */
1574 if (aval->tv_sec < bval->tv_sec)
1577 if (aval->tv_sec > bval->tv_sec)
1583 static void free_entry(gpointer data)
1585 struct last_connected *entry = data;
1587 g_free(entry->ssid);
1591 static int get_latest_connections(int max_ssids,
1592 GSupplicantScanParams *scan_data)
1594 GSequenceIter *iter;
1595 GSequence *latest_list;
1596 struct last_connected *entry;
1605 latest_list = g_sequence_new(free_entry);
1609 services = connman_storage_get_services();
1610 for (i = 0; services && services[i]; i++) {
1611 if (strncmp(services[i], "wifi_", 5) != 0)
1614 keyfile = connman_storage_load_service(services[i]);
1618 str = g_key_file_get_string(keyfile,
1619 services[i], "Favorite", NULL);
1620 if (!str || g_strcmp0(str, "true")) {
1622 g_key_file_free(keyfile);
1627 str = g_key_file_get_string(keyfile,
1628 services[i], "AutoConnect", NULL);
1629 if (!str || g_strcmp0(str, "true")) {
1631 g_key_file_free(keyfile);
1636 str = g_key_file_get_string(keyfile,
1637 services[i], "Modified", NULL);
1639 g_key_file_free(keyfile);
1642 g_time_val_from_iso8601(str, &modified);
1645 ssid = g_key_file_get_string(keyfile,
1646 services[i], "SSID", NULL);
1648 freq = g_key_file_get_integer(keyfile, services[i],
1651 entry = g_try_new(struct last_connected, 1);
1653 g_sequence_free(latest_list);
1654 g_key_file_free(keyfile);
1660 entry->modified = modified;
1663 g_sequence_insert_sorted(latest_list, entry,
1669 g_key_file_free(keyfile);
1672 g_strfreev(services);
1674 num_ssids = num_ssids > max_ssids ? max_ssids : num_ssids;
1676 iter = g_sequence_get_begin_iter(latest_list);
1678 for (i = 0; i < num_ssids; i++) {
1679 entry = g_sequence_get(iter);
1681 DBG("ssid %s freq %d modified %lu", entry->ssid, entry->freq,
1682 entry->modified.tv_sec);
1684 add_scan_param(entry->ssid, NULL, 0, entry->freq, scan_data,
1685 max_ssids, entry->ssid);
1687 iter = g_sequence_iter_next(iter);
1690 g_sequence_free(latest_list);
1694 static int wifi_scan_simple(struct connman_device *device)
1696 reset_autoscan(device);
1698 return throw_wifi_scan(device, scan_callback_hidden);
1701 static gboolean p2p_find_stop(gpointer data)
1703 struct connman_device *device = data;
1704 struct wifi_data *wifi = connman_device_get_data(device);
1709 wifi->p2p_find_timeout = 0;
1711 g_supplicant_interface_p2p_stop_find(wifi->interface);
1714 connman_device_set_scanning(device, CONNMAN_SERVICE_TYPE_P2P, false);
1716 connman_device_unref(device);
1717 reset_autoscan(device);
1722 static void p2p_find_callback(int result, GSupplicantInterface *interface,
1725 struct connman_device *device = user_data;
1726 struct wifi_data *wifi = connman_device_get_data(device);
1728 DBG("result %d wifi %p", result, wifi);
1733 if (wifi->p2p_find_timeout) {
1734 g_source_remove(wifi->p2p_find_timeout);
1735 wifi->p2p_find_timeout = 0;
1741 wifi->p2p_find_timeout = g_timeout_add_seconds(P2P_FIND_TIMEOUT,
1742 p2p_find_stop, device);
1743 if (!wifi->p2p_find_timeout)
1748 p2p_find_stop(device);
1751 static int p2p_find(struct connman_device *device)
1753 struct wifi_data *wifi;
1758 if (!p2p_technology)
1761 wifi = connman_device_get_data(device);
1763 if (g_supplicant_interface_is_p2p_finding(wifi->interface))
1766 reset_autoscan(device);
1767 connman_device_ref(device);
1769 ret = g_supplicant_interface_p2p_find(wifi->interface,
1770 p2p_find_callback, device);
1772 connman_device_unref(device);
1773 start_autoscan(device);
1775 connman_device_set_scanning(device,
1776 CONNMAN_SERVICE_TYPE_P2P, true);
1783 * Note that the hidden scan is only used when connecting to this specific
1784 * hidden AP first time. It is not used when system autoconnects to hidden AP.
1786 static int wifi_scan(enum connman_service_type type,
1787 struct connman_device *device,
1788 const char *ssid, unsigned int ssid_len,
1789 const char *identity, const char* passphrase,
1790 const char *security, void *user_data)
1792 struct wifi_data *wifi = connman_device_get_data(device);
1793 GSupplicantScanParams *scan_params = NULL;
1794 struct scan_ssid *scan_ssid;
1795 struct hidden_params *hidden;
1797 int driver_max_ssids = 0;
1804 if (wifi->p2p_device)
1807 if (wifi->tethering)
1810 if (type == CONNMAN_SERVICE_TYPE_P2P)
1811 return p2p_find(device);
1813 DBG("device %p wifi %p hidden ssid %s", device, wifi->interface, ssid);
1815 scanning = connman_device_get_scanning(device);
1817 if (!ssid || ssid_len == 0 || ssid_len > 32) {
1821 driver_max_ssids = g_supplicant_interface_get_max_scan_ssids(
1823 DBG("max ssids %d", driver_max_ssids);
1824 if (driver_max_ssids == 0)
1825 return wifi_scan_simple(device);
1829 if (scanning && wifi->hidden && wifi->postpone_hidden)
1835 scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
1840 scan_ssid = g_try_new(struct scan_ssid, 1);
1842 g_free(scan_params);
1846 memcpy(scan_ssid->ssid, ssid, ssid_len);
1847 scan_ssid->ssid_len = ssid_len;
1848 scan_params->ssids = g_slist_prepend(scan_params->ssids,
1850 scan_params->num_ssids = 1;
1852 hidden = g_try_new0(struct hidden_params, 1);
1854 g_supplicant_free_scan_params(scan_params);
1859 hidden_free(wifi->hidden);
1860 wifi->hidden = NULL;
1863 memcpy(hidden->ssid, ssid, ssid_len);
1864 hidden->ssid_len = ssid_len;
1865 hidden->identity = g_strdup(identity);
1866 hidden->passphrase = g_strdup(passphrase);
1867 hidden->security = g_strdup(security);
1868 hidden->user_data = user_data;
1869 wifi->hidden = hidden;
1872 /* Let's keep this active scan for later,
1873 * when current scan will be over. */
1874 wifi->postpone_hidden = TRUE;
1875 hidden->scan_params = scan_params;
1879 } else if (wifi->connected) {
1880 g_supplicant_free_scan_params(scan_params);
1881 return wifi_scan_simple(device);
1883 ret = get_latest_connections(driver_max_ssids, scan_params);
1885 g_supplicant_free_scan_params(scan_params);
1886 return wifi_scan_simple(device);
1890 connman_device_ref(device);
1892 reset_autoscan(device);
1894 ret = g_supplicant_interface_scan(wifi->interface, scan_params,
1895 scan_callback, device);
1897 connman_device_set_scanning(device,
1898 CONNMAN_SERVICE_TYPE_WIFI, true);
1900 g_supplicant_free_scan_params(scan_params);
1901 connman_device_unref(device);
1904 hidden_free(wifi->hidden);
1905 wifi->hidden = NULL;
1912 static void wifi_regdom_callback(int result,
1916 struct connman_device *device = user_data;
1918 connman_device_regdom_notify(device, result, alpha2);
1920 connman_device_unref(device);
1923 static int wifi_set_regdom(struct connman_device *device, const char *alpha2)
1925 struct wifi_data *wifi = connman_device_get_data(device);
1931 connman_device_ref(device);
1933 ret = g_supplicant_interface_set_country(wifi->interface,
1934 wifi_regdom_callback,
1937 connman_device_unref(device);
1942 static struct connman_device_driver wifi_ng_driver = {
1944 .type = CONNMAN_DEVICE_TYPE_WIFI,
1945 .priority = CONNMAN_DEVICE_PRIORITY_LOW,
1946 .probe = wifi_probe,
1947 .remove = wifi_remove,
1948 .enable = wifi_enable,
1949 .disable = wifi_disable,
1951 .set_regdom = wifi_set_regdom,
1954 static void system_ready(void)
1958 if (connman_device_driver_register(&wifi_ng_driver) < 0)
1959 connman_error("Failed to register WiFi driver");
1962 static void system_killed(void)
1966 connman_device_driver_unregister(&wifi_ng_driver);
1969 static int network_probe(struct connman_network *network)
1971 DBG("network %p", network);
1976 static void network_remove(struct connman_network *network)
1978 struct connman_device *device = connman_network_get_device(network);
1979 struct wifi_data *wifi;
1981 DBG("network %p", network);
1983 wifi = connman_device_get_data(device);
1987 if (wifi->network != network)
1990 wifi->network = NULL;
1993 static void connect_callback(int result, GSupplicantInterface *interface,
1996 struct connman_network *network = user_data;
1998 DBG("network %p result %d", network, result);
2000 if (result == -ENOKEY) {
2001 connman_network_set_error(network,
2002 CONNMAN_NETWORK_ERROR_INVALID_KEY);
2003 } else if (result < 0) {
2004 connman_network_set_error(network,
2005 CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
2008 connman_network_unref(network);
2011 static GSupplicantSecurity network_security(const char *security)
2013 if (g_str_equal(security, "none"))
2014 return G_SUPPLICANT_SECURITY_NONE;
2015 else if (g_str_equal(security, "wep"))
2016 return G_SUPPLICANT_SECURITY_WEP;
2017 else if (g_str_equal(security, "psk"))
2018 return G_SUPPLICANT_SECURITY_PSK;
2019 else if (g_str_equal(security, "wpa"))
2020 return G_SUPPLICANT_SECURITY_PSK;
2021 else if (g_str_equal(security, "rsn"))
2022 return G_SUPPLICANT_SECURITY_PSK;
2023 else if (g_str_equal(security, "ieee8021x"))
2024 return G_SUPPLICANT_SECURITY_IEEE8021X;
2026 return G_SUPPLICANT_SECURITY_UNKNOWN;
2029 static void ssid_init(GSupplicantSSID *ssid, struct connman_network *network)
2031 const char *security;
2033 memset(ssid, 0, sizeof(*ssid));
2034 ssid->mode = G_SUPPLICANT_MODE_INFRA;
2035 ssid->ssid = connman_network_get_blob(network, "WiFi.SSID",
2037 ssid->scan_ssid = 1;
2038 security = connman_network_get_string(network, "WiFi.Security");
2039 ssid->security = network_security(security);
2040 ssid->passphrase = connman_network_get_string(network,
2043 ssid->eap = connman_network_get_string(network, "WiFi.EAP");
2046 * If our private key password is unset,
2047 * we use the supplied passphrase. That is needed
2048 * for PEAP where 2 passphrases (identity and client
2049 * cert may have to be provided.
2051 if (!connman_network_get_string(network, "WiFi.PrivateKeyPassphrase"))
2052 connman_network_set_string(network,
2053 "WiFi.PrivateKeyPassphrase",
2055 /* We must have an identity for both PEAP and TLS */
2056 ssid->identity = connman_network_get_string(network, "WiFi.Identity");
2058 /* Use agent provided identity as a fallback */
2059 if (!ssid->identity || strlen(ssid->identity) == 0)
2060 ssid->identity = connman_network_get_string(network,
2061 "WiFi.AgentIdentity");
2063 ssid->anonymous_identity = connman_network_get_string(network,
2064 "WiFi.AnonymousIdentity");
2065 ssid->ca_cert_path = connman_network_get_string(network,
2067 ssid->subject_match = connman_network_get_string(network,
2068 "WiFi.SubjectMatch");
2069 ssid->altsubject_match = connman_network_get_string(network,
2070 "WiFi.AltSubjectMatch");
2071 ssid->domain_suffix_match = connman_network_get_string(network,
2072 "WiFi.DomainSuffixMatch");
2073 ssid->domain_match = connman_network_get_string(network,
2074 "WiFi.DomainMatch");
2075 ssid->client_cert_path = connman_network_get_string(network,
2076 "WiFi.ClientCertFile");
2077 ssid->private_key_path = connman_network_get_string(network,
2078 "WiFi.PrivateKeyFile");
2079 ssid->private_key_passphrase = connman_network_get_string(network,
2080 "WiFi.PrivateKeyPassphrase");
2081 ssid->phase2_auth = connman_network_get_string(network, "WiFi.Phase2");
2083 ssid->use_wps = connman_network_get_bool(network, "WiFi.UseWPS");
2084 ssid->pin_wps = connman_network_get_string(network, "WiFi.PinWPS");
2086 if (connman_setting_get_bool("BackgroundScanning"))
2087 ssid->bgscan = BGSCAN_DEFAULT;
2090 static int network_connect(struct connman_network *network)
2092 struct connman_device *device = connman_network_get_device(network);
2093 struct wifi_data *wifi;
2094 GSupplicantInterface *interface;
2095 GSupplicantSSID *ssid;
2097 DBG("network %p", network);
2102 wifi = connman_device_get_data(device);
2106 ssid = g_try_malloc0(sizeof(GSupplicantSSID));
2110 interface = wifi->interface;
2112 ssid_init(ssid, network);
2114 if (wifi->disconnecting) {
2115 wifi->pending_network = network;
2118 wifi->network = connman_network_ref(network);
2121 return g_supplicant_interface_connect(interface, ssid,
2122 connect_callback, network);
2125 return -EINPROGRESS;
2128 static void disconnect_callback(int result, GSupplicantInterface *interface,
2131 struct wifi_data *wifi = user_data;
2133 DBG("result %d supplicant interface %p wifi %p",
2134 result, interface, wifi);
2136 if (result == -ECONNABORTED) {
2137 DBG("wifi interface no longer available");
2141 if (wifi->network) {
2142 connman_network_set_connected(wifi->network, false);
2143 wifi->network = NULL;
2146 wifi->disconnecting = false;
2147 wifi->connected = false;
2149 if (wifi->pending_network) {
2150 network_connect(wifi->pending_network);
2151 wifi->pending_network = NULL;
2154 start_autoscan(wifi->device);
2157 static int network_disconnect(struct connman_network *network)
2159 struct connman_device *device = connman_network_get_device(network);
2160 struct wifi_data *wifi;
2163 DBG("network %p", network);
2165 wifi = connman_device_get_data(device);
2166 if (!wifi || !wifi->interface)
2169 connman_network_set_associating(network, false);
2171 if (wifi->disconnecting)
2174 wifi->disconnecting = true;
2176 err = g_supplicant_interface_disconnect(wifi->interface,
2177 disconnect_callback, wifi);
2179 wifi->disconnecting = false;
2184 static struct connman_network_driver network_driver = {
2186 .type = CONNMAN_NETWORK_TYPE_WIFI,
2187 .priority = CONNMAN_NETWORK_PRIORITY_LOW,
2188 .probe = network_probe,
2189 .remove = network_remove,
2190 .connect = network_connect,
2191 .disconnect = network_disconnect,
2194 static void interface_added(GSupplicantInterface *interface)
2196 const char *ifname = g_supplicant_interface_get_ifname(interface);
2197 const char *driver = g_supplicant_interface_get_driver(interface);
2198 struct wifi_data *wifi;
2200 wifi = g_supplicant_interface_get_data(interface);
2202 wifi = get_pending_wifi_data(ifname);
2206 wifi->interface = interface;
2207 g_supplicant_interface_set_data(interface, wifi);
2208 p2p_iface_list = g_list_append(p2p_iface_list, wifi);
2209 wifi->p2p_device = true;
2212 DBG("ifname %s driver %s wifi %p tethering %d",
2213 ifname, driver, wifi, wifi->tethering);
2215 if (!wifi->device) {
2216 connman_error("WiFi device not set");
2220 connman_device_set_powered(wifi->device, true);
2223 static bool is_idle(struct wifi_data *wifi)
2225 DBG("state %d", wifi->state);
2227 switch (wifi->state) {
2228 case G_SUPPLICANT_STATE_UNKNOWN:
2229 case G_SUPPLICANT_STATE_DISABLED:
2230 case G_SUPPLICANT_STATE_DISCONNECTED:
2231 case G_SUPPLICANT_STATE_INACTIVE:
2232 case G_SUPPLICANT_STATE_SCANNING:
2235 case G_SUPPLICANT_STATE_AUTHENTICATING:
2236 case G_SUPPLICANT_STATE_ASSOCIATING:
2237 case G_SUPPLICANT_STATE_ASSOCIATED:
2238 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
2239 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
2240 case G_SUPPLICANT_STATE_COMPLETED:
2247 static bool is_idle_wps(GSupplicantInterface *interface,
2248 struct wifi_data *wifi)
2250 /* First, let's check if WPS processing did not went wrong */
2251 if (g_supplicant_interface_get_wps_state(interface) ==
2252 G_SUPPLICANT_WPS_STATE_FAIL)
2255 /* Unlike normal connection, being associated while processing wps
2256 * actually means that we are idling. */
2257 switch (wifi->state) {
2258 case G_SUPPLICANT_STATE_UNKNOWN:
2259 case G_SUPPLICANT_STATE_DISABLED:
2260 case G_SUPPLICANT_STATE_DISCONNECTED:
2261 case G_SUPPLICANT_STATE_INACTIVE:
2262 case G_SUPPLICANT_STATE_SCANNING:
2263 case G_SUPPLICANT_STATE_ASSOCIATED:
2265 case G_SUPPLICANT_STATE_AUTHENTICATING:
2266 case G_SUPPLICANT_STATE_ASSOCIATING:
2267 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
2268 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
2269 case G_SUPPLICANT_STATE_COMPLETED:
2276 static bool handle_wps_completion(GSupplicantInterface *interface,
2277 struct connman_network *network,
2278 struct connman_device *device,
2279 struct wifi_data *wifi)
2283 wps = connman_network_get_bool(network, "WiFi.UseWPS");
2285 const unsigned char *ssid, *wps_ssid;
2286 unsigned int ssid_len, wps_ssid_len;
2287 const char *wps_key;
2289 /* Checking if we got associated with requested
2291 ssid = connman_network_get_blob(network, "WiFi.SSID",
2294 wps_ssid = g_supplicant_interface_get_wps_ssid(
2295 interface, &wps_ssid_len);
2297 if (!wps_ssid || wps_ssid_len != ssid_len ||
2298 memcmp(ssid, wps_ssid, ssid_len) != 0) {
2299 connman_network_set_associating(network, false);
2300 g_supplicant_interface_disconnect(wifi->interface,
2301 disconnect_callback, wifi);
2305 wps_key = g_supplicant_interface_get_wps_key(interface);
2306 connman_network_set_string(network, "WiFi.Passphrase",
2309 connman_network_set_string(network, "WiFi.PinWPS", NULL);
2315 static bool handle_assoc_status_code(GSupplicantInterface *interface,
2316 struct wifi_data *wifi)
2318 if (wifi->state == G_SUPPLICANT_STATE_ASSOCIATING &&
2319 wifi->assoc_code == ASSOC_STATUS_NO_CLIENT &&
2320 wifi->load_shaping_retries < LOAD_SHAPING_MAX_RETRIES) {
2321 wifi->load_shaping_retries ++;
2324 wifi->load_shaping_retries = 0;
2328 static bool handle_4way_handshake_failure(GSupplicantInterface *interface,
2329 struct connman_network *network,
2330 struct wifi_data *wifi)
2332 struct connman_service *service;
2334 if (wifi->state != G_SUPPLICANT_STATE_4WAY_HANDSHAKE)
2337 if (wifi->connected)
2340 service = connman_service_lookup_from_network(network);
2346 if (connman_service_get_favorite(service)) {
2347 if (wifi->retries < FAVORITE_MAXIMUM_RETRIES)
2352 connman_network_set_error(network, CONNMAN_NETWORK_ERROR_INVALID_KEY);
2357 static void interface_state(GSupplicantInterface *interface)
2359 struct connman_network *network;
2360 struct connman_device *device;
2361 struct wifi_data *wifi;
2362 GSupplicantState state = g_supplicant_interface_get_state(interface);
2366 wifi = g_supplicant_interface_get_data(interface);
2368 DBG("wifi %p interface state %d", wifi, state);
2373 if (state == G_SUPPLICANT_STATE_COMPLETED) {
2374 if (wifi->tethering_param) {
2375 g_free(wifi->tethering_param->ssid);
2376 g_free(wifi->tethering_param);
2377 wifi->tethering_param = NULL;
2381 device = wifi->device;
2385 if (g_supplicant_interface_get_ready(interface) &&
2386 !wifi->interface_ready) {
2387 wifi->interface_ready = true;
2388 finalize_interface_creation(wifi);
2391 network = wifi->network;
2396 case G_SUPPLICANT_STATE_SCANNING:
2397 if (wifi->connected)
2398 connman_network_set_connected(network, false);
2402 case G_SUPPLICANT_STATE_AUTHENTICATING:
2403 case G_SUPPLICANT_STATE_ASSOCIATING:
2404 stop_autoscan(device);
2406 if (!wifi->connected)
2407 connman_network_set_associating(network, true);
2411 case G_SUPPLICANT_STATE_COMPLETED:
2412 /* though it should be already stopped: */
2413 stop_autoscan(device);
2415 if (!handle_wps_completion(interface, network, device, wifi))
2418 connman_network_set_connected(network, true);
2420 wifi->disconnect_code = 0;
2421 wifi->assoc_code = 0;
2422 wifi->load_shaping_retries = 0;
2425 case G_SUPPLICANT_STATE_DISCONNECTED:
2427 * If we're in one of the idle modes, we have
2428 * not started association yet and thus setting
2429 * those ones to FALSE could cancel an association
2432 wps = connman_network_get_bool(network, "WiFi.UseWPS");
2434 if (is_idle_wps(interface, wifi))
2440 if (handle_assoc_status_code(interface, wifi))
2443 /* If previous state was 4way-handshake, then
2444 * it's either: psk was incorrect and thus we retry
2445 * or if we reach the maximum retries we declare the
2447 if (handle_4way_handshake_failure(interface,
2451 /* See table 8-36 Reason codes in IEEE Std 802.11 */
2452 switch (wifi->disconnect_code) {
2453 case 1: /* Unspecified reason */
2454 /* Let's assume it's because we got blocked */
2456 case 6: /* Class 2 frame received from nonauthenticated STA */
2457 connman_network_set_error(network,
2458 CONNMAN_NETWORK_ERROR_BLOCKED);
2464 connman_network_set_connected(network, false);
2465 connman_network_set_associating(network, false);
2466 wifi->disconnecting = false;
2468 start_autoscan(device);
2472 case G_SUPPLICANT_STATE_INACTIVE:
2473 connman_network_set_associating(network, false);
2474 start_autoscan(device);
2478 case G_SUPPLICANT_STATE_UNKNOWN:
2479 case G_SUPPLICANT_STATE_DISABLED:
2480 case G_SUPPLICANT_STATE_ASSOCIATED:
2481 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
2482 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
2486 old_connected = wifi->connected;
2487 wifi->state = state;
2489 /* Saving wpa_s state policy:
2490 * If connected and if the state changes are roaming related:
2491 * --> We stay connected
2493 * --> We are connected
2495 * --> We are not connected
2498 case G_SUPPLICANT_STATE_AUTHENTICATING:
2499 case G_SUPPLICANT_STATE_ASSOCIATING:
2500 case G_SUPPLICANT_STATE_ASSOCIATED:
2501 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
2502 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
2503 if (wifi->connected)
2504 connman_warn("Probably roaming right now!"
2505 " Staying connected...");
2507 case G_SUPPLICANT_STATE_SCANNING:
2508 wifi->connected = false;
2511 start_autoscan(device);
2513 case G_SUPPLICANT_STATE_COMPLETED:
2514 wifi->connected = true;
2517 wifi->connected = false;
2524 static void interface_removed(GSupplicantInterface *interface)
2526 const char *ifname = g_supplicant_interface_get_ifname(interface);
2527 struct wifi_data *wifi;
2529 DBG("ifname %s", ifname);
2531 wifi = g_supplicant_interface_get_data(interface);
2534 wifi->interface = NULL;
2536 if (wifi && wifi->tethering)
2539 if (!wifi || !wifi->device) {
2540 DBG("wifi interface already removed");
2544 connman_device_set_powered(wifi->device, false);
2546 check_p2p_technology();
2549 static void set_device_type(const char *type, char dev_type[17])
2551 const char *oui = "0050F204";
2552 const char *category = "0001";
2553 const char *sub_category = "0000";
2555 if (!g_strcmp0(type, "handset")) {
2557 sub_category = "0005";
2558 } else if (!g_strcmp0(type, "vm") || !g_strcmp0(type, "container"))
2559 sub_category = "0001";
2560 else if (!g_strcmp0(type, "server"))
2561 sub_category = "0002";
2562 else if (!g_strcmp0(type, "laptop"))
2563 sub_category = "0005";
2564 else if (!g_strcmp0(type, "desktop"))
2565 sub_category = "0006";
2566 else if (!g_strcmp0(type, "tablet"))
2567 sub_category = "0009";
2568 else if (!g_strcmp0(type, "watch"))
2571 snprintf(dev_type, 17, "%s%s%s", category, oui, sub_category);
2574 static void p2p_support(GSupplicantInterface *interface)
2576 char dev_type[17] = {};
2577 const char *hostname;
2584 if (!g_supplicant_interface_has_p2p(interface))
2587 if (connman_technology_driver_register(&p2p_tech_driver) < 0) {
2588 DBG("Could not register P2P technology driver");
2592 hostname = connman_utsname_get_hostname();
2594 hostname = "ConnMan";
2596 set_device_type(connman_machine_get_type(), dev_type);
2597 g_supplicant_interface_set_p2p_device_config(interface,
2598 hostname, dev_type);
2599 connman_peer_driver_register(&peer_driver);
2602 static void scan_started(GSupplicantInterface *interface)
2607 static void scan_finished(GSupplicantInterface *interface)
2612 static void ap_create_fail(GSupplicantInterface *interface)
2614 struct wifi_data *wifi = g_supplicant_interface_get_data(interface);
2617 if ((wifi->tethering) && (wifi->tethering_param)) {
2618 DBG("%s create AP fail \n",
2619 g_supplicant_interface_get_ifname(wifi->interface));
2621 connman_inet_remove_from_bridge(wifi->index, wifi->bridge);
2622 wifi->ap_supported = WIFI_AP_NOT_SUPPORTED;
2623 wifi->tethering = false;
2625 ret = tech_set_tethering(wifi->tethering_param->technology,
2626 wifi->tethering_param->ssid->ssid,
2627 wifi->tethering_param->ssid->passphrase,
2628 wifi->bridge, true);
2630 if ((ret == -EOPNOTSUPP) && (wifi_technology)) {
2631 connman_technology_tethering_notify(wifi_technology,false);
2634 g_free(wifi->tethering_param->ssid);
2635 g_free(wifi->tethering_param);
2636 wifi->tethering_param = NULL;
2642 static unsigned char calculate_strength(GSupplicantNetwork *supplicant_network)
2644 unsigned char strength;
2646 strength = 120 + g_supplicant_network_get_signal(supplicant_network);
2653 static void network_added(GSupplicantNetwork *supplicant_network)
2655 struct connman_network *network;
2656 GSupplicantInterface *interface;
2657 struct wifi_data *wifi;
2658 const char *name, *identifier, *security, *group, *mode;
2659 const unsigned char *ssid;
2660 unsigned int ssid_len;
2664 bool wps_advertizing;
2666 mode = g_supplicant_network_get_mode(supplicant_network);
2667 identifier = g_supplicant_network_get_identifier(supplicant_network);
2669 DBG("%s", identifier);
2671 if (!g_strcmp0(mode, "adhoc"))
2674 interface = g_supplicant_network_get_interface(supplicant_network);
2675 wifi = g_supplicant_interface_get_data(interface);
2676 name = g_supplicant_network_get_name(supplicant_network);
2677 security = g_supplicant_network_get_security(supplicant_network);
2678 group = g_supplicant_network_get_identifier(supplicant_network);
2679 wps = g_supplicant_network_get_wps(supplicant_network);
2680 wps_pbc = g_supplicant_network_is_wps_pbc(supplicant_network);
2681 wps_ready = g_supplicant_network_is_wps_active(supplicant_network);
2682 wps_advertizing = g_supplicant_network_is_wps_advertizing(
2683 supplicant_network);
2688 ssid = g_supplicant_network_get_ssid(supplicant_network, &ssid_len);
2690 network = connman_device_get_network(wifi->device, identifier);
2693 network = connman_network_create(identifier,
2694 CONNMAN_NETWORK_TYPE_WIFI);
2698 connman_network_set_index(network, wifi->index);
2700 if (connman_device_add_network(wifi->device, network) < 0) {
2701 connman_network_unref(network);
2705 wifi->networks = g_slist_prepend(wifi->networks, network);
2708 if (name && name[0] != '\0')
2709 connman_network_set_name(network, name);
2711 connman_network_set_blob(network, "WiFi.SSID",
2713 connman_network_set_string(network, "WiFi.Security", security);
2714 connman_network_set_strength(network,
2715 calculate_strength(supplicant_network));
2716 connman_network_set_bool(network, "WiFi.WPS", wps);
2719 /* Is AP advertizing for WPS association?
2720 * If so, we decide to use WPS by default */
2721 if (wps_ready && wps_pbc &&
2723 connman_network_set_bool(network, "WiFi.UseWPS", true);
2726 connman_network_set_frequency(network,
2727 g_supplicant_network_get_frequency(supplicant_network));
2729 connman_network_set_available(network, true);
2730 connman_network_set_string(network, "WiFi.Mode", mode);
2733 connman_network_set_group(network, group);
2735 if (wifi->hidden && ssid) {
2736 if (!g_strcmp0(wifi->hidden->security, security) &&
2737 wifi->hidden->ssid_len == ssid_len &&
2738 !memcmp(wifi->hidden->ssid, ssid, ssid_len)) {
2739 connman_network_connect_hidden(network,
2740 wifi->hidden->identity,
2741 wifi->hidden->passphrase,
2742 wifi->hidden->user_data);
2743 wifi->hidden->user_data = NULL;
2744 hidden_free(wifi->hidden);
2745 wifi->hidden = NULL;
2750 static void network_removed(GSupplicantNetwork *network)
2752 GSupplicantInterface *interface;
2753 struct wifi_data *wifi;
2754 const char *name, *identifier;
2755 struct connman_network *connman_network;
2757 interface = g_supplicant_network_get_interface(network);
2758 wifi = g_supplicant_interface_get_data(interface);
2759 identifier = g_supplicant_network_get_identifier(network);
2760 name = g_supplicant_network_get_name(network);
2762 DBG("name %s", name);
2767 connman_network = connman_device_get_network(wifi->device, identifier);
2768 if (!connman_network)
2771 wifi->networks = g_slist_remove(wifi->networks, connman_network);
2773 connman_device_remove_network(wifi->device, connman_network);
2774 connman_network_unref(connman_network);
2777 static void network_changed(GSupplicantNetwork *network, const char *property)
2779 GSupplicantInterface *interface;
2780 struct wifi_data *wifi;
2781 const char *name, *identifier;
2782 struct connman_network *connman_network;
2784 interface = g_supplicant_network_get_interface(network);
2785 wifi = g_supplicant_interface_get_data(interface);
2786 identifier = g_supplicant_network_get_identifier(network);
2787 name = g_supplicant_network_get_name(network);
2789 DBG("name %s", name);
2794 connman_network = connman_device_get_network(wifi->device, identifier);
2795 if (!connman_network)
2798 if (g_str_equal(property, "Signal")) {
2799 connman_network_set_strength(connman_network,
2800 calculate_strength(network));
2801 connman_network_update(connman_network);
2805 static void network_associated(GSupplicantNetwork *network)
2807 GSupplicantInterface *interface;
2808 struct wifi_data *wifi;
2809 struct connman_network *connman_network;
2810 const char *identifier;
2814 interface = g_supplicant_network_get_interface(network);
2818 wifi = g_supplicant_interface_get_data(interface);
2822 identifier = g_supplicant_network_get_identifier(network);
2824 connman_network = connman_device_get_network(wifi->device, identifier);
2825 if (!connman_network)
2828 if (wifi->network) {
2829 if (wifi->network == connman_network)
2833 * This should never happen, we got associated with
2834 * a network different than the one we were expecting.
2836 DBG("Associated to %p while expecting %p",
2837 connman_network, wifi->network);
2839 connman_network_set_associating(wifi->network, false);
2842 DBG("Reconnecting to previous network %p from wpa_s", connman_network);
2844 wifi->network = connman_network_ref(connman_network);
2848 * Interface state changes callback (interface_state) is always
2849 * called before network_associated callback thus we need to call
2850 * interface_state again in order to process the new state now that
2851 * we have the network properly set.
2853 interface_state(interface);
2856 static void apply_peer_services(GSupplicantPeer *peer,
2857 struct connman_peer *connman_peer)
2859 const unsigned char *data;
2864 connman_peer_reset_services(connman_peer);
2866 data = g_supplicant_peer_get_widi_ies(peer, &length);
2868 connman_peer_add_service(connman_peer,
2869 CONNMAN_PEER_SERVICE_WIFI_DISPLAY, data, length);
2873 static void peer_found(GSupplicantPeer *peer)
2875 GSupplicantInterface *iface = g_supplicant_peer_get_interface(peer);
2876 struct wifi_data *wifi = g_supplicant_interface_get_data(iface);
2877 struct connman_peer *connman_peer;
2878 const char *identifier, *name;
2881 identifier = g_supplicant_peer_get_identifier(peer);
2882 name = g_supplicant_peer_get_name(peer);
2884 DBG("ident: %s", identifier);
2886 connman_peer = connman_peer_get(wifi->device, identifier);
2890 connman_peer = connman_peer_create(identifier);
2891 connman_peer_set_name(connman_peer, name);
2892 connman_peer_set_device(connman_peer, wifi->device);
2893 apply_peer_services(peer, connman_peer);
2895 ret = connman_peer_register(connman_peer);
2896 if (ret < 0 && ret != -EALREADY)
2897 connman_peer_unref(connman_peer);
2899 wifi->peers = g_slist_prepend(wifi->peers, connman_peer);
2902 static void peer_lost(GSupplicantPeer *peer)
2904 GSupplicantInterface *iface = g_supplicant_peer_get_interface(peer);
2905 struct wifi_data *wifi = g_supplicant_interface_get_data(iface);
2906 struct connman_peer *connman_peer;
2907 const char *identifier;
2912 identifier = g_supplicant_peer_get_identifier(peer);
2914 DBG("ident: %s", identifier);
2916 connman_peer = connman_peer_get(wifi->device, identifier);
2918 if (wifi->p2p_connecting &&
2919 wifi->pending_peer == connman_peer) {
2920 peer_connect_timeout(wifi);
2922 connman_peer_unregister(connman_peer);
2923 connman_peer_unref(connman_peer);
2926 wifi->peers = g_slist_remove(wifi->peers, connman_peer);
2929 static void peer_changed(GSupplicantPeer *peer, GSupplicantPeerState state)
2931 GSupplicantInterface *iface = g_supplicant_peer_get_interface(peer);
2932 struct wifi_data *wifi = g_supplicant_interface_get_data(iface);
2933 enum connman_peer_state p_state = CONNMAN_PEER_STATE_UNKNOWN;
2934 struct connman_peer *connman_peer;
2935 const char *identifier;
2937 identifier = g_supplicant_peer_get_identifier(peer);
2939 DBG("ident: %s", identifier);
2944 connman_peer = connman_peer_get(wifi->device, identifier);
2949 case G_SUPPLICANT_PEER_SERVICES_CHANGED:
2950 apply_peer_services(peer, connman_peer);
2951 connman_peer_services_changed(connman_peer);
2953 case G_SUPPLICANT_PEER_GROUP_CHANGED:
2954 if (!g_supplicant_peer_is_in_a_group(peer))
2955 p_state = CONNMAN_PEER_STATE_IDLE;
2957 p_state = CONNMAN_PEER_STATE_CONFIGURATION;
2959 case G_SUPPLICANT_PEER_GROUP_STARTED:
2961 case G_SUPPLICANT_PEER_GROUP_FINISHED:
2962 p_state = CONNMAN_PEER_STATE_IDLE;
2964 case G_SUPPLICANT_PEER_GROUP_JOINED:
2965 connman_peer_set_iface_address(connman_peer,
2966 g_supplicant_peer_get_iface_address(peer));
2968 case G_SUPPLICANT_PEER_GROUP_DISCONNECTED:
2969 p_state = CONNMAN_PEER_STATE_IDLE;
2971 case G_SUPPLICANT_PEER_GROUP_FAILED:
2972 if (g_supplicant_peer_has_requested_connection(peer))
2973 p_state = CONNMAN_PEER_STATE_IDLE;
2975 p_state = CONNMAN_PEER_STATE_FAILURE;
2979 if (p_state == CONNMAN_PEER_STATE_CONFIGURATION ||
2980 p_state == CONNMAN_PEER_STATE_FAILURE) {
2981 if (wifi->p2p_connecting
2982 && connman_peer == wifi->pending_peer)
2983 peer_cancel_timeout(wifi);
2985 p_state = CONNMAN_PEER_STATE_UNKNOWN;
2988 if (p_state == CONNMAN_PEER_STATE_UNKNOWN)
2991 if (p_state == CONNMAN_PEER_STATE_CONFIGURATION) {
2992 GSupplicantInterface *g_iface;
2993 struct wifi_data *g_wifi;
2995 g_iface = g_supplicant_peer_get_group_interface(peer);
2999 g_wifi = g_supplicant_interface_get_data(g_iface);
3003 connman_peer_set_as_master(connman_peer,
3004 !g_supplicant_peer_is_client(peer));
3005 connman_peer_set_sub_device(connman_peer, g_wifi->device);
3008 * If wpa_supplicant didn't create a dedicated p2p-group
3009 * interface then mark this interface as p2p_device to avoid
3010 * scan and auto-scan are launched on it while P2P is connected.
3012 if (!g_list_find(p2p_iface_list, g_wifi))
3013 wifi->p2p_device = true;
3016 connman_peer_set_state(connman_peer, p_state);
3019 static void peer_request(GSupplicantPeer *peer)
3021 GSupplicantInterface *iface = g_supplicant_peer_get_interface(peer);
3022 struct wifi_data *wifi = g_supplicant_interface_get_data(iface);
3023 struct connman_peer *connman_peer;
3024 const char *identifier;
3026 identifier = g_supplicant_peer_get_identifier(peer);
3028 DBG("ident: %s", identifier);
3030 connman_peer = connman_peer_get(wifi->device, identifier);
3034 connman_peer_request_connection(connman_peer);
3037 static void debug(const char *str)
3039 if (getenv("CONNMAN_SUPPLICANT_DEBUG"))
3040 connman_debug("%s", str);
3043 static void disconnect_reasoncode(GSupplicantInterface *interface,
3046 struct wifi_data *wifi = g_supplicant_interface_get_data(interface);
3049 wifi->disconnect_code = reasoncode;
3053 static void assoc_status_code(GSupplicantInterface *interface, int status_code)
3055 struct wifi_data *wifi = g_supplicant_interface_get_data(interface);
3058 wifi->assoc_code = status_code;
3062 static const GSupplicantCallbacks callbacks = {
3063 .system_ready = system_ready,
3064 .system_killed = system_killed,
3065 .interface_added = interface_added,
3066 .interface_state = interface_state,
3067 .interface_removed = interface_removed,
3068 .p2p_support = p2p_support,
3069 .scan_started = scan_started,
3070 .scan_finished = scan_finished,
3071 .ap_create_fail = ap_create_fail,
3072 .network_added = network_added,
3073 .network_removed = network_removed,
3074 .network_changed = network_changed,
3075 .network_associated = network_associated,
3076 .peer_found = peer_found,
3077 .peer_lost = peer_lost,
3078 .peer_changed = peer_changed,
3079 .peer_request = peer_request,
3081 .disconnect_reasoncode = disconnect_reasoncode,
3082 .assoc_status_code = assoc_status_code,
3086 static int tech_probe(struct connman_technology *technology)
3088 wifi_technology = technology;
3093 static void tech_remove(struct connman_technology *technology)
3095 wifi_technology = NULL;
3098 static GSupplicantSSID *ssid_ap_init(const char *ssid, const char *passphrase)
3100 GSupplicantSSID *ap;
3102 ap = g_try_malloc0(sizeof(GSupplicantSSID));
3106 ap->mode = G_SUPPLICANT_MODE_MASTER;
3108 ap->ssid_len = strlen(ssid);
3112 if (!passphrase || strlen(passphrase) == 0) {
3113 ap->security = G_SUPPLICANT_SECURITY_NONE;
3114 ap->passphrase = NULL;
3116 ap->security = G_SUPPLICANT_SECURITY_PSK;
3117 ap->protocol = G_SUPPLICANT_PROTO_RSN;
3118 ap->pairwise_cipher = G_SUPPLICANT_PAIRWISE_CCMP;
3119 ap->group_cipher = G_SUPPLICANT_GROUP_CCMP;
3120 ap->passphrase = passphrase;
3126 static void ap_start_callback(int result, GSupplicantInterface *interface,
3129 struct wifi_tethering_info *info = user_data;
3131 DBG("result %d index %d bridge %s",
3132 result, info->wifi->index, info->wifi->bridge);
3134 if ((result < 0) || (info->wifi->ap_supported != WIFI_AP_SUPPORTED)) {
3135 connman_inet_remove_from_bridge(info->wifi->index,
3136 info->wifi->bridge);
3138 if (info->wifi->ap_supported == WIFI_AP_SUPPORTED) {
3139 connman_technology_tethering_notify(info->technology, false);
3140 g_free(info->wifi->tethering_param->ssid);
3141 g_free(info->wifi->tethering_param);
3142 info->wifi->tethering_param = NULL;
3146 g_free(info->ifname);
3150 static void ap_create_callback(int result,
3151 GSupplicantInterface *interface,
3154 struct wifi_tethering_info *info = user_data;
3156 DBG("result %d ifname %s", result,
3157 g_supplicant_interface_get_ifname(interface));
3159 if ((result < 0) || (info->wifi->ap_supported != WIFI_AP_SUPPORTED)) {
3160 connman_inet_remove_from_bridge(info->wifi->index,
3161 info->wifi->bridge);
3163 if (info->wifi->ap_supported == WIFI_AP_SUPPORTED) {
3164 connman_technology_tethering_notify(info->technology, false);
3165 g_free(info->wifi->tethering_param->ssid);
3166 g_free(info->wifi->tethering_param);
3167 info->wifi->tethering_param = NULL;
3171 g_free(info->ifname);
3177 info->wifi->interface = interface;
3178 g_supplicant_interface_set_data(interface, info->wifi);
3180 if (g_supplicant_interface_set_apscan(interface, 2) < 0)
3181 connman_error("Failed to set interface ap_scan property");
3183 g_supplicant_interface_connect(interface, info->ssid,
3184 ap_start_callback, info);
3187 static void sta_remove_callback(int result,
3188 GSupplicantInterface *interface,
3191 struct wifi_tethering_info *info = user_data;
3192 const char *driver = connman_option_get_string("wifi");
3194 DBG("ifname %s result %d ", info->ifname, result);
3196 if ((result < 0) || (info->wifi->ap_supported != WIFI_AP_SUPPORTED)) {
3197 info->wifi->tethering = false;
3198 connman_technology_tethering_notify(info->technology, false);
3200 g_free(info->ifname);
3204 if (info->wifi->ap_supported == WIFI_AP_SUPPORTED) {
3205 g_free(info->wifi->tethering_param->ssid);
3206 g_free(info->wifi->tethering_param);
3207 info->wifi->tethering_param = NULL;
3212 info->wifi->interface = NULL;
3214 g_supplicant_interface_create(info->ifname, driver, info->wifi->bridge,
3219 static int enable_wifi_tethering(struct connman_technology *technology,
3220 const char *bridge, const char *identifier,
3221 const char *passphrase, bool available)
3224 GSupplicantInterface *interface;
3225 struct wifi_data *wifi;
3226 struct wifi_tethering_info *info;
3231 for (list = iface_list; list; list = list->next) {
3234 DBG("wifi %p network %p pending_network %p", wifi,
3235 wifi->network, wifi->pending_network);
3237 interface = wifi->interface;
3242 ifname = g_supplicant_interface_get_ifname(wifi->interface);
3246 if (wifi->ap_supported == WIFI_AP_NOT_SUPPORTED) {
3247 DBG("%s does not support AP mode (detected)", ifname);
3251 mode = g_supplicant_interface_get_mode(interface);
3252 if ((mode & G_SUPPLICANT_CAPABILITY_MODE_AP) == 0) {
3253 wifi->ap_supported = WIFI_AP_NOT_SUPPORTED;
3254 DBG("%s does not support AP mode (capability)", ifname);
3258 if (wifi->network && available)
3261 info = g_try_malloc0(sizeof(struct wifi_tethering_info));
3265 wifi->tethering_param = g_try_malloc0(sizeof(struct wifi_tethering_info));
3266 if (!wifi->tethering_param) {
3272 info->technology = technology;
3273 info->wifi->bridge = bridge;
3274 info->ssid = ssid_ap_init(identifier, passphrase);
3278 info->ifname = g_strdup(ifname);
3280 wifi->tethering_param->technology = technology;
3281 wifi->tethering_param->ssid = ssid_ap_init(identifier, passphrase);
3282 if (!wifi->tethering_param->ssid)
3285 info->wifi->tethering = true;
3286 info->wifi->ap_supported = WIFI_AP_SUPPORTED;
3288 berr = connman_technology_tethering_notify(technology, true);
3292 err = g_supplicant_interface_remove(interface,
3293 sta_remove_callback,
3296 DBG("tethering wifi %p ifname %s", wifi, ifname);
3301 g_free(info->ifname);
3304 g_free(wifi->tethering_param);
3305 wifi->tethering_param = NULL;
3308 * Remove bridge if it was correctly created but remove
3309 * operation failed. Instead, if bridge creation failed then
3310 * break out and do not try again on another interface,
3311 * bridge set-up does not depend on it.
3314 connman_technology_tethering_notify(technology, false);
3322 static int tech_set_tethering(struct connman_technology *technology,
3323 const char *identifier, const char *passphrase,
3324 const char *bridge, bool enabled)
3327 struct wifi_data *wifi;
3333 for (list = iface_list; list; list = list->next) {
3336 if (wifi->tethering) {
3337 wifi->tethering = false;
3339 connman_inet_remove_from_bridge(wifi->index,
3341 wifi->bridged = false;
3345 connman_technology_tethering_notify(technology, false);
3350 DBG("trying tethering for available devices");
3351 err = enable_wifi_tethering(technology, bridge, identifier, passphrase,
3355 DBG("trying tethering for any device");
3356 err = enable_wifi_tethering(technology, bridge, identifier,
3363 static void regdom_callback(int result, const char *alpha2, void *user_data)
3367 if (!wifi_technology)
3373 connman_technology_regdom_notify(wifi_technology, alpha2);
3376 static int tech_set_regdom(struct connman_technology *technology, const char *alpha2)
3378 return g_supplicant_set_country(alpha2, regdom_callback, NULL);
3381 static struct connman_technology_driver tech_driver = {
3383 .type = CONNMAN_SERVICE_TYPE_WIFI,
3384 .probe = tech_probe,
3385 .remove = tech_remove,
3386 .set_tethering = tech_set_tethering,
3387 .set_regdom = tech_set_regdom,
3390 static int wifi_init(void)
3394 err = connman_network_driver_register(&network_driver);
3398 err = g_supplicant_register(&callbacks);
3400 connman_network_driver_unregister(&network_driver);
3404 err = connman_technology_driver_register(&tech_driver);
3406 g_supplicant_unregister(&callbacks);
3407 connman_network_driver_unregister(&network_driver);
3414 static void wifi_exit(void)
3418 connman_technology_driver_unregister(&tech_driver);
3420 g_supplicant_unregister(&callbacks);
3422 connman_network_driver_unregister(&network_driver);
3425 CONNMAN_PLUGIN_DEFINE(wifi, "WiFi interface plugin", VERSION,
3426 CONNMAN_PLUGIN_PRIORITY_DEFAULT, wifi_init, wifi_exit)