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