2 * WPA Supplicant / Configuration parser and common functions
3 * Copyright (c) 2003-2012, 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"
20 #if !defined(CONFIG_CTRL_IFACE) && defined(CONFIG_NO_CONFIG_WRITE)
21 #define NO_CONFIG_WRITE
25 * Structure for network configuration parsing. This data is used to implement
26 * a generic parser for each network block variable. The table of configuration
27 * variables is defined below in this file (ssid_fields[]).
30 /* Configuration variable name */
33 /* Parser function for this variable */
34 int (*parser)(const struct parse_data *data, struct wpa_ssid *ssid,
35 int line, const char *value);
37 #ifndef NO_CONFIG_WRITE
38 /* Writer function (i.e., to get the variable in text format from
39 * internal presentation). */
40 char * (*writer)(const struct parse_data *data, struct wpa_ssid *ssid);
41 #endif /* NO_CONFIG_WRITE */
43 /* Variable specific parameters for the parser. */
44 void *param1, *param2, *param3, *param4;
46 /* 0 = this variable can be included in debug output and ctrl_iface
47 * 1 = this variable contains key/private data and it must not be
48 * included in debug output unless explicitly requested. In
49 * addition, this variable will not be readable through the
56 static int wpa_config_parse_str(const struct parse_data *data,
57 struct wpa_ssid *ssid,
58 int line, const char *value)
60 size_t res_len, *dst_len;
63 if (os_strcmp(value, "NULL") == 0) {
64 wpa_printf(MSG_DEBUG, "Unset configuration string '%s'",
71 tmp = wpa_config_parse_string(value, &res_len);
73 wpa_printf(MSG_ERROR, "Line %d: failed to parse %s '%s'.",
75 data->key_data ? "[KEY DATA REMOVED]" : value);
80 wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
83 wpa_hexdump_ascii(MSG_MSGDUMP, data->name,
87 if (data->param3 && res_len < (size_t) data->param3) {
88 wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
89 "min_len=%ld)", line, data->name,
90 (unsigned long) res_len, (long) data->param3);
95 if (data->param4 && res_len > (size_t) data->param4) {
96 wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
97 "max_len=%ld)", line, data->name,
98 (unsigned long) res_len, (long) data->param4);
104 dst = (char **) (((u8 *) ssid) + (long) data->param1);
105 dst_len = (size_t *) (((u8 *) ssid) + (long) data->param2);
115 #ifndef NO_CONFIG_WRITE
116 static char * wpa_config_write_string_ascii(const u8 *value, size_t len)
120 buf = os_malloc(len + 3);
124 os_memcpy(buf + 1, value, len);
132 static char * wpa_config_write_string_hex(const u8 *value, size_t len)
136 buf = os_zalloc(2 * len + 1);
139 wpa_snprintf_hex(buf, 2 * len + 1, value, len);
145 static char * wpa_config_write_string(const u8 *value, size_t len)
150 if (is_hex(value, len))
151 return wpa_config_write_string_hex(value, len);
153 return wpa_config_write_string_ascii(value, len);
157 static char * wpa_config_write_str(const struct parse_data *data,
158 struct wpa_ssid *ssid)
163 src = (char **) (((u8 *) ssid) + (long) data->param1);
168 len = *((size_t *) (((u8 *) ssid) + (long) data->param2));
170 len = os_strlen(*src);
172 return wpa_config_write_string((const u8 *) *src, len);
174 #endif /* NO_CONFIG_WRITE */
177 static int wpa_config_parse_int(const struct parse_data *data,
178 struct wpa_ssid *ssid,
179 int line, const char *value)
184 dst = (int *) (((u8 *) ssid) + (long) data->param1);
185 val = strtol(value, &end, 0);
187 wpa_printf(MSG_ERROR, "Line %d: invalid number \"%s\"",
192 wpa_printf(MSG_MSGDUMP, "%s=%d (0x%x)", data->name, *dst, *dst);
194 if (data->param3 && *dst < (long) data->param3) {
195 wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
196 "min_value=%ld)", line, data->name, *dst,
197 (long) data->param3);
198 *dst = (long) data->param3;
202 if (data->param4 && *dst > (long) data->param4) {
203 wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
204 "max_value=%ld)", line, data->name, *dst,
205 (long) data->param4);
206 *dst = (long) data->param4;
214 #ifndef NO_CONFIG_WRITE
215 static char * wpa_config_write_int(const struct parse_data *data,
216 struct wpa_ssid *ssid)
221 src = (int *) (((u8 *) ssid) + (long) data->param1);
223 value = os_malloc(20);
226 res = os_snprintf(value, 20, "%d", *src);
227 if (res < 0 || res >= 20) {
231 value[20 - 1] = '\0';
234 #endif /* NO_CONFIG_WRITE */
237 static int wpa_config_parse_bssid(const struct parse_data *data,
238 struct wpa_ssid *ssid, int line,
241 if (value[0] == '\0' || os_strcmp(value, "\"\"") == 0 ||
242 os_strcmp(value, "any") == 0) {
244 wpa_printf(MSG_MSGDUMP, "BSSID any");
247 if (hwaddr_aton(value, ssid->bssid)) {
248 wpa_printf(MSG_ERROR, "Line %d: Invalid BSSID '%s'.",
253 wpa_hexdump(MSG_MSGDUMP, "BSSID", ssid->bssid, ETH_ALEN);
258 #ifndef NO_CONFIG_WRITE
259 static char * wpa_config_write_bssid(const struct parse_data *data,
260 struct wpa_ssid *ssid)
265 if (!ssid->bssid_set)
268 value = os_malloc(20);
271 res = os_snprintf(value, 20, MACSTR, MAC2STR(ssid->bssid));
272 if (res < 0 || res >= 20) {
276 value[20 - 1] = '\0';
279 #endif /* NO_CONFIG_WRITE */
282 static int wpa_config_parse_psk(const struct parse_data *data,
283 struct wpa_ssid *ssid, int line,
286 #ifdef CONFIG_EXT_PASSWORD
287 if (os_strncmp(value, "ext:", 4) == 0) {
288 os_free(ssid->passphrase);
289 ssid->passphrase = NULL;
291 os_free(ssid->ext_psk);
292 ssid->ext_psk = os_strdup(value + 4);
293 if (ssid->ext_psk == NULL)
295 wpa_printf(MSG_DEBUG, "PSK: External password '%s'",
299 #endif /* CONFIG_EXT_PASSWORD */
302 #ifndef CONFIG_NO_PBKDF2
307 pos = os_strrchr(value, '"');
311 len = os_strlen(value);
312 if (len < 8 || len > 63) {
313 wpa_printf(MSG_ERROR, "Line %d: Invalid passphrase "
314 "length %lu (expected: 8..63) '%s'.",
315 line, (unsigned long) len, value);
318 wpa_hexdump_ascii_key(MSG_MSGDUMP, "PSK (ASCII passphrase)",
320 if (ssid->passphrase && os_strlen(ssid->passphrase) == len &&
321 os_memcmp(ssid->passphrase, value, len) == 0)
324 os_free(ssid->passphrase);
325 ssid->passphrase = dup_binstr(value, len);
326 if (ssid->passphrase == NULL)
329 #else /* CONFIG_NO_PBKDF2 */
330 wpa_printf(MSG_ERROR, "Line %d: ASCII passphrase not "
333 #endif /* CONFIG_NO_PBKDF2 */
336 if (hexstr2bin(value, ssid->psk, PMK_LEN) ||
337 value[PMK_LEN * 2] != '\0') {
338 wpa_printf(MSG_ERROR, "Line %d: Invalid PSK '%s'.",
343 os_free(ssid->passphrase);
344 ssid->passphrase = NULL;
347 wpa_hexdump_key(MSG_MSGDUMP, "PSK", ssid->psk, PMK_LEN);
352 #ifndef NO_CONFIG_WRITE
353 static char * wpa_config_write_psk(const struct parse_data *data,
354 struct wpa_ssid *ssid)
356 #ifdef CONFIG_EXT_PASSWORD
358 size_t len = 4 + os_strlen(ssid->ext_psk) + 1;
359 char *buf = os_malloc(len);
362 os_snprintf(buf, len, "ext:%s", ssid->ext_psk);
365 #endif /* CONFIG_EXT_PASSWORD */
367 if (ssid->passphrase)
368 return wpa_config_write_string_ascii(
369 (const u8 *) ssid->passphrase,
370 os_strlen(ssid->passphrase));
373 return wpa_config_write_string_hex(ssid->psk, PMK_LEN);
377 #endif /* NO_CONFIG_WRITE */
380 static int wpa_config_parse_proto(const struct parse_data *data,
381 struct wpa_ssid *ssid, int line,
384 int val = 0, last, errors = 0;
385 char *start, *end, *buf;
387 buf = os_strdup(value);
392 while (*start != '\0') {
393 while (*start == ' ' || *start == '\t')
398 while (*end != ' ' && *end != '\t' && *end != '\0')
402 if (os_strcmp(start, "WPA") == 0)
403 val |= WPA_PROTO_WPA;
404 else if (os_strcmp(start, "RSN") == 0 ||
405 os_strcmp(start, "WPA2") == 0)
406 val |= WPA_PROTO_RSN;
408 wpa_printf(MSG_ERROR, "Line %d: invalid proto '%s'",
420 wpa_printf(MSG_ERROR,
421 "Line %d: no proto values configured.", line);
425 wpa_printf(MSG_MSGDUMP, "proto: 0x%x", val);
427 return errors ? -1 : 0;
431 #ifndef NO_CONFIG_WRITE
432 static char * wpa_config_write_proto(const struct parse_data *data,
433 struct wpa_ssid *ssid)
436 char *buf, *pos, *end;
438 pos = buf = os_zalloc(10);
443 if (ssid->proto & WPA_PROTO_WPA) {
444 ret = os_snprintf(pos, end - pos, "%sWPA", first ? "" : " ");
445 if (ret < 0 || ret >= end - pos)
451 if (ssid->proto & WPA_PROTO_RSN) {
452 ret = os_snprintf(pos, end - pos, "%sRSN", first ? "" : " ");
453 if (ret < 0 || ret >= end - pos)
461 #endif /* NO_CONFIG_WRITE */
464 static int wpa_config_parse_key_mgmt(const struct parse_data *data,
465 struct wpa_ssid *ssid, int line,
468 int val = 0, last, errors = 0;
469 char *start, *end, *buf;
471 buf = os_strdup(value);
476 while (*start != '\0') {
477 while (*start == ' ' || *start == '\t')
482 while (*end != ' ' && *end != '\t' && *end != '\0')
486 if (os_strcmp(start, "WPA-PSK") == 0)
487 val |= WPA_KEY_MGMT_PSK;
488 else if (os_strcmp(start, "WPA-EAP") == 0)
489 val |= WPA_KEY_MGMT_IEEE8021X;
490 else if (os_strcmp(start, "IEEE8021X") == 0)
491 val |= WPA_KEY_MGMT_IEEE8021X_NO_WPA;
492 else if (os_strcmp(start, "NONE") == 0)
493 val |= WPA_KEY_MGMT_NONE;
494 else if (os_strcmp(start, "WPA-NONE") == 0)
495 val |= WPA_KEY_MGMT_WPA_NONE;
496 #ifdef CONFIG_IEEE80211R
497 else if (os_strcmp(start, "FT-PSK") == 0)
498 val |= WPA_KEY_MGMT_FT_PSK;
499 else if (os_strcmp(start, "FT-EAP") == 0)
500 val |= WPA_KEY_MGMT_FT_IEEE8021X;
501 #endif /* CONFIG_IEEE80211R */
502 #ifdef CONFIG_IEEE80211W
503 else if (os_strcmp(start, "WPA-PSK-SHA256") == 0)
504 val |= WPA_KEY_MGMT_PSK_SHA256;
505 else if (os_strcmp(start, "WPA-EAP-SHA256") == 0)
506 val |= WPA_KEY_MGMT_IEEE8021X_SHA256;
507 #endif /* CONFIG_IEEE80211W */
509 else if (os_strcmp(start, "WPS") == 0)
510 val |= WPA_KEY_MGMT_WPS;
511 #endif /* CONFIG_WPS */
513 else if (os_strcmp(start, "SAE") == 0)
514 val |= WPA_KEY_MGMT_SAE;
515 else if (os_strcmp(start, "FT-SAE") == 0)
516 val |= WPA_KEY_MGMT_FT_SAE;
517 #endif /* CONFIG_SAE */
519 wpa_printf(MSG_ERROR, "Line %d: invalid key_mgmt '%s'",
531 wpa_printf(MSG_ERROR,
532 "Line %d: no key_mgmt values configured.", line);
536 wpa_printf(MSG_MSGDUMP, "key_mgmt: 0x%x", val);
537 ssid->key_mgmt = val;
538 return errors ? -1 : 0;
542 #ifndef NO_CONFIG_WRITE
543 static char * wpa_config_write_key_mgmt(const struct parse_data *data,
544 struct wpa_ssid *ssid)
546 char *buf, *pos, *end;
549 pos = buf = os_zalloc(50);
554 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
555 ret = os_snprintf(pos, end - pos, "%sWPA-PSK",
556 pos == buf ? "" : " ");
557 if (ret < 0 || ret >= end - pos) {
564 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
565 ret = os_snprintf(pos, end - pos, "%sWPA-EAP",
566 pos == buf ? "" : " ");
567 if (ret < 0 || ret >= end - pos) {
574 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
575 ret = os_snprintf(pos, end - pos, "%sIEEE8021X",
576 pos == buf ? "" : " ");
577 if (ret < 0 || ret >= end - pos) {
584 if (ssid->key_mgmt & WPA_KEY_MGMT_NONE) {
585 ret = os_snprintf(pos, end - pos, "%sNONE",
586 pos == buf ? "" : " ");
587 if (ret < 0 || ret >= end - pos) {
594 if (ssid->key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
595 ret = os_snprintf(pos, end - pos, "%sWPA-NONE",
596 pos == buf ? "" : " ");
597 if (ret < 0 || ret >= end - pos) {
604 #ifdef CONFIG_IEEE80211R
605 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_PSK)
606 pos += os_snprintf(pos, end - pos, "%sFT-PSK",
607 pos == buf ? "" : " ");
609 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X)
610 pos += os_snprintf(pos, end - pos, "%sFT-EAP",
611 pos == buf ? "" : " ");
612 #endif /* CONFIG_IEEE80211R */
614 #ifdef CONFIG_IEEE80211W
615 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK_SHA256)
616 pos += os_snprintf(pos, end - pos, "%sWPA-PSK-SHA256",
617 pos == buf ? "" : " ");
619 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256)
620 pos += os_snprintf(pos, end - pos, "%sWPA-EAP-SHA256",
621 pos == buf ? "" : " ");
622 #endif /* CONFIG_IEEE80211W */
625 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
626 pos += os_snprintf(pos, end - pos, "%sWPS",
627 pos == buf ? "" : " ");
628 #endif /* CONFIG_WPS */
632 #endif /* NO_CONFIG_WRITE */
635 static int wpa_config_parse_cipher(int line, const char *value)
637 int val = wpa_parse_cipher(value);
639 wpa_printf(MSG_ERROR, "Line %d: invalid cipher '%s'.",
644 wpa_printf(MSG_ERROR, "Line %d: no cipher values configured.",
652 #ifndef NO_CONFIG_WRITE
653 static char * wpa_config_write_cipher(int cipher)
655 char *buf = os_zalloc(50);
659 if (wpa_write_ciphers(buf, buf + 50, cipher, " ") < 0) {
666 #endif /* NO_CONFIG_WRITE */
669 static int wpa_config_parse_pairwise(const struct parse_data *data,
670 struct wpa_ssid *ssid, int line,
674 val = wpa_config_parse_cipher(line, value);
677 if (val & ~WPA_ALLOWED_PAIRWISE_CIPHERS) {
678 wpa_printf(MSG_ERROR, "Line %d: not allowed pairwise cipher "
679 "(0x%x).", line, val);
683 wpa_printf(MSG_MSGDUMP, "pairwise: 0x%x", val);
684 ssid->pairwise_cipher = val;
689 #ifndef NO_CONFIG_WRITE
690 static char * wpa_config_write_pairwise(const struct parse_data *data,
691 struct wpa_ssid *ssid)
693 return wpa_config_write_cipher(ssid->pairwise_cipher);
695 #endif /* NO_CONFIG_WRITE */
698 static int wpa_config_parse_group(const struct parse_data *data,
699 struct wpa_ssid *ssid, int line,
703 val = wpa_config_parse_cipher(line, value);
706 if (val & ~WPA_ALLOWED_GROUP_CIPHERS) {
707 wpa_printf(MSG_ERROR, "Line %d: not allowed group cipher "
708 "(0x%x).", line, val);
712 wpa_printf(MSG_MSGDUMP, "group: 0x%x", val);
713 ssid->group_cipher = val;
718 #ifndef NO_CONFIG_WRITE
719 static char * wpa_config_write_group(const struct parse_data *data,
720 struct wpa_ssid *ssid)
722 return wpa_config_write_cipher(ssid->group_cipher);
724 #endif /* NO_CONFIG_WRITE */
727 static int wpa_config_parse_auth_alg(const struct parse_data *data,
728 struct wpa_ssid *ssid, int line,
731 int val = 0, last, errors = 0;
732 char *start, *end, *buf;
734 buf = os_strdup(value);
739 while (*start != '\0') {
740 while (*start == ' ' || *start == '\t')
745 while (*end != ' ' && *end != '\t' && *end != '\0')
749 if (os_strcmp(start, "OPEN") == 0)
750 val |= WPA_AUTH_ALG_OPEN;
751 else if (os_strcmp(start, "SHARED") == 0)
752 val |= WPA_AUTH_ALG_SHARED;
753 else if (os_strcmp(start, "LEAP") == 0)
754 val |= WPA_AUTH_ALG_LEAP;
756 wpa_printf(MSG_ERROR, "Line %d: invalid auth_alg '%s'",
768 wpa_printf(MSG_ERROR,
769 "Line %d: no auth_alg values configured.", line);
773 wpa_printf(MSG_MSGDUMP, "auth_alg: 0x%x", val);
774 ssid->auth_alg = val;
775 return errors ? -1 : 0;
779 #ifndef NO_CONFIG_WRITE
780 static char * wpa_config_write_auth_alg(const struct parse_data *data,
781 struct wpa_ssid *ssid)
783 char *buf, *pos, *end;
786 pos = buf = os_zalloc(30);
791 if (ssid->auth_alg & WPA_AUTH_ALG_OPEN) {
792 ret = os_snprintf(pos, end - pos, "%sOPEN",
793 pos == buf ? "" : " ");
794 if (ret < 0 || ret >= end - pos) {
801 if (ssid->auth_alg & WPA_AUTH_ALG_SHARED) {
802 ret = os_snprintf(pos, end - pos, "%sSHARED",
803 pos == buf ? "" : " ");
804 if (ret < 0 || ret >= end - pos) {
811 if (ssid->auth_alg & WPA_AUTH_ALG_LEAP) {
812 ret = os_snprintf(pos, end - pos, "%sLEAP",
813 pos == buf ? "" : " ");
814 if (ret < 0 || ret >= end - pos) {
823 #endif /* NO_CONFIG_WRITE */
826 static int * wpa_config_parse_int_array(const char *value)
834 freqs = os_calloc(len + 1, sizeof(int));
845 n = os_realloc_array(freqs, len * 2 + 1, sizeof(int));
850 for (i = len; i <= len * 2; i++)
856 freqs[used] = atoi(pos);
857 if (freqs[used] == 0)
860 pos = os_strchr(pos + 1, ' ');
867 static int wpa_config_parse_scan_freq(const struct parse_data *data,
868 struct wpa_ssid *ssid, int line,
873 freqs = wpa_config_parse_int_array(value);
876 os_free(ssid->scan_freq);
877 ssid->scan_freq = freqs;
883 static int wpa_config_parse_freq_list(const struct parse_data *data,
884 struct wpa_ssid *ssid, int line,
889 freqs = wpa_config_parse_int_array(value);
892 os_free(ssid->freq_list);
893 ssid->freq_list = freqs;
899 #ifndef NO_CONFIG_WRITE
900 static char * wpa_config_write_freqs(const struct parse_data *data,
903 char *buf, *pos, *end;
911 for (i = 0; freqs[i]; i++)
914 pos = buf = os_zalloc(10 * count + 1);
917 end = buf + 10 * count + 1;
919 for (i = 0; freqs[i]; i++) {
920 ret = os_snprintf(pos, end - pos, "%s%u",
921 i == 0 ? "" : " ", freqs[i]);
922 if (ret < 0 || ret >= end - pos) {
933 static char * wpa_config_write_scan_freq(const struct parse_data *data,
934 struct wpa_ssid *ssid)
936 return wpa_config_write_freqs(data, ssid->scan_freq);
940 static char * wpa_config_write_freq_list(const struct parse_data *data,
941 struct wpa_ssid *ssid)
943 return wpa_config_write_freqs(data, ssid->freq_list);
945 #endif /* NO_CONFIG_WRITE */
948 #ifdef IEEE8021X_EAPOL
949 static int wpa_config_parse_eap(const struct parse_data *data,
950 struct wpa_ssid *ssid, int line,
953 int last, errors = 0;
954 char *start, *end, *buf;
955 struct eap_method_type *methods = NULL, *tmp;
956 size_t num_methods = 0;
958 buf = os_strdup(value);
963 while (*start != '\0') {
964 while (*start == ' ' || *start == '\t')
969 while (*end != ' ' && *end != '\t' && *end != '\0')
974 methods = os_realloc_array(methods, num_methods + 1,
976 if (methods == NULL) {
981 methods[num_methods].method = eap_peer_get_type(
982 start, &methods[num_methods].vendor);
983 if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
984 methods[num_methods].method == EAP_TYPE_NONE) {
985 wpa_printf(MSG_ERROR, "Line %d: unknown EAP method "
986 "'%s'", line, start);
987 wpa_printf(MSG_ERROR, "You may need to add support for"
988 " this EAP method during wpa_supplicant\n"
989 "build time configuration.\n"
990 "See README for more information.");
992 } else if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
993 methods[num_methods].method == EAP_TYPE_LEAP)
1005 methods = os_realloc_array(methods, num_methods + 1, sizeof(*methods));
1006 if (methods == NULL) {
1010 methods[num_methods].vendor = EAP_VENDOR_IETF;
1011 methods[num_methods].method = EAP_TYPE_NONE;
1014 wpa_hexdump(MSG_MSGDUMP, "eap methods",
1015 (u8 *) methods, num_methods * sizeof(*methods));
1016 os_free(ssid->eap.eap_methods);
1017 ssid->eap.eap_methods = methods;
1018 return errors ? -1 : 0;
1022 static char * wpa_config_write_eap(const struct parse_data *data,
1023 struct wpa_ssid *ssid)
1026 char *buf, *pos, *end;
1027 const struct eap_method_type *eap_methods = ssid->eap.eap_methods;
1030 if (eap_methods == NULL)
1033 pos = buf = os_zalloc(100);
1038 for (i = 0; eap_methods[i].vendor != EAP_VENDOR_IETF ||
1039 eap_methods[i].method != EAP_TYPE_NONE; i++) {
1040 name = eap_get_name(eap_methods[i].vendor,
1041 eap_methods[i].method);
1043 ret = os_snprintf(pos, end - pos, "%s%s",
1044 pos == buf ? "" : " ", name);
1045 if (ret < 0 || ret >= end - pos)
1057 static int wpa_config_parse_password(const struct parse_data *data,
1058 struct wpa_ssid *ssid, int line,
1063 if (os_strcmp(value, "NULL") == 0) {
1064 wpa_printf(MSG_DEBUG, "Unset configuration string 'password'");
1065 os_free(ssid->eap.password);
1066 ssid->eap.password = NULL;
1067 ssid->eap.password_len = 0;
1071 #ifdef CONFIG_EXT_PASSWORD
1072 if (os_strncmp(value, "ext:", 4) == 0) {
1073 char *name = os_strdup(value + 4);
1076 os_free(ssid->eap.password);
1077 ssid->eap.password = (u8 *) name;
1078 ssid->eap.password_len = os_strlen(name);
1079 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
1080 ssid->eap.flags |= EAP_CONFIG_FLAGS_EXT_PASSWORD;
1083 #endif /* CONFIG_EXT_PASSWORD */
1085 if (os_strncmp(value, "hash:", 5) != 0) {
1089 tmp = wpa_config_parse_string(value, &res_len);
1091 wpa_printf(MSG_ERROR, "Line %d: failed to parse "
1095 wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
1096 (u8 *) tmp, res_len);
1098 os_free(ssid->eap.password);
1099 ssid->eap.password = (u8 *) tmp;
1100 ssid->eap.password_len = res_len;
1101 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
1102 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_PASSWORD;
1108 /* NtPasswordHash: hash:<32 hex digits> */
1109 if (os_strlen(value + 5) != 2 * 16) {
1110 wpa_printf(MSG_ERROR, "Line %d: Invalid password hash length "
1111 "(expected 32 hex digits)", line);
1115 hash = os_malloc(16);
1119 if (hexstr2bin(value + 5, hash, 16)) {
1121 wpa_printf(MSG_ERROR, "Line %d: Invalid password hash", line);
1125 wpa_hexdump_key(MSG_MSGDUMP, data->name, hash, 16);
1127 os_free(ssid->eap.password);
1128 ssid->eap.password = hash;
1129 ssid->eap.password_len = 16;
1130 ssid->eap.flags |= EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
1131 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_PASSWORD;
1137 static char * wpa_config_write_password(const struct parse_data *data,
1138 struct wpa_ssid *ssid)
1142 if (ssid->eap.password == NULL)
1145 #ifdef CONFIG_EXT_PASSWORD
1146 if (ssid->eap.flags & EAP_CONFIG_FLAGS_EXT_PASSWORD) {
1147 buf = os_zalloc(4 + ssid->eap.password_len + 1);
1150 os_memcpy(buf, "ext:", 4);
1151 os_memcpy(buf + 4, ssid->eap.password, ssid->eap.password_len);
1154 #endif /* CONFIG_EXT_PASSWORD */
1156 if (!(ssid->eap.flags & EAP_CONFIG_FLAGS_PASSWORD_NTHASH)) {
1157 return wpa_config_write_string(
1158 ssid->eap.password, ssid->eap.password_len);
1161 buf = os_malloc(5 + 32 + 1);
1165 os_memcpy(buf, "hash:", 5);
1166 wpa_snprintf_hex(buf + 5, 32 + 1, ssid->eap.password, 16);
1170 #endif /* IEEE8021X_EAPOL */
1173 static int wpa_config_parse_wep_key(u8 *key, size_t *len, int line,
1174 const char *value, int idx)
1176 char *buf, title[20];
1179 buf = wpa_config_parse_string(value, len);
1181 wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key %d '%s'.",
1185 if (*len > MAX_WEP_KEY_LEN) {
1186 wpa_printf(MSG_ERROR, "Line %d: Too long WEP key %d '%s'.",
1191 if (*len && *len != 5 && *len != 13 && *len != 16) {
1192 wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key length %u - "
1193 "this network block will be ignored",
1194 line, (unsigned int) *len);
1196 os_memcpy(key, buf, *len);
1198 res = os_snprintf(title, sizeof(title), "wep_key%d", idx);
1199 if (res >= 0 && (size_t) res < sizeof(title))
1200 wpa_hexdump_key(MSG_MSGDUMP, title, key, *len);
1205 static int wpa_config_parse_wep_key0(const struct parse_data *data,
1206 struct wpa_ssid *ssid, int line,
1209 return wpa_config_parse_wep_key(ssid->wep_key[0],
1210 &ssid->wep_key_len[0], line,
1215 static int wpa_config_parse_wep_key1(const struct parse_data *data,
1216 struct wpa_ssid *ssid, int line,
1219 return wpa_config_parse_wep_key(ssid->wep_key[1],
1220 &ssid->wep_key_len[1], line,
1225 static int wpa_config_parse_wep_key2(const struct parse_data *data,
1226 struct wpa_ssid *ssid, int line,
1229 return wpa_config_parse_wep_key(ssid->wep_key[2],
1230 &ssid->wep_key_len[2], line,
1235 static int wpa_config_parse_wep_key3(const struct parse_data *data,
1236 struct wpa_ssid *ssid, int line,
1239 return wpa_config_parse_wep_key(ssid->wep_key[3],
1240 &ssid->wep_key_len[3], line,
1245 #ifndef NO_CONFIG_WRITE
1246 static char * wpa_config_write_wep_key(struct wpa_ssid *ssid, int idx)
1248 if (ssid->wep_key_len[idx] == 0)
1250 return wpa_config_write_string(ssid->wep_key[idx],
1251 ssid->wep_key_len[idx]);
1255 static char * wpa_config_write_wep_key0(const struct parse_data *data,
1256 struct wpa_ssid *ssid)
1258 return wpa_config_write_wep_key(ssid, 0);
1262 static char * wpa_config_write_wep_key1(const struct parse_data *data,
1263 struct wpa_ssid *ssid)
1265 return wpa_config_write_wep_key(ssid, 1);
1269 static char * wpa_config_write_wep_key2(const struct parse_data *data,
1270 struct wpa_ssid *ssid)
1272 return wpa_config_write_wep_key(ssid, 2);
1276 static char * wpa_config_write_wep_key3(const struct parse_data *data,
1277 struct wpa_ssid *ssid)
1279 return wpa_config_write_wep_key(ssid, 3);
1281 #endif /* NO_CONFIG_WRITE */
1286 static int wpa_config_parse_p2p_client_list(const struct parse_data *data,
1287 struct wpa_ssid *ssid, int line,
1291 u8 *buf, *n, addr[ETH_ALEN];
1298 while (pos && *pos) {
1302 if (hwaddr_aton(pos, addr)) {
1304 wpa_printf(MSG_ERROR, "Line %d: Invalid "
1305 "p2p_client_list address '%s'.",
1310 /* continue anyway since this could have been from a
1311 * truncated configuration file line */
1312 wpa_printf(MSG_INFO, "Line %d: Ignore likely "
1313 "truncated p2p_client_list address '%s'",
1316 n = os_realloc_array(buf, count + 1, ETH_ALEN);
1322 os_memmove(buf + ETH_ALEN, buf, count * ETH_ALEN);
1323 os_memcpy(buf, addr, ETH_ALEN);
1325 wpa_hexdump(MSG_MSGDUMP, "p2p_client_list",
1329 pos = os_strchr(pos, ' ');
1332 os_free(ssid->p2p_client_list);
1333 ssid->p2p_client_list = buf;
1334 ssid->num_p2p_clients = count;
1340 #ifndef NO_CONFIG_WRITE
1341 static char * wpa_config_write_p2p_client_list(const struct parse_data *data,
1342 struct wpa_ssid *ssid)
1344 char *value, *end, *pos;
1348 if (ssid->p2p_client_list == NULL || ssid->num_p2p_clients == 0)
1351 value = os_malloc(20 * ssid->num_p2p_clients);
1355 end = value + 20 * ssid->num_p2p_clients;
1357 for (i = ssid->num_p2p_clients; i > 0; i--) {
1358 res = os_snprintf(pos, end - pos, MACSTR " ",
1359 MAC2STR(ssid->p2p_client_list +
1360 (i - 1) * ETH_ALEN));
1361 if (res < 0 || res >= end - pos) {
1373 #endif /* NO_CONFIG_WRITE */
1375 #endif /* CONFIG_P2P */
1377 /* Helper macros for network block parser */
1382 /* OFFSET: Get offset of a variable within the wpa_ssid structure */
1383 #define OFFSET(v) ((void *) &((struct wpa_ssid *) 0)->v)
1385 /* STR: Define a string variable for an ASCII string; f = field name */
1386 #ifdef NO_CONFIG_WRITE
1387 #define _STR(f) #f, wpa_config_parse_str, OFFSET(f)
1388 #define _STRe(f) #f, wpa_config_parse_str, OFFSET(eap.f)
1389 #else /* NO_CONFIG_WRITE */
1390 #define _STR(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(f)
1391 #define _STRe(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(eap.f)
1392 #endif /* NO_CONFIG_WRITE */
1393 #define STR(f) _STR(f), NULL, NULL, NULL, 0
1394 #define STRe(f) _STRe(f), NULL, NULL, NULL, 0
1395 #define STR_KEY(f) _STR(f), NULL, NULL, NULL, 1
1396 #define STR_KEYe(f) _STRe(f), NULL, NULL, NULL, 1
1398 /* STR_LEN: Define a string variable with a separate variable for storing the
1399 * data length. Unlike STR(), this can be used to store arbitrary binary data
1400 * (i.e., even nul termination character). */
1401 #define _STR_LEN(f) _STR(f), OFFSET(f ## _len)
1402 #define _STR_LENe(f) _STRe(f), OFFSET(eap.f ## _len)
1403 #define STR_LEN(f) _STR_LEN(f), NULL, NULL, 0
1404 #define STR_LENe(f) _STR_LENe(f), NULL, NULL, 0
1405 #define STR_LEN_KEY(f) _STR_LEN(f), NULL, NULL, 1
1407 /* STR_RANGE: Like STR_LEN(), but with minimum and maximum allowed length
1408 * explicitly specified. */
1409 #define _STR_RANGE(f, min, max) _STR_LEN(f), (void *) (min), (void *) (max)
1410 #define STR_RANGE(f, min, max) _STR_RANGE(f, min, max), 0
1411 #define STR_RANGE_KEY(f, min, max) _STR_RANGE(f, min, max), 1
1413 #ifdef NO_CONFIG_WRITE
1414 #define _INT(f) #f, wpa_config_parse_int, OFFSET(f), (void *) 0
1415 #define _INTe(f) #f, wpa_config_parse_int, OFFSET(eap.f), (void *) 0
1416 #else /* NO_CONFIG_WRITE */
1417 #define _INT(f) #f, wpa_config_parse_int, wpa_config_write_int, \
1418 OFFSET(f), (void *) 0
1419 #define _INTe(f) #f, wpa_config_parse_int, wpa_config_write_int, \
1420 OFFSET(eap.f), (void *) 0
1421 #endif /* NO_CONFIG_WRITE */
1423 /* INT: Define an integer variable */
1424 #define INT(f) _INT(f), NULL, NULL, 0
1425 #define INTe(f) _INTe(f), NULL, NULL, 0
1427 /* INT_RANGE: Define an integer variable with allowed value range */
1428 #define INT_RANGE(f, min, max) _INT(f), (void *) (min), (void *) (max), 0
1430 /* FUNC: Define a configuration variable that uses a custom function for
1431 * parsing and writing the value. */
1432 #ifdef NO_CONFIG_WRITE
1433 #define _FUNC(f) #f, wpa_config_parse_ ## f, NULL, NULL, NULL, NULL
1434 #else /* NO_CONFIG_WRITE */
1435 #define _FUNC(f) #f, wpa_config_parse_ ## f, wpa_config_write_ ## f, \
1436 NULL, NULL, NULL, NULL
1437 #endif /* NO_CONFIG_WRITE */
1438 #define FUNC(f) _FUNC(f), 0
1439 #define FUNC_KEY(f) _FUNC(f), 1
1442 * Table of network configuration variables. This table is used to parse each
1443 * network configuration variable, e.g., each line in wpa_supplicant.conf file
1444 * that is inside a network block.
1446 * This table is generated using the helper macros defined above and with
1447 * generous help from the C pre-processor. The field name is stored as a string
1448 * into .name and for STR and INT types, the offset of the target buffer within
1449 * struct wpa_ssid is stored in .param1. .param2 (if not NULL) is similar
1450 * offset to the field containing the length of the configuration variable.
1451 * .param3 and .param4 can be used to mark the allowed range (length for STR
1452 * and value for INT).
1454 * For each configuration line in wpa_supplicant.conf, the parser goes through
1455 * this table and select the entry that matches with the field name. The parser
1456 * function (.parser) is then called to parse the actual value of the field.
1458 * This kind of mechanism makes it easy to add new configuration parameters,
1459 * since only one line needs to be added into this table and into the
1460 * struct wpa_ssid definition if the new variable is either a string or
1461 * integer. More complex types will need to use their own parser and writer
1464 static const struct parse_data ssid_fields[] = {
1465 { STR_RANGE(ssid, 0, MAX_SSID_LEN) },
1466 { INT_RANGE(scan_ssid, 0, 1) },
1471 { INT(bg_scan_period) },
1475 { FUNC(scan_freq) },
1476 { FUNC(freq_list) },
1477 #ifdef IEEE8021X_EAPOL
1479 { STR_LENe(identity) },
1480 { STR_LENe(anonymous_identity) },
1481 { FUNC_KEY(password) },
1484 { STRe(client_cert) },
1485 { STRe(private_key) },
1486 { STR_KEYe(private_key_passwd) },
1488 { STRe(subject_match) },
1489 { STRe(altsubject_match) },
1492 { STRe(client_cert2) },
1493 { STRe(private_key2) },
1494 { STR_KEYe(private_key2_passwd) },
1496 { STRe(subject_match2) },
1497 { STRe(altsubject_match2) },
1502 { STRe(engine_id) },
1505 { STRe(ca_cert_id) },
1507 { STRe(engine2_id) },
1510 { STRe(ca_cert2_id) },
1513 { INT(eapol_flags) },
1514 #endif /* IEEE8021X_EAPOL */
1515 { FUNC_KEY(wep_key0) },
1516 { FUNC_KEY(wep_key1) },
1517 { FUNC_KEY(wep_key2) },
1518 { FUNC_KEY(wep_key3) },
1519 { INT(wep_tx_keyidx) },
1521 #ifdef IEEE8021X_EAPOL
1522 { INT(eap_workaround) },
1524 { INTe(fragment_size) },
1526 #endif /* IEEE8021X_EAPOL */
1527 { INT_RANGE(mode, 0, 4) },
1528 { INT_RANGE(proactive_key_caching, 0, 1) },
1529 { INT_RANGE(disabled, 0, 2) },
1531 #ifdef CONFIG_IEEE80211W
1532 { INT_RANGE(ieee80211w, 0, 2) },
1533 #endif /* CONFIG_IEEE80211W */
1534 { INT_RANGE(peerkey, 0, 1) },
1535 { INT_RANGE(mixed_cell, 0, 1) },
1536 { INT_RANGE(frequency, 0, 65000) },
1537 { INT(wpa_ptk_rekey) },
1539 { INT_RANGE(ignore_broadcast_ssid, 0, 2) },
1541 { FUNC(p2p_client_list) },
1542 #endif /* CONFIG_P2P */
1543 #ifdef CONFIG_HT_OVERRIDES
1544 { INT_RANGE(disable_ht, 0, 1) },
1545 { INT_RANGE(disable_ht40, -1, 1) },
1546 { INT_RANGE(disable_sgi, 0, 1) },
1547 { INT_RANGE(disable_max_amsdu, -1, 1) },
1548 { INT_RANGE(ampdu_factor, -1, 3) },
1549 { INT_RANGE(ampdu_density, -1, 7) },
1551 #endif /* CONFIG_HT_OVERRIDES */
1552 #ifdef CONFIG_VHT_OVERRIDES
1553 { INT_RANGE(disable_vht, 0, 1) },
1555 { INT(vht_capa_mask) },
1556 { INT_RANGE(vht_rx_mcs_nss_1, -1, 3) },
1557 { INT_RANGE(vht_rx_mcs_nss_2, -1, 3) },
1558 { INT_RANGE(vht_rx_mcs_nss_3, -1, 3) },
1559 { INT_RANGE(vht_rx_mcs_nss_4, -1, 3) },
1560 { INT_RANGE(vht_rx_mcs_nss_5, -1, 3) },
1561 { INT_RANGE(vht_rx_mcs_nss_6, -1, 3) },
1562 { INT_RANGE(vht_rx_mcs_nss_7, -1, 3) },
1563 { INT_RANGE(vht_rx_mcs_nss_8, -1, 3) },
1564 { INT_RANGE(vht_tx_mcs_nss_1, -1, 3) },
1565 { INT_RANGE(vht_tx_mcs_nss_2, -1, 3) },
1566 { INT_RANGE(vht_tx_mcs_nss_3, -1, 3) },
1567 { INT_RANGE(vht_tx_mcs_nss_4, -1, 3) },
1568 { INT_RANGE(vht_tx_mcs_nss_5, -1, 3) },
1569 { INT_RANGE(vht_tx_mcs_nss_6, -1, 3) },
1570 { INT_RANGE(vht_tx_mcs_nss_7, -1, 3) },
1571 { INT_RANGE(vht_tx_mcs_nss_8, -1, 3) },
1572 #endif /* CONFIG_VHT_OVERRIDES */
1573 { INT(ap_max_inactivity) },
1574 { INT(dtim_period) },
1575 { INT(beacon_int) },
1587 #undef STR_RANGE_KEY
1594 #define NUM_SSID_FIELDS (sizeof(ssid_fields) / sizeof(ssid_fields[0]))
1598 * wpa_config_add_prio_network - Add a network to priority lists
1599 * @config: Configuration data from wpa_config_read()
1600 * @ssid: Pointer to the network configuration to be added to the list
1601 * Returns: 0 on success, -1 on failure
1603 * This function is used to add a network block to the priority list of
1604 * networks. This must be called for each network when reading in the full
1605 * configuration. In addition, this can be used indirectly when updating
1606 * priorities by calling wpa_config_update_prio_list().
1608 int wpa_config_add_prio_network(struct wpa_config *config,
1609 struct wpa_ssid *ssid)
1612 struct wpa_ssid *prev, **nlist;
1615 * Add to an existing priority list if one is available for the
1616 * configured priority level for this network.
1618 for (prio = 0; prio < config->num_prio; prio++) {
1619 prev = config->pssid[prio];
1620 if (prev->priority == ssid->priority) {
1628 /* First network for this priority - add a new priority list */
1629 nlist = os_realloc_array(config->pssid, config->num_prio + 1,
1630 sizeof(struct wpa_ssid *));
1634 for (prio = 0; prio < config->num_prio; prio++) {
1635 if (nlist[prio]->priority < ssid->priority) {
1636 os_memmove(&nlist[prio + 1], &nlist[prio],
1637 (config->num_prio - prio) *
1638 sizeof(struct wpa_ssid *));
1645 config->pssid = nlist;
1652 * wpa_config_update_prio_list - Update network priority list
1653 * @config: Configuration data from wpa_config_read()
1654 * Returns: 0 on success, -1 on failure
1656 * This function is called to update the priority list of networks in the
1657 * configuration when a network is being added or removed. This is also called
1658 * if a priority for a network is changed.
1660 int wpa_config_update_prio_list(struct wpa_config *config)
1662 struct wpa_ssid *ssid;
1665 os_free(config->pssid);
1666 config->pssid = NULL;
1667 config->num_prio = 0;
1669 ssid = config->ssid;
1672 if (wpa_config_add_prio_network(config, ssid) < 0)
1681 #ifdef IEEE8021X_EAPOL
1682 static void eap_peer_config_free(struct eap_peer_config *eap)
1684 os_free(eap->eap_methods);
1685 os_free(eap->identity);
1686 os_free(eap->anonymous_identity);
1687 os_free(eap->password);
1688 os_free(eap->ca_cert);
1689 os_free(eap->ca_path);
1690 os_free(eap->client_cert);
1691 os_free(eap->private_key);
1692 os_free(eap->private_key_passwd);
1693 os_free(eap->dh_file);
1694 os_free(eap->subject_match);
1695 os_free(eap->altsubject_match);
1696 os_free(eap->ca_cert2);
1697 os_free(eap->ca_path2);
1698 os_free(eap->client_cert2);
1699 os_free(eap->private_key2);
1700 os_free(eap->private_key2_passwd);
1701 os_free(eap->dh_file2);
1702 os_free(eap->subject_match2);
1703 os_free(eap->altsubject_match2);
1704 os_free(eap->phase1);
1705 os_free(eap->phase2);
1708 os_free(eap->engine_id);
1709 os_free(eap->key_id);
1710 os_free(eap->cert_id);
1711 os_free(eap->ca_cert_id);
1712 os_free(eap->key2_id);
1713 os_free(eap->cert2_id);
1714 os_free(eap->ca_cert2_id);
1716 os_free(eap->engine2_id);
1718 os_free(eap->pending_req_otp);
1719 os_free(eap->pac_file);
1720 os_free(eap->new_password);
1722 #endif /* IEEE8021X_EAPOL */
1726 * wpa_config_free_ssid - Free network/ssid configuration data
1727 * @ssid: Configuration data for the network
1729 * This function frees all resources allocated for the network configuration
1732 void wpa_config_free_ssid(struct wpa_ssid *ssid)
1734 os_free(ssid->ssid);
1735 os_free(ssid->passphrase);
1736 os_free(ssid->ext_psk);
1737 #ifdef IEEE8021X_EAPOL
1738 eap_peer_config_free(&ssid->eap);
1739 #endif /* IEEE8021X_EAPOL */
1740 os_free(ssid->id_str);
1741 os_free(ssid->scan_freq);
1742 os_free(ssid->freq_list);
1743 os_free(ssid->bgscan);
1744 os_free(ssid->p2p_client_list);
1745 #ifdef CONFIG_HT_OVERRIDES
1746 os_free(ssid->ht_mcs);
1747 #endif /* CONFIG_HT_OVERRIDES */
1752 void wpa_config_free_cred(struct wpa_cred *cred)
1754 os_free(cred->realm);
1755 os_free(cred->username);
1756 os_free(cred->password);
1757 os_free(cred->ca_cert);
1758 os_free(cred->client_cert);
1759 os_free(cred->private_key);
1760 os_free(cred->private_key_passwd);
1761 os_free(cred->imsi);
1762 os_free(cred->milenage);
1763 os_free(cred->domain);
1764 os_free(cred->eap_method);
1765 os_free(cred->phase1);
1766 os_free(cred->phase2);
1767 os_free(cred->excluded_ssid);
1773 * wpa_config_free - Free configuration data
1774 * @config: Configuration data from wpa_config_read()
1776 * This function frees all resources allocated for the configuration data by
1777 * wpa_config_read().
1779 void wpa_config_free(struct wpa_config *config)
1781 #ifndef CONFIG_NO_CONFIG_BLOBS
1782 struct wpa_config_blob *blob, *prevblob;
1783 #endif /* CONFIG_NO_CONFIG_BLOBS */
1784 struct wpa_ssid *ssid, *prev = NULL;
1785 struct wpa_cred *cred, *cprev;
1787 ssid = config->ssid;
1791 wpa_config_free_ssid(prev);
1794 cred = config->cred;
1798 wpa_config_free_cred(cprev);
1801 #ifndef CONFIG_NO_CONFIG_BLOBS
1802 blob = config->blobs;
1807 wpa_config_free_blob(prevblob);
1809 #endif /* CONFIG_NO_CONFIG_BLOBS */
1811 wpabuf_free(config->wps_vendor_ext_m1);
1812 os_free(config->ctrl_interface);
1813 os_free(config->ctrl_interface_group);
1814 os_free(config->opensc_engine_path);
1815 os_free(config->pkcs11_engine_path);
1816 os_free(config->pkcs11_module_path);
1817 os_free(config->pcsc_reader);
1818 os_free(config->pcsc_pin);
1819 os_free(config->driver_param);
1820 os_free(config->device_name);
1821 os_free(config->manufacturer);
1822 os_free(config->model_name);
1823 os_free(config->model_number);
1824 os_free(config->serial_number);
1825 os_free(config->config_methods);
1826 os_free(config->p2p_ssid_postfix);
1827 os_free(config->pssid);
1828 os_free(config->p2p_pref_chan);
1829 os_free(config->autoscan);
1830 os_free(config->freq_list);
1831 wpabuf_free(config->wps_nfc_dh_pubkey);
1832 wpabuf_free(config->wps_nfc_dh_privkey);
1833 wpabuf_free(config->wps_nfc_dev_pw);
1834 os_free(config->ext_password_backend);
1835 os_free(config->sae_groups);
1836 wpabuf_free(config->ap_vendor_elements);
1842 * wpa_config_foreach_network - Iterate over each configured network
1843 * @config: Configuration data from wpa_config_read()
1844 * @func: Callback function to process each network
1845 * @arg: Opaque argument to pass to callback function
1847 * Iterate over the set of configured networks calling the specified
1848 * function for each item. We guard against callbacks removing the
1851 void wpa_config_foreach_network(struct wpa_config *config,
1852 void (*func)(void *, struct wpa_ssid *),
1855 struct wpa_ssid *ssid, *next;
1857 ssid = config->ssid;
1867 * wpa_config_get_network - Get configured network based on id
1868 * @config: Configuration data from wpa_config_read()
1869 * @id: Unique network id to search for
1870 * Returns: Network configuration or %NULL if not found
1872 struct wpa_ssid * wpa_config_get_network(struct wpa_config *config, int id)
1874 struct wpa_ssid *ssid;
1876 ssid = config->ssid;
1888 * wpa_config_add_network - Add a new network with empty configuration
1889 * @config: Configuration data from wpa_config_read()
1890 * Returns: The new network configuration or %NULL if operation failed
1892 struct wpa_ssid * wpa_config_add_network(struct wpa_config *config)
1895 struct wpa_ssid *ssid, *last = NULL;
1898 ssid = config->ssid;
1907 ssid = os_zalloc(sizeof(*ssid));
1914 config->ssid = ssid;
1916 wpa_config_update_prio_list(config);
1923 * wpa_config_remove_network - Remove a configured network based on id
1924 * @config: Configuration data from wpa_config_read()
1925 * @id: Unique network id to search for
1926 * Returns: 0 on success, or -1 if the network was not found
1928 int wpa_config_remove_network(struct wpa_config *config, int id)
1930 struct wpa_ssid *ssid, *prev = NULL;
1932 ssid = config->ssid;
1944 prev->next = ssid->next;
1946 config->ssid = ssid->next;
1948 wpa_config_update_prio_list(config);
1949 wpa_config_free_ssid(ssid);
1955 * wpa_config_set_network_defaults - Set network default values
1956 * @ssid: Pointer to network configuration data
1958 void wpa_config_set_network_defaults(struct wpa_ssid *ssid)
1960 ssid->proto = DEFAULT_PROTO;
1961 ssid->pairwise_cipher = DEFAULT_PAIRWISE;
1962 ssid->group_cipher = DEFAULT_GROUP;
1963 ssid->key_mgmt = DEFAULT_KEY_MGMT;
1964 ssid->bg_scan_period = DEFAULT_BG_SCAN_PERIOD;
1965 #ifdef IEEE8021X_EAPOL
1966 ssid->eapol_flags = DEFAULT_EAPOL_FLAGS;
1967 ssid->eap_workaround = DEFAULT_EAP_WORKAROUND;
1968 ssid->eap.fragment_size = DEFAULT_FRAGMENT_SIZE;
1969 #endif /* IEEE8021X_EAPOL */
1970 #ifdef CONFIG_HT_OVERRIDES
1971 ssid->disable_ht = DEFAULT_DISABLE_HT;
1972 ssid->disable_ht40 = DEFAULT_DISABLE_HT40;
1973 ssid->disable_sgi = DEFAULT_DISABLE_SGI;
1974 ssid->disable_max_amsdu = DEFAULT_DISABLE_MAX_AMSDU;
1975 ssid->ampdu_factor = DEFAULT_AMPDU_FACTOR;
1976 ssid->ampdu_density = DEFAULT_AMPDU_DENSITY;
1977 #endif /* CONFIG_HT_OVERRIDES */
1978 #ifdef CONFIG_VHT_OVERRIDES
1979 ssid->vht_rx_mcs_nss_1 = -1;
1980 ssid->vht_rx_mcs_nss_2 = -1;
1981 ssid->vht_rx_mcs_nss_3 = -1;
1982 ssid->vht_rx_mcs_nss_4 = -1;
1983 ssid->vht_rx_mcs_nss_5 = -1;
1984 ssid->vht_rx_mcs_nss_6 = -1;
1985 ssid->vht_rx_mcs_nss_7 = -1;
1986 ssid->vht_rx_mcs_nss_8 = -1;
1987 ssid->vht_tx_mcs_nss_1 = -1;
1988 ssid->vht_tx_mcs_nss_2 = -1;
1989 ssid->vht_tx_mcs_nss_3 = -1;
1990 ssid->vht_tx_mcs_nss_4 = -1;
1991 ssid->vht_tx_mcs_nss_5 = -1;
1992 ssid->vht_tx_mcs_nss_6 = -1;
1993 ssid->vht_tx_mcs_nss_7 = -1;
1994 ssid->vht_tx_mcs_nss_8 = -1;
1995 #endif /* CONFIG_VHT_OVERRIDES */
1996 ssid->proactive_key_caching = -1;
1997 #ifdef CONFIG_IEEE80211W
1998 ssid->ieee80211w = MGMT_FRAME_PROTECTION_DEFAULT;
1999 #endif /* CONFIG_IEEE80211W */
2004 * wpa_config_set - Set a variable in network configuration
2005 * @ssid: Pointer to network configuration data
2006 * @var: Variable name, e.g., "ssid"
2007 * @value: Variable value
2008 * @line: Line number in configuration file or 0 if not used
2009 * Returns: 0 on success, -1 on failure
2011 * This function can be used to set network configuration variables based on
2012 * both the configuration file and management interface input. The value
2013 * parameter must be in the same format as the text-based configuration file is
2014 * using. For example, strings are using double quotation marks.
2016 int wpa_config_set(struct wpa_ssid *ssid, const char *var, const char *value,
2022 if (ssid == NULL || var == NULL || value == NULL)
2025 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2026 const struct parse_data *field = &ssid_fields[i];
2027 if (os_strcmp(var, field->name) != 0)
2030 if (field->parser(field, ssid, line, value)) {
2032 wpa_printf(MSG_ERROR, "Line %d: failed to "
2033 "parse %s '%s'.", line, var, value);
2039 if (i == NUM_SSID_FIELDS) {
2041 wpa_printf(MSG_ERROR, "Line %d: unknown network field "
2042 "'%s'.", line, var);
2051 int wpa_config_set_quoted(struct wpa_ssid *ssid, const char *var,
2058 len = os_strlen(value);
2059 buf = os_malloc(len + 3);
2063 os_memcpy(buf + 1, value, len);
2065 buf[len + 2] = '\0';
2066 ret = wpa_config_set(ssid, var, buf, 0);
2073 * wpa_config_get_all - Get all options from network configuration
2074 * @ssid: Pointer to network configuration data
2075 * @get_keys: Determines if keys/passwords will be included in returned list
2076 * (if they may be exported)
2077 * Returns: %NULL terminated list of all set keys and their values in the form
2078 * of [key1, val1, key2, val2, ... , NULL]
2080 * This function can be used to get list of all configured network properties.
2081 * The caller is responsible for freeing the returned list and all its
2084 char ** wpa_config_get_all(struct wpa_ssid *ssid, int get_keys)
2086 const struct parse_data *field;
2092 get_keys = get_keys && ssid->export_keys;
2094 props = os_calloc(2 * NUM_SSID_FIELDS + 1, sizeof(char *));
2099 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2100 field = &ssid_fields[i];
2101 if (field->key_data && !get_keys)
2103 value = field->writer(field, ssid);
2106 if (os_strlen(value) == 0) {
2111 key = os_strdup(field->name);
2117 props[fields_num * 2] = key;
2118 props[fields_num * 2 + 1] = value;
2134 #ifndef NO_CONFIG_WRITE
2136 * wpa_config_get - Get a variable in network configuration
2137 * @ssid: Pointer to network configuration data
2138 * @var: Variable name, e.g., "ssid"
2139 * Returns: Value of the variable or %NULL on failure
2141 * This function can be used to get network configuration variables. The
2142 * returned value is a copy of the configuration variable in text format, i.e,.
2143 * the same format that the text-based configuration file and wpa_config_set()
2144 * are using for the value. The caller is responsible for freeing the returned
2147 char * wpa_config_get(struct wpa_ssid *ssid, const char *var)
2151 if (ssid == NULL || var == NULL)
2154 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2155 const struct parse_data *field = &ssid_fields[i];
2156 if (os_strcmp(var, field->name) == 0)
2157 return field->writer(field, ssid);
2165 * wpa_config_get_no_key - Get a variable in network configuration (no keys)
2166 * @ssid: Pointer to network configuration data
2167 * @var: Variable name, e.g., "ssid"
2168 * Returns: Value of the variable or %NULL on failure
2170 * This function can be used to get network configuration variable like
2171 * wpa_config_get(). The only difference is that this functions does not expose
2172 * key/password material from the configuration. In case a key/password field
2173 * is requested, the returned value is an empty string or %NULL if the variable
2174 * is not set or "*" if the variable is set (regardless of its value). The
2175 * returned value is a copy of the configuration variable in text format, i.e,.
2176 * the same format that the text-based configuration file and wpa_config_set()
2177 * are using for the value. The caller is responsible for freeing the returned
2180 char * wpa_config_get_no_key(struct wpa_ssid *ssid, const char *var)
2184 if (ssid == NULL || var == NULL)
2187 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2188 const struct parse_data *field = &ssid_fields[i];
2189 if (os_strcmp(var, field->name) == 0) {
2190 char *res = field->writer(field, ssid);
2191 if (field->key_data) {
2192 if (res && res[0]) {
2193 wpa_printf(MSG_DEBUG, "Do not allow "
2194 "key_data field to be "
2197 return os_strdup("*");
2209 #endif /* NO_CONFIG_WRITE */
2213 * wpa_config_update_psk - Update WPA PSK based on passphrase and SSID
2214 * @ssid: Pointer to network configuration data
2216 * This function must be called to update WPA PSK when either SSID or the
2217 * passphrase has changed for the network configuration.
2219 void wpa_config_update_psk(struct wpa_ssid *ssid)
2221 #ifndef CONFIG_NO_PBKDF2
2222 pbkdf2_sha1(ssid->passphrase, ssid->ssid, ssid->ssid_len, 4096,
2223 ssid->psk, PMK_LEN);
2224 wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
2225 ssid->psk, PMK_LEN);
2227 #endif /* CONFIG_NO_PBKDF2 */
2231 int wpa_config_set_cred(struct wpa_cred *cred, const char *var,
2232 const char *value, int line)
2237 if (os_strcmp(var, "priority") == 0) {
2238 cred->priority = atoi(value);
2242 if (os_strcmp(var, "pcsc") == 0) {
2243 cred->pcsc = atoi(value);
2247 if (os_strcmp(var, "eap") == 0) {
2248 struct eap_method_type method;
2249 method.method = eap_peer_get_type(value, &method.vendor);
2250 if (method.vendor == EAP_VENDOR_IETF &&
2251 method.method == EAP_TYPE_NONE) {
2252 wpa_printf(MSG_ERROR, "Line %d: unknown EAP type '%s' "
2253 "for a credential", line, value);
2256 os_free(cred->eap_method);
2257 cred->eap_method = os_malloc(sizeof(*cred->eap_method));
2258 if (cred->eap_method == NULL)
2260 os_memcpy(cred->eap_method, &method, sizeof(method));
2264 if (os_strcmp(var, "password") == 0 &&
2265 os_strncmp(value, "ext:", 4) == 0) {
2266 os_free(cred->password);
2267 cred->password = os_strdup(value);
2268 cred->ext_password = 1;
2272 val = wpa_config_parse_string(value, &len);
2274 wpa_printf(MSG_ERROR, "Line %d: invalid field '%s' string "
2275 "value '%s'.", line, var, value);
2279 if (os_strcmp(var, "realm") == 0) {
2280 os_free(cred->realm);
2285 if (os_strcmp(var, "username") == 0) {
2286 os_free(cred->username);
2287 cred->username = val;
2291 if (os_strcmp(var, "password") == 0) {
2292 os_free(cred->password);
2293 cred->password = val;
2294 cred->ext_password = 0;
2298 if (os_strcmp(var, "ca_cert") == 0) {
2299 os_free(cred->ca_cert);
2300 cred->ca_cert = val;
2304 if (os_strcmp(var, "client_cert") == 0) {
2305 os_free(cred->client_cert);
2306 cred->client_cert = val;
2310 if (os_strcmp(var, "private_key") == 0) {
2311 os_free(cred->private_key);
2312 cred->private_key = val;
2316 if (os_strcmp(var, "private_key_passwd") == 0) {
2317 os_free(cred->private_key_passwd);
2318 cred->private_key_passwd = val;
2322 if (os_strcmp(var, "imsi") == 0) {
2323 os_free(cred->imsi);
2328 if (os_strcmp(var, "milenage") == 0) {
2329 os_free(cred->milenage);
2330 cred->milenage = val;
2334 if (os_strcmp(var, "domain") == 0) {
2335 os_free(cred->domain);
2340 if (os_strcmp(var, "phase1") == 0) {
2341 os_free(cred->phase1);
2346 if (os_strcmp(var, "phase2") == 0) {
2347 os_free(cred->phase2);
2352 if (os_strcmp(var, "roaming_consortium") == 0) {
2353 if (len < 3 || len > sizeof(cred->roaming_consortium)) {
2354 wpa_printf(MSG_ERROR, "Line %d: invalid "
2355 "roaming_consortium length %d (3..15 "
2356 "expected)", line, (int) len);
2360 os_memcpy(cred->roaming_consortium, val, len);
2361 cred->roaming_consortium_len = len;
2366 if (os_strcmp(var, "excluded_ssid") == 0) {
2367 struct excluded_ssid *e;
2369 if (len > MAX_SSID_LEN) {
2370 wpa_printf(MSG_ERROR, "Line %d: invalid "
2371 "excluded_ssid length %d", line, (int) len);
2376 e = os_realloc_array(cred->excluded_ssid,
2377 cred->num_excluded_ssid + 1,
2378 sizeof(struct excluded_ssid));
2383 cred->excluded_ssid = e;
2385 e = &cred->excluded_ssid[cred->num_excluded_ssid++];
2386 os_memcpy(e->ssid, val, len);
2395 wpa_printf(MSG_ERROR, "Line %d: unknown cred field '%s'.",
2405 struct wpa_cred * wpa_config_get_cred(struct wpa_config *config, int id)
2407 struct wpa_cred *cred;
2409 cred = config->cred;
2420 struct wpa_cred * wpa_config_add_cred(struct wpa_config *config)
2423 struct wpa_cred *cred, *last = NULL;
2426 cred = config->cred;
2435 cred = os_zalloc(sizeof(*cred));
2442 config->cred = cred;
2448 int wpa_config_remove_cred(struct wpa_config *config, int id)
2450 struct wpa_cred *cred, *prev = NULL;
2452 cred = config->cred;
2464 prev->next = cred->next;
2466 config->cred = cred->next;
2468 wpa_config_free_cred(cred);
2473 #ifndef CONFIG_NO_CONFIG_BLOBS
2475 * wpa_config_get_blob - Get a named configuration blob
2476 * @config: Configuration data from wpa_config_read()
2477 * @name: Name of the blob
2478 * Returns: Pointer to blob data or %NULL if not found
2480 const struct wpa_config_blob * wpa_config_get_blob(struct wpa_config *config,
2483 struct wpa_config_blob *blob = config->blobs;
2486 if (os_strcmp(blob->name, name) == 0)
2495 * wpa_config_set_blob - Set or add a named configuration blob
2496 * @config: Configuration data from wpa_config_read()
2497 * @blob: New value for the blob
2499 * Adds a new configuration blob or replaces the current value of an existing
2502 void wpa_config_set_blob(struct wpa_config *config,
2503 struct wpa_config_blob *blob)
2505 wpa_config_remove_blob(config, blob->name);
2506 blob->next = config->blobs;
2507 config->blobs = blob;
2512 * wpa_config_free_blob - Free blob data
2513 * @blob: Pointer to blob to be freed
2515 void wpa_config_free_blob(struct wpa_config_blob *blob)
2518 os_free(blob->name);
2519 os_free(blob->data);
2526 * wpa_config_remove_blob - Remove a named configuration blob
2527 * @config: Configuration data from wpa_config_read()
2528 * @name: Name of the blob to remove
2529 * Returns: 0 if blob was removed or -1 if blob was not found
2531 int wpa_config_remove_blob(struct wpa_config *config, const char *name)
2533 struct wpa_config_blob *pos = config->blobs, *prev = NULL;
2536 if (os_strcmp(pos->name, name) == 0) {
2538 prev->next = pos->next;
2540 config->blobs = pos->next;
2541 wpa_config_free_blob(pos);
2550 #endif /* CONFIG_NO_CONFIG_BLOBS */
2554 * wpa_config_alloc_empty - Allocate an empty configuration
2555 * @ctrl_interface: Control interface parameters, e.g., path to UNIX domain
2557 * @driver_param: Driver parameters
2558 * Returns: Pointer to allocated configuration data or %NULL on failure
2560 struct wpa_config * wpa_config_alloc_empty(const char *ctrl_interface,
2561 const char *driver_param)
2563 struct wpa_config *config;
2564 const int aCWmin = 4, aCWmax = 10;
2565 const struct hostapd_wmm_ac_params ac_bk =
2566 { aCWmin, aCWmax, 7, 0, 0 }; /* background traffic */
2567 const struct hostapd_wmm_ac_params ac_be =
2568 { aCWmin, aCWmax, 3, 0, 0 }; /* best effort traffic */
2569 const struct hostapd_wmm_ac_params ac_vi = /* video traffic */
2570 { aCWmin - 1, aCWmin, 2, 3000 / 32, 0 };
2571 const struct hostapd_wmm_ac_params ac_vo = /* voice traffic */
2572 { aCWmin - 2, aCWmin - 1, 2, 1500 / 32, 0 };
2574 config = os_zalloc(sizeof(*config));
2577 config->eapol_version = DEFAULT_EAPOL_VERSION;
2578 config->ap_scan = DEFAULT_AP_SCAN;
2579 config->fast_reauth = DEFAULT_FAST_REAUTH;
2580 config->p2p_go_intent = DEFAULT_P2P_GO_INTENT;
2581 config->p2p_intra_bss = DEFAULT_P2P_INTRA_BSS;
2582 config->p2p_go_max_inactivity = DEFAULT_P2P_GO_MAX_INACTIVITY;
2583 config->bss_max_count = DEFAULT_BSS_MAX_COUNT;
2584 config->bss_expiration_age = DEFAULT_BSS_EXPIRATION_AGE;
2585 config->bss_expiration_scan_count = DEFAULT_BSS_EXPIRATION_SCAN_COUNT;
2586 config->max_num_sta = DEFAULT_MAX_NUM_STA;
2587 config->access_network_type = DEFAULT_ACCESS_NETWORK_TYPE;
2588 config->scan_cur_freq = DEFAULT_SCAN_CUR_FREQ;
2589 config->wmm_ac_params[0] = ac_be;
2590 config->wmm_ac_params[1] = ac_bk;
2591 config->wmm_ac_params[2] = ac_vi;
2592 config->wmm_ac_params[3] = ac_vo;
2595 config->ctrl_interface = os_strdup(ctrl_interface);
2597 config->driver_param = os_strdup(driver_param);
2603 #ifndef CONFIG_NO_STDOUT_DEBUG
2605 * wpa_config_debug_dump_networks - Debug dump of configured networks
2606 * @config: Configuration data from wpa_config_read()
2608 void wpa_config_debug_dump_networks(struct wpa_config *config)
2611 struct wpa_ssid *ssid;
2613 for (prio = 0; prio < config->num_prio; prio++) {
2614 ssid = config->pssid[prio];
2615 wpa_printf(MSG_DEBUG, "Priority group %d",
2618 wpa_printf(MSG_DEBUG, " id=%d ssid='%s'",
2620 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
2625 #endif /* CONFIG_NO_STDOUT_DEBUG */
2628 struct global_parse_data {
2630 int (*parser)(const struct global_parse_data *data,
2631 struct wpa_config *config, int line, const char *value);
2632 void *param1, *param2, *param3;
2633 unsigned int changed_flag;
2637 static int wpa_global_config_parse_int(const struct global_parse_data *data,
2638 struct wpa_config *config, int line,
2644 dst = (int *) (((u8 *) config) + (long) data->param1);
2645 val = strtol(pos, &end, 0);
2647 wpa_printf(MSG_ERROR, "Line %d: invalid number \"%s\"",
2653 wpa_printf(MSG_DEBUG, "%s=%d", data->name, *dst);
2655 if (data->param2 && *dst < (long) data->param2) {
2656 wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
2657 "min_value=%ld)", line, data->name, *dst,
2658 (long) data->param2);
2659 *dst = (long) data->param2;
2663 if (data->param3 && *dst > (long) data->param3) {
2664 wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
2665 "max_value=%ld)", line, data->name, *dst,
2666 (long) data->param3);
2667 *dst = (long) data->param3;
2675 static int wpa_global_config_parse_str(const struct global_parse_data *data,
2676 struct wpa_config *config, int line,
2682 len = os_strlen(pos);
2683 if (data->param2 && len < (size_t) data->param2) {
2684 wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
2685 "min_len=%ld)", line, data->name,
2686 (unsigned long) len, (long) data->param2);
2690 if (data->param3 && len > (size_t) data->param3) {
2691 wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
2692 "max_len=%ld)", line, data->name,
2693 (unsigned long) len, (long) data->param3);
2697 tmp = os_strdup(pos);
2701 dst = (char **) (((u8 *) config) + (long) data->param1);
2704 wpa_printf(MSG_DEBUG, "%s='%s'", data->name, *dst);
2710 static int wpa_global_config_parse_bin(const struct global_parse_data *data,
2711 struct wpa_config *config, int line,
2715 struct wpabuf **dst, *tmp;
2717 len = os_strlen(pos);
2721 tmp = wpabuf_alloc(len / 2);
2725 if (hexstr2bin(pos, wpabuf_put(tmp, len / 2), len / 2)) {
2730 dst = (struct wpabuf **) (((u8 *) config) + (long) data->param1);
2733 wpa_printf(MSG_DEBUG, "%s", data->name);
2739 static int wpa_config_process_freq_list(const struct global_parse_data *data,
2740 struct wpa_config *config, int line,
2745 freqs = wpa_config_parse_int_array(value);
2748 os_free(config->freq_list);
2749 config->freq_list = freqs;
2754 static int wpa_config_process_country(const struct global_parse_data *data,
2755 struct wpa_config *config, int line,
2758 if (!pos[0] || !pos[1]) {
2759 wpa_printf(MSG_DEBUG, "Invalid country set");
2762 config->country[0] = pos[0];
2763 config->country[1] = pos[1];
2764 wpa_printf(MSG_DEBUG, "country='%c%c'",
2765 config->country[0], config->country[1]);
2770 static int wpa_config_process_load_dynamic_eap(
2771 const struct global_parse_data *data, struct wpa_config *config,
2772 int line, const char *so)
2775 wpa_printf(MSG_DEBUG, "load_dynamic_eap=%s", so);
2776 ret = eap_peer_method_load(so);
2778 wpa_printf(MSG_DEBUG, "This EAP type was already loaded - not "
2781 wpa_printf(MSG_ERROR, "Line %d: Failed to load dynamic EAP "
2782 "method '%s'.", line, so);
2792 static int wpa_config_process_uuid(const struct global_parse_data *data,
2793 struct wpa_config *config, int line,
2797 if (uuid_str2bin(pos, config->uuid)) {
2798 wpa_printf(MSG_ERROR, "Line %d: invalid UUID", line);
2801 uuid_bin2str(config->uuid, buf, sizeof(buf));
2802 wpa_printf(MSG_DEBUG, "uuid=%s", buf);
2807 static int wpa_config_process_device_type(
2808 const struct global_parse_data *data,
2809 struct wpa_config *config, int line, const char *pos)
2811 return wps_dev_type_str2bin(pos, config->device_type);
2815 static int wpa_config_process_os_version(const struct global_parse_data *data,
2816 struct wpa_config *config, int line,
2819 if (hexstr2bin(pos, config->os_version, 4)) {
2820 wpa_printf(MSG_ERROR, "Line %d: invalid os_version", line);
2823 wpa_printf(MSG_DEBUG, "os_version=%08x",
2824 WPA_GET_BE32(config->os_version));
2829 static int wpa_config_process_wps_vendor_ext_m1(
2830 const struct global_parse_data *data,
2831 struct wpa_config *config, int line, const char *pos)
2834 int len = os_strlen(pos) / 2;
2838 wpa_printf(MSG_ERROR, "Line %d: "
2839 "invalid wps_vendor_ext_m1", line);
2843 tmp = wpabuf_alloc(len);
2845 p = wpabuf_put(tmp, len);
2847 if (hexstr2bin(pos, p, len)) {
2848 wpa_printf(MSG_ERROR, "Line %d: "
2849 "invalid wps_vendor_ext_m1", line);
2854 wpabuf_free(config->wps_vendor_ext_m1);
2855 config->wps_vendor_ext_m1 = tmp;
2857 wpa_printf(MSG_ERROR, "Can not allocate "
2858 "memory for wps_vendor_ext_m1");
2865 #endif /* CONFIG_WPS */
2868 static int wpa_config_process_sec_device_type(
2869 const struct global_parse_data *data,
2870 struct wpa_config *config, int line, const char *pos)
2874 if (config->num_sec_device_types >= MAX_SEC_DEVICE_TYPES) {
2875 wpa_printf(MSG_ERROR, "Line %d: too many sec_device_type "
2880 idx = config->num_sec_device_types;
2882 if (wps_dev_type_str2bin(pos, config->sec_device_type[idx]))
2885 config->num_sec_device_types++;
2890 static int wpa_config_process_p2p_pref_chan(
2891 const struct global_parse_data *data,
2892 struct wpa_config *config, int line, const char *pos)
2894 struct p2p_channel *pref = NULL, *n;
2895 unsigned int num = 0;
2899 /* format: class:chan,class:chan,... */
2902 op_class = atoi(pos);
2903 pos2 = os_strchr(pos, ':');
2909 n = os_realloc_array(pref, num + 1,
2910 sizeof(struct p2p_channel));
2914 pref[num].op_class = op_class;
2915 pref[num].chan = chan;
2918 pos = os_strchr(pos2, ',');
2924 os_free(config->p2p_pref_chan);
2925 config->p2p_pref_chan = pref;
2926 config->num_p2p_pref_chan = num;
2927 wpa_hexdump(MSG_DEBUG, "P2P: Preferred class/channel pairs",
2928 (u8 *) config->p2p_pref_chan,
2929 config->num_p2p_pref_chan * sizeof(struct p2p_channel));
2935 wpa_printf(MSG_ERROR, "Line %d: Invalid p2p_pref_chan list", line);
2938 #endif /* CONFIG_P2P */
2941 static int wpa_config_process_hessid(
2942 const struct global_parse_data *data,
2943 struct wpa_config *config, int line, const char *pos)
2945 if (hwaddr_aton2(pos, config->hessid) < 0) {
2946 wpa_printf(MSG_ERROR, "Line %d: Invalid hessid '%s'",
2955 static int wpa_config_process_sae_groups(
2956 const struct global_parse_data *data,
2957 struct wpa_config *config, int line, const char *pos)
2959 int *groups = wpa_config_parse_int_array(pos);
2960 if (groups == NULL) {
2961 wpa_printf(MSG_ERROR, "Line %d: Invalid sae_groups '%s'",
2966 os_free(config->sae_groups);
2967 config->sae_groups = groups;
2973 static int wpa_config_process_ap_vendor_elements(
2974 const struct global_parse_data *data,
2975 struct wpa_config *config, int line, const char *pos)
2978 int len = os_strlen(pos) / 2;
2982 wpa_printf(MSG_ERROR, "Line %d: invalid ap_vendor_elements",
2987 tmp = wpabuf_alloc(len);
2989 p = wpabuf_put(tmp, len);
2991 if (hexstr2bin(pos, p, len)) {
2992 wpa_printf(MSG_ERROR, "Line %d: invalid "
2993 "ap_vendor_elements", line);
2998 wpabuf_free(config->ap_vendor_elements);
2999 config->ap_vendor_elements = tmp;
3001 wpa_printf(MSG_ERROR, "Cannot allocate memory for "
3002 "ap_vendor_elements");
3013 /* OFFSET: Get offset of a variable within the wpa_config structure */
3014 #define OFFSET(v) ((void *) &((struct wpa_config *) 0)->v)
3016 #define FUNC(f) #f, wpa_config_process_ ## f, OFFSET(f), NULL, NULL
3017 #define FUNC_NO_VAR(f) #f, wpa_config_process_ ## f, NULL, NULL, NULL
3018 #define _INT(f) #f, wpa_global_config_parse_int, OFFSET(f)
3019 #define INT(f) _INT(f), NULL, NULL
3020 #define INT_RANGE(f, min, max) _INT(f), (void *) min, (void *) max
3021 #define _STR(f) #f, wpa_global_config_parse_str, OFFSET(f)
3022 #define STR(f) _STR(f), NULL, NULL
3023 #define STR_RANGE(f, min, max) _STR(f), (void *) min, (void *) max
3024 #define BIN(f) #f, wpa_global_config_parse_bin, OFFSET(f), NULL, NULL
3026 static const struct global_parse_data global_fields[] = {
3027 #ifdef CONFIG_CTRL_IFACE
3028 { STR(ctrl_interface), 0 },
3029 { STR(ctrl_interface_group), 0 } /* deprecated */,
3030 #endif /* CONFIG_CTRL_IFACE */
3031 { INT_RANGE(eapol_version, 1, 2), 0 },
3032 { INT(ap_scan), 0 },
3033 { INT(disable_scan_offload), 0 },
3034 { INT(fast_reauth), 0 },
3035 { STR(opensc_engine_path), 0 },
3036 { STR(pkcs11_engine_path), 0 },
3037 { STR(pkcs11_module_path), 0 },
3038 { STR(pcsc_reader), 0 },
3039 { STR(pcsc_pin), 0 },
3040 { STR(driver_param), 0 },
3041 { INT(dot11RSNAConfigPMKLifetime), 0 },
3042 { INT(dot11RSNAConfigPMKReauthThreshold), 0 },
3043 { INT(dot11RSNAConfigSATimeout), 0 },
3044 #ifndef CONFIG_NO_CONFIG_WRITE
3045 { INT(update_config), 0 },
3046 #endif /* CONFIG_NO_CONFIG_WRITE */
3047 { FUNC_NO_VAR(load_dynamic_eap), 0 },
3049 { FUNC(uuid), CFG_CHANGED_UUID },
3050 { STR_RANGE(device_name, 0, 32), CFG_CHANGED_DEVICE_NAME },
3051 { STR_RANGE(manufacturer, 0, 64), CFG_CHANGED_WPS_STRING },
3052 { STR_RANGE(model_name, 0, 32), CFG_CHANGED_WPS_STRING },
3053 { STR_RANGE(model_number, 0, 32), CFG_CHANGED_WPS_STRING },
3054 { STR_RANGE(serial_number, 0, 32), CFG_CHANGED_WPS_STRING },
3055 { FUNC(device_type), CFG_CHANGED_DEVICE_TYPE },
3056 { FUNC(os_version), CFG_CHANGED_OS_VERSION },
3057 { STR(config_methods), CFG_CHANGED_CONFIG_METHODS },
3058 { INT_RANGE(wps_cred_processing, 0, 2), 0 },
3059 { FUNC(wps_vendor_ext_m1), CFG_CHANGED_VENDOR_EXTENSION },
3060 #endif /* CONFIG_WPS */
3062 { FUNC(sec_device_type), CFG_CHANGED_SEC_DEVICE_TYPE },
3063 { INT(p2p_listen_reg_class), 0 },
3064 { INT(p2p_listen_channel), 0 },
3065 { INT(p2p_oper_reg_class), 0 },
3066 { INT(p2p_oper_channel), 0 },
3067 { INT_RANGE(p2p_go_intent, 0, 15), 0 },
3068 { STR(p2p_ssid_postfix), CFG_CHANGED_P2P_SSID_POSTFIX },
3069 { INT_RANGE(persistent_reconnect, 0, 1), 0 },
3070 { INT_RANGE(p2p_intra_bss, 0, 1), CFG_CHANGED_P2P_INTRA_BSS },
3071 { INT(p2p_group_idle), 0 },
3072 { FUNC(p2p_pref_chan), CFG_CHANGED_P2P_PREF_CHAN },
3073 { INT(p2p_go_ht40), 0 },
3074 { INT(p2p_disabled), 0 },
3075 { INT(p2p_no_group_iface), 0 },
3076 { INT_RANGE(p2p_ignore_shared_freq, 0, 1), 0 },
3077 #endif /* CONFIG_P2P */
3078 { FUNC(country), CFG_CHANGED_COUNTRY },
3079 { INT(bss_max_count), 0 },
3080 { INT(bss_expiration_age), 0 },
3081 { INT(bss_expiration_scan_count), 0 },
3082 { INT_RANGE(filter_ssids, 0, 1), 0 },
3083 { INT_RANGE(filter_rssi, -100, 0), 0 },
3084 { INT(max_num_sta), 0 },
3085 { INT_RANGE(disassoc_low_ack, 0, 1), 0 },
3087 { INT_RANGE(hs20, 0, 1), 0 },
3088 #endif /* CONFIG_HS20 */
3089 { INT_RANGE(interworking, 0, 1), 0 },
3090 { FUNC(hessid), 0 },
3091 { INT_RANGE(access_network_type, 0, 15), 0 },
3092 { INT_RANGE(pbc_in_m1, 0, 1), 0 },
3093 { STR(autoscan), 0 },
3094 { INT_RANGE(wps_nfc_dev_pw_id, 0x10, 0xffff),
3095 CFG_CHANGED_NFC_PASSWORD_TOKEN },
3096 { BIN(wps_nfc_dh_pubkey), CFG_CHANGED_NFC_PASSWORD_TOKEN },
3097 { BIN(wps_nfc_dh_privkey), CFG_CHANGED_NFC_PASSWORD_TOKEN },
3098 { BIN(wps_nfc_dev_pw), CFG_CHANGED_NFC_PASSWORD_TOKEN },
3099 { STR(ext_password_backend), CFG_CHANGED_EXT_PW_BACKEND },
3100 { INT(p2p_go_max_inactivity), 0 },
3101 { INT_RANGE(auto_interworking, 0, 1), 0 },
3104 { FUNC(sae_groups), 0 },
3105 { INT(dtim_period), 0 },
3106 { INT(beacon_int), 0 },
3107 { FUNC(ap_vendor_elements), 0 },
3108 { INT_RANGE(ignore_old_scan_res, 0, 1), 0 },
3109 { FUNC(freq_list), 0 },
3110 { INT(scan_cur_freq), 0 },
3111 { INT(sched_scan_interval), 0 },
3122 #define NUM_GLOBAL_FIELDS (sizeof(global_fields) / sizeof(global_fields[0]))
3125 int wpa_config_process_global(struct wpa_config *config, char *pos, int line)
3130 for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
3131 const struct global_parse_data *field = &global_fields[i];
3132 size_t flen = os_strlen(field->name);
3133 if (os_strncmp(pos, field->name, flen) != 0 ||
3137 if (field->parser(field, config, line, pos + flen + 1)) {
3138 wpa_printf(MSG_ERROR, "Line %d: failed to "
3139 "parse '%s'.", line, pos);
3142 if (field->changed_flag == CFG_CHANGED_NFC_PASSWORD_TOKEN)
3143 config->wps_nfc_pw_from_config = 1;
3144 config->changed_parameters |= field->changed_flag;
3147 if (i == NUM_GLOBAL_FIELDS) {
3149 if (os_strncmp(pos, "wmm_ac_", 7) == 0) {
3150 char *tmp = os_strchr(pos, '=');
3154 wpa_printf(MSG_ERROR, "Line %d: invalid line "
3159 if (hostapd_config_wmm_ac(config->wmm_ac_params, pos,
3161 wpa_printf(MSG_ERROR, "Line %d: invalid WMM "
3166 #endif /* CONFIG_AP */
3169 wpa_printf(MSG_ERROR, "Line %d: unknown global field '%s'.",