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