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