service: Remove Service.Mode
[platform/upstream/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, *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         security = g_supplicant_network_get_security(supplicant_network);
770         group = g_supplicant_network_get_identifier(supplicant_network);
771         wps = g_supplicant_network_get_wps(supplicant_network);
772
773         if (wifi == NULL)
774                 return;
775
776         ssid = g_supplicant_network_get_ssid(supplicant_network, &ssid_len);
777
778         network = connman_device_get_network(wifi->device, identifier);
779
780         if (network == NULL) {
781                 network = connman_network_create(identifier,
782                                                 CONNMAN_NETWORK_TYPE_WIFI);
783                 if (network == NULL)
784                         return;
785
786                 connman_network_set_index(network, wifi->index);
787
788                 if (connman_device_add_network(wifi->device, network) < 0) {
789                         connman_network_unref(network);
790                         return;
791                 }
792         }
793
794         if (name != NULL && name[0] != '\0')
795                 connman_network_set_name(network, name);
796
797         connman_network_set_blob(network, "WiFi.SSID",
798                                                 ssid, ssid_len);
799         connman_network_set_string(network, "WiFi.Security", security);
800         connman_network_set_strength(network,
801                                 calculate_strength(supplicant_network));
802         connman_network_set_bool(network, "WiFi.WPS", wps);
803
804         connman_network_set_available(network, TRUE);
805
806         if (ssid != NULL)
807                 connman_network_set_group(network, group);
808 }
809
810 static void network_removed(GSupplicantNetwork *network)
811 {
812         GSupplicantInterface *interface;
813         struct wifi_data *wifi;
814         const char *name, *identifier;
815
816         interface = g_supplicant_network_get_interface(network);
817         wifi = g_supplicant_interface_get_data(interface);
818         identifier = g_supplicant_network_get_identifier(network);
819         name = g_supplicant_network_get_name(network);
820
821         DBG("name %s", name);
822
823         if (wifi != NULL)
824                 connman_device_remove_network(wifi->device, identifier);
825 }
826
827 static void debug(const char *str)
828 {
829         if (getenv("CONNMAN_SUPPLICANT_DEBUG"))
830                 connman_debug("%s", str);
831 }
832
833 static const GSupplicantCallbacks callbacks = {
834         .system_ready           = system_ready,
835         .system_killed          = system_killed,
836         .interface_added        = interface_added,
837         .interface_state        = interface_state,
838         .interface_removed      = interface_removed,
839         .scan_started           = scan_started,
840         .scan_finished          = scan_finished,
841         .network_added          = network_added,
842         .network_removed        = network_removed,
843         .debug                  = debug,
844 };
845
846
847 static int tech_probe(struct connman_technology *technology)
848 {
849         wifi_technology = technology;
850
851         return 0;
852 }
853
854 static void tech_remove(struct connman_technology *technology)
855 {
856         wifi_technology = NULL;
857 }
858
859 struct wifi_tethering_info {
860         struct wifi_data *wifi;
861         struct connman_technology *technology;
862         char *ifname;
863         GSupplicantSSID *ssid;
864 };
865
866 static GSupplicantSSID *ssid_ap_init(const char *ssid, const char *passphrase)
867 {
868         GSupplicantSSID *ap;
869
870         ap = g_try_malloc0(sizeof(GSupplicantSSID));
871         if (ap == NULL)
872                 return NULL;
873
874         ap->mode = G_SUPPLICANT_MODE_MASTER;
875         ap->ssid = ssid;
876         ap->ssid_len = strlen(ssid);
877         ap->scan_ssid = 0;
878         ap->freq = 2412;
879
880         if (passphrase == NULL || strlen(passphrase) == 0) {
881                 ap->security = G_SUPPLICANT_SECURITY_NONE;
882                 ap->passphrase = NULL;
883         } else {
884                ap->security = G_SUPPLICANT_SECURITY_PSK;
885                ap->protocol = G_SUPPLICANT_PROTO_RSN;
886                ap->pairwise_cipher = G_SUPPLICANT_PAIRWISE_CCMP;
887                ap->group_cipher = G_SUPPLICANT_GROUP_CCMP;
888                ap->passphrase = passphrase;
889         }
890
891         return ap;
892 }
893
894 static void ap_start_callback(int result, GSupplicantInterface *interface,
895                                                         void *user_data)
896 {
897         struct wifi_tethering_info *info = user_data;
898
899         DBG("result %d index %d bridge %s",
900                 result, info->wifi->index, info->wifi->bridge);
901
902         if (result < 0) {
903                 connman_inet_remove_from_bridge(info->wifi->index,
904                                                         info->wifi->bridge);
905                 connman_technology_tethering_notify(info->technology, FALSE);
906         }
907
908         g_free(info->ifname);
909         g_free(info);
910 }
911
912 static void ap_create_callback(int result,
913                                 GSupplicantInterface *interface,
914                                         void *user_data)
915 {
916         struct wifi_tethering_info *info = user_data;
917
918         DBG("result %d ifname %s", result,
919                                 g_supplicant_interface_get_ifname(interface));
920
921         if (result < 0) {
922                 connman_inet_remove_from_bridge(info->wifi->index,
923                                                         info->wifi->bridge);
924                 connman_technology_tethering_notify(info->technology, FALSE);
925
926                 g_free(info->ifname);
927                 g_free(info);
928                 return;
929         }
930
931         info->wifi->interface = interface;
932         g_supplicant_interface_set_data(interface, info->wifi);
933
934         if (g_supplicant_interface_set_apscan(interface, 2) < 0)
935                 connman_error("Failed to set interface ap_scan property");
936
937         g_supplicant_interface_connect(interface, info->ssid,
938                                                 ap_start_callback, info);
939 }
940
941 static void sta_remove_callback(int result,
942                                 GSupplicantInterface *interface,
943                                         void *user_data)
944 {
945         struct wifi_tethering_info *info = user_data;
946         const char *driver = connman_option_get_string("wifi");
947
948         DBG("ifname %s result %d ", info->ifname, result);
949
950         if (result < 0) {
951                 info->wifi->tethering = TRUE;
952
953                 g_free(info->ifname);
954                 g_free(info);
955                 return;
956         }
957
958         info->wifi->interface = NULL;
959
960         connman_technology_tethering_notify(info->technology, TRUE);
961
962         g_supplicant_interface_create(info->ifname, driver, info->wifi->bridge,
963                                                 ap_create_callback,
964                                                         info);
965 }
966
967 static int tech_set_tethering(struct connman_technology *technology,
968                                 const char *identifier, const char *passphrase,
969                                 const char *bridge, connman_bool_t enabled)
970 {
971         GList *list;
972         GSupplicantInterface *interface;
973         struct wifi_data *wifi;
974         struct wifi_tethering_info *info;
975         const char *ifname;
976         unsigned int mode;
977         int err;
978
979         DBG("");
980
981         if (enabled == FALSE) {
982                 for (list = iface_list; list; list = list->next) {
983                         wifi = list->data;
984
985                         if (wifi->tethering == TRUE) {
986                                 wifi->tethering = FALSE;
987
988                                 connman_inet_remove_from_bridge(wifi->index,
989                                                                         bridge);
990                                 wifi->bridged = FALSE;
991                         }
992                 }
993
994                 connman_technology_tethering_notify(technology, FALSE);
995
996                 return 0;
997         }
998
999         for (list = iface_list; list; list = list->next) {
1000                 wifi = list->data;
1001
1002                 interface = wifi->interface;
1003
1004                 if (interface == NULL)
1005                         continue;
1006
1007                 ifname = g_supplicant_interface_get_ifname(wifi->interface);
1008
1009                 mode = g_supplicant_interface_get_mode(interface);
1010                 if ((mode & G_SUPPLICANT_CAPABILITY_MODE_AP) == 0) {
1011                         DBG("%s does not support AP mode", ifname);
1012                         continue;
1013                 }
1014
1015                 info = g_try_malloc0(sizeof(struct wifi_tethering_info));
1016                 if (info == NULL)
1017                         return -ENOMEM;
1018
1019                 info->wifi = wifi;
1020                 info->technology = technology;
1021                 info->wifi->bridge = bridge;
1022                 info->ssid = ssid_ap_init(identifier, passphrase);
1023                 if (info->ssid == NULL) {
1024                         g_free(info);
1025                         continue;
1026                 }
1027                 info->ifname = g_strdup(ifname);
1028                 if (info->ifname == NULL) {
1029                         g_free(info);
1030                         continue;
1031                 }
1032
1033                 info->wifi->tethering = TRUE;
1034
1035                 err = g_supplicant_interface_remove(interface,
1036                                                 sta_remove_callback,
1037                                                         info);
1038                 if (err == 0)
1039                         return err;
1040         }
1041
1042         return -EOPNOTSUPP;
1043 }
1044
1045 static void regdom_callback(void *user_data)
1046 {
1047         char *alpha2 = user_data;
1048
1049         DBG("");
1050
1051         if (wifi_technology == NULL)
1052                 return;
1053
1054         connman_technology_regdom_notify(wifi_technology, alpha2);
1055 }
1056
1057 static int tech_set_regdom(struct connman_technology *technology, const char *alpha2)
1058 {
1059         return g_supplicant_set_country(alpha2, regdom_callback, alpha2);
1060 }
1061
1062 static struct connman_technology_driver tech_driver = {
1063         .name           = "wifi",
1064         .type           = CONNMAN_SERVICE_TYPE_WIFI,
1065         .probe          = tech_probe,
1066         .remove         = tech_remove,
1067         .set_tethering  = tech_set_tethering,
1068         .set_regdom     = tech_set_regdom,
1069 };
1070
1071 static int wifi_init(void)
1072 {
1073         int err;
1074
1075         err = connman_network_driver_register(&network_driver);
1076         if (err < 0)
1077                 return err;
1078
1079         err = g_supplicant_register(&callbacks);
1080         if (err < 0) {
1081                 connman_network_driver_unregister(&network_driver);
1082                 return err;
1083         }
1084
1085         err = connman_technology_driver_register(&tech_driver);
1086         if (err < 0) {
1087                 g_supplicant_unregister(&callbacks);
1088                 connman_network_driver_unregister(&network_driver);
1089                 return err;
1090         }
1091
1092         return 0;
1093 }
1094
1095 static void wifi_exit(void)
1096 {
1097         DBG();
1098
1099         connman_technology_driver_unregister(&tech_driver);
1100
1101         g_supplicant_unregister(&callbacks);
1102
1103         connman_network_driver_unregister(&network_driver);
1104 }
1105
1106 CONNMAN_PLUGIN_DEFINE(wifi, "WiFi interface plugin", VERSION,
1107                 CONNMAN_PLUGIN_PRIORITY_DEFAULT, wifi_init, wifi_exit)