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