Tethering: Add station information management feature
[platform/upstream/connman.git] / plugins / wifi.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2014  Intel Corporation. All rights reserved.
6  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <unistd.h>
27 #include <stdlib.h>
28 #include <errno.h>
29 #include <stdio.h>
30 #include <string.h>
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>
36
37 #ifndef IFF_LOWER_UP
38 #define IFF_LOWER_UP    0x10000
39 #endif
40
41 #include <dbus/dbus.h>
42 #include <glib.h>
43
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>
59
60 #include <gsupplicant/gsupplicant.h>
61
62 #define CLEANUP_TIMEOUT   8     /* in seconds */
63 #define INACTIVE_TIMEOUT  12    /* in seconds */
64 #define FAVORITE_MAXIMUM_RETRIES 2
65
66 #define BGSCAN_DEFAULT "simple:30:-45:300"
67 #define AUTOSCAN_DEFAULT "exponential:3:300"
68
69 #define P2P_FIND_TIMEOUT 30
70 #define P2P_CONNECTION_TIMEOUT 100
71 #define P2P_LISTEN_PERIOD 500
72 #define P2P_LISTEN_INTERVAL 2000
73
74 static struct connman_technology *wifi_technology = NULL;
75 static struct connman_technology *p2p_technology = NULL;
76
77 struct hidden_params {
78         char ssid[32];
79         unsigned int ssid_len;
80         char *identity;
81         char *passphrase;
82         char *security;
83         GSupplicantScanParams *scan_params;
84         gpointer user_data;
85 };
86
87 /**
88  * Used for autoscan "emulation".
89  * Should be removed when wpa_s autoscan support will be by default.
90  */
91 struct autoscan_params {
92         int base;
93         int limit;
94         int interval;
95         unsigned int timeout;
96 };
97
98 struct wifi_data {
99         char *identifier;
100         struct connman_device *device;
101         struct connman_network *network;
102         struct connman_network *pending_network;
103         GSList *networks;
104         GSupplicantInterface *interface;
105         GSupplicantState state;
106         bool connected;
107         bool disconnecting;
108         bool tethering;
109         bool bridged;
110         bool interface_ready;
111         const char *bridge;
112         int index;
113         unsigned flags;
114         unsigned int watch;
115         int retries;
116         struct hidden_params *hidden;
117         bool postpone_hidden;
118         /**
119          * autoscan "emulation".
120          */
121         struct autoscan_params *autoscan;
122
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;
128         bool p2p_connecting;
129         bool p2p_device;
130         int servicing;
131 };
132
133 static GList *iface_list = NULL;
134
135 static GList *pending_wifi_device = NULL;
136 static GList *p2p_iface_list = NULL;
137 bool wfd_service_registered = false;
138
139 static void start_autoscan(struct connman_device *device);
140
141 static int p2p_tech_probe(struct connman_technology *technology)
142 {
143         p2p_technology = technology;
144
145         return 0;
146 }
147
148 static void p2p_tech_remove(struct connman_technology *technology)
149 {
150         p2p_technology = NULL;
151 }
152
153 static struct connman_technology_driver p2p_tech_driver = {
154         .name           = "p2p",
155         .type           = CONNMAN_SERVICE_TYPE_P2P,
156         .probe          = p2p_tech_probe,
157         .remove         = p2p_tech_remove,
158 };
159
160 static bool is_p2p_connecting(void)
161 {
162         GList *list;
163
164         for (list = iface_list; list; list = list->next) {
165                 struct wifi_data *wifi = list->data;
166
167                 if (wifi->p2p_connecting)
168                         return true;
169         }
170
171         return false;
172 }
173
174 static void add_pending_wifi_device(struct wifi_data *wifi)
175 {
176         if (g_list_find(pending_wifi_device, wifi))
177                 return;
178
179         pending_wifi_device = g_list_append(pending_wifi_device, wifi);
180 }
181
182 static struct wifi_data *get_pending_wifi_data(const char *ifname)
183 {
184         GList *list;
185
186         for (list = pending_wifi_device; list; list = list->next) {
187                 struct wifi_data *wifi;
188                 const char *dev_name;
189
190                 wifi = list->data;
191                 if (!wifi || !wifi->device)
192                         continue;
193
194                 dev_name = connman_device_get_string(wifi->device, "Interface");
195                 if (!g_strcmp0(ifname, dev_name)) {
196                         pending_wifi_device = g_list_delete_link(
197                                                 pending_wifi_device, list);
198                         return wifi;
199                 }
200         }
201
202         return NULL;
203 }
204
205 static void remove_pending_wifi_device(struct wifi_data *wifi)
206 {
207         GList *link;
208
209         link = g_list_find(pending_wifi_device, wifi);
210
211         if (!link)
212                 return;
213
214         pending_wifi_device = g_list_delete_link(pending_wifi_device, link);
215 }
216
217 static void peer_cancel_timeout(struct wifi_data *wifi)
218 {
219         if (wifi->p2p_connection_timeout > 0)
220                 g_source_remove(wifi->p2p_connection_timeout);
221
222         wifi->p2p_connection_timeout = 0;
223         wifi->p2p_connecting = false;
224
225         if (wifi->pending_peer) {
226                 connman_peer_unref(wifi->pending_peer);
227                 wifi->pending_peer = NULL;
228         }
229
230         wifi->peer = NULL;
231 }
232
233 static gboolean peer_connect_timeout(gpointer data)
234 {
235         struct wifi_data *wifi = data;
236
237         DBG("");
238
239         if (wifi->p2p_connecting) {
240                 enum connman_peer_state state = CONNMAN_PEER_STATE_FAILURE;
241
242                 if (g_supplicant_peer_has_requested_connection(wifi->peer))
243                         state = CONNMAN_PEER_STATE_IDLE;
244
245                 connman_peer_set_state(wifi->pending_peer, state);
246         }
247
248         peer_cancel_timeout(wifi);
249
250         return FALSE;
251 }
252
253 static void peer_connect_callback(int result, GSupplicantInterface *interface,
254                                                         void *user_data)
255 {
256         struct wifi_data *wifi = user_data;
257         struct connman_peer *peer = wifi->pending_peer;
258
259         DBG("peer %p - %d", peer, result);
260
261         if (!peer)
262                 return;
263
264         if (result < 0) {
265                 peer_connect_timeout(wifi);
266                 return;
267         }
268
269         connman_peer_set_state(peer, CONNMAN_PEER_STATE_ASSOCIATION);
270
271         wifi->p2p_connection_timeout = g_timeout_add_seconds(
272                                                 P2P_CONNECTION_TIMEOUT,
273                                                 peer_connect_timeout, wifi);
274 }
275
276 static int peer_connect(struct connman_peer *peer,
277                         enum connman_peer_wps_method wps_method,
278                         const char *wps_pin)
279 {
280         struct connman_device *device = connman_peer_get_device(peer);
281         GSupplicantPeerParams *peer_params;
282         GSupplicantPeer *gs_peer;
283         struct wifi_data *wifi;
284         bool pbc, pin;
285         int ret;
286
287         DBG("peer %p", peer);
288
289         if (!device)
290                 return -ENODEV;
291
292         wifi = connman_device_get_data(device);
293         if (!wifi)
294                 return -ENODEV;
295
296         if (wifi->p2p_connecting)
297                 return -EBUSY;
298
299         wifi->peer = NULL;
300
301         gs_peer = g_supplicant_interface_peer_lookup(wifi->interface,
302                                         connman_peer_get_identifier(peer));
303         if (!gs_peer)
304                 return -EINVAL;
305
306         pbc = g_supplicant_peer_is_wps_pbc(gs_peer);
307         pin = g_supplicant_peer_is_wps_pin(gs_peer);
308
309         switch (wps_method) {
310         case CONNMAN_PEER_WPS_UNKNOWN:
311                 if ((pbc && pin) || pin)
312                         return -ENOKEY;
313                 break;
314         case CONNMAN_PEER_WPS_PBC:
315                 if (!pbc)
316                         return -EINVAL;
317                 wps_pin = NULL;
318                 break;
319         case CONNMAN_PEER_WPS_PIN:
320                 if (!pin || !wps_pin)
321                         return -EINVAL;
322                 break;
323         }
324
325         peer_params = g_try_malloc0(sizeof(GSupplicantPeerParams));
326         if (!peer_params)
327                 return -ENOMEM;
328
329         peer_params->path = g_strdup(g_supplicant_peer_get_path(gs_peer));
330         if (wps_pin)
331                 peer_params->wps_pin = g_strdup(wps_pin);
332
333         peer_params->master = connman_peer_service_is_master();
334
335         ret = g_supplicant_interface_p2p_connect(wifi->interface, peer_params,
336                                                 peer_connect_callback, wifi);
337         if (ret == -EINPROGRESS) {
338                 wifi->pending_peer = connman_peer_ref(peer);
339                 wifi->peer = gs_peer;
340                 wifi->p2p_connecting = true;
341         } else if (ret < 0)
342                 g_free(peer_params);
343
344         return ret;
345 }
346
347 static int peer_disconnect(struct connman_peer *peer)
348 {
349         struct connman_device *device = connman_peer_get_device(peer);
350         GSupplicantPeerParams peer_params = {};
351         GSupplicantPeer *gs_peer;
352         struct wifi_data *wifi;
353         int ret;
354
355         DBG("peer %p", peer);
356
357         if (!device)
358                 return -ENODEV;
359
360         wifi = connman_device_get_data(device);
361         if (!wifi)
362                 return -ENODEV;
363
364         gs_peer = g_supplicant_interface_peer_lookup(wifi->interface,
365                                         connman_peer_get_identifier(peer));
366         if (!gs_peer)
367                 return -EINVAL;
368
369         peer_params.path = g_strdup(g_supplicant_peer_get_path(gs_peer));
370
371         ret = g_supplicant_interface_p2p_disconnect(wifi->interface,
372                                                         &peer_params);
373         g_free(peer_params.path);
374
375         if (ret == -EINPROGRESS)
376                 peer_cancel_timeout(wifi);
377
378         return ret;
379 }
380
381 struct peer_service_registration {
382         peer_service_registration_cb_t callback;
383         void *user_data;
384 };
385
386 static bool is_service_wfd(const unsigned char *specs, int length)
387 {
388         if (length < 9 || specs[0] != 0 || specs[1] != 0 || specs[2] != 6)
389                 return false;
390
391         return true;
392 }
393
394 static void apply_p2p_listen_on_iface(gpointer data, gpointer user_data)
395 {
396         struct wifi_data *wifi = data;
397
398         if (!wifi->interface ||
399                         !g_supplicant_interface_has_p2p(wifi->interface))
400                 return;
401
402         if (!wifi->servicing) {
403                 g_supplicant_interface_p2p_listen(wifi->interface,
404                                 P2P_LISTEN_PERIOD, P2P_LISTEN_INTERVAL);
405         }
406
407         wifi->servicing++;
408 }
409
410 static void register_wfd_service_cb(int result,
411                                 GSupplicantInterface *iface, void *user_data)
412 {
413         struct peer_service_registration *reg_data = user_data;
414
415         DBG("");
416
417         if (result == 0)
418                 g_list_foreach(iface_list, apply_p2p_listen_on_iface, NULL);
419
420         if (reg_data && reg_data->callback) {
421                 reg_data->callback(result, reg_data->user_data);
422                 g_free(reg_data);
423         }
424 }
425
426 static GSupplicantP2PServiceParams *fill_in_peer_service_params(
427                                 const unsigned char *spec,
428                                 int spec_length, const unsigned char *query,
429                                 int query_length, int version)
430 {
431         GSupplicantP2PServiceParams *params;
432
433         params = g_try_malloc0(sizeof(GSupplicantP2PServiceParams));
434         if (!params)
435                 return NULL;
436
437         if (version > 0) {
438                 params->version = version;
439                 params->service = g_memdup(spec, spec_length);
440         } else if (query_length > 0 && spec_length > 0) {
441                 params->query = g_memdup(query, query_length);
442                 params->query_length = query_length;
443
444                 params->response = g_memdup(spec, spec_length);
445                 params->response_length = spec_length;
446         } else {
447                 params->wfd_ies = g_memdup(spec, spec_length);
448                 params->wfd_ies_length = spec_length;
449         }
450
451         return params;
452 }
453
454 static void free_peer_service_params(GSupplicantP2PServiceParams *params)
455 {
456         if (!params)
457                 return;
458
459         g_free(params->service);
460         g_free(params->query);
461         g_free(params->response);
462         g_free(params->wfd_ies);
463
464         g_free(params);
465 }
466
467 static int peer_register_wfd_service(const unsigned char *specification,
468                                 int specification_length,
469                                 peer_service_registration_cb_t callback,
470                                 void *user_data)
471 {
472         struct peer_service_registration *reg_data = NULL;
473         static GSupplicantP2PServiceParams *params;
474         int ret;
475
476         DBG("");
477
478         if (wfd_service_registered)
479                 return -EBUSY;
480
481         params = fill_in_peer_service_params(specification,
482                                         specification_length, NULL, 0, 0);
483         if (!params)
484                 return -ENOMEM;
485
486         reg_data = g_try_malloc0(sizeof(*reg_data));
487         if (!reg_data) {
488                 ret = -ENOMEM;
489                 goto error;
490         }
491
492         reg_data->callback = callback;
493         reg_data->user_data = user_data;
494
495         ret = g_supplicant_set_widi_ies(params,
496                                         register_wfd_service_cb, reg_data);
497         if (ret < 0 && ret != -EINPROGRESS)
498                 goto error;
499
500         wfd_service_registered = true;
501
502         return ret;
503 error:
504         free_peer_service_params(params);
505         g_free(reg_data);
506
507         return ret;
508 }
509
510 static void register_peer_service_cb(int result,
511                                 GSupplicantInterface *iface, void *user_data)
512 {
513         struct wifi_data *wifi = g_supplicant_interface_get_data(iface);
514         struct peer_service_registration *reg_data = user_data;
515
516         DBG("");
517
518         if (result == 0)
519                 apply_p2p_listen_on_iface(wifi, NULL);
520
521         if (reg_data->callback)
522                 reg_data->callback(result, reg_data->user_data);
523
524         g_free(reg_data);
525 }
526
527 static int peer_register_service(const unsigned char *specification,
528                                 int specification_length,
529                                 const unsigned char *query,
530                                 int query_length, int version,
531                                 peer_service_registration_cb_t callback,
532                                 void *user_data)
533 {
534         struct peer_service_registration *reg_data;
535         GSupplicantP2PServiceParams *params;
536         bool found = false;
537         int ret, ret_f;
538         GList *list;
539
540         DBG("");
541
542         if (specification && !version && !query &&
543                         is_service_wfd(specification, specification_length)) {
544                 return peer_register_wfd_service(specification,
545                                 specification_length, callback, user_data);
546         }
547
548         reg_data = g_try_malloc0(sizeof(*reg_data));
549         if (!reg_data)
550                 return -ENOMEM;
551
552         reg_data->callback = callback;
553         reg_data->user_data = user_data;
554
555         ret_f = -EOPNOTSUPP;
556
557         for (list = iface_list; list; list = list->next) {
558                 struct wifi_data *wifi = list->data;
559                 GSupplicantInterface *iface = wifi->interface;
560
561                 if (!g_supplicant_interface_has_p2p(iface))
562                         continue;
563
564                 params = fill_in_peer_service_params(specification,
565                                                 specification_length, query,
566                                                 query_length, version);
567                 if (!params) {
568                         ret = -ENOMEM;
569                         continue;
570                 }
571
572                 if (!found) {
573                         ret_f = g_supplicant_interface_p2p_add_service(iface,
574                                 register_peer_service_cb, params, reg_data);
575                         if (ret_f == 0 || ret_f == -EINPROGRESS)
576                                 found = true;
577                         ret = ret_f;
578                 } else
579                         ret = g_supplicant_interface_p2p_add_service(iface,
580                                 register_peer_service_cb, params, NULL);
581                 if (ret != 0 && ret != -EINPROGRESS)
582                         free_peer_service_params(params);
583         }
584
585         if (ret_f != 0 && ret_f != -EINPROGRESS)
586                 g_free(reg_data);
587
588         return ret_f;
589 }
590
591 static int peer_unregister_wfd_service(void)
592 {
593         GSupplicantP2PServiceParams *params;
594         GList *list;
595
596         if (!wfd_service_registered)
597                 return -EALREADY;
598
599         params = fill_in_peer_service_params(NULL, 0, NULL, 0, 0);
600         if (!params)
601                 return -ENOMEM;
602
603         wfd_service_registered = false;
604
605         g_supplicant_set_widi_ies(params, NULL, NULL);
606
607         for (list = iface_list; list; list = list->next) {
608                 struct wifi_data *wifi = list->data;
609
610                 if (!g_supplicant_interface_has_p2p(wifi->interface))
611                         continue;
612
613                 wifi->servicing--;
614                 if (!wifi->servicing || wifi->servicing < 0) {
615                         g_supplicant_interface_p2p_listen(wifi->interface,
616                                                                         0, 0);
617                         wifi->servicing = 0;
618                 }
619         }
620
621         return 0;
622 }
623
624 static int peer_unregister_service(const unsigned char *specification,
625                                                 int specification_length,
626                                                 const unsigned char *query,
627                                                 int query_length, int version)
628 {
629         GSupplicantP2PServiceParams *params;
630         bool wfd = false;
631         GList *list;
632         int ret;
633
634         if (specification && !version && !query &&
635                         is_service_wfd(specification, specification_length)) {
636                 ret = peer_unregister_wfd_service();
637                 if (ret != 0 && ret != -EINPROGRESS)
638                         return ret;
639                 wfd = true;
640         }
641
642         for (list = iface_list; list; list = list->next) {
643                 struct wifi_data *wifi = list->data;
644                 GSupplicantInterface *iface = wifi->interface;
645
646                 if (wfd)
647                         goto stop_listening;
648
649                 if (!g_supplicant_interface_has_p2p(iface))
650                         continue;
651
652                 params = fill_in_peer_service_params(specification,
653                                                 specification_length, query,
654                                                 query_length, version);
655                 if (!params) {
656                         ret = -ENOMEM;
657                         continue;
658                 }
659
660                 ret = g_supplicant_interface_p2p_del_service(iface, params);
661                 if (ret != 0 && ret != -EINPROGRESS)
662                         free_peer_service_params(params);
663 stop_listening:
664                 wifi->servicing--;
665                 if (!wifi->servicing || wifi->servicing < 0) {
666                         g_supplicant_interface_p2p_listen(iface, 0, 0);
667                         wifi->servicing = 0;
668                 }
669         }
670
671         return 0;
672 }
673
674 static struct connman_peer_driver peer_driver = {
675         .connect    = peer_connect,
676         .disconnect = peer_disconnect,
677         .register_service = peer_register_service,
678         .unregister_service = peer_unregister_service,
679 };
680
681 static void handle_tethering(struct wifi_data *wifi)
682 {
683         if (!wifi->tethering)
684                 return;
685
686         if (!wifi->bridge)
687                 return;
688
689         if (wifi->bridged)
690                 return;
691
692         DBG("index %d bridge %s", wifi->index, wifi->bridge);
693
694         if (connman_inet_add_to_bridge(wifi->index, wifi->bridge) < 0)
695                 return;
696
697         wifi->bridged = true;
698 }
699
700 static void wifi_newlink(unsigned flags, unsigned change, void *user_data)
701 {
702         struct connman_device *device = user_data;
703         struct wifi_data *wifi = connman_device_get_data(device);
704
705         if (!wifi)
706                 return;
707
708         DBG("index %d flags %d change %d", wifi->index, flags, change);
709
710         if ((wifi->flags & IFF_UP) != (flags & IFF_UP)) {
711                 if (flags & IFF_UP)
712                         DBG("interface up");
713                 else
714                         DBG("interface down");
715         }
716
717         if ((wifi->flags & IFF_LOWER_UP) != (flags & IFF_LOWER_UP)) {
718                 if (flags & IFF_LOWER_UP) {
719                         DBG("carrier on");
720
721                         handle_tethering(wifi);
722                 } else
723                         DBG("carrier off");
724         }
725
726         wifi->flags = flags;
727 }
728
729 static int wifi_probe(struct connman_device *device)
730 {
731         struct wifi_data *wifi;
732
733         DBG("device %p", device);
734
735         wifi = g_try_new0(struct wifi_data, 1);
736         if (!wifi)
737                 return -ENOMEM;
738
739         wifi->state = G_SUPPLICANT_STATE_INACTIVE;
740
741         connman_device_set_data(device, wifi);
742         wifi->device = connman_device_ref(device);
743
744         wifi->index = connman_device_get_index(device);
745         wifi->flags = 0;
746
747         wifi->watch = connman_rtnl_add_newlink_watch(wifi->index,
748                                                         wifi_newlink, device);
749         if (is_p2p_connecting())
750                 add_pending_wifi_device(wifi);
751         else
752                 iface_list = g_list_append(iface_list, wifi);
753
754         return 0;
755 }
756
757 static void remove_networks(struct connman_device *device,
758                                 struct wifi_data *wifi)
759 {
760         GSList *list;
761
762         for (list = wifi->networks; list; list = list->next) {
763                 struct connman_network *network = list->data;
764
765                 connman_device_remove_network(device, network);
766                 connman_network_unref(network);
767         }
768
769         g_slist_free(wifi->networks);
770         wifi->networks = NULL;
771 }
772
773 static void reset_autoscan(struct connman_device *device)
774 {
775         struct wifi_data *wifi = connman_device_get_data(device);
776         struct autoscan_params *autoscan;
777
778         DBG("");
779
780         if (!wifi || !wifi->autoscan)
781                 return;
782
783         autoscan = wifi->autoscan;
784
785         if (autoscan->timeout == 0 && autoscan->interval == 0)
786                 return;
787
788         g_source_remove(autoscan->timeout);
789
790         autoscan->timeout = 0;
791         autoscan->interval = 0;
792
793         connman_device_unref(device);
794 }
795
796 static void stop_autoscan(struct connman_device *device)
797 {
798         const struct wifi_data *wifi = connman_device_get_data(device);
799
800         if (!wifi || !wifi->autoscan)
801                 return;
802
803         reset_autoscan(device);
804
805         connman_device_set_scanning(device, CONNMAN_SERVICE_TYPE_WIFI, false);
806 }
807
808 static void check_p2p_technology(void)
809 {
810         bool p2p_exists = false;
811         GList *list;
812
813         for (list = iface_list; list; list = list->next) {
814                 struct wifi_data *w = list->data;
815
816                 if (w->interface &&
817                                 g_supplicant_interface_has_p2p(w->interface))
818                         p2p_exists = true;
819         }
820
821         if (!p2p_exists) {
822                 connman_technology_driver_unregister(&p2p_tech_driver);
823                 connman_peer_driver_unregister(&peer_driver);
824         }
825 }
826
827 static void wifi_remove(struct connman_device *device)
828 {
829         struct wifi_data *wifi = connman_device_get_data(device);
830
831         DBG("device %p wifi %p", device, wifi);
832
833         if (!wifi)
834                 return;
835
836         stop_autoscan(device);
837
838         if (wifi->p2p_device)
839                 p2p_iface_list = g_list_remove(p2p_iface_list, wifi);
840         else
841                 iface_list = g_list_remove(iface_list, wifi);
842
843         check_p2p_technology();
844
845         remove_pending_wifi_device(wifi);
846
847         if (wifi->p2p_find_timeout) {
848                 g_source_remove(wifi->p2p_find_timeout);
849                 connman_device_unref(wifi->device);
850         }
851
852         if (wifi->p2p_connection_timeout)
853                 g_source_remove(wifi->p2p_connection_timeout);
854
855         remove_networks(device, wifi);
856
857         connman_device_set_powered(device, false);
858         connman_device_set_data(device, NULL);
859         connman_device_unref(wifi->device);
860         connman_rtnl_remove_watch(wifi->watch);
861
862         g_supplicant_interface_set_data(wifi->interface, NULL);
863
864         g_supplicant_interface_cancel(wifi->interface);
865
866         if (wifi->scan_params)
867                 g_supplicant_free_scan_params(wifi->scan_params);
868
869         g_free(wifi->autoscan);
870         g_free(wifi->identifier);
871         g_free(wifi);
872 }
873
874 static bool is_duplicate(GSList *list, gchar *ssid, int ssid_len)
875 {
876         GSList *iter;
877
878         for (iter = list; iter; iter = g_slist_next(iter)) {
879                 struct scan_ssid *scan_ssid = iter->data;
880
881                 if (ssid_len == scan_ssid->ssid_len &&
882                                 memcmp(ssid, scan_ssid->ssid, ssid_len) == 0)
883                         return true;
884         }
885
886         return false;
887 }
888
889 static int add_scan_param(gchar *hex_ssid, char *raw_ssid, int ssid_len,
890                         int freq, GSupplicantScanParams *scan_data,
891                         int driver_max_scan_ssids, char *ssid_name)
892 {
893         unsigned int i;
894         struct scan_ssid *scan_ssid;
895
896         if ((driver_max_scan_ssids == 0 ||
897                         driver_max_scan_ssids > scan_data->num_ssids) &&
898                         (hex_ssid || raw_ssid)) {
899                 gchar *ssid;
900                 unsigned int j = 0, hex;
901
902                 if (hex_ssid) {
903                         size_t hex_ssid_len = strlen(hex_ssid);
904
905                         ssid = g_try_malloc0(hex_ssid_len / 2);
906                         if (!ssid)
907                                 return -ENOMEM;
908
909                         for (i = 0; i < hex_ssid_len; i += 2) {
910                                 sscanf(hex_ssid + i, "%02x", &hex);
911                                 ssid[j++] = hex;
912                         }
913                 } else {
914                         ssid = raw_ssid;
915                         j = ssid_len;
916                 }
917
918                 /*
919                  * If we have already added hidden AP to the list,
920                  * then do not do it again. This might happen if you have
921                  * used or are using multiple wifi cards, so in that case
922                  * you might have multiple service files for same AP.
923                  */
924                 if (is_duplicate(scan_data->ssids, ssid, j)) {
925                         if (hex_ssid)
926                                 g_free(ssid);
927                         return 0;
928                 }
929
930                 scan_ssid = g_try_new(struct scan_ssid, 1);
931                 if (!scan_ssid) {
932                         if (hex_ssid)
933                                 g_free(ssid);
934                         return -ENOMEM;
935                 }
936
937                 memcpy(scan_ssid->ssid, ssid, j);
938                 scan_ssid->ssid_len = j;
939                 scan_data->ssids = g_slist_prepend(scan_data->ssids,
940                                                                 scan_ssid);
941
942                 scan_data->num_ssids++;
943
944                 DBG("SSID %s added to scanned list of %d entries", ssid_name,
945                                                         scan_data->num_ssids);
946
947                 if (hex_ssid)
948                         g_free(ssid);
949         } else
950                 return -EINVAL;
951
952         scan_data->ssids = g_slist_reverse(scan_data->ssids);
953
954         if (!scan_data->freqs) {
955                 scan_data->freqs = g_try_malloc0(sizeof(uint16_t));
956                 if (!scan_data->freqs) {
957                         g_slist_free_full(scan_data->ssids, g_free);
958                         return -ENOMEM;
959                 }
960
961                 scan_data->num_freqs = 1;
962                 scan_data->freqs[0] = freq;
963         } else {
964                 bool duplicate = false;
965
966                 /* Don't add duplicate entries */
967                 for (i = 0; i < scan_data->num_freqs; i++) {
968                         if (scan_data->freqs[i] == freq) {
969                                 duplicate = true;
970                                 break;
971                         }
972                 }
973
974                 if (!duplicate) {
975                         scan_data->num_freqs++;
976                         scan_data->freqs = g_try_realloc(scan_data->freqs,
977                                 sizeof(uint16_t) * scan_data->num_freqs);
978                         if (!scan_data->freqs) {
979                                 g_slist_free_full(scan_data->ssids, g_free);
980                                 return -ENOMEM;
981                         }
982                         scan_data->freqs[scan_data->num_freqs - 1] = freq;
983                 }
984         }
985
986         return 1;
987 }
988
989 static int get_hidden_connections(GSupplicantScanParams *scan_data)
990 {
991         struct connman_config_entry **entries;
992         GKeyFile *keyfile;
993         gchar **services;
994         char *ssid, *name;
995         int i, freq, ret;
996         bool value;
997         int num_ssids = 0, add_param_failed = 0;
998
999         services = connman_storage_get_services();
1000         for (i = 0; services && services[i]; i++) {
1001                 if (strncmp(services[i], "wifi_", 5) != 0)
1002                         continue;
1003
1004                 keyfile = connman_storage_load_service(services[i]);
1005                 if (!keyfile)
1006                         continue;
1007
1008                 value = g_key_file_get_boolean(keyfile,
1009                                         services[i], "Hidden", NULL);
1010                 if (!value) {
1011                         g_key_file_free(keyfile);
1012                         continue;
1013                 }
1014
1015                 value = g_key_file_get_boolean(keyfile,
1016                                         services[i], "Favorite", NULL);
1017                 if (!value) {
1018                         g_key_file_free(keyfile);
1019                         continue;
1020                 }
1021
1022                 ssid = g_key_file_get_string(keyfile,
1023                                         services[i], "SSID", NULL);
1024
1025                 freq = g_key_file_get_integer(keyfile, services[i],
1026                                         "Frequency", NULL);
1027
1028                 name = g_key_file_get_string(keyfile, services[i], "Name",
1029                                                                 NULL);
1030
1031                 ret = add_scan_param(ssid, NULL, 0, freq, scan_data, 0, name);
1032                 if (ret < 0)
1033                         add_param_failed++;
1034                 else if (ret > 0)
1035                         num_ssids++;
1036
1037                 g_free(ssid);
1038                 g_free(name);
1039                 g_key_file_free(keyfile);
1040         }
1041
1042         /*
1043          * Check if there are any hidden AP that needs to be provisioned.
1044          */
1045         entries = connman_config_get_entries("wifi");
1046         for (i = 0; entries && entries[i]; i++) {
1047                 int len;
1048
1049                 if (!entries[i]->hidden)
1050                         continue;
1051
1052                 if (!entries[i]->ssid) {
1053                         ssid = entries[i]->name;
1054                         len = strlen(ssid);
1055                 } else {
1056                         ssid = entries[i]->ssid;
1057                         len = entries[i]->ssid_len;
1058                 }
1059
1060                 if (!ssid)
1061                         continue;
1062
1063                 ret = add_scan_param(NULL, ssid, len, 0, scan_data, 0, ssid);
1064                 if (ret < 0)
1065                         add_param_failed++;
1066                 else if (ret > 0)
1067                         num_ssids++;
1068         }
1069
1070         connman_config_free_entries(entries);
1071
1072         if (add_param_failed > 0)
1073                 DBG("Unable to scan %d out of %d SSIDs",
1074                                         add_param_failed, num_ssids);
1075
1076         g_strfreev(services);
1077
1078         return num_ssids;
1079 }
1080
1081 static int get_hidden_connections_params(struct wifi_data *wifi,
1082                                         GSupplicantScanParams *scan_params)
1083 {
1084         int driver_max_ssids, i;
1085         GSupplicantScanParams *orig_params;
1086
1087         /*
1088          * Scan hidden networks so that we can autoconnect to them.
1089          * We will assume 1 as a default number of ssid to scan.
1090          */
1091         driver_max_ssids = g_supplicant_interface_get_max_scan_ssids(
1092                                                         wifi->interface);
1093         if (driver_max_ssids == 0)
1094                 driver_max_ssids = 1;
1095
1096         DBG("max ssids %d", driver_max_ssids);
1097
1098         if (!wifi->scan_params) {
1099                 wifi->scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
1100                 if (!wifi->scan_params)
1101                         return 0;
1102
1103                 if (get_hidden_connections(wifi->scan_params) == 0) {
1104                         g_supplicant_free_scan_params(wifi->scan_params);
1105                         wifi->scan_params = NULL;
1106
1107                         return 0;
1108                 }
1109         }
1110
1111         orig_params = wifi->scan_params;
1112
1113         /* Let's transfer driver_max_ssids params */
1114         for (i = 0; i < driver_max_ssids; i++) {
1115                 struct scan_ssid *ssid;
1116
1117                 if (!wifi->scan_params->ssids)
1118                         break;
1119
1120                 ssid = orig_params->ssids->data;
1121                 orig_params->ssids = g_slist_remove(orig_params->ssids, ssid);
1122                 scan_params->ssids = g_slist_prepend(scan_params->ssids, ssid);
1123         }
1124
1125         if (i > 0) {
1126                 scan_params->num_ssids = i;
1127                 scan_params->ssids = g_slist_reverse(scan_params->ssids);
1128
1129                 scan_params->freqs = g_memdup(orig_params->freqs,
1130                                 sizeof(uint16_t) * orig_params->num_freqs);
1131                 if (!scan_params->freqs)
1132                         goto err;
1133
1134                 scan_params->num_freqs = orig_params->num_freqs;
1135
1136         } else
1137                 goto err;
1138
1139         orig_params->num_ssids -= scan_params->num_ssids;
1140
1141         return scan_params->num_ssids;
1142
1143 err:
1144         g_slist_free_full(scan_params->ssids, g_free);
1145         g_supplicant_free_scan_params(wifi->scan_params);
1146         wifi->scan_params = NULL;
1147
1148         return 0;
1149 }
1150
1151 static int throw_wifi_scan(struct connman_device *device,
1152                         GSupplicantInterfaceCallback callback)
1153 {
1154         struct wifi_data *wifi = connman_device_get_data(device);
1155         int ret;
1156
1157         if (!wifi)
1158                 return -ENODEV;
1159
1160         DBG("device %p %p", device, wifi->interface);
1161
1162         if (wifi->tethering)
1163                 return -EBUSY;
1164
1165         if (connman_device_get_scanning(device))
1166                 return -EALREADY;
1167
1168         connman_device_ref(device);
1169
1170         ret = g_supplicant_interface_scan(wifi->interface, NULL,
1171                                                 callback, device);
1172         if (ret == 0) {
1173                 connman_device_set_scanning(device,
1174                                 CONNMAN_SERVICE_TYPE_WIFI, true);
1175         } else
1176                 connman_device_unref(device);
1177
1178         return ret;
1179 }
1180
1181 static void hidden_free(struct hidden_params *hidden)
1182 {
1183         if (!hidden)
1184                 return;
1185
1186         if (hidden->scan_params)
1187                 g_supplicant_free_scan_params(hidden->scan_params);
1188         g_free(hidden->identity);
1189         g_free(hidden->passphrase);
1190         g_free(hidden->security);
1191         g_free(hidden);
1192 }
1193
1194 static void scan_callback(int result, GSupplicantInterface *interface,
1195                                                 void *user_data)
1196 {
1197         struct connman_device *device = user_data;
1198         struct wifi_data *wifi = connman_device_get_data(device);
1199         bool scanning;
1200
1201         DBG("result %d wifi %p", result, wifi);
1202
1203         if (wifi) {
1204                 if (wifi->hidden && !wifi->postpone_hidden) {
1205                         connman_network_clear_hidden(wifi->hidden->user_data);
1206                         hidden_free(wifi->hidden);
1207                         wifi->hidden = NULL;
1208                 }
1209
1210                 if (wifi->scan_params) {
1211                         g_supplicant_free_scan_params(wifi->scan_params);
1212                         wifi->scan_params = NULL;
1213                 }
1214         }
1215
1216         if (result < 0)
1217                 connman_device_reset_scanning(device);
1218
1219         /* User is connecting to a hidden AP, let's wait for finished event */
1220         if (wifi && wifi->hidden && wifi->postpone_hidden) {
1221                 GSupplicantScanParams *scan_params;
1222                 int ret;
1223
1224                 wifi->postpone_hidden = false;
1225                 scan_params = wifi->hidden->scan_params;
1226                 wifi->hidden->scan_params = NULL;
1227
1228                 reset_autoscan(device);
1229
1230                 ret = g_supplicant_interface_scan(wifi->interface, scan_params,
1231                                                         scan_callback, device);
1232                 if (ret == 0)
1233                         return;
1234
1235                 /* On error, let's recall scan_callback, which will cleanup */
1236                 return scan_callback(ret, interface, user_data);
1237         }
1238
1239         scanning = connman_device_get_scanning(device);
1240
1241         if (scanning) {
1242                 connman_device_set_scanning(device,
1243                                 CONNMAN_SERVICE_TYPE_WIFI, false);
1244         }
1245
1246         if (result != -ENOLINK)
1247                 start_autoscan(device);
1248
1249         /*
1250          * If we are here then we were scanning; however, if we are
1251          * also mid-flight disabling the interface, then wifi_disable
1252          * has already cleared the device scanning state and
1253          * unreferenced the device, obviating the need to do it here.
1254          */
1255
1256         if (scanning)
1257                 connman_device_unref(device);
1258 }
1259
1260 static void scan_callback_hidden(int result,
1261                         GSupplicantInterface *interface, void *user_data)
1262 {
1263         struct connman_device *device = user_data;
1264         struct wifi_data *wifi = connman_device_get_data(device);
1265         GSupplicantScanParams *scan_params;
1266         int ret;
1267
1268         DBG("result %d wifi %p", result, wifi);
1269
1270         if (!wifi)
1271                 goto out;
1272
1273         /* User is trying to connect to a hidden AP */
1274         if (wifi->hidden && wifi->postpone_hidden)
1275                 goto out;
1276
1277         scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
1278         if (!scan_params)
1279                 goto out;
1280
1281         if (get_hidden_connections_params(wifi, scan_params) > 0) {
1282                 ret = g_supplicant_interface_scan(wifi->interface,
1283                                                         scan_params,
1284                                                         scan_callback_hidden,
1285                                                         device);
1286                 if (ret == 0)
1287                         return;
1288         }
1289
1290         g_supplicant_free_scan_params(scan_params);
1291
1292 out:
1293         scan_callback(result, interface, user_data);
1294 }
1295
1296 static gboolean autoscan_timeout(gpointer data)
1297 {
1298         struct connman_device *device = data;
1299         struct wifi_data *wifi = connman_device_get_data(device);
1300         struct autoscan_params *autoscan;
1301         int interval;
1302
1303         if (!wifi)
1304                 return FALSE;
1305
1306         autoscan = wifi->autoscan;
1307
1308         if (autoscan->interval <= 0) {
1309                 interval = autoscan->base;
1310                 goto set_interval;
1311         } else
1312                 interval = autoscan->interval * autoscan->base;
1313
1314         if (autoscan->interval >= autoscan->limit)
1315                 interval = autoscan->limit;
1316
1317         throw_wifi_scan(wifi->device, scan_callback_hidden);
1318
1319 set_interval:
1320         DBG("interval %d", interval);
1321
1322         autoscan->interval = interval;
1323
1324         autoscan->timeout = g_timeout_add_seconds(interval,
1325                                                 autoscan_timeout, device);
1326
1327         return FALSE;
1328 }
1329
1330 static void start_autoscan(struct connman_device *device)
1331 {
1332         struct wifi_data *wifi = connman_device_get_data(device);
1333         struct autoscan_params *autoscan;
1334
1335         DBG("");
1336
1337         if (!wifi)
1338                 return;
1339
1340         if (wifi->p2p_device)
1341                 return;
1342
1343         autoscan = wifi->autoscan;
1344         if (!autoscan)
1345                 return;
1346
1347         if (autoscan->timeout > 0 || autoscan->interval > 0)
1348                 return;
1349
1350         connman_device_ref(device);
1351
1352         autoscan_timeout(device);
1353 }
1354
1355 static struct autoscan_params *parse_autoscan_params(const char *params)
1356 {
1357         struct autoscan_params *autoscan;
1358         char **list_params;
1359         int limit;
1360         int base;
1361
1362         DBG("Emulating autoscan");
1363
1364         list_params = g_strsplit(params, ":", 0);
1365         if (list_params == 0)
1366                 return NULL;
1367
1368         if (g_strv_length(list_params) < 3) {
1369                 g_strfreev(list_params);
1370                 return NULL;
1371         }
1372
1373         base = atoi(list_params[1]);
1374         limit = atoi(list_params[2]);
1375
1376         g_strfreev(list_params);
1377
1378         autoscan = g_try_malloc0(sizeof(struct autoscan_params));
1379         if (!autoscan) {
1380                 DBG("Could not allocate memory for autoscan");
1381                 return NULL;
1382         }
1383
1384         DBG("base %d - limit %d", base, limit);
1385         autoscan->base = base;
1386         autoscan->limit = limit;
1387
1388         return autoscan;
1389 }
1390
1391 static void setup_autoscan(struct wifi_data *wifi)
1392 {
1393         if (!wifi->autoscan)
1394                 wifi->autoscan = parse_autoscan_params(AUTOSCAN_DEFAULT);
1395
1396         start_autoscan(wifi->device);
1397 }
1398
1399 static void interface_autoscan_callback(int result,
1400                                         GSupplicantInterface *interface,
1401                                                         void *user_data)
1402 {
1403         struct wifi_data *wifi = user_data;
1404
1405         if (result < 0) {
1406                 DBG("Could not enable Autoscan, falling back...");
1407                 setup_autoscan(wifi);
1408         }
1409 }
1410
1411 static void finalize_interface_creation(struct wifi_data *wifi)
1412 {
1413         GSupplicantInterface *interface = wifi->interface;
1414
1415         DBG("interface is ready wifi %p tethering %d", wifi, wifi->tethering);
1416
1417         if (!wifi->device) {
1418                 connman_error("WiFi device not set");
1419                 return;
1420         }
1421
1422         connman_device_set_powered(wifi->device, true);
1423
1424         if (!connman_setting_get_bool("BackgroundScanning"))
1425                 return;
1426
1427         if (wifi->p2p_device)
1428                 return;
1429
1430         /* Setting up automatic scanning */
1431         if (g_supplicant_interface_autoscan(interface, AUTOSCAN_DEFAULT,
1432                                 interface_autoscan_callback, wifi) < 0) {
1433                 DBG("Could not enable Autoscan, falling back...");
1434                 setup_autoscan(wifi);
1435         }
1436 }
1437
1438 static void interface_create_callback(int result,
1439                                         GSupplicantInterface *interface,
1440                                                         void *user_data)
1441 {
1442         struct wifi_data *wifi = user_data;
1443
1444         DBG("result %d ifname %s, wifi %p", result,
1445                                 g_supplicant_interface_get_ifname(interface),
1446                                 wifi);
1447
1448         if (result < 0 || !wifi)
1449                 return;
1450
1451         wifi->interface = interface;
1452         g_supplicant_interface_set_data(interface, wifi);
1453
1454         if (g_supplicant_interface_get_ready(interface)) {
1455                 wifi->interface_ready = true;
1456                 finalize_interface_creation(wifi);
1457         }
1458 }
1459
1460 static int wifi_enable(struct connman_device *device)
1461 {
1462         struct wifi_data *wifi = connman_device_get_data(device);
1463         int index;
1464         char *interface;
1465         const char *driver = connman_option_get_string("wifi");
1466         int ret;
1467
1468         DBG("device %p %p", device, wifi);
1469
1470         index = connman_device_get_index(device);
1471         if (!wifi || index < 0)
1472                 return -ENODEV;
1473
1474         if (is_p2p_connecting())
1475                 return -EINPROGRESS;
1476
1477         interface = connman_inet_ifname(index);
1478         ret = g_supplicant_interface_create(interface, driver, NULL,
1479                                                 interface_create_callback,
1480                                                         wifi);
1481         g_free(interface);
1482
1483         if (ret < 0)
1484                 return ret;
1485
1486         return -EINPROGRESS;
1487 }
1488
1489 static int wifi_disable(struct connman_device *device)
1490 {
1491         struct wifi_data *wifi = connman_device_get_data(device);
1492         int ret;
1493
1494         DBG("device %p wifi %p", device, wifi);
1495
1496         if (!wifi)
1497                 return -ENODEV;
1498
1499         wifi->connected = false;
1500         wifi->disconnecting = false;
1501
1502         if (wifi->pending_network)
1503                 wifi->pending_network = NULL;
1504
1505         stop_autoscan(device);
1506
1507         if (wifi->p2p_find_timeout) {
1508                 g_source_remove(wifi->p2p_find_timeout);
1509                 wifi->p2p_find_timeout = 0;
1510                 connman_device_set_scanning(device, CONNMAN_SERVICE_TYPE_P2P, false);
1511                 connman_device_unref(wifi->device);
1512         }
1513
1514         /* In case of a user scan, device is still referenced */
1515         if (connman_device_get_scanning(device)) {
1516                 connman_device_set_scanning(device,
1517                                 CONNMAN_SERVICE_TYPE_WIFI, false);
1518                 connman_device_unref(wifi->device);
1519         }
1520
1521         remove_networks(device, wifi);
1522
1523         ret = g_supplicant_interface_remove(wifi->interface, NULL, NULL);
1524         if (ret < 0)
1525                 return ret;
1526
1527         return -EINPROGRESS;
1528 }
1529
1530 struct last_connected {
1531         GTimeVal modified;
1532         gchar *ssid;
1533         int freq;
1534 };
1535
1536 static gint sort_entry(gconstpointer a, gconstpointer b, gpointer user_data)
1537 {
1538         GTimeVal *aval = (GTimeVal *)a;
1539         GTimeVal *bval = (GTimeVal *)b;
1540
1541         /* Note that the sort order is descending */
1542         if (aval->tv_sec < bval->tv_sec)
1543                 return 1;
1544
1545         if (aval->tv_sec > bval->tv_sec)
1546                 return -1;
1547
1548         return 0;
1549 }
1550
1551 static void free_entry(gpointer data)
1552 {
1553         struct last_connected *entry = data;
1554
1555         g_free(entry->ssid);
1556         g_free(entry);
1557 }
1558
1559 static int get_latest_connections(int max_ssids,
1560                                 GSupplicantScanParams *scan_data)
1561 {
1562         GSequenceIter *iter;
1563         GSequence *latest_list;
1564         struct last_connected *entry;
1565         GKeyFile *keyfile;
1566         GTimeVal modified;
1567         gchar **services;
1568         gchar *str;
1569         char *ssid;
1570         int i, freq;
1571         int num_ssids = 0;
1572
1573         latest_list = g_sequence_new(free_entry);
1574         if (!latest_list)
1575                 return -ENOMEM;
1576
1577         services = connman_storage_get_services();
1578         for (i = 0; services && services[i]; i++) {
1579                 if (strncmp(services[i], "wifi_", 5) != 0)
1580                         continue;
1581
1582                 keyfile = connman_storage_load_service(services[i]);
1583                 if (!keyfile)
1584                         continue;
1585
1586                 str = g_key_file_get_string(keyfile,
1587                                         services[i], "Favorite", NULL);
1588                 if (!str || g_strcmp0(str, "true")) {
1589                         g_free(str);
1590                         g_key_file_free(keyfile);
1591                         continue;
1592                 }
1593                 g_free(str);
1594
1595                 str = g_key_file_get_string(keyfile,
1596                                         services[i], "AutoConnect", NULL);
1597                 if (!str || g_strcmp0(str, "true")) {
1598                         g_free(str);
1599                         g_key_file_free(keyfile);
1600                         continue;
1601                 }
1602                 g_free(str);
1603
1604                 str = g_key_file_get_string(keyfile,
1605                                         services[i], "Modified", NULL);
1606                 if (!str) {
1607                         g_key_file_free(keyfile);
1608                         continue;
1609                 }
1610                 g_time_val_from_iso8601(str, &modified);
1611                 g_free(str);
1612
1613                 ssid = g_key_file_get_string(keyfile,
1614                                         services[i], "SSID", NULL);
1615
1616                 freq = g_key_file_get_integer(keyfile, services[i],
1617                                         "Frequency", NULL);
1618                 if (freq) {
1619                         entry = g_try_new(struct last_connected, 1);
1620                         if (!entry) {
1621                                 g_sequence_free(latest_list);
1622                                 g_key_file_free(keyfile);
1623                                 g_free(ssid);
1624                                 return -ENOMEM;
1625                         }
1626
1627                         entry->ssid = ssid;
1628                         entry->modified = modified;
1629                         entry->freq = freq;
1630
1631                         g_sequence_insert_sorted(latest_list, entry,
1632                                                 sort_entry, NULL);
1633                         num_ssids++;
1634                 } else
1635                         g_free(ssid);
1636
1637                 g_key_file_free(keyfile);
1638         }
1639
1640         g_strfreev(services);
1641
1642         num_ssids = num_ssids > max_ssids ? max_ssids : num_ssids;
1643
1644         iter = g_sequence_get_begin_iter(latest_list);
1645
1646         for (i = 0; i < num_ssids; i++) {
1647                 entry = g_sequence_get(iter);
1648
1649                 DBG("ssid %s freq %d modified %lu", entry->ssid, entry->freq,
1650                                                 entry->modified.tv_sec);
1651
1652                 add_scan_param(entry->ssid, NULL, 0, entry->freq, scan_data,
1653                                                 max_ssids, entry->ssid);
1654
1655                 iter = g_sequence_iter_next(iter);
1656         }
1657
1658         g_sequence_free(latest_list);
1659         return num_ssids;
1660 }
1661
1662 static int wifi_scan_simple(struct connman_device *device)
1663 {
1664         reset_autoscan(device);
1665
1666         return throw_wifi_scan(device, scan_callback_hidden);
1667 }
1668
1669 static gboolean p2p_find_stop(gpointer data)
1670 {
1671         struct connman_device *device = data;
1672         struct wifi_data *wifi = connman_device_get_data(device);
1673
1674         DBG("");
1675
1676         wifi->p2p_find_timeout = 0;
1677
1678         connman_device_set_scanning(device, CONNMAN_SERVICE_TYPE_P2P, false);
1679
1680         g_supplicant_interface_p2p_stop_find(wifi->interface);
1681
1682         connman_device_unref(device);
1683         reset_autoscan(device);
1684
1685         return FALSE;
1686 }
1687
1688 static void p2p_find_callback(int result, GSupplicantInterface *interface,
1689                                                         void *user_data)
1690 {
1691         struct connman_device *device = user_data;
1692         struct wifi_data *wifi = connman_device_get_data(device);
1693
1694         DBG("result %d wifi %p", result, wifi);
1695
1696         if (wifi->p2p_find_timeout) {
1697                 g_source_remove(wifi->p2p_find_timeout);
1698                 wifi->p2p_find_timeout = 0;
1699         }
1700
1701         if (result)
1702                 goto error;
1703
1704         wifi->p2p_find_timeout = g_timeout_add_seconds(P2P_FIND_TIMEOUT,
1705                                                         p2p_find_stop, device);
1706         if (!wifi->p2p_find_timeout)
1707                 goto error;
1708
1709         return;
1710 error:
1711         p2p_find_stop(device);
1712 }
1713
1714 static int p2p_find(struct connman_device *device)
1715 {
1716         struct wifi_data *wifi;
1717         int ret;
1718
1719         DBG("");
1720
1721         if (!p2p_technology)
1722                 return -ENOTSUP;
1723
1724         wifi = connman_device_get_data(device);
1725
1726         if (g_supplicant_interface_is_p2p_finding(wifi->interface))
1727                 return -EALREADY;
1728
1729         reset_autoscan(device);
1730         connman_device_ref(device);
1731
1732         ret = g_supplicant_interface_p2p_find(wifi->interface,
1733                                                 p2p_find_callback, device);
1734         if (ret) {
1735                 connman_device_unref(device);
1736                 start_autoscan(device);
1737         } else {
1738                 connman_device_set_scanning(device,
1739                                 CONNMAN_SERVICE_TYPE_P2P, true);
1740         }
1741
1742         return ret;
1743 }
1744
1745 /*
1746  * Note that the hidden scan is only used when connecting to this specific
1747  * hidden AP first time. It is not used when system autoconnects to hidden AP.
1748  */
1749 static int wifi_scan(enum connman_service_type type,
1750                         struct connman_device *device,
1751                         const char *ssid, unsigned int ssid_len,
1752                         const char *identity, const char* passphrase,
1753                         const char *security, void *user_data)
1754 {
1755         struct wifi_data *wifi = connman_device_get_data(device);
1756         GSupplicantScanParams *scan_params = NULL;
1757         struct scan_ssid *scan_ssid;
1758         struct hidden_params *hidden;
1759         int ret;
1760         int driver_max_ssids = 0;
1761         bool do_hidden;
1762         bool scanning;
1763
1764         if (!wifi)
1765                 return -ENODEV;
1766
1767         if (wifi->p2p_device)
1768                 return 0;
1769
1770         if (type == CONNMAN_SERVICE_TYPE_P2P)
1771                 return p2p_find(device);
1772
1773         DBG("device %p wifi %p hidden ssid %s", device, wifi->interface, ssid);
1774
1775         if (wifi->tethering)
1776                 return 0;
1777
1778         scanning = connman_device_get_scanning(device);
1779
1780         if (!ssid || ssid_len == 0 || ssid_len > 32) {
1781                 if (scanning)
1782                         return -EALREADY;
1783
1784                 driver_max_ssids = g_supplicant_interface_get_max_scan_ssids(
1785                                                         wifi->interface);
1786                 DBG("max ssids %d", driver_max_ssids);
1787                 if (driver_max_ssids == 0)
1788                         return wifi_scan_simple(device);
1789
1790                 do_hidden = false;
1791         } else {
1792                 if (scanning && wifi->hidden && wifi->postpone_hidden)
1793                         return -EALREADY;
1794
1795                 do_hidden = true;
1796         }
1797
1798         scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
1799         if (!scan_params)
1800                 return -ENOMEM;
1801
1802         if (do_hidden) {
1803                 scan_ssid = g_try_new(struct scan_ssid, 1);
1804                 if (!scan_ssid) {
1805                         g_free(scan_params);
1806                         return -ENOMEM;
1807                 }
1808
1809                 memcpy(scan_ssid->ssid, ssid, ssid_len);
1810                 scan_ssid->ssid_len = ssid_len;
1811                 scan_params->ssids = g_slist_prepend(scan_params->ssids,
1812                                                                 scan_ssid);
1813                 scan_params->num_ssids = 1;
1814
1815                 hidden = g_try_new0(struct hidden_params, 1);
1816                 if (!hidden) {
1817                         g_supplicant_free_scan_params(scan_params);
1818                         return -ENOMEM;
1819                 }
1820
1821                 if (wifi->hidden) {
1822                         hidden_free(wifi->hidden);
1823                         wifi->hidden = NULL;
1824                 }
1825
1826                 memcpy(hidden->ssid, ssid, ssid_len);
1827                 hidden->ssid_len = ssid_len;
1828                 hidden->identity = g_strdup(identity);
1829                 hidden->passphrase = g_strdup(passphrase);
1830                 hidden->security = g_strdup(security);
1831                 hidden->user_data = user_data;
1832                 wifi->hidden = hidden;
1833
1834                 if (scanning) {
1835                         /* Let's keep this active scan for later,
1836                          * when current scan will be over. */
1837                         wifi->postpone_hidden = TRUE;
1838                         hidden->scan_params = scan_params;
1839
1840                         return 0;
1841                 }
1842         } else {
1843                 ret = get_latest_connections(driver_max_ssids, scan_params);
1844                 if (ret <= 0) {
1845                         g_supplicant_free_scan_params(scan_params);
1846                         return wifi_scan_simple(device);
1847                 }
1848         }
1849
1850         connman_device_ref(device);
1851
1852         reset_autoscan(device);
1853
1854         ret = g_supplicant_interface_scan(wifi->interface, scan_params,
1855                                                 scan_callback, device);
1856         if (ret == 0) {
1857                 connman_device_set_scanning(device,
1858                                 CONNMAN_SERVICE_TYPE_WIFI, true);
1859         } else {
1860                 g_supplicant_free_scan_params(scan_params);
1861                 connman_device_unref(device);
1862
1863                 if (do_hidden) {
1864                         hidden_free(wifi->hidden);
1865                         wifi->hidden = NULL;
1866                 }
1867         }
1868
1869         return ret;
1870 }
1871
1872 static void wifi_regdom_callback(int result,
1873                                         const char *alpha2,
1874                                                 void *user_data)
1875 {
1876         struct connman_device *device = user_data;
1877
1878         connman_device_regdom_notify(device, result, alpha2);
1879
1880         connman_device_unref(device);
1881 }
1882
1883 static int wifi_set_regdom(struct connman_device *device, const char *alpha2)
1884 {
1885         struct wifi_data *wifi = connman_device_get_data(device);
1886         int ret;
1887
1888         if (!wifi)
1889                 return -EINVAL;
1890
1891         connman_device_ref(device);
1892
1893         ret = g_supplicant_interface_set_country(wifi->interface,
1894                                                 wifi_regdom_callback,
1895                                                         alpha2, device);
1896         if (ret != 0)
1897                 connman_device_unref(device);
1898
1899         return ret;
1900 }
1901
1902 static struct connman_device_driver wifi_ng_driver = {
1903         .name           = "wifi",
1904         .type           = CONNMAN_DEVICE_TYPE_WIFI,
1905         .priority       = CONNMAN_DEVICE_PRIORITY_LOW,
1906         .probe          = wifi_probe,
1907         .remove         = wifi_remove,
1908         .enable         = wifi_enable,
1909         .disable        = wifi_disable,
1910         .scan           = wifi_scan,
1911         .set_regdom     = wifi_set_regdom,
1912 };
1913
1914 static void system_ready(void)
1915 {
1916         DBG("");
1917
1918         if (connman_device_driver_register(&wifi_ng_driver) < 0)
1919                 connman_error("Failed to register WiFi driver");
1920 }
1921
1922 static void system_killed(void)
1923 {
1924         DBG("");
1925
1926         connman_device_driver_unregister(&wifi_ng_driver);
1927 }
1928
1929 static int network_probe(struct connman_network *network)
1930 {
1931         DBG("network %p", network);
1932
1933         return 0;
1934 }
1935
1936 static void network_remove(struct connman_network *network)
1937 {
1938         struct connman_device *device = connman_network_get_device(network);
1939         struct wifi_data *wifi;
1940
1941         DBG("network %p", network);
1942
1943         wifi = connman_device_get_data(device);
1944         if (!wifi)
1945                 return;
1946
1947         if (wifi->network != network)
1948                 return;
1949
1950         wifi->network = NULL;
1951 }
1952
1953 static void connect_callback(int result, GSupplicantInterface *interface,
1954                                                         void *user_data)
1955 {
1956         struct connman_network *network = user_data;
1957
1958         DBG("network %p result %d", network, result);
1959
1960         if (result == -ENOKEY) {
1961                 connman_network_set_error(network,
1962                                         CONNMAN_NETWORK_ERROR_INVALID_KEY);
1963         } else if (result < 0) {
1964                 connman_network_set_error(network,
1965                                         CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
1966         }
1967
1968         connman_network_unref(network);
1969 }
1970
1971 static GSupplicantSecurity network_security(const char *security)
1972 {
1973         if (g_str_equal(security, "none"))
1974                 return G_SUPPLICANT_SECURITY_NONE;
1975         else if (g_str_equal(security, "wep"))
1976                 return G_SUPPLICANT_SECURITY_WEP;
1977         else if (g_str_equal(security, "psk"))
1978                 return G_SUPPLICANT_SECURITY_PSK;
1979         else if (g_str_equal(security, "wpa"))
1980                 return G_SUPPLICANT_SECURITY_PSK;
1981         else if (g_str_equal(security, "rsn"))
1982                 return G_SUPPLICANT_SECURITY_PSK;
1983         else if (g_str_equal(security, "ieee8021x"))
1984                 return G_SUPPLICANT_SECURITY_IEEE8021X;
1985
1986         return G_SUPPLICANT_SECURITY_UNKNOWN;
1987 }
1988
1989 static void ssid_init(GSupplicantSSID *ssid, struct connman_network *network)
1990 {
1991         const char *security;
1992
1993         memset(ssid, 0, sizeof(*ssid));
1994         ssid->mode = G_SUPPLICANT_MODE_INFRA;
1995         ssid->ssid = connman_network_get_blob(network, "WiFi.SSID",
1996                                                 &ssid->ssid_len);
1997         ssid->scan_ssid = 1;
1998         security = connman_network_get_string(network, "WiFi.Security");
1999         ssid->security = network_security(security);
2000         ssid->passphrase = connman_network_get_string(network,
2001                                                 "WiFi.Passphrase");
2002
2003         ssid->eap = connman_network_get_string(network, "WiFi.EAP");
2004
2005         /*
2006          * If our private key password is unset,
2007          * we use the supplied passphrase. That is needed
2008          * for PEAP where 2 passphrases (identity and client
2009          * cert may have to be provided.
2010          */
2011         if (!connman_network_get_string(network, "WiFi.PrivateKeyPassphrase"))
2012                 connman_network_set_string(network,
2013                                                 "WiFi.PrivateKeyPassphrase",
2014                                                 ssid->passphrase);
2015         /* We must have an identity for both PEAP and TLS */
2016         ssid->identity = connman_network_get_string(network, "WiFi.Identity");
2017
2018         /* Use agent provided identity as a fallback */
2019         if (!ssid->identity || strlen(ssid->identity) == 0)
2020                 ssid->identity = connman_network_get_string(network,
2021                                                         "WiFi.AgentIdentity");
2022
2023         ssid->ca_cert_path = connman_network_get_string(network,
2024                                                         "WiFi.CACertFile");
2025         ssid->client_cert_path = connman_network_get_string(network,
2026                                                         "WiFi.ClientCertFile");
2027         ssid->private_key_path = connman_network_get_string(network,
2028                                                         "WiFi.PrivateKeyFile");
2029         ssid->private_key_passphrase = connman_network_get_string(network,
2030                                                 "WiFi.PrivateKeyPassphrase");
2031         ssid->phase2_auth = connman_network_get_string(network, "WiFi.Phase2");
2032
2033         ssid->use_wps = connman_network_get_bool(network, "WiFi.UseWPS");
2034         ssid->pin_wps = connman_network_get_string(network, "WiFi.PinWPS");
2035
2036         if (connman_setting_get_bool("BackgroundScanning"))
2037                 ssid->bgscan = BGSCAN_DEFAULT;
2038 }
2039
2040 static int network_connect(struct connman_network *network)
2041 {
2042         struct connman_device *device = connman_network_get_device(network);
2043         struct wifi_data *wifi;
2044         GSupplicantInterface *interface;
2045         GSupplicantSSID *ssid;
2046
2047         DBG("network %p", network);
2048
2049         if (!device)
2050                 return -ENODEV;
2051
2052         wifi = connman_device_get_data(device);
2053         if (!wifi)
2054                 return -ENODEV;
2055
2056         ssid = g_try_malloc0(sizeof(GSupplicantSSID));
2057         if (!ssid)
2058                 return -ENOMEM;
2059
2060         interface = wifi->interface;
2061
2062         ssid_init(ssid, network);
2063
2064         if (wifi->disconnecting) {
2065                 wifi->pending_network = network;
2066                 g_free(ssid);
2067         } else {
2068                 wifi->network = connman_network_ref(network);
2069                 wifi->retries = 0;
2070
2071                 return g_supplicant_interface_connect(interface, ssid,
2072                                                 connect_callback, network);
2073         }
2074
2075         return -EINPROGRESS;
2076 }
2077
2078 static void disconnect_callback(int result, GSupplicantInterface *interface,
2079                                                                 void *user_data)
2080 {
2081         struct wifi_data *wifi = user_data;
2082
2083         DBG("result %d supplicant interface %p wifi %p",
2084                         result, interface, wifi);
2085
2086         if (result == -ECONNABORTED) {
2087                 DBG("wifi interface no longer available");
2088                 return;
2089         }
2090
2091         if (wifi->network) {
2092                 /*
2093                  * if result < 0 supplican return an error because
2094                  * the network is not current.
2095                  * we wont receive G_SUPPLICANT_STATE_DISCONNECTED since it
2096                  * failed, call connman_network_set_connected to report
2097                  * disconnect is completed.
2098                  */
2099                 if (result < 0)
2100                         connman_network_set_connected(wifi->network, false);
2101         }
2102
2103         wifi->network = NULL;
2104
2105         wifi->disconnecting = false;
2106
2107         if (wifi->pending_network) {
2108                 network_connect(wifi->pending_network);
2109                 wifi->pending_network = NULL;
2110         }
2111
2112         start_autoscan(wifi->device);
2113 }
2114
2115 static int network_disconnect(struct connman_network *network)
2116 {
2117         struct connman_device *device = connman_network_get_device(network);
2118         struct wifi_data *wifi;
2119         int err;
2120
2121         DBG("network %p", network);
2122
2123         wifi = connman_device_get_data(device);
2124         if (!wifi || !wifi->interface)
2125                 return -ENODEV;
2126
2127         connman_network_set_associating(network, false);
2128
2129         if (wifi->disconnecting)
2130                 return -EALREADY;
2131
2132         wifi->disconnecting = true;
2133
2134         err = g_supplicant_interface_disconnect(wifi->interface,
2135                                                 disconnect_callback, wifi);
2136         if (err < 0)
2137                 wifi->disconnecting = false;
2138
2139         return err;
2140 }
2141
2142 static struct connman_network_driver network_driver = {
2143         .name           = "wifi",
2144         .type           = CONNMAN_NETWORK_TYPE_WIFI,
2145         .priority       = CONNMAN_NETWORK_PRIORITY_LOW,
2146         .probe          = network_probe,
2147         .remove         = network_remove,
2148         .connect        = network_connect,
2149         .disconnect     = network_disconnect,
2150 };
2151
2152 static void interface_added(GSupplicantInterface *interface)
2153 {
2154         const char *ifname = g_supplicant_interface_get_ifname(interface);
2155         const char *driver = g_supplicant_interface_get_driver(interface);
2156         struct wifi_data *wifi;
2157
2158         wifi = g_supplicant_interface_get_data(interface);
2159         if (!wifi) {
2160                 wifi = get_pending_wifi_data(ifname);
2161                 if (!wifi)
2162                         return;
2163
2164                 g_supplicant_interface_set_data(interface, wifi);
2165                 p2p_iface_list = g_list_append(p2p_iface_list, wifi);
2166                 wifi->p2p_device = true;
2167         }
2168
2169         DBG("ifname %s driver %s wifi %p tethering %d",
2170                         ifname, driver, wifi, wifi->tethering);
2171
2172         if (!wifi->device) {
2173                 connman_error("WiFi device not set");
2174                 return;
2175         }
2176
2177         connman_device_set_powered(wifi->device, true);
2178 }
2179
2180 static bool is_idle(struct wifi_data *wifi)
2181 {
2182         DBG("state %d", wifi->state);
2183
2184         switch (wifi->state) {
2185         case G_SUPPLICANT_STATE_UNKNOWN:
2186         case G_SUPPLICANT_STATE_DISABLED:
2187         case G_SUPPLICANT_STATE_DISCONNECTED:
2188         case G_SUPPLICANT_STATE_INACTIVE:
2189         case G_SUPPLICANT_STATE_SCANNING:
2190                 return true;
2191
2192         case G_SUPPLICANT_STATE_AUTHENTICATING:
2193         case G_SUPPLICANT_STATE_ASSOCIATING:
2194         case G_SUPPLICANT_STATE_ASSOCIATED:
2195         case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
2196         case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
2197         case G_SUPPLICANT_STATE_COMPLETED:
2198                 return false;
2199         }
2200
2201         return false;
2202 }
2203
2204 static bool is_idle_wps(GSupplicantInterface *interface,
2205                                                 struct wifi_data *wifi)
2206 {
2207         /* First, let's check if WPS processing did not went wrong */
2208         if (g_supplicant_interface_get_wps_state(interface) ==
2209                 G_SUPPLICANT_WPS_STATE_FAIL)
2210                 return false;
2211
2212         /* Unlike normal connection, being associated while processing wps
2213          * actually means that we are idling. */
2214         switch (wifi->state) {
2215         case G_SUPPLICANT_STATE_UNKNOWN:
2216         case G_SUPPLICANT_STATE_DISABLED:
2217         case G_SUPPLICANT_STATE_DISCONNECTED:
2218         case G_SUPPLICANT_STATE_INACTIVE:
2219         case G_SUPPLICANT_STATE_SCANNING:
2220         case G_SUPPLICANT_STATE_ASSOCIATED:
2221                 return true;
2222         case G_SUPPLICANT_STATE_AUTHENTICATING:
2223         case G_SUPPLICANT_STATE_ASSOCIATING:
2224         case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
2225         case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
2226         case G_SUPPLICANT_STATE_COMPLETED:
2227                 return false;
2228         }
2229
2230         return false;
2231 }
2232
2233 static bool handle_wps_completion(GSupplicantInterface *interface,
2234                                         struct connman_network *network,
2235                                         struct connman_device *device,
2236                                         struct wifi_data *wifi)
2237 {
2238         bool wps;
2239
2240         wps = connman_network_get_bool(network, "WiFi.UseWPS");
2241         if (wps) {
2242                 const unsigned char *ssid, *wps_ssid;
2243                 unsigned int ssid_len, wps_ssid_len;
2244                 const char *wps_key;
2245
2246                 /* Checking if we got associated with requested
2247                  * network */
2248                 ssid = connman_network_get_blob(network, "WiFi.SSID",
2249                                                 &ssid_len);
2250
2251                 wps_ssid = g_supplicant_interface_get_wps_ssid(
2252                         interface, &wps_ssid_len);
2253
2254                 if (!wps_ssid || wps_ssid_len != ssid_len ||
2255                                 memcmp(ssid, wps_ssid, ssid_len) != 0) {
2256                         connman_network_set_associating(network, false);
2257                         g_supplicant_interface_disconnect(wifi->interface,
2258                                                 disconnect_callback, wifi);
2259                         return false;
2260                 }
2261
2262                 wps_key = g_supplicant_interface_get_wps_key(interface);
2263                 connman_network_set_string(network, "WiFi.Passphrase",
2264                                         wps_key);
2265
2266                 connman_network_set_string(network, "WiFi.PinWPS", NULL);
2267         }
2268
2269         return true;
2270 }
2271
2272 static bool handle_4way_handshake_failure(GSupplicantInterface *interface,
2273                                         struct connman_network *network,
2274                                         struct wifi_data *wifi)
2275 {
2276         struct connman_service *service;
2277
2278         if (wifi->state != G_SUPPLICANT_STATE_4WAY_HANDSHAKE)
2279                 return false;
2280
2281         service = connman_service_lookup_from_network(network);
2282         if (!service)
2283                 return false;
2284
2285         wifi->retries++;
2286
2287         if (connman_service_get_favorite(service)) {
2288                 if (wifi->retries < FAVORITE_MAXIMUM_RETRIES)
2289                         return true;
2290         }
2291
2292         wifi->retries = 0;
2293         connman_network_set_error(network, CONNMAN_NETWORK_ERROR_INVALID_KEY);
2294
2295         return false;
2296 }
2297
2298 static void interface_state(GSupplicantInterface *interface)
2299 {
2300         struct connman_network *network;
2301         struct connman_device *device;
2302         struct wifi_data *wifi;
2303         GSupplicantState state = g_supplicant_interface_get_state(interface);
2304         bool wps;
2305
2306         wifi = g_supplicant_interface_get_data(interface);
2307
2308         DBG("wifi %p interface state %d", wifi, state);
2309
2310         if (!wifi)
2311                 return;
2312
2313         device = wifi->device;
2314         if (!device)
2315                 return;
2316
2317         if (g_supplicant_interface_get_ready(interface) &&
2318                                         !wifi->interface_ready) {
2319                 wifi->interface_ready = true;
2320                 finalize_interface_creation(wifi);
2321         }
2322
2323         network = wifi->network;
2324         if (!network)
2325                 return;
2326
2327         switch (state) {
2328         case G_SUPPLICANT_STATE_SCANNING:
2329                 break;
2330
2331         case G_SUPPLICANT_STATE_AUTHENTICATING:
2332         case G_SUPPLICANT_STATE_ASSOCIATING:
2333                 stop_autoscan(device);
2334
2335                 if (!wifi->connected)
2336                         connman_network_set_associating(network, true);
2337
2338                 break;
2339
2340         case G_SUPPLICANT_STATE_COMPLETED:
2341                 /* though it should be already stopped: */
2342                 stop_autoscan(device);
2343
2344                 if (!handle_wps_completion(interface, network, device, wifi))
2345                         break;
2346
2347                 connman_network_set_connected(network, true);
2348                 break;
2349
2350         case G_SUPPLICANT_STATE_DISCONNECTED:
2351                 /*
2352                  * If we're in one of the idle modes, we have
2353                  * not started association yet and thus setting
2354                  * those ones to FALSE could cancel an association
2355                  * in progress.
2356                  */
2357                 wps = connman_network_get_bool(network, "WiFi.UseWPS");
2358                 if (wps)
2359                         if (is_idle_wps(interface, wifi))
2360                                 break;
2361
2362                 if (is_idle(wifi))
2363                         break;
2364
2365                 /* If previous state was 4way-handshake, then
2366                  * it's either: psk was incorrect and thus we retry
2367                  * or if we reach the maximum retries we declare the
2368                  * psk as wrong */
2369                 if (handle_4way_handshake_failure(interface,
2370                                                 network, wifi))
2371                         break;
2372
2373                 /* We disable the selected network, if not then
2374                  * wpa_supplicant will loop retrying */
2375                 if (g_supplicant_interface_enable_selected_network(interface,
2376                                                 FALSE) != 0)
2377                         DBG("Could not disables selected network");
2378
2379                 connman_network_set_connected(network, false);
2380                 connman_network_set_associating(network, false);
2381                 wifi->disconnecting = false;
2382
2383                 start_autoscan(device);
2384
2385                 break;
2386
2387         case G_SUPPLICANT_STATE_INACTIVE:
2388                 connman_network_set_associating(network, false);
2389                 start_autoscan(device);
2390
2391                 break;
2392
2393         case G_SUPPLICANT_STATE_UNKNOWN:
2394         case G_SUPPLICANT_STATE_DISABLED:
2395         case G_SUPPLICANT_STATE_ASSOCIATED:
2396         case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
2397         case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
2398                 break;
2399         }
2400
2401         wifi->state = state;
2402
2403         /* Saving wpa_s state policy:
2404          * If connected and if the state changes are roaming related:
2405          * --> We stay connected
2406          * If completed
2407          * --> We are connected
2408          * All other case:
2409          * --> We are not connected
2410          * */
2411         switch (state) {
2412         case G_SUPPLICANT_STATE_AUTHENTICATING:
2413         case G_SUPPLICANT_STATE_ASSOCIATING:
2414         case G_SUPPLICANT_STATE_ASSOCIATED:
2415         case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
2416         case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
2417                 if (wifi->connected)
2418                         connman_warn("Probably roaming right now!"
2419                                                 " Staying connected...");
2420                 else
2421                         wifi->connected = false;
2422                 break;
2423         case G_SUPPLICANT_STATE_COMPLETED:
2424                 wifi->connected = true;
2425                 break;
2426         default:
2427                 wifi->connected = false;
2428                 break;
2429         }
2430
2431         DBG("DONE");
2432 }
2433
2434 static void interface_removed(GSupplicantInterface *interface)
2435 {
2436         const char *ifname = g_supplicant_interface_get_ifname(interface);
2437         struct wifi_data *wifi;
2438
2439         DBG("ifname %s", ifname);
2440
2441         wifi = g_supplicant_interface_get_data(interface);
2442
2443         if (wifi)
2444                 wifi->interface = NULL;
2445
2446         if (wifi && wifi->tethering)
2447                 return;
2448
2449         if (!wifi || !wifi->device) {
2450                 DBG("wifi interface already removed");
2451                 return;
2452         }
2453
2454         connman_device_set_powered(wifi->device, false);
2455
2456         check_p2p_technology();
2457 }
2458
2459 static void set_device_type(const char *type, char dev_type[17])
2460 {
2461         const char *oui = "0050F204";
2462         const char *category = "0100";
2463         const char *sub_category = "0000";
2464
2465         if (!g_strcmp0(type, "handset")) {
2466                 category = "0A00";
2467                 sub_category = "0500";
2468         } else if (!g_strcmp0(type, "vm") || !g_strcmp0(type, "container"))
2469                 sub_category = "0100";
2470         else if (!g_strcmp0(type, "server"))
2471                 sub_category = "0200";
2472         else if (!g_strcmp0(type, "laptop"))
2473                 sub_category = "0500";
2474         else if (!g_strcmp0(type, "desktop"))
2475                 sub_category = "0600";
2476         else if (!g_strcmp0(type, "tablet"))
2477                 sub_category = "0900";
2478         else if (!g_strcmp0(type, "watch"))
2479                 category = "FF00";
2480
2481         snprintf(dev_type, 17, "%s%s%s", category, oui, sub_category);
2482 }
2483
2484 static void p2p_support(GSupplicantInterface *interface)
2485 {
2486         char dev_type[17] = {};
2487         const char *hostname;
2488
2489         DBG("");
2490
2491         if (!g_supplicant_interface_has_p2p(interface))
2492                 return;
2493
2494         if (connman_technology_driver_register(&p2p_tech_driver) < 0) {
2495                 DBG("Could not register P2P technology driver");
2496                 return;
2497         }
2498
2499         hostname = connman_utsname_get_hostname();
2500         if (!hostname)
2501                 hostname = "ConnMan";
2502
2503         set_device_type(connman_machine_get_type(), dev_type);
2504         g_supplicant_interface_set_p2p_device_config(interface,
2505                                                         hostname, dev_type);
2506         connman_peer_driver_register(&peer_driver);
2507 }
2508
2509 static void scan_started(GSupplicantInterface *interface)
2510 {
2511         DBG("");
2512 }
2513
2514 static void scan_finished(GSupplicantInterface *interface)
2515 {
2516         DBG("");
2517 }
2518
2519 static unsigned char calculate_strength(GSupplicantNetwork *supplicant_network)
2520 {
2521         unsigned char strength;
2522
2523         strength = 120 + g_supplicant_network_get_signal(supplicant_network);
2524         if (strength > 100)
2525                 strength = 100;
2526
2527         return strength;
2528 }
2529
2530 static void network_added(GSupplicantNetwork *supplicant_network)
2531 {
2532         struct connman_network *network;
2533         GSupplicantInterface *interface;
2534         struct wifi_data *wifi;
2535         const char *name, *identifier, *security, *group, *mode;
2536         const unsigned char *ssid;
2537         unsigned int ssid_len;
2538         bool wps;
2539         bool wps_pbc;
2540         bool wps_ready;
2541         bool wps_advertizing;
2542
2543         mode = g_supplicant_network_get_mode(supplicant_network);
2544         identifier = g_supplicant_network_get_identifier(supplicant_network);
2545
2546         DBG("%s", identifier);
2547
2548         if (!g_strcmp0(mode, "adhoc"))
2549                 return;
2550
2551         interface = g_supplicant_network_get_interface(supplicant_network);
2552         wifi = g_supplicant_interface_get_data(interface);
2553         name = g_supplicant_network_get_name(supplicant_network);
2554         security = g_supplicant_network_get_security(supplicant_network);
2555         group = g_supplicant_network_get_identifier(supplicant_network);
2556         wps = g_supplicant_network_get_wps(supplicant_network);
2557         wps_pbc = g_supplicant_network_is_wps_pbc(supplicant_network);
2558         wps_ready = g_supplicant_network_is_wps_active(supplicant_network);
2559         wps_advertizing = g_supplicant_network_is_wps_advertizing(
2560                                                         supplicant_network);
2561
2562         if (!wifi)
2563                 return;
2564
2565         ssid = g_supplicant_network_get_ssid(supplicant_network, &ssid_len);
2566
2567         network = connman_device_get_network(wifi->device, identifier);
2568
2569         if (!network) {
2570                 network = connman_network_create(identifier,
2571                                                 CONNMAN_NETWORK_TYPE_WIFI);
2572                 if (!network)
2573                         return;
2574
2575                 connman_network_set_index(network, wifi->index);
2576
2577                 if (connman_device_add_network(wifi->device, network) < 0) {
2578                         connman_network_unref(network);
2579                         return;
2580                 }
2581
2582                 wifi->networks = g_slist_prepend(wifi->networks, network);
2583         }
2584
2585         if (name && name[0] != '\0')
2586                 connman_network_set_name(network, name);
2587
2588         connman_network_set_blob(network, "WiFi.SSID",
2589                                                 ssid, ssid_len);
2590         connman_network_set_string(network, "WiFi.Security", security);
2591         connman_network_set_strength(network,
2592                                 calculate_strength(supplicant_network));
2593         connman_network_set_bool(network, "WiFi.WPS", wps);
2594
2595         if (wps) {
2596                 /* Is AP advertizing for WPS association?
2597                  * If so, we decide to use WPS by default */
2598                 if (wps_ready && wps_pbc &&
2599                                                 wps_advertizing)
2600                         connman_network_set_bool(network, "WiFi.UseWPS", true);
2601         }
2602
2603         connman_network_set_frequency(network,
2604                         g_supplicant_network_get_frequency(supplicant_network));
2605 #if defined TIZEN_EXT
2606         connman_network_set_bssid(network,
2607                         g_supplicant_network_get_bssid(supplicant_network));
2608         connman_network_set_maxrate(network,
2609                         g_supplicant_network_get_maxrate(supplicant_network));
2610         connman_network_set_enc_mode(network,
2611                         g_supplicant_network_get_enc_mode(supplicant_network));
2612 #endif
2613         connman_network_set_available(network, true);
2614         connman_network_set_string(network, "WiFi.Mode", mode);
2615
2616         if (ssid)
2617                 connman_network_set_group(network, group);
2618
2619         if (wifi->hidden && ssid) {
2620                 if (!g_strcmp0(wifi->hidden->security, security) &&
2621                                 wifi->hidden->ssid_len == ssid_len &&
2622                                 !memcmp(wifi->hidden->ssid, ssid, ssid_len)) {
2623                         connman_network_connect_hidden(network,
2624                                         wifi->hidden->identity,
2625                                         wifi->hidden->passphrase,
2626                                         wifi->hidden->user_data);
2627                         wifi->hidden->user_data = NULL;
2628                         hidden_free(wifi->hidden);
2629                         wifi->hidden = NULL;
2630                 }
2631         }
2632 }
2633
2634 static void network_removed(GSupplicantNetwork *network)
2635 {
2636         GSupplicantInterface *interface;
2637         struct wifi_data *wifi;
2638         const char *name, *identifier;
2639         struct connman_network *connman_network;
2640
2641         interface = g_supplicant_network_get_interface(network);
2642         wifi = g_supplicant_interface_get_data(interface);
2643         identifier = g_supplicant_network_get_identifier(network);
2644         name = g_supplicant_network_get_name(network);
2645
2646         DBG("name %s", name);
2647
2648         if (!wifi)
2649                 return;
2650
2651         connman_network = connman_device_get_network(wifi->device, identifier);
2652         if (!connman_network)
2653                 return;
2654
2655         wifi->networks = g_slist_remove(wifi->networks, connman_network);
2656
2657         connman_device_remove_network(wifi->device, connman_network);
2658         connman_network_unref(connman_network);
2659 }
2660
2661 static void network_changed(GSupplicantNetwork *network, const char *property)
2662 {
2663         GSupplicantInterface *interface;
2664         struct wifi_data *wifi;
2665         const char *name, *identifier;
2666         struct connman_network *connman_network;
2667
2668 #if defined TIZEN_EXT
2669         const unsigned char *bssid;
2670         unsigned int maxrate;
2671         uint16_t frequency;
2672 #endif
2673
2674         interface = g_supplicant_network_get_interface(network);
2675         wifi = g_supplicant_interface_get_data(interface);
2676         identifier = g_supplicant_network_get_identifier(network);
2677         name = g_supplicant_network_get_name(network);
2678
2679         DBG("name %s", name);
2680
2681         if (!wifi)
2682                 return;
2683
2684         connman_network = connman_device_get_network(wifi->device, identifier);
2685         if (!connman_network)
2686                 return;
2687
2688         if (g_str_equal(property, "Signal")) {
2689                connman_network_set_strength(connman_network,
2690                                         calculate_strength(network));
2691                connman_network_update(connman_network);
2692         }
2693
2694 #if defined TIZEN_EXT
2695         bssid = g_supplicant_network_get_bssid(network);
2696         maxrate = g_supplicant_network_get_maxrate(network);
2697         frequency = g_supplicant_network_get_frequency(network);
2698
2699         connman_network_set_bssid(connman_network, bssid);
2700         connman_network_set_maxrate(connman_network, maxrate);
2701         connman_network_set_frequency(connman_network, frequency);
2702 #endif
2703 }
2704
2705 static void apply_peer_services(GSupplicantPeer *peer,
2706                                 struct connman_peer *connman_peer)
2707 {
2708         const unsigned char *data;
2709         int length;
2710
2711         DBG("");
2712
2713         connman_peer_reset_services(connman_peer);
2714
2715         data = g_supplicant_peer_get_widi_ies(peer, &length);
2716         if (data) {
2717                 connman_peer_add_service(connman_peer,
2718                         CONNMAN_PEER_SERVICE_WIFI_DISPLAY, data, length);
2719         }
2720 }
2721
2722 static void add_station(const char *mac)
2723 {
2724         connman_technology_tethering_add_station(CONNMAN_SERVICE_TYPE_WIFI,
2725                                                  mac);
2726 }
2727
2728 static void remove_station(const char *mac)
2729 {
2730         connman_technology_tethering_remove_station(mac);
2731 }
2732
2733 static void peer_found(GSupplicantPeer *peer)
2734 {
2735         GSupplicantInterface *iface = g_supplicant_peer_get_interface(peer);
2736         struct wifi_data *wifi = g_supplicant_interface_get_data(iface);
2737         struct connman_peer *connman_peer;
2738         const char *identifier, *name;
2739         int ret;
2740
2741         identifier = g_supplicant_peer_get_identifier(peer);
2742         name = g_supplicant_peer_get_name(peer);
2743
2744         DBG("ident: %s", identifier);
2745
2746         connman_peer = connman_peer_get(wifi->device, identifier);
2747         if (connman_peer)
2748                 return;
2749
2750         connman_peer = connman_peer_create(identifier);
2751         connman_peer_set_name(connman_peer, name);
2752         connman_peer_set_device(connman_peer, wifi->device);
2753         apply_peer_services(peer, connman_peer);
2754
2755         ret = connman_peer_register(connman_peer);
2756         if (ret < 0 && ret != -EALREADY)
2757                 connman_peer_unref(connman_peer);
2758 }
2759
2760 static void peer_lost(GSupplicantPeer *peer)
2761 {
2762         GSupplicantInterface *iface = g_supplicant_peer_get_interface(peer);
2763         struct wifi_data *wifi = g_supplicant_interface_get_data(iface);
2764         struct connman_peer *connman_peer;
2765         const char *identifier;
2766
2767         if (!wifi)
2768                 return;
2769
2770         identifier = g_supplicant_peer_get_identifier(peer);
2771
2772         DBG("ident: %s", identifier);
2773
2774         connman_peer = connman_peer_get(wifi->device, identifier);
2775         if (connman_peer) {
2776                 if (wifi->p2p_connecting &&
2777                                 wifi->pending_peer == connman_peer) {
2778                         peer_connect_timeout(wifi);
2779                 }
2780                 connman_peer_unregister(connman_peer);
2781                 connman_peer_unref(connman_peer);
2782         }
2783 }
2784
2785 static void peer_changed(GSupplicantPeer *peer, GSupplicantPeerState state)
2786 {
2787         GSupplicantInterface *iface = g_supplicant_peer_get_interface(peer);
2788         struct wifi_data *wifi = g_supplicant_interface_get_data(iface);
2789         enum connman_peer_state p_state = CONNMAN_PEER_STATE_UNKNOWN;
2790         struct connman_peer *connman_peer;
2791         const char *identifier;
2792
2793         identifier = g_supplicant_peer_get_identifier(peer);
2794
2795         DBG("ident: %s", identifier);
2796
2797         connman_peer = connman_peer_get(wifi->device, identifier);
2798         if (!connman_peer)
2799                 return;
2800
2801         switch (state) {
2802         case G_SUPPLICANT_PEER_SERVICES_CHANGED:
2803                 apply_peer_services(peer, connman_peer);
2804                 connman_peer_services_changed(connman_peer);
2805                 return;
2806         case G_SUPPLICANT_PEER_GROUP_CHANGED:
2807                 if (!g_supplicant_peer_is_in_a_group(peer))
2808                         p_state = CONNMAN_PEER_STATE_IDLE;
2809                 else
2810                         p_state = CONNMAN_PEER_STATE_CONFIGURATION;
2811                 break;
2812         case G_SUPPLICANT_PEER_GROUP_STARTED:
2813                 break;
2814         case G_SUPPLICANT_PEER_GROUP_FINISHED:
2815                 p_state = CONNMAN_PEER_STATE_IDLE;
2816                 break;
2817         case G_SUPPLICANT_PEER_GROUP_JOINED:
2818                 if (!g_supplicant_peer_is_in_a_group(peer))
2819                         break;
2820                 p_state = CONNMAN_PEER_STATE_READY;
2821                 break;
2822         case G_SUPPLICANT_PEER_GROUP_DISCONNECTED:
2823                 p_state = CONNMAN_PEER_STATE_IDLE;
2824                 break;
2825         case G_SUPPLICANT_PEER_GROUP_FAILED:
2826                 if (g_supplicant_peer_has_requested_connection(peer))
2827                         p_state = CONNMAN_PEER_STATE_IDLE;
2828                 else
2829                         p_state = CONNMAN_PEER_STATE_FAILURE;
2830                 break;
2831         }
2832
2833         if (p_state == CONNMAN_PEER_STATE_CONFIGURATION ||
2834                                         p_state == CONNMAN_PEER_STATE_FAILURE) {
2835                 if (wifi->p2p_connecting
2836                                 && connman_peer == wifi->pending_peer)
2837                         peer_cancel_timeout(wifi);
2838                 else
2839                         p_state = CONNMAN_PEER_STATE_UNKNOWN;
2840         }
2841
2842         if (p_state == CONNMAN_PEER_STATE_UNKNOWN)
2843                 return;
2844
2845         if (p_state == CONNMAN_PEER_STATE_CONFIGURATION) {
2846                 GSupplicantInterface *g_iface;
2847                 struct wifi_data *g_wifi;
2848
2849                 g_iface = g_supplicant_peer_get_group_interface(peer);
2850                 if (!g_iface)
2851                         return;
2852
2853                 g_wifi = g_supplicant_interface_get_data(g_iface);
2854                 if (!g_wifi)
2855                         return;
2856
2857                 connman_peer_set_as_master(connman_peer,
2858                                         !g_supplicant_peer_is_client(peer));
2859                 connman_peer_set_sub_device(connman_peer, g_wifi->device);
2860         }
2861
2862         connman_peer_set_state(connman_peer, p_state);
2863 }
2864
2865 static void peer_request(GSupplicantPeer *peer)
2866 {
2867         GSupplicantInterface *iface = g_supplicant_peer_get_interface(peer);
2868         struct wifi_data *wifi = g_supplicant_interface_get_data(iface);
2869         struct connman_peer *connman_peer;
2870         const char *identifier;
2871
2872         identifier = g_supplicant_peer_get_identifier(peer);
2873
2874         DBG("ident: %s", identifier);
2875
2876         connman_peer = connman_peer_get(wifi->device, identifier);
2877         if (!connman_peer)
2878                 return;
2879
2880         connman_peer_request_connection(connman_peer);
2881 }
2882
2883 static void debug(const char *str)
2884 {
2885         if (getenv("CONNMAN_SUPPLICANT_DEBUG"))
2886                 connman_debug("%s", str);
2887 }
2888
2889 static const GSupplicantCallbacks callbacks = {
2890         .system_ready           = system_ready,
2891         .system_killed          = system_killed,
2892         .interface_added        = interface_added,
2893         .interface_state        = interface_state,
2894         .interface_removed      = interface_removed,
2895         .p2p_support            = p2p_support,
2896         .scan_started           = scan_started,
2897         .scan_finished          = scan_finished,
2898         .network_added          = network_added,
2899         .network_removed        = network_removed,
2900         .network_changed        = network_changed,
2901         .add_station            = add_station,
2902         .remove_station         = remove_station,
2903         .peer_found             = peer_found,
2904         .peer_lost              = peer_lost,
2905         .peer_changed           = peer_changed,
2906         .peer_request           = peer_request,
2907         .debug                  = debug,
2908 };
2909
2910
2911 static int tech_probe(struct connman_technology *technology)
2912 {
2913         wifi_technology = technology;
2914
2915         return 0;
2916 }
2917
2918 static void tech_remove(struct connman_technology *technology)
2919 {
2920         wifi_technology = NULL;
2921 }
2922
2923 struct wifi_tethering_info {
2924         struct wifi_data *wifi;
2925         struct connman_technology *technology;
2926         char *ifname;
2927         GSupplicantSSID *ssid;
2928 };
2929
2930 static GSupplicantSSID *ssid_ap_init(const char *ssid, const char *passphrase)
2931 {
2932         GSupplicantSSID *ap;
2933
2934         ap = g_try_malloc0(sizeof(GSupplicantSSID));
2935         if (!ap)
2936                 return NULL;
2937
2938         ap->mode = G_SUPPLICANT_MODE_MASTER;
2939         ap->ssid = ssid;
2940         ap->ssid_len = strlen(ssid);
2941         ap->scan_ssid = 0;
2942         ap->freq = 2412;
2943
2944         if (!passphrase || strlen(passphrase) == 0) {
2945                 ap->security = G_SUPPLICANT_SECURITY_NONE;
2946                 ap->passphrase = NULL;
2947         } else {
2948                ap->security = G_SUPPLICANT_SECURITY_PSK;
2949                ap->protocol = G_SUPPLICANT_PROTO_RSN;
2950                ap->pairwise_cipher = G_SUPPLICANT_PAIRWISE_CCMP;
2951                ap->group_cipher = G_SUPPLICANT_GROUP_CCMP;
2952                ap->passphrase = passphrase;
2953         }
2954
2955         return ap;
2956 }
2957
2958 static void ap_start_callback(int result, GSupplicantInterface *interface,
2959                                                         void *user_data)
2960 {
2961         struct wifi_tethering_info *info = user_data;
2962
2963         DBG("result %d index %d bridge %s",
2964                 result, info->wifi->index, info->wifi->bridge);
2965
2966         if (result < 0) {
2967                 connman_inet_remove_from_bridge(info->wifi->index,
2968                                                         info->wifi->bridge);
2969                 connman_technology_tethering_notify(info->technology, false);
2970         }
2971
2972         g_free(info->ifname);
2973         g_free(info);
2974 }
2975
2976 static void ap_create_callback(int result,
2977                                 GSupplicantInterface *interface,
2978                                         void *user_data)
2979 {
2980         struct wifi_tethering_info *info = user_data;
2981
2982         DBG("result %d ifname %s", result,
2983                                 g_supplicant_interface_get_ifname(interface));
2984
2985         if (result < 0) {
2986                 connman_inet_remove_from_bridge(info->wifi->index,
2987                                                         info->wifi->bridge);
2988                 connman_technology_tethering_notify(info->technology, false);
2989
2990                 g_free(info->ifname);
2991                 g_free(info->ssid);
2992                 g_free(info);
2993                 return;
2994         }
2995
2996         info->wifi->interface = interface;
2997         g_supplicant_interface_set_data(interface, info->wifi);
2998
2999         if (g_supplicant_interface_set_apscan(interface, 2) < 0)
3000                 connman_error("Failed to set interface ap_scan property");
3001
3002         g_supplicant_interface_connect(interface, info->ssid,
3003                                                 ap_start_callback, info);
3004 }
3005
3006 static void sta_remove_callback(int result,
3007                                 GSupplicantInterface *interface,
3008                                         void *user_data)
3009 {
3010         struct wifi_tethering_info *info = user_data;
3011         const char *driver = connman_option_get_string("wifi");
3012
3013         DBG("ifname %s result %d ", info->ifname, result);
3014
3015         if (result < 0) {
3016                 info->wifi->tethering = true;
3017
3018                 g_free(info->ifname);
3019                 g_free(info->ssid);
3020                 g_free(info);
3021                 return;
3022         }
3023
3024         info->wifi->interface = NULL;
3025
3026         connman_technology_tethering_notify(info->technology, true);
3027
3028         g_supplicant_interface_create(info->ifname, driver, info->wifi->bridge,
3029                                                 ap_create_callback,
3030                                                         info);
3031 }
3032
3033 static int tech_set_tethering(struct connman_technology *technology,
3034                                 const char *identifier, const char *passphrase,
3035                                 const char *bridge, bool enabled)
3036 {
3037         GList *list;
3038         GSupplicantInterface *interface;
3039         struct wifi_data *wifi;
3040         struct wifi_tethering_info *info;
3041         const char *ifname;
3042         unsigned int mode;
3043         int err;
3044
3045         DBG("");
3046
3047         if (!enabled) {
3048                 for (list = iface_list; list; list = list->next) {
3049                         wifi = list->data;
3050
3051                         if (wifi->tethering) {
3052                                 wifi->tethering = false;
3053
3054                                 connman_inet_remove_from_bridge(wifi->index,
3055                                                                         bridge);
3056                                 wifi->bridged = false;
3057                         }
3058                 }
3059
3060                 connman_technology_tethering_notify(technology, false);
3061
3062                 return 0;
3063         }
3064
3065         for (list = iface_list; list; list = list->next) {
3066                 wifi = list->data;
3067
3068                 interface = wifi->interface;
3069
3070                 if (!interface)
3071                         continue;
3072
3073                 ifname = g_supplicant_interface_get_ifname(wifi->interface);
3074
3075                 mode = g_supplicant_interface_get_mode(interface);
3076                 if ((mode & G_SUPPLICANT_CAPABILITY_MODE_AP) == 0) {
3077                         DBG("%s does not support AP mode", ifname);
3078                         continue;
3079                 }
3080
3081                 info = g_try_malloc0(sizeof(struct wifi_tethering_info));
3082                 if (!info)
3083                         return -ENOMEM;
3084
3085                 info->wifi = wifi;
3086                 info->technology = technology;
3087                 info->wifi->bridge = bridge;
3088                 info->ssid = ssid_ap_init(identifier, passphrase);
3089                 if (!info->ssid) {
3090                         g_free(info);
3091                         continue;
3092                 }
3093                 info->ifname = g_strdup(ifname);
3094                 if (!info->ifname) {
3095                         g_free(info->ssid);
3096                         g_free(info);
3097                         continue;
3098                 }
3099
3100                 info->wifi->tethering = true;
3101
3102                 err = g_supplicant_interface_remove(interface,
3103                                                 sta_remove_callback,
3104                                                         info);
3105                 if (err == 0)
3106                         return err;
3107         }
3108
3109         return -EOPNOTSUPP;
3110 }
3111
3112 static void regdom_callback(int result, const char *alpha2, void *user_data)
3113 {
3114         DBG("");
3115
3116         if (!wifi_technology)
3117                 return;
3118
3119         if (result != 0)
3120                 alpha2 = NULL;
3121
3122         connman_technology_regdom_notify(wifi_technology, alpha2);
3123 }
3124
3125 static int tech_set_regdom(struct connman_technology *technology, const char *alpha2)
3126 {
3127         return g_supplicant_set_country(alpha2, regdom_callback, NULL);
3128 }
3129
3130 static struct connman_technology_driver tech_driver = {
3131         .name           = "wifi",
3132         .type           = CONNMAN_SERVICE_TYPE_WIFI,
3133         .probe          = tech_probe,
3134         .remove         = tech_remove,
3135         .set_tethering  = tech_set_tethering,
3136         .set_regdom     = tech_set_regdom,
3137 };
3138
3139 static int wifi_init(void)
3140 {
3141         int err;
3142
3143         err = connman_network_driver_register(&network_driver);
3144         if (err < 0)
3145                 return err;
3146
3147         err = g_supplicant_register(&callbacks);
3148         if (err < 0) {
3149                 connman_network_driver_unregister(&network_driver);
3150                 return err;
3151         }
3152
3153         err = connman_technology_driver_register(&tech_driver);
3154         if (err < 0) {
3155                 g_supplicant_unregister(&callbacks);
3156                 connman_network_driver_unregister(&network_driver);
3157                 return err;
3158         }
3159
3160         return 0;
3161 }
3162
3163 static void wifi_exit(void)
3164 {
3165         DBG();
3166
3167         connman_technology_driver_unregister(&tech_driver);
3168
3169         g_supplicant_unregister(&callbacks);
3170
3171         connman_network_driver_unregister(&network_driver);
3172 }
3173
3174 CONNMAN_PLUGIN_DEFINE(wifi, "WiFi interface plugin", VERSION,
3175                 CONNMAN_PLUGIN_PRIORITY_DEFAULT, wifi_init, wifi_exit)