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