Remove unneeded enum connman_network_protocol
[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, *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         identifier = g_supplicant_network_get_identifier(supplicant_network);
423         mode = g_supplicant_network_get_mode(supplicant_network);
424         security = g_supplicant_network_get_security(supplicant_network);
425         group = g_supplicant_network_get_identifier(supplicant_network);
426
427         if (wifi == NULL)
428                 return;
429
430         ssid = g_supplicant_network_get_ssid(supplicant_network, &ssid_len);
431
432         network = connman_device_get_network(wifi->device, identifier);
433
434         if (network == NULL) {
435                 network = connman_network_create(identifier,
436                                                 CONNMAN_NETWORK_TYPE_WIFI);
437                 if (network == NULL)
438                         return;
439
440                 connman_network_set_index(network, wifi->index);
441
442                 if (connman_device_add_network(wifi->device, network) < 0) {
443                         connman_network_unref(network);
444                         return;
445                 }
446         }
447
448         if (name != NULL && name[0] != '\0')
449                 connman_network_set_name(network, name);
450
451         connman_network_set_blob(network, "WiFi.SSID",
452                                                 ssid, ssid_len);
453         connman_network_set_string(network, "WiFi.Mode", mode);
454         connman_network_set_string(network, "WiFi.Security", security);
455         connman_network_set_strength(network,
456                                 calculate_strength(supplicant_network));
457
458         connman_network_set_available(network, TRUE);
459
460         if (ssid != NULL)
461                 connman_network_set_group(network, group);
462 }
463
464 static void network_removed(GSupplicantNetwork *network)
465 {
466         const char *name = g_supplicant_network_get_name(network);
467
468         DBG("* name %s", name);
469 }
470
471 static void debug(const char *str)
472 {
473         connman_debug("%s", str);
474 }
475
476 static const GSupplicantCallbacks callbacks = {
477         .system_ready           = system_ready,
478         .system_killed          = system_killed,
479         .interface_added        = interface_added,
480         .interface_state        = interface_state,
481         .interface_removed      = interface_removed,
482         .scan_started           = scan_started,
483         .scan_finished          = scan_finished,
484         .network_added          = network_added,
485         .network_removed        = network_removed,
486         .debug                  = debug,
487 };
488
489
490 static int network_probe(struct connman_network *network)
491 {
492         DBG("network %p", network);
493
494         return 0;
495 }
496
497 static void network_remove(struct connman_network *network)
498 {
499         DBG("network %p", network);
500 }
501
502 static void connect_callback(int result, GSupplicantInterface *interface,
503                                                         void *user_data)
504 {
505         connman_error("%s", __func__);
506 }
507
508 static void disconnect_callback(int result, GSupplicantInterface *interface,
509                                                         void *user_data)
510 {
511         struct wifi_data *wifi = user_data;
512
513         if (result < 0) {
514                 connman_error("%s", __func__);
515                 return;
516         }
517
518         connman_network_unref(wifi->network);
519
520         wifi->network = NULL;
521 }
522
523
524 static GSupplicantSecurity network_security(const char *security)
525 {
526         if (g_str_equal(security, "none") == TRUE)
527                 return G_SUPPLICANT_SECURITY_NONE;
528         else if (g_str_equal(security, "wep") == TRUE)
529                 return G_SUPPLICANT_SECURITY_WEP;
530         else if (g_str_equal(security, "psk") == TRUE)
531                 return G_SUPPLICANT_SECURITY_PSK;
532         else if (g_str_equal(security, "wpa") == TRUE)
533                 return G_SUPPLICANT_SECURITY_PSK;
534         else if (g_str_equal(security, "rsn") == TRUE)
535                 return G_SUPPLICANT_SECURITY_PSK;
536         else if (g_str_equal(security, "ieee8021x") == TRUE)
537                 return G_SUPPLICANT_SECURITY_IEEE8021X;
538
539         return G_SUPPLICANT_SECURITY_UNKNOWN;
540 }
541
542 static void ssid_init(GSupplicantSSID *ssid, struct connman_network *network)
543 {
544         const char *security;
545
546         memset(ssid, 0, sizeof(*ssid));
547         ssid->ssid = connman_network_get_blob(network, "WiFi.SSID",
548                                                 &ssid->ssid_len);
549         security = connman_network_get_string(network, "WiFi.Security");
550         ssid->security = network_security(security);
551         ssid->passphrase = connman_network_get_string(network,
552                                                         "WiFi.Passphrase");
553         ssid->eap = connman_network_get_string(network, "WiFi.EAP");
554
555         /*
556          * If our private key password is unset,
557          * we use the supplied passphrase. That is needed
558          * for PEAP where 2 passphrases (identity and client
559          * cert may have to be provided.
560          */
561         if (connman_network_get_string(network,
562                                         "WiFi.PrivateKeyPassphrase") == NULL)
563                 connman_network_set_string(network,
564                                                 "WiFi.PrivateKeyPassphrase",
565                                                 ssid->passphrase);
566         /* We must have an identity for both PEAP and TLS */
567         ssid->identity = connman_network_get_string(network, "WiFi.Identity");
568         ssid->ca_cert_path = connman_network_get_string(network,
569                                                         "WiFi.CACertFile");
570         ssid->client_cert_path = connman_network_get_string(network,
571                                                         "WiFi.ClientCertFile");
572         ssid->private_key_path = connman_network_get_string(network,
573                                                         "WiFi.PrivateKeyFile");
574         ssid->private_key_passphrase = connman_network_get_string(network,
575                                                 "WiFi.PrivateKeyPassphrase");
576         ssid->phase2_auth = connman_network_get_string(network, "WiFi.Phase2");
577
578 }
579
580 static int network_connect(struct connman_network *network)
581 {
582         struct connman_device *device = connman_network_get_device(network);
583         struct wifi_data *wifi;
584         GSupplicantInterface *interface;
585         GSupplicantSSID ssid;
586
587         DBG("network %p", network);
588
589         if (device == NULL)
590                 return -ENODEV;
591
592         wifi = connman_device_get_data(device);
593         if (wifi == NULL)
594                 return -ENODEV;
595
596         interface = wifi->interface;
597
598         ssid_init(&ssid, network);
599
600         wifi->network = connman_network_ref(network);
601
602         return g_supplicant_interface_connect(interface, &ssid,
603                                                 connect_callback, NULL);
604 }
605
606 static int network_disconnect(struct connman_network *network)
607 {
608         struct connman_device *device = connman_network_get_device(network);
609         struct wifi_data *wifi;
610
611         DBG("network %p", network);
612
613         wifi = connman_device_get_data(device);
614         if (wifi == NULL || wifi->interface == NULL)
615                 return -ENODEV;
616
617         connman_network_set_associating(network, FALSE);
618
619         return g_supplicant_interface_disconnect(wifi->interface,
620                                                 disconnect_callback, wifi);
621 }
622
623 static struct connman_network_driver network_driver = {
624         .name           = "wifi",
625         .type           = CONNMAN_NETWORK_TYPE_WIFI,
626         .priority       = CONNMAN_NETWORK_PRIORITY_LOW,
627         .probe          = network_probe,
628         .remove         = network_remove,
629         .connect        = network_connect,
630         .disconnect     = network_disconnect,
631 };
632
633 static int wifi_init(void)
634 {
635         int err;
636
637         err = connman_network_driver_register(&network_driver);
638         if (err < 0)
639                 return err;
640
641         err = g_supplicant_register(&callbacks);
642         if (err < 0) {
643                 connman_network_driver_unregister(&network_driver);
644                 return err;
645         }
646
647         return 0;
648 }
649
650 static void wifi_exit(void)
651 {
652         DBG();
653
654         g_supplicant_unregister(&callbacks);
655
656         connman_network_driver_unregister(&network_driver);
657 }
658
659 CONNMAN_PLUGIN_DEFINE(wifi, "WiFi interface plugin", VERSION,
660                 CONNMAN_PLUGIN_PRIORITY_DEFAULT, wifi_init, wifi_exit)