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