]> git.ipfire.org Git - thirdparty/hostap.git/blame - wpa_supplicant/config.c
wpa_supplicant: Fix wrong size memory allocation
[thirdparty/hostap.git] / wpa_supplicant / config.c
CommitLineData
6fc6879b
JM
1/*
2 * WPA Supplicant / Configuration parser and common functions
f64adcd7 3 * Copyright (c) 2003-2012, 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"
6fc6879b
JM
18#include "config.h"
19
20
21#if !defined(CONFIG_CTRL_IFACE) && defined(CONFIG_NO_CONFIG_WRITE)
22#define NO_CONFIG_WRITE
23#endif
24
25/*
26 * Structure for network configuration parsing. This data is used to implement
27 * a generic parser for each network block variable. The table of configuration
28 * variables is defined below in this file (ssid_fields[]).
29 */
30struct parse_data {
31 /* Configuration variable name */
32 char *name;
33
34 /* Parser function for this variable */
35 int (*parser)(const struct parse_data *data, struct wpa_ssid *ssid,
36 int line, const char *value);
37
38#ifndef NO_CONFIG_WRITE
39 /* Writer function (i.e., to get the variable in text format from
40 * internal presentation). */
41 char * (*writer)(const struct parse_data *data, struct wpa_ssid *ssid);
42#endif /* NO_CONFIG_WRITE */
43
44 /* Variable specific parameters for the parser. */
45 void *param1, *param2, *param3, *param4;
46
47 /* 0 = this variable can be included in debug output and ctrl_iface
48 * 1 = this variable contains key/private data and it must not be
49 * included in debug output unless explicitly requested. In
50 * addition, this variable will not be readable through the
51 * ctrl_iface.
52 */
53 int key_data;
54};
55
56
6fc6879b
JM
57static int wpa_config_parse_str(const struct parse_data *data,
58 struct wpa_ssid *ssid,
59 int line, const char *value)
60{
61 size_t res_len, *dst_len;
62 char **dst, *tmp;
63
b56c0546
JM
64 if (os_strcmp(value, "NULL") == 0) {
65 wpa_printf(MSG_DEBUG, "Unset configuration string '%s'",
66 data->name);
67 tmp = NULL;
68 res_len = 0;
69 goto set;
70 }
71
6fc6879b
JM
72 tmp = wpa_config_parse_string(value, &res_len);
73 if (tmp == NULL) {
74 wpa_printf(MSG_ERROR, "Line %d: failed to parse %s '%s'.",
75 line, data->name,
76 data->key_data ? "[KEY DATA REMOVED]" : value);
77 return -1;
78 }
79
80 if (data->key_data) {
81 wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
82 (u8 *) tmp, res_len);
83 } else {
84 wpa_hexdump_ascii(MSG_MSGDUMP, data->name,
85 (u8 *) tmp, res_len);
86 }
87
88 if (data->param3 && res_len < (size_t) data->param3) {
89 wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
90 "min_len=%ld)", line, data->name,
91 (unsigned long) res_len, (long) data->param3);
92 os_free(tmp);
93 return -1;
94 }
95
96 if (data->param4 && res_len > (size_t) data->param4) {
97 wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
98 "max_len=%ld)", line, data->name,
99 (unsigned long) res_len, (long) data->param4);
100 os_free(tmp);
101 return -1;
102 }
103
b56c0546 104set:
6fc6879b
JM
105 dst = (char **) (((u8 *) ssid) + (long) data->param1);
106 dst_len = (size_t *) (((u8 *) ssid) + (long) data->param2);
107 os_free(*dst);
108 *dst = tmp;
109 if (data->param2)
110 *dst_len = res_len;
111
112 return 0;
113}
114
115
116#ifndef NO_CONFIG_WRITE
6fc6879b
JM
117static char * wpa_config_write_string_ascii(const u8 *value, size_t len)
118{
119 char *buf;
120
121 buf = os_malloc(len + 3);
122 if (buf == NULL)
123 return NULL;
124 buf[0] = '"';
125 os_memcpy(buf + 1, value, len);
126 buf[len + 1] = '"';
127 buf[len + 2] = '\0';
128
129 return buf;
130}
131
132
133static char * wpa_config_write_string_hex(const u8 *value, size_t len)
134{
135 char *buf;
136
137 buf = os_zalloc(2 * len + 1);
138 if (buf == NULL)
139 return NULL;
140 wpa_snprintf_hex(buf, 2 * len + 1, value, len);
141
142 return buf;
143}
144
145
146static char * wpa_config_write_string(const u8 *value, size_t len)
147{
148 if (value == NULL)
149 return NULL;
150
151 if (is_hex(value, len))
152 return wpa_config_write_string_hex(value, len);
153 else
154 return wpa_config_write_string_ascii(value, len);
155}
156
157
158static char * wpa_config_write_str(const struct parse_data *data,
159 struct wpa_ssid *ssid)
160{
161 size_t len;
162 char **src;
163
164 src = (char **) (((u8 *) ssid) + (long) data->param1);
165 if (*src == NULL)
166 return NULL;
167
168 if (data->param2)
169 len = *((size_t *) (((u8 *) ssid) + (long) data->param2));
170 else
171 len = os_strlen(*src);
172
173 return wpa_config_write_string((const u8 *) *src, len);
174}
175#endif /* NO_CONFIG_WRITE */
176
177
178static int wpa_config_parse_int(const struct parse_data *data,
179 struct wpa_ssid *ssid,
180 int line, const char *value)
181{
eae3a584
JB
182 int val, *dst;
183 char *end;
6fc6879b
JM
184
185 dst = (int *) (((u8 *) ssid) + (long) data->param1);
eae3a584
JB
186 val = strtol(value, &end, 0);
187 if (*end) {
188 wpa_printf(MSG_ERROR, "Line %d: invalid number \"%s\"",
189 line, value);
190 return -1;
191 }
192 *dst = val;
6fc6879b
JM
193 wpa_printf(MSG_MSGDUMP, "%s=%d (0x%x)", data->name, *dst, *dst);
194
195 if (data->param3 && *dst < (long) data->param3) {
196 wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
197 "min_value=%ld)", line, data->name, *dst,
198 (long) data->param3);
199 *dst = (long) data->param3;
200 return -1;
201 }
202
203 if (data->param4 && *dst > (long) data->param4) {
204 wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
205 "max_value=%ld)", line, data->name, *dst,
206 (long) data->param4);
207 *dst = (long) data->param4;
208 return -1;
209 }
210
211 return 0;
212}
213
214
215#ifndef NO_CONFIG_WRITE
216static char * wpa_config_write_int(const struct parse_data *data,
217 struct wpa_ssid *ssid)
218{
219 int *src, res;
220 char *value;
221
222 src = (int *) (((u8 *) ssid) + (long) data->param1);
223
224 value = os_malloc(20);
225 if (value == NULL)
226 return NULL;
227 res = os_snprintf(value, 20, "%d", *src);
228 if (res < 0 || res >= 20) {
229 os_free(value);
230 return NULL;
231 }
232 value[20 - 1] = '\0';
233 return value;
234}
235#endif /* NO_CONFIG_WRITE */
236
237
238static int wpa_config_parse_bssid(const struct parse_data *data,
239 struct wpa_ssid *ssid, int line,
240 const char *value)
241{
c0a321c5
WJL
242 if (value[0] == '\0' || os_strcmp(value, "\"\"") == 0 ||
243 os_strcmp(value, "any") == 0) {
244 ssid->bssid_set = 0;
245 wpa_printf(MSG_MSGDUMP, "BSSID any");
246 return 0;
247 }
6fc6879b
JM
248 if (hwaddr_aton(value, ssid->bssid)) {
249 wpa_printf(MSG_ERROR, "Line %d: Invalid BSSID '%s'.",
250 line, value);
251 return -1;
252 }
253 ssid->bssid_set = 1;
254 wpa_hexdump(MSG_MSGDUMP, "BSSID", ssid->bssid, ETH_ALEN);
255 return 0;
256}
257
258
259#ifndef NO_CONFIG_WRITE
260static char * wpa_config_write_bssid(const struct parse_data *data,
261 struct wpa_ssid *ssid)
262{
263 char *value;
264 int res;
265
266 if (!ssid->bssid_set)
267 return NULL;
268
269 value = os_malloc(20);
270 if (value == NULL)
271 return NULL;
272 res = os_snprintf(value, 20, MACSTR, MAC2STR(ssid->bssid));
273 if (res < 0 || res >= 20) {
274 os_free(value);
275 return NULL;
276 }
277 value[20 - 1] = '\0';
278 return value;
279}
280#endif /* NO_CONFIG_WRITE */
281
282
283static int wpa_config_parse_psk(const struct parse_data *data,
284 struct wpa_ssid *ssid, int line,
285 const char *value)
286{
9173b16f
JM
287#ifdef CONFIG_EXT_PASSWORD
288 if (os_strncmp(value, "ext:", 4) == 0) {
289 os_free(ssid->passphrase);
290 ssid->passphrase = NULL;
291 ssid->psk_set = 0;
292 os_free(ssid->ext_psk);
293 ssid->ext_psk = os_strdup(value + 4);
294 if (ssid->ext_psk == NULL)
295 return -1;
296 wpa_printf(MSG_DEBUG, "PSK: External password '%s'",
297 ssid->ext_psk);
298 return 0;
299 }
300#endif /* CONFIG_EXT_PASSWORD */
301
6fc6879b
JM
302 if (*value == '"') {
303#ifndef CONFIG_NO_PBKDF2
304 const char *pos;
305 size_t len;
306
307 value++;
308 pos = os_strrchr(value, '"');
309 if (pos)
310 len = pos - value;
311 else
312 len = os_strlen(value);
313 if (len < 8 || len > 63) {
314 wpa_printf(MSG_ERROR, "Line %d: Invalid passphrase "
315 "length %lu (expected: 8..63) '%s'.",
316 line, (unsigned long) len, value);
317 return -1;
318 }
319 wpa_hexdump_ascii_key(MSG_MSGDUMP, "PSK (ASCII passphrase)",
320 (u8 *) value, len);
321 if (ssid->passphrase && os_strlen(ssid->passphrase) == len &&
322 os_memcmp(ssid->passphrase, value, len) == 0)
323 return 0;
324 ssid->psk_set = 0;
325 os_free(ssid->passphrase);
5e24dc8a 326 ssid->passphrase = dup_binstr(value, len);
6fc6879b
JM
327 if (ssid->passphrase == NULL)
328 return -1;
6fc6879b
JM
329 return 0;
330#else /* CONFIG_NO_PBKDF2 */
331 wpa_printf(MSG_ERROR, "Line %d: ASCII passphrase not "
332 "supported.", line);
333 return -1;
334#endif /* CONFIG_NO_PBKDF2 */
335 }
336
337 if (hexstr2bin(value, ssid->psk, PMK_LEN) ||
338 value[PMK_LEN * 2] != '\0') {
339 wpa_printf(MSG_ERROR, "Line %d: Invalid PSK '%s'.",
340 line, value);
341 return -1;
342 }
343
344 os_free(ssid->passphrase);
345 ssid->passphrase = NULL;
346
347 ssid->psk_set = 1;
348 wpa_hexdump_key(MSG_MSGDUMP, "PSK", ssid->psk, PMK_LEN);
349 return 0;
350}
351
352
353#ifndef NO_CONFIG_WRITE
354static char * wpa_config_write_psk(const struct parse_data *data,
355 struct wpa_ssid *ssid)
356{
9173b16f
JM
357#ifdef CONFIG_EXT_PASSWORD
358 if (ssid->ext_psk) {
359 size_t len = 4 + os_strlen(ssid->ext_psk) + 1;
360 char *buf = os_malloc(len);
361 if (buf == NULL)
362 return NULL;
363 os_snprintf(buf, len, "ext:%s", ssid->ext_psk);
364 return buf;
365 }
366#endif /* CONFIG_EXT_PASSWORD */
367
6fc6879b
JM
368 if (ssid->passphrase)
369 return wpa_config_write_string_ascii(
370 (const u8 *) ssid->passphrase,
371 os_strlen(ssid->passphrase));
372
373 if (ssid->psk_set)
374 return wpa_config_write_string_hex(ssid->psk, PMK_LEN);
375
376 return NULL;
377}
378#endif /* NO_CONFIG_WRITE */
379
380
381static int wpa_config_parse_proto(const struct parse_data *data,
382 struct wpa_ssid *ssid, int line,
383 const char *value)
384{
385 int val = 0, last, errors = 0;
386 char *start, *end, *buf;
387
388 buf = os_strdup(value);
389 if (buf == NULL)
390 return -1;
391 start = buf;
392
393 while (*start != '\0') {
394 while (*start == ' ' || *start == '\t')
395 start++;
396 if (*start == '\0')
397 break;
398 end = start;
399 while (*end != ' ' && *end != '\t' && *end != '\0')
400 end++;
401 last = *end == '\0';
402 *end = '\0';
403 if (os_strcmp(start, "WPA") == 0)
404 val |= WPA_PROTO_WPA;
405 else if (os_strcmp(start, "RSN") == 0 ||
406 os_strcmp(start, "WPA2") == 0)
407 val |= WPA_PROTO_RSN;
408 else {
409 wpa_printf(MSG_ERROR, "Line %d: invalid proto '%s'",
410 line, start);
411 errors++;
412 }
413
414 if (last)
415 break;
416 start = end + 1;
417 }
418 os_free(buf);
419
420 if (val == 0) {
421 wpa_printf(MSG_ERROR,
422 "Line %d: no proto values configured.", line);
423 errors++;
424 }
425
426 wpa_printf(MSG_MSGDUMP, "proto: 0x%x", val);
427 ssid->proto = val;
428 return errors ? -1 : 0;
429}
430
431
432#ifndef NO_CONFIG_WRITE
433static char * wpa_config_write_proto(const struct parse_data *data,
434 struct wpa_ssid *ssid)
435{
436 int first = 1, ret;
437 char *buf, *pos, *end;
438
439 pos = buf = os_zalloc(10);
440 if (buf == NULL)
441 return NULL;
442 end = buf + 10;
443
444 if (ssid->proto & WPA_PROTO_WPA) {
445 ret = os_snprintf(pos, end - pos, "%sWPA", first ? "" : " ");
446 if (ret < 0 || ret >= end - pos)
447 return buf;
448 pos += ret;
449 first = 0;
450 }
451
452 if (ssid->proto & WPA_PROTO_RSN) {
453 ret = os_snprintf(pos, end - pos, "%sRSN", first ? "" : " ");
454 if (ret < 0 || ret >= end - pos)
455 return buf;
456 pos += ret;
457 first = 0;
458 }
459
460 return buf;
461}
462#endif /* NO_CONFIG_WRITE */
463
464
465static int wpa_config_parse_key_mgmt(const struct parse_data *data,
466 struct wpa_ssid *ssid, int line,
467 const char *value)
468{
469 int val = 0, last, errors = 0;
470 char *start, *end, *buf;
471
472 buf = os_strdup(value);
473 if (buf == NULL)
474 return -1;
475 start = buf;
476
477 while (*start != '\0') {
478 while (*start == ' ' || *start == '\t')
479 start++;
480 if (*start == '\0')
481 break;
482 end = start;
483 while (*end != ' ' && *end != '\t' && *end != '\0')
484 end++;
485 last = *end == '\0';
486 *end = '\0';
487 if (os_strcmp(start, "WPA-PSK") == 0)
488 val |= WPA_KEY_MGMT_PSK;
489 else if (os_strcmp(start, "WPA-EAP") == 0)
490 val |= WPA_KEY_MGMT_IEEE8021X;
491 else if (os_strcmp(start, "IEEE8021X") == 0)
492 val |= WPA_KEY_MGMT_IEEE8021X_NO_WPA;
493 else if (os_strcmp(start, "NONE") == 0)
494 val |= WPA_KEY_MGMT_NONE;
495 else if (os_strcmp(start, "WPA-NONE") == 0)
496 val |= WPA_KEY_MGMT_WPA_NONE;
497#ifdef CONFIG_IEEE80211R
498 else if (os_strcmp(start, "FT-PSK") == 0)
499 val |= WPA_KEY_MGMT_FT_PSK;
500 else if (os_strcmp(start, "FT-EAP") == 0)
501 val |= WPA_KEY_MGMT_FT_IEEE8021X;
502#endif /* CONFIG_IEEE80211R */
56586197
JM
503#ifdef CONFIG_IEEE80211W
504 else if (os_strcmp(start, "WPA-PSK-SHA256") == 0)
505 val |= WPA_KEY_MGMT_PSK_SHA256;
506 else if (os_strcmp(start, "WPA-EAP-SHA256") == 0)
507 val |= WPA_KEY_MGMT_IEEE8021X_SHA256;
508#endif /* CONFIG_IEEE80211W */
ad08c363
JM
509#ifdef CONFIG_WPS
510 else if (os_strcmp(start, "WPS") == 0)
511 val |= WPA_KEY_MGMT_WPS;
512#endif /* CONFIG_WPS */
c10347f2
JM
513#ifdef CONFIG_SAE
514 else if (os_strcmp(start, "SAE") == 0)
515 val |= WPA_KEY_MGMT_SAE;
516 else if (os_strcmp(start, "FT-SAE") == 0)
517 val |= WPA_KEY_MGMT_FT_SAE;
518#endif /* CONFIG_SAE */
6fc6879b
JM
519 else {
520 wpa_printf(MSG_ERROR, "Line %d: invalid key_mgmt '%s'",
521 line, start);
522 errors++;
523 }
524
525 if (last)
526 break;
527 start = end + 1;
528 }
529 os_free(buf);
530
531 if (val == 0) {
532 wpa_printf(MSG_ERROR,
533 "Line %d: no key_mgmt values configured.", line);
534 errors++;
535 }
536
537 wpa_printf(MSG_MSGDUMP, "key_mgmt: 0x%x", val);
538 ssid->key_mgmt = val;
539 return errors ? -1 : 0;
540}
541
542
543#ifndef NO_CONFIG_WRITE
544static char * wpa_config_write_key_mgmt(const struct parse_data *data,
545 struct wpa_ssid *ssid)
546{
547 char *buf, *pos, *end;
548 int ret;
549
50793929 550 pos = buf = os_zalloc(100);
6fc6879b
JM
551 if (buf == NULL)
552 return NULL;
50793929 553 end = buf + 100;
6fc6879b
JM
554
555 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
556 ret = os_snprintf(pos, end - pos, "%sWPA-PSK",
557 pos == buf ? "" : " ");
558 if (ret < 0 || ret >= end - pos) {
559 end[-1] = '\0';
560 return buf;
561 }
562 pos += ret;
563 }
564
565 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
566 ret = os_snprintf(pos, end - pos, "%sWPA-EAP",
567 pos == buf ? "" : " ");
568 if (ret < 0 || ret >= end - pos) {
569 end[-1] = '\0';
570 return buf;
571 }
572 pos += ret;
573 }
574
575 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
576 ret = os_snprintf(pos, end - pos, "%sIEEE8021X",
577 pos == buf ? "" : " ");
578 if (ret < 0 || ret >= end - pos) {
579 end[-1] = '\0';
580 return buf;
581 }
582 pos += ret;
583 }
584
585 if (ssid->key_mgmt & WPA_KEY_MGMT_NONE) {
586 ret = os_snprintf(pos, end - pos, "%sNONE",
587 pos == buf ? "" : " ");
588 if (ret < 0 || ret >= end - pos) {
589 end[-1] = '\0';
590 return buf;
591 }
592 pos += ret;
593 }
594
595 if (ssid->key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
596 ret = os_snprintf(pos, end - pos, "%sWPA-NONE",
597 pos == buf ? "" : " ");
598 if (ret < 0 || ret >= end - pos) {
599 end[-1] = '\0';
600 return buf;
601 }
602 pos += ret;
603 }
604
605#ifdef CONFIG_IEEE80211R
50793929
PF
606 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_PSK) {
607 ret = os_snprintf(pos, end - pos, "%sFT-PSK",
608 pos == buf ? "" : " ");
609 if (ret < 0 || ret >= end - pos) {
610 end[-1] = '\0';
611 return buf;
612 }
613 pos += ret;
614 }
6fc6879b 615
50793929
PF
616 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
617 ret = os_snprintf(pos, end - pos, "%sFT-EAP",
618 pos == buf ? "" : " ");
619 if (ret < 0 || ret >= end - pos) {
620 end[-1] = '\0';
621 return buf;
622 }
623 pos += ret;
624 }
6fc6879b
JM
625#endif /* CONFIG_IEEE80211R */
626
56586197 627#ifdef CONFIG_IEEE80211W
50793929
PF
628 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
629 ret = os_snprintf(pos, end - pos, "%sWPA-PSK-SHA256",
630 pos == buf ? "" : " ");
631 if (ret < 0 || ret >= end - pos) {
632 end[-1] = '\0';
633 return buf;
634 }
635 pos += ret;
636 }
56586197 637
50793929
PF
638 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
639 ret = os_snprintf(pos, end - pos, "%sWPA-EAP-SHA256",
640 pos == buf ? "" : " ");
641 if (ret < 0 || ret >= end - pos) {
642 end[-1] = '\0';
643 return buf;
644 }
645 pos += ret;
646 }
56586197
JM
647#endif /* CONFIG_IEEE80211W */
648
728fae16 649#ifdef CONFIG_WPS
50793929
PF
650 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
651 ret = os_snprintf(pos, end - pos, "%sWPS",
652 pos == buf ? "" : " ");
653 if (ret < 0 || ret >= end - pos) {
654 end[-1] = '\0';
655 return buf;
656 }
657 pos += ret;
658 }
728fae16
JM
659#endif /* CONFIG_WPS */
660
6fc6879b
JM
661 return buf;
662}
663#endif /* NO_CONFIG_WRITE */
664
665
666static int wpa_config_parse_cipher(int line, const char *value)
667{
a39c78be
JM
668 int val = wpa_parse_cipher(value);
669 if (val < 0) {
670 wpa_printf(MSG_ERROR, "Line %d: invalid cipher '%s'.",
671 line, value);
6fc6879b 672 return -1;
6fc6879b 673 }
6fc6879b
JM
674 if (val == 0) {
675 wpa_printf(MSG_ERROR, "Line %d: no cipher values configured.",
676 line);
677 return -1;
678 }
679 return val;
680}
681
682
683#ifndef NO_CONFIG_WRITE
684static char * wpa_config_write_cipher(int cipher)
685{
0282a8c4 686 char *buf = os_zalloc(50);
6fc6879b
JM
687 if (buf == NULL)
688 return NULL;
6fc6879b 689
0282a8c4
JM
690 if (wpa_write_ciphers(buf, buf + 50, cipher, " ") < 0) {
691 os_free(buf);
692 return NULL;
6fc6879b
JM
693 }
694
695 return buf;
696}
697#endif /* NO_CONFIG_WRITE */
698
699
700static int wpa_config_parse_pairwise(const struct parse_data *data,
701 struct wpa_ssid *ssid, int line,
702 const char *value)
703{
704 int val;
705 val = wpa_config_parse_cipher(line, value);
706 if (val == -1)
707 return -1;
03145326 708 if (val & ~WPA_ALLOWED_PAIRWISE_CIPHERS) {
6fc6879b
JM
709 wpa_printf(MSG_ERROR, "Line %d: not allowed pairwise cipher "
710 "(0x%x).", line, val);
711 return -1;
712 }
713
714 wpa_printf(MSG_MSGDUMP, "pairwise: 0x%x", val);
715 ssid->pairwise_cipher = val;
716 return 0;
717}
718
719
720#ifndef NO_CONFIG_WRITE
721static char * wpa_config_write_pairwise(const struct parse_data *data,
722 struct wpa_ssid *ssid)
723{
724 return wpa_config_write_cipher(ssid->pairwise_cipher);
725}
726#endif /* NO_CONFIG_WRITE */
727
728
729static int wpa_config_parse_group(const struct parse_data *data,
730 struct wpa_ssid *ssid, int line,
731 const char *value)
732{
733 int val;
734 val = wpa_config_parse_cipher(line, value);
735 if (val == -1)
736 return -1;
03145326 737 if (val & ~WPA_ALLOWED_GROUP_CIPHERS) {
6fc6879b
JM
738 wpa_printf(MSG_ERROR, "Line %d: not allowed group cipher "
739 "(0x%x).", line, val);
740 return -1;
741 }
742
743 wpa_printf(MSG_MSGDUMP, "group: 0x%x", val);
744 ssid->group_cipher = val;
745 return 0;
746}
747
748
749#ifndef NO_CONFIG_WRITE
750static char * wpa_config_write_group(const struct parse_data *data,
751 struct wpa_ssid *ssid)
752{
753 return wpa_config_write_cipher(ssid->group_cipher);
754}
755#endif /* NO_CONFIG_WRITE */
756
757
758static int wpa_config_parse_auth_alg(const struct parse_data *data,
759 struct wpa_ssid *ssid, int line,
760 const char *value)
761{
762 int val = 0, last, errors = 0;
763 char *start, *end, *buf;
764
765 buf = os_strdup(value);
766 if (buf == NULL)
767 return -1;
768 start = buf;
769
770 while (*start != '\0') {
771 while (*start == ' ' || *start == '\t')
772 start++;
773 if (*start == '\0')
774 break;
775 end = start;
776 while (*end != ' ' && *end != '\t' && *end != '\0')
777 end++;
778 last = *end == '\0';
779 *end = '\0';
780 if (os_strcmp(start, "OPEN") == 0)
781 val |= WPA_AUTH_ALG_OPEN;
782 else if (os_strcmp(start, "SHARED") == 0)
783 val |= WPA_AUTH_ALG_SHARED;
784 else if (os_strcmp(start, "LEAP") == 0)
785 val |= WPA_AUTH_ALG_LEAP;
786 else {
787 wpa_printf(MSG_ERROR, "Line %d: invalid auth_alg '%s'",
788 line, start);
789 errors++;
790 }
791
792 if (last)
793 break;
794 start = end + 1;
795 }
796 os_free(buf);
797
798 if (val == 0) {
799 wpa_printf(MSG_ERROR,
800 "Line %d: no auth_alg values configured.", line);
801 errors++;
802 }
803
804 wpa_printf(MSG_MSGDUMP, "auth_alg: 0x%x", val);
805 ssid->auth_alg = val;
806 return errors ? -1 : 0;
807}
808
809
810#ifndef NO_CONFIG_WRITE
811static char * wpa_config_write_auth_alg(const struct parse_data *data,
812 struct wpa_ssid *ssid)
813{
814 char *buf, *pos, *end;
815 int ret;
816
817 pos = buf = os_zalloc(30);
818 if (buf == NULL)
819 return NULL;
820 end = buf + 30;
821
822 if (ssid->auth_alg & WPA_AUTH_ALG_OPEN) {
823 ret = os_snprintf(pos, end - pos, "%sOPEN",
824 pos == buf ? "" : " ");
825 if (ret < 0 || ret >= end - pos) {
826 end[-1] = '\0';
827 return buf;
828 }
829 pos += ret;
830 }
831
832 if (ssid->auth_alg & WPA_AUTH_ALG_SHARED) {
833 ret = os_snprintf(pos, end - pos, "%sSHARED",
834 pos == buf ? "" : " ");
835 if (ret < 0 || ret >= end - pos) {
836 end[-1] = '\0';
837 return buf;
838 }
839 pos += ret;
840 }
841
842 if (ssid->auth_alg & WPA_AUTH_ALG_LEAP) {
843 ret = os_snprintf(pos, end - pos, "%sLEAP",
844 pos == buf ? "" : " ");
845 if (ret < 0 || ret >= end - pos) {
846 end[-1] = '\0';
847 return buf;
848 }
849 pos += ret;
850 }
851
852 return buf;
853}
854#endif /* NO_CONFIG_WRITE */
855
856
625f202a 857static int * wpa_config_parse_int_array(const char *value)
d3a98225
JM
858{
859 int *freqs;
860 size_t used, len;
861 const char *pos;
862
863 used = 0;
864 len = 10;
f9884c09 865 freqs = os_calloc(len + 1, sizeof(int));
d3a98225 866 if (freqs == NULL)
b766a9a2 867 return NULL;
d3a98225
JM
868
869 pos = value;
870 while (pos) {
871 while (*pos == ' ')
872 pos++;
873 if (used == len) {
874 int *n;
875 size_t i;
067ffa26 876 n = os_realloc_array(freqs, len * 2 + 1, sizeof(int));
d3a98225
JM
877 if (n == NULL) {
878 os_free(freqs);
b766a9a2 879 return NULL;
d3a98225
JM
880 }
881 for (i = len; i <= len * 2; i++)
882 n[i] = 0;
883 freqs = n;
884 len *= 2;
885 }
886
887 freqs[used] = atoi(pos);
888 if (freqs[used] == 0)
889 break;
890 used++;
891 pos = os_strchr(pos + 1, ' ');
892 }
893
b766a9a2
JM
894 return freqs;
895}
896
897
898static int wpa_config_parse_scan_freq(const struct parse_data *data,
899 struct wpa_ssid *ssid, int line,
900 const char *value)
901{
902 int *freqs;
903
625f202a 904 freqs = wpa_config_parse_int_array(value);
b766a9a2
JM
905 if (freqs == NULL)
906 return -1;
6668efda
JM
907 if (freqs[0] == 0) {
908 os_free(freqs);
909 freqs = NULL;
910 }
d3a98225
JM
911 os_free(ssid->scan_freq);
912 ssid->scan_freq = freqs;
913
914 return 0;
915}
916
917
b766a9a2
JM
918static int wpa_config_parse_freq_list(const struct parse_data *data,
919 struct wpa_ssid *ssid, int line,
920 const char *value)
921{
922 int *freqs;
923
625f202a 924 freqs = wpa_config_parse_int_array(value);
b766a9a2
JM
925 if (freqs == NULL)
926 return -1;
6668efda
JM
927 if (freqs[0] == 0) {
928 os_free(freqs);
929 freqs = NULL;
930 }
b766a9a2
JM
931 os_free(ssid->freq_list);
932 ssid->freq_list = freqs;
933
934 return 0;
935}
936
937
d3a98225 938#ifndef NO_CONFIG_WRITE
b766a9a2
JM
939static char * wpa_config_write_freqs(const struct parse_data *data,
940 const int *freqs)
d3a98225
JM
941{
942 char *buf, *pos, *end;
943 int i, ret;
944 size_t count;
945
b766a9a2 946 if (freqs == NULL)
d3a98225
JM
947 return NULL;
948
949 count = 0;
b766a9a2 950 for (i = 0; freqs[i]; i++)
d3a98225
JM
951 count++;
952
953 pos = buf = os_zalloc(10 * count + 1);
954 if (buf == NULL)
955 return NULL;
956 end = buf + 10 * count + 1;
957
b766a9a2 958 for (i = 0; freqs[i]; i++) {
d3a98225 959 ret = os_snprintf(pos, end - pos, "%s%u",
b766a9a2 960 i == 0 ? "" : " ", freqs[i]);
d3a98225
JM
961 if (ret < 0 || ret >= end - pos) {
962 end[-1] = '\0';
963 return buf;
964 }
965 pos += ret;
966 }
967
968 return buf;
969}
b766a9a2
JM
970
971
972static char * wpa_config_write_scan_freq(const struct parse_data *data,
973 struct wpa_ssid *ssid)
974{
975 return wpa_config_write_freqs(data, ssid->scan_freq);
976}
977
978
979static char * wpa_config_write_freq_list(const struct parse_data *data,
980 struct wpa_ssid *ssid)
981{
982 return wpa_config_write_freqs(data, ssid->freq_list);
983}
d3a98225
JM
984#endif /* NO_CONFIG_WRITE */
985
986
6fc6879b
JM
987#ifdef IEEE8021X_EAPOL
988static int wpa_config_parse_eap(const struct parse_data *data,
989 struct wpa_ssid *ssid, int line,
990 const char *value)
991{
992 int last, errors = 0;
993 char *start, *end, *buf;
994 struct eap_method_type *methods = NULL, *tmp;
995 size_t num_methods = 0;
996
997 buf = os_strdup(value);
998 if (buf == NULL)
999 return -1;
1000 start = buf;
1001
1002 while (*start != '\0') {
1003 while (*start == ' ' || *start == '\t')
1004 start++;
1005 if (*start == '\0')
1006 break;
1007 end = start;
1008 while (*end != ' ' && *end != '\t' && *end != '\0')
1009 end++;
1010 last = *end == '\0';
1011 *end = '\0';
1012 tmp = methods;
067ffa26
JM
1013 methods = os_realloc_array(methods, num_methods + 1,
1014 sizeof(*methods));
6fc6879b
JM
1015 if (methods == NULL) {
1016 os_free(tmp);
1017 os_free(buf);
1018 return -1;
1019 }
1020 methods[num_methods].method = eap_peer_get_type(
1021 start, &methods[num_methods].vendor);
1022 if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
1023 methods[num_methods].method == EAP_TYPE_NONE) {
1024 wpa_printf(MSG_ERROR, "Line %d: unknown EAP method "
1025 "'%s'", line, start);
1026 wpa_printf(MSG_ERROR, "You may need to add support for"
1027 " this EAP method during wpa_supplicant\n"
1028 "build time configuration.\n"
1029 "See README for more information.");
1030 errors++;
1031 } else if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
1032 methods[num_methods].method == EAP_TYPE_LEAP)
1033 ssid->leap++;
1034 else
1035 ssid->non_leap++;
1036 num_methods++;
1037 if (last)
1038 break;
1039 start = end + 1;
1040 }
1041 os_free(buf);
1042
1043 tmp = methods;
067ffa26 1044 methods = os_realloc_array(methods, num_methods + 1, sizeof(*methods));
6fc6879b
JM
1045 if (methods == NULL) {
1046 os_free(tmp);
1047 return -1;
1048 }
1049 methods[num_methods].vendor = EAP_VENDOR_IETF;
1050 methods[num_methods].method = EAP_TYPE_NONE;
1051 num_methods++;
1052
1053 wpa_hexdump(MSG_MSGDUMP, "eap methods",
1054 (u8 *) methods, num_methods * sizeof(*methods));
bb8b1bb0 1055 os_free(ssid->eap.eap_methods);
6fc6879b
JM
1056 ssid->eap.eap_methods = methods;
1057 return errors ? -1 : 0;
1058}
1059
1060
1061static char * wpa_config_write_eap(const struct parse_data *data,
1062 struct wpa_ssid *ssid)
1063{
1064 int i, ret;
1065 char *buf, *pos, *end;
1066 const struct eap_method_type *eap_methods = ssid->eap.eap_methods;
1067 const char *name;
1068
1069 if (eap_methods == NULL)
1070 return NULL;
1071
1072 pos = buf = os_zalloc(100);
1073 if (buf == NULL)
1074 return NULL;
1075 end = buf + 100;
1076
1077 for (i = 0; eap_methods[i].vendor != EAP_VENDOR_IETF ||
1078 eap_methods[i].method != EAP_TYPE_NONE; i++) {
1079 name = eap_get_name(eap_methods[i].vendor,
1080 eap_methods[i].method);
1081 if (name) {
1082 ret = os_snprintf(pos, end - pos, "%s%s",
1083 pos == buf ? "" : " ", name);
1084 if (ret < 0 || ret >= end - pos)
1085 break;
1086 pos += ret;
1087 }
1088 }
1089
1090 end[-1] = '\0';
1091
1092 return buf;
1093}
1094
1095
1096static int wpa_config_parse_password(const struct parse_data *data,
1097 struct wpa_ssid *ssid, int line,
1098 const char *value)
1099{
1100 u8 *hash;
1101
b56c0546
JM
1102 if (os_strcmp(value, "NULL") == 0) {
1103 wpa_printf(MSG_DEBUG, "Unset configuration string 'password'");
1104 os_free(ssid->eap.password);
1105 ssid->eap.password = NULL;
1106 ssid->eap.password_len = 0;
1107 return 0;
1108 }
1109
0ebb23e3
JM
1110#ifdef CONFIG_EXT_PASSWORD
1111 if (os_strncmp(value, "ext:", 4) == 0) {
1112 char *name = os_strdup(value + 4);
1113 if (name == NULL)
1114 return -1;
1115 os_free(ssid->eap.password);
1116 ssid->eap.password = (u8 *) name;
1117 ssid->eap.password_len = os_strlen(name);
1118 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
1119 ssid->eap.flags |= EAP_CONFIG_FLAGS_EXT_PASSWORD;
1120 return 0;
1121 }
1122#endif /* CONFIG_EXT_PASSWORD */
1123
6fc6879b
JM
1124 if (os_strncmp(value, "hash:", 5) != 0) {
1125 char *tmp;
1126 size_t res_len;
1127
1128 tmp = wpa_config_parse_string(value, &res_len);
1129 if (tmp == NULL) {
1130 wpa_printf(MSG_ERROR, "Line %d: failed to parse "
1131 "password.", line);
1132 return -1;
1133 }
556f5a2a
HS
1134 wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
1135 (u8 *) tmp, res_len);
6fc6879b
JM
1136
1137 os_free(ssid->eap.password);
1138 ssid->eap.password = (u8 *) tmp;
1139 ssid->eap.password_len = res_len;
1140 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
0ebb23e3 1141 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_PASSWORD;
6fc6879b
JM
1142
1143 return 0;
1144 }
1145
1146
1147 /* NtPasswordHash: hash:<32 hex digits> */
1148 if (os_strlen(value + 5) != 2 * 16) {
1149 wpa_printf(MSG_ERROR, "Line %d: Invalid password hash length "
1150 "(expected 32 hex digits)", line);
1151 return -1;
1152 }
1153
1154 hash = os_malloc(16);
1155 if (hash == NULL)
1156 return -1;
1157
1158 if (hexstr2bin(value + 5, hash, 16)) {
1159 os_free(hash);
1160 wpa_printf(MSG_ERROR, "Line %d: Invalid password hash", line);
1161 return -1;
1162 }
1163
1164 wpa_hexdump_key(MSG_MSGDUMP, data->name, hash, 16);
1165
1166 os_free(ssid->eap.password);
1167 ssid->eap.password = hash;
1168 ssid->eap.password_len = 16;
1169 ssid->eap.flags |= EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
0ebb23e3 1170 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_PASSWORD;
6fc6879b
JM
1171
1172 return 0;
1173}
1174
1175
1176static char * wpa_config_write_password(const struct parse_data *data,
1177 struct wpa_ssid *ssid)
1178{
1179 char *buf;
1180
1181 if (ssid->eap.password == NULL)
1182 return NULL;
1183
0ebb23e3
JM
1184#ifdef CONFIG_EXT_PASSWORD
1185 if (ssid->eap.flags & EAP_CONFIG_FLAGS_EXT_PASSWORD) {
1186 buf = os_zalloc(4 + ssid->eap.password_len + 1);
1187 if (buf == NULL)
1188 return NULL;
1189 os_memcpy(buf, "ext:", 4);
1190 os_memcpy(buf + 4, ssid->eap.password, ssid->eap.password_len);
1191 return buf;
1192 }
1193#endif /* CONFIG_EXT_PASSWORD */
1194
6fc6879b
JM
1195 if (!(ssid->eap.flags & EAP_CONFIG_FLAGS_PASSWORD_NTHASH)) {
1196 return wpa_config_write_string(
1197 ssid->eap.password, ssid->eap.password_len);
1198 }
1199
1200 buf = os_malloc(5 + 32 + 1);
1201 if (buf == NULL)
1202 return NULL;
1203
1204 os_memcpy(buf, "hash:", 5);
1205 wpa_snprintf_hex(buf + 5, 32 + 1, ssid->eap.password, 16);
1206
1207 return buf;
1208}
1209#endif /* IEEE8021X_EAPOL */
1210
1211
1212static int wpa_config_parse_wep_key(u8 *key, size_t *len, int line,
1213 const char *value, int idx)
1214{
1215 char *buf, title[20];
1216 int res;
1217
1218 buf = wpa_config_parse_string(value, len);
1219 if (buf == NULL) {
1220 wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key %d '%s'.",
1221 line, idx, value);
1222 return -1;
1223 }
1224 if (*len > MAX_WEP_KEY_LEN) {
1225 wpa_printf(MSG_ERROR, "Line %d: Too long WEP key %d '%s'.",
1226 line, idx, value);
1227 os_free(buf);
1228 return -1;
1229 }
fea7c3a0
JM
1230 if (*len && *len != 5 && *len != 13 && *len != 16) {
1231 wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key length %u - "
1232 "this network block will be ignored",
1233 line, (unsigned int) *len);
1234 }
6fc6879b
JM
1235 os_memcpy(key, buf, *len);
1236 os_free(buf);
1237 res = os_snprintf(title, sizeof(title), "wep_key%d", idx);
1238 if (res >= 0 && (size_t) res < sizeof(title))
1239 wpa_hexdump_key(MSG_MSGDUMP, title, key, *len);
1240 return 0;
1241}
1242
1243
1244static int wpa_config_parse_wep_key0(const struct parse_data *data,
1245 struct wpa_ssid *ssid, int line,
1246 const char *value)
1247{
1248 return wpa_config_parse_wep_key(ssid->wep_key[0],
1249 &ssid->wep_key_len[0], line,
1250 value, 0);
1251}
1252
1253
1254static int wpa_config_parse_wep_key1(const struct parse_data *data,
1255 struct wpa_ssid *ssid, int line,
1256 const char *value)
1257{
1258 return wpa_config_parse_wep_key(ssid->wep_key[1],
1259 &ssid->wep_key_len[1], line,
1260 value, 1);
1261}
1262
1263
1264static int wpa_config_parse_wep_key2(const struct parse_data *data,
1265 struct wpa_ssid *ssid, int line,
1266 const char *value)
1267{
1268 return wpa_config_parse_wep_key(ssid->wep_key[2],
1269 &ssid->wep_key_len[2], line,
1270 value, 2);
1271}
1272
1273
1274static int wpa_config_parse_wep_key3(const struct parse_data *data,
1275 struct wpa_ssid *ssid, int line,
1276 const char *value)
1277{
1278 return wpa_config_parse_wep_key(ssid->wep_key[3],
1279 &ssid->wep_key_len[3], line,
1280 value, 3);
1281}
1282
1283
1284#ifndef NO_CONFIG_WRITE
1285static char * wpa_config_write_wep_key(struct wpa_ssid *ssid, int idx)
1286{
1287 if (ssid->wep_key_len[idx] == 0)
1288 return NULL;
1289 return wpa_config_write_string(ssid->wep_key[idx],
1290 ssid->wep_key_len[idx]);
1291}
1292
1293
1294static char * wpa_config_write_wep_key0(const struct parse_data *data,
1295 struct wpa_ssid *ssid)
1296{
1297 return wpa_config_write_wep_key(ssid, 0);
1298}
1299
1300
1301static char * wpa_config_write_wep_key1(const struct parse_data *data,
1302 struct wpa_ssid *ssid)
1303{
1304 return wpa_config_write_wep_key(ssid, 1);
1305}
1306
1307
1308static char * wpa_config_write_wep_key2(const struct parse_data *data,
1309 struct wpa_ssid *ssid)
1310{
1311 return wpa_config_write_wep_key(ssid, 2);
1312}
1313
1314
1315static char * wpa_config_write_wep_key3(const struct parse_data *data,
1316 struct wpa_ssid *ssid)
1317{
1318 return wpa_config_write_wep_key(ssid, 3);
1319}
1320#endif /* NO_CONFIG_WRITE */
1321
1322
fbdcfd57
JM
1323#ifdef CONFIG_P2P
1324
9ec87666
JM
1325static int wpa_config_parse_go_p2p_dev_addr(const struct parse_data *data,
1326 struct wpa_ssid *ssid, int line,
1327 const char *value)
1328{
1329 if (value[0] == '\0' || os_strcmp(value, "\"\"") == 0 ||
1330 os_strcmp(value, "any") == 0) {
1331 os_memset(ssid->go_p2p_dev_addr, 0, ETH_ALEN);
1332 wpa_printf(MSG_MSGDUMP, "GO P2P Device Address any");
1333 return 0;
1334 }
1335 if (hwaddr_aton(value, ssid->go_p2p_dev_addr)) {
1336 wpa_printf(MSG_ERROR, "Line %d: Invalid GO P2P Device Address '%s'.",
1337 line, value);
1338 return -1;
1339 }
1340 ssid->bssid_set = 1;
1341 wpa_printf(MSG_MSGDUMP, "GO P2P Device Address " MACSTR,
1342 MAC2STR(ssid->go_p2p_dev_addr));
1343 return 0;
1344}
1345
1346
1347#ifndef NO_CONFIG_WRITE
1348static char * wpa_config_write_go_p2p_dev_addr(const struct parse_data *data,
1349 struct wpa_ssid *ssid)
1350{
1351 char *value;
1352 int res;
1353
1354 if (is_zero_ether_addr(ssid->go_p2p_dev_addr))
1355 return NULL;
1356
1357 value = os_malloc(20);
1358 if (value == NULL)
1359 return NULL;
1360 res = os_snprintf(value, 20, MACSTR, MAC2STR(ssid->go_p2p_dev_addr));
1361 if (res < 0 || res >= 20) {
1362 os_free(value);
1363 return NULL;
1364 }
1365 value[20 - 1] = '\0';
1366 return value;
1367}
1368#endif /* NO_CONFIG_WRITE */
1369
1370
fbdcfd57
JM
1371static int wpa_config_parse_p2p_client_list(const struct parse_data *data,
1372 struct wpa_ssid *ssid, int line,
1373 const char *value)
1374{
1375 const char *pos;
1376 u8 *buf, *n, addr[ETH_ALEN];
1377 size_t count;
1378
1379 buf = NULL;
1380 count = 0;
1381
1382 pos = value;
1383 while (pos && *pos) {
1384 while (*pos == ' ')
1385 pos++;
1386
1387 if (hwaddr_aton(pos, addr)) {
22316795
JM
1388 if (count == 0) {
1389 wpa_printf(MSG_ERROR, "Line %d: Invalid "
1390 "p2p_client_list address '%s'.",
1391 line, value);
1392 os_free(buf);
1393 return -1;
1394 }
1395 /* continue anyway since this could have been from a
1396 * truncated configuration file line */
1397 wpa_printf(MSG_INFO, "Line %d: Ignore likely "
1398 "truncated p2p_client_list address '%s'",
1399 line, pos);
fbdcfd57 1400 } else {
067ffa26 1401 n = os_realloc_array(buf, count + 1, ETH_ALEN);
fbdcfd57
JM
1402 if (n == NULL) {
1403 os_free(buf);
1404 return -1;
1405 }
1406 buf = n;
22316795
JM
1407 os_memmove(buf + ETH_ALEN, buf, count * ETH_ALEN);
1408 os_memcpy(buf, addr, ETH_ALEN);
fbdcfd57
JM
1409 count++;
1410 wpa_hexdump(MSG_MSGDUMP, "p2p_client_list",
1411 addr, ETH_ALEN);
1412 }
1413
1414 pos = os_strchr(pos, ' ');
1415 }
1416
1417 os_free(ssid->p2p_client_list);
1418 ssid->p2p_client_list = buf;
1419 ssid->num_p2p_clients = count;
1420
1421 return 0;
1422}
1423
1424
1425#ifndef NO_CONFIG_WRITE
1426static char * wpa_config_write_p2p_client_list(const struct parse_data *data,
1427 struct wpa_ssid *ssid)
1428{
1429 char *value, *end, *pos;
1430 int res;
1431 size_t i;
1432
1433 if (ssid->p2p_client_list == NULL || ssid->num_p2p_clients == 0)
1434 return NULL;
1435
1436 value = os_malloc(20 * ssid->num_p2p_clients);
1437 if (value == NULL)
1438 return NULL;
1439 pos = value;
1440 end = value + 20 * ssid->num_p2p_clients;
1441
22316795 1442 for (i = ssid->num_p2p_clients; i > 0; i--) {
fbdcfd57
JM
1443 res = os_snprintf(pos, end - pos, MACSTR " ",
1444 MAC2STR(ssid->p2p_client_list +
22316795 1445 (i - 1) * ETH_ALEN));
fbdcfd57
JM
1446 if (res < 0 || res >= end - pos) {
1447 os_free(value);
1448 return NULL;
1449 }
1450 pos += res;
1451 }
1452
1453 if (pos > value)
1454 pos[-1] = '\0';
1455
1456 return value;
1457}
1458#endif /* NO_CONFIG_WRITE */
1459
01a57fe4
JM
1460
1461static int wpa_config_parse_psk_list(const struct parse_data *data,
1462 struct wpa_ssid *ssid, int line,
1463 const char *value)
1464{
1465 struct psk_list_entry *p;
1466 const char *pos;
1467
1468 p = os_zalloc(sizeof(*p));
1469 if (p == NULL)
1470 return -1;
1471
1472 pos = value;
1473 if (os_strncmp(pos, "P2P-", 4) == 0) {
1474 p->p2p = 1;
1475 pos += 4;
1476 }
1477
1478 if (hwaddr_aton(pos, p->addr)) {
1479 wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list address '%s'",
1480 line, pos);
1481 os_free(p);
1482 return -1;
1483 }
1484 pos += 17;
1485 if (*pos != '-') {
1486 wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list '%s'",
1487 line, pos);
1488 os_free(p);
1489 return -1;
1490 }
1491 pos++;
1492
1493 if (hexstr2bin(pos, p->psk, PMK_LEN) || pos[PMK_LEN * 2] != '\0') {
1494 wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list PSK '%s'",
1495 line, pos);
1496 os_free(p);
1497 return -1;
1498 }
1499
1500 dl_list_add(&ssid->psk_list, &p->list);
1501
1502 return 0;
1503}
1504
1505
1506#ifndef NO_CONFIG_WRITE
1507static char * wpa_config_write_psk_list(const struct parse_data *data,
1508 struct wpa_ssid *ssid)
1509{
1510 return NULL;
1511}
1512#endif /* NO_CONFIG_WRITE */
1513
fbdcfd57
JM
1514#endif /* CONFIG_P2P */
1515
6fc6879b
JM
1516/* Helper macros for network block parser */
1517
1518#ifdef OFFSET
1519#undef OFFSET
1520#endif /* OFFSET */
1521/* OFFSET: Get offset of a variable within the wpa_ssid structure */
1522#define OFFSET(v) ((void *) &((struct wpa_ssid *) 0)->v)
1523
1524/* STR: Define a string variable for an ASCII string; f = field name */
1525#ifdef NO_CONFIG_WRITE
1526#define _STR(f) #f, wpa_config_parse_str, OFFSET(f)
1527#define _STRe(f) #f, wpa_config_parse_str, OFFSET(eap.f)
1528#else /* NO_CONFIG_WRITE */
1529#define _STR(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(f)
1530#define _STRe(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(eap.f)
1531#endif /* NO_CONFIG_WRITE */
1532#define STR(f) _STR(f), NULL, NULL, NULL, 0
1533#define STRe(f) _STRe(f), NULL, NULL, NULL, 0
1534#define STR_KEY(f) _STR(f), NULL, NULL, NULL, 1
1535#define STR_KEYe(f) _STRe(f), NULL, NULL, NULL, 1
1536
1537/* STR_LEN: Define a string variable with a separate variable for storing the
1538 * data length. Unlike STR(), this can be used to store arbitrary binary data
1539 * (i.e., even nul termination character). */
1540#define _STR_LEN(f) _STR(f), OFFSET(f ## _len)
1541#define _STR_LENe(f) _STRe(f), OFFSET(eap.f ## _len)
1542#define STR_LEN(f) _STR_LEN(f), NULL, NULL, 0
1543#define STR_LENe(f) _STR_LENe(f), NULL, NULL, 0
1544#define STR_LEN_KEY(f) _STR_LEN(f), NULL, NULL, 1
1545
1546/* STR_RANGE: Like STR_LEN(), but with minimum and maximum allowed length
1547 * explicitly specified. */
1548#define _STR_RANGE(f, min, max) _STR_LEN(f), (void *) (min), (void *) (max)
1549#define STR_RANGE(f, min, max) _STR_RANGE(f, min, max), 0
1550#define STR_RANGE_KEY(f, min, max) _STR_RANGE(f, min, max), 1
1551
1552#ifdef NO_CONFIG_WRITE
1553#define _INT(f) #f, wpa_config_parse_int, OFFSET(f), (void *) 0
1554#define _INTe(f) #f, wpa_config_parse_int, OFFSET(eap.f), (void *) 0
1555#else /* NO_CONFIG_WRITE */
1556#define _INT(f) #f, wpa_config_parse_int, wpa_config_write_int, \
1557 OFFSET(f), (void *) 0
1558#define _INTe(f) #f, wpa_config_parse_int, wpa_config_write_int, \
1559 OFFSET(eap.f), (void *) 0
1560#endif /* NO_CONFIG_WRITE */
1561
1562/* INT: Define an integer variable */
1563#define INT(f) _INT(f), NULL, NULL, 0
1564#define INTe(f) _INTe(f), NULL, NULL, 0
1565
1566/* INT_RANGE: Define an integer variable with allowed value range */
1567#define INT_RANGE(f, min, max) _INT(f), (void *) (min), (void *) (max), 0
1568
1569/* FUNC: Define a configuration variable that uses a custom function for
1570 * parsing and writing the value. */
1571#ifdef NO_CONFIG_WRITE
1572#define _FUNC(f) #f, wpa_config_parse_ ## f, NULL, NULL, NULL, NULL
1573#else /* NO_CONFIG_WRITE */
1574#define _FUNC(f) #f, wpa_config_parse_ ## f, wpa_config_write_ ## f, \
1575 NULL, NULL, NULL, NULL
1576#endif /* NO_CONFIG_WRITE */
1577#define FUNC(f) _FUNC(f), 0
1578#define FUNC_KEY(f) _FUNC(f), 1
1579
1580/*
1581 * Table of network configuration variables. This table is used to parse each
1582 * network configuration variable, e.g., each line in wpa_supplicant.conf file
1583 * that is inside a network block.
1584 *
1585 * This table is generated using the helper macros defined above and with
1586 * generous help from the C pre-processor. The field name is stored as a string
1587 * into .name and for STR and INT types, the offset of the target buffer within
1588 * struct wpa_ssid is stored in .param1. .param2 (if not NULL) is similar
1589 * offset to the field containing the length of the configuration variable.
1590 * .param3 and .param4 can be used to mark the allowed range (length for STR
1591 * and value for INT).
1592 *
1593 * For each configuration line in wpa_supplicant.conf, the parser goes through
1594 * this table and select the entry that matches with the field name. The parser
1595 * function (.parser) is then called to parse the actual value of the field.
1596 *
1597 * This kind of mechanism makes it easy to add new configuration parameters,
1598 * since only one line needs to be added into this table and into the
1599 * struct wpa_ssid definition if the new variable is either a string or
1600 * integer. More complex types will need to use their own parser and writer
1601 * functions.
1602 */
1603static const struct parse_data ssid_fields[] = {
1604 { STR_RANGE(ssid, 0, MAX_SSID_LEN) },
1605 { INT_RANGE(scan_ssid, 0, 1) },
1606 { FUNC(bssid) },
1607 { FUNC_KEY(psk) },
1608 { FUNC(proto) },
1609 { FUNC(key_mgmt) },
1f6c0ab8 1610 { INT(bg_scan_period) },
6fc6879b
JM
1611 { FUNC(pairwise) },
1612 { FUNC(group) },
1613 { FUNC(auth_alg) },
d3a98225 1614 { FUNC(scan_freq) },
b766a9a2 1615 { FUNC(freq_list) },
6fc6879b
JM
1616#ifdef IEEE8021X_EAPOL
1617 { FUNC(eap) },
1618 { STR_LENe(identity) },
1619 { STR_LENe(anonymous_identity) },
556f5a2a 1620 { FUNC_KEY(password) },
6fc6879b
JM
1621 { STRe(ca_cert) },
1622 { STRe(ca_path) },
1623 { STRe(client_cert) },
1624 { STRe(private_key) },
1625 { STR_KEYe(private_key_passwd) },
1626 { STRe(dh_file) },
1627 { STRe(subject_match) },
1628 { STRe(altsubject_match) },
01f809c7 1629 { STRe(domain_suffix_match) },
6fc6879b
JM
1630 { STRe(ca_cert2) },
1631 { STRe(ca_path2) },
1632 { STRe(client_cert2) },
1633 { STRe(private_key2) },
1634 { STR_KEYe(private_key2_passwd) },
1635 { STRe(dh_file2) },
1636 { STRe(subject_match2) },
1637 { STRe(altsubject_match2) },
01f809c7 1638 { STRe(domain_suffix_match2) },
6fc6879b
JM
1639 { STRe(phase1) },
1640 { STRe(phase2) },
1641 { STRe(pcsc) },
1642 { STR_KEYe(pin) },
1643 { STRe(engine_id) },
1644 { STRe(key_id) },
61ee0f71
DS
1645 { STRe(cert_id) },
1646 { STRe(ca_cert_id) },
98842d51
CL
1647 { STR_KEYe(pin2) },
1648 { STRe(engine2_id) },
61ee0f71
DS
1649 { STRe(key2_id) },
1650 { STRe(cert2_id) },
1651 { STRe(ca_cert2_id) },
6fc6879b 1652 { INTe(engine) },
98842d51 1653 { INTe(engine2) },
6fc6879b
JM
1654 { INT(eapol_flags) },
1655#endif /* IEEE8021X_EAPOL */
1656 { FUNC_KEY(wep_key0) },
1657 { FUNC_KEY(wep_key1) },
1658 { FUNC_KEY(wep_key2) },
1659 { FUNC_KEY(wep_key3) },
1660 { INT(wep_tx_keyidx) },
1661 { INT(priority) },
1662#ifdef IEEE8021X_EAPOL
1663 { INT(eap_workaround) },
1664 { STRe(pac_file) },
1665 { INTe(fragment_size) },
080585c0 1666 { INTe(ocsp) },
6fc6879b 1667#endif /* IEEE8021X_EAPOL */
2c5d725c 1668 { INT_RANGE(mode, 0, 4) },
6fc6879b 1669 { INT_RANGE(proactive_key_caching, 0, 1) },
4dac0245 1670 { INT_RANGE(disabled, 0, 2) },
6fc6879b
JM
1671 { STR(id_str) },
1672#ifdef CONFIG_IEEE80211W
1673 { INT_RANGE(ieee80211w, 0, 2) },
1674#endif /* CONFIG_IEEE80211W */
1675 { INT_RANGE(peerkey, 0, 1) },
1676 { INT_RANGE(mixed_cell, 0, 1) },
7829894c 1677 { INT_RANGE(frequency, 0, 65000) },
60b94c98
JM
1678 { INT(wpa_ptk_rekey) },
1679 { STR(bgscan) },
e62f4ed0 1680 { INT_RANGE(ignore_broadcast_ssid, 0, 2) },
fbdcfd57 1681#ifdef CONFIG_P2P
9ec87666 1682 { FUNC(go_p2p_dev_addr) },
fbdcfd57 1683 { FUNC(p2p_client_list) },
01a57fe4 1684 { FUNC(psk_list) },
fbdcfd57 1685#endif /* CONFIG_P2P */
80e8a5ee
BG
1686#ifdef CONFIG_HT_OVERRIDES
1687 { INT_RANGE(disable_ht, 0, 1) },
1688 { INT_RANGE(disable_ht40, -1, 1) },
a90497f8 1689 { INT_RANGE(disable_sgi, 0, 1) },
80e8a5ee
BG
1690 { INT_RANGE(disable_max_amsdu, -1, 1) },
1691 { INT_RANGE(ampdu_factor, -1, 3) },
1692 { INT_RANGE(ampdu_density, -1, 7) },
1693 { STR(ht_mcs) },
1694#endif /* CONFIG_HT_OVERRIDES */
e9ee8dc3
JB
1695#ifdef CONFIG_VHT_OVERRIDES
1696 { INT_RANGE(disable_vht, 0, 1) },
1697 { INT(vht_capa) },
1698 { INT(vht_capa_mask) },
1699 { INT_RANGE(vht_rx_mcs_nss_1, -1, 3) },
1700 { INT_RANGE(vht_rx_mcs_nss_2, -1, 3) },
1701 { INT_RANGE(vht_rx_mcs_nss_3, -1, 3) },
1702 { INT_RANGE(vht_rx_mcs_nss_4, -1, 3) },
1703 { INT_RANGE(vht_rx_mcs_nss_5, -1, 3) },
1704 { INT_RANGE(vht_rx_mcs_nss_6, -1, 3) },
1705 { INT_RANGE(vht_rx_mcs_nss_7, -1, 3) },
1706 { INT_RANGE(vht_rx_mcs_nss_8, -1, 3) },
1707 { INT_RANGE(vht_tx_mcs_nss_1, -1, 3) },
1708 { INT_RANGE(vht_tx_mcs_nss_2, -1, 3) },
1709 { INT_RANGE(vht_tx_mcs_nss_3, -1, 3) },
1710 { INT_RANGE(vht_tx_mcs_nss_4, -1, 3) },
1711 { INT_RANGE(vht_tx_mcs_nss_5, -1, 3) },
1712 { INT_RANGE(vht_tx_mcs_nss_6, -1, 3) },
1713 { INT_RANGE(vht_tx_mcs_nss_7, -1, 3) },
1714 { INT_RANGE(vht_tx_mcs_nss_8, -1, 3) },
1715#endif /* CONFIG_VHT_OVERRIDES */
07f53b8c 1716 { INT(ap_max_inactivity) },
fdfb1c8b 1717 { INT(dtim_period) },
18206e02 1718 { INT(beacon_int) },
6fc6879b
JM
1719};
1720
1721#undef OFFSET
1722#undef _STR
1723#undef STR
1724#undef STR_KEY
1725#undef _STR_LEN
1726#undef STR_LEN
1727#undef STR_LEN_KEY
1728#undef _STR_RANGE
1729#undef STR_RANGE
1730#undef STR_RANGE_KEY
1731#undef _INT
1732#undef INT
1733#undef INT_RANGE
1734#undef _FUNC
1735#undef FUNC
1736#undef FUNC_KEY
e7ecab4a 1737#define NUM_SSID_FIELDS ARRAY_SIZE(ssid_fields)
6fc6879b
JM
1738
1739
1740/**
1741 * wpa_config_add_prio_network - Add a network to priority lists
1742 * @config: Configuration data from wpa_config_read()
1743 * @ssid: Pointer to the network configuration to be added to the list
1744 * Returns: 0 on success, -1 on failure
1745 *
1746 * This function is used to add a network block to the priority list of
1747 * networks. This must be called for each network when reading in the full
1748 * configuration. In addition, this can be used indirectly when updating
1749 * priorities by calling wpa_config_update_prio_list().
1750 */
1751int wpa_config_add_prio_network(struct wpa_config *config,
1752 struct wpa_ssid *ssid)
1753{
1754 int prio;
1755 struct wpa_ssid *prev, **nlist;
1756
1757 /*
1758 * Add to an existing priority list if one is available for the
1759 * configured priority level for this network.
1760 */
1761 for (prio = 0; prio < config->num_prio; prio++) {
1762 prev = config->pssid[prio];
1763 if (prev->priority == ssid->priority) {
1764 while (prev->pnext)
1765 prev = prev->pnext;
1766 prev->pnext = ssid;
1767 return 0;
1768 }
1769 }
1770
1771 /* First network for this priority - add a new priority list */
067ffa26
JM
1772 nlist = os_realloc_array(config->pssid, config->num_prio + 1,
1773 sizeof(struct wpa_ssid *));
6fc6879b
JM
1774 if (nlist == NULL)
1775 return -1;
1776
1777 for (prio = 0; prio < config->num_prio; prio++) {
2c60ca73
JM
1778 if (nlist[prio]->priority < ssid->priority) {
1779 os_memmove(&nlist[prio + 1], &nlist[prio],
1780 (config->num_prio - prio) *
1781 sizeof(struct wpa_ssid *));
6fc6879b 1782 break;
2c60ca73 1783 }
6fc6879b
JM
1784 }
1785
6fc6879b
JM
1786 nlist[prio] = ssid;
1787 config->num_prio++;
1788 config->pssid = nlist;
1789
1790 return 0;
1791}
1792
1793
1794/**
1795 * wpa_config_update_prio_list - Update network priority list
1796 * @config: Configuration data from wpa_config_read()
1797 * Returns: 0 on success, -1 on failure
1798 *
1799 * This function is called to update the priority list of networks in the
1800 * configuration when a network is being added or removed. This is also called
1801 * if a priority for a network is changed.
1802 */
aa53509f 1803int wpa_config_update_prio_list(struct wpa_config *config)
6fc6879b
JM
1804{
1805 struct wpa_ssid *ssid;
1806 int ret = 0;
1807
1808 os_free(config->pssid);
1809 config->pssid = NULL;
1810 config->num_prio = 0;
1811
1812 ssid = config->ssid;
1813 while (ssid) {
1814 ssid->pnext = NULL;
1815 if (wpa_config_add_prio_network(config, ssid) < 0)
1816 ret = -1;
1817 ssid = ssid->next;
1818 }
1819
1820 return ret;
1821}
1822
1823
1824#ifdef IEEE8021X_EAPOL
1825static void eap_peer_config_free(struct eap_peer_config *eap)
1826{
1827 os_free(eap->eap_methods);
1828 os_free(eap->identity);
1829 os_free(eap->anonymous_identity);
1830 os_free(eap->password);
1831 os_free(eap->ca_cert);
1832 os_free(eap->ca_path);
1833 os_free(eap->client_cert);
1834 os_free(eap->private_key);
1835 os_free(eap->private_key_passwd);
1836 os_free(eap->dh_file);
1837 os_free(eap->subject_match);
1838 os_free(eap->altsubject_match);
01f809c7 1839 os_free(eap->domain_suffix_match);
6fc6879b
JM
1840 os_free(eap->ca_cert2);
1841 os_free(eap->ca_path2);
1842 os_free(eap->client_cert2);
1843 os_free(eap->private_key2);
1844 os_free(eap->private_key2_passwd);
1845 os_free(eap->dh_file2);
1846 os_free(eap->subject_match2);
1847 os_free(eap->altsubject_match2);
01f809c7 1848 os_free(eap->domain_suffix_match2);
6fc6879b
JM
1849 os_free(eap->phase1);
1850 os_free(eap->phase2);
1851 os_free(eap->pcsc);
1852 os_free(eap->pin);
1853 os_free(eap->engine_id);
1854 os_free(eap->key_id);
61ee0f71
DS
1855 os_free(eap->cert_id);
1856 os_free(eap->ca_cert_id);
1857 os_free(eap->key2_id);
1858 os_free(eap->cert2_id);
1859 os_free(eap->ca_cert2_id);
98842d51
CL
1860 os_free(eap->pin2);
1861 os_free(eap->engine2_id);
6fc6879b
JM
1862 os_free(eap->otp);
1863 os_free(eap->pending_req_otp);
1864 os_free(eap->pac_file);
1865 os_free(eap->new_password);
a5d44ac0 1866 os_free(eap->external_sim_resp);
6fc6879b
JM
1867}
1868#endif /* IEEE8021X_EAPOL */
1869
1870
1871/**
1872 * wpa_config_free_ssid - Free network/ssid configuration data
1873 * @ssid: Configuration data for the network
1874 *
1875 * This function frees all resources allocated for the network configuration
1876 * data.
1877 */
1878void wpa_config_free_ssid(struct wpa_ssid *ssid)
1879{
01a57fe4
JM
1880 struct psk_list_entry *psk;
1881
6fc6879b
JM
1882 os_free(ssid->ssid);
1883 os_free(ssid->passphrase);
9173b16f 1884 os_free(ssid->ext_psk);
6fc6879b
JM
1885#ifdef IEEE8021X_EAPOL
1886 eap_peer_config_free(&ssid->eap);
1887#endif /* IEEE8021X_EAPOL */
1888 os_free(ssid->id_str);
d3a98225 1889 os_free(ssid->scan_freq);
b766a9a2 1890 os_free(ssid->freq_list);
60b94c98 1891 os_free(ssid->bgscan);
fbdcfd57 1892 os_free(ssid->p2p_client_list);
80e8a5ee
BG
1893#ifdef CONFIG_HT_OVERRIDES
1894 os_free(ssid->ht_mcs);
1895#endif /* CONFIG_HT_OVERRIDES */
01a57fe4
JM
1896 while ((psk = dl_list_first(&ssid->psk_list, struct psk_list_entry,
1897 list))) {
1898 dl_list_del(&psk->list);
1899 os_free(psk);
1900 }
6fc6879b
JM
1901 os_free(ssid);
1902}
1903
1904
1bb7b8e8
JM
1905void wpa_config_free_cred(struct wpa_cred *cred)
1906{
463c8ffb
JM
1907 size_t i;
1908
1bb7b8e8
JM
1909 os_free(cred->realm);
1910 os_free(cred->username);
1911 os_free(cred->password);
1912 os_free(cred->ca_cert);
11e4f46a
JM
1913 os_free(cred->client_cert);
1914 os_free(cred->private_key);
1915 os_free(cred->private_key_passwd);
1bb7b8e8
JM
1916 os_free(cred->imsi);
1917 os_free(cred->milenage);
463c8ffb
JM
1918 for (i = 0; i < cred->num_domain; i++)
1919 os_free(cred->domain[i]);
1bb7b8e8 1920 os_free(cred->domain);
ac1bc549 1921 os_free(cred->domain_suffix_match);
8ca93c59
JM
1922 os_free(cred->eap_method);
1923 os_free(cred->phase1);
1924 os_free(cred->phase2);
dbea8ac7 1925 os_free(cred->excluded_ssid);
1bb7b8e8
JM
1926 os_free(cred);
1927}
1928
1929
d9bb2821
JM
1930void wpa_config_flush_blobs(struct wpa_config *config)
1931{
1932#ifndef CONFIG_NO_CONFIG_BLOBS
1933 struct wpa_config_blob *blob, *prev;
1934
1935 blob = config->blobs;
1936 config->blobs = NULL;
1937 while (blob) {
1938 prev = blob;
1939 blob = blob->next;
1940 wpa_config_free_blob(prev);
1941 }
1942#endif /* CONFIG_NO_CONFIG_BLOBS */
1943}
1944
1945
6fc6879b
JM
1946/**
1947 * wpa_config_free - Free configuration data
1948 * @config: Configuration data from wpa_config_read()
1949 *
1950 * This function frees all resources allocated for the configuration data by
1951 * wpa_config_read().
1952 */
1953void wpa_config_free(struct wpa_config *config)
1954{
6fc6879b 1955 struct wpa_ssid *ssid, *prev = NULL;
1bb7b8e8 1956 struct wpa_cred *cred, *cprev;
e3768e7c 1957
6fc6879b
JM
1958 ssid = config->ssid;
1959 while (ssid) {
1960 prev = ssid;
1961 ssid = ssid->next;
1962 wpa_config_free_ssid(prev);
1963 }
1964
1bb7b8e8
JM
1965 cred = config->cred;
1966 while (cred) {
1967 cprev = cred;
1968 cred = cred->next;
1969 wpa_config_free_cred(cprev);
1970 }
1971
d9bb2821 1972 wpa_config_flush_blobs(config);
6fc6879b 1973
71dd3b78 1974 wpabuf_free(config->wps_vendor_ext_m1);
6fc6879b
JM
1975 os_free(config->ctrl_interface);
1976 os_free(config->ctrl_interface_group);
6fc6879b
JM
1977 os_free(config->opensc_engine_path);
1978 os_free(config->pkcs11_engine_path);
1979 os_free(config->pkcs11_module_path);
f64adcd7
JM
1980 os_free(config->pcsc_reader);
1981 os_free(config->pcsc_pin);
6fc6879b 1982 os_free(config->driver_param);
3c0b7aa4
JM
1983 os_free(config->device_name);
1984 os_free(config->manufacturer);
1985 os_free(config->model_name);
1986 os_free(config->model_number);
1987 os_free(config->serial_number);
c0e4dd9e 1988 os_free(config->config_methods);
e3768e7c 1989 os_free(config->p2p_ssid_postfix);
6fc6879b 1990 os_free(config->pssid);
21d996f7 1991 os_free(config->p2p_pref_chan);
556b30da 1992 os_free(config->p2p_no_go_freq.range);
b0786fba 1993 os_free(config->autoscan);
f5ffc348 1994 os_free(config->freq_list);
3f2c8ba6
JM
1995 wpabuf_free(config->wps_nfc_dh_pubkey);
1996 wpabuf_free(config->wps_nfc_dh_privkey);
1997 wpabuf_free(config->wps_nfc_dev_pw);
306ae225 1998 os_free(config->ext_password_backend);
625f202a 1999 os_free(config->sae_groups);
18a2eaab 2000 wpabuf_free(config->ap_vendor_elements);
6fc6879b
JM
2001 os_free(config);
2002}
2003
2004
7c49fdd0
SL
2005/**
2006 * wpa_config_foreach_network - Iterate over each configured network
2007 * @config: Configuration data from wpa_config_read()
2008 * @func: Callback function to process each network
2009 * @arg: Opaque argument to pass to callback function
2010 *
2011 * Iterate over the set of configured networks calling the specified
2012 * function for each item. We guard against callbacks removing the
2013 * supplied network.
2014 */
2015void wpa_config_foreach_network(struct wpa_config *config,
2016 void (*func)(void *, struct wpa_ssid *),
2017 void *arg)
2018{
2019 struct wpa_ssid *ssid, *next;
2020
2021 ssid = config->ssid;
2022 while (ssid) {
2023 next = ssid->next;
2024 func(arg, ssid);
2025 ssid = next;
2026 }
2027}
2028
2029
6fc6879b
JM
2030/**
2031 * wpa_config_get_network - Get configured network based on id
2032 * @config: Configuration data from wpa_config_read()
2033 * @id: Unique network id to search for
2034 * Returns: Network configuration or %NULL if not found
2035 */
2036struct wpa_ssid * wpa_config_get_network(struct wpa_config *config, int id)
2037{
2038 struct wpa_ssid *ssid;
2039
2040 ssid = config->ssid;
2041 while (ssid) {
2042 if (id == ssid->id)
2043 break;
2044 ssid = ssid->next;
2045 }
2046
2047 return ssid;
2048}
2049
2050
2051/**
2052 * wpa_config_add_network - Add a new network with empty configuration
2053 * @config: Configuration data from wpa_config_read()
2054 * Returns: The new network configuration or %NULL if operation failed
2055 */
2056struct wpa_ssid * wpa_config_add_network(struct wpa_config *config)
2057{
2058 int id;
2059 struct wpa_ssid *ssid, *last = NULL;
2060
2061 id = -1;
2062 ssid = config->ssid;
2063 while (ssid) {
2064 if (ssid->id > id)
2065 id = ssid->id;
2066 last = ssid;
2067 ssid = ssid->next;
2068 }
2069 id++;
2070
2071 ssid = os_zalloc(sizeof(*ssid));
2072 if (ssid == NULL)
2073 return NULL;
2074 ssid->id = id;
01a57fe4 2075 dl_list_init(&ssid->psk_list);
6fc6879b
JM
2076 if (last)
2077 last->next = ssid;
2078 else
2079 config->ssid = ssid;
2080
2081 wpa_config_update_prio_list(config);
2082
2083 return ssid;
2084}
2085
2086
2087/**
2088 * wpa_config_remove_network - Remove a configured network based on id
2089 * @config: Configuration data from wpa_config_read()
2090 * @id: Unique network id to search for
2091 * Returns: 0 on success, or -1 if the network was not found
2092 */
2093int wpa_config_remove_network(struct wpa_config *config, int id)
2094{
2095 struct wpa_ssid *ssid, *prev = NULL;
2096
2097 ssid = config->ssid;
2098 while (ssid) {
2099 if (id == ssid->id)
2100 break;
2101 prev = ssid;
2102 ssid = ssid->next;
2103 }
2104
2105 if (ssid == NULL)
2106 return -1;
2107
2108 if (prev)
2109 prev->next = ssid->next;
2110 else
2111 config->ssid = ssid->next;
2112
2113 wpa_config_update_prio_list(config);
2114 wpa_config_free_ssid(ssid);
2115 return 0;
2116}
2117
2118
2119/**
2120 * wpa_config_set_network_defaults - Set network default values
2121 * @ssid: Pointer to network configuration data
2122 */
2123void wpa_config_set_network_defaults(struct wpa_ssid *ssid)
2124{
2125 ssid->proto = DEFAULT_PROTO;
2126 ssid->pairwise_cipher = DEFAULT_PAIRWISE;
2127 ssid->group_cipher = DEFAULT_GROUP;
2128 ssid->key_mgmt = DEFAULT_KEY_MGMT;
1f6c0ab8 2129 ssid->bg_scan_period = DEFAULT_BG_SCAN_PERIOD;
6fc6879b
JM
2130#ifdef IEEE8021X_EAPOL
2131 ssid->eapol_flags = DEFAULT_EAPOL_FLAGS;
2132 ssid->eap_workaround = DEFAULT_EAP_WORKAROUND;
2133 ssid->eap.fragment_size = DEFAULT_FRAGMENT_SIZE;
2134#endif /* IEEE8021X_EAPOL */
80e8a5ee
BG
2135#ifdef CONFIG_HT_OVERRIDES
2136 ssid->disable_ht = DEFAULT_DISABLE_HT;
2137 ssid->disable_ht40 = DEFAULT_DISABLE_HT40;
a90497f8 2138 ssid->disable_sgi = DEFAULT_DISABLE_SGI;
80e8a5ee
BG
2139 ssid->disable_max_amsdu = DEFAULT_DISABLE_MAX_AMSDU;
2140 ssid->ampdu_factor = DEFAULT_AMPDU_FACTOR;
2141 ssid->ampdu_density = DEFAULT_AMPDU_DENSITY;
2142#endif /* CONFIG_HT_OVERRIDES */
e9ee8dc3
JB
2143#ifdef CONFIG_VHT_OVERRIDES
2144 ssid->vht_rx_mcs_nss_1 = -1;
2145 ssid->vht_rx_mcs_nss_2 = -1;
2146 ssid->vht_rx_mcs_nss_3 = -1;
2147 ssid->vht_rx_mcs_nss_4 = -1;
2148 ssid->vht_rx_mcs_nss_5 = -1;
2149 ssid->vht_rx_mcs_nss_6 = -1;
2150 ssid->vht_rx_mcs_nss_7 = -1;
2151 ssid->vht_rx_mcs_nss_8 = -1;
2152 ssid->vht_tx_mcs_nss_1 = -1;
2153 ssid->vht_tx_mcs_nss_2 = -1;
2154 ssid->vht_tx_mcs_nss_3 = -1;
2155 ssid->vht_tx_mcs_nss_4 = -1;
2156 ssid->vht_tx_mcs_nss_5 = -1;
2157 ssid->vht_tx_mcs_nss_6 = -1;
2158 ssid->vht_tx_mcs_nss_7 = -1;
2159 ssid->vht_tx_mcs_nss_8 = -1;
2160#endif /* CONFIG_VHT_OVERRIDES */
6e202021 2161 ssid->proactive_key_caching = -1;
62d49803
JM
2162#ifdef CONFIG_IEEE80211W
2163 ssid->ieee80211w = MGMT_FRAME_PROTECTION_DEFAULT;
2164#endif /* CONFIG_IEEE80211W */
6fc6879b
JM
2165}
2166
2167
2168/**
2169 * wpa_config_set - Set a variable in network configuration
2170 * @ssid: Pointer to network configuration data
2171 * @var: Variable name, e.g., "ssid"
2172 * @value: Variable value
2173 * @line: Line number in configuration file or 0 if not used
2174 * Returns: 0 on success, -1 on failure
2175 *
2176 * This function can be used to set network configuration variables based on
2177 * both the configuration file and management interface input. The value
2178 * parameter must be in the same format as the text-based configuration file is
2179 * using. For example, strings are using double quotation marks.
2180 */
2181int wpa_config_set(struct wpa_ssid *ssid, const char *var, const char *value,
2182 int line)
2183{
2184 size_t i;
2185 int ret = 0;
2186
2187 if (ssid == NULL || var == NULL || value == NULL)
2188 return -1;
2189
2190 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2191 const struct parse_data *field = &ssid_fields[i];
2192 if (os_strcmp(var, field->name) != 0)
2193 continue;
2194
2195 if (field->parser(field, ssid, line, value)) {
2196 if (line) {
2197 wpa_printf(MSG_ERROR, "Line %d: failed to "
2198 "parse %s '%s'.", line, var, value);
2199 }
2200 ret = -1;
2201 }
2202 break;
2203 }
2204 if (i == NUM_SSID_FIELDS) {
2205 if (line) {
2206 wpa_printf(MSG_ERROR, "Line %d: unknown network field "
2207 "'%s'.", line, var);
2208 }
2209 ret = -1;
2210 }
2211
2212 return ret;
2213}
2214
2215
67e1b984
JM
2216int wpa_config_set_quoted(struct wpa_ssid *ssid, const char *var,
2217 const char *value)
2218{
2219 size_t len;
2220 char *buf;
2221 int ret;
2222
2223 len = os_strlen(value);
2224 buf = os_malloc(len + 3);
2225 if (buf == NULL)
2226 return -1;
2227 buf[0] = '"';
2228 os_memcpy(buf + 1, value, len);
2229 buf[len + 1] = '"';
2230 buf[len + 2] = '\0';
2231 ret = wpa_config_set(ssid, var, buf, 0);
2232 os_free(buf);
2233 return ret;
2234}
2235
2236
3d3d3056
WS
2237/**
2238 * wpa_config_get_all - Get all options from network configuration
2239 * @ssid: Pointer to network configuration data
2240 * @get_keys: Determines if keys/passwords will be included in returned list
d1c8ac88 2241 * (if they may be exported)
3d3d3056
WS
2242 * Returns: %NULL terminated list of all set keys and their values in the form
2243 * of [key1, val1, key2, val2, ... , NULL]
2244 *
2245 * This function can be used to get list of all configured network properties.
2246 * The caller is responsible for freeing the returned list and all its
2247 * elements.
2248 */
2249char ** wpa_config_get_all(struct wpa_ssid *ssid, int get_keys)
2250{
2251 const struct parse_data *field;
2252 char *key, *value;
2253 size_t i;
2254 char **props;
2255 int fields_num;
2256
d1c8ac88
JB
2257 get_keys = get_keys && ssid->export_keys;
2258
f9884c09 2259 props = os_calloc(2 * NUM_SSID_FIELDS + 1, sizeof(char *));
3d3d3056
WS
2260 if (!props)
2261 return NULL;
2262
2263 fields_num = 0;
2264 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2265 field = &ssid_fields[i];
2266 if (field->key_data && !get_keys)
2267 continue;
2268 value = field->writer(field, ssid);
04746865 2269 if (value == NULL)
3d3d3056 2270 continue;
04746865
JM
2271 if (os_strlen(value) == 0) {
2272 os_free(value);
2273 continue;
2274 }
3d3d3056
WS
2275
2276 key = os_strdup(field->name);
04746865
JM
2277 if (key == NULL) {
2278 os_free(value);
3d3d3056 2279 goto err;
04746865 2280 }
3d3d3056
WS
2281
2282 props[fields_num * 2] = key;
2283 props[fields_num * 2 + 1] = value;
2284
2285 fields_num++;
2286 }
2287
2288 return props;
2289
2290err:
2291 value = *props;
2292 while (value)
2293 os_free(value++);
2294 os_free(props);
2295 return NULL;
2296}
2297
2298
6fc6879b
JM
2299#ifndef NO_CONFIG_WRITE
2300/**
2301 * wpa_config_get - Get a variable in network configuration
2302 * @ssid: Pointer to network configuration data
2303 * @var: Variable name, e.g., "ssid"
2304 * Returns: Value of the variable or %NULL on failure
2305 *
2306 * This function can be used to get network configuration variables. The
2307 * returned value is a copy of the configuration variable in text format, i.e,.
2308 * the same format that the text-based configuration file and wpa_config_set()
2309 * are using for the value. The caller is responsible for freeing the returned
2310 * value.
2311 */
2312char * wpa_config_get(struct wpa_ssid *ssid, const char *var)
2313{
2314 size_t i;
2315
2316 if (ssid == NULL || var == NULL)
2317 return NULL;
2318
2319 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2320 const struct parse_data *field = &ssid_fields[i];
2321 if (os_strcmp(var, field->name) == 0)
2322 return field->writer(field, ssid);
2323 }
2324
2325 return NULL;
2326}
2327
2328
2329/**
2330 * wpa_config_get_no_key - Get a variable in network configuration (no keys)
2331 * @ssid: Pointer to network configuration data
2332 * @var: Variable name, e.g., "ssid"
2333 * Returns: Value of the variable or %NULL on failure
2334 *
2335 * This function can be used to get network configuration variable like
2336 * wpa_config_get(). The only difference is that this functions does not expose
2337 * key/password material from the configuration. In case a key/password field
2338 * is requested, the returned value is an empty string or %NULL if the variable
2339 * is not set or "*" if the variable is set (regardless of its value). The
2340 * returned value is a copy of the configuration variable in text format, i.e,.
2341 * the same format that the text-based configuration file and wpa_config_set()
2342 * are using for the value. The caller is responsible for freeing the returned
2343 * value.
2344 */
2345char * wpa_config_get_no_key(struct wpa_ssid *ssid, const char *var)
2346{
2347 size_t i;
2348
2349 if (ssid == NULL || var == NULL)
2350 return NULL;
2351
2352 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2353 const struct parse_data *field = &ssid_fields[i];
2354 if (os_strcmp(var, field->name) == 0) {
2355 char *res = field->writer(field, ssid);
2356 if (field->key_data) {
2357 if (res && res[0]) {
2358 wpa_printf(MSG_DEBUG, "Do not allow "
2359 "key_data field to be "
2360 "exposed");
2361 os_free(res);
2362 return os_strdup("*");
2363 }
2364
2365 os_free(res);
2366 return NULL;
2367 }
2368 return res;
2369 }
2370 }
2371
2372 return NULL;
2373}
2374#endif /* NO_CONFIG_WRITE */
2375
2376
2377/**
2378 * wpa_config_update_psk - Update WPA PSK based on passphrase and SSID
2379 * @ssid: Pointer to network configuration data
2380 *
2381 * This function must be called to update WPA PSK when either SSID or the
2382 * passphrase has changed for the network configuration.
2383 */
2384void wpa_config_update_psk(struct wpa_ssid *ssid)
2385{
2386#ifndef CONFIG_NO_PBKDF2
986de33d 2387 pbkdf2_sha1(ssid->passphrase, ssid->ssid, ssid->ssid_len, 4096,
6fc6879b
JM
2388 ssid->psk, PMK_LEN);
2389 wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
2390 ssid->psk, PMK_LEN);
2391 ssid->psk_set = 1;
2392#endif /* CONFIG_NO_PBKDF2 */
2393}
2394
2395
1bb7b8e8
JM
2396int wpa_config_set_cred(struct wpa_cred *cred, const char *var,
2397 const char *value, int line)
2398{
2399 char *val;
2400 size_t len;
2401
03ed3324
JM
2402 if (os_strcmp(var, "temporary") == 0) {
2403 cred->temporary = atoi(value);
2404 return 0;
2405 }
2406
1a712d2f
JM
2407 if (os_strcmp(var, "priority") == 0) {
2408 cred->priority = atoi(value);
2409 return 0;
2410 }
2411
d7b01abd
JM
2412 if (os_strcmp(var, "pcsc") == 0) {
2413 cred->pcsc = atoi(value);
2414 return 0;
2415 }
2416
8ca93c59
JM
2417 if (os_strcmp(var, "eap") == 0) {
2418 struct eap_method_type method;
2419 method.method = eap_peer_get_type(value, &method.vendor);
2420 if (method.vendor == EAP_VENDOR_IETF &&
2421 method.method == EAP_TYPE_NONE) {
2422 wpa_printf(MSG_ERROR, "Line %d: unknown EAP type '%s' "
2423 "for a credential", line, value);
2424 return -1;
2425 }
2426 os_free(cred->eap_method);
2427 cred->eap_method = os_malloc(sizeof(*cred->eap_method));
2428 if (cred->eap_method == NULL)
2429 return -1;
2430 os_memcpy(cred->eap_method, &method, sizeof(method));
2431 return 0;
2432 }
2433
02af9c90
JM
2434 if (os_strcmp(var, "password") == 0 &&
2435 os_strncmp(value, "ext:", 4) == 0) {
2436 os_free(cred->password);
2437 cred->password = os_strdup(value);
2438 cred->ext_password = 1;
2439 return 0;
2440 }
2441
1bb7b8e8 2442 val = wpa_config_parse_string(value, &len);
7d86e537
JM
2443 if (val == NULL) {
2444 wpa_printf(MSG_ERROR, "Line %d: invalid field '%s' string "
2445 "value '%s'.", line, var, value);
1bb7b8e8 2446 return -1;
7d86e537 2447 }
1bb7b8e8
JM
2448
2449 if (os_strcmp(var, "realm") == 0) {
2450 os_free(cred->realm);
2451 cred->realm = val;
2452 return 0;
2453 }
2454
2455 if (os_strcmp(var, "username") == 0) {
2456 os_free(cred->username);
2457 cred->username = val;
2458 return 0;
2459 }
2460
2461 if (os_strcmp(var, "password") == 0) {
2462 os_free(cred->password);
2463 cred->password = val;
02af9c90 2464 cred->ext_password = 0;
1bb7b8e8
JM
2465 return 0;
2466 }
2467
2468 if (os_strcmp(var, "ca_cert") == 0) {
2469 os_free(cred->ca_cert);
2470 cred->ca_cert = val;
2471 return 0;
2472 }
2473
11e4f46a
JM
2474 if (os_strcmp(var, "client_cert") == 0) {
2475 os_free(cred->client_cert);
2476 cred->client_cert = val;
2477 return 0;
2478 }
2479
2480 if (os_strcmp(var, "private_key") == 0) {
2481 os_free(cred->private_key);
2482 cred->private_key = val;
2483 return 0;
2484 }
2485
2486 if (os_strcmp(var, "private_key_passwd") == 0) {
2487 os_free(cred->private_key_passwd);
2488 cred->private_key_passwd = val;
2489 return 0;
2490 }
2491
1bb7b8e8
JM
2492 if (os_strcmp(var, "imsi") == 0) {
2493 os_free(cred->imsi);
2494 cred->imsi = val;
2495 return 0;
2496 }
2497
2498 if (os_strcmp(var, "milenage") == 0) {
2499 os_free(cred->milenage);
2500 cred->milenage = val;
2501 return 0;
2502 }
2503
ac1bc549
JM
2504 if (os_strcmp(var, "domain_suffix_match") == 0) {
2505 os_free(cred->domain_suffix_match);
2506 cred->domain_suffix_match = val;
2507 return 0;
2508 }
2509
1bb7b8e8 2510 if (os_strcmp(var, "domain") == 0) {
463c8ffb
JM
2511 char **new_domain;
2512 new_domain = os_realloc_array(cred->domain,
2513 cred->num_domain + 1,
2514 sizeof(char *));
2515 if (new_domain == NULL) {
2516 os_free(val);
2517 return -1;
2518 }
2519 new_domain[cred->num_domain++] = val;
2520 cred->domain = new_domain;
1bb7b8e8
JM
2521 return 0;
2522 }
2523
8ca93c59
JM
2524 if (os_strcmp(var, "phase1") == 0) {
2525 os_free(cred->phase1);
2526 cred->phase1 = val;
2527 return 0;
2528 }
2529
2530 if (os_strcmp(var, "phase2") == 0) {
2531 os_free(cred->phase2);
2532 cred->phase2 = val;
2533 return 0;
2534 }
2535
955567bc
JM
2536 if (os_strcmp(var, "roaming_consortium") == 0) {
2537 if (len < 3 || len > sizeof(cred->roaming_consortium)) {
2538 wpa_printf(MSG_ERROR, "Line %d: invalid "
2539 "roaming_consortium length %d (3..15 "
2540 "expected)", line, (int) len);
2541 os_free(val);
2542 return -1;
2543 }
2544 os_memcpy(cred->roaming_consortium, val, len);
2545 cred->roaming_consortium_len = len;
2546 os_free(val);
2547 return 0;
2548 }
2549
f47c1452
JM
2550 if (os_strcmp(var, "required_roaming_consortium") == 0) {
2551 if (len < 3 || len > sizeof(cred->required_roaming_consortium))
2552 {
2553 wpa_printf(MSG_ERROR, "Line %d: invalid "
2554 "required_roaming_consortium length %d "
2555 "(3..15 expected)", line, (int) len);
2556 os_free(val);
2557 return -1;
2558 }
2559 os_memcpy(cred->required_roaming_consortium, val, len);
2560 cred->required_roaming_consortium_len = len;
2561 os_free(val);
2562 return 0;
2563 }
2564
dbea8ac7
JM
2565 if (os_strcmp(var, "excluded_ssid") == 0) {
2566 struct excluded_ssid *e;
2567
2568 if (len > MAX_SSID_LEN) {
2569 wpa_printf(MSG_ERROR, "Line %d: invalid "
2570 "excluded_ssid length %d", line, (int) len);
2571 os_free(val);
2572 return -1;
2573 }
2574
2575 e = os_realloc_array(cred->excluded_ssid,
2576 cred->num_excluded_ssid + 1,
2577 sizeof(struct excluded_ssid));
2578 if (e == NULL) {
2579 os_free(val);
2580 return -1;
2581 }
2582 cred->excluded_ssid = e;
2583
2584 e = &cred->excluded_ssid[cred->num_excluded_ssid++];
2585 os_memcpy(e->ssid, val, len);
2586 e->ssid_len = len;
2587
2588 os_free(val);
2589
2590 return 0;
2591 }
2592
1bb7b8e8
JM
2593 if (line) {
2594 wpa_printf(MSG_ERROR, "Line %d: unknown cred field '%s'.",
2595 line, var);
2596 }
2597
f4b2d69b
JM
2598 os_free(val);
2599
1bb7b8e8
JM
2600 return -1;
2601}
2602
2603
d94c9ee6
JM
2604struct wpa_cred * wpa_config_get_cred(struct wpa_config *config, int id)
2605{
2606 struct wpa_cred *cred;
2607
2608 cred = config->cred;
2609 while (cred) {
2610 if (id == cred->id)
2611 break;
2612 cred = cred->next;
2613 }
2614
2615 return cred;
2616}
2617
2618
2619struct wpa_cred * wpa_config_add_cred(struct wpa_config *config)
2620{
2621 int id;
2622 struct wpa_cred *cred, *last = NULL;
2623
2624 id = -1;
2625 cred = config->cred;
2626 while (cred) {
2627 if (cred->id > id)
2628 id = cred->id;
2629 last = cred;
2630 cred = cred->next;
2631 }
2632 id++;
2633
2634 cred = os_zalloc(sizeof(*cred));
2635 if (cred == NULL)
2636 return NULL;
2637 cred->id = id;
2638 if (last)
2639 last->next = cred;
2640 else
2641 config->cred = cred;
2642
2643 return cred;
2644}
2645
2646
2647int wpa_config_remove_cred(struct wpa_config *config, int id)
2648{
2649 struct wpa_cred *cred, *prev = NULL;
2650
2651 cred = config->cred;
2652 while (cred) {
2653 if (id == cred->id)
2654 break;
2655 prev = cred;
2656 cred = cred->next;
2657 }
2658
2659 if (cred == NULL)
2660 return -1;
2661
2662 if (prev)
2663 prev->next = cred->next;
2664 else
2665 config->cred = cred->next;
2666
2667 wpa_config_free_cred(cred);
2668 return 0;
2669}
2670
2671
6fc6879b
JM
2672#ifndef CONFIG_NO_CONFIG_BLOBS
2673/**
2674 * wpa_config_get_blob - Get a named configuration blob
2675 * @config: Configuration data from wpa_config_read()
2676 * @name: Name of the blob
2677 * Returns: Pointer to blob data or %NULL if not found
2678 */
2679const struct wpa_config_blob * wpa_config_get_blob(struct wpa_config *config,
2680 const char *name)
2681{
2682 struct wpa_config_blob *blob = config->blobs;
2683
2684 while (blob) {
2685 if (os_strcmp(blob->name, name) == 0)
2686 return blob;
2687 blob = blob->next;
2688 }
2689 return NULL;
2690}
2691
2692
2693/**
2694 * wpa_config_set_blob - Set or add a named configuration blob
2695 * @config: Configuration data from wpa_config_read()
2696 * @blob: New value for the blob
2697 *
2698 * Adds a new configuration blob or replaces the current value of an existing
2699 * blob.
2700 */
2701void wpa_config_set_blob(struct wpa_config *config,
2702 struct wpa_config_blob *blob)
2703{
2704 wpa_config_remove_blob(config, blob->name);
2705 blob->next = config->blobs;
2706 config->blobs = blob;
2707}
2708
2709
2710/**
2711 * wpa_config_free_blob - Free blob data
2712 * @blob: Pointer to blob to be freed
2713 */
2714void wpa_config_free_blob(struct wpa_config_blob *blob)
2715{
2716 if (blob) {
2717 os_free(blob->name);
2718 os_free(blob->data);
2719 os_free(blob);
2720 }
2721}
2722
2723
2724/**
2725 * wpa_config_remove_blob - Remove a named configuration blob
2726 * @config: Configuration data from wpa_config_read()
2727 * @name: Name of the blob to remove
2728 * Returns: 0 if blob was removed or -1 if blob was not found
2729 */
2730int wpa_config_remove_blob(struct wpa_config *config, const char *name)
2731{
2732 struct wpa_config_blob *pos = config->blobs, *prev = NULL;
2733
2734 while (pos) {
2735 if (os_strcmp(pos->name, name) == 0) {
2736 if (prev)
2737 prev->next = pos->next;
2738 else
2739 config->blobs = pos->next;
2740 wpa_config_free_blob(pos);
2741 return 0;
2742 }
2743 prev = pos;
2744 pos = pos->next;
2745 }
2746
2747 return -1;
2748}
2749#endif /* CONFIG_NO_CONFIG_BLOBS */
2750
2751
2752/**
2753 * wpa_config_alloc_empty - Allocate an empty configuration
2754 * @ctrl_interface: Control interface parameters, e.g., path to UNIX domain
2755 * socket
2756 * @driver_param: Driver parameters
2757 * Returns: Pointer to allocated configuration data or %NULL on failure
2758 */
2759struct wpa_config * wpa_config_alloc_empty(const char *ctrl_interface,
2760 const char *driver_param)
2761{
2762 struct wpa_config *config;
c26effe1
YD
2763 const int aCWmin = 4, aCWmax = 10;
2764 const struct hostapd_wmm_ac_params ac_bk =
2765 { aCWmin, aCWmax, 7, 0, 0 }; /* background traffic */
2766 const struct hostapd_wmm_ac_params ac_be =
2767 { aCWmin, aCWmax, 3, 0, 0 }; /* best effort traffic */
2768 const struct hostapd_wmm_ac_params ac_vi = /* video traffic */
623ecdd5 2769 { aCWmin - 1, aCWmin, 2, 3000 / 32, 0 };
c26effe1 2770 const struct hostapd_wmm_ac_params ac_vo = /* voice traffic */
623ecdd5 2771 { aCWmin - 2, aCWmin - 1, 2, 1500 / 32, 0 };
6fc6879b
JM
2772
2773 config = os_zalloc(sizeof(*config));
2774 if (config == NULL)
2775 return NULL;
2776 config->eapol_version = DEFAULT_EAPOL_VERSION;
2777 config->ap_scan = DEFAULT_AP_SCAN;
2778 config->fast_reauth = DEFAULT_FAST_REAUTH;
e3768e7c 2779 config->p2p_go_intent = DEFAULT_P2P_GO_INTENT;
0f66abd2 2780 config->p2p_intra_bss = DEFAULT_P2P_INTRA_BSS;
462a7439 2781 config->p2p_go_max_inactivity = DEFAULT_P2P_GO_MAX_INACTIVITY;
c9c38b09 2782 config->bss_max_count = DEFAULT_BSS_MAX_COUNT;
78633c37
SL
2783 config->bss_expiration_age = DEFAULT_BSS_EXPIRATION_AGE;
2784 config->bss_expiration_scan_count = DEFAULT_BSS_EXPIRATION_SCAN_COUNT;
dae608d5 2785 config->max_num_sta = DEFAULT_MAX_NUM_STA;
11540c0b 2786 config->access_network_type = DEFAULT_ACCESS_NETWORK_TYPE;
6124e858 2787 config->scan_cur_freq = DEFAULT_SCAN_CUR_FREQ;
c26effe1
YD
2788 config->wmm_ac_params[0] = ac_be;
2789 config->wmm_ac_params[1] = ac_bk;
2790 config->wmm_ac_params[2] = ac_vi;
2791 config->wmm_ac_params[3] = ac_vo;
6fc6879b
JM
2792
2793 if (ctrl_interface)
2794 config->ctrl_interface = os_strdup(ctrl_interface);
2795 if (driver_param)
2796 config->driver_param = os_strdup(driver_param);
2797
2798 return config;
2799}
2800
2801
2802#ifndef CONFIG_NO_STDOUT_DEBUG
2803/**
2804 * wpa_config_debug_dump_networks - Debug dump of configured networks
2805 * @config: Configuration data from wpa_config_read()
2806 */
2807void wpa_config_debug_dump_networks(struct wpa_config *config)
2808{
2809 int prio;
2810 struct wpa_ssid *ssid;
2811
2812 for (prio = 0; prio < config->num_prio; prio++) {
2813 ssid = config->pssid[prio];
2814 wpa_printf(MSG_DEBUG, "Priority group %d",
2815 ssid->priority);
2816 while (ssid) {
2817 wpa_printf(MSG_DEBUG, " id=%d ssid='%s'",
2818 ssid->id,
2819 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
2820 ssid = ssid->pnext;
2821 }
2822 }
2823}
2824#endif /* CONFIG_NO_STDOUT_DEBUG */
121adf9c
JM
2825
2826
2827struct global_parse_data {
2828 char *name;
2829 int (*parser)(const struct global_parse_data *data,
2830 struct wpa_config *config, int line, const char *value);
2831 void *param1, *param2, *param3;
1d47214a 2832 unsigned int changed_flag;
121adf9c
JM
2833};
2834
2835
2836static int wpa_global_config_parse_int(const struct global_parse_data *data,
2837 struct wpa_config *config, int line,
2838 const char *pos)
2839{
eae3a584
JB
2840 int val, *dst;
2841 char *end;
2842
121adf9c 2843 dst = (int *) (((u8 *) config) + (long) data->param1);
eae3a584
JB
2844 val = strtol(pos, &end, 0);
2845 if (*end) {
2846 wpa_printf(MSG_ERROR, "Line %d: invalid number \"%s\"",
2847 line, pos);
2848 return -1;
2849 }
2850 *dst = val;
2851
121adf9c
JM
2852 wpa_printf(MSG_DEBUG, "%s=%d", data->name, *dst);
2853
2854 if (data->param2 && *dst < (long) data->param2) {
2855 wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
2856 "min_value=%ld)", line, data->name, *dst,
2857 (long) data->param2);
2858 *dst = (long) data->param2;
2859 return -1;
2860 }
2861
2862 if (data->param3 && *dst > (long) data->param3) {
2863 wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
2864 "max_value=%ld)", line, data->name, *dst,
2865 (long) data->param3);
2866 *dst = (long) data->param3;
2867 return -1;
2868 }
2869
2870 return 0;
2871}
2872
2873
2874static int wpa_global_config_parse_str(const struct global_parse_data *data,
2875 struct wpa_config *config, int line,
2876 const char *pos)
2877{
2878 size_t len;
2879 char **dst, *tmp;
2880
2881 len = os_strlen(pos);
2882 if (data->param2 && len < (size_t) data->param2) {
2883 wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
2884 "min_len=%ld)", line, data->name,
2885 (unsigned long) len, (long) data->param2);
2886 return -1;
2887 }
2888
2889 if (data->param3 && len > (size_t) data->param3) {
2890 wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
2891 "max_len=%ld)", line, data->name,
2892 (unsigned long) len, (long) data->param3);
2893 return -1;
2894 }
2895
2896 tmp = os_strdup(pos);
2897 if (tmp == NULL)
2898 return -1;
2899
2900 dst = (char **) (((u8 *) config) + (long) data->param1);
2901 os_free(*dst);
2902 *dst = tmp;
2903 wpa_printf(MSG_DEBUG, "%s='%s'", data->name, *dst);
2904
2905 return 0;
2906}
2907
2908
31392709
HD
2909static int wpa_config_process_bgscan(const struct global_parse_data *data,
2910 struct wpa_config *config, int line,
2911 const char *pos)
2912{
2913 size_t len;
2914 char *tmp;
2915
2916 tmp = wpa_config_parse_string(pos, &len);
2917 if (tmp == NULL) {
2918 wpa_printf(MSG_ERROR, "Line %d: failed to parse %s",
2919 line, data->name);
2920 return -1;
2921 }
2922
2923 return wpa_global_config_parse_str(data, config, line, tmp);
2924}
2925
2926
3f2c8ba6
JM
2927static int wpa_global_config_parse_bin(const struct global_parse_data *data,
2928 struct wpa_config *config, int line,
2929 const char *pos)
2930{
2931 size_t len;
2932 struct wpabuf **dst, *tmp;
2933
2934 len = os_strlen(pos);
2935 if (len & 0x01)
2936 return -1;
2937
2938 tmp = wpabuf_alloc(len / 2);
2939 if (tmp == NULL)
2940 return -1;
2941
2942 if (hexstr2bin(pos, wpabuf_put(tmp, len / 2), len / 2)) {
2943 wpabuf_free(tmp);
2944 return -1;
2945 }
2946
2947 dst = (struct wpabuf **) (((u8 *) config) + (long) data->param1);
2948 wpabuf_free(*dst);
2949 *dst = tmp;
2950 wpa_printf(MSG_DEBUG, "%s", data->name);
2951
2952 return 0;
2953}
2954
2955
f5ffc348
BG
2956static int wpa_config_process_freq_list(const struct global_parse_data *data,
2957 struct wpa_config *config, int line,
2958 const char *value)
2959{
2960 int *freqs;
2961
2962 freqs = wpa_config_parse_int_array(value);
2963 if (freqs == NULL)
2964 return -1;
6668efda
JM
2965 if (freqs[0] == 0) {
2966 os_free(freqs);
2967 freqs = NULL;
2968 }
f5ffc348
BG
2969 os_free(config->freq_list);
2970 config->freq_list = freqs;
2971 return 0;
2972}
2973
2974
25ef8529
JM
2975#ifdef CONFIG_P2P
2976static int wpa_global_config_parse_ipv4(const struct global_parse_data *data,
2977 struct wpa_config *config, int line,
2978 const char *pos)
2979{
2980 u32 *dst;
2981 struct hostapd_ip_addr addr;
2982
2983 if (hostapd_parse_ip_addr(pos, &addr) < 0)
2984 return -1;
2985 if (addr.af != AF_INET)
2986 return -1;
2987
2988 dst = (u32 *) (((u8 *) config) + (long) data->param1);
2989 os_memcpy(dst, &addr.u.v4.s_addr, 4);
2990 wpa_printf(MSG_DEBUG, "%s = 0x%x", data->name,
2991 WPA_GET_BE32((u8 *) dst));
2992
2993 return 0;
2994}
2995#endif /* CONFIG_P2P */
2996
2997
121adf9c
JM
2998static int wpa_config_process_country(const struct global_parse_data *data,
2999 struct wpa_config *config, int line,
3000 const char *pos)
3001{
3002 if (!pos[0] || !pos[1]) {
3003 wpa_printf(MSG_DEBUG, "Invalid country set");
3004 return -1;
3005 }
3006 config->country[0] = pos[0];
3007 config->country[1] = pos[1];
3008 wpa_printf(MSG_DEBUG, "country='%c%c'",
3009 config->country[0], config->country[1]);
3010 return 0;
3011}
3012
3013
3014static int wpa_config_process_load_dynamic_eap(
3015 const struct global_parse_data *data, struct wpa_config *config,
3016 int line, const char *so)
3017{
3018 int ret;
3019 wpa_printf(MSG_DEBUG, "load_dynamic_eap=%s", so);
3020 ret = eap_peer_method_load(so);
3021 if (ret == -2) {
3022 wpa_printf(MSG_DEBUG, "This EAP type was already loaded - not "
3023 "reloading.");
3024 } else if (ret) {
3025 wpa_printf(MSG_ERROR, "Line %d: Failed to load dynamic EAP "
3026 "method '%s'.", line, so);
3027 return -1;
3028 }
3029
3030 return 0;
3031}
3032
3033
3034#ifdef CONFIG_WPS
3035
3036static int wpa_config_process_uuid(const struct global_parse_data *data,
3037 struct wpa_config *config, int line,
3038 const char *pos)
3039{
3040 char buf[40];
3041 if (uuid_str2bin(pos, config->uuid)) {
3042 wpa_printf(MSG_ERROR, "Line %d: invalid UUID", line);
3043 return -1;
3044 }
3045 uuid_bin2str(config->uuid, buf, sizeof(buf));
3046 wpa_printf(MSG_DEBUG, "uuid=%s", buf);
3047 return 0;
3048}
3049
3050
2f646b6e
JB
3051static int wpa_config_process_device_type(
3052 const struct global_parse_data *data,
3053 struct wpa_config *config, int line, const char *pos)
3054{
3055 return wps_dev_type_str2bin(pos, config->device_type);
3056}
3057
3058
121adf9c
JM
3059static int wpa_config_process_os_version(const struct global_parse_data *data,
3060 struct wpa_config *config, int line,
3061 const char *pos)
3062{
3063 if (hexstr2bin(pos, config->os_version, 4)) {
3064 wpa_printf(MSG_ERROR, "Line %d: invalid os_version", line);
3065 return -1;
3066 }
3067 wpa_printf(MSG_DEBUG, "os_version=%08x",
3068 WPA_GET_BE32(config->os_version));
3069 return 0;
3070}
3071
71dd3b78
AS
3072
3073static int wpa_config_process_wps_vendor_ext_m1(
3074 const struct global_parse_data *data,
3075 struct wpa_config *config, int line, const char *pos)
3076{
3077 struct wpabuf *tmp;
3078 int len = os_strlen(pos) / 2;
3079 u8 *p;
3080
3081 if (!len) {
3082 wpa_printf(MSG_ERROR, "Line %d: "
3083 "invalid wps_vendor_ext_m1", line);
3084 return -1;
3085 }
3086
3087 tmp = wpabuf_alloc(len);
3088 if (tmp) {
3089 p = wpabuf_put(tmp, len);
3090
3091 if (hexstr2bin(pos, p, len)) {
3092 wpa_printf(MSG_ERROR, "Line %d: "
3093 "invalid wps_vendor_ext_m1", line);
3094 wpabuf_free(tmp);
3095 return -1;
3096 }
3097
3098 wpabuf_free(config->wps_vendor_ext_m1);
3099 config->wps_vendor_ext_m1 = tmp;
3100 } else {
3101 wpa_printf(MSG_ERROR, "Can not allocate "
3102 "memory for wps_vendor_ext_m1");
3103 return -1;
3104 }
3105
3106 return 0;
3107}
3108
121adf9c
JM
3109#endif /* CONFIG_WPS */
3110
e3768e7c
JM
3111#ifdef CONFIG_P2P
3112static int wpa_config_process_sec_device_type(
3113 const struct global_parse_data *data,
3114 struct wpa_config *config, int line, const char *pos)
3115{
3116 int idx;
3117
2f646b6e 3118 if (config->num_sec_device_types >= MAX_SEC_DEVICE_TYPES) {
e3768e7c
JM
3119 wpa_printf(MSG_ERROR, "Line %d: too many sec_device_type "
3120 "items", line);
3121 return -1;
3122 }
3123
2f646b6e
JB
3124 idx = config->num_sec_device_types;
3125
3126 if (wps_dev_type_str2bin(pos, config->sec_device_type[idx]))
e3768e7c 3127 return -1;
2f646b6e
JB
3128
3129 config->num_sec_device_types++;
e3768e7c
JM
3130 return 0;
3131}
21d996f7
JM
3132
3133
3134static int wpa_config_process_p2p_pref_chan(
3135 const struct global_parse_data *data,
3136 struct wpa_config *config, int line, const char *pos)
3137{
3138 struct p2p_channel *pref = NULL, *n;
3139 unsigned int num = 0;
3140 const char *pos2;
3141 u8 op_class, chan;
3142
3143 /* format: class:chan,class:chan,... */
3144
3145 while (*pos) {
3146 op_class = atoi(pos);
3147 pos2 = os_strchr(pos, ':');
3148 if (pos2 == NULL)
3149 goto fail;
3150 pos2++;
3151 chan = atoi(pos2);
3152
067ffa26
JM
3153 n = os_realloc_array(pref, num + 1,
3154 sizeof(struct p2p_channel));
21d996f7
JM
3155 if (n == NULL)
3156 goto fail;
3157 pref = n;
3158 pref[num].op_class = op_class;
3159 pref[num].chan = chan;
3160 num++;
3161
3162 pos = os_strchr(pos2, ',');
3163 if (pos == NULL)
3164 break;
3165 pos++;
3166 }
3167
3168 os_free(config->p2p_pref_chan);
3169 config->p2p_pref_chan = pref;
3170 config->num_p2p_pref_chan = num;
3171 wpa_hexdump(MSG_DEBUG, "P2P: Preferred class/channel pairs",
3172 (u8 *) config->p2p_pref_chan,
3173 config->num_p2p_pref_chan * sizeof(struct p2p_channel));
3174
3175 return 0;
3176
3177fail:
3178 os_free(pref);
3179 wpa_printf(MSG_ERROR, "Line %d: Invalid p2p_pref_chan list", line);
3180 return -1;
3181}
556b30da
JM
3182
3183
3184static int wpa_config_process_p2p_no_go_freq(
3185 const struct global_parse_data *data,
3186 struct wpa_config *config, int line, const char *pos)
3187{
3188 int ret;
3189
3190 ret = freq_range_list_parse(&config->p2p_no_go_freq, pos);
3191 if (ret < 0) {
3192 wpa_printf(MSG_ERROR, "Line %d: Invalid p2p_no_go_freq", line);
3193 return -1;
3194 }
3195
3196 wpa_printf(MSG_DEBUG, "P2P: p2p_no_go_freq with %u items",
3197 config->p2p_no_go_freq.num);
3198
3199 return 0;
3200}
3201
e3768e7c
JM
3202#endif /* CONFIG_P2P */
3203
121adf9c 3204
46ee0427
JM
3205static int wpa_config_process_hessid(
3206 const struct global_parse_data *data,
3207 struct wpa_config *config, int line, const char *pos)
3208{
3209 if (hwaddr_aton2(pos, config->hessid) < 0) {
3210 wpa_printf(MSG_ERROR, "Line %d: Invalid hessid '%s'",
3211 line, pos);
3212 return -1;
3213 }
3214
3215 return 0;
3216}
3217
3218
625f202a
JM
3219static int wpa_config_process_sae_groups(
3220 const struct global_parse_data *data,
3221 struct wpa_config *config, int line, const char *pos)
3222{
3223 int *groups = wpa_config_parse_int_array(pos);
3224 if (groups == NULL) {
3225 wpa_printf(MSG_ERROR, "Line %d: Invalid sae_groups '%s'",
3226 line, pos);
3227 return -1;
3228 }
3229
3230 os_free(config->sae_groups);
3231 config->sae_groups = groups;
3232
3233 return 0;
3234}
3235
3236
18a2eaab
JM
3237static int wpa_config_process_ap_vendor_elements(
3238 const struct global_parse_data *data,
3239 struct wpa_config *config, int line, const char *pos)
3240{
3241 struct wpabuf *tmp;
3242 int len = os_strlen(pos) / 2;
3243 u8 *p;
3244
3245 if (!len) {
3246 wpa_printf(MSG_ERROR, "Line %d: invalid ap_vendor_elements",
3247 line);
3248 return -1;
3249 }
3250
3251 tmp = wpabuf_alloc(len);
3252 if (tmp) {
3253 p = wpabuf_put(tmp, len);
3254
3255 if (hexstr2bin(pos, p, len)) {
3256 wpa_printf(MSG_ERROR, "Line %d: invalid "
3257 "ap_vendor_elements", line);
3258 wpabuf_free(tmp);
3259 return -1;
3260 }
3261
3262 wpabuf_free(config->ap_vendor_elements);
3263 config->ap_vendor_elements = tmp;
3264 } else {
3265 wpa_printf(MSG_ERROR, "Cannot allocate memory for "
3266 "ap_vendor_elements");
3267 return -1;
3268 }
3269
3270 return 0;
3271}
3272
3273
298f5185 3274#ifdef CONFIG_CTRL_IFACE
ea61aa1d
JM
3275static int wpa_config_process_no_ctrl_interface(
3276 const struct global_parse_data *data,
3277 struct wpa_config *config, int line, const char *pos)
3278{
3279 wpa_printf(MSG_DEBUG, "no_ctrl_interface -> ctrl_interface=NULL");
3280 os_free(config->ctrl_interface);
3281 config->ctrl_interface = NULL;
3282 return 0;
3283}
298f5185 3284#endif /* CONFIG_CTRL_IFACE */
ea61aa1d
JM
3285
3286
121adf9c
JM
3287#ifdef OFFSET
3288#undef OFFSET
3289#endif /* OFFSET */
3290/* OFFSET: Get offset of a variable within the wpa_config structure */
3291#define OFFSET(v) ((void *) &((struct wpa_config *) 0)->v)
3292
3293#define FUNC(f) #f, wpa_config_process_ ## f, OFFSET(f), NULL, NULL
3294#define FUNC_NO_VAR(f) #f, wpa_config_process_ ## f, NULL, NULL, NULL
3295#define _INT(f) #f, wpa_global_config_parse_int, OFFSET(f)
3296#define INT(f) _INT(f), NULL, NULL
3297#define INT_RANGE(f, min, max) _INT(f), (void *) min, (void *) max
3298#define _STR(f) #f, wpa_global_config_parse_str, OFFSET(f)
3299#define STR(f) _STR(f), NULL, NULL
3300#define STR_RANGE(f, min, max) _STR(f), (void *) min, (void *) max
3f2c8ba6 3301#define BIN(f) #f, wpa_global_config_parse_bin, OFFSET(f), NULL, NULL
25ef8529 3302#define IPV4(f) #f, wpa_global_config_parse_ipv4, OFFSET(f), NULL, NULL
121adf9c
JM
3303
3304static const struct global_parse_data global_fields[] = {
3305#ifdef CONFIG_CTRL_IFACE
1d47214a 3306 { STR(ctrl_interface), 0 },
ea61aa1d 3307 { FUNC_NO_VAR(no_ctrl_interface), 0 },
1d47214a 3308 { STR(ctrl_interface_group), 0 } /* deprecated */,
121adf9c 3309#endif /* CONFIG_CTRL_IFACE */
1d47214a
JM
3310 { INT_RANGE(eapol_version, 1, 2), 0 },
3311 { INT(ap_scan), 0 },
31392709 3312 { FUNC(bgscan), 0 },
54ddd743 3313 { INT(disable_scan_offload), 0 },
1d47214a
JM
3314 { INT(fast_reauth), 0 },
3315 { STR(opensc_engine_path), 0 },
3316 { STR(pkcs11_engine_path), 0 },
3317 { STR(pkcs11_module_path), 0 },
f64adcd7
JM
3318 { STR(pcsc_reader), 0 },
3319 { STR(pcsc_pin), 0 },
a5d44ac0 3320 { INT(external_sim), 0 },
1d47214a
JM
3321 { STR(driver_param), 0 },
3322 { INT(dot11RSNAConfigPMKLifetime), 0 },
3323 { INT(dot11RSNAConfigPMKReauthThreshold), 0 },
3324 { INT(dot11RSNAConfigSATimeout), 0 },
121adf9c 3325#ifndef CONFIG_NO_CONFIG_WRITE
1d47214a 3326 { INT(update_config), 0 },
121adf9c 3327#endif /* CONFIG_NO_CONFIG_WRITE */
1d47214a 3328 { FUNC_NO_VAR(load_dynamic_eap), 0 },
121adf9c 3329#ifdef CONFIG_WPS
1d47214a
JM
3330 { FUNC(uuid), CFG_CHANGED_UUID },
3331 { STR_RANGE(device_name, 0, 32), CFG_CHANGED_DEVICE_NAME },
1c9cb49f
JM
3332 { STR_RANGE(manufacturer, 0, 64), CFG_CHANGED_WPS_STRING },
3333 { STR_RANGE(model_name, 0, 32), CFG_CHANGED_WPS_STRING },
3334 { STR_RANGE(model_number, 0, 32), CFG_CHANGED_WPS_STRING },
3335 { STR_RANGE(serial_number, 0, 32), CFG_CHANGED_WPS_STRING },
2f646b6e 3336 { FUNC(device_type), CFG_CHANGED_DEVICE_TYPE },
1d47214a
JM
3337 { FUNC(os_version), CFG_CHANGED_OS_VERSION },
3338 { STR(config_methods), CFG_CHANGED_CONFIG_METHODS },
3339 { INT_RANGE(wps_cred_processing, 0, 2), 0 },
71dd3b78 3340 { FUNC(wps_vendor_ext_m1), CFG_CHANGED_VENDOR_EXTENSION },
121adf9c 3341#endif /* CONFIG_WPS */
e3768e7c
JM
3342#ifdef CONFIG_P2P
3343 { FUNC(sec_device_type), CFG_CHANGED_SEC_DEVICE_TYPE },
3344 { INT(p2p_listen_reg_class), 0 },
3345 { INT(p2p_listen_channel), 0 },
00eb2993
JM
3346 { INT(p2p_oper_reg_class), CFG_CHANGED_P2P_OPER_CHANNEL },
3347 { INT(p2p_oper_channel), CFG_CHANGED_P2P_OPER_CHANNEL },
e3768e7c
JM
3348 { INT_RANGE(p2p_go_intent, 0, 15), 0 },
3349 { STR(p2p_ssid_postfix), CFG_CHANGED_P2P_SSID_POSTFIX },
3350 { INT_RANGE(persistent_reconnect, 0, 1), 0 },
0f66abd2 3351 { INT_RANGE(p2p_intra_bss, 0, 1), CFG_CHANGED_P2P_INTRA_BSS },
3071e181 3352 { INT(p2p_group_idle), 0 },
21d996f7 3353 { FUNC(p2p_pref_chan), CFG_CHANGED_P2P_PREF_CHAN },
556b30da 3354 { FUNC(p2p_no_go_freq), CFG_CHANGED_P2P_PREF_CHAN },
51e9f228 3355 { INT_RANGE(p2p_add_cli_chan, 0, 1), 0 },
a93a15bb 3356 { INT(p2p_go_ht40), 0 },
20ea1ca4 3357 { INT(p2p_go_vht), 0 },
7a808c7e 3358 { INT(p2p_disabled), 0 },
d76cd41a 3359 { INT(p2p_no_group_iface), 0 },
b277a2be 3360 { INT_RANGE(p2p_ignore_shared_freq, 0, 1), 0 },
25ef8529
JM
3361 { IPV4(ip_addr_go), 0 },
3362 { IPV4(ip_addr_mask), 0 },
3363 { IPV4(ip_addr_start), 0 },
3364 { IPV4(ip_addr_end), 0 },
e3768e7c 3365#endif /* CONFIG_P2P */
1d47214a
JM
3366 { FUNC(country), CFG_CHANGED_COUNTRY },
3367 { INT(bss_max_count), 0 },
78633c37
SL
3368 { INT(bss_expiration_age), 0 },
3369 { INT(bss_expiration_scan_count), 0 },
dae608d5 3370 { INT_RANGE(filter_ssids, 0, 1), 0 },
bf8d6d24 3371 { INT_RANGE(filter_rssi, -100, 0), 0 },
0d7e5a3a 3372 { INT(max_num_sta), 0 },
46ee0427 3373 { INT_RANGE(disassoc_low_ack, 0, 1), 0 },
66aadbd7
JK
3374#ifdef CONFIG_HS20
3375 { INT_RANGE(hs20, 0, 1), 0 },
3376#endif /* CONFIG_HS20 */
46ee0427 3377 { INT_RANGE(interworking, 0, 1), 0 },
11540c0b 3378 { FUNC(hessid), 0 },
1298c145 3379 { INT_RANGE(access_network_type, 0, 15), 0 },
b0786fba 3380 { INT_RANGE(pbc_in_m1, 0, 1), 0 },
3f2c8ba6 3381 { STR(autoscan), 0 },
042ec551
JM
3382 { INT_RANGE(wps_nfc_dev_pw_id, 0x10, 0xffff),
3383 CFG_CHANGED_NFC_PASSWORD_TOKEN },
3384 { BIN(wps_nfc_dh_pubkey), CFG_CHANGED_NFC_PASSWORD_TOKEN },
3385 { BIN(wps_nfc_dh_privkey), CFG_CHANGED_NFC_PASSWORD_TOKEN },
3386 { BIN(wps_nfc_dev_pw), CFG_CHANGED_NFC_PASSWORD_TOKEN },
462a7439
ES
3387 { STR(ext_password_backend), CFG_CHANGED_EXT_PW_BACKEND },
3388 { INT(p2p_go_max_inactivity), 0 },
4d5bda5f 3389 { INT_RANGE(auto_interworking, 0, 1), 0 },
6e202021 3390 { INT(okc), 0 },
62d49803 3391 { INT(pmf), 0 },
625f202a 3392 { FUNC(sae_groups), 0 },
18206e02
JM
3393 { INT(dtim_period), 0 },
3394 { INT(beacon_int), 0 },
18a2eaab 3395 { FUNC(ap_vendor_elements), 0 },
4342326f 3396 { INT_RANGE(ignore_old_scan_res, 0, 1), 0 },
f5ffc348 3397 { FUNC(freq_list), 0 },
6124e858 3398 { INT(scan_cur_freq), 0 },
4aa81868 3399 { INT(sched_scan_interval), 0 },
800d5872 3400 { INT(tdls_external_control), 0},
121adf9c
JM
3401};
3402
3403#undef FUNC
3404#undef _INT
3405#undef INT
3406#undef INT_RANGE
3407#undef _STR
3408#undef STR
3409#undef STR_RANGE
3f2c8ba6 3410#undef BIN
25ef8529 3411#undef IPV4
e7ecab4a 3412#define NUM_GLOBAL_FIELDS ARRAY_SIZE(global_fields)
121adf9c
JM
3413
3414
3415int wpa_config_process_global(struct wpa_config *config, char *pos, int line)
3416{
3417 size_t i;
3418 int ret = 0;
3419
3420 for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
3421 const struct global_parse_data *field = &global_fields[i];
3422 size_t flen = os_strlen(field->name);
3423 if (os_strncmp(pos, field->name, flen) != 0 ||
3424 pos[flen] != '=')
3425 continue;
3426
3427 if (field->parser(field, config, line, pos + flen + 1)) {
3428 wpa_printf(MSG_ERROR, "Line %d: failed to "
3429 "parse '%s'.", line, pos);
3430 ret = -1;
3431 }
042ec551
JM
3432 if (field->changed_flag == CFG_CHANGED_NFC_PASSWORD_TOKEN)
3433 config->wps_nfc_pw_from_config = 1;
1d47214a 3434 config->changed_parameters |= field->changed_flag;
121adf9c
JM
3435 break;
3436 }
3437 if (i == NUM_GLOBAL_FIELDS) {
c26effe1
YD
3438#ifdef CONFIG_AP
3439 if (os_strncmp(pos, "wmm_ac_", 7) == 0) {
3440 char *tmp = os_strchr(pos, '=');
3441 if (tmp == NULL) {
3442 if (line < 0)
3443 return -1;
3444 wpa_printf(MSG_ERROR, "Line %d: invalid line "
3445 "'%s'", line, pos);
3446 return -1;
3447 }
3448 *tmp++ = '\0';
3449 if (hostapd_config_wmm_ac(config->wmm_ac_params, pos,
3450 tmp)) {
3451 wpa_printf(MSG_ERROR, "Line %d: invalid WMM "
3452 "AC item", line);
3453 return -1;
3454 }
3455 }
3456#endif /* CONFIG_AP */
1d47214a
JM
3457 if (line < 0)
3458 return -1;
121adf9c
JM
3459 wpa_printf(MSG_ERROR, "Line %d: unknown global field '%s'.",
3460 line, pos);
3461 ret = -1;
3462 }
3463
3464 return ret;
3465}