42dd4074d4c61d3d030768860b7fede081403953
[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, 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                 name = g_key_file_get_string(keyfile, services[i], "Name",
1026                                                                 NULL);
1027
1028                 ret = add_scan_param(ssid, NULL, 0, 0, scan_data, 0, name);
1029                 if (ret < 0)
1030                         add_param_failed++;
1031                 else if (ret > 0)
1032                         num_ssids++;
1033
1034                 g_free(ssid);
1035                 g_free(name);
1036                 g_key_file_free(keyfile);
1037         }
1038
1039         /*
1040          * Check if there are any hidden AP that needs to be provisioned.
1041          */
1042         entries = connman_config_get_entries("wifi");
1043         for (i = 0; entries && entries[i]; i++) {
1044                 int len;
1045
1046                 if (!entries[i]->hidden)
1047                         continue;
1048
1049                 if (!entries[i]->ssid) {
1050                         ssid = entries[i]->name;
1051                         len = strlen(ssid);
1052                 } else {
1053                         ssid = entries[i]->ssid;
1054                         len = entries[i]->ssid_len;
1055                 }
1056
1057                 if (!ssid)
1058                         continue;
1059
1060                 ret = add_scan_param(NULL, ssid, len, 0, scan_data, 0, ssid);
1061                 if (ret < 0)
1062                         add_param_failed++;
1063                 else if (ret > 0)
1064                         num_ssids++;
1065         }
1066
1067         connman_config_free_entries(entries);
1068
1069         if (add_param_failed > 0)
1070                 DBG("Unable to scan %d out of %d SSIDs",
1071                                         add_param_failed, num_ssids);
1072
1073         g_strfreev(services);
1074
1075         return num_ssids;
1076 }
1077
1078 static int get_hidden_connections_params(struct wifi_data *wifi,
1079                                         GSupplicantScanParams *scan_params)
1080 {
1081         int driver_max_ssids, i;
1082         GSupplicantScanParams *orig_params;
1083
1084         /*
1085          * Scan hidden networks so that we can autoconnect to them.
1086          * We will assume 1 as a default number of ssid to scan.
1087          */
1088         driver_max_ssids = g_supplicant_interface_get_max_scan_ssids(
1089                                                         wifi->interface);
1090         if (driver_max_ssids == 0)
1091                 driver_max_ssids = 1;
1092
1093         DBG("max ssids %d", driver_max_ssids);
1094
1095         if (!wifi->scan_params) {
1096                 wifi->scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
1097                 if (!wifi->scan_params)
1098                         return 0;
1099
1100                 if (get_hidden_connections(wifi->scan_params) == 0) {
1101                         g_supplicant_free_scan_params(wifi->scan_params);
1102                         wifi->scan_params = NULL;
1103
1104                         return 0;
1105                 }
1106         }
1107
1108         orig_params = wifi->scan_params;
1109
1110         /* Let's transfer driver_max_ssids params */
1111         for (i = 0; i < driver_max_ssids; i++) {
1112                 struct scan_ssid *ssid;
1113
1114                 if (!wifi->scan_params->ssids)
1115                         break;
1116
1117                 ssid = orig_params->ssids->data;
1118                 orig_params->ssids = g_slist_remove(orig_params->ssids, ssid);
1119                 scan_params->ssids = g_slist_prepend(scan_params->ssids, ssid);
1120         }
1121
1122         if (i > 0) {
1123                 scan_params->num_ssids = i;
1124                 scan_params->ssids = g_slist_reverse(scan_params->ssids);
1125
1126                 scan_params->freqs = g_memdup(orig_params->freqs,
1127                                 sizeof(uint16_t) * orig_params->num_freqs);
1128                 if (!scan_params->freqs)
1129                         goto err;
1130
1131                 scan_params->num_freqs = orig_params->num_freqs;
1132
1133         } else
1134                 goto err;
1135
1136         orig_params->num_ssids -= scan_params->num_ssids;
1137
1138         return scan_params->num_ssids;
1139
1140 err:
1141         g_slist_free_full(scan_params->ssids, g_free);
1142         g_supplicant_free_scan_params(wifi->scan_params);
1143         wifi->scan_params = NULL;
1144
1145         return 0;
1146 }
1147
1148 static int throw_wifi_scan(struct connman_device *device,
1149                         GSupplicantInterfaceCallback callback)
1150 {
1151         struct wifi_data *wifi = connman_device_get_data(device);
1152         int ret;
1153
1154         if (!wifi)
1155                 return -ENODEV;
1156
1157         DBG("device %p %p", device, wifi->interface);
1158
1159         if (wifi->tethering)
1160                 return -EBUSY;
1161
1162         if (connman_device_get_scanning(device))
1163                 return -EALREADY;
1164
1165         connman_device_ref(device);
1166
1167         ret = g_supplicant_interface_scan(wifi->interface, NULL,
1168                                                 callback, device);
1169         if (ret == 0) {
1170                 connman_device_set_scanning(device,
1171                                 CONNMAN_SERVICE_TYPE_WIFI, true);
1172         } else
1173                 connman_device_unref(device);
1174
1175         return ret;
1176 }
1177
1178 static void hidden_free(struct hidden_params *hidden)
1179 {
1180         if (!hidden)
1181                 return;
1182
1183         if (hidden->scan_params)
1184                 g_supplicant_free_scan_params(hidden->scan_params);
1185         g_free(hidden->identity);
1186         g_free(hidden->passphrase);
1187         g_free(hidden->security);
1188         g_free(hidden);
1189 }
1190
1191 static void scan_callback(int result, GSupplicantInterface *interface,
1192                                                 void *user_data)
1193 {
1194         struct connman_device *device = user_data;
1195         struct wifi_data *wifi = connman_device_get_data(device);
1196         bool scanning;
1197
1198         DBG("result %d wifi %p", result, wifi);
1199
1200         if (wifi) {
1201                 if (wifi->hidden && !wifi->postpone_hidden) {
1202                         connman_network_clear_hidden(wifi->hidden->user_data);
1203                         hidden_free(wifi->hidden);
1204                         wifi->hidden = NULL;
1205                 }
1206
1207                 if (wifi->scan_params) {
1208                         g_supplicant_free_scan_params(wifi->scan_params);
1209                         wifi->scan_params = NULL;
1210                 }
1211         }
1212
1213         if (result < 0)
1214                 connman_device_reset_scanning(device);
1215
1216         /* User is connecting to a hidden AP, let's wait for finished event */
1217         if (wifi && wifi->hidden && wifi->postpone_hidden) {
1218                 GSupplicantScanParams *scan_params;
1219                 int ret;
1220
1221                 wifi->postpone_hidden = false;
1222                 scan_params = wifi->hidden->scan_params;
1223                 wifi->hidden->scan_params = NULL;
1224
1225                 reset_autoscan(device);
1226
1227                 ret = g_supplicant_interface_scan(wifi->interface, scan_params,
1228                                                         scan_callback, device);
1229                 if (ret == 0)
1230                         return;
1231
1232                 /* On error, let's recall scan_callback, which will cleanup */
1233                 return scan_callback(ret, interface, user_data);
1234         }
1235
1236         scanning = connman_device_get_scanning(device);
1237
1238         if (scanning) {
1239                 connman_device_set_scanning(device,
1240                                 CONNMAN_SERVICE_TYPE_WIFI, false);
1241         }
1242
1243         if (result != -ENOLINK)
1244                 start_autoscan(device);
1245
1246         /*
1247          * If we are here then we were scanning; however, if we are
1248          * also mid-flight disabling the interface, then wifi_disable
1249          * has already cleared the device scanning state and
1250          * unreferenced the device, obviating the need to do it here.
1251          */
1252
1253         if (scanning)
1254                 connman_device_unref(device);
1255 }
1256
1257 static void scan_callback_hidden(int result,
1258                         GSupplicantInterface *interface, void *user_data)
1259 {
1260         struct connman_device *device = user_data;
1261         struct wifi_data *wifi = connman_device_get_data(device);
1262         GSupplicantScanParams *scan_params;
1263         int ret;
1264
1265         DBG("result %d wifi %p", result, wifi);
1266
1267         if (!wifi)
1268                 goto out;
1269
1270         /* User is trying to connect to a hidden AP */
1271         if (wifi->hidden && wifi->postpone_hidden)
1272                 goto out;
1273
1274         scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
1275         if (!scan_params)
1276                 goto out;
1277
1278         if (get_hidden_connections_params(wifi, scan_params) > 0) {
1279                 ret = g_supplicant_interface_scan(wifi->interface,
1280                                                         scan_params,
1281                                                         scan_callback_hidden,
1282                                                         device);
1283                 if (ret == 0)
1284                         return;
1285         }
1286
1287         g_supplicant_free_scan_params(scan_params);
1288
1289 out:
1290         scan_callback(result, interface, user_data);
1291 }
1292
1293 static gboolean autoscan_timeout(gpointer data)
1294 {
1295         struct connman_device *device = data;
1296         struct wifi_data *wifi = connman_device_get_data(device);
1297         struct autoscan_params *autoscan;
1298         int interval;
1299
1300         if (!wifi)
1301                 return FALSE;
1302
1303         autoscan = wifi->autoscan;
1304
1305         if (autoscan->interval <= 0) {
1306                 interval = autoscan->base;
1307                 goto set_interval;
1308         } else
1309                 interval = autoscan->interval * autoscan->base;
1310
1311         if (interval > autoscan->limit)
1312                 interval = autoscan->limit;
1313
1314         throw_wifi_scan(wifi->device, scan_callback_hidden);
1315
1316 set_interval:
1317         DBG("interval %d", interval);
1318
1319         autoscan->interval = interval;
1320
1321         autoscan->timeout = g_timeout_add_seconds(interval,
1322                                                 autoscan_timeout, device);
1323
1324         return FALSE;
1325 }
1326
1327 static void start_autoscan(struct connman_device *device)
1328 {
1329         struct wifi_data *wifi = connman_device_get_data(device);
1330         struct autoscan_params *autoscan;
1331
1332         DBG("");
1333
1334         if (!wifi)
1335                 return;
1336
1337         if (wifi->p2p_device)
1338                 return;
1339
1340         if (wifi->connected)
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 finalize_interface_creation(struct wifi_data *wifi)
1400 {
1401         DBG("interface is ready wifi %p tethering %d", wifi, wifi->tethering);
1402
1403         if (!wifi->device) {
1404                 connman_error("WiFi device not set");
1405                 return;
1406         }
1407
1408         connman_device_set_powered(wifi->device, true);
1409
1410         if (!connman_setting_get_bool("BackgroundScanning"))
1411                 return;
1412
1413         if (wifi->p2p_device)
1414                 return;
1415
1416         setup_autoscan(wifi);
1417 }
1418
1419 static void interface_create_callback(int result,
1420                                         GSupplicantInterface *interface,
1421                                                         void *user_data)
1422 {
1423         struct wifi_data *wifi = user_data;
1424
1425         DBG("result %d ifname %s, wifi %p", result,
1426                                 g_supplicant_interface_get_ifname(interface),
1427                                 wifi);
1428
1429         if (result < 0 || !wifi)
1430                 return;
1431
1432         wifi->interface = interface;
1433         g_supplicant_interface_set_data(interface, wifi);
1434
1435         if (g_supplicant_interface_get_ready(interface)) {
1436                 wifi->interface_ready = true;
1437                 finalize_interface_creation(wifi);
1438         }
1439 }
1440
1441 static int wifi_enable(struct connman_device *device)
1442 {
1443         struct wifi_data *wifi = connman_device_get_data(device);
1444         int index;
1445         char *interface;
1446         const char *driver = connman_option_get_string("wifi");
1447         int ret;
1448
1449         DBG("device %p %p", device, wifi);
1450
1451         index = connman_device_get_index(device);
1452         if (!wifi || index < 0)
1453                 return -ENODEV;
1454
1455         if (is_p2p_connecting())
1456                 return -EINPROGRESS;
1457
1458         interface = connman_inet_ifname(index);
1459         ret = g_supplicant_interface_create(interface, driver, NULL,
1460                                                 interface_create_callback,
1461                                                         wifi);
1462         g_free(interface);
1463
1464         if (ret < 0)
1465                 return ret;
1466
1467         return -EINPROGRESS;
1468 }
1469
1470 static int wifi_disable(struct connman_device *device)
1471 {
1472         struct wifi_data *wifi = connman_device_get_data(device);
1473         int ret;
1474
1475         DBG("device %p wifi %p", device, wifi);
1476
1477         if (!wifi)
1478                 return -ENODEV;
1479
1480         wifi->connected = false;
1481         wifi->disconnecting = false;
1482
1483         if (wifi->pending_network)
1484                 wifi->pending_network = NULL;
1485
1486         stop_autoscan(device);
1487
1488         if (wifi->p2p_find_timeout) {
1489                 g_source_remove(wifi->p2p_find_timeout);
1490                 wifi->p2p_find_timeout = 0;
1491                 connman_device_set_scanning(device, CONNMAN_SERVICE_TYPE_P2P, false);
1492                 connman_device_unref(wifi->device);
1493         }
1494
1495         /* In case of a user scan, device is still referenced */
1496         if (connman_device_get_scanning(device)) {
1497                 connman_device_set_scanning(device,
1498                                 CONNMAN_SERVICE_TYPE_WIFI, false);
1499                 connman_device_unref(wifi->device);
1500         }
1501
1502         remove_networks(device, wifi);
1503
1504         ret = g_supplicant_interface_remove(wifi->interface, NULL, NULL);
1505         if (ret < 0)
1506                 return ret;
1507
1508         return -EINPROGRESS;
1509 }
1510
1511 struct last_connected {
1512         GTimeVal modified;
1513         gchar *ssid;
1514         int freq;
1515 };
1516
1517 static gint sort_entry(gconstpointer a, gconstpointer b, gpointer user_data)
1518 {
1519         GTimeVal *aval = (GTimeVal *)a;
1520         GTimeVal *bval = (GTimeVal *)b;
1521
1522         /* Note that the sort order is descending */
1523         if (aval->tv_sec < bval->tv_sec)
1524                 return 1;
1525
1526         if (aval->tv_sec > bval->tv_sec)
1527                 return -1;
1528
1529         return 0;
1530 }
1531
1532 static void free_entry(gpointer data)
1533 {
1534         struct last_connected *entry = data;
1535
1536         g_free(entry->ssid);
1537         g_free(entry);
1538 }
1539
1540 static int get_latest_connections(int max_ssids,
1541                                 GSupplicantScanParams *scan_data)
1542 {
1543         GSequenceIter *iter;
1544         GSequence *latest_list;
1545         struct last_connected *entry;
1546         GKeyFile *keyfile;
1547         GTimeVal modified;
1548         gchar **services;
1549         gchar *str;
1550         char *ssid;
1551         int i, freq;
1552         int num_ssids = 0;
1553
1554         latest_list = g_sequence_new(free_entry);
1555         if (!latest_list)
1556                 return -ENOMEM;
1557
1558         services = connman_storage_get_services();
1559         for (i = 0; services && services[i]; i++) {
1560                 if (strncmp(services[i], "wifi_", 5) != 0)
1561                         continue;
1562
1563                 keyfile = connman_storage_load_service(services[i]);
1564                 if (!keyfile)
1565                         continue;
1566
1567                 str = g_key_file_get_string(keyfile,
1568                                         services[i], "Favorite", NULL);
1569                 if (!str || g_strcmp0(str, "true")) {
1570                         g_free(str);
1571                         g_key_file_free(keyfile);
1572                         continue;
1573                 }
1574                 g_free(str);
1575
1576                 str = g_key_file_get_string(keyfile,
1577                                         services[i], "AutoConnect", NULL);
1578                 if (!str || g_strcmp0(str, "true")) {
1579                         g_free(str);
1580                         g_key_file_free(keyfile);
1581                         continue;
1582                 }
1583                 g_free(str);
1584
1585                 str = g_key_file_get_string(keyfile,
1586                                         services[i], "Modified", NULL);
1587                 if (!str) {
1588                         g_key_file_free(keyfile);
1589                         continue;
1590                 }
1591                 g_time_val_from_iso8601(str, &modified);
1592                 g_free(str);
1593
1594                 ssid = g_key_file_get_string(keyfile,
1595                                         services[i], "SSID", NULL);
1596
1597                 freq = g_key_file_get_integer(keyfile, services[i],
1598                                         "Frequency", NULL);
1599                 if (freq) {
1600                         entry = g_try_new(struct last_connected, 1);
1601                         if (!entry) {
1602                                 g_sequence_free(latest_list);
1603                                 g_key_file_free(keyfile);
1604                                 g_free(ssid);
1605                                 return -ENOMEM;
1606                         }
1607
1608                         entry->ssid = ssid;
1609                         entry->modified = modified;
1610                         entry->freq = freq;
1611
1612                         g_sequence_insert_sorted(latest_list, entry,
1613                                                 sort_entry, NULL);
1614                         num_ssids++;
1615                 } else
1616                         g_free(ssid);
1617
1618                 g_key_file_free(keyfile);
1619         }
1620
1621         g_strfreev(services);
1622
1623         num_ssids = num_ssids > max_ssids ? max_ssids : num_ssids;
1624
1625         iter = g_sequence_get_begin_iter(latest_list);
1626
1627         for (i = 0; i < num_ssids; i++) {
1628                 entry = g_sequence_get(iter);
1629
1630                 DBG("ssid %s freq %d modified %lu", entry->ssid, entry->freq,
1631                                                 entry->modified.tv_sec);
1632
1633                 add_scan_param(entry->ssid, NULL, 0, entry->freq, scan_data,
1634                                                 max_ssids, entry->ssid);
1635
1636                 iter = g_sequence_iter_next(iter);
1637         }
1638
1639         g_sequence_free(latest_list);
1640         return num_ssids;
1641 }
1642
1643 static int wifi_scan_simple(struct connman_device *device)
1644 {
1645         reset_autoscan(device);
1646
1647         return throw_wifi_scan(device, scan_callback_hidden);
1648 }
1649
1650 static gboolean p2p_find_stop(gpointer data)
1651 {
1652         struct connman_device *device = data;
1653         struct wifi_data *wifi = connman_device_get_data(device);
1654
1655         DBG("");
1656
1657         wifi->p2p_find_timeout = 0;
1658
1659         connman_device_set_scanning(device, CONNMAN_SERVICE_TYPE_P2P, false);
1660
1661         g_supplicant_interface_p2p_stop_find(wifi->interface);
1662
1663         connman_device_unref(device);
1664         reset_autoscan(device);
1665
1666         return FALSE;
1667 }
1668
1669 static void p2p_find_callback(int result, GSupplicantInterface *interface,
1670                                                         void *user_data)
1671 {
1672         struct connman_device *device = user_data;
1673         struct wifi_data *wifi = connman_device_get_data(device);
1674
1675         DBG("result %d wifi %p", result, wifi);
1676
1677         if (wifi->p2p_find_timeout) {
1678                 g_source_remove(wifi->p2p_find_timeout);
1679                 wifi->p2p_find_timeout = 0;
1680         }
1681
1682         if (result)
1683                 goto error;
1684
1685         wifi->p2p_find_timeout = g_timeout_add_seconds(P2P_FIND_TIMEOUT,
1686                                                         p2p_find_stop, device);
1687         if (!wifi->p2p_find_timeout)
1688                 goto error;
1689
1690         return;
1691 error:
1692         p2p_find_stop(device);
1693 }
1694
1695 static int p2p_find(struct connman_device *device)
1696 {
1697         struct wifi_data *wifi;
1698         int ret;
1699
1700         DBG("");
1701
1702         if (!p2p_technology)
1703                 return -ENOTSUP;
1704
1705         wifi = connman_device_get_data(device);
1706
1707         if (g_supplicant_interface_is_p2p_finding(wifi->interface))
1708                 return -EALREADY;
1709
1710         reset_autoscan(device);
1711         connman_device_ref(device);
1712
1713         ret = g_supplicant_interface_p2p_find(wifi->interface,
1714                                                 p2p_find_callback, device);
1715         if (ret) {
1716                 connman_device_unref(device);
1717                 start_autoscan(device);
1718         } else {
1719                 connman_device_set_scanning(device,
1720                                 CONNMAN_SERVICE_TYPE_P2P, true);
1721         }
1722
1723         return ret;
1724 }
1725
1726 /*
1727  * Note that the hidden scan is only used when connecting to this specific
1728  * hidden AP first time. It is not used when system autoconnects to hidden AP.
1729  */
1730 static int wifi_scan(enum connman_service_type type,
1731                         struct connman_device *device,
1732                         const char *ssid, unsigned int ssid_len,
1733                         const char *identity, const char* passphrase,
1734                         const char *security, void *user_data)
1735 {
1736         struct wifi_data *wifi = connman_device_get_data(device);
1737         GSupplicantScanParams *scan_params = NULL;
1738         struct scan_ssid *scan_ssid;
1739         struct hidden_params *hidden;
1740         int ret;
1741         int driver_max_ssids = 0;
1742         bool do_hidden;
1743         bool scanning;
1744
1745         if (!wifi)
1746                 return -ENODEV;
1747
1748         if (wifi->p2p_device)
1749                 return 0;
1750
1751         if (type == CONNMAN_SERVICE_TYPE_P2P)
1752                 return p2p_find(device);
1753
1754         DBG("device %p wifi %p hidden ssid %s", device, wifi->interface, ssid);
1755
1756         if (wifi->tethering)
1757                 return 0;
1758
1759         scanning = connman_device_get_scanning(device);
1760
1761         if (!ssid || ssid_len == 0 || ssid_len > 32) {
1762                 if (scanning)
1763                         return -EALREADY;
1764
1765                 driver_max_ssids = g_supplicant_interface_get_max_scan_ssids(
1766                                                         wifi->interface);
1767                 DBG("max ssids %d", driver_max_ssids);
1768                 if (driver_max_ssids == 0)
1769                         return wifi_scan_simple(device);
1770
1771                 do_hidden = false;
1772         } else {
1773                 if (scanning && wifi->hidden && wifi->postpone_hidden)
1774                         return -EALREADY;
1775
1776                 do_hidden = true;
1777         }
1778
1779         scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
1780         if (!scan_params)
1781                 return -ENOMEM;
1782
1783         if (do_hidden) {
1784                 scan_ssid = g_try_new(struct scan_ssid, 1);
1785                 if (!scan_ssid) {
1786                         g_free(scan_params);
1787                         return -ENOMEM;
1788                 }
1789
1790                 memcpy(scan_ssid->ssid, ssid, ssid_len);
1791                 scan_ssid->ssid_len = ssid_len;
1792                 scan_params->ssids = g_slist_prepend(scan_params->ssids,
1793                                                                 scan_ssid);
1794                 scan_params->num_ssids = 1;
1795
1796                 hidden = g_try_new0(struct hidden_params, 1);
1797                 if (!hidden) {
1798                         g_supplicant_free_scan_params(scan_params);
1799                         return -ENOMEM;
1800                 }
1801
1802                 if (wifi->hidden) {
1803                         hidden_free(wifi->hidden);
1804                         wifi->hidden = NULL;
1805                 }
1806
1807                 memcpy(hidden->ssid, ssid, ssid_len);
1808                 hidden->ssid_len = ssid_len;
1809                 hidden->identity = g_strdup(identity);
1810                 hidden->passphrase = g_strdup(passphrase);
1811                 hidden->security = g_strdup(security);
1812                 hidden->user_data = user_data;
1813                 wifi->hidden = hidden;
1814
1815                 if (scanning) {
1816                         /* Let's keep this active scan for later,
1817                          * when current scan will be over. */
1818                         wifi->postpone_hidden = TRUE;
1819                         hidden->scan_params = scan_params;
1820
1821                         return 0;
1822                 }
1823         } else if (wifi->connected) {
1824                 g_supplicant_free_scan_params(scan_params);
1825                 return wifi_scan_simple(device);
1826         } else {
1827                 ret = get_latest_connections(driver_max_ssids, scan_params);
1828                 if (ret <= 0) {
1829                         g_supplicant_free_scan_params(scan_params);
1830                         return wifi_scan_simple(device);
1831                 }
1832         }
1833
1834         connman_device_ref(device);
1835
1836         reset_autoscan(device);
1837
1838         ret = g_supplicant_interface_scan(wifi->interface, scan_params,
1839                                                 scan_callback, device);
1840         if (ret == 0) {
1841                 connman_device_set_scanning(device,
1842                                 CONNMAN_SERVICE_TYPE_WIFI, true);
1843         } else {
1844                 g_supplicant_free_scan_params(scan_params);
1845                 connman_device_unref(device);
1846
1847                 if (do_hidden) {
1848                         hidden_free(wifi->hidden);
1849                         wifi->hidden = NULL;
1850                 }
1851         }
1852
1853         return ret;
1854 }
1855
1856 static void wifi_regdom_callback(int result,
1857                                         const char *alpha2,
1858                                                 void *user_data)
1859 {
1860         struct connman_device *device = user_data;
1861
1862         connman_device_regdom_notify(device, result, alpha2);
1863
1864         connman_device_unref(device);
1865 }
1866
1867 static int wifi_set_regdom(struct connman_device *device, const char *alpha2)
1868 {
1869         struct wifi_data *wifi = connman_device_get_data(device);
1870         int ret;
1871
1872         if (!wifi)
1873                 return -EINVAL;
1874
1875         connman_device_ref(device);
1876
1877         ret = g_supplicant_interface_set_country(wifi->interface,
1878                                                 wifi_regdom_callback,
1879                                                         alpha2, device);
1880         if (ret != 0)
1881                 connman_device_unref(device);
1882
1883         return ret;
1884 }
1885
1886 static struct connman_device_driver wifi_ng_driver = {
1887         .name           = "wifi",
1888         .type           = CONNMAN_DEVICE_TYPE_WIFI,
1889         .priority       = CONNMAN_DEVICE_PRIORITY_LOW,
1890         .probe          = wifi_probe,
1891         .remove         = wifi_remove,
1892         .enable         = wifi_enable,
1893         .disable        = wifi_disable,
1894         .scan           = wifi_scan,
1895         .set_regdom     = wifi_set_regdom,
1896 };
1897
1898 static void system_ready(void)
1899 {
1900         DBG("");
1901
1902         if (connman_device_driver_register(&wifi_ng_driver) < 0)
1903                 connman_error("Failed to register WiFi driver");
1904 }
1905
1906 static void system_killed(void)
1907 {
1908         DBG("");
1909
1910         connman_device_driver_unregister(&wifi_ng_driver);
1911 }
1912
1913 static int network_probe(struct connman_network *network)
1914 {
1915         DBG("network %p", network);
1916
1917         return 0;
1918 }
1919
1920 static void network_remove(struct connman_network *network)
1921 {
1922         struct connman_device *device = connman_network_get_device(network);
1923         struct wifi_data *wifi;
1924
1925         DBG("network %p", network);
1926
1927         wifi = connman_device_get_data(device);
1928         if (!wifi)
1929                 return;
1930
1931         if (wifi->network != network)
1932                 return;
1933
1934         wifi->network = NULL;
1935 }
1936
1937 static void connect_callback(int result, GSupplicantInterface *interface,
1938                                                         void *user_data)
1939 {
1940         struct connman_network *network = user_data;
1941
1942         DBG("network %p result %d", network, result);
1943
1944         if (result == -ENOKEY) {
1945                 connman_network_set_error(network,
1946                                         CONNMAN_NETWORK_ERROR_INVALID_KEY);
1947         } else if (result < 0) {
1948                 connman_network_set_error(network,
1949                                         CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
1950         }
1951
1952         connman_network_unref(network);
1953 }
1954
1955 static GSupplicantSecurity network_security(const char *security)
1956 {
1957         if (g_str_equal(security, "none"))
1958                 return G_SUPPLICANT_SECURITY_NONE;
1959         else if (g_str_equal(security, "wep"))
1960                 return G_SUPPLICANT_SECURITY_WEP;
1961         else if (g_str_equal(security, "psk"))
1962                 return G_SUPPLICANT_SECURITY_PSK;
1963         else if (g_str_equal(security, "wpa"))
1964                 return G_SUPPLICANT_SECURITY_PSK;
1965         else if (g_str_equal(security, "rsn"))
1966                 return G_SUPPLICANT_SECURITY_PSK;
1967         else if (g_str_equal(security, "ieee8021x"))
1968                 return G_SUPPLICANT_SECURITY_IEEE8021X;
1969
1970         return G_SUPPLICANT_SECURITY_UNKNOWN;
1971 }
1972
1973 static void ssid_init(GSupplicantSSID *ssid, struct connman_network *network)
1974 {
1975         const char *security;
1976
1977         memset(ssid, 0, sizeof(*ssid));
1978         ssid->mode = G_SUPPLICANT_MODE_INFRA;
1979         ssid->ssid = connman_network_get_blob(network, "WiFi.SSID",
1980                                                 &ssid->ssid_len);
1981         ssid->scan_ssid = 1;
1982         security = connman_network_get_string(network, "WiFi.Security");
1983         ssid->security = network_security(security);
1984         ssid->passphrase = connman_network_get_string(network,
1985                                                 "WiFi.Passphrase");
1986
1987         ssid->eap = connman_network_get_string(network, "WiFi.EAP");
1988
1989         /*
1990          * If our private key password is unset,
1991          * we use the supplied passphrase. That is needed
1992          * for PEAP where 2 passphrases (identity and client
1993          * cert may have to be provided.
1994          */
1995         if (!connman_network_get_string(network, "WiFi.PrivateKeyPassphrase"))
1996                 connman_network_set_string(network,
1997                                                 "WiFi.PrivateKeyPassphrase",
1998                                                 ssid->passphrase);
1999         /* We must have an identity for both PEAP and TLS */
2000         ssid->identity = connman_network_get_string(network, "WiFi.Identity");
2001
2002         /* Use agent provided identity as a fallback */
2003         if (!ssid->identity || strlen(ssid->identity) == 0)
2004                 ssid->identity = connman_network_get_string(network,
2005                                                         "WiFi.AgentIdentity");
2006
2007         ssid->ca_cert_path = connman_network_get_string(network,
2008                                                         "WiFi.CACertFile");
2009         ssid->client_cert_path = connman_network_get_string(network,
2010                                                         "WiFi.ClientCertFile");
2011         ssid->private_key_path = connman_network_get_string(network,
2012                                                         "WiFi.PrivateKeyFile");
2013         ssid->private_key_passphrase = connman_network_get_string(network,
2014                                                 "WiFi.PrivateKeyPassphrase");
2015         ssid->phase2_auth = connman_network_get_string(network, "WiFi.Phase2");
2016
2017         ssid->use_wps = connman_network_get_bool(network, "WiFi.UseWPS");
2018         ssid->pin_wps = connman_network_get_string(network, "WiFi.PinWPS");
2019
2020         if (connman_setting_get_bool("BackgroundScanning"))
2021                 ssid->bgscan = BGSCAN_DEFAULT;
2022 }
2023
2024 static int network_connect(struct connman_network *network)
2025 {
2026         struct connman_device *device = connman_network_get_device(network);
2027         struct wifi_data *wifi;
2028         GSupplicantInterface *interface;
2029         GSupplicantSSID *ssid;
2030
2031         DBG("network %p", network);
2032
2033         if (!device)
2034                 return -ENODEV;
2035
2036         wifi = connman_device_get_data(device);
2037         if (!wifi)
2038                 return -ENODEV;
2039
2040         ssid = g_try_malloc0(sizeof(GSupplicantSSID));
2041         if (!ssid)
2042                 return -ENOMEM;
2043
2044         interface = wifi->interface;
2045
2046         ssid_init(ssid, network);
2047
2048         if (wifi->disconnecting) {
2049                 wifi->pending_network = network;
2050                 g_free(ssid);
2051         } else {
2052                 wifi->network = connman_network_ref(network);
2053                 wifi->retries = 0;
2054
2055                 return g_supplicant_interface_connect(interface, ssid,
2056                                                 connect_callback, network);
2057         }
2058
2059         return -EINPROGRESS;
2060 }
2061
2062 static void disconnect_callback(int result, GSupplicantInterface *interface,
2063                                                                 void *user_data)
2064 {
2065         struct wifi_data *wifi = user_data;
2066
2067         DBG("result %d supplicant interface %p wifi %p",
2068                         result, interface, wifi);
2069
2070         if (result == -ECONNABORTED) {
2071                 DBG("wifi interface no longer available");
2072                 return;
2073         }
2074
2075         if (wifi->network) {
2076                 /*
2077                  * if result < 0 supplican return an error because
2078                  * the network is not current.
2079                  * we wont receive G_SUPPLICANT_STATE_DISCONNECTED since it
2080                  * failed, call connman_network_set_connected to report
2081                  * disconnect is completed.
2082                  */
2083                 if (result < 0)
2084                         connman_network_set_connected(wifi->network, false);
2085         }
2086
2087         wifi->network = NULL;
2088
2089         wifi->disconnecting = false;
2090
2091         if (wifi->pending_network) {
2092                 network_connect(wifi->pending_network);
2093                 wifi->pending_network = NULL;
2094         }
2095
2096         start_autoscan(wifi->device);
2097 }
2098
2099 static int network_disconnect(struct connman_network *network)
2100 {
2101         struct connman_device *device = connman_network_get_device(network);
2102         struct wifi_data *wifi;
2103         int err;
2104
2105         DBG("network %p", network);
2106
2107         wifi = connman_device_get_data(device);
2108         if (!wifi || !wifi->interface)
2109                 return -ENODEV;
2110
2111         connman_network_set_associating(network, false);
2112
2113         if (wifi->disconnecting)
2114                 return -EALREADY;
2115
2116         wifi->disconnecting = true;
2117
2118         err = g_supplicant_interface_disconnect(wifi->interface,
2119                                                 disconnect_callback, wifi);
2120         if (err < 0)
2121                 wifi->disconnecting = false;
2122
2123         return err;
2124 }
2125
2126 static struct connman_network_driver network_driver = {
2127         .name           = "wifi",
2128         .type           = CONNMAN_NETWORK_TYPE_WIFI,
2129         .priority       = CONNMAN_NETWORK_PRIORITY_LOW,
2130         .probe          = network_probe,
2131         .remove         = network_remove,
2132         .connect        = network_connect,
2133         .disconnect     = network_disconnect,
2134 };
2135
2136 static void interface_added(GSupplicantInterface *interface)
2137 {
2138         const char *ifname = g_supplicant_interface_get_ifname(interface);
2139         const char *driver = g_supplicant_interface_get_driver(interface);
2140         struct wifi_data *wifi;
2141
2142         wifi = g_supplicant_interface_get_data(interface);
2143         if (!wifi) {
2144                 wifi = get_pending_wifi_data(ifname);
2145                 if (!wifi)
2146                         return;
2147
2148                 g_supplicant_interface_set_data(interface, wifi);
2149                 p2p_iface_list = g_list_append(p2p_iface_list, wifi);
2150                 wifi->p2p_device = true;
2151         }
2152
2153         DBG("ifname %s driver %s wifi %p tethering %d",
2154                         ifname, driver, wifi, wifi->tethering);
2155
2156         if (!wifi->device) {
2157                 connman_error("WiFi device not set");
2158                 return;
2159         }
2160
2161         connman_device_set_powered(wifi->device, true);
2162 }
2163
2164 static bool is_idle(struct wifi_data *wifi)
2165 {
2166         DBG("state %d", wifi->state);
2167
2168         switch (wifi->state) {
2169         case G_SUPPLICANT_STATE_UNKNOWN:
2170         case G_SUPPLICANT_STATE_DISABLED:
2171         case G_SUPPLICANT_STATE_DISCONNECTED:
2172         case G_SUPPLICANT_STATE_INACTIVE:
2173         case G_SUPPLICANT_STATE_SCANNING:
2174                 return true;
2175
2176         case G_SUPPLICANT_STATE_AUTHENTICATING:
2177         case G_SUPPLICANT_STATE_ASSOCIATING:
2178         case G_SUPPLICANT_STATE_ASSOCIATED:
2179         case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
2180         case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
2181         case G_SUPPLICANT_STATE_COMPLETED:
2182                 return false;
2183         }
2184
2185         return false;
2186 }
2187
2188 static bool is_idle_wps(GSupplicantInterface *interface,
2189                                                 struct wifi_data *wifi)
2190 {
2191         /* First, let's check if WPS processing did not went wrong */
2192         if (g_supplicant_interface_get_wps_state(interface) ==
2193                 G_SUPPLICANT_WPS_STATE_FAIL)
2194                 return false;
2195
2196         /* Unlike normal connection, being associated while processing wps
2197          * actually means that we are idling. */
2198         switch (wifi->state) {
2199         case G_SUPPLICANT_STATE_UNKNOWN:
2200         case G_SUPPLICANT_STATE_DISABLED:
2201         case G_SUPPLICANT_STATE_DISCONNECTED:
2202         case G_SUPPLICANT_STATE_INACTIVE:
2203         case G_SUPPLICANT_STATE_SCANNING:
2204         case G_SUPPLICANT_STATE_ASSOCIATED:
2205                 return true;
2206         case G_SUPPLICANT_STATE_AUTHENTICATING:
2207         case G_SUPPLICANT_STATE_ASSOCIATING:
2208         case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
2209         case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
2210         case G_SUPPLICANT_STATE_COMPLETED:
2211                 return false;
2212         }
2213
2214         return false;
2215 }
2216
2217 static bool handle_wps_completion(GSupplicantInterface *interface,
2218                                         struct connman_network *network,
2219                                         struct connman_device *device,
2220                                         struct wifi_data *wifi)
2221 {
2222         bool wps;
2223
2224         wps = connman_network_get_bool(network, "WiFi.UseWPS");
2225         if (wps) {
2226                 const unsigned char *ssid, *wps_ssid;
2227                 unsigned int ssid_len, wps_ssid_len;
2228                 const char *wps_key;
2229
2230                 /* Checking if we got associated with requested
2231                  * network */
2232                 ssid = connman_network_get_blob(network, "WiFi.SSID",
2233                                                 &ssid_len);
2234
2235                 wps_ssid = g_supplicant_interface_get_wps_ssid(
2236                         interface, &wps_ssid_len);
2237
2238                 if (!wps_ssid || wps_ssid_len != ssid_len ||
2239                                 memcmp(ssid, wps_ssid, ssid_len) != 0) {
2240                         connman_network_set_associating(network, false);
2241                         g_supplicant_interface_disconnect(wifi->interface,
2242                                                 disconnect_callback, wifi);
2243                         return false;
2244                 }
2245
2246                 wps_key = g_supplicant_interface_get_wps_key(interface);
2247                 connman_network_set_string(network, "WiFi.Passphrase",
2248                                         wps_key);
2249
2250                 connman_network_set_string(network, "WiFi.PinWPS", NULL);
2251         }
2252
2253         return true;
2254 }
2255
2256 static bool handle_4way_handshake_failure(GSupplicantInterface *interface,
2257                                         struct connman_network *network,
2258                                         struct wifi_data *wifi)
2259 {
2260         struct connman_service *service;
2261
2262         if (wifi->state != G_SUPPLICANT_STATE_4WAY_HANDSHAKE)
2263                 return false;
2264
2265         service = connman_service_lookup_from_network(network);
2266         if (!service)
2267                 return false;
2268
2269         wifi->retries++;
2270
2271         if (connman_service_get_favorite(service)) {
2272                 if (wifi->retries < FAVORITE_MAXIMUM_RETRIES)
2273                         return true;
2274         }
2275
2276         wifi->retries = 0;
2277         connman_network_set_error(network, CONNMAN_NETWORK_ERROR_INVALID_KEY);
2278
2279         return false;
2280 }
2281
2282 static void interface_state(GSupplicantInterface *interface)
2283 {
2284         struct connman_network *network;
2285         struct connman_device *device;
2286         struct wifi_data *wifi;
2287         GSupplicantState state = g_supplicant_interface_get_state(interface);
2288         bool wps;
2289
2290         wifi = g_supplicant_interface_get_data(interface);
2291
2292         DBG("wifi %p interface state %d", wifi, state);
2293
2294         if (!wifi)
2295                 return;
2296
2297         device = wifi->device;
2298         if (!device)
2299                 return;
2300
2301         if (g_supplicant_interface_get_ready(interface) &&
2302                                         !wifi->interface_ready) {
2303                 wifi->interface_ready = true;
2304                 finalize_interface_creation(wifi);
2305         }
2306
2307         network = wifi->network;
2308         if (!network)
2309                 return;
2310
2311         switch (state) {
2312         case G_SUPPLICANT_STATE_SCANNING:
2313                 break;
2314
2315         case G_SUPPLICANT_STATE_AUTHENTICATING:
2316         case G_SUPPLICANT_STATE_ASSOCIATING:
2317                 stop_autoscan(device);
2318
2319                 if (!wifi->connected)
2320                         connman_network_set_associating(network, true);
2321
2322                 break;
2323
2324         case G_SUPPLICANT_STATE_COMPLETED:
2325                 /* though it should be already stopped: */
2326                 stop_autoscan(device);
2327
2328                 if (!handle_wps_completion(interface, network, device, wifi))
2329                         break;
2330
2331                 connman_network_set_connected(network, true);
2332                 break;
2333
2334         case G_SUPPLICANT_STATE_DISCONNECTED:
2335                 /*
2336                  * If we're in one of the idle modes, we have
2337                  * not started association yet and thus setting
2338                  * those ones to FALSE could cancel an association
2339                  * in progress.
2340                  */
2341                 wps = connman_network_get_bool(network, "WiFi.UseWPS");
2342                 if (wps)
2343                         if (is_idle_wps(interface, wifi))
2344                                 break;
2345
2346                 if (is_idle(wifi))
2347                         break;
2348
2349                 /* If previous state was 4way-handshake, then
2350                  * it's either: psk was incorrect and thus we retry
2351                  * or if we reach the maximum retries we declare the
2352                  * psk as wrong */
2353                 if (handle_4way_handshake_failure(interface,
2354                                                 network, wifi))
2355                         break;
2356
2357                 /* We disable the selected network, if not then
2358                  * wpa_supplicant will loop retrying */
2359                 if (g_supplicant_interface_enable_selected_network(interface,
2360                                                 FALSE) != 0)
2361                         DBG("Could not disables selected network");
2362
2363                 connman_network_set_connected(network, false);
2364                 connman_network_set_associating(network, false);
2365                 wifi->disconnecting = false;
2366
2367                 start_autoscan(device);
2368
2369                 break;
2370
2371         case G_SUPPLICANT_STATE_INACTIVE:
2372                 connman_network_set_associating(network, false);
2373                 start_autoscan(device);
2374
2375                 break;
2376
2377         case G_SUPPLICANT_STATE_UNKNOWN:
2378         case G_SUPPLICANT_STATE_DISABLED:
2379         case G_SUPPLICANT_STATE_ASSOCIATED:
2380         case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
2381         case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
2382                 break;
2383         }
2384
2385         wifi->state = state;
2386
2387         /* Saving wpa_s state policy:
2388          * If connected and if the state changes are roaming related:
2389          * --> We stay connected
2390          * If completed
2391          * --> We are connected
2392          * All other case:
2393          * --> We are not connected
2394          * */
2395         switch (state) {
2396         case G_SUPPLICANT_STATE_AUTHENTICATING:
2397         case G_SUPPLICANT_STATE_ASSOCIATING:
2398         case G_SUPPLICANT_STATE_ASSOCIATED:
2399         case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
2400         case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
2401                 if (wifi->connected)
2402                         connman_warn("Probably roaming right now!"
2403                                                 " Staying connected...");
2404                 else
2405                         wifi->connected = false;
2406                 break;
2407         case G_SUPPLICANT_STATE_COMPLETED:
2408                 wifi->connected = true;
2409                 break;
2410         default:
2411                 wifi->connected = false;
2412                 break;
2413         }
2414
2415         DBG("DONE");
2416 }
2417
2418 static void interface_removed(GSupplicantInterface *interface)
2419 {
2420         const char *ifname = g_supplicant_interface_get_ifname(interface);
2421         struct wifi_data *wifi;
2422
2423         DBG("ifname %s", ifname);
2424
2425         wifi = g_supplicant_interface_get_data(interface);
2426
2427         if (wifi)
2428                 wifi->interface = NULL;
2429
2430         if (wifi && wifi->tethering)
2431                 return;
2432
2433         if (!wifi || !wifi->device) {
2434                 DBG("wifi interface already removed");
2435                 return;
2436         }
2437
2438         connman_device_set_powered(wifi->device, false);
2439
2440         check_p2p_technology();
2441 }
2442
2443 static void set_device_type(const char *type, char dev_type[17])
2444 {
2445         const char *oui = "0050F204";
2446         const char *category = "0100";
2447         const char *sub_category = "0000";
2448
2449         if (!g_strcmp0(type, "handset")) {
2450                 category = "0A00";
2451                 sub_category = "0500";
2452         } else if (!g_strcmp0(type, "vm") || !g_strcmp0(type, "container"))
2453                 sub_category = "0100";
2454         else if (!g_strcmp0(type, "server"))
2455                 sub_category = "0200";
2456         else if (!g_strcmp0(type, "laptop"))
2457                 sub_category = "0500";
2458         else if (!g_strcmp0(type, "desktop"))
2459                 sub_category = "0600";
2460         else if (!g_strcmp0(type, "tablet"))
2461                 sub_category = "0900";
2462         else if (!g_strcmp0(type, "watch"))
2463                 category = "FF00";
2464
2465         snprintf(dev_type, 17, "%s%s%s", category, oui, sub_category);
2466 }
2467
2468 static void p2p_support(GSupplicantInterface *interface)
2469 {
2470         char dev_type[17] = {};
2471         const char *hostname;
2472
2473         DBG("");
2474
2475         if (!g_supplicant_interface_has_p2p(interface))
2476                 return;
2477
2478         if (connman_technology_driver_register(&p2p_tech_driver) < 0) {
2479                 DBG("Could not register P2P technology driver");
2480                 return;
2481         }
2482
2483         hostname = connman_utsname_get_hostname();
2484         if (!hostname)
2485                 hostname = "ConnMan";
2486
2487         set_device_type(connman_machine_get_type(), dev_type);
2488         g_supplicant_interface_set_p2p_device_config(interface,
2489                                                         hostname, dev_type);
2490         connman_peer_driver_register(&peer_driver);
2491 }
2492
2493 static void scan_started(GSupplicantInterface *interface)
2494 {
2495         DBG("");
2496 }
2497
2498 static void scan_finished(GSupplicantInterface *interface)
2499 {
2500         DBG("");
2501 }
2502
2503 static unsigned char calculate_strength(GSupplicantNetwork *supplicant_network)
2504 {
2505         unsigned char strength;
2506
2507         strength = 120 + g_supplicant_network_get_signal(supplicant_network);
2508         if (strength > 100)
2509                 strength = 100;
2510
2511         return strength;
2512 }
2513
2514 static void network_added(GSupplicantNetwork *supplicant_network)
2515 {
2516         struct connman_network *network;
2517         GSupplicantInterface *interface;
2518         struct wifi_data *wifi;
2519         const char *name, *identifier, *security, *group, *mode;
2520         const unsigned char *ssid;
2521         unsigned int ssid_len;
2522         bool wps;
2523         bool wps_pbc;
2524         bool wps_ready;
2525         bool wps_advertizing;
2526
2527         mode = g_supplicant_network_get_mode(supplicant_network);
2528         identifier = g_supplicant_network_get_identifier(supplicant_network);
2529
2530         DBG("%s", identifier);
2531
2532         if (!g_strcmp0(mode, "adhoc"))
2533                 return;
2534
2535         interface = g_supplicant_network_get_interface(supplicant_network);
2536         wifi = g_supplicant_interface_get_data(interface);
2537         name = g_supplicant_network_get_name(supplicant_network);
2538         security = g_supplicant_network_get_security(supplicant_network);
2539         group = g_supplicant_network_get_identifier(supplicant_network);
2540         wps = g_supplicant_network_get_wps(supplicant_network);
2541         wps_pbc = g_supplicant_network_is_wps_pbc(supplicant_network);
2542         wps_ready = g_supplicant_network_is_wps_active(supplicant_network);
2543         wps_advertizing = g_supplicant_network_is_wps_advertizing(
2544                                                         supplicant_network);
2545
2546         if (!wifi)
2547                 return;
2548
2549         ssid = g_supplicant_network_get_ssid(supplicant_network, &ssid_len);
2550
2551         network = connman_device_get_network(wifi->device, identifier);
2552
2553         if (!network) {
2554                 network = connman_network_create(identifier,
2555                                                 CONNMAN_NETWORK_TYPE_WIFI);
2556                 if (!network)
2557                         return;
2558
2559                 connman_network_set_index(network, wifi->index);
2560
2561                 if (connman_device_add_network(wifi->device, network) < 0) {
2562                         connman_network_unref(network);
2563                         return;
2564                 }
2565
2566                 wifi->networks = g_slist_prepend(wifi->networks, network);
2567         }
2568
2569         if (name && name[0] != '\0')
2570                 connman_network_set_name(network, name);
2571
2572         connman_network_set_blob(network, "WiFi.SSID",
2573                                                 ssid, ssid_len);
2574         connman_network_set_string(network, "WiFi.Security", security);
2575         connman_network_set_strength(network,
2576                                 calculate_strength(supplicant_network));
2577         connman_network_set_bool(network, "WiFi.WPS", wps);
2578
2579         if (wps) {
2580                 /* Is AP advertizing for WPS association?
2581                  * If so, we decide to use WPS by default */
2582                 if (wps_ready && wps_pbc &&
2583                                                 wps_advertizing)
2584                         connman_network_set_bool(network, "WiFi.UseWPS", true);
2585         }
2586
2587         connman_network_set_frequency(network,
2588                         g_supplicant_network_get_frequency(supplicant_network));
2589
2590         connman_network_set_available(network, true);
2591         connman_network_set_string(network, "WiFi.Mode", mode);
2592
2593         if (ssid)
2594                 connman_network_set_group(network, group);
2595
2596         if (wifi->hidden && ssid) {
2597                 if (!g_strcmp0(wifi->hidden->security, security) &&
2598                                 wifi->hidden->ssid_len == ssid_len &&
2599                                 !memcmp(wifi->hidden->ssid, ssid, ssid_len)) {
2600                         connman_network_connect_hidden(network,
2601                                         wifi->hidden->identity,
2602                                         wifi->hidden->passphrase,
2603                                         wifi->hidden->user_data);
2604                         wifi->hidden->user_data = NULL;
2605                         hidden_free(wifi->hidden);
2606                         wifi->hidden = NULL;
2607                 }
2608         }
2609 }
2610
2611 static void network_removed(GSupplicantNetwork *network)
2612 {
2613         GSupplicantInterface *interface;
2614         struct wifi_data *wifi;
2615         const char *name, *identifier;
2616         struct connman_network *connman_network;
2617
2618         interface = g_supplicant_network_get_interface(network);
2619         wifi = g_supplicant_interface_get_data(interface);
2620         identifier = g_supplicant_network_get_identifier(network);
2621         name = g_supplicant_network_get_name(network);
2622
2623         DBG("name %s", name);
2624
2625         if (!wifi)
2626                 return;
2627
2628         connman_network = connman_device_get_network(wifi->device, identifier);
2629         if (!connman_network)
2630                 return;
2631
2632         wifi->networks = g_slist_remove(wifi->networks, connman_network);
2633
2634         connman_device_remove_network(wifi->device, connman_network);
2635         connman_network_unref(connman_network);
2636 }
2637
2638 static void network_changed(GSupplicantNetwork *network, const char *property)
2639 {
2640         GSupplicantInterface *interface;
2641         struct wifi_data *wifi;
2642         const char *name, *identifier;
2643         struct connman_network *connman_network;
2644
2645         interface = g_supplicant_network_get_interface(network);
2646         wifi = g_supplicant_interface_get_data(interface);
2647         identifier = g_supplicant_network_get_identifier(network);
2648         name = g_supplicant_network_get_name(network);
2649
2650         DBG("name %s", name);
2651
2652         if (!wifi)
2653                 return;
2654
2655         connman_network = connman_device_get_network(wifi->device, identifier);
2656         if (!connman_network)
2657                 return;
2658
2659         if (g_str_equal(property, "Signal")) {
2660                connman_network_set_strength(connman_network,
2661                                         calculate_strength(network));
2662                connman_network_update(connman_network);
2663         }
2664 }
2665
2666 static void apply_peer_services(GSupplicantPeer *peer,
2667                                 struct connman_peer *connman_peer)
2668 {
2669         const unsigned char *data;
2670         int length;
2671
2672         DBG("");
2673
2674         connman_peer_reset_services(connman_peer);
2675
2676         data = g_supplicant_peer_get_widi_ies(peer, &length);
2677         if (data) {
2678                 connman_peer_add_service(connman_peer,
2679                         CONNMAN_PEER_SERVICE_WIFI_DISPLAY, data, length);
2680         }
2681 }
2682
2683 static void peer_found(GSupplicantPeer *peer)
2684 {
2685         GSupplicantInterface *iface = g_supplicant_peer_get_interface(peer);
2686         struct wifi_data *wifi = g_supplicant_interface_get_data(iface);
2687         struct connman_peer *connman_peer;
2688         const char *identifier, *name;
2689         int ret;
2690
2691         identifier = g_supplicant_peer_get_identifier(peer);
2692         name = g_supplicant_peer_get_name(peer);
2693
2694         DBG("ident: %s", identifier);
2695
2696         connman_peer = connman_peer_get(wifi->device, identifier);
2697         if (connman_peer)
2698                 return;
2699
2700         connman_peer = connman_peer_create(identifier);
2701         connman_peer_set_name(connman_peer, name);
2702         connman_peer_set_device(connman_peer, wifi->device);
2703         apply_peer_services(peer, connman_peer);
2704
2705         ret = connman_peer_register(connman_peer);
2706         if (ret < 0 && ret != -EALREADY)
2707                 connman_peer_unref(connman_peer);
2708 }
2709
2710 static void peer_lost(GSupplicantPeer *peer)
2711 {
2712         GSupplicantInterface *iface = g_supplicant_peer_get_interface(peer);
2713         struct wifi_data *wifi = g_supplicant_interface_get_data(iface);
2714         struct connman_peer *connman_peer;
2715         const char *identifier;
2716
2717         if (!wifi)
2718                 return;
2719
2720         identifier = g_supplicant_peer_get_identifier(peer);
2721
2722         DBG("ident: %s", identifier);
2723
2724         connman_peer = connman_peer_get(wifi->device, identifier);
2725         if (connman_peer) {
2726                 if (wifi->p2p_connecting &&
2727                                 wifi->pending_peer == connman_peer) {
2728                         peer_connect_timeout(wifi);
2729                 }
2730                 connman_peer_unregister(connman_peer);
2731                 connman_peer_unref(connman_peer);
2732         }
2733 }
2734
2735 static void peer_changed(GSupplicantPeer *peer, GSupplicantPeerState state)
2736 {
2737         GSupplicantInterface *iface = g_supplicant_peer_get_interface(peer);
2738         struct wifi_data *wifi = g_supplicant_interface_get_data(iface);
2739         enum connman_peer_state p_state = CONNMAN_PEER_STATE_UNKNOWN;
2740         struct connman_peer *connman_peer;
2741         const char *identifier;
2742
2743         identifier = g_supplicant_peer_get_identifier(peer);
2744
2745         DBG("ident: %s", identifier);
2746
2747         connman_peer = connman_peer_get(wifi->device, identifier);
2748         if (!connman_peer)
2749                 return;
2750
2751         switch (state) {
2752         case G_SUPPLICANT_PEER_SERVICES_CHANGED:
2753                 apply_peer_services(peer, connman_peer);
2754                 connman_peer_services_changed(connman_peer);
2755                 return;
2756         case G_SUPPLICANT_PEER_GROUP_CHANGED:
2757                 if (!g_supplicant_peer_is_in_a_group(peer))
2758                         p_state = CONNMAN_PEER_STATE_IDLE;
2759                 else
2760                         p_state = CONNMAN_PEER_STATE_CONFIGURATION;
2761                 break;
2762         case G_SUPPLICANT_PEER_GROUP_STARTED:
2763                 break;
2764         case G_SUPPLICANT_PEER_GROUP_FINISHED:
2765                 p_state = CONNMAN_PEER_STATE_IDLE;
2766                 break;
2767         case G_SUPPLICANT_PEER_GROUP_JOINED:
2768                 connman_peer_set_iface_address(connman_peer,
2769                                 g_supplicant_peer_get_iface_address(peer));
2770                 break;
2771         case G_SUPPLICANT_PEER_GROUP_DISCONNECTED:
2772                 p_state = CONNMAN_PEER_STATE_IDLE;
2773                 break;
2774         case G_SUPPLICANT_PEER_GROUP_FAILED:
2775                 if (g_supplicant_peer_has_requested_connection(peer))
2776                         p_state = CONNMAN_PEER_STATE_IDLE;
2777                 else
2778                         p_state = CONNMAN_PEER_STATE_FAILURE;
2779                 break;
2780         }
2781
2782         if (p_state == CONNMAN_PEER_STATE_CONFIGURATION ||
2783                                         p_state == CONNMAN_PEER_STATE_FAILURE) {
2784                 if (wifi->p2p_connecting
2785                                 && connman_peer == wifi->pending_peer)
2786                         peer_cancel_timeout(wifi);
2787                 else
2788                         p_state = CONNMAN_PEER_STATE_UNKNOWN;
2789         }
2790
2791         if (p_state == CONNMAN_PEER_STATE_UNKNOWN)
2792                 return;
2793
2794         if (p_state == CONNMAN_PEER_STATE_CONFIGURATION) {
2795                 GSupplicantInterface *g_iface;
2796                 struct wifi_data *g_wifi;
2797
2798                 g_iface = g_supplicant_peer_get_group_interface(peer);
2799                 if (!g_iface)
2800                         return;
2801
2802                 g_wifi = g_supplicant_interface_get_data(g_iface);
2803                 if (!g_wifi)
2804                         return;
2805
2806                 connman_peer_set_as_master(connman_peer,
2807                                         !g_supplicant_peer_is_client(peer));
2808                 connman_peer_set_sub_device(connman_peer, g_wifi->device);
2809         }
2810
2811         connman_peer_set_state(connman_peer, p_state);
2812 }
2813
2814 static void peer_request(GSupplicantPeer *peer)
2815 {
2816         GSupplicantInterface *iface = g_supplicant_peer_get_interface(peer);
2817         struct wifi_data *wifi = g_supplicant_interface_get_data(iface);
2818         struct connman_peer *connman_peer;
2819         const char *identifier;
2820
2821         identifier = g_supplicant_peer_get_identifier(peer);
2822
2823         DBG("ident: %s", identifier);
2824
2825         connman_peer = connman_peer_get(wifi->device, identifier);
2826         if (!connman_peer)
2827                 return;
2828
2829         connman_peer_request_connection(connman_peer);
2830 }
2831
2832 static void debug(const char *str)
2833 {
2834         if (getenv("CONNMAN_SUPPLICANT_DEBUG"))
2835                 connman_debug("%s", str);
2836 }
2837
2838 static const GSupplicantCallbacks callbacks = {
2839         .system_ready           = system_ready,
2840         .system_killed          = system_killed,
2841         .interface_added        = interface_added,
2842         .interface_state        = interface_state,
2843         .interface_removed      = interface_removed,
2844         .p2p_support            = p2p_support,
2845         .scan_started           = scan_started,
2846         .scan_finished          = scan_finished,
2847         .network_added          = network_added,
2848         .network_removed        = network_removed,
2849         .network_changed        = network_changed,
2850         .peer_found             = peer_found,
2851         .peer_lost              = peer_lost,
2852         .peer_changed           = peer_changed,
2853         .peer_request           = peer_request,
2854         .debug                  = debug,
2855 };
2856
2857
2858 static int tech_probe(struct connman_technology *technology)
2859 {
2860         wifi_technology = technology;
2861
2862         return 0;
2863 }
2864
2865 static void tech_remove(struct connman_technology *technology)
2866 {
2867         wifi_technology = NULL;
2868 }
2869
2870 struct wifi_tethering_info {
2871         struct wifi_data *wifi;
2872         struct connman_technology *technology;
2873         char *ifname;
2874         GSupplicantSSID *ssid;
2875 };
2876
2877 static GSupplicantSSID *ssid_ap_init(const char *ssid, const char *passphrase)
2878 {
2879         GSupplicantSSID *ap;
2880
2881         ap = g_try_malloc0(sizeof(GSupplicantSSID));
2882         if (!ap)
2883                 return NULL;
2884
2885         ap->mode = G_SUPPLICANT_MODE_MASTER;
2886         ap->ssid = ssid;
2887         ap->ssid_len = strlen(ssid);
2888         ap->scan_ssid = 0;
2889         ap->freq = 2412;
2890
2891         if (!passphrase || strlen(passphrase) == 0) {
2892                 ap->security = G_SUPPLICANT_SECURITY_NONE;
2893                 ap->passphrase = NULL;
2894         } else {
2895                ap->security = G_SUPPLICANT_SECURITY_PSK;
2896                ap->protocol = G_SUPPLICANT_PROTO_RSN;
2897                ap->pairwise_cipher = G_SUPPLICANT_PAIRWISE_CCMP;
2898                ap->group_cipher = G_SUPPLICANT_GROUP_CCMP;
2899                ap->passphrase = passphrase;
2900         }
2901
2902         return ap;
2903 }
2904
2905 static void ap_start_callback(int result, GSupplicantInterface *interface,
2906                                                         void *user_data)
2907 {
2908         struct wifi_tethering_info *info = user_data;
2909
2910         DBG("result %d index %d bridge %s",
2911                 result, info->wifi->index, info->wifi->bridge);
2912
2913         if (result < 0) {
2914                 connman_inet_remove_from_bridge(info->wifi->index,
2915                                                         info->wifi->bridge);
2916                 connman_technology_tethering_notify(info->technology, false);
2917         }
2918
2919         g_free(info->ifname);
2920         g_free(info);
2921 }
2922
2923 static void ap_create_callback(int result,
2924                                 GSupplicantInterface *interface,
2925                                         void *user_data)
2926 {
2927         struct wifi_tethering_info *info = user_data;
2928
2929         DBG("result %d ifname %s", result,
2930                                 g_supplicant_interface_get_ifname(interface));
2931
2932         if (result < 0) {
2933                 connman_inet_remove_from_bridge(info->wifi->index,
2934                                                         info->wifi->bridge);
2935                 connman_technology_tethering_notify(info->technology, false);
2936
2937                 g_free(info->ifname);
2938                 g_free(info->ssid);
2939                 g_free(info);
2940                 return;
2941         }
2942
2943         info->wifi->interface = interface;
2944         g_supplicant_interface_set_data(interface, info->wifi);
2945
2946         if (g_supplicant_interface_set_apscan(interface, 2) < 0)
2947                 connman_error("Failed to set interface ap_scan property");
2948
2949         g_supplicant_interface_connect(interface, info->ssid,
2950                                                 ap_start_callback, info);
2951 }
2952
2953 static void sta_remove_callback(int result,
2954                                 GSupplicantInterface *interface,
2955                                         void *user_data)
2956 {
2957         struct wifi_tethering_info *info = user_data;
2958         const char *driver = connman_option_get_string("wifi");
2959
2960         DBG("ifname %s result %d ", info->ifname, result);
2961
2962         if (result < 0) {
2963                 info->wifi->tethering = true;
2964
2965                 g_free(info->ifname);
2966                 g_free(info->ssid);
2967                 g_free(info);
2968                 return;
2969         }
2970
2971         info->wifi->interface = NULL;
2972
2973         connman_technology_tethering_notify(info->technology, true);
2974
2975         g_supplicant_interface_create(info->ifname, driver, info->wifi->bridge,
2976                                                 ap_create_callback,
2977                                                         info);
2978 }
2979
2980 static int tech_set_tethering(struct connman_technology *technology,
2981                                 const char *identifier, const char *passphrase,
2982                                 const char *bridge, bool enabled)
2983 {
2984         GList *list;
2985         GSupplicantInterface *interface;
2986         struct wifi_data *wifi;
2987         struct wifi_tethering_info *info;
2988         const char *ifname;
2989         unsigned int mode;
2990         int err;
2991
2992         DBG("");
2993
2994         if (!enabled) {
2995                 for (list = iface_list; list; list = list->next) {
2996                         wifi = list->data;
2997
2998                         if (wifi->tethering) {
2999                                 wifi->tethering = false;
3000
3001                                 connman_inet_remove_from_bridge(wifi->index,
3002                                                                         bridge);
3003                                 wifi->bridged = false;
3004                         }
3005                 }
3006
3007                 connman_technology_tethering_notify(technology, false);
3008
3009                 return 0;
3010         }
3011
3012         for (list = iface_list; list; list = list->next) {
3013                 wifi = list->data;
3014
3015                 interface = wifi->interface;
3016
3017                 if (!interface)
3018                         continue;
3019
3020                 ifname = g_supplicant_interface_get_ifname(wifi->interface);
3021
3022                 mode = g_supplicant_interface_get_mode(interface);
3023                 if ((mode & G_SUPPLICANT_CAPABILITY_MODE_AP) == 0) {
3024                         DBG("%s does not support AP mode", ifname);
3025                         continue;
3026                 }
3027
3028                 info = g_try_malloc0(sizeof(struct wifi_tethering_info));
3029                 if (!info)
3030                         return -ENOMEM;
3031
3032                 info->wifi = wifi;
3033                 info->technology = technology;
3034                 info->wifi->bridge = bridge;
3035                 info->ssid = ssid_ap_init(identifier, passphrase);
3036                 if (!info->ssid) {
3037                         g_free(info);
3038                         continue;
3039                 }
3040                 info->ifname = g_strdup(ifname);
3041                 if (!info->ifname) {
3042                         g_free(info->ssid);
3043                         g_free(info);
3044                         continue;
3045                 }
3046
3047                 info->wifi->tethering = true;
3048
3049                 err = g_supplicant_interface_remove(interface,
3050                                                 sta_remove_callback,
3051                                                         info);
3052                 if (err == 0)
3053                         return err;
3054         }
3055
3056         return -EOPNOTSUPP;
3057 }
3058
3059 static void regdom_callback(int result, const char *alpha2, void *user_data)
3060 {
3061         DBG("");
3062
3063         if (!wifi_technology)
3064                 return;
3065
3066         if (result != 0)
3067                 alpha2 = NULL;
3068
3069         connman_technology_regdom_notify(wifi_technology, alpha2);
3070 }
3071
3072 static int tech_set_regdom(struct connman_technology *technology, const char *alpha2)
3073 {
3074         return g_supplicant_set_country(alpha2, regdom_callback, NULL);
3075 }
3076
3077 static struct connman_technology_driver tech_driver = {
3078         .name           = "wifi",
3079         .type           = CONNMAN_SERVICE_TYPE_WIFI,
3080         .probe          = tech_probe,
3081         .remove         = tech_remove,
3082         .set_tethering  = tech_set_tethering,
3083         .set_regdom     = tech_set_regdom,
3084 };
3085
3086 static int wifi_init(void)
3087 {
3088         int err;
3089
3090         err = connman_network_driver_register(&network_driver);
3091         if (err < 0)
3092                 return err;
3093
3094         err = g_supplicant_register(&callbacks);
3095         if (err < 0) {
3096                 connman_network_driver_unregister(&network_driver);
3097                 return err;
3098         }
3099
3100         err = connman_technology_driver_register(&tech_driver);
3101         if (err < 0) {
3102                 g_supplicant_unregister(&callbacks);
3103                 connman_network_driver_unregister(&network_driver);
3104                 return err;
3105         }
3106
3107         return 0;
3108 }
3109
3110 static void wifi_exit(void)
3111 {
3112         DBG();
3113
3114         connman_technology_driver_unregister(&tech_driver);
3115
3116         g_supplicant_unregister(&callbacks);
3117
3118         connman_network_driver_unregister(&network_driver);
3119 }
3120
3121 CONNMAN_PLUGIN_DEFINE(wifi, "WiFi interface plugin", VERSION,
3122                 CONNMAN_PLUGIN_PRIORITY_DEFAULT, wifi_init, wifi_exit)