wifi: Disable network in disconnected state
[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 <stdio.h>
30 #include <string.h>
31 #include <sys/ioctl.h>
32 #include <sys/socket.h>
33 #include <linux/if_arp.h>
34 #include <linux/wireless.h>
35 #include <net/ethernet.h>
36
37 #ifndef IFF_LOWER_UP
38 #define IFF_LOWER_UP    0x10000
39 #endif
40
41 #include <dbus/dbus.h>
42 #include <glib.h>
43
44 #define CONNMAN_API_SUBJECT_TO_CHANGE
45 #include <connman/plugin.h>
46 #include <connman/inet.h>
47 #include <connman/device.h>
48 #include <connman/rtnl.h>
49 #include <connman/technology.h>
50 #include <connman/log.h>
51 #include <connman/option.h>
52 #include <connman/storage.h>
53
54 #include <gsupplicant/gsupplicant.h>
55
56 #define CLEANUP_TIMEOUT   8     /* in seconds */
57 #define INACTIVE_TIMEOUT  12    /* in seconds */
58 #define MAXIMUM_RETRIES   4
59
60 struct connman_technology *wifi_technology = NULL;
61
62 struct wifi_data {
63         char *identifier;
64         struct connman_device *device;
65         struct connman_network *network;
66         struct connman_network *pending_network;
67         GSList *networks;
68         GSupplicantInterface *interface;
69         GSupplicantState state;
70         connman_bool_t connected;
71         connman_bool_t disconnecting;
72         connman_bool_t tethering;
73         connman_bool_t bridged;
74         const char *bridge;
75         int index;
76         unsigned flags;
77         unsigned int watch;
78         int retries;
79 };
80
81 static GList *iface_list = NULL;
82
83 static void handle_tethering(struct wifi_data *wifi)
84 {
85         if (wifi->tethering == FALSE)
86                 return;
87
88         if (wifi->bridge == NULL)
89                 return;
90
91         if (wifi->bridged == TRUE)
92                 return;
93
94         DBG("index %d bridge %s", wifi->index, wifi->bridge);
95
96         if (connman_inet_add_to_bridge(wifi->index, wifi->bridge) < 0)
97                 return;
98
99         wifi->bridged = TRUE;
100 }
101
102 static void wifi_newlink(unsigned flags, unsigned change, void *user_data)
103 {
104         struct connman_device *device = user_data;
105         struct wifi_data *wifi = connman_device_get_data(device);
106
107         DBG("index %d flags %d change %d", wifi->index, flags, change);
108
109         if (!change)
110                 return;
111
112         if ((wifi->flags & IFF_UP) != (flags & IFF_UP)) {
113                 if (flags & IFF_UP)
114                         DBG("interface up");
115                 else
116                         DBG("interface down");
117         }
118
119         if ((wifi->flags & IFF_LOWER_UP) != (flags & IFF_LOWER_UP)) {
120                 if (flags & IFF_LOWER_UP) {
121                         DBG("carrier on");
122
123                         handle_tethering(wifi);
124                 } else
125                         DBG("carrier off");
126         }
127
128         wifi->flags = flags;
129 }
130
131 static int wifi_probe(struct connman_device *device)
132 {
133         struct wifi_data *wifi;
134
135         DBG("device %p", device);
136
137         wifi = g_try_new0(struct wifi_data, 1);
138         if (wifi == NULL)
139                 return -ENOMEM;
140
141         wifi->connected = FALSE;
142         wifi->disconnecting = FALSE;
143         wifi->tethering = FALSE;
144         wifi->bridged = FALSE;
145         wifi->bridge = NULL;
146         wifi->state = G_SUPPLICANT_STATE_INACTIVE;
147
148         connman_device_set_data(device, wifi);
149         wifi->device = connman_device_ref(device);
150
151         wifi->index = connman_device_get_index(device);
152         wifi->flags = 0;
153
154         wifi->watch = connman_rtnl_add_newlink_watch(wifi->index,
155                                                         wifi_newlink, device);
156
157         iface_list = g_list_append(iface_list, wifi);
158
159         return 0;
160 }
161
162 static void remove_networks(struct connman_device *device,
163                                 struct wifi_data *wifi)
164 {
165         GSList *list;
166
167         for (list = wifi->networks; list != NULL; list = list->next) {
168                 struct connman_network *network = list->data;
169
170                 connman_device_remove_network(device, network);
171                 connman_network_unref(network);
172         }
173
174         g_slist_free(wifi->networks);
175         wifi->networks = NULL;
176 }
177
178 static void wifi_remove(struct connman_device *device)
179 {
180         struct wifi_data *wifi = connman_device_get_data(device);
181
182         DBG("device %p wifi %p", device, wifi);
183
184         if (wifi == NULL)
185                 return;
186
187         iface_list = g_list_remove(iface_list, wifi);
188
189         remove_networks(device, wifi);
190
191         connman_device_set_data(device, NULL);
192         connman_device_unref(wifi->device);
193         connman_rtnl_remove_watch(wifi->watch);
194
195         g_supplicant_interface_set_data(wifi->interface, NULL);
196
197         g_free(wifi->identifier);
198         g_free(wifi);
199 }
200
201 static void interface_create_callback(int result,
202                                         GSupplicantInterface *interface,
203                                                         void *user_data)
204 {
205         struct wifi_data *wifi = user_data;
206
207         DBG("result %d ifname %s, wifi %p", result,
208                                 g_supplicant_interface_get_ifname(interface),
209                                 wifi);
210
211         if (result < 0 || wifi == NULL)
212                 return;
213
214         wifi->interface = interface;
215         g_supplicant_interface_set_data(interface, wifi);
216
217         if (g_supplicant_interface_get_ready(interface) == FALSE)
218                 return;
219
220         DBG("interface is ready wifi %p tethering %d", wifi, wifi->tethering);
221
222         if (wifi->device == NULL) {
223                 connman_error("WiFi device not set");
224                 return;
225         }
226
227         connman_device_set_powered(wifi->device, TRUE);
228 }
229
230 static void interface_remove_callback(int result,
231                                         GSupplicantInterface *interface,
232                                                         void *user_data)
233 {
234         struct wifi_data *wifi;
235
236         wifi = g_supplicant_interface_get_data(interface);
237
238         DBG("result %d wifi %p", result, wifi);
239
240         if (result < 0 || wifi == NULL)
241                 return;
242
243         wifi->interface = NULL;
244 }
245
246
247 static int wifi_enable(struct connman_device *device)
248 {
249         struct wifi_data *wifi = connman_device_get_data(device);
250         const char *interface = connman_device_get_string(device, "Interface");
251         const char *driver = connman_option_get_string("wifi");
252         int ret;
253
254         DBG("device %p %p", device, wifi);
255
256         ret = g_supplicant_interface_create(interface, driver, NULL,
257                                                 interface_create_callback,
258                                                         wifi);
259         if (ret < 0)
260                 return ret;
261
262         return -EINPROGRESS;
263 }
264
265 static int wifi_disable(struct connman_device *device)
266 {
267         struct wifi_data *wifi = connman_device_get_data(device);
268         int ret;
269
270         DBG("device %p", device);
271
272         wifi->connected = FALSE;
273         wifi->disconnecting = FALSE;
274
275         if (wifi->pending_network != NULL)
276                 wifi->pending_network = NULL;
277
278         remove_networks(device, wifi);
279
280         ret = g_supplicant_interface_remove(wifi->interface,
281                                                 interface_remove_callback,
282                                                 NULL);
283         if (ret < 0)
284                 return ret;
285
286         return -EINPROGRESS;
287 }
288
289 static void scan_callback(int result, GSupplicantInterface *interface,
290                                                 void *user_data)
291 {
292         struct connman_device *device = user_data;
293
294         DBG("result %d", result);
295
296         if (result < 0)
297                 connman_device_reset_scanning(device);
298         else
299                 connman_device_set_scanning(device, FALSE);
300         connman_device_unref(device);
301 }
302
303 static int add_scan_param(gchar *hex_ssid, int freq,
304                         GSupplicantScanParams *scan_data,
305                         int driver_max_scan_ssids)
306 {
307         unsigned int i;
308
309         if (driver_max_scan_ssids > scan_data->num_ssids && hex_ssid != NULL) {
310                 gchar *ssid;
311                 unsigned int j = 0, hex;
312                 size_t hex_ssid_len = strlen(hex_ssid);
313
314                 ssid = g_try_malloc0(hex_ssid_len / 2);
315                 if (ssid == NULL)
316                         return -ENOMEM;
317
318                 for (i = 0; i < hex_ssid_len; i += 2) {
319                         sscanf(hex_ssid + i, "%02x", &hex);
320                         ssid[j++] = hex;
321                 }
322
323                 memcpy(scan_data->ssids[scan_data->num_ssids].ssid, ssid, j);
324                 scan_data->ssids[scan_data->num_ssids].ssid_len = j;
325                 scan_data->num_ssids++;
326
327                 g_free(ssid);
328         }
329
330         /* Don't add duplicate entries */
331         for (i = 0; i < G_SUPPLICANT_MAX_FAST_SCAN; i++) {
332                 if (scan_data->freqs[i] == 0) {
333                         scan_data->freqs[i] = freq;
334                         break;
335                 } else if (scan_data->freqs[i] == freq)
336                         break;
337         }
338
339         return 0;
340 }
341
342 struct last_connected {
343         GTimeVal modified;
344         gchar *ssid;
345         int freq;
346 };
347
348 static gint sort_entry(gconstpointer a, gconstpointer b, gpointer user_data)
349 {
350         GTimeVal *aval = (GTimeVal *)a;
351         GTimeVal *bval = (GTimeVal *)b;
352
353         /* Note that the sort order is descending */
354         if (aval->tv_sec < bval->tv_sec)
355                 return 1;
356
357         if (aval->tv_sec > bval->tv_sec)
358                 return -1;
359
360         return 0;
361 }
362
363 static void free_entry(gpointer data)
364 {
365         struct last_connected *entry = data;
366
367         g_free(entry->ssid);
368         g_free(entry);
369 }
370
371 static int get_latest_connections(int max_ssids,
372                                 GSupplicantScanParams *scan_data)
373 {
374         GSequenceIter *iter;
375         GSequence *latest_list;
376         struct last_connected *entry;
377         GKeyFile *keyfile;
378         GTimeVal modified;
379         gchar **services;
380         gchar *str;
381         char *ssid;
382         int i, freq;
383         int num_ssids = 0;
384
385         latest_list = g_sequence_new(free_entry);
386         if (latest_list == NULL)
387                 return -ENOMEM;
388
389         services = connman_storage_get_services();
390         for (i = 0; services && services[i]; i++) {
391                 if (strncmp(services[i], "wifi_", 5) != 0)
392                         continue;
393
394                 keyfile = connman_storage_load_service(services[i]);
395
396                 str = g_key_file_get_string(keyfile,
397                                         services[i], "Favorite", NULL);
398                 if (str == NULL || g_strcmp0(str, "true")) {
399                         if (str)
400                                 g_free(str);
401                         g_key_file_free(keyfile);
402                         continue;
403                 }
404                 g_free(str);
405
406                 str = g_key_file_get_string(keyfile,
407                                         services[i], "AutoConnect", NULL);
408                 if (str == NULL || g_strcmp0(str, "true")) {
409                         if (str)
410                                 g_free(str);
411                         g_key_file_free(keyfile);
412                         continue;
413                 }
414                 g_free(str);
415
416                 str = g_key_file_get_string(keyfile,
417                                         services[i], "Modified", NULL);
418                 if (str != NULL) {
419                         g_time_val_from_iso8601(str, &modified);
420                         g_free(str);
421                 }
422
423                 ssid = g_key_file_get_string(keyfile,
424                                         services[i], "SSID", NULL);
425
426                 freq = g_key_file_get_integer(keyfile, services[i],
427                                         "Frequency", NULL);
428                 if (freq) {
429                         entry = g_try_new(struct last_connected, 1);
430                         if (entry == NULL) {
431                                 g_sequence_free(latest_list);
432                                 g_key_file_free(keyfile);
433                                 g_free(ssid);
434                                 return -ENOMEM;
435                         }
436
437                         entry->ssid = ssid;
438                         entry->modified = modified;
439                         entry->freq = freq;
440
441                         g_sequence_insert_sorted(latest_list, entry,
442                                                 sort_entry, NULL);
443                         num_ssids++;
444                 } else
445                         g_free(ssid);
446
447                 g_key_file_free(keyfile);
448         }
449
450         g_strfreev(services);
451
452         num_ssids = num_ssids > G_SUPPLICANT_MAX_FAST_SCAN ?
453                 G_SUPPLICANT_MAX_FAST_SCAN : num_ssids;
454
455         iter = g_sequence_get_begin_iter(latest_list);
456
457         for (i = 0; i < num_ssids; i++) {
458                 entry = g_sequence_get(iter);
459
460                 DBG("ssid %s freq %d modified %lu", entry->ssid, entry->freq,
461                                                 entry->modified.tv_sec);
462
463                 add_scan_param(entry->ssid, entry->freq, scan_data, max_ssids);
464
465                 iter = g_sequence_iter_next(iter);
466         }
467
468         g_sequence_free(latest_list);
469         return num_ssids;
470 }
471
472 static int wifi_scan(struct connman_device *device)
473 {
474         struct wifi_data *wifi = connman_device_get_data(device);
475         int ret;
476
477         DBG("device %p %p", device, wifi->interface);
478
479         if (wifi->tethering == TRUE)
480                 return 0;
481
482         connman_device_ref(device);
483         ret = g_supplicant_interface_scan(wifi->interface, NULL,
484                                         scan_callback, device);
485         if (ret == 0)
486                 connman_device_set_scanning(device, TRUE);
487         else
488                 connman_device_unref(device);
489
490         return ret;
491 }
492
493 static int wifi_scan_fast(struct connman_device *device)
494 {
495         struct wifi_data *wifi = connman_device_get_data(device);
496         GSupplicantScanParams *scan_params = NULL;
497         int ret;
498         int driver_max_ssids = 0;
499
500         DBG("device %p %p", device, wifi->interface);
501
502         if (wifi->tethering == TRUE)
503                 return 0;
504
505         driver_max_ssids = g_supplicant_interface_get_max_scan_ssids(
506                                                         wifi->interface);
507         DBG("max ssids %d", driver_max_ssids);
508         if (driver_max_ssids == 0)
509                 return wifi_scan(device);
510
511         scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
512         if (scan_params == NULL)
513                 return -ENOMEM;
514
515         ret = get_latest_connections(driver_max_ssids, scan_params);
516         if (ret <= 0) {
517                 g_free(scan_params);
518                 return wifi_scan(device);
519         }
520
521         connman_device_ref(device);
522         ret = g_supplicant_interface_scan(wifi->interface, scan_params,
523                                                 scan_callback, device);
524         if (ret == 0)
525                 connman_device_set_scanning(device, TRUE);
526         else {
527                 g_free(scan_params);
528                 connman_device_unref(device);
529         }
530
531         return ret;
532 }
533
534 static struct connman_device_driver wifi_ng_driver = {
535         .name           = "wifi",
536         .type           = CONNMAN_DEVICE_TYPE_WIFI,
537         .priority       = CONNMAN_DEVICE_PRIORITY_LOW,
538         .probe          = wifi_probe,
539         .remove         = wifi_remove,
540         .enable         = wifi_enable,
541         .disable        = wifi_disable,
542         .scan           = wifi_scan,
543         .scan_fast      = wifi_scan_fast,
544 };
545
546 static void system_ready(void)
547 {
548         DBG("");
549
550         if (connman_device_driver_register(&wifi_ng_driver) < 0)
551                 connman_error("Failed to register WiFi driver");
552 }
553
554 static void system_killed(void)
555 {
556         DBG("");
557
558         connman_device_driver_unregister(&wifi_ng_driver);
559 }
560
561 static int network_probe(struct connman_network *network)
562 {
563         DBG("network %p", network);
564
565         return 0;
566 }
567
568 static void network_remove(struct connman_network *network)
569 {
570         struct connman_device *device = connman_network_get_device(network);
571         struct wifi_data *wifi;
572
573         DBG("network %p", network);
574
575         wifi = connman_device_get_data(device);
576         if (wifi == NULL)
577                 return;
578
579         if (wifi->network != network)
580                 return;
581
582         wifi->network = NULL;
583 }
584
585 static void connect_callback(int result, GSupplicantInterface *interface,
586                                                         void *user_data)
587 {
588         struct connman_network *network = user_data;
589
590         DBG("network %p result %d", network, result);
591
592         if (result == -ENOKEY) {
593                 connman_network_set_error(network,
594                                         CONNMAN_NETWORK_ERROR_INVALID_KEY);
595         } else if (result < 0) {
596                 connman_network_set_error(network,
597                                         CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
598         }
599 }
600
601 static GSupplicantSecurity network_security(const char *security)
602 {
603         if (g_str_equal(security, "none") == TRUE)
604                 return G_SUPPLICANT_SECURITY_NONE;
605         else if (g_str_equal(security, "wep") == TRUE)
606                 return G_SUPPLICANT_SECURITY_WEP;
607         else if (g_str_equal(security, "psk") == TRUE)
608                 return G_SUPPLICANT_SECURITY_PSK;
609         else if (g_str_equal(security, "wpa") == TRUE)
610                 return G_SUPPLICANT_SECURITY_PSK;
611         else if (g_str_equal(security, "rsn") == TRUE)
612                 return G_SUPPLICANT_SECURITY_PSK;
613         else if (g_str_equal(security, "ieee8021x") == TRUE)
614                 return G_SUPPLICANT_SECURITY_IEEE8021X;
615
616         return G_SUPPLICANT_SECURITY_UNKNOWN;
617 }
618
619 static void ssid_init(GSupplicantSSID *ssid, struct connman_network *network)
620 {
621         const char *security, *passphrase, *agent_passphrase;
622
623         memset(ssid, 0, sizeof(*ssid));
624         ssid->mode = G_SUPPLICANT_MODE_INFRA;
625         ssid->ssid = connman_network_get_blob(network, "WiFi.SSID",
626                                                 &ssid->ssid_len);
627         ssid->scan_ssid = 1;
628         security = connman_network_get_string(network, "WiFi.Security");
629         ssid->security = network_security(security);
630         passphrase = connman_network_get_string(network,
631                                                 "WiFi.Passphrase");
632         if (passphrase == NULL || strlen(passphrase) == 0) {
633
634                 /* Use agent provided passphrase as a fallback */
635                 agent_passphrase = connman_network_get_string(network,
636                                                 "WiFi.AgentPassphrase");
637
638                 if (agent_passphrase == NULL || strlen(agent_passphrase) == 0)
639                         ssid->passphrase = NULL;
640                 else
641                         ssid->passphrase = agent_passphrase;
642         } else
643                 ssid->passphrase = passphrase;
644
645         ssid->eap = connman_network_get_string(network, "WiFi.EAP");
646
647         /*
648          * If our private key password is unset,
649          * we use the supplied passphrase. That is needed
650          * for PEAP where 2 passphrases (identity and client
651          * cert may have to be provided.
652          */
653         if (connman_network_get_string(network,
654                                         "WiFi.PrivateKeyPassphrase") == NULL)
655                 connman_network_set_string(network,
656                                                 "WiFi.PrivateKeyPassphrase",
657                                                 ssid->passphrase);
658         /* We must have an identity for both PEAP and TLS */
659         ssid->identity = connman_network_get_string(network, "WiFi.Identity");
660
661         /* Use agent provided identity as a fallback */
662         if (ssid->identity == NULL || strlen(ssid->identity) == 0)
663                 ssid->identity = connman_network_get_string(network,
664                                                         "WiFi.AgentIdentity");
665
666         ssid->ca_cert_path = connman_network_get_string(network,
667                                                         "WiFi.CACertFile");
668         ssid->client_cert_path = connman_network_get_string(network,
669                                                         "WiFi.ClientCertFile");
670         ssid->private_key_path = connman_network_get_string(network,
671                                                         "WiFi.PrivateKeyFile");
672         ssid->private_key_passphrase = connman_network_get_string(network,
673                                                 "WiFi.PrivateKeyPassphrase");
674         ssid->phase2_auth = connman_network_get_string(network, "WiFi.Phase2");
675
676         ssid->use_wps = connman_network_get_bool(network, "WiFi.UseWPS");
677         ssid->pin_wps = connman_network_get_string(network, "WiFi.PinWPS");
678
679 }
680
681 static int network_connect(struct connman_network *network)
682 {
683         struct connman_device *device = connman_network_get_device(network);
684         struct wifi_data *wifi;
685         GSupplicantInterface *interface;
686         GSupplicantSSID *ssid;
687
688         DBG("network %p", network);
689
690         if (device == NULL)
691                 return -ENODEV;
692
693         wifi = connman_device_get_data(device);
694         if (wifi == NULL)
695                 return -ENODEV;
696
697         ssid = g_try_malloc0(sizeof(GSupplicantSSID));
698         if (ssid == NULL)
699                 return -ENOMEM;
700
701         interface = wifi->interface;
702
703         ssid_init(ssid, network);
704
705         if (wifi->disconnecting == TRUE)
706                 wifi->pending_network = network;
707         else {
708                 wifi->network = network;
709                 wifi->retries = 0;
710
711                 return g_supplicant_interface_connect(interface, ssid,
712                                                 connect_callback, network);
713         }
714
715         return -EINPROGRESS;
716 }
717
718 static void disconnect_callback(int result, GSupplicantInterface *interface,
719                                                                 void *user_data)
720 {
721         struct wifi_data *wifi = user_data;
722
723         if (wifi->network != NULL) {
724                 /*
725                  * if result < 0 supplican return an error because
726                  * the network is not current.
727                  * we wont receive G_SUPPLICANT_STATE_DISCONNECTED since it
728                  * failed, call connman_network_set_connected to report
729                  * disconnect is completed.
730                  */
731                 if (result < 0)
732                         connman_network_set_connected(wifi->network, FALSE);
733         }
734
735         wifi->network = NULL;
736
737         wifi->disconnecting = FALSE;
738
739         if (wifi->pending_network != NULL) {
740                 network_connect(wifi->pending_network);
741                 wifi->pending_network = NULL;
742         }
743
744 }
745
746 static int network_disconnect(struct connman_network *network)
747 {
748         struct connman_device *device = connman_network_get_device(network);
749         struct wifi_data *wifi;
750         int err;
751
752         DBG("network %p", network);
753
754         wifi = connman_device_get_data(device);
755         if (wifi == NULL || wifi->interface == NULL)
756                 return -ENODEV;
757
758         connman_network_set_associating(network, FALSE);
759
760         if (wifi->disconnecting == TRUE)
761                 return -EALREADY;
762
763         wifi->disconnecting = TRUE;
764
765         err = g_supplicant_interface_disconnect(wifi->interface,
766                                                 disconnect_callback, wifi);
767         if (err < 0)
768                 wifi->disconnecting = FALSE;
769
770         return err;
771 }
772
773 static struct connman_network_driver network_driver = {
774         .name           = "wifi",
775         .type           = CONNMAN_NETWORK_TYPE_WIFI,
776         .priority       = CONNMAN_NETWORK_PRIORITY_LOW,
777         .probe          = network_probe,
778         .remove         = network_remove,
779         .connect        = network_connect,
780         .disconnect     = network_disconnect,
781 };
782
783 static void interface_added(GSupplicantInterface *interface)
784 {
785         const char *ifname = g_supplicant_interface_get_ifname(interface);
786         const char *driver = g_supplicant_interface_get_driver(interface);
787         struct wifi_data *wifi;
788
789         wifi = g_supplicant_interface_get_data(interface);
790
791         /*
792          * We can get here with a NULL wifi pointer when
793          * the interface added signal is sent before the
794          * interface creation callback is called.
795          */
796         if (wifi == NULL)
797                 return;
798
799         DBG("ifname %s driver %s wifi %p tethering %d",
800                         ifname, driver, wifi, wifi->tethering);
801
802         if (wifi->device == NULL) {
803                 connman_error("WiFi device not set");
804                 return;
805         }
806
807         connman_device_set_powered(wifi->device, TRUE);
808
809         if (wifi->tethering == TRUE)
810                 return;
811 }
812
813 static connman_bool_t is_idle(struct wifi_data *wifi)
814 {
815         DBG("state %d", wifi->state);
816
817         switch (wifi->state) {
818         case G_SUPPLICANT_STATE_UNKNOWN:
819         case G_SUPPLICANT_STATE_DISCONNECTED:
820         case G_SUPPLICANT_STATE_INACTIVE:
821         case G_SUPPLICANT_STATE_SCANNING:
822                 return TRUE;
823
824         case G_SUPPLICANT_STATE_AUTHENTICATING:
825         case G_SUPPLICANT_STATE_ASSOCIATING:
826         case G_SUPPLICANT_STATE_ASSOCIATED:
827         case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
828         case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
829         case G_SUPPLICANT_STATE_COMPLETED:
830                 return FALSE;
831         }
832
833         return FALSE;
834 }
835
836 static connman_bool_t is_idle_wps(GSupplicantInterface *interface,
837                                                 struct wifi_data *wifi)
838 {
839         /* First, let's check if WPS processing did not went wrong */
840         if (g_supplicant_interface_get_wps_state(interface) ==
841                 G_SUPPLICANT_WPS_STATE_FAIL)
842                 return FALSE;
843
844         /* Unlike normal connection, being associated while processing wps
845          * actually means that we are idling. */
846         switch (wifi->state) {
847         case G_SUPPLICANT_STATE_UNKNOWN:
848         case G_SUPPLICANT_STATE_DISCONNECTED:
849         case G_SUPPLICANT_STATE_INACTIVE:
850         case G_SUPPLICANT_STATE_SCANNING:
851         case G_SUPPLICANT_STATE_ASSOCIATED:
852                 return TRUE;
853         case G_SUPPLICANT_STATE_AUTHENTICATING:
854         case G_SUPPLICANT_STATE_ASSOCIATING:
855         case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
856         case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
857         case G_SUPPLICANT_STATE_COMPLETED:
858                 return FALSE;
859         }
860
861         return FALSE;
862 }
863
864 static connman_bool_t handle_wps_completion(GSupplicantInterface *interface,
865                                         struct connman_network *network,
866                                         struct connman_device *device,
867                                         struct wifi_data *wifi)
868 {
869         connman_bool_t wps;
870
871         wps = connman_network_get_bool(network, "WiFi.UseWPS");
872         if (wps == TRUE) {
873                 const unsigned char *ssid, *wps_ssid;
874                 unsigned int ssid_len, wps_ssid_len;
875                 const char *wps_key;
876
877                 /* Checking if we got associated with requested
878                  * network */
879                 ssid = connman_network_get_blob(network, "WiFi.SSID",
880                                                 &ssid_len);
881
882                 wps_ssid = g_supplicant_interface_get_wps_ssid(
883                         interface, &wps_ssid_len);
884
885                 if (wps_ssid == NULL || wps_ssid_len != ssid_len ||
886                                 memcmp(ssid, wps_ssid, ssid_len) != 0) {
887                         connman_network_set_associating(network, FALSE);
888                         g_supplicant_interface_disconnect(wifi->interface,
889                                                 disconnect_callback, wifi);
890                         return FALSE;
891                 }
892
893                 wps_key = g_supplicant_interface_get_wps_key(interface);
894                 connman_network_set_string(network, "WiFi.Passphrase",
895                                         wps_key);
896
897                 connman_network_set_string(network, "WiFi.PinWPS", NULL);
898         }
899
900         return TRUE;
901 }
902
903 static connman_bool_t handle_4way_handshake_failure(GSupplicantInterface *interface,
904                                         struct connman_network *network,
905                                         struct wifi_data *wifi)
906 {
907         if (wifi->state != G_SUPPLICANT_STATE_4WAY_HANDSHAKE)
908                 return FALSE;
909
910         wifi->retries++;
911
912         if (wifi->retries < MAXIMUM_RETRIES)
913                 return TRUE;
914
915         connman_network_set_error(network, CONNMAN_NETWORK_ERROR_INVALID_KEY);
916
917         return FALSE;
918 }
919
920 static void interface_state(GSupplicantInterface *interface)
921 {
922         struct connman_network *network;
923         struct connman_device *device;
924         struct wifi_data *wifi;
925         GSupplicantState state = g_supplicant_interface_get_state(interface);
926         connman_bool_t wps;
927
928         wifi = g_supplicant_interface_get_data(interface);
929
930         DBG("wifi %p interface state %d", wifi, state);
931
932         if (wifi == NULL)
933                 return;
934
935         network = wifi->network;
936         device = wifi->device;
937
938         if (device == NULL || network == NULL)
939                 return;
940
941         switch (state) {
942         case G_SUPPLICANT_STATE_SCANNING:
943                 break;
944
945         case G_SUPPLICANT_STATE_AUTHENTICATING:
946         case G_SUPPLICANT_STATE_ASSOCIATING:
947                 connman_network_set_associating(network, TRUE);
948                 break;
949
950         case G_SUPPLICANT_STATE_COMPLETED:
951                 if (handle_wps_completion(interface, network, device, wifi) ==
952                                                                         FALSE)
953                         break;
954
955                 /* reset scan trigger and schedule background scan */
956                 connman_device_schedule_scan(device);
957
958                 connman_network_set_connected(network, TRUE);
959                 break;
960
961         case G_SUPPLICANT_STATE_DISCONNECTED:
962                 /*
963                  * If we're in one of the idle modes, we have
964                  * not started association yet and thus setting
965                  * those ones to FALSE could cancel an association
966                  * in progress.
967                  */
968                 wps = connman_network_get_bool(network, "WiFi.UseWPS");
969                 if (wps == TRUE)
970                         if (is_idle_wps(interface, wifi) == TRUE)
971                                 break;
972
973                 if (is_idle(wifi))
974                         break;
975
976                 /* If previous state was 4way-handshake, then
977                  * it's either: psk was incorrect and thus we retry
978                  * or if we reach the maximum retries we declare the
979                  * psk as wrong */
980                 if (handle_4way_handshake_failure(interface,
981                                                 network, wifi) == TRUE)
982                         break;
983
984                 /* We disable the selected network, if not then
985                  * wpa_supplicant will loop retrying */
986                 if (g_supplicant_interface_enable_selected_network(interface,
987                                                 FALSE) != 0)
988                         DBG("Could not disables selected network");
989
990                 connman_network_set_associating(network, FALSE);
991                 connman_network_set_connected(network, FALSE);
992                 break;
993
994         case G_SUPPLICANT_STATE_INACTIVE:
995                 connman_network_set_associating(network, FALSE);
996                 break;
997
998         case G_SUPPLICANT_STATE_UNKNOWN:
999         case G_SUPPLICANT_STATE_ASSOCIATED:
1000         case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
1001         case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
1002                 break;
1003         }
1004
1005         wifi->state = state;
1006
1007         DBG("DONE");
1008 }
1009
1010 static void interface_removed(GSupplicantInterface *interface)
1011 {
1012         const char *ifname = g_supplicant_interface_get_ifname(interface);
1013         struct wifi_data *wifi;
1014
1015         DBG("ifname %s", ifname);
1016
1017         wifi = g_supplicant_interface_get_data(interface);
1018
1019         if (wifi != NULL && wifi->tethering == TRUE)
1020                 return;
1021
1022         if (wifi == NULL || wifi->device == NULL) {
1023                 connman_error("Wrong wifi pointer");
1024                 return;
1025         }
1026
1027         connman_device_set_powered(wifi->device, FALSE);
1028 }
1029
1030 static void scan_started(GSupplicantInterface *interface)
1031 {
1032         DBG("");
1033 }
1034
1035 static void scan_finished(GSupplicantInterface *interface)
1036 {
1037         DBG("");
1038 }
1039
1040 static unsigned char calculate_strength(GSupplicantNetwork *supplicant_network)
1041 {
1042         unsigned char strength;
1043
1044         strength = 120 + g_supplicant_network_get_signal(supplicant_network);
1045         if (strength > 100)
1046                 strength = 100;
1047
1048         return strength;
1049 }
1050
1051 static void network_added(GSupplicantNetwork *supplicant_network)
1052 {
1053         struct connman_network *network;
1054         GSupplicantInterface *interface;
1055         struct wifi_data *wifi;
1056         const char *name, *identifier, *security, *group;
1057         const unsigned char *ssid;
1058         unsigned int ssid_len;
1059         connman_bool_t wps;
1060
1061         DBG("");
1062
1063         interface = g_supplicant_network_get_interface(supplicant_network);
1064         wifi = g_supplicant_interface_get_data(interface);
1065         name = g_supplicant_network_get_name(supplicant_network);
1066         identifier = g_supplicant_network_get_identifier(supplicant_network);
1067         security = g_supplicant_network_get_security(supplicant_network);
1068         group = g_supplicant_network_get_identifier(supplicant_network);
1069         wps = g_supplicant_network_get_wps(supplicant_network);
1070
1071         if (wifi == NULL)
1072                 return;
1073
1074         ssid = g_supplicant_network_get_ssid(supplicant_network, &ssid_len);
1075
1076         network = connman_device_get_network(wifi->device, identifier);
1077
1078         if (network == NULL) {
1079                 network = connman_network_create(identifier,
1080                                                 CONNMAN_NETWORK_TYPE_WIFI);
1081                 if (network == NULL)
1082                         return;
1083
1084                 connman_network_set_index(network, wifi->index);
1085
1086                 if (connman_device_add_network(wifi->device, network) < 0) {
1087                         connman_network_unref(network);
1088                         return;
1089                 }
1090
1091                 wifi->networks = g_slist_append(wifi->networks, network);
1092         }
1093
1094         if (name != NULL && name[0] != '\0')
1095                 connman_network_set_name(network, name);
1096
1097         connman_network_set_blob(network, "WiFi.SSID",
1098                                                 ssid, ssid_len);
1099         connman_network_set_string(network, "WiFi.Security", security);
1100         connman_network_set_strength(network,
1101                                 calculate_strength(supplicant_network));
1102         connman_network_set_bool(network, "WiFi.WPS", wps);
1103
1104         connman_network_set_frequency(network,
1105                         g_supplicant_network_get_frequency(supplicant_network));
1106
1107         connman_network_set_available(network, TRUE);
1108
1109         if (ssid != NULL)
1110                 connman_network_set_group(network, group);
1111 }
1112
1113 static void network_removed(GSupplicantNetwork *network)
1114 {
1115         GSupplicantInterface *interface;
1116         struct wifi_data *wifi;
1117         const char *name, *identifier;
1118         struct connman_network *connman_network;
1119
1120         interface = g_supplicant_network_get_interface(network);
1121         wifi = g_supplicant_interface_get_data(interface);
1122         identifier = g_supplicant_network_get_identifier(network);
1123         name = g_supplicant_network_get_name(network);
1124
1125         DBG("name %s", name);
1126
1127         if (wifi == NULL)
1128                 return;
1129
1130         connman_network = connman_device_get_network(wifi->device, identifier);
1131         if (connman_network == NULL)
1132                 return;
1133
1134         wifi->networks = g_slist_remove(wifi->networks, connman_network);
1135
1136         connman_device_remove_network(wifi->device, connman_network);
1137         connman_network_unref(connman_network);
1138 }
1139
1140 static void network_changed(GSupplicantNetwork *network, const char *property)
1141 {
1142         GSupplicantInterface *interface;
1143         struct wifi_data *wifi;
1144         const char *name, *identifier;
1145         struct connman_network *connman_network;
1146
1147         interface = g_supplicant_network_get_interface(network);
1148         wifi = g_supplicant_interface_get_data(interface);
1149         identifier = g_supplicant_network_get_identifier(network);
1150         name = g_supplicant_network_get_name(network);
1151
1152         DBG("name %s", name);
1153
1154         if (wifi == NULL)
1155                 return;
1156
1157         connman_network = connman_device_get_network(wifi->device, identifier);
1158         if (connman_network == NULL)
1159                 return;
1160
1161         if (g_str_equal(property, "Signal") == TRUE) {
1162                connman_network_set_strength(connman_network,
1163                                         calculate_strength(network));
1164                connman_network_update(connman_network);
1165         }
1166 }
1167
1168 static void debug(const char *str)
1169 {
1170         if (getenv("CONNMAN_SUPPLICANT_DEBUG"))
1171                 connman_debug("%s", str);
1172 }
1173
1174 static const GSupplicantCallbacks callbacks = {
1175         .system_ready           = system_ready,
1176         .system_killed          = system_killed,
1177         .interface_added        = interface_added,
1178         .interface_state        = interface_state,
1179         .interface_removed      = interface_removed,
1180         .scan_started           = scan_started,
1181         .scan_finished          = scan_finished,
1182         .network_added          = network_added,
1183         .network_removed        = network_removed,
1184         .network_changed        = network_changed,
1185         .debug                  = debug,
1186 };
1187
1188
1189 static int tech_probe(struct connman_technology *technology)
1190 {
1191         wifi_technology = technology;
1192
1193         return 0;
1194 }
1195
1196 static void tech_remove(struct connman_technology *technology)
1197 {
1198         wifi_technology = NULL;
1199 }
1200
1201 struct wifi_tethering_info {
1202         struct wifi_data *wifi;
1203         struct connman_technology *technology;
1204         char *ifname;
1205         GSupplicantSSID *ssid;
1206 };
1207
1208 static GSupplicantSSID *ssid_ap_init(const char *ssid, const char *passphrase)
1209 {
1210         GSupplicantSSID *ap;
1211
1212         ap = g_try_malloc0(sizeof(GSupplicantSSID));
1213         if (ap == NULL)
1214                 return NULL;
1215
1216         ap->mode = G_SUPPLICANT_MODE_MASTER;
1217         ap->ssid = ssid;
1218         ap->ssid_len = strlen(ssid);
1219         ap->scan_ssid = 0;
1220         ap->freq = 2412;
1221
1222         if (passphrase == NULL || strlen(passphrase) == 0) {
1223                 ap->security = G_SUPPLICANT_SECURITY_NONE;
1224                 ap->passphrase = NULL;
1225         } else {
1226                ap->security = G_SUPPLICANT_SECURITY_PSK;
1227                ap->protocol = G_SUPPLICANT_PROTO_RSN;
1228                ap->pairwise_cipher = G_SUPPLICANT_PAIRWISE_CCMP;
1229                ap->group_cipher = G_SUPPLICANT_GROUP_CCMP;
1230                ap->passphrase = passphrase;
1231         }
1232
1233         return ap;
1234 }
1235
1236 static void ap_start_callback(int result, GSupplicantInterface *interface,
1237                                                         void *user_data)
1238 {
1239         struct wifi_tethering_info *info = user_data;
1240
1241         DBG("result %d index %d bridge %s",
1242                 result, info->wifi->index, info->wifi->bridge);
1243
1244         if (result < 0) {
1245                 connman_inet_remove_from_bridge(info->wifi->index,
1246                                                         info->wifi->bridge);
1247                 connman_technology_tethering_notify(info->technology, FALSE);
1248         }
1249
1250         g_free(info->ifname);
1251         g_free(info);
1252 }
1253
1254 static void ap_create_callback(int result,
1255                                 GSupplicantInterface *interface,
1256                                         void *user_data)
1257 {
1258         struct wifi_tethering_info *info = user_data;
1259
1260         DBG("result %d ifname %s", result,
1261                                 g_supplicant_interface_get_ifname(interface));
1262
1263         if (result < 0) {
1264                 connman_inet_remove_from_bridge(info->wifi->index,
1265                                                         info->wifi->bridge);
1266                 connman_technology_tethering_notify(info->technology, FALSE);
1267
1268                 g_free(info->ifname);
1269                 g_free(info);
1270                 return;
1271         }
1272
1273         info->wifi->interface = interface;
1274         g_supplicant_interface_set_data(interface, info->wifi);
1275
1276         if (g_supplicant_interface_set_apscan(interface, 2) < 0)
1277                 connman_error("Failed to set interface ap_scan property");
1278
1279         g_supplicant_interface_connect(interface, info->ssid,
1280                                                 ap_start_callback, info);
1281 }
1282
1283 static void sta_remove_callback(int result,
1284                                 GSupplicantInterface *interface,
1285                                         void *user_data)
1286 {
1287         struct wifi_tethering_info *info = user_data;
1288         const char *driver = connman_option_get_string("wifi");
1289
1290         DBG("ifname %s result %d ", info->ifname, result);
1291
1292         if (result < 0) {
1293                 info->wifi->tethering = TRUE;
1294
1295                 g_free(info->ifname);
1296                 g_free(info);
1297                 return;
1298         }
1299
1300         info->wifi->interface = NULL;
1301
1302         connman_technology_tethering_notify(info->technology, TRUE);
1303
1304         g_supplicant_interface_create(info->ifname, driver, info->wifi->bridge,
1305                                                 ap_create_callback,
1306                                                         info);
1307 }
1308
1309 static int tech_set_tethering(struct connman_technology *technology,
1310                                 const char *identifier, const char *passphrase,
1311                                 const char *bridge, connman_bool_t enabled)
1312 {
1313         GList *list;
1314         GSupplicantInterface *interface;
1315         struct wifi_data *wifi;
1316         struct wifi_tethering_info *info;
1317         const char *ifname;
1318         unsigned int mode;
1319         int err;
1320
1321         DBG("");
1322
1323         if (enabled == FALSE) {
1324                 for (list = iface_list; list; list = list->next) {
1325                         wifi = list->data;
1326
1327                         if (wifi->tethering == TRUE) {
1328                                 wifi->tethering = FALSE;
1329
1330                                 connman_inet_remove_from_bridge(wifi->index,
1331                                                                         bridge);
1332                                 wifi->bridged = FALSE;
1333                         }
1334                 }
1335
1336                 connman_technology_tethering_notify(technology, FALSE);
1337
1338                 return 0;
1339         }
1340
1341         for (list = iface_list; list; list = list->next) {
1342                 wifi = list->data;
1343
1344                 interface = wifi->interface;
1345
1346                 if (interface == NULL)
1347                         continue;
1348
1349                 ifname = g_supplicant_interface_get_ifname(wifi->interface);
1350
1351                 mode = g_supplicant_interface_get_mode(interface);
1352                 if ((mode & G_SUPPLICANT_CAPABILITY_MODE_AP) == 0) {
1353                         DBG("%s does not support AP mode", ifname);
1354                         continue;
1355                 }
1356
1357                 info = g_try_malloc0(sizeof(struct wifi_tethering_info));
1358                 if (info == NULL)
1359                         return -ENOMEM;
1360
1361                 info->wifi = wifi;
1362                 info->technology = technology;
1363                 info->wifi->bridge = bridge;
1364                 info->ssid = ssid_ap_init(identifier, passphrase);
1365                 if (info->ssid == NULL) {
1366                         g_free(info);
1367                         continue;
1368                 }
1369                 info->ifname = g_strdup(ifname);
1370                 if (info->ifname == NULL) {
1371                         g_free(info);
1372                         continue;
1373                 }
1374
1375                 info->wifi->tethering = TRUE;
1376
1377                 err = g_supplicant_interface_remove(interface,
1378                                                 sta_remove_callback,
1379                                                         info);
1380                 if (err == 0)
1381                         return err;
1382         }
1383
1384         return -EOPNOTSUPP;
1385 }
1386
1387 static void regdom_callback(void *user_data)
1388 {
1389         char *alpha2 = user_data;
1390
1391         DBG("");
1392
1393         if (wifi_technology == NULL)
1394                 return;
1395
1396         connman_technology_regdom_notify(wifi_technology, alpha2);
1397 }
1398
1399 static int tech_set_regdom(struct connman_technology *technology, const char *alpha2)
1400 {
1401         return g_supplicant_set_country(alpha2, regdom_callback, alpha2);
1402 }
1403
1404 static struct connman_technology_driver tech_driver = {
1405         .name           = "wifi",
1406         .type           = CONNMAN_SERVICE_TYPE_WIFI,
1407         .probe          = tech_probe,
1408         .remove         = tech_remove,
1409         .set_tethering  = tech_set_tethering,
1410         .set_regdom     = tech_set_regdom,
1411 };
1412
1413 static int wifi_init(void)
1414 {
1415         int err;
1416
1417         err = connman_network_driver_register(&network_driver);
1418         if (err < 0)
1419                 return err;
1420
1421         err = g_supplicant_register(&callbacks);
1422         if (err < 0) {
1423                 connman_network_driver_unregister(&network_driver);
1424                 return err;
1425         }
1426
1427         err = connman_technology_driver_register(&tech_driver);
1428         if (err < 0) {
1429                 g_supplicant_unregister(&callbacks);
1430                 connman_network_driver_unregister(&network_driver);
1431                 return err;
1432         }
1433
1434         return 0;
1435 }
1436
1437 static void wifi_exit(void)
1438 {
1439         DBG();
1440
1441         connman_technology_driver_unregister(&tech_driver);
1442
1443         g_supplicant_unregister(&callbacks);
1444
1445         connman_network_driver_unregister(&network_driver);
1446 }
1447
1448 CONNMAN_PLUGIN_DEFINE(wifi, "WiFi interface plugin", VERSION,
1449                 CONNMAN_PLUGIN_PRIORITY_DEFAULT, wifi_init, wifi_exit)