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