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