1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
|
/*
*
* Connection Manager
*
* Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <linux/if_arp.h>
#include <linux/wireless.h>
#include <net/ethernet.h>
#ifndef IFF_LOWER_UP
#define IFF_LOWER_UP 0x10000
#endif
#include <dbus/dbus.h>
#include <glib.h>
#define CONNMAN_API_SUBJECT_TO_CHANGE
#include <connman/plugin.h>
#include <connman/inet.h>
#include <connman/device.h>
#include <connman/rtnl.h>
#include <connman/log.h>
#include <gsupplicant/gsupplicant.h>
#define CLEANUP_TIMEOUT 8 /* in seconds */
#define INACTIVE_TIMEOUT 12 /* in seconds */
struct wifi_data {
char *identifier;
struct connman_device *device;
struct connman_network *network;
GSupplicantInterface *interface;
connman_bool_t connected;
int index;
unsigned flags;
unsigned int watch;
};
static int get_bssid(struct connman_device *device,
unsigned char *bssid, unsigned int *bssid_len)
{
struct iwreq wrq;
char *ifname;
int ifindex;
int fd, err;
ifindex = connman_device_get_index(device);
if (ifindex < 0)
return -EINVAL;
ifname = connman_inet_ifname(ifindex);
if (ifname == NULL)
return -EINVAL;
fd = socket(PF_INET, SOCK_DGRAM, 0);
if (fd < 0) {
g_free(ifname);
return -EINVAL;
}
memset(&wrq, 0, sizeof(wrq));
strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
err = ioctl(fd, SIOCGIWAP, &wrq);
g_free(ifname);
close(fd);
if (err < 0)
return -EIO;
memcpy(bssid, wrq.u.ap_addr.sa_data, ETH_ALEN);
*bssid_len = ETH_ALEN;
return 0;
}
static void wifi_newlink(unsigned flags, unsigned change, void *user_data)
{
struct connman_device *device = user_data;
struct wifi_data *wifi = connman_device_get_data(device);
DBG("index %d flags %d change %d", wifi->index, flags, change);
if (!change)
return;
if ((wifi->flags & IFF_UP) != (flags & IFF_UP)) {
if (flags & IFF_UP)
DBG("interface up");
else
DBG("interface down");
}
if ((wifi->flags & IFF_LOWER_UP) != (flags & IFF_LOWER_UP)) {
if (flags & IFF_LOWER_UP)
DBG("carrier on");
else
DBG("carrier off");
}
wifi->flags = flags;
}
static int wifi_probe(struct connman_device *device)
{
struct wifi_data *wifi;
DBG("device %p", device);
wifi = g_try_new0(struct wifi_data, 1);
if (wifi == NULL)
return -ENOMEM;
wifi->connected = FALSE;
connman_device_set_data(device, wifi);
wifi->device = connman_device_ref(device);
wifi->index = connman_device_get_index(device);
wifi->flags = 0;
wifi->watch = connman_rtnl_add_newlink_watch(wifi->index,
wifi_newlink, device);
return 0;
}
static void wifi_remove(struct connman_device *device)
{
struct wifi_data *wifi = connman_device_get_data(device);
DBG("device %p", device);
connman_device_set_data(device, NULL);
connman_device_unref(wifi->device);
connman_rtnl_remove_watch(wifi->watch);
g_free(wifi->identifier);
g_free(wifi);
}
static void interface_create_callback(int result,
GSupplicantInterface *interface,
void *user_data)
{
struct wifi_data *wifi = user_data;
DBG("result %d ifname %s", result,
g_supplicant_interface_get_ifname(interface));
if (result < 0)
return;
wifi->interface = interface;
g_supplicant_interface_set_data(interface, wifi);
}
static void interface_remove_callback(int result,
GSupplicantInterface *interface,
void *user_data)
{
struct wifi_data *wifi = user_data;
DBG("result %d", result);
if (result < 0)
return;
wifi->interface = NULL;
}
static int wifi_enable(struct connman_device *device)
{
struct wifi_data *wifi = connman_device_get_data(device);
const char *interface = connman_device_get_string(device, "Interface");
DBG("device %p %p", device, wifi);
return g_supplicant_interface_create(interface, "nl80211,wext",
interface_create_callback,
wifi);
}
static int wifi_disable(struct connman_device *device)
{
struct wifi_data *wifi = connman_device_get_data(device);
DBG("device %p", device);
wifi->connected = FALSE;
return g_supplicant_interface_remove(wifi->interface,
interface_remove_callback,
wifi);
}
static void scan_callback(int result, GSupplicantInterface *interface,
void *user_data)
{
struct connman_device *device = user_data;
DBG("result %d", result);
if (result < 0) {
connman_device_set_scanning(device, FALSE);
return;
}
connman_device_set_scanning(device, TRUE);
}
static int wifi_scan(struct connman_device *device)
{
struct wifi_data *wifi = connman_device_get_data(device);
DBG("device %p %p", device, wifi->interface);
return g_supplicant_interface_scan(wifi->interface, scan_callback,
device);
}
static struct connman_device_driver wifi_ng_driver = {
.name = "wifi",
.type = CONNMAN_DEVICE_TYPE_WIFI,
.probe = wifi_probe,
.remove = wifi_remove,
.enable = wifi_enable,
.disable = wifi_disable,
.scan = wifi_scan,
};
static void system_ready(void)
{
DBG("");
if (connman_device_driver_register(&wifi_ng_driver) < 0)
connman_error("Failed to register WiFi driver");
}
static void system_killed(void)
{
DBG("");
connman_device_driver_unregister(&wifi_ng_driver);
}
static void interface_added(GSupplicantInterface *interface)
{
const char *ifname = g_supplicant_interface_get_ifname(interface);
const char *driver = g_supplicant_interface_get_driver(interface);
struct wifi_data *wifi;
wifi = (struct wifi_data *)g_supplicant_interface_get_data(interface);
DBG("ifname %s driver %s wifi %p", ifname, driver, wifi);
if (wifi == NULL || wifi->device == NULL) {
connman_error("Wrong wifi pointer");
return;
}
connman_device_set_powered(wifi->device, TRUE);
wifi_scan(wifi->device);
}
static void interface_state(GSupplicantInterface *interface)
{
struct connman_network *network;
struct connman_device *device;
struct wifi_data *wifi;
GSupplicantState state = g_supplicant_interface_get_state(interface);
unsigned char bssid[ETH_ALEN];
unsigned int bssid_len;
wifi = (struct wifi_data *) g_supplicant_interface_get_data(interface);
DBG("wifi %p interface state %d", wifi, state);
if (wifi == NULL)
return;
network = wifi->network;
device = wifi->device;
if (device == NULL || network == NULL)
return;
switch (state) {
case G_SUPPLICANT_STATE_SCANNING:
connman_device_set_scanning(device, TRUE);
break;
case G_SUPPLICANT_STATE_AUTHENTICATING:
case G_SUPPLICANT_STATE_ASSOCIATING:
connman_network_set_associating(network, TRUE);
break;
case G_SUPPLICANT_STATE_COMPLETED:
/* reset scan trigger and schedule background scan */
connman_device_schedule_scan(device);
if (get_bssid(device, bssid, &bssid_len) == 0)
connman_network_set_address(network,
bssid, bssid_len);
connman_network_set_connected(network, TRUE);
break;
case G_SUPPLICANT_STATE_DISCONNECTED:
connman_network_set_connected(network, FALSE);
break;
case G_SUPPLICANT_STATE_INACTIVE:
break;
case G_SUPPLICANT_STATE_UNKNOWN:
case G_SUPPLICANT_STATE_ASSOCIATED:
case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
break;
}
DBG("DONE");
}
static void interface_removed(GSupplicantInterface *interface)
{
const char *ifname = g_supplicant_interface_get_ifname(interface);
struct wifi_data *wifi;
DBG("ifname %s", ifname);
wifi = (struct wifi_data *)g_supplicant_interface_get_data(interface);
if (wifi == NULL || wifi->device == NULL) {
connman_error("Wrong wifi pointer");
return;
}
connman_device_set_powered(wifi->device, FALSE);
}
static void scan_started(GSupplicantInterface *interface)
{
struct wifi_data *wifi;
DBG("");
wifi = (struct wifi_data *)g_supplicant_interface_get_data(interface);
if (wifi == NULL)
return;
if (wifi->device)
connman_device_set_scanning(wifi->device, TRUE);
}
static void scan_finished(GSupplicantInterface *interface)
{
struct wifi_data *wifi;
DBG("");
wifi = (struct wifi_data *)g_supplicant_interface_get_data(interface);
if (wifi == NULL)
return;
}
static unsigned char calculate_strength(GSupplicantNetwork *supplicant_network)
{
unsigned char strength;
strength = 120 + g_supplicant_network_get_signal(supplicant_network);
if (strength > 100)
strength = 100;
return strength;
}
static void network_added(GSupplicantNetwork *supplicant_network)
{
struct connman_network *network;
GSupplicantInterface *interface;
struct wifi_data *wifi;
const char *name, *path, *identifier, *mode, *security, *group;
unsigned char *ssid;
unsigned int ssid_len;
DBG("");
interface = g_supplicant_network_get_interface(supplicant_network);
wifi = (struct wifi_data *) g_supplicant_interface_get_data(interface);
name = g_supplicant_network_get_name(supplicant_network);
path = g_supplicant_network_get_path(supplicant_network);
identifier = g_supplicant_network_get_identifier(supplicant_network);
mode = g_supplicant_network_get_mode(supplicant_network);
security = g_supplicant_network_get_security(supplicant_network);
group = g_supplicant_network_get_identifier(supplicant_network);
if (wifi == NULL)
return;
ssid = (unsigned char *)g_supplicant_network_get_ssid(supplicant_network, &ssid_len);
network = connman_device_get_network(wifi->device, path);
if (network == NULL) {
network = connman_network_create(identifier,
CONNMAN_NETWORK_TYPE_WIFI);
if (network == NULL)
return;
connman_network_set_index(network, wifi->index);
connman_network_set_protocol(network,
CONNMAN_NETWORK_PROTOCOL_IP);
if (connman_device_add_network(wifi->device, network) < 0) {
connman_network_unref(network);
return;
}
}
if (name != NULL && name[0] != '\0')
connman_network_set_name(network, name);
connman_network_set_blob(network, "WiFi.SSID",
ssid, ssid_len);
connman_network_set_string(network, "WiFi.Mode", mode);
connman_network_set_string(network, "WiFi.Security", security);
connman_network_set_strength(network,
calculate_strength(supplicant_network));
connman_network_set_available(network, TRUE);
if (ssid != NULL)
connman_network_set_group(network, group);
}
static void network_removed(GSupplicantNetwork *network)
{
const char *name = g_supplicant_network_get_name(network);
DBG("* name %s", name);
}
static const GSupplicantCallbacks callbacks = {
.system_ready = system_ready,
.system_killed = system_killed,
.interface_added = interface_added,
.interface_state = interface_state,
.interface_removed = interface_removed,
.scan_started = scan_started,
.scan_finished = scan_finished,
.network_added = network_added,
.network_removed = network_removed,
};
static int network_probe(struct connman_network *network)
{
DBG("network %p", network);
return 0;
}
static void network_remove(struct connman_network *network)
{
DBG("network %p", network);
}
static void connect_callback(int result, GSupplicantInterface *interface,
void *user_data)
{
connman_error("%s", __func__);
}
static void disconnect_callback(int result, GSupplicantInterface *interface,
void *user_data)
{
struct wifi_data *wifi = user_data;
if (result < 0) {
connman_error("%s", __func__);
return;
}
connman_network_unref(wifi->network);
wifi->network = NULL;
}
static GSupplicantSecurity network_security(const char *security)
{
if (g_str_equal(security, "none") == TRUE)
return G_SUPPLICANT_SECURITY_NONE;
else if (g_str_equal(security, "wep") == TRUE)
return G_SUPPLICANT_SECURITY_WEP;
else if (g_str_equal(security, "psk") == TRUE)
return G_SUPPLICANT_SECURITY_PSK;
else if (g_str_equal(security, "wpa") == TRUE)
return G_SUPPLICANT_SECURITY_PSK;
else if (g_str_equal(security, "rsn") == TRUE)
return G_SUPPLICANT_SECURITY_PSK;
else if (g_str_equal(security, "ieee8021x") == TRUE)
return G_SUPPLICANT_SECURITY_IEEE8021X;
return G_SUPPLICANT_SECURITY_UNKNOWN;
}
static int network_connect(struct connman_network *network)
{
struct connman_device *device = connman_network_get_device(network);
struct wifi_data *wifi;
GSupplicantInterface *interface;
GSupplicantSSID ssid;
const char *security;
DBG("network %p", network);
if (device == NULL)
return -ENODEV;
wifi = connman_device_get_data(device);
if (wifi == NULL)
return -ENODEV;
interface = wifi->interface;
memset(&ssid, 0, sizeof(ssid));
ssid.ssid = connman_network_get_blob(network, "WiFi.SSID",
&ssid.ssid_len);
security = connman_network_get_string(network, "WiFi.Security");
ssid.security = network_security(security);
wifi->network = connman_network_ref(network);
return g_supplicant_interface_connect(interface, &ssid,
connect_callback, NULL);
}
static int network_disconnect(struct connman_network *network)
{
struct connman_device *device = connman_network_get_device(network);
struct wifi_data *wifi;
DBG("network %p", network);
wifi = connman_device_get_data(device);
if (wifi == NULL || wifi->interface == NULL)
return -ENODEV;
return g_supplicant_interface_disconnect(wifi->interface,
disconnect_callback, wifi);
}
static struct connman_network_driver network_driver = {
.name = "wifi",
.type = CONNMAN_NETWORK_TYPE_WIFI,
.probe = network_probe,
.remove = network_remove,
.connect = network_connect,
.disconnect = network_disconnect,
};
static int wifi_init(void)
{
int err;
err = connman_network_driver_register(&network_driver);
if (err < 0)
return err;
err = g_supplicant_register(&callbacks);
if (err < 0) {
connman_network_driver_unregister(&network_driver);
return err;
}
return 0;
}
static void wifi_exit(void)
{
DBG();
g_supplicant_unregister(&callbacks);
connman_network_driver_unregister(&network_driver);
}
CONNMAN_PLUGIN_DEFINE(wifi, "WiFi interface plugin", VERSION,
CONNMAN_PLUGIN_PRIORITY_DEFAULT, wifi_init, wifi_exit)
|