service: Connecting hidden network waits until connected
[framework/connectivity/connman.git] / plugins / wifi.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2012  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <unistd.h>
27 #include <stdlib.h>
28 #include <errno.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <sys/ioctl.h>
32 #include <sys/socket.h>
33 #include <linux/if_arp.h>
34 #include <linux/wireless.h>
35 #include <net/ethernet.h>
36
37 #ifndef IFF_LOWER_UP
38 #define IFF_LOWER_UP    0x10000
39 #endif
40
41 #include <dbus/dbus.h>
42 #include <glib.h>
43
44 #define CONNMAN_API_SUBJECT_TO_CHANGE
45 #include <connman/plugin.h>
46 #include <connman/inet.h>
47 #include <connman/device.h>
48 #include <connman/rtnl.h>
49 #include <connman/technology.h>
50 #include <connman/log.h>
51 #include <connman/option.h>
52 #include <connman/storage.h>
53 #include <include/setting.h>
54
55 #include <gsupplicant/gsupplicant.h>
56
57 #define CLEANUP_TIMEOUT   8     /* in seconds */
58 #define INACTIVE_TIMEOUT  12    /* in seconds */
59 #define MAXIMUM_RETRIES   4
60
61 #define BGSCAN_DEFAULT "simple:30:-45:300"
62 #define AUTOSCAN_DEFAULT "exponential:3:300"
63
64 static struct connman_technology *wifi_technology = NULL;
65
66 struct hidden_params {
67         char ssid[32];
68         unsigned int ssid_len;
69         char *identity;
70         char *passphrase;
71         gpointer user_data;
72 };
73
74 /**
75  * Used for autoscan "emulation".
76  * Should be removed when wpa_s autoscan support will be by default.
77  */
78 struct autoscan_params {
79         int base;
80         int limit;
81         int interval;
82         unsigned int timeout;
83 };
84
85 struct wifi_data {
86         char *identifier;
87         struct connman_device *device;
88         struct connman_network *network;
89         struct connman_network *pending_network;
90         GSList *networks;
91         GSupplicantInterface *interface;
92         GSupplicantState state;
93         connman_bool_t connected;
94         connman_bool_t disconnecting;
95         connman_bool_t tethering;
96         connman_bool_t bridged;
97         const char *bridge;
98         int index;
99         unsigned flags;
100         unsigned int watch;
101         int retries;
102         struct hidden_params *hidden;
103         /**
104          * autoscan "emulation".
105          */
106         struct autoscan_params *autoscan;
107 };
108
109 static GList *iface_list = NULL;
110
111 static void handle_tethering(struct wifi_data *wifi)
112 {
113         if (wifi->tethering == FALSE)
114                 return;
115
116         if (wifi->bridge == NULL)
117                 return;
118
119         if (wifi->bridged == TRUE)
120                 return;
121
122         DBG("index %d bridge %s", wifi->index, wifi->bridge);
123
124         if (connman_inet_add_to_bridge(wifi->index, wifi->bridge) < 0)
125                 return;
126
127         wifi->bridged = TRUE;
128 }
129
130 static void wifi_newlink(unsigned flags, unsigned change, void *user_data)
131 {
132         struct connman_device *device = user_data;
133         struct wifi_data *wifi = connman_device_get_data(device);
134
135         DBG("index %d flags %d change %d", wifi->index, flags, change);
136
137         if (!change)
138                 return;
139
140         if ((wifi->flags & IFF_UP) != (flags & IFF_UP)) {
141                 if (flags & IFF_UP)
142                         DBG("interface up");
143                 else
144                         DBG("interface down");
145         }
146
147         if ((wifi->flags & IFF_LOWER_UP) != (flags & IFF_LOWER_UP)) {
148                 if (flags & IFF_LOWER_UP) {
149                         DBG("carrier on");
150
151                         handle_tethering(wifi);
152                 } else
153                         DBG("carrier off");
154         }
155
156         wifi->flags = flags;
157 }
158
159 static int wifi_probe(struct connman_device *device)
160 {
161         struct wifi_data *wifi;
162
163         DBG("device %p", device);
164
165         wifi = g_try_new0(struct wifi_data, 1);
166         if (wifi == NULL)
167                 return -ENOMEM;
168
169         wifi->connected = FALSE;
170         wifi->disconnecting = FALSE;
171         wifi->tethering = FALSE;
172         wifi->bridged = FALSE;
173         wifi->bridge = NULL;
174         wifi->state = G_SUPPLICANT_STATE_INACTIVE;
175
176         connman_device_set_data(device, wifi);
177         wifi->device = connman_device_ref(device);
178
179         wifi->index = connman_device_get_index(device);
180         wifi->flags = 0;
181
182         wifi->watch = connman_rtnl_add_newlink_watch(wifi->index,
183                                                         wifi_newlink, device);
184
185         iface_list = g_list_append(iface_list, wifi);
186
187         return 0;
188 }
189
190 static void remove_networks(struct connman_device *device,
191                                 struct wifi_data *wifi)
192 {
193         GSList *list;
194
195         for (list = wifi->networks; list != NULL; list = list->next) {
196                 struct connman_network *network = list->data;
197
198                 connman_device_remove_network(device, network);
199                 connman_network_unref(network);
200         }
201
202         g_slist_free(wifi->networks);
203         wifi->networks = NULL;
204 }
205
206 static void stop_autoscan(struct connman_device *device)
207 {
208         struct wifi_data *wifi = connman_device_get_data(device);
209         struct autoscan_params *autoscan;
210
211         DBG("");
212
213         if (wifi == NULL || wifi->autoscan == NULL)
214                 return;
215
216         autoscan = wifi->autoscan;
217
218         if (autoscan->timeout == 0 && autoscan->interval == 0)
219                 return;
220
221         g_source_remove(autoscan->timeout);
222
223         autoscan->timeout = 0;
224         autoscan->interval = 0;
225
226         connman_device_set_scanning(device, FALSE);
227
228         connman_device_unref(device);
229 }
230
231 static void wifi_remove(struct connman_device *device)
232 {
233         struct wifi_data *wifi = connman_device_get_data(device);
234
235         DBG("device %p wifi %p", device, wifi);
236
237         if (wifi == NULL)
238                 return;
239
240         stop_autoscan(device);
241
242         /* In case of a user scan, device is still referenced */
243         if (connman_device_get_scanning(device) == TRUE)
244                 connman_device_unref(wifi->device);
245
246         iface_list = g_list_remove(iface_list, wifi);
247
248         remove_networks(device, wifi);
249
250         connman_device_set_powered(device, FALSE);
251         connman_device_set_data(device, NULL);
252         connman_device_unref(wifi->device);
253         connman_rtnl_remove_watch(wifi->watch);
254
255         g_supplicant_interface_set_data(wifi->interface, NULL);
256
257         g_free(wifi->autoscan);
258         g_free(wifi->identifier);
259         g_free(wifi);
260 }
261
262 static int add_scan_param(gchar *hex_ssid, int freq,
263                         GSupplicantScanParams *scan_data,
264                         int driver_max_scan_ssids)
265 {
266         unsigned int i;
267         struct scan_ssid *scan_ssid;
268
269         if (driver_max_scan_ssids > scan_data->num_ssids && hex_ssid != NULL) {
270                 gchar *ssid;
271                 unsigned int j = 0, hex;
272                 size_t hex_ssid_len = strlen(hex_ssid);
273
274                 ssid = g_try_malloc0(hex_ssid_len / 2);
275                 if (ssid == NULL)
276                         return -ENOMEM;
277
278                 for (i = 0; i < hex_ssid_len; i += 2) {
279                         sscanf(hex_ssid + i, "%02x", &hex);
280                         ssid[j++] = hex;
281                 }
282
283                 scan_ssid = g_try_new(struct scan_ssid, 1);
284                 if (scan_ssid == NULL) {
285                         g_free(ssid);
286                         return -ENOMEM;
287                 }
288
289                 memcpy(scan_ssid->ssid, ssid, j);
290                 scan_ssid->ssid_len = j;
291                 scan_data->ssids = g_slist_prepend(scan_data->ssids,
292                                                                 scan_ssid);
293
294                 scan_data->num_ssids++;
295
296                 g_free(ssid);
297         } else
298                 return -EINVAL;
299
300         scan_data->ssids = g_slist_reverse(scan_data->ssids);
301
302         if (scan_data->freqs == NULL) {
303                 scan_data->freqs = g_try_malloc0(sizeof(uint16_t) *
304                                                 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         } else {
310                 scan_data->freqs = g_try_realloc(scan_data->freqs,
311                                 sizeof(uint16_t) * scan_data->num_ssids);
312                 if (scan_data->freqs == NULL) {
313                         g_slist_free_full(scan_data->ssids, g_free);
314                         return -ENOMEM;
315                 }
316                 scan_data->freqs[scan_data->num_ssids - 1] = 0;
317         }
318
319         /* Don't add duplicate entries */
320         for (i = 0; i < scan_data->num_ssids; i++) {
321                 if (scan_data->freqs[i] == 0) {
322                         scan_data->freqs[i] = freq;
323                         break;
324                 } else if (scan_data->freqs[i] == freq)
325                         break;
326         }
327
328         return 0;
329 }
330
331 static int get_hidden_connections(int max_ssids,
332                                 GSupplicantScanParams *scan_data)
333 {
334         GKeyFile *keyfile;
335         gchar **services;
336         char *ssid;
337         gchar *str;
338         int i, freq;
339         gboolean value;
340         int num_ssids = 0, add_param_failed = 0;
341
342         services = connman_storage_get_services();
343         for (i = 0; services && services[i]; i++) {
344                 if (strncmp(services[i], "wifi_", 5) != 0)
345                         continue;
346
347                 keyfile = connman_storage_load_service(services[i]);
348
349                 value = g_key_file_get_boolean(keyfile,
350                                         services[i], "Hidden", 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], "Favorite", NULL);
358                 if (value == FALSE) {
359                         g_key_file_free(keyfile);
360                         continue;
361                 }
362
363                 value = g_key_file_get_boolean(keyfile,
364                                         services[i], "AutoConnect", NULL);
365                 if (value == FALSE) {
366                         g_key_file_free(keyfile);
367                         continue;
368                 }
369
370                 ssid = g_key_file_get_string(keyfile,
371                                         services[i], "SSID", NULL);
372
373                 freq = g_key_file_get_integer(keyfile, services[i],
374                                         "Frequency", NULL);
375
376                 if (add_scan_param(ssid, freq, scan_data, max_ssids) < 0) {
377                         str = g_key_file_get_string(keyfile,
378                                         services[i], "Name", NULL);
379                         DBG("Cannot scan %s (%s)", ssid, str);
380                         g_free(str);
381                         add_param_failed++;
382                 }
383
384                 num_ssids++;
385
386                 g_key_file_free(keyfile);
387         }
388
389         if (add_param_failed > 0)
390                 connman_warn("Unable to scan %d out of %d SSIDs (max is %d)",
391                         add_param_failed, num_ssids, max_ssids);
392
393         g_strfreev(services);
394
395         return num_ssids > max_ssids ? max_ssids : num_ssids;
396 }
397
398 static int throw_wifi_scan(struct connman_device *device,
399                         GSupplicantInterfaceCallback callback)
400 {
401         struct wifi_data *wifi = connman_device_get_data(device);
402         int ret;
403
404         DBG("device %p %p", device, wifi->interface);
405
406         if (wifi->tethering == TRUE)
407                 return 0;
408
409         if (connman_device_get_scanning(device) == TRUE)
410                 return -EALREADY;
411
412         connman_device_ref(device);
413
414         ret = g_supplicant_interface_scan(wifi->interface, NULL,
415                                                 callback, device);
416         if (ret == 0)
417                 connman_device_set_scanning(device, TRUE);
418         else
419                 connman_device_unref(device);
420
421         return ret;
422 }
423
424 static void hidden_scan_callback(int result,
425                         GSupplicantInterface *interface, void *user_data)
426 {
427         struct connman_device *device = user_data;
428
429         DBG("result %d", result);
430
431         connman_device_set_scanning(device, FALSE);
432         connman_device_unref(device);
433 }
434
435 static void autoscan_scan_callback(int result,
436                         GSupplicantInterface *interface, void *user_data)
437 {
438         struct connman_device *device = user_data;
439         struct wifi_data *wifi = connman_device_get_data(device);
440         int driver_max_ssids;
441
442         DBG("result %d", result);
443
444         /*
445          * Scan hidden networks so that we can autoconnect to them.
446          */
447         driver_max_ssids = g_supplicant_interface_get_max_scan_ssids(
448                                                         wifi->interface);
449         DBG("max ssids %d", driver_max_ssids);
450
451         if (driver_max_ssids > 0) {
452                 GSupplicantScanParams *scan_params;
453                 int ret;
454
455                 scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
456                 if (scan_params == NULL)
457                         goto out;
458
459                 if (get_hidden_connections(driver_max_ssids,
460                                                 scan_params) > 0) {
461                         ret = g_supplicant_interface_scan(wifi->interface,
462                                                         scan_params,
463                                                         hidden_scan_callback,
464                                                         device);
465                         if (ret == 0)
466                                 return;
467                 }
468
469                 g_supplicant_free_scan_params(scan_params);
470         }
471
472 out:
473         connman_device_set_scanning(device, FALSE);
474         connman_device_unref(device);
475 }
476
477 static gboolean autoscan_timeout(gpointer data)
478 {
479         struct connman_device *device = data;
480         struct wifi_data *wifi = connman_device_get_data(device);
481         struct autoscan_params *autoscan;
482         int interval;
483
484         autoscan = wifi->autoscan;
485
486         if (autoscan->interval <= 0) {
487                 interval = autoscan->base;
488                 goto set_interval;
489         } else
490                 interval = autoscan->interval * autoscan->base;
491
492         if (autoscan->interval >= autoscan->limit)
493                 interval = autoscan->limit;
494
495         throw_wifi_scan(wifi->device, autoscan_scan_callback);
496
497 set_interval:
498         DBG("interval %d", interval);
499
500         autoscan->interval = interval;
501
502         autoscan->timeout = g_timeout_add_seconds(interval,
503                                                 autoscan_timeout, device);
504
505         return FALSE;
506 }
507
508 static void start_autoscan(struct connman_device *device)
509 {
510         struct wifi_data *wifi = connman_device_get_data(device);
511         struct autoscan_params *autoscan;
512
513         DBG("");
514
515         if (wifi == NULL)
516                 return;
517
518         autoscan = wifi->autoscan;
519         if (autoscan == NULL)
520                 return;
521
522         if (autoscan->timeout > 0 || autoscan->interval > 0)
523                 return;
524
525         connman_device_ref(device);
526
527         autoscan_timeout(device);
528 }
529
530 static struct autoscan_params *parse_autoscan_params(const char *params)
531 {
532         struct autoscan_params *autoscan;
533         char **list_params;
534         int limit;
535         int base;
536
537         DBG("Emulating autoscan");
538
539         list_params = g_strsplit(params, ":", 0);
540         if (list_params == 0)
541                 return NULL;
542
543         if (g_strv_length(list_params) < 3) {
544                 g_strfreev(list_params);
545                 return NULL;
546         }
547
548         base = atoi(list_params[1]);
549         limit = atoi(list_params[2]);
550
551         g_strfreev(list_params);
552
553         autoscan = g_try_malloc0(sizeof(struct autoscan_params));
554         if (autoscan == NULL) {
555                 DBG("Could not allocate memory for autoscan");
556                 return NULL;
557         }
558
559         DBG("base %d - limit %d", base, limit);
560         autoscan->base = base;
561         autoscan->limit = limit;
562
563         return autoscan;
564 }
565
566 static void setup_autoscan(struct wifi_data *wifi)
567 {
568         if (wifi->autoscan == NULL)
569                 wifi->autoscan = parse_autoscan_params(AUTOSCAN_DEFAULT);
570
571         start_autoscan(wifi->device);
572 }
573
574 static void interface_create_callback(int result,
575                                         GSupplicantInterface *interface,
576                                                         void *user_data)
577 {
578         struct wifi_data *wifi = user_data;
579
580         DBG("result %d ifname %s, wifi %p", result,
581                                 g_supplicant_interface_get_ifname(interface),
582                                 wifi);
583
584         if (result < 0 || wifi == NULL)
585                 return;
586
587         wifi->interface = interface;
588         g_supplicant_interface_set_data(interface, wifi);
589
590         if (g_supplicant_interface_get_ready(interface) == FALSE)
591                 return;
592
593         DBG("interface is ready wifi %p tethering %d", wifi, wifi->tethering);
594
595         if (wifi->device == NULL) {
596                 connman_error("WiFi device not set");
597                 return;
598         }
599
600         connman_device_set_powered(wifi->device, TRUE);
601
602         if (connman_setting_get_bool("BackgroundScanning") == FALSE)
603                 return;
604
605         /* Setting up automatic scanning */
606         setup_autoscan(wifi);
607 }
608
609 static int wifi_enable(struct connman_device *device)
610 {
611         struct wifi_data *wifi = connman_device_get_data(device);
612         const char *interface = connman_device_get_string(device, "Interface");
613         const char *driver = connman_option_get_string("wifi");
614         int ret;
615
616         DBG("device %p %p", device, wifi);
617
618         ret = g_supplicant_interface_create(interface, driver, NULL,
619                                                 interface_create_callback,
620                                                         wifi);
621         if (ret < 0)
622                 return ret;
623
624         return -EINPROGRESS;
625 }
626
627 static int wifi_disable(struct connman_device *device)
628 {
629         struct wifi_data *wifi = connman_device_get_data(device);
630         int ret;
631
632         DBG("device %p", device);
633
634         wifi->connected = FALSE;
635         wifi->disconnecting = FALSE;
636
637         if (wifi->pending_network != NULL)
638                 wifi->pending_network = NULL;
639
640         remove_networks(device, wifi);
641
642         ret = g_supplicant_interface_remove(wifi->interface, NULL, NULL);
643         if (ret < 0)
644                 return ret;
645
646         return -EINPROGRESS;
647 }
648
649 static void hidden_free(struct hidden_params *hidden)
650 {
651         if (hidden == NULL)
652                 return;
653
654         g_free(hidden->identity);
655         g_free(hidden->passphrase);
656         g_free(hidden);
657 }
658
659 static void scan_callback(int result, GSupplicantInterface *interface,
660                                                 void *user_data)
661 {
662         struct connman_device *device = user_data;
663         struct wifi_data *wifi = connman_device_get_data(device);
664
665         DBG("result %d", result);
666
667         if (wifi != NULL && wifi->hidden != NULL) {
668                 connman_network_clear_hidden(wifi->hidden->user_data);
669                 hidden_free(wifi->hidden);
670                 wifi->hidden = NULL;
671         }
672
673         if (result < 0)
674                 connman_device_reset_scanning(device);
675
676         connman_device_set_scanning(device, FALSE);
677         connman_device_unref(device);
678
679         start_autoscan(device);
680 }
681
682 struct last_connected {
683         GTimeVal modified;
684         gchar *ssid;
685         int freq;
686 };
687
688 static gint sort_entry(gconstpointer a, gconstpointer b, gpointer user_data)
689 {
690         GTimeVal *aval = (GTimeVal *)a;
691         GTimeVal *bval = (GTimeVal *)b;
692
693         /* Note that the sort order is descending */
694         if (aval->tv_sec < bval->tv_sec)
695                 return 1;
696
697         if (aval->tv_sec > bval->tv_sec)
698                 return -1;
699
700         return 0;
701 }
702
703 static void free_entry(gpointer data)
704 {
705         struct last_connected *entry = data;
706
707         g_free(entry->ssid);
708         g_free(entry);
709 }
710
711 static int get_latest_connections(int max_ssids,
712                                 GSupplicantScanParams *scan_data)
713 {
714         GSequenceIter *iter;
715         GSequence *latest_list;
716         struct last_connected *entry;
717         GKeyFile *keyfile;
718         GTimeVal modified;
719         gchar **services;
720         gchar *str;
721         char *ssid;
722         int i, freq;
723         int num_ssids = 0;
724
725         latest_list = g_sequence_new(free_entry);
726         if (latest_list == NULL)
727                 return -ENOMEM;
728
729         services = connman_storage_get_services();
730         for (i = 0; services && services[i]; i++) {
731                 if (strncmp(services[i], "wifi_", 5) != 0)
732                         continue;
733
734                 keyfile = connman_storage_load_service(services[i]);
735
736                 str = g_key_file_get_string(keyfile,
737                                         services[i], "Favorite", NULL);
738                 if (str == NULL || g_strcmp0(str, "true")) {
739                         if (str)
740                                 g_free(str);
741                         g_key_file_free(keyfile);
742                         continue;
743                 }
744                 g_free(str);
745
746                 str = g_key_file_get_string(keyfile,
747                                         services[i], "AutoConnect", NULL);
748                 if (str == NULL || g_strcmp0(str, "true")) {
749                         if (str)
750                                 g_free(str);
751                         g_key_file_free(keyfile);
752                         continue;
753                 }
754                 g_free(str);
755
756                 str = g_key_file_get_string(keyfile,
757                                         services[i], "Modified", NULL);
758                 if (str != NULL) {
759                         g_time_val_from_iso8601(str, &modified);
760                         g_free(str);
761                 }
762
763                 ssid = g_key_file_get_string(keyfile,
764                                         services[i], "SSID", NULL);
765
766                 freq = g_key_file_get_integer(keyfile, services[i],
767                                         "Frequency", NULL);
768                 if (freq) {
769                         entry = g_try_new(struct last_connected, 1);
770                         if (entry == NULL) {
771                                 g_sequence_free(latest_list);
772                                 g_key_file_free(keyfile);
773                                 g_free(ssid);
774                                 return -ENOMEM;
775                         }
776
777                         entry->ssid = ssid;
778                         entry->modified = modified;
779                         entry->freq = freq;
780
781                         g_sequence_insert_sorted(latest_list, entry,
782                                                 sort_entry, NULL);
783                         num_ssids++;
784                 } else
785                         g_free(ssid);
786
787                 g_key_file_free(keyfile);
788         }
789
790         g_strfreev(services);
791
792         num_ssids = num_ssids > max_ssids ? max_ssids : num_ssids;
793
794         iter = g_sequence_get_begin_iter(latest_list);
795
796         for (i = 0; i < num_ssids; i++) {
797                 entry = g_sequence_get(iter);
798
799                 DBG("ssid %s freq %d modified %lu", entry->ssid, entry->freq,
800                                                 entry->modified.tv_sec);
801
802                 add_scan_param(entry->ssid, entry->freq, scan_data, max_ssids);
803
804                 iter = g_sequence_iter_next(iter);
805         }
806
807         g_sequence_free(latest_list);
808         return num_ssids;
809 }
810
811 static int wifi_scan(struct connman_device *device)
812 {
813         stop_autoscan(device);
814
815         return throw_wifi_scan(device, scan_callback);
816 }
817
818 static int wifi_scan_fast(struct connman_device *device)
819 {
820         struct wifi_data *wifi = connman_device_get_data(device);
821         GSupplicantScanParams *scan_params = NULL;
822         int ret;
823         int driver_max_ssids = 0;
824
825         DBG("device %p %p", device, wifi->interface);
826
827         if (wifi->tethering == TRUE)
828                 return 0;
829
830         stop_autoscan(device);
831
832         if (connman_device_get_scanning(device) == TRUE)
833                 return -EALREADY;
834
835         driver_max_ssids = g_supplicant_interface_get_max_scan_ssids(
836                                                         wifi->interface);
837         DBG("max ssids %d", driver_max_ssids);
838         if (driver_max_ssids == 0)
839                 return wifi_scan(device);
840
841         scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
842         if (scan_params == NULL)
843                 return -ENOMEM;
844
845         ret = get_latest_connections(driver_max_ssids, scan_params);
846         if (ret <= 0) {
847                 g_supplicant_free_scan_params(scan_params);
848                 return wifi_scan(device);
849         }
850
851         connman_device_ref(device);
852         ret = g_supplicant_interface_scan(wifi->interface, scan_params,
853                                                 scan_callback, device);
854         if (ret == 0)
855                 connman_device_set_scanning(device, TRUE);
856         else {
857                 g_supplicant_free_scan_params(scan_params);
858                 connman_device_unref(device);
859         }
860
861         return ret;
862 }
863
864 /*
865  * This func is only used when connecting to this specific AP first time.
866  * It is not used when system autoconnects to hidden AP.
867  */
868 static int wifi_scan_hidden(struct connman_device *device,
869                 const char *ssid, unsigned int ssid_len,
870                 const char *identity, const char* passphrase,
871                 gpointer user_data)
872 {
873         struct wifi_data *wifi = connman_device_get_data(device);
874         GSupplicantScanParams *scan_params = NULL;
875         struct scan_ssid *scan_ssid;
876         struct hidden_params *hidden;
877         int ret;
878
879         DBG("hidden SSID %s", ssid);
880
881         if (wifi->tethering == TRUE || wifi->hidden != NULL)
882                 return -EBUSY;
883
884         if (ssid == NULL || ssid_len == 0 || ssid_len > 32)
885                 return -EINVAL;
886
887         stop_autoscan(device);
888
889         if (connman_device_get_scanning(device) == TRUE)
890                 return -EALREADY;
891
892         scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
893         if (scan_params == NULL)
894                 return -ENOMEM;
895
896         scan_ssid = g_try_new(struct scan_ssid, 1);
897         if (scan_ssid == NULL) {
898                 g_free(scan_params);
899                 return -ENOMEM;
900         }
901
902         memcpy(scan_ssid->ssid, ssid, ssid_len);
903         scan_ssid->ssid_len = ssid_len;
904         scan_params->ssids = g_slist_prepend(scan_params->ssids, scan_ssid);
905
906         scan_params->num_ssids = 1;
907
908         hidden = g_try_new0(struct hidden_params, 1);
909         if (hidden == NULL) {
910                 g_free(scan_params);
911                 return -ENOMEM;
912         }
913         memcpy(hidden->ssid, ssid, ssid_len);
914         hidden->ssid_len = ssid_len;
915         hidden->identity = g_strdup(identity);
916         hidden->passphrase = g_strdup(passphrase);
917         hidden->user_data = user_data;
918         wifi->hidden = hidden;
919
920         connman_device_ref(device);
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                 connman_error("Wrong wifi pointer");
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)