gsupplicant: Add network scan ssid setting
[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         ssid->scan_ssid = 1;
357         security = connman_network_get_string(network, "WiFi.Security");
358         ssid->security = network_security(security);
359         passphrase = connman_network_get_string(network,
360                                                 "WiFi.Passphrase");
361         if (passphrase == NULL || strlen(passphrase) == 0)
362                 ssid->passphrase = NULL;
363         else
364                 ssid->passphrase = passphrase;
365
366         ssid->eap = connman_network_get_string(network, "WiFi.EAP");
367
368         /*
369          * If our private key password is unset,
370          * we use the supplied passphrase. That is needed
371          * for PEAP where 2 passphrases (identity and client
372          * cert may have to be provided.
373          */
374         if (connman_network_get_string(network,
375                                         "WiFi.PrivateKeyPassphrase") == NULL)
376                 connman_network_set_string(network,
377                                                 "WiFi.PrivateKeyPassphrase",
378                                                 ssid->passphrase);
379         /* We must have an identity for both PEAP and TLS */
380         ssid->identity = connman_network_get_string(network, "WiFi.Identity");
381         ssid->ca_cert_path = connman_network_get_string(network,
382                                                         "WiFi.CACertFile");
383         ssid->client_cert_path = connman_network_get_string(network,
384                                                         "WiFi.ClientCertFile");
385         ssid->private_key_path = connman_network_get_string(network,
386                                                         "WiFi.PrivateKeyFile");
387         ssid->private_key_passphrase = connman_network_get_string(network,
388                                                 "WiFi.PrivateKeyPassphrase");
389         ssid->phase2_auth = connman_network_get_string(network, "WiFi.Phase2");
390
391         ssid->use_wps = connman_network_get_bool(network, "WiFi.UseWPS");
392         ssid->pin_wps = connman_network_get_string(network, "WiFi.PinWPS");
393
394 }
395
396 static int network_connect(struct connman_network *network)
397 {
398         struct connman_device *device = connman_network_get_device(network);
399         struct wifi_data *wifi;
400         GSupplicantInterface *interface;
401         GSupplicantSSID *ssid;
402
403         DBG("network %p", network);
404
405         if (device == NULL)
406                 return -ENODEV;
407
408         wifi = connman_device_get_data(device);
409         if (wifi == NULL)
410                 return -ENODEV;
411
412         ssid = g_try_malloc0(sizeof(GSupplicantSSID));
413         if (ssid == NULL)
414                 return -ENOMEM;
415
416         interface = wifi->interface;
417
418         ssid_init(ssid, network);
419
420         if (wifi->disconnecting == TRUE)
421                 wifi->pending_network = connman_network_ref(network);
422         else {
423                 wifi->network = connman_network_ref(network);
424
425                 return g_supplicant_interface_connect(interface, ssid,
426                                                 connect_callback, NULL);
427         }
428
429         return -EINPROGRESS;
430 }
431
432 static void disconnect_callback(int result, GSupplicantInterface *interface,
433                                                                 void *user_data)
434 {
435         struct wifi_data *wifi = user_data;
436
437         if (wifi->network != NULL) {
438                 /*
439                  * if result < 0 supplican return an error because
440                  * the network is not current.
441                  * we wont receive G_SUPPLICANT_STATE_DISCONNECTED since it
442                  * failed, call connman_network_set_connected to report
443                  * disconnect is completed.
444                  */
445                 if (result < 0)
446                         connman_network_set_connected(wifi->network, FALSE);
447
448                 connman_network_unref(wifi->network);
449         }
450
451         wifi->network = NULL;
452
453         wifi->disconnecting = FALSE;
454
455         if (wifi->pending_network != NULL) {
456                 network_connect(wifi->pending_network);
457                 connman_network_unref(wifi->pending_network);
458                 wifi->pending_network = NULL;
459         }
460
461 }
462
463 static int network_disconnect(struct connman_network *network)
464 {
465         struct connman_device *device = connman_network_get_device(network);
466         struct wifi_data *wifi;
467         int err;
468
469         DBG("network %p", network);
470
471         wifi = connman_device_get_data(device);
472         if (wifi == NULL || wifi->interface == NULL)
473                 return -ENODEV;
474
475         connman_network_set_associating(network, FALSE);
476
477         if (wifi->disconnecting == TRUE)
478                 return -EALREADY;
479
480         wifi->disconnecting = TRUE;
481
482         err = g_supplicant_interface_disconnect(wifi->interface,
483                                                 disconnect_callback, wifi);
484         if (err < 0)
485                 wifi->disconnecting = FALSE;
486
487         return err;
488 }
489
490 static struct connman_network_driver network_driver = {
491         .name           = "wifi",
492         .type           = CONNMAN_NETWORK_TYPE_WIFI,
493         .priority       = CONNMAN_NETWORK_PRIORITY_LOW,
494         .probe          = network_probe,
495         .remove         = network_remove,
496         .connect        = network_connect,
497         .disconnect     = network_disconnect,
498 };
499
500 static void interface_added(GSupplicantInterface *interface)
501 {
502         const char *ifname = g_supplicant_interface_get_ifname(interface);
503         const char *driver = g_supplicant_interface_get_driver(interface);
504         struct wifi_data *wifi;
505
506         wifi = g_supplicant_interface_get_data(interface);
507
508         /*
509          * We can get here with a NULL wifi pointer when
510          * the interface added signal is sent before the
511          * interface creation callback is called.
512          */
513         if (wifi == NULL)
514                 return;
515
516         DBG("ifname %s driver %s wifi %p", ifname, driver, wifi);
517
518         if (wifi->device == NULL) {
519                 connman_error("WiFi device not set");
520                 return;
521         }
522
523         connman_device_set_powered(wifi->device, TRUE);
524         wifi_scan(wifi->device);
525 }
526
527 static connman_bool_t is_idle(struct wifi_data *wifi)
528 {
529         DBG("state %d", wifi->state);
530
531         switch (wifi->state) {
532         case G_SUPPLICANT_STATE_UNKNOWN:
533         case G_SUPPLICANT_STATE_DISCONNECTED:
534         case G_SUPPLICANT_STATE_INACTIVE:
535         case G_SUPPLICANT_STATE_SCANNING:
536                 return TRUE;
537
538         case G_SUPPLICANT_STATE_AUTHENTICATING:
539         case G_SUPPLICANT_STATE_ASSOCIATING:
540         case G_SUPPLICANT_STATE_ASSOCIATED:
541         case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
542         case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
543         case G_SUPPLICANT_STATE_COMPLETED:
544                 return FALSE;
545         }
546
547         return FALSE;
548 }
549
550 static connman_bool_t is_idle_wps(GSupplicantInterface *interface,
551                                                 struct wifi_data *wifi)
552 {
553         /* First, let's check if WPS processing did not went wrong */
554         if (g_supplicant_interface_get_wps_state(interface) ==
555                 G_SUPPLICANT_WPS_STATE_FAIL)
556                 return FALSE;
557
558         /* Unlike normal connection, being associated while processing wps
559          * actually means that we are idling. */
560         switch (wifi->state) {
561         case G_SUPPLICANT_STATE_UNKNOWN:
562         case G_SUPPLICANT_STATE_DISCONNECTED:
563         case G_SUPPLICANT_STATE_INACTIVE:
564         case G_SUPPLICANT_STATE_SCANNING:
565         case G_SUPPLICANT_STATE_ASSOCIATED:
566                 return TRUE;
567         case G_SUPPLICANT_STATE_AUTHENTICATING:
568         case G_SUPPLICANT_STATE_ASSOCIATING:
569         case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
570         case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
571         case G_SUPPLICANT_STATE_COMPLETED:
572                 return FALSE;
573         }
574
575         return FALSE;
576 }
577
578 static connman_bool_t handle_wps_completion(GSupplicantInterface *interface,
579                                         struct connman_network *network,
580                                         struct connman_device *device,
581                                         struct wifi_data *wifi)
582 {
583         connman_bool_t wps;
584
585         wps = connman_network_get_bool(network, "WiFi.UseWPS");
586         if (wps == TRUE) {
587                 const unsigned char *ssid, *wps_ssid;
588                 unsigned int ssid_len, wps_ssid_len;
589                 const char *wps_key;
590
591                 /* Checking if we got associated with requested
592                  * network */
593                 ssid = connman_network_get_blob(network, "WiFi.SSID",
594                                                 &ssid_len);
595
596                 wps_ssid = g_supplicant_interface_get_wps_ssid(
597                         interface, &wps_ssid_len);
598
599                 if (wps_ssid == NULL || wps_ssid_len != ssid_len ||
600                                 memcmp(ssid, wps_ssid, ssid_len) != 0) {
601                         connman_network_set_associating(network, FALSE);
602                         g_supplicant_interface_disconnect(wifi->interface,
603                                                 disconnect_callback, wifi);
604                         return FALSE;
605                 }
606
607                 wps_key = g_supplicant_interface_get_wps_key(interface);
608                 connman_network_set_string(network, "WiFi.Passphrase",
609                                         wps_key);
610
611                 connman_network_set_string(network, "WiFi.PinWPS", NULL);
612         }
613
614         return TRUE;
615 }
616
617 static void interface_state(GSupplicantInterface *interface)
618 {
619         struct connman_network *network;
620         struct connman_device *device;
621         struct wifi_data *wifi;
622         GSupplicantState state = g_supplicant_interface_get_state(interface);
623         unsigned char bssid[ETH_ALEN];
624         unsigned int bssid_len;
625         connman_bool_t wps;
626
627         wifi = g_supplicant_interface_get_data(interface);
628
629         DBG("wifi %p interface state %d", wifi, state);
630
631         if (wifi == NULL)
632                 return;
633
634         network = wifi->network;
635         device = wifi->device;
636
637         if (device == NULL || network == NULL)
638                 return;
639
640         switch (state) {
641         case G_SUPPLICANT_STATE_SCANNING:
642                 break;
643
644         case G_SUPPLICANT_STATE_AUTHENTICATING:
645         case G_SUPPLICANT_STATE_ASSOCIATING:
646                 connman_network_set_associating(network, TRUE);
647                 break;
648
649         case G_SUPPLICANT_STATE_COMPLETED:
650                 if (handle_wps_completion(interface, network, device, wifi) ==
651                                                                         FALSE)
652                         break;
653
654                 /* reset scan trigger and schedule background scan */
655                 connman_device_schedule_scan(device);
656
657                 if (get_bssid(device, bssid, &bssid_len) == 0)
658                         connman_network_set_address(network,
659                                                         bssid, bssid_len);
660                 connman_network_set_connected(network, TRUE);
661                 break;
662
663         case G_SUPPLICANT_STATE_DISCONNECTED:
664                 /*
665                  * If we're in one of the idle modes, we have
666                  * not started association yet and thus setting
667                  * those ones to FALSE could cancel an association
668                  * in progress.
669                  */
670                 wps = connman_network_get_bool(network, "WiFi.UseWPS");
671                 if (wps == TRUE)
672                         if (is_idle_wps(interface, wifi) == TRUE)
673                                 break;
674
675                 if (is_idle(wifi))
676                         break;
677                 connman_network_set_associating(network, FALSE);
678                 connman_network_set_connected(network, FALSE);
679                 break;
680
681         case G_SUPPLICANT_STATE_INACTIVE:
682                 connman_network_set_associating(network, FALSE);
683                 break;
684
685         case G_SUPPLICANT_STATE_UNKNOWN:
686         case G_SUPPLICANT_STATE_ASSOCIATED:
687         case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
688         case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
689                 break;
690         }
691
692         wifi->state = state;
693
694         DBG("DONE");
695 }
696
697 static void interface_removed(GSupplicantInterface *interface)
698 {
699         const char *ifname = g_supplicant_interface_get_ifname(interface);
700         struct wifi_data *wifi;
701
702         DBG("ifname %s", ifname);
703
704         wifi = g_supplicant_interface_get_data(interface);
705
706         if (wifi == NULL || wifi->device == NULL) {
707                 connman_error("Wrong wifi pointer");
708                 return;
709         }
710
711         connman_device_set_powered(wifi->device, FALSE);
712 }
713
714 static void scan_started(GSupplicantInterface *interface)
715 {
716         struct wifi_data *wifi;
717
718         DBG("");
719
720         wifi = g_supplicant_interface_get_data(interface);
721
722         if (wifi == NULL)
723                 return;
724 }
725
726 static void scan_finished(GSupplicantInterface *interface)
727 {
728         struct wifi_data *wifi;
729
730         DBG("");
731
732         wifi = g_supplicant_interface_get_data(interface);
733
734         if (wifi == NULL)
735                 return;
736 }
737
738 static unsigned char calculate_strength(GSupplicantNetwork *supplicant_network)
739 {
740         unsigned char strength;
741
742         strength = 120 + g_supplicant_network_get_signal(supplicant_network);
743         if (strength > 100)
744                 strength = 100;
745
746         return strength;
747 }
748
749 static void network_added(GSupplicantNetwork *supplicant_network)
750 {
751         struct connman_network *network;
752         GSupplicantInterface *interface;
753         struct wifi_data *wifi;
754         const char *name, *identifier, *mode, *security, *group;
755         const unsigned char *ssid;
756         unsigned int ssid_len;
757         connman_bool_t wps;
758
759         DBG("");
760
761         interface = g_supplicant_network_get_interface(supplicant_network);
762         wifi = g_supplicant_interface_get_data(interface);
763         name = g_supplicant_network_get_name(supplicant_network);
764         identifier = g_supplicant_network_get_identifier(supplicant_network);
765         mode = g_supplicant_network_get_mode(supplicant_network);
766         security = g_supplicant_network_get_security(supplicant_network);
767         group = g_supplicant_network_get_identifier(supplicant_network);
768         wps = g_supplicant_network_get_wps(supplicant_network);
769
770         if (wifi == NULL)
771                 return;
772
773         ssid = g_supplicant_network_get_ssid(supplicant_network, &ssid_len);
774
775         network = connman_device_get_network(wifi->device, identifier);
776
777         if (network == NULL) {
778                 network = connman_network_create(identifier,
779                                                 CONNMAN_NETWORK_TYPE_WIFI);
780                 if (network == NULL)
781                         return;
782
783                 connman_network_set_index(network, wifi->index);
784
785                 if (connman_device_add_network(wifi->device, network) < 0) {
786                         connman_network_unref(network);
787                         return;
788                 }
789         }
790
791         if (name != NULL && name[0] != '\0')
792                 connman_network_set_name(network, name);
793
794         connman_network_set_blob(network, "WiFi.SSID",
795                                                 ssid, ssid_len);
796         connman_network_set_string(network, "WiFi.Mode", mode);
797         connman_network_set_string(network, "WiFi.Security", security);
798         connman_network_set_strength(network,
799                                 calculate_strength(supplicant_network));
800         connman_network_set_bool(network, "WiFi.WPS", wps);
801
802         connman_network_set_available(network, TRUE);
803
804         if (ssid != NULL)
805                 connman_network_set_group(network, group);
806 }
807
808 static void network_removed(GSupplicantNetwork *network)
809 {
810         GSupplicantInterface *interface;
811         struct wifi_data *wifi;
812         const char *name, *identifier;
813
814         interface = g_supplicant_network_get_interface(network);
815         wifi = g_supplicant_interface_get_data(interface);
816         identifier = g_supplicant_network_get_identifier(network);
817         name = g_supplicant_network_get_name(network);
818
819         DBG("name %s", name);
820
821         if (wifi != NULL)
822                 connman_device_remove_network(wifi->device, identifier);
823 }
824
825 static void debug(const char *str)
826 {
827         if (getenv("CONNMAN_SUPPLICANT_DEBUG"))
828                 connman_debug("%s", str);
829 }
830
831 static const GSupplicantCallbacks callbacks = {
832         .system_ready           = system_ready,
833         .system_killed          = system_killed,
834         .interface_added        = interface_added,
835         .interface_state        = interface_state,
836         .interface_removed      = interface_removed,
837         .scan_started           = scan_started,
838         .scan_finished          = scan_finished,
839         .network_added          = network_added,
840         .network_removed        = network_removed,
841         .debug                  = debug,
842 };
843
844
845 static int tech_probe(struct connman_technology *technology)
846 {
847         wifi_technology = technology;
848
849         return 0;
850 }
851
852 static void tech_remove(struct connman_technology *technology)
853 {
854         wifi_technology = NULL;
855 }
856
857 static void regdom_callback(void *user_data)
858 {
859         char *alpha2 = user_data;
860
861         DBG("");
862
863         if (wifi_technology == NULL)
864                 return;
865
866         connman_technology_regdom_notify(wifi_technology, alpha2);
867 }
868
869 static int tech_set_regdom(struct connman_technology *technology, const char *alpha2)
870 {
871         return g_supplicant_set_country(alpha2, regdom_callback, alpha2);
872 }
873
874 static struct connman_technology_driver tech_driver = {
875         .name           = "wifi",
876         .type           = CONNMAN_SERVICE_TYPE_WIFI,
877         .probe          = tech_probe,
878         .remove         = tech_remove,
879         .set_regdom     = tech_set_regdom,
880 };
881
882 static int wifi_init(void)
883 {
884         int err;
885
886         err = connman_network_driver_register(&network_driver);
887         if (err < 0)
888                 return err;
889
890         err = g_supplicant_register(&callbacks);
891         if (err < 0) {
892                 connman_network_driver_unregister(&network_driver);
893                 return err;
894         }
895
896         err = connman_technology_driver_register(&tech_driver);
897         if (err < 0) {
898                 g_supplicant_unregister(&callbacks);
899                 connman_network_driver_unregister(&network_driver);
900                 return err;
901         }
902
903         return 0;
904 }
905
906 static void wifi_exit(void)
907 {
908         DBG();
909
910         connman_technology_driver_unregister(&tech_driver);
911
912         g_supplicant_unregister(&callbacks);
913
914         connman_network_driver_unregister(&network_driver);
915 }
916
917 CONNMAN_PLUGIN_DEFINE(wifi, "WiFi interface plugin", VERSION,
918                 CONNMAN_PLUGIN_PRIORITY_DEFAULT, wifi_init, wifi_exit)