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