wifi: use const pointer for a ssid buffer
[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         if (result < 0) {
235                 connman_device_set_scanning(device, FALSE);
236                 return;
237         }
238
239         connman_device_set_scanning(device, TRUE);
240 }
241
242 static int wifi_scan(struct connman_device *device)
243 {
244         struct wifi_data *wifi = connman_device_get_data(device);
245
246         DBG("device %p %p", device, wifi->interface);
247
248         return g_supplicant_interface_scan(wifi->interface, scan_callback,
249                                                                 device);
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 void interface_added(GSupplicantInterface *interface)
279 {
280         const char *ifname = g_supplicant_interface_get_ifname(interface);
281         const char *driver = g_supplicant_interface_get_driver(interface);
282         struct wifi_data *wifi;
283
284         wifi = g_supplicant_interface_get_data(interface);
285
286         DBG("ifname %s driver %s wifi %p", ifname, driver, wifi);
287
288         if (wifi == NULL || wifi->device == NULL) {
289                 connman_error("Wrong wifi pointer");
290                 return;
291         }
292
293         connman_device_set_powered(wifi->device, TRUE);
294         wifi_scan(wifi->device);
295 }
296
297 static void interface_state(GSupplicantInterface *interface)
298 {
299         struct connman_network *network;
300         struct connman_device *device;
301         struct wifi_data *wifi;
302         GSupplicantState state = g_supplicant_interface_get_state(interface);
303         unsigned char bssid[ETH_ALEN];
304         unsigned int bssid_len;
305
306         wifi = g_supplicant_interface_get_data(interface);
307
308         DBG("wifi %p interface state %d", wifi, state);
309
310         if (wifi == NULL)
311                 return;
312
313         network = wifi->network;
314         device = wifi->device;
315
316         if (device == NULL || network == NULL)
317                 return;
318
319         switch (state) {
320         case G_SUPPLICANT_STATE_SCANNING:
321                 connman_device_set_scanning(device, TRUE);
322                 break;
323
324         case G_SUPPLICANT_STATE_AUTHENTICATING:
325         case G_SUPPLICANT_STATE_ASSOCIATING:
326                 connman_network_set_associating(network, TRUE);
327                 break;
328
329         case G_SUPPLICANT_STATE_COMPLETED:
330                 /* reset scan trigger and schedule background scan */
331                 connman_device_schedule_scan(device);
332
333                 if (get_bssid(device, bssid, &bssid_len) == 0)
334                         connman_network_set_address(network,
335                                                         bssid, bssid_len);
336                 connman_network_set_connected(network, TRUE);
337                 break;
338
339         case G_SUPPLICANT_STATE_DISCONNECTED:
340                 connman_network_set_associating(network, FALSE);
341                 connman_network_set_connected(network, FALSE);
342                 break;
343
344         case G_SUPPLICANT_STATE_INACTIVE:
345                 connman_network_set_associating(network, FALSE);
346                 break;
347
348         case G_SUPPLICANT_STATE_UNKNOWN:
349         case G_SUPPLICANT_STATE_ASSOCIATED:
350         case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
351         case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
352                 break;
353         }
354
355         DBG("DONE");
356 }
357
358 static void interface_removed(GSupplicantInterface *interface)
359 {
360         const char *ifname = g_supplicant_interface_get_ifname(interface);
361         struct wifi_data *wifi;
362
363         DBG("ifname %s", ifname);
364
365         wifi = g_supplicant_interface_get_data(interface);
366
367         if (wifi == NULL || wifi->device == NULL) {
368                 connman_error("Wrong wifi pointer");
369                 return;
370         }
371
372         connman_device_set_powered(wifi->device, FALSE);
373 }
374
375 static void scan_started(GSupplicantInterface *interface)
376 {
377         struct wifi_data *wifi;
378
379         DBG("");
380
381         wifi = g_supplicant_interface_get_data(interface);
382
383         if (wifi == NULL)
384                 return;
385
386         if (wifi->device)
387                 connman_device_set_scanning(wifi->device, TRUE);
388 }
389
390 static void scan_finished(GSupplicantInterface *interface)
391 {
392         struct wifi_data *wifi;
393
394         DBG("");
395
396         wifi = g_supplicant_interface_get_data(interface);
397
398         if (wifi == NULL)
399                 return;
400 }
401
402 static unsigned char calculate_strength(GSupplicantNetwork *supplicant_network)
403 {
404         unsigned char strength;
405
406         strength = 120 + g_supplicant_network_get_signal(supplicant_network);
407         if (strength > 100)
408                 strength = 100;
409
410         return strength;
411 }
412
413 static void network_added(GSupplicantNetwork *supplicant_network)
414 {
415         struct connman_network *network;
416         GSupplicantInterface *interface;
417         struct wifi_data *wifi;
418         const char *name, *path, *identifier, *mode, *security, *group;
419         const unsigned char *ssid;
420         unsigned int ssid_len;
421
422         DBG("");
423
424         interface = g_supplicant_network_get_interface(supplicant_network);
425         wifi = g_supplicant_interface_get_data(interface);
426         name = g_supplicant_network_get_name(supplicant_network);
427         path = g_supplicant_network_get_path(supplicant_network);
428         identifier = g_supplicant_network_get_identifier(supplicant_network);
429         mode = g_supplicant_network_get_mode(supplicant_network);
430         security = g_supplicant_network_get_security(supplicant_network);
431         group = g_supplicant_network_get_identifier(supplicant_network);
432
433         if (wifi == NULL)
434                 return;
435
436         ssid = g_supplicant_network_get_ssid(supplicant_network, &ssid_len);
437
438         network = connman_device_get_network(wifi->device, path);
439
440         if (network == NULL) {
441                 network = connman_network_create(identifier,
442                                                 CONNMAN_NETWORK_TYPE_WIFI);
443                 if (network == NULL)
444                         return;
445
446                 connman_network_set_index(network, wifi->index);
447
448                 connman_network_set_protocol(network,
449                                                 CONNMAN_NETWORK_PROTOCOL_IP);
450
451                 if (connman_device_add_network(wifi->device, network) < 0) {
452                         connman_network_unref(network);
453                         return;
454                 }
455         }
456
457         if (name != NULL && name[0] != '\0')
458                 connman_network_set_name(network, name);
459
460         connman_network_set_blob(network, "WiFi.SSID",
461                                                 ssid, ssid_len);
462         connman_network_set_string(network, "WiFi.Mode", mode);
463         connman_network_set_string(network, "WiFi.Security", security);
464         connman_network_set_strength(network,
465                                 calculate_strength(supplicant_network));
466
467         connman_network_set_available(network, TRUE);
468
469         if (ssid != NULL)
470                 connman_network_set_group(network, group);
471 }
472
473 static void network_removed(GSupplicantNetwork *network)
474 {
475         const char *name = g_supplicant_network_get_name(network);
476
477         DBG("* name %s", name);
478 }
479
480 static void debug(const char *str)
481 {
482         connman_debug("%s", str);
483 }
484
485 static const GSupplicantCallbacks callbacks = {
486         .system_ready           = system_ready,
487         .system_killed          = system_killed,
488         .interface_added        = interface_added,
489         .interface_state        = interface_state,
490         .interface_removed      = interface_removed,
491         .scan_started           = scan_started,
492         .scan_finished          = scan_finished,
493         .network_added          = network_added,
494         .network_removed        = network_removed,
495         .debug                  = debug,
496 };
497
498
499 static int network_probe(struct connman_network *network)
500 {
501         DBG("network %p", network);
502
503         return 0;
504 }
505
506 static void network_remove(struct connman_network *network)
507 {
508         DBG("network %p", network);
509 }
510
511 static void connect_callback(int result, GSupplicantInterface *interface,
512                                                         void *user_data)
513 {
514         connman_error("%s", __func__);
515 }
516
517 static void disconnect_callback(int result, GSupplicantInterface *interface,
518                                                         void *user_data)
519 {
520         struct wifi_data *wifi = user_data;
521
522         if (result < 0) {
523                 connman_error("%s", __func__);
524                 return;
525         }
526
527         connman_network_unref(wifi->network);
528
529         wifi->network = NULL;
530 }
531
532
533 static GSupplicantSecurity network_security(const char *security)
534 {
535         if (g_str_equal(security, "none") == TRUE)
536                 return G_SUPPLICANT_SECURITY_NONE;
537         else if (g_str_equal(security, "wep") == TRUE)
538                 return G_SUPPLICANT_SECURITY_WEP;
539         else if (g_str_equal(security, "psk") == TRUE)
540                 return G_SUPPLICANT_SECURITY_PSK;
541         else if (g_str_equal(security, "wpa") == TRUE)
542                 return G_SUPPLICANT_SECURITY_PSK;
543         else if (g_str_equal(security, "rsn") == TRUE)
544                 return G_SUPPLICANT_SECURITY_PSK;
545         else if (g_str_equal(security, "ieee8021x") == TRUE)
546                 return G_SUPPLICANT_SECURITY_IEEE8021X;
547
548         return G_SUPPLICANT_SECURITY_UNKNOWN;
549 }
550
551 static void ssid_init(GSupplicantSSID *ssid, struct connman_network *network)
552 {
553         const char *security;
554
555         memset(ssid, 0, sizeof(*ssid));
556         ssid->ssid = connman_network_get_blob(network, "WiFi.SSID",
557                                                 &ssid->ssid_len);
558         security = connman_network_get_string(network, "WiFi.Security");
559         ssid->security = network_security(security);
560         ssid->passphrase = connman_network_get_string(network,
561                                                         "WiFi.Passphrase");
562         ssid->eap = connman_network_get_string(network, "WiFi.EAP");
563
564         /*
565          * If our private key password is unset,
566          * we use the supplied passphrase. That is needed
567          * for PEAP where 2 passphrases (identity and client
568          * cert may have to be provided.
569          */
570         if (connman_network_get_string(network,
571                                         "WiFi.PrivateKeyPassphrase") == NULL)
572                 connman_network_set_string(network,
573                                                 "WiFi.PrivateKeyPassphrase",
574                                                 ssid->passphrase);
575         /* We must have an identity for both PEAP and TLS */
576         ssid->identity = connman_network_get_string(network, "WiFi.Identity");
577         ssid->ca_cert_path = connman_network_get_string(network,
578                                                         "WiFi.CACertFile");
579         ssid->client_cert_path = connman_network_get_string(network,
580                                                         "WiFi.ClientCertFile");
581         ssid->private_key_path = connman_network_get_string(network,
582                                                         "WiFi.PrivateKeyFile");
583         ssid->private_key_passphrase = connman_network_get_string(network,
584                                                 "WiFi.PrivateKeyPassphrase");
585         ssid->phase2_auth = connman_network_get_string(network, "WiFi.Phase2");
586
587 }
588
589 static int network_connect(struct connman_network *network)
590 {
591         struct connman_device *device = connman_network_get_device(network);
592         struct wifi_data *wifi;
593         GSupplicantInterface *interface;
594         GSupplicantSSID ssid;
595
596         DBG("network %p", network);
597
598         if (device == NULL)
599                 return -ENODEV;
600
601         wifi = connman_device_get_data(device);
602         if (wifi == NULL)
603                 return -ENODEV;
604
605         interface = wifi->interface;
606
607         ssid_init(&ssid, network);
608
609         wifi->network = connman_network_ref(network);
610
611         return g_supplicant_interface_connect(interface, &ssid,
612                                                 connect_callback, NULL);
613 }
614
615 static int network_disconnect(struct connman_network *network)
616 {
617         struct connman_device *device = connman_network_get_device(network);
618         struct wifi_data *wifi;
619
620         DBG("network %p", network);
621
622         wifi = connman_device_get_data(device);
623         if (wifi == NULL || wifi->interface == NULL)
624                 return -ENODEV;
625
626         return g_supplicant_interface_disconnect(wifi->interface,
627                                                 disconnect_callback, wifi);
628 }
629
630 static struct connman_network_driver network_driver = {
631         .name           = "wifi",
632         .type           = CONNMAN_NETWORK_TYPE_WIFI,
633         .priority       = CONNMAN_NETWORK_PRIORITY_LOW,
634         .probe          = network_probe,
635         .remove         = network_remove,
636         .connect        = network_connect,
637         .disconnect     = network_disconnect,
638 };
639
640 static int wifi_init(void)
641 {
642         int err;
643
644         err = connman_network_driver_register(&network_driver);
645         if (err < 0)
646                 return err;
647
648         err = g_supplicant_register(&callbacks);
649         if (err < 0) {
650                 connman_network_driver_unregister(&network_driver);
651                 return err;
652         }
653
654         return 0;
655 }
656
657 static void wifi_exit(void)
658 {
659         DBG();
660
661         g_supplicant_unregister(&callbacks);
662
663         connman_network_driver_unregister(&network_driver);
664 }
665
666 CONNMAN_PLUGIN_DEFINE(wifi, "WiFi interface plugin", VERSION,
667                 CONNMAN_PLUGIN_PRIORITY_DEFAULT, wifi_init, wifi_exit)