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