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