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(100);
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 ret = os_snprintf(pos, end - pos, "%sFT-PSK",
607 pos == buf ? "" : " ");
608 if (ret < 0 || ret >= end - pos) {
615 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
616 ret = os_snprintf(pos, end - pos, "%sFT-EAP",
617 pos == buf ? "" : " ");
618 if (ret < 0 || ret >= end - pos) {
624 #endif /* CONFIG_IEEE80211R */
626 #ifdef CONFIG_IEEE80211W
627 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
628 ret = os_snprintf(pos, end - pos, "%sWPA-PSK-SHA256",
629 pos == buf ? "" : " ");
630 if (ret < 0 || ret >= end - pos) {
637 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
638 ret = os_snprintf(pos, end - pos, "%sWPA-EAP-SHA256",
639 pos == buf ? "" : " ");
640 if (ret < 0 || ret >= end - pos) {
646 #endif /* CONFIG_IEEE80211W */
649 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
650 ret = os_snprintf(pos, end - pos, "%sWPS",
651 pos == buf ? "" : " ");
652 if (ret < 0 || ret >= end - pos) {
658 #endif /* CONFIG_WPS */
662 #endif /* NO_CONFIG_WRITE */
665 static int wpa_config_parse_cipher(int line, const char *value)
667 int val = wpa_parse_cipher(value);
669 wpa_printf(MSG_ERROR, "Line %d: invalid cipher '%s'.",
674 wpa_printf(MSG_ERROR, "Line %d: no cipher values configured.",
682 #ifndef NO_CONFIG_WRITE
683 static char * wpa_config_write_cipher(int cipher)
685 char *buf = os_zalloc(50);
689 if (wpa_write_ciphers(buf, buf + 50, cipher, " ") < 0) {
696 #endif /* NO_CONFIG_WRITE */
699 static int wpa_config_parse_pairwise(const struct parse_data *data,
700 struct wpa_ssid *ssid, int line,
704 val = wpa_config_parse_cipher(line, value);
707 if (val & ~WPA_ALLOWED_PAIRWISE_CIPHERS) {
708 wpa_printf(MSG_ERROR, "Line %d: not allowed pairwise cipher "
709 "(0x%x).", line, val);
713 wpa_printf(MSG_MSGDUMP, "pairwise: 0x%x", val);
714 ssid->pairwise_cipher = val;
719 #ifndef NO_CONFIG_WRITE
720 static char * wpa_config_write_pairwise(const struct parse_data *data,
721 struct wpa_ssid *ssid)
723 return wpa_config_write_cipher(ssid->pairwise_cipher);
725 #endif /* NO_CONFIG_WRITE */
728 static int wpa_config_parse_group(const struct parse_data *data,
729 struct wpa_ssid *ssid, int line,
733 val = wpa_config_parse_cipher(line, value);
736 if (val & ~WPA_ALLOWED_GROUP_CIPHERS) {
737 wpa_printf(MSG_ERROR, "Line %d: not allowed group cipher "
738 "(0x%x).", line, val);
742 wpa_printf(MSG_MSGDUMP, "group: 0x%x", val);
743 ssid->group_cipher = val;
748 #ifndef NO_CONFIG_WRITE
749 static char * wpa_config_write_group(const struct parse_data *data,
750 struct wpa_ssid *ssid)
752 return wpa_config_write_cipher(ssid->group_cipher);
754 #endif /* NO_CONFIG_WRITE */
757 static int wpa_config_parse_auth_alg(const struct parse_data *data,
758 struct wpa_ssid *ssid, int line,
761 int val = 0, last, errors = 0;
762 char *start, *end, *buf;
764 buf = os_strdup(value);
769 while (*start != '\0') {
770 while (*start == ' ' || *start == '\t')
775 while (*end != ' ' && *end != '\t' && *end != '\0')
779 if (os_strcmp(start, "OPEN") == 0)
780 val |= WPA_AUTH_ALG_OPEN;
781 else if (os_strcmp(start, "SHARED") == 0)
782 val |= WPA_AUTH_ALG_SHARED;
783 else if (os_strcmp(start, "LEAP") == 0)
784 val |= WPA_AUTH_ALG_LEAP;
786 wpa_printf(MSG_ERROR, "Line %d: invalid auth_alg '%s'",
798 wpa_printf(MSG_ERROR,
799 "Line %d: no auth_alg values configured.", line);
803 wpa_printf(MSG_MSGDUMP, "auth_alg: 0x%x", val);
804 ssid->auth_alg = val;
805 return errors ? -1 : 0;
809 #ifndef NO_CONFIG_WRITE
810 static char * wpa_config_write_auth_alg(const struct parse_data *data,
811 struct wpa_ssid *ssid)
813 char *buf, *pos, *end;
816 pos = buf = os_zalloc(30);
821 if (ssid->auth_alg & WPA_AUTH_ALG_OPEN) {
822 ret = os_snprintf(pos, end - pos, "%sOPEN",
823 pos == buf ? "" : " ");
824 if (ret < 0 || ret >= end - pos) {
831 if (ssid->auth_alg & WPA_AUTH_ALG_SHARED) {
832 ret = os_snprintf(pos, end - pos, "%sSHARED",
833 pos == buf ? "" : " ");
834 if (ret < 0 || ret >= end - pos) {
841 if (ssid->auth_alg & WPA_AUTH_ALG_LEAP) {
842 ret = os_snprintf(pos, end - pos, "%sLEAP",
843 pos == buf ? "" : " ");
844 if (ret < 0 || ret >= end - pos) {
853 #endif /* NO_CONFIG_WRITE */
856 static int * wpa_config_parse_int_array(const char *value)
864 freqs = os_calloc(len + 1, sizeof(int));
875 n = os_realloc_array(freqs, len * 2 + 1, sizeof(int));
880 for (i = len; i <= len * 2; i++)
886 freqs[used] = atoi(pos);
887 if (freqs[used] == 0)
890 pos = os_strchr(pos + 1, ' ');
897 static int wpa_config_parse_scan_freq(const struct parse_data *data,
898 struct wpa_ssid *ssid, int line,
903 freqs = wpa_config_parse_int_array(value);
910 os_free(ssid->scan_freq);
911 ssid->scan_freq = freqs;
917 static int wpa_config_parse_freq_list(const struct parse_data *data,
918 struct wpa_ssid *ssid, int line,
923 freqs = wpa_config_parse_int_array(value);
930 os_free(ssid->freq_list);
931 ssid->freq_list = freqs;
937 #ifndef NO_CONFIG_WRITE
938 static char * wpa_config_write_freqs(const struct parse_data *data,
941 char *buf, *pos, *end;
949 for (i = 0; freqs[i]; i++)
952 pos = buf = os_zalloc(10 * count + 1);
955 end = buf + 10 * count + 1;
957 for (i = 0; freqs[i]; i++) {
958 ret = os_snprintf(pos, end - pos, "%s%u",
959 i == 0 ? "" : " ", freqs[i]);
960 if (ret < 0 || ret >= end - pos) {
971 static char * wpa_config_write_scan_freq(const struct parse_data *data,
972 struct wpa_ssid *ssid)
974 return wpa_config_write_freqs(data, ssid->scan_freq);
978 static char * wpa_config_write_freq_list(const struct parse_data *data,
979 struct wpa_ssid *ssid)
981 return wpa_config_write_freqs(data, ssid->freq_list);
983 #endif /* NO_CONFIG_WRITE */
986 #ifdef IEEE8021X_EAPOL
987 static int wpa_config_parse_eap(const struct parse_data *data,
988 struct wpa_ssid *ssid, int line,
991 int last, errors = 0;
992 char *start, *end, *buf;
993 struct eap_method_type *methods = NULL, *tmp;
994 size_t num_methods = 0;
996 buf = os_strdup(value);
1001 while (*start != '\0') {
1002 while (*start == ' ' || *start == '\t')
1007 while (*end != ' ' && *end != '\t' && *end != '\0')
1009 last = *end == '\0';
1012 methods = os_realloc_array(methods, num_methods + 1,
1014 if (methods == NULL) {
1019 methods[num_methods].method = eap_peer_get_type(
1020 start, &methods[num_methods].vendor);
1021 if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
1022 methods[num_methods].method == EAP_TYPE_NONE) {
1023 wpa_printf(MSG_ERROR, "Line %d: unknown EAP method "
1024 "'%s'", line, start);
1025 wpa_printf(MSG_ERROR, "You may need to add support for"
1026 " this EAP method during wpa_supplicant\n"
1027 "build time configuration.\n"
1028 "See README for more information.");
1030 } else if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
1031 methods[num_methods].method == EAP_TYPE_LEAP)
1043 methods = os_realloc_array(methods, num_methods + 1, sizeof(*methods));
1044 if (methods == NULL) {
1048 methods[num_methods].vendor = EAP_VENDOR_IETF;
1049 methods[num_methods].method = EAP_TYPE_NONE;
1052 wpa_hexdump(MSG_MSGDUMP, "eap methods",
1053 (u8 *) methods, num_methods * sizeof(*methods));
1054 os_free(ssid->eap.eap_methods);
1055 ssid->eap.eap_methods = methods;
1056 return errors ? -1 : 0;
1060 static char * wpa_config_write_eap(const struct parse_data *data,
1061 struct wpa_ssid *ssid)
1064 char *buf, *pos, *end;
1065 const struct eap_method_type *eap_methods = ssid->eap.eap_methods;
1068 if (eap_methods == NULL)
1071 pos = buf = os_zalloc(100);
1076 for (i = 0; eap_methods[i].vendor != EAP_VENDOR_IETF ||
1077 eap_methods[i].method != EAP_TYPE_NONE; i++) {
1078 name = eap_get_name(eap_methods[i].vendor,
1079 eap_methods[i].method);
1081 ret = os_snprintf(pos, end - pos, "%s%s",
1082 pos == buf ? "" : " ", name);
1083 if (ret < 0 || ret >= end - pos)
1095 static int wpa_config_parse_password(const struct parse_data *data,
1096 struct wpa_ssid *ssid, int line,
1101 if (os_strcmp(value, "NULL") == 0) {
1102 wpa_printf(MSG_DEBUG, "Unset configuration string 'password'");
1103 os_free(ssid->eap.password);
1104 ssid->eap.password = NULL;
1105 ssid->eap.password_len = 0;
1109 #ifdef CONFIG_EXT_PASSWORD
1110 if (os_strncmp(value, "ext:", 4) == 0) {
1111 char *name = os_strdup(value + 4);
1114 os_free(ssid->eap.password);
1115 ssid->eap.password = (u8 *) name;
1116 ssid->eap.password_len = os_strlen(name);
1117 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
1118 ssid->eap.flags |= EAP_CONFIG_FLAGS_EXT_PASSWORD;
1121 #endif /* CONFIG_EXT_PASSWORD */
1123 if (os_strncmp(value, "hash:", 5) != 0) {
1127 tmp = wpa_config_parse_string(value, &res_len);
1129 wpa_printf(MSG_ERROR, "Line %d: failed to parse "
1133 wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
1134 (u8 *) tmp, res_len);
1136 os_free(ssid->eap.password);
1137 ssid->eap.password = (u8 *) tmp;
1138 ssid->eap.password_len = res_len;
1139 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
1140 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_PASSWORD;
1146 /* NtPasswordHash: hash:<32 hex digits> */
1147 if (os_strlen(value + 5) != 2 * 16) {
1148 wpa_printf(MSG_ERROR, "Line %d: Invalid password hash length "
1149 "(expected 32 hex digits)", line);
1153 hash = os_malloc(16);
1157 if (hexstr2bin(value + 5, hash, 16)) {
1159 wpa_printf(MSG_ERROR, "Line %d: Invalid password hash", line);
1163 wpa_hexdump_key(MSG_MSGDUMP, data->name, hash, 16);
1165 os_free(ssid->eap.password);
1166 ssid->eap.password = hash;
1167 ssid->eap.password_len = 16;
1168 ssid->eap.flags |= EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
1169 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_PASSWORD;
1175 static char * wpa_config_write_password(const struct parse_data *data,
1176 struct wpa_ssid *ssid)
1180 if (ssid->eap.password == NULL)
1183 #ifdef CONFIG_EXT_PASSWORD
1184 if (ssid->eap.flags & EAP_CONFIG_FLAGS_EXT_PASSWORD) {
1185 buf = os_zalloc(4 + ssid->eap.password_len + 1);
1188 os_memcpy(buf, "ext:", 4);
1189 os_memcpy(buf + 4, ssid->eap.password, ssid->eap.password_len);
1192 #endif /* CONFIG_EXT_PASSWORD */
1194 if (!(ssid->eap.flags & EAP_CONFIG_FLAGS_PASSWORD_NTHASH)) {
1195 return wpa_config_write_string(
1196 ssid->eap.password, ssid->eap.password_len);
1199 buf = os_malloc(5 + 32 + 1);
1203 os_memcpy(buf, "hash:", 5);
1204 wpa_snprintf_hex(buf + 5, 32 + 1, ssid->eap.password, 16);
1208 #endif /* IEEE8021X_EAPOL */
1211 static int wpa_config_parse_wep_key(u8 *key, size_t *len, int line,
1212 const char *value, int idx)
1214 char *buf, title[20];
1217 buf = wpa_config_parse_string(value, len);
1219 wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key %d '%s'.",
1223 if (*len > MAX_WEP_KEY_LEN) {
1224 wpa_printf(MSG_ERROR, "Line %d: Too long WEP key %d '%s'.",
1229 if (*len && *len != 5 && *len != 13 && *len != 16) {
1230 wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key length %u - "
1231 "this network block will be ignored",
1232 line, (unsigned int) *len);
1234 os_memcpy(key, buf, *len);
1236 res = os_snprintf(title, sizeof(title), "wep_key%d", idx);
1237 if (res >= 0 && (size_t) res < sizeof(title))
1238 wpa_hexdump_key(MSG_MSGDUMP, title, key, *len);
1243 static int wpa_config_parse_wep_key0(const struct parse_data *data,
1244 struct wpa_ssid *ssid, int line,
1247 return wpa_config_parse_wep_key(ssid->wep_key[0],
1248 &ssid->wep_key_len[0], line,
1253 static int wpa_config_parse_wep_key1(const struct parse_data *data,
1254 struct wpa_ssid *ssid, int line,
1257 return wpa_config_parse_wep_key(ssid->wep_key[1],
1258 &ssid->wep_key_len[1], line,
1263 static int wpa_config_parse_wep_key2(const struct parse_data *data,
1264 struct wpa_ssid *ssid, int line,
1267 return wpa_config_parse_wep_key(ssid->wep_key[2],
1268 &ssid->wep_key_len[2], line,
1273 static int wpa_config_parse_wep_key3(const struct parse_data *data,
1274 struct wpa_ssid *ssid, int line,
1277 return wpa_config_parse_wep_key(ssid->wep_key[3],
1278 &ssid->wep_key_len[3], line,
1283 #ifndef NO_CONFIG_WRITE
1284 static char * wpa_config_write_wep_key(struct wpa_ssid *ssid, int idx)
1286 if (ssid->wep_key_len[idx] == 0)
1288 return wpa_config_write_string(ssid->wep_key[idx],
1289 ssid->wep_key_len[idx]);
1293 static char * wpa_config_write_wep_key0(const struct parse_data *data,
1294 struct wpa_ssid *ssid)
1296 return wpa_config_write_wep_key(ssid, 0);
1300 static char * wpa_config_write_wep_key1(const struct parse_data *data,
1301 struct wpa_ssid *ssid)
1303 return wpa_config_write_wep_key(ssid, 1);
1307 static char * wpa_config_write_wep_key2(const struct parse_data *data,
1308 struct wpa_ssid *ssid)
1310 return wpa_config_write_wep_key(ssid, 2);
1314 static char * wpa_config_write_wep_key3(const struct parse_data *data,
1315 struct wpa_ssid *ssid)
1317 return wpa_config_write_wep_key(ssid, 3);
1319 #endif /* NO_CONFIG_WRITE */
1324 static int wpa_config_parse_go_p2p_dev_addr(const struct parse_data *data,
1325 struct wpa_ssid *ssid, int line,
1328 if (value[0] == '\0' || os_strcmp(value, "\"\"") == 0 ||
1329 os_strcmp(value, "any") == 0) {
1330 os_memset(ssid->go_p2p_dev_addr, 0, ETH_ALEN);
1331 wpa_printf(MSG_MSGDUMP, "GO P2P Device Address any");
1334 if (hwaddr_aton(value, ssid->go_p2p_dev_addr)) {
1335 wpa_printf(MSG_ERROR, "Line %d: Invalid GO P2P Device Address '%s'.",
1339 ssid->bssid_set = 1;
1340 wpa_printf(MSG_MSGDUMP, "GO P2P Device Address " MACSTR,
1341 MAC2STR(ssid->go_p2p_dev_addr));
1346 #ifndef NO_CONFIG_WRITE
1347 static char * wpa_config_write_go_p2p_dev_addr(const struct parse_data *data,
1348 struct wpa_ssid *ssid)
1353 if (is_zero_ether_addr(ssid->go_p2p_dev_addr))
1356 value = os_malloc(20);
1359 res = os_snprintf(value, 20, MACSTR, MAC2STR(ssid->go_p2p_dev_addr));
1360 if (res < 0 || res >= 20) {
1364 value[20 - 1] = '\0';
1367 #endif /* NO_CONFIG_WRITE */
1370 static int wpa_config_parse_p2p_client_list(const struct parse_data *data,
1371 struct wpa_ssid *ssid, int line,
1375 u8 *buf, *n, addr[ETH_ALEN];
1382 while (pos && *pos) {
1386 if (hwaddr_aton(pos, addr)) {
1388 wpa_printf(MSG_ERROR, "Line %d: Invalid "
1389 "p2p_client_list address '%s'.",
1394 /* continue anyway since this could have been from a
1395 * truncated configuration file line */
1396 wpa_printf(MSG_INFO, "Line %d: Ignore likely "
1397 "truncated p2p_client_list address '%s'",
1400 n = os_realloc_array(buf, count + 1, ETH_ALEN);
1406 os_memmove(buf + ETH_ALEN, buf, count * ETH_ALEN);
1407 os_memcpy(buf, addr, ETH_ALEN);
1409 wpa_hexdump(MSG_MSGDUMP, "p2p_client_list",
1413 pos = os_strchr(pos, ' ');
1416 os_free(ssid->p2p_client_list);
1417 ssid->p2p_client_list = buf;
1418 ssid->num_p2p_clients = count;
1424 #ifndef NO_CONFIG_WRITE
1425 static char * wpa_config_write_p2p_client_list(const struct parse_data *data,
1426 struct wpa_ssid *ssid)
1428 char *value, *end, *pos;
1432 if (ssid->p2p_client_list == NULL || ssid->num_p2p_clients == 0)
1435 value = os_malloc(20 * ssid->num_p2p_clients);
1439 end = value + 20 * ssid->num_p2p_clients;
1441 for (i = ssid->num_p2p_clients; i > 0; i--) {
1442 res = os_snprintf(pos, end - pos, MACSTR " ",
1443 MAC2STR(ssid->p2p_client_list +
1444 (i - 1) * ETH_ALEN));
1445 if (res < 0 || res >= end - pos) {
1457 #endif /* NO_CONFIG_WRITE */
1460 static int wpa_config_parse_psk_list(const struct parse_data *data,
1461 struct wpa_ssid *ssid, int line,
1464 struct psk_list_entry *p;
1467 p = os_zalloc(sizeof(*p));
1472 if (os_strncmp(pos, "P2P-", 4) == 0) {
1477 if (hwaddr_aton(pos, p->addr)) {
1478 wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list address '%s'",
1485 wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list '%s'",
1492 if (hexstr2bin(pos, p->psk, PMK_LEN) || pos[PMK_LEN * 2] != '\0') {
1493 wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list PSK '%s'",
1499 dl_list_add(&ssid->psk_list, &p->list);
1505 #ifndef NO_CONFIG_WRITE
1506 static char * wpa_config_write_psk_list(const struct parse_data *data,
1507 struct wpa_ssid *ssid)
1511 #endif /* NO_CONFIG_WRITE */
1513 #endif /* CONFIG_P2P */
1515 /* Helper macros for network block parser */
1520 /* OFFSET: Get offset of a variable within the wpa_ssid structure */
1521 #define OFFSET(v) ((void *) &((struct wpa_ssid *) 0)->v)
1523 /* STR: Define a string variable for an ASCII string; f = field name */
1524 #ifdef NO_CONFIG_WRITE
1525 #define _STR(f) #f, wpa_config_parse_str, OFFSET(f)
1526 #define _STRe(f) #f, wpa_config_parse_str, OFFSET(eap.f)
1527 #else /* NO_CONFIG_WRITE */
1528 #define _STR(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(f)
1529 #define _STRe(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(eap.f)
1530 #endif /* NO_CONFIG_WRITE */
1531 #define STR(f) _STR(f), NULL, NULL, NULL, 0
1532 #define STRe(f) _STRe(f), NULL, NULL, NULL, 0
1533 #define STR_KEY(f) _STR(f), NULL, NULL, NULL, 1
1534 #define STR_KEYe(f) _STRe(f), NULL, NULL, NULL, 1
1536 /* STR_LEN: Define a string variable with a separate variable for storing the
1537 * data length. Unlike STR(), this can be used to store arbitrary binary data
1538 * (i.e., even nul termination character). */
1539 #define _STR_LEN(f) _STR(f), OFFSET(f ## _len)
1540 #define _STR_LENe(f) _STRe(f), OFFSET(eap.f ## _len)
1541 #define STR_LEN(f) _STR_LEN(f), NULL, NULL, 0
1542 #define STR_LENe(f) _STR_LENe(f), NULL, NULL, 0
1543 #define STR_LEN_KEY(f) _STR_LEN(f), NULL, NULL, 1
1545 /* STR_RANGE: Like STR_LEN(), but with minimum and maximum allowed length
1546 * explicitly specified. */
1547 #define _STR_RANGE(f, min, max) _STR_LEN(f), (void *) (min), (void *) (max)
1548 #define STR_RANGE(f, min, max) _STR_RANGE(f, min, max), 0
1549 #define STR_RANGE_KEY(f, min, max) _STR_RANGE(f, min, max), 1
1551 #ifdef NO_CONFIG_WRITE
1552 #define _INT(f) #f, wpa_config_parse_int, OFFSET(f), (void *) 0
1553 #define _INTe(f) #f, wpa_config_parse_int, OFFSET(eap.f), (void *) 0
1554 #else /* NO_CONFIG_WRITE */
1555 #define _INT(f) #f, wpa_config_parse_int, wpa_config_write_int, \
1556 OFFSET(f), (void *) 0
1557 #define _INTe(f) #f, wpa_config_parse_int, wpa_config_write_int, \
1558 OFFSET(eap.f), (void *) 0
1559 #endif /* NO_CONFIG_WRITE */
1561 /* INT: Define an integer variable */
1562 #define INT(f) _INT(f), NULL, NULL, 0
1563 #define INTe(f) _INTe(f), NULL, NULL, 0
1565 /* INT_RANGE: Define an integer variable with allowed value range */
1566 #define INT_RANGE(f, min, max) _INT(f), (void *) (min), (void *) (max), 0
1568 /* FUNC: Define a configuration variable that uses a custom function for
1569 * parsing and writing the value. */
1570 #ifdef NO_CONFIG_WRITE
1571 #define _FUNC(f) #f, wpa_config_parse_ ## f, NULL, NULL, NULL, NULL
1572 #else /* NO_CONFIG_WRITE */
1573 #define _FUNC(f) #f, wpa_config_parse_ ## f, wpa_config_write_ ## f, \
1574 NULL, NULL, NULL, NULL
1575 #endif /* NO_CONFIG_WRITE */
1576 #define FUNC(f) _FUNC(f), 0
1577 #define FUNC_KEY(f) _FUNC(f), 1
1580 * Table of network configuration variables. This table is used to parse each
1581 * network configuration variable, e.g., each line in wpa_supplicant.conf file
1582 * that is inside a network block.
1584 * This table is generated using the helper macros defined above and with
1585 * generous help from the C pre-processor. The field name is stored as a string
1586 * into .name and for STR and INT types, the offset of the target buffer within
1587 * struct wpa_ssid is stored in .param1. .param2 (if not NULL) is similar
1588 * offset to the field containing the length of the configuration variable.
1589 * .param3 and .param4 can be used to mark the allowed range (length for STR
1590 * and value for INT).
1592 * For each configuration line in wpa_supplicant.conf, the parser goes through
1593 * this table and select the entry that matches with the field name. The parser
1594 * function (.parser) is then called to parse the actual value of the field.
1596 * This kind of mechanism makes it easy to add new configuration parameters,
1597 * since only one line needs to be added into this table and into the
1598 * struct wpa_ssid definition if the new variable is either a string or
1599 * integer. More complex types will need to use their own parser and writer
1602 static const struct parse_data ssid_fields[] = {
1603 { STR_RANGE(ssid, 0, MAX_SSID_LEN) },
1604 { INT_RANGE(scan_ssid, 0, 1) },
1609 { INT(bg_scan_period) },
1613 { FUNC(scan_freq) },
1614 { FUNC(freq_list) },
1615 #ifdef IEEE8021X_EAPOL
1617 { STR_LENe(identity) },
1618 { STR_LENe(anonymous_identity) },
1619 { FUNC_KEY(password) },
1622 { STRe(client_cert) },
1623 { STRe(private_key) },
1624 { STR_KEYe(private_key_passwd) },
1626 { STRe(subject_match) },
1627 { STRe(altsubject_match) },
1628 { STRe(domain_suffix_match) },
1631 { STRe(client_cert2) },
1632 { STRe(private_key2) },
1633 { STR_KEYe(private_key2_passwd) },
1635 { STRe(subject_match2) },
1636 { STRe(altsubject_match2) },
1637 { STRe(domain_suffix_match2) },
1642 { STRe(engine_id) },
1645 { STRe(ca_cert_id) },
1647 { STRe(engine2_id) },
1650 { STRe(ca_cert2_id) },
1653 { INT(eapol_flags) },
1654 #endif /* IEEE8021X_EAPOL */
1655 { FUNC_KEY(wep_key0) },
1656 { FUNC_KEY(wep_key1) },
1657 { FUNC_KEY(wep_key2) },
1658 { FUNC_KEY(wep_key3) },
1659 { INT(wep_tx_keyidx) },
1661 #ifdef IEEE8021X_EAPOL
1662 { INT(eap_workaround) },
1664 { INTe(fragment_size) },
1666 #endif /* IEEE8021X_EAPOL */
1667 { INT_RANGE(mode, 0, 4) },
1668 { INT_RANGE(proactive_key_caching, 0, 1) },
1669 { INT_RANGE(disabled, 0, 2) },
1671 #ifdef CONFIG_IEEE80211W
1672 { INT_RANGE(ieee80211w, 0, 2) },
1673 #endif /* CONFIG_IEEE80211W */
1674 { INT_RANGE(peerkey, 0, 1) },
1675 { INT_RANGE(mixed_cell, 0, 1) },
1676 { INT_RANGE(frequency, 0, 65000) },
1677 { INT(wpa_ptk_rekey) },
1679 { INT_RANGE(ignore_broadcast_ssid, 0, 2) },
1681 { FUNC(go_p2p_dev_addr) },
1682 { FUNC(p2p_client_list) },
1684 #endif /* CONFIG_P2P */
1685 #ifdef CONFIG_HT_OVERRIDES
1686 { INT_RANGE(disable_ht, 0, 1) },
1687 { INT_RANGE(disable_ht40, -1, 1) },
1688 { INT_RANGE(disable_sgi, 0, 1) },
1689 { INT_RANGE(disable_max_amsdu, -1, 1) },
1690 { INT_RANGE(ampdu_factor, -1, 3) },
1691 { INT_RANGE(ampdu_density, -1, 7) },
1693 #endif /* CONFIG_HT_OVERRIDES */
1694 #ifdef CONFIG_VHT_OVERRIDES
1695 { INT_RANGE(disable_vht, 0, 1) },
1697 { INT(vht_capa_mask) },
1698 { INT_RANGE(vht_rx_mcs_nss_1, -1, 3) },
1699 { INT_RANGE(vht_rx_mcs_nss_2, -1, 3) },
1700 { INT_RANGE(vht_rx_mcs_nss_3, -1, 3) },
1701 { INT_RANGE(vht_rx_mcs_nss_4, -1, 3) },
1702 { INT_RANGE(vht_rx_mcs_nss_5, -1, 3) },
1703 { INT_RANGE(vht_rx_mcs_nss_6, -1, 3) },
1704 { INT_RANGE(vht_rx_mcs_nss_7, -1, 3) },
1705 { INT_RANGE(vht_rx_mcs_nss_8, -1, 3) },
1706 { INT_RANGE(vht_tx_mcs_nss_1, -1, 3) },
1707 { INT_RANGE(vht_tx_mcs_nss_2, -1, 3) },
1708 { INT_RANGE(vht_tx_mcs_nss_3, -1, 3) },
1709 { INT_RANGE(vht_tx_mcs_nss_4, -1, 3) },
1710 { INT_RANGE(vht_tx_mcs_nss_5, -1, 3) },
1711 { INT_RANGE(vht_tx_mcs_nss_6, -1, 3) },
1712 { INT_RANGE(vht_tx_mcs_nss_7, -1, 3) },
1713 { INT_RANGE(vht_tx_mcs_nss_8, -1, 3) },
1714 #endif /* CONFIG_VHT_OVERRIDES */
1715 { INT(ap_max_inactivity) },
1716 { INT(dtim_period) },
1717 { INT(beacon_int) },
1729 #undef STR_RANGE_KEY
1736 #define NUM_SSID_FIELDS ARRAY_SIZE(ssid_fields)
1740 * wpa_config_add_prio_network - Add a network to priority lists
1741 * @config: Configuration data from wpa_config_read()
1742 * @ssid: Pointer to the network configuration to be added to the list
1743 * Returns: 0 on success, -1 on failure
1745 * This function is used to add a network block to the priority list of
1746 * networks. This must be called for each network when reading in the full
1747 * configuration. In addition, this can be used indirectly when updating
1748 * priorities by calling wpa_config_update_prio_list().
1750 int wpa_config_add_prio_network(struct wpa_config *config,
1751 struct wpa_ssid *ssid)
1754 struct wpa_ssid *prev, **nlist;
1757 * Add to an existing priority list if one is available for the
1758 * configured priority level for this network.
1760 for (prio = 0; prio < config->num_prio; prio++) {
1761 prev = config->pssid[prio];
1762 if (prev->priority == ssid->priority) {
1770 /* First network for this priority - add a new priority list */
1771 nlist = os_realloc_array(config->pssid, config->num_prio + 1,
1772 sizeof(struct wpa_ssid *));
1776 for (prio = 0; prio < config->num_prio; prio++) {
1777 if (nlist[prio]->priority < ssid->priority) {
1778 os_memmove(&nlist[prio + 1], &nlist[prio],
1779 (config->num_prio - prio) *
1780 sizeof(struct wpa_ssid *));
1787 config->pssid = nlist;
1794 * wpa_config_update_prio_list - Update network priority list
1795 * @config: Configuration data from wpa_config_read()
1796 * Returns: 0 on success, -1 on failure
1798 * This function is called to update the priority list of networks in the
1799 * configuration when a network is being added or removed. This is also called
1800 * if a priority for a network is changed.
1802 int wpa_config_update_prio_list(struct wpa_config *config)
1804 struct wpa_ssid *ssid;
1807 os_free(config->pssid);
1808 config->pssid = NULL;
1809 config->num_prio = 0;
1811 ssid = config->ssid;
1814 if (wpa_config_add_prio_network(config, ssid) < 0)
1823 #ifdef IEEE8021X_EAPOL
1824 static void eap_peer_config_free(struct eap_peer_config *eap)
1826 os_free(eap->eap_methods);
1827 os_free(eap->identity);
1828 os_free(eap->anonymous_identity);
1829 os_free(eap->password);
1830 os_free(eap->ca_cert);
1831 os_free(eap->ca_path);
1832 os_free(eap->client_cert);
1833 os_free(eap->private_key);
1834 os_free(eap->private_key_passwd);
1835 os_free(eap->dh_file);
1836 os_free(eap->subject_match);
1837 os_free(eap->altsubject_match);
1838 os_free(eap->domain_suffix_match);
1839 os_free(eap->ca_cert2);
1840 os_free(eap->ca_path2);
1841 os_free(eap->client_cert2);
1842 os_free(eap->private_key2);
1843 os_free(eap->private_key2_passwd);
1844 os_free(eap->dh_file2);
1845 os_free(eap->subject_match2);
1846 os_free(eap->altsubject_match2);
1847 os_free(eap->domain_suffix_match2);
1848 os_free(eap->phase1);
1849 os_free(eap->phase2);
1852 os_free(eap->engine_id);
1853 os_free(eap->key_id);
1854 os_free(eap->cert_id);
1855 os_free(eap->ca_cert_id);
1856 os_free(eap->key2_id);
1857 os_free(eap->cert2_id);
1858 os_free(eap->ca_cert2_id);
1860 os_free(eap->engine2_id);
1862 os_free(eap->pending_req_otp);
1863 os_free(eap->pac_file);
1864 os_free(eap->new_password);
1865 os_free(eap->external_sim_resp);
1867 #endif /* IEEE8021X_EAPOL */
1871 * wpa_config_free_ssid - Free network/ssid configuration data
1872 * @ssid: Configuration data for the network
1874 * This function frees all resources allocated for the network configuration
1877 void wpa_config_free_ssid(struct wpa_ssid *ssid)
1879 struct psk_list_entry *psk;
1881 os_free(ssid->ssid);
1882 os_free(ssid->passphrase);
1883 os_free(ssid->ext_psk);
1884 #ifdef IEEE8021X_EAPOL
1885 eap_peer_config_free(&ssid->eap);
1886 #endif /* IEEE8021X_EAPOL */
1887 os_free(ssid->id_str);
1888 os_free(ssid->scan_freq);
1889 os_free(ssid->freq_list);
1890 os_free(ssid->bgscan);
1891 os_free(ssid->p2p_client_list);
1892 #ifdef CONFIG_HT_OVERRIDES
1893 os_free(ssid->ht_mcs);
1894 #endif /* CONFIG_HT_OVERRIDES */
1895 while ((psk = dl_list_first(&ssid->psk_list, struct psk_list_entry,
1897 dl_list_del(&psk->list);
1904 void wpa_config_free_cred(struct wpa_cred *cred)
1908 os_free(cred->realm);
1909 os_free(cred->username);
1910 os_free(cred->password);
1911 os_free(cred->ca_cert);
1912 os_free(cred->client_cert);
1913 os_free(cred->private_key);
1914 os_free(cred->private_key_passwd);
1915 os_free(cred->imsi);
1916 os_free(cred->milenage);
1917 for (i = 0; i < cred->num_domain; i++)
1918 os_free(cred->domain[i]);
1919 os_free(cred->domain);
1920 os_free(cred->domain_suffix_match);
1921 os_free(cred->eap_method);
1922 os_free(cred->phase1);
1923 os_free(cred->phase2);
1924 os_free(cred->excluded_ssid);
1930 * wpa_config_free - Free configuration data
1931 * @config: Configuration data from wpa_config_read()
1933 * This function frees all resources allocated for the configuration data by
1934 * wpa_config_read().
1936 void wpa_config_free(struct wpa_config *config)
1938 #ifndef CONFIG_NO_CONFIG_BLOBS
1939 struct wpa_config_blob *blob, *prevblob;
1940 #endif /* CONFIG_NO_CONFIG_BLOBS */
1941 struct wpa_ssid *ssid, *prev = NULL;
1942 struct wpa_cred *cred, *cprev;
1944 ssid = config->ssid;
1948 wpa_config_free_ssid(prev);
1951 cred = config->cred;
1955 wpa_config_free_cred(cprev);
1958 #ifndef CONFIG_NO_CONFIG_BLOBS
1959 blob = config->blobs;
1964 wpa_config_free_blob(prevblob);
1966 #endif /* CONFIG_NO_CONFIG_BLOBS */
1968 wpabuf_free(config->wps_vendor_ext_m1);
1969 os_free(config->ctrl_interface);
1970 os_free(config->ctrl_interface_group);
1971 os_free(config->opensc_engine_path);
1972 os_free(config->pkcs11_engine_path);
1973 os_free(config->pkcs11_module_path);
1974 os_free(config->pcsc_reader);
1975 os_free(config->pcsc_pin);
1976 os_free(config->driver_param);
1977 os_free(config->device_name);
1978 os_free(config->manufacturer);
1979 os_free(config->model_name);
1980 os_free(config->model_number);
1981 os_free(config->serial_number);
1982 os_free(config->config_methods);
1983 os_free(config->p2p_ssid_postfix);
1984 os_free(config->pssid);
1985 os_free(config->p2p_pref_chan);
1986 os_free(config->p2p_no_go_freq.range);
1987 os_free(config->autoscan);
1988 os_free(config->freq_list);
1989 wpabuf_free(config->wps_nfc_dh_pubkey);
1990 wpabuf_free(config->wps_nfc_dh_privkey);
1991 wpabuf_free(config->wps_nfc_dev_pw);
1992 os_free(config->ext_password_backend);
1993 os_free(config->sae_groups);
1994 wpabuf_free(config->ap_vendor_elements);
2000 * wpa_config_foreach_network - Iterate over each configured network
2001 * @config: Configuration data from wpa_config_read()
2002 * @func: Callback function to process each network
2003 * @arg: Opaque argument to pass to callback function
2005 * Iterate over the set of configured networks calling the specified
2006 * function for each item. We guard against callbacks removing the
2009 void wpa_config_foreach_network(struct wpa_config *config,
2010 void (*func)(void *, struct wpa_ssid *),
2013 struct wpa_ssid *ssid, *next;
2015 ssid = config->ssid;
2025 * wpa_config_get_network - Get configured network based on id
2026 * @config: Configuration data from wpa_config_read()
2027 * @id: Unique network id to search for
2028 * Returns: Network configuration or %NULL if not found
2030 struct wpa_ssid * wpa_config_get_network(struct wpa_config *config, int id)
2032 struct wpa_ssid *ssid;
2034 ssid = config->ssid;
2046 * wpa_config_add_network - Add a new network with empty configuration
2047 * @config: Configuration data from wpa_config_read()
2048 * Returns: The new network configuration or %NULL if operation failed
2050 struct wpa_ssid * wpa_config_add_network(struct wpa_config *config)
2053 struct wpa_ssid *ssid, *last = NULL;
2056 ssid = config->ssid;
2065 ssid = os_zalloc(sizeof(*ssid));
2069 dl_list_init(&ssid->psk_list);
2073 config->ssid = ssid;
2075 wpa_config_update_prio_list(config);
2082 * wpa_config_remove_network - Remove a configured network based on id
2083 * @config: Configuration data from wpa_config_read()
2084 * @id: Unique network id to search for
2085 * Returns: 0 on success, or -1 if the network was not found
2087 int wpa_config_remove_network(struct wpa_config *config, int id)
2089 struct wpa_ssid *ssid, *prev = NULL;
2091 ssid = config->ssid;
2103 prev->next = ssid->next;
2105 config->ssid = ssid->next;
2107 wpa_config_update_prio_list(config);
2108 wpa_config_free_ssid(ssid);
2114 * wpa_config_set_network_defaults - Set network default values
2115 * @ssid: Pointer to network configuration data
2117 void wpa_config_set_network_defaults(struct wpa_ssid *ssid)
2119 ssid->proto = DEFAULT_PROTO;
2120 ssid->pairwise_cipher = DEFAULT_PAIRWISE;
2121 ssid->group_cipher = DEFAULT_GROUP;
2122 ssid->key_mgmt = DEFAULT_KEY_MGMT;
2123 ssid->bg_scan_period = DEFAULT_BG_SCAN_PERIOD;
2124 #ifdef IEEE8021X_EAPOL
2125 ssid->eapol_flags = DEFAULT_EAPOL_FLAGS;
2126 ssid->eap_workaround = DEFAULT_EAP_WORKAROUND;
2127 ssid->eap.fragment_size = DEFAULT_FRAGMENT_SIZE;
2128 #endif /* IEEE8021X_EAPOL */
2129 #ifdef CONFIG_HT_OVERRIDES
2130 ssid->disable_ht = DEFAULT_DISABLE_HT;
2131 ssid->disable_ht40 = DEFAULT_DISABLE_HT40;
2132 ssid->disable_sgi = DEFAULT_DISABLE_SGI;
2133 ssid->disable_max_amsdu = DEFAULT_DISABLE_MAX_AMSDU;
2134 ssid->ampdu_factor = DEFAULT_AMPDU_FACTOR;
2135 ssid->ampdu_density = DEFAULT_AMPDU_DENSITY;
2136 #endif /* CONFIG_HT_OVERRIDES */
2137 #ifdef CONFIG_VHT_OVERRIDES
2138 ssid->vht_rx_mcs_nss_1 = -1;
2139 ssid->vht_rx_mcs_nss_2 = -1;
2140 ssid->vht_rx_mcs_nss_3 = -1;
2141 ssid->vht_rx_mcs_nss_4 = -1;
2142 ssid->vht_rx_mcs_nss_5 = -1;
2143 ssid->vht_rx_mcs_nss_6 = -1;
2144 ssid->vht_rx_mcs_nss_7 = -1;
2145 ssid->vht_rx_mcs_nss_8 = -1;
2146 ssid->vht_tx_mcs_nss_1 = -1;
2147 ssid->vht_tx_mcs_nss_2 = -1;
2148 ssid->vht_tx_mcs_nss_3 = -1;
2149 ssid->vht_tx_mcs_nss_4 = -1;
2150 ssid->vht_tx_mcs_nss_5 = -1;
2151 ssid->vht_tx_mcs_nss_6 = -1;
2152 ssid->vht_tx_mcs_nss_7 = -1;
2153 ssid->vht_tx_mcs_nss_8 = -1;
2154 #endif /* CONFIG_VHT_OVERRIDES */
2155 ssid->proactive_key_caching = -1;
2156 #ifdef CONFIG_IEEE80211W
2157 ssid->ieee80211w = MGMT_FRAME_PROTECTION_DEFAULT;
2158 #endif /* CONFIG_IEEE80211W */
2163 * wpa_config_set - Set a variable in network configuration
2164 * @ssid: Pointer to network configuration data
2165 * @var: Variable name, e.g., "ssid"
2166 * @value: Variable value
2167 * @line: Line number in configuration file or 0 if not used
2168 * Returns: 0 on success, -1 on failure
2170 * This function can be used to set network configuration variables based on
2171 * both the configuration file and management interface input. The value
2172 * parameter must be in the same format as the text-based configuration file is
2173 * using. For example, strings are using double quotation marks.
2175 int wpa_config_set(struct wpa_ssid *ssid, const char *var, const char *value,
2181 if (ssid == NULL || var == NULL || value == NULL)
2184 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2185 const struct parse_data *field = &ssid_fields[i];
2186 if (os_strcmp(var, field->name) != 0)
2189 if (field->parser(field, ssid, line, value)) {
2191 wpa_printf(MSG_ERROR, "Line %d: failed to "
2192 "parse %s '%s'.", line, var, value);
2198 if (i == NUM_SSID_FIELDS) {
2200 wpa_printf(MSG_ERROR, "Line %d: unknown network field "
2201 "'%s'.", line, var);
2210 int wpa_config_set_quoted(struct wpa_ssid *ssid, const char *var,
2217 len = os_strlen(value);
2218 buf = os_malloc(len + 3);
2222 os_memcpy(buf + 1, value, len);
2224 buf[len + 2] = '\0';
2225 ret = wpa_config_set(ssid, var, buf, 0);
2232 * wpa_config_get_all - Get all options from network configuration
2233 * @ssid: Pointer to network configuration data
2234 * @get_keys: Determines if keys/passwords will be included in returned list
2235 * (if they may be exported)
2236 * Returns: %NULL terminated list of all set keys and their values in the form
2237 * of [key1, val1, key2, val2, ... , NULL]
2239 * This function can be used to get list of all configured network properties.
2240 * The caller is responsible for freeing the returned list and all its
2243 char ** wpa_config_get_all(struct wpa_ssid *ssid, int get_keys)
2245 const struct parse_data *field;
2251 get_keys = get_keys && ssid->export_keys;
2253 props = os_calloc(2 * NUM_SSID_FIELDS + 1, sizeof(char *));
2258 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2259 field = &ssid_fields[i];
2260 if (field->key_data && !get_keys)
2262 value = field->writer(field, ssid);
2265 if (os_strlen(value) == 0) {
2270 key = os_strdup(field->name);
2276 props[fields_num * 2] = key;
2277 props[fields_num * 2 + 1] = value;
2293 #ifndef NO_CONFIG_WRITE
2295 * wpa_config_get - Get a variable in network configuration
2296 * @ssid: Pointer to network configuration data
2297 * @var: Variable name, e.g., "ssid"
2298 * Returns: Value of the variable or %NULL on failure
2300 * This function can be used to get network configuration variables. The
2301 * returned value is a copy of the configuration variable in text format, i.e,.
2302 * the same format that the text-based configuration file and wpa_config_set()
2303 * are using for the value. The caller is responsible for freeing the returned
2306 char * wpa_config_get(struct wpa_ssid *ssid, const char *var)
2310 if (ssid == NULL || var == NULL)
2313 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2314 const struct parse_data *field = &ssid_fields[i];
2315 if (os_strcmp(var, field->name) == 0)
2316 return field->writer(field, ssid);
2324 * wpa_config_get_no_key - Get a variable in network configuration (no keys)
2325 * @ssid: Pointer to network configuration data
2326 * @var: Variable name, e.g., "ssid"
2327 * Returns: Value of the variable or %NULL on failure
2329 * This function can be used to get network configuration variable like
2330 * wpa_config_get(). The only difference is that this functions does not expose
2331 * key/password material from the configuration. In case a key/password field
2332 * is requested, the returned value is an empty string or %NULL if the variable
2333 * is not set or "*" if the variable is set (regardless of its value). The
2334 * returned value is a copy of the configuration variable in text format, i.e,.
2335 * the same format that the text-based configuration file and wpa_config_set()
2336 * are using for the value. The caller is responsible for freeing the returned
2339 char * wpa_config_get_no_key(struct wpa_ssid *ssid, const char *var)
2343 if (ssid == NULL || var == NULL)
2346 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2347 const struct parse_data *field = &ssid_fields[i];
2348 if (os_strcmp(var, field->name) == 0) {
2349 char *res = field->writer(field, ssid);
2350 if (field->key_data) {
2351 if (res && res[0]) {
2352 wpa_printf(MSG_DEBUG, "Do not allow "
2353 "key_data field to be "
2356 return os_strdup("*");
2368 #endif /* NO_CONFIG_WRITE */
2372 * wpa_config_update_psk - Update WPA PSK based on passphrase and SSID
2373 * @ssid: Pointer to network configuration data
2375 * This function must be called to update WPA PSK when either SSID or the
2376 * passphrase has changed for the network configuration.
2378 void wpa_config_update_psk(struct wpa_ssid *ssid)
2380 #ifndef CONFIG_NO_PBKDF2
2381 pbkdf2_sha1(ssid->passphrase, ssid->ssid, ssid->ssid_len, 4096,
2382 ssid->psk, PMK_LEN);
2383 wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
2384 ssid->psk, PMK_LEN);
2386 #endif /* CONFIG_NO_PBKDF2 */
2390 int wpa_config_set_cred(struct wpa_cred *cred, const char *var,
2391 const char *value, int line)
2396 if (os_strcmp(var, "priority") == 0) {
2397 cred->priority = atoi(value);
2401 if (os_strcmp(var, "pcsc") == 0) {
2402 cred->pcsc = atoi(value);
2406 if (os_strcmp(var, "eap") == 0) {
2407 struct eap_method_type method;
2408 method.method = eap_peer_get_type(value, &method.vendor);
2409 if (method.vendor == EAP_VENDOR_IETF &&
2410 method.method == EAP_TYPE_NONE) {
2411 wpa_printf(MSG_ERROR, "Line %d: unknown EAP type '%s' "
2412 "for a credential", line, value);
2415 os_free(cred->eap_method);
2416 cred->eap_method = os_malloc(sizeof(*cred->eap_method));
2417 if (cred->eap_method == NULL)
2419 os_memcpy(cred->eap_method, &method, sizeof(method));
2423 if (os_strcmp(var, "password") == 0 &&
2424 os_strncmp(value, "ext:", 4) == 0) {
2425 os_free(cred->password);
2426 cred->password = os_strdup(value);
2427 cred->ext_password = 1;
2431 val = wpa_config_parse_string(value, &len);
2433 wpa_printf(MSG_ERROR, "Line %d: invalid field '%s' string "
2434 "value '%s'.", line, var, value);
2438 if (os_strcmp(var, "realm") == 0) {
2439 os_free(cred->realm);
2444 if (os_strcmp(var, "username") == 0) {
2445 os_free(cred->username);
2446 cred->username = val;
2450 if (os_strcmp(var, "password") == 0) {
2451 os_free(cred->password);
2452 cred->password = val;
2453 cred->ext_password = 0;
2457 if (os_strcmp(var, "ca_cert") == 0) {
2458 os_free(cred->ca_cert);
2459 cred->ca_cert = val;
2463 if (os_strcmp(var, "client_cert") == 0) {
2464 os_free(cred->client_cert);
2465 cred->client_cert = val;
2469 if (os_strcmp(var, "private_key") == 0) {
2470 os_free(cred->private_key);
2471 cred->private_key = val;
2475 if (os_strcmp(var, "private_key_passwd") == 0) {
2476 os_free(cred->private_key_passwd);
2477 cred->private_key_passwd = val;
2481 if (os_strcmp(var, "imsi") == 0) {
2482 os_free(cred->imsi);
2487 if (os_strcmp(var, "milenage") == 0) {
2488 os_free(cred->milenage);
2489 cred->milenage = val;
2493 if (os_strcmp(var, "domain_suffix_match") == 0) {
2494 os_free(cred->domain_suffix_match);
2495 cred->domain_suffix_match = val;
2499 if (os_strcmp(var, "domain") == 0) {
2501 new_domain = os_realloc_array(cred->domain,
2502 cred->num_domain + 1,
2504 if (new_domain == NULL) {
2508 new_domain[cred->num_domain++] = val;
2509 cred->domain = new_domain;
2513 if (os_strcmp(var, "phase1") == 0) {
2514 os_free(cred->phase1);
2519 if (os_strcmp(var, "phase2") == 0) {
2520 os_free(cred->phase2);
2525 if (os_strcmp(var, "roaming_consortium") == 0) {
2526 if (len < 3 || len > sizeof(cred->roaming_consortium)) {
2527 wpa_printf(MSG_ERROR, "Line %d: invalid "
2528 "roaming_consortium length %d (3..15 "
2529 "expected)", line, (int) len);
2533 os_memcpy(cred->roaming_consortium, val, len);
2534 cred->roaming_consortium_len = len;
2539 if (os_strcmp(var, "required_roaming_consortium") == 0) {
2540 if (len < 3 || len > sizeof(cred->required_roaming_consortium))
2542 wpa_printf(MSG_ERROR, "Line %d: invalid "
2543 "required_roaming_consortium length %d "
2544 "(3..15 expected)", line, (int) len);
2548 os_memcpy(cred->required_roaming_consortium, val, len);
2549 cred->required_roaming_consortium_len = len;
2554 if (os_strcmp(var, "excluded_ssid") == 0) {
2555 struct excluded_ssid *e;
2557 if (len > MAX_SSID_LEN) {
2558 wpa_printf(MSG_ERROR, "Line %d: invalid "
2559 "excluded_ssid length %d", line, (int) len);
2564 e = os_realloc_array(cred->excluded_ssid,
2565 cred->num_excluded_ssid + 1,
2566 sizeof(struct excluded_ssid));
2571 cred->excluded_ssid = e;
2573 e = &cred->excluded_ssid[cred->num_excluded_ssid++];
2574 os_memcpy(e->ssid, val, len);
2583 wpa_printf(MSG_ERROR, "Line %d: unknown cred field '%s'.",
2593 struct wpa_cred * wpa_config_get_cred(struct wpa_config *config, int id)
2595 struct wpa_cred *cred;
2597 cred = config->cred;
2608 struct wpa_cred * wpa_config_add_cred(struct wpa_config *config)
2611 struct wpa_cred *cred, *last = NULL;
2614 cred = config->cred;
2623 cred = os_zalloc(sizeof(*cred));
2630 config->cred = cred;
2636 int wpa_config_remove_cred(struct wpa_config *config, int id)
2638 struct wpa_cred *cred, *prev = NULL;
2640 cred = config->cred;
2652 prev->next = cred->next;
2654 config->cred = cred->next;
2656 wpa_config_free_cred(cred);
2661 #ifndef CONFIG_NO_CONFIG_BLOBS
2663 * wpa_config_get_blob - Get a named configuration blob
2664 * @config: Configuration data from wpa_config_read()
2665 * @name: Name of the blob
2666 * Returns: Pointer to blob data or %NULL if not found
2668 const struct wpa_config_blob * wpa_config_get_blob(struct wpa_config *config,
2671 struct wpa_config_blob *blob = config->blobs;
2674 if (os_strcmp(blob->name, name) == 0)
2683 * wpa_config_set_blob - Set or add a named configuration blob
2684 * @config: Configuration data from wpa_config_read()
2685 * @blob: New value for the blob
2687 * Adds a new configuration blob or replaces the current value of an existing
2690 void wpa_config_set_blob(struct wpa_config *config,
2691 struct wpa_config_blob *blob)
2693 wpa_config_remove_blob(config, blob->name);
2694 blob->next = config->blobs;
2695 config->blobs = blob;
2700 * wpa_config_free_blob - Free blob data
2701 * @blob: Pointer to blob to be freed
2703 void wpa_config_free_blob(struct wpa_config_blob *blob)
2706 os_free(blob->name);
2707 os_free(blob->data);
2714 * wpa_config_remove_blob - Remove a named configuration blob
2715 * @config: Configuration data from wpa_config_read()
2716 * @name: Name of the blob to remove
2717 * Returns: 0 if blob was removed or -1 if blob was not found
2719 int wpa_config_remove_blob(struct wpa_config *config, const char *name)
2721 struct wpa_config_blob *pos = config->blobs, *prev = NULL;
2724 if (os_strcmp(pos->name, name) == 0) {
2726 prev->next = pos->next;
2728 config->blobs = pos->next;
2729 wpa_config_free_blob(pos);
2738 #endif /* CONFIG_NO_CONFIG_BLOBS */
2742 * wpa_config_alloc_empty - Allocate an empty configuration
2743 * @ctrl_interface: Control interface parameters, e.g., path to UNIX domain
2745 * @driver_param: Driver parameters
2746 * Returns: Pointer to allocated configuration data or %NULL on failure
2748 struct wpa_config * wpa_config_alloc_empty(const char *ctrl_interface,
2749 const char *driver_param)
2751 struct wpa_config *config;
2752 const int aCWmin = 4, aCWmax = 10;
2753 const struct hostapd_wmm_ac_params ac_bk =
2754 { aCWmin, aCWmax, 7, 0, 0 }; /* background traffic */
2755 const struct hostapd_wmm_ac_params ac_be =
2756 { aCWmin, aCWmax, 3, 0, 0 }; /* best effort traffic */
2757 const struct hostapd_wmm_ac_params ac_vi = /* video traffic */
2758 { aCWmin - 1, aCWmin, 2, 3000 / 32, 0 };
2759 const struct hostapd_wmm_ac_params ac_vo = /* voice traffic */
2760 { aCWmin - 2, aCWmin - 1, 2, 1500 / 32, 0 };
2762 config = os_zalloc(sizeof(*config));
2765 config->eapol_version = DEFAULT_EAPOL_VERSION;
2766 config->ap_scan = DEFAULT_AP_SCAN;
2767 config->fast_reauth = DEFAULT_FAST_REAUTH;
2768 config->p2p_go_intent = DEFAULT_P2P_GO_INTENT;
2769 config->p2p_intra_bss = DEFAULT_P2P_INTRA_BSS;
2770 config->p2p_go_max_inactivity = DEFAULT_P2P_GO_MAX_INACTIVITY;
2771 config->bss_max_count = DEFAULT_BSS_MAX_COUNT;
2772 config->bss_expiration_age = DEFAULT_BSS_EXPIRATION_AGE;
2773 config->bss_expiration_scan_count = DEFAULT_BSS_EXPIRATION_SCAN_COUNT;
2774 config->max_num_sta = DEFAULT_MAX_NUM_STA;
2775 config->access_network_type = DEFAULT_ACCESS_NETWORK_TYPE;
2776 config->scan_cur_freq = DEFAULT_SCAN_CUR_FREQ;
2777 config->wmm_ac_params[0] = ac_be;
2778 config->wmm_ac_params[1] = ac_bk;
2779 config->wmm_ac_params[2] = ac_vi;
2780 config->wmm_ac_params[3] = ac_vo;
2783 config->ctrl_interface = os_strdup(ctrl_interface);
2785 config->driver_param = os_strdup(driver_param);
2791 #ifndef CONFIG_NO_STDOUT_DEBUG
2793 * wpa_config_debug_dump_networks - Debug dump of configured networks
2794 * @config: Configuration data from wpa_config_read()
2796 void wpa_config_debug_dump_networks(struct wpa_config *config)
2799 struct wpa_ssid *ssid;
2801 for (prio = 0; prio < config->num_prio; prio++) {
2802 ssid = config->pssid[prio];
2803 wpa_printf(MSG_DEBUG, "Priority group %d",
2806 wpa_printf(MSG_DEBUG, " id=%d ssid='%s'",
2808 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
2813 #endif /* CONFIG_NO_STDOUT_DEBUG */
2816 struct global_parse_data {
2818 int (*parser)(const struct global_parse_data *data,
2819 struct wpa_config *config, int line, const char *value);
2820 void *param1, *param2, *param3;
2821 unsigned int changed_flag;
2825 static int wpa_global_config_parse_int(const struct global_parse_data *data,
2826 struct wpa_config *config, int line,
2832 dst = (int *) (((u8 *) config) + (long) data->param1);
2833 val = strtol(pos, &end, 0);
2835 wpa_printf(MSG_ERROR, "Line %d: invalid number \"%s\"",
2841 wpa_printf(MSG_DEBUG, "%s=%d", data->name, *dst);
2843 if (data->param2 && *dst < (long) data->param2) {
2844 wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
2845 "min_value=%ld)", line, data->name, *dst,
2846 (long) data->param2);
2847 *dst = (long) data->param2;
2851 if (data->param3 && *dst > (long) data->param3) {
2852 wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
2853 "max_value=%ld)", line, data->name, *dst,
2854 (long) data->param3);
2855 *dst = (long) data->param3;
2863 static int wpa_global_config_parse_str(const struct global_parse_data *data,
2864 struct wpa_config *config, int line,
2870 len = os_strlen(pos);
2871 if (data->param2 && len < (size_t) data->param2) {
2872 wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
2873 "min_len=%ld)", line, data->name,
2874 (unsigned long) len, (long) data->param2);
2878 if (data->param3 && len > (size_t) data->param3) {
2879 wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
2880 "max_len=%ld)", line, data->name,
2881 (unsigned long) len, (long) data->param3);
2885 tmp = os_strdup(pos);
2889 dst = (char **) (((u8 *) config) + (long) data->param1);
2892 wpa_printf(MSG_DEBUG, "%s='%s'", data->name, *dst);
2898 static int wpa_config_process_bgscan(const struct global_parse_data *data,
2899 struct wpa_config *config, int line,
2905 tmp = wpa_config_parse_string(pos, &len);
2907 wpa_printf(MSG_ERROR, "Line %d: failed to parse %s",
2912 return wpa_global_config_parse_str(data, config, line, tmp);
2916 static int wpa_global_config_parse_bin(const struct global_parse_data *data,
2917 struct wpa_config *config, int line,
2921 struct wpabuf **dst, *tmp;
2923 len = os_strlen(pos);
2927 tmp = wpabuf_alloc(len / 2);
2931 if (hexstr2bin(pos, wpabuf_put(tmp, len / 2), len / 2)) {
2936 dst = (struct wpabuf **) (((u8 *) config) + (long) data->param1);
2939 wpa_printf(MSG_DEBUG, "%s", data->name);
2945 static int wpa_config_process_freq_list(const struct global_parse_data *data,
2946 struct wpa_config *config, int line,
2951 freqs = wpa_config_parse_int_array(value);
2954 if (freqs[0] == 0) {
2958 os_free(config->freq_list);
2959 config->freq_list = freqs;
2964 static int wpa_config_process_country(const struct global_parse_data *data,
2965 struct wpa_config *config, int line,
2968 if (!pos[0] || !pos[1]) {
2969 wpa_printf(MSG_DEBUG, "Invalid country set");
2972 config->country[0] = pos[0];
2973 config->country[1] = pos[1];
2974 wpa_printf(MSG_DEBUG, "country='%c%c'",
2975 config->country[0], config->country[1]);
2980 static int wpa_config_process_load_dynamic_eap(
2981 const struct global_parse_data *data, struct wpa_config *config,
2982 int line, const char *so)
2985 wpa_printf(MSG_DEBUG, "load_dynamic_eap=%s", so);
2986 ret = eap_peer_method_load(so);
2988 wpa_printf(MSG_DEBUG, "This EAP type was already loaded - not "
2991 wpa_printf(MSG_ERROR, "Line %d: Failed to load dynamic EAP "
2992 "method '%s'.", line, so);
3002 static int wpa_config_process_uuid(const struct global_parse_data *data,
3003 struct wpa_config *config, int line,
3007 if (uuid_str2bin(pos, config->uuid)) {
3008 wpa_printf(MSG_ERROR, "Line %d: invalid UUID", line);
3011 uuid_bin2str(config->uuid, buf, sizeof(buf));
3012 wpa_printf(MSG_DEBUG, "uuid=%s", buf);
3017 static int wpa_config_process_device_type(
3018 const struct global_parse_data *data,
3019 struct wpa_config *config, int line, const char *pos)
3021 return wps_dev_type_str2bin(pos, config->device_type);
3025 static int wpa_config_process_os_version(const struct global_parse_data *data,
3026 struct wpa_config *config, int line,
3029 if (hexstr2bin(pos, config->os_version, 4)) {
3030 wpa_printf(MSG_ERROR, "Line %d: invalid os_version", line);
3033 wpa_printf(MSG_DEBUG, "os_version=%08x",
3034 WPA_GET_BE32(config->os_version));
3039 static int wpa_config_process_wps_vendor_ext_m1(
3040 const struct global_parse_data *data,
3041 struct wpa_config *config, int line, const char *pos)
3044 int len = os_strlen(pos) / 2;
3048 wpa_printf(MSG_ERROR, "Line %d: "
3049 "invalid wps_vendor_ext_m1", line);
3053 tmp = wpabuf_alloc(len);
3055 p = wpabuf_put(tmp, len);
3057 if (hexstr2bin(pos, p, len)) {
3058 wpa_printf(MSG_ERROR, "Line %d: "
3059 "invalid wps_vendor_ext_m1", line);
3064 wpabuf_free(config->wps_vendor_ext_m1);
3065 config->wps_vendor_ext_m1 = tmp;
3067 wpa_printf(MSG_ERROR, "Can not allocate "
3068 "memory for wps_vendor_ext_m1");
3075 #endif /* CONFIG_WPS */
3078 static int wpa_config_process_sec_device_type(
3079 const struct global_parse_data *data,
3080 struct wpa_config *config, int line, const char *pos)
3084 if (config->num_sec_device_types >= MAX_SEC_DEVICE_TYPES) {
3085 wpa_printf(MSG_ERROR, "Line %d: too many sec_device_type "
3090 idx = config->num_sec_device_types;
3092 if (wps_dev_type_str2bin(pos, config->sec_device_type[idx]))
3095 config->num_sec_device_types++;
3100 static int wpa_config_process_p2p_pref_chan(
3101 const struct global_parse_data *data,
3102 struct wpa_config *config, int line, const char *pos)
3104 struct p2p_channel *pref = NULL, *n;
3105 unsigned int num = 0;
3109 /* format: class:chan,class:chan,... */
3112 op_class = atoi(pos);
3113 pos2 = os_strchr(pos, ':');
3119 n = os_realloc_array(pref, num + 1,
3120 sizeof(struct p2p_channel));
3124 pref[num].op_class = op_class;
3125 pref[num].chan = chan;
3128 pos = os_strchr(pos2, ',');
3134 os_free(config->p2p_pref_chan);
3135 config->p2p_pref_chan = pref;
3136 config->num_p2p_pref_chan = num;
3137 wpa_hexdump(MSG_DEBUG, "P2P: Preferred class/channel pairs",
3138 (u8 *) config->p2p_pref_chan,
3139 config->num_p2p_pref_chan * sizeof(struct p2p_channel));
3145 wpa_printf(MSG_ERROR, "Line %d: Invalid p2p_pref_chan list", line);
3150 static int wpa_config_process_p2p_no_go_freq(
3151 const struct global_parse_data *data,
3152 struct wpa_config *config, int line, const char *pos)
3156 ret = freq_range_list_parse(&config->p2p_no_go_freq, pos);
3158 wpa_printf(MSG_ERROR, "Line %d: Invalid p2p_no_go_freq", line);
3162 wpa_printf(MSG_DEBUG, "P2P: p2p_no_go_freq with %u items",
3163 config->p2p_no_go_freq.num);
3168 #endif /* CONFIG_P2P */
3171 static int wpa_config_process_hessid(
3172 const struct global_parse_data *data,
3173 struct wpa_config *config, int line, const char *pos)
3175 if (hwaddr_aton2(pos, config->hessid) < 0) {
3176 wpa_printf(MSG_ERROR, "Line %d: Invalid hessid '%s'",
3185 static int wpa_config_process_sae_groups(
3186 const struct global_parse_data *data,
3187 struct wpa_config *config, int line, const char *pos)
3189 int *groups = wpa_config_parse_int_array(pos);
3190 if (groups == NULL) {
3191 wpa_printf(MSG_ERROR, "Line %d: Invalid sae_groups '%s'",
3196 os_free(config->sae_groups);
3197 config->sae_groups = groups;
3203 static int wpa_config_process_ap_vendor_elements(
3204 const struct global_parse_data *data,
3205 struct wpa_config *config, int line, const char *pos)
3208 int len = os_strlen(pos) / 2;
3212 wpa_printf(MSG_ERROR, "Line %d: invalid ap_vendor_elements",
3217 tmp = wpabuf_alloc(len);
3219 p = wpabuf_put(tmp, len);
3221 if (hexstr2bin(pos, p, len)) {
3222 wpa_printf(MSG_ERROR, "Line %d: invalid "
3223 "ap_vendor_elements", line);
3228 wpabuf_free(config->ap_vendor_elements);
3229 config->ap_vendor_elements = tmp;
3231 wpa_printf(MSG_ERROR, "Cannot allocate memory for "
3232 "ap_vendor_elements");
3240 #ifdef CONFIG_CTRL_IFACE
3241 static int wpa_config_process_no_ctrl_interface(
3242 const struct global_parse_data *data,
3243 struct wpa_config *config, int line, const char *pos)
3245 wpa_printf(MSG_DEBUG, "no_ctrl_interface -> ctrl_interface=NULL");
3246 os_free(config->ctrl_interface);
3247 config->ctrl_interface = NULL;
3250 #endif /* CONFIG_CTRL_IFACE */
3256 /* OFFSET: Get offset of a variable within the wpa_config structure */
3257 #define OFFSET(v) ((void *) &((struct wpa_config *) 0)->v)
3259 #define FUNC(f) #f, wpa_config_process_ ## f, OFFSET(f), NULL, NULL
3260 #define FUNC_NO_VAR(f) #f, wpa_config_process_ ## f, NULL, NULL, NULL
3261 #define _INT(f) #f, wpa_global_config_parse_int, OFFSET(f)
3262 #define INT(f) _INT(f), NULL, NULL
3263 #define INT_RANGE(f, min, max) _INT(f), (void *) min, (void *) max
3264 #define _STR(f) #f, wpa_global_config_parse_str, OFFSET(f)
3265 #define STR(f) _STR(f), NULL, NULL
3266 #define STR_RANGE(f, min, max) _STR(f), (void *) min, (void *) max
3267 #define BIN(f) #f, wpa_global_config_parse_bin, OFFSET(f), NULL, NULL
3269 static const struct global_parse_data global_fields[] = {
3270 #ifdef CONFIG_CTRL_IFACE
3271 { STR(ctrl_interface), 0 },
3272 { FUNC_NO_VAR(no_ctrl_interface), 0 },
3273 { STR(ctrl_interface_group), 0 } /* deprecated */,
3274 #endif /* CONFIG_CTRL_IFACE */
3275 { INT_RANGE(eapol_version, 1, 2), 0 },
3276 { INT(ap_scan), 0 },
3277 { FUNC(bgscan), 0 },
3278 { INT(disable_scan_offload), 0 },
3279 { INT(fast_reauth), 0 },
3280 { STR(opensc_engine_path), 0 },
3281 { STR(pkcs11_engine_path), 0 },
3282 { STR(pkcs11_module_path), 0 },
3283 { STR(pcsc_reader), 0 },
3284 { STR(pcsc_pin), 0 },
3285 { INT(external_sim), 0 },
3286 { STR(driver_param), 0 },
3287 { INT(dot11RSNAConfigPMKLifetime), 0 },
3288 { INT(dot11RSNAConfigPMKReauthThreshold), 0 },
3289 { INT(dot11RSNAConfigSATimeout), 0 },
3290 #ifndef CONFIG_NO_CONFIG_WRITE
3291 { INT(update_config), 0 },
3292 #endif /* CONFIG_NO_CONFIG_WRITE */
3293 { FUNC_NO_VAR(load_dynamic_eap), 0 },
3295 { FUNC(uuid), CFG_CHANGED_UUID },
3296 { STR_RANGE(device_name, 0, 32), CFG_CHANGED_DEVICE_NAME },
3297 { STR_RANGE(manufacturer, 0, 64), CFG_CHANGED_WPS_STRING },
3298 { STR_RANGE(model_name, 0, 32), CFG_CHANGED_WPS_STRING },
3299 { STR_RANGE(model_number, 0, 32), CFG_CHANGED_WPS_STRING },
3300 { STR_RANGE(serial_number, 0, 32), CFG_CHANGED_WPS_STRING },
3301 { FUNC(device_type), CFG_CHANGED_DEVICE_TYPE },
3302 { FUNC(os_version), CFG_CHANGED_OS_VERSION },
3303 { STR(config_methods), CFG_CHANGED_CONFIG_METHODS },
3304 { INT_RANGE(wps_cred_processing, 0, 2), 0 },
3305 { FUNC(wps_vendor_ext_m1), CFG_CHANGED_VENDOR_EXTENSION },
3306 #endif /* CONFIG_WPS */
3308 { FUNC(sec_device_type), CFG_CHANGED_SEC_DEVICE_TYPE },
3309 { INT(p2p_listen_reg_class), 0 },
3310 { INT(p2p_listen_channel), 0 },
3311 { INT(p2p_oper_reg_class), CFG_CHANGED_P2P_OPER_CHANNEL },
3312 { INT(p2p_oper_channel), CFG_CHANGED_P2P_OPER_CHANNEL },
3313 { INT_RANGE(p2p_go_intent, 0, 15), 0 },
3314 { STR(p2p_ssid_postfix), CFG_CHANGED_P2P_SSID_POSTFIX },
3315 { INT_RANGE(persistent_reconnect, 0, 1), 0 },
3316 { INT_RANGE(p2p_intra_bss, 0, 1), CFG_CHANGED_P2P_INTRA_BSS },
3317 { INT(p2p_group_idle), 0 },
3318 { FUNC(p2p_pref_chan), CFG_CHANGED_P2P_PREF_CHAN },
3319 { FUNC(p2p_no_go_freq), CFG_CHANGED_P2P_PREF_CHAN },
3320 { INT_RANGE(p2p_add_cli_chan, 0, 1), 0 },
3321 { INT(p2p_go_ht40), 0 },
3322 { INT(p2p_go_vht), 0 },
3323 { INT(p2p_disabled), 0 },
3324 { INT(p2p_no_group_iface), 0 },
3325 { INT_RANGE(p2p_ignore_shared_freq, 0, 1), 0 },
3326 #endif /* CONFIG_P2P */
3327 { FUNC(country), CFG_CHANGED_COUNTRY },
3328 { INT(bss_max_count), 0 },
3329 { INT(bss_expiration_age), 0 },
3330 { INT(bss_expiration_scan_count), 0 },
3331 { INT_RANGE(filter_ssids, 0, 1), 0 },
3332 { INT_RANGE(filter_rssi, -100, 0), 0 },
3333 { INT(max_num_sta), 0 },
3334 { INT_RANGE(disassoc_low_ack, 0, 1), 0 },
3336 { INT_RANGE(hs20, 0, 1), 0 },
3337 #endif /* CONFIG_HS20 */
3338 { INT_RANGE(interworking, 0, 1), 0 },
3339 { FUNC(hessid), 0 },
3340 { INT_RANGE(access_network_type, 0, 15), 0 },
3341 { INT_RANGE(pbc_in_m1, 0, 1), 0 },
3342 { STR(autoscan), 0 },
3343 { INT_RANGE(wps_nfc_dev_pw_id, 0x10, 0xffff),
3344 CFG_CHANGED_NFC_PASSWORD_TOKEN },
3345 { BIN(wps_nfc_dh_pubkey), CFG_CHANGED_NFC_PASSWORD_TOKEN },
3346 { BIN(wps_nfc_dh_privkey), CFG_CHANGED_NFC_PASSWORD_TOKEN },
3347 { BIN(wps_nfc_dev_pw), CFG_CHANGED_NFC_PASSWORD_TOKEN },
3348 { STR(ext_password_backend), CFG_CHANGED_EXT_PW_BACKEND },
3349 { INT(p2p_go_max_inactivity), 0 },
3350 { INT_RANGE(auto_interworking, 0, 1), 0 },
3353 { FUNC(sae_groups), 0 },
3354 { INT(dtim_period), 0 },
3355 { INT(beacon_int), 0 },
3356 { FUNC(ap_vendor_elements), 0 },
3357 { INT_RANGE(ignore_old_scan_res, 0, 1), 0 },
3358 { FUNC(freq_list), 0 },
3359 { INT(scan_cur_freq), 0 },
3360 { INT(sched_scan_interval), 0 },
3361 { INT(tdls_external_control), 0},
3372 #define NUM_GLOBAL_FIELDS ARRAY_SIZE(global_fields)
3375 int wpa_config_process_global(struct wpa_config *config, char *pos, int line)
3380 for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
3381 const struct global_parse_data *field = &global_fields[i];
3382 size_t flen = os_strlen(field->name);
3383 if (os_strncmp(pos, field->name, flen) != 0 ||
3387 if (field->parser(field, config, line, pos + flen + 1)) {
3388 wpa_printf(MSG_ERROR, "Line %d: failed to "
3389 "parse '%s'.", line, pos);
3392 if (field->changed_flag == CFG_CHANGED_NFC_PASSWORD_TOKEN)
3393 config->wps_nfc_pw_from_config = 1;
3394 config->changed_parameters |= field->changed_flag;
3397 if (i == NUM_GLOBAL_FIELDS) {
3399 if (os_strncmp(pos, "wmm_ac_", 7) == 0) {
3400 char *tmp = os_strchr(pos, '=');
3404 wpa_printf(MSG_ERROR, "Line %d: invalid line "
3409 if (hostapd_config_wmm_ac(config->wmm_ac_params, pos,
3411 wpa_printf(MSG_ERROR, "Line %d: invalid WMM "
3416 #endif /* CONFIG_AP */
3419 wpa_printf(MSG_ERROR, "Line %d: unknown global field '%s'.",