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"
26 #if !defined(CONFIG_CTRL_IFACE) && defined(CONFIG_NO_CONFIG_WRITE)
27 #define NO_CONFIG_WRITE
31 * Structure for network configuration parsing. This data is used to implement
32 * a generic parser for each network block variable. The table of configuration
33 * variables is defined below in this file (ssid_fields[]).
36 /* Configuration variable name */
39 /* Parser function for this variable */
40 int (*parser)(const struct parse_data *data, struct wpa_ssid *ssid,
41 int line, const char *value);
43 #ifndef NO_CONFIG_WRITE
44 /* Writer function (i.e., to get the variable in text format from
45 * internal presentation). */
46 char * (*writer)(const struct parse_data *data, struct wpa_ssid *ssid);
47 #endif /* NO_CONFIG_WRITE */
49 /* Variable specific parameters for the parser. */
50 void *param1, *param2, *param3, *param4;
52 /* 0 = this variable can be included in debug output and ctrl_iface
53 * 1 = this variable contains key/private data and it must not be
54 * included in debug output unless explicitly requested. In
55 * addition, this variable will not be readable through the
62 static char * wpa_config_parse_string(const char *value, size_t *len)
68 pos = os_strrchr(value, '"');
69 if (pos == NULL || pos[1] != '\0')
72 str = os_malloc(*len + 1);
75 os_memcpy(str, value, *len);
80 size_t tlen, hlen = os_strlen(value);
84 str = os_malloc(tlen + 1);
87 if (hexstr2bin(value, str, tlen)) {
98 static int wpa_config_parse_str(const struct parse_data *data,
99 struct wpa_ssid *ssid,
100 int line, const char *value)
102 size_t res_len, *dst_len;
105 if (os_strcmp(value, "NULL") == 0) {
106 wpa_printf(MSG_DEBUG, "Unset configuration string '%s'",
113 tmp = wpa_config_parse_string(value, &res_len);
115 wpa_printf(MSG_ERROR, "Line %d: failed to parse %s '%s'.",
117 data->key_data ? "[KEY DATA REMOVED]" : value);
121 if (data->key_data) {
122 wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
123 (u8 *) tmp, res_len);
125 wpa_hexdump_ascii(MSG_MSGDUMP, data->name,
126 (u8 *) tmp, res_len);
129 if (data->param3 && res_len < (size_t) data->param3) {
130 wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
131 "min_len=%ld)", line, data->name,
132 (unsigned long) res_len, (long) data->param3);
137 if (data->param4 && res_len > (size_t) data->param4) {
138 wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
139 "max_len=%ld)", line, data->name,
140 (unsigned long) res_len, (long) data->param4);
146 dst = (char **) (((u8 *) ssid) + (long) data->param1);
147 dst_len = (size_t *) (((u8 *) ssid) + (long) data->param2);
157 #ifndef NO_CONFIG_WRITE
158 static int is_hex(const u8 *data, size_t len)
162 for (i = 0; i < len; i++) {
163 if (data[i] < 32 || data[i] >= 127)
170 static char * wpa_config_write_string_ascii(const u8 *value, size_t len)
174 buf = os_malloc(len + 3);
178 os_memcpy(buf + 1, value, len);
186 static char * wpa_config_write_string_hex(const u8 *value, size_t len)
190 buf = os_zalloc(2 * len + 1);
193 wpa_snprintf_hex(buf, 2 * len + 1, value, len);
199 static char * wpa_config_write_string(const u8 *value, size_t len)
204 if (is_hex(value, len))
205 return wpa_config_write_string_hex(value, len);
207 return wpa_config_write_string_ascii(value, len);
211 static char * wpa_config_write_str(const struct parse_data *data,
212 struct wpa_ssid *ssid)
217 src = (char **) (((u8 *) ssid) + (long) data->param1);
222 len = *((size_t *) (((u8 *) ssid) + (long) data->param2));
224 len = os_strlen(*src);
226 return wpa_config_write_string((const u8 *) *src, len);
228 #endif /* NO_CONFIG_WRITE */
231 static int wpa_config_parse_int(const struct parse_data *data,
232 struct wpa_ssid *ssid,
233 int line, const char *value)
237 dst = (int *) (((u8 *) ssid) + (long) data->param1);
239 wpa_printf(MSG_MSGDUMP, "%s=%d (0x%x)", data->name, *dst, *dst);
241 if (data->param3 && *dst < (long) data->param3) {
242 wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
243 "min_value=%ld)", line, data->name, *dst,
244 (long) data->param3);
245 *dst = (long) data->param3;
249 if (data->param4 && *dst > (long) data->param4) {
250 wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
251 "max_value=%ld)", line, data->name, *dst,
252 (long) data->param4);
253 *dst = (long) data->param4;
261 #ifndef NO_CONFIG_WRITE
262 static char * wpa_config_write_int(const struct parse_data *data,
263 struct wpa_ssid *ssid)
268 src = (int *) (((u8 *) ssid) + (long) data->param1);
270 value = os_malloc(20);
273 res = os_snprintf(value, 20, "%d", *src);
274 if (res < 0 || res >= 20) {
278 value[20 - 1] = '\0';
281 #endif /* NO_CONFIG_WRITE */
284 static int wpa_config_parse_bssid(const struct parse_data *data,
285 struct wpa_ssid *ssid, int line,
288 if (value[0] == '\0' || os_strcmp(value, "\"\"") == 0 ||
289 os_strcmp(value, "any") == 0) {
291 wpa_printf(MSG_MSGDUMP, "BSSID any");
294 if (hwaddr_aton(value, ssid->bssid)) {
295 wpa_printf(MSG_ERROR, "Line %d: Invalid BSSID '%s'.",
300 wpa_hexdump(MSG_MSGDUMP, "BSSID", ssid->bssid, ETH_ALEN);
305 #ifndef NO_CONFIG_WRITE
306 static char * wpa_config_write_bssid(const struct parse_data *data,
307 struct wpa_ssid *ssid)
312 if (!ssid->bssid_set)
315 value = os_malloc(20);
318 res = os_snprintf(value, 20, MACSTR, MAC2STR(ssid->bssid));
319 if (res < 0 || res >= 20) {
323 value[20 - 1] = '\0';
326 #endif /* NO_CONFIG_WRITE */
329 static int wpa_config_parse_psk(const struct parse_data *data,
330 struct wpa_ssid *ssid, int line,
334 #ifndef CONFIG_NO_PBKDF2
339 pos = os_strrchr(value, '"');
343 len = os_strlen(value);
344 if (len < 8 || len > 63) {
345 wpa_printf(MSG_ERROR, "Line %d: Invalid passphrase "
346 "length %lu (expected: 8..63) '%s'.",
347 line, (unsigned long) len, value);
350 wpa_hexdump_ascii_key(MSG_MSGDUMP, "PSK (ASCII passphrase)",
352 if (ssid->passphrase && os_strlen(ssid->passphrase) == len &&
353 os_memcmp(ssid->passphrase, value, len) == 0)
356 os_free(ssid->passphrase);
357 ssid->passphrase = os_malloc(len + 1);
358 if (ssid->passphrase == NULL)
360 os_memcpy(ssid->passphrase, value, len);
361 ssid->passphrase[len] = '\0';
363 #else /* CONFIG_NO_PBKDF2 */
364 wpa_printf(MSG_ERROR, "Line %d: ASCII passphrase not "
367 #endif /* CONFIG_NO_PBKDF2 */
370 if (hexstr2bin(value, ssid->psk, PMK_LEN) ||
371 value[PMK_LEN * 2] != '\0') {
372 wpa_printf(MSG_ERROR, "Line %d: Invalid PSK '%s'.",
377 os_free(ssid->passphrase);
378 ssid->passphrase = NULL;
381 wpa_hexdump_key(MSG_MSGDUMP, "PSK", ssid->psk, PMK_LEN);
386 #ifndef NO_CONFIG_WRITE
387 static char * wpa_config_write_psk(const struct parse_data *data,
388 struct wpa_ssid *ssid)
390 if (ssid->passphrase)
391 return wpa_config_write_string_ascii(
392 (const u8 *) ssid->passphrase,
393 os_strlen(ssid->passphrase));
396 return wpa_config_write_string_hex(ssid->psk, PMK_LEN);
400 #endif /* NO_CONFIG_WRITE */
403 static int wpa_config_parse_proto(const struct parse_data *data,
404 struct wpa_ssid *ssid, int line,
407 int val = 0, last, errors = 0;
408 char *start, *end, *buf;
410 buf = os_strdup(value);
415 while (*start != '\0') {
416 while (*start == ' ' || *start == '\t')
421 while (*end != ' ' && *end != '\t' && *end != '\0')
425 if (os_strcmp(start, "WPA") == 0)
426 val |= WPA_PROTO_WPA;
427 else if (os_strcmp(start, "RSN") == 0 ||
428 os_strcmp(start, "WPA2") == 0)
429 val |= WPA_PROTO_RSN;
431 wpa_printf(MSG_ERROR, "Line %d: invalid proto '%s'",
443 wpa_printf(MSG_ERROR,
444 "Line %d: no proto values configured.", line);
448 wpa_printf(MSG_MSGDUMP, "proto: 0x%x", val);
450 return errors ? -1 : 0;
454 #ifndef NO_CONFIG_WRITE
455 static char * wpa_config_write_proto(const struct parse_data *data,
456 struct wpa_ssid *ssid)
459 char *buf, *pos, *end;
461 pos = buf = os_zalloc(10);
466 if (ssid->proto & WPA_PROTO_WPA) {
467 ret = os_snprintf(pos, end - pos, "%sWPA", first ? "" : " ");
468 if (ret < 0 || ret >= end - pos)
474 if (ssid->proto & WPA_PROTO_RSN) {
475 ret = os_snprintf(pos, end - pos, "%sRSN", first ? "" : " ");
476 if (ret < 0 || ret >= end - pos)
484 #endif /* NO_CONFIG_WRITE */
487 static int wpa_config_parse_key_mgmt(const struct parse_data *data,
488 struct wpa_ssid *ssid, int line,
491 int val = 0, last, errors = 0;
492 char *start, *end, *buf;
494 buf = os_strdup(value);
499 while (*start != '\0') {
500 while (*start == ' ' || *start == '\t')
505 while (*end != ' ' && *end != '\t' && *end != '\0')
509 if (os_strcmp(start, "WPA-PSK") == 0)
510 val |= WPA_KEY_MGMT_PSK;
511 else if (os_strcmp(start, "WPA-EAP") == 0)
512 val |= WPA_KEY_MGMT_IEEE8021X;
513 else if (os_strcmp(start, "IEEE8021X") == 0)
514 val |= WPA_KEY_MGMT_IEEE8021X_NO_WPA;
515 else if (os_strcmp(start, "NONE") == 0)
516 val |= WPA_KEY_MGMT_NONE;
517 else if (os_strcmp(start, "WPA-NONE") == 0)
518 val |= WPA_KEY_MGMT_WPA_NONE;
519 #ifdef CONFIG_IEEE80211R
520 else if (os_strcmp(start, "FT-PSK") == 0)
521 val |= WPA_KEY_MGMT_FT_PSK;
522 else if (os_strcmp(start, "FT-EAP") == 0)
523 val |= WPA_KEY_MGMT_FT_IEEE8021X;
524 #endif /* CONFIG_IEEE80211R */
525 #ifdef CONFIG_IEEE80211W
526 else if (os_strcmp(start, "WPA-PSK-SHA256") == 0)
527 val |= WPA_KEY_MGMT_PSK_SHA256;
528 else if (os_strcmp(start, "WPA-EAP-SHA256") == 0)
529 val |= WPA_KEY_MGMT_IEEE8021X_SHA256;
530 #endif /* CONFIG_IEEE80211W */
532 else if (os_strcmp(start, "WPS") == 0)
533 val |= WPA_KEY_MGMT_WPS;
534 #endif /* CONFIG_WPS */
536 wpa_printf(MSG_ERROR, "Line %d: invalid key_mgmt '%s'",
548 wpa_printf(MSG_ERROR,
549 "Line %d: no key_mgmt values configured.", line);
553 wpa_printf(MSG_MSGDUMP, "key_mgmt: 0x%x", val);
554 ssid->key_mgmt = val;
555 return errors ? -1 : 0;
559 #ifndef NO_CONFIG_WRITE
560 static char * wpa_config_write_key_mgmt(const struct parse_data *data,
561 struct wpa_ssid *ssid)
563 char *buf, *pos, *end;
566 pos = buf = os_zalloc(50);
571 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
572 ret = os_snprintf(pos, end - pos, "%sWPA-PSK",
573 pos == buf ? "" : " ");
574 if (ret < 0 || ret >= end - pos) {
581 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
582 ret = os_snprintf(pos, end - pos, "%sWPA-EAP",
583 pos == buf ? "" : " ");
584 if (ret < 0 || ret >= end - pos) {
591 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
592 ret = os_snprintf(pos, end - pos, "%sIEEE8021X",
593 pos == buf ? "" : " ");
594 if (ret < 0 || ret >= end - pos) {
601 if (ssid->key_mgmt & WPA_KEY_MGMT_NONE) {
602 ret = os_snprintf(pos, end - pos, "%sNONE",
603 pos == buf ? "" : " ");
604 if (ret < 0 || ret >= end - pos) {
611 if (ssid->key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
612 ret = os_snprintf(pos, end - pos, "%sWPA-NONE",
613 pos == buf ? "" : " ");
614 if (ret < 0 || ret >= end - pos) {
621 #ifdef CONFIG_IEEE80211R
622 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_PSK)
623 pos += os_snprintf(pos, end - pos, "%sFT-PSK",
624 pos == buf ? "" : " ");
626 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X)
627 pos += os_snprintf(pos, end - pos, "%sFT-EAP",
628 pos == buf ? "" : " ");
629 #endif /* CONFIG_IEEE80211R */
631 #ifdef CONFIG_IEEE80211W
632 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK_SHA256)
633 pos += os_snprintf(pos, end - pos, "%sWPA-PSK-SHA256",
634 pos == buf ? "" : " ");
636 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256)
637 pos += os_snprintf(pos, end - pos, "%sWPA-EAP-SHA256",
638 pos == buf ? "" : " ");
639 #endif /* CONFIG_IEEE80211W */
642 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
643 pos += os_snprintf(pos, end - pos, "%sWPS",
644 pos == buf ? "" : " ");
645 #endif /* CONFIG_WPS */
649 #endif /* NO_CONFIG_WRITE */
652 static int wpa_config_parse_cipher(int line, const char *value)
655 char *start, *end, *buf;
657 buf = os_strdup(value);
662 while (*start != '\0') {
663 while (*start == ' ' || *start == '\t')
668 while (*end != ' ' && *end != '\t' && *end != '\0')
672 if (os_strcmp(start, "CCMP") == 0)
673 val |= WPA_CIPHER_CCMP;
674 else if (os_strcmp(start, "TKIP") == 0)
675 val |= WPA_CIPHER_TKIP;
676 else if (os_strcmp(start, "WEP104") == 0)
677 val |= WPA_CIPHER_WEP104;
678 else if (os_strcmp(start, "WEP40") == 0)
679 val |= WPA_CIPHER_WEP40;
680 else if (os_strcmp(start, "NONE") == 0)
681 val |= WPA_CIPHER_NONE;
683 wpa_printf(MSG_ERROR, "Line %d: invalid cipher '%s'.",
696 wpa_printf(MSG_ERROR, "Line %d: no cipher values configured.",
704 #ifndef NO_CONFIG_WRITE
705 static char * wpa_config_write_cipher(int cipher)
707 char *buf, *pos, *end;
710 pos = buf = os_zalloc(50);
715 if (cipher & WPA_CIPHER_CCMP) {
716 ret = os_snprintf(pos, end - pos, "%sCCMP",
717 pos == buf ? "" : " ");
718 if (ret < 0 || ret >= end - pos) {
725 if (cipher & WPA_CIPHER_TKIP) {
726 ret = os_snprintf(pos, end - pos, "%sTKIP",
727 pos == buf ? "" : " ");
728 if (ret < 0 || ret >= end - pos) {
735 if (cipher & WPA_CIPHER_WEP104) {
736 ret = os_snprintf(pos, end - pos, "%sWEP104",
737 pos == buf ? "" : " ");
738 if (ret < 0 || ret >= end - pos) {
745 if (cipher & WPA_CIPHER_WEP40) {
746 ret = os_snprintf(pos, end - pos, "%sWEP40",
747 pos == buf ? "" : " ");
748 if (ret < 0 || ret >= end - pos) {
755 if (cipher & WPA_CIPHER_NONE) {
756 ret = os_snprintf(pos, end - pos, "%sNONE",
757 pos == buf ? "" : " ");
758 if (ret < 0 || ret >= end - pos) {
767 #endif /* NO_CONFIG_WRITE */
770 static int wpa_config_parse_pairwise(const struct parse_data *data,
771 struct wpa_ssid *ssid, int line,
775 val = wpa_config_parse_cipher(line, value);
778 if (val & ~(WPA_CIPHER_CCMP | WPA_CIPHER_TKIP | WPA_CIPHER_NONE)) {
779 wpa_printf(MSG_ERROR, "Line %d: not allowed pairwise cipher "
780 "(0x%x).", line, val);
784 wpa_printf(MSG_MSGDUMP, "pairwise: 0x%x", val);
785 ssid->pairwise_cipher = val;
790 #ifndef NO_CONFIG_WRITE
791 static char * wpa_config_write_pairwise(const struct parse_data *data,
792 struct wpa_ssid *ssid)
794 return wpa_config_write_cipher(ssid->pairwise_cipher);
796 #endif /* NO_CONFIG_WRITE */
799 static int wpa_config_parse_group(const struct parse_data *data,
800 struct wpa_ssid *ssid, int line,
804 val = wpa_config_parse_cipher(line, value);
807 if (val & ~(WPA_CIPHER_CCMP | WPA_CIPHER_TKIP | WPA_CIPHER_WEP104 |
809 wpa_printf(MSG_ERROR, "Line %d: not allowed group cipher "
810 "(0x%x).", line, val);
814 wpa_printf(MSG_MSGDUMP, "group: 0x%x", val);
815 ssid->group_cipher = val;
820 #ifndef NO_CONFIG_WRITE
821 static char * wpa_config_write_group(const struct parse_data *data,
822 struct wpa_ssid *ssid)
824 return wpa_config_write_cipher(ssid->group_cipher);
826 #endif /* NO_CONFIG_WRITE */
829 static int wpa_config_parse_auth_alg(const struct parse_data *data,
830 struct wpa_ssid *ssid, int line,
833 int val = 0, last, errors = 0;
834 char *start, *end, *buf;
836 buf = os_strdup(value);
841 while (*start != '\0') {
842 while (*start == ' ' || *start == '\t')
847 while (*end != ' ' && *end != '\t' && *end != '\0')
851 if (os_strcmp(start, "OPEN") == 0)
852 val |= WPA_AUTH_ALG_OPEN;
853 else if (os_strcmp(start, "SHARED") == 0)
854 val |= WPA_AUTH_ALG_SHARED;
855 else if (os_strcmp(start, "LEAP") == 0)
856 val |= WPA_AUTH_ALG_LEAP;
858 wpa_printf(MSG_ERROR, "Line %d: invalid auth_alg '%s'",
870 wpa_printf(MSG_ERROR,
871 "Line %d: no auth_alg values configured.", line);
875 wpa_printf(MSG_MSGDUMP, "auth_alg: 0x%x", val);
876 ssid->auth_alg = val;
877 return errors ? -1 : 0;
881 #ifndef NO_CONFIG_WRITE
882 static char * wpa_config_write_auth_alg(const struct parse_data *data,
883 struct wpa_ssid *ssid)
885 char *buf, *pos, *end;
888 pos = buf = os_zalloc(30);
893 if (ssid->auth_alg & WPA_AUTH_ALG_OPEN) {
894 ret = os_snprintf(pos, end - pos, "%sOPEN",
895 pos == buf ? "" : " ");
896 if (ret < 0 || ret >= end - pos) {
903 if (ssid->auth_alg & WPA_AUTH_ALG_SHARED) {
904 ret = os_snprintf(pos, end - pos, "%sSHARED",
905 pos == buf ? "" : " ");
906 if (ret < 0 || ret >= end - pos) {
913 if (ssid->auth_alg & WPA_AUTH_ALG_LEAP) {
914 ret = os_snprintf(pos, end - pos, "%sLEAP",
915 pos == buf ? "" : " ");
916 if (ret < 0 || ret >= end - pos) {
925 #endif /* NO_CONFIG_WRITE */
928 static int * wpa_config_parse_freqs(const struct parse_data *data,
929 struct wpa_ssid *ssid, int line,
938 freqs = os_zalloc((len + 1) * sizeof(int));
949 n = os_realloc(freqs, (len * 2 + 1) * sizeof(int));
954 for (i = len; i <= len * 2; i++)
960 freqs[used] = atoi(pos);
961 if (freqs[used] == 0)
964 pos = os_strchr(pos + 1, ' ');
971 static int wpa_config_parse_scan_freq(const struct parse_data *data,
972 struct wpa_ssid *ssid, int line,
977 freqs = wpa_config_parse_freqs(data, ssid, line, value);
980 os_free(ssid->scan_freq);
981 ssid->scan_freq = freqs;
987 static int wpa_config_parse_freq_list(const struct parse_data *data,
988 struct wpa_ssid *ssid, int line,
993 freqs = wpa_config_parse_freqs(data, ssid, line, value);
996 os_free(ssid->freq_list);
997 ssid->freq_list = freqs;
1003 #ifndef NO_CONFIG_WRITE
1004 static char * wpa_config_write_freqs(const struct parse_data *data,
1007 char *buf, *pos, *end;
1015 for (i = 0; freqs[i]; i++)
1018 pos = buf = os_zalloc(10 * count + 1);
1021 end = buf + 10 * count + 1;
1023 for (i = 0; freqs[i]; i++) {
1024 ret = os_snprintf(pos, end - pos, "%s%u",
1025 i == 0 ? "" : " ", freqs[i]);
1026 if (ret < 0 || ret >= end - pos) {
1037 static char * wpa_config_write_scan_freq(const struct parse_data *data,
1038 struct wpa_ssid *ssid)
1040 return wpa_config_write_freqs(data, ssid->scan_freq);
1044 static char * wpa_config_write_freq_list(const struct parse_data *data,
1045 struct wpa_ssid *ssid)
1047 return wpa_config_write_freqs(data, ssid->freq_list);
1049 #endif /* NO_CONFIG_WRITE */
1052 #ifdef IEEE8021X_EAPOL
1053 static int wpa_config_parse_eap(const struct parse_data *data,
1054 struct wpa_ssid *ssid, int line,
1057 int last, errors = 0;
1058 char *start, *end, *buf;
1059 struct eap_method_type *methods = NULL, *tmp;
1060 size_t num_methods = 0;
1062 buf = os_strdup(value);
1067 while (*start != '\0') {
1068 while (*start == ' ' || *start == '\t')
1073 while (*end != ' ' && *end != '\t' && *end != '\0')
1075 last = *end == '\0';
1078 methods = os_realloc(methods,
1079 (num_methods + 1) * sizeof(*methods));
1080 if (methods == NULL) {
1085 methods[num_methods].method = eap_peer_get_type(
1086 start, &methods[num_methods].vendor);
1087 if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
1088 methods[num_methods].method == EAP_TYPE_NONE) {
1089 wpa_printf(MSG_ERROR, "Line %d: unknown EAP method "
1090 "'%s'", line, start);
1091 wpa_printf(MSG_ERROR, "You may need to add support for"
1092 " this EAP method during wpa_supplicant\n"
1093 "build time configuration.\n"
1094 "See README for more information.");
1096 } else if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
1097 methods[num_methods].method == EAP_TYPE_LEAP)
1109 methods = os_realloc(methods, (num_methods + 1) * sizeof(*methods));
1110 if (methods == NULL) {
1114 methods[num_methods].vendor = EAP_VENDOR_IETF;
1115 methods[num_methods].method = EAP_TYPE_NONE;
1118 wpa_hexdump(MSG_MSGDUMP, "eap methods",
1119 (u8 *) methods, num_methods * sizeof(*methods));
1120 os_free(ssid->eap.eap_methods);
1121 ssid->eap.eap_methods = methods;
1122 return errors ? -1 : 0;
1126 static char * wpa_config_write_eap(const struct parse_data *data,
1127 struct wpa_ssid *ssid)
1130 char *buf, *pos, *end;
1131 const struct eap_method_type *eap_methods = ssid->eap.eap_methods;
1134 if (eap_methods == NULL)
1137 pos = buf = os_zalloc(100);
1142 for (i = 0; eap_methods[i].vendor != EAP_VENDOR_IETF ||
1143 eap_methods[i].method != EAP_TYPE_NONE; i++) {
1144 name = eap_get_name(eap_methods[i].vendor,
1145 eap_methods[i].method);
1147 ret = os_snprintf(pos, end - pos, "%s%s",
1148 pos == buf ? "" : " ", name);
1149 if (ret < 0 || ret >= end - pos)
1161 static int wpa_config_parse_password(const struct parse_data *data,
1162 struct wpa_ssid *ssid, int line,
1167 if (os_strcmp(value, "NULL") == 0) {
1168 wpa_printf(MSG_DEBUG, "Unset configuration string 'password'");
1169 os_free(ssid->eap.password);
1170 ssid->eap.password = NULL;
1171 ssid->eap.password_len = 0;
1175 if (os_strncmp(value, "hash:", 5) != 0) {
1179 tmp = wpa_config_parse_string(value, &res_len);
1181 wpa_printf(MSG_ERROR, "Line %d: failed to parse "
1185 wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
1186 (u8 *) tmp, res_len);
1188 os_free(ssid->eap.password);
1189 ssid->eap.password = (u8 *) tmp;
1190 ssid->eap.password_len = res_len;
1191 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
1197 /* NtPasswordHash: hash:<32 hex digits> */
1198 if (os_strlen(value + 5) != 2 * 16) {
1199 wpa_printf(MSG_ERROR, "Line %d: Invalid password hash length "
1200 "(expected 32 hex digits)", line);
1204 hash = os_malloc(16);
1208 if (hexstr2bin(value + 5, hash, 16)) {
1210 wpa_printf(MSG_ERROR, "Line %d: Invalid password hash", line);
1214 wpa_hexdump_key(MSG_MSGDUMP, data->name, hash, 16);
1216 os_free(ssid->eap.password);
1217 ssid->eap.password = hash;
1218 ssid->eap.password_len = 16;
1219 ssid->eap.flags |= EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
1225 static char * wpa_config_write_password(const struct parse_data *data,
1226 struct wpa_ssid *ssid)
1230 if (ssid->eap.password == NULL)
1233 if (!(ssid->eap.flags & EAP_CONFIG_FLAGS_PASSWORD_NTHASH)) {
1234 return wpa_config_write_string(
1235 ssid->eap.password, ssid->eap.password_len);
1238 buf = os_malloc(5 + 32 + 1);
1242 os_memcpy(buf, "hash:", 5);
1243 wpa_snprintf_hex(buf + 5, 32 + 1, ssid->eap.password, 16);
1247 #endif /* IEEE8021X_EAPOL */
1250 static int wpa_config_parse_wep_key(u8 *key, size_t *len, int line,
1251 const char *value, int idx)
1253 char *buf, title[20];
1256 buf = wpa_config_parse_string(value, len);
1258 wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key %d '%s'.",
1262 if (*len > MAX_WEP_KEY_LEN) {
1263 wpa_printf(MSG_ERROR, "Line %d: Too long WEP key %d '%s'.",
1268 os_memcpy(key, buf, *len);
1270 res = os_snprintf(title, sizeof(title), "wep_key%d", idx);
1271 if (res >= 0 && (size_t) res < sizeof(title))
1272 wpa_hexdump_key(MSG_MSGDUMP, title, key, *len);
1277 static int wpa_config_parse_wep_key0(const struct parse_data *data,
1278 struct wpa_ssid *ssid, int line,
1281 return wpa_config_parse_wep_key(ssid->wep_key[0],
1282 &ssid->wep_key_len[0], line,
1287 static int wpa_config_parse_wep_key1(const struct parse_data *data,
1288 struct wpa_ssid *ssid, int line,
1291 return wpa_config_parse_wep_key(ssid->wep_key[1],
1292 &ssid->wep_key_len[1], line,
1297 static int wpa_config_parse_wep_key2(const struct parse_data *data,
1298 struct wpa_ssid *ssid, int line,
1301 return wpa_config_parse_wep_key(ssid->wep_key[2],
1302 &ssid->wep_key_len[2], line,
1307 static int wpa_config_parse_wep_key3(const struct parse_data *data,
1308 struct wpa_ssid *ssid, int line,
1311 return wpa_config_parse_wep_key(ssid->wep_key[3],
1312 &ssid->wep_key_len[3], line,
1317 #ifndef NO_CONFIG_WRITE
1318 static char * wpa_config_write_wep_key(struct wpa_ssid *ssid, int idx)
1320 if (ssid->wep_key_len[idx] == 0)
1322 return wpa_config_write_string(ssid->wep_key[idx],
1323 ssid->wep_key_len[idx]);
1327 static char * wpa_config_write_wep_key0(const struct parse_data *data,
1328 struct wpa_ssid *ssid)
1330 return wpa_config_write_wep_key(ssid, 0);
1334 static char * wpa_config_write_wep_key1(const struct parse_data *data,
1335 struct wpa_ssid *ssid)
1337 return wpa_config_write_wep_key(ssid, 1);
1341 static char * wpa_config_write_wep_key2(const struct parse_data *data,
1342 struct wpa_ssid *ssid)
1344 return wpa_config_write_wep_key(ssid, 2);
1348 static char * wpa_config_write_wep_key3(const struct parse_data *data,
1349 struct wpa_ssid *ssid)
1351 return wpa_config_write_wep_key(ssid, 3);
1353 #endif /* NO_CONFIG_WRITE */
1358 static int wpa_config_parse_p2p_client_list(const struct parse_data *data,
1359 struct wpa_ssid *ssid, int line,
1363 u8 *buf, *n, addr[ETH_ALEN];
1370 while (pos && *pos) {
1374 if (hwaddr_aton(pos, addr)) {
1375 wpa_printf(MSG_ERROR, "Line %d: Invalid "
1376 "p2p_client_list address '%s'.",
1378 /* continue anyway */
1380 n = os_realloc(buf, (count + 1) * ETH_ALEN);
1386 os_memcpy(buf + count * ETH_ALEN, addr, ETH_ALEN);
1388 wpa_hexdump(MSG_MSGDUMP, "p2p_client_list",
1392 pos = os_strchr(pos, ' ');
1395 os_free(ssid->p2p_client_list);
1396 ssid->p2p_client_list = buf;
1397 ssid->num_p2p_clients = count;
1403 #ifndef NO_CONFIG_WRITE
1404 static char * wpa_config_write_p2p_client_list(const struct parse_data *data,
1405 struct wpa_ssid *ssid)
1407 char *value, *end, *pos;
1411 if (ssid->p2p_client_list == NULL || ssid->num_p2p_clients == 0)
1414 value = os_malloc(20 * ssid->num_p2p_clients);
1418 end = value + 20 * ssid->num_p2p_clients;
1420 for (i = 0; i < ssid->num_p2p_clients; i++) {
1421 res = os_snprintf(pos, end - pos, MACSTR " ",
1422 MAC2STR(ssid->p2p_client_list +
1424 if (res < 0 || res >= end - pos) {
1436 #endif /* NO_CONFIG_WRITE */
1438 #endif /* CONFIG_P2P */
1440 /* Helper macros for network block parser */
1445 /* OFFSET: Get offset of a variable within the wpa_ssid structure */
1446 #define OFFSET(v) ((void *) &((struct wpa_ssid *) 0)->v)
1448 /* STR: Define a string variable for an ASCII string; f = field name */
1449 #ifdef NO_CONFIG_WRITE
1450 #define _STR(f) #f, wpa_config_parse_str, OFFSET(f)
1451 #define _STRe(f) #f, wpa_config_parse_str, OFFSET(eap.f)
1452 #else /* NO_CONFIG_WRITE */
1453 #define _STR(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(f)
1454 #define _STRe(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(eap.f)
1455 #endif /* NO_CONFIG_WRITE */
1456 #define STR(f) _STR(f), NULL, NULL, NULL, 0
1457 #define STRe(f) _STRe(f), NULL, NULL, NULL, 0
1458 #define STR_KEY(f) _STR(f), NULL, NULL, NULL, 1
1459 #define STR_KEYe(f) _STRe(f), NULL, NULL, NULL, 1
1461 /* STR_LEN: Define a string variable with a separate variable for storing the
1462 * data length. Unlike STR(), this can be used to store arbitrary binary data
1463 * (i.e., even nul termination character). */
1464 #define _STR_LEN(f) _STR(f), OFFSET(f ## _len)
1465 #define _STR_LENe(f) _STRe(f), OFFSET(eap.f ## _len)
1466 #define STR_LEN(f) _STR_LEN(f), NULL, NULL, 0
1467 #define STR_LENe(f) _STR_LENe(f), NULL, NULL, 0
1468 #define STR_LEN_KEY(f) _STR_LEN(f), NULL, NULL, 1
1470 /* STR_RANGE: Like STR_LEN(), but with minimum and maximum allowed length
1471 * explicitly specified. */
1472 #define _STR_RANGE(f, min, max) _STR_LEN(f), (void *) (min), (void *) (max)
1473 #define STR_RANGE(f, min, max) _STR_RANGE(f, min, max), 0
1474 #define STR_RANGE_KEY(f, min, max) _STR_RANGE(f, min, max), 1
1476 #ifdef NO_CONFIG_WRITE
1477 #define _INT(f) #f, wpa_config_parse_int, OFFSET(f), (void *) 0
1478 #define _INTe(f) #f, wpa_config_parse_int, OFFSET(eap.f), (void *) 0
1479 #else /* NO_CONFIG_WRITE */
1480 #define _INT(f) #f, wpa_config_parse_int, wpa_config_write_int, \
1481 OFFSET(f), (void *) 0
1482 #define _INTe(f) #f, wpa_config_parse_int, wpa_config_write_int, \
1483 OFFSET(eap.f), (void *) 0
1484 #endif /* NO_CONFIG_WRITE */
1486 /* INT: Define an integer variable */
1487 #define INT(f) _INT(f), NULL, NULL, 0
1488 #define INTe(f) _INTe(f), NULL, NULL, 0
1490 /* INT_RANGE: Define an integer variable with allowed value range */
1491 #define INT_RANGE(f, min, max) _INT(f), (void *) (min), (void *) (max), 0
1493 /* FUNC: Define a configuration variable that uses a custom function for
1494 * parsing and writing the value. */
1495 #ifdef NO_CONFIG_WRITE
1496 #define _FUNC(f) #f, wpa_config_parse_ ## f, NULL, NULL, NULL, NULL
1497 #else /* NO_CONFIG_WRITE */
1498 #define _FUNC(f) #f, wpa_config_parse_ ## f, wpa_config_write_ ## f, \
1499 NULL, NULL, NULL, NULL
1500 #endif /* NO_CONFIG_WRITE */
1501 #define FUNC(f) _FUNC(f), 0
1502 #define FUNC_KEY(f) _FUNC(f), 1
1505 * Table of network configuration variables. This table is used to parse each
1506 * network configuration variable, e.g., each line in wpa_supplicant.conf file
1507 * that is inside a network block.
1509 * This table is generated using the helper macros defined above and with
1510 * generous help from the C pre-processor. The field name is stored as a string
1511 * into .name and for STR and INT types, the offset of the target buffer within
1512 * struct wpa_ssid is stored in .param1. .param2 (if not NULL) is similar
1513 * offset to the field containing the length of the configuration variable.
1514 * .param3 and .param4 can be used to mark the allowed range (length for STR
1515 * and value for INT).
1517 * For each configuration line in wpa_supplicant.conf, the parser goes through
1518 * this table and select the entry that matches with the field name. The parser
1519 * function (.parser) is then called to parse the actual value of the field.
1521 * This kind of mechanism makes it easy to add new configuration parameters,
1522 * since only one line needs to be added into this table and into the
1523 * struct wpa_ssid definition if the new variable is either a string or
1524 * integer. More complex types will need to use their own parser and writer
1527 static const struct parse_data ssid_fields[] = {
1528 { STR_RANGE(ssid, 0, MAX_SSID_LEN) },
1529 { INT_RANGE(scan_ssid, 0, 1) },
1537 { FUNC(scan_freq) },
1538 { FUNC(freq_list) },
1539 #ifdef IEEE8021X_EAPOL
1541 { STR_LENe(identity) },
1542 { STR_LENe(anonymous_identity) },
1543 { FUNC_KEY(password) },
1546 { STRe(client_cert) },
1547 { STRe(private_key) },
1548 { STR_KEYe(private_key_passwd) },
1550 { STRe(subject_match) },
1551 { STRe(altsubject_match) },
1554 { STRe(client_cert2) },
1555 { STRe(private_key2) },
1556 { STR_KEYe(private_key2_passwd) },
1558 { STRe(subject_match2) },
1559 { STRe(altsubject_match2) },
1564 { STRe(engine_id) },
1567 { STRe(ca_cert_id) },
1569 { STRe(engine2_id) },
1572 { STRe(ca_cert2_id) },
1575 { INT(eapol_flags) },
1576 #endif /* IEEE8021X_EAPOL */
1577 { FUNC_KEY(wep_key0) },
1578 { FUNC_KEY(wep_key1) },
1579 { FUNC_KEY(wep_key2) },
1580 { FUNC_KEY(wep_key3) },
1581 { INT(wep_tx_keyidx) },
1583 #ifdef IEEE8021X_EAPOL
1584 { INT(eap_workaround) },
1586 { INTe(fragment_size) },
1587 #endif /* IEEE8021X_EAPOL */
1588 { INT_RANGE(mode, 0, 4) },
1589 { INT_RANGE(proactive_key_caching, 0, 1) },
1590 { INT_RANGE(disabled, 0, 2) },
1592 #ifdef CONFIG_IEEE80211W
1593 { INT_RANGE(ieee80211w, 0, 2) },
1594 #endif /* CONFIG_IEEE80211W */
1595 { INT_RANGE(peerkey, 0, 1) },
1596 { INT_RANGE(mixed_cell, 0, 1) },
1597 { INT_RANGE(frequency, 0, 10000) },
1598 { INT(wpa_ptk_rekey) },
1601 { FUNC(p2p_client_list) },
1602 #endif /* CONFIG_P2P */
1614 #undef STR_RANGE_KEY
1621 #define NUM_SSID_FIELDS (sizeof(ssid_fields) / sizeof(ssid_fields[0]))
1625 * wpa_config_add_prio_network - Add a network to priority lists
1626 * @config: Configuration data from wpa_config_read()
1627 * @ssid: Pointer to the network configuration to be added to the list
1628 * Returns: 0 on success, -1 on failure
1630 * This function is used to add a network block to the priority list of
1631 * networks. This must be called for each network when reading in the full
1632 * configuration. In addition, this can be used indirectly when updating
1633 * priorities by calling wpa_config_update_prio_list().
1635 int wpa_config_add_prio_network(struct wpa_config *config,
1636 struct wpa_ssid *ssid)
1639 struct wpa_ssid *prev, **nlist;
1642 * Add to an existing priority list if one is available for the
1643 * configured priority level for this network.
1645 for (prio = 0; prio < config->num_prio; prio++) {
1646 prev = config->pssid[prio];
1647 if (prev->priority == ssid->priority) {
1655 /* First network for this priority - add a new priority list */
1656 nlist = os_realloc(config->pssid,
1657 (config->num_prio + 1) * sizeof(struct wpa_ssid *));
1661 for (prio = 0; prio < config->num_prio; prio++) {
1662 if (nlist[prio]->priority < ssid->priority)
1666 os_memmove(&nlist[prio + 1], &nlist[prio],
1667 (config->num_prio - prio) * sizeof(struct wpa_ssid *));
1671 config->pssid = nlist;
1678 * wpa_config_update_prio_list - Update network priority list
1679 * @config: Configuration data from wpa_config_read()
1680 * Returns: 0 on success, -1 on failure
1682 * This function is called to update the priority list of networks in the
1683 * configuration when a network is being added or removed. This is also called
1684 * if a priority for a network is changed.
1686 int wpa_config_update_prio_list(struct wpa_config *config)
1688 struct wpa_ssid *ssid;
1691 os_free(config->pssid);
1692 config->pssid = NULL;
1693 config->num_prio = 0;
1695 ssid = config->ssid;
1698 if (wpa_config_add_prio_network(config, ssid) < 0)
1707 #ifdef IEEE8021X_EAPOL
1708 static void eap_peer_config_free(struct eap_peer_config *eap)
1710 os_free(eap->eap_methods);
1711 os_free(eap->identity);
1712 os_free(eap->anonymous_identity);
1713 os_free(eap->password);
1714 os_free(eap->ca_cert);
1715 os_free(eap->ca_path);
1716 os_free(eap->client_cert);
1717 os_free(eap->private_key);
1718 os_free(eap->private_key_passwd);
1719 os_free(eap->dh_file);
1720 os_free(eap->subject_match);
1721 os_free(eap->altsubject_match);
1722 os_free(eap->ca_cert2);
1723 os_free(eap->ca_path2);
1724 os_free(eap->client_cert2);
1725 os_free(eap->private_key2);
1726 os_free(eap->private_key2_passwd);
1727 os_free(eap->dh_file2);
1728 os_free(eap->subject_match2);
1729 os_free(eap->altsubject_match2);
1730 os_free(eap->phase1);
1731 os_free(eap->phase2);
1734 os_free(eap->engine_id);
1735 os_free(eap->key_id);
1736 os_free(eap->cert_id);
1737 os_free(eap->ca_cert_id);
1738 os_free(eap->key2_id);
1739 os_free(eap->cert2_id);
1740 os_free(eap->ca_cert2_id);
1742 os_free(eap->engine2_id);
1744 os_free(eap->pending_req_otp);
1745 os_free(eap->pac_file);
1746 os_free(eap->new_password);
1748 #endif /* IEEE8021X_EAPOL */
1752 * wpa_config_free_ssid - Free network/ssid configuration data
1753 * @ssid: Configuration data for the network
1755 * This function frees all resources allocated for the network configuration
1758 void wpa_config_free_ssid(struct wpa_ssid *ssid)
1760 os_free(ssid->ssid);
1761 os_free(ssid->passphrase);
1762 #ifdef IEEE8021X_EAPOL
1763 eap_peer_config_free(&ssid->eap);
1764 #endif /* IEEE8021X_EAPOL */
1765 os_free(ssid->id_str);
1766 os_free(ssid->scan_freq);
1767 os_free(ssid->freq_list);
1768 os_free(ssid->bgscan);
1769 os_free(ssid->p2p_client_list);
1775 * wpa_config_free - Free configuration data
1776 * @config: Configuration data from wpa_config_read()
1778 * This function frees all resources allocated for the configuration data by
1779 * wpa_config_read().
1781 void wpa_config_free(struct wpa_config *config)
1783 #ifndef CONFIG_NO_CONFIG_BLOBS
1784 struct wpa_config_blob *blob, *prevblob;
1785 #endif /* CONFIG_NO_CONFIG_BLOBS */
1786 struct wpa_ssid *ssid, *prev = NULL;
1788 ssid = config->ssid;
1792 wpa_config_free_ssid(prev);
1795 #ifndef CONFIG_NO_CONFIG_BLOBS
1796 blob = config->blobs;
1801 wpa_config_free_blob(prevblob);
1803 #endif /* CONFIG_NO_CONFIG_BLOBS */
1805 os_free(config->ctrl_interface);
1806 os_free(config->ctrl_interface_group);
1807 os_free(config->opensc_engine_path);
1808 os_free(config->pkcs11_engine_path);
1809 os_free(config->pkcs11_module_path);
1810 os_free(config->driver_param);
1811 os_free(config->device_name);
1812 os_free(config->manufacturer);
1813 os_free(config->model_name);
1814 os_free(config->model_number);
1815 os_free(config->serial_number);
1816 os_free(config->config_methods);
1817 os_free(config->p2p_ssid_postfix);
1818 os_free(config->pssid);
1819 os_free(config->home_realm);
1820 os_free(config->home_username);
1821 os_free(config->home_password);
1822 os_free(config->home_ca_cert);
1823 os_free(config->home_imsi);
1824 os_free(config->home_milenage);
1825 os_free(config->p2p_pref_chan);
1831 * wpa_config_foreach_network - Iterate over each configured network
1832 * @config: Configuration data from wpa_config_read()
1833 * @func: Callback function to process each network
1834 * @arg: Opaque argument to pass to callback function
1836 * Iterate over the set of configured networks calling the specified
1837 * function for each item. We guard against callbacks removing the
1840 void wpa_config_foreach_network(struct wpa_config *config,
1841 void (*func)(void *, struct wpa_ssid *),
1844 struct wpa_ssid *ssid, *next;
1846 ssid = config->ssid;
1856 * wpa_config_get_network - Get configured network based on id
1857 * @config: Configuration data from wpa_config_read()
1858 * @id: Unique network id to search for
1859 * Returns: Network configuration or %NULL if not found
1861 struct wpa_ssid * wpa_config_get_network(struct wpa_config *config, int id)
1863 struct wpa_ssid *ssid;
1865 ssid = config->ssid;
1877 * wpa_config_add_network - Add a new network with empty configuration
1878 * @config: Configuration data from wpa_config_read()
1879 * Returns: The new network configuration or %NULL if operation failed
1881 struct wpa_ssid * wpa_config_add_network(struct wpa_config *config)
1884 struct wpa_ssid *ssid, *last = NULL;
1887 ssid = config->ssid;
1896 ssid = os_zalloc(sizeof(*ssid));
1903 config->ssid = ssid;
1905 wpa_config_update_prio_list(config);
1912 * wpa_config_remove_network - Remove a configured network based on id
1913 * @config: Configuration data from wpa_config_read()
1914 * @id: Unique network id to search for
1915 * Returns: 0 on success, or -1 if the network was not found
1917 int wpa_config_remove_network(struct wpa_config *config, int id)
1919 struct wpa_ssid *ssid, *prev = NULL;
1921 ssid = config->ssid;
1933 prev->next = ssid->next;
1935 config->ssid = ssid->next;
1937 wpa_config_update_prio_list(config);
1938 wpa_config_free_ssid(ssid);
1944 * wpa_config_set_network_defaults - Set network default values
1945 * @ssid: Pointer to network configuration data
1947 void wpa_config_set_network_defaults(struct wpa_ssid *ssid)
1949 ssid->proto = DEFAULT_PROTO;
1950 ssid->pairwise_cipher = DEFAULT_PAIRWISE;
1951 ssid->group_cipher = DEFAULT_GROUP;
1952 ssid->key_mgmt = DEFAULT_KEY_MGMT;
1953 #ifdef IEEE8021X_EAPOL
1954 ssid->eapol_flags = DEFAULT_EAPOL_FLAGS;
1955 ssid->eap_workaround = DEFAULT_EAP_WORKAROUND;
1956 ssid->eap.fragment_size = DEFAULT_FRAGMENT_SIZE;
1957 #endif /* IEEE8021X_EAPOL */
1962 * wpa_config_set - Set a variable in network configuration
1963 * @ssid: Pointer to network configuration data
1964 * @var: Variable name, e.g., "ssid"
1965 * @value: Variable value
1966 * @line: Line number in configuration file or 0 if not used
1967 * Returns: 0 on success, -1 on failure
1969 * This function can be used to set network configuration variables based on
1970 * both the configuration file and management interface input. The value
1971 * parameter must be in the same format as the text-based configuration file is
1972 * using. For example, strings are using double quotation marks.
1974 int wpa_config_set(struct wpa_ssid *ssid, const char *var, const char *value,
1980 if (ssid == NULL || var == NULL || value == NULL)
1983 for (i = 0; i < NUM_SSID_FIELDS; i++) {
1984 const struct parse_data *field = &ssid_fields[i];
1985 if (os_strcmp(var, field->name) != 0)
1988 if (field->parser(field, ssid, line, value)) {
1990 wpa_printf(MSG_ERROR, "Line %d: failed to "
1991 "parse %s '%s'.", line, var, value);
1997 if (i == NUM_SSID_FIELDS) {
1999 wpa_printf(MSG_ERROR, "Line %d: unknown network field "
2000 "'%s'.", line, var);
2009 int wpa_config_set_quoted(struct wpa_ssid *ssid, const char *var,
2016 len = os_strlen(value);
2017 buf = os_malloc(len + 3);
2021 os_memcpy(buf + 1, value, len);
2023 buf[len + 2] = '\0';
2024 ret = wpa_config_set(ssid, var, buf, 0);
2031 * wpa_config_get_all - Get all options from network configuration
2032 * @ssid: Pointer to network configuration data
2033 * @get_keys: Determines if keys/passwords will be included in returned list
2034 * (if they may be exported)
2035 * Returns: %NULL terminated list of all set keys and their values in the form
2036 * of [key1, val1, key2, val2, ... , NULL]
2038 * This function can be used to get list of all configured network properties.
2039 * The caller is responsible for freeing the returned list and all its
2042 char ** wpa_config_get_all(struct wpa_ssid *ssid, int get_keys)
2044 const struct parse_data *field;
2050 get_keys = get_keys && ssid->export_keys;
2052 props = os_zalloc(sizeof(char *) * ((2 * NUM_SSID_FIELDS) + 1));
2057 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2058 field = &ssid_fields[i];
2059 if (field->key_data && !get_keys)
2061 value = field->writer(field, ssid);
2064 if (os_strlen(value) == 0) {
2069 key = os_strdup(field->name);
2075 props[fields_num * 2] = key;
2076 props[fields_num * 2 + 1] = value;
2092 #ifndef NO_CONFIG_WRITE
2094 * wpa_config_get - Get a variable in network configuration
2095 * @ssid: Pointer to network configuration data
2096 * @var: Variable name, e.g., "ssid"
2097 * Returns: Value of the variable or %NULL on failure
2099 * This function can be used to get network configuration variables. The
2100 * returned value is a copy of the configuration variable in text format, i.e,.
2101 * the same format that the text-based configuration file and wpa_config_set()
2102 * are using for the value. The caller is responsible for freeing the returned
2105 char * wpa_config_get(struct wpa_ssid *ssid, const char *var)
2109 if (ssid == NULL || var == NULL)
2112 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2113 const struct parse_data *field = &ssid_fields[i];
2114 if (os_strcmp(var, field->name) == 0)
2115 return field->writer(field, ssid);
2123 * wpa_config_get_no_key - Get a variable in network configuration (no keys)
2124 * @ssid: Pointer to network configuration data
2125 * @var: Variable name, e.g., "ssid"
2126 * Returns: Value of the variable or %NULL on failure
2128 * This function can be used to get network configuration variable like
2129 * wpa_config_get(). The only difference is that this functions does not expose
2130 * key/password material from the configuration. In case a key/password field
2131 * is requested, the returned value is an empty string or %NULL if the variable
2132 * is not set or "*" if the variable is set (regardless of its value). The
2133 * returned value is a copy of the configuration variable in text format, i.e,.
2134 * the same format that the text-based configuration file and wpa_config_set()
2135 * are using for the value. The caller is responsible for freeing the returned
2138 char * wpa_config_get_no_key(struct wpa_ssid *ssid, const char *var)
2142 if (ssid == NULL || var == NULL)
2145 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2146 const struct parse_data *field = &ssid_fields[i];
2147 if (os_strcmp(var, field->name) == 0) {
2148 char *res = field->writer(field, ssid);
2149 if (field->key_data) {
2150 if (res && res[0]) {
2151 wpa_printf(MSG_DEBUG, "Do not allow "
2152 "key_data field to be "
2155 return os_strdup("*");
2167 #endif /* NO_CONFIG_WRITE */
2171 * wpa_config_update_psk - Update WPA PSK based on passphrase and SSID
2172 * @ssid: Pointer to network configuration data
2174 * This function must be called to update WPA PSK when either SSID or the
2175 * passphrase has changed for the network configuration.
2177 void wpa_config_update_psk(struct wpa_ssid *ssid)
2179 #ifndef CONFIG_NO_PBKDF2
2180 pbkdf2_sha1(ssid->passphrase,
2181 (char *) ssid->ssid, ssid->ssid_len, 4096,
2182 ssid->psk, PMK_LEN);
2183 wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
2184 ssid->psk, PMK_LEN);
2186 #endif /* CONFIG_NO_PBKDF2 */
2190 #ifndef CONFIG_NO_CONFIG_BLOBS
2192 * wpa_config_get_blob - Get a named configuration blob
2193 * @config: Configuration data from wpa_config_read()
2194 * @name: Name of the blob
2195 * Returns: Pointer to blob data or %NULL if not found
2197 const struct wpa_config_blob * wpa_config_get_blob(struct wpa_config *config,
2200 struct wpa_config_blob *blob = config->blobs;
2203 if (os_strcmp(blob->name, name) == 0)
2212 * wpa_config_set_blob - Set or add a named configuration blob
2213 * @config: Configuration data from wpa_config_read()
2214 * @blob: New value for the blob
2216 * Adds a new configuration blob or replaces the current value of an existing
2219 void wpa_config_set_blob(struct wpa_config *config,
2220 struct wpa_config_blob *blob)
2222 wpa_config_remove_blob(config, blob->name);
2223 blob->next = config->blobs;
2224 config->blobs = blob;
2229 * wpa_config_free_blob - Free blob data
2230 * @blob: Pointer to blob to be freed
2232 void wpa_config_free_blob(struct wpa_config_blob *blob)
2235 os_free(blob->name);
2236 os_free(blob->data);
2243 * wpa_config_remove_blob - Remove a named configuration blob
2244 * @config: Configuration data from wpa_config_read()
2245 * @name: Name of the blob to remove
2246 * Returns: 0 if blob was removed or -1 if blob was not found
2248 int wpa_config_remove_blob(struct wpa_config *config, const char *name)
2250 struct wpa_config_blob *pos = config->blobs, *prev = NULL;
2253 if (os_strcmp(pos->name, name) == 0) {
2255 prev->next = pos->next;
2257 config->blobs = pos->next;
2258 wpa_config_free_blob(pos);
2267 #endif /* CONFIG_NO_CONFIG_BLOBS */
2271 * wpa_config_alloc_empty - Allocate an empty configuration
2272 * @ctrl_interface: Control interface parameters, e.g., path to UNIX domain
2274 * @driver_param: Driver parameters
2275 * Returns: Pointer to allocated configuration data or %NULL on failure
2277 struct wpa_config * wpa_config_alloc_empty(const char *ctrl_interface,
2278 const char *driver_param)
2280 struct wpa_config *config;
2282 config = os_zalloc(sizeof(*config));
2285 config->eapol_version = DEFAULT_EAPOL_VERSION;
2286 config->ap_scan = DEFAULT_AP_SCAN;
2287 config->fast_reauth = DEFAULT_FAST_REAUTH;
2288 config->p2p_go_intent = DEFAULT_P2P_GO_INTENT;
2289 config->p2p_intra_bss = DEFAULT_P2P_INTRA_BSS;
2290 config->bss_max_count = DEFAULT_BSS_MAX_COUNT;
2291 config->bss_expiration_age = DEFAULT_BSS_EXPIRATION_AGE;
2292 config->bss_expiration_scan_count = DEFAULT_BSS_EXPIRATION_SCAN_COUNT;
2293 config->max_num_sta = DEFAULT_MAX_NUM_STA;
2294 config->access_network_type = DEFAULT_ACCESS_NETWORK_TYPE;
2297 config->ctrl_interface = os_strdup(ctrl_interface);
2299 config->driver_param = os_strdup(driver_param);
2305 #ifndef CONFIG_NO_STDOUT_DEBUG
2307 * wpa_config_debug_dump_networks - Debug dump of configured networks
2308 * @config: Configuration data from wpa_config_read()
2310 void wpa_config_debug_dump_networks(struct wpa_config *config)
2313 struct wpa_ssid *ssid;
2315 for (prio = 0; prio < config->num_prio; prio++) {
2316 ssid = config->pssid[prio];
2317 wpa_printf(MSG_DEBUG, "Priority group %d",
2320 wpa_printf(MSG_DEBUG, " id=%d ssid='%s'",
2322 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
2327 #endif /* CONFIG_NO_STDOUT_DEBUG */
2330 struct global_parse_data {
2332 int (*parser)(const struct global_parse_data *data,
2333 struct wpa_config *config, int line, const char *value);
2334 void *param1, *param2, *param3;
2335 unsigned int changed_flag;
2339 static int wpa_global_config_parse_int(const struct global_parse_data *data,
2340 struct wpa_config *config, int line,
2344 dst = (int *) (((u8 *) config) + (long) data->param1);
2346 wpa_printf(MSG_DEBUG, "%s=%d", data->name, *dst);
2348 if (data->param2 && *dst < (long) data->param2) {
2349 wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
2350 "min_value=%ld)", line, data->name, *dst,
2351 (long) data->param2);
2352 *dst = (long) data->param2;
2356 if (data->param3 && *dst > (long) data->param3) {
2357 wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
2358 "max_value=%ld)", line, data->name, *dst,
2359 (long) data->param3);
2360 *dst = (long) data->param3;
2368 static int wpa_global_config_parse_str(const struct global_parse_data *data,
2369 struct wpa_config *config, int line,
2375 len = os_strlen(pos);
2376 if (data->param2 && len < (size_t) data->param2) {
2377 wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
2378 "min_len=%ld)", line, data->name,
2379 (unsigned long) len, (long) data->param2);
2383 if (data->param3 && len > (size_t) data->param3) {
2384 wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
2385 "max_len=%ld)", line, data->name,
2386 (unsigned long) len, (long) data->param3);
2390 tmp = os_strdup(pos);
2394 dst = (char **) (((u8 *) config) + (long) data->param1);
2397 wpa_printf(MSG_DEBUG, "%s='%s'", data->name, *dst);
2403 static int wpa_config_process_country(const struct global_parse_data *data,
2404 struct wpa_config *config, int line,
2407 if (!pos[0] || !pos[1]) {
2408 wpa_printf(MSG_DEBUG, "Invalid country set");
2411 config->country[0] = pos[0];
2412 config->country[1] = pos[1];
2413 wpa_printf(MSG_DEBUG, "country='%c%c'",
2414 config->country[0], config->country[1]);
2419 static int wpa_config_process_load_dynamic_eap(
2420 const struct global_parse_data *data, struct wpa_config *config,
2421 int line, const char *so)
2424 wpa_printf(MSG_DEBUG, "load_dynamic_eap=%s", so);
2425 ret = eap_peer_method_load(so);
2427 wpa_printf(MSG_DEBUG, "This EAP type was already loaded - not "
2430 wpa_printf(MSG_ERROR, "Line %d: Failed to load dynamic EAP "
2431 "method '%s'.", line, so);
2441 static int wpa_config_process_uuid(const struct global_parse_data *data,
2442 struct wpa_config *config, int line,
2446 if (uuid_str2bin(pos, config->uuid)) {
2447 wpa_printf(MSG_ERROR, "Line %d: invalid UUID", line);
2450 uuid_bin2str(config->uuid, buf, sizeof(buf));
2451 wpa_printf(MSG_DEBUG, "uuid=%s", buf);
2456 static int wpa_config_process_device_type(
2457 const struct global_parse_data *data,
2458 struct wpa_config *config, int line, const char *pos)
2460 return wps_dev_type_str2bin(pos, config->device_type);
2464 static int wpa_config_process_os_version(const struct global_parse_data *data,
2465 struct wpa_config *config, int line,
2468 if (hexstr2bin(pos, config->os_version, 4)) {
2469 wpa_printf(MSG_ERROR, "Line %d: invalid os_version", line);
2472 wpa_printf(MSG_DEBUG, "os_version=%08x",
2473 WPA_GET_BE32(config->os_version));
2477 #endif /* CONFIG_WPS */
2480 static int wpa_config_process_sec_device_type(
2481 const struct global_parse_data *data,
2482 struct wpa_config *config, int line, const char *pos)
2486 if (config->num_sec_device_types >= MAX_SEC_DEVICE_TYPES) {
2487 wpa_printf(MSG_ERROR, "Line %d: too many sec_device_type "
2492 idx = config->num_sec_device_types;
2494 if (wps_dev_type_str2bin(pos, config->sec_device_type[idx]))
2497 config->num_sec_device_types++;
2502 static int wpa_config_process_p2p_pref_chan(
2503 const struct global_parse_data *data,
2504 struct wpa_config *config, int line, const char *pos)
2506 struct p2p_channel *pref = NULL, *n;
2507 unsigned int num = 0;
2511 /* format: class:chan,class:chan,... */
2514 op_class = atoi(pos);
2515 pos2 = os_strchr(pos, ':');
2521 n = os_realloc(pref, (num + 1) * sizeof(struct p2p_channel));
2525 pref[num].op_class = op_class;
2526 pref[num].chan = chan;
2529 pos = os_strchr(pos2, ',');
2535 os_free(config->p2p_pref_chan);
2536 config->p2p_pref_chan = pref;
2537 config->num_p2p_pref_chan = num;
2538 wpa_hexdump(MSG_DEBUG, "P2P: Preferred class/channel pairs",
2539 (u8 *) config->p2p_pref_chan,
2540 config->num_p2p_pref_chan * sizeof(struct p2p_channel));
2546 wpa_printf(MSG_ERROR, "Line %d: Invalid p2p_pref_chan list", line);
2549 #endif /* CONFIG_P2P */
2552 static int wpa_config_process_hessid(
2553 const struct global_parse_data *data,
2554 struct wpa_config *config, int line, const char *pos)
2556 if (hwaddr_aton2(pos, config->hessid) < 0) {
2557 wpa_printf(MSG_ERROR, "Line %d: Invalid hessid '%s'",
2569 /* OFFSET: Get offset of a variable within the wpa_config structure */
2570 #define OFFSET(v) ((void *) &((struct wpa_config *) 0)->v)
2572 #define FUNC(f) #f, wpa_config_process_ ## f, OFFSET(f), NULL, NULL
2573 #define FUNC_NO_VAR(f) #f, wpa_config_process_ ## f, NULL, NULL, NULL
2574 #define _INT(f) #f, wpa_global_config_parse_int, OFFSET(f)
2575 #define INT(f) _INT(f), NULL, NULL
2576 #define INT_RANGE(f, min, max) _INT(f), (void *) min, (void *) max
2577 #define _STR(f) #f, wpa_global_config_parse_str, OFFSET(f)
2578 #define STR(f) _STR(f), NULL, NULL
2579 #define STR_RANGE(f, min, max) _STR(f), (void *) min, (void *) max
2581 static const struct global_parse_data global_fields[] = {
2582 #ifdef CONFIG_CTRL_IFACE
2583 { STR(ctrl_interface), 0 },
2584 { STR(ctrl_interface_group), 0 } /* deprecated */,
2585 #endif /* CONFIG_CTRL_IFACE */
2586 { INT_RANGE(eapol_version, 1, 2), 0 },
2587 { INT(ap_scan), 0 },
2588 { INT(fast_reauth), 0 },
2589 { STR(opensc_engine_path), 0 },
2590 { STR(pkcs11_engine_path), 0 },
2591 { STR(pkcs11_module_path), 0 },
2592 { STR(driver_param), 0 },
2593 { INT(dot11RSNAConfigPMKLifetime), 0 },
2594 { INT(dot11RSNAConfigPMKReauthThreshold), 0 },
2595 { INT(dot11RSNAConfigSATimeout), 0 },
2596 #ifndef CONFIG_NO_CONFIG_WRITE
2597 { INT(update_config), 0 },
2598 #endif /* CONFIG_NO_CONFIG_WRITE */
2599 { FUNC_NO_VAR(load_dynamic_eap), 0 },
2601 { FUNC(uuid), CFG_CHANGED_UUID },
2602 { STR_RANGE(device_name, 0, 32), CFG_CHANGED_DEVICE_NAME },
2603 { STR_RANGE(manufacturer, 0, 64), CFG_CHANGED_WPS_STRING },
2604 { STR_RANGE(model_name, 0, 32), CFG_CHANGED_WPS_STRING },
2605 { STR_RANGE(model_number, 0, 32), CFG_CHANGED_WPS_STRING },
2606 { STR_RANGE(serial_number, 0, 32), CFG_CHANGED_WPS_STRING },
2607 { FUNC(device_type), CFG_CHANGED_DEVICE_TYPE },
2608 { FUNC(os_version), CFG_CHANGED_OS_VERSION },
2609 { STR(config_methods), CFG_CHANGED_CONFIG_METHODS },
2610 { INT_RANGE(wps_cred_processing, 0, 2), 0 },
2611 #endif /* CONFIG_WPS */
2613 { FUNC(sec_device_type), CFG_CHANGED_SEC_DEVICE_TYPE },
2614 { INT(p2p_listen_reg_class), 0 },
2615 { INT(p2p_listen_channel), 0 },
2616 { INT(p2p_oper_reg_class), 0 },
2617 { INT(p2p_oper_channel), 0 },
2618 { INT_RANGE(p2p_go_intent, 0, 15), 0 },
2619 { STR(p2p_ssid_postfix), CFG_CHANGED_P2P_SSID_POSTFIX },
2620 { INT_RANGE(persistent_reconnect, 0, 1), 0 },
2621 { INT_RANGE(p2p_intra_bss, 0, 1), CFG_CHANGED_P2P_INTRA_BSS },
2622 { INT(p2p_group_idle), 0 },
2623 { FUNC(p2p_pref_chan), CFG_CHANGED_P2P_PREF_CHAN },
2624 #endif /* CONFIG_P2P */
2625 { FUNC(country), CFG_CHANGED_COUNTRY },
2626 { INT(bss_max_count), 0 },
2627 { INT(bss_expiration_age), 0 },
2628 { INT(bss_expiration_scan_count), 0 },
2629 { INT_RANGE(filter_ssids, 0, 1), 0 },
2630 { INT(max_num_sta), 0 },
2631 { INT_RANGE(disassoc_low_ack, 0, 1), 0 },
2632 { STR(home_realm), 0 },
2633 { STR(home_username), 0 },
2634 { STR(home_password), 0 },
2635 { STR(home_ca_cert), 0 },
2636 { STR(home_imsi), 0 },
2637 { STR(home_milenage), 0 },
2638 { INT_RANGE(interworking, 0, 1), 0 },
2639 { FUNC(hessid), 0 },
2640 { INT_RANGE(access_network_type, 0, 15), 0 }
2650 #define NUM_GLOBAL_FIELDS (sizeof(global_fields) / sizeof(global_fields[0]))
2653 int wpa_config_process_global(struct wpa_config *config, char *pos, int line)
2658 for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
2659 const struct global_parse_data *field = &global_fields[i];
2660 size_t flen = os_strlen(field->name);
2661 if (os_strncmp(pos, field->name, flen) != 0 ||
2665 if (field->parser(field, config, line, pos + flen + 1)) {
2666 wpa_printf(MSG_ERROR, "Line %d: failed to "
2667 "parse '%s'.", line, pos);
2670 config->changed_parameters |= field->changed_flag;
2673 if (i == NUM_GLOBAL_FIELDS) {
2676 wpa_printf(MSG_ERROR, "Line %d: unknown global field '%s'.",