gsupplicant: Set interface data to NULL upon driver removal
[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_supplicant_interface_set_data(wifi->interface, NULL);
167
168         g_free(wifi->identifier);
169         g_free(wifi);
170 }
171
172 static void interface_create_callback(int result,
173                                         GSupplicantInterface *interface,
174                                                         void *user_data)
175 {
176         struct wifi_data *wifi = user_data;
177
178         DBG("result %d ifname %s", result,
179                                 g_supplicant_interface_get_ifname(interface));
180
181         if (result < 0)
182                 return;
183
184         wifi->interface = interface;
185         g_supplicant_interface_set_data(interface, wifi);
186 }
187
188 static void interface_remove_callback(int result,
189                                         GSupplicantInterface *interface,
190                                                         void *user_data)
191 {
192         struct wifi_data *wifi = user_data;
193
194         DBG("result %d", result);
195
196         if (result < 0)
197                 return;
198
199         wifi->interface = NULL;
200 }
201
202
203 static int wifi_enable(struct connman_device *device)
204 {
205         struct wifi_data *wifi = connman_device_get_data(device);
206         const char *interface = connman_device_get_string(device, "Interface");
207         const char *driver = connman_option_get_string("wifi");
208
209         DBG("device %p %p", device, wifi);
210
211         return g_supplicant_interface_create(interface, driver,
212                                                 interface_create_callback,
213                                                         wifi);
214 }
215
216 static int wifi_disable(struct connman_device *device)
217 {
218         struct wifi_data *wifi = connman_device_get_data(device);
219
220         DBG("device %p", device);
221
222         wifi->connected = FALSE;
223
224         return g_supplicant_interface_remove(wifi->interface,
225                                                 interface_remove_callback,
226                                                         wifi);
227 }
228
229 static void scan_callback(int result, GSupplicantInterface *interface,
230                                                 void *user_data)
231 {
232         struct connman_device *device = user_data;
233
234         DBG("result %d", result);
235
236         connman_device_set_scanning(device, FALSE);
237 }
238
239 static int wifi_scan(struct connman_device *device)
240 {
241         struct wifi_data *wifi = connman_device_get_data(device);
242
243         DBG("device %p %p", device, wifi->interface);
244
245         return g_supplicant_interface_scan(wifi->interface, scan_callback,
246                                                                 device);
247 }
248
249 static struct connman_device_driver wifi_ng_driver = {
250         .name           = "wifi",
251         .type           = CONNMAN_DEVICE_TYPE_WIFI,
252         .priority       = CONNMAN_DEVICE_PRIORITY_LOW,
253         .probe          = wifi_probe,
254         .remove         = wifi_remove,
255         .enable         = wifi_enable,
256         .disable        = wifi_disable,
257         .scan           = wifi_scan,
258 };
259
260 static void system_ready(void)
261 {
262         DBG("");
263
264         if (connman_device_driver_register(&wifi_ng_driver) < 0)
265                 connman_error("Failed to register WiFi driver");
266 }
267
268 static void system_killed(void)
269 {
270         DBG("");
271
272         connman_device_driver_unregister(&wifi_ng_driver);
273 }
274
275 static void interface_added(GSupplicantInterface *interface)
276 {
277         const char *ifname = g_supplicant_interface_get_ifname(interface);
278         const char *driver = g_supplicant_interface_get_driver(interface);
279         struct wifi_data *wifi;
280
281         wifi = g_supplicant_interface_get_data(interface);
282
283         DBG("ifname %s driver %s wifi %p", ifname, driver, wifi);
284
285         if (wifi == NULL || wifi->device == NULL) {
286                 connman_error("Wrong wifi pointer");
287                 return;
288         }
289
290         connman_device_set_powered(wifi->device, TRUE);
291         wifi_scan(wifi->device);
292 }
293
294 static void interface_state(GSupplicantInterface *interface)
295 {
296         struct connman_network *network;
297         struct connman_device *device;
298         struct wifi_data *wifi;
299         GSupplicantState state = g_supplicant_interface_get_state(interface);
300         unsigned char bssid[ETH_ALEN];
301         unsigned int bssid_len;
302
303         wifi = g_supplicant_interface_get_data(interface);
304
305         DBG("wifi %p interface state %d", wifi, state);
306
307         if (wifi == NULL)
308                 return;
309
310         network = wifi->network;
311         device = wifi->device;
312
313         if (device == NULL || network == NULL)
314                 return;
315
316         switch (state) {
317         case G_SUPPLICANT_STATE_SCANNING:
318                 connman_device_set_scanning(device, TRUE);
319                 break;
320
321         case G_SUPPLICANT_STATE_AUTHENTICATING:
322         case G_SUPPLICANT_STATE_ASSOCIATING:
323                 connman_network_set_associating(network, TRUE);
324                 break;
325
326         case G_SUPPLICANT_STATE_COMPLETED:
327                 /* reset scan trigger and schedule background scan */
328                 connman_device_schedule_scan(device);
329
330                 if (get_bssid(device, bssid, &bssid_len) == 0)
331                         connman_network_set_address(network,
332                                                         bssid, bssid_len);
333                 connman_network_set_connected(network, TRUE);
334                 break;
335
336         case G_SUPPLICANT_STATE_DISCONNECTED:
337                 connman_network_set_associating(network, FALSE);
338                 connman_network_set_connected(network, FALSE);
339                 break;
340
341         case G_SUPPLICANT_STATE_INACTIVE:
342                 connman_network_set_associating(network, FALSE);
343                 break;
344
345         case G_SUPPLICANT_STATE_UNKNOWN:
346         case G_SUPPLICANT_STATE_ASSOCIATED:
347         case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
348         case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
349                 break;
350         }
351
352         DBG("DONE");
353 }
354
355 static void interface_removed(GSupplicantInterface *interface)
356 {
357         const char *ifname = g_supplicant_interface_get_ifname(interface);
358         struct wifi_data *wifi;
359
360         DBG("ifname %s", ifname);
361
362         wifi = g_supplicant_interface_get_data(interface);
363
364         if (wifi == NULL || wifi->device == NULL) {
365                 connman_error("Wrong wifi pointer");
366                 return;
367         }
368
369         connman_device_set_powered(wifi->device, FALSE);
370 }
371
372 static void scan_started(GSupplicantInterface *interface)
373 {
374         struct wifi_data *wifi;
375
376         DBG("");
377
378         wifi = g_supplicant_interface_get_data(interface);
379
380         if (wifi == NULL)
381                 return;
382
383         if (wifi->device)
384                 connman_device_set_scanning(wifi->device, TRUE);
385 }
386
387 static void scan_finished(GSupplicantInterface *interface)
388 {
389         struct wifi_data *wifi;
390
391         DBG("");
392
393         wifi = g_supplicant_interface_get_data(interface);
394
395         if (wifi == NULL)
396                 return;
397 }
398
399 static unsigned char calculate_strength(GSupplicantNetwork *supplicant_network)
400 {
401         unsigned char strength;
402
403         strength = 120 + g_supplicant_network_get_signal(supplicant_network);
404         if (strength > 100)
405                 strength = 100;
406
407         return strength;
408 }
409
410 static void network_added(GSupplicantNetwork *supplicant_network)
411 {
412         struct connman_network *network;
413         GSupplicantInterface *interface;
414         struct wifi_data *wifi;
415         const char *name, *identifier, *mode, *security, *group;
416         const unsigned char *ssid;
417         unsigned int ssid_len;
418
419         DBG("");
420
421         interface = g_supplicant_network_get_interface(supplicant_network);
422         wifi = g_supplicant_interface_get_data(interface);
423         name = g_supplicant_network_get_name(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 = g_supplicant_network_get_ssid(supplicant_network, &ssid_len);
433
434         network = connman_device_get_network(wifi->device, identifier);
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                 if (connman_device_add_network(wifi->device, network) < 0) {
445                         connman_network_unref(network);
446                         return;
447                 }
448         }
449
450         if (name != NULL && name[0] != '\0')
451                 connman_network_set_name(network, name);
452
453         connman_network_set_blob(network, "WiFi.SSID",
454                                                 ssid, ssid_len);
455         connman_network_set_string(network, "WiFi.Mode", mode);
456         connman_network_set_string(network, "WiFi.Security", security);
457         connman_network_set_strength(network,
458                                 calculate_strength(supplicant_network));
459
460         connman_network_set_available(network, TRUE);
461
462         if (ssid != NULL)
463                 connman_network_set_group(network, group);
464 }
465
466 static void network_removed(GSupplicantNetwork *network)
467 {
468         const char *name = g_supplicant_network_get_name(network);
469
470         DBG("* name %s", name);
471 }
472
473 static void debug(const char *str)
474 {
475         connman_debug("%s", str);
476 }
477
478 static const GSupplicantCallbacks callbacks = {
479         .system_ready           = system_ready,
480         .system_killed          = system_killed,
481         .interface_added        = interface_added,
482         .interface_state        = interface_state,
483         .interface_removed      = interface_removed,
484         .scan_started           = scan_started,
485         .scan_finished          = scan_finished,
486         .network_added          = network_added,
487         .network_removed        = network_removed,
488         .debug                  = debug,
489 };
490
491
492 static int network_probe(struct connman_network *network)
493 {
494         DBG("network %p", network);
495
496         return 0;
497 }
498
499 static void network_remove(struct connman_network *network)
500 {
501         DBG("network %p", network);
502 }
503
504 static void connect_callback(int result, GSupplicantInterface *interface,
505                                                         void *user_data)
506 {
507         connman_error("%s", __func__);
508 }
509
510 static void disconnect_callback(int result, GSupplicantInterface *interface,
511                                                         void *user_data)
512 {
513         struct wifi_data *wifi = user_data;
514
515         if (result < 0) {
516                 connman_error("%s", __func__);
517                 return;
518         }
519
520         connman_network_unref(wifi->network);
521
522         wifi->network = NULL;
523 }
524
525
526 static GSupplicantSecurity network_security(const char *security)
527 {
528         if (g_str_equal(security, "none") == TRUE)
529                 return G_SUPPLICANT_SECURITY_NONE;
530         else if (g_str_equal(security, "wep") == TRUE)
531                 return G_SUPPLICANT_SECURITY_WEP;
532         else if (g_str_equal(security, "psk") == TRUE)
533                 return G_SUPPLICANT_SECURITY_PSK;
534         else if (g_str_equal(security, "wpa") == TRUE)
535                 return G_SUPPLICANT_SECURITY_PSK;
536         else if (g_str_equal(security, "rsn") == TRUE)
537                 return G_SUPPLICANT_SECURITY_PSK;
538         else if (g_str_equal(security, "ieee8021x") == TRUE)
539                 return G_SUPPLICANT_SECURITY_IEEE8021X;
540
541         return G_SUPPLICANT_SECURITY_UNKNOWN;
542 }
543
544 static void ssid_init(GSupplicantSSID *ssid, struct connman_network *network)
545 {
546         const char *security;
547
548         memset(ssid, 0, sizeof(*ssid));
549         ssid->ssid = connman_network_get_blob(network, "WiFi.SSID",
550                                                 &ssid->ssid_len);
551         security = connman_network_get_string(network, "WiFi.Security");
552         ssid->security = network_security(security);
553         ssid->passphrase = connman_network_get_string(network,
554                                                         "WiFi.Passphrase");
555         ssid->eap = connman_network_get_string(network, "WiFi.EAP");
556
557         /*
558          * If our private key password is unset,
559          * we use the supplied passphrase. That is needed
560          * for PEAP where 2 passphrases (identity and client
561          * cert may have to be provided.
562          */
563         if (connman_network_get_string(network,
564                                         "WiFi.PrivateKeyPassphrase") == NULL)
565                 connman_network_set_string(network,
566                                                 "WiFi.PrivateKeyPassphrase",
567                                                 ssid->passphrase);
568         /* We must have an identity for both PEAP and TLS */
569         ssid->identity = connman_network_get_string(network, "WiFi.Identity");
570         ssid->ca_cert_path = connman_network_get_string(network,
571                                                         "WiFi.CACertFile");
572         ssid->client_cert_path = connman_network_get_string(network,
573                                                         "WiFi.ClientCertFile");
574         ssid->private_key_path = connman_network_get_string(network,
575                                                         "WiFi.PrivateKeyFile");
576         ssid->private_key_passphrase = connman_network_get_string(network,
577                                                 "WiFi.PrivateKeyPassphrase");
578         ssid->phase2_auth = connman_network_get_string(network, "WiFi.Phase2");
579
580 }
581
582 static int network_connect(struct connman_network *network)
583 {
584         struct connman_device *device = connman_network_get_device(network);
585         struct wifi_data *wifi;
586         GSupplicantInterface *interface;
587         GSupplicantSSID ssid;
588
589         DBG("network %p", network);
590
591         if (device == NULL)
592                 return -ENODEV;
593
594         wifi = connman_device_get_data(device);
595         if (wifi == NULL)
596                 return -ENODEV;
597
598         interface = wifi->interface;
599
600         ssid_init(&ssid, network);
601
602         wifi->network = connman_network_ref(network);
603
604         return g_supplicant_interface_connect(interface, &ssid,
605                                                 connect_callback, NULL);
606 }
607
608 static int network_disconnect(struct connman_network *network)
609 {
610         struct connman_device *device = connman_network_get_device(network);
611         struct wifi_data *wifi;
612
613         DBG("network %p", network);
614
615         wifi = connman_device_get_data(device);
616         if (wifi == NULL || wifi->interface == NULL)
617                 return -ENODEV;
618
619         connman_network_set_associating(network, FALSE);
620
621         return g_supplicant_interface_disconnect(wifi->interface,
622                                                 disconnect_callback, wifi);
623 }
624
625 static struct connman_network_driver network_driver = {
626         .name           = "wifi",
627         .type           = CONNMAN_NETWORK_TYPE_WIFI,
628         .priority       = CONNMAN_NETWORK_PRIORITY_LOW,
629         .probe          = network_probe,
630         .remove         = network_remove,
631         .connect        = network_connect,
632         .disconnect     = network_disconnect,
633 };
634
635 static int wifi_init(void)
636 {
637         int err;
638
639         err = connman_network_driver_register(&network_driver);
640         if (err < 0)
641                 return err;
642
643         err = g_supplicant_register(&callbacks);
644         if (err < 0) {
645                 connman_network_driver_unregister(&network_driver);
646                 return err;
647         }
648
649         return 0;
650 }
651
652 static void wifi_exit(void)
653 {
654         DBG();
655
656         g_supplicant_unregister(&callbacks);
657
658         connman_network_driver_unregister(&network_driver);
659 }
660
661 CONNMAN_PLUGIN_DEFINE(wifi, "WiFi interface plugin", VERSION,
662                 CONNMAN_PLUGIN_PRIORITY_DEFAULT, wifi_init, wifi_exit)