forked from nrfconnect/sdk-zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsupp_api.c
3217 lines (2685 loc) · 77 KB
/
supp_api.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) 2023 Nordic Semiconductor ASA
* Copyright 2024 NXP
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <stdarg.h>
#include <zephyr/logging/log.h>
#include <zephyr/kernel.h>
#include <zephyr/net/wifi_mgmt.h>
#include "includes.h"
#include "common.h"
#include "common/defs.h"
#include "common/ieee802_11_defs.h"
#include "common/ieee802_11_common.h"
#include "wpa_supplicant/config.h"
#include "wpa_supplicant_i.h"
#include "driver_i.h"
#include "supp_main.h"
#include "supp_api.h"
#include "wpa_cli_zephyr.h"
#ifdef CONFIG_WIFI_NM_HOSTAPD_AP
#include "hostapd.h"
#include "hostapd_cli_zephyr.h"
#include "ap_drv_ops.h"
#endif
#include "supp_events.h"
#include "wpa_supplicant/bss.h"
extern struct k_sem wpa_supplicant_ready_sem;
extern struct wpa_global *global;
/* save the last wifi connection parameters */
static struct wifi_connect_req_params last_wifi_conn_params;
enum requested_ops {
CONNECT = 0,
DISCONNECT,
WPS_PBC,
WPS_PIN,
};
enum status_thread_state {
STATUS_THREAD_STOPPED = 0,
STATUS_THREAD_RUNNING,
};
#define OP_STATUS_POLLING_INTERVAL 1
#define CONNECTION_SUCCESS 0
#define CONNECTION_FAILURE 1
#define CONNECTION_TERMINATED 2
#define DISCONNECT_TIMEOUT_MS 5000
#ifdef CONFIG_WIFI_NM_WPA_SUPPLICANT_CRYPTO_ENTERPRISE
static struct wifi_enterprise_creds_params enterprise_creds;
#endif
K_MUTEX_DEFINE(wpa_supplicant_mutex);
extern struct k_work_q *get_workq(void);
struct wpa_supp_api_ctrl {
const struct device *dev;
enum requested_ops requested_op;
enum status_thread_state status_thread_state;
int connection_timeout; /* in seconds */
struct k_work_sync sync;
bool terminate;
};
static struct wpa_supp_api_ctrl wpas_api_ctrl;
static void supp_shell_connect_status(struct k_work *work);
static K_WORK_DELAYABLE_DEFINE(wpa_supp_status_work,
supp_shell_connect_status);
#define wpa_cli_cmd_v(cmd, ...) ({ \
bool status; \
\
if (zephyr_wpa_cli_cmd_v(cmd, ##__VA_ARGS__) < 0) { \
wpa_printf(MSG_ERROR, \
"Failed to execute wpa_cli command: %s", \
cmd); \
status = false; \
} else { \
status = true; \
} \
\
status; \
})
static struct wpa_supplicant *get_wpa_s_handle(const struct device *dev)
{
struct net_if *iface = net_if_lookup_by_dev(dev);
char if_name[CONFIG_NET_INTERFACE_NAME_LEN + 1];
struct wpa_supplicant *wpa_s;
int ret;
if (!iface) {
wpa_printf(MSG_ERROR, "Interface for device %s not found", dev->name);
return NULL;
}
ret = net_if_get_name(iface, if_name, sizeof(if_name));
if (!ret) {
wpa_printf(MSG_ERROR, "Cannot get interface name (%d)", ret);
return NULL;
}
wpa_s = zephyr_get_handle_by_ifname(if_name);
if (!wpa_s) {
wpa_printf(MSG_ERROR, "Interface %s not found", if_name);
return NULL;
}
return wpa_s;
}
#ifdef CONFIG_WIFI_NM_HOSTAPD_AP
#define hostapd_cli_cmd_v(cmd, ...) ({ \
bool status; \
\
if (zephyr_hostapd_cli_cmd_v(cmd, ##__VA_ARGS__) < 0) { \
wpa_printf(MSG_ERROR, \
"Failed to execute wpa_cli command: %s", \
cmd); \
status = false; \
} else { \
status = true; \
} \
\
status; \
})
static inline struct hostapd_iface *get_hostapd_handle(const struct device *dev)
{
struct net_if *iface = net_if_lookup_by_dev(dev);
char if_name[CONFIG_NET_INTERFACE_NAME_LEN + 1];
struct hostapd_iface *hapd;
int ret;
if (!iface) {
wpa_printf(MSG_ERROR, "Interface for device %s not found", dev->name);
return NULL;
}
ret = net_if_get_name(iface, if_name, sizeof(if_name));
if (!ret) {
wpa_printf(MSG_ERROR, "Cannot get interface name (%d)", ret);
return NULL;
}
hapd = zephyr_get_hapd_handle_by_ifname(if_name);
if (!hapd) {
wpa_printf(MSG_ERROR, "Interface %s not found", if_name);
return NULL;
}
return hapd;
}
#endif
#define WPA_SUPP_STATE_POLLING_MS 10
static int wait_for_disconnect_complete(const struct device *dev)
{
int ret = 0;
int attempts = 0;
struct wpa_supplicant *wpa_s = get_wpa_s_handle(dev);
unsigned int max_attempts = DISCONNECT_TIMEOUT_MS / WPA_SUPP_STATE_POLLING_MS;
if (!wpa_s) {
ret = -ENODEV;
wpa_printf(MSG_ERROR, "Failed to get wpa_s handle");
goto out;
}
while (wpa_s->wpa_state != WPA_DISCONNECTED) {
if (attempts++ > max_attempts) {
ret = -ETIMEDOUT;
wpa_printf(MSG_WARNING, "Failed to disconnect from network");
break;
}
k_sleep(K_MSEC(WPA_SUPP_STATE_POLLING_MS));
}
out:
return ret;
}
static void supp_shell_connect_status(struct k_work *work)
{
static int seconds_counter;
int status = CONNECTION_SUCCESS;
int conn_result = CONNECTION_FAILURE;
struct wpa_supplicant *wpa_s;
struct wpa_supp_api_ctrl *ctrl = &wpas_api_ctrl;
k_mutex_lock(&wpa_supplicant_mutex, K_FOREVER);
if (ctrl->status_thread_state == STATUS_THREAD_RUNNING && ctrl->terminate) {
status = CONNECTION_TERMINATED;
goto out;
}
wpa_s = get_wpa_s_handle(ctrl->dev);
if (!wpa_s) {
status = CONNECTION_FAILURE;
goto out;
}
if (ctrl->requested_op == CONNECT && wpa_s->wpa_state != WPA_COMPLETED) {
if (ctrl->connection_timeout > 0 &&
seconds_counter++ > ctrl->connection_timeout) {
if (!wpa_cli_cmd_v("disconnect")) {
goto out;
}
conn_result = -ETIMEDOUT;
supplicant_send_wifi_mgmt_event(wpa_s->ifname,
NET_EVENT_WIFI_CMD_CONNECT_RESULT,
(void *)&conn_result, sizeof(int));
status = CONNECTION_FAILURE;
goto out;
}
k_work_reschedule_for_queue(get_workq(), &wpa_supp_status_work,
K_SECONDS(OP_STATUS_POLLING_INTERVAL));
ctrl->status_thread_state = STATUS_THREAD_RUNNING;
k_mutex_unlock(&wpa_supplicant_mutex);
return;
}
out:
seconds_counter = 0;
ctrl->status_thread_state = STATUS_THREAD_STOPPED;
k_mutex_unlock(&wpa_supplicant_mutex);
}
static struct hostapd_hw_modes *get_mode_by_band(struct wpa_supplicant *wpa_s, uint8_t band)
{
enum hostapd_hw_mode hw_mode;
bool is_6ghz = (band == WIFI_FREQ_BAND_6_GHZ) ? true : false;
if (band == WIFI_FREQ_BAND_2_4_GHZ) {
hw_mode = HOSTAPD_MODE_IEEE80211G;
} else if ((band == WIFI_FREQ_BAND_5_GHZ) ||
(band == WIFI_FREQ_BAND_6_GHZ)) {
hw_mode = HOSTAPD_MODE_IEEE80211A;
} else {
return NULL;
}
return get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, hw_mode, is_6ghz);
}
static int wpa_supp_supported_channels(struct wpa_supplicant *wpa_s, uint8_t band, char **chan_list)
{
struct hostapd_hw_modes *mode = NULL;
int i;
int offset, retval;
int size;
char *_chan_list;
mode = get_mode_by_band(wpa_s, band);
if (!mode) {
wpa_printf(MSG_ERROR, "Unsupported or invalid band: %d", band);
return -EINVAL;
}
size = ((mode->num_channels) * CHAN_NUM_LEN) + 1;
_chan_list = os_malloc(size);
if (!_chan_list) {
wpa_printf(MSG_ERROR, "Mem alloc failed for channel list");
return -ENOMEM;
}
retval = 0;
offset = 0;
for (i = 0; i < mode->num_channels; i++) {
retval = snprintf(_chan_list + offset, CHAN_NUM_LEN, " %d",
mode->channels[i].freq);
offset += retval;
}
*chan_list = _chan_list;
return 0;
}
static int wpa_supp_band_chan_compat(struct wpa_supplicant *wpa_s, uint8_t band, uint8_t channel)
{
struct hostapd_hw_modes *mode = NULL;
int i;
mode = get_mode_by_band(wpa_s, band);
if (!mode) {
wpa_printf(MSG_ERROR, "Unsupported or invalid band: %d", band);
return -EINVAL;
}
for (i = 0; i < mode->num_channels; i++) {
if (mode->channels[i].chan == channel) {
return mode->channels[i].freq;
}
}
wpa_printf(MSG_ERROR, "Channel %d not supported for band %d", channel, band);
return -EINVAL;
}
static inline void wpa_supp_restart_status_work(void)
{
/* Terminate synchronously */
wpas_api_ctrl.terminate = 1;
k_work_flush_delayable(&wpa_supp_status_work, &wpas_api_ctrl.sync);
wpas_api_ctrl.terminate = 0;
/* Start afresh */
k_work_reschedule_for_queue(get_workq(), &wpa_supp_status_work, K_MSEC(10));
}
static inline int chan_to_freq(int chan)
{
/* We use global channel list here and also use the widest
* op_class for 5GHz channels as there is no user input
* for these (yet).
*/
int freq = -1;
int op_classes[] = {81, 82, 128};
int op_classes_size = ARRAY_SIZE(op_classes);
for (int i = 0; i < op_classes_size; i++) {
freq = ieee80211_chan_to_freq(NULL, op_classes[i], chan);
if (freq > 0) {
break;
}
}
if (freq <= 0) {
wpa_printf(MSG_ERROR, "Invalid channel %d", chan);
return -1;
}
return freq;
}
static inline enum wifi_frequency_bands wpas_band_to_zephyr(enum wpa_radio_work_band band)
{
switch (band) {
case BAND_2_4_GHZ:
return WIFI_FREQ_BAND_2_4_GHZ;
case BAND_5_GHZ:
return WIFI_FREQ_BAND_5_GHZ;
default:
return WIFI_FREQ_BAND_UNKNOWN;
}
}
static inline enum wifi_security_type wpas_key_mgmt_to_zephyr(int key_mgmt, int proto, int pwe)
{
switch (key_mgmt) {
case WPA_KEY_MGMT_IEEE8021X:
case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
return WIFI_SECURITY_TYPE_EAP_TLS;
case WPA_KEY_MGMT_NONE:
return WIFI_SECURITY_TYPE_NONE;
case WPA_KEY_MGMT_PSK:
if (proto == WPA_PROTO_RSN) {
return WIFI_SECURITY_TYPE_PSK;
} else {
return WIFI_SECURITY_TYPE_WPA_PSK;
}
case WPA_KEY_MGMT_PSK_SHA256:
return WIFI_SECURITY_TYPE_PSK_SHA256;
case WPA_KEY_MGMT_SAE:
if (pwe == 1) {
return WIFI_SECURITY_TYPE_SAE_H2E;
} else if (pwe == 2) {
return WIFI_SECURITY_TYPE_SAE_AUTO;
} else {
return WIFI_SECURITY_TYPE_SAE_HNP;
}
case WPA_KEY_MGMT_SAE | WPA_KEY_MGMT_PSK:
return WIFI_SECURITY_TYPE_WPA_AUTO_PERSONAL;
case WPA_KEY_MGMT_FT_PSK:
return WIFI_SECURITY_TYPE_FT_PSK;
case WPA_KEY_MGMT_FT_SAE:
return WIFI_SECURITY_TYPE_FT_SAE;
case WPA_KEY_MGMT_FT_IEEE8021X:
return WIFI_SECURITY_TYPE_FT_EAP;
case WPA_KEY_MGMT_FT_IEEE8021X_SHA384:
return WIFI_SECURITY_TYPE_FT_EAP_SHA384;
default:
return WIFI_SECURITY_TYPE_UNKNOWN;
}
}
#ifdef CONFIG_WIFI_NM_WPA_SUPPLICANT_CRYPTO_ENTERPRISE
static int wpas_config_process_blob(struct wpa_config *config, char *name, uint8_t *data,
uint32_t data_len)
{
struct wpa_config_blob *blob;
if (!data || !data_len) {
return -1;
}
blob = os_zalloc(sizeof(*blob));
if (blob == NULL) {
return -1;
}
blob->data = os_zalloc(data_len);
if (blob->data == NULL) {
os_free(blob);
return -1;
}
blob->name = os_strdup(name);
if (blob->name == NULL) {
wpa_config_free_blob(blob);
return -1;
}
os_memcpy(blob->data, data, data_len);
blob->len = data_len;
wpa_config_set_blob(config, blob);
return 0;
}
#endif
#if defined CONFIG_WIFI_NM_WPA_SUPPLICANT_CRYPTO_ENTERPRISE || \
defined CONFIG_WIFI_NM_HOSTAPD_CRYPTO_ENTERPRISE
int supplicant_add_enterprise_creds(const struct device *dev,
struct wifi_enterprise_creds_params *creds)
{
int ret = 0;
if (!creds) {
ret = -1;
wpa_printf(MSG_ERROR, "enterprise creds is NULL");
goto out;
}
memcpy((void *)&enterprise_creds, (void *)creds,
sizeof(struct wifi_enterprise_creds_params));
out:
return ret;
}
static const struct wifi_cipher_desc ciphers[] = {
{WPA_CAPA_ENC_GCMP_256, "GCMP-256"},
{WPA_CAPA_ENC_CCMP, "CCMP"},
{WPA_CAPA_ENC_GCMP, "GCMP"},
};
static const struct wifi_cipher_desc ciphers_group_mgmt[] = {
{WPA_CAPA_ENC_BIP, "AES-128-CMAC"},
{WPA_CAPA_ENC_BIP_GMAC_128, "BIP-GMAC-128"},
{WPA_CAPA_ENC_BIP_GMAC_256, "BIP-GMAC-256"},
};
static struct wifi_eap_config eap_config[] = {
{WIFI_SECURITY_TYPE_EAP_TLS, WIFI_EAP_TYPE_TLS, WIFI_EAP_TYPE_NONE, "TLS", NULL},
{WIFI_SECURITY_TYPE_EAP_PEAP_MSCHAPV2, WIFI_EAP_TYPE_PEAP, WIFI_EAP_TYPE_MSCHAPV2, "PEAP",
"auth=MSCHAPV2"},
{WIFI_SECURITY_TYPE_EAP_PEAP_GTC, WIFI_EAP_TYPE_PEAP, WIFI_EAP_TYPE_GTC, "PEAP",
"auth=GTC"},
{WIFI_SECURITY_TYPE_EAP_TTLS_MSCHAPV2, WIFI_EAP_TYPE_TTLS, WIFI_EAP_TYPE_NONE, "TTLS",
"auth=MSCHAPV2"},
{WIFI_SECURITY_TYPE_EAP_PEAP_TLS, WIFI_EAP_TYPE_PEAP, WIFI_EAP_TYPE_TLS, "PEAP",
"auth=TLS"},
};
int process_cipher_config(struct wifi_connect_req_params *params,
struct wifi_eap_cipher_config *cipher_config)
{
unsigned int cipher_capa;
unsigned int gropu_mgmt_cipher_capa;
unsigned int index;
if (params->suiteb_type == WIFI_SUITEB) {
cipher_capa = WPA_CAPA_ENC_GCMP;
gropu_mgmt_cipher_capa = WPA_CAPA_ENC_BIP_GMAC_128;
cipher_config->key_mgmt = "WPA-EAP-SUITE-B";
cipher_config->openssl_ciphers = "SUITEB128";
cipher_config->tls_flags = "[SUITEB]";
} else if (params->suiteb_type == WIFI_SUITEB_192) {
cipher_capa = WPA_CAPA_ENC_GCMP_256;
gropu_mgmt_cipher_capa = WPA_CAPA_ENC_BIP_GMAC_256;
if (params->ft_used) {
cipher_config->key_mgmt = "WPA-EAP-SUITE-B-192 FT-EAP-SHA384";
} else {
cipher_config->key_mgmt = "WPA-EAP-SUITE-B-192";
}
cipher_config->openssl_ciphers = "SUITEB192";
cipher_config->tls_flags = "[SUITEB]";
} else {
cipher_capa = WPA_CAPA_ENC_CCMP;
gropu_mgmt_cipher_capa = WPA_CAPA_ENC_BIP;
if (params->ft_used) {
cipher_config->key_mgmt = "WPA-EAP FT-EAP";
} else {
cipher_config->key_mgmt = "WPA-EAP";
}
}
for (index = 0; index < ARRAY_SIZE(ciphers); index++) {
if (cipher_capa == ciphers[index].capa) {
cipher_config->group_cipher = ciphers[index].name;
cipher_config->pairwise_cipher = ciphers[index].name;
break;
}
}
if (index == ARRAY_SIZE(ciphers)) {
wpa_printf(MSG_ERROR, "Get ciphers error");
goto out;
}
for (index = 0; index < ARRAY_SIZE(ciphers_group_mgmt); index++) {
if (gropu_mgmt_cipher_capa == ciphers_group_mgmt[index].capa) {
cipher_config->group_mgmt_cipher = ciphers_group_mgmt[index].name;
break;
}
}
if (index == ARRAY_SIZE(ciphers_group_mgmt)) {
wpa_printf(MSG_ERROR, "Get group mgmt ciphers error");
goto out;
}
return 0;
out:
return -EINVAL;
}
static int is_eap_valid_security(int security)
{
return (security == WIFI_SECURITY_TYPE_EAP_TLS ||
security == WIFI_SECURITY_TYPE_EAP_PEAP_MSCHAPV2 ||
security == WIFI_SECURITY_TYPE_EAP_PEAP_GTC ||
security == WIFI_SECURITY_TYPE_EAP_TTLS_MSCHAPV2 ||
security == WIFI_SECURITY_TYPE_EAP_PEAP_TLS);
}
#endif
#ifdef CONFIG_WIFI_NM_HOSTAPD_CRYPTO_ENTERPRISE
static int hapd_process_cert_data(struct hostapd_bss_config *conf,
char *type, uint8_t *data, uint32_t data_len)
{
if (os_strcmp(type, "ca_cert_blob") == 0) {
conf->ca_cert_blob = data;
conf->ca_cert_blob_len = data_len;
} else if (os_strcmp(type, "server_cert_blob") == 0) {
conf->server_cert_blob = data;
conf->server_cert_blob_len = data_len;
} else if (os_strcmp(type, "private_key_blob") == 0) {
conf->private_key_blob = data;
conf->private_key_blob_len = data_len;
} else if (os_strcmp(type, "dh_blob") == 0) {
conf->dh_blob = data;
conf->dh_blob_len = data_len;
} else {
wpa_printf(MSG_ERROR, "input type error");
return -ENOTSUP;
}
return 0;
}
static int hapd_get_eap_config(struct wifi_connect_req_params *params,
struct wifi_eap_config *eap_cfg)
{
unsigned int index = 0;
for (index = 0; index < ARRAY_SIZE(eap_config); index++) {
if (params->security == eap_config[index].type) {
memcpy(eap_cfg, &eap_config[index], sizeof(struct wifi_eap_config));
break;
}
}
if (index == ARRAY_SIZE(eap_config)) {
wpa_printf(MSG_ERROR, "Get eap method type with security type: %d",
params->security);
return -ENOTSUP;
}
return 0;
}
static struct hostapd_eap_user *hostapd_process_eap_user_phase1(
struct wifi_connect_req_params *params, struct hostapd_eap_user **pnew_user)
{
struct hostapd_eap_user *user = NULL, *tail = NULL, *new_user = NULL;
struct wifi_eap_config eap_cfg;
user = os_zalloc(sizeof(*user));
if (user == NULL) {
wpa_printf(MSG_ERROR, "EAP user allocation failed");
goto failed;
}
user->force_version = -1;
if (params->eap_ver >= 0) {
user->force_version = params->eap_ver;
}
if (hapd_get_eap_config(params, &eap_cfg)) {
goto failed;
}
user->methods[0].method = eap_cfg.eap_type_phase1;
user->methods[0].vendor = 0;
if (tail == NULL) {
tail = new_user = user;
} else {
tail->next = user;
tail = user;
}
*pnew_user = new_user;
return tail;
failed:
if (user) {
hostapd_config_free_eap_user(user);
}
return NULL;
}
static int hostapd_process_eap_user(struct wifi_connect_req_params *params,
struct hostapd_bss_config *conf)
{
struct hostapd_eap_user *user = NULL, *tail = NULL, *user_list = NULL;
int i, nusers = params->nusers;
const char *identity, *password;
struct wifi_eap_config eap_cfg;
int ret = 0;
if (hapd_get_eap_config(params, &eap_cfg)) {
goto failed;
}
if (eap_cfg.phase2 != NULL) {
tail = hostapd_process_eap_user_phase1(params, &user_list);
}
if (eap_cfg.phase2 != NULL && !nusers) {
wpa_printf(MSG_ERROR, "EAP users not found.");
goto failed;
}
for (i = 0; i < nusers; i++) {
user = os_zalloc(sizeof(*user));
if (user == NULL) {
wpa_printf(MSG_ERROR, "EAP user allocation failed");
goto failed;
}
user->force_version = -1;
if (params->eap_ver >= 0) {
user->force_version = params->eap_ver;
}
identity = params->identities[i];
password = params->passwords[i];
user->identity = os_memdup(identity, os_strlen(identity));
if (user->identity == NULL) {
wpa_printf(MSG_ERROR,
"Failed to allocate "
"memory for EAP identity");
goto failed;
}
user->identity_len = os_strlen(identity);
user->methods[0].method = eap_cfg.eap_type_phase1;
user->methods[0].vendor = 0;
if (eap_cfg.phase2 != NULL) {
user->methods[0].method = eap_cfg.eap_type_phase2;
user->password = os_memdup(password, os_strlen(password));
if (user->password == NULL) {
wpa_printf(MSG_ERROR,
"Failed to allocate "
"memory for EAP password");
goto failed;
}
user->password_len = os_strlen(password);
user->phase2 = 1;
}
if (params->security == WIFI_SECURITY_TYPE_EAP_TTLS_MSCHAPV2) {
user->ttls_auth |= 0x1E;
}
if (tail == NULL) {
tail = user_list = user;
} else {
tail->next = user;
tail = user;
}
continue;
failed:
if (user) {
hostapd_config_free_eap_user(user);
}
ret = -1;
break;
}
if (ret == 0) {
hostapd_config_free_eap_users(conf->eap_user);
conf->eap_user = user_list;
} else {
hostapd_config_free_eap_users(user_list);
}
return ret;
}
int hapd_process_enterprise_config(struct hostapd_iface *iface,
struct wifi_connect_req_params *params)
{
struct wifi_eap_cipher_config cipher_config = {
NULL, "DEFAULT:!EXP:!LOW", "CCMP", "CCMP", "AES-128-CMAC", NULL};
int ret = 0;
if (process_cipher_config(params, &cipher_config)) {
goto out;
}
if (!hostapd_cli_cmd_v("set wpa %d", WPA_PROTO_RSN)) {
goto out;
}
if (!hostapd_cli_cmd_v("set wpa_key_mgmt %s", cipher_config.key_mgmt)) {
goto out;
}
if (!hostapd_cli_cmd_v("set rsn_pairwise %s", cipher_config.pairwise_cipher)) {
goto out;
}
if (!hostapd_cli_cmd_v("set wpa_pairwise %s", cipher_config.pairwise_cipher)) {
goto out;
}
if (!hostapd_cli_cmd_v("set group_cipher %s", cipher_config.group_cipher)) {
goto out;
}
if (!hostapd_cli_cmd_v("set group_mgmt_cipher %s", cipher_config.group_mgmt_cipher)) {
goto out;
}
if (cipher_config.tls_flags != NULL) {
if (!hostapd_cli_cmd_v("set tls_flags %s", cipher_config.tls_flags)) {
goto out;
}
}
if (!hostapd_cli_cmd_v("set ieee8021x %d", 1)) {
goto out;
}
if (!hostapd_cli_cmd_v("set eapol_version %d", 2)) {
goto out;
}
if (!hostapd_cli_cmd_v("set eap_server %d", 1)) {
goto out;
}
if (hapd_process_cert_data(iface->bss[0]->conf, "ca_cert_blob",
enterprise_creds.ca_cert, enterprise_creds.ca_cert_len)) {
goto out;
}
if (hapd_process_cert_data(iface->bss[0]->conf, "server_cert_blob",
enterprise_creds.server_cert, enterprise_creds.server_cert_len)) {
goto out;
}
if (hapd_process_cert_data(iface->bss[0]->conf, "private_key_blob",
enterprise_creds.server_key, enterprise_creds.server_key_len)) {
goto out;
}
if (hapd_process_cert_data(iface->bss[0]->conf, "dh_blob",
enterprise_creds.dh_param, enterprise_creds.dh_param_len)) {
goto out;
}
if (!hostapd_cli_cmd_v("set private_key_passwd %s", params->key_passwd)) {
goto out;
}
if (hostapd_process_eap_user(params, iface->bss[0]->conf)) {
goto out;
}
return ret;
out:
return -1;
}
#endif
#ifdef CONFIG_WIFI_NM_WPA_SUPPLICANT_CRYPTO_ENTERPRISE
static void wpas_remove_certs(struct wpa_supplicant *wpa_s)
{
wpa_config_remove_blob(wpa_s->conf, "ca_cert");
wpa_config_remove_blob(wpa_s->conf, "client_cert");
wpa_config_remove_blob(wpa_s->conf, "private_key");
wpa_config_remove_blob(wpa_s->conf, "ca_cert2");
wpa_config_remove_blob(wpa_s->conf, "client_cert2");
wpa_config_remove_blob(wpa_s->conf, "private_key2");
}
#endif
static int wpas_add_and_config_network(struct wpa_supplicant *wpa_s,
struct wifi_connect_req_params *params,
bool mode_ap)
{
struct add_network_resp resp = {0};
char *chan_list = NULL;
struct net_eth_addr mac = {0};
int ret = 0;
uint8_t ssid_null_terminated[WIFI_SSID_MAX_LEN + 1];
uint8_t psk_null_terminated[WIFI_PSK_MAX_LEN + 1];
uint8_t sae_null_terminated[WIFI_SAE_PSWD_MAX_LEN + 1];
#ifdef CONFIG_WIFI_NM_WPA_SUPPLICANT_CRYPTO_ENTERPRISE
struct wifi_eap_cipher_config cipher_config = {NULL, "DEFAULT:!EXP:!LOW", "CCMP",
"CCMP", "AES-128-CMAC", NULL};
char *method = NULL;
char phase1[128] = {0};
char *phase2 = NULL;
unsigned int index;
wpas_remove_certs(wpa_s);
#endif
if (!wpa_cli_cmd_v("remove_network all")) {
goto out;
}
ret = z_wpa_ctrl_add_network(&resp);
if (ret) {
wpa_printf(MSG_ERROR, "Failed to add network");
goto out;
}
wpa_printf(MSG_DEBUG, "NET added: %d", resp.network_id);
if (mode_ap) {
if (!wpa_cli_cmd_v("set_network %d mode 2", resp.network_id)) {
goto out;
}
}
if (params->ssid_length > WIFI_SSID_MAX_LEN) {
wpa_printf(MSG_ERROR, "SSID too long (max %d characters)", WIFI_SSID_MAX_LEN);
goto out;
}
strncpy(ssid_null_terminated, params->ssid, WIFI_SSID_MAX_LEN);
ssid_null_terminated[params->ssid_length] = '\0';
if (!wpa_cli_cmd_v("set_network %d ssid \"%s\"",
resp.network_id, ssid_null_terminated)) {
goto out;
}
if (!wpa_cli_cmd_v("set_network %d scan_ssid 1", resp.network_id)) {
goto out;
}
if (!wpa_cli_cmd_v("set_network %d key_mgmt NONE", resp.network_id)) {
goto out;
}
if (!wpa_cli_cmd_v("set_network %d ieee80211w 0", resp.network_id)) {
goto out;
}
if (params->band != WIFI_FREQ_BAND_UNKNOWN) {
ret = wpa_supp_supported_channels(wpa_s, params->band, &chan_list);
if (ret < 0) {
goto rem_net;
}
if (chan_list) {
if (!wpa_cli_cmd_v("set_network %d scan_freq%s", resp.network_id,
chan_list)) {
os_free(chan_list);
goto out;
}
os_free(chan_list);
}
}
if (params->security != WIFI_SECURITY_TYPE_NONE) {
if (params->psk) {
if ((params->psk_length < WIFI_PSK_MIN_LEN) ||
(params->psk_length > WIFI_PSK_MAX_LEN)) {
wpa_printf(MSG_ERROR,
"Passphrase should be in range (%d-%d) characters",
WIFI_PSK_MIN_LEN, WIFI_PSK_MAX_LEN);
goto out;
}
strncpy(psk_null_terminated, params->psk, WIFI_PSK_MAX_LEN);
psk_null_terminated[params->psk_length] = '\0';
}
/* SAP - only open and WPA2-PSK are supported for now */
if (mode_ap && params->security != WIFI_SECURITY_TYPE_PSK) {
ret = -1;
wpa_printf(MSG_ERROR, "Unsupported security type: %d",
params->security);
goto rem_net;
}
/* Except for WPA-PSK, rest all are under WPA2 */
if (params->security != WIFI_SECURITY_TYPE_WPA_PSK) {
if (!wpa_cli_cmd_v("set_network %d proto RSN",
resp.network_id)) {
goto out;
}
}
if (params->security == WIFI_SECURITY_TYPE_SAE_HNP ||
params->security == WIFI_SECURITY_TYPE_SAE_H2E ||
params->security == WIFI_SECURITY_TYPE_SAE_AUTO) {
if (params->sae_password) {
if ((params->sae_password_length < WIFI_PSK_MIN_LEN) ||
(params->sae_password_length > WIFI_SAE_PSWD_MAX_LEN)) {
wpa_printf(MSG_ERROR,
"Passphrase should be in range (%d-%d) characters",
WIFI_PSK_MIN_LEN, WIFI_SAE_PSWD_MAX_LEN);
goto out;
}
strncpy(sae_null_terminated, params->sae_password,
WIFI_SAE_PSWD_MAX_LEN);
sae_null_terminated[params->sae_password_length] = '\0';
if (!wpa_cli_cmd_v("set_network %d sae_password \"%s\"",
resp.network_id, sae_null_terminated)) {
goto out;
}
} else {
if (!wpa_cli_cmd_v("set_network %d sae_password \"%s\"",
resp.network_id, psk_null_terminated)) {
goto out;
}
}
if (params->security == WIFI_SECURITY_TYPE_SAE_H2E ||
params->security == WIFI_SECURITY_TYPE_SAE_AUTO) {
if (!wpa_cli_cmd_v("set sae_pwe %d",
(params->security == WIFI_SECURITY_TYPE_SAE_H2E)
? 1
: 2)) {
goto out;
}
}
if (!wpa_cli_cmd_v("set_network %d key_mgmt SAE%s", resp.network_id,
params->ft_used ? " FT-SAE" : "")) {
goto out;
}
} else if (params->security == WIFI_SECURITY_TYPE_PSK_SHA256) {
if (!wpa_cli_cmd_v("set_network %d psk \"%s\"",
resp.network_id, psk_null_terminated)) {
goto out;
}
if (!wpa_cli_cmd_v("set_network %d key_mgmt WPA-PSK-SHA256",
resp.network_id)) {
goto out;
}