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