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