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