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