wifi: Fix network owner ship
[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 <string.h>
30 #include <sys/ioctl.h>
31 #include <sys/socket.h>
32 #include <linux/if_arp.h>
33 #include <linux/wireless.h>
34 #include <net/ethernet.h>
35
36 #ifndef IFF_LOWER_UP
37 #define IFF_LOWER_UP    0x10000
38 #endif
39
40 #include <dbus/dbus.h>
41 #include <glib.h>
42
43 #define CONNMAN_API_SUBJECT_TO_CHANGE
44 #include <connman/plugin.h>
45 #include <connman/inet.h>
46 #include <connman/device.h>
47 #include <connman/rtnl.h>
48 #include <connman/technology.h>
49 #include <connman/log.h>
50 #include <connman/option.h>
51
52 #include <gsupplicant/gsupplicant.h>
53
54 #define CLEANUP_TIMEOUT   8     /* in seconds */
55 #define INACTIVE_TIMEOUT  12    /* in seconds */
56
57 struct connman_technology *wifi_technology = NULL;
58
59 struct wifi_data {
60         char *identifier;
61         struct connman_device *device;
62         struct connman_network *network;
63         struct connman_network *pending_network;
64         GSList *networks;
65         GSupplicantInterface *interface;
66         GSupplicantState state;
67         connman_bool_t connected;
68         connman_bool_t disconnecting;
69         connman_bool_t tethering;
70         connman_bool_t bridged;
71         const char *bridge;
72         int index;
73         unsigned flags;
74         unsigned int watch;
75 };
76
77 static GList *iface_list = NULL;
78
79 static void handle_tethering(struct wifi_data *wifi)
80 {
81         if (wifi->tethering == FALSE)
82                 return;
83
84         if (wifi->bridge == NULL)
85                 return;
86
87         if (wifi->bridged == TRUE)
88                 return;
89
90         DBG("index %d bridge %s", wifi->index, wifi->bridge);
91
92         if (connman_inet_add_to_bridge(wifi->index, wifi->bridge) < 0)
93                 return;
94
95         wifi->bridged = TRUE;
96 }
97
98 static void wifi_newlink(unsigned flags, unsigned change, void *user_data)
99 {
100         struct connman_device *device = user_data;
101         struct wifi_data *wifi = connman_device_get_data(device);
102
103         DBG("index %d flags %d change %d", wifi->index, flags, change);
104
105         if (!change)
106                 return;
107
108         if ((wifi->flags & IFF_UP) != (flags & IFF_UP)) {
109                 if (flags & IFF_UP)
110                         DBG("interface up");
111                 else
112                         DBG("interface down");
113         }
114
115         if ((wifi->flags & IFF_LOWER_UP) != (flags & IFF_LOWER_UP)) {
116                 if (flags & IFF_LOWER_UP) {
117                         DBG("carrier on");
118
119                         handle_tethering(wifi);
120                 } else
121                         DBG("carrier off");
122         }
123
124         wifi->flags = flags;
125 }
126
127 static int wifi_probe(struct connman_device *device)
128 {
129         struct wifi_data *wifi;
130
131         DBG("device %p", device);
132
133         wifi = g_try_new0(struct wifi_data, 1);
134         if (wifi == NULL)
135                 return -ENOMEM;
136
137         wifi->connected = FALSE;
138         wifi->disconnecting = FALSE;
139         wifi->tethering = FALSE;
140         wifi->bridged = FALSE;
141         wifi->bridge = NULL;
142         wifi->state = G_SUPPLICANT_STATE_INACTIVE;
143
144         connman_device_set_data(device, wifi);
145         wifi->device = connman_device_ref(device);
146
147         wifi->index = connman_device_get_index(device);
148         wifi->flags = 0;
149
150         wifi->watch = connman_rtnl_add_newlink_watch(wifi->index,
151                                                         wifi_newlink, device);
152
153         iface_list = g_list_append(iface_list, wifi);
154
155         return 0;
156 }
157
158 static void remove_networks(struct connman_device *device,
159                                 struct wifi_data *wifi)
160 {
161         GSList *list;
162
163         for (list = wifi->networks; list != NULL; list = list->next) {
164                 struct connman_network *network = list->data;
165
166                 connman_device_remove_network(device, network);
167                 connman_network_unref(network);
168         }
169
170         g_slist_free(wifi->networks);
171         wifi->networks = NULL;
172 }
173
174 static void wifi_remove(struct connman_device *device)
175 {
176         struct wifi_data *wifi = connman_device_get_data(device);
177
178         DBG("device %p", device);
179
180         if (wifi == NULL)
181                 return;
182
183         iface_list = g_list_remove(iface_list, wifi);
184
185         remove_networks(device, wifi);
186
187         connman_device_set_data(device, NULL);
188         connman_device_unref(wifi->device);
189         connman_rtnl_remove_watch(wifi->watch);
190
191         g_supplicant_interface_set_data(wifi->interface, NULL);
192
193         g_free(wifi->identifier);
194         g_free(wifi);
195 }
196
197 static void interface_create_callback(int result,
198                                         GSupplicantInterface *interface,
199                                                         void *user_data)
200 {
201         struct wifi_data *wifi = user_data;
202
203         DBG("result %d ifname %s", result,
204                                 g_supplicant_interface_get_ifname(interface));
205
206         if (result < 0)
207                 return;
208
209         wifi->interface = interface;
210         g_supplicant_interface_set_data(interface, wifi);
211 }
212
213 static void interface_remove_callback(int result,
214                                         GSupplicantInterface *interface,
215                                                         void *user_data)
216 {
217         struct wifi_data *wifi = user_data;
218
219         DBG("result %d", result);
220
221         if (result < 0)
222                 return;
223
224         wifi->interface = NULL;
225 }
226
227
228 static int wifi_enable(struct connman_device *device)
229 {
230         struct wifi_data *wifi = connman_device_get_data(device);
231         const char *interface = connman_device_get_string(device, "Interface");
232         const char *driver = connman_option_get_string("wifi");
233         int ret;
234
235         DBG("device %p %p", device, wifi);
236
237         ret = g_supplicant_interface_create(interface, driver, NULL,
238                                                 interface_create_callback,
239                                                         wifi);
240         if (ret < 0)
241                 return ret;
242
243         return -EINPROGRESS;
244 }
245
246 static int wifi_disable(struct connman_device *device)
247 {
248         struct wifi_data *wifi = connman_device_get_data(device);
249         int ret;
250
251         DBG("device %p", device);
252
253         wifi->connected = FALSE;
254         wifi->disconnecting = FALSE;
255
256         if (wifi->pending_network != NULL)
257                 wifi->pending_network = NULL;
258
259         remove_networks(device, wifi);
260
261         ret = g_supplicant_interface_remove(wifi->interface,
262                                                 interface_remove_callback,
263                                                         wifi);
264         if (ret < 0)
265                 return ret;
266
267         return -EINPROGRESS;
268 }
269
270 static void scan_callback(int result, GSupplicantInterface *interface,
271                                                 void *user_data)
272 {
273         struct connman_device *device = user_data;
274
275         DBG("result %d", result);
276
277         if (result < 0)
278                 connman_device_reset_scanning(device);
279         else
280                 connman_device_set_scanning(device, FALSE);
281 }
282
283 static int wifi_scan(struct connman_device *device)
284 {
285         struct wifi_data *wifi = connman_device_get_data(device);
286         int ret;
287
288         DBG("device %p %p", device, wifi->interface);
289
290         if (wifi->tethering == TRUE)
291                 return 0;
292
293         ret = g_supplicant_interface_scan(wifi->interface, scan_callback,
294                                                                 device);
295         if (ret == 0)
296                 connman_device_set_scanning(device, TRUE);
297
298         return ret;
299 }
300
301 static struct connman_device_driver wifi_ng_driver = {
302         .name           = "wifi",
303         .type           = CONNMAN_DEVICE_TYPE_WIFI,
304         .priority       = CONNMAN_DEVICE_PRIORITY_LOW,
305         .probe          = wifi_probe,
306         .remove         = wifi_remove,
307         .enable         = wifi_enable,
308         .disable        = wifi_disable,
309         .scan           = wifi_scan,
310 };
311
312 static void system_ready(void)
313 {
314         DBG("");
315
316         if (connman_device_driver_register(&wifi_ng_driver) < 0)
317                 connman_error("Failed to register WiFi driver");
318 }
319
320 static void system_killed(void)
321 {
322         DBG("");
323
324         connman_device_driver_unregister(&wifi_ng_driver);
325 }
326
327 static int network_probe(struct connman_network *network)
328 {
329         DBG("network %p", network);
330
331         return 0;
332 }
333
334 static void network_remove(struct connman_network *network)
335 {
336         DBG("network %p", network);
337 }
338
339 static void connect_callback(int result, GSupplicantInterface *interface,
340                                                         void *user_data)
341 {
342         struct connman_network *network = user_data;
343
344         DBG("network %p result %d", network, result);
345
346         if (result == -ENOKEY) {
347                 connman_network_set_error(network,
348                                         CONNMAN_NETWORK_ERROR_INVALID_KEY);
349         } else if (result < 0) {
350                 connman_network_set_error(network,
351                                         CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
352         }
353 }
354
355 static GSupplicantSecurity network_security(const char *security)
356 {
357         if (g_str_equal(security, "none") == TRUE)
358                 return G_SUPPLICANT_SECURITY_NONE;
359         else if (g_str_equal(security, "wep") == TRUE)
360                 return G_SUPPLICANT_SECURITY_WEP;
361         else if (g_str_equal(security, "psk") == TRUE)
362                 return G_SUPPLICANT_SECURITY_PSK;
363         else if (g_str_equal(security, "wpa") == TRUE)
364                 return G_SUPPLICANT_SECURITY_PSK;
365         else if (g_str_equal(security, "rsn") == TRUE)
366                 return G_SUPPLICANT_SECURITY_PSK;
367         else if (g_str_equal(security, "ieee8021x") == TRUE)
368                 return G_SUPPLICANT_SECURITY_IEEE8021X;
369
370         return G_SUPPLICANT_SECURITY_UNKNOWN;
371 }
372
373 static void ssid_init(GSupplicantSSID *ssid, struct connman_network *network)
374 {
375         const char *security, *passphrase;
376
377         memset(ssid, 0, sizeof(*ssid));
378         ssid->mode = G_SUPPLICANT_MODE_INFRA;
379         ssid->ssid = connman_network_get_blob(network, "WiFi.SSID",
380                                                 &ssid->ssid_len);
381         ssid->scan_ssid = 1;
382         security = connman_network_get_string(network, "WiFi.Security");
383         ssid->security = network_security(security);
384         passphrase = connman_network_get_string(network,
385                                                 "WiFi.Passphrase");
386         if (passphrase == NULL || strlen(passphrase) == 0)
387                 ssid->passphrase = NULL;
388         else
389                 ssid->passphrase = passphrase;
390
391         ssid->eap = connman_network_get_string(network, "WiFi.EAP");
392
393         /*
394          * If our private key password is unset,
395          * we use the supplied passphrase. That is needed
396          * for PEAP where 2 passphrases (identity and client
397          * cert may have to be provided.
398          */
399         if (connman_network_get_string(network,
400                                         "WiFi.PrivateKeyPassphrase") == NULL)
401                 connman_network_set_string(network,
402                                                 "WiFi.PrivateKeyPassphrase",
403                                                 ssid->passphrase);
404         /* We must have an identity for both PEAP and TLS */
405         ssid->identity = connman_network_get_string(network, "WiFi.Identity");
406         ssid->ca_cert_path = connman_network_get_string(network,
407                                                         "WiFi.CACertFile");
408         ssid->client_cert_path = connman_network_get_string(network,
409                                                         "WiFi.ClientCertFile");
410         ssid->private_key_path = connman_network_get_string(network,
411                                                         "WiFi.PrivateKeyFile");
412         ssid->private_key_passphrase = connman_network_get_string(network,
413                                                 "WiFi.PrivateKeyPassphrase");
414         ssid->phase2_auth = connman_network_get_string(network, "WiFi.Phase2");
415
416         ssid->use_wps = connman_network_get_bool(network, "WiFi.UseWPS");
417         ssid->pin_wps = connman_network_get_string(network, "WiFi.PinWPS");
418
419 }
420
421 static int network_connect(struct connman_network *network)
422 {
423         struct connman_device *device = connman_network_get_device(network);
424         struct wifi_data *wifi;
425         GSupplicantInterface *interface;
426         GSupplicantSSID *ssid;
427
428         DBG("network %p", network);
429
430         if (device == NULL)
431                 return -ENODEV;
432
433         wifi = connman_device_get_data(device);
434         if (wifi == NULL)
435                 return -ENODEV;
436
437         ssid = g_try_malloc0(sizeof(GSupplicantSSID));
438         if (ssid == NULL)
439                 return -ENOMEM;
440
441         interface = wifi->interface;
442
443         ssid_init(ssid, network);
444
445         if (wifi->disconnecting == TRUE)
446                 wifi->pending_network = network;
447         else {
448                 wifi->network = network;
449
450                 return g_supplicant_interface_connect(interface, ssid,
451                                                 connect_callback, network);
452         }
453
454         return -EINPROGRESS;
455 }
456
457 static void disconnect_callback(int result, GSupplicantInterface *interface,
458                                                                 void *user_data)
459 {
460         struct wifi_data *wifi = user_data;
461
462         if (wifi->network != NULL) {
463                 /*
464                  * if result < 0 supplican return an error because
465                  * the network is not current.
466                  * we wont receive G_SUPPLICANT_STATE_DISCONNECTED since it
467                  * failed, call connman_network_set_connected to report
468                  * disconnect is completed.
469                  */
470                 if (result < 0)
471                         connman_network_set_connected(wifi->network, FALSE);
472         }
473
474         wifi->network = NULL;
475
476         wifi->disconnecting = FALSE;
477
478         if (wifi->pending_network != NULL) {
479                 network_connect(wifi->pending_network);
480                 wifi->pending_network = NULL;
481         }
482
483 }
484
485 static int network_disconnect(struct connman_network *network)
486 {
487         struct connman_device *device = connman_network_get_device(network);
488         struct wifi_data *wifi;
489         int err;
490
491         DBG("network %p", network);
492
493         wifi = connman_device_get_data(device);
494         if (wifi == NULL || wifi->interface == NULL)
495                 return -ENODEV;
496
497         connman_network_set_associating(network, FALSE);
498
499         if (wifi->disconnecting == TRUE)
500                 return -EALREADY;
501
502         wifi->disconnecting = TRUE;
503
504         err = g_supplicant_interface_disconnect(wifi->interface,
505                                                 disconnect_callback, wifi);
506         if (err < 0)
507                 wifi->disconnecting = FALSE;
508
509         return err;
510 }
511
512 static struct connman_network_driver network_driver = {
513         .name           = "wifi",
514         .type           = CONNMAN_NETWORK_TYPE_WIFI,
515         .priority       = CONNMAN_NETWORK_PRIORITY_LOW,
516         .probe          = network_probe,
517         .remove         = network_remove,
518         .connect        = network_connect,
519         .disconnect     = network_disconnect,
520 };
521
522 static void interface_added(GSupplicantInterface *interface)
523 {
524         const char *ifname = g_supplicant_interface_get_ifname(interface);
525         const char *driver = g_supplicant_interface_get_driver(interface);
526         struct wifi_data *wifi;
527
528         wifi = g_supplicant_interface_get_data(interface);
529
530         /*
531          * We can get here with a NULL wifi pointer when
532          * the interface added signal is sent before the
533          * interface creation callback is called.
534          */
535         if (wifi == NULL)
536                 return;
537
538         DBG("ifname %s driver %s wifi %p tethering %d",
539                         ifname, driver, wifi, wifi->tethering);
540
541         if (wifi->device == NULL) {
542                 connman_error("WiFi device not set");
543                 return;
544         }
545
546         connman_device_set_powered(wifi->device, TRUE);
547
548         if (wifi->tethering == TRUE)
549                 return;
550
551         wifi_scan(wifi->device);
552 }
553
554 static connman_bool_t is_idle(struct wifi_data *wifi)
555 {
556         DBG("state %d", wifi->state);
557
558         switch (wifi->state) {
559         case G_SUPPLICANT_STATE_UNKNOWN:
560         case G_SUPPLICANT_STATE_DISCONNECTED:
561         case G_SUPPLICANT_STATE_INACTIVE:
562         case G_SUPPLICANT_STATE_SCANNING:
563                 return TRUE;
564
565         case G_SUPPLICANT_STATE_AUTHENTICATING:
566         case G_SUPPLICANT_STATE_ASSOCIATING:
567         case G_SUPPLICANT_STATE_ASSOCIATED:
568         case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
569         case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
570         case G_SUPPLICANT_STATE_COMPLETED:
571                 return FALSE;
572         }
573
574         return FALSE;
575 }
576
577 static connman_bool_t is_idle_wps(GSupplicantInterface *interface,
578                                                 struct wifi_data *wifi)
579 {
580         /* First, let's check if WPS processing did not went wrong */
581         if (g_supplicant_interface_get_wps_state(interface) ==
582                 G_SUPPLICANT_WPS_STATE_FAIL)
583                 return FALSE;
584
585         /* Unlike normal connection, being associated while processing wps
586          * actually means that we are idling. */
587         switch (wifi->state) {
588         case G_SUPPLICANT_STATE_UNKNOWN:
589         case G_SUPPLICANT_STATE_DISCONNECTED:
590         case G_SUPPLICANT_STATE_INACTIVE:
591         case G_SUPPLICANT_STATE_SCANNING:
592         case G_SUPPLICANT_STATE_ASSOCIATED:
593                 return TRUE;
594         case G_SUPPLICANT_STATE_AUTHENTICATING:
595         case G_SUPPLICANT_STATE_ASSOCIATING:
596         case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
597         case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
598         case G_SUPPLICANT_STATE_COMPLETED:
599                 return FALSE;
600         }
601
602         return FALSE;
603 }
604
605 static connman_bool_t handle_wps_completion(GSupplicantInterface *interface,
606                                         struct connman_network *network,
607                                         struct connman_device *device,
608                                         struct wifi_data *wifi)
609 {
610         connman_bool_t wps;
611
612         wps = connman_network_get_bool(network, "WiFi.UseWPS");
613         if (wps == TRUE) {
614                 const unsigned char *ssid, *wps_ssid;
615                 unsigned int ssid_len, wps_ssid_len;
616                 const char *wps_key;
617
618                 /* Checking if we got associated with requested
619                  * network */
620                 ssid = connman_network_get_blob(network, "WiFi.SSID",
621                                                 &ssid_len);
622
623                 wps_ssid = g_supplicant_interface_get_wps_ssid(
624                         interface, &wps_ssid_len);
625
626                 if (wps_ssid == NULL || wps_ssid_len != ssid_len ||
627                                 memcmp(ssid, wps_ssid, ssid_len) != 0) {
628                         connman_network_set_associating(network, FALSE);
629                         g_supplicant_interface_disconnect(wifi->interface,
630                                                 disconnect_callback, wifi);
631                         return FALSE;
632                 }
633
634                 wps_key = g_supplicant_interface_get_wps_key(interface);
635                 connman_network_set_string(network, "WiFi.Passphrase",
636                                         wps_key);
637
638                 connman_network_set_string(network, "WiFi.PinWPS", NULL);
639         }
640
641         return TRUE;
642 }
643
644 static void interface_state(GSupplicantInterface *interface)
645 {
646         struct connman_network *network;
647         struct connman_device *device;
648         struct wifi_data *wifi;
649         GSupplicantState state = g_supplicant_interface_get_state(interface);
650         connman_bool_t wps;
651
652         wifi = g_supplicant_interface_get_data(interface);
653
654         DBG("wifi %p interface state %d", wifi, state);
655
656         if (wifi == NULL)
657                 return;
658
659         network = wifi->network;
660         device = wifi->device;
661
662         if (device == NULL || network == NULL)
663                 return;
664
665         switch (state) {
666         case G_SUPPLICANT_STATE_SCANNING:
667                 break;
668
669         case G_SUPPLICANT_STATE_AUTHENTICATING:
670         case G_SUPPLICANT_STATE_ASSOCIATING:
671                 connman_network_set_associating(network, TRUE);
672                 break;
673
674         case G_SUPPLICANT_STATE_COMPLETED:
675                 if (handle_wps_completion(interface, network, device, wifi) ==
676                                                                         FALSE)
677                         break;
678
679                 /* reset scan trigger and schedule background scan */
680                 connman_device_schedule_scan(device);
681
682                 connman_network_set_connected(network, TRUE);
683                 break;
684
685         case G_SUPPLICANT_STATE_DISCONNECTED:
686                 /*
687                  * If we're in one of the idle modes, we have
688                  * not started association yet and thus setting
689                  * those ones to FALSE could cancel an association
690                  * in progress.
691                  */
692                 wps = connman_network_get_bool(network, "WiFi.UseWPS");
693                 if (wps == TRUE)
694                         if (is_idle_wps(interface, wifi) == TRUE)
695                                 break;
696
697                 if (is_idle(wifi))
698                         break;
699                 connman_network_set_associating(network, FALSE);
700                 connman_network_set_connected(network, FALSE);
701                 break;
702
703         case G_SUPPLICANT_STATE_INACTIVE:
704                 connman_network_set_associating(network, FALSE);
705                 break;
706
707         case G_SUPPLICANT_STATE_UNKNOWN:
708         case G_SUPPLICANT_STATE_ASSOCIATED:
709         case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
710         case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
711                 break;
712         }
713
714         wifi->state = state;
715
716         DBG("DONE");
717 }
718
719 static void interface_removed(GSupplicantInterface *interface)
720 {
721         const char *ifname = g_supplicant_interface_get_ifname(interface);
722         struct wifi_data *wifi;
723
724         DBG("ifname %s", ifname);
725
726         wifi = g_supplicant_interface_get_data(interface);
727
728         if (wifi != NULL && wifi->tethering == TRUE)
729                 return;
730
731         if (wifi == NULL || wifi->device == NULL) {
732                 connman_error("Wrong wifi pointer");
733                 return;
734         }
735
736         connman_device_set_powered(wifi->device, FALSE);
737 }
738
739 static void scan_started(GSupplicantInterface *interface)
740 {
741         struct wifi_data *wifi;
742
743         DBG("");
744
745         wifi = g_supplicant_interface_get_data(interface);
746
747         if (wifi == NULL)
748                 return;
749 }
750
751 static void scan_finished(GSupplicantInterface *interface)
752 {
753         struct wifi_data *wifi;
754
755         DBG("");
756
757         wifi = g_supplicant_interface_get_data(interface);
758
759         if (wifi == NULL)
760                 return;
761 }
762
763 static unsigned char calculate_strength(GSupplicantNetwork *supplicant_network)
764 {
765         unsigned char strength;
766
767         strength = 120 + g_supplicant_network_get_signal(supplicant_network);
768         if (strength > 100)
769                 strength = 100;
770
771         return strength;
772 }
773
774 static void network_added(GSupplicantNetwork *supplicant_network)
775 {
776         struct connman_network *network;
777         GSupplicantInterface *interface;
778         struct wifi_data *wifi;
779         const char *name, *identifier, *security, *group;
780         const unsigned char *ssid;
781         unsigned int ssid_len;
782         connman_bool_t wps;
783
784         DBG("");
785
786         interface = g_supplicant_network_get_interface(supplicant_network);
787         wifi = g_supplicant_interface_get_data(interface);
788         name = g_supplicant_network_get_name(supplicant_network);
789         identifier = g_supplicant_network_get_identifier(supplicant_network);
790         security = g_supplicant_network_get_security(supplicant_network);
791         group = g_supplicant_network_get_identifier(supplicant_network);
792         wps = g_supplicant_network_get_wps(supplicant_network);
793
794         if (wifi == NULL)
795                 return;
796
797         ssid = g_supplicant_network_get_ssid(supplicant_network, &ssid_len);
798
799         network = connman_device_get_network(wifi->device, identifier);
800
801         if (network == NULL) {
802                 network = connman_network_create(identifier,
803                                                 CONNMAN_NETWORK_TYPE_WIFI);
804                 if (network == NULL)
805                         return;
806
807                 connman_network_set_index(network, wifi->index);
808
809                 if (connman_device_add_network(wifi->device, network) < 0) {
810                         connman_network_unref(network);
811                         return;
812                 }
813
814                 wifi->networks = g_slist_append(wifi->networks, network);
815         }
816
817         if (name != NULL && name[0] != '\0')
818                 connman_network_set_name(network, name);
819
820         connman_network_set_blob(network, "WiFi.SSID",
821                                                 ssid, ssid_len);
822         connman_network_set_string(network, "WiFi.Security", security);
823         connman_network_set_strength(network,
824                                 calculate_strength(supplicant_network));
825         connman_network_set_bool(network, "WiFi.WPS", wps);
826
827         connman_network_set_available(network, TRUE);
828
829         if (ssid != NULL)
830                 connman_network_set_group(network, group);
831 }
832
833 static void network_removed(GSupplicantNetwork *network)
834 {
835         GSupplicantInterface *interface;
836         struct wifi_data *wifi;
837         const char *name, *identifier;
838         struct connman_network *connman_network;
839
840         interface = g_supplicant_network_get_interface(network);
841         wifi = g_supplicant_interface_get_data(interface);
842         identifier = g_supplicant_network_get_identifier(network);
843         name = g_supplicant_network_get_name(network);
844
845         DBG("name %s", name);
846
847         if (wifi == NULL)
848                 return;
849
850         connman_network = connman_device_get_network(wifi->device, identifier);
851         if (connman_network == NULL)
852                 return;
853
854         wifi->networks = g_slist_remove(wifi->networks, connman_network);
855
856         connman_device_remove_network(wifi->device, connman_network);
857         connman_network_unref(connman_network);
858 }
859
860 static void debug(const char *str)
861 {
862         if (getenv("CONNMAN_SUPPLICANT_DEBUG"))
863                 connman_debug("%s", str);
864 }
865
866 static const GSupplicantCallbacks callbacks = {
867         .system_ready           = system_ready,
868         .system_killed          = system_killed,
869         .interface_added        = interface_added,
870         .interface_state        = interface_state,
871         .interface_removed      = interface_removed,
872         .scan_started           = scan_started,
873         .scan_finished          = scan_finished,
874         .network_added          = network_added,
875         .network_removed        = network_removed,
876         .debug                  = debug,
877 };
878
879
880 static int tech_probe(struct connman_technology *technology)
881 {
882         wifi_technology = technology;
883
884         return 0;
885 }
886
887 static void tech_remove(struct connman_technology *technology)
888 {
889         wifi_technology = NULL;
890 }
891
892 struct wifi_tethering_info {
893         struct wifi_data *wifi;
894         struct connman_technology *technology;
895         char *ifname;
896         GSupplicantSSID *ssid;
897 };
898
899 static GSupplicantSSID *ssid_ap_init(const char *ssid, const char *passphrase)
900 {
901         GSupplicantSSID *ap;
902
903         ap = g_try_malloc0(sizeof(GSupplicantSSID));
904         if (ap == NULL)
905                 return NULL;
906
907         ap->mode = G_SUPPLICANT_MODE_MASTER;
908         ap->ssid = ssid;
909         ap->ssid_len = strlen(ssid);
910         ap->scan_ssid = 0;
911         ap->freq = 2412;
912
913         if (passphrase == NULL || strlen(passphrase) == 0) {
914                 ap->security = G_SUPPLICANT_SECURITY_NONE;
915                 ap->passphrase = NULL;
916         } else {
917                ap->security = G_SUPPLICANT_SECURITY_PSK;
918                ap->protocol = G_SUPPLICANT_PROTO_RSN;
919                ap->pairwise_cipher = G_SUPPLICANT_PAIRWISE_CCMP;
920                ap->group_cipher = G_SUPPLICANT_GROUP_CCMP;
921                ap->passphrase = passphrase;
922         }
923
924         return ap;
925 }
926
927 static void ap_start_callback(int result, GSupplicantInterface *interface,
928                                                         void *user_data)
929 {
930         struct wifi_tethering_info *info = user_data;
931
932         DBG("result %d index %d bridge %s",
933                 result, info->wifi->index, info->wifi->bridge);
934
935         if (result < 0) {
936                 connman_inet_remove_from_bridge(info->wifi->index,
937                                                         info->wifi->bridge);
938                 connman_technology_tethering_notify(info->technology, FALSE);
939         }
940
941         g_free(info->ifname);
942         g_free(info);
943 }
944
945 static void ap_create_callback(int result,
946                                 GSupplicantInterface *interface,
947                                         void *user_data)
948 {
949         struct wifi_tethering_info *info = user_data;
950
951         DBG("result %d ifname %s", result,
952                                 g_supplicant_interface_get_ifname(interface));
953
954         if (result < 0) {
955                 connman_inet_remove_from_bridge(info->wifi->index,
956                                                         info->wifi->bridge);
957                 connman_technology_tethering_notify(info->technology, FALSE);
958
959                 g_free(info->ifname);
960                 g_free(info);
961                 return;
962         }
963
964         info->wifi->interface = interface;
965         g_supplicant_interface_set_data(interface, info->wifi);
966
967         if (g_supplicant_interface_set_apscan(interface, 2) < 0)
968                 connman_error("Failed to set interface ap_scan property");
969
970         g_supplicant_interface_connect(interface, info->ssid,
971                                                 ap_start_callback, info);
972 }
973
974 static void sta_remove_callback(int result,
975                                 GSupplicantInterface *interface,
976                                         void *user_data)
977 {
978         struct wifi_tethering_info *info = user_data;
979         const char *driver = connman_option_get_string("wifi");
980
981         DBG("ifname %s result %d ", info->ifname, result);
982
983         if (result < 0) {
984                 info->wifi->tethering = TRUE;
985
986                 g_free(info->ifname);
987                 g_free(info);
988                 return;
989         }
990
991         info->wifi->interface = NULL;
992
993         connman_technology_tethering_notify(info->technology, TRUE);
994
995         g_supplicant_interface_create(info->ifname, driver, info->wifi->bridge,
996                                                 ap_create_callback,
997                                                         info);
998 }
999
1000 static int tech_set_tethering(struct connman_technology *technology,
1001                                 const char *identifier, const char *passphrase,
1002                                 const char *bridge, connman_bool_t enabled)
1003 {
1004         GList *list;
1005         GSupplicantInterface *interface;
1006         struct wifi_data *wifi;
1007         struct wifi_tethering_info *info;
1008         const char *ifname;
1009         unsigned int mode;
1010         int err;
1011
1012         DBG("");
1013
1014         if (enabled == FALSE) {
1015                 for (list = iface_list; list; list = list->next) {
1016                         wifi = list->data;
1017
1018                         if (wifi->tethering == TRUE) {
1019                                 wifi->tethering = FALSE;
1020
1021                                 connman_inet_remove_from_bridge(wifi->index,
1022                                                                         bridge);
1023                                 wifi->bridged = FALSE;
1024                         }
1025                 }
1026
1027                 connman_technology_tethering_notify(technology, FALSE);
1028
1029                 return 0;
1030         }
1031
1032         for (list = iface_list; list; list = list->next) {
1033                 wifi = list->data;
1034
1035                 interface = wifi->interface;
1036
1037                 if (interface == NULL)
1038                         continue;
1039
1040                 ifname = g_supplicant_interface_get_ifname(wifi->interface);
1041
1042                 mode = g_supplicant_interface_get_mode(interface);
1043                 if ((mode & G_SUPPLICANT_CAPABILITY_MODE_AP) == 0) {
1044                         DBG("%s does not support AP mode", ifname);
1045                         continue;
1046                 }
1047
1048                 info = g_try_malloc0(sizeof(struct wifi_tethering_info));
1049                 if (info == NULL)
1050                         return -ENOMEM;
1051
1052                 info->wifi = wifi;
1053                 info->technology = technology;
1054                 info->wifi->bridge = bridge;
1055                 info->ssid = ssid_ap_init(identifier, passphrase);
1056                 if (info->ssid == NULL) {
1057                         g_free(info);
1058                         continue;
1059                 }
1060                 info->ifname = g_strdup(ifname);
1061                 if (info->ifname == NULL) {
1062                         g_free(info);
1063                         continue;
1064                 }
1065
1066                 info->wifi->tethering = TRUE;
1067
1068                 err = g_supplicant_interface_remove(interface,
1069                                                 sta_remove_callback,
1070                                                         info);
1071                 if (err == 0)
1072                         return err;
1073         }
1074
1075         return -EOPNOTSUPP;
1076 }
1077
1078 static void regdom_callback(void *user_data)
1079 {
1080         char *alpha2 = user_data;
1081
1082         DBG("");
1083
1084         if (wifi_technology == NULL)
1085                 return;
1086
1087         connman_technology_regdom_notify(wifi_technology, alpha2);
1088 }
1089
1090 static int tech_set_regdom(struct connman_technology *technology, const char *alpha2)
1091 {
1092         return g_supplicant_set_country(alpha2, regdom_callback, alpha2);
1093 }
1094
1095 static struct connman_technology_driver tech_driver = {
1096         .name           = "wifi",
1097         .type           = CONNMAN_SERVICE_TYPE_WIFI,
1098         .probe          = tech_probe,
1099         .remove         = tech_remove,
1100         .set_tethering  = tech_set_tethering,
1101         .set_regdom     = tech_set_regdom,
1102 };
1103
1104 static int wifi_init(void)
1105 {
1106         int err;
1107
1108         err = connman_network_driver_register(&network_driver);
1109         if (err < 0)
1110                 return err;
1111
1112         err = g_supplicant_register(&callbacks);
1113         if (err < 0) {
1114                 connman_network_driver_unregister(&network_driver);
1115                 return err;
1116         }
1117
1118         err = connman_technology_driver_register(&tech_driver);
1119         if (err < 0) {
1120                 g_supplicant_unregister(&callbacks);
1121                 connman_network_driver_unregister(&network_driver);
1122                 return err;
1123         }
1124
1125         return 0;
1126 }
1127
1128 static void wifi_exit(void)
1129 {
1130         DBG();
1131
1132         connman_technology_driver_unregister(&tech_driver);
1133
1134         g_supplicant_unregister(&callbacks);
1135
1136         connman_network_driver_unregister(&network_driver);
1137 }
1138
1139 CONNMAN_PLUGIN_DEFINE(wifi, "WiFi interface plugin", VERSION,
1140                 CONNMAN_PLUGIN_PRIORITY_DEFAULT, wifi_init, wifi_exit)