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