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