wifi: Dont start new connection until disassociation completes
[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         connman_bool_t connected;
66         connman_bool_t disconnecting;
67         int index;
68         unsigned flags;
69         unsigned int watch;
70 };
71
72 static int get_bssid(struct connman_device *device,
73                                 unsigned char *bssid, unsigned int *bssid_len)
74 {
75         struct iwreq wrq;
76         char *ifname;
77         int ifindex;
78         int fd, err;
79
80         ifindex = connman_device_get_index(device);
81         if (ifindex < 0)
82                 return -EINVAL;
83
84         ifname = connman_inet_ifname(ifindex);
85         if (ifname == NULL)
86                 return -EINVAL;
87
88         fd = socket(PF_INET, SOCK_DGRAM, 0);
89         if (fd < 0) {
90                 g_free(ifname);
91                 return -EINVAL;
92         }
93
94         memset(&wrq, 0, sizeof(wrq));
95         strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
96
97         err = ioctl(fd, SIOCGIWAP, &wrq);
98
99         g_free(ifname);
100         close(fd);
101
102         if (err < 0)
103                 return -EIO;
104
105         memcpy(bssid, wrq.u.ap_addr.sa_data, ETH_ALEN);
106         *bssid_len = ETH_ALEN;
107
108         return 0;
109 }
110
111 static void wifi_newlink(unsigned flags, unsigned change, void *user_data)
112 {
113         struct connman_device *device = user_data;
114         struct wifi_data *wifi = connman_device_get_data(device);
115
116         DBG("index %d flags %d change %d", wifi->index, flags, change);
117
118         if (!change)
119                 return;
120
121         if ((wifi->flags & IFF_UP) != (flags & IFF_UP)) {
122                 if (flags & IFF_UP)
123                         DBG("interface up");
124                 else
125                         DBG("interface down");
126         }
127
128         if ((wifi->flags & IFF_LOWER_UP) != (flags & IFF_LOWER_UP)) {
129                 if (flags & IFF_LOWER_UP)
130                         DBG("carrier on");
131                 else
132                         DBG("carrier off");
133         }
134
135         wifi->flags = flags;
136 }
137
138 static int wifi_probe(struct connman_device *device)
139 {
140         struct wifi_data *wifi;
141
142         DBG("device %p", device);
143
144         wifi = g_try_new0(struct wifi_data, 1);
145         if (wifi == NULL)
146                 return -ENOMEM;
147
148         wifi->connected = FALSE;
149         wifi->disconnecting = FALSE;
150
151         connman_device_set_data(device, wifi);
152         wifi->device = connman_device_ref(device);
153
154         wifi->index = connman_device_get_index(device);
155         wifi->flags = 0;
156
157         wifi->watch = connman_rtnl_add_newlink_watch(wifi->index,
158                                                         wifi_newlink, device);
159
160         return 0;
161 }
162
163 static void wifi_remove(struct connman_device *device)
164 {
165         struct wifi_data *wifi = connman_device_get_data(device);
166
167         DBG("device %p", device);
168
169         if (wifi->pending_network != NULL) {
170                 connman_network_unref(wifi->pending_network);
171                 wifi->pending_network = NULL;
172         }
173
174         connman_device_set_data(device, NULL);
175         connman_device_unref(wifi->device);
176         connman_rtnl_remove_watch(wifi->watch);
177
178         g_supplicant_interface_set_data(wifi->interface, NULL);
179
180         g_free(wifi->identifier);
181         g_free(wifi);
182 }
183
184 static void interface_create_callback(int result,
185                                         GSupplicantInterface *interface,
186                                                         void *user_data)
187 {
188         struct wifi_data *wifi = user_data;
189
190         DBG("result %d ifname %s", result,
191                                 g_supplicant_interface_get_ifname(interface));
192
193         if (result < 0)
194                 return;
195
196         wifi->interface = interface;
197         g_supplicant_interface_set_data(interface, wifi);
198 }
199
200 static void interface_remove_callback(int result,
201                                         GSupplicantInterface *interface,
202                                                         void *user_data)
203 {
204         struct wifi_data *wifi = user_data;
205
206         DBG("result %d", result);
207
208         if (result < 0)
209                 return;
210
211         wifi->interface = NULL;
212 }
213
214
215 static int wifi_enable(struct connman_device *device)
216 {
217         struct wifi_data *wifi = connman_device_get_data(device);
218         const char *interface = connman_device_get_string(device, "Interface");
219         const char *driver = connman_option_get_string("wifi");
220
221         DBG("device %p %p", device, wifi);
222
223         return g_supplicant_interface_create(interface, driver,
224                                                 interface_create_callback,
225                                                         wifi);
226 }
227
228 static int wifi_disable(struct connman_device *device)
229 {
230         struct wifi_data *wifi = connman_device_get_data(device);
231
232         DBG("device %p", device);
233
234         wifi->connected = FALSE;
235         wifi->disconnecting = FALSE;
236
237         if (wifi->pending_network != NULL) {
238                 connman_network_unref(wifi->pending_network);
239                 wifi->pending_network = NULL;
240         }
241
242         return g_supplicant_interface_remove(wifi->interface,
243                                                 interface_remove_callback,
244                                                         wifi);
245 }
246
247 static void scan_callback(int result, GSupplicantInterface *interface,
248                                                 void *user_data)
249 {
250         struct connman_device *device = user_data;
251
252         DBG("result %d", result);
253
254         connman_device_set_scanning(device, FALSE);
255 }
256
257 static int wifi_scan(struct connman_device *device)
258 {
259         struct wifi_data *wifi = connman_device_get_data(device);
260
261         DBG("device %p %p", device, wifi->interface);
262
263         return g_supplicant_interface_scan(wifi->interface, scan_callback,
264                                                                 device);
265 }
266
267 static struct connman_device_driver wifi_ng_driver = {
268         .name           = "wifi",
269         .type           = CONNMAN_DEVICE_TYPE_WIFI,
270         .priority       = CONNMAN_DEVICE_PRIORITY_LOW,
271         .probe          = wifi_probe,
272         .remove         = wifi_remove,
273         .enable         = wifi_enable,
274         .disable        = wifi_disable,
275         .scan           = wifi_scan,
276 };
277
278 static void system_ready(void)
279 {
280         DBG("");
281
282         if (connman_device_driver_register(&wifi_ng_driver) < 0)
283                 connman_error("Failed to register WiFi driver");
284 }
285
286 static void system_killed(void)
287 {
288         DBG("");
289
290         connman_device_driver_unregister(&wifi_ng_driver);
291 }
292
293 static void interface_added(GSupplicantInterface *interface)
294 {
295         const char *ifname = g_supplicant_interface_get_ifname(interface);
296         const char *driver = g_supplicant_interface_get_driver(interface);
297         struct wifi_data *wifi;
298
299         wifi = g_supplicant_interface_get_data(interface);
300
301         /*
302          * We can get here with a NULL wifi pointer when
303          * the interface added signal is sent before the
304          * interface creation callback is called.
305          */
306         if (wifi == NULL)
307                 return;
308
309         DBG("ifname %s driver %s wifi %p", ifname, driver, wifi);
310
311         if (wifi->device == NULL) {
312                 connman_error("WiFi device not set");
313                 return;
314         }
315
316         connman_device_set_powered(wifi->device, TRUE);
317         wifi_scan(wifi->device);
318 }
319
320 static void interface_state(GSupplicantInterface *interface)
321 {
322         struct connman_network *network;
323         struct connman_device *device;
324         struct wifi_data *wifi;
325         GSupplicantState state = g_supplicant_interface_get_state(interface);
326         unsigned char bssid[ETH_ALEN];
327         unsigned int bssid_len;
328
329         wifi = g_supplicant_interface_get_data(interface);
330
331         DBG("wifi %p interface state %d", wifi, state);
332
333         if (wifi == NULL)
334                 return;
335
336         network = wifi->network;
337         device = wifi->device;
338
339         if (device == NULL || network == NULL)
340                 return;
341
342         switch (state) {
343         case G_SUPPLICANT_STATE_SCANNING:
344                 connman_device_set_scanning(device, TRUE);
345                 break;
346
347         case G_SUPPLICANT_STATE_AUTHENTICATING:
348         case G_SUPPLICANT_STATE_ASSOCIATING:
349                 connman_network_set_associating(network, TRUE);
350                 break;
351
352         case G_SUPPLICANT_STATE_COMPLETED:
353                 /* reset scan trigger and schedule background scan */
354                 connman_device_schedule_scan(device);
355
356                 if (get_bssid(device, bssid, &bssid_len) == 0)
357                         connman_network_set_address(network,
358                                                         bssid, bssid_len);
359                 connman_network_set_connected(network, TRUE);
360                 break;
361
362         case G_SUPPLICANT_STATE_DISCONNECTED:
363                 connman_network_set_associating(network, FALSE);
364                 connman_network_set_connected(network, FALSE);
365                 break;
366
367         case G_SUPPLICANT_STATE_INACTIVE:
368                 connman_network_set_associating(network, FALSE);
369                 break;
370
371         case G_SUPPLICANT_STATE_UNKNOWN:
372         case G_SUPPLICANT_STATE_ASSOCIATED:
373         case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
374         case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
375                 break;
376         }
377
378         DBG("DONE");
379 }
380
381 static void interface_removed(GSupplicantInterface *interface)
382 {
383         const char *ifname = g_supplicant_interface_get_ifname(interface);
384         struct wifi_data *wifi;
385
386         DBG("ifname %s", ifname);
387
388         wifi = g_supplicant_interface_get_data(interface);
389
390         if (wifi == NULL || wifi->device == NULL) {
391                 connman_error("Wrong wifi pointer");
392                 return;
393         }
394
395         connman_device_set_powered(wifi->device, FALSE);
396 }
397
398 static void scan_started(GSupplicantInterface *interface)
399 {
400         struct wifi_data *wifi;
401
402         DBG("");
403
404         wifi = g_supplicant_interface_get_data(interface);
405
406         if (wifi == NULL)
407                 return;
408
409         if (wifi->device)
410                 connman_device_set_scanning(wifi->device, TRUE);
411 }
412
413 static void scan_finished(GSupplicantInterface *interface)
414 {
415         struct wifi_data *wifi;
416
417         DBG("");
418
419         wifi = g_supplicant_interface_get_data(interface);
420
421         if (wifi == NULL)
422                 return;
423 }
424
425 static unsigned char calculate_strength(GSupplicantNetwork *supplicant_network)
426 {
427         unsigned char strength;
428
429         strength = 120 + g_supplicant_network_get_signal(supplicant_network);
430         if (strength > 100)
431                 strength = 100;
432
433         return strength;
434 }
435
436 static void network_added(GSupplicantNetwork *supplicant_network)
437 {
438         struct connman_network *network;
439         GSupplicantInterface *interface;
440         struct wifi_data *wifi;
441         const char *name, *identifier, *mode, *security, *group;
442         const unsigned char *ssid;
443         unsigned int ssid_len;
444
445         DBG("");
446
447         interface = g_supplicant_network_get_interface(supplicant_network);
448         wifi = g_supplicant_interface_get_data(interface);
449         name = g_supplicant_network_get_name(supplicant_network);
450         identifier = g_supplicant_network_get_identifier(supplicant_network);
451         mode = g_supplicant_network_get_mode(supplicant_network);
452         security = g_supplicant_network_get_security(supplicant_network);
453         group = g_supplicant_network_get_identifier(supplicant_network);
454
455         if (wifi == NULL)
456                 return;
457
458         ssid = g_supplicant_network_get_ssid(supplicant_network, &ssid_len);
459
460         network = connman_device_get_network(wifi->device, identifier);
461
462         if (network == NULL) {
463                 network = connman_network_create(identifier,
464                                                 CONNMAN_NETWORK_TYPE_WIFI);
465                 if (network == NULL)
466                         return;
467
468                 connman_network_set_index(network, wifi->index);
469
470                 if (connman_device_add_network(wifi->device, network) < 0) {
471                         connman_network_unref(network);
472                         return;
473                 }
474         }
475
476         if (name != NULL && name[0] != '\0')
477                 connman_network_set_name(network, name);
478
479         connman_network_set_blob(network, "WiFi.SSID",
480                                                 ssid, ssid_len);
481         connman_network_set_string(network, "WiFi.Mode", mode);
482         connman_network_set_string(network, "WiFi.Security", security);
483         connman_network_set_strength(network,
484                                 calculate_strength(supplicant_network));
485
486         connman_network_set_available(network, TRUE);
487
488         if (ssid != NULL)
489                 connman_network_set_group(network, group);
490 }
491
492 static void network_removed(GSupplicantNetwork *network)
493 {
494         const char *name = g_supplicant_network_get_name(network);
495
496         DBG("* name %s", name);
497 }
498
499 static void debug(const char *str)
500 {
501         if (getenv("CONNMAN_SUPPLICANT_DEBUG"))
502                 connman_debug("%s", str);
503 }
504
505 static const GSupplicantCallbacks callbacks = {
506         .system_ready           = system_ready,
507         .system_killed          = system_killed,
508         .interface_added        = interface_added,
509         .interface_state        = interface_state,
510         .interface_removed      = interface_removed,
511         .scan_started           = scan_started,
512         .scan_finished          = scan_finished,
513         .network_added          = network_added,
514         .network_removed        = network_removed,
515         .debug                  = debug,
516 };
517
518
519 static int network_probe(struct connman_network *network)
520 {
521         DBG("network %p", network);
522
523         return 0;
524 }
525
526 static void network_remove(struct connman_network *network)
527 {
528         DBG("network %p", network);
529 }
530
531 static void connect_callback(int result, GSupplicantInterface *interface,
532                                                         void *user_data)
533 {
534         connman_error("%s", __func__);
535 }
536
537 static GSupplicantSecurity network_security(const char *security)
538 {
539         if (g_str_equal(security, "none") == TRUE)
540                 return G_SUPPLICANT_SECURITY_NONE;
541         else if (g_str_equal(security, "wep") == TRUE)
542                 return G_SUPPLICANT_SECURITY_WEP;
543         else if (g_str_equal(security, "psk") == TRUE)
544                 return G_SUPPLICANT_SECURITY_PSK;
545         else if (g_str_equal(security, "wpa") == TRUE)
546                 return G_SUPPLICANT_SECURITY_PSK;
547         else if (g_str_equal(security, "rsn") == TRUE)
548                 return G_SUPPLICANT_SECURITY_PSK;
549         else if (g_str_equal(security, "ieee8021x") == TRUE)
550                 return G_SUPPLICANT_SECURITY_IEEE8021X;
551
552         return G_SUPPLICANT_SECURITY_UNKNOWN;
553 }
554
555 static void ssid_init(GSupplicantSSID *ssid, struct connman_network *network)
556 {
557         const char *security;
558
559         memset(ssid, 0, sizeof(*ssid));
560         ssid->ssid = connman_network_get_blob(network, "WiFi.SSID",
561                                                 &ssid->ssid_len);
562         security = connman_network_get_string(network, "WiFi.Security");
563         ssid->security = network_security(security);
564         ssid->passphrase = connman_network_get_string(network,
565                                                         "WiFi.Passphrase");
566         ssid->eap = connman_network_get_string(network, "WiFi.EAP");
567
568         /*
569          * If our private key password is unset,
570          * we use the supplied passphrase. That is needed
571          * for PEAP where 2 passphrases (identity and client
572          * cert may have to be provided.
573          */
574         if (connman_network_get_string(network,
575                                         "WiFi.PrivateKeyPassphrase") == NULL)
576                 connman_network_set_string(network,
577                                                 "WiFi.PrivateKeyPassphrase",
578                                                 ssid->passphrase);
579         /* We must have an identity for both PEAP and TLS */
580         ssid->identity = connman_network_get_string(network, "WiFi.Identity");
581         ssid->ca_cert_path = connman_network_get_string(network,
582                                                         "WiFi.CACertFile");
583         ssid->client_cert_path = connman_network_get_string(network,
584                                                         "WiFi.ClientCertFile");
585         ssid->private_key_path = connman_network_get_string(network,
586                                                         "WiFi.PrivateKeyFile");
587         ssid->private_key_passphrase = connman_network_get_string(network,
588                                                 "WiFi.PrivateKeyPassphrase");
589         ssid->phase2_auth = connman_network_get_string(network, "WiFi.Phase2");
590
591 }
592
593 static int network_connect(struct connman_network *network)
594 {
595         struct connman_device *device = connman_network_get_device(network);
596         struct wifi_data *wifi;
597         GSupplicantInterface *interface;
598         GSupplicantSSID ssid;
599
600         DBG("network %p", network);
601
602         if (device == NULL)
603                 return -ENODEV;
604
605         wifi = connman_device_get_data(device);
606         if (wifi == NULL)
607                 return -ENODEV;
608
609         interface = wifi->interface;
610
611         ssid_init(&ssid, network);
612
613         if (wifi->disconnecting == TRUE)
614                 wifi->pending_network = connman_network_ref(network);
615         else {
616                 wifi->network = connman_network_ref(network);
617
618                 return g_supplicant_interface_connect(interface, &ssid,
619                                                 connect_callback, NULL);
620         }
621
622         return -EINPROGRESS;
623 }
624
625 static void disconnect_callback(int result, GSupplicantInterface *interface,
626                                                                 void *user_data)
627 {
628         struct wifi_data *wifi = user_data;
629
630         if (wifi->network != NULL) {
631                 /*
632                  * if result < 0 supplican return an error because
633                  * the network is not current.
634                  * we wont receive G_SUPPLICANT_STATE_DISCONNECTED since it
635                  * failed, call connman_network_set_connected to report
636                  * disconnect is completed.
637                  */
638                 if (result < 0)
639                         connman_network_set_connected(wifi->network, FALSE);
640
641                 connman_network_unref(wifi->network);
642         }
643
644         wifi->network = NULL;
645
646         wifi->disconnecting = FALSE;
647
648         if (wifi->pending_network != NULL) {
649                 network_connect(wifi->pending_network);
650                 connman_network_unref(wifi->pending_network);
651                 wifi->pending_network = NULL;
652         }
653
654 }
655
656 static int network_disconnect(struct connman_network *network)
657 {
658         struct connman_device *device = connman_network_get_device(network);
659         struct wifi_data *wifi;
660         int err;
661
662         DBG("network %p", network);
663
664         wifi = connman_device_get_data(device);
665         if (wifi == NULL || wifi->interface == NULL)
666                 return -ENODEV;
667
668         connman_network_set_associating(network, FALSE);
669
670         if (wifi->disconnecting == TRUE)
671                 return -EALREADY;
672
673         wifi->disconnecting = TRUE;
674
675         err = g_supplicant_interface_disconnect(wifi->interface,
676                                                 disconnect_callback, wifi);
677         if (err < 0)
678                 wifi->disconnecting = FALSE;
679
680         return err;
681 }
682
683 static struct connman_network_driver network_driver = {
684         .name           = "wifi",
685         .type           = CONNMAN_NETWORK_TYPE_WIFI,
686         .priority       = CONNMAN_NETWORK_PRIORITY_LOW,
687         .probe          = network_probe,
688         .remove         = network_remove,
689         .connect        = network_connect,
690         .disconnect     = network_disconnect,
691 };
692
693 static int tech_probe(struct connman_technology *technology)
694 {
695         wifi_technology = technology;
696
697         return 0;
698 }
699
700 static void tech_remove(struct connman_technology *technology)
701 {
702         wifi_technology = NULL;
703 }
704
705 static void regdom_callback(void *user_data)
706 {
707         char *alpha2 = user_data;
708
709         DBG("");
710
711         if (wifi_technology == NULL)
712                 return;
713
714         connman_technology_regdom_notify(wifi_technology, alpha2);
715 }
716
717 static int tech_set_regdom(struct connman_technology *technology, const char *alpha2)
718 {
719         return g_supplicant_set_country(alpha2, regdom_callback, alpha2);
720 }
721
722 static struct connman_technology_driver tech_driver = {
723         .name           = "wifi",
724         .type           = CONNMAN_SERVICE_TYPE_WIFI,
725         .probe          = tech_probe,
726         .remove         = tech_remove,
727         .set_regdom     = tech_set_regdom,
728 };
729
730 static int wifi_init(void)
731 {
732         int err;
733
734         err = connman_network_driver_register(&network_driver);
735         if (err < 0)
736                 return err;
737
738         err = g_supplicant_register(&callbacks);
739         if (err < 0) {
740                 connman_network_driver_unregister(&network_driver);
741                 return err;
742         }
743
744         err = connman_technology_driver_register(&tech_driver);
745         if (err < 0) {
746                 g_supplicant_unregister(&callbacks);
747                 connman_network_driver_unregister(&network_driver);
748                 return err;
749         }
750
751         return 0;
752 }
753
754 static void wifi_exit(void)
755 {
756         DBG();
757
758         connman_technology_driver_unregister(&tech_driver);
759
760         g_supplicant_unregister(&callbacks);
761
762         connman_network_driver_unregister(&network_driver);
763 }
764
765 CONNMAN_PLUGIN_DEFINE(wifi, "WiFi interface plugin", VERSION,
766                 CONNMAN_PLUGIN_PRIORITY_DEFAULT, wifi_init, wifi_exit)