wifi: network_remove: Clear network pointer in wifi
[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         struct wifi_data *wifi;
768
769         DBG("");
770
771         wifi = g_supplicant_interface_get_data(interface);
772
773         if (wifi == NULL)
774                 return;
775 }
776
777 static void scan_finished(GSupplicantInterface *interface)
778 {
779         struct wifi_data *wifi;
780
781         DBG("");
782
783         wifi = g_supplicant_interface_get_data(interface);
784
785         if (wifi == NULL)
786                 return;
787 }
788
789 static unsigned char calculate_strength(GSupplicantNetwork *supplicant_network)
790 {
791         unsigned char strength;
792
793         strength = 120 + g_supplicant_network_get_signal(supplicant_network);
794         if (strength > 100)
795                 strength = 100;
796
797         return strength;
798 }
799
800 static void network_added(GSupplicantNetwork *supplicant_network)
801 {
802         struct connman_network *network;
803         GSupplicantInterface *interface;
804         struct wifi_data *wifi;
805         const char *name, *identifier, *security, *group;
806         const unsigned char *ssid;
807         unsigned int ssid_len;
808         connman_bool_t wps;
809
810         DBG("");
811
812         interface = g_supplicant_network_get_interface(supplicant_network);
813         wifi = g_supplicant_interface_get_data(interface);
814         name = g_supplicant_network_get_name(supplicant_network);
815         identifier = g_supplicant_network_get_identifier(supplicant_network);
816         security = g_supplicant_network_get_security(supplicant_network);
817         group = g_supplicant_network_get_identifier(supplicant_network);
818         wps = g_supplicant_network_get_wps(supplicant_network);
819
820         if (wifi == NULL)
821                 return;
822
823         ssid = g_supplicant_network_get_ssid(supplicant_network, &ssid_len);
824
825         network = connman_device_get_network(wifi->device, identifier);
826
827         if (network == NULL) {
828                 network = connman_network_create(identifier,
829                                                 CONNMAN_NETWORK_TYPE_WIFI);
830                 if (network == NULL)
831                         return;
832
833                 connman_network_set_index(network, wifi->index);
834
835                 if (connman_device_add_network(wifi->device, network) < 0) {
836                         connman_network_unref(network);
837                         return;
838                 }
839
840                 wifi->networks = g_slist_append(wifi->networks, network);
841         }
842
843         if (name != NULL && name[0] != '\0')
844                 connman_network_set_name(network, name);
845
846         connman_network_set_blob(network, "WiFi.SSID",
847                                                 ssid, ssid_len);
848         connman_network_set_string(network, "WiFi.Security", security);
849         connman_network_set_strength(network,
850                                 calculate_strength(supplicant_network));
851         connman_network_set_bool(network, "WiFi.WPS", wps);
852
853         connman_network_set_available(network, TRUE);
854
855         if (ssid != NULL)
856                 connman_network_set_group(network, group);
857 }
858
859 static void network_removed(GSupplicantNetwork *network)
860 {
861         GSupplicantInterface *interface;
862         struct wifi_data *wifi;
863         const char *name, *identifier;
864         struct connman_network *connman_network;
865
866         interface = g_supplicant_network_get_interface(network);
867         wifi = g_supplicant_interface_get_data(interface);
868         identifier = g_supplicant_network_get_identifier(network);
869         name = g_supplicant_network_get_name(network);
870
871         DBG("name %s", name);
872
873         if (wifi == NULL)
874                 return;
875
876         connman_network = connman_device_get_network(wifi->device, identifier);
877         if (connman_network == NULL)
878                 return;
879
880         wifi->networks = g_slist_remove(wifi->networks, connman_network);
881
882         connman_device_remove_network(wifi->device, connman_network);
883         connman_network_unref(connman_network);
884 }
885
886 static void debug(const char *str)
887 {
888         if (getenv("CONNMAN_SUPPLICANT_DEBUG"))
889                 connman_debug("%s", str);
890 }
891
892 static const GSupplicantCallbacks callbacks = {
893         .system_ready           = system_ready,
894         .system_killed          = system_killed,
895         .interface_added        = interface_added,
896         .interface_state        = interface_state,
897         .interface_removed      = interface_removed,
898         .scan_started           = scan_started,
899         .scan_finished          = scan_finished,
900         .network_added          = network_added,
901         .network_removed        = network_removed,
902         .debug                  = debug,
903 };
904
905
906 static int tech_probe(struct connman_technology *technology)
907 {
908         wifi_technology = technology;
909
910         return 0;
911 }
912
913 static void tech_remove(struct connman_technology *technology)
914 {
915         wifi_technology = NULL;
916 }
917
918 struct wifi_tethering_info {
919         struct wifi_data *wifi;
920         struct connman_technology *technology;
921         char *ifname;
922         GSupplicantSSID *ssid;
923 };
924
925 static GSupplicantSSID *ssid_ap_init(const char *ssid, const char *passphrase)
926 {
927         GSupplicantSSID *ap;
928
929         ap = g_try_malloc0(sizeof(GSupplicantSSID));
930         if (ap == NULL)
931                 return NULL;
932
933         ap->mode = G_SUPPLICANT_MODE_MASTER;
934         ap->ssid = ssid;
935         ap->ssid_len = strlen(ssid);
936         ap->scan_ssid = 0;
937         ap->freq = 2412;
938
939         if (passphrase == NULL || strlen(passphrase) == 0) {
940                 ap->security = G_SUPPLICANT_SECURITY_NONE;
941                 ap->passphrase = NULL;
942         } else {
943                ap->security = G_SUPPLICANT_SECURITY_PSK;
944                ap->protocol = G_SUPPLICANT_PROTO_RSN;
945                ap->pairwise_cipher = G_SUPPLICANT_PAIRWISE_CCMP;
946                ap->group_cipher = G_SUPPLICANT_GROUP_CCMP;
947                ap->passphrase = passphrase;
948         }
949
950         return ap;
951 }
952
953 static void ap_start_callback(int result, GSupplicantInterface *interface,
954                                                         void *user_data)
955 {
956         struct wifi_tethering_info *info = user_data;
957
958         DBG("result %d index %d bridge %s",
959                 result, info->wifi->index, info->wifi->bridge);
960
961         if (result < 0) {
962                 connman_inet_remove_from_bridge(info->wifi->index,
963                                                         info->wifi->bridge);
964                 connman_technology_tethering_notify(info->technology, FALSE);
965         }
966
967         g_free(info->ifname);
968         g_free(info);
969 }
970
971 static void ap_create_callback(int result,
972                                 GSupplicantInterface *interface,
973                                         void *user_data)
974 {
975         struct wifi_tethering_info *info = user_data;
976
977         DBG("result %d ifname %s", result,
978                                 g_supplicant_interface_get_ifname(interface));
979
980         if (result < 0) {
981                 connman_inet_remove_from_bridge(info->wifi->index,
982                                                         info->wifi->bridge);
983                 connman_technology_tethering_notify(info->technology, FALSE);
984
985                 g_free(info->ifname);
986                 g_free(info);
987                 return;
988         }
989
990         info->wifi->interface = interface;
991         g_supplicant_interface_set_data(interface, info->wifi);
992
993         if (g_supplicant_interface_set_apscan(interface, 2) < 0)
994                 connman_error("Failed to set interface ap_scan property");
995
996         g_supplicant_interface_connect(interface, info->ssid,
997                                                 ap_start_callback, info);
998 }
999
1000 static void sta_remove_callback(int result,
1001                                 GSupplicantInterface *interface,
1002                                         void *user_data)
1003 {
1004         struct wifi_tethering_info *info = user_data;
1005         const char *driver = connman_option_get_string("wifi");
1006
1007         DBG("ifname %s result %d ", info->ifname, result);
1008
1009         if (result < 0) {
1010                 info->wifi->tethering = TRUE;
1011
1012                 g_free(info->ifname);
1013                 g_free(info);
1014                 return;
1015         }
1016
1017         info->wifi->interface = NULL;
1018
1019         connman_technology_tethering_notify(info->technology, TRUE);
1020
1021         g_supplicant_interface_create(info->ifname, driver, info->wifi->bridge,
1022                                                 ap_create_callback,
1023                                                         info);
1024 }
1025
1026 static int tech_set_tethering(struct connman_technology *technology,
1027                                 const char *identifier, const char *passphrase,
1028                                 const char *bridge, connman_bool_t enabled)
1029 {
1030         GList *list;
1031         GSupplicantInterface *interface;
1032         struct wifi_data *wifi;
1033         struct wifi_tethering_info *info;
1034         const char *ifname;
1035         unsigned int mode;
1036         int err;
1037
1038         DBG("");
1039
1040         if (enabled == FALSE) {
1041                 for (list = iface_list; list; list = list->next) {
1042                         wifi = list->data;
1043
1044                         if (wifi->tethering == TRUE) {
1045                                 wifi->tethering = FALSE;
1046
1047                                 connman_inet_remove_from_bridge(wifi->index,
1048                                                                         bridge);
1049                                 wifi->bridged = FALSE;
1050                         }
1051                 }
1052
1053                 connman_technology_tethering_notify(technology, FALSE);
1054
1055                 return 0;
1056         }
1057
1058         for (list = iface_list; list; list = list->next) {
1059                 wifi = list->data;
1060
1061                 interface = wifi->interface;
1062
1063                 if (interface == NULL)
1064                         continue;
1065
1066                 ifname = g_supplicant_interface_get_ifname(wifi->interface);
1067
1068                 mode = g_supplicant_interface_get_mode(interface);
1069                 if ((mode & G_SUPPLICANT_CAPABILITY_MODE_AP) == 0) {
1070                         DBG("%s does not support AP mode", ifname);
1071                         continue;
1072                 }
1073
1074                 info = g_try_malloc0(sizeof(struct wifi_tethering_info));
1075                 if (info == NULL)
1076                         return -ENOMEM;
1077
1078                 info->wifi = wifi;
1079                 info->technology = technology;
1080                 info->wifi->bridge = bridge;
1081                 info->ssid = ssid_ap_init(identifier, passphrase);
1082                 if (info->ssid == NULL) {
1083                         g_free(info);
1084                         continue;
1085                 }
1086                 info->ifname = g_strdup(ifname);
1087                 if (info->ifname == NULL) {
1088                         g_free(info);
1089                         continue;
1090                 }
1091
1092                 info->wifi->tethering = TRUE;
1093
1094                 err = g_supplicant_interface_remove(interface,
1095                                                 sta_remove_callback,
1096                                                         info);
1097                 if (err == 0)
1098                         return err;
1099         }
1100
1101         return -EOPNOTSUPP;
1102 }
1103
1104 static void regdom_callback(void *user_data)
1105 {
1106         char *alpha2 = user_data;
1107
1108         DBG("");
1109
1110         if (wifi_technology == NULL)
1111                 return;
1112
1113         connman_technology_regdom_notify(wifi_technology, alpha2);
1114 }
1115
1116 static int tech_set_regdom(struct connman_technology *technology, const char *alpha2)
1117 {
1118         return g_supplicant_set_country(alpha2, regdom_callback, alpha2);
1119 }
1120
1121 static struct connman_technology_driver tech_driver = {
1122         .name           = "wifi",
1123         .type           = CONNMAN_SERVICE_TYPE_WIFI,
1124         .probe          = tech_probe,
1125         .remove         = tech_remove,
1126         .set_tethering  = tech_set_tethering,
1127         .set_regdom     = tech_set_regdom,
1128 };
1129
1130 static int wifi_init(void)
1131 {
1132         int err;
1133
1134         err = connman_network_driver_register(&network_driver);
1135         if (err < 0)
1136                 return err;
1137
1138         err = g_supplicant_register(&callbacks);
1139         if (err < 0) {
1140                 connman_network_driver_unregister(&network_driver);
1141                 return err;
1142         }
1143
1144         err = connman_technology_driver_register(&tech_driver);
1145         if (err < 0) {
1146                 g_supplicant_unregister(&callbacks);
1147                 connman_network_driver_unregister(&network_driver);
1148                 return err;
1149         }
1150
1151         return 0;
1152 }
1153
1154 static void wifi_exit(void)
1155 {
1156         DBG();
1157
1158         connman_technology_driver_unregister(&tech_driver);
1159
1160         g_supplicant_unregister(&callbacks);
1161
1162         connman_network_driver_unregister(&network_driver);
1163 }
1164
1165 CONNMAN_PLUGIN_DEFINE(wifi, "WiFi interface plugin", VERSION,
1166                 CONNMAN_PLUGIN_PRIORITY_DEFAULT, wifi_init, wifi_exit)