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