wifi: Register technology driver
[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/technology.h>
48 #include <connman/log.h>
49 #include <connman/option.h>
50
51 #include <gsupplicant/gsupplicant.h>
52
53 #define CLEANUP_TIMEOUT   8     /* in seconds */
54 #define INACTIVE_TIMEOUT  12    /* in seconds */
55
56 struct wifi_data {
57         char *identifier;
58         struct connman_device *device;
59         struct connman_network *network;
60         GSupplicantInterface *interface;
61         connman_bool_t connected;
62         int index;
63         unsigned flags;
64         unsigned int watch;
65 };
66
67 static int get_bssid(struct connman_device *device,
68                                 unsigned char *bssid, unsigned int *bssid_len)
69 {
70         struct iwreq wrq;
71         char *ifname;
72         int ifindex;
73         int fd, err;
74
75         ifindex = connman_device_get_index(device);
76         if (ifindex < 0)
77                 return -EINVAL;
78
79         ifname = connman_inet_ifname(ifindex);
80         if (ifname == NULL)
81                 return -EINVAL;
82
83         fd = socket(PF_INET, SOCK_DGRAM, 0);
84         if (fd < 0) {
85                 g_free(ifname);
86                 return -EINVAL;
87         }
88
89         memset(&wrq, 0, sizeof(wrq));
90         strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
91
92         err = ioctl(fd, SIOCGIWAP, &wrq);
93
94         g_free(ifname);
95         close(fd);
96
97         if (err < 0)
98                 return -EIO;
99
100         memcpy(bssid, wrq.u.ap_addr.sa_data, ETH_ALEN);
101         *bssid_len = ETH_ALEN;
102
103         return 0;
104 }
105
106 static void wifi_newlink(unsigned flags, unsigned change, void *user_data)
107 {
108         struct connman_device *device = user_data;
109         struct wifi_data *wifi = connman_device_get_data(device);
110
111         DBG("index %d flags %d change %d", wifi->index, flags, change);
112
113         if (!change)
114                 return;
115
116         if ((wifi->flags & IFF_UP) != (flags & IFF_UP)) {
117                 if (flags & IFF_UP)
118                         DBG("interface up");
119                 else
120                         DBG("interface down");
121         }
122
123         if ((wifi->flags & IFF_LOWER_UP) != (flags & IFF_LOWER_UP)) {
124                 if (flags & IFF_LOWER_UP)
125                         DBG("carrier on");
126                 else
127                         DBG("carrier off");
128         }
129
130         wifi->flags = flags;
131 }
132
133 static int wifi_probe(struct connman_device *device)
134 {
135         struct wifi_data *wifi;
136
137         DBG("device %p", device);
138
139         wifi = g_try_new0(struct wifi_data, 1);
140         if (wifi == NULL)
141                 return -ENOMEM;
142
143         wifi->connected = FALSE;
144
145         connman_device_set_data(device, wifi);
146         wifi->device = connman_device_ref(device);
147
148         wifi->index = connman_device_get_index(device);
149         wifi->flags = 0;
150
151         wifi->watch = connman_rtnl_add_newlink_watch(wifi->index,
152                                                         wifi_newlink, device);
153
154         return 0;
155 }
156
157 static void wifi_remove(struct connman_device *device)
158 {
159         struct wifi_data *wifi = connman_device_get_data(device);
160
161         DBG("device %p", device);
162
163         connman_device_set_data(device, NULL);
164         connman_device_unref(wifi->device);
165         connman_rtnl_remove_watch(wifi->watch);
166
167         g_supplicant_interface_set_data(wifi->interface, NULL);
168
169         g_free(wifi->identifier);
170         g_free(wifi);
171 }
172
173 static void interface_create_callback(int result,
174                                         GSupplicantInterface *interface,
175                                                         void *user_data)
176 {
177         struct wifi_data *wifi = user_data;
178
179         DBG("result %d ifname %s", result,
180                                 g_supplicant_interface_get_ifname(interface));
181
182         if (result < 0)
183                 return;
184
185         wifi->interface = interface;
186         g_supplicant_interface_set_data(interface, wifi);
187 }
188
189 static void interface_remove_callback(int result,
190                                         GSupplicantInterface *interface,
191                                                         void *user_data)
192 {
193         struct wifi_data *wifi = user_data;
194
195         DBG("result %d", result);
196
197         if (result < 0)
198                 return;
199
200         wifi->interface = NULL;
201 }
202
203
204 static int wifi_enable(struct connman_device *device)
205 {
206         struct wifi_data *wifi = connman_device_get_data(device);
207         const char *interface = connman_device_get_string(device, "Interface");
208         const char *driver = connman_option_get_string("wifi");
209
210         DBG("device %p %p", device, wifi);
211
212         return g_supplicant_interface_create(interface, driver,
213                                                 interface_create_callback,
214                                                         wifi);
215 }
216
217 static int wifi_disable(struct connman_device *device)
218 {
219         struct wifi_data *wifi = connman_device_get_data(device);
220
221         DBG("device %p", device);
222
223         wifi->connected = FALSE;
224
225         return g_supplicant_interface_remove(wifi->interface,
226                                                 interface_remove_callback,
227                                                         wifi);
228 }
229
230 static void scan_callback(int result, GSupplicantInterface *interface,
231                                                 void *user_data)
232 {
233         struct connman_device *device = user_data;
234
235         DBG("result %d", result);
236
237         connman_device_set_scanning(device, FALSE);
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 = 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 = 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_associating(network, FALSE);
339                 connman_network_set_connected(network, FALSE);
340                 break;
341
342         case G_SUPPLICANT_STATE_INACTIVE:
343                 connman_network_set_associating(network, FALSE);
344                 break;
345
346         case G_SUPPLICANT_STATE_UNKNOWN:
347         case G_SUPPLICANT_STATE_ASSOCIATED:
348         case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
349         case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
350                 break;
351         }
352
353         DBG("DONE");
354 }
355
356 static void interface_removed(GSupplicantInterface *interface)
357 {
358         const char *ifname = g_supplicant_interface_get_ifname(interface);
359         struct wifi_data *wifi;
360
361         DBG("ifname %s", ifname);
362
363         wifi = g_supplicant_interface_get_data(interface);
364
365         if (wifi == NULL || wifi->device == NULL) {
366                 connman_error("Wrong wifi pointer");
367                 return;
368         }
369
370         connman_device_set_powered(wifi->device, FALSE);
371 }
372
373 static void scan_started(GSupplicantInterface *interface)
374 {
375         struct wifi_data *wifi;
376
377         DBG("");
378
379         wifi = g_supplicant_interface_get_data(interface);
380
381         if (wifi == NULL)
382                 return;
383
384         if (wifi->device)
385                 connman_device_set_scanning(wifi->device, TRUE);
386 }
387
388 static void scan_finished(GSupplicantInterface *interface)
389 {
390         struct wifi_data *wifi;
391
392         DBG("");
393
394         wifi = g_supplicant_interface_get_data(interface);
395
396         if (wifi == NULL)
397                 return;
398 }
399
400 static unsigned char calculate_strength(GSupplicantNetwork *supplicant_network)
401 {
402         unsigned char strength;
403
404         strength = 120 + g_supplicant_network_get_signal(supplicant_network);
405         if (strength > 100)
406                 strength = 100;
407
408         return strength;
409 }
410
411 static void network_added(GSupplicantNetwork *supplicant_network)
412 {
413         struct connman_network *network;
414         GSupplicantInterface *interface;
415         struct wifi_data *wifi;
416         const char *name, *identifier, *mode, *security, *group;
417         const unsigned char *ssid;
418         unsigned int ssid_len;
419
420         DBG("");
421
422         interface = g_supplicant_network_get_interface(supplicant_network);
423         wifi = g_supplicant_interface_get_data(interface);
424         name = g_supplicant_network_get_name(supplicant_network);
425         identifier = g_supplicant_network_get_identifier(supplicant_network);
426         mode = g_supplicant_network_get_mode(supplicant_network);
427         security = g_supplicant_network_get_security(supplicant_network);
428         group = g_supplicant_network_get_identifier(supplicant_network);
429
430         if (wifi == NULL)
431                 return;
432
433         ssid = g_supplicant_network_get_ssid(supplicant_network, &ssid_len);
434
435         network = connman_device_get_network(wifi->device, identifier);
436
437         if (network == NULL) {
438                 network = connman_network_create(identifier,
439                                                 CONNMAN_NETWORK_TYPE_WIFI);
440                 if (network == NULL)
441                         return;
442
443                 connman_network_set_index(network, wifi->index);
444
445                 if (connman_device_add_network(wifi->device, network) < 0) {
446                         connman_network_unref(network);
447                         return;
448                 }
449         }
450
451         if (name != NULL && name[0] != '\0')
452                 connman_network_set_name(network, name);
453
454         connman_network_set_blob(network, "WiFi.SSID",
455                                                 ssid, ssid_len);
456         connman_network_set_string(network, "WiFi.Mode", mode);
457         connman_network_set_string(network, "WiFi.Security", security);
458         connman_network_set_strength(network,
459                                 calculate_strength(supplicant_network));
460
461         connman_network_set_available(network, TRUE);
462
463         if (ssid != NULL)
464                 connman_network_set_group(network, group);
465 }
466
467 static void network_removed(GSupplicantNetwork *network)
468 {
469         const char *name = g_supplicant_network_get_name(network);
470
471         DBG("* name %s", name);
472 }
473
474 static void debug(const char *str)
475 {
476         connman_debug("%s", str);
477 }
478
479 static const GSupplicantCallbacks callbacks = {
480         .system_ready           = system_ready,
481         .system_killed          = system_killed,
482         .interface_added        = interface_added,
483         .interface_state        = interface_state,
484         .interface_removed      = interface_removed,
485         .scan_started           = scan_started,
486         .scan_finished          = scan_finished,
487         .network_added          = network_added,
488         .network_removed        = network_removed,
489         .debug                  = debug,
490 };
491
492
493 static int network_probe(struct connman_network *network)
494 {
495         DBG("network %p", network);
496
497         return 0;
498 }
499
500 static void network_remove(struct connman_network *network)
501 {
502         DBG("network %p", network);
503 }
504
505 static void connect_callback(int result, GSupplicantInterface *interface,
506                                                         void *user_data)
507 {
508         connman_error("%s", __func__);
509 }
510
511 static void disconnect_callback(int result, GSupplicantInterface *interface,
512                                                         void *user_data)
513 {
514         struct wifi_data *wifi = user_data;
515
516         if (result < 0) {
517                 connman_error("%s", __func__);
518                 return;
519         }
520
521         connman_network_unref(wifi->network);
522
523         wifi->network = NULL;
524 }
525
526
527 static GSupplicantSecurity network_security(const char *security)
528 {
529         if (g_str_equal(security, "none") == TRUE)
530                 return G_SUPPLICANT_SECURITY_NONE;
531         else if (g_str_equal(security, "wep") == TRUE)
532                 return G_SUPPLICANT_SECURITY_WEP;
533         else if (g_str_equal(security, "psk") == TRUE)
534                 return G_SUPPLICANT_SECURITY_PSK;
535         else if (g_str_equal(security, "wpa") == TRUE)
536                 return G_SUPPLICANT_SECURITY_PSK;
537         else if (g_str_equal(security, "rsn") == TRUE)
538                 return G_SUPPLICANT_SECURITY_PSK;
539         else if (g_str_equal(security, "ieee8021x") == TRUE)
540                 return G_SUPPLICANT_SECURITY_IEEE8021X;
541
542         return G_SUPPLICANT_SECURITY_UNKNOWN;
543 }
544
545 static void ssid_init(GSupplicantSSID *ssid, struct connman_network *network)
546 {
547         const char *security;
548
549         memset(ssid, 0, sizeof(*ssid));
550         ssid->ssid = connman_network_get_blob(network, "WiFi.SSID",
551                                                 &ssid->ssid_len);
552         security = connman_network_get_string(network, "WiFi.Security");
553         ssid->security = network_security(security);
554         ssid->passphrase = connman_network_get_string(network,
555                                                         "WiFi.Passphrase");
556         ssid->eap = connman_network_get_string(network, "WiFi.EAP");
557
558         /*
559          * If our private key password is unset,
560          * we use the supplied passphrase. That is needed
561          * for PEAP where 2 passphrases (identity and client
562          * cert may have to be provided.
563          */
564         if (connman_network_get_string(network,
565                                         "WiFi.PrivateKeyPassphrase") == NULL)
566                 connman_network_set_string(network,
567                                                 "WiFi.PrivateKeyPassphrase",
568                                                 ssid->passphrase);
569         /* We must have an identity for both PEAP and TLS */
570         ssid->identity = connman_network_get_string(network, "WiFi.Identity");
571         ssid->ca_cert_path = connman_network_get_string(network,
572                                                         "WiFi.CACertFile");
573         ssid->client_cert_path = connman_network_get_string(network,
574                                                         "WiFi.ClientCertFile");
575         ssid->private_key_path = connman_network_get_string(network,
576                                                         "WiFi.PrivateKeyFile");
577         ssid->private_key_passphrase = connman_network_get_string(network,
578                                                 "WiFi.PrivateKeyPassphrase");
579         ssid->phase2_auth = connman_network_get_string(network, "WiFi.Phase2");
580
581 }
582
583 static int network_connect(struct connman_network *network)
584 {
585         struct connman_device *device = connman_network_get_device(network);
586         struct wifi_data *wifi;
587         GSupplicantInterface *interface;
588         GSupplicantSSID ssid;
589
590         DBG("network %p", network);
591
592         if (device == NULL)
593                 return -ENODEV;
594
595         wifi = connman_device_get_data(device);
596         if (wifi == NULL)
597                 return -ENODEV;
598
599         interface = wifi->interface;
600
601         ssid_init(&ssid, network);
602
603         wifi->network = connman_network_ref(network);
604
605         return g_supplicant_interface_connect(interface, &ssid,
606                                                 connect_callback, NULL);
607 }
608
609 static int network_disconnect(struct connman_network *network)
610 {
611         struct connman_device *device = connman_network_get_device(network);
612         struct wifi_data *wifi;
613
614         DBG("network %p", network);
615
616         wifi = connman_device_get_data(device);
617         if (wifi == NULL || wifi->interface == NULL)
618                 return -ENODEV;
619
620         connman_network_set_associating(network, FALSE);
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 tech_probe(struct connman_technology *technology)
637 {
638         return 0;
639 }
640
641 static void tech_remove(struct connman_technology *technology)
642 {
643 }
644
645 static int tech_set_regdom(struct connman_technology *technology, const char *alpha2)
646 {
647         return 0;
648 }
649
650 static struct connman_technology_driver tech_driver = {
651         .name           = "bluetooth",
652         .type           = CONNMAN_SERVICE_TYPE_BLUETOOTH,
653         .probe          = tech_probe,
654         .remove         = tech_remove,
655         .set_regdom     = tech_set_regdom,
656 };
657
658 static int wifi_init(void)
659 {
660         int err;
661
662         err = connman_network_driver_register(&network_driver);
663         if (err < 0)
664                 return err;
665
666         err = g_supplicant_register(&callbacks);
667         if (err < 0) {
668                 connman_network_driver_unregister(&network_driver);
669                 return err;
670         }
671
672         err = connman_technology_driver_register(&tech_driver);
673         if (err < 0) {
674                 g_supplicant_unregister(&callbacks);
675                 connman_network_driver_unregister(&network_driver);
676                 return err;
677         }
678
679         return 0;
680 }
681
682 static void wifi_exit(void)
683 {
684         DBG();
685
686         connman_technology_driver_unregister(&tech_driver);
687
688         g_supplicant_unregister(&callbacks);
689
690         connman_network_driver_unregister(&network_driver);
691 }
692
693 CONNMAN_PLUGIN_DEFINE(wifi, "WiFi interface plugin", VERSION,
694                 CONNMAN_PLUGIN_PRIORITY_DEFAULT, wifi_init, wifi_exit)