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