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