2 * WPA Supplicant / Configuration parser and common functions
3 * Copyright (c) 2003-2008, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
18 #include "utils/uuid.h"
19 #include "crypto/sha1.h"
20 #include "rsn_supp/wpa.h"
21 #include "eap_peer/eap.h"
25 #if !defined(CONFIG_CTRL_IFACE) && defined(CONFIG_NO_CONFIG_WRITE)
26 #define NO_CONFIG_WRITE
30 * Structure for network configuration parsing. This data is used to implement
31 * a generic parser for each network block variable. The table of configuration
32 * variables is defined below in this file (ssid_fields[]).
35 /* Configuration variable name */
38 /* Parser function for this variable */
39 int (*parser)(const struct parse_data *data, struct wpa_ssid *ssid,
40 int line, const char *value);
42 #ifndef NO_CONFIG_WRITE
43 /* Writer function (i.e., to get the variable in text format from
44 * internal presentation). */
45 char * (*writer)(const struct parse_data *data, struct wpa_ssid *ssid);
46 #endif /* NO_CONFIG_WRITE */
48 /* Variable specific parameters for the parser. */
49 void *param1, *param2, *param3, *param4;
51 /* 0 = this variable can be included in debug output and ctrl_iface
52 * 1 = this variable contains key/private data and it must not be
53 * included in debug output unless explicitly requested. In
54 * addition, this variable will not be readable through the
61 static char * wpa_config_parse_string(const char *value, size_t *len)
67 pos = os_strrchr(value, '"');
68 if (pos == NULL || pos[1] != '\0')
71 str = os_malloc(*len + 1);
74 os_memcpy(str, value, *len);
79 size_t tlen, hlen = os_strlen(value);
83 str = os_malloc(tlen + 1);
86 if (hexstr2bin(value, str, tlen)) {
97 static int wpa_config_parse_str(const struct parse_data *data,
98 struct wpa_ssid *ssid,
99 int line, const char *value)
101 size_t res_len, *dst_len;
104 if (os_strcmp(value, "NULL") == 0) {
105 wpa_printf(MSG_DEBUG, "Unset configuration string '%s'",
112 tmp = wpa_config_parse_string(value, &res_len);
114 wpa_printf(MSG_ERROR, "Line %d: failed to parse %s '%s'.",
116 data->key_data ? "[KEY DATA REMOVED]" : value);
120 if (data->key_data) {
121 wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
122 (u8 *) tmp, res_len);
124 wpa_hexdump_ascii(MSG_MSGDUMP, data->name,
125 (u8 *) tmp, res_len);
128 if (data->param3 && res_len < (size_t) data->param3) {
129 wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
130 "min_len=%ld)", line, data->name,
131 (unsigned long) res_len, (long) data->param3);
136 if (data->param4 && res_len > (size_t) data->param4) {
137 wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
138 "max_len=%ld)", line, data->name,
139 (unsigned long) res_len, (long) data->param4);
145 dst = (char **) (((u8 *) ssid) + (long) data->param1);
146 dst_len = (size_t *) (((u8 *) ssid) + (long) data->param2);
156 #ifndef NO_CONFIG_WRITE
157 static int is_hex(const u8 *data, size_t len)
161 for (i = 0; i < len; i++) {
162 if (data[i] < 32 || data[i] >= 127)
169 static char * wpa_config_write_string_ascii(const u8 *value, size_t len)
173 buf = os_malloc(len + 3);
177 os_memcpy(buf + 1, value, len);
185 static char * wpa_config_write_string_hex(const u8 *value, size_t len)
189 buf = os_zalloc(2 * len + 1);
192 wpa_snprintf_hex(buf, 2 * len + 1, value, len);
198 static char * wpa_config_write_string(const u8 *value, size_t len)
203 if (is_hex(value, len))
204 return wpa_config_write_string_hex(value, len);
206 return wpa_config_write_string_ascii(value, len);
210 static char * wpa_config_write_str(const struct parse_data *data,
211 struct wpa_ssid *ssid)
216 src = (char **) (((u8 *) ssid) + (long) data->param1);
221 len = *((size_t *) (((u8 *) ssid) + (long) data->param2));
223 len = os_strlen(*src);
225 return wpa_config_write_string((const u8 *) *src, len);
227 #endif /* NO_CONFIG_WRITE */
230 static int wpa_config_parse_int(const struct parse_data *data,
231 struct wpa_ssid *ssid,
232 int line, const char *value)
236 dst = (int *) (((u8 *) ssid) + (long) data->param1);
238 wpa_printf(MSG_MSGDUMP, "%s=%d (0x%x)", data->name, *dst, *dst);
240 if (data->param3 && *dst < (long) data->param3) {
241 wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
242 "min_value=%ld)", line, data->name, *dst,
243 (long) data->param3);
244 *dst = (long) data->param3;
248 if (data->param4 && *dst > (long) data->param4) {
249 wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
250 "max_value=%ld)", line, data->name, *dst,
251 (long) data->param4);
252 *dst = (long) data->param4;
260 #ifndef NO_CONFIG_WRITE
261 static char * wpa_config_write_int(const struct parse_data *data,
262 struct wpa_ssid *ssid)
267 src = (int *) (((u8 *) ssid) + (long) data->param1);
269 value = os_malloc(20);
272 res = os_snprintf(value, 20, "%d", *src);
273 if (res < 0 || res >= 20) {
277 value[20 - 1] = '\0';
280 #endif /* NO_CONFIG_WRITE */
283 static int wpa_config_parse_bssid(const struct parse_data *data,
284 struct wpa_ssid *ssid, int line,
287 if (value[0] == '\0' || os_strcmp(value, "\"\"") == 0 ||
288 os_strcmp(value, "any") == 0) {
290 wpa_printf(MSG_MSGDUMP, "BSSID any");
293 if (hwaddr_aton(value, ssid->bssid)) {
294 wpa_printf(MSG_ERROR, "Line %d: Invalid BSSID '%s'.",
299 wpa_hexdump(MSG_MSGDUMP, "BSSID", ssid->bssid, ETH_ALEN);
304 #ifndef NO_CONFIG_WRITE
305 static char * wpa_config_write_bssid(const struct parse_data *data,
306 struct wpa_ssid *ssid)
311 if (!ssid->bssid_set)
314 value = os_malloc(20);
317 res = os_snprintf(value, 20, MACSTR, MAC2STR(ssid->bssid));
318 if (res < 0 || res >= 20) {
322 value[20 - 1] = '\0';
325 #endif /* NO_CONFIG_WRITE */
328 static int wpa_config_parse_psk(const struct parse_data *data,
329 struct wpa_ssid *ssid, int line,
333 #ifndef CONFIG_NO_PBKDF2
338 pos = os_strrchr(value, '"');
342 len = os_strlen(value);
343 if (len < 8 || len > 63) {
344 wpa_printf(MSG_ERROR, "Line %d: Invalid passphrase "
345 "length %lu (expected: 8..63) '%s'.",
346 line, (unsigned long) len, value);
349 wpa_hexdump_ascii_key(MSG_MSGDUMP, "PSK (ASCII passphrase)",
351 if (ssid->passphrase && os_strlen(ssid->passphrase) == len &&
352 os_memcmp(ssid->passphrase, value, len) == 0)
355 os_free(ssid->passphrase);
356 ssid->passphrase = os_malloc(len + 1);
357 if (ssid->passphrase == NULL)
359 os_memcpy(ssid->passphrase, value, len);
360 ssid->passphrase[len] = '\0';
362 #else /* CONFIG_NO_PBKDF2 */
363 wpa_printf(MSG_ERROR, "Line %d: ASCII passphrase not "
366 #endif /* CONFIG_NO_PBKDF2 */
369 if (hexstr2bin(value, ssid->psk, PMK_LEN) ||
370 value[PMK_LEN * 2] != '\0') {
371 wpa_printf(MSG_ERROR, "Line %d: Invalid PSK '%s'.",
376 os_free(ssid->passphrase);
377 ssid->passphrase = NULL;
380 wpa_hexdump_key(MSG_MSGDUMP, "PSK", ssid->psk, PMK_LEN);
385 #ifndef NO_CONFIG_WRITE
386 static char * wpa_config_write_psk(const struct parse_data *data,
387 struct wpa_ssid *ssid)
389 if (ssid->passphrase)
390 return wpa_config_write_string_ascii(
391 (const u8 *) ssid->passphrase,
392 os_strlen(ssid->passphrase));
395 return wpa_config_write_string_hex(ssid->psk, PMK_LEN);
399 #endif /* NO_CONFIG_WRITE */
402 static int wpa_config_parse_proto(const struct parse_data *data,
403 struct wpa_ssid *ssid, int line,
406 int val = 0, last, errors = 0;
407 char *start, *end, *buf;
409 buf = os_strdup(value);
414 while (*start != '\0') {
415 while (*start == ' ' || *start == '\t')
420 while (*end != ' ' && *end != '\t' && *end != '\0')
424 if (os_strcmp(start, "WPA") == 0)
425 val |= WPA_PROTO_WPA;
426 else if (os_strcmp(start, "RSN") == 0 ||
427 os_strcmp(start, "WPA2") == 0)
428 val |= WPA_PROTO_RSN;
430 wpa_printf(MSG_ERROR, "Line %d: invalid proto '%s'",
442 wpa_printf(MSG_ERROR,
443 "Line %d: no proto values configured.", line);
447 wpa_printf(MSG_MSGDUMP, "proto: 0x%x", val);
449 return errors ? -1 : 0;
453 #ifndef NO_CONFIG_WRITE
454 static char * wpa_config_write_proto(const struct parse_data *data,
455 struct wpa_ssid *ssid)
458 char *buf, *pos, *end;
460 pos = buf = os_zalloc(10);
465 if (ssid->proto & WPA_PROTO_WPA) {
466 ret = os_snprintf(pos, end - pos, "%sWPA", first ? "" : " ");
467 if (ret < 0 || ret >= end - pos)
473 if (ssid->proto & WPA_PROTO_RSN) {
474 ret = os_snprintf(pos, end - pos, "%sRSN", first ? "" : " ");
475 if (ret < 0 || ret >= end - pos)
483 #endif /* NO_CONFIG_WRITE */
486 static int wpa_config_parse_key_mgmt(const struct parse_data *data,
487 struct wpa_ssid *ssid, int line,
490 int val = 0, last, errors = 0;
491 char *start, *end, *buf;
493 buf = os_strdup(value);
498 while (*start != '\0') {
499 while (*start == ' ' || *start == '\t')
504 while (*end != ' ' && *end != '\t' && *end != '\0')
508 if (os_strcmp(start, "WPA-PSK") == 0)
509 val |= WPA_KEY_MGMT_PSK;
510 else if (os_strcmp(start, "WPA-EAP") == 0)
511 val |= WPA_KEY_MGMT_IEEE8021X;
512 else if (os_strcmp(start, "IEEE8021X") == 0)
513 val |= WPA_KEY_MGMT_IEEE8021X_NO_WPA;
514 else if (os_strcmp(start, "NONE") == 0)
515 val |= WPA_KEY_MGMT_NONE;
516 else if (os_strcmp(start, "WPA-NONE") == 0)
517 val |= WPA_KEY_MGMT_WPA_NONE;
518 #ifdef CONFIG_IEEE80211R
519 else if (os_strcmp(start, "FT-PSK") == 0)
520 val |= WPA_KEY_MGMT_FT_PSK;
521 else if (os_strcmp(start, "FT-EAP") == 0)
522 val |= WPA_KEY_MGMT_FT_IEEE8021X;
523 #endif /* CONFIG_IEEE80211R */
524 #ifdef CONFIG_IEEE80211W
525 else if (os_strcmp(start, "WPA-PSK-SHA256") == 0)
526 val |= WPA_KEY_MGMT_PSK_SHA256;
527 else if (os_strcmp(start, "WPA-EAP-SHA256") == 0)
528 val |= WPA_KEY_MGMT_IEEE8021X_SHA256;
529 #endif /* CONFIG_IEEE80211W */
531 else if (os_strcmp(start, "WPS") == 0)
532 val |= WPA_KEY_MGMT_WPS;
533 #endif /* CONFIG_WPS */
535 wpa_printf(MSG_ERROR, "Line %d: invalid key_mgmt '%s'",
547 wpa_printf(MSG_ERROR,
548 "Line %d: no key_mgmt values configured.", line);
552 wpa_printf(MSG_MSGDUMP, "key_mgmt: 0x%x", val);
553 ssid->key_mgmt = val;
554 return errors ? -1 : 0;
558 #ifndef NO_CONFIG_WRITE
559 static char * wpa_config_write_key_mgmt(const struct parse_data *data,
560 struct wpa_ssid *ssid)
562 char *buf, *pos, *end;
565 pos = buf = os_zalloc(50);
570 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
571 ret = os_snprintf(pos, end - pos, "%sWPA-PSK",
572 pos == buf ? "" : " ");
573 if (ret < 0 || ret >= end - pos) {
580 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
581 ret = os_snprintf(pos, end - pos, "%sWPA-EAP",
582 pos == buf ? "" : " ");
583 if (ret < 0 || ret >= end - pos) {
590 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
591 ret = os_snprintf(pos, end - pos, "%sIEEE8021X",
592 pos == buf ? "" : " ");
593 if (ret < 0 || ret >= end - pos) {
600 if (ssid->key_mgmt & WPA_KEY_MGMT_NONE) {
601 ret = os_snprintf(pos, end - pos, "%sNONE",
602 pos == buf ? "" : " ");
603 if (ret < 0 || ret >= end - pos) {
610 if (ssid->key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
611 ret = os_snprintf(pos, end - pos, "%sWPA-NONE",
612 pos == buf ? "" : " ");
613 if (ret < 0 || ret >= end - pos) {
620 #ifdef CONFIG_IEEE80211R
621 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_PSK)
622 pos += os_snprintf(pos, end - pos, "%sFT-PSK",
623 pos == buf ? "" : " ");
625 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X)
626 pos += os_snprintf(pos, end - pos, "%sFT-EAP",
627 pos == buf ? "" : " ");
628 #endif /* CONFIG_IEEE80211R */
630 #ifdef CONFIG_IEEE80211W
631 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK_SHA256)
632 pos += os_snprintf(pos, end - pos, "%sWPA-PSK-SHA256",
633 pos == buf ? "" : " ");
635 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256)
636 pos += os_snprintf(pos, end - pos, "%sWPA-EAP-SHA256",
637 pos == buf ? "" : " ");
638 #endif /* CONFIG_IEEE80211W */
641 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
642 pos += os_snprintf(pos, end - pos, "%sWPS",
643 pos == buf ? "" : " ");
644 #endif /* CONFIG_WPS */
648 #endif /* NO_CONFIG_WRITE */
651 static int wpa_config_parse_cipher(int line, const char *value)
654 char *start, *end, *buf;
656 buf = os_strdup(value);
661 while (*start != '\0') {
662 while (*start == ' ' || *start == '\t')
667 while (*end != ' ' && *end != '\t' && *end != '\0')
671 if (os_strcmp(start, "CCMP") == 0)
672 val |= WPA_CIPHER_CCMP;
673 else if (os_strcmp(start, "TKIP") == 0)
674 val |= WPA_CIPHER_TKIP;
675 else if (os_strcmp(start, "WEP104") == 0)
676 val |= WPA_CIPHER_WEP104;
677 else if (os_strcmp(start, "WEP40") == 0)
678 val |= WPA_CIPHER_WEP40;
679 else if (os_strcmp(start, "NONE") == 0)
680 val |= WPA_CIPHER_NONE;
682 wpa_printf(MSG_ERROR, "Line %d: invalid cipher '%s'.",
695 wpa_printf(MSG_ERROR, "Line %d: no cipher values configured.",
703 #ifndef NO_CONFIG_WRITE
704 static char * wpa_config_write_cipher(int cipher)
706 char *buf, *pos, *end;
709 pos = buf = os_zalloc(50);
714 if (cipher & WPA_CIPHER_CCMP) {
715 ret = os_snprintf(pos, end - pos, "%sCCMP",
716 pos == buf ? "" : " ");
717 if (ret < 0 || ret >= end - pos) {
724 if (cipher & WPA_CIPHER_TKIP) {
725 ret = os_snprintf(pos, end - pos, "%sTKIP",
726 pos == buf ? "" : " ");
727 if (ret < 0 || ret >= end - pos) {
734 if (cipher & WPA_CIPHER_WEP104) {
735 ret = os_snprintf(pos, end - pos, "%sWEP104",
736 pos == buf ? "" : " ");
737 if (ret < 0 || ret >= end - pos) {
744 if (cipher & WPA_CIPHER_WEP40) {
745 ret = os_snprintf(pos, end - pos, "%sWEP40",
746 pos == buf ? "" : " ");
747 if (ret < 0 || ret >= end - pos) {
754 if (cipher & WPA_CIPHER_NONE) {
755 ret = os_snprintf(pos, end - pos, "%sNONE",
756 pos == buf ? "" : " ");
757 if (ret < 0 || ret >= end - pos) {
766 #endif /* NO_CONFIG_WRITE */
769 static int wpa_config_parse_pairwise(const struct parse_data *data,
770 struct wpa_ssid *ssid, int line,
774 val = wpa_config_parse_cipher(line, value);
777 if (val & ~(WPA_CIPHER_CCMP | WPA_CIPHER_TKIP | WPA_CIPHER_NONE)) {
778 wpa_printf(MSG_ERROR, "Line %d: not allowed pairwise cipher "
779 "(0x%x).", line, val);
783 wpa_printf(MSG_MSGDUMP, "pairwise: 0x%x", val);
784 ssid->pairwise_cipher = val;
789 #ifndef NO_CONFIG_WRITE
790 static char * wpa_config_write_pairwise(const struct parse_data *data,
791 struct wpa_ssid *ssid)
793 return wpa_config_write_cipher(ssid->pairwise_cipher);
795 #endif /* NO_CONFIG_WRITE */
798 static int wpa_config_parse_group(const struct parse_data *data,
799 struct wpa_ssid *ssid, int line,
803 val = wpa_config_parse_cipher(line, value);
806 if (val & ~(WPA_CIPHER_CCMP | WPA_CIPHER_TKIP | WPA_CIPHER_WEP104 |
808 wpa_printf(MSG_ERROR, "Line %d: not allowed group cipher "
809 "(0x%x).", line, val);
813 wpa_printf(MSG_MSGDUMP, "group: 0x%x", val);
814 ssid->group_cipher = val;
819 #ifndef NO_CONFIG_WRITE
820 static char * wpa_config_write_group(const struct parse_data *data,
821 struct wpa_ssid *ssid)
823 return wpa_config_write_cipher(ssid->group_cipher);
825 #endif /* NO_CONFIG_WRITE */
828 static int wpa_config_parse_auth_alg(const struct parse_data *data,
829 struct wpa_ssid *ssid, int line,
832 int val = 0, last, errors = 0;
833 char *start, *end, *buf;
835 buf = os_strdup(value);
840 while (*start != '\0') {
841 while (*start == ' ' || *start == '\t')
846 while (*end != ' ' && *end != '\t' && *end != '\0')
850 if (os_strcmp(start, "OPEN") == 0)
851 val |= WPA_AUTH_ALG_OPEN;
852 else if (os_strcmp(start, "SHARED") == 0)
853 val |= WPA_AUTH_ALG_SHARED;
854 else if (os_strcmp(start, "LEAP") == 0)
855 val |= WPA_AUTH_ALG_LEAP;
857 wpa_printf(MSG_ERROR, "Line %d: invalid auth_alg '%s'",
869 wpa_printf(MSG_ERROR,
870 "Line %d: no auth_alg values configured.", line);
874 wpa_printf(MSG_MSGDUMP, "auth_alg: 0x%x", val);
875 ssid->auth_alg = val;
876 return errors ? -1 : 0;
880 #ifndef NO_CONFIG_WRITE
881 static char * wpa_config_write_auth_alg(const struct parse_data *data,
882 struct wpa_ssid *ssid)
884 char *buf, *pos, *end;
887 pos = buf = os_zalloc(30);
892 if (ssid->auth_alg & WPA_AUTH_ALG_OPEN) {
893 ret = os_snprintf(pos, end - pos, "%sOPEN",
894 pos == buf ? "" : " ");
895 if (ret < 0 || ret >= end - pos) {
902 if (ssid->auth_alg & WPA_AUTH_ALG_SHARED) {
903 ret = os_snprintf(pos, end - pos, "%sSHARED",
904 pos == buf ? "" : " ");
905 if (ret < 0 || ret >= end - pos) {
912 if (ssid->auth_alg & WPA_AUTH_ALG_LEAP) {
913 ret = os_snprintf(pos, end - pos, "%sLEAP",
914 pos == buf ? "" : " ");
915 if (ret < 0 || ret >= end - pos) {
924 #endif /* NO_CONFIG_WRITE */
927 static int * wpa_config_parse_freqs(const struct parse_data *data,
928 struct wpa_ssid *ssid, int line,
937 freqs = os_zalloc((len + 1) * sizeof(int));
948 n = os_realloc(freqs, (len * 2 + 1) * sizeof(int));
953 for (i = len; i <= len * 2; i++)
959 freqs[used] = atoi(pos);
960 if (freqs[used] == 0)
963 pos = os_strchr(pos + 1, ' ');
970 static int wpa_config_parse_scan_freq(const struct parse_data *data,
971 struct wpa_ssid *ssid, int line,
976 freqs = wpa_config_parse_freqs(data, ssid, line, value);
979 os_free(ssid->scan_freq);
980 ssid->scan_freq = freqs;
986 static int wpa_config_parse_freq_list(const struct parse_data *data,
987 struct wpa_ssid *ssid, int line,
992 freqs = wpa_config_parse_freqs(data, ssid, line, value);
995 os_free(ssid->freq_list);
996 ssid->freq_list = freqs;
1002 #ifndef NO_CONFIG_WRITE
1003 static char * wpa_config_write_freqs(const struct parse_data *data,
1006 char *buf, *pos, *end;
1014 for (i = 0; freqs[i]; i++)
1017 pos = buf = os_zalloc(10 * count + 1);
1020 end = buf + 10 * count + 1;
1022 for (i = 0; freqs[i]; i++) {
1023 ret = os_snprintf(pos, end - pos, "%s%u",
1024 i == 0 ? "" : " ", freqs[i]);
1025 if (ret < 0 || ret >= end - pos) {
1036 static char * wpa_config_write_scan_freq(const struct parse_data *data,
1037 struct wpa_ssid *ssid)
1039 return wpa_config_write_freqs(data, ssid->scan_freq);
1043 static char * wpa_config_write_freq_list(const struct parse_data *data,
1044 struct wpa_ssid *ssid)
1046 return wpa_config_write_freqs(data, ssid->freq_list);
1048 #endif /* NO_CONFIG_WRITE */
1051 #ifdef IEEE8021X_EAPOL
1052 static int wpa_config_parse_eap(const struct parse_data *data,
1053 struct wpa_ssid *ssid, int line,
1056 int last, errors = 0;
1057 char *start, *end, *buf;
1058 struct eap_method_type *methods = NULL, *tmp;
1059 size_t num_methods = 0;
1061 buf = os_strdup(value);
1066 while (*start != '\0') {
1067 while (*start == ' ' || *start == '\t')
1072 while (*end != ' ' && *end != '\t' && *end != '\0')
1074 last = *end == '\0';
1077 methods = os_realloc(methods,
1078 (num_methods + 1) * sizeof(*methods));
1079 if (methods == NULL) {
1084 methods[num_methods].method = eap_peer_get_type(
1085 start, &methods[num_methods].vendor);
1086 if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
1087 methods[num_methods].method == EAP_TYPE_NONE) {
1088 wpa_printf(MSG_ERROR, "Line %d: unknown EAP method "
1089 "'%s'", line, start);
1090 wpa_printf(MSG_ERROR, "You may need to add support for"
1091 " this EAP method during wpa_supplicant\n"
1092 "build time configuration.\n"
1093 "See README for more information.");
1095 } else if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
1096 methods[num_methods].method == EAP_TYPE_LEAP)
1108 methods = os_realloc(methods, (num_methods + 1) * sizeof(*methods));
1109 if (methods == NULL) {
1113 methods[num_methods].vendor = EAP_VENDOR_IETF;
1114 methods[num_methods].method = EAP_TYPE_NONE;
1117 wpa_hexdump(MSG_MSGDUMP, "eap methods",
1118 (u8 *) methods, num_methods * sizeof(*methods));
1119 ssid->eap.eap_methods = methods;
1120 return errors ? -1 : 0;
1124 static char * wpa_config_write_eap(const struct parse_data *data,
1125 struct wpa_ssid *ssid)
1128 char *buf, *pos, *end;
1129 const struct eap_method_type *eap_methods = ssid->eap.eap_methods;
1132 if (eap_methods == NULL)
1135 pos = buf = os_zalloc(100);
1140 for (i = 0; eap_methods[i].vendor != EAP_VENDOR_IETF ||
1141 eap_methods[i].method != EAP_TYPE_NONE; i++) {
1142 name = eap_get_name(eap_methods[i].vendor,
1143 eap_methods[i].method);
1145 ret = os_snprintf(pos, end - pos, "%s%s",
1146 pos == buf ? "" : " ", name);
1147 if (ret < 0 || ret >= end - pos)
1159 static int wpa_config_parse_password(const struct parse_data *data,
1160 struct wpa_ssid *ssid, int line,
1165 if (os_strcmp(value, "NULL") == 0) {
1166 wpa_printf(MSG_DEBUG, "Unset configuration string 'password'");
1167 os_free(ssid->eap.password);
1168 ssid->eap.password = NULL;
1169 ssid->eap.password_len = 0;
1173 if (os_strncmp(value, "hash:", 5) != 0) {
1177 tmp = wpa_config_parse_string(value, &res_len);
1179 wpa_printf(MSG_ERROR, "Line %d: failed to parse "
1183 wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
1184 (u8 *) tmp, res_len);
1186 os_free(ssid->eap.password);
1187 ssid->eap.password = (u8 *) tmp;
1188 ssid->eap.password_len = res_len;
1189 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
1195 /* NtPasswordHash: hash:<32 hex digits> */
1196 if (os_strlen(value + 5) != 2 * 16) {
1197 wpa_printf(MSG_ERROR, "Line %d: Invalid password hash length "
1198 "(expected 32 hex digits)", line);
1202 hash = os_malloc(16);
1206 if (hexstr2bin(value + 5, hash, 16)) {
1208 wpa_printf(MSG_ERROR, "Line %d: Invalid password hash", line);
1212 wpa_hexdump_key(MSG_MSGDUMP, data->name, hash, 16);
1214 os_free(ssid->eap.password);
1215 ssid->eap.password = hash;
1216 ssid->eap.password_len = 16;
1217 ssid->eap.flags |= EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
1223 static char * wpa_config_write_password(const struct parse_data *data,
1224 struct wpa_ssid *ssid)
1228 if (ssid->eap.password == NULL)
1231 if (!(ssid->eap.flags & EAP_CONFIG_FLAGS_PASSWORD_NTHASH)) {
1232 return wpa_config_write_string(
1233 ssid->eap.password, ssid->eap.password_len);
1236 buf = os_malloc(5 + 32 + 1);
1240 os_memcpy(buf, "hash:", 5);
1241 wpa_snprintf_hex(buf + 5, 32 + 1, ssid->eap.password, 16);
1245 #endif /* IEEE8021X_EAPOL */
1248 static int wpa_config_parse_wep_key(u8 *key, size_t *len, int line,
1249 const char *value, int idx)
1251 char *buf, title[20];
1254 buf = wpa_config_parse_string(value, len);
1256 wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key %d '%s'.",
1260 if (*len > MAX_WEP_KEY_LEN) {
1261 wpa_printf(MSG_ERROR, "Line %d: Too long WEP key %d '%s'.",
1266 os_memcpy(key, buf, *len);
1268 res = os_snprintf(title, sizeof(title), "wep_key%d", idx);
1269 if (res >= 0 && (size_t) res < sizeof(title))
1270 wpa_hexdump_key(MSG_MSGDUMP, title, key, *len);
1275 static int wpa_config_parse_wep_key0(const struct parse_data *data,
1276 struct wpa_ssid *ssid, int line,
1279 return wpa_config_parse_wep_key(ssid->wep_key[0],
1280 &ssid->wep_key_len[0], line,
1285 static int wpa_config_parse_wep_key1(const struct parse_data *data,
1286 struct wpa_ssid *ssid, int line,
1289 return wpa_config_parse_wep_key(ssid->wep_key[1],
1290 &ssid->wep_key_len[1], line,
1295 static int wpa_config_parse_wep_key2(const struct parse_data *data,
1296 struct wpa_ssid *ssid, int line,
1299 return wpa_config_parse_wep_key(ssid->wep_key[2],
1300 &ssid->wep_key_len[2], line,
1305 static int wpa_config_parse_wep_key3(const struct parse_data *data,
1306 struct wpa_ssid *ssid, int line,
1309 return wpa_config_parse_wep_key(ssid->wep_key[3],
1310 &ssid->wep_key_len[3], line,
1315 #ifndef NO_CONFIG_WRITE
1316 static char * wpa_config_write_wep_key(struct wpa_ssid *ssid, int idx)
1318 if (ssid->wep_key_len[idx] == 0)
1320 return wpa_config_write_string(ssid->wep_key[idx],
1321 ssid->wep_key_len[idx]);
1325 static char * wpa_config_write_wep_key0(const struct parse_data *data,
1326 struct wpa_ssid *ssid)
1328 return wpa_config_write_wep_key(ssid, 0);
1332 static char * wpa_config_write_wep_key1(const struct parse_data *data,
1333 struct wpa_ssid *ssid)
1335 return wpa_config_write_wep_key(ssid, 1);
1339 static char * wpa_config_write_wep_key2(const struct parse_data *data,
1340 struct wpa_ssid *ssid)
1342 return wpa_config_write_wep_key(ssid, 2);
1346 static char * wpa_config_write_wep_key3(const struct parse_data *data,
1347 struct wpa_ssid *ssid)
1349 return wpa_config_write_wep_key(ssid, 3);
1351 #endif /* NO_CONFIG_WRITE */
1356 static int wpa_config_parse_p2p_client_list(const struct parse_data *data,
1357 struct wpa_ssid *ssid, int line,
1361 u8 *buf, *n, addr[ETH_ALEN];
1368 while (pos && *pos) {
1372 if (hwaddr_aton(pos, addr)) {
1373 wpa_printf(MSG_ERROR, "Line %d: Invalid "
1374 "p2p_client_list address '%s'.",
1376 /* continue anyway */
1378 n = os_realloc(buf, (count + 1) * ETH_ALEN);
1384 os_memcpy(buf + count * ETH_ALEN, addr, ETH_ALEN);
1386 wpa_hexdump(MSG_MSGDUMP, "p2p_client_list",
1390 pos = os_strchr(pos, ' ');
1393 os_free(ssid->p2p_client_list);
1394 ssid->p2p_client_list = buf;
1395 ssid->num_p2p_clients = count;
1401 #ifndef NO_CONFIG_WRITE
1402 static char * wpa_config_write_p2p_client_list(const struct parse_data *data,
1403 struct wpa_ssid *ssid)
1405 char *value, *end, *pos;
1409 if (ssid->p2p_client_list == NULL || ssid->num_p2p_clients == 0)
1412 value = os_malloc(20 * ssid->num_p2p_clients);
1416 end = value + 20 * ssid->num_p2p_clients;
1418 for (i = 0; i < ssid->num_p2p_clients; i++) {
1419 res = os_snprintf(pos, end - pos, MACSTR " ",
1420 MAC2STR(ssid->p2p_client_list +
1422 if (res < 0 || res >= end - pos) {
1434 #endif /* NO_CONFIG_WRITE */
1436 #endif /* CONFIG_P2P */
1438 /* Helper macros for network block parser */
1443 /* OFFSET: Get offset of a variable within the wpa_ssid structure */
1444 #define OFFSET(v) ((void *) &((struct wpa_ssid *) 0)->v)
1446 /* STR: Define a string variable for an ASCII string; f = field name */
1447 #ifdef NO_CONFIG_WRITE
1448 #define _STR(f) #f, wpa_config_parse_str, OFFSET(f)
1449 #define _STRe(f) #f, wpa_config_parse_str, OFFSET(eap.f)
1450 #else /* NO_CONFIG_WRITE */
1451 #define _STR(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(f)
1452 #define _STRe(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(eap.f)
1453 #endif /* NO_CONFIG_WRITE */
1454 #define STR(f) _STR(f), NULL, NULL, NULL, 0
1455 #define STRe(f) _STRe(f), NULL, NULL, NULL, 0
1456 #define STR_KEY(f) _STR(f), NULL, NULL, NULL, 1
1457 #define STR_KEYe(f) _STRe(f), NULL, NULL, NULL, 1
1459 /* STR_LEN: Define a string variable with a separate variable for storing the
1460 * data length. Unlike STR(), this can be used to store arbitrary binary data
1461 * (i.e., even nul termination character). */
1462 #define _STR_LEN(f) _STR(f), OFFSET(f ## _len)
1463 #define _STR_LENe(f) _STRe(f), OFFSET(eap.f ## _len)
1464 #define STR_LEN(f) _STR_LEN(f), NULL, NULL, 0
1465 #define STR_LENe(f) _STR_LENe(f), NULL, NULL, 0
1466 #define STR_LEN_KEY(f) _STR_LEN(f), NULL, NULL, 1
1468 /* STR_RANGE: Like STR_LEN(), but with minimum and maximum allowed length
1469 * explicitly specified. */
1470 #define _STR_RANGE(f, min, max) _STR_LEN(f), (void *) (min), (void *) (max)
1471 #define STR_RANGE(f, min, max) _STR_RANGE(f, min, max), 0
1472 #define STR_RANGE_KEY(f, min, max) _STR_RANGE(f, min, max), 1
1474 #ifdef NO_CONFIG_WRITE
1475 #define _INT(f) #f, wpa_config_parse_int, OFFSET(f), (void *) 0
1476 #define _INTe(f) #f, wpa_config_parse_int, OFFSET(eap.f), (void *) 0
1477 #else /* NO_CONFIG_WRITE */
1478 #define _INT(f) #f, wpa_config_parse_int, wpa_config_write_int, \
1479 OFFSET(f), (void *) 0
1480 #define _INTe(f) #f, wpa_config_parse_int, wpa_config_write_int, \
1481 OFFSET(eap.f), (void *) 0
1482 #endif /* NO_CONFIG_WRITE */
1484 /* INT: Define an integer variable */
1485 #define INT(f) _INT(f), NULL, NULL, 0
1486 #define INTe(f) _INTe(f), NULL, NULL, 0
1488 /* INT_RANGE: Define an integer variable with allowed value range */
1489 #define INT_RANGE(f, min, max) _INT(f), (void *) (min), (void *) (max), 0
1491 /* FUNC: Define a configuration variable that uses a custom function for
1492 * parsing and writing the value. */
1493 #ifdef NO_CONFIG_WRITE
1494 #define _FUNC(f) #f, wpa_config_parse_ ## f, NULL, NULL, NULL, NULL
1495 #else /* NO_CONFIG_WRITE */
1496 #define _FUNC(f) #f, wpa_config_parse_ ## f, wpa_config_write_ ## f, \
1497 NULL, NULL, NULL, NULL
1498 #endif /* NO_CONFIG_WRITE */
1499 #define FUNC(f) _FUNC(f), 0
1500 #define FUNC_KEY(f) _FUNC(f), 1
1503 * Table of network configuration variables. This table is used to parse each
1504 * network configuration variable, e.g., each line in wpa_supplicant.conf file
1505 * that is inside a network block.
1507 * This table is generated using the helper macros defined above and with
1508 * generous help from the C pre-processor. The field name is stored as a string
1509 * into .name and for STR and INT types, the offset of the target buffer within
1510 * struct wpa_ssid is stored in .param1. .param2 (if not NULL) is similar
1511 * offset to the field containing the length of the configuration variable.
1512 * .param3 and .param4 can be used to mark the allowed range (length for STR
1513 * and value for INT).
1515 * For each configuration line in wpa_supplicant.conf, the parser goes through
1516 * this table and select the entry that matches with the field name. The parser
1517 * function (.parser) is then called to parse the actual value of the field.
1519 * This kind of mechanism makes it easy to add new configuration parameters,
1520 * since only one line needs to be added into this table and into the
1521 * struct wpa_ssid definition if the new variable is either a string or
1522 * integer. More complex types will need to use their own parser and writer
1525 static const struct parse_data ssid_fields[] = {
1526 { STR_RANGE(ssid, 0, MAX_SSID_LEN) },
1527 { INT_RANGE(scan_ssid, 0, 1) },
1535 { FUNC(scan_freq) },
1536 { FUNC(freq_list) },
1537 #ifdef IEEE8021X_EAPOL
1539 { STR_LENe(identity) },
1540 { STR_LENe(anonymous_identity) },
1541 { FUNC_KEY(password) },
1544 { STRe(client_cert) },
1545 { STRe(private_key) },
1546 { STR_KEYe(private_key_passwd) },
1548 { STRe(subject_match) },
1549 { STRe(altsubject_match) },
1552 { STRe(client_cert2) },
1553 { STRe(private_key2) },
1554 { STR_KEYe(private_key2_passwd) },
1556 { STRe(subject_match2) },
1557 { STRe(altsubject_match2) },
1562 { STRe(engine_id) },
1565 { STRe(ca_cert_id) },
1567 { STRe(engine2_id) },
1570 { STRe(ca_cert2_id) },
1573 { INT(eapol_flags) },
1574 #endif /* IEEE8021X_EAPOL */
1575 { FUNC_KEY(wep_key0) },
1576 { FUNC_KEY(wep_key1) },
1577 { FUNC_KEY(wep_key2) },
1578 { FUNC_KEY(wep_key3) },
1579 { INT(wep_tx_keyidx) },
1581 #ifdef IEEE8021X_EAPOL
1582 { INT(eap_workaround) },
1584 { INTe(fragment_size) },
1585 #endif /* IEEE8021X_EAPOL */
1586 { INT_RANGE(mode, 0, 4) },
1587 { INT_RANGE(proactive_key_caching, 0, 1) },
1588 { INT_RANGE(disabled, 0, 2) },
1590 #ifdef CONFIG_IEEE80211W
1591 { INT_RANGE(ieee80211w, 0, 2) },
1592 #endif /* CONFIG_IEEE80211W */
1593 { INT_RANGE(peerkey, 0, 1) },
1594 { INT_RANGE(mixed_cell, 0, 1) },
1595 { INT_RANGE(frequency, 0, 10000) },
1596 { INT(wpa_ptk_rekey) },
1599 { FUNC(p2p_client_list) },
1600 #endif /* CONFIG_P2P */
1612 #undef STR_RANGE_KEY
1619 #define NUM_SSID_FIELDS (sizeof(ssid_fields) / sizeof(ssid_fields[0]))
1623 * wpa_config_add_prio_network - Add a network to priority lists
1624 * @config: Configuration data from wpa_config_read()
1625 * @ssid: Pointer to the network configuration to be added to the list
1626 * Returns: 0 on success, -1 on failure
1628 * This function is used to add a network block to the priority list of
1629 * networks. This must be called for each network when reading in the full
1630 * configuration. In addition, this can be used indirectly when updating
1631 * priorities by calling wpa_config_update_prio_list().
1633 int wpa_config_add_prio_network(struct wpa_config *config,
1634 struct wpa_ssid *ssid)
1637 struct wpa_ssid *prev, **nlist;
1640 * Add to an existing priority list if one is available for the
1641 * configured priority level for this network.
1643 for (prio = 0; prio < config->num_prio; prio++) {
1644 prev = config->pssid[prio];
1645 if (prev->priority == ssid->priority) {
1653 /* First network for this priority - add a new priority list */
1654 nlist = os_realloc(config->pssid,
1655 (config->num_prio + 1) * sizeof(struct wpa_ssid *));
1659 for (prio = 0; prio < config->num_prio; prio++) {
1660 if (nlist[prio]->priority < ssid->priority)
1664 os_memmove(&nlist[prio + 1], &nlist[prio],
1665 (config->num_prio - prio) * sizeof(struct wpa_ssid *));
1669 config->pssid = nlist;
1676 * wpa_config_update_prio_list - Update network priority list
1677 * @config: Configuration data from wpa_config_read()
1678 * Returns: 0 on success, -1 on failure
1680 * This function is called to update the priority list of networks in the
1681 * configuration when a network is being added or removed. This is also called
1682 * if a priority for a network is changed.
1684 int wpa_config_update_prio_list(struct wpa_config *config)
1686 struct wpa_ssid *ssid;
1689 os_free(config->pssid);
1690 config->pssid = NULL;
1691 config->num_prio = 0;
1693 ssid = config->ssid;
1696 if (wpa_config_add_prio_network(config, ssid) < 0)
1705 #ifdef IEEE8021X_EAPOL
1706 static void eap_peer_config_free(struct eap_peer_config *eap)
1708 os_free(eap->eap_methods);
1709 os_free(eap->identity);
1710 os_free(eap->anonymous_identity);
1711 os_free(eap->password);
1712 os_free(eap->ca_cert);
1713 os_free(eap->ca_path);
1714 os_free(eap->client_cert);
1715 os_free(eap->private_key);
1716 os_free(eap->private_key_passwd);
1717 os_free(eap->dh_file);
1718 os_free(eap->subject_match);
1719 os_free(eap->altsubject_match);
1720 os_free(eap->ca_cert2);
1721 os_free(eap->ca_path2);
1722 os_free(eap->client_cert2);
1723 os_free(eap->private_key2);
1724 os_free(eap->private_key2_passwd);
1725 os_free(eap->dh_file2);
1726 os_free(eap->subject_match2);
1727 os_free(eap->altsubject_match2);
1728 os_free(eap->phase1);
1729 os_free(eap->phase2);
1732 os_free(eap->engine_id);
1733 os_free(eap->key_id);
1734 os_free(eap->cert_id);
1735 os_free(eap->ca_cert_id);
1736 os_free(eap->key2_id);
1737 os_free(eap->cert2_id);
1738 os_free(eap->ca_cert2_id);
1740 os_free(eap->engine2_id);
1742 os_free(eap->pending_req_otp);
1743 os_free(eap->pac_file);
1744 os_free(eap->new_password);
1746 #endif /* IEEE8021X_EAPOL */
1750 * wpa_config_free_ssid - Free network/ssid configuration data
1751 * @ssid: Configuration data for the network
1753 * This function frees all resources allocated for the network configuration
1756 void wpa_config_free_ssid(struct wpa_ssid *ssid)
1758 os_free(ssid->ssid);
1759 os_free(ssid->passphrase);
1760 #ifdef IEEE8021X_EAPOL
1761 eap_peer_config_free(&ssid->eap);
1762 #endif /* IEEE8021X_EAPOL */
1763 os_free(ssid->id_str);
1764 os_free(ssid->scan_freq);
1765 os_free(ssid->freq_list);
1766 os_free(ssid->bgscan);
1767 os_free(ssid->p2p_client_list);
1773 * wpa_config_free - Free configuration data
1774 * @config: Configuration data from wpa_config_read()
1776 * This function frees all resources allocated for the configuration data by
1777 * wpa_config_read().
1779 void wpa_config_free(struct wpa_config *config)
1781 #ifndef CONFIG_NO_CONFIG_BLOBS
1782 struct wpa_config_blob *blob, *prevblob;
1783 #endif /* CONFIG_NO_CONFIG_BLOBS */
1784 struct wpa_ssid *ssid, *prev = NULL;
1786 ssid = config->ssid;
1790 wpa_config_free_ssid(prev);
1793 #ifndef CONFIG_NO_CONFIG_BLOBS
1794 blob = config->blobs;
1799 wpa_config_free_blob(prevblob);
1801 #endif /* CONFIG_NO_CONFIG_BLOBS */
1803 os_free(config->ctrl_interface);
1804 os_free(config->ctrl_interface_group);
1805 os_free(config->opensc_engine_path);
1806 os_free(config->pkcs11_engine_path);
1807 os_free(config->pkcs11_module_path);
1808 os_free(config->driver_param);
1809 os_free(config->device_name);
1810 os_free(config->manufacturer);
1811 os_free(config->model_name);
1812 os_free(config->model_number);
1813 os_free(config->serial_number);
1814 os_free(config->config_methods);
1815 os_free(config->p2p_ssid_postfix);
1816 os_free(config->pssid);
1817 os_free(config->home_realm);
1818 os_free(config->home_username);
1819 os_free(config->home_password);
1820 os_free(config->home_ca_cert);
1821 os_free(config->home_imsi);
1822 os_free(config->home_milenage);
1828 * wpa_config_foreach_network - Iterate over each configured network
1829 * @config: Configuration data from wpa_config_read()
1830 * @func: Callback function to process each network
1831 * @arg: Opaque argument to pass to callback function
1833 * Iterate over the set of configured networks calling the specified
1834 * function for each item. We guard against callbacks removing the
1837 void wpa_config_foreach_network(struct wpa_config *config,
1838 void (*func)(void *, struct wpa_ssid *),
1841 struct wpa_ssid *ssid, *next;
1843 ssid = config->ssid;
1853 * wpa_config_get_network - Get configured network based on id
1854 * @config: Configuration data from wpa_config_read()
1855 * @id: Unique network id to search for
1856 * Returns: Network configuration or %NULL if not found
1858 struct wpa_ssid * wpa_config_get_network(struct wpa_config *config, int id)
1860 struct wpa_ssid *ssid;
1862 ssid = config->ssid;
1874 * wpa_config_add_network - Add a new network with empty configuration
1875 * @config: Configuration data from wpa_config_read()
1876 * Returns: The new network configuration or %NULL if operation failed
1878 struct wpa_ssid * wpa_config_add_network(struct wpa_config *config)
1881 struct wpa_ssid *ssid, *last = NULL;
1884 ssid = config->ssid;
1893 ssid = os_zalloc(sizeof(*ssid));
1900 config->ssid = ssid;
1902 wpa_config_update_prio_list(config);
1909 * wpa_config_remove_network - Remove a configured network based on id
1910 * @config: Configuration data from wpa_config_read()
1911 * @id: Unique network id to search for
1912 * Returns: 0 on success, or -1 if the network was not found
1914 int wpa_config_remove_network(struct wpa_config *config, int id)
1916 struct wpa_ssid *ssid, *prev = NULL;
1918 ssid = config->ssid;
1930 prev->next = ssid->next;
1932 config->ssid = ssid->next;
1934 wpa_config_update_prio_list(config);
1935 wpa_config_free_ssid(ssid);
1941 * wpa_config_set_network_defaults - Set network default values
1942 * @ssid: Pointer to network configuration data
1944 void wpa_config_set_network_defaults(struct wpa_ssid *ssid)
1946 ssid->proto = DEFAULT_PROTO;
1947 ssid->pairwise_cipher = DEFAULT_PAIRWISE;
1948 ssid->group_cipher = DEFAULT_GROUP;
1949 ssid->key_mgmt = DEFAULT_KEY_MGMT;
1950 #ifdef IEEE8021X_EAPOL
1951 ssid->eapol_flags = DEFAULT_EAPOL_FLAGS;
1952 ssid->eap_workaround = DEFAULT_EAP_WORKAROUND;
1953 ssid->eap.fragment_size = DEFAULT_FRAGMENT_SIZE;
1954 #endif /* IEEE8021X_EAPOL */
1959 * wpa_config_set - Set a variable in network configuration
1960 * @ssid: Pointer to network configuration data
1961 * @var: Variable name, e.g., "ssid"
1962 * @value: Variable value
1963 * @line: Line number in configuration file or 0 if not used
1964 * Returns: 0 on success, -1 on failure
1966 * This function can be used to set network configuration variables based on
1967 * both the configuration file and management interface input. The value
1968 * parameter must be in the same format as the text-based configuration file is
1969 * using. For example, strings are using double quotation marks.
1971 int wpa_config_set(struct wpa_ssid *ssid, const char *var, const char *value,
1977 if (ssid == NULL || var == NULL || value == NULL)
1980 for (i = 0; i < NUM_SSID_FIELDS; i++) {
1981 const struct parse_data *field = &ssid_fields[i];
1982 if (os_strcmp(var, field->name) != 0)
1985 if (field->parser(field, ssid, line, value)) {
1987 wpa_printf(MSG_ERROR, "Line %d: failed to "
1988 "parse %s '%s'.", line, var, value);
1994 if (i == NUM_SSID_FIELDS) {
1996 wpa_printf(MSG_ERROR, "Line %d: unknown network field "
1997 "'%s'.", line, var);
2006 int wpa_config_set_quoted(struct wpa_ssid *ssid, const char *var,
2013 len = os_strlen(value);
2014 buf = os_malloc(len + 3);
2018 os_memcpy(buf + 1, value, len);
2020 buf[len + 2] = '\0';
2021 ret = wpa_config_set(ssid, var, buf, 0);
2028 * wpa_config_get_all - Get all options from network configuration
2029 * @ssid: Pointer to network configuration data
2030 * @get_keys: Determines if keys/passwords will be included in returned list
2031 * (if they may be exported)
2032 * Returns: %NULL terminated list of all set keys and their values in the form
2033 * of [key1, val1, key2, val2, ... , NULL]
2035 * This function can be used to get list of all configured network properties.
2036 * The caller is responsible for freeing the returned list and all its
2039 char ** wpa_config_get_all(struct wpa_ssid *ssid, int get_keys)
2041 const struct parse_data *field;
2047 get_keys = get_keys && ssid->export_keys;
2049 props = os_zalloc(sizeof(char *) * ((2 * NUM_SSID_FIELDS) + 1));
2054 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2055 field = &ssid_fields[i];
2056 if (field->key_data && !get_keys)
2058 value = field->writer(field, ssid);
2061 if (os_strlen(value) == 0) {
2066 key = os_strdup(field->name);
2072 props[fields_num * 2] = key;
2073 props[fields_num * 2 + 1] = value;
2089 #ifndef NO_CONFIG_WRITE
2091 * wpa_config_get - Get a variable in network configuration
2092 * @ssid: Pointer to network configuration data
2093 * @var: Variable name, e.g., "ssid"
2094 * Returns: Value of the variable or %NULL on failure
2096 * This function can be used to get network configuration variables. The
2097 * returned value is a copy of the configuration variable in text format, i.e,.
2098 * the same format that the text-based configuration file and wpa_config_set()
2099 * are using for the value. The caller is responsible for freeing the returned
2102 char * wpa_config_get(struct wpa_ssid *ssid, const char *var)
2106 if (ssid == NULL || var == NULL)
2109 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2110 const struct parse_data *field = &ssid_fields[i];
2111 if (os_strcmp(var, field->name) == 0)
2112 return field->writer(field, ssid);
2120 * wpa_config_get_no_key - Get a variable in network configuration (no keys)
2121 * @ssid: Pointer to network configuration data
2122 * @var: Variable name, e.g., "ssid"
2123 * Returns: Value of the variable or %NULL on failure
2125 * This function can be used to get network configuration variable like
2126 * wpa_config_get(). The only difference is that this functions does not expose
2127 * key/password material from the configuration. In case a key/password field
2128 * is requested, the returned value is an empty string or %NULL if the variable
2129 * is not set or "*" if the variable is set (regardless of its value). The
2130 * returned value is a copy of the configuration variable in text format, i.e,.
2131 * the same format that the text-based configuration file and wpa_config_set()
2132 * are using for the value. The caller is responsible for freeing the returned
2135 char * wpa_config_get_no_key(struct wpa_ssid *ssid, const char *var)
2139 if (ssid == NULL || var == NULL)
2142 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2143 const struct parse_data *field = &ssid_fields[i];
2144 if (os_strcmp(var, field->name) == 0) {
2145 char *res = field->writer(field, ssid);
2146 if (field->key_data) {
2147 if (res && res[0]) {
2148 wpa_printf(MSG_DEBUG, "Do not allow "
2149 "key_data field to be "
2152 return os_strdup("*");
2164 #endif /* NO_CONFIG_WRITE */
2168 * wpa_config_update_psk - Update WPA PSK based on passphrase and SSID
2169 * @ssid: Pointer to network configuration data
2171 * This function must be called to update WPA PSK when either SSID or the
2172 * passphrase has changed for the network configuration.
2174 void wpa_config_update_psk(struct wpa_ssid *ssid)
2176 #ifndef CONFIG_NO_PBKDF2
2177 pbkdf2_sha1(ssid->passphrase,
2178 (char *) ssid->ssid, ssid->ssid_len, 4096,
2179 ssid->psk, PMK_LEN);
2180 wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
2181 ssid->psk, PMK_LEN);
2183 #endif /* CONFIG_NO_PBKDF2 */
2187 #ifndef CONFIG_NO_CONFIG_BLOBS
2189 * wpa_config_get_blob - Get a named configuration blob
2190 * @config: Configuration data from wpa_config_read()
2191 * @name: Name of the blob
2192 * Returns: Pointer to blob data or %NULL if not found
2194 const struct wpa_config_blob * wpa_config_get_blob(struct wpa_config *config,
2197 struct wpa_config_blob *blob = config->blobs;
2200 if (os_strcmp(blob->name, name) == 0)
2209 * wpa_config_set_blob - Set or add a named configuration blob
2210 * @config: Configuration data from wpa_config_read()
2211 * @blob: New value for the blob
2213 * Adds a new configuration blob or replaces the current value of an existing
2216 void wpa_config_set_blob(struct wpa_config *config,
2217 struct wpa_config_blob *blob)
2219 wpa_config_remove_blob(config, blob->name);
2220 blob->next = config->blobs;
2221 config->blobs = blob;
2226 * wpa_config_free_blob - Free blob data
2227 * @blob: Pointer to blob to be freed
2229 void wpa_config_free_blob(struct wpa_config_blob *blob)
2232 os_free(blob->name);
2233 os_free(blob->data);
2240 * wpa_config_remove_blob - Remove a named configuration blob
2241 * @config: Configuration data from wpa_config_read()
2242 * @name: Name of the blob to remove
2243 * Returns: 0 if blob was removed or -1 if blob was not found
2245 int wpa_config_remove_blob(struct wpa_config *config, const char *name)
2247 struct wpa_config_blob *pos = config->blobs, *prev = NULL;
2250 if (os_strcmp(pos->name, name) == 0) {
2252 prev->next = pos->next;
2254 config->blobs = pos->next;
2255 wpa_config_free_blob(pos);
2264 #endif /* CONFIG_NO_CONFIG_BLOBS */
2268 * wpa_config_alloc_empty - Allocate an empty configuration
2269 * @ctrl_interface: Control interface parameters, e.g., path to UNIX domain
2271 * @driver_param: Driver parameters
2272 * Returns: Pointer to allocated configuration data or %NULL on failure
2274 struct wpa_config * wpa_config_alloc_empty(const char *ctrl_interface,
2275 const char *driver_param)
2277 struct wpa_config *config;
2279 config = os_zalloc(sizeof(*config));
2282 config->eapol_version = DEFAULT_EAPOL_VERSION;
2283 config->ap_scan = DEFAULT_AP_SCAN;
2284 config->fast_reauth = DEFAULT_FAST_REAUTH;
2285 config->p2p_go_intent = DEFAULT_P2P_GO_INTENT;
2286 config->p2p_intra_bss = DEFAULT_P2P_INTRA_BSS;
2287 config->bss_max_count = DEFAULT_BSS_MAX_COUNT;
2288 config->bss_expiration_age = DEFAULT_BSS_EXPIRATION_AGE;
2289 config->bss_expiration_scan_count = DEFAULT_BSS_EXPIRATION_SCAN_COUNT;
2290 config->max_num_sta = DEFAULT_MAX_NUM_STA;
2291 config->access_network_type = DEFAULT_ACCESS_NETWORK_TYPE;
2294 config->ctrl_interface = os_strdup(ctrl_interface);
2296 config->driver_param = os_strdup(driver_param);
2302 #ifndef CONFIG_NO_STDOUT_DEBUG
2304 * wpa_config_debug_dump_networks - Debug dump of configured networks
2305 * @config: Configuration data from wpa_config_read()
2307 void wpa_config_debug_dump_networks(struct wpa_config *config)
2310 struct wpa_ssid *ssid;
2312 for (prio = 0; prio < config->num_prio; prio++) {
2313 ssid = config->pssid[prio];
2314 wpa_printf(MSG_DEBUG, "Priority group %d",
2317 wpa_printf(MSG_DEBUG, " id=%d ssid='%s'",
2319 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
2324 #endif /* CONFIG_NO_STDOUT_DEBUG */
2327 struct global_parse_data {
2329 int (*parser)(const struct global_parse_data *data,
2330 struct wpa_config *config, int line, const char *value);
2331 void *param1, *param2, *param3;
2332 unsigned int changed_flag;
2336 static int wpa_global_config_parse_int(const struct global_parse_data *data,
2337 struct wpa_config *config, int line,
2341 dst = (int *) (((u8 *) config) + (long) data->param1);
2343 wpa_printf(MSG_DEBUG, "%s=%d", data->name, *dst);
2345 if (data->param2 && *dst < (long) data->param2) {
2346 wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
2347 "min_value=%ld)", line, data->name, *dst,
2348 (long) data->param2);
2349 *dst = (long) data->param2;
2353 if (data->param3 && *dst > (long) data->param3) {
2354 wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
2355 "max_value=%ld)", line, data->name, *dst,
2356 (long) data->param3);
2357 *dst = (long) data->param3;
2365 static int wpa_global_config_parse_str(const struct global_parse_data *data,
2366 struct wpa_config *config, int line,
2372 len = os_strlen(pos);
2373 if (data->param2 && len < (size_t) data->param2) {
2374 wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
2375 "min_len=%ld)", line, data->name,
2376 (unsigned long) len, (long) data->param2);
2380 if (data->param3 && len > (size_t) data->param3) {
2381 wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
2382 "max_len=%ld)", line, data->name,
2383 (unsigned long) len, (long) data->param3);
2387 tmp = os_strdup(pos);
2391 dst = (char **) (((u8 *) config) + (long) data->param1);
2394 wpa_printf(MSG_DEBUG, "%s='%s'", data->name, *dst);
2400 static int wpa_config_process_country(const struct global_parse_data *data,
2401 struct wpa_config *config, int line,
2404 if (!pos[0] || !pos[1]) {
2405 wpa_printf(MSG_DEBUG, "Invalid country set");
2408 config->country[0] = pos[0];
2409 config->country[1] = pos[1];
2410 wpa_printf(MSG_DEBUG, "country='%c%c'",
2411 config->country[0], config->country[1]);
2416 static int wpa_config_process_load_dynamic_eap(
2417 const struct global_parse_data *data, struct wpa_config *config,
2418 int line, const char *so)
2421 wpa_printf(MSG_DEBUG, "load_dynamic_eap=%s", so);
2422 ret = eap_peer_method_load(so);
2424 wpa_printf(MSG_DEBUG, "This EAP type was already loaded - not "
2427 wpa_printf(MSG_ERROR, "Line %d: Failed to load dynamic EAP "
2428 "method '%s'.", line, so);
2438 static int wpa_config_process_uuid(const struct global_parse_data *data,
2439 struct wpa_config *config, int line,
2443 if (uuid_str2bin(pos, config->uuid)) {
2444 wpa_printf(MSG_ERROR, "Line %d: invalid UUID", line);
2447 uuid_bin2str(config->uuid, buf, sizeof(buf));
2448 wpa_printf(MSG_DEBUG, "uuid=%s", buf);
2453 static int wpa_config_process_device_type(
2454 const struct global_parse_data *data,
2455 struct wpa_config *config, int line, const char *pos)
2457 return wps_dev_type_str2bin(pos, config->device_type);
2461 static int wpa_config_process_os_version(const struct global_parse_data *data,
2462 struct wpa_config *config, int line,
2465 if (hexstr2bin(pos, config->os_version, 4)) {
2466 wpa_printf(MSG_ERROR, "Line %d: invalid os_version", line);
2469 wpa_printf(MSG_DEBUG, "os_version=%08x",
2470 WPA_GET_BE32(config->os_version));
2474 #endif /* CONFIG_WPS */
2477 static int wpa_config_process_sec_device_type(
2478 const struct global_parse_data *data,
2479 struct wpa_config *config, int line, const char *pos)
2483 if (config->num_sec_device_types >= MAX_SEC_DEVICE_TYPES) {
2484 wpa_printf(MSG_ERROR, "Line %d: too many sec_device_type "
2489 idx = config->num_sec_device_types;
2491 if (wps_dev_type_str2bin(pos, config->sec_device_type[idx]))
2494 config->num_sec_device_types++;
2497 #endif /* CONFIG_P2P */
2500 static int wpa_config_process_hessid(
2501 const struct global_parse_data *data,
2502 struct wpa_config *config, int line, const char *pos)
2504 if (hwaddr_aton2(pos, config->hessid) < 0) {
2505 wpa_printf(MSG_ERROR, "Line %d: Invalid hessid '%s'",
2517 /* OFFSET: Get offset of a variable within the wpa_config structure */
2518 #define OFFSET(v) ((void *) &((struct wpa_config *) 0)->v)
2520 #define FUNC(f) #f, wpa_config_process_ ## f, OFFSET(f), NULL, NULL
2521 #define FUNC_NO_VAR(f) #f, wpa_config_process_ ## f, NULL, NULL, NULL
2522 #define _INT(f) #f, wpa_global_config_parse_int, OFFSET(f)
2523 #define INT(f) _INT(f), NULL, NULL
2524 #define INT_RANGE(f, min, max) _INT(f), (void *) min, (void *) max
2525 #define _STR(f) #f, wpa_global_config_parse_str, OFFSET(f)
2526 #define STR(f) _STR(f), NULL, NULL
2527 #define STR_RANGE(f, min, max) _STR(f), (void *) min, (void *) max
2529 static const struct global_parse_data global_fields[] = {
2530 #ifdef CONFIG_CTRL_IFACE
2531 { STR(ctrl_interface), 0 },
2532 { STR(ctrl_interface_group), 0 } /* deprecated */,
2533 #endif /* CONFIG_CTRL_IFACE */
2534 { INT_RANGE(eapol_version, 1, 2), 0 },
2535 { INT(ap_scan), 0 },
2536 { INT(fast_reauth), 0 },
2537 { STR(opensc_engine_path), 0 },
2538 { STR(pkcs11_engine_path), 0 },
2539 { STR(pkcs11_module_path), 0 },
2540 { STR(driver_param), 0 },
2541 { INT(dot11RSNAConfigPMKLifetime), 0 },
2542 { INT(dot11RSNAConfigPMKReauthThreshold), 0 },
2543 { INT(dot11RSNAConfigSATimeout), 0 },
2544 #ifndef CONFIG_NO_CONFIG_WRITE
2545 { INT(update_config), 0 },
2546 #endif /* CONFIG_NO_CONFIG_WRITE */
2547 { FUNC_NO_VAR(load_dynamic_eap), 0 },
2549 { FUNC(uuid), CFG_CHANGED_UUID },
2550 { STR_RANGE(device_name, 0, 32), CFG_CHANGED_DEVICE_NAME },
2551 { STR_RANGE(manufacturer, 0, 64), CFG_CHANGED_WPS_STRING },
2552 { STR_RANGE(model_name, 0, 32), CFG_CHANGED_WPS_STRING },
2553 { STR_RANGE(model_number, 0, 32), CFG_CHANGED_WPS_STRING },
2554 { STR_RANGE(serial_number, 0, 32), CFG_CHANGED_WPS_STRING },
2555 { FUNC(device_type), CFG_CHANGED_DEVICE_TYPE },
2556 { FUNC(os_version), CFG_CHANGED_OS_VERSION },
2557 { STR(config_methods), CFG_CHANGED_CONFIG_METHODS },
2558 { INT_RANGE(wps_cred_processing, 0, 2), 0 },
2559 #endif /* CONFIG_WPS */
2561 { FUNC(sec_device_type), CFG_CHANGED_SEC_DEVICE_TYPE },
2562 { INT(p2p_listen_reg_class), 0 },
2563 { INT(p2p_listen_channel), 0 },
2564 { INT(p2p_oper_reg_class), 0 },
2565 { INT(p2p_oper_channel), 0 },
2566 { INT_RANGE(p2p_go_intent, 0, 15), 0 },
2567 { STR(p2p_ssid_postfix), CFG_CHANGED_P2P_SSID_POSTFIX },
2568 { INT_RANGE(persistent_reconnect, 0, 1), 0 },
2569 { INT_RANGE(p2p_intra_bss, 0, 1), CFG_CHANGED_P2P_INTRA_BSS },
2570 { INT(p2p_group_idle), 0 },
2571 #endif /* CONFIG_P2P */
2572 { FUNC(country), CFG_CHANGED_COUNTRY },
2573 { INT(bss_max_count), 0 },
2574 { INT(bss_expiration_age), 0 },
2575 { INT(bss_expiration_scan_count), 0 },
2576 { INT_RANGE(filter_ssids, 0, 1), 0 },
2577 { INT(max_num_sta), 0 },
2578 { INT_RANGE(disassoc_low_ack, 0, 1), 0 },
2579 { STR(home_realm), 0 },
2580 { STR(home_username), 0 },
2581 { STR(home_password), 0 },
2582 { STR(home_ca_cert), 0 },
2583 { STR(home_imsi), 0 },
2584 { STR(home_milenage), 0 },
2585 { INT_RANGE(interworking, 0, 1), 0 },
2586 { FUNC(hessid), 0 },
2587 { INT_RANGE(access_network_type, 0, 15), 0 }
2597 #define NUM_GLOBAL_FIELDS (sizeof(global_fields) / sizeof(global_fields[0]))
2600 int wpa_config_process_global(struct wpa_config *config, char *pos, int line)
2605 for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
2606 const struct global_parse_data *field = &global_fields[i];
2607 size_t flen = os_strlen(field->name);
2608 if (os_strncmp(pos, field->name, flen) != 0 ||
2612 if (field->parser(field, config, line, pos + flen + 1)) {
2613 wpa_printf(MSG_ERROR, "Line %d: failed to "
2614 "parse '%s'.", line, pos);
2617 config->changed_parameters |= field->changed_flag;
2620 if (i == NUM_GLOBAL_FIELDS) {
2623 wpa_printf(MSG_ERROR, "Line %d: unknown global field '%s'.",