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 "utils/ip_addr.h"
14 #include "crypto/sha1.h"
15 #include "rsn_supp/wpa.h"
16 #include "eap_peer/eap.h"
21 #if !defined(CONFIG_CTRL_IFACE) && defined(CONFIG_NO_CONFIG_WRITE)
22 #define NO_CONFIG_WRITE
26 * Structure for network configuration parsing. This data is used to implement
27 * a generic parser for each network block variable. The table of configuration
28 * variables is defined below in this file (ssid_fields[]).
31 /* Configuration variable name */
34 /* Parser function for this variable */
35 int (*parser)(const struct parse_data *data, struct wpa_ssid *ssid,
36 int line, const char *value);
38 #ifndef NO_CONFIG_WRITE
39 /* Writer function (i.e., to get the variable in text format from
40 * internal presentation). */
41 char * (*writer)(const struct parse_data *data, struct wpa_ssid *ssid);
42 #endif /* NO_CONFIG_WRITE */
44 /* Variable specific parameters for the parser. */
45 void *param1, *param2, *param3, *param4;
47 /* 0 = this variable can be included in debug output and ctrl_iface
48 * 1 = this variable contains key/private data and it must not be
49 * included in debug output unless explicitly requested. In
50 * addition, this variable will not be readable through the
57 static int wpa_config_parse_str(const struct parse_data *data,
58 struct wpa_ssid *ssid,
59 int line, const char *value)
61 size_t res_len, *dst_len;
64 if (os_strcmp(value, "NULL") == 0) {
65 wpa_printf(MSG_DEBUG, "Unset configuration string '%s'",
72 tmp = wpa_config_parse_string(value, &res_len);
74 wpa_printf(MSG_ERROR, "Line %d: failed to parse %s '%s'.",
76 data->key_data ? "[KEY DATA REMOVED]" : value);
81 wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
84 wpa_hexdump_ascii(MSG_MSGDUMP, data->name,
88 if (data->param3 && res_len < (size_t) data->param3) {
89 wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
90 "min_len=%ld)", line, data->name,
91 (unsigned long) res_len, (long) data->param3);
96 if (data->param4 && res_len > (size_t) data->param4) {
97 wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
98 "max_len=%ld)", line, data->name,
99 (unsigned long) res_len, (long) data->param4);
105 dst = (char **) (((u8 *) ssid) + (long) data->param1);
106 dst_len = (size_t *) (((u8 *) ssid) + (long) data->param2);
116 #ifndef NO_CONFIG_WRITE
117 static char * wpa_config_write_string_ascii(const u8 *value, size_t len)
121 buf = os_malloc(len + 3);
125 os_memcpy(buf + 1, value, len);
133 static char * wpa_config_write_string_hex(const u8 *value, size_t len)
137 buf = os_zalloc(2 * len + 1);
140 wpa_snprintf_hex(buf, 2 * len + 1, value, len);
146 static char * wpa_config_write_string(const u8 *value, size_t len)
151 if (is_hex(value, len))
152 return wpa_config_write_string_hex(value, len);
154 return wpa_config_write_string_ascii(value, len);
158 static char * wpa_config_write_str(const struct parse_data *data,
159 struct wpa_ssid *ssid)
164 src = (char **) (((u8 *) ssid) + (long) data->param1);
169 len = *((size_t *) (((u8 *) ssid) + (long) data->param2));
171 len = os_strlen(*src);
173 return wpa_config_write_string((const u8 *) *src, len);
175 #endif /* NO_CONFIG_WRITE */
178 static int wpa_config_parse_int(const struct parse_data *data,
179 struct wpa_ssid *ssid,
180 int line, const char *value)
185 dst = (int *) (((u8 *) ssid) + (long) data->param1);
186 val = strtol(value, &end, 0);
188 wpa_printf(MSG_ERROR, "Line %d: invalid number \"%s\"",
193 wpa_printf(MSG_MSGDUMP, "%s=%d (0x%x)", data->name, *dst, *dst);
195 if (data->param3 && *dst < (long) data->param3) {
196 wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
197 "min_value=%ld)", line, data->name, *dst,
198 (long) data->param3);
199 *dst = (long) data->param3;
203 if (data->param4 && *dst > (long) data->param4) {
204 wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
205 "max_value=%ld)", line, data->name, *dst,
206 (long) data->param4);
207 *dst = (long) data->param4;
215 #ifndef NO_CONFIG_WRITE
216 static char * wpa_config_write_int(const struct parse_data *data,
217 struct wpa_ssid *ssid)
222 src = (int *) (((u8 *) ssid) + (long) data->param1);
224 value = os_malloc(20);
227 res = os_snprintf(value, 20, "%d", *src);
228 if (res < 0 || res >= 20) {
232 value[20 - 1] = '\0';
235 #endif /* NO_CONFIG_WRITE */
238 static int wpa_config_parse_bssid(const struct parse_data *data,
239 struct wpa_ssid *ssid, int line,
242 if (value[0] == '\0' || os_strcmp(value, "\"\"") == 0 ||
243 os_strcmp(value, "any") == 0) {
245 wpa_printf(MSG_MSGDUMP, "BSSID any");
248 if (hwaddr_aton(value, ssid->bssid)) {
249 wpa_printf(MSG_ERROR, "Line %d: Invalid BSSID '%s'.",
254 wpa_hexdump(MSG_MSGDUMP, "BSSID", ssid->bssid, ETH_ALEN);
259 #ifndef NO_CONFIG_WRITE
260 static char * wpa_config_write_bssid(const struct parse_data *data,
261 struct wpa_ssid *ssid)
266 if (!ssid->bssid_set)
269 value = os_malloc(20);
272 res = os_snprintf(value, 20, MACSTR, MAC2STR(ssid->bssid));
273 if (res < 0 || res >= 20) {
277 value[20 - 1] = '\0';
280 #endif /* NO_CONFIG_WRITE */
283 static int wpa_config_parse_psk(const struct parse_data *data,
284 struct wpa_ssid *ssid, int line,
287 #ifdef CONFIG_EXT_PASSWORD
288 if (os_strncmp(value, "ext:", 4) == 0) {
289 os_free(ssid->passphrase);
290 ssid->passphrase = NULL;
292 os_free(ssid->ext_psk);
293 ssid->ext_psk = os_strdup(value + 4);
294 if (ssid->ext_psk == NULL)
296 wpa_printf(MSG_DEBUG, "PSK: External password '%s'",
300 #endif /* CONFIG_EXT_PASSWORD */
303 #ifndef CONFIG_NO_PBKDF2
308 pos = os_strrchr(value, '"');
312 len = os_strlen(value);
313 if (len < 8 || len > 63) {
314 wpa_printf(MSG_ERROR, "Line %d: Invalid passphrase "
315 "length %lu (expected: 8..63) '%s'.",
316 line, (unsigned long) len, value);
319 wpa_hexdump_ascii_key(MSG_MSGDUMP, "PSK (ASCII passphrase)",
321 if (ssid->passphrase && os_strlen(ssid->passphrase) == len &&
322 os_memcmp(ssid->passphrase, value, len) == 0)
325 os_free(ssid->passphrase);
326 ssid->passphrase = dup_binstr(value, len);
327 if (ssid->passphrase == NULL)
330 #else /* CONFIG_NO_PBKDF2 */
331 wpa_printf(MSG_ERROR, "Line %d: ASCII passphrase not "
334 #endif /* CONFIG_NO_PBKDF2 */
337 if (hexstr2bin(value, ssid->psk, PMK_LEN) ||
338 value[PMK_LEN * 2] != '\0') {
339 wpa_printf(MSG_ERROR, "Line %d: Invalid PSK '%s'.",
344 os_free(ssid->passphrase);
345 ssid->passphrase = NULL;
348 wpa_hexdump_key(MSG_MSGDUMP, "PSK", ssid->psk, PMK_LEN);
353 #ifndef NO_CONFIG_WRITE
354 static char * wpa_config_write_psk(const struct parse_data *data,
355 struct wpa_ssid *ssid)
357 #ifdef CONFIG_EXT_PASSWORD
359 size_t len = 4 + os_strlen(ssid->ext_psk) + 1;
360 char *buf = os_malloc(len);
363 os_snprintf(buf, len, "ext:%s", ssid->ext_psk);
366 #endif /* CONFIG_EXT_PASSWORD */
368 if (ssid->passphrase)
369 return wpa_config_write_string_ascii(
370 (const u8 *) ssid->passphrase,
371 os_strlen(ssid->passphrase));
374 return wpa_config_write_string_hex(ssid->psk, PMK_LEN);
378 #endif /* NO_CONFIG_WRITE */
381 static int wpa_config_parse_proto(const struct parse_data *data,
382 struct wpa_ssid *ssid, int line,
385 int val = 0, last, errors = 0;
386 char *start, *end, *buf;
388 buf = os_strdup(value);
393 while (*start != '\0') {
394 while (*start == ' ' || *start == '\t')
399 while (*end != ' ' && *end != '\t' && *end != '\0')
403 if (os_strcmp(start, "WPA") == 0)
404 val |= WPA_PROTO_WPA;
405 else if (os_strcmp(start, "RSN") == 0 ||
406 os_strcmp(start, "WPA2") == 0)
407 val |= WPA_PROTO_RSN;
408 else if (os_strcmp(start, "OSEN") == 0)
409 val |= WPA_PROTO_OSEN;
411 wpa_printf(MSG_ERROR, "Line %d: invalid proto '%s'",
423 wpa_printf(MSG_ERROR,
424 "Line %d: no proto values configured.", line);
428 wpa_printf(MSG_MSGDUMP, "proto: 0x%x", val);
430 return errors ? -1 : 0;
434 #ifndef NO_CONFIG_WRITE
435 static char * wpa_config_write_proto(const struct parse_data *data,
436 struct wpa_ssid *ssid)
439 char *buf, *pos, *end;
441 pos = buf = os_zalloc(10);
446 if (ssid->proto & WPA_PROTO_WPA) {
447 ret = os_snprintf(pos, end - pos, "%sWPA", first ? "" : " ");
448 if (ret < 0 || ret >= end - pos)
454 if (ssid->proto & WPA_PROTO_RSN) {
455 ret = os_snprintf(pos, end - pos, "%sRSN", first ? "" : " ");
456 if (ret < 0 || ret >= end - pos)
464 #endif /* NO_CONFIG_WRITE */
467 static int wpa_config_parse_key_mgmt(const struct parse_data *data,
468 struct wpa_ssid *ssid, int line,
471 int val = 0, last, errors = 0;
472 char *start, *end, *buf;
474 buf = os_strdup(value);
479 while (*start != '\0') {
480 while (*start == ' ' || *start == '\t')
485 while (*end != ' ' && *end != '\t' && *end != '\0')
489 if (os_strcmp(start, "WPA-PSK") == 0)
490 val |= WPA_KEY_MGMT_PSK;
491 else if (os_strcmp(start, "WPA-EAP") == 0)
492 val |= WPA_KEY_MGMT_IEEE8021X;
493 else if (os_strcmp(start, "IEEE8021X") == 0)
494 val |= WPA_KEY_MGMT_IEEE8021X_NO_WPA;
495 else if (os_strcmp(start, "NONE") == 0)
496 val |= WPA_KEY_MGMT_NONE;
497 else if (os_strcmp(start, "WPA-NONE") == 0)
498 val |= WPA_KEY_MGMT_WPA_NONE;
499 #ifdef CONFIG_IEEE80211R
500 else if (os_strcmp(start, "FT-PSK") == 0)
501 val |= WPA_KEY_MGMT_FT_PSK;
502 else if (os_strcmp(start, "FT-EAP") == 0)
503 val |= WPA_KEY_MGMT_FT_IEEE8021X;
504 #endif /* CONFIG_IEEE80211R */
505 #ifdef CONFIG_IEEE80211W
506 else if (os_strcmp(start, "WPA-PSK-SHA256") == 0)
507 val |= WPA_KEY_MGMT_PSK_SHA256;
508 else if (os_strcmp(start, "WPA-EAP-SHA256") == 0)
509 val |= WPA_KEY_MGMT_IEEE8021X_SHA256;
510 #endif /* CONFIG_IEEE80211W */
512 else if (os_strcmp(start, "WPS") == 0)
513 val |= WPA_KEY_MGMT_WPS;
514 #endif /* CONFIG_WPS */
516 else if (os_strcmp(start, "SAE") == 0)
517 val |= WPA_KEY_MGMT_SAE;
518 else if (os_strcmp(start, "FT-SAE") == 0)
519 val |= WPA_KEY_MGMT_FT_SAE;
520 #endif /* CONFIG_SAE */
522 else if (os_strcmp(start, "OSEN") == 0)
523 val |= WPA_KEY_MGMT_OSEN;
524 #endif /* CONFIG_HS20 */
526 wpa_printf(MSG_ERROR, "Line %d: invalid key_mgmt '%s'",
538 wpa_printf(MSG_ERROR,
539 "Line %d: no key_mgmt values configured.", line);
543 wpa_printf(MSG_MSGDUMP, "key_mgmt: 0x%x", val);
544 ssid->key_mgmt = val;
545 return errors ? -1 : 0;
549 #ifndef NO_CONFIG_WRITE
550 static char * wpa_config_write_key_mgmt(const struct parse_data *data,
551 struct wpa_ssid *ssid)
553 char *buf, *pos, *end;
556 pos = buf = os_zalloc(100);
561 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK) {
562 ret = os_snprintf(pos, end - pos, "%sWPA-PSK",
563 pos == buf ? "" : " ");
564 if (ret < 0 || ret >= end - pos) {
571 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
572 ret = os_snprintf(pos, end - pos, "%sWPA-EAP",
573 pos == buf ? "" : " ");
574 if (ret < 0 || ret >= end - pos) {
581 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
582 ret = os_snprintf(pos, end - pos, "%sIEEE8021X",
583 pos == buf ? "" : " ");
584 if (ret < 0 || ret >= end - pos) {
591 if (ssid->key_mgmt & WPA_KEY_MGMT_NONE) {
592 ret = os_snprintf(pos, end - pos, "%sNONE",
593 pos == buf ? "" : " ");
594 if (ret < 0 || ret >= end - pos) {
601 if (ssid->key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
602 ret = os_snprintf(pos, end - pos, "%sWPA-NONE",
603 pos == buf ? "" : " ");
604 if (ret < 0 || ret >= end - pos) {
611 #ifdef CONFIG_IEEE80211R
612 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_PSK) {
613 ret = os_snprintf(pos, end - pos, "%sFT-PSK",
614 pos == buf ? "" : " ");
615 if (ret < 0 || ret >= end - pos) {
622 if (ssid->key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
623 ret = os_snprintf(pos, end - pos, "%sFT-EAP",
624 pos == buf ? "" : " ");
625 if (ret < 0 || ret >= end - pos) {
631 #endif /* CONFIG_IEEE80211R */
633 #ifdef CONFIG_IEEE80211W
634 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
635 ret = os_snprintf(pos, end - pos, "%sWPA-PSK-SHA256",
636 pos == buf ? "" : " ");
637 if (ret < 0 || ret >= end - pos) {
644 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
645 ret = os_snprintf(pos, end - pos, "%sWPA-EAP-SHA256",
646 pos == buf ? "" : " ");
647 if (ret < 0 || ret >= end - pos) {
653 #endif /* CONFIG_IEEE80211W */
656 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
657 ret = os_snprintf(pos, end - pos, "%sWPS",
658 pos == buf ? "" : " ");
659 if (ret < 0 || ret >= end - pos) {
665 #endif /* CONFIG_WPS */
669 #endif /* NO_CONFIG_WRITE */
672 static int wpa_config_parse_cipher(int line, const char *value)
674 int val = wpa_parse_cipher(value);
676 wpa_printf(MSG_ERROR, "Line %d: invalid cipher '%s'.",
681 wpa_printf(MSG_ERROR, "Line %d: no cipher values configured.",
689 #ifndef NO_CONFIG_WRITE
690 static char * wpa_config_write_cipher(int cipher)
692 char *buf = os_zalloc(50);
696 if (wpa_write_ciphers(buf, buf + 50, cipher, " ") < 0) {
703 #endif /* NO_CONFIG_WRITE */
706 static int wpa_config_parse_pairwise(const struct parse_data *data,
707 struct wpa_ssid *ssid, int line,
711 val = wpa_config_parse_cipher(line, value);
714 if (val & ~WPA_ALLOWED_PAIRWISE_CIPHERS) {
715 wpa_printf(MSG_ERROR, "Line %d: not allowed pairwise cipher "
716 "(0x%x).", line, val);
720 wpa_printf(MSG_MSGDUMP, "pairwise: 0x%x", val);
721 ssid->pairwise_cipher = val;
726 #ifndef NO_CONFIG_WRITE
727 static char * wpa_config_write_pairwise(const struct parse_data *data,
728 struct wpa_ssid *ssid)
730 return wpa_config_write_cipher(ssid->pairwise_cipher);
732 #endif /* NO_CONFIG_WRITE */
735 static int wpa_config_parse_group(const struct parse_data *data,
736 struct wpa_ssid *ssid, int line,
740 val = wpa_config_parse_cipher(line, value);
743 if (val & ~WPA_ALLOWED_GROUP_CIPHERS) {
744 wpa_printf(MSG_ERROR, "Line %d: not allowed group cipher "
745 "(0x%x).", line, val);
749 wpa_printf(MSG_MSGDUMP, "group: 0x%x", val);
750 ssid->group_cipher = val;
755 #ifndef NO_CONFIG_WRITE
756 static char * wpa_config_write_group(const struct parse_data *data,
757 struct wpa_ssid *ssid)
759 return wpa_config_write_cipher(ssid->group_cipher);
761 #endif /* NO_CONFIG_WRITE */
764 static int wpa_config_parse_auth_alg(const struct parse_data *data,
765 struct wpa_ssid *ssid, int line,
768 int val = 0, last, errors = 0;
769 char *start, *end, *buf;
771 buf = os_strdup(value);
776 while (*start != '\0') {
777 while (*start == ' ' || *start == '\t')
782 while (*end != ' ' && *end != '\t' && *end != '\0')
786 if (os_strcmp(start, "OPEN") == 0)
787 val |= WPA_AUTH_ALG_OPEN;
788 else if (os_strcmp(start, "SHARED") == 0)
789 val |= WPA_AUTH_ALG_SHARED;
790 else if (os_strcmp(start, "LEAP") == 0)
791 val |= WPA_AUTH_ALG_LEAP;
793 wpa_printf(MSG_ERROR, "Line %d: invalid auth_alg '%s'",
805 wpa_printf(MSG_ERROR,
806 "Line %d: no auth_alg values configured.", line);
810 wpa_printf(MSG_MSGDUMP, "auth_alg: 0x%x", val);
811 ssid->auth_alg = val;
812 return errors ? -1 : 0;
816 #ifndef NO_CONFIG_WRITE
817 static char * wpa_config_write_auth_alg(const struct parse_data *data,
818 struct wpa_ssid *ssid)
820 char *buf, *pos, *end;
823 pos = buf = os_zalloc(30);
828 if (ssid->auth_alg & WPA_AUTH_ALG_OPEN) {
829 ret = os_snprintf(pos, end - pos, "%sOPEN",
830 pos == buf ? "" : " ");
831 if (ret < 0 || ret >= end - pos) {
838 if (ssid->auth_alg & WPA_AUTH_ALG_SHARED) {
839 ret = os_snprintf(pos, end - pos, "%sSHARED",
840 pos == buf ? "" : " ");
841 if (ret < 0 || ret >= end - pos) {
848 if (ssid->auth_alg & WPA_AUTH_ALG_LEAP) {
849 ret = os_snprintf(pos, end - pos, "%sLEAP",
850 pos == buf ? "" : " ");
851 if (ret < 0 || ret >= end - pos) {
860 #endif /* NO_CONFIG_WRITE */
863 static int * wpa_config_parse_int_array(const char *value)
871 freqs = os_calloc(len + 1, sizeof(int));
882 n = os_realloc_array(freqs, len * 2 + 1, sizeof(int));
887 for (i = len; i <= len * 2; i++)
893 freqs[used] = atoi(pos);
894 if (freqs[used] == 0)
897 pos = os_strchr(pos + 1, ' ');
904 static int wpa_config_parse_scan_freq(const struct parse_data *data,
905 struct wpa_ssid *ssid, int line,
910 freqs = wpa_config_parse_int_array(value);
917 os_free(ssid->scan_freq);
918 ssid->scan_freq = freqs;
924 static int wpa_config_parse_freq_list(const struct parse_data *data,
925 struct wpa_ssid *ssid, int line,
930 freqs = wpa_config_parse_int_array(value);
937 os_free(ssid->freq_list);
938 ssid->freq_list = freqs;
944 #ifndef NO_CONFIG_WRITE
945 static char * wpa_config_write_freqs(const struct parse_data *data,
948 char *buf, *pos, *end;
956 for (i = 0; freqs[i]; i++)
959 pos = buf = os_zalloc(10 * count + 1);
962 end = buf + 10 * count + 1;
964 for (i = 0; freqs[i]; i++) {
965 ret = os_snprintf(pos, end - pos, "%s%u",
966 i == 0 ? "" : " ", freqs[i]);
967 if (ret < 0 || ret >= end - pos) {
978 static char * wpa_config_write_scan_freq(const struct parse_data *data,
979 struct wpa_ssid *ssid)
981 return wpa_config_write_freqs(data, ssid->scan_freq);
985 static char * wpa_config_write_freq_list(const struct parse_data *data,
986 struct wpa_ssid *ssid)
988 return wpa_config_write_freqs(data, ssid->freq_list);
990 #endif /* NO_CONFIG_WRITE */
993 #ifdef IEEE8021X_EAPOL
994 static int wpa_config_parse_eap(const struct parse_data *data,
995 struct wpa_ssid *ssid, int line,
998 int last, errors = 0;
999 char *start, *end, *buf;
1000 struct eap_method_type *methods = NULL, *tmp;
1001 size_t num_methods = 0;
1003 buf = os_strdup(value);
1008 while (*start != '\0') {
1009 while (*start == ' ' || *start == '\t')
1014 while (*end != ' ' && *end != '\t' && *end != '\0')
1016 last = *end == '\0';
1019 methods = os_realloc_array(methods, num_methods + 1,
1021 if (methods == NULL) {
1026 methods[num_methods].method = eap_peer_get_type(
1027 start, &methods[num_methods].vendor);
1028 if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
1029 methods[num_methods].method == EAP_TYPE_NONE) {
1030 wpa_printf(MSG_ERROR, "Line %d: unknown EAP method "
1031 "'%s'", line, start);
1032 wpa_printf(MSG_ERROR, "You may need to add support for"
1033 " this EAP method during wpa_supplicant\n"
1034 "build time configuration.\n"
1035 "See README for more information.");
1037 } else if (methods[num_methods].vendor == EAP_VENDOR_IETF &&
1038 methods[num_methods].method == EAP_TYPE_LEAP)
1050 methods = os_realloc_array(methods, num_methods + 1, sizeof(*methods));
1051 if (methods == NULL) {
1055 methods[num_methods].vendor = EAP_VENDOR_IETF;
1056 methods[num_methods].method = EAP_TYPE_NONE;
1059 wpa_hexdump(MSG_MSGDUMP, "eap methods",
1060 (u8 *) methods, num_methods * sizeof(*methods));
1061 os_free(ssid->eap.eap_methods);
1062 ssid->eap.eap_methods = methods;
1063 return errors ? -1 : 0;
1067 static char * wpa_config_write_eap(const struct parse_data *data,
1068 struct wpa_ssid *ssid)
1071 char *buf, *pos, *end;
1072 const struct eap_method_type *eap_methods = ssid->eap.eap_methods;
1075 if (eap_methods == NULL)
1078 pos = buf = os_zalloc(100);
1083 for (i = 0; eap_methods[i].vendor != EAP_VENDOR_IETF ||
1084 eap_methods[i].method != EAP_TYPE_NONE; i++) {
1085 name = eap_get_name(eap_methods[i].vendor,
1086 eap_methods[i].method);
1088 ret = os_snprintf(pos, end - pos, "%s%s",
1089 pos == buf ? "" : " ", name);
1090 if (ret < 0 || ret >= end - pos)
1102 static int wpa_config_parse_password(const struct parse_data *data,
1103 struct wpa_ssid *ssid, int line,
1108 if (os_strcmp(value, "NULL") == 0) {
1109 wpa_printf(MSG_DEBUG, "Unset configuration string 'password'");
1110 os_free(ssid->eap.password);
1111 ssid->eap.password = NULL;
1112 ssid->eap.password_len = 0;
1116 #ifdef CONFIG_EXT_PASSWORD
1117 if (os_strncmp(value, "ext:", 4) == 0) {
1118 char *name = os_strdup(value + 4);
1121 os_free(ssid->eap.password);
1122 ssid->eap.password = (u8 *) name;
1123 ssid->eap.password_len = os_strlen(name);
1124 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
1125 ssid->eap.flags |= EAP_CONFIG_FLAGS_EXT_PASSWORD;
1128 #endif /* CONFIG_EXT_PASSWORD */
1130 if (os_strncmp(value, "hash:", 5) != 0) {
1134 tmp = wpa_config_parse_string(value, &res_len);
1136 wpa_printf(MSG_ERROR, "Line %d: failed to parse "
1140 wpa_hexdump_ascii_key(MSG_MSGDUMP, data->name,
1141 (u8 *) tmp, res_len);
1143 os_free(ssid->eap.password);
1144 ssid->eap.password = (u8 *) tmp;
1145 ssid->eap.password_len = res_len;
1146 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
1147 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_PASSWORD;
1153 /* NtPasswordHash: hash:<32 hex digits> */
1154 if (os_strlen(value + 5) != 2 * 16) {
1155 wpa_printf(MSG_ERROR, "Line %d: Invalid password hash length "
1156 "(expected 32 hex digits)", line);
1160 hash = os_malloc(16);
1164 if (hexstr2bin(value + 5, hash, 16)) {
1166 wpa_printf(MSG_ERROR, "Line %d: Invalid password hash", line);
1170 wpa_hexdump_key(MSG_MSGDUMP, data->name, hash, 16);
1172 os_free(ssid->eap.password);
1173 ssid->eap.password = hash;
1174 ssid->eap.password_len = 16;
1175 ssid->eap.flags |= EAP_CONFIG_FLAGS_PASSWORD_NTHASH;
1176 ssid->eap.flags &= ~EAP_CONFIG_FLAGS_EXT_PASSWORD;
1182 static char * wpa_config_write_password(const struct parse_data *data,
1183 struct wpa_ssid *ssid)
1187 if (ssid->eap.password == NULL)
1190 #ifdef CONFIG_EXT_PASSWORD
1191 if (ssid->eap.flags & EAP_CONFIG_FLAGS_EXT_PASSWORD) {
1192 buf = os_zalloc(4 + ssid->eap.password_len + 1);
1195 os_memcpy(buf, "ext:", 4);
1196 os_memcpy(buf + 4, ssid->eap.password, ssid->eap.password_len);
1199 #endif /* CONFIG_EXT_PASSWORD */
1201 if (!(ssid->eap.flags & EAP_CONFIG_FLAGS_PASSWORD_NTHASH)) {
1202 return wpa_config_write_string(
1203 ssid->eap.password, ssid->eap.password_len);
1206 buf = os_malloc(5 + 32 + 1);
1210 os_memcpy(buf, "hash:", 5);
1211 wpa_snprintf_hex(buf + 5, 32 + 1, ssid->eap.password, 16);
1215 #endif /* IEEE8021X_EAPOL */
1218 static int wpa_config_parse_wep_key(u8 *key, size_t *len, int line,
1219 const char *value, int idx)
1221 char *buf, title[20];
1224 buf = wpa_config_parse_string(value, len);
1226 wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key %d '%s'.",
1230 if (*len > MAX_WEP_KEY_LEN) {
1231 wpa_printf(MSG_ERROR, "Line %d: Too long WEP key %d '%s'.",
1236 if (*len && *len != 5 && *len != 13 && *len != 16) {
1237 wpa_printf(MSG_ERROR, "Line %d: Invalid WEP key length %u - "
1238 "this network block will be ignored",
1239 line, (unsigned int) *len);
1241 os_memcpy(key, buf, *len);
1243 res = os_snprintf(title, sizeof(title), "wep_key%d", idx);
1244 if (res >= 0 && (size_t) res < sizeof(title))
1245 wpa_hexdump_key(MSG_MSGDUMP, title, key, *len);
1250 static int wpa_config_parse_wep_key0(const struct parse_data *data,
1251 struct wpa_ssid *ssid, int line,
1254 return wpa_config_parse_wep_key(ssid->wep_key[0],
1255 &ssid->wep_key_len[0], line,
1260 static int wpa_config_parse_wep_key1(const struct parse_data *data,
1261 struct wpa_ssid *ssid, int line,
1264 return wpa_config_parse_wep_key(ssid->wep_key[1],
1265 &ssid->wep_key_len[1], line,
1270 static int wpa_config_parse_wep_key2(const struct parse_data *data,
1271 struct wpa_ssid *ssid, int line,
1274 return wpa_config_parse_wep_key(ssid->wep_key[2],
1275 &ssid->wep_key_len[2], line,
1280 static int wpa_config_parse_wep_key3(const struct parse_data *data,
1281 struct wpa_ssid *ssid, int line,
1284 return wpa_config_parse_wep_key(ssid->wep_key[3],
1285 &ssid->wep_key_len[3], line,
1290 #ifndef NO_CONFIG_WRITE
1291 static char * wpa_config_write_wep_key(struct wpa_ssid *ssid, int idx)
1293 if (ssid->wep_key_len[idx] == 0)
1295 return wpa_config_write_string(ssid->wep_key[idx],
1296 ssid->wep_key_len[idx]);
1300 static char * wpa_config_write_wep_key0(const struct parse_data *data,
1301 struct wpa_ssid *ssid)
1303 return wpa_config_write_wep_key(ssid, 0);
1307 static char * wpa_config_write_wep_key1(const struct parse_data *data,
1308 struct wpa_ssid *ssid)
1310 return wpa_config_write_wep_key(ssid, 1);
1314 static char * wpa_config_write_wep_key2(const struct parse_data *data,
1315 struct wpa_ssid *ssid)
1317 return wpa_config_write_wep_key(ssid, 2);
1321 static char * wpa_config_write_wep_key3(const struct parse_data *data,
1322 struct wpa_ssid *ssid)
1324 return wpa_config_write_wep_key(ssid, 3);
1326 #endif /* NO_CONFIG_WRITE */
1331 static int wpa_config_parse_go_p2p_dev_addr(const struct parse_data *data,
1332 struct wpa_ssid *ssid, int line,
1335 if (value[0] == '\0' || os_strcmp(value, "\"\"") == 0 ||
1336 os_strcmp(value, "any") == 0) {
1337 os_memset(ssid->go_p2p_dev_addr, 0, ETH_ALEN);
1338 wpa_printf(MSG_MSGDUMP, "GO P2P Device Address any");
1341 if (hwaddr_aton(value, ssid->go_p2p_dev_addr)) {
1342 wpa_printf(MSG_ERROR, "Line %d: Invalid GO P2P Device Address '%s'.",
1346 ssid->bssid_set = 1;
1347 wpa_printf(MSG_MSGDUMP, "GO P2P Device Address " MACSTR,
1348 MAC2STR(ssid->go_p2p_dev_addr));
1353 #ifndef NO_CONFIG_WRITE
1354 static char * wpa_config_write_go_p2p_dev_addr(const struct parse_data *data,
1355 struct wpa_ssid *ssid)
1360 if (is_zero_ether_addr(ssid->go_p2p_dev_addr))
1363 value = os_malloc(20);
1366 res = os_snprintf(value, 20, MACSTR, MAC2STR(ssid->go_p2p_dev_addr));
1367 if (res < 0 || res >= 20) {
1371 value[20 - 1] = '\0';
1374 #endif /* NO_CONFIG_WRITE */
1377 static int wpa_config_parse_p2p_client_list(const struct parse_data *data,
1378 struct wpa_ssid *ssid, int line,
1382 u8 *buf, *n, addr[ETH_ALEN];
1389 while (pos && *pos) {
1393 if (hwaddr_aton(pos, addr)) {
1395 wpa_printf(MSG_ERROR, "Line %d: Invalid "
1396 "p2p_client_list address '%s'.",
1401 /* continue anyway since this could have been from a
1402 * truncated configuration file line */
1403 wpa_printf(MSG_INFO, "Line %d: Ignore likely "
1404 "truncated p2p_client_list address '%s'",
1407 n = os_realloc_array(buf, count + 1, ETH_ALEN);
1413 os_memmove(buf + ETH_ALEN, buf, count * ETH_ALEN);
1414 os_memcpy(buf, addr, ETH_ALEN);
1416 wpa_hexdump(MSG_MSGDUMP, "p2p_client_list",
1420 pos = os_strchr(pos, ' ');
1423 os_free(ssid->p2p_client_list);
1424 ssid->p2p_client_list = buf;
1425 ssid->num_p2p_clients = count;
1431 #ifndef NO_CONFIG_WRITE
1432 static char * wpa_config_write_p2p_client_list(const struct parse_data *data,
1433 struct wpa_ssid *ssid)
1435 char *value, *end, *pos;
1439 if (ssid->p2p_client_list == NULL || ssid->num_p2p_clients == 0)
1442 value = os_malloc(20 * ssid->num_p2p_clients);
1446 end = value + 20 * ssid->num_p2p_clients;
1448 for (i = ssid->num_p2p_clients; i > 0; i--) {
1449 res = os_snprintf(pos, end - pos, MACSTR " ",
1450 MAC2STR(ssid->p2p_client_list +
1451 (i - 1) * ETH_ALEN));
1452 if (res < 0 || res >= end - pos) {
1464 #endif /* NO_CONFIG_WRITE */
1467 static int wpa_config_parse_psk_list(const struct parse_data *data,
1468 struct wpa_ssid *ssid, int line,
1471 struct psk_list_entry *p;
1474 p = os_zalloc(sizeof(*p));
1479 if (os_strncmp(pos, "P2P-", 4) == 0) {
1484 if (hwaddr_aton(pos, p->addr)) {
1485 wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list address '%s'",
1492 wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list '%s'",
1499 if (hexstr2bin(pos, p->psk, PMK_LEN) || pos[PMK_LEN * 2] != '\0') {
1500 wpa_printf(MSG_ERROR, "Line %d: Invalid psk_list PSK '%s'",
1506 dl_list_add(&ssid->psk_list, &p->list);
1512 #ifndef NO_CONFIG_WRITE
1513 static char * wpa_config_write_psk_list(const struct parse_data *data,
1514 struct wpa_ssid *ssid)
1518 #endif /* NO_CONFIG_WRITE */
1520 #endif /* CONFIG_P2P */
1522 /* Helper macros for network block parser */
1527 /* OFFSET: Get offset of a variable within the wpa_ssid structure */
1528 #define OFFSET(v) ((void *) &((struct wpa_ssid *) 0)->v)
1530 /* STR: Define a string variable for an ASCII string; f = field name */
1531 #ifdef NO_CONFIG_WRITE
1532 #define _STR(f) #f, wpa_config_parse_str, OFFSET(f)
1533 #define _STRe(f) #f, wpa_config_parse_str, OFFSET(eap.f)
1534 #else /* NO_CONFIG_WRITE */
1535 #define _STR(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(f)
1536 #define _STRe(f) #f, wpa_config_parse_str, wpa_config_write_str, OFFSET(eap.f)
1537 #endif /* NO_CONFIG_WRITE */
1538 #define STR(f) _STR(f), NULL, NULL, NULL, 0
1539 #define STRe(f) _STRe(f), NULL, NULL, NULL, 0
1540 #define STR_KEY(f) _STR(f), NULL, NULL, NULL, 1
1541 #define STR_KEYe(f) _STRe(f), NULL, NULL, NULL, 1
1543 /* STR_LEN: Define a string variable with a separate variable for storing the
1544 * data length. Unlike STR(), this can be used to store arbitrary binary data
1545 * (i.e., even nul termination character). */
1546 #define _STR_LEN(f) _STR(f), OFFSET(f ## _len)
1547 #define _STR_LENe(f) _STRe(f), OFFSET(eap.f ## _len)
1548 #define STR_LEN(f) _STR_LEN(f), NULL, NULL, 0
1549 #define STR_LENe(f) _STR_LENe(f), NULL, NULL, 0
1550 #define STR_LEN_KEY(f) _STR_LEN(f), NULL, NULL, 1
1552 /* STR_RANGE: Like STR_LEN(), but with minimum and maximum allowed length
1553 * explicitly specified. */
1554 #define _STR_RANGE(f, min, max) _STR_LEN(f), (void *) (min), (void *) (max)
1555 #define STR_RANGE(f, min, max) _STR_RANGE(f, min, max), 0
1556 #define STR_RANGE_KEY(f, min, max) _STR_RANGE(f, min, max), 1
1558 #ifdef NO_CONFIG_WRITE
1559 #define _INT(f) #f, wpa_config_parse_int, OFFSET(f), (void *) 0
1560 #define _INTe(f) #f, wpa_config_parse_int, OFFSET(eap.f), (void *) 0
1561 #else /* NO_CONFIG_WRITE */
1562 #define _INT(f) #f, wpa_config_parse_int, wpa_config_write_int, \
1563 OFFSET(f), (void *) 0
1564 #define _INTe(f) #f, wpa_config_parse_int, wpa_config_write_int, \
1565 OFFSET(eap.f), (void *) 0
1566 #endif /* NO_CONFIG_WRITE */
1568 /* INT: Define an integer variable */
1569 #define INT(f) _INT(f), NULL, NULL, 0
1570 #define INTe(f) _INTe(f), NULL, NULL, 0
1572 /* INT_RANGE: Define an integer variable with allowed value range */
1573 #define INT_RANGE(f, min, max) _INT(f), (void *) (min), (void *) (max), 0
1575 /* FUNC: Define a configuration variable that uses a custom function for
1576 * parsing and writing the value. */
1577 #ifdef NO_CONFIG_WRITE
1578 #define _FUNC(f) #f, wpa_config_parse_ ## f, NULL, NULL, NULL, NULL
1579 #else /* NO_CONFIG_WRITE */
1580 #define _FUNC(f) #f, wpa_config_parse_ ## f, wpa_config_write_ ## f, \
1581 NULL, NULL, NULL, NULL
1582 #endif /* NO_CONFIG_WRITE */
1583 #define FUNC(f) _FUNC(f), 0
1584 #define FUNC_KEY(f) _FUNC(f), 1
1587 * Table of network configuration variables. This table is used to parse each
1588 * network configuration variable, e.g., each line in wpa_supplicant.conf file
1589 * that is inside a network block.
1591 * This table is generated using the helper macros defined above and with
1592 * generous help from the C pre-processor. The field name is stored as a string
1593 * into .name and for STR and INT types, the offset of the target buffer within
1594 * struct wpa_ssid is stored in .param1. .param2 (if not NULL) is similar
1595 * offset to the field containing the length of the configuration variable.
1596 * .param3 and .param4 can be used to mark the allowed range (length for STR
1597 * and value for INT).
1599 * For each configuration line in wpa_supplicant.conf, the parser goes through
1600 * this table and select the entry that matches with the field name. The parser
1601 * function (.parser) is then called to parse the actual value of the field.
1603 * This kind of mechanism makes it easy to add new configuration parameters,
1604 * since only one line needs to be added into this table and into the
1605 * struct wpa_ssid definition if the new variable is either a string or
1606 * integer. More complex types will need to use their own parser and writer
1609 static const struct parse_data ssid_fields[] = {
1610 { STR_RANGE(ssid, 0, MAX_SSID_LEN) },
1611 { INT_RANGE(scan_ssid, 0, 1) },
1616 { INT(bg_scan_period) },
1620 { FUNC(scan_freq) },
1621 { FUNC(freq_list) },
1622 #ifdef IEEE8021X_EAPOL
1624 { STR_LENe(identity) },
1625 { STR_LENe(anonymous_identity) },
1626 { FUNC_KEY(password) },
1629 { STRe(client_cert) },
1630 { STRe(private_key) },
1631 { STR_KEYe(private_key_passwd) },
1633 { STRe(subject_match) },
1634 { STRe(altsubject_match) },
1635 { STRe(domain_suffix_match) },
1638 { STRe(client_cert2) },
1639 { STRe(private_key2) },
1640 { STR_KEYe(private_key2_passwd) },
1642 { STRe(subject_match2) },
1643 { STRe(altsubject_match2) },
1644 { STRe(domain_suffix_match2) },
1649 { STRe(engine_id) },
1652 { STRe(ca_cert_id) },
1654 { STRe(engine2_id) },
1657 { STRe(ca_cert2_id) },
1660 { INT(eapol_flags) },
1661 #endif /* IEEE8021X_EAPOL */
1662 { FUNC_KEY(wep_key0) },
1663 { FUNC_KEY(wep_key1) },
1664 { FUNC_KEY(wep_key2) },
1665 { FUNC_KEY(wep_key3) },
1666 { INT(wep_tx_keyidx) },
1668 #ifdef IEEE8021X_EAPOL
1669 { INT(eap_workaround) },
1671 { INTe(fragment_size) },
1673 #endif /* IEEE8021X_EAPOL */
1674 { INT_RANGE(mode, 0, 4) },
1675 { INT_RANGE(proactive_key_caching, 0, 1) },
1676 { INT_RANGE(disabled, 0, 2) },
1678 #ifdef CONFIG_IEEE80211W
1679 { INT_RANGE(ieee80211w, 0, 2) },
1680 #endif /* CONFIG_IEEE80211W */
1681 { INT_RANGE(peerkey, 0, 1) },
1682 { INT_RANGE(mixed_cell, 0, 1) },
1683 { INT_RANGE(frequency, 0, 65000) },
1684 { INT(wpa_ptk_rekey) },
1686 { INT_RANGE(ignore_broadcast_ssid, 0, 2) },
1688 { FUNC(go_p2p_dev_addr) },
1689 { FUNC(p2p_client_list) },
1691 #endif /* CONFIG_P2P */
1692 #ifdef CONFIG_HT_OVERRIDES
1693 { INT_RANGE(disable_ht, 0, 1) },
1694 { INT_RANGE(disable_ht40, -1, 1) },
1695 { INT_RANGE(disable_sgi, 0, 1) },
1696 { INT_RANGE(disable_max_amsdu, -1, 1) },
1697 { INT_RANGE(ampdu_factor, -1, 3) },
1698 { INT_RANGE(ampdu_density, -1, 7) },
1700 #endif /* CONFIG_HT_OVERRIDES */
1701 #ifdef CONFIG_VHT_OVERRIDES
1702 { INT_RANGE(disable_vht, 0, 1) },
1704 { INT(vht_capa_mask) },
1705 { INT_RANGE(vht_rx_mcs_nss_1, -1, 3) },
1706 { INT_RANGE(vht_rx_mcs_nss_2, -1, 3) },
1707 { INT_RANGE(vht_rx_mcs_nss_3, -1, 3) },
1708 { INT_RANGE(vht_rx_mcs_nss_4, -1, 3) },
1709 { INT_RANGE(vht_rx_mcs_nss_5, -1, 3) },
1710 { INT_RANGE(vht_rx_mcs_nss_6, -1, 3) },
1711 { INT_RANGE(vht_rx_mcs_nss_7, -1, 3) },
1712 { INT_RANGE(vht_rx_mcs_nss_8, -1, 3) },
1713 { INT_RANGE(vht_tx_mcs_nss_1, -1, 3) },
1714 { INT_RANGE(vht_tx_mcs_nss_2, -1, 3) },
1715 { INT_RANGE(vht_tx_mcs_nss_3, -1, 3) },
1716 { INT_RANGE(vht_tx_mcs_nss_4, -1, 3) },
1717 { INT_RANGE(vht_tx_mcs_nss_5, -1, 3) },
1718 { INT_RANGE(vht_tx_mcs_nss_6, -1, 3) },
1719 { INT_RANGE(vht_tx_mcs_nss_7, -1, 3) },
1720 { INT_RANGE(vht_tx_mcs_nss_8, -1, 3) },
1721 #endif /* CONFIG_VHT_OVERRIDES */
1722 { INT(ap_max_inactivity) },
1723 { INT(dtim_period) },
1724 { INT(beacon_int) },
1736 #undef STR_RANGE_KEY
1743 #define NUM_SSID_FIELDS ARRAY_SIZE(ssid_fields)
1747 * wpa_config_add_prio_network - Add a network to priority lists
1748 * @config: Configuration data from wpa_config_read()
1749 * @ssid: Pointer to the network configuration to be added to the list
1750 * Returns: 0 on success, -1 on failure
1752 * This function is used to add a network block to the priority list of
1753 * networks. This must be called for each network when reading in the full
1754 * configuration. In addition, this can be used indirectly when updating
1755 * priorities by calling wpa_config_update_prio_list().
1757 int wpa_config_add_prio_network(struct wpa_config *config,
1758 struct wpa_ssid *ssid)
1761 struct wpa_ssid *prev, **nlist;
1764 * Add to an existing priority list if one is available for the
1765 * configured priority level for this network.
1767 for (prio = 0; prio < config->num_prio; prio++) {
1768 prev = config->pssid[prio];
1769 if (prev->priority == ssid->priority) {
1777 /* First network for this priority - add a new priority list */
1778 nlist = os_realloc_array(config->pssid, config->num_prio + 1,
1779 sizeof(struct wpa_ssid *));
1783 for (prio = 0; prio < config->num_prio; prio++) {
1784 if (nlist[prio]->priority < ssid->priority) {
1785 os_memmove(&nlist[prio + 1], &nlist[prio],
1786 (config->num_prio - prio) *
1787 sizeof(struct wpa_ssid *));
1794 config->pssid = nlist;
1801 * wpa_config_update_prio_list - Update network priority list
1802 * @config: Configuration data from wpa_config_read()
1803 * Returns: 0 on success, -1 on failure
1805 * This function is called to update the priority list of networks in the
1806 * configuration when a network is being added or removed. This is also called
1807 * if a priority for a network is changed.
1809 int wpa_config_update_prio_list(struct wpa_config *config)
1811 struct wpa_ssid *ssid;
1814 os_free(config->pssid);
1815 config->pssid = NULL;
1816 config->num_prio = 0;
1818 ssid = config->ssid;
1821 if (wpa_config_add_prio_network(config, ssid) < 0)
1830 #ifdef IEEE8021X_EAPOL
1831 static void eap_peer_config_free(struct eap_peer_config *eap)
1833 os_free(eap->eap_methods);
1834 os_free(eap->identity);
1835 os_free(eap->anonymous_identity);
1836 os_free(eap->password);
1837 os_free(eap->ca_cert);
1838 os_free(eap->ca_path);
1839 os_free(eap->client_cert);
1840 os_free(eap->private_key);
1841 os_free(eap->private_key_passwd);
1842 os_free(eap->dh_file);
1843 os_free(eap->subject_match);
1844 os_free(eap->altsubject_match);
1845 os_free(eap->domain_suffix_match);
1846 os_free(eap->ca_cert2);
1847 os_free(eap->ca_path2);
1848 os_free(eap->client_cert2);
1849 os_free(eap->private_key2);
1850 os_free(eap->private_key2_passwd);
1851 os_free(eap->dh_file2);
1852 os_free(eap->subject_match2);
1853 os_free(eap->altsubject_match2);
1854 os_free(eap->domain_suffix_match2);
1855 os_free(eap->phase1);
1856 os_free(eap->phase2);
1859 os_free(eap->engine_id);
1860 os_free(eap->key_id);
1861 os_free(eap->cert_id);
1862 os_free(eap->ca_cert_id);
1863 os_free(eap->key2_id);
1864 os_free(eap->cert2_id);
1865 os_free(eap->ca_cert2_id);
1867 os_free(eap->engine2_id);
1869 os_free(eap->pending_req_otp);
1870 os_free(eap->pac_file);
1871 os_free(eap->new_password);
1872 os_free(eap->external_sim_resp);
1874 #endif /* IEEE8021X_EAPOL */
1878 * wpa_config_free_ssid - Free network/ssid configuration data
1879 * @ssid: Configuration data for the network
1881 * This function frees all resources allocated for the network configuration
1884 void wpa_config_free_ssid(struct wpa_ssid *ssid)
1886 struct psk_list_entry *psk;
1888 os_free(ssid->ssid);
1889 os_free(ssid->passphrase);
1890 os_free(ssid->ext_psk);
1891 #ifdef IEEE8021X_EAPOL
1892 eap_peer_config_free(&ssid->eap);
1893 #endif /* IEEE8021X_EAPOL */
1894 os_free(ssid->id_str);
1895 os_free(ssid->scan_freq);
1896 os_free(ssid->freq_list);
1897 os_free(ssid->bgscan);
1898 os_free(ssid->p2p_client_list);
1899 #ifdef CONFIG_HT_OVERRIDES
1900 os_free(ssid->ht_mcs);
1901 #endif /* CONFIG_HT_OVERRIDES */
1902 while ((psk = dl_list_first(&ssid->psk_list, struct psk_list_entry,
1904 dl_list_del(&psk->list);
1911 void wpa_config_free_cred(struct wpa_cred *cred)
1915 os_free(cred->realm);
1916 os_free(cred->username);
1917 os_free(cred->password);
1918 os_free(cred->ca_cert);
1919 os_free(cred->client_cert);
1920 os_free(cred->private_key);
1921 os_free(cred->private_key_passwd);
1922 os_free(cred->imsi);
1923 os_free(cred->milenage);
1924 for (i = 0; i < cred->num_domain; i++)
1925 os_free(cred->domain[i]);
1926 os_free(cred->domain);
1927 os_free(cred->domain_suffix_match);
1928 os_free(cred->eap_method);
1929 os_free(cred->phase1);
1930 os_free(cred->phase2);
1931 os_free(cred->excluded_ssid);
1932 os_free(cred->roaming_partner);
1937 void wpa_config_flush_blobs(struct wpa_config *config)
1939 #ifndef CONFIG_NO_CONFIG_BLOBS
1940 struct wpa_config_blob *blob, *prev;
1942 blob = config->blobs;
1943 config->blobs = NULL;
1947 wpa_config_free_blob(prev);
1949 #endif /* CONFIG_NO_CONFIG_BLOBS */
1954 * wpa_config_free - Free configuration data
1955 * @config: Configuration data from wpa_config_read()
1957 * This function frees all resources allocated for the configuration data by
1958 * wpa_config_read().
1960 void wpa_config_free(struct wpa_config *config)
1962 struct wpa_ssid *ssid, *prev = NULL;
1963 struct wpa_cred *cred, *cprev;
1965 ssid = config->ssid;
1969 wpa_config_free_ssid(prev);
1972 cred = config->cred;
1976 wpa_config_free_cred(cprev);
1979 wpa_config_flush_blobs(config);
1981 wpabuf_free(config->wps_vendor_ext_m1);
1982 os_free(config->ctrl_interface);
1983 os_free(config->ctrl_interface_group);
1984 os_free(config->opensc_engine_path);
1985 os_free(config->pkcs11_engine_path);
1986 os_free(config->pkcs11_module_path);
1987 os_free(config->pcsc_reader);
1988 os_free(config->pcsc_pin);
1989 os_free(config->driver_param);
1990 os_free(config->device_name);
1991 os_free(config->manufacturer);
1992 os_free(config->model_name);
1993 os_free(config->model_number);
1994 os_free(config->serial_number);
1995 os_free(config->config_methods);
1996 os_free(config->p2p_ssid_postfix);
1997 os_free(config->pssid);
1998 os_free(config->p2p_pref_chan);
1999 os_free(config->p2p_no_go_freq.range);
2000 os_free(config->autoscan);
2001 os_free(config->freq_list);
2002 wpabuf_free(config->wps_nfc_dh_pubkey);
2003 wpabuf_free(config->wps_nfc_dh_privkey);
2004 wpabuf_free(config->wps_nfc_dev_pw);
2005 os_free(config->ext_password_backend);
2006 os_free(config->sae_groups);
2007 wpabuf_free(config->ap_vendor_elements);
2008 os_free(config->osu_dir);
2014 * wpa_config_foreach_network - Iterate over each configured network
2015 * @config: Configuration data from wpa_config_read()
2016 * @func: Callback function to process each network
2017 * @arg: Opaque argument to pass to callback function
2019 * Iterate over the set of configured networks calling the specified
2020 * function for each item. We guard against callbacks removing the
2023 void wpa_config_foreach_network(struct wpa_config *config,
2024 void (*func)(void *, struct wpa_ssid *),
2027 struct wpa_ssid *ssid, *next;
2029 ssid = config->ssid;
2039 * wpa_config_get_network - Get configured network based on id
2040 * @config: Configuration data from wpa_config_read()
2041 * @id: Unique network id to search for
2042 * Returns: Network configuration or %NULL if not found
2044 struct wpa_ssid * wpa_config_get_network(struct wpa_config *config, int id)
2046 struct wpa_ssid *ssid;
2048 ssid = config->ssid;
2060 * wpa_config_add_network - Add a new network with empty configuration
2061 * @config: Configuration data from wpa_config_read()
2062 * Returns: The new network configuration or %NULL if operation failed
2064 struct wpa_ssid * wpa_config_add_network(struct wpa_config *config)
2067 struct wpa_ssid *ssid, *last = NULL;
2070 ssid = config->ssid;
2079 ssid = os_zalloc(sizeof(*ssid));
2083 dl_list_init(&ssid->psk_list);
2087 config->ssid = ssid;
2089 wpa_config_update_prio_list(config);
2096 * wpa_config_remove_network - Remove a configured network based on id
2097 * @config: Configuration data from wpa_config_read()
2098 * @id: Unique network id to search for
2099 * Returns: 0 on success, or -1 if the network was not found
2101 int wpa_config_remove_network(struct wpa_config *config, int id)
2103 struct wpa_ssid *ssid, *prev = NULL;
2105 ssid = config->ssid;
2117 prev->next = ssid->next;
2119 config->ssid = ssid->next;
2121 wpa_config_update_prio_list(config);
2122 wpa_config_free_ssid(ssid);
2128 * wpa_config_set_network_defaults - Set network default values
2129 * @ssid: Pointer to network configuration data
2131 void wpa_config_set_network_defaults(struct wpa_ssid *ssid)
2133 ssid->proto = DEFAULT_PROTO;
2134 ssid->pairwise_cipher = DEFAULT_PAIRWISE;
2135 ssid->group_cipher = DEFAULT_GROUP;
2136 ssid->key_mgmt = DEFAULT_KEY_MGMT;
2137 ssid->bg_scan_period = DEFAULT_BG_SCAN_PERIOD;
2138 #ifdef IEEE8021X_EAPOL
2139 ssid->eapol_flags = DEFAULT_EAPOL_FLAGS;
2140 ssid->eap_workaround = DEFAULT_EAP_WORKAROUND;
2141 ssid->eap.fragment_size = DEFAULT_FRAGMENT_SIZE;
2142 #endif /* IEEE8021X_EAPOL */
2143 #ifdef CONFIG_HT_OVERRIDES
2144 ssid->disable_ht = DEFAULT_DISABLE_HT;
2145 ssid->disable_ht40 = DEFAULT_DISABLE_HT40;
2146 ssid->disable_sgi = DEFAULT_DISABLE_SGI;
2147 ssid->disable_max_amsdu = DEFAULT_DISABLE_MAX_AMSDU;
2148 ssid->ampdu_factor = DEFAULT_AMPDU_FACTOR;
2149 ssid->ampdu_density = DEFAULT_AMPDU_DENSITY;
2150 #endif /* CONFIG_HT_OVERRIDES */
2151 #ifdef CONFIG_VHT_OVERRIDES
2152 ssid->vht_rx_mcs_nss_1 = -1;
2153 ssid->vht_rx_mcs_nss_2 = -1;
2154 ssid->vht_rx_mcs_nss_3 = -1;
2155 ssid->vht_rx_mcs_nss_4 = -1;
2156 ssid->vht_rx_mcs_nss_5 = -1;
2157 ssid->vht_rx_mcs_nss_6 = -1;
2158 ssid->vht_rx_mcs_nss_7 = -1;
2159 ssid->vht_rx_mcs_nss_8 = -1;
2160 ssid->vht_tx_mcs_nss_1 = -1;
2161 ssid->vht_tx_mcs_nss_2 = -1;
2162 ssid->vht_tx_mcs_nss_3 = -1;
2163 ssid->vht_tx_mcs_nss_4 = -1;
2164 ssid->vht_tx_mcs_nss_5 = -1;
2165 ssid->vht_tx_mcs_nss_6 = -1;
2166 ssid->vht_tx_mcs_nss_7 = -1;
2167 ssid->vht_tx_mcs_nss_8 = -1;
2168 #endif /* CONFIG_VHT_OVERRIDES */
2169 ssid->proactive_key_caching = -1;
2170 #ifdef CONFIG_IEEE80211W
2171 ssid->ieee80211w = MGMT_FRAME_PROTECTION_DEFAULT;
2172 #endif /* CONFIG_IEEE80211W */
2177 * wpa_config_set - Set a variable in network configuration
2178 * @ssid: Pointer to network configuration data
2179 * @var: Variable name, e.g., "ssid"
2180 * @value: Variable value
2181 * @line: Line number in configuration file or 0 if not used
2182 * Returns: 0 on success, -1 on failure
2184 * This function can be used to set network configuration variables based on
2185 * both the configuration file and management interface input. The value
2186 * parameter must be in the same format as the text-based configuration file is
2187 * using. For example, strings are using double quotation marks.
2189 int wpa_config_set(struct wpa_ssid *ssid, const char *var, const char *value,
2195 if (ssid == NULL || var == NULL || value == NULL)
2198 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2199 const struct parse_data *field = &ssid_fields[i];
2200 if (os_strcmp(var, field->name) != 0)
2203 if (field->parser(field, ssid, line, value)) {
2205 wpa_printf(MSG_ERROR, "Line %d: failed to "
2206 "parse %s '%s'.", line, var, value);
2212 if (i == NUM_SSID_FIELDS) {
2214 wpa_printf(MSG_ERROR, "Line %d: unknown network field "
2215 "'%s'.", line, var);
2224 int wpa_config_set_quoted(struct wpa_ssid *ssid, const char *var,
2231 len = os_strlen(value);
2232 buf = os_malloc(len + 3);
2236 os_memcpy(buf + 1, value, len);
2238 buf[len + 2] = '\0';
2239 ret = wpa_config_set(ssid, var, buf, 0);
2246 * wpa_config_get_all - Get all options from network configuration
2247 * @ssid: Pointer to network configuration data
2248 * @get_keys: Determines if keys/passwords will be included in returned list
2249 * (if they may be exported)
2250 * Returns: %NULL terminated list of all set keys and their values in the form
2251 * of [key1, val1, key2, val2, ... , NULL]
2253 * This function can be used to get list of all configured network properties.
2254 * The caller is responsible for freeing the returned list and all its
2257 char ** wpa_config_get_all(struct wpa_ssid *ssid, int get_keys)
2259 const struct parse_data *field;
2265 get_keys = get_keys && ssid->export_keys;
2267 props = os_calloc(2 * NUM_SSID_FIELDS + 1, sizeof(char *));
2272 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2273 field = &ssid_fields[i];
2274 if (field->key_data && !get_keys)
2276 value = field->writer(field, ssid);
2279 if (os_strlen(value) == 0) {
2284 key = os_strdup(field->name);
2290 props[fields_num * 2] = key;
2291 props[fields_num * 2 + 1] = value;
2307 #ifndef NO_CONFIG_WRITE
2309 * wpa_config_get - Get a variable in network configuration
2310 * @ssid: Pointer to network configuration data
2311 * @var: Variable name, e.g., "ssid"
2312 * Returns: Value of the variable or %NULL on failure
2314 * This function can be used to get network configuration variables. The
2315 * returned value is a copy of the configuration variable in text format, i.e,.
2316 * the same format that the text-based configuration file and wpa_config_set()
2317 * are using for the value. The caller is responsible for freeing the returned
2320 char * wpa_config_get(struct wpa_ssid *ssid, const char *var)
2324 if (ssid == NULL || var == NULL)
2327 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2328 const struct parse_data *field = &ssid_fields[i];
2329 if (os_strcmp(var, field->name) == 0)
2330 return field->writer(field, ssid);
2338 * wpa_config_get_no_key - Get a variable in network configuration (no keys)
2339 * @ssid: Pointer to network configuration data
2340 * @var: Variable name, e.g., "ssid"
2341 * Returns: Value of the variable or %NULL on failure
2343 * This function can be used to get network configuration variable like
2344 * wpa_config_get(). The only difference is that this functions does not expose
2345 * key/password material from the configuration. In case a key/password field
2346 * is requested, the returned value is an empty string or %NULL if the variable
2347 * is not set or "*" if the variable is set (regardless of its value). The
2348 * returned value is a copy of the configuration variable in text format, i.e,.
2349 * the same format that the text-based configuration file and wpa_config_set()
2350 * are using for the value. The caller is responsible for freeing the returned
2353 char * wpa_config_get_no_key(struct wpa_ssid *ssid, const char *var)
2357 if (ssid == NULL || var == NULL)
2360 for (i = 0; i < NUM_SSID_FIELDS; i++) {
2361 const struct parse_data *field = &ssid_fields[i];
2362 if (os_strcmp(var, field->name) == 0) {
2363 char *res = field->writer(field, ssid);
2364 if (field->key_data) {
2365 if (res && res[0]) {
2366 wpa_printf(MSG_DEBUG, "Do not allow "
2367 "key_data field to be "
2370 return os_strdup("*");
2382 #endif /* NO_CONFIG_WRITE */
2386 * wpa_config_update_psk - Update WPA PSK based on passphrase and SSID
2387 * @ssid: Pointer to network configuration data
2389 * This function must be called to update WPA PSK when either SSID or the
2390 * passphrase has changed for the network configuration.
2392 void wpa_config_update_psk(struct wpa_ssid *ssid)
2394 #ifndef CONFIG_NO_PBKDF2
2395 pbkdf2_sha1(ssid->passphrase, ssid->ssid, ssid->ssid_len, 4096,
2396 ssid->psk, PMK_LEN);
2397 wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
2398 ssid->psk, PMK_LEN);
2400 #endif /* CONFIG_NO_PBKDF2 */
2404 int wpa_config_set_cred(struct wpa_cred *cred, const char *var,
2405 const char *value, int line)
2410 if (os_strcmp(var, "temporary") == 0) {
2411 cred->temporary = atoi(value);
2415 if (os_strcmp(var, "priority") == 0) {
2416 cred->priority = atoi(value);
2420 if (os_strcmp(var, "pcsc") == 0) {
2421 cred->pcsc = atoi(value);
2425 if (os_strcmp(var, "eap") == 0) {
2426 struct eap_method_type method;
2427 method.method = eap_peer_get_type(value, &method.vendor);
2428 if (method.vendor == EAP_VENDOR_IETF &&
2429 method.method == EAP_TYPE_NONE) {
2430 wpa_printf(MSG_ERROR, "Line %d: unknown EAP type '%s' "
2431 "for a credential", line, value);
2434 os_free(cred->eap_method);
2435 cred->eap_method = os_malloc(sizeof(*cred->eap_method));
2436 if (cred->eap_method == NULL)
2438 os_memcpy(cred->eap_method, &method, sizeof(method));
2442 if (os_strcmp(var, "password") == 0 &&
2443 os_strncmp(value, "ext:", 4) == 0) {
2444 os_free(cred->password);
2445 cred->password = os_strdup(value);
2446 cred->ext_password = 1;
2450 if (os_strcmp(var, "update_identifier") == 0) {
2451 cred->update_identifier = atoi(value);
2455 val = wpa_config_parse_string(value, &len);
2457 wpa_printf(MSG_ERROR, "Line %d: invalid field '%s' string "
2458 "value '%s'.", line, var, value);
2462 if (os_strcmp(var, "realm") == 0) {
2463 os_free(cred->realm);
2468 if (os_strcmp(var, "username") == 0) {
2469 os_free(cred->username);
2470 cred->username = val;
2474 if (os_strcmp(var, "password") == 0) {
2475 os_free(cred->password);
2476 cred->password = val;
2477 cred->ext_password = 0;
2481 if (os_strcmp(var, "ca_cert") == 0) {
2482 os_free(cred->ca_cert);
2483 cred->ca_cert = val;
2487 if (os_strcmp(var, "client_cert") == 0) {
2488 os_free(cred->client_cert);
2489 cred->client_cert = val;
2493 if (os_strcmp(var, "private_key") == 0) {
2494 os_free(cred->private_key);
2495 cred->private_key = val;
2499 if (os_strcmp(var, "private_key_passwd") == 0) {
2500 os_free(cred->private_key_passwd);
2501 cred->private_key_passwd = val;
2505 if (os_strcmp(var, "imsi") == 0) {
2506 os_free(cred->imsi);
2511 if (os_strcmp(var, "milenage") == 0) {
2512 os_free(cred->milenage);
2513 cred->milenage = val;
2517 if (os_strcmp(var, "domain_suffix_match") == 0) {
2518 os_free(cred->domain_suffix_match);
2519 cred->domain_suffix_match = val;
2523 if (os_strcmp(var, "domain") == 0) {
2525 new_domain = os_realloc_array(cred->domain,
2526 cred->num_domain + 1,
2528 if (new_domain == NULL) {
2532 new_domain[cred->num_domain++] = val;
2533 cred->domain = new_domain;
2537 if (os_strcmp(var, "phase1") == 0) {
2538 os_free(cred->phase1);
2543 if (os_strcmp(var, "phase2") == 0) {
2544 os_free(cred->phase2);
2549 if (os_strcmp(var, "roaming_consortium") == 0) {
2550 if (len < 3 || len > sizeof(cred->roaming_consortium)) {
2551 wpa_printf(MSG_ERROR, "Line %d: invalid "
2552 "roaming_consortium length %d (3..15 "
2553 "expected)", line, (int) len);
2557 os_memcpy(cred->roaming_consortium, val, len);
2558 cred->roaming_consortium_len = len;
2563 if (os_strcmp(var, "required_roaming_consortium") == 0) {
2564 if (len < 3 || len > sizeof(cred->required_roaming_consortium))
2566 wpa_printf(MSG_ERROR, "Line %d: invalid "
2567 "required_roaming_consortium length %d "
2568 "(3..15 expected)", line, (int) len);
2572 os_memcpy(cred->required_roaming_consortium, val, len);
2573 cred->required_roaming_consortium_len = len;
2578 if (os_strcmp(var, "excluded_ssid") == 0) {
2579 struct excluded_ssid *e;
2581 if (len > MAX_SSID_LEN) {
2582 wpa_printf(MSG_ERROR, "Line %d: invalid "
2583 "excluded_ssid length %d", line, (int) len);
2588 e = os_realloc_array(cred->excluded_ssid,
2589 cred->num_excluded_ssid + 1,
2590 sizeof(struct excluded_ssid));
2595 cred->excluded_ssid = e;
2597 e = &cred->excluded_ssid[cred->num_excluded_ssid++];
2598 os_memcpy(e->ssid, val, len);
2606 if (os_strcmp(var, "roaming_partner") == 0) {
2607 struct roaming_partner *p;
2610 p = os_realloc_array(cred->roaming_partner,
2611 cred->num_roaming_partner + 1,
2612 sizeof(struct roaming_partner));
2617 cred->roaming_partner = p;
2619 p = &cred->roaming_partner[cred->num_roaming_partner];
2621 pos = os_strchr(val, ',');
2627 if (pos - val - 1 >= (int) sizeof(p->fqdn)) {
2631 os_memcpy(p->fqdn, val, pos - val);
2633 p->exact_match = atoi(pos);
2635 pos = os_strchr(pos, ',');
2642 p->priority = atoi(pos);
2644 pos = os_strchr(pos, ',');
2651 if (os_strlen(pos) >= sizeof(p->country)) {
2655 os_memcpy(p->country, pos, os_strlen(pos) + 1);
2657 cred->num_roaming_partner++;
2664 wpa_printf(MSG_ERROR, "Line %d: unknown cred field '%s'.",
2674 struct wpa_cred * wpa_config_get_cred(struct wpa_config *config, int id)
2676 struct wpa_cred *cred;
2678 cred = config->cred;
2689 struct wpa_cred * wpa_config_add_cred(struct wpa_config *config)
2692 struct wpa_cred *cred, *last = NULL;
2695 cred = config->cred;
2704 cred = os_zalloc(sizeof(*cred));
2711 config->cred = cred;
2717 int wpa_config_remove_cred(struct wpa_config *config, int id)
2719 struct wpa_cred *cred, *prev = NULL;
2721 cred = config->cred;
2733 prev->next = cred->next;
2735 config->cred = cred->next;
2737 wpa_config_free_cred(cred);
2742 #ifndef CONFIG_NO_CONFIG_BLOBS
2744 * wpa_config_get_blob - Get a named configuration blob
2745 * @config: Configuration data from wpa_config_read()
2746 * @name: Name of the blob
2747 * Returns: Pointer to blob data or %NULL if not found
2749 const struct wpa_config_blob * wpa_config_get_blob(struct wpa_config *config,
2752 struct wpa_config_blob *blob = config->blobs;
2755 if (os_strcmp(blob->name, name) == 0)
2764 * wpa_config_set_blob - Set or add a named configuration blob
2765 * @config: Configuration data from wpa_config_read()
2766 * @blob: New value for the blob
2768 * Adds a new configuration blob or replaces the current value of an existing
2771 void wpa_config_set_blob(struct wpa_config *config,
2772 struct wpa_config_blob *blob)
2774 wpa_config_remove_blob(config, blob->name);
2775 blob->next = config->blobs;
2776 config->blobs = blob;
2781 * wpa_config_free_blob - Free blob data
2782 * @blob: Pointer to blob to be freed
2784 void wpa_config_free_blob(struct wpa_config_blob *blob)
2787 os_free(blob->name);
2788 os_free(blob->data);
2795 * wpa_config_remove_blob - Remove a named configuration blob
2796 * @config: Configuration data from wpa_config_read()
2797 * @name: Name of the blob to remove
2798 * Returns: 0 if blob was removed or -1 if blob was not found
2800 int wpa_config_remove_blob(struct wpa_config *config, const char *name)
2802 struct wpa_config_blob *pos = config->blobs, *prev = NULL;
2805 if (os_strcmp(pos->name, name) == 0) {
2807 prev->next = pos->next;
2809 config->blobs = pos->next;
2810 wpa_config_free_blob(pos);
2819 #endif /* CONFIG_NO_CONFIG_BLOBS */
2823 * wpa_config_alloc_empty - Allocate an empty configuration
2824 * @ctrl_interface: Control interface parameters, e.g., path to UNIX domain
2826 * @driver_param: Driver parameters
2827 * Returns: Pointer to allocated configuration data or %NULL on failure
2829 struct wpa_config * wpa_config_alloc_empty(const char *ctrl_interface,
2830 const char *driver_param)
2832 struct wpa_config *config;
2833 const int aCWmin = 4, aCWmax = 10;
2834 const struct hostapd_wmm_ac_params ac_bk =
2835 { aCWmin, aCWmax, 7, 0, 0 }; /* background traffic */
2836 const struct hostapd_wmm_ac_params ac_be =
2837 { aCWmin, aCWmax, 3, 0, 0 }; /* best effort traffic */
2838 const struct hostapd_wmm_ac_params ac_vi = /* video traffic */
2839 { aCWmin - 1, aCWmin, 2, 3000 / 32, 0 };
2840 const struct hostapd_wmm_ac_params ac_vo = /* voice traffic */
2841 { aCWmin - 2, aCWmin - 1, 2, 1500 / 32, 0 };
2843 config = os_zalloc(sizeof(*config));
2846 config->eapol_version = DEFAULT_EAPOL_VERSION;
2847 config->ap_scan = DEFAULT_AP_SCAN;
2848 config->fast_reauth = DEFAULT_FAST_REAUTH;
2849 config->p2p_go_intent = DEFAULT_P2P_GO_INTENT;
2850 config->p2p_intra_bss = DEFAULT_P2P_INTRA_BSS;
2851 config->p2p_go_max_inactivity = DEFAULT_P2P_GO_MAX_INACTIVITY;
2852 config->bss_max_count = DEFAULT_BSS_MAX_COUNT;
2853 config->bss_expiration_age = DEFAULT_BSS_EXPIRATION_AGE;
2854 config->bss_expiration_scan_count = DEFAULT_BSS_EXPIRATION_SCAN_COUNT;
2855 config->max_num_sta = DEFAULT_MAX_NUM_STA;
2856 config->access_network_type = DEFAULT_ACCESS_NETWORK_TYPE;
2857 config->scan_cur_freq = DEFAULT_SCAN_CUR_FREQ;
2858 config->wmm_ac_params[0] = ac_be;
2859 config->wmm_ac_params[1] = ac_bk;
2860 config->wmm_ac_params[2] = ac_vi;
2861 config->wmm_ac_params[3] = ac_vo;
2864 config->ctrl_interface = os_strdup(ctrl_interface);
2866 config->driver_param = os_strdup(driver_param);
2872 #ifndef CONFIG_NO_STDOUT_DEBUG
2874 * wpa_config_debug_dump_networks - Debug dump of configured networks
2875 * @config: Configuration data from wpa_config_read()
2877 void wpa_config_debug_dump_networks(struct wpa_config *config)
2880 struct wpa_ssid *ssid;
2882 for (prio = 0; prio < config->num_prio; prio++) {
2883 ssid = config->pssid[prio];
2884 wpa_printf(MSG_DEBUG, "Priority group %d",
2887 wpa_printf(MSG_DEBUG, " id=%d ssid='%s'",
2889 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
2894 #endif /* CONFIG_NO_STDOUT_DEBUG */
2897 struct global_parse_data {
2899 int (*parser)(const struct global_parse_data *data,
2900 struct wpa_config *config, int line, const char *value);
2901 void *param1, *param2, *param3;
2902 unsigned int changed_flag;
2906 static int wpa_global_config_parse_int(const struct global_parse_data *data,
2907 struct wpa_config *config, int line,
2913 dst = (int *) (((u8 *) config) + (long) data->param1);
2914 val = strtol(pos, &end, 0);
2916 wpa_printf(MSG_ERROR, "Line %d: invalid number \"%s\"",
2922 wpa_printf(MSG_DEBUG, "%s=%d", data->name, *dst);
2924 if (data->param2 && *dst < (long) data->param2) {
2925 wpa_printf(MSG_ERROR, "Line %d: too small %s (value=%d "
2926 "min_value=%ld)", line, data->name, *dst,
2927 (long) data->param2);
2928 *dst = (long) data->param2;
2932 if (data->param3 && *dst > (long) data->param3) {
2933 wpa_printf(MSG_ERROR, "Line %d: too large %s (value=%d "
2934 "max_value=%ld)", line, data->name, *dst,
2935 (long) data->param3);
2936 *dst = (long) data->param3;
2944 static int wpa_global_config_parse_str(const struct global_parse_data *data,
2945 struct wpa_config *config, int line,
2951 len = os_strlen(pos);
2952 if (data->param2 && len < (size_t) data->param2) {
2953 wpa_printf(MSG_ERROR, "Line %d: too short %s (len=%lu "
2954 "min_len=%ld)", line, data->name,
2955 (unsigned long) len, (long) data->param2);
2959 if (data->param3 && len > (size_t) data->param3) {
2960 wpa_printf(MSG_ERROR, "Line %d: too long %s (len=%lu "
2961 "max_len=%ld)", line, data->name,
2962 (unsigned long) len, (long) data->param3);
2966 tmp = os_strdup(pos);
2970 dst = (char **) (((u8 *) config) + (long) data->param1);
2973 wpa_printf(MSG_DEBUG, "%s='%s'", data->name, *dst);
2979 static int wpa_config_process_bgscan(const struct global_parse_data *data,
2980 struct wpa_config *config, int line,
2987 tmp = wpa_config_parse_string(pos, &len);
2989 wpa_printf(MSG_ERROR, "Line %d: failed to parse %s",
2994 res = wpa_global_config_parse_str(data, config, line, tmp);
3000 static int wpa_global_config_parse_bin(const struct global_parse_data *data,
3001 struct wpa_config *config, int line,
3005 struct wpabuf **dst, *tmp;
3007 len = os_strlen(pos);
3011 tmp = wpabuf_alloc(len / 2);
3015 if (hexstr2bin(pos, wpabuf_put(tmp, len / 2), len / 2)) {
3020 dst = (struct wpabuf **) (((u8 *) config) + (long) data->param1);
3023 wpa_printf(MSG_DEBUG, "%s", data->name);
3029 static int wpa_config_process_freq_list(const struct global_parse_data *data,
3030 struct wpa_config *config, int line,
3035 freqs = wpa_config_parse_int_array(value);
3038 if (freqs[0] == 0) {
3042 os_free(config->freq_list);
3043 config->freq_list = freqs;
3049 static int wpa_global_config_parse_ipv4(const struct global_parse_data *data,
3050 struct wpa_config *config, int line,
3054 struct hostapd_ip_addr addr;
3056 if (hostapd_parse_ip_addr(pos, &addr) < 0)
3058 if (addr.af != AF_INET)
3061 dst = (u32 *) (((u8 *) config) + (long) data->param1);
3062 os_memcpy(dst, &addr.u.v4.s_addr, 4);
3063 wpa_printf(MSG_DEBUG, "%s = 0x%x", data->name,
3064 WPA_GET_BE32((u8 *) dst));
3068 #endif /* CONFIG_P2P */
3071 static int wpa_config_process_country(const struct global_parse_data *data,
3072 struct wpa_config *config, int line,
3075 if (!pos[0] || !pos[1]) {
3076 wpa_printf(MSG_DEBUG, "Invalid country set");
3079 config->country[0] = pos[0];
3080 config->country[1] = pos[1];
3081 wpa_printf(MSG_DEBUG, "country='%c%c'",
3082 config->country[0], config->country[1]);
3087 static int wpa_config_process_load_dynamic_eap(
3088 const struct global_parse_data *data, struct wpa_config *config,
3089 int line, const char *so)
3092 wpa_printf(MSG_DEBUG, "load_dynamic_eap=%s", so);
3093 ret = eap_peer_method_load(so);
3095 wpa_printf(MSG_DEBUG, "This EAP type was already loaded - not "
3098 wpa_printf(MSG_ERROR, "Line %d: Failed to load dynamic EAP "
3099 "method '%s'.", line, so);
3109 static int wpa_config_process_uuid(const struct global_parse_data *data,
3110 struct wpa_config *config, int line,
3114 if (uuid_str2bin(pos, config->uuid)) {
3115 wpa_printf(MSG_ERROR, "Line %d: invalid UUID", line);
3118 uuid_bin2str(config->uuid, buf, sizeof(buf));
3119 wpa_printf(MSG_DEBUG, "uuid=%s", buf);
3124 static int wpa_config_process_device_type(
3125 const struct global_parse_data *data,
3126 struct wpa_config *config, int line, const char *pos)
3128 return wps_dev_type_str2bin(pos, config->device_type);
3132 static int wpa_config_process_os_version(const struct global_parse_data *data,
3133 struct wpa_config *config, int line,
3136 if (hexstr2bin(pos, config->os_version, 4)) {
3137 wpa_printf(MSG_ERROR, "Line %d: invalid os_version", line);
3140 wpa_printf(MSG_DEBUG, "os_version=%08x",
3141 WPA_GET_BE32(config->os_version));
3146 static int wpa_config_process_wps_vendor_ext_m1(
3147 const struct global_parse_data *data,
3148 struct wpa_config *config, int line, const char *pos)
3151 int len = os_strlen(pos) / 2;
3155 wpa_printf(MSG_ERROR, "Line %d: "
3156 "invalid wps_vendor_ext_m1", line);
3160 tmp = wpabuf_alloc(len);
3162 p = wpabuf_put(tmp, len);
3164 if (hexstr2bin(pos, p, len)) {
3165 wpa_printf(MSG_ERROR, "Line %d: "
3166 "invalid wps_vendor_ext_m1", line);
3171 wpabuf_free(config->wps_vendor_ext_m1);
3172 config->wps_vendor_ext_m1 = tmp;
3174 wpa_printf(MSG_ERROR, "Can not allocate "
3175 "memory for wps_vendor_ext_m1");
3182 #endif /* CONFIG_WPS */
3185 static int wpa_config_process_sec_device_type(
3186 const struct global_parse_data *data,
3187 struct wpa_config *config, int line, const char *pos)
3191 if (config->num_sec_device_types >= MAX_SEC_DEVICE_TYPES) {
3192 wpa_printf(MSG_ERROR, "Line %d: too many sec_device_type "
3197 idx = config->num_sec_device_types;
3199 if (wps_dev_type_str2bin(pos, config->sec_device_type[idx]))
3202 config->num_sec_device_types++;
3207 static int wpa_config_process_p2p_pref_chan(
3208 const struct global_parse_data *data,
3209 struct wpa_config *config, int line, const char *pos)
3211 struct p2p_channel *pref = NULL, *n;
3212 unsigned int num = 0;
3216 /* format: class:chan,class:chan,... */
3219 op_class = atoi(pos);
3220 pos2 = os_strchr(pos, ':');
3226 n = os_realloc_array(pref, num + 1,
3227 sizeof(struct p2p_channel));
3231 pref[num].op_class = op_class;
3232 pref[num].chan = chan;
3235 pos = os_strchr(pos2, ',');
3241 os_free(config->p2p_pref_chan);
3242 config->p2p_pref_chan = pref;
3243 config->num_p2p_pref_chan = num;
3244 wpa_hexdump(MSG_DEBUG, "P2P: Preferred class/channel pairs",
3245 (u8 *) config->p2p_pref_chan,
3246 config->num_p2p_pref_chan * sizeof(struct p2p_channel));
3252 wpa_printf(MSG_ERROR, "Line %d: Invalid p2p_pref_chan list", line);
3257 static int wpa_config_process_p2p_no_go_freq(
3258 const struct global_parse_data *data,
3259 struct wpa_config *config, int line, const char *pos)
3263 ret = freq_range_list_parse(&config->p2p_no_go_freq, pos);
3265 wpa_printf(MSG_ERROR, "Line %d: Invalid p2p_no_go_freq", line);
3269 wpa_printf(MSG_DEBUG, "P2P: p2p_no_go_freq with %u items",
3270 config->p2p_no_go_freq.num);
3275 #endif /* CONFIG_P2P */
3278 static int wpa_config_process_hessid(
3279 const struct global_parse_data *data,
3280 struct wpa_config *config, int line, const char *pos)
3282 if (hwaddr_aton2(pos, config->hessid) < 0) {
3283 wpa_printf(MSG_ERROR, "Line %d: Invalid hessid '%s'",
3292 static int wpa_config_process_sae_groups(
3293 const struct global_parse_data *data,
3294 struct wpa_config *config, int line, const char *pos)
3296 int *groups = wpa_config_parse_int_array(pos);
3297 if (groups == NULL) {
3298 wpa_printf(MSG_ERROR, "Line %d: Invalid sae_groups '%s'",
3303 os_free(config->sae_groups);
3304 config->sae_groups = groups;
3310 static int wpa_config_process_ap_vendor_elements(
3311 const struct global_parse_data *data,
3312 struct wpa_config *config, int line, const char *pos)
3315 int len = os_strlen(pos) / 2;
3319 wpa_printf(MSG_ERROR, "Line %d: invalid ap_vendor_elements",
3324 tmp = wpabuf_alloc(len);
3326 p = wpabuf_put(tmp, len);
3328 if (hexstr2bin(pos, p, len)) {
3329 wpa_printf(MSG_ERROR, "Line %d: invalid "
3330 "ap_vendor_elements", line);
3335 wpabuf_free(config->ap_vendor_elements);
3336 config->ap_vendor_elements = tmp;
3338 wpa_printf(MSG_ERROR, "Cannot allocate memory for "
3339 "ap_vendor_elements");
3347 #ifdef CONFIG_CTRL_IFACE
3348 static int wpa_config_process_no_ctrl_interface(
3349 const struct global_parse_data *data,
3350 struct wpa_config *config, int line, const char *pos)
3352 wpa_printf(MSG_DEBUG, "no_ctrl_interface -> ctrl_interface=NULL");
3353 os_free(config->ctrl_interface);
3354 config->ctrl_interface = NULL;
3357 #endif /* CONFIG_CTRL_IFACE */
3363 /* OFFSET: Get offset of a variable within the wpa_config structure */
3364 #define OFFSET(v) ((void *) &((struct wpa_config *) 0)->v)
3366 #define FUNC(f) #f, wpa_config_process_ ## f, OFFSET(f), NULL, NULL
3367 #define FUNC_NO_VAR(f) #f, wpa_config_process_ ## f, NULL, NULL, NULL
3368 #define _INT(f) #f, wpa_global_config_parse_int, OFFSET(f)
3369 #define INT(f) _INT(f), NULL, NULL
3370 #define INT_RANGE(f, min, max) _INT(f), (void *) min, (void *) max
3371 #define _STR(f) #f, wpa_global_config_parse_str, OFFSET(f)
3372 #define STR(f) _STR(f), NULL, NULL
3373 #define STR_RANGE(f, min, max) _STR(f), (void *) min, (void *) max
3374 #define BIN(f) #f, wpa_global_config_parse_bin, OFFSET(f), NULL, NULL
3375 #define IPV4(f) #f, wpa_global_config_parse_ipv4, OFFSET(f), NULL, NULL
3377 static const struct global_parse_data global_fields[] = {
3378 #ifdef CONFIG_CTRL_IFACE
3379 { STR(ctrl_interface), 0 },
3380 { FUNC_NO_VAR(no_ctrl_interface), 0 },
3381 { STR(ctrl_interface_group), 0 } /* deprecated */,
3382 #endif /* CONFIG_CTRL_IFACE */
3383 { INT_RANGE(eapol_version, 1, 2), 0 },
3384 { INT(ap_scan), 0 },
3385 { FUNC(bgscan), 0 },
3386 { INT(disable_scan_offload), 0 },
3387 { INT(fast_reauth), 0 },
3388 { STR(opensc_engine_path), 0 },
3389 { STR(pkcs11_engine_path), 0 },
3390 { STR(pkcs11_module_path), 0 },
3391 { STR(pcsc_reader), 0 },
3392 { STR(pcsc_pin), 0 },
3393 { INT(external_sim), 0 },
3394 { STR(driver_param), 0 },
3395 { INT(dot11RSNAConfigPMKLifetime), 0 },
3396 { INT(dot11RSNAConfigPMKReauthThreshold), 0 },
3397 { INT(dot11RSNAConfigSATimeout), 0 },
3398 #ifndef CONFIG_NO_CONFIG_WRITE
3399 { INT(update_config), 0 },
3400 #endif /* CONFIG_NO_CONFIG_WRITE */
3401 { FUNC_NO_VAR(load_dynamic_eap), 0 },
3403 { FUNC(uuid), CFG_CHANGED_UUID },
3404 { STR_RANGE(device_name, 0, 32), CFG_CHANGED_DEVICE_NAME },
3405 { STR_RANGE(manufacturer, 0, 64), CFG_CHANGED_WPS_STRING },
3406 { STR_RANGE(model_name, 0, 32), CFG_CHANGED_WPS_STRING },
3407 { STR_RANGE(model_number, 0, 32), CFG_CHANGED_WPS_STRING },
3408 { STR_RANGE(serial_number, 0, 32), CFG_CHANGED_WPS_STRING },
3409 { FUNC(device_type), CFG_CHANGED_DEVICE_TYPE },
3410 { FUNC(os_version), CFG_CHANGED_OS_VERSION },
3411 { STR(config_methods), CFG_CHANGED_CONFIG_METHODS },
3412 { INT_RANGE(wps_cred_processing, 0, 2), 0 },
3413 { FUNC(wps_vendor_ext_m1), CFG_CHANGED_VENDOR_EXTENSION },
3414 #endif /* CONFIG_WPS */
3416 { FUNC(sec_device_type), CFG_CHANGED_SEC_DEVICE_TYPE },
3417 { INT(p2p_listen_reg_class), 0 },
3418 { INT(p2p_listen_channel), 0 },
3419 { INT(p2p_oper_reg_class), CFG_CHANGED_P2P_OPER_CHANNEL },
3420 { INT(p2p_oper_channel), CFG_CHANGED_P2P_OPER_CHANNEL },
3421 { INT_RANGE(p2p_go_intent, 0, 15), 0 },
3422 { STR(p2p_ssid_postfix), CFG_CHANGED_P2P_SSID_POSTFIX },
3423 { INT_RANGE(persistent_reconnect, 0, 1), 0 },
3424 { INT_RANGE(p2p_intra_bss, 0, 1), CFG_CHANGED_P2P_INTRA_BSS },
3425 { INT(p2p_group_idle), 0 },
3426 { FUNC(p2p_pref_chan), CFG_CHANGED_P2P_PREF_CHAN },
3427 { FUNC(p2p_no_go_freq), CFG_CHANGED_P2P_PREF_CHAN },
3428 { INT_RANGE(p2p_add_cli_chan, 0, 1), 0 },
3429 { INT(p2p_go_ht40), 0 },
3430 { INT(p2p_go_vht), 0 },
3431 { INT(p2p_disabled), 0 },
3432 { INT(p2p_no_group_iface), 0 },
3433 { INT_RANGE(p2p_ignore_shared_freq, 0, 1), 0 },
3434 { IPV4(ip_addr_go), 0 },
3435 { IPV4(ip_addr_mask), 0 },
3436 { IPV4(ip_addr_start), 0 },
3437 { IPV4(ip_addr_end), 0 },
3438 #endif /* CONFIG_P2P */
3439 { FUNC(country), CFG_CHANGED_COUNTRY },
3440 { INT(bss_max_count), 0 },
3441 { INT(bss_expiration_age), 0 },
3442 { INT(bss_expiration_scan_count), 0 },
3443 { INT_RANGE(filter_ssids, 0, 1), 0 },
3444 { INT_RANGE(filter_rssi, -100, 0), 0 },
3445 { INT(max_num_sta), 0 },
3446 { INT_RANGE(disassoc_low_ack, 0, 1), 0 },
3448 { INT_RANGE(hs20, 0, 1), 0 },
3449 #endif /* CONFIG_HS20 */
3450 { INT_RANGE(interworking, 0, 1), 0 },
3451 { FUNC(hessid), 0 },
3452 { INT_RANGE(access_network_type, 0, 15), 0 },
3453 { INT_RANGE(pbc_in_m1, 0, 1), 0 },
3454 { STR(autoscan), 0 },
3455 { INT_RANGE(wps_nfc_dev_pw_id, 0x10, 0xffff),
3456 CFG_CHANGED_NFC_PASSWORD_TOKEN },
3457 { BIN(wps_nfc_dh_pubkey), CFG_CHANGED_NFC_PASSWORD_TOKEN },
3458 { BIN(wps_nfc_dh_privkey), CFG_CHANGED_NFC_PASSWORD_TOKEN },
3459 { BIN(wps_nfc_dev_pw), CFG_CHANGED_NFC_PASSWORD_TOKEN },
3460 { STR(ext_password_backend), CFG_CHANGED_EXT_PW_BACKEND },
3461 { INT(p2p_go_max_inactivity), 0 },
3462 { INT_RANGE(auto_interworking, 0, 1), 0 },
3465 { FUNC(sae_groups), 0 },
3466 { INT(dtim_period), 0 },
3467 { INT(beacon_int), 0 },
3468 { FUNC(ap_vendor_elements), 0 },
3469 { INT_RANGE(ignore_old_scan_res, 0, 1), 0 },
3470 { FUNC(freq_list), 0 },
3471 { INT(scan_cur_freq), 0 },
3472 { INT(sched_scan_interval), 0 },
3473 { INT(tdls_external_control), 0},
3474 { STR(osu_dir), 0 },
3486 #define NUM_GLOBAL_FIELDS ARRAY_SIZE(global_fields)
3489 int wpa_config_process_global(struct wpa_config *config, char *pos, int line)
3494 for (i = 0; i < NUM_GLOBAL_FIELDS; i++) {
3495 const struct global_parse_data *field = &global_fields[i];
3496 size_t flen = os_strlen(field->name);
3497 if (os_strncmp(pos, field->name, flen) != 0 ||
3501 if (field->parser(field, config, line, pos + flen + 1)) {
3502 wpa_printf(MSG_ERROR, "Line %d: failed to "
3503 "parse '%s'.", line, pos);
3506 if (field->changed_flag == CFG_CHANGED_NFC_PASSWORD_TOKEN)
3507 config->wps_nfc_pw_from_config = 1;
3508 config->changed_parameters |= field->changed_flag;
3511 if (i == NUM_GLOBAL_FIELDS) {
3513 if (os_strncmp(pos, "wmm_ac_", 7) == 0) {
3514 char *tmp = os_strchr(pos, '=');
3518 wpa_printf(MSG_ERROR, "Line %d: invalid line "
3523 if (hostapd_config_wmm_ac(config->wmm_ac_params, pos,
3525 wpa_printf(MSG_ERROR, "Line %d: invalid WMM "
3530 #endif /* CONFIG_AP */
3533 wpa_printf(MSG_ERROR, "Line %d: unknown global field '%s'.",