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