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