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