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