WPA-EAP support for GSupplicant
[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
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 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 };
487
488
489 static int network_probe(struct connman_network *network)
490 {
491         DBG("network %p", network);
492
493         return 0;
494 }
495
496 static void network_remove(struct connman_network *network)
497 {
498         DBG("network %p", network);
499 }
500
501 static void connect_callback(int result, GSupplicantInterface *interface,
502                                                         void *user_data)
503 {
504         connman_error("%s", __func__);
505 }
506
507 static void disconnect_callback(int result, GSupplicantInterface *interface,
508                                                         void *user_data)
509 {
510         struct wifi_data *wifi = user_data;
511
512         if (result < 0) {
513                 connman_error("%s", __func__);
514                 return;
515         }
516
517         connman_network_unref(wifi->network);
518
519         wifi->network = NULL;
520 }
521
522
523 static GSupplicantSecurity network_security(const char *security)
524 {
525         if (g_str_equal(security, "none") == TRUE)
526                 return G_SUPPLICANT_SECURITY_NONE;
527         else if (g_str_equal(security, "wep") == TRUE)
528                 return G_SUPPLICANT_SECURITY_WEP;
529         else if (g_str_equal(security, "psk") == TRUE)
530                 return G_SUPPLICANT_SECURITY_PSK;
531         else if (g_str_equal(security, "wpa") == TRUE)
532                 return G_SUPPLICANT_SECURITY_PSK;
533         else if (g_str_equal(security, "rsn") == TRUE)
534                 return G_SUPPLICANT_SECURITY_PSK;
535         else if (g_str_equal(security, "ieee8021x") == TRUE)
536                 return G_SUPPLICANT_SECURITY_IEEE8021X;
537
538         return G_SUPPLICANT_SECURITY_UNKNOWN;
539 }
540
541 static void ssid_init(GSupplicantSSID *ssid, struct connman_network *network)
542 {
543         const char *security;
544
545         memset(ssid, 0, sizeof(*ssid));
546         ssid->ssid = connman_network_get_blob(network, "WiFi.SSID",
547                                                 &ssid->ssid_len);
548         security = connman_network_get_string(network, "WiFi.Security");
549         ssid->security = network_security(security);
550         ssid->passphrase = connman_network_get_string(network,
551                                                         "WiFi.Passphrase");
552         ssid->eap = connman_network_get_string(network, "WiFi.EAP");
553
554         /*
555          * If our private key password is unset,
556          * we use the supplied passphrase. That is needed
557          * for PEAP where 2 passphrases (identity and client
558          * cert may have to be provided.
559          */
560         if (connman_network_get_string(network,
561                                         "WiFi.PrivateKeyPassphrase") == NULL)
562                 connman_network_set_string(network,
563                                                 "WiFi.PrivateKeyPassphrase",
564                                                 ssid->passphrase);
565         /* We must have an identity for both PEAP and TLS */
566         ssid->identity = connman_network_get_string(network, "WiFi.Identity");
567         ssid->ca_cert_path = connman_network_get_string(network,
568                                                         "WiFi.CACertFile");
569         ssid->client_cert_path = connman_network_get_string(network,
570                                                         "WiFi.ClientCertFile");
571         ssid->private_key_path = connman_network_get_string(network,
572                                                         "WiFi.PrivateKeyFile");
573         ssid->private_key_passphrase = connman_network_get_string(network,
574                                                 "WiFi.PrivateKeyPassphrase");
575         ssid->phase2_auth = connman_network_get_string(network, "WiFi.Phase2");
576
577 }
578
579 static int network_connect(struct connman_network *network)
580 {
581         struct connman_device *device = connman_network_get_device(network);
582         struct wifi_data *wifi;
583         GSupplicantInterface *interface;
584         GSupplicantSSID ssid;
585
586         DBG("network %p", network);
587
588         if (device == NULL)
589                 return -ENODEV;
590
591         wifi = connman_device_get_data(device);
592         if (wifi == NULL)
593                 return -ENODEV;
594
595         interface = wifi->interface;
596
597         ssid_init(&ssid, network);
598
599         wifi->network = connman_network_ref(network);
600
601         return g_supplicant_interface_connect(interface, &ssid,
602                                                 connect_callback, NULL);
603 }
604
605 static int network_disconnect(struct connman_network *network)
606 {
607         struct connman_device *device = connman_network_get_device(network);
608         struct wifi_data *wifi;
609
610         DBG("network %p", network);
611
612         wifi = connman_device_get_data(device);
613         if (wifi == NULL || wifi->interface == NULL)
614                 return -ENODEV;
615
616         return g_supplicant_interface_disconnect(wifi->interface,
617                                                 disconnect_callback, wifi);
618 }
619
620 static struct connman_network_driver network_driver = {
621         .name           = "wifi",
622         .type           = CONNMAN_NETWORK_TYPE_WIFI,
623         .priority       = CONNMAN_NETWORK_PRIORITY_LOW,
624         .probe          = network_probe,
625         .remove         = network_remove,
626         .connect        = network_connect,
627         .disconnect     = network_disconnect,
628 };
629
630 static int wifi_init(void)
631 {
632         int err;
633
634         err = connman_network_driver_register(&network_driver);
635         if (err < 0)
636                 return err;
637
638         err = g_supplicant_register(&callbacks);
639         if (err < 0) {
640                 connman_network_driver_unregister(&network_driver);
641                 return err;
642         }
643
644         return 0;
645 }
646
647 static void wifi_exit(void)
648 {
649         DBG();
650
651         g_supplicant_unregister(&callbacks);
652
653         connman_network_driver_unregister(&network_driver);
654 }
655
656 CONNMAN_PLUGIN_DEFINE(wifi, "WiFi interface plugin", VERSION,
657                 CONNMAN_PLUGIN_PRIORITY_DEFAULT, wifi_init, wifi_exit)