Move current wifi plugin to legacy
[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         .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 = (struct wifi_data *)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 = (struct wifi_data *) 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_connected(network, FALSE);
338                 break;
339
340         case G_SUPPLICANT_STATE_INACTIVE:
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 = (struct wifi_data *)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 = (struct wifi_data *)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 = (struct wifi_data *)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, *path, *identifier, *mode, *security, *group;
414         unsigned char *ssid;
415         unsigned int ssid_len;
416
417         DBG("");
418
419         interface = g_supplicant_network_get_interface(supplicant_network);
420         wifi = (struct wifi_data *) g_supplicant_interface_get_data(interface);
421         name = g_supplicant_network_get_name(supplicant_network);
422         path = g_supplicant_network_get_path(supplicant_network);
423         identifier = g_supplicant_network_get_identifier(supplicant_network);
424         mode = g_supplicant_network_get_mode(supplicant_network);
425         security = g_supplicant_network_get_security(supplicant_network);
426         group = g_supplicant_network_get_identifier(supplicant_network);
427
428         if (wifi == NULL)
429                 return;
430
431         ssid = (unsigned char *)g_supplicant_network_get_ssid(supplicant_network, &ssid_len);
432
433         network = connman_device_get_network(wifi->device, path);
434
435         if (network == NULL) {
436                 network = connman_network_create(identifier,
437                                                 CONNMAN_NETWORK_TYPE_WIFI);
438                 if (network == NULL)
439                         return;
440
441                 connman_network_set_index(network, wifi->index);
442
443                 connman_network_set_protocol(network,
444                                                 CONNMAN_NETWORK_PROTOCOL_IP);
445
446                 if (connman_device_add_network(wifi->device, network) < 0) {
447                         connman_network_unref(network);
448                         return;
449                 }
450         }
451
452         if (name != NULL && name[0] != '\0')
453                 connman_network_set_name(network, name);
454
455         connman_network_set_blob(network, "WiFi.SSID",
456                                                 ssid, ssid_len);
457         connman_network_set_string(network, "WiFi.Mode", mode);
458         connman_network_set_string(network, "WiFi.Security", security);
459         connman_network_set_strength(network,
460                                 calculate_strength(supplicant_network));
461
462         connman_network_set_available(network, TRUE);
463
464         if (ssid != NULL)
465                 connman_network_set_group(network, group);
466 }
467
468 static void network_removed(GSupplicantNetwork *network)
469 {
470         const char *name = g_supplicant_network_get_name(network);
471
472         DBG("* name %s", name);
473 }
474
475 static const GSupplicantCallbacks callbacks = {
476         .system_ready           = system_ready,
477         .system_killed          = system_killed,
478         .interface_added        = interface_added,
479         .interface_state        = interface_state,
480         .interface_removed      = interface_removed,
481         .scan_started           = scan_started,
482         .scan_finished          = scan_finished,
483         .network_added          = network_added,
484         .network_removed        = network_removed,
485 };
486
487
488 static int network_probe(struct connman_network *network)
489 {
490         DBG("network %p", network);
491
492         return 0;
493 }
494
495 static void network_remove(struct connman_network *network)
496 {
497         DBG("network %p", network);
498 }
499
500 static void connect_callback(int result, GSupplicantInterface *interface,
501                                                         void *user_data)
502 {
503         connman_error("%s", __func__);
504 }
505
506 static void disconnect_callback(int result, GSupplicantInterface *interface,
507                                                         void *user_data)
508 {
509         struct wifi_data *wifi = user_data;
510
511         if (result < 0) {
512                 connman_error("%s", __func__);
513                 return;
514         }
515
516         connman_network_unref(wifi->network);
517
518         wifi->network = NULL;
519 }
520
521
522 static GSupplicantSecurity network_security(const char *security)
523 {
524         if (g_str_equal(security, "none") == TRUE)
525                 return G_SUPPLICANT_SECURITY_NONE;
526         else if (g_str_equal(security, "wep") == TRUE)
527                 return G_SUPPLICANT_SECURITY_WEP;
528         else if (g_str_equal(security, "psk") == TRUE)
529                 return G_SUPPLICANT_SECURITY_PSK;
530         else if (g_str_equal(security, "wpa") == TRUE)
531                 return G_SUPPLICANT_SECURITY_PSK;
532         else if (g_str_equal(security, "rsn") == TRUE)
533                 return G_SUPPLICANT_SECURITY_PSK;
534         else if (g_str_equal(security, "ieee8021x") == TRUE)
535                 return G_SUPPLICANT_SECURITY_IEEE8021X;
536
537         return G_SUPPLICANT_SECURITY_UNKNOWN;
538 }
539
540 static int network_connect(struct connman_network *network)
541 {
542         struct connman_device *device = connman_network_get_device(network);
543         struct wifi_data *wifi;
544         GSupplicantInterface *interface;
545         GSupplicantSSID ssid;
546         const char *security;
547
548         DBG("network %p", network);
549
550         if (device == NULL)
551                 return -ENODEV;
552
553         wifi = connman_device_get_data(device);
554         if (wifi == NULL)
555                 return -ENODEV;
556
557         interface = wifi->interface;
558
559         memset(&ssid, 0, sizeof(ssid));
560         ssid.ssid = connman_network_get_blob(network, "WiFi.SSID",
561                                                 &ssid.ssid_len);
562         security = connman_network_get_string(network, "WiFi.Security");
563         ssid.security = network_security(security);
564
565         wifi->network = connman_network_ref(network);
566
567         return g_supplicant_interface_connect(interface, &ssid,
568                                                 connect_callback, NULL);
569 }
570
571 static int network_disconnect(struct connman_network *network)
572 {
573         struct connman_device *device = connman_network_get_device(network);
574         struct wifi_data *wifi;
575
576         DBG("network %p", network);
577
578         wifi = connman_device_get_data(device);
579         if (wifi == NULL || wifi->interface == NULL)
580                 return -ENODEV;
581
582         return g_supplicant_interface_disconnect(wifi->interface,
583                                                 disconnect_callback, wifi);
584 }
585
586 static struct connman_network_driver network_driver = {
587         .name           = "wifi",
588         .type           = CONNMAN_NETWORK_TYPE_WIFI,
589         .probe          = network_probe,
590         .remove         = network_remove,
591         .connect        = network_connect,
592         .disconnect     = network_disconnect,
593 };
594
595 static int wifi_init(void)
596 {
597         int err;
598
599         err = connman_network_driver_register(&network_driver);
600         if (err < 0)
601                 return err;
602
603         err = g_supplicant_register(&callbacks);
604         if (err < 0) {
605                 connman_network_driver_unregister(&network_driver);
606                 return err;
607         }
608
609         return 0;
610 }
611
612 static void wifi_exit(void)
613 {
614         DBG();
615
616         g_supplicant_unregister(&callbacks);
617
618         connman_network_driver_unregister(&network_driver);
619 }
620
621 CONNMAN_PLUGIN_DEFINE(wifi, "WiFi interface plugin", VERSION,
622                 CONNMAN_PLUGIN_PRIORITY_DEFAULT, wifi_init, wifi_exit)