2 * WPA Supplicant / Configuration parser and common functions
3 * Copyright (c) 2003-2008, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
12 #include "utils/uuid.h"
13 #include "crypto/sha1.h"
14 #include "rsn_supp/wpa.h"
15 #include "eap_peer/eap.h"
19 #if !defined(CONFIG_CTRL_IFACE) && defined(CONFIG_NO_CONFIG_WRITE)
20 #define NO_CONFIG_WRITE
24 * Structure for network configuration parsing. This data is used to implement
25 * a generic parser for each network block variable. The table of configuration
26 * variables is defined below in this file (ssid_fields[]).
29 /* Configuration variable name */
32 /* Parser function for this variable */
33 int (*parser)(const struct parse_data *data, struct wpa_ssid *ssid,
34 int line, const char *value);
36 #ifndef NO_CONFIG_WRITE
37 /* Writer function (i.e., to get the variable in text format from
38 * internal presentation). */
39 char * (*writer)(const struct parse_data *data, struct wpa_ssid *ssid);
40 #endif /* NO_CONFIG_WRITE */
42 /* Variable specific parameters for the parser. */
43 void *param1, *param2, *param3, *param4;
45 /* 0 = this variable can be included in debug output and ctrl_iface
46 * 1 = this variable contains key/private data and it must not be
47 * included in debug output unless explicitly requested. In
48 * addition, this variable will not be readable through the
55 static char * wpa_config_parse_string(const char *value, size_t *len)
61 pos = os_strrchr(value, '"');
62 if (pos == NULL || pos[1] != '\0')
65 str = os_malloc(*len + 1);
68 os_memcpy(str, value, *len);
73 size_t tlen, hlen = os_strlen(value);
77 str = os_malloc(tlen + 1);
80 if (hexstr2bin(value, str, tlen)) {
91 static int wpa_config_parse_str(const struct parse_data *data,
92 struct wpa_ssid *ssid,
93 int line, const char *value)
95 size_t res_len, *dst_len;
98 if (os_strcmp(value, "NULL") == 0) {
99 wpa_printf(MSG_DEBUG, "Unset configuration string '%s'",
106 tmp = wpa_config_parse_string(value, &res_len);
108 wpa_printf(MSG_ERROR, "Line %d: failed to parse %s '%s'.",
110 data->key_data ? "[KEY DATA REMOVED]" : value);
114 if (data->key_data) {
115 wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
116 (u8 *) tmp, res_len);
118 wpa_hexdump_ascii(MSG_MSGDUMP, data->name,
119 (u8 *) tmp, res_len);
122 if (data->param3 && res_len < (size_t) data->param3) {
123 wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
124 "min_len=%ld)", line, data->name,
125 (unsigned long) res_len, (long) data->param3);
130 if (data->param4 && res_len > (size_t) data->param4) {
131 wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
132 "max_len=%ld)", line, data->name,
133 (unsigned long) res_len, (long) data->param4);
139 dst = (char **) (((u8 *) ssid) + (long) data->param1);
140 dst_len = (size_t *) (((u8 *) ssid) + (long) data->param2);
150 #ifndef NO_CONFIG_WRITE
151 static int is_hex(const u8 *data, size_t len)
155 for (i = 0; i < len; i++) {
156 if (data[i] < 32 || data[i] >= 127)
163 static char * wpa_config_write_string_ascii(const u8 *value, size_t len)
167 buf = os_malloc(len + 3);
171 os_memcpy(buf + 1, value, len);
179 static char * wpa_config_write_string_hex(const u8 *value, size_t len)
183 buf = os_zalloc(2 * len + 1);
186 wpa_snprintf_hex(buf, 2 * len + 1, value, len);
192 static char * wpa_config_write_string(const u8 *value, size_t len)
197 if (is_hex(value, len))
198 return wpa_config_write_string_hex(value, len);
200 return wpa_config_write_string_ascii(value, len);
204 static char * wpa_config_write_str(const struct parse_data *data,
205 struct wpa_ssid *ssid)
210 src = (char **) (((u8 *) ssid) + (long) data->param1);
215 len = *((size_t *) (((u8 *) ssid) + (long) data->param2));
217 len = os_strlen(*src);
219 return wpa_config_write_string((const u8 *) *src, len);
221 #endif /* NO_CONFIG_WRITE */
224 static int wpa_config_parse_int(const struct parse_data *data,
225 struct wpa_ssid *ssid,
226 int line, const char *value)
230 dst = (int *) (((u8 *) ssid) + (long) data->param1);
232 wpa_printf(MSG_MSGDUMP, "%s=%d (0x%x)", data->name, *dst, *dst);
234 if (data->param3 && *dst < (long) data->param3) {
235 wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
236 "min_value=%ld)", line, data->name, *dst,
237 (long) data->param3);
238 *dst = (long) data->param3;
242 if (data->param4 && *dst > (long) data->param4) {
243 wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
244 "max_value=%ld)", line, data->name, *dst,
245 (long) data->param4);
246 *dst = (long) data->param4;
254 #ifndef NO_CONFIG_WRITE
255 static char * wpa_config_write_int(const struct parse_data *data,
256 struct wpa_ssid *ssid)
261 src = (int *) (((u8 *) ssid) + (long) data->param1);
263 value = os_malloc(20);
266 res = os_snprintf(value, 20, "%d", *src);
267 if (res < 0 || res >= 20) {
271 value[20 - 1] = '\0';
274 #endif /* NO_CONFIG_WRITE */
277 static int wpa_config_parse_bssid(const struct parse_data *data,
278 struct wpa_ssid *ssid, int line,
281 if (value[0] == '\0' || os_strcmp(value, "\"\"") == 0 ||
282 os_strcmp(value, "any") == 0) {
284 wpa_printf(MSG_MSGDUMP, "BSSID any");
287 if (hwaddr_aton(value, ssid->bssid)) {
288 wpa_printf(MSG_ERROR, "Line %d: Invalid BSSID '%s'.",
293 wpa_hexdump(MSG_MSGDUMP, "BSSID", ssid->bssid, ETH_ALEN);
298 #ifndef NO_CONFIG_WRITE
299 static char * wpa_config_write_bssid(const struct parse_data *data,
300 struct wpa_ssid *ssid)
305 if (!ssid->bssid_set)
308 value = os_malloc(20);
311 res = os_snprintf(value, 20, MACSTR, MAC2STR(ssid->bssid));
312 if (res < 0 || res >= 20) {
316 value[20 - 1] = '\0';
319 #endif /* NO_CONFIG_WRITE */
322 static int wpa_config_parse_psk(const struct parse_data *data,
323 struct wpa_ssid *ssid, int line,
327 #ifndef CONFIG_NO_PBKDF2
332 pos = os_strrchr(value, '"');
336 len = os_strlen(value);
337 if (len < 8 || len > 63) {
338 wpa_printf(MSG_ERROR, "Line %d: Invalid passphrase "
339 "length %lu (expected: 8..63) '%s'.",
340 line, (unsigned long) len, value);
343 wpa_hexdump_ascii_key(MSG_MSGDUMP, "PSK (ASCII passphrase)",
345 if (ssid->passphrase && os_strlen(ssid->passphrase) == len &&
346 os_memcmp(ssid->passphrase, value, len) == 0)
349 os_free(ssid->passphrase);
350 ssid->passphrase = os_malloc(len + 1);
351 if (ssid->passphrase == NULL)
353 os_memcpy(ssid->passphrase, value, len);
354 ssid->passphrase[len] = '\0';
356 #else /* CONFIG_NO_PBKDF2 */
357 wpa_printf(MSG_ERROR, "Line %d: ASCII passphrase not "
360 #endif /* CONFIG_NO_PBKDF2 */
363 if (hexstr2bin(value, ssid->psk, PMK_LEN) ||
364 value[PMK_LEN * 2] != '\0') {
365 wpa_printf(MSG_ERROR, "Line %d: Invalid PSK '%s'.",
370 os_free(ssid->passphrase);
371 ssid->passphrase = NULL;
374 wpa_hexdump_key(MSG_MSGDUMP, "PSK", ssid->psk, PMK_LEN);
379 #ifndef NO_CONFIG_WRITE
380 static char * wpa_config_write_psk(const struct parse_data *data,
381 struct wpa_ssid *ssid)
383 if (ssid->passphrase)
384 return wpa_config_write_string_ascii(
385 (const u8 *) ssid->passphrase,
386 os_strlen(ssid->passphrase));
389 return wpa_config_write_string_hex(ssid->psk, PMK_LEN);
393 #endif /* NO_CONFIG_WRITE */
396 static int wpa_config_parse_proto(const struct parse_data *data,
397 struct wpa_ssid *ssid, int line,
400 int val = 0, last, errors = 0;
401 char *start, *end, *buf;
403 buf = os_strdup(value);
408 while (*start != '\0') {
409 while (*start == ' ' || *start == '\t')
414 while (*end != ' ' && *end != '\t' && *end != '\0')
418 if (os_strcmp(start, "WPA") == 0)
419 val |= WPA_PROTO_WPA;
420 else if (os_strcmp(start, "RSN") == 0 ||
421 os_strcmp(start, "WPA2") == 0)
422 val |= WPA_PROTO_RSN;
424 wpa_printf(MSG_ERROR, "Line %d: invalid proto '%s'",
436 wpa_printf(MSG_ERROR,
437 "Line %d: no proto values configured.", line);
441 wpa_printf(MSG_MSGDUMP, "proto: 0x%x", val);
443 return errors ? -1 : 0;
447 #ifndef NO_CONFIG_WRITE
448 static char * wpa_config_write_proto(const struct parse_data *data,
449 struct wpa_ssid *ssid)
452 char *buf, *pos, *end;
454 pos = buf = os_zalloc(10);
459 if (ssid->proto & WPA_PROTO_WPA) {
460 ret = os_snprintf(pos, end - pos, "%sWPA", first ? "" : " ");
461 if (ret < 0 || ret >= end - pos)
467 if (ssid->proto & WPA_PROTO_RSN) {
468 ret = os_snprintf(pos, end - pos, "%sRSN", first ? "" : " ");
469 if (ret < 0 || ret >= end - pos)
477 #endif /* NO_CONFIG_WRITE */
480 static int wpa_config_parse_key_mgmt(const struct parse_data *data,
481 struct wpa_ssid *ssid, int line,
484 int val = 0, last, errors = 0;
485 char *start, *end, *buf;
487 buf = os_strdup(value);
492 while (*start != '\0') {
493 while (*start == ' ' || *start == '\t')
498 while (*end != ' ' && *end != '\t' && *end != '\0')
502 if (os_strcmp(start, "WPA-PSK") == 0)
503 val |= WPA_KEY_MGMT_PSK;
504 else if (os_strcmp(start, "WPA-EAP") == 0)
505 val |= WPA_KEY_MGMT_IEEE8021X;
506 else if (os_strcmp(start, "IEEE8021X") == 0)
507 val |= WPA_KEY_MGMT_IEEE8021X_NO_WPA;
508 else if (os_strcmp(start, "NONE") == 0)
509 val |= WPA_KEY_MGMT_NONE;
510 else if (os_strcmp(start, "WPA-NONE") == 0)
511 val |= WPA_KEY_MGMT_WPA_NONE;
512 #ifdef CONFIG_IEEE80211R
513 else if (os_strcmp(start, "FT-PSK") == 0)
514 val |= WPA_KEY_MGMT_FT_PSK;
515 else if (os_strcmp(start, "FT-EAP") == 0)
516 val |= WPA_KEY_MGMT_FT_IEEE8021X;
517 #endif /* CONFIG_IEEE80211R */
518 #ifdef CONFIG_IEEE80211W
519 else if (os_strcmp(start, "WPA-PSK-SHA256") == 0)
520 val |= WPA_KEY_MGMT_PSK_SHA256;
521 else if (os_strcmp(start, "WPA-EAP-SHA256") == 0)
522 val |= WPA_KEY_MGMT_IEEE8021X_SHA256;
523 #endif /* CONFIG_IEEE80211W */
525 else if (os_strcmp(start, "WPS") == 0)
526 val |= WPA_KEY_MGMT_WPS;
527 #endif /* CONFIG_WPS */
529 wpa_printf(MSG_ERROR, "Line %d: invalid key_mgmt '%s'",
541 wpa_printf(MSG_ERROR,
542 "Line %d: no key_mgmt values configured.", line);
546 wpa_printf(MSG_MSGDUMP, "key_mgmt: 0x%x", val);
547 ssid->key_mgmt = val;
548 return errors ? -1 : 0;
552 #ifndef NO_CONFIG_WRITE
553 static char * wpa_config_write_key_mgmt(const struct parse_data *data,
554 struct wpa_ssid *ssid)
556 char *buf, *pos, *end;
559 pos = buf = os_zalloc(50);
564 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
565 ret = os_snprintf(pos, end - pos, "%sWPA-PSK",
566 pos == buf ? "" : " ");
567 if (ret < 0 || ret >= end - pos) {
574 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
575 ret = os_snprintf(pos, end - pos, "%sWPA-EAP",
576 pos == buf ? "" : " ");
577 if (ret < 0 || ret >= end - pos) {
584 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
585 ret = os_snprintf(pos, end - pos, "%sIEEE8021X",
586 pos == buf ? "" : " ");
587 if (ret < 0 || ret >= end - pos) {
594 if (ssid->key_mgmt & WPA_KEY_MGMT_NONE) {
595 ret = os_snprintf(pos, end - pos, "%sNONE",
596 pos == buf ? "" : " ");
597 if (ret < 0 || ret >= end - pos) {
604 if (ssid->key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
605 ret = os_snprintf(pos, end - pos, "%sWPA-NONE",
606 pos == buf ? "" : " ");
607 if (ret < 0 || ret >= end - pos) {
614 #ifdef CONFIG_IEEE80211R
615 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_PSK)
616 pos += os_snprintf(pos, end - pos, "%sFT-PSK",
617 pos == buf ? "" : " ");
619 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X)
620 pos += os_snprintf(pos, end - pos, "%sFT-EAP",
621 pos == buf ? "" : " ");
622 #endif /* CONFIG_IEEE80211R */
624 #ifdef CONFIG_IEEE80211W
625 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK_SHA256)
626 pos += os_snprintf(pos, end - pos, "%sWPA-PSK-SHA256",
627 pos == buf ? "" : " ");
629 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256)
630 pos += os_snprintf(pos, end - pos, "%sWPA-EAP-SHA256",
631 pos == buf ? "" : " ");
632 #endif /* CONFIG_IEEE80211W */
635 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
636 pos += os_snprintf(pos, end - pos, "%sWPS",
637 pos == buf ? "" : " ");
638 #endif /* CONFIG_WPS */
642 #endif /* NO_CONFIG_WRITE */
645 static int wpa_config_parse_cipher(int line, const char *value)
648 char *start, *end, *buf;
650 buf = os_strdup(value);
655 while (*start != '\0') {
656 while (*start == ' ' || *start == '\t')
661 while (*end != ' ' && *end != '\t' && *end != '\0')
665 if (os_strcmp(start, "CCMP") == 0)
666 val |= WPA_CIPHER_CCMP;
667 else if (os_strcmp(start, "TKIP") == 0)
668 val |= WPA_CIPHER_TKIP;
669 else if (os_strcmp(start, "WEP104") == 0)
670 val |= WPA_CIPHER_WEP104;
671 else if (os_strcmp(start, "WEP40") == 0)
672 val |= WPA_CIPHER_WEP40;
673 else if (os_strcmp(start, "NONE") == 0)
674 val |= WPA_CIPHER_NONE;
676 wpa_printf(MSG_ERROR, "Line %d: invalid cipher '%s'.",
689 wpa_printf(MSG_ERROR, "Line %d: no cipher values configured.",
697 #ifndef NO_CONFIG_WRITE
698 static char * wpa_config_write_cipher(int cipher)
700 char *buf, *pos, *end;
703 pos = buf = os_zalloc(50);
708 if (cipher & WPA_CIPHER_CCMP) {
709 ret = os_snprintf(pos, end - pos, "%sCCMP",
710 pos == buf ? "" : " ");
711 if (ret < 0 || ret >= end - pos) {
718 if (cipher & WPA_CIPHER_TKIP) {
719 ret = os_snprintf(pos, end - pos, "%sTKIP",
720 pos == buf ? "" : " ");
721 if (ret < 0 || ret >= end - pos) {
728 if (cipher & WPA_CIPHER_WEP104) {
729 ret = os_snprintf(pos, end - pos, "%sWEP104",
730 pos == buf ? "" : " ");
731 if (ret < 0 || ret >= end - pos) {
738 if (cipher & WPA_CIPHER_WEP40) {
739 ret = os_snprintf(pos, end - pos, "%sWEP40",
740 pos == buf ? "" : " ");
741 if (ret < 0 || ret >= end - pos) {
748 if (cipher & WPA_CIPHER_NONE) {
749 ret = os_snprintf(pos, end - pos, "%sNONE",
750 pos == buf ? "" : " ");
751 if (ret < 0 || ret >= end - pos) {
760 #endif /* NO_CONFIG_WRITE */
763 static int wpa_config_parse_pairwise(const struct parse_data *data,
764 struct wpa_ssid *ssid, int line,
768 val = wpa_config_parse_cipher(line, value);
771 if (val & ~(WPA_CIPHER_CCMP | WPA_CIPHER_TKIP | WPA_CIPHER_NONE)) {
772 wpa_printf(MSG_ERROR, "Line %d: not allowed pairwise cipher "
773 "(0x%x).", line, val);
777 wpa_printf(MSG_MSGDUMP, "pairwise: 0x%x", val);
778 ssid->pairwise_cipher = val;
783 #ifndef NO_CONFIG_WRITE
784 static char * wpa_config_write_pairwise(const struct parse_data *data,
785 struct wpa_ssid *ssid)
787 return wpa_config_write_cipher(ssid->pairwise_cipher);
789 #endif /* NO_CONFIG_WRITE */
792 static int wpa_config_parse_group(const struct parse_data *data,
793 struct wpa_ssid *ssid, int line,
797 val = wpa_config_parse_cipher(line, value);
800 if (val & ~(WPA_CIPHER_CCMP | WPA_CIPHER_TKIP | WPA_CIPHER_WEP104 |
802 wpa_printf(MSG_ERROR, "Line %d: not allowed group cipher "
803 "(0x%x).", line, val);
807 wpa_printf(MSG_MSGDUMP, "group: 0x%x", val);
808 ssid->group_cipher = val;
813 #ifndef NO_CONFIG_WRITE
814 static char * wpa_config_write_group(const struct parse_data *data,
815 struct wpa_ssid *ssid)
817 return wpa_config_write_cipher(ssid->group_cipher);
819 #endif /* NO_CONFIG_WRITE */
822 static int wpa_config_parse_auth_alg(const struct parse_data *data,
823 struct wpa_ssid *ssid, int line,
826 int val = 0, last, errors = 0;
827 char *start, *end, *buf;
829 buf = os_strdup(value);
834 while (*start != '\0') {
835 while (*start == ' ' || *start == '\t')
840 while (*end != ' ' && *end != '\t' && *end != '\0')
844 if (os_strcmp(start, "OPEN") == 0)
845 val |= WPA_AUTH_ALG_OPEN;
846 else if (os_strcmp(start, "SHARED") == 0)
847 val |= WPA_AUTH_ALG_SHARED;
848 else if (os_strcmp(start, "LEAP") == 0)
849 val |= WPA_AUTH_ALG_LEAP;
851 wpa_printf(MSG_ERROR, "Line %d: invalid auth_alg '%s'",
863 wpa_printf(MSG_ERROR,
864 "Line %d: no auth_alg values configured.", line);
868 wpa_printf(MSG_MSGDUMP, "auth_alg: 0x%x", val);
869 ssid->auth_alg = val;
870 return errors ? -1 : 0;
874 #ifndef NO_CONFIG_WRITE
875 static char * wpa_config_write_auth_alg(const struct parse_data *data,
876 struct wpa_ssid *ssid)
878 char *buf, *pos, *end;
881 pos = buf = os_zalloc(30);
886 if (ssid->auth_alg & WPA_AUTH_ALG_OPEN) {
887 ret = os_snprintf(pos, end - pos, "%sOPEN",
888 pos == buf ? "" : " ");
889 if (ret < 0 || ret >= end - pos) {
896 if (ssid->auth_alg & WPA_AUTH_ALG_SHARED) {
897 ret = os_snprintf(pos, end - pos, "%sSHARED",
898 pos == buf ? "" : " ");
899 if (ret < 0 || ret >= end - pos) {
906 if (ssid->auth_alg & WPA_AUTH_ALG_LEAP) {
907 ret = os_snprintf(pos, end - pos, "%sLEAP",
908 pos == buf ? "" : " ");
909 if (ret < 0 || ret >= end - pos) {
918 #endif /* NO_CONFIG_WRITE */
921 static int * wpa_config_parse_freqs(const struct parse_data *data,
922 struct wpa_ssid *ssid, int line,
931 freqs = os_zalloc((len + 1) * sizeof(int));
942 n = os_realloc(freqs, (len * 2 + 1) * sizeof(int));
947 for (i = len; i <= len * 2; i++)
953 freqs[used] = atoi(pos);
954 if (freqs[used] == 0)
957 pos = os_strchr(pos + 1, ' ');
964 static int wpa_config_parse_scan_freq(const struct parse_data *data,
965 struct wpa_ssid *ssid, int line,
970 freqs = wpa_config_parse_freqs(data, ssid, line, value);
973 os_free(ssid->scan_freq);
974 ssid->scan_freq = freqs;
980 static int wpa_config_parse_freq_list(const struct parse_data *data,
981 struct wpa_ssid *ssid, int line,
986 freqs = wpa_config_parse_freqs(data, ssid, line, value);
989 os_free(ssid->freq_list);
990 ssid->freq_list = freqs;
996 #ifndef NO_CONFIG_WRITE
997 static char * wpa_config_write_freqs(const struct parse_data *data,
1000 char *buf, *pos, *end;
1008 for (i = 0; freqs[i]; i++)
1011 pos = buf = os_zalloc(10 * count + 1);
1014 end = buf + 10 * count + 1;
1016 for (i = 0; freqs[i]; i++) {
1017 ret = os_snprintf(pos, end - pos, "%s%u",
1018 i == 0 ? "" : " ", freqs[i]);
1019 if (ret < 0 || ret >= end - pos) {
1030 static char * wpa_config_write_scan_freq(const struct parse_data *data,
1031 struct wpa_ssid *ssid)
1033 return wpa_config_write_freqs(data, ssid->scan_freq);
1037 static char * wpa_config_write_freq_list(const struct parse_data *data,
1038 struct wpa_ssid *ssid)
1040 return wpa_config_write_freqs(data, ssid->freq_list);
1042 #endif /* NO_CONFIG_WRITE */
1045 #ifdef IEEE8021X_EAPOL
1046 static int wpa_config_parse_eap(const struct parse_data *data,
1047 struct wpa_ssid *ssid, int line,
1050 int last, errors = 0;
1051 char *start, *end, *buf;
1052 struct eap_method_type *methods = NULL, *tmp;
1053 size_t num_methods = 0;
1055 buf = os_strdup(value);
1060 while (*start != '\0') {
1061 while (*start == ' ' || *start == '\t')
1066 while (*end != ' ' && *end != '\t' && *end != '\0')
1068 last = *end == '\0';
1071 methods = os_realloc(methods,
1072 (num_methods + 1) * sizeof(*methods));
1073 if (methods == NULL) {
1078 methods[num_methods].method = eap_peer_get_type(
1079 start, &methods[num_methods].vendor);
1080 if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
1081 methods[num_methods].method == EAP_TYPE_NONE) {
1082 wpa_printf(MSG_ERROR, "Line %d: unknown EAP method "
1083 "'%s'", line, start);
1084 wpa_printf(MSG_ERROR, "You may need to add support for"
1085 " this EAP method during wpa_supplicant\n"
1086 "build time configuration.\n"
1087 "See README for more information.");
1089 } else if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
1090 methods[num_methods].method == EAP_TYPE_LEAP)
1102 methods = os_realloc(methods, (num_methods + 1) * sizeof(*methods));
1103 if (methods == NULL) {
1107 methods[num_methods].vendor = EAP_VENDOR_IETF;
1108 methods[num_methods].method = EAP_TYPE_NONE;
1111 wpa_hexdump(MSG_MSGDUMP, "eap methods",
1112 (u8 *) methods, num_methods * sizeof(*methods));
1113 ssid->eap.eap_methods = methods;
1114 return errors ? -1 : 0;
1118 static char * wpa_config_write_eap(const struct parse_data *data,
1119 struct wpa_ssid *ssid)
1122 char *buf, *pos, *end;
1123 const struct eap_method_type *eap_methods = ssid->eap.eap_methods;
1126 if (eap_methods == NULL)
1129 pos = buf = os_zalloc(100);
1134 for (i = 0; eap_methods[i].vendor != EAP_VENDOR_IETF ||
1135 eap_methods[i].method != EAP_TYPE_NONE; i++) {
1136 name = eap_get_name(eap_methods[i].vendor,
1137 eap_methods[i].method);
1139 ret = os_snprintf(pos, end - pos, "%s%s",
1140 pos == buf ? "" : " ", name);
1141 if (ret < 0 || ret >= end - pos)
1153 static int wpa_config_parse_password(const struct parse_data *data,
1154 struct wpa_ssid *ssid, int line,
1159 if (os_strcmp(value, "NULL") == 0) {
1160 wpa_printf(MSG_DEBUG, "Unset configuration string 'password'");
1161 os_free(ssid->eap.password);
1162 ssid->eap.password = NULL;
1163 ssid->eap.password_len = 0;
1167 if (os_strncmp(value, "hash:", 5) != 0) {
1171 tmp = wpa_config_parse_string(value, &res_len);
1173 wpa_printf(MSG_ERROR, "Line %d: failed to parse "
1177 wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
1178 (u8 *) tmp, res_len);
1180 os_free(ssid->eap.password);
1181 ssid->eap.password = (u8 *) tmp;
1182 ssid->eap.password_len = res_len;
1183 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
1189 /* NtPasswordHash: hash:<32 hex digits> */
1190 if (os_strlen(value + 5) != 2 * 16) {
1191 wpa_printf(MSG_ERROR, "Line %d: Invalid password hash length "
1192 "(expected 32 hex digits)", line);
1196 hash = os_malloc(16);
1200 if (hexstr2bin(value + 5, hash, 16)) {
1202 wpa_printf(MSG_ERROR, "Line %d: Invalid password hash", line);
1206 wpa_hexdump_key(MSG_MSGDUMP, data->name, hash, 16);
1208 os_free(ssid->eap.password);
1209 ssid->eap.password = hash;
1210 ssid->eap.password_len = 16;
1211 ssid->eap.flags |= EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
1217 static char * wpa_config_write_password(const struct parse_data *data,
1218 struct wpa_ssid *ssid)
1222 if (ssid->eap.password == NULL)
1225 if (!(ssid->eap.flags & EAP_CONFIG_FLAGS_PASSWORD_NTHASH)) {
1226 return wpa_config_write_string(
1227 ssid->eap.password, ssid->eap.password_len);
1230 buf = os_malloc(5 + 32 + 1);
1234 os_memcpy(buf, "hash:", 5);
1235 wpa_snprintf_hex(buf + 5, 32 + 1, ssid->eap.password, 16);
1239 #endif /* IEEE8021X_EAPOL */
1242 static int wpa_config_parse_wep_key(u8 *key, size_t *len, int line,
1243 const char *value, int idx)
1245 char *buf, title[20];
1248 buf = wpa_config_parse_string(value, len);
1250 wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key %d '%s'.",
1254 if (*len > MAX_WEP_KEY_LEN) {
1255 wpa_printf(MSG_ERROR, "Line %d: Too long WEP key %d '%s'.",
1260 os_memcpy(key, buf, *len);
1262 res = os_snprintf(title, sizeof(title), "wep_key%d", idx);
1263 if (res >= 0 && (size_t) res < sizeof(title))
1264 wpa_hexdump_key(MSG_MSGDUMP, title, key, *len);
1269 static int wpa_config_parse_wep_key0(const struct parse_data *data,
1270 struct wpa_ssid *ssid, int line,
1273 return wpa_config_parse_wep_key(ssid->wep_key[0],
1274 &ssid->wep_key_len[0], line,
1279 static int wpa_config_parse_wep_key1(const struct parse_data *data,
1280 struct wpa_ssid *ssid, int line,
1283 return wpa_config_parse_wep_key(ssid->wep_key[1],
1284 &ssid->wep_key_len[1], line,
1289 static int wpa_config_parse_wep_key2(const struct parse_data *data,
1290 struct wpa_ssid *ssid, int line,
1293 return wpa_config_parse_wep_key(ssid->wep_key[2],
1294 &ssid->wep_key_len[2], line,
1299 static int wpa_config_parse_wep_key3(const struct parse_data *data,
1300 struct wpa_ssid *ssid, int line,
1303 return wpa_config_parse_wep_key(ssid->wep_key[3],
1304 &ssid->wep_key_len[3], line,
1309 #ifndef NO_CONFIG_WRITE
1310 static char * wpa_config_write_wep_key(struct wpa_ssid *ssid, int idx)
1312 if (ssid->wep_key_len[idx] == 0)
1314 return wpa_config_write_string(ssid->wep_key[idx],
1315 ssid->wep_key_len[idx]);
1319 static char * wpa_config_write_wep_key0(const struct parse_data *data,
1320 struct wpa_ssid *ssid)
1322 return wpa_config_write_wep_key(ssid, 0);
1326 static char * wpa_config_write_wep_key1(const struct parse_data *data,
1327 struct wpa_ssid *ssid)
1329 return wpa_config_write_wep_key(ssid, 1);
1333 static char * wpa_config_write_wep_key2(const struct parse_data *data,
1334 struct wpa_ssid *ssid)
1336 return wpa_config_write_wep_key(ssid, 2);
1340 static char * wpa_config_write_wep_key3(const struct parse_data *data,
1341 struct wpa_ssid *ssid)
1343 return wpa_config_write_wep_key(ssid, 3);
1345 #endif /* NO_CONFIG_WRITE */
1350 static int wpa_config_parse_p2p_client_list(const struct parse_data *data,
1351 struct wpa_ssid *ssid, int line,
1355 u8 *buf, *n, addr[ETH_ALEN];
1362 while (pos && *pos) {
1366 if (hwaddr_aton(pos, addr)) {
1367 wpa_printf(MSG_ERROR, "Line %d: Invalid "
1368 "p2p_client_list address '%s'.",
1370 /* continue anyway */
1372 n = os_realloc(buf, (count + 1) * ETH_ALEN);
1378 os_memcpy(buf + count * ETH_ALEN, addr, ETH_ALEN);
1380 wpa_hexdump(MSG_MSGDUMP, "p2p_client_list",
1384 pos = os_strchr(pos, ' ');
1387 os_free(ssid->p2p_client_list);
1388 ssid->p2p_client_list = buf;
1389 ssid->num_p2p_clients = count;
1395 #ifndef NO_CONFIG_WRITE
1396 static char * wpa_config_write_p2p_client_list(const struct parse_data *data,
1397 struct wpa_ssid *ssid)
1399 char *value, *end, *pos;
1403 if (ssid->p2p_client_list == NULL || ssid->num_p2p_clients == 0)
1406 value = os_malloc(20 * ssid->num_p2p_clients);
1410 end = value + 20 * ssid->num_p2p_clients;
1412 for (i = 0; i < ssid->num_p2p_clients; i++) {
1413 res = os_snprintf(pos, end - pos, MACSTR " ",
1414 MAC2STR(ssid->p2p_client_list +
1416 if (res < 0 || res >= end - pos) {
1428 #endif /* NO_CONFIG_WRITE */
1430 #endif /* CONFIG_P2P */
1432 /* Helper macros for network block parser */
1437 /* OFFSET: Get offset of a variable within the wpa_ssid structure */
1438 #define OFFSET(v) ((void *) &((struct wpa_ssid *) 0)->v)
1440 /* STR: Define a string variable for an ASCII string; f = field name */
1441 #ifdef NO_CONFIG_WRITE
1442 #define _STR(f) #f, wpa_config_parse_str, OFFSET(f)
1443 #define _STRe(f) #f, wpa_config_parse_str, OFFSET(eap.f)
1444 #else /* NO_CONFIG_WRITE */
1445 #define _STR(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(f)
1446 #define _STRe(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(eap.f)
1447 #endif /* NO_CONFIG_WRITE */
1448 #define STR(f) _STR(f), NULL, NULL, NULL, 0
1449 #define STRe(f) _STRe(f), NULL, NULL, NULL, 0
1450 #define STR_KEY(f) _STR(f), NULL, NULL, NULL, 1
1451 #define STR_KEYe(f) _STRe(f), NULL, NULL, NULL, 1
1453 /* STR_LEN: Define a string variable with a separate variable for storing the
1454 * data length. Unlike STR(), this can be used to store arbitrary binary data
1455 * (i.e., even nul termination character). */
1456 #define _STR_LEN(f) _STR(f), OFFSET(f ## _len)
1457 #define _STR_LENe(f) _STRe(f), OFFSET(eap.f ## _len)
1458 #define STR_LEN(f) _STR_LEN(f), NULL, NULL, 0
1459 #define STR_LENe(f) _STR_LENe(f), NULL, NULL, 0
1460 #define STR_LEN_KEY(f) _STR_LEN(f), NULL, NULL, 1
1462 /* STR_RANGE: Like STR_LEN(), but with minimum and maximum allowed length
1463 * explicitly specified. */
1464 #define _STR_RANGE(f, min, max) _STR_LEN(f), (void *) (min), (void *) (max)
1465 #define STR_RANGE(f, min, max) _STR_RANGE(f, min, max), 0
1466 #define STR_RANGE_KEY(f, min, max) _STR_RANGE(f, min, max), 1
1468 #ifdef NO_CONFIG_WRITE
1469 #define _INT(f) #f, wpa_config_parse_int, OFFSET(f), (void *) 0
1470 #define _INTe(f) #f, wpa_config_parse_int, OFFSET(eap.f), (void *) 0
1471 #else /* NO_CONFIG_WRITE */
1472 #define _INT(f) #f, wpa_config_parse_int, wpa_config_write_int, \
1473 OFFSET(f), (void *) 0
1474 #define _INTe(f) #f, wpa_config_parse_int, wpa_config_write_int, \
1475 OFFSET(eap.f), (void *) 0
1476 #endif /* NO_CONFIG_WRITE */
1478 /* INT: Define an integer variable */
1479 #define INT(f) _INT(f), NULL, NULL, 0
1480 #define INTe(f) _INTe(f), NULL, NULL, 0
1482 /* INT_RANGE: Define an integer variable with allowed value range */
1483 #define INT_RANGE(f, min, max) _INT(f), (void *) (min), (void *) (max), 0
1485 /* FUNC: Define a configuration variable that uses a custom function for
1486 * parsing and writing the value. */
1487 #ifdef NO_CONFIG_WRITE
1488 #define _FUNC(f) #f, wpa_config_parse_ ## f, NULL, NULL, NULL, NULL
1489 #else /* NO_CONFIG_WRITE */
1490 #define _FUNC(f) #f, wpa_config_parse_ ## f, wpa_config_write_ ## f, \
1491 NULL, NULL, NULL, NULL
1492 #endif /* NO_CONFIG_WRITE */
1493 #define FUNC(f) _FUNC(f), 0
1494 #define FUNC_KEY(f) _FUNC(f), 1
1497 * Table of network configuration variables. This table is used to parse each
1498 * network configuration variable, e.g., each line in wpa_supplicant.conf file
1499 * that is inside a network block.
1501 * This table is generated using the helper macros defined above and with
1502 * generous help from the C pre-processor. The field name is stored as a string
1503 * into .name and for STR and INT types, the offset of the target buffer within
1504 * struct wpa_ssid is stored in .param1. .param2 (if not NULL) is similar
1505 * offset to the field containing the length of the configuration variable.
1506 * .param3 and .param4 can be used to mark the allowed range (length for STR
1507 * and value for INT).
1509 * For each configuration line in wpa_supplicant.conf, the parser goes through
1510 * this table and select the entry that matches with the field name. The parser
1511 * function (.parser) is then called to parse the actual value of the field.
1513 * This kind of mechanism makes it easy to add new configuration parameters,
1514 * since only one line needs to be added into this table and into the
1515 * struct wpa_ssid definition if the new variable is either a string or
1516 * integer. More complex types will need to use their own parser and writer
1519 static const struct parse_data ssid_fields[] = {
1520 { STR_RANGE(ssid, 0, MAX_SSID_LEN) },
1521 { INT_RANGE(scan_ssid, 0, 1) },
1529 { FUNC(scan_freq) },
1530 { FUNC(freq_list) },
1531 #ifdef IEEE8021X_EAPOL
1533 { STR_LENe(identity) },
1534 { STR_LENe(anonymous_identity) },
1535 { FUNC_KEY(password) },
1538 { STRe(client_cert) },
1539 { STRe(private_key) },
1540 { STR_KEYe(private_key_passwd) },
1542 { STRe(subject_match) },
1543 { STRe(altsubject_match) },
1546 { STRe(client_cert2) },
1547 { STRe(private_key2) },
1548 { STR_KEYe(private_key2_passwd) },
1550 { STRe(subject_match2) },
1551 { STRe(altsubject_match2) },
1556 { STRe(engine_id) },
1559 { STRe(ca_cert_id) },
1561 { STRe(engine2_id) },
1564 { STRe(ca_cert2_id) },
1567 { INT(eapol_flags) },
1568 #endif /* IEEE8021X_EAPOL */
1569 { FUNC_KEY(wep_key0) },
1570 { FUNC_KEY(wep_key1) },
1571 { FUNC_KEY(wep_key2) },
1572 { FUNC_KEY(wep_key3) },
1573 { INT(wep_tx_keyidx) },
1575 #ifdef IEEE8021X_EAPOL
1576 { INT(eap_workaround) },
1578 { INTe(fragment_size) },
1579 #endif /* IEEE8021X_EAPOL */
1580 { INT_RANGE(mode, 0, 4) },
1581 { INT_RANGE(proactive_key_caching, 0, 1) },
1582 { INT_RANGE(disabled, 0, 2) },
1584 #ifdef CONFIG_IEEE80211W
1585 { INT_RANGE(ieee80211w, 0, 2) },
1586 #endif /* CONFIG_IEEE80211W */
1587 { INT_RANGE(peerkey, 0, 1) },
1588 { INT_RANGE(mixed_cell, 0, 1) },
1589 { INT_RANGE(frequency, 0, 10000) },
1590 { INT(wpa_ptk_rekey) },
1593 { FUNC(p2p_client_list) },
1594 #endif /* CONFIG_P2P */
1595 #ifdef CONFIG_HT_OVERRIDES
1596 { INT_RANGE(disable_ht, 0, 1) },
1597 { INT_RANGE(disable_ht40, -1, 1) },
1598 { INT_RANGE(disable_max_amsdu, -1, 1) },
1599 { INT_RANGE(ampdu_factor, -1, 3) },
1600 { INT_RANGE(ampdu_density, -1, 7) },
1602 #endif /* CONFIG_HT_OVERRIDES */
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);
1770 #ifdef CONFIG_HT_OVERRIDES
1771 os_free(ssid->ht_mcs);
1772 #endif /* CONFIG_HT_OVERRIDES */
1777 void wpa_config_free_cred(struct wpa_cred *cred)
1779 os_free(cred->realm);
1780 os_free(cred->username);
1781 os_free(cred->password);
1782 os_free(cred->ca_cert);
1783 os_free(cred->imsi);
1784 os_free(cred->milenage);
1785 os_free(cred->domain);
1791 * wpa_config_free - Free configuration data
1792 * @config: Configuration data from wpa_config_read()
1794 * This function frees all resources allocated for the configuration data by
1795 * wpa_config_read().
1797 void wpa_config_free(struct wpa_config *config)
1799 #ifndef CONFIG_NO_CONFIG_BLOBS
1800 struct wpa_config_blob *blob, *prevblob;
1801 #endif /* CONFIG_NO_CONFIG_BLOBS */
1802 struct wpa_ssid *ssid, *prev = NULL;
1803 struct wpa_cred *cred, *cprev;
1805 ssid = config->ssid;
1809 wpa_config_free_ssid(prev);
1812 cred = config->cred;
1816 wpa_config_free_cred(cprev);
1819 #ifndef CONFIG_NO_CONFIG_BLOBS
1820 blob = config->blobs;
1825 wpa_config_free_blob(prevblob);
1827 #endif /* CONFIG_NO_CONFIG_BLOBS */
1829 os_free(config->ctrl_interface);
1830 os_free(config->ctrl_interface_group);
1831 os_free(config->opensc_engine_path);
1832 os_free(config->pkcs11_engine_path);
1833 os_free(config->pkcs11_module_path);
1834 os_free(config->driver_param);
1835 os_free(config->device_name);
1836 os_free(config->manufacturer);
1837 os_free(config->model_name);
1838 os_free(config->model_number);
1839 os_free(config->serial_number);
1840 os_free(config->config_methods);
1841 os_free(config->p2p_ssid_postfix);
1842 os_free(config->pssid);
1848 * wpa_config_foreach_network - Iterate over each configured network
1849 * @config: Configuration data from wpa_config_read()
1850 * @func: Callback function to process each network
1851 * @arg: Opaque argument to pass to callback function
1853 * Iterate over the set of configured networks calling the specified
1854 * function for each item. We guard against callbacks removing the
1857 void wpa_config_foreach_network(struct wpa_config *config,
1858 void (*func)(void *, struct wpa_ssid *),
1861 struct wpa_ssid *ssid, *next;
1863 ssid = config->ssid;
1873 * wpa_config_get_network - Get configured network based on id
1874 * @config: Configuration data from wpa_config_read()
1875 * @id: Unique network id to search for
1876 * Returns: Network configuration or %NULL if not found
1878 struct wpa_ssid * wpa_config_get_network(struct wpa_config *config, int id)
1880 struct wpa_ssid *ssid;
1882 ssid = config->ssid;
1894 * wpa_config_add_network - Add a new network with empty configuration
1895 * @config: Configuration data from wpa_config_read()
1896 * Returns: The new network configuration or %NULL if operation failed
1898 struct wpa_ssid * wpa_config_add_network(struct wpa_config *config)
1901 struct wpa_ssid *ssid, *last = NULL;
1904 ssid = config->ssid;
1913 ssid = os_zalloc(sizeof(*ssid));
1920 config->ssid = ssid;
1922 wpa_config_update_prio_list(config);
1929 * wpa_config_remove_network - Remove a configured network based on id
1930 * @config: Configuration data from wpa_config_read()
1931 * @id: Unique network id to search for
1932 * Returns: 0 on success, or -1 if the network was not found
1934 int wpa_config_remove_network(struct wpa_config *config, int id)
1936 struct wpa_ssid *ssid, *prev = NULL;
1938 ssid = config->ssid;
1950 prev->next = ssid->next;
1952 config->ssid = ssid->next;
1954 wpa_config_update_prio_list(config);
1955 wpa_config_free_ssid(ssid);
1961 * wpa_config_set_network_defaults - Set network default values
1962 * @ssid: Pointer to network configuration data
1964 void wpa_config_set_network_defaults(struct wpa_ssid *ssid)
1966 ssid->proto = DEFAULT_PROTO;
1967 ssid->pairwise_cipher = DEFAULT_PAIRWISE;
1968 ssid->group_cipher = DEFAULT_GROUP;
1969 ssid->key_mgmt = DEFAULT_KEY_MGMT;
1970 #ifdef IEEE8021X_EAPOL
1971 ssid->eapol_flags = DEFAULT_EAPOL_FLAGS;
1972 ssid->eap_workaround = DEFAULT_EAP_WORKAROUND;
1973 ssid->eap.fragment_size = DEFAULT_FRAGMENT_SIZE;
1974 #endif /* IEEE8021X_EAPOL */
1975 #ifdef CONFIG_HT_OVERRIDES
1976 ssid->disable_ht = DEFAULT_DISABLE_HT;
1977 ssid->disable_ht40 = DEFAULT_DISABLE_HT40;
1978 ssid->disable_max_amsdu = DEFAULT_DISABLE_MAX_AMSDU;
1979 ssid->ampdu_factor = DEFAULT_AMPDU_FACTOR;
1980 ssid->ampdu_density = DEFAULT_AMPDU_DENSITY;
1981 #endif /* CONFIG_HT_OVERRIDES */
1986 * wpa_config_set - Set a variable in network configuration
1987 * @ssid: Pointer to network configuration data
1988 * @var: Variable name, e.g., "ssid"
1989 * @value: Variable value
1990 * @line: Line number in configuration file or 0 if not used
1991 * Returns: 0 on success, -1 on failure
1993 * This function can be used to set network configuration variables based on
1994 * both the configuration file and management interface input. The value
1995 * parameter must be in the same format as the text-based configuration file is
1996 * using. For example, strings are using double quotation marks.
1998 int wpa_config_set(struct wpa_ssid *ssid, const char *var, const char *value,
2004 if (ssid == NULL || var == NULL || value == NULL)
2007 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2008 const struct parse_data *field = &ssid_fields[i];
2009 if (os_strcmp(var, field->name) != 0)
2012 if (field->parser(field, ssid, line, value)) {
2014 wpa_printf(MSG_ERROR, "Line %d: failed to "
2015 "parse %s '%s'.", line, var, value);
2021 if (i == NUM_SSID_FIELDS) {
2023 wpa_printf(MSG_ERROR, "Line %d: unknown network field "
2024 "'%s'.", line, var);
2033 int wpa_config_set_quoted(struct wpa_ssid *ssid, const char *var,
2040 len = os_strlen(value);
2041 buf = os_malloc(len + 3);
2045 os_memcpy(buf + 1, value, len);
2047 buf[len + 2] = '\0';
2048 ret = wpa_config_set(ssid, var, buf, 0);
2055 * wpa_config_get_all - Get all options from network configuration
2056 * @ssid: Pointer to network configuration data
2057 * @get_keys: Determines if keys/passwords will be included in returned list
2058 * (if they may be exported)
2059 * Returns: %NULL terminated list of all set keys and their values in the form
2060 * of [key1, val1, key2, val2, ... , NULL]
2062 * This function can be used to get list of all configured network properties.
2063 * The caller is responsible for freeing the returned list and all its
2066 char ** wpa_config_get_all(struct wpa_ssid *ssid, int get_keys)
2068 const struct parse_data *field;
2074 get_keys = get_keys && ssid->export_keys;
2076 props = os_zalloc(sizeof(char *) * ((2 * NUM_SSID_FIELDS) + 1));
2081 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2082 field = &ssid_fields[i];
2083 if (field->key_data && !get_keys)
2085 value = field->writer(field, ssid);
2088 if (os_strlen(value) == 0) {
2093 key = os_strdup(field->name);
2099 props[fields_num * 2] = key;
2100 props[fields_num * 2 + 1] = value;
2116 #ifndef NO_CONFIG_WRITE
2118 * wpa_config_get - Get a variable in network configuration
2119 * @ssid: Pointer to network configuration data
2120 * @var: Variable name, e.g., "ssid"
2121 * Returns: Value of the variable or %NULL on failure
2123 * This function can be used to get network configuration variables. The
2124 * returned value is a copy of the configuration variable in text format, i.e,.
2125 * the same format that the text-based configuration file and wpa_config_set()
2126 * are using for the value. The caller is responsible for freeing the returned
2129 char * wpa_config_get(struct wpa_ssid *ssid, const char *var)
2133 if (ssid == NULL || var == NULL)
2136 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2137 const struct parse_data *field = &ssid_fields[i];
2138 if (os_strcmp(var, field->name) == 0)
2139 return field->writer(field, ssid);
2147 * wpa_config_get_no_key - Get a variable in network configuration (no keys)
2148 * @ssid: Pointer to network configuration data
2149 * @var: Variable name, e.g., "ssid"
2150 * Returns: Value of the variable or %NULL on failure
2152 * This function can be used to get network configuration variable like
2153 * wpa_config_get(). The only difference is that this functions does not expose
2154 * key/password material from the configuration. In case a key/password field
2155 * is requested, the returned value is an empty string or %NULL if the variable
2156 * is not set or "*" if the variable is set (regardless of its value). The
2157 * returned value is a copy of the configuration variable in text format, i.e,.
2158 * the same format that the text-based configuration file and wpa_config_set()
2159 * are using for the value. The caller is responsible for freeing the returned
2162 char * wpa_config_get_no_key(struct wpa_ssid *ssid, const char *var)
2166 if (ssid == NULL || var == NULL)
2169 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2170 const struct parse_data *field = &ssid_fields[i];
2171 if (os_strcmp(var, field->name) == 0) {
2172 char *res = field->writer(field, ssid);
2173 if (field->key_data) {
2174 if (res && res[0]) {
2175 wpa_printf(MSG_DEBUG, "Do not allow "
2176 "key_data field to be "
2179 return os_strdup("*");
2191 #endif /* NO_CONFIG_WRITE */
2195 * wpa_config_update_psk - Update WPA PSK based on passphrase and SSID
2196 * @ssid: Pointer to network configuration data
2198 * This function must be called to update WPA PSK when either SSID or the
2199 * passphrase has changed for the network configuration.
2201 void wpa_config_update_psk(struct wpa_ssid *ssid)
2203 #ifndef CONFIG_NO_PBKDF2
2204 pbkdf2_sha1(ssid->passphrase,
2205 (char *) ssid->ssid, ssid->ssid_len, 4096,
2206 ssid->psk, PMK_LEN);
2207 wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
2208 ssid->psk, PMK_LEN);
2210 #endif /* CONFIG_NO_PBKDF2 */
2214 int wpa_config_set_cred(struct wpa_cred *cred, const char *var,
2215 const char *value, int line)
2220 val = wpa_config_parse_string(value, &len);
2224 if (os_strcmp(var, "realm") == 0) {
2225 os_free(cred->realm);
2230 if (os_strcmp(var, "username") == 0) {
2231 os_free(cred->username);
2232 cred->username = val;
2236 if (os_strcmp(var, "password") == 0) {
2237 os_free(cred->password);
2238 cred->password = val;
2242 if (os_strcmp(var, "ca_cert") == 0) {
2243 os_free(cred->ca_cert);
2244 cred->ca_cert = val;
2248 if (os_strcmp(var, "imsi") == 0) {
2249 os_free(cred->imsi);
2254 if (os_strcmp(var, "milenage") == 0) {
2255 os_free(cred->milenage);
2256 cred->milenage = val;
2260 if (os_strcmp(var, "domain") == 0) {
2261 os_free(cred->domain);
2267 wpa_printf(MSG_ERROR, "Line %d: unknown cred field '%s'.",
2275 struct wpa_cred * wpa_config_get_cred(struct wpa_config *config, int id)
2277 struct wpa_cred *cred;
2279 cred = config->cred;
2290 struct wpa_cred * wpa_config_add_cred(struct wpa_config *config)
2293 struct wpa_cred *cred, *last = NULL;
2296 cred = config->cred;
2305 cred = os_zalloc(sizeof(*cred));
2312 config->cred = cred;
2318 int wpa_config_remove_cred(struct wpa_config *config, int id)
2320 struct wpa_cred *cred, *prev = NULL;
2322 cred = config->cred;
2334 prev->next = cred->next;
2336 config->cred = cred->next;
2338 wpa_config_free_cred(cred);
2343 #ifndef CONFIG_NO_CONFIG_BLOBS
2345 * wpa_config_get_blob - Get a named configuration blob
2346 * @config: Configuration data from wpa_config_read()
2347 * @name: Name of the blob
2348 * Returns: Pointer to blob data or %NULL if not found
2350 const struct wpa_config_blob * wpa_config_get_blob(struct wpa_config *config,
2353 struct wpa_config_blob *blob = config->blobs;
2356 if (os_strcmp(blob->name, name) == 0)
2365 * wpa_config_set_blob - Set or add a named configuration blob
2366 * @config: Configuration data from wpa_config_read()
2367 * @blob: New value for the blob
2369 * Adds a new configuration blob or replaces the current value of an existing
2372 void wpa_config_set_blob(struct wpa_config *config,
2373 struct wpa_config_blob *blob)
2375 wpa_config_remove_blob(config, blob->name);
2376 blob->next = config->blobs;
2377 config->blobs = blob;
2382 * wpa_config_free_blob - Free blob data
2383 * @blob: Pointer to blob to be freed
2385 void wpa_config_free_blob(struct wpa_config_blob *blob)
2388 os_free(blob->name);
2389 os_free(blob->data);
2396 * wpa_config_remove_blob - Remove a named configuration blob
2397 * @config: Configuration data from wpa_config_read()
2398 * @name: Name of the blob to remove
2399 * Returns: 0 if blob was removed or -1 if blob was not found
2401 int wpa_config_remove_blob(struct wpa_config *config, const char *name)
2403 struct wpa_config_blob *pos = config->blobs, *prev = NULL;
2406 if (os_strcmp(pos->name, name) == 0) {
2408 prev->next = pos->next;
2410 config->blobs = pos->next;
2411 wpa_config_free_blob(pos);
2420 #endif /* CONFIG_NO_CONFIG_BLOBS */
2424 * wpa_config_alloc_empty - Allocate an empty configuration
2425 * @ctrl_interface: Control interface parameters, e.g., path to UNIX domain
2427 * @driver_param: Driver parameters
2428 * Returns: Pointer to allocated configuration data or %NULL on failure
2430 struct wpa_config * wpa_config_alloc_empty(const char *ctrl_interface,
2431 const char *driver_param)
2433 struct wpa_config *config;
2435 config = os_zalloc(sizeof(*config));
2438 config->eapol_version = DEFAULT_EAPOL_VERSION;
2439 config->ap_scan = DEFAULT_AP_SCAN;
2440 config->fast_reauth = DEFAULT_FAST_REAUTH;
2441 config->p2p_go_intent = DEFAULT_P2P_GO_INTENT;
2442 config->p2p_intra_bss = DEFAULT_P2P_INTRA_BSS;
2443 config->bss_max_count = DEFAULT_BSS_MAX_COUNT;
2444 config->bss_expiration_age = DEFAULT_BSS_EXPIRATION_AGE;
2445 config->bss_expiration_scan_count = DEFAULT_BSS_EXPIRATION_SCAN_COUNT;
2446 config->max_num_sta = DEFAULT_MAX_NUM_STA;
2447 config->access_network_type = DEFAULT_ACCESS_NETWORK_TYPE;
2450 config->ctrl_interface = os_strdup(ctrl_interface);
2452 config->driver_param = os_strdup(driver_param);
2458 #ifndef CONFIG_NO_STDOUT_DEBUG
2460 * wpa_config_debug_dump_networks - Debug dump of configured networks
2461 * @config: Configuration data from wpa_config_read()
2463 void wpa_config_debug_dump_networks(struct wpa_config *config)
2466 struct wpa_ssid *ssid;
2468 for (prio = 0; prio < config->num_prio; prio++) {
2469 ssid = config->pssid[prio];
2470 wpa_printf(MSG_DEBUG, "Priority group %d",
2473 wpa_printf(MSG_DEBUG, " id=%d ssid='%s'",
2475 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
2480 #endif /* CONFIG_NO_STDOUT_DEBUG */
2483 struct global_parse_data {
2485 int (*parser)(const struct global_parse_data *data,
2486 struct wpa_config *config, int line, const char *value);
2487 void *param1, *param2, *param3;
2488 unsigned int changed_flag;
2492 static int wpa_global_config_parse_int(const struct global_parse_data *data,
2493 struct wpa_config *config, int line,
2497 dst = (int *) (((u8 *) config) + (long) data->param1);
2499 wpa_printf(MSG_DEBUG, "%s=%d", data->name, *dst);
2501 if (data->param2 && *dst < (long) data->param2) {
2502 wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
2503 "min_value=%ld)", line, data->name, *dst,
2504 (long) data->param2);
2505 *dst = (long) data->param2;
2509 if (data->param3 && *dst > (long) data->param3) {
2510 wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
2511 "max_value=%ld)", line, data->name, *dst,
2512 (long) data->param3);
2513 *dst = (long) data->param3;
2521 static int wpa_global_config_parse_str(const struct global_parse_data *data,
2522 struct wpa_config *config, int line,
2528 len = os_strlen(pos);
2529 if (data->param2 && len < (size_t) data->param2) {
2530 wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
2531 "min_len=%ld)", line, data->name,
2532 (unsigned long) len, (long) data->param2);
2536 if (data->param3 && len > (size_t) data->param3) {
2537 wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
2538 "max_len=%ld)", line, data->name,
2539 (unsigned long) len, (long) data->param3);
2543 tmp = os_strdup(pos);
2547 dst = (char **) (((u8 *) config) + (long) data->param1);
2550 wpa_printf(MSG_DEBUG, "%s='%s'", data->name, *dst);
2556 static int wpa_config_process_country(const struct global_parse_data *data,
2557 struct wpa_config *config, int line,
2560 if (!pos[0] || !pos[1]) {
2561 wpa_printf(MSG_DEBUG, "Invalid country set");
2564 config->country[0] = pos[0];
2565 config->country[1] = pos[1];
2566 wpa_printf(MSG_DEBUG, "country='%c%c'",
2567 config->country[0], config->country[1]);
2572 static int wpa_config_process_load_dynamic_eap(
2573 const struct global_parse_data *data, struct wpa_config *config,
2574 int line, const char *so)
2577 wpa_printf(MSG_DEBUG, "load_dynamic_eap=%s", so);
2578 ret = eap_peer_method_load(so);
2580 wpa_printf(MSG_DEBUG, "This EAP type was already loaded - not "
2583 wpa_printf(MSG_ERROR, "Line %d: Failed to load dynamic EAP "
2584 "method '%s'.", line, so);
2594 static int wpa_config_process_uuid(const struct global_parse_data *data,
2595 struct wpa_config *config, int line,
2599 if (uuid_str2bin(pos, config->uuid)) {
2600 wpa_printf(MSG_ERROR, "Line %d: invalid UUID", line);
2603 uuid_bin2str(config->uuid, buf, sizeof(buf));
2604 wpa_printf(MSG_DEBUG, "uuid=%s", buf);
2609 static int wpa_config_process_device_type(
2610 const struct global_parse_data *data,
2611 struct wpa_config *config, int line, const char *pos)
2613 return wps_dev_type_str2bin(pos, config->device_type);
2617 static int wpa_config_process_os_version(const struct global_parse_data *data,
2618 struct wpa_config *config, int line,
2621 if (hexstr2bin(pos, config->os_version, 4)) {
2622 wpa_printf(MSG_ERROR, "Line %d: invalid os_version", line);
2625 wpa_printf(MSG_DEBUG, "os_version=%08x",
2626 WPA_GET_BE32(config->os_version));
2630 #endif /* CONFIG_WPS */
2633 static int wpa_config_process_sec_device_type(
2634 const struct global_parse_data *data,
2635 struct wpa_config *config, int line, const char *pos)
2639 if (config->num_sec_device_types >= MAX_SEC_DEVICE_TYPES) {
2640 wpa_printf(MSG_ERROR, "Line %d: too many sec_device_type "
2645 idx = config->num_sec_device_types;
2647 if (wps_dev_type_str2bin(pos, config->sec_device_type[idx]))
2650 config->num_sec_device_types++;
2653 #endif /* CONFIG_P2P */
2656 static int wpa_config_process_hessid(
2657 const struct global_parse_data *data,
2658 struct wpa_config *config, int line, const char *pos)
2660 if (hwaddr_aton2(pos, config->hessid) < 0) {
2661 wpa_printf(MSG_ERROR, "Line %d: Invalid hessid '%s'",
2673 /* OFFSET: Get offset of a variable within the wpa_config structure */
2674 #define OFFSET(v) ((void *) &((struct wpa_config *) 0)->v)
2676 #define FUNC(f) #f, wpa_config_process_ ## f, OFFSET(f), NULL, NULL
2677 #define FUNC_NO_VAR(f) #f, wpa_config_process_ ## f, NULL, NULL, NULL
2678 #define _INT(f) #f, wpa_global_config_parse_int, OFFSET(f)
2679 #define INT(f) _INT(f), NULL, NULL
2680 #define INT_RANGE(f, min, max) _INT(f), (void *) min, (void *) max
2681 #define _STR(f) #f, wpa_global_config_parse_str, OFFSET(f)
2682 #define STR(f) _STR(f), NULL, NULL
2683 #define STR_RANGE(f, min, max) _STR(f), (void *) min, (void *) max
2685 static const struct global_parse_data global_fields[] = {
2686 #ifdef CONFIG_CTRL_IFACE
2687 { STR(ctrl_interface), 0 },
2688 { STR(ctrl_interface_group), 0 } /* deprecated */,
2689 #endif /* CONFIG_CTRL_IFACE */
2690 { INT_RANGE(eapol_version, 1, 2), 0 },
2691 { INT(ap_scan), 0 },
2692 { INT(fast_reauth), 0 },
2693 { STR(opensc_engine_path), 0 },
2694 { STR(pkcs11_engine_path), 0 },
2695 { STR(pkcs11_module_path), 0 },
2696 { STR(driver_param), 0 },
2697 { INT(dot11RSNAConfigPMKLifetime), 0 },
2698 { INT(dot11RSNAConfigPMKReauthThreshold), 0 },
2699 { INT(dot11RSNAConfigSATimeout), 0 },
2700 #ifndef CONFIG_NO_CONFIG_WRITE
2701 { INT(update_config), 0 },
2702 #endif /* CONFIG_NO_CONFIG_WRITE */
2703 { FUNC_NO_VAR(load_dynamic_eap), 0 },
2705 { FUNC(uuid), CFG_CHANGED_UUID },
2706 { STR_RANGE(device_name, 0, 32), CFG_CHANGED_DEVICE_NAME },
2707 { STR_RANGE(manufacturer, 0, 64), CFG_CHANGED_WPS_STRING },
2708 { STR_RANGE(model_name, 0, 32), CFG_CHANGED_WPS_STRING },
2709 { STR_RANGE(model_number, 0, 32), CFG_CHANGED_WPS_STRING },
2710 { STR_RANGE(serial_number, 0, 32), CFG_CHANGED_WPS_STRING },
2711 { FUNC(device_type), CFG_CHANGED_DEVICE_TYPE },
2712 { FUNC(os_version), CFG_CHANGED_OS_VERSION },
2713 { STR(config_methods), CFG_CHANGED_CONFIG_METHODS },
2714 { INT_RANGE(wps_cred_processing, 0, 2), 0 },
2715 #endif /* CONFIG_WPS */
2717 { FUNC(sec_device_type), CFG_CHANGED_SEC_DEVICE_TYPE },
2718 { INT(p2p_listen_reg_class), 0 },
2719 { INT(p2p_listen_channel), 0 },
2720 { INT(p2p_oper_reg_class), 0 },
2721 { INT(p2p_oper_channel), 0 },
2722 { INT_RANGE(p2p_go_intent, 0, 15), 0 },
2723 { STR(p2p_ssid_postfix), CFG_CHANGED_P2P_SSID_POSTFIX },
2724 { INT_RANGE(persistent_reconnect, 0, 1), 0 },
2725 { INT_RANGE(p2p_intra_bss, 0, 1), CFG_CHANGED_P2P_INTRA_BSS },
2726 { INT(p2p_group_idle), 0 },
2727 #endif /* CONFIG_P2P */
2728 { FUNC(country), CFG_CHANGED_COUNTRY },
2729 { INT(bss_max_count), 0 },
2730 { INT(bss_expiration_age), 0 },
2731 { INT(bss_expiration_scan_count), 0 },
2732 { INT_RANGE(filter_ssids, 0, 1), 0 },
2733 { INT(max_num_sta), 0 },
2734 { INT_RANGE(disassoc_low_ack, 0, 1), 0 },
2735 { INT_RANGE(interworking, 0, 1), 0 },
2736 { FUNC(hessid), 0 },
2737 { INT_RANGE(access_network_type, 0, 15), 0 }
2747 #define NUM_GLOBAL_FIELDS (sizeof(global_fields) / sizeof(global_fields[0]))
2750 int wpa_config_process_global(struct wpa_config *config, char *pos, int line)
2755 for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
2756 const struct global_parse_data *field = &global_fields[i];
2757 size_t flen = os_strlen(field->name);
2758 if (os_strncmp(pos, field->name, flen) != 0 ||
2762 if (field->parser(field, config, line, pos + flen + 1)) {
2763 wpa_printf(MSG_ERROR, "Line %d: failed to "
2764 "parse '%s'.", line, pos);
2767 config->changed_parameters |= field->changed_flag;
2770 if (i == NUM_GLOBAL_FIELDS) {
2773 wpa_printf(MSG_ERROR, "Line %d: unknown global field '%s'.",