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