gsupplicant: Add network mode support
[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 <string.h>
30 #include <sys/ioctl.h>
31 #include <sys/socket.h>
32 #include <linux/if_arp.h>
33 #include <linux/wireless.h>
34 #include <net/ethernet.h>
35
36 #ifndef IFF_LOWER_UP
37 #define IFF_LOWER_UP    0x10000
38 #endif
39
40 #include <dbus/dbus.h>
41 #include <glib.h>
42
43 #define CONNMAN_API_SUBJECT_TO_CHANGE
44 #include <connman/plugin.h>
45 #include <connman/inet.h>
46 #include <connman/device.h>
47 #include <connman/rtnl.h>
48 #include <connman/technology.h>
49 #include <connman/log.h>
50 #include <connman/option.h>
51
52 #include <gsupplicant/gsupplicant.h>
53
54 #define CLEANUP_TIMEOUT   8     /* in seconds */
55 #define INACTIVE_TIMEOUT  12    /* in seconds */
56
57 struct connman_technology *wifi_technology = NULL;
58
59 struct wifi_data {
60         char *identifier;
61         struct connman_device *device;
62         struct connman_network *network;
63         struct connman_network *pending_network;
64         GSupplicantInterface *interface;
65         GSupplicantState state;
66         connman_bool_t connected;
67         connman_bool_t disconnecting;
68         int index;
69         unsigned flags;
70         unsigned int watch;
71 };
72
73 static int get_bssid(struct connman_device *device,
74                                 unsigned char *bssid, unsigned int *bssid_len)
75 {
76         struct iwreq wrq;
77         char *ifname;
78         int ifindex;
79         int fd, err;
80
81         ifindex = connman_device_get_index(device);
82         if (ifindex < 0)
83                 return -EINVAL;
84
85         ifname = connman_inet_ifname(ifindex);
86         if (ifname == NULL)
87                 return -EINVAL;
88
89         fd = socket(PF_INET, SOCK_DGRAM, 0);
90         if (fd < 0) {
91                 g_free(ifname);
92                 return -EINVAL;
93         }
94
95         memset(&wrq, 0, sizeof(wrq));
96         strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
97
98         err = ioctl(fd, SIOCGIWAP, &wrq);
99
100         g_free(ifname);
101         close(fd);
102
103         if (err < 0)
104                 return -EIO;
105
106         memcpy(bssid, wrq.u.ap_addr.sa_data, ETH_ALEN);
107         *bssid_len = ETH_ALEN;
108
109         return 0;
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                 else
133                         DBG("carrier off");
134         }
135
136         wifi->flags = flags;
137 }
138
139 static int wifi_probe(struct connman_device *device)
140 {
141         struct wifi_data *wifi;
142
143         DBG("device %p", device);
144
145         wifi = g_try_new0(struct wifi_data, 1);
146         if (wifi == NULL)
147                 return -ENOMEM;
148
149         wifi->connected = FALSE;
150         wifi->disconnecting = FALSE;
151         wifi->state = G_SUPPLICANT_STATE_INACTIVE;
152
153         connman_device_set_data(device, wifi);
154         wifi->device = connman_device_ref(device);
155
156         wifi->index = connman_device_get_index(device);
157         wifi->flags = 0;
158
159         wifi->watch = connman_rtnl_add_newlink_watch(wifi->index,
160                                                         wifi_newlink, device);
161
162         return 0;
163 }
164
165 static void wifi_remove(struct connman_device *device)
166 {
167         struct wifi_data *wifi = connman_device_get_data(device);
168
169         DBG("device %p", device);
170
171         if (wifi->pending_network != NULL) {
172                 connman_network_unref(wifi->pending_network);
173                 wifi->pending_network = NULL;
174         }
175
176         connman_device_set_data(device, NULL);
177         connman_device_unref(wifi->device);
178         connman_rtnl_remove_watch(wifi->watch);
179
180         g_supplicant_interface_set_data(wifi->interface, NULL);
181
182         g_free(wifi->identifier);
183         g_free(wifi);
184 }
185
186 static void interface_create_callback(int result,
187                                         GSupplicantInterface *interface,
188                                                         void *user_data)
189 {
190         struct wifi_data *wifi = user_data;
191
192         DBG("result %d ifname %s", result,
193                                 g_supplicant_interface_get_ifname(interface));
194
195         if (result < 0)
196                 return;
197
198         wifi->interface = interface;
199         g_supplicant_interface_set_data(interface, wifi);
200 }
201
202 static void interface_remove_callback(int result,
203                                         GSupplicantInterface *interface,
204                                                         void *user_data)
205 {
206         struct wifi_data *wifi = user_data;
207
208         DBG("result %d", result);
209
210         if (result < 0)
211                 return;
212
213         wifi->interface = NULL;
214 }
215
216
217 static int wifi_enable(struct connman_device *device)
218 {
219         struct wifi_data *wifi = connman_device_get_data(device);
220         const char *interface = connman_device_get_string(device, "Interface");
221         const char *driver = connman_option_get_string("wifi");
222
223         DBG("device %p %p", device, wifi);
224
225         return g_supplicant_interface_create(interface, driver,
226                                                 interface_create_callback,
227                                                         wifi);
228 }
229
230 static int wifi_disable(struct connman_device *device)
231 {
232         struct wifi_data *wifi = connman_device_get_data(device);
233
234         DBG("device %p", device);
235
236         wifi->connected = FALSE;
237         wifi->disconnecting = FALSE;
238
239         if (wifi->pending_network != NULL) {
240                 connman_network_unref(wifi->pending_network);
241                 wifi->pending_network = NULL;
242         }
243
244         return g_supplicant_interface_remove(wifi->interface,
245                                                 interface_remove_callback,
246                                                         wifi);
247 }
248
249 static void scan_callback(int result, GSupplicantInterface *interface,
250                                                 void *user_data)
251 {
252         struct connman_device *device = user_data;
253
254         DBG("result %d", result);
255
256         if (result < 0)
257                 connman_device_reset_scanning(device);
258         else
259                 connman_device_set_scanning(device, FALSE);
260 }
261
262 static int wifi_scan(struct connman_device *device)
263 {
264         struct wifi_data *wifi = connman_device_get_data(device);
265         int ret;
266
267         DBG("device %p %p", device, wifi->interface);
268
269         ret = g_supplicant_interface_scan(wifi->interface, scan_callback,
270                                                                 device);
271         if (ret == 0)
272                 connman_device_set_scanning(device, TRUE);
273
274         return ret;
275 }
276
277 static struct connman_device_driver wifi_ng_driver = {
278         .name           = "wifi",
279         .type           = CONNMAN_DEVICE_TYPE_WIFI,
280         .priority       = CONNMAN_DEVICE_PRIORITY_LOW,
281         .probe          = wifi_probe,
282         .remove         = wifi_remove,
283         .enable         = wifi_enable,
284         .disable        = wifi_disable,
285         .scan           = wifi_scan,
286 };
287
288 static void system_ready(void)
289 {
290         DBG("");
291
292         if (connman_device_driver_register(&wifi_ng_driver) < 0)
293                 connman_error("Failed to register WiFi driver");
294 }
295
296 static void system_killed(void)
297 {
298         DBG("");
299
300         connman_device_driver_unregister(&wifi_ng_driver);
301 }
302
303 static int network_probe(struct connman_network *network)
304 {
305         DBG("network %p", network);
306
307         return 0;
308 }
309
310 static void network_remove(struct connman_network *network)
311 {
312         DBG("network %p", network);
313 }
314
315 static void connect_callback(int result, GSupplicantInterface *interface,
316                                                         void *user_data)
317 {
318         connman_error("%s", __func__);
319 }
320
321 static GSupplicantSecurity network_security(const char *security)
322 {
323         if (g_str_equal(security, "none") == TRUE)
324                 return G_SUPPLICANT_SECURITY_NONE;
325         else if (g_str_equal(security, "wep") == TRUE)
326                 return G_SUPPLICANT_SECURITY_WEP;
327         else if (g_str_equal(security, "psk") == TRUE)
328                 return G_SUPPLICANT_SECURITY_PSK;
329         else if (g_str_equal(security, "wpa") == TRUE)
330                 return G_SUPPLICANT_SECURITY_PSK;
331         else if (g_str_equal(security, "rsn") == TRUE)
332                 return G_SUPPLICANT_SECURITY_PSK;
333         else if (g_str_equal(security, "ieee8021x") == TRUE)
334                 return G_SUPPLICANT_SECURITY_IEEE8021X;
335
336         return G_SUPPLICANT_SECURITY_UNKNOWN;
337 }
338
339 static void ssid_init(GSupplicantSSID *ssid, struct connman_network *network)
340 {
341         const char *security, *passphrase;
342
343         memset(ssid, 0, sizeof(*ssid));
344         ssid->mode = G_SUPPLICANT_MODE_INFRA;
345         ssid->ssid = connman_network_get_blob(network, "WiFi.SSID",
346                                                 &ssid->ssid_len);
347         security = connman_network_get_string(network, "WiFi.Security");
348         ssid->security = network_security(security);
349         passphrase = connman_network_get_string(network,
350                                                 "WiFi.Passphrase");
351         if (passphrase == NULL || strlen(passphrase) == 0)
352                 ssid->passphrase = NULL;
353         else
354                 ssid->passphrase = passphrase;
355
356         ssid->eap = connman_network_get_string(network, "WiFi.EAP");
357
358         /*
359          * If our private key password is unset,
360          * we use the supplied passphrase. That is needed
361          * for PEAP where 2 passphrases (identity and client
362          * cert may have to be provided.
363          */
364         if (connman_network_get_string(network,
365                                         "WiFi.PrivateKeyPassphrase") == NULL)
366                 connman_network_set_string(network,
367                                                 "WiFi.PrivateKeyPassphrase",
368                                                 ssid->passphrase);
369         /* We must have an identity for both PEAP and TLS */
370         ssid->identity = connman_network_get_string(network, "WiFi.Identity");
371         ssid->ca_cert_path = connman_network_get_string(network,
372                                                         "WiFi.CACertFile");
373         ssid->client_cert_path = connman_network_get_string(network,
374                                                         "WiFi.ClientCertFile");
375         ssid->private_key_path = connman_network_get_string(network,
376                                                         "WiFi.PrivateKeyFile");
377         ssid->private_key_passphrase = connman_network_get_string(network,
378                                                 "WiFi.PrivateKeyPassphrase");
379         ssid->phase2_auth = connman_network_get_string(network, "WiFi.Phase2");
380
381         ssid->use_wps = connman_network_get_bool(network, "WiFi.UseWPS");
382         ssid->pin_wps = connman_network_get_string(network, "WiFi.PinWPS");
383
384 }
385
386 static int network_connect(struct connman_network *network)
387 {
388         struct connman_device *device = connman_network_get_device(network);
389         struct wifi_data *wifi;
390         GSupplicantInterface *interface;
391         GSupplicantSSID *ssid;
392
393         DBG("network %p", network);
394
395         if (device == NULL)
396                 return -ENODEV;
397
398         wifi = connman_device_get_data(device);
399         if (wifi == NULL)
400                 return -ENODEV;
401
402         ssid = g_try_malloc0(sizeof(GSupplicantSSID));
403         if (ssid == NULL)
404                 return -ENOMEM;
405
406         interface = wifi->interface;
407
408         ssid_init(ssid, network);
409
410         if (wifi->disconnecting == TRUE)
411                 wifi->pending_network = connman_network_ref(network);
412         else {
413                 wifi->network = connman_network_ref(network);
414
415                 return g_supplicant_interface_connect(interface, ssid,
416                                                 connect_callback, NULL);
417         }
418
419         return -EINPROGRESS;
420 }
421
422 static void disconnect_callback(int result, GSupplicantInterface *interface,
423                                                                 void *user_data)
424 {
425         struct wifi_data *wifi = user_data;
426
427         if (wifi->network != NULL) {
428                 /*
429                  * if result < 0 supplican return an error because
430                  * the network is not current.
431                  * we wont receive G_SUPPLICANT_STATE_DISCONNECTED since it
432                  * failed, call connman_network_set_connected to report
433                  * disconnect is completed.
434                  */
435                 if (result < 0)
436                         connman_network_set_connected(wifi->network, FALSE);
437
438                 connman_network_unref(wifi->network);
439         }
440
441         wifi->network = NULL;
442
443         wifi->disconnecting = FALSE;
444
445         if (wifi->pending_network != NULL) {
446                 network_connect(wifi->pending_network);
447                 connman_network_unref(wifi->pending_network);
448                 wifi->pending_network = NULL;
449         }
450
451 }
452
453 static int network_disconnect(struct connman_network *network)
454 {
455         struct connman_device *device = connman_network_get_device(network);
456         struct wifi_data *wifi;
457         int err;
458
459         DBG("network %p", network);
460
461         wifi = connman_device_get_data(device);
462         if (wifi == NULL || wifi->interface == NULL)
463                 return -ENODEV;
464
465         connman_network_set_associating(network, FALSE);
466
467         if (wifi->disconnecting == TRUE)
468                 return -EALREADY;
469
470         wifi->disconnecting = TRUE;
471
472         err = g_supplicant_interface_disconnect(wifi->interface,
473                                                 disconnect_callback, wifi);
474         if (err < 0)
475                 wifi->disconnecting = FALSE;
476
477         return err;
478 }
479
480 static struct connman_network_driver network_driver = {
481         .name           = "wifi",
482         .type           = CONNMAN_NETWORK_TYPE_WIFI,
483         .priority       = CONNMAN_NETWORK_PRIORITY_LOW,
484         .probe          = network_probe,
485         .remove         = network_remove,
486         .connect        = network_connect,
487         .disconnect     = network_disconnect,
488 };
489
490 static void interface_added(GSupplicantInterface *interface)
491 {
492         const char *ifname = g_supplicant_interface_get_ifname(interface);
493         const char *driver = g_supplicant_interface_get_driver(interface);
494         struct wifi_data *wifi;
495
496         wifi = g_supplicant_interface_get_data(interface);
497
498         /*
499          * We can get here with a NULL wifi pointer when
500          * the interface added signal is sent before the
501          * interface creation callback is called.
502          */
503         if (wifi == NULL)
504                 return;
505
506         DBG("ifname %s driver %s wifi %p", ifname, driver, wifi);
507
508         if (wifi->device == NULL) {
509                 connman_error("WiFi device not set");
510                 return;
511         }
512
513         connman_device_set_powered(wifi->device, TRUE);
514         wifi_scan(wifi->device);
515 }
516
517 static connman_bool_t is_idle(struct wifi_data *wifi)
518 {
519         DBG("state %d", wifi->state);
520
521         switch (wifi->state) {
522         case G_SUPPLICANT_STATE_UNKNOWN:
523         case G_SUPPLICANT_STATE_DISCONNECTED:
524         case G_SUPPLICANT_STATE_INACTIVE:
525         case G_SUPPLICANT_STATE_SCANNING:
526                 return TRUE;
527
528         case G_SUPPLICANT_STATE_AUTHENTICATING:
529         case G_SUPPLICANT_STATE_ASSOCIATING:
530         case G_SUPPLICANT_STATE_ASSOCIATED:
531         case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
532         case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
533         case G_SUPPLICANT_STATE_COMPLETED:
534                 return FALSE;
535         }
536
537         return FALSE;
538 }
539
540 static connman_bool_t is_idle_wps(GSupplicantInterface *interface,
541                                                 struct wifi_data *wifi)
542 {
543         /* First, let's check if WPS processing did not went wrong */
544         if (g_supplicant_interface_get_wps_state(interface) ==
545                 G_SUPPLICANT_WPS_STATE_FAIL)
546                 return FALSE;
547
548         /* Unlike normal connection, being associated while processing wps
549          * actually means that we are idling. */
550         switch (wifi->state) {
551         case G_SUPPLICANT_STATE_UNKNOWN:
552         case G_SUPPLICANT_STATE_DISCONNECTED:
553         case G_SUPPLICANT_STATE_INACTIVE:
554         case G_SUPPLICANT_STATE_SCANNING:
555         case G_SUPPLICANT_STATE_ASSOCIATED:
556                 return TRUE;
557         case G_SUPPLICANT_STATE_AUTHENTICATING:
558         case G_SUPPLICANT_STATE_ASSOCIATING:
559         case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
560         case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
561         case G_SUPPLICANT_STATE_COMPLETED:
562                 return FALSE;
563         }
564
565         return FALSE;
566 }
567
568 static connman_bool_t handle_wps_completion(GSupplicantInterface *interface,
569                                         struct connman_network *network,
570                                         struct connman_device *device,
571                                         struct wifi_data *wifi)
572 {
573         connman_bool_t wps;
574
575         wps = connman_network_get_bool(network, "WiFi.UseWPS");
576         if (wps == TRUE) {
577                 const unsigned char *ssid, *wps_ssid;
578                 unsigned int ssid_len, wps_ssid_len;
579                 const char *wps_key;
580
581                 /* Checking if we got associated with requested
582                  * network */
583                 ssid = connman_network_get_blob(network, "WiFi.SSID",
584                                                 &ssid_len);
585
586                 wps_ssid = g_supplicant_interface_get_wps_ssid(
587                         interface, &wps_ssid_len);
588
589                 if (wps_ssid == NULL || wps_ssid_len != ssid_len ||
590                                 memcmp(ssid, wps_ssid, ssid_len) != 0) {
591                         connman_network_set_associating(network, FALSE);
592                         g_supplicant_interface_disconnect(wifi->interface,
593                                                 disconnect_callback, wifi);
594                         return FALSE;
595                 }
596
597                 wps_key = g_supplicant_interface_get_wps_key(interface);
598                 connman_network_set_string(network, "WiFi.Passphrase",
599                                         wps_key);
600
601                 connman_network_set_string(network, "WiFi.PinWPS", NULL);
602         }
603
604         return TRUE;
605 }
606
607 static void interface_state(GSupplicantInterface *interface)
608 {
609         struct connman_network *network;
610         struct connman_device *device;
611         struct wifi_data *wifi;
612         GSupplicantState state = g_supplicant_interface_get_state(interface);
613         unsigned char bssid[ETH_ALEN];
614         unsigned int bssid_len;
615         connman_bool_t wps;
616
617         wifi = g_supplicant_interface_get_data(interface);
618
619         DBG("wifi %p interface state %d", wifi, state);
620
621         if (wifi == NULL)
622                 return;
623
624         network = wifi->network;
625         device = wifi->device;
626
627         if (device == NULL || network == NULL)
628                 return;
629
630         switch (state) {
631         case G_SUPPLICANT_STATE_SCANNING:
632                 break;
633
634         case G_SUPPLICANT_STATE_AUTHENTICATING:
635         case G_SUPPLICANT_STATE_ASSOCIATING:
636                 connman_network_set_associating(network, TRUE);
637                 break;
638
639         case G_SUPPLICANT_STATE_COMPLETED:
640                 if (handle_wps_completion(interface, network, device, wifi) ==
641                                                                         FALSE)
642                         break;
643
644                 /* reset scan trigger and schedule background scan */
645                 connman_device_schedule_scan(device);
646
647                 if (get_bssid(device, bssid, &bssid_len) == 0)
648                         connman_network_set_address(network,
649                                                         bssid, bssid_len);
650                 connman_network_set_connected(network, TRUE);
651                 break;
652
653         case G_SUPPLICANT_STATE_DISCONNECTED:
654                 /*
655                  * If we're in one of the idle modes, we have
656                  * not started association yet and thus setting
657                  * those ones to FALSE could cancel an association
658                  * in progress.
659                  */
660                 wps = connman_network_get_bool(network, "WiFi.UseWPS");
661                 if (wps == TRUE)
662                         if (is_idle_wps(interface, wifi) == TRUE)
663                                 break;
664
665                 if (is_idle(wifi))
666                         break;
667                 connman_network_set_associating(network, FALSE);
668                 connman_network_set_connected(network, FALSE);
669                 break;
670
671         case G_SUPPLICANT_STATE_INACTIVE:
672                 connman_network_set_associating(network, FALSE);
673                 break;
674
675         case G_SUPPLICANT_STATE_UNKNOWN:
676         case G_SUPPLICANT_STATE_ASSOCIATED:
677         case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
678         case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
679                 break;
680         }
681
682         wifi->state = state;
683
684         DBG("DONE");
685 }
686
687 static void interface_removed(GSupplicantInterface *interface)
688 {
689         const char *ifname = g_supplicant_interface_get_ifname(interface);
690         struct wifi_data *wifi;
691
692         DBG("ifname %s", ifname);
693
694         wifi = g_supplicant_interface_get_data(interface);
695
696         if (wifi == NULL || wifi->device == NULL) {
697                 connman_error("Wrong wifi pointer");
698                 return;
699         }
700
701         connman_device_set_powered(wifi->device, FALSE);
702 }
703
704 static void scan_started(GSupplicantInterface *interface)
705 {
706         struct wifi_data *wifi;
707
708         DBG("");
709
710         wifi = g_supplicant_interface_get_data(interface);
711
712         if (wifi == NULL)
713                 return;
714 }
715
716 static void scan_finished(GSupplicantInterface *interface)
717 {
718         struct wifi_data *wifi;
719
720         DBG("");
721
722         wifi = g_supplicant_interface_get_data(interface);
723
724         if (wifi == NULL)
725                 return;
726 }
727
728 static unsigned char calculate_strength(GSupplicantNetwork *supplicant_network)
729 {
730         unsigned char strength;
731
732         strength = 120 + g_supplicant_network_get_signal(supplicant_network);
733         if (strength > 100)
734                 strength = 100;
735
736         return strength;
737 }
738
739 static void network_added(GSupplicantNetwork *supplicant_network)
740 {
741         struct connman_network *network;
742         GSupplicantInterface *interface;
743         struct wifi_data *wifi;
744         const char *name, *identifier, *mode, *security, *group;
745         const unsigned char *ssid;
746         unsigned int ssid_len;
747         connman_bool_t wps;
748
749         DBG("");
750
751         interface = g_supplicant_network_get_interface(supplicant_network);
752         wifi = g_supplicant_interface_get_data(interface);
753         name = g_supplicant_network_get_name(supplicant_network);
754         identifier = g_supplicant_network_get_identifier(supplicant_network);
755         mode = g_supplicant_network_get_mode(supplicant_network);
756         security = g_supplicant_network_get_security(supplicant_network);
757         group = g_supplicant_network_get_identifier(supplicant_network);
758         wps = g_supplicant_network_get_wps(supplicant_network);
759
760         if (wifi == NULL)
761                 return;
762
763         ssid = g_supplicant_network_get_ssid(supplicant_network, &ssid_len);
764
765         network = connman_device_get_network(wifi->device, identifier);
766
767         if (network == NULL) {
768                 network = connman_network_create(identifier,
769                                                 CONNMAN_NETWORK_TYPE_WIFI);
770                 if (network == NULL)
771                         return;
772
773                 connman_network_set_index(network, wifi->index);
774
775                 if (connman_device_add_network(wifi->device, network) < 0) {
776                         connman_network_unref(network);
777                         return;
778                 }
779         }
780
781         if (name != NULL && name[0] != '\0')
782                 connman_network_set_name(network, name);
783
784         connman_network_set_blob(network, "WiFi.SSID",
785                                                 ssid, ssid_len);
786         connman_network_set_string(network, "WiFi.Mode", mode);
787         connman_network_set_string(network, "WiFi.Security", security);
788         connman_network_set_strength(network,
789                                 calculate_strength(supplicant_network));
790         connman_network_set_bool(network, "WiFi.WPS", wps);
791
792         connman_network_set_available(network, TRUE);
793
794         if (ssid != NULL)
795                 connman_network_set_group(network, group);
796 }
797
798 static void network_removed(GSupplicantNetwork *network)
799 {
800         GSupplicantInterface *interface;
801         struct wifi_data *wifi;
802         const char *name, *identifier;
803
804         interface = g_supplicant_network_get_interface(network);
805         wifi = g_supplicant_interface_get_data(interface);
806         identifier = g_supplicant_network_get_identifier(network);
807         name = g_supplicant_network_get_name(network);
808
809         DBG("name %s", name);
810
811         if (wifi != NULL)
812                 connman_device_remove_network(wifi->device, identifier);
813 }
814
815 static void debug(const char *str)
816 {
817         if (getenv("CONNMAN_SUPPLICANT_DEBUG"))
818                 connman_debug("%s", str);
819 }
820
821 static const GSupplicantCallbacks callbacks = {
822         .system_ready           = system_ready,
823         .system_killed          = system_killed,
824         .interface_added        = interface_added,
825         .interface_state        = interface_state,
826         .interface_removed      = interface_removed,
827         .scan_started           = scan_started,
828         .scan_finished          = scan_finished,
829         .network_added          = network_added,
830         .network_removed        = network_removed,
831         .debug                  = debug,
832 };
833
834
835 static int tech_probe(struct connman_technology *technology)
836 {
837         wifi_technology = technology;
838
839         return 0;
840 }
841
842 static void tech_remove(struct connman_technology *technology)
843 {
844         wifi_technology = NULL;
845 }
846
847 static void regdom_callback(void *user_data)
848 {
849         char *alpha2 = user_data;
850
851         DBG("");
852
853         if (wifi_technology == NULL)
854                 return;
855
856         connman_technology_regdom_notify(wifi_technology, alpha2);
857 }
858
859 static int tech_set_regdom(struct connman_technology *technology, const char *alpha2)
860 {
861         return g_supplicant_set_country(alpha2, regdom_callback, alpha2);
862 }
863
864 static struct connman_technology_driver tech_driver = {
865         .name           = "wifi",
866         .type           = CONNMAN_SERVICE_TYPE_WIFI,
867         .probe          = tech_probe,
868         .remove         = tech_remove,
869         .set_regdom     = tech_set_regdom,
870 };
871
872 static int wifi_init(void)
873 {
874         int err;
875
876         err = connman_network_driver_register(&network_driver);
877         if (err < 0)
878                 return err;
879
880         err = g_supplicant_register(&callbacks);
881         if (err < 0) {
882                 connman_network_driver_unregister(&network_driver);
883                 return err;
884         }
885
886         err = connman_technology_driver_register(&tech_driver);
887         if (err < 0) {
888                 g_supplicant_unregister(&callbacks);
889                 connman_network_driver_unregister(&network_driver);
890                 return err;
891         }
892
893         return 0;
894 }
895
896 static void wifi_exit(void)
897 {
898         DBG();
899
900         connman_technology_driver_unregister(&tech_driver);
901
902         g_supplicant_unregister(&callbacks);
903
904         connman_network_driver_unregister(&network_driver);
905 }
906
907 CONNMAN_PLUGIN_DEFINE(wifi, "WiFi interface plugin", VERSION,
908                 CONNMAN_PLUGIN_PRIORITY_DEFAULT, wifi_init, wifi_exit)