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