blob: 9fbfbf9fb9fe329303933a0d3058f5a1dc734838 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * WPA Supplicant / Configuration backend: text file
Hai Shalomc3565922019-10-28 11:58:20 -07003 * Copyright (c) 2003-2019, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 *
8 * This file implements a configuration backend for text files. All the
9 * configuration information is stored in a text file that uses a format
10 * described in the sample configuration file, wpa_supplicant.conf.
11 */
12
13#include "includes.h"
Dmitry Shmidt7f656022015-02-25 14:36:37 -080014#ifdef ANDROID
15#include <sys/stat.h>
16#endif /* ANDROID */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070017
18#include "common.h"
19#include "config.h"
20#include "base64.h"
21#include "uuid.h"
Dmitry Shmidt29333592017-01-09 12:27:11 -080022#include "common/ieee802_1x_defs.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070023#include "p2p/p2p.h"
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080024#include "eap_peer/eap_methods.h"
25#include "eap_peer/eap.h"
Hai Shalom60840252021-02-19 19:02:11 -080026#include "utils/config.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070027
28
29static int wpa_config_validate_network(struct wpa_ssid *ssid, int line)
30{
31 int errors = 0;
32
33 if (ssid->passphrase) {
34 if (ssid->psk_set) {
35 wpa_printf(MSG_ERROR, "Line %d: both PSK and "
36 "passphrase configured.", line);
37 errors++;
38 }
39 wpa_config_update_psk(ssid);
40 }
41
Dmitry Shmidt29333592017-01-09 12:27:11 -080042 if (ssid->disabled == 2)
43 ssid->p2p_persistent_group = 1;
44
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070045 if ((ssid->group_cipher & WPA_CIPHER_CCMP) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -070046 !(ssid->pairwise_cipher & (WPA_CIPHER_CCMP | WPA_CIPHER_CCMP_256 |
47 WPA_CIPHER_GCMP | WPA_CIPHER_GCMP_256 |
48 WPA_CIPHER_NONE))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070049 /* Group cipher cannot be stronger than the pairwise cipher. */
50 wpa_printf(MSG_DEBUG, "Line %d: removed CCMP from group cipher"
51 " list since it was not allowed for pairwise "
52 "cipher", line);
53 ssid->group_cipher &= ~WPA_CIPHER_CCMP;
54 }
55
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080056 if (ssid->mode == WPAS_MODE_MESH &&
57 (ssid->key_mgmt != WPA_KEY_MGMT_NONE &&
58 ssid->key_mgmt != WPA_KEY_MGMT_SAE)) {
59 wpa_printf(MSG_ERROR,
60 "Line %d: key_mgmt for mesh network should be open or SAE",
61 line);
62 errors++;
63 }
64
Hai Shalom74f70d42019-02-11 14:42:39 -080065#ifdef CONFIG_OCV
66 if (ssid->ocv && ssid->ieee80211w == NO_MGMT_FRAME_PROTECTION) {
67 wpa_printf(MSG_ERROR,
68 "Line %d: PMF needs to be enabled whenever using OCV",
69 line);
70 errors++;
71 }
72#endif /* CONFIG_OCV */
73
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070074 return errors;
75}
76
77
78static struct wpa_ssid * wpa_config_read_network(FILE *f, int *line, int id)
79{
80 struct wpa_ssid *ssid;
81 int errors = 0, end = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070082 char buf[2000], *pos, *pos2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070083
84 wpa_printf(MSG_MSGDUMP, "Line: %d - start of a new network block",
85 *line);
86 ssid = os_zalloc(sizeof(*ssid));
87 if (ssid == NULL)
88 return NULL;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -070089 dl_list_init(&ssid->psk_list);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070090 ssid->id = id;
91
92 wpa_config_set_network_defaults(ssid);
93
94 while (wpa_config_get_line(buf, sizeof(buf), f, line, &pos)) {
95 if (os_strcmp(pos, "}") == 0) {
96 end = 1;
97 break;
98 }
99
100 pos2 = os_strchr(pos, '=');
101 if (pos2 == NULL) {
102 wpa_printf(MSG_ERROR, "Line %d: Invalid SSID line "
103 "'%s'.", *line, pos);
104 errors++;
105 continue;
106 }
107
108 *pos2++ = '\0';
109 if (*pos2 == '"') {
110 if (os_strchr(pos2 + 1, '"') == NULL) {
111 wpa_printf(MSG_ERROR, "Line %d: invalid "
112 "quotation '%s'.", *line, pos2);
113 errors++;
114 continue;
115 }
116 }
117
Hai Shalomfdcde762020-04-02 11:19:20 -0700118 if (wpa_config_set(ssid, pos, pos2, *line) < 0) {
119#ifndef CONFIG_WEP
120 if (os_strcmp(pos, "wep_key0") == 0 ||
121 os_strcmp(pos, "wep_key1") == 0 ||
122 os_strcmp(pos, "wep_key2") == 0 ||
123 os_strcmp(pos, "wep_key3") == 0 ||
124 os_strcmp(pos, "wep_tx_keyidx") == 0) {
125 wpa_printf(MSG_ERROR,
126 "Line %d: unsupported WEP parameter",
127 *line);
128 ssid->disabled = 1;
129 continue;
130 }
131#endif /* CONFIG_WEP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700132 errors++;
Hai Shalomfdcde762020-04-02 11:19:20 -0700133 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700134 }
135
136 if (!end) {
137 wpa_printf(MSG_ERROR, "Line %d: network block was not "
138 "terminated properly.", *line);
139 errors++;
140 }
141
142 errors += wpa_config_validate_network(ssid, *line);
143
144 if (errors) {
145 wpa_config_free_ssid(ssid);
146 ssid = NULL;
147 }
148
149 return ssid;
150}
151
152
Dmitry Shmidt04949592012-07-19 12:16:46 -0700153static struct wpa_cred * wpa_config_read_cred(FILE *f, int *line, int id)
154{
155 struct wpa_cred *cred;
156 int errors = 0, end = 0;
157 char buf[256], *pos, *pos2;
158
159 wpa_printf(MSG_MSGDUMP, "Line: %d - start of a new cred block", *line);
160 cred = os_zalloc(sizeof(*cred));
161 if (cred == NULL)
162 return NULL;
163 cred->id = id;
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -0700164 cred->sim_num = DEFAULT_USER_SELECTED_SIM;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700165
166 while (wpa_config_get_line(buf, sizeof(buf), f, line, &pos)) {
167 if (os_strcmp(pos, "}") == 0) {
168 end = 1;
169 break;
170 }
171
172 pos2 = os_strchr(pos, '=');
173 if (pos2 == NULL) {
174 wpa_printf(MSG_ERROR, "Line %d: Invalid cred line "
175 "'%s'.", *line, pos);
176 errors++;
177 continue;
178 }
179
180 *pos2++ = '\0';
181 if (*pos2 == '"') {
182 if (os_strchr(pos2 + 1, '"') == NULL) {
183 wpa_printf(MSG_ERROR, "Line %d: invalid "
184 "quotation '%s'.", *line, pos2);
185 errors++;
186 continue;
187 }
188 }
189
190 if (wpa_config_set_cred(cred, pos, pos2, *line) < 0)
191 errors++;
192 }
193
194 if (!end) {
195 wpa_printf(MSG_ERROR, "Line %d: cred block was not "
196 "terminated properly.", *line);
197 errors++;
198 }
199
200 if (errors) {
201 wpa_config_free_cred(cred);
202 cred = NULL;
203 }
204
205 return cred;
206}
207
208
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700209#ifndef CONFIG_NO_CONFIG_BLOBS
210static struct wpa_config_blob * wpa_config_read_blob(FILE *f, int *line,
211 const char *name)
212{
213 struct wpa_config_blob *blob;
214 char buf[256], *pos;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800215 char *encoded = NULL, *nencoded;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700216 int end = 0;
217 size_t encoded_len = 0, len;
218
219 wpa_printf(MSG_MSGDUMP, "Line: %d - start of a new named blob '%s'",
220 *line, name);
221
222 while (wpa_config_get_line(buf, sizeof(buf), f, line, &pos)) {
223 if (os_strcmp(pos, "}") == 0) {
224 end = 1;
225 break;
226 }
227
228 len = os_strlen(pos);
229 nencoded = os_realloc(encoded, encoded_len + len);
230 if (nencoded == NULL) {
231 wpa_printf(MSG_ERROR, "Line %d: not enough memory for "
232 "blob", *line);
233 os_free(encoded);
234 return NULL;
235 }
236 encoded = nencoded;
237 os_memcpy(encoded + encoded_len, pos, len);
238 encoded_len += len;
239 }
240
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700241 if (!end || !encoded) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700242 wpa_printf(MSG_ERROR, "Line %d: blob was not terminated "
243 "properly", *line);
244 os_free(encoded);
245 return NULL;
246 }
247
248 blob = os_zalloc(sizeof(*blob));
249 if (blob == NULL) {
250 os_free(encoded);
251 return NULL;
252 }
253 blob->name = os_strdup(name);
254 blob->data = base64_decode(encoded, encoded_len, &blob->len);
255 os_free(encoded);
256
257 if (blob->name == NULL || blob->data == NULL) {
258 wpa_config_free_blob(blob);
259 return NULL;
260 }
261
262 return blob;
263}
264
265
266static int wpa_config_process_blob(struct wpa_config *config, FILE *f,
267 int *line, char *bname)
268{
269 char *name_end;
270 struct wpa_config_blob *blob;
271
272 name_end = os_strchr(bname, '=');
273 if (name_end == NULL) {
274 wpa_printf(MSG_ERROR, "Line %d: no blob name terminator",
275 *line);
276 return -1;
277 }
278 *name_end = '\0';
279
280 blob = wpa_config_read_blob(f, line, bname);
281 if (blob == NULL) {
282 wpa_printf(MSG_ERROR, "Line %d: failed to read blob %s",
283 *line, bname);
284 return -1;
285 }
286 wpa_config_set_blob(config, blob);
287 return 0;
288}
289#endif /* CONFIG_NO_CONFIG_BLOBS */
290
291
Dmitry Shmidt64f47c52013-04-16 10:41:54 -0700292struct wpa_config * wpa_config_read(const char *name, struct wpa_config *cfgp)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700293{
294 FILE *f;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700295 char buf[512], *pos;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700296 int errors = 0, line = 0;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700297 struct wpa_ssid *ssid, *tail, *head;
298 struct wpa_cred *cred, *cred_tail, *cred_head;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700299 struct wpa_config *config;
300 int id = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700301 int cred_id = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700302
Dmitry Shmidt64f47c52013-04-16 10:41:54 -0700303 if (name == NULL)
304 return NULL;
305 if (cfgp)
306 config = cfgp;
307 else
308 config = wpa_config_alloc_empty(NULL, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700309 if (config == NULL) {
310 wpa_printf(MSG_ERROR, "Failed to allocate config file "
311 "structure");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700312 return NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700313 }
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700314 tail = head = config->ssid;
315 while (tail && tail->next)
316 tail = tail->next;
317 cred_tail = cred_head = config->cred;
318 while (cred_tail && cred_tail->next)
319 cred_tail = cred_tail->next;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700320
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700321 wpa_printf(MSG_DEBUG, "Reading configuration file '%s'", name);
322 f = fopen(name, "r");
323 if (f == NULL) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700324 wpa_printf(MSG_ERROR, "Failed to open config file '%s', "
325 "error: %s", name, strerror(errno));
Dmitry Shmidte3d76d92018-02-01 00:34:54 +0000326 if (config != cfgp)
327 os_free(config);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700328 return NULL;
329 }
330
331 while (wpa_config_get_line(buf, sizeof(buf), f, &line, &pos)) {
332 if (os_strcmp(pos, "network={") == 0) {
333 ssid = wpa_config_read_network(f, &line, id++);
334 if (ssid == NULL) {
335 wpa_printf(MSG_ERROR, "Line %d: failed to "
336 "parse network block.", line);
337 errors++;
338 continue;
339 }
340 if (head == NULL) {
341 head = tail = ssid;
342 } else {
343 tail->next = ssid;
344 tail = ssid;
345 }
346 if (wpa_config_add_prio_network(config, ssid)) {
347 wpa_printf(MSG_ERROR, "Line %d: failed to add "
348 "network block to priority list.",
349 line);
350 errors++;
351 continue;
352 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700353 } else if (os_strcmp(pos, "cred={") == 0) {
354 cred = wpa_config_read_cred(f, &line, cred_id++);
355 if (cred == NULL) {
356 wpa_printf(MSG_ERROR, "Line %d: failed to "
357 "parse cred block.", line);
358 errors++;
359 continue;
360 }
361 if (cred_head == NULL) {
362 cred_head = cred_tail = cred;
363 } else {
364 cred_tail->next = cred;
365 cred_tail = cred;
366 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700367#ifndef CONFIG_NO_CONFIG_BLOBS
368 } else if (os_strncmp(pos, "blob-base64-", 12) == 0) {
369 if (wpa_config_process_blob(config, f, &line, pos + 12)
370 < 0) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700371 wpa_printf(MSG_ERROR, "Line %d: failed to "
372 "process blob.", line);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700373 errors++;
374 continue;
375 }
376#endif /* CONFIG_NO_CONFIG_BLOBS */
377 } else if (wpa_config_process_global(config, pos, line) < 0) {
378 wpa_printf(MSG_ERROR, "Line %d: Invalid configuration "
379 "line '%s'.", line, pos);
380 errors++;
381 continue;
382 }
383 }
384
385 fclose(f);
386
Iliyan Malchev97d98062013-04-23 02:37:51 +0000387 config->ssid = head;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700388 wpa_config_debug_dump_networks(config);
Iliyan Malchev97d98062013-04-23 02:37:51 +0000389 config->cred = cred_head;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700390
391#ifndef WPA_IGNORE_CONFIG_ERRORS
392 if (errors) {
Roshan Pius3a1667e2018-07-03 15:17:14 -0700393 if (config != cfgp)
394 wpa_config_free(config);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700395 config = NULL;
396 head = NULL;
397 }
398#endif /* WPA_IGNORE_CONFIG_ERRORS */
399
400 return config;
401}
402
403
404#ifndef CONFIG_NO_CONFIG_WRITE
405
406static void write_str(FILE *f, const char *field, struct wpa_ssid *ssid)
407{
408 char *value = wpa_config_get(ssid, field);
409 if (value == NULL)
410 return;
411 fprintf(f, "\t%s=%s\n", field, value);
Hai Shalom74f70d42019-02-11 14:42:39 -0800412 str_clear_free(value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700413}
414
415
416static void write_int(FILE *f, const char *field, int value, int def)
417{
418 if (value == def)
419 return;
420 fprintf(f, "\t%s=%d\n", field, value);
421}
422
423
424static void write_bssid(FILE *f, struct wpa_ssid *ssid)
425{
426 char *value = wpa_config_get(ssid, "bssid");
427 if (value == NULL)
428 return;
429 fprintf(f, "\tbssid=%s\n", value);
430 os_free(value);
431}
432
433
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700434static void write_bssid_hint(FILE *f, struct wpa_ssid *ssid)
435{
436 char *value = wpa_config_get(ssid, "bssid_hint");
437
438 if (!value)
439 return;
440 fprintf(f, "\tbssid_hint=%s\n", value);
441 os_free(value);
442}
443
444
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700445static void write_psk(FILE *f, struct wpa_ssid *ssid)
446{
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700447 char *value;
448
449 if (ssid->mem_only_psk)
450 return;
451
452 value = wpa_config_get(ssid, "psk");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700453 if (value == NULL)
454 return;
455 fprintf(f, "\tpsk=%s\n", value);
456 os_free(value);
457}
458
459
460static void write_proto(FILE *f, struct wpa_ssid *ssid)
461{
462 char *value;
463
464 if (ssid->proto == DEFAULT_PROTO)
465 return;
466
467 value = wpa_config_get(ssid, "proto");
468 if (value == NULL)
469 return;
470 if (value[0])
471 fprintf(f, "\tproto=%s\n", value);
472 os_free(value);
473}
474
475
476static void write_key_mgmt(FILE *f, struct wpa_ssid *ssid)
477{
478 char *value;
479
480 if (ssid->key_mgmt == DEFAULT_KEY_MGMT)
481 return;
482
483 value = wpa_config_get(ssid, "key_mgmt");
484 if (value == NULL)
485 return;
486 if (value[0])
487 fprintf(f, "\tkey_mgmt=%s\n", value);
488 os_free(value);
489}
490
491
492static void write_pairwise(FILE *f, struct wpa_ssid *ssid)
493{
494 char *value;
495
496 if (ssid->pairwise_cipher == DEFAULT_PAIRWISE)
497 return;
498
499 value = wpa_config_get(ssid, "pairwise");
500 if (value == NULL)
501 return;
502 if (value[0])
503 fprintf(f, "\tpairwise=%s\n", value);
504 os_free(value);
505}
506
507
508static void write_group(FILE *f, struct wpa_ssid *ssid)
509{
510 char *value;
511
512 if (ssid->group_cipher == DEFAULT_GROUP)
513 return;
514
515 value = wpa_config_get(ssid, "group");
516 if (value == NULL)
517 return;
518 if (value[0])
519 fprintf(f, "\tgroup=%s\n", value);
520 os_free(value);
521}
522
523
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700524static void write_group_mgmt(FILE *f, struct wpa_ssid *ssid)
525{
526 char *value;
527
528 if (!ssid->group_mgmt_cipher)
529 return;
530
531 value = wpa_config_get(ssid, "group_mgmt");
532 if (!value)
533 return;
534 if (value[0])
535 fprintf(f, "\tgroup_mgmt=%s\n", value);
536 os_free(value);
537}
538
539
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700540static void write_auth_alg(FILE *f, struct wpa_ssid *ssid)
541{
542 char *value;
543
544 if (ssid->auth_alg == 0)
545 return;
546
547 value = wpa_config_get(ssid, "auth_alg");
548 if (value == NULL)
549 return;
550 if (value[0])
551 fprintf(f, "\tauth_alg=%s\n", value);
552 os_free(value);
553}
554
555
556#ifdef IEEE8021X_EAPOL
557static void write_eap(FILE *f, struct wpa_ssid *ssid)
558{
559 char *value;
560
561 value = wpa_config_get(ssid, "eap");
562 if (value == NULL)
563 return;
564
565 if (value[0])
566 fprintf(f, "\teap=%s\n", value);
567 os_free(value);
568}
569#endif /* IEEE8021X_EAPOL */
570
571
Hai Shalomfdcde762020-04-02 11:19:20 -0700572#ifdef CONFIG_WEP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700573static void write_wep_key(FILE *f, int idx, struct wpa_ssid *ssid)
574{
575 char field[20], *value;
576 int res;
577
578 res = os_snprintf(field, sizeof(field), "wep_key%d", idx);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800579 if (os_snprintf_error(sizeof(field), res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700580 return;
581 value = wpa_config_get(ssid, field);
582 if (value) {
583 fprintf(f, "\t%s=%s\n", field, value);
584 os_free(value);
585 }
586}
Hai Shalomfdcde762020-04-02 11:19:20 -0700587#endif /* CONFIG_WEP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700588
589
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800590#ifdef CONFIG_P2P
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700591
Dmitry Shmidt54605472013-11-08 11:10:19 -0800592static void write_go_p2p_dev_addr(FILE *f, struct wpa_ssid *ssid)
593{
594 char *value = wpa_config_get(ssid, "go_p2p_dev_addr");
595 if (value == NULL)
596 return;
597 fprintf(f, "\tgo_p2p_dev_addr=%s\n", value);
598 os_free(value);
599}
600
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800601static void write_p2p_client_list(FILE *f, struct wpa_ssid *ssid)
602{
603 char *value = wpa_config_get(ssid, "p2p_client_list");
604 if (value == NULL)
605 return;
606 fprintf(f, "\tp2p_client_list=%s\n", value);
607 os_free(value);
608}
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700609
610
611static void write_psk_list(FILE *f, struct wpa_ssid *ssid)
612{
613 struct psk_list_entry *psk;
614 char hex[32 * 2 + 1];
615
616 dl_list_for_each(psk, &ssid->psk_list, struct psk_list_entry, list) {
617 wpa_snprintf_hex(hex, sizeof(hex), psk->psk, sizeof(psk->psk));
618 fprintf(f, "\tpsk_list=%s" MACSTR "-%s\n",
619 psk->p2p ? "P2P-" : "", MAC2STR(psk->addr), hex);
620 }
621}
622
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800623#endif /* CONFIG_P2P */
624
625
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800626#ifdef CONFIG_MACSEC
627
628static void write_mka_cak(FILE *f, struct wpa_ssid *ssid)
629{
630 char *value;
631
632 if (!(ssid->mka_psk_set & MKA_PSK_SET_CAK))
633 return;
634
635 value = wpa_config_get(ssid, "mka_cak");
636 if (!value)
637 return;
638 fprintf(f, "\tmka_cak=%s\n", value);
639 os_free(value);
640}
641
642
643static void write_mka_ckn(FILE *f, struct wpa_ssid *ssid)
644{
645 char *value;
646
647 if (!(ssid->mka_psk_set & MKA_PSK_SET_CKN))
648 return;
649
650 value = wpa_config_get(ssid, "mka_ckn");
651 if (!value)
652 return;
653 fprintf(f, "\tmka_ckn=%s\n", value);
654 os_free(value);
655}
656
657#endif /* CONFIG_MACSEC */
658
659
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700660static void wpa_config_write_network(FILE *f, struct wpa_ssid *ssid)
661{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700662#define STR(t) write_str(f, #t, ssid)
663#define INT(t) write_int(f, #t, ssid->t, 0)
Hai Shalomc3565922019-10-28 11:58:20 -0700664#define INTe(t, m) write_int(f, #t, ssid->eap.m, 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700665#define INT_DEF(t, def) write_int(f, #t, ssid->t, def)
Hai Shalomc3565922019-10-28 11:58:20 -0700666#define INT_DEFe(t, m, def) write_int(f, #t, ssid->eap.m, def)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700667
668 STR(ssid);
669 INT(scan_ssid);
670 write_bssid(f, ssid);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700671 write_bssid_hint(f, ssid);
Hai Shalom60840252021-02-19 19:02:11 -0800672 write_str(f, "bssid_ignore", ssid);
673 write_str(f, "bssid_accept", ssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700674 write_psk(f, ssid);
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -0700675 INT(mem_only_psk);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700676 STR(sae_password);
Roshan Pius3a1667e2018-07-03 15:17:14 -0700677 STR(sae_password_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700678 write_proto(f, ssid);
679 write_key_mgmt(f, ssid);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700680 INT_DEF(bg_scan_period, DEFAULT_BG_SCAN_PERIOD);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700681 write_pairwise(f, ssid);
682 write_group(f, ssid);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700683 write_group_mgmt(f, ssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700684 write_auth_alg(f, ssid);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700685 STR(bgscan);
686 STR(autoscan);
Dmitry Shmidt56052862013-10-04 10:23:25 -0700687 STR(scan_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700688#ifdef IEEE8021X_EAPOL
689 write_eap(f, ssid);
690 STR(identity);
691 STR(anonymous_identity);
Jouni Malinena3cb6f02017-12-08 17:05:40 +0200692 STR(imsi_identity);
Hai Shalomc3565922019-10-28 11:58:20 -0700693 STR(machine_identity);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700694 STR(password);
Hai Shalomc3565922019-10-28 11:58:20 -0700695 STR(machine_password);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700696 STR(ca_cert);
697 STR(ca_path);
698 STR(client_cert);
699 STR(private_key);
700 STR(private_key_passwd);
701 STR(dh_file);
702 STR(subject_match);
Hai Shalom021b0b52019-04-10 11:17:58 -0700703 STR(check_cert_subject);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700704 STR(altsubject_match);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700705 STR(domain_suffix_match);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800706 STR(domain_match);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700707 STR(ca_cert2);
708 STR(ca_path2);
709 STR(client_cert2);
710 STR(private_key2);
711 STR(private_key2_passwd);
712 STR(dh_file2);
713 STR(subject_match2);
Hai Shalom021b0b52019-04-10 11:17:58 -0700714 STR(check_cert_subject2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700715 STR(altsubject_match2);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700716 STR(domain_suffix_match2);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800717 STR(domain_match2);
Hai Shalomc3565922019-10-28 11:58:20 -0700718 STR(machine_ca_cert);
719 STR(machine_ca_path);
720 STR(machine_client_cert);
721 STR(machine_private_key);
722 STR(machine_private_key_passwd);
723 STR(machine_dh_file);
724 STR(machine_subject_match);
725 STR(machine_check_cert_subject);
726 STR(machine_altsubject_match);
727 STR(machine_domain_suffix_match);
728 STR(machine_domain_match);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700729 STR(phase1);
730 STR(phase2);
Hai Shalomc3565922019-10-28 11:58:20 -0700731 STR(machine_phase2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700732 STR(pcsc);
733 STR(pin);
734 STR(engine_id);
735 STR(key_id);
736 STR(cert_id);
737 STR(ca_cert_id);
738 STR(key2_id);
739 STR(pin2);
740 STR(engine2_id);
741 STR(cert2_id);
742 STR(ca_cert2_id);
Hai Shalomc3565922019-10-28 11:58:20 -0700743 INTe(engine, cert.engine);
744 INTe(engine2, phase2_cert.engine);
745 INTe(machine_engine, machine_cert.engine);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700746 INT_DEF(eapol_flags, DEFAULT_EAPOL_FLAGS);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800747 STR(openssl_ciphers);
Hai Shalomc3565922019-10-28 11:58:20 -0700748 INTe(erp, erp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700749#endif /* IEEE8021X_EAPOL */
Hai Shalomfdcde762020-04-02 11:19:20 -0700750#ifdef CONFIG_WEP
751 {
752 int i;
753
754 for (i = 0; i < 4; i++)
755 write_wep_key(f, i, ssid);
756 INT(wep_tx_keyidx);
757 }
758#endif /* CONFIG_WEP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700759 INT(priority);
760#ifdef IEEE8021X_EAPOL
761 INT_DEF(eap_workaround, DEFAULT_EAP_WORKAROUND);
762 STR(pac_file);
Hai Shalomc3565922019-10-28 11:58:20 -0700763 INT_DEFe(fragment_size, fragment_size, DEFAULT_FRAGMENT_SIZE);
764 INTe(ocsp, cert.ocsp);
765 INTe(ocsp2, phase2_cert.ocsp);
766 INTe(machine_ocsp, machine_cert.ocsp);
767 INT_DEFe(sim_num, sim_num, DEFAULT_USER_SELECTED_SIM);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700768#endif /* IEEE8021X_EAPOL */
769 INT(mode);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800770 INT(no_auto_peer);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800771 INT(frequency);
Hai Shalomc3565922019-10-28 11:58:20 -0700772 INT(enable_edmg);
773 INT(edmg_channel);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800774 INT(fixed_freq);
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -0800775#ifdef CONFIG_ACS
776 INT(acs);
777#endif /* CONFIG_ACS */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800778 write_int(f, "proactive_key_caching", ssid->proactive_key_caching, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700779 INT(disabled);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800780 INT(mixed_cell);
Hai Shalom899fcc72020-10-19 14:38:18 -0700781 INT_DEF(vht, 1);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700782 INT_DEF(ht, 1);
783 INT(ht40);
Hai Shalom899fcc72020-10-19 14:38:18 -0700784 INT_DEF(he, 1);
Hai Shalom74f70d42019-02-11 14:42:39 -0800785 INT_DEF(max_oper_chwidth, DEFAULT_MAX_OPER_CHWIDTH);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700786 INT(vht_center_freq1);
787 INT(vht_center_freq2);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800788 INT(pbss);
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700789 INT(wps_disabled);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700790 INT(fils_dh_group);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800791 write_int(f, "ieee80211w", ssid->ieee80211w,
792 MGMT_FRAME_PROTECTION_DEFAULT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700793 STR(id_str);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800794#ifdef CONFIG_P2P
Dmitry Shmidt54605472013-11-08 11:10:19 -0800795 write_go_p2p_dev_addr(f, ssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800796 write_p2p_client_list(f, ssid);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700797 write_psk_list(f, ssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800798#endif /* CONFIG_P2P */
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800799 INT(ap_max_inactivity);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -0800800 INT(dtim_period);
801 INT(beacon_int);
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -0700802#ifdef CONFIG_MACSEC
803 INT(macsec_policy);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800804 write_mka_cak(f, ssid);
805 write_mka_ckn(f, ssid);
806 INT(macsec_integ_only);
Hai Shalom74f70d42019-02-11 14:42:39 -0800807 INT(macsec_replay_protect);
808 INT(macsec_replay_window);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800809 INT(macsec_port);
Dmitry Shmidt29333592017-01-09 12:27:11 -0800810 INT_DEF(mka_priority, DEFAULT_PRIO_NOT_KEY_SERVER);
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -0700811#endif /* CONFIG_MACSEC */
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700812#ifdef CONFIG_HS20
813 INT(update_identifier);
Roshan Pius3a1667e2018-07-03 15:17:14 -0700814 STR(roaming_consortium_selection);
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700815#endif /* CONFIG_HS20 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700816 write_int(f, "mac_addr", ssid->mac_addr, -1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800817#ifdef CONFIG_MESH
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800818 STR(mesh_basic_rates);
819 INT_DEF(dot11MeshMaxRetries, DEFAULT_MESH_MAX_RETRIES);
820 INT_DEF(dot11MeshRetryTimeout, DEFAULT_MESH_RETRY_TIMEOUT);
821 INT_DEF(dot11MeshConfirmTimeout, DEFAULT_MESH_CONFIRM_TIMEOUT);
822 INT_DEF(dot11MeshHoldingTimeout, DEFAULT_MESH_HOLDING_TIMEOUT);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700823 INT_DEF(mesh_rssi_threshold, DEFAULT_MESH_RSSI_THRESHOLD);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800824#endif /* CONFIG_MESH */
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800825 INT(wpa_ptk_rekey);
Hai Shalomfdcde762020-04-02 11:19:20 -0700826 INT(wpa_deny_ptk0_rekey);
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700827 INT(group_rekey);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800828 INT(ignore_broadcast_ssid);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700829#ifdef CONFIG_DPP
830 STR(dpp_connector);
831 STR(dpp_netaccesskey);
832 INT(dpp_netaccesskey_expiry);
833 STR(dpp_csign);
Hai Shalom899fcc72020-10-19 14:38:18 -0700834 STR(dpp_pp_key);
Hai Shalomfdcde762020-04-02 11:19:20 -0700835 INT(dpp_pfs);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700836#endif /* CONFIG_DPP */
837 INT(owe_group);
Roshan Pius3a1667e2018-07-03 15:17:14 -0700838 INT(owe_only);
Hai Shalomfdcde762020-04-02 11:19:20 -0700839 INT(owe_ptk_workaround);
Hai Shalom74f70d42019-02-11 14:42:39 -0800840 INT(multi_ap_backhaul_sta);
Hai Shalom81f62d82019-07-22 12:10:00 -0700841 INT(ft_eap_pmksa_caching);
Hai Shalomfdcde762020-04-02 11:19:20 -0700842 INT(beacon_prot);
843 INT(transition_disable);
Hai Shalom899fcc72020-10-19 14:38:18 -0700844 INT(sae_pk);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800845#ifdef CONFIG_HT_OVERRIDES
846 INT_DEF(disable_ht, DEFAULT_DISABLE_HT);
847 INT_DEF(disable_ht40, DEFAULT_DISABLE_HT40);
848 INT_DEF(disable_sgi, DEFAULT_DISABLE_SGI);
849 INT_DEF(disable_ldpc, DEFAULT_DISABLE_LDPC);
850 INT(ht40_intolerant);
Hai Shalom74f70d42019-02-11 14:42:39 -0800851 INT_DEF(tx_stbc, DEFAULT_TX_STBC);
852 INT_DEF(rx_stbc, DEFAULT_RX_STBC);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800853 INT_DEF(disable_max_amsdu, DEFAULT_DISABLE_MAX_AMSDU);
854 INT_DEF(ampdu_factor, DEFAULT_AMPDU_FACTOR);
855 INT_DEF(ampdu_density, DEFAULT_AMPDU_DENSITY);
856 STR(ht_mcs);
857#endif /* CONFIG_HT_OVERRIDES */
858#ifdef CONFIG_VHT_OVERRIDES
859 INT(disable_vht);
860 INT(vht_capa);
861 INT(vht_capa_mask);
862 INT_DEF(vht_rx_mcs_nss_1, -1);
863 INT_DEF(vht_rx_mcs_nss_2, -1);
864 INT_DEF(vht_rx_mcs_nss_3, -1);
865 INT_DEF(vht_rx_mcs_nss_4, -1);
866 INT_DEF(vht_rx_mcs_nss_5, -1);
867 INT_DEF(vht_rx_mcs_nss_6, -1);
868 INT_DEF(vht_rx_mcs_nss_7, -1);
869 INT_DEF(vht_rx_mcs_nss_8, -1);
870 INT_DEF(vht_tx_mcs_nss_1, -1);
871 INT_DEF(vht_tx_mcs_nss_2, -1);
872 INT_DEF(vht_tx_mcs_nss_3, -1);
873 INT_DEF(vht_tx_mcs_nss_4, -1);
874 INT_DEF(vht_tx_mcs_nss_5, -1);
875 INT_DEF(vht_tx_mcs_nss_6, -1);
876 INT_DEF(vht_tx_mcs_nss_7, -1);
877 INT_DEF(vht_tx_mcs_nss_8, -1);
878#endif /* CONFIG_VHT_OVERRIDES */
Hai Shalomfdcde762020-04-02 11:19:20 -0700879#ifdef CONFIG_HE_OVERRIDES
880 INT(disable_he);
881#endif /* CONFIG_HE_OVERRIDES */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700882
883#undef STR
884#undef INT
885#undef INT_DEF
886}
887
888
Dmitry Shmidt04949592012-07-19 12:16:46 -0700889static void wpa_config_write_cred(FILE *f, struct wpa_cred *cred)
890{
Dmitry Shmidt051af732013-10-22 13:52:46 -0700891 size_t i;
892
Dmitry Shmidt04949592012-07-19 12:16:46 -0700893 if (cred->priority)
894 fprintf(f, "\tpriority=%d\n", cred->priority);
895 if (cred->pcsc)
896 fprintf(f, "\tpcsc=%d\n", cred->pcsc);
897 if (cred->realm)
898 fprintf(f, "\trealm=\"%s\"\n", cred->realm);
899 if (cred->username)
900 fprintf(f, "\tusername=\"%s\"\n", cred->username);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800901 if (cred->password && cred->ext_password)
902 fprintf(f, "\tpassword=ext:%s\n", cred->password);
903 else if (cred->password)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700904 fprintf(f, "\tpassword=\"%s\"\n", cred->password);
905 if (cred->ca_cert)
906 fprintf(f, "\tca_cert=\"%s\"\n", cred->ca_cert);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800907 if (cred->client_cert)
908 fprintf(f, "\tclient_cert=\"%s\"\n", cred->client_cert);
909 if (cred->private_key)
910 fprintf(f, "\tprivate_key=\"%s\"\n", cred->private_key);
911 if (cred->private_key_passwd)
912 fprintf(f, "\tprivate_key_passwd=\"%s\"\n",
913 cred->private_key_passwd);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700914 if (cred->imsi)
915 fprintf(f, "\timsi=\"%s\"\n", cred->imsi);
916 if (cred->milenage)
917 fprintf(f, "\tmilenage=\"%s\"\n", cred->milenage);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700918 for (i = 0; i < cred->num_domain; i++)
919 fprintf(f, "\tdomain=\"%s\"\n", cred->domain[i]);
920 if (cred->domain_suffix_match)
Dmitry Shmidtb58836e2014-04-29 14:35:56 -0700921 fprintf(f, "\tdomain_suffix_match=\"%s\"\n",
Dmitry Shmidt051af732013-10-22 13:52:46 -0700922 cred->domain_suffix_match);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800923 if (cred->roaming_consortium_len) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800924 fprintf(f, "\troaming_consortium=");
925 for (i = 0; i < cred->roaming_consortium_len; i++)
926 fprintf(f, "%02x", cred->roaming_consortium[i]);
927 fprintf(f, "\n");
928 }
929 if (cred->eap_method) {
930 const char *name;
931 name = eap_get_name(cred->eap_method[0].vendor,
932 cred->eap_method[0].method);
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -0700933 if (name)
934 fprintf(f, "\teap=%s\n", name);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800935 }
936 if (cred->phase1)
937 fprintf(f, "\tphase1=\"%s\"\n", cred->phase1);
938 if (cred->phase2)
939 fprintf(f, "\tphase2=\"%s\"\n", cred->phase2);
940 if (cred->excluded_ssid) {
Dmitry Shmidt051af732013-10-22 13:52:46 -0700941 size_t j;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800942 for (i = 0; i < cred->num_excluded_ssid; i++) {
943 struct excluded_ssid *e = &cred->excluded_ssid[i];
944 fprintf(f, "\texcluded_ssid=");
945 for (j = 0; j < e->ssid_len; j++)
946 fprintf(f, "%02x", e->ssid[j]);
947 fprintf(f, "\n");
948 }
949 }
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800950 if (cred->roaming_partner) {
951 for (i = 0; i < cred->num_roaming_partner; i++) {
952 struct roaming_partner *p = &cred->roaming_partner[i];
953 fprintf(f, "\troaming_partner=\"%s,%d,%u,%s\"\n",
954 p->fqdn, p->exact_match, p->priority,
955 p->country);
956 }
957 }
958 if (cred->update_identifier)
959 fprintf(f, "\tupdate_identifier=%d\n", cred->update_identifier);
960
961 if (cred->provisioning_sp)
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700962 fprintf(f, "\tprovisioning_sp=\"%s\"\n", cred->provisioning_sp);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800963 if (cred->sp_priority)
964 fprintf(f, "\tsp_priority=%d\n", cred->sp_priority);
965
966 if (cred->min_dl_bandwidth_home)
967 fprintf(f, "\tmin_dl_bandwidth_home=%u\n",
968 cred->min_dl_bandwidth_home);
969 if (cred->min_ul_bandwidth_home)
970 fprintf(f, "\tmin_ul_bandwidth_home=%u\n",
971 cred->min_ul_bandwidth_home);
972 if (cred->min_dl_bandwidth_roaming)
973 fprintf(f, "\tmin_dl_bandwidth_roaming=%u\n",
974 cred->min_dl_bandwidth_roaming);
975 if (cred->min_ul_bandwidth_roaming)
976 fprintf(f, "\tmin_ul_bandwidth_roaming=%u\n",
977 cred->min_ul_bandwidth_roaming);
978
979 if (cred->max_bss_load)
980 fprintf(f, "\tmax_bss_load=%u\n",
981 cred->max_bss_load);
982
983 if (cred->ocsp)
984 fprintf(f, "\tocsp=%d\n", cred->ocsp);
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -0700985
986 if (cred->num_req_conn_capab) {
987 for (i = 0; i < cred->num_req_conn_capab; i++) {
988 int *ports;
989
990 fprintf(f, "\treq_conn_capab=%u",
991 cred->req_conn_capab_proto[i]);
992 ports = cred->req_conn_capab_port[i];
993 if (ports) {
994 int j;
995 for (j = 0; ports[j] != -1; j++) {
996 fprintf(f, "%s%d", j > 0 ? "," : ":",
997 ports[j]);
998 }
999 }
1000 fprintf(f, "\n");
1001 }
1002 }
1003
1004 if (cred->required_roaming_consortium_len) {
1005 fprintf(f, "\trequired_roaming_consortium=");
1006 for (i = 0; i < cred->required_roaming_consortium_len; i++)
1007 fprintf(f, "%02x",
1008 cred->required_roaming_consortium[i]);
1009 fprintf(f, "\n");
1010 }
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -07001011
Roshan Pius3a1667e2018-07-03 15:17:14 -07001012 if (cred->num_roaming_consortiums) {
1013 size_t j;
1014
1015 fprintf(f, "\troaming_consortiums=\"");
1016 for (i = 0; i < cred->num_roaming_consortiums; i++) {
1017 if (i > 0)
1018 fprintf(f, ",");
1019 for (j = 0; j < cred->roaming_consortiums_len[i]; j++)
1020 fprintf(f, "%02x",
1021 cred->roaming_consortiums[i][j]);
1022 }
1023 fprintf(f, "\"\n");
1024 }
1025
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -07001026 if (cred->sim_num != DEFAULT_USER_SELECTED_SIM)
1027 fprintf(f, "\tsim_num=%d\n", cred->sim_num);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001028}
1029
1030
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001031#ifndef CONFIG_NO_CONFIG_BLOBS
1032static int wpa_config_write_blob(FILE *f, struct wpa_config_blob *blob)
1033{
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001034 char *encoded;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001035
1036 encoded = base64_encode(blob->data, blob->len, NULL);
1037 if (encoded == NULL)
1038 return -1;
1039
1040 fprintf(f, "\nblob-base64-%s={\n%s}\n", blob->name, encoded);
1041 os_free(encoded);
1042 return 0;
1043}
1044#endif /* CONFIG_NO_CONFIG_BLOBS */
1045
1046
Dmitry Shmidt04949592012-07-19 12:16:46 -07001047static void write_global_bin(FILE *f, const char *field,
1048 const struct wpabuf *val)
1049{
1050 size_t i;
1051 const u8 *pos;
1052
1053 if (val == NULL)
1054 return;
1055
1056 fprintf(f, "%s=", field);
1057 pos = wpabuf_head(val);
1058 for (i = 0; i < wpabuf_len(val); i++)
1059 fprintf(f, "%02X", *pos++);
1060 fprintf(f, "\n");
1061}
1062
1063
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001064static void wpa_config_write_global(FILE *f, struct wpa_config *config)
1065{
1066#ifdef CONFIG_CTRL_IFACE
1067 if (config->ctrl_interface)
1068 fprintf(f, "ctrl_interface=%s\n", config->ctrl_interface);
1069 if (config->ctrl_interface_group)
1070 fprintf(f, "ctrl_interface_group=%s\n",
1071 config->ctrl_interface_group);
1072#endif /* CONFIG_CTRL_IFACE */
1073 if (config->eapol_version != DEFAULT_EAPOL_VERSION)
1074 fprintf(f, "eapol_version=%d\n", config->eapol_version);
1075 if (config->ap_scan != DEFAULT_AP_SCAN)
1076 fprintf(f, "ap_scan=%d\n", config->ap_scan);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001077 if (config->disable_scan_offload)
1078 fprintf(f, "disable_scan_offload=%d\n",
1079 config->disable_scan_offload);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001080 if (config->fast_reauth != DEFAULT_FAST_REAUTH)
1081 fprintf(f, "fast_reauth=%d\n", config->fast_reauth);
1082 if (config->opensc_engine_path)
1083 fprintf(f, "opensc_engine_path=%s\n",
1084 config->opensc_engine_path);
1085 if (config->pkcs11_engine_path)
1086 fprintf(f, "pkcs11_engine_path=%s\n",
1087 config->pkcs11_engine_path);
1088 if (config->pkcs11_module_path)
1089 fprintf(f, "pkcs11_module_path=%s\n",
1090 config->pkcs11_module_path);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001091 if (config->openssl_ciphers)
1092 fprintf(f, "openssl_ciphers=%s\n", config->openssl_ciphers);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001093 if (config->pcsc_reader)
1094 fprintf(f, "pcsc_reader=%s\n", config->pcsc_reader);
1095 if (config->pcsc_pin)
1096 fprintf(f, "pcsc_pin=%s\n", config->pcsc_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001097 if (config->driver_param)
1098 fprintf(f, "driver_param=%s\n", config->driver_param);
1099 if (config->dot11RSNAConfigPMKLifetime)
Dmitry Shmidt41712582015-06-29 11:02:15 -07001100 fprintf(f, "dot11RSNAConfigPMKLifetime=%u\n",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001101 config->dot11RSNAConfigPMKLifetime);
1102 if (config->dot11RSNAConfigPMKReauthThreshold)
Dmitry Shmidt41712582015-06-29 11:02:15 -07001103 fprintf(f, "dot11RSNAConfigPMKReauthThreshold=%u\n",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001104 config->dot11RSNAConfigPMKReauthThreshold);
1105 if (config->dot11RSNAConfigSATimeout)
Dmitry Shmidt41712582015-06-29 11:02:15 -07001106 fprintf(f, "dot11RSNAConfigSATimeout=%u\n",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001107 config->dot11RSNAConfigSATimeout);
1108 if (config->update_config)
1109 fprintf(f, "update_config=%d\n", config->update_config);
1110#ifdef CONFIG_WPS
1111 if (!is_nil_uuid(config->uuid)) {
1112 char buf[40];
1113 uuid_bin2str(config->uuid, buf, sizeof(buf));
1114 fprintf(f, "uuid=%s\n", buf);
1115 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001116 if (config->auto_uuid)
1117 fprintf(f, "auto_uuid=%d\n", config->auto_uuid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001118 if (config->device_name)
1119 fprintf(f, "device_name=%s\n", config->device_name);
1120 if (config->manufacturer)
1121 fprintf(f, "manufacturer=%s\n", config->manufacturer);
1122 if (config->model_name)
1123 fprintf(f, "model_name=%s\n", config->model_name);
1124 if (config->model_number)
1125 fprintf(f, "model_number=%s\n", config->model_number);
1126 if (config->serial_number)
1127 fprintf(f, "serial_number=%s\n", config->serial_number);
1128 {
1129 char _buf[WPS_DEV_TYPE_BUFSIZE], *buf;
1130 buf = wps_dev_type_bin2str(config->device_type,
1131 _buf, sizeof(_buf));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001132 if (os_strcmp(buf, "0-00000000-0") != 0)
1133 fprintf(f, "device_type=%s\n", buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001134 }
1135 if (WPA_GET_BE32(config->os_version))
1136 fprintf(f, "os_version=%08x\n",
1137 WPA_GET_BE32(config->os_version));
1138 if (config->config_methods)
1139 fprintf(f, "config_methods=%s\n", config->config_methods);
1140 if (config->wps_cred_processing)
1141 fprintf(f, "wps_cred_processing=%d\n",
1142 config->wps_cred_processing);
Hai Shalom021b0b52019-04-10 11:17:58 -07001143 if (config->wps_cred_add_sae)
1144 fprintf(f, "wps_cred_add_sae=%d\n",
1145 config->wps_cred_add_sae);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001146 if (config->wps_vendor_ext_m1) {
1147 int i, len = wpabuf_len(config->wps_vendor_ext_m1);
1148 const u8 *p = wpabuf_head_u8(config->wps_vendor_ext_m1);
1149 if (len > 0) {
1150 fprintf(f, "wps_vendor_ext_m1=");
1151 for (i = 0; i < len; i++)
1152 fprintf(f, "%02x", *p++);
1153 fprintf(f, "\n");
1154 }
1155 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001156#endif /* CONFIG_WPS */
1157#ifdef CONFIG_P2P
Paul Stewart092955c2017-02-06 09:13:09 -08001158 {
1159 int i;
1160 char _buf[WPS_DEV_TYPE_BUFSIZE], *buf;
1161
1162 for (i = 0; i < config->num_sec_device_types; i++) {
1163 buf = wps_dev_type_bin2str(config->sec_device_type[i],
1164 _buf, sizeof(_buf));
1165 if (buf)
1166 fprintf(f, "sec_device_type=%s\n", buf);
1167 }
1168 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001169 if (config->p2p_listen_reg_class)
Dmitry Shmidt41712582015-06-29 11:02:15 -07001170 fprintf(f, "p2p_listen_reg_class=%d\n",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001171 config->p2p_listen_reg_class);
1172 if (config->p2p_listen_channel)
Dmitry Shmidt41712582015-06-29 11:02:15 -07001173 fprintf(f, "p2p_listen_channel=%d\n",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001174 config->p2p_listen_channel);
1175 if (config->p2p_oper_reg_class)
Dmitry Shmidt41712582015-06-29 11:02:15 -07001176 fprintf(f, "p2p_oper_reg_class=%d\n",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001177 config->p2p_oper_reg_class);
1178 if (config->p2p_oper_channel)
Dmitry Shmidt41712582015-06-29 11:02:15 -07001179 fprintf(f, "p2p_oper_channel=%d\n", config->p2p_oper_channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001180 if (config->p2p_go_intent != DEFAULT_P2P_GO_INTENT)
Dmitry Shmidt41712582015-06-29 11:02:15 -07001181 fprintf(f, "p2p_go_intent=%d\n", config->p2p_go_intent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001182 if (config->p2p_ssid_postfix)
1183 fprintf(f, "p2p_ssid_postfix=%s\n", config->p2p_ssid_postfix);
1184 if (config->persistent_reconnect)
Dmitry Shmidt41712582015-06-29 11:02:15 -07001185 fprintf(f, "persistent_reconnect=%d\n",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001186 config->persistent_reconnect);
1187 if (config->p2p_intra_bss != DEFAULT_P2P_INTRA_BSS)
Dmitry Shmidt41712582015-06-29 11:02:15 -07001188 fprintf(f, "p2p_intra_bss=%d\n", config->p2p_intra_bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001189 if (config->p2p_group_idle)
Dmitry Shmidt41712582015-06-29 11:02:15 -07001190 fprintf(f, "p2p_group_idle=%d\n", config->p2p_group_idle);
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07001191 if (config->p2p_passphrase_len)
1192 fprintf(f, "p2p_passphrase_len=%u\n",
1193 config->p2p_passphrase_len);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001194 if (config->p2p_pref_chan) {
1195 unsigned int i;
1196 fprintf(f, "p2p_pref_chan=");
1197 for (i = 0; i < config->num_p2p_pref_chan; i++) {
1198 fprintf(f, "%s%u:%u", i > 0 ? "," : "",
1199 config->p2p_pref_chan[i].op_class,
1200 config->p2p_pref_chan[i].chan);
1201 }
1202 fprintf(f, "\n");
1203 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001204 if (config->p2p_no_go_freq.num) {
1205 char *val = freq_range_list_str(&config->p2p_no_go_freq);
1206 if (val) {
1207 fprintf(f, "p2p_no_go_freq=%s\n", val);
1208 os_free(val);
1209 }
1210 }
1211 if (config->p2p_add_cli_chan)
1212 fprintf(f, "p2p_add_cli_chan=%d\n", config->p2p_add_cli_chan);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001213 if (config->p2p_optimize_listen_chan !=
1214 DEFAULT_P2P_OPTIMIZE_LISTEN_CHAN)
1215 fprintf(f, "p2p_optimize_listen_chan=%d\n",
1216 config->p2p_optimize_listen_chan);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001217 if (config->p2p_go_ht40)
Dmitry Shmidt41712582015-06-29 11:02:15 -07001218 fprintf(f, "p2p_go_ht40=%d\n", config->p2p_go_ht40);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001219 if (config->p2p_go_vht)
Dmitry Shmidt41712582015-06-29 11:02:15 -07001220 fprintf(f, "p2p_go_vht=%d\n", config->p2p_go_vht);
Hai Shalom74f70d42019-02-11 14:42:39 -08001221 if (config->p2p_go_he)
1222 fprintf(f, "p2p_go_he=%d\n", config->p2p_go_he);
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08001223 if (config->p2p_go_edmg)
1224 fprintf(f, "p2p_go_edmg=%d\n", config->p2p_go_edmg);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001225 if (config->p2p_go_ctwindow != DEFAULT_P2P_GO_CTWINDOW)
Dmitry Shmidt41712582015-06-29 11:02:15 -07001226 fprintf(f, "p2p_go_ctwindow=%d\n", config->p2p_go_ctwindow);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001227 if (config->p2p_disabled)
Dmitry Shmidt41712582015-06-29 11:02:15 -07001228 fprintf(f, "p2p_disabled=%d\n", config->p2p_disabled);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001229 if (config->p2p_no_group_iface)
Dmitry Shmidt41712582015-06-29 11:02:15 -07001230 fprintf(f, "p2p_no_group_iface=%d\n",
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001231 config->p2p_no_group_iface);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07001232 if (config->p2p_ignore_shared_freq)
Dmitry Shmidt41712582015-06-29 11:02:15 -07001233 fprintf(f, "p2p_ignore_shared_freq=%d\n",
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07001234 config->p2p_ignore_shared_freq);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001235 if (config->p2p_cli_probe)
Dmitry Shmidt41712582015-06-29 11:02:15 -07001236 fprintf(f, "p2p_cli_probe=%d\n", config->p2p_cli_probe);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001237 if (config->p2p_go_freq_change_policy != DEFAULT_P2P_GO_FREQ_MOVE)
1238 fprintf(f, "p2p_go_freq_change_policy=%u\n",
1239 config->p2p_go_freq_change_policy);
Hai Shalom899fcc72020-10-19 14:38:18 -07001240
1241 if (config->p2p_6ghz_disable)
1242 fprintf(f, "p2p_6ghz_disable=%d\n", config->p2p_6ghz_disable);
1243
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08001244 if (WPA_GET_BE32(config->ip_addr_go))
1245 fprintf(f, "ip_addr_go=%u.%u.%u.%u\n",
1246 config->ip_addr_go[0], config->ip_addr_go[1],
1247 config->ip_addr_go[2], config->ip_addr_go[3]);
1248 if (WPA_GET_BE32(config->ip_addr_mask))
1249 fprintf(f, "ip_addr_mask=%u.%u.%u.%u\n",
1250 config->ip_addr_mask[0], config->ip_addr_mask[1],
1251 config->ip_addr_mask[2], config->ip_addr_mask[3]);
1252 if (WPA_GET_BE32(config->ip_addr_start))
1253 fprintf(f, "ip_addr_start=%u.%u.%u.%u\n",
1254 config->ip_addr_start[0], config->ip_addr_start[1],
1255 config->ip_addr_start[2], config->ip_addr_start[3]);
1256 if (WPA_GET_BE32(config->ip_addr_end))
1257 fprintf(f, "ip_addr_end=%u.%u.%u.%u\n",
1258 config->ip_addr_end[0], config->ip_addr_end[1],
1259 config->ip_addr_end[2], config->ip_addr_end[3]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001260#endif /* CONFIG_P2P */
1261 if (config->country[0] && config->country[1]) {
1262 fprintf(f, "country=%c%c\n",
1263 config->country[0], config->country[1]);
1264 }
1265 if (config->bss_max_count != DEFAULT_BSS_MAX_COUNT)
1266 fprintf(f, "bss_max_count=%u\n", config->bss_max_count);
1267 if (config->bss_expiration_age != DEFAULT_BSS_EXPIRATION_AGE)
1268 fprintf(f, "bss_expiration_age=%u\n",
1269 config->bss_expiration_age);
1270 if (config->bss_expiration_scan_count !=
1271 DEFAULT_BSS_EXPIRATION_SCAN_COUNT)
1272 fprintf(f, "bss_expiration_scan_count=%u\n",
1273 config->bss_expiration_scan_count);
1274 if (config->filter_ssids)
1275 fprintf(f, "filter_ssids=%d\n", config->filter_ssids);
Paul Stewart092955c2017-02-06 09:13:09 -08001276 if (config->filter_rssi)
1277 fprintf(f, "filter_rssi=%d\n", config->filter_rssi);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001278 if (config->max_num_sta != DEFAULT_MAX_NUM_STA)
1279 fprintf(f, "max_num_sta=%u\n", config->max_num_sta);
Roshan Pius3a1667e2018-07-03 15:17:14 -07001280 if (config->ap_isolate != DEFAULT_AP_ISOLATE)
1281 fprintf(f, "ap_isolate=%u\n", config->ap_isolate);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001282 if (config->disassoc_low_ack)
Dmitry Shmidt41712582015-06-29 11:02:15 -07001283 fprintf(f, "disassoc_low_ack=%d\n", config->disassoc_low_ack);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001284#ifdef CONFIG_HS20
1285 if (config->hs20)
1286 fprintf(f, "hs20=1\n");
1287#endif /* CONFIG_HS20 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001288#ifdef CONFIG_INTERWORKING
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001289 if (config->interworking)
Dmitry Shmidt41712582015-06-29 11:02:15 -07001290 fprintf(f, "interworking=%d\n", config->interworking);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001291 if (!is_zero_ether_addr(config->hessid))
1292 fprintf(f, "hessid=" MACSTR "\n", MAC2STR(config->hessid));
1293 if (config->access_network_type != DEFAULT_ACCESS_NETWORK_TYPE)
1294 fprintf(f, "access_network_type=%d\n",
1295 config->access_network_type);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001296 if (config->go_interworking)
1297 fprintf(f, "go_interworking=%d\n", config->go_interworking);
1298 if (config->go_access_network_type)
1299 fprintf(f, "go_access_network_type=%d\n",
1300 config->go_access_network_type);
1301 if (config->go_internet)
1302 fprintf(f, "go_internet=%d\n", config->go_internet);
1303 if (config->go_venue_group)
1304 fprintf(f, "go_venue_group=%d\n", config->go_venue_group);
1305 if (config->go_venue_type)
1306 fprintf(f, "go_venue_type=%d\n", config->go_venue_type);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001307#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001308 if (config->pbc_in_m1)
Dmitry Shmidt41712582015-06-29 11:02:15 -07001309 fprintf(f, "pbc_in_m1=%d\n", config->pbc_in_m1);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001310 if (config->wps_nfc_pw_from_config) {
1311 if (config->wps_nfc_dev_pw_id)
1312 fprintf(f, "wps_nfc_dev_pw_id=%d\n",
1313 config->wps_nfc_dev_pw_id);
1314 write_global_bin(f, "wps_nfc_dh_pubkey",
1315 config->wps_nfc_dh_pubkey);
1316 write_global_bin(f, "wps_nfc_dh_privkey",
1317 config->wps_nfc_dh_privkey);
1318 write_global_bin(f, "wps_nfc_dev_pw", config->wps_nfc_dev_pw);
1319 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001320
1321 if (config->ext_password_backend)
1322 fprintf(f, "ext_password_backend=%s\n",
1323 config->ext_password_backend);
1324 if (config->p2p_go_max_inactivity != DEFAULT_P2P_GO_MAX_INACTIVITY)
1325 fprintf(f, "p2p_go_max_inactivity=%d\n",
1326 config->p2p_go_max_inactivity);
1327 if (config->auto_interworking)
1328 fprintf(f, "auto_interworking=%d\n",
1329 config->auto_interworking);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001330 if (config->okc)
1331 fprintf(f, "okc=%d\n", config->okc);
1332 if (config->pmf)
1333 fprintf(f, "pmf=%d\n", config->pmf);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08001334 if (config->dtim_period)
1335 fprintf(f, "dtim_period=%d\n", config->dtim_period);
1336 if (config->beacon_int)
1337 fprintf(f, "beacon_int=%d\n", config->beacon_int);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001338
1339 if (config->sae_groups) {
1340 int i;
1341 fprintf(f, "sae_groups=");
Paul Stewart092955c2017-02-06 09:13:09 -08001342 for (i = 0; config->sae_groups[i] > 0; i++) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001343 fprintf(f, "%s%d", i > 0 ? " " : "",
1344 config->sae_groups[i]);
1345 }
1346 fprintf(f, "\n");
1347 }
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07001348
Hai Shalomc3565922019-10-28 11:58:20 -07001349 if (config->sae_pwe)
1350 fprintf(f, "sae_pwe=%d\n", config->sae_pwe);
1351
1352 if (config->sae_pmkid_in_assoc)
1353 fprintf(f, "sae_pmkid_in_assoc=%d\n",
1354 config->sae_pmkid_in_assoc);
1355
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07001356 if (config->ap_vendor_elements) {
1357 int i, len = wpabuf_len(config->ap_vendor_elements);
1358 const u8 *p = wpabuf_head_u8(config->ap_vendor_elements);
1359 if (len > 0) {
1360 fprintf(f, "ap_vendor_elements=");
1361 for (i = 0; i < len; i++)
1362 fprintf(f, "%02x", *p++);
1363 fprintf(f, "\n");
1364 }
1365 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001366
1367 if (config->ignore_old_scan_res)
1368 fprintf(f, "ignore_old_scan_res=%d\n",
1369 config->ignore_old_scan_res);
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001370
1371 if (config->freq_list && config->freq_list[0]) {
1372 int i;
1373 fprintf(f, "freq_list=");
1374 for (i = 0; config->freq_list[i]; i++) {
Dmitry Shmidt41712582015-06-29 11:02:15 -07001375 fprintf(f, "%s%d", i > 0 ? " " : "",
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001376 config->freq_list[i]);
1377 }
1378 fprintf(f, "\n");
1379 }
Hai Shalom60840252021-02-19 19:02:11 -08001380 if (config->initial_freq_list && config->initial_freq_list[0]) {
1381 int i;
1382 fprintf(f, "initial_freq_list=");
1383 for (i = 0; config->initial_freq_list[i]; i++) {
1384 fprintf(f, "%s%d", i > 0 ? " " : "",
1385 config->initial_freq_list[i]);
1386 }
1387 fprintf(f, "\n");
1388 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001389 if (config->scan_cur_freq != DEFAULT_SCAN_CUR_FREQ)
1390 fprintf(f, "scan_cur_freq=%d\n", config->scan_cur_freq);
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001391
Hai Shalom60840252021-02-19 19:02:11 -08001392 if (config->scan_res_valid_for_connect !=
1393 DEFAULT_SCAN_RES_VALID_FOR_CONNECT)
1394 fprintf(f, "scan_res_valid_for_connect=%d\n",
1395 config->scan_res_valid_for_connect);
1396
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001397 if (config->sched_scan_interval)
1398 fprintf(f, "sched_scan_interval=%u\n",
1399 config->sched_scan_interval);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001400
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001401 if (config->sched_scan_start_delay)
1402 fprintf(f, "sched_scan_start_delay=%u\n",
1403 config->sched_scan_start_delay);
1404
Dmitry Shmidt051af732013-10-22 13:52:46 -07001405 if (config->external_sim)
1406 fprintf(f, "external_sim=%d\n", config->external_sim);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001407
1408 if (config->tdls_external_control)
1409 fprintf(f, "tdls_external_control=%d\n",
1410 config->tdls_external_control);
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001411
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07001412 if (config->wowlan_triggers)
Dmitry Shmidt03658832014-08-13 11:03:49 -07001413 fprintf(f, "wowlan_triggers=%s\n",
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07001414 config->wowlan_triggers);
1415
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001416 if (config->bgscan)
1417 fprintf(f, "bgscan=\"%s\"\n", config->bgscan);
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07001418
Paul Stewart092955c2017-02-06 09:13:09 -08001419 if (config->autoscan)
1420 fprintf(f, "autoscan=%s\n", config->autoscan);
1421
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07001422 if (config->p2p_search_delay != DEFAULT_P2P_SEARCH_DELAY)
1423 fprintf(f, "p2p_search_delay=%u\n",
1424 config->p2p_search_delay);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001425
1426 if (config->mac_addr)
1427 fprintf(f, "mac_addr=%d\n", config->mac_addr);
1428
1429 if (config->rand_addr_lifetime != DEFAULT_RAND_ADDR_LIFETIME)
1430 fprintf(f, "rand_addr_lifetime=%u\n",
1431 config->rand_addr_lifetime);
1432
1433 if (config->preassoc_mac_addr)
1434 fprintf(f, "preassoc_mac_addr=%d\n", config->preassoc_mac_addr);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001435
1436 if (config->key_mgmt_offload != DEFAULT_KEY_MGMT_OFFLOAD)
Dmitry Shmidt41712582015-06-29 11:02:15 -07001437 fprintf(f, "key_mgmt_offload=%d\n", config->key_mgmt_offload);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001438
1439 if (config->user_mpm != DEFAULT_USER_MPM)
1440 fprintf(f, "user_mpm=%d\n", config->user_mpm);
1441
1442 if (config->max_peer_links != DEFAULT_MAX_PEER_LINKS)
1443 fprintf(f, "max_peer_links=%d\n", config->max_peer_links);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001444
1445 if (config->cert_in_cb != DEFAULT_CERT_IN_CB)
1446 fprintf(f, "cert_in_cb=%d\n", config->cert_in_cb);
1447
1448 if (config->mesh_max_inactivity != DEFAULT_MESH_MAX_INACTIVITY)
1449 fprintf(f, "mesh_max_inactivity=%d\n",
1450 config->mesh_max_inactivity);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001451
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001452 if (config->dot11RSNASAERetransPeriod !=
1453 DEFAULT_DOT11_RSNA_SAE_RETRANS_PERIOD)
1454 fprintf(f, "dot11RSNASAERetransPeriod=%d\n",
1455 config->dot11RSNASAERetransPeriod);
1456
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001457 if (config->passive_scan)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001458 fprintf(f, "passive_scan=%d\n", config->passive_scan);
1459
1460 if (config->reassoc_same_bss_optim)
1461 fprintf(f, "reassoc_same_bss_optim=%d\n",
1462 config->reassoc_same_bss_optim);
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07001463
1464 if (config->wps_priority)
1465 fprintf(f, "wps_priority=%d\n", config->wps_priority);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001466
1467 if (config->wpa_rsc_relaxation != DEFAULT_WPA_RSC_RELAXATION)
1468 fprintf(f, "wpa_rsc_relaxation=%d\n",
1469 config->wpa_rsc_relaxation);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001470
1471 if (config->sched_scan_plans)
1472 fprintf(f, "sched_scan_plans=%s\n", config->sched_scan_plans);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001473
1474#ifdef CONFIG_MBO
1475 if (config->non_pref_chan)
1476 fprintf(f, "non_pref_chan=%s\n", config->non_pref_chan);
1477 if (config->mbo_cell_capa != DEFAULT_MBO_CELL_CAPA)
1478 fprintf(f, "mbo_cell_capa=%u\n", config->mbo_cell_capa);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001479 if (config->disassoc_imminent_rssi_threshold !=
1480 DEFAULT_DISASSOC_IMMINENT_RSSI_THRESHOLD)
1481 fprintf(f, "disassoc_imminent_rssi_threshold=%d\n",
1482 config->disassoc_imminent_rssi_threshold);
1483 if (config->oce != DEFAULT_OCE_SUPPORT)
1484 fprintf(f, "oce=%u\n", config->oce);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001485#endif /* CONFIG_MBO */
1486
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001487 if (config->gas_address3)
1488 fprintf(f, "gas_address3=%d\n", config->gas_address3);
Dmitry Shmidt7d175302016-09-06 13:11:34 -07001489
1490 if (config->ftm_responder)
1491 fprintf(f, "ftm_responder=%d\n", config->ftm_responder);
1492 if (config->ftm_initiator)
1493 fprintf(f, "ftm_initiator=%d\n", config->ftm_initiator);
Paul Stewart092955c2017-02-06 09:13:09 -08001494
1495 if (config->osu_dir)
1496 fprintf(f, "osu_dir=%s\n", config->osu_dir);
1497
1498 if (config->fst_group_id)
1499 fprintf(f, "fst_group_id=%s\n", config->fst_group_id);
1500 if (config->fst_priority)
1501 fprintf(f, "fst_priority=%d\n", config->fst_priority);
1502 if (config->fst_llt)
1503 fprintf(f, "fst_llt=%d\n", config->fst_llt);
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08001504
1505 if (config->gas_rand_addr_lifetime != DEFAULT_RAND_ADDR_LIFETIME)
1506 fprintf(f, "gas_rand_addr_lifetime=%u\n",
1507 config->gas_rand_addr_lifetime);
1508 if (config->gas_rand_mac_addr)
1509 fprintf(f, "gas_rand_mac_addr=%d\n", config->gas_rand_mac_addr);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001510 if (config->dpp_config_processing)
1511 fprintf(f, "dpp_config_processing=%d\n",
1512 config->dpp_config_processing);
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001513 if (config->coloc_intf_reporting)
1514 fprintf(f, "coloc_intf_reporting=%d\n",
1515 config->coloc_intf_reporting);
Jimmy Chenf887c7b2018-11-13 15:19:57 +08001516 if (config->p2p_device_random_mac_addr)
1517 fprintf(f, "p2p_device_random_mac_addr=%d\n",
1518 config->p2p_device_random_mac_addr);
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001519 if (!is_zero_ether_addr(config->p2p_device_persistent_mac_addr))
1520 fprintf(f, "p2p_device_persistent_mac_addr=" MACSTR "\n",
1521 MAC2STR(config->p2p_device_persistent_mac_addr));
Jimmy Chen36c21992018-11-29 16:46:43 +08001522 if (config->p2p_interface_random_mac_addr)
1523 fprintf(f, "p2p_interface_random_mac_addr=%d\n",
1524 config->p2p_interface_random_mac_addr);
Jimmy Chen0bb4f862019-03-21 18:41:47 +08001525 if (config->bss_no_flush_when_down)
1526 fprintf(f, "bss_no_flush_when_down=%d\n",
1527 config->bss_no_flush_when_down);
Hai Shalom81f62d82019-07-22 12:10:00 -07001528 if (config->disable_btm)
1529 fprintf(f, "disable_btm=1\n");
Hai Shalomfdcde762020-04-02 11:19:20 -07001530 if (config->extended_key_id != DEFAULT_EXTENDED_KEY_ID)
1531 fprintf(f, "extended_key_id=%d\n",
1532 config->extended_key_id);
Hai Shalom60840252021-02-19 19:02:11 -08001533 if (config->wowlan_disconnect_on_deinit)
1534 fprintf(f, "wowlan_disconnect_on_deinit=%d\n",
1535 config->wowlan_disconnect_on_deinit);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001536}
1537
1538#endif /* CONFIG_NO_CONFIG_WRITE */
1539
1540
1541int wpa_config_write(const char *name, struct wpa_config *config)
1542{
1543#ifndef CONFIG_NO_CONFIG_WRITE
1544 FILE *f;
1545 struct wpa_ssid *ssid;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001546 struct wpa_cred *cred;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001547#ifndef CONFIG_NO_CONFIG_BLOBS
1548 struct wpa_config_blob *blob;
1549#endif /* CONFIG_NO_CONFIG_BLOBS */
1550 int ret = 0;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001551 const char *orig_name = name;
Hai Shalomfdcde762020-04-02 11:19:20 -07001552 int tmp_len;
1553 char *tmp_name;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001554
Hai Shalomfdcde762020-04-02 11:19:20 -07001555 if (!name) {
1556 wpa_printf(MSG_ERROR, "No configuration file for writing");
1557 return -1;
1558 }
1559
1560 tmp_len = os_strlen(name) + 5; /* allow space for .tmp suffix */
1561 tmp_name = os_malloc(tmp_len);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001562 if (tmp_name) {
Vinit Deshpande0a217de2015-02-05 12:48:02 -08001563 os_snprintf(tmp_name, tmp_len, "%s.tmp", name);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001564 name = tmp_name;
1565 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001566
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001567 wpa_printf(MSG_DEBUG, "Writing configuration file '%s'", name);
Vinit Deshpande0a217de2015-02-05 12:48:02 -08001568
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001569 f = fopen(name, "w");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001570 if (f == NULL) {
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001571 wpa_printf(MSG_DEBUG, "Failed to open '%s' for writing", name);
1572 os_free(tmp_name);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001573 return -1;
1574 }
1575
1576 wpa_config_write_global(f, config);
1577
Dmitry Shmidt04949592012-07-19 12:16:46 -07001578 for (cred = config->cred; cred; cred = cred->next) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001579 if (cred->temporary)
1580 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001581 fprintf(f, "\ncred={\n");
1582 wpa_config_write_cred(f, cred);
1583 fprintf(f, "}\n");
1584 }
1585
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001586 for (ssid = config->ssid; ssid; ssid = ssid->next) {
1587 if (ssid->key_mgmt == WPA_KEY_MGMT_WPS || ssid->temporary)
1588 continue; /* do not save temporary networks */
Hai Shalomfdcde762020-04-02 11:19:20 -07001589 if (wpa_key_mgmt_wpa_psk_no_sae(ssid->key_mgmt) &&
1590 !ssid->psk_set && !ssid->passphrase)
1591 continue; /* do not save invalid network */
1592 if (wpa_key_mgmt_sae(ssid->key_mgmt) &&
1593 !ssid->passphrase && !ssid->sae_password)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001594 continue; /* do not save invalid network */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001595 fprintf(f, "\nnetwork={\n");
1596 wpa_config_write_network(f, ssid);
1597 fprintf(f, "}\n");
1598 }
1599
1600#ifndef CONFIG_NO_CONFIG_BLOBS
1601 for (blob = config->blobs; blob; blob = blob->next) {
1602 ret = wpa_config_write_blob(f, blob);
1603 if (ret)
1604 break;
1605 }
1606#endif /* CONFIG_NO_CONFIG_BLOBS */
1607
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001608 os_fdatasync(f);
Mitchell Wills447c7ff2015-08-24 17:24:30 -07001609
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001610 fclose(f);
1611
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001612 if (tmp_name) {
Vinit Deshpande0a217de2015-02-05 12:48:02 -08001613 int chmod_ret = 0;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001614
Vinit Deshpande0a217de2015-02-05 12:48:02 -08001615#ifdef ANDROID
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001616 chmod_ret = chmod(tmp_name,
1617 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
1618#endif /* ANDROID */
1619 if (chmod_ret != 0 || rename(tmp_name, orig_name) != 0)
Vinit Deshpande0a217de2015-02-05 12:48:02 -08001620 ret = -1;
1621
1622 os_free(tmp_name);
1623 }
1624
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001625 wpa_printf(MSG_DEBUG, "Configuration file '%s' written %ssuccessfully",
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001626 orig_name, ret ? "un" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001627 return ret;
1628#else /* CONFIG_NO_CONFIG_WRITE */
1629 return -1;
1630#endif /* CONFIG_NO_CONFIG_WRITE */
1631}