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