blob: d1d8ca3e47880d14f4f162e546d77a51a58d64b8 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * WPA Supplicant / Configuration parser and common functions
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
9#include "includes.h"
10
11#include "common.h"
12#include "utils/uuid.h"
Dmitry Shmidtcf32e602014-01-28 10:57:39 -080013#include "utils/ip_addr.h"
Dmitry Shmidt29333592017-01-09 12:27:11 -080014#include "common/ieee802_1x_defs.h"
Hai Shalomc3565922019-10-28 11:58:20 -070015#include "common/sae.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070016#include "crypto/sha1.h"
17#include "rsn_supp/wpa.h"
18#include "eap_peer/eap.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070019#include "p2p/p2p.h"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080020#include "fst/fst.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070021#include "config.h"
22
23
24#if !defined(CONFIG_CTRL_IFACE) && defined(CONFIG_NO_CONFIG_WRITE)
25#define NO_CONFIG_WRITE
26#endif
27
28/*
29 * Structure for network configuration parsing. This data is used to implement
30 * a generic parser for each network block variable. The table of configuration
31 * variables is defined below in this file (ssid_fields[]).
32 */
33struct parse_data {
34 /* Configuration variable name */
35 char *name;
36
Dmitry Shmidte4663042016-04-04 10:07:49 -070037 /* Parser function for this variable. The parser functions return 0 or 1
38 * to indicate success. Value 0 indicates that the parameter value may
39 * have changed while value 1 means that the value did not change.
40 * Error cases (failure to parse the string) are indicated by returning
41 * -1. */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070042 int (*parser)(const struct parse_data *data, struct wpa_ssid *ssid,
43 int line, const char *value);
44
45#ifndef NO_CONFIG_WRITE
46 /* Writer function (i.e., to get the variable in text format from
47 * internal presentation). */
48 char * (*writer)(const struct parse_data *data, struct wpa_ssid *ssid);
49#endif /* NO_CONFIG_WRITE */
50
51 /* Variable specific parameters for the parser. */
52 void *param1, *param2, *param3, *param4;
53
54 /* 0 = this variable can be included in debug output and ctrl_iface
55 * 1 = this variable contains key/private data and it must not be
56 * included in debug output unless explicitly requested. In
57 * addition, this variable will not be readable through the
58 * ctrl_iface.
59 */
60 int key_data;
61};
62
63
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070064static int wpa_config_parse_str(const struct parse_data *data,
65 struct wpa_ssid *ssid,
66 int line, const char *value)
67{
Dmitry Shmidte4663042016-04-04 10:07:49 -070068 size_t res_len, *dst_len, prev_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070069 char **dst, *tmp;
70
71 if (os_strcmp(value, "NULL") == 0) {
72 wpa_printf(MSG_DEBUG, "Unset configuration string '%s'",
73 data->name);
74 tmp = NULL;
75 res_len = 0;
76 goto set;
77 }
78
79 tmp = wpa_config_parse_string(value, &res_len);
80 if (tmp == NULL) {
81 wpa_printf(MSG_ERROR, "Line %d: failed to parse %s '%s'.",
82 line, data->name,
83 data->key_data ? "[KEY DATA REMOVED]" : value);
84 return -1;
85 }
86
87 if (data->key_data) {
88 wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
89 (u8 *) tmp, res_len);
90 } else {
91 wpa_hexdump_ascii(MSG_MSGDUMP, data->name,
92 (u8 *) tmp, res_len);
93 }
94
95 if (data->param3 && res_len < (size_t) data->param3) {
96 wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
97 "min_len=%ld)", line, data->name,
98 (unsigned long) res_len, (long) data->param3);
99 os_free(tmp);
100 return -1;
101 }
102
103 if (data->param4 && res_len > (size_t) data->param4) {
104 wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
105 "max_len=%ld)", line, data->name,
106 (unsigned long) res_len, (long) data->param4);
107 os_free(tmp);
108 return -1;
109 }
110
111set:
112 dst = (char **) (((u8 *) ssid) + (long) data->param1);
113 dst_len = (size_t *) (((u8 *) ssid) + (long) data->param2);
Dmitry Shmidte4663042016-04-04 10:07:49 -0700114
115 if (data->param2)
116 prev_len = *dst_len;
117 else if (*dst)
118 prev_len = os_strlen(*dst);
119 else
120 prev_len = 0;
121 if ((*dst == NULL && tmp == NULL) ||
122 (*dst && tmp && prev_len == res_len &&
123 os_memcmp(*dst, tmp, res_len) == 0)) {
124 /* No change to the previously configured value */
125 os_free(tmp);
126 return 1;
127 }
128
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700129 os_free(*dst);
130 *dst = tmp;
131 if (data->param2)
132 *dst_len = res_len;
133
134 return 0;
135}
136
137
138#ifndef NO_CONFIG_WRITE
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700139static char * wpa_config_write_string_ascii(const u8 *value, size_t len)
140{
141 char *buf;
142
143 buf = os_malloc(len + 3);
144 if (buf == NULL)
145 return NULL;
146 buf[0] = '"';
147 os_memcpy(buf + 1, value, len);
148 buf[len + 1] = '"';
149 buf[len + 2] = '\0';
150
151 return buf;
152}
153
154
155static char * wpa_config_write_string_hex(const u8 *value, size_t len)
156{
157 char *buf;
158
159 buf = os_zalloc(2 * len + 1);
160 if (buf == NULL)
161 return NULL;
162 wpa_snprintf_hex(buf, 2 * len + 1, value, len);
163
164 return buf;
165}
166
167
168static char * wpa_config_write_string(const u8 *value, size_t len)
169{
170 if (value == NULL)
171 return NULL;
172
173 if (is_hex(value, len))
174 return wpa_config_write_string_hex(value, len);
175 else
176 return wpa_config_write_string_ascii(value, len);
177}
178
179
180static char * wpa_config_write_str(const struct parse_data *data,
181 struct wpa_ssid *ssid)
182{
183 size_t len;
184 char **src;
185
186 src = (char **) (((u8 *) ssid) + (long) data->param1);
187 if (*src == NULL)
188 return NULL;
189
190 if (data->param2)
191 len = *((size_t *) (((u8 *) ssid) + (long) data->param2));
192 else
193 len = os_strlen(*src);
194
195 return wpa_config_write_string((const u8 *) *src, len);
196}
197#endif /* NO_CONFIG_WRITE */
198
199
200static int wpa_config_parse_int(const struct parse_data *data,
201 struct wpa_ssid *ssid,
202 int line, const char *value)
203{
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700204 int val, *dst;
205 char *end;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700206
207 dst = (int *) (((u8 *) ssid) + (long) data->param1);
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700208 val = strtol(value, &end, 0);
209 if (*end) {
210 wpa_printf(MSG_ERROR, "Line %d: invalid number \"%s\"",
211 line, value);
212 return -1;
213 }
Dmitry Shmidte4663042016-04-04 10:07:49 -0700214
215 if (*dst == val)
216 return 1;
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700217 *dst = val;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700218 wpa_printf(MSG_MSGDUMP, "%s=%d (0x%x)", data->name, *dst, *dst);
219
220 if (data->param3 && *dst < (long) data->param3) {
221 wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
222 "min_value=%ld)", line, data->name, *dst,
223 (long) data->param3);
224 *dst = (long) data->param3;
225 return -1;
226 }
227
228 if (data->param4 && *dst > (long) data->param4) {
229 wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
230 "max_value=%ld)", line, data->name, *dst,
231 (long) data->param4);
232 *dst = (long) data->param4;
233 return -1;
234 }
235
236 return 0;
237}
238
239
240#ifndef NO_CONFIG_WRITE
241static char * wpa_config_write_int(const struct parse_data *data,
242 struct wpa_ssid *ssid)
243{
244 int *src, res;
245 char *value;
246
247 src = (int *) (((u8 *) ssid) + (long) data->param1);
248
249 value = os_malloc(20);
250 if (value == NULL)
251 return NULL;
252 res = os_snprintf(value, 20, "%d", *src);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800253 if (os_snprintf_error(20, res)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700254 os_free(value);
255 return NULL;
256 }
257 value[20 - 1] = '\0';
258 return value;
259}
260#endif /* NO_CONFIG_WRITE */
261
262
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800263static int wpa_config_parse_addr_list(const struct parse_data *data,
264 int line, const char *value,
265 u8 **list, size_t *num, char *name,
266 u8 abort_on_error, u8 masked)
267{
268 const char *pos;
269 u8 *buf, *n, addr[2 * ETH_ALEN];
270 size_t count;
271
272 buf = NULL;
273 count = 0;
274
275 pos = value;
276 while (pos && *pos) {
277 while (*pos == ' ')
278 pos++;
279
280 if (hwaddr_masked_aton(pos, addr, &addr[ETH_ALEN], masked)) {
281 if (abort_on_error || count == 0) {
282 wpa_printf(MSG_ERROR,
283 "Line %d: Invalid %s address '%s'",
284 line, name, value);
285 os_free(buf);
286 return -1;
287 }
288 /* continue anyway since this could have been from a
289 * truncated configuration file line */
290 wpa_printf(MSG_INFO,
291 "Line %d: Ignore likely truncated %s address '%s'",
292 line, name, pos);
293 } else {
294 n = os_realloc_array(buf, count + 1, 2 * ETH_ALEN);
295 if (n == NULL) {
296 os_free(buf);
297 return -1;
298 }
299 buf = n;
300 os_memmove(buf + 2 * ETH_ALEN, buf,
301 count * 2 * ETH_ALEN);
302 os_memcpy(buf, addr, 2 * ETH_ALEN);
303 count++;
304 wpa_printf(MSG_MSGDUMP,
305 "%s: addr=" MACSTR " mask=" MACSTR,
306 name, MAC2STR(addr),
307 MAC2STR(&addr[ETH_ALEN]));
308 }
309
310 pos = os_strchr(pos, ' ');
311 }
312
313 os_free(*list);
314 *list = buf;
315 *num = count;
316
317 return 0;
318}
319
320
321#ifndef NO_CONFIG_WRITE
322static char * wpa_config_write_addr_list(const struct parse_data *data,
323 const u8 *list, size_t num, char *name)
324{
325 char *value, *end, *pos;
326 int res;
327 size_t i;
328
329 if (list == NULL || num == 0)
330 return NULL;
331
332 value = os_malloc(2 * 20 * num);
333 if (value == NULL)
334 return NULL;
335 pos = value;
336 end = value + 2 * 20 * num;
337
338 for (i = num; i > 0; i--) {
339 const u8 *a = list + (i - 1) * 2 * ETH_ALEN;
340 const u8 *m = a + ETH_ALEN;
341
342 if (i < num)
343 *pos++ = ' ';
344 res = hwaddr_mask_txt(pos, end - pos, a, m);
345 if (res < 0) {
346 os_free(value);
347 return NULL;
348 }
349 pos += res;
350 }
351
352 return value;
353}
354#endif /* NO_CONFIG_WRITE */
355
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700356static int wpa_config_parse_bssid(const struct parse_data *data,
357 struct wpa_ssid *ssid, int line,
358 const char *value)
359{
360 if (value[0] == '\0' || os_strcmp(value, "\"\"") == 0 ||
361 os_strcmp(value, "any") == 0) {
362 ssid->bssid_set = 0;
363 wpa_printf(MSG_MSGDUMP, "BSSID any");
364 return 0;
365 }
366 if (hwaddr_aton(value, ssid->bssid)) {
367 wpa_printf(MSG_ERROR, "Line %d: Invalid BSSID '%s'.",
368 line, value);
369 return -1;
370 }
371 ssid->bssid_set = 1;
372 wpa_hexdump(MSG_MSGDUMP, "BSSID", ssid->bssid, ETH_ALEN);
373 return 0;
374}
375
376
377#ifndef NO_CONFIG_WRITE
378static char * wpa_config_write_bssid(const struct parse_data *data,
379 struct wpa_ssid *ssid)
380{
381 char *value;
382 int res;
383
384 if (!ssid->bssid_set)
385 return NULL;
386
387 value = os_malloc(20);
388 if (value == NULL)
389 return NULL;
390 res = os_snprintf(value, 20, MACSTR, MAC2STR(ssid->bssid));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800391 if (os_snprintf_error(20, res)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700392 os_free(value);
393 return NULL;
394 }
395 value[20 - 1] = '\0';
396 return value;
397}
398#endif /* NO_CONFIG_WRITE */
399
400
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700401static int wpa_config_parse_bssid_hint(const struct parse_data *data,
402 struct wpa_ssid *ssid, int line,
403 const char *value)
404{
405 if (value[0] == '\0' || os_strcmp(value, "\"\"") == 0 ||
406 os_strcmp(value, "any") == 0) {
407 ssid->bssid_hint_set = 0;
408 wpa_printf(MSG_MSGDUMP, "BSSID hint any");
409 return 0;
410 }
411 if (hwaddr_aton(value, ssid->bssid_hint)) {
412 wpa_printf(MSG_ERROR, "Line %d: Invalid BSSID hint '%s'.",
413 line, value);
414 return -1;
415 }
416 ssid->bssid_hint_set = 1;
417 wpa_hexdump(MSG_MSGDUMP, "BSSID hint", ssid->bssid_hint, ETH_ALEN);
418 return 0;
419}
420
421
422#ifndef NO_CONFIG_WRITE
423static char * wpa_config_write_bssid_hint(const struct parse_data *data,
424 struct wpa_ssid *ssid)
425{
426 char *value;
427 int res;
428
429 if (!ssid->bssid_hint_set)
430 return NULL;
431
432 value = os_malloc(20);
433 if (!value)
434 return NULL;
435 res = os_snprintf(value, 20, MACSTR, MAC2STR(ssid->bssid_hint));
436 if (os_snprintf_error(20, res)) {
437 os_free(value);
438 return NULL;
439 }
440 return value;
441}
442#endif /* NO_CONFIG_WRITE */
443
444
Hai Shalom60840252021-02-19 19:02:11 -0800445static int wpa_config_parse_bssid_ignore(const struct parse_data *data,
446 struct wpa_ssid *ssid, int line,
447 const char *value)
448{
449 return wpa_config_parse_addr_list(data, line, value,
450 &ssid->bssid_ignore,
451 &ssid->num_bssid_ignore,
452 "bssid_ignore", 1, 1);
453}
454
455
456/* deprecated alias for bssid_ignore for backwards compatibility */
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800457static int wpa_config_parse_bssid_blacklist(const struct parse_data *data,
458 struct wpa_ssid *ssid, int line,
459 const char *value)
460{
461 return wpa_config_parse_addr_list(data, line, value,
Hai Shalom60840252021-02-19 19:02:11 -0800462 &ssid->bssid_ignore,
463 &ssid->num_bssid_ignore,
464 "bssid_ignore", 1, 1);
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800465}
466
467
468#ifndef NO_CONFIG_WRITE
Hai Shalom60840252021-02-19 19:02:11 -0800469
470static char * wpa_config_write_bssid_ignore(const struct parse_data *data,
471 struct wpa_ssid *ssid)
472{
473 return wpa_config_write_addr_list(data, ssid->bssid_ignore,
474 ssid->num_bssid_ignore,
475 "bssid_ignore");
476}
477
478
479/* deprecated alias for bssid_ignore for backwards compatibility */
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800480static char * wpa_config_write_bssid_blacklist(const struct parse_data *data,
481 struct wpa_ssid *ssid)
482{
Hai Shalom60840252021-02-19 19:02:11 -0800483 return wpa_config_write_addr_list(data, ssid->bssid_ignore,
484 ssid->num_bssid_ignore,
485 "bssid_ignore");
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800486}
Hai Shalom60840252021-02-19 19:02:11 -0800487
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800488#endif /* NO_CONFIG_WRITE */
489
490
Hai Shalom60840252021-02-19 19:02:11 -0800491static int wpa_config_parse_bssid_accept(const struct parse_data *data,
492 struct wpa_ssid *ssid, int line,
493 const char *value)
494{
495 return wpa_config_parse_addr_list(data, line, value,
496 &ssid->bssid_accept,
497 &ssid->num_bssid_accept,
498 "bssid_accept", 1, 1);
499}
500
501
502/* deprecated alias for bssid_accept for backwards compatibility */
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800503static int wpa_config_parse_bssid_whitelist(const struct parse_data *data,
504 struct wpa_ssid *ssid, int line,
505 const char *value)
506{
507 return wpa_config_parse_addr_list(data, line, value,
Hai Shalom60840252021-02-19 19:02:11 -0800508 &ssid->bssid_accept,
509 &ssid->num_bssid_accept,
510 "bssid_accept", 1, 1);
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800511}
512
513
514#ifndef NO_CONFIG_WRITE
Hai Shalom60840252021-02-19 19:02:11 -0800515
516static char * wpa_config_write_bssid_accept(const struct parse_data *data,
517 struct wpa_ssid *ssid)
518{
519 return wpa_config_write_addr_list(data, ssid->bssid_accept,
520 ssid->num_bssid_accept,
521 "bssid_accept");
522}
523
524
525/* deprecated alias for bssid_accept for backwards compatibility */
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800526static char * wpa_config_write_bssid_whitelist(const struct parse_data *data,
527 struct wpa_ssid *ssid)
528{
Hai Shalom60840252021-02-19 19:02:11 -0800529 return wpa_config_write_addr_list(data, ssid->bssid_accept,
530 ssid->num_bssid_accept,
531 "bssid_accept");
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800532}
Hai Shalom60840252021-02-19 19:02:11 -0800533
534#endif /* NO_CONFIG_WRITE */
535
536
537#ifndef NO_CONFIG_WRITE
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800538#endif /* NO_CONFIG_WRITE */
539
540
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700541static int wpa_config_parse_psk(const struct parse_data *data,
542 struct wpa_ssid *ssid, int line,
543 const char *value)
544{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700545#ifdef CONFIG_EXT_PASSWORD
546 if (os_strncmp(value, "ext:", 4) == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700547 str_clear_free(ssid->passphrase);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700548 ssid->passphrase = NULL;
549 ssid->psk_set = 0;
550 os_free(ssid->ext_psk);
551 ssid->ext_psk = os_strdup(value + 4);
552 if (ssid->ext_psk == NULL)
553 return -1;
554 wpa_printf(MSG_DEBUG, "PSK: External password '%s'",
555 ssid->ext_psk);
556 return 0;
557 }
558#endif /* CONFIG_EXT_PASSWORD */
559
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700560 if (*value == '"') {
561#ifndef CONFIG_NO_PBKDF2
562 const char *pos;
563 size_t len;
564
565 value++;
566 pos = os_strrchr(value, '"');
567 if (pos)
568 len = pos - value;
569 else
570 len = os_strlen(value);
571 if (len < 8 || len > 63) {
572 wpa_printf(MSG_ERROR, "Line %d: Invalid passphrase "
573 "length %lu (expected: 8..63) '%s'.",
574 line, (unsigned long) len, value);
575 return -1;
576 }
577 wpa_hexdump_ascii_key(MSG_MSGDUMP, "PSK (ASCII passphrase)",
578 (u8 *) value, len);
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700579 if (has_ctrl_char((u8 *) value, len)) {
580 wpa_printf(MSG_ERROR,
581 "Line %d: Invalid passphrase character",
582 line);
583 return -1;
584 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700585 if (ssid->passphrase && os_strlen(ssid->passphrase) == len &&
Dmitry Shmidte4663042016-04-04 10:07:49 -0700586 os_memcmp(ssid->passphrase, value, len) == 0) {
587 /* No change to the previously configured value */
588 return 1;
589 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700590 ssid->psk_set = 0;
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700591 str_clear_free(ssid->passphrase);
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700592 ssid->passphrase = dup_binstr(value, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700593 if (ssid->passphrase == NULL)
594 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700595 return 0;
596#else /* CONFIG_NO_PBKDF2 */
597 wpa_printf(MSG_ERROR, "Line %d: ASCII passphrase not "
598 "supported.", line);
599 return -1;
600#endif /* CONFIG_NO_PBKDF2 */
601 }
602
603 if (hexstr2bin(value, ssid->psk, PMK_LEN) ||
604 value[PMK_LEN * 2] != '\0') {
605 wpa_printf(MSG_ERROR, "Line %d: Invalid PSK '%s'.",
606 line, value);
607 return -1;
608 }
609
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700610 str_clear_free(ssid->passphrase);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700611 ssid->passphrase = NULL;
612
613 ssid->psk_set = 1;
614 wpa_hexdump_key(MSG_MSGDUMP, "PSK", ssid->psk, PMK_LEN);
615 return 0;
616}
617
618
619#ifndef NO_CONFIG_WRITE
620static char * wpa_config_write_psk(const struct parse_data *data,
621 struct wpa_ssid *ssid)
622{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700623#ifdef CONFIG_EXT_PASSWORD
624 if (ssid->ext_psk) {
625 size_t len = 4 + os_strlen(ssid->ext_psk) + 1;
626 char *buf = os_malloc(len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800627 int res;
628
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700629 if (buf == NULL)
630 return NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800631 res = os_snprintf(buf, len, "ext:%s", ssid->ext_psk);
632 if (os_snprintf_error(len, res)) {
633 os_free(buf);
634 buf = NULL;
635 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700636 return buf;
637 }
638#endif /* CONFIG_EXT_PASSWORD */
639
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700640 if (ssid->passphrase)
641 return wpa_config_write_string_ascii(
642 (const u8 *) ssid->passphrase,
643 os_strlen(ssid->passphrase));
644
645 if (ssid->psk_set)
646 return wpa_config_write_string_hex(ssid->psk, PMK_LEN);
647
648 return NULL;
649}
650#endif /* NO_CONFIG_WRITE */
651
652
653static int wpa_config_parse_proto(const struct parse_data *data,
654 struct wpa_ssid *ssid, int line,
655 const char *value)
656{
657 int val = 0, last, errors = 0;
658 char *start, *end, *buf;
659
660 buf = os_strdup(value);
661 if (buf == NULL)
662 return -1;
663 start = buf;
664
665 while (*start != '\0') {
666 while (*start == ' ' || *start == '\t')
667 start++;
668 if (*start == '\0')
669 break;
670 end = start;
671 while (*end != ' ' && *end != '\t' && *end != '\0')
672 end++;
673 last = *end == '\0';
674 *end = '\0';
675 if (os_strcmp(start, "WPA") == 0)
676 val |= WPA_PROTO_WPA;
677 else if (os_strcmp(start, "RSN") == 0 ||
678 os_strcmp(start, "WPA2") == 0)
679 val |= WPA_PROTO_RSN;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800680 else if (os_strcmp(start, "OSEN") == 0)
681 val |= WPA_PROTO_OSEN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700682 else {
683 wpa_printf(MSG_ERROR, "Line %d: invalid proto '%s'",
684 line, start);
685 errors++;
686 }
687
688 if (last)
689 break;
690 start = end + 1;
691 }
692 os_free(buf);
693
694 if (val == 0) {
695 wpa_printf(MSG_ERROR,
696 "Line %d: no proto values configured.", line);
697 errors++;
698 }
699
Dmitry Shmidte4663042016-04-04 10:07:49 -0700700 if (!errors && ssid->proto == val)
701 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700702 wpa_printf(MSG_MSGDUMP, "proto: 0x%x", val);
703 ssid->proto = val;
704 return errors ? -1 : 0;
705}
706
707
708#ifndef NO_CONFIG_WRITE
709static char * wpa_config_write_proto(const struct parse_data *data,
710 struct wpa_ssid *ssid)
711{
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700712 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700713 char *buf, *pos, *end;
714
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800715 pos = buf = os_zalloc(20);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700716 if (buf == NULL)
717 return NULL;
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800718 end = buf + 20;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700719
720 if (ssid->proto & WPA_PROTO_WPA) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700721 ret = os_snprintf(pos, end - pos, "%sWPA",
722 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800723 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700724 return buf;
725 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700726 }
727
728 if (ssid->proto & WPA_PROTO_RSN) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700729 ret = os_snprintf(pos, end - pos, "%sRSN",
730 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800731 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700732 return buf;
733 pos += ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700734 }
735
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800736 if (ssid->proto & WPA_PROTO_OSEN) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700737 ret = os_snprintf(pos, end - pos, "%sOSEN",
738 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800739 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800740 return buf;
741 pos += ret;
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700742 }
743
744 if (pos == buf) {
745 os_free(buf);
746 buf = NULL;
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800747 }
748
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700749 return buf;
750}
751#endif /* NO_CONFIG_WRITE */
752
753
754static int wpa_config_parse_key_mgmt(const struct parse_data *data,
755 struct wpa_ssid *ssid, int line,
756 const char *value)
757{
758 int val = 0, last, errors = 0;
759 char *start, *end, *buf;
760
761 buf = os_strdup(value);
762 if (buf == NULL)
763 return -1;
764 start = buf;
765
766 while (*start != '\0') {
767 while (*start == ' ' || *start == '\t')
768 start++;
769 if (*start == '\0')
770 break;
771 end = start;
772 while (*end != ' ' && *end != '\t' && *end != '\0')
773 end++;
774 last = *end == '\0';
775 *end = '\0';
776 if (os_strcmp(start, "WPA-PSK") == 0)
777 val |= WPA_KEY_MGMT_PSK;
778 else if (os_strcmp(start, "WPA-EAP") == 0)
779 val |= WPA_KEY_MGMT_IEEE8021X;
780 else if (os_strcmp(start, "IEEE8021X") == 0)
781 val |= WPA_KEY_MGMT_IEEE8021X_NO_WPA;
782 else if (os_strcmp(start, "NONE") == 0)
783 val |= WPA_KEY_MGMT_NONE;
784 else if (os_strcmp(start, "WPA-NONE") == 0)
785 val |= WPA_KEY_MGMT_WPA_NONE;
786#ifdef CONFIG_IEEE80211R
787 else if (os_strcmp(start, "FT-PSK") == 0)
788 val |= WPA_KEY_MGMT_FT_PSK;
789 else if (os_strcmp(start, "FT-EAP") == 0)
790 val |= WPA_KEY_MGMT_FT_IEEE8021X;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700791#ifdef CONFIG_SHA384
792 else if (os_strcmp(start, "FT-EAP-SHA384") == 0)
793 val |= WPA_KEY_MGMT_FT_IEEE8021X_SHA384;
794#endif /* CONFIG_SHA384 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700795#endif /* CONFIG_IEEE80211R */
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000796#ifdef CONFIG_SHA384
797 else if (os_strcmp(start, "WPA-EAP-SHA384") == 0)
798 val |= WPA_KEY_MGMT_IEEE8021X_SHA384;
799#endif /* CONFIG_SHA384 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700800 else if (os_strcmp(start, "WPA-PSK-SHA256") == 0)
801 val |= WPA_KEY_MGMT_PSK_SHA256;
802 else if (os_strcmp(start, "WPA-EAP-SHA256") == 0)
803 val |= WPA_KEY_MGMT_IEEE8021X_SHA256;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700804#ifdef CONFIG_WPS
805 else if (os_strcmp(start, "WPS") == 0)
806 val |= WPA_KEY_MGMT_WPS;
807#endif /* CONFIG_WPS */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800808#ifdef CONFIG_SAE
809 else if (os_strcmp(start, "SAE") == 0)
810 val |= WPA_KEY_MGMT_SAE;
Sunil Ravi89eba102022-09-13 21:04:37 -0700811 else if (os_strcmp(start, "SAE-EXT-KEY") == 0)
812 val |= WPA_KEY_MGMT_SAE_EXT_KEY;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800813 else if (os_strcmp(start, "FT-SAE") == 0)
814 val |= WPA_KEY_MGMT_FT_SAE;
Sunil Ravi89eba102022-09-13 21:04:37 -0700815 else if (os_strcmp(start, "FT-SAE-EXT-KEY") == 0)
816 val |= WPA_KEY_MGMT_FT_SAE_EXT_KEY;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800817#endif /* CONFIG_SAE */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800818#ifdef CONFIG_HS20
819 else if (os_strcmp(start, "OSEN") == 0)
820 val |= WPA_KEY_MGMT_OSEN;
821#endif /* CONFIG_HS20 */
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800822#ifdef CONFIG_SUITEB
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800823 else if (os_strcmp(start, "WPA-EAP-SUITE-B") == 0)
824 val |= WPA_KEY_MGMT_IEEE8021X_SUITE_B;
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800825#endif /* CONFIG_SUITEB */
826#ifdef CONFIG_SUITEB192
827 else if (os_strcmp(start, "WPA-EAP-SUITE-B-192") == 0)
828 val |= WPA_KEY_MGMT_IEEE8021X_SUITE_B_192;
829#endif /* CONFIG_SUITEB192 */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800830#ifdef CONFIG_FILS
831 else if (os_strcmp(start, "FILS-SHA256") == 0)
832 val |= WPA_KEY_MGMT_FILS_SHA256;
833 else if (os_strcmp(start, "FILS-SHA384") == 0)
834 val |= WPA_KEY_MGMT_FILS_SHA384;
835#ifdef CONFIG_IEEE80211R
836 else if (os_strcmp(start, "FT-FILS-SHA256") == 0)
837 val |= WPA_KEY_MGMT_FT_FILS_SHA256;
838 else if (os_strcmp(start, "FT-FILS-SHA384") == 0)
839 val |= WPA_KEY_MGMT_FT_FILS_SHA384;
840#endif /* CONFIG_IEEE80211R */
841#endif /* CONFIG_FILS */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700842#ifdef CONFIG_OWE
843 else if (os_strcmp(start, "OWE") == 0)
844 val |= WPA_KEY_MGMT_OWE;
845#endif /* CONFIG_OWE */
846#ifdef CONFIG_DPP
847 else if (os_strcmp(start, "DPP") == 0)
848 val |= WPA_KEY_MGMT_DPP;
849#endif /* CONFIG_DPP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700850 else {
851 wpa_printf(MSG_ERROR, "Line %d: invalid key_mgmt '%s'",
852 line, start);
853 errors++;
854 }
855
856 if (last)
857 break;
858 start = end + 1;
859 }
860 os_free(buf);
861
862 if (val == 0) {
863 wpa_printf(MSG_ERROR,
864 "Line %d: no key_mgmt values configured.", line);
865 errors++;
866 }
867
Dmitry Shmidte4663042016-04-04 10:07:49 -0700868 if (!errors && ssid->key_mgmt == val)
869 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700870 wpa_printf(MSG_MSGDUMP, "key_mgmt: 0x%x", val);
871 ssid->key_mgmt = val;
872 return errors ? -1 : 0;
873}
874
875
876#ifndef NO_CONFIG_WRITE
877static char * wpa_config_write_key_mgmt(const struct parse_data *data,
878 struct wpa_ssid *ssid)
879{
880 char *buf, *pos, *end;
881 int ret;
882
Dmitry Shmidt96571392013-10-14 12:54:46 -0700883 pos = buf = os_zalloc(100);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700884 if (buf == NULL)
885 return NULL;
Dmitry Shmidt96571392013-10-14 12:54:46 -0700886 end = buf + 100;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700887
888 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
889 ret = os_snprintf(pos, end - pos, "%sWPA-PSK",
890 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800891 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700892 end[-1] = '\0';
893 return buf;
894 }
895 pos += ret;
896 }
897
898 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
899 ret = os_snprintf(pos, end - pos, "%sWPA-EAP",
900 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800901 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700902 end[-1] = '\0';
903 return buf;
904 }
905 pos += ret;
906 }
907
908 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
909 ret = os_snprintf(pos, end - pos, "%sIEEE8021X",
910 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800911 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700912 end[-1] = '\0';
913 return buf;
914 }
915 pos += ret;
916 }
917
918 if (ssid->key_mgmt & WPA_KEY_MGMT_NONE) {
919 ret = os_snprintf(pos, end - pos, "%sNONE",
920 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800921 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700922 end[-1] = '\0';
923 return buf;
924 }
925 pos += ret;
926 }
927
928 if (ssid->key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
929 ret = os_snprintf(pos, end - pos, "%sWPA-NONE",
930 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800931 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700932 end[-1] = '\0';
933 return buf;
934 }
935 pos += ret;
936 }
937
938#ifdef CONFIG_IEEE80211R
Dmitry Shmidt96571392013-10-14 12:54:46 -0700939 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_PSK) {
940 ret = os_snprintf(pos, end - pos, "%sFT-PSK",
941 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800942 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt96571392013-10-14 12:54:46 -0700943 end[-1] = '\0';
944 return buf;
945 }
946 pos += ret;
947 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700948
Dmitry Shmidt96571392013-10-14 12:54:46 -0700949 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
950 ret = os_snprintf(pos, end - pos, "%sFT-EAP",
951 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800952 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt96571392013-10-14 12:54:46 -0700953 end[-1] = '\0';
954 return buf;
955 }
956 pos += ret;
957 }
Roshan Pius3a1667e2018-07-03 15:17:14 -0700958
959#ifdef CONFIG_SHA384
960 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X_SHA384) {
961 ret = os_snprintf(pos, end - pos, "%sFT-EAP-SHA384",
962 pos == buf ? "" : " ");
963 if (os_snprintf_error(end - pos, ret)) {
964 end[-1] = '\0';
965 return buf;
966 }
967 pos += ret;
968 }
969#endif /* CONFIG_SHA384 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700970#endif /* CONFIG_IEEE80211R */
971
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000972#ifdef CONFIG_SHA384
973 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA384) {
974 ret = os_snprintf(pos, end - pos, "%sWPA-EAP-SHA384",
975 pos == buf ? "" : " ");
976 if (os_snprintf_error(end - pos, ret)) {
977 end[-1] = '\0';
978 return buf;
979 }
980 pos += ret;
981 }
982#endif /* CONFIG_SHA384 */
983
Dmitry Shmidt96571392013-10-14 12:54:46 -0700984 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
985 ret = os_snprintf(pos, end - pos, "%sWPA-PSK-SHA256",
986 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800987 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt96571392013-10-14 12:54:46 -0700988 end[-1] = '\0';
989 return buf;
990 }
991 pos += ret;
992 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700993
Dmitry Shmidt96571392013-10-14 12:54:46 -0700994 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
995 ret = os_snprintf(pos, end - pos, "%sWPA-EAP-SHA256",
996 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800997 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt96571392013-10-14 12:54:46 -0700998 end[-1] = '\0';
999 return buf;
1000 }
1001 pos += ret;
1002 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001003
1004#ifdef CONFIG_WPS
Dmitry Shmidt96571392013-10-14 12:54:46 -07001005 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
1006 ret = os_snprintf(pos, end - pos, "%sWPS",
1007 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001008 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt96571392013-10-14 12:54:46 -07001009 end[-1] = '\0';
1010 return buf;
1011 }
1012 pos += ret;
1013 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001014#endif /* CONFIG_WPS */
1015
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001016#ifdef CONFIG_SAE
1017 if (ssid->key_mgmt & WPA_KEY_MGMT_SAE) {
1018 ret = os_snprintf(pos, end - pos, "%sSAE",
1019 pos == buf ? "" : " ");
1020 if (os_snprintf_error(end - pos, ret)) {
1021 end[-1] = '\0';
1022 return buf;
1023 }
1024 pos += ret;
1025 }
1026
Sunil Ravi89eba102022-09-13 21:04:37 -07001027 if (ssid->key_mgmt & WPA_KEY_MGMT_SAE_EXT_KEY) {
1028 ret = os_snprintf(pos, end - pos, "%sSAE-EXT-KEY",
1029 pos == buf ? "" : " ");
1030 if (os_snprintf_error(end - pos, ret)) {
1031 end[-1] = '\0';
1032 return buf;
1033 }
1034 pos += ret;
1035 }
1036
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001037 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_SAE) {
1038 ret = os_snprintf(pos, end - pos, "%sFT-SAE",
1039 pos == buf ? "" : " ");
1040 if (os_snprintf_error(end - pos, ret)) {
1041 end[-1] = '\0';
1042 return buf;
1043 }
1044 pos += ret;
1045 }
Sunil Ravi89eba102022-09-13 21:04:37 -07001046
1047 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_SAE_EXT_KEY) {
1048 ret = os_snprintf(pos, end - pos, "%sFT-SAE-EXT-KEY",
1049 pos == buf ? "" : " ");
1050 if (os_snprintf_error(end - pos, ret)) {
1051 end[-1] = '\0';
1052 return buf;
1053 }
1054 pos += ret;
1055 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001056#endif /* CONFIG_SAE */
1057
1058#ifdef CONFIG_HS20
1059 if (ssid->key_mgmt & WPA_KEY_MGMT_OSEN) {
1060 ret = os_snprintf(pos, end - pos, "%sOSEN",
1061 pos == buf ? "" : " ");
1062 if (os_snprintf_error(end - pos, ret)) {
1063 end[-1] = '\0';
1064 return buf;
1065 }
1066 pos += ret;
1067 }
1068#endif /* CONFIG_HS20 */
1069
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001070#ifdef CONFIG_SUITEB
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001071 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
1072 ret = os_snprintf(pos, end - pos, "%sWPA-EAP-SUITE-B",
1073 pos == buf ? "" : " ");
1074 if (os_snprintf_error(end - pos, ret)) {
1075 end[-1] = '\0';
1076 return buf;
1077 }
1078 pos += ret;
1079 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001080#endif /* CONFIG_SUITEB */
1081
1082#ifdef CONFIG_SUITEB192
1083 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
1084 ret = os_snprintf(pos, end - pos, "%sWPA-EAP-SUITE-B-192",
1085 pos == buf ? "" : " ");
1086 if (os_snprintf_error(end - pos, ret)) {
1087 end[-1] = '\0';
1088 return buf;
1089 }
1090 pos += ret;
1091 }
1092#endif /* CONFIG_SUITEB192 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001093
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001094#ifdef CONFIG_FILS
1095 if (ssid->key_mgmt & WPA_KEY_MGMT_FILS_SHA256) {
1096 ret = os_snprintf(pos, end - pos, "%sFILS-SHA256",
1097 pos == buf ? "" : " ");
1098 if (os_snprintf_error(end - pos, ret)) {
1099 end[-1] = '\0';
1100 return buf;
1101 }
1102 pos += ret;
1103 }
1104 if (ssid->key_mgmt & WPA_KEY_MGMT_FILS_SHA384) {
1105 ret = os_snprintf(pos, end - pos, "%sFILS-SHA384",
1106 pos == buf ? "" : " ");
1107 if (os_snprintf_error(end - pos, ret)) {
1108 end[-1] = '\0';
1109 return buf;
1110 }
1111 pos += ret;
1112 }
1113#ifdef CONFIG_IEEE80211R
1114 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA256) {
1115 ret = os_snprintf(pos, end - pos, "%sFT-FILS-SHA256",
1116 pos == buf ? "" : " ");
1117 if (os_snprintf_error(end - pos, ret)) {
1118 end[-1] = '\0';
1119 return buf;
1120 }
1121 pos += ret;
1122 }
1123 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA384) {
1124 ret = os_snprintf(pos, end - pos, "%sFT-FILS-SHA384",
1125 pos == buf ? "" : " ");
1126 if (os_snprintf_error(end - pos, ret)) {
1127 end[-1] = '\0';
1128 return buf;
1129 }
1130 pos += ret;
1131 }
1132#endif /* CONFIG_IEEE80211R */
1133#endif /* CONFIG_FILS */
1134
Hai Shalom74f70d42019-02-11 14:42:39 -08001135#ifdef CONFIG_DPP
1136 if (ssid->key_mgmt & WPA_KEY_MGMT_DPP) {
1137 ret = os_snprintf(pos, end - pos, "%sDPP",
1138 pos == buf ? "" : " ");
1139 if (os_snprintf_error(end - pos, ret)) {
1140 end[-1] = '\0';
1141 return buf;
1142 }
1143 pos += ret;
1144 }
1145#endif /* CONFIG_DPP */
1146
1147#ifdef CONFIG_OWE
1148 if (ssid->key_mgmt & WPA_KEY_MGMT_OWE) {
1149 ret = os_snprintf(pos, end - pos, "%sOWE",
1150 pos == buf ? "" : " ");
1151 if (os_snprintf_error(end - pos, ret)) {
1152 end[-1] = '\0';
1153 return buf;
1154 }
1155 pos += ret;
1156 }
1157#endif /* CONFIG_OWE */
1158
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001159 if (pos == buf) {
1160 os_free(buf);
1161 buf = NULL;
1162 }
1163
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001164 return buf;
1165}
1166#endif /* NO_CONFIG_WRITE */
1167
1168
1169static int wpa_config_parse_cipher(int line, const char *value)
1170{
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001171#ifdef CONFIG_NO_WPA
1172 return -1;
1173#else /* CONFIG_NO_WPA */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001174 int val = wpa_parse_cipher(value);
1175 if (val < 0) {
1176 wpa_printf(MSG_ERROR, "Line %d: invalid cipher '%s'.",
1177 line, value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001178 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001179 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001180 if (val == 0) {
1181 wpa_printf(MSG_ERROR, "Line %d: no cipher values configured.",
1182 line);
1183 return -1;
1184 }
1185 return val;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001186#endif /* CONFIG_NO_WPA */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001187}
1188
1189
1190#ifndef NO_CONFIG_WRITE
1191static char * wpa_config_write_cipher(int cipher)
1192{
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001193#ifdef CONFIG_NO_WPA
1194 return NULL;
1195#else /* CONFIG_NO_WPA */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001196 char *buf = os_zalloc(50);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001197 if (buf == NULL)
1198 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001199
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001200 if (wpa_write_ciphers(buf, buf + 50, cipher, " ") < 0) {
1201 os_free(buf);
1202 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001203 }
1204
1205 return buf;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001206#endif /* CONFIG_NO_WPA */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001207}
1208#endif /* NO_CONFIG_WRITE */
1209
1210
1211static int wpa_config_parse_pairwise(const struct parse_data *data,
1212 struct wpa_ssid *ssid, int line,
1213 const char *value)
1214{
1215 int val;
1216 val = wpa_config_parse_cipher(line, value);
1217 if (val == -1)
1218 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001219 if (val & ~WPA_ALLOWED_PAIRWISE_CIPHERS) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001220 wpa_printf(MSG_ERROR, "Line %d: not allowed pairwise cipher "
1221 "(0x%x).", line, val);
1222 return -1;
1223 }
1224
Dmitry Shmidte4663042016-04-04 10:07:49 -07001225 if (ssid->pairwise_cipher == val)
1226 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001227 wpa_printf(MSG_MSGDUMP, "pairwise: 0x%x", val);
1228 ssid->pairwise_cipher = val;
1229 return 0;
1230}
1231
1232
1233#ifndef NO_CONFIG_WRITE
1234static char * wpa_config_write_pairwise(const struct parse_data *data,
1235 struct wpa_ssid *ssid)
1236{
1237 return wpa_config_write_cipher(ssid->pairwise_cipher);
1238}
1239#endif /* NO_CONFIG_WRITE */
1240
1241
1242static int wpa_config_parse_group(const struct parse_data *data,
1243 struct wpa_ssid *ssid, int line,
1244 const char *value)
1245{
1246 int val;
1247 val = wpa_config_parse_cipher(line, value);
1248 if (val == -1)
1249 return -1;
Dmitry Shmidt41712582015-06-29 11:02:15 -07001250
1251 /*
1252 * Backwards compatibility - filter out WEP ciphers that were previously
1253 * allowed.
1254 */
1255 val &= ~(WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40);
1256
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001257 if (val & ~WPA_ALLOWED_GROUP_CIPHERS) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001258 wpa_printf(MSG_ERROR, "Line %d: not allowed group cipher "
1259 "(0x%x).", line, val);
1260 return -1;
1261 }
1262
Dmitry Shmidte4663042016-04-04 10:07:49 -07001263 if (ssid->group_cipher == val)
1264 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001265 wpa_printf(MSG_MSGDUMP, "group: 0x%x", val);
1266 ssid->group_cipher = val;
1267 return 0;
1268}
1269
1270
1271#ifndef NO_CONFIG_WRITE
1272static char * wpa_config_write_group(const struct parse_data *data,
1273 struct wpa_ssid *ssid)
1274{
1275 return wpa_config_write_cipher(ssid->group_cipher);
1276}
1277#endif /* NO_CONFIG_WRITE */
1278
1279
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001280static int wpa_config_parse_group_mgmt(const struct parse_data *data,
1281 struct wpa_ssid *ssid, int line,
1282 const char *value)
1283{
1284 int val;
1285
1286 val = wpa_config_parse_cipher(line, value);
1287 if (val == -1)
1288 return -1;
1289
1290 if (val & ~WPA_ALLOWED_GROUP_MGMT_CIPHERS) {
1291 wpa_printf(MSG_ERROR,
1292 "Line %d: not allowed group management cipher (0x%x).",
1293 line, val);
1294 return -1;
1295 }
1296
1297 if (ssid->group_mgmt_cipher == val)
1298 return 1;
1299 wpa_printf(MSG_MSGDUMP, "group_mgmt: 0x%x", val);
1300 ssid->group_mgmt_cipher = val;
1301 return 0;
1302}
1303
1304
1305#ifndef NO_CONFIG_WRITE
1306static char * wpa_config_write_group_mgmt(const struct parse_data *data,
1307 struct wpa_ssid *ssid)
1308{
1309 return wpa_config_write_cipher(ssid->group_mgmt_cipher);
1310}
1311#endif /* NO_CONFIG_WRITE */
1312
1313
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001314static int wpa_config_parse_auth_alg(const struct parse_data *data,
1315 struct wpa_ssid *ssid, int line,
1316 const char *value)
1317{
1318 int val = 0, last, errors = 0;
1319 char *start, *end, *buf;
1320
1321 buf = os_strdup(value);
1322 if (buf == NULL)
1323 return -1;
1324 start = buf;
1325
1326 while (*start != '\0') {
1327 while (*start == ' ' || *start == '\t')
1328 start++;
1329 if (*start == '\0')
1330 break;
1331 end = start;
1332 while (*end != ' ' && *end != '\t' && *end != '\0')
1333 end++;
1334 last = *end == '\0';
1335 *end = '\0';
1336 if (os_strcmp(start, "OPEN") == 0)
1337 val |= WPA_AUTH_ALG_OPEN;
1338 else if (os_strcmp(start, "SHARED") == 0)
1339 val |= WPA_AUTH_ALG_SHARED;
1340 else if (os_strcmp(start, "LEAP") == 0)
1341 val |= WPA_AUTH_ALG_LEAP;
1342 else {
1343 wpa_printf(MSG_ERROR, "Line %d: invalid auth_alg '%s'",
1344 line, start);
1345 errors++;
1346 }
1347
1348 if (last)
1349 break;
1350 start = end + 1;
1351 }
1352 os_free(buf);
1353
1354 if (val == 0) {
1355 wpa_printf(MSG_ERROR,
1356 "Line %d: no auth_alg values configured.", line);
1357 errors++;
1358 }
1359
Dmitry Shmidte4663042016-04-04 10:07:49 -07001360 if (!errors && ssid->auth_alg == val)
1361 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001362 wpa_printf(MSG_MSGDUMP, "auth_alg: 0x%x", val);
1363 ssid->auth_alg = val;
1364 return errors ? -1 : 0;
1365}
1366
1367
1368#ifndef NO_CONFIG_WRITE
1369static char * wpa_config_write_auth_alg(const struct parse_data *data,
1370 struct wpa_ssid *ssid)
1371{
1372 char *buf, *pos, *end;
1373 int ret;
1374
1375 pos = buf = os_zalloc(30);
1376 if (buf == NULL)
1377 return NULL;
1378 end = buf + 30;
1379
1380 if (ssid->auth_alg & WPA_AUTH_ALG_OPEN) {
1381 ret = os_snprintf(pos, end - pos, "%sOPEN",
1382 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001383 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001384 end[-1] = '\0';
1385 return buf;
1386 }
1387 pos += ret;
1388 }
1389
1390 if (ssid->auth_alg & WPA_AUTH_ALG_SHARED) {
1391 ret = os_snprintf(pos, end - pos, "%sSHARED",
1392 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001393 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001394 end[-1] = '\0';
1395 return buf;
1396 }
1397 pos += ret;
1398 }
1399
1400 if (ssid->auth_alg & WPA_AUTH_ALG_LEAP) {
1401 ret = os_snprintf(pos, end - pos, "%sLEAP",
1402 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001403 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001404 end[-1] = '\0';
1405 return buf;
1406 }
1407 pos += ret;
1408 }
1409
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001410 if (pos == buf) {
1411 os_free(buf);
1412 buf = NULL;
1413 }
1414
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001415 return buf;
1416}
1417#endif /* NO_CONFIG_WRITE */
1418
1419
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001420static int * wpa_config_parse_int_array(const char *value)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001421{
1422 int *freqs;
1423 size_t used, len;
1424 const char *pos;
1425
1426 used = 0;
1427 len = 10;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001428 freqs = os_calloc(len + 1, sizeof(int));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001429 if (freqs == NULL)
1430 return NULL;
1431
1432 pos = value;
1433 while (pos) {
1434 while (*pos == ' ')
1435 pos++;
1436 if (used == len) {
1437 int *n;
1438 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001439 n = os_realloc_array(freqs, len * 2 + 1, sizeof(int));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001440 if (n == NULL) {
1441 os_free(freqs);
1442 return NULL;
1443 }
1444 for (i = len; i <= len * 2; i++)
1445 n[i] = 0;
1446 freqs = n;
1447 len *= 2;
1448 }
1449
1450 freqs[used] = atoi(pos);
1451 if (freqs[used] == 0)
1452 break;
1453 used++;
1454 pos = os_strchr(pos + 1, ' ');
1455 }
1456
1457 return freqs;
1458}
1459
1460
1461static int wpa_config_parse_scan_freq(const struct parse_data *data,
1462 struct wpa_ssid *ssid, int line,
1463 const char *value)
1464{
1465 int *freqs;
1466
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001467 freqs = wpa_config_parse_int_array(value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001468 if (freqs == NULL)
1469 return -1;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001470 if (freqs[0] == 0) {
1471 os_free(freqs);
1472 freqs = NULL;
1473 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001474 os_free(ssid->scan_freq);
1475 ssid->scan_freq = freqs;
1476
1477 return 0;
1478}
1479
1480
1481static int wpa_config_parse_freq_list(const struct parse_data *data,
1482 struct wpa_ssid *ssid, int line,
1483 const char *value)
1484{
1485 int *freqs;
1486
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001487 freqs = wpa_config_parse_int_array(value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001488 if (freqs == NULL)
1489 return -1;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001490 if (freqs[0] == 0) {
1491 os_free(freqs);
1492 freqs = NULL;
1493 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001494 os_free(ssid->freq_list);
1495 ssid->freq_list = freqs;
1496
1497 return 0;
1498}
1499
1500
1501#ifndef NO_CONFIG_WRITE
1502static char * wpa_config_write_freqs(const struct parse_data *data,
1503 const int *freqs)
1504{
1505 char *buf, *pos, *end;
1506 int i, ret;
1507 size_t count;
1508
1509 if (freqs == NULL)
1510 return NULL;
1511
1512 count = 0;
1513 for (i = 0; freqs[i]; i++)
1514 count++;
1515
1516 pos = buf = os_zalloc(10 * count + 1);
1517 if (buf == NULL)
1518 return NULL;
1519 end = buf + 10 * count + 1;
1520
1521 for (i = 0; freqs[i]; i++) {
1522 ret = os_snprintf(pos, end - pos, "%s%u",
1523 i == 0 ? "" : " ", freqs[i]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001524 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001525 end[-1] = '\0';
1526 return buf;
1527 }
1528 pos += ret;
1529 }
1530
1531 return buf;
1532}
1533
1534
1535static char * wpa_config_write_scan_freq(const struct parse_data *data,
1536 struct wpa_ssid *ssid)
1537{
1538 return wpa_config_write_freqs(data, ssid->scan_freq);
1539}
1540
1541
1542static char * wpa_config_write_freq_list(const struct parse_data *data,
1543 struct wpa_ssid *ssid)
1544{
1545 return wpa_config_write_freqs(data, ssid->freq_list);
1546}
1547#endif /* NO_CONFIG_WRITE */
1548
1549
1550#ifdef IEEE8021X_EAPOL
1551static int wpa_config_parse_eap(const struct parse_data *data,
1552 struct wpa_ssid *ssid, int line,
1553 const char *value)
1554{
1555 int last, errors = 0;
1556 char *start, *end, *buf;
1557 struct eap_method_type *methods = NULL, *tmp;
1558 size_t num_methods = 0;
1559
1560 buf = os_strdup(value);
1561 if (buf == NULL)
1562 return -1;
1563 start = buf;
1564
1565 while (*start != '\0') {
1566 while (*start == ' ' || *start == '\t')
1567 start++;
1568 if (*start == '\0')
1569 break;
1570 end = start;
1571 while (*end != ' ' && *end != '\t' && *end != '\0')
1572 end++;
1573 last = *end == '\0';
1574 *end = '\0';
1575 tmp = methods;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001576 methods = os_realloc_array(methods, num_methods + 1,
1577 sizeof(*methods));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001578 if (methods == NULL) {
1579 os_free(tmp);
1580 os_free(buf);
1581 return -1;
1582 }
1583 methods[num_methods].method = eap_peer_get_type(
1584 start, &methods[num_methods].vendor);
1585 if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
1586 methods[num_methods].method == EAP_TYPE_NONE) {
1587 wpa_printf(MSG_ERROR, "Line %d: unknown EAP method "
1588 "'%s'", line, start);
1589 wpa_printf(MSG_ERROR, "You may need to add support for"
1590 " this EAP method during wpa_supplicant\n"
1591 "build time configuration.\n"
1592 "See README for more information.");
1593 errors++;
1594 } else if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
1595 methods[num_methods].method == EAP_TYPE_LEAP)
1596 ssid->leap++;
1597 else
1598 ssid->non_leap++;
1599 num_methods++;
1600 if (last)
1601 break;
1602 start = end + 1;
1603 }
1604 os_free(buf);
1605
1606 tmp = methods;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001607 methods = os_realloc_array(methods, num_methods + 1, sizeof(*methods));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001608 if (methods == NULL) {
1609 os_free(tmp);
1610 return -1;
1611 }
1612 methods[num_methods].vendor = EAP_VENDOR_IETF;
1613 methods[num_methods].method = EAP_TYPE_NONE;
1614 num_methods++;
1615
Dmitry Shmidte4663042016-04-04 10:07:49 -07001616 if (!errors && ssid->eap.eap_methods) {
1617 struct eap_method_type *prev_m;
1618 size_t i, j, prev_methods, match = 0;
1619
1620 prev_m = ssid->eap.eap_methods;
1621 for (i = 0; prev_m[i].vendor != EAP_VENDOR_IETF ||
1622 prev_m[i].method != EAP_TYPE_NONE; i++) {
1623 /* Count the methods */
1624 }
1625 prev_methods = i + 1;
1626
1627 for (i = 0; prev_methods == num_methods && i < prev_methods;
1628 i++) {
1629 for (j = 0; j < num_methods; j++) {
1630 if (prev_m[i].vendor == methods[j].vendor &&
1631 prev_m[i].method == methods[j].method) {
1632 match++;
1633 break;
1634 }
1635 }
1636 }
1637 if (match == num_methods) {
1638 os_free(methods);
1639 return 1;
1640 }
1641 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001642 wpa_hexdump(MSG_MSGDUMP, "eap methods",
1643 (u8 *) methods, num_methods * sizeof(*methods));
Dmitry Shmidt04949592012-07-19 12:16:46 -07001644 os_free(ssid->eap.eap_methods);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001645 ssid->eap.eap_methods = methods;
1646 return errors ? -1 : 0;
1647}
1648
1649
Dmitry Shmidt83474442015-04-15 13:47:09 -07001650#ifndef NO_CONFIG_WRITE
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001651static char * wpa_config_write_eap(const struct parse_data *data,
1652 struct wpa_ssid *ssid)
1653{
1654 int i, ret;
1655 char *buf, *pos, *end;
1656 const struct eap_method_type *eap_methods = ssid->eap.eap_methods;
1657 const char *name;
1658
1659 if (eap_methods == NULL)
1660 return NULL;
1661
1662 pos = buf = os_zalloc(100);
1663 if (buf == NULL)
1664 return NULL;
1665 end = buf + 100;
1666
1667 for (i = 0; eap_methods[i].vendor != EAP_VENDOR_IETF ||
1668 eap_methods[i].method != EAP_TYPE_NONE; i++) {
1669 name = eap_get_name(eap_methods[i].vendor,
1670 eap_methods[i].method);
1671 if (name) {
1672 ret = os_snprintf(pos, end - pos, "%s%s",
1673 pos == buf ? "" : " ", name);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001674 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001675 break;
1676 pos += ret;
1677 }
1678 }
1679
1680 end[-1] = '\0';
1681
1682 return buf;
1683}
Dmitry Shmidt83474442015-04-15 13:47:09 -07001684#endif /* NO_CONFIG_WRITE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001685
1686
1687static int wpa_config_parse_password(const struct parse_data *data,
1688 struct wpa_ssid *ssid, int line,
1689 const char *value)
1690{
1691 u8 *hash;
1692
1693 if (os_strcmp(value, "NULL") == 0) {
Dmitry Shmidte4663042016-04-04 10:07:49 -07001694 if (!ssid->eap.password)
1695 return 1; /* Already unset */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001696 wpa_printf(MSG_DEBUG, "Unset configuration string 'password'");
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001697 bin_clear_free(ssid->eap.password, ssid->eap.password_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001698 ssid->eap.password = NULL;
1699 ssid->eap.password_len = 0;
1700 return 0;
1701 }
1702
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001703#ifdef CONFIG_EXT_PASSWORD
1704 if (os_strncmp(value, "ext:", 4) == 0) {
1705 char *name = os_strdup(value + 4);
Hai Shalomc3565922019-10-28 11:58:20 -07001706 if (!name)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001707 return -1;
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001708 bin_clear_free(ssid->eap.password, ssid->eap.password_len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001709 ssid->eap.password = (u8 *) name;
1710 ssid->eap.password_len = os_strlen(name);
1711 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
1712 ssid->eap.flags |= EAP_CONFIG_FLAGS_EXT_PASSWORD;
1713 return 0;
1714 }
1715#endif /* CONFIG_EXT_PASSWORD */
1716
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001717 if (os_strncmp(value, "hash:", 5) != 0) {
1718 char *tmp;
1719 size_t res_len;
1720
1721 tmp = wpa_config_parse_string(value, &res_len);
Hai Shalomc3565922019-10-28 11:58:20 -07001722 if (!tmp) {
1723 wpa_printf(MSG_ERROR,
1724 "Line %d: failed to parse password.", line);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001725 return -1;
1726 }
1727 wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
1728 (u8 *) tmp, res_len);
1729
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001730 bin_clear_free(ssid->eap.password, ssid->eap.password_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001731 ssid->eap.password = (u8 *) tmp;
1732 ssid->eap.password_len = res_len;
1733 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001734 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_PASSWORD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001735
1736 return 0;
1737 }
1738
1739
1740 /* NtPasswordHash: hash:<32 hex digits> */
1741 if (os_strlen(value + 5) != 2 * 16) {
Hai Shalomc3565922019-10-28 11:58:20 -07001742 wpa_printf(MSG_ERROR,
1743 "Line %d: Invalid password hash length (expected 32 hex digits)",
1744 line);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001745 return -1;
1746 }
1747
1748 hash = os_malloc(16);
Hai Shalomc3565922019-10-28 11:58:20 -07001749 if (!hash)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001750 return -1;
1751
1752 if (hexstr2bin(value + 5, hash, 16)) {
1753 os_free(hash);
1754 wpa_printf(MSG_ERROR, "Line %d: Invalid password hash", line);
1755 return -1;
1756 }
1757
1758 wpa_hexdump_key(MSG_MSGDUMP, data->name, hash, 16);
1759
Dmitry Shmidte4663042016-04-04 10:07:49 -07001760 if (ssid->eap.password && ssid->eap.password_len == 16 &&
1761 os_memcmp(ssid->eap.password, hash, 16) == 0 &&
1762 (ssid->eap.flags & EAP_CONFIG_FLAGS_PASSWORD_NTHASH)) {
1763 bin_clear_free(hash, 16);
1764 return 1;
1765 }
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001766 bin_clear_free(ssid->eap.password, ssid->eap.password_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001767 ssid->eap.password = hash;
1768 ssid->eap.password_len = 16;
1769 ssid->eap.flags |= EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001770 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_PASSWORD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001771
1772 return 0;
1773}
1774
1775
Hai Shalomc3565922019-10-28 11:58:20 -07001776static int wpa_config_parse_machine_password(const struct parse_data *data,
1777 struct wpa_ssid *ssid, int line,
1778 const char *value)
1779{
1780 u8 *hash;
1781
1782 if (os_strcmp(value, "NULL") == 0) {
1783 if (!ssid->eap.machine_password)
1784 return 1; /* Already unset */
1785 wpa_printf(MSG_DEBUG,
1786 "Unset configuration string 'machine_password'");
1787 bin_clear_free(ssid->eap.machine_password,
1788 ssid->eap.machine_password_len);
1789 ssid->eap.machine_password = NULL;
1790 ssid->eap.machine_password_len = 0;
1791 return 0;
1792 }
1793
1794#ifdef CONFIG_EXT_PASSWORD
1795 if (os_strncmp(value, "ext:", 4) == 0) {
1796 char *name = os_strdup(value + 4);
1797
1798 if (!name)
1799 return -1;
1800 bin_clear_free(ssid->eap.machine_password,
1801 ssid->eap.machine_password_len);
1802 ssid->eap.machine_password = (u8 *) name;
1803 ssid->eap.machine_password_len = os_strlen(name);
1804 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_MACHINE_PASSWORD_NTHASH;
1805 ssid->eap.flags |= EAP_CONFIG_FLAGS_EXT_MACHINE_PASSWORD;
1806 return 0;
1807 }
1808#endif /* CONFIG_EXT_PASSWORD */
1809
1810 if (os_strncmp(value, "hash:", 5) != 0) {
1811 char *tmp;
1812 size_t res_len;
1813
1814 tmp = wpa_config_parse_string(value, &res_len);
1815 if (!tmp) {
1816 wpa_printf(MSG_ERROR,
1817 "Line %d: failed to parse machine_password.",
1818 line);
1819 return -1;
1820 }
1821 wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
1822 (u8 *) tmp, res_len);
1823
1824 bin_clear_free(ssid->eap.machine_password,
1825 ssid->eap.machine_password_len);
1826 ssid->eap.machine_password = (u8 *) tmp;
1827 ssid->eap.machine_password_len = res_len;
1828 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_MACHINE_PASSWORD_NTHASH;
1829 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_MACHINE_PASSWORD;
1830
1831 return 0;
1832 }
1833
1834
1835 /* NtPasswordHash: hash:<32 hex digits> */
1836 if (os_strlen(value + 5) != 2 * 16) {
1837 wpa_printf(MSG_ERROR,
1838 "Line %d: Invalid machine_password hash length (expected 32 hex digits)",
1839 line);
1840 return -1;
1841 }
1842
1843 hash = os_malloc(16);
1844 if (!hash)
1845 return -1;
1846
1847 if (hexstr2bin(value + 5, hash, 16)) {
1848 os_free(hash);
1849 wpa_printf(MSG_ERROR, "Line %d: Invalid machine_password hash",
1850 line);
1851 return -1;
1852 }
1853
1854 wpa_hexdump_key(MSG_MSGDUMP, data->name, hash, 16);
1855
1856 if (ssid->eap.machine_password &&
1857 ssid->eap.machine_password_len == 16 &&
1858 os_memcmp(ssid->eap.machine_password, hash, 16) == 0 &&
1859 (ssid->eap.flags & EAP_CONFIG_FLAGS_MACHINE_PASSWORD_NTHASH)) {
1860 bin_clear_free(hash, 16);
1861 return 1;
1862 }
1863 bin_clear_free(ssid->eap.machine_password,
1864 ssid->eap.machine_password_len);
1865 ssid->eap.machine_password = hash;
1866 ssid->eap.machine_password_len = 16;
1867 ssid->eap.flags |= EAP_CONFIG_FLAGS_MACHINE_PASSWORD_NTHASH;
1868 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_MACHINE_PASSWORD;
1869
1870 return 0;
1871}
1872
1873
Dmitry Shmidt83474442015-04-15 13:47:09 -07001874#ifndef NO_CONFIG_WRITE
Hai Shalomc3565922019-10-28 11:58:20 -07001875
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001876static char * wpa_config_write_password(const struct parse_data *data,
1877 struct wpa_ssid *ssid)
1878{
1879 char *buf;
1880
Hai Shalomc3565922019-10-28 11:58:20 -07001881 if (!ssid->eap.password)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001882 return NULL;
1883
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001884#ifdef CONFIG_EXT_PASSWORD
1885 if (ssid->eap.flags & EAP_CONFIG_FLAGS_EXT_PASSWORD) {
1886 buf = os_zalloc(4 + ssid->eap.password_len + 1);
Hai Shalomc3565922019-10-28 11:58:20 -07001887 if (!buf)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001888 return NULL;
1889 os_memcpy(buf, "ext:", 4);
1890 os_memcpy(buf + 4, ssid->eap.password, ssid->eap.password_len);
1891 return buf;
1892 }
1893#endif /* CONFIG_EXT_PASSWORD */
1894
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001895 if (!(ssid->eap.flags & EAP_CONFIG_FLAGS_PASSWORD_NTHASH)) {
1896 return wpa_config_write_string(
1897 ssid->eap.password, ssid->eap.password_len);
1898 }
1899
1900 buf = os_malloc(5 + 32 + 1);
Hai Shalomc3565922019-10-28 11:58:20 -07001901 if (!buf)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001902 return NULL;
1903
1904 os_memcpy(buf, "hash:", 5);
1905 wpa_snprintf_hex(buf + 5, 32 + 1, ssid->eap.password, 16);
1906
1907 return buf;
1908}
Hai Shalomc3565922019-10-28 11:58:20 -07001909
1910
1911static char * wpa_config_write_machine_password(const struct parse_data *data,
1912 struct wpa_ssid *ssid)
1913{
1914 char *buf;
1915
1916 if (!ssid->eap.machine_password)
1917 return NULL;
1918
1919#ifdef CONFIG_EXT_PASSWORD
1920 if (ssid->eap.flags & EAP_CONFIG_FLAGS_EXT_MACHINE_PASSWORD) {
1921 buf = os_zalloc(4 + ssid->eap.machine_password_len + 1);
1922 if (!buf)
1923 return NULL;
1924 os_memcpy(buf, "ext:", 4);
1925 os_memcpy(buf + 4, ssid->eap.machine_password,
1926 ssid->eap.machine_password_len);
1927 return buf;
1928 }
1929#endif /* CONFIG_EXT_PASSWORD */
1930
1931 if (!(ssid->eap.flags & EAP_CONFIG_FLAGS_MACHINE_PASSWORD_NTHASH)) {
1932 return wpa_config_write_string(
1933 ssid->eap.machine_password,
1934 ssid->eap.machine_password_len);
1935 }
1936
1937 buf = os_malloc(5 + 32 + 1);
1938 if (!buf)
1939 return NULL;
1940
1941 os_memcpy(buf, "hash:", 5);
1942 wpa_snprintf_hex(buf + 5, 32 + 1, ssid->eap.machine_password, 16);
1943
1944 return buf;
1945}
1946
Dmitry Shmidt83474442015-04-15 13:47:09 -07001947#endif /* NO_CONFIG_WRITE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001948#endif /* IEEE8021X_EAPOL */
1949
1950
Hai Shalomfdcde762020-04-02 11:19:20 -07001951#ifdef CONFIG_WEP
1952
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001953static int wpa_config_parse_wep_key(u8 *key, size_t *len, int line,
1954 const char *value, int idx)
1955{
1956 char *buf, title[20];
1957 int res;
1958
1959 buf = wpa_config_parse_string(value, len);
1960 if (buf == NULL) {
1961 wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key %d '%s'.",
1962 line, idx, value);
1963 return -1;
1964 }
1965 if (*len > MAX_WEP_KEY_LEN) {
1966 wpa_printf(MSG_ERROR, "Line %d: Too long WEP key %d '%s'.",
1967 line, idx, value);
1968 os_free(buf);
1969 return -1;
1970 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001971 if (*len && *len != 5 && *len != 13 && *len != 16) {
1972 wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key length %u - "
1973 "this network block will be ignored",
1974 line, (unsigned int) *len);
1975 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001976 os_memcpy(key, buf, *len);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001977 str_clear_free(buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001978 res = os_snprintf(title, sizeof(title), "wep_key%d", idx);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001979 if (!os_snprintf_error(sizeof(title), res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001980 wpa_hexdump_key(MSG_MSGDUMP, title, key, *len);
1981 return 0;
1982}
1983
1984
1985static int wpa_config_parse_wep_key0(const struct parse_data *data,
1986 struct wpa_ssid *ssid, int line,
1987 const char *value)
1988{
1989 return wpa_config_parse_wep_key(ssid->wep_key[0],
1990 &ssid->wep_key_len[0], line,
1991 value, 0);
1992}
1993
1994
1995static int wpa_config_parse_wep_key1(const struct parse_data *data,
1996 struct wpa_ssid *ssid, int line,
1997 const char *value)
1998{
1999 return wpa_config_parse_wep_key(ssid->wep_key[1],
2000 &ssid->wep_key_len[1], line,
2001 value, 1);
2002}
2003
2004
2005static int wpa_config_parse_wep_key2(const struct parse_data *data,
2006 struct wpa_ssid *ssid, int line,
2007 const char *value)
2008{
2009 return wpa_config_parse_wep_key(ssid->wep_key[2],
2010 &ssid->wep_key_len[2], line,
2011 value, 2);
2012}
2013
2014
2015static int wpa_config_parse_wep_key3(const struct parse_data *data,
2016 struct wpa_ssid *ssid, int line,
2017 const char *value)
2018{
2019 return wpa_config_parse_wep_key(ssid->wep_key[3],
2020 &ssid->wep_key_len[3], line,
2021 value, 3);
2022}
2023
2024
2025#ifndef NO_CONFIG_WRITE
2026static char * wpa_config_write_wep_key(struct wpa_ssid *ssid, int idx)
2027{
2028 if (ssid->wep_key_len[idx] == 0)
2029 return NULL;
2030 return wpa_config_write_string(ssid->wep_key[idx],
2031 ssid->wep_key_len[idx]);
2032}
2033
2034
2035static char * wpa_config_write_wep_key0(const struct parse_data *data,
2036 struct wpa_ssid *ssid)
2037{
2038 return wpa_config_write_wep_key(ssid, 0);
2039}
2040
2041
2042static char * wpa_config_write_wep_key1(const struct parse_data *data,
2043 struct wpa_ssid *ssid)
2044{
2045 return wpa_config_write_wep_key(ssid, 1);
2046}
2047
2048
2049static char * wpa_config_write_wep_key2(const struct parse_data *data,
2050 struct wpa_ssid *ssid)
2051{
2052 return wpa_config_write_wep_key(ssid, 2);
2053}
2054
2055
2056static char * wpa_config_write_wep_key3(const struct parse_data *data,
2057 struct wpa_ssid *ssid)
2058{
2059 return wpa_config_write_wep_key(ssid, 3);
2060}
2061#endif /* NO_CONFIG_WRITE */
2062
Hai Shalomfdcde762020-04-02 11:19:20 -07002063#endif /* CONFIG_WEP */
2064
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002065
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002066#ifdef CONFIG_P2P
2067
Dmitry Shmidt54605472013-11-08 11:10:19 -08002068static int wpa_config_parse_go_p2p_dev_addr(const struct parse_data *data,
2069 struct wpa_ssid *ssid, int line,
2070 const char *value)
2071{
2072 if (value[0] == '\0' || os_strcmp(value, "\"\"") == 0 ||
2073 os_strcmp(value, "any") == 0) {
2074 os_memset(ssid->go_p2p_dev_addr, 0, ETH_ALEN);
2075 wpa_printf(MSG_MSGDUMP, "GO P2P Device Address any");
2076 return 0;
2077 }
2078 if (hwaddr_aton(value, ssid->go_p2p_dev_addr)) {
2079 wpa_printf(MSG_ERROR, "Line %d: Invalid GO P2P Device Address '%s'.",
2080 line, value);
2081 return -1;
2082 }
2083 ssid->bssid_set = 1;
2084 wpa_printf(MSG_MSGDUMP, "GO P2P Device Address " MACSTR,
2085 MAC2STR(ssid->go_p2p_dev_addr));
2086 return 0;
2087}
2088
2089
2090#ifndef NO_CONFIG_WRITE
2091static char * wpa_config_write_go_p2p_dev_addr(const struct parse_data *data,
2092 struct wpa_ssid *ssid)
2093{
2094 char *value;
2095 int res;
2096
2097 if (is_zero_ether_addr(ssid->go_p2p_dev_addr))
2098 return NULL;
2099
2100 value = os_malloc(20);
2101 if (value == NULL)
2102 return NULL;
2103 res = os_snprintf(value, 20, MACSTR, MAC2STR(ssid->go_p2p_dev_addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002104 if (os_snprintf_error(20, res)) {
Dmitry Shmidt54605472013-11-08 11:10:19 -08002105 os_free(value);
2106 return NULL;
2107 }
2108 value[20 - 1] = '\0';
2109 return value;
2110}
2111#endif /* NO_CONFIG_WRITE */
2112
2113
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002114static int wpa_config_parse_p2p_client_list(const struct parse_data *data,
2115 struct wpa_ssid *ssid, int line,
2116 const char *value)
2117{
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002118 return wpa_config_parse_addr_list(data, line, value,
2119 &ssid->p2p_client_list,
2120 &ssid->num_p2p_clients,
2121 "p2p_client_list", 0, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002122}
2123
2124
2125#ifndef NO_CONFIG_WRITE
2126static char * wpa_config_write_p2p_client_list(const struct parse_data *data,
2127 struct wpa_ssid *ssid)
2128{
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002129 return wpa_config_write_addr_list(data, ssid->p2p_client_list,
2130 ssid->num_p2p_clients,
2131 "p2p_client_list");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002132}
2133#endif /* NO_CONFIG_WRITE */
2134
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002135
2136static int wpa_config_parse_psk_list(const struct parse_data *data,
2137 struct wpa_ssid *ssid, int line,
2138 const char *value)
2139{
2140 struct psk_list_entry *p;
2141 const char *pos;
2142
2143 p = os_zalloc(sizeof(*p));
2144 if (p == NULL)
2145 return -1;
2146
2147 pos = value;
2148 if (os_strncmp(pos, "P2P-", 4) == 0) {
2149 p->p2p = 1;
2150 pos += 4;
2151 }
2152
2153 if (hwaddr_aton(pos, p->addr)) {
2154 wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list address '%s'",
2155 line, pos);
2156 os_free(p);
2157 return -1;
2158 }
2159 pos += 17;
2160 if (*pos != '-') {
2161 wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list '%s'",
2162 line, pos);
2163 os_free(p);
2164 return -1;
2165 }
2166 pos++;
2167
2168 if (hexstr2bin(pos, p->psk, PMK_LEN) || pos[PMK_LEN * 2] != '\0') {
2169 wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list PSK '%s'",
2170 line, pos);
2171 os_free(p);
2172 return -1;
2173 }
2174
2175 dl_list_add(&ssid->psk_list, &p->list);
2176
2177 return 0;
2178}
2179
2180
2181#ifndef NO_CONFIG_WRITE
2182static char * wpa_config_write_psk_list(const struct parse_data *data,
2183 struct wpa_ssid *ssid)
2184{
2185 return NULL;
2186}
2187#endif /* NO_CONFIG_WRITE */
2188
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002189#endif /* CONFIG_P2P */
2190
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002191
2192#ifdef CONFIG_MESH
2193
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002194static int wpa_config_parse_mesh_basic_rates(const struct parse_data *data,
2195 struct wpa_ssid *ssid, int line,
2196 const char *value)
2197{
2198 int *rates = wpa_config_parse_int_array(value);
2199
2200 if (rates == NULL) {
2201 wpa_printf(MSG_ERROR, "Line %d: Invalid mesh_basic_rates '%s'",
2202 line, value);
2203 return -1;
2204 }
2205 if (rates[0] == 0) {
2206 os_free(rates);
2207 rates = NULL;
2208 }
2209
2210 os_free(ssid->mesh_basic_rates);
2211 ssid->mesh_basic_rates = rates;
2212
2213 return 0;
2214}
2215
2216
2217#ifndef NO_CONFIG_WRITE
2218
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002219static char * wpa_config_write_mesh_basic_rates(const struct parse_data *data,
2220 struct wpa_ssid *ssid)
2221{
2222 return wpa_config_write_freqs(data, ssid->mesh_basic_rates);
2223}
2224
2225#endif /* NO_CONFIG_WRITE */
2226
2227#endif /* CONFIG_MESH */
2228
2229
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002230#ifdef CONFIG_MACSEC
2231
2232static int wpa_config_parse_mka_cak(const struct parse_data *data,
2233 struct wpa_ssid *ssid, int line,
2234 const char *value)
2235{
Hai Shalom74f70d42019-02-11 14:42:39 -08002236 size_t len;
2237
2238 len = os_strlen(value);
2239 if (len > 2 * MACSEC_CAK_MAX_LEN ||
2240 (len != 2 * 16 && len != 2 * 32) ||
2241 hexstr2bin(value, ssid->mka_cak, len / 2)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002242 wpa_printf(MSG_ERROR, "Line %d: Invalid MKA-CAK '%s'.",
2243 line, value);
2244 return -1;
2245 }
Hai Shalom74f70d42019-02-11 14:42:39 -08002246 ssid->mka_cak_len = len / 2;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002247 ssid->mka_psk_set |= MKA_PSK_SET_CAK;
2248
Hai Shalom74f70d42019-02-11 14:42:39 -08002249 wpa_hexdump_key(MSG_MSGDUMP, "MKA-CAK", ssid->mka_cak,
2250 ssid->mka_cak_len);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002251 return 0;
2252}
2253
2254
2255static int wpa_config_parse_mka_ckn(const struct parse_data *data,
2256 struct wpa_ssid *ssid, int line,
2257 const char *value)
2258{
Hai Shalom74f70d42019-02-11 14:42:39 -08002259 size_t len;
2260
2261 len = os_strlen(value);
2262 if (len > 2 * MACSEC_CKN_MAX_LEN || /* too long */
2263 len < 2 || /* too short */
2264 len % 2 != 0 /* not an integral number of bytes */) {
2265 wpa_printf(MSG_ERROR, "Line %d: Invalid MKA-CKN '%s'.",
2266 line, value);
2267 return -1;
2268 }
2269 ssid->mka_ckn_len = len / 2;
2270 if (hexstr2bin(value, ssid->mka_ckn, ssid->mka_ckn_len)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002271 wpa_printf(MSG_ERROR, "Line %d: Invalid MKA-CKN '%s'.",
2272 line, value);
2273 return -1;
2274 }
2275
2276 ssid->mka_psk_set |= MKA_PSK_SET_CKN;
2277
Hai Shalom74f70d42019-02-11 14:42:39 -08002278 wpa_hexdump_key(MSG_MSGDUMP, "MKA-CKN", ssid->mka_ckn,
2279 ssid->mka_ckn_len);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002280 return 0;
2281}
2282
2283
2284#ifndef NO_CONFIG_WRITE
2285
2286static char * wpa_config_write_mka_cak(const struct parse_data *data,
2287 struct wpa_ssid *ssid)
2288{
2289 if (!(ssid->mka_psk_set & MKA_PSK_SET_CAK))
2290 return NULL;
2291
Hai Shalom74f70d42019-02-11 14:42:39 -08002292 return wpa_config_write_string_hex(ssid->mka_cak, ssid->mka_cak_len);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002293}
2294
2295
2296static char * wpa_config_write_mka_ckn(const struct parse_data *data,
2297 struct wpa_ssid *ssid)
2298{
2299 if (!(ssid->mka_psk_set & MKA_PSK_SET_CKN))
2300 return NULL;
Hai Shalom74f70d42019-02-11 14:42:39 -08002301 return wpa_config_write_string_hex(ssid->mka_ckn, ssid->mka_ckn_len);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002302}
2303
2304#endif /* NO_CONFIG_WRITE */
2305
2306#endif /* CONFIG_MACSEC */
2307
2308
Hai Shalom74f70d42019-02-11 14:42:39 -08002309#ifdef CONFIG_OCV
2310
2311static int wpa_config_parse_ocv(const struct parse_data *data,
2312 struct wpa_ssid *ssid, int line,
2313 const char *value)
2314{
2315 char *end;
2316
2317 ssid->ocv = strtol(value, &end, 0);
2318 if (*end || ssid->ocv < 0 || ssid->ocv > 1) {
2319 wpa_printf(MSG_ERROR, "Line %d: Invalid ocv value '%s'.",
2320 line, value);
2321 return -1;
2322 }
2323 if (ssid->ocv && ssid->ieee80211w == NO_MGMT_FRAME_PROTECTION)
2324 ssid->ieee80211w = MGMT_FRAME_PROTECTION_OPTIONAL;
2325 return 0;
2326}
2327
2328
2329#ifndef NO_CONFIG_WRITE
2330static char * wpa_config_write_ocv(const struct parse_data *data,
2331 struct wpa_ssid *ssid)
2332{
2333 char *value = os_malloc(20);
2334
2335 if (!value)
2336 return NULL;
2337 os_snprintf(value, 20, "%d", ssid->ocv);
2338 value[20 - 1] = '\0';
2339 return value;
2340}
2341#endif /* NO_CONFIG_WRITE */
2342
2343#endif /* CONFIG_OCV */
2344
2345
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002346static int wpa_config_parse_peerkey(const struct parse_data *data,
2347 struct wpa_ssid *ssid, int line,
2348 const char *value)
2349{
2350 wpa_printf(MSG_INFO, "NOTE: Obsolete peerkey parameter ignored");
2351 return 0;
2352}
2353
2354
2355#ifndef NO_CONFIG_WRITE
2356static char * wpa_config_write_peerkey(const struct parse_data *data,
2357 struct wpa_ssid *ssid)
2358{
2359 return NULL;
2360}
2361#endif /* NO_CONFIG_WRITE */
2362
2363
Sunil Ravi77d572f2023-01-17 23:58:31 +00002364static int wpa_config_parse_mac_value(const struct parse_data *data,
2365 struct wpa_ssid *ssid, int line,
2366 const char *value)
2367{
2368 u8 mac_value[ETH_ALEN];
2369
2370 if (hwaddr_aton(value, mac_value) == 0) {
2371 if (os_memcmp(mac_value, ssid->mac_value, ETH_ALEN) == 0)
2372 return 1;
2373 os_memcpy(ssid->mac_value, mac_value, ETH_ALEN);
2374 return 0;
2375 }
2376
2377 wpa_printf(MSG_ERROR, "Line %d: Invalid MAC address '%s'",
2378 line, value);
2379 return -1;
2380}
2381
2382
2383#ifndef NO_CONFIG_WRITE
2384static char * wpa_config_write_mac_value(const struct parse_data *data,
2385 struct wpa_ssid *ssid)
2386{
2387 const size_t size = 3 * ETH_ALEN;
2388 char *value;
2389 int res;
2390
2391 if (ssid->mac_addr != WPAS_MAC_ADDR_STYLE_DEDICATED_PER_ESS)
2392 return NULL;
2393
2394 value = os_malloc(size);
2395 if (!value)
2396 return NULL;
2397 res = os_snprintf(value, size, MACSTR, MAC2STR(ssid->mac_value));
2398 if (os_snprintf_error(size, res)) {
2399 os_free(value);
2400 return NULL;
2401 }
2402 value[size - 1] = '\0';
2403 return value;
2404}
2405#endif /* NO_CONFIG_WRITE */
2406
2407
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002408/* Helper macros for network block parser */
2409
2410#ifdef OFFSET
2411#undef OFFSET
2412#endif /* OFFSET */
2413/* OFFSET: Get offset of a variable within the wpa_ssid structure */
2414#define OFFSET(v) ((void *) &((struct wpa_ssid *) 0)->v)
2415
2416/* STR: Define a string variable for an ASCII string; f = field name */
2417#ifdef NO_CONFIG_WRITE
2418#define _STR(f) #f, wpa_config_parse_str, OFFSET(f)
Hai Shalomc3565922019-10-28 11:58:20 -07002419#define _STRe(f, m) #f, wpa_config_parse_str, OFFSET(eap.m)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002420#else /* NO_CONFIG_WRITE */
2421#define _STR(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(f)
Hai Shalomc3565922019-10-28 11:58:20 -07002422#define _STRe(f, m) #f, wpa_config_parse_str, wpa_config_write_str, \
2423 OFFSET(eap.m)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002424#endif /* NO_CONFIG_WRITE */
2425#define STR(f) _STR(f), NULL, NULL, NULL, 0
Hai Shalomc3565922019-10-28 11:58:20 -07002426#define STRe(f, m) _STRe(f, m), NULL, NULL, NULL, 0
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002427#define STR_KEY(f) _STR(f), NULL, NULL, NULL, 1
Hai Shalomc3565922019-10-28 11:58:20 -07002428#define STR_KEYe(f, m) _STRe(f, m), NULL, NULL, NULL, 1
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002429
2430/* STR_LEN: Define a string variable with a separate variable for storing the
2431 * data length. Unlike STR(), this can be used to store arbitrary binary data
2432 * (i.e., even nul termination character). */
2433#define _STR_LEN(f) _STR(f), OFFSET(f ## _len)
Hai Shalomc3565922019-10-28 11:58:20 -07002434#define _STR_LENe(f, m) _STRe(f, m), OFFSET(eap.m ## _len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002435#define STR_LEN(f) _STR_LEN(f), NULL, NULL, 0
Hai Shalomc3565922019-10-28 11:58:20 -07002436#define STR_LENe(f, m) _STR_LENe(f, m), NULL, NULL, 0
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002437#define STR_LEN_KEY(f) _STR_LEN(f), NULL, NULL, 1
2438
2439/* STR_RANGE: Like STR_LEN(), but with minimum and maximum allowed length
2440 * explicitly specified. */
2441#define _STR_RANGE(f, min, max) _STR_LEN(f), (void *) (min), (void *) (max)
2442#define STR_RANGE(f, min, max) _STR_RANGE(f, min, max), 0
2443#define STR_RANGE_KEY(f, min, max) _STR_RANGE(f, min, max), 1
2444
2445#ifdef NO_CONFIG_WRITE
2446#define _INT(f) #f, wpa_config_parse_int, OFFSET(f), (void *) 0
Hai Shalomc3565922019-10-28 11:58:20 -07002447#define _INTe(f, m) #f, wpa_config_parse_int, OFFSET(eap.m), (void *) 0
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002448#else /* NO_CONFIG_WRITE */
2449#define _INT(f) #f, wpa_config_parse_int, wpa_config_write_int, \
2450 OFFSET(f), (void *) 0
Hai Shalomc3565922019-10-28 11:58:20 -07002451#define _INTe(f, m) #f, wpa_config_parse_int, wpa_config_write_int, \
2452 OFFSET(eap.m), (void *) 0
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002453#endif /* NO_CONFIG_WRITE */
2454
2455/* INT: Define an integer variable */
2456#define INT(f) _INT(f), NULL, NULL, 0
Hai Shalomc3565922019-10-28 11:58:20 -07002457#define INTe(f, m) _INTe(f, m), NULL, NULL, 0
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002458
2459/* INT_RANGE: Define an integer variable with allowed value range */
2460#define INT_RANGE(f, min, max) _INT(f), (void *) (min), (void *) (max), 0
2461
2462/* FUNC: Define a configuration variable that uses a custom function for
2463 * parsing and writing the value. */
2464#ifdef NO_CONFIG_WRITE
2465#define _FUNC(f) #f, wpa_config_parse_ ## f, NULL, NULL, NULL, NULL
2466#else /* NO_CONFIG_WRITE */
2467#define _FUNC(f) #f, wpa_config_parse_ ## f, wpa_config_write_ ## f, \
2468 NULL, NULL, NULL, NULL
2469#endif /* NO_CONFIG_WRITE */
2470#define FUNC(f) _FUNC(f), 0
2471#define FUNC_KEY(f) _FUNC(f), 1
2472
2473/*
2474 * Table of network configuration variables. This table is used to parse each
2475 * network configuration variable, e.g., each line in wpa_supplicant.conf file
2476 * that is inside a network block.
2477 *
2478 * This table is generated using the helper macros defined above and with
2479 * generous help from the C pre-processor. The field name is stored as a string
2480 * into .name and for STR and INT types, the offset of the target buffer within
2481 * struct wpa_ssid is stored in .param1. .param2 (if not NULL) is similar
2482 * offset to the field containing the length of the configuration variable.
2483 * .param3 and .param4 can be used to mark the allowed range (length for STR
2484 * and value for INT).
2485 *
2486 * For each configuration line in wpa_supplicant.conf, the parser goes through
2487 * this table and select the entry that matches with the field name. The parser
2488 * function (.parser) is then called to parse the actual value of the field.
2489 *
2490 * This kind of mechanism makes it easy to add new configuration parameters,
2491 * since only one line needs to be added into this table and into the
2492 * struct wpa_ssid definition if the new variable is either a string or
2493 * integer. More complex types will need to use their own parser and writer
2494 * functions.
2495 */
2496static const struct parse_data ssid_fields[] = {
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07002497 { STR_RANGE(ssid, 0, SSID_MAX_LEN) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002498 { INT_RANGE(scan_ssid, 0, 1) },
2499 { FUNC(bssid) },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002500 { FUNC(bssid_hint) },
Hai Shalom60840252021-02-19 19:02:11 -08002501 { FUNC(bssid_ignore) },
2502 { FUNC(bssid_accept) },
2503 { FUNC(bssid_blacklist) }, /* deprecated alias for bssid_ignore */
2504 { FUNC(bssid_whitelist) }, /* deprecated alias for bssid_accept */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002505 { FUNC_KEY(psk) },
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07002506 { INT(mem_only_psk) },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002507 { STR_KEY(sae_password) },
Roshan Pius3a1667e2018-07-03 15:17:14 -07002508 { STR(sae_password_id) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002509 { FUNC(proto) },
2510 { FUNC(key_mgmt) },
Dmitry Shmidt04949592012-07-19 12:16:46 -07002511 { INT(bg_scan_period) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002512 { FUNC(pairwise) },
2513 { FUNC(group) },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002514 { FUNC(group_mgmt) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002515 { FUNC(auth_alg) },
2516 { FUNC(scan_freq) },
2517 { FUNC(freq_list) },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002518 { INT_RANGE(ht, 0, 1) },
2519 { INT_RANGE(vht, 0, 1) },
Sunil Ravi640215c2023-06-28 23:08:09 +00002520 { INT_RANGE(he, 0, 1) },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002521 { INT_RANGE(ht40, -1, 1) },
Sunil8cd6f4d2022-06-28 18:40:46 +00002522 { INT_RANGE(max_oper_chwidth, CONF_OPER_CHWIDTH_USE_HT,
2523 CONF_OPER_CHWIDTH_80P80MHZ) },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002524 { INT(vht_center_freq1) },
2525 { INT(vht_center_freq2) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002526#ifdef IEEE8021X_EAPOL
2527 { FUNC(eap) },
Hai Shalomc3565922019-10-28 11:58:20 -07002528 { STR_LENe(identity, identity) },
2529 { STR_LENe(anonymous_identity, anonymous_identity) },
2530 { STR_LENe(imsi_identity, imsi_identity) },
2531 { STR_LENe(machine_identity, machine_identity) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002532 { FUNC_KEY(password) },
Hai Shalomc3565922019-10-28 11:58:20 -07002533 { FUNC_KEY(machine_password) },
2534 { STRe(ca_cert, cert.ca_cert) },
2535 { STRe(ca_path, cert.ca_path) },
2536 { STRe(client_cert, cert.client_cert) },
2537 { STRe(private_key, cert.private_key) },
2538 { STR_KEYe(private_key_passwd, cert.private_key_passwd) },
Hai Shalomc3565922019-10-28 11:58:20 -07002539 { STRe(subject_match, cert.subject_match) },
2540 { STRe(check_cert_subject, cert.check_cert_subject) },
2541 { STRe(altsubject_match, cert.altsubject_match) },
2542 { STRe(domain_suffix_match, cert.domain_suffix_match) },
2543 { STRe(domain_match, cert.domain_match) },
2544 { STRe(ca_cert2, phase2_cert.ca_cert) },
2545 { STRe(ca_path2, phase2_cert.ca_path) },
2546 { STRe(client_cert2, phase2_cert.client_cert) },
2547 { STRe(private_key2, phase2_cert.private_key) },
2548 { STR_KEYe(private_key2_passwd, phase2_cert.private_key_passwd) },
Hai Shalomc3565922019-10-28 11:58:20 -07002549 { STRe(subject_match2, phase2_cert.subject_match) },
2550 { STRe(check_cert_subject2, phase2_cert.check_cert_subject) },
2551 { STRe(altsubject_match2, phase2_cert.altsubject_match) },
2552 { STRe(domain_suffix_match2, phase2_cert.domain_suffix_match) },
2553 { STRe(domain_match2, phase2_cert.domain_match) },
2554 { STRe(phase1, phase1) },
2555 { STRe(phase2, phase2) },
2556 { STRe(machine_phase2, machine_phase2) },
2557 { STRe(pcsc, pcsc) },
2558 { STR_KEYe(pin, cert.pin) },
2559 { STRe(engine_id, cert.engine_id) },
2560 { STRe(key_id, cert.key_id) },
2561 { STRe(cert_id, cert.cert_id) },
2562 { STRe(ca_cert_id, cert.ca_cert_id) },
2563 { STR_KEYe(pin2, phase2_cert.pin) },
2564 { STRe(engine_id2, phase2_cert.engine_id) },
2565 { STRe(key_id2, phase2_cert.key_id) },
2566 { STRe(cert_id2, phase2_cert.cert_id) },
2567 { STRe(ca_cert_id2, phase2_cert.ca_cert_id) },
2568 { INTe(engine, cert.engine) },
2569 { INTe(engine2, phase2_cert.engine) },
2570 { STRe(machine_ca_cert, machine_cert.ca_cert) },
2571 { STRe(machine_ca_path, machine_cert.ca_path) },
2572 { STRe(machine_client_cert, machine_cert.client_cert) },
2573 { STRe(machine_private_key, machine_cert.private_key) },
2574 { STR_KEYe(machine_private_key_passwd,
2575 machine_cert.private_key_passwd) },
Hai Shalomc3565922019-10-28 11:58:20 -07002576 { STRe(machine_subject_match, machine_cert.subject_match) },
2577 { STRe(machine_check_cert_subject, machine_cert.check_cert_subject) },
2578 { STRe(machine_altsubject_match, machine_cert.altsubject_match) },
2579 { STRe(machine_domain_suffix_match,
2580 machine_cert.domain_suffix_match) },
2581 { STRe(machine_domain_match, machine_cert.domain_match) },
2582 { STR_KEYe(machine_pin, machine_cert.pin) },
2583 { STRe(machine_engine_id, machine_cert.engine_id) },
2584 { STRe(machine_key_id, machine_cert.key_id) },
2585 { STRe(machine_cert_id, machine_cert.cert_id) },
2586 { STRe(machine_ca_cert_id, machine_cert.ca_cert_id) },
2587 { INTe(machine_engine, machine_cert.engine) },
2588 { INTe(machine_ocsp, machine_cert.ocsp) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002589 { INT(eapol_flags) },
Hai Shalomc3565922019-10-28 11:58:20 -07002590 { INTe(sim_num, sim_num) },
Sunil8cd6f4d2022-06-28 18:40:46 +00002591 { STRe(imsi_privacy_cert, imsi_privacy_cert) },
2592 { STRe(imsi_privacy_attr, imsi_privacy_attr) },
Steven Liu9138d432022-11-23 22:29:05 +00002593 { INTe(strict_conservative_peer_mode, strict_conservative_peer_mode) },
Hai Shalomc3565922019-10-28 11:58:20 -07002594 { STRe(openssl_ciphers, openssl_ciphers) },
2595 { INTe(erp, erp) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002596#endif /* IEEE8021X_EAPOL */
Hai Shalomfdcde762020-04-02 11:19:20 -07002597#ifdef CONFIG_WEP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002598 { FUNC_KEY(wep_key0) },
2599 { FUNC_KEY(wep_key1) },
2600 { FUNC_KEY(wep_key2) },
2601 { FUNC_KEY(wep_key3) },
2602 { INT(wep_tx_keyidx) },
Hai Shalomfdcde762020-04-02 11:19:20 -07002603#endif /* CONFIG_WEP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002604 { INT(priority) },
2605#ifdef IEEE8021X_EAPOL
2606 { INT(eap_workaround) },
Hai Shalomc3565922019-10-28 11:58:20 -07002607 { STRe(pac_file, pac_file) },
2608 { INTe(fragment_size, fragment_size) },
2609 { INTe(ocsp, cert.ocsp) },
2610 { INTe(ocsp2, phase2_cert.ocsp) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002611#endif /* IEEE8021X_EAPOL */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002612#ifdef CONFIG_MESH
2613 { INT_RANGE(mode, 0, 5) },
2614 { INT_RANGE(no_auto_peer, 0, 1) },
Hai Shaloma20dcd72022-02-04 13:43:00 -08002615 { INT_RANGE(mesh_fwding, 0, 1) },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002616 { INT_RANGE(mesh_rssi_threshold, -255, 1) },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002617#else /* CONFIG_MESH */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002618 { INT_RANGE(mode, 0, 4) },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002619#endif /* CONFIG_MESH */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002620 { INT_RANGE(proactive_key_caching, 0, 1) },
2621 { INT_RANGE(disabled, 0, 2) },
2622 { STR(id_str) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002623 { INT_RANGE(ieee80211w, 0, 2) },
Hai Shalom74f70d42019-02-11 14:42:39 -08002624#ifdef CONFIG_OCV
2625 { FUNC(ocv) },
2626#endif /* CONFIG_OCV */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002627 { FUNC(peerkey) /* obsolete - removed */ },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002628 { INT_RANGE(mixed_cell, 0, 1) },
Hai Shalomc3565922019-10-28 11:58:20 -07002629 { INT_RANGE(frequency, 0, 70200) },
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002630 { INT_RANGE(fixed_freq, 0, 1) },
Hai Shalomc3565922019-10-28 11:58:20 -07002631 { INT_RANGE(enable_edmg, 0, 1) },
2632 { INT_RANGE(edmg_channel, 9, 13) },
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -08002633#ifdef CONFIG_ACS
2634 { INT_RANGE(acs, 0, 1) },
2635#endif /* CONFIG_ACS */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002636#ifdef CONFIG_MESH
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002637 { FUNC(mesh_basic_rates) },
2638 { INT(dot11MeshMaxRetries) },
2639 { INT(dot11MeshRetryTimeout) },
2640 { INT(dot11MeshConfirmTimeout) },
2641 { INT(dot11MeshHoldingTimeout) },
2642#endif /* CONFIG_MESH */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002643 { INT(wpa_ptk_rekey) },
Hai Shalomfdcde762020-04-02 11:19:20 -07002644 { INT_RANGE(wpa_deny_ptk0_rekey, 0, 2) },
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07002645 { INT(group_rekey) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002646 { STR(bgscan) },
Dmitry Shmidt04949592012-07-19 12:16:46 -07002647 { INT_RANGE(ignore_broadcast_ssid, 0, 2) },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002648#ifdef CONFIG_P2P
Dmitry Shmidt54605472013-11-08 11:10:19 -08002649 { FUNC(go_p2p_dev_addr) },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002650 { FUNC(p2p_client_list) },
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002651 { FUNC(psk_list) },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002652#endif /* CONFIG_P2P */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002653#ifdef CONFIG_HT_OVERRIDES
2654 { INT_RANGE(disable_ht, 0, 1) },
2655 { INT_RANGE(disable_ht40, -1, 1) },
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002656 { INT_RANGE(disable_sgi, 0, 1) },
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002657 { INT_RANGE(disable_ldpc, 0, 1) },
Dmitry Shmidt61593f02014-04-21 16:27:35 -07002658 { INT_RANGE(ht40_intolerant, 0, 1) },
Hai Shalom74f70d42019-02-11 14:42:39 -08002659 { INT_RANGE(tx_stbc, -1, 1) },
2660 { INT_RANGE(rx_stbc, -1, 3) },
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002661 { INT_RANGE(disable_max_amsdu, -1, 1) },
2662 { INT_RANGE(ampdu_factor, -1, 3) },
2663 { INT_RANGE(ampdu_density, -1, 7) },
2664 { STR(ht_mcs) },
2665#endif /* CONFIG_HT_OVERRIDES */
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002666#ifdef CONFIG_VHT_OVERRIDES
2667 { INT_RANGE(disable_vht, 0, 1) },
2668 { INT(vht_capa) },
2669 { INT(vht_capa_mask) },
2670 { INT_RANGE(vht_rx_mcs_nss_1, -1, 3) },
2671 { INT_RANGE(vht_rx_mcs_nss_2, -1, 3) },
2672 { INT_RANGE(vht_rx_mcs_nss_3, -1, 3) },
2673 { INT_RANGE(vht_rx_mcs_nss_4, -1, 3) },
2674 { INT_RANGE(vht_rx_mcs_nss_5, -1, 3) },
2675 { INT_RANGE(vht_rx_mcs_nss_6, -1, 3) },
2676 { INT_RANGE(vht_rx_mcs_nss_7, -1, 3) },
2677 { INT_RANGE(vht_rx_mcs_nss_8, -1, 3) },
2678 { INT_RANGE(vht_tx_mcs_nss_1, -1, 3) },
2679 { INT_RANGE(vht_tx_mcs_nss_2, -1, 3) },
2680 { INT_RANGE(vht_tx_mcs_nss_3, -1, 3) },
2681 { INT_RANGE(vht_tx_mcs_nss_4, -1, 3) },
2682 { INT_RANGE(vht_tx_mcs_nss_5, -1, 3) },
2683 { INT_RANGE(vht_tx_mcs_nss_6, -1, 3) },
2684 { INT_RANGE(vht_tx_mcs_nss_7, -1, 3) },
2685 { INT_RANGE(vht_tx_mcs_nss_8, -1, 3) },
2686#endif /* CONFIG_VHT_OVERRIDES */
Hai Shalomfdcde762020-04-02 11:19:20 -07002687#ifdef CONFIG_HE_OVERRIDES
2688 { INT_RANGE(disable_he, 0, 1)},
2689#endif /* CONFIG_HE_OVERRIDES */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002690 { INT(ap_max_inactivity) },
2691 { INT(dtim_period) },
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002692 { INT(beacon_int) },
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07002693#ifdef CONFIG_MACSEC
2694 { INT_RANGE(macsec_policy, 0, 1) },
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002695 { INT_RANGE(macsec_integ_only, 0, 1) },
Hai Shalom74f70d42019-02-11 14:42:39 -08002696 { INT_RANGE(macsec_replay_protect, 0, 1) },
2697 { INT(macsec_replay_window) },
Sunil Ravi036cec52023-03-29 11:35:17 -07002698 { INT_RANGE(macsec_offload, 0, 2) },
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002699 { INT_RANGE(macsec_port, 1, 65534) },
Dmitry Shmidt29333592017-01-09 12:27:11 -08002700 { INT_RANGE(mka_priority, 0, 255) },
Sunil Ravia04bd252022-05-02 22:54:18 -07002701 { INT_RANGE(macsec_csindex, 0, 1) },
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002702 { FUNC_KEY(mka_cak) },
2703 { FUNC_KEY(mka_ckn) },
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07002704#endif /* CONFIG_MACSEC */
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002705#ifdef CONFIG_HS20
2706 { INT(update_identifier) },
Roshan Pius3a1667e2018-07-03 15:17:14 -07002707 { STR_RANGE(roaming_consortium_selection, 0, MAX_ROAMING_CONS_OI_LEN) },
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002708#endif /* CONFIG_HS20 */
Sunil Ravi77d572f2023-01-17 23:58:31 +00002709 { INT_RANGE(mac_addr, 0, 3) },
2710 { FUNC_KEY(mac_value) },
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002711 { INT_RANGE(pbss, 0, 2) },
2712 { INT_RANGE(wps_disabled, 0, 1) },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002713 { INT_RANGE(fils_dh_group, 0, 65535) },
2714#ifdef CONFIG_DPP
2715 { STR(dpp_connector) },
2716 { STR_LEN(dpp_netaccesskey) },
2717 { INT(dpp_netaccesskey_expiry) },
2718 { STR_LEN(dpp_csign) },
Hai Shalom899fcc72020-10-19 14:38:18 -07002719 { STR_LEN(dpp_pp_key) },
Hai Shalomfdcde762020-04-02 11:19:20 -07002720 { INT_RANGE(dpp_pfs, 0, 2) },
Sunil Ravi89eba102022-09-13 21:04:37 -07002721 { INT_RANGE(dpp_connector_privacy, 0, 1) },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002722#endif /* CONFIG_DPP */
2723 { INT_RANGE(owe_group, 0, 65535) },
Roshan Pius3a1667e2018-07-03 15:17:14 -07002724 { INT_RANGE(owe_only, 0, 1) },
Hai Shalomfdcde762020-04-02 11:19:20 -07002725 { INT_RANGE(owe_ptk_workaround, 0, 1) },
Hai Shalom74f70d42019-02-11 14:42:39 -08002726 { INT_RANGE(multi_ap_backhaul_sta, 0, 1) },
Hai Shalom81f62d82019-07-22 12:10:00 -07002727 { INT_RANGE(ft_eap_pmksa_caching, 0, 1) },
Hai Shalomfdcde762020-04-02 11:19:20 -07002728 { INT_RANGE(beacon_prot, 0, 1) },
2729 { INT_RANGE(transition_disable, 0, 255) },
Hai Shalom899fcc72020-10-19 14:38:18 -07002730 { INT_RANGE(sae_pk, 0, 2) },
Sunil Ravi77d572f2023-01-17 23:58:31 +00002731 { INT_RANGE(disable_eht, 0, 1)},
Sunil Ravi036cec52023-03-29 11:35:17 -07002732 { INT_RANGE(enable_4addr_mode, 0, 1)},
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002733};
2734
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002735#undef OFFSET
2736#undef _STR
2737#undef STR
2738#undef STR_KEY
2739#undef _STR_LEN
2740#undef STR_LEN
2741#undef STR_LEN_KEY
2742#undef _STR_RANGE
2743#undef STR_RANGE
2744#undef STR_RANGE_KEY
2745#undef _INT
2746#undef INT
2747#undef INT_RANGE
2748#undef _FUNC
2749#undef FUNC
2750#undef FUNC_KEY
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002751#define NUM_SSID_FIELDS ARRAY_SIZE(ssid_fields)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002752
2753
2754/**
2755 * wpa_config_add_prio_network - Add a network to priority lists
2756 * @config: Configuration data from wpa_config_read()
2757 * @ssid: Pointer to the network configuration to be added to the list
2758 * Returns: 0 on success, -1 on failure
2759 *
2760 * This function is used to add a network block to the priority list of
2761 * networks. This must be called for each network when reading in the full
2762 * configuration. In addition, this can be used indirectly when updating
2763 * priorities by calling wpa_config_update_prio_list().
2764 */
2765int wpa_config_add_prio_network(struct wpa_config *config,
2766 struct wpa_ssid *ssid)
2767{
Hai Shalomfdcde762020-04-02 11:19:20 -07002768 size_t prio;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002769 struct wpa_ssid *prev, **nlist;
2770
2771 /*
2772 * Add to an existing priority list if one is available for the
2773 * configured priority level for this network.
2774 */
2775 for (prio = 0; prio < config->num_prio; prio++) {
2776 prev = config->pssid[prio];
2777 if (prev->priority == ssid->priority) {
2778 while (prev->pnext)
2779 prev = prev->pnext;
2780 prev->pnext = ssid;
2781 return 0;
2782 }
2783 }
2784
2785 /* First network for this priority - add a new priority list */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002786 nlist = os_realloc_array(config->pssid, config->num_prio + 1,
2787 sizeof(struct wpa_ssid *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002788 if (nlist == NULL)
2789 return -1;
2790
2791 for (prio = 0; prio < config->num_prio; prio++) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002792 if (nlist[prio]->priority < ssid->priority) {
2793 os_memmove(&nlist[prio + 1], &nlist[prio],
2794 (config->num_prio - prio) *
2795 sizeof(struct wpa_ssid *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002796 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002797 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002798 }
2799
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002800 nlist[prio] = ssid;
2801 config->num_prio++;
2802 config->pssid = nlist;
2803
2804 return 0;
2805}
2806
2807
2808/**
2809 * wpa_config_update_prio_list - Update network priority list
2810 * @config: Configuration data from wpa_config_read()
2811 * Returns: 0 on success, -1 on failure
2812 *
2813 * This function is called to update the priority list of networks in the
2814 * configuration when a network is being added or removed. This is also called
2815 * if a priority for a network is changed.
2816 */
2817int wpa_config_update_prio_list(struct wpa_config *config)
2818{
2819 struct wpa_ssid *ssid;
2820 int ret = 0;
2821
2822 os_free(config->pssid);
2823 config->pssid = NULL;
2824 config->num_prio = 0;
2825
2826 ssid = config->ssid;
2827 while (ssid) {
2828 ssid->pnext = NULL;
2829 if (wpa_config_add_prio_network(config, ssid) < 0)
2830 ret = -1;
2831 ssid = ssid->next;
2832 }
2833
2834 return ret;
2835}
2836
2837
2838#ifdef IEEE8021X_EAPOL
Hai Shalomc3565922019-10-28 11:58:20 -07002839
2840static void eap_peer_config_free_cert(struct eap_peer_cert_config *cert)
2841{
2842 os_free(cert->ca_cert);
2843 os_free(cert->ca_path);
2844 os_free(cert->client_cert);
2845 os_free(cert->private_key);
2846 str_clear_free(cert->private_key_passwd);
Hai Shalomc3565922019-10-28 11:58:20 -07002847 os_free(cert->subject_match);
2848 os_free(cert->check_cert_subject);
2849 os_free(cert->altsubject_match);
2850 os_free(cert->domain_suffix_match);
2851 os_free(cert->domain_match);
2852 str_clear_free(cert->pin);
2853 os_free(cert->engine_id);
2854 os_free(cert->key_id);
2855 os_free(cert->cert_id);
2856 os_free(cert->ca_cert_id);
2857}
2858
2859
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002860static void eap_peer_config_free(struct eap_peer_config *eap)
2861{
2862 os_free(eap->eap_methods);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002863 bin_clear_free(eap->identity, eap->identity_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002864 os_free(eap->anonymous_identity);
Jouni Malinena3cb6f02017-12-08 17:05:40 +02002865 os_free(eap->imsi_identity);
Sunil8cd6f4d2022-06-28 18:40:46 +00002866 os_free(eap->imsi_privacy_cert);
2867 os_free(eap->imsi_privacy_attr);
Hai Shalomc3565922019-10-28 11:58:20 -07002868 os_free(eap->machine_identity);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002869 bin_clear_free(eap->password, eap->password_len);
Hai Shalomc3565922019-10-28 11:58:20 -07002870 bin_clear_free(eap->machine_password, eap->machine_password_len);
2871 eap_peer_config_free_cert(&eap->cert);
2872 eap_peer_config_free_cert(&eap->phase2_cert);
2873 eap_peer_config_free_cert(&eap->machine_cert);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002874 os_free(eap->phase1);
2875 os_free(eap->phase2);
Hai Shalomc3565922019-10-28 11:58:20 -07002876 os_free(eap->machine_phase2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002877 os_free(eap->pcsc);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002878 os_free(eap->otp);
2879 os_free(eap->pending_req_otp);
2880 os_free(eap->pac_file);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002881 bin_clear_free(eap->new_password, eap->new_password_len);
2882 str_clear_free(eap->external_sim_resp);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002883 os_free(eap->openssl_ciphers);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002884}
Hai Shalomc3565922019-10-28 11:58:20 -07002885
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002886#endif /* IEEE8021X_EAPOL */
2887
2888
2889/**
2890 * wpa_config_free_ssid - Free network/ssid configuration data
2891 * @ssid: Configuration data for the network
2892 *
2893 * This function frees all resources allocated for the network configuration
2894 * data.
2895 */
2896void wpa_config_free_ssid(struct wpa_ssid *ssid)
2897{
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002898 struct psk_list_entry *psk;
2899
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002900 os_free(ssid->ssid);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002901 str_clear_free(ssid->passphrase);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002902 os_free(ssid->ext_psk);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002903 str_clear_free(ssid->sae_password);
Roshan Pius3a1667e2018-07-03 15:17:14 -07002904 os_free(ssid->sae_password_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002905#ifdef IEEE8021X_EAPOL
2906 eap_peer_config_free(&ssid->eap);
2907#endif /* IEEE8021X_EAPOL */
2908 os_free(ssid->id_str);
2909 os_free(ssid->scan_freq);
2910 os_free(ssid->freq_list);
2911 os_free(ssid->bgscan);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002912 os_free(ssid->p2p_client_list);
Hai Shalom60840252021-02-19 19:02:11 -08002913 os_free(ssid->bssid_ignore);
2914 os_free(ssid->bssid_accept);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002915#ifdef CONFIG_HT_OVERRIDES
2916 os_free(ssid->ht_mcs);
2917#endif /* CONFIG_HT_OVERRIDES */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002918#ifdef CONFIG_MESH
2919 os_free(ssid->mesh_basic_rates);
2920#endif /* CONFIG_MESH */
Roshan Pius3a1667e2018-07-03 15:17:14 -07002921#ifdef CONFIG_HS20
2922 os_free(ssid->roaming_consortium_selection);
2923#endif /* CONFIG_HS20 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002924 os_free(ssid->dpp_connector);
2925 bin_clear_free(ssid->dpp_netaccesskey, ssid->dpp_netaccesskey_len);
2926 os_free(ssid->dpp_csign);
Hai Shalom899fcc72020-10-19 14:38:18 -07002927 os_free(ssid->dpp_pp_key);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002928 while ((psk = dl_list_first(&ssid->psk_list, struct psk_list_entry,
2929 list))) {
2930 dl_list_del(&psk->list);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002931 bin_clear_free(psk, sizeof(*psk));
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002932 }
Hai Shalomc3565922019-10-28 11:58:20 -07002933#ifdef CONFIG_SAE
2934 sae_deinit_pt(ssid->pt);
2935#endif /* CONFIG_SAE */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002936 bin_clear_free(ssid, sizeof(*ssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002937}
2938
2939
Dmitry Shmidt04949592012-07-19 12:16:46 -07002940void wpa_config_free_cred(struct wpa_cred *cred)
2941{
Dmitry Shmidt051af732013-10-22 13:52:46 -07002942 size_t i;
2943
Dmitry Shmidt04949592012-07-19 12:16:46 -07002944 os_free(cred->realm);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002945 str_clear_free(cred->username);
2946 str_clear_free(cred->password);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002947 os_free(cred->ca_cert);
2948 os_free(cred->client_cert);
2949 os_free(cred->private_key);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002950 str_clear_free(cred->private_key_passwd);
Hai Shaloma20dcd72022-02-04 13:43:00 -08002951 os_free(cred->engine_id);
2952 os_free(cred->ca_cert_id);
2953 os_free(cred->cert_id);
2954 os_free(cred->key_id);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002955 os_free(cred->imsi);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002956 str_clear_free(cred->milenage);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002957 for (i = 0; i < cred->num_domain; i++)
2958 os_free(cred->domain[i]);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002959 os_free(cred->domain);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002960 os_free(cred->domain_suffix_match);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002961 os_free(cred->eap_method);
2962 os_free(cred->phase1);
2963 os_free(cred->phase2);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002964 os_free(cred->excluded_ssid);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002965 os_free(cred->roaming_partner);
2966 os_free(cred->provisioning_sp);
2967 for (i = 0; i < cred->num_req_conn_capab; i++)
2968 os_free(cred->req_conn_capab_port[i]);
2969 os_free(cred->req_conn_capab_port);
2970 os_free(cred->req_conn_capab_proto);
Sunil8cd6f4d2022-06-28 18:40:46 +00002971 os_free(cred->imsi_privacy_cert);
2972 os_free(cred->imsi_privacy_attr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002973 os_free(cred);
2974}
2975
2976
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002977void wpa_config_flush_blobs(struct wpa_config *config)
2978{
2979#ifndef CONFIG_NO_CONFIG_BLOBS
2980 struct wpa_config_blob *blob, *prev;
2981
2982 blob = config->blobs;
2983 config->blobs = NULL;
2984 while (blob) {
2985 prev = blob;
2986 blob = blob->next;
2987 wpa_config_free_blob(prev);
2988 }
2989#endif /* CONFIG_NO_CONFIG_BLOBS */
2990}
2991
2992
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002993/**
2994 * wpa_config_free - Free configuration data
2995 * @config: Configuration data from wpa_config_read()
2996 *
2997 * This function frees all resources allocated for the configuration data by
2998 * wpa_config_read().
2999 */
3000void wpa_config_free(struct wpa_config *config)
3001{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003002 struct wpa_ssid *ssid, *prev = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003003 struct wpa_cred *cred, *cprev;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003004 int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003005
3006 ssid = config->ssid;
3007 while (ssid) {
3008 prev = ssid;
3009 ssid = ssid->next;
3010 wpa_config_free_ssid(prev);
3011 }
3012
Dmitry Shmidt04949592012-07-19 12:16:46 -07003013 cred = config->cred;
3014 while (cred) {
3015 cprev = cred;
3016 cred = cred->next;
3017 wpa_config_free_cred(cprev);
3018 }
3019
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003020 wpa_config_flush_blobs(config);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003021
Dmitry Shmidt04949592012-07-19 12:16:46 -07003022 wpabuf_free(config->wps_vendor_ext_m1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003023 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++)
3024 wpabuf_free(config->wps_vendor_ext[i]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003025 os_free(config->ctrl_interface);
3026 os_free(config->ctrl_interface_group);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00003027#ifndef CONFIG_OPENSC_ENGINE_PATH
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003028 os_free(config->opensc_engine_path);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00003029#endif /* CONFIG_OPENSC_ENGINE_PATH */
3030#ifndef CONFIG_PKCS11_ENGINE_PATH
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003031 os_free(config->pkcs11_engine_path);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00003032#endif /* CONFIG_PKCS11_ENGINE_PATH */
3033#ifndef CONFIG_PKCS11_MODULE_PATH
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003034 os_free(config->pkcs11_module_path);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00003035#endif /* CONFIG_PKCS11_MODULE_PATH */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003036 os_free(config->openssl_ciphers);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003037 os_free(config->pcsc_reader);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003038 str_clear_free(config->pcsc_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003039 os_free(config->driver_param);
3040 os_free(config->device_name);
3041 os_free(config->manufacturer);
3042 os_free(config->model_name);
3043 os_free(config->model_number);
3044 os_free(config->serial_number);
3045 os_free(config->config_methods);
3046 os_free(config->p2p_ssid_postfix);
3047 os_free(config->pssid);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003048 os_free(config->p2p_pref_chan);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003049 os_free(config->p2p_no_go_freq.range);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003050 os_free(config->autoscan);
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07003051 os_free(config->freq_list);
Hai Shalom60840252021-02-19 19:02:11 -08003052 os_free(config->initial_freq_list);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003053 wpabuf_free(config->wps_nfc_dh_pubkey);
3054 wpabuf_free(config->wps_nfc_dh_privkey);
3055 wpabuf_free(config->wps_nfc_dev_pw);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003056 os_free(config->ext_password_backend);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003057 os_free(config->sae_groups);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07003058 wpabuf_free(config->ap_vendor_elements);
Hai Shaloma20dcd72022-02-04 13:43:00 -08003059 wpabuf_free(config->ap_assocresp_elements);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003060 os_free(config->osu_dir);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003061 os_free(config->bgscan);
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07003062 os_free(config->wowlan_triggers);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003063 os_free(config->fst_group_id);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003064 os_free(config->sched_scan_plans);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003065#ifdef CONFIG_MBO
3066 os_free(config->non_pref_chan);
3067#endif /* CONFIG_MBO */
Hai Shalomc3565922019-10-28 11:58:20 -07003068 os_free(config->dpp_name);
3069 os_free(config->dpp_mud_url);
Sunil Ravi89eba102022-09-13 21:04:37 -07003070 os_free(config->dpp_extra_conf_req_name);
3071 os_free(config->dpp_extra_conf_req_value);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003072
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003073 os_free(config);
3074}
3075
3076
3077/**
3078 * wpa_config_foreach_network - Iterate over each configured network
3079 * @config: Configuration data from wpa_config_read()
3080 * @func: Callback function to process each network
3081 * @arg: Opaque argument to pass to callback function
3082 *
3083 * Iterate over the set of configured networks calling the specified
3084 * function for each item. We guard against callbacks removing the
3085 * supplied network.
3086 */
3087void wpa_config_foreach_network(struct wpa_config *config,
3088 void (*func)(void *, struct wpa_ssid *),
3089 void *arg)
3090{
3091 struct wpa_ssid *ssid, *next;
3092
3093 ssid = config->ssid;
3094 while (ssid) {
3095 next = ssid->next;
3096 func(arg, ssid);
3097 ssid = next;
3098 }
3099}
3100
3101
3102/**
3103 * wpa_config_get_network - Get configured network based on id
3104 * @config: Configuration data from wpa_config_read()
3105 * @id: Unique network id to search for
3106 * Returns: Network configuration or %NULL if not found
3107 */
3108struct wpa_ssid * wpa_config_get_network(struct wpa_config *config, int id)
3109{
3110 struct wpa_ssid *ssid;
3111
3112 ssid = config->ssid;
3113 while (ssid) {
3114 if (id == ssid->id)
3115 break;
3116 ssid = ssid->next;
3117 }
3118
3119 return ssid;
3120}
3121
3122
3123/**
3124 * wpa_config_add_network - Add a new network with empty configuration
3125 * @config: Configuration data from wpa_config_read()
3126 * Returns: The new network configuration or %NULL if operation failed
3127 */
3128struct wpa_ssid * wpa_config_add_network(struct wpa_config *config)
3129{
3130 int id;
3131 struct wpa_ssid *ssid, *last = NULL;
3132
3133 id = -1;
3134 ssid = config->ssid;
3135 while (ssid) {
3136 if (ssid->id > id)
3137 id = ssid->id;
3138 last = ssid;
3139 ssid = ssid->next;
3140 }
3141 id++;
3142
3143 ssid = os_zalloc(sizeof(*ssid));
3144 if (ssid == NULL)
3145 return NULL;
3146 ssid->id = id;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003147 dl_list_init(&ssid->psk_list);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003148 if (last)
3149 last->next = ssid;
3150 else
3151 config->ssid = ssid;
3152
3153 wpa_config_update_prio_list(config);
3154
3155 return ssid;
3156}
3157
3158
3159/**
3160 * wpa_config_remove_network - Remove a configured network based on id
3161 * @config: Configuration data from wpa_config_read()
3162 * @id: Unique network id to search for
3163 * Returns: 0 on success, or -1 if the network was not found
3164 */
3165int wpa_config_remove_network(struct wpa_config *config, int id)
3166{
3167 struct wpa_ssid *ssid, *prev = NULL;
3168
3169 ssid = config->ssid;
3170 while (ssid) {
3171 if (id == ssid->id)
3172 break;
3173 prev = ssid;
3174 ssid = ssid->next;
3175 }
3176
3177 if (ssid == NULL)
3178 return -1;
3179
3180 if (prev)
3181 prev->next = ssid->next;
3182 else
3183 config->ssid = ssid->next;
3184
3185 wpa_config_update_prio_list(config);
3186 wpa_config_free_ssid(ssid);
3187 return 0;
3188}
3189
3190
3191/**
3192 * wpa_config_set_network_defaults - Set network default values
3193 * @ssid: Pointer to network configuration data
3194 */
3195void wpa_config_set_network_defaults(struct wpa_ssid *ssid)
3196{
3197 ssid->proto = DEFAULT_PROTO;
3198 ssid->pairwise_cipher = DEFAULT_PAIRWISE;
3199 ssid->group_cipher = DEFAULT_GROUP;
3200 ssid->key_mgmt = DEFAULT_KEY_MGMT;
Hai Shalomfdcde762020-04-02 11:19:20 -07003201 ssid->wpa_deny_ptk0_rekey = PTK0_REKEY_ALLOW_ALWAYS;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003202 ssid->bg_scan_period = DEFAULT_BG_SCAN_PERIOD;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003203 ssid->ht = 1;
Hai Shalom899fcc72020-10-19 14:38:18 -07003204 ssid->vht = 1;
3205 ssid->he = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003206#ifdef IEEE8021X_EAPOL
3207 ssid->eapol_flags = DEFAULT_EAPOL_FLAGS;
3208 ssid->eap_workaround = DEFAULT_EAP_WORKAROUND;
3209 ssid->eap.fragment_size = DEFAULT_FRAGMENT_SIZE;
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -07003210 ssid->eap.sim_num = DEFAULT_USER_SELECTED_SIM;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003211#endif /* IEEE8021X_EAPOL */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003212#ifdef CONFIG_MESH
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003213 ssid->dot11MeshMaxRetries = DEFAULT_MESH_MAX_RETRIES;
3214 ssid->dot11MeshRetryTimeout = DEFAULT_MESH_RETRY_TIMEOUT;
3215 ssid->dot11MeshConfirmTimeout = DEFAULT_MESH_CONFIRM_TIMEOUT;
3216 ssid->dot11MeshHoldingTimeout = DEFAULT_MESH_HOLDING_TIMEOUT;
Hai Shaloma20dcd72022-02-04 13:43:00 -08003217 ssid->mesh_fwding = DEFAULT_MESH_FWDING;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003218 ssid->mesh_rssi_threshold = DEFAULT_MESH_RSSI_THRESHOLD;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003219#endif /* CONFIG_MESH */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003220#ifdef CONFIG_HT_OVERRIDES
3221 ssid->disable_ht = DEFAULT_DISABLE_HT;
3222 ssid->disable_ht40 = DEFAULT_DISABLE_HT40;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003223 ssid->disable_sgi = DEFAULT_DISABLE_SGI;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07003224 ssid->disable_ldpc = DEFAULT_DISABLE_LDPC;
Hai Shalom74f70d42019-02-11 14:42:39 -08003225 ssid->tx_stbc = DEFAULT_TX_STBC;
3226 ssid->rx_stbc = DEFAULT_RX_STBC;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003227 ssid->disable_max_amsdu = DEFAULT_DISABLE_MAX_AMSDU;
3228 ssid->ampdu_factor = DEFAULT_AMPDU_FACTOR;
3229 ssid->ampdu_density = DEFAULT_AMPDU_DENSITY;
3230#endif /* CONFIG_HT_OVERRIDES */
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003231#ifdef CONFIG_VHT_OVERRIDES
3232 ssid->vht_rx_mcs_nss_1 = -1;
3233 ssid->vht_rx_mcs_nss_2 = -1;
3234 ssid->vht_rx_mcs_nss_3 = -1;
3235 ssid->vht_rx_mcs_nss_4 = -1;
3236 ssid->vht_rx_mcs_nss_5 = -1;
3237 ssid->vht_rx_mcs_nss_6 = -1;
3238 ssid->vht_rx_mcs_nss_7 = -1;
3239 ssid->vht_rx_mcs_nss_8 = -1;
3240 ssid->vht_tx_mcs_nss_1 = -1;
3241 ssid->vht_tx_mcs_nss_2 = -1;
3242 ssid->vht_tx_mcs_nss_3 = -1;
3243 ssid->vht_tx_mcs_nss_4 = -1;
3244 ssid->vht_tx_mcs_nss_5 = -1;
3245 ssid->vht_tx_mcs_nss_6 = -1;
3246 ssid->vht_tx_mcs_nss_7 = -1;
3247 ssid->vht_tx_mcs_nss_8 = -1;
3248#endif /* CONFIG_VHT_OVERRIDES */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003249 ssid->proactive_key_caching = -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003250 ssid->ieee80211w = MGMT_FRAME_PROTECTION_DEFAULT;
Hai Shaloma20dcd72022-02-04 13:43:00 -08003251 ssid->sae_pwe = DEFAULT_SAE_PWE;
Dmitry Shmidt29333592017-01-09 12:27:11 -08003252#ifdef CONFIG_MACSEC
3253 ssid->mka_priority = DEFAULT_PRIO_NOT_KEY_SERVER;
3254#endif /* CONFIG_MACSEC */
Sunil Ravi77d572f2023-01-17 23:58:31 +00003255 ssid->mac_addr = WPAS_MAC_ADDR_STYLE_NOT_SET;
Hai Shalom74f70d42019-02-11 14:42:39 -08003256 ssid->max_oper_chwidth = DEFAULT_MAX_OPER_CHWIDTH;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003257}
3258
3259
Sunil Ravia04bd252022-05-02 22:54:18 -07003260static const char *removed_fields[] = {
3261 "dh_file",
3262 "dh_file2",
3263 "machine_dh_file",
3264 NULL
3265};
3266
3267static bool removed_field(const char *field)
3268{
3269 int i;
3270
3271 for (i = 0; removed_fields[i]; i++) {
3272 if (os_strcmp(field, removed_fields[i]) == 0)
3273 return true;
3274 }
3275
3276 return false;
3277}
3278
3279
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003280/**
3281 * wpa_config_set - Set a variable in network configuration
3282 * @ssid: Pointer to network configuration data
3283 * @var: Variable name, e.g., "ssid"
3284 * @value: Variable value
3285 * @line: Line number in configuration file or 0 if not used
Dmitry Shmidte4663042016-04-04 10:07:49 -07003286 * Returns: 0 on success with possible change in the value, 1 on success with
3287 * no change to previously configured value, or -1 on failure
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003288 *
3289 * This function can be used to set network configuration variables based on
3290 * both the configuration file and management interface input. The value
3291 * parameter must be in the same format as the text-based configuration file is
3292 * using. For example, strings are using double quotation marks.
3293 */
3294int wpa_config_set(struct wpa_ssid *ssid, const char *var, const char *value,
3295 int line)
3296{
3297 size_t i;
3298 int ret = 0;
3299
3300 if (ssid == NULL || var == NULL || value == NULL)
3301 return -1;
3302
3303 for (i = 0; i < NUM_SSID_FIELDS; i++) {
3304 const struct parse_data *field = &ssid_fields[i];
3305 if (os_strcmp(var, field->name) != 0)
3306 continue;
3307
Dmitry Shmidte4663042016-04-04 10:07:49 -07003308 ret = field->parser(field, ssid, line, value);
3309 if (ret < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003310 if (line) {
3311 wpa_printf(MSG_ERROR, "Line %d: failed to "
3312 "parse %s '%s'.", line, var, value);
3313 }
3314 ret = -1;
3315 }
Hai Shalomc3565922019-10-28 11:58:20 -07003316#ifdef CONFIG_SAE
3317 if (os_strcmp(var, "ssid") == 0 ||
3318 os_strcmp(var, "psk") == 0 ||
3319 os_strcmp(var, "sae_password") == 0 ||
3320 os_strcmp(var, "sae_password_id") == 0) {
3321 sae_deinit_pt(ssid->pt);
3322 ssid->pt = NULL;
3323 }
3324#endif /* CONFIG_SAE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003325 break;
3326 }
3327 if (i == NUM_SSID_FIELDS) {
Sunil Ravia04bd252022-05-02 22:54:18 -07003328 if (removed_field(var)) {
3329 wpa_printf(MSG_INFO,
3330 "Line %d: Ignore removed configuration field '%s'",
3331 line, var);
3332 return ret;
3333 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003334 if (line) {
3335 wpa_printf(MSG_ERROR, "Line %d: unknown network field "
3336 "'%s'.", line, var);
3337 }
3338 ret = -1;
3339 }
Hai Shalom899fcc72020-10-19 14:38:18 -07003340 ssid->was_recently_reconfigured = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003341
3342 return ret;
3343}
3344
3345
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003346int wpa_config_set_quoted(struct wpa_ssid *ssid, const char *var,
3347 const char *value)
3348{
3349 size_t len;
3350 char *buf;
3351 int ret;
3352
3353 len = os_strlen(value);
3354 buf = os_malloc(len + 3);
3355 if (buf == NULL)
3356 return -1;
3357 buf[0] = '"';
3358 os_memcpy(buf + 1, value, len);
3359 buf[len + 1] = '"';
3360 buf[len + 2] = '\0';
3361 ret = wpa_config_set(ssid, var, buf, 0);
3362 os_free(buf);
3363 return ret;
3364}
3365
3366
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003367/**
3368 * wpa_config_get_all - Get all options from network configuration
3369 * @ssid: Pointer to network configuration data
3370 * @get_keys: Determines if keys/passwords will be included in returned list
3371 * (if they may be exported)
3372 * Returns: %NULL terminated list of all set keys and their values in the form
3373 * of [key1, val1, key2, val2, ... , NULL]
3374 *
3375 * This function can be used to get list of all configured network properties.
3376 * The caller is responsible for freeing the returned list and all its
3377 * elements.
3378 */
3379char ** wpa_config_get_all(struct wpa_ssid *ssid, int get_keys)
3380{
Dmitry Shmidt83474442015-04-15 13:47:09 -07003381#ifdef NO_CONFIG_WRITE
3382 return NULL;
3383#else /* NO_CONFIG_WRITE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003384 const struct parse_data *field;
3385 char *key, *value;
3386 size_t i;
3387 char **props;
3388 int fields_num;
3389
3390 get_keys = get_keys && ssid->export_keys;
3391
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003392 props = os_calloc(2 * NUM_SSID_FIELDS + 1, sizeof(char *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003393 if (!props)
3394 return NULL;
3395
3396 fields_num = 0;
3397 for (i = 0; i < NUM_SSID_FIELDS; i++) {
3398 field = &ssid_fields[i];
3399 if (field->key_data && !get_keys)
3400 continue;
3401 value = field->writer(field, ssid);
3402 if (value == NULL)
3403 continue;
3404 if (os_strlen(value) == 0) {
3405 os_free(value);
3406 continue;
3407 }
3408
3409 key = os_strdup(field->name);
3410 if (key == NULL) {
3411 os_free(value);
3412 goto err;
3413 }
3414
3415 props[fields_num * 2] = key;
3416 props[fields_num * 2 + 1] = value;
3417
3418 fields_num++;
3419 }
3420
3421 return props;
3422
3423err:
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07003424 for (i = 0; props[i]; i++)
3425 os_free(props[i]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003426 os_free(props);
3427 return NULL;
Dmitry Shmidt83474442015-04-15 13:47:09 -07003428#endif /* NO_CONFIG_WRITE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003429}
3430
3431
3432#ifndef NO_CONFIG_WRITE
3433/**
3434 * wpa_config_get - Get a variable in network configuration
3435 * @ssid: Pointer to network configuration data
3436 * @var: Variable name, e.g., "ssid"
3437 * Returns: Value of the variable or %NULL on failure
3438 *
3439 * This function can be used to get network configuration variables. The
3440 * returned value is a copy of the configuration variable in text format, i.e,.
3441 * the same format that the text-based configuration file and wpa_config_set()
3442 * are using for the value. The caller is responsible for freeing the returned
3443 * value.
3444 */
3445char * wpa_config_get(struct wpa_ssid *ssid, const char *var)
3446{
3447 size_t i;
3448
3449 if (ssid == NULL || var == NULL)
3450 return NULL;
3451
3452 for (i = 0; i < NUM_SSID_FIELDS; i++) {
3453 const struct parse_data *field = &ssid_fields[i];
Paul Stewart85c72c62016-03-03 15:33:47 -08003454 if (os_strcmp(var, field->name) == 0) {
3455 char *ret = field->writer(field, ssid);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003456
3457 if (ret && has_newline(ret)) {
Paul Stewart35800752016-03-18 18:14:26 -07003458 wpa_printf(MSG_ERROR,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003459 "Found newline in value for %s; not returning it",
3460 var);
Paul Stewart85c72c62016-03-03 15:33:47 -08003461 os_free(ret);
3462 ret = NULL;
3463 }
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003464
Paul Stewart85c72c62016-03-03 15:33:47 -08003465 return ret;
3466 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003467 }
3468
3469 return NULL;
3470}
3471
3472
3473/**
3474 * wpa_config_get_no_key - Get a variable in network configuration (no keys)
3475 * @ssid: Pointer to network configuration data
3476 * @var: Variable name, e.g., "ssid"
3477 * Returns: Value of the variable or %NULL on failure
3478 *
3479 * This function can be used to get network configuration variable like
3480 * wpa_config_get(). The only difference is that this functions does not expose
3481 * key/password material from the configuration. In case a key/password field
3482 * is requested, the returned value is an empty string or %NULL if the variable
3483 * is not set or "*" if the variable is set (regardless of its value). The
3484 * returned value is a copy of the configuration variable in text format, i.e,.
3485 * the same format that the text-based configuration file and wpa_config_set()
3486 * are using for the value. The caller is responsible for freeing the returned
3487 * value.
3488 */
3489char * wpa_config_get_no_key(struct wpa_ssid *ssid, const char *var)
3490{
3491 size_t i;
3492
3493 if (ssid == NULL || var == NULL)
3494 return NULL;
3495
3496 for (i = 0; i < NUM_SSID_FIELDS; i++) {
3497 const struct parse_data *field = &ssid_fields[i];
3498 if (os_strcmp(var, field->name) == 0) {
3499 char *res = field->writer(field, ssid);
3500 if (field->key_data) {
3501 if (res && res[0]) {
3502 wpa_printf(MSG_DEBUG, "Do not allow "
3503 "key_data field to be "
3504 "exposed");
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003505 str_clear_free(res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003506 return os_strdup("*");
3507 }
3508
3509 os_free(res);
3510 return NULL;
3511 }
3512 return res;
3513 }
3514 }
3515
3516 return NULL;
3517}
3518#endif /* NO_CONFIG_WRITE */
3519
3520
3521/**
3522 * wpa_config_update_psk - Update WPA PSK based on passphrase and SSID
3523 * @ssid: Pointer to network configuration data
3524 *
3525 * This function must be called to update WPA PSK when either SSID or the
3526 * passphrase has changed for the network configuration.
3527 */
3528void wpa_config_update_psk(struct wpa_ssid *ssid)
3529{
3530#ifndef CONFIG_NO_PBKDF2
Sunil Ravia04bd252022-05-02 22:54:18 -07003531 if (pbkdf2_sha1(ssid->passphrase, ssid->ssid, ssid->ssid_len, 4096,
3532 ssid->psk, PMK_LEN) != 0) {
3533 wpa_printf(MSG_ERROR, "Error in pbkdf2_sha1()");
3534 return;
3535 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003536 wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
3537 ssid->psk, PMK_LEN);
3538 ssid->psk_set = 1;
3539#endif /* CONFIG_NO_PBKDF2 */
3540}
3541
3542
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003543static int wpa_config_set_cred_req_conn_capab(struct wpa_cred *cred,
3544 const char *value)
3545{
3546 u8 *proto;
3547 int **port;
3548 int *ports, *nports;
3549 const char *pos;
3550 unsigned int num_ports;
3551
3552 proto = os_realloc_array(cred->req_conn_capab_proto,
3553 cred->num_req_conn_capab + 1, sizeof(u8));
3554 if (proto == NULL)
3555 return -1;
3556 cred->req_conn_capab_proto = proto;
3557
3558 port = os_realloc_array(cred->req_conn_capab_port,
3559 cred->num_req_conn_capab + 1, sizeof(int *));
3560 if (port == NULL)
3561 return -1;
3562 cred->req_conn_capab_port = port;
3563
3564 proto[cred->num_req_conn_capab] = atoi(value);
3565
3566 pos = os_strchr(value, ':');
3567 if (pos == NULL) {
3568 port[cred->num_req_conn_capab] = NULL;
3569 cred->num_req_conn_capab++;
3570 return 0;
3571 }
3572 pos++;
3573
3574 ports = NULL;
3575 num_ports = 0;
3576
3577 while (*pos) {
3578 nports = os_realloc_array(ports, num_ports + 1, sizeof(int));
3579 if (nports == NULL) {
3580 os_free(ports);
3581 return -1;
3582 }
3583 ports = nports;
3584 ports[num_ports++] = atoi(pos);
3585
3586 pos = os_strchr(pos, ',');
3587 if (pos == NULL)
3588 break;
3589 pos++;
3590 }
3591
3592 nports = os_realloc_array(ports, num_ports + 1, sizeof(int));
3593 if (nports == NULL) {
3594 os_free(ports);
3595 return -1;
3596 }
3597 ports = nports;
3598 ports[num_ports] = -1;
3599
3600 port[cred->num_req_conn_capab] = ports;
3601 cred->num_req_conn_capab++;
3602 return 0;
3603}
3604
3605
Sunil Ravi77d572f2023-01-17 23:58:31 +00003606static int
3607wpa_config_set_cred_ois(u8 cred_ois[MAX_ROAMING_CONS][MAX_ROAMING_CONS_OI_LEN],
3608 size_t cred_ois_len[MAX_ROAMING_CONS],
3609 unsigned int *cred_num_ois,
3610 const char *value)
Roshan Pius3a1667e2018-07-03 15:17:14 -07003611{
Sunil Ravi77d572f2023-01-17 23:58:31 +00003612 u8 ois[MAX_ROAMING_CONS][MAX_ROAMING_CONS_OI_LEN];
3613 size_t ois_len[MAX_ROAMING_CONS];
3614 unsigned int num_ois = 0;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003615 const char *pos, *end;
3616 size_t len;
3617
Sunil Ravi77d572f2023-01-17 23:58:31 +00003618 len = os_strlen(value);
3619 if (len / 2 < 3) {
3620 wpa_printf(MSG_ERROR,
3621 "Invalid organisation identifier (OI) list: %s",
3622 value);
3623 return -1;
3624 }
3625
3626 os_memset(ois, 0, sizeof(ois));
3627 os_memset(ois_len, 0, sizeof(ois_len));
Roshan Pius3a1667e2018-07-03 15:17:14 -07003628
3629 for (pos = value;;) {
3630 end = os_strchr(pos, ',');
3631 len = end ? (size_t) (end - pos) : os_strlen(pos);
3632 if (!end && len == 0)
3633 break;
Sunil Ravi77d572f2023-01-17 23:58:31 +00003634 if (len / 2 < 3 || (len & 1) != 0 ||
Roshan Pius3a1667e2018-07-03 15:17:14 -07003635 len / 2 > MAX_ROAMING_CONS_OI_LEN ||
3636 hexstr2bin(pos,
Sunil Ravi77d572f2023-01-17 23:58:31 +00003637 ois[num_ois],
Roshan Pius3a1667e2018-07-03 15:17:14 -07003638 len / 2) < 0) {
3639 wpa_printf(MSG_INFO,
Sunil Ravi77d572f2023-01-17 23:58:31 +00003640 "Invalid organisation identifier (OI) entry: %s",
Roshan Pius3a1667e2018-07-03 15:17:14 -07003641 pos);
3642 return -1;
3643 }
Sunil Ravi77d572f2023-01-17 23:58:31 +00003644 ois_len[num_ois] = len / 2;
3645 num_ois++;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08003646
3647 if (!end)
3648 break;
3649
Sunil Ravi77d572f2023-01-17 23:58:31 +00003650 if (num_ois >= MAX_ROAMING_CONS) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07003651 wpa_printf(MSG_INFO,
Sunil Ravi77d572f2023-01-17 23:58:31 +00003652 "Too many OIs");
Roshan Pius3a1667e2018-07-03 15:17:14 -07003653 return -1;
3654 }
3655
Roshan Pius3a1667e2018-07-03 15:17:14 -07003656 pos = end + 1;
3657 }
3658
Sunil Ravi77d572f2023-01-17 23:58:31 +00003659 os_memcpy(cred_ois, ois, sizeof(ois));
3660 os_memcpy(cred_ois_len, ois_len, sizeof(ois_len));
3661 *cred_num_ois = num_ois;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003662
3663 return 0;
3664}
3665
3666
Dmitry Shmidt04949592012-07-19 12:16:46 -07003667int wpa_config_set_cred(struct wpa_cred *cred, const char *var,
3668 const char *value, int line)
3669{
3670 char *val;
3671 size_t len;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003672 int res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003673
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003674 if (os_strcmp(var, "temporary") == 0) {
3675 cred->temporary = atoi(value);
3676 return 0;
3677 }
3678
Dmitry Shmidt04949592012-07-19 12:16:46 -07003679 if (os_strcmp(var, "priority") == 0) {
3680 cred->priority = atoi(value);
3681 return 0;
3682 }
3683
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003684 if (os_strcmp(var, "sp_priority") == 0) {
3685 int prio = atoi(value);
3686 if (prio < 0 || prio > 255)
3687 return -1;
3688 cred->sp_priority = prio;
3689 return 0;
3690 }
3691
Dmitry Shmidt04949592012-07-19 12:16:46 -07003692 if (os_strcmp(var, "pcsc") == 0) {
3693 cred->pcsc = atoi(value);
3694 return 0;
3695 }
3696
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003697 if (os_strcmp(var, "eap") == 0) {
3698 struct eap_method_type method;
3699 method.method = eap_peer_get_type(value, &method.vendor);
3700 if (method.vendor == EAP_VENDOR_IETF &&
3701 method.method == EAP_TYPE_NONE) {
3702 wpa_printf(MSG_ERROR, "Line %d: unknown EAP type '%s' "
3703 "for a credential", line, value);
3704 return -1;
3705 }
3706 os_free(cred->eap_method);
3707 cred->eap_method = os_malloc(sizeof(*cred->eap_method));
3708 if (cred->eap_method == NULL)
3709 return -1;
3710 os_memcpy(cred->eap_method, &method, sizeof(method));
3711 return 0;
3712 }
3713
3714 if (os_strcmp(var, "password") == 0 &&
3715 os_strncmp(value, "ext:", 4) == 0) {
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003716 if (has_newline(value))
3717 return -1;
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003718 str_clear_free(cred->password);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003719 cred->password = os_strdup(value);
3720 cred->ext_password = 1;
3721 return 0;
3722 }
3723
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003724 if (os_strcmp(var, "update_identifier") == 0) {
3725 cred->update_identifier = atoi(value);
3726 return 0;
3727 }
3728
3729 if (os_strcmp(var, "min_dl_bandwidth_home") == 0) {
3730 cred->min_dl_bandwidth_home = atoi(value);
3731 return 0;
3732 }
3733
3734 if (os_strcmp(var, "min_ul_bandwidth_home") == 0) {
3735 cred->min_ul_bandwidth_home = atoi(value);
3736 return 0;
3737 }
3738
3739 if (os_strcmp(var, "min_dl_bandwidth_roaming") == 0) {
3740 cred->min_dl_bandwidth_roaming = atoi(value);
3741 return 0;
3742 }
3743
3744 if (os_strcmp(var, "min_ul_bandwidth_roaming") == 0) {
3745 cred->min_ul_bandwidth_roaming = atoi(value);
3746 return 0;
3747 }
3748
3749 if (os_strcmp(var, "max_bss_load") == 0) {
3750 cred->max_bss_load = atoi(value);
3751 return 0;
3752 }
3753
3754 if (os_strcmp(var, "req_conn_capab") == 0)
3755 return wpa_config_set_cred_req_conn_capab(cred, value);
3756
3757 if (os_strcmp(var, "ocsp") == 0) {
3758 cred->ocsp = atoi(value);
3759 return 0;
3760 }
3761
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -07003762 if (os_strcmp(var, "sim_num") == 0) {
3763 cred->sim_num = atoi(value);
3764 return 0;
3765 }
3766
Hai Shaloma20dcd72022-02-04 13:43:00 -08003767 if (os_strcmp(var, "engine") == 0) {
3768 cred->engine = atoi(value);
3769 return 0;
3770 }
3771
Dmitry Shmidt04949592012-07-19 12:16:46 -07003772 val = wpa_config_parse_string(value, &len);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003773 if (val == NULL ||
3774 (os_strcmp(var, "excluded_ssid") != 0 &&
3775 os_strcmp(var, "roaming_consortium") != 0 &&
3776 os_strcmp(var, "required_roaming_consortium") != 0 &&
3777 has_newline(val))) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07003778 wpa_printf(MSG_ERROR, "Line %d: invalid field '%s' string "
3779 "value '%s'.", line, var, value);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003780 os_free(val);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003781 return -1;
3782 }
3783
3784 if (os_strcmp(var, "realm") == 0) {
3785 os_free(cred->realm);
3786 cred->realm = val;
3787 return 0;
3788 }
3789
3790 if (os_strcmp(var, "username") == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003791 str_clear_free(cred->username);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003792 cred->username = val;
3793 return 0;
3794 }
3795
3796 if (os_strcmp(var, "password") == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003797 str_clear_free(cred->password);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003798 cred->password = val;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003799 cred->ext_password = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003800 return 0;
3801 }
3802
3803 if (os_strcmp(var, "ca_cert") == 0) {
3804 os_free(cred->ca_cert);
3805 cred->ca_cert = val;
3806 return 0;
3807 }
3808
3809 if (os_strcmp(var, "client_cert") == 0) {
3810 os_free(cred->client_cert);
3811 cred->client_cert = val;
3812 return 0;
3813 }
3814
3815 if (os_strcmp(var, "private_key") == 0) {
3816 os_free(cred->private_key);
3817 cred->private_key = val;
3818 return 0;
3819 }
3820
3821 if (os_strcmp(var, "private_key_passwd") == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003822 str_clear_free(cred->private_key_passwd);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003823 cred->private_key_passwd = val;
3824 return 0;
3825 }
3826
Hai Shaloma20dcd72022-02-04 13:43:00 -08003827 if (os_strcmp(var, "engine_id") == 0) {
3828 os_free(cred->engine_id);
3829 cred->engine_id = val;
3830 return 0;
3831 }
3832
3833 if (os_strcmp(var, "ca_cert_id") == 0) {
3834 os_free(cred->ca_cert_id);
3835 cred->ca_cert_id = val;
3836 return 0;
3837 }
3838
3839 if (os_strcmp(var, "cert_id") == 0) {
3840 os_free(cred->cert_id);
3841 cred->cert_id = val;
3842 return 0;
3843 }
3844
3845 if (os_strcmp(var, "key_id") == 0) {
3846 os_free(cred->key_id);
3847 cred->key_id = val;
3848 return 0;
3849 }
3850
Dmitry Shmidt04949592012-07-19 12:16:46 -07003851 if (os_strcmp(var, "imsi") == 0) {
3852 os_free(cred->imsi);
3853 cred->imsi = val;
3854 return 0;
3855 }
3856
3857 if (os_strcmp(var, "milenage") == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003858 str_clear_free(cred->milenage);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003859 cred->milenage = val;
3860 return 0;
3861 }
3862
Dmitry Shmidt051af732013-10-22 13:52:46 -07003863 if (os_strcmp(var, "domain_suffix_match") == 0) {
3864 os_free(cred->domain_suffix_match);
3865 cred->domain_suffix_match = val;
3866 return 0;
3867 }
3868
Dmitry Shmidt04949592012-07-19 12:16:46 -07003869 if (os_strcmp(var, "domain") == 0) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07003870 char **new_domain;
3871 new_domain = os_realloc_array(cred->domain,
3872 cred->num_domain + 1,
3873 sizeof(char *));
3874 if (new_domain == NULL) {
3875 os_free(val);
3876 return -1;
3877 }
3878 new_domain[cred->num_domain++] = val;
3879 cred->domain = new_domain;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003880 return 0;
3881 }
3882
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003883 if (os_strcmp(var, "phase1") == 0) {
3884 os_free(cred->phase1);
3885 cred->phase1 = val;
3886 return 0;
3887 }
3888
3889 if (os_strcmp(var, "phase2") == 0) {
3890 os_free(cred->phase2);
3891 cred->phase2 = val;
3892 return 0;
3893 }
3894
3895 if (os_strcmp(var, "roaming_consortium") == 0) {
Sunil Ravi77d572f2023-01-17 23:58:31 +00003896 if (len < 3 || len > sizeof(cred->home_ois[0])) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003897 wpa_printf(MSG_ERROR, "Line %d: invalid "
3898 "roaming_consortium length %d (3..15 "
3899 "expected)", line, (int) len);
3900 os_free(val);
3901 return -1;
3902 }
Sunil Ravi77d572f2023-01-17 23:58:31 +00003903 wpa_printf(MSG_WARNING,
3904 "Line %d: option roaming_consortium is deprecated and will be removed in the future",
3905 line);
3906 os_memcpy(cred->home_ois[0], val, len);
3907 cred->home_ois_len[0] = len;
3908 cred->num_home_ois = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003909 os_free(val);
3910 return 0;
3911 }
3912
Dmitry Shmidt051af732013-10-22 13:52:46 -07003913 if (os_strcmp(var, "required_roaming_consortium") == 0) {
Sunil Ravi77d572f2023-01-17 23:58:31 +00003914 if (len < 3 || len > sizeof(cred->required_home_ois[0])) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07003915 wpa_printf(MSG_ERROR, "Line %d: invalid "
3916 "required_roaming_consortium length %d "
3917 "(3..15 expected)", line, (int) len);
3918 os_free(val);
3919 return -1;
3920 }
Sunil Ravi77d572f2023-01-17 23:58:31 +00003921 wpa_printf(MSG_WARNING,
3922 "Line %d: option required_roaming_consortium is deprecated and will be removed in the future",
3923 line);
3924 os_memcpy(cred->required_home_ois[0], val, len);
3925 cred->required_home_ois_len[0] = len;
3926 cred->num_required_home_ois = 1;
Dmitry Shmidt051af732013-10-22 13:52:46 -07003927 os_free(val);
3928 return 0;
3929 }
3930
Sunil Ravi77d572f2023-01-17 23:58:31 +00003931 if (os_strcmp(var, "home_ois") == 0) {
3932 res = wpa_config_set_cred_ois(cred->home_ois,
3933 cred->home_ois_len,
3934 &cred->num_home_ois,
3935 val);
3936 if (res < 0)
3937 wpa_printf(MSG_ERROR, "Line %d: invalid home_ois",
3938 line);
3939 os_free(val);
3940 return res;
3941 }
3942
3943 if (os_strcmp(var, "required_home_ois") == 0) {
3944 res = wpa_config_set_cred_ois(cred->required_home_ois,
3945 cred->required_home_ois_len,
3946 &cred->num_required_home_ois,
3947 val);
3948 if (res < 0)
3949 wpa_printf(MSG_ERROR,
3950 "Line %d: invalid required_home_ois", line);
3951 os_free(val);
3952 return res;
3953 }
3954
Roshan Pius3a1667e2018-07-03 15:17:14 -07003955 if (os_strcmp(var, "roaming_consortiums") == 0) {
Sunil Ravi77d572f2023-01-17 23:58:31 +00003956 res = wpa_config_set_cred_ois(cred->roaming_consortiums,
3957 cred->roaming_consortiums_len,
3958 &cred->num_roaming_consortiums,
3959 val);
Roshan Pius3a1667e2018-07-03 15:17:14 -07003960 if (res < 0)
3961 wpa_printf(MSG_ERROR,
3962 "Line %d: invalid roaming_consortiums",
3963 line);
3964 os_free(val);
3965 return res;
3966 }
3967
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003968 if (os_strcmp(var, "excluded_ssid") == 0) {
3969 struct excluded_ssid *e;
3970
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07003971 if (len > SSID_MAX_LEN) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003972 wpa_printf(MSG_ERROR, "Line %d: invalid "
3973 "excluded_ssid length %d", line, (int) len);
3974 os_free(val);
3975 return -1;
3976 }
3977
3978 e = os_realloc_array(cred->excluded_ssid,
3979 cred->num_excluded_ssid + 1,
3980 sizeof(struct excluded_ssid));
3981 if (e == NULL) {
3982 os_free(val);
3983 return -1;
3984 }
3985 cred->excluded_ssid = e;
3986
3987 e = &cred->excluded_ssid[cred->num_excluded_ssid++];
3988 os_memcpy(e->ssid, val, len);
3989 e->ssid_len = len;
3990
3991 os_free(val);
3992
3993 return 0;
3994 }
3995
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003996 if (os_strcmp(var, "roaming_partner") == 0) {
3997 struct roaming_partner *p;
3998 char *pos;
3999
4000 p = os_realloc_array(cred->roaming_partner,
4001 cred->num_roaming_partner + 1,
4002 sizeof(struct roaming_partner));
4003 if (p == NULL) {
4004 os_free(val);
4005 return -1;
4006 }
4007 cred->roaming_partner = p;
4008
4009 p = &cred->roaming_partner[cred->num_roaming_partner];
4010
4011 pos = os_strchr(val, ',');
4012 if (pos == NULL) {
4013 os_free(val);
4014 return -1;
4015 }
4016 *pos++ = '\0';
4017 if (pos - val - 1 >= (int) sizeof(p->fqdn)) {
4018 os_free(val);
4019 return -1;
4020 }
4021 os_memcpy(p->fqdn, val, pos - val);
4022
4023 p->exact_match = atoi(pos);
4024
4025 pos = os_strchr(pos, ',');
4026 if (pos == NULL) {
4027 os_free(val);
4028 return -1;
4029 }
4030 *pos++ = '\0';
4031
4032 p->priority = atoi(pos);
4033
4034 pos = os_strchr(pos, ',');
4035 if (pos == NULL) {
4036 os_free(val);
4037 return -1;
4038 }
4039 *pos++ = '\0';
4040
4041 if (os_strlen(pos) >= sizeof(p->country)) {
4042 os_free(val);
4043 return -1;
4044 }
4045 os_memcpy(p->country, pos, os_strlen(pos) + 1);
4046
4047 cred->num_roaming_partner++;
4048 os_free(val);
4049
4050 return 0;
4051 }
4052
4053 if (os_strcmp(var, "provisioning_sp") == 0) {
4054 os_free(cred->provisioning_sp);
4055 cred->provisioning_sp = val;
4056 return 0;
4057 }
4058
Sunil8cd6f4d2022-06-28 18:40:46 +00004059 if (os_strcmp(var, "imsi_privacy_cert") == 0) {
4060 os_free(cred->imsi_privacy_cert);
4061 cred->imsi_privacy_cert = val;
4062 return 0;
4063 }
4064
4065 if (os_strcmp(var, "imsi_privacy_attr") == 0) {
4066 os_free(cred->imsi_privacy_attr);
4067 cred->imsi_privacy_attr = val;
Sunil Ravia04bd252022-05-02 22:54:18 -07004068 return 0;
4069 }
4070
Steven Liu9138d432022-11-23 22:29:05 +00004071 if (os_strcmp(var, "strict_conservative_peer_mode") == 0) {
4072 cred->strict_conservative_peer_mode = atoi(val);
4073 return 0;
4074 }
4075
Dmitry Shmidt04949592012-07-19 12:16:46 -07004076 if (line) {
4077 wpa_printf(MSG_ERROR, "Line %d: unknown cred field '%s'.",
4078 line, var);
4079 }
4080
4081 os_free(val);
4082
4083 return -1;
4084}
4085
4086
Dmitry Shmidtc2817022014-07-02 10:32:10 -07004087static char * alloc_int_str(int val)
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004088{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004089 const unsigned int bufsize = 20;
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004090 char *buf;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004091 int res;
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004092
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004093 buf = os_malloc(bufsize);
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004094 if (buf == NULL)
4095 return NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004096 res = os_snprintf(buf, bufsize, "%d", val);
4097 if (os_snprintf_error(bufsize, res)) {
4098 os_free(buf);
4099 buf = NULL;
4100 }
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004101 return buf;
4102}
4103
4104
Dmitry Shmidtc2817022014-07-02 10:32:10 -07004105static char * alloc_strdup(const char *str)
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004106{
4107 if (str == NULL)
4108 return NULL;
4109 return os_strdup(str);
4110}
4111
4112
4113char * wpa_config_get_cred_no_key(struct wpa_cred *cred, const char *var)
4114{
4115 if (os_strcmp(var, "temporary") == 0)
4116 return alloc_int_str(cred->temporary);
4117
4118 if (os_strcmp(var, "priority") == 0)
4119 return alloc_int_str(cred->priority);
4120
4121 if (os_strcmp(var, "sp_priority") == 0)
4122 return alloc_int_str(cred->sp_priority);
4123
4124 if (os_strcmp(var, "pcsc") == 0)
4125 return alloc_int_str(cred->pcsc);
4126
4127 if (os_strcmp(var, "eap") == 0) {
4128 if (!cred->eap_method)
4129 return NULL;
4130 return alloc_strdup(eap_get_name(cred->eap_method[0].vendor,
4131 cred->eap_method[0].method));
4132 }
4133
4134 if (os_strcmp(var, "update_identifier") == 0)
4135 return alloc_int_str(cred->update_identifier);
4136
4137 if (os_strcmp(var, "min_dl_bandwidth_home") == 0)
4138 return alloc_int_str(cred->min_dl_bandwidth_home);
4139
4140 if (os_strcmp(var, "min_ul_bandwidth_home") == 0)
4141 return alloc_int_str(cred->min_ul_bandwidth_home);
4142
4143 if (os_strcmp(var, "min_dl_bandwidth_roaming") == 0)
4144 return alloc_int_str(cred->min_dl_bandwidth_roaming);
4145
4146 if (os_strcmp(var, "min_ul_bandwidth_roaming") == 0)
4147 return alloc_int_str(cred->min_ul_bandwidth_roaming);
4148
4149 if (os_strcmp(var, "max_bss_load") == 0)
4150 return alloc_int_str(cred->max_bss_load);
4151
4152 if (os_strcmp(var, "req_conn_capab") == 0) {
4153 unsigned int i;
4154 char *buf, *end, *pos;
4155 int ret;
4156
4157 if (!cred->num_req_conn_capab)
4158 return NULL;
4159
4160 buf = os_malloc(4000);
4161 if (buf == NULL)
4162 return NULL;
4163 pos = buf;
4164 end = pos + 4000;
4165 for (i = 0; i < cred->num_req_conn_capab; i++) {
4166 int *ports;
4167
4168 ret = os_snprintf(pos, end - pos, "%s%u",
4169 i > 0 ? "\n" : "",
4170 cred->req_conn_capab_proto[i]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004171 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004172 return buf;
4173 pos += ret;
4174
4175 ports = cred->req_conn_capab_port[i];
4176 if (ports) {
4177 int j;
4178 for (j = 0; ports[j] != -1; j++) {
4179 ret = os_snprintf(pos, end - pos,
4180 "%s%d",
4181 j > 0 ? "," : ":",
4182 ports[j]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004183 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004184 return buf;
4185 pos += ret;
4186 }
4187 }
4188 }
4189
4190 return buf;
4191 }
4192
4193 if (os_strcmp(var, "ocsp") == 0)
4194 return alloc_int_str(cred->ocsp);
4195
4196 if (os_strcmp(var, "realm") == 0)
4197 return alloc_strdup(cred->realm);
4198
4199 if (os_strcmp(var, "username") == 0)
4200 return alloc_strdup(cred->username);
4201
4202 if (os_strcmp(var, "password") == 0) {
4203 if (!cred->password)
4204 return NULL;
4205 return alloc_strdup("*");
4206 }
4207
4208 if (os_strcmp(var, "ca_cert") == 0)
4209 return alloc_strdup(cred->ca_cert);
4210
4211 if (os_strcmp(var, "client_cert") == 0)
4212 return alloc_strdup(cred->client_cert);
4213
4214 if (os_strcmp(var, "private_key") == 0)
4215 return alloc_strdup(cred->private_key);
4216
4217 if (os_strcmp(var, "private_key_passwd") == 0) {
4218 if (!cred->private_key_passwd)
4219 return NULL;
4220 return alloc_strdup("*");
4221 }
4222
4223 if (os_strcmp(var, "imsi") == 0)
4224 return alloc_strdup(cred->imsi);
4225
Sunil8cd6f4d2022-06-28 18:40:46 +00004226 if (os_strcmp(var, "imsi_privacy_cert") == 0)
4227 return alloc_strdup(cred->imsi_privacy_cert);
4228
4229 if (os_strcmp(var, "imsi_privacy_attr") == 0)
4230 return alloc_strdup(cred->imsi_privacy_attr);
Sunil Ravia04bd252022-05-02 22:54:18 -07004231
Steven Liu9138d432022-11-23 22:29:05 +00004232 if (os_strcmp(var, "strict_conservative_peer_mode") == 0)
4233 return alloc_int_str(cred->strict_conservative_peer_mode);
4234
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004235 if (os_strcmp(var, "milenage") == 0) {
4236 if (!(cred->milenage))
4237 return NULL;
4238 return alloc_strdup("*");
4239 }
4240
4241 if (os_strcmp(var, "domain_suffix_match") == 0)
4242 return alloc_strdup(cred->domain_suffix_match);
4243
4244 if (os_strcmp(var, "domain") == 0) {
4245 unsigned int i;
4246 char *buf, *end, *pos;
4247 int ret;
4248
4249 if (!cred->num_domain)
4250 return NULL;
4251
4252 buf = os_malloc(4000);
4253 if (buf == NULL)
4254 return NULL;
4255 pos = buf;
4256 end = pos + 4000;
4257
4258 for (i = 0; i < cred->num_domain; i++) {
4259 ret = os_snprintf(pos, end - pos, "%s%s",
4260 i > 0 ? "\n" : "", cred->domain[i]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004261 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004262 return buf;
4263 pos += ret;
4264 }
4265
4266 return buf;
4267 }
4268
4269 if (os_strcmp(var, "phase1") == 0)
4270 return alloc_strdup(cred->phase1);
4271
4272 if (os_strcmp(var, "phase2") == 0)
4273 return alloc_strdup(cred->phase2);
4274
4275 if (os_strcmp(var, "roaming_consortium") == 0) {
4276 size_t buflen;
4277 char *buf;
4278
Sunil Ravi77d572f2023-01-17 23:58:31 +00004279 if (!cred->num_home_ois || !cred->home_ois_len[0])
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004280 return NULL;
Sunil Ravi77d572f2023-01-17 23:58:31 +00004281 buflen = cred->home_ois_len[0] * 2 + 1;
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004282 buf = os_malloc(buflen);
4283 if (buf == NULL)
4284 return NULL;
Sunil Ravi77d572f2023-01-17 23:58:31 +00004285 wpa_snprintf_hex(buf, buflen, cred->home_ois[0],
4286 cred->home_ois_len[0]);
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004287 return buf;
4288 }
4289
4290 if (os_strcmp(var, "required_roaming_consortium") == 0) {
4291 size_t buflen;
4292 char *buf;
4293
Sunil Ravi77d572f2023-01-17 23:58:31 +00004294 if (!cred->num_required_home_ois ||
4295 !cred->required_home_ois_len[0])
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004296 return NULL;
Sunil Ravi77d572f2023-01-17 23:58:31 +00004297 buflen = cred->required_home_ois_len[0] * 2 + 1;
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004298 buf = os_malloc(buflen);
4299 if (buf == NULL)
4300 return NULL;
Sunil Ravi77d572f2023-01-17 23:58:31 +00004301 wpa_snprintf_hex(buf, buflen, cred->required_home_ois[0],
4302 cred->required_home_ois_len[0]);
4303 return buf;
4304 }
4305
4306 if (os_strcmp(var, "home_ois") == 0) {
4307 size_t buflen;
4308 char *buf, *pos;
4309 size_t i;
4310
4311 if (!cred->num_home_ois)
4312 return NULL;
4313 buflen = cred->num_home_ois * MAX_ROAMING_CONS_OI_LEN * 2 + 1;
4314 buf = os_malloc(buflen);
4315 if (!buf)
4316 return NULL;
4317 pos = buf;
4318 for (i = 0; i < cred->num_home_ois; i++) {
4319 if (i > 0)
4320 *pos++ = ',';
4321 pos += wpa_snprintf_hex(
4322 pos, buf + buflen - pos,
4323 cred->home_ois[i],
4324 cred->home_ois_len[i]);
4325 }
4326 *pos = '\0';
4327 return buf;
4328 }
4329
4330 if (os_strcmp(var, "required_home_ois") == 0) {
4331 size_t buflen;
4332 char *buf, *pos;
4333 size_t i;
4334
4335 if (!cred->num_required_home_ois)
4336 return NULL;
4337 buflen = cred->num_required_home_ois *
4338 MAX_ROAMING_CONS_OI_LEN * 2 + 1;
4339 buf = os_malloc(buflen);
4340 if (!buf)
4341 return NULL;
4342 pos = buf;
4343 for (i = 0; i < cred->num_required_home_ois; i++) {
4344 if (i > 0)
4345 *pos++ = ',';
4346 pos += wpa_snprintf_hex(
4347 pos, buf + buflen - pos,
4348 cred->required_home_ois[i],
4349 cred->required_home_ois_len[i]);
4350 }
4351 *pos = '\0';
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004352 return buf;
4353 }
4354
Roshan Pius3a1667e2018-07-03 15:17:14 -07004355 if (os_strcmp(var, "roaming_consortiums") == 0) {
4356 size_t buflen;
4357 char *buf, *pos;
4358 size_t i;
4359
4360 if (!cred->num_roaming_consortiums)
4361 return NULL;
4362 buflen = cred->num_roaming_consortiums *
4363 MAX_ROAMING_CONS_OI_LEN * 2 + 1;
4364 buf = os_malloc(buflen);
4365 if (!buf)
4366 return NULL;
4367 pos = buf;
4368 for (i = 0; i < cred->num_roaming_consortiums; i++) {
4369 if (i > 0)
4370 *pos++ = ',';
4371 pos += wpa_snprintf_hex(
4372 pos, buf + buflen - pos,
4373 cred->roaming_consortiums[i],
4374 cred->roaming_consortiums_len[i]);
4375 }
4376 *pos = '\0';
4377 return buf;
4378 }
4379
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004380 if (os_strcmp(var, "excluded_ssid") == 0) {
4381 unsigned int i;
4382 char *buf, *end, *pos;
4383
4384 if (!cred->num_excluded_ssid)
4385 return NULL;
4386
4387 buf = os_malloc(4000);
4388 if (buf == NULL)
4389 return NULL;
4390 pos = buf;
4391 end = pos + 4000;
4392
4393 for (i = 0; i < cred->num_excluded_ssid; i++) {
4394 struct excluded_ssid *e;
4395 int ret;
4396
4397 e = &cred->excluded_ssid[i];
4398 ret = os_snprintf(pos, end - pos, "%s%s",
4399 i > 0 ? "\n" : "",
4400 wpa_ssid_txt(e->ssid, e->ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004401 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004402 return buf;
4403 pos += ret;
4404 }
4405
4406 return buf;
4407 }
4408
4409 if (os_strcmp(var, "roaming_partner") == 0) {
4410 unsigned int i;
4411 char *buf, *end, *pos;
4412
4413 if (!cred->num_roaming_partner)
4414 return NULL;
4415
4416 buf = os_malloc(4000);
4417 if (buf == NULL)
4418 return NULL;
4419 pos = buf;
4420 end = pos + 4000;
4421
4422 for (i = 0; i < cred->num_roaming_partner; i++) {
4423 struct roaming_partner *p;
4424 int ret;
4425
4426 p = &cred->roaming_partner[i];
4427 ret = os_snprintf(pos, end - pos, "%s%s,%d,%u,%s",
4428 i > 0 ? "\n" : "",
4429 p->fqdn, p->exact_match, p->priority,
4430 p->country);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004431 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004432 return buf;
4433 pos += ret;
4434 }
4435
4436 return buf;
4437 }
4438
4439 if (os_strcmp(var, "provisioning_sp") == 0)
4440 return alloc_strdup(cred->provisioning_sp);
4441
4442 return NULL;
4443}
4444
4445
Dmitry Shmidt04949592012-07-19 12:16:46 -07004446struct wpa_cred * wpa_config_get_cred(struct wpa_config *config, int id)
4447{
4448 struct wpa_cred *cred;
4449
4450 cred = config->cred;
4451 while (cred) {
4452 if (id == cred->id)
4453 break;
4454 cred = cred->next;
4455 }
4456
4457 return cred;
4458}
4459
4460
4461struct wpa_cred * wpa_config_add_cred(struct wpa_config *config)
4462{
4463 int id;
4464 struct wpa_cred *cred, *last = NULL;
4465
4466 id = -1;
4467 cred = config->cred;
4468 while (cred) {
4469 if (cred->id > id)
4470 id = cred->id;
4471 last = cred;
4472 cred = cred->next;
4473 }
4474 id++;
4475
4476 cred = os_zalloc(sizeof(*cred));
4477 if (cred == NULL)
4478 return NULL;
4479 cred->id = id;
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -07004480 cred->sim_num = DEFAULT_USER_SELECTED_SIM;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004481 if (last)
4482 last->next = cred;
4483 else
4484 config->cred = cred;
4485
4486 return cred;
4487}
4488
4489
4490int wpa_config_remove_cred(struct wpa_config *config, int id)
4491{
4492 struct wpa_cred *cred, *prev = NULL;
4493
4494 cred = config->cred;
4495 while (cred) {
4496 if (id == cred->id)
4497 break;
4498 prev = cred;
4499 cred = cred->next;
4500 }
4501
4502 if (cred == NULL)
4503 return -1;
4504
4505 if (prev)
4506 prev->next = cred->next;
4507 else
4508 config->cred = cred->next;
4509
4510 wpa_config_free_cred(cred);
4511 return 0;
4512}
4513
4514
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004515#ifndef CONFIG_NO_CONFIG_BLOBS
4516/**
4517 * wpa_config_get_blob - Get a named configuration blob
4518 * @config: Configuration data from wpa_config_read()
4519 * @name: Name of the blob
4520 * Returns: Pointer to blob data or %NULL if not found
4521 */
4522const struct wpa_config_blob * wpa_config_get_blob(struct wpa_config *config,
4523 const char *name)
4524{
4525 struct wpa_config_blob *blob = config->blobs;
4526
4527 while (blob) {
4528 if (os_strcmp(blob->name, name) == 0)
4529 return blob;
4530 blob = blob->next;
4531 }
4532 return NULL;
4533}
4534
4535
4536/**
4537 * wpa_config_set_blob - Set or add a named configuration blob
4538 * @config: Configuration data from wpa_config_read()
4539 * @blob: New value for the blob
4540 *
4541 * Adds a new configuration blob or replaces the current value of an existing
4542 * blob.
4543 */
4544void wpa_config_set_blob(struct wpa_config *config,
4545 struct wpa_config_blob *blob)
4546{
4547 wpa_config_remove_blob(config, blob->name);
4548 blob->next = config->blobs;
4549 config->blobs = blob;
4550}
4551
4552
4553/**
4554 * wpa_config_free_blob - Free blob data
4555 * @blob: Pointer to blob to be freed
4556 */
4557void wpa_config_free_blob(struct wpa_config_blob *blob)
4558{
4559 if (blob) {
4560 os_free(blob->name);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07004561 bin_clear_free(blob->data, blob->len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004562 os_free(blob);
4563 }
4564}
4565
4566
4567/**
4568 * wpa_config_remove_blob - Remove a named configuration blob
4569 * @config: Configuration data from wpa_config_read()
4570 * @name: Name of the blob to remove
4571 * Returns: 0 if blob was removed or -1 if blob was not found
4572 */
4573int wpa_config_remove_blob(struct wpa_config *config, const char *name)
4574{
4575 struct wpa_config_blob *pos = config->blobs, *prev = NULL;
4576
4577 while (pos) {
4578 if (os_strcmp(pos->name, name) == 0) {
4579 if (prev)
4580 prev->next = pos->next;
4581 else
4582 config->blobs = pos->next;
4583 wpa_config_free_blob(pos);
4584 return 0;
4585 }
4586 prev = pos;
4587 pos = pos->next;
4588 }
4589
4590 return -1;
4591}
4592#endif /* CONFIG_NO_CONFIG_BLOBS */
4593
4594
4595/**
4596 * wpa_config_alloc_empty - Allocate an empty configuration
4597 * @ctrl_interface: Control interface parameters, e.g., path to UNIX domain
4598 * socket
4599 * @driver_param: Driver parameters
4600 * Returns: Pointer to allocated configuration data or %NULL on failure
4601 */
4602struct wpa_config * wpa_config_alloc_empty(const char *ctrl_interface,
4603 const char *driver_param)
4604{
Hai Shalom899fcc72020-10-19 14:38:18 -07004605#define ecw2cw(ecw) ((1 << (ecw)) - 1)
4606
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004607 struct wpa_config *config;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004608 const int aCWmin = 4, aCWmax = 10;
4609 const struct hostapd_wmm_ac_params ac_bk =
4610 { aCWmin, aCWmax, 7, 0, 0 }; /* background traffic */
4611 const struct hostapd_wmm_ac_params ac_be =
4612 { aCWmin, aCWmax, 3, 0, 0 }; /* best effort traffic */
4613 const struct hostapd_wmm_ac_params ac_vi = /* video traffic */
Hai Shalom899fcc72020-10-19 14:38:18 -07004614 { aCWmin - 1, aCWmin, 2, 3008 / 32, 0 };
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004615 const struct hostapd_wmm_ac_params ac_vo = /* voice traffic */
Hai Shalom899fcc72020-10-19 14:38:18 -07004616 { aCWmin - 2, aCWmin - 1, 2, 1504 / 32, 0 };
4617 const struct hostapd_tx_queue_params txq_bk =
4618 { 7, ecw2cw(aCWmin), ecw2cw(aCWmax), 0 };
4619 const struct hostapd_tx_queue_params txq_be =
4620 { 3, ecw2cw(aCWmin), 4 * (ecw2cw(aCWmin) + 1) - 1, 0 };
4621 const struct hostapd_tx_queue_params txq_vi =
4622 { 1, (ecw2cw(aCWmin) + 1) / 2 - 1, ecw2cw(aCWmin), 30 };
4623 const struct hostapd_tx_queue_params txq_vo =
4624 { 1, (ecw2cw(aCWmin) + 1) / 4 - 1,
4625 (ecw2cw(aCWmin) + 1) / 2 - 1, 15 };
4626
4627#undef ecw2cw
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004628
4629 config = os_zalloc(sizeof(*config));
4630 if (config == NULL)
4631 return NULL;
4632 config->eapol_version = DEFAULT_EAPOL_VERSION;
4633 config->ap_scan = DEFAULT_AP_SCAN;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004634 config->user_mpm = DEFAULT_USER_MPM;
4635 config->max_peer_links = DEFAULT_MAX_PEER_LINKS;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08004636 config->mesh_max_inactivity = DEFAULT_MESH_MAX_INACTIVITY;
Hai Shaloma20dcd72022-02-04 13:43:00 -08004637 config->mesh_fwding = DEFAULT_MESH_FWDING;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004638 config->dot11RSNASAERetransPeriod =
4639 DEFAULT_DOT11_RSNA_SAE_RETRANS_PERIOD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004640 config->fast_reauth = DEFAULT_FAST_REAUTH;
4641 config->p2p_go_intent = DEFAULT_P2P_GO_INTENT;
4642 config->p2p_intra_bss = DEFAULT_P2P_INTRA_BSS;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004643 config->p2p_go_freq_change_policy = DEFAULT_P2P_GO_FREQ_MOVE;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004644 config->p2p_go_max_inactivity = DEFAULT_P2P_GO_MAX_INACTIVITY;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004645 config->p2p_optimize_listen_chan = DEFAULT_P2P_OPTIMIZE_LISTEN_CHAN;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004646 config->p2p_go_ctwindow = DEFAULT_P2P_GO_CTWINDOW;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004647 config->bss_max_count = DEFAULT_BSS_MAX_COUNT;
4648 config->bss_expiration_age = DEFAULT_BSS_EXPIRATION_AGE;
4649 config->bss_expiration_scan_count = DEFAULT_BSS_EXPIRATION_SCAN_COUNT;
4650 config->max_num_sta = DEFAULT_MAX_NUM_STA;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004651 config->ap_isolate = DEFAULT_AP_ISOLATE;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004652 config->access_network_type = DEFAULT_ACCESS_NETWORK_TYPE;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07004653 config->scan_cur_freq = DEFAULT_SCAN_CUR_FREQ;
Hai Shalom60840252021-02-19 19:02:11 -08004654 config->scan_res_valid_for_connect = DEFAULT_SCAN_RES_VALID_FOR_CONNECT;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004655 config->wmm_ac_params[0] = ac_be;
4656 config->wmm_ac_params[1] = ac_bk;
4657 config->wmm_ac_params[2] = ac_vi;
4658 config->wmm_ac_params[3] = ac_vo;
Hai Shalom899fcc72020-10-19 14:38:18 -07004659 config->tx_queue[0] = txq_vo;
4660 config->tx_queue[1] = txq_vi;
4661 config->tx_queue[2] = txq_be;
4662 config->tx_queue[3] = txq_bk;
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07004663 config->p2p_search_delay = DEFAULT_P2P_SEARCH_DELAY;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004664 config->rand_addr_lifetime = DEFAULT_RAND_ADDR_LIFETIME;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004665 config->key_mgmt_offload = DEFAULT_KEY_MGMT_OFFLOAD;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08004666 config->cert_in_cb = DEFAULT_CERT_IN_CB;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004667 config->wpa_rsc_relaxation = DEFAULT_WPA_RSC_RELAXATION;
Hai Shalomfdcde762020-04-02 11:19:20 -07004668 config->extended_key_id = DEFAULT_EXTENDED_KEY_ID;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004669
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004670#ifdef CONFIG_MBO
4671 config->mbo_cell_capa = DEFAULT_MBO_CELL_CAPA;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004672 config->disassoc_imminent_rssi_threshold =
4673 DEFAULT_DISASSOC_IMMINENT_RSSI_THRESHOLD;
4674 config->oce = DEFAULT_OCE_SUPPORT;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004675#endif /* CONFIG_MBO */
Sunil Ravi84bb3e12021-06-10 09:52:05 -07004676 config->btm_offload = DEFAULT_BTM_OFFLOAD;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004677
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004678 if (ctrl_interface)
4679 config->ctrl_interface = os_strdup(ctrl_interface);
4680 if (driver_param)
4681 config->driver_param = os_strdup(driver_param);
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004682 config->gas_rand_addr_lifetime = DEFAULT_RAND_ADDR_LIFETIME;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004683
4684 return config;
4685}
4686
4687
4688#ifndef CONFIG_NO_STDOUT_DEBUG
4689/**
4690 * wpa_config_debug_dump_networks - Debug dump of configured networks
4691 * @config: Configuration data from wpa_config_read()
4692 */
4693void wpa_config_debug_dump_networks(struct wpa_config *config)
4694{
Hai Shalomfdcde762020-04-02 11:19:20 -07004695 size_t prio;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004696 struct wpa_ssid *ssid;
4697
4698 for (prio = 0; prio < config->num_prio; prio++) {
4699 ssid = config->pssid[prio];
4700 wpa_printf(MSG_DEBUG, "Priority group %d",
4701 ssid->priority);
4702 while (ssid) {
4703 wpa_printf(MSG_DEBUG, " id=%d ssid='%s'",
4704 ssid->id,
4705 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
4706 ssid = ssid->pnext;
4707 }
4708 }
4709}
4710#endif /* CONFIG_NO_STDOUT_DEBUG */
4711
4712
Hai Shalom899fcc72020-10-19 14:38:18 -07004713/**
4714 * Structure for global configuration parsing. This data is used to implement a
4715 * generic parser for the global interface configuration. The table of variables
4716 * is defined below in this file (global_fields[]).
4717 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004718struct global_parse_data {
Hai Shalom899fcc72020-10-19 14:38:18 -07004719 /* Configuration variable name */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004720 char *name;
Hai Shalom899fcc72020-10-19 14:38:18 -07004721
4722 /* Parser function for this variable. The parser functions return 0 or 1
4723 * to indicate success. Value 0 indicates that the parameter value may
4724 * have changed while value 1 means that the value did not change.
4725 * Error cases (failure to parse the string) are indicated by returning
4726 * -1. */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004727 int (*parser)(const struct global_parse_data *data,
4728 struct wpa_config *config, int line, const char *value);
Hai Shalom899fcc72020-10-19 14:38:18 -07004729
4730 /* Getter function to print the variable in text format to buf. */
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004731 int (*get)(const char *name, struct wpa_config *config, long offset,
4732 char *buf, size_t buflen, int pretty_print);
Hai Shalom899fcc72020-10-19 14:38:18 -07004733
4734 /* Variable specific parameters for the parser. */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004735 void *param1, *param2, *param3;
Hai Shalom899fcc72020-10-19 14:38:18 -07004736
4737 /* Indicates which configuration variable has changed. */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004738 unsigned int changed_flag;
4739};
4740
4741
4742static int wpa_global_config_parse_int(const struct global_parse_data *data,
4743 struct wpa_config *config, int line,
4744 const char *pos)
4745{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07004746 int val, *dst;
4747 char *end;
Hai Shalom899fcc72020-10-19 14:38:18 -07004748 bool same;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07004749
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004750 dst = (int *) (((u8 *) config) + (long) data->param1);
Dmitry Shmidt2f023192013-03-12 12:44:17 -07004751 val = strtol(pos, &end, 0);
4752 if (*end) {
4753 wpa_printf(MSG_ERROR, "Line %d: invalid number \"%s\"",
4754 line, pos);
4755 return -1;
4756 }
Hai Shalom899fcc72020-10-19 14:38:18 -07004757 same = *dst == val;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07004758 *dst = val;
4759
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004760 wpa_printf(MSG_DEBUG, "%s=%d", data->name, *dst);
4761
4762 if (data->param2 && *dst < (long) data->param2) {
4763 wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
4764 "min_value=%ld)", line, data->name, *dst,
4765 (long) data->param2);
4766 *dst = (long) data->param2;
4767 return -1;
4768 }
4769
4770 if (data->param3 && *dst > (long) data->param3) {
4771 wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
4772 "max_value=%ld)", line, data->name, *dst,
4773 (long) data->param3);
4774 *dst = (long) data->param3;
4775 return -1;
4776 }
4777
Hai Shalom899fcc72020-10-19 14:38:18 -07004778 return same;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004779}
4780
4781
4782static int wpa_global_config_parse_str(const struct global_parse_data *data,
4783 struct wpa_config *config, int line,
4784 const char *pos)
4785{
Hai Shalom899fcc72020-10-19 14:38:18 -07004786 size_t len, prev_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004787 char **dst, *tmp;
4788
4789 len = os_strlen(pos);
4790 if (data->param2 && len < (size_t) data->param2) {
4791 wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
4792 "min_len=%ld)", line, data->name,
4793 (unsigned long) len, (long) data->param2);
4794 return -1;
4795 }
4796
4797 if (data->param3 && len > (size_t) data->param3) {
4798 wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
4799 "max_len=%ld)", line, data->name,
4800 (unsigned long) len, (long) data->param3);
4801 return -1;
4802 }
4803
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004804 if (has_newline(pos)) {
4805 wpa_printf(MSG_ERROR, "Line %d: invalid %s value with newline",
4806 line, data->name);
4807 return -1;
4808 }
4809
Hai Shalom899fcc72020-10-19 14:38:18 -07004810 dst = (char **) (((u8 *) config) + (long) data->param1);
4811 if (*dst)
4812 prev_len = os_strlen(*dst);
4813 else
4814 prev_len = 0;
4815
4816 /* No change to the previously configured value */
Hai Shalom60840252021-02-19 19:02:11 -08004817 if (*dst && prev_len == len && os_memcmp(*dst, pos, len) == 0)
Hai Shalom899fcc72020-10-19 14:38:18 -07004818 return 1;
4819
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004820 tmp = os_strdup(pos);
4821 if (tmp == NULL)
4822 return -1;
4823
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004824 os_free(*dst);
4825 *dst = tmp;
4826 wpa_printf(MSG_DEBUG, "%s='%s'", data->name, *dst);
4827
4828 return 0;
4829}
4830
4831
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08004832static int wpa_config_process_bgscan(const struct global_parse_data *data,
4833 struct wpa_config *config, int line,
4834 const char *pos)
4835{
4836 size_t len;
4837 char *tmp;
Dmitry Shmidt97672262014-02-03 13:02:54 -08004838 int res;
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08004839
4840 tmp = wpa_config_parse_string(pos, &len);
4841 if (tmp == NULL) {
4842 wpa_printf(MSG_ERROR, "Line %d: failed to parse %s",
4843 line, data->name);
4844 return -1;
4845 }
4846
Dmitry Shmidt97672262014-02-03 13:02:54 -08004847 res = wpa_global_config_parse_str(data, config, line, tmp);
4848 os_free(tmp);
4849 return res;
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08004850}
4851
4852
Dmitry Shmidt04949592012-07-19 12:16:46 -07004853static int wpa_global_config_parse_bin(const struct global_parse_data *data,
4854 struct wpa_config *config, int line,
4855 const char *pos)
4856{
Dmitry Shmidt04949592012-07-19 12:16:46 -07004857 struct wpabuf **dst, *tmp;
4858
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004859 tmp = wpabuf_parse_bin(pos);
4860 if (!tmp)
Dmitry Shmidt04949592012-07-19 12:16:46 -07004861 return -1;
4862
Dmitry Shmidt04949592012-07-19 12:16:46 -07004863 dst = (struct wpabuf **) (((u8 *) config) + (long) data->param1);
Hai Shalom899fcc72020-10-19 14:38:18 -07004864 if (wpabuf_cmp(*dst, tmp) == 0) {
4865 wpabuf_free(tmp);
4866 return 1;
4867 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004868 wpabuf_free(*dst);
4869 *dst = tmp;
4870 wpa_printf(MSG_DEBUG, "%s", data->name);
4871
4872 return 0;
4873}
4874
4875
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07004876static int wpa_config_process_freq_list(const struct global_parse_data *data,
4877 struct wpa_config *config, int line,
4878 const char *value)
4879{
4880 int *freqs;
4881
4882 freqs = wpa_config_parse_int_array(value);
4883 if (freqs == NULL)
4884 return -1;
Dmitry Shmidt56052862013-10-04 10:23:25 -07004885 if (freqs[0] == 0) {
4886 os_free(freqs);
4887 freqs = NULL;
4888 }
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07004889 os_free(config->freq_list);
4890 config->freq_list = freqs;
4891 return 0;
4892}
4893
4894
Hai Shalom60840252021-02-19 19:02:11 -08004895static int
4896wpa_config_process_initial_freq_list(const struct global_parse_data *data,
4897 struct wpa_config *config, int line,
4898 const char *value)
4899{
4900 int *freqs;
4901
4902 freqs = wpa_config_parse_int_array(value);
4903 if (!freqs)
4904 return -1;
4905 if (freqs[0] == 0) {
4906 os_free(freqs);
4907 freqs = NULL;
4908 }
4909 os_free(config->initial_freq_list);
4910 config->initial_freq_list = freqs;
4911 return 0;
4912}
4913
4914
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004915#ifdef CONFIG_P2P
4916static int wpa_global_config_parse_ipv4(const struct global_parse_data *data,
4917 struct wpa_config *config, int line,
4918 const char *pos)
4919{
4920 u32 *dst;
4921 struct hostapd_ip_addr addr;
4922
4923 if (hostapd_parse_ip_addr(pos, &addr) < 0)
4924 return -1;
4925 if (addr.af != AF_INET)
4926 return -1;
4927
4928 dst = (u32 *) (((u8 *) config) + (long) data->param1);
Hai Shalom899fcc72020-10-19 14:38:18 -07004929 if (os_memcmp(dst, &addr.u.v4.s_addr, 4) == 0)
4930 return 1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004931 os_memcpy(dst, &addr.u.v4.s_addr, 4);
4932 wpa_printf(MSG_DEBUG, "%s = 0x%x", data->name,
4933 WPA_GET_BE32((u8 *) dst));
4934
4935 return 0;
4936}
4937#endif /* CONFIG_P2P */
4938
4939
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004940static int wpa_config_process_country(const struct global_parse_data *data,
4941 struct wpa_config *config, int line,
4942 const char *pos)
4943{
4944 if (!pos[0] || !pos[1]) {
4945 wpa_printf(MSG_DEBUG, "Invalid country set");
4946 return -1;
4947 }
Hai Shalom899fcc72020-10-19 14:38:18 -07004948 if (pos[0] == config->country[0] && pos[1] == config->country[1])
4949 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004950 config->country[0] = pos[0];
4951 config->country[1] = pos[1];
4952 wpa_printf(MSG_DEBUG, "country='%c%c'",
4953 config->country[0], config->country[1]);
4954 return 0;
4955}
4956
4957
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004958#ifndef CONFIG_NO_LOAD_DYNAMIC_EAP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004959static int wpa_config_process_load_dynamic_eap(
4960 const struct global_parse_data *data, struct wpa_config *config,
4961 int line, const char *so)
4962{
4963 int ret;
4964 wpa_printf(MSG_DEBUG, "load_dynamic_eap=%s", so);
4965 ret = eap_peer_method_load(so);
4966 if (ret == -2) {
4967 wpa_printf(MSG_DEBUG, "This EAP type was already loaded - not "
4968 "reloading.");
4969 } else if (ret) {
4970 wpa_printf(MSG_ERROR, "Line %d: Failed to load dynamic EAP "
4971 "method '%s'.", line, so);
4972 return -1;
4973 }
4974
4975 return 0;
4976}
Sunil Ravi2a14cf12023-11-21 00:54:38 +00004977#endif /* CONFIG_NO_LOAD_DYNAMIC_EAP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004978
4979
4980#ifdef CONFIG_WPS
4981
4982static int wpa_config_process_uuid(const struct global_parse_data *data,
4983 struct wpa_config *config, int line,
4984 const char *pos)
4985{
4986 char buf[40];
4987 if (uuid_str2bin(pos, config->uuid)) {
4988 wpa_printf(MSG_ERROR, "Line %d: invalid UUID", line);
4989 return -1;
4990 }
4991 uuid_bin2str(config->uuid, buf, sizeof(buf));
4992 wpa_printf(MSG_DEBUG, "uuid=%s", buf);
4993 return 0;
4994}
4995
4996
4997static int wpa_config_process_device_type(
4998 const struct global_parse_data *data,
4999 struct wpa_config *config, int line, const char *pos)
5000{
5001 return wps_dev_type_str2bin(pos, config->device_type);
5002}
5003
5004
5005static int wpa_config_process_os_version(const struct global_parse_data *data,
5006 struct wpa_config *config, int line,
5007 const char *pos)
5008{
5009 if (hexstr2bin(pos, config->os_version, 4)) {
5010 wpa_printf(MSG_ERROR, "Line %d: invalid os_version", line);
5011 return -1;
5012 }
5013 wpa_printf(MSG_DEBUG, "os_version=%08x",
5014 WPA_GET_BE32(config->os_version));
5015 return 0;
5016}
5017
Dmitry Shmidt04949592012-07-19 12:16:46 -07005018
5019static int wpa_config_process_wps_vendor_ext_m1(
5020 const struct global_parse_data *data,
5021 struct wpa_config *config, int line, const char *pos)
5022{
5023 struct wpabuf *tmp;
5024 int len = os_strlen(pos) / 2;
5025 u8 *p;
5026
5027 if (!len) {
5028 wpa_printf(MSG_ERROR, "Line %d: "
5029 "invalid wps_vendor_ext_m1", line);
5030 return -1;
5031 }
5032
5033 tmp = wpabuf_alloc(len);
5034 if (tmp) {
5035 p = wpabuf_put(tmp, len);
5036
5037 if (hexstr2bin(pos, p, len)) {
5038 wpa_printf(MSG_ERROR, "Line %d: "
5039 "invalid wps_vendor_ext_m1", line);
5040 wpabuf_free(tmp);
5041 return -1;
5042 }
5043
5044 wpabuf_free(config->wps_vendor_ext_m1);
5045 config->wps_vendor_ext_m1 = tmp;
5046 } else {
5047 wpa_printf(MSG_ERROR, "Can not allocate "
5048 "memory for wps_vendor_ext_m1");
5049 return -1;
5050 }
5051
5052 return 0;
5053}
5054
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005055#endif /* CONFIG_WPS */
5056
5057#ifdef CONFIG_P2P
5058static int wpa_config_process_sec_device_type(
5059 const struct global_parse_data *data,
5060 struct wpa_config *config, int line, const char *pos)
5061{
5062 int idx;
5063
5064 if (config->num_sec_device_types >= MAX_SEC_DEVICE_TYPES) {
5065 wpa_printf(MSG_ERROR, "Line %d: too many sec_device_type "
5066 "items", line);
5067 return -1;
5068 }
5069
5070 idx = config->num_sec_device_types;
5071
5072 if (wps_dev_type_str2bin(pos, config->sec_device_type[idx]))
5073 return -1;
5074
5075 config->num_sec_device_types++;
5076 return 0;
5077}
Dmitry Shmidt04949592012-07-19 12:16:46 -07005078
5079
5080static int wpa_config_process_p2p_pref_chan(
5081 const struct global_parse_data *data,
5082 struct wpa_config *config, int line, const char *pos)
5083{
5084 struct p2p_channel *pref = NULL, *n;
Hai Shalomfdcde762020-04-02 11:19:20 -07005085 size_t num = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005086 const char *pos2;
5087 u8 op_class, chan;
5088
5089 /* format: class:chan,class:chan,... */
5090
5091 while (*pos) {
5092 op_class = atoi(pos);
5093 pos2 = os_strchr(pos, ':');
5094 if (pos2 == NULL)
5095 goto fail;
5096 pos2++;
5097 chan = atoi(pos2);
5098
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005099 n = os_realloc_array(pref, num + 1,
5100 sizeof(struct p2p_channel));
Dmitry Shmidt04949592012-07-19 12:16:46 -07005101 if (n == NULL)
5102 goto fail;
5103 pref = n;
5104 pref[num].op_class = op_class;
5105 pref[num].chan = chan;
5106 num++;
5107
5108 pos = os_strchr(pos2, ',');
5109 if (pos == NULL)
5110 break;
5111 pos++;
5112 }
5113
5114 os_free(config->p2p_pref_chan);
5115 config->p2p_pref_chan = pref;
5116 config->num_p2p_pref_chan = num;
5117 wpa_hexdump(MSG_DEBUG, "P2P: Preferred class/channel pairs",
5118 (u8 *) config->p2p_pref_chan,
5119 config->num_p2p_pref_chan * sizeof(struct p2p_channel));
5120
5121 return 0;
5122
5123fail:
5124 os_free(pref);
5125 wpa_printf(MSG_ERROR, "Line %d: Invalid p2p_pref_chan list", line);
5126 return -1;
5127}
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005128
5129
5130static int wpa_config_process_p2p_no_go_freq(
5131 const struct global_parse_data *data,
5132 struct wpa_config *config, int line, const char *pos)
5133{
5134 int ret;
5135
5136 ret = freq_range_list_parse(&config->p2p_no_go_freq, pos);
5137 if (ret < 0) {
5138 wpa_printf(MSG_ERROR, "Line %d: Invalid p2p_no_go_freq", line);
5139 return -1;
5140 }
5141
5142 wpa_printf(MSG_DEBUG, "P2P: p2p_no_go_freq with %u items",
5143 config->p2p_no_go_freq.num);
5144
5145 return 0;
5146}
5147
Hai Shalom39ba6fc2019-01-22 12:40:38 -08005148static int wpa_config_process_p2p_device_persistent_mac_addr(
5149 const struct global_parse_data *data,
5150 struct wpa_config *config, int line, const char *pos)
5151{
5152 if (hwaddr_aton2(pos, config->p2p_device_persistent_mac_addr) < 0) {
5153 wpa_printf(MSG_ERROR,
5154 "Line %d: Invalid p2p_device_persistent_mac_addr '%s'",
5155 line, pos);
5156 return -1;
5157 }
5158
5159 return 0;
5160}
5161
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005162#endif /* CONFIG_P2P */
5163
5164
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005165static int wpa_config_process_hessid(
5166 const struct global_parse_data *data,
5167 struct wpa_config *config, int line, const char *pos)
5168{
5169 if (hwaddr_aton2(pos, config->hessid) < 0) {
5170 wpa_printf(MSG_ERROR, "Line %d: Invalid hessid '%s'",
5171 line, pos);
5172 return -1;
5173 }
5174
5175 return 0;
5176}
5177
5178
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005179static int wpa_config_process_sae_groups(
5180 const struct global_parse_data *data,
5181 struct wpa_config *config, int line, const char *pos)
5182{
5183 int *groups = wpa_config_parse_int_array(pos);
5184 if (groups == NULL) {
5185 wpa_printf(MSG_ERROR, "Line %d: Invalid sae_groups '%s'",
5186 line, pos);
5187 return -1;
5188 }
5189
5190 os_free(config->sae_groups);
5191 config->sae_groups = groups;
5192
5193 return 0;
5194}
5195
5196
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005197static int wpa_config_process_ap_vendor_elements(
5198 const struct global_parse_data *data,
5199 struct wpa_config *config, int line, const char *pos)
5200{
5201 struct wpabuf *tmp;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005202
Hai Shaloma20dcd72022-02-04 13:43:00 -08005203 if (!*pos) {
5204 wpabuf_free(config->ap_vendor_elements);
5205 config->ap_vendor_elements = NULL;
5206 return 0;
5207 }
5208
5209 tmp = wpabuf_parse_bin(pos);
5210 if (!tmp) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005211 wpa_printf(MSG_ERROR, "Line %d: invalid ap_vendor_elements",
5212 line);
5213 return -1;
5214 }
Hai Shaloma20dcd72022-02-04 13:43:00 -08005215 wpabuf_free(config->ap_vendor_elements);
5216 config->ap_vendor_elements = tmp;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005217
Hai Shaloma20dcd72022-02-04 13:43:00 -08005218 return 0;
5219}
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005220
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005221
Hai Shaloma20dcd72022-02-04 13:43:00 -08005222static int wpa_config_process_ap_assocresp_elements(
5223 const struct global_parse_data *data,
5224 struct wpa_config *config, int line, const char *pos)
5225{
5226 struct wpabuf *tmp;
5227
5228 if (!*pos) {
5229 wpabuf_free(config->ap_assocresp_elements);
5230 config->ap_assocresp_elements = NULL;
5231 return 0;
5232 }
5233
5234 tmp = wpabuf_parse_bin(pos);
5235 if (!tmp) {
5236 wpa_printf(MSG_ERROR, "Line %d: invalid ap_assocresp_elements",
5237 line);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005238 return -1;
5239 }
Hai Shaloma20dcd72022-02-04 13:43:00 -08005240 wpabuf_free(config->ap_assocresp_elements);
5241 config->ap_assocresp_elements = tmp;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005242
5243 return 0;
5244}
5245
5246
Dmitry Shmidt56052862013-10-04 10:23:25 -07005247#ifdef CONFIG_CTRL_IFACE
5248static int wpa_config_process_no_ctrl_interface(
5249 const struct global_parse_data *data,
5250 struct wpa_config *config, int line, const char *pos)
5251{
5252 wpa_printf(MSG_DEBUG, "no_ctrl_interface -> ctrl_interface=NULL");
5253 os_free(config->ctrl_interface);
5254 config->ctrl_interface = NULL;
5255 return 0;
5256}
5257#endif /* CONFIG_CTRL_IFACE */
5258
5259
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005260static int wpa_config_get_int(const char *name, struct wpa_config *config,
5261 long offset, char *buf, size_t buflen,
5262 int pretty_print)
5263{
5264 int *val = (int *) (((u8 *) config) + (long) offset);
5265
5266 if (pretty_print)
5267 return os_snprintf(buf, buflen, "%s=%d\n", name, *val);
5268 return os_snprintf(buf, buflen, "%d", *val);
5269}
5270
5271
5272static int wpa_config_get_str(const char *name, struct wpa_config *config,
5273 long offset, char *buf, size_t buflen,
5274 int pretty_print)
5275{
5276 char **val = (char **) (((u8 *) config) + (long) offset);
5277 int res;
5278
5279 if (pretty_print)
5280 res = os_snprintf(buf, buflen, "%s=%s\n", name,
5281 *val ? *val : "null");
5282 else if (!*val)
5283 return -1;
5284 else
5285 res = os_snprintf(buf, buflen, "%s", *val);
5286 if (os_snprintf_error(buflen, res))
5287 res = -1;
5288
5289 return res;
5290}
5291
5292
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07005293#ifdef CONFIG_P2P
5294static int wpa_config_get_ipv4(const char *name, struct wpa_config *config,
5295 long offset, char *buf, size_t buflen,
5296 int pretty_print)
5297{
5298 void *val = ((u8 *) config) + (long) offset;
5299 int res;
5300 char addr[INET_ADDRSTRLEN];
5301
5302 if (!val || !inet_ntop(AF_INET, val, addr, sizeof(addr)))
5303 return -1;
5304
5305 if (pretty_print)
5306 res = os_snprintf(buf, buflen, "%s=%s\n", name, addr);
5307 else
5308 res = os_snprintf(buf, buflen, "%s", addr);
5309
5310 if (os_snprintf_error(buflen, res))
5311 res = -1;
5312
5313 return res;
5314}
5315#endif /* CONFIG_P2P */
5316
5317
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005318#ifdef OFFSET
5319#undef OFFSET
5320#endif /* OFFSET */
5321/* OFFSET: Get offset of a variable within the wpa_config structure */
5322#define OFFSET(v) ((void *) &((struct wpa_config *) 0)->v)
5323
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005324#define FUNC(f) #f, wpa_config_process_ ## f, NULL, OFFSET(f), NULL, NULL
5325#define FUNC_NO_VAR(f) #f, wpa_config_process_ ## f, NULL, NULL, NULL, NULL
5326#define _INT(f) #f, wpa_global_config_parse_int, wpa_config_get_int, OFFSET(f)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005327#define INT(f) _INT(f), NULL, NULL
5328#define INT_RANGE(f, min, max) _INT(f), (void *) min, (void *) max
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005329#define _STR(f) #f, wpa_global_config_parse_str, wpa_config_get_str, OFFSET(f)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005330#define STR(f) _STR(f), NULL, NULL
5331#define STR_RANGE(f, min, max) _STR(f), (void *) min, (void *) max
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005332#define BIN(f) #f, wpa_global_config_parse_bin, NULL, OFFSET(f), NULL, NULL
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07005333#define IPV4(f) #f, wpa_global_config_parse_ipv4, wpa_config_get_ipv4, \
5334 OFFSET(f), NULL, NULL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005335
5336static const struct global_parse_data global_fields[] = {
5337#ifdef CONFIG_CTRL_IFACE
5338 { STR(ctrl_interface), 0 },
Dmitry Shmidt56052862013-10-04 10:23:25 -07005339 { FUNC_NO_VAR(no_ctrl_interface), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005340 { STR(ctrl_interface_group), 0 } /* deprecated */,
5341#endif /* CONFIG_CTRL_IFACE */
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07005342#ifdef CONFIG_MACSEC
5343 { INT_RANGE(eapol_version, 1, 3), 0 },
5344#else /* CONFIG_MACSEC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005345 { INT_RANGE(eapol_version, 1, 2), 0 },
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07005346#endif /* CONFIG_MACSEC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005347 { INT(ap_scan), 0 },
Hai Shalom899fcc72020-10-19 14:38:18 -07005348 { FUNC(bgscan), CFG_CHANGED_BGSCAN },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005349#ifdef CONFIG_MESH
5350 { INT(user_mpm), 0 },
5351 { INT_RANGE(max_peer_links, 0, 255), 0 },
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08005352 { INT(mesh_max_inactivity), 0 },
Hai Shaloma20dcd72022-02-04 13:43:00 -08005353 { INT_RANGE(mesh_fwding, 0, 1), 0 },
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005354 { INT(dot11RSNASAERetransPeriod), 0 },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005355#endif /* CONFIG_MESH */
Dmitry Shmidt04949592012-07-19 12:16:46 -07005356 { INT(disable_scan_offload), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005357 { INT(fast_reauth), 0 },
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005358#ifndef CONFIG_OPENSC_ENGINE_PATH
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005359 { STR(opensc_engine_path), 0 },
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005360#endif /* CONFIG_OPENSC_ENGINE_PATH */
5361#ifndef CONFIG_PKCS11_ENGINE_PATH
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005362 { STR(pkcs11_engine_path), 0 },
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005363#endif /* CONFIG_PKCS11_ENGINE_PATH */
5364#ifndef CONFIG_PKCS11_MODULE_PATH
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005365 { STR(pkcs11_module_path), 0 },
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005366#endif /* CONFIG_PKCS11_MODULE_PATH */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005367 { STR(openssl_ciphers), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07005368 { STR(pcsc_reader), 0 },
5369 { STR(pcsc_pin), 0 },
Dmitry Shmidt051af732013-10-22 13:52:46 -07005370 { INT(external_sim), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005371 { STR(driver_param), 0 },
5372 { INT(dot11RSNAConfigPMKLifetime), 0 },
5373 { INT(dot11RSNAConfigPMKReauthThreshold), 0 },
5374 { INT(dot11RSNAConfigSATimeout), 0 },
5375#ifndef CONFIG_NO_CONFIG_WRITE
5376 { INT(update_config), 0 },
5377#endif /* CONFIG_NO_CONFIG_WRITE */
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005378#ifndef CONFIG_NO_LOAD_DYNAMIC_EAP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005379 { FUNC_NO_VAR(load_dynamic_eap), 0 },
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005380#endif /* CONFIG_NO_LOAD_DYNAMIC_EAP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005381#ifdef CONFIG_WPS
5382 { FUNC(uuid), CFG_CHANGED_UUID },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005383 { INT_RANGE(auto_uuid, 0, 1), 0 },
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07005384 { STR_RANGE(device_name, 0, WPS_DEV_NAME_MAX_LEN),
5385 CFG_CHANGED_DEVICE_NAME },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005386 { STR_RANGE(manufacturer, 0, 64), CFG_CHANGED_WPS_STRING },
5387 { STR_RANGE(model_name, 0, 32), CFG_CHANGED_WPS_STRING },
5388 { STR_RANGE(model_number, 0, 32), CFG_CHANGED_WPS_STRING },
5389 { STR_RANGE(serial_number, 0, 32), CFG_CHANGED_WPS_STRING },
5390 { FUNC(device_type), CFG_CHANGED_DEVICE_TYPE },
5391 { FUNC(os_version), CFG_CHANGED_OS_VERSION },
5392 { STR(config_methods), CFG_CHANGED_CONFIG_METHODS },
5393 { INT_RANGE(wps_cred_processing, 0, 2), 0 },
Hai Shalom021b0b52019-04-10 11:17:58 -07005394 { INT_RANGE(wps_cred_add_sae, 0, 1), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07005395 { FUNC(wps_vendor_ext_m1), CFG_CHANGED_VENDOR_EXTENSION },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005396#endif /* CONFIG_WPS */
5397#ifdef CONFIG_P2P
5398 { FUNC(sec_device_type), CFG_CHANGED_SEC_DEVICE_TYPE },
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005399 { INT(p2p_listen_reg_class), CFG_CHANGED_P2P_LISTEN_CHANNEL },
5400 { INT(p2p_listen_channel), CFG_CHANGED_P2P_LISTEN_CHANNEL },
Dmitry Shmidt56052862013-10-04 10:23:25 -07005401 { INT(p2p_oper_reg_class), CFG_CHANGED_P2P_OPER_CHANNEL },
5402 { INT(p2p_oper_channel), CFG_CHANGED_P2P_OPER_CHANNEL },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005403 { INT_RANGE(p2p_go_intent, 0, 15), 0 },
5404 { STR(p2p_ssid_postfix), CFG_CHANGED_P2P_SSID_POSTFIX },
5405 { INT_RANGE(persistent_reconnect, 0, 1), 0 },
5406 { INT_RANGE(p2p_intra_bss, 0, 1), CFG_CHANGED_P2P_INTRA_BSS },
5407 { INT(p2p_group_idle), 0 },
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005408 { INT_RANGE(p2p_go_freq_change_policy, 0, P2P_GO_FREQ_MOVE_MAX), 0 },
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07005409 { INT_RANGE(p2p_passphrase_len, 8, 63),
5410 CFG_CHANGED_P2P_PASSPHRASE_LEN },
Dmitry Shmidt04949592012-07-19 12:16:46 -07005411 { FUNC(p2p_pref_chan), CFG_CHANGED_P2P_PREF_CHAN },
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005412 { FUNC(p2p_no_go_freq), CFG_CHANGED_P2P_PREF_CHAN },
5413 { INT_RANGE(p2p_add_cli_chan, 0, 1), 0 },
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005414 { INT_RANGE(p2p_optimize_listen_chan, 0, 1), 0 },
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005415 { INT(p2p_go_ht40), 0 },
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005416 { INT(p2p_go_vht), 0 },
Hai Shalom74f70d42019-02-11 14:42:39 -08005417 { INT(p2p_go_he), 0 },
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005418 { INT(p2p_go_edmg), 0 },
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005419 { INT(p2p_disabled), 0 },
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005420 { INT_RANGE(p2p_go_ctwindow, 0, 127), 0 },
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005421 { INT(p2p_no_group_iface), 0 },
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005422 { INT_RANGE(p2p_ignore_shared_freq, 0, 1), 0 },
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005423 { IPV4(ip_addr_go), 0 },
5424 { IPV4(ip_addr_mask), 0 },
5425 { IPV4(ip_addr_start), 0 },
5426 { IPV4(ip_addr_end), 0 },
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005427 { INT_RANGE(p2p_cli_probe, 0, 1), 0 },
Hai Shalom39ba6fc2019-01-22 12:40:38 -08005428 { INT(p2p_device_random_mac_addr), 0 },
5429 { FUNC(p2p_device_persistent_mac_addr), 0 },
5430 { INT(p2p_interface_random_mac_addr), 0 },
Hai Shalom899fcc72020-10-19 14:38:18 -07005431 { INT(p2p_6ghz_disable), 0 },
Shuibing Dai64a8a892023-03-08 10:26:22 -08005432 { INT(p2p_dfs_chan_enable), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005433#endif /* CONFIG_P2P */
5434 { FUNC(country), CFG_CHANGED_COUNTRY },
5435 { INT(bss_max_count), 0 },
5436 { INT(bss_expiration_age), 0 },
5437 { INT(bss_expiration_scan_count), 0 },
5438 { INT_RANGE(filter_ssids, 0, 1), 0 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005439 { INT_RANGE(filter_rssi, -100, 0), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005440 { INT(max_num_sta), 0 },
Roshan Pius3a1667e2018-07-03 15:17:14 -07005441 { INT_RANGE(ap_isolate, 0, 1), 0 },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005442 { INT_RANGE(disassoc_low_ack, 0, 1), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07005443#ifdef CONFIG_HS20
5444 { INT_RANGE(hs20, 0, 1), 0 },
5445#endif /* CONFIG_HS20 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005446 { INT_RANGE(interworking, 0, 1), 0 },
5447 { FUNC(hessid), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07005448 { INT_RANGE(access_network_type, 0, 15), 0 },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005449 { INT_RANGE(go_interworking, 0, 1), 0 },
5450 { INT_RANGE(go_access_network_type, 0, 15), 0 },
5451 { INT_RANGE(go_internet, 0, 1), 0 },
5452 { INT_RANGE(go_venue_group, 0, 255), 0 },
5453 { INT_RANGE(go_venue_type, 0, 255), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07005454 { INT_RANGE(pbc_in_m1, 0, 1), 0 },
5455 { STR(autoscan), 0 },
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005456 { INT_RANGE(wps_nfc_dev_pw_id, 0x10, 0xffff),
5457 CFG_CHANGED_NFC_PASSWORD_TOKEN },
5458 { BIN(wps_nfc_dh_pubkey), CFG_CHANGED_NFC_PASSWORD_TOKEN },
5459 { BIN(wps_nfc_dh_privkey), CFG_CHANGED_NFC_PASSWORD_TOKEN },
5460 { BIN(wps_nfc_dev_pw), CFG_CHANGED_NFC_PASSWORD_TOKEN },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005461 { STR(ext_password_backend), CFG_CHANGED_EXT_PW_BACKEND },
5462 { INT(p2p_go_max_inactivity), 0 },
5463 { INT_RANGE(auto_interworking, 0, 1), 0 },
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005464 { INT(okc), 0 },
5465 { INT(pmf), 0 },
Sunil Ravi77d572f2023-01-17 23:58:31 +00005466 { INT_RANGE(sae_check_mfp, 0, 1), 0 },
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005467 { FUNC(sae_groups), 0 },
Hai Shalomfdcde762020-04-02 11:19:20 -07005468 { INT_RANGE(sae_pwe, 0, 3), 0 },
Hai Shalomc3565922019-10-28 11:58:20 -07005469 { INT_RANGE(sae_pmkid_in_assoc, 0, 1), 0 },
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005470 { INT(dtim_period), 0 },
5471 { INT(beacon_int), 0 },
Hai Shaloma20dcd72022-02-04 13:43:00 -08005472 { FUNC(ap_assocresp_elements), 0 },
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005473 { FUNC(ap_vendor_elements), 0 },
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005474 { INT_RANGE(ignore_old_scan_res, 0, 1), 0 },
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07005475 { FUNC(freq_list), 0 },
Hai Shalom60840252021-02-19 19:02:11 -08005476 { FUNC(initial_freq_list), 0},
Dmitry Shmidtea69e842013-05-13 14:52:28 -07005477 { INT(scan_cur_freq), 0 },
Hai Shalom60840252021-02-19 19:02:11 -08005478 { INT(scan_res_valid_for_connect), 0},
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07005479 { INT(sched_scan_interval), 0 },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005480 { INT(sched_scan_start_delay), 0 },
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005481 { INT(tdls_external_control), 0},
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08005482 { STR(osu_dir), 0 },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005483 { STR(wowlan_triggers), CFG_CHANGED_WOWLAN_TRIGGERS },
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07005484 { INT(p2p_search_delay), 0},
Sunil Ravi77d572f2023-01-17 23:58:31 +00005485 { INT_RANGE(mac_addr, 0, 2), 0 },
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005486 { INT(rand_addr_lifetime), 0 },
Sunil Ravi77d572f2023-01-17 23:58:31 +00005487 { INT_RANGE(preassoc_mac_addr, 0, 2), 0 },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005488 { INT(key_mgmt_offload), 0},
Dmitry Shmidt807291d2015-01-27 13:40:23 -08005489 { INT(passive_scan), 0 },
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005490 { INT(reassoc_same_bss_optim), 0 },
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07005491 { INT(wps_priority), 0},
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005492#ifdef CONFIG_FST
5493 { STR_RANGE(fst_group_id, 1, FST_MAX_GROUP_ID_LEN), 0 },
5494 { INT_RANGE(fst_priority, 1, FST_MAX_PRIO_VALUE), 0 },
5495 { INT_RANGE(fst_llt, 1, FST_MAX_LLT_MS), 0 },
5496#endif /* CONFIG_FST */
Paul Stewart092955c2017-02-06 09:13:09 -08005497 { INT_RANGE(cert_in_cb, 0, 1), 0 },
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005498 { INT_RANGE(wpa_rsc_relaxation, 0, 1), 0 },
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005499 { STR(sched_scan_plans), CFG_CHANGED_SCHED_SCAN_PLANS },
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005500#ifdef CONFIG_MBO
5501 { STR(non_pref_chan), 0 },
5502 { INT_RANGE(mbo_cell_capa, MBO_CELL_CAPA_AVAILABLE,
5503 MBO_CELL_CAPA_NOT_SUPPORTED), 0 },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005504 { INT_RANGE(disassoc_imminent_rssi_threshold, -120, 0), 0 },
5505 { INT_RANGE(oce, 0, 3), 0 },
5506#endif /* CONFIG_MBO */
Sunil Ravi84bb3e12021-06-10 09:52:05 -07005507 { INT_RANGE(btm_offload, 0, 1), CFG_CHANGED_DISABLE_BTM_NOTIFY },
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07005508 { INT(gas_address3), 0 },
Dmitry Shmidt7d175302016-09-06 13:11:34 -07005509 { INT_RANGE(ftm_responder, 0, 1), 0 },
5510 { INT_RANGE(ftm_initiator, 0, 1), 0 },
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005511 { INT(gas_rand_addr_lifetime), 0 },
5512 { INT_RANGE(gas_rand_mac_addr, 0, 2), 0 },
Hai Shalomc3565922019-10-28 11:58:20 -07005513#ifdef CONFIG_DPP
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005514 { INT_RANGE(dpp_config_processing, 0, 2), 0 },
Hai Shalomc3565922019-10-28 11:58:20 -07005515 { STR(dpp_name), 0 },
5516 { STR(dpp_mud_url), 0 },
Sunil Ravi89eba102022-09-13 21:04:37 -07005517 { STR(dpp_extra_conf_req_name), 0 },
5518 { STR(dpp_extra_conf_req_value), 0 },
5519 { INT_RANGE(dpp_connector_privacy_default, 0, 1), 0 },
Hai Shalomc3565922019-10-28 11:58:20 -07005520#endif /* CONFIG_DPP */
Hai Shalom39ba6fc2019-01-22 12:40:38 -08005521 { INT_RANGE(coloc_intf_reporting, 0, 1), 0 },
Jimmy Chen0bb4f862019-03-21 18:41:47 +08005522 { INT_RANGE(bss_no_flush_when_down, 0, 1), 0 },
Hai Shalom81f62d82019-07-22 12:10:00 -07005523#ifdef CONFIG_WNM
5524 { INT_RANGE(disable_btm, 0, 1), CFG_CHANGED_DISABLE_BTM },
Hai Shalomfdcde762020-04-02 11:19:20 -07005525 { INT_RANGE(extended_key_id, 0, 1), 0 },
Hai Shalom81f62d82019-07-22 12:10:00 -07005526#endif /* CONFIG_WNM */
Hai Shalom60840252021-02-19 19:02:11 -08005527 { INT_RANGE(wowlan_disconnect_on_deinit, 0, 1), 0},
5528#ifdef CONFIG_PASN
5529#ifdef CONFIG_TESTING_OPTIONS
5530 { INT_RANGE(force_kdk_derivation, 0, 1), 0 },
Hai Shaloma20dcd72022-02-04 13:43:00 -08005531 { INT_RANGE(pasn_corrupt_mic, 0, 1), 0 },
Hai Shalom60840252021-02-19 19:02:11 -08005532#endif /* CONFIG_TESTING_OPTIONS */
5533#endif /* CONFIG_PASN */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005534};
5535
5536#undef FUNC
5537#undef _INT
5538#undef INT
5539#undef INT_RANGE
5540#undef _STR
5541#undef STR
5542#undef STR_RANGE
Dmitry Shmidt04949592012-07-19 12:16:46 -07005543#undef BIN
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005544#undef IPV4
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005545#define NUM_GLOBAL_FIELDS ARRAY_SIZE(global_fields)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005546
5547
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005548int wpa_config_dump_values(struct wpa_config *config, char *buf, size_t buflen)
5549{
5550 int result = 0;
5551 size_t i;
5552
5553 for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
5554 const struct global_parse_data *field = &global_fields[i];
5555 int tmp;
5556
5557 if (!field->get)
5558 continue;
5559
5560 tmp = field->get(field->name, config, (long) field->param1,
5561 buf, buflen, 1);
5562 if (tmp < 0)
5563 return -1;
5564 buf += tmp;
5565 buflen -= tmp;
5566 result += tmp;
5567 }
5568 return result;
5569}
5570
5571
5572int wpa_config_get_value(const char *name, struct wpa_config *config,
5573 char *buf, size_t buflen)
5574{
5575 size_t i;
5576
5577 for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
5578 const struct global_parse_data *field = &global_fields[i];
5579
5580 if (os_strcmp(name, field->name) != 0)
5581 continue;
5582 if (!field->get)
5583 break;
5584 return field->get(name, config, (long) field->param1,
5585 buf, buflen, 0);
5586 }
5587
5588 return -1;
5589}
5590
5591
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005592int wpa_config_get_num_global_field_names(void)
5593{
5594 return NUM_GLOBAL_FIELDS;
5595}
5596
5597
5598const char * wpa_config_get_global_field_name(unsigned int i, int *no_var)
5599{
5600 if (i >= NUM_GLOBAL_FIELDS)
5601 return NULL;
5602
5603 if (no_var)
5604 *no_var = !global_fields[i].param1;
5605 return global_fields[i].name;
5606}
5607
5608
Hai Shalom899fcc72020-10-19 14:38:18 -07005609/**
5610 * wpa_config_process_global - Set a variable in global configuration
5611 * @config: Pointer to global configuration data
5612 * @pos: Name and value in the format "{name}={value}"
5613 * @line: Line number in configuration file or 0 if not used
5614 * Returns: 0 on success with a possible change in value, 1 on success with no
5615 * change to previously configured value, or -1 on failure
5616 *
5617 * This function can be used to set global configuration variables based on
5618 * both the configuration file and management interface input. The value
5619 * parameter must be in the same format as the text-based configuration file is
5620 * using. For example, strings are using double quotation marks.
5621 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005622int wpa_config_process_global(struct wpa_config *config, char *pos, int line)
5623{
5624 size_t i;
5625 int ret = 0;
5626
5627 for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
5628 const struct global_parse_data *field = &global_fields[i];
5629 size_t flen = os_strlen(field->name);
5630 if (os_strncmp(pos, field->name, flen) != 0 ||
5631 pos[flen] != '=')
5632 continue;
5633
Hai Shalom899fcc72020-10-19 14:38:18 -07005634 ret = field->parser(field, config, line, pos + flen + 1);
5635 if (ret < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005636 wpa_printf(MSG_ERROR, "Line %d: failed to "
5637 "parse '%s'.", line, pos);
5638 ret = -1;
5639 }
Hai Shalom899fcc72020-10-19 14:38:18 -07005640 if (ret == 1)
5641 break;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005642 if (field->changed_flag == CFG_CHANGED_NFC_PASSWORD_TOKEN)
5643 config->wps_nfc_pw_from_config = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005644 config->changed_parameters |= field->changed_flag;
5645 break;
5646 }
5647 if (i == NUM_GLOBAL_FIELDS) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005648#ifdef CONFIG_AP
Hai Shalom899fcc72020-10-19 14:38:18 -07005649 if (os_strncmp(pos, "tx_queue_", 9) == 0) {
5650 char *tmp = os_strchr(pos, '=');
5651
5652 if (!tmp) {
5653 if (line < 0)
5654 wpa_printf(MSG_ERROR,
5655 "Line %d: invalid line %s",
5656 line, pos);
5657 return -1;
5658 }
5659 *tmp++ = '\0';
5660 if (hostapd_config_tx_queue(config->tx_queue, pos,
5661 tmp)) {
5662 wpa_printf(MSG_ERROR,
5663 "Line %d: invalid TX queue item",
5664 line);
5665 return -1;
5666 }
Sunil Ravi2a14cf12023-11-21 00:54:38 +00005667 return ret;
Hai Shalom899fcc72020-10-19 14:38:18 -07005668 }
5669
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005670 if (os_strncmp(pos, "wmm_ac_", 7) == 0) {
5671 char *tmp = os_strchr(pos, '=');
5672 if (tmp == NULL) {
5673 if (line < 0)
5674 return -1;
5675 wpa_printf(MSG_ERROR, "Line %d: invalid line "
5676 "'%s'", line, pos);
5677 return -1;
5678 }
5679 *tmp++ = '\0';
5680 if (hostapd_config_wmm_ac(config->wmm_ac_params, pos,
5681 tmp)) {
5682 wpa_printf(MSG_ERROR, "Line %d: invalid WMM "
5683 "AC item", line);
5684 return -1;
5685 }
Hai Shalomc3565922019-10-28 11:58:20 -07005686 return ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005687 }
5688#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005689 if (line < 0)
5690 return -1;
5691 wpa_printf(MSG_ERROR, "Line %d: unknown global field '%s'.",
5692 line, pos);
5693 ret = -1;
5694 }
5695
5696 return ret;
5697}