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);
2515 #if defined TIZEN_EXT
2516 static bool handle_wifi_assoc_retry(struct connman_network *network,
2517 struct wifi_data *wifi)
2519 const char *security;
2521 if (!wifi->network || wifi->connected || wifi->disconnecting ||
2522 connman_network_get_connecting(network) != true) {
2523 wifi->assoc_retry_count = 0;
2527 if (wifi->state != G_SUPPLICANT_STATE_ASSOCIATING &&
2528 wifi->state != G_SUPPLICANT_STATE_ASSOCIATED) {
2529 wifi->assoc_retry_count = 0;
2533 security = connman_network_get_string(network, "WiFi.Security");
2534 if (g_str_equal(security, "ieee8021x") == true &&
2535 wifi->state == G_SUPPLICANT_STATE_ASSOCIATED) {
2536 wifi->assoc_retry_count = 0;
2540 if (++wifi->assoc_retry_count >= TIZEN_ASSOC_RETRY_COUNT) {
2541 wifi->assoc_retry_count = 0;
2543 /* Honestly it's not an invalid-key error,
2544 * however QA team recommends that the invalid-key error
2545 * might be better to display for user experience.
2547 connman_network_set_error(network, CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
2556 static void interface_state(GSupplicantInterface *interface)
2558 struct connman_network *network;
2559 struct connman_device *device;
2560 struct wifi_data *wifi;
2561 GSupplicantState state = g_supplicant_interface_get_state(interface);
2564 wifi = g_supplicant_interface_get_data(interface);
2566 DBG("wifi %p interface state %d", wifi, state);
2571 device = wifi->device;
2575 if (g_supplicant_interface_get_ready(interface) &&
2576 !wifi->interface_ready) {
2577 wifi->interface_ready = true;
2578 finalize_interface_creation(wifi);
2581 network = wifi->network;
2586 case G_SUPPLICANT_STATE_SCANNING:
2589 case G_SUPPLICANT_STATE_AUTHENTICATING:
2590 case G_SUPPLICANT_STATE_ASSOCIATING:
2591 #if defined TIZEN_EXT
2592 reset_autoscan(device);
2594 stop_autoscan(device);
2597 if (!wifi->connected)
2598 connman_network_set_associating(network, true);
2602 case G_SUPPLICANT_STATE_COMPLETED:
2603 #if defined TIZEN_EXT
2604 /* though it should be already reset: */
2605 reset_autoscan(device);
2607 wifi->assoc_retry_count = 0;
2609 wifi->scan_pending_network = NULL;
2611 /* though it should be already stopped: */
2612 stop_autoscan(device);
2615 if (!handle_wps_completion(interface, network, device, wifi))
2618 connman_network_set_connected(network, true);
2621 case G_SUPPLICANT_STATE_DISCONNECTED:
2623 * If we're in one of the idle modes, we have
2624 * not started association yet and thus setting
2625 * those ones to FALSE could cancel an association
2628 wps = connman_network_get_bool(network, "WiFi.UseWPS");
2630 if (is_idle_wps(interface, wifi))
2636 /* If previous state was 4way-handshake, then
2637 * it's either: psk was incorrect and thus we retry
2638 * or if we reach the maximum retries we declare the
2640 if (handle_4way_handshake_failure(interface,
2644 /* We disable the selected network, if not then
2645 * wpa_supplicant will loop retrying */
2646 if (g_supplicant_interface_enable_selected_network(interface,
2648 DBG("Could not disables selected network");
2650 #if defined TIZEN_EXT
2652 int reason_code = 0;
2654 err = g_supplicant_interface_remove_network(wifi->interface);
2656 DBG("Failed to remove network(%d)", err);
2658 reason_code = g_supplicant_interface_get_disconnect_reason(wifi->interface);
2660 /* Some of Wi-Fi networks are not comply Wi-Fi specification.
2661 * Retry association until its retry count is expired */
2662 if (handle_wifi_assoc_retry(network, wifi) == true) {
2663 throw_wifi_scan(wifi->device, scan_callback);
2664 wifi->scan_pending_network = wifi->network;
2668 if(reason_code > 0){
2669 DBG("Set disconnect reason code(%d)", reason_code);
2670 connman_network_set_disconnect_reason(network, reason_code);
2673 /* To avoid unnecessary repeated association in wpa_supplicant,
2674 * "RemoveNetwork" should be made when Wi-Fi is disconnected */
2675 if (wps != true && wifi->network && wifi->disconnecting == false) {
2676 wifi->disconnecting = true;
2677 err = g_supplicant_interface_disconnect(wifi->interface,
2678 disconnect_callback, wifi->network);
2680 wifi->disconnecting = false;
2682 connman_network_set_connected(network, false);
2683 connman_network_set_associating(network, false);
2685 start_autoscan(device);
2691 connman_network_set_connected(network, false);
2692 connman_network_set_associating(network, false);
2693 wifi->disconnecting = false;
2695 start_autoscan(device);
2699 case G_SUPPLICANT_STATE_INACTIVE:
2700 #if defined TIZEN_EXT
2701 if (handle_wps_completion(interface, network, device, wifi) == false)
2704 connman_network_set_associating(network, false);
2705 start_autoscan(device);
2709 case G_SUPPLICANT_STATE_UNKNOWN:
2710 case G_SUPPLICANT_STATE_DISABLED:
2711 case G_SUPPLICANT_STATE_ASSOCIATED:
2712 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
2713 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
2717 wifi->state = state;
2719 /* Saving wpa_s state policy:
2720 * If connected and if the state changes are roaming related:
2721 * --> We stay connected
2723 * --> We are connected
2725 * --> We are not connected
2728 #if defined TIZEN_EXT
2729 case G_SUPPLICANT_STATE_SCANNING:
2732 case G_SUPPLICANT_STATE_AUTHENTICATING:
2733 case G_SUPPLICANT_STATE_ASSOCIATING:
2734 case G_SUPPLICANT_STATE_ASSOCIATED:
2735 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
2736 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
2737 if (wifi->connected)
2738 connman_warn("Probably roaming right now!"
2739 " Staying connected...");
2741 wifi->connected = false;
2743 case G_SUPPLICANT_STATE_COMPLETED:
2744 wifi->connected = true;
2747 wifi->connected = false;
2754 static void interface_removed(GSupplicantInterface *interface)
2756 const char *ifname = g_supplicant_interface_get_ifname(interface);
2757 struct wifi_data *wifi;
2759 DBG("ifname %s", ifname);
2761 wifi = g_supplicant_interface_get_data(interface);
2764 wifi->interface = NULL;
2766 if (wifi && wifi->tethering)
2769 if (!wifi || !wifi->device) {
2770 DBG("wifi interface already removed");
2774 connman_device_set_powered(wifi->device, false);
2776 check_p2p_technology();
2779 static void set_device_type(const char *type, char dev_type[17])
2781 const char *oui = "0050F204";
2782 const char *category = "0100";
2783 const char *sub_category = "0000";
2785 if (!g_strcmp0(type, "handset")) {
2787 sub_category = "0500";
2788 } else if (!g_strcmp0(type, "vm") || !g_strcmp0(type, "container"))
2789 sub_category = "0100";
2790 else if (!g_strcmp0(type, "server"))
2791 sub_category = "0200";
2792 else if (!g_strcmp0(type, "laptop"))
2793 sub_category = "0500";
2794 else if (!g_strcmp0(type, "desktop"))
2795 sub_category = "0600";
2796 else if (!g_strcmp0(type, "tablet"))
2797 sub_category = "0900";
2798 else if (!g_strcmp0(type, "watch"))
2801 snprintf(dev_type, 17, "%s%s%s", category, oui, sub_category);
2804 static void p2p_support(GSupplicantInterface *interface)
2806 char dev_type[17] = {};
2807 const char *hostname;
2811 if (!g_supplicant_interface_has_p2p(interface))
2814 if (connman_technology_driver_register(&p2p_tech_driver) < 0) {
2815 DBG("Could not register P2P technology driver");
2819 hostname = connman_utsname_get_hostname();
2821 hostname = "ConnMan";
2823 set_device_type(connman_machine_get_type(), dev_type);
2824 g_supplicant_interface_set_p2p_device_config(interface,
2825 hostname, dev_type);
2826 connman_peer_driver_register(&peer_driver);
2829 static void scan_started(GSupplicantInterface *interface)
2834 static void scan_finished(GSupplicantInterface *interface)
2836 #if defined TIZEN_EXT
2837 struct wifi_data *wifi;
2838 bool is_associating = false;
2839 static bool is_scanning = true;
2844 #if defined TIZEN_EXT
2845 wifi = g_supplicant_interface_get_data(interface);
2846 if (wifi && wifi->scan_pending_network) {
2847 network_connect(wifi->scan_pending_network);
2848 wifi->scan_pending_network = NULL;
2851 //service state - associating
2852 if(!wifi || !wifi->network)
2855 is_associating = connman_network_get_associating(wifi->network);
2856 if(is_associating && is_scanning){
2857 is_scanning = false;
2858 DBG("send scan for connecting");
2859 throw_wifi_scan(wifi->device, scan_callback);
2870 static unsigned char calculate_strength(GSupplicantNetwork *supplicant_network)
2872 unsigned char strength;
2874 strength = 120 + g_supplicant_network_get_signal(supplicant_network);
2881 static void network_added(GSupplicantNetwork *supplicant_network)
2883 struct connman_network *network;
2884 GSupplicantInterface *interface;
2885 struct wifi_data *wifi;
2886 const char *name, *identifier, *security, *group, *mode;
2887 const unsigned char *ssid;
2888 unsigned int ssid_len;
2892 bool wps_advertizing;
2894 mode = g_supplicant_network_get_mode(supplicant_network);
2895 identifier = g_supplicant_network_get_identifier(supplicant_network);
2897 DBG("%s", identifier);
2899 if (!g_strcmp0(mode, "adhoc"))
2902 interface = g_supplicant_network_get_interface(supplicant_network);
2903 wifi = g_supplicant_interface_get_data(interface);
2904 name = g_supplicant_network_get_name(supplicant_network);
2905 security = g_supplicant_network_get_security(supplicant_network);
2906 group = g_supplicant_network_get_identifier(supplicant_network);
2907 wps = g_supplicant_network_get_wps(supplicant_network);
2908 wps_pbc = g_supplicant_network_is_wps_pbc(supplicant_network);
2909 wps_ready = g_supplicant_network_is_wps_active(supplicant_network);
2910 wps_advertizing = g_supplicant_network_is_wps_advertizing(
2911 supplicant_network);
2916 ssid = g_supplicant_network_get_ssid(supplicant_network, &ssid_len);
2918 network = connman_device_get_network(wifi->device, identifier);
2921 network = connman_network_create(identifier,
2922 CONNMAN_NETWORK_TYPE_WIFI);
2926 connman_network_set_index(network, wifi->index);
2928 if (connman_device_add_network(wifi->device, network) < 0) {
2929 connman_network_unref(network);
2933 wifi->networks = g_slist_prepend(wifi->networks, network);
2936 if (name && name[0] != '\0')
2937 connman_network_set_name(network, name);
2939 connman_network_set_blob(network, "WiFi.SSID",
2941 connman_network_set_string(network, "WiFi.Security", security);
2942 connman_network_set_strength(network,
2943 calculate_strength(supplicant_network));
2944 connman_network_set_bool(network, "WiFi.WPS", wps);
2947 /* Is AP advertizing for WPS association?
2948 * If so, we decide to use WPS by default */
2949 if (wps_ready && wps_pbc &&
2951 #if !defined TIZEN_EXT
2952 connman_network_set_bool(network, "WiFi.UseWPS", true);
2954 DBG("wps is activating by ap but ignore it.");
2959 connman_network_set_frequency(network,
2960 g_supplicant_network_get_frequency(supplicant_network));
2961 #if defined TIZEN_EXT
2962 connman_network_set_bssid(network,
2963 g_supplicant_network_get_bssid(supplicant_network));
2964 connman_network_set_maxrate(network,
2965 g_supplicant_network_get_maxrate(supplicant_network));
2966 connman_network_set_enc_mode(network,
2967 g_supplicant_network_get_enc_mode(supplicant_network));
2968 connman_network_set_rsn_mode(network,
2969 g_supplicant_network_get_rsn_mode(supplicant_network));
2970 connman_network_set_keymgmt(network,
2971 g_supplicant_network_get_keymgmt(supplicant_network));
2973 connman_network_set_available(network, true);
2974 connman_network_set_string(network, "WiFi.Mode", mode);
2976 #if defined TIZEN_EXT
2981 connman_network_set_group(network, group);
2983 #if defined TIZEN_EXT
2984 if (wifi_first_scan == true)
2985 found_with_first_scan = true;
2988 if (wifi->hidden && ssid) {
2989 if (!g_strcmp0(wifi->hidden->security, security) &&
2990 wifi->hidden->ssid_len == ssid_len &&
2991 !memcmp(wifi->hidden->ssid, ssid, ssid_len)) {
2992 connman_network_connect_hidden(network,
2993 wifi->hidden->identity,
2994 wifi->hidden->passphrase,
2995 wifi->hidden->user_data);
2996 wifi->hidden->user_data = NULL;
2997 hidden_free(wifi->hidden);
2998 wifi->hidden = NULL;
3003 static void network_removed(GSupplicantNetwork *network)
3005 GSupplicantInterface *interface;
3006 struct wifi_data *wifi;
3007 const char *name, *identifier;
3008 struct connman_network *connman_network;
3010 interface = g_supplicant_network_get_interface(network);
3011 wifi = g_supplicant_interface_get_data(interface);
3012 identifier = g_supplicant_network_get_identifier(network);
3013 name = g_supplicant_network_get_name(network);
3015 DBG("name %s", name);
3020 connman_network = connman_device_get_network(wifi->device, identifier);
3021 if (!connman_network)
3024 #if defined TIZEN_EXT
3025 if (connman_network == wifi->scan_pending_network)
3026 wifi->scan_pending_network = NULL;
3028 if (connman_network == wifi->pending_network)
3029 wifi->pending_network = NULL;
3031 if(connman_network_get_connecting(connman_network) == true){
3032 connman_network_set_connected(connman_network, false);
3036 wifi->networks = g_slist_remove(wifi->networks, connman_network);
3038 connman_device_remove_network(wifi->device, connman_network);
3039 connman_network_unref(connman_network);
3042 static void network_changed(GSupplicantNetwork *network, const char *property)
3044 GSupplicantInterface *interface;
3045 struct wifi_data *wifi;
3046 const char *name, *identifier;
3047 struct connman_network *connman_network;
3049 #if defined TIZEN_EXT
3050 const unsigned char *bssid;
3051 unsigned int maxrate;
3056 interface = g_supplicant_network_get_interface(network);
3057 wifi = g_supplicant_interface_get_data(interface);
3058 identifier = g_supplicant_network_get_identifier(network);
3059 name = g_supplicant_network_get_name(network);
3061 DBG("name %s", name);
3066 connman_network = connman_device_get_network(wifi->device, identifier);
3067 if (!connman_network)
3070 if (g_str_equal(property, "Signal")) {
3071 connman_network_set_strength(connman_network,
3072 calculate_strength(network));
3073 connman_network_update(connman_network);
3076 #if defined TIZEN_EXT
3077 bssid = g_supplicant_network_get_bssid(network);
3078 maxrate = g_supplicant_network_get_maxrate(network);
3079 frequency = g_supplicant_network_get_frequency(network);
3080 wps = g_supplicant_network_get_wps(network);
3082 connman_network_set_bssid(connman_network, bssid);
3083 connman_network_set_maxrate(connman_network, maxrate);
3084 connman_network_set_frequency(connman_network, frequency);
3085 connman_network_set_bool(connman_network, "WiFi.WPS", wps);
3089 static void apply_peer_services(GSupplicantPeer *peer,
3090 struct connman_peer *connman_peer)
3092 const unsigned char *data;
3097 connman_peer_reset_services(connman_peer);
3099 data = g_supplicant_peer_get_widi_ies(peer, &length);
3101 connman_peer_add_service(connman_peer,
3102 CONNMAN_PEER_SERVICE_WIFI_DISPLAY, data, length);
3106 static void add_station(const char *mac)
3108 connman_technology_tethering_add_station(CONNMAN_SERVICE_TYPE_WIFI,
3112 static void remove_station(const char *mac)
3114 connman_technology_tethering_remove_station(mac);
3117 static void peer_found(GSupplicantPeer *peer)
3119 GSupplicantInterface *iface = g_supplicant_peer_get_interface(peer);
3120 struct wifi_data *wifi = g_supplicant_interface_get_data(iface);
3121 struct connman_peer *connman_peer;
3122 const char *identifier, *name;
3125 identifier = g_supplicant_peer_get_identifier(peer);
3126 name = g_supplicant_peer_get_name(peer);
3128 DBG("ident: %s", identifier);
3130 connman_peer = connman_peer_get(wifi->device, identifier);
3134 connman_peer = connman_peer_create(identifier);
3135 connman_peer_set_name(connman_peer, name);
3136 connman_peer_set_device(connman_peer, wifi->device);
3137 apply_peer_services(peer, connman_peer);
3139 ret = connman_peer_register(connman_peer);
3140 if (ret < 0 && ret != -EALREADY)
3141 connman_peer_unref(connman_peer);
3144 static void peer_lost(GSupplicantPeer *peer)
3146 GSupplicantInterface *iface = g_supplicant_peer_get_interface(peer);
3147 struct wifi_data *wifi = g_supplicant_interface_get_data(iface);
3148 struct connman_peer *connman_peer;
3149 const char *identifier;
3154 identifier = g_supplicant_peer_get_identifier(peer);
3156 DBG("ident: %s", identifier);
3158 connman_peer = connman_peer_get(wifi->device, identifier);
3160 if (wifi->p2p_connecting &&
3161 wifi->pending_peer == connman_peer) {
3162 peer_connect_timeout(wifi);
3164 connman_peer_unregister(connman_peer);
3165 connman_peer_unref(connman_peer);
3169 static void peer_changed(GSupplicantPeer *peer, GSupplicantPeerState state)
3171 GSupplicantInterface *iface = g_supplicant_peer_get_interface(peer);
3172 struct wifi_data *wifi = g_supplicant_interface_get_data(iface);
3173 enum connman_peer_state p_state = CONNMAN_PEER_STATE_UNKNOWN;
3174 struct connman_peer *connman_peer;
3175 const char *identifier;
3177 identifier = g_supplicant_peer_get_identifier(peer);
3179 DBG("ident: %s", identifier);
3181 connman_peer = connman_peer_get(wifi->device, identifier);
3186 case G_SUPPLICANT_PEER_SERVICES_CHANGED:
3187 apply_peer_services(peer, connman_peer);
3188 connman_peer_services_changed(connman_peer);
3190 case G_SUPPLICANT_PEER_GROUP_CHANGED:
3191 if (!g_supplicant_peer_is_in_a_group(peer))
3192 p_state = CONNMAN_PEER_STATE_IDLE;
3194 p_state = CONNMAN_PEER_STATE_CONFIGURATION;
3196 case G_SUPPLICANT_PEER_GROUP_STARTED:
3198 case G_SUPPLICANT_PEER_GROUP_FINISHED:
3199 p_state = CONNMAN_PEER_STATE_IDLE;
3201 case G_SUPPLICANT_PEER_GROUP_JOINED:
3202 connman_peer_set_iface_address(connman_peer,
3203 g_supplicant_peer_get_iface_address(peer));
3205 case G_SUPPLICANT_PEER_GROUP_DISCONNECTED:
3206 p_state = CONNMAN_PEER_STATE_IDLE;
3208 case G_SUPPLICANT_PEER_GROUP_FAILED:
3209 if (g_supplicant_peer_has_requested_connection(peer))
3210 p_state = CONNMAN_PEER_STATE_IDLE;
3212 p_state = CONNMAN_PEER_STATE_FAILURE;
3216 if (p_state == CONNMAN_PEER_STATE_CONFIGURATION ||
3217 p_state == CONNMAN_PEER_STATE_FAILURE) {
3218 if (wifi->p2p_connecting
3219 && connman_peer == wifi->pending_peer)
3220 peer_cancel_timeout(wifi);
3222 p_state = CONNMAN_PEER_STATE_UNKNOWN;
3225 if (p_state == CONNMAN_PEER_STATE_UNKNOWN)
3228 if (p_state == CONNMAN_PEER_STATE_CONFIGURATION) {
3229 GSupplicantInterface *g_iface;
3230 struct wifi_data *g_wifi;
3232 g_iface = g_supplicant_peer_get_group_interface(peer);
3236 g_wifi = g_supplicant_interface_get_data(g_iface);
3240 connman_peer_set_as_master(connman_peer,
3241 !g_supplicant_peer_is_client(peer));
3242 connman_peer_set_sub_device(connman_peer, g_wifi->device);
3245 connman_peer_set_state(connman_peer, p_state);
3248 static void peer_request(GSupplicantPeer *peer)
3250 GSupplicantInterface *iface = g_supplicant_peer_get_interface(peer);
3251 struct wifi_data *wifi = g_supplicant_interface_get_data(iface);
3252 struct connman_peer *connman_peer;
3253 const char *identifier;
3255 identifier = g_supplicant_peer_get_identifier(peer);
3257 DBG("ident: %s", identifier);
3259 connman_peer = connman_peer_get(wifi->device, identifier);
3263 connman_peer_request_connection(connman_peer);
3266 #if defined TIZEN_EXT
3267 static void system_power_off(void)
3270 struct wifi_data *wifi;
3271 struct connman_service *service;
3272 struct connman_ipconfig *ipconfig_ipv4;
3274 if (connman_setting_get_bool("WiFiDHCPRelease") == true) {
3275 for (list = iface_list; list; list = list->next) {
3278 if (wifi->network != NULL) {
3279 service = connman_service_lookup_from_network(wifi->network);
3280 ipconfig_ipv4 = __connman_service_get_ip4config(service);
3281 __connman_dhcp_stop(ipconfig_ipv4);
3287 static void network_merged(GSupplicantNetwork *network)
3289 GSupplicantInterface *interface;
3290 GSupplicantState state;
3291 struct wifi_data *wifi;
3292 const char *identifier;
3293 struct connman_network *connman_network;
3294 unsigned int ishs20AP = 0;
3297 interface = g_supplicant_network_get_interface(network);
3301 state = g_supplicant_interface_get_state(interface);
3302 if (state < G_SUPPLICANT_STATE_AUTHENTICATING)
3305 wifi = g_supplicant_interface_get_data(interface);
3309 identifier = g_supplicant_network_get_identifier(network);
3311 connman_network = connman_device_get_network(wifi->device, identifier);
3312 if (!connman_network)
3315 DBG("merged identifier %s", identifier);
3317 if (wifi->connected == FALSE) {
3319 case G_SUPPLICANT_STATE_AUTHENTICATING:
3320 case G_SUPPLICANT_STATE_ASSOCIATING:
3321 case G_SUPPLICANT_STATE_ASSOCIATED:
3322 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
3323 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
3324 connman_network_set_associating(connman_network, TRUE);
3326 case G_SUPPLICANT_STATE_COMPLETED:
3327 connman_network_set_connected(connman_network, TRUE);
3330 DBG("Not handled the state : %d", state);
3335 ishs20AP = g_supplicant_network_is_hs20AP(network);
3336 connman_network_set_is_hs20AP(connman_network, ishs20AP);
3339 g_strcmp0(g_supplicant_network_get_security(network), "ieee8021x") == 0) {
3340 temp = g_ascii_strdown(g_supplicant_network_get_eap(network), -1);
3341 connman_network_set_string(connman_network, "WiFi.EAP",
3343 connman_network_set_string(connman_network, "WiFi.Identity",
3344 g_supplicant_network_get_identity(network));
3345 connman_network_set_string(connman_network, "WiFi.Phase2",
3346 g_supplicant_network_get_phase2(network));
3351 wifi->network = connman_network;
3355 static void debug(const char *str)
3357 if (getenv("CONNMAN_SUPPLICANT_DEBUG"))
3358 connman_debug("%s", str);
3361 static const GSupplicantCallbacks callbacks = {
3362 .system_ready = system_ready,
3363 .system_killed = system_killed,
3364 .interface_added = interface_added,
3365 .interface_state = interface_state,
3366 .interface_removed = interface_removed,
3367 .p2p_support = p2p_support,
3368 .scan_started = scan_started,
3369 .scan_finished = scan_finished,
3370 .network_added = network_added,
3371 .network_removed = network_removed,
3372 .network_changed = network_changed,
3373 .add_station = add_station,
3374 .remove_station = remove_station,
3375 .peer_found = peer_found,
3376 .peer_lost = peer_lost,
3377 .peer_changed = peer_changed,
3378 .peer_request = peer_request,
3379 #if defined TIZEN_EXT
3380 .system_power_off = system_power_off,
3381 .network_merged = network_merged,
3387 static int tech_probe(struct connman_technology *technology)
3389 wifi_technology = technology;
3394 static void tech_remove(struct connman_technology *technology)
3396 wifi_technology = NULL;
3399 struct wifi_tethering_info {
3400 struct wifi_data *wifi;
3401 struct connman_technology *technology;
3403 GSupplicantSSID *ssid;
3406 static GSupplicantSSID *ssid_ap_init(const char *ssid,
3407 const char *passphrase, bool hidden)
3409 GSupplicantSSID *ap;
3411 ap = g_try_malloc0(sizeof(GSupplicantSSID));
3415 ap->mode = G_SUPPLICANT_MODE_MASTER;
3417 ap->ssid_len = strlen(ssid);
3421 if (!passphrase || strlen(passphrase) == 0) {
3422 ap->security = G_SUPPLICANT_SECURITY_NONE;
3423 ap->passphrase = NULL;
3425 ap->security = G_SUPPLICANT_SECURITY_PSK;
3426 ap->protocol = G_SUPPLICANT_PROTO_RSN;
3427 ap->pairwise_cipher = G_SUPPLICANT_PAIRWISE_CCMP;
3428 ap->group_cipher = G_SUPPLICANT_GROUP_CCMP;
3429 ap->passphrase = passphrase;
3433 ap->ignore_broadcast_ssid =
3434 G_SUPPLICANT_AP_HIDDEN_SSID_ZERO_CONTENTS;
3436 ap->ignore_broadcast_ssid = G_SUPPLICANT_AP_NO_SSID_HIDING;
3441 static void ap_start_callback(int result, GSupplicantInterface *interface,
3444 struct wifi_tethering_info *info = user_data;
3446 DBG("result %d index %d bridge %s",
3447 result, info->wifi->index, info->wifi->bridge);
3450 connman_inet_remove_from_bridge(info->wifi->index,
3451 info->wifi->bridge);
3452 connman_technology_tethering_notify(info->technology, false);
3455 g_free(info->ifname);
3459 static void ap_create_callback(int result,
3460 GSupplicantInterface *interface,
3463 struct wifi_tethering_info *info = user_data;
3465 DBG("result %d ifname %s", result,
3466 g_supplicant_interface_get_ifname(interface));
3469 connman_inet_remove_from_bridge(info->wifi->index,
3470 info->wifi->bridge);
3471 connman_technology_tethering_notify(info->technology, false);
3473 g_free(info->ifname);
3479 info->wifi->interface = interface;
3480 g_supplicant_interface_set_data(interface, info->wifi);
3482 if (g_supplicant_interface_set_apscan(interface, 2) < 0)
3483 connman_error("Failed to set interface ap_scan property");
3485 g_supplicant_interface_connect(interface, info->ssid,
3486 ap_start_callback, info);
3489 static void sta_remove_callback(int result,
3490 GSupplicantInterface *interface,
3493 struct wifi_tethering_info *info = user_data;
3494 const char *driver = connman_option_get_string("wifi");
3496 DBG("ifname %s result %d ", info->ifname, result);
3499 info->wifi->tethering = true;
3501 g_free(info->ifname);
3507 info->wifi->interface = NULL;
3509 connman_technology_tethering_notify(info->technology, true);
3511 g_supplicant_interface_create(info->ifname, driver, info->wifi->bridge,
3516 static int tech_set_tethering(struct connman_technology *technology,
3517 const char *identifier, const char *passphrase,
3518 const char *bridge, bool enabled, bool hidden)
3521 GSupplicantInterface *interface;
3522 struct wifi_data *wifi;
3523 struct wifi_tethering_info *info;
3531 for (list = iface_list; list; list = list->next) {
3534 if (wifi->tethering) {
3535 wifi->tethering = false;
3537 connman_inet_remove_from_bridge(wifi->index,
3539 wifi->bridged = false;
3543 connman_technology_tethering_notify(technology, false);
3548 for (list = iface_list; list; list = list->next) {
3551 interface = wifi->interface;
3556 ifname = g_supplicant_interface_get_ifname(wifi->interface);
3558 mode = g_supplicant_interface_get_mode(interface);
3559 if ((mode & G_SUPPLICANT_CAPABILITY_MODE_AP) == 0) {
3560 DBG("%s does not support AP mode", ifname);
3564 info = g_try_malloc0(sizeof(struct wifi_tethering_info));
3569 info->technology = technology;
3570 info->wifi->bridge = bridge;
3571 info->ssid = ssid_ap_init(identifier, passphrase, hidden);
3576 info->ifname = g_strdup(ifname);
3577 if (!info->ifname) {
3583 info->wifi->tethering = true;
3585 err = g_supplicant_interface_remove(interface,
3586 sta_remove_callback,
3595 static void regdom_callback(int result, const char *alpha2, void *user_data)
3599 if (!wifi_technology)
3605 connman_technology_regdom_notify(wifi_technology, alpha2);
3608 static int tech_set_regdom(struct connman_technology *technology, const char *alpha2)
3610 return g_supplicant_set_country(alpha2, regdom_callback, NULL);
3613 static struct connman_technology_driver tech_driver = {
3615 .type = CONNMAN_SERVICE_TYPE_WIFI,
3616 .probe = tech_probe,
3617 .remove = tech_remove,
3618 .set_tethering = tech_set_tethering,
3619 .set_regdom = tech_set_regdom,
3622 static int wifi_init(void)
3626 err = connman_network_driver_register(&network_driver);
3630 err = g_supplicant_register(&callbacks);
3632 connman_network_driver_unregister(&network_driver);
3636 err = connman_technology_driver_register(&tech_driver);
3638 g_supplicant_unregister(&callbacks);
3639 connman_network_driver_unregister(&network_driver);
3646 static void wifi_exit(void)
3650 connman_technology_driver_unregister(&tech_driver);
3652 g_supplicant_unregister(&callbacks);
3654 connman_network_driver_unregister(&network_driver);
3657 CONNMAN_PLUGIN_DEFINE(wifi, "WiFi interface plugin", VERSION,
3658 CONNMAN_PLUGIN_PRIORITY_DEFAULT, wifi_init, wifi_exit)