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