blob: 065b6fba758684fbcb9e441cae2e2be3357e113c [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 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700796 else if (os_strcmp(start, "WPA-PSK-SHA256") == 0)
797 val |= WPA_KEY_MGMT_PSK_SHA256;
798 else if (os_strcmp(start, "WPA-EAP-SHA256") == 0)
799 val |= WPA_KEY_MGMT_IEEE8021X_SHA256;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700800#ifdef CONFIG_WPS
801 else if (os_strcmp(start, "WPS") == 0)
802 val |= WPA_KEY_MGMT_WPS;
803#endif /* CONFIG_WPS */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800804#ifdef CONFIG_SAE
805 else if (os_strcmp(start, "SAE") == 0)
806 val |= WPA_KEY_MGMT_SAE;
Sunil Ravi89eba102022-09-13 21:04:37 -0700807 else if (os_strcmp(start, "SAE-EXT-KEY") == 0)
808 val |= WPA_KEY_MGMT_SAE_EXT_KEY;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800809 else if (os_strcmp(start, "FT-SAE") == 0)
810 val |= WPA_KEY_MGMT_FT_SAE;
Sunil Ravi89eba102022-09-13 21:04:37 -0700811 else if (os_strcmp(start, "FT-SAE-EXT-KEY") == 0)
812 val |= WPA_KEY_MGMT_FT_SAE_EXT_KEY;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800813#endif /* CONFIG_SAE */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800814#ifdef CONFIG_HS20
815 else if (os_strcmp(start, "OSEN") == 0)
816 val |= WPA_KEY_MGMT_OSEN;
817#endif /* CONFIG_HS20 */
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800818#ifdef CONFIG_SUITEB
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800819 else if (os_strcmp(start, "WPA-EAP-SUITE-B") == 0)
820 val |= WPA_KEY_MGMT_IEEE8021X_SUITE_B;
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800821#endif /* CONFIG_SUITEB */
822#ifdef CONFIG_SUITEB192
823 else if (os_strcmp(start, "WPA-EAP-SUITE-B-192") == 0)
824 val |= WPA_KEY_MGMT_IEEE8021X_SUITE_B_192;
825#endif /* CONFIG_SUITEB192 */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800826#ifdef CONFIG_FILS
827 else if (os_strcmp(start, "FILS-SHA256") == 0)
828 val |= WPA_KEY_MGMT_FILS_SHA256;
829 else if (os_strcmp(start, "FILS-SHA384") == 0)
830 val |= WPA_KEY_MGMT_FILS_SHA384;
831#ifdef CONFIG_IEEE80211R
832 else if (os_strcmp(start, "FT-FILS-SHA256") == 0)
833 val |= WPA_KEY_MGMT_FT_FILS_SHA256;
834 else if (os_strcmp(start, "FT-FILS-SHA384") == 0)
835 val |= WPA_KEY_MGMT_FT_FILS_SHA384;
836#endif /* CONFIG_IEEE80211R */
837#endif /* CONFIG_FILS */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700838#ifdef CONFIG_OWE
839 else if (os_strcmp(start, "OWE") == 0)
840 val |= WPA_KEY_MGMT_OWE;
841#endif /* CONFIG_OWE */
842#ifdef CONFIG_DPP
843 else if (os_strcmp(start, "DPP") == 0)
844 val |= WPA_KEY_MGMT_DPP;
845#endif /* CONFIG_DPP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700846 else {
847 wpa_printf(MSG_ERROR, "Line %d: invalid key_mgmt '%s'",
848 line, start);
849 errors++;
850 }
851
852 if (last)
853 break;
854 start = end + 1;
855 }
856 os_free(buf);
857
858 if (val == 0) {
859 wpa_printf(MSG_ERROR,
860 "Line %d: no key_mgmt values configured.", line);
861 errors++;
862 }
863
Dmitry Shmidte4663042016-04-04 10:07:49 -0700864 if (!errors && ssid->key_mgmt == val)
865 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700866 wpa_printf(MSG_MSGDUMP, "key_mgmt: 0x%x", val);
867 ssid->key_mgmt = val;
868 return errors ? -1 : 0;
869}
870
871
872#ifndef NO_CONFIG_WRITE
873static char * wpa_config_write_key_mgmt(const struct parse_data *data,
874 struct wpa_ssid *ssid)
875{
876 char *buf, *pos, *end;
877 int ret;
878
Dmitry Shmidt96571392013-10-14 12:54:46 -0700879 pos = buf = os_zalloc(100);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700880 if (buf == NULL)
881 return NULL;
Dmitry Shmidt96571392013-10-14 12:54:46 -0700882 end = buf + 100;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700883
884 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
885 ret = os_snprintf(pos, end - pos, "%sWPA-PSK",
886 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800887 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700888 end[-1] = '\0';
889 return buf;
890 }
891 pos += ret;
892 }
893
894 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
895 ret = os_snprintf(pos, end - pos, "%sWPA-EAP",
896 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800897 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700898 end[-1] = '\0';
899 return buf;
900 }
901 pos += ret;
902 }
903
904 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
905 ret = os_snprintf(pos, end - pos, "%sIEEE8021X",
906 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800907 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700908 end[-1] = '\0';
909 return buf;
910 }
911 pos += ret;
912 }
913
914 if (ssid->key_mgmt & WPA_KEY_MGMT_NONE) {
915 ret = os_snprintf(pos, end - pos, "%sNONE",
916 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800917 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700918 end[-1] = '\0';
919 return buf;
920 }
921 pos += ret;
922 }
923
924 if (ssid->key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
925 ret = os_snprintf(pos, end - pos, "%sWPA-NONE",
926 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800927 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700928 end[-1] = '\0';
929 return buf;
930 }
931 pos += ret;
932 }
933
934#ifdef CONFIG_IEEE80211R
Dmitry Shmidt96571392013-10-14 12:54:46 -0700935 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_PSK) {
936 ret = os_snprintf(pos, end - pos, "%sFT-PSK",
937 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800938 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt96571392013-10-14 12:54:46 -0700939 end[-1] = '\0';
940 return buf;
941 }
942 pos += ret;
943 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700944
Dmitry Shmidt96571392013-10-14 12:54:46 -0700945 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
946 ret = os_snprintf(pos, end - pos, "%sFT-EAP",
947 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800948 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt96571392013-10-14 12:54:46 -0700949 end[-1] = '\0';
950 return buf;
951 }
952 pos += ret;
953 }
Roshan Pius3a1667e2018-07-03 15:17:14 -0700954
955#ifdef CONFIG_SHA384
956 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X_SHA384) {
957 ret = os_snprintf(pos, end - pos, "%sFT-EAP-SHA384",
958 pos == buf ? "" : " ");
959 if (os_snprintf_error(end - pos, ret)) {
960 end[-1] = '\0';
961 return buf;
962 }
963 pos += ret;
964 }
965#endif /* CONFIG_SHA384 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700966#endif /* CONFIG_IEEE80211R */
967
Dmitry Shmidt96571392013-10-14 12:54:46 -0700968 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
969 ret = os_snprintf(pos, end - pos, "%sWPA-PSK-SHA256",
970 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800971 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt96571392013-10-14 12:54:46 -0700972 end[-1] = '\0';
973 return buf;
974 }
975 pos += ret;
976 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700977
Dmitry Shmidt96571392013-10-14 12:54:46 -0700978 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
979 ret = os_snprintf(pos, end - pos, "%sWPA-EAP-SHA256",
980 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800981 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt96571392013-10-14 12:54:46 -0700982 end[-1] = '\0';
983 return buf;
984 }
985 pos += ret;
986 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700987
988#ifdef CONFIG_WPS
Dmitry Shmidt96571392013-10-14 12:54:46 -0700989 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
990 ret = os_snprintf(pos, end - pos, "%sWPS",
991 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800992 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt96571392013-10-14 12:54:46 -0700993 end[-1] = '\0';
994 return buf;
995 }
996 pos += ret;
997 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700998#endif /* CONFIG_WPS */
999
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001000#ifdef CONFIG_SAE
1001 if (ssid->key_mgmt & WPA_KEY_MGMT_SAE) {
1002 ret = os_snprintf(pos, end - pos, "%sSAE",
1003 pos == buf ? "" : " ");
1004 if (os_snprintf_error(end - pos, ret)) {
1005 end[-1] = '\0';
1006 return buf;
1007 }
1008 pos += ret;
1009 }
1010
Sunil Ravi89eba102022-09-13 21:04:37 -07001011 if (ssid->key_mgmt & WPA_KEY_MGMT_SAE_EXT_KEY) {
1012 ret = os_snprintf(pos, end - pos, "%sSAE-EXT-KEY",
1013 pos == buf ? "" : " ");
1014 if (os_snprintf_error(end - pos, ret)) {
1015 end[-1] = '\0';
1016 return buf;
1017 }
1018 pos += ret;
1019 }
1020
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001021 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_SAE) {
1022 ret = os_snprintf(pos, end - pos, "%sFT-SAE",
1023 pos == buf ? "" : " ");
1024 if (os_snprintf_error(end - pos, ret)) {
1025 end[-1] = '\0';
1026 return buf;
1027 }
1028 pos += ret;
1029 }
Sunil Ravi89eba102022-09-13 21:04:37 -07001030
1031 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_SAE_EXT_KEY) {
1032 ret = os_snprintf(pos, end - pos, "%sFT-SAE-EXT-KEY",
1033 pos == buf ? "" : " ");
1034 if (os_snprintf_error(end - pos, ret)) {
1035 end[-1] = '\0';
1036 return buf;
1037 }
1038 pos += ret;
1039 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001040#endif /* CONFIG_SAE */
1041
1042#ifdef CONFIG_HS20
1043 if (ssid->key_mgmt & WPA_KEY_MGMT_OSEN) {
1044 ret = os_snprintf(pos, end - pos, "%sOSEN",
1045 pos == buf ? "" : " ");
1046 if (os_snprintf_error(end - pos, ret)) {
1047 end[-1] = '\0';
1048 return buf;
1049 }
1050 pos += ret;
1051 }
1052#endif /* CONFIG_HS20 */
1053
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001054#ifdef CONFIG_SUITEB
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001055 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
1056 ret = os_snprintf(pos, end - pos, "%sWPA-EAP-SUITE-B",
1057 pos == buf ? "" : " ");
1058 if (os_snprintf_error(end - pos, ret)) {
1059 end[-1] = '\0';
1060 return buf;
1061 }
1062 pos += ret;
1063 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001064#endif /* CONFIG_SUITEB */
1065
1066#ifdef CONFIG_SUITEB192
1067 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
1068 ret = os_snprintf(pos, end - pos, "%sWPA-EAP-SUITE-B-192",
1069 pos == buf ? "" : " ");
1070 if (os_snprintf_error(end - pos, ret)) {
1071 end[-1] = '\0';
1072 return buf;
1073 }
1074 pos += ret;
1075 }
1076#endif /* CONFIG_SUITEB192 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001077
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001078#ifdef CONFIG_FILS
1079 if (ssid->key_mgmt & WPA_KEY_MGMT_FILS_SHA256) {
1080 ret = os_snprintf(pos, end - pos, "%sFILS-SHA256",
1081 pos == buf ? "" : " ");
1082 if (os_snprintf_error(end - pos, ret)) {
1083 end[-1] = '\0';
1084 return buf;
1085 }
1086 pos += ret;
1087 }
1088 if (ssid->key_mgmt & WPA_KEY_MGMT_FILS_SHA384) {
1089 ret = os_snprintf(pos, end - pos, "%sFILS-SHA384",
1090 pos == buf ? "" : " ");
1091 if (os_snprintf_error(end - pos, ret)) {
1092 end[-1] = '\0';
1093 return buf;
1094 }
1095 pos += ret;
1096 }
1097#ifdef CONFIG_IEEE80211R
1098 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA256) {
1099 ret = os_snprintf(pos, end - pos, "%sFT-FILS-SHA256",
1100 pos == buf ? "" : " ");
1101 if (os_snprintf_error(end - pos, ret)) {
1102 end[-1] = '\0';
1103 return buf;
1104 }
1105 pos += ret;
1106 }
1107 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA384) {
1108 ret = os_snprintf(pos, end - pos, "%sFT-FILS-SHA384",
1109 pos == buf ? "" : " ");
1110 if (os_snprintf_error(end - pos, ret)) {
1111 end[-1] = '\0';
1112 return buf;
1113 }
1114 pos += ret;
1115 }
1116#endif /* CONFIG_IEEE80211R */
1117#endif /* CONFIG_FILS */
1118
Hai Shalom74f70d42019-02-11 14:42:39 -08001119#ifdef CONFIG_DPP
1120 if (ssid->key_mgmt & WPA_KEY_MGMT_DPP) {
1121 ret = os_snprintf(pos, end - pos, "%sDPP",
1122 pos == buf ? "" : " ");
1123 if (os_snprintf_error(end - pos, ret)) {
1124 end[-1] = '\0';
1125 return buf;
1126 }
1127 pos += ret;
1128 }
1129#endif /* CONFIG_DPP */
1130
1131#ifdef CONFIG_OWE
1132 if (ssid->key_mgmt & WPA_KEY_MGMT_OWE) {
1133 ret = os_snprintf(pos, end - pos, "%sOWE",
1134 pos == buf ? "" : " ");
1135 if (os_snprintf_error(end - pos, ret)) {
1136 end[-1] = '\0';
1137 return buf;
1138 }
1139 pos += ret;
1140 }
1141#endif /* CONFIG_OWE */
1142
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001143 if (pos == buf) {
1144 os_free(buf);
1145 buf = NULL;
1146 }
1147
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001148 return buf;
1149}
1150#endif /* NO_CONFIG_WRITE */
1151
1152
1153static int wpa_config_parse_cipher(int line, const char *value)
1154{
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001155#ifdef CONFIG_NO_WPA
1156 return -1;
1157#else /* CONFIG_NO_WPA */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001158 int val = wpa_parse_cipher(value);
1159 if (val < 0) {
1160 wpa_printf(MSG_ERROR, "Line %d: invalid cipher '%s'.",
1161 line, value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001162 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001163 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001164 if (val == 0) {
1165 wpa_printf(MSG_ERROR, "Line %d: no cipher values configured.",
1166 line);
1167 return -1;
1168 }
1169 return val;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001170#endif /* CONFIG_NO_WPA */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001171}
1172
1173
1174#ifndef NO_CONFIG_WRITE
1175static char * wpa_config_write_cipher(int cipher)
1176{
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001177#ifdef CONFIG_NO_WPA
1178 return NULL;
1179#else /* CONFIG_NO_WPA */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001180 char *buf = os_zalloc(50);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001181 if (buf == NULL)
1182 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001183
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001184 if (wpa_write_ciphers(buf, buf + 50, cipher, " ") < 0) {
1185 os_free(buf);
1186 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001187 }
1188
1189 return buf;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001190#endif /* CONFIG_NO_WPA */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001191}
1192#endif /* NO_CONFIG_WRITE */
1193
1194
1195static int wpa_config_parse_pairwise(const struct parse_data *data,
1196 struct wpa_ssid *ssid, int line,
1197 const char *value)
1198{
1199 int val;
1200 val = wpa_config_parse_cipher(line, value);
1201 if (val == -1)
1202 return -1;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001203 if (val & ~WPA_ALLOWED_PAIRWISE_CIPHERS) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001204 wpa_printf(MSG_ERROR, "Line %d: not allowed pairwise cipher "
1205 "(0x%x).", line, val);
1206 return -1;
1207 }
1208
Dmitry Shmidte4663042016-04-04 10:07:49 -07001209 if (ssid->pairwise_cipher == val)
1210 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001211 wpa_printf(MSG_MSGDUMP, "pairwise: 0x%x", val);
1212 ssid->pairwise_cipher = val;
1213 return 0;
1214}
1215
1216
1217#ifndef NO_CONFIG_WRITE
1218static char * wpa_config_write_pairwise(const struct parse_data *data,
1219 struct wpa_ssid *ssid)
1220{
1221 return wpa_config_write_cipher(ssid->pairwise_cipher);
1222}
1223#endif /* NO_CONFIG_WRITE */
1224
1225
1226static int wpa_config_parse_group(const struct parse_data *data,
1227 struct wpa_ssid *ssid, int line,
1228 const char *value)
1229{
1230 int val;
1231 val = wpa_config_parse_cipher(line, value);
1232 if (val == -1)
1233 return -1;
Dmitry Shmidt41712582015-06-29 11:02:15 -07001234
1235 /*
1236 * Backwards compatibility - filter out WEP ciphers that were previously
1237 * allowed.
1238 */
1239 val &= ~(WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40);
1240
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001241 if (val & ~WPA_ALLOWED_GROUP_CIPHERS) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001242 wpa_printf(MSG_ERROR, "Line %d: not allowed group cipher "
1243 "(0x%x).", line, val);
1244 return -1;
1245 }
1246
Dmitry Shmidte4663042016-04-04 10:07:49 -07001247 if (ssid->group_cipher == val)
1248 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001249 wpa_printf(MSG_MSGDUMP, "group: 0x%x", val);
1250 ssid->group_cipher = val;
1251 return 0;
1252}
1253
1254
1255#ifndef NO_CONFIG_WRITE
1256static char * wpa_config_write_group(const struct parse_data *data,
1257 struct wpa_ssid *ssid)
1258{
1259 return wpa_config_write_cipher(ssid->group_cipher);
1260}
1261#endif /* NO_CONFIG_WRITE */
1262
1263
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001264static int wpa_config_parse_group_mgmt(const struct parse_data *data,
1265 struct wpa_ssid *ssid, int line,
1266 const char *value)
1267{
1268 int val;
1269
1270 val = wpa_config_parse_cipher(line, value);
1271 if (val == -1)
1272 return -1;
1273
1274 if (val & ~WPA_ALLOWED_GROUP_MGMT_CIPHERS) {
1275 wpa_printf(MSG_ERROR,
1276 "Line %d: not allowed group management cipher (0x%x).",
1277 line, val);
1278 return -1;
1279 }
1280
1281 if (ssid->group_mgmt_cipher == val)
1282 return 1;
1283 wpa_printf(MSG_MSGDUMP, "group_mgmt: 0x%x", val);
1284 ssid->group_mgmt_cipher = val;
1285 return 0;
1286}
1287
1288
1289#ifndef NO_CONFIG_WRITE
1290static char * wpa_config_write_group_mgmt(const struct parse_data *data,
1291 struct wpa_ssid *ssid)
1292{
1293 return wpa_config_write_cipher(ssid->group_mgmt_cipher);
1294}
1295#endif /* NO_CONFIG_WRITE */
1296
1297
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001298static int wpa_config_parse_auth_alg(const struct parse_data *data,
1299 struct wpa_ssid *ssid, int line,
1300 const char *value)
1301{
1302 int val = 0, last, errors = 0;
1303 char *start, *end, *buf;
1304
1305 buf = os_strdup(value);
1306 if (buf == NULL)
1307 return -1;
1308 start = buf;
1309
1310 while (*start != '\0') {
1311 while (*start == ' ' || *start == '\t')
1312 start++;
1313 if (*start == '\0')
1314 break;
1315 end = start;
1316 while (*end != ' ' && *end != '\t' && *end != '\0')
1317 end++;
1318 last = *end == '\0';
1319 *end = '\0';
1320 if (os_strcmp(start, "OPEN") == 0)
1321 val |= WPA_AUTH_ALG_OPEN;
1322 else if (os_strcmp(start, "SHARED") == 0)
1323 val |= WPA_AUTH_ALG_SHARED;
1324 else if (os_strcmp(start, "LEAP") == 0)
1325 val |= WPA_AUTH_ALG_LEAP;
1326 else {
1327 wpa_printf(MSG_ERROR, "Line %d: invalid auth_alg '%s'",
1328 line, start);
1329 errors++;
1330 }
1331
1332 if (last)
1333 break;
1334 start = end + 1;
1335 }
1336 os_free(buf);
1337
1338 if (val == 0) {
1339 wpa_printf(MSG_ERROR,
1340 "Line %d: no auth_alg values configured.", line);
1341 errors++;
1342 }
1343
Dmitry Shmidte4663042016-04-04 10:07:49 -07001344 if (!errors && ssid->auth_alg == val)
1345 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001346 wpa_printf(MSG_MSGDUMP, "auth_alg: 0x%x", val);
1347 ssid->auth_alg = val;
1348 return errors ? -1 : 0;
1349}
1350
1351
1352#ifndef NO_CONFIG_WRITE
1353static char * wpa_config_write_auth_alg(const struct parse_data *data,
1354 struct wpa_ssid *ssid)
1355{
1356 char *buf, *pos, *end;
1357 int ret;
1358
1359 pos = buf = os_zalloc(30);
1360 if (buf == NULL)
1361 return NULL;
1362 end = buf + 30;
1363
1364 if (ssid->auth_alg & WPA_AUTH_ALG_OPEN) {
1365 ret = os_snprintf(pos, end - pos, "%sOPEN",
1366 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001367 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001368 end[-1] = '\0';
1369 return buf;
1370 }
1371 pos += ret;
1372 }
1373
1374 if (ssid->auth_alg & WPA_AUTH_ALG_SHARED) {
1375 ret = os_snprintf(pos, end - pos, "%sSHARED",
1376 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001377 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001378 end[-1] = '\0';
1379 return buf;
1380 }
1381 pos += ret;
1382 }
1383
1384 if (ssid->auth_alg & WPA_AUTH_ALG_LEAP) {
1385 ret = os_snprintf(pos, end - pos, "%sLEAP",
1386 pos == buf ? "" : " ");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001387 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001388 end[-1] = '\0';
1389 return buf;
1390 }
1391 pos += ret;
1392 }
1393
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001394 if (pos == buf) {
1395 os_free(buf);
1396 buf = NULL;
1397 }
1398
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001399 return buf;
1400}
1401#endif /* NO_CONFIG_WRITE */
1402
1403
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001404static int * wpa_config_parse_int_array(const char *value)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001405{
1406 int *freqs;
1407 size_t used, len;
1408 const char *pos;
1409
1410 used = 0;
1411 len = 10;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001412 freqs = os_calloc(len + 1, sizeof(int));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001413 if (freqs == NULL)
1414 return NULL;
1415
1416 pos = value;
1417 while (pos) {
1418 while (*pos == ' ')
1419 pos++;
1420 if (used == len) {
1421 int *n;
1422 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001423 n = os_realloc_array(freqs, len * 2 + 1, sizeof(int));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001424 if (n == NULL) {
1425 os_free(freqs);
1426 return NULL;
1427 }
1428 for (i = len; i <= len * 2; i++)
1429 n[i] = 0;
1430 freqs = n;
1431 len *= 2;
1432 }
1433
1434 freqs[used] = atoi(pos);
1435 if (freqs[used] == 0)
1436 break;
1437 used++;
1438 pos = os_strchr(pos + 1, ' ');
1439 }
1440
1441 return freqs;
1442}
1443
1444
1445static int wpa_config_parse_scan_freq(const struct parse_data *data,
1446 struct wpa_ssid *ssid, int line,
1447 const char *value)
1448{
1449 int *freqs;
1450
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001451 freqs = wpa_config_parse_int_array(value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001452 if (freqs == NULL)
1453 return -1;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001454 if (freqs[0] == 0) {
1455 os_free(freqs);
1456 freqs = NULL;
1457 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001458 os_free(ssid->scan_freq);
1459 ssid->scan_freq = freqs;
1460
1461 return 0;
1462}
1463
1464
1465static int wpa_config_parse_freq_list(const struct parse_data *data,
1466 struct wpa_ssid *ssid, int line,
1467 const char *value)
1468{
1469 int *freqs;
1470
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001471 freqs = wpa_config_parse_int_array(value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001472 if (freqs == NULL)
1473 return -1;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001474 if (freqs[0] == 0) {
1475 os_free(freqs);
1476 freqs = NULL;
1477 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001478 os_free(ssid->freq_list);
1479 ssid->freq_list = freqs;
1480
1481 return 0;
1482}
1483
1484
1485#ifndef NO_CONFIG_WRITE
1486static char * wpa_config_write_freqs(const struct parse_data *data,
1487 const int *freqs)
1488{
1489 char *buf, *pos, *end;
1490 int i, ret;
1491 size_t count;
1492
1493 if (freqs == NULL)
1494 return NULL;
1495
1496 count = 0;
1497 for (i = 0; freqs[i]; i++)
1498 count++;
1499
1500 pos = buf = os_zalloc(10 * count + 1);
1501 if (buf == NULL)
1502 return NULL;
1503 end = buf + 10 * count + 1;
1504
1505 for (i = 0; freqs[i]; i++) {
1506 ret = os_snprintf(pos, end - pos, "%s%u",
1507 i == 0 ? "" : " ", freqs[i]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001508 if (os_snprintf_error(end - pos, ret)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001509 end[-1] = '\0';
1510 return buf;
1511 }
1512 pos += ret;
1513 }
1514
1515 return buf;
1516}
1517
1518
1519static char * wpa_config_write_scan_freq(const struct parse_data *data,
1520 struct wpa_ssid *ssid)
1521{
1522 return wpa_config_write_freqs(data, ssid->scan_freq);
1523}
1524
1525
1526static char * wpa_config_write_freq_list(const struct parse_data *data,
1527 struct wpa_ssid *ssid)
1528{
1529 return wpa_config_write_freqs(data, ssid->freq_list);
1530}
1531#endif /* NO_CONFIG_WRITE */
1532
1533
1534#ifdef IEEE8021X_EAPOL
1535static int wpa_config_parse_eap(const struct parse_data *data,
1536 struct wpa_ssid *ssid, int line,
1537 const char *value)
1538{
1539 int last, errors = 0;
1540 char *start, *end, *buf;
1541 struct eap_method_type *methods = NULL, *tmp;
1542 size_t num_methods = 0;
1543
1544 buf = os_strdup(value);
1545 if (buf == NULL)
1546 return -1;
1547 start = buf;
1548
1549 while (*start != '\0') {
1550 while (*start == ' ' || *start == '\t')
1551 start++;
1552 if (*start == '\0')
1553 break;
1554 end = start;
1555 while (*end != ' ' && *end != '\t' && *end != '\0')
1556 end++;
1557 last = *end == '\0';
1558 *end = '\0';
1559 tmp = methods;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001560 methods = os_realloc_array(methods, num_methods + 1,
1561 sizeof(*methods));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001562 if (methods == NULL) {
1563 os_free(tmp);
1564 os_free(buf);
1565 return -1;
1566 }
1567 methods[num_methods].method = eap_peer_get_type(
1568 start, &methods[num_methods].vendor);
1569 if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
1570 methods[num_methods].method == EAP_TYPE_NONE) {
1571 wpa_printf(MSG_ERROR, "Line %d: unknown EAP method "
1572 "'%s'", line, start);
1573 wpa_printf(MSG_ERROR, "You may need to add support for"
1574 " this EAP method during wpa_supplicant\n"
1575 "build time configuration.\n"
1576 "See README for more information.");
1577 errors++;
1578 } else if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
1579 methods[num_methods].method == EAP_TYPE_LEAP)
1580 ssid->leap++;
1581 else
1582 ssid->non_leap++;
1583 num_methods++;
1584 if (last)
1585 break;
1586 start = end + 1;
1587 }
1588 os_free(buf);
1589
1590 tmp = methods;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001591 methods = os_realloc_array(methods, num_methods + 1, sizeof(*methods));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001592 if (methods == NULL) {
1593 os_free(tmp);
1594 return -1;
1595 }
1596 methods[num_methods].vendor = EAP_VENDOR_IETF;
1597 methods[num_methods].method = EAP_TYPE_NONE;
1598 num_methods++;
1599
Dmitry Shmidte4663042016-04-04 10:07:49 -07001600 if (!errors && ssid->eap.eap_methods) {
1601 struct eap_method_type *prev_m;
1602 size_t i, j, prev_methods, match = 0;
1603
1604 prev_m = ssid->eap.eap_methods;
1605 for (i = 0; prev_m[i].vendor != EAP_VENDOR_IETF ||
1606 prev_m[i].method != EAP_TYPE_NONE; i++) {
1607 /* Count the methods */
1608 }
1609 prev_methods = i + 1;
1610
1611 for (i = 0; prev_methods == num_methods && i < prev_methods;
1612 i++) {
1613 for (j = 0; j < num_methods; j++) {
1614 if (prev_m[i].vendor == methods[j].vendor &&
1615 prev_m[i].method == methods[j].method) {
1616 match++;
1617 break;
1618 }
1619 }
1620 }
1621 if (match == num_methods) {
1622 os_free(methods);
1623 return 1;
1624 }
1625 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001626 wpa_hexdump(MSG_MSGDUMP, "eap methods",
1627 (u8 *) methods, num_methods * sizeof(*methods));
Dmitry Shmidt04949592012-07-19 12:16:46 -07001628 os_free(ssid->eap.eap_methods);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001629 ssid->eap.eap_methods = methods;
1630 return errors ? -1 : 0;
1631}
1632
1633
Dmitry Shmidt83474442015-04-15 13:47:09 -07001634#ifndef NO_CONFIG_WRITE
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001635static char * wpa_config_write_eap(const struct parse_data *data,
1636 struct wpa_ssid *ssid)
1637{
1638 int i, ret;
1639 char *buf, *pos, *end;
1640 const struct eap_method_type *eap_methods = ssid->eap.eap_methods;
1641 const char *name;
1642
1643 if (eap_methods == NULL)
1644 return NULL;
1645
1646 pos = buf = os_zalloc(100);
1647 if (buf == NULL)
1648 return NULL;
1649 end = buf + 100;
1650
1651 for (i = 0; eap_methods[i].vendor != EAP_VENDOR_IETF ||
1652 eap_methods[i].method != EAP_TYPE_NONE; i++) {
1653 name = eap_get_name(eap_methods[i].vendor,
1654 eap_methods[i].method);
1655 if (name) {
1656 ret = os_snprintf(pos, end - pos, "%s%s",
1657 pos == buf ? "" : " ", name);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001658 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001659 break;
1660 pos += ret;
1661 }
1662 }
1663
1664 end[-1] = '\0';
1665
1666 return buf;
1667}
Dmitry Shmidt83474442015-04-15 13:47:09 -07001668#endif /* NO_CONFIG_WRITE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001669
1670
1671static int wpa_config_parse_password(const struct parse_data *data,
1672 struct wpa_ssid *ssid, int line,
1673 const char *value)
1674{
1675 u8 *hash;
1676
1677 if (os_strcmp(value, "NULL") == 0) {
Dmitry Shmidte4663042016-04-04 10:07:49 -07001678 if (!ssid->eap.password)
1679 return 1; /* Already unset */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001680 wpa_printf(MSG_DEBUG, "Unset configuration string 'password'");
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001681 bin_clear_free(ssid->eap.password, ssid->eap.password_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001682 ssid->eap.password = NULL;
1683 ssid->eap.password_len = 0;
1684 return 0;
1685 }
1686
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001687#ifdef CONFIG_EXT_PASSWORD
1688 if (os_strncmp(value, "ext:", 4) == 0) {
1689 char *name = os_strdup(value + 4);
Hai Shalomc3565922019-10-28 11:58:20 -07001690 if (!name)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001691 return -1;
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001692 bin_clear_free(ssid->eap.password, ssid->eap.password_len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001693 ssid->eap.password = (u8 *) name;
1694 ssid->eap.password_len = os_strlen(name);
1695 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
1696 ssid->eap.flags |= EAP_CONFIG_FLAGS_EXT_PASSWORD;
1697 return 0;
1698 }
1699#endif /* CONFIG_EXT_PASSWORD */
1700
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001701 if (os_strncmp(value, "hash:", 5) != 0) {
1702 char *tmp;
1703 size_t res_len;
1704
1705 tmp = wpa_config_parse_string(value, &res_len);
Hai Shalomc3565922019-10-28 11:58:20 -07001706 if (!tmp) {
1707 wpa_printf(MSG_ERROR,
1708 "Line %d: failed to parse password.", line);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001709 return -1;
1710 }
1711 wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
1712 (u8 *) tmp, res_len);
1713
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001714 bin_clear_free(ssid->eap.password, ssid->eap.password_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001715 ssid->eap.password = (u8 *) tmp;
1716 ssid->eap.password_len = res_len;
1717 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001718 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_PASSWORD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001719
1720 return 0;
1721 }
1722
1723
1724 /* NtPasswordHash: hash:<32 hex digits> */
1725 if (os_strlen(value + 5) != 2 * 16) {
Hai Shalomc3565922019-10-28 11:58:20 -07001726 wpa_printf(MSG_ERROR,
1727 "Line %d: Invalid password hash length (expected 32 hex digits)",
1728 line);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001729 return -1;
1730 }
1731
1732 hash = os_malloc(16);
Hai Shalomc3565922019-10-28 11:58:20 -07001733 if (!hash)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001734 return -1;
1735
1736 if (hexstr2bin(value + 5, hash, 16)) {
1737 os_free(hash);
1738 wpa_printf(MSG_ERROR, "Line %d: Invalid password hash", line);
1739 return -1;
1740 }
1741
1742 wpa_hexdump_key(MSG_MSGDUMP, data->name, hash, 16);
1743
Dmitry Shmidte4663042016-04-04 10:07:49 -07001744 if (ssid->eap.password && ssid->eap.password_len == 16 &&
1745 os_memcmp(ssid->eap.password, hash, 16) == 0 &&
1746 (ssid->eap.flags & EAP_CONFIG_FLAGS_PASSWORD_NTHASH)) {
1747 bin_clear_free(hash, 16);
1748 return 1;
1749 }
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001750 bin_clear_free(ssid->eap.password, ssid->eap.password_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001751 ssid->eap.password = hash;
1752 ssid->eap.password_len = 16;
1753 ssid->eap.flags |= EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001754 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_PASSWORD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001755
1756 return 0;
1757}
1758
1759
Hai Shalomc3565922019-10-28 11:58:20 -07001760static int wpa_config_parse_machine_password(const struct parse_data *data,
1761 struct wpa_ssid *ssid, int line,
1762 const char *value)
1763{
1764 u8 *hash;
1765
1766 if (os_strcmp(value, "NULL") == 0) {
1767 if (!ssid->eap.machine_password)
1768 return 1; /* Already unset */
1769 wpa_printf(MSG_DEBUG,
1770 "Unset configuration string 'machine_password'");
1771 bin_clear_free(ssid->eap.machine_password,
1772 ssid->eap.machine_password_len);
1773 ssid->eap.machine_password = NULL;
1774 ssid->eap.machine_password_len = 0;
1775 return 0;
1776 }
1777
1778#ifdef CONFIG_EXT_PASSWORD
1779 if (os_strncmp(value, "ext:", 4) == 0) {
1780 char *name = os_strdup(value + 4);
1781
1782 if (!name)
1783 return -1;
1784 bin_clear_free(ssid->eap.machine_password,
1785 ssid->eap.machine_password_len);
1786 ssid->eap.machine_password = (u8 *) name;
1787 ssid->eap.machine_password_len = os_strlen(name);
1788 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_MACHINE_PASSWORD_NTHASH;
1789 ssid->eap.flags |= EAP_CONFIG_FLAGS_EXT_MACHINE_PASSWORD;
1790 return 0;
1791 }
1792#endif /* CONFIG_EXT_PASSWORD */
1793
1794 if (os_strncmp(value, "hash:", 5) != 0) {
1795 char *tmp;
1796 size_t res_len;
1797
1798 tmp = wpa_config_parse_string(value, &res_len);
1799 if (!tmp) {
1800 wpa_printf(MSG_ERROR,
1801 "Line %d: failed to parse machine_password.",
1802 line);
1803 return -1;
1804 }
1805 wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
1806 (u8 *) tmp, res_len);
1807
1808 bin_clear_free(ssid->eap.machine_password,
1809 ssid->eap.machine_password_len);
1810 ssid->eap.machine_password = (u8 *) tmp;
1811 ssid->eap.machine_password_len = res_len;
1812 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_MACHINE_PASSWORD_NTHASH;
1813 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_MACHINE_PASSWORD;
1814
1815 return 0;
1816 }
1817
1818
1819 /* NtPasswordHash: hash:<32 hex digits> */
1820 if (os_strlen(value + 5) != 2 * 16) {
1821 wpa_printf(MSG_ERROR,
1822 "Line %d: Invalid machine_password hash length (expected 32 hex digits)",
1823 line);
1824 return -1;
1825 }
1826
1827 hash = os_malloc(16);
1828 if (!hash)
1829 return -1;
1830
1831 if (hexstr2bin(value + 5, hash, 16)) {
1832 os_free(hash);
1833 wpa_printf(MSG_ERROR, "Line %d: Invalid machine_password hash",
1834 line);
1835 return -1;
1836 }
1837
1838 wpa_hexdump_key(MSG_MSGDUMP, data->name, hash, 16);
1839
1840 if (ssid->eap.machine_password &&
1841 ssid->eap.machine_password_len == 16 &&
1842 os_memcmp(ssid->eap.machine_password, hash, 16) == 0 &&
1843 (ssid->eap.flags & EAP_CONFIG_FLAGS_MACHINE_PASSWORD_NTHASH)) {
1844 bin_clear_free(hash, 16);
1845 return 1;
1846 }
1847 bin_clear_free(ssid->eap.machine_password,
1848 ssid->eap.machine_password_len);
1849 ssid->eap.machine_password = hash;
1850 ssid->eap.machine_password_len = 16;
1851 ssid->eap.flags |= EAP_CONFIG_FLAGS_MACHINE_PASSWORD_NTHASH;
1852 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_MACHINE_PASSWORD;
1853
1854 return 0;
1855}
1856
1857
Dmitry Shmidt83474442015-04-15 13:47:09 -07001858#ifndef NO_CONFIG_WRITE
Hai Shalomc3565922019-10-28 11:58:20 -07001859
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001860static char * wpa_config_write_password(const struct parse_data *data,
1861 struct wpa_ssid *ssid)
1862{
1863 char *buf;
1864
Hai Shalomc3565922019-10-28 11:58:20 -07001865 if (!ssid->eap.password)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001866 return NULL;
1867
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001868#ifdef CONFIG_EXT_PASSWORD
1869 if (ssid->eap.flags & EAP_CONFIG_FLAGS_EXT_PASSWORD) {
1870 buf = os_zalloc(4 + ssid->eap.password_len + 1);
Hai Shalomc3565922019-10-28 11:58:20 -07001871 if (!buf)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001872 return NULL;
1873 os_memcpy(buf, "ext:", 4);
1874 os_memcpy(buf + 4, ssid->eap.password, ssid->eap.password_len);
1875 return buf;
1876 }
1877#endif /* CONFIG_EXT_PASSWORD */
1878
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001879 if (!(ssid->eap.flags & EAP_CONFIG_FLAGS_PASSWORD_NTHASH)) {
1880 return wpa_config_write_string(
1881 ssid->eap.password, ssid->eap.password_len);
1882 }
1883
1884 buf = os_malloc(5 + 32 + 1);
Hai Shalomc3565922019-10-28 11:58:20 -07001885 if (!buf)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001886 return NULL;
1887
1888 os_memcpy(buf, "hash:", 5);
1889 wpa_snprintf_hex(buf + 5, 32 + 1, ssid->eap.password, 16);
1890
1891 return buf;
1892}
Hai Shalomc3565922019-10-28 11:58:20 -07001893
1894
1895static char * wpa_config_write_machine_password(const struct parse_data *data,
1896 struct wpa_ssid *ssid)
1897{
1898 char *buf;
1899
1900 if (!ssid->eap.machine_password)
1901 return NULL;
1902
1903#ifdef CONFIG_EXT_PASSWORD
1904 if (ssid->eap.flags & EAP_CONFIG_FLAGS_EXT_MACHINE_PASSWORD) {
1905 buf = os_zalloc(4 + ssid->eap.machine_password_len + 1);
1906 if (!buf)
1907 return NULL;
1908 os_memcpy(buf, "ext:", 4);
1909 os_memcpy(buf + 4, ssid->eap.machine_password,
1910 ssid->eap.machine_password_len);
1911 return buf;
1912 }
1913#endif /* CONFIG_EXT_PASSWORD */
1914
1915 if (!(ssid->eap.flags & EAP_CONFIG_FLAGS_MACHINE_PASSWORD_NTHASH)) {
1916 return wpa_config_write_string(
1917 ssid->eap.machine_password,
1918 ssid->eap.machine_password_len);
1919 }
1920
1921 buf = os_malloc(5 + 32 + 1);
1922 if (!buf)
1923 return NULL;
1924
1925 os_memcpy(buf, "hash:", 5);
1926 wpa_snprintf_hex(buf + 5, 32 + 1, ssid->eap.machine_password, 16);
1927
1928 return buf;
1929}
1930
Dmitry Shmidt83474442015-04-15 13:47:09 -07001931#endif /* NO_CONFIG_WRITE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001932#endif /* IEEE8021X_EAPOL */
1933
1934
Hai Shalomfdcde762020-04-02 11:19:20 -07001935#ifdef CONFIG_WEP
1936
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001937static int wpa_config_parse_wep_key(u8 *key, size_t *len, int line,
1938 const char *value, int idx)
1939{
1940 char *buf, title[20];
1941 int res;
1942
1943 buf = wpa_config_parse_string(value, len);
1944 if (buf == NULL) {
1945 wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key %d '%s'.",
1946 line, idx, value);
1947 return -1;
1948 }
1949 if (*len > MAX_WEP_KEY_LEN) {
1950 wpa_printf(MSG_ERROR, "Line %d: Too long WEP key %d '%s'.",
1951 line, idx, value);
1952 os_free(buf);
1953 return -1;
1954 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001955 if (*len && *len != 5 && *len != 13 && *len != 16) {
1956 wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key length %u - "
1957 "this network block will be ignored",
1958 line, (unsigned int) *len);
1959 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001960 os_memcpy(key, buf, *len);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001961 str_clear_free(buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001962 res = os_snprintf(title, sizeof(title), "wep_key%d", idx);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001963 if (!os_snprintf_error(sizeof(title), res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001964 wpa_hexdump_key(MSG_MSGDUMP, title, key, *len);
1965 return 0;
1966}
1967
1968
1969static int wpa_config_parse_wep_key0(const struct parse_data *data,
1970 struct wpa_ssid *ssid, int line,
1971 const char *value)
1972{
1973 return wpa_config_parse_wep_key(ssid->wep_key[0],
1974 &ssid->wep_key_len[0], line,
1975 value, 0);
1976}
1977
1978
1979static int wpa_config_parse_wep_key1(const struct parse_data *data,
1980 struct wpa_ssid *ssid, int line,
1981 const char *value)
1982{
1983 return wpa_config_parse_wep_key(ssid->wep_key[1],
1984 &ssid->wep_key_len[1], line,
1985 value, 1);
1986}
1987
1988
1989static int wpa_config_parse_wep_key2(const struct parse_data *data,
1990 struct wpa_ssid *ssid, int line,
1991 const char *value)
1992{
1993 return wpa_config_parse_wep_key(ssid->wep_key[2],
1994 &ssid->wep_key_len[2], line,
1995 value, 2);
1996}
1997
1998
1999static int wpa_config_parse_wep_key3(const struct parse_data *data,
2000 struct wpa_ssid *ssid, int line,
2001 const char *value)
2002{
2003 return wpa_config_parse_wep_key(ssid->wep_key[3],
2004 &ssid->wep_key_len[3], line,
2005 value, 3);
2006}
2007
2008
2009#ifndef NO_CONFIG_WRITE
2010static char * wpa_config_write_wep_key(struct wpa_ssid *ssid, int idx)
2011{
2012 if (ssid->wep_key_len[idx] == 0)
2013 return NULL;
2014 return wpa_config_write_string(ssid->wep_key[idx],
2015 ssid->wep_key_len[idx]);
2016}
2017
2018
2019static char * wpa_config_write_wep_key0(const struct parse_data *data,
2020 struct wpa_ssid *ssid)
2021{
2022 return wpa_config_write_wep_key(ssid, 0);
2023}
2024
2025
2026static char * wpa_config_write_wep_key1(const struct parse_data *data,
2027 struct wpa_ssid *ssid)
2028{
2029 return wpa_config_write_wep_key(ssid, 1);
2030}
2031
2032
2033static char * wpa_config_write_wep_key2(const struct parse_data *data,
2034 struct wpa_ssid *ssid)
2035{
2036 return wpa_config_write_wep_key(ssid, 2);
2037}
2038
2039
2040static char * wpa_config_write_wep_key3(const struct parse_data *data,
2041 struct wpa_ssid *ssid)
2042{
2043 return wpa_config_write_wep_key(ssid, 3);
2044}
2045#endif /* NO_CONFIG_WRITE */
2046
Hai Shalomfdcde762020-04-02 11:19:20 -07002047#endif /* CONFIG_WEP */
2048
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002049
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002050#ifdef CONFIG_P2P
2051
Dmitry Shmidt54605472013-11-08 11:10:19 -08002052static int wpa_config_parse_go_p2p_dev_addr(const struct parse_data *data,
2053 struct wpa_ssid *ssid, int line,
2054 const char *value)
2055{
2056 if (value[0] == '\0' || os_strcmp(value, "\"\"") == 0 ||
2057 os_strcmp(value, "any") == 0) {
2058 os_memset(ssid->go_p2p_dev_addr, 0, ETH_ALEN);
2059 wpa_printf(MSG_MSGDUMP, "GO P2P Device Address any");
2060 return 0;
2061 }
2062 if (hwaddr_aton(value, ssid->go_p2p_dev_addr)) {
2063 wpa_printf(MSG_ERROR, "Line %d: Invalid GO P2P Device Address '%s'.",
2064 line, value);
2065 return -1;
2066 }
2067 ssid->bssid_set = 1;
2068 wpa_printf(MSG_MSGDUMP, "GO P2P Device Address " MACSTR,
2069 MAC2STR(ssid->go_p2p_dev_addr));
2070 return 0;
2071}
2072
2073
2074#ifndef NO_CONFIG_WRITE
2075static char * wpa_config_write_go_p2p_dev_addr(const struct parse_data *data,
2076 struct wpa_ssid *ssid)
2077{
2078 char *value;
2079 int res;
2080
2081 if (is_zero_ether_addr(ssid->go_p2p_dev_addr))
2082 return NULL;
2083
2084 value = os_malloc(20);
2085 if (value == NULL)
2086 return NULL;
2087 res = os_snprintf(value, 20, MACSTR, MAC2STR(ssid->go_p2p_dev_addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002088 if (os_snprintf_error(20, res)) {
Dmitry Shmidt54605472013-11-08 11:10:19 -08002089 os_free(value);
2090 return NULL;
2091 }
2092 value[20 - 1] = '\0';
2093 return value;
2094}
2095#endif /* NO_CONFIG_WRITE */
2096
2097
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002098static int wpa_config_parse_p2p_client_list(const struct parse_data *data,
2099 struct wpa_ssid *ssid, int line,
2100 const char *value)
2101{
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002102 return wpa_config_parse_addr_list(data, line, value,
2103 &ssid->p2p_client_list,
2104 &ssid->num_p2p_clients,
2105 "p2p_client_list", 0, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002106}
2107
2108
2109#ifndef NO_CONFIG_WRITE
2110static char * wpa_config_write_p2p_client_list(const struct parse_data *data,
2111 struct wpa_ssid *ssid)
2112{
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002113 return wpa_config_write_addr_list(data, ssid->p2p_client_list,
2114 ssid->num_p2p_clients,
2115 "p2p_client_list");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002116}
2117#endif /* NO_CONFIG_WRITE */
2118
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002119
2120static int wpa_config_parse_psk_list(const struct parse_data *data,
2121 struct wpa_ssid *ssid, int line,
2122 const char *value)
2123{
2124 struct psk_list_entry *p;
2125 const char *pos;
2126
2127 p = os_zalloc(sizeof(*p));
2128 if (p == NULL)
2129 return -1;
2130
2131 pos = value;
2132 if (os_strncmp(pos, "P2P-", 4) == 0) {
2133 p->p2p = 1;
2134 pos += 4;
2135 }
2136
2137 if (hwaddr_aton(pos, p->addr)) {
2138 wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list address '%s'",
2139 line, pos);
2140 os_free(p);
2141 return -1;
2142 }
2143 pos += 17;
2144 if (*pos != '-') {
2145 wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list '%s'",
2146 line, pos);
2147 os_free(p);
2148 return -1;
2149 }
2150 pos++;
2151
2152 if (hexstr2bin(pos, p->psk, PMK_LEN) || pos[PMK_LEN * 2] != '\0') {
2153 wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list PSK '%s'",
2154 line, pos);
2155 os_free(p);
2156 return -1;
2157 }
2158
2159 dl_list_add(&ssid->psk_list, &p->list);
2160
2161 return 0;
2162}
2163
2164
2165#ifndef NO_CONFIG_WRITE
2166static char * wpa_config_write_psk_list(const struct parse_data *data,
2167 struct wpa_ssid *ssid)
2168{
2169 return NULL;
2170}
2171#endif /* NO_CONFIG_WRITE */
2172
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002173#endif /* CONFIG_P2P */
2174
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002175
2176#ifdef CONFIG_MESH
2177
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002178static int wpa_config_parse_mesh_basic_rates(const struct parse_data *data,
2179 struct wpa_ssid *ssid, int line,
2180 const char *value)
2181{
2182 int *rates = wpa_config_parse_int_array(value);
2183
2184 if (rates == NULL) {
2185 wpa_printf(MSG_ERROR, "Line %d: Invalid mesh_basic_rates '%s'",
2186 line, value);
2187 return -1;
2188 }
2189 if (rates[0] == 0) {
2190 os_free(rates);
2191 rates = NULL;
2192 }
2193
2194 os_free(ssid->mesh_basic_rates);
2195 ssid->mesh_basic_rates = rates;
2196
2197 return 0;
2198}
2199
2200
2201#ifndef NO_CONFIG_WRITE
2202
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002203static char * wpa_config_write_mesh_basic_rates(const struct parse_data *data,
2204 struct wpa_ssid *ssid)
2205{
2206 return wpa_config_write_freqs(data, ssid->mesh_basic_rates);
2207}
2208
2209#endif /* NO_CONFIG_WRITE */
2210
2211#endif /* CONFIG_MESH */
2212
2213
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002214#ifdef CONFIG_MACSEC
2215
2216static int wpa_config_parse_mka_cak(const struct parse_data *data,
2217 struct wpa_ssid *ssid, int line,
2218 const char *value)
2219{
Hai Shalom74f70d42019-02-11 14:42:39 -08002220 size_t len;
2221
2222 len = os_strlen(value);
2223 if (len > 2 * MACSEC_CAK_MAX_LEN ||
2224 (len != 2 * 16 && len != 2 * 32) ||
2225 hexstr2bin(value, ssid->mka_cak, len / 2)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002226 wpa_printf(MSG_ERROR, "Line %d: Invalid MKA-CAK '%s'.",
2227 line, value);
2228 return -1;
2229 }
Hai Shalom74f70d42019-02-11 14:42:39 -08002230 ssid->mka_cak_len = len / 2;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002231 ssid->mka_psk_set |= MKA_PSK_SET_CAK;
2232
Hai Shalom74f70d42019-02-11 14:42:39 -08002233 wpa_hexdump_key(MSG_MSGDUMP, "MKA-CAK", ssid->mka_cak,
2234 ssid->mka_cak_len);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002235 return 0;
2236}
2237
2238
2239static int wpa_config_parse_mka_ckn(const struct parse_data *data,
2240 struct wpa_ssid *ssid, int line,
2241 const char *value)
2242{
Hai Shalom74f70d42019-02-11 14:42:39 -08002243 size_t len;
2244
2245 len = os_strlen(value);
2246 if (len > 2 * MACSEC_CKN_MAX_LEN || /* too long */
2247 len < 2 || /* too short */
2248 len % 2 != 0 /* not an integral number of bytes */) {
2249 wpa_printf(MSG_ERROR, "Line %d: Invalid MKA-CKN '%s'.",
2250 line, value);
2251 return -1;
2252 }
2253 ssid->mka_ckn_len = len / 2;
2254 if (hexstr2bin(value, ssid->mka_ckn, ssid->mka_ckn_len)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002255 wpa_printf(MSG_ERROR, "Line %d: Invalid MKA-CKN '%s'.",
2256 line, value);
2257 return -1;
2258 }
2259
2260 ssid->mka_psk_set |= MKA_PSK_SET_CKN;
2261
Hai Shalom74f70d42019-02-11 14:42:39 -08002262 wpa_hexdump_key(MSG_MSGDUMP, "MKA-CKN", ssid->mka_ckn,
2263 ssid->mka_ckn_len);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002264 return 0;
2265}
2266
2267
2268#ifndef NO_CONFIG_WRITE
2269
2270static char * wpa_config_write_mka_cak(const struct parse_data *data,
2271 struct wpa_ssid *ssid)
2272{
2273 if (!(ssid->mka_psk_set & MKA_PSK_SET_CAK))
2274 return NULL;
2275
Hai Shalom74f70d42019-02-11 14:42:39 -08002276 return wpa_config_write_string_hex(ssid->mka_cak, ssid->mka_cak_len);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002277}
2278
2279
2280static char * wpa_config_write_mka_ckn(const struct parse_data *data,
2281 struct wpa_ssid *ssid)
2282{
2283 if (!(ssid->mka_psk_set & MKA_PSK_SET_CKN))
2284 return NULL;
Hai Shalom74f70d42019-02-11 14:42:39 -08002285 return wpa_config_write_string_hex(ssid->mka_ckn, ssid->mka_ckn_len);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002286}
2287
2288#endif /* NO_CONFIG_WRITE */
2289
2290#endif /* CONFIG_MACSEC */
2291
2292
Hai Shalom74f70d42019-02-11 14:42:39 -08002293#ifdef CONFIG_OCV
2294
2295static int wpa_config_parse_ocv(const struct parse_data *data,
2296 struct wpa_ssid *ssid, int line,
2297 const char *value)
2298{
2299 char *end;
2300
2301 ssid->ocv = strtol(value, &end, 0);
2302 if (*end || ssid->ocv < 0 || ssid->ocv > 1) {
2303 wpa_printf(MSG_ERROR, "Line %d: Invalid ocv value '%s'.",
2304 line, value);
2305 return -1;
2306 }
2307 if (ssid->ocv && ssid->ieee80211w == NO_MGMT_FRAME_PROTECTION)
2308 ssid->ieee80211w = MGMT_FRAME_PROTECTION_OPTIONAL;
2309 return 0;
2310}
2311
2312
2313#ifndef NO_CONFIG_WRITE
2314static char * wpa_config_write_ocv(const struct parse_data *data,
2315 struct wpa_ssid *ssid)
2316{
2317 char *value = os_malloc(20);
2318
2319 if (!value)
2320 return NULL;
2321 os_snprintf(value, 20, "%d", ssid->ocv);
2322 value[20 - 1] = '\0';
2323 return value;
2324}
2325#endif /* NO_CONFIG_WRITE */
2326
2327#endif /* CONFIG_OCV */
2328
2329
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002330static int wpa_config_parse_peerkey(const struct parse_data *data,
2331 struct wpa_ssid *ssid, int line,
2332 const char *value)
2333{
2334 wpa_printf(MSG_INFO, "NOTE: Obsolete peerkey parameter ignored");
2335 return 0;
2336}
2337
2338
2339#ifndef NO_CONFIG_WRITE
2340static char * wpa_config_write_peerkey(const struct parse_data *data,
2341 struct wpa_ssid *ssid)
2342{
2343 return NULL;
2344}
2345#endif /* NO_CONFIG_WRITE */
2346
2347
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002348/* Helper macros for network block parser */
2349
2350#ifdef OFFSET
2351#undef OFFSET
2352#endif /* OFFSET */
2353/* OFFSET: Get offset of a variable within the wpa_ssid structure */
2354#define OFFSET(v) ((void *) &((struct wpa_ssid *) 0)->v)
2355
2356/* STR: Define a string variable for an ASCII string; f = field name */
2357#ifdef NO_CONFIG_WRITE
2358#define _STR(f) #f, wpa_config_parse_str, OFFSET(f)
Hai Shalomc3565922019-10-28 11:58:20 -07002359#define _STRe(f, m) #f, wpa_config_parse_str, OFFSET(eap.m)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002360#else /* NO_CONFIG_WRITE */
2361#define _STR(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(f)
Hai Shalomc3565922019-10-28 11:58:20 -07002362#define _STRe(f, m) #f, wpa_config_parse_str, wpa_config_write_str, \
2363 OFFSET(eap.m)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002364#endif /* NO_CONFIG_WRITE */
2365#define STR(f) _STR(f), NULL, NULL, NULL, 0
Hai Shalomc3565922019-10-28 11:58:20 -07002366#define STRe(f, m) _STRe(f, m), NULL, NULL, NULL, 0
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002367#define STR_KEY(f) _STR(f), NULL, NULL, NULL, 1
Hai Shalomc3565922019-10-28 11:58:20 -07002368#define STR_KEYe(f, m) _STRe(f, m), NULL, NULL, NULL, 1
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002369
2370/* STR_LEN: Define a string variable with a separate variable for storing the
2371 * data length. Unlike STR(), this can be used to store arbitrary binary data
2372 * (i.e., even nul termination character). */
2373#define _STR_LEN(f) _STR(f), OFFSET(f ## _len)
Hai Shalomc3565922019-10-28 11:58:20 -07002374#define _STR_LENe(f, m) _STRe(f, m), OFFSET(eap.m ## _len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002375#define STR_LEN(f) _STR_LEN(f), NULL, NULL, 0
Hai Shalomc3565922019-10-28 11:58:20 -07002376#define STR_LENe(f, m) _STR_LENe(f, m), NULL, NULL, 0
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002377#define STR_LEN_KEY(f) _STR_LEN(f), NULL, NULL, 1
2378
2379/* STR_RANGE: Like STR_LEN(), but with minimum and maximum allowed length
2380 * explicitly specified. */
2381#define _STR_RANGE(f, min, max) _STR_LEN(f), (void *) (min), (void *) (max)
2382#define STR_RANGE(f, min, max) _STR_RANGE(f, min, max), 0
2383#define STR_RANGE_KEY(f, min, max) _STR_RANGE(f, min, max), 1
2384
2385#ifdef NO_CONFIG_WRITE
2386#define _INT(f) #f, wpa_config_parse_int, OFFSET(f), (void *) 0
Hai Shalomc3565922019-10-28 11:58:20 -07002387#define _INTe(f, m) #f, wpa_config_parse_int, OFFSET(eap.m), (void *) 0
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002388#else /* NO_CONFIG_WRITE */
2389#define _INT(f) #f, wpa_config_parse_int, wpa_config_write_int, \
2390 OFFSET(f), (void *) 0
Hai Shalomc3565922019-10-28 11:58:20 -07002391#define _INTe(f, m) #f, wpa_config_parse_int, wpa_config_write_int, \
2392 OFFSET(eap.m), (void *) 0
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002393#endif /* NO_CONFIG_WRITE */
2394
2395/* INT: Define an integer variable */
2396#define INT(f) _INT(f), NULL, NULL, 0
Hai Shalomc3565922019-10-28 11:58:20 -07002397#define INTe(f, m) _INTe(f, m), NULL, NULL, 0
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002398
2399/* INT_RANGE: Define an integer variable with allowed value range */
2400#define INT_RANGE(f, min, max) _INT(f), (void *) (min), (void *) (max), 0
2401
2402/* FUNC: Define a configuration variable that uses a custom function for
2403 * parsing and writing the value. */
2404#ifdef NO_CONFIG_WRITE
2405#define _FUNC(f) #f, wpa_config_parse_ ## f, NULL, NULL, NULL, NULL
2406#else /* NO_CONFIG_WRITE */
2407#define _FUNC(f) #f, wpa_config_parse_ ## f, wpa_config_write_ ## f, \
2408 NULL, NULL, NULL, NULL
2409#endif /* NO_CONFIG_WRITE */
2410#define FUNC(f) _FUNC(f), 0
2411#define FUNC_KEY(f) _FUNC(f), 1
2412
2413/*
2414 * Table of network configuration variables. This table is used to parse each
2415 * network configuration variable, e.g., each line in wpa_supplicant.conf file
2416 * that is inside a network block.
2417 *
2418 * This table is generated using the helper macros defined above and with
2419 * generous help from the C pre-processor. The field name is stored as a string
2420 * into .name and for STR and INT types, the offset of the target buffer within
2421 * struct wpa_ssid is stored in .param1. .param2 (if not NULL) is similar
2422 * offset to the field containing the length of the configuration variable.
2423 * .param3 and .param4 can be used to mark the allowed range (length for STR
2424 * and value for INT).
2425 *
2426 * For each configuration line in wpa_supplicant.conf, the parser goes through
2427 * this table and select the entry that matches with the field name. The parser
2428 * function (.parser) is then called to parse the actual value of the field.
2429 *
2430 * This kind of mechanism makes it easy to add new configuration parameters,
2431 * since only one line needs to be added into this table and into the
2432 * struct wpa_ssid definition if the new variable is either a string or
2433 * integer. More complex types will need to use their own parser and writer
2434 * functions.
2435 */
2436static const struct parse_data ssid_fields[] = {
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07002437 { STR_RANGE(ssid, 0, SSID_MAX_LEN) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002438 { INT_RANGE(scan_ssid, 0, 1) },
2439 { FUNC(bssid) },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002440 { FUNC(bssid_hint) },
Hai Shalom60840252021-02-19 19:02:11 -08002441 { FUNC(bssid_ignore) },
2442 { FUNC(bssid_accept) },
2443 { FUNC(bssid_blacklist) }, /* deprecated alias for bssid_ignore */
2444 { FUNC(bssid_whitelist) }, /* deprecated alias for bssid_accept */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002445 { FUNC_KEY(psk) },
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07002446 { INT(mem_only_psk) },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002447 { STR_KEY(sae_password) },
Roshan Pius3a1667e2018-07-03 15:17:14 -07002448 { STR(sae_password_id) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002449 { FUNC(proto) },
2450 { FUNC(key_mgmt) },
Dmitry Shmidt04949592012-07-19 12:16:46 -07002451 { INT(bg_scan_period) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002452 { FUNC(pairwise) },
2453 { FUNC(group) },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002454 { FUNC(group_mgmt) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002455 { FUNC(auth_alg) },
2456 { FUNC(scan_freq) },
2457 { FUNC(freq_list) },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002458 { INT_RANGE(ht, 0, 1) },
2459 { INT_RANGE(vht, 0, 1) },
2460 { INT_RANGE(ht40, -1, 1) },
Sunil8cd6f4d2022-06-28 18:40:46 +00002461 { INT_RANGE(max_oper_chwidth, CONF_OPER_CHWIDTH_USE_HT,
2462 CONF_OPER_CHWIDTH_80P80MHZ) },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002463 { INT(vht_center_freq1) },
2464 { INT(vht_center_freq2) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002465#ifdef IEEE8021X_EAPOL
2466 { FUNC(eap) },
Hai Shalomc3565922019-10-28 11:58:20 -07002467 { STR_LENe(identity, identity) },
2468 { STR_LENe(anonymous_identity, anonymous_identity) },
2469 { STR_LENe(imsi_identity, imsi_identity) },
2470 { STR_LENe(machine_identity, machine_identity) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002471 { FUNC_KEY(password) },
Hai Shalomc3565922019-10-28 11:58:20 -07002472 { FUNC_KEY(machine_password) },
2473 { STRe(ca_cert, cert.ca_cert) },
2474 { STRe(ca_path, cert.ca_path) },
2475 { STRe(client_cert, cert.client_cert) },
2476 { STRe(private_key, cert.private_key) },
2477 { STR_KEYe(private_key_passwd, cert.private_key_passwd) },
Hai Shalomc3565922019-10-28 11:58:20 -07002478 { STRe(subject_match, cert.subject_match) },
2479 { STRe(check_cert_subject, cert.check_cert_subject) },
2480 { STRe(altsubject_match, cert.altsubject_match) },
2481 { STRe(domain_suffix_match, cert.domain_suffix_match) },
2482 { STRe(domain_match, cert.domain_match) },
2483 { STRe(ca_cert2, phase2_cert.ca_cert) },
2484 { STRe(ca_path2, phase2_cert.ca_path) },
2485 { STRe(client_cert2, phase2_cert.client_cert) },
2486 { STRe(private_key2, phase2_cert.private_key) },
2487 { STR_KEYe(private_key2_passwd, phase2_cert.private_key_passwd) },
Hai Shalomc3565922019-10-28 11:58:20 -07002488 { STRe(subject_match2, phase2_cert.subject_match) },
2489 { STRe(check_cert_subject2, phase2_cert.check_cert_subject) },
2490 { STRe(altsubject_match2, phase2_cert.altsubject_match) },
2491 { STRe(domain_suffix_match2, phase2_cert.domain_suffix_match) },
2492 { STRe(domain_match2, phase2_cert.domain_match) },
2493 { STRe(phase1, phase1) },
2494 { STRe(phase2, phase2) },
2495 { STRe(machine_phase2, machine_phase2) },
2496 { STRe(pcsc, pcsc) },
2497 { STR_KEYe(pin, cert.pin) },
2498 { STRe(engine_id, cert.engine_id) },
2499 { STRe(key_id, cert.key_id) },
2500 { STRe(cert_id, cert.cert_id) },
2501 { STRe(ca_cert_id, cert.ca_cert_id) },
2502 { STR_KEYe(pin2, phase2_cert.pin) },
2503 { STRe(engine_id2, phase2_cert.engine_id) },
2504 { STRe(key_id2, phase2_cert.key_id) },
2505 { STRe(cert_id2, phase2_cert.cert_id) },
2506 { STRe(ca_cert_id2, phase2_cert.ca_cert_id) },
2507 { INTe(engine, cert.engine) },
2508 { INTe(engine2, phase2_cert.engine) },
2509 { STRe(machine_ca_cert, machine_cert.ca_cert) },
2510 { STRe(machine_ca_path, machine_cert.ca_path) },
2511 { STRe(machine_client_cert, machine_cert.client_cert) },
2512 { STRe(machine_private_key, machine_cert.private_key) },
2513 { STR_KEYe(machine_private_key_passwd,
2514 machine_cert.private_key_passwd) },
Hai Shalomc3565922019-10-28 11:58:20 -07002515 { STRe(machine_subject_match, machine_cert.subject_match) },
2516 { STRe(machine_check_cert_subject, machine_cert.check_cert_subject) },
2517 { STRe(machine_altsubject_match, machine_cert.altsubject_match) },
2518 { STRe(machine_domain_suffix_match,
2519 machine_cert.domain_suffix_match) },
2520 { STRe(machine_domain_match, machine_cert.domain_match) },
2521 { STR_KEYe(machine_pin, machine_cert.pin) },
2522 { STRe(machine_engine_id, machine_cert.engine_id) },
2523 { STRe(machine_key_id, machine_cert.key_id) },
2524 { STRe(machine_cert_id, machine_cert.cert_id) },
2525 { STRe(machine_ca_cert_id, machine_cert.ca_cert_id) },
2526 { INTe(machine_engine, machine_cert.engine) },
2527 { INTe(machine_ocsp, machine_cert.ocsp) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002528 { INT(eapol_flags) },
Hai Shalomc3565922019-10-28 11:58:20 -07002529 { INTe(sim_num, sim_num) },
Sunil8cd6f4d2022-06-28 18:40:46 +00002530 { STRe(imsi_privacy_cert, imsi_privacy_cert) },
2531 { STRe(imsi_privacy_attr, imsi_privacy_attr) },
Hai Shalomc3565922019-10-28 11:58:20 -07002532 { STRe(openssl_ciphers, openssl_ciphers) },
2533 { INTe(erp, erp) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002534#endif /* IEEE8021X_EAPOL */
Hai Shalomfdcde762020-04-02 11:19:20 -07002535#ifdef CONFIG_WEP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002536 { FUNC_KEY(wep_key0) },
2537 { FUNC_KEY(wep_key1) },
2538 { FUNC_KEY(wep_key2) },
2539 { FUNC_KEY(wep_key3) },
2540 { INT(wep_tx_keyidx) },
Hai Shalomfdcde762020-04-02 11:19:20 -07002541#endif /* CONFIG_WEP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002542 { INT(priority) },
2543#ifdef IEEE8021X_EAPOL
2544 { INT(eap_workaround) },
Hai Shalomc3565922019-10-28 11:58:20 -07002545 { STRe(pac_file, pac_file) },
2546 { INTe(fragment_size, fragment_size) },
2547 { INTe(ocsp, cert.ocsp) },
2548 { INTe(ocsp2, phase2_cert.ocsp) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002549#endif /* IEEE8021X_EAPOL */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002550#ifdef CONFIG_MESH
2551 { INT_RANGE(mode, 0, 5) },
2552 { INT_RANGE(no_auto_peer, 0, 1) },
Hai Shaloma20dcd72022-02-04 13:43:00 -08002553 { INT_RANGE(mesh_fwding, 0, 1) },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002554 { INT_RANGE(mesh_rssi_threshold, -255, 1) },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002555#else /* CONFIG_MESH */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002556 { INT_RANGE(mode, 0, 4) },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002557#endif /* CONFIG_MESH */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002558 { INT_RANGE(proactive_key_caching, 0, 1) },
2559 { INT_RANGE(disabled, 0, 2) },
2560 { STR(id_str) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002561 { INT_RANGE(ieee80211w, 0, 2) },
Hai Shalom74f70d42019-02-11 14:42:39 -08002562#ifdef CONFIG_OCV
2563 { FUNC(ocv) },
2564#endif /* CONFIG_OCV */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002565 { FUNC(peerkey) /* obsolete - removed */ },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002566 { INT_RANGE(mixed_cell, 0, 1) },
Hai Shalomc3565922019-10-28 11:58:20 -07002567 { INT_RANGE(frequency, 0, 70200) },
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002568 { INT_RANGE(fixed_freq, 0, 1) },
Hai Shalomc3565922019-10-28 11:58:20 -07002569 { INT_RANGE(enable_edmg, 0, 1) },
2570 { INT_RANGE(edmg_channel, 9, 13) },
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -08002571#ifdef CONFIG_ACS
2572 { INT_RANGE(acs, 0, 1) },
2573#endif /* CONFIG_ACS */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002574#ifdef CONFIG_MESH
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002575 { FUNC(mesh_basic_rates) },
2576 { INT(dot11MeshMaxRetries) },
2577 { INT(dot11MeshRetryTimeout) },
2578 { INT(dot11MeshConfirmTimeout) },
2579 { INT(dot11MeshHoldingTimeout) },
2580#endif /* CONFIG_MESH */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002581 { INT(wpa_ptk_rekey) },
Hai Shalomfdcde762020-04-02 11:19:20 -07002582 { INT_RANGE(wpa_deny_ptk0_rekey, 0, 2) },
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07002583 { INT(group_rekey) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002584 { STR(bgscan) },
Dmitry Shmidt04949592012-07-19 12:16:46 -07002585 { INT_RANGE(ignore_broadcast_ssid, 0, 2) },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002586#ifdef CONFIG_P2P
Dmitry Shmidt54605472013-11-08 11:10:19 -08002587 { FUNC(go_p2p_dev_addr) },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002588 { FUNC(p2p_client_list) },
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002589 { FUNC(psk_list) },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002590#endif /* CONFIG_P2P */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002591#ifdef CONFIG_HT_OVERRIDES
2592 { INT_RANGE(disable_ht, 0, 1) },
2593 { INT_RANGE(disable_ht40, -1, 1) },
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002594 { INT_RANGE(disable_sgi, 0, 1) },
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002595 { INT_RANGE(disable_ldpc, 0, 1) },
Dmitry Shmidt61593f02014-04-21 16:27:35 -07002596 { INT_RANGE(ht40_intolerant, 0, 1) },
Hai Shalom74f70d42019-02-11 14:42:39 -08002597 { INT_RANGE(tx_stbc, -1, 1) },
2598 { INT_RANGE(rx_stbc, -1, 3) },
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002599 { INT_RANGE(disable_max_amsdu, -1, 1) },
2600 { INT_RANGE(ampdu_factor, -1, 3) },
2601 { INT_RANGE(ampdu_density, -1, 7) },
2602 { STR(ht_mcs) },
2603#endif /* CONFIG_HT_OVERRIDES */
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002604#ifdef CONFIG_VHT_OVERRIDES
2605 { INT_RANGE(disable_vht, 0, 1) },
2606 { INT(vht_capa) },
2607 { INT(vht_capa_mask) },
2608 { INT_RANGE(vht_rx_mcs_nss_1, -1, 3) },
2609 { INT_RANGE(vht_rx_mcs_nss_2, -1, 3) },
2610 { INT_RANGE(vht_rx_mcs_nss_3, -1, 3) },
2611 { INT_RANGE(vht_rx_mcs_nss_4, -1, 3) },
2612 { INT_RANGE(vht_rx_mcs_nss_5, -1, 3) },
2613 { INT_RANGE(vht_rx_mcs_nss_6, -1, 3) },
2614 { INT_RANGE(vht_rx_mcs_nss_7, -1, 3) },
2615 { INT_RANGE(vht_rx_mcs_nss_8, -1, 3) },
2616 { INT_RANGE(vht_tx_mcs_nss_1, -1, 3) },
2617 { INT_RANGE(vht_tx_mcs_nss_2, -1, 3) },
2618 { INT_RANGE(vht_tx_mcs_nss_3, -1, 3) },
2619 { INT_RANGE(vht_tx_mcs_nss_4, -1, 3) },
2620 { INT_RANGE(vht_tx_mcs_nss_5, -1, 3) },
2621 { INT_RANGE(vht_tx_mcs_nss_6, -1, 3) },
2622 { INT_RANGE(vht_tx_mcs_nss_7, -1, 3) },
2623 { INT_RANGE(vht_tx_mcs_nss_8, -1, 3) },
2624#endif /* CONFIG_VHT_OVERRIDES */
Hai Shalomfdcde762020-04-02 11:19:20 -07002625#ifdef CONFIG_HE_OVERRIDES
2626 { INT_RANGE(disable_he, 0, 1)},
2627#endif /* CONFIG_HE_OVERRIDES */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002628 { INT(ap_max_inactivity) },
2629 { INT(dtim_period) },
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002630 { INT(beacon_int) },
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07002631#ifdef CONFIG_MACSEC
2632 { INT_RANGE(macsec_policy, 0, 1) },
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002633 { INT_RANGE(macsec_integ_only, 0, 1) },
Hai Shalom74f70d42019-02-11 14:42:39 -08002634 { INT_RANGE(macsec_replay_protect, 0, 1) },
2635 { INT(macsec_replay_window) },
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002636 { INT_RANGE(macsec_port, 1, 65534) },
Dmitry Shmidt29333592017-01-09 12:27:11 -08002637 { INT_RANGE(mka_priority, 0, 255) },
Sunil Ravia04bd252022-05-02 22:54:18 -07002638 { INT_RANGE(macsec_csindex, 0, 1) },
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08002639 { FUNC_KEY(mka_cak) },
2640 { FUNC_KEY(mka_ckn) },
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07002641#endif /* CONFIG_MACSEC */
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002642#ifdef CONFIG_HS20
2643 { INT(update_identifier) },
Roshan Pius3a1667e2018-07-03 15:17:14 -07002644 { STR_RANGE(roaming_consortium_selection, 0, MAX_ROAMING_CONS_OI_LEN) },
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002645#endif /* CONFIG_HS20 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002646 { INT_RANGE(mac_addr, 0, 2) },
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002647 { INT_RANGE(pbss, 0, 2) },
2648 { INT_RANGE(wps_disabled, 0, 1) },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002649 { INT_RANGE(fils_dh_group, 0, 65535) },
2650#ifdef CONFIG_DPP
2651 { STR(dpp_connector) },
2652 { STR_LEN(dpp_netaccesskey) },
2653 { INT(dpp_netaccesskey_expiry) },
2654 { STR_LEN(dpp_csign) },
Hai Shalom899fcc72020-10-19 14:38:18 -07002655 { STR_LEN(dpp_pp_key) },
Hai Shalomfdcde762020-04-02 11:19:20 -07002656 { INT_RANGE(dpp_pfs, 0, 2) },
Sunil Ravi89eba102022-09-13 21:04:37 -07002657 { INT_RANGE(dpp_connector_privacy, 0, 1) },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002658#endif /* CONFIG_DPP */
2659 { INT_RANGE(owe_group, 0, 65535) },
Roshan Pius3a1667e2018-07-03 15:17:14 -07002660 { INT_RANGE(owe_only, 0, 1) },
Hai Shalomfdcde762020-04-02 11:19:20 -07002661 { INT_RANGE(owe_ptk_workaround, 0, 1) },
Hai Shalom74f70d42019-02-11 14:42:39 -08002662 { INT_RANGE(multi_ap_backhaul_sta, 0, 1) },
Hai Shalom81f62d82019-07-22 12:10:00 -07002663 { INT_RANGE(ft_eap_pmksa_caching, 0, 1) },
Hai Shalomfdcde762020-04-02 11:19:20 -07002664 { INT_RANGE(beacon_prot, 0, 1) },
2665 { INT_RANGE(transition_disable, 0, 255) },
Hai Shalom899fcc72020-10-19 14:38:18 -07002666 { INT_RANGE(sae_pk, 0, 2) },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002667};
2668
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002669#undef OFFSET
2670#undef _STR
2671#undef STR
2672#undef STR_KEY
2673#undef _STR_LEN
2674#undef STR_LEN
2675#undef STR_LEN_KEY
2676#undef _STR_RANGE
2677#undef STR_RANGE
2678#undef STR_RANGE_KEY
2679#undef _INT
2680#undef INT
2681#undef INT_RANGE
2682#undef _FUNC
2683#undef FUNC
2684#undef FUNC_KEY
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002685#define NUM_SSID_FIELDS ARRAY_SIZE(ssid_fields)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002686
2687
2688/**
2689 * wpa_config_add_prio_network - Add a network to priority lists
2690 * @config: Configuration data from wpa_config_read()
2691 * @ssid: Pointer to the network configuration to be added to the list
2692 * Returns: 0 on success, -1 on failure
2693 *
2694 * This function is used to add a network block to the priority list of
2695 * networks. This must be called for each network when reading in the full
2696 * configuration. In addition, this can be used indirectly when updating
2697 * priorities by calling wpa_config_update_prio_list().
2698 */
2699int wpa_config_add_prio_network(struct wpa_config *config,
2700 struct wpa_ssid *ssid)
2701{
Hai Shalomfdcde762020-04-02 11:19:20 -07002702 size_t prio;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002703 struct wpa_ssid *prev, **nlist;
2704
2705 /*
2706 * Add to an existing priority list if one is available for the
2707 * configured priority level for this network.
2708 */
2709 for (prio = 0; prio < config->num_prio; prio++) {
2710 prev = config->pssid[prio];
2711 if (prev->priority == ssid->priority) {
2712 while (prev->pnext)
2713 prev = prev->pnext;
2714 prev->pnext = ssid;
2715 return 0;
2716 }
2717 }
2718
2719 /* First network for this priority - add a new priority list */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002720 nlist = os_realloc_array(config->pssid, config->num_prio + 1,
2721 sizeof(struct wpa_ssid *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002722 if (nlist == NULL)
2723 return -1;
2724
2725 for (prio = 0; prio < config->num_prio; prio++) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002726 if (nlist[prio]->priority < ssid->priority) {
2727 os_memmove(&nlist[prio + 1], &nlist[prio],
2728 (config->num_prio - prio) *
2729 sizeof(struct wpa_ssid *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002730 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002731 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002732 }
2733
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002734 nlist[prio] = ssid;
2735 config->num_prio++;
2736 config->pssid = nlist;
2737
2738 return 0;
2739}
2740
2741
2742/**
2743 * wpa_config_update_prio_list - Update network priority list
2744 * @config: Configuration data from wpa_config_read()
2745 * Returns: 0 on success, -1 on failure
2746 *
2747 * This function is called to update the priority list of networks in the
2748 * configuration when a network is being added or removed. This is also called
2749 * if a priority for a network is changed.
2750 */
2751int wpa_config_update_prio_list(struct wpa_config *config)
2752{
2753 struct wpa_ssid *ssid;
2754 int ret = 0;
2755
2756 os_free(config->pssid);
2757 config->pssid = NULL;
2758 config->num_prio = 0;
2759
2760 ssid = config->ssid;
2761 while (ssid) {
2762 ssid->pnext = NULL;
2763 if (wpa_config_add_prio_network(config, ssid) < 0)
2764 ret = -1;
2765 ssid = ssid->next;
2766 }
2767
2768 return ret;
2769}
2770
2771
2772#ifdef IEEE8021X_EAPOL
Hai Shalomc3565922019-10-28 11:58:20 -07002773
2774static void eap_peer_config_free_cert(struct eap_peer_cert_config *cert)
2775{
2776 os_free(cert->ca_cert);
2777 os_free(cert->ca_path);
2778 os_free(cert->client_cert);
2779 os_free(cert->private_key);
2780 str_clear_free(cert->private_key_passwd);
Hai Shalomc3565922019-10-28 11:58:20 -07002781 os_free(cert->subject_match);
2782 os_free(cert->check_cert_subject);
2783 os_free(cert->altsubject_match);
2784 os_free(cert->domain_suffix_match);
2785 os_free(cert->domain_match);
2786 str_clear_free(cert->pin);
2787 os_free(cert->engine_id);
2788 os_free(cert->key_id);
2789 os_free(cert->cert_id);
2790 os_free(cert->ca_cert_id);
2791}
2792
2793
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002794static void eap_peer_config_free(struct eap_peer_config *eap)
2795{
2796 os_free(eap->eap_methods);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002797 bin_clear_free(eap->identity, eap->identity_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002798 os_free(eap->anonymous_identity);
Jouni Malinena3cb6f02017-12-08 17:05:40 +02002799 os_free(eap->imsi_identity);
Sunil8cd6f4d2022-06-28 18:40:46 +00002800 os_free(eap->imsi_privacy_cert);
2801 os_free(eap->imsi_privacy_attr);
Hai Shalomc3565922019-10-28 11:58:20 -07002802 os_free(eap->machine_identity);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002803 bin_clear_free(eap->password, eap->password_len);
Hai Shalomc3565922019-10-28 11:58:20 -07002804 bin_clear_free(eap->machine_password, eap->machine_password_len);
2805 eap_peer_config_free_cert(&eap->cert);
2806 eap_peer_config_free_cert(&eap->phase2_cert);
2807 eap_peer_config_free_cert(&eap->machine_cert);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002808 os_free(eap->phase1);
2809 os_free(eap->phase2);
Hai Shalomc3565922019-10-28 11:58:20 -07002810 os_free(eap->machine_phase2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002811 os_free(eap->pcsc);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002812 os_free(eap->otp);
2813 os_free(eap->pending_req_otp);
2814 os_free(eap->pac_file);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002815 bin_clear_free(eap->new_password, eap->new_password_len);
2816 str_clear_free(eap->external_sim_resp);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002817 os_free(eap->openssl_ciphers);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002818}
Hai Shalomc3565922019-10-28 11:58:20 -07002819
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002820#endif /* IEEE8021X_EAPOL */
2821
2822
2823/**
2824 * wpa_config_free_ssid - Free network/ssid configuration data
2825 * @ssid: Configuration data for the network
2826 *
2827 * This function frees all resources allocated for the network configuration
2828 * data.
2829 */
2830void wpa_config_free_ssid(struct wpa_ssid *ssid)
2831{
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002832 struct psk_list_entry *psk;
2833
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002834 os_free(ssid->ssid);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002835 str_clear_free(ssid->passphrase);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002836 os_free(ssid->ext_psk);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002837 str_clear_free(ssid->sae_password);
Roshan Pius3a1667e2018-07-03 15:17:14 -07002838 os_free(ssid->sae_password_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002839#ifdef IEEE8021X_EAPOL
2840 eap_peer_config_free(&ssid->eap);
2841#endif /* IEEE8021X_EAPOL */
2842 os_free(ssid->id_str);
2843 os_free(ssid->scan_freq);
2844 os_free(ssid->freq_list);
2845 os_free(ssid->bgscan);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002846 os_free(ssid->p2p_client_list);
Hai Shalom60840252021-02-19 19:02:11 -08002847 os_free(ssid->bssid_ignore);
2848 os_free(ssid->bssid_accept);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002849#ifdef CONFIG_HT_OVERRIDES
2850 os_free(ssid->ht_mcs);
2851#endif /* CONFIG_HT_OVERRIDES */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002852#ifdef CONFIG_MESH
2853 os_free(ssid->mesh_basic_rates);
2854#endif /* CONFIG_MESH */
Roshan Pius3a1667e2018-07-03 15:17:14 -07002855#ifdef CONFIG_HS20
2856 os_free(ssid->roaming_consortium_selection);
2857#endif /* CONFIG_HS20 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002858 os_free(ssid->dpp_connector);
2859 bin_clear_free(ssid->dpp_netaccesskey, ssid->dpp_netaccesskey_len);
2860 os_free(ssid->dpp_csign);
Hai Shalom899fcc72020-10-19 14:38:18 -07002861 os_free(ssid->dpp_pp_key);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002862 while ((psk = dl_list_first(&ssid->psk_list, struct psk_list_entry,
2863 list))) {
2864 dl_list_del(&psk->list);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002865 bin_clear_free(psk, sizeof(*psk));
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002866 }
Hai Shalomc3565922019-10-28 11:58:20 -07002867#ifdef CONFIG_SAE
2868 sae_deinit_pt(ssid->pt);
2869#endif /* CONFIG_SAE */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002870 bin_clear_free(ssid, sizeof(*ssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002871}
2872
2873
Dmitry Shmidt04949592012-07-19 12:16:46 -07002874void wpa_config_free_cred(struct wpa_cred *cred)
2875{
Dmitry Shmidt051af732013-10-22 13:52:46 -07002876 size_t i;
2877
Dmitry Shmidt04949592012-07-19 12:16:46 -07002878 os_free(cred->realm);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002879 str_clear_free(cred->username);
2880 str_clear_free(cred->password);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002881 os_free(cred->ca_cert);
2882 os_free(cred->client_cert);
2883 os_free(cred->private_key);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002884 str_clear_free(cred->private_key_passwd);
Hai Shaloma20dcd72022-02-04 13:43:00 -08002885 os_free(cred->engine_id);
2886 os_free(cred->ca_cert_id);
2887 os_free(cred->cert_id);
2888 os_free(cred->key_id);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002889 os_free(cred->imsi);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002890 str_clear_free(cred->milenage);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002891 for (i = 0; i < cred->num_domain; i++)
2892 os_free(cred->domain[i]);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002893 os_free(cred->domain);
Dmitry Shmidt051af732013-10-22 13:52:46 -07002894 os_free(cred->domain_suffix_match);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002895 os_free(cred->eap_method);
2896 os_free(cred->phase1);
2897 os_free(cred->phase2);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002898 os_free(cred->excluded_ssid);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002899 os_free(cred->roaming_partner);
2900 os_free(cred->provisioning_sp);
2901 for (i = 0; i < cred->num_req_conn_capab; i++)
2902 os_free(cred->req_conn_capab_port[i]);
2903 os_free(cred->req_conn_capab_port);
2904 os_free(cred->req_conn_capab_proto);
Sunil8cd6f4d2022-06-28 18:40:46 +00002905 os_free(cred->imsi_privacy_cert);
2906 os_free(cred->imsi_privacy_attr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002907 os_free(cred);
2908}
2909
2910
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002911void wpa_config_flush_blobs(struct wpa_config *config)
2912{
2913#ifndef CONFIG_NO_CONFIG_BLOBS
2914 struct wpa_config_blob *blob, *prev;
2915
2916 blob = config->blobs;
2917 config->blobs = NULL;
2918 while (blob) {
2919 prev = blob;
2920 blob = blob->next;
2921 wpa_config_free_blob(prev);
2922 }
2923#endif /* CONFIG_NO_CONFIG_BLOBS */
2924}
2925
2926
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002927/**
2928 * wpa_config_free - Free configuration data
2929 * @config: Configuration data from wpa_config_read()
2930 *
2931 * This function frees all resources allocated for the configuration data by
2932 * wpa_config_read().
2933 */
2934void wpa_config_free(struct wpa_config *config)
2935{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002936 struct wpa_ssid *ssid, *prev = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002937 struct wpa_cred *cred, *cprev;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002938 int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002939
2940 ssid = config->ssid;
2941 while (ssid) {
2942 prev = ssid;
2943 ssid = ssid->next;
2944 wpa_config_free_ssid(prev);
2945 }
2946
Dmitry Shmidt04949592012-07-19 12:16:46 -07002947 cred = config->cred;
2948 while (cred) {
2949 cprev = cred;
2950 cred = cred->next;
2951 wpa_config_free_cred(cprev);
2952 }
2953
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002954 wpa_config_flush_blobs(config);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002955
Dmitry Shmidt04949592012-07-19 12:16:46 -07002956 wpabuf_free(config->wps_vendor_ext_m1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002957 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++)
2958 wpabuf_free(config->wps_vendor_ext[i]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002959 os_free(config->ctrl_interface);
2960 os_free(config->ctrl_interface_group);
2961 os_free(config->opensc_engine_path);
2962 os_free(config->pkcs11_engine_path);
2963 os_free(config->pkcs11_module_path);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002964 os_free(config->openssl_ciphers);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002965 os_free(config->pcsc_reader);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002966 str_clear_free(config->pcsc_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002967 os_free(config->driver_param);
2968 os_free(config->device_name);
2969 os_free(config->manufacturer);
2970 os_free(config->model_name);
2971 os_free(config->model_number);
2972 os_free(config->serial_number);
2973 os_free(config->config_methods);
2974 os_free(config->p2p_ssid_postfix);
2975 os_free(config->pssid);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002976 os_free(config->p2p_pref_chan);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002977 os_free(config->p2p_no_go_freq.range);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002978 os_free(config->autoscan);
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07002979 os_free(config->freq_list);
Hai Shalom60840252021-02-19 19:02:11 -08002980 os_free(config->initial_freq_list);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002981 wpabuf_free(config->wps_nfc_dh_pubkey);
2982 wpabuf_free(config->wps_nfc_dh_privkey);
2983 wpabuf_free(config->wps_nfc_dev_pw);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002984 os_free(config->ext_password_backend);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002985 os_free(config->sae_groups);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07002986 wpabuf_free(config->ap_vendor_elements);
Hai Shaloma20dcd72022-02-04 13:43:00 -08002987 wpabuf_free(config->ap_assocresp_elements);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002988 os_free(config->osu_dir);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002989 os_free(config->bgscan);
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07002990 os_free(config->wowlan_triggers);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002991 os_free(config->fst_group_id);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002992 os_free(config->sched_scan_plans);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002993#ifdef CONFIG_MBO
2994 os_free(config->non_pref_chan);
2995#endif /* CONFIG_MBO */
Hai Shalomc3565922019-10-28 11:58:20 -07002996 os_free(config->dpp_name);
2997 os_free(config->dpp_mud_url);
Sunil Ravi89eba102022-09-13 21:04:37 -07002998 os_free(config->dpp_extra_conf_req_name);
2999 os_free(config->dpp_extra_conf_req_value);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003000
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003001 os_free(config);
3002}
3003
3004
3005/**
3006 * wpa_config_foreach_network - Iterate over each configured network
3007 * @config: Configuration data from wpa_config_read()
3008 * @func: Callback function to process each network
3009 * @arg: Opaque argument to pass to callback function
3010 *
3011 * Iterate over the set of configured networks calling the specified
3012 * function for each item. We guard against callbacks removing the
3013 * supplied network.
3014 */
3015void wpa_config_foreach_network(struct wpa_config *config,
3016 void (*func)(void *, struct wpa_ssid *),
3017 void *arg)
3018{
3019 struct wpa_ssid *ssid, *next;
3020
3021 ssid = config->ssid;
3022 while (ssid) {
3023 next = ssid->next;
3024 func(arg, ssid);
3025 ssid = next;
3026 }
3027}
3028
3029
3030/**
3031 * wpa_config_get_network - Get configured network based on id
3032 * @config: Configuration data from wpa_config_read()
3033 * @id: Unique network id to search for
3034 * Returns: Network configuration or %NULL if not found
3035 */
3036struct wpa_ssid * wpa_config_get_network(struct wpa_config *config, int id)
3037{
3038 struct wpa_ssid *ssid;
3039
3040 ssid = config->ssid;
3041 while (ssid) {
3042 if (id == ssid->id)
3043 break;
3044 ssid = ssid->next;
3045 }
3046
3047 return ssid;
3048}
3049
3050
3051/**
3052 * wpa_config_add_network - Add a new network with empty configuration
3053 * @config: Configuration data from wpa_config_read()
3054 * Returns: The new network configuration or %NULL if operation failed
3055 */
3056struct wpa_ssid * wpa_config_add_network(struct wpa_config *config)
3057{
3058 int id;
3059 struct wpa_ssid *ssid, *last = NULL;
3060
3061 id = -1;
3062 ssid = config->ssid;
3063 while (ssid) {
3064 if (ssid->id > id)
3065 id = ssid->id;
3066 last = ssid;
3067 ssid = ssid->next;
3068 }
3069 id++;
3070
3071 ssid = os_zalloc(sizeof(*ssid));
3072 if (ssid == NULL)
3073 return NULL;
3074 ssid->id = id;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003075 dl_list_init(&ssid->psk_list);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003076 if (last)
3077 last->next = ssid;
3078 else
3079 config->ssid = ssid;
3080
3081 wpa_config_update_prio_list(config);
3082
3083 return ssid;
3084}
3085
3086
3087/**
3088 * wpa_config_remove_network - Remove a configured network based on id
3089 * @config: Configuration data from wpa_config_read()
3090 * @id: Unique network id to search for
3091 * Returns: 0 on success, or -1 if the network was not found
3092 */
3093int wpa_config_remove_network(struct wpa_config *config, int id)
3094{
3095 struct wpa_ssid *ssid, *prev = NULL;
3096
3097 ssid = config->ssid;
3098 while (ssid) {
3099 if (id == ssid->id)
3100 break;
3101 prev = ssid;
3102 ssid = ssid->next;
3103 }
3104
3105 if (ssid == NULL)
3106 return -1;
3107
3108 if (prev)
3109 prev->next = ssid->next;
3110 else
3111 config->ssid = ssid->next;
3112
3113 wpa_config_update_prio_list(config);
3114 wpa_config_free_ssid(ssid);
3115 return 0;
3116}
3117
3118
3119/**
3120 * wpa_config_set_network_defaults - Set network default values
3121 * @ssid: Pointer to network configuration data
3122 */
3123void wpa_config_set_network_defaults(struct wpa_ssid *ssid)
3124{
3125 ssid->proto = DEFAULT_PROTO;
3126 ssid->pairwise_cipher = DEFAULT_PAIRWISE;
3127 ssid->group_cipher = DEFAULT_GROUP;
3128 ssid->key_mgmt = DEFAULT_KEY_MGMT;
Hai Shalomfdcde762020-04-02 11:19:20 -07003129 ssid->wpa_deny_ptk0_rekey = PTK0_REKEY_ALLOW_ALWAYS;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003130 ssid->bg_scan_period = DEFAULT_BG_SCAN_PERIOD;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003131 ssid->ht = 1;
Hai Shalom899fcc72020-10-19 14:38:18 -07003132 ssid->vht = 1;
3133 ssid->he = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003134#ifdef IEEE8021X_EAPOL
3135 ssid->eapol_flags = DEFAULT_EAPOL_FLAGS;
3136 ssid->eap_workaround = DEFAULT_EAP_WORKAROUND;
3137 ssid->eap.fragment_size = DEFAULT_FRAGMENT_SIZE;
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -07003138 ssid->eap.sim_num = DEFAULT_USER_SELECTED_SIM;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003139#endif /* IEEE8021X_EAPOL */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003140#ifdef CONFIG_MESH
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003141 ssid->dot11MeshMaxRetries = DEFAULT_MESH_MAX_RETRIES;
3142 ssid->dot11MeshRetryTimeout = DEFAULT_MESH_RETRY_TIMEOUT;
3143 ssid->dot11MeshConfirmTimeout = DEFAULT_MESH_CONFIRM_TIMEOUT;
3144 ssid->dot11MeshHoldingTimeout = DEFAULT_MESH_HOLDING_TIMEOUT;
Hai Shaloma20dcd72022-02-04 13:43:00 -08003145 ssid->mesh_fwding = DEFAULT_MESH_FWDING;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003146 ssid->mesh_rssi_threshold = DEFAULT_MESH_RSSI_THRESHOLD;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003147#endif /* CONFIG_MESH */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003148#ifdef CONFIG_HT_OVERRIDES
3149 ssid->disable_ht = DEFAULT_DISABLE_HT;
3150 ssid->disable_ht40 = DEFAULT_DISABLE_HT40;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003151 ssid->disable_sgi = DEFAULT_DISABLE_SGI;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07003152 ssid->disable_ldpc = DEFAULT_DISABLE_LDPC;
Hai Shalom74f70d42019-02-11 14:42:39 -08003153 ssid->tx_stbc = DEFAULT_TX_STBC;
3154 ssid->rx_stbc = DEFAULT_RX_STBC;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003155 ssid->disable_max_amsdu = DEFAULT_DISABLE_MAX_AMSDU;
3156 ssid->ampdu_factor = DEFAULT_AMPDU_FACTOR;
3157 ssid->ampdu_density = DEFAULT_AMPDU_DENSITY;
3158#endif /* CONFIG_HT_OVERRIDES */
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003159#ifdef CONFIG_VHT_OVERRIDES
3160 ssid->vht_rx_mcs_nss_1 = -1;
3161 ssid->vht_rx_mcs_nss_2 = -1;
3162 ssid->vht_rx_mcs_nss_3 = -1;
3163 ssid->vht_rx_mcs_nss_4 = -1;
3164 ssid->vht_rx_mcs_nss_5 = -1;
3165 ssid->vht_rx_mcs_nss_6 = -1;
3166 ssid->vht_rx_mcs_nss_7 = -1;
3167 ssid->vht_rx_mcs_nss_8 = -1;
3168 ssid->vht_tx_mcs_nss_1 = -1;
3169 ssid->vht_tx_mcs_nss_2 = -1;
3170 ssid->vht_tx_mcs_nss_3 = -1;
3171 ssid->vht_tx_mcs_nss_4 = -1;
3172 ssid->vht_tx_mcs_nss_5 = -1;
3173 ssid->vht_tx_mcs_nss_6 = -1;
3174 ssid->vht_tx_mcs_nss_7 = -1;
3175 ssid->vht_tx_mcs_nss_8 = -1;
3176#endif /* CONFIG_VHT_OVERRIDES */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003177 ssid->proactive_key_caching = -1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003178 ssid->ieee80211w = MGMT_FRAME_PROTECTION_DEFAULT;
Hai Shaloma20dcd72022-02-04 13:43:00 -08003179 ssid->sae_pwe = DEFAULT_SAE_PWE;
Dmitry Shmidt29333592017-01-09 12:27:11 -08003180#ifdef CONFIG_MACSEC
3181 ssid->mka_priority = DEFAULT_PRIO_NOT_KEY_SERVER;
3182#endif /* CONFIG_MACSEC */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003183 ssid->mac_addr = -1;
Hai Shalom74f70d42019-02-11 14:42:39 -08003184 ssid->max_oper_chwidth = DEFAULT_MAX_OPER_CHWIDTH;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003185}
3186
3187
Sunil Ravia04bd252022-05-02 22:54:18 -07003188static const char *removed_fields[] = {
3189 "dh_file",
3190 "dh_file2",
3191 "machine_dh_file",
3192 NULL
3193};
3194
3195static bool removed_field(const char *field)
3196{
3197 int i;
3198
3199 for (i = 0; removed_fields[i]; i++) {
3200 if (os_strcmp(field, removed_fields[i]) == 0)
3201 return true;
3202 }
3203
3204 return false;
3205}
3206
3207
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003208/**
3209 * wpa_config_set - Set a variable in network configuration
3210 * @ssid: Pointer to network configuration data
3211 * @var: Variable name, e.g., "ssid"
3212 * @value: Variable value
3213 * @line: Line number in configuration file or 0 if not used
Dmitry Shmidte4663042016-04-04 10:07:49 -07003214 * Returns: 0 on success with possible change in the value, 1 on success with
3215 * no change to previously configured value, or -1 on failure
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003216 *
3217 * This function can be used to set network configuration variables based on
3218 * both the configuration file and management interface input. The value
3219 * parameter must be in the same format as the text-based configuration file is
3220 * using. For example, strings are using double quotation marks.
3221 */
3222int wpa_config_set(struct wpa_ssid *ssid, const char *var, const char *value,
3223 int line)
3224{
3225 size_t i;
3226 int ret = 0;
3227
3228 if (ssid == NULL || var == NULL || value == NULL)
3229 return -1;
3230
3231 for (i = 0; i < NUM_SSID_FIELDS; i++) {
3232 const struct parse_data *field = &ssid_fields[i];
3233 if (os_strcmp(var, field->name) != 0)
3234 continue;
3235
Dmitry Shmidte4663042016-04-04 10:07:49 -07003236 ret = field->parser(field, ssid, line, value);
3237 if (ret < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003238 if (line) {
3239 wpa_printf(MSG_ERROR, "Line %d: failed to "
3240 "parse %s '%s'.", line, var, value);
3241 }
3242 ret = -1;
3243 }
Hai Shalomc3565922019-10-28 11:58:20 -07003244#ifdef CONFIG_SAE
3245 if (os_strcmp(var, "ssid") == 0 ||
3246 os_strcmp(var, "psk") == 0 ||
3247 os_strcmp(var, "sae_password") == 0 ||
3248 os_strcmp(var, "sae_password_id") == 0) {
3249 sae_deinit_pt(ssid->pt);
3250 ssid->pt = NULL;
3251 }
3252#endif /* CONFIG_SAE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003253 break;
3254 }
3255 if (i == NUM_SSID_FIELDS) {
Sunil Ravia04bd252022-05-02 22:54:18 -07003256 if (removed_field(var)) {
3257 wpa_printf(MSG_INFO,
3258 "Line %d: Ignore removed configuration field '%s'",
3259 line, var);
3260 return ret;
3261 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003262 if (line) {
3263 wpa_printf(MSG_ERROR, "Line %d: unknown network field "
3264 "'%s'.", line, var);
3265 }
3266 ret = -1;
3267 }
Hai Shalom899fcc72020-10-19 14:38:18 -07003268 ssid->was_recently_reconfigured = true;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003269
3270 return ret;
3271}
3272
3273
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003274int wpa_config_set_quoted(struct wpa_ssid *ssid, const char *var,
3275 const char *value)
3276{
3277 size_t len;
3278 char *buf;
3279 int ret;
3280
3281 len = os_strlen(value);
3282 buf = os_malloc(len + 3);
3283 if (buf == NULL)
3284 return -1;
3285 buf[0] = '"';
3286 os_memcpy(buf + 1, value, len);
3287 buf[len + 1] = '"';
3288 buf[len + 2] = '\0';
3289 ret = wpa_config_set(ssid, var, buf, 0);
3290 os_free(buf);
3291 return ret;
3292}
3293
3294
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003295/**
3296 * wpa_config_get_all - Get all options from network configuration
3297 * @ssid: Pointer to network configuration data
3298 * @get_keys: Determines if keys/passwords will be included in returned list
3299 * (if they may be exported)
3300 * Returns: %NULL terminated list of all set keys and their values in the form
3301 * of [key1, val1, key2, val2, ... , NULL]
3302 *
3303 * This function can be used to get list of all configured network properties.
3304 * The caller is responsible for freeing the returned list and all its
3305 * elements.
3306 */
3307char ** wpa_config_get_all(struct wpa_ssid *ssid, int get_keys)
3308{
Dmitry Shmidt83474442015-04-15 13:47:09 -07003309#ifdef NO_CONFIG_WRITE
3310 return NULL;
3311#else /* NO_CONFIG_WRITE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003312 const struct parse_data *field;
3313 char *key, *value;
3314 size_t i;
3315 char **props;
3316 int fields_num;
3317
3318 get_keys = get_keys && ssid->export_keys;
3319
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003320 props = os_calloc(2 * NUM_SSID_FIELDS + 1, sizeof(char *));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003321 if (!props)
3322 return NULL;
3323
3324 fields_num = 0;
3325 for (i = 0; i < NUM_SSID_FIELDS; i++) {
3326 field = &ssid_fields[i];
3327 if (field->key_data && !get_keys)
3328 continue;
3329 value = field->writer(field, ssid);
3330 if (value == NULL)
3331 continue;
3332 if (os_strlen(value) == 0) {
3333 os_free(value);
3334 continue;
3335 }
3336
3337 key = os_strdup(field->name);
3338 if (key == NULL) {
3339 os_free(value);
3340 goto err;
3341 }
3342
3343 props[fields_num * 2] = key;
3344 props[fields_num * 2 + 1] = value;
3345
3346 fields_num++;
3347 }
3348
3349 return props;
3350
3351err:
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07003352 for (i = 0; props[i]; i++)
3353 os_free(props[i]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003354 os_free(props);
3355 return NULL;
Dmitry Shmidt83474442015-04-15 13:47:09 -07003356#endif /* NO_CONFIG_WRITE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003357}
3358
3359
3360#ifndef NO_CONFIG_WRITE
3361/**
3362 * wpa_config_get - Get a variable in network configuration
3363 * @ssid: Pointer to network configuration data
3364 * @var: Variable name, e.g., "ssid"
3365 * Returns: Value of the variable or %NULL on failure
3366 *
3367 * This function can be used to get network configuration variables. The
3368 * returned value is a copy of the configuration variable in text format, i.e,.
3369 * the same format that the text-based configuration file and wpa_config_set()
3370 * are using for the value. The caller is responsible for freeing the returned
3371 * value.
3372 */
3373char * wpa_config_get(struct wpa_ssid *ssid, const char *var)
3374{
3375 size_t i;
3376
3377 if (ssid == NULL || var == NULL)
3378 return NULL;
3379
3380 for (i = 0; i < NUM_SSID_FIELDS; i++) {
3381 const struct parse_data *field = &ssid_fields[i];
Paul Stewart85c72c62016-03-03 15:33:47 -08003382 if (os_strcmp(var, field->name) == 0) {
3383 char *ret = field->writer(field, ssid);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003384
3385 if (ret && has_newline(ret)) {
Paul Stewart35800752016-03-18 18:14:26 -07003386 wpa_printf(MSG_ERROR,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003387 "Found newline in value for %s; not returning it",
3388 var);
Paul Stewart85c72c62016-03-03 15:33:47 -08003389 os_free(ret);
3390 ret = NULL;
3391 }
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003392
Paul Stewart85c72c62016-03-03 15:33:47 -08003393 return ret;
3394 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003395 }
3396
3397 return NULL;
3398}
3399
3400
3401/**
3402 * wpa_config_get_no_key - Get a variable in network configuration (no keys)
3403 * @ssid: Pointer to network configuration data
3404 * @var: Variable name, e.g., "ssid"
3405 * Returns: Value of the variable or %NULL on failure
3406 *
3407 * This function can be used to get network configuration variable like
3408 * wpa_config_get(). The only difference is that this functions does not expose
3409 * key/password material from the configuration. In case a key/password field
3410 * is requested, the returned value is an empty string or %NULL if the variable
3411 * is not set or "*" if the variable is set (regardless of its value). The
3412 * returned value is a copy of the configuration variable in text format, i.e,.
3413 * the same format that the text-based configuration file and wpa_config_set()
3414 * are using for the value. The caller is responsible for freeing the returned
3415 * value.
3416 */
3417char * wpa_config_get_no_key(struct wpa_ssid *ssid, const char *var)
3418{
3419 size_t i;
3420
3421 if (ssid == NULL || var == NULL)
3422 return NULL;
3423
3424 for (i = 0; i < NUM_SSID_FIELDS; i++) {
3425 const struct parse_data *field = &ssid_fields[i];
3426 if (os_strcmp(var, field->name) == 0) {
3427 char *res = field->writer(field, ssid);
3428 if (field->key_data) {
3429 if (res && res[0]) {
3430 wpa_printf(MSG_DEBUG, "Do not allow "
3431 "key_data field to be "
3432 "exposed");
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003433 str_clear_free(res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003434 return os_strdup("*");
3435 }
3436
3437 os_free(res);
3438 return NULL;
3439 }
3440 return res;
3441 }
3442 }
3443
3444 return NULL;
3445}
3446#endif /* NO_CONFIG_WRITE */
3447
3448
3449/**
3450 * wpa_config_update_psk - Update WPA PSK based on passphrase and SSID
3451 * @ssid: Pointer to network configuration data
3452 *
3453 * This function must be called to update WPA PSK when either SSID or the
3454 * passphrase has changed for the network configuration.
3455 */
3456void wpa_config_update_psk(struct wpa_ssid *ssid)
3457{
3458#ifndef CONFIG_NO_PBKDF2
Sunil Ravia04bd252022-05-02 22:54:18 -07003459 if (pbkdf2_sha1(ssid->passphrase, ssid->ssid, ssid->ssid_len, 4096,
3460 ssid->psk, PMK_LEN) != 0) {
3461 wpa_printf(MSG_ERROR, "Error in pbkdf2_sha1()");
3462 return;
3463 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003464 wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
3465 ssid->psk, PMK_LEN);
3466 ssid->psk_set = 1;
3467#endif /* CONFIG_NO_PBKDF2 */
3468}
3469
3470
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003471static int wpa_config_set_cred_req_conn_capab(struct wpa_cred *cred,
3472 const char *value)
3473{
3474 u8 *proto;
3475 int **port;
3476 int *ports, *nports;
3477 const char *pos;
3478 unsigned int num_ports;
3479
3480 proto = os_realloc_array(cred->req_conn_capab_proto,
3481 cred->num_req_conn_capab + 1, sizeof(u8));
3482 if (proto == NULL)
3483 return -1;
3484 cred->req_conn_capab_proto = proto;
3485
3486 port = os_realloc_array(cred->req_conn_capab_port,
3487 cred->num_req_conn_capab + 1, sizeof(int *));
3488 if (port == NULL)
3489 return -1;
3490 cred->req_conn_capab_port = port;
3491
3492 proto[cred->num_req_conn_capab] = atoi(value);
3493
3494 pos = os_strchr(value, ':');
3495 if (pos == NULL) {
3496 port[cred->num_req_conn_capab] = NULL;
3497 cred->num_req_conn_capab++;
3498 return 0;
3499 }
3500 pos++;
3501
3502 ports = NULL;
3503 num_ports = 0;
3504
3505 while (*pos) {
3506 nports = os_realloc_array(ports, num_ports + 1, sizeof(int));
3507 if (nports == NULL) {
3508 os_free(ports);
3509 return -1;
3510 }
3511 ports = nports;
3512 ports[num_ports++] = atoi(pos);
3513
3514 pos = os_strchr(pos, ',');
3515 if (pos == NULL)
3516 break;
3517 pos++;
3518 }
3519
3520 nports = os_realloc_array(ports, num_ports + 1, sizeof(int));
3521 if (nports == NULL) {
3522 os_free(ports);
3523 return -1;
3524 }
3525 ports = nports;
3526 ports[num_ports] = -1;
3527
3528 port[cred->num_req_conn_capab] = ports;
3529 cred->num_req_conn_capab++;
3530 return 0;
3531}
3532
3533
Roshan Pius3a1667e2018-07-03 15:17:14 -07003534static int wpa_config_set_cred_roaming_consortiums(struct wpa_cred *cred,
3535 const char *value)
3536{
3537 u8 roaming_consortiums[MAX_ROAMING_CONS][MAX_ROAMING_CONS_OI_LEN];
3538 size_t roaming_consortiums_len[MAX_ROAMING_CONS];
3539 unsigned int num_roaming_consortiums = 0;
3540 const char *pos, *end;
3541 size_t len;
3542
3543 os_memset(roaming_consortiums, 0, sizeof(roaming_consortiums));
3544 os_memset(roaming_consortiums_len, 0, sizeof(roaming_consortiums_len));
3545
3546 for (pos = value;;) {
3547 end = os_strchr(pos, ',');
3548 len = end ? (size_t) (end - pos) : os_strlen(pos);
3549 if (!end && len == 0)
3550 break;
3551 if (len == 0 || (len & 1) != 0 ||
3552 len / 2 > MAX_ROAMING_CONS_OI_LEN ||
3553 hexstr2bin(pos,
3554 roaming_consortiums[num_roaming_consortiums],
3555 len / 2) < 0) {
3556 wpa_printf(MSG_INFO,
3557 "Invalid roaming_consortiums entry: %s",
3558 pos);
3559 return -1;
3560 }
3561 roaming_consortiums_len[num_roaming_consortiums] = len / 2;
3562 num_roaming_consortiums++;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08003563
3564 if (!end)
3565 break;
3566
3567 if (num_roaming_consortiums >= MAX_ROAMING_CONS) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07003568 wpa_printf(MSG_INFO,
3569 "Too many roaming_consortiums OIs");
3570 return -1;
3571 }
3572
Roshan Pius3a1667e2018-07-03 15:17:14 -07003573 pos = end + 1;
3574 }
3575
3576 os_memcpy(cred->roaming_consortiums, roaming_consortiums,
3577 sizeof(roaming_consortiums));
3578 os_memcpy(cred->roaming_consortiums_len, roaming_consortiums_len,
3579 sizeof(roaming_consortiums_len));
3580 cred->num_roaming_consortiums = num_roaming_consortiums;
3581
3582 return 0;
3583}
3584
3585
Dmitry Shmidt04949592012-07-19 12:16:46 -07003586int wpa_config_set_cred(struct wpa_cred *cred, const char *var,
3587 const char *value, int line)
3588{
3589 char *val;
3590 size_t len;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003591 int res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003592
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003593 if (os_strcmp(var, "temporary") == 0) {
3594 cred->temporary = atoi(value);
3595 return 0;
3596 }
3597
Dmitry Shmidt04949592012-07-19 12:16:46 -07003598 if (os_strcmp(var, "priority") == 0) {
3599 cred->priority = atoi(value);
3600 return 0;
3601 }
3602
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003603 if (os_strcmp(var, "sp_priority") == 0) {
3604 int prio = atoi(value);
3605 if (prio < 0 || prio > 255)
3606 return -1;
3607 cred->sp_priority = prio;
3608 return 0;
3609 }
3610
Dmitry Shmidt04949592012-07-19 12:16:46 -07003611 if (os_strcmp(var, "pcsc") == 0) {
3612 cred->pcsc = atoi(value);
3613 return 0;
3614 }
3615
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003616 if (os_strcmp(var, "eap") == 0) {
3617 struct eap_method_type method;
3618 method.method = eap_peer_get_type(value, &method.vendor);
3619 if (method.vendor == EAP_VENDOR_IETF &&
3620 method.method == EAP_TYPE_NONE) {
3621 wpa_printf(MSG_ERROR, "Line %d: unknown EAP type '%s' "
3622 "for a credential", line, value);
3623 return -1;
3624 }
3625 os_free(cred->eap_method);
3626 cred->eap_method = os_malloc(sizeof(*cred->eap_method));
3627 if (cred->eap_method == NULL)
3628 return -1;
3629 os_memcpy(cred->eap_method, &method, sizeof(method));
3630 return 0;
3631 }
3632
3633 if (os_strcmp(var, "password") == 0 &&
3634 os_strncmp(value, "ext:", 4) == 0) {
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003635 if (has_newline(value))
3636 return -1;
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003637 str_clear_free(cred->password);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003638 cred->password = os_strdup(value);
3639 cred->ext_password = 1;
3640 return 0;
3641 }
3642
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003643 if (os_strcmp(var, "update_identifier") == 0) {
3644 cred->update_identifier = atoi(value);
3645 return 0;
3646 }
3647
3648 if (os_strcmp(var, "min_dl_bandwidth_home") == 0) {
3649 cred->min_dl_bandwidth_home = atoi(value);
3650 return 0;
3651 }
3652
3653 if (os_strcmp(var, "min_ul_bandwidth_home") == 0) {
3654 cred->min_ul_bandwidth_home = atoi(value);
3655 return 0;
3656 }
3657
3658 if (os_strcmp(var, "min_dl_bandwidth_roaming") == 0) {
3659 cred->min_dl_bandwidth_roaming = atoi(value);
3660 return 0;
3661 }
3662
3663 if (os_strcmp(var, "min_ul_bandwidth_roaming") == 0) {
3664 cred->min_ul_bandwidth_roaming = atoi(value);
3665 return 0;
3666 }
3667
3668 if (os_strcmp(var, "max_bss_load") == 0) {
3669 cred->max_bss_load = atoi(value);
3670 return 0;
3671 }
3672
3673 if (os_strcmp(var, "req_conn_capab") == 0)
3674 return wpa_config_set_cred_req_conn_capab(cred, value);
3675
3676 if (os_strcmp(var, "ocsp") == 0) {
3677 cred->ocsp = atoi(value);
3678 return 0;
3679 }
3680
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -07003681 if (os_strcmp(var, "sim_num") == 0) {
3682 cred->sim_num = atoi(value);
3683 return 0;
3684 }
3685
Hai Shaloma20dcd72022-02-04 13:43:00 -08003686 if (os_strcmp(var, "engine") == 0) {
3687 cred->engine = atoi(value);
3688 return 0;
3689 }
3690
Dmitry Shmidt04949592012-07-19 12:16:46 -07003691 val = wpa_config_parse_string(value, &len);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003692 if (val == NULL ||
3693 (os_strcmp(var, "excluded_ssid") != 0 &&
3694 os_strcmp(var, "roaming_consortium") != 0 &&
3695 os_strcmp(var, "required_roaming_consortium") != 0 &&
3696 has_newline(val))) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07003697 wpa_printf(MSG_ERROR, "Line %d: invalid field '%s' string "
3698 "value '%s'.", line, var, value);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003699 os_free(val);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003700 return -1;
3701 }
3702
3703 if (os_strcmp(var, "realm") == 0) {
3704 os_free(cred->realm);
3705 cred->realm = val;
3706 return 0;
3707 }
3708
3709 if (os_strcmp(var, "username") == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003710 str_clear_free(cred->username);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003711 cred->username = val;
3712 return 0;
3713 }
3714
3715 if (os_strcmp(var, "password") == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003716 str_clear_free(cred->password);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003717 cred->password = val;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003718 cred->ext_password = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003719 return 0;
3720 }
3721
3722 if (os_strcmp(var, "ca_cert") == 0) {
3723 os_free(cred->ca_cert);
3724 cred->ca_cert = val;
3725 return 0;
3726 }
3727
3728 if (os_strcmp(var, "client_cert") == 0) {
3729 os_free(cred->client_cert);
3730 cred->client_cert = val;
3731 return 0;
3732 }
3733
3734 if (os_strcmp(var, "private_key") == 0) {
3735 os_free(cred->private_key);
3736 cred->private_key = val;
3737 return 0;
3738 }
3739
3740 if (os_strcmp(var, "private_key_passwd") == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003741 str_clear_free(cred->private_key_passwd);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003742 cred->private_key_passwd = val;
3743 return 0;
3744 }
3745
Hai Shaloma20dcd72022-02-04 13:43:00 -08003746 if (os_strcmp(var, "engine_id") == 0) {
3747 os_free(cred->engine_id);
3748 cred->engine_id = val;
3749 return 0;
3750 }
3751
3752 if (os_strcmp(var, "ca_cert_id") == 0) {
3753 os_free(cred->ca_cert_id);
3754 cred->ca_cert_id = val;
3755 return 0;
3756 }
3757
3758 if (os_strcmp(var, "cert_id") == 0) {
3759 os_free(cred->cert_id);
3760 cred->cert_id = val;
3761 return 0;
3762 }
3763
3764 if (os_strcmp(var, "key_id") == 0) {
3765 os_free(cred->key_id);
3766 cred->key_id = val;
3767 return 0;
3768 }
3769
Dmitry Shmidt04949592012-07-19 12:16:46 -07003770 if (os_strcmp(var, "imsi") == 0) {
3771 os_free(cred->imsi);
3772 cred->imsi = val;
3773 return 0;
3774 }
3775
3776 if (os_strcmp(var, "milenage") == 0) {
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003777 str_clear_free(cred->milenage);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003778 cred->milenage = val;
3779 return 0;
3780 }
3781
Dmitry Shmidt051af732013-10-22 13:52:46 -07003782 if (os_strcmp(var, "domain_suffix_match") == 0) {
3783 os_free(cred->domain_suffix_match);
3784 cred->domain_suffix_match = val;
3785 return 0;
3786 }
3787
Dmitry Shmidt04949592012-07-19 12:16:46 -07003788 if (os_strcmp(var, "domain") == 0) {
Dmitry Shmidt051af732013-10-22 13:52:46 -07003789 char **new_domain;
3790 new_domain = os_realloc_array(cred->domain,
3791 cred->num_domain + 1,
3792 sizeof(char *));
3793 if (new_domain == NULL) {
3794 os_free(val);
3795 return -1;
3796 }
3797 new_domain[cred->num_domain++] = val;
3798 cred->domain = new_domain;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003799 return 0;
3800 }
3801
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003802 if (os_strcmp(var, "phase1") == 0) {
3803 os_free(cred->phase1);
3804 cred->phase1 = val;
3805 return 0;
3806 }
3807
3808 if (os_strcmp(var, "phase2") == 0) {
3809 os_free(cred->phase2);
3810 cred->phase2 = val;
3811 return 0;
3812 }
3813
3814 if (os_strcmp(var, "roaming_consortium") == 0) {
3815 if (len < 3 || len > sizeof(cred->roaming_consortium)) {
3816 wpa_printf(MSG_ERROR, "Line %d: invalid "
3817 "roaming_consortium length %d (3..15 "
3818 "expected)", line, (int) len);
3819 os_free(val);
3820 return -1;
3821 }
3822 os_memcpy(cred->roaming_consortium, val, len);
3823 cred->roaming_consortium_len = len;
3824 os_free(val);
3825 return 0;
3826 }
3827
Dmitry Shmidt051af732013-10-22 13:52:46 -07003828 if (os_strcmp(var, "required_roaming_consortium") == 0) {
3829 if (len < 3 || len > sizeof(cred->required_roaming_consortium))
3830 {
3831 wpa_printf(MSG_ERROR, "Line %d: invalid "
3832 "required_roaming_consortium length %d "
3833 "(3..15 expected)", line, (int) len);
3834 os_free(val);
3835 return -1;
3836 }
3837 os_memcpy(cred->required_roaming_consortium, val, len);
3838 cred->required_roaming_consortium_len = len;
3839 os_free(val);
3840 return 0;
3841 }
3842
Roshan Pius3a1667e2018-07-03 15:17:14 -07003843 if (os_strcmp(var, "roaming_consortiums") == 0) {
3844 res = wpa_config_set_cred_roaming_consortiums(cred, val);
3845 if (res < 0)
3846 wpa_printf(MSG_ERROR,
3847 "Line %d: invalid roaming_consortiums",
3848 line);
3849 os_free(val);
3850 return res;
3851 }
3852
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003853 if (os_strcmp(var, "excluded_ssid") == 0) {
3854 struct excluded_ssid *e;
3855
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07003856 if (len > SSID_MAX_LEN) {
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003857 wpa_printf(MSG_ERROR, "Line %d: invalid "
3858 "excluded_ssid length %d", line, (int) len);
3859 os_free(val);
3860 return -1;
3861 }
3862
3863 e = os_realloc_array(cred->excluded_ssid,
3864 cred->num_excluded_ssid + 1,
3865 sizeof(struct excluded_ssid));
3866 if (e == NULL) {
3867 os_free(val);
3868 return -1;
3869 }
3870 cred->excluded_ssid = e;
3871
3872 e = &cred->excluded_ssid[cred->num_excluded_ssid++];
3873 os_memcpy(e->ssid, val, len);
3874 e->ssid_len = len;
3875
3876 os_free(val);
3877
3878 return 0;
3879 }
3880
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003881 if (os_strcmp(var, "roaming_partner") == 0) {
3882 struct roaming_partner *p;
3883 char *pos;
3884
3885 p = os_realloc_array(cred->roaming_partner,
3886 cred->num_roaming_partner + 1,
3887 sizeof(struct roaming_partner));
3888 if (p == NULL) {
3889 os_free(val);
3890 return -1;
3891 }
3892 cred->roaming_partner = p;
3893
3894 p = &cred->roaming_partner[cred->num_roaming_partner];
3895
3896 pos = os_strchr(val, ',');
3897 if (pos == NULL) {
3898 os_free(val);
3899 return -1;
3900 }
3901 *pos++ = '\0';
3902 if (pos - val - 1 >= (int) sizeof(p->fqdn)) {
3903 os_free(val);
3904 return -1;
3905 }
3906 os_memcpy(p->fqdn, val, pos - val);
3907
3908 p->exact_match = atoi(pos);
3909
3910 pos = os_strchr(pos, ',');
3911 if (pos == NULL) {
3912 os_free(val);
3913 return -1;
3914 }
3915 *pos++ = '\0';
3916
3917 p->priority = atoi(pos);
3918
3919 pos = os_strchr(pos, ',');
3920 if (pos == NULL) {
3921 os_free(val);
3922 return -1;
3923 }
3924 *pos++ = '\0';
3925
3926 if (os_strlen(pos) >= sizeof(p->country)) {
3927 os_free(val);
3928 return -1;
3929 }
3930 os_memcpy(p->country, pos, os_strlen(pos) + 1);
3931
3932 cred->num_roaming_partner++;
3933 os_free(val);
3934
3935 return 0;
3936 }
3937
3938 if (os_strcmp(var, "provisioning_sp") == 0) {
3939 os_free(cred->provisioning_sp);
3940 cred->provisioning_sp = val;
3941 return 0;
3942 }
3943
Sunil8cd6f4d2022-06-28 18:40:46 +00003944 if (os_strcmp(var, "imsi_privacy_cert") == 0) {
3945 os_free(cred->imsi_privacy_cert);
3946 cred->imsi_privacy_cert = val;
3947 return 0;
3948 }
3949
3950 if (os_strcmp(var, "imsi_privacy_attr") == 0) {
3951 os_free(cred->imsi_privacy_attr);
3952 cred->imsi_privacy_attr = val;
Sunil Ravia04bd252022-05-02 22:54:18 -07003953 return 0;
3954 }
3955
Dmitry Shmidt04949592012-07-19 12:16:46 -07003956 if (line) {
3957 wpa_printf(MSG_ERROR, "Line %d: unknown cred field '%s'.",
3958 line, var);
3959 }
3960
3961 os_free(val);
3962
3963 return -1;
3964}
3965
3966
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003967static char * alloc_int_str(int val)
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003968{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003969 const unsigned int bufsize = 20;
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003970 char *buf;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003971 int res;
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003972
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003973 buf = os_malloc(bufsize);
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003974 if (buf == NULL)
3975 return NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003976 res = os_snprintf(buf, bufsize, "%d", val);
3977 if (os_snprintf_error(bufsize, res)) {
3978 os_free(buf);
3979 buf = NULL;
3980 }
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003981 return buf;
3982}
3983
3984
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003985static char * alloc_strdup(const char *str)
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07003986{
3987 if (str == NULL)
3988 return NULL;
3989 return os_strdup(str);
3990}
3991
3992
3993char * wpa_config_get_cred_no_key(struct wpa_cred *cred, const char *var)
3994{
3995 if (os_strcmp(var, "temporary") == 0)
3996 return alloc_int_str(cred->temporary);
3997
3998 if (os_strcmp(var, "priority") == 0)
3999 return alloc_int_str(cred->priority);
4000
4001 if (os_strcmp(var, "sp_priority") == 0)
4002 return alloc_int_str(cred->sp_priority);
4003
4004 if (os_strcmp(var, "pcsc") == 0)
4005 return alloc_int_str(cred->pcsc);
4006
4007 if (os_strcmp(var, "eap") == 0) {
4008 if (!cred->eap_method)
4009 return NULL;
4010 return alloc_strdup(eap_get_name(cred->eap_method[0].vendor,
4011 cred->eap_method[0].method));
4012 }
4013
4014 if (os_strcmp(var, "update_identifier") == 0)
4015 return alloc_int_str(cred->update_identifier);
4016
4017 if (os_strcmp(var, "min_dl_bandwidth_home") == 0)
4018 return alloc_int_str(cred->min_dl_bandwidth_home);
4019
4020 if (os_strcmp(var, "min_ul_bandwidth_home") == 0)
4021 return alloc_int_str(cred->min_ul_bandwidth_home);
4022
4023 if (os_strcmp(var, "min_dl_bandwidth_roaming") == 0)
4024 return alloc_int_str(cred->min_dl_bandwidth_roaming);
4025
4026 if (os_strcmp(var, "min_ul_bandwidth_roaming") == 0)
4027 return alloc_int_str(cred->min_ul_bandwidth_roaming);
4028
4029 if (os_strcmp(var, "max_bss_load") == 0)
4030 return alloc_int_str(cred->max_bss_load);
4031
4032 if (os_strcmp(var, "req_conn_capab") == 0) {
4033 unsigned int i;
4034 char *buf, *end, *pos;
4035 int ret;
4036
4037 if (!cred->num_req_conn_capab)
4038 return NULL;
4039
4040 buf = os_malloc(4000);
4041 if (buf == NULL)
4042 return NULL;
4043 pos = buf;
4044 end = pos + 4000;
4045 for (i = 0; i < cred->num_req_conn_capab; i++) {
4046 int *ports;
4047
4048 ret = os_snprintf(pos, end - pos, "%s%u",
4049 i > 0 ? "\n" : "",
4050 cred->req_conn_capab_proto[i]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004051 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004052 return buf;
4053 pos += ret;
4054
4055 ports = cred->req_conn_capab_port[i];
4056 if (ports) {
4057 int j;
4058 for (j = 0; ports[j] != -1; j++) {
4059 ret = os_snprintf(pos, end - pos,
4060 "%s%d",
4061 j > 0 ? "," : ":",
4062 ports[j]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004063 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004064 return buf;
4065 pos += ret;
4066 }
4067 }
4068 }
4069
4070 return buf;
4071 }
4072
4073 if (os_strcmp(var, "ocsp") == 0)
4074 return alloc_int_str(cred->ocsp);
4075
4076 if (os_strcmp(var, "realm") == 0)
4077 return alloc_strdup(cred->realm);
4078
4079 if (os_strcmp(var, "username") == 0)
4080 return alloc_strdup(cred->username);
4081
4082 if (os_strcmp(var, "password") == 0) {
4083 if (!cred->password)
4084 return NULL;
4085 return alloc_strdup("*");
4086 }
4087
4088 if (os_strcmp(var, "ca_cert") == 0)
4089 return alloc_strdup(cred->ca_cert);
4090
4091 if (os_strcmp(var, "client_cert") == 0)
4092 return alloc_strdup(cred->client_cert);
4093
4094 if (os_strcmp(var, "private_key") == 0)
4095 return alloc_strdup(cred->private_key);
4096
4097 if (os_strcmp(var, "private_key_passwd") == 0) {
4098 if (!cred->private_key_passwd)
4099 return NULL;
4100 return alloc_strdup("*");
4101 }
4102
4103 if (os_strcmp(var, "imsi") == 0)
4104 return alloc_strdup(cred->imsi);
4105
Sunil8cd6f4d2022-06-28 18:40:46 +00004106 if (os_strcmp(var, "imsi_privacy_cert") == 0)
4107 return alloc_strdup(cred->imsi_privacy_cert);
4108
4109 if (os_strcmp(var, "imsi_privacy_attr") == 0)
4110 return alloc_strdup(cred->imsi_privacy_attr);
Sunil Ravia04bd252022-05-02 22:54:18 -07004111
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004112 if (os_strcmp(var, "milenage") == 0) {
4113 if (!(cred->milenage))
4114 return NULL;
4115 return alloc_strdup("*");
4116 }
4117
4118 if (os_strcmp(var, "domain_suffix_match") == 0)
4119 return alloc_strdup(cred->domain_suffix_match);
4120
4121 if (os_strcmp(var, "domain") == 0) {
4122 unsigned int i;
4123 char *buf, *end, *pos;
4124 int ret;
4125
4126 if (!cred->num_domain)
4127 return NULL;
4128
4129 buf = os_malloc(4000);
4130 if (buf == NULL)
4131 return NULL;
4132 pos = buf;
4133 end = pos + 4000;
4134
4135 for (i = 0; i < cred->num_domain; i++) {
4136 ret = os_snprintf(pos, end - pos, "%s%s",
4137 i > 0 ? "\n" : "", cred->domain[i]);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004138 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004139 return buf;
4140 pos += ret;
4141 }
4142
4143 return buf;
4144 }
4145
4146 if (os_strcmp(var, "phase1") == 0)
4147 return alloc_strdup(cred->phase1);
4148
4149 if (os_strcmp(var, "phase2") == 0)
4150 return alloc_strdup(cred->phase2);
4151
4152 if (os_strcmp(var, "roaming_consortium") == 0) {
4153 size_t buflen;
4154 char *buf;
4155
4156 if (!cred->roaming_consortium_len)
4157 return NULL;
4158 buflen = cred->roaming_consortium_len * 2 + 1;
4159 buf = os_malloc(buflen);
4160 if (buf == NULL)
4161 return NULL;
4162 wpa_snprintf_hex(buf, buflen, cred->roaming_consortium,
4163 cred->roaming_consortium_len);
4164 return buf;
4165 }
4166
4167 if (os_strcmp(var, "required_roaming_consortium") == 0) {
4168 size_t buflen;
4169 char *buf;
4170
4171 if (!cred->required_roaming_consortium_len)
4172 return NULL;
4173 buflen = cred->required_roaming_consortium_len * 2 + 1;
4174 buf = os_malloc(buflen);
4175 if (buf == NULL)
4176 return NULL;
4177 wpa_snprintf_hex(buf, buflen, cred->required_roaming_consortium,
4178 cred->required_roaming_consortium_len);
4179 return buf;
4180 }
4181
Roshan Pius3a1667e2018-07-03 15:17:14 -07004182 if (os_strcmp(var, "roaming_consortiums") == 0) {
4183 size_t buflen;
4184 char *buf, *pos;
4185 size_t i;
4186
4187 if (!cred->num_roaming_consortiums)
4188 return NULL;
4189 buflen = cred->num_roaming_consortiums *
4190 MAX_ROAMING_CONS_OI_LEN * 2 + 1;
4191 buf = os_malloc(buflen);
4192 if (!buf)
4193 return NULL;
4194 pos = buf;
4195 for (i = 0; i < cred->num_roaming_consortiums; i++) {
4196 if (i > 0)
4197 *pos++ = ',';
4198 pos += wpa_snprintf_hex(
4199 pos, buf + buflen - pos,
4200 cred->roaming_consortiums[i],
4201 cred->roaming_consortiums_len[i]);
4202 }
4203 *pos = '\0';
4204 return buf;
4205 }
4206
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004207 if (os_strcmp(var, "excluded_ssid") == 0) {
4208 unsigned int i;
4209 char *buf, *end, *pos;
4210
4211 if (!cred->num_excluded_ssid)
4212 return NULL;
4213
4214 buf = os_malloc(4000);
4215 if (buf == NULL)
4216 return NULL;
4217 pos = buf;
4218 end = pos + 4000;
4219
4220 for (i = 0; i < cred->num_excluded_ssid; i++) {
4221 struct excluded_ssid *e;
4222 int ret;
4223
4224 e = &cred->excluded_ssid[i];
4225 ret = os_snprintf(pos, end - pos, "%s%s",
4226 i > 0 ? "\n" : "",
4227 wpa_ssid_txt(e->ssid, e->ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004228 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004229 return buf;
4230 pos += ret;
4231 }
4232
4233 return buf;
4234 }
4235
4236 if (os_strcmp(var, "roaming_partner") == 0) {
4237 unsigned int i;
4238 char *buf, *end, *pos;
4239
4240 if (!cred->num_roaming_partner)
4241 return NULL;
4242
4243 buf = os_malloc(4000);
4244 if (buf == NULL)
4245 return NULL;
4246 pos = buf;
4247 end = pos + 4000;
4248
4249 for (i = 0; i < cred->num_roaming_partner; i++) {
4250 struct roaming_partner *p;
4251 int ret;
4252
4253 p = &cred->roaming_partner[i];
4254 ret = os_snprintf(pos, end - pos, "%s%s,%d,%u,%s",
4255 i > 0 ? "\n" : "",
4256 p->fqdn, p->exact_match, p->priority,
4257 p->country);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004258 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt0cfd5f72014-04-04 14:48:05 -07004259 return buf;
4260 pos += ret;
4261 }
4262
4263 return buf;
4264 }
4265
4266 if (os_strcmp(var, "provisioning_sp") == 0)
4267 return alloc_strdup(cred->provisioning_sp);
4268
4269 return NULL;
4270}
4271
4272
Dmitry Shmidt04949592012-07-19 12:16:46 -07004273struct wpa_cred * wpa_config_get_cred(struct wpa_config *config, int id)
4274{
4275 struct wpa_cred *cred;
4276
4277 cred = config->cred;
4278 while (cred) {
4279 if (id == cred->id)
4280 break;
4281 cred = cred->next;
4282 }
4283
4284 return cred;
4285}
4286
4287
4288struct wpa_cred * wpa_config_add_cred(struct wpa_config *config)
4289{
4290 int id;
4291 struct wpa_cred *cred, *last = NULL;
4292
4293 id = -1;
4294 cred = config->cred;
4295 while (cred) {
4296 if (cred->id > id)
4297 id = cred->id;
4298 last = cred;
4299 cred = cred->next;
4300 }
4301 id++;
4302
4303 cred = os_zalloc(sizeof(*cred));
4304 if (cred == NULL)
4305 return NULL;
4306 cred->id = id;
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -07004307 cred->sim_num = DEFAULT_USER_SELECTED_SIM;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004308 if (last)
4309 last->next = cred;
4310 else
4311 config->cred = cred;
4312
4313 return cred;
4314}
4315
4316
4317int wpa_config_remove_cred(struct wpa_config *config, int id)
4318{
4319 struct wpa_cred *cred, *prev = NULL;
4320
4321 cred = config->cred;
4322 while (cred) {
4323 if (id == cred->id)
4324 break;
4325 prev = cred;
4326 cred = cred->next;
4327 }
4328
4329 if (cred == NULL)
4330 return -1;
4331
4332 if (prev)
4333 prev->next = cred->next;
4334 else
4335 config->cred = cred->next;
4336
4337 wpa_config_free_cred(cred);
4338 return 0;
4339}
4340
4341
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004342#ifndef CONFIG_NO_CONFIG_BLOBS
4343/**
4344 * wpa_config_get_blob - Get a named configuration blob
4345 * @config: Configuration data from wpa_config_read()
4346 * @name: Name of the blob
4347 * Returns: Pointer to blob data or %NULL if not found
4348 */
4349const struct wpa_config_blob * wpa_config_get_blob(struct wpa_config *config,
4350 const char *name)
4351{
4352 struct wpa_config_blob *blob = config->blobs;
4353
4354 while (blob) {
4355 if (os_strcmp(blob->name, name) == 0)
4356 return blob;
4357 blob = blob->next;
4358 }
4359 return NULL;
4360}
4361
4362
4363/**
4364 * wpa_config_set_blob - Set or add a named configuration blob
4365 * @config: Configuration data from wpa_config_read()
4366 * @blob: New value for the blob
4367 *
4368 * Adds a new configuration blob or replaces the current value of an existing
4369 * blob.
4370 */
4371void wpa_config_set_blob(struct wpa_config *config,
4372 struct wpa_config_blob *blob)
4373{
4374 wpa_config_remove_blob(config, blob->name);
4375 blob->next = config->blobs;
4376 config->blobs = blob;
4377}
4378
4379
4380/**
4381 * wpa_config_free_blob - Free blob data
4382 * @blob: Pointer to blob to be freed
4383 */
4384void wpa_config_free_blob(struct wpa_config_blob *blob)
4385{
4386 if (blob) {
4387 os_free(blob->name);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07004388 bin_clear_free(blob->data, blob->len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004389 os_free(blob);
4390 }
4391}
4392
4393
4394/**
4395 * wpa_config_remove_blob - Remove a named configuration blob
4396 * @config: Configuration data from wpa_config_read()
4397 * @name: Name of the blob to remove
4398 * Returns: 0 if blob was removed or -1 if blob was not found
4399 */
4400int wpa_config_remove_blob(struct wpa_config *config, const char *name)
4401{
4402 struct wpa_config_blob *pos = config->blobs, *prev = NULL;
4403
4404 while (pos) {
4405 if (os_strcmp(pos->name, name) == 0) {
4406 if (prev)
4407 prev->next = pos->next;
4408 else
4409 config->blobs = pos->next;
4410 wpa_config_free_blob(pos);
4411 return 0;
4412 }
4413 prev = pos;
4414 pos = pos->next;
4415 }
4416
4417 return -1;
4418}
4419#endif /* CONFIG_NO_CONFIG_BLOBS */
4420
4421
4422/**
4423 * wpa_config_alloc_empty - Allocate an empty configuration
4424 * @ctrl_interface: Control interface parameters, e.g., path to UNIX domain
4425 * socket
4426 * @driver_param: Driver parameters
4427 * Returns: Pointer to allocated configuration data or %NULL on failure
4428 */
4429struct wpa_config * wpa_config_alloc_empty(const char *ctrl_interface,
4430 const char *driver_param)
4431{
Hai Shalom899fcc72020-10-19 14:38:18 -07004432#define ecw2cw(ecw) ((1 << (ecw)) - 1)
4433
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004434 struct wpa_config *config;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004435 const int aCWmin = 4, aCWmax = 10;
4436 const struct hostapd_wmm_ac_params ac_bk =
4437 { aCWmin, aCWmax, 7, 0, 0 }; /* background traffic */
4438 const struct hostapd_wmm_ac_params ac_be =
4439 { aCWmin, aCWmax, 3, 0, 0 }; /* best effort traffic */
4440 const struct hostapd_wmm_ac_params ac_vi = /* video traffic */
Hai Shalom899fcc72020-10-19 14:38:18 -07004441 { aCWmin - 1, aCWmin, 2, 3008 / 32, 0 };
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004442 const struct hostapd_wmm_ac_params ac_vo = /* voice traffic */
Hai Shalom899fcc72020-10-19 14:38:18 -07004443 { aCWmin - 2, aCWmin - 1, 2, 1504 / 32, 0 };
4444 const struct hostapd_tx_queue_params txq_bk =
4445 { 7, ecw2cw(aCWmin), ecw2cw(aCWmax), 0 };
4446 const struct hostapd_tx_queue_params txq_be =
4447 { 3, ecw2cw(aCWmin), 4 * (ecw2cw(aCWmin) + 1) - 1, 0 };
4448 const struct hostapd_tx_queue_params txq_vi =
4449 { 1, (ecw2cw(aCWmin) + 1) / 2 - 1, ecw2cw(aCWmin), 30 };
4450 const struct hostapd_tx_queue_params txq_vo =
4451 { 1, (ecw2cw(aCWmin) + 1) / 4 - 1,
4452 (ecw2cw(aCWmin) + 1) / 2 - 1, 15 };
4453
4454#undef ecw2cw
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004455
4456 config = os_zalloc(sizeof(*config));
4457 if (config == NULL)
4458 return NULL;
4459 config->eapol_version = DEFAULT_EAPOL_VERSION;
4460 config->ap_scan = DEFAULT_AP_SCAN;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004461 config->user_mpm = DEFAULT_USER_MPM;
4462 config->max_peer_links = DEFAULT_MAX_PEER_LINKS;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08004463 config->mesh_max_inactivity = DEFAULT_MESH_MAX_INACTIVITY;
Hai Shaloma20dcd72022-02-04 13:43:00 -08004464 config->mesh_fwding = DEFAULT_MESH_FWDING;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004465 config->dot11RSNASAERetransPeriod =
4466 DEFAULT_DOT11_RSNA_SAE_RETRANS_PERIOD;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004467 config->fast_reauth = DEFAULT_FAST_REAUTH;
4468 config->p2p_go_intent = DEFAULT_P2P_GO_INTENT;
4469 config->p2p_intra_bss = DEFAULT_P2P_INTRA_BSS;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004470 config->p2p_go_freq_change_policy = DEFAULT_P2P_GO_FREQ_MOVE;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004471 config->p2p_go_max_inactivity = DEFAULT_P2P_GO_MAX_INACTIVITY;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004472 config->p2p_optimize_listen_chan = DEFAULT_P2P_OPTIMIZE_LISTEN_CHAN;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004473 config->p2p_go_ctwindow = DEFAULT_P2P_GO_CTWINDOW;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004474 config->bss_max_count = DEFAULT_BSS_MAX_COUNT;
4475 config->bss_expiration_age = DEFAULT_BSS_EXPIRATION_AGE;
4476 config->bss_expiration_scan_count = DEFAULT_BSS_EXPIRATION_SCAN_COUNT;
4477 config->max_num_sta = DEFAULT_MAX_NUM_STA;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004478 config->ap_isolate = DEFAULT_AP_ISOLATE;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004479 config->access_network_type = DEFAULT_ACCESS_NETWORK_TYPE;
Dmitry Shmidtea69e842013-05-13 14:52:28 -07004480 config->scan_cur_freq = DEFAULT_SCAN_CUR_FREQ;
Hai Shalom60840252021-02-19 19:02:11 -08004481 config->scan_res_valid_for_connect = DEFAULT_SCAN_RES_VALID_FOR_CONNECT;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004482 config->wmm_ac_params[0] = ac_be;
4483 config->wmm_ac_params[1] = ac_bk;
4484 config->wmm_ac_params[2] = ac_vi;
4485 config->wmm_ac_params[3] = ac_vo;
Hai Shalom899fcc72020-10-19 14:38:18 -07004486 config->tx_queue[0] = txq_vo;
4487 config->tx_queue[1] = txq_vi;
4488 config->tx_queue[2] = txq_be;
4489 config->tx_queue[3] = txq_bk;
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07004490 config->p2p_search_delay = DEFAULT_P2P_SEARCH_DELAY;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004491 config->rand_addr_lifetime = DEFAULT_RAND_ADDR_LIFETIME;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004492 config->key_mgmt_offload = DEFAULT_KEY_MGMT_OFFLOAD;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08004493 config->cert_in_cb = DEFAULT_CERT_IN_CB;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004494 config->wpa_rsc_relaxation = DEFAULT_WPA_RSC_RELAXATION;
Hai Shalomfdcde762020-04-02 11:19:20 -07004495 config->extended_key_id = DEFAULT_EXTENDED_KEY_ID;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004496
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004497#ifdef CONFIG_MBO
4498 config->mbo_cell_capa = DEFAULT_MBO_CELL_CAPA;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004499 config->disassoc_imminent_rssi_threshold =
4500 DEFAULT_DISASSOC_IMMINENT_RSSI_THRESHOLD;
4501 config->oce = DEFAULT_OCE_SUPPORT;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004502#endif /* CONFIG_MBO */
Sunil Ravi84bb3e12021-06-10 09:52:05 -07004503 config->btm_offload = DEFAULT_BTM_OFFLOAD;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004504
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004505 if (ctrl_interface)
4506 config->ctrl_interface = os_strdup(ctrl_interface);
4507 if (driver_param)
4508 config->driver_param = os_strdup(driver_param);
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004509 config->gas_rand_addr_lifetime = DEFAULT_RAND_ADDR_LIFETIME;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004510
4511 return config;
4512}
4513
4514
4515#ifndef CONFIG_NO_STDOUT_DEBUG
4516/**
4517 * wpa_config_debug_dump_networks - Debug dump of configured networks
4518 * @config: Configuration data from wpa_config_read()
4519 */
4520void wpa_config_debug_dump_networks(struct wpa_config *config)
4521{
Hai Shalomfdcde762020-04-02 11:19:20 -07004522 size_t prio;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004523 struct wpa_ssid *ssid;
4524
4525 for (prio = 0; prio < config->num_prio; prio++) {
4526 ssid = config->pssid[prio];
4527 wpa_printf(MSG_DEBUG, "Priority group %d",
4528 ssid->priority);
4529 while (ssid) {
4530 wpa_printf(MSG_DEBUG, " id=%d ssid='%s'",
4531 ssid->id,
4532 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
4533 ssid = ssid->pnext;
4534 }
4535 }
4536}
4537#endif /* CONFIG_NO_STDOUT_DEBUG */
4538
4539
Hai Shalom899fcc72020-10-19 14:38:18 -07004540/**
4541 * Structure for global configuration parsing. This data is used to implement a
4542 * generic parser for the global interface configuration. The table of variables
4543 * is defined below in this file (global_fields[]).
4544 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004545struct global_parse_data {
Hai Shalom899fcc72020-10-19 14:38:18 -07004546 /* Configuration variable name */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004547 char *name;
Hai Shalom899fcc72020-10-19 14:38:18 -07004548
4549 /* Parser function for this variable. The parser functions return 0 or 1
4550 * to indicate success. Value 0 indicates that the parameter value may
4551 * have changed while value 1 means that the value did not change.
4552 * Error cases (failure to parse the string) are indicated by returning
4553 * -1. */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004554 int (*parser)(const struct global_parse_data *data,
4555 struct wpa_config *config, int line, const char *value);
Hai Shalom899fcc72020-10-19 14:38:18 -07004556
4557 /* Getter function to print the variable in text format to buf. */
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004558 int (*get)(const char *name, struct wpa_config *config, long offset,
4559 char *buf, size_t buflen, int pretty_print);
Hai Shalom899fcc72020-10-19 14:38:18 -07004560
4561 /* Variable specific parameters for the parser. */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004562 void *param1, *param2, *param3;
Hai Shalom899fcc72020-10-19 14:38:18 -07004563
4564 /* Indicates which configuration variable has changed. */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004565 unsigned int changed_flag;
4566};
4567
4568
4569static int wpa_global_config_parse_int(const struct global_parse_data *data,
4570 struct wpa_config *config, int line,
4571 const char *pos)
4572{
Dmitry Shmidt2f023192013-03-12 12:44:17 -07004573 int val, *dst;
4574 char *end;
Hai Shalom899fcc72020-10-19 14:38:18 -07004575 bool same;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07004576
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004577 dst = (int *) (((u8 *) config) + (long) data->param1);
Dmitry Shmidt2f023192013-03-12 12:44:17 -07004578 val = strtol(pos, &end, 0);
4579 if (*end) {
4580 wpa_printf(MSG_ERROR, "Line %d: invalid number \"%s\"",
4581 line, pos);
4582 return -1;
4583 }
Hai Shalom899fcc72020-10-19 14:38:18 -07004584 same = *dst == val;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07004585 *dst = val;
4586
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004587 wpa_printf(MSG_DEBUG, "%s=%d", data->name, *dst);
4588
4589 if (data->param2 && *dst < (long) data->param2) {
4590 wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
4591 "min_value=%ld)", line, data->name, *dst,
4592 (long) data->param2);
4593 *dst = (long) data->param2;
4594 return -1;
4595 }
4596
4597 if (data->param3 && *dst > (long) data->param3) {
4598 wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
4599 "max_value=%ld)", line, data->name, *dst,
4600 (long) data->param3);
4601 *dst = (long) data->param3;
4602 return -1;
4603 }
4604
Hai Shalom899fcc72020-10-19 14:38:18 -07004605 return same;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004606}
4607
4608
4609static int wpa_global_config_parse_str(const struct global_parse_data *data,
4610 struct wpa_config *config, int line,
4611 const char *pos)
4612{
Hai Shalom899fcc72020-10-19 14:38:18 -07004613 size_t len, prev_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004614 char **dst, *tmp;
4615
4616 len = os_strlen(pos);
4617 if (data->param2 && len < (size_t) data->param2) {
4618 wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
4619 "min_len=%ld)", line, data->name,
4620 (unsigned long) len, (long) data->param2);
4621 return -1;
4622 }
4623
4624 if (data->param3 && len > (size_t) data->param3) {
4625 wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
4626 "max_len=%ld)", line, data->name,
4627 (unsigned long) len, (long) data->param3);
4628 return -1;
4629 }
4630
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004631 if (has_newline(pos)) {
4632 wpa_printf(MSG_ERROR, "Line %d: invalid %s value with newline",
4633 line, data->name);
4634 return -1;
4635 }
4636
Hai Shalom899fcc72020-10-19 14:38:18 -07004637 dst = (char **) (((u8 *) config) + (long) data->param1);
4638 if (*dst)
4639 prev_len = os_strlen(*dst);
4640 else
4641 prev_len = 0;
4642
4643 /* No change to the previously configured value */
Hai Shalom60840252021-02-19 19:02:11 -08004644 if (*dst && prev_len == len && os_memcmp(*dst, pos, len) == 0)
Hai Shalom899fcc72020-10-19 14:38:18 -07004645 return 1;
4646
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004647 tmp = os_strdup(pos);
4648 if (tmp == NULL)
4649 return -1;
4650
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004651 os_free(*dst);
4652 *dst = tmp;
4653 wpa_printf(MSG_DEBUG, "%s='%s'", data->name, *dst);
4654
4655 return 0;
4656}
4657
4658
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08004659static int wpa_config_process_bgscan(const struct global_parse_data *data,
4660 struct wpa_config *config, int line,
4661 const char *pos)
4662{
4663 size_t len;
4664 char *tmp;
Dmitry Shmidt97672262014-02-03 13:02:54 -08004665 int res;
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08004666
4667 tmp = wpa_config_parse_string(pos, &len);
4668 if (tmp == NULL) {
4669 wpa_printf(MSG_ERROR, "Line %d: failed to parse %s",
4670 line, data->name);
4671 return -1;
4672 }
4673
Dmitry Shmidt97672262014-02-03 13:02:54 -08004674 res = wpa_global_config_parse_str(data, config, line, tmp);
4675 os_free(tmp);
4676 return res;
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08004677}
4678
4679
Dmitry Shmidt04949592012-07-19 12:16:46 -07004680static int wpa_global_config_parse_bin(const struct global_parse_data *data,
4681 struct wpa_config *config, int line,
4682 const char *pos)
4683{
Dmitry Shmidt04949592012-07-19 12:16:46 -07004684 struct wpabuf **dst, *tmp;
4685
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004686 tmp = wpabuf_parse_bin(pos);
4687 if (!tmp)
Dmitry Shmidt04949592012-07-19 12:16:46 -07004688 return -1;
4689
Dmitry Shmidt04949592012-07-19 12:16:46 -07004690 dst = (struct wpabuf **) (((u8 *) config) + (long) data->param1);
Hai Shalom899fcc72020-10-19 14:38:18 -07004691 if (wpabuf_cmp(*dst, tmp) == 0) {
4692 wpabuf_free(tmp);
4693 return 1;
4694 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004695 wpabuf_free(*dst);
4696 *dst = tmp;
4697 wpa_printf(MSG_DEBUG, "%s", data->name);
4698
4699 return 0;
4700}
4701
4702
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07004703static int wpa_config_process_freq_list(const struct global_parse_data *data,
4704 struct wpa_config *config, int line,
4705 const char *value)
4706{
4707 int *freqs;
4708
4709 freqs = wpa_config_parse_int_array(value);
4710 if (freqs == NULL)
4711 return -1;
Dmitry Shmidt56052862013-10-04 10:23:25 -07004712 if (freqs[0] == 0) {
4713 os_free(freqs);
4714 freqs = NULL;
4715 }
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07004716 os_free(config->freq_list);
4717 config->freq_list = freqs;
4718 return 0;
4719}
4720
4721
Hai Shalom60840252021-02-19 19:02:11 -08004722static int
4723wpa_config_process_initial_freq_list(const struct global_parse_data *data,
4724 struct wpa_config *config, int line,
4725 const char *value)
4726{
4727 int *freqs;
4728
4729 freqs = wpa_config_parse_int_array(value);
4730 if (!freqs)
4731 return -1;
4732 if (freqs[0] == 0) {
4733 os_free(freqs);
4734 freqs = NULL;
4735 }
4736 os_free(config->initial_freq_list);
4737 config->initial_freq_list = freqs;
4738 return 0;
4739}
4740
4741
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004742#ifdef CONFIG_P2P
4743static int wpa_global_config_parse_ipv4(const struct global_parse_data *data,
4744 struct wpa_config *config, int line,
4745 const char *pos)
4746{
4747 u32 *dst;
4748 struct hostapd_ip_addr addr;
4749
4750 if (hostapd_parse_ip_addr(pos, &addr) < 0)
4751 return -1;
4752 if (addr.af != AF_INET)
4753 return -1;
4754
4755 dst = (u32 *) (((u8 *) config) + (long) data->param1);
Hai Shalom899fcc72020-10-19 14:38:18 -07004756 if (os_memcmp(dst, &addr.u.v4.s_addr, 4) == 0)
4757 return 1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004758 os_memcpy(dst, &addr.u.v4.s_addr, 4);
4759 wpa_printf(MSG_DEBUG, "%s = 0x%x", data->name,
4760 WPA_GET_BE32((u8 *) dst));
4761
4762 return 0;
4763}
4764#endif /* CONFIG_P2P */
4765
4766
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004767static int wpa_config_process_country(const struct global_parse_data *data,
4768 struct wpa_config *config, int line,
4769 const char *pos)
4770{
4771 if (!pos[0] || !pos[1]) {
4772 wpa_printf(MSG_DEBUG, "Invalid country set");
4773 return -1;
4774 }
Hai Shalom899fcc72020-10-19 14:38:18 -07004775 if (pos[0] == config->country[0] && pos[1] == config->country[1])
4776 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004777 config->country[0] = pos[0];
4778 config->country[1] = pos[1];
4779 wpa_printf(MSG_DEBUG, "country='%c%c'",
4780 config->country[0], config->country[1]);
4781 return 0;
4782}
4783
4784
4785static int wpa_config_process_load_dynamic_eap(
4786 const struct global_parse_data *data, struct wpa_config *config,
4787 int line, const char *so)
4788{
4789 int ret;
4790 wpa_printf(MSG_DEBUG, "load_dynamic_eap=%s", so);
4791 ret = eap_peer_method_load(so);
4792 if (ret == -2) {
4793 wpa_printf(MSG_DEBUG, "This EAP type was already loaded - not "
4794 "reloading.");
4795 } else if (ret) {
4796 wpa_printf(MSG_ERROR, "Line %d: Failed to load dynamic EAP "
4797 "method '%s'.", line, so);
4798 return -1;
4799 }
4800
4801 return 0;
4802}
4803
4804
4805#ifdef CONFIG_WPS
4806
4807static int wpa_config_process_uuid(const struct global_parse_data *data,
4808 struct wpa_config *config, int line,
4809 const char *pos)
4810{
4811 char buf[40];
4812 if (uuid_str2bin(pos, config->uuid)) {
4813 wpa_printf(MSG_ERROR, "Line %d: invalid UUID", line);
4814 return -1;
4815 }
4816 uuid_bin2str(config->uuid, buf, sizeof(buf));
4817 wpa_printf(MSG_DEBUG, "uuid=%s", buf);
4818 return 0;
4819}
4820
4821
4822static int wpa_config_process_device_type(
4823 const struct global_parse_data *data,
4824 struct wpa_config *config, int line, const char *pos)
4825{
4826 return wps_dev_type_str2bin(pos, config->device_type);
4827}
4828
4829
4830static int wpa_config_process_os_version(const struct global_parse_data *data,
4831 struct wpa_config *config, int line,
4832 const char *pos)
4833{
4834 if (hexstr2bin(pos, config->os_version, 4)) {
4835 wpa_printf(MSG_ERROR, "Line %d: invalid os_version", line);
4836 return -1;
4837 }
4838 wpa_printf(MSG_DEBUG, "os_version=%08x",
4839 WPA_GET_BE32(config->os_version));
4840 return 0;
4841}
4842
Dmitry Shmidt04949592012-07-19 12:16:46 -07004843
4844static int wpa_config_process_wps_vendor_ext_m1(
4845 const struct global_parse_data *data,
4846 struct wpa_config *config, int line, const char *pos)
4847{
4848 struct wpabuf *tmp;
4849 int len = os_strlen(pos) / 2;
4850 u8 *p;
4851
4852 if (!len) {
4853 wpa_printf(MSG_ERROR, "Line %d: "
4854 "invalid wps_vendor_ext_m1", line);
4855 return -1;
4856 }
4857
4858 tmp = wpabuf_alloc(len);
4859 if (tmp) {
4860 p = wpabuf_put(tmp, len);
4861
4862 if (hexstr2bin(pos, p, len)) {
4863 wpa_printf(MSG_ERROR, "Line %d: "
4864 "invalid wps_vendor_ext_m1", line);
4865 wpabuf_free(tmp);
4866 return -1;
4867 }
4868
4869 wpabuf_free(config->wps_vendor_ext_m1);
4870 config->wps_vendor_ext_m1 = tmp;
4871 } else {
4872 wpa_printf(MSG_ERROR, "Can not allocate "
4873 "memory for wps_vendor_ext_m1");
4874 return -1;
4875 }
4876
4877 return 0;
4878}
4879
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004880#endif /* CONFIG_WPS */
4881
4882#ifdef CONFIG_P2P
4883static int wpa_config_process_sec_device_type(
4884 const struct global_parse_data *data,
4885 struct wpa_config *config, int line, const char *pos)
4886{
4887 int idx;
4888
4889 if (config->num_sec_device_types >= MAX_SEC_DEVICE_TYPES) {
4890 wpa_printf(MSG_ERROR, "Line %d: too many sec_device_type "
4891 "items", line);
4892 return -1;
4893 }
4894
4895 idx = config->num_sec_device_types;
4896
4897 if (wps_dev_type_str2bin(pos, config->sec_device_type[idx]))
4898 return -1;
4899
4900 config->num_sec_device_types++;
4901 return 0;
4902}
Dmitry Shmidt04949592012-07-19 12:16:46 -07004903
4904
4905static int wpa_config_process_p2p_pref_chan(
4906 const struct global_parse_data *data,
4907 struct wpa_config *config, int line, const char *pos)
4908{
4909 struct p2p_channel *pref = NULL, *n;
Hai Shalomfdcde762020-04-02 11:19:20 -07004910 size_t num = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004911 const char *pos2;
4912 u8 op_class, chan;
4913
4914 /* format: class:chan,class:chan,... */
4915
4916 while (*pos) {
4917 op_class = atoi(pos);
4918 pos2 = os_strchr(pos, ':');
4919 if (pos2 == NULL)
4920 goto fail;
4921 pos2++;
4922 chan = atoi(pos2);
4923
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004924 n = os_realloc_array(pref, num + 1,
4925 sizeof(struct p2p_channel));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004926 if (n == NULL)
4927 goto fail;
4928 pref = n;
4929 pref[num].op_class = op_class;
4930 pref[num].chan = chan;
4931 num++;
4932
4933 pos = os_strchr(pos2, ',');
4934 if (pos == NULL)
4935 break;
4936 pos++;
4937 }
4938
4939 os_free(config->p2p_pref_chan);
4940 config->p2p_pref_chan = pref;
4941 config->num_p2p_pref_chan = num;
4942 wpa_hexdump(MSG_DEBUG, "P2P: Preferred class/channel pairs",
4943 (u8 *) config->p2p_pref_chan,
4944 config->num_p2p_pref_chan * sizeof(struct p2p_channel));
4945
4946 return 0;
4947
4948fail:
4949 os_free(pref);
4950 wpa_printf(MSG_ERROR, "Line %d: Invalid p2p_pref_chan list", line);
4951 return -1;
4952}
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004953
4954
4955static int wpa_config_process_p2p_no_go_freq(
4956 const struct global_parse_data *data,
4957 struct wpa_config *config, int line, const char *pos)
4958{
4959 int ret;
4960
4961 ret = freq_range_list_parse(&config->p2p_no_go_freq, pos);
4962 if (ret < 0) {
4963 wpa_printf(MSG_ERROR, "Line %d: Invalid p2p_no_go_freq", line);
4964 return -1;
4965 }
4966
4967 wpa_printf(MSG_DEBUG, "P2P: p2p_no_go_freq with %u items",
4968 config->p2p_no_go_freq.num);
4969
4970 return 0;
4971}
4972
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004973static int wpa_config_process_p2p_device_persistent_mac_addr(
4974 const struct global_parse_data *data,
4975 struct wpa_config *config, int line, const char *pos)
4976{
4977 if (hwaddr_aton2(pos, config->p2p_device_persistent_mac_addr) < 0) {
4978 wpa_printf(MSG_ERROR,
4979 "Line %d: Invalid p2p_device_persistent_mac_addr '%s'",
4980 line, pos);
4981 return -1;
4982 }
4983
4984 return 0;
4985}
4986
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004987#endif /* CONFIG_P2P */
4988
4989
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004990static int wpa_config_process_hessid(
4991 const struct global_parse_data *data,
4992 struct wpa_config *config, int line, const char *pos)
4993{
4994 if (hwaddr_aton2(pos, config->hessid) < 0) {
4995 wpa_printf(MSG_ERROR, "Line %d: Invalid hessid '%s'",
4996 line, pos);
4997 return -1;
4998 }
4999
5000 return 0;
5001}
5002
5003
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005004static int wpa_config_process_sae_groups(
5005 const struct global_parse_data *data,
5006 struct wpa_config *config, int line, const char *pos)
5007{
5008 int *groups = wpa_config_parse_int_array(pos);
5009 if (groups == NULL) {
5010 wpa_printf(MSG_ERROR, "Line %d: Invalid sae_groups '%s'",
5011 line, pos);
5012 return -1;
5013 }
5014
5015 os_free(config->sae_groups);
5016 config->sae_groups = groups;
5017
5018 return 0;
5019}
5020
5021
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005022static int wpa_config_process_ap_vendor_elements(
5023 const struct global_parse_data *data,
5024 struct wpa_config *config, int line, const char *pos)
5025{
5026 struct wpabuf *tmp;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005027
Hai Shaloma20dcd72022-02-04 13:43:00 -08005028 if (!*pos) {
5029 wpabuf_free(config->ap_vendor_elements);
5030 config->ap_vendor_elements = NULL;
5031 return 0;
5032 }
5033
5034 tmp = wpabuf_parse_bin(pos);
5035 if (!tmp) {
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005036 wpa_printf(MSG_ERROR, "Line %d: invalid ap_vendor_elements",
5037 line);
5038 return -1;
5039 }
Hai Shaloma20dcd72022-02-04 13:43:00 -08005040 wpabuf_free(config->ap_vendor_elements);
5041 config->ap_vendor_elements = tmp;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005042
Hai Shaloma20dcd72022-02-04 13:43:00 -08005043 return 0;
5044}
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005045
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005046
Hai Shaloma20dcd72022-02-04 13:43:00 -08005047static int wpa_config_process_ap_assocresp_elements(
5048 const struct global_parse_data *data,
5049 struct wpa_config *config, int line, const char *pos)
5050{
5051 struct wpabuf *tmp;
5052
5053 if (!*pos) {
5054 wpabuf_free(config->ap_assocresp_elements);
5055 config->ap_assocresp_elements = NULL;
5056 return 0;
5057 }
5058
5059 tmp = wpabuf_parse_bin(pos);
5060 if (!tmp) {
5061 wpa_printf(MSG_ERROR, "Line %d: invalid ap_assocresp_elements",
5062 line);
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005063 return -1;
5064 }
Hai Shaloma20dcd72022-02-04 13:43:00 -08005065 wpabuf_free(config->ap_assocresp_elements);
5066 config->ap_assocresp_elements = tmp;
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005067
5068 return 0;
5069}
5070
5071
Dmitry Shmidt56052862013-10-04 10:23:25 -07005072#ifdef CONFIG_CTRL_IFACE
5073static int wpa_config_process_no_ctrl_interface(
5074 const struct global_parse_data *data,
5075 struct wpa_config *config, int line, const char *pos)
5076{
5077 wpa_printf(MSG_DEBUG, "no_ctrl_interface -> ctrl_interface=NULL");
5078 os_free(config->ctrl_interface);
5079 config->ctrl_interface = NULL;
5080 return 0;
5081}
5082#endif /* CONFIG_CTRL_IFACE */
5083
5084
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005085static int wpa_config_get_int(const char *name, struct wpa_config *config,
5086 long offset, char *buf, size_t buflen,
5087 int pretty_print)
5088{
5089 int *val = (int *) (((u8 *) config) + (long) offset);
5090
5091 if (pretty_print)
5092 return os_snprintf(buf, buflen, "%s=%d\n", name, *val);
5093 return os_snprintf(buf, buflen, "%d", *val);
5094}
5095
5096
5097static int wpa_config_get_str(const char *name, struct wpa_config *config,
5098 long offset, char *buf, size_t buflen,
5099 int pretty_print)
5100{
5101 char **val = (char **) (((u8 *) config) + (long) offset);
5102 int res;
5103
5104 if (pretty_print)
5105 res = os_snprintf(buf, buflen, "%s=%s\n", name,
5106 *val ? *val : "null");
5107 else if (!*val)
5108 return -1;
5109 else
5110 res = os_snprintf(buf, buflen, "%s", *val);
5111 if (os_snprintf_error(buflen, res))
5112 res = -1;
5113
5114 return res;
5115}
5116
5117
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07005118#ifdef CONFIG_P2P
5119static int wpa_config_get_ipv4(const char *name, struct wpa_config *config,
5120 long offset, char *buf, size_t buflen,
5121 int pretty_print)
5122{
5123 void *val = ((u8 *) config) + (long) offset;
5124 int res;
5125 char addr[INET_ADDRSTRLEN];
5126
5127 if (!val || !inet_ntop(AF_INET, val, addr, sizeof(addr)))
5128 return -1;
5129
5130 if (pretty_print)
5131 res = os_snprintf(buf, buflen, "%s=%s\n", name, addr);
5132 else
5133 res = os_snprintf(buf, buflen, "%s", addr);
5134
5135 if (os_snprintf_error(buflen, res))
5136 res = -1;
5137
5138 return res;
5139}
5140#endif /* CONFIG_P2P */
5141
5142
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005143#ifdef OFFSET
5144#undef OFFSET
5145#endif /* OFFSET */
5146/* OFFSET: Get offset of a variable within the wpa_config structure */
5147#define OFFSET(v) ((void *) &((struct wpa_config *) 0)->v)
5148
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005149#define FUNC(f) #f, wpa_config_process_ ## f, NULL, OFFSET(f), NULL, NULL
5150#define FUNC_NO_VAR(f) #f, wpa_config_process_ ## f, NULL, NULL, NULL, NULL
5151#define _INT(f) #f, wpa_global_config_parse_int, wpa_config_get_int, OFFSET(f)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005152#define INT(f) _INT(f), NULL, NULL
5153#define INT_RANGE(f, min, max) _INT(f), (void *) min, (void *) max
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005154#define _STR(f) #f, wpa_global_config_parse_str, wpa_config_get_str, OFFSET(f)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005155#define STR(f) _STR(f), NULL, NULL
5156#define STR_RANGE(f, min, max) _STR(f), (void *) min, (void *) max
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005157#define BIN(f) #f, wpa_global_config_parse_bin, NULL, OFFSET(f), NULL, NULL
Dmitry Shmidtf73259c2015-03-17 11:00:54 -07005158#define IPV4(f) #f, wpa_global_config_parse_ipv4, wpa_config_get_ipv4, \
5159 OFFSET(f), NULL, NULL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005160
5161static const struct global_parse_data global_fields[] = {
5162#ifdef CONFIG_CTRL_IFACE
5163 { STR(ctrl_interface), 0 },
Dmitry Shmidt56052862013-10-04 10:23:25 -07005164 { FUNC_NO_VAR(no_ctrl_interface), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005165 { STR(ctrl_interface_group), 0 } /* deprecated */,
5166#endif /* CONFIG_CTRL_IFACE */
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07005167#ifdef CONFIG_MACSEC
5168 { INT_RANGE(eapol_version, 1, 3), 0 },
5169#else /* CONFIG_MACSEC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005170 { INT_RANGE(eapol_version, 1, 2), 0 },
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07005171#endif /* CONFIG_MACSEC */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005172 { INT(ap_scan), 0 },
Hai Shalom899fcc72020-10-19 14:38:18 -07005173 { FUNC(bgscan), CFG_CHANGED_BGSCAN },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005174#ifdef CONFIG_MESH
5175 { INT(user_mpm), 0 },
5176 { INT_RANGE(max_peer_links, 0, 255), 0 },
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08005177 { INT(mesh_max_inactivity), 0 },
Hai Shaloma20dcd72022-02-04 13:43:00 -08005178 { INT_RANGE(mesh_fwding, 0, 1), 0 },
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005179 { INT(dot11RSNASAERetransPeriod), 0 },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005180#endif /* CONFIG_MESH */
Dmitry Shmidt04949592012-07-19 12:16:46 -07005181 { INT(disable_scan_offload), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005182 { INT(fast_reauth), 0 },
5183 { STR(opensc_engine_path), 0 },
5184 { STR(pkcs11_engine_path), 0 },
5185 { STR(pkcs11_module_path), 0 },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005186 { STR(openssl_ciphers), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07005187 { STR(pcsc_reader), 0 },
5188 { STR(pcsc_pin), 0 },
Dmitry Shmidt051af732013-10-22 13:52:46 -07005189 { INT(external_sim), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005190 { STR(driver_param), 0 },
5191 { INT(dot11RSNAConfigPMKLifetime), 0 },
5192 { INT(dot11RSNAConfigPMKReauthThreshold), 0 },
5193 { INT(dot11RSNAConfigSATimeout), 0 },
5194#ifndef CONFIG_NO_CONFIG_WRITE
5195 { INT(update_config), 0 },
5196#endif /* CONFIG_NO_CONFIG_WRITE */
5197 { FUNC_NO_VAR(load_dynamic_eap), 0 },
5198#ifdef CONFIG_WPS
5199 { FUNC(uuid), CFG_CHANGED_UUID },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005200 { INT_RANGE(auto_uuid, 0, 1), 0 },
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07005201 { STR_RANGE(device_name, 0, WPS_DEV_NAME_MAX_LEN),
5202 CFG_CHANGED_DEVICE_NAME },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005203 { STR_RANGE(manufacturer, 0, 64), CFG_CHANGED_WPS_STRING },
5204 { STR_RANGE(model_name, 0, 32), CFG_CHANGED_WPS_STRING },
5205 { STR_RANGE(model_number, 0, 32), CFG_CHANGED_WPS_STRING },
5206 { STR_RANGE(serial_number, 0, 32), CFG_CHANGED_WPS_STRING },
5207 { FUNC(device_type), CFG_CHANGED_DEVICE_TYPE },
5208 { FUNC(os_version), CFG_CHANGED_OS_VERSION },
5209 { STR(config_methods), CFG_CHANGED_CONFIG_METHODS },
5210 { INT_RANGE(wps_cred_processing, 0, 2), 0 },
Hai Shalom021b0b52019-04-10 11:17:58 -07005211 { INT_RANGE(wps_cred_add_sae, 0, 1), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07005212 { FUNC(wps_vendor_ext_m1), CFG_CHANGED_VENDOR_EXTENSION },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005213#endif /* CONFIG_WPS */
5214#ifdef CONFIG_P2P
5215 { FUNC(sec_device_type), CFG_CHANGED_SEC_DEVICE_TYPE },
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005216 { INT(p2p_listen_reg_class), CFG_CHANGED_P2P_LISTEN_CHANNEL },
5217 { INT(p2p_listen_channel), CFG_CHANGED_P2P_LISTEN_CHANNEL },
Dmitry Shmidt56052862013-10-04 10:23:25 -07005218 { INT(p2p_oper_reg_class), CFG_CHANGED_P2P_OPER_CHANNEL },
5219 { INT(p2p_oper_channel), CFG_CHANGED_P2P_OPER_CHANNEL },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005220 { INT_RANGE(p2p_go_intent, 0, 15), 0 },
5221 { STR(p2p_ssid_postfix), CFG_CHANGED_P2P_SSID_POSTFIX },
5222 { INT_RANGE(persistent_reconnect, 0, 1), 0 },
5223 { INT_RANGE(p2p_intra_bss, 0, 1), CFG_CHANGED_P2P_INTRA_BSS },
5224 { INT(p2p_group_idle), 0 },
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005225 { INT_RANGE(p2p_go_freq_change_policy, 0, P2P_GO_FREQ_MOVE_MAX), 0 },
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07005226 { INT_RANGE(p2p_passphrase_len, 8, 63),
5227 CFG_CHANGED_P2P_PASSPHRASE_LEN },
Dmitry Shmidt04949592012-07-19 12:16:46 -07005228 { FUNC(p2p_pref_chan), CFG_CHANGED_P2P_PREF_CHAN },
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005229 { FUNC(p2p_no_go_freq), CFG_CHANGED_P2P_PREF_CHAN },
5230 { INT_RANGE(p2p_add_cli_chan, 0, 1), 0 },
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005231 { INT_RANGE(p2p_optimize_listen_chan, 0, 1), 0 },
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005232 { INT(p2p_go_ht40), 0 },
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005233 { INT(p2p_go_vht), 0 },
Hai Shalom74f70d42019-02-11 14:42:39 -08005234 { INT(p2p_go_he), 0 },
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08005235 { INT(p2p_go_edmg), 0 },
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005236 { INT(p2p_disabled), 0 },
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005237 { INT_RANGE(p2p_go_ctwindow, 0, 127), 0 },
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005238 { INT(p2p_no_group_iface), 0 },
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005239 { INT_RANGE(p2p_ignore_shared_freq, 0, 1), 0 },
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005240 { IPV4(ip_addr_go), 0 },
5241 { IPV4(ip_addr_mask), 0 },
5242 { IPV4(ip_addr_start), 0 },
5243 { IPV4(ip_addr_end), 0 },
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005244 { INT_RANGE(p2p_cli_probe, 0, 1), 0 },
Hai Shalom39ba6fc2019-01-22 12:40:38 -08005245 { INT(p2p_device_random_mac_addr), 0 },
5246 { FUNC(p2p_device_persistent_mac_addr), 0 },
5247 { INT(p2p_interface_random_mac_addr), 0 },
Hai Shalom899fcc72020-10-19 14:38:18 -07005248 { INT(p2p_6ghz_disable), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005249#endif /* CONFIG_P2P */
5250 { FUNC(country), CFG_CHANGED_COUNTRY },
5251 { INT(bss_max_count), 0 },
5252 { INT(bss_expiration_age), 0 },
5253 { INT(bss_expiration_scan_count), 0 },
5254 { INT_RANGE(filter_ssids, 0, 1), 0 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005255 { INT_RANGE(filter_rssi, -100, 0), 0 },
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005256 { INT(max_num_sta), 0 },
Roshan Pius3a1667e2018-07-03 15:17:14 -07005257 { INT_RANGE(ap_isolate, 0, 1), 0 },
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005258 { INT_RANGE(disassoc_low_ack, 0, 1), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07005259#ifdef CONFIG_HS20
5260 { INT_RANGE(hs20, 0, 1), 0 },
5261#endif /* CONFIG_HS20 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005262 { INT_RANGE(interworking, 0, 1), 0 },
5263 { FUNC(hessid), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07005264 { INT_RANGE(access_network_type, 0, 15), 0 },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005265 { INT_RANGE(go_interworking, 0, 1), 0 },
5266 { INT_RANGE(go_access_network_type, 0, 15), 0 },
5267 { INT_RANGE(go_internet, 0, 1), 0 },
5268 { INT_RANGE(go_venue_group, 0, 255), 0 },
5269 { INT_RANGE(go_venue_type, 0, 255), 0 },
Dmitry Shmidt04949592012-07-19 12:16:46 -07005270 { INT_RANGE(pbc_in_m1, 0, 1), 0 },
5271 { STR(autoscan), 0 },
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005272 { INT_RANGE(wps_nfc_dev_pw_id, 0x10, 0xffff),
5273 CFG_CHANGED_NFC_PASSWORD_TOKEN },
5274 { BIN(wps_nfc_dh_pubkey), CFG_CHANGED_NFC_PASSWORD_TOKEN },
5275 { BIN(wps_nfc_dh_privkey), CFG_CHANGED_NFC_PASSWORD_TOKEN },
5276 { BIN(wps_nfc_dev_pw), CFG_CHANGED_NFC_PASSWORD_TOKEN },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005277 { STR(ext_password_backend), CFG_CHANGED_EXT_PW_BACKEND },
5278 { INT(p2p_go_max_inactivity), 0 },
5279 { INT_RANGE(auto_interworking, 0, 1), 0 },
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005280 { INT(okc), 0 },
5281 { INT(pmf), 0 },
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005282 { FUNC(sae_groups), 0 },
Hai Shalomfdcde762020-04-02 11:19:20 -07005283 { INT_RANGE(sae_pwe, 0, 3), 0 },
Hai Shalomc3565922019-10-28 11:58:20 -07005284 { INT_RANGE(sae_pmkid_in_assoc, 0, 1), 0 },
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005285 { INT(dtim_period), 0 },
5286 { INT(beacon_int), 0 },
Hai Shaloma20dcd72022-02-04 13:43:00 -08005287 { FUNC(ap_assocresp_elements), 0 },
Dmitry Shmidt0ccb66e2013-03-29 16:41:28 -07005288 { FUNC(ap_vendor_elements), 0 },
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005289 { INT_RANGE(ignore_old_scan_res, 0, 1), 0 },
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07005290 { FUNC(freq_list), 0 },
Hai Shalom60840252021-02-19 19:02:11 -08005291 { FUNC(initial_freq_list), 0},
Dmitry Shmidtea69e842013-05-13 14:52:28 -07005292 { INT(scan_cur_freq), 0 },
Hai Shalom60840252021-02-19 19:02:11 -08005293 { INT(scan_res_valid_for_connect), 0},
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07005294 { INT(sched_scan_interval), 0 },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005295 { INT(sched_scan_start_delay), 0 },
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005296 { INT(tdls_external_control), 0},
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08005297 { STR(osu_dir), 0 },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005298 { STR(wowlan_triggers), CFG_CHANGED_WOWLAN_TRIGGERS },
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07005299 { INT(p2p_search_delay), 0},
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005300 { INT(mac_addr), 0 },
5301 { INT(rand_addr_lifetime), 0 },
5302 { INT(preassoc_mac_addr), 0 },
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005303 { INT(key_mgmt_offload), 0},
Dmitry Shmidt807291d2015-01-27 13:40:23 -08005304 { INT(passive_scan), 0 },
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005305 { INT(reassoc_same_bss_optim), 0 },
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07005306 { INT(wps_priority), 0},
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005307#ifdef CONFIG_FST
5308 { STR_RANGE(fst_group_id, 1, FST_MAX_GROUP_ID_LEN), 0 },
5309 { INT_RANGE(fst_priority, 1, FST_MAX_PRIO_VALUE), 0 },
5310 { INT_RANGE(fst_llt, 1, FST_MAX_LLT_MS), 0 },
5311#endif /* CONFIG_FST */
Paul Stewart092955c2017-02-06 09:13:09 -08005312 { INT_RANGE(cert_in_cb, 0, 1), 0 },
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005313 { INT_RANGE(wpa_rsc_relaxation, 0, 1), 0 },
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005314 { STR(sched_scan_plans), CFG_CHANGED_SCHED_SCAN_PLANS },
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005315#ifdef CONFIG_MBO
5316 { STR(non_pref_chan), 0 },
5317 { INT_RANGE(mbo_cell_capa, MBO_CELL_CAPA_AVAILABLE,
5318 MBO_CELL_CAPA_NOT_SUPPORTED), 0 },
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005319 { INT_RANGE(disassoc_imminent_rssi_threshold, -120, 0), 0 },
5320 { INT_RANGE(oce, 0, 3), 0 },
5321#endif /* CONFIG_MBO */
Sunil Ravi84bb3e12021-06-10 09:52:05 -07005322 { INT_RANGE(btm_offload, 0, 1), CFG_CHANGED_DISABLE_BTM_NOTIFY },
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07005323 { INT(gas_address3), 0 },
Dmitry Shmidt7d175302016-09-06 13:11:34 -07005324 { INT_RANGE(ftm_responder, 0, 1), 0 },
5325 { INT_RANGE(ftm_initiator, 0, 1), 0 },
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005326 { INT(gas_rand_addr_lifetime), 0 },
5327 { INT_RANGE(gas_rand_mac_addr, 0, 2), 0 },
Hai Shalomc3565922019-10-28 11:58:20 -07005328#ifdef CONFIG_DPP
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005329 { INT_RANGE(dpp_config_processing, 0, 2), 0 },
Hai Shalomc3565922019-10-28 11:58:20 -07005330 { STR(dpp_name), 0 },
5331 { STR(dpp_mud_url), 0 },
Sunil Ravi89eba102022-09-13 21:04:37 -07005332 { STR(dpp_extra_conf_req_name), 0 },
5333 { STR(dpp_extra_conf_req_value), 0 },
5334 { INT_RANGE(dpp_connector_privacy_default, 0, 1), 0 },
Hai Shalomc3565922019-10-28 11:58:20 -07005335#endif /* CONFIG_DPP */
Hai Shalom39ba6fc2019-01-22 12:40:38 -08005336 { INT_RANGE(coloc_intf_reporting, 0, 1), 0 },
Jimmy Chen0bb4f862019-03-21 18:41:47 +08005337 { INT_RANGE(bss_no_flush_when_down, 0, 1), 0 },
Hai Shalom81f62d82019-07-22 12:10:00 -07005338#ifdef CONFIG_WNM
5339 { INT_RANGE(disable_btm, 0, 1), CFG_CHANGED_DISABLE_BTM },
Hai Shalomfdcde762020-04-02 11:19:20 -07005340 { INT_RANGE(extended_key_id, 0, 1), 0 },
Hai Shalom81f62d82019-07-22 12:10:00 -07005341#endif /* CONFIG_WNM */
Hai Shalom60840252021-02-19 19:02:11 -08005342 { INT_RANGE(wowlan_disconnect_on_deinit, 0, 1), 0},
5343#ifdef CONFIG_PASN
5344#ifdef CONFIG_TESTING_OPTIONS
5345 { INT_RANGE(force_kdk_derivation, 0, 1), 0 },
Hai Shaloma20dcd72022-02-04 13:43:00 -08005346 { INT_RANGE(pasn_corrupt_mic, 0, 1), 0 },
Hai Shalom60840252021-02-19 19:02:11 -08005347#endif /* CONFIG_TESTING_OPTIONS */
5348#endif /* CONFIG_PASN */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005349};
5350
5351#undef FUNC
5352#undef _INT
5353#undef INT
5354#undef INT_RANGE
5355#undef _STR
5356#undef STR
5357#undef STR_RANGE
Dmitry Shmidt04949592012-07-19 12:16:46 -07005358#undef BIN
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005359#undef IPV4
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005360#define NUM_GLOBAL_FIELDS ARRAY_SIZE(global_fields)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005361
5362
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005363int wpa_config_dump_values(struct wpa_config *config, char *buf, size_t buflen)
5364{
5365 int result = 0;
5366 size_t i;
5367
5368 for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
5369 const struct global_parse_data *field = &global_fields[i];
5370 int tmp;
5371
5372 if (!field->get)
5373 continue;
5374
5375 tmp = field->get(field->name, config, (long) field->param1,
5376 buf, buflen, 1);
5377 if (tmp < 0)
5378 return -1;
5379 buf += tmp;
5380 buflen -= tmp;
5381 result += tmp;
5382 }
5383 return result;
5384}
5385
5386
5387int wpa_config_get_value(const char *name, struct wpa_config *config,
5388 char *buf, size_t buflen)
5389{
5390 size_t i;
5391
5392 for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
5393 const struct global_parse_data *field = &global_fields[i];
5394
5395 if (os_strcmp(name, field->name) != 0)
5396 continue;
5397 if (!field->get)
5398 break;
5399 return field->get(name, config, (long) field->param1,
5400 buf, buflen, 0);
5401 }
5402
5403 return -1;
5404}
5405
5406
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005407int wpa_config_get_num_global_field_names(void)
5408{
5409 return NUM_GLOBAL_FIELDS;
5410}
5411
5412
5413const char * wpa_config_get_global_field_name(unsigned int i, int *no_var)
5414{
5415 if (i >= NUM_GLOBAL_FIELDS)
5416 return NULL;
5417
5418 if (no_var)
5419 *no_var = !global_fields[i].param1;
5420 return global_fields[i].name;
5421}
5422
5423
Hai Shalom899fcc72020-10-19 14:38:18 -07005424/**
5425 * wpa_config_process_global - Set a variable in global configuration
5426 * @config: Pointer to global configuration data
5427 * @pos: Name and value in the format "{name}={value}"
5428 * @line: Line number in configuration file or 0 if not used
5429 * Returns: 0 on success with a possible change in value, 1 on success with no
5430 * change to previously configured value, or -1 on failure
5431 *
5432 * This function can be used to set global configuration variables based on
5433 * both the configuration file and management interface input. The value
5434 * parameter must be in the same format as the text-based configuration file is
5435 * using. For example, strings are using double quotation marks.
5436 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005437int wpa_config_process_global(struct wpa_config *config, char *pos, int line)
5438{
5439 size_t i;
5440 int ret = 0;
5441
5442 for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
5443 const struct global_parse_data *field = &global_fields[i];
5444 size_t flen = os_strlen(field->name);
5445 if (os_strncmp(pos, field->name, flen) != 0 ||
5446 pos[flen] != '=')
5447 continue;
5448
Hai Shalom899fcc72020-10-19 14:38:18 -07005449 ret = field->parser(field, config, line, pos + flen + 1);
5450 if (ret < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005451 wpa_printf(MSG_ERROR, "Line %d: failed to "
5452 "parse '%s'.", line, pos);
5453 ret = -1;
5454 }
Hai Shalom899fcc72020-10-19 14:38:18 -07005455 if (ret == 1)
5456 break;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08005457 if (field->changed_flag == CFG_CHANGED_NFC_PASSWORD_TOKEN)
5458 config->wps_nfc_pw_from_config = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005459 config->changed_parameters |= field->changed_flag;
5460 break;
5461 }
5462 if (i == NUM_GLOBAL_FIELDS) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005463#ifdef CONFIG_AP
Hai Shalom899fcc72020-10-19 14:38:18 -07005464 if (os_strncmp(pos, "tx_queue_", 9) == 0) {
5465 char *tmp = os_strchr(pos, '=');
5466
5467 if (!tmp) {
5468 if (line < 0)
5469 wpa_printf(MSG_ERROR,
5470 "Line %d: invalid line %s",
5471 line, pos);
5472 return -1;
5473 }
5474 *tmp++ = '\0';
5475 if (hostapd_config_tx_queue(config->tx_queue, pos,
5476 tmp)) {
5477 wpa_printf(MSG_ERROR,
5478 "Line %d: invalid TX queue item",
5479 line);
5480 return -1;
5481 }
5482 }
5483
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005484 if (os_strncmp(pos, "wmm_ac_", 7) == 0) {
5485 char *tmp = os_strchr(pos, '=');
5486 if (tmp == NULL) {
5487 if (line < 0)
5488 return -1;
5489 wpa_printf(MSG_ERROR, "Line %d: invalid line "
5490 "'%s'", line, pos);
5491 return -1;
5492 }
5493 *tmp++ = '\0';
5494 if (hostapd_config_wmm_ac(config->wmm_ac_params, pos,
5495 tmp)) {
5496 wpa_printf(MSG_ERROR, "Line %d: invalid WMM "
5497 "AC item", line);
5498 return -1;
5499 }
Hai Shalomc3565922019-10-28 11:58:20 -07005500 return ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005501 }
5502#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005503 if (line < 0)
5504 return -1;
5505 wpa_printf(MSG_ERROR, "Line %d: unknown global field '%s'.",
5506 line, pos);
5507 ret = -1;
5508 }
5509
5510 return ret;
5511}