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