wifi: Ignore duplicate hidden APs
[platform/upstream/connman.git] / plugins / wifi.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2012  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/log.h>
52 #include <connman/option.h>
53 #include <connman/storage.h>
54 #include <include/setting.h>
55 #include <connman/provision.h>
56
57 #include <gsupplicant/gsupplicant.h>
58
59 #define CLEANUP_TIMEOUT   8     /* in seconds */
60 #define INACTIVE_TIMEOUT  12    /* in seconds */
61 #define MAXIMUM_RETRIES   2
62 #define FAVORITE_MAXIMUM_RETRIES 4
63
64 #define BGSCAN_DEFAULT "simple:30:-45:300"
65 #define AUTOSCAN_DEFAULT "exponential:3:300"
66
67 static struct connman_technology *wifi_technology = NULL;
68
69 struct hidden_params {
70         char ssid[32];
71         unsigned int ssid_len;
72         char *identity;
73         char *passphrase;
74         gpointer user_data;
75 };
76
77 /**
78  * Used for autoscan "emulation".
79  * Should be removed when wpa_s autoscan support will be by default.
80  */
81 struct autoscan_params {
82         int base;
83         int limit;
84         int interval;
85         unsigned int timeout;
86 };
87
88 struct wifi_data {
89         char *identifier;
90         struct connman_device *device;
91         struct connman_network *network;
92         struct connman_network *pending_network;
93         GSList *networks;
94         GSupplicantInterface *interface;
95         GSupplicantState state;
96         connman_bool_t connected;
97         connman_bool_t disconnecting;
98         connman_bool_t tethering;
99         connman_bool_t bridged;
100         const char *bridge;
101         int index;
102         unsigned flags;
103         unsigned int watch;
104         int retries;
105         struct hidden_params *hidden;
106         /**
107          * autoscan "emulation".
108          */
109         struct autoscan_params *autoscan;
110 };
111
112 static GList *iface_list = NULL;
113
114 static void start_autoscan(struct connman_device *device);
115
116 static void handle_tethering(struct wifi_data *wifi)
117 {
118         if (wifi->tethering == FALSE)
119                 return;
120
121         if (wifi->bridge == NULL)
122                 return;
123
124         if (wifi->bridged == TRUE)
125                 return;
126
127         DBG("index %d bridge %s", wifi->index, wifi->bridge);
128
129         if (connman_inet_add_to_bridge(wifi->index, wifi->bridge) < 0)
130                 return;
131
132         wifi->bridged = TRUE;
133 }
134
135 static void wifi_newlink(unsigned flags, unsigned change, void *user_data)
136 {
137         struct connman_device *device = user_data;
138         struct wifi_data *wifi = connman_device_get_data(device);
139
140         if (wifi == NULL)
141                 return;
142
143         DBG("index %d flags %d change %d", wifi->index, flags, change);
144
145         if (!change)
146                 return;
147
148         if ((wifi->flags & IFF_UP) != (flags & IFF_UP)) {
149                 if (flags & IFF_UP)
150                         DBG("interface up");
151                 else
152                         DBG("interface down");
153         }
154
155         if ((wifi->flags & IFF_LOWER_UP) != (flags & IFF_LOWER_UP)) {
156                 if (flags & IFF_LOWER_UP) {
157                         DBG("carrier on");
158
159                         handle_tethering(wifi);
160                 } else
161                         DBG("carrier off");
162         }
163
164         wifi->flags = flags;
165 }
166
167 static int wifi_probe(struct connman_device *device)
168 {
169         struct wifi_data *wifi;
170
171         DBG("device %p", device);
172
173         wifi = g_try_new0(struct wifi_data, 1);
174         if (wifi == NULL)
175                 return -ENOMEM;
176
177         wifi->connected = FALSE;
178         wifi->disconnecting = FALSE;
179         wifi->tethering = FALSE;
180         wifi->bridged = FALSE;
181         wifi->bridge = NULL;
182         wifi->state = G_SUPPLICANT_STATE_INACTIVE;
183
184         connman_device_set_data(device, wifi);
185         wifi->device = connman_device_ref(device);
186
187         wifi->index = connman_device_get_index(device);
188         wifi->flags = 0;
189
190         wifi->watch = connman_rtnl_add_newlink_watch(wifi->index,
191                                                         wifi_newlink, device);
192
193         iface_list = g_list_append(iface_list, wifi);
194
195         return 0;
196 }
197
198 static void remove_networks(struct connman_device *device,
199                                 struct wifi_data *wifi)
200 {
201         GSList *list;
202
203         for (list = wifi->networks; list != NULL; list = list->next) {
204                 struct connman_network *network = list->data;
205
206                 connman_device_remove_network(device, network);
207                 connman_network_unref(network);
208         }
209
210         g_slist_free(wifi->networks);
211         wifi->networks = NULL;
212 }
213
214 static void reset_autoscan(struct connman_device *device)
215 {
216         struct wifi_data *wifi = connman_device_get_data(device);
217         struct autoscan_params *autoscan;
218
219         DBG("");
220
221         if (wifi == NULL || wifi->autoscan == NULL)
222                 return;
223
224         autoscan = wifi->autoscan;
225
226         if (autoscan->timeout == 0 && autoscan->interval == 0)
227                 return;
228
229         g_source_remove(autoscan->timeout);
230
231         autoscan->timeout = 0;
232         autoscan->interval = 0;
233
234         connman_device_unref(device);
235 }
236
237 static void stop_autoscan(struct connman_device *device)
238 {
239         reset_autoscan(device);
240
241         connman_device_set_scanning(device, FALSE);
242 }
243
244 static void wifi_remove(struct connman_device *device)
245 {
246         struct wifi_data *wifi = connman_device_get_data(device);
247
248         DBG("device %p wifi %p", device, wifi);
249
250         if (wifi == NULL)
251                 return;
252
253         iface_list = g_list_remove(iface_list, wifi);
254
255         remove_networks(device, wifi);
256
257         connman_device_set_powered(device, FALSE);
258         connman_device_set_data(device, NULL);
259         connman_device_unref(wifi->device);
260         connman_rtnl_remove_watch(wifi->watch);
261
262         g_supplicant_interface_set_data(wifi->interface, NULL);
263
264         g_free(wifi->autoscan);
265         g_free(wifi->identifier);
266         g_free(wifi);
267 }
268
269 static gboolean is_duplicate(GSList *list, gchar *ssid, int ssid_len)
270 {
271         GSList *iter;
272
273         for (iter = list; iter != NULL; iter = g_slist_next(iter)) {
274                 struct scan_ssid *scan_ssid = iter->data;
275
276                 if (ssid_len == scan_ssid->ssid_len &&
277                                 memcmp(ssid, scan_ssid->ssid, ssid_len) == 0)
278                         return TRUE;
279         }
280
281         return FALSE;
282 }
283
284 static int add_scan_param(gchar *hex_ssid, char *raw_ssid, int ssid_len,
285                         int freq, GSupplicantScanParams *scan_data,
286                         int driver_max_scan_ssids, char *ssid_name)
287 {
288         unsigned int i;
289         struct scan_ssid *scan_ssid;
290
291         if (driver_max_scan_ssids > scan_data->num_ssids &&
292                         (hex_ssid != NULL || raw_ssid != NULL)) {
293                 gchar *ssid;
294                 unsigned int j = 0, hex;
295
296                 if (hex_ssid != NULL) {
297                         size_t hex_ssid_len = strlen(hex_ssid);
298
299                         ssid = g_try_malloc0(hex_ssid_len / 2);
300                         if (ssid == NULL)
301                                 return -ENOMEM;
302
303                         for (i = 0; i < hex_ssid_len; i += 2) {
304                                 sscanf(hex_ssid + i, "%02x", &hex);
305                                 ssid[j++] = hex;
306                         }
307                 } else {
308                         ssid = raw_ssid;
309                         j = ssid_len;
310                 }
311
312                 /*
313                  * If we have already added hidden AP to the list,
314                  * then do not do it again. This might happen if you have
315                  * used or are using multiple wifi cards, so in that case
316                  * you might have multiple service files for same AP.
317                  */
318                 if (is_duplicate(scan_data->ssids, ssid, j) == TRUE)
319                         return 0;
320
321                 scan_ssid = g_try_new(struct scan_ssid, 1);
322                 if (scan_ssid == NULL) {
323                         g_free(ssid);
324                         return -ENOMEM;
325                 }
326
327                 memcpy(scan_ssid->ssid, ssid, j);
328                 scan_ssid->ssid_len = j;
329                 scan_data->ssids = g_slist_prepend(scan_data->ssids,
330                                                                 scan_ssid);
331
332                 scan_data->num_ssids++;
333
334                 DBG("SSID %s added to scanned list of %d entries", ssid_name,
335                                                         scan_data->num_ssids);
336
337                 if (hex_ssid != NULL)
338                         g_free(ssid);
339         } else
340                 return -EINVAL;
341
342         scan_data->ssids = g_slist_reverse(scan_data->ssids);
343
344         if (scan_data->freqs == NULL) {
345                 scan_data->freqs = g_try_malloc0(sizeof(uint16_t) *
346                                                 scan_data->num_ssids);
347                 if (scan_data->freqs == NULL) {
348                         g_slist_free_full(scan_data->ssids, g_free);
349                         return -ENOMEM;
350                 }
351         } else {
352                 scan_data->freqs = g_try_realloc(scan_data->freqs,
353                                 sizeof(uint16_t) * scan_data->num_ssids);
354                 if (scan_data->freqs == NULL) {
355                         g_slist_free_full(scan_data->ssids, g_free);
356                         return -ENOMEM;
357                 }
358                 scan_data->freqs[scan_data->num_ssids - 1] = 0;
359         }
360
361         /* Don't add duplicate entries */
362         for (i = 0; i < scan_data->num_ssids; i++) {
363                 if (scan_data->freqs[i] == 0) {
364                         scan_data->freqs[i] = freq;
365                         break;
366                 } else if (scan_data->freqs[i] == freq)
367                         break;
368         }
369
370         return 1;
371 }
372
373 static int get_hidden_connections(int max_ssids,
374                                 GSupplicantScanParams *scan_data)
375 {
376         struct connman_config_entry **entries;
377         GKeyFile *keyfile;
378         gchar **services;
379         char *ssid, *name;
380         int i, freq, ret;
381         gboolean value;
382         int num_ssids = 0, add_param_failed = 0;
383
384         services = connman_storage_get_services();
385         for (i = 0; services && services[i]; i++) {
386                 if (strncmp(services[i], "wifi_", 5) != 0)
387                         continue;
388
389                 keyfile = connman_storage_load_service(services[i]);
390
391                 value = g_key_file_get_boolean(keyfile,
392                                         services[i], "Hidden", NULL);
393                 if (value == FALSE) {
394                         g_key_file_free(keyfile);
395                         continue;
396                 }
397
398                 value = g_key_file_get_boolean(keyfile,
399                                         services[i], "Favorite", NULL);
400                 if (value == FALSE) {
401                         g_key_file_free(keyfile);
402                         continue;
403                 }
404
405                 ssid = g_key_file_get_string(keyfile,
406                                         services[i], "SSID", NULL);
407
408                 freq = g_key_file_get_integer(keyfile, services[i],
409                                         "Frequency", NULL);
410
411                 name = g_key_file_get_string(keyfile, services[i], "Name",
412                                                                 NULL);
413
414                 ret = add_scan_param(ssid, NULL, 0, freq, scan_data,
415                                 max_ssids, name);
416                 if (ret < 0)
417                         add_param_failed++;
418                 else if (ret > 0)
419                         num_ssids++;
420
421                 g_free(name);
422                 g_key_file_free(keyfile);
423         }
424
425         /*
426          * Check if there are any hidden AP that needs to be provisioned.
427          */
428         entries = connman_config_get_entries();
429         for (i = 0; entries && entries[i]; i++) {
430                 int len;
431
432                 if (entries[i]->hidden == FALSE)
433                         continue;
434
435                 if (entries[i]->ssid == NULL) {
436                         ssid = entries[i]->name;
437                         len = strlen(ssid);
438                 } else {
439                         ssid = entries[i]->ssid;
440                         len = entries[i]->ssid_len;
441                 }
442
443                 if (ssid == NULL)
444                         continue;
445
446                 ret = add_scan_param(NULL, ssid, len, 0, scan_data,
447                                                         max_ssids, ssid);
448                 if (ret < 0)
449                         add_param_failed++;
450                 else if (ret > 0)
451                         num_ssids++;
452         }
453
454         connman_config_free_entries(entries);
455
456         if (add_param_failed > 0)
457                 DBG("Unable to scan %d out of %d SSIDs (max is %d)",
458                         add_param_failed, num_ssids, max_ssids);
459
460         g_strfreev(services);
461
462         return num_ssids > max_ssids ? max_ssids : num_ssids;
463 }
464
465 static int throw_wifi_scan(struct connman_device *device,
466                         GSupplicantInterfaceCallback callback)
467 {
468         struct wifi_data *wifi = connman_device_get_data(device);
469         int ret;
470
471         if (wifi == NULL)
472                 return -ENODEV;
473
474         DBG("device %p %p", device, wifi->interface);
475
476         if (wifi->tethering == TRUE)
477                 return -EBUSY;
478
479         if (connman_device_get_scanning(device) == TRUE)
480                 return -EALREADY;
481
482         connman_device_ref(device);
483
484         ret = g_supplicant_interface_scan(wifi->interface, NULL,
485                                                 callback, device);
486         if (ret == 0)
487                 connman_device_set_scanning(device, TRUE);
488         else
489                 connman_device_unref(device);
490
491         return ret;
492 }
493
494 static void hidden_free(struct hidden_params *hidden)
495 {
496         if (hidden == NULL)
497                 return;
498
499         g_free(hidden->identity);
500         g_free(hidden->passphrase);
501         g_free(hidden);
502 }
503
504 static void scan_callback(int result, GSupplicantInterface *interface,
505                                                 void *user_data)
506 {
507         struct connman_device *device = user_data;
508         struct wifi_data *wifi = connman_device_get_data(device);
509
510         DBG("result %d wifi %p", result, wifi);
511
512         if (wifi != NULL && wifi->hidden != NULL) {
513                 connman_network_clear_hidden(wifi->hidden->user_data);
514                 hidden_free(wifi->hidden);
515                 wifi->hidden = NULL;
516         }
517
518         if (result < 0)
519                 connman_device_reset_scanning(device);
520
521         connman_device_set_scanning(device, FALSE);
522         start_autoscan(device);
523         connman_device_unref(device);
524 }
525
526 static void scan_callback_hidden(int result,
527                         GSupplicantInterface *interface, void *user_data)
528 {
529         struct connman_device *device = user_data;
530         struct wifi_data *wifi = connman_device_get_data(device);
531         int driver_max_ssids;
532
533         DBG("result %d wifi %p", result, wifi);
534
535         if (wifi == NULL)
536                 goto out;
537
538         /*
539          * Scan hidden networks so that we can autoconnect to them.
540          */
541         driver_max_ssids = g_supplicant_interface_get_max_scan_ssids(
542                                                         wifi->interface);
543         DBG("max ssids %d", driver_max_ssids);
544
545         if (driver_max_ssids > 0) {
546                 GSupplicantScanParams *scan_params;
547                 int ret;
548
549                 scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
550                 if (scan_params == NULL)
551                         goto out;
552
553                 if (get_hidden_connections(driver_max_ssids,
554                                                 scan_params) > 0) {
555                         ret = g_supplicant_interface_scan(wifi->interface,
556                                                         scan_params,
557                                                         scan_callback,
558                                                         device);
559                         if (ret == 0)
560                                 return;
561                 }
562
563                 g_supplicant_free_scan_params(scan_params);
564         }
565
566 out:
567         scan_callback(result, interface, user_data);
568 }
569
570 static gboolean autoscan_timeout(gpointer data)
571 {
572         struct connman_device *device = data;
573         struct wifi_data *wifi = connman_device_get_data(device);
574         struct autoscan_params *autoscan;
575         int interval;
576
577         if (wifi == NULL)
578                 return FALSE;
579
580         autoscan = wifi->autoscan;
581
582         if (autoscan->interval <= 0) {
583                 interval = autoscan->base;
584                 goto set_interval;
585         } else
586                 interval = autoscan->interval * autoscan->base;
587
588         if (autoscan->interval >= autoscan->limit)
589                 interval = autoscan->limit;
590
591         throw_wifi_scan(wifi->device, scan_callback_hidden);
592
593 set_interval:
594         DBG("interval %d", interval);
595
596         autoscan->interval = interval;
597
598         autoscan->timeout = g_timeout_add_seconds(interval,
599                                                 autoscan_timeout, device);
600
601         return FALSE;
602 }
603
604 static void start_autoscan(struct connman_device *device)
605 {
606         struct wifi_data *wifi = connman_device_get_data(device);
607         struct autoscan_params *autoscan;
608
609         DBG("");
610
611         if (wifi == NULL)
612                 return;
613
614         autoscan = wifi->autoscan;
615         if (autoscan == NULL)
616                 return;
617
618         if (autoscan->timeout > 0 || autoscan->interval > 0)
619                 return;
620
621         connman_device_ref(device);
622
623         autoscan_timeout(device);
624 }
625
626 static struct autoscan_params *parse_autoscan_params(const char *params)
627 {
628         struct autoscan_params *autoscan;
629         char **list_params;
630         int limit;
631         int base;
632
633         DBG("Emulating autoscan");
634
635         list_params = g_strsplit(params, ":", 0);
636         if (list_params == 0)
637                 return NULL;
638
639         if (g_strv_length(list_params) < 3) {
640                 g_strfreev(list_params);
641                 return NULL;
642         }
643
644         base = atoi(list_params[1]);
645         limit = atoi(list_params[2]);
646
647         g_strfreev(list_params);
648
649         autoscan = g_try_malloc0(sizeof(struct autoscan_params));
650         if (autoscan == NULL) {
651                 DBG("Could not allocate memory for autoscan");
652                 return NULL;
653         }
654
655         DBG("base %d - limit %d", base, limit);
656         autoscan->base = base;
657         autoscan->limit = limit;
658
659         return autoscan;
660 }
661
662 static void setup_autoscan(struct wifi_data *wifi)
663 {
664         if (wifi->autoscan == NULL)
665                 wifi->autoscan = parse_autoscan_params(AUTOSCAN_DEFAULT);
666
667         start_autoscan(wifi->device);
668 }
669
670 static void interface_autoscan_callback(int result,
671                                         GSupplicantInterface *interface,
672                                                         void *user_data)
673 {
674         struct wifi_data *wifi = user_data;
675
676         if (result < 0) {
677                 DBG("Could not enable Autoscan, falling back...");
678                 setup_autoscan(wifi);
679         }
680 }
681
682 static void interface_create_callback(int result,
683                                         GSupplicantInterface *interface,
684                                                         void *user_data)
685 {
686         struct wifi_data *wifi = user_data;
687
688         DBG("result %d ifname %s, wifi %p", result,
689                                 g_supplicant_interface_get_ifname(interface),
690                                 wifi);
691
692         if (result < 0 || wifi == NULL)
693                 return;
694
695         wifi->interface = interface;
696         g_supplicant_interface_set_data(interface, wifi);
697
698         if (g_supplicant_interface_get_ready(interface) == FALSE)
699                 return;
700
701         DBG("interface is ready wifi %p tethering %d", wifi, wifi->tethering);
702
703         if (wifi->device == NULL) {
704                 connman_error("WiFi device not set");
705                 return;
706         }
707
708         connman_device_set_powered(wifi->device, TRUE);
709
710         if (connman_setting_get_bool("BackgroundScanning") == FALSE)
711                 return;
712
713         /* Setting up automatic scanning */
714         if (g_supplicant_interface_autoscan(interface, AUTOSCAN_DEFAULT,
715                                 interface_autoscan_callback, wifi) < 0) {
716                 DBG("Could not enable Autoscan, falling back...");
717                 setup_autoscan(wifi);
718         }
719 }
720
721 static int wifi_enable(struct connman_device *device)
722 {
723         struct wifi_data *wifi = connman_device_get_data(device);
724         const char *interface = connman_device_get_string(device, "Interface");
725         const char *driver = connman_option_get_string("wifi");
726         int ret;
727
728         DBG("device %p %p", device, wifi);
729
730         if (wifi == NULL)
731                 return -ENODEV;
732
733         ret = g_supplicant_interface_create(interface, driver, NULL,
734                                                 interface_create_callback,
735                                                         wifi);
736         if (ret < 0)
737                 return ret;
738
739         return -EINPROGRESS;
740 }
741
742 static int wifi_disable(struct connman_device *device)
743 {
744         struct wifi_data *wifi = connman_device_get_data(device);
745         int ret;
746
747         DBG("device %p wifi %p", device, wifi);
748
749         if (wifi == NULL)
750                 return -ENODEV;
751
752         wifi->connected = FALSE;
753         wifi->disconnecting = FALSE;
754
755         if (wifi->pending_network != NULL)
756                 wifi->pending_network = NULL;
757
758         stop_autoscan(device);
759
760         /* In case of a user scan, device is still referenced */
761         if (connman_device_get_scanning(device) == TRUE) {
762                 connman_device_set_scanning(device, FALSE);
763                 connman_device_unref(wifi->device);
764         }
765
766         remove_networks(device, wifi);
767
768         ret = g_supplicant_interface_remove(wifi->interface, NULL, NULL);
769         if (ret < 0)
770                 return ret;
771
772         return -EINPROGRESS;
773 }
774
775 struct last_connected {
776         GTimeVal modified;
777         gchar *ssid;
778         int freq;
779 };
780
781 static gint sort_entry(gconstpointer a, gconstpointer b, gpointer user_data)
782 {
783         GTimeVal *aval = (GTimeVal *)a;
784         GTimeVal *bval = (GTimeVal *)b;
785
786         /* Note that the sort order is descending */
787         if (aval->tv_sec < bval->tv_sec)
788                 return 1;
789
790         if (aval->tv_sec > bval->tv_sec)
791                 return -1;
792
793         return 0;
794 }
795
796 static void free_entry(gpointer data)
797 {
798         struct last_connected *entry = data;
799
800         g_free(entry->ssid);
801         g_free(entry);
802 }
803
804 static int get_latest_connections(int max_ssids,
805                                 GSupplicantScanParams *scan_data)
806 {
807         GSequenceIter *iter;
808         GSequence *latest_list;
809         struct last_connected *entry;
810         GKeyFile *keyfile;
811         GTimeVal modified;
812         gchar **services;
813         gchar *str;
814         char *ssid;
815         int i, freq;
816         int num_ssids = 0;
817
818         latest_list = g_sequence_new(free_entry);
819         if (latest_list == NULL)
820                 return -ENOMEM;
821
822         services = connman_storage_get_services();
823         for (i = 0; services && services[i]; i++) {
824                 if (strncmp(services[i], "wifi_", 5) != 0)
825                         continue;
826
827                 keyfile = connman_storage_load_service(services[i]);
828
829                 str = g_key_file_get_string(keyfile,
830                                         services[i], "Favorite", NULL);
831                 if (str == NULL || g_strcmp0(str, "true")) {
832                         if (str)
833                                 g_free(str);
834                         g_key_file_free(keyfile);
835                         continue;
836                 }
837                 g_free(str);
838
839                 str = g_key_file_get_string(keyfile,
840                                         services[i], "AutoConnect", NULL);
841                 if (str == NULL || g_strcmp0(str, "true")) {
842                         if (str)
843                                 g_free(str);
844                         g_key_file_free(keyfile);
845                         continue;
846                 }
847                 g_free(str);
848
849                 str = g_key_file_get_string(keyfile,
850                                         services[i], "Modified", NULL);
851                 if (str != NULL) {
852                         g_time_val_from_iso8601(str, &modified);
853                         g_free(str);
854                 }
855
856                 ssid = g_key_file_get_string(keyfile,
857                                         services[i], "SSID", NULL);
858
859                 freq = g_key_file_get_integer(keyfile, services[i],
860                                         "Frequency", NULL);
861                 if (freq) {
862                         entry = g_try_new(struct last_connected, 1);
863                         if (entry == NULL) {
864                                 g_sequence_free(latest_list);
865                                 g_key_file_free(keyfile);
866                                 g_free(ssid);
867                                 return -ENOMEM;
868                         }
869
870                         entry->ssid = ssid;
871                         entry->modified = modified;
872                         entry->freq = freq;
873
874                         g_sequence_insert_sorted(latest_list, entry,
875                                                 sort_entry, NULL);
876                         num_ssids++;
877                 } else
878                         g_free(ssid);
879
880                 g_key_file_free(keyfile);
881         }
882
883         g_strfreev(services);
884
885         num_ssids = num_ssids > max_ssids ? max_ssids : num_ssids;
886
887         iter = g_sequence_get_begin_iter(latest_list);
888
889         for (i = 0; i < num_ssids; i++) {
890                 entry = g_sequence_get(iter);
891
892                 DBG("ssid %s freq %d modified %lu", entry->ssid, entry->freq,
893                                                 entry->modified.tv_sec);
894
895                 add_scan_param(entry->ssid, NULL, 0, entry->freq, scan_data,
896                                                 max_ssids, entry->ssid);
897
898                 iter = g_sequence_iter_next(iter);
899         }
900
901         g_sequence_free(latest_list);
902         return num_ssids;
903 }
904
905 static int wifi_scan(struct connman_device *device)
906 {
907         reset_autoscan(device);
908
909         return throw_wifi_scan(device, scan_callback_hidden);
910 }
911
912 static int wifi_scan_fast(struct connman_device *device)
913 {
914         struct wifi_data *wifi = connman_device_get_data(device);
915         GSupplicantScanParams *scan_params = NULL;
916         int ret;
917         int driver_max_ssids = 0;
918
919         if (wifi == NULL)
920                 return -ENODEV;
921
922         DBG("device %p %p", device, wifi->interface);
923
924         if (wifi->tethering == TRUE)
925                 return 0;
926
927         if (connman_device_get_scanning(device) == TRUE)
928                 return -EALREADY;
929
930         driver_max_ssids = g_supplicant_interface_get_max_scan_ssids(
931                                                         wifi->interface);
932         DBG("max ssids %d", driver_max_ssids);
933         if (driver_max_ssids == 0)
934                 return wifi_scan(device);
935
936         scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
937         if (scan_params == NULL)
938                 return -ENOMEM;
939
940         ret = get_latest_connections(driver_max_ssids, scan_params);
941         if (ret <= 0) {
942                 g_supplicant_free_scan_params(scan_params);
943                 return wifi_scan(device);
944         }
945
946         connman_device_ref(device);
947         reset_autoscan(device);
948
949         ret = g_supplicant_interface_scan(wifi->interface, scan_params,
950                                                 scan_callback, device);
951         if (ret == 0)
952                 connman_device_set_scanning(device, TRUE);
953         else {
954                 g_supplicant_free_scan_params(scan_params);
955                 connman_device_unref(device);
956         }
957
958         return ret;
959 }
960
961 /*
962  * This func is only used when connecting to this specific AP first time.
963  * It is not used when system autoconnects to hidden AP.
964  */
965 static int wifi_scan_hidden(struct connman_device *device,
966                 const char *ssid, unsigned int ssid_len,
967                 const char *identity, const char* passphrase,
968                 gpointer user_data)
969 {
970         struct wifi_data *wifi = connman_device_get_data(device);
971         GSupplicantScanParams *scan_params = NULL;
972         struct scan_ssid *scan_ssid;
973         struct hidden_params *hidden;
974         int ret;
975
976         if (wifi == NULL)
977                 return -ENODEV;
978
979         DBG("hidden SSID %s", ssid);
980
981         if (wifi->tethering == TRUE || wifi->hidden != NULL)
982                 return -EBUSY;
983
984         if (ssid == NULL || ssid_len == 0 || ssid_len > 32)
985                 return -EINVAL;
986
987         if (connman_device_get_scanning(device) == TRUE)
988                 return -EALREADY;
989
990         scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
991         if (scan_params == NULL)
992                 return -ENOMEM;
993
994         scan_ssid = g_try_new(struct scan_ssid, 1);
995         if (scan_ssid == NULL) {
996                 g_free(scan_params);
997                 return -ENOMEM;
998         }
999
1000         memcpy(scan_ssid->ssid, ssid, ssid_len);
1001         scan_ssid->ssid_len = ssid_len;
1002         scan_params->ssids = g_slist_prepend(scan_params->ssids, scan_ssid);
1003
1004         scan_params->num_ssids = 1;
1005
1006         hidden = g_try_new0(struct hidden_params, 1);
1007         if (hidden == NULL) {
1008                 g_free(scan_params);
1009                 return -ENOMEM;
1010         }
1011         memcpy(hidden->ssid, ssid, ssid_len);
1012         hidden->ssid_len = ssid_len;
1013         hidden->identity = g_strdup(identity);
1014         hidden->passphrase = g_strdup(passphrase);
1015         hidden->user_data = user_data;
1016         wifi->hidden = hidden;
1017
1018         connman_device_ref(device);
1019
1020         reset_autoscan(device);
1021
1022         ret = g_supplicant_interface_scan(wifi->interface, scan_params,
1023                         scan_callback, device);
1024         if (ret == 0)
1025                 connman_device_set_scanning(device, TRUE);
1026         else {
1027                 connman_device_unref(device);
1028                 g_supplicant_free_scan_params(scan_params);
1029                 hidden_free(wifi->hidden);
1030                 wifi->hidden = NULL;
1031         }
1032
1033         return ret;
1034 }
1035
1036 static void wifi_regdom_callback(int result,
1037                                         const char *alpha2,
1038                                                 void *user_data)
1039 {
1040         struct connman_device *device = user_data;
1041
1042         connman_device_regdom_notify(device, result, alpha2);
1043
1044         connman_device_unref(device);
1045 }
1046
1047 static int wifi_set_regdom(struct connman_device *device, const char *alpha2)
1048 {
1049         struct wifi_data *wifi = connman_device_get_data(device);
1050         int ret;
1051
1052         if (wifi == NULL)
1053                 return -EINVAL;
1054
1055         connman_device_ref(device);
1056
1057         ret = g_supplicant_interface_set_country(wifi->interface,
1058                                                 wifi_regdom_callback,
1059                                                         alpha2, device);
1060         if (ret != 0)
1061                 connman_device_unref(device);
1062
1063         return ret;
1064 }
1065
1066 static struct connman_device_driver wifi_ng_driver = {
1067         .name           = "wifi",
1068         .type           = CONNMAN_DEVICE_TYPE_WIFI,
1069         .priority       = CONNMAN_DEVICE_PRIORITY_LOW,
1070         .probe          = wifi_probe,
1071         .remove         = wifi_remove,
1072         .enable         = wifi_enable,
1073         .disable        = wifi_disable,
1074         .scan           = wifi_scan,
1075         .scan_fast      = wifi_scan_fast,
1076         .scan_hidden    = wifi_scan_hidden,
1077         .set_regdom     = wifi_set_regdom,
1078 };
1079
1080 static void system_ready(void)
1081 {
1082         DBG("");
1083
1084         if (connman_device_driver_register(&wifi_ng_driver) < 0)
1085                 connman_error("Failed to register WiFi driver");
1086 }
1087
1088 static void system_killed(void)
1089 {
1090         DBG("");
1091
1092         connman_device_driver_unregister(&wifi_ng_driver);
1093 }
1094
1095 static int network_probe(struct connman_network *network)
1096 {
1097         DBG("network %p", network);
1098
1099         return 0;
1100 }
1101
1102 static void network_remove(struct connman_network *network)
1103 {
1104         struct connman_device *device = connman_network_get_device(network);
1105         struct wifi_data *wifi;
1106
1107         DBG("network %p", network);
1108
1109         wifi = connman_device_get_data(device);
1110         if (wifi == NULL)
1111                 return;
1112
1113         if (wifi->network != network)
1114                 return;
1115
1116         wifi->network = NULL;
1117 }
1118
1119 static void connect_callback(int result, GSupplicantInterface *interface,
1120                                                         void *user_data)
1121 {
1122         struct connman_network *network = user_data;
1123
1124         DBG("network %p result %d", network, result);
1125
1126         if (result == -ENOKEY) {
1127                 connman_network_set_error(network,
1128                                         CONNMAN_NETWORK_ERROR_INVALID_KEY);
1129         } else if (result < 0) {
1130                 connman_network_set_error(network,
1131                                         CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
1132         }
1133 }
1134
1135 static GSupplicantSecurity network_security(const char *security)
1136 {
1137         if (g_str_equal(security, "none") == TRUE)
1138                 return G_SUPPLICANT_SECURITY_NONE;
1139         else if (g_str_equal(security, "wep") == TRUE)
1140                 return G_SUPPLICANT_SECURITY_WEP;
1141         else if (g_str_equal(security, "psk") == TRUE)
1142                 return G_SUPPLICANT_SECURITY_PSK;
1143         else if (g_str_equal(security, "wpa") == TRUE)
1144                 return G_SUPPLICANT_SECURITY_PSK;
1145         else if (g_str_equal(security, "rsn") == TRUE)
1146                 return G_SUPPLICANT_SECURITY_PSK;
1147         else if (g_str_equal(security, "ieee8021x") == TRUE)
1148                 return G_SUPPLICANT_SECURITY_IEEE8021X;
1149
1150         return G_SUPPLICANT_SECURITY_UNKNOWN;
1151 }
1152
1153 static void ssid_init(GSupplicantSSID *ssid, struct connman_network *network)
1154 {
1155         const char *security, *passphrase, *agent_passphrase;
1156
1157         memset(ssid, 0, sizeof(*ssid));
1158         ssid->mode = G_SUPPLICANT_MODE_INFRA;
1159         ssid->ssid = connman_network_get_blob(network, "WiFi.SSID",
1160                                                 &ssid->ssid_len);
1161         ssid->scan_ssid = 1;
1162         security = connman_network_get_string(network, "WiFi.Security");
1163         ssid->security = network_security(security);
1164         passphrase = connman_network_get_string(network,
1165                                                 "WiFi.Passphrase");
1166         if (passphrase == NULL || strlen(passphrase) == 0) {
1167
1168                 /* Use agent provided passphrase as a fallback */
1169                 agent_passphrase = connman_network_get_string(network,
1170                                                 "WiFi.AgentPassphrase");
1171
1172                 if (agent_passphrase == NULL || strlen(agent_passphrase) == 0)
1173                         ssid->passphrase = NULL;
1174                 else
1175                         ssid->passphrase = agent_passphrase;
1176         } else
1177                 ssid->passphrase = passphrase;
1178
1179         ssid->eap = connman_network_get_string(network, "WiFi.EAP");
1180
1181         /*
1182          * If our private key password is unset,
1183          * we use the supplied passphrase. That is needed
1184          * for PEAP where 2 passphrases (identity and client
1185          * cert may have to be provided.
1186          */
1187         if (connman_network_get_string(network,
1188                                         "WiFi.PrivateKeyPassphrase") == NULL)
1189                 connman_network_set_string(network,
1190                                                 "WiFi.PrivateKeyPassphrase",
1191                                                 ssid->passphrase);
1192         /* We must have an identity for both PEAP and TLS */
1193         ssid->identity = connman_network_get_string(network, "WiFi.Identity");
1194
1195         /* Use agent provided identity as a fallback */
1196         if (ssid->identity == NULL || strlen(ssid->identity) == 0)
1197                 ssid->identity = connman_network_get_string(network,
1198                                                         "WiFi.AgentIdentity");
1199
1200         ssid->ca_cert_path = connman_network_get_string(network,
1201                                                         "WiFi.CACertFile");
1202         ssid->client_cert_path = connman_network_get_string(network,
1203                                                         "WiFi.ClientCertFile");
1204         ssid->private_key_path = connman_network_get_string(network,
1205                                                         "WiFi.PrivateKeyFile");
1206         ssid->private_key_passphrase = connman_network_get_string(network,
1207                                                 "WiFi.PrivateKeyPassphrase");
1208         ssid->phase2_auth = connman_network_get_string(network, "WiFi.Phase2");
1209
1210         ssid->use_wps = connman_network_get_bool(network, "WiFi.UseWPS");
1211         ssid->pin_wps = connman_network_get_string(network, "WiFi.PinWPS");
1212
1213         if (connman_setting_get_bool("BackgroundScanning") == TRUE)
1214                 ssid->bgscan = BGSCAN_DEFAULT;
1215 }
1216
1217 static int network_connect(struct connman_network *network)
1218 {
1219         struct connman_device *device = connman_network_get_device(network);
1220         struct wifi_data *wifi;
1221         GSupplicantInterface *interface;
1222         GSupplicantSSID *ssid;
1223
1224         DBG("network %p", network);
1225
1226         if (device == NULL)
1227                 return -ENODEV;
1228
1229         wifi = connman_device_get_data(device);
1230         if (wifi == NULL)
1231                 return -ENODEV;
1232
1233         ssid = g_try_malloc0(sizeof(GSupplicantSSID));
1234         if (ssid == NULL)
1235                 return -ENOMEM;
1236
1237         interface = wifi->interface;
1238
1239         ssid_init(ssid, network);
1240
1241         if (wifi->disconnecting == TRUE)
1242                 wifi->pending_network = network;
1243         else {
1244                 wifi->network = network;
1245                 wifi->retries = 0;
1246
1247                 return g_supplicant_interface_connect(interface, ssid,
1248                                                 connect_callback, network);
1249         }
1250
1251         return -EINPROGRESS;
1252 }
1253
1254 static void disconnect_callback(int result, GSupplicantInterface *interface,
1255                                                                 void *user_data)
1256 {
1257         struct wifi_data *wifi = user_data;
1258
1259         if (wifi->network != NULL) {
1260                 /*
1261                  * if result < 0 supplican return an error because
1262                  * the network is not current.
1263                  * we wont receive G_SUPPLICANT_STATE_DISCONNECTED since it
1264                  * failed, call connman_network_set_connected to report
1265                  * disconnect is completed.
1266                  */
1267                 if (result < 0)
1268                         connman_network_set_connected(wifi->network, FALSE);
1269         }
1270
1271         wifi->network = NULL;
1272
1273         wifi->disconnecting = FALSE;
1274
1275         if (wifi->pending_network != NULL) {
1276                 network_connect(wifi->pending_network);
1277                 wifi->pending_network = NULL;
1278         }
1279
1280         start_autoscan(wifi->device);
1281 }
1282
1283 static int network_disconnect(struct connman_network *network)
1284 {
1285         struct connman_device *device = connman_network_get_device(network);
1286         struct wifi_data *wifi;
1287         int err;
1288
1289         DBG("network %p", network);
1290
1291         wifi = connman_device_get_data(device);
1292         if (wifi == NULL || wifi->interface == NULL)
1293                 return -ENODEV;
1294
1295         connman_network_set_associating(network, FALSE);
1296
1297         if (wifi->disconnecting == TRUE)
1298                 return -EALREADY;
1299
1300         wifi->disconnecting = TRUE;
1301
1302         err = g_supplicant_interface_disconnect(wifi->interface,
1303                                                 disconnect_callback, wifi);
1304         if (err < 0)
1305                 wifi->disconnecting = FALSE;
1306
1307         return err;
1308 }
1309
1310 static struct connman_network_driver network_driver = {
1311         .name           = "wifi",
1312         .type           = CONNMAN_NETWORK_TYPE_WIFI,
1313         .priority       = CONNMAN_NETWORK_PRIORITY_LOW,
1314         .probe          = network_probe,
1315         .remove         = network_remove,
1316         .connect        = network_connect,
1317         .disconnect     = network_disconnect,
1318 };
1319
1320 static void interface_added(GSupplicantInterface *interface)
1321 {
1322         const char *ifname = g_supplicant_interface_get_ifname(interface);
1323         const char *driver = g_supplicant_interface_get_driver(interface);
1324         struct wifi_data *wifi;
1325
1326         wifi = g_supplicant_interface_get_data(interface);
1327
1328         /*
1329          * We can get here with a NULL wifi pointer when
1330          * the interface added signal is sent before the
1331          * interface creation callback is called.
1332          */
1333         if (wifi == NULL)
1334                 return;
1335
1336         DBG("ifname %s driver %s wifi %p tethering %d",
1337                         ifname, driver, wifi, wifi->tethering);
1338
1339         if (wifi->device == NULL) {
1340                 connman_error("WiFi device not set");
1341                 return;
1342         }
1343
1344         connman_device_set_powered(wifi->device, TRUE);
1345
1346         if (wifi->tethering == TRUE)
1347                 return;
1348 }
1349
1350 static connman_bool_t is_idle(struct wifi_data *wifi)
1351 {
1352         DBG("state %d", wifi->state);
1353
1354         switch (wifi->state) {
1355         case G_SUPPLICANT_STATE_UNKNOWN:
1356         case G_SUPPLICANT_STATE_DISCONNECTED:
1357         case G_SUPPLICANT_STATE_INACTIVE:
1358         case G_SUPPLICANT_STATE_SCANNING:
1359                 return TRUE;
1360
1361         case G_SUPPLICANT_STATE_AUTHENTICATING:
1362         case G_SUPPLICANT_STATE_ASSOCIATING:
1363         case G_SUPPLICANT_STATE_ASSOCIATED:
1364         case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
1365         case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
1366         case G_SUPPLICANT_STATE_COMPLETED:
1367                 return FALSE;
1368         }
1369
1370         return FALSE;
1371 }
1372
1373 static connman_bool_t is_idle_wps(GSupplicantInterface *interface,
1374                                                 struct wifi_data *wifi)
1375 {
1376         /* First, let's check if WPS processing did not went wrong */
1377         if (g_supplicant_interface_get_wps_state(interface) ==
1378                 G_SUPPLICANT_WPS_STATE_FAIL)
1379                 return FALSE;
1380
1381         /* Unlike normal connection, being associated while processing wps
1382          * actually means that we are idling. */
1383         switch (wifi->state) {
1384         case G_SUPPLICANT_STATE_UNKNOWN:
1385         case G_SUPPLICANT_STATE_DISCONNECTED:
1386         case G_SUPPLICANT_STATE_INACTIVE:
1387         case G_SUPPLICANT_STATE_SCANNING:
1388         case G_SUPPLICANT_STATE_ASSOCIATED:
1389                 return TRUE;
1390         case G_SUPPLICANT_STATE_AUTHENTICATING:
1391         case G_SUPPLICANT_STATE_ASSOCIATING:
1392         case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
1393         case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
1394         case G_SUPPLICANT_STATE_COMPLETED:
1395                 return FALSE;
1396         }
1397
1398         return FALSE;
1399 }
1400
1401 static connman_bool_t handle_wps_completion(GSupplicantInterface *interface,
1402                                         struct connman_network *network,
1403                                         struct connman_device *device,
1404                                         struct wifi_data *wifi)
1405 {
1406         connman_bool_t wps;
1407
1408         wps = connman_network_get_bool(network, "WiFi.UseWPS");
1409         if (wps == TRUE) {
1410                 const unsigned char *ssid, *wps_ssid;
1411                 unsigned int ssid_len, wps_ssid_len;
1412                 const char *wps_key;
1413
1414                 /* Checking if we got associated with requested
1415                  * network */
1416                 ssid = connman_network_get_blob(network, "WiFi.SSID",
1417                                                 &ssid_len);
1418
1419                 wps_ssid = g_supplicant_interface_get_wps_ssid(
1420                         interface, &wps_ssid_len);
1421
1422                 if (wps_ssid == NULL || wps_ssid_len != ssid_len ||
1423                                 memcmp(ssid, wps_ssid, ssid_len) != 0) {
1424                         connman_network_set_associating(network, FALSE);
1425                         g_supplicant_interface_disconnect(wifi->interface,
1426                                                 disconnect_callback, wifi);
1427                         return FALSE;
1428                 }
1429
1430                 wps_key = g_supplicant_interface_get_wps_key(interface);
1431                 connman_network_set_string(network, "WiFi.Passphrase",
1432                                         wps_key);
1433
1434                 connman_network_set_string(network, "WiFi.PinWPS", NULL);
1435         }
1436
1437         return TRUE;
1438 }
1439
1440 static connman_bool_t handle_4way_handshake_failure(GSupplicantInterface *interface,
1441                                         struct connman_network *network,
1442                                         struct wifi_data *wifi)
1443 {
1444         struct connman_service *service;
1445
1446         if (wifi->state != G_SUPPLICANT_STATE_4WAY_HANDSHAKE)
1447                 return FALSE;
1448
1449         service = connman_service_lookup_from_network(network);
1450         if (service == NULL)
1451                 return FALSE;
1452
1453         wifi->retries++;
1454
1455         if (connman_service_get_favorite(service) == TRUE) {
1456                 if (wifi->retries < FAVORITE_MAXIMUM_RETRIES)
1457                         return TRUE;
1458         } else if (wifi->retries < MAXIMUM_RETRIES)
1459                 return TRUE;
1460
1461         connman_network_set_error(network, CONNMAN_NETWORK_ERROR_INVALID_KEY);
1462
1463         return FALSE;
1464 }
1465
1466 static void interface_state(GSupplicantInterface *interface)
1467 {
1468         struct connman_network *network;
1469         struct connman_device *device;
1470         struct wifi_data *wifi;
1471         GSupplicantState state = g_supplicant_interface_get_state(interface);
1472         connman_bool_t wps;
1473
1474         wifi = g_supplicant_interface_get_data(interface);
1475
1476         DBG("wifi %p interface state %d", wifi, state);
1477
1478         if (wifi == NULL)
1479                 return;
1480
1481         network = wifi->network;
1482         device = wifi->device;
1483
1484         if (device == NULL || network == NULL)
1485                 return;
1486
1487         switch (state) {
1488         case G_SUPPLICANT_STATE_SCANNING:
1489                 break;
1490
1491         case G_SUPPLICANT_STATE_AUTHENTICATING:
1492         case G_SUPPLICANT_STATE_ASSOCIATING:
1493                 stop_autoscan(device);
1494
1495                 if (wifi->connected == FALSE)
1496                         connman_network_set_associating(network, TRUE);
1497
1498                 break;
1499
1500         case G_SUPPLICANT_STATE_COMPLETED:
1501                 /* though it should be already stopped: */
1502                 stop_autoscan(device);
1503
1504                 if (handle_wps_completion(interface, network, device, wifi) ==
1505                                                                         FALSE)
1506                         break;
1507
1508                 connman_network_set_connected(network, TRUE);
1509                 break;
1510
1511         case G_SUPPLICANT_STATE_DISCONNECTED:
1512                 /*
1513                  * If we're in one of the idle modes, we have
1514                  * not started association yet and thus setting
1515                  * those ones to FALSE could cancel an association
1516                  * in progress.
1517                  */
1518                 wps = connman_network_get_bool(network, "WiFi.UseWPS");
1519                 if (wps == TRUE)
1520                         if (is_idle_wps(interface, wifi) == TRUE)
1521                                 break;
1522
1523                 if (is_idle(wifi))
1524                         break;
1525
1526                 /* If previous state was 4way-handshake, then
1527                  * it's either: psk was incorrect and thus we retry
1528                  * or if we reach the maximum retries we declare the
1529                  * psk as wrong */
1530                 if (handle_4way_handshake_failure(interface,
1531                                                 network, wifi) == TRUE)
1532                         break;
1533
1534                 /* We disable the selected network, if not then
1535                  * wpa_supplicant will loop retrying */
1536                 if (g_supplicant_interface_enable_selected_network(interface,
1537                                                 FALSE) != 0)
1538                         DBG("Could not disables selected network");
1539
1540                 connman_network_set_connected(network, FALSE);
1541                 connman_network_set_associating(network, FALSE);
1542                 wifi->disconnecting = FALSE;
1543
1544                 start_autoscan(device);
1545
1546                 break;
1547
1548         case G_SUPPLICANT_STATE_INACTIVE:
1549                 connman_network_set_associating(network, FALSE);
1550                 start_autoscan(device);
1551
1552                 break;
1553
1554         case G_SUPPLICANT_STATE_UNKNOWN:
1555         case G_SUPPLICANT_STATE_ASSOCIATED:
1556         case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
1557         case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
1558                 break;
1559         }
1560
1561         wifi->state = state;
1562
1563         /* Saving wpa_s state policy:
1564          * If connected and if the state changes are roaming related:
1565          * --> We stay connected
1566          * If completed
1567          * --> We are connected
1568          * All other case:
1569          * --> We are not connected
1570          * */
1571         switch (state) {
1572         case G_SUPPLICANT_STATE_AUTHENTICATING:
1573         case G_SUPPLICANT_STATE_ASSOCIATING:
1574         case G_SUPPLICANT_STATE_ASSOCIATED:
1575         case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
1576         case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
1577                 if (wifi->connected == TRUE)
1578                         connman_warn("Probably roaming right now!"
1579                                                 " Staying connected...");
1580                 else
1581                         wifi->connected = FALSE;
1582                 break;
1583         case G_SUPPLICANT_STATE_COMPLETED:
1584                 wifi->connected = TRUE;
1585                 break;
1586         default:
1587                 wifi->connected = FALSE;
1588                 break;
1589         }
1590
1591         DBG("DONE");
1592 }
1593
1594 static void interface_removed(GSupplicantInterface *interface)
1595 {
1596         const char *ifname = g_supplicant_interface_get_ifname(interface);
1597         struct wifi_data *wifi;
1598
1599         DBG("ifname %s", ifname);
1600
1601         wifi = g_supplicant_interface_get_data(interface);
1602
1603         if (wifi != NULL && wifi->tethering == TRUE)
1604                 return;
1605
1606         if (wifi == NULL || wifi->device == NULL) {
1607                 DBG("wifi interface already removed");
1608                 return;
1609         }
1610
1611         wifi->interface = NULL;
1612         connman_device_set_powered(wifi->device, FALSE);
1613 }
1614
1615 static void scan_started(GSupplicantInterface *interface)
1616 {
1617         DBG("");
1618 }
1619
1620 static void scan_finished(GSupplicantInterface *interface)
1621 {
1622         DBG("");
1623 }
1624
1625 static unsigned char calculate_strength(GSupplicantNetwork *supplicant_network)
1626 {
1627         unsigned char strength;
1628
1629         strength = 120 + g_supplicant_network_get_signal(supplicant_network);
1630         if (strength > 100)
1631                 strength = 100;
1632
1633         return strength;
1634 }
1635
1636 static void network_added(GSupplicantNetwork *supplicant_network)
1637 {
1638         struct connman_network *network;
1639         GSupplicantInterface *interface;
1640         struct wifi_data *wifi;
1641         const char *name, *identifier, *security, *group, *mode;
1642         const unsigned char *ssid;
1643         unsigned int ssid_len;
1644         connman_bool_t wps;
1645         connman_bool_t wps_pbc;
1646         connman_bool_t wps_ready;
1647         connman_bool_t wps_advertizing;
1648
1649         DBG("");
1650
1651         interface = g_supplicant_network_get_interface(supplicant_network);
1652         wifi = g_supplicant_interface_get_data(interface);
1653         name = g_supplicant_network_get_name(supplicant_network);
1654         identifier = g_supplicant_network_get_identifier(supplicant_network);
1655         security = g_supplicant_network_get_security(supplicant_network);
1656         group = g_supplicant_network_get_identifier(supplicant_network);
1657         wps = g_supplicant_network_get_wps(supplicant_network);
1658         wps_pbc = g_supplicant_network_is_wps_pbc(supplicant_network);
1659         wps_ready = g_supplicant_network_is_wps_active(supplicant_network);
1660         wps_advertizing = g_supplicant_network_is_wps_advertizing(
1661                                                         supplicant_network);
1662         mode = g_supplicant_network_get_mode(supplicant_network);
1663
1664         if (wifi == NULL)
1665                 return;
1666
1667         ssid = g_supplicant_network_get_ssid(supplicant_network, &ssid_len);
1668
1669         network = connman_device_get_network(wifi->device, identifier);
1670
1671         if (network == NULL) {
1672                 network = connman_network_create(identifier,
1673                                                 CONNMAN_NETWORK_TYPE_WIFI);
1674                 if (network == NULL)
1675                         return;
1676
1677                 connman_network_set_index(network, wifi->index);
1678
1679                 if (connman_device_add_network(wifi->device, network) < 0) {
1680                         connman_network_unref(network);
1681                         return;
1682                 }
1683
1684                 wifi->networks = g_slist_append(wifi->networks, network);
1685         }
1686
1687         if (name != NULL && name[0] != '\0')
1688                 connman_network_set_name(network, name);
1689
1690         connman_network_set_blob(network, "WiFi.SSID",
1691                                                 ssid, ssid_len);
1692         connman_network_set_string(network, "WiFi.Security", security);
1693         connman_network_set_strength(network,
1694                                 calculate_strength(supplicant_network));
1695         connman_network_set_bool(network, "WiFi.WPS", wps);
1696
1697         if (wps == TRUE) {
1698                 /* Is AP advertizing for WPS association?
1699                  * If so, we decide to use WPS by default */
1700                 if (wps_ready == TRUE && wps_pbc == TRUE &&
1701                                                 wps_advertizing == TRUE)
1702                         connman_network_set_bool(network, "WiFi.UseWPS", TRUE);
1703         }
1704
1705         connman_network_set_frequency(network,
1706                         g_supplicant_network_get_frequency(supplicant_network));
1707
1708         connman_network_set_available(network, TRUE);
1709         connman_network_set_string(network, "WiFi.Mode", mode);
1710
1711         if (ssid != NULL)
1712                 connman_network_set_group(network, group);
1713
1714         if (wifi->hidden != NULL && ssid != NULL) {
1715                 if (wifi->hidden->ssid_len == ssid_len &&
1716                                 memcmp(wifi->hidden->ssid, ssid,
1717                                                 ssid_len) == 0) {
1718                         connman_network_connect_hidden(network,
1719                                         wifi->hidden->identity,
1720                                         wifi->hidden->passphrase,
1721                                         wifi->hidden->user_data);
1722                         wifi->hidden->user_data = NULL;
1723                         hidden_free(wifi->hidden);
1724                         wifi->hidden = NULL;
1725                 }
1726         }
1727 }
1728
1729 static void network_removed(GSupplicantNetwork *network)
1730 {
1731         GSupplicantInterface *interface;
1732         struct wifi_data *wifi;
1733         const char *name, *identifier;
1734         struct connman_network *connman_network;
1735
1736         interface = g_supplicant_network_get_interface(network);
1737         wifi = g_supplicant_interface_get_data(interface);
1738         identifier = g_supplicant_network_get_identifier(network);
1739         name = g_supplicant_network_get_name(network);
1740
1741         DBG("name %s", name);
1742
1743         if (wifi == NULL)
1744                 return;
1745
1746         connman_network = connman_device_get_network(wifi->device, identifier);
1747         if (connman_network == NULL)
1748                 return;
1749
1750         wifi->networks = g_slist_remove(wifi->networks, connman_network);
1751
1752         connman_device_remove_network(wifi->device, connman_network);
1753         connman_network_unref(connman_network);
1754 }
1755
1756 static void network_changed(GSupplicantNetwork *network, const char *property)
1757 {
1758         GSupplicantInterface *interface;
1759         struct wifi_data *wifi;
1760         const char *name, *identifier;
1761         struct connman_network *connman_network;
1762
1763         interface = g_supplicant_network_get_interface(network);
1764         wifi = g_supplicant_interface_get_data(interface);
1765         identifier = g_supplicant_network_get_identifier(network);
1766         name = g_supplicant_network_get_name(network);
1767
1768         DBG("name %s", name);
1769
1770         if (wifi == NULL)
1771                 return;
1772
1773         connman_network = connman_device_get_network(wifi->device, identifier);
1774         if (connman_network == NULL)
1775                 return;
1776
1777         if (g_str_equal(property, "Signal") == TRUE) {
1778                connman_network_set_strength(connman_network,
1779                                         calculate_strength(network));
1780                connman_network_update(connman_network);
1781         }
1782 }
1783
1784 static void debug(const char *str)
1785 {
1786         if (getenv("CONNMAN_SUPPLICANT_DEBUG"))
1787                 connman_debug("%s", str);
1788 }
1789
1790 static const GSupplicantCallbacks callbacks = {
1791         .system_ready           = system_ready,
1792         .system_killed          = system_killed,
1793         .interface_added        = interface_added,
1794         .interface_state        = interface_state,
1795         .interface_removed      = interface_removed,
1796         .scan_started           = scan_started,
1797         .scan_finished          = scan_finished,
1798         .network_added          = network_added,
1799         .network_removed        = network_removed,
1800         .network_changed        = network_changed,
1801         .debug                  = debug,
1802 };
1803
1804
1805 static int tech_probe(struct connman_technology *technology)
1806 {
1807         wifi_technology = technology;
1808
1809         return 0;
1810 }
1811
1812 static void tech_remove(struct connman_technology *technology)
1813 {
1814         wifi_technology = NULL;
1815 }
1816
1817 struct wifi_tethering_info {
1818         struct wifi_data *wifi;
1819         struct connman_technology *technology;
1820         char *ifname;
1821         GSupplicantSSID *ssid;
1822 };
1823
1824 static GSupplicantSSID *ssid_ap_init(const char *ssid, const char *passphrase)
1825 {
1826         GSupplicantSSID *ap;
1827
1828         ap = g_try_malloc0(sizeof(GSupplicantSSID));
1829         if (ap == NULL)
1830                 return NULL;
1831
1832         ap->mode = G_SUPPLICANT_MODE_MASTER;
1833         ap->ssid = ssid;
1834         ap->ssid_len = strlen(ssid);
1835         ap->scan_ssid = 0;
1836         ap->freq = 2412;
1837
1838         if (passphrase == NULL || strlen(passphrase) == 0) {
1839                 ap->security = G_SUPPLICANT_SECURITY_NONE;
1840                 ap->passphrase = NULL;
1841         } else {
1842                ap->security = G_SUPPLICANT_SECURITY_PSK;
1843                ap->protocol = G_SUPPLICANT_PROTO_RSN;
1844                ap->pairwise_cipher = G_SUPPLICANT_PAIRWISE_CCMP;
1845                ap->group_cipher = G_SUPPLICANT_GROUP_CCMP;
1846                ap->passphrase = passphrase;
1847         }
1848
1849         return ap;
1850 }
1851
1852 static void ap_start_callback(int result, GSupplicantInterface *interface,
1853                                                         void *user_data)
1854 {
1855         struct wifi_tethering_info *info = user_data;
1856
1857         DBG("result %d index %d bridge %s",
1858                 result, info->wifi->index, info->wifi->bridge);
1859
1860         if (result < 0) {
1861                 connman_inet_remove_from_bridge(info->wifi->index,
1862                                                         info->wifi->bridge);
1863                 connman_technology_tethering_notify(info->technology, FALSE);
1864         }
1865
1866         g_free(info->ifname);
1867         g_free(info);
1868 }
1869
1870 static void ap_create_callback(int result,
1871                                 GSupplicantInterface *interface,
1872                                         void *user_data)
1873 {
1874         struct wifi_tethering_info *info = user_data;
1875
1876         DBG("result %d ifname %s", result,
1877                                 g_supplicant_interface_get_ifname(interface));
1878
1879         if (result < 0) {
1880                 connman_inet_remove_from_bridge(info->wifi->index,
1881                                                         info->wifi->bridge);
1882                 connman_technology_tethering_notify(info->technology, FALSE);
1883
1884                 g_free(info->ifname);
1885                 g_free(info);
1886                 return;
1887         }
1888
1889         info->wifi->interface = interface;
1890         g_supplicant_interface_set_data(interface, info->wifi);
1891
1892         if (g_supplicant_interface_set_apscan(interface, 2) < 0)
1893                 connman_error("Failed to set interface ap_scan property");
1894
1895         g_supplicant_interface_connect(interface, info->ssid,
1896                                                 ap_start_callback, info);
1897 }
1898
1899 static void sta_remove_callback(int result,
1900                                 GSupplicantInterface *interface,
1901                                         void *user_data)
1902 {
1903         struct wifi_tethering_info *info = user_data;
1904         const char *driver = connman_option_get_string("wifi");
1905
1906         DBG("ifname %s result %d ", info->ifname, result);
1907
1908         if (result < 0) {
1909                 info->wifi->tethering = TRUE;
1910
1911                 g_free(info->ifname);
1912                 g_free(info);
1913                 return;
1914         }
1915
1916         info->wifi->interface = NULL;
1917
1918         connman_technology_tethering_notify(info->technology, TRUE);
1919
1920         g_supplicant_interface_create(info->ifname, driver, info->wifi->bridge,
1921                                                 ap_create_callback,
1922                                                         info);
1923 }
1924
1925 static int tech_set_tethering(struct connman_technology *technology,
1926                                 const char *identifier, const char *passphrase,
1927                                 const char *bridge, connman_bool_t enabled)
1928 {
1929         GList *list;
1930         GSupplicantInterface *interface;
1931         struct wifi_data *wifi;
1932         struct wifi_tethering_info *info;
1933         const char *ifname;
1934         unsigned int mode;
1935         int err;
1936
1937         DBG("");
1938
1939         if (enabled == FALSE) {
1940                 for (list = iface_list; list; list = list->next) {
1941                         wifi = list->data;
1942
1943                         if (wifi->tethering == TRUE) {
1944                                 wifi->tethering = FALSE;
1945
1946                                 connman_inet_remove_from_bridge(wifi->index,
1947                                                                         bridge);
1948                                 wifi->bridged = FALSE;
1949                         }
1950                 }
1951
1952                 connman_technology_tethering_notify(technology, FALSE);
1953
1954                 return 0;
1955         }
1956
1957         for (list = iface_list; list; list = list->next) {
1958                 wifi = list->data;
1959
1960                 interface = wifi->interface;
1961
1962                 if (interface == NULL)
1963                         continue;
1964
1965                 ifname = g_supplicant_interface_get_ifname(wifi->interface);
1966
1967                 mode = g_supplicant_interface_get_mode(interface);
1968                 if ((mode & G_SUPPLICANT_CAPABILITY_MODE_AP) == 0) {
1969                         DBG("%s does not support AP mode", ifname);
1970                         continue;
1971                 }
1972
1973                 info = g_try_malloc0(sizeof(struct wifi_tethering_info));
1974                 if (info == NULL)
1975                         return -ENOMEM;
1976
1977                 info->wifi = wifi;
1978                 info->technology = technology;
1979                 info->wifi->bridge = bridge;
1980                 info->ssid = ssid_ap_init(identifier, passphrase);
1981                 if (info->ssid == NULL) {
1982                         g_free(info);
1983                         continue;
1984                 }
1985                 info->ifname = g_strdup(ifname);
1986                 if (info->ifname == NULL) {
1987                         g_free(info);
1988                         continue;
1989                 }
1990
1991                 info->wifi->tethering = TRUE;
1992
1993                 err = g_supplicant_interface_remove(interface,
1994                                                 sta_remove_callback,
1995                                                         info);
1996                 if (err == 0)
1997                         return err;
1998         }
1999
2000         return -EOPNOTSUPP;
2001 }
2002
2003 static void regdom_callback(int result, const char *alpha2, void *user_data)
2004 {
2005         DBG("");
2006
2007         if (wifi_technology == NULL)
2008                 return;
2009
2010         if (result != 0)
2011                 alpha2 = NULL;
2012
2013         connman_technology_regdom_notify(wifi_technology, alpha2);
2014 }
2015
2016 static int tech_set_regdom(struct connman_technology *technology, const char *alpha2)
2017 {
2018         return g_supplicant_set_country(alpha2, regdom_callback, NULL);
2019 }
2020
2021 static struct connman_technology_driver tech_driver = {
2022         .name           = "wifi",
2023         .type           = CONNMAN_SERVICE_TYPE_WIFI,
2024         .probe          = tech_probe,
2025         .remove         = tech_remove,
2026         .set_tethering  = tech_set_tethering,
2027         .set_regdom     = tech_set_regdom,
2028 };
2029
2030 static int wifi_init(void)
2031 {
2032         int err;
2033
2034         err = connman_network_driver_register(&network_driver);
2035         if (err < 0)
2036                 return err;
2037
2038         err = g_supplicant_register(&callbacks);
2039         if (err < 0) {
2040                 connman_network_driver_unregister(&network_driver);
2041                 return err;
2042         }
2043
2044         err = connman_technology_driver_register(&tech_driver);
2045         if (err < 0) {
2046                 g_supplicant_unregister(&callbacks);
2047                 connman_network_driver_unregister(&network_driver);
2048                 return err;
2049         }
2050
2051         return 0;
2052 }
2053
2054 static void wifi_exit(void)
2055 {
2056         DBG();
2057
2058         connman_technology_driver_unregister(&tech_driver);
2059
2060         g_supplicant_unregister(&callbacks);
2061
2062         connman_network_driver_unregister(&network_driver);
2063 }
2064
2065 CONNMAN_PLUGIN_DEFINE(wifi, "WiFi interface plugin", VERSION,
2066                 CONNMAN_PLUGIN_PRIORITY_DEFAULT, wifi_init, wifi_exit)