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