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