]> git.ipfire.org Git - thirdparty/hostap.git/blame - wpa_supplicant/config.c
tests: Layer 2 Update frame behavior in mac80211
[thirdparty/hostap.git] / wpa_supplicant / config.c
CommitLineData
6fc6879b
JM
1/*
2 * WPA Supplicant / Configuration parser and common functions
b99c4cad 3 * Copyright (c) 2003-2019, Jouni Malinen <j@w1.fi>
6fc6879b 4 *
0f3d578e
JM
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
6fc6879b
JM
7 */
8
9#include "includes.h"
10
11#include "common.h"
121adf9c 12#include "utils/uuid.h"
25ef8529 13#include "utils/ip_addr.h"
65dfa872 14#include "common/ieee802_1x_defs.h"
03da66bd 15#include "crypto/sha1.h"
3acb5005 16#include "rsn_supp/wpa.h"
6fc6879b 17#include "eap_peer/eap.h"
21d996f7 18#include "p2p/p2p.h"
76ca15b7 19#include "fst/fst.h"
6fc6879b
JM
20#include "config.h"
21
22
23#if !defined(CONFIG_CTRL_IFACE) && defined(CONFIG_NO_CONFIG_WRITE)
24#define NO_CONFIG_WRITE
25#endif
26
27/*
28 * Structure for network configuration parsing. This data is used to implement
29 * a generic parser for each network block variable. The table of configuration
30 * variables is defined below in this file (ssid_fields[]).
31 */
32struct parse_data {
33 /* Configuration variable name */
34 char *name;
35
5cd317d3
BKB
36 /* Parser function for this variable. The parser functions return 0 or 1
37 * to indicate success. Value 0 indicates that the parameter value may
38 * have changed while value 1 means that the value did not change.
39 * Error cases (failure to parse the string) are indicated by returning
40 * -1. */
6fc6879b
JM
41 int (*parser)(const struct parse_data *data, struct wpa_ssid *ssid,
42 int line, const char *value);
43
44#ifndef NO_CONFIG_WRITE
45 /* Writer function (i.e., to get the variable in text format from
46 * internal presentation). */
47 char * (*writer)(const struct parse_data *data, struct wpa_ssid *ssid);
48#endif /* NO_CONFIG_WRITE */
49
50 /* Variable specific parameters for the parser. */
51 void *param1, *param2, *param3, *param4;
52
53 /* 0 = this variable can be included in debug output and ctrl_iface
54 * 1 = this variable contains key/private data and it must not be
55 * included in debug output unless explicitly requested. In
56 * addition, this variable will not be readable through the
57 * ctrl_iface.
58 */
59 int key_data;
60};
61
62
6fc6879b
JM
63static int wpa_config_parse_str(const struct parse_data *data,
64 struct wpa_ssid *ssid,
65 int line, const char *value)
66{
5cd317d3 67 size_t res_len, *dst_len, prev_len;
6fc6879b
JM
68 char **dst, *tmp;
69
b56c0546
JM
70 if (os_strcmp(value, "NULL") == 0) {
71 wpa_printf(MSG_DEBUG, "Unset configuration string '%s'",
72 data->name);
73 tmp = NULL;
74 res_len = 0;
75 goto set;
76 }
77
6fc6879b
JM
78 tmp = wpa_config_parse_string(value, &res_len);
79 if (tmp == NULL) {
80 wpa_printf(MSG_ERROR, "Line %d: failed to parse %s '%s'.",
81 line, data->name,
82 data->key_data ? "[KEY DATA REMOVED]" : value);
83 return -1;
84 }
85
86 if (data->key_data) {
87 wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
88 (u8 *) tmp, res_len);
89 } else {
90 wpa_hexdump_ascii(MSG_MSGDUMP, data->name,
91 (u8 *) tmp, res_len);
92 }
93
94 if (data->param3 && res_len < (size_t) data->param3) {
95 wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
96 "min_len=%ld)", line, data->name,
97 (unsigned long) res_len, (long) data->param3);
98 os_free(tmp);
99 return -1;
100 }
101
102 if (data->param4 && res_len > (size_t) data->param4) {
103 wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
104 "max_len=%ld)", line, data->name,
105 (unsigned long) res_len, (long) data->param4);
106 os_free(tmp);
107 return -1;
108 }
109
b56c0546 110set:
6fc6879b
JM
111 dst = (char **) (((u8 *) ssid) + (long) data->param1);
112 dst_len = (size_t *) (((u8 *) ssid) + (long) data->param2);
5cd317d3
BKB
113
114 if (data->param2)
115 prev_len = *dst_len;
116 else if (*dst)
117 prev_len = os_strlen(*dst);
118 else
119 prev_len = 0;
120 if ((*dst == NULL && tmp == NULL) ||
121 (*dst && tmp && prev_len == res_len &&
122 os_memcmp(*dst, tmp, res_len) == 0)) {
123 /* No change to the previously configured value */
124 os_free(tmp);
125 return 1;
126 }
127
6fc6879b
JM
128 os_free(*dst);
129 *dst = tmp;
130 if (data->param2)
131 *dst_len = res_len;
132
133 return 0;
134}
135
136
137#ifndef NO_CONFIG_WRITE
6fc6879b
JM
138static char * wpa_config_write_string_ascii(const u8 *value, size_t len)
139{
140 char *buf;
141
142 buf = os_malloc(len + 3);
143 if (buf == NULL)
144 return NULL;
145 buf[0] = '"';
146 os_memcpy(buf + 1, value, len);
147 buf[len + 1] = '"';
148 buf[len + 2] = '\0';
149
150 return buf;
151}
152
153
154static char * wpa_config_write_string_hex(const u8 *value, size_t len)
155{
156 char *buf;
157
158 buf = os_zalloc(2 * len + 1);
159 if (buf == NULL)
160 return NULL;
161 wpa_snprintf_hex(buf, 2 * len + 1, value, len);
162
163 return buf;
164}
165
166
167static char * wpa_config_write_string(const u8 *value, size_t len)
168{
169 if (value == NULL)
170 return NULL;
171
172 if (is_hex(value, len))
173 return wpa_config_write_string_hex(value, len);
174 else
175 return wpa_config_write_string_ascii(value, len);
176}
177
178
179static char * wpa_config_write_str(const struct parse_data *data,
180 struct wpa_ssid *ssid)
181{
182 size_t len;
183 char **src;
184
185 src = (char **) (((u8 *) ssid) + (long) data->param1);
186 if (*src == NULL)
187 return NULL;
188
189 if (data->param2)
190 len = *((size_t *) (((u8 *) ssid) + (long) data->param2));
191 else
192 len = os_strlen(*src);
193
194 return wpa_config_write_string((const u8 *) *src, len);
195}
196#endif /* NO_CONFIG_WRITE */
197
198
199static int wpa_config_parse_int(const struct parse_data *data,
200 struct wpa_ssid *ssid,
201 int line, const char *value)
202{
eae3a584
JB
203 int val, *dst;
204 char *end;
6fc6879b
JM
205
206 dst = (int *) (((u8 *) ssid) + (long) data->param1);
eae3a584
JB
207 val = strtol(value, &end, 0);
208 if (*end) {
209 wpa_printf(MSG_ERROR, "Line %d: invalid number \"%s\"",
210 line, value);
211 return -1;
212 }
5cd317d3
BKB
213
214 if (*dst == val)
215 return 1;
eae3a584 216 *dst = val;
6fc6879b
JM
217 wpa_printf(MSG_MSGDUMP, "%s=%d (0x%x)", data->name, *dst, *dst);
218
219 if (data->param3 && *dst < (long) data->param3) {
220 wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
221 "min_value=%ld)", line, data->name, *dst,
222 (long) data->param3);
223 *dst = (long) data->param3;
224 return -1;
225 }
226
227 if (data->param4 && *dst > (long) data->param4) {
228 wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
229 "max_value=%ld)", line, data->name, *dst,
230 (long) data->param4);
231 *dst = (long) data->param4;
232 return -1;
233 }
234
235 return 0;
236}
237
238
239#ifndef NO_CONFIG_WRITE
240static char * wpa_config_write_int(const struct parse_data *data,
241 struct wpa_ssid *ssid)
242{
243 int *src, res;
244 char *value;
245
246 src = (int *) (((u8 *) ssid) + (long) data->param1);
247
248 value = os_malloc(20);
249 if (value == NULL)
250 return NULL;
251 res = os_snprintf(value, 20, "%d", *src);
d85e1fc8 252 if (os_snprintf_error(20, res)) {
6fc6879b
JM
253 os_free(value);
254 return NULL;
255 }
256 value[20 - 1] = '\0';
257 return value;
258}
259#endif /* NO_CONFIG_WRITE */
260
261
b3d6a0a8
ST
262static int wpa_config_parse_addr_list(const struct parse_data *data,
263 int line, const char *value,
264 u8 **list, size_t *num, char *name,
79cd993a 265 u8 abort_on_error, u8 masked)
b3d6a0a8
ST
266{
267 const char *pos;
79cd993a 268 u8 *buf, *n, addr[2 * ETH_ALEN];
b3d6a0a8
ST
269 size_t count;
270
271 buf = NULL;
272 count = 0;
273
274 pos = value;
275 while (pos && *pos) {
276 while (*pos == ' ')
277 pos++;
278
79cd993a 279 if (hwaddr_masked_aton(pos, addr, &addr[ETH_ALEN], masked)) {
b3d6a0a8
ST
280 if (abort_on_error || count == 0) {
281 wpa_printf(MSG_ERROR,
282 "Line %d: Invalid %s address '%s'",
283 line, name, value);
284 os_free(buf);
285 return -1;
286 }
287 /* continue anyway since this could have been from a
288 * truncated configuration file line */
289 wpa_printf(MSG_INFO,
290 "Line %d: Ignore likely truncated %s address '%s'",
291 line, name, pos);
292 } else {
79cd993a 293 n = os_realloc_array(buf, count + 1, 2 * ETH_ALEN);
b3d6a0a8
ST
294 if (n == NULL) {
295 os_free(buf);
296 return -1;
297 }
298 buf = n;
79cd993a
ST
299 os_memmove(buf + 2 * ETH_ALEN, buf,
300 count * 2 * ETH_ALEN);
301 os_memcpy(buf, addr, 2 * ETH_ALEN);
b3d6a0a8 302 count++;
79cd993a
ST
303 wpa_printf(MSG_MSGDUMP,
304 "%s: addr=" MACSTR " mask=" MACSTR,
305 name, MAC2STR(addr),
306 MAC2STR(&addr[ETH_ALEN]));
b3d6a0a8
ST
307 }
308
309 pos = os_strchr(pos, ' ');
310 }
311
312 os_free(*list);
313 *list = buf;
314 *num = count;
315
316 return 0;
317}
318
319
320#ifndef NO_CONFIG_WRITE
321static char * wpa_config_write_addr_list(const struct parse_data *data,
322 const u8 *list, size_t num, char *name)
323{
324 char *value, *end, *pos;
325 int res;
326 size_t i;
327
328 if (list == NULL || num == 0)
329 return NULL;
330
79cd993a 331 value = os_malloc(2 * 20 * num);
b3d6a0a8
ST
332 if (value == NULL)
333 return NULL;
334 pos = value;
79cd993a 335 end = value + 2 * 20 * num;
b3d6a0a8
ST
336
337 for (i = num; i > 0; i--) {
79cd993a
ST
338 const u8 *a = list + (i - 1) * 2 * ETH_ALEN;
339 const u8 *m = a + ETH_ALEN;
340
341 if (i < num)
342 *pos++ = ' ';
343 res = hwaddr_mask_txt(pos, end - pos, a, m);
344 if (res < 0) {
b3d6a0a8
ST
345 os_free(value);
346 return NULL;
347 }
348 pos += res;
349 }
350
b3d6a0a8
ST
351 return value;
352}
353#endif /* NO_CONFIG_WRITE */
354
6fc6879b
JM
355static int wpa_config_parse_bssid(const struct parse_data *data,
356 struct wpa_ssid *ssid, int line,
357 const char *value)
358{
c0a321c5
WJL
359 if (value[0] == '\0' || os_strcmp(value, "\"\"") == 0 ||
360 os_strcmp(value, "any") == 0) {
361 ssid->bssid_set = 0;
362 wpa_printf(MSG_MSGDUMP, "BSSID any");
363 return 0;
364 }
6fc6879b
JM
365 if (hwaddr_aton(value, ssid->bssid)) {
366 wpa_printf(MSG_ERROR, "Line %d: Invalid BSSID '%s'.",
367 line, value);
368 return -1;
369 }
370 ssid->bssid_set = 1;
371 wpa_hexdump(MSG_MSGDUMP, "BSSID", ssid->bssid, ETH_ALEN);
372 return 0;
373}
374
375
376#ifndef NO_CONFIG_WRITE
377static char * wpa_config_write_bssid(const struct parse_data *data,
378 struct wpa_ssid *ssid)
379{
380 char *value;
381 int res;
382
383 if (!ssid->bssid_set)
384 return NULL;
385
386 value = os_malloc(20);
387 if (value == NULL)
388 return NULL;
389 res = os_snprintf(value, 20, MACSTR, MAC2STR(ssid->bssid));
d85e1fc8 390 if (os_snprintf_error(20, res)) {
6fc6879b
JM
391 os_free(value);
392 return NULL;
393 }
394 value[20 - 1] = '\0';
395 return value;
396}
397#endif /* NO_CONFIG_WRITE */
398
399
43a356b2
PK
400static int wpa_config_parse_bssid_hint(const struct parse_data *data,
401 struct wpa_ssid *ssid, int line,
402 const char *value)
403{
404 if (value[0] == '\0' || os_strcmp(value, "\"\"") == 0 ||
405 os_strcmp(value, "any") == 0) {
406 ssid->bssid_hint_set = 0;
407 wpa_printf(MSG_MSGDUMP, "BSSID hint any");
408 return 0;
409 }
410 if (hwaddr_aton(value, ssid->bssid_hint)) {
411 wpa_printf(MSG_ERROR, "Line %d: Invalid BSSID hint '%s'.",
412 line, value);
413 return -1;
414 }
415 ssid->bssid_hint_set = 1;
416 wpa_hexdump(MSG_MSGDUMP, "BSSID hint", ssid->bssid_hint, ETH_ALEN);
417 return 0;
418}
419
420
421#ifndef NO_CONFIG_WRITE
422static char * wpa_config_write_bssid_hint(const struct parse_data *data,
423 struct wpa_ssid *ssid)
424{
425 char *value;
426 int res;
427
428 if (!ssid->bssid_hint_set)
429 return NULL;
430
431 value = os_malloc(20);
432 if (!value)
433 return NULL;
434 res = os_snprintf(value, 20, MACSTR, MAC2STR(ssid->bssid_hint));
435 if (os_snprintf_error(20, res)) {
436 os_free(value);
437 return NULL;
438 }
439 return value;
440}
441#endif /* NO_CONFIG_WRITE */
442
443
b83e4554
ST
444static int wpa_config_parse_bssid_blacklist(const struct parse_data *data,
445 struct wpa_ssid *ssid, int line,
446 const char *value)
447{
448 return wpa_config_parse_addr_list(data, line, value,
449 &ssid->bssid_blacklist,
450 &ssid->num_bssid_blacklist,
79cd993a 451 "bssid_blacklist", 1, 1);
b83e4554
ST
452}
453
454
455#ifndef NO_CONFIG_WRITE
456static char * wpa_config_write_bssid_blacklist(const struct parse_data *data,
457 struct wpa_ssid *ssid)
458{
459 return wpa_config_write_addr_list(data, ssid->bssid_blacklist,
460 ssid->num_bssid_blacklist,
461 "bssid_blacklist");
462}
463#endif /* NO_CONFIG_WRITE */
464
465
466static int wpa_config_parse_bssid_whitelist(const struct parse_data *data,
467 struct wpa_ssid *ssid, int line,
468 const char *value)
469{
470 return wpa_config_parse_addr_list(data, line, value,
471 &ssid->bssid_whitelist,
472 &ssid->num_bssid_whitelist,
79cd993a 473 "bssid_whitelist", 1, 1);
b83e4554
ST
474}
475
476
477#ifndef NO_CONFIG_WRITE
478static char * wpa_config_write_bssid_whitelist(const struct parse_data *data,
479 struct wpa_ssid *ssid)
480{
481 return wpa_config_write_addr_list(data, ssid->bssid_whitelist,
482 ssid->num_bssid_whitelist,
483 "bssid_whitelist");
484}
485#endif /* NO_CONFIG_WRITE */
486
487
6fc6879b
JM
488static int wpa_config_parse_psk(const struct parse_data *data,
489 struct wpa_ssid *ssid, int line,
490 const char *value)
491{
9173b16f
JM
492#ifdef CONFIG_EXT_PASSWORD
493 if (os_strncmp(value, "ext:", 4) == 0) {
19c48da0 494 str_clear_free(ssid->passphrase);
9173b16f
JM
495 ssid->passphrase = NULL;
496 ssid->psk_set = 0;
497 os_free(ssid->ext_psk);
498 ssid->ext_psk = os_strdup(value + 4);
499 if (ssid->ext_psk == NULL)
500 return -1;
501 wpa_printf(MSG_DEBUG, "PSK: External password '%s'",
502 ssid->ext_psk);
503 return 0;
504 }
505#endif /* CONFIG_EXT_PASSWORD */
506
6fc6879b
JM
507 if (*value == '"') {
508#ifndef CONFIG_NO_PBKDF2
509 const char *pos;
510 size_t len;
511
512 value++;
513 pos = os_strrchr(value, '"');
514 if (pos)
515 len = pos - value;
516 else
517 len = os_strlen(value);
518 if (len < 8 || len > 63) {
519 wpa_printf(MSG_ERROR, "Line %d: Invalid passphrase "
520 "length %lu (expected: 8..63) '%s'.",
521 line, (unsigned long) len, value);
522 return -1;
523 }
524 wpa_hexdump_ascii_key(MSG_MSGDUMP, "PSK (ASCII passphrase)",
525 (u8 *) value, len);
73e4abb2
JM
526 if (has_ctrl_char((u8 *) value, len)) {
527 wpa_printf(MSG_ERROR,
528 "Line %d: Invalid passphrase character",
529 line);
530 return -1;
531 }
6fc6879b 532 if (ssid->passphrase && os_strlen(ssid->passphrase) == len &&
5cd317d3
BKB
533 os_memcmp(ssid->passphrase, value, len) == 0) {
534 /* No change to the previously configured value */
535 return 1;
536 }
6fc6879b 537 ssid->psk_set = 0;
19c48da0 538 str_clear_free(ssid->passphrase);
5e24dc8a 539 ssid->passphrase = dup_binstr(value, len);
6fc6879b
JM
540 if (ssid->passphrase == NULL)
541 return -1;
6fc6879b
JM
542 return 0;
543#else /* CONFIG_NO_PBKDF2 */
544 wpa_printf(MSG_ERROR, "Line %d: ASCII passphrase not "
545 "supported.", line);
546 return -1;
547#endif /* CONFIG_NO_PBKDF2 */
548 }
549
550 if (hexstr2bin(value, ssid->psk, PMK_LEN) ||
551 value[PMK_LEN * 2] != '\0') {
552 wpa_printf(MSG_ERROR, "Line %d: Invalid PSK '%s'.",
553 line, value);
554 return -1;
555 }
556
19c48da0 557 str_clear_free(ssid->passphrase);
6fc6879b
JM
558 ssid->passphrase = NULL;
559
560 ssid->psk_set = 1;
561 wpa_hexdump_key(MSG_MSGDUMP, "PSK", ssid->psk, PMK_LEN);
562 return 0;
563}
564
565
566#ifndef NO_CONFIG_WRITE
567static char * wpa_config_write_psk(const struct parse_data *data,
568 struct wpa_ssid *ssid)
569{
9173b16f
JM
570#ifdef CONFIG_EXT_PASSWORD
571 if (ssid->ext_psk) {
572 size_t len = 4 + os_strlen(ssid->ext_psk) + 1;
573 char *buf = os_malloc(len);
1d399771
JM
574 int res;
575
9173b16f
JM
576 if (buf == NULL)
577 return NULL;
1d399771
JM
578 res = os_snprintf(buf, len, "ext:%s", ssid->ext_psk);
579 if (os_snprintf_error(len, res)) {
580 os_free(buf);
581 buf = NULL;
582 }
9173b16f
JM
583 return buf;
584 }
585#endif /* CONFIG_EXT_PASSWORD */
586
6fc6879b
JM
587 if (ssid->passphrase)
588 return wpa_config_write_string_ascii(
589 (const u8 *) ssid->passphrase,
590 os_strlen(ssid->passphrase));
591
592 if (ssid->psk_set)
593 return wpa_config_write_string_hex(ssid->psk, PMK_LEN);
594
595 return NULL;
596}
597#endif /* NO_CONFIG_WRITE */
598
599
600static int wpa_config_parse_proto(const struct parse_data *data,
601 struct wpa_ssid *ssid, int line,
602 const char *value)
603{
604 int val = 0, last, errors = 0;
605 char *start, *end, *buf;
606
607 buf = os_strdup(value);
608 if (buf == NULL)
609 return -1;
610 start = buf;
611
612 while (*start != '\0') {
613 while (*start == ' ' || *start == '\t')
614 start++;
615 if (*start == '\0')
616 break;
617 end = start;
618 while (*end != ' ' && *end != '\t' && *end != '\0')
619 end++;
620 last = *end == '\0';
621 *end = '\0';
622 if (os_strcmp(start, "WPA") == 0)
623 val |= WPA_PROTO_WPA;
624 else if (os_strcmp(start, "RSN") == 0 ||
625 os_strcmp(start, "WPA2") == 0)
626 val |= WPA_PROTO_RSN;
df0f01d9
JM
627 else if (os_strcmp(start, "OSEN") == 0)
628 val |= WPA_PROTO_OSEN;
6fc6879b
JM
629 else {
630 wpa_printf(MSG_ERROR, "Line %d: invalid proto '%s'",
631 line, start);
632 errors++;
633 }
634
635 if (last)
636 break;
637 start = end + 1;
638 }
639 os_free(buf);
640
641 if (val == 0) {
642 wpa_printf(MSG_ERROR,
643 "Line %d: no proto values configured.", line);
644 errors++;
645 }
646
5cd317d3
BKB
647 if (!errors && ssid->proto == val)
648 return 1;
6fc6879b
JM
649 wpa_printf(MSG_MSGDUMP, "proto: 0x%x", val);
650 ssid->proto = val;
651 return errors ? -1 : 0;
652}
653
654
655#ifndef NO_CONFIG_WRITE
656static char * wpa_config_write_proto(const struct parse_data *data,
657 struct wpa_ssid *ssid)
658{
290ea6a7 659 int ret;
6fc6879b
JM
660 char *buf, *pos, *end;
661
3141b82c 662 pos = buf = os_zalloc(20);
6fc6879b
JM
663 if (buf == NULL)
664 return NULL;
3141b82c 665 end = buf + 20;
6fc6879b
JM
666
667 if (ssid->proto & WPA_PROTO_WPA) {
290ea6a7
JM
668 ret = os_snprintf(pos, end - pos, "%sWPA",
669 pos == buf ? "" : " ");
d85e1fc8 670 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
671 return buf;
672 pos += ret;
6fc6879b
JM
673 }
674
675 if (ssid->proto & WPA_PROTO_RSN) {
290ea6a7
JM
676 ret = os_snprintf(pos, end - pos, "%sRSN",
677 pos == buf ? "" : " ");
d85e1fc8 678 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
679 return buf;
680 pos += ret;
6fc6879b
JM
681 }
682
3141b82c 683 if (ssid->proto & WPA_PROTO_OSEN) {
290ea6a7
JM
684 ret = os_snprintf(pos, end - pos, "%sOSEN",
685 pos == buf ? "" : " ");
d85e1fc8 686 if (os_snprintf_error(end - pos, ret))
3141b82c
JM
687 return buf;
688 pos += ret;
3141b82c
JM
689 }
690
a1e46f32
JM
691 if (pos == buf) {
692 os_free(buf);
693 buf = NULL;
694 }
695
6fc6879b
JM
696 return buf;
697}
698#endif /* NO_CONFIG_WRITE */
699
700
701static int wpa_config_parse_key_mgmt(const struct parse_data *data,
702 struct wpa_ssid *ssid, int line,
703 const char *value)
704{
705 int val = 0, last, errors = 0;
706 char *start, *end, *buf;
707
708 buf = os_strdup(value);
709 if (buf == NULL)
710 return -1;
711 start = buf;
712
713 while (*start != '\0') {
714 while (*start == ' ' || *start == '\t')
715 start++;
716 if (*start == '\0')
717 break;
718 end = start;
719 while (*end != ' ' && *end != '\t' && *end != '\0')
720 end++;
721 last = *end == '\0';
722 *end = '\0';
723 if (os_strcmp(start, "WPA-PSK") == 0)
724 val |= WPA_KEY_MGMT_PSK;
725 else if (os_strcmp(start, "WPA-EAP") == 0)
726 val |= WPA_KEY_MGMT_IEEE8021X;
727 else if (os_strcmp(start, "IEEE8021X") == 0)
728 val |= WPA_KEY_MGMT_IEEE8021X_NO_WPA;
729 else if (os_strcmp(start, "NONE") == 0)
730 val |= WPA_KEY_MGMT_NONE;
731 else if (os_strcmp(start, "WPA-NONE") == 0)
732 val |= WPA_KEY_MGMT_WPA_NONE;
733#ifdef CONFIG_IEEE80211R
734 else if (os_strcmp(start, "FT-PSK") == 0)
735 val |= WPA_KEY_MGMT_FT_PSK;
736 else if (os_strcmp(start, "FT-EAP") == 0)
737 val |= WPA_KEY_MGMT_FT_IEEE8021X;
d8e8c992
JM
738#ifdef CONFIG_SHA384
739 else if (os_strcmp(start, "FT-EAP-SHA384") == 0)
740 val |= WPA_KEY_MGMT_FT_IEEE8021X_SHA384;
741#endif /* CONFIG_SHA384 */
6fc6879b 742#endif /* CONFIG_IEEE80211R */
56586197
JM
743 else if (os_strcmp(start, "WPA-PSK-SHA256") == 0)
744 val |= WPA_KEY_MGMT_PSK_SHA256;
745 else if (os_strcmp(start, "WPA-EAP-SHA256") == 0)
746 val |= WPA_KEY_MGMT_IEEE8021X_SHA256;
ad08c363
JM
747#ifdef CONFIG_WPS
748 else if (os_strcmp(start, "WPS") == 0)
749 val |= WPA_KEY_MGMT_WPS;
750#endif /* CONFIG_WPS */
c10347f2
JM
751#ifdef CONFIG_SAE
752 else if (os_strcmp(start, "SAE") == 0)
753 val |= WPA_KEY_MGMT_SAE;
754 else if (os_strcmp(start, "FT-SAE") == 0)
755 val |= WPA_KEY_MGMT_FT_SAE;
756#endif /* CONFIG_SAE */
df0f01d9
JM
757#ifdef CONFIG_HS20
758 else if (os_strcmp(start, "OSEN") == 0)
759 val |= WPA_KEY_MGMT_OSEN;
760#endif /* CONFIG_HS20 */
5e3b5197 761#ifdef CONFIG_SUITEB
666497c8
JM
762 else if (os_strcmp(start, "WPA-EAP-SUITE-B") == 0)
763 val |= WPA_KEY_MGMT_IEEE8021X_SUITE_B;
5e3b5197
JM
764#endif /* CONFIG_SUITEB */
765#ifdef CONFIG_SUITEB192
766 else if (os_strcmp(start, "WPA-EAP-SUITE-B-192") == 0)
767 val |= WPA_KEY_MGMT_IEEE8021X_SUITE_B_192;
768#endif /* CONFIG_SUITEB192 */
9b7a2b83
JM
769#ifdef CONFIG_FILS
770 else if (os_strcmp(start, "FILS-SHA256") == 0)
771 val |= WPA_KEY_MGMT_FILS_SHA256;
772 else if (os_strcmp(start, "FILS-SHA384") == 0)
773 val |= WPA_KEY_MGMT_FILS_SHA384;
774#ifdef CONFIG_IEEE80211R
775 else if (os_strcmp(start, "FT-FILS-SHA256") == 0)
776 val |= WPA_KEY_MGMT_FT_FILS_SHA256;
777 else if (os_strcmp(start, "FT-FILS-SHA384") == 0)
778 val |= WPA_KEY_MGMT_FT_FILS_SHA384;
779#endif /* CONFIG_IEEE80211R */
780#endif /* CONFIG_FILS */
a1ea1b45
JM
781#ifdef CONFIG_OWE
782 else if (os_strcmp(start, "OWE") == 0)
783 val |= WPA_KEY_MGMT_OWE;
784#endif /* CONFIG_OWE */
567da5bb
JM
785#ifdef CONFIG_DPP
786 else if (os_strcmp(start, "DPP") == 0)
787 val |= WPA_KEY_MGMT_DPP;
788#endif /* CONFIG_DPP */
6fc6879b
JM
789 else {
790 wpa_printf(MSG_ERROR, "Line %d: invalid key_mgmt '%s'",
791 line, start);
792 errors++;
793 }
794
795 if (last)
796 break;
797 start = end + 1;
798 }
799 os_free(buf);
800
801 if (val == 0) {
802 wpa_printf(MSG_ERROR,
803 "Line %d: no key_mgmt values configured.", line);
804 errors++;
805 }
806
5cd317d3
BKB
807 if (!errors && ssid->key_mgmt == val)
808 return 1;
6fc6879b
JM
809 wpa_printf(MSG_MSGDUMP, "key_mgmt: 0x%x", val);
810 ssid->key_mgmt = val;
811 return errors ? -1 : 0;
812}
813
814
815#ifndef NO_CONFIG_WRITE
816static char * wpa_config_write_key_mgmt(const struct parse_data *data,
817 struct wpa_ssid *ssid)
818{
819 char *buf, *pos, *end;
820 int ret;
821
50793929 822 pos = buf = os_zalloc(100);
6fc6879b
JM
823 if (buf == NULL)
824 return NULL;
50793929 825 end = buf + 100;
6fc6879b
JM
826
827 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
828 ret = os_snprintf(pos, end - pos, "%sWPA-PSK",
829 pos == buf ? "" : " ");
d85e1fc8 830 if (os_snprintf_error(end - pos, ret)) {
6fc6879b
JM
831 end[-1] = '\0';
832 return buf;
833 }
834 pos += ret;
835 }
836
837 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
838 ret = os_snprintf(pos, end - pos, "%sWPA-EAP",
839 pos == buf ? "" : " ");
d85e1fc8 840 if (os_snprintf_error(end - pos, ret)) {
6fc6879b
JM
841 end[-1] = '\0';
842 return buf;
843 }
844 pos += ret;
845 }
846
847 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
848 ret = os_snprintf(pos, end - pos, "%sIEEE8021X",
849 pos == buf ? "" : " ");
d85e1fc8 850 if (os_snprintf_error(end - pos, ret)) {
6fc6879b
JM
851 end[-1] = '\0';
852 return buf;
853 }
854 pos += ret;
855 }
856
857 if (ssid->key_mgmt & WPA_KEY_MGMT_NONE) {
858 ret = os_snprintf(pos, end - pos, "%sNONE",
859 pos == buf ? "" : " ");
d85e1fc8 860 if (os_snprintf_error(end - pos, ret)) {
6fc6879b
JM
861 end[-1] = '\0';
862 return buf;
863 }
864 pos += ret;
865 }
866
867 if (ssid->key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
868 ret = os_snprintf(pos, end - pos, "%sWPA-NONE",
869 pos == buf ? "" : " ");
d85e1fc8 870 if (os_snprintf_error(end - pos, ret)) {
6fc6879b
JM
871 end[-1] = '\0';
872 return buf;
873 }
874 pos += ret;
875 }
876
877#ifdef CONFIG_IEEE80211R
50793929
PF
878 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_PSK) {
879 ret = os_snprintf(pos, end - pos, "%sFT-PSK",
880 pos == buf ? "" : " ");
d85e1fc8 881 if (os_snprintf_error(end - pos, ret)) {
50793929
PF
882 end[-1] = '\0';
883 return buf;
884 }
885 pos += ret;
886 }
6fc6879b 887
50793929
PF
888 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
889 ret = os_snprintf(pos, end - pos, "%sFT-EAP",
890 pos == buf ? "" : " ");
d85e1fc8 891 if (os_snprintf_error(end - pos, ret)) {
50793929
PF
892 end[-1] = '\0';
893 return buf;
894 }
895 pos += ret;
896 }
d8e8c992
JM
897
898#ifdef CONFIG_SHA384
899 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X_SHA384) {
900 ret = os_snprintf(pos, end - pos, "%sFT-EAP-SHA384",
901 pos == buf ? "" : " ");
902 if (os_snprintf_error(end - pos, ret)) {
903 end[-1] = '\0';
904 return buf;
905 }
906 pos += ret;
907 }
908#endif /* CONFIG_SHA384 */
6fc6879b
JM
909#endif /* CONFIG_IEEE80211R */
910
50793929
PF
911 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
912 ret = os_snprintf(pos, end - pos, "%sWPA-PSK-SHA256",
913 pos == buf ? "" : " ");
d85e1fc8 914 if (os_snprintf_error(end - pos, ret)) {
50793929
PF
915 end[-1] = '\0';
916 return buf;
917 }
918 pos += ret;
919 }
56586197 920
50793929
PF
921 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
922 ret = os_snprintf(pos, end - pos, "%sWPA-EAP-SHA256",
923 pos == buf ? "" : " ");
d85e1fc8 924 if (os_snprintf_error(end - pos, ret)) {
50793929
PF
925 end[-1] = '\0';
926 return buf;
927 }
928 pos += ret;
929 }
56586197 930
728fae16 931#ifdef CONFIG_WPS
50793929
PF
932 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
933 ret = os_snprintf(pos, end - pos, "%sWPS",
934 pos == buf ? "" : " ");
d85e1fc8 935 if (os_snprintf_error(end - pos, ret)) {
50793929
PF
936 end[-1] = '\0';
937 return buf;
938 }
939 pos += ret;
940 }
728fae16
JM
941#endif /* CONFIG_WPS */
942
d3fd563f
TP
943#ifdef CONFIG_SAE
944 if (ssid->key_mgmt & WPA_KEY_MGMT_SAE) {
945 ret = os_snprintf(pos, end - pos, "%sSAE",
946 pos == buf ? "" : " ");
d85e1fc8 947 if (os_snprintf_error(end - pos, ret)) {
d3fd563f
TP
948 end[-1] = '\0';
949 return buf;
950 }
951 pos += ret;
952 }
953
954 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_SAE) {
955 ret = os_snprintf(pos, end - pos, "%sFT-SAE",
956 pos == buf ? "" : " ");
d85e1fc8 957 if (os_snprintf_error(end - pos, ret)) {
d3fd563f
TP
958 end[-1] = '\0';
959 return buf;
960 }
961 pos += ret;
962 }
963#endif /* CONFIG_SAE */
964
2d6ee86f
JM
965#ifdef CONFIG_HS20
966 if (ssid->key_mgmt & WPA_KEY_MGMT_OSEN) {
967 ret = os_snprintf(pos, end - pos, "%sOSEN",
968 pos == buf ? "" : " ");
d85e1fc8 969 if (os_snprintf_error(end - pos, ret)) {
2d6ee86f
JM
970 end[-1] = '\0';
971 return buf;
972 }
973 pos += ret;
974 }
975#endif /* CONFIG_HS20 */
976
5e3b5197 977#ifdef CONFIG_SUITEB
666497c8
JM
978 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
979 ret = os_snprintf(pos, end - pos, "%sWPA-EAP-SUITE-B",
980 pos == buf ? "" : " ");
d85e1fc8 981 if (os_snprintf_error(end - pos, ret)) {
666497c8
JM
982 end[-1] = '\0';
983 return buf;
984 }
985 pos += ret;
986 }
5e3b5197
JM
987#endif /* CONFIG_SUITEB */
988
989#ifdef CONFIG_SUITEB192
990 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
991 ret = os_snprintf(pos, end - pos, "%sWPA-EAP-SUITE-B-192",
992 pos == buf ? "" : " ");
993 if (os_snprintf_error(end - pos, ret)) {
994 end[-1] = '\0';
995 return buf;
996 }
997 pos += ret;
998 }
999#endif /* CONFIG_SUITEB192 */
666497c8 1000
199eb3a4 1001#ifdef CONFIG_FILS
1002 if (ssid->key_mgmt & WPA_KEY_MGMT_FILS_SHA256) {
1003 ret = os_snprintf(pos, end - pos, "%sFILS-SHA256",
1004 pos == buf ? "" : " ");
1005 if (os_snprintf_error(end - pos, ret)) {
1006 end[-1] = '\0';
1007 return buf;
1008 }
1009 pos += ret;
1010 }
1011 if (ssid->key_mgmt & WPA_KEY_MGMT_FILS_SHA384) {
1012 ret = os_snprintf(pos, end - pos, "%sFILS-SHA384",
1013 pos == buf ? "" : " ");
1014 if (os_snprintf_error(end - pos, ret)) {
1015 end[-1] = '\0';
1016 return buf;
1017 }
1018 pos += ret;
1019 }
1020#ifdef CONFIG_IEEE80211R
1021 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA256) {
1022 ret = os_snprintf(pos, end - pos, "%sFT-FILS-SHA256",
1023 pos == buf ? "" : " ");
1024 if (os_snprintf_error(end - pos, ret)) {
1025 end[-1] = '\0';
1026 return buf;
1027 }
1028 pos += ret;
1029 }
1030 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA384) {
1031 ret = os_snprintf(pos, end - pos, "%sFT-FILS-SHA384",
1032 pos == buf ? "" : " ");
1033 if (os_snprintf_error(end - pos, ret)) {
1034 end[-1] = '\0';
1035 return buf;
1036 }
1037 pos += ret;
1038 }
1039#endif /* CONFIG_IEEE80211R */
1040#endif /* CONFIG_FILS */
1041
ad6a9247
DRC
1042#ifdef CONFIG_DPP
1043 if (ssid->key_mgmt & WPA_KEY_MGMT_DPP) {
1044 ret = os_snprintf(pos, end - pos, "%sDPP",
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_DPP */
1053
06c00e6d
JM
1054#ifdef CONFIG_OWE
1055 if (ssid->key_mgmt & WPA_KEY_MGMT_OWE) {
1056 ret = os_snprintf(pos, end - pos, "%sOWE",
1057 pos == buf ? "" : " ");
1058 if (os_snprintf_error(end - pos, ret)) {
1059 end[-1] = '\0';
1060 return buf;
1061 }
1062 pos += ret;
1063 }
1064#endif /* CONFIG_OWE */
1065
a1e46f32
JM
1066 if (pos == buf) {
1067 os_free(buf);
1068 buf = NULL;
1069 }
1070
6fc6879b
JM
1071 return buf;
1072}
1073#endif /* NO_CONFIG_WRITE */
1074
1075
1076static int wpa_config_parse_cipher(int line, const char *value)
1077{
9e68742e
JM
1078#ifdef CONFIG_NO_WPA
1079 return -1;
1080#else /* CONFIG_NO_WPA */
a39c78be
JM
1081 int val = wpa_parse_cipher(value);
1082 if (val < 0) {
1083 wpa_printf(MSG_ERROR, "Line %d: invalid cipher '%s'.",
1084 line, value);
6fc6879b 1085 return -1;
6fc6879b 1086 }
6fc6879b
JM
1087 if (val == 0) {
1088 wpa_printf(MSG_ERROR, "Line %d: no cipher values configured.",
1089 line);
1090 return -1;
1091 }
1092 return val;
9e68742e 1093#endif /* CONFIG_NO_WPA */
6fc6879b
JM
1094}
1095
1096
1097#ifndef NO_CONFIG_WRITE
1098static char * wpa_config_write_cipher(int cipher)
1099{
9e68742e
JM
1100#ifdef CONFIG_NO_WPA
1101 return NULL;
1102#else /* CONFIG_NO_WPA */
0282a8c4 1103 char *buf = os_zalloc(50);
6fc6879b
JM
1104 if (buf == NULL)
1105 return NULL;
6fc6879b 1106
0282a8c4
JM
1107 if (wpa_write_ciphers(buf, buf + 50, cipher, " ") < 0) {
1108 os_free(buf);
1109 return NULL;
6fc6879b
JM
1110 }
1111
1112 return buf;
9e68742e 1113#endif /* CONFIG_NO_WPA */
6fc6879b
JM
1114}
1115#endif /* NO_CONFIG_WRITE */
1116
1117
1118static int wpa_config_parse_pairwise(const struct parse_data *data,
1119 struct wpa_ssid *ssid, int line,
1120 const char *value)
1121{
1122 int val;
1123 val = wpa_config_parse_cipher(line, value);
1124 if (val == -1)
1125 return -1;
03145326 1126 if (val & ~WPA_ALLOWED_PAIRWISE_CIPHERS) {
6fc6879b
JM
1127 wpa_printf(MSG_ERROR, "Line %d: not allowed pairwise cipher "
1128 "(0x%x).", line, val);
1129 return -1;
1130 }
1131
5cd317d3
BKB
1132 if (ssid->pairwise_cipher == val)
1133 return 1;
6fc6879b
JM
1134 wpa_printf(MSG_MSGDUMP, "pairwise: 0x%x", val);
1135 ssid->pairwise_cipher = val;
1136 return 0;
1137}
1138
1139
1140#ifndef NO_CONFIG_WRITE
1141static char * wpa_config_write_pairwise(const struct parse_data *data,
1142 struct wpa_ssid *ssid)
1143{
1144 return wpa_config_write_cipher(ssid->pairwise_cipher);
1145}
1146#endif /* NO_CONFIG_WRITE */
1147
1148
1149static int wpa_config_parse_group(const struct parse_data *data,
1150 struct wpa_ssid *ssid, int line,
1151 const char *value)
1152{
1153 int val;
1154 val = wpa_config_parse_cipher(line, value);
1155 if (val == -1)
1156 return -1;
ce8963fc
JM
1157
1158 /*
1159 * Backwards compatibility - filter out WEP ciphers that were previously
1160 * allowed.
1161 */
1162 val &= ~(WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40);
1163
03145326 1164 if (val & ~WPA_ALLOWED_GROUP_CIPHERS) {
6fc6879b
JM
1165 wpa_printf(MSG_ERROR, "Line %d: not allowed group cipher "
1166 "(0x%x).", line, val);
1167 return -1;
1168 }
1169
5cd317d3
BKB
1170 if (ssid->group_cipher == val)
1171 return 1;
6fc6879b
JM
1172 wpa_printf(MSG_MSGDUMP, "group: 0x%x", val);
1173 ssid->group_cipher = val;
1174 return 0;
1175}
1176
1177
1178#ifndef NO_CONFIG_WRITE
1179static char * wpa_config_write_group(const struct parse_data *data,
1180 struct wpa_ssid *ssid)
1181{
1182 return wpa_config_write_cipher(ssid->group_cipher);
1183}
1184#endif /* NO_CONFIG_WRITE */
1185
1186
61a56c14
JM
1187static int wpa_config_parse_group_mgmt(const struct parse_data *data,
1188 struct wpa_ssid *ssid, int line,
1189 const char *value)
1190{
1191 int val;
1192
1193 val = wpa_config_parse_cipher(line, value);
1194 if (val == -1)
1195 return -1;
1196
1197 if (val & ~WPA_ALLOWED_GROUP_MGMT_CIPHERS) {
1198 wpa_printf(MSG_ERROR,
1199 "Line %d: not allowed group management cipher (0x%x).",
1200 line, val);
1201 return -1;
1202 }
1203
1204 if (ssid->group_mgmt_cipher == val)
1205 return 1;
1206 wpa_printf(MSG_MSGDUMP, "group_mgmt: 0x%x", val);
1207 ssid->group_mgmt_cipher = val;
1208 return 0;
1209}
1210
1211
1212#ifndef NO_CONFIG_WRITE
1213static char * wpa_config_write_group_mgmt(const struct parse_data *data,
1214 struct wpa_ssid *ssid)
1215{
1216 return wpa_config_write_cipher(ssid->group_mgmt_cipher);
1217}
1218#endif /* NO_CONFIG_WRITE */
1219
1220
6fc6879b
JM
1221static int wpa_config_parse_auth_alg(const struct parse_data *data,
1222 struct wpa_ssid *ssid, int line,
1223 const char *value)
1224{
1225 int val = 0, last, errors = 0;
1226 char *start, *end, *buf;
1227
1228 buf = os_strdup(value);
1229 if (buf == NULL)
1230 return -1;
1231 start = buf;
1232
1233 while (*start != '\0') {
1234 while (*start == ' ' || *start == '\t')
1235 start++;
1236 if (*start == '\0')
1237 break;
1238 end = start;
1239 while (*end != ' ' && *end != '\t' && *end != '\0')
1240 end++;
1241 last = *end == '\0';
1242 *end = '\0';
1243 if (os_strcmp(start, "OPEN") == 0)
1244 val |= WPA_AUTH_ALG_OPEN;
1245 else if (os_strcmp(start, "SHARED") == 0)
1246 val |= WPA_AUTH_ALG_SHARED;
1247 else if (os_strcmp(start, "LEAP") == 0)
1248 val |= WPA_AUTH_ALG_LEAP;
1249 else {
1250 wpa_printf(MSG_ERROR, "Line %d: invalid auth_alg '%s'",
1251 line, start);
1252 errors++;
1253 }
1254
1255 if (last)
1256 break;
1257 start = end + 1;
1258 }
1259 os_free(buf);
1260
1261 if (val == 0) {
1262 wpa_printf(MSG_ERROR,
1263 "Line %d: no auth_alg values configured.", line);
1264 errors++;
1265 }
1266
5cd317d3
BKB
1267 if (!errors && ssid->auth_alg == val)
1268 return 1;
6fc6879b
JM
1269 wpa_printf(MSG_MSGDUMP, "auth_alg: 0x%x", val);
1270 ssid->auth_alg = val;
1271 return errors ? -1 : 0;
1272}
1273
1274
1275#ifndef NO_CONFIG_WRITE
1276static char * wpa_config_write_auth_alg(const struct parse_data *data,
1277 struct wpa_ssid *ssid)
1278{
1279 char *buf, *pos, *end;
1280 int ret;
1281
1282 pos = buf = os_zalloc(30);
1283 if (buf == NULL)
1284 return NULL;
1285 end = buf + 30;
1286
1287 if (ssid->auth_alg & WPA_AUTH_ALG_OPEN) {
1288 ret = os_snprintf(pos, end - pos, "%sOPEN",
1289 pos == buf ? "" : " ");
d85e1fc8 1290 if (os_snprintf_error(end - pos, ret)) {
6fc6879b
JM
1291 end[-1] = '\0';
1292 return buf;
1293 }
1294 pos += ret;
1295 }
1296
1297 if (ssid->auth_alg & WPA_AUTH_ALG_SHARED) {
1298 ret = os_snprintf(pos, end - pos, "%sSHARED",
1299 pos == buf ? "" : " ");
d85e1fc8 1300 if (os_snprintf_error(end - pos, ret)) {
6fc6879b
JM
1301 end[-1] = '\0';
1302 return buf;
1303 }
1304 pos += ret;
1305 }
1306
1307 if (ssid->auth_alg & WPA_AUTH_ALG_LEAP) {
1308 ret = os_snprintf(pos, end - pos, "%sLEAP",
1309 pos == buf ? "" : " ");
d85e1fc8 1310 if (os_snprintf_error(end - pos, ret)) {
6fc6879b
JM
1311 end[-1] = '\0';
1312 return buf;
1313 }
1314 pos += ret;
1315 }
1316
a1e46f32
JM
1317 if (pos == buf) {
1318 os_free(buf);
1319 buf = NULL;
1320 }
1321
6fc6879b
JM
1322 return buf;
1323}
1324#endif /* NO_CONFIG_WRITE */
1325
1326
625f202a 1327static int * wpa_config_parse_int_array(const char *value)
d3a98225
JM
1328{
1329 int *freqs;
1330 size_t used, len;
1331 const char *pos;
1332
1333 used = 0;
1334 len = 10;
f9884c09 1335 freqs = os_calloc(len + 1, sizeof(int));
d3a98225 1336 if (freqs == NULL)
b766a9a2 1337 return NULL;
d3a98225
JM
1338
1339 pos = value;
1340 while (pos) {
1341 while (*pos == ' ')
1342 pos++;
1343 if (used == len) {
1344 int *n;
1345 size_t i;
067ffa26 1346 n = os_realloc_array(freqs, len * 2 + 1, sizeof(int));
d3a98225
JM
1347 if (n == NULL) {
1348 os_free(freqs);
b766a9a2 1349 return NULL;
d3a98225
JM
1350 }
1351 for (i = len; i <= len * 2; i++)
1352 n[i] = 0;
1353 freqs = n;
1354 len *= 2;
1355 }
1356
1357 freqs[used] = atoi(pos);
1358 if (freqs[used] == 0)
1359 break;
1360 used++;
1361 pos = os_strchr(pos + 1, ' ');
1362 }
1363
b766a9a2
JM
1364 return freqs;
1365}
1366
1367
1368static int wpa_config_parse_scan_freq(const struct parse_data *data,
1369 struct wpa_ssid *ssid, int line,
1370 const char *value)
1371{
1372 int *freqs;
1373
625f202a 1374 freqs = wpa_config_parse_int_array(value);
b766a9a2
JM
1375 if (freqs == NULL)
1376 return -1;
6668efda
JM
1377 if (freqs[0] == 0) {
1378 os_free(freqs);
1379 freqs = NULL;
1380 }
d3a98225
JM
1381 os_free(ssid->scan_freq);
1382 ssid->scan_freq = freqs;
1383
1384 return 0;
1385}
1386
1387
b766a9a2
JM
1388static int wpa_config_parse_freq_list(const struct parse_data *data,
1389 struct wpa_ssid *ssid, int line,
1390 const char *value)
1391{
1392 int *freqs;
1393
625f202a 1394 freqs = wpa_config_parse_int_array(value);
b766a9a2
JM
1395 if (freqs == NULL)
1396 return -1;
6668efda
JM
1397 if (freqs[0] == 0) {
1398 os_free(freqs);
1399 freqs = NULL;
1400 }
b766a9a2
JM
1401 os_free(ssid->freq_list);
1402 ssid->freq_list = freqs;
1403
1404 return 0;
1405}
1406
1407
d3a98225 1408#ifndef NO_CONFIG_WRITE
b766a9a2
JM
1409static char * wpa_config_write_freqs(const struct parse_data *data,
1410 const int *freqs)
d3a98225
JM
1411{
1412 char *buf, *pos, *end;
1413 int i, ret;
1414 size_t count;
1415
b766a9a2 1416 if (freqs == NULL)
d3a98225
JM
1417 return NULL;
1418
1419 count = 0;
b766a9a2 1420 for (i = 0; freqs[i]; i++)
d3a98225
JM
1421 count++;
1422
1423 pos = buf = os_zalloc(10 * count + 1);
1424 if (buf == NULL)
1425 return NULL;
1426 end = buf + 10 * count + 1;
1427
b766a9a2 1428 for (i = 0; freqs[i]; i++) {
d3a98225 1429 ret = os_snprintf(pos, end - pos, "%s%u",
b766a9a2 1430 i == 0 ? "" : " ", freqs[i]);
d85e1fc8 1431 if (os_snprintf_error(end - pos, ret)) {
d3a98225
JM
1432 end[-1] = '\0';
1433 return buf;
1434 }
1435 pos += ret;
1436 }
1437
1438 return buf;
1439}
b766a9a2
JM
1440
1441
1442static char * wpa_config_write_scan_freq(const struct parse_data *data,
1443 struct wpa_ssid *ssid)
1444{
1445 return wpa_config_write_freqs(data, ssid->scan_freq);
1446}
1447
1448
1449static char * wpa_config_write_freq_list(const struct parse_data *data,
1450 struct wpa_ssid *ssid)
1451{
1452 return wpa_config_write_freqs(data, ssid->freq_list);
1453}
d3a98225
JM
1454#endif /* NO_CONFIG_WRITE */
1455
1456
6fc6879b
JM
1457#ifdef IEEE8021X_EAPOL
1458static int wpa_config_parse_eap(const struct parse_data *data,
1459 struct wpa_ssid *ssid, int line,
1460 const char *value)
1461{
1462 int last, errors = 0;
1463 char *start, *end, *buf;
1464 struct eap_method_type *methods = NULL, *tmp;
1465 size_t num_methods = 0;
1466
1467 buf = os_strdup(value);
1468 if (buf == NULL)
1469 return -1;
1470 start = buf;
1471
1472 while (*start != '\0') {
1473 while (*start == ' ' || *start == '\t')
1474 start++;
1475 if (*start == '\0')
1476 break;
1477 end = start;
1478 while (*end != ' ' && *end != '\t' && *end != '\0')
1479 end++;
1480 last = *end == '\0';
1481 *end = '\0';
1482 tmp = methods;
067ffa26
JM
1483 methods = os_realloc_array(methods, num_methods + 1,
1484 sizeof(*methods));
6fc6879b
JM
1485 if (methods == NULL) {
1486 os_free(tmp);
1487 os_free(buf);
1488 return -1;
1489 }
1490 methods[num_methods].method = eap_peer_get_type(
1491 start, &methods[num_methods].vendor);
1492 if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
1493 methods[num_methods].method == EAP_TYPE_NONE) {
1494 wpa_printf(MSG_ERROR, "Line %d: unknown EAP method "
1495 "'%s'", line, start);
1496 wpa_printf(MSG_ERROR, "You may need to add support for"
1497 " this EAP method during wpa_supplicant\n"
1498 "build time configuration.\n"
1499 "See README for more information.");
1500 errors++;
1501 } else if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
1502 methods[num_methods].method == EAP_TYPE_LEAP)
1503 ssid->leap++;
1504 else
1505 ssid->non_leap++;
1506 num_methods++;
1507 if (last)
1508 break;
1509 start = end + 1;
1510 }
1511 os_free(buf);
1512
1513 tmp = methods;
067ffa26 1514 methods = os_realloc_array(methods, num_methods + 1, sizeof(*methods));
6fc6879b
JM
1515 if (methods == NULL) {
1516 os_free(tmp);
1517 return -1;
1518 }
1519 methods[num_methods].vendor = EAP_VENDOR_IETF;
1520 methods[num_methods].method = EAP_TYPE_NONE;
1521 num_methods++;
1522
5cd317d3
BKB
1523 if (!errors && ssid->eap.eap_methods) {
1524 struct eap_method_type *prev_m;
1525 size_t i, j, prev_methods, match = 0;
1526
1527 prev_m = ssid->eap.eap_methods;
1528 for (i = 0; prev_m[i].vendor != EAP_VENDOR_IETF ||
1529 prev_m[i].method != EAP_TYPE_NONE; i++) {
1530 /* Count the methods */
1531 }
1532 prev_methods = i + 1;
1533
1534 for (i = 0; prev_methods == num_methods && i < prev_methods;
1535 i++) {
1536 for (j = 0; j < num_methods; j++) {
1537 if (prev_m[i].vendor == methods[j].vendor &&
1538 prev_m[i].method == methods[j].method) {
1539 match++;
1540 break;
1541 }
1542 }
1543 }
1544 if (match == num_methods) {
1545 os_free(methods);
1546 return 1;
1547 }
1548 }
6fc6879b
JM
1549 wpa_hexdump(MSG_MSGDUMP, "eap methods",
1550 (u8 *) methods, num_methods * sizeof(*methods));
bb8b1bb0 1551 os_free(ssid->eap.eap_methods);
6fc6879b
JM
1552 ssid->eap.eap_methods = methods;
1553 return errors ? -1 : 0;
1554}
1555
1556
954f03aa 1557#ifndef NO_CONFIG_WRITE
6fc6879b
JM
1558static char * wpa_config_write_eap(const struct parse_data *data,
1559 struct wpa_ssid *ssid)
1560{
1561 int i, ret;
1562 char *buf, *pos, *end;
1563 const struct eap_method_type *eap_methods = ssid->eap.eap_methods;
1564 const char *name;
1565
1566 if (eap_methods == NULL)
1567 return NULL;
1568
1569 pos = buf = os_zalloc(100);
1570 if (buf == NULL)
1571 return NULL;
1572 end = buf + 100;
1573
1574 for (i = 0; eap_methods[i].vendor != EAP_VENDOR_IETF ||
1575 eap_methods[i].method != EAP_TYPE_NONE; i++) {
1576 name = eap_get_name(eap_methods[i].vendor,
1577 eap_methods[i].method);
1578 if (name) {
1579 ret = os_snprintf(pos, end - pos, "%s%s",
1580 pos == buf ? "" : " ", name);
d85e1fc8 1581 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
1582 break;
1583 pos += ret;
1584 }
1585 }
1586
1587 end[-1] = '\0';
1588
1589 return buf;
1590}
954f03aa 1591#endif /* NO_CONFIG_WRITE */
6fc6879b
JM
1592
1593
1594static int wpa_config_parse_password(const struct parse_data *data,
1595 struct wpa_ssid *ssid, int line,
1596 const char *value)
1597{
1598 u8 *hash;
1599
b56c0546 1600 if (os_strcmp(value, "NULL") == 0) {
5cd317d3
BKB
1601 if (!ssid->eap.password)
1602 return 1; /* Already unset */
b56c0546 1603 wpa_printf(MSG_DEBUG, "Unset configuration string 'password'");
19c48da0 1604 bin_clear_free(ssid->eap.password, ssid->eap.password_len);
b56c0546
JM
1605 ssid->eap.password = NULL;
1606 ssid->eap.password_len = 0;
1607 return 0;
1608 }
1609
0ebb23e3
JM
1610#ifdef CONFIG_EXT_PASSWORD
1611 if (os_strncmp(value, "ext:", 4) == 0) {
1612 char *name = os_strdup(value + 4);
c724a0a1 1613 if (!name)
0ebb23e3 1614 return -1;
19c48da0 1615 bin_clear_free(ssid->eap.password, ssid->eap.password_len);
0ebb23e3
JM
1616 ssid->eap.password = (u8 *) name;
1617 ssid->eap.password_len = os_strlen(name);
1618 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
1619 ssid->eap.flags |= EAP_CONFIG_FLAGS_EXT_PASSWORD;
1620 return 0;
1621 }
1622#endif /* CONFIG_EXT_PASSWORD */
1623
6fc6879b
JM
1624 if (os_strncmp(value, "hash:", 5) != 0) {
1625 char *tmp;
1626 size_t res_len;
1627
1628 tmp = wpa_config_parse_string(value, &res_len);
c724a0a1
JM
1629 if (!tmp) {
1630 wpa_printf(MSG_ERROR,
1631 "Line %d: failed to parse password.", line);
6fc6879b
JM
1632 return -1;
1633 }
556f5a2a
HS
1634 wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
1635 (u8 *) tmp, res_len);
6fc6879b 1636
19c48da0 1637 bin_clear_free(ssid->eap.password, ssid->eap.password_len);
6fc6879b
JM
1638 ssid->eap.password = (u8 *) tmp;
1639 ssid->eap.password_len = res_len;
1640 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
0ebb23e3 1641 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_PASSWORD;
6fc6879b
JM
1642
1643 return 0;
1644 }
1645
1646
1647 /* NtPasswordHash: hash:<32 hex digits> */
1648 if (os_strlen(value + 5) != 2 * 16) {
c724a0a1
JM
1649 wpa_printf(MSG_ERROR,
1650 "Line %d: Invalid password hash length (expected 32 hex digits)",
1651 line);
6fc6879b
JM
1652 return -1;
1653 }
1654
1655 hash = os_malloc(16);
c724a0a1 1656 if (!hash)
6fc6879b
JM
1657 return -1;
1658
1659 if (hexstr2bin(value + 5, hash, 16)) {
1660 os_free(hash);
1661 wpa_printf(MSG_ERROR, "Line %d: Invalid password hash", line);
1662 return -1;
1663 }
1664
1665 wpa_hexdump_key(MSG_MSGDUMP, data->name, hash, 16);
1666
5cd317d3
BKB
1667 if (ssid->eap.password && ssid->eap.password_len == 16 &&
1668 os_memcmp(ssid->eap.password, hash, 16) == 0 &&
1669 (ssid->eap.flags & EAP_CONFIG_FLAGS_PASSWORD_NTHASH)) {
1670 bin_clear_free(hash, 16);
1671 return 1;
1672 }
19c48da0 1673 bin_clear_free(ssid->eap.password, ssid->eap.password_len);
6fc6879b
JM
1674 ssid->eap.password = hash;
1675 ssid->eap.password_len = 16;
1676 ssid->eap.flags |= EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
0ebb23e3 1677 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_PASSWORD;
6fc6879b
JM
1678
1679 return 0;
1680}
1681
1682
c724a0a1
JM
1683static int wpa_config_parse_machine_password(const struct parse_data *data,
1684 struct wpa_ssid *ssid, int line,
1685 const char *value)
1686{
1687 u8 *hash;
1688
1689 if (os_strcmp(value, "NULL") == 0) {
1690 if (!ssid->eap.machine_password)
1691 return 1; /* Already unset */
1692 wpa_printf(MSG_DEBUG,
1693 "Unset configuration string 'machine_password'");
1694 bin_clear_free(ssid->eap.machine_password,
1695 ssid->eap.machine_password_len);
1696 ssid->eap.machine_password = NULL;
1697 ssid->eap.machine_password_len = 0;
1698 return 0;
1699 }
1700
1701#ifdef CONFIG_EXT_PASSWORD
1702 if (os_strncmp(value, "ext:", 4) == 0) {
1703 char *name = os_strdup(value + 4);
1704
1705 if (!name)
1706 return -1;
1707 bin_clear_free(ssid->eap.machine_password,
1708 ssid->eap.machine_password_len);
1709 ssid->eap.machine_password = (u8 *) name;
1710 ssid->eap.machine_password_len = os_strlen(name);
1711 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_MACHINE_PASSWORD_NTHASH;
1712 ssid->eap.flags |= EAP_CONFIG_FLAGS_EXT_MACHINE_PASSWORD;
1713 return 0;
1714 }
1715#endif /* CONFIG_EXT_PASSWORD */
1716
1717 if (os_strncmp(value, "hash:", 5) != 0) {
1718 char *tmp;
1719 size_t res_len;
1720
1721 tmp = wpa_config_parse_string(value, &res_len);
1722 if (!tmp) {
1723 wpa_printf(MSG_ERROR,
1724 "Line %d: failed to parse machine_password.",
1725 line);
1726 return -1;
1727 }
1728 wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
1729 (u8 *) tmp, res_len);
1730
1731 bin_clear_free(ssid->eap.machine_password,
1732 ssid->eap.machine_password_len);
1733 ssid->eap.machine_password = (u8 *) tmp;
1734 ssid->eap.machine_password_len = res_len;
1735 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_MACHINE_PASSWORD_NTHASH;
1736 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_MACHINE_PASSWORD;
1737
1738 return 0;
1739 }
1740
1741
1742 /* NtPasswordHash: hash:<32 hex digits> */
1743 if (os_strlen(value + 5) != 2 * 16) {
1744 wpa_printf(MSG_ERROR,
1745 "Line %d: Invalid machine_password hash length (expected 32 hex digits)",
1746 line);
1747 return -1;
1748 }
1749
1750 hash = os_malloc(16);
1751 if (!hash)
1752 return -1;
1753
1754 if (hexstr2bin(value + 5, hash, 16)) {
1755 os_free(hash);
1756 wpa_printf(MSG_ERROR, "Line %d: Invalid machine_password hash",
1757 line);
1758 return -1;
1759 }
1760
1761 wpa_hexdump_key(MSG_MSGDUMP, data->name, hash, 16);
1762
1763 if (ssid->eap.machine_password &&
1764 ssid->eap.machine_password_len == 16 &&
1765 os_memcmp(ssid->eap.machine_password, hash, 16) == 0 &&
1766 (ssid->eap.flags & EAP_CONFIG_FLAGS_MACHINE_PASSWORD_NTHASH)) {
1767 bin_clear_free(hash, 16);
1768 return 1;
1769 }
1770 bin_clear_free(ssid->eap.machine_password,
1771 ssid->eap.machine_password_len);
1772 ssid->eap.machine_password = hash;
1773 ssid->eap.machine_password_len = 16;
1774 ssid->eap.flags |= EAP_CONFIG_FLAGS_MACHINE_PASSWORD_NTHASH;
1775 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_MACHINE_PASSWORD;
1776
1777 return 0;
1778}
1779
1780
954f03aa 1781#ifndef NO_CONFIG_WRITE
c724a0a1 1782
6fc6879b
JM
1783static char * wpa_config_write_password(const struct parse_data *data,
1784 struct wpa_ssid *ssid)
1785{
1786 char *buf;
1787
c724a0a1 1788 if (!ssid->eap.password)
6fc6879b
JM
1789 return NULL;
1790
0ebb23e3
JM
1791#ifdef CONFIG_EXT_PASSWORD
1792 if (ssid->eap.flags & EAP_CONFIG_FLAGS_EXT_PASSWORD) {
1793 buf = os_zalloc(4 + ssid->eap.password_len + 1);
c724a0a1 1794 if (!buf)
0ebb23e3
JM
1795 return NULL;
1796 os_memcpy(buf, "ext:", 4);
1797 os_memcpy(buf + 4, ssid->eap.password, ssid->eap.password_len);
1798 return buf;
1799 }
1800#endif /* CONFIG_EXT_PASSWORD */
1801
6fc6879b
JM
1802 if (!(ssid->eap.flags & EAP_CONFIG_FLAGS_PASSWORD_NTHASH)) {
1803 return wpa_config_write_string(
1804 ssid->eap.password, ssid->eap.password_len);
1805 }
1806
1807 buf = os_malloc(5 + 32 + 1);
c724a0a1 1808 if (!buf)
6fc6879b
JM
1809 return NULL;
1810
1811 os_memcpy(buf, "hash:", 5);
1812 wpa_snprintf_hex(buf + 5, 32 + 1, ssid->eap.password, 16);
1813
1814 return buf;
1815}
c724a0a1
JM
1816
1817
1818static char * wpa_config_write_machine_password(const struct parse_data *data,
1819 struct wpa_ssid *ssid)
1820{
1821 char *buf;
1822
1823 if (!ssid->eap.machine_password)
1824 return NULL;
1825
1826#ifdef CONFIG_EXT_PASSWORD
1827 if (ssid->eap.flags & EAP_CONFIG_FLAGS_EXT_MACHINE_PASSWORD) {
1828 buf = os_zalloc(4 + ssid->eap.machine_password_len + 1);
1829 if (!buf)
1830 return NULL;
1831 os_memcpy(buf, "ext:", 4);
1832 os_memcpy(buf + 4, ssid->eap.machine_password,
1833 ssid->eap.machine_password_len);
1834 return buf;
1835 }
1836#endif /* CONFIG_EXT_PASSWORD */
1837
1838 if (!(ssid->eap.flags & EAP_CONFIG_FLAGS_MACHINE_PASSWORD_NTHASH)) {
1839 return wpa_config_write_string(
1840 ssid->eap.machine_password,
1841 ssid->eap.machine_password_len);
1842 }
1843
1844 buf = os_malloc(5 + 32 + 1);
1845 if (!buf)
1846 return NULL;
1847
1848 os_memcpy(buf, "hash:", 5);
1849 wpa_snprintf_hex(buf + 5, 32 + 1, ssid->eap.machine_password, 16);
1850
1851 return buf;
1852}
1853
954f03aa 1854#endif /* NO_CONFIG_WRITE */
6fc6879b
JM
1855#endif /* IEEE8021X_EAPOL */
1856
1857
1858static int wpa_config_parse_wep_key(u8 *key, size_t *len, int line,
1859 const char *value, int idx)
1860{
1861 char *buf, title[20];
1862 int res;
1863
1864 buf = wpa_config_parse_string(value, len);
1865 if (buf == NULL) {
1866 wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key %d '%s'.",
1867 line, idx, value);
1868 return -1;
1869 }
1870 if (*len > MAX_WEP_KEY_LEN) {
1871 wpa_printf(MSG_ERROR, "Line %d: Too long WEP key %d '%s'.",
1872 line, idx, value);
1873 os_free(buf);
1874 return -1;
1875 }
fea7c3a0
JM
1876 if (*len && *len != 5 && *len != 13 && *len != 16) {
1877 wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key length %u - "
1878 "this network block will be ignored",
1879 line, (unsigned int) *len);
1880 }
6fc6879b 1881 os_memcpy(key, buf, *len);
19c48da0 1882 str_clear_free(buf);
6fc6879b 1883 res = os_snprintf(title, sizeof(title), "wep_key%d", idx);
a80ba67a 1884 if (!os_snprintf_error(sizeof(title), res))
6fc6879b
JM
1885 wpa_hexdump_key(MSG_MSGDUMP, title, key, *len);
1886 return 0;
1887}
1888
1889
1890static int wpa_config_parse_wep_key0(const struct parse_data *data,
1891 struct wpa_ssid *ssid, int line,
1892 const char *value)
1893{
1894 return wpa_config_parse_wep_key(ssid->wep_key[0],
1895 &ssid->wep_key_len[0], line,
1896 value, 0);
1897}
1898
1899
1900static int wpa_config_parse_wep_key1(const struct parse_data *data,
1901 struct wpa_ssid *ssid, int line,
1902 const char *value)
1903{
1904 return wpa_config_parse_wep_key(ssid->wep_key[1],
1905 &ssid->wep_key_len[1], line,
1906 value, 1);
1907}
1908
1909
1910static int wpa_config_parse_wep_key2(const struct parse_data *data,
1911 struct wpa_ssid *ssid, int line,
1912 const char *value)
1913{
1914 return wpa_config_parse_wep_key(ssid->wep_key[2],
1915 &ssid->wep_key_len[2], line,
1916 value, 2);
1917}
1918
1919
1920static int wpa_config_parse_wep_key3(const struct parse_data *data,
1921 struct wpa_ssid *ssid, int line,
1922 const char *value)
1923{
1924 return wpa_config_parse_wep_key(ssid->wep_key[3],
1925 &ssid->wep_key_len[3], line,
1926 value, 3);
1927}
1928
1929
1930#ifndef NO_CONFIG_WRITE
1931static char * wpa_config_write_wep_key(struct wpa_ssid *ssid, int idx)
1932{
1933 if (ssid->wep_key_len[idx] == 0)
1934 return NULL;
1935 return wpa_config_write_string(ssid->wep_key[idx],
1936 ssid->wep_key_len[idx]);
1937}
1938
1939
1940static char * wpa_config_write_wep_key0(const struct parse_data *data,
1941 struct wpa_ssid *ssid)
1942{
1943 return wpa_config_write_wep_key(ssid, 0);
1944}
1945
1946
1947static char * wpa_config_write_wep_key1(const struct parse_data *data,
1948 struct wpa_ssid *ssid)
1949{
1950 return wpa_config_write_wep_key(ssid, 1);
1951}
1952
1953
1954static char * wpa_config_write_wep_key2(const struct parse_data *data,
1955 struct wpa_ssid *ssid)
1956{
1957 return wpa_config_write_wep_key(ssid, 2);
1958}
1959
1960
1961static char * wpa_config_write_wep_key3(const struct parse_data *data,
1962 struct wpa_ssid *ssid)
1963{
1964 return wpa_config_write_wep_key(ssid, 3);
1965}
1966#endif /* NO_CONFIG_WRITE */
1967
1968
fbdcfd57
JM
1969#ifdef CONFIG_P2P
1970
9ec87666
JM
1971static int wpa_config_parse_go_p2p_dev_addr(const struct parse_data *data,
1972 struct wpa_ssid *ssid, int line,
1973 const char *value)
1974{
1975 if (value[0] == '\0' || os_strcmp(value, "\"\"") == 0 ||
1976 os_strcmp(value, "any") == 0) {
1977 os_memset(ssid->go_p2p_dev_addr, 0, ETH_ALEN);
1978 wpa_printf(MSG_MSGDUMP, "GO P2P Device Address any");
1979 return 0;
1980 }
1981 if (hwaddr_aton(value, ssid->go_p2p_dev_addr)) {
1982 wpa_printf(MSG_ERROR, "Line %d: Invalid GO P2P Device Address '%s'.",
1983 line, value);
1984 return -1;
1985 }
1986 ssid->bssid_set = 1;
1987 wpa_printf(MSG_MSGDUMP, "GO P2P Device Address " MACSTR,
1988 MAC2STR(ssid->go_p2p_dev_addr));
1989 return 0;
1990}
1991
1992
1993#ifndef NO_CONFIG_WRITE
1994static char * wpa_config_write_go_p2p_dev_addr(const struct parse_data *data,
1995 struct wpa_ssid *ssid)
1996{
1997 char *value;
1998 int res;
1999
2000 if (is_zero_ether_addr(ssid->go_p2p_dev_addr))
2001 return NULL;
2002
2003 value = os_malloc(20);
2004 if (value == NULL)
2005 return NULL;
2006 res = os_snprintf(value, 20, MACSTR, MAC2STR(ssid->go_p2p_dev_addr));
d85e1fc8 2007 if (os_snprintf_error(20, res)) {
9ec87666
JM
2008 os_free(value);
2009 return NULL;
2010 }
2011 value[20 - 1] = '\0';
2012 return value;
2013}
2014#endif /* NO_CONFIG_WRITE */
2015
2016
fbdcfd57
JM
2017static int wpa_config_parse_p2p_client_list(const struct parse_data *data,
2018 struct wpa_ssid *ssid, int line,
2019 const char *value)
2020{
b3d6a0a8
ST
2021 return wpa_config_parse_addr_list(data, line, value,
2022 &ssid->p2p_client_list,
2023 &ssid->num_p2p_clients,
79cd993a 2024 "p2p_client_list", 0, 0);
fbdcfd57
JM
2025}
2026
2027
2028#ifndef NO_CONFIG_WRITE
2029static char * wpa_config_write_p2p_client_list(const struct parse_data *data,
2030 struct wpa_ssid *ssid)
2031{
b3d6a0a8
ST
2032 return wpa_config_write_addr_list(data, ssid->p2p_client_list,
2033 ssid->num_p2p_clients,
2034 "p2p_client_list");
fbdcfd57
JM
2035}
2036#endif /* NO_CONFIG_WRITE */
2037
01a57fe4
JM
2038
2039static int wpa_config_parse_psk_list(const struct parse_data *data,
2040 struct wpa_ssid *ssid, int line,
2041 const char *value)
2042{
2043 struct psk_list_entry *p;
2044 const char *pos;
2045
2046 p = os_zalloc(sizeof(*p));
2047 if (p == NULL)
2048 return -1;
2049
2050 pos = value;
2051 if (os_strncmp(pos, "P2P-", 4) == 0) {
2052 p->p2p = 1;
2053 pos += 4;
2054 }
2055
2056 if (hwaddr_aton(pos, p->addr)) {
2057 wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list address '%s'",
2058 line, pos);
2059 os_free(p);
2060 return -1;
2061 }
2062 pos += 17;
2063 if (*pos != '-') {
2064 wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list '%s'",
2065 line, pos);
2066 os_free(p);
2067 return -1;
2068 }
2069 pos++;
2070
2071 if (hexstr2bin(pos, p->psk, PMK_LEN) || pos[PMK_LEN * 2] != '\0') {
2072 wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list PSK '%s'",
2073 line, pos);
2074 os_free(p);
2075 return -1;
2076 }
2077
2078 dl_list_add(&ssid->psk_list, &p->list);
2079
2080 return 0;
2081}
2082
2083
2084#ifndef NO_CONFIG_WRITE
2085static char * wpa_config_write_psk_list(const struct parse_data *data,
2086 struct wpa_ssid *ssid)
2087{
2088 return NULL;
2089}
2090#endif /* NO_CONFIG_WRITE */
2091
fbdcfd57
JM
2092#endif /* CONFIG_P2P */
2093
5cfb672d
JM
2094
2095#ifdef CONFIG_MESH
2096
2b2bb5a8
MH
2097static int wpa_config_parse_mesh_basic_rates(const struct parse_data *data,
2098 struct wpa_ssid *ssid, int line,
2099 const char *value)
2100{
2101 int *rates = wpa_config_parse_int_array(value);
2102
2103 if (rates == NULL) {
2104 wpa_printf(MSG_ERROR, "Line %d: Invalid mesh_basic_rates '%s'",
2105 line, value);
2106 return -1;
2107 }
2108 if (rates[0] == 0) {
2109 os_free(rates);
2110 rates = NULL;
2111 }
2112
2113 os_free(ssid->mesh_basic_rates);
2114 ssid->mesh_basic_rates = rates;
2115
2116 return 0;
2117}
2118
2119
5cfb672d 2120#ifndef NO_CONFIG_WRITE
2b2bb5a8 2121
2b2bb5a8
MH
2122static char * wpa_config_write_mesh_basic_rates(const struct parse_data *data,
2123 struct wpa_ssid *ssid)
2124{
2125 return wpa_config_write_freqs(data, ssid->mesh_basic_rates);
2126}
2127
5cfb672d
JM
2128#endif /* NO_CONFIG_WRITE */
2129
2130#endif /* CONFIG_MESH */
2131
2132
ad51731a
SD
2133#ifdef CONFIG_MACSEC
2134
2135static int wpa_config_parse_mka_cak(const struct parse_data *data,
2136 struct wpa_ssid *ssid, int line,
2137 const char *value)
2138{
871439b5
JM
2139 size_t len;
2140
2141 len = os_strlen(value);
2142 if (len > 2 * MACSEC_CAK_MAX_LEN ||
2143 (len != 2 * 16 && len != 2 * 32) ||
2144 hexstr2bin(value, ssid->mka_cak, len / 2)) {
ad51731a
SD
2145 wpa_printf(MSG_ERROR, "Line %d: Invalid MKA-CAK '%s'.",
2146 line, value);
2147 return -1;
2148 }
871439b5 2149 ssid->mka_cak_len = len / 2;
ad51731a
SD
2150 ssid->mka_psk_set |= MKA_PSK_SET_CAK;
2151
871439b5
JM
2152 wpa_hexdump_key(MSG_MSGDUMP, "MKA-CAK", ssid->mka_cak,
2153 ssid->mka_cak_len);
ad51731a
SD
2154 return 0;
2155}
2156
2157
2158static int wpa_config_parse_mka_ckn(const struct parse_data *data,
2159 struct wpa_ssid *ssid, int line,
2160 const char *value)
2161{
b678ed1e 2162 size_t len;
2163
2164 len = os_strlen(value);
2165 if (len > 2 * MACSEC_CKN_MAX_LEN || /* too long */
2166 len < 2 || /* too short */
2167 len % 2 != 0 /* not an integral number of bytes */) {
2168 wpa_printf(MSG_ERROR, "Line %d: Invalid MKA-CKN '%s'.",
2169 line, value);
2170 return -1;
2171 }
2172 ssid->mka_ckn_len = len / 2;
2173 if (hexstr2bin(value, ssid->mka_ckn, ssid->mka_ckn_len)) {
ad51731a
SD
2174 wpa_printf(MSG_ERROR, "Line %d: Invalid MKA-CKN '%s'.",
2175 line, value);
2176 return -1;
2177 }
2178
2179 ssid->mka_psk_set |= MKA_PSK_SET_CKN;
2180
b678ed1e 2181 wpa_hexdump_key(MSG_MSGDUMP, "MKA-CKN", ssid->mka_ckn,
2182 ssid->mka_ckn_len);
ad51731a
SD
2183 return 0;
2184}
2185
2186
2187#ifndef NO_CONFIG_WRITE
2188
2189static char * wpa_config_write_mka_cak(const struct parse_data *data,
2190 struct wpa_ssid *ssid)
2191{
2192 if (!(ssid->mka_psk_set & MKA_PSK_SET_CAK))
2193 return NULL;
2194
871439b5 2195 return wpa_config_write_string_hex(ssid->mka_cak, ssid->mka_cak_len);
ad51731a
SD
2196}
2197
2198
2199static char * wpa_config_write_mka_ckn(const struct parse_data *data,
2200 struct wpa_ssid *ssid)
2201{
2202 if (!(ssid->mka_psk_set & MKA_PSK_SET_CKN))
2203 return NULL;
b678ed1e 2204 return wpa_config_write_string_hex(ssid->mka_ckn, ssid->mka_ckn_len);
ad51731a
SD
2205}
2206
2207#endif /* NO_CONFIG_WRITE */
2208
2209#endif /* CONFIG_MACSEC */
2210
2211
ce6829c2
MV
2212#ifdef CONFIG_OCV
2213
2214static int wpa_config_parse_ocv(const struct parse_data *data,
2215 struct wpa_ssid *ssid, int line,
2216 const char *value)
2217{
2218 char *end;
2219
2220 ssid->ocv = strtol(value, &end, 0);
2221 if (*end || ssid->ocv < 0 || ssid->ocv > 1) {
2222 wpa_printf(MSG_ERROR, "Line %d: Invalid ocv value '%s'.",
2223 line, value);
2224 return -1;
2225 }
2226 if (ssid->ocv && ssid->ieee80211w == NO_MGMT_FRAME_PROTECTION)
2227 ssid->ieee80211w = MGMT_FRAME_PROTECTION_OPTIONAL;
2228 return 0;
2229}
2230
2231
2232#ifndef NO_CONFIG_WRITE
2233static char * wpa_config_write_ocv(const struct parse_data *data,
2234 struct wpa_ssid *ssid)
2235{
2236 char *value = os_malloc(20);
2237
2238 if (!value)
2239 return NULL;
2240 os_snprintf(value, 20, "%d", ssid->ocv);
2241 value[20 - 1] = '\0';
2242 return value;
2243}
2244#endif /* NO_CONFIG_WRITE */
2245
2246#endif /* CONFIG_OCV */
2247
2248
a0bf1b68
JM
2249static int wpa_config_parse_peerkey(const struct parse_data *data,
2250 struct wpa_ssid *ssid, int line,
2251 const char *value)
2252{
2253 wpa_printf(MSG_INFO, "NOTE: Obsolete peerkey parameter ignored");
2254 return 0;
2255}
2256
2257
2258#ifndef NO_CONFIG_WRITE
2259static char * wpa_config_write_peerkey(const struct parse_data *data,
2260 struct wpa_ssid *ssid)
2261{
2262 return NULL;
2263}
2264#endif /* NO_CONFIG_WRITE */
2265
2266
6fc6879b
JM
2267/* Helper macros for network block parser */
2268
2269#ifdef OFFSET
2270#undef OFFSET
2271#endif /* OFFSET */
2272/* OFFSET: Get offset of a variable within the wpa_ssid structure */
2273#define OFFSET(v) ((void *) &((struct wpa_ssid *) 0)->v)
2274
2275/* STR: Define a string variable for an ASCII string; f = field name */
2276#ifdef NO_CONFIG_WRITE
2277#define _STR(f) #f, wpa_config_parse_str, OFFSET(f)
b99c4cad 2278#define _STRe(f, m) #f, wpa_config_parse_str, OFFSET(eap.m)
6fc6879b
JM
2279#else /* NO_CONFIG_WRITE */
2280#define _STR(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(f)
b99c4cad
JM
2281#define _STRe(f, m) #f, wpa_config_parse_str, wpa_config_write_str, \
2282 OFFSET(eap.m)
6fc6879b
JM
2283#endif /* NO_CONFIG_WRITE */
2284#define STR(f) _STR(f), NULL, NULL, NULL, 0
b99c4cad 2285#define STRe(f, m) _STRe(f, m), NULL, NULL, NULL, 0
6fc6879b 2286#define STR_KEY(f) _STR(f), NULL, NULL, NULL, 1
b99c4cad 2287#define STR_KEYe(f, m) _STRe(f, m), NULL, NULL, NULL, 1
6fc6879b
JM
2288
2289/* STR_LEN: Define a string variable with a separate variable for storing the
2290 * data length. Unlike STR(), this can be used to store arbitrary binary data
2291 * (i.e., even nul termination character). */
2292#define _STR_LEN(f) _STR(f), OFFSET(f ## _len)
b99c4cad 2293#define _STR_LENe(f, m) _STRe(f, m), OFFSET(eap.m ## _len)
6fc6879b 2294#define STR_LEN(f) _STR_LEN(f), NULL, NULL, 0
b99c4cad 2295#define STR_LENe(f, m) _STR_LENe(f, m), NULL, NULL, 0
6fc6879b
JM
2296#define STR_LEN_KEY(f) _STR_LEN(f), NULL, NULL, 1
2297
2298/* STR_RANGE: Like STR_LEN(), but with minimum and maximum allowed length
2299 * explicitly specified. */
2300#define _STR_RANGE(f, min, max) _STR_LEN(f), (void *) (min), (void *) (max)
2301#define STR_RANGE(f, min, max) _STR_RANGE(f, min, max), 0
2302#define STR_RANGE_KEY(f, min, max) _STR_RANGE(f, min, max), 1
2303
2304#ifdef NO_CONFIG_WRITE
2305#define _INT(f) #f, wpa_config_parse_int, OFFSET(f), (void *) 0
b99c4cad 2306#define _INTe(f, m) #f, wpa_config_parse_int, OFFSET(eap.m), (void *) 0
6fc6879b
JM
2307#else /* NO_CONFIG_WRITE */
2308#define _INT(f) #f, wpa_config_parse_int, wpa_config_write_int, \
2309 OFFSET(f), (void *) 0
b99c4cad
JM
2310#define _INTe(f, m) #f, wpa_config_parse_int, wpa_config_write_int, \
2311 OFFSET(eap.m), (void *) 0
6fc6879b
JM
2312#endif /* NO_CONFIG_WRITE */
2313
2314/* INT: Define an integer variable */
2315#define INT(f) _INT(f), NULL, NULL, 0
b99c4cad 2316#define INTe(f, m) _INTe(f, m), NULL, NULL, 0
6fc6879b
JM
2317
2318/* INT_RANGE: Define an integer variable with allowed value range */
2319#define INT_RANGE(f, min, max) _INT(f), (void *) (min), (void *) (max), 0
2320
2321/* FUNC: Define a configuration variable that uses a custom function for
2322 * parsing and writing the value. */
2323#ifdef NO_CONFIG_WRITE
2324#define _FUNC(f) #f, wpa_config_parse_ ## f, NULL, NULL, NULL, NULL
2325#else /* NO_CONFIG_WRITE */
2326#define _FUNC(f) #f, wpa_config_parse_ ## f, wpa_config_write_ ## f, \
2327 NULL, NULL, NULL, NULL
2328#endif /* NO_CONFIG_WRITE */
2329#define FUNC(f) _FUNC(f), 0
2330#define FUNC_KEY(f) _FUNC(f), 1
2331
2332/*
2333 * Table of network configuration variables. This table is used to parse each
2334 * network configuration variable, e.g., each line in wpa_supplicant.conf file
2335 * that is inside a network block.
2336 *
2337 * This table is generated using the helper macros defined above and with
2338 * generous help from the C pre-processor. The field name is stored as a string
2339 * into .name and for STR and INT types, the offset of the target buffer within
2340 * struct wpa_ssid is stored in .param1. .param2 (if not NULL) is similar
2341 * offset to the field containing the length of the configuration variable.
2342 * .param3 and .param4 can be used to mark the allowed range (length for STR
2343 * and value for INT).
2344 *
2345 * For each configuration line in wpa_supplicant.conf, the parser goes through
2346 * this table and select the entry that matches with the field name. The parser
2347 * function (.parser) is then called to parse the actual value of the field.
2348 *
2349 * This kind of mechanism makes it easy to add new configuration parameters,
2350 * since only one line needs to be added into this table and into the
2351 * struct wpa_ssid definition if the new variable is either a string or
2352 * integer. More complex types will need to use their own parser and writer
2353 * functions.
2354 */
2355static const struct parse_data ssid_fields[] = {
eaa8eefe 2356 { STR_RANGE(ssid, 0, SSID_MAX_LEN) },
6fc6879b
JM
2357 { INT_RANGE(scan_ssid, 0, 1) },
2358 { FUNC(bssid) },
43a356b2 2359 { FUNC(bssid_hint) },
b83e4554
ST
2360 { FUNC(bssid_blacklist) },
2361 { FUNC(bssid_whitelist) },
6fc6879b 2362 { FUNC_KEY(psk) },
a52410c2 2363 { INT(mem_only_psk) },
a34ca59e 2364 { STR_KEY(sae_password) },
9be19d0b 2365 { STR(sae_password_id) },
6fc6879b
JM
2366 { FUNC(proto) },
2367 { FUNC(key_mgmt) },
1f6c0ab8 2368 { INT(bg_scan_period) },
6fc6879b
JM
2369 { FUNC(pairwise) },
2370 { FUNC(group) },
61a56c14 2371 { FUNC(group_mgmt) },
6fc6879b 2372 { FUNC(auth_alg) },
d3a98225 2373 { FUNC(scan_freq) },
b766a9a2 2374 { FUNC(freq_list) },
b07ff9cb 2375 { INT_RANGE(ht, 0, 1) },
2124a615
JB
2376 { INT_RANGE(vht, 0, 1) },
2377 { INT_RANGE(ht40, -1, 1) },
464dcfd0
JC
2378 { INT_RANGE(max_oper_chwidth, CHANWIDTH_USE_HT,
2379 CHANWIDTH_80P80MHZ) },
2124a615
JB
2380 { INT(vht_center_freq1) },
2381 { INT(vht_center_freq2) },
6fc6879b
JM
2382#ifdef IEEE8021X_EAPOL
2383 { FUNC(eap) },
b99c4cad
JM
2384 { STR_LENe(identity, identity) },
2385 { STR_LENe(anonymous_identity, anonymous_identity) },
2386 { STR_LENe(imsi_identity, imsi_identity) },
2387 { STR_LENe(machine_identity, machine_identity) },
556f5a2a 2388 { FUNC_KEY(password) },
c724a0a1 2389 { FUNC_KEY(machine_password) },
b99c4cad
JM
2390 { STRe(ca_cert, cert.ca_cert) },
2391 { STRe(ca_path, cert.ca_path) },
2392 { STRe(client_cert, cert.client_cert) },
2393 { STRe(private_key, cert.private_key) },
2394 { STR_KEYe(private_key_passwd, cert.private_key_passwd) },
2395 { STRe(dh_file, cert.dh_file) },
2396 { STRe(subject_match, cert.subject_match) },
2397 { STRe(check_cert_subject, cert.check_cert_subject) },
2398 { STRe(altsubject_match, cert.altsubject_match) },
2399 { STRe(domain_suffix_match, cert.domain_suffix_match) },
2400 { STRe(domain_match, cert.domain_match) },
2401 { STRe(ca_cert2, phase2_cert.ca_cert) },
2402 { STRe(ca_path2, phase2_cert.ca_path) },
2403 { STRe(client_cert2, phase2_cert.client_cert) },
2404 { STRe(private_key2, phase2_cert.private_key) },
2405 { STR_KEYe(private_key2_passwd, phase2_cert.private_key_passwd) },
2406 { STRe(dh_file2, phase2_cert.dh_file) },
2407 { STRe(subject_match2, phase2_cert.subject_match) },
2408 { STRe(check_cert_subject2, phase2_cert.check_cert_subject) },
2409 { STRe(altsubject_match2, phase2_cert.altsubject_match) },
2410 { STRe(domain_suffix_match2, phase2_cert.domain_suffix_match) },
2411 { STRe(domain_match2, phase2_cert.domain_match) },
2412 { STRe(phase1, phase1) },
2413 { STRe(phase2, phase2) },
68161824 2414 { STRe(machine_phase2, machine_phase2) },
b99c4cad
JM
2415 { STRe(pcsc, pcsc) },
2416 { STR_KEYe(pin, cert.pin) },
2417 { STRe(engine_id, cert.engine_id) },
2418 { STRe(key_id, cert.key_id) },
2419 { STRe(cert_id, cert.cert_id) },
2420 { STRe(ca_cert_id, cert.ca_cert_id) },
2421 { STR_KEYe(pin2, phase2_cert.pin) },
2422 { STRe(engine_id2, phase2_cert.engine_id) },
2423 { STRe(key_id2, phase2_cert.key_id) },
2424 { STRe(cert_id2, phase2_cert.cert_id) },
2425 { STRe(ca_cert_id2, phase2_cert.ca_cert_id) },
2426 { INTe(engine, cert.engine) },
2427 { INTe(engine2, phase2_cert.engine) },
68161824
JM
2428 { STRe(machine_ca_cert, machine_cert.ca_cert) },
2429 { STRe(machine_ca_path, machine_cert.ca_path) },
2430 { STRe(machine_client_cert, machine_cert.client_cert) },
2431 { STRe(machine_private_key, machine_cert.private_key) },
2432 { STR_KEYe(machine_private_key_passwd,
2433 machine_cert.private_key_passwd) },
2434 { STRe(machine_dh_file, machine_cert.dh_file) },
2435 { STRe(machine_subject_match, machine_cert.subject_match) },
2436 { STRe(machine_check_cert_subject, machine_cert.check_cert_subject) },
2437 { STRe(machine_altsubject_match, machine_cert.altsubject_match) },
2438 { STRe(machine_domain_suffix_match,
2439 machine_cert.domain_suffix_match) },
2440 { STRe(machine_domain_match, machine_cert.domain_match) },
2441 { STR_KEYe(machine_pin, machine_cert.pin) },
2442 { STRe(machine_engine_id, machine_cert.engine_id) },
2443 { STRe(machine_key_id, machine_cert.key_id) },
2444 { STRe(machine_cert_id, machine_cert.cert_id) },
2445 { STRe(machine_ca_cert_id, machine_cert.ca_cert_id) },
2446 { INTe(machine_engine, machine_cert.engine) },
2447 { INTe(machine_ocsp, machine_cert.ocsp) },
6fc6879b 2448 { INT(eapol_flags) },
b99c4cad
JM
2449 { INTe(sim_num, sim_num) },
2450 { STRe(openssl_ciphers, openssl_ciphers) },
2451 { INTe(erp, erp) },
6fc6879b
JM
2452#endif /* IEEE8021X_EAPOL */
2453 { FUNC_KEY(wep_key0) },
2454 { FUNC_KEY(wep_key1) },
2455 { FUNC_KEY(wep_key2) },
2456 { FUNC_KEY(wep_key3) },
2457 { INT(wep_tx_keyidx) },
2458 { INT(priority) },
2459#ifdef IEEE8021X_EAPOL
2460 { INT(eap_workaround) },
b99c4cad
JM
2461 { STRe(pac_file, pac_file) },
2462 { INTe(fragment_size, fragment_size) },
043de65f
JM
2463 { INTe(ocsp, cert.ocsp) },
2464 { INTe(ocsp2, phase2_cert.ocsp) },
6fc6879b 2465#endif /* IEEE8021X_EAPOL */
476e6bb6
TP
2466#ifdef CONFIG_MESH
2467 { INT_RANGE(mode, 0, 5) },
07cb45cc 2468 { INT_RANGE(no_auto_peer, 0, 1) },
31a856a1 2469 { INT_RANGE(mesh_rssi_threshold, -255, 1) },
476e6bb6 2470#else /* CONFIG_MESH */
2c5d725c 2471 { INT_RANGE(mode, 0, 4) },
476e6bb6 2472#endif /* CONFIG_MESH */
6fc6879b 2473 { INT_RANGE(proactive_key_caching, 0, 1) },
4dac0245 2474 { INT_RANGE(disabled, 0, 2) },
6fc6879b 2475 { STR(id_str) },
6fc6879b 2476 { INT_RANGE(ieee80211w, 0, 2) },
ce6829c2
MV
2477#ifdef CONFIG_OCV
2478 { FUNC(ocv) },
2479#endif /* CONFIG_OCV */
a0bf1b68 2480 { FUNC(peerkey) /* obsolete - removed */ },
6fc6879b 2481 { INT_RANGE(mixed_cell, 0, 1) },
dc6c3be4 2482 { INT_RANGE(frequency, 0, 70200) },
4d9e6fba 2483 { INT_RANGE(fixed_freq, 0, 1) },
d9909717
TB
2484#ifdef CONFIG_ACS
2485 { INT_RANGE(acs, 0, 1) },
2486#endif /* CONFIG_ACS */
5cfb672d 2487#ifdef CONFIG_MESH
2b2bb5a8 2488 { FUNC(mesh_basic_rates) },
e6096799
MH
2489 { INT(dot11MeshMaxRetries) },
2490 { INT(dot11MeshRetryTimeout) },
2491 { INT(dot11MeshConfirmTimeout) },
2492 { INT(dot11MeshHoldingTimeout) },
5cfb672d 2493#endif /* CONFIG_MESH */
60b94c98 2494 { INT(wpa_ptk_rekey) },
6c33ca9f 2495 { INT(group_rekey) },
60b94c98 2496 { STR(bgscan) },
e62f4ed0 2497 { INT_RANGE(ignore_broadcast_ssid, 0, 2) },
fbdcfd57 2498#ifdef CONFIG_P2P
9ec87666 2499 { FUNC(go_p2p_dev_addr) },
fbdcfd57 2500 { FUNC(p2p_client_list) },
01a57fe4 2501 { FUNC(psk_list) },
fbdcfd57 2502#endif /* CONFIG_P2P */
80e8a5ee
BG
2503#ifdef CONFIG_HT_OVERRIDES
2504 { INT_RANGE(disable_ht, 0, 1) },
2505 { INT_RANGE(disable_ht40, -1, 1) },
a90497f8 2506 { INT_RANGE(disable_sgi, 0, 1) },
39a5800f 2507 { INT_RANGE(disable_ldpc, 0, 1) },
d41cc8cc 2508 { INT_RANGE(ht40_intolerant, 0, 1) },
cdeea70f
SM
2509 { INT_RANGE(tx_stbc, -1, 1) },
2510 { INT_RANGE(rx_stbc, -1, 3) },
80e8a5ee
BG
2511 { INT_RANGE(disable_max_amsdu, -1, 1) },
2512 { INT_RANGE(ampdu_factor, -1, 3) },
2513 { INT_RANGE(ampdu_density, -1, 7) },
2514 { STR(ht_mcs) },
2515#endif /* CONFIG_HT_OVERRIDES */
e9ee8dc3
JB
2516#ifdef CONFIG_VHT_OVERRIDES
2517 { INT_RANGE(disable_vht, 0, 1) },
2518 { INT(vht_capa) },
2519 { INT(vht_capa_mask) },
2520 { INT_RANGE(vht_rx_mcs_nss_1, -1, 3) },
2521 { INT_RANGE(vht_rx_mcs_nss_2, -1, 3) },
2522 { INT_RANGE(vht_rx_mcs_nss_3, -1, 3) },
2523 { INT_RANGE(vht_rx_mcs_nss_4, -1, 3) },
2524 { INT_RANGE(vht_rx_mcs_nss_5, -1, 3) },
2525 { INT_RANGE(vht_rx_mcs_nss_6, -1, 3) },
2526 { INT_RANGE(vht_rx_mcs_nss_7, -1, 3) },
2527 { INT_RANGE(vht_rx_mcs_nss_8, -1, 3) },
2528 { INT_RANGE(vht_tx_mcs_nss_1, -1, 3) },
2529 { INT_RANGE(vht_tx_mcs_nss_2, -1, 3) },
2530 { INT_RANGE(vht_tx_mcs_nss_3, -1, 3) },
2531 { INT_RANGE(vht_tx_mcs_nss_4, -1, 3) },
2532 { INT_RANGE(vht_tx_mcs_nss_5, -1, 3) },
2533 { INT_RANGE(vht_tx_mcs_nss_6, -1, 3) },
2534 { INT_RANGE(vht_tx_mcs_nss_7, -1, 3) },
2535 { INT_RANGE(vht_tx_mcs_nss_8, -1, 3) },
2536#endif /* CONFIG_VHT_OVERRIDES */
07f53b8c 2537 { INT(ap_max_inactivity) },
fdfb1c8b 2538 { INT(dtim_period) },
18206e02 2539 { INT(beacon_int) },
dd10abcc
HW
2540#ifdef CONFIG_MACSEC
2541 { INT_RANGE(macsec_policy, 0, 1) },
7b4d546e 2542 { INT_RANGE(macsec_integ_only, 0, 1) },
e49b78c0
AK
2543 { INT_RANGE(macsec_replay_protect, 0, 1) },
2544 { INT(macsec_replay_window) },
e0d9fd34 2545 { INT_RANGE(macsec_port, 1, 65534) },
65dfa872 2546 { INT_RANGE(mka_priority, 0, 255) },
ad51731a
SD
2547 { FUNC_KEY(mka_cak) },
2548 { FUNC_KEY(mka_ckn) },
dd10abcc 2549#endif /* CONFIG_MACSEC */
e376290c
DS
2550#ifdef CONFIG_HS20
2551 { INT(update_identifier) },
6311547e 2552 { STR_RANGE(roaming_consortium_selection, 0, MAX_ROAMING_CONS_OI_LEN) },
e376290c 2553#endif /* CONFIG_HS20 */
a313d17d 2554 { INT_RANGE(mac_addr, 0, 2) },
90f14962 2555 { INT_RANGE(pbss, 0, 2) },
b6317b41 2556 { INT_RANGE(wps_disabled, 0, 1) },
76e20f4f 2557 { INT_RANGE(fils_dh_group, 0, 65535) },
b979caae
JM
2558#ifdef CONFIG_DPP
2559 { STR(dpp_connector) },
2560 { STR_LEN(dpp_netaccesskey) },
2561 { INT(dpp_netaccesskey_expiry) },
2562 { STR_LEN(dpp_csign) },
b979caae 2563#endif /* CONFIG_DPP */
ec9f4837 2564 { INT_RANGE(owe_group, 0, 65535) },
c1790a5f 2565 { INT_RANGE(owe_only, 0, 1) },
5abc7823 2566 { INT_RANGE(multi_ap_backhaul_sta, 0, 1) },
9083ef13 2567 { INT_RANGE(ft_eap_pmksa_caching, 0, 1) },
6fc6879b
JM
2568};
2569
2570#undef OFFSET
2571#undef _STR
2572#undef STR
2573#undef STR_KEY
2574#undef _STR_LEN
2575#undef STR_LEN
2576#undef STR_LEN_KEY
2577#undef _STR_RANGE
2578#undef STR_RANGE
2579#undef STR_RANGE_KEY
2580#undef _INT
2581#undef INT
2582#undef INT_RANGE
2583#undef _FUNC
2584#undef FUNC
2585#undef FUNC_KEY
e7ecab4a 2586#define NUM_SSID_FIELDS ARRAY_SIZE(ssid_fields)
6fc6879b
JM
2587
2588
2589/**
2590 * wpa_config_add_prio_network - Add a network to priority lists
2591 * @config: Configuration data from wpa_config_read()
2592 * @ssid: Pointer to the network configuration to be added to the list
2593 * Returns: 0 on success, -1 on failure
2594 *
2595 * This function is used to add a network block to the priority list of
2596 * networks. This must be called for each network when reading in the full
2597 * configuration. In addition, this can be used indirectly when updating
2598 * priorities by calling wpa_config_update_prio_list().
2599 */
2600int wpa_config_add_prio_network(struct wpa_config *config,
2601 struct wpa_ssid *ssid)
2602{
2603 int prio;
2604 struct wpa_ssid *prev, **nlist;
2605
2606 /*
2607 * Add to an existing priority list if one is available for the
2608 * configured priority level for this network.
2609 */
2610 for (prio = 0; prio < config->num_prio; prio++) {
2611 prev = config->pssid[prio];
2612 if (prev->priority == ssid->priority) {
2613 while (prev->pnext)
2614 prev = prev->pnext;
2615 prev->pnext = ssid;
2616 return 0;
2617 }
2618 }
2619
2620 /* First network for this priority - add a new priority list */
067ffa26
JM
2621 nlist = os_realloc_array(config->pssid, config->num_prio + 1,
2622 sizeof(struct wpa_ssid *));
6fc6879b
JM
2623 if (nlist == NULL)
2624 return -1;
2625
2626 for (prio = 0; prio < config->num_prio; prio++) {
2c60ca73
JM
2627 if (nlist[prio]->priority < ssid->priority) {
2628 os_memmove(&nlist[prio + 1], &nlist[prio],
2629 (config->num_prio - prio) *
2630 sizeof(struct wpa_ssid *));
6fc6879b 2631 break;
2c60ca73 2632 }
6fc6879b
JM
2633 }
2634
6fc6879b
JM
2635 nlist[prio] = ssid;
2636 config->num_prio++;
2637 config->pssid = nlist;
2638
2639 return 0;
2640}
2641
2642
2643/**
2644 * wpa_config_update_prio_list - Update network priority list
2645 * @config: Configuration data from wpa_config_read()
2646 * Returns: 0 on success, -1 on failure
2647 *
2648 * This function is called to update the priority list of networks in the
2649 * configuration when a network is being added or removed. This is also called
2650 * if a priority for a network is changed.
2651 */
aa53509f 2652int wpa_config_update_prio_list(struct wpa_config *config)
6fc6879b
JM
2653{
2654 struct wpa_ssid *ssid;
2655 int ret = 0;
2656
2657 os_free(config->pssid);
2658 config->pssid = NULL;
2659 config->num_prio = 0;
2660
2661 ssid = config->ssid;
2662 while (ssid) {
2663 ssid->pnext = NULL;
2664 if (wpa_config_add_prio_network(config, ssid) < 0)
2665 ret = -1;
2666 ssid = ssid->next;
2667 }
2668
2669 return ret;
2670}
2671
2672
2673#ifdef IEEE8021X_EAPOL
b99c4cad
JM
2674
2675static void eap_peer_config_free_cert(struct eap_peer_cert_config *cert)
2676{
2677 os_free(cert->ca_cert);
2678 os_free(cert->ca_path);
2679 os_free(cert->client_cert);
2680 os_free(cert->private_key);
2681 str_clear_free(cert->private_key_passwd);
2682 os_free(cert->dh_file);
2683 os_free(cert->subject_match);
2684 os_free(cert->check_cert_subject);
2685 os_free(cert->altsubject_match);
2686 os_free(cert->domain_suffix_match);
2687 os_free(cert->domain_match);
2688 str_clear_free(cert->pin);
2689 os_free(cert->engine_id);
2690 os_free(cert->key_id);
2691 os_free(cert->cert_id);
2692 os_free(cert->ca_cert_id);
2693}
2694
2695
6fc6879b
JM
2696static void eap_peer_config_free(struct eap_peer_config *eap)
2697{
2698 os_free(eap->eap_methods);
19c48da0 2699 bin_clear_free(eap->identity, eap->identity_len);
6fc6879b 2700 os_free(eap->anonymous_identity);
9e834fc6 2701 os_free(eap->imsi_identity);
c724a0a1 2702 os_free(eap->machine_identity);
19c48da0 2703 bin_clear_free(eap->password, eap->password_len);
c724a0a1 2704 bin_clear_free(eap->machine_password, eap->machine_password_len);
b99c4cad
JM
2705 eap_peer_config_free_cert(&eap->cert);
2706 eap_peer_config_free_cert(&eap->phase2_cert);
68161824 2707 eap_peer_config_free_cert(&eap->machine_cert);
6fc6879b
JM
2708 os_free(eap->phase1);
2709 os_free(eap->phase2);
68161824 2710 os_free(eap->machine_phase2);
6fc6879b 2711 os_free(eap->pcsc);
6fc6879b
JM
2712 os_free(eap->otp);
2713 os_free(eap->pending_req_otp);
2714 os_free(eap->pac_file);
19c48da0
JM
2715 bin_clear_free(eap->new_password, eap->new_password_len);
2716 str_clear_free(eap->external_sim_resp);
07e2de31 2717 os_free(eap->openssl_ciphers);
6fc6879b 2718}
b99c4cad 2719
6fc6879b
JM
2720#endif /* IEEE8021X_EAPOL */
2721
2722
2723/**
2724 * wpa_config_free_ssid - Free network/ssid configuration data
2725 * @ssid: Configuration data for the network
2726 *
2727 * This function frees all resources allocated for the network configuration
2728 * data.
2729 */
2730void wpa_config_free_ssid(struct wpa_ssid *ssid)
2731{
01a57fe4
JM
2732 struct psk_list_entry *psk;
2733
6fc6879b 2734 os_free(ssid->ssid);
19c48da0 2735 str_clear_free(ssid->passphrase);
9173b16f 2736 os_free(ssid->ext_psk);
a34ca59e 2737 str_clear_free(ssid->sae_password);
9be19d0b 2738 os_free(ssid->sae_password_id);
6fc6879b
JM
2739#ifdef IEEE8021X_EAPOL
2740 eap_peer_config_free(&ssid->eap);
2741#endif /* IEEE8021X_EAPOL */
2742 os_free(ssid->id_str);
d3a98225 2743 os_free(ssid->scan_freq);
b766a9a2 2744 os_free(ssid->freq_list);
60b94c98 2745 os_free(ssid->bgscan);
fbdcfd57 2746 os_free(ssid->p2p_client_list);
79cd993a
ST
2747 os_free(ssid->bssid_blacklist);
2748 os_free(ssid->bssid_whitelist);
80e8a5ee
BG
2749#ifdef CONFIG_HT_OVERRIDES
2750 os_free(ssid->ht_mcs);
2751#endif /* CONFIG_HT_OVERRIDES */
2b2bb5a8
MH
2752#ifdef CONFIG_MESH
2753 os_free(ssid->mesh_basic_rates);
2754#endif /* CONFIG_MESH */
6311547e
JM
2755#ifdef CONFIG_HS20
2756 os_free(ssid->roaming_consortium_selection);
2757#endif /* CONFIG_HS20 */
b979caae
JM
2758 os_free(ssid->dpp_connector);
2759 bin_clear_free(ssid->dpp_netaccesskey, ssid->dpp_netaccesskey_len);
2760 os_free(ssid->dpp_csign);
01a57fe4
JM
2761 while ((psk = dl_list_first(&ssid->psk_list, struct psk_list_entry,
2762 list))) {
2763 dl_list_del(&psk->list);
6df19739 2764 bin_clear_free(psk, sizeof(*psk));
01a57fe4 2765 }
6df19739 2766 bin_clear_free(ssid, sizeof(*ssid));
6fc6879b
JM
2767}
2768
2769
1bb7b8e8
JM
2770void wpa_config_free_cred(struct wpa_cred *cred)
2771{
463c8ffb
JM
2772 size_t i;
2773
1bb7b8e8 2774 os_free(cred->realm);
19c48da0
JM
2775 str_clear_free(cred->username);
2776 str_clear_free(cred->password);
1bb7b8e8 2777 os_free(cred->ca_cert);
11e4f46a
JM
2778 os_free(cred->client_cert);
2779 os_free(cred->private_key);
19c48da0 2780 str_clear_free(cred->private_key_passwd);
1bb7b8e8 2781 os_free(cred->imsi);
19c48da0 2782 str_clear_free(cred->milenage);
463c8ffb
JM
2783 for (i = 0; i < cred->num_domain; i++)
2784 os_free(cred->domain[i]);
1bb7b8e8 2785 os_free(cred->domain);
ac1bc549 2786 os_free(cred->domain_suffix_match);
8ca93c59
JM
2787 os_free(cred->eap_method);
2788 os_free(cred->phase1);
2789 os_free(cred->phase2);
dbea8ac7 2790 os_free(cred->excluded_ssid);
bc00053c 2791 os_free(cred->roaming_partner);
aa26ba68 2792 os_free(cred->provisioning_sp);
33fb8c52
JM
2793 for (i = 0; i < cred->num_req_conn_capab; i++)
2794 os_free(cred->req_conn_capab_port[i]);
2795 os_free(cred->req_conn_capab_port);
2796 os_free(cred->req_conn_capab_proto);
1bb7b8e8
JM
2797 os_free(cred);
2798}
2799
2800
d9bb2821
JM
2801void wpa_config_flush_blobs(struct wpa_config *config)
2802{
2803#ifndef CONFIG_NO_CONFIG_BLOBS
2804 struct wpa_config_blob *blob, *prev;
2805
2806 blob = config->blobs;
2807 config->blobs = NULL;
2808 while (blob) {
2809 prev = blob;
2810 blob = blob->next;
2811 wpa_config_free_blob(prev);
2812 }
2813#endif /* CONFIG_NO_CONFIG_BLOBS */
2814}
2815
2816
6fc6879b
JM
2817/**
2818 * wpa_config_free - Free configuration data
2819 * @config: Configuration data from wpa_config_read()
2820 *
2821 * This function frees all resources allocated for the configuration data by
2822 * wpa_config_read().
2823 */
2824void wpa_config_free(struct wpa_config *config)
2825{
6fc6879b 2826 struct wpa_ssid *ssid, *prev = NULL;
1bb7b8e8 2827 struct wpa_cred *cred, *cprev;
54e06b4f 2828 int i;
e3768e7c 2829
6fc6879b
JM
2830 ssid = config->ssid;
2831 while (ssid) {
2832 prev = ssid;
2833 ssid = ssid->next;
2834 wpa_config_free_ssid(prev);
2835 }
2836
1bb7b8e8
JM
2837 cred = config->cred;
2838 while (cred) {
2839 cprev = cred;
2840 cred = cred->next;
2841 wpa_config_free_cred(cprev);
2842 }
2843
d9bb2821 2844 wpa_config_flush_blobs(config);
6fc6879b 2845
71dd3b78 2846 wpabuf_free(config->wps_vendor_ext_m1);
54e06b4f
JM
2847 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++)
2848 wpabuf_free(config->wps_vendor_ext[i]);
6fc6879b
JM
2849 os_free(config->ctrl_interface);
2850 os_free(config->ctrl_interface_group);
6fc6879b
JM
2851 os_free(config->opensc_engine_path);
2852 os_free(config->pkcs11_engine_path);
2853 os_free(config->pkcs11_module_path);
07e2de31 2854 os_free(config->openssl_ciphers);
f64adcd7 2855 os_free(config->pcsc_reader);
19c48da0 2856 str_clear_free(config->pcsc_pin);
6fc6879b 2857 os_free(config->driver_param);
3c0b7aa4
JM
2858 os_free(config->device_name);
2859 os_free(config->manufacturer);
2860 os_free(config->model_name);
2861 os_free(config->model_number);
2862 os_free(config->serial_number);
c0e4dd9e 2863 os_free(config->config_methods);
e3768e7c 2864 os_free(config->p2p_ssid_postfix);
6fc6879b 2865 os_free(config->pssid);
21d996f7 2866 os_free(config->p2p_pref_chan);
556b30da 2867 os_free(config->p2p_no_go_freq.range);
b0786fba 2868 os_free(config->autoscan);
f5ffc348 2869 os_free(config->freq_list);
3f2c8ba6
JM
2870 wpabuf_free(config->wps_nfc_dh_pubkey);
2871 wpabuf_free(config->wps_nfc_dh_privkey);
2872 wpabuf_free(config->wps_nfc_dev_pw);
306ae225 2873 os_free(config->ext_password_backend);
625f202a 2874 os_free(config->sae_groups);
18a2eaab 2875 wpabuf_free(config->ap_vendor_elements);
b572df86 2876 os_free(config->osu_dir);
dd09e424 2877 os_free(config->bgscan);
e4fa8b12 2878 os_free(config->wowlan_triggers);
76ca15b7 2879 os_free(config->fst_group_id);
32c02261 2880 os_free(config->sched_scan_plans);
facf2c72
DS
2881#ifdef CONFIG_MBO
2882 os_free(config->non_pref_chan);
2883#endif /* CONFIG_MBO */
32c02261 2884
6fc6879b
JM
2885 os_free(config);
2886}
2887
2888
7c49fdd0
SL
2889/**
2890 * wpa_config_foreach_network - Iterate over each configured network
2891 * @config: Configuration data from wpa_config_read()
2892 * @func: Callback function to process each network
2893 * @arg: Opaque argument to pass to callback function
2894 *
2895 * Iterate over the set of configured networks calling the specified
2896 * function for each item. We guard against callbacks removing the
2897 * supplied network.
2898 */
2899void wpa_config_foreach_network(struct wpa_config *config,
2900 void (*func)(void *, struct wpa_ssid *),
2901 void *arg)
2902{
2903 struct wpa_ssid *ssid, *next;
2904
2905 ssid = config->ssid;
2906 while (ssid) {
2907 next = ssid->next;
2908 func(arg, ssid);
2909 ssid = next;
2910 }
2911}
2912
2913
6fc6879b
JM
2914/**
2915 * wpa_config_get_network - Get configured network based on id
2916 * @config: Configuration data from wpa_config_read()
2917 * @id: Unique network id to search for
2918 * Returns: Network configuration or %NULL if not found
2919 */
2920struct wpa_ssid * wpa_config_get_network(struct wpa_config *config, int id)
2921{
2922 struct wpa_ssid *ssid;
2923
2924 ssid = config->ssid;
2925 while (ssid) {
2926 if (id == ssid->id)
2927 break;
2928 ssid = ssid->next;
2929 }
2930
2931 return ssid;
2932}
2933
2934
2935/**
2936 * wpa_config_add_network - Add a new network with empty configuration
2937 * @config: Configuration data from wpa_config_read()
2938 * Returns: The new network configuration or %NULL if operation failed
2939 */
2940struct wpa_ssid * wpa_config_add_network(struct wpa_config *config)
2941{
2942 int id;
2943 struct wpa_ssid *ssid, *last = NULL;
2944
2945 id = -1;
2946 ssid = config->ssid;
2947 while (ssid) {
2948 if (ssid->id > id)
2949 id = ssid->id;
2950 last = ssid;
2951 ssid = ssid->next;
2952 }
2953 id++;
2954
2955 ssid = os_zalloc(sizeof(*ssid));
2956 if (ssid == NULL)
2957 return NULL;
2958 ssid->id = id;
01a57fe4 2959 dl_list_init(&ssid->psk_list);
6fc6879b
JM
2960 if (last)
2961 last->next = ssid;
2962 else
2963 config->ssid = ssid;
2964
2965 wpa_config_update_prio_list(config);
2966
2967 return ssid;
2968}
2969
2970
2971/**
2972 * wpa_config_remove_network - Remove a configured network based on id
2973 * @config: Configuration data from wpa_config_read()
2974 * @id: Unique network id to search for
2975 * Returns: 0 on success, or -1 if the network was not found
2976 */
2977int wpa_config_remove_network(struct wpa_config *config, int id)
2978{
2979 struct wpa_ssid *ssid, *prev = NULL;
2980
2981 ssid = config->ssid;
2982 while (ssid) {
2983 if (id == ssid->id)
2984 break;
2985 prev = ssid;
2986 ssid = ssid->next;
2987 }
2988
2989 if (ssid == NULL)
2990 return -1;
2991
2992 if (prev)
2993 prev->next = ssid->next;
2994 else
2995 config->ssid = ssid->next;
2996
2997 wpa_config_update_prio_list(config);
2998 wpa_config_free_ssid(ssid);
2999 return 0;
3000}
3001
3002
3003/**
3004 * wpa_config_set_network_defaults - Set network default values
3005 * @ssid: Pointer to network configuration data
3006 */
3007void wpa_config_set_network_defaults(struct wpa_ssid *ssid)
3008{
3009 ssid->proto = DEFAULT_PROTO;
3010 ssid->pairwise_cipher = DEFAULT_PAIRWISE;
3011 ssid->group_cipher = DEFAULT_GROUP;
3012 ssid->key_mgmt = DEFAULT_KEY_MGMT;
1f6c0ab8 3013 ssid->bg_scan_period = DEFAULT_BG_SCAN_PERIOD;
b07ff9cb 3014 ssid->ht = 1;
6fc6879b
JM
3015#ifdef IEEE8021X_EAPOL
3016 ssid->eapol_flags = DEFAULT_EAPOL_FLAGS;
3017 ssid->eap_workaround = DEFAULT_EAP_WORKAROUND;
3018 ssid->eap.fragment_size = DEFAULT_FRAGMENT_SIZE;
13f6a07e 3019 ssid->eap.sim_num = DEFAULT_USER_SELECTED_SIM;
6fc6879b 3020#endif /* IEEE8021X_EAPOL */
5cfb672d 3021#ifdef CONFIG_MESH
e6096799
MH
3022 ssid->dot11MeshMaxRetries = DEFAULT_MESH_MAX_RETRIES;
3023 ssid->dot11MeshRetryTimeout = DEFAULT_MESH_RETRY_TIMEOUT;
3024 ssid->dot11MeshConfirmTimeout = DEFAULT_MESH_CONFIRM_TIMEOUT;
3025 ssid->dot11MeshHoldingTimeout = DEFAULT_MESH_HOLDING_TIMEOUT;
31a856a1 3026 ssid->mesh_rssi_threshold = DEFAULT_MESH_RSSI_THRESHOLD;
5cfb672d 3027#endif /* CONFIG_MESH */
80e8a5ee
BG
3028#ifdef CONFIG_HT_OVERRIDES
3029 ssid->disable_ht = DEFAULT_DISABLE_HT;
3030 ssid->disable_ht40 = DEFAULT_DISABLE_HT40;
a90497f8 3031 ssid->disable_sgi = DEFAULT_DISABLE_SGI;
39a5800f 3032 ssid->disable_ldpc = DEFAULT_DISABLE_LDPC;
cdeea70f
SM
3033 ssid->tx_stbc = DEFAULT_TX_STBC;
3034 ssid->rx_stbc = DEFAULT_RX_STBC;
80e8a5ee
BG
3035 ssid->disable_max_amsdu = DEFAULT_DISABLE_MAX_AMSDU;
3036 ssid->ampdu_factor = DEFAULT_AMPDU_FACTOR;
3037 ssid->ampdu_density = DEFAULT_AMPDU_DENSITY;
3038#endif /* CONFIG_HT_OVERRIDES */
e9ee8dc3
JB
3039#ifdef CONFIG_VHT_OVERRIDES
3040 ssid->vht_rx_mcs_nss_1 = -1;
3041 ssid->vht_rx_mcs_nss_2 = -1;
3042 ssid->vht_rx_mcs_nss_3 = -1;
3043 ssid->vht_rx_mcs_nss_4 = -1;
3044 ssid->vht_rx_mcs_nss_5 = -1;
3045 ssid->vht_rx_mcs_nss_6 = -1;
3046 ssid->vht_rx_mcs_nss_7 = -1;
3047 ssid->vht_rx_mcs_nss_8 = -1;
3048 ssid->vht_tx_mcs_nss_1 = -1;
3049 ssid->vht_tx_mcs_nss_2 = -1;
3050 ssid->vht_tx_mcs_nss_3 = -1;
3051 ssid->vht_tx_mcs_nss_4 = -1;
3052 ssid->vht_tx_mcs_nss_5 = -1;
3053 ssid->vht_tx_mcs_nss_6 = -1;
3054 ssid->vht_tx_mcs_nss_7 = -1;
3055 ssid->vht_tx_mcs_nss_8 = -1;
3056#endif /* CONFIG_VHT_OVERRIDES */
6e202021 3057 ssid->proactive_key_caching = -1;
62d49803 3058 ssid->ieee80211w = MGMT_FRAME_PROTECTION_DEFAULT;
65dfa872
BA
3059#ifdef CONFIG_MACSEC
3060 ssid->mka_priority = DEFAULT_PRIO_NOT_KEY_SERVER;
3061#endif /* CONFIG_MACSEC */
c267753b 3062 ssid->mac_addr = -1;
806db174 3063 ssid->max_oper_chwidth = DEFAULT_MAX_OPER_CHWIDTH;
6fc6879b
JM
3064}
3065
3066
3067/**
3068 * wpa_config_set - Set a variable in network configuration
3069 * @ssid: Pointer to network configuration data
3070 * @var: Variable name, e.g., "ssid"
3071 * @value: Variable value
3072 * @line: Line number in configuration file or 0 if not used
5cd317d3
BKB
3073 * Returns: 0 on success with possible change in the value, 1 on success with
3074 * no change to previously configured value, or -1 on failure
6fc6879b
JM
3075 *
3076 * This function can be used to set network configuration variables based on
3077 * both the configuration file and management interface input. The value
3078 * parameter must be in the same format as the text-based configuration file is
3079 * using. For example, strings are using double quotation marks.
3080 */
3081int wpa_config_set(struct wpa_ssid *ssid, const char *var, const char *value,
3082 int line)
3083{
3084 size_t i;
3085 int ret = 0;
3086
3087 if (ssid == NULL || var == NULL || value == NULL)
3088 return -1;
3089
3090 for (i = 0; i < NUM_SSID_FIELDS; i++) {
3091 const struct parse_data *field = &ssid_fields[i];
3092 if (os_strcmp(var, field->name) != 0)
3093 continue;
3094
5cd317d3
BKB
3095 ret = field->parser(field, ssid, line, value);
3096 if (ret < 0) {
6fc6879b
JM
3097 if (line) {
3098 wpa_printf(MSG_ERROR, "Line %d: failed to "
3099 "parse %s '%s'.", line, var, value);
3100 }
3101 ret = -1;
3102 }
3103 break;
3104 }
3105 if (i == NUM_SSID_FIELDS) {
3106 if (line) {
3107 wpa_printf(MSG_ERROR, "Line %d: unknown network field "
3108 "'%s'.", line, var);
3109 }
3110 ret = -1;
3111 }
3112
3113 return ret;
3114}
3115
3116
67e1b984
JM
3117int wpa_config_set_quoted(struct wpa_ssid *ssid, const char *var,
3118 const char *value)
3119{
3120 size_t len;
3121 char *buf;
3122 int ret;
3123
3124 len = os_strlen(value);
3125 buf = os_malloc(len + 3);
3126 if (buf == NULL)
3127 return -1;
3128 buf[0] = '"';
3129 os_memcpy(buf + 1, value, len);
3130 buf[len + 1] = '"';
3131 buf[len + 2] = '\0';
3132 ret = wpa_config_set(ssid, var, buf, 0);
3133 os_free(buf);
3134 return ret;
3135}
3136
3137
3d3d3056
WS
3138/**
3139 * wpa_config_get_all - Get all options from network configuration
3140 * @ssid: Pointer to network configuration data
3141 * @get_keys: Determines if keys/passwords will be included in returned list
d1c8ac88 3142 * (if they may be exported)
3d3d3056
WS
3143 * Returns: %NULL terminated list of all set keys and their values in the form
3144 * of [key1, val1, key2, val2, ... , NULL]
3145 *
3146 * This function can be used to get list of all configured network properties.
3147 * The caller is responsible for freeing the returned list and all its
3148 * elements.
3149 */
3150char ** wpa_config_get_all(struct wpa_ssid *ssid, int get_keys)
3151{
954f03aa
JM
3152#ifdef NO_CONFIG_WRITE
3153 return NULL;
3154#else /* NO_CONFIG_WRITE */
3d3d3056
WS
3155 const struct parse_data *field;
3156 char *key, *value;
3157 size_t i;
3158 char **props;
3159 int fields_num;
3160
d1c8ac88
JB
3161 get_keys = get_keys && ssid->export_keys;
3162
f9884c09 3163 props = os_calloc(2 * NUM_SSID_FIELDS + 1, sizeof(char *));
3d3d3056
WS
3164 if (!props)
3165 return NULL;
3166
3167 fields_num = 0;
3168 for (i = 0; i < NUM_SSID_FIELDS; i++) {
3169 field = &ssid_fields[i];
3170 if (field->key_data && !get_keys)
3171 continue;
3172 value = field->writer(field, ssid);
04746865 3173 if (value == NULL)
3d3d3056 3174 continue;
04746865
JM
3175 if (os_strlen(value) == 0) {
3176 os_free(value);
3177 continue;
3178 }
3d3d3056
WS
3179
3180 key = os_strdup(field->name);
04746865
JM
3181 if (key == NULL) {
3182 os_free(value);
3d3d3056 3183 goto err;
04746865 3184 }
3d3d3056
WS
3185
3186 props[fields_num * 2] = key;
3187 props[fields_num * 2 + 1] = value;
3188
3189 fields_num++;
3190 }
3191
3192 return props;
3193
3194err:
8329ad4d
JM
3195 for (i = 0; props[i]; i++)
3196 os_free(props[i]);
3d3d3056
WS
3197 os_free(props);
3198 return NULL;
954f03aa 3199#endif /* NO_CONFIG_WRITE */
3d3d3056
WS
3200}
3201
3202
6fc6879b
JM
3203#ifndef NO_CONFIG_WRITE
3204/**
3205 * wpa_config_get - Get a variable in network configuration
3206 * @ssid: Pointer to network configuration data
3207 * @var: Variable name, e.g., "ssid"
3208 * Returns: Value of the variable or %NULL on failure
3209 *
3210 * This function can be used to get network configuration variables. The
3211 * returned value is a copy of the configuration variable in text format, i.e,.
3212 * the same format that the text-based configuration file and wpa_config_set()
3213 * are using for the value. The caller is responsible for freeing the returned
3214 * value.
3215 */
3216char * wpa_config_get(struct wpa_ssid *ssid, const char *var)
3217{
3218 size_t i;
3219
3220 if (ssid == NULL || var == NULL)
3221 return NULL;
3222
3223 for (i = 0; i < NUM_SSID_FIELDS; i++) {
3224 const struct parse_data *field = &ssid_fields[i];
0fe5a234
PS
3225 if (os_strcmp(var, field->name) == 0) {
3226 char *ret = field->writer(field, ssid);
3227
3228 if (ret && has_newline(ret)) {
3229 wpa_printf(MSG_ERROR,
3230 "Found newline in value for %s; not returning it",
3231 var);
3232 os_free(ret);
3233 ret = NULL;
3234 }
3235
3236 return ret;
3237 }
6fc6879b
JM
3238 }
3239
3240 return NULL;
3241}
3242
3243
3244/**
3245 * wpa_config_get_no_key - Get a variable in network configuration (no keys)
3246 * @ssid: Pointer to network configuration data
3247 * @var: Variable name, e.g., "ssid"
3248 * Returns: Value of the variable or %NULL on failure
3249 *
3250 * This function can be used to get network configuration variable like
3251 * wpa_config_get(). The only difference is that this functions does not expose
3252 * key/password material from the configuration. In case a key/password field
3253 * is requested, the returned value is an empty string or %NULL if the variable
3254 * is not set or "*" if the variable is set (regardless of its value). The
3255 * returned value is a copy of the configuration variable in text format, i.e,.
3256 * the same format that the text-based configuration file and wpa_config_set()
3257 * are using for the value. The caller is responsible for freeing the returned
3258 * value.
3259 */
3260char * wpa_config_get_no_key(struct wpa_ssid *ssid, const char *var)
3261{
3262 size_t i;
3263
3264 if (ssid == NULL || var == NULL)
3265 return NULL;
3266
3267 for (i = 0; i < NUM_SSID_FIELDS; i++) {
3268 const struct parse_data *field = &ssid_fields[i];
3269 if (os_strcmp(var, field->name) == 0) {
3270 char *res = field->writer(field, ssid);
3271 if (field->key_data) {
3272 if (res && res[0]) {
3273 wpa_printf(MSG_DEBUG, "Do not allow "
3274 "key_data field to be "
3275 "exposed");
19c48da0 3276 str_clear_free(res);
6fc6879b
JM
3277 return os_strdup("*");
3278 }
3279
3280 os_free(res);
3281 return NULL;
3282 }
3283 return res;
3284 }
3285 }
3286
3287 return NULL;
3288}
3289#endif /* NO_CONFIG_WRITE */
3290
3291
3292/**
3293 * wpa_config_update_psk - Update WPA PSK based on passphrase and SSID
3294 * @ssid: Pointer to network configuration data
3295 *
3296 * This function must be called to update WPA PSK when either SSID or the
3297 * passphrase has changed for the network configuration.
3298 */
3299void wpa_config_update_psk(struct wpa_ssid *ssid)
3300{
3301#ifndef CONFIG_NO_PBKDF2
986de33d 3302 pbkdf2_sha1(ssid->passphrase, ssid->ssid, ssid->ssid_len, 4096,
6fc6879b
JM
3303 ssid->psk, PMK_LEN);
3304 wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
3305 ssid->psk, PMK_LEN);
3306 ssid->psk_set = 1;
3307#endif /* CONFIG_NO_PBKDF2 */
3308}
3309
3310
33fb8c52
JM
3311static int wpa_config_set_cred_req_conn_capab(struct wpa_cred *cred,
3312 const char *value)
3313{
3314 u8 *proto;
3315 int **port;
3316 int *ports, *nports;
3317 const char *pos;
3318 unsigned int num_ports;
3319
3320 proto = os_realloc_array(cred->req_conn_capab_proto,
3321 cred->num_req_conn_capab + 1, sizeof(u8));
3322 if (proto == NULL)
3323 return -1;
3324 cred->req_conn_capab_proto = proto;
3325
3326 port = os_realloc_array(cred->req_conn_capab_port,
3327 cred->num_req_conn_capab + 1, sizeof(int *));
3328 if (port == NULL)
3329 return -1;
3330 cred->req_conn_capab_port = port;
3331
3332 proto[cred->num_req_conn_capab] = atoi(value);
3333
3334 pos = os_strchr(value, ':');
3335 if (pos == NULL) {
3336 port[cred->num_req_conn_capab] = NULL;
3337 cred->num_req_conn_capab++;
3338 return 0;
3339 }
3340 pos++;
3341
3342 ports = NULL;
3343 num_ports = 0;
3344
3345 while (*pos) {
3346 nports = os_realloc_array(ports, num_ports + 1, sizeof(int));
3347 if (nports == NULL) {
3348 os_free(ports);
3349 return -1;
3350 }
3351 ports = nports;
3352 ports[num_ports++] = atoi(pos);
3353
3354 pos = os_strchr(pos, ',');
3355 if (pos == NULL)
3356 break;
3357 pos++;
3358 }
3359
3360 nports = os_realloc_array(ports, num_ports + 1, sizeof(int));
3361 if (nports == NULL) {
3362 os_free(ports);
3363 return -1;
3364 }
3365 ports = nports;
3366 ports[num_ports] = -1;
3367
3368 port[cred->num_req_conn_capab] = ports;
3369 cred->num_req_conn_capab++;
3370 return 0;
3371}
3372
3373
909a948b
JM
3374static int wpa_config_set_cred_roaming_consortiums(struct wpa_cred *cred,
3375 const char *value)
3376{
3377 u8 roaming_consortiums[MAX_ROAMING_CONS][MAX_ROAMING_CONS_OI_LEN];
3378 size_t roaming_consortiums_len[MAX_ROAMING_CONS];
3379 unsigned int num_roaming_consortiums = 0;
3380 const char *pos, *end;
3381 size_t len;
3382
3383 os_memset(roaming_consortiums, 0, sizeof(roaming_consortiums));
3384 os_memset(roaming_consortiums_len, 0, sizeof(roaming_consortiums_len));
3385
3386 for (pos = value;;) {
3387 end = os_strchr(pos, ',');
3388 len = end ? (size_t) (end - pos) : os_strlen(pos);
3389 if (!end && len == 0)
3390 break;
3391 if (len == 0 || (len & 1) != 0 ||
3392 len / 2 > MAX_ROAMING_CONS_OI_LEN ||
3393 hexstr2bin(pos,
3394 roaming_consortiums[num_roaming_consortiums],
3395 len / 2) < 0) {
3396 wpa_printf(MSG_INFO,
3397 "Invalid roaming_consortiums entry: %s",
3398 pos);
3399 return -1;
3400 }
3401 roaming_consortiums_len[num_roaming_consortiums] = len / 2;
3402 num_roaming_consortiums++;
ac0ac1dd
AO
3403
3404 if (!end)
3405 break;
3406
3407 if (num_roaming_consortiums >= MAX_ROAMING_CONS) {
909a948b
JM
3408 wpa_printf(MSG_INFO,
3409 "Too many roaming_consortiums OIs");
3410 return -1;
3411 }
3412
909a948b
JM
3413 pos = end + 1;
3414 }
3415
3416 os_memcpy(cred->roaming_consortiums, roaming_consortiums,
3417 sizeof(roaming_consortiums));
3418 os_memcpy(cred->roaming_consortiums_len, roaming_consortiums_len,
3419 sizeof(roaming_consortiums_len));
3420 cred->num_roaming_consortiums = num_roaming_consortiums;
3421
3422 return 0;
3423}
3424
3425
1bb7b8e8
JM
3426int wpa_config_set_cred(struct wpa_cred *cred, const char *var,
3427 const char *value, int line)
3428{
3429 char *val;
3430 size_t len;
909a948b 3431 int res;
1bb7b8e8 3432
03ed3324
JM
3433 if (os_strcmp(var, "temporary") == 0) {
3434 cred->temporary = atoi(value);
3435 return 0;
3436 }
3437
1a712d2f
JM
3438 if (os_strcmp(var, "priority") == 0) {
3439 cred->priority = atoi(value);
3440 return 0;
3441 }
3442
74794891
JM
3443 if (os_strcmp(var, "sp_priority") == 0) {
3444 int prio = atoi(value);
3445 if (prio < 0 || prio > 255)
3446 return -1;
3447 cred->sp_priority = prio;
3448 return 0;
3449 }
3450
d7b01abd
JM
3451 if (os_strcmp(var, "pcsc") == 0) {
3452 cred->pcsc = atoi(value);
3453 return 0;
3454 }
3455
8ca93c59
JM
3456 if (os_strcmp(var, "eap") == 0) {
3457 struct eap_method_type method;
3458 method.method = eap_peer_get_type(value, &method.vendor);
3459 if (method.vendor == EAP_VENDOR_IETF &&
3460 method.method == EAP_TYPE_NONE) {
3461 wpa_printf(MSG_ERROR, "Line %d: unknown EAP type '%s' "
3462 "for a credential", line, value);
3463 return -1;
3464 }
3465 os_free(cred->eap_method);
3466 cred->eap_method = os_malloc(sizeof(*cred->eap_method));
3467 if (cred->eap_method == NULL)
3468 return -1;
3469 os_memcpy(cred->eap_method, &method, sizeof(method));
3470 return 0;
3471 }
3472
02af9c90
JM
3473 if (os_strcmp(var, "password") == 0 &&
3474 os_strncmp(value, "ext:", 4) == 0) {
b166cd84
JM
3475 if (has_newline(value))
3476 return -1;
19c48da0 3477 str_clear_free(cred->password);
02af9c90
JM
3478 cred->password = os_strdup(value);
3479 cred->ext_password = 1;
3480 return 0;
3481 }
3482
f9cd147d
JM
3483 if (os_strcmp(var, "update_identifier") == 0) {
3484 cred->update_identifier = atoi(value);
3485 return 0;
3486 }
3487
4cad9df1
JM
3488 if (os_strcmp(var, "min_dl_bandwidth_home") == 0) {
3489 cred->min_dl_bandwidth_home = atoi(value);
3490 return 0;
3491 }
3492
3493 if (os_strcmp(var, "min_ul_bandwidth_home") == 0) {
3494 cred->min_ul_bandwidth_home = atoi(value);
3495 return 0;
3496 }
3497
3498 if (os_strcmp(var, "min_dl_bandwidth_roaming") == 0) {
3499 cred->min_dl_bandwidth_roaming = atoi(value);
3500 return 0;
3501 }
3502
3503 if (os_strcmp(var, "min_ul_bandwidth_roaming") == 0) {
3504 cred->min_ul_bandwidth_roaming = atoi(value);
3505 return 0;
3506 }
3507
a45b2dc5
JM
3508 if (os_strcmp(var, "max_bss_load") == 0) {
3509 cred->max_bss_load = atoi(value);
3510 return 0;
3511 }
3512
33fb8c52
JM
3513 if (os_strcmp(var, "req_conn_capab") == 0)
3514 return wpa_config_set_cred_req_conn_capab(cred, value);
3515
cf6d08a6
JM
3516 if (os_strcmp(var, "ocsp") == 0) {
3517 cred->ocsp = atoi(value);
3518 return 0;
3519 }
3520
13f6a07e
NJ
3521 if (os_strcmp(var, "sim_num") == 0) {
3522 cred->sim_num = atoi(value);
3523 return 0;
3524 }
3525
1bb7b8e8 3526 val = wpa_config_parse_string(value, &len);
b166cd84
JM
3527 if (val == NULL ||
3528 (os_strcmp(var, "excluded_ssid") != 0 &&
3529 os_strcmp(var, "roaming_consortium") != 0 &&
3530 os_strcmp(var, "required_roaming_consortium") != 0 &&
3531 has_newline(val))) {
7d86e537
JM
3532 wpa_printf(MSG_ERROR, "Line %d: invalid field '%s' string "
3533 "value '%s'.", line, var, value);
b166cd84 3534 os_free(val);
1bb7b8e8 3535 return -1;
7d86e537 3536 }
1bb7b8e8
JM
3537
3538 if (os_strcmp(var, "realm") == 0) {
3539 os_free(cred->realm);
3540 cred->realm = val;
3541 return 0;
3542 }
3543
3544 if (os_strcmp(var, "username") == 0) {
19c48da0 3545 str_clear_free(cred->username);
1bb7b8e8
JM
3546 cred->username = val;
3547 return 0;
3548 }
3549
3550 if (os_strcmp(var, "password") == 0) {
19c48da0 3551 str_clear_free(cred->password);
1bb7b8e8 3552 cred->password = val;
02af9c90 3553 cred->ext_password = 0;
1bb7b8e8
JM
3554 return 0;
3555 }
3556
3557 if (os_strcmp(var, "ca_cert") == 0) {
3558 os_free(cred->ca_cert);
3559 cred->ca_cert = val;
3560 return 0;
3561 }
3562
11e4f46a
JM
3563 if (os_strcmp(var, "client_cert") == 0) {
3564 os_free(cred->client_cert);
3565 cred->client_cert = val;
3566 return 0;
3567 }
3568
3569 if (os_strcmp(var, "private_key") == 0) {
3570 os_free(cred->private_key);
3571 cred->private_key = val;
3572 return 0;
3573 }
3574
3575 if (os_strcmp(var, "private_key_passwd") == 0) {
19c48da0 3576 str_clear_free(cred->private_key_passwd);
11e4f46a
JM
3577 cred->private_key_passwd = val;
3578 return 0;
3579 }
3580
1bb7b8e8
JM
3581 if (os_strcmp(var, "imsi") == 0) {
3582 os_free(cred->imsi);
3583 cred->imsi = val;
3584 return 0;
3585 }
3586
3587 if (os_strcmp(var, "milenage") == 0) {
19c48da0 3588 str_clear_free(cred->milenage);
1bb7b8e8
JM
3589 cred->milenage = val;
3590 return 0;
3591 }
3592
ac1bc549
JM
3593 if (os_strcmp(var, "domain_suffix_match") == 0) {
3594 os_free(cred->domain_suffix_match);
3595 cred->domain_suffix_match = val;
3596 return 0;
3597 }
3598
1bb7b8e8 3599 if (os_strcmp(var, "domain") == 0) {
463c8ffb
JM
3600 char **new_domain;
3601 new_domain = os_realloc_array(cred->domain,
3602 cred->num_domain + 1,
3603 sizeof(char *));
3604 if (new_domain == NULL) {
3605 os_free(val);
3606 return -1;
3607 }
3608 new_domain[cred->num_domain++] = val;
3609 cred->domain = new_domain;
1bb7b8e8
JM
3610 return 0;
3611 }
3612
8ca93c59
JM
3613 if (os_strcmp(var, "phase1") == 0) {
3614 os_free(cred->phase1);
3615 cred->phase1 = val;
3616 return 0;
3617 }
3618
3619 if (os_strcmp(var, "phase2") == 0) {
3620 os_free(cred->phase2);
3621 cred->phase2 = val;
3622 return 0;
3623 }
3624
955567bc
JM
3625 if (os_strcmp(var, "roaming_consortium") == 0) {
3626 if (len < 3 || len > sizeof(cred->roaming_consortium)) {
3627 wpa_printf(MSG_ERROR, "Line %d: invalid "
3628 "roaming_consortium length %d (3..15 "
3629 "expected)", line, (int) len);
3630 os_free(val);
3631 return -1;
3632 }
3633 os_memcpy(cred->roaming_consortium, val, len);
3634 cred->roaming_consortium_len = len;
3635 os_free(val);
3636 return 0;
3637 }
3638
f47c1452
JM
3639 if (os_strcmp(var, "required_roaming_consortium") == 0) {
3640 if (len < 3 || len > sizeof(cred->required_roaming_consortium))
3641 {
3642 wpa_printf(MSG_ERROR, "Line %d: invalid "
3643 "required_roaming_consortium length %d "
3644 "(3..15 expected)", line, (int) len);
3645 os_free(val);
3646 return -1;
3647 }
3648 os_memcpy(cred->required_roaming_consortium, val, len);
3649 cred->required_roaming_consortium_len = len;
3650 os_free(val);
3651 return 0;
3652 }
3653
909a948b
JM
3654 if (os_strcmp(var, "roaming_consortiums") == 0) {
3655 res = wpa_config_set_cred_roaming_consortiums(cred, val);
3656 if (res < 0)
3657 wpa_printf(MSG_ERROR,
3658 "Line %d: invalid roaming_consortiums",
3659 line);
3660 os_free(val);
3661 return res;
3662 }
3663
dbea8ac7
JM
3664 if (os_strcmp(var, "excluded_ssid") == 0) {
3665 struct excluded_ssid *e;
3666
eaa8eefe 3667 if (len > SSID_MAX_LEN) {
dbea8ac7
JM
3668 wpa_printf(MSG_ERROR, "Line %d: invalid "
3669 "excluded_ssid length %d", line, (int) len);
3670 os_free(val);
3671 return -1;
3672 }
3673
3674 e = os_realloc_array(cred->excluded_ssid,
3675 cred->num_excluded_ssid + 1,
3676 sizeof(struct excluded_ssid));
3677 if (e == NULL) {
3678 os_free(val);
3679 return -1;
3680 }
3681 cred->excluded_ssid = e;
3682
3683 e = &cred->excluded_ssid[cred->num_excluded_ssid++];
3684 os_memcpy(e->ssid, val, len);
3685 e->ssid_len = len;
3686
3687 os_free(val);
3688
3689 return 0;
3690 }
3691
bc00053c
JM
3692 if (os_strcmp(var, "roaming_partner") == 0) {
3693 struct roaming_partner *p;
3694 char *pos;
3695
3696 p = os_realloc_array(cred->roaming_partner,
3697 cred->num_roaming_partner + 1,
3698 sizeof(struct roaming_partner));
3699 if (p == NULL) {
3700 os_free(val);
3701 return -1;
3702 }
3703 cred->roaming_partner = p;
3704
3705 p = &cred->roaming_partner[cred->num_roaming_partner];
3706
3707 pos = os_strchr(val, ',');
3708 if (pos == NULL) {
3709 os_free(val);
3710 return -1;
3711 }
3712 *pos++ = '\0';
3713 if (pos - val - 1 >= (int) sizeof(p->fqdn)) {
3714 os_free(val);
3715 return -1;
3716 }
3717 os_memcpy(p->fqdn, val, pos - val);
3718
3719 p->exact_match = atoi(pos);
3720
3721 pos = os_strchr(pos, ',');
3722 if (pos == NULL) {
3723 os_free(val);
3724 return -1;
3725 }
3726 *pos++ = '\0';
3727
3728 p->priority = atoi(pos);
3729
3730 pos = os_strchr(pos, ',');
3731 if (pos == NULL) {
3732 os_free(val);
3733 return -1;
3734 }
3735 *pos++ = '\0';
3736
3737 if (os_strlen(pos) >= sizeof(p->country)) {
3738 os_free(val);
3739 return -1;
3740 }
3741 os_memcpy(p->country, pos, os_strlen(pos) + 1);
3742
3743 cred->num_roaming_partner++;
3744 os_free(val);
3745
3746 return 0;
3747 }
3748
aa26ba68
JM
3749 if (os_strcmp(var, "provisioning_sp") == 0) {
3750 os_free(cred->provisioning_sp);
3751 cred->provisioning_sp = val;
3752 return 0;
3753 }
3754
1bb7b8e8
JM
3755 if (line) {
3756 wpa_printf(MSG_ERROR, "Line %d: unknown cred field '%s'.",
3757 line, var);
3758 }
3759
f4b2d69b
JM
3760 os_free(val);
3761
1bb7b8e8
JM
3762 return -1;
3763}
3764
3765
8f03ac90 3766static char * alloc_int_str(int val)
c880ab87 3767{
1d399771 3768 const unsigned int bufsize = 20;
c880ab87 3769 char *buf;
1d399771 3770 int res;
c880ab87 3771
1d399771 3772 buf = os_malloc(bufsize);
c880ab87
JM
3773 if (buf == NULL)
3774 return NULL;
1d399771
JM
3775 res = os_snprintf(buf, bufsize, "%d", val);
3776 if (os_snprintf_error(bufsize, res)) {
3777 os_free(buf);
3778 buf = NULL;
3779 }
c880ab87
JM
3780 return buf;
3781}
3782
3783
8f03ac90 3784static char * alloc_strdup(const char *str)
c880ab87
JM
3785{
3786 if (str == NULL)
3787 return NULL;
3788 return os_strdup(str);
3789}
3790
3791
3792char * wpa_config_get_cred_no_key(struct wpa_cred *cred, const char *var)
3793{
3794 if (os_strcmp(var, "temporary") == 0)
3795 return alloc_int_str(cred->temporary);
3796
3797 if (os_strcmp(var, "priority") == 0)
3798 return alloc_int_str(cred->priority);
3799
3800 if (os_strcmp(var, "sp_priority") == 0)
3801 return alloc_int_str(cred->sp_priority);
3802
3803 if (os_strcmp(var, "pcsc") == 0)
3804 return alloc_int_str(cred->pcsc);
3805
3806 if (os_strcmp(var, "eap") == 0) {
3807 if (!cred->eap_method)
3808 return NULL;
3809 return alloc_strdup(eap_get_name(cred->eap_method[0].vendor,
3810 cred->eap_method[0].method));
3811 }
3812
3813 if (os_strcmp(var, "update_identifier") == 0)
3814 return alloc_int_str(cred->update_identifier);
3815
3816 if (os_strcmp(var, "min_dl_bandwidth_home") == 0)
3817 return alloc_int_str(cred->min_dl_bandwidth_home);
3818
3819 if (os_strcmp(var, "min_ul_bandwidth_home") == 0)
3820 return alloc_int_str(cred->min_ul_bandwidth_home);
3821
3822 if (os_strcmp(var, "min_dl_bandwidth_roaming") == 0)
3823 return alloc_int_str(cred->min_dl_bandwidth_roaming);
3824
3825 if (os_strcmp(var, "min_ul_bandwidth_roaming") == 0)
3826 return alloc_int_str(cred->min_ul_bandwidth_roaming);
3827
3828 if (os_strcmp(var, "max_bss_load") == 0)
3829 return alloc_int_str(cred->max_bss_load);
3830
3831 if (os_strcmp(var, "req_conn_capab") == 0) {
3832 unsigned int i;
3833 char *buf, *end, *pos;
3834 int ret;
3835
3836 if (!cred->num_req_conn_capab)
3837 return NULL;
3838
3839 buf = os_malloc(4000);
3840 if (buf == NULL)
3841 return NULL;
3842 pos = buf;
3843 end = pos + 4000;
3844 for (i = 0; i < cred->num_req_conn_capab; i++) {
3845 int *ports;
3846
3847 ret = os_snprintf(pos, end - pos, "%s%u",
3848 i > 0 ? "\n" : "",
3849 cred->req_conn_capab_proto[i]);
d85e1fc8 3850 if (os_snprintf_error(end - pos, ret))
c880ab87
JM
3851 return buf;
3852 pos += ret;
3853
3854 ports = cred->req_conn_capab_port[i];
3855 if (ports) {
3856 int j;
3857 for (j = 0; ports[j] != -1; j++) {
3858 ret = os_snprintf(pos, end - pos,
3859 "%s%d",
3860 j > 0 ? "," : ":",
3861 ports[j]);
d85e1fc8 3862 if (os_snprintf_error(end - pos, ret))
c880ab87
JM
3863 return buf;
3864 pos += ret;
3865 }
3866 }
3867 }
3868
3869 return buf;
3870 }
3871
3872 if (os_strcmp(var, "ocsp") == 0)
3873 return alloc_int_str(cred->ocsp);
3874
3875 if (os_strcmp(var, "realm") == 0)
3876 return alloc_strdup(cred->realm);
3877
3878 if (os_strcmp(var, "username") == 0)
3879 return alloc_strdup(cred->username);
3880
3881 if (os_strcmp(var, "password") == 0) {
3882 if (!cred->password)
3883 return NULL;
3884 return alloc_strdup("*");
3885 }
3886
3887 if (os_strcmp(var, "ca_cert") == 0)
3888 return alloc_strdup(cred->ca_cert);
3889
3890 if (os_strcmp(var, "client_cert") == 0)
3891 return alloc_strdup(cred->client_cert);
3892
3893 if (os_strcmp(var, "private_key") == 0)
3894 return alloc_strdup(cred->private_key);
3895
3896 if (os_strcmp(var, "private_key_passwd") == 0) {
3897 if (!cred->private_key_passwd)
3898 return NULL;
3899 return alloc_strdup("*");
3900 }
3901
3902 if (os_strcmp(var, "imsi") == 0)
3903 return alloc_strdup(cred->imsi);
3904
3905 if (os_strcmp(var, "milenage") == 0) {
3906 if (!(cred->milenage))
3907 return NULL;
3908 return alloc_strdup("*");
3909 }
3910
3911 if (os_strcmp(var, "domain_suffix_match") == 0)
3912 return alloc_strdup(cred->domain_suffix_match);
3913
3914 if (os_strcmp(var, "domain") == 0) {
3915 unsigned int i;
3916 char *buf, *end, *pos;
3917 int ret;
3918
3919 if (!cred->num_domain)
3920 return NULL;
3921
3922 buf = os_malloc(4000);
3923 if (buf == NULL)
3924 return NULL;
3925 pos = buf;
3926 end = pos + 4000;
3927
3928 for (i = 0; i < cred->num_domain; i++) {
3929 ret = os_snprintf(pos, end - pos, "%s%s",
3930 i > 0 ? "\n" : "", cred->domain[i]);
d85e1fc8 3931 if (os_snprintf_error(end - pos, ret))
c880ab87
JM
3932 return buf;
3933 pos += ret;
3934 }
3935
3936 return buf;
3937 }
3938
3939 if (os_strcmp(var, "phase1") == 0)
3940 return alloc_strdup(cred->phase1);
3941
3942 if (os_strcmp(var, "phase2") == 0)
3943 return alloc_strdup(cred->phase2);
3944
3945 if (os_strcmp(var, "roaming_consortium") == 0) {
3946 size_t buflen;
3947 char *buf;
3948
3949 if (!cred->roaming_consortium_len)
3950 return NULL;
3951 buflen = cred->roaming_consortium_len * 2 + 1;
3952 buf = os_malloc(buflen);
3953 if (buf == NULL)
3954 return NULL;
3955 wpa_snprintf_hex(buf, buflen, cred->roaming_consortium,
3956 cred->roaming_consortium_len);
3957 return buf;
3958 }
3959
3960 if (os_strcmp(var, "required_roaming_consortium") == 0) {
3961 size_t buflen;
3962 char *buf;
3963
3964 if (!cred->required_roaming_consortium_len)
3965 return NULL;
3966 buflen = cred->required_roaming_consortium_len * 2 + 1;
3967 buf = os_malloc(buflen);
3968 if (buf == NULL)
3969 return NULL;
3970 wpa_snprintf_hex(buf, buflen, cred->required_roaming_consortium,
3971 cred->required_roaming_consortium_len);
3972 return buf;
3973 }
3974
909a948b
JM
3975 if (os_strcmp(var, "roaming_consortiums") == 0) {
3976 size_t buflen;
3977 char *buf, *pos;
3978 size_t i;
3979
3980 if (!cred->num_roaming_consortiums)
3981 return NULL;
3982 buflen = cred->num_roaming_consortiums *
3983 MAX_ROAMING_CONS_OI_LEN * 2 + 1;
3984 buf = os_malloc(buflen);
3985 if (!buf)
3986 return NULL;
3987 pos = buf;
3988 for (i = 0; i < cred->num_roaming_consortiums; i++) {
3989 if (i > 0)
3990 *pos++ = ',';
3991 pos += wpa_snprintf_hex(
3992 pos, buf + buflen - pos,
3993 cred->roaming_consortiums[i],
3994 cred->roaming_consortiums_len[i]);
3995 }
3996 *pos = '\0';
3997 return buf;
3998 }
3999
c880ab87
JM
4000 if (os_strcmp(var, "excluded_ssid") == 0) {
4001 unsigned int i;
4002 char *buf, *end, *pos;
4003
4004 if (!cred->num_excluded_ssid)
4005 return NULL;
4006
4007 buf = os_malloc(4000);
4008 if (buf == NULL)
4009 return NULL;
4010 pos = buf;
4011 end = pos + 4000;
4012
4013 for (i = 0; i < cred->num_excluded_ssid; i++) {
4014 struct excluded_ssid *e;
4015 int ret;
4016
4017 e = &cred->excluded_ssid[i];
4018 ret = os_snprintf(pos, end - pos, "%s%s",
4019 i > 0 ? "\n" : "",
4020 wpa_ssid_txt(e->ssid, e->ssid_len));
d85e1fc8 4021 if (os_snprintf_error(end - pos, ret))
c880ab87
JM
4022 return buf;
4023 pos += ret;
4024 }
4025
4026 return buf;
4027 }
4028
4029 if (os_strcmp(var, "roaming_partner") == 0) {
4030 unsigned int i;
4031 char *buf, *end, *pos;
4032
4033 if (!cred->num_roaming_partner)
4034 return NULL;
4035
4036 buf = os_malloc(4000);
4037 if (buf == NULL)
4038 return NULL;
4039 pos = buf;
4040 end = pos + 4000;
4041
4042 for (i = 0; i < cred->num_roaming_partner; i++) {
4043 struct roaming_partner *p;
4044 int ret;
4045
4046 p = &cred->roaming_partner[i];
4047 ret = os_snprintf(pos, end - pos, "%s%s,%d,%u,%s",
4048 i > 0 ? "\n" : "",
4049 p->fqdn, p->exact_match, p->priority,
4050 p->country);
d85e1fc8 4051 if (os_snprintf_error(end - pos, ret))
c880ab87
JM
4052 return buf;
4053 pos += ret;
4054 }
4055
4056 return buf;
4057 }
4058
4059 if (os_strcmp(var, "provisioning_sp") == 0)
4060 return alloc_strdup(cred->provisioning_sp);
4061
4062 return NULL;
4063}
4064
4065
d94c9ee6
JM
4066struct wpa_cred * wpa_config_get_cred(struct wpa_config *config, int id)
4067{
4068 struct wpa_cred *cred;
4069
4070 cred = config->cred;
4071 while (cred) {
4072 if (id == cred->id)
4073 break;
4074 cred = cred->next;
4075 }
4076
4077 return cred;
4078}
4079
4080
4081struct wpa_cred * wpa_config_add_cred(struct wpa_config *config)
4082{
4083 int id;
4084 struct wpa_cred *cred, *last = NULL;
4085
4086 id = -1;
4087 cred = config->cred;
4088 while (cred) {
4089 if (cred->id > id)
4090 id = cred->id;
4091 last = cred;
4092 cred = cred->next;
4093 }
4094 id++;
4095
4096 cred = os_zalloc(sizeof(*cred));
4097 if (cred == NULL)
4098 return NULL;
4099 cred->id = id;
13f6a07e 4100 cred->sim_num = DEFAULT_USER_SELECTED_SIM;
d94c9ee6
JM
4101 if (last)
4102 last->next = cred;
4103 else
4104 config->cred = cred;
4105
4106 return cred;
4107}
4108
4109
4110int wpa_config_remove_cred(struct wpa_config *config, int id)
4111{
4112 struct wpa_cred *cred, *prev = NULL;
4113
4114 cred = config->cred;
4115 while (cred) {
4116 if (id == cred->id)
4117 break;
4118 prev = cred;
4119 cred = cred->next;
4120 }
4121
4122 if (cred == NULL)
4123 return -1;
4124
4125 if (prev)
4126 prev->next = cred->next;
4127 else
4128 config->cred = cred->next;
4129
4130 wpa_config_free_cred(cred);
4131 return 0;
4132}
4133
4134
6fc6879b
JM
4135#ifndef CONFIG_NO_CONFIG_BLOBS
4136/**
4137 * wpa_config_get_blob - Get a named configuration blob
4138 * @config: Configuration data from wpa_config_read()
4139 * @name: Name of the blob
4140 * Returns: Pointer to blob data or %NULL if not found
4141 */
4142const struct wpa_config_blob * wpa_config_get_blob(struct wpa_config *config,
4143 const char *name)
4144{
4145 struct wpa_config_blob *blob = config->blobs;
4146
4147 while (blob) {
4148 if (os_strcmp(blob->name, name) == 0)
4149 return blob;
4150 blob = blob->next;
4151 }
4152 return NULL;
4153}
4154
4155
4156/**
4157 * wpa_config_set_blob - Set or add a named configuration blob
4158 * @config: Configuration data from wpa_config_read()
4159 * @blob: New value for the blob
4160 *
4161 * Adds a new configuration blob or replaces the current value of an existing
4162 * blob.
4163 */
4164void wpa_config_set_blob(struct wpa_config *config,
4165 struct wpa_config_blob *blob)
4166{
4167 wpa_config_remove_blob(config, blob->name);
4168 blob->next = config->blobs;
4169 config->blobs = blob;
4170}
4171
4172
4173/**
4174 * wpa_config_free_blob - Free blob data
4175 * @blob: Pointer to blob to be freed
4176 */
4177void wpa_config_free_blob(struct wpa_config_blob *blob)
4178{
4179 if (blob) {
4180 os_free(blob->name);
19c48da0 4181 bin_clear_free(blob->data, blob->len);
6fc6879b
JM
4182 os_free(blob);
4183 }
4184}
4185
4186
4187/**
4188 * wpa_config_remove_blob - Remove a named configuration blob
4189 * @config: Configuration data from wpa_config_read()
4190 * @name: Name of the blob to remove
4191 * Returns: 0 if blob was removed or -1 if blob was not found
4192 */
4193int wpa_config_remove_blob(struct wpa_config *config, const char *name)
4194{
4195 struct wpa_config_blob *pos = config->blobs, *prev = NULL;
4196
4197 while (pos) {
4198 if (os_strcmp(pos->name, name) == 0) {
4199 if (prev)
4200 prev->next = pos->next;
4201 else
4202 config->blobs = pos->next;
4203 wpa_config_free_blob(pos);
4204 return 0;
4205 }
4206 prev = pos;
4207 pos = pos->next;
4208 }
4209
4210 return -1;
4211}
4212#endif /* CONFIG_NO_CONFIG_BLOBS */
4213
4214
4215/**
4216 * wpa_config_alloc_empty - Allocate an empty configuration
4217 * @ctrl_interface: Control interface parameters, e.g., path to UNIX domain
4218 * socket
4219 * @driver_param: Driver parameters
4220 * Returns: Pointer to allocated configuration data or %NULL on failure
4221 */
4222struct wpa_config * wpa_config_alloc_empty(const char *ctrl_interface,
4223 const char *driver_param)
4224{
4225 struct wpa_config *config;
c26effe1
YD
4226 const int aCWmin = 4, aCWmax = 10;
4227 const struct hostapd_wmm_ac_params ac_bk =
4228 { aCWmin, aCWmax, 7, 0, 0 }; /* background traffic */
4229 const struct hostapd_wmm_ac_params ac_be =
4230 { aCWmin, aCWmax, 3, 0, 0 }; /* best effort traffic */
4231 const struct hostapd_wmm_ac_params ac_vi = /* video traffic */
623ecdd5 4232 { aCWmin - 1, aCWmin, 2, 3000 / 32, 0 };
c26effe1 4233 const struct hostapd_wmm_ac_params ac_vo = /* voice traffic */
623ecdd5 4234 { aCWmin - 2, aCWmin - 1, 2, 1500 / 32, 0 };
6fc6879b
JM
4235
4236 config = os_zalloc(sizeof(*config));
4237 if (config == NULL)
4238 return NULL;
4239 config->eapol_version = DEFAULT_EAPOL_VERSION;
4240 config->ap_scan = DEFAULT_AP_SCAN;
e45e8989 4241 config->user_mpm = DEFAULT_USER_MPM;
4b409368 4242 config->max_peer_links = DEFAULT_MAX_PEER_LINKS;
5a2a6de6 4243 config->mesh_max_inactivity = DEFAULT_MESH_MAX_INACTIVITY;
ecd40fef
MH
4244 config->dot11RSNASAERetransPeriod =
4245 DEFAULT_DOT11_RSNA_SAE_RETRANS_PERIOD;
6fc6879b 4246 config->fast_reauth = DEFAULT_FAST_REAUTH;
e3768e7c 4247 config->p2p_go_intent = DEFAULT_P2P_GO_INTENT;
0f66abd2 4248 config->p2p_intra_bss = DEFAULT_P2P_INTRA_BSS;
1a471ff0 4249 config->p2p_go_freq_change_policy = DEFAULT_P2P_GO_FREQ_MOVE;
462a7439 4250 config->p2p_go_max_inactivity = DEFAULT_P2P_GO_MAX_INACTIVITY;
e3bd6e9d 4251 config->p2p_optimize_listen_chan = DEFAULT_P2P_OPTIMIZE_LISTEN_CHAN;
0b8bcaa5 4252 config->p2p_go_ctwindow = DEFAULT_P2P_GO_CTWINDOW;
c9c38b09 4253 config->bss_max_count = DEFAULT_BSS_MAX_COUNT;
78633c37
SL
4254 config->bss_expiration_age = DEFAULT_BSS_EXPIRATION_AGE;
4255 config->bss_expiration_scan_count = DEFAULT_BSS_EXPIRATION_SCAN_COUNT;
dae608d5 4256 config->max_num_sta = DEFAULT_MAX_NUM_STA;
19e20c14 4257 config->ap_isolate = DEFAULT_AP_ISOLATE;
11540c0b 4258 config->access_network_type = DEFAULT_ACCESS_NETWORK_TYPE;
6124e858 4259 config->scan_cur_freq = DEFAULT_SCAN_CUR_FREQ;
c26effe1
YD
4260 config->wmm_ac_params[0] = ac_be;
4261 config->wmm_ac_params[1] = ac_bk;
4262 config->wmm_ac_params[2] = ac_vi;
4263 config->wmm_ac_params[3] = ac_vo;
d3b20469 4264 config->p2p_search_delay = DEFAULT_P2P_SEARCH_DELAY;
c267753b 4265 config->rand_addr_lifetime = DEFAULT_RAND_ADDR_LIFETIME;
b41f2684 4266 config->key_mgmt_offload = DEFAULT_KEY_MGMT_OFFLOAD;
483dd6a5 4267 config->cert_in_cb = DEFAULT_CERT_IN_CB;
73ed03f3 4268 config->wpa_rsc_relaxation = DEFAULT_WPA_RSC_RELAXATION;
6fc6879b 4269
c5d193d7
DS
4270#ifdef CONFIG_MBO
4271 config->mbo_cell_capa = DEFAULT_MBO_CELL_CAPA;
af8bc24d
KV
4272 config->disassoc_imminent_rssi_threshold =
4273 DEFAULT_DISASSOC_IMMINENT_RSSI_THRESHOLD;
332aadb8 4274 config->oce = DEFAULT_OCE_SUPPORT;
c5d193d7
DS
4275#endif /* CONFIG_MBO */
4276
6fc6879b
JM
4277 if (ctrl_interface)
4278 config->ctrl_interface = os_strdup(ctrl_interface);
4279 if (driver_param)
4280 config->driver_param = os_strdup(driver_param);
1d9d21f3 4281 config->gas_rand_addr_lifetime = DEFAULT_RAND_ADDR_LIFETIME;
6fc6879b
JM
4282
4283 return config;
4284}
4285
4286
4287#ifndef CONFIG_NO_STDOUT_DEBUG
4288/**
4289 * wpa_config_debug_dump_networks - Debug dump of configured networks
4290 * @config: Configuration data from wpa_config_read()
4291 */
4292void wpa_config_debug_dump_networks(struct wpa_config *config)
4293{
4294 int prio;
4295 struct wpa_ssid *ssid;
4296
4297 for (prio = 0; prio < config->num_prio; prio++) {
4298 ssid = config->pssid[prio];
4299 wpa_printf(MSG_DEBUG, "Priority group %d",
4300 ssid->priority);
4301 while (ssid) {
4302 wpa_printf(MSG_DEBUG, " id=%d ssid='%s'",
4303 ssid->id,
4304 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
4305 ssid = ssid->pnext;
4306 }
4307 }
4308}
4309#endif /* CONFIG_NO_STDOUT_DEBUG */
121adf9c
JM
4310
4311
4312struct global_parse_data {
4313 char *name;
4314 int (*parser)(const struct global_parse_data *data,
4315 struct wpa_config *config, int line, const char *value);
10263dc2
OO
4316 int (*get)(const char *name, struct wpa_config *config, long offset,
4317 char *buf, size_t buflen, int pretty_print);
121adf9c 4318 void *param1, *param2, *param3;
1d47214a 4319 unsigned int changed_flag;
121adf9c
JM
4320};
4321
4322
4323static int wpa_global_config_parse_int(const struct global_parse_data *data,
4324 struct wpa_config *config, int line,
4325 const char *pos)
4326{
eae3a584
JB
4327 int val, *dst;
4328 char *end;
4329
121adf9c 4330 dst = (int *) (((u8 *) config) + (long) data->param1);
eae3a584
JB
4331 val = strtol(pos, &end, 0);
4332 if (*end) {
4333 wpa_printf(MSG_ERROR, "Line %d: invalid number \"%s\"",
4334 line, pos);
4335 return -1;
4336 }
4337 *dst = val;
4338
121adf9c
JM
4339 wpa_printf(MSG_DEBUG, "%s=%d", data->name, *dst);
4340
4341 if (data->param2 && *dst < (long) data->param2) {
4342 wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
4343 "min_value=%ld)", line, data->name, *dst,
4344 (long) data->param2);
4345 *dst = (long) data->param2;
4346 return -1;
4347 }
4348
4349 if (data->param3 && *dst > (long) data->param3) {
4350 wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
4351 "max_value=%ld)", line, data->name, *dst,
4352 (long) data->param3);
4353 *dst = (long) data->param3;
4354 return -1;
4355 }
4356
4357 return 0;
4358}
4359
4360
4361static int wpa_global_config_parse_str(const struct global_parse_data *data,
4362 struct wpa_config *config, int line,
4363 const char *pos)
4364{
4365 size_t len;
4366 char **dst, *tmp;
4367
4368 len = os_strlen(pos);
4369 if (data->param2 && len < (size_t) data->param2) {
4370 wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
4371 "min_len=%ld)", line, data->name,
4372 (unsigned long) len, (long) data->param2);
4373 return -1;
4374 }
4375
4376 if (data->param3 && len > (size_t) data->param3) {
4377 wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
4378 "max_len=%ld)", line, data->name,
4379 (unsigned long) len, (long) data->param3);
4380 return -1;
4381 }
4382
2a3f5650
JM
4383 if (has_newline(pos)) {
4384 wpa_printf(MSG_ERROR, "Line %d: invalid %s value with newline",
4385 line, data->name);
4386 return -1;
4387 }
4388
121adf9c
JM
4389 tmp = os_strdup(pos);
4390 if (tmp == NULL)
4391 return -1;
4392
4393 dst = (char **) (((u8 *) config) + (long) data->param1);
4394 os_free(*dst);
4395 *dst = tmp;
4396 wpa_printf(MSG_DEBUG, "%s='%s'", data->name, *dst);
4397
4398 return 0;
4399}
4400
4401
31392709
HD
4402static int wpa_config_process_bgscan(const struct global_parse_data *data,
4403 struct wpa_config *config, int line,
4404 const char *pos)
4405{
4406 size_t len;
4407 char *tmp;
04c366cb 4408 int res;
31392709
HD
4409
4410 tmp = wpa_config_parse_string(pos, &len);
4411 if (tmp == NULL) {
4412 wpa_printf(MSG_ERROR, "Line %d: failed to parse %s",
4413 line, data->name);
4414 return -1;
4415 }
4416
04c366cb
EL
4417 res = wpa_global_config_parse_str(data, config, line, tmp);
4418 os_free(tmp);
4419 return res;
31392709
HD
4420}
4421
4422
3f2c8ba6
JM
4423static int wpa_global_config_parse_bin(const struct global_parse_data *data,
4424 struct wpa_config *config, int line,
4425 const char *pos)
4426{
3f2c8ba6
JM
4427 struct wpabuf **dst, *tmp;
4428
9d955f75
DS
4429 tmp = wpabuf_parse_bin(pos);
4430 if (!tmp)
3f2c8ba6 4431 return -1;
3f2c8ba6
JM
4432
4433 dst = (struct wpabuf **) (((u8 *) config) + (long) data->param1);
4434 wpabuf_free(*dst);
4435 *dst = tmp;
4436 wpa_printf(MSG_DEBUG, "%s", data->name);
4437
4438 return 0;
4439}
4440
4441
f5ffc348
BG
4442static int wpa_config_process_freq_list(const struct global_parse_data *data,
4443 struct wpa_config *config, int line,
4444 const char *value)
4445{
4446 int *freqs;
4447
4448 freqs = wpa_config_parse_int_array(value);
4449 if (freqs == NULL)
4450 return -1;
6668efda
JM
4451 if (freqs[0] == 0) {
4452 os_free(freqs);
4453 freqs = NULL;
4454 }
f5ffc348
BG
4455 os_free(config->freq_list);
4456 config->freq_list = freqs;
4457 return 0;
4458}
4459
4460
25ef8529
JM
4461#ifdef CONFIG_P2P
4462static int wpa_global_config_parse_ipv4(const struct global_parse_data *data,
4463 struct wpa_config *config, int line,
4464 const char *pos)
4465{
4466 u32 *dst;
4467 struct hostapd_ip_addr addr;
4468
4469 if (hostapd_parse_ip_addr(pos, &addr) < 0)
4470 return -1;
4471 if (addr.af != AF_INET)
4472 return -1;
4473
4474 dst = (u32 *) (((u8 *) config) + (long) data->param1);
4475 os_memcpy(dst, &addr.u.v4.s_addr, 4);
4476 wpa_printf(MSG_DEBUG, "%s = 0x%x", data->name,
4477 WPA_GET_BE32((u8 *) dst));
4478
4479 return 0;
4480}
4481#endif /* CONFIG_P2P */
4482
4483
121adf9c
JM
4484static int wpa_config_process_country(const struct global_parse_data *data,
4485 struct wpa_config *config, int line,
4486 const char *pos)
4487{
4488 if (!pos[0] || !pos[1]) {
4489 wpa_printf(MSG_DEBUG, "Invalid country set");
4490 return -1;
4491 }
4492 config->country[0] = pos[0];
4493 config->country[1] = pos[1];
4494 wpa_printf(MSG_DEBUG, "country='%c%c'",
4495 config->country[0], config->country[1]);
4496 return 0;
4497}
4498
4499
4500static int wpa_config_process_load_dynamic_eap(
4501 const struct global_parse_data *data, struct wpa_config *config,
4502 int line, const char *so)
4503{
4504 int ret;
4505 wpa_printf(MSG_DEBUG, "load_dynamic_eap=%s", so);
4506 ret = eap_peer_method_load(so);
4507 if (ret == -2) {
4508 wpa_printf(MSG_DEBUG, "This EAP type was already loaded - not "
4509 "reloading.");
4510 } else if (ret) {
4511 wpa_printf(MSG_ERROR, "Line %d: Failed to load dynamic EAP "
4512 "method '%s'.", line, so);
4513 return -1;
4514 }
4515
4516 return 0;
4517}
4518
4519
4520#ifdef CONFIG_WPS
4521
4522static int wpa_config_process_uuid(const struct global_parse_data *data,
4523 struct wpa_config *config, int line,
4524 const char *pos)
4525{
4526 char buf[40];
4527 if (uuid_str2bin(pos, config->uuid)) {
4528 wpa_printf(MSG_ERROR, "Line %d: invalid UUID", line);
4529 return -1;
4530 }
4531 uuid_bin2str(config->uuid, buf, sizeof(buf));
4532 wpa_printf(MSG_DEBUG, "uuid=%s", buf);
4533 return 0;
4534}
4535
4536
2f646b6e
JB
4537static int wpa_config_process_device_type(
4538 const struct global_parse_data *data,
4539 struct wpa_config *config, int line, const char *pos)
4540{
4541 return wps_dev_type_str2bin(pos, config->device_type);
4542}
4543
4544
121adf9c
JM
4545static int wpa_config_process_os_version(const struct global_parse_data *data,
4546 struct wpa_config *config, int line,
4547 const char *pos)
4548{
4549 if (hexstr2bin(pos, config->os_version, 4)) {
4550 wpa_printf(MSG_ERROR, "Line %d: invalid os_version", line);
4551 return -1;
4552 }
4553 wpa_printf(MSG_DEBUG, "os_version=%08x",
4554 WPA_GET_BE32(config->os_version));
4555 return 0;
4556}
4557
71dd3b78
AS
4558
4559static int wpa_config_process_wps_vendor_ext_m1(
4560 const struct global_parse_data *data,
4561 struct wpa_config *config, int line, const char *pos)
4562{
4563 struct wpabuf *tmp;
4564 int len = os_strlen(pos) / 2;
4565 u8 *p;
4566
4567 if (!len) {
4568 wpa_printf(MSG_ERROR, "Line %d: "
4569 "invalid wps_vendor_ext_m1", line);
4570 return -1;
4571 }
4572
4573 tmp = wpabuf_alloc(len);
4574 if (tmp) {
4575 p = wpabuf_put(tmp, len);
4576
4577 if (hexstr2bin(pos, p, len)) {
4578 wpa_printf(MSG_ERROR, "Line %d: "
4579 "invalid wps_vendor_ext_m1", line);
4580 wpabuf_free(tmp);
4581 return -1;
4582 }
4583
4584 wpabuf_free(config->wps_vendor_ext_m1);
4585 config->wps_vendor_ext_m1 = tmp;
4586 } else {
4587 wpa_printf(MSG_ERROR, "Can not allocate "
4588 "memory for wps_vendor_ext_m1");
4589 return -1;
4590 }
4591
4592 return 0;
4593}
4594
121adf9c
JM
4595#endif /* CONFIG_WPS */
4596
e3768e7c
JM
4597#ifdef CONFIG_P2P
4598static int wpa_config_process_sec_device_type(
4599 const struct global_parse_data *data,
4600 struct wpa_config *config, int line, const char *pos)
4601{
4602 int idx;
4603
2f646b6e 4604 if (config->num_sec_device_types >= MAX_SEC_DEVICE_TYPES) {
e3768e7c
JM
4605 wpa_printf(MSG_ERROR, "Line %d: too many sec_device_type "
4606 "items", line);
4607 return -1;
4608 }
4609
2f646b6e
JB
4610 idx = config->num_sec_device_types;
4611
4612 if (wps_dev_type_str2bin(pos, config->sec_device_type[idx]))
e3768e7c 4613 return -1;
2f646b6e
JB
4614
4615 config->num_sec_device_types++;
e3768e7c
JM
4616 return 0;
4617}
21d996f7
JM
4618
4619
4620static int wpa_config_process_p2p_pref_chan(
4621 const struct global_parse_data *data,
4622 struct wpa_config *config, int line, const char *pos)
4623{
4624 struct p2p_channel *pref = NULL, *n;
4625 unsigned int num = 0;
4626 const char *pos2;
4627 u8 op_class, chan;
4628
4629 /* format: class:chan,class:chan,... */
4630
4631 while (*pos) {
4632 op_class = atoi(pos);
4633 pos2 = os_strchr(pos, ':');
4634 if (pos2 == NULL)
4635 goto fail;
4636 pos2++;
4637 chan = atoi(pos2);
4638
067ffa26
JM
4639 n = os_realloc_array(pref, num + 1,
4640 sizeof(struct p2p_channel));
21d996f7
JM
4641 if (n == NULL)
4642 goto fail;
4643 pref = n;
4644 pref[num].op_class = op_class;
4645 pref[num].chan = chan;
4646 num++;
4647
4648 pos = os_strchr(pos2, ',');
4649 if (pos == NULL)
4650 break;
4651 pos++;
4652 }
4653
4654 os_free(config->p2p_pref_chan);
4655 config->p2p_pref_chan = pref;
4656 config->num_p2p_pref_chan = num;
4657 wpa_hexdump(MSG_DEBUG, "P2P: Preferred class/channel pairs",
4658 (u8 *) config->p2p_pref_chan,
4659 config->num_p2p_pref_chan * sizeof(struct p2p_channel));
4660
4661 return 0;
4662
4663fail:
4664 os_free(pref);
4665 wpa_printf(MSG_ERROR, "Line %d: Invalid p2p_pref_chan list", line);
4666 return -1;
4667}
556b30da
JM
4668
4669
4670static int wpa_config_process_p2p_no_go_freq(
4671 const struct global_parse_data *data,
4672 struct wpa_config *config, int line, const char *pos)
4673{
4674 int ret;
4675
4676 ret = freq_range_list_parse(&config->p2p_no_go_freq, pos);
4677 if (ret < 0) {
4678 wpa_printf(MSG_ERROR, "Line %d: Invalid p2p_no_go_freq", line);
4679 return -1;
4680 }
4681
4682 wpa_printf(MSG_DEBUG, "P2P: p2p_no_go_freq with %u items",
4683 config->p2p_no_go_freq.num);
4684
4685 return 0;
4686}
4687
9359cc84
JC
4688
4689static int wpa_config_process_p2p_device_persistent_mac_addr(
4690 const struct global_parse_data *data,
4691 struct wpa_config *config, int line, const char *pos)
4692{
4693 if (hwaddr_aton2(pos, config->p2p_device_persistent_mac_addr) < 0) {
4694 wpa_printf(MSG_ERROR,
4695 "Line %d: Invalid p2p_device_persistent_mac_addr '%s'",
4696 line, pos);
4697 return -1;
4698 }
4699
4700 return 0;
4701}
4702
e3768e7c
JM
4703#endif /* CONFIG_P2P */
4704
121adf9c 4705
46ee0427
JM
4706static int wpa_config_process_hessid(
4707 const struct global_parse_data *data,
4708 struct wpa_config *config, int line, const char *pos)
4709{
4710 if (hwaddr_aton2(pos, config->hessid) < 0) {
4711 wpa_printf(MSG_ERROR, "Line %d: Invalid hessid '%s'",
4712 line, pos);
4713 return -1;
4714 }
4715
4716 return 0;
4717}
4718
4719
625f202a
JM
4720static int wpa_config_process_sae_groups(
4721 const struct global_parse_data *data,
4722 struct wpa_config *config, int line, const char *pos)
4723{
4724 int *groups = wpa_config_parse_int_array(pos);
4725 if (groups == NULL) {
4726 wpa_printf(MSG_ERROR, "Line %d: Invalid sae_groups '%s'",
4727 line, pos);
4728 return -1;
4729 }
4730
4731 os_free(config->sae_groups);
4732 config->sae_groups = groups;
4733
4734 return 0;
4735}
4736
4737
18a2eaab
JM
4738static int wpa_config_process_ap_vendor_elements(
4739 const struct global_parse_data *data,
4740 struct wpa_config *config, int line, const char *pos)
4741{
4742 struct wpabuf *tmp;
4743 int len = os_strlen(pos) / 2;
4744 u8 *p;
4745
4746 if (!len) {
4747 wpa_printf(MSG_ERROR, "Line %d: invalid ap_vendor_elements",
4748 line);
4749 return -1;
4750 }
4751
4752 tmp = wpabuf_alloc(len);
4753 if (tmp) {
4754 p = wpabuf_put(tmp, len);
4755
4756 if (hexstr2bin(pos, p, len)) {
4757 wpa_printf(MSG_ERROR, "Line %d: invalid "
4758 "ap_vendor_elements", line);
4759 wpabuf_free(tmp);
4760 return -1;
4761 }
4762
4763 wpabuf_free(config->ap_vendor_elements);
4764 config->ap_vendor_elements = tmp;
4765 } else {
4766 wpa_printf(MSG_ERROR, "Cannot allocate memory for "
4767 "ap_vendor_elements");
4768 return -1;
4769 }
4770
4771 return 0;
4772}
4773
4774
298f5185 4775#ifdef CONFIG_CTRL_IFACE
ea61aa1d
JM
4776static int wpa_config_process_no_ctrl_interface(
4777 const struct global_parse_data *data,
4778 struct wpa_config *config, int line, const char *pos)
4779{
4780 wpa_printf(MSG_DEBUG, "no_ctrl_interface -> ctrl_interface=NULL");
4781 os_free(config->ctrl_interface);
4782 config->ctrl_interface = NULL;
4783 return 0;
4784}
298f5185 4785#endif /* CONFIG_CTRL_IFACE */
ea61aa1d
JM
4786
4787
10263dc2
OO
4788static int wpa_config_get_int(const char *name, struct wpa_config *config,
4789 long offset, char *buf, size_t buflen,
4790 int pretty_print)
4791{
4792 int *val = (int *) (((u8 *) config) + (long) offset);
4793
4794 if (pretty_print)
4795 return os_snprintf(buf, buflen, "%s=%d\n", name, *val);
4796 return os_snprintf(buf, buflen, "%d", *val);
4797}
4798
4799
4800static int wpa_config_get_str(const char *name, struct wpa_config *config,
4801 long offset, char *buf, size_t buflen,
4802 int pretty_print)
4803{
4804 char **val = (char **) (((u8 *) config) + (long) offset);
4805 int res;
4806
4807 if (pretty_print)
4808 res = os_snprintf(buf, buflen, "%s=%s\n", name,
4809 *val ? *val : "null");
4810 else if (!*val)
4811 return -1;
4812 else
4813 res = os_snprintf(buf, buflen, "%s", *val);
4814 if (os_snprintf_error(buflen, res))
4815 res = -1;
4816
4817 return res;
4818}
4819
4820
5c6c315f
MK
4821#ifdef CONFIG_P2P
4822static int wpa_config_get_ipv4(const char *name, struct wpa_config *config,
4823 long offset, char *buf, size_t buflen,
4824 int pretty_print)
4825{
4826 void *val = ((u8 *) config) + (long) offset;
4827 int res;
4828 char addr[INET_ADDRSTRLEN];
4829
4830 if (!val || !inet_ntop(AF_INET, val, addr, sizeof(addr)))
4831 return -1;
4832
4833 if (pretty_print)
4834 res = os_snprintf(buf, buflen, "%s=%s\n", name, addr);
4835 else
4836 res = os_snprintf(buf, buflen, "%s", addr);
4837
4838 if (os_snprintf_error(buflen, res))
4839 res = -1;
4840
4841 return res;
4842}
4843#endif /* CONFIG_P2P */
4844
4845
121adf9c
JM
4846#ifdef OFFSET
4847#undef OFFSET
4848#endif /* OFFSET */
4849/* OFFSET: Get offset of a variable within the wpa_config structure */
4850#define OFFSET(v) ((void *) &((struct wpa_config *) 0)->v)
4851
10263dc2
OO
4852#define FUNC(f) #f, wpa_config_process_ ## f, NULL, OFFSET(f), NULL, NULL
4853#define FUNC_NO_VAR(f) #f, wpa_config_process_ ## f, NULL, NULL, NULL, NULL
4854#define _INT(f) #f, wpa_global_config_parse_int, wpa_config_get_int, OFFSET(f)
121adf9c
JM
4855#define INT(f) _INT(f), NULL, NULL
4856#define INT_RANGE(f, min, max) _INT(f), (void *) min, (void *) max
10263dc2 4857#define _STR(f) #f, wpa_global_config_parse_str, wpa_config_get_str, OFFSET(f)
121adf9c
JM
4858#define STR(f) _STR(f), NULL, NULL
4859#define STR_RANGE(f, min, max) _STR(f), (void *) min, (void *) max
10263dc2 4860#define BIN(f) #f, wpa_global_config_parse_bin, NULL, OFFSET(f), NULL, NULL
5c6c315f
MK
4861#define IPV4(f) #f, wpa_global_config_parse_ipv4, wpa_config_get_ipv4, \
4862 OFFSET(f), NULL, NULL
121adf9c
JM
4863
4864static const struct global_parse_data global_fields[] = {
4865#ifdef CONFIG_CTRL_IFACE
1d47214a 4866 { STR(ctrl_interface), 0 },
ea61aa1d 4867 { FUNC_NO_VAR(no_ctrl_interface), 0 },
1d47214a 4868 { STR(ctrl_interface_group), 0 } /* deprecated */,
121adf9c 4869#endif /* CONFIG_CTRL_IFACE */
0836c04b
HW
4870#ifdef CONFIG_MACSEC
4871 { INT_RANGE(eapol_version, 1, 3), 0 },
4872#else /* CONFIG_MACSEC */
1d47214a 4873 { INT_RANGE(eapol_version, 1, 2), 0 },
0836c04b 4874#endif /* CONFIG_MACSEC */
1d47214a 4875 { INT(ap_scan), 0 },
31392709 4876 { FUNC(bgscan), 0 },
e45e8989
TP
4877#ifdef CONFIG_MESH
4878 { INT(user_mpm), 0 },
4b409368 4879 { INT_RANGE(max_peer_links, 0, 255), 0 },
5a2a6de6 4880 { INT(mesh_max_inactivity), 0 },
ecd40fef 4881 { INT(dot11RSNASAERetransPeriod), 0 },
e45e8989 4882#endif /* CONFIG_MESH */
54ddd743 4883 { INT(disable_scan_offload), 0 },
1d47214a
JM
4884 { INT(fast_reauth), 0 },
4885 { STR(opensc_engine_path), 0 },
4886 { STR(pkcs11_engine_path), 0 },
4887 { STR(pkcs11_module_path), 0 },
07e2de31 4888 { STR(openssl_ciphers), 0 },
f64adcd7
JM
4889 { STR(pcsc_reader), 0 },
4890 { STR(pcsc_pin), 0 },
a5d44ac0 4891 { INT(external_sim), 0 },
1d47214a
JM
4892 { STR(driver_param), 0 },
4893 { INT(dot11RSNAConfigPMKLifetime), 0 },
4894 { INT(dot11RSNAConfigPMKReauthThreshold), 0 },
4895 { INT(dot11RSNAConfigSATimeout), 0 },
121adf9c 4896#ifndef CONFIG_NO_CONFIG_WRITE
1d47214a 4897 { INT(update_config), 0 },
121adf9c 4898#endif /* CONFIG_NO_CONFIG_WRITE */
1d47214a 4899 { FUNC_NO_VAR(load_dynamic_eap), 0 },
121adf9c 4900#ifdef CONFIG_WPS
1d47214a 4901 { FUNC(uuid), CFG_CHANGED_UUID },
183d3924 4902 { INT_RANGE(auto_uuid, 0, 1), 0 },
cc6f2438
JM
4903 { STR_RANGE(device_name, 0, WPS_DEV_NAME_MAX_LEN),
4904 CFG_CHANGED_DEVICE_NAME },
1c9cb49f
JM
4905 { STR_RANGE(manufacturer, 0, 64), CFG_CHANGED_WPS_STRING },
4906 { STR_RANGE(model_name, 0, 32), CFG_CHANGED_WPS_STRING },
4907 { STR_RANGE(model_number, 0, 32), CFG_CHANGED_WPS_STRING },
4908 { STR_RANGE(serial_number, 0, 32), CFG_CHANGED_WPS_STRING },
2f646b6e 4909 { FUNC(device_type), CFG_CHANGED_DEVICE_TYPE },
1d47214a
JM
4910 { FUNC(os_version), CFG_CHANGED_OS_VERSION },
4911 { STR(config_methods), CFG_CHANGED_CONFIG_METHODS },
4912 { INT_RANGE(wps_cred_processing, 0, 2), 0 },
339dc8bd 4913 { INT_RANGE(wps_cred_add_sae, 0, 1), 0 },
71dd3b78 4914 { FUNC(wps_vendor_ext_m1), CFG_CHANGED_VENDOR_EXTENSION },
121adf9c 4915#endif /* CONFIG_WPS */
e3768e7c
JM
4916#ifdef CONFIG_P2P
4917 { FUNC(sec_device_type), CFG_CHANGED_SEC_DEVICE_TYPE },
86e2dcf5
PK
4918 { INT(p2p_listen_reg_class), CFG_CHANGED_P2P_LISTEN_CHANNEL },
4919 { INT(p2p_listen_channel), CFG_CHANGED_P2P_LISTEN_CHANNEL },
00eb2993
JM
4920 { INT(p2p_oper_reg_class), CFG_CHANGED_P2P_OPER_CHANNEL },
4921 { INT(p2p_oper_channel), CFG_CHANGED_P2P_OPER_CHANNEL },
e3768e7c
JM
4922 { INT_RANGE(p2p_go_intent, 0, 15), 0 },
4923 { STR(p2p_ssid_postfix), CFG_CHANGED_P2P_SSID_POSTFIX },
4924 { INT_RANGE(persistent_reconnect, 0, 1), 0 },
0f66abd2 4925 { INT_RANGE(p2p_intra_bss, 0, 1), CFG_CHANGED_P2P_INTRA_BSS },
3071e181 4926 { INT(p2p_group_idle), 0 },
1a471ff0 4927 { INT_RANGE(p2p_go_freq_change_policy, 0, P2P_GO_FREQ_MOVE_MAX), 0 },
1b928f96
JM
4928 { INT_RANGE(p2p_passphrase_len, 8, 63),
4929 CFG_CHANGED_P2P_PASSPHRASE_LEN },
21d996f7 4930 { FUNC(p2p_pref_chan), CFG_CHANGED_P2P_PREF_CHAN },
556b30da 4931 { FUNC(p2p_no_go_freq), CFG_CHANGED_P2P_PREF_CHAN },
51e9f228 4932 { INT_RANGE(p2p_add_cli_chan, 0, 1), 0 },
e3bd6e9d 4933 { INT_RANGE(p2p_optimize_listen_chan, 0, 1), 0 },
a93a15bb 4934 { INT(p2p_go_ht40), 0 },
20ea1ca4 4935 { INT(p2p_go_vht), 0 },
5a3319ab 4936 { INT(p2p_go_he), 0 },
7a808c7e 4937 { INT(p2p_disabled), 0 },
0b8bcaa5 4938 { INT_RANGE(p2p_go_ctwindow, 0, 127), 0 },
d76cd41a 4939 { INT(p2p_no_group_iface), 0 },
b277a2be 4940 { INT_RANGE(p2p_ignore_shared_freq, 0, 1), 0 },
25ef8529
JM
4941 { IPV4(ip_addr_go), 0 },
4942 { IPV4(ip_addr_mask), 0 },
4943 { IPV4(ip_addr_start), 0 },
4944 { IPV4(ip_addr_end), 0 },
07c1e987 4945 { INT_RANGE(p2p_cli_probe, 0, 1), 0 },
9359cc84
JC
4946 { INT(p2p_device_random_mac_addr), 0 },
4947 { FUNC(p2p_device_persistent_mac_addr), 0 },
a95906f9 4948 { INT(p2p_interface_random_mac_addr), 0 },
e3768e7c 4949#endif /* CONFIG_P2P */
1d47214a
JM
4950 { FUNC(country), CFG_CHANGED_COUNTRY },
4951 { INT(bss_max_count), 0 },
78633c37
SL
4952 { INT(bss_expiration_age), 0 },
4953 { INT(bss_expiration_scan_count), 0 },
dae608d5 4954 { INT_RANGE(filter_ssids, 0, 1), 0 },
bf8d6d24 4955 { INT_RANGE(filter_rssi, -100, 0), 0 },
0d7e5a3a 4956 { INT(max_num_sta), 0 },
19e20c14 4957 { INT_RANGE(ap_isolate, 0, 1), 0 },
46ee0427 4958 { INT_RANGE(disassoc_low_ack, 0, 1), 0 },
66aadbd7
JK
4959#ifdef CONFIG_HS20
4960 { INT_RANGE(hs20, 0, 1), 0 },
4961#endif /* CONFIG_HS20 */
46ee0427 4962 { INT_RANGE(interworking, 0, 1), 0 },
11540c0b 4963 { FUNC(hessid), 0 },
1298c145 4964 { INT_RANGE(access_network_type, 0, 15), 0 },
63bc0ab0
SD
4965 { INT_RANGE(go_interworking, 0, 1), 0 },
4966 { INT_RANGE(go_access_network_type, 0, 15), 0 },
4967 { INT_RANGE(go_internet, 0, 1), 0 },
4968 { INT_RANGE(go_venue_group, 0, 255), 0 },
4969 { INT_RANGE(go_venue_type, 0, 255), 0 },
b0786fba 4970 { INT_RANGE(pbc_in_m1, 0, 1), 0 },
3f2c8ba6 4971 { STR(autoscan), 0 },
042ec551
JM
4972 { INT_RANGE(wps_nfc_dev_pw_id, 0x10, 0xffff),
4973 CFG_CHANGED_NFC_PASSWORD_TOKEN },
4974 { BIN(wps_nfc_dh_pubkey), CFG_CHANGED_NFC_PASSWORD_TOKEN },
4975 { BIN(wps_nfc_dh_privkey), CFG_CHANGED_NFC_PASSWORD_TOKEN },
4976 { BIN(wps_nfc_dev_pw), CFG_CHANGED_NFC_PASSWORD_TOKEN },
462a7439
ES
4977 { STR(ext_password_backend), CFG_CHANGED_EXT_PW_BACKEND },
4978 { INT(p2p_go_max_inactivity), 0 },
4d5bda5f 4979 { INT_RANGE(auto_interworking, 0, 1), 0 },
6e202021 4980 { INT(okc), 0 },
62d49803 4981 { INT(pmf), 0 },
625f202a 4982 { FUNC(sae_groups), 0 },
d2b20838 4983 { INT_RANGE(sae_pmkid_in_assoc, 0, 1), 0 },
18206e02
JM
4984 { INT(dtim_period), 0 },
4985 { INT(beacon_int), 0 },
18a2eaab 4986 { FUNC(ap_vendor_elements), 0 },
4342326f 4987 { INT_RANGE(ignore_old_scan_res, 0, 1), 0 },
f5ffc348 4988 { FUNC(freq_list), 0 },
6124e858 4989 { INT(scan_cur_freq), 0 },
4aa81868 4990 { INT(sched_scan_interval), 0 },
d0330d57 4991 { INT(sched_scan_start_delay), 0 },
800d5872 4992 { INT(tdls_external_control), 0},
b572df86 4993 { STR(osu_dir), 0 },
3c7863f8 4994 { STR(wowlan_triggers), CFG_CHANGED_WOWLAN_TRIGGERS },
d3b20469 4995 { INT(p2p_search_delay), 0},
c267753b
JM
4996 { INT(mac_addr), 0 },
4997 { INT(rand_addr_lifetime), 0 },
4998 { INT(preassoc_mac_addr), 0 },
b41f2684 4999 { INT(key_mgmt_offload), 0},
c35e35ed 5000 { INT(passive_scan), 0 },
59b416c7 5001 { INT(reassoc_same_bss_optim), 0 },
94687a0a 5002 { INT(wps_priority), 0},
76ca15b7
AN
5003#ifdef CONFIG_FST
5004 { STR_RANGE(fst_group_id, 1, FST_MAX_GROUP_ID_LEN), 0 },
5005 { INT_RANGE(fst_priority, 1, FST_MAX_PRIO_VALUE), 0 },
5006 { INT_RANGE(fst_llt, 1, FST_MAX_LLT_MS), 0 },
5007#endif /* CONFIG_FST */
f8c20186 5008 { INT_RANGE(cert_in_cb, 0, 1), 0 },
73ed03f3 5009 { INT_RANGE(wpa_rsc_relaxation, 0, 1), 0 },
bea48f77 5010 { STR(sched_scan_plans), CFG_CHANGED_SCHED_SCAN_PLANS },
facf2c72
DS
5011#ifdef CONFIG_MBO
5012 { STR(non_pref_chan), 0 },
c5d193d7
DS
5013 { INT_RANGE(mbo_cell_capa, MBO_CELL_CAPA_AVAILABLE,
5014 MBO_CELL_CAPA_NOT_SUPPORTED), 0 },
af8bc24d 5015 { INT_RANGE(disassoc_imminent_rssi_threshold, -120, 0), 0 },
332aadb8 5016 { INT_RANGE(oce, 0, 3), 0 },
6d49aeb7 5017#endif /* CONFIG_MBO */
c86bef29 5018 { INT(gas_address3), 0 },
d1723c55
LD
5019 { INT_RANGE(ftm_responder, 0, 1), 0 },
5020 { INT_RANGE(ftm_initiator, 0, 1), 0 },
1d9d21f3
VK
5021 { INT(gas_rand_addr_lifetime), 0 },
5022 { INT_RANGE(gas_rand_mac_addr, 0, 2), 0 },
8528994e 5023 { INT_RANGE(dpp_config_processing, 0, 2), 0 },
d514b502 5024 { INT_RANGE(coloc_intf_reporting, 0, 1), 0 },
ef59f987
AB
5025#ifdef CONFIG_WNM
5026 { INT_RANGE(disable_btm, 0, 1), CFG_CHANGED_DISABLE_BTM },
5027#endif /* CONFIG_WNM */
121adf9c
JM
5028};
5029
5030#undef FUNC
5031#undef _INT
5032#undef INT
5033#undef INT_RANGE
5034#undef _STR
5035#undef STR
5036#undef STR_RANGE
3f2c8ba6 5037#undef BIN
25ef8529 5038#undef IPV4
e7ecab4a 5039#define NUM_GLOBAL_FIELDS ARRAY_SIZE(global_fields)
121adf9c
JM
5040
5041
10263dc2
OO
5042int wpa_config_dump_values(struct wpa_config *config, char *buf, size_t buflen)
5043{
5044 int result = 0;
5045 size_t i;
5046
5047 for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
5048 const struct global_parse_data *field = &global_fields[i];
5049 int tmp;
5050
5051 if (!field->get)
5052 continue;
5053
5054 tmp = field->get(field->name, config, (long) field->param1,
5055 buf, buflen, 1);
5056 if (tmp < 0)
5057 return -1;
5058 buf += tmp;
5059 buflen -= tmp;
5060 result += tmp;
5061 }
5062 return result;
5063}
5064
5065
5066int wpa_config_get_value(const char *name, struct wpa_config *config,
5067 char *buf, size_t buflen)
5068{
5069 size_t i;
5070
5071 for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
5072 const struct global_parse_data *field = &global_fields[i];
5073
5074 if (os_strcmp(name, field->name) != 0)
5075 continue;
5076 if (!field->get)
5077 break;
5078 return field->get(name, config, (long) field->param1,
5079 buf, buflen, 0);
5080 }
5081
5082 return -1;
5083}
5084
5085
e50c50d5
DW
5086int wpa_config_get_num_global_field_names(void)
5087{
5088 return NUM_GLOBAL_FIELDS;
5089}
5090
5091
5092const char * wpa_config_get_global_field_name(unsigned int i, int *no_var)
5093{
5094 if (i >= NUM_GLOBAL_FIELDS)
5095 return NULL;
5096
5097 if (no_var)
5098 *no_var = !global_fields[i].param1;
5099 return global_fields[i].name;
5100}
5101
5102
121adf9c
JM
5103int wpa_config_process_global(struct wpa_config *config, char *pos, int line)
5104{
5105 size_t i;
5106 int ret = 0;
5107
5108 for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
5109 const struct global_parse_data *field = &global_fields[i];
5110 size_t flen = os_strlen(field->name);
5111 if (os_strncmp(pos, field->name, flen) != 0 ||
5112 pos[flen] != '=')
5113 continue;
5114
5115 if (field->parser(field, config, line, pos + flen + 1)) {
5116 wpa_printf(MSG_ERROR, "Line %d: failed to "
5117 "parse '%s'.", line, pos);
5118 ret = -1;
5119 }
042ec551
JM
5120 if (field->changed_flag == CFG_CHANGED_NFC_PASSWORD_TOKEN)
5121 config->wps_nfc_pw_from_config = 1;
1d47214a 5122 config->changed_parameters |= field->changed_flag;
121adf9c
JM
5123 break;
5124 }
5125 if (i == NUM_GLOBAL_FIELDS) {
c26effe1
YD
5126#ifdef CONFIG_AP
5127 if (os_strncmp(pos, "wmm_ac_", 7) == 0) {
5128 char *tmp = os_strchr(pos, '=');
5129 if (tmp == NULL) {
5130 if (line < 0)
5131 return -1;
5132 wpa_printf(MSG_ERROR, "Line %d: invalid line "
5133 "'%s'", line, pos);
5134 return -1;
5135 }
5136 *tmp++ = '\0';
5137 if (hostapd_config_wmm_ac(config->wmm_ac_params, pos,
5138 tmp)) {
5139 wpa_printf(MSG_ERROR, "Line %d: invalid WMM "
5140 "AC item", line);
5141 return -1;
5142 }
21dc1627 5143 return ret;
c26effe1
YD
5144 }
5145#endif /* CONFIG_AP */
1d47214a
JM
5146 if (line < 0)
5147 return -1;
121adf9c
JM
5148 wpa_printf(MSG_ERROR, "Line %d: unknown global field '%s'.",
5149 line, pos);
5150 ret = -1;
5151 }
5152
5153 return ret;
5154}