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