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