]> git.ipfire.org Git - thirdparty/hostap.git/blame - wpa_supplicant/config.c
PAE: Validate input before pointer
[thirdparty/hostap.git] / wpa_supplicant / config.c
CommitLineData
6fc6879b
JM
1/*
2 * WPA Supplicant / Configuration parser and common functions
5e3b5197 3 * Copyright (c) 2003-2015, 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;
738#endif /* CONFIG_IEEE80211R */
56586197
JM
739#ifdef CONFIG_IEEE80211W
740 else if (os_strcmp(start, "WPA-PSK-SHA256") == 0)
741 val |= WPA_KEY_MGMT_PSK_SHA256;
742 else if (os_strcmp(start, "WPA-EAP-SHA256") == 0)
743 val |= WPA_KEY_MGMT_IEEE8021X_SHA256;
744#endif /* CONFIG_IEEE80211W */
ad08c363
JM
745#ifdef CONFIG_WPS
746 else if (os_strcmp(start, "WPS") == 0)
747 val |= WPA_KEY_MGMT_WPS;
748#endif /* CONFIG_WPS */
c10347f2
JM
749#ifdef CONFIG_SAE
750 else if (os_strcmp(start, "SAE") == 0)
751 val |= WPA_KEY_MGMT_SAE;
752 else if (os_strcmp(start, "FT-SAE") == 0)
753 val |= WPA_KEY_MGMT_FT_SAE;
754#endif /* CONFIG_SAE */
df0f01d9
JM
755#ifdef CONFIG_HS20
756 else if (os_strcmp(start, "OSEN") == 0)
757 val |= WPA_KEY_MGMT_OSEN;
758#endif /* CONFIG_HS20 */
5e3b5197 759#ifdef CONFIG_SUITEB
666497c8
JM
760 else if (os_strcmp(start, "WPA-EAP-SUITE-B") == 0)
761 val |= WPA_KEY_MGMT_IEEE8021X_SUITE_B;
5e3b5197
JM
762#endif /* CONFIG_SUITEB */
763#ifdef CONFIG_SUITEB192
764 else if (os_strcmp(start, "WPA-EAP-SUITE-B-192") == 0)
765 val |= WPA_KEY_MGMT_IEEE8021X_SUITE_B_192;
766#endif /* CONFIG_SUITEB192 */
9b7a2b83
JM
767#ifdef CONFIG_FILS
768 else if (os_strcmp(start, "FILS-SHA256") == 0)
769 val |= WPA_KEY_MGMT_FILS_SHA256;
770 else if (os_strcmp(start, "FILS-SHA384") == 0)
771 val |= WPA_KEY_MGMT_FILS_SHA384;
772#ifdef CONFIG_IEEE80211R
773 else if (os_strcmp(start, "FT-FILS-SHA256") == 0)
774 val |= WPA_KEY_MGMT_FT_FILS_SHA256;
775 else if (os_strcmp(start, "FT-FILS-SHA384") == 0)
776 val |= WPA_KEY_MGMT_FT_FILS_SHA384;
777#endif /* CONFIG_IEEE80211R */
778#endif /* CONFIG_FILS */
a1ea1b45
JM
779#ifdef CONFIG_OWE
780 else if (os_strcmp(start, "OWE") == 0)
781 val |= WPA_KEY_MGMT_OWE;
782#endif /* CONFIG_OWE */
567da5bb
JM
783#ifdef CONFIG_DPP
784 else if (os_strcmp(start, "DPP") == 0)
785 val |= WPA_KEY_MGMT_DPP;
786#endif /* CONFIG_DPP */
6fc6879b
JM
787 else {
788 wpa_printf(MSG_ERROR, "Line %d: invalid key_mgmt '%s'",
789 line, start);
790 errors++;
791 }
792
793 if (last)
794 break;
795 start = end + 1;
796 }
797 os_free(buf);
798
799 if (val == 0) {
800 wpa_printf(MSG_ERROR,
801 "Line %d: no key_mgmt values configured.", line);
802 errors++;
803 }
804
5cd317d3
BKB
805 if (!errors && ssid->key_mgmt == val)
806 return 1;
6fc6879b
JM
807 wpa_printf(MSG_MSGDUMP, "key_mgmt: 0x%x", val);
808 ssid->key_mgmt = val;
809 return errors ? -1 : 0;
810}
811
812
813#ifndef NO_CONFIG_WRITE
814static char * wpa_config_write_key_mgmt(const struct parse_data *data,
815 struct wpa_ssid *ssid)
816{
817 char *buf, *pos, *end;
818 int ret;
819
50793929 820 pos = buf = os_zalloc(100);
6fc6879b
JM
821 if (buf == NULL)
822 return NULL;
50793929 823 end = buf + 100;
6fc6879b
JM
824
825 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
826 ret = os_snprintf(pos, end - pos, "%sWPA-PSK",
827 pos == buf ? "" : " ");
d85e1fc8 828 if (os_snprintf_error(end - pos, ret)) {
6fc6879b
JM
829 end[-1] = '\0';
830 return buf;
831 }
832 pos += ret;
833 }
834
835 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
836 ret = os_snprintf(pos, end - pos, "%sWPA-EAP",
837 pos == buf ? "" : " ");
d85e1fc8 838 if (os_snprintf_error(end - pos, ret)) {
6fc6879b
JM
839 end[-1] = '\0';
840 return buf;
841 }
842 pos += ret;
843 }
844
845 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
846 ret = os_snprintf(pos, end - pos, "%sIEEE8021X",
847 pos == buf ? "" : " ");
d85e1fc8 848 if (os_snprintf_error(end - pos, ret)) {
6fc6879b
JM
849 end[-1] = '\0';
850 return buf;
851 }
852 pos += ret;
853 }
854
855 if (ssid->key_mgmt & WPA_KEY_MGMT_NONE) {
856 ret = os_snprintf(pos, end - pos, "%sNONE",
857 pos == buf ? "" : " ");
d85e1fc8 858 if (os_snprintf_error(end - pos, ret)) {
6fc6879b
JM
859 end[-1] = '\0';
860 return buf;
861 }
862 pos += ret;
863 }
864
865 if (ssid->key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
866 ret = os_snprintf(pos, end - pos, "%sWPA-NONE",
867 pos == buf ? "" : " ");
d85e1fc8 868 if (os_snprintf_error(end - pos, ret)) {
6fc6879b
JM
869 end[-1] = '\0';
870 return buf;
871 }
872 pos += ret;
873 }
874
875#ifdef CONFIG_IEEE80211R
50793929
PF
876 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_PSK) {
877 ret = os_snprintf(pos, end - pos, "%sFT-PSK",
878 pos == buf ? "" : " ");
d85e1fc8 879 if (os_snprintf_error(end - pos, ret)) {
50793929
PF
880 end[-1] = '\0';
881 return buf;
882 }
883 pos += ret;
884 }
6fc6879b 885
50793929
PF
886 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
887 ret = os_snprintf(pos, end - pos, "%sFT-EAP",
888 pos == buf ? "" : " ");
d85e1fc8 889 if (os_snprintf_error(end - pos, ret)) {
50793929
PF
890 end[-1] = '\0';
891 return buf;
892 }
893 pos += ret;
894 }
6fc6879b
JM
895#endif /* CONFIG_IEEE80211R */
896
56586197 897#ifdef CONFIG_IEEE80211W
50793929
PF
898 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
899 ret = os_snprintf(pos, end - pos, "%sWPA-PSK-SHA256",
900 pos == buf ? "" : " ");
d85e1fc8 901 if (os_snprintf_error(end - pos, ret)) {
50793929
PF
902 end[-1] = '\0';
903 return buf;
904 }
905 pos += ret;
906 }
56586197 907
50793929
PF
908 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
909 ret = os_snprintf(pos, end - pos, "%sWPA-EAP-SHA256",
910 pos == buf ? "" : " ");
d85e1fc8 911 if (os_snprintf_error(end - pos, ret)) {
50793929
PF
912 end[-1] = '\0';
913 return buf;
914 }
915 pos += ret;
916 }
56586197
JM
917#endif /* CONFIG_IEEE80211W */
918
728fae16 919#ifdef CONFIG_WPS
50793929
PF
920 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
921 ret = os_snprintf(pos, end - pos, "%sWPS",
922 pos == buf ? "" : " ");
d85e1fc8 923 if (os_snprintf_error(end - pos, ret)) {
50793929
PF
924 end[-1] = '\0';
925 return buf;
926 }
927 pos += ret;
928 }
728fae16
JM
929#endif /* CONFIG_WPS */
930
d3fd563f
TP
931#ifdef CONFIG_SAE
932 if (ssid->key_mgmt & WPA_KEY_MGMT_SAE) {
933 ret = os_snprintf(pos, end - pos, "%sSAE",
934 pos == buf ? "" : " ");
d85e1fc8 935 if (os_snprintf_error(end - pos, ret)) {
d3fd563f
TP
936 end[-1] = '\0';
937 return buf;
938 }
939 pos += ret;
940 }
941
942 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_SAE) {
943 ret = os_snprintf(pos, end - pos, "%sFT-SAE",
944 pos == buf ? "" : " ");
d85e1fc8 945 if (os_snprintf_error(end - pos, ret)) {
d3fd563f
TP
946 end[-1] = '\0';
947 return buf;
948 }
949 pos += ret;
950 }
951#endif /* CONFIG_SAE */
952
2d6ee86f
JM
953#ifdef CONFIG_HS20
954 if (ssid->key_mgmt & WPA_KEY_MGMT_OSEN) {
955 ret = os_snprintf(pos, end - pos, "%sOSEN",
956 pos == buf ? "" : " ");
d85e1fc8 957 if (os_snprintf_error(end - pos, ret)) {
2d6ee86f
JM
958 end[-1] = '\0';
959 return buf;
960 }
961 pos += ret;
962 }
963#endif /* CONFIG_HS20 */
964
5e3b5197 965#ifdef CONFIG_SUITEB
666497c8
JM
966 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
967 ret = os_snprintf(pos, end - pos, "%sWPA-EAP-SUITE-B",
968 pos == buf ? "" : " ");
d85e1fc8 969 if (os_snprintf_error(end - pos, ret)) {
666497c8
JM
970 end[-1] = '\0';
971 return buf;
972 }
973 pos += ret;
974 }
5e3b5197
JM
975#endif /* CONFIG_SUITEB */
976
977#ifdef CONFIG_SUITEB192
978 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
979 ret = os_snprintf(pos, end - pos, "%sWPA-EAP-SUITE-B-192",
980 pos == buf ? "" : " ");
981 if (os_snprintf_error(end - pos, ret)) {
982 end[-1] = '\0';
983 return buf;
984 }
985 pos += ret;
986 }
987#endif /* CONFIG_SUITEB192 */
666497c8 988
199eb3a4 989#ifdef CONFIG_FILS
990 if (ssid->key_mgmt & WPA_KEY_MGMT_FILS_SHA256) {
991 ret = os_snprintf(pos, end - pos, "%sFILS-SHA256",
992 pos == buf ? "" : " ");
993 if (os_snprintf_error(end - pos, ret)) {
994 end[-1] = '\0';
995 return buf;
996 }
997 pos += ret;
998 }
999 if (ssid->key_mgmt & WPA_KEY_MGMT_FILS_SHA384) {
1000 ret = os_snprintf(pos, end - pos, "%sFILS-SHA384",
1001 pos == buf ? "" : " ");
1002 if (os_snprintf_error(end - pos, ret)) {
1003 end[-1] = '\0';
1004 return buf;
1005 }
1006 pos += ret;
1007 }
1008#ifdef CONFIG_IEEE80211R
1009 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA256) {
1010 ret = os_snprintf(pos, end - pos, "%sFT-FILS-SHA256",
1011 pos == buf ? "" : " ");
1012 if (os_snprintf_error(end - pos, ret)) {
1013 end[-1] = '\0';
1014 return buf;
1015 }
1016 pos += ret;
1017 }
1018 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA384) {
1019 ret = os_snprintf(pos, end - pos, "%sFT-FILS-SHA384",
1020 pos == buf ? "" : " ");
1021 if (os_snprintf_error(end - pos, ret)) {
1022 end[-1] = '\0';
1023 return buf;
1024 }
1025 pos += ret;
1026 }
1027#endif /* CONFIG_IEEE80211R */
1028#endif /* CONFIG_FILS */
1029
a1e46f32
JM
1030 if (pos == buf) {
1031 os_free(buf);
1032 buf = NULL;
1033 }
1034
6fc6879b
JM
1035 return buf;
1036}
1037#endif /* NO_CONFIG_WRITE */
1038
1039
1040static int wpa_config_parse_cipher(int line, const char *value)
1041{
9e68742e
JM
1042#ifdef CONFIG_NO_WPA
1043 return -1;
1044#else /* CONFIG_NO_WPA */
a39c78be
JM
1045 int val = wpa_parse_cipher(value);
1046 if (val < 0) {
1047 wpa_printf(MSG_ERROR, "Line %d: invalid cipher '%s'.",
1048 line, value);
6fc6879b 1049 return -1;
6fc6879b 1050 }
6fc6879b
JM
1051 if (val == 0) {
1052 wpa_printf(MSG_ERROR, "Line %d: no cipher values configured.",
1053 line);
1054 return -1;
1055 }
1056 return val;
9e68742e 1057#endif /* CONFIG_NO_WPA */
6fc6879b
JM
1058}
1059
1060
1061#ifndef NO_CONFIG_WRITE
1062static char * wpa_config_write_cipher(int cipher)
1063{
9e68742e
JM
1064#ifdef CONFIG_NO_WPA
1065 return NULL;
1066#else /* CONFIG_NO_WPA */
0282a8c4 1067 char *buf = os_zalloc(50);
6fc6879b
JM
1068 if (buf == NULL)
1069 return NULL;
6fc6879b 1070
0282a8c4
JM
1071 if (wpa_write_ciphers(buf, buf + 50, cipher, " ") < 0) {
1072 os_free(buf);
1073 return NULL;
6fc6879b
JM
1074 }
1075
1076 return buf;
9e68742e 1077#endif /* CONFIG_NO_WPA */
6fc6879b
JM
1078}
1079#endif /* NO_CONFIG_WRITE */
1080
1081
1082static int wpa_config_parse_pairwise(const struct parse_data *data,
1083 struct wpa_ssid *ssid, int line,
1084 const char *value)
1085{
1086 int val;
1087 val = wpa_config_parse_cipher(line, value);
1088 if (val == -1)
1089 return -1;
03145326 1090 if (val & ~WPA_ALLOWED_PAIRWISE_CIPHERS) {
6fc6879b
JM
1091 wpa_printf(MSG_ERROR, "Line %d: not allowed pairwise cipher "
1092 "(0x%x).", line, val);
1093 return -1;
1094 }
1095
5cd317d3
BKB
1096 if (ssid->pairwise_cipher == val)
1097 return 1;
6fc6879b
JM
1098 wpa_printf(MSG_MSGDUMP, "pairwise: 0x%x", val);
1099 ssid->pairwise_cipher = val;
1100 return 0;
1101}
1102
1103
1104#ifndef NO_CONFIG_WRITE
1105static char * wpa_config_write_pairwise(const struct parse_data *data,
1106 struct wpa_ssid *ssid)
1107{
1108 return wpa_config_write_cipher(ssid->pairwise_cipher);
1109}
1110#endif /* NO_CONFIG_WRITE */
1111
1112
1113static int wpa_config_parse_group(const struct parse_data *data,
1114 struct wpa_ssid *ssid, int line,
1115 const char *value)
1116{
1117 int val;
1118 val = wpa_config_parse_cipher(line, value);
1119 if (val == -1)
1120 return -1;
ce8963fc
JM
1121
1122 /*
1123 * Backwards compatibility - filter out WEP ciphers that were previously
1124 * allowed.
1125 */
1126 val &= ~(WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40);
1127
03145326 1128 if (val & ~WPA_ALLOWED_GROUP_CIPHERS) {
6fc6879b
JM
1129 wpa_printf(MSG_ERROR, "Line %d: not allowed group cipher "
1130 "(0x%x).", line, val);
1131 return -1;
1132 }
1133
5cd317d3
BKB
1134 if (ssid->group_cipher == val)
1135 return 1;
6fc6879b
JM
1136 wpa_printf(MSG_MSGDUMP, "group: 0x%x", val);
1137 ssid->group_cipher = val;
1138 return 0;
1139}
1140
1141
1142#ifndef NO_CONFIG_WRITE
1143static char * wpa_config_write_group(const struct parse_data *data,
1144 struct wpa_ssid *ssid)
1145{
1146 return wpa_config_write_cipher(ssid->group_cipher);
1147}
1148#endif /* NO_CONFIG_WRITE */
1149
1150
1151static int wpa_config_parse_auth_alg(const struct parse_data *data,
1152 struct wpa_ssid *ssid, int line,
1153 const char *value)
1154{
1155 int val = 0, last, errors = 0;
1156 char *start, *end, *buf;
1157
1158 buf = os_strdup(value);
1159 if (buf == NULL)
1160 return -1;
1161 start = buf;
1162
1163 while (*start != '\0') {
1164 while (*start == ' ' || *start == '\t')
1165 start++;
1166 if (*start == '\0')
1167 break;
1168 end = start;
1169 while (*end != ' ' && *end != '\t' && *end != '\0')
1170 end++;
1171 last = *end == '\0';
1172 *end = '\0';
1173 if (os_strcmp(start, "OPEN") == 0)
1174 val |= WPA_AUTH_ALG_OPEN;
1175 else if (os_strcmp(start, "SHARED") == 0)
1176 val |= WPA_AUTH_ALG_SHARED;
1177 else if (os_strcmp(start, "LEAP") == 0)
1178 val |= WPA_AUTH_ALG_LEAP;
1179 else {
1180 wpa_printf(MSG_ERROR, "Line %d: invalid auth_alg '%s'",
1181 line, start);
1182 errors++;
1183 }
1184
1185 if (last)
1186 break;
1187 start = end + 1;
1188 }
1189 os_free(buf);
1190
1191 if (val == 0) {
1192 wpa_printf(MSG_ERROR,
1193 "Line %d: no auth_alg values configured.", line);
1194 errors++;
1195 }
1196
5cd317d3
BKB
1197 if (!errors && ssid->auth_alg == val)
1198 return 1;
6fc6879b
JM
1199 wpa_printf(MSG_MSGDUMP, "auth_alg: 0x%x", val);
1200 ssid->auth_alg = val;
1201 return errors ? -1 : 0;
1202}
1203
1204
1205#ifndef NO_CONFIG_WRITE
1206static char * wpa_config_write_auth_alg(const struct parse_data *data,
1207 struct wpa_ssid *ssid)
1208{
1209 char *buf, *pos, *end;
1210 int ret;
1211
1212 pos = buf = os_zalloc(30);
1213 if (buf == NULL)
1214 return NULL;
1215 end = buf + 30;
1216
1217 if (ssid->auth_alg & WPA_AUTH_ALG_OPEN) {
1218 ret = os_snprintf(pos, end - pos, "%sOPEN",
1219 pos == buf ? "" : " ");
d85e1fc8 1220 if (os_snprintf_error(end - pos, ret)) {
6fc6879b
JM
1221 end[-1] = '\0';
1222 return buf;
1223 }
1224 pos += ret;
1225 }
1226
1227 if (ssid->auth_alg & WPA_AUTH_ALG_SHARED) {
1228 ret = os_snprintf(pos, end - pos, "%sSHARED",
1229 pos == buf ? "" : " ");
d85e1fc8 1230 if (os_snprintf_error(end - pos, ret)) {
6fc6879b
JM
1231 end[-1] = '\0';
1232 return buf;
1233 }
1234 pos += ret;
1235 }
1236
1237 if (ssid->auth_alg & WPA_AUTH_ALG_LEAP) {
1238 ret = os_snprintf(pos, end - pos, "%sLEAP",
1239 pos == buf ? "" : " ");
d85e1fc8 1240 if (os_snprintf_error(end - pos, ret)) {
6fc6879b
JM
1241 end[-1] = '\0';
1242 return buf;
1243 }
1244 pos += ret;
1245 }
1246
a1e46f32
JM
1247 if (pos == buf) {
1248 os_free(buf);
1249 buf = NULL;
1250 }
1251
6fc6879b
JM
1252 return buf;
1253}
1254#endif /* NO_CONFIG_WRITE */
1255
1256
625f202a 1257static int * wpa_config_parse_int_array(const char *value)
d3a98225
JM
1258{
1259 int *freqs;
1260 size_t used, len;
1261 const char *pos;
1262
1263 used = 0;
1264 len = 10;
f9884c09 1265 freqs = os_calloc(len + 1, sizeof(int));
d3a98225 1266 if (freqs == NULL)
b766a9a2 1267 return NULL;
d3a98225
JM
1268
1269 pos = value;
1270 while (pos) {
1271 while (*pos == ' ')
1272 pos++;
1273 if (used == len) {
1274 int *n;
1275 size_t i;
067ffa26 1276 n = os_realloc_array(freqs, len * 2 + 1, sizeof(int));
d3a98225
JM
1277 if (n == NULL) {
1278 os_free(freqs);
b766a9a2 1279 return NULL;
d3a98225
JM
1280 }
1281 for (i = len; i <= len * 2; i++)
1282 n[i] = 0;
1283 freqs = n;
1284 len *= 2;
1285 }
1286
1287 freqs[used] = atoi(pos);
1288 if (freqs[used] == 0)
1289 break;
1290 used++;
1291 pos = os_strchr(pos + 1, ' ');
1292 }
1293
b766a9a2
JM
1294 return freqs;
1295}
1296
1297
1298static int wpa_config_parse_scan_freq(const struct parse_data *data,
1299 struct wpa_ssid *ssid, int line,
1300 const char *value)
1301{
1302 int *freqs;
1303
625f202a 1304 freqs = wpa_config_parse_int_array(value);
b766a9a2
JM
1305 if (freqs == NULL)
1306 return -1;
6668efda
JM
1307 if (freqs[0] == 0) {
1308 os_free(freqs);
1309 freqs = NULL;
1310 }
d3a98225
JM
1311 os_free(ssid->scan_freq);
1312 ssid->scan_freq = freqs;
1313
1314 return 0;
1315}
1316
1317
b766a9a2
JM
1318static int wpa_config_parse_freq_list(const struct parse_data *data,
1319 struct wpa_ssid *ssid, int line,
1320 const char *value)
1321{
1322 int *freqs;
1323
625f202a 1324 freqs = wpa_config_parse_int_array(value);
b766a9a2
JM
1325 if (freqs == NULL)
1326 return -1;
6668efda
JM
1327 if (freqs[0] == 0) {
1328 os_free(freqs);
1329 freqs = NULL;
1330 }
b766a9a2
JM
1331 os_free(ssid->freq_list);
1332 ssid->freq_list = freqs;
1333
1334 return 0;
1335}
1336
1337
d3a98225 1338#ifndef NO_CONFIG_WRITE
b766a9a2
JM
1339static char * wpa_config_write_freqs(const struct parse_data *data,
1340 const int *freqs)
d3a98225
JM
1341{
1342 char *buf, *pos, *end;
1343 int i, ret;
1344 size_t count;
1345
b766a9a2 1346 if (freqs == NULL)
d3a98225
JM
1347 return NULL;
1348
1349 count = 0;
b766a9a2 1350 for (i = 0; freqs[i]; i++)
d3a98225
JM
1351 count++;
1352
1353 pos = buf = os_zalloc(10 * count + 1);
1354 if (buf == NULL)
1355 return NULL;
1356 end = buf + 10 * count + 1;
1357
b766a9a2 1358 for (i = 0; freqs[i]; i++) {
d3a98225 1359 ret = os_snprintf(pos, end - pos, "%s%u",
b766a9a2 1360 i == 0 ? "" : " ", freqs[i]);
d85e1fc8 1361 if (os_snprintf_error(end - pos, ret)) {
d3a98225
JM
1362 end[-1] = '\0';
1363 return buf;
1364 }
1365 pos += ret;
1366 }
1367
1368 return buf;
1369}
b766a9a2
JM
1370
1371
1372static char * wpa_config_write_scan_freq(const struct parse_data *data,
1373 struct wpa_ssid *ssid)
1374{
1375 return wpa_config_write_freqs(data, ssid->scan_freq);
1376}
1377
1378
1379static char * wpa_config_write_freq_list(const struct parse_data *data,
1380 struct wpa_ssid *ssid)
1381{
1382 return wpa_config_write_freqs(data, ssid->freq_list);
1383}
d3a98225
JM
1384#endif /* NO_CONFIG_WRITE */
1385
1386
6fc6879b
JM
1387#ifdef IEEE8021X_EAPOL
1388static int wpa_config_parse_eap(const struct parse_data *data,
1389 struct wpa_ssid *ssid, int line,
1390 const char *value)
1391{
1392 int last, errors = 0;
1393 char *start, *end, *buf;
1394 struct eap_method_type *methods = NULL, *tmp;
1395 size_t num_methods = 0;
1396
1397 buf = os_strdup(value);
1398 if (buf == NULL)
1399 return -1;
1400 start = buf;
1401
1402 while (*start != '\0') {
1403 while (*start == ' ' || *start == '\t')
1404 start++;
1405 if (*start == '\0')
1406 break;
1407 end = start;
1408 while (*end != ' ' && *end != '\t' && *end != '\0')
1409 end++;
1410 last = *end == '\0';
1411 *end = '\0';
1412 tmp = methods;
067ffa26
JM
1413 methods = os_realloc_array(methods, num_methods + 1,
1414 sizeof(*methods));
6fc6879b
JM
1415 if (methods == NULL) {
1416 os_free(tmp);
1417 os_free(buf);
1418 return -1;
1419 }
1420 methods[num_methods].method = eap_peer_get_type(
1421 start, &methods[num_methods].vendor);
1422 if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
1423 methods[num_methods].method == EAP_TYPE_NONE) {
1424 wpa_printf(MSG_ERROR, "Line %d: unknown EAP method "
1425 "'%s'", line, start);
1426 wpa_printf(MSG_ERROR, "You may need to add support for"
1427 " this EAP method during wpa_supplicant\n"
1428 "build time configuration.\n"
1429 "See README for more information.");
1430 errors++;
1431 } else if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
1432 methods[num_methods].method == EAP_TYPE_LEAP)
1433 ssid->leap++;
1434 else
1435 ssid->non_leap++;
1436 num_methods++;
1437 if (last)
1438 break;
1439 start = end + 1;
1440 }
1441 os_free(buf);
1442
1443 tmp = methods;
067ffa26 1444 methods = os_realloc_array(methods, num_methods + 1, sizeof(*methods));
6fc6879b
JM
1445 if (methods == NULL) {
1446 os_free(tmp);
1447 return -1;
1448 }
1449 methods[num_methods].vendor = EAP_VENDOR_IETF;
1450 methods[num_methods].method = EAP_TYPE_NONE;
1451 num_methods++;
1452
5cd317d3
BKB
1453 if (!errors && ssid->eap.eap_methods) {
1454 struct eap_method_type *prev_m;
1455 size_t i, j, prev_methods, match = 0;
1456
1457 prev_m = ssid->eap.eap_methods;
1458 for (i = 0; prev_m[i].vendor != EAP_VENDOR_IETF ||
1459 prev_m[i].method != EAP_TYPE_NONE; i++) {
1460 /* Count the methods */
1461 }
1462 prev_methods = i + 1;
1463
1464 for (i = 0; prev_methods == num_methods && i < prev_methods;
1465 i++) {
1466 for (j = 0; j < num_methods; j++) {
1467 if (prev_m[i].vendor == methods[j].vendor &&
1468 prev_m[i].method == methods[j].method) {
1469 match++;
1470 break;
1471 }
1472 }
1473 }
1474 if (match == num_methods) {
1475 os_free(methods);
1476 return 1;
1477 }
1478 }
6fc6879b
JM
1479 wpa_hexdump(MSG_MSGDUMP, "eap methods",
1480 (u8 *) methods, num_methods * sizeof(*methods));
bb8b1bb0 1481 os_free(ssid->eap.eap_methods);
6fc6879b
JM
1482 ssid->eap.eap_methods = methods;
1483 return errors ? -1 : 0;
1484}
1485
1486
954f03aa 1487#ifndef NO_CONFIG_WRITE
6fc6879b
JM
1488static char * wpa_config_write_eap(const struct parse_data *data,
1489 struct wpa_ssid *ssid)
1490{
1491 int i, ret;
1492 char *buf, *pos, *end;
1493 const struct eap_method_type *eap_methods = ssid->eap.eap_methods;
1494 const char *name;
1495
1496 if (eap_methods == NULL)
1497 return NULL;
1498
1499 pos = buf = os_zalloc(100);
1500 if (buf == NULL)
1501 return NULL;
1502 end = buf + 100;
1503
1504 for (i = 0; eap_methods[i].vendor != EAP_VENDOR_IETF ||
1505 eap_methods[i].method != EAP_TYPE_NONE; i++) {
1506 name = eap_get_name(eap_methods[i].vendor,
1507 eap_methods[i].method);
1508 if (name) {
1509 ret = os_snprintf(pos, end - pos, "%s%s",
1510 pos == buf ? "" : " ", name);
d85e1fc8 1511 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
1512 break;
1513 pos += ret;
1514 }
1515 }
1516
1517 end[-1] = '\0';
1518
1519 return buf;
1520}
954f03aa 1521#endif /* NO_CONFIG_WRITE */
6fc6879b
JM
1522
1523
1524static int wpa_config_parse_password(const struct parse_data *data,
1525 struct wpa_ssid *ssid, int line,
1526 const char *value)
1527{
1528 u8 *hash;
1529
b56c0546 1530 if (os_strcmp(value, "NULL") == 0) {
5cd317d3
BKB
1531 if (!ssid->eap.password)
1532 return 1; /* Already unset */
b56c0546 1533 wpa_printf(MSG_DEBUG, "Unset configuration string 'password'");
19c48da0 1534 bin_clear_free(ssid->eap.password, ssid->eap.password_len);
b56c0546
JM
1535 ssid->eap.password = NULL;
1536 ssid->eap.password_len = 0;
1537 return 0;
1538 }
1539
0ebb23e3
JM
1540#ifdef CONFIG_EXT_PASSWORD
1541 if (os_strncmp(value, "ext:", 4) == 0) {
1542 char *name = os_strdup(value + 4);
1543 if (name == NULL)
1544 return -1;
19c48da0 1545 bin_clear_free(ssid->eap.password, ssid->eap.password_len);
0ebb23e3
JM
1546 ssid->eap.password = (u8 *) name;
1547 ssid->eap.password_len = os_strlen(name);
1548 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
1549 ssid->eap.flags |= EAP_CONFIG_FLAGS_EXT_PASSWORD;
1550 return 0;
1551 }
1552#endif /* CONFIG_EXT_PASSWORD */
1553
6fc6879b
JM
1554 if (os_strncmp(value, "hash:", 5) != 0) {
1555 char *tmp;
1556 size_t res_len;
1557
1558 tmp = wpa_config_parse_string(value, &res_len);
1559 if (tmp == NULL) {
1560 wpa_printf(MSG_ERROR, "Line %d: failed to parse "
1561 "password.", line);
1562 return -1;
1563 }
556f5a2a
HS
1564 wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
1565 (u8 *) tmp, res_len);
6fc6879b 1566
19c48da0 1567 bin_clear_free(ssid->eap.password, ssid->eap.password_len);
6fc6879b
JM
1568 ssid->eap.password = (u8 *) tmp;
1569 ssid->eap.password_len = res_len;
1570 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
0ebb23e3 1571 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_PASSWORD;
6fc6879b
JM
1572
1573 return 0;
1574 }
1575
1576
1577 /* NtPasswordHash: hash:<32 hex digits> */
1578 if (os_strlen(value + 5) != 2 * 16) {
1579 wpa_printf(MSG_ERROR, "Line %d: Invalid password hash length "
1580 "(expected 32 hex digits)", line);
1581 return -1;
1582 }
1583
1584 hash = os_malloc(16);
1585 if (hash == NULL)
1586 return -1;
1587
1588 if (hexstr2bin(value + 5, hash, 16)) {
1589 os_free(hash);
1590 wpa_printf(MSG_ERROR, "Line %d: Invalid password hash", line);
1591 return -1;
1592 }
1593
1594 wpa_hexdump_key(MSG_MSGDUMP, data->name, hash, 16);
1595
5cd317d3
BKB
1596 if (ssid->eap.password && ssid->eap.password_len == 16 &&
1597 os_memcmp(ssid->eap.password, hash, 16) == 0 &&
1598 (ssid->eap.flags & EAP_CONFIG_FLAGS_PASSWORD_NTHASH)) {
1599 bin_clear_free(hash, 16);
1600 return 1;
1601 }
19c48da0 1602 bin_clear_free(ssid->eap.password, ssid->eap.password_len);
6fc6879b
JM
1603 ssid->eap.password = hash;
1604 ssid->eap.password_len = 16;
1605 ssid->eap.flags |= EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
0ebb23e3 1606 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_PASSWORD;
6fc6879b
JM
1607
1608 return 0;
1609}
1610
1611
954f03aa 1612#ifndef NO_CONFIG_WRITE
6fc6879b
JM
1613static char * wpa_config_write_password(const struct parse_data *data,
1614 struct wpa_ssid *ssid)
1615{
1616 char *buf;
1617
1618 if (ssid->eap.password == NULL)
1619 return NULL;
1620
0ebb23e3
JM
1621#ifdef CONFIG_EXT_PASSWORD
1622 if (ssid->eap.flags & EAP_CONFIG_FLAGS_EXT_PASSWORD) {
1623 buf = os_zalloc(4 + ssid->eap.password_len + 1);
1624 if (buf == NULL)
1625 return NULL;
1626 os_memcpy(buf, "ext:", 4);
1627 os_memcpy(buf + 4, ssid->eap.password, ssid->eap.password_len);
1628 return buf;
1629 }
1630#endif /* CONFIG_EXT_PASSWORD */
1631
6fc6879b
JM
1632 if (!(ssid->eap.flags & EAP_CONFIG_FLAGS_PASSWORD_NTHASH)) {
1633 return wpa_config_write_string(
1634 ssid->eap.password, ssid->eap.password_len);
1635 }
1636
1637 buf = os_malloc(5 + 32 + 1);
1638 if (buf == NULL)
1639 return NULL;
1640
1641 os_memcpy(buf, "hash:", 5);
1642 wpa_snprintf_hex(buf + 5, 32 + 1, ssid->eap.password, 16);
1643
1644 return buf;
1645}
954f03aa 1646#endif /* NO_CONFIG_WRITE */
6fc6879b
JM
1647#endif /* IEEE8021X_EAPOL */
1648
1649
1650static int wpa_config_parse_wep_key(u8 *key, size_t *len, int line,
1651 const char *value, int idx)
1652{
1653 char *buf, title[20];
1654 int res;
1655
1656 buf = wpa_config_parse_string(value, len);
1657 if (buf == NULL) {
1658 wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key %d '%s'.",
1659 line, idx, value);
1660 return -1;
1661 }
1662 if (*len > MAX_WEP_KEY_LEN) {
1663 wpa_printf(MSG_ERROR, "Line %d: Too long WEP key %d '%s'.",
1664 line, idx, value);
1665 os_free(buf);
1666 return -1;
1667 }
fea7c3a0
JM
1668 if (*len && *len != 5 && *len != 13 && *len != 16) {
1669 wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key length %u - "
1670 "this network block will be ignored",
1671 line, (unsigned int) *len);
1672 }
6fc6879b 1673 os_memcpy(key, buf, *len);
19c48da0 1674 str_clear_free(buf);
6fc6879b 1675 res = os_snprintf(title, sizeof(title), "wep_key%d", idx);
a80ba67a 1676 if (!os_snprintf_error(sizeof(title), res))
6fc6879b
JM
1677 wpa_hexdump_key(MSG_MSGDUMP, title, key, *len);
1678 return 0;
1679}
1680
1681
1682static int wpa_config_parse_wep_key0(const struct parse_data *data,
1683 struct wpa_ssid *ssid, int line,
1684 const char *value)
1685{
1686 return wpa_config_parse_wep_key(ssid->wep_key[0],
1687 &ssid->wep_key_len[0], line,
1688 value, 0);
1689}
1690
1691
1692static int wpa_config_parse_wep_key1(const struct parse_data *data,
1693 struct wpa_ssid *ssid, int line,
1694 const char *value)
1695{
1696 return wpa_config_parse_wep_key(ssid->wep_key[1],
1697 &ssid->wep_key_len[1], line,
1698 value, 1);
1699}
1700
1701
1702static int wpa_config_parse_wep_key2(const struct parse_data *data,
1703 struct wpa_ssid *ssid, int line,
1704 const char *value)
1705{
1706 return wpa_config_parse_wep_key(ssid->wep_key[2],
1707 &ssid->wep_key_len[2], line,
1708 value, 2);
1709}
1710
1711
1712static int wpa_config_parse_wep_key3(const struct parse_data *data,
1713 struct wpa_ssid *ssid, int line,
1714 const char *value)
1715{
1716 return wpa_config_parse_wep_key(ssid->wep_key[3],
1717 &ssid->wep_key_len[3], line,
1718 value, 3);
1719}
1720
1721
1722#ifndef NO_CONFIG_WRITE
1723static char * wpa_config_write_wep_key(struct wpa_ssid *ssid, int idx)
1724{
1725 if (ssid->wep_key_len[idx] == 0)
1726 return NULL;
1727 return wpa_config_write_string(ssid->wep_key[idx],
1728 ssid->wep_key_len[idx]);
1729}
1730
1731
1732static char * wpa_config_write_wep_key0(const struct parse_data *data,
1733 struct wpa_ssid *ssid)
1734{
1735 return wpa_config_write_wep_key(ssid, 0);
1736}
1737
1738
1739static char * wpa_config_write_wep_key1(const struct parse_data *data,
1740 struct wpa_ssid *ssid)
1741{
1742 return wpa_config_write_wep_key(ssid, 1);
1743}
1744
1745
1746static char * wpa_config_write_wep_key2(const struct parse_data *data,
1747 struct wpa_ssid *ssid)
1748{
1749 return wpa_config_write_wep_key(ssid, 2);
1750}
1751
1752
1753static char * wpa_config_write_wep_key3(const struct parse_data *data,
1754 struct wpa_ssid *ssid)
1755{
1756 return wpa_config_write_wep_key(ssid, 3);
1757}
1758#endif /* NO_CONFIG_WRITE */
1759
1760
fbdcfd57
JM
1761#ifdef CONFIG_P2P
1762
9ec87666
JM
1763static int wpa_config_parse_go_p2p_dev_addr(const struct parse_data *data,
1764 struct wpa_ssid *ssid, int line,
1765 const char *value)
1766{
1767 if (value[0] == '\0' || os_strcmp(value, "\"\"") == 0 ||
1768 os_strcmp(value, "any") == 0) {
1769 os_memset(ssid->go_p2p_dev_addr, 0, ETH_ALEN);
1770 wpa_printf(MSG_MSGDUMP, "GO P2P Device Address any");
1771 return 0;
1772 }
1773 if (hwaddr_aton(value, ssid->go_p2p_dev_addr)) {
1774 wpa_printf(MSG_ERROR, "Line %d: Invalid GO P2P Device Address '%s'.",
1775 line, value);
1776 return -1;
1777 }
1778 ssid->bssid_set = 1;
1779 wpa_printf(MSG_MSGDUMP, "GO P2P Device Address " MACSTR,
1780 MAC2STR(ssid->go_p2p_dev_addr));
1781 return 0;
1782}
1783
1784
1785#ifndef NO_CONFIG_WRITE
1786static char * wpa_config_write_go_p2p_dev_addr(const struct parse_data *data,
1787 struct wpa_ssid *ssid)
1788{
1789 char *value;
1790 int res;
1791
1792 if (is_zero_ether_addr(ssid->go_p2p_dev_addr))
1793 return NULL;
1794
1795 value = os_malloc(20);
1796 if (value == NULL)
1797 return NULL;
1798 res = os_snprintf(value, 20, MACSTR, MAC2STR(ssid->go_p2p_dev_addr));
d85e1fc8 1799 if (os_snprintf_error(20, res)) {
9ec87666
JM
1800 os_free(value);
1801 return NULL;
1802 }
1803 value[20 - 1] = '\0';
1804 return value;
1805}
1806#endif /* NO_CONFIG_WRITE */
1807
1808
fbdcfd57
JM
1809static int wpa_config_parse_p2p_client_list(const struct parse_data *data,
1810 struct wpa_ssid *ssid, int line,
1811 const char *value)
1812{
b3d6a0a8
ST
1813 return wpa_config_parse_addr_list(data, line, value,
1814 &ssid->p2p_client_list,
1815 &ssid->num_p2p_clients,
79cd993a 1816 "p2p_client_list", 0, 0);
fbdcfd57
JM
1817}
1818
1819
1820#ifndef NO_CONFIG_WRITE
1821static char * wpa_config_write_p2p_client_list(const struct parse_data *data,
1822 struct wpa_ssid *ssid)
1823{
b3d6a0a8
ST
1824 return wpa_config_write_addr_list(data, ssid->p2p_client_list,
1825 ssid->num_p2p_clients,
1826 "p2p_client_list");
fbdcfd57
JM
1827}
1828#endif /* NO_CONFIG_WRITE */
1829
01a57fe4
JM
1830
1831static int wpa_config_parse_psk_list(const struct parse_data *data,
1832 struct wpa_ssid *ssid, int line,
1833 const char *value)
1834{
1835 struct psk_list_entry *p;
1836 const char *pos;
1837
1838 p = os_zalloc(sizeof(*p));
1839 if (p == NULL)
1840 return -1;
1841
1842 pos = value;
1843 if (os_strncmp(pos, "P2P-", 4) == 0) {
1844 p->p2p = 1;
1845 pos += 4;
1846 }
1847
1848 if (hwaddr_aton(pos, p->addr)) {
1849 wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list address '%s'",
1850 line, pos);
1851 os_free(p);
1852 return -1;
1853 }
1854 pos += 17;
1855 if (*pos != '-') {
1856 wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list '%s'",
1857 line, pos);
1858 os_free(p);
1859 return -1;
1860 }
1861 pos++;
1862
1863 if (hexstr2bin(pos, p->psk, PMK_LEN) || pos[PMK_LEN * 2] != '\0') {
1864 wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list PSK '%s'",
1865 line, pos);
1866 os_free(p);
1867 return -1;
1868 }
1869
1870 dl_list_add(&ssid->psk_list, &p->list);
1871
1872 return 0;
1873}
1874
1875
1876#ifndef NO_CONFIG_WRITE
1877static char * wpa_config_write_psk_list(const struct parse_data *data,
1878 struct wpa_ssid *ssid)
1879{
1880 return NULL;
1881}
1882#endif /* NO_CONFIG_WRITE */
1883
fbdcfd57
JM
1884#endif /* CONFIG_P2P */
1885
5cfb672d
JM
1886
1887#ifdef CONFIG_MESH
1888
2b2bb5a8
MH
1889static int wpa_config_parse_mesh_basic_rates(const struct parse_data *data,
1890 struct wpa_ssid *ssid, int line,
1891 const char *value)
1892{
1893 int *rates = wpa_config_parse_int_array(value);
1894
1895 if (rates == NULL) {
1896 wpa_printf(MSG_ERROR, "Line %d: Invalid mesh_basic_rates '%s'",
1897 line, value);
1898 return -1;
1899 }
1900 if (rates[0] == 0) {
1901 os_free(rates);
1902 rates = NULL;
1903 }
1904
1905 os_free(ssid->mesh_basic_rates);
1906 ssid->mesh_basic_rates = rates;
1907
1908 return 0;
1909}
1910
1911
5cfb672d 1912#ifndef NO_CONFIG_WRITE
2b2bb5a8 1913
2b2bb5a8
MH
1914static char * wpa_config_write_mesh_basic_rates(const struct parse_data *data,
1915 struct wpa_ssid *ssid)
1916{
1917 return wpa_config_write_freqs(data, ssid->mesh_basic_rates);
1918}
1919
5cfb672d
JM
1920#endif /* NO_CONFIG_WRITE */
1921
1922#endif /* CONFIG_MESH */
1923
1924
ad51731a
SD
1925#ifdef CONFIG_MACSEC
1926
1927static int wpa_config_parse_mka_cak(const struct parse_data *data,
1928 struct wpa_ssid *ssid, int line,
1929 const char *value)
1930{
1931 if (hexstr2bin(value, ssid->mka_cak, MACSEC_CAK_LEN) ||
1932 value[MACSEC_CAK_LEN * 2] != '\0') {
1933 wpa_printf(MSG_ERROR, "Line %d: Invalid MKA-CAK '%s'.",
1934 line, value);
1935 return -1;
1936 }
1937
1938 ssid->mka_psk_set |= MKA_PSK_SET_CAK;
1939
1940 wpa_hexdump_key(MSG_MSGDUMP, "MKA-CAK", ssid->mka_cak, MACSEC_CAK_LEN);
1941 return 0;
1942}
1943
1944
1945static int wpa_config_parse_mka_ckn(const struct parse_data *data,
1946 struct wpa_ssid *ssid, int line,
1947 const char *value)
1948{
1949 if (hexstr2bin(value, ssid->mka_ckn, MACSEC_CKN_LEN) ||
1950 value[MACSEC_CKN_LEN * 2] != '\0') {
1951 wpa_printf(MSG_ERROR, "Line %d: Invalid MKA-CKN '%s'.",
1952 line, value);
1953 return -1;
1954 }
1955
1956 ssid->mka_psk_set |= MKA_PSK_SET_CKN;
1957
1958 wpa_hexdump_key(MSG_MSGDUMP, "MKA-CKN", ssid->mka_ckn, MACSEC_CKN_LEN);
1959 return 0;
1960}
1961
1962
1963#ifndef NO_CONFIG_WRITE
1964
1965static char * wpa_config_write_mka_cak(const struct parse_data *data,
1966 struct wpa_ssid *ssid)
1967{
1968 if (!(ssid->mka_psk_set & MKA_PSK_SET_CAK))
1969 return NULL;
1970
1971 return wpa_config_write_string_hex(ssid->mka_cak, MACSEC_CAK_LEN);
1972}
1973
1974
1975static char * wpa_config_write_mka_ckn(const struct parse_data *data,
1976 struct wpa_ssid *ssid)
1977{
1978 if (!(ssid->mka_psk_set & MKA_PSK_SET_CKN))
1979 return NULL;
1980 return wpa_config_write_string_hex(ssid->mka_ckn, MACSEC_CKN_LEN);
1981}
1982
1983#endif /* NO_CONFIG_WRITE */
1984
1985#endif /* CONFIG_MACSEC */
1986
1987
6fc6879b
JM
1988/* Helper macros for network block parser */
1989
1990#ifdef OFFSET
1991#undef OFFSET
1992#endif /* OFFSET */
1993/* OFFSET: Get offset of a variable within the wpa_ssid structure */
1994#define OFFSET(v) ((void *) &((struct wpa_ssid *) 0)->v)
1995
1996/* STR: Define a string variable for an ASCII string; f = field name */
1997#ifdef NO_CONFIG_WRITE
1998#define _STR(f) #f, wpa_config_parse_str, OFFSET(f)
1999#define _STRe(f) #f, wpa_config_parse_str, OFFSET(eap.f)
2000#else /* NO_CONFIG_WRITE */
2001#define _STR(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(f)
2002#define _STRe(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(eap.f)
2003#endif /* NO_CONFIG_WRITE */
2004#define STR(f) _STR(f), NULL, NULL, NULL, 0
2005#define STRe(f) _STRe(f), NULL, NULL, NULL, 0
2006#define STR_KEY(f) _STR(f), NULL, NULL, NULL, 1
2007#define STR_KEYe(f) _STRe(f), NULL, NULL, NULL, 1
2008
2009/* STR_LEN: Define a string variable with a separate variable for storing the
2010 * data length. Unlike STR(), this can be used to store arbitrary binary data
2011 * (i.e., even nul termination character). */
2012#define _STR_LEN(f) _STR(f), OFFSET(f ## _len)
2013#define _STR_LENe(f) _STRe(f), OFFSET(eap.f ## _len)
2014#define STR_LEN(f) _STR_LEN(f), NULL, NULL, 0
2015#define STR_LENe(f) _STR_LENe(f), NULL, NULL, 0
2016#define STR_LEN_KEY(f) _STR_LEN(f), NULL, NULL, 1
2017
2018/* STR_RANGE: Like STR_LEN(), but with minimum and maximum allowed length
2019 * explicitly specified. */
2020#define _STR_RANGE(f, min, max) _STR_LEN(f), (void *) (min), (void *) (max)
2021#define STR_RANGE(f, min, max) _STR_RANGE(f, min, max), 0
2022#define STR_RANGE_KEY(f, min, max) _STR_RANGE(f, min, max), 1
2023
2024#ifdef NO_CONFIG_WRITE
2025#define _INT(f) #f, wpa_config_parse_int, OFFSET(f), (void *) 0
2026#define _INTe(f) #f, wpa_config_parse_int, OFFSET(eap.f), (void *) 0
2027#else /* NO_CONFIG_WRITE */
2028#define _INT(f) #f, wpa_config_parse_int, wpa_config_write_int, \
2029 OFFSET(f), (void *) 0
2030#define _INTe(f) #f, wpa_config_parse_int, wpa_config_write_int, \
2031 OFFSET(eap.f), (void *) 0
2032#endif /* NO_CONFIG_WRITE */
2033
2034/* INT: Define an integer variable */
2035#define INT(f) _INT(f), NULL, NULL, 0
2036#define INTe(f) _INTe(f), NULL, NULL, 0
2037
2038/* INT_RANGE: Define an integer variable with allowed value range */
2039#define INT_RANGE(f, min, max) _INT(f), (void *) (min), (void *) (max), 0
2040
2041/* FUNC: Define a configuration variable that uses a custom function for
2042 * parsing and writing the value. */
2043#ifdef NO_CONFIG_WRITE
2044#define _FUNC(f) #f, wpa_config_parse_ ## f, NULL, NULL, NULL, NULL
2045#else /* NO_CONFIG_WRITE */
2046#define _FUNC(f) #f, wpa_config_parse_ ## f, wpa_config_write_ ## f, \
2047 NULL, NULL, NULL, NULL
2048#endif /* NO_CONFIG_WRITE */
2049#define FUNC(f) _FUNC(f), 0
2050#define FUNC_KEY(f) _FUNC(f), 1
2051
2052/*
2053 * Table of network configuration variables. This table is used to parse each
2054 * network configuration variable, e.g., each line in wpa_supplicant.conf file
2055 * that is inside a network block.
2056 *
2057 * This table is generated using the helper macros defined above and with
2058 * generous help from the C pre-processor. The field name is stored as a string
2059 * into .name and for STR and INT types, the offset of the target buffer within
2060 * struct wpa_ssid is stored in .param1. .param2 (if not NULL) is similar
2061 * offset to the field containing the length of the configuration variable.
2062 * .param3 and .param4 can be used to mark the allowed range (length for STR
2063 * and value for INT).
2064 *
2065 * For each configuration line in wpa_supplicant.conf, the parser goes through
2066 * this table and select the entry that matches with the field name. The parser
2067 * function (.parser) is then called to parse the actual value of the field.
2068 *
2069 * This kind of mechanism makes it easy to add new configuration parameters,
2070 * since only one line needs to be added into this table and into the
2071 * struct wpa_ssid definition if the new variable is either a string or
2072 * integer. More complex types will need to use their own parser and writer
2073 * functions.
2074 */
2075static const struct parse_data ssid_fields[] = {
eaa8eefe 2076 { STR_RANGE(ssid, 0, SSID_MAX_LEN) },
6fc6879b
JM
2077 { INT_RANGE(scan_ssid, 0, 1) },
2078 { FUNC(bssid) },
43a356b2 2079 { FUNC(bssid_hint) },
b83e4554
ST
2080 { FUNC(bssid_blacklist) },
2081 { FUNC(bssid_whitelist) },
6fc6879b 2082 { FUNC_KEY(psk) },
a52410c2 2083 { INT(mem_only_psk) },
6fc6879b
JM
2084 { FUNC(proto) },
2085 { FUNC(key_mgmt) },
1f6c0ab8 2086 { INT(bg_scan_period) },
6fc6879b
JM
2087 { FUNC(pairwise) },
2088 { FUNC(group) },
2089 { FUNC(auth_alg) },
d3a98225 2090 { FUNC(scan_freq) },
b766a9a2 2091 { FUNC(freq_list) },
b07ff9cb 2092 { INT_RANGE(ht, 0, 1) },
2124a615
JB
2093 { INT_RANGE(vht, 0, 1) },
2094 { INT_RANGE(ht40, -1, 1) },
0f29bc68
AK
2095 { INT_RANGE(max_oper_chwidth, VHT_CHANWIDTH_USE_HT,
2096 VHT_CHANWIDTH_80P80MHZ) },
2124a615
JB
2097 { INT(vht_center_freq1) },
2098 { INT(vht_center_freq2) },
6fc6879b
JM
2099#ifdef IEEE8021X_EAPOL
2100 { FUNC(eap) },
2101 { STR_LENe(identity) },
2102 { STR_LENe(anonymous_identity) },
556f5a2a 2103 { FUNC_KEY(password) },
6fc6879b
JM
2104 { STRe(ca_cert) },
2105 { STRe(ca_path) },
2106 { STRe(client_cert) },
2107 { STRe(private_key) },
2108 { STR_KEYe(private_key_passwd) },
2109 { STRe(dh_file) },
2110 { STRe(subject_match) },
2111 { STRe(altsubject_match) },
01f809c7 2112 { STRe(domain_suffix_match) },
cebee30f 2113 { STRe(domain_match) },
6fc6879b
JM
2114 { STRe(ca_cert2) },
2115 { STRe(ca_path2) },
2116 { STRe(client_cert2) },
2117 { STRe(private_key2) },
2118 { STR_KEYe(private_key2_passwd) },
2119 { STRe(dh_file2) },
2120 { STRe(subject_match2) },
2121 { STRe(altsubject_match2) },
01f809c7 2122 { STRe(domain_suffix_match2) },
cebee30f 2123 { STRe(domain_match2) },
6fc6879b
JM
2124 { STRe(phase1) },
2125 { STRe(phase2) },
2126 { STRe(pcsc) },
2127 { STR_KEYe(pin) },
2128 { STRe(engine_id) },
2129 { STRe(key_id) },
61ee0f71
DS
2130 { STRe(cert_id) },
2131 { STRe(ca_cert_id) },
98842d51
CL
2132 { STR_KEYe(pin2) },
2133 { STRe(engine2_id) },
61ee0f71
DS
2134 { STRe(key2_id) },
2135 { STRe(cert2_id) },
2136 { STRe(ca_cert2_id) },
6fc6879b 2137 { INTe(engine) },
98842d51 2138 { INTe(engine2) },
6fc6879b 2139 { INT(eapol_flags) },
13f6a07e 2140 { INTe(sim_num) },
07e2de31 2141 { STRe(openssl_ciphers) },
02a8d45a 2142 { INTe(erp) },
6fc6879b
JM
2143#endif /* IEEE8021X_EAPOL */
2144 { FUNC_KEY(wep_key0) },
2145 { FUNC_KEY(wep_key1) },
2146 { FUNC_KEY(wep_key2) },
2147 { FUNC_KEY(wep_key3) },
2148 { INT(wep_tx_keyidx) },
2149 { INT(priority) },
2150#ifdef IEEE8021X_EAPOL
2151 { INT(eap_workaround) },
2152 { STRe(pac_file) },
2153 { INTe(fragment_size) },
080585c0 2154 { INTe(ocsp) },
6fc6879b 2155#endif /* IEEE8021X_EAPOL */
476e6bb6
TP
2156#ifdef CONFIG_MESH
2157 { INT_RANGE(mode, 0, 5) },
07cb45cc 2158 { INT_RANGE(no_auto_peer, 0, 1) },
31a856a1 2159 { INT_RANGE(mesh_rssi_threshold, -255, 1) },
476e6bb6 2160#else /* CONFIG_MESH */
2c5d725c 2161 { INT_RANGE(mode, 0, 4) },
476e6bb6 2162#endif /* CONFIG_MESH */
6fc6879b 2163 { INT_RANGE(proactive_key_caching, 0, 1) },
4dac0245 2164 { INT_RANGE(disabled, 0, 2) },
6fc6879b
JM
2165 { STR(id_str) },
2166#ifdef CONFIG_IEEE80211W
2167 { INT_RANGE(ieee80211w, 0, 2) },
2168#endif /* CONFIG_IEEE80211W */
2169 { INT_RANGE(peerkey, 0, 1) },
2170 { INT_RANGE(mixed_cell, 0, 1) },
7829894c 2171 { INT_RANGE(frequency, 0, 65000) },
4d9e6fba 2172 { INT_RANGE(fixed_freq, 0, 1) },
d9909717
TB
2173#ifdef CONFIG_ACS
2174 { INT_RANGE(acs, 0, 1) },
2175#endif /* CONFIG_ACS */
5cfb672d 2176#ifdef CONFIG_MESH
2b2bb5a8 2177 { FUNC(mesh_basic_rates) },
e6096799
MH
2178 { INT(dot11MeshMaxRetries) },
2179 { INT(dot11MeshRetryTimeout) },
2180 { INT(dot11MeshConfirmTimeout) },
2181 { INT(dot11MeshHoldingTimeout) },
5cfb672d 2182#endif /* CONFIG_MESH */
60b94c98 2183 { INT(wpa_ptk_rekey) },
6c33ca9f 2184 { INT(group_rekey) },
60b94c98 2185 { STR(bgscan) },
e62f4ed0 2186 { INT_RANGE(ignore_broadcast_ssid, 0, 2) },
fbdcfd57 2187#ifdef CONFIG_P2P
9ec87666 2188 { FUNC(go_p2p_dev_addr) },
fbdcfd57 2189 { FUNC(p2p_client_list) },
01a57fe4 2190 { FUNC(psk_list) },
fbdcfd57 2191#endif /* CONFIG_P2P */
80e8a5ee
BG
2192#ifdef CONFIG_HT_OVERRIDES
2193 { INT_RANGE(disable_ht, 0, 1) },
2194 { INT_RANGE(disable_ht40, -1, 1) },
a90497f8 2195 { INT_RANGE(disable_sgi, 0, 1) },
39a5800f 2196 { INT_RANGE(disable_ldpc, 0, 1) },
d41cc8cc 2197 { INT_RANGE(ht40_intolerant, 0, 1) },
80e8a5ee
BG
2198 { INT_RANGE(disable_max_amsdu, -1, 1) },
2199 { INT_RANGE(ampdu_factor, -1, 3) },
2200 { INT_RANGE(ampdu_density, -1, 7) },
2201 { STR(ht_mcs) },
2202#endif /* CONFIG_HT_OVERRIDES */
e9ee8dc3
JB
2203#ifdef CONFIG_VHT_OVERRIDES
2204 { INT_RANGE(disable_vht, 0, 1) },
2205 { INT(vht_capa) },
2206 { INT(vht_capa_mask) },
2207 { INT_RANGE(vht_rx_mcs_nss_1, -1, 3) },
2208 { INT_RANGE(vht_rx_mcs_nss_2, -1, 3) },
2209 { INT_RANGE(vht_rx_mcs_nss_3, -1, 3) },
2210 { INT_RANGE(vht_rx_mcs_nss_4, -1, 3) },
2211 { INT_RANGE(vht_rx_mcs_nss_5, -1, 3) },
2212 { INT_RANGE(vht_rx_mcs_nss_6, -1, 3) },
2213 { INT_RANGE(vht_rx_mcs_nss_7, -1, 3) },
2214 { INT_RANGE(vht_rx_mcs_nss_8, -1, 3) },
2215 { INT_RANGE(vht_tx_mcs_nss_1, -1, 3) },
2216 { INT_RANGE(vht_tx_mcs_nss_2, -1, 3) },
2217 { INT_RANGE(vht_tx_mcs_nss_3, -1, 3) },
2218 { INT_RANGE(vht_tx_mcs_nss_4, -1, 3) },
2219 { INT_RANGE(vht_tx_mcs_nss_5, -1, 3) },
2220 { INT_RANGE(vht_tx_mcs_nss_6, -1, 3) },
2221 { INT_RANGE(vht_tx_mcs_nss_7, -1, 3) },
2222 { INT_RANGE(vht_tx_mcs_nss_8, -1, 3) },
2223#endif /* CONFIG_VHT_OVERRIDES */
07f53b8c 2224 { INT(ap_max_inactivity) },
fdfb1c8b 2225 { INT(dtim_period) },
18206e02 2226 { INT(beacon_int) },
dd10abcc
HW
2227#ifdef CONFIG_MACSEC
2228 { INT_RANGE(macsec_policy, 0, 1) },
7b4d546e 2229 { INT_RANGE(macsec_integ_only, 0, 1) },
e0d9fd34 2230 { INT_RANGE(macsec_port, 1, 65534) },
65dfa872 2231 { INT_RANGE(mka_priority, 0, 255) },
ad51731a
SD
2232 { FUNC_KEY(mka_cak) },
2233 { FUNC_KEY(mka_ckn) },
dd10abcc 2234#endif /* CONFIG_MACSEC */
e376290c
DS
2235#ifdef CONFIG_HS20
2236 { INT(update_identifier) },
2237#endif /* CONFIG_HS20 */
a313d17d 2238 { INT_RANGE(mac_addr, 0, 2) },
90f14962 2239 { INT_RANGE(pbss, 0, 2) },
b6317b41 2240 { INT_RANGE(wps_disabled, 0, 1) },
76e20f4f 2241 { INT_RANGE(fils_dh_group, 0, 65535) },
b979caae
JM
2242#ifdef CONFIG_DPP
2243 { STR(dpp_connector) },
2244 { STR_LEN(dpp_netaccesskey) },
2245 { INT(dpp_netaccesskey_expiry) },
2246 { STR_LEN(dpp_csign) },
2247 { INT(dpp_csign_expiry) },
2248#endif /* CONFIG_DPP */
6fc6879b
JM
2249};
2250
2251#undef OFFSET
2252#undef _STR
2253#undef STR
2254#undef STR_KEY
2255#undef _STR_LEN
2256#undef STR_LEN
2257#undef STR_LEN_KEY
2258#undef _STR_RANGE
2259#undef STR_RANGE
2260#undef STR_RANGE_KEY
2261#undef _INT
2262#undef INT
2263#undef INT_RANGE
2264#undef _FUNC
2265#undef FUNC
2266#undef FUNC_KEY
e7ecab4a 2267#define NUM_SSID_FIELDS ARRAY_SIZE(ssid_fields)
6fc6879b
JM
2268
2269
2270/**
2271 * wpa_config_add_prio_network - Add a network to priority lists
2272 * @config: Configuration data from wpa_config_read()
2273 * @ssid: Pointer to the network configuration to be added to the list
2274 * Returns: 0 on success, -1 on failure
2275 *
2276 * This function is used to add a network block to the priority list of
2277 * networks. This must be called for each network when reading in the full
2278 * configuration. In addition, this can be used indirectly when updating
2279 * priorities by calling wpa_config_update_prio_list().
2280 */
2281int wpa_config_add_prio_network(struct wpa_config *config,
2282 struct wpa_ssid *ssid)
2283{
2284 int prio;
2285 struct wpa_ssid *prev, **nlist;
2286
2287 /*
2288 * Add to an existing priority list if one is available for the
2289 * configured priority level for this network.
2290 */
2291 for (prio = 0; prio < config->num_prio; prio++) {
2292 prev = config->pssid[prio];
2293 if (prev->priority == ssid->priority) {
2294 while (prev->pnext)
2295 prev = prev->pnext;
2296 prev->pnext = ssid;
2297 return 0;
2298 }
2299 }
2300
2301 /* First network for this priority - add a new priority list */
067ffa26
JM
2302 nlist = os_realloc_array(config->pssid, config->num_prio + 1,
2303 sizeof(struct wpa_ssid *));
6fc6879b
JM
2304 if (nlist == NULL)
2305 return -1;
2306
2307 for (prio = 0; prio < config->num_prio; prio++) {
2c60ca73
JM
2308 if (nlist[prio]->priority < ssid->priority) {
2309 os_memmove(&nlist[prio + 1], &nlist[prio],
2310 (config->num_prio - prio) *
2311 sizeof(struct wpa_ssid *));
6fc6879b 2312 break;
2c60ca73 2313 }
6fc6879b
JM
2314 }
2315
6fc6879b
JM
2316 nlist[prio] = ssid;
2317 config->num_prio++;
2318 config->pssid = nlist;
2319
2320 return 0;
2321}
2322
2323
2324/**
2325 * wpa_config_update_prio_list - Update network priority list
2326 * @config: Configuration data from wpa_config_read()
2327 * Returns: 0 on success, -1 on failure
2328 *
2329 * This function is called to update the priority list of networks in the
2330 * configuration when a network is being added or removed. This is also called
2331 * if a priority for a network is changed.
2332 */
aa53509f 2333int wpa_config_update_prio_list(struct wpa_config *config)
6fc6879b
JM
2334{
2335 struct wpa_ssid *ssid;
2336 int ret = 0;
2337
2338 os_free(config->pssid);
2339 config->pssid = NULL;
2340 config->num_prio = 0;
2341
2342 ssid = config->ssid;
2343 while (ssid) {
2344 ssid->pnext = NULL;
2345 if (wpa_config_add_prio_network(config, ssid) < 0)
2346 ret = -1;
2347 ssid = ssid->next;
2348 }
2349
2350 return ret;
2351}
2352
2353
2354#ifdef IEEE8021X_EAPOL
2355static void eap_peer_config_free(struct eap_peer_config *eap)
2356{
2357 os_free(eap->eap_methods);
19c48da0 2358 bin_clear_free(eap->identity, eap->identity_len);
6fc6879b 2359 os_free(eap->anonymous_identity);
19c48da0 2360 bin_clear_free(eap->password, eap->password_len);
6fc6879b
JM
2361 os_free(eap->ca_cert);
2362 os_free(eap->ca_path);
2363 os_free(eap->client_cert);
2364 os_free(eap->private_key);
19c48da0 2365 str_clear_free(eap->private_key_passwd);
6fc6879b
JM
2366 os_free(eap->dh_file);
2367 os_free(eap->subject_match);
2368 os_free(eap->altsubject_match);
01f809c7 2369 os_free(eap->domain_suffix_match);
cebee30f 2370 os_free(eap->domain_match);
6fc6879b
JM
2371 os_free(eap->ca_cert2);
2372 os_free(eap->ca_path2);
2373 os_free(eap->client_cert2);
2374 os_free(eap->private_key2);
19c48da0 2375 str_clear_free(eap->private_key2_passwd);
6fc6879b
JM
2376 os_free(eap->dh_file2);
2377 os_free(eap->subject_match2);
2378 os_free(eap->altsubject_match2);
01f809c7 2379 os_free(eap->domain_suffix_match2);
cebee30f 2380 os_free(eap->domain_match2);
6fc6879b
JM
2381 os_free(eap->phase1);
2382 os_free(eap->phase2);
2383 os_free(eap->pcsc);
19c48da0 2384 str_clear_free(eap->pin);
6fc6879b
JM
2385 os_free(eap->engine_id);
2386 os_free(eap->key_id);
61ee0f71
DS
2387 os_free(eap->cert_id);
2388 os_free(eap->ca_cert_id);
2389 os_free(eap->key2_id);
2390 os_free(eap->cert2_id);
2391 os_free(eap->ca_cert2_id);
19c48da0 2392 str_clear_free(eap->pin2);
98842d51 2393 os_free(eap->engine2_id);
6fc6879b
JM
2394 os_free(eap->otp);
2395 os_free(eap->pending_req_otp);
2396 os_free(eap->pac_file);
19c48da0
JM
2397 bin_clear_free(eap->new_password, eap->new_password_len);
2398 str_clear_free(eap->external_sim_resp);
07e2de31 2399 os_free(eap->openssl_ciphers);
6fc6879b
JM
2400}
2401#endif /* IEEE8021X_EAPOL */
2402
2403
2404/**
2405 * wpa_config_free_ssid - Free network/ssid configuration data
2406 * @ssid: Configuration data for the network
2407 *
2408 * This function frees all resources allocated for the network configuration
2409 * data.
2410 */
2411void wpa_config_free_ssid(struct wpa_ssid *ssid)
2412{
01a57fe4
JM
2413 struct psk_list_entry *psk;
2414
6fc6879b 2415 os_free(ssid->ssid);
19c48da0 2416 str_clear_free(ssid->passphrase);
9173b16f 2417 os_free(ssid->ext_psk);
6fc6879b
JM
2418#ifdef IEEE8021X_EAPOL
2419 eap_peer_config_free(&ssid->eap);
2420#endif /* IEEE8021X_EAPOL */
2421 os_free(ssid->id_str);
d3a98225 2422 os_free(ssid->scan_freq);
b766a9a2 2423 os_free(ssid->freq_list);
60b94c98 2424 os_free(ssid->bgscan);
fbdcfd57 2425 os_free(ssid->p2p_client_list);
79cd993a
ST
2426 os_free(ssid->bssid_blacklist);
2427 os_free(ssid->bssid_whitelist);
80e8a5ee
BG
2428#ifdef CONFIG_HT_OVERRIDES
2429 os_free(ssid->ht_mcs);
2430#endif /* CONFIG_HT_OVERRIDES */
2b2bb5a8
MH
2431#ifdef CONFIG_MESH
2432 os_free(ssid->mesh_basic_rates);
2433#endif /* CONFIG_MESH */
b979caae
JM
2434 os_free(ssid->dpp_connector);
2435 bin_clear_free(ssid->dpp_netaccesskey, ssid->dpp_netaccesskey_len);
2436 os_free(ssid->dpp_csign);
01a57fe4
JM
2437 while ((psk = dl_list_first(&ssid->psk_list, struct psk_list_entry,
2438 list))) {
2439 dl_list_del(&psk->list);
6df19739 2440 bin_clear_free(psk, sizeof(*psk));
01a57fe4 2441 }
6df19739 2442 bin_clear_free(ssid, sizeof(*ssid));
6fc6879b
JM
2443}
2444
2445
1bb7b8e8
JM
2446void wpa_config_free_cred(struct wpa_cred *cred)
2447{
463c8ffb
JM
2448 size_t i;
2449
1bb7b8e8 2450 os_free(cred->realm);
19c48da0
JM
2451 str_clear_free(cred->username);
2452 str_clear_free(cred->password);
1bb7b8e8 2453 os_free(cred->ca_cert);
11e4f46a
JM
2454 os_free(cred->client_cert);
2455 os_free(cred->private_key);
19c48da0 2456 str_clear_free(cred->private_key_passwd);
1bb7b8e8 2457 os_free(cred->imsi);
19c48da0 2458 str_clear_free(cred->milenage);
463c8ffb
JM
2459 for (i = 0; i < cred->num_domain; i++)
2460 os_free(cred->domain[i]);
1bb7b8e8 2461 os_free(cred->domain);
ac1bc549 2462 os_free(cred->domain_suffix_match);
8ca93c59
JM
2463 os_free(cred->eap_method);
2464 os_free(cred->phase1);
2465 os_free(cred->phase2);
dbea8ac7 2466 os_free(cred->excluded_ssid);
bc00053c 2467 os_free(cred->roaming_partner);
aa26ba68 2468 os_free(cred->provisioning_sp);
33fb8c52
JM
2469 for (i = 0; i < cred->num_req_conn_capab; i++)
2470 os_free(cred->req_conn_capab_port[i]);
2471 os_free(cred->req_conn_capab_port);
2472 os_free(cred->req_conn_capab_proto);
1bb7b8e8
JM
2473 os_free(cred);
2474}
2475
2476
d9bb2821
JM
2477void wpa_config_flush_blobs(struct wpa_config *config)
2478{
2479#ifndef CONFIG_NO_CONFIG_BLOBS
2480 struct wpa_config_blob *blob, *prev;
2481
2482 blob = config->blobs;
2483 config->blobs = NULL;
2484 while (blob) {
2485 prev = blob;
2486 blob = blob->next;
2487 wpa_config_free_blob(prev);
2488 }
2489#endif /* CONFIG_NO_CONFIG_BLOBS */
2490}
2491
2492
6fc6879b
JM
2493/**
2494 * wpa_config_free - Free configuration data
2495 * @config: Configuration data from wpa_config_read()
2496 *
2497 * This function frees all resources allocated for the configuration data by
2498 * wpa_config_read().
2499 */
2500void wpa_config_free(struct wpa_config *config)
2501{
6fc6879b 2502 struct wpa_ssid *ssid, *prev = NULL;
1bb7b8e8 2503 struct wpa_cred *cred, *cprev;
54e06b4f 2504 int i;
e3768e7c 2505
6fc6879b
JM
2506 ssid = config->ssid;
2507 while (ssid) {
2508 prev = ssid;
2509 ssid = ssid->next;
2510 wpa_config_free_ssid(prev);
2511 }
2512
1bb7b8e8
JM
2513 cred = config->cred;
2514 while (cred) {
2515 cprev = cred;
2516 cred = cred->next;
2517 wpa_config_free_cred(cprev);
2518 }
2519
d9bb2821 2520 wpa_config_flush_blobs(config);
6fc6879b 2521
71dd3b78 2522 wpabuf_free(config->wps_vendor_ext_m1);
54e06b4f
JM
2523 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++)
2524 wpabuf_free(config->wps_vendor_ext[i]);
6fc6879b
JM
2525 os_free(config->ctrl_interface);
2526 os_free(config->ctrl_interface_group);
6fc6879b
JM
2527 os_free(config->opensc_engine_path);
2528 os_free(config->pkcs11_engine_path);
2529 os_free(config->pkcs11_module_path);
07e2de31 2530 os_free(config->openssl_ciphers);
f64adcd7 2531 os_free(config->pcsc_reader);
19c48da0 2532 str_clear_free(config->pcsc_pin);
6fc6879b 2533 os_free(config->driver_param);
3c0b7aa4
JM
2534 os_free(config->device_name);
2535 os_free(config->manufacturer);
2536 os_free(config->model_name);
2537 os_free(config->model_number);
2538 os_free(config->serial_number);
c0e4dd9e 2539 os_free(config->config_methods);
e3768e7c 2540 os_free(config->p2p_ssid_postfix);
6fc6879b 2541 os_free(config->pssid);
21d996f7 2542 os_free(config->p2p_pref_chan);
556b30da 2543 os_free(config->p2p_no_go_freq.range);
b0786fba 2544 os_free(config->autoscan);
f5ffc348 2545 os_free(config->freq_list);
3f2c8ba6
JM
2546 wpabuf_free(config->wps_nfc_dh_pubkey);
2547 wpabuf_free(config->wps_nfc_dh_privkey);
2548 wpabuf_free(config->wps_nfc_dev_pw);
306ae225 2549 os_free(config->ext_password_backend);
625f202a 2550 os_free(config->sae_groups);
18a2eaab 2551 wpabuf_free(config->ap_vendor_elements);
b572df86 2552 os_free(config->osu_dir);
dd09e424 2553 os_free(config->bgscan);
e4fa8b12 2554 os_free(config->wowlan_triggers);
76ca15b7 2555 os_free(config->fst_group_id);
32c02261 2556 os_free(config->sched_scan_plans);
facf2c72
DS
2557#ifdef CONFIG_MBO
2558 os_free(config->non_pref_chan);
2559#endif /* CONFIG_MBO */
32c02261 2560
6fc6879b
JM
2561 os_free(config);
2562}
2563
2564
7c49fdd0
SL
2565/**
2566 * wpa_config_foreach_network - Iterate over each configured network
2567 * @config: Configuration data from wpa_config_read()
2568 * @func: Callback function to process each network
2569 * @arg: Opaque argument to pass to callback function
2570 *
2571 * Iterate over the set of configured networks calling the specified
2572 * function for each item. We guard against callbacks removing the
2573 * supplied network.
2574 */
2575void wpa_config_foreach_network(struct wpa_config *config,
2576 void (*func)(void *, struct wpa_ssid *),
2577 void *arg)
2578{
2579 struct wpa_ssid *ssid, *next;
2580
2581 ssid = config->ssid;
2582 while (ssid) {
2583 next = ssid->next;
2584 func(arg, ssid);
2585 ssid = next;
2586 }
2587}
2588
2589
6fc6879b
JM
2590/**
2591 * wpa_config_get_network - Get configured network based on id
2592 * @config: Configuration data from wpa_config_read()
2593 * @id: Unique network id to search for
2594 * Returns: Network configuration or %NULL if not found
2595 */
2596struct wpa_ssid * wpa_config_get_network(struct wpa_config *config, int id)
2597{
2598 struct wpa_ssid *ssid;
2599
2600 ssid = config->ssid;
2601 while (ssid) {
2602 if (id == ssid->id)
2603 break;
2604 ssid = ssid->next;
2605 }
2606
2607 return ssid;
2608}
2609
2610
2611/**
2612 * wpa_config_add_network - Add a new network with empty configuration
2613 * @config: Configuration data from wpa_config_read()
2614 * Returns: The new network configuration or %NULL if operation failed
2615 */
2616struct wpa_ssid * wpa_config_add_network(struct wpa_config *config)
2617{
2618 int id;
2619 struct wpa_ssid *ssid, *last = NULL;
2620
2621 id = -1;
2622 ssid = config->ssid;
2623 while (ssid) {
2624 if (ssid->id > id)
2625 id = ssid->id;
2626 last = ssid;
2627 ssid = ssid->next;
2628 }
2629 id++;
2630
2631 ssid = os_zalloc(sizeof(*ssid));
2632 if (ssid == NULL)
2633 return NULL;
2634 ssid->id = id;
01a57fe4 2635 dl_list_init(&ssid->psk_list);
6fc6879b
JM
2636 if (last)
2637 last->next = ssid;
2638 else
2639 config->ssid = ssid;
2640
2641 wpa_config_update_prio_list(config);
2642
2643 return ssid;
2644}
2645
2646
2647/**
2648 * wpa_config_remove_network - Remove a configured network based on id
2649 * @config: Configuration data from wpa_config_read()
2650 * @id: Unique network id to search for
2651 * Returns: 0 on success, or -1 if the network was not found
2652 */
2653int wpa_config_remove_network(struct wpa_config *config, int id)
2654{
2655 struct wpa_ssid *ssid, *prev = NULL;
2656
2657 ssid = config->ssid;
2658 while (ssid) {
2659 if (id == ssid->id)
2660 break;
2661 prev = ssid;
2662 ssid = ssid->next;
2663 }
2664
2665 if (ssid == NULL)
2666 return -1;
2667
2668 if (prev)
2669 prev->next = ssid->next;
2670 else
2671 config->ssid = ssid->next;
2672
2673 wpa_config_update_prio_list(config);
2674 wpa_config_free_ssid(ssid);
2675 return 0;
2676}
2677
2678
2679/**
2680 * wpa_config_set_network_defaults - Set network default values
2681 * @ssid: Pointer to network configuration data
2682 */
2683void wpa_config_set_network_defaults(struct wpa_ssid *ssid)
2684{
2685 ssid->proto = DEFAULT_PROTO;
2686 ssid->pairwise_cipher = DEFAULT_PAIRWISE;
2687 ssid->group_cipher = DEFAULT_GROUP;
2688 ssid->key_mgmt = DEFAULT_KEY_MGMT;
1f6c0ab8 2689 ssid->bg_scan_period = DEFAULT_BG_SCAN_PERIOD;
b07ff9cb 2690 ssid->ht = 1;
6fc6879b
JM
2691#ifdef IEEE8021X_EAPOL
2692 ssid->eapol_flags = DEFAULT_EAPOL_FLAGS;
2693 ssid->eap_workaround = DEFAULT_EAP_WORKAROUND;
2694 ssid->eap.fragment_size = DEFAULT_FRAGMENT_SIZE;
13f6a07e 2695 ssid->eap.sim_num = DEFAULT_USER_SELECTED_SIM;
6fc6879b 2696#endif /* IEEE8021X_EAPOL */
5cfb672d 2697#ifdef CONFIG_MESH
e6096799
MH
2698 ssid->dot11MeshMaxRetries = DEFAULT_MESH_MAX_RETRIES;
2699 ssid->dot11MeshRetryTimeout = DEFAULT_MESH_RETRY_TIMEOUT;
2700 ssid->dot11MeshConfirmTimeout = DEFAULT_MESH_CONFIRM_TIMEOUT;
2701 ssid->dot11MeshHoldingTimeout = DEFAULT_MESH_HOLDING_TIMEOUT;
31a856a1 2702 ssid->mesh_rssi_threshold = DEFAULT_MESH_RSSI_THRESHOLD;
5cfb672d 2703#endif /* CONFIG_MESH */
80e8a5ee
BG
2704#ifdef CONFIG_HT_OVERRIDES
2705 ssid->disable_ht = DEFAULT_DISABLE_HT;
2706 ssid->disable_ht40 = DEFAULT_DISABLE_HT40;
a90497f8 2707 ssid->disable_sgi = DEFAULT_DISABLE_SGI;
39a5800f 2708 ssid->disable_ldpc = DEFAULT_DISABLE_LDPC;
80e8a5ee
BG
2709 ssid->disable_max_amsdu = DEFAULT_DISABLE_MAX_AMSDU;
2710 ssid->ampdu_factor = DEFAULT_AMPDU_FACTOR;
2711 ssid->ampdu_density = DEFAULT_AMPDU_DENSITY;
2712#endif /* CONFIG_HT_OVERRIDES */
e9ee8dc3
JB
2713#ifdef CONFIG_VHT_OVERRIDES
2714 ssid->vht_rx_mcs_nss_1 = -1;
2715 ssid->vht_rx_mcs_nss_2 = -1;
2716 ssid->vht_rx_mcs_nss_3 = -1;
2717 ssid->vht_rx_mcs_nss_4 = -1;
2718 ssid->vht_rx_mcs_nss_5 = -1;
2719 ssid->vht_rx_mcs_nss_6 = -1;
2720 ssid->vht_rx_mcs_nss_7 = -1;
2721 ssid->vht_rx_mcs_nss_8 = -1;
2722 ssid->vht_tx_mcs_nss_1 = -1;
2723 ssid->vht_tx_mcs_nss_2 = -1;
2724 ssid->vht_tx_mcs_nss_3 = -1;
2725 ssid->vht_tx_mcs_nss_4 = -1;
2726 ssid->vht_tx_mcs_nss_5 = -1;
2727 ssid->vht_tx_mcs_nss_6 = -1;
2728 ssid->vht_tx_mcs_nss_7 = -1;
2729 ssid->vht_tx_mcs_nss_8 = -1;
2730#endif /* CONFIG_VHT_OVERRIDES */
6e202021 2731 ssid->proactive_key_caching = -1;
62d49803
JM
2732#ifdef CONFIG_IEEE80211W
2733 ssid->ieee80211w = MGMT_FRAME_PROTECTION_DEFAULT;
2734#endif /* CONFIG_IEEE80211W */
65dfa872
BA
2735#ifdef CONFIG_MACSEC
2736 ssid->mka_priority = DEFAULT_PRIO_NOT_KEY_SERVER;
2737#endif /* CONFIG_MACSEC */
c267753b 2738 ssid->mac_addr = -1;
6fc6879b
JM
2739}
2740
2741
2742/**
2743 * wpa_config_set - Set a variable in network configuration
2744 * @ssid: Pointer to network configuration data
2745 * @var: Variable name, e.g., "ssid"
2746 * @value: Variable value
2747 * @line: Line number in configuration file or 0 if not used
5cd317d3
BKB
2748 * Returns: 0 on success with possible change in the value, 1 on success with
2749 * no change to previously configured value, or -1 on failure
6fc6879b
JM
2750 *
2751 * This function can be used to set network configuration variables based on
2752 * both the configuration file and management interface input. The value
2753 * parameter must be in the same format as the text-based configuration file is
2754 * using. For example, strings are using double quotation marks.
2755 */
2756int wpa_config_set(struct wpa_ssid *ssid, const char *var, const char *value,
2757 int line)
2758{
2759 size_t i;
2760 int ret = 0;
2761
2762 if (ssid == NULL || var == NULL || value == NULL)
2763 return -1;
2764
2765 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2766 const struct parse_data *field = &ssid_fields[i];
2767 if (os_strcmp(var, field->name) != 0)
2768 continue;
2769
5cd317d3
BKB
2770 ret = field->parser(field, ssid, line, value);
2771 if (ret < 0) {
6fc6879b
JM
2772 if (line) {
2773 wpa_printf(MSG_ERROR, "Line %d: failed to "
2774 "parse %s '%s'.", line, var, value);
2775 }
2776 ret = -1;
2777 }
2778 break;
2779 }
2780 if (i == NUM_SSID_FIELDS) {
2781 if (line) {
2782 wpa_printf(MSG_ERROR, "Line %d: unknown network field "
2783 "'%s'.", line, var);
2784 }
2785 ret = -1;
2786 }
2787
2788 return ret;
2789}
2790
2791
67e1b984
JM
2792int wpa_config_set_quoted(struct wpa_ssid *ssid, const char *var,
2793 const char *value)
2794{
2795 size_t len;
2796 char *buf;
2797 int ret;
2798
2799 len = os_strlen(value);
2800 buf = os_malloc(len + 3);
2801 if (buf == NULL)
2802 return -1;
2803 buf[0] = '"';
2804 os_memcpy(buf + 1, value, len);
2805 buf[len + 1] = '"';
2806 buf[len + 2] = '\0';
2807 ret = wpa_config_set(ssid, var, buf, 0);
2808 os_free(buf);
2809 return ret;
2810}
2811
2812
3d3d3056
WS
2813/**
2814 * wpa_config_get_all - Get all options from network configuration
2815 * @ssid: Pointer to network configuration data
2816 * @get_keys: Determines if keys/passwords will be included in returned list
d1c8ac88 2817 * (if they may be exported)
3d3d3056
WS
2818 * Returns: %NULL terminated list of all set keys and their values in the form
2819 * of [key1, val1, key2, val2, ... , NULL]
2820 *
2821 * This function can be used to get list of all configured network properties.
2822 * The caller is responsible for freeing the returned list and all its
2823 * elements.
2824 */
2825char ** wpa_config_get_all(struct wpa_ssid *ssid, int get_keys)
2826{
954f03aa
JM
2827#ifdef NO_CONFIG_WRITE
2828 return NULL;
2829#else /* NO_CONFIG_WRITE */
3d3d3056
WS
2830 const struct parse_data *field;
2831 char *key, *value;
2832 size_t i;
2833 char **props;
2834 int fields_num;
2835
d1c8ac88
JB
2836 get_keys = get_keys && ssid->export_keys;
2837
f9884c09 2838 props = os_calloc(2 * NUM_SSID_FIELDS + 1, sizeof(char *));
3d3d3056
WS
2839 if (!props)
2840 return NULL;
2841
2842 fields_num = 0;
2843 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2844 field = &ssid_fields[i];
2845 if (field->key_data && !get_keys)
2846 continue;
2847 value = field->writer(field, ssid);
04746865 2848 if (value == NULL)
3d3d3056 2849 continue;
04746865
JM
2850 if (os_strlen(value) == 0) {
2851 os_free(value);
2852 continue;
2853 }
3d3d3056
WS
2854
2855 key = os_strdup(field->name);
04746865
JM
2856 if (key == NULL) {
2857 os_free(value);
3d3d3056 2858 goto err;
04746865 2859 }
3d3d3056
WS
2860
2861 props[fields_num * 2] = key;
2862 props[fields_num * 2 + 1] = value;
2863
2864 fields_num++;
2865 }
2866
2867 return props;
2868
2869err:
8329ad4d
JM
2870 for (i = 0; props[i]; i++)
2871 os_free(props[i]);
3d3d3056
WS
2872 os_free(props);
2873 return NULL;
954f03aa 2874#endif /* NO_CONFIG_WRITE */
3d3d3056
WS
2875}
2876
2877
6fc6879b
JM
2878#ifndef NO_CONFIG_WRITE
2879/**
2880 * wpa_config_get - Get a variable in network configuration
2881 * @ssid: Pointer to network configuration data
2882 * @var: Variable name, e.g., "ssid"
2883 * Returns: Value of the variable or %NULL on failure
2884 *
2885 * This function can be used to get network configuration variables. The
2886 * returned value is a copy of the configuration variable in text format, i.e,.
2887 * the same format that the text-based configuration file and wpa_config_set()
2888 * are using for the value. The caller is responsible for freeing the returned
2889 * value.
2890 */
2891char * wpa_config_get(struct wpa_ssid *ssid, const char *var)
2892{
2893 size_t i;
2894
2895 if (ssid == NULL || var == NULL)
2896 return NULL;
2897
2898 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2899 const struct parse_data *field = &ssid_fields[i];
0fe5a234
PS
2900 if (os_strcmp(var, field->name) == 0) {
2901 char *ret = field->writer(field, ssid);
2902
2903 if (ret && has_newline(ret)) {
2904 wpa_printf(MSG_ERROR,
2905 "Found newline in value for %s; not returning it",
2906 var);
2907 os_free(ret);
2908 ret = NULL;
2909 }
2910
2911 return ret;
2912 }
6fc6879b
JM
2913 }
2914
2915 return NULL;
2916}
2917
2918
2919/**
2920 * wpa_config_get_no_key - Get a variable in network configuration (no keys)
2921 * @ssid: Pointer to network configuration data
2922 * @var: Variable name, e.g., "ssid"
2923 * Returns: Value of the variable or %NULL on failure
2924 *
2925 * This function can be used to get network configuration variable like
2926 * wpa_config_get(). The only difference is that this functions does not expose
2927 * key/password material from the configuration. In case a key/password field
2928 * is requested, the returned value is an empty string or %NULL if the variable
2929 * is not set or "*" if the variable is set (regardless of its value). The
2930 * returned value is a copy of the configuration variable in text format, i.e,.
2931 * the same format that the text-based configuration file and wpa_config_set()
2932 * are using for the value. The caller is responsible for freeing the returned
2933 * value.
2934 */
2935char * wpa_config_get_no_key(struct wpa_ssid *ssid, const char *var)
2936{
2937 size_t i;
2938
2939 if (ssid == NULL || var == NULL)
2940 return NULL;
2941
2942 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2943 const struct parse_data *field = &ssid_fields[i];
2944 if (os_strcmp(var, field->name) == 0) {
2945 char *res = field->writer(field, ssid);
2946 if (field->key_data) {
2947 if (res && res[0]) {
2948 wpa_printf(MSG_DEBUG, "Do not allow "
2949 "key_data field to be "
2950 "exposed");
19c48da0 2951 str_clear_free(res);
6fc6879b
JM
2952 return os_strdup("*");
2953 }
2954
2955 os_free(res);
2956 return NULL;
2957 }
2958 return res;
2959 }
2960 }
2961
2962 return NULL;
2963}
2964#endif /* NO_CONFIG_WRITE */
2965
2966
2967/**
2968 * wpa_config_update_psk - Update WPA PSK based on passphrase and SSID
2969 * @ssid: Pointer to network configuration data
2970 *
2971 * This function must be called to update WPA PSK when either SSID or the
2972 * passphrase has changed for the network configuration.
2973 */
2974void wpa_config_update_psk(struct wpa_ssid *ssid)
2975{
2976#ifndef CONFIG_NO_PBKDF2
986de33d 2977 pbkdf2_sha1(ssid->passphrase, ssid->ssid, ssid->ssid_len, 4096,
6fc6879b
JM
2978 ssid->psk, PMK_LEN);
2979 wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
2980 ssid->psk, PMK_LEN);
2981 ssid->psk_set = 1;
2982#endif /* CONFIG_NO_PBKDF2 */
2983}
2984
2985
33fb8c52
JM
2986static int wpa_config_set_cred_req_conn_capab(struct wpa_cred *cred,
2987 const char *value)
2988{
2989 u8 *proto;
2990 int **port;
2991 int *ports, *nports;
2992 const char *pos;
2993 unsigned int num_ports;
2994
2995 proto = os_realloc_array(cred->req_conn_capab_proto,
2996 cred->num_req_conn_capab + 1, sizeof(u8));
2997 if (proto == NULL)
2998 return -1;
2999 cred->req_conn_capab_proto = proto;
3000
3001 port = os_realloc_array(cred->req_conn_capab_port,
3002 cred->num_req_conn_capab + 1, sizeof(int *));
3003 if (port == NULL)
3004 return -1;
3005 cred->req_conn_capab_port = port;
3006
3007 proto[cred->num_req_conn_capab] = atoi(value);
3008
3009 pos = os_strchr(value, ':');
3010 if (pos == NULL) {
3011 port[cred->num_req_conn_capab] = NULL;
3012 cred->num_req_conn_capab++;
3013 return 0;
3014 }
3015 pos++;
3016
3017 ports = NULL;
3018 num_ports = 0;
3019
3020 while (*pos) {
3021 nports = os_realloc_array(ports, num_ports + 1, sizeof(int));
3022 if (nports == NULL) {
3023 os_free(ports);
3024 return -1;
3025 }
3026 ports = nports;
3027 ports[num_ports++] = atoi(pos);
3028
3029 pos = os_strchr(pos, ',');
3030 if (pos == NULL)
3031 break;
3032 pos++;
3033 }
3034
3035 nports = os_realloc_array(ports, num_ports + 1, sizeof(int));
3036 if (nports == NULL) {
3037 os_free(ports);
3038 return -1;
3039 }
3040 ports = nports;
3041 ports[num_ports] = -1;
3042
3043 port[cred->num_req_conn_capab] = ports;
3044 cred->num_req_conn_capab++;
3045 return 0;
3046}
3047
3048
1bb7b8e8
JM
3049int wpa_config_set_cred(struct wpa_cred *cred, const char *var,
3050 const char *value, int line)
3051{
3052 char *val;
3053 size_t len;
3054
03ed3324
JM
3055 if (os_strcmp(var, "temporary") == 0) {
3056 cred->temporary = atoi(value);
3057 return 0;
3058 }
3059
1a712d2f
JM
3060 if (os_strcmp(var, "priority") == 0) {
3061 cred->priority = atoi(value);
3062 return 0;
3063 }
3064
74794891
JM
3065 if (os_strcmp(var, "sp_priority") == 0) {
3066 int prio = atoi(value);
3067 if (prio < 0 || prio > 255)
3068 return -1;
3069 cred->sp_priority = prio;
3070 return 0;
3071 }
3072
d7b01abd
JM
3073 if (os_strcmp(var, "pcsc") == 0) {
3074 cred->pcsc = atoi(value);
3075 return 0;
3076 }
3077
8ca93c59
JM
3078 if (os_strcmp(var, "eap") == 0) {
3079 struct eap_method_type method;
3080 method.method = eap_peer_get_type(value, &method.vendor);
3081 if (method.vendor == EAP_VENDOR_IETF &&
3082 method.method == EAP_TYPE_NONE) {
3083 wpa_printf(MSG_ERROR, "Line %d: unknown EAP type '%s' "
3084 "for a credential", line, value);
3085 return -1;
3086 }
3087 os_free(cred->eap_method);
3088 cred->eap_method = os_malloc(sizeof(*cred->eap_method));
3089 if (cred->eap_method == NULL)
3090 return -1;
3091 os_memcpy(cred->eap_method, &method, sizeof(method));
3092 return 0;
3093 }
3094
02af9c90
JM
3095 if (os_strcmp(var, "password") == 0 &&
3096 os_strncmp(value, "ext:", 4) == 0) {
b166cd84
JM
3097 if (has_newline(value))
3098 return -1;
19c48da0 3099 str_clear_free(cred->password);
02af9c90
JM
3100 cred->password = os_strdup(value);
3101 cred->ext_password = 1;
3102 return 0;
3103 }
3104
f9cd147d
JM
3105 if (os_strcmp(var, "update_identifier") == 0) {
3106 cred->update_identifier = atoi(value);
3107 return 0;
3108 }
3109
4cad9df1
JM
3110 if (os_strcmp(var, "min_dl_bandwidth_home") == 0) {
3111 cred->min_dl_bandwidth_home = atoi(value);
3112 return 0;
3113 }
3114
3115 if (os_strcmp(var, "min_ul_bandwidth_home") == 0) {
3116 cred->min_ul_bandwidth_home = atoi(value);
3117 return 0;
3118 }
3119
3120 if (os_strcmp(var, "min_dl_bandwidth_roaming") == 0) {
3121 cred->min_dl_bandwidth_roaming = atoi(value);
3122 return 0;
3123 }
3124
3125 if (os_strcmp(var, "min_ul_bandwidth_roaming") == 0) {
3126 cred->min_ul_bandwidth_roaming = atoi(value);
3127 return 0;
3128 }
3129
a45b2dc5
JM
3130 if (os_strcmp(var, "max_bss_load") == 0) {
3131 cred->max_bss_load = atoi(value);
3132 return 0;
3133 }
3134
33fb8c52
JM
3135 if (os_strcmp(var, "req_conn_capab") == 0)
3136 return wpa_config_set_cred_req_conn_capab(cred, value);
3137
cf6d08a6
JM
3138 if (os_strcmp(var, "ocsp") == 0) {
3139 cred->ocsp = atoi(value);
3140 return 0;
3141 }
3142
13f6a07e
NJ
3143 if (os_strcmp(var, "sim_num") == 0) {
3144 cred->sim_num = atoi(value);
3145 return 0;
3146 }
3147
1bb7b8e8 3148 val = wpa_config_parse_string(value, &len);
b166cd84
JM
3149 if (val == NULL ||
3150 (os_strcmp(var, "excluded_ssid") != 0 &&
3151 os_strcmp(var, "roaming_consortium") != 0 &&
3152 os_strcmp(var, "required_roaming_consortium") != 0 &&
3153 has_newline(val))) {
7d86e537
JM
3154 wpa_printf(MSG_ERROR, "Line %d: invalid field '%s' string "
3155 "value '%s'.", line, var, value);
b166cd84 3156 os_free(val);
1bb7b8e8 3157 return -1;
7d86e537 3158 }
1bb7b8e8
JM
3159
3160 if (os_strcmp(var, "realm") == 0) {
3161 os_free(cred->realm);
3162 cred->realm = val;
3163 return 0;
3164 }
3165
3166 if (os_strcmp(var, "username") == 0) {
19c48da0 3167 str_clear_free(cred->username);
1bb7b8e8
JM
3168 cred->username = val;
3169 return 0;
3170 }
3171
3172 if (os_strcmp(var, "password") == 0) {
19c48da0 3173 str_clear_free(cred->password);
1bb7b8e8 3174 cred->password = val;
02af9c90 3175 cred->ext_password = 0;
1bb7b8e8
JM
3176 return 0;
3177 }
3178
3179 if (os_strcmp(var, "ca_cert") == 0) {
3180 os_free(cred->ca_cert);
3181 cred->ca_cert = val;
3182 return 0;
3183 }
3184
11e4f46a
JM
3185 if (os_strcmp(var, "client_cert") == 0) {
3186 os_free(cred->client_cert);
3187 cred->client_cert = val;
3188 return 0;
3189 }
3190
3191 if (os_strcmp(var, "private_key") == 0) {
3192 os_free(cred->private_key);
3193 cred->private_key = val;
3194 return 0;
3195 }
3196
3197 if (os_strcmp(var, "private_key_passwd") == 0) {
19c48da0 3198 str_clear_free(cred->private_key_passwd);
11e4f46a
JM
3199 cred->private_key_passwd = val;
3200 return 0;
3201 }
3202
1bb7b8e8
JM
3203 if (os_strcmp(var, "imsi") == 0) {
3204 os_free(cred->imsi);
3205 cred->imsi = val;
3206 return 0;
3207 }
3208
3209 if (os_strcmp(var, "milenage") == 0) {
19c48da0 3210 str_clear_free(cred->milenage);
1bb7b8e8
JM
3211 cred->milenage = val;
3212 return 0;
3213 }
3214
ac1bc549
JM
3215 if (os_strcmp(var, "domain_suffix_match") == 0) {
3216 os_free(cred->domain_suffix_match);
3217 cred->domain_suffix_match = val;
3218 return 0;
3219 }
3220
1bb7b8e8 3221 if (os_strcmp(var, "domain") == 0) {
463c8ffb
JM
3222 char **new_domain;
3223 new_domain = os_realloc_array(cred->domain,
3224 cred->num_domain + 1,
3225 sizeof(char *));
3226 if (new_domain == NULL) {
3227 os_free(val);
3228 return -1;
3229 }
3230 new_domain[cred->num_domain++] = val;
3231 cred->domain = new_domain;
1bb7b8e8
JM
3232 return 0;
3233 }
3234
8ca93c59
JM
3235 if (os_strcmp(var, "phase1") == 0) {
3236 os_free(cred->phase1);
3237 cred->phase1 = val;
3238 return 0;
3239 }
3240
3241 if (os_strcmp(var, "phase2") == 0) {
3242 os_free(cred->phase2);
3243 cred->phase2 = val;
3244 return 0;
3245 }
3246
955567bc
JM
3247 if (os_strcmp(var, "roaming_consortium") == 0) {
3248 if (len < 3 || len > sizeof(cred->roaming_consortium)) {
3249 wpa_printf(MSG_ERROR, "Line %d: invalid "
3250 "roaming_consortium length %d (3..15 "
3251 "expected)", line, (int) len);
3252 os_free(val);
3253 return -1;
3254 }
3255 os_memcpy(cred->roaming_consortium, val, len);
3256 cred->roaming_consortium_len = len;
3257 os_free(val);
3258 return 0;
3259 }
3260
f47c1452
JM
3261 if (os_strcmp(var, "required_roaming_consortium") == 0) {
3262 if (len < 3 || len > sizeof(cred->required_roaming_consortium))
3263 {
3264 wpa_printf(MSG_ERROR, "Line %d: invalid "
3265 "required_roaming_consortium length %d "
3266 "(3..15 expected)", line, (int) len);
3267 os_free(val);
3268 return -1;
3269 }
3270 os_memcpy(cred->required_roaming_consortium, val, len);
3271 cred->required_roaming_consortium_len = len;
3272 os_free(val);
3273 return 0;
3274 }
3275
dbea8ac7
JM
3276 if (os_strcmp(var, "excluded_ssid") == 0) {
3277 struct excluded_ssid *e;
3278
eaa8eefe 3279 if (len > SSID_MAX_LEN) {
dbea8ac7
JM
3280 wpa_printf(MSG_ERROR, "Line %d: invalid "
3281 "excluded_ssid length %d", line, (int) len);
3282 os_free(val);
3283 return -1;
3284 }
3285
3286 e = os_realloc_array(cred->excluded_ssid,
3287 cred->num_excluded_ssid + 1,
3288 sizeof(struct excluded_ssid));
3289 if (e == NULL) {
3290 os_free(val);
3291 return -1;
3292 }
3293 cred->excluded_ssid = e;
3294
3295 e = &cred->excluded_ssid[cred->num_excluded_ssid++];
3296 os_memcpy(e->ssid, val, len);
3297 e->ssid_len = len;
3298
3299 os_free(val);
3300
3301 return 0;
3302 }
3303
bc00053c
JM
3304 if (os_strcmp(var, "roaming_partner") == 0) {
3305 struct roaming_partner *p;
3306 char *pos;
3307
3308 p = os_realloc_array(cred->roaming_partner,
3309 cred->num_roaming_partner + 1,
3310 sizeof(struct roaming_partner));
3311 if (p == NULL) {
3312 os_free(val);
3313 return -1;
3314 }
3315 cred->roaming_partner = p;
3316
3317 p = &cred->roaming_partner[cred->num_roaming_partner];
3318
3319 pos = os_strchr(val, ',');
3320 if (pos == NULL) {
3321 os_free(val);
3322 return -1;
3323 }
3324 *pos++ = '\0';
3325 if (pos - val - 1 >= (int) sizeof(p->fqdn)) {
3326 os_free(val);
3327 return -1;
3328 }
3329 os_memcpy(p->fqdn, val, pos - val);
3330
3331 p->exact_match = atoi(pos);
3332
3333 pos = os_strchr(pos, ',');
3334 if (pos == NULL) {
3335 os_free(val);
3336 return -1;
3337 }
3338 *pos++ = '\0';
3339
3340 p->priority = atoi(pos);
3341
3342 pos = os_strchr(pos, ',');
3343 if (pos == NULL) {
3344 os_free(val);
3345 return -1;
3346 }
3347 *pos++ = '\0';
3348
3349 if (os_strlen(pos) >= sizeof(p->country)) {
3350 os_free(val);
3351 return -1;
3352 }
3353 os_memcpy(p->country, pos, os_strlen(pos) + 1);
3354
3355 cred->num_roaming_partner++;
3356 os_free(val);
3357
3358 return 0;
3359 }
3360
aa26ba68
JM
3361 if (os_strcmp(var, "provisioning_sp") == 0) {
3362 os_free(cred->provisioning_sp);
3363 cred->provisioning_sp = val;
3364 return 0;
3365 }
3366
1bb7b8e8
JM
3367 if (line) {
3368 wpa_printf(MSG_ERROR, "Line %d: unknown cred field '%s'.",
3369 line, var);
3370 }
3371
f4b2d69b
JM
3372 os_free(val);
3373
1bb7b8e8
JM
3374 return -1;
3375}
3376
3377
8f03ac90 3378static char * alloc_int_str(int val)
c880ab87 3379{
1d399771 3380 const unsigned int bufsize = 20;
c880ab87 3381 char *buf;
1d399771 3382 int res;
c880ab87 3383
1d399771 3384 buf = os_malloc(bufsize);
c880ab87
JM
3385 if (buf == NULL)
3386 return NULL;
1d399771
JM
3387 res = os_snprintf(buf, bufsize, "%d", val);
3388 if (os_snprintf_error(bufsize, res)) {
3389 os_free(buf);
3390 buf = NULL;
3391 }
c880ab87
JM
3392 return buf;
3393}
3394
3395
8f03ac90 3396static char * alloc_strdup(const char *str)
c880ab87
JM
3397{
3398 if (str == NULL)
3399 return NULL;
3400 return os_strdup(str);
3401}
3402
3403
3404char * wpa_config_get_cred_no_key(struct wpa_cred *cred, const char *var)
3405{
3406 if (os_strcmp(var, "temporary") == 0)
3407 return alloc_int_str(cred->temporary);
3408
3409 if (os_strcmp(var, "priority") == 0)
3410 return alloc_int_str(cred->priority);
3411
3412 if (os_strcmp(var, "sp_priority") == 0)
3413 return alloc_int_str(cred->sp_priority);
3414
3415 if (os_strcmp(var, "pcsc") == 0)
3416 return alloc_int_str(cred->pcsc);
3417
3418 if (os_strcmp(var, "eap") == 0) {
3419 if (!cred->eap_method)
3420 return NULL;
3421 return alloc_strdup(eap_get_name(cred->eap_method[0].vendor,
3422 cred->eap_method[0].method));
3423 }
3424
3425 if (os_strcmp(var, "update_identifier") == 0)
3426 return alloc_int_str(cred->update_identifier);
3427
3428 if (os_strcmp(var, "min_dl_bandwidth_home") == 0)
3429 return alloc_int_str(cred->min_dl_bandwidth_home);
3430
3431 if (os_strcmp(var, "min_ul_bandwidth_home") == 0)
3432 return alloc_int_str(cred->min_ul_bandwidth_home);
3433
3434 if (os_strcmp(var, "min_dl_bandwidth_roaming") == 0)
3435 return alloc_int_str(cred->min_dl_bandwidth_roaming);
3436
3437 if (os_strcmp(var, "min_ul_bandwidth_roaming") == 0)
3438 return alloc_int_str(cred->min_ul_bandwidth_roaming);
3439
3440 if (os_strcmp(var, "max_bss_load") == 0)
3441 return alloc_int_str(cred->max_bss_load);
3442
3443 if (os_strcmp(var, "req_conn_capab") == 0) {
3444 unsigned int i;
3445 char *buf, *end, *pos;
3446 int ret;
3447
3448 if (!cred->num_req_conn_capab)
3449 return NULL;
3450
3451 buf = os_malloc(4000);
3452 if (buf == NULL)
3453 return NULL;
3454 pos = buf;
3455 end = pos + 4000;
3456 for (i = 0; i < cred->num_req_conn_capab; i++) {
3457 int *ports;
3458
3459 ret = os_snprintf(pos, end - pos, "%s%u",
3460 i > 0 ? "\n" : "",
3461 cred->req_conn_capab_proto[i]);
d85e1fc8 3462 if (os_snprintf_error(end - pos, ret))
c880ab87
JM
3463 return buf;
3464 pos += ret;
3465
3466 ports = cred->req_conn_capab_port[i];
3467 if (ports) {
3468 int j;
3469 for (j = 0; ports[j] != -1; j++) {
3470 ret = os_snprintf(pos, end - pos,
3471 "%s%d",
3472 j > 0 ? "," : ":",
3473 ports[j]);
d85e1fc8 3474 if (os_snprintf_error(end - pos, ret))
c880ab87
JM
3475 return buf;
3476 pos += ret;
3477 }
3478 }
3479 }
3480
3481 return buf;
3482 }
3483
3484 if (os_strcmp(var, "ocsp") == 0)
3485 return alloc_int_str(cred->ocsp);
3486
3487 if (os_strcmp(var, "realm") == 0)
3488 return alloc_strdup(cred->realm);
3489
3490 if (os_strcmp(var, "username") == 0)
3491 return alloc_strdup(cred->username);
3492
3493 if (os_strcmp(var, "password") == 0) {
3494 if (!cred->password)
3495 return NULL;
3496 return alloc_strdup("*");
3497 }
3498
3499 if (os_strcmp(var, "ca_cert") == 0)
3500 return alloc_strdup(cred->ca_cert);
3501
3502 if (os_strcmp(var, "client_cert") == 0)
3503 return alloc_strdup(cred->client_cert);
3504
3505 if (os_strcmp(var, "private_key") == 0)
3506 return alloc_strdup(cred->private_key);
3507
3508 if (os_strcmp(var, "private_key_passwd") == 0) {
3509 if (!cred->private_key_passwd)
3510 return NULL;
3511 return alloc_strdup("*");
3512 }
3513
3514 if (os_strcmp(var, "imsi") == 0)
3515 return alloc_strdup(cred->imsi);
3516
3517 if (os_strcmp(var, "milenage") == 0) {
3518 if (!(cred->milenage))
3519 return NULL;
3520 return alloc_strdup("*");
3521 }
3522
3523 if (os_strcmp(var, "domain_suffix_match") == 0)
3524 return alloc_strdup(cred->domain_suffix_match);
3525
3526 if (os_strcmp(var, "domain") == 0) {
3527 unsigned int i;
3528 char *buf, *end, *pos;
3529 int ret;
3530
3531 if (!cred->num_domain)
3532 return NULL;
3533
3534 buf = os_malloc(4000);
3535 if (buf == NULL)
3536 return NULL;
3537 pos = buf;
3538 end = pos + 4000;
3539
3540 for (i = 0; i < cred->num_domain; i++) {
3541 ret = os_snprintf(pos, end - pos, "%s%s",
3542 i > 0 ? "\n" : "", cred->domain[i]);
d85e1fc8 3543 if (os_snprintf_error(end - pos, ret))
c880ab87
JM
3544 return buf;
3545 pos += ret;
3546 }
3547
3548 return buf;
3549 }
3550
3551 if (os_strcmp(var, "phase1") == 0)
3552 return alloc_strdup(cred->phase1);
3553
3554 if (os_strcmp(var, "phase2") == 0)
3555 return alloc_strdup(cred->phase2);
3556
3557 if (os_strcmp(var, "roaming_consortium") == 0) {
3558 size_t buflen;
3559 char *buf;
3560
3561 if (!cred->roaming_consortium_len)
3562 return NULL;
3563 buflen = cred->roaming_consortium_len * 2 + 1;
3564 buf = os_malloc(buflen);
3565 if (buf == NULL)
3566 return NULL;
3567 wpa_snprintf_hex(buf, buflen, cred->roaming_consortium,
3568 cred->roaming_consortium_len);
3569 return buf;
3570 }
3571
3572 if (os_strcmp(var, "required_roaming_consortium") == 0) {
3573 size_t buflen;
3574 char *buf;
3575
3576 if (!cred->required_roaming_consortium_len)
3577 return NULL;
3578 buflen = cred->required_roaming_consortium_len * 2 + 1;
3579 buf = os_malloc(buflen);
3580 if (buf == NULL)
3581 return NULL;
3582 wpa_snprintf_hex(buf, buflen, cred->required_roaming_consortium,
3583 cred->required_roaming_consortium_len);
3584 return buf;
3585 }
3586
3587 if (os_strcmp(var, "excluded_ssid") == 0) {
3588 unsigned int i;
3589 char *buf, *end, *pos;
3590
3591 if (!cred->num_excluded_ssid)
3592 return NULL;
3593
3594 buf = os_malloc(4000);
3595 if (buf == NULL)
3596 return NULL;
3597 pos = buf;
3598 end = pos + 4000;
3599
3600 for (i = 0; i < cred->num_excluded_ssid; i++) {
3601 struct excluded_ssid *e;
3602 int ret;
3603
3604 e = &cred->excluded_ssid[i];
3605 ret = os_snprintf(pos, end - pos, "%s%s",
3606 i > 0 ? "\n" : "",
3607 wpa_ssid_txt(e->ssid, e->ssid_len));
d85e1fc8 3608 if (os_snprintf_error(end - pos, ret))
c880ab87
JM
3609 return buf;
3610 pos += ret;
3611 }
3612
3613 return buf;
3614 }
3615
3616 if (os_strcmp(var, "roaming_partner") == 0) {
3617 unsigned int i;
3618 char *buf, *end, *pos;
3619
3620 if (!cred->num_roaming_partner)
3621 return NULL;
3622
3623 buf = os_malloc(4000);
3624 if (buf == NULL)
3625 return NULL;
3626 pos = buf;
3627 end = pos + 4000;
3628
3629 for (i = 0; i < cred->num_roaming_partner; i++) {
3630 struct roaming_partner *p;
3631 int ret;
3632
3633 p = &cred->roaming_partner[i];
3634 ret = os_snprintf(pos, end - pos, "%s%s,%d,%u,%s",
3635 i > 0 ? "\n" : "",
3636 p->fqdn, p->exact_match, p->priority,
3637 p->country);
d85e1fc8 3638 if (os_snprintf_error(end - pos, ret))
c880ab87
JM
3639 return buf;
3640 pos += ret;
3641 }
3642
3643 return buf;
3644 }
3645
3646 if (os_strcmp(var, "provisioning_sp") == 0)
3647 return alloc_strdup(cred->provisioning_sp);
3648
3649 return NULL;
3650}
3651
3652
d94c9ee6
JM
3653struct wpa_cred * wpa_config_get_cred(struct wpa_config *config, int id)
3654{
3655 struct wpa_cred *cred;
3656
3657 cred = config->cred;
3658 while (cred) {
3659 if (id == cred->id)
3660 break;
3661 cred = cred->next;
3662 }
3663
3664 return cred;
3665}
3666
3667
3668struct wpa_cred * wpa_config_add_cred(struct wpa_config *config)
3669{
3670 int id;
3671 struct wpa_cred *cred, *last = NULL;
3672
3673 id = -1;
3674 cred = config->cred;
3675 while (cred) {
3676 if (cred->id > id)
3677 id = cred->id;
3678 last = cred;
3679 cred = cred->next;
3680 }
3681 id++;
3682
3683 cred = os_zalloc(sizeof(*cred));
3684 if (cred == NULL)
3685 return NULL;
3686 cred->id = id;
13f6a07e 3687 cred->sim_num = DEFAULT_USER_SELECTED_SIM;
d94c9ee6
JM
3688 if (last)
3689 last->next = cred;
3690 else
3691 config->cred = cred;
3692
3693 return cred;
3694}
3695
3696
3697int wpa_config_remove_cred(struct wpa_config *config, int id)
3698{
3699 struct wpa_cred *cred, *prev = NULL;
3700
3701 cred = config->cred;
3702 while (cred) {
3703 if (id == cred->id)
3704 break;
3705 prev = cred;
3706 cred = cred->next;
3707 }
3708
3709 if (cred == NULL)
3710 return -1;
3711
3712 if (prev)
3713 prev->next = cred->next;
3714 else
3715 config->cred = cred->next;
3716
3717 wpa_config_free_cred(cred);
3718 return 0;
3719}
3720
3721
6fc6879b
JM
3722#ifndef CONFIG_NO_CONFIG_BLOBS
3723/**
3724 * wpa_config_get_blob - Get a named configuration blob
3725 * @config: Configuration data from wpa_config_read()
3726 * @name: Name of the blob
3727 * Returns: Pointer to blob data or %NULL if not found
3728 */
3729const struct wpa_config_blob * wpa_config_get_blob(struct wpa_config *config,
3730 const char *name)
3731{
3732 struct wpa_config_blob *blob = config->blobs;
3733
3734 while (blob) {
3735 if (os_strcmp(blob->name, name) == 0)
3736 return blob;
3737 blob = blob->next;
3738 }
3739 return NULL;
3740}
3741
3742
3743/**
3744 * wpa_config_set_blob - Set or add a named configuration blob
3745 * @config: Configuration data from wpa_config_read()
3746 * @blob: New value for the blob
3747 *
3748 * Adds a new configuration blob or replaces the current value of an existing
3749 * blob.
3750 */
3751void wpa_config_set_blob(struct wpa_config *config,
3752 struct wpa_config_blob *blob)
3753{
3754 wpa_config_remove_blob(config, blob->name);
3755 blob->next = config->blobs;
3756 config->blobs = blob;
3757}
3758
3759
3760/**
3761 * wpa_config_free_blob - Free blob data
3762 * @blob: Pointer to blob to be freed
3763 */
3764void wpa_config_free_blob(struct wpa_config_blob *blob)
3765{
3766 if (blob) {
3767 os_free(blob->name);
19c48da0 3768 bin_clear_free(blob->data, blob->len);
6fc6879b
JM
3769 os_free(blob);
3770 }
3771}
3772
3773
3774/**
3775 * wpa_config_remove_blob - Remove a named configuration blob
3776 * @config: Configuration data from wpa_config_read()
3777 * @name: Name of the blob to remove
3778 * Returns: 0 if blob was removed or -1 if blob was not found
3779 */
3780int wpa_config_remove_blob(struct wpa_config *config, const char *name)
3781{
3782 struct wpa_config_blob *pos = config->blobs, *prev = NULL;
3783
3784 while (pos) {
3785 if (os_strcmp(pos->name, name) == 0) {
3786 if (prev)
3787 prev->next = pos->next;
3788 else
3789 config->blobs = pos->next;
3790 wpa_config_free_blob(pos);
3791 return 0;
3792 }
3793 prev = pos;
3794 pos = pos->next;
3795 }
3796
3797 return -1;
3798}
3799#endif /* CONFIG_NO_CONFIG_BLOBS */
3800
3801
3802/**
3803 * wpa_config_alloc_empty - Allocate an empty configuration
3804 * @ctrl_interface: Control interface parameters, e.g., path to UNIX domain
3805 * socket
3806 * @driver_param: Driver parameters
3807 * Returns: Pointer to allocated configuration data or %NULL on failure
3808 */
3809struct wpa_config * wpa_config_alloc_empty(const char *ctrl_interface,
3810 const char *driver_param)
3811{
3812 struct wpa_config *config;
c26effe1
YD
3813 const int aCWmin = 4, aCWmax = 10;
3814 const struct hostapd_wmm_ac_params ac_bk =
3815 { aCWmin, aCWmax, 7, 0, 0 }; /* background traffic */
3816 const struct hostapd_wmm_ac_params ac_be =
3817 { aCWmin, aCWmax, 3, 0, 0 }; /* best effort traffic */
3818 const struct hostapd_wmm_ac_params ac_vi = /* video traffic */
623ecdd5 3819 { aCWmin - 1, aCWmin, 2, 3000 / 32, 0 };
c26effe1 3820 const struct hostapd_wmm_ac_params ac_vo = /* voice traffic */
623ecdd5 3821 { aCWmin - 2, aCWmin - 1, 2, 1500 / 32, 0 };
6fc6879b
JM
3822
3823 config = os_zalloc(sizeof(*config));
3824 if (config == NULL)
3825 return NULL;
3826 config->eapol_version = DEFAULT_EAPOL_VERSION;
3827 config->ap_scan = DEFAULT_AP_SCAN;
e45e8989 3828 config->user_mpm = DEFAULT_USER_MPM;
4b409368 3829 config->max_peer_links = DEFAULT_MAX_PEER_LINKS;
5a2a6de6 3830 config->mesh_max_inactivity = DEFAULT_MESH_MAX_INACTIVITY;
ecd40fef
MH
3831 config->dot11RSNASAERetransPeriod =
3832 DEFAULT_DOT11_RSNA_SAE_RETRANS_PERIOD;
6fc6879b 3833 config->fast_reauth = DEFAULT_FAST_REAUTH;
e3768e7c 3834 config->p2p_go_intent = DEFAULT_P2P_GO_INTENT;
0f66abd2 3835 config->p2p_intra_bss = DEFAULT_P2P_INTRA_BSS;
1a471ff0 3836 config->p2p_go_freq_change_policy = DEFAULT_P2P_GO_FREQ_MOVE;
462a7439 3837 config->p2p_go_max_inactivity = DEFAULT_P2P_GO_MAX_INACTIVITY;
e3bd6e9d 3838 config->p2p_optimize_listen_chan = DEFAULT_P2P_OPTIMIZE_LISTEN_CHAN;
0b8bcaa5 3839 config->p2p_go_ctwindow = DEFAULT_P2P_GO_CTWINDOW;
c9c38b09 3840 config->bss_max_count = DEFAULT_BSS_MAX_COUNT;
78633c37
SL
3841 config->bss_expiration_age = DEFAULT_BSS_EXPIRATION_AGE;
3842 config->bss_expiration_scan_count = DEFAULT_BSS_EXPIRATION_SCAN_COUNT;
dae608d5 3843 config->max_num_sta = DEFAULT_MAX_NUM_STA;
11540c0b 3844 config->access_network_type = DEFAULT_ACCESS_NETWORK_TYPE;
6124e858 3845 config->scan_cur_freq = DEFAULT_SCAN_CUR_FREQ;
c26effe1
YD
3846 config->wmm_ac_params[0] = ac_be;
3847 config->wmm_ac_params[1] = ac_bk;
3848 config->wmm_ac_params[2] = ac_vi;
3849 config->wmm_ac_params[3] = ac_vo;
d3b20469 3850 config->p2p_search_delay = DEFAULT_P2P_SEARCH_DELAY;
c267753b 3851 config->rand_addr_lifetime = DEFAULT_RAND_ADDR_LIFETIME;
b41f2684 3852 config->key_mgmt_offload = DEFAULT_KEY_MGMT_OFFLOAD;
483dd6a5 3853 config->cert_in_cb = DEFAULT_CERT_IN_CB;
73ed03f3 3854 config->wpa_rsc_relaxation = DEFAULT_WPA_RSC_RELAXATION;
6fc6879b 3855
c5d193d7
DS
3856#ifdef CONFIG_MBO
3857 config->mbo_cell_capa = DEFAULT_MBO_CELL_CAPA;
af8bc24d
KV
3858 config->disassoc_imminent_rssi_threshold =
3859 DEFAULT_DISASSOC_IMMINENT_RSSI_THRESHOLD;
332aadb8 3860 config->oce = DEFAULT_OCE_SUPPORT;
c5d193d7
DS
3861#endif /* CONFIG_MBO */
3862
6fc6879b
JM
3863 if (ctrl_interface)
3864 config->ctrl_interface = os_strdup(ctrl_interface);
3865 if (driver_param)
3866 config->driver_param = os_strdup(driver_param);
1d9d21f3 3867 config->gas_rand_addr_lifetime = DEFAULT_RAND_ADDR_LIFETIME;
6fc6879b
JM
3868
3869 return config;
3870}
3871
3872
3873#ifndef CONFIG_NO_STDOUT_DEBUG
3874/**
3875 * wpa_config_debug_dump_networks - Debug dump of configured networks
3876 * @config: Configuration data from wpa_config_read()
3877 */
3878void wpa_config_debug_dump_networks(struct wpa_config *config)
3879{
3880 int prio;
3881 struct wpa_ssid *ssid;
3882
3883 for (prio = 0; prio < config->num_prio; prio++) {
3884 ssid = config->pssid[prio];
3885 wpa_printf(MSG_DEBUG, "Priority group %d",
3886 ssid->priority);
3887 while (ssid) {
3888 wpa_printf(MSG_DEBUG, " id=%d ssid='%s'",
3889 ssid->id,
3890 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
3891 ssid = ssid->pnext;
3892 }
3893 }
3894}
3895#endif /* CONFIG_NO_STDOUT_DEBUG */
121adf9c
JM
3896
3897
3898struct global_parse_data {
3899 char *name;
3900 int (*parser)(const struct global_parse_data *data,
3901 struct wpa_config *config, int line, const char *value);
10263dc2
OO
3902 int (*get)(const char *name, struct wpa_config *config, long offset,
3903 char *buf, size_t buflen, int pretty_print);
121adf9c 3904 void *param1, *param2, *param3;
1d47214a 3905 unsigned int changed_flag;
121adf9c
JM
3906};
3907
3908
3909static int wpa_global_config_parse_int(const struct global_parse_data *data,
3910 struct wpa_config *config, int line,
3911 const char *pos)
3912{
eae3a584
JB
3913 int val, *dst;
3914 char *end;
3915
121adf9c 3916 dst = (int *) (((u8 *) config) + (long) data->param1);
eae3a584
JB
3917 val = strtol(pos, &end, 0);
3918 if (*end) {
3919 wpa_printf(MSG_ERROR, "Line %d: invalid number \"%s\"",
3920 line, pos);
3921 return -1;
3922 }
3923 *dst = val;
3924
121adf9c
JM
3925 wpa_printf(MSG_DEBUG, "%s=%d", data->name, *dst);
3926
3927 if (data->param2 && *dst < (long) data->param2) {
3928 wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
3929 "min_value=%ld)", line, data->name, *dst,
3930 (long) data->param2);
3931 *dst = (long) data->param2;
3932 return -1;
3933 }
3934
3935 if (data->param3 && *dst > (long) data->param3) {
3936 wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
3937 "max_value=%ld)", line, data->name, *dst,
3938 (long) data->param3);
3939 *dst = (long) data->param3;
3940 return -1;
3941 }
3942
3943 return 0;
3944}
3945
3946
3947static int wpa_global_config_parse_str(const struct global_parse_data *data,
3948 struct wpa_config *config, int line,
3949 const char *pos)
3950{
3951 size_t len;
3952 char **dst, *tmp;
3953
3954 len = os_strlen(pos);
3955 if (data->param2 && len < (size_t) data->param2) {
3956 wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
3957 "min_len=%ld)", line, data->name,
3958 (unsigned long) len, (long) data->param2);
3959 return -1;
3960 }
3961
3962 if (data->param3 && len > (size_t) data->param3) {
3963 wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
3964 "max_len=%ld)", line, data->name,
3965 (unsigned long) len, (long) data->param3);
3966 return -1;
3967 }
3968
2a3f5650
JM
3969 if (has_newline(pos)) {
3970 wpa_printf(MSG_ERROR, "Line %d: invalid %s value with newline",
3971 line, data->name);
3972 return -1;
3973 }
3974
121adf9c
JM
3975 tmp = os_strdup(pos);
3976 if (tmp == NULL)
3977 return -1;
3978
3979 dst = (char **) (((u8 *) config) + (long) data->param1);
3980 os_free(*dst);
3981 *dst = tmp;
3982 wpa_printf(MSG_DEBUG, "%s='%s'", data->name, *dst);
3983
3984 return 0;
3985}
3986
3987
31392709
HD
3988static int wpa_config_process_bgscan(const struct global_parse_data *data,
3989 struct wpa_config *config, int line,
3990 const char *pos)
3991{
3992 size_t len;
3993 char *tmp;
04c366cb 3994 int res;
31392709
HD
3995
3996 tmp = wpa_config_parse_string(pos, &len);
3997 if (tmp == NULL) {
3998 wpa_printf(MSG_ERROR, "Line %d: failed to parse %s",
3999 line, data->name);
4000 return -1;
4001 }
4002
04c366cb
EL
4003 res = wpa_global_config_parse_str(data, config, line, tmp);
4004 os_free(tmp);
4005 return res;
31392709
HD
4006}
4007
4008
3f2c8ba6
JM
4009static int wpa_global_config_parse_bin(const struct global_parse_data *data,
4010 struct wpa_config *config, int line,
4011 const char *pos)
4012{
3f2c8ba6
JM
4013 struct wpabuf **dst, *tmp;
4014
9d955f75
DS
4015 tmp = wpabuf_parse_bin(pos);
4016 if (!tmp)
3f2c8ba6 4017 return -1;
3f2c8ba6
JM
4018
4019 dst = (struct wpabuf **) (((u8 *) config) + (long) data->param1);
4020 wpabuf_free(*dst);
4021 *dst = tmp;
4022 wpa_printf(MSG_DEBUG, "%s", data->name);
4023
4024 return 0;
4025}
4026
4027
f5ffc348
BG
4028static int wpa_config_process_freq_list(const struct global_parse_data *data,
4029 struct wpa_config *config, int line,
4030 const char *value)
4031{
4032 int *freqs;
4033
4034 freqs = wpa_config_parse_int_array(value);
4035 if (freqs == NULL)
4036 return -1;
6668efda
JM
4037 if (freqs[0] == 0) {
4038 os_free(freqs);
4039 freqs = NULL;
4040 }
f5ffc348
BG
4041 os_free(config->freq_list);
4042 config->freq_list = freqs;
4043 return 0;
4044}
4045
4046
25ef8529
JM
4047#ifdef CONFIG_P2P
4048static int wpa_global_config_parse_ipv4(const struct global_parse_data *data,
4049 struct wpa_config *config, int line,
4050 const char *pos)
4051{
4052 u32 *dst;
4053 struct hostapd_ip_addr addr;
4054
4055 if (hostapd_parse_ip_addr(pos, &addr) < 0)
4056 return -1;
4057 if (addr.af != AF_INET)
4058 return -1;
4059
4060 dst = (u32 *) (((u8 *) config) + (long) data->param1);
4061 os_memcpy(dst, &addr.u.v4.s_addr, 4);
4062 wpa_printf(MSG_DEBUG, "%s = 0x%x", data->name,
4063 WPA_GET_BE32((u8 *) dst));
4064
4065 return 0;
4066}
4067#endif /* CONFIG_P2P */
4068
4069
121adf9c
JM
4070static int wpa_config_process_country(const struct global_parse_data *data,
4071 struct wpa_config *config, int line,
4072 const char *pos)
4073{
4074 if (!pos[0] || !pos[1]) {
4075 wpa_printf(MSG_DEBUG, "Invalid country set");
4076 return -1;
4077 }
4078 config->country[0] = pos[0];
4079 config->country[1] = pos[1];
4080 wpa_printf(MSG_DEBUG, "country='%c%c'",
4081 config->country[0], config->country[1]);
4082 return 0;
4083}
4084
4085
4086static int wpa_config_process_load_dynamic_eap(
4087 const struct global_parse_data *data, struct wpa_config *config,
4088 int line, const char *so)
4089{
4090 int ret;
4091 wpa_printf(MSG_DEBUG, "load_dynamic_eap=%s", so);
4092 ret = eap_peer_method_load(so);
4093 if (ret == -2) {
4094 wpa_printf(MSG_DEBUG, "This EAP type was already loaded - not "
4095 "reloading.");
4096 } else if (ret) {
4097 wpa_printf(MSG_ERROR, "Line %d: Failed to load dynamic EAP "
4098 "method '%s'.", line, so);
4099 return -1;
4100 }
4101
4102 return 0;
4103}
4104
4105
4106#ifdef CONFIG_WPS
4107
4108static int wpa_config_process_uuid(const struct global_parse_data *data,
4109 struct wpa_config *config, int line,
4110 const char *pos)
4111{
4112 char buf[40];
4113 if (uuid_str2bin(pos, config->uuid)) {
4114 wpa_printf(MSG_ERROR, "Line %d: invalid UUID", line);
4115 return -1;
4116 }
4117 uuid_bin2str(config->uuid, buf, sizeof(buf));
4118 wpa_printf(MSG_DEBUG, "uuid=%s", buf);
4119 return 0;
4120}
4121
4122
2f646b6e
JB
4123static int wpa_config_process_device_type(
4124 const struct global_parse_data *data,
4125 struct wpa_config *config, int line, const char *pos)
4126{
4127 return wps_dev_type_str2bin(pos, config->device_type);
4128}
4129
4130
121adf9c
JM
4131static int wpa_config_process_os_version(const struct global_parse_data *data,
4132 struct wpa_config *config, int line,
4133 const char *pos)
4134{
4135 if (hexstr2bin(pos, config->os_version, 4)) {
4136 wpa_printf(MSG_ERROR, "Line %d: invalid os_version", line);
4137 return -1;
4138 }
4139 wpa_printf(MSG_DEBUG, "os_version=%08x",
4140 WPA_GET_BE32(config->os_version));
4141 return 0;
4142}
4143
71dd3b78
AS
4144
4145static int wpa_config_process_wps_vendor_ext_m1(
4146 const struct global_parse_data *data,
4147 struct wpa_config *config, int line, const char *pos)
4148{
4149 struct wpabuf *tmp;
4150 int len = os_strlen(pos) / 2;
4151 u8 *p;
4152
4153 if (!len) {
4154 wpa_printf(MSG_ERROR, "Line %d: "
4155 "invalid wps_vendor_ext_m1", line);
4156 return -1;
4157 }
4158
4159 tmp = wpabuf_alloc(len);
4160 if (tmp) {
4161 p = wpabuf_put(tmp, len);
4162
4163 if (hexstr2bin(pos, p, len)) {
4164 wpa_printf(MSG_ERROR, "Line %d: "
4165 "invalid wps_vendor_ext_m1", line);
4166 wpabuf_free(tmp);
4167 return -1;
4168 }
4169
4170 wpabuf_free(config->wps_vendor_ext_m1);
4171 config->wps_vendor_ext_m1 = tmp;
4172 } else {
4173 wpa_printf(MSG_ERROR, "Can not allocate "
4174 "memory for wps_vendor_ext_m1");
4175 return -1;
4176 }
4177
4178 return 0;
4179}
4180
121adf9c
JM
4181#endif /* CONFIG_WPS */
4182
e3768e7c
JM
4183#ifdef CONFIG_P2P
4184static int wpa_config_process_sec_device_type(
4185 const struct global_parse_data *data,
4186 struct wpa_config *config, int line, const char *pos)
4187{
4188 int idx;
4189
2f646b6e 4190 if (config->num_sec_device_types >= MAX_SEC_DEVICE_TYPES) {
e3768e7c
JM
4191 wpa_printf(MSG_ERROR, "Line %d: too many sec_device_type "
4192 "items", line);
4193 return -1;
4194 }
4195
2f646b6e
JB
4196 idx = config->num_sec_device_types;
4197
4198 if (wps_dev_type_str2bin(pos, config->sec_device_type[idx]))
e3768e7c 4199 return -1;
2f646b6e
JB
4200
4201 config->num_sec_device_types++;
e3768e7c
JM
4202 return 0;
4203}
21d996f7
JM
4204
4205
4206static int wpa_config_process_p2p_pref_chan(
4207 const struct global_parse_data *data,
4208 struct wpa_config *config, int line, const char *pos)
4209{
4210 struct p2p_channel *pref = NULL, *n;
4211 unsigned int num = 0;
4212 const char *pos2;
4213 u8 op_class, chan;
4214
4215 /* format: class:chan,class:chan,... */
4216
4217 while (*pos) {
4218 op_class = atoi(pos);
4219 pos2 = os_strchr(pos, ':');
4220 if (pos2 == NULL)
4221 goto fail;
4222 pos2++;
4223 chan = atoi(pos2);
4224
067ffa26
JM
4225 n = os_realloc_array(pref, num + 1,
4226 sizeof(struct p2p_channel));
21d996f7
JM
4227 if (n == NULL)
4228 goto fail;
4229 pref = n;
4230 pref[num].op_class = op_class;
4231 pref[num].chan = chan;
4232 num++;
4233
4234 pos = os_strchr(pos2, ',');
4235 if (pos == NULL)
4236 break;
4237 pos++;
4238 }
4239
4240 os_free(config->p2p_pref_chan);
4241 config->p2p_pref_chan = pref;
4242 config->num_p2p_pref_chan = num;
4243 wpa_hexdump(MSG_DEBUG, "P2P: Preferred class/channel pairs",
4244 (u8 *) config->p2p_pref_chan,
4245 config->num_p2p_pref_chan * sizeof(struct p2p_channel));
4246
4247 return 0;
4248
4249fail:
4250 os_free(pref);
4251 wpa_printf(MSG_ERROR, "Line %d: Invalid p2p_pref_chan list", line);
4252 return -1;
4253}
556b30da
JM
4254
4255
4256static int wpa_config_process_p2p_no_go_freq(
4257 const struct global_parse_data *data,
4258 struct wpa_config *config, int line, const char *pos)
4259{
4260 int ret;
4261
4262 ret = freq_range_list_parse(&config->p2p_no_go_freq, pos);
4263 if (ret < 0) {
4264 wpa_printf(MSG_ERROR, "Line %d: Invalid p2p_no_go_freq", line);
4265 return -1;
4266 }
4267
4268 wpa_printf(MSG_DEBUG, "P2P: p2p_no_go_freq with %u items",
4269 config->p2p_no_go_freq.num);
4270
4271 return 0;
4272}
4273
e3768e7c
JM
4274#endif /* CONFIG_P2P */
4275
121adf9c 4276
46ee0427
JM
4277static int wpa_config_process_hessid(
4278 const struct global_parse_data *data,
4279 struct wpa_config *config, int line, const char *pos)
4280{
4281 if (hwaddr_aton2(pos, config->hessid) < 0) {
4282 wpa_printf(MSG_ERROR, "Line %d: Invalid hessid '%s'",
4283 line, pos);
4284 return -1;
4285 }
4286
4287 return 0;
4288}
4289
4290
625f202a
JM
4291static int wpa_config_process_sae_groups(
4292 const struct global_parse_data *data,
4293 struct wpa_config *config, int line, const char *pos)
4294{
4295 int *groups = wpa_config_parse_int_array(pos);
4296 if (groups == NULL) {
4297 wpa_printf(MSG_ERROR, "Line %d: Invalid sae_groups '%s'",
4298 line, pos);
4299 return -1;
4300 }
4301
4302 os_free(config->sae_groups);
4303 config->sae_groups = groups;
4304
4305 return 0;
4306}
4307
4308
18a2eaab
JM
4309static int wpa_config_process_ap_vendor_elements(
4310 const struct global_parse_data *data,
4311 struct wpa_config *config, int line, const char *pos)
4312{
4313 struct wpabuf *tmp;
4314 int len = os_strlen(pos) / 2;
4315 u8 *p;
4316
4317 if (!len) {
4318 wpa_printf(MSG_ERROR, "Line %d: invalid ap_vendor_elements",
4319 line);
4320 return -1;
4321 }
4322
4323 tmp = wpabuf_alloc(len);
4324 if (tmp) {
4325 p = wpabuf_put(tmp, len);
4326
4327 if (hexstr2bin(pos, p, len)) {
4328 wpa_printf(MSG_ERROR, "Line %d: invalid "
4329 "ap_vendor_elements", line);
4330 wpabuf_free(tmp);
4331 return -1;
4332 }
4333
4334 wpabuf_free(config->ap_vendor_elements);
4335 config->ap_vendor_elements = tmp;
4336 } else {
4337 wpa_printf(MSG_ERROR, "Cannot allocate memory for "
4338 "ap_vendor_elements");
4339 return -1;
4340 }
4341
4342 return 0;
4343}
4344
4345
298f5185 4346#ifdef CONFIG_CTRL_IFACE
ea61aa1d
JM
4347static int wpa_config_process_no_ctrl_interface(
4348 const struct global_parse_data *data,
4349 struct wpa_config *config, int line, const char *pos)
4350{
4351 wpa_printf(MSG_DEBUG, "no_ctrl_interface -> ctrl_interface=NULL");
4352 os_free(config->ctrl_interface);
4353 config->ctrl_interface = NULL;
4354 return 0;
4355}
298f5185 4356#endif /* CONFIG_CTRL_IFACE */
ea61aa1d
JM
4357
4358
10263dc2
OO
4359static int wpa_config_get_int(const char *name, struct wpa_config *config,
4360 long offset, char *buf, size_t buflen,
4361 int pretty_print)
4362{
4363 int *val = (int *) (((u8 *) config) + (long) offset);
4364
4365 if (pretty_print)
4366 return os_snprintf(buf, buflen, "%s=%d\n", name, *val);
4367 return os_snprintf(buf, buflen, "%d", *val);
4368}
4369
4370
4371static int wpa_config_get_str(const char *name, struct wpa_config *config,
4372 long offset, char *buf, size_t buflen,
4373 int pretty_print)
4374{
4375 char **val = (char **) (((u8 *) config) + (long) offset);
4376 int res;
4377
4378 if (pretty_print)
4379 res = os_snprintf(buf, buflen, "%s=%s\n", name,
4380 *val ? *val : "null");
4381 else if (!*val)
4382 return -1;
4383 else
4384 res = os_snprintf(buf, buflen, "%s", *val);
4385 if (os_snprintf_error(buflen, res))
4386 res = -1;
4387
4388 return res;
4389}
4390
4391
5c6c315f
MK
4392#ifdef CONFIG_P2P
4393static int wpa_config_get_ipv4(const char *name, struct wpa_config *config,
4394 long offset, char *buf, size_t buflen,
4395 int pretty_print)
4396{
4397 void *val = ((u8 *) config) + (long) offset;
4398 int res;
4399 char addr[INET_ADDRSTRLEN];
4400
4401 if (!val || !inet_ntop(AF_INET, val, addr, sizeof(addr)))
4402 return -1;
4403
4404 if (pretty_print)
4405 res = os_snprintf(buf, buflen, "%s=%s\n", name, addr);
4406 else
4407 res = os_snprintf(buf, buflen, "%s", addr);
4408
4409 if (os_snprintf_error(buflen, res))
4410 res = -1;
4411
4412 return res;
4413}
4414#endif /* CONFIG_P2P */
4415
4416
121adf9c
JM
4417#ifdef OFFSET
4418#undef OFFSET
4419#endif /* OFFSET */
4420/* OFFSET: Get offset of a variable within the wpa_config structure */
4421#define OFFSET(v) ((void *) &((struct wpa_config *) 0)->v)
4422
10263dc2
OO
4423#define FUNC(f) #f, wpa_config_process_ ## f, NULL, OFFSET(f), NULL, NULL
4424#define FUNC_NO_VAR(f) #f, wpa_config_process_ ## f, NULL, NULL, NULL, NULL
4425#define _INT(f) #f, wpa_global_config_parse_int, wpa_config_get_int, OFFSET(f)
121adf9c
JM
4426#define INT(f) _INT(f), NULL, NULL
4427#define INT_RANGE(f, min, max) _INT(f), (void *) min, (void *) max
10263dc2 4428#define _STR(f) #f, wpa_global_config_parse_str, wpa_config_get_str, OFFSET(f)
121adf9c
JM
4429#define STR(f) _STR(f), NULL, NULL
4430#define STR_RANGE(f, min, max) _STR(f), (void *) min, (void *) max
10263dc2 4431#define BIN(f) #f, wpa_global_config_parse_bin, NULL, OFFSET(f), NULL, NULL
5c6c315f
MK
4432#define IPV4(f) #f, wpa_global_config_parse_ipv4, wpa_config_get_ipv4, \
4433 OFFSET(f), NULL, NULL
121adf9c
JM
4434
4435static const struct global_parse_data global_fields[] = {
4436#ifdef CONFIG_CTRL_IFACE
1d47214a 4437 { STR(ctrl_interface), 0 },
ea61aa1d 4438 { FUNC_NO_VAR(no_ctrl_interface), 0 },
1d47214a 4439 { STR(ctrl_interface_group), 0 } /* deprecated */,
121adf9c 4440#endif /* CONFIG_CTRL_IFACE */
0836c04b
HW
4441#ifdef CONFIG_MACSEC
4442 { INT_RANGE(eapol_version, 1, 3), 0 },
4443#else /* CONFIG_MACSEC */
1d47214a 4444 { INT_RANGE(eapol_version, 1, 2), 0 },
0836c04b 4445#endif /* CONFIG_MACSEC */
1d47214a 4446 { INT(ap_scan), 0 },
31392709 4447 { FUNC(bgscan), 0 },
e45e8989
TP
4448#ifdef CONFIG_MESH
4449 { INT(user_mpm), 0 },
4b409368 4450 { INT_RANGE(max_peer_links, 0, 255), 0 },
5a2a6de6 4451 { INT(mesh_max_inactivity), 0 },
ecd40fef 4452 { INT(dot11RSNASAERetransPeriod), 0 },
e45e8989 4453#endif /* CONFIG_MESH */
54ddd743 4454 { INT(disable_scan_offload), 0 },
1d47214a
JM
4455 { INT(fast_reauth), 0 },
4456 { STR(opensc_engine_path), 0 },
4457 { STR(pkcs11_engine_path), 0 },
4458 { STR(pkcs11_module_path), 0 },
07e2de31 4459 { STR(openssl_ciphers), 0 },
f64adcd7
JM
4460 { STR(pcsc_reader), 0 },
4461 { STR(pcsc_pin), 0 },
a5d44ac0 4462 { INT(external_sim), 0 },
1d47214a
JM
4463 { STR(driver_param), 0 },
4464 { INT(dot11RSNAConfigPMKLifetime), 0 },
4465 { INT(dot11RSNAConfigPMKReauthThreshold), 0 },
4466 { INT(dot11RSNAConfigSATimeout), 0 },
121adf9c 4467#ifndef CONFIG_NO_CONFIG_WRITE
1d47214a 4468 { INT(update_config), 0 },
121adf9c 4469#endif /* CONFIG_NO_CONFIG_WRITE */
1d47214a 4470 { FUNC_NO_VAR(load_dynamic_eap), 0 },
121adf9c 4471#ifdef CONFIG_WPS
1d47214a 4472 { FUNC(uuid), CFG_CHANGED_UUID },
183d3924 4473 { INT_RANGE(auto_uuid, 0, 1), 0 },
cc6f2438
JM
4474 { STR_RANGE(device_name, 0, WPS_DEV_NAME_MAX_LEN),
4475 CFG_CHANGED_DEVICE_NAME },
1c9cb49f
JM
4476 { STR_RANGE(manufacturer, 0, 64), CFG_CHANGED_WPS_STRING },
4477 { STR_RANGE(model_name, 0, 32), CFG_CHANGED_WPS_STRING },
4478 { STR_RANGE(model_number, 0, 32), CFG_CHANGED_WPS_STRING },
4479 { STR_RANGE(serial_number, 0, 32), CFG_CHANGED_WPS_STRING },
2f646b6e 4480 { FUNC(device_type), CFG_CHANGED_DEVICE_TYPE },
1d47214a
JM
4481 { FUNC(os_version), CFG_CHANGED_OS_VERSION },
4482 { STR(config_methods), CFG_CHANGED_CONFIG_METHODS },
4483 { INT_RANGE(wps_cred_processing, 0, 2), 0 },
71dd3b78 4484 { FUNC(wps_vendor_ext_m1), CFG_CHANGED_VENDOR_EXTENSION },
121adf9c 4485#endif /* CONFIG_WPS */
e3768e7c
JM
4486#ifdef CONFIG_P2P
4487 { FUNC(sec_device_type), CFG_CHANGED_SEC_DEVICE_TYPE },
86e2dcf5
PK
4488 { INT(p2p_listen_reg_class), CFG_CHANGED_P2P_LISTEN_CHANNEL },
4489 { INT(p2p_listen_channel), CFG_CHANGED_P2P_LISTEN_CHANNEL },
00eb2993
JM
4490 { INT(p2p_oper_reg_class), CFG_CHANGED_P2P_OPER_CHANNEL },
4491 { INT(p2p_oper_channel), CFG_CHANGED_P2P_OPER_CHANNEL },
e3768e7c
JM
4492 { INT_RANGE(p2p_go_intent, 0, 15), 0 },
4493 { STR(p2p_ssid_postfix), CFG_CHANGED_P2P_SSID_POSTFIX },
4494 { INT_RANGE(persistent_reconnect, 0, 1), 0 },
0f66abd2 4495 { INT_RANGE(p2p_intra_bss, 0, 1), CFG_CHANGED_P2P_INTRA_BSS },
3071e181 4496 { INT(p2p_group_idle), 0 },
1a471ff0 4497 { INT_RANGE(p2p_go_freq_change_policy, 0, P2P_GO_FREQ_MOVE_MAX), 0 },
1b928f96
JM
4498 { INT_RANGE(p2p_passphrase_len, 8, 63),
4499 CFG_CHANGED_P2P_PASSPHRASE_LEN },
21d996f7 4500 { FUNC(p2p_pref_chan), CFG_CHANGED_P2P_PREF_CHAN },
556b30da 4501 { FUNC(p2p_no_go_freq), CFG_CHANGED_P2P_PREF_CHAN },
51e9f228 4502 { INT_RANGE(p2p_add_cli_chan, 0, 1), 0 },
e3bd6e9d 4503 { INT_RANGE(p2p_optimize_listen_chan, 0, 1), 0 },
a93a15bb 4504 { INT(p2p_go_ht40), 0 },
20ea1ca4 4505 { INT(p2p_go_vht), 0 },
7a808c7e 4506 { INT(p2p_disabled), 0 },
0b8bcaa5 4507 { INT_RANGE(p2p_go_ctwindow, 0, 127), 0 },
d76cd41a 4508 { INT(p2p_no_group_iface), 0 },
b277a2be 4509 { INT_RANGE(p2p_ignore_shared_freq, 0, 1), 0 },
25ef8529
JM
4510 { IPV4(ip_addr_go), 0 },
4511 { IPV4(ip_addr_mask), 0 },
4512 { IPV4(ip_addr_start), 0 },
4513 { IPV4(ip_addr_end), 0 },
07c1e987 4514 { INT_RANGE(p2p_cli_probe, 0, 1), 0 },
e3768e7c 4515#endif /* CONFIG_P2P */
1d47214a
JM
4516 { FUNC(country), CFG_CHANGED_COUNTRY },
4517 { INT(bss_max_count), 0 },
78633c37
SL
4518 { INT(bss_expiration_age), 0 },
4519 { INT(bss_expiration_scan_count), 0 },
dae608d5 4520 { INT_RANGE(filter_ssids, 0, 1), 0 },
bf8d6d24 4521 { INT_RANGE(filter_rssi, -100, 0), 0 },
0d7e5a3a 4522 { INT(max_num_sta), 0 },
46ee0427 4523 { INT_RANGE(disassoc_low_ack, 0, 1), 0 },
66aadbd7
JK
4524#ifdef CONFIG_HS20
4525 { INT_RANGE(hs20, 0, 1), 0 },
4526#endif /* CONFIG_HS20 */
46ee0427 4527 { INT_RANGE(interworking, 0, 1), 0 },
11540c0b 4528 { FUNC(hessid), 0 },
1298c145 4529 { INT_RANGE(access_network_type, 0, 15), 0 },
b0786fba 4530 { INT_RANGE(pbc_in_m1, 0, 1), 0 },
3f2c8ba6 4531 { STR(autoscan), 0 },
042ec551
JM
4532 { INT_RANGE(wps_nfc_dev_pw_id, 0x10, 0xffff),
4533 CFG_CHANGED_NFC_PASSWORD_TOKEN },
4534 { BIN(wps_nfc_dh_pubkey), CFG_CHANGED_NFC_PASSWORD_TOKEN },
4535 { BIN(wps_nfc_dh_privkey), CFG_CHANGED_NFC_PASSWORD_TOKEN },
4536 { BIN(wps_nfc_dev_pw), CFG_CHANGED_NFC_PASSWORD_TOKEN },
462a7439
ES
4537 { STR(ext_password_backend), CFG_CHANGED_EXT_PW_BACKEND },
4538 { INT(p2p_go_max_inactivity), 0 },
4d5bda5f 4539 { INT_RANGE(auto_interworking, 0, 1), 0 },
6e202021 4540 { INT(okc), 0 },
62d49803 4541 { INT(pmf), 0 },
625f202a 4542 { FUNC(sae_groups), 0 },
18206e02
JM
4543 { INT(dtim_period), 0 },
4544 { INT(beacon_int), 0 },
18a2eaab 4545 { FUNC(ap_vendor_elements), 0 },
4342326f 4546 { INT_RANGE(ignore_old_scan_res, 0, 1), 0 },
f5ffc348 4547 { FUNC(freq_list), 0 },
6124e858 4548 { INT(scan_cur_freq), 0 },
4aa81868 4549 { INT(sched_scan_interval), 0 },
d0330d57 4550 { INT(sched_scan_start_delay), 0 },
800d5872 4551 { INT(tdls_external_control), 0},
b572df86 4552 { STR(osu_dir), 0 },
3c7863f8 4553 { STR(wowlan_triggers), CFG_CHANGED_WOWLAN_TRIGGERS },
d3b20469 4554 { INT(p2p_search_delay), 0},
c267753b
JM
4555 { INT(mac_addr), 0 },
4556 { INT(rand_addr_lifetime), 0 },
4557 { INT(preassoc_mac_addr), 0 },
b41f2684 4558 { INT(key_mgmt_offload), 0},
c35e35ed 4559 { INT(passive_scan), 0 },
59b416c7 4560 { INT(reassoc_same_bss_optim), 0 },
94687a0a 4561 { INT(wps_priority), 0},
76ca15b7
AN
4562#ifdef CONFIG_FST
4563 { STR_RANGE(fst_group_id, 1, FST_MAX_GROUP_ID_LEN), 0 },
4564 { INT_RANGE(fst_priority, 1, FST_MAX_PRIO_VALUE), 0 },
4565 { INT_RANGE(fst_llt, 1, FST_MAX_LLT_MS), 0 },
4566#endif /* CONFIG_FST */
f8c20186 4567 { INT_RANGE(cert_in_cb, 0, 1), 0 },
73ed03f3 4568 { INT_RANGE(wpa_rsc_relaxation, 0, 1), 0 },
bea48f77 4569 { STR(sched_scan_plans), CFG_CHANGED_SCHED_SCAN_PLANS },
facf2c72
DS
4570#ifdef CONFIG_MBO
4571 { STR(non_pref_chan), 0 },
c5d193d7
DS
4572 { INT_RANGE(mbo_cell_capa, MBO_CELL_CAPA_AVAILABLE,
4573 MBO_CELL_CAPA_NOT_SUPPORTED), 0 },
af8bc24d 4574 { INT_RANGE(disassoc_imminent_rssi_threshold, -120, 0), 0 },
332aadb8 4575 { INT_RANGE(oce, 0, 3), 0 },
6d49aeb7 4576#endif /* CONFIG_MBO */
c86bef29 4577 { INT(gas_address3), 0 },
d1723c55
LD
4578 { INT_RANGE(ftm_responder, 0, 1), 0 },
4579 { INT_RANGE(ftm_initiator, 0, 1), 0 },
1d9d21f3
VK
4580 { INT(gas_rand_addr_lifetime), 0 },
4581 { INT_RANGE(gas_rand_mac_addr, 0, 2), 0 },
8528994e 4582 { INT_RANGE(dpp_config_processing, 0, 2), 0 },
121adf9c
JM
4583};
4584
4585#undef FUNC
4586#undef _INT
4587#undef INT
4588#undef INT_RANGE
4589#undef _STR
4590#undef STR
4591#undef STR_RANGE
3f2c8ba6 4592#undef BIN
25ef8529 4593#undef IPV4
e7ecab4a 4594#define NUM_GLOBAL_FIELDS ARRAY_SIZE(global_fields)
121adf9c
JM
4595
4596
10263dc2
OO
4597int wpa_config_dump_values(struct wpa_config *config, char *buf, size_t buflen)
4598{
4599 int result = 0;
4600 size_t i;
4601
4602 for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
4603 const struct global_parse_data *field = &global_fields[i];
4604 int tmp;
4605
4606 if (!field->get)
4607 continue;
4608
4609 tmp = field->get(field->name, config, (long) field->param1,
4610 buf, buflen, 1);
4611 if (tmp < 0)
4612 return -1;
4613 buf += tmp;
4614 buflen -= tmp;
4615 result += tmp;
4616 }
4617 return result;
4618}
4619
4620
4621int wpa_config_get_value(const char *name, struct wpa_config *config,
4622 char *buf, size_t buflen)
4623{
4624 size_t i;
4625
4626 for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
4627 const struct global_parse_data *field = &global_fields[i];
4628
4629 if (os_strcmp(name, field->name) != 0)
4630 continue;
4631 if (!field->get)
4632 break;
4633 return field->get(name, config, (long) field->param1,
4634 buf, buflen, 0);
4635 }
4636
4637 return -1;
4638}
4639
4640
e50c50d5
DW
4641int wpa_config_get_num_global_field_names(void)
4642{
4643 return NUM_GLOBAL_FIELDS;
4644}
4645
4646
4647const char * wpa_config_get_global_field_name(unsigned int i, int *no_var)
4648{
4649 if (i >= NUM_GLOBAL_FIELDS)
4650 return NULL;
4651
4652 if (no_var)
4653 *no_var = !global_fields[i].param1;
4654 return global_fields[i].name;
4655}
4656
4657
121adf9c
JM
4658int wpa_config_process_global(struct wpa_config *config, char *pos, int line)
4659{
4660 size_t i;
4661 int ret = 0;
4662
4663 for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
4664 const struct global_parse_data *field = &global_fields[i];
4665 size_t flen = os_strlen(field->name);
4666 if (os_strncmp(pos, field->name, flen) != 0 ||
4667 pos[flen] != '=')
4668 continue;
4669
4670 if (field->parser(field, config, line, pos + flen + 1)) {
4671 wpa_printf(MSG_ERROR, "Line %d: failed to "
4672 "parse '%s'.", line, pos);
4673 ret = -1;
4674 }
042ec551
JM
4675 if (field->changed_flag == CFG_CHANGED_NFC_PASSWORD_TOKEN)
4676 config->wps_nfc_pw_from_config = 1;
1d47214a 4677 config->changed_parameters |= field->changed_flag;
121adf9c
JM
4678 break;
4679 }
4680 if (i == NUM_GLOBAL_FIELDS) {
c26effe1
YD
4681#ifdef CONFIG_AP
4682 if (os_strncmp(pos, "wmm_ac_", 7) == 0) {
4683 char *tmp = os_strchr(pos, '=');
4684 if (tmp == NULL) {
4685 if (line < 0)
4686 return -1;
4687 wpa_printf(MSG_ERROR, "Line %d: invalid line "
4688 "'%s'", line, pos);
4689 return -1;
4690 }
4691 *tmp++ = '\0';
4692 if (hostapd_config_wmm_ac(config->wmm_ac_params, pos,
4693 tmp)) {
4694 wpa_printf(MSG_ERROR, "Line %d: invalid WMM "
4695 "AC item", line);
4696 return -1;
4697 }
4698 }
4699#endif /* CONFIG_AP */
1d47214a
JM
4700 if (line < 0)
4701 return -1;
121adf9c
JM
4702 wpa_printf(MSG_ERROR, "Line %d: unknown global field '%s'.",
4703 line, pos);
4704 ret = -1;
4705 }
4706
4707 return ret;
4708}