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