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