Set device scanning state to false from the scan callback
[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 <errno.h>
28 #include <string.h>
29 #include <sys/ioctl.h>
30 #include <sys/socket.h>
31 #include <linux/if_arp.h>
32 #include <linux/wireless.h>
33 #include <net/ethernet.h>
34
35 #ifndef IFF_LOWER_UP
36 #define IFF_LOWER_UP    0x10000
37 #endif
38
39 #include <dbus/dbus.h>
40 #include <glib.h>
41
42 #define CONNMAN_API_SUBJECT_TO_CHANGE
43 #include <connman/plugin.h>
44 #include <connman/inet.h>
45 #include <connman/device.h>
46 #include <connman/rtnl.h>
47 #include <connman/log.h>
48 #include <connman/option.h>
49
50 #include <gsupplicant/gsupplicant.h>
51
52 #define CLEANUP_TIMEOUT   8     /* in seconds */
53 #define INACTIVE_TIMEOUT  12    /* in seconds */
54
55 struct wifi_data {
56         char *identifier;
57         struct connman_device *device;
58         struct connman_network *network;
59         GSupplicantInterface *interface;
60         connman_bool_t connected;
61         int index;
62         unsigned flags;
63         unsigned int watch;
64 };
65
66 static int get_bssid(struct connman_device *device,
67                                 unsigned char *bssid, unsigned int *bssid_len)
68 {
69         struct iwreq wrq;
70         char *ifname;
71         int ifindex;
72         int fd, err;
73
74         ifindex = connman_device_get_index(device);
75         if (ifindex < 0)
76                 return -EINVAL;
77
78         ifname = connman_inet_ifname(ifindex);
79         if (ifname == NULL)
80                 return -EINVAL;
81
82         fd = socket(PF_INET, SOCK_DGRAM, 0);
83         if (fd < 0) {
84                 g_free(ifname);
85                 return -EINVAL;
86         }
87
88         memset(&wrq, 0, sizeof(wrq));
89         strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
90
91         err = ioctl(fd, SIOCGIWAP, &wrq);
92
93         g_free(ifname);
94         close(fd);
95
96         if (err < 0)
97                 return -EIO;
98
99         memcpy(bssid, wrq.u.ap_addr.sa_data, ETH_ALEN);
100         *bssid_len = ETH_ALEN;
101
102         return 0;
103 }
104
105 static void wifi_newlink(unsigned flags, unsigned change, void *user_data)
106 {
107         struct connman_device *device = user_data;
108         struct wifi_data *wifi = connman_device_get_data(device);
109
110         DBG("index %d flags %d change %d", wifi->index, flags, change);
111
112         if (!change)
113                 return;
114
115         if ((wifi->flags & IFF_UP) != (flags & IFF_UP)) {
116                 if (flags & IFF_UP)
117                         DBG("interface up");
118                 else
119                         DBG("interface down");
120         }
121
122         if ((wifi->flags & IFF_LOWER_UP) != (flags & IFF_LOWER_UP)) {
123                 if (flags & IFF_LOWER_UP)
124                         DBG("carrier on");
125                 else
126                         DBG("carrier off");
127         }
128
129         wifi->flags = flags;
130 }
131
132 static int wifi_probe(struct connman_device *device)
133 {
134         struct wifi_data *wifi;
135
136         DBG("device %p", device);
137
138         wifi = g_try_new0(struct wifi_data, 1);
139         if (wifi == NULL)
140                 return -ENOMEM;
141
142         wifi->connected = FALSE;
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         return 0;
154 }
155
156 static void wifi_remove(struct connman_device *device)
157 {
158         struct wifi_data *wifi = connman_device_get_data(device);
159
160         DBG("device %p", device);
161
162         connman_device_set_data(device, NULL);
163         connman_device_unref(wifi->device);
164         connman_rtnl_remove_watch(wifi->watch);
165
166         g_free(wifi->identifier);
167         g_free(wifi);
168 }
169
170 static void interface_create_callback(int result,
171                                         GSupplicantInterface *interface,
172                                                         void *user_data)
173 {
174         struct wifi_data *wifi = user_data;
175
176         DBG("result %d ifname %s", result,
177                                 g_supplicant_interface_get_ifname(interface));
178
179         if (result < 0)
180                 return;
181
182         wifi->interface = interface;
183         g_supplicant_interface_set_data(interface, wifi);
184 }
185
186 static void interface_remove_callback(int result,
187                                         GSupplicantInterface *interface,
188                                                         void *user_data)
189 {
190         struct wifi_data *wifi = user_data;
191
192         DBG("result %d", result);
193
194         if (result < 0)
195                 return;
196
197         wifi->interface = NULL;
198 }
199
200
201 static int wifi_enable(struct connman_device *device)
202 {
203         struct wifi_data *wifi = connman_device_get_data(device);
204         const char *interface = connman_device_get_string(device, "Interface");
205         const char *driver = connman_option_get_string("wifi");
206
207         DBG("device %p %p", device, wifi);
208
209         return g_supplicant_interface_create(interface, driver,
210                                                 interface_create_callback,
211                                                         wifi);
212 }
213
214 static int wifi_disable(struct connman_device *device)
215 {
216         struct wifi_data *wifi = connman_device_get_data(device);
217
218         DBG("device %p", device);
219
220         wifi->connected = FALSE;
221
222         return g_supplicant_interface_remove(wifi->interface,
223                                                 interface_remove_callback,
224                                                         wifi);
225 }
226
227 static void scan_callback(int result, GSupplicantInterface *interface,
228                                                 void *user_data)
229 {
230         struct connman_device *device = user_data;
231
232         DBG("result %d", result);
233
234         connman_device_set_scanning(device, FALSE);
235 }
236
237 static int wifi_scan(struct connman_device *device)
238 {
239         struct wifi_data *wifi = connman_device_get_data(device);
240
241         DBG("device %p %p", device, wifi->interface);
242
243         return g_supplicant_interface_scan(wifi->interface, scan_callback,
244                                                                 device);
245 }
246
247 static struct connman_device_driver wifi_ng_driver = {
248         .name           = "wifi",
249         .type           = CONNMAN_DEVICE_TYPE_WIFI,
250         .priority       = CONNMAN_DEVICE_PRIORITY_LOW,
251         .probe          = wifi_probe,
252         .remove         = wifi_remove,
253         .enable         = wifi_enable,
254         .disable        = wifi_disable,
255         .scan           = wifi_scan,
256 };
257
258 static void system_ready(void)
259 {
260         DBG("");
261
262         if (connman_device_driver_register(&wifi_ng_driver) < 0)
263                 connman_error("Failed to register WiFi driver");
264 }
265
266 static void system_killed(void)
267 {
268         DBG("");
269
270         connman_device_driver_unregister(&wifi_ng_driver);
271 }
272
273 static void interface_added(GSupplicantInterface *interface)
274 {
275         const char *ifname = g_supplicant_interface_get_ifname(interface);
276         const char *driver = g_supplicant_interface_get_driver(interface);
277         struct wifi_data *wifi;
278
279         wifi = g_supplicant_interface_get_data(interface);
280
281         DBG("ifname %s driver %s wifi %p", ifname, driver, wifi);
282
283         if (wifi == NULL || wifi->device == NULL) {
284                 connman_error("Wrong wifi pointer");
285                 return;
286         }
287
288         connman_device_set_powered(wifi->device, TRUE);
289         wifi_scan(wifi->device);
290 }
291
292 static void interface_state(GSupplicantInterface *interface)
293 {
294         struct connman_network *network;
295         struct connman_device *device;
296         struct wifi_data *wifi;
297         GSupplicantState state = g_supplicant_interface_get_state(interface);
298         unsigned char bssid[ETH_ALEN];
299         unsigned int bssid_len;
300
301         wifi = g_supplicant_interface_get_data(interface);
302
303         DBG("wifi %p interface state %d", wifi, state);
304
305         if (wifi == NULL)
306                 return;
307
308         network = wifi->network;
309         device = wifi->device;
310
311         if (device == NULL || network == NULL)
312                 return;
313
314         switch (state) {
315         case G_SUPPLICANT_STATE_SCANNING:
316                 connman_device_set_scanning(device, TRUE);
317                 break;
318
319         case G_SUPPLICANT_STATE_AUTHENTICATING:
320         case G_SUPPLICANT_STATE_ASSOCIATING:
321                 connman_network_set_associating(network, TRUE);
322                 break;
323
324         case G_SUPPLICANT_STATE_COMPLETED:
325                 /* reset scan trigger and schedule background scan */
326                 connman_device_schedule_scan(device);
327
328                 if (get_bssid(device, bssid, &bssid_len) == 0)
329                         connman_network_set_address(network,
330                                                         bssid, bssid_len);
331                 connman_network_set_connected(network, TRUE);
332                 break;
333
334         case G_SUPPLICANT_STATE_DISCONNECTED:
335                 connman_network_set_associating(network, FALSE);
336                 connman_network_set_connected(network, FALSE);
337                 break;
338
339         case G_SUPPLICANT_STATE_INACTIVE:
340                 connman_network_set_associating(network, FALSE);
341                 break;
342
343         case G_SUPPLICANT_STATE_UNKNOWN:
344         case G_SUPPLICANT_STATE_ASSOCIATED:
345         case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
346         case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
347                 break;
348         }
349
350         DBG("DONE");
351 }
352
353 static void interface_removed(GSupplicantInterface *interface)
354 {
355         const char *ifname = g_supplicant_interface_get_ifname(interface);
356         struct wifi_data *wifi;
357
358         DBG("ifname %s", ifname);
359
360         wifi = g_supplicant_interface_get_data(interface);
361
362         if (wifi == NULL || wifi->device == NULL) {
363                 connman_error("Wrong wifi pointer");
364                 return;
365         }
366
367         connman_device_set_powered(wifi->device, FALSE);
368 }
369
370 static void scan_started(GSupplicantInterface *interface)
371 {
372         struct wifi_data *wifi;
373
374         DBG("");
375
376         wifi = g_supplicant_interface_get_data(interface);
377
378         if (wifi == NULL)
379                 return;
380
381         if (wifi->device)
382                 connman_device_set_scanning(wifi->device, TRUE);
383 }
384
385 static void scan_finished(GSupplicantInterface *interface)
386 {
387         struct wifi_data *wifi;
388
389         DBG("");
390
391         wifi = g_supplicant_interface_get_data(interface);
392
393         if (wifi == NULL)
394                 return;
395 }
396
397 static unsigned char calculate_strength(GSupplicantNetwork *supplicant_network)
398 {
399         unsigned char strength;
400
401         strength = 120 + g_supplicant_network_get_signal(supplicant_network);
402         if (strength > 100)
403                 strength = 100;
404
405         return strength;
406 }
407
408 static void network_added(GSupplicantNetwork *supplicant_network)
409 {
410         struct connman_network *network;
411         GSupplicantInterface *interface;
412         struct wifi_data *wifi;
413         const char *name, *path, *identifier, *mode, *security, *group;
414         const unsigned char *ssid;
415         unsigned int ssid_len;
416
417         DBG("");
418
419         interface = g_supplicant_network_get_interface(supplicant_network);
420         wifi = g_supplicant_interface_get_data(interface);
421         name = g_supplicant_network_get_name(supplicant_network);
422         path = g_supplicant_network_get_path(supplicant_network);
423         identifier = g_supplicant_network_get_identifier(supplicant_network);
424         mode = g_supplicant_network_get_mode(supplicant_network);
425         security = g_supplicant_network_get_security(supplicant_network);
426         group = g_supplicant_network_get_identifier(supplicant_network);
427
428         if (wifi == NULL)
429                 return;
430
431         ssid = g_supplicant_network_get_ssid(supplicant_network, &ssid_len);
432
433         network = connman_device_get_network(wifi->device, path);
434
435         if (network == NULL) {
436                 network = connman_network_create(identifier,
437                                                 CONNMAN_NETWORK_TYPE_WIFI);
438                 if (network == NULL)
439                         return;
440
441                 connman_network_set_index(network, wifi->index);
442
443                 connman_network_set_protocol(network,
444                                                 CONNMAN_NETWORK_PROTOCOL_IP);
445
446                 if (connman_device_add_network(wifi->device, network) < 0) {
447                         connman_network_unref(network);
448                         return;
449                 }
450         }
451
452         if (name != NULL && name[0] != '\0')
453                 connman_network_set_name(network, name);
454
455         connman_network_set_blob(network, "WiFi.SSID",
456                                                 ssid, ssid_len);
457         connman_network_set_string(network, "WiFi.Mode", mode);
458         connman_network_set_string(network, "WiFi.Security", security);
459         connman_network_set_strength(network,
460                                 calculate_strength(supplicant_network));
461
462         connman_network_set_available(network, TRUE);
463
464         if (ssid != NULL)
465                 connman_network_set_group(network, group);
466 }
467
468 static void network_removed(GSupplicantNetwork *network)
469 {
470         const char *name = g_supplicant_network_get_name(network);
471
472         DBG("* name %s", name);
473 }
474
475 static void debug(const char *str)
476 {
477         connman_debug("%s", str);
478 }
479
480 static const GSupplicantCallbacks callbacks = {
481         .system_ready           = system_ready,
482         .system_killed          = system_killed,
483         .interface_added        = interface_added,
484         .interface_state        = interface_state,
485         .interface_removed      = interface_removed,
486         .scan_started           = scan_started,
487         .scan_finished          = scan_finished,
488         .network_added          = network_added,
489         .network_removed        = network_removed,
490         .debug                  = debug,
491 };
492
493
494 static int network_probe(struct connman_network *network)
495 {
496         DBG("network %p", network);
497
498         return 0;
499 }
500
501 static void network_remove(struct connman_network *network)
502 {
503         DBG("network %p", network);
504 }
505
506 static void connect_callback(int result, GSupplicantInterface *interface,
507                                                         void *user_data)
508 {
509         connman_error("%s", __func__);
510 }
511
512 static void disconnect_callback(int result, GSupplicantInterface *interface,
513                                                         void *user_data)
514 {
515         struct wifi_data *wifi = user_data;
516
517         if (result < 0) {
518                 connman_error("%s", __func__);
519                 return;
520         }
521
522         connman_network_unref(wifi->network);
523
524         wifi->network = NULL;
525 }
526
527
528 static GSupplicantSecurity network_security(const char *security)
529 {
530         if (g_str_equal(security, "none") == TRUE)
531                 return G_SUPPLICANT_SECURITY_NONE;
532         else if (g_str_equal(security, "wep") == TRUE)
533                 return G_SUPPLICANT_SECURITY_WEP;
534         else if (g_str_equal(security, "psk") == TRUE)
535                 return G_SUPPLICANT_SECURITY_PSK;
536         else if (g_str_equal(security, "wpa") == TRUE)
537                 return G_SUPPLICANT_SECURITY_PSK;
538         else if (g_str_equal(security, "rsn") == TRUE)
539                 return G_SUPPLICANT_SECURITY_PSK;
540         else if (g_str_equal(security, "ieee8021x") == TRUE)
541                 return G_SUPPLICANT_SECURITY_IEEE8021X;
542
543         return G_SUPPLICANT_SECURITY_UNKNOWN;
544 }
545
546 static void ssid_init(GSupplicantSSID *ssid, struct connman_network *network)
547 {
548         const char *security;
549
550         memset(ssid, 0, sizeof(*ssid));
551         ssid->ssid = connman_network_get_blob(network, "WiFi.SSID",
552                                                 &ssid->ssid_len);
553         security = connman_network_get_string(network, "WiFi.Security");
554         ssid->security = network_security(security);
555         ssid->passphrase = connman_network_get_string(network,
556                                                         "WiFi.Passphrase");
557         ssid->eap = connman_network_get_string(network, "WiFi.EAP");
558
559         /*
560          * If our private key password is unset,
561          * we use the supplied passphrase. That is needed
562          * for PEAP where 2 passphrases (identity and client
563          * cert may have to be provided.
564          */
565         if (connman_network_get_string(network,
566                                         "WiFi.PrivateKeyPassphrase") == NULL)
567                 connman_network_set_string(network,
568                                                 "WiFi.PrivateKeyPassphrase",
569                                                 ssid->passphrase);
570         /* We must have an identity for both PEAP and TLS */
571         ssid->identity = connman_network_get_string(network, "WiFi.Identity");
572         ssid->ca_cert_path = connman_network_get_string(network,
573                                                         "WiFi.CACertFile");
574         ssid->client_cert_path = connman_network_get_string(network,
575                                                         "WiFi.ClientCertFile");
576         ssid->private_key_path = connman_network_get_string(network,
577                                                         "WiFi.PrivateKeyFile");
578         ssid->private_key_passphrase = connman_network_get_string(network,
579                                                 "WiFi.PrivateKeyPassphrase");
580         ssid->phase2_auth = connman_network_get_string(network, "WiFi.Phase2");
581
582 }
583
584 static int network_connect(struct connman_network *network)
585 {
586         struct connman_device *device = connman_network_get_device(network);
587         struct wifi_data *wifi;
588         GSupplicantInterface *interface;
589         GSupplicantSSID ssid;
590
591         DBG("network %p", network);
592
593         if (device == NULL)
594                 return -ENODEV;
595
596         wifi = connman_device_get_data(device);
597         if (wifi == NULL)
598                 return -ENODEV;
599
600         interface = wifi->interface;
601
602         ssid_init(&ssid, network);
603
604         wifi->network = connman_network_ref(network);
605
606         return g_supplicant_interface_connect(interface, &ssid,
607                                                 connect_callback, NULL);
608 }
609
610 static int network_disconnect(struct connman_network *network)
611 {
612         struct connman_device *device = connman_network_get_device(network);
613         struct wifi_data *wifi;
614
615         DBG("network %p", network);
616
617         wifi = connman_device_get_data(device);
618         if (wifi == NULL || wifi->interface == NULL)
619                 return -ENODEV;
620
621         return g_supplicant_interface_disconnect(wifi->interface,
622                                                 disconnect_callback, wifi);
623 }
624
625 static struct connman_network_driver network_driver = {
626         .name           = "wifi",
627         .type           = CONNMAN_NETWORK_TYPE_WIFI,
628         .priority       = CONNMAN_NETWORK_PRIORITY_LOW,
629         .probe          = network_probe,
630         .remove         = network_remove,
631         .connect        = network_connect,
632         .disconnect     = network_disconnect,
633 };
634
635 static int wifi_init(void)
636 {
637         int err;
638
639         err = connman_network_driver_register(&network_driver);
640         if (err < 0)
641                 return err;
642
643         err = g_supplicant_register(&callbacks);
644         if (err < 0) {
645                 connman_network_driver_unregister(&network_driver);
646                 return err;
647         }
648
649         return 0;
650 }
651
652 static void wifi_exit(void)
653 {
654         DBG();
655
656         g_supplicant_unregister(&callbacks);
657
658         connman_network_driver_unregister(&network_driver);
659 }
660
661 CONNMAN_PLUGIN_DEFINE(wifi, "WiFi interface plugin", VERSION,
662                 CONNMAN_PLUGIN_PRIORITY_DEFAULT, wifi_init, wifi_exit)