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)
183 dst = (int *) (((u8 *) ssid) + (long) data->param1);
185 wpa_printf(MSG_MSGDUMP, "%s=%d (0x%x)", data->name, *dst, *dst);
187 if (data->param3 && *dst < (long) data->param3) {
188 wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
189 "min_value=%ld)", line, data->name, *dst,
190 (long) data->param3);
191 *dst = (long) data->param3;
195 if (data->param4 && *dst > (long) data->param4) {
196 wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
197 "max_value=%ld)", line, data->name, *dst,
198 (long) data->param4);
199 *dst = (long) data->param4;
207 #ifndef NO_CONFIG_WRITE
208 static char * wpa_config_write_int(const struct parse_data *data,
209 struct wpa_ssid *ssid)
214 src = (int *) (((u8 *) ssid) + (long) data->param1);
216 value = os_malloc(20);
219 res = os_snprintf(value, 20, "%d", *src);
220 if (res < 0 || res >= 20) {
224 value[20 - 1] = '\0';
227 #endif /* NO_CONFIG_WRITE */
230 static int wpa_config_parse_bssid(const struct parse_data *data,
231 struct wpa_ssid *ssid, int line,
234 if (value[0] == '\0' || os_strcmp(value, "\"\"") == 0 ||
235 os_strcmp(value, "any") == 0) {
237 wpa_printf(MSG_MSGDUMP, "BSSID any");
240 if (hwaddr_aton(value, ssid->bssid)) {
241 wpa_printf(MSG_ERROR, "Line %d: Invalid BSSID '%s'.",
246 wpa_hexdump(MSG_MSGDUMP, "BSSID", ssid->bssid, ETH_ALEN);
251 #ifndef NO_CONFIG_WRITE
252 static char * wpa_config_write_bssid(const struct parse_data *data,
253 struct wpa_ssid *ssid)
258 if (!ssid->bssid_set)
261 value = os_malloc(20);
264 res = os_snprintf(value, 20, MACSTR, MAC2STR(ssid->bssid));
265 if (res < 0 || res >= 20) {
269 value[20 - 1] = '\0';
272 #endif /* NO_CONFIG_WRITE */
275 static int wpa_config_parse_psk(const struct parse_data *data,
276 struct wpa_ssid *ssid, int line,
279 #ifdef CONFIG_EXT_PASSWORD
280 if (os_strncmp(value, "ext:", 4) == 0) {
281 os_free(ssid->passphrase);
282 ssid->passphrase = NULL;
284 os_free(ssid->ext_psk);
285 ssid->ext_psk = os_strdup(value + 4);
286 if (ssid->ext_psk == NULL)
288 wpa_printf(MSG_DEBUG, "PSK: External password '%s'",
292 #endif /* CONFIG_EXT_PASSWORD */
295 #ifndef CONFIG_NO_PBKDF2
300 pos = os_strrchr(value, '"');
304 len = os_strlen(value);
305 if (len < 8 || len > 63) {
306 wpa_printf(MSG_ERROR, "Line %d: Invalid passphrase "
307 "length %lu (expected: 8..63) '%s'.",
308 line, (unsigned long) len, value);
311 wpa_hexdump_ascii_key(MSG_MSGDUMP, "PSK (ASCII passphrase)",
313 if (ssid->passphrase && os_strlen(ssid->passphrase) == len &&
314 os_memcmp(ssid->passphrase, value, len) == 0)
317 os_free(ssid->passphrase);
318 ssid->passphrase = os_malloc(len + 1);
319 if (ssid->passphrase == NULL)
321 os_memcpy(ssid->passphrase, value, len);
322 ssid->passphrase[len] = '\0';
324 #else /* CONFIG_NO_PBKDF2 */
325 wpa_printf(MSG_ERROR, "Line %d: ASCII passphrase not "
328 #endif /* CONFIG_NO_PBKDF2 */
331 if (hexstr2bin(value, ssid->psk, PMK_LEN) ||
332 value[PMK_LEN * 2] != '\0') {
333 wpa_printf(MSG_ERROR, "Line %d: Invalid PSK '%s'.",
338 os_free(ssid->passphrase);
339 ssid->passphrase = NULL;
342 wpa_hexdump_key(MSG_MSGDUMP, "PSK", ssid->psk, PMK_LEN);
347 #ifndef NO_CONFIG_WRITE
348 static char * wpa_config_write_psk(const struct parse_data *data,
349 struct wpa_ssid *ssid)
351 #ifdef CONFIG_EXT_PASSWORD
353 size_t len = 4 + os_strlen(ssid->ext_psk) + 1;
354 char *buf = os_malloc(len);
357 os_snprintf(buf, len, "ext:%s", ssid->ext_psk);
360 #endif /* CONFIG_EXT_PASSWORD */
362 if (ssid->passphrase)
363 return wpa_config_write_string_ascii(
364 (const u8 *) ssid->passphrase,
365 os_strlen(ssid->passphrase));
368 return wpa_config_write_string_hex(ssid->psk, PMK_LEN);
372 #endif /* NO_CONFIG_WRITE */
375 static int wpa_config_parse_proto(const struct parse_data *data,
376 struct wpa_ssid *ssid, int line,
379 int val = 0, last, errors = 0;
380 char *start, *end, *buf;
382 buf = os_strdup(value);
387 while (*start != '\0') {
388 while (*start == ' ' || *start == '\t')
393 while (*end != ' ' && *end != '\t' && *end != '\0')
397 if (os_strcmp(start, "WPA") == 0)
398 val |= WPA_PROTO_WPA;
399 else if (os_strcmp(start, "RSN") == 0 ||
400 os_strcmp(start, "WPA2") == 0)
401 val |= WPA_PROTO_RSN;
403 wpa_printf(MSG_ERROR, "Line %d: invalid proto '%s'",
415 wpa_printf(MSG_ERROR,
416 "Line %d: no proto values configured.", line);
420 wpa_printf(MSG_MSGDUMP, "proto: 0x%x", val);
422 return errors ? -1 : 0;
426 #ifndef NO_CONFIG_WRITE
427 static char * wpa_config_write_proto(const struct parse_data *data,
428 struct wpa_ssid *ssid)
431 char *buf, *pos, *end;
433 pos = buf = os_zalloc(10);
438 if (ssid->proto & WPA_PROTO_WPA) {
439 ret = os_snprintf(pos, end - pos, "%sWPA", first ? "" : " ");
440 if (ret < 0 || ret >= end - pos)
446 if (ssid->proto & WPA_PROTO_RSN) {
447 ret = os_snprintf(pos, end - pos, "%sRSN", first ? "" : " ");
448 if (ret < 0 || ret >= end - pos)
456 #endif /* NO_CONFIG_WRITE */
459 static int wpa_config_parse_key_mgmt(const struct parse_data *data,
460 struct wpa_ssid *ssid, int line,
463 int val = 0, last, errors = 0;
464 char *start, *end, *buf;
466 buf = os_strdup(value);
471 while (*start != '\0') {
472 while (*start == ' ' || *start == '\t')
477 while (*end != ' ' && *end != '\t' && *end != '\0')
481 if (os_strcmp(start, "WPA-PSK") == 0)
482 val |= WPA_KEY_MGMT_PSK;
483 else if (os_strcmp(start, "WPA-EAP") == 0)
484 val |= WPA_KEY_MGMT_IEEE8021X;
485 else if (os_strcmp(start, "IEEE8021X") == 0)
486 val |= WPA_KEY_MGMT_IEEE8021X_NO_WPA;
487 else if (os_strcmp(start, "NONE") == 0)
488 val |= WPA_KEY_MGMT_NONE;
489 else if (os_strcmp(start, "WPA-NONE") == 0)
490 val |= WPA_KEY_MGMT_WPA_NONE;
491 #ifdef CONFIG_IEEE80211R
492 else if (os_strcmp(start, "FT-PSK") == 0)
493 val |= WPA_KEY_MGMT_FT_PSK;
494 else if (os_strcmp(start, "FT-EAP") == 0)
495 val |= WPA_KEY_MGMT_FT_IEEE8021X;
496 #endif /* CONFIG_IEEE80211R */
497 #ifdef CONFIG_IEEE80211W
498 else if (os_strcmp(start, "WPA-PSK-SHA256") == 0)
499 val |= WPA_KEY_MGMT_PSK_SHA256;
500 else if (os_strcmp(start, "WPA-EAP-SHA256") == 0)
501 val |= WPA_KEY_MGMT_IEEE8021X_SHA256;
502 #endif /* CONFIG_IEEE80211W */
504 else if (os_strcmp(start, "WPS") == 0)
505 val |= WPA_KEY_MGMT_WPS;
506 #endif /* CONFIG_WPS */
508 else if (os_strcmp(start, "SAE") == 0)
509 val |= WPA_KEY_MGMT_SAE;
510 else if (os_strcmp(start, "FT-SAE") == 0)
511 val |= WPA_KEY_MGMT_FT_SAE;
512 #endif /* CONFIG_SAE */
514 wpa_printf(MSG_ERROR, "Line %d: invalid key_mgmt '%s'",
526 wpa_printf(MSG_ERROR,
527 "Line %d: no key_mgmt values configured.", line);
531 wpa_printf(MSG_MSGDUMP, "key_mgmt: 0x%x", val);
532 ssid->key_mgmt = val;
533 return errors ? -1 : 0;
537 #ifndef NO_CONFIG_WRITE
538 static char * wpa_config_write_key_mgmt(const struct parse_data *data,
539 struct wpa_ssid *ssid)
541 char *buf, *pos, *end;
544 pos = buf = os_zalloc(50);
549 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
550 ret = os_snprintf(pos, end - pos, "%sWPA-PSK",
551 pos == buf ? "" : " ");
552 if (ret < 0 || ret >= end - pos) {
559 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
560 ret = os_snprintf(pos, end - pos, "%sWPA-EAP",
561 pos == buf ? "" : " ");
562 if (ret < 0 || ret >= end - pos) {
569 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
570 ret = os_snprintf(pos, end - pos, "%sIEEE8021X",
571 pos == buf ? "" : " ");
572 if (ret < 0 || ret >= end - pos) {
579 if (ssid->key_mgmt & WPA_KEY_MGMT_NONE) {
580 ret = os_snprintf(pos, end - pos, "%sNONE",
581 pos == buf ? "" : " ");
582 if (ret < 0 || ret >= end - pos) {
589 if (ssid->key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
590 ret = os_snprintf(pos, end - pos, "%sWPA-NONE",
591 pos == buf ? "" : " ");
592 if (ret < 0 || ret >= end - pos) {
599 #ifdef CONFIG_IEEE80211R
600 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_PSK)
601 pos += os_snprintf(pos, end - pos, "%sFT-PSK",
602 pos == buf ? "" : " ");
604 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X)
605 pos += os_snprintf(pos, end - pos, "%sFT-EAP",
606 pos == buf ? "" : " ");
607 #endif /* CONFIG_IEEE80211R */
609 #ifdef CONFIG_IEEE80211W
610 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK_SHA256)
611 pos += os_snprintf(pos, end - pos, "%sWPA-PSK-SHA256",
612 pos == buf ? "" : " ");
614 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256)
615 pos += os_snprintf(pos, end - pos, "%sWPA-EAP-SHA256",
616 pos == buf ? "" : " ");
617 #endif /* CONFIG_IEEE80211W */
620 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
621 pos += os_snprintf(pos, end - pos, "%sWPS",
622 pos == buf ? "" : " ");
623 #endif /* CONFIG_WPS */
627 #endif /* NO_CONFIG_WRITE */
630 static int wpa_config_parse_cipher(int line, const char *value)
632 int val = wpa_parse_cipher(value);
634 wpa_printf(MSG_ERROR, "Line %d: invalid cipher '%s'.",
639 wpa_printf(MSG_ERROR, "Line %d: no cipher values configured.",
647 #ifndef NO_CONFIG_WRITE
648 static char * wpa_config_write_cipher(int cipher)
650 char *buf = os_zalloc(50);
654 if (wpa_write_ciphers(buf, buf + 50, cipher, " ") < 0) {
661 #endif /* NO_CONFIG_WRITE */
664 static int wpa_config_parse_pairwise(const struct parse_data *data,
665 struct wpa_ssid *ssid, int line,
669 val = wpa_config_parse_cipher(line, value);
672 if (val & ~WPA_ALLOWED_PAIRWISE_CIPHERS) {
673 wpa_printf(MSG_ERROR, "Line %d: not allowed pairwise cipher "
674 "(0x%x).", line, val);
678 wpa_printf(MSG_MSGDUMP, "pairwise: 0x%x", val);
679 ssid->pairwise_cipher = val;
684 #ifndef NO_CONFIG_WRITE
685 static char * wpa_config_write_pairwise(const struct parse_data *data,
686 struct wpa_ssid *ssid)
688 return wpa_config_write_cipher(ssid->pairwise_cipher);
690 #endif /* NO_CONFIG_WRITE */
693 static int wpa_config_parse_group(const struct parse_data *data,
694 struct wpa_ssid *ssid, int line,
698 val = wpa_config_parse_cipher(line, value);
701 if (val & ~WPA_ALLOWED_GROUP_CIPHERS) {
702 wpa_printf(MSG_ERROR, "Line %d: not allowed group cipher "
703 "(0x%x).", line, val);
707 wpa_printf(MSG_MSGDUMP, "group: 0x%x", val);
708 ssid->group_cipher = val;
713 #ifndef NO_CONFIG_WRITE
714 static char * wpa_config_write_group(const struct parse_data *data,
715 struct wpa_ssid *ssid)
717 return wpa_config_write_cipher(ssid->group_cipher);
719 #endif /* NO_CONFIG_WRITE */
722 static int wpa_config_parse_auth_alg(const struct parse_data *data,
723 struct wpa_ssid *ssid, int line,
726 int val = 0, last, errors = 0;
727 char *start, *end, *buf;
729 buf = os_strdup(value);
734 while (*start != '\0') {
735 while (*start == ' ' || *start == '\t')
740 while (*end != ' ' && *end != '\t' && *end != '\0')
744 if (os_strcmp(start, "OPEN") == 0)
745 val |= WPA_AUTH_ALG_OPEN;
746 else if (os_strcmp(start, "SHARED") == 0)
747 val |= WPA_AUTH_ALG_SHARED;
748 else if (os_strcmp(start, "LEAP") == 0)
749 val |= WPA_AUTH_ALG_LEAP;
751 wpa_printf(MSG_ERROR, "Line %d: invalid auth_alg '%s'",
763 wpa_printf(MSG_ERROR,
764 "Line %d: no auth_alg values configured.", line);
768 wpa_printf(MSG_MSGDUMP, "auth_alg: 0x%x", val);
769 ssid->auth_alg = val;
770 return errors ? -1 : 0;
774 #ifndef NO_CONFIG_WRITE
775 static char * wpa_config_write_auth_alg(const struct parse_data *data,
776 struct wpa_ssid *ssid)
778 char *buf, *pos, *end;
781 pos = buf = os_zalloc(30);
786 if (ssid->auth_alg & WPA_AUTH_ALG_OPEN) {
787 ret = os_snprintf(pos, end - pos, "%sOPEN",
788 pos == buf ? "" : " ");
789 if (ret < 0 || ret >= end - pos) {
796 if (ssid->auth_alg & WPA_AUTH_ALG_SHARED) {
797 ret = os_snprintf(pos, end - pos, "%sSHARED",
798 pos == buf ? "" : " ");
799 if (ret < 0 || ret >= end - pos) {
806 if (ssid->auth_alg & WPA_AUTH_ALG_LEAP) {
807 ret = os_snprintf(pos, end - pos, "%sLEAP",
808 pos == buf ? "" : " ");
809 if (ret < 0 || ret >= end - pos) {
818 #endif /* NO_CONFIG_WRITE */
821 static int * wpa_config_parse_int_array(const char *value)
829 freqs = os_calloc(len + 1, sizeof(int));
840 n = os_realloc_array(freqs, len * 2 + 1, sizeof(int));
845 for (i = len; i <= len * 2; i++)
851 freqs[used] = atoi(pos);
852 if (freqs[used] == 0)
855 pos = os_strchr(pos + 1, ' ');
862 static int wpa_config_parse_scan_freq(const struct parse_data *data,
863 struct wpa_ssid *ssid, int line,
868 freqs = wpa_config_parse_int_array(value);
871 os_free(ssid->scan_freq);
872 ssid->scan_freq = freqs;
878 static int wpa_config_parse_freq_list(const struct parse_data *data,
879 struct wpa_ssid *ssid, int line,
884 freqs = wpa_config_parse_int_array(value);
887 os_free(ssid->freq_list);
888 ssid->freq_list = freqs;
894 #ifndef NO_CONFIG_WRITE
895 static char * wpa_config_write_freqs(const struct parse_data *data,
898 char *buf, *pos, *end;
906 for (i = 0; freqs[i]; i++)
909 pos = buf = os_zalloc(10 * count + 1);
912 end = buf + 10 * count + 1;
914 for (i = 0; freqs[i]; i++) {
915 ret = os_snprintf(pos, end - pos, "%s%u",
916 i == 0 ? "" : " ", freqs[i]);
917 if (ret < 0 || ret >= end - pos) {
928 static char * wpa_config_write_scan_freq(const struct parse_data *data,
929 struct wpa_ssid *ssid)
931 return wpa_config_write_freqs(data, ssid->scan_freq);
935 static char * wpa_config_write_freq_list(const struct parse_data *data,
936 struct wpa_ssid *ssid)
938 return wpa_config_write_freqs(data, ssid->freq_list);
940 #endif /* NO_CONFIG_WRITE */
943 #ifdef IEEE8021X_EAPOL
944 static int wpa_config_parse_eap(const struct parse_data *data,
945 struct wpa_ssid *ssid, int line,
948 int last, errors = 0;
949 char *start, *end, *buf;
950 struct eap_method_type *methods = NULL, *tmp;
951 size_t num_methods = 0;
953 buf = os_strdup(value);
958 while (*start != '\0') {
959 while (*start == ' ' || *start == '\t')
964 while (*end != ' ' && *end != '\t' && *end != '\0')
969 methods = os_realloc_array(methods, num_methods + 1,
971 if (methods == NULL) {
976 methods[num_methods].method = eap_peer_get_type(
977 start, &methods[num_methods].vendor);
978 if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
979 methods[num_methods].method == EAP_TYPE_NONE) {
980 wpa_printf(MSG_ERROR, "Line %d: unknown EAP method "
981 "'%s'", line, start);
982 wpa_printf(MSG_ERROR, "You may need to add support for"
983 " this EAP method during wpa_supplicant\n"
984 "build time configuration.\n"
985 "See README for more information.");
987 } else if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
988 methods[num_methods].method == EAP_TYPE_LEAP)
1000 methods = os_realloc_array(methods, num_methods + 1, sizeof(*methods));
1001 if (methods == NULL) {
1005 methods[num_methods].vendor = EAP_VENDOR_IETF;
1006 methods[num_methods].method = EAP_TYPE_NONE;
1009 wpa_hexdump(MSG_MSGDUMP, "eap methods",
1010 (u8 *) methods, num_methods * sizeof(*methods));
1011 os_free(ssid->eap.eap_methods);
1012 ssid->eap.eap_methods = methods;
1013 return errors ? -1 : 0;
1017 static char * wpa_config_write_eap(const struct parse_data *data,
1018 struct wpa_ssid *ssid)
1021 char *buf, *pos, *end;
1022 const struct eap_method_type *eap_methods = ssid->eap.eap_methods;
1025 if (eap_methods == NULL)
1028 pos = buf = os_zalloc(100);
1033 for (i = 0; eap_methods[i].vendor != EAP_VENDOR_IETF ||
1034 eap_methods[i].method != EAP_TYPE_NONE; i++) {
1035 name = eap_get_name(eap_methods[i].vendor,
1036 eap_methods[i].method);
1038 ret = os_snprintf(pos, end - pos, "%s%s",
1039 pos == buf ? "" : " ", name);
1040 if (ret < 0 || ret >= end - pos)
1052 static int wpa_config_parse_password(const struct parse_data *data,
1053 struct wpa_ssid *ssid, int line,
1058 if (os_strcmp(value, "NULL") == 0) {
1059 wpa_printf(MSG_DEBUG, "Unset configuration string 'password'");
1060 os_free(ssid->eap.password);
1061 ssid->eap.password = NULL;
1062 ssid->eap.password_len = 0;
1066 #ifdef CONFIG_EXT_PASSWORD
1067 if (os_strncmp(value, "ext:", 4) == 0) {
1068 char *name = os_strdup(value + 4);
1071 os_free(ssid->eap.password);
1072 ssid->eap.password = (u8 *) name;
1073 ssid->eap.password_len = os_strlen(name);
1074 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
1075 ssid->eap.flags |= EAP_CONFIG_FLAGS_EXT_PASSWORD;
1078 #endif /* CONFIG_EXT_PASSWORD */
1080 if (os_strncmp(value, "hash:", 5) != 0) {
1084 tmp = wpa_config_parse_string(value, &res_len);
1086 wpa_printf(MSG_ERROR, "Line %d: failed to parse "
1090 wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
1091 (u8 *) tmp, res_len);
1093 os_free(ssid->eap.password);
1094 ssid->eap.password = (u8 *) tmp;
1095 ssid->eap.password_len = res_len;
1096 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
1097 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_PASSWORD;
1103 /* NtPasswordHash: hash:<32 hex digits> */
1104 if (os_strlen(value + 5) != 2 * 16) {
1105 wpa_printf(MSG_ERROR, "Line %d: Invalid password hash length "
1106 "(expected 32 hex digits)", line);
1110 hash = os_malloc(16);
1114 if (hexstr2bin(value + 5, hash, 16)) {
1116 wpa_printf(MSG_ERROR, "Line %d: Invalid password hash", line);
1120 wpa_hexdump_key(MSG_MSGDUMP, data->name, hash, 16);
1122 os_free(ssid->eap.password);
1123 ssid->eap.password = hash;
1124 ssid->eap.password_len = 16;
1125 ssid->eap.flags |= EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
1126 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_PASSWORD;
1132 static char * wpa_config_write_password(const struct parse_data *data,
1133 struct wpa_ssid *ssid)
1137 if (ssid->eap.password == NULL)
1140 #ifdef CONFIG_EXT_PASSWORD
1141 if (ssid->eap.flags & EAP_CONFIG_FLAGS_EXT_PASSWORD) {
1142 buf = os_zalloc(4 + ssid->eap.password_len + 1);
1145 os_memcpy(buf, "ext:", 4);
1146 os_memcpy(buf + 4, ssid->eap.password, ssid->eap.password_len);
1149 #endif /* CONFIG_EXT_PASSWORD */
1151 if (!(ssid->eap.flags & EAP_CONFIG_FLAGS_PASSWORD_NTHASH)) {
1152 return wpa_config_write_string(
1153 ssid->eap.password, ssid->eap.password_len);
1156 buf = os_malloc(5 + 32 + 1);
1160 os_memcpy(buf, "hash:", 5);
1161 wpa_snprintf_hex(buf + 5, 32 + 1, ssid->eap.password, 16);
1165 #endif /* IEEE8021X_EAPOL */
1168 static int wpa_config_parse_wep_key(u8 *key, size_t *len, int line,
1169 const char *value, int idx)
1171 char *buf, title[20];
1174 buf = wpa_config_parse_string(value, len);
1176 wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key %d '%s'.",
1180 if (*len > MAX_WEP_KEY_LEN) {
1181 wpa_printf(MSG_ERROR, "Line %d: Too long WEP key %d '%s'.",
1186 if (*len && *len != 5 && *len != 13 && *len != 16) {
1187 wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key length %u - "
1188 "this network block will be ignored",
1189 line, (unsigned int) *len);
1191 os_memcpy(key, buf, *len);
1193 res = os_snprintf(title, sizeof(title), "wep_key%d", idx);
1194 if (res >= 0 && (size_t) res < sizeof(title))
1195 wpa_hexdump_key(MSG_MSGDUMP, title, key, *len);
1200 static int wpa_config_parse_wep_key0(const struct parse_data *data,
1201 struct wpa_ssid *ssid, int line,
1204 return wpa_config_parse_wep_key(ssid->wep_key[0],
1205 &ssid->wep_key_len[0], line,
1210 static int wpa_config_parse_wep_key1(const struct parse_data *data,
1211 struct wpa_ssid *ssid, int line,
1214 return wpa_config_parse_wep_key(ssid->wep_key[1],
1215 &ssid->wep_key_len[1], line,
1220 static int wpa_config_parse_wep_key2(const struct parse_data *data,
1221 struct wpa_ssid *ssid, int line,
1224 return wpa_config_parse_wep_key(ssid->wep_key[2],
1225 &ssid->wep_key_len[2], line,
1230 static int wpa_config_parse_wep_key3(const struct parse_data *data,
1231 struct wpa_ssid *ssid, int line,
1234 return wpa_config_parse_wep_key(ssid->wep_key[3],
1235 &ssid->wep_key_len[3], line,
1240 #ifndef NO_CONFIG_WRITE
1241 static char * wpa_config_write_wep_key(struct wpa_ssid *ssid, int idx)
1243 if (ssid->wep_key_len[idx] == 0)
1245 return wpa_config_write_string(ssid->wep_key[idx],
1246 ssid->wep_key_len[idx]);
1250 static char * wpa_config_write_wep_key0(const struct parse_data *data,
1251 struct wpa_ssid *ssid)
1253 return wpa_config_write_wep_key(ssid, 0);
1257 static char * wpa_config_write_wep_key1(const struct parse_data *data,
1258 struct wpa_ssid *ssid)
1260 return wpa_config_write_wep_key(ssid, 1);
1264 static char * wpa_config_write_wep_key2(const struct parse_data *data,
1265 struct wpa_ssid *ssid)
1267 return wpa_config_write_wep_key(ssid, 2);
1271 static char * wpa_config_write_wep_key3(const struct parse_data *data,
1272 struct wpa_ssid *ssid)
1274 return wpa_config_write_wep_key(ssid, 3);
1276 #endif /* NO_CONFIG_WRITE */
1281 static int wpa_config_parse_p2p_client_list(const struct parse_data *data,
1282 struct wpa_ssid *ssid, int line,
1286 u8 *buf, *n, addr[ETH_ALEN];
1293 while (pos && *pos) {
1297 if (hwaddr_aton(pos, addr)) {
1299 wpa_printf(MSG_ERROR, "Line %d: Invalid "
1300 "p2p_client_list address '%s'.",
1305 /* continue anyway since this could have been from a
1306 * truncated configuration file line */
1307 wpa_printf(MSG_INFO, "Line %d: Ignore likely "
1308 "truncated p2p_client_list address '%s'",
1311 n = os_realloc_array(buf, count + 1, ETH_ALEN);
1317 os_memmove(buf + ETH_ALEN, buf, count * ETH_ALEN);
1318 os_memcpy(buf, addr, ETH_ALEN);
1320 wpa_hexdump(MSG_MSGDUMP, "p2p_client_list",
1324 pos = os_strchr(pos, ' ');
1327 os_free(ssid->p2p_client_list);
1328 ssid->p2p_client_list = buf;
1329 ssid->num_p2p_clients = count;
1335 #ifndef NO_CONFIG_WRITE
1336 static char * wpa_config_write_p2p_client_list(const struct parse_data *data,
1337 struct wpa_ssid *ssid)
1339 char *value, *end, *pos;
1343 if (ssid->p2p_client_list == NULL || ssid->num_p2p_clients == 0)
1346 value = os_malloc(20 * ssid->num_p2p_clients);
1350 end = value + 20 * ssid->num_p2p_clients;
1352 for (i = ssid->num_p2p_clients; i > 0; i--) {
1353 res = os_snprintf(pos, end - pos, MACSTR " ",
1354 MAC2STR(ssid->p2p_client_list +
1355 (i - 1) * ETH_ALEN));
1356 if (res < 0 || res >= end - pos) {
1368 #endif /* NO_CONFIG_WRITE */
1370 #endif /* CONFIG_P2P */
1372 /* Helper macros for network block parser */
1377 /* OFFSET: Get offset of a variable within the wpa_ssid structure */
1378 #define OFFSET(v) ((void *) &((struct wpa_ssid *) 0)->v)
1380 /* STR: Define a string variable for an ASCII string; f = field name */
1381 #ifdef NO_CONFIG_WRITE
1382 #define _STR(f) #f, wpa_config_parse_str, OFFSET(f)
1383 #define _STRe(f) #f, wpa_config_parse_str, OFFSET(eap.f)
1384 #else /* NO_CONFIG_WRITE */
1385 #define _STR(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(f)
1386 #define _STRe(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(eap.f)
1387 #endif /* NO_CONFIG_WRITE */
1388 #define STR(f) _STR(f), NULL, NULL, NULL, 0
1389 #define STRe(f) _STRe(f), NULL, NULL, NULL, 0
1390 #define STR_KEY(f) _STR(f), NULL, NULL, NULL, 1
1391 #define STR_KEYe(f) _STRe(f), NULL, NULL, NULL, 1
1393 /* STR_LEN: Define a string variable with a separate variable for storing the
1394 * data length. Unlike STR(), this can be used to store arbitrary binary data
1395 * (i.e., even nul termination character). */
1396 #define _STR_LEN(f) _STR(f), OFFSET(f ## _len)
1397 #define _STR_LENe(f) _STRe(f), OFFSET(eap.f ## _len)
1398 #define STR_LEN(f) _STR_LEN(f), NULL, NULL, 0
1399 #define STR_LENe(f) _STR_LENe(f), NULL, NULL, 0
1400 #define STR_LEN_KEY(f) _STR_LEN(f), NULL, NULL, 1
1402 /* STR_RANGE: Like STR_LEN(), but with minimum and maximum allowed length
1403 * explicitly specified. */
1404 #define _STR_RANGE(f, min, max) _STR_LEN(f), (void *) (min), (void *) (max)
1405 #define STR_RANGE(f, min, max) _STR_RANGE(f, min, max), 0
1406 #define STR_RANGE_KEY(f, min, max) _STR_RANGE(f, min, max), 1
1408 #ifdef NO_CONFIG_WRITE
1409 #define _INT(f) #f, wpa_config_parse_int, OFFSET(f), (void *) 0
1410 #define _INTe(f) #f, wpa_config_parse_int, OFFSET(eap.f), (void *) 0
1411 #else /* NO_CONFIG_WRITE */
1412 #define _INT(f) #f, wpa_config_parse_int, wpa_config_write_int, \
1413 OFFSET(f), (void *) 0
1414 #define _INTe(f) #f, wpa_config_parse_int, wpa_config_write_int, \
1415 OFFSET(eap.f), (void *) 0
1416 #endif /* NO_CONFIG_WRITE */
1418 /* INT: Define an integer variable */
1419 #define INT(f) _INT(f), NULL, NULL, 0
1420 #define INTe(f) _INTe(f), NULL, NULL, 0
1422 /* INT_RANGE: Define an integer variable with allowed value range */
1423 #define INT_RANGE(f, min, max) _INT(f), (void *) (min), (void *) (max), 0
1425 /* FUNC: Define a configuration variable that uses a custom function for
1426 * parsing and writing the value. */
1427 #ifdef NO_CONFIG_WRITE
1428 #define _FUNC(f) #f, wpa_config_parse_ ## f, NULL, NULL, NULL, NULL
1429 #else /* NO_CONFIG_WRITE */
1430 #define _FUNC(f) #f, wpa_config_parse_ ## f, wpa_config_write_ ## f, \
1431 NULL, NULL, NULL, NULL
1432 #endif /* NO_CONFIG_WRITE */
1433 #define FUNC(f) _FUNC(f), 0
1434 #define FUNC_KEY(f) _FUNC(f), 1
1437 * Table of network configuration variables. This table is used to parse each
1438 * network configuration variable, e.g., each line in wpa_supplicant.conf file
1439 * that is inside a network block.
1441 * This table is generated using the helper macros defined above and with
1442 * generous help from the C pre-processor. The field name is stored as a string
1443 * into .name and for STR and INT types, the offset of the target buffer within
1444 * struct wpa_ssid is stored in .param1. .param2 (if not NULL) is similar
1445 * offset to the field containing the length of the configuration variable.
1446 * .param3 and .param4 can be used to mark the allowed range (length for STR
1447 * and value for INT).
1449 * For each configuration line in wpa_supplicant.conf, the parser goes through
1450 * this table and select the entry that matches with the field name. The parser
1451 * function (.parser) is then called to parse the actual value of the field.
1453 * This kind of mechanism makes it easy to add new configuration parameters,
1454 * since only one line needs to be added into this table and into the
1455 * struct wpa_ssid definition if the new variable is either a string or
1456 * integer. More complex types will need to use their own parser and writer
1459 static const struct parse_data ssid_fields[] = {
1460 { STR_RANGE(ssid, 0, MAX_SSID_LEN) },
1461 { INT_RANGE(scan_ssid, 0, 1) },
1466 { INT(bg_scan_period) },
1470 { FUNC(scan_freq) },
1471 { FUNC(freq_list) },
1472 #ifdef IEEE8021X_EAPOL
1474 { STR_LENe(identity) },
1475 { STR_LENe(anonymous_identity) },
1476 { FUNC_KEY(password) },
1479 { STRe(client_cert) },
1480 { STRe(private_key) },
1481 { STR_KEYe(private_key_passwd) },
1483 { STRe(subject_match) },
1484 { STRe(altsubject_match) },
1487 { STRe(client_cert2) },
1488 { STRe(private_key2) },
1489 { STR_KEYe(private_key2_passwd) },
1491 { STRe(subject_match2) },
1492 { STRe(altsubject_match2) },
1497 { STRe(engine_id) },
1500 { STRe(ca_cert_id) },
1502 { STRe(engine2_id) },
1505 { STRe(ca_cert2_id) },
1508 { INT(eapol_flags) },
1509 #endif /* IEEE8021X_EAPOL */
1510 { FUNC_KEY(wep_key0) },
1511 { FUNC_KEY(wep_key1) },
1512 { FUNC_KEY(wep_key2) },
1513 { FUNC_KEY(wep_key3) },
1514 { INT(wep_tx_keyidx) },
1516 #ifdef IEEE8021X_EAPOL
1517 { INT(eap_workaround) },
1519 { INTe(fragment_size) },
1520 #endif /* IEEE8021X_EAPOL */
1521 { INT_RANGE(mode, 0, 4) },
1522 { INT_RANGE(proactive_key_caching, 0, 1) },
1523 { INT_RANGE(disabled, 0, 2) },
1525 #ifdef CONFIG_IEEE80211W
1526 { INT_RANGE(ieee80211w, 0, 2) },
1527 #endif /* CONFIG_IEEE80211W */
1528 { INT_RANGE(peerkey, 0, 1) },
1529 { INT_RANGE(mixed_cell, 0, 1) },
1530 { INT_RANGE(frequency, 0, 65000) },
1531 { INT(wpa_ptk_rekey) },
1533 { INT_RANGE(ignore_broadcast_ssid, 0, 2) },
1535 { FUNC(p2p_client_list) },
1536 #endif /* CONFIG_P2P */
1537 #ifdef CONFIG_HT_OVERRIDES
1538 { INT_RANGE(disable_ht, 0, 1) },
1539 { INT_RANGE(disable_ht40, -1, 1) },
1540 { INT_RANGE(disable_sgi, 0, 1) },
1541 { INT_RANGE(disable_max_amsdu, -1, 1) },
1542 { INT_RANGE(ampdu_factor, -1, 3) },
1543 { INT_RANGE(ampdu_density, -1, 7) },
1545 #endif /* CONFIG_HT_OVERRIDES */
1546 { INT(ap_max_inactivity) },
1547 { INT(dtim_period) },
1559 #undef STR_RANGE_KEY
1566 #define NUM_SSID_FIELDS (sizeof(ssid_fields) / sizeof(ssid_fields[0]))
1570 * wpa_config_add_prio_network - Add a network to priority lists
1571 * @config: Configuration data from wpa_config_read()
1572 * @ssid: Pointer to the network configuration to be added to the list
1573 * Returns: 0 on success, -1 on failure
1575 * This function is used to add a network block to the priority list of
1576 * networks. This must be called for each network when reading in the full
1577 * configuration. In addition, this can be used indirectly when updating
1578 * priorities by calling wpa_config_update_prio_list().
1580 int wpa_config_add_prio_network(struct wpa_config *config,
1581 struct wpa_ssid *ssid)
1584 struct wpa_ssid *prev, **nlist;
1587 * Add to an existing priority list if one is available for the
1588 * configured priority level for this network.
1590 for (prio = 0; prio < config->num_prio; prio++) {
1591 prev = config->pssid[prio];
1592 if (prev->priority == ssid->priority) {
1600 /* First network for this priority - add a new priority list */
1601 nlist = os_realloc_array(config->pssid, config->num_prio + 1,
1602 sizeof(struct wpa_ssid *));
1606 for (prio = 0; prio < config->num_prio; prio++) {
1607 if (nlist[prio]->priority < ssid->priority) {
1608 os_memmove(&nlist[prio + 1], &nlist[prio],
1609 (config->num_prio - prio) *
1610 sizeof(struct wpa_ssid *));
1617 config->pssid = nlist;
1624 * wpa_config_update_prio_list - Update network priority list
1625 * @config: Configuration data from wpa_config_read()
1626 * Returns: 0 on success, -1 on failure
1628 * This function is called to update the priority list of networks in the
1629 * configuration when a network is being added or removed. This is also called
1630 * if a priority for a network is changed.
1632 int wpa_config_update_prio_list(struct wpa_config *config)
1634 struct wpa_ssid *ssid;
1637 os_free(config->pssid);
1638 config->pssid = NULL;
1639 config->num_prio = 0;
1641 ssid = config->ssid;
1644 if (wpa_config_add_prio_network(config, ssid) < 0)
1653 #ifdef IEEE8021X_EAPOL
1654 static void eap_peer_config_free(struct eap_peer_config *eap)
1656 os_free(eap->eap_methods);
1657 os_free(eap->identity);
1658 os_free(eap->anonymous_identity);
1659 os_free(eap->password);
1660 os_free(eap->ca_cert);
1661 os_free(eap->ca_path);
1662 os_free(eap->client_cert);
1663 os_free(eap->private_key);
1664 os_free(eap->private_key_passwd);
1665 os_free(eap->dh_file);
1666 os_free(eap->subject_match);
1667 os_free(eap->altsubject_match);
1668 os_free(eap->ca_cert2);
1669 os_free(eap->ca_path2);
1670 os_free(eap->client_cert2);
1671 os_free(eap->private_key2);
1672 os_free(eap->private_key2_passwd);
1673 os_free(eap->dh_file2);
1674 os_free(eap->subject_match2);
1675 os_free(eap->altsubject_match2);
1676 os_free(eap->phase1);
1677 os_free(eap->phase2);
1680 os_free(eap->engine_id);
1681 os_free(eap->key_id);
1682 os_free(eap->cert_id);
1683 os_free(eap->ca_cert_id);
1684 os_free(eap->key2_id);
1685 os_free(eap->cert2_id);
1686 os_free(eap->ca_cert2_id);
1688 os_free(eap->engine2_id);
1690 os_free(eap->pending_req_otp);
1691 os_free(eap->pac_file);
1692 os_free(eap->new_password);
1694 #endif /* IEEE8021X_EAPOL */
1698 * wpa_config_free_ssid - Free network/ssid configuration data
1699 * @ssid: Configuration data for the network
1701 * This function frees all resources allocated for the network configuration
1704 void wpa_config_free_ssid(struct wpa_ssid *ssid)
1706 os_free(ssid->ssid);
1707 os_free(ssid->passphrase);
1708 os_free(ssid->ext_psk);
1709 #ifdef IEEE8021X_EAPOL
1710 eap_peer_config_free(&ssid->eap);
1711 #endif /* IEEE8021X_EAPOL */
1712 os_free(ssid->id_str);
1713 os_free(ssid->scan_freq);
1714 os_free(ssid->freq_list);
1715 os_free(ssid->bgscan);
1716 os_free(ssid->p2p_client_list);
1717 #ifdef CONFIG_HT_OVERRIDES
1718 os_free(ssid->ht_mcs);
1719 #endif /* CONFIG_HT_OVERRIDES */
1724 void wpa_config_free_cred(struct wpa_cred *cred)
1726 os_free(cred->realm);
1727 os_free(cred->username);
1728 os_free(cred->password);
1729 os_free(cred->ca_cert);
1730 os_free(cred->client_cert);
1731 os_free(cred->private_key);
1732 os_free(cred->private_key_passwd);
1733 os_free(cred->imsi);
1734 os_free(cred->milenage);
1735 os_free(cred->domain);
1736 os_free(cred->eap_method);
1737 os_free(cred->phase1);
1738 os_free(cred->phase2);
1739 os_free(cred->excluded_ssid);
1745 * wpa_config_free - Free configuration data
1746 * @config: Configuration data from wpa_config_read()
1748 * This function frees all resources allocated for the configuration data by
1749 * wpa_config_read().
1751 void wpa_config_free(struct wpa_config *config)
1753 #ifndef CONFIG_NO_CONFIG_BLOBS
1754 struct wpa_config_blob *blob, *prevblob;
1755 #endif /* CONFIG_NO_CONFIG_BLOBS */
1756 struct wpa_ssid *ssid, *prev = NULL;
1757 struct wpa_cred *cred, *cprev;
1759 ssid = config->ssid;
1763 wpa_config_free_ssid(prev);
1766 cred = config->cred;
1770 wpa_config_free_cred(cprev);
1773 #ifndef CONFIG_NO_CONFIG_BLOBS
1774 blob = config->blobs;
1779 wpa_config_free_blob(prevblob);
1781 #endif /* CONFIG_NO_CONFIG_BLOBS */
1783 wpabuf_free(config->wps_vendor_ext_m1);
1784 os_free(config->ctrl_interface);
1785 os_free(config->ctrl_interface_group);
1786 os_free(config->opensc_engine_path);
1787 os_free(config->pkcs11_engine_path);
1788 os_free(config->pkcs11_module_path);
1789 os_free(config->pcsc_reader);
1790 os_free(config->pcsc_pin);
1791 os_free(config->driver_param);
1792 os_free(config->device_name);
1793 os_free(config->manufacturer);
1794 os_free(config->model_name);
1795 os_free(config->model_number);
1796 os_free(config->serial_number);
1797 os_free(config->config_methods);
1798 os_free(config->p2p_ssid_postfix);
1799 os_free(config->pssid);
1800 os_free(config->p2p_pref_chan);
1801 os_free(config->autoscan);
1802 wpabuf_free(config->wps_nfc_dh_pubkey);
1803 wpabuf_free(config->wps_nfc_dh_privkey);
1804 wpabuf_free(config->wps_nfc_dev_pw);
1805 os_free(config->ext_password_backend);
1806 os_free(config->sae_groups);
1812 * wpa_config_foreach_network - Iterate over each configured network
1813 * @config: Configuration data from wpa_config_read()
1814 * @func: Callback function to process each network
1815 * @arg: Opaque argument to pass to callback function
1817 * Iterate over the set of configured networks calling the specified
1818 * function for each item. We guard against callbacks removing the
1821 void wpa_config_foreach_network(struct wpa_config *config,
1822 void (*func)(void *, struct wpa_ssid *),
1825 struct wpa_ssid *ssid, *next;
1827 ssid = config->ssid;
1837 * wpa_config_get_network - Get configured network based on id
1838 * @config: Configuration data from wpa_config_read()
1839 * @id: Unique network id to search for
1840 * Returns: Network configuration or %NULL if not found
1842 struct wpa_ssid * wpa_config_get_network(struct wpa_config *config, int id)
1844 struct wpa_ssid *ssid;
1846 ssid = config->ssid;
1858 * wpa_config_add_network - Add a new network with empty configuration
1859 * @config: Configuration data from wpa_config_read()
1860 * Returns: The new network configuration or %NULL if operation failed
1862 struct wpa_ssid * wpa_config_add_network(struct wpa_config *config)
1865 struct wpa_ssid *ssid, *last = NULL;
1868 ssid = config->ssid;
1877 ssid = os_zalloc(sizeof(*ssid));
1884 config->ssid = ssid;
1886 wpa_config_update_prio_list(config);
1893 * wpa_config_remove_network - Remove a configured network based on id
1894 * @config: Configuration data from wpa_config_read()
1895 * @id: Unique network id to search for
1896 * Returns: 0 on success, or -1 if the network was not found
1898 int wpa_config_remove_network(struct wpa_config *config, int id)
1900 struct wpa_ssid *ssid, *prev = NULL;
1902 ssid = config->ssid;
1914 prev->next = ssid->next;
1916 config->ssid = ssid->next;
1918 wpa_config_update_prio_list(config);
1919 wpa_config_free_ssid(ssid);
1925 * wpa_config_set_network_defaults - Set network default values
1926 * @ssid: Pointer to network configuration data
1928 void wpa_config_set_network_defaults(struct wpa_ssid *ssid)
1930 ssid->proto = DEFAULT_PROTO;
1931 ssid->pairwise_cipher = DEFAULT_PAIRWISE;
1932 ssid->group_cipher = DEFAULT_GROUP;
1933 ssid->key_mgmt = DEFAULT_KEY_MGMT;
1934 ssid->bg_scan_period = DEFAULT_BG_SCAN_PERIOD;
1935 #ifdef IEEE8021X_EAPOL
1936 ssid->eapol_flags = DEFAULT_EAPOL_FLAGS;
1937 ssid->eap_workaround = DEFAULT_EAP_WORKAROUND;
1938 ssid->eap.fragment_size = DEFAULT_FRAGMENT_SIZE;
1939 #endif /* IEEE8021X_EAPOL */
1940 #ifdef CONFIG_HT_OVERRIDES
1941 ssid->disable_ht = DEFAULT_DISABLE_HT;
1942 ssid->disable_ht40 = DEFAULT_DISABLE_HT40;
1943 ssid->disable_sgi = DEFAULT_DISABLE_SGI;
1944 ssid->disable_max_amsdu = DEFAULT_DISABLE_MAX_AMSDU;
1945 ssid->ampdu_factor = DEFAULT_AMPDU_FACTOR;
1946 ssid->ampdu_density = DEFAULT_AMPDU_DENSITY;
1947 #endif /* CONFIG_HT_OVERRIDES */
1948 ssid->proactive_key_caching = -1;
1949 #ifdef CONFIG_IEEE80211W
1950 ssid->ieee80211w = MGMT_FRAME_PROTECTION_DEFAULT;
1951 #endif /* CONFIG_IEEE80211W */
1956 * wpa_config_set - Set a variable in network configuration
1957 * @ssid: Pointer to network configuration data
1958 * @var: Variable name, e.g., "ssid"
1959 * @value: Variable value
1960 * @line: Line number in configuration file or 0 if not used
1961 * Returns: 0 on success, -1 on failure
1963 * This function can be used to set network configuration variables based on
1964 * both the configuration file and management interface input. The value
1965 * parameter must be in the same format as the text-based configuration file is
1966 * using. For example, strings are using double quotation marks.
1968 int wpa_config_set(struct wpa_ssid *ssid, const char *var, const char *value,
1974 if (ssid == NULL || var == NULL || value == NULL)
1977 for (i = 0; i < NUM_SSID_FIELDS; i++) {
1978 const struct parse_data *field = &ssid_fields[i];
1979 if (os_strcmp(var, field->name) != 0)
1982 if (field->parser(field, ssid, line, value)) {
1984 wpa_printf(MSG_ERROR, "Line %d: failed to "
1985 "parse %s '%s'.", line, var, value);
1991 if (i == NUM_SSID_FIELDS) {
1993 wpa_printf(MSG_ERROR, "Line %d: unknown network field "
1994 "'%s'.", line, var);
2003 int wpa_config_set_quoted(struct wpa_ssid *ssid, const char *var,
2010 len = os_strlen(value);
2011 buf = os_malloc(len + 3);
2015 os_memcpy(buf + 1, value, len);
2017 buf[len + 2] = '\0';
2018 ret = wpa_config_set(ssid, var, buf, 0);
2025 * wpa_config_get_all - Get all options from network configuration
2026 * @ssid: Pointer to network configuration data
2027 * @get_keys: Determines if keys/passwords will be included in returned list
2028 * (if they may be exported)
2029 * Returns: %NULL terminated list of all set keys and their values in the form
2030 * of [key1, val1, key2, val2, ... , NULL]
2032 * This function can be used to get list of all configured network properties.
2033 * The caller is responsible for freeing the returned list and all its
2036 char ** wpa_config_get_all(struct wpa_ssid *ssid, int get_keys)
2038 const struct parse_data *field;
2044 get_keys = get_keys && ssid->export_keys;
2046 props = os_calloc(2 * NUM_SSID_FIELDS + 1, sizeof(char *));
2051 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2052 field = &ssid_fields[i];
2053 if (field->key_data && !get_keys)
2055 value = field->writer(field, ssid);
2058 if (os_strlen(value) == 0) {
2063 key = os_strdup(field->name);
2069 props[fields_num * 2] = key;
2070 props[fields_num * 2 + 1] = value;
2086 #ifndef NO_CONFIG_WRITE
2088 * wpa_config_get - Get a variable in network configuration
2089 * @ssid: Pointer to network configuration data
2090 * @var: Variable name, e.g., "ssid"
2091 * Returns: Value of the variable or %NULL on failure
2093 * This function can be used to get network configuration variables. The
2094 * returned value is a copy of the configuration variable in text format, i.e,.
2095 * the same format that the text-based configuration file and wpa_config_set()
2096 * are using for the value. The caller is responsible for freeing the returned
2099 char * wpa_config_get(struct wpa_ssid *ssid, const char *var)
2103 if (ssid == NULL || var == NULL)
2106 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2107 const struct parse_data *field = &ssid_fields[i];
2108 if (os_strcmp(var, field->name) == 0)
2109 return field->writer(field, ssid);
2117 * wpa_config_get_no_key - Get a variable in network configuration (no keys)
2118 * @ssid: Pointer to network configuration data
2119 * @var: Variable name, e.g., "ssid"
2120 * Returns: Value of the variable or %NULL on failure
2122 * This function can be used to get network configuration variable like
2123 * wpa_config_get(). The only difference is that this functions does not expose
2124 * key/password material from the configuration. In case a key/password field
2125 * is requested, the returned value is an empty string or %NULL if the variable
2126 * is not set or "*" if the variable is set (regardless of its value). The
2127 * returned value is a copy of the configuration variable in text format, i.e,.
2128 * the same format that the text-based configuration file and wpa_config_set()
2129 * are using for the value. The caller is responsible for freeing the returned
2132 char * wpa_config_get_no_key(struct wpa_ssid *ssid, const char *var)
2136 if (ssid == NULL || var == NULL)
2139 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2140 const struct parse_data *field = &ssid_fields[i];
2141 if (os_strcmp(var, field->name) == 0) {
2142 char *res = field->writer(field, ssid);
2143 if (field->key_data) {
2144 if (res && res[0]) {
2145 wpa_printf(MSG_DEBUG, "Do not allow "
2146 "key_data field to be "
2149 return os_strdup("*");
2161 #endif /* NO_CONFIG_WRITE */
2165 * wpa_config_update_psk - Update WPA PSK based on passphrase and SSID
2166 * @ssid: Pointer to network configuration data
2168 * This function must be called to update WPA PSK when either SSID or the
2169 * passphrase has changed for the network configuration.
2171 void wpa_config_update_psk(struct wpa_ssid *ssid)
2173 #ifndef CONFIG_NO_PBKDF2
2174 pbkdf2_sha1(ssid->passphrase, ssid->ssid, ssid->ssid_len, 4096,
2175 ssid->psk, PMK_LEN);
2176 wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
2177 ssid->psk, PMK_LEN);
2179 #endif /* CONFIG_NO_PBKDF2 */
2183 int wpa_config_set_cred(struct wpa_cred *cred, const char *var,
2184 const char *value, int line)
2189 if (os_strcmp(var, "priority") == 0) {
2190 cred->priority = atoi(value);
2194 if (os_strcmp(var, "pcsc") == 0) {
2195 cred->pcsc = atoi(value);
2199 if (os_strcmp(var, "eap") == 0) {
2200 struct eap_method_type method;
2201 method.method = eap_peer_get_type(value, &method.vendor);
2202 if (method.vendor == EAP_VENDOR_IETF &&
2203 method.method == EAP_TYPE_NONE) {
2204 wpa_printf(MSG_ERROR, "Line %d: unknown EAP type '%s' "
2205 "for a credential", line, value);
2208 os_free(cred->eap_method);
2209 cred->eap_method = os_malloc(sizeof(*cred->eap_method));
2210 if (cred->eap_method == NULL)
2212 os_memcpy(cred->eap_method, &method, sizeof(method));
2216 if (os_strcmp(var, "password") == 0 &&
2217 os_strncmp(value, "ext:", 4) == 0) {
2218 os_free(cred->password);
2219 cred->password = os_strdup(value);
2220 cred->ext_password = 1;
2224 val = wpa_config_parse_string(value, &len);
2226 wpa_printf(MSG_ERROR, "Line %d: invalid field '%s' string "
2227 "value '%s'.", line, var, value);
2231 if (os_strcmp(var, "realm") == 0) {
2232 os_free(cred->realm);
2237 if (os_strcmp(var, "username") == 0) {
2238 os_free(cred->username);
2239 cred->username = val;
2243 if (os_strcmp(var, "password") == 0) {
2244 os_free(cred->password);
2245 cred->password = val;
2246 cred->ext_password = 0;
2250 if (os_strcmp(var, "ca_cert") == 0) {
2251 os_free(cred->ca_cert);
2252 cred->ca_cert = val;
2256 if (os_strcmp(var, "client_cert") == 0) {
2257 os_free(cred->client_cert);
2258 cred->client_cert = val;
2262 if (os_strcmp(var, "private_key") == 0) {
2263 os_free(cred->private_key);
2264 cred->private_key = val;
2268 if (os_strcmp(var, "private_key_passwd") == 0) {
2269 os_free(cred->private_key_passwd);
2270 cred->private_key_passwd = val;
2274 if (os_strcmp(var, "imsi") == 0) {
2275 os_free(cred->imsi);
2280 if (os_strcmp(var, "milenage") == 0) {
2281 os_free(cred->milenage);
2282 cred->milenage = val;
2286 if (os_strcmp(var, "domain") == 0) {
2287 os_free(cred->domain);
2292 if (os_strcmp(var, "phase1") == 0) {
2293 os_free(cred->phase1);
2298 if (os_strcmp(var, "phase2") == 0) {
2299 os_free(cred->phase2);
2304 if (os_strcmp(var, "roaming_consortium") == 0) {
2305 if (len < 3 || len > sizeof(cred->roaming_consortium)) {
2306 wpa_printf(MSG_ERROR, "Line %d: invalid "
2307 "roaming_consortium length %d (3..15 "
2308 "expected)", line, (int) len);
2312 os_memcpy(cred->roaming_consortium, val, len);
2313 cred->roaming_consortium_len = len;
2318 if (os_strcmp(var, "excluded_ssid") == 0) {
2319 struct excluded_ssid *e;
2321 if (len > MAX_SSID_LEN) {
2322 wpa_printf(MSG_ERROR, "Line %d: invalid "
2323 "excluded_ssid length %d", line, (int) len);
2328 e = os_realloc_array(cred->excluded_ssid,
2329 cred->num_excluded_ssid + 1,
2330 sizeof(struct excluded_ssid));
2335 cred->excluded_ssid = e;
2337 e = &cred->excluded_ssid[cred->num_excluded_ssid++];
2338 os_memcpy(e->ssid, val, len);
2347 wpa_printf(MSG_ERROR, "Line %d: unknown cred field '%s'.",
2357 struct wpa_cred * wpa_config_get_cred(struct wpa_config *config, int id)
2359 struct wpa_cred *cred;
2361 cred = config->cred;
2372 struct wpa_cred * wpa_config_add_cred(struct wpa_config *config)
2375 struct wpa_cred *cred, *last = NULL;
2378 cred = config->cred;
2387 cred = os_zalloc(sizeof(*cred));
2394 config->cred = cred;
2400 int wpa_config_remove_cred(struct wpa_config *config, int id)
2402 struct wpa_cred *cred, *prev = NULL;
2404 cred = config->cred;
2416 prev->next = cred->next;
2418 config->cred = cred->next;
2420 wpa_config_free_cred(cred);
2425 #ifndef CONFIG_NO_CONFIG_BLOBS
2427 * wpa_config_get_blob - Get a named configuration blob
2428 * @config: Configuration data from wpa_config_read()
2429 * @name: Name of the blob
2430 * Returns: Pointer to blob data or %NULL if not found
2432 const struct wpa_config_blob * wpa_config_get_blob(struct wpa_config *config,
2435 struct wpa_config_blob *blob = config->blobs;
2438 if (os_strcmp(blob->name, name) == 0)
2447 * wpa_config_set_blob - Set or add a named configuration blob
2448 * @config: Configuration data from wpa_config_read()
2449 * @blob: New value for the blob
2451 * Adds a new configuration blob or replaces the current value of an existing
2454 void wpa_config_set_blob(struct wpa_config *config,
2455 struct wpa_config_blob *blob)
2457 wpa_config_remove_blob(config, blob->name);
2458 blob->next = config->blobs;
2459 config->blobs = blob;
2464 * wpa_config_free_blob - Free blob data
2465 * @blob: Pointer to blob to be freed
2467 void wpa_config_free_blob(struct wpa_config_blob *blob)
2470 os_free(blob->name);
2471 os_free(blob->data);
2478 * wpa_config_remove_blob - Remove a named configuration blob
2479 * @config: Configuration data from wpa_config_read()
2480 * @name: Name of the blob to remove
2481 * Returns: 0 if blob was removed or -1 if blob was not found
2483 int wpa_config_remove_blob(struct wpa_config *config, const char *name)
2485 struct wpa_config_blob *pos = config->blobs, *prev = NULL;
2488 if (os_strcmp(pos->name, name) == 0) {
2490 prev->next = pos->next;
2492 config->blobs = pos->next;
2493 wpa_config_free_blob(pos);
2502 #endif /* CONFIG_NO_CONFIG_BLOBS */
2506 * wpa_config_alloc_empty - Allocate an empty configuration
2507 * @ctrl_interface: Control interface parameters, e.g., path to UNIX domain
2509 * @driver_param: Driver parameters
2510 * Returns: Pointer to allocated configuration data or %NULL on failure
2512 struct wpa_config * wpa_config_alloc_empty(const char *ctrl_interface,
2513 const char *driver_param)
2515 struct wpa_config *config;
2516 const int aCWmin = 4, aCWmax = 10;
2517 const struct hostapd_wmm_ac_params ac_bk =
2518 { aCWmin, aCWmax, 7, 0, 0 }; /* background traffic */
2519 const struct hostapd_wmm_ac_params ac_be =
2520 { aCWmin, aCWmax, 3, 0, 0 }; /* best effort traffic */
2521 const struct hostapd_wmm_ac_params ac_vi = /* video traffic */
2522 { aCWmin - 1, aCWmin, 2, 3000 / 32, 0 };
2523 const struct hostapd_wmm_ac_params ac_vo = /* voice traffic */
2524 { aCWmin - 2, aCWmin - 1, 2, 1500 / 32, 0 };
2526 config = os_zalloc(sizeof(*config));
2529 config->eapol_version = DEFAULT_EAPOL_VERSION;
2530 config->ap_scan = DEFAULT_AP_SCAN;
2531 config->fast_reauth = DEFAULT_FAST_REAUTH;
2532 config->p2p_go_intent = DEFAULT_P2P_GO_INTENT;
2533 config->p2p_intra_bss = DEFAULT_P2P_INTRA_BSS;
2534 config->p2p_go_max_inactivity = DEFAULT_P2P_GO_MAX_INACTIVITY;
2535 config->bss_max_count = DEFAULT_BSS_MAX_COUNT;
2536 config->bss_expiration_age = DEFAULT_BSS_EXPIRATION_AGE;
2537 config->bss_expiration_scan_count = DEFAULT_BSS_EXPIRATION_SCAN_COUNT;
2538 config->max_num_sta = DEFAULT_MAX_NUM_STA;
2539 config->access_network_type = DEFAULT_ACCESS_NETWORK_TYPE;
2540 config->wmm_ac_params[0] = ac_be;
2541 config->wmm_ac_params[1] = ac_bk;
2542 config->wmm_ac_params[2] = ac_vi;
2543 config->wmm_ac_params[3] = ac_vo;
2546 config->ctrl_interface = os_strdup(ctrl_interface);
2548 config->driver_param = os_strdup(driver_param);
2554 #ifndef CONFIG_NO_STDOUT_DEBUG
2556 * wpa_config_debug_dump_networks - Debug dump of configured networks
2557 * @config: Configuration data from wpa_config_read()
2559 void wpa_config_debug_dump_networks(struct wpa_config *config)
2562 struct wpa_ssid *ssid;
2564 for (prio = 0; prio < config->num_prio; prio++) {
2565 ssid = config->pssid[prio];
2566 wpa_printf(MSG_DEBUG, "Priority group %d",
2569 wpa_printf(MSG_DEBUG, " id=%d ssid='%s'",
2571 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
2576 #endif /* CONFIG_NO_STDOUT_DEBUG */
2579 struct global_parse_data {
2581 int (*parser)(const struct global_parse_data *data,
2582 struct wpa_config *config, int line, const char *value);
2583 void *param1, *param2, *param3;
2584 unsigned int changed_flag;
2588 static int wpa_global_config_parse_int(const struct global_parse_data *data,
2589 struct wpa_config *config, int line,
2593 dst = (int *) (((u8 *) config) + (long) data->param1);
2595 wpa_printf(MSG_DEBUG, "%s=%d", data->name, *dst);
2597 if (data->param2 && *dst < (long) data->param2) {
2598 wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
2599 "min_value=%ld)", line, data->name, *dst,
2600 (long) data->param2);
2601 *dst = (long) data->param2;
2605 if (data->param3 && *dst > (long) data->param3) {
2606 wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
2607 "max_value=%ld)", line, data->name, *dst,
2608 (long) data->param3);
2609 *dst = (long) data->param3;
2617 static int wpa_global_config_parse_str(const struct global_parse_data *data,
2618 struct wpa_config *config, int line,
2624 len = os_strlen(pos);
2625 if (data->param2 && len < (size_t) data->param2) {
2626 wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
2627 "min_len=%ld)", line, data->name,
2628 (unsigned long) len, (long) data->param2);
2632 if (data->param3 && len > (size_t) data->param3) {
2633 wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
2634 "max_len=%ld)", line, data->name,
2635 (unsigned long) len, (long) data->param3);
2639 tmp = os_strdup(pos);
2643 dst = (char **) (((u8 *) config) + (long) data->param1);
2646 wpa_printf(MSG_DEBUG, "%s='%s'", data->name, *dst);
2652 static int wpa_global_config_parse_bin(const struct global_parse_data *data,
2653 struct wpa_config *config, int line,
2657 struct wpabuf **dst, *tmp;
2659 len = os_strlen(pos);
2663 tmp = wpabuf_alloc(len / 2);
2667 if (hexstr2bin(pos, wpabuf_put(tmp, len / 2), len / 2)) {
2672 dst = (struct wpabuf **) (((u8 *) config) + (long) data->param1);
2675 wpa_printf(MSG_DEBUG, "%s", data->name);
2681 static int wpa_config_process_country(const struct global_parse_data *data,
2682 struct wpa_config *config, int line,
2685 if (!pos[0] || !pos[1]) {
2686 wpa_printf(MSG_DEBUG, "Invalid country set");
2689 config->country[0] = pos[0];
2690 config->country[1] = pos[1];
2691 wpa_printf(MSG_DEBUG, "country='%c%c'",
2692 config->country[0], config->country[1]);
2697 static int wpa_config_process_load_dynamic_eap(
2698 const struct global_parse_data *data, struct wpa_config *config,
2699 int line, const char *so)
2702 wpa_printf(MSG_DEBUG, "load_dynamic_eap=%s", so);
2703 ret = eap_peer_method_load(so);
2705 wpa_printf(MSG_DEBUG, "This EAP type was already loaded - not "
2708 wpa_printf(MSG_ERROR, "Line %d: Failed to load dynamic EAP "
2709 "method '%s'.", line, so);
2719 static int wpa_config_process_uuid(const struct global_parse_data *data,
2720 struct wpa_config *config, int line,
2724 if (uuid_str2bin(pos, config->uuid)) {
2725 wpa_printf(MSG_ERROR, "Line %d: invalid UUID", line);
2728 uuid_bin2str(config->uuid, buf, sizeof(buf));
2729 wpa_printf(MSG_DEBUG, "uuid=%s", buf);
2734 static int wpa_config_process_device_type(
2735 const struct global_parse_data *data,
2736 struct wpa_config *config, int line, const char *pos)
2738 return wps_dev_type_str2bin(pos, config->device_type);
2742 static int wpa_config_process_os_version(const struct global_parse_data *data,
2743 struct wpa_config *config, int line,
2746 if (hexstr2bin(pos, config->os_version, 4)) {
2747 wpa_printf(MSG_ERROR, "Line %d: invalid os_version", line);
2750 wpa_printf(MSG_DEBUG, "os_version=%08x",
2751 WPA_GET_BE32(config->os_version));
2756 static int wpa_config_process_wps_vendor_ext_m1(
2757 const struct global_parse_data *data,
2758 struct wpa_config *config, int line, const char *pos)
2761 int len = os_strlen(pos) / 2;
2765 wpa_printf(MSG_ERROR, "Line %d: "
2766 "invalid wps_vendor_ext_m1", line);
2770 tmp = wpabuf_alloc(len);
2772 p = wpabuf_put(tmp, len);
2774 if (hexstr2bin(pos, p, len)) {
2775 wpa_printf(MSG_ERROR, "Line %d: "
2776 "invalid wps_vendor_ext_m1", line);
2781 wpabuf_free(config->wps_vendor_ext_m1);
2782 config->wps_vendor_ext_m1 = tmp;
2784 wpa_printf(MSG_ERROR, "Can not allocate "
2785 "memory for wps_vendor_ext_m1");
2792 #endif /* CONFIG_WPS */
2795 static int wpa_config_process_sec_device_type(
2796 const struct global_parse_data *data,
2797 struct wpa_config *config, int line, const char *pos)
2801 if (config->num_sec_device_types >= MAX_SEC_DEVICE_TYPES) {
2802 wpa_printf(MSG_ERROR, "Line %d: too many sec_device_type "
2807 idx = config->num_sec_device_types;
2809 if (wps_dev_type_str2bin(pos, config->sec_device_type[idx]))
2812 config->num_sec_device_types++;
2817 static int wpa_config_process_p2p_pref_chan(
2818 const struct global_parse_data *data,
2819 struct wpa_config *config, int line, const char *pos)
2821 struct p2p_channel *pref = NULL, *n;
2822 unsigned int num = 0;
2826 /* format: class:chan,class:chan,... */
2829 op_class = atoi(pos);
2830 pos2 = os_strchr(pos, ':');
2836 n = os_realloc_array(pref, num + 1,
2837 sizeof(struct p2p_channel));
2841 pref[num].op_class = op_class;
2842 pref[num].chan = chan;
2845 pos = os_strchr(pos2, ',');
2851 os_free(config->p2p_pref_chan);
2852 config->p2p_pref_chan = pref;
2853 config->num_p2p_pref_chan = num;
2854 wpa_hexdump(MSG_DEBUG, "P2P: Preferred class/channel pairs",
2855 (u8 *) config->p2p_pref_chan,
2856 config->num_p2p_pref_chan * sizeof(struct p2p_channel));
2862 wpa_printf(MSG_ERROR, "Line %d: Invalid p2p_pref_chan list", line);
2865 #endif /* CONFIG_P2P */
2868 static int wpa_config_process_hessid(
2869 const struct global_parse_data *data,
2870 struct wpa_config *config, int line, const char *pos)
2872 if (hwaddr_aton2(pos, config->hessid) < 0) {
2873 wpa_printf(MSG_ERROR, "Line %d: Invalid hessid '%s'",
2882 static int wpa_config_process_sae_groups(
2883 const struct global_parse_data *data,
2884 struct wpa_config *config, int line, const char *pos)
2886 int *groups = wpa_config_parse_int_array(pos);
2887 if (groups == NULL) {
2888 wpa_printf(MSG_ERROR, "Line %d: Invalid sae_groups '%s'",
2893 os_free(config->sae_groups);
2894 config->sae_groups = groups;
2903 /* OFFSET: Get offset of a variable within the wpa_config structure */
2904 #define OFFSET(v) ((void *) &((struct wpa_config *) 0)->v)
2906 #define FUNC(f) #f, wpa_config_process_ ## f, OFFSET(f), NULL, NULL
2907 #define FUNC_NO_VAR(f) #f, wpa_config_process_ ## f, NULL, NULL, NULL
2908 #define _INT(f) #f, wpa_global_config_parse_int, OFFSET(f)
2909 #define INT(f) _INT(f), NULL, NULL
2910 #define INT_RANGE(f, min, max) _INT(f), (void *) min, (void *) max
2911 #define _STR(f) #f, wpa_global_config_parse_str, OFFSET(f)
2912 #define STR(f) _STR(f), NULL, NULL
2913 #define STR_RANGE(f, min, max) _STR(f), (void *) min, (void *) max
2914 #define BIN(f) #f, wpa_global_config_parse_bin, OFFSET(f), NULL, NULL
2916 static const struct global_parse_data global_fields[] = {
2917 #ifdef CONFIG_CTRL_IFACE
2918 { STR(ctrl_interface), 0 },
2919 { STR(ctrl_interface_group), 0 } /* deprecated */,
2920 #endif /* CONFIG_CTRL_IFACE */
2921 { INT_RANGE(eapol_version, 1, 2), 0 },
2922 { INT(ap_scan), 0 },
2923 { INT(disable_scan_offload), 0 },
2924 { INT(fast_reauth), 0 },
2925 { STR(opensc_engine_path), 0 },
2926 { STR(pkcs11_engine_path), 0 },
2927 { STR(pkcs11_module_path), 0 },
2928 { STR(pcsc_reader), 0 },
2929 { STR(pcsc_pin), 0 },
2930 { STR(driver_param), 0 },
2931 { INT(dot11RSNAConfigPMKLifetime), 0 },
2932 { INT(dot11RSNAConfigPMKReauthThreshold), 0 },
2933 { INT(dot11RSNAConfigSATimeout), 0 },
2934 #ifndef CONFIG_NO_CONFIG_WRITE
2935 { INT(update_config), 0 },
2936 #endif /* CONFIG_NO_CONFIG_WRITE */
2937 { FUNC_NO_VAR(load_dynamic_eap), 0 },
2939 { FUNC(uuid), CFG_CHANGED_UUID },
2940 { STR_RANGE(device_name, 0, 32), CFG_CHANGED_DEVICE_NAME },
2941 { STR_RANGE(manufacturer, 0, 64), CFG_CHANGED_WPS_STRING },
2942 { STR_RANGE(model_name, 0, 32), CFG_CHANGED_WPS_STRING },
2943 { STR_RANGE(model_number, 0, 32), CFG_CHANGED_WPS_STRING },
2944 { STR_RANGE(serial_number, 0, 32), CFG_CHANGED_WPS_STRING },
2945 { FUNC(device_type), CFG_CHANGED_DEVICE_TYPE },
2946 { FUNC(os_version), CFG_CHANGED_OS_VERSION },
2947 { STR(config_methods), CFG_CHANGED_CONFIG_METHODS },
2948 { INT_RANGE(wps_cred_processing, 0, 2), 0 },
2949 { FUNC(wps_vendor_ext_m1), CFG_CHANGED_VENDOR_EXTENSION },
2950 #endif /* CONFIG_WPS */
2952 { FUNC(sec_device_type), CFG_CHANGED_SEC_DEVICE_TYPE },
2953 { INT(p2p_listen_reg_class), 0 },
2954 { INT(p2p_listen_channel), 0 },
2955 { INT(p2p_oper_reg_class), 0 },
2956 { INT(p2p_oper_channel), 0 },
2957 { INT_RANGE(p2p_go_intent, 0, 15), 0 },
2958 { STR(p2p_ssid_postfix), CFG_CHANGED_P2P_SSID_POSTFIX },
2959 { INT_RANGE(persistent_reconnect, 0, 1), 0 },
2960 { INT_RANGE(p2p_intra_bss, 0, 1), CFG_CHANGED_P2P_INTRA_BSS },
2961 { INT(p2p_group_idle), 0 },
2962 { FUNC(p2p_pref_chan), CFG_CHANGED_P2P_PREF_CHAN },
2963 { INT(p2p_go_ht40), 0 },
2964 { INT(p2p_disabled), 0 },
2965 { INT(p2p_no_group_iface), 0 },
2966 #endif /* CONFIG_P2P */
2967 { FUNC(country), CFG_CHANGED_COUNTRY },
2968 { INT(bss_max_count), 0 },
2969 { INT(bss_expiration_age), 0 },
2970 { INT(bss_expiration_scan_count), 0 },
2971 { INT_RANGE(filter_ssids, 0, 1), 0 },
2972 { INT_RANGE(filter_rssi, -100, 0), 0 },
2973 { INT(max_num_sta), 0 },
2974 { INT_RANGE(disassoc_low_ack, 0, 1), 0 },
2976 { INT_RANGE(hs20, 0, 1), 0 },
2977 #endif /* CONFIG_HS20 */
2978 { INT_RANGE(interworking, 0, 1), 0 },
2979 { FUNC(hessid), 0 },
2980 { INT_RANGE(access_network_type, 0, 15), 0 },
2981 { INT_RANGE(pbc_in_m1, 0, 1), 0 },
2982 { STR(autoscan), 0 },
2983 { INT_RANGE(wps_nfc_dev_pw_id, 0x10, 0xffff),
2984 CFG_CHANGED_NFC_PASSWORD_TOKEN },
2985 { BIN(wps_nfc_dh_pubkey), CFG_CHANGED_NFC_PASSWORD_TOKEN },
2986 { BIN(wps_nfc_dh_privkey), CFG_CHANGED_NFC_PASSWORD_TOKEN },
2987 { BIN(wps_nfc_dev_pw), CFG_CHANGED_NFC_PASSWORD_TOKEN },
2988 { STR(ext_password_backend), CFG_CHANGED_EXT_PW_BACKEND },
2989 { INT(p2p_go_max_inactivity), 0 },
2990 { INT_RANGE(auto_interworking, 0, 1), 0 },
2993 { FUNC(sae_groups), 0 },
3004 #define NUM_GLOBAL_FIELDS (sizeof(global_fields) / sizeof(global_fields[0]))
3007 int wpa_config_process_global(struct wpa_config *config, char *pos, int line)
3012 for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
3013 const struct global_parse_data *field = &global_fields[i];
3014 size_t flen = os_strlen(field->name);
3015 if (os_strncmp(pos, field->name, flen) != 0 ||
3019 if (field->parser(field, config, line, pos + flen + 1)) {
3020 wpa_printf(MSG_ERROR, "Line %d: failed to "
3021 "parse '%s'.", line, pos);
3024 if (field->changed_flag == CFG_CHANGED_NFC_PASSWORD_TOKEN)
3025 config->wps_nfc_pw_from_config = 1;
3026 config->changed_parameters |= field->changed_flag;
3029 if (i == NUM_GLOBAL_FIELDS) {
3031 if (os_strncmp(pos, "wmm_ac_", 7) == 0) {
3032 char *tmp = os_strchr(pos, '=');
3036 wpa_printf(MSG_ERROR, "Line %d: invalid line "
3041 if (hostapd_config_wmm_ac(config->wmm_ac_params, pos,
3043 wpa_printf(MSG_ERROR, "Line %d: invalid WMM "
3048 #endif /* CONFIG_AP */
3051 wpa_printf(MSG_ERROR, "Line %d: unknown global field '%s'.",