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