/
gsupplicant_interface.c
3257 lines (3064 loc) · 107 KB
/
gsupplicant_interface.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
/*
* Copyright (C) 2015-2018 Jolla Ltd.
* Copyright (C) 2015-2018 Slava Monich <slava.monich@jolla.com>
*
* You may use this file under the terms of BSD license as follows:
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the names of the copyright holders nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "gsupplicant_interface.h"
#include "gsupplicant_network.h"
#include "gsupplicant_bss.h"
#include "gsupplicant.h"
#include "gsupplicant_util_p.h"
#include "gsupplicant_dbus.h"
#include "gsupplicant_log.h"
#include "gsupplicant_error.h"
#include <gutil_strv.h>
#include <gutil_misc.h>
/* Generated headers */
#include "fi.w1.wpa_supplicant1.Interface.h"
#include "fi.w1.wpa_supplicant1.Interface.WPS.h"
/* Constants */
#define WPS_DEFAULT_CONNECT_TIMEOUT_SEC (30)
/* Internal data structures */
typedef union gsupplicant_interface_call_func_union {
GCallback cb;
GSupplicantInterfaceResultFunc fn_void;
GSupplicantInterfaceStringResultFunc fn_string;
GSupplicantInterfaceSignalPollResultFunc fn_signal_poll;
} GSupplicantInterfaceCallFuncUnion;
typedef struct gsupplicant_interface_call GSupplicantInterfaceCall;
typedef
void
(*GSupplicantInterfaceCallFinishFunc)(
GSupplicantInterfaceCall* call,
GAsyncResult* result);
struct gsupplicant_interface_call {
GSupplicantInterface* iface;
GCancellable* cancel;
GSupplicantInterfaceCallFinishFunc finish;
GSupplicantInterfaceCallFuncUnion fn;
GDestroyNotify destroy;
void* data;
};
enum add_network_handler_id {
ADD_NETWORK_VALID_CHANGED,
ADD_NETWORK_ENABLED_CHANGED,
ADD_NETWORK_HANDLER_COUNT
};
typedef struct gsupplicant_interface_add_network_call {
GSupplicantInterface* iface;
GSupplicantNetwork* network;
gulong network_event_id[ADD_NETWORK_HANDLER_COUNT];
GCancellable* cancel;
gulong cancel_id;
GVariant* args;
GHashTable* blobs;
GHashTableIter iter;
gboolean pending;
GSupplicantInterfaceStringResultFunc fn;
GDestroyNotify destroy;
void* data;
guint flags;
char* path;
} GSupplicantInterfaceAddNetworkCall;
enum gsupplicant_wps_proxy_handler_id {
WPS_PROXY_EVENT,
WPS_PROXY_CREDENTIALS,
WPS_PROXY_HANDLER_COUNT
};
typedef enum gsupplicant_wps_connect_status {
WPS_CONNECT_PENDING,
WPS_CONNECT_SUCCESS,
WPS_CONNECT_FAIL,
WPS_CONNECT_M2D,
WPS_CONNECT_PBC_OVERLAP
} WPS_CONNECT_STATE;
typedef struct gsupplicant_interface_wps_connect {
GSupplicantInterface* iface;
GSupplicantWPSParams wps;
FiW1Wpa_supplicant1InterfaceWPS* wps_proxy;
gulong wps_proxy_handler_id[WPS_PROXY_HANDLER_COUNT];
char* pin;
char* new_pin;
GCancellable* cancel;
gulong cancel_id;
guint timeout_id;
WPS_CONNECT_STATE state;
GSupplicantInterfaceStringResultFunc fn;
GDestroyNotify destroy;
void* data;
} GSupplicantInterfaceWPSConnect;
/* Object definition */
enum supplicant_interface_proxy_handler_id {
PROXY_BSS_ADDED,
PROXY_BSS_REMOVED,
PROXY_NETWORK_ADDED,
PROXY_NETWORK_REMOVED,
PROXY_NETWORK_SELECTED,
PROXY_STA_AUTHORIZED,
PROXY_STA_DEAUTHORIZED,
PROXY_NOTIFY_STATE,
PROXY_NOTIFY_SCANNING,
PROXY_NOTIFY_AP_SCAN,
PROXY_NOTIFY_SCAN_INTERVAL,
PROXY_NOTIFY_CAPABILITIES,
PROXY_NOTIFY_COUNTRY,
PROXY_NOTIFY_DRIVER,
PROXY_NOTIFY_IFNAME,
PROXY_NOTIFY_BRIDGE_IFNAME,
PROXY_NOTIFY_CURRENT_BSS,
PROXY_NOTIFY_CURRENT_NETWORK,
PROXY_NOTIFY_BSSS,
PROXY_NOTIFY_NETWORKS,
PROXY_HANDLER_COUNT
};
enum supplicant_handler_id {
SUPPLICANT_VALID_CHANGED,
SUPPLICANT_INTERFACES_CHANGED,
SUPPLICANT_HANDLER_COUNT
};
struct gsupplicant_interface_priv {
GDBusConnection* bus;
FiW1Wpa_supplicant1Interface* proxy;
gulong proxy_handler_id[PROXY_HANDLER_COUNT];
gulong supplicant_handler_id[SUPPLICANT_HANDLER_COUNT];
GSupplicantWPSCredentials wps_credentials;
guint32 pending_signals;
GStrV* bsss;
GStrV* networks;
GStrV* stations;
char* path;
char* country;
char* driver;
char* ifname;
char* bridge_ifname;
char* current_bss;
char* current_network;
};
typedef GObjectClass GSupplicantInterfaceClass;
G_DEFINE_TYPE(GSupplicantInterface, gsupplicant_interface, G_TYPE_OBJECT)
#define GSUPPLICANT_INTERFACE_TYPE (gsupplicant_interface_get_type())
#define GSUPPLICANT_INTERFACE(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), \
GSUPPLICANT_INTERFACE_TYPE, GSupplicantInterface))
#define SUPER_CLASS gsupplicant_interface_parent_class
/* Supplicant interface properties */
#define GSUPPLICANT_INTERFACE_PROPERTIES_(p) \
p(VALID,valid) \
p(PRESENT,present) \
p(CAPS,caps) \
p(STATE,state) \
p(WPS_CREDENTIALS,wps-credentials) \
p(SCANNING,scanning) \
p(AP_SCAN,ap-scan) \
p(COUNTRY,country) \
p(DRIVER,driver) \
p(IFNAME,ifname) \
p(BRIDGE_IFNAME,bridge-ifname) \
p(CURRENT_BSS,current-bss) \
p(CURRENT_NETWORK,current-network) \
p(BSSS,bsss) \
p(NETWORKS,networks) \
p(SCAN_INTERVAL,scan-interval) \
p(STATIONS,stations)
typedef enum gsupplicant_interface_signal {
#define SIGNAL_ENUM_(P,p) SIGNAL_##P##_CHANGED,
GSUPPLICANT_INTERFACE_PROPERTIES_(SIGNAL_ENUM_)
#undef SIGNAL_ENUM_
SIGNAL_PROPERTY_CHANGED,
SIGNAL_COUNT
} GSUPPLICANT_INTERFACE_SIGNAL;
#define SIGNAL_BIT(name) (1 << SIGNAL_##name##_CHANGED)
/*
* The code assumes that VALID is the first one and that their number
* doesn't exceed the number of bits in pending_signals (currently, 32)
*/
G_STATIC_ASSERT(SIGNAL_VALID_CHANGED == 0);
G_STATIC_ASSERT(SIGNAL_PROPERTY_CHANGED <= 32);
/* Assert that we have covered all publicly defined properties */
G_STATIC_ASSERT((int)SIGNAL_PROPERTY_CHANGED ==
((int)GSUPPLICANT_INTERFACE_PROPERTY_COUNT-1));
#define SIGNAL_PROPERTY_CHANGED_NAME "property-changed"
#define SIGNAL_PROPERTY_CHANGED_DETAIL "%x"
#define SIGNAL_PROPERTY_CHANGED_DETAIL_MAX_LEN (8)
static GQuark gsupplicant_interface_property_quarks[SIGNAL_PROPERTY_CHANGED];
static guint gsupplicant_interface_signals[SIGNAL_COUNT];
static const char* gsupplicant_interface_signame[] = {
#define SIGNAL_NAME_(P,p) #p "-changed",
GSUPPLICANT_INTERFACE_PROPERTIES_(SIGNAL_NAME_)
#undef SIGNAL_NAME_
SIGNAL_PROPERTY_CHANGED_NAME
};
G_STATIC_ASSERT(G_N_ELEMENTS(gsupplicant_interface_signame) == SIGNAL_COUNT);
/* Proxy properties */
#define PROXY_PROPERTY_NAME_CAPABILITIES "Capabilities"
#define PROXY_PROPERTY_NAME_STATE "State"
#define PROXY_PROPERTY_NAME_SCANNING "Scanning"
#define PROXY_PROPERTY_NAME_AP_SCAN "ApScan"
#define PROXY_PROPERTY_NAME_BSS_EXPIRE_AGE "BSSExpireAge"
#define PROXY_PROPERTY_NAME_BSS_EXPIRE_COUNT "BSSExpireCount"
#define PROXY_PROPERTY_NAME_COUNTRY "Country"
#define PROXY_PROPERTY_NAME_DRIVER "Driver"
#define PROXY_PROPERTY_NAME_IFNAME "Ifname"
#define PROXY_PROPERTY_NAME_BRIDGE_IFNAME "BridgeIfname"
#define PROXY_PROPERTY_NAME_CURRENT_BSS "CurrentBSS"
#define PROXY_PROPERTY_NAME_CURRENT_NETWORK "CurrentNetwork"
#define PROXY_PROPERTY_NAME_CURRENT_AUTH_MODE "CurrentAuthMode"
#define PROXY_PROPERTY_NAME_BLOBS "Blobs"
#define PROXY_PROPERTY_NAME_BSSS "BSSs"
#define PROXY_PROPERTY_NAME_NETWORKS "Networks"
#define PROXY_PROPERTY_NAME_FAST_REAUTH "FastReauth"
#define PROXY_PROPERTY_NAME_SCAN_INTERVAL "ScanInterval"
#define PROXY_PROPERTY_NAME_PKCS11_ENGINE_PATH "PKCS11EnginePath"
#define PROXY_PROPERTY_NAME_PKCS11_MODULE_PATH "PKCS11ModulePath"
#define PROXY_PROPERTY_NAME_DISCONNECT_REASON "DisconnectReason"
/* Weak references to the instances of GSupplicantInterface */
static GHashTable* gsupplicant_interface_table = NULL;
/* States */
static const GSupNameIntPair gsupplicant_interface_states [] = {
{ "disconnected", GSUPPLICANT_INTERFACE_STATE_DISCONNECTED },
{ "inactive", GSUPPLICANT_INTERFACE_STATE_INACTIVE },
{ "scanning", GSUPPLICANT_INTERFACE_STATE_SCANNING },
{ "authenticating", GSUPPLICANT_INTERFACE_STATE_AUTHENTICATING },
{ "associating", GSUPPLICANT_INTERFACE_STATE_ASSOCIATING },
{ "associated", GSUPPLICANT_INTERFACE_STATE_ASSOCIATED },
{ "4way_handshake", GSUPPLICANT_INTERFACE_STATE_4WAY_HANDSHAKE },
{ "group_handshake", GSUPPLICANT_INTERFACE_STATE_GROUP_HANDSHAKE },
{ "completed", GSUPPLICANT_INTERFACE_STATE_COMPLETED },
{ "unknown", GSUPPLICANT_INTERFACE_STATE_UNKNOWN }
};
/*==========================================================================*
* Property change signals
*==========================================================================*/
static inline
GSUPPLICANT_INTERFACE_PROPERTY
gsupplicant_interface_property_from_signal(
GSUPPLICANT_INTERFACE_SIGNAL sig)
{
switch (sig) {
#define SIGNAL_PROPERTY_MAP_(P,p) \
case SIGNAL_##P##_CHANGED: return GSUPPLICANT_INTERFACE_PROPERTY_##P;
GSUPPLICANT_INTERFACE_PROPERTIES_(SIGNAL_PROPERTY_MAP_)
#undef SIGNAL_PROPERTY_MAP_
default: /* unreachable */ return GSUPPLICANT_INTERFACE_PROPERTY_ANY;
}
}
/* "/" means that there's no association at all. */
static inline
const char*
gsupplicant_interface_association_path_filter(
const char* path)
{
return (path && path[0] == '/' && path[1] == '\0') ? NULL : path;
}
static
void
gsupplicant_interface_signal_property_change(
GSupplicantInterface* self,
GSUPPLICANT_INTERFACE_SIGNAL sig,
GSUPPLICANT_INTERFACE_PROPERTY prop)
{
GSupplicantInterfacePriv* priv = self->priv;
GASSERT(prop > GSUPPLICANT_INTERFACE_PROPERTY_ANY);
GASSERT(prop < GSUPPLICANT_INTERFACE_PROPERTY_COUNT);
GASSERT(sig < G_N_ELEMENTS(gsupplicant_interface_property_quarks));
if (!gsupplicant_interface_property_quarks[sig]) {
char buf[SIGNAL_PROPERTY_CHANGED_DETAIL_MAX_LEN + 1];
snprintf(buf, sizeof(buf), SIGNAL_PROPERTY_CHANGED_DETAIL, prop);
buf[sizeof(buf)-1] = 0;
gsupplicant_interface_property_quarks[sig] = g_quark_from_string(buf);
}
priv->pending_signals &= ~(1 << sig);
g_signal_emit(self, gsupplicant_interface_signals[sig], 0);
g_signal_emit(self, gsupplicant_interface_signals[SIGNAL_PROPERTY_CHANGED],
gsupplicant_interface_property_quarks[sig], prop);
}
static
void
gsupplicant_interface_emit_pending_signals(
GSupplicantInterface* self)
{
GSupplicantInterfacePriv* priv = self->priv;
GSUPPLICANT_INTERFACE_SIGNAL sig;
gboolean valid_changed;
/* Handlers could drops their references to us */
gsupplicant_interface_ref(self);
/* VALID is the last one to be emitted if we BECOME valid */
if ((priv->pending_signals & SIGNAL_BIT(VALID)) && self->valid) {
priv->pending_signals &= ~SIGNAL_BIT(VALID);
valid_changed = TRUE;
} else {
valid_changed = FALSE;
}
/* Emit the signals. Not that in case if valid has become FALSE, then
* VALID is emitted first, otherwise it's emitted last */
for (sig = SIGNAL_VALID_CHANGED;
sig < SIGNAL_PROPERTY_CHANGED && priv->pending_signals;
sig++) {
if (priv->pending_signals & (1 << sig)) {
gsupplicant_interface_signal_property_change(self, sig,
gsupplicant_interface_property_from_signal(sig));
}
}
/* Then emit VALID if valid has become TRUE */
if (valid_changed) {
gsupplicant_interface_signal_property_change(self,
SIGNAL_VALID_CHANGED, GSUPPLICANT_INTERFACE_PROPERTY_VALID);
}
/* And release the temporary reference */
gsupplicant_interface_unref(self);
}
/*==========================================================================*
* Network parameters
*==========================================================================*/
static
void
gsupplicant_interface_add_network_args_security_wep(
GVariantBuilder* builder,
const GSupplicantNetworkParams* np)
{
if (np->passphrase && np->passphrase[0]) {
const char* key = "wep_key0";
const gsize len = strlen(np->passphrase);
const guint8* bin = NULL;
guint8 buf[13];
if (len == 10 || len == 26) {
/* Check for hex representation of the WEP key */
bin = gutil_hex2bin(np->passphrase, len, buf);
}
if (bin) {
gsupplicant_dict_add_value(builder, key, g_variant_new_fixed_array(
G_VARIANT_TYPE_BYTE, bin, len/2, 1));
} else {
gsupplicant_dict_add_string(builder, key, np->passphrase);
}
gsupplicant_dict_add_uint32(builder, "wep_tx_keyidx", 0);
}
}
static
void
gsupplicant_interface_add_network_args_security_psk(
GVariantBuilder* builder,
const GSupplicantNetworkParams* np)
{
if (np->passphrase && np->passphrase[0]) {
const char* key = "psk";
const gsize len = strlen(np->passphrase);
const guint8* bin = NULL;
guint8 buf[32];
if (len == 64) {
/* Check for hex representation of the 256-bit pre-shared key */
bin = gutil_hex2bin(np->passphrase, len, buf);
}
if (bin) {
gsupplicant_dict_add_value(builder, key, g_variant_new_fixed_array(
G_VARIANT_TYPE_BYTE, bin, len/2, 1));
} else {
gsupplicant_dict_add_string(builder, key, np->passphrase);
}
}
}
static
void
gsupplicant_interface_add_network_args_security_peap(
GVariantBuilder* builder,
const GSupplicantNetworkParams* np,
GHashTable* blobs)
{
if (np->eap == GSUPPLICANT_EAP_METHOD_PEAP) {
switch (np->auth_flags &
(GSUPPLICANT_AUTH_PHASE1_PEAPV0 |
GSUPPLICANT_AUTH_PHASE1_PEAPV1)) {
case GSUPPLICANT_AUTH_PHASE1_PEAPV0:
gsupplicant_dict_add_string(builder, "phase1", "peapver=0");
break;
case GSUPPLICANT_AUTH_PHASE1_PEAPV1:
gsupplicant_dict_add_string(builder, "phase1", "peapver=1");
break;
default:
GWARN("Trying to force PEAPv0 and v1, ignoring");
case 0:
break;
}
}
/*
* Multiple protocols in phase2 should be allowed,
* e.g "autheap=MSCHAPV2 autheap=MD5" for EAP-TTLS
* Does the order matter?
*/
if (np->phase2 != GSUPPLICANT_EAP_METHOD_NONE) {
const char* ca_cert2 =
gsupplicant_check_blob_or_abs_path(np->ca_cert_file2,
blobs);
const char* client_cert2 =
gsupplicant_check_blob_or_abs_path(np->client_cert_file2,
blobs);
const char* auth = (np->auth_flags & GSUPPLICANT_AUTH_PHASE2_AUTHEAP) ?
"autheap" : "auth";
GString* buf = g_string_new(NULL);
guint found, phase2 = np->phase2;
const char* method = gsupplicant_eap_method_name(phase2, &found);
while (method) {
if (buf->len) g_string_append_c(buf, ' ');
g_string_append(buf, auth);
g_string_append_c(buf, '=');
g_string_append(buf, method);
phase2 &= ~found;
method = gsupplicant_eap_method_name(phase2, &found);
}
if (buf->len > 0) {
gsupplicant_dict_add_string(builder, "phase2", buf->str);
}
g_string_free(buf, TRUE);
gsupplicant_dict_add_string0(builder, "ca_cert2", ca_cert2);
if (client_cert2) {
if (np->private_key_file2 && np->private_key_file2[0]) {
const char* private_key2 =
gsupplicant_check_blob_or_abs_path(np->private_key_file2,
blobs);
if (private_key2) {
gsupplicant_dict_add_string(builder, "client_cert2",
client_cert2);
gsupplicant_dict_add_string(builder, "private_key2",
np->private_key_file2);
gsupplicant_dict_add_string_ne(builder,
"private_key_passwd2", np->private_key_passphrase2);
}
} else {
GWARN("Missing private key for phase2");
}
}
gsupplicant_dict_add_string_ne(builder, "subject_match2",
np->subject_match2);
gsupplicant_dict_add_string_ne(builder, "altsubject_match2",
np->altsubject_match2);
gsupplicant_dict_add_string_ne(builder, "domain_suffix_match2",
np->domain_suffix_match2);
}
}
static
void
gsupplicant_interface_add_network_args_security_eap(
GVariantBuilder* builder,
const GSupplicantNetworkParams* np,
GHashTable* blobs)
{
guint found;
const char* ca_cert =
gsupplicant_check_blob_or_abs_path(np->ca_cert_file, blobs);
const char* client_cert =
gsupplicant_check_blob_or_abs_path(np->client_cert_file, blobs);
const char* private_key =
gsupplicant_check_blob_or_abs_path(np->private_key_file, blobs);
const char* method = gsupplicant_eap_method_name(np->eap, &found);
GASSERT(found == np->eap); /* Only one method should be specified */
gsupplicant_dict_add_string_ne(builder, "eap", method);
switch (np->eap) {
case GSUPPLICANT_EAP_METHOD_NONE:
GERR_("No EAP method specified!");
return;
case GSUPPLICANT_EAP_METHOD_PEAP:
case GSUPPLICANT_EAP_METHOD_TTLS:
gsupplicant_interface_add_network_args_security_peap(builder, np, blobs);
break;
case GSUPPLICANT_EAP_METHOD_TLS:
break;
default:
GWARN_("Unsupported EAP method %s", method);
break;
}
gsupplicant_dict_add_string_ne(builder, "identity", np->identity);
gsupplicant_dict_add_string_ne(builder, "anonymous_identity",
np->anonymous_identity);
gsupplicant_dict_add_string_ne(builder, "password", np->passphrase);
gsupplicant_dict_add_string0(builder, "ca_cert", ca_cert);
if (client_cert) {
if (private_key) {
gsupplicant_dict_add_string(builder, "client_cert",
client_cert);
} else {
GWARN("Missing private key");
}
}
if (private_key) {
gsupplicant_dict_add_string(builder, "private_key",
private_key);
gsupplicant_dict_add_string_ne(builder, "private_key_passwd",
np->private_key_passphrase);
}
gsupplicant_dict_add_string_ne(builder, "domain_match",
np->domain_match);
gsupplicant_dict_add_string_ne(builder, "subject_match",
np->subject_match);
gsupplicant_dict_add_string_ne(builder, "altsubject_match",
np->altsubject_match);
gsupplicant_dict_add_string_ne(builder, "domain_suffix_match",
np->domain_suffix_match);
}
static
void
gsupplicant_interface_add_network_args_security_ciphers(
GVariantBuilder* builder,
const GSupplicantNetworkParams* np)
{
static const GSupNameIntPair ciphers [] = {
{ "CCMP", GSUPPLICANT_CIPHER_CCMP },
{ "TKIP", GSUPPLICANT_CIPHER_TKIP },
{ "WEP104", GSUPPLICANT_CIPHER_WEP104 },
{ "WEP40", GSUPPLICANT_CIPHER_WEP40 }
};
char* pairwise = gsupplicant_name_int_concat(np->pairwise, ' ',
ciphers, G_N_ELEMENTS(ciphers));
char* group = gsupplicant_name_int_concat(np->group, ' ',
ciphers, G_N_ELEMENTS(ciphers));
if (pairwise) {
gsupplicant_dict_add_string(builder, "pairwise", pairwise);
g_free(pairwise);
}
if (group) {
gsupplicant_dict_add_string(builder, "group", group);
g_free(group);
}
}
static
void
gsupplicant_interface_add_network_args_security_proto(
GVariantBuilder* builder,
const GSupplicantNetworkParams* np)
{
static const GSupNameIntPair protos [] = {
{ "RSN", GSUPPLICANT_PROTOCOL_RSN },
{ "WPA", GSUPPLICANT_PROTOCOL_WPA }
};
char* proto = gsupplicant_name_int_concat(np->protocol, ' ',
protos, G_N_ELEMENTS(protos));
if (proto) {
gsupplicant_dict_add_string(builder, "proto", proto);
g_free(proto);
}
}
static
GVariant*
gsupplicant_interface_add_network_args_new(
const GSupplicantNetworkParams* np,
GHashTable* blobs)
{
const char* key_mgmt = NULL;
const char* auth_alg = NULL;
GVariantBuilder builder;
g_variant_builder_init(&builder, G_VARIANT_TYPE_VARDICT);
gsupplicant_dict_add_bytes0(&builder, "ssid", np->ssid);
if (np->frequency) {
gsupplicant_dict_add_uint32(&builder, "frequency", np->frequency);
}
gsupplicant_dict_add_string_ne(&builder, "bgscan", np->bgscan);
gsupplicant_dict_add_uint32(&builder, "scan_ssid", np->scan_ssid);
gsupplicant_dict_add_uint32(&builder, "mode", np->mode);
switch (np->security) {
case GSUPPLICANT_SECURITY_NONE:
GDEBUG_("no security");
key_mgmt = "NONE";
auth_alg = "OPEN";
break;
case GSUPPLICANT_SECURITY_WEP:
GDEBUG_("WEP security");
key_mgmt = "NONE";
auth_alg = "OPEN SHARED";
gsupplicant_interface_add_network_args_security_wep(&builder, np);
gsupplicant_interface_add_network_args_security_ciphers(&builder, np);
break;
case GSUPPLICANT_SECURITY_PSK:
GDEBUG_("PSK security");
key_mgmt = "WPA-PSK";
gsupplicant_interface_add_network_args_security_psk(&builder, np);
gsupplicant_interface_add_network_args_security_proto(&builder, np);
gsupplicant_interface_add_network_args_security_ciphers(&builder, np);
break;
case GSUPPLICANT_SECURITY_EAP:
GDEBUG_("EAP security");
key_mgmt = "WPA-EAP";
gsupplicant_interface_add_network_args_security_eap(&builder, np, blobs);
gsupplicant_interface_add_network_args_security_proto(&builder, np);
gsupplicant_interface_add_network_args_security_ciphers(&builder, np);
break;
}
gsupplicant_dict_add_string0(&builder, "auth_alg", auth_alg);
gsupplicant_dict_add_string0(&builder, "key_mgmt", key_mgmt);
return g_variant_ref_sink(g_variant_builder_end(&builder));
}
/*==========================================================================*
* WPS Connect
*==========================================================================*/
static
void
gsupplicant_interface_clear_wps_credentials(
GSupplicantInterface* self)
{
GSupplicantInterfacePriv* priv = self->priv;
priv->wps_credentials.auth_types = GSUPPLICANT_AUTH_NONE;
priv->wps_credentials.encr_types = GSUPPLICANT_WPS_ENCR_NONE;
priv->wps_credentials.key_index = 0;
if (priv->wps_credentials.bssid) {
g_bytes_unref(priv->wps_credentials.bssid);
priv->wps_credentials.bssid = NULL;
}
if (priv->wps_credentials.ssid) {
g_bytes_unref(priv->wps_credentials.ssid);
priv->wps_credentials.ssid = NULL;
}
if (priv->wps_credentials.key) {
g_bytes_unref(priv->wps_credentials.key);
priv->wps_credentials.key = NULL;
}
if (self->wps_credentials) {
self->wps_credentials = NULL;
priv->pending_signals |= SIGNAL_BIT(WPS_CREDENTIALS);
}
}
static
void
gsupplicant_interface_wps_parse_creds(
const char* name,
GVariant* value,
void* data)
{
GSupplicantWPSCredentials* wps = data;
if (!g_strcmp0(name, "BSSID")) {
if (wps->bssid) g_bytes_unref(wps->bssid);
wps->bssid = gsupplicant_variant_data_as_bytes(value);
GVERBOSE(" %s: %s", name, gsupplicant_format_bytes(wps->bssid, TRUE));
} else if (!g_strcmp0(name, "SSID")) {
if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
gsize length = 0;
const char* ssid = g_variant_get_string(value, &length);
if (ssid) {
if (wps->ssid) g_bytes_unref(wps->ssid);
wps->ssid = g_bytes_new(ssid, length);
GVERBOSE(" %s: \"%.*s\"", name, (int)length, ssid);
}
}
} else if (!g_strcmp0(name, "AuthType")) {
static const GSupNameIntPair auth_types_map [] = {
{ "open", GSUPPLICANT_AUTH_OPEN },
{ "shared", GSUPPLICANT_AUTH_SHARED },
{ "wpa-psk", GSUPPLICANT_AUTH_WPA_PSK },
{ "wpa-eap", GSUPPLICANT_AUTH_WPA_EAP },
{ "wpa2-eap", GSUPPLICANT_AUTH_WPA2_EAP },
{ "wpa2-psk", GSUPPLICANT_AUTH_WPA2_PSK }
};
wps->auth_types = gsupplicant_parse_bits_array(0, name, value,
auth_types_map, G_N_ELEMENTS(auth_types_map));
} else if (!g_strcmp0(name, "EncrType")) {
static const GSupNameIntPair encr_types_map [] = {
{ "none", GSUPPLICANT_WPS_ENCR_NONE },
{ "wep", GSUPPLICANT_WPS_ENCR_WEP },
{ "tkip", GSUPPLICANT_WPS_ENCR_TKIP },
{ "aes", GSUPPLICANT_WPS_ENCR_AES }
};
wps->encr_types = gsupplicant_parse_bits_array(0, name, value,
encr_types_map, G_N_ELEMENTS(encr_types_map));
} else if (!g_strcmp0(name, "Key")) {
if (wps->key) g_bytes_unref(wps->key);
wps->key = gsupplicant_variant_data_as_bytes(value);
GVERBOSE(" %s: %s", name, gsupplicant_format_bytes(wps->key, TRUE));
} else if (!g_strcmp0(name, "KeyIndex")) {
if (g_variant_is_of_type(value, G_VARIANT_TYPE_UINT32)) {
wps->key_index = g_variant_get_uint32(value);
GVERBOSE(" %s: %u", name, wps->key_index);
}
}
}
static
void
gsupplicant_interface_wps_connect_dispose(
GSupplicantInterfaceWPSConnect* connect)
{
/* May be invoked twice */
if (connect->wps_proxy) {
gutil_disconnect_handlers(connect->wps_proxy,
connect->wps_proxy_handler_id,
G_N_ELEMENTS(connect->wps_proxy_handler_id));
g_object_unref(connect->wps_proxy);
connect->wps_proxy = NULL;
}
if (connect->timeout_id) {
g_source_remove(connect->timeout_id);
connect->timeout_id = 0;
}
}
static
void
gsupplicant_interface_wps_connect_free(
GSupplicantInterfaceWPSConnect* connect)
{
gsupplicant_interface_wps_connect_dispose(connect);
if (connect->wps.bssid) g_bytes_unref(connect->wps.bssid);
if (connect->wps.p2p_address) g_bytes_unref(connect->wps.p2p_address);
gsupplicant_interface_unref(connect->iface);
if (connect->cancel_id) {
g_cancellable_disconnect(connect->cancel, connect->cancel_id);
}
g_object_unref(connect->cancel);
g_free(connect->pin);
g_free(connect->new_pin);
if (connect->destroy) {
connect->destroy(connect->data);
}
g_slice_free(GSupplicantInterfaceWPSConnect, connect);
}
static
void
gsupplicant_interface_wps_connect_free1(
void* connect)
{
gsupplicant_interface_wps_connect_free(connect);
}
static
void
gsupplicant_interface_wps_connect_cancelled(
GCancellable* cancel,
gpointer data)
{
/*
* Under GLib < 2.40 this function is invoked under the lock
* protecting cancellable. We can't call g_cancellable_disconnect
* because it wouild deadlock, it has to be invoked on a fresh stack.
* That sucks.
*/
GSupplicantInterfaceWPSConnect* connect = data;
gsupplicant_interface_wps_connect_dispose(connect);
gsupplicant_call_later(gsupplicant_interface_wps_connect_free1, connect);
}
static
void
gsupplicant_interface_wps_connect_ok(
GSupplicantInterfaceWPSConnect* connect)
{
GDEBUG("[%s] WPS connect OK", connect->iface->path);
GASSERT(!g_cancellable_is_cancelled(connect->cancel));
if (connect->fn) {
if (connect->cancel_id) {
/* In case if callback calls g_cancellable_cancel() */
g_cancellable_disconnect(connect->cancel, connect->cancel_id);
connect->cancel_id = 0;
}
connect->fn(connect->iface, connect->cancel, NULL, connect->new_pin,
connect->data);
}
gsupplicant_interface_wps_connect_free(connect);
}
static
void
gsupplicant_interface_wps_connect_error_free(
GSupplicantInterfaceWPSConnect* connect,
const GError* error)
{
GERR("Failed to start WPS: %s", GERRMSG(error));
if (connect->fn && !g_cancellable_is_cancelled(connect->cancel)) {
GError* tmp_error = NULL;
if (connect->cancel_id) {
/* In case if callback calls g_cancellable_cancel() */
g_cancellable_disconnect(connect->cancel, connect->cancel_id);
connect->cancel_id = 0;
}
if (!error) {
error = tmp_error = g_error_new_literal(G_IO_ERROR,
G_IO_ERROR_FAILED, "WPS connect failed");
}
connect->fn(connect->iface, connect->cancel, error, NULL,
connect->data);
if (tmp_error) g_error_free(tmp_error);
}
gsupplicant_interface_wps_connect_free(connect);
}
static
gboolean
gsupplicant_interface_wps_connect_timeout(
gpointer data)
{
GSupplicantInterfaceWPSConnect* connect = data;
GDEBUG("WPS connect timed out");
GASSERT(!g_cancellable_is_cancelled(connect->cancel));
connect->timeout_id = 0;
if (connect->fn) {
GError* error = g_error_new_literal(G_IO_ERROR, G_IO_ERROR_TIMED_OUT,
"WPS connect timed out");
if (connect->cancel_id) {
/* In case if callback calls g_cancellable_cancel() */
g_cancellable_disconnect(connect->cancel, connect->cancel_id);
connect->cancel_id = 0;
}
connect->fn(connect->iface, connect->cancel, error, NULL,
connect->data);
g_error_free(error);
}
gsupplicant_interface_wps_connect_free(connect);
return G_SOURCE_REMOVE;
}
static
GSupplicantInterfaceWPSConnect*
gsupplicant_interface_wps_connect_new(
GSupplicantInterface* iface,
GCancellable* cancel,
const GSupplicantWPSParams* params,
gint timeout_sec, /* 0 = default, negative = no timeout */
GSupplicantInterfaceStringResultFunc fn,
GDestroyNotify destroy,
void* data)
{
GSupplicantInterfaceWPSConnect* connect =
g_slice_new0(GSupplicantInterfaceWPSConnect);
connect->iface = gsupplicant_interface_ref(iface);
connect->cancel = cancel ? g_object_ref(cancel) : g_cancellable_new();
connect->wps.role = params->role;
connect->wps.auth = params->auth;
connect->wps.pin = connect->pin = g_strdup(params->pin);
if (params->bssid) {
connect->wps.bssid = g_bytes_ref(params->bssid);
}
if (params->p2p_address) {
connect->wps.p2p_address = g_bytes_ref(params->p2p_address);
}
if (!timeout_sec) timeout_sec = WPS_DEFAULT_CONNECT_TIMEOUT_SEC;
if (timeout_sec > 0) {
connect->timeout_id = g_timeout_add_seconds(timeout_sec,
gsupplicant_interface_wps_connect_timeout, connect);
}
connect->fn = fn;
connect->destroy = destroy;
connect->data = data;
return connect;
}
static
void
gsupplicant_interface_wps_connect_proxy_credentials(
FiW1Wpa_supplicant1InterfaceWPS* proxy,
GVariant* args,
gpointer data)
{
GSupplicantInterfaceWPSConnect* connect = data;
GSupplicantInterface* self = connect->iface;
GSupplicantInterfacePriv* priv = self->priv;
GSupplicantWPSCredentials* wps = &priv->wps_credentials;
GDEBUG("[%s] WPS credentials received", priv->path);
gsupplicant_interface_clear_wps_credentials(self);
gsupplicant_dict_parse(args, gsupplicant_interface_wps_parse_creds, wps);
self->wps_credentials = wps;
priv->pending_signals |= SIGNAL_BIT(WPS_CREDENTIALS);
gsupplicant_interface_emit_pending_signals(self);
}
static
void
gsupplicant_interface_wps_connect_proxy_event(
FiW1Wpa_supplicant1InterfaceWPS* proxy,
const char* type,
GVariant* args,
gpointer data)
{
static const GSupNameIntPair event_types[] = {
{ "success", WPS_CONNECT_SUCCESS },
{ "fail", WPS_CONNECT_FAIL },
{ "m2d", WPS_CONNECT_M2D },
{ "pbc-overlap", WPS_CONNECT_PBC_OVERLAP }
};
GSupplicantInterfaceWPSConnect* connect = data;
GDEBUG("[%s] WPS event \"%s\"", connect->iface->path, type);
connect->state = gsupplicant_name_int_get_int(type, event_types,
G_N_ELEMENTS(event_types), WPS_CONNECT_FAIL);
if (connect->state == WPS_CONNECT_SUCCESS) {
/*
* If connect_id is non-zero, then Start() call has completed
* and we have been waiting for this event. Otherwise, we will
* wait for the Start() call to complete.
*/
if (connect->cancel_id) {
gsupplicant_interface_wps_connect_ok(connect);
}
} else {
GError* error = g_error_new(G_IO_ERROR, G_IO_ERROR_FAILED,
"WPS connect failed (%s)", type);
gsupplicant_interface_wps_connect_error_free(connect, error);
g_error_free(error);
}
}
static
GVariant*
gsupplicant_interface_wps_start_args_new(
const GSupplicantWPSParams* wps)
{
GVariantBuilder builder;
const gboolean enrollee = (wps->role != GSUPPLICANT_WPS_ROLE_REGISTRAR);
const char* role = enrollee ? "enrollee" : "registrar";
g_variant_builder_init(&builder, G_VARIANT_TYPE_VARDICT);
GVERBOSE_("Role: %s", role);
gsupplicant_dict_add_string(&builder, "Role", role);
if (enrollee) {
const char* type = wps->auth == GSUPPLICANT_WPS_AUTH_PIN && wps->pin ?
"pin" : "pbc";
GVERBOSE_("Type: %s", type);
gsupplicant_dict_add_string(&builder, "Type", type);
}
gsupplicant_dict_add_string0(&builder, "Pin", wps->pin);
gsupplicant_dict_add_bytes0(&builder, "Bssid", wps->bssid);
gsupplicant_dict_add_bytes0(&builder, "P2PDeviceAddress",
wps->p2p_address);
return g_variant_ref_sink(g_variant_builder_end(&builder));
}
static
void
gsupplicant_interface_wps_start_pin(
const char* key,