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 static struct connman_technology *wifi_technology = NULL;
75 static struct connman_technology *p2p_technology = NULL;
77 struct hidden_params {
79 unsigned int ssid_len;
83 GSupplicantScanParams *scan_params;
88 * Used for autoscan "emulation".
89 * Should be removed when wpa_s autoscan support will be by default.
91 struct autoscan_params {
100 struct connman_device *device;
101 struct connman_network *network;
102 struct connman_network *pending_network;
104 GSupplicantInterface *interface;
105 GSupplicantState state;
110 bool interface_ready;
116 struct hidden_params *hidden;
117 bool postpone_hidden;
119 * autoscan "emulation".
121 struct autoscan_params *autoscan;
123 GSupplicantScanParams *scan_params;
124 unsigned int p2p_find_timeout;
125 unsigned int p2p_connection_timeout;
126 struct connman_peer *pending_peer;
127 GSupplicantPeer *peer;
131 #if defined TIZEN_EXT
132 int assoc_retry_count;
133 struct connman_network *scan_pending_network;
137 #if defined TIZEN_EXT
140 #define TIZEN_ASSOC_RETRY_COUNT 4
142 static gboolean wifi_first_scan = false;
143 static gboolean found_with_first_scan = false;
144 static gboolean is_wifi_notifier_registered = false;
147 static GList *iface_list = NULL;
149 static GList *pending_wifi_device = NULL;
150 static GList *p2p_iface_list = NULL;
151 bool wfd_service_registered = false;
153 static void start_autoscan(struct connman_device *device);
155 static int p2p_tech_probe(struct connman_technology *technology)
157 p2p_technology = technology;
162 static void p2p_tech_remove(struct connman_technology *technology)
164 p2p_technology = NULL;
167 static struct connman_technology_driver p2p_tech_driver = {
169 .type = CONNMAN_SERVICE_TYPE_P2P,
170 .probe = p2p_tech_probe,
171 .remove = p2p_tech_remove,
174 static bool is_p2p_connecting(void)
178 for (list = iface_list; list; list = list->next) {
179 struct wifi_data *wifi = list->data;
181 if (wifi->p2p_connecting)
188 static void add_pending_wifi_device(struct wifi_data *wifi)
190 if (g_list_find(pending_wifi_device, wifi))
193 pending_wifi_device = g_list_append(pending_wifi_device, wifi);
196 static struct wifi_data *get_pending_wifi_data(const char *ifname)
200 for (list = pending_wifi_device; list; list = list->next) {
201 struct wifi_data *wifi;
202 const char *dev_name;
205 if (!wifi || !wifi->device)
208 dev_name = connman_device_get_string(wifi->device, "Interface");
209 if (!g_strcmp0(ifname, dev_name)) {
210 pending_wifi_device = g_list_delete_link(
211 pending_wifi_device, list);
219 static void remove_pending_wifi_device(struct wifi_data *wifi)
223 link = g_list_find(pending_wifi_device, wifi);
228 pending_wifi_device = g_list_delete_link(pending_wifi_device, link);
231 static void peer_cancel_timeout(struct wifi_data *wifi)
233 if (wifi->p2p_connection_timeout > 0)
234 g_source_remove(wifi->p2p_connection_timeout);
236 wifi->p2p_connection_timeout = 0;
237 wifi->p2p_connecting = false;
239 if (wifi->pending_peer) {
240 connman_peer_unref(wifi->pending_peer);
241 wifi->pending_peer = NULL;
247 static gboolean peer_connect_timeout(gpointer data)
249 struct wifi_data *wifi = data;
253 if (wifi->p2p_connecting) {
254 enum connman_peer_state state = CONNMAN_PEER_STATE_FAILURE;
256 if (g_supplicant_peer_has_requested_connection(wifi->peer))
257 state = CONNMAN_PEER_STATE_IDLE;
259 connman_peer_set_state(wifi->pending_peer, state);
262 peer_cancel_timeout(wifi);
267 static void peer_connect_callback(int result, GSupplicantInterface *interface,
270 struct wifi_data *wifi = user_data;
271 struct connman_peer *peer = wifi->pending_peer;
273 DBG("peer %p - %d", peer, result);
279 peer_connect_timeout(wifi);
283 connman_peer_set_state(peer, CONNMAN_PEER_STATE_ASSOCIATION);
285 wifi->p2p_connection_timeout = g_timeout_add_seconds(
286 P2P_CONNECTION_TIMEOUT,
287 peer_connect_timeout, wifi);
290 static int peer_connect(struct connman_peer *peer,
291 enum connman_peer_wps_method wps_method,
294 struct connman_device *device = connman_peer_get_device(peer);
295 GSupplicantPeerParams *peer_params;
296 GSupplicantPeer *gs_peer;
297 struct wifi_data *wifi;
301 DBG("peer %p", peer);
306 wifi = connman_device_get_data(device);
310 if (wifi->p2p_connecting)
315 gs_peer = g_supplicant_interface_peer_lookup(wifi->interface,
316 connman_peer_get_identifier(peer));
320 pbc = g_supplicant_peer_is_wps_pbc(gs_peer);
321 pin = g_supplicant_peer_is_wps_pin(gs_peer);
323 switch (wps_method) {
324 case CONNMAN_PEER_WPS_UNKNOWN:
325 if ((pbc && pin) || pin)
328 case CONNMAN_PEER_WPS_PBC:
333 case CONNMAN_PEER_WPS_PIN:
334 if (!pin || !wps_pin)
339 peer_params = g_try_malloc0(sizeof(GSupplicantPeerParams));
343 peer_params->path = g_strdup(g_supplicant_peer_get_path(gs_peer));
345 peer_params->wps_pin = g_strdup(wps_pin);
347 peer_params->master = connman_peer_service_is_master();
349 ret = g_supplicant_interface_p2p_connect(wifi->interface, peer_params,
350 peer_connect_callback, wifi);
351 if (ret == -EINPROGRESS) {
352 wifi->pending_peer = connman_peer_ref(peer);
353 wifi->peer = gs_peer;
354 wifi->p2p_connecting = true;
361 static int peer_disconnect(struct connman_peer *peer)
363 struct connman_device *device = connman_peer_get_device(peer);
364 GSupplicantPeerParams peer_params = {};
365 GSupplicantPeer *gs_peer;
366 struct wifi_data *wifi;
369 DBG("peer %p", peer);
374 wifi = connman_device_get_data(device);
378 gs_peer = g_supplicant_interface_peer_lookup(wifi->interface,
379 connman_peer_get_identifier(peer));
383 peer_params.path = g_strdup(g_supplicant_peer_get_path(gs_peer));
385 ret = g_supplicant_interface_p2p_disconnect(wifi->interface,
387 g_free(peer_params.path);
389 if (ret == -EINPROGRESS)
390 peer_cancel_timeout(wifi);
395 struct peer_service_registration {
396 peer_service_registration_cb_t callback;
400 static bool is_service_wfd(const unsigned char *specs, int length)
402 if (length < 9 || specs[0] != 0 || specs[1] != 0 || specs[2] != 6)
408 static void apply_p2p_listen_on_iface(gpointer data, gpointer user_data)
410 struct wifi_data *wifi = data;
412 if (!wifi->interface ||
413 !g_supplicant_interface_has_p2p(wifi->interface))
416 if (!wifi->servicing) {
417 g_supplicant_interface_p2p_listen(wifi->interface,
418 P2P_LISTEN_PERIOD, P2P_LISTEN_INTERVAL);
424 static void register_wfd_service_cb(int result,
425 GSupplicantInterface *iface, void *user_data)
427 struct peer_service_registration *reg_data = user_data;
432 g_list_foreach(iface_list, apply_p2p_listen_on_iface, NULL);
434 if (reg_data && reg_data->callback) {
435 reg_data->callback(result, reg_data->user_data);
440 static GSupplicantP2PServiceParams *fill_in_peer_service_params(
441 const unsigned char *spec,
442 int spec_length, const unsigned char *query,
443 int query_length, int version)
445 GSupplicantP2PServiceParams *params;
447 params = g_try_malloc0(sizeof(GSupplicantP2PServiceParams));
452 params->version = version;
453 params->service = g_memdup(spec, spec_length);
454 } else if (query_length > 0 && spec_length > 0) {
455 params->query = g_memdup(query, query_length);
456 params->query_length = query_length;
458 params->response = g_memdup(spec, spec_length);
459 params->response_length = spec_length;
461 params->wfd_ies = g_memdup(spec, spec_length);
462 params->wfd_ies_length = spec_length;
468 static void free_peer_service_params(GSupplicantP2PServiceParams *params)
473 g_free(params->service);
474 g_free(params->query);
475 g_free(params->response);
476 g_free(params->wfd_ies);
481 static int peer_register_wfd_service(const unsigned char *specification,
482 int specification_length,
483 peer_service_registration_cb_t callback,
486 struct peer_service_registration *reg_data = NULL;
487 static GSupplicantP2PServiceParams *params;
492 if (wfd_service_registered)
495 params = fill_in_peer_service_params(specification,
496 specification_length, NULL, 0, 0);
500 reg_data = g_try_malloc0(sizeof(*reg_data));
506 reg_data->callback = callback;
507 reg_data->user_data = user_data;
509 ret = g_supplicant_set_widi_ies(params,
510 register_wfd_service_cb, reg_data);
511 if (ret < 0 && ret != -EINPROGRESS)
514 wfd_service_registered = true;
518 free_peer_service_params(params);
524 static void register_peer_service_cb(int result,
525 GSupplicantInterface *iface, void *user_data)
527 struct wifi_data *wifi = g_supplicant_interface_get_data(iface);
528 struct peer_service_registration *reg_data = user_data;
533 apply_p2p_listen_on_iface(wifi, NULL);
535 if (reg_data->callback)
536 reg_data->callback(result, reg_data->user_data);
541 static int peer_register_service(const unsigned char *specification,
542 int specification_length,
543 const unsigned char *query,
544 int query_length, int version,
545 peer_service_registration_cb_t callback,
548 struct peer_service_registration *reg_data;
549 GSupplicantP2PServiceParams *params;
556 if (specification && !version && !query &&
557 is_service_wfd(specification, specification_length)) {
558 return peer_register_wfd_service(specification,
559 specification_length, callback, user_data);
562 reg_data = g_try_malloc0(sizeof(*reg_data));
566 reg_data->callback = callback;
567 reg_data->user_data = user_data;
571 for (list = iface_list; list; list = list->next) {
572 struct wifi_data *wifi = list->data;
573 GSupplicantInterface *iface = wifi->interface;
575 if (!g_supplicant_interface_has_p2p(iface))
578 params = fill_in_peer_service_params(specification,
579 specification_length, query,
580 query_length, version);
587 ret_f = g_supplicant_interface_p2p_add_service(iface,
588 register_peer_service_cb, params, reg_data);
589 if (ret_f == 0 || ret_f == -EINPROGRESS)
593 ret = g_supplicant_interface_p2p_add_service(iface,
594 register_peer_service_cb, params, NULL);
595 if (ret != 0 && ret != -EINPROGRESS)
596 free_peer_service_params(params);
599 if (ret_f != 0 && ret_f != -EINPROGRESS)
605 static int peer_unregister_wfd_service(void)
607 GSupplicantP2PServiceParams *params;
610 if (!wfd_service_registered)
613 params = fill_in_peer_service_params(NULL, 0, NULL, 0, 0);
617 wfd_service_registered = false;
619 g_supplicant_set_widi_ies(params, NULL, NULL);
621 for (list = iface_list; list; list = list->next) {
622 struct wifi_data *wifi = list->data;
624 if (!g_supplicant_interface_has_p2p(wifi->interface))
628 if (!wifi->servicing || wifi->servicing < 0) {
629 g_supplicant_interface_p2p_listen(wifi->interface,
638 static int peer_unregister_service(const unsigned char *specification,
639 int specification_length,
640 const unsigned char *query,
641 int query_length, int version)
643 GSupplicantP2PServiceParams *params;
648 if (specification && !version && !query &&
649 is_service_wfd(specification, specification_length)) {
650 ret = peer_unregister_wfd_service();
651 if (ret != 0 && ret != -EINPROGRESS)
656 for (list = iface_list; list; list = list->next) {
657 struct wifi_data *wifi = list->data;
658 GSupplicantInterface *iface = wifi->interface;
663 if (!g_supplicant_interface_has_p2p(iface))
666 params = fill_in_peer_service_params(specification,
667 specification_length, query,
668 query_length, version);
674 ret = g_supplicant_interface_p2p_del_service(iface, params);
675 if (ret != 0 && ret != -EINPROGRESS)
676 free_peer_service_params(params);
679 if (!wifi->servicing || wifi->servicing < 0) {
680 g_supplicant_interface_p2p_listen(iface, 0, 0);
688 static struct connman_peer_driver peer_driver = {
689 .connect = peer_connect,
690 .disconnect = peer_disconnect,
691 .register_service = peer_register_service,
692 .unregister_service = peer_unregister_service,
695 static void handle_tethering(struct wifi_data *wifi)
697 if (!wifi->tethering)
706 DBG("index %d bridge %s", wifi->index, wifi->bridge);
708 if (connman_inet_add_to_bridge(wifi->index, wifi->bridge) < 0)
711 wifi->bridged = true;
714 static void wifi_newlink(unsigned flags, unsigned change, void *user_data)
716 struct connman_device *device = user_data;
717 struct wifi_data *wifi = connman_device_get_data(device);
722 DBG("index %d flags %d change %d", wifi->index, flags, change);
724 if ((wifi->flags & IFF_UP) != (flags & IFF_UP)) {
728 DBG("interface down");
731 if ((wifi->flags & IFF_LOWER_UP) != (flags & IFF_LOWER_UP)) {
732 if (flags & IFF_LOWER_UP) {
735 handle_tethering(wifi);
743 static int wifi_probe(struct connman_device *device)
745 struct wifi_data *wifi;
747 DBG("device %p", device);
749 wifi = g_try_new0(struct wifi_data, 1);
753 wifi->state = G_SUPPLICANT_STATE_INACTIVE;
755 connman_device_set_data(device, wifi);
756 wifi->device = connman_device_ref(device);
758 wifi->index = connman_device_get_index(device);
761 wifi->watch = connman_rtnl_add_newlink_watch(wifi->index,
762 wifi_newlink, device);
763 if (is_p2p_connecting())
764 add_pending_wifi_device(wifi);
766 iface_list = g_list_append(iface_list, wifi);
771 static void remove_networks(struct connman_device *device,
772 struct wifi_data *wifi)
776 for (list = wifi->networks; list; list = list->next) {
777 struct connman_network *network = list->data;
779 connman_device_remove_network(device, network);
780 connman_network_unref(network);
783 g_slist_free(wifi->networks);
784 wifi->networks = NULL;
787 static void reset_autoscan(struct connman_device *device)
789 struct wifi_data *wifi = connman_device_get_data(device);
790 struct autoscan_params *autoscan;
794 if (!wifi || !wifi->autoscan)
797 autoscan = wifi->autoscan;
799 if (autoscan->timeout == 0 && autoscan->interval == 0)
802 g_source_remove(autoscan->timeout);
804 autoscan->timeout = 0;
805 autoscan->interval = 0;
807 connman_device_unref(device);
810 static void stop_autoscan(struct connman_device *device)
812 const struct wifi_data *wifi = connman_device_get_data(device);
814 if (!wifi || !wifi->autoscan)
817 reset_autoscan(device);
819 connman_device_set_scanning(device, CONNMAN_SERVICE_TYPE_WIFI, false);
822 static void check_p2p_technology(void)
824 bool p2p_exists = false;
827 for (list = iface_list; list; list = list->next) {
828 struct wifi_data *w = list->data;
831 g_supplicant_interface_has_p2p(w->interface))
836 connman_technology_driver_unregister(&p2p_tech_driver);
837 connman_peer_driver_unregister(&peer_driver);
841 static void wifi_remove(struct connman_device *device)
843 struct wifi_data *wifi = connman_device_get_data(device);
845 DBG("device %p wifi %p", device, wifi);
850 stop_autoscan(device);
852 if (wifi->p2p_device)
853 p2p_iface_list = g_list_remove(p2p_iface_list, wifi);
855 iface_list = g_list_remove(iface_list, wifi);
857 check_p2p_technology();
859 remove_pending_wifi_device(wifi);
861 if (wifi->p2p_find_timeout) {
862 g_source_remove(wifi->p2p_find_timeout);
863 connman_device_unref(wifi->device);
866 if (wifi->p2p_connection_timeout)
867 g_source_remove(wifi->p2p_connection_timeout);
869 remove_networks(device, wifi);
871 connman_device_set_powered(device, false);
872 connman_device_set_data(device, NULL);
873 connman_device_unref(wifi->device);
874 connman_rtnl_remove_watch(wifi->watch);
876 g_supplicant_interface_set_data(wifi->interface, NULL);
878 g_supplicant_interface_cancel(wifi->interface);
880 if (wifi->scan_params)
881 g_supplicant_free_scan_params(wifi->scan_params);
883 g_free(wifi->autoscan);
884 g_free(wifi->identifier);
888 static bool is_duplicate(GSList *list, gchar *ssid, int ssid_len)
892 for (iter = list; iter; iter = g_slist_next(iter)) {
893 struct scan_ssid *scan_ssid = iter->data;
895 if (ssid_len == scan_ssid->ssid_len &&
896 memcmp(ssid, scan_ssid->ssid, ssid_len) == 0)
903 static int add_scan_param(gchar *hex_ssid, char *raw_ssid, int ssid_len,
904 int freq, GSupplicantScanParams *scan_data,
905 int driver_max_scan_ssids, char *ssid_name)
908 struct scan_ssid *scan_ssid;
910 if ((driver_max_scan_ssids == 0 ||
911 driver_max_scan_ssids > scan_data->num_ssids) &&
912 (hex_ssid || raw_ssid)) {
914 unsigned int j = 0, hex;
917 size_t hex_ssid_len = strlen(hex_ssid);
919 ssid = g_try_malloc0(hex_ssid_len / 2);
923 for (i = 0; i < hex_ssid_len; i += 2) {
924 sscanf(hex_ssid + i, "%02x", &hex);
933 * If we have already added hidden AP to the list,
934 * then do not do it again. This might happen if you have
935 * used or are using multiple wifi cards, so in that case
936 * you might have multiple service files for same AP.
938 if (is_duplicate(scan_data->ssids, ssid, j)) {
944 scan_ssid = g_try_new(struct scan_ssid, 1);
951 memcpy(scan_ssid->ssid, ssid, j);
952 scan_ssid->ssid_len = j;
953 scan_data->ssids = g_slist_prepend(scan_data->ssids,
956 scan_data->num_ssids++;
958 DBG("SSID %s added to scanned list of %d entries", ssid_name,
959 scan_data->num_ssids);
966 scan_data->ssids = g_slist_reverse(scan_data->ssids);
968 if (!scan_data->freqs) {
969 scan_data->freqs = g_try_malloc0(sizeof(uint16_t));
970 if (!scan_data->freqs) {
971 g_slist_free_full(scan_data->ssids, g_free);
975 scan_data->num_freqs = 1;
976 scan_data->freqs[0] = freq;
978 bool duplicate = false;
980 /* Don't add duplicate entries */
981 for (i = 0; i < scan_data->num_freqs; i++) {
982 if (scan_data->freqs[i] == freq) {
989 scan_data->num_freqs++;
990 scan_data->freqs = g_try_realloc(scan_data->freqs,
991 sizeof(uint16_t) * scan_data->num_freqs);
992 if (!scan_data->freqs) {
993 g_slist_free_full(scan_data->ssids, g_free);
996 scan_data->freqs[scan_data->num_freqs - 1] = freq;
1003 static int get_hidden_connections(GSupplicantScanParams *scan_data)
1005 struct connman_config_entry **entries;
1011 int num_ssids = 0, add_param_failed = 0;
1013 services = connman_storage_get_services();
1014 for (i = 0; services && services[i]; i++) {
1015 if (strncmp(services[i], "wifi_", 5) != 0)
1018 keyfile = connman_storage_load_service(services[i]);
1022 value = g_key_file_get_boolean(keyfile,
1023 services[i], "Hidden", NULL);
1025 g_key_file_free(keyfile);
1029 value = g_key_file_get_boolean(keyfile,
1030 services[i], "Favorite", NULL);
1032 g_key_file_free(keyfile);
1036 #if defined TIZEN_EXT
1037 value = g_key_file_get_boolean(keyfile,
1038 services[i], "AutoConnect", NULL);
1040 g_key_file_free(keyfile);
1045 ssid = g_key_file_get_string(keyfile,
1046 services[i], "SSID", NULL);
1048 name = g_key_file_get_string(keyfile, services[i], "Name",
1051 ret = add_scan_param(ssid, NULL, 0, 0, scan_data, 0, name);
1059 g_key_file_free(keyfile);
1063 * Check if there are any hidden AP that needs to be provisioned.
1065 entries = connman_config_get_entries("wifi");
1066 for (i = 0; entries && entries[i]; i++) {
1069 if (!entries[i]->hidden)
1072 if (!entries[i]->ssid) {
1073 ssid = entries[i]->name;
1076 ssid = entries[i]->ssid;
1077 len = entries[i]->ssid_len;
1083 ret = add_scan_param(NULL, ssid, len, 0, scan_data, 0, ssid);
1090 connman_config_free_entries(entries);
1092 if (add_param_failed > 0)
1093 DBG("Unable to scan %d out of %d SSIDs",
1094 add_param_failed, num_ssids);
1096 g_strfreev(services);
1101 static int get_hidden_connections_params(struct wifi_data *wifi,
1102 GSupplicantScanParams *scan_params)
1104 int driver_max_ssids, i;
1105 GSupplicantScanParams *orig_params;
1108 * Scan hidden networks so that we can autoconnect to them.
1109 * We will assume 1 as a default number of ssid to scan.
1111 driver_max_ssids = g_supplicant_interface_get_max_scan_ssids(
1113 if (driver_max_ssids == 0)
1114 driver_max_ssids = 1;
1116 DBG("max ssids %d", driver_max_ssids);
1118 if (!wifi->scan_params) {
1119 wifi->scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
1120 if (!wifi->scan_params)
1123 if (get_hidden_connections(wifi->scan_params) == 0) {
1124 g_supplicant_free_scan_params(wifi->scan_params);
1125 wifi->scan_params = NULL;
1131 orig_params = wifi->scan_params;
1133 /* Let's transfer driver_max_ssids params */
1134 for (i = 0; i < driver_max_ssids; i++) {
1135 struct scan_ssid *ssid;
1137 if (!wifi->scan_params->ssids)
1140 ssid = orig_params->ssids->data;
1141 orig_params->ssids = g_slist_remove(orig_params->ssids, ssid);
1142 scan_params->ssids = g_slist_prepend(scan_params->ssids, ssid);
1146 scan_params->num_ssids = i;
1147 scan_params->ssids = g_slist_reverse(scan_params->ssids);
1149 scan_params->freqs = g_memdup(orig_params->freqs,
1150 sizeof(uint16_t) * orig_params->num_freqs);
1151 if (!scan_params->freqs)
1154 scan_params->num_freqs = orig_params->num_freqs;
1159 orig_params->num_ssids -= scan_params->num_ssids;
1161 return scan_params->num_ssids;
1164 g_slist_free_full(scan_params->ssids, g_free);
1165 g_supplicant_free_scan_params(wifi->scan_params);
1166 wifi->scan_params = NULL;
1171 static int throw_wifi_scan(struct connman_device *device,
1172 GSupplicantInterfaceCallback callback)
1174 struct wifi_data *wifi = connman_device_get_data(device);
1180 DBG("device %p %p", device, wifi->interface);
1182 if (wifi->tethering)
1185 if (connman_device_get_scanning(device))
1188 connman_device_ref(device);
1190 ret = g_supplicant_interface_scan(wifi->interface, NULL,
1193 connman_device_set_scanning(device,
1194 CONNMAN_SERVICE_TYPE_WIFI, true);
1196 connman_device_unref(device);
1201 static void hidden_free(struct hidden_params *hidden)
1206 if (hidden->scan_params)
1207 g_supplicant_free_scan_params(hidden->scan_params);
1208 g_free(hidden->identity);
1209 g_free(hidden->passphrase);
1210 g_free(hidden->security);
1214 #if defined TIZEN_EXT
1215 static void service_state_changed(struct connman_service *service,
1216 enum connman_service_state state);
1218 static int network_connect(struct connman_network *network);
1220 static struct connman_notifier notifier = {
1222 .priority = CONNMAN_NOTIFIER_PRIORITY_DEFAULT,
1223 .service_state_changed = service_state_changed,
1226 static void service_state_changed(struct connman_service *service,
1227 enum connman_service_state state)
1229 enum connman_service_type type;
1231 type = connman_service_get_type(service);
1232 if (type != CONNMAN_SERVICE_TYPE_WIFI)
1235 DBG("service %p state %d", service, state);
1238 case CONNMAN_SERVICE_STATE_READY:
1239 case CONNMAN_SERVICE_STATE_ONLINE:
1240 case CONNMAN_SERVICE_STATE_FAILURE:
1241 connman_notifier_unregister(¬ifier);
1242 is_wifi_notifier_registered = FALSE;
1244 __connman_device_request_scan(type);
1253 static void scan_callback(int result, GSupplicantInterface *interface,
1256 struct connman_device *device = user_data;
1257 struct wifi_data *wifi = connman_device_get_data(device);
1260 DBG("result %d wifi %p", result, wifi);
1263 if (wifi->hidden && !wifi->postpone_hidden) {
1264 connman_network_clear_hidden(wifi->hidden->user_data);
1265 hidden_free(wifi->hidden);
1266 wifi->hidden = NULL;
1269 if (wifi->scan_params) {
1270 g_supplicant_free_scan_params(wifi->scan_params);
1271 wifi->scan_params = NULL;
1276 connman_device_reset_scanning(device);
1278 /* User is connecting to a hidden AP, let's wait for finished event */
1279 if (wifi && wifi->hidden && wifi->postpone_hidden) {
1280 GSupplicantScanParams *scan_params;
1283 wifi->postpone_hidden = false;
1284 scan_params = wifi->hidden->scan_params;
1285 wifi->hidden->scan_params = NULL;
1287 reset_autoscan(device);
1289 ret = g_supplicant_interface_scan(wifi->interface, scan_params,
1290 scan_callback, device);
1294 /* On error, let's recall scan_callback, which will cleanup */
1295 return scan_callback(ret, interface, user_data);
1298 scanning = connman_device_get_scanning(device);
1301 connman_device_set_scanning(device,
1302 CONNMAN_SERVICE_TYPE_WIFI, false);
1305 if (result != -ENOLINK)
1306 #if defined TIZEN_EXT
1309 start_autoscan(device);
1312 * If we are here then we were scanning; however, if we are
1313 * also mid-flight disabling the interface, then wifi_disable
1314 * has already cleared the device scanning state and
1315 * unreferenced the device, obviating the need to do it here.
1319 connman_device_unref(device);
1321 #if defined TIZEN_EXT
1322 if (wifi && wifi->scan_pending_network && result != -EIO) {
1323 network_connect(wifi->scan_pending_network);
1324 wifi->scan_pending_network = NULL;
1325 connman_network_set_connecting(wifi->network);
1328 if (is_wifi_notifier_registered != true &&
1329 wifi_first_scan == true && found_with_first_scan == true) {
1330 wifi_first_scan = false;
1331 found_with_first_scan = false;
1333 connman_notifier_register(¬ifier);
1334 is_wifi_notifier_registered = true;
1339 static void scan_callback_hidden(int result,
1340 GSupplicantInterface *interface, void *user_data)
1342 struct connman_device *device = user_data;
1343 struct wifi_data *wifi = connman_device_get_data(device);
1344 GSupplicantScanParams *scan_params;
1347 DBG("result %d wifi %p", result, wifi);
1352 /* User is trying to connect to a hidden AP */
1353 if (wifi->hidden && wifi->postpone_hidden)
1356 scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
1360 if (get_hidden_connections_params(wifi, scan_params) > 0) {
1361 ret = g_supplicant_interface_scan(wifi->interface,
1363 scan_callback_hidden,
1369 g_supplicant_free_scan_params(scan_params);
1372 scan_callback(result, interface, user_data);
1375 static gboolean autoscan_timeout(gpointer data)
1377 struct connman_device *device = data;
1378 struct wifi_data *wifi = connman_device_get_data(device);
1379 struct autoscan_params *autoscan;
1385 autoscan = wifi->autoscan;
1387 if (autoscan->interval <= 0) {
1388 interval = autoscan->base;
1391 interval = autoscan->interval * autoscan->base;
1393 #if defined TIZEN_EXT
1394 if (autoscan->interval >= autoscan->limit)
1396 if (interval > autoscan->limit)
1398 interval = autoscan->limit;
1400 throw_wifi_scan(wifi->device, scan_callback_hidden);
1403 DBG("interval %d", interval);
1405 autoscan->interval = interval;
1407 autoscan->timeout = g_timeout_add_seconds(interval,
1408 autoscan_timeout, device);
1413 static void start_autoscan(struct connman_device *device)
1415 struct wifi_data *wifi = connman_device_get_data(device);
1416 struct autoscan_params *autoscan;
1423 if (wifi->p2p_device)
1426 if (wifi->connected)
1429 autoscan = wifi->autoscan;
1433 if (autoscan->timeout > 0 || autoscan->interval > 0)
1436 connman_device_ref(device);
1438 autoscan_timeout(device);
1441 static struct autoscan_params *parse_autoscan_params(const char *params)
1443 struct autoscan_params *autoscan;
1448 DBG("Emulating autoscan");
1450 list_params = g_strsplit(params, ":", 0);
1451 if (list_params == 0)
1454 if (g_strv_length(list_params) < 3) {
1455 g_strfreev(list_params);
1459 base = atoi(list_params[1]);
1460 limit = atoi(list_params[2]);
1462 g_strfreev(list_params);
1464 autoscan = g_try_malloc0(sizeof(struct autoscan_params));
1466 DBG("Could not allocate memory for autoscan");
1470 DBG("base %d - limit %d", base, limit);
1471 autoscan->base = base;
1472 autoscan->limit = limit;
1477 static void setup_autoscan(struct wifi_data *wifi)
1479 if (!wifi->autoscan)
1480 wifi->autoscan = parse_autoscan_params(AUTOSCAN_DEFAULT);
1482 start_autoscan(wifi->device);
1485 static void finalize_interface_creation(struct wifi_data *wifi)
1487 DBG("interface is ready wifi %p tethering %d", wifi, wifi->tethering);
1489 if (!wifi->device) {
1490 connman_error("WiFi device not set");
1494 connman_device_set_powered(wifi->device, true);
1496 if (!connman_setting_get_bool("BackgroundScanning"))
1499 if (wifi->p2p_device)
1502 setup_autoscan(wifi);
1505 static void interface_create_callback(int result,
1506 GSupplicantInterface *interface,
1509 struct wifi_data *wifi = user_data;
1511 DBG("result %d ifname %s, wifi %p", result,
1512 g_supplicant_interface_get_ifname(interface),
1515 if (result < 0 || !wifi)
1518 wifi->interface = interface;
1519 g_supplicant_interface_set_data(interface, wifi);
1521 if (g_supplicant_interface_get_ready(interface)) {
1522 wifi->interface_ready = true;
1523 finalize_interface_creation(wifi);
1527 static int wifi_enable(struct connman_device *device)
1529 struct wifi_data *wifi = connman_device_get_data(device);
1532 const char *driver = connman_option_get_string("wifi");
1535 DBG("device %p %p", device, wifi);
1537 index = connman_device_get_index(device);
1538 if (!wifi || index < 0)
1541 if (is_p2p_connecting())
1542 return -EINPROGRESS;
1544 interface = connman_inet_ifname(index);
1545 ret = g_supplicant_interface_create(interface, driver, NULL,
1546 interface_create_callback,
1553 return -EINPROGRESS;
1556 static int wifi_disable(struct connman_device *device)
1558 struct wifi_data *wifi = connman_device_get_data(device);
1561 DBG("device %p wifi %p", device, wifi);
1566 wifi->connected = false;
1567 wifi->disconnecting = false;
1569 if (wifi->pending_network)
1570 wifi->pending_network = NULL;
1572 stop_autoscan(device);
1574 if (wifi->p2p_find_timeout) {
1575 g_source_remove(wifi->p2p_find_timeout);
1576 wifi->p2p_find_timeout = 0;
1577 connman_device_set_scanning(device, CONNMAN_SERVICE_TYPE_P2P, false);
1578 connman_device_unref(wifi->device);
1581 /* In case of a user scan, device is still referenced */
1582 if (connman_device_get_scanning(device)) {
1583 connman_device_set_scanning(device,
1584 CONNMAN_SERVICE_TYPE_WIFI, false);
1585 connman_device_unref(wifi->device);
1588 remove_networks(device, wifi);
1590 #if defined TIZEN_EXT
1591 wifi->scan_pending_network = NULL;
1593 if (is_wifi_notifier_registered == true) {
1594 connman_notifier_unregister(¬ifier);
1595 is_wifi_notifier_registered = false;
1599 ret = g_supplicant_interface_remove(wifi->interface, NULL, NULL);
1603 return -EINPROGRESS;
1606 struct last_connected {
1612 static gint sort_entry(gconstpointer a, gconstpointer b, gpointer user_data)
1614 GTimeVal *aval = (GTimeVal *)a;
1615 GTimeVal *bval = (GTimeVal *)b;
1617 /* Note that the sort order is descending */
1618 if (aval->tv_sec < bval->tv_sec)
1621 if (aval->tv_sec > bval->tv_sec)
1627 static void free_entry(gpointer data)
1629 struct last_connected *entry = data;
1631 g_free(entry->ssid);
1635 static int get_latest_connections(int max_ssids,
1636 GSupplicantScanParams *scan_data)
1638 GSequenceIter *iter;
1639 GSequence *latest_list;
1640 struct last_connected *entry;
1649 latest_list = g_sequence_new(free_entry);
1653 services = connman_storage_get_services();
1654 for (i = 0; services && services[i]; i++) {
1655 if (strncmp(services[i], "wifi_", 5) != 0)
1658 keyfile = connman_storage_load_service(services[i]);
1662 str = g_key_file_get_string(keyfile,
1663 services[i], "Favorite", NULL);
1664 if (!str || g_strcmp0(str, "true")) {
1666 g_key_file_free(keyfile);
1671 str = g_key_file_get_string(keyfile,
1672 services[i], "AutoConnect", 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], "Modified", NULL);
1683 g_key_file_free(keyfile);
1686 g_time_val_from_iso8601(str, &modified);
1689 ssid = g_key_file_get_string(keyfile,
1690 services[i], "SSID", NULL);
1692 freq = g_key_file_get_integer(keyfile, services[i],
1695 entry = g_try_new(struct last_connected, 1);
1697 g_sequence_free(latest_list);
1698 g_key_file_free(keyfile);
1704 entry->modified = modified;
1707 g_sequence_insert_sorted(latest_list, entry,
1713 g_key_file_free(keyfile);
1716 g_strfreev(services);
1718 num_ssids = num_ssids > max_ssids ? max_ssids : num_ssids;
1720 iter = g_sequence_get_begin_iter(latest_list);
1722 for (i = 0; i < num_ssids; i++) {
1723 entry = g_sequence_get(iter);
1725 DBG("ssid %s freq %d modified %lu", entry->ssid, entry->freq,
1726 entry->modified.tv_sec);
1728 add_scan_param(entry->ssid, NULL, 0, entry->freq, scan_data,
1729 max_ssids, entry->ssid);
1731 iter = g_sequence_iter_next(iter);
1734 g_sequence_free(latest_list);
1738 static int wifi_scan_simple(struct connman_device *device)
1740 reset_autoscan(device);
1742 return throw_wifi_scan(device, scan_callback_hidden);
1745 static gboolean p2p_find_stop(gpointer data)
1747 struct connman_device *device = data;
1748 struct wifi_data *wifi = connman_device_get_data(device);
1752 wifi->p2p_find_timeout = 0;
1754 connman_device_set_scanning(device, CONNMAN_SERVICE_TYPE_P2P, false);
1756 g_supplicant_interface_p2p_stop_find(wifi->interface);
1758 connman_device_unref(device);
1759 reset_autoscan(device);
1764 static void p2p_find_callback(int result, GSupplicantInterface *interface,
1767 struct connman_device *device = user_data;
1768 struct wifi_data *wifi = connman_device_get_data(device);
1770 DBG("result %d wifi %p", result, wifi);
1772 if (wifi->p2p_find_timeout) {
1773 g_source_remove(wifi->p2p_find_timeout);
1774 wifi->p2p_find_timeout = 0;
1780 wifi->p2p_find_timeout = g_timeout_add_seconds(P2P_FIND_TIMEOUT,
1781 p2p_find_stop, device);
1782 if (!wifi->p2p_find_timeout)
1787 p2p_find_stop(device);
1790 static int p2p_find(struct connman_device *device)
1792 struct wifi_data *wifi;
1797 if (!p2p_technology)
1800 wifi = connman_device_get_data(device);
1802 if (g_supplicant_interface_is_p2p_finding(wifi->interface))
1805 reset_autoscan(device);
1806 connman_device_ref(device);
1808 ret = g_supplicant_interface_p2p_find(wifi->interface,
1809 p2p_find_callback, device);
1811 connman_device_unref(device);
1812 start_autoscan(device);
1814 connman_device_set_scanning(device,
1815 CONNMAN_SERVICE_TYPE_P2P, true);
1822 * Note that the hidden scan is only used when connecting to this specific
1823 * hidden AP first time. It is not used when system autoconnects to hidden AP.
1825 static int wifi_scan(enum connman_service_type type,
1826 struct connman_device *device,
1827 const char *ssid, unsigned int ssid_len,
1828 const char *identity, const char* passphrase,
1829 const char *security, void *user_data)
1831 struct wifi_data *wifi = connman_device_get_data(device);
1832 GSupplicantScanParams *scan_params = NULL;
1833 struct scan_ssid *scan_ssid;
1834 struct hidden_params *hidden;
1836 int driver_max_ssids = 0;
1843 if (wifi->p2p_device)
1846 if (type == CONNMAN_SERVICE_TYPE_P2P)
1847 return p2p_find(device);
1849 DBG("device %p wifi %p hidden ssid %s", device, wifi->interface, ssid);
1851 if (wifi->tethering)
1854 scanning = connman_device_get_scanning(device);
1856 if (!ssid || ssid_len == 0 || ssid_len > 32) {
1860 driver_max_ssids = g_supplicant_interface_get_max_scan_ssids(
1862 DBG("max ssids %d", driver_max_ssids);
1863 if (driver_max_ssids == 0)
1864 return wifi_scan_simple(device);
1868 if (scanning && wifi->hidden && wifi->postpone_hidden)
1874 scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
1879 scan_ssid = g_try_new(struct scan_ssid, 1);
1881 g_free(scan_params);
1885 memcpy(scan_ssid->ssid, ssid, ssid_len);
1886 scan_ssid->ssid_len = ssid_len;
1887 scan_params->ssids = g_slist_prepend(scan_params->ssids,
1889 scan_params->num_ssids = 1;
1891 hidden = g_try_new0(struct hidden_params, 1);
1893 g_supplicant_free_scan_params(scan_params);
1898 hidden_free(wifi->hidden);
1899 wifi->hidden = NULL;
1902 memcpy(hidden->ssid, ssid, ssid_len);
1903 hidden->ssid_len = ssid_len;
1904 hidden->identity = g_strdup(identity);
1905 hidden->passphrase = g_strdup(passphrase);
1906 hidden->security = g_strdup(security);
1907 hidden->user_data = user_data;
1908 wifi->hidden = hidden;
1911 /* Let's keep this active scan for later,
1912 * when current scan will be over. */
1913 wifi->postpone_hidden = TRUE;
1914 hidden->scan_params = scan_params;
1918 } else if (wifi->connected) {
1919 g_supplicant_free_scan_params(scan_params);
1920 return wifi_scan_simple(device);
1922 ret = get_latest_connections(driver_max_ssids, scan_params);
1924 g_supplicant_free_scan_params(scan_params);
1925 return wifi_scan_simple(device);
1929 connman_device_ref(device);
1931 reset_autoscan(device);
1933 #if defined TIZEN_EXT
1935 ret = g_supplicant_interface_scan(wifi->interface, scan_params,
1936 scan_callback, device);
1939 ret = g_supplicant_interface_scan(wifi->interface, NULL,
1940 scan_callback_hidden, device);
1943 ret = g_supplicant_interface_scan(wifi->interface, scan_params,
1944 scan_callback, device);
1947 connman_device_set_scanning(device,
1948 CONNMAN_SERVICE_TYPE_WIFI, true);
1950 g_supplicant_free_scan_params(scan_params);
1951 connman_device_unref(device);
1954 hidden_free(wifi->hidden);
1955 wifi->hidden = NULL;
1962 static void wifi_regdom_callback(int result,
1966 struct connman_device *device = user_data;
1968 connman_device_regdom_notify(device, result, alpha2);
1970 connman_device_unref(device);
1973 static int wifi_set_regdom(struct connman_device *device, const char *alpha2)
1975 struct wifi_data *wifi = connman_device_get_data(device);
1981 connman_device_ref(device);
1983 ret = g_supplicant_interface_set_country(wifi->interface,
1984 wifi_regdom_callback,
1987 connman_device_unref(device);
1992 static struct connman_device_driver wifi_ng_driver = {
1994 .type = CONNMAN_DEVICE_TYPE_WIFI,
1995 .priority = CONNMAN_DEVICE_PRIORITY_LOW,
1996 .probe = wifi_probe,
1997 .remove = wifi_remove,
1998 .enable = wifi_enable,
1999 .disable = wifi_disable,
2001 .set_regdom = wifi_set_regdom,
2004 static void system_ready(void)
2008 if (connman_device_driver_register(&wifi_ng_driver) < 0)
2009 connman_error("Failed to register WiFi driver");
2012 static void system_killed(void)
2016 connman_device_driver_unregister(&wifi_ng_driver);
2019 static int network_probe(struct connman_network *network)
2021 DBG("network %p", network);
2026 static void network_remove(struct connman_network *network)
2028 struct connman_device *device = connman_network_get_device(network);
2029 struct wifi_data *wifi;
2031 DBG("network %p", network);
2033 wifi = connman_device_get_data(device);
2037 if (wifi->network != network)
2040 wifi->network = NULL;
2042 #if defined TIZEN_EXT
2043 wifi->disconnecting = false;
2045 if (wifi->pending_network == network)
2046 wifi->pending_network = NULL;
2048 if (wifi->scan_pending_network == network)
2049 wifi->scan_pending_network = NULL;
2053 static void connect_callback(int result, GSupplicantInterface *interface,
2056 #if defined TIZEN_EXT
2058 struct wifi_data *wifi;
2060 struct connman_network *network = user_data;
2062 DBG("network %p result %d", network, result);
2064 #if defined TIZEN_EXT
2065 for (list = iface_list; list; list = list->next) {
2068 if (wifi && wifi->network == network)
2072 /* wifi_data may be invalid because wifi is already disabled */
2077 if (result == -ENOKEY) {
2078 connman_network_set_error(network,
2079 CONNMAN_NETWORK_ERROR_INVALID_KEY);
2080 } else if (result < 0) {
2081 connman_network_set_error(network,
2082 CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
2085 connman_network_unref(network);
2088 static GSupplicantSecurity network_security(const char *security)
2090 if (g_str_equal(security, "none"))
2091 return G_SUPPLICANT_SECURITY_NONE;
2092 else if (g_str_equal(security, "wep"))
2093 return G_SUPPLICANT_SECURITY_WEP;
2094 else if (g_str_equal(security, "psk"))
2095 return G_SUPPLICANT_SECURITY_PSK;
2096 else if (g_str_equal(security, "wpa"))
2097 return G_SUPPLICANT_SECURITY_PSK;
2098 else if (g_str_equal(security, "rsn"))
2099 return G_SUPPLICANT_SECURITY_PSK;
2100 else if (g_str_equal(security, "ieee8021x"))
2101 return G_SUPPLICANT_SECURITY_IEEE8021X;
2102 #if defined TIZEN_EXT
2103 else if (g_str_equal(security, "ft_psk") == TRUE)
2104 return G_SUPPLICANT_SECURITY_FT_PSK;
2105 else if (g_str_equal(security, "ft_ieee8021x") == TRUE)
2106 return G_SUPPLICANT_SECURITY_FT_IEEE8021X;
2109 return G_SUPPLICANT_SECURITY_UNKNOWN;
2112 static void ssid_init(GSupplicantSSID *ssid, struct connman_network *network)
2114 const char *security;
2116 memset(ssid, 0, sizeof(*ssid));
2117 ssid->mode = G_SUPPLICANT_MODE_INFRA;
2118 ssid->ssid = connman_network_get_blob(network, "WiFi.SSID",
2120 ssid->scan_ssid = 1;
2121 security = connman_network_get_string(network, "WiFi.Security");
2122 ssid->security = network_security(security);
2123 ssid->passphrase = connman_network_get_string(network,
2126 ssid->eap = connman_network_get_string(network, "WiFi.EAP");
2129 * If our private key password is unset,
2130 * we use the supplied passphrase. That is needed
2131 * for PEAP where 2 passphrases (identity and client
2132 * cert may have to be provided.
2134 if (!connman_network_get_string(network, "WiFi.PrivateKeyPassphrase"))
2135 connman_network_set_string(network,
2136 "WiFi.PrivateKeyPassphrase",
2138 /* We must have an identity for both PEAP and TLS */
2139 ssid->identity = connman_network_get_string(network, "WiFi.Identity");
2141 /* Use agent provided identity as a fallback */
2142 if (!ssid->identity || strlen(ssid->identity) == 0)
2143 ssid->identity = connman_network_get_string(network,
2144 "WiFi.AgentIdentity");
2146 ssid->ca_cert_path = connman_network_get_string(network,
2148 ssid->client_cert_path = connman_network_get_string(network,
2149 "WiFi.ClientCertFile");
2150 ssid->private_key_path = connman_network_get_string(network,
2151 "WiFi.PrivateKeyFile");
2152 ssid->private_key_passphrase = connman_network_get_string(network,
2153 "WiFi.PrivateKeyPassphrase");
2154 ssid->phase2_auth = connman_network_get_string(network, "WiFi.Phase2");
2156 ssid->use_wps = connman_network_get_bool(network, "WiFi.UseWPS");
2157 ssid->pin_wps = connman_network_get_string(network, "WiFi.PinWPS");
2159 #if defined TIZEN_EXT
2160 ssid->bssid = connman_network_get_bssid(network);
2163 if (connman_setting_get_bool("BackgroundScanning"))
2164 ssid->bgscan = BGSCAN_DEFAULT;
2167 static int network_connect(struct connman_network *network)
2169 struct connman_device *device = connman_network_get_device(network);
2170 struct wifi_data *wifi;
2171 GSupplicantInterface *interface;
2172 GSupplicantSSID *ssid;
2174 DBG("network %p", network);
2179 wifi = connman_device_get_data(device);
2183 ssid = g_try_malloc0(sizeof(GSupplicantSSID));
2187 interface = wifi->interface;
2189 ssid_init(ssid, network);
2191 if (wifi->disconnecting) {
2192 wifi->pending_network = network;
2195 wifi->network = connman_network_ref(network);
2197 #if defined TIZEN_EXT
2198 wifi->scan_pending_network = NULL;
2201 return g_supplicant_interface_connect(interface, ssid,
2202 connect_callback, network);
2205 return -EINPROGRESS;
2208 static void disconnect_callback(int result, GSupplicantInterface *interface,
2211 #if defined TIZEN_EXT
2213 struct wifi_data *wifi;
2214 struct connman_network *network = user_data;
2216 DBG("network %p result %d", network, result);
2218 for (list = iface_list; list; list = list->next) {
2221 if (wifi->network == NULL && wifi->disconnecting == true)
2222 wifi->disconnecting = false;
2224 if (wifi->network == network)
2228 /* wifi_data may be invalid because wifi is already disabled */
2233 struct wifi_data *wifi = user_data;
2236 DBG("result %d supplicant interface %p wifi %p",
2237 result, interface, wifi);
2239 if (result == -ECONNABORTED) {
2240 DBG("wifi interface no longer available");
2244 if (wifi->network) {
2246 * if result < 0 supplican return an error because
2247 * the network is not current.
2248 * we wont receive G_SUPPLICANT_STATE_DISCONNECTED since it
2249 * failed, call connman_network_set_connected to report
2250 * disconnect is completed.
2253 connman_network_set_connected(wifi->network, false);
2256 wifi->network = NULL;
2258 wifi->disconnecting = false;
2260 if (wifi->pending_network) {
2261 network_connect(wifi->pending_network);
2262 wifi->pending_network = NULL;
2265 start_autoscan(wifi->device);
2268 static int network_disconnect(struct connman_network *network)
2270 struct connman_device *device = connman_network_get_device(network);
2271 struct wifi_data *wifi;
2273 #if defined TIZEN_EXT
2274 struct connman_service *service;
2277 DBG("network %p", network);
2279 wifi = connman_device_get_data(device);
2280 if (!wifi || !wifi->interface)
2283 #if defined TIZEN_EXT
2284 if (connman_network_get_associating(network) == true) {
2285 connman_network_clear_associating(network);
2286 connman_network_set_bool(network, "WiFi.UseWPS", false);
2288 service = connman_service_lookup_from_network(network);
2290 if (service != NULL &&
2291 (__connman_service_is_connected_state(service,
2292 CONNMAN_IPCONFIG_TYPE_IPV4) == false &&
2293 __connman_service_is_connected_state(service,
2294 CONNMAN_IPCONFIG_TYPE_IPV6) == false) &&
2295 (connman_service_get_favorite(service) == false))
2296 __connman_service_set_passphrase(service, NULL);
2299 if (wifi->pending_network == network)
2300 wifi->pending_network = NULL;
2302 if (wifi->scan_pending_network == network)
2303 wifi->scan_pending_network = NULL;
2306 connman_network_set_associating(network, false);
2308 if (wifi->disconnecting)
2311 wifi->disconnecting = true;
2313 #if defined TIZEN_EXT
2314 err = g_supplicant_interface_disconnect(wifi->interface,
2315 disconnect_callback, network);
2317 err = g_supplicant_interface_disconnect(wifi->interface,
2318 disconnect_callback, wifi);
2322 wifi->disconnecting = false;
2327 static struct connman_network_driver network_driver = {
2329 .type = CONNMAN_NETWORK_TYPE_WIFI,
2330 .priority = CONNMAN_NETWORK_PRIORITY_LOW,
2331 .probe = network_probe,
2332 .remove = network_remove,
2333 .connect = network_connect,
2334 .disconnect = network_disconnect,
2337 static void interface_added(GSupplicantInterface *interface)
2339 const char *ifname = g_supplicant_interface_get_ifname(interface);
2340 const char *driver = g_supplicant_interface_get_driver(interface);
2341 struct wifi_data *wifi;
2343 wifi = g_supplicant_interface_get_data(interface);
2345 wifi = get_pending_wifi_data(ifname);
2349 g_supplicant_interface_set_data(interface, wifi);
2350 p2p_iface_list = g_list_append(p2p_iface_list, wifi);
2351 wifi->p2p_device = true;
2354 DBG("ifname %s driver %s wifi %p tethering %d",
2355 ifname, driver, wifi, wifi->tethering);
2357 if (!wifi->device) {
2358 connman_error("WiFi device not set");
2362 connman_device_set_powered(wifi->device, true);
2365 static bool is_idle(struct wifi_data *wifi)
2367 DBG("state %d", wifi->state);
2369 switch (wifi->state) {
2370 case G_SUPPLICANT_STATE_UNKNOWN:
2371 case G_SUPPLICANT_STATE_DISABLED:
2372 case G_SUPPLICANT_STATE_DISCONNECTED:
2373 case G_SUPPLICANT_STATE_INACTIVE:
2374 case G_SUPPLICANT_STATE_SCANNING:
2377 case G_SUPPLICANT_STATE_AUTHENTICATING:
2378 case G_SUPPLICANT_STATE_ASSOCIATING:
2379 case G_SUPPLICANT_STATE_ASSOCIATED:
2380 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
2381 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
2382 case G_SUPPLICANT_STATE_COMPLETED:
2389 static bool is_idle_wps(GSupplicantInterface *interface,
2390 struct wifi_data *wifi)
2392 /* First, let's check if WPS processing did not went wrong */
2393 if (g_supplicant_interface_get_wps_state(interface) ==
2394 G_SUPPLICANT_WPS_STATE_FAIL)
2397 /* Unlike normal connection, being associated while processing wps
2398 * actually means that we are idling. */
2399 switch (wifi->state) {
2400 case G_SUPPLICANT_STATE_UNKNOWN:
2401 case G_SUPPLICANT_STATE_DISABLED:
2402 case G_SUPPLICANT_STATE_DISCONNECTED:
2403 case G_SUPPLICANT_STATE_INACTIVE:
2404 case G_SUPPLICANT_STATE_SCANNING:
2405 case G_SUPPLICANT_STATE_ASSOCIATED:
2407 case G_SUPPLICANT_STATE_AUTHENTICATING:
2408 case G_SUPPLICANT_STATE_ASSOCIATING:
2409 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
2410 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
2411 case G_SUPPLICANT_STATE_COMPLETED:
2418 static bool handle_wps_completion(GSupplicantInterface *interface,
2419 struct connman_network *network,
2420 struct connman_device *device,
2421 struct wifi_data *wifi)
2425 wps = connman_network_get_bool(network, "WiFi.UseWPS");
2427 const unsigned char *ssid, *wps_ssid;
2428 unsigned int ssid_len, wps_ssid_len;
2429 const char *wps_key;
2431 /* Checking if we got associated with requested
2433 ssid = connman_network_get_blob(network, "WiFi.SSID",
2436 wps_ssid = g_supplicant_interface_get_wps_ssid(
2437 interface, &wps_ssid_len);
2439 if (!wps_ssid || wps_ssid_len != ssid_len ||
2440 memcmp(ssid, wps_ssid, ssid_len) != 0) {
2441 connman_network_set_associating(network, false);
2442 #if defined TIZEN_EXT
2443 g_supplicant_interface_disconnect(wifi->interface,
2444 disconnect_callback, wifi->network);
2446 connman_network_set_bool(network, "WiFi.UseWPS", false);
2447 connman_network_set_string(network, "WiFi.PinWPS", NULL);
2449 g_supplicant_interface_disconnect(wifi->interface,
2450 disconnect_callback, wifi);
2455 wps_key = g_supplicant_interface_get_wps_key(interface);
2456 connman_network_set_string(network, "WiFi.Passphrase",
2459 connman_network_set_string(network, "WiFi.PinWPS", NULL);
2465 static bool handle_4way_handshake_failure(GSupplicantInterface *interface,
2466 struct connman_network *network,
2467 struct wifi_data *wifi)
2469 #if defined TIZEN_EXT
2470 const char *security;
2471 struct connman_service *service;
2473 if (wifi->connected)
2476 security = connman_network_get_string(network, "WiFi.Security");
2478 if (g_str_equal(security, "ieee8021x") == true &&
2479 wifi->state == G_SUPPLICANT_STATE_ASSOCIATED) {
2481 connman_network_set_error(network, CONNMAN_NETWORK_ERROR_INVALID_KEY);
2486 if (wifi->state != G_SUPPLICANT_STATE_4WAY_HANDSHAKE)
2489 struct connman_service *service;
2491 if (wifi->state != G_SUPPLICANT_STATE_4WAY_HANDSHAKE)
2494 if (wifi->connected)
2498 service = connman_service_lookup_from_network(network);
2504 if (connman_service_get_favorite(service)) {
2505 if (wifi->retries < FAVORITE_MAXIMUM_RETRIES)
2510 connman_network_set_error(network, CONNMAN_NETWORK_ERROR_INVALID_KEY);
2512 #if defined TIZEN_EXT
2513 /* not retry autoconnect in case of invalid-key error */
2514 __connman_service_set_autoconnect(service, false);
2520 #if defined TIZEN_EXT
2521 static bool handle_wifi_assoc_retry(struct connman_network *network,
2522 struct wifi_data *wifi)
2524 const char *security;
2526 if (!wifi->network || wifi->connected || wifi->disconnecting ||
2527 connman_network_get_connecting(network) != true) {
2528 wifi->assoc_retry_count = 0;
2532 if (wifi->state != G_SUPPLICANT_STATE_ASSOCIATING &&
2533 wifi->state != G_SUPPLICANT_STATE_ASSOCIATED) {
2534 wifi->assoc_retry_count = 0;
2538 security = connman_network_get_string(network, "WiFi.Security");
2539 if (g_str_equal(security, "ieee8021x") == true &&
2540 wifi->state == G_SUPPLICANT_STATE_ASSOCIATED) {
2541 wifi->assoc_retry_count = 0;
2545 if (++wifi->assoc_retry_count >= TIZEN_ASSOC_RETRY_COUNT) {
2546 wifi->assoc_retry_count = 0;
2548 /* Honestly it's not an invalid-key error,
2549 * however QA team recommends that the invalid-key error
2550 * might be better to display for user experience.
2552 connman_network_set_error(network, CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
2561 static void interface_state(GSupplicantInterface *interface)
2563 struct connman_network *network;
2564 struct connman_device *device;
2565 struct wifi_data *wifi;
2566 GSupplicantState state = g_supplicant_interface_get_state(interface);
2569 wifi = g_supplicant_interface_get_data(interface);
2571 DBG("wifi %p interface state %d", wifi, state);
2576 device = wifi->device;
2580 if (g_supplicant_interface_get_ready(interface) &&
2581 !wifi->interface_ready) {
2582 wifi->interface_ready = true;
2583 finalize_interface_creation(wifi);
2586 network = wifi->network;
2591 case G_SUPPLICANT_STATE_SCANNING:
2594 case G_SUPPLICANT_STATE_AUTHENTICATING:
2595 case G_SUPPLICANT_STATE_ASSOCIATING:
2596 #if defined TIZEN_EXT
2597 reset_autoscan(device);
2599 stop_autoscan(device);
2602 if (!wifi->connected)
2603 connman_network_set_associating(network, true);
2607 case G_SUPPLICANT_STATE_COMPLETED:
2608 #if defined TIZEN_EXT
2609 /* though it should be already reset: */
2610 reset_autoscan(device);
2612 wifi->assoc_retry_count = 0;
2614 wifi->scan_pending_network = NULL;
2616 /* though it should be already stopped: */
2617 stop_autoscan(device);
2620 if (!handle_wps_completion(interface, network, device, wifi))
2623 connman_network_set_connected(network, true);
2626 case G_SUPPLICANT_STATE_DISCONNECTED:
2628 * If we're in one of the idle modes, we have
2629 * not started association yet and thus setting
2630 * those ones to FALSE could cancel an association
2633 wps = connman_network_get_bool(network, "WiFi.UseWPS");
2635 if (is_idle_wps(interface, wifi))
2641 /* If previous state was 4way-handshake, then
2642 * it's either: psk was incorrect and thus we retry
2643 * or if we reach the maximum retries we declare the
2645 if (handle_4way_handshake_failure(interface,
2649 /* We disable the selected network, if not then
2650 * wpa_supplicant will loop retrying */
2651 if (g_supplicant_interface_enable_selected_network(interface,
2653 DBG("Could not disables selected network");
2655 #if defined TIZEN_EXT
2657 int reason_code = 0;
2659 err = g_supplicant_interface_remove_network(wifi->interface);
2661 DBG("Failed to remove network(%d)", err);
2663 reason_code = g_supplicant_interface_get_disconnect_reason(wifi->interface);
2665 /* Some of Wi-Fi networks are not comply Wi-Fi specification.
2666 * Retry association until its retry count is expired */
2667 if (handle_wifi_assoc_retry(network, wifi) == true) {
2668 throw_wifi_scan(wifi->device, scan_callback);
2669 wifi->scan_pending_network = wifi->network;
2673 if(reason_code > 0){
2674 DBG("Set disconnect reason code(%d)", reason_code);
2675 connman_network_set_disconnect_reason(network, reason_code);
2678 /* To avoid unnecessary repeated association in wpa_supplicant,
2679 * "RemoveNetwork" should be made when Wi-Fi is disconnected */
2680 if (wps != true && wifi->network && wifi->disconnecting == false) {
2681 wifi->disconnecting = true;
2682 err = g_supplicant_interface_disconnect(wifi->interface,
2683 disconnect_callback, wifi->network);
2685 wifi->disconnecting = false;
2687 connman_network_set_connected(network, false);
2688 connman_network_set_associating(network, false);
2690 start_autoscan(device);
2696 connman_network_set_connected(network, false);
2697 connman_network_set_associating(network, false);
2698 wifi->disconnecting = false;
2700 start_autoscan(device);
2704 case G_SUPPLICANT_STATE_INACTIVE:
2705 #if defined TIZEN_EXT
2706 if (handle_wps_completion(interface, network, device, wifi) == false)
2709 connman_network_set_associating(network, false);
2710 start_autoscan(device);
2714 case G_SUPPLICANT_STATE_UNKNOWN:
2715 case G_SUPPLICANT_STATE_DISABLED:
2716 case G_SUPPLICANT_STATE_ASSOCIATED:
2717 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
2718 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
2722 wifi->state = state;
2724 /* Saving wpa_s state policy:
2725 * If connected and if the state changes are roaming related:
2726 * --> We stay connected
2728 * --> We are connected
2730 * --> We are not connected
2733 #if defined TIZEN_EXT
2734 case G_SUPPLICANT_STATE_SCANNING:
2737 case G_SUPPLICANT_STATE_AUTHENTICATING:
2738 case G_SUPPLICANT_STATE_ASSOCIATING:
2739 case G_SUPPLICANT_STATE_ASSOCIATED:
2740 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
2741 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
2742 if (wifi->connected)
2743 connman_warn("Probably roaming right now!"
2744 " Staying connected...");
2746 wifi->connected = false;
2748 case G_SUPPLICANT_STATE_COMPLETED:
2749 wifi->connected = true;
2752 wifi->connected = false;
2759 static void interface_removed(GSupplicantInterface *interface)
2761 const char *ifname = g_supplicant_interface_get_ifname(interface);
2762 struct wifi_data *wifi;
2764 DBG("ifname %s", ifname);
2766 wifi = g_supplicant_interface_get_data(interface);
2769 wifi->interface = NULL;
2771 if (wifi && wifi->tethering)
2774 if (!wifi || !wifi->device) {
2775 DBG("wifi interface already removed");
2779 connman_device_set_powered(wifi->device, false);
2781 check_p2p_technology();
2784 static void set_device_type(const char *type, char dev_type[17])
2786 const char *oui = "0050F204";
2787 const char *category = "0100";
2788 const char *sub_category = "0000";
2790 if (!g_strcmp0(type, "handset")) {
2792 sub_category = "0500";
2793 } else if (!g_strcmp0(type, "vm") || !g_strcmp0(type, "container"))
2794 sub_category = "0100";
2795 else if (!g_strcmp0(type, "server"))
2796 sub_category = "0200";
2797 else if (!g_strcmp0(type, "laptop"))
2798 sub_category = "0500";
2799 else if (!g_strcmp0(type, "desktop"))
2800 sub_category = "0600";
2801 else if (!g_strcmp0(type, "tablet"))
2802 sub_category = "0900";
2803 else if (!g_strcmp0(type, "watch"))
2806 snprintf(dev_type, 17, "%s%s%s", category, oui, sub_category);
2809 static void p2p_support(GSupplicantInterface *interface)
2811 char dev_type[17] = {};
2812 const char *hostname;
2816 if (!g_supplicant_interface_has_p2p(interface))
2819 if (connman_technology_driver_register(&p2p_tech_driver) < 0) {
2820 DBG("Could not register P2P technology driver");
2824 hostname = connman_utsname_get_hostname();
2826 hostname = "ConnMan";
2828 set_device_type(connman_machine_get_type(), dev_type);
2829 g_supplicant_interface_set_p2p_device_config(interface,
2830 hostname, dev_type);
2831 connman_peer_driver_register(&peer_driver);
2834 static void scan_started(GSupplicantInterface *interface)
2839 static void scan_finished(GSupplicantInterface *interface)
2841 #if defined TIZEN_EXT
2842 struct wifi_data *wifi;
2843 bool is_associating = false;
2844 static bool is_scanning = true;
2849 #if defined TIZEN_EXT
2850 wifi = g_supplicant_interface_get_data(interface);
2851 if (wifi && wifi->scan_pending_network) {
2852 network_connect(wifi->scan_pending_network);
2853 wifi->scan_pending_network = NULL;
2856 //service state - associating
2857 if(!wifi || !wifi->network)
2860 is_associating = connman_network_get_associating(wifi->network);
2861 if(is_associating && is_scanning){
2862 is_scanning = false;
2863 DBG("send scan for connecting");
2864 throw_wifi_scan(wifi->device, scan_callback);
2875 static unsigned char calculate_strength(GSupplicantNetwork *supplicant_network)
2877 unsigned char strength;
2879 strength = 120 + g_supplicant_network_get_signal(supplicant_network);
2886 static void network_added(GSupplicantNetwork *supplicant_network)
2888 struct connman_network *network;
2889 GSupplicantInterface *interface;
2890 struct wifi_data *wifi;
2891 const char *name, *identifier, *security, *group, *mode;
2892 const unsigned char *ssid;
2893 unsigned int ssid_len;
2897 bool wps_advertizing;
2899 mode = g_supplicant_network_get_mode(supplicant_network);
2900 identifier = g_supplicant_network_get_identifier(supplicant_network);
2902 DBG("%s", identifier);
2904 if (!g_strcmp0(mode, "adhoc"))
2907 interface = g_supplicant_network_get_interface(supplicant_network);
2908 wifi = g_supplicant_interface_get_data(interface);
2909 name = g_supplicant_network_get_name(supplicant_network);
2910 security = g_supplicant_network_get_security(supplicant_network);
2911 group = g_supplicant_network_get_identifier(supplicant_network);
2912 wps = g_supplicant_network_get_wps(supplicant_network);
2913 wps_pbc = g_supplicant_network_is_wps_pbc(supplicant_network);
2914 wps_ready = g_supplicant_network_is_wps_active(supplicant_network);
2915 wps_advertizing = g_supplicant_network_is_wps_advertizing(
2916 supplicant_network);
2921 ssid = g_supplicant_network_get_ssid(supplicant_network, &ssid_len);
2923 network = connman_device_get_network(wifi->device, identifier);
2926 network = connman_network_create(identifier,
2927 CONNMAN_NETWORK_TYPE_WIFI);
2931 connman_network_set_index(network, wifi->index);
2933 if (connman_device_add_network(wifi->device, network) < 0) {
2934 connman_network_unref(network);
2938 wifi->networks = g_slist_prepend(wifi->networks, network);
2941 if (name && name[0] != '\0')
2942 connman_network_set_name(network, name);
2944 connman_network_set_blob(network, "WiFi.SSID",
2946 connman_network_set_string(network, "WiFi.Security", security);
2947 connman_network_set_strength(network,
2948 calculate_strength(supplicant_network));
2949 connman_network_set_bool(network, "WiFi.WPS", wps);
2952 /* Is AP advertizing for WPS association?
2953 * If so, we decide to use WPS by default */
2954 if (wps_ready && wps_pbc &&
2956 #if !defined TIZEN_EXT
2957 connman_network_set_bool(network, "WiFi.UseWPS", true);
2959 DBG("wps is activating by ap but ignore it.");
2964 connman_network_set_frequency(network,
2965 g_supplicant_network_get_frequency(supplicant_network));
2966 #if defined TIZEN_EXT
2967 connman_network_set_bssid(network,
2968 g_supplicant_network_get_bssid(supplicant_network));
2969 connman_network_set_maxrate(network,
2970 g_supplicant_network_get_maxrate(supplicant_network));
2971 connman_network_set_enc_mode(network,
2972 g_supplicant_network_get_enc_mode(supplicant_network));
2973 connman_network_set_rsn_mode(network,
2974 g_supplicant_network_get_rsn_mode(supplicant_network));
2975 connman_network_set_keymgmt(network,
2976 g_supplicant_network_get_keymgmt(supplicant_network));
2978 connman_network_set_available(network, true);
2979 connman_network_set_string(network, "WiFi.Mode", mode);
2981 #if defined TIZEN_EXT
2986 connman_network_set_group(network, group);
2988 #if defined TIZEN_EXT
2989 if (wifi_first_scan == true)
2990 found_with_first_scan = true;
2993 if (wifi->hidden && ssid) {
2994 if (!g_strcmp0(wifi->hidden->security, security) &&
2995 wifi->hidden->ssid_len == ssid_len &&
2996 !memcmp(wifi->hidden->ssid, ssid, ssid_len)) {
2997 connman_network_connect_hidden(network,
2998 wifi->hidden->identity,
2999 wifi->hidden->passphrase,
3000 wifi->hidden->user_data);
3001 wifi->hidden->user_data = NULL;
3002 hidden_free(wifi->hidden);
3003 wifi->hidden = NULL;
3008 static void network_removed(GSupplicantNetwork *network)
3010 GSupplicantInterface *interface;
3011 struct wifi_data *wifi;
3012 const char *name, *identifier;
3013 struct connman_network *connman_network;
3015 interface = g_supplicant_network_get_interface(network);
3016 wifi = g_supplicant_interface_get_data(interface);
3017 identifier = g_supplicant_network_get_identifier(network);
3018 name = g_supplicant_network_get_name(network);
3020 DBG("name %s", name);
3025 connman_network = connman_device_get_network(wifi->device, identifier);
3026 if (!connman_network)
3029 #if defined TIZEN_EXT
3030 if (connman_network == wifi->scan_pending_network)
3031 wifi->scan_pending_network = NULL;
3033 if (connman_network == wifi->pending_network)
3034 wifi->pending_network = NULL;
3036 if(connman_network_get_connecting(connman_network) == true){
3037 connman_network_set_connected(connman_network, false);
3041 wifi->networks = g_slist_remove(wifi->networks, connman_network);
3043 connman_device_remove_network(wifi->device, connman_network);
3044 connman_network_unref(connman_network);
3047 static void network_changed(GSupplicantNetwork *network, const char *property)
3049 GSupplicantInterface *interface;
3050 struct wifi_data *wifi;
3051 const char *name, *identifier;
3052 struct connman_network *connman_network;
3054 #if defined TIZEN_EXT
3055 const unsigned char *bssid;
3056 unsigned int maxrate;
3061 interface = g_supplicant_network_get_interface(network);
3062 wifi = g_supplicant_interface_get_data(interface);
3063 identifier = g_supplicant_network_get_identifier(network);
3064 name = g_supplicant_network_get_name(network);
3066 DBG("name %s", name);
3071 connman_network = connman_device_get_network(wifi->device, identifier);
3072 if (!connman_network)
3075 if (g_str_equal(property, "Signal")) {
3076 connman_network_set_strength(connman_network,
3077 calculate_strength(network));
3078 connman_network_update(connman_network);
3081 #if defined TIZEN_EXT
3082 bssid = g_supplicant_network_get_bssid(network);
3083 maxrate = g_supplicant_network_get_maxrate(network);
3084 frequency = g_supplicant_network_get_frequency(network);
3085 wps = g_supplicant_network_get_wps(network);
3087 connman_network_set_bssid(connman_network, bssid);
3088 connman_network_set_maxrate(connman_network, maxrate);
3089 connman_network_set_frequency(connman_network, frequency);
3090 connman_network_set_bool(connman_network, "WiFi.WPS", wps);
3094 static void apply_peer_services(GSupplicantPeer *peer,
3095 struct connman_peer *connman_peer)
3097 const unsigned char *data;
3102 connman_peer_reset_services(connman_peer);
3104 data = g_supplicant_peer_get_widi_ies(peer, &length);
3106 connman_peer_add_service(connman_peer,
3107 CONNMAN_PEER_SERVICE_WIFI_DISPLAY, data, length);
3111 static void add_station(const char *mac)
3113 connman_technology_tethering_add_station(CONNMAN_SERVICE_TYPE_WIFI,
3117 static void remove_station(const char *mac)
3119 connman_technology_tethering_remove_station(mac);
3122 static void peer_found(GSupplicantPeer *peer)
3124 GSupplicantInterface *iface = g_supplicant_peer_get_interface(peer);
3125 struct wifi_data *wifi = g_supplicant_interface_get_data(iface);
3126 struct connman_peer *connman_peer;
3127 const char *identifier, *name;
3130 identifier = g_supplicant_peer_get_identifier(peer);
3131 name = g_supplicant_peer_get_name(peer);
3133 DBG("ident: %s", identifier);
3135 connman_peer = connman_peer_get(wifi->device, identifier);
3139 connman_peer = connman_peer_create(identifier);
3140 connman_peer_set_name(connman_peer, name);
3141 connman_peer_set_device(connman_peer, wifi->device);
3142 apply_peer_services(peer, connman_peer);
3144 ret = connman_peer_register(connman_peer);
3145 if (ret < 0 && ret != -EALREADY)
3146 connman_peer_unref(connman_peer);
3149 static void peer_lost(GSupplicantPeer *peer)
3151 GSupplicantInterface *iface = g_supplicant_peer_get_interface(peer);
3152 struct wifi_data *wifi = g_supplicant_interface_get_data(iface);
3153 struct connman_peer *connman_peer;
3154 const char *identifier;
3159 identifier = g_supplicant_peer_get_identifier(peer);
3161 DBG("ident: %s", identifier);
3163 connman_peer = connman_peer_get(wifi->device, identifier);
3165 if (wifi->p2p_connecting &&
3166 wifi->pending_peer == connman_peer) {
3167 peer_connect_timeout(wifi);
3169 connman_peer_unregister(connman_peer);
3170 connman_peer_unref(connman_peer);
3174 static void peer_changed(GSupplicantPeer *peer, GSupplicantPeerState state)
3176 GSupplicantInterface *iface = g_supplicant_peer_get_interface(peer);
3177 struct wifi_data *wifi = g_supplicant_interface_get_data(iface);
3178 enum connman_peer_state p_state = CONNMAN_PEER_STATE_UNKNOWN;
3179 struct connman_peer *connman_peer;
3180 const char *identifier;
3182 identifier = g_supplicant_peer_get_identifier(peer);
3184 DBG("ident: %s", identifier);
3186 connman_peer = connman_peer_get(wifi->device, identifier);
3191 case G_SUPPLICANT_PEER_SERVICES_CHANGED:
3192 apply_peer_services(peer, connman_peer);
3193 connman_peer_services_changed(connman_peer);
3195 case G_SUPPLICANT_PEER_GROUP_CHANGED:
3196 if (!g_supplicant_peer_is_in_a_group(peer))
3197 p_state = CONNMAN_PEER_STATE_IDLE;
3199 p_state = CONNMAN_PEER_STATE_CONFIGURATION;
3201 case G_SUPPLICANT_PEER_GROUP_STARTED:
3203 case G_SUPPLICANT_PEER_GROUP_FINISHED:
3204 p_state = CONNMAN_PEER_STATE_IDLE;
3206 case G_SUPPLICANT_PEER_GROUP_JOINED:
3207 connman_peer_set_iface_address(connman_peer,
3208 g_supplicant_peer_get_iface_address(peer));
3210 case G_SUPPLICANT_PEER_GROUP_DISCONNECTED:
3211 p_state = CONNMAN_PEER_STATE_IDLE;
3213 case G_SUPPLICANT_PEER_GROUP_FAILED:
3214 if (g_supplicant_peer_has_requested_connection(peer))
3215 p_state = CONNMAN_PEER_STATE_IDLE;
3217 p_state = CONNMAN_PEER_STATE_FAILURE;
3221 if (p_state == CONNMAN_PEER_STATE_CONFIGURATION ||
3222 p_state == CONNMAN_PEER_STATE_FAILURE) {
3223 if (wifi->p2p_connecting
3224 && connman_peer == wifi->pending_peer)
3225 peer_cancel_timeout(wifi);
3227 p_state = CONNMAN_PEER_STATE_UNKNOWN;
3230 if (p_state == CONNMAN_PEER_STATE_UNKNOWN)
3233 if (p_state == CONNMAN_PEER_STATE_CONFIGURATION) {
3234 GSupplicantInterface *g_iface;
3235 struct wifi_data *g_wifi;
3237 g_iface = g_supplicant_peer_get_group_interface(peer);
3241 g_wifi = g_supplicant_interface_get_data(g_iface);
3245 connman_peer_set_as_master(connman_peer,
3246 !g_supplicant_peer_is_client(peer));
3247 connman_peer_set_sub_device(connman_peer, g_wifi->device);
3250 connman_peer_set_state(connman_peer, p_state);
3253 static void peer_request(GSupplicantPeer *peer)
3255 GSupplicantInterface *iface = g_supplicant_peer_get_interface(peer);
3256 struct wifi_data *wifi = g_supplicant_interface_get_data(iface);
3257 struct connman_peer *connman_peer;
3258 const char *identifier;
3260 identifier = g_supplicant_peer_get_identifier(peer);
3262 DBG("ident: %s", identifier);
3264 connman_peer = connman_peer_get(wifi->device, identifier);
3268 connman_peer_request_connection(connman_peer);
3271 #if defined TIZEN_EXT
3272 static void system_power_off(void)
3275 struct wifi_data *wifi;
3276 struct connman_service *service;
3277 struct connman_ipconfig *ipconfig_ipv4;
3279 if (connman_setting_get_bool("WiFiDHCPRelease") == true) {
3280 for (list = iface_list; list; list = list->next) {
3283 if (wifi->network != NULL) {
3284 service = connman_service_lookup_from_network(wifi->network);
3285 ipconfig_ipv4 = __connman_service_get_ip4config(service);
3286 __connman_dhcp_stop(ipconfig_ipv4);
3292 static void network_merged(GSupplicantNetwork *network)
3294 GSupplicantInterface *interface;
3295 GSupplicantState state;
3296 struct wifi_data *wifi;
3297 const char *identifier;
3298 struct connman_network *connman_network;
3299 unsigned int ishs20AP = 0;
3302 interface = g_supplicant_network_get_interface(network);
3306 state = g_supplicant_interface_get_state(interface);
3307 if (state < G_SUPPLICANT_STATE_AUTHENTICATING)
3310 wifi = g_supplicant_interface_get_data(interface);
3314 identifier = g_supplicant_network_get_identifier(network);
3316 connman_network = connman_device_get_network(wifi->device, identifier);
3317 if (!connman_network)
3320 DBG("merged identifier %s", identifier);
3322 if (wifi->connected == FALSE) {
3324 case G_SUPPLICANT_STATE_AUTHENTICATING:
3325 case G_SUPPLICANT_STATE_ASSOCIATING:
3326 case G_SUPPLICANT_STATE_ASSOCIATED:
3327 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
3328 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
3329 connman_network_set_associating(connman_network, TRUE);
3331 case G_SUPPLICANT_STATE_COMPLETED:
3332 connman_network_set_connected(connman_network, TRUE);
3335 DBG("Not handled the state : %d", state);
3340 ishs20AP = g_supplicant_network_is_hs20AP(network);
3341 connman_network_set_is_hs20AP(connman_network, ishs20AP);
3344 g_strcmp0(g_supplicant_network_get_security(network), "ieee8021x") == 0) {
3345 temp = g_ascii_strdown(g_supplicant_network_get_eap(network), -1);
3346 connman_network_set_string(connman_network, "WiFi.EAP",
3348 connman_network_set_string(connman_network, "WiFi.Identity",
3349 g_supplicant_network_get_identity(network));
3350 connman_network_set_string(connman_network, "WiFi.Phase2",
3351 g_supplicant_network_get_phase2(network));
3356 wifi->network = connman_network;
3360 static void debug(const char *str)
3362 if (getenv("CONNMAN_SUPPLICANT_DEBUG"))
3363 connman_debug("%s", str);
3366 static const GSupplicantCallbacks callbacks = {
3367 .system_ready = system_ready,
3368 .system_killed = system_killed,
3369 .interface_added = interface_added,
3370 .interface_state = interface_state,
3371 .interface_removed = interface_removed,
3372 .p2p_support = p2p_support,
3373 .scan_started = scan_started,
3374 .scan_finished = scan_finished,
3375 .network_added = network_added,
3376 .network_removed = network_removed,
3377 .network_changed = network_changed,
3378 .add_station = add_station,
3379 .remove_station = remove_station,
3380 .peer_found = peer_found,
3381 .peer_lost = peer_lost,
3382 .peer_changed = peer_changed,
3383 .peer_request = peer_request,
3384 #if defined TIZEN_EXT
3385 .system_power_off = system_power_off,
3386 .network_merged = network_merged,
3392 static int tech_probe(struct connman_technology *technology)
3394 wifi_technology = technology;
3399 static void tech_remove(struct connman_technology *technology)
3401 wifi_technology = NULL;
3404 struct wifi_tethering_info {
3405 struct wifi_data *wifi;
3406 struct connman_technology *technology;
3408 GSupplicantSSID *ssid;
3411 static GSupplicantSSID *ssid_ap_init(const char *ssid,
3412 const char *passphrase, bool hidden)
3414 GSupplicantSSID *ap;
3416 ap = g_try_malloc0(sizeof(GSupplicantSSID));
3420 ap->mode = G_SUPPLICANT_MODE_MASTER;
3422 ap->ssid_len = strlen(ssid);
3426 if (!passphrase || strlen(passphrase) == 0) {
3427 ap->security = G_SUPPLICANT_SECURITY_NONE;
3428 ap->passphrase = NULL;
3430 ap->security = G_SUPPLICANT_SECURITY_PSK;
3431 ap->protocol = G_SUPPLICANT_PROTO_RSN;
3432 ap->pairwise_cipher = G_SUPPLICANT_PAIRWISE_CCMP;
3433 ap->group_cipher = G_SUPPLICANT_GROUP_CCMP;
3434 ap->passphrase = passphrase;
3438 ap->ignore_broadcast_ssid =
3439 G_SUPPLICANT_AP_HIDDEN_SSID_ZERO_CONTENTS;
3441 ap->ignore_broadcast_ssid = G_SUPPLICANT_AP_NO_SSID_HIDING;
3446 static void ap_start_callback(int result, GSupplicantInterface *interface,
3449 struct wifi_tethering_info *info = user_data;
3451 DBG("result %d index %d bridge %s",
3452 result, info->wifi->index, info->wifi->bridge);
3455 connman_inet_remove_from_bridge(info->wifi->index,
3456 info->wifi->bridge);
3457 connman_technology_tethering_notify(info->technology, false);
3460 g_free(info->ifname);
3464 static void ap_create_callback(int result,
3465 GSupplicantInterface *interface,
3468 struct wifi_tethering_info *info = user_data;
3470 DBG("result %d ifname %s", result,
3471 g_supplicant_interface_get_ifname(interface));
3474 connman_inet_remove_from_bridge(info->wifi->index,
3475 info->wifi->bridge);
3476 connman_technology_tethering_notify(info->technology, false);
3478 g_free(info->ifname);
3484 info->wifi->interface = interface;
3485 g_supplicant_interface_set_data(interface, info->wifi);
3487 if (g_supplicant_interface_set_apscan(interface, 2) < 0)
3488 connman_error("Failed to set interface ap_scan property");
3490 g_supplicant_interface_connect(interface, info->ssid,
3491 ap_start_callback, info);
3494 static void sta_remove_callback(int result,
3495 GSupplicantInterface *interface,
3498 struct wifi_tethering_info *info = user_data;
3499 const char *driver = connman_option_get_string("wifi");
3501 DBG("ifname %s result %d ", info->ifname, result);
3504 info->wifi->tethering = true;
3506 g_free(info->ifname);
3512 info->wifi->interface = NULL;
3514 connman_technology_tethering_notify(info->technology, true);
3516 g_supplicant_interface_create(info->ifname, driver, info->wifi->bridge,
3521 static int tech_set_tethering(struct connman_technology *technology,
3522 const char *identifier, const char *passphrase,
3523 const char *bridge, bool enabled, bool hidden)
3526 GSupplicantInterface *interface;
3527 struct wifi_data *wifi;
3528 struct wifi_tethering_info *info;
3536 for (list = iface_list; list; list = list->next) {
3539 if (wifi->tethering) {
3540 wifi->tethering = false;
3542 connman_inet_remove_from_bridge(wifi->index,
3544 wifi->bridged = false;
3548 connman_technology_tethering_notify(technology, false);
3553 for (list = iface_list; list; list = list->next) {
3556 interface = wifi->interface;
3561 ifname = g_supplicant_interface_get_ifname(wifi->interface);
3563 mode = g_supplicant_interface_get_mode(interface);
3564 if ((mode & G_SUPPLICANT_CAPABILITY_MODE_AP) == 0) {
3565 DBG("%s does not support AP mode", ifname);
3569 info = g_try_malloc0(sizeof(struct wifi_tethering_info));
3574 info->technology = technology;
3575 info->wifi->bridge = bridge;
3576 info->ssid = ssid_ap_init(identifier, passphrase, hidden);
3581 info->ifname = g_strdup(ifname);
3582 if (!info->ifname) {
3588 info->wifi->tethering = true;
3590 err = g_supplicant_interface_remove(interface,
3591 sta_remove_callback,
3600 static void regdom_callback(int result, const char *alpha2, void *user_data)
3604 if (!wifi_technology)
3610 connman_technology_regdom_notify(wifi_technology, alpha2);
3613 static int tech_set_regdom(struct connman_technology *technology, const char *alpha2)
3615 return g_supplicant_set_country(alpha2, regdom_callback, NULL);
3618 static struct connman_technology_driver tech_driver = {
3620 .type = CONNMAN_SERVICE_TYPE_WIFI,
3621 .probe = tech_probe,
3622 .remove = tech_remove,
3623 .set_tethering = tech_set_tethering,
3624 .set_regdom = tech_set_regdom,
3627 static int wifi_init(void)
3631 err = connman_network_driver_register(&network_driver);
3635 err = g_supplicant_register(&callbacks);
3637 connman_network_driver_unregister(&network_driver);
3641 err = connman_technology_driver_register(&tech_driver);
3643 g_supplicant_unregister(&callbacks);
3644 connman_network_driver_unregister(&network_driver);
3651 static void wifi_exit(void)
3655 connman_technology_driver_unregister(&tech_driver);
3657 g_supplicant_unregister(&callbacks);
3659 connman_network_driver_unregister(&network_driver);
3662 CONNMAN_PLUGIN_DEFINE(wifi, "WiFi interface plugin", VERSION,
3663 CONNMAN_PLUGIN_PRIORITY_DEFAULT, wifi_init, wifi_exit)