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