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