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