/
wifi.c
3631 lines (2850 loc) · 84.2 KB
/
wifi.c
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
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
*
* Connection Manager
*
* Copyright (C) 2007-2014 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 <stdlib.h>
#include <errno.h>
#include <stdio.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/technology.h>
#include <connman/service.h>
#include <connman/peer.h>
#include <connman/log.h>
#include <connman/option.h>
#include <connman/storage.h>
#include <include/setting.h>
#include <connman/provision.h>
#include <connman/utsname.h>
#include <connman/machine.h>
#include <gsupplicant/gsupplicant.h>
#include <gsupplicant/dbus.h>
#define CLEANUP_TIMEOUT 8 /* in seconds */
#define INACTIVE_TIMEOUT 12 /* in seconds */
#define SCAN_FAIL_TIMEOUT 60 /* in seconds */
#define FAVORITE_MAXIMUM_RETRIES 2
#define BGSCAN_DEFAULT "simple:30:-65:300"
#define AUTOSCAN_DEFAULT "exponential:3:300"
#define P2P_FIND_TIMEOUT 30
#define P2P_CONNECTION_TIMEOUT 100
#define P2P_LISTEN_PERIOD 500
#define P2P_LISTEN_INTERVAL 2000
static struct connman_technology *wifi_technology = NULL;
static struct connman_technology *p2p_technology = NULL;
enum wifi_ap_capability{
WIFI_AP_UNKNOWN = 0,
WIFI_AP_SUPPORTED = 1,
WIFI_AP_NOT_SUPPORTED = 2,
};
struct hidden_params {
char ssid[32];
unsigned int ssid_len;
char *identity;
char *anonymous_identity;
char *subject_match;
char *altsubject_match;
char *domain_suffix_match;
char *domain_match;
char *passphrase;
char *security;
GSupplicantScanParams *scan_params;
gpointer user_data;
};
/**
* Used for autoscan "emulation".
* Should be removed when wpa_s autoscan support will be by default.
*/
struct autoscan_params {
int base;
int limit;
int interval;
unsigned int timeout;
};
#define SIGNAL_POLL_HISTORY_SIZE (10) /* Number of history entries */
#define SIGNAL_POLL_HISTORY_SECS (10) /* Max history depth in seconds */
struct signal_poll_record {
unsigned char strength;
time_t time;
};
struct signal_poll_data {
bool in_progress; /* D-Bus call in progress */
int first; /* Oldest position in the history (inclusive) */
int last; /* Latest position in the history (inclusive) */
struct signal_poll_record history[SIGNAL_POLL_HISTORY_SIZE];
};
struct wifi_tethering_info {
struct wifi_data *wifi;
struct connman_technology *technology;
char *ifname;
GSupplicantSSID *ssid;
};
struct wifi_data {
char *identifier;
struct connman_device *device;
struct connman_network *network;
struct connman_network *pending_network;
GSList *networks;
GSupplicantInterface *interface;
GSupplicantState state;
bool connected;
bool disconnecting;
bool tethering;
enum wifi_ap_capability ap_supported;
bool bridged;
bool interface_ready;
const char *bridge;
int index;
unsigned flags;
unsigned int watch;
int retries;
struct hidden_params *hidden;
bool postpone_hidden;
struct signal_poll_data signal_poll;
struct wifi_tethering_info *tethering_param;
unsigned int scan_fail_timeout;
/**
* autoscan "emulation".
*/
struct autoscan_params *autoscan;
GSupplicantScanParams *scan_params;
unsigned int p2p_find_timeout;
unsigned int p2p_connection_timeout;
struct connman_peer *pending_peer;
GSList *peers;
bool p2p_connecting;
bool p2p_device;
int servicing;
};
static GSList *tethering_info_list = NULL;
static GList *iface_list = NULL;
static GList *pending_wifi_device = NULL;
static GList *p2p_iface_list = NULL;
bool wfd_service_registered = false;
static GString *supplicant_debug_str;
static void start_autoscan(struct connman_device *device);
static int tech_set_tethering(struct connman_technology *technology,
const char *identifier, const char *passphrase,
const char *bridge, bool enabled);
static int p2p_tech_probe(struct connman_technology *technology)
{
p2p_technology = technology;
return 0;
}
static void p2p_tech_remove(struct connman_technology *technology)
{
p2p_technology = NULL;
}
static struct connman_technology_driver p2p_tech_driver = {
.name = "p2p",
.type = CONNMAN_SERVICE_TYPE_P2P,
.probe = p2p_tech_probe,
.remove = p2p_tech_remove,
};
static bool is_valid_wifi_data(struct wifi_data *wifi)
{
return wifi && (g_list_find(iface_list, wifi) ||
g_list_find(p2p_iface_list, wifi));
}
static bool is_p2p_connecting(void)
{
GList *list;
for (list = iface_list; list; list = list->next) {
struct wifi_data *wifi = list->data;
if (wifi->p2p_connecting)
return true;
}
return false;
}
static void add_pending_wifi_device(struct wifi_data *wifi)
{
if (g_list_find(pending_wifi_device, wifi))
return;
pending_wifi_device = g_list_append(pending_wifi_device, wifi);
}
static struct wifi_data *get_pending_wifi_data(const char *ifname)
{
GList *list;
for (list = pending_wifi_device; list; list = list->next) {
struct wifi_data *wifi;
const char *dev_name;
wifi = list->data;
if (!wifi || !wifi->device)
continue;
dev_name = connman_device_get_string(wifi->device, "Interface");
if (!g_strcmp0(ifname, dev_name)) {
pending_wifi_device = g_list_delete_link(
pending_wifi_device, list);
return wifi;
}
}
return NULL;
}
static void remove_pending_wifi_device(struct wifi_data *wifi)
{
GList *link;
link = g_list_find(pending_wifi_device, wifi);
if (!link)
return;
pending_wifi_device = g_list_delete_link(pending_wifi_device, link);
}
static void peer_cancel_timeout(struct wifi_data *wifi)
{
if (wifi->p2p_connection_timeout > 0)
g_source_remove(wifi->p2p_connection_timeout);
wifi->p2p_connection_timeout = 0;
wifi->p2p_connecting = false;
if (wifi->pending_peer) {
connman_peer_unref(wifi->pending_peer);
wifi->pending_peer = NULL;
}
}
static gboolean peer_connect_timeout(gpointer data)
{
struct wifi_data *wifi = data;
DBG("");
if (wifi->p2p_connecting) {
enum connman_peer_state state = CONNMAN_PEER_STATE_FAILURE;
GSupplicantPeer *gs_peer =
g_supplicant_interface_peer_lookup(wifi->interface,
connman_peer_get_identifier(wifi->pending_peer));
if (g_supplicant_peer_has_requested_connection(gs_peer))
state = CONNMAN_PEER_STATE_IDLE;
connman_peer_set_state(wifi->pending_peer, state);
}
peer_cancel_timeout(wifi);
return FALSE;
}
static void peer_connect_callback(int result, GSupplicantInterface *interface,
void *user_data)
{
struct wifi_data *wifi = user_data;
struct connman_peer *peer = wifi->pending_peer;
DBG("peer %p - %d", peer, result);
if (!peer)
return;
if (result < 0) {
peer_connect_timeout(wifi);
return;
}
connman_peer_set_state(peer, CONNMAN_PEER_STATE_ASSOCIATION);
wifi->p2p_connection_timeout = g_timeout_add_seconds(
P2P_CONNECTION_TIMEOUT,
peer_connect_timeout, wifi);
}
static int peer_connect(struct connman_peer *peer,
enum connman_peer_wps_method wps_method,
const char *wps_pin)
{
struct connman_device *device = connman_peer_get_device(peer);
GSupplicantPeerParams *peer_params;
GSupplicantPeer *gs_peer;
struct wifi_data *wifi;
bool pbc, pin;
int ret;
DBG("peer %p", peer);
if (!device)
return -ENODEV;
wifi = connman_device_get_data(device);
if (!wifi || !wifi->interface)
return -ENODEV;
if (wifi->p2p_connecting)
return -EBUSY;
gs_peer = g_supplicant_interface_peer_lookup(wifi->interface,
connman_peer_get_identifier(peer));
if (!gs_peer)
return -EINVAL;
pbc = g_supplicant_peer_is_wps_pbc(gs_peer);
pin = g_supplicant_peer_is_wps_pin(gs_peer);
switch (wps_method) {
case CONNMAN_PEER_WPS_UNKNOWN:
if ((pbc && pin) || pin)
return -ENOKEY;
break;
case CONNMAN_PEER_WPS_PBC:
if (!pbc)
return -EINVAL;
wps_pin = NULL;
break;
case CONNMAN_PEER_WPS_PIN:
if (!pin || !wps_pin)
return -EINVAL;
break;
}
peer_params = g_try_malloc0(sizeof(GSupplicantPeerParams));
if (!peer_params)
return -ENOMEM;
peer_params->path = g_strdup(g_supplicant_peer_get_path(gs_peer));
if (wps_pin)
peer_params->wps_pin = g_strdup(wps_pin);
peer_params->master = connman_peer_service_is_master();
ret = g_supplicant_interface_p2p_connect(wifi->interface, peer_params,
peer_connect_callback, wifi);
if (ret == -EINPROGRESS) {
wifi->pending_peer = connman_peer_ref(peer);
wifi->p2p_connecting = true;
} else if (ret < 0) {
g_free(peer_params->path);
g_free(peer_params->wps_pin);
g_free(peer_params);
}
return ret;
}
static int peer_disconnect(struct connman_peer *peer)
{
struct connman_device *device = connman_peer_get_device(peer);
GSupplicantPeerParams peer_params = {};
GSupplicantPeer *gs_peer;
struct wifi_data *wifi;
int ret;
DBG("peer %p", peer);
if (!device)
return -ENODEV;
wifi = connman_device_get_data(device);
if (!wifi)
return -ENODEV;
gs_peer = g_supplicant_interface_peer_lookup(wifi->interface,
connman_peer_get_identifier(peer));
if (!gs_peer)
return -EINVAL;
peer_params.path = g_strdup(g_supplicant_peer_get_path(gs_peer));
ret = g_supplicant_interface_p2p_disconnect(wifi->interface,
&peer_params);
g_free(peer_params.path);
if (ret == -EINPROGRESS)
peer_cancel_timeout(wifi);
return ret;
}
struct peer_service_registration {
peer_service_registration_cb_t callback;
void *user_data;
};
static bool is_service_wfd(const unsigned char *specs, int length)
{
if (length < 9 || specs[0] != 0 || specs[1] != 0 || specs[2] != 6)
return false;
return true;
}
static void apply_p2p_listen_on_iface(gpointer data, gpointer user_data)
{
struct wifi_data *wifi = data;
if (!wifi->interface ||
!g_supplicant_interface_has_p2p(wifi->interface))
return;
if (!wifi->servicing) {
g_supplicant_interface_p2p_listen(wifi->interface,
P2P_LISTEN_PERIOD, P2P_LISTEN_INTERVAL);
}
wifi->servicing++;
}
static void register_wfd_service_cb(int result,
GSupplicantInterface *iface, void *user_data)
{
struct peer_service_registration *reg_data = user_data;
DBG("");
if (result == 0)
g_list_foreach(iface_list, apply_p2p_listen_on_iface, NULL);
if (reg_data && reg_data->callback) {
reg_data->callback(result, reg_data->user_data);
g_free(reg_data);
}
}
static GSupplicantP2PServiceParams *fill_in_peer_service_params(
const unsigned char *spec,
int spec_length, const unsigned char *query,
int query_length, int version)
{
GSupplicantP2PServiceParams *params;
params = g_try_malloc0(sizeof(GSupplicantP2PServiceParams));
if (!params)
return NULL;
if (version > 0) {
params->version = version;
params->service = g_memdup(spec, spec_length);
} else if (query_length > 0 && spec_length > 0) {
params->query = g_memdup(query, query_length);
params->query_length = query_length;
params->response = g_memdup(spec, spec_length);
params->response_length = spec_length;
} else {
params->wfd_ies = g_memdup(spec, spec_length);
params->wfd_ies_length = spec_length;
}
return params;
}
static void free_peer_service_params(GSupplicantP2PServiceParams *params)
{
if (!params)
return;
g_free(params->service);
g_free(params->query);
g_free(params->response);
g_free(params->wfd_ies);
g_free(params);
}
static int peer_register_wfd_service(const unsigned char *specification,
int specification_length,
peer_service_registration_cb_t callback,
void *user_data)
{
struct peer_service_registration *reg_data = NULL;
static GSupplicantP2PServiceParams *params;
int ret;
DBG("");
if (wfd_service_registered)
return -EBUSY;
params = fill_in_peer_service_params(specification,
specification_length, NULL, 0, 0);
if (!params)
return -ENOMEM;
reg_data = g_try_malloc0(sizeof(*reg_data));
if (!reg_data) {
ret = -ENOMEM;
goto error;
}
reg_data->callback = callback;
reg_data->user_data = user_data;
ret = g_supplicant_set_widi_ies(params,
register_wfd_service_cb, reg_data);
if (ret < 0 && ret != -EINPROGRESS)
goto error;
wfd_service_registered = true;
return ret;
error:
free_peer_service_params(params);
g_free(reg_data);
return ret;
}
static void register_peer_service_cb(int result,
GSupplicantInterface *iface, void *user_data)
{
struct wifi_data *wifi = g_supplicant_interface_get_data(iface);
struct peer_service_registration *reg_data = user_data;
DBG("");
if (result == 0)
apply_p2p_listen_on_iface(wifi, NULL);
if (reg_data->callback)
reg_data->callback(result, reg_data->user_data);
g_free(reg_data);
}
static int peer_register_service(const unsigned char *specification,
int specification_length,
const unsigned char *query,
int query_length, int version,
peer_service_registration_cb_t callback,
void *user_data)
{
struct peer_service_registration *reg_data;
GSupplicantP2PServiceParams *params;
bool found = false;
int ret, ret_f;
GList *list;
DBG("");
if (specification && !version && !query &&
is_service_wfd(specification, specification_length)) {
return peer_register_wfd_service(specification,
specification_length, callback, user_data);
}
reg_data = g_try_malloc0(sizeof(*reg_data));
if (!reg_data)
return -ENOMEM;
reg_data->callback = callback;
reg_data->user_data = user_data;
ret_f = -EOPNOTSUPP;
for (list = iface_list; list; list = list->next) {
struct wifi_data *wifi = list->data;
GSupplicantInterface *iface = wifi->interface;
if (!g_supplicant_interface_has_p2p(iface))
continue;
params = fill_in_peer_service_params(specification,
specification_length, query,
query_length, version);
if (!params) {
ret = -ENOMEM;
continue;
}
if (!found) {
ret_f = g_supplicant_interface_p2p_add_service(iface,
register_peer_service_cb, params, reg_data);
if (ret_f == 0 || ret_f == -EINPROGRESS)
found = true;
ret = ret_f;
} else
ret = g_supplicant_interface_p2p_add_service(iface,
register_peer_service_cb, params, NULL);
if (ret != 0 && ret != -EINPROGRESS)
free_peer_service_params(params);
}
if (ret_f != 0 && ret_f != -EINPROGRESS)
g_free(reg_data);
return ret_f;
}
static int peer_unregister_wfd_service(void)
{
GSupplicantP2PServiceParams *params;
GList *list;
if (!wfd_service_registered)
return -EALREADY;
params = fill_in_peer_service_params(NULL, 0, NULL, 0, 0);
if (!params)
return -ENOMEM;
wfd_service_registered = false;
g_supplicant_set_widi_ies(params, NULL, NULL);
for (list = iface_list; list; list = list->next) {
struct wifi_data *wifi = list->data;
if (!g_supplicant_interface_has_p2p(wifi->interface))
continue;
wifi->servicing--;
if (!wifi->servicing || wifi->servicing < 0) {
g_supplicant_interface_p2p_listen(wifi->interface,
0, 0);
wifi->servicing = 0;
}
}
return 0;
}
static int peer_unregister_service(const unsigned char *specification,
int specification_length,
const unsigned char *query,
int query_length, int version)
{
GSupplicantP2PServiceParams *params;
bool wfd = false;
GList *list;
int ret;
if (specification && !version && !query &&
is_service_wfd(specification, specification_length)) {
ret = peer_unregister_wfd_service();
if (ret != 0 && ret != -EINPROGRESS)
return ret;
wfd = true;
}
for (list = iface_list; list; list = list->next) {
struct wifi_data *wifi = list->data;
GSupplicantInterface *iface = wifi->interface;
if (wfd)
goto stop_listening;
if (!g_supplicant_interface_has_p2p(iface))
continue;
params = fill_in_peer_service_params(specification,
specification_length, query,
query_length, version);
if (!params) {
ret = -ENOMEM;
continue;
}
ret = g_supplicant_interface_p2p_del_service(iface, params);
if (ret != 0 && ret != -EINPROGRESS)
free_peer_service_params(params);
stop_listening:
wifi->servicing--;
if (!wifi->servicing || wifi->servicing < 0) {
g_supplicant_interface_p2p_listen(iface, 0, 0);
wifi->servicing = 0;
}
}
return 0;
}
static struct connman_peer_driver peer_driver = {
.connect = peer_connect,
.disconnect = peer_disconnect,
.register_service = peer_register_service,
.unregister_service = peer_unregister_service,
};
static void handle_tethering(struct wifi_data *wifi)
{
if (!wifi->tethering)
return;
if (!wifi->bridge)
return;
if (wifi->bridged)
return;
DBG("index %d bridge %s", wifi->index, wifi->bridge);
if (connman_inet_add_to_bridge(wifi->index, wifi->bridge) < 0)
return;
wifi->bridged = true;
}
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);
if (!wifi)
return;
//DBG("index %d flags %d change %d", wifi->index, flags, change);
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");
handle_tethering(wifi);
} 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)
return -ENOMEM;
wifi->state = G_SUPPLICANT_STATE_INACTIVE;
wifi->signal_poll.first = wifi->signal_poll.last = -1;
wifi->ap_supported = WIFI_AP_UNKNOWN;
wifi->tethering_param = NULL;
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);
if (is_p2p_connecting())
add_pending_wifi_device(wifi);
else
iface_list = g_list_append(iface_list, wifi);
return 0;
}
static void remove_networks(struct connman_device *device,
struct wifi_data *wifi)
{
GSList *list;
for (list = wifi->networks; list; list = list->next) {
struct connman_network *network = list->data;
connman_device_remove_network(device, network);
connman_network_unref(network);
}
g_slist_free(wifi->networks);
wifi->networks = NULL;
}
static void remove_peers(struct wifi_data *wifi)
{
GSList *list;
for (list = wifi->peers; list; list = list->next) {
struct connman_peer *peer = list->data;
connman_peer_unregister(peer);
connman_peer_unref(peer);
}
g_slist_free(wifi->peers);
wifi->peers = NULL;
}
static void reset_autoscan(struct connman_device *device)
{
struct wifi_data *wifi = connman_device_get_data(device);
struct autoscan_params *autoscan;
DBG("");
if (!wifi || !wifi->autoscan)
return;
autoscan = wifi->autoscan;
if (autoscan->timeout == 0 && autoscan->interval == 0)
return;
g_source_remove(autoscan->timeout);
autoscan->timeout = 0;
autoscan->interval = 0;
connman_device_unref(device);
}
static gboolean scan_fail_timeout(gpointer data)
{
struct connman_device *device = data;
struct wifi_data *wifi = connman_device_get_data(device);
DBG("");
if (!wifi)
return FALSE;
connman_device_set_scanning(device, CONNMAN_SERVICE_TYPE_WIFI, false);
wifi->scan_fail_timeout = 0;
return FALSE;
}
static void stop_autoscan(struct connman_device *device)
{
struct wifi_data *wifi = connman_device_get_data(device);
if (!wifi || !wifi->autoscan)
return;
reset_autoscan(device);
connman_device_set_scanning(device, CONNMAN_SERVICE_TYPE_WIFI, false);
if (wifi->scan_fail_timeout) {
g_source_remove(wifi->scan_fail_timeout);
wifi->scan_fail_timeout = 0;
}
}
static void check_p2p_technology(void)
{
bool p2p_exists = false;
GList *list;
for (list = iface_list; list; list = list->next) {
struct wifi_data *w = list->data;
if (w->interface &&
g_supplicant_interface_has_p2p(w->interface))
p2p_exists = true;
}
if (!p2p_exists) {
connman_technology_driver_unregister(&p2p_tech_driver);
connman_peer_driver_unregister(&peer_driver);
}
}
static void wifi_remove(struct connman_device *device)
{
struct wifi_data *wifi = connman_device_get_data(device);
GSList *t;
DBG("device %p wifi %p", device, wifi);
if (!wifi)
return;
stop_autoscan(device);
if (wifi->p2p_device)
p2p_iface_list = g_list_remove(p2p_iface_list, wifi);
else
iface_list = g_list_remove(iface_list, wifi);
check_p2p_technology();
remove_pending_wifi_device(wifi);
if (wifi->p2p_find_timeout) {
g_source_remove(wifi->p2p_find_timeout);
connman_device_unref(wifi->device);
}
if (wifi->p2p_connection_timeout)
g_source_remove(wifi->p2p_connection_timeout);
if (wifi->scan_fail_timeout) {
g_source_remove(wifi->scan_fail_timeout);
}
if (wifi->pending_network)
connman_network_unref(wifi->pending_network);
remove_networks(device, wifi);
remove_peers(wifi);
connman_device_set_powered(device, false);
connman_device_set_data(device, NULL);
connman_device_unref(wifi->device);
connman_rtnl_remove_watch(wifi->watch);
g_supplicant_interface_set_data(wifi->interface, NULL);
g_supplicant_interface_cancel(wifi->interface);
if (wifi->scan_params)
g_supplicant_free_scan_params(wifi->scan_params);
g_free(wifi->autoscan);
g_free(wifi->identifier);
g_free(wifi);
for (t = tethering_info_list; t; t = t->next) {
struct wifi_tethering_info *info = t->data;
if (info->wifi == wifi)
info->wifi = NULL;
}
}
static bool is_duplicate(GSList *list, gchar *ssid, int ssid_len)
{
GSList *iter;
for (iter = list; iter; iter = g_slist_next(iter)) {
struct scan_ssid *scan_ssid = iter->data;
if (ssid_len == scan_ssid->ssid_len &&
memcmp(ssid, scan_ssid->ssid, ssid_len) == 0)
return true;
}
return false;
}
static int add_scan_param(gchar *hex_ssid, char *raw_ssid, int ssid_len,
int freq, GSupplicantScanParams *scan_data,
int driver_max_scan_ssids, char *ssid_name)
{
unsigned int i;