wifi: Add capability to set regulatory domain through device's interface
[framework/connectivity/connman.git] / plugins / wifi.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2012  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 #include <include/setting.h>
54
55 #include <gsupplicant/gsupplicant.h>
56
57 #define CLEANUP_TIMEOUT   8     /* in seconds */
58 #define INACTIVE_TIMEOUT  12    /* in seconds */
59 #define MAXIMUM_RETRIES   4
60
61 #define BGSCAN_DEFAULT "simple:30:-45:300"
62 #define AUTOSCAN_DEFAULT "exponential:3:300"
63
64 static struct connman_technology *wifi_technology = NULL;
65
66 struct hidden_params {
67         char ssid[32];
68         unsigned int ssid_len;
69         char *identity;
70         char *passphrase;
71         gpointer user_data;
72 };
73
74 /**
75  * Used for autoscan "emulation".
76  * Should be removed when wpa_s autoscan support will be by default.
77  */
78 struct autoscan_params {
79         int base;
80         int limit;
81         int interval;
82         unsigned int timeout;
83 };
84
85 struct wifi_data {
86         char *identifier;
87         struct connman_device *device;
88         struct connman_network *network;
89         struct connman_network *pending_network;
90         GSList *networks;
91         GSupplicantInterface *interface;
92         GSupplicantState state;
93         connman_bool_t connected;
94         connman_bool_t disconnecting;
95         connman_bool_t tethering;
96         connman_bool_t bridged;
97         const char *bridge;
98         int index;
99         unsigned flags;
100         unsigned int watch;
101         int retries;
102         struct hidden_params *hidden;
103         /**
104          * autoscan "emulation".
105          */
106         struct autoscan_params *autoscan;
107 };
108
109 static GList *iface_list = NULL;
110
111 static void start_autoscan(struct connman_device *device);
112
113 static void handle_tethering(struct wifi_data *wifi)
114 {
115         if (wifi->tethering == FALSE)
116                 return;
117
118         if (wifi->bridge == NULL)
119                 return;
120
121         if (wifi->bridged == TRUE)
122                 return;
123
124         DBG("index %d bridge %s", wifi->index, wifi->bridge);
125
126         if (connman_inet_add_to_bridge(wifi->index, wifi->bridge) < 0)
127                 return;
128
129         wifi->bridged = TRUE;
130 }
131
132 static void wifi_newlink(unsigned flags, unsigned change, void *user_data)
133 {
134         struct connman_device *device = user_data;
135         struct wifi_data *wifi = connman_device_get_data(device);
136
137         if (wifi == NULL)
138                 return;
139
140         DBG("index %d flags %d change %d", wifi->index, flags, change);
141
142         if (!change)
143                 return;
144
145         if ((wifi->flags & IFF_UP) != (flags & IFF_UP)) {
146                 if (flags & IFF_UP)
147                         DBG("interface up");
148                 else
149                         DBG("interface down");
150         }
151
152         if ((wifi->flags & IFF_LOWER_UP) != (flags & IFF_LOWER_UP)) {
153                 if (flags & IFF_LOWER_UP) {
154                         DBG("carrier on");
155
156                         handle_tethering(wifi);
157                 } else
158                         DBG("carrier off");
159         }
160
161         wifi->flags = flags;
162 }
163
164 static int wifi_probe(struct connman_device *device)
165 {
166         struct wifi_data *wifi;
167
168         DBG("device %p", device);
169
170         wifi = g_try_new0(struct wifi_data, 1);
171         if (wifi == NULL)
172                 return -ENOMEM;
173
174         wifi->connected = FALSE;
175         wifi->disconnecting = FALSE;
176         wifi->tethering = FALSE;
177         wifi->bridged = FALSE;
178         wifi->bridge = NULL;
179         wifi->state = G_SUPPLICANT_STATE_INACTIVE;
180
181         connman_device_set_data(device, wifi);
182         wifi->device = connman_device_ref(device);
183
184         wifi->index = connman_device_get_index(device);
185         wifi->flags = 0;
186
187         wifi->watch = connman_rtnl_add_newlink_watch(wifi->index,
188                                                         wifi_newlink, device);
189
190         iface_list = g_list_append(iface_list, wifi);
191
192         return 0;
193 }
194
195 static void remove_networks(struct connman_device *device,
196                                 struct wifi_data *wifi)
197 {
198         GSList *list;
199
200         for (list = wifi->networks; list != NULL; list = list->next) {
201                 struct connman_network *network = list->data;
202
203                 connman_device_remove_network(device, network);
204                 connman_network_unref(network);
205         }
206
207         g_slist_free(wifi->networks);
208         wifi->networks = NULL;
209 }
210
211 static void reset_autoscan(struct connman_device *device)
212 {
213         struct wifi_data *wifi = connman_device_get_data(device);
214         struct autoscan_params *autoscan;
215
216         DBG("");
217
218         if (wifi == NULL || wifi->autoscan == NULL)
219                 return;
220
221         autoscan = wifi->autoscan;
222
223         if (autoscan->timeout == 0 && autoscan->interval == 0)
224                 return;
225
226         g_source_remove(autoscan->timeout);
227
228         autoscan->timeout = 0;
229         autoscan->interval = 0;
230
231         connman_device_unref(device);
232 }
233
234 static void stop_autoscan(struct connman_device *device)
235 {
236         reset_autoscan(device);
237
238         connman_device_set_scanning(device, FALSE);
239 }
240
241 static void wifi_remove(struct connman_device *device)
242 {
243         struct wifi_data *wifi = connman_device_get_data(device);
244
245         DBG("device %p wifi %p", device, wifi);
246
247         if (wifi == NULL)
248                 return;
249
250         iface_list = g_list_remove(iface_list, wifi);
251
252         remove_networks(device, wifi);
253
254         connman_device_set_powered(device, FALSE);
255         connman_device_set_data(device, NULL);
256         connman_device_unref(wifi->device);
257         connman_rtnl_remove_watch(wifi->watch);
258
259         g_supplicant_interface_set_data(wifi->interface, NULL);
260
261         g_free(wifi->autoscan);
262         g_free(wifi->identifier);
263         g_free(wifi);
264 }
265
266 static int add_scan_param(gchar *hex_ssid, int freq,
267                         GSupplicantScanParams *scan_data,
268                         int driver_max_scan_ssids)
269 {
270         unsigned int i;
271         struct scan_ssid *scan_ssid;
272
273         if (driver_max_scan_ssids > scan_data->num_ssids && hex_ssid != NULL) {
274                 gchar *ssid;
275                 unsigned int j = 0, hex;
276                 size_t hex_ssid_len = strlen(hex_ssid);
277
278                 ssid = g_try_malloc0(hex_ssid_len / 2);
279                 if (ssid == NULL)
280                         return -ENOMEM;
281
282                 for (i = 0; i < hex_ssid_len; i += 2) {
283                         sscanf(hex_ssid + i, "%02x", &hex);
284                         ssid[j++] = hex;
285                 }
286
287                 scan_ssid = g_try_new(struct scan_ssid, 1);
288                 if (scan_ssid == NULL) {
289                         g_free(ssid);
290                         return -ENOMEM;
291                 }
292
293                 memcpy(scan_ssid->ssid, ssid, j);
294                 scan_ssid->ssid_len = j;
295                 scan_data->ssids = g_slist_prepend(scan_data->ssids,
296                                                                 scan_ssid);
297
298                 scan_data->num_ssids++;
299
300                 g_free(ssid);
301         } else
302                 return -EINVAL;
303
304         scan_data->ssids = g_slist_reverse(scan_data->ssids);
305
306         if (scan_data->freqs == NULL) {
307                 scan_data->freqs = g_try_malloc0(sizeof(uint16_t) *
308                                                 scan_data->num_ssids);
309                 if (scan_data->freqs == NULL) {
310                         g_slist_free_full(scan_data->ssids, g_free);
311                         return -ENOMEM;
312                 }
313         } else {
314                 scan_data->freqs = g_try_realloc(scan_data->freqs,
315                                 sizeof(uint16_t) * scan_data->num_ssids);
316                 if (scan_data->freqs == NULL) {
317                         g_slist_free_full(scan_data->ssids, g_free);
318                         return -ENOMEM;
319                 }
320                 scan_data->freqs[scan_data->num_ssids - 1] = 0;
321         }
322
323         /* Don't add duplicate entries */
324         for (i = 0; i < scan_data->num_ssids; i++) {
325                 if (scan_data->freqs[i] == 0) {
326                         scan_data->freqs[i] = freq;
327                         break;
328                 } else if (scan_data->freqs[i] == freq)
329                         break;
330         }
331
332         return 0;
333 }
334
335 static int get_hidden_connections(int max_ssids,
336                                 GSupplicantScanParams *scan_data)
337 {
338         GKeyFile *keyfile;
339         gchar **services;
340         char *ssid;
341         gchar *str;
342         int i, freq;
343         gboolean value;
344         int num_ssids = 0, add_param_failed = 0;
345
346         services = connman_storage_get_services();
347         for (i = 0; services && services[i]; i++) {
348                 if (strncmp(services[i], "wifi_", 5) != 0)
349                         continue;
350
351                 keyfile = connman_storage_load_service(services[i]);
352
353                 value = g_key_file_get_boolean(keyfile,
354                                         services[i], "Hidden", NULL);
355                 if (value == FALSE) {
356                         g_key_file_free(keyfile);
357                         continue;
358                 }
359
360                 value = g_key_file_get_boolean(keyfile,
361                                         services[i], "Favorite", NULL);
362                 if (value == FALSE) {
363                         g_key_file_free(keyfile);
364                         continue;
365                 }
366
367                 value = g_key_file_get_boolean(keyfile,
368                                         services[i], "AutoConnect", NULL);
369                 if (value == FALSE) {
370                         g_key_file_free(keyfile);
371                         continue;
372                 }
373
374                 ssid = g_key_file_get_string(keyfile,
375                                         services[i], "SSID", NULL);
376
377                 freq = g_key_file_get_integer(keyfile, services[i],
378                                         "Frequency", NULL);
379
380                 if (add_scan_param(ssid, freq, scan_data, max_ssids) < 0) {
381                         str = g_key_file_get_string(keyfile,
382                                         services[i], "Name", NULL);
383                         DBG("Cannot scan %s (%s)", ssid, str);
384                         g_free(str);
385                         add_param_failed++;
386                 }
387
388                 num_ssids++;
389
390                 g_key_file_free(keyfile);
391         }
392
393         if (add_param_failed > 0)
394                 connman_warn("Unable to scan %d out of %d SSIDs (max is %d)",
395                         add_param_failed, num_ssids, max_ssids);
396
397         g_strfreev(services);
398
399         return num_ssids > max_ssids ? max_ssids : num_ssids;
400 }
401
402 static int throw_wifi_scan(struct connman_device *device,
403                         GSupplicantInterfaceCallback callback)
404 {
405         struct wifi_data *wifi = connman_device_get_data(device);
406         int ret;
407
408         if (wifi == NULL)
409                 return -ENODEV;
410
411         DBG("device %p %p", device, wifi->interface);
412
413         if (wifi->tethering == TRUE)
414                 return 0;
415
416         if (connman_device_get_scanning(device) == TRUE)
417                 return -EALREADY;
418
419         connman_device_ref(device);
420
421         ret = g_supplicant_interface_scan(wifi->interface, NULL,
422                                                 callback, device);
423         if (ret == 0)
424                 connman_device_set_scanning(device, TRUE);
425         else
426                 connman_device_unref(device);
427
428         return ret;
429 }
430
431 static void hidden_free(struct hidden_params *hidden)
432 {
433         if (hidden == NULL)
434                 return;
435
436         g_free(hidden->identity);
437         g_free(hidden->passphrase);
438         g_free(hidden);
439 }
440
441 static void scan_callback(int result, GSupplicantInterface *interface,
442                                                 void *user_data)
443 {
444         struct connman_device *device = user_data;
445         struct wifi_data *wifi = connman_device_get_data(device);
446
447         DBG("result %d wifi %p", result, wifi);
448
449         if (wifi != NULL && wifi->hidden != NULL) {
450                 connman_network_clear_hidden(wifi->hidden->user_data);
451                 hidden_free(wifi->hidden);
452                 wifi->hidden = NULL;
453         }
454
455         if (result < 0)
456                 connman_device_reset_scanning(device);
457
458         connman_device_set_scanning(device, FALSE);
459         start_autoscan(device);
460         connman_device_unref(device);
461 }
462
463 static void scan_callback_hidden(int result,
464                         GSupplicantInterface *interface, void *user_data)
465 {
466         struct connman_device *device = user_data;
467         struct wifi_data *wifi = connman_device_get_data(device);
468         int driver_max_ssids;
469
470         DBG("result %d wifi %p", result, wifi);
471
472         if (wifi == NULL)
473                 goto out;
474
475         /*
476          * Scan hidden networks so that we can autoconnect to them.
477          */
478         driver_max_ssids = g_supplicant_interface_get_max_scan_ssids(
479                                                         wifi->interface);
480         DBG("max ssids %d", driver_max_ssids);
481
482         if (driver_max_ssids > 0) {
483                 GSupplicantScanParams *scan_params;
484                 int ret;
485
486                 scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
487                 if (scan_params == NULL)
488                         goto out;
489
490                 if (get_hidden_connections(driver_max_ssids,
491                                                 scan_params) > 0) {
492                         ret = g_supplicant_interface_scan(wifi->interface,
493                                                         scan_params,
494                                                         scan_callback,
495                                                         device);
496                         if (ret == 0)
497                                 return;
498                 }
499
500                 g_supplicant_free_scan_params(scan_params);
501         }
502
503 out:
504         scan_callback(result, interface, user_data);
505 }
506
507 static gboolean autoscan_timeout(gpointer data)
508 {
509         struct connman_device *device = data;
510         struct wifi_data *wifi = connman_device_get_data(device);
511         struct autoscan_params *autoscan;
512         int interval;
513
514         if (wifi == NULL)
515                 return FALSE;
516
517         autoscan = wifi->autoscan;
518
519         if (autoscan->interval <= 0) {
520                 interval = autoscan->base;
521                 goto set_interval;
522         } else
523                 interval = autoscan->interval * autoscan->base;
524
525         if (autoscan->interval >= autoscan->limit)
526                 interval = autoscan->limit;
527
528         throw_wifi_scan(wifi->device, scan_callback_hidden);
529
530 set_interval:
531         DBG("interval %d", interval);
532
533         autoscan->interval = interval;
534
535         autoscan->timeout = g_timeout_add_seconds(interval,
536                                                 autoscan_timeout, device);
537
538         return FALSE;
539 }
540
541 static void start_autoscan(struct connman_device *device)
542 {
543         struct wifi_data *wifi = connman_device_get_data(device);
544         struct autoscan_params *autoscan;
545
546         DBG("");
547
548         if (wifi == NULL)
549                 return;
550
551         autoscan = wifi->autoscan;
552         if (autoscan == NULL)
553                 return;
554
555         if (autoscan->timeout > 0 || autoscan->interval > 0)
556                 return;
557
558         connman_device_ref(device);
559
560         autoscan_timeout(device);
561 }
562
563 static struct autoscan_params *parse_autoscan_params(const char *params)
564 {
565         struct autoscan_params *autoscan;
566         char **list_params;
567         int limit;
568         int base;
569
570         DBG("Emulating autoscan");
571
572         list_params = g_strsplit(params, ":", 0);
573         if (list_params == 0)
574                 return NULL;
575
576         if (g_strv_length(list_params) < 3) {
577                 g_strfreev(list_params);
578                 return NULL;
579         }
580
581         base = atoi(list_params[1]);
582         limit = atoi(list_params[2]);
583
584         g_strfreev(list_params);
585
586         autoscan = g_try_malloc0(sizeof(struct autoscan_params));
587         if (autoscan == NULL) {
588                 DBG("Could not allocate memory for autoscan");
589                 return NULL;
590         }
591
592         DBG("base %d - limit %d", base, limit);
593         autoscan->base = base;
594         autoscan->limit = limit;
595
596         return autoscan;
597 }
598
599 static void setup_autoscan(struct wifi_data *wifi)
600 {
601         if (wifi->autoscan == NULL)
602                 wifi->autoscan = parse_autoscan_params(AUTOSCAN_DEFAULT);
603
604         start_autoscan(wifi->device);
605 }
606
607 static void interface_create_callback(int result,
608                                         GSupplicantInterface *interface,
609                                                         void *user_data)
610 {
611         struct wifi_data *wifi = user_data;
612
613         DBG("result %d ifname %s, wifi %p", result,
614                                 g_supplicant_interface_get_ifname(interface),
615                                 wifi);
616
617         if (result < 0 || wifi == NULL)
618                 return;
619
620         wifi->interface = interface;
621         g_supplicant_interface_set_data(interface, wifi);
622
623         if (g_supplicant_interface_get_ready(interface) == FALSE)
624                 return;
625
626         DBG("interface is ready wifi %p tethering %d", wifi, wifi->tethering);
627
628         if (wifi->device == NULL) {
629                 connman_error("WiFi device not set");
630                 return;
631         }
632
633         connman_device_set_powered(wifi->device, TRUE);
634
635         if (connman_setting_get_bool("BackgroundScanning") == FALSE)
636                 return;
637
638         /* Setting up automatic scanning */
639         setup_autoscan(wifi);
640 }
641
642 static int wifi_enable(struct connman_device *device)
643 {
644         struct wifi_data *wifi = connman_device_get_data(device);
645         const char *interface = connman_device_get_string(device, "Interface");
646         const char *driver = connman_option_get_string("wifi");
647         int ret;
648
649         DBG("device %p %p", device, wifi);
650
651         if (wifi == NULL)
652                 return -ENODEV;
653
654         ret = g_supplicant_interface_create(interface, driver, NULL,
655                                                 interface_create_callback,
656                                                         wifi);
657         if (ret < 0)
658                 return ret;
659
660         return -EINPROGRESS;
661 }
662
663 static int wifi_disable(struct connman_device *device)
664 {
665         struct wifi_data *wifi = connman_device_get_data(device);
666         int ret;
667
668         DBG("device %p wifi %p", device, wifi);
669
670         if (wifi == NULL)
671                 return -ENODEV;
672
673         wifi->connected = FALSE;
674         wifi->disconnecting = FALSE;
675
676         if (wifi->pending_network != NULL)
677                 wifi->pending_network = NULL;
678
679         stop_autoscan(device);
680
681         /* In case of a user scan, device is still referenced */
682         if (connman_device_get_scanning(device) == TRUE) {
683                 connman_device_set_scanning(device, FALSE);
684                 connman_device_unref(wifi->device);
685         }
686
687         remove_networks(device, wifi);
688
689         ret = g_supplicant_interface_remove(wifi->interface, NULL, NULL);
690         if (ret < 0)
691                 return ret;
692
693         return -EINPROGRESS;
694 }
695
696 struct last_connected {
697         GTimeVal modified;
698         gchar *ssid;
699         int freq;
700 };
701
702 static gint sort_entry(gconstpointer a, gconstpointer b, gpointer user_data)
703 {
704         GTimeVal *aval = (GTimeVal *)a;
705         GTimeVal *bval = (GTimeVal *)b;
706
707         /* Note that the sort order is descending */
708         if (aval->tv_sec < bval->tv_sec)
709                 return 1;
710
711         if (aval->tv_sec > bval->tv_sec)
712                 return -1;
713
714         return 0;
715 }
716
717 static void free_entry(gpointer data)
718 {
719         struct last_connected *entry = data;
720
721         g_free(entry->ssid);
722         g_free(entry);
723 }
724
725 static int get_latest_connections(int max_ssids,
726                                 GSupplicantScanParams *scan_data)
727 {
728         GSequenceIter *iter;
729         GSequence *latest_list;
730         struct last_connected *entry;
731         GKeyFile *keyfile;
732         GTimeVal modified;
733         gchar **services;
734         gchar *str;
735         char *ssid;
736         int i, freq;
737         int num_ssids = 0;
738
739         latest_list = g_sequence_new(free_entry);
740         if (latest_list == NULL)
741                 return -ENOMEM;
742
743         services = connman_storage_get_services();
744         for (i = 0; services && services[i]; i++) {
745                 if (strncmp(services[i], "wifi_", 5) != 0)
746                         continue;
747
748                 keyfile = connman_storage_load_service(services[i]);
749
750                 str = g_key_file_get_string(keyfile,
751                                         services[i], "Favorite", NULL);
752                 if (str == NULL || g_strcmp0(str, "true")) {
753                         if (str)
754                                 g_free(str);
755                         g_key_file_free(keyfile);
756                         continue;
757                 }
758                 g_free(str);
759
760                 str = g_key_file_get_string(keyfile,
761                                         services[i], "AutoConnect", NULL);
762                 if (str == NULL || g_strcmp0(str, "true")) {
763                         if (str)
764                                 g_free(str);
765                         g_key_file_free(keyfile);
766                         continue;
767                 }
768                 g_free(str);
769
770                 str = g_key_file_get_string(keyfile,
771                                         services[i], "Modified", NULL);
772                 if (str != NULL) {
773                         g_time_val_from_iso8601(str, &modified);
774                         g_free(str);
775                 }
776
777                 ssid = g_key_file_get_string(keyfile,
778                                         services[i], "SSID", NULL);
779
780                 freq = g_key_file_get_integer(keyfile, services[i],
781                                         "Frequency", NULL);
782                 if (freq) {
783                         entry = g_try_new(struct last_connected, 1);
784                         if (entry == NULL) {
785                                 g_sequence_free(latest_list);
786                                 g_key_file_free(keyfile);
787                                 g_free(ssid);
788                                 return -ENOMEM;
789                         }
790
791                         entry->ssid = ssid;
792                         entry->modified = modified;
793                         entry->freq = freq;
794
795                         g_sequence_insert_sorted(latest_list, entry,
796                                                 sort_entry, NULL);
797                         num_ssids++;
798                 } else
799                         g_free(ssid);
800
801                 g_key_file_free(keyfile);
802         }
803
804         g_strfreev(services);
805
806         num_ssids = num_ssids > max_ssids ? max_ssids : num_ssids;
807
808         iter = g_sequence_get_begin_iter(latest_list);
809
810         for (i = 0; i < num_ssids; i++) {
811                 entry = g_sequence_get(iter);
812
813                 DBG("ssid %s freq %d modified %lu", entry->ssid, entry->freq,
814                                                 entry->modified.tv_sec);
815
816                 add_scan_param(entry->ssid, entry->freq, scan_data, max_ssids);
817
818                 iter = g_sequence_iter_next(iter);
819         }
820
821         g_sequence_free(latest_list);
822         return num_ssids;
823 }
824
825 static int wifi_scan(struct connman_device *device)
826 {
827         reset_autoscan(device);
828
829         return throw_wifi_scan(device, scan_callback_hidden);
830 }
831
832 static int wifi_scan_fast(struct connman_device *device)
833 {
834         struct wifi_data *wifi = connman_device_get_data(device);
835         GSupplicantScanParams *scan_params = NULL;
836         int ret;
837         int driver_max_ssids = 0;
838
839         if (wifi == NULL)
840                 return -ENODEV;
841
842         DBG("device %p %p", device, wifi->interface);
843
844         if (wifi->tethering == TRUE)
845                 return 0;
846
847         if (connman_device_get_scanning(device) == TRUE)
848                 return -EALREADY;
849
850         driver_max_ssids = g_supplicant_interface_get_max_scan_ssids(
851                                                         wifi->interface);
852         DBG("max ssids %d", driver_max_ssids);
853         if (driver_max_ssids == 0)
854                 return wifi_scan(device);
855
856         scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
857         if (scan_params == NULL)
858                 return -ENOMEM;
859
860         ret = get_latest_connections(driver_max_ssids, scan_params);
861         if (ret <= 0) {
862                 g_supplicant_free_scan_params(scan_params);
863                 return wifi_scan(device);
864         }
865
866         connman_device_ref(device);
867         reset_autoscan(device);
868
869         ret = g_supplicant_interface_scan(wifi->interface, scan_params,
870                                                 scan_callback, device);
871         if (ret == 0)
872                 connman_device_set_scanning(device, TRUE);
873         else {
874                 g_supplicant_free_scan_params(scan_params);
875                 connman_device_unref(device);
876         }
877
878         return ret;
879 }
880
881 /*
882  * This func is only used when connecting to this specific AP first time.
883  * It is not used when system autoconnects to hidden AP.
884  */
885 static int wifi_scan_hidden(struct connman_device *device,
886                 const char *ssid, unsigned int ssid_len,
887                 const char *identity, const char* passphrase,
888                 gpointer user_data)
889 {
890         struct wifi_data *wifi = connman_device_get_data(device);
891         GSupplicantScanParams *scan_params = NULL;
892         struct scan_ssid *scan_ssid;
893         struct hidden_params *hidden;
894         int ret;
895
896         if (wifi == NULL)
897                 return -ENODEV;
898
899         DBG("hidden SSID %s", ssid);
900
901         if (wifi->tethering == TRUE || wifi->hidden != NULL)
902                 return -EBUSY;
903
904         if (ssid == NULL || ssid_len == 0 || ssid_len > 32)
905                 return -EINVAL;
906
907         if (connman_device_get_scanning(device) == TRUE)
908                 return -EALREADY;
909
910         scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
911         if (scan_params == NULL)
912                 return -ENOMEM;
913
914         scan_ssid = g_try_new(struct scan_ssid, 1);
915         if (scan_ssid == NULL) {
916                 g_free(scan_params);
917                 return -ENOMEM;
918         }
919
920         memcpy(scan_ssid->ssid, ssid, ssid_len);
921         scan_ssid->ssid_len = ssid_len;
922         scan_params->ssids = g_slist_prepend(scan_params->ssids, scan_ssid);
923
924         scan_params->num_ssids = 1;
925
926         hidden = g_try_new0(struct hidden_params, 1);
927         if (hidden == NULL) {
928                 g_free(scan_params);
929                 return -ENOMEM;
930         }
931         memcpy(hidden->ssid, ssid, ssid_len);
932         hidden->ssid_len = ssid_len;
933         hidden->identity = g_strdup(identity);
934         hidden->passphrase = g_strdup(passphrase);
935         hidden->user_data = user_data;
936         wifi->hidden = hidden;
937
938         connman_device_ref(device);
939
940         reset_autoscan(device);
941
942         ret = g_supplicant_interface_scan(wifi->interface, scan_params,
943                         scan_callback, device);
944         if (ret == 0)
945                 connman_device_set_scanning(device, TRUE);
946         else {
947                 connman_device_unref(device);
948                 g_supplicant_free_scan_params(scan_params);
949                 hidden_free(wifi->hidden);
950                 wifi->hidden = NULL;
951         }
952
953         return ret;
954 }
955
956 static void wifi_regdom_callback(int result,
957                                         const char *alpha2,
958                                                 void *user_data)
959 {
960         struct connman_device *device = user_data;
961
962         connman_device_regdom_notify(device, result, alpha2);
963
964         connman_device_unref(device);
965 }
966
967 static int wifi_set_regdom(struct connman_device *device, const char *alpha2)
968 {
969         struct wifi_data *wifi = connman_device_get_data(device);
970         int ret;
971
972         if (wifi == NULL)
973                 return -EINVAL;
974
975         connman_device_ref(device);
976
977         ret = g_supplicant_interface_set_country(wifi->interface,
978                                                 wifi_regdom_callback,
979                                                         alpha2, device);
980         if (ret != 0)
981                 connman_device_unref(device);
982
983         return ret;
984 }
985
986 static struct connman_device_driver wifi_ng_driver = {
987         .name           = "wifi",
988         .type           = CONNMAN_DEVICE_TYPE_WIFI,
989         .priority       = CONNMAN_DEVICE_PRIORITY_LOW,
990         .probe          = wifi_probe,
991         .remove         = wifi_remove,
992         .enable         = wifi_enable,
993         .disable        = wifi_disable,
994         .scan           = wifi_scan,
995         .scan_fast      = wifi_scan_fast,
996         .scan_hidden    = wifi_scan_hidden,
997         .set_regdom     = wifi_set_regdom,
998 };
999
1000 static void system_ready(void)
1001 {
1002         DBG("");
1003
1004         if (connman_device_driver_register(&wifi_ng_driver) < 0)
1005                 connman_error("Failed to register WiFi driver");
1006 }
1007
1008 static void system_killed(void)
1009 {
1010         DBG("");
1011
1012         connman_device_driver_unregister(&wifi_ng_driver);
1013 }
1014
1015 static int network_probe(struct connman_network *network)
1016 {
1017         DBG("network %p", network);
1018
1019         return 0;
1020 }
1021
1022 static void network_remove(struct connman_network *network)
1023 {
1024         struct connman_device *device = connman_network_get_device(network);
1025         struct wifi_data *wifi;
1026
1027         DBG("network %p", network);
1028
1029         wifi = connman_device_get_data(device);
1030         if (wifi == NULL)
1031                 return;
1032
1033         if (wifi->network != network)
1034                 return;
1035
1036         wifi->network = NULL;
1037 }
1038
1039 static void connect_callback(int result, GSupplicantInterface *interface,
1040                                                         void *user_data)
1041 {
1042         struct connman_network *network = user_data;
1043
1044         DBG("network %p result %d", network, result);
1045
1046         if (result == -ENOKEY) {
1047                 connman_network_set_error(network,
1048                                         CONNMAN_NETWORK_ERROR_INVALID_KEY);
1049         } else if (result < 0) {
1050                 connman_network_set_error(network,
1051                                         CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
1052         }
1053 }
1054
1055 static GSupplicantSecurity network_security(const char *security)
1056 {
1057         if (g_str_equal(security, "none") == TRUE)
1058                 return G_SUPPLICANT_SECURITY_NONE;
1059         else if (g_str_equal(security, "wep") == TRUE)
1060                 return G_SUPPLICANT_SECURITY_WEP;
1061         else if (g_str_equal(security, "psk") == TRUE)
1062                 return G_SUPPLICANT_SECURITY_PSK;
1063         else if (g_str_equal(security, "wpa") == TRUE)
1064                 return G_SUPPLICANT_SECURITY_PSK;
1065         else if (g_str_equal(security, "rsn") == TRUE)
1066                 return G_SUPPLICANT_SECURITY_PSK;
1067         else if (g_str_equal(security, "ieee8021x") == TRUE)
1068                 return G_SUPPLICANT_SECURITY_IEEE8021X;
1069
1070         return G_SUPPLICANT_SECURITY_UNKNOWN;
1071 }
1072
1073 static void ssid_init(GSupplicantSSID *ssid, struct connman_network *network)
1074 {
1075         const char *security, *passphrase, *agent_passphrase;
1076
1077         memset(ssid, 0, sizeof(*ssid));
1078         ssid->mode = G_SUPPLICANT_MODE_INFRA;
1079         ssid->ssid = connman_network_get_blob(network, "WiFi.SSID",
1080                                                 &ssid->ssid_len);
1081         ssid->scan_ssid = 1;
1082         security = connman_network_get_string(network, "WiFi.Security");
1083         ssid->security = network_security(security);
1084         passphrase = connman_network_get_string(network,
1085                                                 "WiFi.Passphrase");
1086         if (passphrase == NULL || strlen(passphrase) == 0) {
1087
1088                 /* Use agent provided passphrase as a fallback */
1089                 agent_passphrase = connman_network_get_string(network,
1090                                                 "WiFi.AgentPassphrase");
1091
1092                 if (agent_passphrase == NULL || strlen(agent_passphrase) == 0)
1093                         ssid->passphrase = NULL;
1094                 else
1095                         ssid->passphrase = agent_passphrase;
1096         } else
1097                 ssid->passphrase = passphrase;
1098
1099         ssid->eap = connman_network_get_string(network, "WiFi.EAP");
1100
1101         /*
1102          * If our private key password is unset,
1103          * we use the supplied passphrase. That is needed
1104          * for PEAP where 2 passphrases (identity and client
1105          * cert may have to be provided.
1106          */
1107         if (connman_network_get_string(network,
1108                                         "WiFi.PrivateKeyPassphrase") == NULL)
1109                 connman_network_set_string(network,
1110                                                 "WiFi.PrivateKeyPassphrase",
1111                                                 ssid->passphrase);
1112         /* We must have an identity for both PEAP and TLS */
1113         ssid->identity = connman_network_get_string(network, "WiFi.Identity");
1114
1115         /* Use agent provided identity as a fallback */
1116         if (ssid->identity == NULL || strlen(ssid->identity) == 0)
1117                 ssid->identity = connman_network_get_string(network,
1118                                                         "WiFi.AgentIdentity");
1119
1120         ssid->ca_cert_path = connman_network_get_string(network,
1121                                                         "WiFi.CACertFile");
1122         ssid->client_cert_path = connman_network_get_string(network,
1123                                                         "WiFi.ClientCertFile");
1124         ssid->private_key_path = connman_network_get_string(network,
1125                                                         "WiFi.PrivateKeyFile");
1126         ssid->private_key_passphrase = connman_network_get_string(network,
1127                                                 "WiFi.PrivateKeyPassphrase");
1128         ssid->phase2_auth = connman_network_get_string(network, "WiFi.Phase2");
1129
1130         ssid->use_wps = connman_network_get_bool(network, "WiFi.UseWPS");
1131         ssid->pin_wps = connman_network_get_string(network, "WiFi.PinWPS");
1132
1133         if (connman_setting_get_bool("BackgroundScanning") == TRUE)
1134                 ssid->bgscan = BGSCAN_DEFAULT;
1135 }
1136
1137 static int network_connect(struct connman_network *network)
1138 {
1139         struct connman_device *device = connman_network_get_device(network);
1140         struct wifi_data *wifi;
1141         GSupplicantInterface *interface;
1142         GSupplicantSSID *ssid;
1143
1144         DBG("network %p", network);
1145
1146         if (device == NULL)
1147                 return -ENODEV;
1148
1149         wifi = connman_device_get_data(device);
1150         if (wifi == NULL)
1151                 return -ENODEV;
1152
1153         ssid = g_try_malloc0(sizeof(GSupplicantSSID));
1154         if (ssid == NULL)
1155                 return -ENOMEM;
1156
1157         interface = wifi->interface;
1158
1159         ssid_init(ssid, network);
1160
1161         if (wifi->disconnecting == TRUE)
1162                 wifi->pending_network = network;
1163         else {
1164                 wifi->network = network;
1165                 wifi->retries = 0;
1166
1167                 return g_supplicant_interface_connect(interface, ssid,
1168                                                 connect_callback, network);
1169         }
1170
1171         return -EINPROGRESS;
1172 }
1173
1174 static void disconnect_callback(int result, GSupplicantInterface *interface,
1175                                                                 void *user_data)
1176 {
1177         struct wifi_data *wifi = user_data;
1178
1179         if (wifi->network != NULL) {
1180                 /*
1181                  * if result < 0 supplican return an error because
1182                  * the network is not current.
1183                  * we wont receive G_SUPPLICANT_STATE_DISCONNECTED since it
1184                  * failed, call connman_network_set_connected to report
1185                  * disconnect is completed.
1186                  */
1187                 if (result < 0)
1188                         connman_network_set_connected(wifi->network, FALSE);
1189         }
1190
1191         wifi->network = NULL;
1192
1193         wifi->disconnecting = FALSE;
1194
1195         if (wifi->pending_network != NULL) {
1196                 network_connect(wifi->pending_network);
1197                 wifi->pending_network = NULL;
1198         }
1199
1200         start_autoscan(wifi->device);
1201 }
1202
1203 static int network_disconnect(struct connman_network *network)
1204 {
1205         struct connman_device *device = connman_network_get_device(network);
1206         struct wifi_data *wifi;
1207         int err;
1208
1209         DBG("network %p", network);
1210
1211         wifi = connman_device_get_data(device);
1212         if (wifi == NULL || wifi->interface == NULL)
1213                 return -ENODEV;
1214
1215         connman_network_set_associating(network, FALSE);
1216
1217         if (wifi->disconnecting == TRUE)
1218                 return -EALREADY;
1219
1220         wifi->disconnecting = TRUE;
1221
1222         err = g_supplicant_interface_disconnect(wifi->interface,
1223                                                 disconnect_callback, wifi);
1224         if (err < 0)
1225                 wifi->disconnecting = FALSE;
1226
1227         return err;
1228 }
1229
1230 static struct connman_network_driver network_driver = {
1231         .name           = "wifi",
1232         .type           = CONNMAN_NETWORK_TYPE_WIFI,
1233         .priority       = CONNMAN_NETWORK_PRIORITY_LOW,
1234         .probe          = network_probe,
1235         .remove         = network_remove,
1236         .connect        = network_connect,
1237         .disconnect     = network_disconnect,
1238 };
1239
1240 static void interface_added(GSupplicantInterface *interface)
1241 {
1242         const char *ifname = g_supplicant_interface_get_ifname(interface);
1243         const char *driver = g_supplicant_interface_get_driver(interface);
1244         struct wifi_data *wifi;
1245
1246         wifi = g_supplicant_interface_get_data(interface);
1247
1248         /*
1249          * We can get here with a NULL wifi pointer when
1250          * the interface added signal is sent before the
1251          * interface creation callback is called.
1252          */
1253         if (wifi == NULL)
1254                 return;
1255
1256         DBG("ifname %s driver %s wifi %p tethering %d",
1257                         ifname, driver, wifi, wifi->tethering);
1258
1259         if (wifi->device == NULL) {
1260                 connman_error("WiFi device not set");
1261                 return;
1262         }
1263
1264         connman_device_set_powered(wifi->device, TRUE);
1265
1266         if (wifi->tethering == TRUE)
1267                 return;
1268 }
1269
1270 static connman_bool_t is_idle(struct wifi_data *wifi)
1271 {
1272         DBG("state %d", wifi->state);
1273
1274         switch (wifi->state) {
1275         case G_SUPPLICANT_STATE_UNKNOWN:
1276         case G_SUPPLICANT_STATE_DISCONNECTED:
1277         case G_SUPPLICANT_STATE_INACTIVE:
1278         case G_SUPPLICANT_STATE_SCANNING:
1279                 return TRUE;
1280
1281         case G_SUPPLICANT_STATE_AUTHENTICATING:
1282         case G_SUPPLICANT_STATE_ASSOCIATING:
1283         case G_SUPPLICANT_STATE_ASSOCIATED:
1284         case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
1285         case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
1286         case G_SUPPLICANT_STATE_COMPLETED:
1287                 return FALSE;
1288         }
1289
1290         return FALSE;
1291 }
1292
1293 static connman_bool_t is_idle_wps(GSupplicantInterface *interface,
1294                                                 struct wifi_data *wifi)
1295 {
1296         /* First, let's check if WPS processing did not went wrong */
1297         if (g_supplicant_interface_get_wps_state(interface) ==
1298                 G_SUPPLICANT_WPS_STATE_FAIL)
1299                 return FALSE;
1300
1301         /* Unlike normal connection, being associated while processing wps
1302          * actually means that we are idling. */
1303         switch (wifi->state) {
1304         case G_SUPPLICANT_STATE_UNKNOWN:
1305         case G_SUPPLICANT_STATE_DISCONNECTED:
1306         case G_SUPPLICANT_STATE_INACTIVE:
1307         case G_SUPPLICANT_STATE_SCANNING:
1308         case G_SUPPLICANT_STATE_ASSOCIATED:
1309                 return TRUE;
1310         case G_SUPPLICANT_STATE_AUTHENTICATING:
1311         case G_SUPPLICANT_STATE_ASSOCIATING:
1312         case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
1313         case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
1314         case G_SUPPLICANT_STATE_COMPLETED:
1315                 return FALSE;
1316         }
1317
1318         return FALSE;
1319 }
1320
1321 static connman_bool_t handle_wps_completion(GSupplicantInterface *interface,
1322                                         struct connman_network *network,
1323                                         struct connman_device *device,
1324                                         struct wifi_data *wifi)
1325 {
1326         connman_bool_t wps;
1327
1328         wps = connman_network_get_bool(network, "WiFi.UseWPS");
1329         if (wps == TRUE) {
1330                 const unsigned char *ssid, *wps_ssid;
1331                 unsigned int ssid_len, wps_ssid_len;
1332                 const char *wps_key;
1333
1334                 /* Checking if we got associated with requested
1335                  * network */
1336                 ssid = connman_network_get_blob(network, "WiFi.SSID",
1337                                                 &ssid_len);
1338
1339                 wps_ssid = g_supplicant_interface_get_wps_ssid(
1340                         interface, &wps_ssid_len);
1341
1342                 if (wps_ssid == NULL || wps_ssid_len != ssid_len ||
1343                                 memcmp(ssid, wps_ssid, ssid_len) != 0) {
1344                         connman_network_set_associating(network, FALSE);
1345                         g_supplicant_interface_disconnect(wifi->interface,
1346                                                 disconnect_callback, wifi);
1347                         return FALSE;
1348                 }
1349
1350                 wps_key = g_supplicant_interface_get_wps_key(interface);
1351                 connman_network_set_string(network, "WiFi.Passphrase",
1352                                         wps_key);
1353
1354                 connman_network_set_string(network, "WiFi.PinWPS", NULL);
1355         }
1356
1357         return TRUE;
1358 }
1359
1360 static connman_bool_t handle_4way_handshake_failure(GSupplicantInterface *interface,
1361                                         struct connman_network *network,
1362                                         struct wifi_data *wifi)
1363 {
1364         if (wifi->state != G_SUPPLICANT_STATE_4WAY_HANDSHAKE)
1365                 return FALSE;
1366
1367         wifi->retries++;
1368
1369         if (wifi->retries < MAXIMUM_RETRIES)
1370                 return TRUE;
1371
1372         connman_network_set_error(network, CONNMAN_NETWORK_ERROR_INVALID_KEY);
1373
1374         return FALSE;
1375 }
1376
1377 static void interface_state(GSupplicantInterface *interface)
1378 {
1379         struct connman_network *network;
1380         struct connman_device *device;
1381         struct wifi_data *wifi;
1382         GSupplicantState state = g_supplicant_interface_get_state(interface);
1383         connman_bool_t wps;
1384
1385         wifi = g_supplicant_interface_get_data(interface);
1386
1387         DBG("wifi %p interface state %d", wifi, state);
1388
1389         if (wifi == NULL)
1390                 return;
1391
1392         network = wifi->network;
1393         device = wifi->device;
1394
1395         if (device == NULL || network == NULL)
1396                 return;
1397
1398         switch (state) {
1399         case G_SUPPLICANT_STATE_SCANNING:
1400                 break;
1401
1402         case G_SUPPLICANT_STATE_AUTHENTICATING:
1403         case G_SUPPLICANT_STATE_ASSOCIATING:
1404                 stop_autoscan(device);
1405
1406                 if (wifi->connected == FALSE)
1407                         connman_network_set_associating(network, TRUE);
1408
1409                 break;
1410
1411         case G_SUPPLICANT_STATE_COMPLETED:
1412                 /* though it should be already stopped: */
1413                 stop_autoscan(device);
1414
1415                 if (handle_wps_completion(interface, network, device, wifi) ==
1416                                                                         FALSE)
1417                         break;
1418
1419                 connman_network_set_connected(network, TRUE);
1420                 break;
1421
1422         case G_SUPPLICANT_STATE_DISCONNECTED:
1423                 /*
1424                  * If we're in one of the idle modes, we have
1425                  * not started association yet and thus setting
1426                  * those ones to FALSE could cancel an association
1427                  * in progress.
1428                  */
1429                 wps = connman_network_get_bool(network, "WiFi.UseWPS");
1430                 if (wps == TRUE)
1431                         if (is_idle_wps(interface, wifi) == TRUE)
1432                                 break;
1433
1434                 if (is_idle(wifi))
1435                         break;
1436
1437                 /* If previous state was 4way-handshake, then
1438                  * it's either: psk was incorrect and thus we retry
1439                  * or if we reach the maximum retries we declare the
1440                  * psk as wrong */
1441                 if (handle_4way_handshake_failure(interface,
1442                                                 network, wifi) == TRUE)
1443                         break;
1444
1445                 /* We disable the selected network, if not then
1446                  * wpa_supplicant will loop retrying */
1447                 if (g_supplicant_interface_enable_selected_network(interface,
1448                                                 FALSE) != 0)
1449                         DBG("Could not disables selected network");
1450
1451                 connman_network_set_connected(network, FALSE);
1452                 connman_network_set_associating(network, FALSE);
1453                 wifi->disconnecting = FALSE;
1454
1455                 start_autoscan(device);
1456
1457                 break;
1458
1459         case G_SUPPLICANT_STATE_INACTIVE:
1460                 connman_network_set_associating(network, FALSE);
1461                 start_autoscan(device);
1462
1463                 break;
1464
1465         case G_SUPPLICANT_STATE_UNKNOWN:
1466         case G_SUPPLICANT_STATE_ASSOCIATED:
1467         case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
1468         case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
1469                 break;
1470         }
1471
1472         wifi->state = state;
1473
1474         /* Saving wpa_s state policy:
1475          * If connected and if the state changes are roaming related:
1476          * --> We stay connected
1477          * If completed
1478          * --> We are connected
1479          * All other case:
1480          * --> We are not connected
1481          * */
1482         switch (state) {
1483         case G_SUPPLICANT_STATE_AUTHENTICATING:
1484         case G_SUPPLICANT_STATE_ASSOCIATING:
1485         case G_SUPPLICANT_STATE_ASSOCIATED:
1486         case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
1487         case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
1488                 if (wifi->connected == TRUE)
1489                         connman_warn("Probably roaming right now!"
1490                                                 " Staying connected...");
1491                 else
1492                         wifi->connected = FALSE;
1493                 break;
1494         case G_SUPPLICANT_STATE_COMPLETED:
1495                 wifi->connected = TRUE;
1496                 break;
1497         default:
1498                 wifi->connected = FALSE;
1499                 break;
1500         }
1501
1502         DBG("DONE");
1503 }
1504
1505 static void interface_removed(GSupplicantInterface *interface)
1506 {
1507         const char *ifname = g_supplicant_interface_get_ifname(interface);
1508         struct wifi_data *wifi;
1509
1510         DBG("ifname %s", ifname);
1511
1512         wifi = g_supplicant_interface_get_data(interface);
1513
1514         if (wifi != NULL && wifi->tethering == TRUE)
1515                 return;
1516
1517         if (wifi == NULL || wifi->device == NULL) {
1518                 DBG("wifi interface already removed");
1519                 return;
1520         }
1521
1522         wifi->interface = NULL;
1523         connman_device_set_powered(wifi->device, FALSE);
1524 }
1525
1526 static void scan_started(GSupplicantInterface *interface)
1527 {
1528         DBG("");
1529 }
1530
1531 static void scan_finished(GSupplicantInterface *interface)
1532 {
1533         DBG("");
1534 }
1535
1536 static unsigned char calculate_strength(GSupplicantNetwork *supplicant_network)
1537 {
1538         unsigned char strength;
1539
1540         strength = 120 + g_supplicant_network_get_signal(supplicant_network);
1541         if (strength > 100)
1542                 strength = 100;
1543
1544         return strength;
1545 }
1546
1547 static void network_added(GSupplicantNetwork *supplicant_network)
1548 {
1549         struct connman_network *network;
1550         GSupplicantInterface *interface;
1551         struct wifi_data *wifi;
1552         const char *name, *identifier, *security, *group, *mode;
1553         const unsigned char *ssid;
1554         unsigned int ssid_len;
1555         connman_bool_t wps;
1556         connman_bool_t wps_pbc;
1557         connman_bool_t wps_ready;
1558         connman_bool_t wps_advertizing;
1559
1560         DBG("");
1561
1562         interface = g_supplicant_network_get_interface(supplicant_network);
1563         wifi = g_supplicant_interface_get_data(interface);
1564         name = g_supplicant_network_get_name(supplicant_network);
1565         identifier = g_supplicant_network_get_identifier(supplicant_network);
1566         security = g_supplicant_network_get_security(supplicant_network);
1567         group = g_supplicant_network_get_identifier(supplicant_network);
1568         wps = g_supplicant_network_get_wps(supplicant_network);
1569         wps_pbc = g_supplicant_network_is_wps_pbc(supplicant_network);
1570         wps_ready = g_supplicant_network_is_wps_active(supplicant_network);
1571         wps_advertizing = g_supplicant_network_is_wps_advertizing(
1572                                                         supplicant_network);
1573         mode = g_supplicant_network_get_mode(supplicant_network);
1574
1575         if (wifi == NULL)
1576                 return;
1577
1578         ssid = g_supplicant_network_get_ssid(supplicant_network, &ssid_len);
1579
1580         network = connman_device_get_network(wifi->device, identifier);
1581
1582         if (network == NULL) {
1583                 network = connman_network_create(identifier,
1584                                                 CONNMAN_NETWORK_TYPE_WIFI);
1585                 if (network == NULL)
1586                         return;
1587
1588                 connman_network_set_index(network, wifi->index);
1589
1590                 if (connman_device_add_network(wifi->device, network) < 0) {
1591                         connman_network_unref(network);
1592                         return;
1593                 }
1594
1595                 wifi->networks = g_slist_append(wifi->networks, network);
1596         }
1597
1598         if (name != NULL && name[0] != '\0')
1599                 connman_network_set_name(network, name);
1600
1601         connman_network_set_blob(network, "WiFi.SSID",
1602                                                 ssid, ssid_len);
1603         connman_network_set_string(network, "WiFi.Security", security);
1604         connman_network_set_strength(network,
1605                                 calculate_strength(supplicant_network));
1606         connman_network_set_bool(network, "WiFi.WPS", wps);
1607
1608         if (wps == TRUE) {
1609                 /* Is AP advertizing for WPS association?
1610                  * If so, we decide to use WPS by default */
1611                 if (wps_ready == TRUE && wps_pbc == TRUE &&
1612                                                 wps_advertizing == TRUE)
1613                         connman_network_set_bool(network, "WiFi.UseWPS", TRUE);
1614         }
1615
1616         connman_network_set_frequency(network,
1617                         g_supplicant_network_get_frequency(supplicant_network));
1618
1619         connman_network_set_available(network, TRUE);
1620         connman_network_set_string(network, "WiFi.Mode", mode);
1621
1622         if (ssid != NULL)
1623                 connman_network_set_group(network, group);
1624
1625         if (wifi->hidden != NULL && ssid != NULL) {
1626                 if (wifi->hidden->ssid_len == ssid_len &&
1627                                 memcmp(wifi->hidden->ssid, ssid,
1628                                                 ssid_len) == 0) {
1629                         connman_network_connect_hidden(network,
1630                                         wifi->hidden->identity,
1631                                         wifi->hidden->passphrase,
1632                                         wifi->hidden->user_data);
1633                         wifi->hidden->user_data = NULL;
1634                         hidden_free(wifi->hidden);
1635                         wifi->hidden = NULL;
1636                 }
1637         }
1638 }
1639
1640 static void network_removed(GSupplicantNetwork *network)
1641 {
1642         GSupplicantInterface *interface;
1643         struct wifi_data *wifi;
1644         const char *name, *identifier;
1645         struct connman_network *connman_network;
1646
1647         interface = g_supplicant_network_get_interface(network);
1648         wifi = g_supplicant_interface_get_data(interface);
1649         identifier = g_supplicant_network_get_identifier(network);
1650         name = g_supplicant_network_get_name(network);
1651
1652         DBG("name %s", name);
1653
1654         if (wifi == NULL)
1655                 return;
1656
1657         connman_network = connman_device_get_network(wifi->device, identifier);
1658         if (connman_network == NULL)
1659                 return;
1660
1661         wifi->networks = g_slist_remove(wifi->networks, connman_network);
1662
1663         connman_device_remove_network(wifi->device, connman_network);
1664         connman_network_unref(connman_network);
1665 }
1666
1667 static void network_changed(GSupplicantNetwork *network, const char *property)
1668 {
1669         GSupplicantInterface *interface;
1670         struct wifi_data *wifi;
1671         const char *name, *identifier;
1672         struct connman_network *connman_network;
1673
1674         interface = g_supplicant_network_get_interface(network);
1675         wifi = g_supplicant_interface_get_data(interface);
1676         identifier = g_supplicant_network_get_identifier(network);
1677         name = g_supplicant_network_get_name(network);
1678
1679         DBG("name %s", name);
1680
1681         if (wifi == NULL)
1682                 return;
1683
1684         connman_network = connman_device_get_network(wifi->device, identifier);
1685         if (connman_network == NULL)
1686                 return;
1687
1688         if (g_str_equal(property, "Signal") == TRUE) {
1689                connman_network_set_strength(connman_network,
1690                                         calculate_strength(network));
1691                connman_network_update(connman_network);
1692         }
1693 }
1694
1695 static void debug(const char *str)
1696 {
1697         if (getenv("CONNMAN_SUPPLICANT_DEBUG"))
1698                 connman_debug("%s", str);
1699 }
1700
1701 static const GSupplicantCallbacks callbacks = {
1702         .system_ready           = system_ready,
1703         .system_killed          = system_killed,
1704         .interface_added        = interface_added,
1705         .interface_state        = interface_state,
1706         .interface_removed      = interface_removed,
1707         .scan_started           = scan_started,
1708         .scan_finished          = scan_finished,
1709         .network_added          = network_added,
1710         .network_removed        = network_removed,
1711         .network_changed        = network_changed,
1712         .debug                  = debug,
1713 };
1714
1715
1716 static int tech_probe(struct connman_technology *technology)
1717 {
1718         wifi_technology = technology;
1719
1720         return 0;
1721 }
1722
1723 static void tech_remove(struct connman_technology *technology)
1724 {
1725         wifi_technology = NULL;
1726 }
1727
1728 struct wifi_tethering_info {
1729         struct wifi_data *wifi;
1730         struct connman_technology *technology;
1731         char *ifname;
1732         GSupplicantSSID *ssid;
1733 };
1734
1735 static GSupplicantSSID *ssid_ap_init(const char *ssid, const char *passphrase)
1736 {
1737         GSupplicantSSID *ap;
1738
1739         ap = g_try_malloc0(sizeof(GSupplicantSSID));
1740         if (ap == NULL)
1741                 return NULL;
1742
1743         ap->mode = G_SUPPLICANT_MODE_MASTER;
1744         ap->ssid = ssid;
1745         ap->ssid_len = strlen(ssid);
1746         ap->scan_ssid = 0;
1747         ap->freq = 2412;
1748
1749         if (passphrase == NULL || strlen(passphrase) == 0) {
1750                 ap->security = G_SUPPLICANT_SECURITY_NONE;
1751                 ap->passphrase = NULL;
1752         } else {
1753                ap->security = G_SUPPLICANT_SECURITY_PSK;
1754                ap->protocol = G_SUPPLICANT_PROTO_RSN;
1755                ap->pairwise_cipher = G_SUPPLICANT_PAIRWISE_CCMP;
1756                ap->group_cipher = G_SUPPLICANT_GROUP_CCMP;
1757                ap->passphrase = passphrase;
1758         }
1759
1760         return ap;
1761 }
1762
1763 static void ap_start_callback(int result, GSupplicantInterface *interface,
1764                                                         void *user_data)
1765 {
1766         struct wifi_tethering_info *info = user_data;
1767
1768         DBG("result %d index %d bridge %s",
1769                 result, info->wifi->index, info->wifi->bridge);
1770
1771         if (result < 0) {
1772                 connman_inet_remove_from_bridge(info->wifi->index,
1773                                                         info->wifi->bridge);
1774                 connman_technology_tethering_notify(info->technology, FALSE);
1775         }
1776
1777         g_free(info->ifname);
1778         g_free(info);
1779 }
1780
1781 static void ap_create_callback(int result,
1782                                 GSupplicantInterface *interface,
1783                                         void *user_data)
1784 {
1785         struct wifi_tethering_info *info = user_data;
1786
1787         DBG("result %d ifname %s", result,
1788                                 g_supplicant_interface_get_ifname(interface));
1789
1790         if (result < 0) {
1791                 connman_inet_remove_from_bridge(info->wifi->index,
1792                                                         info->wifi->bridge);
1793                 connman_technology_tethering_notify(info->technology, FALSE);
1794
1795                 g_free(info->ifname);
1796                 g_free(info);
1797                 return;
1798         }
1799
1800         info->wifi->interface = interface;
1801         g_supplicant_interface_set_data(interface, info->wifi);
1802
1803         if (g_supplicant_interface_set_apscan(interface, 2) < 0)
1804                 connman_error("Failed to set interface ap_scan property");
1805
1806         g_supplicant_interface_connect(interface, info->ssid,
1807                                                 ap_start_callback, info);
1808 }
1809
1810 static void sta_remove_callback(int result,
1811                                 GSupplicantInterface *interface,
1812                                         void *user_data)
1813 {
1814         struct wifi_tethering_info *info = user_data;
1815         const char *driver = connman_option_get_string("wifi");
1816
1817         DBG("ifname %s result %d ", info->ifname, result);
1818
1819         if (result < 0) {
1820                 info->wifi->tethering = TRUE;
1821
1822                 g_free(info->ifname);
1823                 g_free(info);
1824                 return;
1825         }
1826
1827         info->wifi->interface = NULL;
1828
1829         connman_technology_tethering_notify(info->technology, TRUE);
1830
1831         g_supplicant_interface_create(info->ifname, driver, info->wifi->bridge,
1832                                                 ap_create_callback,
1833                                                         info);
1834 }
1835
1836 static int tech_set_tethering(struct connman_technology *technology,
1837                                 const char *identifier, const char *passphrase,
1838                                 const char *bridge, connman_bool_t enabled)
1839 {
1840         GList *list;
1841         GSupplicantInterface *interface;
1842         struct wifi_data *wifi;
1843         struct wifi_tethering_info *info;
1844         const char *ifname;
1845         unsigned int mode;
1846         int err;
1847
1848         DBG("");
1849
1850         if (enabled == FALSE) {
1851                 for (list = iface_list; list; list = list->next) {
1852                         wifi = list->data;
1853
1854                         if (wifi->tethering == TRUE) {
1855                                 wifi->tethering = FALSE;
1856
1857                                 connman_inet_remove_from_bridge(wifi->index,
1858                                                                         bridge);
1859                                 wifi->bridged = FALSE;
1860                         }
1861                 }
1862
1863                 connman_technology_tethering_notify(technology, FALSE);
1864
1865                 return 0;
1866         }
1867
1868         for (list = iface_list; list; list = list->next) {
1869                 wifi = list->data;
1870
1871                 interface = wifi->interface;
1872
1873                 if (interface == NULL)
1874                         continue;
1875
1876                 ifname = g_supplicant_interface_get_ifname(wifi->interface);
1877
1878                 mode = g_supplicant_interface_get_mode(interface);
1879                 if ((mode & G_SUPPLICANT_CAPABILITY_MODE_AP) == 0) {
1880                         DBG("%s does not support AP mode", ifname);
1881                         continue;
1882                 }
1883
1884                 info = g_try_malloc0(sizeof(struct wifi_tethering_info));
1885                 if (info == NULL)
1886                         return -ENOMEM;
1887
1888                 info->wifi = wifi;
1889                 info->technology = technology;
1890                 info->wifi->bridge = bridge;
1891                 info->ssid = ssid_ap_init(identifier, passphrase);
1892                 if (info->ssid == NULL) {
1893                         g_free(info);
1894                         continue;
1895                 }
1896                 info->ifname = g_strdup(ifname);
1897                 if (info->ifname == NULL) {
1898                         g_free(info);
1899                         continue;
1900                 }
1901
1902                 info->wifi->tethering = TRUE;
1903
1904                 err = g_supplicant_interface_remove(interface,
1905                                                 sta_remove_callback,
1906                                                         info);
1907                 if (err == 0)
1908                         return err;
1909         }
1910
1911         return -EOPNOTSUPP;
1912 }
1913
1914 static void regdom_callback(int result, const char *alpha2, void *user_data)
1915 {
1916         DBG("");
1917
1918         if (wifi_technology == NULL)
1919                 return;
1920
1921         if (result != 0)
1922                 alpha2 = NULL;
1923
1924         connman_technology_regdom_notify(wifi_technology, alpha2);
1925 }
1926
1927 static int tech_set_regdom(struct connman_technology *technology, const char *alpha2)
1928 {
1929         return g_supplicant_set_country(alpha2, regdom_callback, NULL);
1930 }
1931
1932 static struct connman_technology_driver tech_driver = {
1933         .name           = "wifi",
1934         .type           = CONNMAN_SERVICE_TYPE_WIFI,
1935         .probe          = tech_probe,
1936         .remove         = tech_remove,
1937         .set_tethering  = tech_set_tethering,
1938         .set_regdom     = tech_set_regdom,
1939 };
1940
1941 static int wifi_init(void)
1942 {
1943         int err;
1944
1945         err = connman_network_driver_register(&network_driver);
1946         if (err < 0)
1947                 return err;
1948
1949         err = g_supplicant_register(&callbacks);
1950         if (err < 0) {
1951                 connman_network_driver_unregister(&network_driver);
1952                 return err;
1953         }
1954
1955         err = connman_technology_driver_register(&tech_driver);
1956         if (err < 0) {
1957                 g_supplicant_unregister(&callbacks);
1958                 connman_network_driver_unregister(&network_driver);
1959                 return err;
1960         }
1961
1962         return 0;
1963 }
1964
1965 static void wifi_exit(void)
1966 {
1967         DBG();
1968
1969         connman_technology_driver_unregister(&tech_driver);
1970
1971         g_supplicant_unregister(&callbacks);
1972
1973         connman_network_driver_unregister(&network_driver);
1974 }
1975
1976 CONNMAN_PLUGIN_DEFINE(wifi, "WiFi interface plugin", VERSION,
1977                 CONNMAN_PLUGIN_PRIORITY_DEFAULT, wifi_init, wifi_exit)